/* io.c - ber general i/o routines */
/* $OpenLDAP$ */
/*
- * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
+ * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
*/
/* Portions
#include <io.h>
#endif
-#undef LDAP_F_PRE
-#define LDAP_F_PRE LDAP_F_EXPORT
-
#include "lber-int.h"
-static ber_slen_t BerRead LDAP_P((
- Sockbuf *sb,
- char *buf,
- ber_len_t len ));
-
-static int ber_realloc LDAP_P((
- BerElement *ber,
- ber_len_t len ));
-
#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,
- char *buf,
+ unsigned char *buf,
ber_len_t len )
{
ber_slen_t c;
assert( SOCKBUF_VALID( sb ) );
while ( len > 0 ) {
- if ( (c = ber_pvt_sb_read( sb, buf, len )) <= 0 ) {
+ if ( (c = ber_int_sb_read( sb, buf, len )) <= 0 ) {
if ( nread > 0 )
break;
return( c );
}
- buf+= c;
+ buf+=c;
nread+=c;
len-=c;
}
nleft = ber->ber_end - ber->ber_ptr;
actuallen = nleft < len ? nleft : len;
- SAFEMEMCPY( buf, ber->ber_ptr, actuallen );
+ AC_MEMCPY( buf, ber->ber_ptr, actuallen );
ber->ber_ptr += actuallen;
if ( ber_realloc( ber, len ) != 0 )
return( -1 );
}
- SAFEMEMCPY( ber->ber_ptr, buf, (size_t)len );
+ AC_MEMCPY( ber->ber_ptr, buf, (size_t)len );
ber->ber_ptr += len;
return( (ber_slen_t) len );
if ( ber_realloc( ber, len ) != 0 )
return( -1 );
}
- SAFEMEMCPY( ber->ber_sos->sos_ptr, buf, (size_t)len );
+ AC_MEMCPY( ber->ber_sos->sos_ptr, buf, (size_t)len );
ber->ber_sos->sos_ptr += len;
ber->ber_sos->sos_clen += len;
return( (ber_slen_t) len );
}
}
-static int
+int
ber_realloc( BerElement *ber, ber_len_t len )
{
ber_len_t need, have, total;
assert( BER_VALID( ber ) );
- if ( freebuf && ber->ber_buf != NULL )
+ if ( freebuf ) {
+ Seqorset *s, *next;
LBER_FREE( ber->ber_buf );
+ for( s = ber->ber_sos ; s != NULL ; s = next ) {
+ next = s->sos_next;
+ LBER_FREE( s );
+ }
+ }
+
ber->ber_buf = NULL;
+ ber->ber_sos = NULL;
ber->ber_valid = LBER_UNINITIALIZED;
LBER_FREE( (char *) ber );
assert( sb != NULL );
assert( ber != NULL );
- assert( SOCKBUF_VALID( ber ) );
+ assert( SOCKBUF_VALID( sb ) );
assert( BER_VALID( ber ) );
if ( ber->ber_rwptr == NULL ) {
if ( sb->sb_debug ) {
ber_log_printf( LDAP_DEBUG_ANY, sb->sb_debug,
- "ber_flush: %ld bytes to sd %ld%s\n", towrite,
- (long) sb->sb_sd, ber->ber_rwptr != ber->ber_buf ? " (re-flush)"
- : "" );
+ "ber_flush: %ld bytes to sd %ld%s\n",
+ towrite, (long) sb->sb_fd,
+ ber->ber_rwptr != ber->ber_buf ? " (re-flush)" : "" );
ber_log_bprint( LDAP_DEBUG_PACKETS, sb->sb_debug,
ber->ber_rwptr, towrite );
}
-#if HAVE_WRITE
- if ( sb->sb_options & (LBER_TO_FILE | LBER_TO_FILE_ONLY) ) {
- rc = write( sb->sb_fd, ber->ber_rwptr, towrite );
- if ( sb->sb_options & LBER_TO_FILE_ONLY ) {
- if ( freeit )
- ber_free( ber, 1 );
- return( (int)rc );
- }
- }
-#endif
-
nwritten = 0;
do {
- rc = ber_pvt_sb_write( sb, ber->ber_rwptr, towrite );
+ rc = ber_int_sb_write( sb, ber->ber_rwptr, towrite );
if (rc<=0) {
return -1;
}
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 */
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 ) {
/* 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;
}
- SAFEMEMCPY( bv->bv_val, ber->ber_buf, len );
+ AC_MEMCPY( bv->bv_val, ber->ber_buf, len );
bv->bv_val[len] = '\0';
bv->bv_len = len;
}
*bvPtr = bv;
- return( 0 );
+ return 0;
}
void
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_pvt_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_pvt_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 were it stopped until
+ * for the same packet. It will simply continue where it stopped until
* a full packet is read.
*/
ber->ber_tag = 0;
}
-#define PTR_IN_VAR( ptr, var )\
-(((ptr)>=(char *) &(var)) && ((ptr)< (char *) &(var)+sizeof(var)))
+#undef PTR_IN_VAR
+#define PTR_IN_VAR( ptr, var ) \
+ (((ptr)>=(char *) &(var)) && ((ptr)< (char *) &(var)+sizeof(var)))
if (PTR_IN_VAR(ber->ber_rwptr, ber->ber_tag)) {
if (ber->ber_rwptr == (char *) &ber->ber_tag) {
- if (ber_pvt_sb_read( sb, ber->ber_rwptr, 1)<=0)
+ if (ber_int_sb_read( sb, ber->ber_rwptr, 1)<=0)
return LBER_DEFAULT;
if ((ber->ber_rwptr[0] & LBER_BIG_TAG_MASK)
!= LBER_BIG_TAG_MASK) {
}
do {
/* reading the tag... */
- if (ber_pvt_sb_read( sb, ber->ber_rwptr, 1)<=0)
+ if (ber_int_sb_read( sb, ber->ber_rwptr, 1)<=0) {
return LBER_DEFAULT;
+ }
+
if (! (ber->ber_rwptr[0] & LBER_MORE_TAG_MASK) ) {
ber->ber_tag>>=sizeof(ber->ber_tag) -
((char *) &ber->ber_tag - ber->ber_rwptr);
ber->ber_rwptr = (char *) &ber->ber_usertag;
goto get_lenbyte;
}
- } while (PTR_IN_VAR(ber->ber_rwptr,ber->ber_tag));
+ } while( PTR_IN_VAR(ber->ber_rwptr, ber->ber_tag ));
+
errno = ERANGE; /* this is a serious error. */
return LBER_DEFAULT;
}
+
get_lenbyte:
if (ber->ber_rwptr==(char *) &ber->ber_usertag) {
unsigned char c;
- if (ber_pvt_sb_read( sb, (char *) &c, 1)<=0)
+ if (ber_int_sb_read( sb, (char *) &c, 1)<=0)
return LBER_DEFAULT;
if (c & 0x80U) {
int len = c & 0x7fU;
goto fill_buffer;
}
}
+
if (PTR_IN_VAR(ber->ber_rwptr, ber->ber_len)) {
+ unsigned char netlen[sizeof(ber_len_t)];
+
ber_slen_t res;
ber_slen_t to_go;
to_go = (char *) &ber->ber_len + sizeof( ber->ber_len ) -
ber->ber_rwptr;
assert( to_go > 0 );
- res = ber_pvt_sb_read( sb, ber->ber_rwptr, to_go );
- if (res <=0)
+ res = BerRead( sb, netlen, to_go );
+ if (res <= 0) {
return LBER_DEFAULT;
+ }
ber->ber_rwptr += res;
- if (res==to_go) {
- /* convert length. */
- ber->ber_len = LBER_LEN_NTOH( ber->ber_len );
- goto fill_buffer;
- } else {
-#if defined( EWOULDBLOCK )
- errno = EWOULDBLOCK;
-#elif defined( EAGAIN )
- errno = EAGAIN;
-#endif
- return LBER_DEFAULT;
+
+ /* convert length. */
+ ber->ber_len = 0;
+ for( to_go = 0; to_go < res ; to_go++ ) {
+ ber->ber_len <<= 8;
+ ber->ber_len |= netlen[to_go];
}
}
+
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;
}
+
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_pvt_sb_read( sb, ber->ber_rwptr, to_go );
+ res = ber_int_sb_read( sb, ber->ber_rwptr, to_go );
if (res<=0)
return LBER_DEFAULT;
ber->ber_rwptr+=res;