1999-09-09 03:06:24 +08:00
|
|
|
/* $OpenLDAP$ */
|
2003-11-26 07:17:08 +08:00
|
|
|
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
|
|
|
|
*
|
2020-01-10 00:50:21 +08:00
|
|
|
* Copyright 1998-2020 The OpenLDAP Foundation.
|
2003-11-26 07:17:08 +08:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted only as authorized by the OpenLDAP
|
|
|
|
* Public License.
|
|
|
|
*
|
|
|
|
* A copy of this license is available in the file LICENSE in the
|
|
|
|
* top-level directory of the distribution or, alternatively, at
|
|
|
|
* <http://www.OpenLDAP.org/license.html>.
|
1999-08-18 03:00:59 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "portable.h"
|
|
|
|
|
2010-08-13 07:32:37 +08:00
|
|
|
#include <limits.h>
|
2003-05-15 09:03:38 +08:00
|
|
|
#include <stdio.h>
|
1999-08-18 03:00:59 +08:00
|
|
|
#include <ac/stdlib.h>
|
2007-12-15 23:23:23 +08:00
|
|
|
#include <ac/stdarg.h>
|
1999-08-18 03:00:59 +08:00
|
|
|
#include <ac/string.h>
|
2004-09-28 20:12:13 +08:00
|
|
|
#include <ac/ctype.h>
|
2001-12-18 10:54:49 +08:00
|
|
|
#include <ac/unistd.h>
|
2002-07-27 08:25:16 +08:00
|
|
|
#include <ac/time.h>
|
2007-10-01 14:50:19 +08:00
|
|
|
#include <ac/errno.h>
|
2001-12-18 12:52:55 +08:00
|
|
|
#ifdef HAVE_IO_H
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
2001-12-18 10:48:20 +08:00
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
2007-02-03 07:10:30 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
1999-08-18 03:00:59 +08:00
|
|
|
|
2006-12-04 04:56:25 +08:00
|
|
|
#include "lutil.h"
|
|
|
|
#include "ldap_defaults.h"
|
|
|
|
#include "ldap_pvt.h"
|
2007-12-03 03:11:41 +08:00
|
|
|
#include "lber_pvt.h"
|
1999-08-18 03:00:59 +08:00
|
|
|
|
2002-07-27 08:25:16 +08:00
|
|
|
#ifdef HAVE_EBCDIC
|
|
|
|
int _trans_argv = 1;
|
|
|
|
#endif
|
|
|
|
|
2004-03-19 16:06:42 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
/* Some Windows versions accept both forward and backslashes in
|
|
|
|
* directory paths, but we always use backslashes when generating
|
|
|
|
* and parsing...
|
|
|
|
*/
|
|
|
|
void lutil_slashpath( char *path )
|
|
|
|
{
|
|
|
|
char *c, *p;
|
|
|
|
|
|
|
|
p = path;
|
|
|
|
while (( c=strchr( p, '/' ))) {
|
|
|
|
*c++ = '\\';
|
|
|
|
p = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-08-18 03:00:59 +08:00
|
|
|
char* lutil_progname( const char* name, int argc, char *argv[] )
|
|
|
|
{
|
|
|
|
char *progname;
|
|
|
|
|
|
|
|
if(argc == 0) {
|
2002-07-27 08:25:16 +08:00
|
|
|
return (char *)name;
|
1999-08-18 03:00:59 +08:00
|
|
|
}
|
|
|
|
|
2002-07-27 08:25:16 +08:00
|
|
|
#ifdef HAVE_EBCDIC
|
|
|
|
if (_trans_argv) {
|
|
|
|
int i;
|
|
|
|
for (i=0; i<argc; i++) __etoa(argv[i]);
|
|
|
|
_trans_argv = 0;
|
|
|
|
}
|
|
|
|
#endif
|
2004-03-19 16:06:42 +08:00
|
|
|
LUTIL_SLASHPATH( argv[0] );
|
1999-08-18 03:00:59 +08:00
|
|
|
progname = strrchr ( argv[0], *LDAP_DIRSEP );
|
2002-07-27 08:25:16 +08:00
|
|
|
progname = progname ? &progname[1] : argv[0];
|
2008-07-22 18:16:11 +08:00
|
|
|
#ifdef _WIN32
|
|
|
|
{
|
|
|
|
size_t len = strlen( progname );
|
|
|
|
if ( len > 4 && strcasecmp( &progname[len - 4], ".exe" ) == 0 )
|
|
|
|
progname[len - 4] = '\0';
|
|
|
|
}
|
|
|
|
#endif
|
1999-08-18 03:00:59 +08:00
|
|
|
return progname;
|
|
|
|
}
|
2001-06-15 12:16:55 +08:00
|
|
|
|
2003-05-15 09:18:45 +08:00
|
|
|
#if 0
|
2003-05-15 09:03:38 +08:00
|
|
|
size_t lutil_gentime( char *s, size_t smax, const struct tm *tm )
|
2001-06-15 12:16:55 +08:00
|
|
|
{
|
2002-07-27 08:25:16 +08:00
|
|
|
size_t ret;
|
|
|
|
#ifdef HAVE_EBCDIC
|
|
|
|
/* We've been compiling in ASCII so far, but we want EBCDIC now since
|
|
|
|
* strftime only understands EBCDIC input.
|
|
|
|
*/
|
|
|
|
#pragma convlit(suspend)
|
2001-12-18 10:54:49 +08:00
|
|
|
#endif
|
2003-05-15 09:03:38 +08:00
|
|
|
ret = strftime( s, smax, "%Y%m%d%H%M%SZ", tm );
|
2002-07-27 08:25:16 +08:00
|
|
|
#ifdef HAVE_EBCDIC
|
|
|
|
#pragma convlit(resume)
|
|
|
|
__etoa( s );
|
2001-10-11 12:01:45 +08:00
|
|
|
#endif
|
2002-07-27 08:25:16 +08:00
|
|
|
return ret;
|
|
|
|
}
|
2003-05-15 09:18:45 +08:00
|
|
|
#endif
|
2002-07-20 01:33:14 +08:00
|
|
|
|
2003-05-15 09:03:38 +08:00
|
|
|
size_t lutil_localtime( char *s, size_t smax, const struct tm *tm, long delta )
|
|
|
|
{
|
|
|
|
size_t ret;
|
|
|
|
char *p;
|
|
|
|
|
2003-05-15 09:18:45 +08:00
|
|
|
if ( smax < 16 ) { /* YYYYmmddHHMMSSZ */
|
|
|
|
return 0;
|
2003-05-15 09:03:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_EBCDIC
|
|
|
|
/* We've been compiling in ASCII so far, but we want EBCDIC now since
|
|
|
|
* strftime only understands EBCDIC input.
|
|
|
|
*/
|
|
|
|
#pragma convlit(suspend)
|
|
|
|
#endif
|
|
|
|
ret = strftime( s, smax, "%Y%m%d%H%M%SZ", tm );
|
2003-05-29 14:15:03 +08:00
|
|
|
#ifdef HAVE_EBCDIC
|
|
|
|
#pragma convlit(resume)
|
|
|
|
__etoa( s );
|
|
|
|
#endif
|
2003-05-15 09:18:45 +08:00
|
|
|
if ( delta == 0 || ret == 0 ) {
|
2003-05-15 09:03:38 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2003-05-15 09:18:45 +08:00
|
|
|
if ( smax < 20 ) { /* YYYYmmddHHMMSS+HHMM */
|
|
|
|
return 0;
|
2003-05-15 09:03:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
p = s + 14;
|
|
|
|
|
|
|
|
if ( delta < 0 ) {
|
|
|
|
p[ 0 ] = '-';
|
|
|
|
delta = -delta;
|
|
|
|
} else {
|
|
|
|
p[ 0 ] = '+';
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
|
|
|
|
snprintf( p, smax - 15, "%02ld%02ld", delta / 3600,
|
|
|
|
( delta % 3600 ) / 60 );
|
|
|
|
|
2010-05-03 13:13:44 +08:00
|
|
|
return ret + 4;
|
2003-05-15 09:03:38 +08:00
|
|
|
}
|
|
|
|
|
2020-01-20 03:05:04 +08:00
|
|
|
int lutil_tm2time( struct lutil_tm *tm, struct lutil_timet *tt )
|
|
|
|
{
|
|
|
|
static int moffset[12] = {
|
|
|
|
0, 31, 59, 90, 120,
|
|
|
|
151, 181, 212, 243,
|
|
|
|
273, 304, 334 };
|
|
|
|
int sec;
|
|
|
|
|
|
|
|
tt->tt_usec = tm->tm_usec;
|
|
|
|
|
|
|
|
/* special case 0000/01/01+00:00:00 is returned as zero */
|
|
|
|
if ( tm->tm_year == -1900 && tm->tm_mon == 0 && tm->tm_mday == 1 &&
|
|
|
|
tm->tm_hour == 0 && tm->tm_min == 0 && tm->tm_sec == 0 ) {
|
|
|
|
tt->tt_sec = 0;
|
|
|
|
tt->tt_gsec = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* tm->tm_year is years since 1900 */
|
|
|
|
/* calculate days from years since 1970 (epoch) */
|
|
|
|
tt->tt_sec = tm->tm_year - 70;
|
|
|
|
tt->tt_sec *= 365L;
|
|
|
|
|
|
|
|
/* count leap days in preceding years */
|
|
|
|
tt->tt_sec += ((tm->tm_year -69) >> 2);
|
|
|
|
|
|
|
|
/* calculate days from months */
|
|
|
|
tt->tt_sec += moffset[tm->tm_mon];
|
|
|
|
|
|
|
|
/* add in this year's leap day, if any */
|
|
|
|
if (((tm->tm_year & 3) == 0) && (tm->tm_mon > 1)) {
|
|
|
|
tt->tt_sec ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add in days in this month */
|
|
|
|
tt->tt_sec += (tm->tm_mday - 1);
|
|
|
|
|
|
|
|
/* this function can handle a range of about 17408 years... */
|
|
|
|
/* 86400 seconds in a day, divided by 128 = 675 */
|
|
|
|
tt->tt_sec *= 675;
|
|
|
|
|
|
|
|
/* move high 7 bits into tt_gsec */
|
|
|
|
tt->tt_gsec = tt->tt_sec >> 25;
|
|
|
|
tt->tt_sec -= tt->tt_gsec << 25;
|
|
|
|
|
|
|
|
/* get hours */
|
|
|
|
sec = tm->tm_hour;
|
|
|
|
|
|
|
|
/* convert to minutes */
|
|
|
|
sec *= 60L;
|
|
|
|
sec += tm->tm_min;
|
|
|
|
|
|
|
|
/* convert to seconds */
|
|
|
|
sec *= 60L;
|
|
|
|
sec += tm->tm_sec;
|
|
|
|
|
|
|
|
/* add remaining seconds */
|
|
|
|
tt->tt_sec <<= 7;
|
|
|
|
tt->tt_sec += sec;
|
|
|
|
|
|
|
|
/* return success */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-29 05:31:06 +08:00
|
|
|
/* Proleptic Gregorian Calendar, 1BCE = year 0 */
|
|
|
|
|
2020-01-20 03:05:04 +08:00
|
|
|
int lutil_tm2gtime( struct lutil_tm *tm, struct lutil_timet *tt )
|
2004-09-28 20:12:13 +08:00
|
|
|
{
|
|
|
|
static int moffset[12] = {
|
|
|
|
0, 31, 59, 90, 120,
|
|
|
|
151, 181, 212, 243,
|
2020-01-20 03:05:04 +08:00
|
|
|
273, 304, 334 };
|
2019-11-29 05:31:06 +08:00
|
|
|
int sec, year;
|
|
|
|
long tmp;
|
2004-09-28 20:12:13 +08:00
|
|
|
|
|
|
|
tt->tt_usec = tm->tm_usec;
|
|
|
|
|
|
|
|
/* tm->tm_year is years since 1900 */
|
2019-11-29 05:31:06 +08:00
|
|
|
/* calculate days from 0000 */
|
|
|
|
year = tm->tm_year + 1900;
|
|
|
|
tmp = year * 365;
|
|
|
|
|
|
|
|
/* add in leap days */
|
|
|
|
sec = (year - 1) / 4;
|
|
|
|
tmp += sec;
|
|
|
|
sec /= 25;
|
|
|
|
tmp -= sec;
|
|
|
|
sec /= 4;
|
|
|
|
tmp += sec;
|
|
|
|
/* Year 0000 was a leap year */
|
|
|
|
if (year > 0)
|
|
|
|
tmp++;
|
2004-09-28 20:12:13 +08:00
|
|
|
|
2020-01-20 03:05:04 +08:00
|
|
|
/* calculate days from months */
|
2019-11-29 05:31:06 +08:00
|
|
|
tmp += moffset[tm->tm_mon];
|
2004-09-28 20:12:13 +08:00
|
|
|
|
2019-11-29 05:31:06 +08:00
|
|
|
/* add in this year's leap day, if any */
|
|
|
|
if (tm->tm_mon > 1) {
|
|
|
|
sec = (year % 4) ? 0 : (year % 100) ? 1 : (year % 400) ? 0 : 1;
|
|
|
|
tmp += sec;
|
|
|
|
}
|
2004-09-28 20:12:13 +08:00
|
|
|
|
2019-11-29 05:31:06 +08:00
|
|
|
/* add in days in this month */
|
|
|
|
tmp += (tm->tm_mday - 1);
|
2004-09-28 20:12:13 +08:00
|
|
|
|
2004-09-28 20:36:34 +08:00
|
|
|
/* this function can handle a range of about 17408 years... */
|
2004-09-28 20:12:13 +08:00
|
|
|
/* 86400 seconds in a day, divided by 128 = 675 */
|
2019-11-29 05:31:06 +08:00
|
|
|
tmp *= 675;
|
2004-09-28 20:36:34 +08:00
|
|
|
|
|
|
|
/* move high 7 bits into tt_gsec */
|
2019-11-29 05:31:06 +08:00
|
|
|
tt->tt_gsec = tmp >> 25;
|
|
|
|
tmp -= tt->tt_gsec << 25;
|
|
|
|
|
|
|
|
/* toggle sign bit, keep positive greater than negative */
|
|
|
|
tt->tt_gsec &= 0x7f;
|
|
|
|
tt->tt_gsec ^= 0x40;
|
2004-09-28 20:12:13 +08:00
|
|
|
|
2019-11-29 05:31:06 +08:00
|
|
|
/* get hours */
|
|
|
|
sec = tm->tm_hour;
|
2004-09-28 20:12:13 +08:00
|
|
|
|
2019-11-29 05:31:06 +08:00
|
|
|
/* convert to minutes */
|
|
|
|
sec *= 60L;
|
|
|
|
sec += tm->tm_min;
|
2004-09-28 20:12:13 +08:00
|
|
|
|
2019-11-29 05:31:06 +08:00
|
|
|
/* convert to seconds */
|
|
|
|
sec *= 60L;
|
|
|
|
sec += tm->tm_sec;
|
2004-09-28 20:12:13 +08:00
|
|
|
|
2004-09-28 20:36:34 +08:00
|
|
|
/* add remaining seconds */
|
2019-11-29 05:31:06 +08:00
|
|
|
tmp <<= 7;
|
|
|
|
tmp += sec;
|
|
|
|
tt->tt_sec = tmp;
|
2004-09-28 20:12:13 +08:00
|
|
|
|
|
|
|
/* return success */
|
2019-11-29 05:31:06 +08:00
|
|
|
return 0;
|
2004-09-28 20:12:13 +08:00
|
|
|
}
|
|
|
|
|
2004-09-28 20:22:22 +08:00
|
|
|
int lutil_parsetime( char *atm, struct lutil_tm *tm )
|
2004-09-28 20:12:13 +08:00
|
|
|
{
|
|
|
|
while (atm && tm) {
|
2019-11-29 05:31:06 +08:00
|
|
|
char *ptr;
|
2005-09-11 02:59:35 +08:00
|
|
|
unsigned i, fracs;
|
2019-11-29 05:31:06 +08:00
|
|
|
int neg = 0;
|
|
|
|
|
|
|
|
if (*atm == '-') {
|
|
|
|
neg = 1;
|
|
|
|
atm++;
|
|
|
|
}
|
|
|
|
ptr = atm;
|
2004-09-28 20:12:13 +08:00
|
|
|
|
|
|
|
/* Is the stamp reasonably long? */
|
2006-04-02 07:44:42 +08:00
|
|
|
for (i=0; isdigit((unsigned char) atm[i]); i++);
|
2004-09-28 20:12:13 +08:00
|
|
|
if (i < sizeof("00000101000000")-1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parse the time into a struct tm
|
|
|
|
*/
|
|
|
|
/* 4 digit year to year - 1900 */
|
|
|
|
tm->tm_year = *ptr++ - '0';
|
|
|
|
tm->tm_year *= 10; tm->tm_year += *ptr++ - '0';
|
|
|
|
tm->tm_year *= 10; tm->tm_year += *ptr++ - '0';
|
|
|
|
tm->tm_year *= 10; tm->tm_year += *ptr++ - '0';
|
2019-11-29 05:31:06 +08:00
|
|
|
if (neg)
|
|
|
|
tm->tm_year = -tm->tm_year;
|
2004-09-28 20:12:13 +08:00
|
|
|
tm->tm_year -= 1900;
|
|
|
|
/* month 01-12 to 0-11 */
|
|
|
|
tm->tm_mon = *ptr++ - '0';
|
|
|
|
tm->tm_mon *=10; tm->tm_mon += *ptr++ - '0';
|
|
|
|
if (tm->tm_mon < 1 || tm->tm_mon > 12) break;
|
|
|
|
tm->tm_mon--;
|
|
|
|
|
|
|
|
/* day of month 01-31 */
|
|
|
|
tm->tm_mday = *ptr++ - '0';
|
|
|
|
tm->tm_mday *=10; tm->tm_mday += *ptr++ - '0';
|
|
|
|
if (tm->tm_mday < 1 || tm->tm_mday > 31) break;
|
|
|
|
|
|
|
|
/* Hour 00-23 */
|
|
|
|
tm->tm_hour = *ptr++ - '0';
|
|
|
|
tm->tm_hour *=10; tm->tm_hour += *ptr++ - '0';
|
|
|
|
if (tm->tm_hour < 0 || tm->tm_hour > 23) break;
|
|
|
|
|
|
|
|
/* Minute 00-59 */
|
|
|
|
tm->tm_min = *ptr++ - '0';
|
|
|
|
tm->tm_min *=10; tm->tm_min += *ptr++ - '0';
|
|
|
|
if (tm->tm_min < 0 || tm->tm_min > 59) break;
|
|
|
|
|
|
|
|
/* Second 00-61 */
|
|
|
|
tm->tm_sec = *ptr++ - '0';
|
|
|
|
tm->tm_sec *=10; tm->tm_sec += *ptr++ - '0';
|
|
|
|
if (tm->tm_sec < 0 || tm->tm_sec > 61) break;
|
|
|
|
|
|
|
|
/* Fractions of seconds */
|
2006-04-28 09:26:05 +08:00
|
|
|
if ( *ptr == '.' ) {
|
|
|
|
ptr++;
|
|
|
|
for (i = 0, fracs = 0; isdigit((unsigned char) *ptr); ) {
|
|
|
|
i*=10; i+= *ptr++ - '0';
|
|
|
|
fracs++;
|
|
|
|
}
|
|
|
|
tm->tm_usec = i;
|
|
|
|
if (i) {
|
|
|
|
for (i = fracs; i<6; i++)
|
|
|
|
tm->tm_usec *= 10;
|
|
|
|
}
|
2019-11-29 05:31:06 +08:00
|
|
|
} else {
|
|
|
|
tm->tm_usec = 0;
|
2004-09-28 20:12:13 +08:00
|
|
|
}
|
2019-11-29 05:31:06 +08:00
|
|
|
tm->tm_usub = 0;
|
2004-09-28 20:12:13 +08:00
|
|
|
|
|
|
|
/* Must be UTC */
|
|
|
|
if (*ptr != 'Z') break;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
2003-05-15 09:03:38 +08:00
|
|
|
|
2002-07-27 08:25:16 +08:00
|
|
|
/* 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.
|
2002-07-20 01:33:14 +08:00
|
|
|
*/
|
2002-07-27 08:25:16 +08:00
|
|
|
char *
|
|
|
|
lutil_strcopy(
|
|
|
|
char *a,
|
|
|
|
const char *b
|
|
|
|
)
|
2002-07-20 01:33:14 +08:00
|
|
|
{
|
2002-07-27 08:25:16 +08:00
|
|
|
if (!a || !b)
|
|
|
|
return a;
|
|
|
|
|
|
|
|
while ((*a++ = *b++)) ;
|
|
|
|
return a-1;
|
2002-07-20 01:33:14 +08:00
|
|
|
}
|
|
|
|
|
2002-07-27 08:25:16 +08:00
|
|
|
/* strncopy 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.
|
|
|
|
*/
|
|
|
|
char *
|
|
|
|
lutil_strncopy(
|
|
|
|
char *a,
|
|
|
|
const char *b,
|
|
|
|
size_t n
|
|
|
|
)
|
2002-07-20 01:33:14 +08:00
|
|
|
{
|
2002-07-27 08:25:16 +08:00
|
|
|
if (!a || !b || n == 0)
|
|
|
|
return a;
|
|
|
|
|
|
|
|
while ((*a++ = *b++) && n-- > 0) ;
|
|
|
|
return a-1;
|
|
|
|
}
|
2008-12-12 07:17:08 +08:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
2002-07-20 01:33:14 +08:00
|
|
|
|
2002-07-27 08:25:16 +08:00
|
|
|
#ifndef HAVE_MKSTEMP
|
|
|
|
int mkstemp( char * template )
|
|
|
|
{
|
|
|
|
#ifdef HAVE_MKTEMP
|
|
|
|
return open ( mktemp ( template ), O_RDWR|O_CREAT|O_EXCL, 0600 );
|
|
|
|
#else
|
|
|
|
return -1;
|
|
|
|
#endif
|
2002-07-20 01:33:14 +08:00
|
|
|
}
|
2002-07-27 08:25:16 +08:00
|
|
|
#endif
|
2005-11-14 06:26:53 +08:00
|
|
|
|
2006-09-14 16:01:05 +08:00
|
|
|
#ifdef _MSC_VER
|
2009-04-11 12:22:27 +08:00
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2006-09-14 16:01:05 +08:00
|
|
|
struct dirent {
|
|
|
|
char *d_name;
|
|
|
|
};
|
|
|
|
typedef struct DIR {
|
|
|
|
HANDLE dir;
|
|
|
|
struct dirent data;
|
|
|
|
int first;
|
|
|
|
char buf[MAX_PATH+1];
|
|
|
|
} DIR;
|
|
|
|
DIR *opendir( char *path )
|
|
|
|
{
|
|
|
|
char tmp[32768];
|
|
|
|
int len = strlen(path);
|
|
|
|
DIR *d;
|
|
|
|
HANDLE h;
|
|
|
|
WIN32_FIND_DATA data;
|
|
|
|
|
2009-04-11 12:22:27 +08:00
|
|
|
if (len+3 >= sizeof(tmp)) {
|
|
|
|
errno = ENAMETOOLONG;
|
2006-09-14 16:01:05 +08:00
|
|
|
return NULL;
|
2009-04-11 12:22:27 +08:00
|
|
|
}
|
2006-09-14 16:01:05 +08:00
|
|
|
|
|
|
|
strcpy(tmp, path);
|
|
|
|
tmp[len++] = '\\';
|
|
|
|
tmp[len++] = '*';
|
|
|
|
tmp[len] = '\0';
|
|
|
|
|
|
|
|
h = FindFirstFile( tmp, &data );
|
2009-04-11 12:22:27 +08:00
|
|
|
|
|
|
|
if ( h == INVALID_HANDLE_VALUE ) {
|
|
|
|
errno = win2errno( GetLastError());
|
2006-09-14 16:01:05 +08:00
|
|
|
return NULL;
|
2009-04-11 12:22:27 +08:00
|
|
|
}
|
2006-09-14 16:01:05 +08:00
|
|
|
|
|
|
|
d = ber_memalloc( sizeof(DIR) );
|
|
|
|
if ( !d )
|
|
|
|
return NULL;
|
|
|
|
d->dir = h;
|
|
|
|
d->data.d_name = d->buf;
|
|
|
|
d->first = 1;
|
|
|
|
strcpy(d->data.d_name, data.cFileName);
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
struct dirent *readdir(DIR *dir)
|
|
|
|
{
|
|
|
|
WIN32_FIND_DATA data;
|
|
|
|
|
|
|
|
if (dir->first) {
|
|
|
|
dir->first = 0;
|
|
|
|
} else {
|
|
|
|
if (!FindNextFile(dir->dir, &data))
|
|
|
|
return NULL;
|
|
|
|
strcpy(dir->data.d_name, data.cFileName);
|
|
|
|
}
|
|
|
|
return &dir->data;
|
|
|
|
}
|
2009-04-11 12:22:27 +08:00
|
|
|
int closedir(DIR *dir)
|
2006-09-14 16:01:05 +08:00
|
|
|
{
|
2014-10-11 03:15:48 +08:00
|
|
|
(void) FindClose(dir->dir);
|
2006-09-14 16:01:05 +08:00
|
|
|
ber_memfree(dir);
|
2014-10-11 03:15:48 +08:00
|
|
|
return 0;
|
2006-09-14 16:01:05 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2005-11-14 23:40:01 +08:00
|
|
|
/*
|
2005-11-14 06:26:53 +08:00
|
|
|
* Memory Reverse Search
|
|
|
|
*/
|
|
|
|
void *
|
2009-11-21 04:43:50 +08:00
|
|
|
(lutil_memrchr)(const void *b, int c, size_t n)
|
2005-11-14 06:26:53 +08:00
|
|
|
{
|
|
|
|
if (n != 0) {
|
2005-11-14 23:40:01 +08:00
|
|
|
const unsigned char *s, *bb = b, cc = c;
|
2005-11-14 06:26:53 +08:00
|
|
|
|
2005-11-14 23:40:01 +08:00
|
|
|
for ( s = bb + n; s > bb; ) {
|
|
|
|
if ( *--s == cc ) {
|
|
|
|
return (void *) s;
|
2005-11-14 06:26:53 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
2005-11-14 23:40:01 +08:00
|
|
|
}
|
2005-11-23 20:46:33 +08:00
|
|
|
|
|
|
|
int
|
2005-11-24 09:10:05 +08:00
|
|
|
lutil_atoix( int *v, const char *s, int x )
|
2005-11-23 20:46:33 +08:00
|
|
|
{
|
|
|
|
char *next;
|
|
|
|
long i;
|
|
|
|
|
|
|
|
assert( s != NULL );
|
|
|
|
assert( v != NULL );
|
|
|
|
|
2005-11-24 09:10:05 +08:00
|
|
|
i = strtol( s, &next, x );
|
2005-11-23 20:46:33 +08:00
|
|
|
if ( next == s || next[ 0 ] != '\0' ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (long)(int)i != i ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*v = (int)i;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-11-24 09:10:05 +08:00
|
|
|
lutil_atoux( unsigned *v, const char *s, int x )
|
2005-11-23 20:46:33 +08:00
|
|
|
{
|
|
|
|
char *next;
|
|
|
|
unsigned long u;
|
|
|
|
|
|
|
|
assert( s != NULL );
|
|
|
|
assert( v != NULL );
|
|
|
|
|
2006-04-25 23:46:44 +08:00
|
|
|
/* strtoul() has an odd interface */
|
|
|
|
if ( s[ 0 ] == '-' ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-11-24 09:10:05 +08:00
|
|
|
u = strtoul( s, &next, x );
|
2005-11-23 20:46:33 +08:00
|
|
|
if ( next == s || next[ 0 ] != '\0' ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (unsigned long)(unsigned)u != u ) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*v = u;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-11-24 09:10:05 +08:00
|
|
|
lutil_atolx( long *v, const char *s, int x )
|
2005-11-23 20:46:33 +08:00
|
|
|
{
|
2010-08-13 07:32:37 +08:00
|
|
|
char *next;
|
|
|
|
long l;
|
|
|
|
int save_errno;
|
2005-11-23 20:46:33 +08:00
|
|
|
|
|
|
|
assert( s != NULL );
|
|
|
|
assert( v != NULL );
|
|
|
|
|
2010-08-13 07:32:37 +08:00
|
|
|
if ( isspace( s[ 0 ] ) ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
errno = 0;
|
2005-11-24 09:10:05 +08:00
|
|
|
l = strtol( s, &next, x );
|
2010-08-13 07:32:37 +08:00
|
|
|
save_errno = errno;
|
2005-11-23 20:46:33 +08:00
|
|
|
if ( next == s || next[ 0 ] != '\0' ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-08-13 07:32:37 +08:00
|
|
|
if ( ( l == LONG_MIN || l == LONG_MAX ) && save_errno != 0 ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-11-23 20:46:33 +08:00
|
|
|
*v = l;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2005-11-24 09:10:05 +08:00
|
|
|
lutil_atoulx( unsigned long *v, const char *s, int x )
|
2005-11-23 20:46:33 +08:00
|
|
|
{
|
2010-08-13 07:32:37 +08:00
|
|
|
char *next;
|
|
|
|
unsigned long ul;
|
|
|
|
int save_errno;
|
2005-11-23 20:46:33 +08:00
|
|
|
|
|
|
|
assert( s != NULL );
|
|
|
|
assert( v != NULL );
|
|
|
|
|
2006-04-25 23:46:44 +08:00
|
|
|
/* strtoul() has an odd interface */
|
2010-08-13 07:32:37 +08:00
|
|
|
if ( s[ 0 ] == '-' || isspace( s[ 0 ] ) ) {
|
2006-04-25 23:46:44 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-08-13 07:32:37 +08:00
|
|
|
errno = 0;
|
2005-11-24 09:10:05 +08:00
|
|
|
ul = strtoul( s, &next, x );
|
2010-08-13 07:32:37 +08:00
|
|
|
save_errno = errno;
|
2005-11-23 20:46:33 +08:00
|
|
|
if ( next == s || next[ 0 ] != '\0' ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-08-13 07:32:37 +08:00
|
|
|
if ( ( ul == 0 || ul == ULONG_MAX ) && save_errno != 0 ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-11-23 20:46:33 +08:00
|
|
|
*v = ul;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-08-13 07:32:37 +08:00
|
|
|
#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 */
|
|
|
|
|
2007-10-01 14:50:19 +08:00
|
|
|
/* Multiply an integer by 100000000 and add new */
|
2007-11-23 20:47:53 +08:00
|
|
|
typedef struct lutil_int_decnum {
|
2007-10-01 14:50:19 +08:00
|
|
|
unsigned char *buf;
|
|
|
|
int bufsiz;
|
|
|
|
int beg;
|
|
|
|
int len;
|
2007-11-23 20:47:53 +08:00
|
|
|
} lutil_int_decnum;
|
2007-10-01 14:50:19 +08:00
|
|
|
|
|
|
|
#define FACTOR1 (100000000&0xffff)
|
|
|
|
#define FACTOR2 (100000000>>16)
|
|
|
|
|
|
|
|
static void
|
2007-11-23 20:47:53 +08:00
|
|
|
scale( int new, lutil_int_decnum *prev, unsigned char *tmp )
|
2007-10-01 14:50:19 +08:00
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
unsigned char *in = prev->buf+prev->beg;
|
|
|
|
unsigned int part;
|
|
|
|
unsigned char *out = tmp + prev->bufsiz - prev->len;
|
|
|
|
|
|
|
|
memset( tmp, 0, prev->bufsiz );
|
|
|
|
if ( prev->len ) {
|
|
|
|
for ( i = prev->len-1; i>=0; i-- ) {
|
|
|
|
part = in[i] * FACTOR1;
|
|
|
|
for ( j = i; part; j-- ) {
|
|
|
|
part += out[j];
|
|
|
|
out[j] = part & 0xff;
|
|
|
|
part >>= 8;
|
|
|
|
}
|
|
|
|
part = in[i] * FACTOR2;
|
|
|
|
for ( j = i-2; part; j-- ) {
|
|
|
|
part += out[j];
|
|
|
|
out[j] = part & 0xff;
|
|
|
|
part >>= 8;
|
|
|
|
}
|
|
|
|
}
|
2007-12-01 19:14:40 +08:00
|
|
|
j++;
|
|
|
|
prev->beg += j;
|
|
|
|
prev->len -= j;
|
2007-10-01 14:50:19 +08:00
|
|
|
}
|
|
|
|
|
2007-12-01 02:32:42 +08:00
|
|
|
out = tmp + prev->bufsiz;
|
2007-12-01 06:26:23 +08:00
|
|
|
i = 0;
|
|
|
|
do {
|
|
|
|
i--;
|
2007-10-01 14:50:19 +08:00
|
|
|
new += out[i];
|
|
|
|
out[i] = new & 0xff;
|
|
|
|
new >>= 8;
|
2007-12-01 06:26:23 +08:00
|
|
|
} while ( new );
|
2007-12-01 19:14:40 +08:00
|
|
|
i = -i;
|
|
|
|
if ( prev->len < i ) {
|
|
|
|
prev->beg = prev->bufsiz - i;
|
|
|
|
prev->len = i;
|
|
|
|
}
|
2007-10-01 14:50:19 +08:00
|
|
|
AC_MEMCPY( prev->buf+prev->beg, tmp+prev->beg, prev->len );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert unlimited length decimal or hex string to binary.
|
|
|
|
* Output buffer must be provided, bv_len must indicate buffer size
|
|
|
|
* Hex input can be "0x1234" or "'1234'H"
|
2007-11-21 09:49:09 +08:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* any hex input.
|
2007-10-01 14:50:19 +08:00
|
|
|
*/
|
|
|
|
int
|
2007-12-03 03:11:41 +08:00
|
|
|
lutil_str2bin( struct berval *in, struct berval *out, void *ctx )
|
2007-10-01 14:50:19 +08:00
|
|
|
{
|
2012-02-20 10:10:16 +08:00
|
|
|
char *pin, *pout;
|
2007-10-01 14:50:19 +08:00
|
|
|
char *end;
|
|
|
|
int i, chunk, len, rc = 0, hex = 0;
|
|
|
|
if ( !out || !out->bv_val || out->bv_len < in->bv_len )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pout = out->bv_val;
|
|
|
|
/* Leading "0x" for hex input */
|
|
|
|
if ( in->bv_len > 2 && in->bv_val[0] == '0' &&
|
2007-10-06 23:40:55 +08:00
|
|
|
( in->bv_val[1] == 'x' || in->bv_val[1] == 'X' ) )
|
|
|
|
{
|
2007-10-01 14:50:19 +08:00
|
|
|
len = in->bv_len - 2;
|
|
|
|
pin = in->bv_val + 2;
|
|
|
|
hex = 1;
|
|
|
|
} else if ( in->bv_len > 3 && in->bv_val[0] == '\'' &&
|
2007-10-06 23:40:55 +08:00
|
|
|
in->bv_val[in->bv_len-2] == '\'' &&
|
|
|
|
in->bv_val[in->bv_len-1] == 'H' )
|
|
|
|
{
|
2007-10-01 14:50:19 +08:00
|
|
|
len = in->bv_len - 3;
|
|
|
|
pin = in->bv_val + 1;
|
|
|
|
hex = 1;
|
|
|
|
}
|
|
|
|
if ( hex ) {
|
|
|
|
#define HEXMAX (2 * sizeof(long))
|
2008-09-14 02:46:50 +08:00
|
|
|
unsigned long l;
|
2012-02-20 10:10:16 +08:00
|
|
|
char tbuf[HEXMAX+1];
|
|
|
|
|
2007-10-01 14:50:19 +08:00
|
|
|
/* Convert a longword at a time, but handle leading
|
|
|
|
* odd bytes first
|
|
|
|
*/
|
2008-09-14 02:46:50 +08:00
|
|
|
chunk = len % HEXMAX;
|
2007-10-01 14:50:19 +08:00
|
|
|
if ( !chunk )
|
|
|
|
chunk = HEXMAX;
|
|
|
|
|
|
|
|
while ( len ) {
|
2008-09-14 02:46:50 +08:00
|
|
|
int ochunk;
|
2012-02-20 10:10:16 +08:00
|
|
|
memcpy( tbuf, pin, chunk );
|
|
|
|
tbuf[chunk] = '\0';
|
2007-10-01 14:50:19 +08:00
|
|
|
errno = 0;
|
2012-02-20 10:10:16 +08:00
|
|
|
l = strtoul( tbuf, &end, 16 );
|
2007-10-01 14:50:19 +08:00
|
|
|
if ( errno )
|
|
|
|
return -1;
|
2008-09-14 02:46:50 +08:00
|
|
|
ochunk = (chunk + 1)/2;
|
|
|
|
for ( i = ochunk - 1; i >= 0; i-- ) {
|
2007-10-01 14:50:19 +08:00
|
|
|
pout[i] = l & 0xff;
|
|
|
|
l >>= 8;
|
|
|
|
}
|
|
|
|
pin += chunk;
|
2008-09-14 02:46:50 +08:00
|
|
|
pout += ochunk;
|
2007-10-01 14:50:19 +08:00
|
|
|
len -= chunk;
|
|
|
|
chunk = HEXMAX;
|
|
|
|
}
|
2008-09-14 02:46:50 +08:00
|
|
|
out->bv_len = pout - out->bv_val;
|
2007-10-01 14:50:19 +08:00
|
|
|
} else {
|
|
|
|
/* Decimal */
|
2012-02-20 10:10:16 +08:00
|
|
|
#define DECMAX 8 /* 8 digits at a time */
|
2007-10-01 14:50:19 +08:00
|
|
|
char tmpbuf[64], *tmp;
|
2007-11-23 20:47:53 +08:00
|
|
|
lutil_int_decnum num;
|
2007-11-21 09:49:09 +08:00
|
|
|
int neg = 0;
|
2008-09-14 02:46:50 +08:00
|
|
|
long l;
|
2012-02-20 10:10:16 +08:00
|
|
|
char tbuf[DECMAX+1];
|
2007-10-01 14:50:19 +08:00
|
|
|
|
|
|
|
len = in->bv_len;
|
|
|
|
pin = in->bv_val;
|
2007-11-23 20:47:53 +08:00
|
|
|
num.buf = (unsigned char *)out->bv_val;
|
2007-10-01 14:50:19 +08:00
|
|
|
num.bufsiz = out->bv_len;
|
|
|
|
num.beg = num.bufsiz-1;
|
|
|
|
num.len = 0;
|
2007-11-21 09:49:09 +08:00
|
|
|
if ( pin[0] == '-' ) {
|
2007-12-02 03:18:43 +08:00
|
|
|
neg = 0xff;
|
2007-11-21 09:49:09 +08:00
|
|
|
len--;
|
|
|
|
pin++;
|
|
|
|
}
|
2007-10-01 14:50:19 +08:00
|
|
|
|
2007-12-02 04:28:18 +08:00
|
|
|
/* tmp must be at least as large as outbuf */
|
|
|
|
if ( out->bv_len > sizeof(tmpbuf)) {
|
2007-12-03 03:11:41 +08:00
|
|
|
tmp = ber_memalloc_x( out->bv_len, ctx );
|
2007-10-01 14:50:19 +08:00
|
|
|
} else {
|
|
|
|
tmp = tmpbuf;
|
|
|
|
}
|
|
|
|
chunk = len & (DECMAX-1);
|
|
|
|
if ( !chunk )
|
|
|
|
chunk = DECMAX;
|
|
|
|
|
|
|
|
while ( len ) {
|
2012-02-20 10:10:16 +08:00
|
|
|
memcpy( tbuf, pin, chunk );
|
|
|
|
tbuf[chunk] = '\0';
|
2007-10-01 14:50:19 +08:00
|
|
|
errno = 0;
|
2012-02-20 10:10:16 +08:00
|
|
|
l = strtol( tbuf, &end, 10 );
|
2007-10-01 14:50:19 +08:00
|
|
|
if ( errno ) {
|
|
|
|
rc = -1;
|
|
|
|
goto decfail;
|
|
|
|
}
|
2007-11-23 20:47:53 +08:00
|
|
|
scale( l, &num, (unsigned char *)tmp );
|
2007-10-01 14:50:19 +08:00
|
|
|
pin += chunk;
|
|
|
|
len -= chunk;
|
2007-11-21 02:14:17 +08:00
|
|
|
chunk = DECMAX;
|
2007-10-01 14:50:19 +08:00
|
|
|
}
|
2007-11-21 09:49:09 +08:00
|
|
|
/* Negate the result */
|
|
|
|
if ( neg ) {
|
|
|
|
unsigned char *ptr;
|
|
|
|
|
|
|
|
ptr = num.buf+num.beg;
|
|
|
|
|
|
|
|
/* flip all bits */
|
|
|
|
for ( i=0; i<num.len; i++ )
|
|
|
|
ptr[i] ^= 0xff;
|
|
|
|
|
2007-12-02 03:18:43 +08:00
|
|
|
/* 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 ) {
|
2007-11-21 09:49:09 +08:00
|
|
|
num.beg--;
|
|
|
|
num.len++;
|
2007-12-02 03:18:43 +08:00
|
|
|
num.buf[num.beg] = neg;
|
2007-11-21 09:49:09 +08:00
|
|
|
}
|
2007-10-01 14:50:19 +08:00
|
|
|
if ( num.beg )
|
|
|
|
AC_MEMCPY( num.buf, num.buf+num.beg, num.len );
|
|
|
|
out->bv_len = num.len;
|
|
|
|
decfail:
|
|
|
|
if ( tmp != tmpbuf ) {
|
2007-12-03 03:11:41 +08:00
|
|
|
ber_memfree_x( tmp, ctx );
|
2007-10-01 14:50:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2005-11-23 20:46:33 +08:00
|
|
|
static char time_unit[] = "dhms";
|
|
|
|
|
2007-02-03 07:10:30 +08:00
|
|
|
/* Used to parse and unparse time intervals, not timestamps */
|
2005-11-23 20:46:33 +08:00
|
|
|
int
|
|
|
|
lutil_parse_time(
|
|
|
|
const char *in,
|
|
|
|
unsigned long *tp )
|
|
|
|
{
|
|
|
|
unsigned long t = 0;
|
|
|
|
char *s,
|
|
|
|
*next;
|
|
|
|
int sofar = -1,
|
|
|
|
scale[] = { 86400, 3600, 60, 1 };
|
|
|
|
|
|
|
|
*tp = 0;
|
|
|
|
|
|
|
|
for ( s = (char *)in; s[ 0 ] != '\0'; ) {
|
|
|
|
unsigned long u;
|
|
|
|
char *what;
|
|
|
|
|
2006-04-25 23:46:44 +08:00
|
|
|
/* strtoul() has an odd interface */
|
|
|
|
if ( s[ 0 ] == '-' ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2005-11-23 20:46:33 +08:00
|
|
|
u = strtoul( s, &next, 10 );
|
|
|
|
if ( next == s ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( next[ 0 ] == '\0' ) {
|
|
|
|
/* assume seconds */
|
|
|
|
t += u;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
what = strchr( time_unit, next[ 0 ] );
|
|
|
|
if ( what == NULL ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( what - time_unit <= sofar ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
sofar = what - time_unit;
|
|
|
|
t += u * scale[ sofar ];
|
|
|
|
|
|
|
|
s = &next[ 1 ];
|
|
|
|
}
|
|
|
|
|
|
|
|
*tp = t;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
lutil_unparse_time(
|
|
|
|
char *buf,
|
|
|
|
size_t buflen,
|
|
|
|
unsigned long t )
|
|
|
|
{
|
|
|
|
int len, i;
|
|
|
|
unsigned long v[ 4 ];
|
2006-01-02 16:14:55 +08:00
|
|
|
char *ptr = buf;
|
2005-11-23 20:46:33 +08:00
|
|
|
|
|
|
|
v[ 0 ] = t/86400;
|
|
|
|
v[ 1 ] = (t%86400)/3600;
|
|
|
|
v[ 2 ] = (t%3600)/60;
|
|
|
|
v[ 3 ] = t%60;
|
|
|
|
|
|
|
|
for ( i = 0; i < 4; i++ ) {
|
2006-01-02 16:14:55 +08:00
|
|
|
if ( v[i] > 0 || ( i == 3 && ptr == buf ) ) {
|
|
|
|
len = snprintf( ptr, buflen, "%lu%c", v[ i ], time_unit[ i ] );
|
2005-11-23 20:46:33 +08:00
|
|
|
if ( len < 0 || (unsigned)len >= buflen ) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
buflen -= len;
|
2006-01-02 16:14:55 +08:00
|
|
|
ptr += len;
|
2005-11-23 20:46:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-12-15 23:23:23 +08:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
2008-10-13 16:13:27 +08:00
|
|
|
if ( (unsigned) ret >= bufsize ) {
|
2007-12-15 23:23:23 +08:00
|
|
|
if ( next ) {
|
|
|
|
*next = &buf[ bufsize - 1 ];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( next ) {
|
|
|
|
*next = &buf[ ret ];
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|