Skip to content
Snippets Groups Projects
io.c 14 KiB
Newer Older
  • Learn to ignore specific revisions
  • Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    /* io.c - ber general i/o routines */
    
     * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
    
     * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
     */
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
     * Copyright (c) 1990 Regents of the University of Michigan.
     * All rights reserved.
     *
     * Redistribution and use in source and binary forms are permitted
     * provided that this notice is preserved and that due credit is given
     * to the University of Michigan at Ann Arbor. The name of the University
     * may not be used to endorse or promote products derived from this
     * software without specific prior written permission. This software
     * is provided ``as is'' without express or implied warranty.
     */
    
    
    #include "portable.h"
    
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    #include <stdio.h>
    
    
    #include <ac/stdlib.h>
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    
    #include <ac/ctype.h>
    #include <ac/errno.h>
    #include <ac/socket.h>
    #include <ac/string.h>
    #include <ac/unistd.h>
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    
    #ifdef HAVE_IO_H
    #include <io.h>
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    #endif
    
    
    Julius Enarusai's avatar
     
    Julius Enarusai committed
    #include "ldap_log.h"
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    
    ber_slen_t
    ber_read(
    	BerElement *ber,
    	char *buf,
    	ber_len_t len )
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    {
    
    	ber_len_t	actuallen, nleft;
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	assert( ber != NULL );
    	assert( buf != NULL );
    
    
    	assert( LBER_VALID( ber ) );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	nleft = ber_pvt_ber_remaining( ber );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	actuallen = nleft < len ? nleft : len;
    
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	AC_MEMCPY( buf, ber->ber_ptr, actuallen );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    	ber->ber_ptr += actuallen;
    
    
    	return( (ber_slen_t) actuallen );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    }
    
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    ber_write(
    	BerElement *ber,
    	LDAP_CONST char *buf,
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	int nosos )
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    {
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	assert( ber != NULL );
    	assert( buf != NULL );
    
    
    	assert( LBER_VALID( ber ) );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	if ( nosos || ber->ber_sos == NULL ) {
    		if ( ber->ber_ptr + len > ber->ber_end ) {
    			if ( ber_realloc( ber, len ) != 0 )
    				return( -1 );
    		}
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    		AC_MEMCPY( ber->ber_ptr, buf, (size_t)len );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    		ber->ber_ptr += len;
    
    		return( (ber_slen_t) len );
    
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	} else {
    		if ( ber->ber_sos->sos_ptr + len > ber->ber_end ) {
    			if ( ber_realloc( ber, len ) != 0 )
    				return( -1 );
    		}
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    		AC_MEMCPY( ber->ber_sos->sos_ptr, buf, (size_t)len );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    		ber->ber_sos->sos_ptr += len;
    		ber->ber_sos->sos_clen += len;
    
    		return( (ber_slen_t) len );
    
    ber_realloc( BerElement *ber, ber_len_t len )
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    {
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	Seqorset	*s;
    	long		off;
    	char		*oldbuf;
    
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	assert( ber != NULL );
    	assert( len > 0 );
    
    	assert( LBER_VALID( ber ) );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	total = ber_pvt_ber_total( ber );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    #define LBER_EXBUFSIZ	4060 /* a few words less than 2^N for binary buddy */
    
    #if defined( LBER_EXBUFSIZ ) && LBER_EXBUFSIZ > 0
    # ifndef notdef
    	/* don't realloc by small amounts */
    	total += len < LBER_EXBUFSIZ ? LBER_EXBUFSIZ : len;
    # else
    	{	/* not sure what value this adds */
    		ber_len_t have = (total + (LBER_EXBUFSIZE - 1)) / LBER_EXBUFSIZ;
    		ber_len_t need = (len + (LBER_EXBUFSIZ - 1)) / LBER_EXBUFSIZ;
    		total = ( have + need ) * LBER_EXBUFSIZ;
    	}
    # endif
    #else
    	total += len;	/* realloc just what's needed */
    #endif
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    	oldbuf = ber->ber_buf;
    
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	ber->ber_buf = (char *) LBER_REALLOC( oldbuf, total );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	if ( ber->ber_buf == NULL ) {
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    		return( -1 );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    	ber->ber_end = ber->ber_buf + total;
    
    	/*
    	 * If the stinking thing was moved, we need to go through and
    
    Gary Williams's avatar
    Gary Williams committed
    	 * reset all the sos and ber pointers.	Offsets would've been
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	 * a better idea... oh well.
    	 */
    
    	if ( ber->ber_buf != oldbuf ) {
    		ber->ber_ptr = ber->ber_buf + (ber->ber_ptr - oldbuf);
    
    
    		for ( s = ber->ber_sos; s != NULL; s = s->sos_next ) {
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    			off = s->sos_first - oldbuf;
    			s->sos_first = ber->ber_buf + off;
    
    			off = s->sos_ptr - oldbuf;
    			s->sos_ptr = ber->ber_buf + off;
    		}
    	}
    
    	return( 0 );
    }
    
    void
    
    Howard Chu's avatar
    Howard Chu committed
    ber_free_buf( BerElement *ber )
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    {
    
    Howard Chu's avatar
    Howard Chu committed
    	Seqorset *s, *next;
    
    	assert( LBER_VALID( ber ) );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    
    Howard Chu's avatar
    Howard Chu committed
    	if ( ber->ber_buf) LBER_FREE( ber->ber_buf );
    
    Howard Chu's avatar
    Howard Chu committed
    	for( s = ber->ber_sos ; s != NULL ; s = next ) {
    		next = s->sos_next;
    		LBER_FREE( s );
    
    	ber->ber_buf = NULL;
    
    Howard Chu's avatar
    Howard Chu committed
    }
    
    void
    ber_free( BerElement *ber, int freebuf )
    {
    #ifdef LDAP_MEMORY_DEBUG
    	assert( ber != NULL );
    #endif
    
    	if( ber == NULL ) {
    		return;
    	}
    
    	if( freebuf )
    		ber_free_buf( ber );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    }
    
    int
    ber_flush( Sockbuf *sb, BerElement *ber, int freeit )
    {
    
    	ber_len_t	towrite;
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	assert( sb != NULL );
    	assert( ber != NULL );
    
    
    	assert( SOCKBUF_VALID( sb ) );
    
    	assert( LBER_VALID( ber ) );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	if ( ber->ber_rwptr == NULL ) {
    		ber->ber_rwptr = ber->ber_buf;
    	}
    	towrite = ber->ber_ptr - ber->ber_rwptr;
    
    
    Gary Williams's avatar
    Gary Williams committed
    #ifdef NEW_LOGGING
    
    Julius Enarusai's avatar
     
    Julius Enarusai committed
    		LDAP_LOG( BER, DETAIL1,
    
    Gary Williams's avatar
    Gary Williams committed
    			   "ber_flush: %ld bytes to sd %ld%s\n",
    			   towrite, (long)sb->sb_fd,
    
    Julius Enarusai's avatar
     
    Julius Enarusai committed
    			   ber->ber_rwptr != ber->ber_buf ? " (re-flush)" : "" );
    
    		if(LDAP_LOGS_TEST(BER, DETAIL2))
    				BER_DUMP(( "liblber", LDAP_LEVEL_DETAIL2, ber, 1 ));
    
    
    Gary Williams's avatar
    Gary Williams committed
    #else
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    		ber_log_printf( LDAP_DEBUG_ANY, sb->sb_debug,
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    			"ber_flush: %ld bytes to sd %ld%s\n",
    			towrite, (long) sb->sb_fd,
    			ber->ber_rwptr != ber->ber_buf ?  " (re-flush)" : "" );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    		ber_log_bprint( LDAP_DEBUG_PACKETS, sb->sb_debug,
    
    			ber->ber_rwptr, towrite );
    
    Gary Williams's avatar
    Gary Williams committed
    #endif
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	}
    
    	while ( towrite > 0 ) {
    
    		rc = ber_int_sb_write( sb, ber->ber_rwptr, towrite );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    		}
    		towrite -= rc;
    		ber->ber_rwptr += rc;
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    	if ( freeit )
    		ber_free( ber, 1 );
    
    	return( 0 );
    }
    
    BerElement *
    ber_alloc_t( int options )
    {
    	BerElement	*ber;
    
    
        ber_int_options.lbo_valid = LBER_INITIALIZED;
    
    
    	ber = (BerElement *) LBER_CALLOC( 1, sizeof(BerElement) );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	if ( ber == NULL ) {
    		return NULL;
    	}
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    
    	ber->ber_valid = LBER_VALID_BERELEMENT;
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	ber->ber_tag = LBER_DEFAULT;
    
    	ber->ber_options = options;
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	ber->ber_debug = ber_int_debug;
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    
    	assert( LBER_VALID( ber ) );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	return ber;
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    }
    
    BerElement *
    
    ber_alloc( void )	/* deprecated */
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    {
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	return ber_alloc_t( 0 );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    }
    
    BerElement *
    
    der_alloc( void )	/* deprecated */
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    {
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	return ber_alloc_t( LBER_USE_DER );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    }
    
    BerElement *
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    {
    	BerElement	*new;
    
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	assert( ber != NULL );
    
    	assert( LBER_VALID( ber ) );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    
    	if ( (new = ber_alloc_t( ber->ber_options )) == NULL ) {
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    		return NULL;
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    	*new = *ber;
    
    
    	assert( LBER_VALID( new ) );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	return( new );
    }
    
    
    void
    
    ber_init2( BerElement *ber, struct berval *bv, int options )
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    {
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	assert( ber != NULL );
    
    
    	ber_int_options.lbo_valid = LBER_INITIALIZED;
    
    	(void) memset( (char *)ber, '\0', sizeof( BerElement ));
    
    	ber->ber_valid = LBER_VALID_BERELEMENT;
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	ber->ber_tag = LBER_DEFAULT;
    
    	ber->ber_options = (char) options;
    
    	ber->ber_debug = ber_int_debug;
    
    	if ( bv != NULL ) {
    		ber->ber_buf = bv->bv_val;
    		ber->ber_ptr = ber->ber_buf;
    		ber->ber_end = ber->ber_buf + bv->bv_len;
    	}
    
    
    	assert( LBER_VALID( ber ) );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    }
    
    
    /* OLD U-Mich ber_init() */
    void
    ber_init_w_nullc( BerElement *ber, int options )
    {
    	ber_init2( ber, NULL, options );
    }
    
    
    /* New C-API ber_init() */
    /* This function constructs a BerElement containing a copy
    ** of the data in the bv argument.
    */
    
    BerElement *
    ber_init( struct berval *bv )
    {
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	assert( bv != NULL );
    
    
        ber_int_options.lbo_valid = LBER_INITIALIZED;
    
    
    	if ( bv == NULL ) {
    		return NULL;
    	}
    
    	ber = ber_alloc_t( 0 );
    
    
    	if( ber == NULL ) {
    
    		return NULL;
    
    	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;
    
    	}
    
    	ber_reset( ber, 1 );	/* reset the pointer to the start of the buffer */
    
    	return ber;
    
    /* New C-API ber_flatten routine */
    /* This routine allocates a struct berval whose contents are a BER
    
    ** encoding taken from the ber argument.  The bvPtr pointer points to
    
    **
    ** ber_flatten2 is the same, but uses a struct berval passed by
    ** the caller. If alloc is 0 the returned bv uses the ber buf directly.
    
    	ber_int_options.lbo_valid = LBER_INITIALIZED;
    
    	if ( bv == NULL ) {
    		return -1;
    
    	}
    
    	if ( ber == NULL ) {
    		/* ber is null, create an empty berval */
    		bv->bv_val = NULL;
    		bv->bv_len = 0;
    
    	} else {
    		/* copy the berval */
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    		ber_len_t len = ber_pvt_ber_write( ber );
    
    		if ( alloc ) {
    			bv->bv_val = (char *) LBER_MALLOC( len + 1 );
    			if ( bv->bv_val == NULL ) {
    				return -1;
    			}
    			AC_MEMCPY( bv->bv_val, ber->ber_buf, len );
    		} else {
    			bv->bv_val = ber->ber_buf;
    
    		}
    		bv->bv_val[len] = '\0';
    		bv->bv_len = len;
    	}
    
    	return 0;
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    
    int ber_flatten(
    	BerElement *ber,
    	struct berval **bvPtr)
    {
    	struct berval *bv;
    	int rc;
     
    	assert( bvPtr != NULL );
    
    	ber_int_options.lbo_valid = LBER_INITIALIZED;
    
    	if(bvPtr == NULL) {
    		return -1;
    	}
    
    	bv = LBER_MALLOC( sizeof(struct berval) );
    	if ( bv == NULL ) {
    		return -1;
    	}
    	rc = ber_flatten2(ber, bv, 1);
    	if (rc == -1) {
    		LBER_FREE(bv);
    	} else {
    		*bvPtr = bv;
    	}
    	return rc;
    }
    
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    void
    ber_reset( BerElement *ber, int was_writing )
    {
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	assert( ber != NULL );
    
    	assert( LBER_VALID( ber ) );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	if ( was_writing ) {
    		ber->ber_end = ber->ber_ptr;
    		ber->ber_ptr = ber->ber_buf;
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	} else {
    		ber->ber_ptr = ber->ber_end;
    	}
    
    	ber->ber_rwptr = NULL;
    }
    
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
     * 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
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    
    ber_tag_t
    ber_get_next(
    	Sockbuf *sb,
    	ber_len_t *len,
    	BerElement *ber )
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    {
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	assert( sb != NULL );
    	assert( len != NULL );
    	assert( ber != NULL );
    
    
    	assert( LBER_VALID( ber ) );
    
    Gary Williams's avatar
    Gary Williams committed
    #ifdef NEW_LOGGING
    
    Julius Enarusai's avatar
     
    Julius Enarusai committed
    	LDAP_LOG( BER, ENTRY, "ber_get_next: enter\n", 0, 0, 0 );
    
    Gary Williams's avatar
    Gary Williams committed
    #else
    
    	ber_log_printf( LDAP_DEBUG_TRACE, ber->ber_debug,
    		"ber_get_next\n" );
    
    Gary Williams's avatar
    Gary Williams committed
    #endif
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	/*
    	 * Any ber element looks like this: tag length contents.
    	 * Assuming everything's ok, we return the tag byte (we
    	 * can assume a single byte), return the length in len,
    	 * and the rest of the undecoded element in buf.
    	 *
    	 * Assumptions:
    	 *	1) small tags (less than 128)
    	 *	2) definite lengths
    	 *	3) primitive encodings used whenever possible
    	 */
    
    		/* XXYYZ
    		 * dtest does like this assert.
    		 */
    		/* assert( ber->ber_buf == NULL ); */
    
    		ber->ber_rwptr = (char *) &ber->ber_len-1;
    		ber->ber_ptr = ber->ber_rwptr;
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    
    	while (ber->ber_rwptr > (char *)&ber->ber_tag && ber->ber_rwptr <
    		(char *)(&ber->ber_usertag + 1)) {
    
    		char buf[sizeof(ber->ber_len)-1];
    		ber_len_t tlen = 0;
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    
    		if ((i=ber_int_sb_read( sb, ber->ber_rwptr,
    			(char *)(&ber->ber_usertag+1)-ber->ber_rwptr))<=0) {
    			return LBER_DEFAULT;
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    		}
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    
    		ber->ber_rwptr += i;
    
    		/* We got at least one byte, try to parse the tag. */
    		if (ber->ber_ptr == (char *)&ber->ber_len-1) {
    			ber_tag_t tag;
    			unsigned char *p = (unsigned char *)ber->ber_ptr;
    			tag = *p++;
    			if ((tag & LBER_BIG_TAG_MASK) == LBER_BIG_TAG_MASK) {
    				for (i=1; (char *)p<ber->ber_rwptr; i++,p++) {
    					tag <<= 8;
    					tag |= *p;
    					if (!(*p & LBER_MORE_TAG_MASK))
    						break;
    					/* Is the tag too big? */
    					if (i == sizeof(ber_tag_t)-1) {
    						errno = ERANGE;
    						return LBER_DEFAULT;
    					}
    				}
    				/* Did we run out of bytes? */
    				if ((char *)p == ber->ber_rwptr) {
    					return LBER_DEFAULT;
    				}
    				p++;
    
    			ber->ber_tag = tag;
    			ber->ber_ptr = (char *)p;
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    		}
    
    
    		if (i == 1) continue;
    
    
    		/* Now look for the length */
    		if (*ber->ber_ptr & 0x80) {	/* multi-byte */
    			int llen = *(unsigned char *)ber->ber_ptr++ & 0x7f;
    
    			if (llen > (int)sizeof(ber_len_t)) {
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    			}
    
    			/* Not enough bytes? */
    			if (ber->ber_rwptr - ber->ber_ptr < llen) {
    				return LBER_DEFAULT;
    			}
    			for (i=0; i<llen && ber->ber_ptr<ber->ber_rwptr; i++,ber->ber_ptr++) {
    				tlen <<=8;
    				tlen |= *(unsigned char *)ber->ber_ptr;
    			}
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    		} else {
    
    			tlen = *(unsigned char *)ber->ber_ptr++;
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    		}
    
    		/* Are there leftover data bytes inside ber->ber_len? */
    		if (ber->ber_ptr < (char *)&ber->ber_usertag) {
    
    			if (ber->ber_rwptr < (char *)&ber->ber_usertag)
    				i = ber->ber_rwptr - ber->ber_ptr;
    			else
    				i = (char *)&ber->ber_usertag - ber->ber_ptr;
    
    			AC_MEMCPY(buf, ber->ber_ptr, i);
    			ber->ber_ptr += i;
    		} else {
    			i = 0;
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    
    		/* make sure length is reasonable */
    		if ( ber->ber_len == 0 ) {
    			errno = ERANGE;
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    			return LBER_DEFAULT;
    
    		} else if ( sb->sb_max_incoming && ber->ber_len > sb->sb_max_incoming ) {
    
    Julius Enarusai's avatar
     
    Julius Enarusai committed
    			LDAP_LOG( BER, ERR, 
    
    				"ber_get_next: sockbuf_max_incoming limit hit "
    
    Julius Enarusai's avatar
     
    Julius Enarusai committed
    				"(%d > %d)\n", ber->ber_len, sb->sb_max_incoming, 0 );
    
    			ber_log_printf( LDAP_DEBUG_CONNS, ber->ber_debug,
    				"ber_get_next: sockbuf_max_incoming limit hit "
    				"(%ld > %ld)\n", ber->ber_len, sb->sb_max_incoming );
    
    		if (ber->ber_buf==NULL) {
    
    			ber_len_t l = ber->ber_rwptr - ber->ber_ptr;
    			/* ber->ber_ptr is always <= ber->ber->ber_rwptr.
    			 * make sure ber->ber_len agrees with what we've
    			 * already read.
    			 */
    			if ( ber->ber_len < i + l ) {
    				errno = ERANGE;
    				return LBER_DEFAULT;
    			}
    
    			ber->ber_buf = (char *) LBER_MALLOC( ber->ber_len + 1 );
    			if (ber->ber_buf==NULL) {
    				return LBER_DEFAULT;
    			}
    			ber->ber_end = ber->ber_buf + ber->ber_len;
    			if (i) {
    				AC_MEMCPY(ber->ber_buf, buf, i);
    			}
    
    			if (l > 0) {
    				AC_MEMCPY(ber->ber_buf + i, ber->ber_ptr, l);
    				i += l;
    
    			}
    			ber->ber_ptr = ber->ber_buf;
    			ber->ber_usertag = 0;
    
    			if ((ber_len_t)i == ber->ber_len) {
    
    				goto done;
    			}
    			ber->ber_rwptr = ber->ber_buf + i;
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	}
    
    	if ((ber->ber_rwptr>=ber->ber_buf) && (ber->ber_rwptr<ber->ber_end)) {
    
    		ber_slen_t res;
    		ber_slen_t to_go;
    
    		
    		to_go = ber->ber_end - ber->ber_rwptr;
    		assert( to_go > 0 );
    		
    
    		res = ber_int_sb_read( sb, ber->ber_rwptr, to_go );
    
    		if (res<=0)
    			return LBER_DEFAULT;
    		ber->ber_rwptr+=res;
    		
    		if (res<to_go) {
    #if defined( EWOULDBLOCK )
    			errno = EWOULDBLOCK;
    #elif defined( EAGAIN )
    			errno = EAGAIN;
    #endif			
    			return LBER_DEFAULT;
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    		}
    
    		ber->ber_rwptr = NULL;
    		*len = ber->ber_len;
    		if ( ber->ber_debug ) {
    
    Gary Williams's avatar
    Gary Williams committed
    #ifdef NEW_LOGGING
    
    Julius Enarusai's avatar
     
    Julius Enarusai committed
    			LDAP_LOG( BER, DETAIL1, 
    				"ber_get_next: tag 0x%lx len %ld\n", 
    				ber->ber_tag, ber->ber_len, 0  );
    			if(LDAP_LOGS_TEST(BER, DETAIL2))
    					BER_DUMP(( "liblber", LDAP_LEVEL_DETAIL2, ber, 1 ));
    
    Gary Williams's avatar
    Gary Williams committed
    #else
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    			ber_log_printf( LDAP_DEBUG_TRACE, ber->ber_debug,
    
    				"ber_get_next: tag 0x%lx len %ld contents:\n",
    				ber->ber_tag, ber->ber_len );
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    			ber_log_dump( LDAP_DEBUG_BER, ber->ber_debug, ber, 1 );
    
    Gary Williams's avatar
    Gary Williams committed
    #endif
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    	}
    
    Kurt Zeilenga's avatar
    Kurt Zeilenga committed
    
    
    	assert( 0 ); /* ber structure is messed up ?*/
    	return LBER_DEFAULT;
    }