Back to index

lightning-sunbird  0.9+nobinonly
lber.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 /* lber.h - header file for ber_* functions */
00038 #ifndef _LBER_H
00039 #define _LBER_H
00040 
00041 #ifdef __cplusplus
00042 extern "C" {
00043 #endif
00044 
00045 #include <stdlib.h>  /* to pick up size_t typedef */
00046 
00047 /*
00048  * Note that LBER_ERROR and LBER_DEFAULT are values that can never appear
00049  * as valid BER tags, and so it is safe to use them to report errors.  In
00050  * fact, any tag for which the following is true is invalid:
00051  *     (( tag & 0x00000080 ) != 0 ) && (( tag & 0xFFFFFF00 ) != 0 )
00052  */
00053 #define LBER_ERROR              0xffffffffUL
00054 #define LBER_DEFAULT            0xffffffffUL     
00055 #define LBER_END_OF_SEQORSET       0xfffffffeUL
00056 
00057 /* BER classes and mask */
00058 #define LBER_CLASS_UNIVERSAL    0x00
00059 #define LBER_CLASS_APPLICATION  0x40
00060 #define LBER_CLASS_CONTEXT      0x80
00061 #define LBER_CLASS_PRIVATE      0xc0
00062 #define LBER_CLASS_MASK         0xc0
00063 
00064 /* BER encoding type and mask */
00065 #define LBER_PRIMITIVE          0x00
00066 #define LBER_CONSTRUCTED        0x20
00067 #define LBER_ENCODING_MASK      0x20
00068 
00069 #define LBER_BIG_TAG_MASK       0x1f
00070 #define LBER_MORE_TAG_MASK      0x80
00071 
00072 /* general BER types we know about */
00073 #define LBER_BOOLEAN        0x01L
00074 #define LBER_INTEGER        0x02L
00075 #define LBER_BITSTRING             0x03L
00076 #define LBER_OCTETSTRING    0x04L
00077 #define LBER_NULL           0x05L
00078 #define LBER_ENUMERATED            0x0aL
00079 #define LBER_SEQUENCE              0x30L
00080 #define LBER_SET            0x31L
00081 
00082 /* BerElement set/get options */
00083 #define LBER_OPT_REMAINING_BYTES   0x01
00084 #define LBER_OPT_TOTAL_BYTES              0x02
00085 #define LBER_OPT_USE_DER           0x04
00086 #define LBER_OPT_TRANSLATE_STRINGS 0x08
00087 #define LBER_OPT_BYTES_TO_WRITE           0x10
00088 #define LBER_OPT_MEMALLOC_FN_PTRS  0x20
00089 #define LBER_OPT_DEBUG_LEVEL              0x40
00090 /*
00091  * LBER_USE_DER is defined for compatibility with the C LDAP API RFC.
00092  * In our implementation, we recognize it (instead of the numerically
00093  * identical LBER_OPT_REMAINING_BYTES) in calls to ber_alloc_t() and 
00094  * ber_init_w_nullchar() only.  Callers of ber_set_option() or
00095  * ber_get_option() must use LBER_OPT_USE_DER instead.  Sorry!
00096  */
00097 #define LBER_USE_DER               0x01
00098 
00099 
00100 /* Sockbuf set/get options */
00101 #define LBER_SOCKBUF_OPT_TO_FILE          0x001
00102 #define LBER_SOCKBUF_OPT_TO_FILE_ONLY            0x002
00103 #define LBER_SOCKBUF_OPT_MAX_INCOMING_SIZE       0x004
00104 #define LBER_SOCKBUF_OPT_NO_READ_AHEAD           0x008
00105 #define LBER_SOCKBUF_OPT_DESC                    0x010
00106 #define LBER_SOCKBUF_OPT_COPYDESC         0x020
00107 #define LBER_SOCKBUF_OPT_READ_FN          0x040
00108 #define LBER_SOCKBUF_OPT_WRITE_FN         0x080
00109 #define LBER_SOCKBUF_OPT_EXT_IO_FNS              0x100
00110 #define       LBER_SOCKBUF_OPT_VALID_TAG         0x200
00111 
00112 #define LBER_OPT_ON  ((void *) 1)
00113 #define LBER_OPT_OFF ((void *) 0)
00114 
00115 
00116 typedef struct berval {
00117        unsigned long bv_len;
00118        char          *bv_val;
00119 } BerValue;
00120 
00121 typedef struct berelement BerElement;
00122 typedef struct sockbuf Sockbuf;
00123 typedef int (*BERTranslateProc)( char **bufp, unsigned long *buflenp,
00124        int free_input );
00125 #if defined( _WINDOWS ) || defined( _WIN32) || defined( _CONSOLE )
00126 #include <winsock.h> /* for SOCKET */
00127 typedef SOCKET LBER_SOCKET;
00128 #else
00129 typedef int LBER_SOCKET;
00130 #endif /* _WINDOWS */
00131 
00132 /* calling conventions used by library */
00133 #ifndef LDAP_CALL
00134 #if defined( _WINDOWS ) || defined( _WIN32 )
00135 #define LDAP_C __cdecl
00136 #ifndef _WIN32 
00137 #define __stdcall _far _pascal
00138 #define LDAP_CALLBACK _loadds
00139 #else
00140 #define LDAP_CALLBACK
00141 #endif /* _WIN32 */
00142 #define LDAP_PASCAL __stdcall
00143 #define LDAP_CALL LDAP_PASCAL
00144 #else /* _WINDOWS */
00145 #define LDAP_C
00146 #define LDAP_CALLBACK
00147 #define LDAP_PASCAL
00148 #define LDAP_CALL
00149 #endif /* _WINDOWS */
00150 #endif /* LDAP_CALL */
00151 
00152 /*
00153  * function prototypes for lber library
00154  */
00155 
00156 #ifndef LDAP_API
00157 #if defined( _WINDOWS ) || defined( _WIN32 )
00158 #define LDAP_API(rt) rt
00159 #else /* _WINDOWS */
00160 #define LDAP_API(rt) rt
00161 #endif /* _WINDOWS */
00162 #endif /* LDAP_API */
00163 
00164 struct lextiof_socket_private;          /* Defined by the extended I/O */
00165                                         /* callback functions */
00166 struct lextiof_session_private;         /* Defined by the extended I/O */
00167                                         /* callback functions */
00168 
00169 /* This is modeled after the PRIOVec that is passed to the NSPR
00170    writev function! The void* is a char* in that struct */
00171 typedef struct ldap_x_iovec {
00172         char    *ldapiov_base;
00173         int     ldapiov_len;
00174 } ldap_x_iovec;
00175 
00176 /*
00177  * libldap read and write I/O function callbacks.  The rest of the I/O callback
00178  * types are defined in ldap.h
00179  */
00180 typedef int (LDAP_C LDAP_CALLBACK LDAP_IOF_READ_CALLBACK)( LBER_SOCKET s,
00181        void *buf, int bufsize );
00182 typedef int (LDAP_C LDAP_CALLBACK LDAP_IOF_WRITE_CALLBACK)( LBER_SOCKET s,
00183        const void *buf, int len );
00184 typedef int (LDAP_C LDAP_CALLBACK LDAP_X_EXTIOF_READ_CALLBACK)( int s,
00185        void *buf, int bufsize, struct lextiof_socket_private *socketarg );
00186 typedef int (LDAP_C LDAP_CALLBACK LDAP_X_EXTIOF_WRITE_CALLBACK)( int s,
00187        const void *buf, int len, struct lextiof_socket_private *socketarg );
00188 typedef int (LDAP_C LDAP_CALLBACK LDAP_X_EXTIOF_WRITEV_CALLBACK)(int s,
00189        const ldap_x_iovec iov[], int iovcnt, struct lextiof_socket_private *socketarg);
00190                                                
00191 
00192 /*
00193  * Structure for use with LBER_SOCKBUF_OPT_EXT_IO_FNS:
00194  */
00195 struct lber_x_ext_io_fns {
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         LDAP_X_EXTIOF_WRITEV_CALLBACK   *lbextiofn_writev;
00202 };
00203 #define LBER_X_EXTIO_FNS_SIZE sizeof(struct lber_x_ext_io_fns)
00204 
00205 /*
00206  * liblber memory allocation callback functions.  These are global to all
00207  *  Sockbufs and BerElements.  Install your own functions by using a call
00208  *  like this: ber_set_option( NULL, LBER_OPT_MEMALLOC_FN_PTRS, &memalloc_fns );
00209  */
00210 typedef void * (LDAP_C LDAP_CALLBACK LDAP_MALLOC_CALLBACK)( size_t size );
00211 typedef void * (LDAP_C LDAP_CALLBACK LDAP_CALLOC_CALLBACK)( size_t nelem,
00212        size_t elsize );
00213 typedef void * (LDAP_C LDAP_CALLBACK LDAP_REALLOC_CALLBACK)( void *ptr,
00214        size_t size );
00215 typedef void (LDAP_C LDAP_CALLBACK LDAP_FREE_CALLBACK)( void *ptr );
00216 
00217 struct lber_memalloc_fns {
00218        LDAP_MALLOC_CALLBACK *lbermem_malloc;
00219        LDAP_CALLOC_CALLBACK *lbermem_calloc;
00220        LDAP_REALLOC_CALLBACK       *lbermem_realloc;
00221        LDAP_FREE_CALLBACK   *lbermem_free;
00222 };
00223 
00224 /*
00225  * decode routines
00226  */
00227 LDAP_API(unsigned long) LDAP_CALL ber_get_tag( BerElement *ber );
00228 LDAP_API(unsigned long) LDAP_CALL ber_skip_tag( BerElement *ber, 
00229        unsigned long *len );
00230 LDAP_API(unsigned long) LDAP_CALL ber_peek_tag( BerElement *ber, 
00231        unsigned long *len );
00232 LDAP_API(unsigned long) LDAP_CALL ber_get_int( BerElement *ber, long *num );
00233 LDAP_API(unsigned long) LDAP_CALL ber_get_stringb( BerElement *ber, char *buf,
00234        unsigned long *len );
00235 LDAP_API(unsigned long) LDAP_CALL ber_get_stringa( BerElement *ber, 
00236        char **buf );
00237 LDAP_API(unsigned long) LDAP_CALL ber_get_stringal( BerElement *ber,
00238        struct berval **bv );
00239 LDAP_API(unsigned long) LDAP_CALL ber_get_bitstringa( BerElement *ber, 
00240        char **buf, unsigned long *len );
00241 LDAP_API(unsigned long) LDAP_CALL ber_get_null( BerElement *ber );
00242 LDAP_API(unsigned long) LDAP_CALL ber_get_boolean( BerElement *ber, 
00243        int *boolval );
00244 LDAP_API(unsigned long) LDAP_CALL ber_first_element( BerElement *ber,
00245        unsigned long *len, char **last );
00246 LDAP_API(unsigned long) LDAP_CALL ber_next_element( BerElement *ber,
00247        unsigned long *len, char *last );
00248 LDAP_API(unsigned long) LDAP_C ber_scanf( BerElement *ber, const char *fmt,
00249        ... );
00250 LDAP_API(void) LDAP_CALL ber_bvfree( struct berval *bv );
00251 LDAP_API(void) LDAP_CALL ber_bvecfree( struct berval **bv );
00252 LDAP_API(void) LDAP_CALL ber_svecfree( char **vals );
00253 LDAP_API(struct berval *) LDAP_CALL ber_bvdup( const struct berval *bv );
00254 LDAP_API(void) LDAP_CALL ber_set_string_translators( BerElement *ber,
00255        BERTranslateProc encode_proc, BERTranslateProc decode_proc );
00256 LDAP_API(BerElement *) LDAP_CALL ber_init( const struct berval *bv );
00257 
00258 /*
00259  * encoding routines
00260  */
00261 LDAP_API(int) LDAP_CALL ber_put_enum( BerElement *ber, long num, 
00262        unsigned long tag );
00263 LDAP_API(int) LDAP_CALL ber_put_int( BerElement *ber, long num, 
00264        unsigned long tag );
00265 LDAP_API(int) LDAP_CALL ber_put_ostring( BerElement *ber, char *str, 
00266        unsigned long len, unsigned long tag );
00267 LDAP_API(int) LDAP_CALL ber_put_string( BerElement *ber, char *str,
00268        unsigned long tag );
00269 LDAP_API(int) LDAP_CALL ber_put_bitstring( BerElement *ber, char *str,
00270        unsigned long bitlen, unsigned long tag );
00271 LDAP_API(int) LDAP_CALL ber_put_null( BerElement *ber, unsigned long tag );
00272 LDAP_API(int) LDAP_CALL ber_put_boolean( BerElement *ber, int boolval,
00273        unsigned long tag );
00274 LDAP_API(int) LDAP_CALL ber_start_seq( BerElement *ber, unsigned long tag );
00275 LDAP_API(int) LDAP_CALL ber_start_set( BerElement *ber, unsigned long tag );
00276 LDAP_API(int) LDAP_CALL ber_put_seq( BerElement *ber );
00277 LDAP_API(int) LDAP_CALL ber_put_set( BerElement *ber );
00278 LDAP_API(int) LDAP_C ber_printf( BerElement *ber, const char *fmt, ... );
00279 LDAP_API(int) LDAP_CALL ber_flatten( BerElement *ber,
00280        struct berval **bvPtr );
00281 
00282 /*
00283  * miscellaneous routines
00284  */
00285 LDAP_API(void) LDAP_CALL ber_free( BerElement *ber, int freebuf );
00286 LDAP_API(void) LDAP_CALL ber_special_free(void* buf, BerElement *ber);
00287 LDAP_API(int) LDAP_CALL ber_flush( Sockbuf *sb, BerElement *ber, int freeit );
00288 LDAP_API(BerElement*) LDAP_CALL ber_alloc( void );
00289 LDAP_API(BerElement*) LDAP_CALL der_alloc( void );
00290 LDAP_API(BerElement*) LDAP_CALL ber_alloc_t( int options );
00291 LDAP_API(void*) LDAP_CALL ber_special_alloc(size_t size, BerElement **ppBer);
00292 LDAP_API(BerElement*) LDAP_CALL ber_dup( BerElement *ber );
00293 LDAP_API(unsigned long) LDAP_CALL ber_get_next( Sockbuf *sb, unsigned long *len,
00294        BerElement *ber );
00295 LDAP_API(unsigned long) LDAP_CALL ber_get_next_buffer( void *buffer,
00296        size_t buffer_size, unsigned long *len, BerElement *ber,
00297        unsigned long *Bytes_Scanned );
00298 LDAP_API(unsigned long) LDAP_CALL ber_get_next_buffer_ext( void *buffer,
00299        size_t buffer_size, unsigned long *len, BerElement *ber,
00300        unsigned long *Bytes_Scanned, Sockbuf *sb );
00301 LDAP_API(long) LDAP_CALL ber_read( BerElement *ber, char *buf, 
00302        unsigned long len );
00303 LDAP_API(long) LDAP_CALL ber_write( BerElement *ber, char *buf, 
00304        unsigned long len, int nosos );
00305 LDAP_API(void) LDAP_CALL ber_init_w_nullchar( BerElement *ber, int options );
00306 LDAP_API(void) LDAP_CALL ber_reset( BerElement *ber, int was_writing );
00307 LDAP_API(int) LDAP_CALL ber_set_option( BerElement *ber, int option, 
00308        void *value );
00309 LDAP_API(int) LDAP_CALL ber_get_option( BerElement *ber, int option, 
00310        void *value );
00311 LDAP_API(Sockbuf*) LDAP_CALL ber_sockbuf_alloc( void );
00312 LDAP_API(void) LDAP_CALL ber_sockbuf_free( Sockbuf* p );
00313 LDAP_API(int) LDAP_CALL ber_sockbuf_set_option( Sockbuf *sb, int option, 
00314        void *value );
00315 LDAP_API(int) LDAP_CALL ber_sockbuf_get_option( Sockbuf *sb, int option, 
00316        void *value );
00317 
00318 #ifdef __cplusplus
00319 }
00320 #endif
00321 #endif /* _LBER_H */