1b03c590c124d1e899c9957e22e7d8d2df6689e0
[krb5.git] / src / include / k5-platform.h
1 /*
2  * k5-platform.h
3  *
4  * Copyright 2003, 2004, 2005 Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
7  * Export of this software from the United States of America may
8  *   require a specific license from the United States Government.
9  *   It is the responsibility of any person or organization contemplating
10  *   export to obtain such a license before exporting.
11  * 
12  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13  * distribute this software and its documentation for any purpose and
14  * without fee is hereby granted, provided that the above copyright
15  * notice appear in all copies and that both that copyright notice and
16  * this permission notice appear in supporting documentation, and that
17  * the name of M.I.T. not be used in advertising or publicity pertaining
18  * to distribution of the software without specific, written prior
19  * permission.  Furthermore if you modify this software you must label
20  * your software as modified software and not distribute it in such a
21  * fashion that it might be confused with the original M.I.T. software.
22  * M.I.T. makes no representations about the suitability of
23  * this software for any purpose.  It is provided "as is" without express
24  * or implied warranty.
25  * 
26  *
27  * Some platform-dependent definitions to sync up the C support level.
28  * Some to a C99-ish level, some related utility code.
29  *
30  * Currently:
31  * + make "static inline" work
32  * + 64-bit types and load/store code
33  * + SIZE_MAX
34  * + shared library init/fini hooks
35  * + consistent getpwnam/getpwuid interfaces
36  */
37
38 #ifndef K5_PLATFORM_H
39 #define K5_PLATFORM_H
40
41 #include "autoconf.h"
42
43 /* Initialization and finalization function support for libraries.
44
45    At top level, before the functions are defined or even declared:
46    MAKE_INIT_FUNCTION(init_fn);
47    MAKE_FINI_FUNCTION(fini_fn);
48    Then:
49    int init_fn(void) { ... }
50    void fini_fn(void) { if (INITIALIZER_RAN(init_fn)) ... }
51    In code, in the same file:
52    err = CALL_INIT_FUNCTION(init_fn);
53
54    To trigger or verify the initializer invocation from another file,
55    a helper function must be created.
56
57    This model handles both the load-time execution (Windows) and
58    delayed execution (pthread_once) approaches, and should be able to
59    guarantee in both cases that the init function is run once, in one
60    thread, before other stuff in the library is done; furthermore, the
61    finalization code should only run if the initialization code did.
62    (Maybe I could've made the "if INITIALIZER_RAN" test implicit, via
63    another function hidden in macros, but this is hairy enough
64    already.)
65
66    The init_fn and fini_fn names should be chosen such that any
67    exported names staring with those names, and optionally followed by
68    additional characters, fits in with any namespace constraints on
69    the library in question.
70
71
72    There's also PROGRAM_EXITING() currently always defined as zero.
73    If there's some trivial way to find out if the fini function is
74    being called because the program that the library is linked into is
75    exiting, we can just skip all the work because the resources are
76    about to be freed up anyways.  Generally this is likely to be the
77    same as distinguishing whether the library was loaded dynamically
78    while the program was running, or loaded as part of program
79    startup.  On most platforms, I don't think we can distinguish these
80    cases easily, and it's probably not worth expending any significant
81    effort.  (Note in particular that atexit() won't do, because if the
82    library is explicitly loaded and unloaded, it would have to be able
83    to deregister the atexit callback function.  Also, the system limit
84    on atexit callbacks may be small.)
85
86
87    Implementation outline:
88
89    Windows: MAKE_FINI_FUNCTION creates a symbol with a magic name that
90    is sought at library build time, and code is added to invoke the
91    function when the library is unloaded.  MAKE_INIT_FUNCTION does
92    likewise, but the function is invoked when the library is loaded,
93    and an extra variable is declared to hold an error code and a "yes
94    the initializer ran" flag.  CALL_INIT_FUNCTION blows up if the flag
95    isn't set, otherwise returns the error code.
96
97    UNIX: MAKE_INIT_FUNCTION creates and initializes a variable with a
98    name derived from the function name, containing a k5_once_t
99    (pthread_once_t or int), an error code, and a pointer to the
100    function.  The function itself is declared static, but the
101    associated variable has external linkage.  CALL_INIT_FUNCTION
102    ensures thath the function is called exactly once (pthread_once or
103    just check the flag) and returns the stored error code (or the
104    pthread_once error).
105
106    (That's the basic idea.  With some debugging assert() calls and
107    such, it's a bit more complicated.  And we also need to handle
108    doing the pthread test at run time on systems where that works, so
109    we use the k5_once_t stuff instead.)
110
111    UNIX, with compiler support: MAKE_FINI_FUNCTION declares the
112    function as a destructor, and the run time linker support or
113    whatever will cause it to be invoked when the library is unloaded,
114    the program ends, etc.
115
116    UNIX, with linker support: MAKE_FINI_FUNCTION creates a symbol with
117    a magic name that is sought at library build time, and linker
118    options are used to mark it as a finalization function for the
119    library.  The symbol must be exported.
120
121    UNIX, no library finalization support: The finalization function
122    never runs, and we leak memory.  Tough.
123
124    DELAY_INITIALIZER will be defined by the configure script if we
125    want to use k5_once instead of load-time initialization.  That'll
126    be the preferred method on most systems except Windows, where we
127    have to initialize some mutexes.
128
129
130
131
132    For maximum flexibility in defining the macros, the function name
133    parameter should be a simple name, not even a macro defined as
134    another name.  The function should have a unique name, and should
135    conform to whatever namespace is used by the library in question.
136    (We do have export lists, but (1) they're not used for all
137    platforms, and (2) they're not used for static libraries.)
138
139    If the macro expansion needs the function to have been declared, it
140    must include a declaration.  If it is not necessary for the symbol
141    name to be exported from the object file, the macro should declare
142    it as "static".  Hence the signature must exactly match "void
143    foo(void)".  (ANSI C allows a static declaration followed by a
144    non-static one; the result is internal linkage.)  The macro
145    expansion has to come before the function, because gcc apparently
146    won't act on "__attribute__((constructor))" if it comes after the
147    function definition.
148
149    This is going to be compiler- and environment-specific, and may
150    require some support at library build time, and/or "asm"
151    statements.  But through macro expansion and auxiliary functions,
152    we should be able to handle most things except #pragma.
153
154    It's okay for this code to require that the library be built
155    with the same compiler and compiler options throughout, but
156    we shouldn't require that the library and application use the
157    same compiler.
158
159    For static libraries, we don't really care about cleanup too much,
160    since it's all memory handling and mutex allocation which will all
161    be cleaned up when the program exits.  Thus, it's okay if gcc-built
162    static libraries don't play nicely with cc-built executables when
163    it comes to static constructors, just as long as it doesn't cause
164    linking to fail.
165
166    For dynamic libraries on UNIX, we'll use pthread_once-type support
167    to do delayed initialization, so if finalization can't be made to
168    work, we'll only have memory leaks in a load/use/unload cycle.  If
169    anyone (like, say, the OS vendor) complains about this, they can
170    tell us how to get a shared library finalization function invoked
171    automatically.
172
173    Currently there's --disable-delayed-initialization for preventing
174    the initialization from being delayed on UNIX, but that's mainly
175    just for testing the linker options for initialization, and will
176    probably be removed at some point.  */
177
178 /* Helper macros.  */
179
180 # define JOIN__2_2(A,B) A ## _ ## _ ## B
181 # define JOIN__2(A,B) JOIN__2_2(A,B)
182
183 /* XXX Should test USE_LINKER_INIT_OPTION early, and if it's set,
184    always provide a function by the expected name, even if we're
185    delaying initialization.  */
186
187 #if defined(DELAY_INITIALIZER)
188
189 /* Run the initialization code during program execution, at the latest
190    possible moment.  This means multiple threads may be active.  */
191 # include "k5-thread.h"
192 typedef struct { k5_once_t once; int error, did_run; void (*fn)(void); } k5_init_t;
193 # ifdef USE_LINKER_INIT_OPTION
194 #  define MAYBE_DUMMY_INIT(NAME)                \
195         void JOIN__2(NAME, auxinit) () { }
196 # else
197 #  define MAYBE_DUMMY_INIT(NAME)
198 # endif
199 # ifdef __GNUC__
200 /* Do it in macro form so we get the file/line of the invocation if
201    the assertion fails.  */
202 #  define k5_call_init_function(I)                                      \
203         (__extension__ ({                                               \
204                 k5_init_t *k5int_i = (I);                               \
205                 int k5int_err = k5_once(&k5int_i->once, k5int_i->fn);   \
206                 (k5int_err                                              \
207                  ? k5int_err                                            \
208                  : (assert(k5int_i->did_run != 0), k5int_i->error));    \
209             }))
210 #  define MAYBE_DEFINE_CALLINIT_FUNCTION
211 # else
212 #  define MAYBE_DEFINE_CALLINIT_FUNCTION                        \
213         static inline int k5_call_init_function(k5_init_t *i)   \
214         {                                                       \
215             int err;                                            \
216             err = k5_once(&i->once, i->fn);                     \
217             if (err)                                            \
218                 return err;                                     \
219             assert (i->did_run != 0);                           \
220             return i->error;                                    \
221         }
222 # endif
223 # define MAKE_INIT_FUNCTION(NAME)                               \
224         static int NAME(void);                                  \
225         MAYBE_DUMMY_INIT(NAME)                                  \
226         /* forward declaration for use in initializer */        \
227         static void JOIN__2(NAME, aux) (void);                  \
228         static k5_init_t JOIN__2(NAME, once) =                  \
229                 { K5_ONCE_INIT, 0, 0, JOIN__2(NAME, aux) };     \
230         MAYBE_DEFINE_CALLINIT_FUNCTION                          \
231         static void JOIN__2(NAME, aux) (void)                   \
232         {                                                       \
233             JOIN__2(NAME, once).did_run = 1;                    \
234             JOIN__2(NAME, once).error = NAME();                 \
235         }                                                       \
236         /* so ';' following macro use won't get error */        \
237         static int NAME(void)
238 # define CALL_INIT_FUNCTION(NAME)       \
239         k5_call_init_function(& JOIN__2(NAME, once))
240 /* This should be called in finalization only, so we shouldn't have
241    multiple active threads mucking around in our library at this
242    point.  So ignore the once_t object and just look at the flag.
243
244    XXX Could we have problems with memory coherence between processors
245    if we don't invoke mutex/once routines?  Probably not, the
246    application code should already be coordinating things such that
247    the library code is not in use by this point, and memory
248    synchronization will be needed there.  */
249 # define INITIALIZER_RAN(NAME)  \
250         (JOIN__2(NAME, once).did_run && JOIN__2(NAME, once).error == 0)
251
252 # define PROGRAM_EXITING()              (0)
253
254 #elif defined(__GNUC__) && !defined(_WIN32) && defined(CONSTRUCTOR_ATTR_WORKS)
255
256 /* Run initializer at load time, via GCC/C++ hook magic.  */
257
258 # ifdef USE_LINKER_INIT_OPTION
259      /* Both gcc and linker option??  Favor gcc.  */
260 #  define MAYBE_DUMMY_INIT(NAME)                \
261         void JOIN__2(NAME, auxinit) () { }
262 # else
263 #  define MAYBE_DUMMY_INIT(NAME)
264 # endif
265
266 typedef struct { int error; unsigned char did_run; } k5_init_t;
267 # define MAKE_INIT_FUNCTION(NAME)               \
268         MAYBE_DUMMY_INIT(NAME)                  \
269         static k5_init_t JOIN__2(NAME, ran)     \
270                 = { 0, 2 };                     \
271         static void JOIN__2(NAME, aux)(void)    \
272             __attribute__((constructor));       \
273         static int NAME(void);                  \
274         static void JOIN__2(NAME, aux)(void)    \
275         {                                       \
276             JOIN__2(NAME, ran).error = NAME();  \
277             JOIN__2(NAME, ran).did_run = 3;     \
278         }                                       \
279         static int NAME(void)
280 # define CALL_INIT_FUNCTION(NAME)               \
281         (JOIN__2(NAME, ran).did_run == 3        \
282          ? JOIN__2(NAME, ran).error             \
283          : (abort(),0))
284 # define INITIALIZER_RAN(NAME)  (JOIN__2(NAME,ran).did_run == 3 && JOIN__2(NAME, ran).error == 0)
285
286 # define PROGRAM_EXITING()              (0)
287
288 #elif defined(USE_LINKER_INIT_OPTION) || defined(_WIN32)
289
290 /* Run initializer at load time, via linker magic, or in the
291    case of WIN32, win_glue.c hard-coded knowledge.  */
292 typedef struct { int error; unsigned char did_run; } k5_init_t;
293 # define MAKE_INIT_FUNCTION(NAME)               \
294         static k5_init_t JOIN__2(NAME, ran)     \
295                 = { 0, 2 };                     \
296         static int NAME(void);                  \
297         void JOIN__2(NAME, auxinit)()           \
298         {                                       \
299             JOIN__2(NAME, ran).error = NAME();  \
300             JOIN__2(NAME, ran).did_run = 3;     \
301         }                                       \
302         static int NAME(void)
303 # define CALL_INIT_FUNCTION(NAME)               \
304         (JOIN__2(NAME, ran).did_run == 3        \
305          ? JOIN__2(NAME, ran).error             \
306          : (abort(),0))
307 # define INITIALIZER_RAN(NAME)  \
308         (JOIN__2(NAME, ran).error == 0)
309
310 # define PROGRAM_EXITING()              (0)
311
312 #else
313
314 # error "Don't know how to do load-time initializers for this configuration."
315
316 # define PROGRAM_EXITING()              (0)
317
318 #endif
319
320
321
322 #if defined(USE_LINKER_FINI_OPTION) || defined(_WIN32)
323 /* If we're told the linker option will be used, it doesn't really
324    matter what compiler we're using.  Do it the same way
325    regardless.  */
326
327 # ifdef __hpux
328
329      /* On HP-UX, we need this auxiliary function.  At dynamic load or
330         unload time (but *not* program startup and termination for
331         link-time specified libraries), the linker-indicated function
332         is called with a handle on the library and a flag indicating
333         whether it's being loaded or unloaded.
334
335         The "real" fini function doesn't need to be exported, so
336         declare it static.
337
338         As usual, the final declaration is just for syntactic
339         convenience, so the top-level invocation of this macro can be
340         followed by a semicolon.  */
341
342 #  include <dl.h>
343 #  define MAKE_FINI_FUNCTION(NAME)                                          \
344         static void NAME(void);                                             \
345         void JOIN__2(NAME, auxfini)(shl_t, int); /* silence gcc warnings */ \
346         void JOIN__2(NAME, auxfini)(shl_t h, int l) { if (!l) NAME(); }     \
347         static void NAME(void)
348
349 # else /* not hpux */
350
351 #  define MAKE_FINI_FUNCTION(NAME)      \
352         void NAME(void)
353
354 # endif
355
356 #elif defined(__GNUC__) && defined(DESTRUCTOR_ATTR_WORKS)
357 /* If we're using gcc, if the C++ support works, the compiler should
358    build executables and shared libraries that support the use of
359    static constructors and destructors.  The C compiler supports a
360    function attribute that makes use of the same facility as C++.
361
362    XXX How do we know if the C++ support actually works?  */
363 # define MAKE_FINI_FUNCTION(NAME)       \
364         static void NAME(void) __attribute__((destructor))
365
366 #elif !defined(SHARED)
367
368 /* In this case, we just don't care about finalization.
369
370    The code will still define the function, but we won't do anything
371    with it.  Annoying: This may generate unused-function warnings.  */
372
373 # define MAKE_FINI_FUNCTION(NAME)       \
374         static void NAME(void)
375
376 #else
377
378 # error "Don't know how to do unload-time finalization for this configuration."
379
380 #endif
381
382
383 /* 64-bit support: krb5_ui_8 and krb5_int64.
384
385    This should move to krb5.h eventually, but without the namespace
386    pollution from the autoconf macros.  */
387 #if defined(HAVE_STDINT_H) || defined(HAVE_INTTYPES_H)
388 # ifdef HAVE_STDINT_H
389 #  include <stdint.h>
390 # endif
391 # ifdef HAVE_INTTYPES_H
392 #  include <inttypes.h>
393 # endif
394 # define INT64_TYPE int64_t
395 # define UINT64_TYPE uint64_t
396 #elif defined(_WIN32)
397 # define INT64_TYPE signed __int64
398 # define UINT64_TYPE unsigned __int64
399 #else /* not Windows, and neither stdint.h nor inttypes.h */
400 # define INT64_TYPE signed long long
401 # define UINT64_TYPE unsigned long long
402 #endif
403
404 #include <limits.h>
405 #ifndef SIZE_MAX
406 # define SIZE_MAX ((size_t)((size_t)0 - 1))
407 #endif
408
409 /* Read and write integer values as (unaligned) octet strings in
410    specific byte orders.
411
412    Add per-platform optimizations later if needed.  (E.g., maybe x86
413    unaligned word stores and gcc/asm instructions for byte swaps,
414    etc.)  */
415
416 /* Optimize for GCC on architectures with known byte orders.
417
418    MIPS can use either byte order, but the preprocessor tells us which
419    mode we're compiling for.  The GCC config files indicate that
420    variants of Alpha and IA64 might be out there with both byte
421    orders, but until we encounter the "wrong" ones in the real world,
422    just go with the default (unless there are cpp predefines to help
423    us there too).
424
425    As far as I know, only PDP11 and ARM (which we don't handle here)
426    have strange byte orders where an 8-byte value isn't laid out as
427    either 12345678 or 87654321.
428
429    See also lib/crypto/aes/aesopt.h for code checking available header
430    files for endianness preprocessor macros.  */
431 #if defined(__i386__) || defined(_MIPSEL) || defined(__alpha__) || defined(__ia64__)
432 # define K5_LE  /* little-endian */
433 #endif
434 #if defined(__hppa__) || defined(__rs6000__) || defined(__sparc__) || defined(_MIPSEB) || defined(__m68k__) || defined(__sparc64__) || defined(__ppc__) || defined(__ppc64__)
435 # define K5_BE  /* big-endian */
436 #endif
437
438 /* GCC's packed structures can be written to with any alignment; the
439    compiler will use byte operations, unaligned-word operations, or
440    normal memory ops as appropriate for the architecture.
441
442    This assumes the availability of uint##_t types, which should work
443    on most of our platforms except Windows, where we're not using
444    GCC.  */
445 #ifdef __GNUC__
446 # define PUT(SIZE,PTR,VAL)      (((struct { uint##SIZE##_t i; } __attribute__((packed)) *)(PTR))->i = (VAL))
447 # define GET(SIZE,PTR)          (((const struct { uint##SIZE##_t i; } __attribute__((packed)) *)(PTR))->i)
448 # define PUTSWAPPED(SIZE,PTR,VAL)       PUT(SIZE,PTR,SWAP##SIZE(VAL))
449 # define GETSWAPPED(SIZE,PTR)           SWAP##SIZE(GET(SIZE,PTR))
450 #endif
451 /* To do: Define SWAP16, SWAP32, SWAP64 macros to byte-swap values
452    with the indicated numbers of bits.
453
454    Linux: byteswap.h, bswap_16 etc.
455    Solaris 10: none
456    Mac OS X: machine/endian.h or byte_order.h, NXSwap{Short,Int,LongLong}
457    NetBSD: sys/bswap.h, bswap16 etc.  */
458
459 static inline void
460 store_16_be (unsigned int val, unsigned char *p)
461 {
462 #if defined(__GNUC__) && defined(K5_BE)
463     PUT(16,p,val);
464 #elif defined(__GNUC__) && defined(K5_LE) && defined(SWAP16)
465     PUTSWAPPED(16,p,val);
466 #else
467     p[0] = (val >>  8) & 0xff;
468     p[1] = (val      ) & 0xff;
469 #endif
470 }
471 static inline void
472 store_16_le (unsigned int val, unsigned char *p)
473 {
474 #if defined(__GNUC__) && defined(K5_LE)
475     PUT(16,p,val);
476 #elif defined(__GNUC__) && defined(K5_BE) && defined(SWAP16)
477     PUTSWAPPED(16,p,val);
478 #else
479     p[1] = (val >>  8) & 0xff;
480     p[0] = (val      ) & 0xff;
481 #endif
482 }
483 static inline void
484 store_32_be (unsigned int val, unsigned char *p)
485 {
486 #if defined(__GNUC__) && defined(K5_BE)
487     PUT(32,p,val);
488 #elif defined(__GNUC__) && defined(K5_LE) && defined(SWAP32)
489     PUTSWAPPED(32,p,val);
490 #else
491     p[0] = (val >> 24) & 0xff;
492     p[1] = (val >> 16) & 0xff;
493     p[2] = (val >>  8) & 0xff;
494     p[3] = (val      ) & 0xff;
495 #endif
496 }
497 static inline void
498 store_32_le (unsigned int val, unsigned char *p)
499 {
500 #if defined(__GNUC__) && defined(K5_LE)
501     PUT(32,p,val);
502 #elif defined(__GNUC__) && defined(K5_BE) && defined(SWAP32)
503     PUTSWAPPED(32,p,val);
504 #else
505     p[3] = (val >> 24) & 0xff;
506     p[2] = (val >> 16) & 0xff;
507     p[1] = (val >>  8) & 0xff;
508     p[0] = (val      ) & 0xff;
509 #endif
510 }
511 static inline void
512 store_64_be (UINT64_TYPE val, unsigned char *p)
513 {
514 #if defined(__GNUC__) && defined(K5_BE)
515     PUT(64,p,val);
516 #elif defined(__GNUC__) && defined(K5_LE) && defined(SWAP64)
517     PUTSWAPPED(64,p,val);
518 #else
519     p[0] = (unsigned char)((val >> 56) & 0xff);
520     p[1] = (unsigned char)((val >> 48) & 0xff);
521     p[2] = (unsigned char)((val >> 40) & 0xff);
522     p[3] = (unsigned char)((val >> 32) & 0xff);
523     p[4] = (unsigned char)((val >> 24) & 0xff);
524     p[5] = (unsigned char)((val >> 16) & 0xff);
525     p[6] = (unsigned char)((val >>  8) & 0xff);
526     p[7] = (unsigned char)((val      ) & 0xff);
527 #endif
528 }
529 static inline void
530 store_64_le (UINT64_TYPE val, unsigned char *p)
531 {
532 #if defined(__GNUC__) && defined(K5_LE)
533     PUT(64,p,val);
534 #elif defined(__GNUC__) && defined(K5_BE) && defined(SWAP64)
535     PUTSWAPPED(64,p,val);
536 #else
537     p[7] = (unsigned char)((val >> 56) & 0xff);
538     p[6] = (unsigned char)((val >> 48) & 0xff);
539     p[5] = (unsigned char)((val >> 40) & 0xff);
540     p[4] = (unsigned char)((val >> 32) & 0xff);
541     p[3] = (unsigned char)((val >> 24) & 0xff);
542     p[2] = (unsigned char)((val >> 16) & 0xff);
543     p[1] = (unsigned char)((val >>  8) & 0xff);
544     p[0] = (unsigned char)((val      ) & 0xff);
545 #endif
546 }
547 static inline unsigned short
548 load_16_be (const unsigned char *p)
549 {
550 #if defined(__GNUC__) && defined(K5_BE)
551     return GET(16,p);
552 #elif defined(__GNUC__) && defined(K5_LE) && defined(SWAP16)
553     return GETSWAPPED(16,p);
554 #else
555     return (p[1] | (p[0] << 8));
556 #endif
557 }
558 static inline unsigned short
559 load_16_le (const unsigned char *p)
560 {
561 #if defined(__GNUC__) && defined(K5_LE)
562     return GET(16,p);
563 #elif defined(__GNUC__) && defined(K5_BE) && defined(SWAP16)
564     return GETSWAPPED(16,p);
565 #else
566     return (p[0] | (p[1] << 8));
567 #endif
568 }
569 static inline unsigned int
570 load_32_be (const unsigned char *p)
571 {
572 #if defined(__GNUC__) && defined(K5_BE)
573     return GET(32,p);
574 #elif defined(__GNUC__) && defined(K5_LE) && defined(SWAP32)
575     return GETSWAPPED(32,p);
576 #else
577     return (p[3] | (p[2] << 8) | (p[1] << 16) | (p[0] << 24));
578 #endif
579 }
580 static inline unsigned int
581 load_32_le (const unsigned char *p)
582 {
583 #if defined(__GNUC__) && defined(K5_LE)
584     return GET(32,p);
585 #elif defined(__GNUC__) && defined(K5_BE) && defined(SWAP32)
586     return GETSWAPPED(32,p);
587 #else
588     return (p[0] | (p[1] << 8) | (p[2] << 16) | (p[3] << 24));
589 #endif
590 }
591 static inline UINT64_TYPE
592 load_64_be (const unsigned char *p)
593 {
594 #if defined(__GNUC__) && defined(K5_BE)
595     return GET(64,p);
596 #elif defined(__GNUC__) && defined(K5_LE) && defined(SWAP64)
597     return GETSWAPPED(64,p);
598 #else
599     return ((UINT64_TYPE)load_32_be(p) << 32) | load_32_be(p+4);
600 #endif
601 }
602 static inline UINT64_TYPE
603 load_64_le (const unsigned char *p)
604 {
605 #if defined(__GNUC__) && defined(K5_LE)
606     return GET(64,p);
607 #elif defined(__GNUC__) && defined(K5_BE) && defined(SWAP64)
608     return GETSWAPPED(64,p);
609 #else
610     return ((UINT64_TYPE)load_32_le(p+4) << 32) | load_32_le(p);
611 #endif
612 }
613
614
615 /* Make the interfaces to getpwnam and getpwuid consistent.
616    Model the wrappers on the POSIX thread-safe versions, but
617    use the unsafe system versions if the safe ones don't exist
618    or we can't figure out their interfaces.  */
619
620 /* int k5_getpwnam_r(const char *, blah blah) */
621 #ifdef HAVE_GETPWNAM_R
622 # ifndef GETPWNAM_R_4_ARGS
623 /* POSIX */
624 #  define k5_getpwnam_r(NAME, REC, BUF, BUFSIZE, OUT) \
625         getpwnam_r(NAME,REC,BUF,BUFSIZE,OUT)
626 # else
627 /* POSIX drafts? */
628 #  ifdef GETPWNAM_R_RETURNS_INT
629 #   define k5_getpwnam_r(NAME, REC, BUF, BUFSIZE, OUT)  \
630         (getpwnam_r(NAME,REC,BUF,BUFSIZE) == 0          \
631          ? (*(OUT) = REC, 0)                            \
632          : (*(OUT) = NULL, -1))
633 #  else
634 #   define k5_getpwnam_r(NAME, REC, BUF, BUFSIZE, OUT)  \
635         (*(OUT) = getpwnam_r(NAME,REC,BUF,BUFSIZE), *(OUT) == NULL ? -1 : 0)
636 #  endif
637 # endif
638 #else /* no getpwnam_r, or can't figure out #args or return type */
639 /* Will get warnings about unused variables.  */
640 # define k5_getpwnam_r(NAME, REC, BUF, BUFSIZE, OUT) \
641         (*(OUT) = getpwnam(NAME), *(OUT) == NULL ? -1 : 0)
642 #endif
643
644 /* int k5_getpwuid_r(uid_t, blah blah) */
645 #ifdef HAVE_GETPWUID_R
646 # ifndef GETPWUID_R_4_ARGS
647 /* POSIX */
648 #  define k5_getpwuid_r(UID, REC, BUF, BUFSIZE, OUT) \
649         getpwuid_r(UID,REC,BUF,BUFSIZE,OUT)
650 # else
651 /* POSIX drafts?  Yes, I mean to test GETPWNAM... here.  Less junk to
652    do at configure time.  */
653 #  ifdef GETPWNAM_R_RETURNS_INT
654 #   define k5_getpwuid_r(UID, REC, BUF, BUFSIZE, OUT)   \
655         (getpwuid_r(UID,REC,BUF,BUFSIZE) == 0           \
656          ? (*(OUT) = REC, 0)                            \
657          : (*(OUT) = NULL, -1))
658 #  else
659 #   define k5_getpwuid_r(UID, REC, BUF, BUFSIZE, OUT)  \
660         (*(OUT) = getpwuid_r(UID,REC,BUF,BUFSIZE), *(OUT) == NULL ? -1 : 0)
661 #  endif
662 # endif
663 #else /* no getpwuid_r, or can't figure out #args or return type */
664 /* Will get warnings about unused variables.  */
665 # define k5_getpwuid_r(UID, REC, BUF, BUFSIZE, OUT) \
666         (*(OUT) = getpwuid(UID), *(OUT) == NULL ? -1 : 0)
667 #endif
668
669
670 #endif /* K5_PLATFORM_H */