]> git.sur5r.net Git - openldap/blobdiff - libraries/liblutil/utils.c
Merge remote-tracking branch 'origin/mdb.master'
[openldap] / libraries / liblutil / utils.c
index 386657593f103de9b47b14da12c9c6f8d260f149..6ed4c6bb04fa6daa917768608614aef987f12b6b 100644 (file)
@@ -1,7 +1,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1998-2007 The OpenLDAP Foundation.
+ * Copyright 1998-2012 The OpenLDAP Foundation.
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
 
 #include "portable.h"
 
+#include <limits.h>
 #include <stdio.h>
 #include <ac/stdlib.h>
+#include <ac/stdarg.h>
 #include <ac/string.h>
 #include <ac/ctype.h>
 #include <ac/unistd.h>
@@ -35,6 +37,7 @@
 #include "lutil.h"
 #include "ldap_defaults.h"
 #include "ldap_pvt.h"
+#include "lber_pvt.h"
 
 #ifdef HAVE_EBCDIC
 int _trans_argv = 1;
@@ -75,6 +78,13 @@ char* lutil_progname( const char* name, int argc, char *argv[] )
        LUTIL_SLASHPATH( argv[0] );
        progname = strrchr ( argv[0], *LDAP_DIRSEP );
        progname = progname ? &progname[1] : argv[0];
+#ifdef _WIN32
+       {
+               size_t len = strlen( progname );
+               if ( len > 4 && strcasecmp( &progname[len - 4], ".exe" ) == 0 )
+                       progname[len - 4] = '\0';
+       }
+#endif
        return progname;
 }
 
@@ -138,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 )
@@ -273,117 +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 );
-
-       /* 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.
-        */
-       if ( !cFreq.QuadPart ) {
-               long long t;
-               int usec;
-               QueryPerformanceFrequency( &cFreq );
-
-               t = count.QuadPart * 1000000;
-               t /= cFreq.QuadPart;
-               usec = t % 10000000;
-               usec /= 1000;
-               offset = ( usec - st.wMilliseconds ) * 1000;
-       }
-
-       /* 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;
-       }
-
-       tm->tm_usub = subs;
-
-       /* convert to microseconds */
-       count.QuadPart *= 1000000;
-       count.QuadPart /= cFreq.QuadPart;
-       count.QuadPart -= offset;
-
-       tm->tm_usec = count.QuadPart % 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.
@@ -419,6 +318,21 @@ lutil_strncopy(
        return a-1;
 }
 
+/* memcopy is like memcpy except it returns a pointer to the byte past
+ * the end of the result buffer, set to NULL. This allows fast construction
+ * of catenated buffers.  Provided for API consistency with lutil_str*copy().
+ */
+char *
+lutil_memcopy(
+       char *a,
+       const char *b,
+       size_t n
+)
+{
+       AC_MEMCPY(a, b, n);
+       return a + n;
+}
+
 #ifndef HAVE_MKSTEMP
 int mkstemp( char * template )
 {
@@ -431,6 +345,40 @@ int mkstemp( char * template )
 #endif
 
 #ifdef _MSC_VER
+/* Equivalent of MS CRT's _dosmaperr().
+ * @param lastError[in] Result of GetLastError().
+ */
+static errno_t win2errno(DWORD lastError)
+{
+       const struct { 
+               DWORD   windows_code;
+               errno_t errno_code;
+       } WIN2ERRNO_TABLE[] = {
+               { ERROR_SUCCESS, 0 },
+               { ERROR_FILE_NOT_FOUND, ENOENT },
+               { ERROR_PATH_NOT_FOUND, ENOENT },
+               { ERROR_TOO_MANY_OPEN_FILES, EMFILE },
+               { ERROR_ACCESS_DENIED, EACCES },
+               { ERROR_INVALID_HANDLE, EBADF },
+               { ERROR_NOT_ENOUGH_MEMORY, ENOMEM },
+               { ERROR_LOCK_VIOLATION, EACCES },
+               { ERROR_FILE_EXISTS, EEXIST },
+               { ERROR_INVALID_PARAMETER, EINVAL },
+               { ERROR_FILENAME_EXCED_RANGE, ENAMETOOLONG },
+       };
+       const unsigned int WIN2ERRNO_TABLE_SIZE = sizeof(WIN2ERRNO_TABLE) /
+sizeof(WIN2ERRNO_TABLE[0]);
+       const errno_t DEFAULT_ERRNO_ERROR = -1;
+       unsigned int i;
+
+       for (i = 0; i < WIN2ERRNO_TABLE_SIZE; ++i) {
+               if (WIN2ERRNO_TABLE[i].windows_code == lastError) {
+                       return WIN2ERRNO_TABLE[i].errno_code;
+               }
+       }
+       return DEFAULT_ERRNO_ERROR;
+}
+
 struct dirent {
        char *d_name;
 };
@@ -448,8 +396,10 @@ DIR *opendir( char *path )
        HANDLE h;
        WIN32_FIND_DATA data;
        
-       if (len+3 >= sizeof(tmp))
+       if (len+3 >= sizeof(tmp)) {
+               errno = ENAMETOOLONG;
                return NULL;
+       }
 
        strcpy(tmp, path);
        tmp[len++] = '\\';
@@ -457,9 +407,11 @@ DIR *opendir( char *path )
        tmp[len] = '\0';
 
        h = FindFirstFile( tmp, &data );
-       
-       if ( h == INVALID_HANDLE_VALUE )
+
+       if ( h == INVALID_HANDLE_VALUE ) {
+               errno = win2errno( GetLastError());
                return NULL;
+       }
 
        d = ber_memalloc( sizeof(DIR) );
        if ( !d )
@@ -483,7 +435,7 @@ struct dirent *readdir(DIR *dir)
        }
        return &dir->data;
 }
-void closedir(DIR *dir)
+int closedir(DIR *dir)
 {
        FindClose(dir->dir);
        ber_memfree(dir);
@@ -494,7 +446,7 @@ void closedir(DIR *dir)
  * Memory Reverse Search
  */
 void *
-lutil_memrchr(const void *b, int c, size_t n)
+(lutil_memrchr)(const void *b, int c, size_t n)
 {
        if (n != 0) {
                const unsigned char *s, *bb = b, cc = c;
@@ -563,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;
@@ -582,40 +545,128 @@ 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 _decnum {
+typedef struct lutil_int_decnum {
        unsigned char *buf;
        int bufsiz;
        int beg;
        int len;
-} _decnum;
+} lutil_int_decnum;
 
 #define        FACTOR1 (100000000&0xffff)
 #define FACTOR2 (100000000>>16)
 
 static void
-scale( int new, _decnum *prev, unsigned char *tmp )
+scale( int new, lutil_int_decnum *prev, unsigned char *tmp )
 {
        int i, j;
        unsigned char *in = prev->buf+prev->beg;
@@ -643,18 +694,18 @@ scale( int new, _decnum *prev, unsigned char *tmp )
                prev->len -= j;
        }
 
-       out = tmp + prev->bufsiz - 1;
-       for ( i = 0; new ; i-- ) {
+       out = tmp + prev->bufsiz;
+       i = 0;
+       do {
+               i--;
                new += out[i];
                out[i] = new & 0xff;
                new >>= 8;
-               if (!new )
-                       break;
-       }
-       if ( !prev->len ) {
-               prev->beg += i;
-               prev->len = -i;
-               prev->len++;
+       } while ( new );
+       i = -i;
+       if ( prev->len < i ) {
+               prev->beg = prev->bufsiz - i;
+               prev->len = i;
        }
        AC_MEMCPY( prev->buf+prev->beg, tmp+prev->beg, prev->len );
 }
@@ -669,11 +720,10 @@ scale( int new, _decnum *prev, unsigned char *tmp )
  * any hex input.
  */
 int
-lutil_str2bin( struct berval *in, struct berval *out )
+lutil_str2bin( struct berval *in, struct berval *out, void *ctx )
 {
-       char *pin, *pout, ctmp;
+       char *pin, *pout;
        char *end;
-       long l;
        int i, chunk, len, rc = 0, hex = 0;
        if ( !out || !out->bv_val || out->bv_len < in->bv_len )
                return -1;
@@ -696,55 +746,59 @@ lutil_str2bin( struct berval *in, struct berval *out )
        }
        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
                 */
-               chunk = len & (HEXMAX-1);
+               chunk = len % HEXMAX;
                if ( !chunk )
                        chunk = HEXMAX;
 
                while ( len ) {
-                       ctmp = pin[chunk];
-                       pin[chunk] = '\0';
+                       int ochunk;
+                       memcpy( tbuf, pin, chunk );
+                       tbuf[chunk] = '\0';
                        errno = 0;
-                       l = strtol( pin, &end, 16 );
-                       pin[chunk] = ctmp;
+                       l = strtoul( tbuf, &end, 16 );
                        if ( errno )
                                return -1;
-                       chunk++;
-                       chunk >>= 1;
-                       for ( i = chunk; i>=0; i-- ) {
+                       ochunk = (chunk + 1)/2;
+                       for ( i = ochunk - 1; i >= 0; i-- ) {
                                pout[i] = l & 0xff;
                                l >>= 8;
                        }
                        pin += chunk;
-                       pout += sizeof(long);
+                       pout += ochunk;
                        len -= chunk;
                        chunk = HEXMAX;
                }
-               out->bv_len = pout + len - out->bv_val;
+               out->bv_len = pout - out->bv_val;
        } else {
        /* Decimal */
+#define        DECMAX  8       /* 8 digits at a time */
                char tmpbuf[64], *tmp;
-               _decnum num;
+               lutil_int_decnum num;
                int neg = 0;
+               long l;
+               char tbuf[DECMAX+1];
 
                len = in->bv_len;
                pin = in->bv_val;
-               num.buf = out->bv_val;
+               num.buf = (unsigned char *)out->bv_val;
                num.bufsiz = out->bv_len;
                num.beg = num.bufsiz-1;
                num.len = 0;
                if ( pin[0] == '-' ) {
-                       neg = 1;
+                       neg = 0xff;
                        len--;
                        pin++;
                }
 
-#define        DECMAX  8       /* 8 digits at a time */
-
-               if ( len > sizeof(tmpbuf)) {
-                       tmp = ber_memalloc( len );
+               /* tmp must be at least as large as outbuf */
+               if ( out->bv_len > sizeof(tmpbuf)) {
+                       tmp = ber_memalloc_x( out->bv_len, ctx );
                } else {
                        tmp = tmpbuf;
                }
@@ -753,23 +807,21 @@ lutil_str2bin( struct berval *in, struct berval *out )
                        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;
                        }
-                       scale( l, &num, tmp );
+                       scale( l, &num, (unsigned char *)tmp );
                        pin += chunk;
                        len -= chunk;
                        chunk = DECMAX;
                }
                /* Negate the result */
                if ( neg ) {
-                       int i, j;
                        unsigned char *ptr;
 
                        ptr = num.buf+num.beg;
@@ -778,36 +830,21 @@ lutil_str2bin( struct berval *in, struct berval *out )
                        for ( i=0; i<num.len; i++ )
                                ptr[i] ^= 0xff;
 
-                       /* Add 1, with carry */
-                       i--;
-                       j = 1;
-                       for ( ; i>=0; i-- ) {
-                               j += ptr[i];
-                               ptr[i] = j & 0xff;
-                               j >>= 8;
-                               if (!j)
-                                       break;
-                       }
-                       /* If we overflowed and there's still room,
-                        * set an explicit sign byte
-                        */
-                       if ( !(  ptr[0] & 0x80 ) && num.beg ) {
-                               num.beg--;
-                               num.len++;
-                               num.buf[num.beg] = 0x80;
-                       }
-               } else if (( num.buf[num.beg] & 0x80 ) && num.beg ) {
-                       /* positive int with high bit set, prepend 0 */
+                       /* add 1, with carry - overflow handled below */
+                       while ( i-- && ! (ptr[i] = (ptr[i] + 1) & 0xff )) ;
+               }
+               /* Prepend sign byte if wrong sign bit */
+               if (( num.buf[num.beg] ^ neg ) & 0x80 ) {
                        num.beg--;
                        num.len++;
-                       num.buf[num.beg] = 0;
+                       num.buf[num.beg] = neg;
                }
                if ( num.beg )
                        AC_MEMCPY( num.buf, num.buf+num.beg, num.len );
                out->bv_len = num.len;
 decfail:
                if ( tmp != tmpbuf ) {
-                       ber_memfree( tmp );
+                       ber_memfree_x( tmp, ctx );
                }
        }
        return rc;
@@ -897,3 +934,53 @@ lutil_unparse_time(
        return 0;
 }
 
+/*
+ * formatted print to string
+ *
+ * - if return code < 0, the error code returned by vsnprintf(3) is returned
+ *
+ * - if return code > 0, the buffer was not long enough;
+ *     - if next is not NULL, *next will be set to buf + bufsize - 1
+ *     - if len is not NULL, *len will contain the required buffer length
+ *
+ * - if return code == 0, the buffer was long enough;
+ *     - if next is not NULL, *next will point to the end of the string printed so far
+ *     - if len is not NULL, *len will contain the length of the string printed so far 
+ */
+int
+lutil_snprintf( char *buf, ber_len_t bufsize, char **next, ber_len_t *len, LDAP_CONST char *fmt, ... )
+{
+       va_list         ap;
+       int             ret;
+
+       assert( buf != NULL );
+       assert( bufsize > 0 );
+       assert( fmt != NULL );
+
+       va_start( ap, fmt );
+       ret = vsnprintf( buf, bufsize, fmt, ap );
+       va_end( ap );
+
+       if ( ret < 0 ) {
+               return ret;
+       }
+
+       if ( len ) {
+               *len = ret;
+       }
+
+       if ( (unsigned) ret >= bufsize ) {
+               if ( next ) {
+                       *next = &buf[ bufsize - 1 ];
+               }
+
+               return 1;
+       }
+
+       if ( next ) {
+               *next = &buf[ ret ];
+       }
+
+       return 0;
+}
+