X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=libraries%2Fliblber%2Fdecode.c;h=04ba7cabc65b8dd87cc01ada0946cab6cd1576c3;hb=fde95747cb6aa9e2b71e4c81f31a2c5dc449cc73;hp=2123f28b6b2462c9d64fe49bec16d754efe73869;hpb=76386bde5ed8a834d25a5c62ebeb086f2abcf5e0;p=openldap diff --git a/libraries/liblber/decode.c b/libraries/liblber/decode.c index 2123f28b6b..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-2002 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 @@ -299,88 +308,86 @@ enum bgbvc { ChArray, BvArray, BvVec, BvOff }; * stack use to the absolute minimum. */ typedef struct bgbvr { - BerElement *ber; enum bgbvc choice; - ber_tag_t tag; - ber_len_t len; - char *last; + BerElement *ber; + int alloc; ber_len_t siz; ber_len_t off; union { char ***c; - BVarray *ba; + BerVarray *ba; struct berval ***bv; } 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. - */ -ber_tag_t -ber_get_stringbvr( bgbvr *b, int n ) +static ber_tag_t +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 ) == 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: @@ -390,24 +397,47 @@ 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: - *(BVarray)((long)(*b->res.ba)+n*b->siz+b->off) = bv; + *(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 ( bvp ) LBER_FREE( bvp ); - LBER_FREE( bv.bv_val ); } - 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 -ber_get_stringbv( BerElement *ber, struct berval *bv ) +ber_get_stringbv( BerElement *ber, struct berval *bv, int alloc ) { ber_tag_t tag; @@ -421,18 +451,30 @@ ber_get_stringbv( BerElement *ber, struct berval *bv ) return LBER_DEFAULT; } - if ( (bv->bv_val = (char *) LBER_MALLOC( bv->bv_len + 1 )) == NULL ) { + if ( (ber_len_t) ber_pvt_ber_remaining( ber ) < bv->bv_len ) { 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; + 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; } - bv->bv_val[bv->bv_len] = '\0'; ber->ber_tag = *(unsigned char *)ber->ber_ptr; + bv->bv_val[bv->bv_len] = '\0'; return tag; } @@ -445,7 +487,7 @@ ber_get_stringa( BerElement *ber, char **buf ) assert( buf != NULL ); - tag = ber_get_stringbv( ber, &bv ); + tag = ber_get_stringbv( ber, &bv, 1 ); *buf = bv.bv_val; return tag; @@ -459,12 +501,13 @@ 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; } - tag = ber_get_stringbv( ber, *bv ); + tag = ber_get_stringbv( ber, *bv, 1 ); if ( tag == LBER_DEFAULT ) { LBER_FREE( *bv ); *bv = NULL; @@ -494,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; } @@ -593,7 +637,7 @@ ber_next_element( assert( LBER_VALID( ber ) ); - if ( ber->ber_ptr == last ) { + if ( ber->ber_ptr >= last ) { return LBER_DEFAULT; } @@ -609,13 +653,6 @@ ber_scanf ( BerElement *ber, va_list ap; LDAP_CONST char *fmt_reset; char *s, **ss; -#ifdef TMP_SLOTS - char *last; - char ***sss; - ber_tag_t tag; - struct berval ***bv; - int j; -#endif struct berval **bvp, *bval; ber_int_t *i; ber_len_t *l; @@ -632,14 +669,9 @@ ber_scanf ( BerElement *ber, fmt_reset = fmt; -#ifdef NEW_LOGGING - LDAP_LOG(( "liblber", LDAP_LEVEL_ENTRY, "ber_scanf fmt (%s) ber:\n", fmt )); - 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 @@ -686,9 +718,14 @@ ber_scanf ( BerElement *ber, rc = ber_get_stringb( ber, s, l ); break; + case 'm': /* octet string in berval, in-place */ + bval = va_arg( ap, struct berval * ); + rc = ber_get_stringbv( ber, bval, 0 ); + break; + case 'o': /* octet string in a supplied berval */ bval = va_arg( ap, struct berval * ); - rc = ber_get_stringbv( ber, bval ); + rc = ber_get_stringbv( ber, bval, 1 ); break; case 'O': /* octet string - allocate & include length */ @@ -714,41 +751,48 @@ ber_scanf ( BerElement *ber, case 'v': /* sequence of strings */ { - bgbvr cookie = { ber, ChArray }; + bgbvr cookie = { ChArray }; + cookie.ber = ber; cookie.res.c = va_arg( ap, char *** ); - rc = ber_get_stringbvr( &cookie, 0 ); + cookie.alloc = 1; + rc = ber_get_stringbvl( &cookie, NULL ); break; } case 'V': /* sequence of strings + lengths */ { - bgbvr cookie = { ber, BvVec }; + bgbvr cookie = { BvVec }; + cookie.ber = ber; cookie.res.bv = va_arg( ap, struct berval *** ); - rc = ber_get_stringbvr( &cookie, 0 ); + cookie.alloc = 1; + rc = ber_get_stringbvl( &cookie, NULL ); break; } case 'W': /* bvarray */ { - bgbvr cookie = { ber, BvArray }; + bgbvr cookie = { BvArray }; + cookie.ber = ber; cookie.res.ba = va_arg( ap, struct berval ** ); - rc = ber_get_stringbvr( &cookie, 0 ); + cookie.alloc = 1; + rc = ber_get_stringbvl( &cookie, NULL ); break; } - case 'w': /* bvoffarray - must include address of + case 'M': /* bvoffarray - must include address of * a record len, and record offset. * number of records will be returned thru - * len ptr on finish. + * len ptr on finish. parsed in-place. */ { - bgbvr cookie = { ber, BvOff }; + bgbvr cookie = { BvOff }; + cookie.ber = ber; cookie.res.ba = va_arg( ap, struct berval ** ); + cookie.alloc = 0; 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; } @@ -762,7 +806,7 @@ ber_scanf ( BerElement *ber, case '{': /* begin sequence */ case '[': /* begin set */ if ( *(fmt + 1) != 'v' && *(fmt + 1) != 'V' - && *(fmt + 1) != 'W' && *(fmt + 1) != 'w' ) + && *(fmt + 1) != 'W' && *(fmt + 1) != 'M' ) rc = ber_skip_tag( ber, &len ); break; @@ -772,13 +816,8 @@ ber_scanf ( BerElement *ber, default: if( ber->ber_debug ) { -#ifdef NEW_LOGGING - LDAP_LOG(( "liblber", LDAP_LEVEL_ERR, - "ber_scanf: unknown fmt %c\n", *fmt )); -#else ber_log_printf( LDAP_DEBUG_ANY, ber->ber_debug, "ber_scanf: unknown fmt %c\n", *fmt ); -#endif } rc = LBER_DEFAULT; break; @@ -787,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; @@ -861,7 +900,9 @@ ber_scanf ( BerElement *ber, case 'v': /* sequence of strings */ case 'V': /* sequence of strings + lengths */ - case 'W': /* BVarray */ + case 'W': /* BerVarray */ + case 'm': /* berval in-place */ + case 'M': /* BVoff array in-place */ case 'n': /* null */ case 'x': /* skip the next element - whatever it is */ case '{': /* begin sequence */ @@ -874,9 +915,9 @@ ber_scanf ( BerElement *ber, /* format should be good */ assert( 0 ); } - } + } - va_end( ap ); + va_end( ap ); } return rc;