1998-08-09 08:43:13 +08:00
|
|
|
/* decode.c - ber input decoding routines */
|
|
|
|
/*
|
1998-12-29 04:38:04 +08:00
|
|
|
* Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
|
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
|
|
|
/* Portions
|
1998-08-09 08:43:13 +08:00
|
|
|
* Copyright (c) 1990 Regents of the University of Michigan.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms are permitted
|
|
|
|
* provided that this notice is preserved and that due credit is given
|
|
|
|
* to the University of Michigan at Ann Arbor. The name of the University
|
|
|
|
* may not be used to endorse or promote products derived from this
|
|
|
|
* software without specific prior written permission. This software
|
|
|
|
* is provided ``as is'' without express or implied warranty.
|
|
|
|
*/
|
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#include "portable.h"
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1998-11-04 09:41:00 +08:00
|
|
|
|
1998-11-06 09:18:49 +08:00
|
|
|
#include <ac/stdarg.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
|
1998-10-26 09:18:41 +08:00
|
|
|
#include "lber-int.h"
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
static int ber_getnint LDAP_P(( BerElement *ber, long *num, int len ));
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/* return the tag - LBER_DEFAULT returned means trouble */
|
|
|
|
unsigned long
|
|
|
|
ber_get_tag( BerElement *ber )
|
|
|
|
{
|
|
|
|
unsigned char xbyte;
|
|
|
|
unsigned long tag;
|
|
|
|
char *tagp;
|
1998-11-04 21:15:18 +08:00
|
|
|
unsigned int i;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
|
|
|
|
return( LBER_DEFAULT );
|
|
|
|
|
|
|
|
if ( (xbyte & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK )
|
|
|
|
return( (unsigned long) xbyte );
|
|
|
|
|
|
|
|
tagp = (char *) &tag;
|
|
|
|
tagp[0] = xbyte;
|
|
|
|
for ( i = 1; i < sizeof(long); i++ ) {
|
|
|
|
if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 )
|
|
|
|
return( LBER_DEFAULT );
|
|
|
|
|
|
|
|
tagp[i] = xbyte;
|
|
|
|
|
|
|
|
if ( ! (xbyte & LBER_MORE_TAG_MASK) )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* tag too big! */
|
|
|
|
if ( i == sizeof(long) )
|
|
|
|
return( LBER_DEFAULT );
|
|
|
|
|
|
|
|
/* want leading, not trailing 0's */
|
|
|
|
return( tag >> (sizeof(long) - i - 1) );
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long
|
|
|
|
ber_skip_tag( BerElement *ber, unsigned long *len )
|
|
|
|
{
|
|
|
|
unsigned long tag;
|
|
|
|
unsigned char lc;
|
|
|
|
int noctets, diff;
|
|
|
|
unsigned long netlen;
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
|
|
|
assert( len != NULL );
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
|
|
|
* Any ber element looks like this: tag length contents.
|
|
|
|
* Assuming everything's ok, we return the tag byte (we
|
|
|
|
* can assume a single byte), and return the length in len.
|
|
|
|
*
|
|
|
|
* Assumptions:
|
|
|
|
* 1) definite lengths
|
|
|
|
* 2) primitive encodings used whenever possible
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First, we read the tag.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if ( (tag = ber_get_tag( ber )) == LBER_DEFAULT )
|
|
|
|
return( LBER_DEFAULT );
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Next, read the length. The first byte contains the length of
|
|
|
|
* the length. If bit 8 is set, the length is the long form,
|
|
|
|
* otherwise it's the short form. We don't allow a length that's
|
|
|
|
* greater than what we can hold in an unsigned long.
|
|
|
|
*/
|
|
|
|
|
|
|
|
*len = netlen = 0;
|
|
|
|
if ( ber_read( ber, (char *) &lc, 1 ) != 1 )
|
|
|
|
return( LBER_DEFAULT );
|
1999-05-19 14:13:44 +08:00
|
|
|
if ( lc & 0x80U ) {
|
|
|
|
noctets = (lc & 0x7fU);
|
1999-04-27 13:19:32 +08:00
|
|
|
if ( (unsigned) noctets > sizeof(unsigned long) )
|
1998-08-09 08:43:13 +08:00
|
|
|
return( LBER_DEFAULT );
|
|
|
|
diff = sizeof(unsigned long) - noctets;
|
|
|
|
if ( ber_read( ber, (char *) &netlen + diff, noctets )
|
|
|
|
!= noctets )
|
|
|
|
return( LBER_DEFAULT );
|
1998-10-25 09:41:42 +08:00
|
|
|
*len = AC_NTOHL( netlen );
|
1998-08-09 08:43:13 +08:00
|
|
|
} else {
|
|
|
|
*len = lc;
|
|
|
|
}
|
|
|
|
|
|
|
|
return( tag );
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_peek_tag(
|
|
|
|
LDAP_CONST BerElement *ber_in, /* not const per c-api-02 */
|
|
|
|
unsigned long *len )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
unsigned long tag;
|
1999-05-19 09:12:33 +08:00
|
|
|
BerElement *ber = ber_dup( ber_in );
|
|
|
|
|
|
|
|
if( ber == NULL ) {
|
|
|
|
return LBER_ERROR;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
tag = ber_skip_tag( ber, len );
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_free( ber, 1 );
|
1998-08-09 08:43:13 +08:00
|
|
|
return( tag );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ber_getnint( BerElement *ber, long *num, int len )
|
|
|
|
{
|
|
|
|
int diff, sign, i;
|
|
|
|
long netnum;
|
1998-08-09 11:40:29 +08:00
|
|
|
char *p;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
|
|
|
assert( num != NULL );
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
|
|
|
* The tag and length have already been stripped off. We should
|
|
|
|
* be sitting right before len bytes of 2's complement integer,
|
|
|
|
* ready to be read straight into an int. We may have to sign
|
|
|
|
* extend after we read it in.
|
|
|
|
*/
|
|
|
|
|
1999-04-27 13:19:32 +08:00
|
|
|
if ( (unsigned) len > sizeof(long) )
|
1998-08-09 08:43:13 +08:00
|
|
|
return( -1 );
|
|
|
|
|
|
|
|
netnum = 0;
|
|
|
|
diff = sizeof(long) - len;
|
|
|
|
/* read into the low-order bytes of netnum */
|
|
|
|
if ( ber_read( ber, ((char *) &netnum) + diff, len ) != len )
|
|
|
|
return( -1 );
|
|
|
|
|
1998-08-09 11:40:29 +08:00
|
|
|
/* sign extend if necessary */
|
|
|
|
p = (char *) &netnum;
|
|
|
|
sign = (0x80 & *(p+diff) );
|
1999-04-27 13:19:32 +08:00
|
|
|
if ( sign && ((unsigned) len < sizeof(long)) ) {
|
1998-08-09 11:40:29 +08:00
|
|
|
for ( i = 0; i < diff; i++ ) {
|
1998-10-25 09:41:42 +08:00
|
|
|
*(p+i) = (unsigned char) 0xff;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
}
|
1998-10-25 09:41:42 +08:00
|
|
|
*num = AC_NTOHL( netnum );
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
return( len );
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long
|
|
|
|
ber_get_int( BerElement *ber, long *num )
|
|
|
|
{
|
|
|
|
unsigned long tag, len;
|
|
|
|
|
|
|
|
if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
|
|
|
|
return( LBER_DEFAULT );
|
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
if ( (unsigned long) ber_getnint( ber, num, (int)len ) != len )
|
1998-08-09 08:43:13 +08:00
|
|
|
return( LBER_DEFAULT );
|
|
|
|
else
|
|
|
|
return( tag );
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long
|
|
|
|
ber_get_stringb( BerElement *ber, char *buf, unsigned long *len )
|
|
|
|
{
|
|
|
|
unsigned long datalen, tag;
|
|
|
|
#ifdef STR_TRANSLATION
|
|
|
|
char *transbuf;
|
|
|
|
#endif /* STR_TRANSLATION */
|
|
|
|
|
|
|
|
if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT )
|
|
|
|
return( LBER_DEFAULT );
|
|
|
|
if ( datalen > (*len - 1) )
|
|
|
|
return( LBER_DEFAULT );
|
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
if ( (unsigned long) ber_read( ber, buf, datalen ) != datalen )
|
1998-08-09 08:43:13 +08:00
|
|
|
return( LBER_DEFAULT );
|
|
|
|
|
|
|
|
buf[datalen] = '\0';
|
|
|
|
|
|
|
|
#ifdef STR_TRANSLATION
|
|
|
|
if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
|
1999-03-03 23:57:57 +08:00
|
|
|
&& ber->ber_decode_translate_proc ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
transbuf = buf;
|
|
|
|
++datalen;
|
|
|
|
if ( (*(ber->ber_decode_translate_proc))( &transbuf, &datalen,
|
|
|
|
0 ) != 0 ) {
|
|
|
|
return( LBER_DEFAULT );
|
|
|
|
}
|
|
|
|
if ( datalen > *len ) {
|
|
|
|
free( transbuf );
|
|
|
|
return( LBER_DEFAULT );
|
|
|
|
}
|
|
|
|
SAFEMEMCPY( buf, transbuf, datalen );
|
|
|
|
free( transbuf );
|
|
|
|
--datalen;
|
|
|
|
}
|
|
|
|
#endif /* STR_TRANSLATION */
|
|
|
|
|
|
|
|
*len = datalen;
|
|
|
|
return( tag );
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long
|
|
|
|
ber_get_stringa( BerElement *ber, char **buf )
|
|
|
|
{
|
|
|
|
unsigned long datalen, tag;
|
|
|
|
|
1998-12-26 17:05:57 +08:00
|
|
|
if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) {
|
|
|
|
*buf = NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
return( LBER_DEFAULT );
|
1998-12-26 17:05:57 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
if ( (*buf = (char *) malloc( (size_t)datalen + 1 )) == NULL )
|
|
|
|
return( LBER_DEFAULT );
|
|
|
|
|
1998-12-26 17:05:57 +08:00
|
|
|
if ( (unsigned long) ber_read( ber, *buf, datalen ) != datalen ) {
|
|
|
|
free( *buf );
|
|
|
|
*buf = NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
return( LBER_DEFAULT );
|
1998-12-26 17:05:57 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
(*buf)[datalen] = '\0';
|
|
|
|
|
|
|
|
#ifdef STR_TRANSLATION
|
|
|
|
if ( datalen > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
|
1999-03-03 23:57:57 +08:00
|
|
|
&& ber->ber_decode_translate_proc ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
++datalen;
|
|
|
|
if ( (*(ber->ber_decode_translate_proc))( buf, &datalen, 1 )
|
|
|
|
!= 0 ) {
|
|
|
|
free( *buf );
|
1998-12-26 17:05:57 +08:00
|
|
|
*buf = NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
return( LBER_DEFAULT );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* STR_TRANSLATION */
|
|
|
|
|
|
|
|
return( tag );
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long
|
|
|
|
ber_get_stringal( BerElement *ber, struct berval **bv )
|
|
|
|
{
|
|
|
|
unsigned long len, tag;
|
|
|
|
|
1998-12-26 17:05:57 +08:00
|
|
|
if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT ) {
|
|
|
|
*bv = NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
return( LBER_DEFAULT );
|
1998-12-26 17:05:57 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-12-26 17:05:57 +08:00
|
|
|
if ( (*bv = (struct berval *) malloc( sizeof(struct berval) )) == NULL )
|
1998-08-09 08:43:13 +08:00
|
|
|
return( LBER_DEFAULT );
|
|
|
|
|
1998-12-26 17:05:57 +08:00
|
|
|
if ( ((*bv)->bv_val = (char *) malloc( (size_t)len + 1 )) == NULL ) {
|
|
|
|
free( *bv );
|
|
|
|
*bv = NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
return( LBER_DEFAULT );
|
1998-12-26 17:05:57 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-12-26 17:05:57 +08:00
|
|
|
if ( (unsigned long) ber_read( ber, (*bv)->bv_val, len ) != len ) {
|
|
|
|
ber_bvfree( *bv );
|
|
|
|
*bv = NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
return( LBER_DEFAULT );
|
1998-12-26 17:05:57 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
((*bv)->bv_val)[len] = '\0';
|
|
|
|
(*bv)->bv_len = len;
|
|
|
|
|
|
|
|
#ifdef STR_TRANSLATION
|
|
|
|
if ( len > 0 && ( ber->ber_options & LBER_TRANSLATE_STRINGS ) != 0
|
1999-03-03 23:57:57 +08:00
|
|
|
&& ber->ber_decode_translate_proc ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
++len;
|
|
|
|
if ( (*(ber->ber_decode_translate_proc))( &((*bv)->bv_val),
|
|
|
|
&len, 1 ) != 0 ) {
|
1998-12-26 17:05:57 +08:00
|
|
|
ber_bvfree( *bv );
|
|
|
|
*bv = NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
return( LBER_DEFAULT );
|
|
|
|
}
|
|
|
|
(*bv)->bv_len = len - 1;
|
|
|
|
}
|
|
|
|
#endif /* STR_TRANSLATION */
|
|
|
|
|
|
|
|
return( tag );
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long
|
|
|
|
ber_get_bitstringa( BerElement *ber, char **buf, unsigned long *blen )
|
|
|
|
{
|
|
|
|
unsigned long datalen, tag;
|
|
|
|
unsigned char unusedbits;
|
|
|
|
|
1998-12-26 17:05:57 +08:00
|
|
|
if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) {
|
|
|
|
*buf = NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
return( LBER_DEFAULT );
|
1998-12-26 17:05:57 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
--datalen;
|
|
|
|
|
|
|
|
if ( (*buf = (char *) malloc( (size_t)datalen )) == NULL )
|
|
|
|
return( LBER_DEFAULT );
|
|
|
|
|
1998-12-26 17:05:57 +08:00
|
|
|
if ( ber_read( ber, (char *)&unusedbits, 1 ) != 1 ) {
|
|
|
|
free( buf );
|
|
|
|
*buf = NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
return( LBER_DEFAULT );
|
1998-12-26 17:05:57 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-12-26 17:05:57 +08:00
|
|
|
if ( (unsigned long) ber_read( ber, *buf, datalen ) != datalen ) {
|
|
|
|
free( buf );
|
|
|
|
*buf = NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
return( LBER_DEFAULT );
|
1998-12-26 17:05:57 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
*blen = datalen * 8 - unusedbits;
|
|
|
|
return( tag );
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long
|
|
|
|
ber_get_null( BerElement *ber )
|
|
|
|
{
|
|
|
|
unsigned long len, tag;
|
|
|
|
|
|
|
|
if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
|
|
|
|
return( LBER_DEFAULT );
|
|
|
|
|
|
|
|
if ( len != 0 )
|
|
|
|
return( LBER_DEFAULT );
|
|
|
|
|
|
|
|
return( tag );
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long
|
|
|
|
ber_get_boolean( BerElement *ber, int *boolval )
|
|
|
|
{
|
|
|
|
long longbool;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
rc = ber_get_int( ber, &longbool );
|
|
|
|
*boolval = longbool;
|
|
|
|
|
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long
|
|
|
|
ber_first_element( BerElement *ber, unsigned long *len, char **last )
|
|
|
|
{
|
|
|
|
/* skip the sequence header, use the len to mark where to stop */
|
|
|
|
if ( ber_skip_tag( ber, len ) == LBER_DEFAULT ) {
|
1998-12-26 17:05:57 +08:00
|
|
|
*last = NULL;
|
1998-08-09 08:43:13 +08:00
|
|
|
return( LBER_DEFAULT );
|
|
|
|
}
|
|
|
|
|
|
|
|
*last = ber->ber_ptr + *len;
|
|
|
|
|
|
|
|
if ( *last == ber->ber_ptr ) {
|
1998-12-19 05:08:28 +08:00
|
|
|
#ifdef LBER_END_SEQORSET
|
|
|
|
return( LBER_END_SEQORSET );
|
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
return( LBER_DEFAULT );
|
1998-12-19 05:08:28 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return( ber_peek_tag( ber, len ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long
|
|
|
|
ber_next_element( BerElement *ber, unsigned long *len, char *last )
|
|
|
|
{
|
|
|
|
if ( ber->ber_ptr == last ) {
|
1998-12-19 05:08:28 +08:00
|
|
|
#ifdef LBER_END_SEQORSET
|
|
|
|
return( LBER_END_SEQORSET );
|
|
|
|
#else
|
1998-08-09 08:43:13 +08:00
|
|
|
return( LBER_DEFAULT );
|
1998-12-19 05:08:28 +08:00
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return( ber_peek_tag( ber, len ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* VARARGS */
|
|
|
|
unsigned long
|
1998-10-25 09:41:42 +08:00
|
|
|
ber_scanf
|
1998-11-06 09:18:49 +08:00
|
|
|
#if HAVE_STDARG
|
1999-05-19 09:12:33 +08:00
|
|
|
( BerElement *ber,
|
|
|
|
LDAP_CONST char *fmt,
|
|
|
|
... )
|
1998-08-09 08:43:13 +08:00
|
|
|
#else
|
1998-10-25 09:41:42 +08:00
|
|
|
( va_alist )
|
1998-08-09 08:43:13 +08:00
|
|
|
va_dcl
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
va_list ap;
|
1998-11-06 09:18:49 +08:00
|
|
|
#ifndef HAVE_STDARG
|
1998-08-09 08:43:13 +08:00
|
|
|
BerElement *ber;
|
|
|
|
char *fmt;
|
|
|
|
#endif
|
1999-05-19 09:12:33 +08:00
|
|
|
LDAP_CONST char *fmt_reset;
|
1998-08-09 08:43:13 +08:00
|
|
|
char *last;
|
|
|
|
char *s, **ss, ***sss;
|
|
|
|
struct berval ***bv, **bvp, *bval;
|
|
|
|
int *i, j;
|
1998-11-04 21:15:18 +08:00
|
|
|
long *l;
|
|
|
|
unsigned long rc, tag, len;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
|
|
|
|
1998-11-06 09:18:49 +08:00
|
|
|
#ifdef HAVE_STDARG
|
1998-08-09 08:43:13 +08:00
|
|
|
va_start( ap, fmt );
|
|
|
|
#else
|
|
|
|
va_start( ap );
|
|
|
|
ber = va_arg( ap, BerElement * );
|
|
|
|
fmt = va_arg( ap, char * );
|
|
|
|
#endif
|
1999-05-19 09:12:33 +08:00
|
|
|
|
|
|
|
assert( ber != NULL );
|
|
|
|
assert( fmt != NULL );
|
|
|
|
|
1998-12-26 17:05:57 +08:00
|
|
|
fmt_reset = fmt;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-12-22 09:34:01 +08:00
|
|
|
if ( ber->ber_debug ) {
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_log_printf( LDAP_DEBUG_TRACE, ber->ber_debug,
|
1998-12-22 09:34:01 +08:00
|
|
|
"ber_scanf fmt (%s) ber:\n", fmt );
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_log_dump( LDAP_DEBUG_BER, ber->ber_debug, ber, 1 );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for ( rc = 0; *fmt && rc != LBER_DEFAULT; fmt++ ) {
|
1998-12-26 17:05:57 +08:00
|
|
|
/* When this is modified, remember to update
|
|
|
|
* the error-cleanup code below accordingly. */
|
1998-08-09 08:43:13 +08:00
|
|
|
switch ( *fmt ) {
|
1999-05-19 09:12:33 +08:00
|
|
|
case '!': { /* Hook */
|
|
|
|
BERDecodeCallback *f;
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
f = va_arg( ap, BERDecodeCallback * );
|
|
|
|
p = va_arg( ap, void * );
|
|
|
|
|
|
|
|
rc = (*f)( ber, p, 0 );
|
|
|
|
} break;
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
case 'a': /* octet string - allocate storage as needed */
|
|
|
|
ss = va_arg( ap, char ** );
|
|
|
|
rc = ber_get_stringa( ber, ss );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b': /* boolean */
|
|
|
|
i = va_arg( ap, int * );
|
|
|
|
rc = ber_get_boolean( ber, i );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'e': /* enumerated */
|
|
|
|
case 'i': /* int */
|
|
|
|
l = va_arg( ap, long * );
|
|
|
|
rc = ber_get_int( ber, l );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l': /* length of next item */
|
|
|
|
l = va_arg( ap, long * );
|
|
|
|
rc = ber_peek_tag( ber, (unsigned long *)l );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'n': /* null */
|
|
|
|
rc = ber_get_null( ber );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's': /* octet string - in a buffer */
|
|
|
|
s = va_arg( ap, char * );
|
|
|
|
l = va_arg( ap, long * );
|
|
|
|
rc = ber_get_stringb( ber, s, (unsigned long *)l );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'o': /* octet string in a supplied berval */
|
|
|
|
bval = va_arg( ap, struct berval * );
|
|
|
|
ber_peek_tag( ber, &bval->bv_len );
|
|
|
|
rc = ber_get_stringa( ber, &bval->bv_val );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'O': /* octet string - allocate & include length */
|
|
|
|
bvp = va_arg( ap, struct berval ** );
|
|
|
|
rc = ber_get_stringal( ber, bvp );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'B': /* bit string - allocate storage as needed */
|
|
|
|
ss = va_arg( ap, char ** );
|
|
|
|
l = va_arg( ap, long * ); /* for length, in bits */
|
|
|
|
rc = ber_get_bitstringa( ber, ss, (unsigned long *)l );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't': /* tag of next item */
|
|
|
|
i = va_arg( ap, int * );
|
|
|
|
*i = rc = ber_peek_tag( ber, &len );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'T': /* skip tag of next item */
|
|
|
|
i = va_arg( ap, int * );
|
|
|
|
*i = rc = ber_skip_tag( ber, &len );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v': /* sequence of strings */
|
|
|
|
sss = va_arg( ap, char *** );
|
|
|
|
*sss = NULL;
|
|
|
|
j = 0;
|
|
|
|
for ( tag = ber_first_element( ber, &len, &last );
|
1998-12-19 05:08:28 +08:00
|
|
|
tag != LBER_DEFAULT &&
|
1999-04-30 16:23:32 +08:00
|
|
|
#ifdef LBER_END_SEQORSET
|
1998-12-19 05:08:28 +08:00
|
|
|
tag != LBER_END_SEQORSET &&
|
|
|
|
#endif
|
|
|
|
rc != LBER_DEFAULT;
|
|
|
|
tag = ber_next_element( ber, &len, last ) )
|
|
|
|
{
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( *sss == NULL ) {
|
|
|
|
*sss = (char **) malloc(
|
|
|
|
2 * sizeof(char *) );
|
|
|
|
} else {
|
|
|
|
*sss = (char **) realloc( *sss,
|
|
|
|
(j + 2) * sizeof(char *) );
|
|
|
|
}
|
|
|
|
rc = ber_get_stringa( ber, &((*sss)[j]) );
|
|
|
|
j++;
|
|
|
|
}
|
1999-04-30 16:23:32 +08:00
|
|
|
#ifdef LBER_END_SEQORSET
|
1998-12-19 05:08:28 +08:00
|
|
|
if (rc != LBER_DEFAULT &&
|
|
|
|
tag != LBER_END_SEQORSET )
|
|
|
|
{
|
|
|
|
rc = LBER_DEFAULT;
|
|
|
|
}
|
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( j > 0 )
|
|
|
|
(*sss)[j] = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'V': /* sequence of strings + lengths */
|
|
|
|
bv = va_arg( ap, struct berval *** );
|
|
|
|
*bv = NULL;
|
|
|
|
j = 0;
|
|
|
|
for ( tag = ber_first_element( ber, &len, &last );
|
1998-12-19 05:08:28 +08:00
|
|
|
tag != LBER_DEFAULT &&
|
1999-04-30 16:23:32 +08:00
|
|
|
#ifdef LBER_END_SEQORSET
|
1998-12-19 05:08:28 +08:00
|
|
|
tag != LBER_END_SEQORSET &&
|
|
|
|
#endif
|
|
|
|
rc != LBER_DEFAULT;
|
|
|
|
tag = ber_next_element( ber, &len, last ) )
|
|
|
|
{
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( *bv == NULL ) {
|
|
|
|
*bv = (struct berval **) malloc(
|
|
|
|
2 * sizeof(struct berval *) );
|
|
|
|
} else {
|
|
|
|
*bv = (struct berval **) realloc( *bv,
|
|
|
|
(j + 2) * sizeof(struct berval *) );
|
|
|
|
}
|
|
|
|
rc = ber_get_stringal( ber, &((*bv)[j]) );
|
|
|
|
j++;
|
|
|
|
}
|
1999-04-30 16:23:32 +08:00
|
|
|
#ifdef LBER_END_SEQORSET
|
1998-12-19 05:08:28 +08:00
|
|
|
if (rc != LBER_DEFAULT &&
|
|
|
|
tag != LBER_END_SEQORSET )
|
|
|
|
{
|
|
|
|
rc = LBER_DEFAULT;
|
|
|
|
}
|
|
|
|
#endif
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( j > 0 )
|
|
|
|
(*bv)[j] = NULL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'x': /* skip the next element - whatever it is */
|
|
|
|
if ( (rc = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
|
|
|
|
break;
|
|
|
|
ber->ber_ptr += len;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '{': /* begin sequence */
|
|
|
|
case '[': /* begin set */
|
|
|
|
if ( *(fmt + 1) != 'v' && *(fmt + 1) != 'V' )
|
|
|
|
rc = ber_skip_tag( ber, &len );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '}': /* end sequence */
|
|
|
|
case ']': /* end set */
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
1998-12-22 09:34:01 +08:00
|
|
|
if( ber->ber_debug ) {
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_log_printf( LDAP_DEBUG_ANY, ber->ber_debug,
|
1998-12-22 09:34:01 +08:00
|
|
|
"ber_scanf: unknown fmt %c\n", *fmt );
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
rc = LBER_DEFAULT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end( ap );
|
|
|
|
|
1998-12-26 17:05:57 +08:00
|
|
|
if ( rc == LBER_DEFAULT ) {
|
|
|
|
/*
|
|
|
|
* Error. Reclaim malloced memory that was given to the caller.
|
|
|
|
* Set allocated pointers to NULL, "data length" outvalues to 0.
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_STDARG
|
|
|
|
va_start( ap, fmt );
|
|
|
|
#else
|
|
|
|
va_start( ap );
|
|
|
|
(void) va_arg( ap, BerElement * );
|
|
|
|
(void) va_arg( ap, char * );
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for ( ; fmt_reset < fmt; fmt_reset++ ) {
|
|
|
|
switch ( *fmt_reset ) {
|
1999-05-19 09:12:33 +08:00
|
|
|
case '!': { /* Hook */
|
|
|
|
BERDecodeCallback *f;
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
f = va_arg( ap, BERDecodeCallback * );
|
|
|
|
p = va_arg( ap, void * );
|
|
|
|
|
|
|
|
(void) (*f)( ber, p, 1 );
|
|
|
|
} break;
|
|
|
|
|
1998-12-26 17:05:57 +08:00
|
|
|
case 'a': /* octet string - allocate storage as needed */
|
|
|
|
ss = va_arg( ap, char ** );
|
|
|
|
if ( *ss ) {
|
|
|
|
free( *ss );
|
|
|
|
*ss = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b': /* boolean */
|
|
|
|
case 't': /* tag of next item */
|
|
|
|
case 'T': /* skip tag of next item */
|
|
|
|
(void) va_arg( ap, int * );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 's': /* octet string - in a buffer */
|
|
|
|
(void) va_arg( ap, char * );
|
|
|
|
/* Fall through */
|
|
|
|
case 'e': /* enumerated */
|
|
|
|
case 'i': /* int */
|
|
|
|
case 'l': /* length of next item */
|
|
|
|
(void) va_arg( ap, long * );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'o': /* octet string in a supplied berval */
|
|
|
|
bval = va_arg( ap, struct berval * );
|
|
|
|
if ( bval->bv_val ) {
|
|
|
|
free( bval->bv_val );
|
|
|
|
bval->bv_val = NULL;
|
|
|
|
}
|
|
|
|
bval->bv_len = 0;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'O': /* octet string - allocate & include length */
|
|
|
|
bvp = va_arg( ap, struct berval ** );
|
|
|
|
if ( *bvp ) {
|
|
|
|
ber_bvfree( *bvp );
|
|
|
|
*bvp = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'B': /* bit string - allocate storage as needed */
|
|
|
|
ss = va_arg( ap, char ** );
|
|
|
|
if ( *ss ) {
|
|
|
|
free( *ss );
|
|
|
|
*ss = NULL;
|
|
|
|
}
|
|
|
|
*(va_arg( ap, long * )) = 0; /* for length, in bits */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v': /* sequence of strings */
|
|
|
|
sss = va_arg( ap, char *** );
|
|
|
|
if ( *sss ) {
|
1999-05-19 14:13:44 +08:00
|
|
|
for (j = 0; (*sss)[j]; j++) {
|
1998-12-26 17:05:57 +08:00
|
|
|
free( (*sss)[j] );
|
1999-05-19 14:13:44 +08:00
|
|
|
(*sss)[j] = NULL;
|
|
|
|
}
|
1998-12-26 17:05:57 +08:00
|
|
|
free( *sss );
|
|
|
|
*sss = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'V': /* sequence of strings + lengths */
|
|
|
|
bv = va_arg( ap, struct berval *** );
|
|
|
|
if ( *bv ) {
|
|
|
|
ber_bvecfree( *bv );
|
|
|
|
*bv = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
#if 0 /* No action for these format characters */
|
|
|
|
case 'n': /* null */
|
|
|
|
case 'x': /* skip the next element - whatever it is */
|
|
|
|
case '{': /* begin sequence */
|
|
|
|
case '[': /* begin set */
|
|
|
|
case '}': /* end sequence */
|
|
|
|
case ']': /* end set */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
va_end( ap );
|
|
|
|
}
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
return( rc );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ber_bvfree( struct berval *bv )
|
|
|
|
{
|
1998-12-19 05:08:28 +08:00
|
|
|
assert(bv != NULL); /* bv damn better point to something */
|
1999-05-19 09:12:33 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( bv->bv_val != NULL )
|
|
|
|
free( bv->bv_val );
|
|
|
|
free( (char *) bv );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ber_bvecfree( struct berval **bv )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
1998-12-19 05:08:28 +08:00
|
|
|
assert(bv != NULL); /* bv damn better point to something */
|
1999-05-19 09:12:33 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
for ( i = 0; bv[i] != NULL; i++ )
|
|
|
|
ber_bvfree( bv[i] );
|
|
|
|
free( (char *) bv );
|
|
|
|
}
|
|
|
|
|
|
|
|
struct berval *
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_bvdup(
|
|
|
|
LDAP_CONST struct berval *bv )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
struct berval *new;
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( bv != NULL );
|
|
|
|
|
|
|
|
if( bv == NULL ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( (new = (struct berval *) malloc( sizeof(struct berval) ))
|
|
|
|
== NULL ) {
|
|
|
|
return( NULL );
|
|
|
|
}
|
1998-12-19 05:08:28 +08:00
|
|
|
|
|
|
|
if ( bv->bv_val == NULL ) {
|
|
|
|
new->bv_val = NULL;
|
|
|
|
new->bv_len = 0;
|
|
|
|
return ( new );
|
|
|
|
}
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( (new->bv_val = (char *) malloc( bv->bv_len + 1 )) == NULL ) {
|
1998-12-19 05:08:28 +08:00
|
|
|
free( new );
|
1998-08-09 08:43:13 +08:00
|
|
|
return( NULL );
|
|
|
|
}
|
1998-12-19 05:08:28 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
SAFEMEMCPY( new->bv_val, bv->bv_val, (size_t) bv->bv_len );
|
|
|
|
new->bv_val[bv->bv_len] = '\0';
|
|
|
|
new->bv_len = bv->bv_len;
|
|
|
|
|
|
|
|
return( new );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef STR_TRANSLATION
|
|
|
|
void
|
|
|
|
ber_set_string_translators( BerElement *ber, BERTranslateProc encode_proc,
|
|
|
|
BERTranslateProc decode_proc )
|
|
|
|
{
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
ber->ber_encode_translate_proc = encode_proc;
|
|
|
|
ber->ber_decode_translate_proc = decode_proc;
|
|
|
|
}
|
|
|
|
#endif /* STR_TRANSLATION */
|