1998-08-09 08:43:13 +08:00
|
|
|
/* decode.c - ber input decoding routines */
|
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/>.
|
|
|
|
*
|
2007-01-03 04:00:42 +08:00
|
|
|
* Copyright 1998-2007 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>.
|
1998-12-29 04:38:04 +08:00
|
|
|
*/
|
2003-11-26 07:17:08 +08:00
|
|
|
/* Portions Copyright (c) 1990 Regents of the University of Michigan.
|
1998-08-09 08:43:13 +08:00
|
|
|
* 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.
|
|
|
|
*/
|
2003-11-27 06:51:15 +08:00
|
|
|
/* ACKNOWLEDGEMENTS:
|
|
|
|
* This work was originally developed by the University of Michigan
|
|
|
|
* (as part of U-MICH LDAP).
|
|
|
|
*/
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#include "portable.h"
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <stdio.h>
|
1998-11-04 09:41:00 +08:00
|
|
|
|
1999-06-03 08:37:44 +08:00
|
|
|
#include <ac/stdlib.h>
|
1998-10-25 09:41:42 +08:00
|
|
|
|
1999-06-03 08:37:44 +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
|
|
|
|
1999-08-02 09:50:08 +08:00
|
|
|
static ber_len_t ber_getnint LDAP_P((
|
1999-06-19 07:53:05 +08:00
|
|
|
BerElement *ber,
|
|
|
|
ber_int_t *num,
|
|
|
|
ber_len_t len ));
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2007-03-20 23:10:16 +08:00
|
|
|
/* out->bv_len should be the buffer size on input */
|
|
|
|
int
|
|
|
|
ber_decode_oid( BerValue *in, BerValue *out )
|
|
|
|
{
|
|
|
|
unsigned char *der = in->bv_val;
|
|
|
|
unsigned long val, val1;
|
|
|
|
int i, len;
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
assert( in != NULL );
|
|
|
|
assert( out != NULL );
|
|
|
|
|
|
|
|
/* expands by 5/2, and we add dots - call it 3 */
|
|
|
|
if ( !out->bv_val || out->bv_len < in->bv_len * 3 )
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
val1 = der[0] / 40;
|
|
|
|
val = der[0] - val1 * 40;
|
|
|
|
|
|
|
|
len = sprintf( out->bv_val, "%ld.%ld", val1, val );
|
|
|
|
ptr = out->bv_val + len;
|
|
|
|
val = 0;
|
|
|
|
for ( i=1; i<in->bv_len; i++ ) {
|
|
|
|
val = val << 7;
|
|
|
|
val |= der[i] & 0x7f;
|
|
|
|
if ( !( der[i] & 0x80 )) {
|
|
|
|
ptr += sprintf( ptr, ".%ld", val );
|
|
|
|
val = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
out->bv_len = ptr - out->bv_val;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/* return the tag - LBER_DEFAULT returned means trouble */
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_tag_t
|
1998-08-09 08:43:13 +08:00
|
|
|
ber_get_tag( BerElement *ber )
|
|
|
|
{
|
|
|
|
unsigned char xbyte;
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_tag_t tag;
|
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 );
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-19 09:12:33 +08:00
|
|
|
|
2002-01-06 10:23:36 +08:00
|
|
|
if ( ber_pvt_ber_remaining( ber ) < 1 ) {
|
2000-10-11 10:43:14 +08:00
|
|
|
return LBER_DEFAULT;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2003-07-08 11:46:20 +08:00
|
|
|
if ( ber->ber_ptr == ber->ber_buf ) {
|
2002-01-06 11:05:02 +08:00
|
|
|
tag = *(unsigned char *)ber->ber_ptr;
|
2003-07-08 11:46:20 +08:00
|
|
|
} else {
|
2002-01-06 11:05:02 +08:00
|
|
|
tag = ber->ber_tag;
|
2003-07-08 11:46:20 +08:00
|
|
|
}
|
2002-01-06 10:23:36 +08:00
|
|
|
ber->ber_ptr++;
|
|
|
|
|
2002-01-06 11:05:02 +08:00
|
|
|
if ( (tag & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK ) {
|
2000-04-25 18:42:32 +08:00
|
|
|
return tag;
|
2000-10-11 10:43:14 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
for ( i = 1; i < sizeof(ber_tag_t); i++ ) {
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 ) {
|
|
|
|
return LBER_DEFAULT;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-05-13 08:02:36 +08:00
|
|
|
tag <<= 8;
|
2000-04-26 17:17:05 +08:00
|
|
|
tag |= 0x00ffUL & (ber_tag_t) xbyte;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( ! (xbyte & LBER_MORE_TAG_MASK) ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
break;
|
2000-10-11 10:43:14 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* tag too big! */
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( i == sizeof(ber_tag_t) ) {
|
|
|
|
return LBER_DEFAULT;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-04-25 18:42:32 +08:00
|
|
|
return tag;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_tag_t
|
|
|
|
ber_skip_tag( BerElement *ber, ber_len_t *len )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_tag_t tag;
|
1998-08-09 08:43:13 +08:00
|
|
|
unsigned char lc;
|
1999-11-27 07:32:18 +08:00
|
|
|
ber_len_t i, noctets;
|
|
|
|
unsigned char netlen[sizeof(ber_len_t)];
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
|
|
|
assert( len != NULL );
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-19 09:12:33 +08:00
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
1999-12-10 05:30:32 +08:00
|
|
|
*len = 0;
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
|
|
|
* First, we read the tag.
|
|
|
|
*/
|
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( (tag = ber_get_tag( ber )) == LBER_DEFAULT ) {
|
|
|
|
return LBER_DEFAULT;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Next, read the length. The first byte contains the length of
|
2001-01-18 01:08:44 +08:00
|
|
|
* the length. If bit 8 is set, the length is the long form,
|
1998-08-09 08:43:13 +08:00
|
|
|
* otherwise it's the short form. We don't allow a length that's
|
1999-08-02 09:50:08 +08:00
|
|
|
* greater than what we can hold in a ber_len_t.
|
1998-08-09 08:43:13 +08:00
|
|
|
*/
|
|
|
|
|
2003-07-08 11:46:20 +08:00
|
|
|
if ( ber_read( ber, (char *) &lc, 1 ) != 1 ) {
|
2000-10-11 10:43:14 +08:00
|
|
|
return LBER_DEFAULT;
|
2003-07-08 11:46:20 +08:00
|
|
|
}
|
1999-11-27 07:32:18 +08:00
|
|
|
|
1999-05-19 14:13:44 +08:00
|
|
|
if ( lc & 0x80U ) {
|
|
|
|
noctets = (lc & 0x7fU);
|
1999-11-27 07:32:18 +08:00
|
|
|
|
|
|
|
if ( noctets > sizeof(ber_len_t) ) {
|
2000-10-11 10:43:14 +08:00
|
|
|
return LBER_DEFAULT;
|
1999-11-27 07:32:18 +08:00
|
|
|
}
|
|
|
|
|
2003-05-02 21:29:28 +08:00
|
|
|
if( (unsigned) ber_read( ber, (char *) netlen, noctets ) != noctets ) {
|
2000-10-11 10:43:14 +08:00
|
|
|
return LBER_DEFAULT;
|
1999-11-27 07:32:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for( i = 0; i < noctets; i++ ) {
|
|
|
|
*len <<= 8;
|
|
|
|
*len |= netlen[i];
|
|
|
|
}
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
} else {
|
|
|
|
*len = lc;
|
|
|
|
}
|
|
|
|
|
2001-05-07 03:07:24 +08:00
|
|
|
/* BER element should have enough data left */
|
2001-12-18 06:42:55 +08:00
|
|
|
if( *len > (ber_len_t) ber_pvt_ber_remaining( ber ) ) {
|
2001-05-07 03:07:24 +08:00
|
|
|
return LBER_DEFAULT;
|
|
|
|
}
|
2002-01-06 11:05:02 +08:00
|
|
|
ber->ber_tag = *(unsigned char *)ber->ber_ptr;
|
2001-05-07 03:07:24 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return tag;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_tag_t
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_peek_tag(
|
2000-07-27 07:32:39 +08:00
|
|
|
BerElement *ber,
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_len_t *len )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
2000-07-27 07:32:39 +08:00
|
|
|
/*
|
|
|
|
* This implementation assumes ber_skip_tag() only
|
|
|
|
* modifies ber_ptr field of the BerElement.
|
|
|
|
*/
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-07-27 07:32:39 +08:00
|
|
|
char *save;
|
2002-01-06 10:23:36 +08:00
|
|
|
ber_tag_t tag, old;
|
1999-11-08 23:38:59 +08:00
|
|
|
|
2002-01-06 10:23:36 +08:00
|
|
|
old = ber->ber_tag;
|
2000-07-27 07:32:39 +08:00
|
|
|
save = ber->ber_ptr;
|
1999-11-09 03:36:30 +08:00
|
|
|
tag = ber_skip_tag( ber, len );
|
2000-07-27 07:32:39 +08:00
|
|
|
ber->ber_ptr = save;
|
2002-01-06 10:23:36 +08:00
|
|
|
ber->ber_tag = old;
|
1999-11-09 03:36:30 +08:00
|
|
|
|
2000-07-27 07:32:39 +08:00
|
|
|
return tag;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-08-02 09:50:08 +08:00
|
|
|
static ber_len_t
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_getnint(
|
|
|
|
BerElement *ber,
|
|
|
|
ber_int_t *num,
|
|
|
|
ber_len_t len )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-06-19 07:53:05 +08:00
|
|
|
unsigned char buf[sizeof(ber_int_t)];
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
|
|
|
assert( num != NULL );
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-29 03:33:05 +08:00
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( len > sizeof(ber_int_t) ) {
|
|
|
|
return -1;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
/* read into the low-order bytes of our buffer */
|
1999-08-02 04:27:03 +08:00
|
|
|
if ( (ber_len_t) ber_read( ber, (char *) buf, len ) != len ) {
|
2000-10-11 10:43:14 +08:00
|
|
|
return -1;
|
1999-06-19 07:53:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if( len ) {
|
|
|
|
/* sign extend if necessary */
|
|
|
|
ber_len_t i;
|
|
|
|
ber_int_t netnum = 0x80 & buf[0] ? -1 : 0;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
/* shift in the bytes */
|
|
|
|
for( i=0 ; i<len; i++ ) {
|
|
|
|
netnum = (netnum << 8 ) | buf[i];
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1999-06-19 07:53:05 +08:00
|
|
|
|
|
|
|
*num = netnum;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
*num = 0;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
2002-01-06 11:05:02 +08:00
|
|
|
ber->ber_tag = *(unsigned char *)ber->ber_ptr;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return len;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_tag_t
|
|
|
|
ber_get_int(
|
|
|
|
BerElement *ber,
|
|
|
|
ber_int_t *num )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_tag_t tag;
|
|
|
|
ber_len_t len;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-21 05:21:57 +08:00
|
|
|
assert( ber != NULL );
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-21 05:21:57 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT ) {
|
|
|
|
return LBER_DEFAULT;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( ber_getnint( ber, num, len ) != len ) {
|
|
|
|
return LBER_DEFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
return tag;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2000-05-14 07:08:25 +08:00
|
|
|
ber_tag_t
|
|
|
|
ber_get_enum(
|
|
|
|
BerElement *ber,
|
|
|
|
ber_int_t *num )
|
|
|
|
{
|
2000-05-14 11:38:27 +08:00
|
|
|
return ber_get_int( ber, num );
|
2000-05-14 07:08:25 +08:00
|
|
|
}
|
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_tag_t
|
|
|
|
ber_get_stringb(
|
|
|
|
BerElement *ber,
|
|
|
|
char *buf,
|
|
|
|
ber_len_t *len )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_len_t datalen;
|
|
|
|
ber_tag_t tag;
|
|
|
|
|
1999-05-21 05:21:57 +08:00
|
|
|
assert( ber != NULL );
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-21 05:21:57 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) {
|
|
|
|
return LBER_DEFAULT;
|
|
|
|
}
|
2001-05-07 03:07:24 +08:00
|
|
|
|
|
|
|
/* must fit within allocated space with termination */
|
|
|
|
if ( datalen >= *len ) {
|
2000-10-11 10:43:14 +08:00
|
|
|
return LBER_DEFAULT;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( (ber_len_t) ber_read( ber, buf, datalen ) != datalen ) {
|
|
|
|
return LBER_DEFAULT;
|
|
|
|
}
|
2002-01-06 11:05:02 +08:00
|
|
|
ber->ber_tag = *(unsigned char *)ber->ber_ptr;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
buf[datalen] = '\0';
|
|
|
|
|
|
|
|
*len = datalen;
|
2000-10-11 10:43:14 +08:00
|
|
|
return tag;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2003-03-03 23:54:49 +08:00
|
|
|
/* Definitions for get_string vector
|
2002-01-03 13:36:26 +08:00
|
|
|
*
|
|
|
|
* ChArray, BvArray, and BvVec are self-explanatory.
|
|
|
|
* BvOff is a struct berval embedded in an array of larger structures
|
|
|
|
* of siz bytes at off bytes from the beginning of the struct.
|
|
|
|
*/
|
|
|
|
enum bgbvc { ChArray, BvArray, BvVec, BvOff };
|
2002-01-03 10:53:41 +08:00
|
|
|
|
2002-01-03 11:30:55 +08:00
|
|
|
/* Use this single cookie for state, to keep actual
|
|
|
|
* stack use to the absolute minimum.
|
|
|
|
*/
|
2002-01-03 10:53:41 +08:00
|
|
|
typedef struct bgbvr {
|
|
|
|
enum bgbvc choice;
|
2002-01-12 10:23:04 +08:00
|
|
|
BerElement *ber;
|
2002-01-06 13:36:07 +08:00
|
|
|
int alloc;
|
2002-01-03 13:36:26 +08:00
|
|
|
ber_len_t siz;
|
|
|
|
ber_len_t off;
|
2002-01-03 10:53:41 +08:00
|
|
|
union {
|
|
|
|
char ***c;
|
2002-01-14 09:43:17 +08:00
|
|
|
BerVarray *ba;
|
2002-01-03 10:53:41 +08:00
|
|
|
struct berval ***bv;
|
|
|
|
} res;
|
|
|
|
} bgbvr;
|
|
|
|
|
2002-01-06 13:36:07 +08:00
|
|
|
static ber_tag_t
|
2003-03-03 23:54:49 +08:00
|
|
|
ber_get_stringbvl( bgbvr *b, ber_len_t *rlen )
|
2002-01-03 10:53:41 +08:00
|
|
|
{
|
2003-03-03 23:54:49 +08:00
|
|
|
int i = 0, n;
|
|
|
|
ber_tag_t tag;
|
|
|
|
ber_len_t len;
|
|
|
|
char *last, *orig;
|
2002-01-03 11:11:14 +08:00
|
|
|
struct berval bv, *bvp = NULL;
|
2002-01-03 10:53:41 +08:00
|
|
|
|
2003-03-12 11:23:09 +08:00
|
|
|
/* For rewinding, just like ber_peek_tag() */
|
2003-03-03 23:54:49 +08:00
|
|
|
orig = b->ber->ber_ptr;
|
2003-03-12 10:33:53 +08:00
|
|
|
tag = b->ber->ber_tag;
|
2002-01-03 13:36:26 +08:00
|
|
|
|
2003-03-12 10:33:53 +08:00
|
|
|
if ( ber_first_element( b->ber, &len, &last ) != LBER_DEFAULT ) {
|
2003-07-08 11:46:20 +08:00
|
|
|
for ( ; b->ber->ber_ptr < last; i++ ) {
|
2003-03-12 10:33:53 +08:00
|
|
|
if (ber_skip_tag( b->ber, &len ) == LBER_DEFAULT) break;
|
2003-03-03 23:54:49 +08:00
|
|
|
b->ber->ber_ptr += len;
|
2003-04-24 09:37:01 +08:00
|
|
|
b->ber->ber_tag = *(unsigned char *)b->ber->ber_ptr;
|
2002-01-03 10:53:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-03 23:54:49 +08:00
|
|
|
if ( rlen ) *rlen = i;
|
2002-01-03 11:11:14 +08:00
|
|
|
|
2003-07-08 11:46:20 +08:00
|
|
|
if ( i == 0 ) {
|
2003-03-03 23:54:49 +08:00
|
|
|
*b->res.c = NULL;
|
|
|
|
return 0;
|
2002-01-03 11:11:14 +08:00
|
|
|
}
|
2002-01-03 10:53:41 +08:00
|
|
|
|
2003-03-03 23:54:49 +08:00
|
|
|
n = i;
|
|
|
|
|
|
|
|
/* Allocate the result vector */
|
|
|
|
switch (b->choice) {
|
|
|
|
case ChArray:
|
2003-07-08 11:46:20 +08:00
|
|
|
*b->res.c = ber_memalloc_x( (n+1)*sizeof( char * ),
|
|
|
|
b->ber->ber_memctx);
|
|
|
|
if ( *b->res.c == NULL ) return LBER_DEFAULT;
|
2003-03-03 23:54:49 +08:00
|
|
|
(*b->res.c)[n] = NULL;
|
|
|
|
break;
|
|
|
|
case BvArray:
|
2003-07-08 11:46:20 +08:00
|
|
|
*b->res.ba = ber_memalloc_x( (n+1)*sizeof( struct berval ),
|
|
|
|
b->ber->ber_memctx);
|
|
|
|
if ( *b->res.ba == NULL ) return LBER_DEFAULT;
|
2003-03-03 23:54:49 +08:00
|
|
|
(*b->res.ba)[n].bv_val = NULL;
|
|
|
|
break;
|
|
|
|
case BvVec:
|
2003-07-08 11:46:20 +08:00
|
|
|
*b->res.bv = ber_memalloc_x( (n+1)*sizeof( struct berval *),
|
|
|
|
b->ber->ber_memctx);
|
|
|
|
if ( *b->res.bv == NULL ) return LBER_DEFAULT;
|
2003-03-03 23:54:49 +08:00
|
|
|
(*b->res.bv)[n] = NULL;
|
|
|
|
break;
|
|
|
|
case BvOff:
|
2003-04-09 21:25:11 +08:00
|
|
|
*b->res.ba = ber_memalloc_x( (n+1) * b->siz, b->ber->ber_memctx );
|
2003-07-08 11:46:20 +08:00
|
|
|
if ( *b->res.ba == NULL ) return LBER_DEFAULT;
|
2003-03-03 23:54:49 +08:00
|
|
|
((struct berval *)((long)(*b->res.ba) + n*b->siz +
|
|
|
|
b->off))->bv_val = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
b->ber->ber_ptr = orig;
|
2003-03-12 10:33:53 +08:00
|
|
|
b->ber->ber_tag = tag;
|
2003-03-03 23:54:49 +08:00
|
|
|
ber_skip_tag( b->ber, &len );
|
2002-01-03 11:30:55 +08:00
|
|
|
|
2003-03-03 23:54:49 +08:00
|
|
|
for (n=0; n<i; n++)
|
2002-01-03 10:53:41 +08:00
|
|
|
{
|
2003-03-03 23:54:49 +08:00
|
|
|
tag = ber_next_element( b->ber, &len, last );
|
2003-07-08 11:46:20 +08:00
|
|
|
if ( ber_get_stringbv( b->ber, &bv, b->alloc ) == LBER_DEFAULT ) {
|
2003-03-03 23:54:49 +08:00
|
|
|
goto nomem;
|
2003-07-08 11:46:20 +08:00
|
|
|
}
|
2003-03-03 23:54:49 +08:00
|
|
|
|
2002-01-03 10:53:41 +08:00
|
|
|
/* store my result */
|
|
|
|
switch (b->choice) {
|
|
|
|
case ChArray:
|
|
|
|
(*b->res.c)[n] = bv.bv_val;
|
|
|
|
break;
|
|
|
|
case BvArray:
|
|
|
|
(*b->res.ba)[n] = bv;
|
|
|
|
break;
|
|
|
|
case BvVec:
|
2003-04-09 21:25:11 +08:00
|
|
|
bvp = ber_memalloc_x( sizeof( struct berval ), b->ber->ber_memctx);
|
2003-03-03 23:54:49 +08:00
|
|
|
if ( !bvp ) {
|
|
|
|
LBER_FREE(bv.bv_val);
|
|
|
|
goto nomem;
|
|
|
|
}
|
2002-01-03 11:11:14 +08:00
|
|
|
(*b->res.bv)[n] = bvp;
|
|
|
|
*bvp = bv;
|
2002-01-03 10:53:41 +08:00
|
|
|
break;
|
2002-01-03 13:36:26 +08:00
|
|
|
case BvOff:
|
2002-01-14 09:43:17 +08:00
|
|
|
*(BerVarray)((long)(*b->res.ba)+n*b->siz+b->off) = bv;
|
2003-03-03 23:54:49 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tag;
|
2003-07-08 11:46:20 +08:00
|
|
|
|
2003-03-03 23:54:49 +08:00
|
|
|
nomem:
|
2003-07-08 11:46:20 +08:00
|
|
|
if (b->alloc || b->choice == BvVec) {
|
|
|
|
for (--n; n>=0; n--) {
|
2003-03-03 23:54:49 +08:00
|
|
|
switch(b->choice) {
|
2003-07-08 11:46:20 +08:00
|
|
|
case ChArray:
|
|
|
|
LBER_FREE((*b->res.c)[n]);
|
|
|
|
break;
|
|
|
|
case BvArray:
|
|
|
|
LBER_FREE((*b->res.ba)[n].bv_val);
|
|
|
|
break;
|
|
|
|
case BvVec:
|
|
|
|
LBER_FREE((*b->res.bv)[n]->bv_val);
|
|
|
|
LBER_FREE((*b->res.bv)[n]);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2003-03-03 23:54:49 +08:00
|
|
|
}
|
2002-01-03 10:53:41 +08:00
|
|
|
}
|
|
|
|
}
|
2003-03-03 23:54:49 +08:00
|
|
|
LBER_FREE(*b->res.c);
|
|
|
|
*b->res.c = NULL;
|
|
|
|
return LBER_DEFAULT;
|
2002-01-03 10:53:41 +08:00
|
|
|
}
|
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_tag_t
|
2002-01-06 13:36:07 +08:00
|
|
|
ber_get_stringbv( BerElement *ber, struct berval *bv, int alloc )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_tag_t tag;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-21 05:21:57 +08:00
|
|
|
assert( ber != NULL );
|
2001-12-31 22:43:54 +08:00
|
|
|
assert( bv != NULL );
|
1999-05-21 05:21:57 +08:00
|
|
|
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-29 03:33:05 +08:00
|
|
|
|
2001-12-31 22:43:54 +08:00
|
|
|
if ( (tag = ber_skip_tag( ber, &bv->bv_len )) == LBER_DEFAULT ) {
|
|
|
|
bv->bv_val = NULL;
|
2000-10-11 10:43:14 +08:00
|
|
|
return LBER_DEFAULT;
|
1998-12-26 17:05:57 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2002-01-08 05:48:17 +08:00
|
|
|
if ( (ber_len_t) ber_pvt_ber_remaining( ber ) < bv->bv_len ) {
|
2000-10-11 10:43:14 +08:00
|
|
|
return LBER_DEFAULT;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2002-01-06 13:36:07 +08:00
|
|
|
if ( alloc ) {
|
2003-07-08 11:46:20 +08:00
|
|
|
bv->bv_val = (char *) ber_memalloc_x( bv->bv_len + 1,
|
|
|
|
ber->ber_memctx );
|
|
|
|
if ( bv->bv_val == NULL ) {
|
2002-01-06 13:36:07 +08:00
|
|
|
return LBER_DEFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( bv->bv_len > 0 && (ber_len_t) ber_read( ber, bv->bv_val,
|
2003-07-08 11:46:20 +08:00
|
|
|
bv->bv_len ) != bv->bv_len )
|
|
|
|
{
|
2002-01-06 13:36:07 +08:00
|
|
|
LBER_FREE( bv->bv_val );
|
|
|
|
bv->bv_val = NULL;
|
|
|
|
return LBER_DEFAULT;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bv->bv_val = ber->ber_ptr;
|
|
|
|
ber->ber_ptr += bv->bv_len;
|
1998-12-26 17:05:57 +08:00
|
|
|
}
|
2002-01-06 11:05:02 +08:00
|
|
|
ber->ber_tag = *(unsigned char *)ber->ber_ptr;
|
2002-01-06 13:36:07 +08:00
|
|
|
bv->bv_val[bv->bv_len] = '\0';
|
2001-12-31 22:43:54 +08:00
|
|
|
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
2005-11-13 02:42:03 +08:00
|
|
|
ber_tag_t
|
|
|
|
ber_get_stringbv_null( BerElement *ber, struct berval *bv, int alloc )
|
|
|
|
{
|
|
|
|
ber_tag_t tag;
|
|
|
|
|
|
|
|
assert( ber != NULL );
|
|
|
|
assert( bv != NULL );
|
|
|
|
|
|
|
|
assert( LBER_VALID( ber ) );
|
|
|
|
|
|
|
|
if ( (tag = ber_skip_tag( ber, &bv->bv_len )) == LBER_DEFAULT ) {
|
|
|
|
bv->bv_val = NULL;
|
|
|
|
return LBER_DEFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( (ber_len_t) ber_pvt_ber_remaining( ber ) < bv->bv_len ) {
|
|
|
|
return LBER_DEFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( bv->bv_len == 0 ) {
|
|
|
|
bv->bv_val = NULL;
|
|
|
|
ber->ber_tag = *(unsigned char *)ber->ber_ptr;
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( alloc ) {
|
|
|
|
bv->bv_val = (char *) ber_memalloc_x( bv->bv_len + 1,
|
|
|
|
ber->ber_memctx );
|
|
|
|
if ( bv->bv_val == NULL ) {
|
|
|
|
return LBER_DEFAULT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( bv->bv_len > 0 && (ber_len_t) ber_read( ber, bv->bv_val,
|
|
|
|
bv->bv_len ) != bv->bv_len )
|
|
|
|
{
|
|
|
|
LBER_FREE( bv->bv_val );
|
|
|
|
bv->bv_val = NULL;
|
|
|
|
return LBER_DEFAULT;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bv->bv_val = ber->ber_ptr;
|
|
|
|
ber->ber_ptr += bv->bv_len;
|
|
|
|
}
|
|
|
|
ber->ber_tag = *(unsigned char *)ber->ber_ptr;
|
|
|
|
bv->bv_val[bv->bv_len] = '\0';
|
|
|
|
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
2001-12-31 22:43:54 +08:00
|
|
|
ber_tag_t
|
|
|
|
ber_get_stringa( BerElement *ber, char **buf )
|
|
|
|
{
|
|
|
|
BerValue bv;
|
|
|
|
ber_tag_t tag;
|
|
|
|
|
|
|
|
assert( buf != NULL );
|
|
|
|
|
2002-01-06 13:36:07 +08:00
|
|
|
tag = ber_get_stringbv( ber, &bv, 1 );
|
2001-12-31 22:43:54 +08:00
|
|
|
*buf = bv.bv_val;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return tag;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2005-11-13 02:42:03 +08:00
|
|
|
ber_tag_t
|
|
|
|
ber_get_stringa_null( BerElement *ber, char **buf )
|
|
|
|
{
|
|
|
|
BerValue bv;
|
|
|
|
ber_tag_t tag;
|
|
|
|
|
|
|
|
assert( buf != NULL );
|
|
|
|
|
|
|
|
tag = ber_get_stringbv_null( ber, &bv, 1 );
|
|
|
|
*buf = bv.bv_val;
|
|
|
|
|
|
|
|
return tag;
|
|
|
|
}
|
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_tag_t
|
1998-08-09 08:43:13 +08:00
|
|
|
ber_get_stringal( BerElement *ber, struct berval **bv )
|
|
|
|
{
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_tag_t tag;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-21 05:21:57 +08:00
|
|
|
assert( ber != NULL );
|
|
|
|
assert( bv != NULL );
|
|
|
|
|
2003-07-08 11:46:20 +08:00
|
|
|
*bv = (struct berval *) ber_memalloc_x( sizeof(struct berval),
|
|
|
|
ber->ber_memctx );
|
2000-10-03 01:43:06 +08:00
|
|
|
if ( *bv == NULL ) {
|
|
|
|
return LBER_DEFAULT;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2002-01-06 13:36:07 +08:00
|
|
|
tag = ber_get_stringbv( ber, *bv, 1 );
|
2001-12-31 22:43:54 +08:00
|
|
|
if ( tag == LBER_DEFAULT ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
LBER_FREE( *bv );
|
1998-12-26 17:05:57 +08:00
|
|
|
*bv = NULL;
|
|
|
|
}
|
2000-10-11 10:43:14 +08:00
|
|
|
return tag;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_tag_t
|
|
|
|
ber_get_bitstringa(
|
|
|
|
BerElement *ber,
|
|
|
|
char **buf,
|
|
|
|
ber_len_t *blen )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_len_t datalen;
|
|
|
|
ber_tag_t tag;
|
1998-08-09 08:43:13 +08:00
|
|
|
unsigned char unusedbits;
|
|
|
|
|
1999-05-21 05:21:57 +08:00
|
|
|
assert( ber != NULL );
|
|
|
|
assert( buf != NULL );
|
|
|
|
assert( blen != NULL );
|
|
|
|
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-29 03:33:05 +08:00
|
|
|
|
1998-12-26 17:05:57 +08:00
|
|
|
if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) {
|
|
|
|
*buf = NULL;
|
2000-10-11 10:43:14 +08:00
|
|
|
return LBER_DEFAULT;
|
1998-12-26 17:05:57 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
--datalen;
|
|
|
|
|
2003-07-08 11:46:20 +08:00
|
|
|
*buf = (char *) ber_memalloc_x( datalen, ber->ber_memctx );
|
|
|
|
if ( *buf == NULL ) {
|
2000-10-11 10:43:14 +08:00
|
|
|
return LBER_DEFAULT;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-12-26 17:05:57 +08:00
|
|
|
if ( ber_read( ber, (char *)&unusedbits, 1 ) != 1 ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
LBER_FREE( buf );
|
1998-12-26 17:05:57 +08:00
|
|
|
*buf = NULL;
|
2000-10-11 10:43:14 +08:00
|
|
|
return LBER_DEFAULT;
|
1998-12-26 17:05:57 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-08-02 09:50:08 +08:00
|
|
|
if ( (ber_len_t) ber_read( ber, *buf, datalen ) != datalen ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
LBER_FREE( buf );
|
1998-12-26 17:05:57 +08:00
|
|
|
*buf = NULL;
|
2000-10-11 10:43:14 +08:00
|
|
|
return LBER_DEFAULT;
|
1998-12-26 17:05:57 +08:00
|
|
|
}
|
2002-01-06 11:05:02 +08:00
|
|
|
ber->ber_tag = *(unsigned char *)ber->ber_ptr;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
*blen = datalen * 8 - unusedbits;
|
2000-10-11 10:43:14 +08:00
|
|
|
return tag;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_tag_t
|
1998-08-09 08:43:13 +08:00
|
|
|
ber_get_null( BerElement *ber )
|
|
|
|
{
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_len_t len;
|
|
|
|
ber_tag_t tag;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-21 05:21:57 +08:00
|
|
|
assert( ber != NULL );
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-21 05:21:57 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT ) {
|
|
|
|
return LBER_DEFAULT;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
if ( len != 0 ) {
|
|
|
|
return LBER_DEFAULT;
|
|
|
|
}
|
2002-01-06 11:05:02 +08:00
|
|
|
ber->ber_tag = *(unsigned char *)ber->ber_ptr;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
return( tag );
|
|
|
|
}
|
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_tag_t
|
|
|
|
ber_get_boolean(
|
|
|
|
BerElement *ber,
|
|
|
|
ber_int_t *boolval )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_int_t longbool;
|
|
|
|
ber_tag_t rc;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-21 05:21:57 +08:00
|
|
|
assert( ber != NULL );
|
|
|
|
assert( boolval != NULL );
|
|
|
|
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-29 03:33:05 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
rc = ber_get_int( ber, &longbool );
|
|
|
|
*boolval = longbool;
|
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return rc;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_tag_t
|
|
|
|
ber_first_element(
|
|
|
|
BerElement *ber,
|
|
|
|
ber_len_t *len,
|
|
|
|
char **last )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-05-21 05:21:57 +08:00
|
|
|
assert( ber != NULL );
|
|
|
|
assert( len != NULL );
|
|
|
|
assert( last != NULL );
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/* skip the sequence header, use the len to mark where to stop */
|
|
|
|
if ( ber_skip_tag( ber, len ) == LBER_DEFAULT ) {
|
1999-11-09 03:36:30 +08:00
|
|
|
*last = NULL;
|
2000-10-11 10:43:14 +08:00
|
|
|
return LBER_DEFAULT;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
2002-01-06 11:05:02 +08:00
|
|
|
ber->ber_tag = *(unsigned char *)ber->ber_ptr;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
*last = ber->ber_ptr + *len;
|
|
|
|
|
|
|
|
if ( *last == ber->ber_ptr ) {
|
2000-10-11 10:43:14 +08:00
|
|
|
return LBER_DEFAULT;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return ber_peek_tag( ber, len );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_tag_t
|
|
|
|
ber_next_element(
|
|
|
|
BerElement *ber,
|
|
|
|
ber_len_t *len,
|
1999-11-08 23:38:59 +08:00
|
|
|
LDAP_CONST char *last )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-05-21 05:21:57 +08:00
|
|
|
assert( ber != NULL );
|
|
|
|
assert( len != NULL );
|
|
|
|
assert( last != NULL );
|
|
|
|
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-29 03:33:05 +08:00
|
|
|
|
2003-01-23 10:19:59 +08:00
|
|
|
if ( ber->ber_ptr >= last ) {
|
2000-10-11 10:43:14 +08:00
|
|
|
return LBER_DEFAULT;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return ber_peek_tag( ber, len );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* VARARGS */
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_tag_t
|
1999-08-24 08:27:53 +08:00
|
|
|
ber_scanf ( BerElement *ber,
|
1999-05-19 09:12:33 +08:00
|
|
|
LDAP_CONST char *fmt,
|
|
|
|
... )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
va_list ap;
|
1999-05-19 09:12:33 +08:00
|
|
|
LDAP_CONST char *fmt_reset;
|
2002-01-04 04:04:31 +08:00
|
|
|
char *s, **ss;
|
|
|
|
struct berval **bvp, *bval;
|
|
|
|
ber_int_t *i;
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_len_t *l;
|
|
|
|
ber_tag_t *t;
|
2002-01-04 04:04:31 +08:00
|
|
|
ber_tag_t rc;
|
1999-06-19 07:53:05 +08:00
|
|
|
ber_len_t len;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
va_start( ap, fmt );
|
1999-05-19 09:12:33 +08:00
|
|
|
|
|
|
|
assert( ber != NULL );
|
|
|
|
assert( fmt != NULL );
|
|
|
|
|
2001-07-22 07:13:04 +08:00
|
|
|
assert( LBER_VALID( ber ) );
|
1999-05-29 03:33:05 +08:00
|
|
|
|
1998-12-26 17:05:57 +08:00
|
|
|
fmt_reset = fmt;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-21 03:38:03 +08:00
|
|
|
ber_log_printf( LDAP_DEBUG_TRACE, ber->ber_debug,
|
|
|
|
"ber_scanf fmt (%s) ber:\n", fmt );
|
|
|
|
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;
|
|
|
|
|
2005-11-13 02:42:03 +08:00
|
|
|
case 'A': /* octet string - allocate storage as needed,
|
|
|
|
* but return NULL if len == 0 */
|
|
|
|
ss = va_arg( ap, char ** );
|
|
|
|
rc = ber_get_stringa_null( ber, ss );
|
|
|
|
break;
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
case 'b': /* boolean */
|
1999-06-19 07:53:05 +08:00
|
|
|
i = va_arg( ap, ber_int_t * );
|
1998-08-09 08:43:13 +08:00
|
|
|
rc = ber_get_boolean( ber, i );
|
|
|
|
break;
|
|
|
|
|
2005-11-13 02:42:03 +08:00
|
|
|
case 'B': /* bit string - allocate storage as needed */
|
|
|
|
ss = va_arg( ap, char ** );
|
|
|
|
l = va_arg( ap, ber_len_t * ); /* for length, in bits */
|
|
|
|
rc = ber_get_bitstringa( ber, ss, l );
|
|
|
|
break;
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
case 'e': /* enumerated */
|
|
|
|
case 'i': /* int */
|
1999-06-19 07:53:05 +08:00
|
|
|
i = va_arg( ap, ber_int_t * );
|
|
|
|
rc = ber_get_int( ber, i );
|
1998-08-09 08:43:13 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l': /* length of next item */
|
1999-06-19 07:53:05 +08:00
|
|
|
l = va_arg( ap, ber_len_t * );
|
1999-05-20 04:38:02 +08:00
|
|
|
rc = ber_peek_tag( ber, l );
|
1998-08-09 08:43:13 +08:00
|
|
|
break;
|
|
|
|
|
2005-11-13 02:42:03 +08:00
|
|
|
case 'm': /* octet string in berval, in-place */
|
|
|
|
bval = va_arg( ap, struct berval * );
|
|
|
|
rc = ber_get_stringbv( ber, bval, 0 );
|
1998-08-09 08:43:13 +08:00
|
|
|
break;
|
|
|
|
|
2005-11-13 02:42:03 +08:00
|
|
|
case 'M': /* bvoffarray - must include address of
|
|
|
|
* a record len, and record offset.
|
|
|
|
* number of records will be returned thru
|
|
|
|
* len ptr on finish. parsed in-place.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
bgbvr cookie = { BvOff };
|
|
|
|
cookie.ber = ber;
|
|
|
|
cookie.res.ba = va_arg( ap, struct berval ** );
|
|
|
|
cookie.alloc = 0;
|
1999-06-19 07:53:05 +08:00
|
|
|
l = va_arg( ap, ber_len_t * );
|
2005-11-13 02:42:03 +08:00
|
|
|
cookie.siz = *l;
|
|
|
|
cookie.off = va_arg( ap, ber_len_t );
|
|
|
|
rc = ber_get_stringbvl( &cookie, l );
|
1998-08-09 08:43:13 +08:00
|
|
|
break;
|
2005-11-13 02:42:03 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
2005-11-13 02:42:03 +08:00
|
|
|
case 'n': /* null */
|
|
|
|
rc = ber_get_null( ber );
|
2002-01-06 13:36:07 +08:00
|
|
|
break;
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
case 'o': /* octet string in a supplied berval */
|
|
|
|
bval = va_arg( ap, struct berval * );
|
2002-01-06 13:36:07 +08:00
|
|
|
rc = ber_get_stringbv( ber, bval, 1 );
|
1998-08-09 08:43:13 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'O': /* octet string - allocate & include length */
|
|
|
|
bvp = va_arg( ap, struct berval ** );
|
|
|
|
rc = ber_get_stringal( ber, bvp );
|
|
|
|
break;
|
|
|
|
|
2005-11-13 02:42:03 +08:00
|
|
|
case 's': /* octet string - in a buffer */
|
|
|
|
s = va_arg( ap, char * );
|
|
|
|
l = va_arg( ap, ber_len_t * );
|
|
|
|
rc = ber_get_stringb( ber, s, l );
|
1998-08-09 08:43:13 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 't': /* tag of next item */
|
1999-06-19 07:53:05 +08:00
|
|
|
t = va_arg( ap, ber_tag_t * );
|
|
|
|
*t = rc = ber_peek_tag( ber, &len );
|
1998-08-09 08:43:13 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'T': /* skip tag of next item */
|
1999-06-19 07:53:05 +08:00
|
|
|
t = va_arg( ap, ber_tag_t * );
|
|
|
|
*t = rc = ber_skip_tag( ber, &len );
|
1998-08-09 08:43:13 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v': /* sequence of strings */
|
2001-12-31 22:43:54 +08:00
|
|
|
{
|
2002-01-12 10:23:04 +08:00
|
|
|
bgbvr cookie = { ChArray };
|
|
|
|
cookie.ber = ber;
|
2002-01-03 10:53:41 +08:00
|
|
|
cookie.res.c = va_arg( ap, char *** );
|
2002-01-06 13:36:07 +08:00
|
|
|
cookie.alloc = 1;
|
2003-03-03 23:54:49 +08:00
|
|
|
rc = ber_get_stringbvl( &cookie, NULL );
|
1998-08-09 08:43:13 +08:00
|
|
|
break;
|
2001-12-31 22:43:54 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
case 'V': /* sequence of strings + lengths */
|
2001-12-31 22:43:54 +08:00
|
|
|
{
|
2002-01-12 10:23:04 +08:00
|
|
|
bgbvr cookie = { BvVec };
|
|
|
|
cookie.ber = ber;
|
2002-01-03 10:53:41 +08:00
|
|
|
cookie.res.bv = va_arg( ap, struct berval *** );
|
2002-01-06 13:36:07 +08:00
|
|
|
cookie.alloc = 1;
|
2003-03-03 23:54:49 +08:00
|
|
|
rc = ber_get_stringbvl( &cookie, NULL );
|
2001-12-31 22:43:54 +08:00
|
|
|
break;
|
|
|
|
}
|
2001-11-04 06:07:41 +08:00
|
|
|
|
2001-12-31 22:43:54 +08:00
|
|
|
case 'W': /* bvarray */
|
|
|
|
{
|
2002-01-12 10:23:04 +08:00
|
|
|
bgbvr cookie = { BvArray };
|
|
|
|
cookie.ber = ber;
|
2002-01-03 10:53:41 +08:00
|
|
|
cookie.res.ba = va_arg( ap, struct berval ** );
|
2002-01-06 13:36:07 +08:00
|
|
|
cookie.alloc = 1;
|
2003-03-03 23:54:49 +08:00
|
|
|
rc = ber_get_stringbvl( &cookie, NULL );
|
2002-01-03 13:36:26 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
case 'x': /* skip the next element - whatever it is */
|
|
|
|
if ( (rc = ber_skip_tag( ber, &len )) == LBER_DEFAULT )
|
|
|
|
break;
|
|
|
|
ber->ber_ptr += len;
|
2002-01-06 11:05:02 +08:00
|
|
|
ber->ber_tag = *(unsigned char *)ber->ber_ptr;
|
1998-08-09 08:43:13 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case '{': /* begin sequence */
|
|
|
|
case '[': /* begin set */
|
2002-01-02 16:50:07 +08:00
|
|
|
if ( *(fmt + 1) != 'v' && *(fmt + 1) != 'V'
|
2002-01-06 14:10:05 +08:00
|
|
|
&& *(fmt + 1) != 'W' && *(fmt + 1) != 'M' )
|
1998-08-09 08:43:13 +08:00
|
|
|
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 ) {
|
2003-07-08 11:46:20 +08:00
|
|
|
/*
|
|
|
|
* Error. Reclaim malloced memory that was given to the caller.
|
|
|
|
* Set allocated pointers to NULL, "data length" outvalues to 0.
|
|
|
|
*/
|
|
|
|
va_start( ap, fmt );
|
1998-12-26 17:05:57 +08:00
|
|
|
|
2003-07-08 11:46:20 +08:00
|
|
|
for ( ; fmt_reset < fmt; fmt_reset++ ) {
|
1998-12-26 17:05:57 +08:00
|
|
|
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 */
|
2005-11-13 02:42:03 +08:00
|
|
|
case 'A':
|
1998-12-26 17:05:57 +08:00
|
|
|
ss = va_arg( ap, char ** );
|
|
|
|
if ( *ss ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
LBER_FREE( *ss );
|
1998-12-26 17:05:57 +08:00
|
|
|
*ss = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'b': /* boolean */
|
1999-06-19 07:53:05 +08:00
|
|
|
case 'e': /* enumerated */
|
|
|
|
case 'i': /* int */
|
1998-12-26 17:05:57 +08:00
|
|
|
(void) va_arg( ap, int * );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l': /* length of next item */
|
1999-06-19 07:53:05 +08:00
|
|
|
(void) va_arg( ap, ber_len_t * );
|
|
|
|
break;
|
|
|
|
|
1998-12-26 17:05:57 +08:00
|
|
|
case 'o': /* octet string in a supplied berval */
|
|
|
|
bval = va_arg( ap, struct berval * );
|
1999-05-21 03:38:03 +08:00
|
|
|
if ( bval->bv_val != NULL ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
LBER_FREE( bval->bv_val );
|
1998-12-26 17:05:57 +08:00
|
|
|
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;
|
|
|
|
|
2005-11-13 02:42:03 +08:00
|
|
|
case 's': /* octet string - in a buffer */
|
|
|
|
(void) va_arg( ap, char * );
|
|
|
|
(void) va_arg( ap, ber_len_t * );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 't': /* tag of next item */
|
|
|
|
case 'T': /* skip tag of next item */
|
|
|
|
(void) va_arg( ap, ber_tag_t * );
|
|
|
|
break;
|
|
|
|
|
1998-12-26 17:05:57 +08:00
|
|
|
case 'B': /* bit string - allocate storage as needed */
|
|
|
|
ss = va_arg( ap, char ** );
|
|
|
|
if ( *ss ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
LBER_FREE( *ss );
|
1998-12-26 17:05:57 +08:00
|
|
|
*ss = NULL;
|
|
|
|
}
|
1999-06-19 07:53:05 +08:00
|
|
|
*(va_arg( ap, ber_len_t * )) = 0; /* for length, in bits */
|
1998-12-26 17:05:57 +08:00
|
|
|
break;
|
|
|
|
|
2002-01-06 14:10:05 +08:00
|
|
|
case 'm': /* berval in-place */
|
|
|
|
case 'M': /* BVoff array in-place */
|
1998-12-26 17:05:57 +08:00
|
|
|
case 'n': /* null */
|
2005-11-13 02:42:03 +08:00
|
|
|
case 'v': /* sequence of strings */
|
|
|
|
case 'V': /* sequence of strings + lengths */
|
|
|
|
case 'W': /* BerVarray */
|
1998-12-26 17:05:57 +08:00
|
|
|
case 'x': /* skip the next element - whatever it is */
|
|
|
|
case '{': /* begin sequence */
|
|
|
|
case '[': /* begin set */
|
|
|
|
case '}': /* end sequence */
|
|
|
|
case ']': /* end set */
|
1999-05-20 04:38:02 +08:00
|
|
|
break;
|
1998-12-26 17:05:57 +08:00
|
|
|
|
1999-05-20 04:38:02 +08:00
|
|
|
default:
|
|
|
|
/* format should be good */
|
|
|
|
assert( 0 );
|
1998-12-26 17:05:57 +08:00
|
|
|
}
|
2003-07-08 11:46:20 +08:00
|
|
|
}
|
1998-12-26 17:05:57 +08:00
|
|
|
|
2003-07-08 11:46:20 +08:00
|
|
|
va_end( ap );
|
1998-12-26 17:05:57 +08:00
|
|
|
}
|
|
|
|
|
2000-10-11 10:43:14 +08:00
|
|
|
return rc;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|