/* decode.c - ber input decoding routines */
/* $OpenLDAP$ */
-/*
- * Copyright 1998-2003 The OpenLDAP Foundation, All Rights Reserved.
- * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
+/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
+ *
+ * Copyright 1998-2005 The OpenLDAP Foundation.
+ * 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>.
*/
-/* Portions
- * Copyright (c) 1990 Regents of the University of Michigan.
+/* Portions Copyright (c) 1990 Regents of the University of Michigan.
* All rights reserved.
*
* Redistribution and use in source and binary forms are permitted
* software without specific prior written permission. This software
* is provided ``as is'' without express or implied warranty.
*/
+/* ACKNOWLEDGEMENTS:
+ * This work was originally developed by the University of Michigan
+ * (as part of U-MICH LDAP).
+ */
#include "portable.h"
return LBER_DEFAULT;
}
- if ( ber->ber_ptr == ber->ber_buf )
+ if ( ber->ber_ptr == ber->ber_buf ) {
tag = *(unsigned char *)ber->ber_ptr;
- else
+ } else {
tag = ber->ber_tag;
+ }
ber->ber_ptr++;
if ( (tag & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK ) {
* greater than what we can hold in a ber_len_t.
*/
- if ( ber_read( ber, (char *) &lc, 1 ) != 1 )
+ if ( ber_read( ber, (char *) &lc, 1 ) != 1 ) {
return LBER_DEFAULT;
+ }
if ( lc & 0x80U ) {
noctets = (lc & 0x7fU);
return LBER_DEFAULT;
}
- if( (unsigned) ber_read( ber, netlen, noctets ) != noctets ) {
+ if( (unsigned) ber_read( ber, (char *) netlen, noctets ) != noctets ) {
return LBER_DEFAULT;
}
char *last, *orig;
struct berval bv, *bvp = NULL;
+ /* For rewinding, just like ber_peek_tag() */
orig = b->ber->ber_ptr;
+ tag = b->ber->ber_tag;
- tag = ber_first_element( b->ber, &len, &last );
- if ( tag != LBER_DEFAULT ) {
- for ( ; b->ber->ber_ptr < last; i++ )
- {
- tag = ber_skip_tag( b->ber, &len );
- if (tag == LBER_DEFAULT) break;
+ if ( ber_first_element( b->ber, &len, &last ) != LBER_DEFAULT ) {
+ for ( ; b->ber->ber_ptr < last; i++ ) {
+ if (ber_skip_tag( b->ber, &len ) == LBER_DEFAULT) break;
b->ber->ber_ptr += len;
+ b->ber->ber_tag = *(unsigned char *)b->ber->ber_ptr;
}
}
if ( rlen ) *rlen = i;
- if ( i == 0 )
- {
+ if ( i == 0 ) {
*b->res.c = NULL;
return 0;
}
/* Allocate the result vector */
switch (b->choice) {
case ChArray:
- *b->res.c = LBER_MALLOC( (n+1) * sizeof( char * ));
- if ( *b->res.c == NULL )
- return LBER_DEFAULT;
+ *b->res.c = ber_memalloc_x( (n+1)*sizeof( char * ),
+ b->ber->ber_memctx);
+ if ( *b->res.c == NULL ) return LBER_DEFAULT;
(*b->res.c)[n] = NULL;
break;
case BvArray:
- *b->res.ba = LBER_MALLOC( (n+1) * sizeof( struct berval ));
- if ( *b->res.ba == NULL )
- return LBER_DEFAULT;
+ *b->res.ba = ber_memalloc_x( (n+1)*sizeof( struct berval ),
+ b->ber->ber_memctx);
+ if ( *b->res.ba == NULL ) return LBER_DEFAULT;
(*b->res.ba)[n].bv_val = NULL;
break;
case BvVec:
- *b->res.bv = LBER_MALLOC( (n+1) * sizeof( struct berval *));
- if ( *b->res.bv == NULL )
- return LBER_DEFAULT;
+ *b->res.bv = ber_memalloc_x( (n+1)*sizeof( struct berval *),
+ b->ber->ber_memctx);
+ if ( *b->res.bv == NULL ) return LBER_DEFAULT;
(*b->res.bv)[n] = NULL;
break;
case BvOff:
- *b->res.ba = LBER_MALLOC( (n+1) * b->siz );
- if ( *b->res.ba == NULL )
- return LBER_DEFAULT;
+ *b->res.ba = ber_memalloc_x( (n+1) * b->siz, b->ber->ber_memctx );
+ if ( *b->res.ba == NULL ) return LBER_DEFAULT;
((struct berval *)((long)(*b->res.ba) + n*b->siz +
b->off))->bv_val = NULL;
break;
}
b->ber->ber_ptr = orig;
+ b->ber->ber_tag = tag;
ber_skip_tag( b->ber, &len );
for (n=0; n<i; n++)
{
tag = ber_next_element( b->ber, &len, last );
- if ( ber_get_stringbv( b->ber, &bv, b->alloc ) == LBER_DEFAULT )
+ if ( ber_get_stringbv( b->ber, &bv, b->alloc ) == LBER_DEFAULT ) {
goto nomem;
+ }
/* store my result */
switch (b->choice) {
(*b->res.ba)[n] = bv;
break;
case BvVec:
- bvp = LBER_MALLOC( sizeof( struct berval ));
+ bvp = ber_memalloc_x( sizeof( struct berval ), b->ber->ber_memctx);
if ( !bvp ) {
LBER_FREE(bv.bv_val);
goto nomem;
}
}
return tag;
+
nomem:
- if (b->alloc || b->choice == BvVec)
- {
- for (--n; n>=0; n--)
- {
+ if (b->alloc || b->choice == BvVec) {
+ for (--n; n>=0; n--) {
switch(b->choice) {
- 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;
+ 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;
}
}
}
}
if ( alloc ) {
- if ( (bv->bv_val = (char *) LBER_MALLOC( bv->bv_len + 1 )) == NULL ) {
+ 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 ) {
+ bv->bv_len ) != bv->bv_len )
+ {
LBER_FREE( bv->bv_val );
bv->bv_val = NULL;
return LBER_DEFAULT;
assert( ber != NULL );
assert( bv != NULL );
- *bv = (struct berval *) LBER_MALLOC( sizeof(struct berval) );
+ *bv = (struct berval *) ber_memalloc_x( sizeof(struct berval),
+ ber->ber_memctx );
if ( *bv == NULL ) {
return LBER_DEFAULT;
}
}
--datalen;
- if ( (*buf = (char *) LBER_MALLOC( datalen )) == NULL ) {
+ *buf = (char *) ber_memalloc_x( datalen, ber->ber_memctx );
+ if ( *buf == NULL ) {
return LBER_DEFAULT;
}
fmt_reset = fmt;
-#ifdef NEW_LOGGING
- LDAP_LOG( BER, ENTRY, "ber_scanf fmt (%s) ber:\n", fmt, 0, 0 );
-
- if ( LDAP_LOGS_TEST(BER, DETAIL2 ))
- BER_DUMP(( "liblber", LDAP_LEVEL_DETAIL2, ber, 1 ));
-#else
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 );
-#endif
for ( rc = 0; *fmt && rc != LBER_DEFAULT; fmt++ ) {
/* When this is modified, remember to update
default:
if( ber->ber_debug ) {
-#ifdef NEW_LOGGING
- LDAP_LOG( BER, ERR,
- "ber_scanf: unknown fmt %c\n", *fmt, 0, 0 );
-#else
ber_log_printf( LDAP_DEBUG_ANY, ber->ber_debug,
"ber_scanf: unknown fmt %c\n", *fmt );
-#endif
}
rc = LBER_DEFAULT;
break;
va_end( ap );
if ( rc == LBER_DEFAULT ) {
- /*
- * Error. Reclaim malloced memory that was given to the caller.
- * Set allocated pointers to NULL, "data length" outvalues to 0.
- */
- va_start( ap, fmt );
+ /*
+ * Error. Reclaim malloced memory that was given to the caller.
+ * Set allocated pointers to NULL, "data length" outvalues to 0.
+ */
+ va_start( ap, fmt );
- for ( ; fmt_reset < fmt; fmt_reset++ ) {
+ for ( ; fmt_reset < fmt; fmt_reset++ ) {
switch ( *fmt_reset ) {
case '!': { /* Hook */
BERDecodeCallback *f;
/* format should be good */
assert( 0 );
}
- }
+ }
- va_end( ap );
+ va_end( ap );
}
return rc;