1 /* $NetBSD: lber-int.h,v 1.3 2021/08/14 16:14:55 christos Exp $ */ 2 3 /* $OpenLDAP$ */ 4 /* This work is part of OpenLDAP Software <http://www.openldap.org/>. 5 * 6 * Copyright 1998-2021 The OpenLDAP Foundation. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted only as authorized by the OpenLDAP 11 * Public License. 12 * 13 * A copy of this license is available in the file LICENSE in the 14 * top-level directory of the distribution or, alternatively, at 15 * <http://www.OpenLDAP.org/license.html>. 16 */ 17 /* Portions Copyright (c) 1990 Regents of the University of Michigan. 18 * All rights reserved. 19 * 20 * Redistribution and use in source and binary forms are permitted 21 * provided that this notice is preserved and that due credit is given 22 * to the University of Michigan at Ann Arbor. The name of the University 23 * may not be used to endorse or promote products derived from this 24 * software without specific prior written permission. This software 25 * is provided ``as is'' without express or implied warranty. 26 */ 27 28 #ifndef _LBER_INT_H 29 #define _LBER_INT_H 30 31 #include "lber.h" 32 #define LDAP_INT_DEBUG 33 #include "ldap_log.h" 34 #include "lber_pvt.h" 35 #include "ldap_queue.h" 36 37 LDAP_BEGIN_DECL 38 39 typedef void (*BER_LOG_FN)(FILE *file, 40 const char *subsys, int level, const char *fmt, ... ); 41 42 LBER_V (BER_ERRNO_FN) ber_int_errno_fn; 43 44 #ifdef LDAP_MEMORY_TRACE 45 # ifndef LDAP_MEMORY_DEBUG 46 # define LDAP_MEMORY_DEBUG 1 47 # endif 48 #endif 49 50 #ifdef LDAP_MEMORY_DEBUG 51 LBER_V (long) ber_int_meminuse; 52 #endif 53 #if defined(LDAP_MEMORY_DEBUG) && ((LDAP_MEMORY_DEBUG +0) & 2) 54 # define LDAP_MEMORY_DEBUG_ASSERT assert 55 #else 56 # define LDAP_MEMORY_DEBUG_ASSERT(expr) ((void) 0) 57 #endif 58 59 struct lber_options { 60 short lbo_valid; 61 unsigned short lbo_options; 62 int lbo_debug; 63 }; 64 65 LBER_F( int ) ber_pvt_log_output( 66 const char *subsystem, 67 int level, 68 const char *fmt, ... ); 69 70 #define LBER_UNINITIALIZED 0x0 71 #define LBER_INITIALIZED 0x1 72 #define LBER_VALID_BERELEMENT 0x2 73 #define LBER_VALID_SOCKBUF 0x3 74 75 LBER_V (struct lber_options) ber_int_options; 76 #define ber_int_debug ber_int_options.lbo_debug 77 78 /* Data encoded in ASN.1 BER format */ 79 struct berelement { 80 struct lber_options ber_opts; 81 #define ber_valid ber_opts.lbo_valid 82 #define ber_options ber_opts.lbo_options 83 #define ber_debug ber_opts.lbo_debug 84 85 /* 86 * The members below, when not NULL/LBER_DEFAULT/etc, are: 87 * ber_buf Data buffer. Other pointers normally point into it. 88 * ber_rwptr Read/write cursor for Sockbuf I/O. 89 * ber_memctx Context passed to ber_memalloc() & co. 90 * When decoding data (reading it from the BerElement): 91 * ber_end End of BER data. 92 * ber_ptr Read cursor, except for 1st octet of tags. 93 * ber_tag 1st octet of next tag, saved from *ber_ptr when 94 * ber_ptr may be pointing at a tag and is >ber_buf. 95 * The octet *ber_ptr itself may get overwritten with 96 * a \0, to terminate the preceding element. 97 * When encoding data (writing it to the BerElement): 98 * ber_end End of allocated buffer - 1 (allowing a final \0). 99 * ber_ptr Last complete BER element (normally write cursor). 100 * ber_sos_ptr NULL or write cursor for incomplete sequence or set. 101 * ber_sos_inner offset(seq/set length octets) if ber_sos_ptr!=NULL. 102 * ber_tag Default tag for next ber_printf() element. 103 * ber_usertag Boolean set by ber_printf "!" if it sets ber_tag. 104 * ber_len Reused for ber_sos_inner. 105 * When output to a Sockbuf: 106 * ber_ptr End of encoded data to write. 107 * When input from a Sockbuf: 108 * See ber_get_next(). 109 */ 110 111 /* Do not change the order of these 3 fields! see ber_get_next */ 112 ber_tag_t ber_tag; 113 ber_len_t ber_len; 114 ber_tag_t ber_usertag; 115 116 char *ber_buf; 117 char *ber_ptr; 118 char *ber_end; 119 120 char *ber_sos_ptr; 121 # define ber_sos_inner ber_len /* reused for binary compat */ 122 123 char *ber_rwptr; 124 void *ber_memctx; 125 }; 126 #define LBER_VALID(ber) ((ber)->ber_valid==LBER_VALID_BERELEMENT) 127 128 #define ber_pvt_ber_remaining(ber) ((ber)->ber_end - (ber)->ber_ptr) 129 #define ber_pvt_ber_total(ber) ((ber)->ber_end - (ber)->ber_buf) 130 #define ber_pvt_ber_write(ber) ((ber)->ber_ptr - (ber)->ber_buf) 131 132 struct sockbuf { 133 struct lber_options sb_opts; 134 Sockbuf_IO_Desc *sb_iod; /* I/O functions */ 135 #define sb_valid sb_opts.lbo_valid 136 #define sb_options sb_opts.lbo_options 137 #define sb_debug sb_opts.lbo_debug 138 ber_socket_t sb_fd; 139 ber_len_t sb_max_incoming; 140 unsigned int sb_trans_needs_read:1; 141 unsigned int sb_trans_needs_write:1; 142 #ifdef LDAP_PF_LOCAL_SENDMSG 143 char sb_ungetlen; 144 char sb_ungetbuf[8]; 145 #endif 146 }; 147 148 #define SOCKBUF_VALID( sb ) ( (sb)->sb_valid == LBER_VALID_SOCKBUF ) 149 150 151 /* 152 * decode.c, encode.c 153 */ 154 155 /* Simplest OID max-DER-component to implement in both decode and encode */ 156 #define LBER_OID_COMPONENT_MAX ((unsigned long)-1 - 128) 157 158 159 /* 160 * io.c 161 */ 162 LBER_F( int ) 163 ber_realloc LDAP_P(( 164 BerElement *ber, 165 ber_len_t len )); 166 167 LBER_F (char *) ber_start LDAP_P(( BerElement * )); 168 LBER_F (int) ber_len LDAP_P(( BerElement * )); 169 LBER_F (int) ber_ptrlen LDAP_P(( BerElement * )); 170 LBER_F (void) ber_rewind LDAP_P(( BerElement * )); 171 172 /* 173 * bprint.c 174 */ 175 #define ber_log_printf ber_pvt_log_printf 176 177 LBER_F( int ) 178 ber_log_bprint LDAP_P(( 179 int errlvl, 180 int loglvl, 181 const char *data, 182 ber_len_t len )); 183 184 LBER_F( int ) 185 ber_log_dump LDAP_P(( 186 int errlvl, 187 int loglvl, 188 BerElement *ber, 189 int inout )); 190 191 LBER_V (BER_LOG_FN) ber_int_log_proc; 192 LBER_V (FILE *) ber_pvt_err_file; 193 194 /* memory.c */ 195 /* simple macros to realloc for now */ 196 LBER_V (BerMemoryFunctions *) ber_int_memory_fns; 197 LBER_F (char *) ber_strndup( LDAP_CONST char *, ber_len_t ); 198 LBER_F (char *) ber_strndup_x( LDAP_CONST char *, ber_len_t, void *ctx ); 199 200 #define LBER_MALLOC(s) ber_memalloc((s)) 201 #define LBER_CALLOC(n,s) ber_memcalloc((n),(s)) 202 #define LBER_REALLOC(p,s) ber_memrealloc((p),(s)) 203 #define LBER_FREE(p) ber_memfree((p)) 204 #define LBER_VFREE(v) ber_memvfree((void**)(v)) 205 #define LBER_STRDUP(s) ber_strdup((s)) 206 #define LBER_STRNDUP(s,l) ber_strndup((s),(l)) 207 208 /* sockbuf.c */ 209 210 LBER_F( int ) 211 ber_int_sb_init LDAP_P(( Sockbuf *sb )); 212 213 LBER_F( int ) 214 ber_int_sb_close LDAP_P(( Sockbuf *sb )); 215 216 LBER_F( int ) 217 ber_int_sb_destroy LDAP_P(( Sockbuf *sb )); 218 219 LBER_F( ber_slen_t ) 220 ber_int_sb_read LDAP_P(( Sockbuf *sb, void *buf, ber_len_t len )); 221 222 LBER_F( ber_slen_t ) 223 ber_int_sb_write LDAP_P(( Sockbuf *sb, void *buf, ber_len_t len )); 224 225 LDAP_END_DECL 226 227 #endif /* _LBER_INT_H */ 228