X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libraries%2Fliblber%2Fdecode.c;h=04ba7cabc65b8dd87cc01ada0946cab6cd1576c3;hb=fde95747cb6aa9e2b71e4c81f31a2c5dc449cc73;hp=33f33b4c6d5efb2ad252ef1fe949fb714b15efa7;hpb=3cc07977fa26133739e74116dc248e8ecb7cf9b3;p=openldap diff --git a/libraries/liblber/decode.c b/libraries/liblber/decode.c index 33f33b4c6d..04ba7cabc6 100644 --- a/libraries/liblber/decode.c +++ b/libraries/liblber/decode.c @@ -1,11 +1,19 @@ /* 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 . + * + * 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 + * . */ -/* 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 @@ -15,6 +23,10 @@ * 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" @@ -48,10 +60,11 @@ ber_get_tag( BerElement *ber ) 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 ) { @@ -118,8 +131,9 @@ ber_skip_tag( BerElement *ber, ber_len_t *len ) * 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); @@ -128,7 +142,7 @@ ber_skip_tag( BerElement *ber, ber_len_t *len ) return LBER_DEFAULT; } - if( (unsigned) ber_read( ber, netlen, noctets ) != noctets ) { + if( (unsigned) ber_read( ber, (char *) netlen, noctets ) != noctets ) { return LBER_DEFAULT; } @@ -315,22 +329,21 @@ ber_get_stringbvl( bgbvr *b, ber_len_t *rlen ) 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; } @@ -340,39 +353,40 @@ ber_get_stringbvl( bgbvr *b, ber_len_t *rlen ) /* 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; nber, &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) { @@ -383,7 +397,7 @@ ber_get_stringbvl( bgbvr *b, ber_len_t *rlen ) (*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; @@ -397,16 +411,23 @@ ber_get_stringbvl( bgbvr *b, ber_len_t *rlen ) } } 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; } } } @@ -435,12 +456,15 @@ ber_get_stringbv( BerElement *ber, struct berval *bv, int alloc ) } 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; @@ -477,7 +501,8 @@ ber_get_stringal( BerElement *ber, struct berval **bv ) 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; } @@ -512,7 +537,8 @@ ber_get_bitstringa( } --datalen; - if ( (*buf = (char *) LBER_MALLOC( datalen )) == NULL ) { + *buf = (char *) ber_memalloc_x( datalen, ber->ber_memctx ); + if ( *buf == NULL ) { return LBER_DEFAULT; } @@ -643,16 +669,9 @@ ber_scanf ( BerElement *ber, 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 @@ -797,13 +816,8 @@ ber_scanf ( BerElement *ber, 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; @@ -812,13 +826,13 @@ ber_scanf ( BerElement *ber, 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; @@ -901,9 +915,9 @@ ber_scanf ( BerElement *ber, /* format should be good */ assert( 0 ); } - } + } - va_end( ap ); + va_end( ap ); } return rc;