X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libraries%2Fliblber%2Fdecode.c;h=04ba7cabc65b8dd87cc01ada0946cab6cd1576c3;hb=fde95747cb6aa9e2b71e4c81f31a2c5dc449cc73;hp=c01c2b209cd25fb5aa274cdfab1f158375e93579;hpb=6939c531700652491f4be4688c6a1f35a1ab8a18;p=openldap diff --git a/libraries/liblber/decode.c b/libraries/liblber/decode.c index c01c2b209c..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; } @@ -141,11 +155,6 @@ ber_skip_tag( BerElement *ber, ber_len_t *len ) *len = lc; } - /* BER length should be non-negative */ - if( *len < 0 ) { - return LBER_DEFAULT; - } - /* BER element should have enough data left */ if( *len > (ber_len_t) ber_pvt_ber_remaining( ber ) ) { return LBER_DEFAULT; @@ -287,7 +296,7 @@ ber_get_stringb( return tag; } -/* Definitions for recursive get_string +/* Definitions for get_string vector * * ChArray, BvArray, and BvVec are self-explanatory. * BvOff is a struct berval embedded in an array of larger structures @@ -301,9 +310,6 @@ enum bgbvc { ChArray, BvArray, BvVec, BvOff }; typedef struct bgbvr { enum bgbvc choice; BerElement *ber; - ber_tag_t tag; - ber_len_t len; - char *last; int alloc; ber_len_t siz; ber_len_t off; @@ -314,74 +320,74 @@ typedef struct bgbvr { } res; } bgbvr; -/* Recursive get_string, for decoding a vector of strings. The number - * of elements in the vector is limited only by available stack space. - * Each invocation consumes 24 bytes of stack on a 32-bit machine. - */ static ber_tag_t -ber_get_stringbvr( bgbvr *b, int n ) +ber_get_stringbvl( bgbvr *b, ber_len_t *rlen ) { + int i = 0, n; + ber_tag_t tag; + ber_len_t len; + char *last, *orig; struct berval bv, *bvp = NULL; - if ( n ) - b->tag = ber_next_element( b->ber, &b->len, b->last ); - else - b->tag = ber_first_element( b->ber, &b->len, &b->last ); + /* For rewinding, just like ber_peek_tag() */ + orig = b->ber->ber_ptr; + tag = b->ber->ber_tag; - if ( b->tag == LBER_DEFAULT ) - { - b->len = n; - - if ( n == 0 ) { - *b->res.c = NULL; - return 0; + 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; } - /* 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)[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)[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)[n] = NULL; - break; - case BvOff: - *b->res.ba = LBER_MALLOC( (n+1) * b->siz ); - if ( *b->res.ba == NULL ) - return LBER_DEFAULT; - ((struct berval *)((long)(*b->res.ba) + n*b->siz + - b->off))->bv_val = NULL; - break; - } - return 0; } - /* Do all local allocs before the recursion. Then there - * cannot possibly be any failures on the return trip. - */ - if ( b->choice == BvVec ) - bvp = LBER_MALLOC( sizeof( struct berval )); + if ( rlen ) *rlen = i; - if ( ber_get_stringbv( b->ber, &bv, b->alloc ) == LBER_DEFAULT ) { - if ( bvp ) LBER_FREE( bvp ); - return LBER_DEFAULT; + if ( i == 0 ) { + *b->res.c = NULL; + return 0; } - b->tag = ber_get_stringbvr( b, n+1 ); + n = i; + + /* Allocate the result vector */ + switch (b->choice) { + case ChArray: + *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 = 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 = 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 = 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 ); - if ( b->tag == 0 ) + for (n=0; nber, &len, last ); + if ( ber_get_stringbv( b->ber, &bv, b->alloc ) == LBER_DEFAULT ) { + goto nomem; + } + /* store my result */ switch (b->choice) { case ChArray: @@ -391,20 +397,43 @@ ber_get_stringbvr( bgbvr *b, int n ) (*b->res.ba)[n] = bv; break; case BvVec: + bvp = ber_memalloc_x( sizeof( struct berval ), b->ber->ber_memctx); + if ( !bvp ) { + LBER_FREE(bv.bv_val); + goto nomem; + } (*b->res.bv)[n] = bvp; *bvp = bv; break; case BvOff: *(BerVarray)((long)(*b->res.ba)+n*b->siz+b->off) = bv; + break; } - } else { - /* Failure will propagate up and free in reverse - * order, which is actually ideal. - */ - if ( b->alloc ) LBER_FREE( bv.bv_val ); - if ( bvp ) LBER_FREE( bvp ); } - return b->tag; + return tag; + +nomem: + 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; + default: + break; + } + } + } + LBER_FREE(*b->res.c); + *b->res.c = NULL; + return LBER_DEFAULT; } ber_tag_t @@ -427,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; @@ -469,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; } @@ -504,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; } @@ -603,7 +637,7 @@ ber_next_element( assert( LBER_VALID( ber ) ); - if ( ber->ber_ptr == last ) { + if ( ber->ber_ptr >= last ) { return LBER_DEFAULT; } @@ -635,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 @@ -728,7 +755,7 @@ ber_scanf ( BerElement *ber, cookie.ber = ber; cookie.res.c = va_arg( ap, char *** ); cookie.alloc = 1; - rc = ber_get_stringbvr( &cookie, 0 ); + rc = ber_get_stringbvl( &cookie, NULL ); break; } @@ -738,7 +765,7 @@ ber_scanf ( BerElement *ber, cookie.ber = ber; cookie.res.bv = va_arg( ap, struct berval *** ); cookie.alloc = 1; - rc = ber_get_stringbvr( &cookie, 0 ); + rc = ber_get_stringbvl( &cookie, NULL ); break; } @@ -748,7 +775,7 @@ ber_scanf ( BerElement *ber, cookie.ber = ber; cookie.res.ba = va_arg( ap, struct berval ** ); cookie.alloc = 1; - rc = ber_get_stringbvr( &cookie, 0 ); + rc = ber_get_stringbvl( &cookie, NULL ); break; } @@ -765,8 +792,7 @@ ber_scanf ( BerElement *ber, l = va_arg( ap, ber_len_t * ); cookie.siz = *l; cookie.off = va_arg( ap, ber_len_t ); - rc = ber_get_stringbvr( &cookie, 0 ); - *l = cookie.len; + rc = ber_get_stringbvl( &cookie, l ); break; } @@ -790,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; @@ -805,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; @@ -894,9 +915,9 @@ ber_scanf ( BerElement *ber, /* format should be good */ assert( 0 ); } - } + } - va_end( ap ); + va_end( ap ); } return rc;