From: Kurt Zeilenga Date: Wed, 11 Oct 2000 02:43:14 +0000 (+0000) Subject: Remove max buffer constraint! X-Git-Tag: LDBM_PRE_GIANT_RWLOCK~1784 X-Git-Url: https://git.sur5r.net/?a=commitdiff_plain;h=1dc469e064e212aee9ae4d26438f952536ab754a;p=openldap Remove max buffer constraint! and general cleanup --- diff --git a/libraries/liblber/bprint.c b/libraries/liblber/bprint.c index df1be029da..425164cc42 100644 --- a/libraries/liblber/bprint.c +++ b/libraries/liblber/bprint.c @@ -174,7 +174,7 @@ ber_bprint( line[ off ] = hexdig[ 0x0f & ( data[i] >> 4 ) ]; line[ off+1 ] = hexdig[ 0x0f & data[i] ]; - off = BP_GRAPH + n; + off = BP_GRAPH + n + ((n >= 8)?1:0); if ( isprint( data[i] )) { line[ BP_GRAPH + n ] = data[i]; diff --git a/libraries/liblber/decode.c b/libraries/liblber/decode.c index e55354f5e1..6846377424 100644 --- a/libraries/liblber/decode.c +++ b/libraries/liblber/decode.c @@ -44,28 +44,33 @@ ber_get_tag( BerElement *ber ) assert( ber != NULL ); assert( BER_VALID( ber ) ); - if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 ) - return( LBER_DEFAULT ); + if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 ) { + return LBER_DEFAULT; + } tag = xbyte; - if ( (xbyte & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK ) + if ( (xbyte & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK ) { return tag; + } for ( i = 1; i < sizeof(ber_tag_t); i++ ) { - if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 ) - return( LBER_DEFAULT ); + if ( ber_read( ber, (char *) &xbyte, 1 ) != 1 ) { + return LBER_DEFAULT; + } tag <<= 8; tag |= 0x00ffUL & (ber_tag_t) xbyte; - if ( ! (xbyte & LBER_MORE_TAG_MASK) ) + if ( ! (xbyte & LBER_MORE_TAG_MASK) ) { break; + } } /* tag too big! */ - if ( i == sizeof(ber_tag_t) ) - return( LBER_DEFAULT ); + if ( i == sizeof(ber_tag_t) ) { + return LBER_DEFAULT; + } return tag; } @@ -98,8 +103,9 @@ ber_skip_tag( BerElement *ber, ber_len_t *len ) * First, we read the tag. */ - if ( (tag = ber_get_tag( ber )) == LBER_DEFAULT ) - return( LBER_DEFAULT ); + if ( (tag = ber_get_tag( ber )) == LBER_DEFAULT ) { + return LBER_DEFAULT; + } /* * Next, read the length. The first byte contains the length of @@ -109,17 +115,17 @@ ber_skip_tag( BerElement *ber, ber_len_t *len ) */ if ( ber_read( ber, (char *) &lc, 1 ) != 1 ) - return( LBER_DEFAULT ); + return LBER_DEFAULT; if ( lc & 0x80U ) { noctets = (lc & 0x7fU); if ( noctets > sizeof(ber_len_t) ) { - return( LBER_DEFAULT ); + return LBER_DEFAULT; } if( (unsigned) ber_read( ber, netlen, noctets ) != noctets ) { - return( LBER_DEFAULT ); + return LBER_DEFAULT; } for( i = 0; i < noctets; i++ ) { @@ -131,7 +137,7 @@ ber_skip_tag( BerElement *ber, ber_len_t *len ) *len = lc; } - return( tag ); + return tag; } ber_tag_t @@ -164,7 +170,6 @@ ber_getnint( assert( ber != NULL ); assert( num != NULL ); - assert( BER_VALID( ber ) ); /* @@ -174,12 +179,13 @@ ber_getnint( * extend after we read it in. */ - if ( len > sizeof(ber_int_t) ) - return( -1 ); + if ( len > sizeof(ber_int_t) ) { + return -1; + } /* read into the low-order bytes of our buffer */ if ( (ber_len_t) ber_read( ber, (char *) buf, len ) != len ) { - return( -1 ); + return -1; } if( len ) { @@ -198,7 +204,7 @@ ber_getnint( *num = 0; } - return( len ); + return len; } ber_tag_t @@ -212,13 +218,15 @@ ber_get_int( assert( ber != NULL ); assert( BER_VALID( ber ) ); - if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT ) - return( LBER_DEFAULT ); + if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT ) { + return LBER_DEFAULT; + } - if ( ber_getnint( ber, num, len ) != len ) - return( LBER_DEFAULT ); - else - return( tag ); + if ( ber_getnint( ber, num, len ) != len ) { + return LBER_DEFAULT; + } + + return tag; } ber_tag_t @@ -241,18 +249,21 @@ ber_get_stringb( assert( ber != NULL ); assert( BER_VALID( ber ) ); - if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) - return( LBER_DEFAULT ); - if ( datalen > (*len - 1) ) - return( LBER_DEFAULT ); + if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) { + return LBER_DEFAULT; + } + if ( datalen > (*len - 1) ) { + return LBER_DEFAULT; + } - if ( (ber_len_t) ber_read( ber, buf, datalen ) != datalen ) - return( LBER_DEFAULT ); + if ( (ber_len_t) ber_read( ber, buf, datalen ) != datalen ) { + return LBER_DEFAULT; + } buf[datalen] = '\0'; *len = datalen; - return( tag ); + return tag; } ber_tag_t @@ -268,20 +279,21 @@ ber_get_stringa( BerElement *ber, char **buf ) if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) { *buf = NULL; - return( LBER_DEFAULT ); + return LBER_DEFAULT; } - if ( (*buf = (char *) LBER_MALLOC( datalen + 1 )) == NULL ) - return( LBER_DEFAULT ); + if ( (*buf = (char *) LBER_MALLOC( datalen + 1 )) == NULL ) { + return LBER_DEFAULT; + } if ( (ber_len_t) ber_read( ber, *buf, datalen ) != datalen ) { LBER_FREE( *buf ); *buf = NULL; - return( LBER_DEFAULT ); + return LBER_DEFAULT; } (*buf)[datalen] = '\0'; - return( tag ); + return tag; } ber_tag_t @@ -327,7 +339,7 @@ ber_get_stringal( BerElement *ber, struct berval **bv ) ((*bv)->bv_val)[len] = '\0'; (*bv)->bv_len = len; - return( tag ); + return tag; } ber_tag_t @@ -348,27 +360,28 @@ ber_get_bitstringa( if ( (tag = ber_skip_tag( ber, &datalen )) == LBER_DEFAULT ) { *buf = NULL; - return( LBER_DEFAULT ); + return LBER_DEFAULT; } --datalen; - if ( (*buf = (char *) LBER_MALLOC( datalen )) == NULL ) - return( LBER_DEFAULT ); + if ( (*buf = (char *) LBER_MALLOC( datalen )) == NULL ) { + return LBER_DEFAULT; + } if ( ber_read( ber, (char *)&unusedbits, 1 ) != 1 ) { LBER_FREE( buf ); *buf = NULL; - return( LBER_DEFAULT ); + return LBER_DEFAULT; } if ( (ber_len_t) ber_read( ber, *buf, datalen ) != datalen ) { LBER_FREE( buf ); *buf = NULL; - return( LBER_DEFAULT ); + return LBER_DEFAULT; } *blen = datalen * 8 - unusedbits; - return( tag ); + return tag; } ber_tag_t @@ -380,11 +393,13 @@ ber_get_null( BerElement *ber ) assert( ber != NULL ); assert( BER_VALID( ber ) ); - if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT ) - return( LBER_DEFAULT ); + if ( (tag = ber_skip_tag( ber, &len )) == LBER_DEFAULT ) { + return LBER_DEFAULT; + } - if ( len != 0 ) - return( LBER_DEFAULT ); + if ( len != 0 ) { + return LBER_DEFAULT; + } return( tag ); } @@ -405,7 +420,7 @@ ber_get_boolean( rc = ber_get_int( ber, &longbool ); *boolval = longbool; - return( rc ); + return rc; } ber_tag_t @@ -421,16 +436,16 @@ ber_first_element( /* skip the sequence header, use the len to mark where to stop */ if ( ber_skip_tag( ber, len ) == LBER_DEFAULT ) { *last = NULL; - return( LBER_DEFAULT ); + return LBER_DEFAULT; } *last = ber->ber_ptr + *len; if ( *last == ber->ber_ptr ) { - return( LBER_DEFAULT ); + return LBER_DEFAULT; } - return( ber_peek_tag( ber, len ) ); + return ber_peek_tag( ber, len ); } ber_tag_t @@ -446,10 +461,10 @@ ber_next_element( assert( BER_VALID( ber ) ); if ( ber->ber_ptr == last ) { - return( LBER_DEFAULT ); + return LBER_DEFAULT; } - return( ber_peek_tag( ber, len ) ); + return ber_peek_tag( ber, len ); } /* VARARGS */ @@ -729,5 +744,5 @@ ber_scanf ( BerElement *ber, va_end( ap ); } - return( rc ); + return rc; } diff --git a/libraries/liblber/encode.c b/libraries/liblber/encode.c index ff3bf64be3..dae8090ee8 100644 --- a/libraries/liblber/encode.c +++ b/libraries/liblber/encode.c @@ -55,11 +55,10 @@ ber_calc_taglen( ber_tag_t tag ) for ( i = sizeof(ber_tag_t) - 1; i > 0; i-- ) { mask = ((ber_tag_t)0xffU << (i * 8)); /* not all zero */ - if ( tag & mask ) - break; + if ( tag & mask ) break; } - return( i + 1 ); + return i + 1; } static int @@ -99,7 +98,7 @@ ber_calc_lenlen( ber_len_t len ) */ if ( len <= (ber_len_t) 0x7FU ) - return( 1 ); + return 1; /* * long len otherwise - one byte with bit 8 set, giving the @@ -107,13 +106,13 @@ ber_calc_lenlen( ber_len_t len ) */ if ( len <= (ber_len_t) 0xffU ) - return( 2 ); + return 2; if ( len <= (ber_len_t) 0xffffU ) - return( 3 ); + return 3; if ( len <= (ber_len_t) 0xffffffU ) - return( 4 ); + return 4; - return( 5 ); + return 5; } static int @@ -135,7 +134,7 @@ ber_put_len( BerElement *ber, ber_len_t len, int nosos ) if ( len <= 127 ) { char length_byte = (char) len; - return( ber_write( ber, &length_byte, 1, nosos ) ); + return ber_write( ber, &length_byte, 1, nosos ); } /* @@ -147,18 +146,17 @@ ber_put_len( BerElement *ber, ber_len_t len, int nosos ) for ( i = sizeof(ber_len_t) - 1; i > 0; i-- ) { mask = ((ber_len_t)0xffU << (i * 8)); /* not all zero */ - if ( len & mask ) - break; + if ( len & mask ) break; } lenlen = (unsigned char) ++i; if ( lenlen > 4 ) - return( -1 ); + return -1; lenlen |= 0x80UL; /* write the length of the length */ if ( ber_write( ber, &lenlen, 1, nosos ) != 1 ) - return( -1 ); + return -1; for( j=0; jsos_ber = ber; - if ( ber->ber_sos == NULL ) + if ( ber->ber_sos == NULL ) { new->sos_first = ber->ber_ptr; - else + } else { new->sos_first = ber->ber_sos->sos_ptr; + } /* Set aside room for a 4 byte length field */ new->sos_ptr = new->sos_first + ber_calc_taglen( tag ) + FOUR_BYTE_LEN; @@ -446,7 +461,7 @@ ber_start_seqorset( new->sos_next = ber->ber_sos; ber->ber_sos = new; - return( 0 ); + return 0; } int @@ -455,10 +470,11 @@ ber_start_seq( BerElement *ber, ber_tag_t tag ) assert( ber != NULL ); assert( BER_VALID( ber ) ); - if ( tag == LBER_DEFAULT ) + if ( tag == LBER_DEFAULT ) { tag = LBER_SEQUENCE; + } - return( ber_start_seqorset( ber, tag ) ); + return ber_start_seqorset( ber, tag ); } int @@ -467,10 +483,11 @@ ber_start_set( BerElement *ber, ber_tag_t tag ) assert( ber != NULL ); assert( BER_VALID( ber ) ); - if ( tag == LBER_DEFAULT ) + if ( tag == LBER_DEFAULT ) { tag = LBER_SET; + } - return( ber_start_seqorset( ber, tag ) ); + return ber_start_seqorset( ber, tag ); } static int @@ -497,8 +514,9 @@ ber_put_seqorset( BerElement *ber ) len = (*sos)->sos_clen; - if ( sizeof(ber_len_t) > 4 && len > 0xffffffffUL ) - return( -1 ); + if ( sizeof(ber_len_t) > 4 && len > 0xffffffffUL ) { + return -1; + } if ( ber->ber_options & LBER_USE_DER ) { lenlen = ber_calc_lenlen( len ); @@ -524,8 +542,9 @@ ber_put_seqorset( BerElement *ber ) if ( ber->ber_options & LBER_USE_DER ) { /* Write the length in the minimum # of octets */ - if ( ber_put_len( ber, len, 1 ) == -1 ) - return( -1 ); + if ( ber_put_len( ber, len, 1 ) == -1 ) { + return -1; + } if (lenlen != FOUR_BYTE_LEN) { /* @@ -540,8 +559,9 @@ ber_put_seqorset( BerElement *ber ) } else { /* Fill FOUR_BYTE_LEN bytes for length field */ /* one byte of length length */ - if ( ber_write( ber, (char *)<ag, 1, 1 ) != 1 ) - return( -1 ); + if ( ber_write( ber, (char *)<ag, 1, 1 ) != 1 ) { + return -1; + } /* the length itself */ rc = ber_write( ber, @@ -549,7 +569,7 @@ ber_put_seqorset( BerElement *ber ) FOUR_BYTE_LEN-1, 1 ); if( rc != FOUR_BYTE_LEN - 1 ) { - return( -1 ); + return -1; } } /* The ber_ptr is at the set/seq start - move it to the end */ @@ -628,7 +648,7 @@ ber_put_seqorset( BerElement *ber ) LBER_FREE( (char *) (*sos) ); *sos = next; - return( taglen + lenlen + len ); + return taglen + lenlen + len; } int @@ -637,7 +657,7 @@ ber_put_seq( BerElement *ber ) assert( ber != NULL ); assert( BER_VALID( ber ) ); - return( ber_put_seqorset( ber ) ); + return ber_put_seqorset( ber ); } int @@ -646,7 +666,7 @@ ber_put_set( BerElement *ber ) assert( ber != NULL ); assert( BER_VALID( ber ) ); - return( ber_put_seqorset( ber ) ); + return ber_put_seqorset( ber ); } /* N tag */ @@ -792,5 +812,5 @@ ber_printf( BerElement *ber, LDAP_CONST char *fmt, ... ) va_end( ap ); - return( rc ); + return rc; } diff --git a/libraries/liblber/io.c b/libraries/liblber/io.c index 6de4335ca2..1e78515c4c 100644 --- a/libraries/liblber/io.c +++ b/libraries/liblber/io.c @@ -36,14 +36,6 @@ #define EXBUFSIZ 1024 -/* probably far too large... */ -#define MAX_BERBUFSIZE (128*1024) - -#if defined( DOS ) && !defined( _WIN32 ) && (MAX_BERBUFSIZE > 65535) -# undef MAX_BERBUFSIZE -# define MAX_BERBUFSIZE 65535 -#endif - static ber_slen_t BerRead( Sockbuf *sb, @@ -340,19 +332,20 @@ ber_init( struct berval *bv ) if( ber == NULL ) { /* allocation failed */ - return ( NULL ); + return NULL; } /* copy the data */ - if ( ( (ber_len_t) ber_write ( ber, bv->bv_val, bv->bv_len, 0 )) != bv->bv_len ) { + if ( ((ber_len_t) ber_write ( ber, bv->bv_val, bv->bv_len, 0 )) + != bv->bv_len ) + { /* write failed, so free and return NULL */ ber_free( ber, 1 ); - return( NULL ); + return NULL; } ber_reset( ber, 1 ); /* reset the pointer to the start of the buffer */ - - return ( ber ); + return ber; } /* New C-API ber_flatten routine */ @@ -371,11 +364,12 @@ int ber_flatten( ber_int_options.lbo_valid = LBER_INITIALIZED; if(bvPtr == NULL) { - return( -1 ); + return -1; } - if ( (bv = LBER_MALLOC( sizeof(struct berval))) == NULL ) { - return( -1 ); + bv = LBER_MALLOC( sizeof(struct berval)); + if ( bv == NULL ) { + return -1; } if ( ber == NULL ) { @@ -387,9 +381,10 @@ int ber_flatten( /* copy the berval */ ber_len_t len = ber->ber_ptr - ber->ber_buf; - if ( (bv->bv_val = (char *) LBER_MALLOC( len + 1 )) == NULL ) { - ber_bvfree( bv ); - return( -1 ); + bv->bv_val = (char *) LBER_MALLOC( len + 1 ); + if ( bv->bv_val == NULL ) { + LBER_FREE( bv ); + return -1; } AC_MEMCPY( bv->bv_val, ber->ber_buf, len ); @@ -398,7 +393,7 @@ int ber_flatten( } *bvPtr = bv; - return( 0 ); + return 0; } void @@ -417,46 +412,6 @@ ber_reset( BerElement *ber, int was_writing ) ber->ber_rwptr = NULL; } -#if 0 -/* return the tag - LBER_DEFAULT returned means trouble */ -static ber_tag_t -get_tag( Sockbuf *sb ) -{ - unsigned char xbyte; - ber_tag_t tag; - char *tagp; - unsigned int i; - - assert( sb != NULL ); - assert( SOCKBUF_VALID( sb ) ); - - if ( ber_int_sb_read( sb, (char *) &xbyte, 1 ) != 1 ) - return( LBER_DEFAULT ); - - if ( (xbyte & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK ) - return( (ber_tag_t) xbyte ); - - tagp = (char *) &tag; - tagp[0] = xbyte; - for ( i = 1; i < sizeof(ber_tag_t); i++ ) { - if ( ber_int_sb_read( sb, (char *) &xbyte, 1 ) != 1 ) - return( LBER_DEFAULT ); - - tagp[i] = xbyte; - - if ( ! (xbyte & LBER_MORE_TAG_MASK) ) - break; - } - - /* tag too big! */ - if ( i == sizeof(ber_tag_t) ) - return( LBER_DEFAULT ); - - /* want leading, not trailing 0's */ - return( tag >> (sizeof(ber_tag_t) - i - 1) ); -} -#endif - /* * A rewrite of ber_get_next that can safely be called multiple times * for the same packet. It will simply continue where it stopped until @@ -579,13 +534,10 @@ get_lenbyte: fill_buffer: /* now fill the buffer. */ if (ber->ber_buf==NULL) { - if (ber->ber_len > MAX_BERBUFSIZE) { - errno = ERANGE; - return LBER_DEFAULT; - } ber->ber_buf = (char *) LBER_MALLOC( ber->ber_len ); - if (ber->ber_buf==NULL) + if (ber->ber_buf==NULL) { return LBER_DEFAULT; + } ber->ber_rwptr = ber->ber_buf; ber->ber_ptr = ber->ber_buf; ber->ber_end = ber->ber_buf + ber->ber_len; diff --git a/libraries/liblber/memory.c b/libraries/liblber/memory.c index b908c40b7d..90720fa792 100644 --- a/libraries/liblber/memory.c +++ b/libraries/liblber/memory.c @@ -150,7 +150,7 @@ ber_memfree( void *p ) mh->bm_sequence, mh, mh->bm_length, ber_int_options.lbo_meminuse); #endif /* Fill the free space with poison */ - memset( mh, 0xff, mh->bm_length + sizeof(struct ber_mem_hdr) + sizeof( ber_int_t)); + memset( mh, 0xff, mh->bm_length + sizeof(struct ber_mem_hdr) + sizeof(ber_int_t)); free( mh ); #else free( p ); @@ -365,8 +365,9 @@ ber_bvfree( struct berval *bv ) BER_MEM_VALID( bv ); - if ( bv->bv_val != NULL ) + if ( bv->bv_val != NULL ) { LBER_FREE( bv->bv_val ); + } LBER_FREE( (char *) bv ); } @@ -385,8 +386,9 @@ ber_bvecfree( struct berval **bv ) BER_MEM_VALID( bv ); - for ( i = 0; bv[i] != NULL; i++ ) + for ( i = 0; bv[i] != NULL; i++ ) { ber_bvfree( bv[i] ); + } LBER_FREE( (char *) bv ); } @@ -477,7 +479,7 @@ ber_bvdup( new->bv_val[bv->bv_len] = '\0'; new->bv_len = bv->bv_len; - return( new ); + return new; } struct berval * @@ -531,7 +533,7 @@ ber_bvstrdup( LBER_FREE( p ); } - return( new ); + return new; } char * @@ -548,16 +550,16 @@ ber_strdup( LDAP_CONST char *s ) if( s == NULL ) { ber_errno = LBER_ERROR_PARAM; - return( NULL ); + return NULL; } len = strlen( s ) + 1; if ( (p = LBER_MALLOC( len )) == NULL ) { ber_errno = LBER_ERROR_MEMORY; - return( NULL ); + return NULL; } AC_MEMCPY( p, s, len ); - return( p ); + return p; } diff --git a/libraries/liblber/sockbuf.c b/libraries/liblber/sockbuf.c index b33d16be2f..887d21b883 100644 --- a/libraries/liblber/sockbuf.c +++ b/libraries/liblber/sockbuf.c @@ -68,7 +68,6 @@ ber_sockbuf_ctrl( Sockbuf *sb, int opt, void *arg ) { Sockbuf_IO_Desc *p; int ret = 0; - char buf[4096]; assert( sb != NULL ); @@ -95,16 +94,17 @@ ber_sockbuf_ctrl( Sockbuf *sb, int opt, void *arg ) ret = ber_pvt_socket_set_nonblock( sb->sb_fd, arg != NULL) ? -1 : 1; break; - case LBER_SB_OPT_DRAIN: - /* Drain the data source to enable possible errors (e.g. - * TLS) to be propagated to the upper layers - */ - do { - ret = ber_int_sb_read( sb, buf, sizeof( buf ) ); - } while ( ret == sizeof( buf ) ); + case LBER_SB_OPT_DRAIN: { + /* Drain the data source to enable possible errors (e.g. + * TLS) to be propagated to the upper layers + */ + char buf[MIN_BUFF_SIZE]; + do { + ret = ber_int_sb_read( sb, buf, sizeof( buf ) ); + } while ( ret == sizeof( buf ) ); - ret = 1; - break; + ret = 1; + } break; case LBER_SB_OPT_NEEDS_READ: ret = ( sb->sb_trans_needs_read ? 1 : 0 ); break;