1 /* 2 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 3 * Use is subject to license terms. 4 */ 5 6 #pragma ident "%Z%%M% %I% %E% SMI" 7 8 /* 9 * k5-platform.h 10 * 11 * Copyright 2003 by the Massachusetts Institute of Technology. 12 * All Rights Reserved. 13 * 14 * Export of this software from the United States of America may 15 * require a specific license from the United States Government. 16 * It is the responsibility of any person or organization contemplating 17 * export to obtain such a license before exporting. 18 * 19 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and 20 * distribute this software and its documentation for any purpose and 21 * without fee is hereby granted, provided that the above copyright 22 * notice appear in all copies and that both that copyright notice and 23 * this permission notice appear in supporting documentation, and that 24 * the name of M.I.T. not be used in advertising or publicity pertaining 25 * to distribution of the software without specific, written prior 26 * permission. Furthermore if you modify this software you must label 27 * your software as modified software and not distribute it in such a 28 * fashion that it might be confused with the original M.I.T. software. 29 * M.I.T. makes no representations about the suitability of 30 * this software for any purpose. It is provided "as is" without express 31 * or implied warranty. 32 * 33 * 34 * Some platform-dependent definitions to sync up the C support level. 35 * Some to a C99-ish level, some related utility code. 36 * 37 * Currently: make "static" work; 64-bit types and load/store 38 * code; SIZE_MAX. 39 */ 40 41 #ifndef K5_PLATFORM_H 42 #define K5_PLATFORM_H 43 44 /* 64-bit support: krb5_ui_8 and krb5_int64. 45 #include "autoconf.h" 46 47 This should move to krb5.h eventually, but without the namespace 48 pollution from the autoconf macros. */ 49 #if defined(HAVE_STDINT_H) || defined(HAVE_INTTYPES_H) 50 # ifdef HAVE_STDINT_H 51 # include <stdint.h> 52 # endif 53 # ifdef HAVE_INTTYPES_H 54 # include <inttypes.h> 55 # endif 56 # define INT64_TYPE int64_t 57 # define UINT64_TYPE uint64_t 58 #elif defined(_WIN32) 59 # define INT64_TYPE signed __int64 60 # define UINT64_TYPE unsigned __int64 61 #else /* not Windows, and neither stdint.h nor inttypes.h */ 62 # define INT64_TYPE signed long long 63 # define UINT64_TYPE unsigned long long 64 #endif 65 66 #ifndef SIZE_MAX 67 # define SIZE_MAX ((size_t)((size_t)0 - 1)) 68 #endif 69 70 /* Read and write integer values as (unaligned) octet strings in 71 specific byte orders. 72 73 Add per-platform optimizations later if needed. (E.g., maybe x86 74 unaligned word stores and gcc/asm instructions for byte swaps, 75 etc.) */ 76 77 static void 78 store_16_be (unsigned int val, unsigned char *p) 79 { 80 p[0] = (val >> 8) & 0xff; 81 p[1] = (val ) & 0xff; 82 } 83 static void 84 store_16_le (unsigned int val, unsigned char *p) 85 { 86 p[1] = (val >> 8) & 0xff; 87 p[0] = (val ) & 0xff; 88 } 89 static void 90 store_32_be (unsigned int val, unsigned char *p) 91 { 92 p[0] = (val >> 24) & 0xff; 93 p[1] = (val >> 16) & 0xff; 94 p[2] = (val >> 8) & 0xff; 95 p[3] = (val ) & 0xff; 96 } 97 static void 98 store_32_le (unsigned int val, unsigned char *p) 99 { 100 p[3] = (val >> 24) & 0xff; 101 p[2] = (val >> 16) & 0xff; 102 p[1] = (val >> 8) & 0xff; 103 p[0] = (val ) & 0xff; 104 } 105 static void 106 store_64_be (UINT64_TYPE val, unsigned char *p) 107 { 108 p[0] = (unsigned char)((val >> 56) & 0xff); 109 p[1] = (unsigned char)((val >> 48) & 0xff); 110 p[2] = (unsigned char)((val >> 40) & 0xff); 111 p[3] = (unsigned char)((val >> 32) & 0xff); 112 p[4] = (unsigned char)((val >> 24) & 0xff); 113 p[5] = (unsigned char)((val >> 16) & 0xff); 114 p[6] = (unsigned char)((val >> 8) & 0xff); 115 p[7] = (unsigned char)((val ) & 0xff); 116 } 117 static void 118 store_64_le (UINT64_TYPE val, unsigned char *p) 119 { 120 p[7] = (unsigned char)((val >> 56) & 0xff); 121 p[6] = (unsigned char)((val >> 48) & 0xff); 122 p[5] = (unsigned char)((val >> 40) & 0xff); 123 p[4] = (unsigned char)((val >> 32) & 0xff); 124 p[3] = (unsigned char)((val >> 24) & 0xff); 125 p[2] = (unsigned char)((val >> 16) & 0xff); 126 p[1] = (unsigned char)((val >> 8) & 0xff); 127 p[0] = (unsigned char)((val ) & 0xff); 128 } 129 static unsigned short 130 load_16_be (unsigned char *p) 131 { 132 return (p[1] | (p[0] << 8)); 133 } 134 static unsigned short 135 load_16_le (unsigned char *p) 136 { 137 return (p[0] | (p[1] << 8)); 138 } 139 static unsigned int 140 load_32_be (unsigned char *p) 141 { 142 return (p[3] | (p[2] << 8) | (p[1] << 16) | (p[0] << 24)); 143 } 144 static unsigned int 145 load_32_le (unsigned char *p) 146 { 147 return (p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24)); 148 } 149 static UINT64_TYPE 150 load_64_be (unsigned char *p) 151 { 152 return ((UINT64_TYPE)load_32_be(p) << 32) | load_32_be(p+4); 153 } 154 static UINT64_TYPE 155 load_64_le (unsigned char *p) 156 { 157 return ((UINT64_TYPE)load_32_le(p+4) << 32) | load_32_le(p); 158 } 159 160 #endif /* K5_PLATFORM_H */ 161