1998-08-09 08:43:13 +08:00
|
|
|
/* io.c - ber general i/o routines */
|
1998-12-29 04:38:04 +08:00
|
|
|
/*
|
|
|
|
* Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
|
|
|
|
* COPYING RESTRICTIONS APPLY, see COPYRIGHT file
|
|
|
|
*/
|
1998-12-29 04:46:13 +08:00
|
|
|
/* Portions
|
1998-08-09 08:43:13 +08:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#include "portable.h"
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#include <stdio.h>
|
1999-06-03 08:37:44 +08:00
|
|
|
|
|
|
|
#include <ac/stdlib.h>
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#include <ac/ctype.h>
|
|
|
|
#include <ac/errno.h>
|
|
|
|
#include <ac/socket.h>
|
|
|
|
#include <ac/string.h>
|
|
|
|
#include <ac/unistd.h>
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
#ifdef HAVE_IO_H
|
|
|
|
#include <io.h>
|
1998-08-09 08:43:13 +08:00
|
|
|
#endif
|
|
|
|
|
1999-06-04 02:24:22 +08:00
|
|
|
#undef LDAP_F_PRE
|
|
|
|
#define LDAP_F_PRE LDAP_F_EXPORT
|
|
|
|
|
1998-10-26 09:18:41 +08:00
|
|
|
#include "lber-int.h"
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1998-10-25 09:41:42 +08:00
|
|
|
static long BerRead LDAP_P(( Sockbuf *sb, char *buf, long len ));
|
1999-03-03 04:23:14 +08:00
|
|
|
static int ber_realloc LDAP_P(( BerElement *ber, unsigned long len ));
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
#define EXBUFSIZ 1024
|
|
|
|
|
1999-03-04 00:38:56 +08:00
|
|
|
/* 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
|
|
|
|
|
Protoized, moved extern definitions to .h files, fixed related bugs.
Most function and variable definitions are now preceded by its extern
definition, for error checking. Retyped a number of functions, usually
to return void. Fixed a number of printf format errors.
API changes (in ldap/include):
Added avl_dup_ok, avl_prefixapply, removed ber_fatten (probably typo
for ber_flatten), retyped ldap_sort_strcasecmp, grew lutil.h.
A number of `extern' declarations are left (some added by protoize), to
be cleaned away later. Mostly strdup(), strcasecmp(), mktemp(), optind,
optarg, errno.
1998-11-16 06:40:11 +08:00
|
|
|
static long
|
1998-08-09 08:43:13 +08:00
|
|
|
BerRead( Sockbuf *sb, char *buf, long len )
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
long nread = 0;
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( sb != NULL );
|
|
|
|
assert( buf != NULL );
|
|
|
|
|
1999-05-29 03:33:05 +08:00
|
|
|
assert( SOCKBUF_VALID( sb ) );
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
while ( len > 0 ) {
|
1999-05-19 09:12:33 +08:00
|
|
|
if ( (c = ber_pvt_sb_read( sb, buf, len )) <= 0 ) {
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( nread > 0 )
|
|
|
|
break;
|
|
|
|
return( c );
|
|
|
|
}
|
1999-03-03 04:23:14 +08:00
|
|
|
buf+= c;
|
|
|
|
nread+=c;
|
|
|
|
len-=c;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return( nread );
|
|
|
|
}
|
|
|
|
|
|
|
|
long
|
|
|
|
ber_read( BerElement *ber, char *buf, unsigned long len )
|
|
|
|
{
|
|
|
|
unsigned long actuallen, nleft;
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
|
|
|
assert( buf != NULL );
|
|
|
|
|
1999-05-29 03:33:05 +08:00
|
|
|
assert( BER_VALID( ber ) );
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
nleft = ber->ber_end - ber->ber_ptr;
|
|
|
|
actuallen = nleft < len ? nleft : len;
|
|
|
|
|
|
|
|
SAFEMEMCPY( buf, ber->ber_ptr, (size_t)actuallen );
|
|
|
|
|
|
|
|
ber->ber_ptr += actuallen;
|
|
|
|
|
|
|
|
return( (long)actuallen );
|
|
|
|
}
|
|
|
|
|
|
|
|
long
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_write(
|
|
|
|
BerElement *ber,
|
|
|
|
LDAP_CONST char *buf,
|
|
|
|
unsigned long len,
|
|
|
|
int nosos )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
|
|
|
assert( buf != NULL );
|
|
|
|
|
1999-05-29 03:33:05 +08:00
|
|
|
assert( BER_VALID( ber ) );
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( nosos || ber->ber_sos == NULL ) {
|
|
|
|
if ( ber->ber_ptr + len > ber->ber_end ) {
|
|
|
|
if ( ber_realloc( ber, len ) != 0 )
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
SAFEMEMCPY( ber->ber_ptr, buf, (size_t)len );
|
|
|
|
ber->ber_ptr += len;
|
|
|
|
return( len );
|
|
|
|
} else {
|
|
|
|
if ( ber->ber_sos->sos_ptr + len > ber->ber_end ) {
|
|
|
|
if ( ber_realloc( ber, len ) != 0 )
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
SAFEMEMCPY( ber->ber_sos->sos_ptr, buf, (size_t)len );
|
|
|
|
ber->ber_sos->sos_ptr += len;
|
|
|
|
ber->ber_sos->sos_clen += len;
|
|
|
|
return( len );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
ber_realloc( BerElement *ber, unsigned long len )
|
|
|
|
{
|
|
|
|
unsigned long need, have, total;
|
|
|
|
Seqorset *s;
|
|
|
|
long off;
|
|
|
|
char *oldbuf;
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
|
|
|
assert( len > 0 );
|
|
|
|
|
1999-05-29 03:33:05 +08:00
|
|
|
assert( BER_VALID( ber ) );
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
have = (ber->ber_end - ber->ber_buf) / EXBUFSIZ;
|
|
|
|
need = (len < EXBUFSIZ ? 1 : (len + (EXBUFSIZ - 1)) / EXBUFSIZ);
|
|
|
|
total = have * EXBUFSIZ + need * EXBUFSIZ;
|
|
|
|
|
|
|
|
oldbuf = ber->ber_buf;
|
|
|
|
|
1999-05-29 09:19:14 +08:00
|
|
|
ber->ber_buf = (char *) LBER_REALLOC( ber->ber_buf, total );
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( ber->ber_buf == NULL ) {
|
1999-05-29 09:19:14 +08:00
|
|
|
ber->ber_buf = oldbuf;
|
1998-08-09 08:43:13 +08:00
|
|
|
return( -1 );
|
1999-05-29 09:19:14 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
ber->ber_end = ber->ber_buf + total;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the stinking thing was moved, we need to go through and
|
|
|
|
* reset all the sos and ber pointers. Offsets would've been
|
|
|
|
* 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 != NULLSEQORSET; s = s->sos_next ) {
|
|
|
|
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
|
|
|
|
ber_free( BerElement *ber, int freebuf )
|
|
|
|
{
|
1999-06-08 02:48:22 +08:00
|
|
|
#ifdef LDAP_MEMORY_DEBUG
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
1999-06-08 02:48:22 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
if( ber == NULL ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-05-29 03:33:05 +08:00
|
|
|
assert( BER_VALID( ber ) );
|
1999-05-19 09:12:33 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( freebuf && ber->ber_buf != NULL )
|
1999-05-29 09:19:14 +08:00
|
|
|
LBER_FREE( ber->ber_buf );
|
1999-05-29 04:19:28 +08:00
|
|
|
|
1999-01-29 14:07:02 +08:00
|
|
|
ber->ber_buf = NULL;
|
1999-05-29 03:33:05 +08:00
|
|
|
ber->ber_valid = LBER_UNINITIALIZED;
|
1999-05-29 04:19:28 +08:00
|
|
|
|
1999-05-29 09:19:14 +08:00
|
|
|
LBER_FREE( (char *) ber );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ber_flush( Sockbuf *sb, BerElement *ber, int freeit )
|
|
|
|
{
|
1999-03-03 04:23:14 +08:00
|
|
|
long nwritten, towrite, rc;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( sb != NULL );
|
|
|
|
assert( ber != NULL );
|
|
|
|
|
1999-05-29 03:33:05 +08:00
|
|
|
assert( SOCKBUF_VALID( ber ) );
|
|
|
|
assert( BER_VALID( ber ) );
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( ber->ber_rwptr == NULL ) {
|
|
|
|
ber->ber_rwptr = ber->ber_buf;
|
|
|
|
}
|
|
|
|
towrite = ber->ber_ptr - ber->ber_rwptr;
|
|
|
|
|
1998-12-22 09:34:01 +08:00
|
|
|
if ( sb->sb_debug ) {
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_log_printf( LDAP_DEBUG_ANY, sb->sb_debug,
|
1998-12-22 09:34:01 +08:00
|
|
|
"ber_flush: %ld bytes to sd %ld%s\n", towrite,
|
1998-08-21 03:42:38 +08:00
|
|
|
(long) sb->sb_sd, ber->ber_rwptr != ber->ber_buf ? " (re-flush)"
|
1998-08-09 08:43:13 +08:00
|
|
|
: "" );
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_log_bprint( LDAP_DEBUG_PACKETS, sb->sb_debug,
|
1998-12-22 09:34:01 +08:00
|
|
|
ber->ber_rwptr, towrite );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1998-12-22 09:34:01 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
#if !defined(MACOS) && !defined(DOS)
|
|
|
|
if ( sb->sb_options & (LBER_TO_FILE | LBER_TO_FILE_ONLY) ) {
|
1999-03-03 04:23:14 +08:00
|
|
|
rc = write( sb->sb_fd, ber->ber_rwptr, towrite );
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( sb->sb_options & LBER_TO_FILE_ONLY ) {
|
|
|
|
return( (int)rc );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
1999-03-03 04:23:14 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
nwritten = 0;
|
|
|
|
do {
|
1999-05-19 09:12:33 +08:00
|
|
|
rc = ber_pvt_sb_write( sb, ber->ber_rwptr, towrite );
|
1999-03-03 04:23:14 +08:00
|
|
|
if (rc<=0) {
|
|
|
|
return -1;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
towrite -= rc;
|
|
|
|
nwritten += rc;
|
|
|
|
ber->ber_rwptr += rc;
|
|
|
|
} while ( towrite > 0 );
|
|
|
|
|
|
|
|
if ( freeit )
|
|
|
|
ber_free( ber, 1 );
|
|
|
|
|
|
|
|
return( 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
BerElement *
|
|
|
|
ber_alloc_t( int options )
|
|
|
|
{
|
|
|
|
BerElement *ber;
|
|
|
|
|
1999-05-29 13:16:31 +08:00
|
|
|
ber_int_options.lbo_valid = LBER_INITIALIZED;
|
|
|
|
|
1999-05-29 09:19:14 +08:00
|
|
|
ber = (BerElement *) LBER_CALLOC( 1, sizeof(BerElement) );
|
1999-05-19 09:12:33 +08:00
|
|
|
|
|
|
|
if ( ber == NULLBER )
|
1998-08-09 08:43:13 +08:00
|
|
|
return( NULLBER );
|
1999-05-19 09:12:33 +08:00
|
|
|
|
1999-05-29 03:33:05 +08:00
|
|
|
ber->ber_valid = LBER_VALID_BERELEMENT;
|
1998-08-09 08:43:13 +08:00
|
|
|
ber->ber_tag = LBER_DEFAULT;
|
1998-12-22 09:34:01 +08:00
|
|
|
ber->ber_options = options;
|
1999-05-19 09:12:33 +08:00
|
|
|
ber->ber_debug = ber_int_debug;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-29 03:33:05 +08:00
|
|
|
assert( BER_VALID( ber ) );
|
1998-08-09 08:43:13 +08:00
|
|
|
return( ber );
|
|
|
|
}
|
|
|
|
|
|
|
|
BerElement *
|
1999-05-21 05:21:57 +08:00
|
|
|
ber_alloc( void ) /* deprecated */
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
return( ber_alloc_t( 0 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
BerElement *
|
1999-05-21 05:21:57 +08:00
|
|
|
der_alloc( void ) /* deprecated */
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
return( ber_alloc_t( LBER_USE_DER ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
BerElement *
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_dup( LDAP_CONST BerElement *ber )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
|
|
|
BerElement *new;
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
1999-05-29 03:33:05 +08:00
|
|
|
assert( BER_VALID( ber ) );
|
1999-05-19 09:12:33 +08:00
|
|
|
|
1999-05-21 05:21:57 +08:00
|
|
|
if ( (new = ber_alloc_t( ber->ber_options )) == NULL ) {
|
1999-05-19 09:12:33 +08:00
|
|
|
return( NULL );
|
1999-05-21 05:21:57 +08:00
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
*new = *ber;
|
|
|
|
|
1999-05-29 03:33:05 +08:00
|
|
|
assert( BER_VALID( new ) );
|
1998-08-09 08:43:13 +08:00
|
|
|
return( new );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-12-19 05:08:28 +08:00
|
|
|
/* OLD U-Mich ber_init() */
|
1998-08-09 08:43:13 +08:00
|
|
|
void
|
1998-10-26 10:38:39 +08:00
|
|
|
ber_init_w_nullc( BerElement *ber, int options )
|
1998-08-09 08:43:13 +08:00
|
|
|
{
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
|
|
|
|
1999-05-29 13:16:31 +08:00
|
|
|
ber_int_options.lbo_valid = LBER_INITIALIZED;
|
|
|
|
|
1999-03-03 23:35:29 +08:00
|
|
|
(void) memset( (char *)ber, '\0', sizeof( BerElement ));
|
1999-05-29 03:33:05 +08:00
|
|
|
ber->ber_valid = LBER_VALID_BERELEMENT;
|
1998-08-09 08:43:13 +08:00
|
|
|
ber->ber_tag = LBER_DEFAULT;
|
1998-10-25 09:41:42 +08:00
|
|
|
ber->ber_options = (char) options;
|
1999-05-21 05:21:57 +08:00
|
|
|
ber->ber_debug = ber_int_debug;
|
1999-05-29 03:33:05 +08:00
|
|
|
|
|
|
|
assert( BER_VALID( ber ) );
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
|
|
|
|
1998-12-19 05:08:28 +08:00
|
|
|
/* New C-API ber_init() */
|
|
|
|
/* This function constructs a BerElement containing a copy
|
|
|
|
** of the data in the bv argument.
|
|
|
|
*/
|
1998-10-26 10:38:39 +08:00
|
|
|
BerElement *
|
|
|
|
ber_init( struct berval *bv )
|
|
|
|
{
|
1998-12-19 05:08:28 +08:00
|
|
|
BerElement *ber;
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( bv != NULL );
|
|
|
|
|
1999-05-29 13:16:31 +08:00
|
|
|
ber_int_options.lbo_valid = LBER_INITIALIZED;
|
|
|
|
|
1998-12-19 05:08:28 +08:00
|
|
|
if ( bv == NULL ) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ber = ber_alloc_t( 0 );
|
|
|
|
|
|
|
|
if( ber == NULLBER ) {
|
|
|
|
/* allocation failed */
|
|
|
|
return ( NULL );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* copy the data */
|
1999-02-12 05:16:03 +08:00
|
|
|
if ( ( (unsigned int) ber_write ( ber, bv->bv_val, bv->bv_len, 0 )) != bv->bv_len ) {
|
1998-12-19 05:08:28 +08:00
|
|
|
/* 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 );
|
1998-10-26 10:38:39 +08:00
|
|
|
}
|
|
|
|
|
1998-12-19 05:08:28 +08:00
|
|
|
/* 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 pointers to
|
|
|
|
** the returned berval.
|
|
|
|
*/
|
|
|
|
int ber_flatten(
|
1999-05-19 09:12:33 +08:00
|
|
|
LDAP_CONST BerElement *ber,
|
1998-12-19 05:08:28 +08:00
|
|
|
struct berval **bvPtr)
|
|
|
|
{
|
|
|
|
struct berval *bv;
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( bvPtr != NULL );
|
|
|
|
|
1999-05-29 13:16:31 +08:00
|
|
|
ber_int_options.lbo_valid = LBER_INITIALIZED;
|
|
|
|
|
1998-12-19 05:08:28 +08:00
|
|
|
if(bvPtr == NULL) {
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
1999-05-29 09:19:14 +08:00
|
|
|
if ( (bv = LBER_MALLOC( sizeof(struct berval))) == NULL ) {
|
1998-12-19 05:08:28 +08:00
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ber == NULL ) {
|
|
|
|
/* ber is null, create an empty berval */
|
|
|
|
bv->bv_val = NULL;
|
|
|
|
bv->bv_len = 0;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/* copy the berval */
|
|
|
|
ptrdiff_t len = ber->ber_ptr - ber->ber_buf;
|
|
|
|
|
1999-05-29 09:19:14 +08:00
|
|
|
if ( (bv->bv_val = (char *) LBER_MALLOC( len + 1 )) == NULL ) {
|
1998-12-19 05:08:28 +08:00
|
|
|
ber_bvfree( bv );
|
|
|
|
return( -1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
SAFEMEMCPY( bv->bv_val, ber->ber_buf, (size_t)len );
|
|
|
|
bv->bv_val[len] = '\0';
|
|
|
|
bv->bv_len = len;
|
|
|
|
}
|
|
|
|
|
|
|
|
*bvPtr = bv;
|
|
|
|
return( 0 );
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
void
|
|
|
|
ber_reset( BerElement *ber, int was_writing )
|
|
|
|
{
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( ber != NULL );
|
1999-05-29 03:33:05 +08:00
|
|
|
assert( BER_VALID( ber ) );
|
1999-05-19 09:12:33 +08:00
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
if ( was_writing ) {
|
|
|
|
ber->ber_end = ber->ber_ptr;
|
|
|
|
ber->ber_ptr = ber->ber_buf;
|
|
|
|
} else {
|
|
|
|
ber->ber_ptr = ber->ber_end;
|
|
|
|
}
|
|
|
|
|
|
|
|
ber->ber_rwptr = NULL;
|
|
|
|
}
|
|
|
|
|
1999-03-04 00:38:56 +08:00
|
|
|
#if 0
|
1998-08-09 08:43:13 +08:00
|
|
|
/* return the tag - LBER_DEFAULT returned means trouble */
|
|
|
|
static unsigned long
|
|
|
|
get_tag( Sockbuf *sb )
|
|
|
|
{
|
|
|
|
unsigned char xbyte;
|
|
|
|
unsigned long tag;
|
|
|
|
char *tagp;
|
1998-11-04 21:15:18 +08:00
|
|
|
unsigned int i;
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( sb != NULL );
|
1999-05-29 03:33:05 +08:00
|
|
|
assert( SOCKBUF_VALID( sb ) );
|
1999-05-19 09:12:33 +08:00
|
|
|
|
|
|
|
if ( ber_pvt_sb_read( sb, (char *) &xbyte, 1 ) != 1 )
|
1998-08-09 08:43:13 +08:00
|
|
|
return( LBER_DEFAULT );
|
|
|
|
|
|
|
|
if ( (xbyte & LBER_BIG_TAG_MASK) != LBER_BIG_TAG_MASK )
|
|
|
|
return( (unsigned long) xbyte );
|
|
|
|
|
|
|
|
tagp = (char *) &tag;
|
|
|
|
tagp[0] = xbyte;
|
|
|
|
for ( i = 1; i < sizeof(long); i++ ) {
|
1999-05-19 09:12:33 +08:00
|
|
|
if ( ber_pvt_sb_read( sb, (char *) &xbyte, 1 ) != 1 )
|
1998-08-09 08:43:13 +08:00
|
|
|
return( LBER_DEFAULT );
|
|
|
|
|
|
|
|
tagp[i] = xbyte;
|
|
|
|
|
|
|
|
if ( ! (xbyte & LBER_MORE_TAG_MASK) )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* tag too big! */
|
|
|
|
if ( i == sizeof(long) )
|
|
|
|
return( LBER_DEFAULT );
|
|
|
|
|
|
|
|
/* want leading, not trailing 0's */
|
|
|
|
return( tag >> (sizeof(long) - i - 1) );
|
|
|
|
}
|
1999-03-04 00:38:56 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
1999-05-19 09:12:33 +08:00
|
|
|
* A rewrite of ber_get_next that can safely be called multiple times
|
1999-03-04 00:38:56 +08:00
|
|
|
* for the same packet. It will simply continue were it stopped until
|
|
|
|
* a full packet is read.
|
|
|
|
*/
|
1998-08-09 08:43:13 +08:00
|
|
|
|
|
|
|
unsigned long
|
|
|
|
ber_get_next( Sockbuf *sb, unsigned long *len, BerElement *ber )
|
|
|
|
{
|
1999-05-19 09:12:33 +08:00
|
|
|
assert( sb != NULL );
|
|
|
|
assert( len != NULL );
|
|
|
|
assert( ber != NULL );
|
|
|
|
|
1999-05-29 03:33:05 +08:00
|
|
|
assert( SOCKBUF_VALID( sb ) );
|
|
|
|
assert( BER_VALID( ber ) );
|
|
|
|
|
|
|
|
ber_log_printf( LDAP_DEBUG_TRACE, ber->ber_debug,
|
|
|
|
"ber_get_next\n" );
|
|
|
|
|
1998-08-09 08:43:13 +08:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
1999-03-04 00:38:56 +08:00
|
|
|
|
|
|
|
if (ber->ber_rwptr == NULL) {
|
1999-05-21 06:28:45 +08:00
|
|
|
/* XXYYZ
|
|
|
|
* dtest does like this assert.
|
|
|
|
*/
|
|
|
|
/* assert( ber->ber_buf == NULL ); */
|
1999-03-04 00:38:56 +08:00
|
|
|
ber->ber_rwptr = (char *) &ber->ber_tag;
|
|
|
|
ber->ber_tag = 0;
|
|
|
|
}
|
1998-08-09 08:43:13 +08:00
|
|
|
|
1999-03-04 00:38:56 +08:00
|
|
|
#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) {
|
1999-05-19 09:12:33 +08:00
|
|
|
if (ber_pvt_sb_read( sb, ber->ber_rwptr, 1)<=0)
|
1999-03-04 00:38:56 +08:00
|
|
|
return LBER_DEFAULT;
|
|
|
|
if ((ber->ber_rwptr[0] & LBER_BIG_TAG_MASK)
|
|
|
|
!= LBER_BIG_TAG_MASK) {
|
|
|
|
ber->ber_tag = ber->ber_rwptr[0];
|
|
|
|
ber->ber_rwptr = (char *) &ber->ber_usertag;
|
|
|
|
goto get_lenbyte;
|
|
|
|
}
|
|
|
|
ber->ber_rwptr++;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1999-03-04 00:38:56 +08:00
|
|
|
do {
|
|
|
|
/* reading the tag... */
|
1999-05-19 09:12:33 +08:00
|
|
|
if (ber_pvt_sb_read( sb, ber->ber_rwptr, 1)<=0)
|
1999-03-04 00:38:56 +08:00
|
|
|
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));
|
|
|
|
errno = ERANGE; /* this is a serious error. */
|
|
|
|
return LBER_DEFAULT;
|
|
|
|
}
|
|
|
|
get_lenbyte:
|
|
|
|
if (ber->ber_rwptr==(char *) &ber->ber_usertag) {
|
|
|
|
unsigned char c;
|
1999-05-19 09:12:33 +08:00
|
|
|
if (ber_pvt_sb_read( sb, (char *) &c, 1)<=0)
|
1999-03-04 00:38:56 +08:00
|
|
|
return LBER_DEFAULT;
|
1999-05-19 14:13:44 +08:00
|
|
|
if (c & 0x80U) {
|
|
|
|
int len = c & 0x7fU;
|
1999-04-27 13:19:32 +08:00
|
|
|
if ( (len==0) || ((unsigned) len>sizeof( ber->ber_len ) ) ) {
|
1999-03-04 00:38:56 +08:00
|
|
|
errno = ERANGE;
|
|
|
|
return LBER_DEFAULT;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1999-03-04 00:38:56 +08:00
|
|
|
ber->ber_rwptr = (char *) &ber->ber_len +
|
|
|
|
sizeof(ber->ber_len) - len;
|
|
|
|
ber->ber_len = 0;
|
1998-08-09 08:43:13 +08:00
|
|
|
} else {
|
1999-03-04 00:38:56 +08:00
|
|
|
ber->ber_len = c;
|
|
|
|
goto fill_buffer;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1999-03-04 00:38:56 +08:00
|
|
|
}
|
|
|
|
if (PTR_IN_VAR(ber->ber_rwptr, ber->ber_len)) {
|
|
|
|
int res;
|
|
|
|
int to_go;
|
|
|
|
to_go = (char *) &ber->ber_len + sizeof( ber->ber_len ) -
|
|
|
|
ber->ber_rwptr;
|
|
|
|
assert( to_go > 0 );
|
1999-05-19 09:12:33 +08:00
|
|
|
res = ber_pvt_sb_read( sb, ber->ber_rwptr, to_go );
|
1999-03-04 00:38:56 +08:00
|
|
|
if (res <=0)
|
|
|
|
return LBER_DEFAULT;
|
|
|
|
ber->ber_rwptr += res;
|
|
|
|
if (res==to_go) {
|
|
|
|
/* convert length. */
|
|
|
|
ber->ber_len = AC_NTOHL( ber->ber_len );
|
|
|
|
goto fill_buffer;
|
|
|
|
} else {
|
|
|
|
#if defined( EWOULDBLOCK )
|
|
|
|
errno = EWOULDBLOCK;
|
|
|
|
#elif defined( EAGAIN )
|
|
|
|
errno = EAGAIN;
|
|
|
|
#endif
|
|
|
|
return LBER_DEFAULT;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1999-03-04 00:38:56 +08:00
|
|
|
}
|
|
|
|
fill_buffer:
|
|
|
|
/* now fill the buffer. */
|
|
|
|
if (ber->ber_buf==NULL) {
|
|
|
|
if (ber->ber_len > MAX_BERBUFSIZE) {
|
|
|
|
errno = ERANGE;
|
|
|
|
return LBER_DEFAULT;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1999-05-29 09:19:14 +08:00
|
|
|
ber->ber_buf = (char *) LBER_MALLOC( ber->ber_len );
|
1999-03-04 00:38:56 +08:00
|
|
|
if (ber->ber_buf==NULL)
|
|
|
|
return LBER_DEFAULT;
|
1998-08-09 08:43:13 +08:00
|
|
|
ber->ber_rwptr = ber->ber_buf;
|
1999-03-04 00:38:56 +08:00
|
|
|
ber->ber_ptr = ber->ber_buf;
|
|
|
|
ber->ber_end = ber->ber_buf + ber->ber_len;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1999-03-04 00:38:56 +08:00
|
|
|
if ((ber->ber_rwptr>=ber->ber_buf) && (ber->ber_rwptr<ber->ber_end)) {
|
|
|
|
int res;
|
|
|
|
int to_go;
|
|
|
|
|
|
|
|
to_go = ber->ber_end - ber->ber_rwptr;
|
|
|
|
assert( to_go > 0 );
|
|
|
|
|
1999-05-19 09:12:33 +08:00
|
|
|
res = ber_pvt_sb_read( sb, ber->ber_rwptr, to_go );
|
1999-03-04 00:38:56 +08:00
|
|
|
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;
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1999-03-04 00:38:56 +08:00
|
|
|
|
|
|
|
ber->ber_rwptr = NULL;
|
|
|
|
*len = ber->ber_len;
|
|
|
|
if ( ber->ber_debug ) {
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_log_printf( LDAP_DEBUG_TRACE, ber->ber_debug,
|
1999-03-04 00:38:56 +08:00
|
|
|
"ber_get_next: tag 0x%lx len %ld contents:\n",
|
|
|
|
ber->ber_tag, ber->ber_len );
|
1999-05-19 09:12:33 +08:00
|
|
|
ber_log_dump( LDAP_DEBUG_BER, ber->ber_debug, ber, 1 );
|
1999-03-04 00:38:56 +08:00
|
|
|
}
|
|
|
|
return (ber->ber_tag);
|
1998-08-09 08:43:13 +08:00
|
|
|
}
|
1999-03-04 00:38:56 +08:00
|
|
|
assert( 0 ); /* ber structure is messed up ?*/
|
|
|
|
return LBER_DEFAULT;
|
|
|
|
}
|