Back to index

lightning-sunbird  0.9+nobinonly
Classes | Defines | Typedefs | Functions | Variables
io.c File Reference
#include "lber-int.h"

Go to the source code of this file.

Classes

struct  byte_buffer

Defines

#define bergetc(sb, len)
#define BerWrite(sb, b, l)   write( sb->sb_sd, b, l )
#define udp_read(sb, b, l, al)
#define udp_write(sb, b, l)
#define EXBUFSIZ   1024

Typedefs

typedef struct byte_buffer

Functions

static void nslberi_install_compat_io_fns (Sockbuf *sb)
static int nslberi_extread_compat (int s, void *buf, int len, struct lextiof_socket_private *arg)
static int nslberi_extwrite_compat (int s, const void *buf, int len, struct lextiof_socket_private *arg)
static unsigned long get_tag (Sockbuf *sb, BerElement *ber)
static unsigned long get_ber_len (BerElement *ber)
static unsigned long read_len_in_ber (Sockbuf *sb, BerElement *ber)
static int ber_filbuf (Sockbuf *sb, long len)
static long BerRead (Sockbuf *sb, char *buf, long len)
long LDAP_CALL ber_read (BerElement *ber, char *buf, unsigned long len)
int nslberi_ber_realloc (BerElement *ber, unsigned long len)
long LDAP_CALL ber_write (BerElement *ber, char *buf, unsigned long len, int nosos)
void LDAP_CALL ber_free (BerElement *ber, int freebuf)
int LDAP_CALL ber_flush (Sockbuf *sb, BerElement *ber, int freeit)
BerElement *LDAP_CALL ber_alloc_t (int options)
BerElement *LDAP_CALL ber_alloc ()
BerElement *LDAP_CALL der_alloc ()
BerElement *LDAP_CALL ber_dup (BerElement *ber)
void LDAP_CALL ber_init_w_nullchar (BerElement *ber, int options)
void LDAP_CALL ber_reset (BerElement *ber, int was_writing)
unsigned long LDAP_CALL ber_get_next (Sockbuf *sb, unsigned long *len, BerElement *ber)
Sockbuf *LDAP_CALL ber_sockbuf_alloc ()
void LDAP_CALL ber_sockbuf_free (Sockbuf *p)
int LDAP_CALL ber_set_option (struct berelement *ber, int option, void *value)
int LDAP_CALL ber_get_option (struct berelement *ber, int option, void *value)
int LDAP_CALL ber_sockbuf_set_option (Sockbuf *sb, int option, void *value)
int LDAP_CALL ber_sockbuf_get_option (Sockbuf *sb, int option, void *value)
void *LDAP_CALL ber_special_alloc (size_t size, BerElement **ppBer)
void LDAP_CALL ber_special_free (void *buf, BerElement *ber)
static int read_bytes (byte_buffer *b, unsigned char *return_buffer, int bytes_to_read)
static unsigned long get_buffer_tag (byte_buffer *sb)
unsigned long LDAP_CALL ber_get_next_buffer (void *buffer, size_t buffer_size, unsigned long *len, BerElement *ber, unsigned long *Bytes_Scanned)
unsigned long LDAP_CALL ber_get_next_buffer_ext (void *buffer, size_t buffer_size, unsigned long *len, BerElement *ber, unsigned long *Bytes_Scanned, Sockbuf *sock)
int LDAP_CALL ber_flatten (BerElement *ber, struct berval **bvPtr)
BerElement *LDAP_CALL ber_init (const struct berval *bv)
voidnslberi_malloc (size_t size)
voidnslberi_calloc (size_t nelem, size_t elsize)
voidnslberi_realloc (void *ptr, size_t size)
void nslberi_free (void *ptr)

Variables

static struct lber_memalloc_fns

Class Documentation

struct byte_buffer

Definition at line 1046 of file io.c.

Collaboration diagram for byte_buffer:
Class Members
int length
int offset
unsigned char * p

Define Documentation

#define bergetc (   sb,
  len 
)
Value:
( sb->sb_ber.ber_end > sb->sb_ber.ber_ptr ? \
                       (unsigned char)*sb->sb_ber.ber_ptr++ : \
                       ber_filbuf( sb, len ))

Definition at line 53 of file io.c.

#define BerWrite (   sb,
  b,
  l 
)    write( sb->sb_sd, b, l )

Definition at line 75 of file io.c.

#define EXBUFSIZ   1024

Definition at line 89 of file io.c.

#define udp_read (   sb,
  b,
  l,
  al 
)
Value:
recvfrom(sb->sb_sd, (char *)b, l, 0, \
              (struct sockaddr *)sb->sb_fromaddr, \
              (al = sizeof(struct sockaddr), &al))

Definition at line 76 of file io.c.

#define udp_write (   sb,
  b,
  l 
)
Value:
sendto(sb->sb_sd, (char *)(b), l, 0, \
              (struct sockaddr *)sb->sb_useaddr, sizeof(struct sockaddr))

Definition at line 79 of file io.c.


Typedef Documentation

typedef struct byte_buffer

Definition at line 1051 of file io.c.


Function Documentation

BerElement* LDAP_CALL ber_alloc ( )

Definition at line 475 of file io.c.

{
       return( ber_alloc_t( 0 ) );
}

Here is the call graph for this function:

Here is the caller graph for this function:

BerElement* LDAP_CALL ber_alloc_t ( int  options)

Definition at line 444 of file io.c.

{
       BerElement    *ber;

       if ( (ber = (BerElement*)NSLBERI_CALLOC( 1,
           sizeof(struct berelement) + EXBUFSIZ )) == NULL ) {
              return( NULL );
       }

       /*
        * for compatibility with the C LDAP API standard, we recognize
        * LBER_USE_DER as LBER_OPT_USE_DER.  See lber.h for a bit more info.
        */
       if ( options & LBER_USE_DER ) {
              options &= ~LBER_USE_DER;
              options |= LBER_OPT_USE_DER;
       }

       ber->ber_tag = LBER_DEFAULT;
       ber->ber_options = options;
       ber->ber_buf = (char*)ber + sizeof(struct berelement);
       ber->ber_ptr = ber->ber_buf;
       ber->ber_end = ber->ber_buf + EXBUFSIZ;
       ber->ber_flags = LBER_FLAG_NO_FREE_BUFFER;

       return( ber );
}

Here is the caller graph for this function:

BerElement* LDAP_CALL ber_dup ( BerElement *  ber)

Definition at line 489 of file io.c.

{
       BerElement    *new;

       if ( (new = ber_alloc()) == NULL )
              return( NULL );

       *new = *ber;

       return( new );
}

Here is the call graph for this function:

Here is the caller graph for this function:

static int ber_filbuf ( Sockbuf *  sb,
long  len 
) [static]

Definition at line 118 of file io.c.

{
       short  rc;
#ifdef CLDAP
       int    addrlen;
#endif /* CLDAP */

       if ( sb->sb_ber.ber_buf == NULL ) {
              if ( (sb->sb_ber.ber_buf = (char *)NSLBERI_MALLOC(
                  READBUFSIZ )) == NULL ) {
                     return( -1 );
              }
              sb->sb_ber.ber_flags &= ~LBER_FLAG_NO_FREE_BUFFER;
              sb->sb_ber.ber_ptr = sb->sb_ber.ber_buf;
              sb->sb_ber.ber_end = sb->sb_ber.ber_buf;
       }

       if ( sb->sb_naddr > 0 ) {
#ifdef CLDAP
              rc = udp_read(sb, sb->sb_ber.ber_buf, READBUFSIZ, addrlen );
#ifdef LDAP_DEBUG
              if ( lber_debug ) {
                     char msg[80];
                     sprintf( msg, "ber_filbuf udp_read %d bytes\n",
                            rc );
                     ber_err_print( msg );
                     if ( lber_debug > 1 && rc > 0 )
                            lber_bprint( sb->sb_ber.ber_buf, rc );
              }
#endif /* LDAP_DEBUG */
#else /* CLDAP */
              rc = -1;
#endif /* CLDAP */
       } else {
              if ( sb->sb_ext_io_fns.lbextiofn_read != NULL ) {
                     rc = sb->sb_ext_io_fns.lbextiofn_read(
                         sb->sb_sd, sb->sb_ber.ber_buf,
                         ((sb->sb_options & LBER_SOCKBUF_OPT_NO_READ_AHEAD)
                         && (len < READBUFSIZ)) ? len : READBUFSIZ,
                         sb->sb_ext_io_fns.lbextiofn_socket_arg );
              } else {
#ifdef NSLDAPI_AVOID_OS_SOCKETS
                     return( -1 );
#else
                     rc = read( sb->sb_sd, sb->sb_ber.ber_buf,
                         ((sb->sb_options & LBER_SOCKBUF_OPT_NO_READ_AHEAD)
                         && (len < READBUFSIZ)) ? len : READBUFSIZ );
#endif
              }
       }

       if ( rc > 0 ) {
              sb->sb_ber.ber_ptr = sb->sb_ber.ber_buf + 1;
              sb->sb_ber.ber_end = sb->sb_ber.ber_buf + rc;
              return( (unsigned char)*sb->sb_ber.ber_buf );
       }

       return( -1 );
}

Here is the call graph for this function:

int LDAP_CALL ber_flatten ( BerElement *  ber,
struct berval **  bvPtr 
)

Definition at line 1318 of file io.c.

{
       struct berval *new;
       unsigned long len;

       /* allocate a struct berval */
       if ( (new = (struct berval *)NSLBERI_MALLOC( sizeof(struct berval) ))
           == NULL ) {
              return( -1 );
       }

       /*
        * Copy everything from the BerElement's ber_buf to ber_ptr
        * into the berval structure.
        */
       if ( ber == NULL ) {
           new->bv_val = NULL;
           new->bv_len = 0;
       } else {
           len = ber->ber_ptr - ber->ber_buf; 
           if ( ( new->bv_val = (char *)NSLBERI_MALLOC( len + 1 )) == NULL ) {
                  ber_bvfree( new );
                  return( -1 );
           }
           SAFEMEMCPY( new->bv_val, ber->ber_buf, (size_t)len ); 
           new->bv_val[len] = '\0';
           new->bv_len = len;
       }

       /* set bvPtr pointer to point to the returned berval */
       *bvPtr = new; 

        return( 0 );
}

Here is the call graph for this function:

Here is the caller graph for this function:

int LDAP_CALL ber_flush ( Sockbuf *  sb,
BerElement *  ber,
int  freeit 
)

Definition at line 334 of file io.c.

{
       long   nwritten = 0, towrite, rc;
       int     i = 0;
       
       if (ber->ber_rwptr == NULL) {
         ber->ber_rwptr = ber->ber_buf;
       } else if (ber->ber_rwptr >= ber->ber_end) {
         /* we will use the ber_rwptr to continue an exited flush,
            so if rwptr is not within the buffer we return an error. */
         return( -1 );
       }
       
       /* writev section - for iDAR only!!! This section ignores freeit!!! */
       if (sb->sb_ext_io_fns.lbextiofn_writev != NULL) {

         /* add the sizes of the different buffers to write with writev */
         for (towrite = 0, i = 0; i < BER_ARRAY_QUANTITY; ++i) {
           /* don't add lengths of null buffers - writev will ignore them */
           if (ber->ber_struct[i].ldapiov_base) {
             towrite += ber->ber_struct[i].ldapiov_len;
           }
         }
         
         rc = sb->sb_ext_io_fns.lbextiofn_writev(sb->sb_sd, ber->ber_struct, BER_ARRAY_QUANTITY, 
                                            sb->sb_ext_io_fns.lbextiofn_socket_arg);
         
         if (rc >= 0) {
           /* return the number of bytes TO BE written */
           return (towrite - rc);
         } else {
           /* otherwise it's an error */
           return (rc);
         }
       } /* end writev section */

       towrite = ber->ber_ptr - ber->ber_rwptr;

#ifdef LDAP_DEBUG
       if ( lber_debug ) {
              char msg[80];
              sprintf( msg, "ber_flush: %ld bytes to sd %ld%s\n", towrite,
                  sb->sb_sd, ber->ber_rwptr != ber->ber_buf ? " (re-flush)"
                  : "" );
              ber_err_print( msg );
              if ( lber_debug > 1 )
                     lber_bprint( ber->ber_rwptr, towrite );
       }
#endif
#if !defined(macintosh) && !defined(DOS)
       if ( sb->sb_options & (LBER_SOCKBUF_OPT_TO_FILE | LBER_SOCKBUF_OPT_TO_FILE_ONLY) ) {
              rc = write( sb->sb_copyfd, ber->ber_buf, towrite );
              if ( sb->sb_options & LBER_SOCKBUF_OPT_TO_FILE_ONLY ) {
                     return( (int)rc );
              }
       }
#endif

       nwritten = 0;
       do {
              if (sb->sb_naddr > 0) {
#ifdef CLDAP
                     rc = udp_write( sb, ber->ber_buf + nwritten,
                         (size_t)towrite );
#else /* CLDAP */
                     rc = -1;
#endif /* CLDAP */
                     if ( rc <= 0 )
                            return( -1 );
                     /* fake error if write was not atomic */
                     if (rc < towrite) {
#if !defined( macintosh ) && !defined( DOS )
                         errno = EMSGSIZE;  /* For Win32, see portable.h */
#endif
                         return( -1 );
                     }
              } else {
                     if ( sb->sb_ext_io_fns.lbextiofn_write != NULL ) {
                            if ( (rc = sb->sb_ext_io_fns.lbextiofn_write(
                                sb->sb_sd, ber->ber_rwptr, (size_t)towrite,
                                sb->sb_ext_io_fns.lbextiofn_socket_arg ))
                                <= 0 ) {
                                   return( -1 );
                            }
                     } else {
#ifdef NSLDAPI_AVOID_OS_SOCKETS
                            return( -1 );
#else
                            if ( (rc = BerWrite( sb, ber->ber_rwptr,
                                (size_t) towrite )) <= 0 ) {
                                   return( -1 );
                            }
#endif
                     }
              }
              towrite -= rc;
              nwritten += rc;
              ber->ber_rwptr += rc;
       } while ( towrite > 0 );

       if ( freeit )
              ber_free( ber, 1 );

       return( 0 );
}

Here is the call graph for this function:

Here is the caller graph for this function:

void LDAP_CALL ber_free ( BerElement *  ber,
int  freebuf 
)

Definition at line 318 of file io.c.

{
       if ( ber != NULL ) {
                  if ( freebuf &&
                     !(ber->ber_flags & LBER_FLAG_NO_FREE_BUFFER)) {
                         NSLBERI_FREE(ber->ber_buf);
                  }
                  NSLBERI_FREE( (char *) ber );
       }
}
unsigned long LDAP_CALL ber_get_next ( Sockbuf *  sb,
unsigned long len,
BerElement *  ber 
)

Definition at line 669 of file io.c.

{
       unsigned long tag, toread, newlen;
       long          rc;

#ifdef LDAP_DEBUG
       if ( lber_debug )
              ber_err_print( "ber_get_next\n" );
#endif

       /*
        * first time through - malloc the buffer, set up ptrs, and
        * read the tag and the length and as much of the rest as we can
        */

       if ( ber->ber_rwptr == NULL ) {
         /* read the tag */
         if ((tag = get_tag(sb, ber)) == LBER_DEFAULT ) {
           return( LBER_DEFAULT );
         }

         if((sb->sb_options & LBER_SOCKBUF_OPT_VALID_TAG) &&
            (tag != sb->sb_valid_tag)) {
           return( LBER_DEFAULT);
          }

         ber->ber_tag_contents[0] = (char)tag; /* we only handle 1 byte tags */
         
         /* read the length */
         if ((newlen = read_len_in_ber(sb, ber)) == LBER_DEFAULT ) {
           return( LBER_DEFAULT );
         }

         /*
          * Finally, malloc a buffer for the contents and read it in.
          * It's this buffer that's passed to all the other ber decoding
          * routines.
          */
         
#if defined( DOS ) && !( defined( _WIN32 ) || defined(XP_OS2) )
         if ( newlen > 65535 ) {   /* DOS can't allocate > 64K */
           return( LBER_DEFAULT );
         }
#endif /* DOS && !_WIN32 */
         
         if ( ( sb->sb_options & LBER_SOCKBUF_OPT_MAX_INCOMING_SIZE )
              && newlen > sb->sb_max_incoming ) {
           return( LBER_DEFAULT );
         }
         
         /* check to see if we already have enough memory allocated */
         if ( (ber->ber_end - ber->ber_buf) < (size_t)newlen) {         
           if ( (ber->ber_buf = (char *)NSLBERI_CALLOC( 1,(size_t)newlen ))
               == NULL ) {
             return( LBER_DEFAULT );
           }
           ber->ber_flags &= ~LBER_FLAG_NO_FREE_BUFFER;
         }
         

         ber->ber_len = newlen;
         ber->ber_ptr = ber->ber_buf;
         ber->ber_end = ber->ber_buf + newlen;
         ber->ber_rwptr = ber->ber_buf;
       }

       /* OK, we've malloc-ed the buffer; now read the rest of the expected length */
       toread = (unsigned long)ber->ber_end - (unsigned long)ber->ber_rwptr;
       do {
         if ( (rc = BerRead( sb, ber->ber_rwptr, (long)toread )) <= 0 ) {
           return( LBER_DEFAULT );
         }
         
         toread -= rc;
         ber->ber_rwptr += rc;
       } while ( toread > 0 );
       
#ifdef LDAP_DEBUG
       if ( lber_debug ) {
         char msg[80];
         sprintf( msg, "ber_get_next: tag 0x%lx len %ld contents:\n",
                 tag, ber->ber_len );
         ber_err_print( msg );
         if ( lber_debug > 1 )
           ber_dump( ber, 1 );
       }
#endif
              
       ber->ber_rwptr = NULL;
       *len = newlen;
       ber->ber_struct[BER_STRUCT_VAL].ldapiov_len = newlen;
       return(tag);
}

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned long LDAP_CALL ber_get_next_buffer ( void buffer,
size_t  buffer_size,
unsigned long len,
BerElement *  ber,
unsigned long Bytes_Scanned 
)

Definition at line 1163 of file io.c.

Here is the call graph for this function:

unsigned long LDAP_CALL ber_get_next_buffer_ext ( void buffer,
size_t  buffer_size,
unsigned long len,
BerElement *  ber,
unsigned long Bytes_Scanned,
Sockbuf *  sock 
)

Definition at line 1172 of file io.c.

{
       unsigned long tag = 0, netlen, toread;
       unsigned char lc;
       long          rc;
       int           noctets, diff;
       byte_buffer sb = {0};


       /*
        * 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
        */

       /*
        * first time through - malloc the buffer, set up ptrs, and
        * read the tag and the length and as much of the rest as we can
        */

       sb.p = buffer;
       sb.length = buffer_size;

       if ( ber->ber_rwptr == NULL ) {
              /*
               * First, we read the tag.
               */

                /* if we have been called before with a fragment not
               * containing a comlete length, we have no rwptr but
               * a tag already
                 */
              if ( ber->ber_tag == LBER_DEFAULT ) {
                     if ( (tag = get_buffer_tag( &sb )) == LBER_DEFAULT ) {
                            goto premature_exit;
                     }
                     ber->ber_tag = tag;
              }

              if((sock->sb_options & LBER_SOCKBUF_OPT_VALID_TAG) &&
                (tag != sock->sb_valid_tag)) {
                     *Bytes_Scanned=0;
                     return( LBER_DEFAULT);
              }

              /*
               * Next, read the length.  The first byte contains the length
               * of the length.  If bit 8 is set, the length is the long
               * form, otherwise it's the short form.  We don't allow a
               * length that's greater than what we can hold in an unsigned
               * long.
               */

                /* if the length is in long form and we don't get it in one
                 * fragment, we should handle this (TBD).
                 */

              *len = netlen = 0;
              if ( read_bytes( &sb, &lc, 1 ) != 1 ) {
                     goto premature_exit;
              }
              if ( lc & 0x80 ) {
                     noctets = (lc & 0x7f);
                     if ( noctets > sizeof(unsigned long) )
                            goto premature_exit;
                     diff = sizeof(unsigned long) - noctets;
                     if ( read_bytes( &sb, (unsigned char *)&netlen + diff,
                         noctets ) != noctets ) {
                            goto premature_exit;
                     }
                     *len = LBER_NTOHL( netlen );
              } else {
                     *len = lc;
              }
              ber->ber_len = *len;

              /*
               * Finally, malloc a buffer for the contents and read it in.
               * It's this buffer that's passed to all the other ber decoding
               * routines.
               */

#if defined( DOS ) && !defined( _WIN32 )
              if ( *len > 65535 ) {       /* DOS can't allocate > 64K */
                     goto premature_exit;
              }
#endif /* DOS && !_WIN32 */

                if ( (sock != NULL)  &&
                  ( sock->sb_options & LBER_SOCKBUF_OPT_MAX_INCOMING_SIZE )
                    && (*len > sock->sb_max_incoming) ) {
                        return( LBER_DEFAULT );
                }

              if ( ber->ber_buf + *len > ber->ber_end ) {
                     if ( nslberi_ber_realloc( ber, *len ) != 0 )
                            goto premature_exit;
              }
              ber->ber_ptr = ber->ber_buf;
              ber->ber_end = ber->ber_buf + *len;
              ber->ber_rwptr = ber->ber_buf;
       }

       toread = (unsigned long)ber->ber_end - (unsigned long)ber->ber_rwptr;
       do {
              if ( (rc = read_bytes( &sb, (unsigned char *)ber->ber_rwptr,
                  (long)toread )) <= 0 ) {
                     goto premature_exit;
              }

              toread -= rc;
              ber->ber_rwptr += rc;
       } while ( toread > 0 );

       *len = ber->ber_len;
       *Bytes_Scanned = sb.offset;
       return( ber->ber_tag );

premature_exit:
       /*
        * we're here because we hit the end of the buffer before seeing
        * all of the PDU
        */
       *Bytes_Scanned = sb.offset;
       return(LBER_DEFAULT);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int LDAP_CALL ber_get_option ( struct berelement ber,
int  option,
void value 
)

Definition at line 849 of file io.c.

{
       /*
        * memory callocation callbacks are global, so it is OK to pass
        * NULL for ber.  Handle this as a special case
        */
       if ( option == LBER_OPT_MEMALLOC_FN_PTRS ) {
              /* struct copy */
              *((struct lber_memalloc_fns *)value) = nslberi_memalloc_fns;
              return( 0 );
       }
       
       /*
        * lber_debug is global, so it is OK to pass
        * NULL for ber.  Handle this as a special case.
        */
       if ( option == LBER_OPT_DEBUG_LEVEL ) {
#ifdef LDAP_DEBUG
        *(int *)value =  lber_debug;
#endif
         return( 0 );
       }
       /*
        * all the rest require a non-NULL ber
        */
       if ( !NSLBERI_VALID_BERELEMENT_POINTER( ber )) {
              return( -1 );
       }

       switch ( option ) {
       case LBER_OPT_USE_DER:
       case LBER_OPT_TRANSLATE_STRINGS:
              *((int *) value) = (ber->ber_options & option);
              break;
       case LBER_OPT_REMAINING_BYTES:
              *((unsigned long *) value) = ber->ber_end - ber->ber_ptr;
              break;
       case LBER_OPT_TOTAL_BYTES:
              *((unsigned long *) value) = ber->ber_end - ber->ber_buf;
              break;
       case LBER_OPT_BYTES_TO_WRITE:
              *((unsigned long *) value) = ber->ber_ptr - ber->ber_buf;
              break;
       default:
              return( -1 );
       }

       return( 0 );
}

Here is the caller graph for this function:

BerElement* LDAP_CALL ber_init ( const struct berval bv)

Definition at line 1361 of file io.c.

{
       BerElement *ber;

       /* construct BerElement */
       if (( ber = ber_alloc_t( 0 )) != NULLBER ) {
              /* copy data from the bv argument into BerElement */
              /* XXXmcs: had to cast unsigned long bv_len to long */
              if ( (ber_write ( ber, bv->bv_val, bv->bv_len, 0 ))
                  != (long)bv->bv_len ) {
                     ber_free( ber, 1 );
                     return( NULL );
              }
       }
       
       /*
        * reset ber_ptr back to the beginning of buffer so that this new
        * and initialized ber element can be READ
        */
       ber_reset( ber, 1);

       /*
        * return a ptr to a new BerElement containing a copy of the data
        * in the bv argument or a null pointer on error
        */
       return( ber );
}

Here is the call graph for this function:

Here is the caller graph for this function:

void LDAP_CALL ber_init_w_nullchar ( BerElement *  ber,
int  options 
)

Definition at line 504 of file io.c.

{
       (void) memset( (char *)ber, '\0', sizeof(struct berelement) );
       ber->ber_tag = LBER_DEFAULT;

       /*
        * For compatibility with the C LDAP API standard, we recognize
        * LBER_USE_DER as LBER_OPT_USE_DER.  See lber.h for a bit more info.
        */
       if ( options & LBER_USE_DER ) {
              options &= ~LBER_USE_DER;
              options |= LBER_OPT_USE_DER;
       }

       ber->ber_options = options;
}

Here is the call graph for this function:

long LDAP_CALL ber_read ( BerElement *  ber,
char *  buf,
unsigned long  len 
)

Definition at line 208 of file io.c.

{
       unsigned long actuallen, nleft;

       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 );
}

Here is the caller graph for this function:

void LDAP_CALL ber_reset ( BerElement *  ber,
int  was_writing 
)

Definition at line 523 of file io.c.

{
       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;

       memset(ber->ber_struct, 0, BER_CONTENTS_STRUCT_SIZE);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int LDAP_CALL ber_set_option ( struct berelement ber,
int  option,
void value 
)

Definition at line 788 of file io.c.

{
  
  /*
   * memory allocation callbacks are global, so it is OK to pass
   * NULL for ber.  Handle this as a special case.
   */
  if ( option == LBER_OPT_MEMALLOC_FN_PTRS ) {
    /* struct copy */
    nslberi_memalloc_fns = *((struct lber_memalloc_fns *)value);
    return( 0 );
  }
  
  /*
   * lber_debug is global, so it is OK to pass
   * NULL for ber.  Handle this as a special case.
   */
  if ( option == LBER_OPT_DEBUG_LEVEL ) {
#ifdef LDAP_DEBUG
    lber_debug = *(int *)value;
#endif
    return( 0 );
  }
  
  /*
   * all the rest require a non-NULL ber
   */
  if ( !NSLBERI_VALID_BERELEMENT_POINTER( ber )) {
    return( -1 );
  }
  
  switch ( option ) {
       case LBER_OPT_USE_DER:
       case LBER_OPT_TRANSLATE_STRINGS:
              if ( value != NULL ) {
                     ber->ber_options |= option;
              } else {
                     ber->ber_options &= ~option;
              }
              break;
       case LBER_OPT_REMAINING_BYTES:
              ber->ber_end = ber->ber_ptr + *((unsigned long *)value);
              break;
       case LBER_OPT_TOTAL_BYTES:
              ber->ber_end = ber->ber_buf + *((unsigned long *)value);
              break;
       case LBER_OPT_BYTES_TO_WRITE:
              ber->ber_ptr = ber->ber_buf + *((unsigned long *)value);
              break;
       default:
              return( -1 );
  }
  
  return( 0 );
}

Here is the caller graph for this function:

Definition at line 765 of file io.c.

{
       return( (Sockbuf *)NSLBERI_CALLOC( 1, sizeof(struct sockbuf) ) );
}

Here is the caller graph for this function:

void LDAP_CALL ber_sockbuf_free ( Sockbuf *  p)

Definition at line 772 of file io.c.

{
       if ( p != NULL ) {
              if ( p->sb_ber.ber_buf != NULL &&
                  !(p->sb_ber.ber_flags & LBER_FLAG_NO_FREE_BUFFER) ) {
                     NSLBERI_FREE( p->sb_ber.ber_buf );
              }
              NSLBERI_FREE(p);
       }
}

Here is the caller graph for this function:

int LDAP_CALL ber_sockbuf_get_option ( Sockbuf *  sb,
int  option,
void value 
)

Definition at line 984 of file io.c.

{
       struct lber_x_ext_io_fns    *extiofns;

       if ( !NSLBERI_VALID_SOCKBUF_POINTER( sb )) {
              return( -1 );
       }

       switch ( option ) {
       case LBER_SOCKBUF_OPT_VALID_TAG:
              *((unsigned long *) value) = sb->sb_valid_tag;
              break;
       case LBER_SOCKBUF_OPT_MAX_INCOMING_SIZE:
              *((unsigned long *) value) = sb->sb_max_incoming;
              break;
       case LBER_SOCKBUF_OPT_TO_FILE:
       case LBER_SOCKBUF_OPT_TO_FILE_ONLY:
       case LBER_SOCKBUF_OPT_NO_READ_AHEAD:
              *((int *) value) = (sb->sb_options & option);
              break;
       case LBER_SOCKBUF_OPT_DESC:
              *((LBER_SOCKET *) value) = sb->sb_sd;
              break;
       case LBER_SOCKBUF_OPT_COPYDESC:
              *((LBER_SOCKET *) value) = sb->sb_copyfd;
              break;
       case LBER_SOCKBUF_OPT_READ_FN:
              *((LDAP_IOF_READ_CALLBACK **) value)
                  = sb->sb_io_fns.lbiof_read;
              break;
       case LBER_SOCKBUF_OPT_WRITE_FN:
              *((LDAP_IOF_WRITE_CALLBACK **) value)
                  = sb->sb_io_fns.lbiof_write;
              break;
       case LBER_SOCKBUF_OPT_EXT_IO_FNS:
              extiofns = (struct lber_x_ext_io_fns *) value;
              if ( extiofns == NULL ) {
                     return( -1 );
              } else if ( extiofns->lbextiofn_size
                         == LBER_X_EXTIO_FNS_SIZE ) {
                     /* struct copy */
                     *extiofns = sb->sb_ext_io_fns;
              } else if ( extiofns->lbextiofn_size
                         == LBER_X_EXTIO_FNS_SIZE_REV0 ) {
                     /* backwards compatiblity for older struct */
                     extiofns->lbextiofn_read = sb->sb_ext_io_fns.lbextiofn_read;
                     extiofns->lbextiofn_write = sb->sb_ext_io_fns.lbextiofn_write;
                     extiofns->lbextiofn_socket_arg = sb->sb_ext_io_fns.lbextiofn_socket_arg;
              } else {
                     return( -1 );
              }
              break;
       default:
              return( -1 );
       }

       return( 0 );
}

Here is the caller graph for this function:

int LDAP_CALL ber_sockbuf_set_option ( Sockbuf *  sb,
int  option,
void value 
)

Definition at line 904 of file io.c.

{
       struct lber_x_ext_io_fns    *extiofns;

       if ( !NSLBERI_VALID_SOCKBUF_POINTER( sb )) {
              return( -1 );
       }

       switch ( option ) {
       case LBER_SOCKBUF_OPT_VALID_TAG:
              sb->sb_valid_tag= *((unsigned long *) value);
              if ( value != NULL ) {
                     sb->sb_options |= option;
              } else {
                     sb->sb_options &= ~option;
              }
              break;
       case LBER_SOCKBUF_OPT_MAX_INCOMING_SIZE:
              sb->sb_max_incoming = *((unsigned long *) value);
              /* FALL */
       case LBER_SOCKBUF_OPT_TO_FILE:
       case LBER_SOCKBUF_OPT_TO_FILE_ONLY:
       case LBER_SOCKBUF_OPT_NO_READ_AHEAD:
              if ( value != NULL ) {
                     sb->sb_options |= option;
              } else {
                     sb->sb_options &= ~option;
              }
              break;
       case LBER_SOCKBUF_OPT_DESC:
              sb->sb_sd = *((LBER_SOCKET *) value);
              break;
       case LBER_SOCKBUF_OPT_COPYDESC:
              sb->sb_copyfd = *((LBER_SOCKET *) value);
              break;
       case LBER_SOCKBUF_OPT_READ_FN:
              sb->sb_io_fns.lbiof_read = (LDAP_IOF_READ_CALLBACK *) value;
              nslberi_install_compat_io_fns( sb );
              break;
       case LBER_SOCKBUF_OPT_WRITE_FN:
              sb->sb_io_fns.lbiof_write = (LDAP_IOF_WRITE_CALLBACK *) value;
              nslberi_install_compat_io_fns( sb );
              break;
       case LBER_SOCKBUF_OPT_EXT_IO_FNS:
              extiofns = (struct lber_x_ext_io_fns *) value;
              if ( extiofns == NULL ) {   /* remove */
                     (void)memset( (char *)&sb->sb_ext_io_fns, '\0',
                            sizeof(sb->sb_ext_io_fns ));
              } else if ( extiofns->lbextiofn_size
                  == LBER_X_EXTIO_FNS_SIZE ) {
                     /* struct copy */
                     sb->sb_ext_io_fns = *extiofns;
              } else if ( extiofns->lbextiofn_size
                         == LBER_X_EXTIO_FNS_SIZE_REV0 ) {
                     /* backwards compatiblity for older struct */
                     sb->sb_ext_io_fns.lbextiofn_size =
                            LBER_X_EXTIO_FNS_SIZE;
                     sb->sb_ext_io_fns.lbextiofn_read =
                            extiofns->lbextiofn_read;
                     sb->sb_ext_io_fns.lbextiofn_write =
                                extiofns->lbextiofn_write;
                     sb->sb_ext_io_fns.lbextiofn_writev = NULL;
                     sb->sb_ext_io_fns.lbextiofn_socket_arg =
                                extiofns->lbextiofn_socket_arg;
              } else {
                     return( -1 );
              }
              break;
       default:
              return( -1 );
       }

       return( 0 );
}

Here is the call graph for this function:

Here is the caller graph for this function:

void* LDAP_CALL ber_special_alloc ( size_t  size,
BerElement **  ppBer 
)

Definition at line 1061 of file io.c.

{
       char *mem = NULL;

       /* Make sure mem size requested is aligned */
       if (0 != ( size & 0x03 )) {
              size += (sizeof(long) - (size & 0x03));
       }

       mem = NSLBERI_MALLOC(sizeof(struct berelement) + EXBUFSIZ + size );
       if (NULL == mem) {
              return NULL;
       } 
       *ppBer = (BerElement*) (mem + size);
       memset(*ppBer,0,sizeof(struct berelement));
       (*ppBer)->ber_tag = LBER_DEFAULT;
       (*ppBer)->ber_buf = mem + size + sizeof(struct berelement);
       (*ppBer)->ber_ptr = (*ppBer)->ber_buf;
       (*ppBer)->ber_end = (*ppBer)->ber_buf + EXBUFSIZ;
       (*ppBer)->ber_flags = LBER_FLAG_NO_FREE_BUFFER;
       return (void*)mem;
}

Here is the call graph for this function:

void LDAP_CALL ber_special_free ( void buf,
BerElement *  ber 
)

Definition at line 1086 of file io.c.

{
       if (!(ber->ber_flags & LBER_FLAG_NO_FREE_BUFFER)) {
              NSLBERI_FREE(ber->ber_buf);
       }
       NSLBERI_FREE( buf );
}
long LDAP_CALL ber_write ( BerElement *  ber,
char *  buf,
unsigned long  len,
int  nosos 
)

Definition at line 294 of file io.c.

{
       if ( nosos || ber->ber_sos == NULL ) {
              if ( ber->ber_ptr + len > ber->ber_end ) {
                     if ( nslberi_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 ( nslberi_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 );
       }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static long BerRead ( Sockbuf *  sb,
char *  buf,
long  len 
) [static]

Definition at line 180 of file io.c.

{
       int    c;
       long   nread = 0;

       while ( len > 0 ) {
              if ( (c = bergetc( sb, len )) < 0 ) {
                     if ( nread > 0 )
                            break;
                     return( c );
              }
              *buf++ = c;
              nread++;
              len--;
       }

       return( nread );
}

Here is the caller graph for this function:

BerElement* LDAP_CALL der_alloc ( )

Definition at line 482 of file io.c.

{
       return( ber_alloc_t( LBER_OPT_USE_DER ) );
}

Here is the call graph for this function:

Here is the caller graph for this function:

static unsigned long get_ber_len ( BerElement *  ber) [static]

Definition at line 605 of file io.c.

{
  int noctets;
  unsigned long len = 0;
  char xbyte;

  xbyte = ber->ber_len_contents[0];
  
  /* long form */
  if (xbyte & 0x80) {
    noctets = (int) (xbyte & 0x7f);
    if (noctets >= MAX_LEN_SIZE) {
      return(LBER_DEFAULT);
    }
    memcpy((char*) &len + sizeof(unsigned long) - noctets, &ber->ber_len_contents[1], noctets);
    len = LBER_NTOHL(len);
    return(len);
  } else {
    return((unsigned long)(xbyte));
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static unsigned long get_buffer_tag ( byte_buffer sb) [static]

Definition at line 1120 of file io.c.

{
       unsigned char xbyte;
       unsigned long tag;
       char          *tagp;
       int           i;

       if ( (i = read_bytes( sb, &xbyte, 1 )) != 1 ) {
              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++ ) {
              if ( read_bytes( sb, &xbyte, 1 ) != 1 )
                     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) );
}

Here is the call graph for this function:

Here is the caller graph for this function:

static unsigned long get_tag ( Sockbuf *  sb,
BerElement *  ber 
) [static]

Definition at line 584 of file io.c.

{
        unsigned char   xbyte;

        if ( (BerRead( sb, (char *) &xbyte, 1 )) != 1 ) {
                return( LBER_DEFAULT );
        }

        if ( (xbyte & LBER_BIG_TAG_MASK) == LBER_BIG_TAG_MASK ) {
                return( LBER_DEFAULT );
        }

       ber->ber_tag_contents[0] = xbyte;
       ber->ber_struct[BER_STRUCT_TAG].ldapiov_len = 1;
       return((unsigned long)xbyte);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int nslberi_ber_realloc ( BerElement *  ber,
unsigned long  len 
)

Definition at line 227 of file io.c.

{
       unsigned long need, have, total;
       size_t        have_bytes;
       Seqorset      *s;
       long          off;
       char          *oldbuf;

       have_bytes = ber->ber_end - ber->ber_buf;
       have = have_bytes / EXBUFSIZ;
       need = (len < EXBUFSIZ ? 1 : (len + (EXBUFSIZ - 1)) / EXBUFSIZ);
       total = have * EXBUFSIZ + need * EXBUFSIZ;

       oldbuf = ber->ber_buf;

       if (ber->ber_buf == NULL) {
              if ( (ber->ber_buf = (char *)NSLBERI_MALLOC( (size_t)total ))
                  == NULL ) {
                     return( -1 );
              }
              ber->ber_flags &= ~LBER_FLAG_NO_FREE_BUFFER;
       } else {
              if ( ber->ber_flags & LBER_FLAG_NO_FREE_BUFFER ) {
                     /* transition to malloc'd buffer */
                     if ( (ber->ber_buf = (char *)NSLBERI_MALLOC(
                         (size_t)total )) == NULL ) {
                            return( -1 );
                     }
                     ber->ber_flags &= ~LBER_FLAG_NO_FREE_BUFFER;
                     /* copy existing data into new malloc'd buffer */
                     SAFEMEMCPY( ber->ber_buf, oldbuf, have_bytes );
              } else {
                     if ( (ber->ber_buf = (char *)NSLBERI_REALLOC(
                         ber->ber_buf,(size_t)total )) == NULL ) {
                            return( -1 );
                     }
              }
       }

       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 );
}

Here is the caller graph for this function:

void* nslberi_calloc ( size_t  nelem,
size_t  elsize 
)

Definition at line 1403 of file io.c.

{
       return( nslberi_memalloc_fns.lbermem_calloc == NULL ?
           calloc(  nelem, elsize ) :
           nslberi_memalloc_fns.lbermem_calloc( nelem, elsize ));
}

Here is the call graph for this function:

static int nslberi_extread_compat ( int  s,
void buf,
int  len,
struct lextiof_socket_private arg 
) [static]

Definition at line 1443 of file io.c.

{
       Sockbuf       *sb = (Sockbuf *)arg;

       return( sb->sb_io_fns.lbiof_read( sb->sb_sd, buf, len ));
}

Here is the caller graph for this function:

static int nslberi_extwrite_compat ( int  s,
const void buf,
int  len,
struct lextiof_socket_private arg 
) [static]

Definition at line 1453 of file io.c.

{
       Sockbuf       *sb = (Sockbuf *)arg;

       return( sb->sb_io_fns.lbiof_write( sb->sb_sd, buf, len ));
}

Here is the caller graph for this function:

void nslberi_free ( void ptr)

Definition at line 1421 of file io.c.

{
       if ( nslberi_memalloc_fns.lbermem_free == NULL ) {
              free( ptr );
       } else {
              nslberi_memalloc_fns.lbermem_free( ptr );
       }
}
static void nslberi_install_compat_io_fns ( Sockbuf *  sb) [static]

Definition at line 1466 of file io.c.

{
       sb->sb_ext_io_fns.lbextiofn_size = LBER_X_EXTIO_FNS_SIZE;
       sb->sb_ext_io_fns.lbextiofn_read = nslberi_extread_compat;
       sb->sb_ext_io_fns.lbextiofn_write = nslberi_extwrite_compat;
       sb->sb_ext_io_fns.lbextiofn_writev = NULL;
       sb->sb_ext_io_fns.lbextiofn_socket_arg = (void *)sb;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void* nslberi_malloc ( size_t  size)

Definition at line 1394 of file io.c.

{
       return( nslberi_memalloc_fns.lbermem_malloc == NULL ?
           malloc( size ) :
           nslberi_memalloc_fns.lbermem_malloc( size ));
}
void* nslberi_realloc ( void ptr,
size_t  size 
)

Definition at line 1412 of file io.c.

{
       return( nslberi_memalloc_fns.lbermem_realloc == NULL ?
           realloc( ptr, size ) :
           nslberi_memalloc_fns.lbermem_realloc( ptr, size ));
}

Here is the call graph for this function:

static int read_bytes ( byte_buffer b,
unsigned char *  return_buffer,
int  bytes_to_read 
) [static]

Definition at line 1095 of file io.c.

{
       /* copy up to bytes_to_read bytes into the caller's buffer, return the number of bytes copied */
       int bytes_to_copy = 0;

       if (bytes_to_read <= (b->length - b->offset) ) {
              bytes_to_copy = bytes_to_read;
       } else {
              bytes_to_copy = (b->length - b->offset);
       }
       if (1 == bytes_to_copy) {
              *return_buffer = *(b->p+b->offset++);
       } else 
       if (0 == bytes_to_copy) {
              ;
       } else
       {
              memcpy(return_buffer,b->p+b->offset,bytes_to_copy);
              b->offset += bytes_to_copy;
       }
       return bytes_to_copy;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static unsigned long read_len_in_ber ( Sockbuf *  sb,
BerElement *  ber 
) [static]

Definition at line 630 of file io.c.

{
  unsigned char xbyte;
  int           noctets;
  int           rc = 0;

  /*
   * Next, read the length.  The first byte contains the length
   * of the length.  If bit 8 is set, the length is the long
   * form, otherwise it's the short form.  We don't allow a
   * length that's greater than what we can hold in a long (2GB)
   */
 
  if ( BerRead( sb, (char *) &xbyte, 1 ) != 1 ) {
    return( LBER_DEFAULT );
  }

  ber->ber_len_contents[0] = xbyte;

  /* long form of the length value */
  if ( xbyte & 0x80 ) {
    noctets = (xbyte & 0x7f);
    if ( noctets >= MAX_LEN_SIZE )
      return( LBER_DEFAULT );
    while (rc < noctets) {
      if ( (rc += BerRead( sb, &(ber->ber_len_contents[1]) + rc, noctets - rc )) <= 0) {
       return( LBER_DEFAULT );
      }
    }
    ber->ber_struct[BER_STRUCT_LEN].ldapiov_len = 1 + noctets;
  } else { /* short form of the length value */ 
    ber->ber_struct[BER_STRUCT_LEN].ldapiov_len = 1;
  }
  return(get_ber_len(ber));
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

struct lber_memalloc_fns [static]

Definition at line 110 of file io.c.