lber-int.h 6.3 KB

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