1 /* io.c - ber general i/o routines */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
8 * Copyright (c) 1990 Regents of the University of Michigan.
11 * Redistribution and use in source and binary forms are permitted
12 * provided that this notice is preserved and that due credit is given
13 * to the University of Michigan at Ann Arbor. The name of the University
14 * may not be used to endorse or promote products derived from this
15 * software without specific prior written permission. This software
16 * is provided ``as is'' without express or implied warranty.
23 #include <ac/stdlib.h>
27 #include <ac/socket.h>
28 #include <ac/string.h>
29 #include <ac/unistd.h>
49 assert( buf != NULL );
51 assert( SOCKBUF_VALID( sb ) );
54 if ( (c = ber_int_sb_read( sb, buf, len )) <= 0 ) {
73 ber_len_t actuallen, nleft;
75 assert( ber != NULL );
76 assert( buf != NULL );
78 assert( BER_VALID( ber ) );
80 nleft = ber_pvt_ber_remaining( ber );
81 actuallen = nleft < len ? nleft : len;
83 AC_MEMCPY( buf, ber->ber_ptr, actuallen );
85 ber->ber_ptr += actuallen;
87 return( (ber_slen_t) actuallen );
97 assert( ber != NULL );
98 assert( buf != NULL );
100 assert( BER_VALID( ber ) );
102 if ( nosos || ber->ber_sos == NULL ) {
103 if ( ber->ber_ptr + len > ber->ber_end ) {
104 if ( ber_realloc( ber, len ) != 0 )
107 AC_MEMCPY( ber->ber_ptr, buf, (size_t)len );
109 return( (ber_slen_t) len );
112 if ( ber->ber_sos->sos_ptr + len > ber->ber_end ) {
113 if ( ber_realloc( ber, len ) != 0 )
116 AC_MEMCPY( ber->ber_sos->sos_ptr, buf, (size_t)len );
117 ber->ber_sos->sos_ptr += len;
118 ber->ber_sos->sos_clen += len;
119 return( (ber_slen_t) len );
124 ber_realloc( BerElement *ber, ber_len_t len )
126 ber_len_t need, have, total;
131 assert( ber != NULL );
134 assert( BER_VALID( ber ) );
136 total = ber_pvt_ber_total( ber );
137 have = total / EXBUFSIZ;
138 need = (len < EXBUFSIZ ? 1 : (len + (EXBUFSIZ - 1)) / EXBUFSIZ);
139 total = have * EXBUFSIZ + need * EXBUFSIZ;
141 oldbuf = ber->ber_buf;
143 ber->ber_buf = (char *) LBER_REALLOC( oldbuf, total );
145 if ( ber->ber_buf == NULL ) {
146 ber->ber_buf = oldbuf;
150 ber->ber_end = ber->ber_buf + total;
153 * If the stinking thing was moved, we need to go through and
154 * reset all the sos and ber pointers. Offsets would've been
155 * a better idea... oh well.
158 if ( ber->ber_buf != oldbuf ) {
159 ber->ber_ptr = ber->ber_buf + (ber->ber_ptr - oldbuf);
161 for ( s = ber->ber_sos; s != NULL; s = s->sos_next ) {
162 off = s->sos_first - oldbuf;
163 s->sos_first = ber->ber_buf + off;
165 off = s->sos_ptr - oldbuf;
166 s->sos_ptr = ber->ber_buf + off;
174 ber_free( BerElement *ber, int freebuf )
176 #ifdef LDAP_MEMORY_DEBUG
177 assert( ber != NULL );
184 assert( BER_VALID( ber ) );
188 LBER_FREE( ber->ber_buf );
190 for( s = ber->ber_sos ; s != NULL ; s = next ) {
198 ber->ber_valid = LBER_UNINITIALIZED;
200 LBER_FREE( (char *) ber );
204 ber_flush( Sockbuf *sb, BerElement *ber, int freeit )
206 ber_len_t nwritten, towrite;
209 assert( sb != NULL );
210 assert( ber != NULL );
212 assert( SOCKBUF_VALID( sb ) );
213 assert( BER_VALID( ber ) );
215 if ( ber->ber_rwptr == NULL ) {
216 ber->ber_rwptr = ber->ber_buf;
218 towrite = ber->ber_ptr - ber->ber_rwptr;
220 if ( sb->sb_debug ) {
222 LDAP_LOG(( "liblber", LDAP_LEVEL_DETAIL1,
223 "ber_flush: %ld bytes to sd %ld%s\n",
224 towrite, (long)sb->sb_fd,
225 ber->ber_rwptr != ber->ber_buf ? " (re-flush)" : "" ));
226 BER_DUMP(( "liblber", LDAP_LEVEL_DETAIL2, ber, 1 ));
228 ber_log_printf( LDAP_DEBUG_ANY, sb->sb_debug,
229 "ber_flush: %ld bytes to sd %ld%s\n",
230 towrite, (long) sb->sb_fd,
231 ber->ber_rwptr != ber->ber_buf ? " (re-flush)" : "" );
232 ber_log_bprint( LDAP_DEBUG_PACKETS, sb->sb_debug,
233 ber->ber_rwptr, towrite );
239 rc = ber_int_sb_write( sb, ber->ber_rwptr, towrite );
245 ber->ber_rwptr += rc;
246 } while ( towrite > 0 );
255 ber_alloc_t( int options )
259 ber_int_options.lbo_valid = LBER_INITIALIZED;
261 ber = (BerElement *) LBER_CALLOC( 1, sizeof(BerElement) );
267 ber->ber_valid = LBER_VALID_BERELEMENT;
268 ber->ber_tag = LBER_DEFAULT;
269 ber->ber_options = options;
270 ber->ber_debug = ber_int_debug;
272 assert( BER_VALID( ber ) );
277 ber_alloc( void ) /* deprecated */
279 return ber_alloc_t( 0 );
283 der_alloc( void ) /* deprecated */
285 return ber_alloc_t( LBER_USE_DER );
289 ber_dup( BerElement *ber )
293 assert( ber != NULL );
294 assert( BER_VALID( ber ) );
296 if ( (new = ber_alloc_t( ber->ber_options )) == NULL ) {
302 assert( BER_VALID( new ) );
307 /* OLD U-Mich ber_init() */
309 ber_init_w_nullc( BerElement *ber, int options )
311 assert( ber != NULL );
313 ber_int_options.lbo_valid = LBER_INITIALIZED;
315 (void) memset( (char *)ber, '\0', sizeof( BerElement ));
316 ber->ber_valid = LBER_VALID_BERELEMENT;
317 ber->ber_tag = LBER_DEFAULT;
318 ber->ber_options = (char) options;
319 ber->ber_debug = ber_int_debug;
321 assert( BER_VALID( ber ) );
324 /* New C-API ber_init() */
325 /* This function constructs a BerElement containing a copy
326 ** of the data in the bv argument.
329 ber_init( struct berval *bv )
333 assert( bv != NULL );
335 ber_int_options.lbo_valid = LBER_INITIALIZED;
341 ber = ber_alloc_t( 0 );
344 /* allocation failed */
349 if ( ((ber_len_t) ber_write ( ber, bv->bv_val, bv->bv_len, 0 ))
352 /* write failed, so free and return NULL */
357 ber_reset( ber, 1 ); /* reset the pointer to the start of the buffer */
361 /* New C-API ber_flatten routine */
362 /* This routine allocates a struct berval whose contents are a BER
363 ** encoding taken from the ber argument. The bvPtr pointer pointers to
364 ** the returned berval.
368 struct berval **bvPtr)
372 assert( bvPtr != NULL );
374 ber_int_options.lbo_valid = LBER_INITIALIZED;
380 bv = LBER_MALLOC( sizeof(struct berval) );
386 /* ber is null, create an empty berval */
391 /* copy the berval */
392 ber_len_t len = ber_pvt_ber_write( ber );
394 bv->bv_val = (char *) LBER_MALLOC( len + 1 );
395 if ( bv->bv_val == NULL ) {
400 AC_MEMCPY( bv->bv_val, ber->ber_buf, len );
401 bv->bv_val[len] = '\0';
410 ber_reset( BerElement *ber, int was_writing )
412 assert( ber != NULL );
413 assert( BER_VALID( ber ) );
416 ber->ber_end = ber->ber_ptr;
417 ber->ber_ptr = ber->ber_buf;
420 ber->ber_ptr = ber->ber_end;
423 ber->ber_rwptr = NULL;
427 * A rewrite of ber_get_next that can safely be called multiple times
428 * for the same packet. It will simply continue where it stopped until
429 * a full packet is read.
438 assert( sb != NULL );
439 assert( len != NULL );
440 assert( ber != NULL );
442 assert( SOCKBUF_VALID( sb ) );
443 assert( BER_VALID( ber ) );
446 LDAP_LOG(( "liblber", LDAP_LEVEL_ENTRY, "ber_get_next: enter\n" ));
448 ber_log_printf( LDAP_DEBUG_TRACE, ber->ber_debug,
453 * Any ber element looks like this: tag length contents.
454 * Assuming everything's ok, we return the tag byte (we
455 * can assume a single byte), return the length in len,
456 * and the rest of the undecoded element in buf.
459 * 1) small tags (less than 128)
460 * 2) definite lengths
461 * 3) primitive encodings used whenever possible
464 if (ber->ber_rwptr == NULL) {
466 * dtest does like this assert.
468 /* assert( ber->ber_buf == NULL ); */
469 ber->ber_rwptr = (char *) &ber->ber_tag;
474 #define PTR_IN_VAR( ptr, var ) \
475 (((ptr)>=(char *) &(var)) && ((ptr)< (char *) &(var)+sizeof(var)))
477 if (PTR_IN_VAR(ber->ber_rwptr, ber->ber_tag)) {
478 if (ber->ber_rwptr == (char *) &ber->ber_tag) {
479 if (ber_int_sb_read( sb, ber->ber_rwptr, 1)<=0)
482 if ((ber->ber_rwptr[0] & LBER_BIG_TAG_MASK)
483 != LBER_BIG_TAG_MASK)
485 ber->ber_tag = ber->ber_rwptr[0];
486 ber->ber_rwptr = (char *) &ber->ber_usertag;
492 /* reading the tag... */
493 if (ber_int_sb_read( sb, ber->ber_rwptr, 1)<=0) {
497 if (! (ber->ber_rwptr[0] & LBER_MORE_TAG_MASK) ) {
498 ber->ber_tag>>=sizeof(ber->ber_tag) -
499 ((char *) &ber->ber_tag - ber->ber_rwptr);
500 ber->ber_rwptr = (char *) &ber->ber_usertag;
503 } while( PTR_IN_VAR(ber->ber_rwptr, ber->ber_tag ));
505 errno = ERANGE; /* this is a serious error. */
510 if (ber->ber_rwptr==(char *) &ber->ber_usertag) {
512 if (ber_int_sb_read( sb, (char *) &c, 1)<=0) {
518 if ( (len==0) || ( len>sizeof( ber->ber_len ) ) ) {
523 ber->ber_rwptr = (char *) &ber->ber_len +
524 sizeof(ber->ber_len) - len;
533 if (PTR_IN_VAR(ber->ber_rwptr, ber->ber_len)) {
534 unsigned char netlen[sizeof(ber_len_t)];
538 to_go = (char *) &ber->ber_len + sizeof( ber->ber_len ) -
542 res = BerRead( sb, netlen, to_go );
546 ber->ber_rwptr += res;
548 /* convert length. */
549 for( to_go = 0; to_go < res ; to_go++ ) {
551 ber->ber_len |= netlen[to_go];
554 if (PTR_IN_VAR(ber->ber_rwptr, ber->ber_len)) {
560 /* now fill the buffer. */
562 /* make sure length is reasonable */
563 if ( ber->ber_len == 0 ||
564 ( sb->sb_max_incoming && ber->ber_len > sb->sb_max_incoming ))
570 if (ber->ber_buf==NULL) {
571 ber->ber_buf = (char *) LBER_MALLOC( ber->ber_len );
572 if (ber->ber_buf==NULL) {
575 ber->ber_rwptr = ber->ber_buf;
576 ber->ber_ptr = ber->ber_buf;
577 ber->ber_end = ber->ber_buf + ber->ber_len;
580 if ((ber->ber_rwptr>=ber->ber_buf) && (ber->ber_rwptr<ber->ber_end)) {
584 to_go = ber->ber_end - ber->ber_rwptr;
587 res = ber_int_sb_read( sb, ber->ber_rwptr, to_go );
593 #if defined( EWOULDBLOCK )
595 #elif defined( EAGAIN )
601 ber->ber_rwptr = NULL;
603 if ( ber->ber_debug ) {
605 LDAP_LOG(( "liblber", LDAP_LEVEL_DETAIL1,
606 "ber_get_next: tag 0x%lx len %ld\n",
607 ber->ber_tag, ber->ber_len ));
608 BER_DUMP(( "liblber", LDAP_LEVEL_DETAIL2, ber, 1 ));
610 ber_log_printf( LDAP_DEBUG_TRACE, ber->ber_debug,
611 "ber_get_next: tag 0x%lx len %ld contents:\n",
612 ber->ber_tag, ber->ber_len );
613 ber_log_dump( LDAP_DEBUG_BER, ber->ber_debug, ber, 1 );
616 return (ber->ber_tag);
619 assert( 0 ); /* ber structure is messed up ?*/