Diff for /libraries/liblber/decode.c between versions 1.80 and 1.102

version 1.80, 2002/01/06 04:36:07 version 1.102, 2005/01/01 19:49:43
Line 1 Line 1
 /* decode.c - ber input decoding routines */  /* decode.c - ber input decoding routines */
 /* $OpenLDAP: pkg/ldap/libraries/liblber/decode.c,v 1.78 2002/01/06 01:23:36 hyc Exp $ */  /* $OpenLDAP: pkg/ldap/libraries/liblber/decode.c,v 1.101 2004/09/04 02:54:28 kurt Exp $ */
 /*  /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.   *
  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file   * 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  /* Portions Copyright (c) 1990 Regents of the University of Michigan.
  * Copyright (c) 1990 Regents of the University of Michigan.  
  * All rights reserved.   * All rights reserved.
  *   *
  * Redistribution and use in source and binary forms are permitted   * Redistribution and use in source and binary forms are permitted
Line 15 Line 23
  * software without specific prior written permission. This software   * software without specific prior written permission. This software
  * is provided ``as is'' without express or implied warranty.   * 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"  #include "portable.h"
   
Line 48  ber_get_tag( BerElement *ber ) Line 60  ber_get_tag( BerElement *ber )
                 return LBER_DEFAULT;                  return LBER_DEFAULT;
         }          }
   
         if ( ber->ber_ptr == ber->ber_buf )          if ( ber->ber_ptr == ber->ber_buf ) {
                 tag = *(unsigned char *)ber->ber_ptr;                  tag = *(unsigned char *)ber->ber_ptr;
         else          } else {
                 tag = ber->ber_tag;                  tag = ber->ber_tag;
           }
         ber->ber_ptr++;          ber->ber_ptr++;
   
         if ( (tag & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK ) {          if ( (tag & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK ) {
Line 118  ber_skip_tag( BerElement *ber, ber_len_t Line 131  ber_skip_tag( BerElement *ber, ber_len_t
          * greater than what we can hold in a ber_len_t.           * 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;                  return LBER_DEFAULT;
           }
   
         if ( lc & 0x80U ) {          if ( lc & 0x80U ) {
                 noctets = (lc & 0x7fU);                  noctets = (lc & 0x7fU);
Line 128  ber_skip_tag( BerElement *ber, ber_len_t Line 142  ber_skip_tag( BerElement *ber, ber_len_t
                         return LBER_DEFAULT;                          return LBER_DEFAULT;
                 }                  }
   
                 if( (unsigned) ber_read( ber, netlen, noctets ) != noctets ) {                  if( (unsigned) ber_read( ber, (char *) netlen, noctets ) != noctets ) {
                         return LBER_DEFAULT;                          return LBER_DEFAULT;
                 }                  }
   
Line 141  ber_skip_tag( BerElement *ber, ber_len_t Line 155  ber_skip_tag( BerElement *ber, ber_len_t
                 *len = lc;                  *len = lc;
         }          }
   
         /* BER length should be non-negative */  
         if( *len < 0 ) {  
                 return LBER_DEFAULT;  
         }  
   
         /* BER element should have enough data left */          /* BER element should have enough data left */
         if( *len > (ber_len_t) ber_pvt_ber_remaining( ber ) ) {          if( *len > (ber_len_t) ber_pvt_ber_remaining( ber ) ) {
                 return LBER_DEFAULT;                  return LBER_DEFAULT;
Line 287  ber_get_stringb( Line 296  ber_get_stringb(
         return tag;          return tag;
 }  }
   
 /* Definitions for recursive get_string  /* Definitions for get_string vector
  *   *
  * ChArray, BvArray, and BvVec are self-explanatory.   * ChArray, BvArray, and BvVec are self-explanatory.
  * BvOff is a struct berval embedded in an array of larger structures   * BvOff is a struct berval embedded in an array of larger structures
Line 299  enum bgbvc { ChArray, BvArray, BvVec, Bv Line 308  enum bgbvc { ChArray, BvArray, BvVec, Bv
  * stack use to the absolute minimum.   * stack use to the absolute minimum.
  */   */
 typedef struct bgbvr {  typedef struct bgbvr {
         BerElement *ber;  
         enum bgbvc choice;          enum bgbvc choice;
         ber_tag_t tag;          BerElement *ber;
         ber_len_t len;  
         char *last;  
         int alloc;          int alloc;
         ber_len_t siz;          ber_len_t siz;
         ber_len_t off;          ber_len_t off;
         union {          union {
                 char ***c;                  char ***c;
                 BVarray *ba;                  BerVarray *ba;
                 struct berval ***bv;                  struct berval ***bv;
         } res;          } res;
 } bgbvr;  } 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  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;          struct berval bv, *bvp = NULL;
   
         if ( n )          /* For rewinding, just like ber_peek_tag() */
                 b->tag = ber_next_element( b->ber, &b->len, b->last );          orig = b->ber->ber_ptr;
         else          tag = b->ber->ber_tag;
                 b->tag = ber_first_element( b->ber, &b->len, &b->last );  
   
         if ( b->tag == LBER_DEFAULT )          if ( ber_first_element( b->ber, &len, &last ) != LBER_DEFAULT ) {
         {                  for ( ; b->ber->ber_ptr < last; i++ ) {
                 b->len = n;                          if (ber_skip_tag( b->ber, &len ) == LBER_DEFAULT) break;
                           b->ber->ber_ptr += len;
                 if ( n == 0 ) {                          b->ber->ber_tag = *(unsigned char *)b->ber->ber_ptr;
                         *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)[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          if ( rlen ) *rlen = i;
          * cannot possibly be any failures on the return trip.  
          */  
         if ( b->choice == BvVec )  
                 bvp = LBER_MALLOC( sizeof( struct berval ));  
   
         if ( ber_get_stringbv( b->ber, &bv, b->alloc ) == LBER_DEFAULT ) {          if ( i == 0 ) {
                 if ( bvp ) LBER_FREE( bvp );                  *b->res.c = NULL;
                 return LBER_DEFAULT;                  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; n<i; n++)
         {          {
                   tag = ber_next_element( b->ber, &len, last );
                   if ( ber_get_stringbv( b->ber, &bv, b->alloc ) == LBER_DEFAULT ) {
                           goto nomem;
                   }
   
                 /* store my result */                  /* store my result */
                 switch (b->choice) {                  switch (b->choice) {
                 case ChArray:                  case ChArray:
Line 391  ber_get_stringbvr( bgbvr *b, int n ) Line 397  ber_get_stringbvr( bgbvr *b, int n )
                         (*b->res.ba)[n] = bv;                          (*b->res.ba)[n] = bv;
                         break;                          break;
                 case BvVec:                  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;                          (*b->res.bv)[n] = bvp;
                         *bvp = bv;                          *bvp = bv;
                         break;                          break;
                 case BvOff:                  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 ( 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  ber_tag_t
Line 422  ber_get_stringbv( BerElement *ber, struc Line 451  ber_get_stringbv( BerElement *ber, struc
                 return LBER_DEFAULT;                  return LBER_DEFAULT;
         }          }
   
         if ( ber_pvt_ber_remaining( ber ) < bv->bv_len ) {          if ( (ber_len_t) ber_pvt_ber_remaining( ber ) < bv->bv_len ) {
                 return LBER_DEFAULT;                  return LBER_DEFAULT;
         }          }
   
         if ( 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;                          return LBER_DEFAULT;
                 }                  }
   
                 if ( bv->bv_len > 0 && (ber_len_t) ber_read( ber, bv->bv_val,                  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 );                          LBER_FREE( bv->bv_val );
                         bv->bv_val = NULL;                          bv->bv_val = NULL;
                         return LBER_DEFAULT;                          return LBER_DEFAULT;
Line 469  ber_get_stringal( BerElement *ber, struc Line 501  ber_get_stringal( BerElement *ber, struc
         assert( ber != NULL );          assert( ber != NULL );
         assert( bv != 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 ) {          if ( *bv == NULL ) {
                 return LBER_DEFAULT;                  return LBER_DEFAULT;
         }          }
Line 504  ber_get_bitstringa( Line 537  ber_get_bitstringa(
         }          }
         --datalen;          --datalen;
   
         if ( (*buf = (char *) LBER_MALLOC( datalen )) == NULL ) {          *buf = (char *) ber_memalloc_x( datalen, ber->ber_memctx );
           if ( *buf == NULL ) {
                 return LBER_DEFAULT;                  return LBER_DEFAULT;
         }          }
   
Line 603  ber_next_element( Line 637  ber_next_element(
   
         assert( LBER_VALID( ber ) );          assert( LBER_VALID( ber ) );
   
         if ( ber->ber_ptr == last ) {          if ( ber->ber_ptr >= last ) {
                 return LBER_DEFAULT;                  return LBER_DEFAULT;
         }          }
   
Line 619  ber_scanf ( BerElement *ber, Line 653  ber_scanf ( BerElement *ber,
         va_list         ap;          va_list         ap;
         LDAP_CONST char         *fmt_reset;          LDAP_CONST char         *fmt_reset;
         char            *s, **ss;          char            *s, **ss;
 #ifdef TMP_SLOTS  
         char            *last;  
         char ***sss;  
         ber_tag_t tag;  
         struct berval ***bv;  
         int j;  
 #endif  
         struct berval   **bvp, *bval;          struct berval   **bvp, *bval;
         ber_int_t       *i;          ber_int_t       *i;
         ber_len_t       *l;          ber_len_t       *l;
Line 642  ber_scanf ( BerElement *ber, Line 669  ber_scanf ( BerElement *ber,
   
         fmt_reset = fmt;          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_log_printf( LDAP_DEBUG_TRACE, ber->ber_debug,
                 "ber_scanf fmt (%s) ber:\n", fmt );                  "ber_scanf fmt (%s) ber:\n", fmt );
         ber_log_dump( LDAP_DEBUG_BER, ber->ber_debug, ber, 1 );          ber_log_dump( LDAP_DEBUG_BER, ber->ber_debug, ber, 1 );
 #endif  
   
         for ( rc = 0; *fmt && rc != LBER_DEFAULT; fmt++ ) {          for ( rc = 0; *fmt && rc != LBER_DEFAULT; fmt++ ) {
                 /* When this is modified, remember to update                  /* When this is modified, remember to update
Line 729  ber_scanf ( BerElement *ber, Line 751  ber_scanf ( BerElement *ber,
   
                 case 'v':       /* sequence of strings */                  case 'v':       /* sequence of strings */
                 {                  {
                         bgbvr cookie = { ber, ChArray };                          bgbvr cookie = { ChArray };
                           cookie.ber = ber;
                         cookie.res.c = va_arg( ap, char *** );                          cookie.res.c = va_arg( ap, char *** );
                         cookie.alloc = 1;                          cookie.alloc = 1;
                         rc = ber_get_stringbvr( &cookie, 0 );                          rc = ber_get_stringbvl( &cookie, NULL );
                         break;                          break;
                 }                  }
   
                 case 'V':       /* sequence of strings + lengths */                  case 'V':       /* sequence of strings + lengths */
                 {                  {
                         bgbvr cookie = { ber, BvVec };                          bgbvr cookie = { BvVec };
                           cookie.ber = ber;
                         cookie.res.bv = va_arg( ap, struct berval *** );                          cookie.res.bv = va_arg( ap, struct berval *** );
                         cookie.alloc = 1;                          cookie.alloc = 1;
                         rc = ber_get_stringbvr( &cookie, 0 );                          rc = ber_get_stringbvl( &cookie, NULL );
                         break;                          break;
                 }                  }
   
                 case 'W':       /* bvarray */                  case 'W':       /* bvarray */
                 {                  {
                         bgbvr cookie = { ber, BvArray };                          bgbvr cookie = { BvArray };
                           cookie.ber = ber;
                         cookie.res.ba = va_arg( ap, struct berval ** );                          cookie.res.ba = va_arg( ap, struct berval ** );
                         cookie.alloc = 1;                          cookie.alloc = 1;
                         rc = ber_get_stringbvr( &cookie, 0 );                          rc = ber_get_stringbvl( &cookie, NULL );
                         break;                          break;
                 }                  }
   
                 case 'w':       /* bvoffarray - must include address of                  case 'M':       /* bvoffarray - must include address of
                                  * a record len, and record offset.                                   * a record len, and record offset.
                                  * number of records will be returned thru                                   * 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.res.ba = va_arg( ap, struct berval ** );
                         cookie.alloc = 0;                          cookie.alloc = 0;
                         l = va_arg( ap, ber_len_t * );                          l = va_arg( ap, ber_len_t * );
                         cookie.siz = *l;                          cookie.siz = *l;
                         cookie.off = va_arg( ap, ber_len_t );                          cookie.off = va_arg( ap, ber_len_t );
                         rc = ber_get_stringbvr( &cookie, 0 );                          rc = ber_get_stringbvl( &cookie, l );
                         *l = cookie.len;  
                         break;                          break;
                 }                  }
   
Line 781  ber_scanf ( BerElement *ber, Line 806  ber_scanf ( BerElement *ber,
                 case '{':       /* begin sequence */                  case '{':       /* begin sequence */
                 case '[':       /* begin set */                  case '[':       /* begin set */
                         if ( *(fmt + 1) != 'v' && *(fmt + 1) != 'V'                          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 );                                  rc = ber_skip_tag( ber, &len );
                         break;                          break;
   
Line 791  ber_scanf ( BerElement *ber, Line 816  ber_scanf ( BerElement *ber,
   
                 default:                  default:
                         if( ber->ber_debug ) {                          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_log_printf( LDAP_DEBUG_ANY, ber->ber_debug,
                                         "ber_scanf: unknown fmt %c\n", *fmt );                                          "ber_scanf: unknown fmt %c\n", *fmt );
 #endif  
                         }                          }
                         rc = LBER_DEFAULT;                          rc = LBER_DEFAULT;
                         break;                          break;
Line 806  ber_scanf ( BerElement *ber, Line 826  ber_scanf ( BerElement *ber,
   
         va_end( ap );          va_end( ap );
         if ( rc == LBER_DEFAULT ) {          if ( rc == LBER_DEFAULT ) {
             /*                  /*
              * Error.  Reclaim malloced memory that was given to the caller.                   * Error.  Reclaim malloced memory that was given to the caller.
              * Set allocated pointers to NULL, "data length" outvalues to 0.                   * Set allocated pointers to NULL, "data length" outvalues to 0.
              */                   */
             va_start( ap, fmt );                  va_start( ap, fmt );
   
             for ( ; fmt_reset < fmt; fmt_reset++ ) {                  for ( ; fmt_reset < fmt; fmt_reset++ ) {
                 switch ( *fmt_reset ) {                  switch ( *fmt_reset ) {
                 case '!': { /* Hook */                  case '!': { /* Hook */
                                 BERDecodeCallback *f;                                  BERDecodeCallback *f;
Line 880  ber_scanf ( BerElement *ber, Line 900  ber_scanf ( BerElement *ber,
   
                 case 'v':       /* sequence of strings */                  case 'v':       /* sequence of strings */
                 case 'V':       /* sequence of strings + lengths */                  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 'n':       /* null */
                 case 'x':       /* skip the next element - whatever it is */                  case 'x':       /* skip the next element - whatever it is */
                 case '{':       /* begin sequence */                  case '{':       /* begin sequence */
Line 893  ber_scanf ( BerElement *ber, Line 915  ber_scanf ( BerElement *ber,
                         /* format should be good */                          /* format should be good */
                         assert( 0 );                          assert( 0 );
                 }                  }
             }                  }
   
             va_end( ap );                  va_end( ap );
         }          }
   
         return rc;          return rc;

Removed from v.1.80  
changed lines
  Added in v.1.102


______________
© Copyright 1998-2020, OpenLDAP Foundation, info@OpenLDAP.org