Back to index

lightning-sunbird  0.9+nobinonly
lber-int.h
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 /*
00039  * Copyright (c) 1990 Regents of the University of Michigan.
00040  * All rights reserved.
00041  *
00042  * Redistribution and use in source and binary forms are permitted
00043  * provided that this notice is preserved and that due credit is given
00044  * to the University of Michigan at Ann Arbor. The name of the University
00045  * may not be used to endorse or promote products derived from this
00046  * software without specific prior written permission. This software
00047  * is provided ``as is'' without express or implied warranty.
00048  */
00049 /* lbet-int.h - internal header file for liblber */
00050 
00051 #ifndef _LBERINT_H
00052 #define _LBERINT_H
00053 
00054 #ifdef __cplusplus
00055 extern "C" {
00056 #endif
00057 
00058 #include <stdio.h>
00059 #include <ctype.h>
00060 #include <stdarg.h>
00061 #include <stdlib.h>
00062 
00063 #ifdef macintosh
00064 # include "ldap-macos.h"
00065 #else /* macintosh */
00066 #if !defined(BSDI) && !defined(DARWIN) && !defined(FREEBSD)
00067 # include <malloc.h>
00068 #endif
00069 # include <errno.h>
00070 # include <sys/types.h>
00071 #if defined(SUNOS4) || defined(SCOOS)
00072 # include <sys/time.h>
00073 #endif
00074 #if defined( _WINDOWS )
00075 #  define WIN32_LEAN_AND_MEAN
00076 #  include <windows.h>
00077 #  include <time.h>
00078 /* No stderr in a 16-bit Windows DLL */
00079 #  if defined(_WINDLL) && !defined(_WIN32)
00080 #    define USE_DBG_WIN
00081 #  endif
00082 # else
00083 /* #  include <sys/varargs.h> */
00084 #  include <sys/socket.h>
00085 #  include <netinet/in.h>
00086 #if !defined(XP_OS2) && !defined(DARWIN)
00087 #  include <unistd.h>
00088 #endif
00089 # endif /* defined( _WINDOWS ) */
00090 #endif /* macintosh */
00091 
00092 #include <memory.h>
00093 #include <string.h>
00094 #include "portable.h"
00095 
00096 #ifdef _WINDOWS
00097 #include <winsock.h>
00098 #include <io.h>
00099 #endif /* _WINDOWS */
00100 
00101 #ifdef XP_OS2
00102 #include <io.h>
00103 #endif /* XP_OS2 */
00104 
00105 /* No stderr in a 16-bit Windows DLL */
00106 #if defined(_WINDLL) && !defined(_WIN32)
00107 #define stderr NULL
00108 #endif
00109 
00110 #include "lber.h"
00111 
00112 #ifdef macintosh
00113 #define NSLDAPI_LBER_SOCKET_IS_PTR
00114 #endif
00115 
00116 #define OLD_LBER_SEQUENCE   0x10L  /* w/o constructed bit - broken */
00117 #define OLD_LBER_SET        0x11L  /* w/o constructed bit - broken */
00118 
00119 #ifndef _IFP
00120 #define _IFP
00121 typedef int (LDAP_C LDAP_CALLBACK *IFP)();
00122 #endif
00123 
00124 typedef struct seqorset {
00125   unsigned long      sos_clen;
00126   unsigned long      sos_tag;
00127   char        *sos_first;
00128   char        *sos_ptr;
00129   struct seqorset    *sos_next;
00130 } Seqorset;
00131 #define NULLSEQORSET ((Seqorset *) 0)
00132 
00133 #define SOS_STACK_SIZE 8 /* depth of the pre-allocated sos structure stack */
00134 
00135 
00136 #define MAX_TAG_SIZE (1 + sizeof(long)) /* One byte for the length of the tag */
00137 #define MAX_LEN_SIZE (1 + sizeof(long)) /* One byte for the length of the length */
00138 #define MAX_VALUE_PREFIX_SIZE (2 + sizeof(long)) /* 1 byte for the tag and 1 for the len (msgid) */
00139 #define BER_ARRAY_QUANTITY 7 /* 0:Tag   1:Length   2:Value-prefix   3:Value   4:Value-suffix  */
00140 #define BER_STRUCT_TAG 0     /* 5:ControlA   6:ControlB */
00141 #define BER_STRUCT_LEN 1
00142 #define BER_STRUCT_PRE 2
00143 #define BER_STRUCT_VAL 3
00144 #define BER_STRUCT_SUF 4
00145 #define BER_STRUCT_CO1 5
00146 #define BER_STRUCT_CO2 6
00147 
00148 struct berelement {
00149   ldap_x_iovec  ber_struct[BER_ARRAY_QUANTITY];   /* See above */
00150 
00151   char          ber_tag_contents[MAX_TAG_SIZE];
00152   char          ber_len_contents[MAX_LEN_SIZE];
00153   char          ber_pre_contents[MAX_VALUE_PREFIX_SIZE];
00154   char          ber_suf_contents[MAX_LEN_SIZE+1];
00155 
00156   char          *ber_buf; /* update the value value when writing in case realloc is called */
00157   char        *ber_ptr;
00158   char        *ber_end;
00159   struct seqorset    *ber_sos;
00160   unsigned long      ber_tag; /* Remove me someday */
00161   unsigned long      ber_len; /* Remove me someday */
00162   int         ber_usertag;
00163   char        ber_options;
00164   char        *ber_rwptr;
00165   BERTranslateProc ber_encode_translate_proc;
00166   BERTranslateProc ber_decode_translate_proc;
00167   int         ber_flags;
00168 #define LBER_FLAG_NO_FREE_BUFFER   1      /* don't free ber_buf */
00169   int         ber_sos_stack_posn;
00170   Seqorset    ber_sos_stack[SOS_STACK_SIZE];
00171 };
00172 
00173 #define BER_CONTENTS_STRUCT_SIZE (sizeof(ldap_x_iovec) * BER_ARRAY_QUANTITY)
00174 
00175 #define NULLBER      ((BerElement *)NULL)
00176 
00177 #ifdef LDAP_DEBUG
00178 void ber_dump( BerElement *ber, int inout );
00179 #endif
00180 
00181 
00182 
00183 /*
00184  * structure for read/write I/O callback functions.
00185  */
00186 struct nslberi_io_fns {
00187     LDAP_IOF_READ_CALLBACK  *lbiof_read;
00188     LDAP_IOF_WRITE_CALLBACK *lbiof_write;
00189 };
00190 
00191 
00192 /*
00193  * Old  structure for use with LBER_SOCKBUF_OPT_EXT_IO_FNS:
00194  */
00195 struct lber_x_ext_io_fns_rev0 {
00196            /* lbextiofn_size should always be set to LBER_X_EXTIO_FNS_SIZE */
00197        int                         lbextiofn_size;
00198        LDAP_X_EXTIOF_READ_CALLBACK *lbextiofn_read;
00199        LDAP_X_EXTIOF_WRITE_CALLBACK       *lbextiofn_write;
00200        struct lextiof_socket_private      *lbextiofn_socket_arg;
00201 };
00202 #define LBER_X_EXTIO_FNS_SIZE_REV0    sizeof(struct lber_x_ext_io_fns_rev0)
00203 
00204 
00205 
00206 struct sockbuf {
00207        LBER_SOCKET   sb_sd;
00208        BerElement    sb_ber;
00209        int           sb_naddr;     /* > 0 implies using CLDAP (UDP) */
00210        void          *sb_useaddr;  /* pointer to sockaddr to use next */
00211        void          *sb_fromaddr; /* pointer to message source sockaddr */
00212        void          **sb_addrs;   /* actually an array of pointers to
00213                                       sockaddrs */
00214 
00215        int           sb_options;   /* to support copying ber elements */
00216        LBER_SOCKET   sb_copyfd;    /* for LBER_SOCKBUF_OPT_TO_FILE* opts */
00217        unsigned long sb_max_incoming;
00218        unsigned long   sb_valid_tag;      /* valid tag to accept */
00219        struct nslberi_io_fns
00220                      sb_io_fns;    /* classic I/O callback functions */
00221 
00222        struct lber_x_ext_io_fns
00223                      sb_ext_io_fns;       /* extended I/O callback functions */
00224 };
00225 #define NULLSOCKBUF  ((Sockbuf *)NULL)
00226 
00227 /* needed by libldap, even in non-DEBUG builds */
00228 void ber_err_print( char *data );
00229 
00230 #ifndef NSLBERI_LBER_INT_FRIEND
00231 /*
00232  * Everything from this point on is excluded if NSLBERI_LBER_INT_FRIEND is
00233  * defined.  The code under ../libraries/libldap defines this.
00234  */
00235 
00236 #define READBUFSIZ   8192
00237 
00238 /*
00239  * macros used to check validity of data structures and parameters
00240  */
00241 #define NSLBERI_VALID_BERELEMENT_POINTER( ber ) \
00242        ( (ber) != NULLBER )
00243 
00244 #define NSLBERI_VALID_SOCKBUF_POINTER( sb ) \
00245        ( (sb) != NULLSOCKBUF )
00246 
00247 
00248 #if defined(_WIN32) && defined(_ALPHA)
00249 #define LBER_HTONL( _l ) \
00250            ((((_l)&0xff)<<24) + (((_l)&0xff00)<<8) + \
00251             (((_l)&0xff0000)>>8) + (((_l)&0xff000000)>>24))
00252 #define LBER_NTOHL(_l) LBER_HTONL(_l)
00253 
00254 #elif (!defined(__alpha) || defined(VMS)) && !defined(__amd64__) && !defined(__x86_64__)
00255 
00256 #define LBER_HTONL( l )     htonl( l )
00257 #define LBER_NTOHL( l )     ntohl( l )
00258 
00259 #else /* __alpha || __amd64__ */
00260 /*
00261  * htonl and ntohl on the 64 bit UNIX platforms only swap the lower-order
00262  * 32-bits of a (64-bit) long, so we define correct versions
00263  * here.
00264  */
00265 #define LBER_HTONL( l )     (((long)htonl( (l) & 0x00000000FFFFFFFF )) << 32 \
00266                      | htonl( ( (l) & 0xFFFFFFFF00000000 ) >> 32 ))
00267 
00268 #define LBER_NTOHL( l )     (((long)ntohl( (l) & 0x00000000FFFFFFFF )) << 32 \
00269                      | ntohl( ( (l) & 0xFFFFFFFF00000000 ) >> 32 ))
00270 #endif /* __alpha || __amd64__ */
00271 
00272 
00273 /* function prototypes */
00274 #ifdef LDAP_DEBUG
00275 void lber_bprint( char *data, int len );
00276 #endif
00277 void *nslberi_malloc( size_t size );
00278 void *nslberi_calloc( size_t nelem, size_t elsize );
00279 void *nslberi_realloc( void *ptr, size_t size );
00280 void nslberi_free( void *ptr );
00281 int nslberi_ber_realloc( BerElement *ber, unsigned long len );
00282 
00283 
00284 
00285 /* blame: dboreham 
00286  * slapd spends much of its time doing memcpy's for the ber code.
00287  * Most of these are single-byte, so we special-case those and speed
00288  * things up considerably. 
00289  */
00290 
00291 #ifdef sunos4
00292 #define THEMEMCPY( d, s, n )       bcopy( s, d, n )
00293 #else /* sunos4 */
00294 #define THEMEMCPY( d, s, n )       memmove( d, s, n )
00295 #endif /* sunos4 */
00296 
00297 #ifdef SAFEMEMCPY
00298 #undef SAFEMEMCPY
00299 #define SAFEMEMCPY(d,s,n) if (1 == n) *((char*)d) = *((char*)s); else THEMEMCPY(d,s,n);
00300 #endif
00301 
00302 /*
00303  * Memory allocation done in liblber should all go through one of the
00304  * following macros. This is so we can plug-in alternative memory
00305  * allocators, etc. as the need arises.
00306  */
00307 #define NSLBERI_MALLOC( size )            nslberi_malloc( size )
00308 #define NSLBERI_CALLOC( nelem, elsize )   nslberi_calloc( nelem, elsize )
00309 #define NSLBERI_REALLOC( ptr, size )      nslberi_realloc( ptr, size )
00310 #define NSLBERI_FREE( ptr )        nslberi_free( ptr )
00311 
00312 /* allow the library to access the debug variable */
00313 
00314 extern int lber_debug;
00315 
00316 #endif /* !NSLBERI_LBER_INT_FRIEND */
00317 
00318 
00319 #ifdef __cplusplus
00320 }
00321 #endif
00322 #endif /* _LBERINT_H */