From: Tom Yu Date: Mon, 30 Nov 2009 21:35:38 +0000 (+0000) Subject: Mark and reindent util/support X-Git-Tag: krb5-1.8-alpha1~117 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=207e73bd53f9e32910e6ce6187704b823b6296c0;p=krb5.git Mark and reindent util/support git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@23392 dc483132-0cff-0310-8789-dd5450dbe970 --- diff --git a/src/Makefile.in b/src/Makefile.in index c1a048517..a8887677a 100644 --- a/src/Makefile.in +++ b/src/Makefile.in @@ -670,7 +670,8 @@ INDENTDIRS = \ lib/krb5 \ plugins \ prototype \ - slave + slave \ + util/support BSDFILES = \ kadmin/cli/strftime.c \ @@ -683,7 +684,9 @@ BSDFILES = \ lib/kadm5/srv/adb_xdr.c \ lib/krb5/krb/strftime.c \ lib/krb5/krb/strptime.c \ - slave/kpropd_rpc.c + slave/kpropd_rpc.c \ + util/support/mkstemp.c \ + util/support/strlcpy.c OTHEREXCLUDES = \ include/iprop.h \ diff --git a/src/util/support/cache-addrinfo.h b/src/util/support/cache-addrinfo.h index 95f522dcb..d4d26b14f 100644 --- a/src/util/support/cache-addrinfo.h +++ b/src/util/support/cache-addrinfo.h @@ -1,3 +1,4 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * Copyright (C) 2004 by the Massachusetts Institute of Technology, * Cambridge, MA, USA. All Rights Reserved. @@ -39,62 +40,65 @@ * fashion that it might be confused with the original M.I.T. software. */ -/* Approach overview: - - If a system version is available but buggy, save handles to it, - redefine the names to refer to static functions defined here, and - in those functions, call the system versions and fix up the - returned data. Use the native data structures and flag values. - - If no system version exists, use gethostby* and fake it. Define - the data structures and flag values locally. - - - On Mac OS X, getaddrinfo results aren't cached (though - gethostbyname results are), so we need to build a cache here. Now - things are getting really messy. Because the cache is in use, we - use getservbyname, and throw away thread safety. (Not that the - cache is thread safe, but when we get locking support, that'll be - dealt with.) This code needs tearing down and rebuilding, soon. - - - Note that recent Windows developers' code has an interesting hack: - When you include the right header files, with the right set of - macros indicating system versions, you'll get an inline function - that looks for getaddrinfo (or whatever) in the system library, and - calls it if it's there. If it's not there, it fakes it with - gethostby* calls. - - We're taking a simpler approach: A system provides these routines or - it does not. - - Someday, we may want to take into account different versions (say, - different revs of GNU libc) where some are broken in one way, and - some work or are broken in another way. Cross that bridge when we - come to it. */ +/* + * Approach overview: + * + * If a system version is available but buggy, save handles to it, + * redefine the names to refer to static functions defined here, and + * in those functions, call the system versions and fix up the + * returned data. Use the native data structures and flag values. + * + * If no system version exists, use gethostby* and fake it. Define + * the data structures and flag values locally. + * + * + * On Mac OS X, getaddrinfo results aren't cached (though + * gethostbyname results are), so we need to build a cache here. Now + * things are getting really messy. Because the cache is in use, we + * use getservbyname, and throw away thread safety. (Not that the + * cache is thread safe, but when we get locking support, that'll be + * dealt with.) This code needs tearing down and rebuilding, soon. + * + * + * Note that recent Windows developers' code has an interesting hack: + * When you include the right header files, with the right set of + * macros indicating system versions, you'll get an inline function + * that looks for getaddrinfo (or whatever) in the system library, and + * calls it if it's there. If it's not there, it fakes it with + * gethostby* calls. + * + * We're taking a simpler approach: A system provides these routines or + * it does not. + * + * Someday, we may want to take into account different versions (say, + * different revs of GNU libc) where some are broken in one way, and + * some work or are broken in another way. Cross that bridge when we + * come to it. + */ /* To do, maybe: - - + For AIX 4.3.3, using the RFC 2133 definition: Implement - AI_NUMERICHOST. It's not defined in the header file. - - For certain (old?) versions of GNU libc, AI_NUMERICHOST is - defined but not implemented. - - + Use gethostbyname2, inet_aton and other IPv6 or thread-safe - functions if available. But, see - http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=135182 for one - gethostbyname2 problem on Linux. And besides, if a platform is - supporting IPv6 at all, they really should be doing getaddrinfo - by now. - - + inet_ntop, inet_pton - - + Conditionally export/import the function definitions, so a - library can have a single copy instead of multiple. - - + Upgrade host requirements to include working implementations of - these functions, and throw all this away. Pleeease? :-) */ + * + * + For AIX 4.3.3, using the RFC 2133 definition: Implement + * AI_NUMERICHOST. It's not defined in the header file. + * + * For certain (old?) versions of GNU libc, AI_NUMERICHOST is + * defined but not implemented. + * + * + Use gethostbyname2, inet_aton and other IPv6 or thread-safe + * functions if available. But, see + * http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=135182 for one + * gethostbyname2 problem on Linux. And besides, if a platform is + * supporting IPv6 at all, they really should be doing getaddrinfo + * by now. + * + * + inet_ntop, inet_pton + * + * + Conditionally export/import the function definitions, so a + * library can have a single copy instead of multiple. + * + * + Upgrade host requirements to include working implementations of + * these functions, and throw all this away. Pleeease? :-) + */ #include "port-sockets.h" #include "socket-utils.h" diff --git a/src/util/support/errors.c b/src/util/support/errors.c index 967c57a2f..00cc922eb 100644 --- a/src/util/support/errors.c +++ b/src/util/support/errors.c @@ -1,3 +1,4 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* Can't include krb5.h here, or k5-int.h which includes it, because krb5.h needs to be generated with error tables, after util/et, which builds after this directory. */ @@ -30,9 +31,9 @@ krb5int_err_init (void) { return k5_mutex_finish_init (&krb5int_error_info_support_mutex); } -#define initialize() krb5int_call_thread_support_init() -#define lock() k5_mutex_lock(&krb5int_error_info_support_mutex) -#define unlock() k5_mutex_unlock(&krb5int_error_info_support_mutex) +#define initialize() krb5int_call_thread_support_init() +#define lock() k5_mutex_lock(&krb5int_error_info_support_mutex) +#define unlock() k5_mutex_unlock(&krb5int_error_info_support_mutex) #undef krb5int_set_error void @@ -46,7 +47,7 @@ krb5int_set_error (struct errinfo *ep, long code, const char *fmt, ...) void krb5int_set_error_fl (struct errinfo *ep, long code, - const char *file, int line, const char *fmt, ...) + const char *file, int line, const char *fmt, ...) { va_list args; va_start (args, fmt); @@ -56,15 +57,15 @@ krb5int_set_error_fl (struct errinfo *ep, long code, void krb5int_vset_error (struct errinfo *ep, long code, - const char *fmt, va_list args) + const char *fmt, va_list args) { krb5int_vset_error_fl(ep, code, NULL, 0, fmt, args); } void krb5int_vset_error_fl (struct errinfo *ep, long code, - const char *file, int line, - const char *fmt, va_list args) + const char *file, int line, + const char *fmt, va_list args) { va_list args2; char *str = NULL, *str2, *slash; @@ -79,19 +80,19 @@ krb5int_vset_error_fl (struct errinfo *ep, long code, /* try vasprintf first */ va_copy(args2, args); if (vasprintf(&str, fmt, args2) < 0) { - str = NULL; + str = NULL; } va_end(args2); if (str && line) { - /* Try to add file and line suffix. */ - slash = strrchr(file, '/'); - if (slash) - file = slash + 1; - if (asprintf(&str2, "%s (%s: %d)", str, file, line) > 0) { - free(str); - str = str2; - } + /* Try to add file and line suffix. */ + slash = strrchr(file, '/'); + if (slash) + file = slash + 1; + if (asprintf(&str2, "%s (%s: %d)", str, file, line) > 0) { + free(str); + str = str2; + } } /* If that failed, try using scratch_buf */ @@ -102,8 +103,8 @@ krb5int_vset_error_fl (struct errinfo *ep, long code, /* free old string before setting new one */ if (ep->msg && ep->msg != ep->scratch_buf) { - krb5int_free_error (ep, ep->msg); - ep->msg = NULL; + krb5int_free_error (ep, ep->msg); + ep->msg = NULL; } ep->code = code; ep->msg = str ? str : ep->scratch_buf; @@ -118,72 +119,72 @@ krb5int_get_error (struct errinfo *ep, long code) { const char *r, *r2; if (code == ep->code && ep->msg) { - r = strdup(ep->msg); - if (r == NULL) { - strlcpy(ep->scratch_buf, _("Out of memory"), - sizeof(ep->scratch_buf)); - r = ep->scratch_buf; - } - return r; + r = strdup(ep->msg); + if (r == NULL) { + strlcpy(ep->scratch_buf, _("Out of memory"), + sizeof(ep->scratch_buf)); + r = ep->scratch_buf; + } + return r; } if (initialize() != 0) { - strncpy(ep->scratch_buf, _("Kerberos library initialization failure"), - sizeof(ep->scratch_buf)); - ep->scratch_buf[sizeof(ep->scratch_buf)-1] = 0; - ep->msg = NULL; - return ep->scratch_buf; + strncpy(ep->scratch_buf, _("Kerberos library initialization failure"), + sizeof(ep->scratch_buf)); + ep->scratch_buf[sizeof(ep->scratch_buf)-1] = 0; + ep->msg = NULL; + return ep->scratch_buf; } if (lock()) - goto no_fptr; + goto no_fptr; if (fptr == NULL) { - unlock(); + unlock(); no_fptr: - /* Theoretically, according to ISO C, strerror should be able - to give us a message back for any int value. However, on - UNIX at least, the errno codes strerror will actually be - useful for are positive, so a negative value here would be - kind of weird. - - Coverity Prevent thinks we shouldn't be passing negative - values to strerror, and it's not likely to be useful, so - let's not do it. - - Besides, normally we shouldn't get here; fptr should take - us to a callback function in the com_err library. */ - if (code < 0) - goto format_number; + /* Theoretically, according to ISO C, strerror should be able + to give us a message back for any int value. However, on + UNIX at least, the errno codes strerror will actually be + useful for are positive, so a negative value here would be + kind of weird. + + Coverity Prevent thinks we shouldn't be passing negative + values to strerror, and it's not likely to be useful, so + let's not do it. + + Besides, normally we shouldn't get here; fptr should take + us to a callback function in the com_err library. */ + if (code < 0) + goto format_number; #ifdef HAVE_STRERROR_R - if (strerror_r(code, ep->scratch_buf, sizeof(ep->scratch_buf)) == 0) { - char *p = strdup(ep->scratch_buf); - if (p) - return p; - return ep->scratch_buf; - } + if (strerror_r(code, ep->scratch_buf, sizeof(ep->scratch_buf)) == 0) { + char *p = strdup(ep->scratch_buf); + if (p) + return p; + return ep->scratch_buf; + } #endif - r = strerror(code); - if (r) { - strlcpy(ep->scratch_buf, r, sizeof(ep->scratch_buf)); - return ep->scratch_buf; - } + r = strerror(code); + if (r) { + strlcpy(ep->scratch_buf, r, sizeof(ep->scratch_buf)); + return ep->scratch_buf; + } format_number: - snprintf (ep->scratch_buf, sizeof(ep->scratch_buf), - _("error %ld"), code); - return ep->scratch_buf; + snprintf (ep->scratch_buf, sizeof(ep->scratch_buf), + _("error %ld"), code); + return ep->scratch_buf; } r = fptr(code); if (r == NULL) { - unlock(); - goto format_number; + unlock(); + goto format_number; } r2 = strdup(r); if (r2 == NULL) { - strlcpy(ep->scratch_buf, r, sizeof(ep->scratch_buf)); - unlock(); - return ep->scratch_buf; + strlcpy(ep->scratch_buf, r, sizeof(ep->scratch_buf)); + unlock(); + return ep->scratch_buf; } else { - unlock(); - return r2; + unlock(); + return r2; } } @@ -191,7 +192,7 @@ void krb5int_free_error (struct errinfo *ep, const char *msg) { if (msg != ep->scratch_buf) - free ((char *) msg); + free ((char *) msg); } void @@ -206,7 +207,7 @@ krb5int_set_error_info_callout_fn (const char *(KRB5_CALLCONV *f)(long)) { initialize(); if (lock() == 0) { - fptr = f; - unlock(); + fptr = f; + unlock(); } } diff --git a/src/util/support/fake-addrinfo.c b/src/util/support/fake-addrinfo.c index 5d90e7217..64d84e5ca 100644 --- a/src/util/support/fake-addrinfo.c +++ b/src/util/support/fake-addrinfo.c @@ -1,3 +1,4 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * Copyright (C) 2001,2002,2003,2004,2005,2006 by the Massachusetts Institute of Technology, * Cambridge, MA, USA. All Rights Reserved. @@ -39,62 +40,66 @@ * fashion that it might be confused with the original M.I.T. software. */ -/* Approach overview: - - If a system version is available but buggy, save handles to it, - redefine the names to refer to static functions defined here, and - in those functions, call the system versions and fix up the - returned data. Use the native data structures and flag values. - - If no system version exists, use gethostby* and fake it. Define - the data structures and flag values locally. - - - On Mac OS X, getaddrinfo results aren't cached (though - gethostbyname results are), so we need to build a cache here. Now - things are getting really messy. Because the cache is in use, we - use getservbyname, and throw away thread safety. (Not that the - cache is thread safe, but when we get locking support, that'll be - dealt with.) This code needs tearing down and rebuilding, soon. - - - Note that recent Windows developers' code has an interesting hack: - When you include the right header files, with the right set of - macros indicating system versions, you'll get an inline function - that looks for getaddrinfo (or whatever) in the system library, and - calls it if it's there. If it's not there, it fakes it with - gethostby* calls. - - We're taking a simpler approach: A system provides these routines or - it does not. - - Someday, we may want to take into account different versions (say, - different revs of GNU libc) where some are broken in one way, and - some work or are broken in another way. Cross that bridge when we - come to it. */ - -/* To do, maybe: - - + For AIX 4.3.3, using the RFC 2133 definition: Implement - AI_NUMERICHOST. It's not defined in the header file. - - For certain (old?) versions of GNU libc, AI_NUMERICHOST is - defined but not implemented. - - + Use gethostbyname2, inet_aton and other IPv6 or thread-safe - functions if available. But, see - http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=135182 for one - gethostbyname2 problem on Linux. And besides, if a platform is - supporting IPv6 at all, they really should be doing getaddrinfo - by now. - - + inet_ntop, inet_pton - - + Conditionally export/import the function definitions, so a - library can have a single copy instead of multiple. +/* + * Approach overview: + * + * If a system version is available but buggy, save handles to it, + * redefine the names to refer to static functions defined here, and + * in those functions, call the system versions and fix up the + * returned data. Use the native data structures and flag values. + * + * If no system version exists, use gethostby* and fake it. Define + * the data structures and flag values locally. + * + * + * On Mac OS X, getaddrinfo results aren't cached (though + * gethostbyname results are), so we need to build a cache here. Now + * things are getting really messy. Because the cache is in use, we + * use getservbyname, and throw away thread safety. (Not that the + * cache is thread safe, but when we get locking support, that'll be + * dealt with.) This code needs tearing down and rebuilding, soon. + * + * + * Note that recent Windows developers' code has an interesting hack: + * When you include the right header files, with the right set of + * macros indicating system versions, you'll get an inline function + * that looks for getaddrinfo (or whatever) in the system library, and + * calls it if it's there. If it's not there, it fakes it with + * gethostby* calls. + * + * We're taking a simpler approach: A system provides these routines or + * it does not. + * + * Someday, we may want to take into account different versions (say, + * different revs of GNU libc) where some are broken in one way, and + * some work or are broken in another way. Cross that bridge when we + * come to it. + */ - + Upgrade host requirements to include working implementations of - these functions, and throw all this away. Pleeease? :-) */ +/* + * To do, maybe: + * + * + For AIX 4.3.3, using the RFC 2133 definition: Implement + * AI_NUMERICHOST. It's not defined in the header file. + * + * For certain (old?) versions of GNU libc, AI_NUMERICHOST is + * defined but not implemented. + * + * + Use gethostbyname2, inet_aton and other IPv6 or thread-safe + * functions if available. But, see + * http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=135182 for one + * gethostbyname2 problem on Linux. And besides, if a platform is + * supporting IPv6 at all, they really should be doing getaddrinfo + * by now. + * + * + inet_ntop, inet_pton + * + * + Conditionally export/import the function definitions, so a + * library can have a single copy instead of multiple. + * + * + Upgrade host requirements to include working implementations of + * these functions, and throw all this away. Pleeease? :-) + */ #include "port-sockets.h" #include "socket-utils.h" @@ -112,20 +117,20 @@ /*@-incondefs@*/ extern int getaddrinfo (/*@in@*/ /*@null@*/ const char *, - /*@in@*/ /*@null@*/ const char *, - /*@in@*/ /*@null@*/ const struct addrinfo *, - /*@out@*/ struct addrinfo **) + /*@in@*/ /*@null@*/ const char *, + /*@in@*/ /*@null@*/ const struct addrinfo *, + /*@out@*/ struct addrinfo **) ; extern void freeaddrinfo (/*@only@*/ /*@out@*/ struct addrinfo *) ; extern int getnameinfo (const struct sockaddr *addr, socklen_t addrsz, - /*@out@*/ /*@null@*/ char *h, socklen_t hsz, - /*@out@*/ /*@null@*/ char *s, socklen_t ssz, - int flags) - /*@requires (maxSet(h)+1) >= hsz /\ (maxSet(s)+1) >= ssz @*/ - /* too hard: maxRead(addr) >= (addrsz-1) */ + /*@out@*/ /*@null@*/ char *h, socklen_t hsz, + /*@out@*/ /*@null@*/ char *s, socklen_t ssz, + int flags) +/*@requires (maxSet(h)+1) >= hsz /\ (maxSet(s)+1) >= ssz @*/ +/* too hard: maxRead(addr) >= (addrsz-1) */ /*@modifies *h, *s@*/; extern /*@dependent@*/ char *gai_strerror (int code) /*@*/; /*@=incondefs@*/ @@ -162,8 +167,8 @@ extern /*@dependent@*/ char *gai_strerror (int code) /*@*/; #endif #ifdef NUMERIC_SERVICE_BROKEN -# include /* isdigit */ -# include /* strtoul */ +# include /* isdigit */ +# include /* strtoul */ #endif @@ -172,9 +177,9 @@ extern /*@dependent@*/ char *gai_strerror (int code) /*@*/; gethostbyname_r? */ #if !defined(HAVE_GETHOSTBYNAME_R) || defined(THREADSAFE_GETHOSTBYNAME) typedef struct hostent *GET_HOST_TMP; -#define GET_HOST_BY_NAME(NAME, HP, ERR, TMP) \ +#define GET_HOST_BY_NAME(NAME, HP, ERR, TMP) \ { TMP = gethostbyname (NAME); (ERR) = h_errno; (HP) = TMP; } -#define GET_HOST_BY_ADDR(ADDR, ADDRLEN, FAMILY, HP, ERR, TMP) \ +#define GET_HOST_BY_ADDR(ADDR, ADDRLEN, FAMILY, HP, ERR, TMP) \ { TMP = gethostbyaddr ((ADDR), (ADDRLEN), (FAMILY)); (ERR) = h_errno; (HP) = TMP; } #else #ifdef _AIX /* XXX should have a feature test! */ @@ -182,24 +187,24 @@ typedef struct { struct hostent ent; struct hostent_data data; } GET_HOST_TMP; -#define GET_HOST_BY_NAME(NAME, HP, ERR, TMP) \ - { \ - (HP) = (gethostbyname_r((NAME), &TMP.ent, &TMP.data) \ - ? 0 \ - : &TMP.ent); \ - (ERR) = h_errno; \ +#define GET_HOST_BY_NAME(NAME, HP, ERR, TMP) \ + { \ + (HP) = (gethostbyname_r((NAME), &TMP.ent, &TMP.data) \ + ? 0 \ + : &TMP.ent); \ + (ERR) = h_errno; \ } /* -#define GET_HOST_BY_ADDR(ADDR, ADDRLEN, FAMILY, HP, ERR) \ - { \ - struct hostent my_h_ent; \ - struct hostent_data my_h_ent_data; \ - (HP) = (gethostbyaddr_r((ADDR), (ADDRLEN), (FAMILY), &my_h_ent, \ - &my_h_ent_data) \ - ? 0 \ - : &my_h_ent); \ - (ERR) = my_h_err; \ - } + #define GET_HOST_BY_ADDR(ADDR, ADDRLEN, FAMILY, HP, ERR) \ + { \ + struct hostent my_h_ent; \ + struct hostent_data my_h_ent_data; \ + (HP) = (gethostbyaddr_r((ADDR), (ADDRLEN), (FAMILY), &my_h_ent, \ + &my_h_ent_data) \ + ? 0 \ + : &my_h_ent); \ + (ERR) = my_h_err; \ + } */ #else #ifdef GETHOSTBYNAME_R_RETURNS_INT @@ -207,48 +212,48 @@ typedef struct { struct hostent ent; char buf[8192]; } GET_HOST_TMP; -#define GET_HOST_BY_NAME(NAME, HP, ERR, TMP) \ - { \ - struct hostent *my_hp = NULL; \ - int my_h_err, my_ret; \ - my_ret = gethostbyname_r((NAME), &TMP.ent, \ - TMP.buf, sizeof (TMP.buf), &my_hp, \ - &my_h_err); \ - (HP) = (((my_ret != 0) || (my_hp != &TMP.ent)) \ - ? 0 \ - : &TMP.ent); \ - (ERR) = my_h_err; \ +#define GET_HOST_BY_NAME(NAME, HP, ERR, TMP) \ + { \ + struct hostent *my_hp = NULL; \ + int my_h_err, my_ret; \ + my_ret = gethostbyname_r((NAME), &TMP.ent, \ + TMP.buf, sizeof (TMP.buf), &my_hp, \ + &my_h_err); \ + (HP) = (((my_ret != 0) || (my_hp != &TMP.ent)) \ + ? 0 \ + : &TMP.ent); \ + (ERR) = my_h_err; \ } -#define GET_HOST_BY_ADDR(ADDR, ADDRLEN, FAMILY, HP, ERR, TMP) \ - { \ - struct hostent *my_hp; \ - int my_h_err, my_ret; \ - my_ret = gethostbyaddr_r((ADDR), (ADDRLEN), (FAMILY), &TMP.ent, \ - TMP.buf, sizeof (TMP.buf), &my_hp, \ - &my_h_err); \ - (HP) = (((my_ret != 0) || (my_hp != &TMP.ent)) \ - ? 0 \ - : &TMP.ent); \ - (ERR) = my_h_err; \ +#define GET_HOST_BY_ADDR(ADDR, ADDRLEN, FAMILY, HP, ERR, TMP) \ + { \ + struct hostent *my_hp; \ + int my_h_err, my_ret; \ + my_ret = gethostbyaddr_r((ADDR), (ADDRLEN), (FAMILY), &TMP.ent, \ + TMP.buf, sizeof (TMP.buf), &my_hp, \ + &my_h_err); \ + (HP) = (((my_ret != 0) || (my_hp != &TMP.ent)) \ + ? 0 \ + : &TMP.ent); \ + (ERR) = my_h_err; \ } #else typedef struct { struct hostent ent; char buf[8192]; } GET_HOST_TMP; -#define GET_HOST_BY_NAME(NAME, HP, ERR, TMP) \ - { \ - int my_h_err; \ - (HP) = gethostbyname_r((NAME), &TMP.ent, \ - TMP.buf, sizeof (TMP.buf), &my_h_err); \ - (ERR) = my_h_err; \ +#define GET_HOST_BY_NAME(NAME, HP, ERR, TMP) \ + { \ + int my_h_err; \ + (HP) = gethostbyname_r((NAME), &TMP.ent, \ + TMP.buf, sizeof (TMP.buf), &my_h_err); \ + (ERR) = my_h_err; \ } -#define GET_HOST_BY_ADDR(ADDR, ADDRLEN, FAMILY, HP, ERR, TMP) \ - { \ - int my_h_err; \ - (HP) = gethostbyaddr_r((ADDR), (ADDRLEN), (FAMILY), &TMP.ent, \ - TMP.buf, sizeof (TMP.buf), &my_h_err); \ - (ERR) = my_h_err; \ +#define GET_HOST_BY_ADDR(ADDR, ADDRLEN, FAMILY, HP, ERR, TMP) \ + { \ + int my_h_err; \ + (HP) = gethostbyaddr_r((ADDR), (ADDRLEN), (FAMILY), &TMP.ent, \ + TMP.buf, sizeof (TMP.buf), &my_h_err); \ + (ERR) = my_h_err; \ } #endif /* returns int? */ #endif /* _AIX */ @@ -257,9 +262,9 @@ typedef struct { /* Now do the same for getservby* functions. */ #ifndef HAVE_GETSERVBYNAME_R typedef struct servent *GET_SERV_TMP; -#define GET_SERV_BY_NAME(NAME, PROTO, SP, ERR, TMP) \ +#define GET_SERV_BY_NAME(NAME, PROTO, SP, ERR, TMP) \ (TMP = getservbyname (NAME, PROTO), (SP) = TMP, (ERR) = (SP) ? 0 : -1) -#define GET_SERV_BY_PORT(PORT, PROTO, SP, ERR, TMP) \ +#define GET_SERV_BY_PORT(PORT, PROTO, SP, ERR, TMP) \ (TMP = getservbyport (PORT, PROTO), (SP) = TMP, (ERR) = (SP) ? 0 : -1) #else #ifdef GETSERVBYNAME_R_RETURNS_INT @@ -267,27 +272,27 @@ typedef struct { struct servent ent; char buf[8192]; } GET_SERV_TMP; -#define GET_SERV_BY_NAME(NAME, PROTO, SP, ERR, TMP) \ - { \ - struct servent *my_sp; \ - int my_s_err; \ - (SP) = (getservbyname_r((NAME), (PROTO), &TMP.ent, \ - TMP.buf, sizeof (TMP.buf), &my_sp, \ - &my_s_err) \ - ? 0 \ - : &TMP.ent); \ - (ERR) = my_s_err; \ +#define GET_SERV_BY_NAME(NAME, PROTO, SP, ERR, TMP) \ + { \ + struct servent *my_sp; \ + int my_s_err; \ + (SP) = (getservbyname_r((NAME), (PROTO), &TMP.ent, \ + TMP.buf, sizeof (TMP.buf), &my_sp, \ + &my_s_err) \ + ? 0 \ + : &TMP.ent); \ + (ERR) = my_s_err; \ } -#define GET_SERV_BY_PORT(PORT, PROTO, SP, ERR, TMP) \ - { \ - struct servent *my_sp; \ - int my_s_err; \ - (SP) = (getservbyport_r((PORT), (PROTO), &TMP.ent, \ - TMP.buf, sizeof (TMP.buf), &my_sp, \ - &my_s_err) \ - ? 0 \ - : &TMP.ent); \ - (ERR) = my_s_err; \ +#define GET_SERV_BY_PORT(PORT, PROTO, SP, ERR, TMP) \ + { \ + struct servent *my_sp; \ + int my_s_err; \ + (SP) = (getservbyport_r((PORT), (PROTO), &TMP.ent, \ + TMP.buf, sizeof (TMP.buf), &my_sp, \ + &my_s_err) \ + ? 0 \ + : &TMP.ent); \ + (ERR) = my_s_err; \ } #else /* returns ptr -- IRIX? */ @@ -295,21 +300,21 @@ typedef struct { struct servent ent; char buf[8192]; } GET_SERV_TMP; -#define GET_SERV_BY_NAME(NAME, PROTO, SP, ERR, TMP) \ - { \ - (SP) = getservbyname_r((NAME), (PROTO), &TMP.ent, \ - TMP.buf, sizeof (TMP.buf)); \ - (ERR) = (SP) == NULL; \ +#define GET_SERV_BY_NAME(NAME, PROTO, SP, ERR, TMP) \ + { \ + (SP) = getservbyname_r((NAME), (PROTO), &TMP.ent, \ + TMP.buf, sizeof (TMP.buf)); \ + (ERR) = (SP) == NULL; \ } -#define GET_SERV_BY_PORT(PORT, PROTO, SP, ERR, TMP) \ - { \ - struct servent *my_sp; \ - my_sp = getservbyport_r((PORT), (PROTO), &TMP.ent, \ - TMP.buf, sizeof (TMP.buf)); \ - (SP) = my_sp; \ - (ERR) = my_sp == 0; \ - (ERR) = (ERR); /* avoid "unused" warning */ \ +#define GET_SERV_BY_PORT(PORT, PROTO, SP, ERR, TMP) \ + { \ + struct servent *my_sp; \ + my_sp = getservbyport_r((PORT), (PROTO), &TMP.ent, \ + TMP.buf, sizeof (TMP.buf)); \ + (SP) = my_sp; \ + (ERR) = my_sp == 0; \ + (ERR) = (ERR); /* avoid "unused" warning */ \ } #endif #endif @@ -317,8 +322,8 @@ typedef struct { #if defined(WRAP_GETADDRINFO) || defined(FAI_CACHE) static inline int system_getaddrinfo (const char *name, const char *serv, - const struct addrinfo *hint, - struct addrinfo **res) + const struct addrinfo *hint, + struct addrinfo **res) { return getaddrinfo(name, serv, hint, res); } @@ -336,8 +341,8 @@ system_freeaddrinfo (struct addrinfo *ai) but we don't have an autoconf test for that right now. */ static inline int system_getnameinfo (const struct sockaddr *sa, socklen_t salen, - char *host, size_t hostlen, char *serv, size_t servlen, - int flags) + char *host, size_t hostlen, char *serv, size_t servlen, + int flags) { return getnameinfo(sa, salen, host, hostlen, serv, servlen, flags); } @@ -346,16 +351,16 @@ system_getnameinfo (const struct sockaddr *sa, socklen_t salen, #if !defined (HAVE_GETADDRINFO) || defined(WRAP_GETADDRINFO) || defined(FAI_CACHE) #undef getaddrinfo -#define getaddrinfo my_fake_getaddrinfo +#define getaddrinfo my_fake_getaddrinfo #undef freeaddrinfo -#define freeaddrinfo my_fake_freeaddrinfo +#define freeaddrinfo my_fake_freeaddrinfo #endif #if !defined (HAVE_GETADDRINFO) #undef gai_strerror -#define gai_strerror my_fake_gai_strerror +#define gai_strerror my_fake_gai_strerror #endif /* ! HAVE_GETADDRINFO */ @@ -400,8 +405,8 @@ static const char *socktypename (int t, char *buf, size_t bufsize) { static const char *familyname (int f, char *buf, size_t bufsize) { switch (f) { default: - snprintf(buf, bufsize, "AF %d", f); - return buf; + snprintf(buf, bufsize, "AF %d", f); + return buf; case AF_INET: return "AF_INET"; case AF_INET6: return "AF_INET6"; #ifdef AF_UNIX @@ -411,35 +416,35 @@ static const char *familyname (int f, char *buf, size_t bufsize) { } static void debug_dump_getaddrinfo_args (const char *name, const char *serv, - const struct addrinfo *hint) + const struct addrinfo *hint) { const char *sep; fprintf(stderr, - "getaddrinfo(hostname %s, service %s,\n" - " hints { ", - name ? name : "(null)", serv ? serv : "(null)"); + "getaddrinfo(hostname %s, service %s,\n" + " hints { ", + name ? name : "(null)", serv ? serv : "(null)"); if (hint) { - char buf[30]; - sep = ""; + char buf[30]; + sep = ""; #define Z(FLAG) if (hint->ai_flags & AI_##FLAG) fprintf(stderr, "%s%s", sep, #FLAG), sep = "|" - Z(CANONNAME); - Z(PASSIVE); + Z(CANONNAME); + Z(PASSIVE); #ifdef AI_NUMERICHOST - Z(NUMERICHOST); -#endif - if (sep[0] == 0) - fprintf(stderr, "no-flags"); - if (hint->ai_family) - fprintf(stderr, " %s", familyname(hint->ai_family, buf, - sizeof(buf))); - if (hint->ai_socktype) - fprintf(stderr, " SOCK_%s", socktypename(hint->ai_socktype, buf, - sizeof(buf))); - if (hint->ai_protocol) - fprintf(stderr, " IPPROTO_%s", protoname(hint->ai_protocol, buf, - sizeof(buf))); + Z(NUMERICHOST); +#endif + if (sep[0] == 0) + fprintf(stderr, "no-flags"); + if (hint->ai_family) + fprintf(stderr, " %s", familyname(hint->ai_family, buf, + sizeof(buf))); + if (hint->ai_socktype) + fprintf(stderr, " SOCK_%s", socktypename(hint->ai_socktype, buf, + sizeof(buf))); + if (hint->ai_protocol) + fprintf(stderr, " IPPROTO_%s", protoname(hint->ai_protocol, buf, + sizeof(buf))); } else - fprintf(stderr, "(null)"); + fprintf(stderr, "(null)"); fprintf(stderr, " }):\n"); } @@ -454,17 +459,17 @@ static void debug_dump_addrinfos (const struct addrinfo *ai) char buf[10]; fprintf(stderr, "addrinfos returned:\n"); while (ai) { - fprintf(stderr, "%p...", ai); - fprintf(stderr, " socktype=%s", socktypename(ai->ai_socktype, buf, - sizeof(buf))); - fprintf(stderr, " ai_family=%s", familyname(ai->ai_family, buf, - sizeof(buf))); - if (ai->ai_family != ai->ai_addr->sa_family) - fprintf(stderr, " sa_family=%s", - familyname(ai->ai_addr->sa_family, buf, sizeof(buf))); - fprintf(stderr, "\n"); - ai = ai->ai_next; - count++; + fprintf(stderr, "%p...", ai); + fprintf(stderr, " socktype=%s", socktypename(ai->ai_socktype, buf, + sizeof(buf))); + fprintf(stderr, " ai_family=%s", familyname(ai->ai_family, buf, + sizeof(buf))); + if (ai->ai_family != ai->ai_addr->sa_family) + fprintf(stderr, " sa_family=%s", + familyname(ai->ai_addr->sa_family, buf, sizeof(buf))); + fprintf(stderr, "\n"); + ai = ai->ai_next; + count++; } fprintf(stderr, "end addrinfos returned (%d)\n"); } @@ -475,7 +480,7 @@ static void debug_dump_addrinfos (const struct addrinfo *ai) static int getaddrinfo (const char *name, const char *serv, - const struct addrinfo *hint, struct addrinfo **result); + const struct addrinfo *hint, struct addrinfo **result); static void freeaddrinfo (struct addrinfo *ai); @@ -491,7 +496,7 @@ void freeaddrinfo (struct addrinfo *ai); #define HAVE_GETNAMEINFO 1 #undef getnameinfo -#define getnameinfo my_fake_getnameinfo +#define getnameinfo my_fake_getnameinfo static char *gai_strerror (int code); @@ -501,9 +506,9 @@ char *gai_strerror (int code); #if !defined (HAVE_GETADDRINFO) static int getnameinfo (const struct sockaddr *addr, socklen_t len, - char *host, socklen_t hostlen, - char *service, socklen_t servicelen, - int flags); + char *host, socklen_t hostlen, + char *service, socklen_t servicelen, + int flags); #endif /* Fudge things on older gai implementations. */ @@ -541,45 +546,45 @@ int getnameinfo (const struct sockaddr *addr, socklen_t len, static inline int translate_h_errno (int h); static inline int fai_add_entry (struct addrinfo **result, void *addr, - int port, const struct addrinfo *template) + int port, const struct addrinfo *template) { struct addrinfo *n = malloc (sizeof (struct addrinfo)); if (n == 0) - return EAI_MEMORY; + return EAI_MEMORY; if (template->ai_family != AF_INET #ifdef KRB5_USE_INET6 - && template->ai_family != AF_INET6 + && template->ai_family != AF_INET6 #endif - ) - return EAI_FAMILY; + ) + return EAI_FAMILY; *n = *template; if (template->ai_family == AF_INET) { - struct sockaddr_in *sin4; - sin4 = malloc (sizeof (struct sockaddr_in)); - if (sin4 == 0) - return EAI_MEMORY; + struct sockaddr_in *sin4; + sin4 = malloc (sizeof (struct sockaddr_in)); + if (sin4 == 0) + return EAI_MEMORY; memset (sin4, 0, sizeof (struct sockaddr_in)); /* for sin_zero */ - n->ai_addr = (struct sockaddr *) sin4; - sin4->sin_family = AF_INET; - sin4->sin_addr = *(struct in_addr *)addr; - sin4->sin_port = port; + n->ai_addr = (struct sockaddr *) sin4; + sin4->sin_family = AF_INET; + sin4->sin_addr = *(struct in_addr *)addr; + sin4->sin_port = port; #ifdef HAVE_SA_LEN - sin4->sin_len = sizeof (struct sockaddr_in); + sin4->sin_len = sizeof (struct sockaddr_in); #endif } #ifdef KRB5_USE_INET6 if (template->ai_family == AF_INET6) { - struct sockaddr_in6 *sin6; - sin6 = malloc (sizeof (struct sockaddr_in6)); - if (sin6 == 0) - return EAI_MEMORY; + struct sockaddr_in6 *sin6; + sin6 = malloc (sizeof (struct sockaddr_in6)); + if (sin6 == 0) + return EAI_MEMORY; memset (sin6, 0, sizeof (struct sockaddr_in6)); /* for sin_zero */ - n->ai_addr = (struct sockaddr *) sin6; - sin6->sin6_family = AF_INET6; - sin6->sin6_addr = *(struct in6_addr *)addr; - sin6->sin6_port = port; + n->ai_addr = (struct sockaddr *) sin6; + sin6->sin6_family = AF_INET6; + sin6->sin6_addr = *(struct in6_addr *)addr; + sin6->sin6_port = port; #ifdef HAVE_SA_LEN - sin6->sin6_len = sizeof (struct sockaddr_in6); + sin6->sin6_len = sizeof (struct sockaddr_in6); #endif } #endif @@ -590,21 +595,21 @@ static inline int fai_add_entry (struct addrinfo **result, void *addr, #ifdef FAI_CACHE /* fake addrinfo cache entries */ -#define CACHE_ENTRY_LIFETIME 15 /* seconds */ +#define CACHE_ENTRY_LIFETIME 15 /* seconds */ static void plant_face (const char *name, struct face *entry) { entry->name = strdup(name); if (entry->name == NULL) - /* @@ Wastes memory. */ - return; + /* @@ Wastes memory. */ + return; k5_mutex_assert_locked(&krb5int_fac.lock); entry->next = krb5int_fac.data; entry->expiration = time(0) + CACHE_ENTRY_LIFETIME; krb5int_fac.data = entry; #ifdef DEBUG_ADDRINFO printf("added cache entry '%s' at %p: %d ipv4, %d ipv6; expire %d\n", - name, entry, entry->naddrs4, entry->naddrs6, entry->expiration); + name, entry, entry->naddrs4, entry->naddrs6, entry->expiration); #endif } @@ -620,38 +625,38 @@ static int find_face (const char *name, struct face **entry) #endif k5_mutex_assert_locked(&krb5int_fac.lock); for (fpp = &krb5int_fac.data; *fpp; ) { - fp = *fpp; + fp = *fpp; #ifdef DEBUG_ADDRINFO - printf(" checking expiration time of @%p: %d\n", - fp, fp->expiration); + printf(" checking expiration time of @%p: %d\n", + fp, fp->expiration); #endif - if (fp->expiration < now) { + if (fp->expiration < now) { #ifdef DEBUG_ADDRINFO - printf("\texpiring cache entry\n"); -#endif - free(fp->name); - free(fp->canonname); - free(fp->addrs4); - free(fp->addrs6); - *fpp = fp->next; - free(fp); - /* Stay at this point in the list, and check again. */ - } else - /* Move forward. */ - fpp = &(*fpp)->next; + printf("\texpiring cache entry\n"); +#endif + free(fp->name); + free(fp->canonname); + free(fp->addrs4); + free(fp->addrs6); + *fpp = fp->next; + free(fp); + /* Stay at this point in the list, and check again. */ + } else + /* Move forward. */ + fpp = &(*fpp)->next; } for (fp = krb5int_fac.data; fp; fp = fp->next) { #ifdef DEBUG_ADDRINFO - printf(" comparing entry @%p\n", fp); + printf(" comparing entry @%p\n", fp); #endif - if (!strcasecmp(fp->name, name)) { + if (!strcasecmp(fp->name, name)) { #ifdef DEBUG_ADDRINFO - printf("\tMATCH!\n"); + printf("\tMATCH!\n"); #endif - *entry = fp; - return 1; - } + *entry = fp; + return 1; + } } return 0; } @@ -663,9 +668,9 @@ static int krb5int_lock_fac(void), krb5int_unlock_fac(void); #endif static inline int fai_add_hosts_by_name (const char *name, - struct addrinfo *template, - int portnum, int flags, - struct addrinfo **result) + struct addrinfo *template, + int portnum, int flags, + struct addrinfo **result) { #ifdef FAI_CACHE @@ -674,127 +679,127 @@ static inline int fai_add_hosts_by_name (const char *name, err = krb5int_lock_fac(); if (err) { - errno = err; - return EAI_SYSTEM; + errno = err; + return EAI_SYSTEM; } if (!find_face(name, &ce)) { - struct addrinfo myhints = { 0 }, *ai, *ai2; - int i4, i6, aierr; + struct addrinfo myhints = { 0 }, *ai, *ai2; + int i4, i6, aierr; #ifdef DEBUG_ADDRINFO - printf("looking up new data for '%s'...\n", name); -#endif - myhints.ai_socktype = SOCK_STREAM; - myhints.ai_flags = AI_CANONNAME; - /* Don't set ai_family -- we want to cache all address types, - because the next lookup may not use the same constraints as - the current one. We *could* cache them separately, so that - we never have to look up an IPv6 address if we are always - asked for IPv4 only, but let's deal with that later, if we - have to. */ - /* Try NULL for the service for now. - - It would be nice to use the requested service name, and not - have to patch things up, but then we'd be doing multiple - queries for the same host when we get different services. - We were using "telnet" for a little more confidence that - getaddrinfo would heed the hints to only give us stream - socket types (with no socket type and null service name, we - might get stream *and* dgram *and* raw, for each address, - or only raw). The RFC 3493 description of ai_socktype - sometimes associates it with the specified service, - sometimes not. - - But on Mac OS X (10.3, 10.4) they've "extended" getaddrinfo - to make SRV RR queries. (Please, somebody, show me - something in the specs that actually supports this? RFC - 3493 says nothing about it, but it does say getaddrinfo is - the new way to look up hostnames. RFC 2782 says SRV - records should *not* be used unless the application - protocol spec says to do so. The Telnet spec does not say - to do it.) And then they complain when our code - "unexpectedly" seems to use this "extension" in cases where - they don't want it to be used. - - Fortunately, it appears that if we specify ai_socktype as - SOCK_STREAM and use a null service name, we only get one - copy of each address on all the platforms I've tried, - although it may not have ai_socktype filled in properly. - So, we'll fudge it with that for now. */ - aierr = system_getaddrinfo(name, NULL, &myhints, &ai); - if (aierr) { - krb5int_unlock_fac(); - return aierr; - } - ce = malloc(sizeof(struct face)); - memset(ce, 0, sizeof(*ce)); - ce->expiration = time(0) + 30; - for (ai2 = ai; ai2; ai2 = ai2->ai_next) { + printf("looking up new data for '%s'...\n", name); +#endif + myhints.ai_socktype = SOCK_STREAM; + myhints.ai_flags = AI_CANONNAME; + /* Don't set ai_family -- we want to cache all address types, + because the next lookup may not use the same constraints as + the current one. We *could* cache them separately, so that + we never have to look up an IPv6 address if we are always + asked for IPv4 only, but let's deal with that later, if we + have to. */ + /* Try NULL for the service for now. + + It would be nice to use the requested service name, and not + have to patch things up, but then we'd be doing multiple + queries for the same host when we get different services. + We were using "telnet" for a little more confidence that + getaddrinfo would heed the hints to only give us stream + socket types (with no socket type and null service name, we + might get stream *and* dgram *and* raw, for each address, + or only raw). The RFC 3493 description of ai_socktype + sometimes associates it with the specified service, + sometimes not. + + But on Mac OS X (10.3, 10.4) they've "extended" getaddrinfo + to make SRV RR queries. (Please, somebody, show me + something in the specs that actually supports this? RFC + 3493 says nothing about it, but it does say getaddrinfo is + the new way to look up hostnames. RFC 2782 says SRV + records should *not* be used unless the application + protocol spec says to do so. The Telnet spec does not say + to do it.) And then they complain when our code + "unexpectedly" seems to use this "extension" in cases where + they don't want it to be used. + + Fortunately, it appears that if we specify ai_socktype as + SOCK_STREAM and use a null service name, we only get one + copy of each address on all the platforms I've tried, + although it may not have ai_socktype filled in properly. + So, we'll fudge it with that for now. */ + aierr = system_getaddrinfo(name, NULL, &myhints, &ai); + if (aierr) { + krb5int_unlock_fac(); + return aierr; + } + ce = malloc(sizeof(struct face)); + memset(ce, 0, sizeof(*ce)); + ce->expiration = time(0) + 30; + for (ai2 = ai; ai2; ai2 = ai2->ai_next) { #ifdef DEBUG_ADDRINFO - printf(" found an address in family %d...\n", ai2->ai_family); -#endif - switch (ai2->ai_family) { - case AF_INET: - ce->naddrs4++; - break; - case AF_INET6: - ce->naddrs6++; - break; - default: - break; - } - } - ce->addrs4 = calloc(ce->naddrs4, sizeof(*ce->addrs4)); - if (ce->addrs4 == NULL && ce->naddrs4 != 0) { - krb5int_unlock_fac(); - system_freeaddrinfo(ai); - return EAI_MEMORY; - } - ce->addrs6 = calloc(ce->naddrs6, sizeof(*ce->addrs6)); - if (ce->addrs6 == NULL && ce->naddrs6 != 0) { - krb5int_unlock_fac(); - free(ce->addrs4); - system_freeaddrinfo(ai); - return EAI_MEMORY; - } - for (ai2 = ai, i4 = i6 = 0; ai2; ai2 = ai2->ai_next) { - switch (ai2->ai_family) { - case AF_INET: - ce->addrs4[i4++] = ((struct sockaddr_in *)ai2->ai_addr)->sin_addr; - break; - case AF_INET6: - ce->addrs6[i6++] = ((struct sockaddr_in6 *)ai2->ai_addr)->sin6_addr; - break; - default: - break; - } - } - ce->canonname = ai->ai_canonname ? strdup(ai->ai_canonname) : 0; - system_freeaddrinfo(ai); - plant_face(name, ce); + printf(" found an address in family %d...\n", ai2->ai_family); +#endif + switch (ai2->ai_family) { + case AF_INET: + ce->naddrs4++; + break; + case AF_INET6: + ce->naddrs6++; + break; + default: + break; + } + } + ce->addrs4 = calloc(ce->naddrs4, sizeof(*ce->addrs4)); + if (ce->addrs4 == NULL && ce->naddrs4 != 0) { + krb5int_unlock_fac(); + system_freeaddrinfo(ai); + return EAI_MEMORY; + } + ce->addrs6 = calloc(ce->naddrs6, sizeof(*ce->addrs6)); + if (ce->addrs6 == NULL && ce->naddrs6 != 0) { + krb5int_unlock_fac(); + free(ce->addrs4); + system_freeaddrinfo(ai); + return EAI_MEMORY; + } + for (ai2 = ai, i4 = i6 = 0; ai2; ai2 = ai2->ai_next) { + switch (ai2->ai_family) { + case AF_INET: + ce->addrs4[i4++] = ((struct sockaddr_in *)ai2->ai_addr)->sin_addr; + break; + case AF_INET6: + ce->addrs6[i6++] = ((struct sockaddr_in6 *)ai2->ai_addr)->sin6_addr; + break; + default: + break; + } + } + ce->canonname = ai->ai_canonname ? strdup(ai->ai_canonname) : 0; + system_freeaddrinfo(ai); + plant_face(name, ce); } template->ai_family = AF_INET6; template->ai_addrlen = sizeof(struct sockaddr_in6); for (i = 0; i < ce->naddrs6; i++) { - r = fai_add_entry (result, &ce->addrs6[i], portnum, template); - if (r) { - krb5int_unlock_fac(); - return r; - } + r = fai_add_entry (result, &ce->addrs6[i], portnum, template); + if (r) { + krb5int_unlock_fac(); + return r; + } } template->ai_family = AF_INET; template->ai_addrlen = sizeof(struct sockaddr_in); for (i = 0; i < ce->naddrs4; i++) { - r = fai_add_entry (result, &ce->addrs4[i], portnum, template); - if (r) { - krb5int_unlock_fac(); - return r; - } + r = fai_add_entry (result, &ce->addrs4[i], portnum, template); + if (r) { + krb5int_unlock_fac(); + return r; + } } if (*result && (flags & AI_CANONNAME)) - (*result)->ai_canonname = (ce->canonname - ? strdup(ce->canonname) - : NULL); + (*result)->ai_canonname = (ce->canonname + ? strdup(ce->canonname) + : NULL); krb5int_unlock_fac(); return 0; @@ -807,14 +812,14 @@ static inline int fai_add_hosts_by_name (const char *name, GET_HOST_BY_NAME (name, hp, herr, htmp); if (hp == 0) - return translate_h_errno (herr); + return translate_h_errno (herr); for (i = 0; hp->h_addr_list[i]; i++) { - r = fai_add_entry (result, hp->h_addr_list[i], portnum, template); - if (r) - return r; + r = fai_add_entry (result, hp->h_addr_list[i], portnum, template); + if (r) + return r; } if (*result && (flags & AI_CANONNAME)) - (*result)->ai_canonname = strdup (hp->h_name); + (*result)->ai_canonname = strdup (hp->h_name); return 0; #endif @@ -825,19 +830,19 @@ fake_freeaddrinfo (struct addrinfo *ai) { struct addrinfo *next; while (ai) { - next = ai->ai_next; - if (ai->ai_canonname) - free (ai->ai_canonname); - if (ai->ai_addr) - free (ai->ai_addr); - free (ai); - ai = next; + next = ai->ai_next; + if (ai->ai_canonname) + free (ai->ai_canonname); + if (ai->ai_addr) + free (ai->ai_addr); + free (ai); + ai = next; } } static inline int fake_getaddrinfo (const char *name, const char *serv, - const struct addrinfo *hint, struct addrinfo **result) + const struct addrinfo *hint, struct addrinfo **result) { struct addrinfo *res = 0; int ret; @@ -850,49 +855,49 @@ fake_getaddrinfo (const char *name, const char *serv, #endif if (hint != 0) { - if (hint->ai_family != 0 && hint->ai_family != AF_INET) - return EAI_NODATA; - socktype = hint->ai_socktype; - flags = hint->ai_flags; + if (hint->ai_family != 0 && hint->ai_family != AF_INET) + return EAI_NODATA; + socktype = hint->ai_socktype; + flags = hint->ai_flags; } else { - socktype = 0; - flags = 0; + socktype = 0; + flags = 0; } if (serv) { - size_t numlen = strspn (serv, "0123456789"); - if (serv[numlen] == '\0') { - /* pure numeric */ - unsigned long p = strtoul (serv, 0, 10); - if (p == 0 || p > 65535) - return EAI_NONAME; - port = htons (p); - } else { - struct servent *sp; - int try_dgram_too = 0, s_err; - GET_SERV_TMP stmp; - - if (socktype == 0) { - try_dgram_too = 1; - socktype = SOCK_STREAM; - } - try_service_lookup: - GET_SERV_BY_NAME(serv, socktype == SOCK_STREAM ? "tcp" : "udp", - sp, s_err, stmp); - if (sp == 0) { - if (try_dgram_too) { - socktype = SOCK_DGRAM; - goto try_service_lookup; - } - return EAI_SERVICE; - } - port = sp->s_port; - } + size_t numlen = strspn (serv, "0123456789"); + if (serv[numlen] == '\0') { + /* pure numeric */ + unsigned long p = strtoul (serv, 0, 10); + if (p == 0 || p > 65535) + return EAI_NONAME; + port = htons (p); + } else { + struct servent *sp; + int try_dgram_too = 0, s_err; + GET_SERV_TMP stmp; + + if (socktype == 0) { + try_dgram_too = 1; + socktype = SOCK_STREAM; + } + try_service_lookup: + GET_SERV_BY_NAME(serv, socktype == SOCK_STREAM ? "tcp" : "udp", + sp, s_err, stmp); + if (sp == 0) { + if (try_dgram_too) { + socktype = SOCK_DGRAM; + goto try_service_lookup; + } + return EAI_SERVICE; + } + port = sp->s_port; + } } if (name == 0) { - name = (flags & AI_PASSIVE) ? "0.0.0.0" : "127.0.0.1"; - flags |= AI_NUMERICHOST; + name = (flags & AI_PASSIVE) ? "0.0.0.0" : "127.0.0.1"; + flags |= AI_NUMERICHOST; } template.ai_family = AF_INET; @@ -907,29 +912,29 @@ fake_getaddrinfo (const char *name, const char *serv, /* If NUMERICHOST is set, parse a numeric address. If it's not set, don't accept such names. */ if (flags & AI_NUMERICHOST) { - struct in_addr addr4; + struct in_addr addr4; #if 0 - ret = inet_aton (name, &addr4); - if (ret) - return EAI_NONAME; + ret = inet_aton (name, &addr4); + if (ret) + return EAI_NONAME; #else - addr4.s_addr = inet_addr (name); - if (addr4.s_addr == 0xffffffff || addr4.s_addr == -1) - /* 255.255.255.255 or parse error, both bad */ - return EAI_NONAME; + addr4.s_addr = inet_addr (name); + if (addr4.s_addr == 0xffffffff || addr4.s_addr == -1) + /* 255.255.255.255 or parse error, both bad */ + return EAI_NONAME; #endif - ret = fai_add_entry (&res, &addr4, port, &template); + ret = fai_add_entry (&res, &addr4, port, &template); } else { - ret = fai_add_hosts_by_name (name, &template, port, flags, - &res); + ret = fai_add_hosts_by_name (name, &template, port, flags, + &res); } if (ret && ret != NO_ADDRESS) { - fake_freeaddrinfo (res); - return ret; + fake_freeaddrinfo (res); + return ret; } if (res == 0) - return NO_ADDRESS; + return NO_ADDRESS; *result = res; return 0; } @@ -937,9 +942,9 @@ fake_getaddrinfo (const char *name, const char *serv, #ifdef NEED_FAKE_GETNAMEINFO static inline int fake_getnameinfo (const struct sockaddr *sa, socklen_t len, - char *host, socklen_t hostlen, - char *service, socklen_t servicelen, - int flags) + char *host, socklen_t hostlen, + char *service, socklen_t servicelen, + int flags) { struct hostent *hp; const struct sockaddr_in *sinp; @@ -947,83 +952,83 @@ fake_getnameinfo (const struct sockaddr *sa, socklen_t len, size_t hlen, slen; if (sa->sa_family != AF_INET) { - return EAI_FAMILY; + return EAI_FAMILY; } sinp = (const struct sockaddr_in *) sa; hlen = hostlen; if (hostlen < 0 || hlen != hostlen) { - errno = EINVAL; - return EAI_SYSTEM; + errno = EINVAL; + return EAI_SYSTEM; } slen = servicelen; if (servicelen < 0 || slen != servicelen) { - errno = EINVAL; - return EAI_SYSTEM; + errno = EINVAL; + return EAI_SYSTEM; } if (host) { - if (flags & NI_NUMERICHOST) { + if (flags & NI_NUMERICHOST) { #if (defined(__GNUC__) && defined(__mips__)) || 1 /* thread safety always */ - /* The inet_ntoa call, passing a struct, fails on IRIX 6.5 - using gcc 2.95; we get back "0.0.0.0". Since this in a - configuration still important at Athena, here's the - workaround, which also happens to be thread-safe.... */ - const unsigned char *uc; - char tmpbuf[20]; - numeric_host: - uc = (const unsigned char *) &sinp->sin_addr; - snprintf(tmpbuf, sizeof(tmpbuf), "%d.%d.%d.%d", - uc[0], uc[1], uc[2], uc[3]); - strncpy(host, tmpbuf, hlen); + /* The inet_ntoa call, passing a struct, fails on IRIX 6.5 + using gcc 2.95; we get back "0.0.0.0". Since this in a + configuration still important at Athena, here's the + workaround, which also happens to be thread-safe.... */ + const unsigned char *uc; + char tmpbuf[20]; + numeric_host: + uc = (const unsigned char *) &sinp->sin_addr; + snprintf(tmpbuf, sizeof(tmpbuf), "%d.%d.%d.%d", + uc[0], uc[1], uc[2], uc[3]); + strncpy(host, tmpbuf, hlen); #else - char *p; - numeric_host: - p = inet_ntoa (sinp->sin_addr); - strncpy (host, p, hlen); -#endif - } else { - int herr; - GET_HOST_TMP htmp; - - GET_HOST_BY_ADDR((const char *) &sinp->sin_addr, - sizeof (struct in_addr), - sa->sa_family, hp, herr, htmp); - if (hp == 0) { - if (herr == NO_ADDRESS && !(flags & NI_NAMEREQD)) /* ??? */ - goto numeric_host; - return translate_h_errno (herr); - } - /* According to the Open Group spec, getnameinfo can - silently truncate, but must still return a - null-terminated string. */ - strncpy (host, hp->h_name, hlen); - } - host[hostlen-1] = 0; + char *p; + numeric_host: + p = inet_ntoa (sinp->sin_addr); + strncpy (host, p, hlen); +#endif + } else { + int herr; + GET_HOST_TMP htmp; + + GET_HOST_BY_ADDR((const char *) &sinp->sin_addr, + sizeof (struct in_addr), + sa->sa_family, hp, herr, htmp); + if (hp == 0) { + if (herr == NO_ADDRESS && !(flags & NI_NAMEREQD)) /* ??? */ + goto numeric_host; + return translate_h_errno (herr); + } + /* According to the Open Group spec, getnameinfo can + silently truncate, but must still return a + null-terminated string. */ + strncpy (host, hp->h_name, hlen); + } + host[hostlen-1] = 0; } if (service) { - if (flags & NI_NUMERICSERV) { - char numbuf[10]; - int port; - numeric_service: - port = ntohs (sinp->sin_port); - if (port < 0 || port > 65535) - return EAI_FAIL; - snprintf (numbuf, sizeof(numbuf), "%d", port); - strncpy (service, numbuf, slen); - } else { - int serr; - GET_SERV_TMP stmp; - - GET_SERV_BY_PORT(sinp->sin_port, - (flags & NI_DGRAM) ? "udp" : "tcp", - sp, serr, stmp); - if (sp == 0) - goto numeric_service; - strncpy (service, sp->s_name, slen); - } - service[servicelen-1] = 0; + if (flags & NI_NUMERICSERV) { + char numbuf[10]; + int port; + numeric_service: + port = ntohs (sinp->sin_port); + if (port < 0 || port > 65535) + return EAI_FAIL; + snprintf (numbuf, sizeof(numbuf), "%d", port); + strncpy (service, numbuf, slen); + } else { + int serr; + GET_SERV_TMP stmp; + + GET_SERV_BY_PORT(sinp->sin_port, + (flags & NI_DGRAM) ? "udp" : "tcp", + sp, serr, stmp); + if (sp == 0) + goto numeric_service; + strncpy (service, sp->s_name, slen); + } + service[servicelen-1] = 0; } return 0; @@ -1037,17 +1042,17 @@ char *gai_strerror (int code) { switch (code) { case EAI_ADDRFAMILY: return "address family for nodename not supported"; - case EAI_AGAIN: return "temporary failure in name resolution"; - case EAI_BADFLAGS: return "bad flags to getaddrinfo/getnameinfo"; - case EAI_FAIL: return "non-recoverable failure in name resolution"; - case EAI_FAMILY: return "ai_family not supported"; - case EAI_MEMORY: return "out of memory"; - case EAI_NODATA: return "no address associated with hostname"; - case EAI_NONAME: return "name does not exist"; - case EAI_SERVICE: return "service name not supported for specified socket type"; - case EAI_SOCKTYPE: return "ai_socktype not supported"; - case EAI_SYSTEM: return strerror (errno); - default: return "bogus getaddrinfo error?"; + case EAI_AGAIN: return "temporary failure in name resolution"; + case EAI_BADFLAGS: return "bad flags to getaddrinfo/getnameinfo"; + case EAI_FAIL: return "non-recoverable failure in name resolution"; + case EAI_FAMILY: return "ai_family not supported"; + case EAI_MEMORY: return "out of memory"; + case EAI_NODATA: return "no address associated with hostname"; + case EAI_NONAME: return "name does not exist"; + case EAI_SERVICE: return "service name not supported for specified socket type"; + case EAI_SOCKTYPE: return "ai_socktype not supported"; + case EAI_SYSTEM: return strerror (errno); + default: return "bogus getaddrinfo error?"; } } #endif @@ -1056,33 +1061,33 @@ static inline int translate_h_errno (int h) { switch (h) { case 0: - return 0; + return 0; #ifdef NETDB_INTERNAL case NETDB_INTERNAL: - if (errno == ENOMEM) - return EAI_MEMORY; - return EAI_SYSTEM; + if (errno == ENOMEM) + return EAI_MEMORY; + return EAI_SYSTEM; #endif case HOST_NOT_FOUND: - return EAI_NONAME; + return EAI_NONAME; case TRY_AGAIN: - return EAI_AGAIN; + return EAI_AGAIN; case NO_RECOVERY: - return EAI_FAIL; + return EAI_FAIL; case NO_DATA: #if NO_DATA != NO_ADDRESS case NO_ADDRESS: #endif - return EAI_NODATA; + return EAI_NODATA; default: - return EAI_SYSTEM; + return EAI_SYSTEM; } } #if defined(HAVE_FAKE_GETADDRINFO) || defined(FAI_CACHE) static inline int getaddrinfo (const char *name, const char *serv, - const struct addrinfo *hint, struct addrinfo **result) + const struct addrinfo *hint, struct addrinfo **result) { return fake_getaddrinfo(name, serv, hint, result); } @@ -1096,12 +1101,12 @@ void freeaddrinfo (struct addrinfo *ai) #ifdef NEED_FAKE_GETNAMEINFO static inline int getnameinfo (const struct sockaddr *sa, socklen_t len, - char *host, socklen_t hostlen, - char *service, socklen_t servicelen, - int flags) + char *host, socklen_t hostlen, + char *service, socklen_t servicelen, + int flags) { return fake_getnameinfo(sa, len, host, hostlen, service, servicelen, - flags); + flags); } #endif /* NEED_FAKE_GETNAMEINFO */ #endif /* HAVE_FAKE_GETADDRINFO */ @@ -1113,7 +1118,7 @@ int getnameinfo (const struct sockaddr *sa, socklen_t len, static inline int getaddrinfo (const char *name, const char *serv, const struct addrinfo *hint, - struct addrinfo **result) + struct addrinfo **result) { int aierr; #if defined(_AIX) || defined(COPY_FIRST_CANONNAME) @@ -1137,32 +1142,32 @@ getaddrinfo (const char *name, const char *serv, const struct addrinfo *hint, code (for "host not found") is returned. If the port maps to a known service for both udp and tcp, all is well. */ if (serv && serv[0] && isdigit(serv[0])) { - unsigned long lport; - char *end; - lport = strtoul(serv, &end, 10); - if (!*end) { - if (lport > 65535) - return EAI_SOCKTYPE; - service_is_numeric = 1; - service_port = htons(lport); + unsigned long lport; + char *end; + lport = strtoul(serv, &end, 10); + if (!*end) { + if (lport > 65535) + return EAI_SOCKTYPE; + service_is_numeric = 1; + service_port = htons(lport); #ifdef AI_NUMERICSERV - if (hint && hint->ai_flags & AI_NUMERICSERV) - serv = "9"; - else -#endif - serv = "discard"; /* defined for both udp and tcp */ - if (hint) - socket_type = hint->ai_socktype; - } + if (hint && hint->ai_flags & AI_NUMERICSERV) + serv = "9"; + else +#endif + serv = "discard"; /* defined for both udp and tcp */ + if (hint) + socket_type = hint->ai_socktype; + } } #endif aierr = system_getaddrinfo (name, serv, hint, result); if (aierr || *result == 0) { #ifdef DEBUG_ADDRINFO - debug_dump_error(aierr); + debug_dump_error(aierr); #endif - return aierr; + return aierr; } /* Linux libc version 6 prior to 2.3.4 is broken. @@ -1221,87 +1226,87 @@ getaddrinfo (const char *name, const char *serv, const struct addrinfo *hint, */ ai = *result; if (ai->ai_canonname) { - struct hostent *hp; - const char *name2 = 0; - int i, herr; - GET_HOST_TMP htmp; - - /* - * Current versions of GET_HOST_BY_NAME will fail if the - * target hostname has IPv6 addresses only. Make sure it - * fails fairly cleanly. - */ - GET_HOST_BY_NAME (name, hp, herr, htmp); - if (hp == 0) { - /* - * This case probably means it's an IPv6-only name. If - * ai_canonname is a numeric address, get rid of it. - */ - if (ai->ai_canonname && strchr(ai->ai_canonname, ':')) - ai->ai_canonname = 0; - name2 = ai->ai_canonname ? ai->ai_canonname : name; - } else { - /* Sometimes gethostbyname will be directed to /etc/hosts - first, and sometimes that file will have entries with - the unqualified name first. So take the first entry - that looks like it could be a FQDN. */ - for (i = 0; hp->h_aliases[i]; i++) { - if (strchr(hp->h_aliases[i], '.') != 0) { - name2 = hp->h_aliases[i]; - break; - } - } - /* Give up, just use the first name (h_name == - h_aliases[0] on all systems I've seen). */ - if (hp->h_aliases[i] == 0) - name2 = hp->h_name; - } - - ai->ai_canonname = strdup(name2); - if (name2 != 0 && ai->ai_canonname == 0) { - system_freeaddrinfo(ai); - *result = 0; + struct hostent *hp; + const char *name2 = 0; + int i, herr; + GET_HOST_TMP htmp; + + /* + * Current versions of GET_HOST_BY_NAME will fail if the + * target hostname has IPv6 addresses only. Make sure it + * fails fairly cleanly. + */ + GET_HOST_BY_NAME (name, hp, herr, htmp); + if (hp == 0) { + /* + * This case probably means it's an IPv6-only name. If + * ai_canonname is a numeric address, get rid of it. + */ + if (ai->ai_canonname && strchr(ai->ai_canonname, ':')) + ai->ai_canonname = 0; + name2 = ai->ai_canonname ? ai->ai_canonname : name; + } else { + /* Sometimes gethostbyname will be directed to /etc/hosts + first, and sometimes that file will have entries with + the unqualified name first. So take the first entry + that looks like it could be a FQDN. */ + for (i = 0; hp->h_aliases[i]; i++) { + if (strchr(hp->h_aliases[i], '.') != 0) { + name2 = hp->h_aliases[i]; + break; + } + } + /* Give up, just use the first name (h_name == + h_aliases[0] on all systems I've seen). */ + if (hp->h_aliases[i] == 0) + name2 = hp->h_name; + } + + ai->ai_canonname = strdup(name2); + if (name2 != 0 && ai->ai_canonname == 0) { + system_freeaddrinfo(ai); + *result = 0; #ifdef DEBUG_ADDRINFO - debug_dump_error(EAI_MEMORY); -#endif - return EAI_MEMORY; - } - /* Zap the remaining ai_canonname fields glibc fills in, in - case the application messes around with the list - structure. */ - while ((ai = ai->ai_next) != NULL) - ai->ai_canonname = 0; + debug_dump_error(EAI_MEMORY); +#endif + return EAI_MEMORY; + } + /* Zap the remaining ai_canonname fields glibc fills in, in + case the application messes around with the list + structure. */ + while ((ai = ai->ai_next) != NULL) + ai->ai_canonname = 0; } #endif #ifdef NUMERIC_SERVICE_BROKEN if (service_port != 0) { - for (ai = *result; ai; ai = ai->ai_next) { - if (socket_type != 0 && ai->ai_socktype == 0) - /* Is this check actually needed? */ - ai->ai_socktype = socket_type; - switch (ai->ai_family) { - case AF_INET: - ((struct sockaddr_in *)ai->ai_addr)->sin_port = service_port; - break; - case AF_INET6: - ((struct sockaddr_in6 *)ai->ai_addr)->sin6_port = service_port; - break; - } - } + for (ai = *result; ai; ai = ai->ai_next) { + if (socket_type != 0 && ai->ai_socktype == 0) + /* Is this check actually needed? */ + ai->ai_socktype = socket_type; + switch (ai->ai_family) { + case AF_INET: + ((struct sockaddr_in *)ai->ai_addr)->sin_port = service_port; + break; + case AF_INET6: + ((struct sockaddr_in6 *)ai->ai_addr)->sin6_port = service_port; + break; + } + } } #endif #ifdef _AIX for (ai = *result; ai; ai = ai->ai_next) { - /* AIX 4.3.3 libc is broken. It doesn't set the family or len - fields of the sockaddr structures. Usually, sa_family is - zero, but I've seen it set to 1 in some cases also (maybe - just leftover from previous contents of the memory - block?). So, always override what libc returned. */ - ai->ai_addr->sa_family = ai->ai_family; + /* AIX 4.3.3 libc is broken. It doesn't set the family or len + fields of the sockaddr structures. Usually, sa_family is + zero, but I've seen it set to 1 in some cases also (maybe + just leftover from previous contents of the memory + block?). So, always override what libc returned. */ + ai->ai_addr->sa_family = ai->ai_family; #ifdef HAVE_SA_LEN /* always true on AIX, actually */ - ai->ai_addr->sa_len = ai->ai_addrlen; + ai->ai_addr->sa_len = ai->ai_addrlen; #endif } #endif @@ -1309,8 +1314,8 @@ getaddrinfo (const char *name, const char *serv, const struct addrinfo *hint, /* Not dealt with currently: - Some versions of GNU libc can lose some IPv4 addresses in - certain cases when multiple IPv4 and IPv6 addresses are - available. */ + certain cases when multiple IPv4 and IPv6 addresses are + available. */ #ifdef DEBUG_ADDRINFO debug_dump_addrinfos(*result); @@ -1324,9 +1329,9 @@ void freeaddrinfo (struct addrinfo *ai) { #ifdef COPY_FIRST_CANONNAME if (ai) { - free(ai->ai_canonname); - ai->ai_canonname = 0; - system_freeaddrinfo(ai); + free(ai->ai_canonname); + ai->ai_canonname = 0; + system_freeaddrinfo(ai); } #else system_freeaddrinfo(ai); @@ -1340,7 +1345,7 @@ static int krb5int_lock_fac (void) int err; err = krb5int_call_thread_support_init(); if (err) - return err; + return err; return k5_mutex_lock(&krb5int_fac.lock); } @@ -1361,8 +1366,8 @@ const char krb5int_in6addr_any = 0; #endif int krb5int_getaddrinfo (const char *node, const char *service, - const struct addrinfo *hints, - struct addrinfo **aip) + const struct addrinfo *hints, + struct addrinfo **aip) { return getaddrinfo(node, service, hints, aip); } @@ -1378,9 +1383,9 @@ const char *krb5int_gai_strerror(int err) } int krb5int_getnameinfo (const struct sockaddr *sa, socklen_t salen, - char *hbuf, size_t hbuflen, - char *sbuf, size_t sbuflen, - int flags) + char *hbuf, size_t hbuflen, + char *sbuf, size_t sbuflen, + int flags) { return getnameinfo(sa, salen, hbuf, hbuflen, sbuf, sbuflen, flags); } diff --git a/src/util/support/gmt_mktime.c b/src/util/support/gmt_mktime.c index c6ec60201..2047a8a01 100644 --- a/src/util/support/gmt_mktime.c +++ b/src/util/support/gmt_mktime.c @@ -1,3 +1,4 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* This code placed in the public domain by Mark W. Eichin */ #include @@ -22,108 +23,108 @@ /* unlike mktime, this does not set them... it only passes a return value. */ static const int days_in_month[12] = { -0, /* jan 31 */ -31, /* feb 28 */ -59, /* mar 31 */ -90, /* apr 30 */ -120, /* may 31 */ -151, /* jun 30 */ -181, /* jul 31 */ -212, /* aug 31 */ -243, /* sep 30 */ -273, /* oct 31 */ -304, /* nov 30 */ -334 /* dec 31 */ + 0, /* jan 31 */ + 31, /* feb 28 */ + 59, /* mar 31 */ + 90, /* apr 30 */ + 120, /* may 31 */ + 151, /* jun 30 */ + 181, /* jul 31 */ + 212, /* aug 31 */ + 243, /* sep 30 */ + 273, /* oct 31 */ + 304, /* nov 30 */ + 334 /* dec 31 */ }; #define hasleapday(year) (year%400?(year%100?(year%4?0:1):0):1) time_t krb5int_gmt_mktime(struct tm *t) { - time_t accum; + time_t accum; #define assert_time(cnd) if(!(cnd)) return (time_t) -1 - /* - * For 32-bit signed time_t centered on 1/1/1970, the range is: - * time 0x80000000 -> Fri Dec 13 16:45:52 1901 - * time 0x7fffffff -> Mon Jan 18 22:14:07 2038 - * - * So years 1901 and 2038 are allowable, but we can't encode all - * dates in those years, and we're not doing overflow/underflow - * checking for such cases. - */ - assert_time(t->tm_year>=1); - assert_time(t->tm_year<=138); - - assert_time(t->tm_mon>=0); - assert_time(t->tm_mon<=11); - assert_time(t->tm_mday>=1); - assert_time(t->tm_mday<=31); - assert_time(t->tm_hour>=0); - assert_time(t->tm_hour<=23); - assert_time(t->tm_min>=0); - assert_time(t->tm_min<=59); - assert_time(t->tm_sec>=0); - assert_time(t->tm_sec<=62); + /* + * For 32-bit signed time_t centered on 1/1/1970, the range is: + * time 0x80000000 -> Fri Dec 13 16:45:52 1901 + * time 0x7fffffff -> Mon Jan 18 22:14:07 2038 + * + * So years 1901 and 2038 are allowable, but we can't encode all + * dates in those years, and we're not doing overflow/underflow + * checking for such cases. + */ + assert_time(t->tm_year>=1); + assert_time(t->tm_year<=138); + + assert_time(t->tm_mon>=0); + assert_time(t->tm_mon<=11); + assert_time(t->tm_mday>=1); + assert_time(t->tm_mday<=31); + assert_time(t->tm_hour>=0); + assert_time(t->tm_hour<=23); + assert_time(t->tm_min>=0); + assert_time(t->tm_min<=59); + assert_time(t->tm_sec>=0); + assert_time(t->tm_sec<=62); #undef assert_time - accum = t->tm_year - 70; - accum *= 365; /* 365 days/normal year */ - - /* add in leap day for all previous years */ - if (t->tm_year >= 70) - accum += (t->tm_year - 69) / 4; - else - accum -= (72 - t->tm_year) / 4; - /* add in leap day for this year */ - if(t->tm_mon >= 2) /* march or later */ - if(hasleapday((t->tm_year + 1900))) accum += 1; - - accum += days_in_month[t->tm_mon]; - accum += t->tm_mday-1; /* days of month are the only 1-based field */ - accum *= 24; /* 24 hour/day */ - accum += t->tm_hour; - accum *= 60; /* 60 minute/hour */ - accum += t->tm_min; - accum *= 60; /* 60 seconds/minute */ - accum += t->tm_sec; - - return accum; + accum = t->tm_year - 70; + accum *= 365; /* 365 days/normal year */ + + /* add in leap day for all previous years */ + if (t->tm_year >= 70) + accum += (t->tm_year - 69) / 4; + else + accum -= (72 - t->tm_year) / 4; + /* add in leap day for this year */ + if(t->tm_mon >= 2) /* march or later */ + if(hasleapday((t->tm_year + 1900))) accum += 1; + + accum += days_in_month[t->tm_mon]; + accum += t->tm_mday-1; /* days of month are the only 1-based field */ + accum *= 24; /* 24 hour/day */ + accum += t->tm_hour; + accum *= 60; /* 60 minute/hour */ + accum += t->tm_min; + accum *= 60; /* 60 seconds/minute */ + accum += t->tm_sec; + + return accum; } #ifdef TEST_LEAP int main (int argc, char *argv[]) { - int yr; - time_t t; - struct tm tm = { - .tm_mon = 0, .tm_mday = 1, - .tm_hour = 0, .tm_min = 0, .tm_sec = 0, - }; - for (yr = 60; yr <= 104; yr++) + int yr; + time_t t; + struct tm tm = { + .tm_mon = 0, .tm_mday = 1, + .tm_hour = 0, .tm_min = 0, .tm_sec = 0, + }; + for (yr = 60; yr <= 104; yr++) { - printf ("1/1/%d%c -> ", 1900 + yr, hasleapday((1900+yr)) ? '*' : ' '); - tm.tm_year = yr; - t = gmt_mktime (&tm); - if (t == (time_t) -1) - printf ("-1\n"); - else - { - long u; - if (t % (24 * 60 * 60)) - printf ("(not integral multiple of days) "); - u = t / (24 * 60 * 60); - printf ("%3ld*365%+ld\t0x%08lx\n", - (long) (u / 365), (long) (u % 365), - (long) t); - } + printf ("1/1/%d%c -> ", 1900 + yr, hasleapday((1900+yr)) ? '*' : ' '); + tm.tm_year = yr; + t = gmt_mktime (&tm); + if (t == (time_t) -1) + printf ("-1\n"); + else + { + long u; + if (t % (24 * 60 * 60)) + printf ("(not integral multiple of days) "); + u = t / (24 * 60 * 60); + printf ("%3ld*365%+ld\t0x%08lx\n", + (long) (u / 365), (long) (u % 365), + (long) t); + } } - t = 0x80000000, printf ("time 0x%lx -> %s", t, ctime (&t)); - t = 0x7fffffff, printf ("time 0x%lx -> %s", t, ctime (&t)); - return 0; + t = 0x80000000, printf ("time 0x%lx -> %s", t, ctime (&t)); + t = 0x7fffffff, printf ("time 0x%lx -> %s", t, ctime (&t)); + return 0; } #endif diff --git a/src/util/support/init-addrinfo.c b/src/util/support/init-addrinfo.c index af8746762..7822ce266 100644 --- a/src/util/support/init-addrinfo.c +++ b/src/util/support/init-addrinfo.c @@ -1,3 +1,4 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * Copyright (C) 2004 by the Massachusetts Institute of Technology, * Cambridge, MA, USA. All Rights Reserved. diff --git a/src/util/support/ipc_stream.c b/src/util/support/ipc_stream.c index 778d6f1f2..2cea709ab 100644 --- a/src/util/support/ipc_stream.c +++ b/src/util/support/ipc_stream.c @@ -1,3 +1,4 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * $Header$ * @@ -53,7 +54,7 @@ static const struct k5_ipc_stream_s k5_ipc_stream_initializer = { NULL, 0, 0 }; /* ------------------------------------------------------------------------ */ static uint32_t krb5int_ipc_stream_reallocate (k5_ipc_stream io_stream, - uint64_t in_new_size) + uint64_t in_new_size) { int32_t err = 0; uint64_t new_max_size = 0; @@ -165,8 +166,8 @@ const char *krb5int_ipc_stream_data (k5_ipc_stream in_stream) /* ------------------------------------------------------------------------ */ uint32_t krb5int_ipc_stream_read (k5_ipc_stream io_stream, - void *io_data, - uint64_t in_size) + void *io_data, + uint64_t in_size) { int32_t err = 0; @@ -197,8 +198,8 @@ uint32_t krb5int_ipc_stream_read (k5_ipc_stream io_stream, /* ------------------------------------------------------------------------ */ uint32_t krb5int_ipc_stream_write (k5_ipc_stream io_stream, - const void *in_data, - uint64_t in_size) + const void *in_data, + uint64_t in_size) { int32_t err = 0; @@ -238,7 +239,7 @@ void krb5int_ipc_stream_free_string (char *in_string) /* ------------------------------------------------------------------------ */ uint32_t krb5int_ipc_stream_read_string (k5_ipc_stream io_stream, - char **out_string) + char **out_string) { int32_t err = 0; uint32_t length = 0; @@ -273,7 +274,7 @@ uint32_t krb5int_ipc_stream_read_string (k5_ipc_stream io_stream, /* ------------------------------------------------------------------------ */ uint32_t krb5int_ipc_stream_write_string (k5_ipc_stream io_stream, - const char *in_string) + const char *in_string) { int32_t err = 0; uint32_t length = 0; @@ -301,7 +302,7 @@ uint32_t krb5int_ipc_stream_write_string (k5_ipc_stream io_stream, /* ------------------------------------------------------------------------ */ uint32_t krb5int_ipc_stream_read_int32 (k5_ipc_stream io_stream, - int32_t *out_int32) + int32_t *out_int32) { int32_t err = 0; int32_t int32 = 0; @@ -323,7 +324,7 @@ uint32_t krb5int_ipc_stream_read_int32 (k5_ipc_stream io_stream, /* ------------------------------------------------------------------------ */ uint32_t krb5int_ipc_stream_write_int32 (k5_ipc_stream io_stream, - int32_t in_int32) + int32_t in_int32) { int32_t err = 0; int32_t int32 = htonl (in_int32); @@ -344,7 +345,7 @@ uint32_t krb5int_ipc_stream_write_int32 (k5_ipc_stream io_stream, /* ------------------------------------------------------------------------ */ uint32_t krb5int_ipc_stream_read_uint32 (k5_ipc_stream io_stream, - uint32_t *out_uint32) + uint32_t *out_uint32) { int32_t err = 0; uint32_t uint32 = 0; @@ -366,7 +367,7 @@ uint32_t krb5int_ipc_stream_read_uint32 (k5_ipc_stream io_stream, /* ------------------------------------------------------------------------ */ uint32_t krb5int_ipc_stream_write_uint32 (k5_ipc_stream io_stream, - uint32_t in_uint32) + uint32_t in_uint32) { int32_t err = 0; int32_t uint32 = htonl (in_uint32); @@ -387,7 +388,7 @@ uint32_t krb5int_ipc_stream_write_uint32 (k5_ipc_stream io_stream, /* ------------------------------------------------------------------------ */ uint32_t krb5int_ipc_stream_read_int64 (k5_ipc_stream io_stream, - int64_t *out_int64) + int64_t *out_int64) { int32_t err = 0; uint64_t int64 = 0; @@ -409,7 +410,7 @@ uint32_t krb5int_ipc_stream_read_int64 (k5_ipc_stream io_stream, /* ------------------------------------------------------------------------ */ uint32_t krb5int_ipc_stream_write_int64 (k5_ipc_stream io_stream, - int64_t in_int64) + int64_t in_int64) { int32_t err = 0; int64_t int64 = htonll (in_int64); @@ -431,7 +432,7 @@ uint32_t krb5int_ipc_stream_write_int64 (k5_ipc_stream io_stream, /* ------------------------------------------------------------------------ */ uint32_t krb5int_ipc_stream_read_uint64 (k5_ipc_stream io_stream, - uint64_t *out_uint64) + uint64_t *out_uint64) { int32_t err = 0; uint64_t uint64 = 0; @@ -453,7 +454,7 @@ uint32_t krb5int_ipc_stream_read_uint64 (k5_ipc_stream io_stream, /* ------------------------------------------------------------------------ */ uint32_t krb5int_ipc_stream_write_uint64 (k5_ipc_stream io_stream, - uint64_t in_uint64) + uint64_t in_uint64) { int32_t err = 0; int64_t uint64 = htonll (in_uint64); diff --git a/src/util/support/k5buf-int.h b/src/util/support/k5buf-int.h index 6f2253e47..616cee50a 100644 --- a/src/util/support/k5buf-int.h +++ b/src/util/support/k5buf-int.h @@ -1,4 +1,4 @@ -/* -*- mode: c; indent-tabs-mode: nil -*- */ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * k5buf-int.h diff --git a/src/util/support/k5buf.c b/src/util/support/k5buf.c index a994abf63..799c9f94d 100644 --- a/src/util/support/k5buf.c +++ b/src/util/support/k5buf.c @@ -1,5 +1,4 @@ -/* -*- mode: c; indent-tabs-mode: nil -*- */ - +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * k5buf.c * @@ -34,15 +33,16 @@ #include "k5buf-int.h" #include -/* Structure invariants: - - buftype is BUFTYPE_FIXED, BUFTYPE_DYNAMIC, or BUFTYPE_ERROR - if buftype is not BUFTYPE_ERROR: - space > 0 - space <= floor(SIZE_MAX / 2) (to fit within ssize_t) - len < space - data[len] = '\0' -*/ +/* + * Structure invariants: + * + * buftype is BUFTYPE_FIXED, BUFTYPE_DYNAMIC, or BUFTYPE_ERROR + * if buftype is not BUFTYPE_ERROR: + * space > 0 + * space <= floor(SIZE_MAX / 2) (to fit within ssize_t) + * len < space + * data[len] = '\0' + */ /* Make sure there is room for LEN more characters in BUF, in addition to the null terminator and what's already in there. Return true on @@ -71,7 +71,7 @@ static int ensure_space(struct k5buf *buf, size_t len) buf->space = new_space; return 1; - error_exit: +error_exit: if (buf->buftype == BUFTYPE_DYNAMIC) { free(buf->data); buf->data = NULL; diff --git a/src/util/support/mkstemp.c b/src/util/support/mkstemp.c index b68aa4dbd..9ef586a09 100644 --- a/src/util/support/mkstemp.c +++ b/src/util/support/mkstemp.c @@ -1,3 +1,4 @@ +/* -*- mode: c; c-file-style: "bsd"; indent-tabs-mode: t -*- */ /* * Copyright (c) 1987, 1993 * The Regents of the University of California. All rights reserved. diff --git a/src/util/support/plugins.c b/src/util/support/plugins.c index 8c7fb5ffe..fa7b36ebf 100644 --- a/src/util/support/plugins.c +++ b/src/util/support/plugins.c @@ -1,3 +1,4 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * util/support/plugins.c * @@ -269,7 +270,7 @@ krb5int_open_plugin (const char *filepath, struct plugin_file_handle **h, struct const char *e = dlerror(); Tprintf ("dlopen(%s): %s\n", filepath, e); err = ENOENT; /* XXX */ - krb5int_set_error (ep, err, "%s", e); + krb5int_set_error (ep, err, "%s", e); } } @@ -322,7 +323,7 @@ krb5int_open_plugin (const char *filepath, struct plugin_file_handle **h, struct static long krb5int_get_plugin_sym (struct plugin_file_handle *h, const char *csymname, int isfunc, void **ptr, - struct errinfo *ep) + struct errinfo *ep) { long err = 0; void *sym = NULL; @@ -330,13 +331,13 @@ krb5int_get_plugin_sym (struct plugin_file_handle *h, #if USE_DLOPEN if (!err && !sym && (h->dlhandle != NULL)) { /* XXX Do we need to add a leading "_" to the symbol name on any - modern platforms? */ + modern platforms? */ sym = dlsym (h->dlhandle, csymname); if (sym == NULL) { const char *e = dlerror (); /* XXX copy and save away */ Tprintf ("dlsym(%s): %s\n", csymname, e); err = ENOENT; /* XXX */ - krb5int_set_error(ep, err, "%s", e); + krb5int_set_error(ep, err, "%s", e); } } #endif @@ -362,9 +363,9 @@ krb5int_get_plugin_sym (struct plugin_file_handle *h, (LPTSTR) &lpMsgBuf, 0, NULL )) { - fprintf (stderr, "unable to get dll symbol, %s\n", (LPCTSTR)lpMsgBuf); - LocalFree(lpMsgBuf); - } + fprintf (stderr, "unable to get dll symbol, %s\n", (LPCTSTR)lpMsgBuf); + LocalFree(lpMsgBuf); + } } } #endif @@ -382,14 +383,14 @@ krb5int_get_plugin_sym (struct plugin_file_handle *h, long KRB5_CALLCONV krb5int_get_plugin_data (struct plugin_file_handle *h, const char *csymname, - void **ptr, struct errinfo *ep) + void **ptr, struct errinfo *ep) { return krb5int_get_plugin_sym (h, csymname, 0, ptr, ep); } long KRB5_CALLCONV krb5int_get_plugin_func (struct plugin_file_handle *h, const char *csymname, - void (**ptr)(), struct errinfo *ep) + void (**ptr)(), struct errinfo *ep) { void *dptr = NULL; long err = krb5int_get_plugin_sym (h, csymname, 1, &dptr, ep); @@ -434,10 +435,10 @@ krb5int_close_plugin (struct plugin_file_handle *h) #ifdef HAVE_STRERROR_R -#define ERRSTR(ERR, BUF) \ +#define ERRSTR(ERR, BUF) \ (strerror_r (ERR, BUF, sizeof(BUF)) == 0 ? BUF : strerror (ERR)) #else -#define ERRSTR(ERR, BUF) \ +#define ERRSTR(ERR, BUF) \ (strerror (ERR)) #endif @@ -466,7 +467,7 @@ krb5int_plugin_file_handle_array_add (struct plugin_file_handle ***harray, size_ } else { newharray[newcount - 1] = p; newharray[newcount] = NULL; - *count = newcount; + *count = newcount; *harray = newharray; } @@ -531,11 +532,11 @@ krb5int_get_plugin_filenames (const char * const *filebases, char ***filenames) size_t j; for (i = 0; !err && filebases[i]; i++) { for (j = 0; !err && fileexts[j]; j++) { - if (asprintf(&tempnames[(i*exts_count)+j], "%s%s", + if (asprintf(&tempnames[(i*exts_count)+j], "%s%s", filebases[i], fileexts[j]) < 0) { - tempnames[(i*exts_count)+j] = NULL; - err = ENOMEM; - } + tempnames[(i*exts_count)+j] = NULL; + err = ENOMEM; + } } } tempnames[bases_count * exts_count] = NULL; /* NUL-terminate */ @@ -559,7 +560,7 @@ krb5int_get_plugin_filenames (const char * const *filebases, char ***filenames) long KRB5_CALLCONV krb5int_open_plugin_dirs (const char * const *dirnames, const char * const *filebases, - struct plugin_dir_handle *dirhandle, + struct plugin_dir_handle *dirhandle, struct errinfo *ep) { long err = 0; @@ -573,7 +574,7 @@ krb5int_open_plugin_dirs (const char * const *dirnames, } if (!err && (filebases != NULL)) { - err = krb5int_get_plugin_filenames (filebases, &filenames); + err = krb5int_get_plugin_filenames (filebases, &filenames); } for (i = 0; !err && dirnames[i] != NULL; i++) { @@ -583,26 +584,26 @@ krb5int_open_plugin_dirs (const char * const *dirnames, for (j = 0; !err && filenames[j] != NULL; j++) { struct plugin_file_handle *handle = NULL; - char *filepath = NULL; + char *filepath = NULL; - if (!err) { - if (asprintf(&filepath, "%s/%s", dirnames[i], filenames[j]) < 0) { - filepath = NULL; - err = ENOMEM; - } - } + if (!err) { + if (asprintf(&filepath, "%s/%s", dirnames[i], filenames[j]) < 0) { + filepath = NULL; + err = ENOMEM; + } + } if (krb5int_open_plugin (filepath, &handle, ep) == 0) { err = krb5int_plugin_file_handle_array_add (&h, &count, handle); if (!err) { handle = NULL; } /* h takes ownership */ } - if (filepath != NULL) { free (filepath); } - if (handle != NULL) { krb5int_close_plugin (handle); } + if (filepath != NULL) { free (filepath); } + if (handle != NULL) { krb5int_close_plugin (handle); } } } else { /* load all plugins in each directory */ - DIR *dir = opendir (dirnames[i]); + DIR *dir = opendir (dirnames[i]); while (dir != NULL && !err) { struct dirent *d = NULL; @@ -617,13 +618,13 @@ krb5int_open_plugin_dirs (const char * const *dirnames, continue; } - if (!err) { + if (!err) { int len = NAMELEN (d); - if (asprintf(&filepath, "%s/%*s", dirnames[i], len, d->d_name) < 0) { - filepath = NULL; - err = ENOMEM; - } - } + if (asprintf(&filepath, "%s/%*s", dirnames[i], len, d->d_name) < 0) { + filepath = NULL; + err = ENOMEM; + } + } if (!err) { if (krb5int_open_plugin (filepath, &handle, ep) == 0) { @@ -677,9 +678,9 @@ krb5int_free_plugin_dir_data (void **ptrs) long KRB5_CALLCONV krb5int_get_plugin_dir_data (struct plugin_dir_handle *dirhandle, - const char *symname, - void ***ptrs, - struct errinfo *ep) + const char *symname, + void ***ptrs, + struct errinfo *ep) { long err = 0; void **p = NULL; @@ -736,16 +737,16 @@ krb5int_free_plugin_dir_func (void (**ptrs)(void)) long KRB5_CALLCONV krb5int_get_plugin_dir_func (struct plugin_dir_handle *dirhandle, - const char *symname, - void (***ptrs)(void), - struct errinfo *ep) + const char *symname, + void (***ptrs)(void), + struct errinfo *ep) { long err = 0; void (**p)() = NULL; size_t count = 0; /* XXX Do we need to add a leading "_" to the symbol name on any - modern platforms? */ + modern platforms? */ Tprintf("get_plugin_data_sym(%s)\n", symname); diff --git a/src/util/support/printf.c b/src/util/support/printf.c index 88552d297..b3cf46b38 100644 --- a/src/util/support/printf.c +++ b/src/util/support/printf.c @@ -1,3 +1,4 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * printf.c * @@ -16,7 +17,7 @@ * this permission notice appear in supporting documentation, and that * the name of M.I.T. not be used in advertising or publicity pertaining * to distribution of the software without specific, written prior - * permission. Furthermore if you modify this software you must label + * permission. Furthermore if you modify this software you must label * your software as modified software and not distribute it in such a * fashion that it might be confused with the original M.I.T. software. * M.I.T. makes no representations about the suitability of @@ -42,43 +43,43 @@ krb5int_vasprintf(char **ret, const char *format, va_list ap) int len2; while (1) { - if (len >= INT_MAX || len == 0) - goto fail; - nstr = realloc(str, len); - if (nstr == NULL) - goto fail; - str = nstr; - va_copy(ap2, ap); - len2 = vsnprintf(str, len, format, ap2); - va_end(ap2); - /* ISO C vsnprintf returns the needed length. Some old - vsnprintf implementations return -1 on truncation. */ - if (len2 < 0) { - /* Don't know how much space we need, just that we didn't - supply enough; get a bigger buffer and try again. */ - if (len <= SIZE_MAX/2) - len *= 2; - else if (len < SIZE_MAX) - len = SIZE_MAX; - else - goto fail; - } else if ((unsigned int) len2 >= SIZE_MAX) { - /* Need more space than we can request. */ - goto fail; - } else if ((size_t) len2 >= len) { - /* Need more space, but we know how much. */ - len = (size_t) len2 + 1; - } else { - /* Success! */ - break; - } + if (len >= INT_MAX || len == 0) + goto fail; + nstr = realloc(str, len); + if (nstr == NULL) + goto fail; + str = nstr; + va_copy(ap2, ap); + len2 = vsnprintf(str, len, format, ap2); + va_end(ap2); + /* ISO C vsnprintf returns the needed length. Some old + vsnprintf implementations return -1 on truncation. */ + if (len2 < 0) { + /* Don't know how much space we need, just that we didn't + supply enough; get a bigger buffer and try again. */ + if (len <= SIZE_MAX/2) + len *= 2; + else if (len < SIZE_MAX) + len = SIZE_MAX; + else + goto fail; + } else if ((unsigned int) len2 >= SIZE_MAX) { + /* Need more space than we can request. */ + goto fail; + } else if ((size_t) len2 >= len) { + /* Need more space, but we know how much. */ + len = (size_t) len2 + 1; + } else { + /* Success! */ + break; + } } /* We might've allocated more than we need, if we're still using the initial guess, or we got here by doubling. */ if ((size_t) len2 < len - 1) { - nstr = realloc(str, (size_t) len2 + 1); - if (nstr) - str = nstr; + nstr = realloc(str, (size_t) len2 + 1); + if (nstr) + str = nstr; } *ret = str; return len2; diff --git a/src/util/support/strlcpy.c b/src/util/support/strlcpy.c index fd2c79b76..a3abbfec9 100644 --- a/src/util/support/strlcpy.c +++ b/src/util/support/strlcpy.c @@ -1,3 +1,4 @@ +/* -*- mode: c; c-file-style: "bsd"; indent-tabs-mode: t -*- */ #include "k5-platform.h" /* Provide strlcpy and strlcat for platforms that don't have them. */ diff --git a/src/util/support/supp-int.h b/src/util/support/supp-int.h index 85641005f..cf8c2888f 100644 --- a/src/util/support/supp-int.h +++ b/src/util/support/supp-int.h @@ -1,3 +1,4 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * util/support/supp-int.h * diff --git a/src/util/support/t_k5buf.c b/src/util/support/t_k5buf.c index b0ae2807b..a74ad858c 100644 --- a/src/util/support/t_k5buf.c +++ b/src/util/support/t_k5buf.c @@ -1,4 +1,4 @@ -/* -*- mode: c; indent-tabs-mode: nil -*- */ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * t_k5buf.c diff --git a/src/util/support/t_unal.c b/src/util/support/t_unal.c index d9a3cc4b0..6b2720b42 100644 --- a/src/util/support/t_unal.c +++ b/src/util/support/t_unal.c @@ -1,11 +1,12 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ #undef NDEBUG #include #include "k5-platform.h" int main () { - /* Test some low-level assumptions the Kerberos code depends - on. */ + /* Test some low-level assumptions the Kerberos code depends + on. */ union { UINT64_TYPE n64; diff --git a/src/util/support/threads.c b/src/util/support/threads.c index 12b549286..dddf6aae7 100644 --- a/src/util/support/threads.c +++ b/src/util/support/threads.c @@ -1,3 +1,4 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * util/support/threads.c * @@ -55,7 +56,7 @@ int krb5int_pthread_loaded (void) static DWORD tls_idx; static CRITICAL_SECTION key_lock; struct tsd_block { - void *values[K5_KEY_MAX]; + void *values[K5_KEY_MAX]; }; static void (*destructors[K5_KEY_MAX])(void *); static unsigned char destructors_set[K5_KEY_MAX]; @@ -69,17 +70,17 @@ void krb5int_thread_detach_hook (void) err = CALL_INIT_FUNCTION(krb5int_thread_support_init); if (err) - return; + return; t = TlsGetValue(tls_idx); if (t == NULL) - return; + return; for (i = 0; i < K5_KEY_MAX; i++) { - if (destructors_set[i] && destructors[i] && t->values[i]) { - void *v = t->values[i]; - t->values[i] = 0; - (*destructors[i])(v); - } + if (destructors_set[i] && destructors[i] && t->values[i]) { + void *v = t->values[i]; + t->values[i] = 0; + (*destructors[i])(v); + } } } @@ -124,44 +125,44 @@ static volatile int flag_pthread_loaded = -1; static void loaded_test_aux(void) { if (flag_pthread_loaded == -1) - flag_pthread_loaded = 1; + flag_pthread_loaded = 1; else - /* Could we have been called twice? */ - flag_pthread_loaded = 0; + /* Could we have been called twice? */ + flag_pthread_loaded = 0; } static pthread_once_t loaded_test_once = PTHREAD_ONCE_INIT; int krb5int_pthread_loaded (void) { int x = flag_pthread_loaded; if (x != -1) - return x; + return x; if (&pthread_getspecific == 0 - || &pthread_setspecific == 0 - || &pthread_key_create == 0 - || &pthread_key_delete == 0 - || &pthread_once == 0 - || &pthread_mutex_lock == 0 - || &pthread_mutex_unlock == 0 - || &pthread_mutex_destroy == 0 - || &pthread_mutex_init == 0 - || &pthread_self == 0 - || &pthread_equal == 0 - /* Any program that's really multithreaded will have to be - able to create threads. */ - || &pthread_create == 0 - || &pthread_join == 0 - /* Okay, all the interesting functions -- or stubs for them -- - seem to be present. If we call pthread_once, does it - actually seem to cause the indicated function to get called - exactly one time? */ - || pthread_once(&loaded_test_once, loaded_test_aux) != 0 - || pthread_once(&loaded_test_once, loaded_test_aux) != 0 - /* This catches cases where pthread_once does nothing, and - never causes the function to get called. That's a pretty - clear violation of the POSIX spec, but hey, it happens. */ - || flag_pthread_loaded < 0) { - flag_pthread_loaded = 0; - return 0; + || &pthread_setspecific == 0 + || &pthread_key_create == 0 + || &pthread_key_delete == 0 + || &pthread_once == 0 + || &pthread_mutex_lock == 0 + || &pthread_mutex_unlock == 0 + || &pthread_mutex_destroy == 0 + || &pthread_mutex_init == 0 + || &pthread_self == 0 + || &pthread_equal == 0 + /* Any program that's really multithreaded will have to be + able to create threads. */ + || &pthread_create == 0 + || &pthread_join == 0 + /* Okay, all the interesting functions -- or stubs for them -- + seem to be present. If we call pthread_once, does it + actually seem to cause the indicated function to get called + exactly one time? */ + || pthread_once(&loaded_test_once, loaded_test_aux) != 0 + || pthread_once(&loaded_test_once, loaded_test_aux) != 0 + /* This catches cases where pthread_once does nothing, and + never causes the function to get called. That's a pretty + clear violation of the POSIX spec, but hey, it happens. */ + || flag_pthread_loaded < 0) { + flag_pthread_loaded = 0; + return 0; } /* If we wanted to be super-paranoid, we could try testing whether pthread_get/setspecific work, too. I don't know -- so far -- @@ -169,13 +170,13 @@ int krb5int_pthread_loaded (void) return flag_pthread_loaded; } static struct tsd_block tsd_if_single; -# define GET_NO_PTHREAD_TSD() (&tsd_if_single) +# define GET_NO_PTHREAD_TSD() (&tsd_if_single) #else int krb5int_pthread_loaded (void) { return 1; } -# define GET_NO_PTHREAD_TSD() (abort(),(struct tsd_block *)0) +# define GET_NO_PTHREAD_TSD() (abort(),(struct tsd_block *)0) #endif static pthread_key_t key; @@ -189,12 +190,12 @@ static void thread_termination (void *tptr) struct tsd_block *t = tptr; /* Make multiple passes in case, for example, a libkrb5 cleanup - function wants to print out an error message, which causes - com_err to allocate a thread-specific buffer, after we just - freed up the old one. + function wants to print out an error message, which causes + com_err to allocate a thread-specific buffer, after we just + freed up the old one. - Shouldn't actually happen, if we're careful, but check just in - case. */ + Shouldn't actually happen, if we're careful, but check just in + case. */ pass = 0; none_found = 0; @@ -211,7 +212,7 @@ static void thread_termination (void *tptr) } free (t); err = k5_mutex_unlock(&key_lock); - } + } /* remove thread from global linked list */ } @@ -225,7 +226,7 @@ void *k5_getspecific (k5_key_t keynum) err = CALL_INIT_FUNCTION(krb5int_thread_support_init); if (err) - return NULL; + return NULL; assert(keynum >= 0 && keynum < K5_KEY_MAX); assert(destructors_set[keynum] == 1); @@ -241,14 +242,14 @@ void *k5_getspecific (k5_key_t keynum) #else /* POSIX */ if (K5_PTHREADS_LOADED) - t = pthread_getspecific(key); + t = pthread_getspecific(key); else - t = GET_NO_PTHREAD_TSD(); + t = GET_NO_PTHREAD_TSD(); #endif if (t == NULL) - return NULL; + return NULL; return t->values[keynum]; } @@ -259,7 +260,7 @@ int k5_setspecific (k5_key_t keynum, void *value) err = CALL_INIT_FUNCTION(krb5int_thread_support_init); if (err) - return err; + return err; assert(keynum >= 0 && keynum < K5_KEY_MAX); assert(destructors_set[keynum] == 1); @@ -272,42 +273,42 @@ int k5_setspecific (k5_key_t keynum, void *value) t = TlsGetValue(tls_idx); if (t == NULL) { - int i; - t = malloc(sizeof(*t)); - if (t == NULL) - return ENOMEM; - for (i = 0; i < K5_KEY_MAX; i++) - t->values[i] = 0; - /* add to global linked list */ - /* t->next = 0; */ - err = TlsSetValue(tls_idx, t); - if (!err) { - free(t); - return GetLastError(); - } + int i; + t = malloc(sizeof(*t)); + if (t == NULL) + return ENOMEM; + for (i = 0; i < K5_KEY_MAX; i++) + t->values[i] = 0; + /* add to global linked list */ + /* t->next = 0; */ + err = TlsSetValue(tls_idx, t); + if (!err) { + free(t); + return GetLastError(); + } } #else /* POSIX */ if (K5_PTHREADS_LOADED) { - t = pthread_getspecific(key); - if (t == NULL) { - int i; - t = malloc(sizeof(*t)); - if (t == NULL) - return ENOMEM; - for (i = 0; i < K5_KEY_MAX; i++) - t->values[i] = 0; - /* add to global linked list */ - t->next = 0; - err = pthread_setspecific(key, t); - if (err) { - free(t); - return err; - } - } + t = pthread_getspecific(key); + if (t == NULL) { + int i; + t = malloc(sizeof(*t)); + if (t == NULL) + return ENOMEM; + for (i = 0; i < K5_KEY_MAX; i++) + t->values[i] = 0; + /* add to global linked list */ + t->next = 0; + err = pthread_setspecific(key, t); + if (err) { + free(t); + return err; + } + } } else { - t = GET_NO_PTHREAD_TSD(); + t = GET_NO_PTHREAD_TSD(); } #endif @@ -322,7 +323,7 @@ int k5_key_register (k5_key_t keynum, void (*destructor)(void *)) err = CALL_INIT_FUNCTION(krb5int_thread_support_init); if (err) - return err; + return err; assert(keynum >= 0 && keynum < K5_KEY_MAX); @@ -347,10 +348,10 @@ int k5_key_register (k5_key_t keynum, void (*destructor)(void *)) err = k5_mutex_lock(&key_lock); if (err == 0) { - assert(destructors_set[keynum] == 0); - destructors_set[keynum] = 1; - destructors[keynum] = destructor; - err = k5_mutex_unlock(&key_lock); + assert(destructors_set[keynum] == 0); + destructors_set[keynum] = 1; + destructors[keynum] = destructor; + err = k5_mutex_unlock(&key_lock); } #endif @@ -365,7 +366,7 @@ int k5_key_delete (k5_key_t keynum) assert(destructors_set[keynum] == 1); if (destructors[keynum] && tsd_no_threads.values[keynum]) - (*destructors[keynum])(tsd_no_threads.values[keynum]); + (*destructors[keynum])(tsd_no_threads.values[keynum]); destructors[keynum] = 0; tsd_no_threads.values[keynum] = 0; destructors_set[keynum] = 0; @@ -385,19 +386,19 @@ int k5_key_delete (k5_key_t keynum) #else /* POSIX */ { - int err; + int err; - /* XXX RESOURCE LEAK: + /* XXX RESOURCE LEAK: - Need to destroy the allocated objects first! */ + Need to destroy the allocated objects first! */ - err = k5_mutex_lock(&key_lock); - if (err == 0) { - assert(destructors_set[keynum] == 1); - destructors_set[keynum] = 0; - destructors[keynum] = NULL; - k5_mutex_unlock(&key_lock); - } + err = k5_mutex_lock(&key_lock); + if (err == 0) { + assert(destructors_set[keynum] == 1); + destructors_set[keynum] = 0; + destructors[keynum] = NULL; + k5_mutex_unlock(&key_lock); + } } #endif @@ -434,22 +435,22 @@ int krb5int_thread_support_init (void) err = k5_mutex_finish_init(&key_lock); if (err) - return err; + return err; if (K5_PTHREADS_LOADED) { - err = pthread_key_create(&key, thread_termination); - if (err) - return err; + err = pthread_key_create(&key, thread_termination); + if (err) + return err; } #endif err = krb5int_init_fac(); if (err) - return err; + return err; err = krb5int_err_init(); if (err) - return err; + return err; return 0; } @@ -457,7 +458,7 @@ int krb5int_thread_support_init (void) void krb5int_thread_support_fini (void) { if (! INITIALIZER_RAN (krb5int_thread_support_init)) - return; + return; #ifdef SHOW_INITFINI_FUNCS printf("krb5int_thread_support_fini\n"); @@ -476,9 +477,9 @@ void krb5int_thread_support_fini (void) #else /* POSIX */ if (! INITIALIZER_RAN(krb5int_thread_support_init)) - return; + return; if (K5_PTHREADS_LOADED) - pthread_key_delete(key); + pthread_key_delete(key); /* ... delete stuff ... */ k5_mutex_destroy(&key_lock); @@ -497,11 +498,11 @@ krb5int_mutex_alloc (k5_mutex_t **m) ptr = malloc (sizeof (k5_mutex_t)); if (ptr == NULL) - return ENOMEM; + return ENOMEM; err = k5_mutex_init (ptr); if (err) { - free (ptr); - return err; + free (ptr); + return err; } *m = ptr; return 0; diff --git a/src/util/support/utf8.c b/src/util/support/utf8.c index 3d9021317..dcaa4cba4 100644 --- a/src/util/support/utf8.c +++ b/src/util/support/utf8.c @@ -1,3 +1,4 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * util/support/utf8.c * @@ -60,7 +61,7 @@ size_t krb5int_utf8_bytes(const char *p) size_t bytes; for (bytes = 0; p[bytes]; bytes++) - ; + ; return bytes; } @@ -71,7 +72,7 @@ size_t krb5int_utf8_chars(const char *p) size_t chars = 0; for ( ; *p ; KRB5_UTF8_INCR(p)) - chars++; + chars++; return chars; } @@ -83,7 +84,7 @@ size_t krb5int_utf8c_chars(const char *p, size_t length) const char *end = p + length; for ( ; p < end; KRB5_UTF8_INCR(p)) - chars++; + chars++; return chars; } @@ -110,7 +111,7 @@ const char krb5int_utf8_lentab[] = { int krb5int_utf8_charlen(const char *p) { if (!(*p & 0x80)) - return 1; + return 1; return krb5int_utf8_lentab[*(const unsigned char *)p ^ 0x80]; } @@ -152,8 +153,8 @@ int krb5int_utf8_charlen2(const char *p) int i = KRB5_UTF8_CHARLEN(p); if (i > 2) { - if (!(krb5int_utf8_mintab[*p & 0x1f] & p[1])) - i = 0; + if (!(krb5int_utf8_mintab[*p & 0x1f] & p[1])) + i = 0; } return i; @@ -169,22 +170,22 @@ int krb5int_utf8_to_ucs4(const char *p, krb5_ucs4 *out) krb5_ucs4 ch; int len, i; static unsigned char mask[] = { - 0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 }; + 0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 }; *out = 0; len = KRB5_UTF8_CHARLEN2(p, len); if (len == 0) - return -1; + return -1; ch = c[0] & mask[len]; for (i = 1; i < len; i++) { - if ((c[i] & 0xc0) != 0x80) - return -1; + if ((c[i] & 0xc0) != 0x80) + return -1; - ch <<= 6; - ch |= c[i] & 0x3f; + ch <<= 6; + ch |= c[i] & 0x3f; } *out = ch; @@ -197,7 +198,7 @@ int krb5int_utf8_to_ucs2(const char *p, krb5_ucs2 *out) *out = 0; if (krb5int_utf8_to_ucs4(p, &ch) == -1 || ch > 0xFFFF) - return -1; + return -1; *out = (krb5_ucs2) ch; return 0; } @@ -210,45 +211,45 @@ size_t krb5int_ucs4_to_utf8(krb5_ucs4 c, char *buf) /* not a valid Unicode character */ if (c < 0) - return 0; + return 0; /* Just return length, don't convert */ if (buf == NULL) { - if (c < 0x80) return 1; - else if (c < 0x800) return 2; - else if (c < 0x10000) return 3; - else if (c < 0x200000) return 4; - else if (c < 0x4000000) return 5; - else return 6; + if (c < 0x80) return 1; + else if (c < 0x800) return 2; + else if (c < 0x10000) return 3; + else if (c < 0x200000) return 4; + else if (c < 0x4000000) return 5; + else return 6; } if (c < 0x80) { - p[len++] = c; + p[len++] = c; } else if (c < 0x800) { - p[len++] = 0xc0 | ( c >> 6 ); - p[len++] = 0x80 | ( c & 0x3f ); + p[len++] = 0xc0 | ( c >> 6 ); + p[len++] = 0x80 | ( c & 0x3f ); } else if (c < 0x10000) { - p[len++] = 0xe0 | ( c >> 12 ); - p[len++] = 0x80 | ( (c >> 6) & 0x3f ); - p[len++] = 0x80 | ( c & 0x3f ); + p[len++] = 0xe0 | ( c >> 12 ); + p[len++] = 0x80 | ( (c >> 6) & 0x3f ); + p[len++] = 0x80 | ( c & 0x3f ); } else if (c < 0x200000) { - p[len++] = 0xf0 | ( c >> 18 ); - p[len++] = 0x80 | ( (c >> 12) & 0x3f ); - p[len++] = 0x80 | ( (c >> 6) & 0x3f ); - p[len++] = 0x80 | ( c & 0x3f ); + p[len++] = 0xf0 | ( c >> 18 ); + p[len++] = 0x80 | ( (c >> 12) & 0x3f ); + p[len++] = 0x80 | ( (c >> 6) & 0x3f ); + p[len++] = 0x80 | ( c & 0x3f ); } else if (c < 0x4000000) { - p[len++] = 0xf8 | ( c >> 24 ); - p[len++] = 0x80 | ( (c >> 18) & 0x3f ); - p[len++] = 0x80 | ( (c >> 12) & 0x3f ); - p[len++] = 0x80 | ( (c >> 6) & 0x3f ); - p[len++] = 0x80 | ( c & 0x3f ); + p[len++] = 0xf8 | ( c >> 24 ); + p[len++] = 0x80 | ( (c >> 18) & 0x3f ); + p[len++] = 0x80 | ( (c >> 12) & 0x3f ); + p[len++] = 0x80 | ( (c >> 6) & 0x3f ); + p[len++] = 0x80 | ( c & 0x3f ); } else /* if( c < 0x80000000 ) */ { - p[len++] = 0xfc | ( c >> 30 ); - p[len++] = 0x80 | ( (c >> 24) & 0x3f ); - p[len++] = 0x80 | ( (c >> 18) & 0x3f ); - p[len++] = 0x80 | ( (c >> 12) & 0x3f ); - p[len++] = 0x80 | ( (c >> 6) & 0x3f ); - p[len++] = 0x80 | ( c & 0x3f ); + p[len++] = 0xfc | ( c >> 30 ); + p[len++] = 0x80 | ( (c >> 24) & 0x3f ); + p[len++] = 0x80 | ( (c >> 18) & 0x3f ); + p[len++] = 0x80 | ( (c >> 12) & 0x3f ); + p[len++] = 0x80 | ( (c >> 6) & 0x3f ); + p[len++] = 0x80 | ( c & 0x3f ); } return len; @@ -259,9 +260,9 @@ size_t krb5int_ucs2_to_utf8(krb5_ucs2 c, char *buf) return krb5int_ucs4_to_utf8((krb5_ucs4)c, buf); } -#define KRB5_UCS_UTF8LEN(c) \ - c < 0 ? 0 : (c < 0x80 ? 1 : (c < 0x800 ? 2 : (c < 0x10000 ? 3 : \ - (c < 0x200000 ? 4 : (c < 0x4000000 ? 5 : 6))))) +#define KRB5_UCS_UTF8LEN(c) \ + c < 0 ? 0 : (c < 0x80 ? 1 : (c < 0x800 ? 2 : (c < 0x10000 ? 3 : \ + (c < 0x200000 ? 4 : (c < 0x4000000 ? 5 : 6))))) /* * Advance to the next UTF-8 character @@ -278,13 +279,13 @@ char *krb5int_utf8_next(const char *p) const unsigned char *u = (const unsigned char *) p; if (KRB5_UTF8_ISASCII(u)) { - return (char *) &p[1]; + return (char *) &p[1]; } for (i = 1; i < 6; i++) { - if ((u[i] & 0xc0) != 0x80) { - return (char *) &p[i]; - } + if ((u[i] & 0xc0) != 0x80) { + return (char *) &p[i]; + } } return (char *) &p[i]; @@ -305,9 +306,9 @@ char *krb5int_utf8_prev(const char *p) const unsigned char *u = (const unsigned char *) p; for (i = -1; i>-6 ; i--) { - if ((u[i] & 0xc0 ) != 0x80) { - return (char *) &p[i]; - } + if ((u[i] & 0xc0 ) != 0x80) { + return (char *) &p[i]; + } } return (char *) &p[i]; @@ -331,14 +332,14 @@ int krb5int_utf8_copy(char* dst, const char *src) dst[0] = src[0]; if (KRB5_UTF8_ISASCII(u)) { - return 1; + return 1; } for (i=1; i<6; i++) { - if ((u[i] & 0xc0) != 0x80) { - return i; - } - dst[i] = src[i]; + if ((u[i] & 0xc0) != 0x80) { + return i; + } + dst[i] = src[i]; } return i; @@ -362,7 +363,7 @@ int krb5int_utf8_isdigit(const char * p) unsigned c = * (const unsigned char *) p; if (!KRB5_ASCII(c)) - return 0; + return 0; return KRB5_DIGIT( c ); } @@ -372,7 +373,7 @@ int krb5int_utf8_isxdigit(const char * p) unsigned c = * (const unsigned char *) p; if (!KRB5_ASCII(c)) - return 0; + return 0; return KRB5_HEX(c); } @@ -382,7 +383,7 @@ int krb5int_utf8_isspace(const char * p) unsigned c = * (const unsigned char *) p; if (!KRB5_ASCII(c)) - return 0; + return 0; switch(c) { case ' ': @@ -391,7 +392,7 @@ int krb5int_utf8_isspace(const char * p) case '\r': case '\v': case '\f': - return 1; + return 1; } return 0; @@ -406,7 +407,7 @@ int krb5int_utf8_isalpha(const char * p) unsigned c = * (const unsigned char *) p; if (!KRB5_ASCII(c)) - return 0; + return 0; return KRB5_ALPHA(c); } @@ -416,7 +417,7 @@ int krb5int_utf8_isalnum(const char * p) unsigned c = * (const unsigned char *) p; if (!KRB5_ASCII(c)) - return 0; + return 0; return KRB5_ALNUM(c); } @@ -427,7 +428,7 @@ int krb5int_utf8_islower(const char * p) unsigned c = * (const unsigned char *) p; if (!KRB5_ASCII(c)) - return 0; + return 0; return KRB5_LOWER(c); } @@ -437,7 +438,7 @@ int krb5int_utf8_isupper(const char * p) unsigned c = * (const unsigned char *) p; if (!KRB5_ASCII(c)) - return 0; + return 0; return KRB5_UPPER(c); } @@ -455,10 +456,10 @@ char *krb5int_utf8_strchr(const char *str, const char *chr) krb5_ucs4 chs, ch; if (krb5int_utf8_to_ucs4(chr, &ch) == -1) - return NULL; + return NULL; for ( ; *str != '\0'; KRB5_UTF8_INCR(str)) { - if (krb5int_utf8_to_ucs4(str, &chs) == 0 && chs == ch) - return (char *)str; + if (krb5int_utf8_to_ucs4(str, &chs) == 0 && chs == ch) + return (char *)str; } return NULL; @@ -471,11 +472,11 @@ size_t krb5int_utf8_strcspn(const char *str, const char *set) krb5_ucs4 chstr, chset; for (cstr = str; *cstr != '\0'; KRB5_UTF8_INCR(cstr)) { - for (cset = set; *cset != '\0'; KRB5_UTF8_INCR(cset)) { - if (krb5int_utf8_to_ucs4(cstr, &chstr) == 0 - && krb5int_utf8_to_ucs4(cset, &chset) == 0 && chstr == chset) - return cstr - str; - } + for (cset = set; *cset != '\0'; KRB5_UTF8_INCR(cset)) { + if (krb5int_utf8_to_ucs4(cstr, &chstr) == 0 + && krb5int_utf8_to_ucs4(cset, &chset) == 0 && chstr == chset) + return cstr - str; + } } return cstr - str; @@ -488,13 +489,13 @@ size_t krb5int_utf8_strspn(const char *str, const char *set) krb5_ucs4 chstr, chset; for (cstr = str; *cstr != '\0'; KRB5_UTF8_INCR(cstr)) { - for (cset = set; ; KRB5_UTF8_INCR(cset)) { - if (*cset == '\0') - return cstr - str; - if (krb5int_utf8_to_ucs4(cstr, &chstr) == 0 - && krb5int_utf8_to_ucs4(cset, &chset) == 0 && chstr == chset) - break; - } + for (cset = set; ; KRB5_UTF8_INCR(cset)) { + if (*cset == '\0') + return cstr - str; + if (krb5int_utf8_to_ucs4(cstr, &chstr) == 0 + && krb5int_utf8_to_ucs4(cset, &chset) == 0 && chstr == chset) + break; + } } return cstr - str; @@ -507,11 +508,11 @@ char *krb5int_utf8_strpbrk(const char *str, const char *set) krb5_ucs4 chstr, chset; for ( ; *str != '\0'; KRB5_UTF8_INCR(str)) { - for (cset = set; *cset != '\0'; KRB5_UTF8_INCR(cset)) { - if (krb5int_utf8_to_ucs4(str, &chstr) == 0 - && krb5int_utf8_to_ucs4(cset, &chset) == 0 && chstr == chset) - return (char *)str; - } + for (cset = set; *cset != '\0'; KRB5_UTF8_INCR(cset)) { + if (krb5int_utf8_to_ucs4(str, &chstr) == 0 + && krb5int_utf8_to_ucs4(cset, &chset) == 0 && chstr == chset) + return (char *)str; + } } return NULL; @@ -524,23 +525,23 @@ char *krb5int_utf8_strtok(char *str, const char *sep, char **last) char *end; if (last == NULL) - return NULL; + return NULL; begin = str ? str : *last; begin += krb5int_utf8_strspn(begin, sep); if (*begin == '\0') { - *last = NULL; - return NULL; + *last = NULL; + return NULL; } end = &begin[krb5int_utf8_strcspn(begin, sep)]; if (*end != '\0') { - char *next = KRB5_UTF8_NEXT(end); - *end = '\0'; - end = next; + char *next = KRB5_UTF8_NEXT(end); + *end = '\0'; + end = next; } *last = end; diff --git a/src/util/support/utf8_conv.c b/src/util/support/utf8_conv.c index 03303d744..47ff9511b 100644 --- a/src/util/support/utf8_conv.c +++ b/src/util/support/utf8_conv.c @@ -1,3 +1,4 @@ +/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ /* * util/support/utf8_conv.c * @@ -67,9 +68,9 @@ static unsigned char mask[] = { 0, 0x7f, 0x1f, 0x0f, 0x07, 0x03, 0x01 }; static ssize_t k5_utf8s_to_ucs2s(krb5_ucs2 *ucs2str, - const char *utf8str, - size_t count, - int little_endian) + const char *utf8str, + size_t count, + int little_endian) { size_t ucs2len = 0; size_t utflen, i; @@ -77,52 +78,52 @@ k5_utf8s_to_ucs2s(krb5_ucs2 *ucs2str, /* If input ptr is NULL or empty... */ if (utf8str == NULL || *utf8str == '\0') { - *ucs2str = 0; + *ucs2str = 0; - return 0; + return 0; } /* Examine next UTF-8 character. */ while (*utf8str && ucs2len < count) { - /* Get UTF-8 sequence length from 1st byte */ - utflen = KRB5_UTF8_CHARLEN2(utf8str, utflen); + /* Get UTF-8 sequence length from 1st byte */ + utflen = KRB5_UTF8_CHARLEN2(utf8str, utflen); - if (utflen == 0 || utflen > KRB5_MAX_UTF8_LEN) - return -1; + if (utflen == 0 || utflen > KRB5_MAX_UTF8_LEN) + return -1; - /* First byte minus length tag */ - ch = (krb5_ucs2)(utf8str[0] & mask[utflen]); + /* First byte minus length tag */ + ch = (krb5_ucs2)(utf8str[0] & mask[utflen]); - for (i = 1; i < utflen; i++) { - /* Subsequent bytes must start with 10 */ - if ((utf8str[i] & 0xc0) != 0x80) - return -1; + for (i = 1; i < utflen; i++) { + /* Subsequent bytes must start with 10 */ + if ((utf8str[i] & 0xc0) != 0x80) + return -1; - ch <<= 6; /* 6 bits of data in each subsequent byte */ - ch |= (krb5_ucs2)(utf8str[i] & 0x3f); - } + ch <<= 6; /* 6 bits of data in each subsequent byte */ + ch |= (krb5_ucs2)(utf8str[i] & 0x3f); + } - if (ucs2str != NULL) { + if (ucs2str != NULL) { #ifdef K5_BE #ifndef SWAP16 -#define SWAP16(X) ((((X) << 8) | ((X) >> 8)) & 0xFFFF) +#define SWAP16(X) ((((X) << 8) | ((X) >> 8)) & 0xFFFF) #endif - if (little_endian) - ucs2str[ucs2len] = SWAP16(ch); - else + if (little_endian) + ucs2str[ucs2len] = SWAP16(ch); + else #endif - ucs2str[ucs2len] = ch; - } + ucs2str[ucs2len] = ch; + } - utf8str += utflen; /* Move to next UTF-8 character */ - ucs2len++; /* Count number of wide chars stored/required */ + utf8str += utflen; /* Move to next UTF-8 character */ + ucs2len++; /* Count number of wide chars stored/required */ } assert(ucs2len < count); if (ucs2str != NULL) { - /* Add null terminator if there's room in the buffer. */ - ucs2str[ucs2len] = 0; + /* Add null terminator if there's room in the buffer. */ + ucs2str[ucs2len] = 0; } return ucs2len; @@ -130,8 +131,8 @@ k5_utf8s_to_ucs2s(krb5_ucs2 *ucs2str, int krb5int_utf8s_to_ucs2s(const char *utf8s, - krb5_ucs2 **ucs2s, - size_t *ucs2chars) + krb5_ucs2 **ucs2s, + size_t *ucs2chars) { ssize_t len; size_t chars; @@ -139,18 +140,18 @@ krb5int_utf8s_to_ucs2s(const char *utf8s, chars = krb5int_utf8_chars(utf8s); *ucs2s = (krb5_ucs2 *)malloc((chars + 1) * sizeof(krb5_ucs2)); if (*ucs2s == NULL) { - return ENOMEM; + return ENOMEM; } len = k5_utf8s_to_ucs2s(*ucs2s, utf8s, chars + 1, 0); if (len < 0) { - free(*ucs2s); - *ucs2s = NULL; - return EINVAL; + free(*ucs2s); + *ucs2s = NULL; + return EINVAL; } if (ucs2chars != NULL) { - *ucs2chars = chars; + *ucs2chars = chars; } return 0; @@ -158,9 +159,9 @@ krb5int_utf8s_to_ucs2s(const char *utf8s, int krb5int_utf8cs_to_ucs2s(const char *utf8s, - size_t utf8slen, - krb5_ucs2 **ucs2s, - size_t *ucs2chars) + size_t utf8slen, + krb5_ucs2 **ucs2s, + size_t *ucs2chars) { ssize_t len; size_t chars; @@ -168,18 +169,18 @@ krb5int_utf8cs_to_ucs2s(const char *utf8s, chars = krb5int_utf8c_chars(utf8s, utf8slen); *ucs2s = (krb5_ucs2 *)malloc((chars + 1) * sizeof(krb5_ucs2)); if (*ucs2s == NULL) { - return ENOMEM; + return ENOMEM; } len = k5_utf8s_to_ucs2s(*ucs2s, utf8s, chars + 1, 0); if (len < 0) { - free(*ucs2s); - *ucs2s = NULL; - return EINVAL; + free(*ucs2s); + *ucs2s = NULL; + return EINVAL; } if (ucs2chars != NULL) { - *ucs2chars = chars; + *ucs2chars = chars; } return 0; @@ -188,7 +189,7 @@ krb5int_utf8cs_to_ucs2s(const char *utf8s, int krb5int_utf8s_to_ucs2les(const char *utf8s, unsigned char **ucs2les, - size_t *ucs2leslen) + size_t *ucs2leslen) { ssize_t len; size_t chars; @@ -197,18 +198,18 @@ krb5int_utf8s_to_ucs2les(const char *utf8s, *ucs2les = (unsigned char *)malloc((chars + 1) * sizeof(krb5_ucs2)); if (*ucs2les == NULL) { - return ENOMEM; + return ENOMEM; } len = k5_utf8s_to_ucs2s((krb5_ucs2 *)*ucs2les, utf8s, chars + 1, 1); if (len < 0) { - free(*ucs2les); - *ucs2les = NULL; - return EINVAL; + free(*ucs2les); + *ucs2les = NULL; + return EINVAL; } if (ucs2leslen != NULL) { - *ucs2leslen = chars * sizeof(krb5_ucs2); + *ucs2leslen = chars * sizeof(krb5_ucs2); } return 0; @@ -216,9 +217,9 @@ krb5int_utf8s_to_ucs2les(const char *utf8s, int krb5int_utf8cs_to_ucs2les(const char *utf8s, - size_t utf8slen, - unsigned char **ucs2les, - size_t *ucs2leslen) + size_t utf8slen, + unsigned char **ucs2les, + size_t *ucs2leslen) { ssize_t len; size_t chars; @@ -227,95 +228,95 @@ krb5int_utf8cs_to_ucs2les(const char *utf8s, *ucs2les = (unsigned char *)malloc((chars + 1) * sizeof(krb5_ucs2)); if (*ucs2les == NULL) { - return ENOMEM; + return ENOMEM; } len = k5_utf8s_to_ucs2s((krb5_ucs2 *)*ucs2les, utf8s, chars + 1, 1); if (len < 0) { - free(*ucs2les); - *ucs2les = NULL; - return EINVAL; + free(*ucs2les); + *ucs2les = NULL; + return EINVAL; } if (ucs2leslen != NULL) { - *ucs2leslen = chars * sizeof(krb5_ucs2); + *ucs2leslen = chars * sizeof(krb5_ucs2); } return 0; } /*----------------------------------------------------------------------------- - Convert a wide char string to a UTF-8 string. - No more than 'count' bytes will be written to the output buffer. - Return the # of bytes written to the output buffer, excl null terminator. + Convert a wide char string to a UTF-8 string. + No more than 'count' bytes will be written to the output buffer. + Return the # of bytes written to the output buffer, excl null terminator. - ucs2len is -1 if the UCS-2 string is NUL terminated, otherwise it is the - length of the UCS-2 string in characters + ucs2len is -1 if the UCS-2 string is NUL terminated, otherwise it is the + length of the UCS-2 string in characters */ static ssize_t k5_ucs2s_to_utf8s(char *utf8str, const krb5_ucs2 *ucs2str, - size_t count, ssize_t ucs2len, int little_endian) + size_t count, ssize_t ucs2len, int little_endian) { int len = 0; int n; char *p = utf8str; krb5_ucs2 empty = 0, ch; - if (ucs2str == NULL) /* Treat input ptr NULL as an empty string */ - ucs2str = ∅ + if (ucs2str == NULL) /* Treat input ptr NULL as an empty string */ + ucs2str = ∅ - if (utf8str == NULL) /* Just compute size of output, excl null */ + if (utf8str == NULL) /* Just compute size of output, excl null */ { - while (ucs2len == -1 ? *ucs2str : --ucs2len >= 0) { - /* Get UTF-8 size of next wide char */ - ch = *ucs2str++; + while (ucs2len == -1 ? *ucs2str : --ucs2len >= 0) { + /* Get UTF-8 size of next wide char */ + ch = *ucs2str++; #ifdef K5_BE - if (little_endian) - ch = SWAP16(ch); + if (little_endian) + ch = SWAP16(ch); #endif - n = krb5int_ucs2_to_utf8(ch, NULL); - if (n < 1) - return -1; - if (len + n < len) - return -1; /* overflow */ - len += n; - } + n = krb5int_ucs2_to_utf8(ch, NULL); + if (n < 1) + return -1; + if (len + n < len) + return -1; /* overflow */ + len += n; + } - return len; + return len; } /* Do the actual conversion. */ - n = 1; /* In case of empty ucs2str */ + n = 1; /* In case of empty ucs2str */ while (ucs2len == -1 ? *ucs2str != 0 : --ucs2len >= 0) { - ch = *ucs2str++; + ch = *ucs2str++; #ifdef K5_BE - if (little_endian) - ch = SWAP16(ch); + if (little_endian) + ch = SWAP16(ch); #endif - n = krb5int_ucs2_to_utf8(ch, p); + n = krb5int_ucs2_to_utf8(ch, p); - if (n < 1) - break; + if (n < 1) + break; - p += n; - count -= n; /* Space left in output buffer */ + p += n; + count -= n; /* Space left in output buffer */ } /* If not enough room for last character, pad remainder with null so that return value = original count, indicating buffer full. */ if (n == 0) { - while (count--) - *p++ = 0; + while (count--) + *p++ = 0; } /* Add a null terminator if there's room. */ else if (count) - *p = 0; + *p = 0; - if (n == -1) /* Conversion encountered invalid wide char. */ - return -1; + if (n == -1) /* Conversion encountered invalid wide char. */ + return -1; /* Return the number of bytes written to output buffer, excl null. */ return (p - utf8str); @@ -323,30 +324,30 @@ k5_ucs2s_to_utf8s(char *utf8str, const krb5_ucs2 *ucs2str, int krb5int_ucs2s_to_utf8s(const krb5_ucs2 *ucs2s, - char **utf8s, - size_t *utf8slen) + char **utf8s, + size_t *utf8slen) { ssize_t len; len = k5_ucs2s_to_utf8s(NULL, ucs2s, 0, -1, 0); if (len < 0) { - return EINVAL; + return EINVAL; } *utf8s = (char *)malloc((size_t)len + 1); if (*utf8s == NULL) { - return ENOMEM; + return ENOMEM; } len = k5_ucs2s_to_utf8s(*utf8s, ucs2s, (size_t)len + 1, -1, 0); if (len < 0) { - free(*utf8s); - *utf8s = NULL; - return EINVAL; + free(*utf8s); + *utf8s = NULL; + return EINVAL; } if (utf8slen != NULL) { - *utf8slen = len; + *utf8slen = len; } return 0; @@ -354,29 +355,29 @@ krb5int_ucs2s_to_utf8s(const krb5_ucs2 *ucs2s, int krb5int_ucs2les_to_utf8s(const unsigned char *ucs2les, - char **utf8s, - size_t *utf8slen) + char **utf8s, + size_t *utf8slen) { ssize_t len; len = k5_ucs2s_to_utf8s(NULL, (krb5_ucs2 *)ucs2les, 0, -1, 1); if (len < 0) - return EINVAL; + return EINVAL; *utf8s = (char *)malloc((size_t)len + 1); if (*utf8s == NULL) { - return ENOMEM; + return ENOMEM; } len = k5_ucs2s_to_utf8s(*utf8s, (krb5_ucs2 *)ucs2les, (size_t)len + 1, -1, 1); if (len < 0) { - free(*utf8s); - *utf8s = NULL; - return EINVAL; + free(*utf8s); + *utf8s = NULL; + return EINVAL; } if (utf8slen != NULL) { - *utf8slen = len; + *utf8slen = len; } return 0; @@ -391,28 +392,28 @@ krb5int_ucs2cs_to_utf8s(const krb5_ucs2 *ucs2s, ssize_t len; if (ucs2slen > SSIZE_MAX) - return ERANGE; + return ERANGE; len = k5_ucs2s_to_utf8s(NULL, (krb5_ucs2 *)ucs2s, 0, - (ssize_t)ucs2slen, 0); + (ssize_t)ucs2slen, 0); if (len < 0) - return EINVAL; + return EINVAL; *utf8s = (char *)malloc((size_t)len + 1); if (*utf8s == NULL) { - return ENOMEM; + return ENOMEM; } len = k5_ucs2s_to_utf8s(*utf8s, (krb5_ucs2 *)ucs2s, - (size_t)len + 1, (ssize_t)ucs2slen, 0); + (size_t)len + 1, (ssize_t)ucs2slen, 0); if (len < 0) { - free(*utf8s); - *utf8s = NULL; - return EINVAL; + free(*utf8s); + *utf8s = NULL; + return EINVAL; } if (utf8slen != NULL) { - *utf8slen = len; + *utf8slen = len; } return 0; @@ -427,28 +428,28 @@ krb5int_ucs2lecs_to_utf8s(const unsigned char *ucs2les, ssize_t len; if (ucs2leslen > SSIZE_MAX) - return ERANGE; + return ERANGE; len = k5_ucs2s_to_utf8s(NULL, (krb5_ucs2 *)ucs2les, 0, - (ssize_t)ucs2leslen, 1); + (ssize_t)ucs2leslen, 1); if (len < 0) - return EINVAL; + return EINVAL; *utf8s = (char *)malloc((size_t)len + 1); if (*utf8s == NULL) { - return ENOMEM; + return ENOMEM; } len = k5_ucs2s_to_utf8s(*utf8s, (krb5_ucs2 *)ucs2les, - (size_t)len + 1, (ssize_t)ucs2leslen, 1); + (size_t)len + 1, (ssize_t)ucs2leslen, 1); if (len < 0) { - free(*utf8s); - *utf8s = NULL; - return EINVAL; + free(*utf8s); + *utf8s = NULL; + return EINVAL; } if (utf8slen != NULL) { - *utf8slen = len; + *utf8slen = len; } return 0;