]> git.sur5r.net Git - openldap/blobdiff - libraries/liblutil/utils.c
Merge remote-tracking branch 'origin/mdb.master' into OPENLDAP_REL_ENG_2_4
[openldap] / libraries / liblutil / utils.c
index 09e02c031b0fe86c04d40822aeadd62557f1290e..3de49b970b13ab0f62102aa8c789ab912797ebec 100644 (file)
@@ -1,7 +1,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2010 The OpenLDAP Foundation.
+ * Copyright 1998-2013 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -15,6 +15,7 @@
 
 #include "portable.h"
 
+#include <limits.h>
 #include <stdio.h>
 #include <ac/stdlib.h>
 #include <ac/stdarg.h>
@@ -147,7 +148,7 @@ size_t lutil_localtime( char *s, size_t smax, const struct tm *tm, long delta )
        snprintf( p, smax - 15, "%02ld%02ld", delta / 3600,
                        ( delta % 3600 ) / 60 );
 
-       return ret + 5;
+       return ret + 4;
 }
 
 int lutil_tm2time( struct lutil_tm *tm, struct lutil_timet *tt )
@@ -282,128 +283,6 @@ int lutil_parsetime( char *atm, struct lutil_tm *tm )
        return -1;
 }
 
-/* return a broken out time, with microseconds
- * Must be mutex-protected.
- */
-#ifdef _WIN32
-/* Windows SYSTEMTIME only has 10 millisecond resolution, so we
- * also need to use a high resolution timer to get microseconds.
- * This is pretty clunky.
- */
-void
-lutil_gettime( struct lutil_tm *tm )
-{
-       static LARGE_INTEGER cFreq;
-       static LARGE_INTEGER prevCount;
-       static int subs;
-       static int offset;
-       LARGE_INTEGER count;
-       SYSTEMTIME st;
-
-       GetSystemTime( &st );
-       QueryPerformanceCounter( &count );
-
-       /* It shouldn't ever go backwards, but multiple CPUs might
-        * be able to hit in the same tick.
-        */
-       if ( count.QuadPart <= prevCount.QuadPart ) {
-               subs++;
-       } else {
-               subs = 0;
-               prevCount = count;
-       }
-
-       /* We assume Windows has at least a vague idea of
-        * when a second begins. So we align our microsecond count
-        * with the Windows millisecond count using this offset.
-        * We retain the submillisecond portion of our own count.
-        *
-        * Note - this also assumes that the relationship between
-        * the PerformanceCouunter and SystemTime stays constant;
-        * that assumption breaks if the SystemTime is adjusted by
-        * an external action.
-        */
-       if ( !cFreq.QuadPart ) {
-               long long t;
-               int usec;
-               QueryPerformanceFrequency( &cFreq );
-
-               /* just get sub-second portion of counter */
-               t = count.QuadPart % cFreq.QuadPart;
-
-               /* convert to microseconds */
-               t *= 1000000;
-               usec = t / cFreq.QuadPart;
-
-               offset = usec - st.wMilliseconds * 1000;
-       }
-
-       tm->tm_usub = subs;
-
-       /* convert to microseconds */
-       count.QuadPart %= cFreq.QuadPart;
-       count.QuadPart *= 1000000;
-       count.QuadPart /= cFreq.QuadPart;
-       count.QuadPart -= offset;
-
-       tm->tm_usec = count.QuadPart % 1000000;
-       if ( tm->tm_usec < 0 )
-               tm->tm_usec += 1000000;
-
-       /* any difference larger than microseconds is
-        * already reflected in st
-        */
-
-       tm->tm_sec = st.wSecond;
-       tm->tm_min = st.wMinute;
-       tm->tm_hour = st.wHour;
-       tm->tm_mday = st.wDay;
-       tm->tm_mon = st.wMonth - 1;
-       tm->tm_year = st.wYear - 1900;
-}
-#else
-void
-lutil_gettime( struct lutil_tm *ltm )
-{
-       struct timeval tv;
-       static struct timeval prevTv;
-       static int subs;
-
-#ifdef HAVE_GMTIME_R
-       struct tm tm_buf;
-#endif
-       struct tm *tm;
-       time_t t;
-
-       gettimeofday( &tv, NULL );
-       t = tv.tv_sec;
-
-       if ( tv.tv_sec < prevTv.tv_sec
-               || ( tv.tv_sec == prevTv.tv_sec && tv.tv_usec <= prevTv.tv_usec )) {
-               subs++;
-       } else {
-               subs = 0;
-               prevTv = tv;
-       }
-
-       ltm->tm_usub = subs;
-
-#ifdef HAVE_GMTIME_R
-       tm = gmtime_r( &t, &tm_buf );
-#else
-       tm = gmtime( &t );
-#endif
-
-       ltm->tm_sec = tm->tm_sec;
-       ltm->tm_min = tm->tm_min;
-       ltm->tm_hour = tm->tm_hour;
-       ltm->tm_mday = tm->tm_mday;
-       ltm->tm_mon = tm->tm_mon;
-       ltm->tm_year = tm->tm_year;
-       ltm->tm_usec = tv.tv_usec;
-}
-#endif
-
 /* strcopy is like strcpy except it returns a pointer to the trailing NUL of
  * the result string. This allows fast construction of catenated strings
  * without the overhead of strlen/strcat.
@@ -636,17 +515,28 @@ lutil_atoux( unsigned *v, const char *s, int x )
 int
 lutil_atolx( long *v, const char *s, int x )
 {
-       char            *next;
-       long            l;
+       char *next;
+       long l;
+       int save_errno;
 
        assert( s != NULL );
        assert( v != NULL );
 
+       if ( isspace( s[ 0 ] ) ) {
+               return -1;
+       }
+
+       errno = 0;
        l = strtol( s, &next, x );
+       save_errno = errno;
        if ( next == s || next[ 0 ] != '\0' ) {
                return -1;
        }
 
+       if ( ( l == LONG_MIN || l == LONG_MAX ) && save_errno != 0 ) {
+               return -1;
+       }
+
        *v = l;
 
        return 0;
@@ -655,27 +545,115 @@ lutil_atolx( long *v, const char *s, int x )
 int
 lutil_atoulx( unsigned long *v, const char *s, int x )
 {
-       char            *next;
-       unsigned long   ul;
+       char *next;
+       unsigned long ul;
+       int save_errno;
 
        assert( s != NULL );
        assert( v != NULL );
 
        /* strtoul() has an odd interface */
-       if ( s[ 0 ] == '-' ) {
+       if ( s[ 0 ] == '-' || isspace( s[ 0 ] ) ) {
                return -1;
        }
 
+       errno = 0;
        ul = strtoul( s, &next, x );
+       save_errno = errno;
        if ( next == s || next[ 0 ] != '\0' ) {
                return -1;
        }
 
+       if ( ( ul == 0 || ul == ULONG_MAX ) && save_errno != 0 ) {
+               return -1;
+       }
+
        *v = ul;
 
        return 0;
 }
 
+#ifdef HAVE_LONG_LONG
+#if defined(HAVE_STRTOLL) || defined(HAVE_STRTOQ)
+int
+lutil_atollx( long long *v, const char *s, int x )
+{
+       char *next;
+       long long ll;
+       int save_errno;
+
+       assert( s != NULL );
+       assert( v != NULL );
+
+       if ( isspace( s[ 0 ] ) ) {
+               return -1;
+       }
+
+       errno = 0;
+#ifdef HAVE_STRTOLL
+       ll = strtoll( s, &next, x );
+#else /* HAVE_STRTOQ */
+       ll = (unsigned long long)strtoq( s, &next, x );
+#endif /* HAVE_STRTOQ */
+       save_errno = errno;
+       if ( next == s || next[ 0 ] != '\0' ) {
+               return -1;
+       }
+
+       /* LLONG_MIN, LLONG_MAX are C99 only */
+#if defined (LLONG_MIN) && defined(LLONG_MAX)
+       if ( ( ll == LLONG_MIN || ll == LLONG_MAX ) && save_errno != 0 ) {
+               return -1;
+       }
+#endif /* LLONG_MIN && LLONG_MAX */
+
+       *v = ll;
+
+       return 0;
+}
+#endif /* HAVE_STRTOLL || HAVE_STRTOQ */
+
+#if defined(HAVE_STRTOULL) || defined(HAVE_STRTOUQ)
+int
+lutil_atoullx( unsigned long long *v, const char *s, int x )
+{
+       char *next;
+       unsigned long long ull;
+       int save_errno;
+
+       assert( s != NULL );
+       assert( v != NULL );
+
+       /* strtoull() has an odd interface */
+       if ( s[ 0 ] == '-' || isspace( s[ 0 ] ) ) {
+               return -1;
+       }
+
+       errno = 0;
+#ifdef HAVE_STRTOULL
+       ull = strtoull( s, &next, x );
+#else /* HAVE_STRTOUQ */
+       ull = (unsigned long long)strtouq( s, &next, x );
+#endif /* HAVE_STRTOUQ */
+       save_errno = errno;
+       if ( next == s || next[ 0 ] != '\0' ) {
+               return -1;
+       }
+
+       /* ULLONG_MAX is C99 only */
+#if defined(ULLONG_MAX)
+       if ( ( ull == 0 || ull == ULLONG_MAX ) && save_errno != 0 ) {
+               return -1;
+       }
+#endif /* ULLONG_MAX */
+
+       *v = ull;
+
+       return 0;
+}
+#endif /* HAVE_STRTOULL || HAVE_STRTOUQ */
+#endif /* HAVE_LONG_LONG */
+
 /* Multiply an integer by 100000000 and add new */
 typedef struct lutil_int_decnum {
        unsigned char *buf;
@@ -736,8 +714,6 @@ scale( int new, lutil_int_decnum *prev, unsigned char *tmp )
  * Output buffer must be provided, bv_len must indicate buffer size
  * Hex input can be "0x1234" or "'1234'H"
  *
- * Temporarily modifies the input string.
- *
  * Note: High bit of binary form is always the sign bit. If the number
  * is supposed to be positive but has the high bit set, a zero byte
  * is prepended. It is assumed that this has already been handled on
@@ -746,7 +722,7 @@ scale( int new, lutil_int_decnum *prev, unsigned char *tmp )
 int
 lutil_str2bin( struct berval *in, struct berval *out, void *ctx )
 {
-       char *pin, *pout, ctmp;
+       char *pin, *pout;
        char *end;
        int i, chunk, len, rc = 0, hex = 0;
        if ( !out || !out->bv_val || out->bv_len < in->bv_len )
@@ -771,6 +747,8 @@ lutil_str2bin( struct berval *in, struct berval *out, void *ctx )
        if ( hex ) {
 #define HEXMAX (2 * sizeof(long))
                unsigned long l;
+               char tbuf[HEXMAX+1];
+
                /* Convert a longword at a time, but handle leading
                 * odd bytes first
                 */
@@ -780,11 +758,10 @@ lutil_str2bin( struct berval *in, struct berval *out, void *ctx )
 
                while ( len ) {
                        int ochunk;
-                       ctmp = pin[chunk];
-                       pin[chunk] = '\0';
+                       memcpy( tbuf, pin, chunk );
+                       tbuf[chunk] = '\0';
                        errno = 0;
-                       l = strtoul( pin, &end, 16 );
-                       pin[chunk] = ctmp;
+                       l = strtoul( tbuf, &end, 16 );
                        if ( errno )
                                return -1;
                        ochunk = (chunk + 1)/2;
@@ -800,10 +777,12 @@ lutil_str2bin( struct berval *in, struct berval *out, void *ctx )
                out->bv_len = pout - out->bv_val;
        } else {
        /* Decimal */
+#define        DECMAX  8       /* 8 digits at a time */
                char tmpbuf[64], *tmp;
                lutil_int_decnum num;
                int neg = 0;
                long l;
+               char tbuf[DECMAX+1];
 
                len = in->bv_len;
                pin = in->bv_val;
@@ -817,8 +796,6 @@ lutil_str2bin( struct berval *in, struct berval *out, void *ctx )
                        pin++;
                }
 
-#define        DECMAX  8       /* 8 digits at a time */
-
                /* tmp must be at least as large as outbuf */
                if ( out->bv_len > sizeof(tmpbuf)) {
                        tmp = ber_memalloc_x( out->bv_len, ctx );
@@ -830,11 +807,10 @@ lutil_str2bin( struct berval *in, struct berval *out, void *ctx )
                        chunk = DECMAX;
 
                while ( len ) {
-                       ctmp = pin[chunk];
-                       pin[chunk] = '\0';
+                       memcpy( tbuf, pin, chunk );
+                       tbuf[chunk] = '\0';
                        errno = 0;
-                       l = strtol( pin, &end, 10 );
-                       pin[chunk] = ctmp;
+                       l = strtol( tbuf, &end, 10 );
                        if ( errno ) {
                                rc = -1;
                                goto decfail;