windows-version-1-5-20050817
[openafs.git] / src / config / stds.h
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #ifndef OPENAFS_AFS_CONFIG_STDS_H
11 #define OPENAFS_AFS_CONFIG_STDS_H       1
12
13 #include <afs/param.h>
14 #include <sys/types.h>
15
16 #define IN                      /* indicates a parameter is read in */
17 #define OUT                     /* indicates a parameter is sent out (a ptr) */
18 #define INOUT                   /* indicates a parameter is read in and sent out (a ptr) */
19
20 #ifndef MACRO_BEGIN
21 #define MACRO_BEGIN     do {
22 #endif
23 #ifndef MACRO_END
24 #define MACRO_END       } while (0)
25 #endif
26
27 typedef void *opaque;
28
29 #ifndef _ATT4
30 #if defined(__HIGHC__)
31 /*
32  * keep HC from complaining about the use of "old-style" function definitions
33  * with prototypes
34  */
35 pragma Off(Prototype_override_warnings);
36 #endif /* defined(__HIGHC__) */
37 #endif
38
39 /* Now some types to enhance portability.  Always use these on the wire or when
40  * laying out shared structures on disk. */
41
42 /* Imagine that this worked...
43 #if (sizeof(long) != 4) || (sizeof(short) != 2)
44 #error We require size of long and pointers to be equal
45 #endif */
46
47 typedef short afs_int16;
48 typedef unsigned short afs_uint16;
49 #ifdef  AFS_64BIT_ENV
50 typedef int afs_int32;
51 typedef unsigned int afs_uint32;
52 #if defined(AFS_NT40_ENV) && defined(_MSC_VER)
53 typedef __int64 afs_int64;
54 typedef unsigned __int64 afs_uint64;
55 #else
56 typedef long long afs_int64;
57 typedef unsigned long long afs_uint64;
58 #endif
59 #define ZeroInt64(a)       (a) = 0
60 #define AssignInt64(a, b) *(b) = (a) 
61 #define AddInt64(a,b,c) *(c) = (afs_int64)(a) + (afs_int64)(b)
62 #define AddUInt64(a,b,c) *(c) = (afs_uint64)(a) + (afs_uint64)(b)
63 #define SubtractInt64(a,b,c) *(c) = (afs_int64)(a) - (afs_int64)(b)
64 #define SubtractUInt64(a,b,c) *(c) = (afs_uint64)(a) - (afs_uint64)(b)
65 #define CompareInt64(a,b) (afs_int64)(a) - (afs_int64)(b)
66 #define CompareUInt64(a,b) (afs_uint64)(a) - (afs_uint64)(b)
67 #define NonZeroInt64(a)                (a)
68 #define Int64ToInt32(a)    (a) & 0xFFFFFFFFL
69 #define FillInt64(t,h,l) (t) = (h); (t) <<= 32; (t) |= (l);
70 #define SplitInt64(t,h,l) (h) = (t) >> 32; (l) = (t) & 0xFFFFFFFF;
71 #else /* AFS_64BIT_ENV */
72 typedef long afs_int32;
73 typedef unsigned long afs_uint32;
74
75 struct Int64 {
76     afs_int32 high;
77     afs_uint32 low;
78 };
79 typedef struct Int64 afs_int64;
80
81 struct u_Int64 {
82     afs_uint32 high;
83     afs_uint32 low;
84 };
85 typedef struct u_Int64 afs_uint64;
86 #define ZeroInt64(a) (a).high = (a).low = 0
87 #define AssignInt64(a, b) (b)->high = (a).high; (b)->low = (a).low
88 #define CompareInt64(a,b) (((afs_int32)(a).high - (afs_int32)(b).high) || (((a).high == (b).high) && ((a).low - (b).low))) 
89 #define AddInt64(a, b, c) {  afs_int64 _a, _b; _a = a; _b = b; (c)->low = _a.low + _b.low; (c)->high = _a.high + _b.high + ((c)->low < _b.low); } 
90 #define SubtractInt64(a, b, c) { afs_int64 _a, _b; _a = a; _b = b; (c)->low = _a.low - _b.low;  (c)->high = _a.high - _b.high - (_a.low < _b.low); } 
91 #define CompareUInt64(a,b) (((afs_uint32)(a).high - (afs_uint32)(b).high) || (((a).high == (b).high) && ((a).low - (b).low))) 
92 #define AddUInt64(a, b, c) {  afs_uint64 _a, _b; _a = a; _b = b; (c)->low = _a.low + _b.low; (c)->high = _a.high + _b.high + ((c)->low < _b.low); } 
93 #define SubtractUInt64(a, b, c) { afs_uint64 _a, _b; _a = a; _b = b; (c)->low = _a.low - _b.low;  (c)->high = _a.high - _b.high - (_a.low < _b.low); } 
94 #define NonZeroInt64(a)   (a).low || (a).high
95 #define Int64ToInt32(a)    (a).low
96 #define FillInt64(t,h,l) (t).high = (h); (t).low = (l);
97 #define SplitInt64(t,h,l) (h) = (t).high; (l) = (t).low;
98 #endif /* AFS_64BIT_ENV */
99
100 /* AFS_64BIT_CLIENT should presently be set only for AFS_64BIT_ENV systems */
101
102 #ifdef AFS_64BIT_CLIENT
103 typedef afs_int64 afs_size_t;
104 typedef afs_uint64 afs_offs_t;
105 #else /* AFS_64BIT_CLIENT */
106 typedef afs_int32 afs_size_t;
107 typedef afs_uint32 afs_offs_t;
108 #endif /* AFS_64BIT_CLIENT */
109
110 #ifdef AFS_LARGEFILE_ENV
111 typedef afs_int64 afs_foff_t;
112 typedef afs_uint64 afs_fsize_t;
113 typedef afs_int64 afs_sfsize_t;
114 #define SplitOffsetOrSize(t,h,l) SplitInt64(t,h,l)
115 #else /* !AFS_LARGEFILE_ENV */
116 typedef afs_int32 afs_foff_t;
117 typedef afs_uint32 afs_fsize_t;
118 typedef afs_int32 afs_sfsize_t;
119 #define SplitOffsetOrSize(t,h,l) (h) = 0; (l) = (t);
120 #endif /* !AFS_LARGEFILE_ENV */
121
122 /* Maximum integer sizes.  Also what is expected by %lld, %llu in
123  * afs_snprintf. */
124 #ifdef AFS_64BIT_CLIENT
125 typedef afs_int64 afs_intmax_t;
126 typedef afs_uint64 afs_uintmax_t;
127 #else /* !AFS_64BIT_CLIENT */
128 typedef afs_int32 afs_intmax_t;
129 typedef afs_uint32 afs_uintmax_t;
130 #endif /* !AFS_64BIT_CLIENT */
131
132 /* you still have to include <netinet/in.h> to make these work */
133
134 #define hton32 htonl
135 #define hton16 htons
136 #define ntoh32 ntohl
137 #define ntoh16 ntohs
138
139
140 /* Since there is going to be considerable use of 64 bit integers we provide
141  * some assistence in this matter.  The hyper type is supposed to be compatible
142  * with the afsHyper type: the same macros will work on both. */
143
144 #if     defined(AFS_64BIT_ENV) && 0
145
146 typedef unsigned long afs_hyper_t;
147
148 #define hcmp(a,b)       ((a) < (b) ? -1 : ((a) > (b) ? 1 : 0))
149 #define hsame(a,b)      ((a) == (b))
150 #define hiszero(a)      ((a) == 0)
151 #define hfitsin32(a)    ((a) & 0xffffffff00000000) == 0)
152 #define hset(a,b)       ((a) = (b))
153 #define hzero(a)        ((a) = 0)
154 #define hones(a)        ((a) = ~((unsigned long)0))
155 #define hget32(i,a)     ((i) = (unsigned int)(a))
156 #define hget64(hi,lo,a) ((lo) = ((unsigned int)(a)), (hi) = ((a) & (0xffffffff00000000)))
157 #define hset32(a,i)     ((a) = ((unsigned int)(i)))
158 #define hset64(a,hi,lo) ((a) = (((hi) << 32) | (lo)))
159 #define hgetlo(a)       ((a) & 0xffffffff)
160 #define hgethi(a)       (((unsigned int)(a)) >> 32)
161 #define hadd(a,b)       ((a) += (b))
162 /* XXX */
163 #define hadd32(a,b)     ((a) += (b))
164 #define hshlft(a,n)     ((a)<<(n))
165
166 #else /* AFS_64BIT_ENV */
167
168 typedef struct afs_hyper_t {    /* unsigned 64 bit integers */
169     unsigned int high;
170     unsigned int low;
171 } afs_hyper_t;
172
173 #define hcmp(a,b) ((a).high<(b).high? -1 : ((a).high > (b).high? 1 : \
174     ((a).low <(b).low? -1 : ((a).low > (b).low? 1 : 0))))
175 #define hsame(a,b) ((a).low == (b).low && (a).high == (b).high)
176 #define hiszero(a) ((a).low == 0 && (a).high == 0)
177 #define hfitsin32(a) ((a).high == 0)
178 #define hset(a,b) ((a) = (b))
179 #define hzero(a) ((a).low = 0, (a).high = 0)
180 #define hones(a) ((a).low = 0xffffffff, (a).high = 0xffffffff)
181 #define hget32(i,a) ((i) = (a).low)
182 #define hget64(hi,lo,a) ((lo) = (a).low, (hi) = (a).high)
183 #define hset32(a,i) ((a).high = 0, (a).low = (i))
184 #define hset64(a,hi,lo) ((a).high = (hi), (a).low = (lo))
185 #define hgetlo(a) ((a).low)
186 #define hgethi(a) ((a).high)
187 #define hshlft(a,n)                                                        \
188      { /*Shift Left n bits*/                                               \
189         int s = sizeof((a).low) * 8; /*#bits*/                             \
190         if ((n) <= 0) { /*No shift*/                                       \
191         } else if ((n) >= 2*s) { /*Shift off all bits*/                    \
192            (a).high = (a).low = 0;                                         \
193         } else if ((n) < s) { /*Part of low shifted into high*/            \
194            (a).high = ((a).high<<(n)) | (((a).low>>(s-(n))) & (1<<(n))-1); \
195            (a).low  = (a).low << (n);                                      \
196         } else if ((n) >= s) { /*All of low shifted into high plus some*/  \
197            (a).high = (a).low << ((n)-s);                                  \
198            (a).low=0;                                                      \
199         }                                                                  \
200      }
201
202 /* The algorithm here is to check for two cases that cause a carry.  If the top
203  * two bits are different then if the sum has the top bit off then there must
204  * have been a carry.  If the top bits are both one then there is always a
205  * carry.  We assume 32 bit ints and twos complement arithmetic. */
206
207 #define SIGN 0x80000000
208 #define hadd32(a,i) \
209     (((((a).low ^ (int)(i)) & SIGN) \
210       ? (((((a).low + (int)(i)) & SIGN) == 0) && (a).high++) \
211       : (((a).low & (int)(i) & SIGN) && (a).high++)), \
212      (a).low += (int)(i))
213
214 #define hadd(a,b) (hadd32(a,(b).low), (a).high += (b).high)
215 #endif /* AFS_64BIT_ENV */
216
217 #ifndef KERNEL
218 #ifndef AFS_NT40_ENV
219 #define max(a, b)               ((a) < (b) ? (b) : (a))
220 #define min(a, b)               ((a) > (b) ? (b) : (a))
221 #endif
222 /*#define abs(x)                  ((x) >= 0 ? (x) : -(x))*/
223 #endif
224
225 /* minumum length of string to pass to int_to_base64 */
226 typedef char b64_string_t[8];
227 #ifndef AFS_NT40_ENV
228 #if defined(AFS_HPUX_ENV) || defined(AFS_USR_HPUX_ENV) || (defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV))
229 char *int_to_base64();
230 int base64_to_int();
231 #else
232 char *int_to_base64(b64_string_t s, int a);
233 int base64_to_int(char *s);
234 #endif
235 #endif /* AFS_NT40_ENV */
236 /*
237  * The afsUUID data type is built in to RX
238  */
239 struct afsUUID {
240     afs_uint32 time_low;
241     afs_uint16 time_mid;
242     afs_uint16 time_hi_and_version;
243     char clock_seq_hi_and_reserved;
244     char clock_seq_low;
245     char node[6];
246 };
247 typedef struct afsUUID afsUUID;
248
249 #endif /* OPENAFS_CONFIG_AFS_STDS_H */