48f84687976453d0200b4df839797e81a413a7ae
[krb5.git] / src / include / k5-platform.h
1 /* Copyright 2003 Massachusetts Institute of Technology.  All rights reserved.  */
2 /* Platform-dependent junk.  */
3
4 #ifndef K5_PLATFORM_H
5 #define K5_PLATFORM_H
6
7 #if !defined(inline)
8 # if __STDC_VERSION__ >= 199901L
9 /* C99 supports inline, don't do anything.  */
10 # elif defined(__GNUC__)
11 #  define inline __inline__ /* this form silences -pedantic warnings */
12 # elif defined(__mips) && defined(__sgi)
13 #  define inline __inline /* IRIX used at MIT does inline but not c99 yet */
14 # elif defined(__sun) && __SUNPRO_C >= 0x540
15 /* The Forte Developer 7 C compiler supports "inline".  */
16 # elif defined(_WIN32)
17 #  define inline __inline
18 # else
19 #  define inline /* nothing, just static */
20 # endif
21 #endif
22
23 #include "autoconf.h"
24
25 /* 64-bit support: krb5_ui_8 and krb5_int64.
26
27    This should move to krb5.h eventually, but without the namespace
28    pollution from the autoconf macros.  */
29 #if defined(HAVE_STDINT_H) || defined(HAVE_INTTYPES_H)
30 # ifdef HAVE_STDINT_H
31 #  include <stdint.h>
32 # endif
33 # ifdef HAVE_INTTYPES_H
34 #  include <inttypes.h>
35 # endif
36 # define INT64_TYPE int64_t
37 # define UINT64_TYPE uint64_t
38 #elif defined(_WIN32)
39 # define INT64_TYPE signed __int64
40 # define UINT64_TYPE unsigned __int64
41 #else /* not Windows, and neither stdint.h nor inttypes.h */
42 # define INT64_TYPE signed long long
43 # define UINT64_TYPE unsigned long long
44 #endif
45
46 /* Read and write integer values as (unaligned) octet strings in
47    specific byte orders.
48
49    Add per-platform optimizations later if needed.  (E.g., maybe x86
50    unaligned word stores and gcc/asm instructions for byte swaps,
51    etc.)  */
52
53 static inline void
54 store_16_be (unsigned int val, unsigned char *p)
55 {
56     p[0] = (val >>  8) & 0xff;
57     p[1] = (val      ) & 0xff;
58 }
59 static inline void
60 store_16_le (unsigned int val, unsigned char *p)
61 {
62     p[1] = (val >>  8) & 0xff;
63     p[0] = (val      ) & 0xff;
64 }
65 static inline void
66 store_32_be (unsigned int val, unsigned char *p)
67 {
68     p[0] = (val >> 24) & 0xff;
69     p[1] = (val >> 16) & 0xff;
70     p[2] = (val >>  8) & 0xff;
71     p[3] = (val      ) & 0xff;
72 }
73 static inline void
74 store_32_le (unsigned int val, unsigned char *p)
75 {
76     p[3] = (val >> 24) & 0xff;
77     p[2] = (val >> 16) & 0xff;
78     p[1] = (val >>  8) & 0xff;
79     p[0] = (val      ) & 0xff;
80 }
81 static inline void
82 store_64_be (UINT64_TYPE val, unsigned char *p)
83 {
84     p[0] = (unsigned char)((val >> 56) & 0xff);
85     p[1] = (unsigned char)((val >> 48) & 0xff);
86     p[2] = (unsigned char)((val >> 40) & 0xff);
87     p[3] = (unsigned char)((val >> 32) & 0xff);
88     p[4] = (unsigned char)((val >> 24) & 0xff);
89     p[5] = (unsigned char)((val >> 16) & 0xff);
90     p[6] = (unsigned char)((val >>  8) & 0xff);
91     p[7] = (unsigned char)((val      ) & 0xff);
92 }
93 static inline void
94 store_64_le (UINT64_TYPE val, unsigned char *p)
95 {
96     p[7] = (unsigned char)((val >> 56) & 0xff);
97     p[6] = (unsigned char)((val >> 48) & 0xff);
98     p[5] = (unsigned char)((val >> 40) & 0xff);
99     p[4] = (unsigned char)((val >> 32) & 0xff);
100     p[3] = (unsigned char)((val >> 24) & 0xff);
101     p[2] = (unsigned char)((val >> 16) & 0xff);
102     p[1] = (unsigned char)((val >>  8) & 0xff);
103     p[0] = (unsigned char)((val      ) & 0xff);
104 }
105 static inline unsigned short
106 load_16_be (unsigned char *p)
107 {
108     return (p[1] | (p[0] << 8));
109 }
110 static inline unsigned short
111 load_16_le (unsigned char *p)
112 {
113     return (p[0] | (p[1] << 8));
114 }
115 static inline unsigned int
116 load_32_be (unsigned char *p)
117 {
118     return (p[3] | (p[2] << 8) | (p[1] << 16) | (p[0] << 24));
119 }
120 static inline unsigned int
121 load_32_le (unsigned char *p)
122 {
123     return (p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24));
124 }
125 static inline UINT64_TYPE
126 load_64_be (unsigned char *p)
127 {
128     return ((UINT64_TYPE)load_32_be(p) << 32) | load_32_be(p+4);
129 }
130 static inline UINT64_TYPE
131 load_64_le (unsigned char *p)
132 {
133     return ((UINT64_TYPE)load_32_le(p+4) << 32) | load_32_le(p);
134 }
135
136 #endif /* K5_PLATFORM_H */