xref: /netbsd-src/external/bsd/openldap/dist/libraries/liblutil/utils.c (revision aad9773e38ed2370a628a6416e098f9008fc10a7)
1 /*	$NetBSD: utils.c,v 1.1.1.4 2014/05/28 09:58:45 tron Exp $	*/
2 
3 /* $OpenLDAP$ */
4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5  *
6  * Copyright 1998-2014 The OpenLDAP Foundation.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 
18 #include "portable.h"
19 
20 #include <limits.h>
21 #include <stdio.h>
22 #include <ac/stdlib.h>
23 #include <ac/stdarg.h>
24 #include <ac/string.h>
25 #include <ac/ctype.h>
26 #include <ac/unistd.h>
27 #include <ac/time.h>
28 #include <ac/errno.h>
29 #ifdef HAVE_IO_H
30 #include <io.h>
31 #endif
32 #ifdef HAVE_FCNTL_H
33 #include <fcntl.h>
34 #endif
35 #ifdef _WIN32
36 #include <windows.h>
37 #endif
38 
39 #include "lutil.h"
40 #include "ldap_defaults.h"
41 #include "ldap_pvt.h"
42 #include "lber_pvt.h"
43 
44 #ifdef HAVE_EBCDIC
45 int _trans_argv = 1;
46 #endif
47 
48 #ifdef _WIN32
49 /* Some Windows versions accept both forward and backslashes in
50  * directory paths, but we always use backslashes when generating
51  * and parsing...
52  */
53 void lutil_slashpath( char *path )
54 {
55 	char *c, *p;
56 
57 	p = path;
58 	while (( c=strchr( p, '/' ))) {
59 		*c++ = '\\';
60 		p = c;
61 	}
62 }
63 #endif
64 
65 char* lutil_progname( const char* name, int argc, char *argv[] )
66 {
67 	char *progname;
68 
69 	if(argc == 0) {
70 		return (char *)name;
71 	}
72 
73 #ifdef HAVE_EBCDIC
74 	if (_trans_argv) {
75 		int i;
76 		for (i=0; i<argc; i++) __etoa(argv[i]);
77 		_trans_argv = 0;
78 	}
79 #endif
80 	LUTIL_SLASHPATH( argv[0] );
81 	progname = strrchr ( argv[0], *LDAP_DIRSEP );
82 	progname = progname ? &progname[1] : argv[0];
83 #ifdef _WIN32
84 	{
85 		size_t len = strlen( progname );
86 		if ( len > 4 && strcasecmp( &progname[len - 4], ".exe" ) == 0 )
87 			progname[len - 4] = '\0';
88 	}
89 #endif
90 	return progname;
91 }
92 
93 #if 0
94 size_t lutil_gentime( char *s, size_t smax, const struct tm *tm )
95 {
96 	size_t ret;
97 #ifdef HAVE_EBCDIC
98 /* We've been compiling in ASCII so far, but we want EBCDIC now since
99  * strftime only understands EBCDIC input.
100  */
101 #pragma convlit(suspend)
102 #endif
103 	ret = strftime( s, smax, "%Y%m%d%H%M%SZ", tm );
104 #ifdef HAVE_EBCDIC
105 #pragma convlit(resume)
106 	__etoa( s );
107 #endif
108 	return ret;
109 }
110 #endif
111 
112 size_t lutil_localtime( char *s, size_t smax, const struct tm *tm, long delta )
113 {
114 	size_t	ret;
115 	char	*p;
116 
117 	if ( smax < 16 ) {	/* YYYYmmddHHMMSSZ */
118 		return 0;
119 	}
120 
121 #ifdef HAVE_EBCDIC
122 /* We've been compiling in ASCII so far, but we want EBCDIC now since
123  * strftime only understands EBCDIC input.
124  */
125 #pragma convlit(suspend)
126 #endif
127 	ret = strftime( s, smax, "%Y%m%d%H%M%SZ", tm );
128 #ifdef HAVE_EBCDIC
129 #pragma convlit(resume)
130 	__etoa( s );
131 #endif
132 	if ( delta == 0 || ret == 0 ) {
133 		return ret;
134 	}
135 
136 	if ( smax < 20 ) {	/* YYYYmmddHHMMSS+HHMM */
137 		return 0;
138 	}
139 
140 	p = s + 14;
141 
142 	if ( delta < 0 ) {
143 		p[ 0 ] = '-';
144 		delta = -delta;
145 	} else {
146 		p[ 0 ] = '+';
147 	}
148 	p++;
149 
150 	snprintf( p, smax - 15, "%02ld%02ld", delta / 3600,
151 			( delta % 3600 ) / 60 );
152 
153 	return ret + 4;
154 }
155 
156 int lutil_tm2time( struct lutil_tm *tm, struct lutil_timet *tt )
157 {
158 	static int moffset[12] = {
159 		0, 31, 59, 90, 120,
160 		151, 181, 212, 243,
161 		273, 304, 334 };
162 	int sec;
163 
164 	tt->tt_usec = tm->tm_usec;
165 
166 	/* special case 0000/01/01+00:00:00 is returned as zero */
167 	if ( tm->tm_year == -1900 && tm->tm_mon == 0 && tm->tm_mday == 1 &&
168 		tm->tm_hour == 0 && tm->tm_min == 0 && tm->tm_sec == 0 ) {
169 		tt->tt_sec = 0;
170 		tt->tt_gsec = 0;
171 		return 0;
172 	}
173 
174 	/* tm->tm_year is years since 1900 */
175 	/* calculate days from years since 1970 (epoch) */
176 	tt->tt_sec = tm->tm_year - 70;
177 	tt->tt_sec *= 365L;
178 
179 	/* count leap days in preceding years */
180 	tt->tt_sec += ((tm->tm_year -69) >> 2);
181 
182 	/* calculate days from months */
183 	tt->tt_sec += moffset[tm->tm_mon];
184 
185 	/* add in this year's leap day, if any */
186 	if (((tm->tm_year & 3) == 0) && (tm->tm_mon > 1)) {
187 		tt->tt_sec ++;
188 	}
189 
190 	/* add in days in this month */
191 	tt->tt_sec += (tm->tm_mday - 1);
192 
193 	/* this function can handle a range of about 17408 years... */
194 	/* 86400 seconds in a day, divided by 128 = 675 */
195 	tt->tt_sec *= 675;
196 
197 	/* move high 7 bits into tt_gsec */
198 	tt->tt_gsec = tt->tt_sec >> 25;
199 	tt->tt_sec -= tt->tt_gsec << 25;
200 
201 	/* get hours */
202 	sec = tm->tm_hour;
203 
204 	/* convert to minutes */
205 	sec *= 60L;
206 	sec += tm->tm_min;
207 
208 	/* convert to seconds */
209 	sec *= 60L;
210 	sec += tm->tm_sec;
211 
212 	/* add remaining seconds */
213 	tt->tt_sec <<= 7;
214 	tt->tt_sec += sec;
215 
216 	/* return success */
217 	return 0;
218 }
219 
220 int lutil_parsetime( char *atm, struct lutil_tm *tm )
221 {
222 	while (atm && tm) {
223 		char *ptr = atm;
224 		unsigned i, fracs;
225 
226 		/* Is the stamp reasonably long? */
227 		for (i=0; isdigit((unsigned char) atm[i]); i++);
228 		if (i < sizeof("00000101000000")-1)
229 			break;
230 
231 		/*
232 		 * parse the time into a struct tm
233 		 */
234 		/* 4 digit year to year - 1900 */
235 		tm->tm_year = *ptr++ - '0';
236 		tm->tm_year *= 10; tm->tm_year += *ptr++ - '0';
237 		tm->tm_year *= 10; tm->tm_year += *ptr++ - '0';
238 		tm->tm_year *= 10; tm->tm_year += *ptr++ - '0';
239 		tm->tm_year -= 1900;
240 		/* month 01-12 to 0-11 */
241 		tm->tm_mon = *ptr++ - '0';
242 		tm->tm_mon *=10; tm->tm_mon += *ptr++ - '0';
243 		if (tm->tm_mon < 1 || tm->tm_mon > 12) break;
244 		tm->tm_mon--;
245 
246 		/* day of month 01-31 */
247 		tm->tm_mday = *ptr++ - '0';
248 		tm->tm_mday *=10; tm->tm_mday += *ptr++ - '0';
249 		if (tm->tm_mday < 1 || tm->tm_mday > 31) break;
250 
251 		/* Hour 00-23 */
252 		tm->tm_hour = *ptr++ - '0';
253 		tm->tm_hour *=10; tm->tm_hour += *ptr++ - '0';
254 		if (tm->tm_hour < 0 || tm->tm_hour > 23) break;
255 
256 		/* Minute 00-59 */
257 		tm->tm_min = *ptr++ - '0';
258 		tm->tm_min *=10; tm->tm_min += *ptr++ - '0';
259 		if (tm->tm_min < 0 || tm->tm_min > 59) break;
260 
261 		/* Second 00-61 */
262 		tm->tm_sec = *ptr++ - '0';
263 		tm->tm_sec *=10; tm->tm_sec += *ptr++ - '0';
264 		if (tm->tm_sec < 0 || tm->tm_sec > 61) break;
265 
266 		/* Fractions of seconds */
267 		if ( *ptr == '.' ) {
268 			ptr++;
269 			for (i = 0, fracs = 0; isdigit((unsigned char) *ptr); ) {
270 				i*=10; i+= *ptr++ - '0';
271 				fracs++;
272 			}
273 			tm->tm_usec = i;
274 			if (i) {
275 				for (i = fracs; i<6; i++)
276 					tm->tm_usec *= 10;
277 			}
278 		}
279 
280 		/* Must be UTC */
281 		if (*ptr != 'Z') break;
282 
283 		return 0;
284 	}
285 	return -1;
286 }
287 
288 /* strcopy is like strcpy except it returns a pointer to the trailing NUL of
289  * the result string. This allows fast construction of catenated strings
290  * without the overhead of strlen/strcat.
291  */
292 char *
293 lutil_strcopy(
294 	char *a,
295 	const char *b
296 )
297 {
298 	if (!a || !b)
299 		return a;
300 
301 	while ((*a++ = *b++)) ;
302 	return a-1;
303 }
304 
305 /* strncopy is like strcpy except it returns a pointer to the trailing NUL of
306  * the result string. This allows fast construction of catenated strings
307  * without the overhead of strlen/strcat.
308  */
309 char *
310 lutil_strncopy(
311 	char *a,
312 	const char *b,
313 	size_t n
314 )
315 {
316 	if (!a || !b || n == 0)
317 		return a;
318 
319 	while ((*a++ = *b++) && n-- > 0) ;
320 	return a-1;
321 }
322 
323 /* memcopy is like memcpy except it returns a pointer to the byte past
324  * the end of the result buffer, set to NULL. This allows fast construction
325  * of catenated buffers.  Provided for API consistency with lutil_str*copy().
326  */
327 char *
328 lutil_memcopy(
329 	char *a,
330 	const char *b,
331 	size_t n
332 )
333 {
334 	AC_MEMCPY(a, b, n);
335 	return a + n;
336 }
337 
338 #ifndef HAVE_MKSTEMP
339 int mkstemp( char * template )
340 {
341 #ifdef HAVE_MKTEMP
342 	return open ( mktemp ( template ), O_RDWR|O_CREAT|O_EXCL, 0600 );
343 #else
344 	return -1;
345 #endif
346 }
347 #endif
348 
349 #ifdef _MSC_VER
350 /* Equivalent of MS CRT's _dosmaperr().
351  * @param lastError[in] Result of GetLastError().
352  */
353 static errno_t win2errno(DWORD lastError)
354 {
355 	const struct {
356 		DWORD   windows_code;
357 		errno_t errno_code;
358 	} WIN2ERRNO_TABLE[] = {
359 		{ ERROR_SUCCESS, 0 },
360 		{ ERROR_FILE_NOT_FOUND, ENOENT },
361 		{ ERROR_PATH_NOT_FOUND, ENOENT },
362 		{ ERROR_TOO_MANY_OPEN_FILES, EMFILE },
363 		{ ERROR_ACCESS_DENIED, EACCES },
364 		{ ERROR_INVALID_HANDLE, EBADF },
365 		{ ERROR_NOT_ENOUGH_MEMORY, ENOMEM },
366 		{ ERROR_LOCK_VIOLATION, EACCES },
367 		{ ERROR_FILE_EXISTS, EEXIST },
368 		{ ERROR_INVALID_PARAMETER, EINVAL },
369 		{ ERROR_FILENAME_EXCED_RANGE, ENAMETOOLONG },
370 	};
371 	const unsigned int WIN2ERRNO_TABLE_SIZE = sizeof(WIN2ERRNO_TABLE) /
372 sizeof(WIN2ERRNO_TABLE[0]);
373 	const errno_t DEFAULT_ERRNO_ERROR = -1;
374 	unsigned int i;
375 
376 	for (i = 0; i < WIN2ERRNO_TABLE_SIZE; ++i) {
377 		if (WIN2ERRNO_TABLE[i].windows_code == lastError) {
378 			return WIN2ERRNO_TABLE[i].errno_code;
379 		}
380 	}
381 	return DEFAULT_ERRNO_ERROR;
382 }
383 
384 struct dirent {
385 	char *d_name;
386 };
387 typedef struct DIR {
388 	HANDLE dir;
389 	struct dirent data;
390 	int first;
391 	char buf[MAX_PATH+1];
392 } DIR;
393 DIR *opendir( char *path )
394 {
395 	char tmp[32768];
396 	int len = strlen(path);
397 	DIR *d;
398 	HANDLE h;
399 	WIN32_FIND_DATA data;
400 
401 	if (len+3 >= sizeof(tmp)) {
402 		errno = ENAMETOOLONG;
403 		return NULL;
404 	}
405 
406 	strcpy(tmp, path);
407 	tmp[len++] = '\\';
408 	tmp[len++] = '*';
409 	tmp[len] = '\0';
410 
411 	h = FindFirstFile( tmp, &data );
412 
413 	if ( h == INVALID_HANDLE_VALUE ) {
414 		errno = win2errno( GetLastError());
415 		return NULL;
416 	}
417 
418 	d = ber_memalloc( sizeof(DIR) );
419 	if ( !d )
420 		return NULL;
421 	d->dir = h;
422 	d->data.d_name = d->buf;
423 	d->first = 1;
424 	strcpy(d->data.d_name, data.cFileName);
425 	return d;
426 }
427 struct dirent *readdir(DIR *dir)
428 {
429 	WIN32_FIND_DATA data;
430 
431 	if (dir->first) {
432 		dir->first = 0;
433 	} else {
434 		if (!FindNextFile(dir->dir, &data))
435 			return NULL;
436 		strcpy(dir->data.d_name, data.cFileName);
437 	}
438 	return &dir->data;
439 }
440 int closedir(DIR *dir)
441 {
442 	FindClose(dir->dir);
443 	ber_memfree(dir);
444 }
445 #endif
446 
447 /*
448  * Memory Reverse Search
449  */
450 void *
451 (lutil_memrchr)(const void *b, int c, size_t n)
452 {
453 	if (n != 0) {
454 		const unsigned char *s, *bb = b, cc = c;
455 
456 		for ( s = bb + n; s > bb; ) {
457 			if ( *--s == cc ) {
458 				return (void *) s;
459 			}
460 		}
461 	}
462 
463 	return NULL;
464 }
465 
466 int
467 lutil_atoix( int *v, const char *s, int x )
468 {
469 	char		*next;
470 	long		i;
471 
472 	assert( s != NULL );
473 	assert( v != NULL );
474 
475 	i = strtol( s, &next, x );
476 	if ( next == s || next[ 0 ] != '\0' ) {
477 		return -1;
478 	}
479 
480 	if ( (long)(int)i != i ) {
481 		return 1;
482 	}
483 
484 	*v = (int)i;
485 
486 	return 0;
487 }
488 
489 int
490 lutil_atoux( unsigned *v, const char *s, int x )
491 {
492 	char		*next;
493 	unsigned long	u;
494 
495 	assert( s != NULL );
496 	assert( v != NULL );
497 
498 	/* strtoul() has an odd interface */
499 	if ( s[ 0 ] == '-' ) {
500 		return -1;
501 	}
502 
503 	u = strtoul( s, &next, x );
504 	if ( next == s || next[ 0 ] != '\0' ) {
505 		return -1;
506 	}
507 
508 	if ( (unsigned long)(unsigned)u != u ) {
509 		return 1;
510 	}
511 
512 	*v = u;
513 
514 	return 0;
515 }
516 
517 int
518 lutil_atolx( long *v, const char *s, int x )
519 {
520 	char *next;
521 	long l;
522 	int save_errno;
523 
524 	assert( s != NULL );
525 	assert( v != NULL );
526 
527 	if ( isspace( s[ 0 ] ) ) {
528 		return -1;
529 	}
530 
531 	errno = 0;
532 	l = strtol( s, &next, x );
533 	save_errno = errno;
534 	if ( next == s || next[ 0 ] != '\0' ) {
535 		return -1;
536 	}
537 
538 	if ( ( l == LONG_MIN || l == LONG_MAX ) && save_errno != 0 ) {
539 		return -1;
540 	}
541 
542 	*v = l;
543 
544 	return 0;
545 }
546 
547 int
548 lutil_atoulx( unsigned long *v, const char *s, int x )
549 {
550 	char *next;
551 	unsigned long ul;
552 	int save_errno;
553 
554 	assert( s != NULL );
555 	assert( v != NULL );
556 
557 	/* strtoul() has an odd interface */
558 	if ( s[ 0 ] == '-' || isspace( s[ 0 ] ) ) {
559 		return -1;
560 	}
561 
562 	errno = 0;
563 	ul = strtoul( s, &next, x );
564 	save_errno = errno;
565 	if ( next == s || next[ 0 ] != '\0' ) {
566 		return -1;
567 	}
568 
569 	if ( ( ul == 0 || ul == ULONG_MAX ) && save_errno != 0 ) {
570 		return -1;
571 	}
572 
573 	*v = ul;
574 
575 	return 0;
576 }
577 
578 #ifdef HAVE_LONG_LONG
579 #if defined(HAVE_STRTOLL) || defined(HAVE_STRTOQ)
580 int
581 lutil_atollx( long long *v, const char *s, int x )
582 {
583 	char *next;
584 	long long ll;
585 	int save_errno;
586 
587 	assert( s != NULL );
588 	assert( v != NULL );
589 
590 	if ( isspace( s[ 0 ] ) ) {
591 		return -1;
592 	}
593 
594 	errno = 0;
595 #ifdef HAVE_STRTOLL
596 	ll = strtoll( s, &next, x );
597 #else /* HAVE_STRTOQ */
598 	ll = (unsigned long long)strtoq( s, &next, x );
599 #endif /* HAVE_STRTOQ */
600 	save_errno = errno;
601 	if ( next == s || next[ 0 ] != '\0' ) {
602 		return -1;
603 	}
604 
605 	/* LLONG_MIN, LLONG_MAX are C99 only */
606 #if defined (LLONG_MIN) && defined(LLONG_MAX)
607 	if ( ( ll == LLONG_MIN || ll == LLONG_MAX ) && save_errno != 0 ) {
608 		return -1;
609 	}
610 #endif /* LLONG_MIN && LLONG_MAX */
611 
612 	*v = ll;
613 
614 	return 0;
615 }
616 #endif /* HAVE_STRTOLL || HAVE_STRTOQ */
617 
618 #if defined(HAVE_STRTOULL) || defined(HAVE_STRTOUQ)
619 int
620 lutil_atoullx( unsigned long long *v, const char *s, int x )
621 {
622 	char *next;
623 	unsigned long long ull;
624 	int save_errno;
625 
626 	assert( s != NULL );
627 	assert( v != NULL );
628 
629 	/* strtoull() has an odd interface */
630 	if ( s[ 0 ] == '-' || isspace( s[ 0 ] ) ) {
631 		return -1;
632 	}
633 
634 	errno = 0;
635 #ifdef HAVE_STRTOULL
636 	ull = strtoull( s, &next, x );
637 #else /* HAVE_STRTOUQ */
638 	ull = (unsigned long long)strtouq( s, &next, x );
639 #endif /* HAVE_STRTOUQ */
640 	save_errno = errno;
641 	if ( next == s || next[ 0 ] != '\0' ) {
642 		return -1;
643 	}
644 
645 	/* ULLONG_MAX is C99 only */
646 #if defined(ULLONG_MAX)
647 	if ( ( ull == 0 || ull == ULLONG_MAX ) && save_errno != 0 ) {
648 		return -1;
649 	}
650 #endif /* ULLONG_MAX */
651 
652 	*v = ull;
653 
654 	return 0;
655 }
656 #endif /* HAVE_STRTOULL || HAVE_STRTOUQ */
657 #endif /* HAVE_LONG_LONG */
658 
659 /* Multiply an integer by 100000000 and add new */
660 typedef struct lutil_int_decnum {
661 	unsigned char *buf;
662 	int bufsiz;
663 	int beg;
664 	int len;
665 } lutil_int_decnum;
666 
667 #define	FACTOR1	(100000000&0xffff)
668 #define FACTOR2 (100000000>>16)
669 
670 static void
671 scale( int new, lutil_int_decnum *prev, unsigned char *tmp )
672 {
673 	int i, j;
674 	unsigned char *in = prev->buf+prev->beg;
675 	unsigned int part;
676 	unsigned char *out = tmp + prev->bufsiz - prev->len;
677 
678 	memset( tmp, 0, prev->bufsiz );
679 	if ( prev->len ) {
680 		for ( i = prev->len-1; i>=0; i-- ) {
681 			part = in[i] * FACTOR1;
682 			for ( j = i; part; j-- ) {
683 				part += out[j];
684 				out[j] = part & 0xff;
685 				part >>= 8;
686 			}
687 			part = in[i] * FACTOR2;
688 			for ( j = i-2; part; j-- ) {
689 				part += out[j];
690 				out[j] = part & 0xff;
691 				part >>= 8;
692 			}
693 		}
694 		j++;
695 		prev->beg += j;
696 		prev->len -= j;
697 	}
698 
699 	out = tmp + prev->bufsiz;
700 	i = 0;
701 	do {
702 		i--;
703 		new += out[i];
704 		out[i] = new & 0xff;
705 		new >>= 8;
706 	} while ( new );
707 	i = -i;
708 	if ( prev->len < i ) {
709 		prev->beg = prev->bufsiz - i;
710 		prev->len = i;
711 	}
712 	AC_MEMCPY( prev->buf+prev->beg, tmp+prev->beg, prev->len );
713 }
714 
715 /* Convert unlimited length decimal or hex string to binary.
716  * Output buffer must be provided, bv_len must indicate buffer size
717  * Hex input can be "0x1234" or "'1234'H"
718  *
719  * Note: High bit of binary form is always the sign bit. If the number
720  * is supposed to be positive but has the high bit set, a zero byte
721  * is prepended. It is assumed that this has already been handled on
722  * any hex input.
723  */
724 int
725 lutil_str2bin( struct berval *in, struct berval *out, void *ctx )
726 {
727 	char *pin, *pout;
728 	char *end;
729 	int i, chunk, len, rc = 0, hex = 0;
730 	if ( !out || !out->bv_val || out->bv_len < in->bv_len )
731 		return -1;
732 
733 	pout = out->bv_val;
734 	/* Leading "0x" for hex input */
735 	if ( in->bv_len > 2 && in->bv_val[0] == '0' &&
736 		( in->bv_val[1] == 'x' || in->bv_val[1] == 'X' ) )
737 	{
738 		len = in->bv_len - 2;
739 		pin = in->bv_val + 2;
740 		hex = 1;
741 	} else if ( in->bv_len > 3 && in->bv_val[0] == '\'' &&
742 		in->bv_val[in->bv_len-2] == '\'' &&
743 		in->bv_val[in->bv_len-1] == 'H' )
744 	{
745 		len = in->bv_len - 3;
746 		pin = in->bv_val + 1;
747 		hex = 1;
748 	}
749 	if ( hex ) {
750 #define HEXMAX	(2 * sizeof(long))
751 		unsigned long l;
752 		char tbuf[HEXMAX+1];
753 
754 		/* Convert a longword at a time, but handle leading
755 		 * odd bytes first
756 		 */
757 		chunk = len % HEXMAX;
758 		if ( !chunk )
759 			chunk = HEXMAX;
760 
761 		while ( len ) {
762 			int ochunk;
763 			memcpy( tbuf, pin, chunk );
764 			tbuf[chunk] = '\0';
765 			errno = 0;
766 			l = strtoul( tbuf, &end, 16 );
767 			if ( errno )
768 				return -1;
769 			ochunk = (chunk + 1)/2;
770 			for ( i = ochunk - 1; i >= 0; i-- ) {
771 				pout[i] = l & 0xff;
772 				l >>= 8;
773 			}
774 			pin += chunk;
775 			pout += ochunk;
776 			len -= chunk;
777 			chunk = HEXMAX;
778 		}
779 		out->bv_len = pout - out->bv_val;
780 	} else {
781 	/* Decimal */
782 #define	DECMAX	8	/* 8 digits at a time */
783 		char tmpbuf[64], *tmp;
784 		lutil_int_decnum num;
785 		int neg = 0;
786 		long l;
787 		char tbuf[DECMAX+1];
788 
789 		len = in->bv_len;
790 		pin = in->bv_val;
791 		num.buf = (unsigned char *)out->bv_val;
792 		num.bufsiz = out->bv_len;
793 		num.beg = num.bufsiz-1;
794 		num.len = 0;
795 		if ( pin[0] == '-' ) {
796 			neg = 0xff;
797 			len--;
798 			pin++;
799 		}
800 
801 		/* tmp must be at least as large as outbuf */
802 		if ( out->bv_len > sizeof(tmpbuf)) {
803 			tmp = ber_memalloc_x( out->bv_len, ctx );
804 		} else {
805 			tmp = tmpbuf;
806 		}
807 		chunk = len & (DECMAX-1);
808 		if ( !chunk )
809 			chunk = DECMAX;
810 
811 		while ( len ) {
812 			memcpy( tbuf, pin, chunk );
813 			tbuf[chunk] = '\0';
814 			errno = 0;
815 			l = strtol( tbuf, &end, 10 );
816 			if ( errno ) {
817 				rc = -1;
818 				goto decfail;
819 			}
820 			scale( l, &num, (unsigned char *)tmp );
821 			pin += chunk;
822 			len -= chunk;
823 			chunk = DECMAX;
824 		}
825 		/* Negate the result */
826 		if ( neg ) {
827 			unsigned char *ptr;
828 
829 			ptr = num.buf+num.beg;
830 
831 			/* flip all bits */
832 			for ( i=0; i<num.len; i++ )
833 				ptr[i] ^= 0xff;
834 
835 			/* add 1, with carry - overflow handled below */
836 			while ( i-- && ! (ptr[i] = (ptr[i] + 1) & 0xff )) ;
837 		}
838 		/* Prepend sign byte if wrong sign bit */
839 		if (( num.buf[num.beg] ^ neg ) & 0x80 ) {
840 			num.beg--;
841 			num.len++;
842 			num.buf[num.beg] = neg;
843 		}
844 		if ( num.beg )
845 			AC_MEMCPY( num.buf, num.buf+num.beg, num.len );
846 		out->bv_len = num.len;
847 decfail:
848 		if ( tmp != tmpbuf ) {
849 			ber_memfree_x( tmp, ctx );
850 		}
851 	}
852 	return rc;
853 }
854 
855 static	char		time_unit[] = "dhms";
856 
857 /* Used to parse and unparse time intervals, not timestamps */
858 int
859 lutil_parse_time(
860 	const char	*in,
861 	unsigned long	*tp )
862 {
863 	unsigned long	t = 0;
864 	char		*s,
865 			*next;
866 	int		sofar = -1,
867 			scale[] = { 86400, 3600, 60, 1 };
868 
869 	*tp = 0;
870 
871 	for ( s = (char *)in; s[ 0 ] != '\0'; ) {
872 		unsigned long	u;
873 		char		*what;
874 
875 		/* strtoul() has an odd interface */
876 		if ( s[ 0 ] == '-' ) {
877 			return -1;
878 		}
879 
880 		u = strtoul( s, &next, 10 );
881 		if ( next == s ) {
882 			return -1;
883 		}
884 
885 		if ( next[ 0 ] == '\0' ) {
886 			/* assume seconds */
887 			t += u;
888 			break;
889 		}
890 
891 		what = strchr( time_unit, next[ 0 ] );
892 		if ( what == NULL ) {
893 			return -1;
894 		}
895 
896 		if ( what - time_unit <= sofar ) {
897 			return -1;
898 		}
899 
900 		sofar = what - time_unit;
901 		t += u * scale[ sofar ];
902 
903 		s = &next[ 1 ];
904 	}
905 
906 	*tp = t;
907 	return 0;
908 }
909 
910 int
911 lutil_unparse_time(
912 	char			*buf,
913 	size_t			buflen,
914 	unsigned long		t )
915 {
916 	int		len, i;
917 	unsigned long	v[ 4 ];
918 	char		*ptr = buf;
919 
920 	v[ 0 ] = t/86400;
921 	v[ 1 ] = (t%86400)/3600;
922 	v[ 2 ] = (t%3600)/60;
923 	v[ 3 ] = t%60;
924 
925 	for ( i = 0; i < 4; i++ ) {
926 		if ( v[i] > 0 || ( i == 3 && ptr == buf ) ) {
927 			len = snprintf( ptr, buflen, "%lu%c", v[ i ], time_unit[ i ] );
928 			if ( len < 0 || (unsigned)len >= buflen ) {
929 				return -1;
930 			}
931 			buflen -= len;
932 			ptr += len;
933 		}
934 	}
935 
936 	return 0;
937 }
938 
939 /*
940  * formatted print to string
941  *
942  * - if return code < 0, the error code returned by vsnprintf(3) is returned
943  *
944  * - if return code > 0, the buffer was not long enough;
945  *	- if next is not NULL, *next will be set to buf + bufsize - 1
946  *	- if len is not NULL, *len will contain the required buffer length
947  *
948  * - if return code == 0, the buffer was long enough;
949  *	- if next is not NULL, *next will point to the end of the string printed so far
950  *	- if len is not NULL, *len will contain the length of the string printed so far
951  */
952 int
953 lutil_snprintf( char *buf, ber_len_t bufsize, char **next, ber_len_t *len, LDAP_CONST char *fmt, ... )
954 {
955 	va_list		ap;
956 	int		ret;
957 
958 	assert( buf != NULL );
959 	assert( bufsize > 0 );
960 	assert( fmt != NULL );
961 
962 	va_start( ap, fmt );
963 	ret = vsnprintf( buf, bufsize, fmt, ap );
964 	va_end( ap );
965 
966 	if ( ret < 0 ) {
967 		return ret;
968 	}
969 
970 	if ( len ) {
971 		*len = ret;
972 	}
973 
974 	if ( (unsigned) ret >= bufsize ) {
975 		if ( next ) {
976 			*next = &buf[ bufsize - 1 ];
977 		}
978 
979 		return 1;
980 	}
981 
982 	if ( next ) {
983 		*next = &buf[ ret ];
984 	}
985 
986 	return 0;
987 }
988 
989