jwawinber.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. {******************************************************************************}
  2. { }
  3. { Basic Encoding Rules API interface Unit for Object Pascal }
  4. { }
  5. { Portions created by Microsoft are Copyright (C) 1995-2001 Microsoft }
  6. { Corporation. All Rights Reserved. }
  7. { }
  8. { The original file is: winber.h, released August 2001. The original Pascal }
  9. { code is: WinBer.pas, released December 2000. The initial developer of the }
  10. { Pascal code is Marcel van Brakel (brakelm att chello dott nl). }
  11. { }
  12. { Portions created by Marcel van Brakel are Copyright (C) 1999-2001 }
  13. { Marcel van Brakel. All Rights Reserved. }
  14. { }
  15. { Obtained through: Joint Endeavour of Delphi Innovators (Project JEDI) }
  16. { }
  17. { You may retrieve the latest version of this file at the Project JEDI }
  18. { APILIB home page, located at http://jedi-apilib.sourceforge.net }
  19. { }
  20. { The contents of this file are used with permission, subject to the Mozilla }
  21. { Public License Version 1.1 (the "License"); you may not use this file except }
  22. { in compliance with the License. You may obtain a copy of the License at }
  23. { http://www.mozilla.org/MPL/MPL-1.1.html }
  24. { }
  25. { Software distributed under the License is distributed on an "AS IS" basis, }
  26. { WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for }
  27. { the specific language governing rights and limitations under the License. }
  28. { }
  29. { Alternatively, the contents of this file may be used under the terms of the }
  30. { GNU Lesser General Public License (the "LGPL License"), in which case the }
  31. { provisions of the LGPL License are applicable instead of those above. }
  32. { If you wish to allow use of your version of this file only under the terms }
  33. { of the LGPL License and not to allow others to use your version of this file }
  34. { under the MPL, indicate your decision by deleting the provisions above and }
  35. { replace them with the notice and other provisions required by the LGPL }
  36. { License. If you do not delete the provisions above, a recipient may use }
  37. { your version of this file under either the MPL or the LGPL License. }
  38. { }
  39. { For more information about the LGPL: http://www.gnu.org/copyleft/lesser.html }
  40. { }
  41. {******************************************************************************}
  42. // $Id: JwaWinBer.pas,v 1.9 2007/09/14 06:48:48 marquardt Exp $
  43. {$IFNDEF JWA_OMIT_SECTIONS}
  44. unit JwaWinBer;
  45. {$WEAKPACKAGEUNIT}
  46. {$ENDIF JWA_OMIT_SECTIONS}
  47. {$HPPEMIT ''}
  48. {$HPPEMIT '#include "WinBer.h"'}
  49. {$HPPEMIT ''}
  50. {$IFNDEF JWA_OMIT_SECTIONS}
  51. {$I jediapilib.inc}
  52. interface
  53. uses
  54. JwaWinLDAP, JwaWinType;
  55. {$ENDIF JWA_OMIT_SECTIONS}
  56. {$IFNDEF JWA_IMPLEMENTATIONSECTION}
  57. const
  58. LBER_ERROR = DWORD($ffffffff);
  59. {$EXTERNALSYM LBER_ERROR}
  60. LBER_DEFAULT = DWORD($ffffffff);
  61. {$EXTERNALSYM LBER_DEFAULT}
  62. type
  63. ber_tag_t = Cardinal; // for BER tags
  64. {$EXTERNALSYM ber_tag_t}
  65. ber_int_t = Integer; // for BER ints, enums, and Booleans
  66. {$EXTERNALSYM ber_int_t}
  67. ber_uint_t = Cardinal; // unsigned equivalent of ber_int_t
  68. {$EXTERNALSYM ber_uint_t}
  69. ber_slen_t = Integer; // signed equivalent of ber_len_t
  70. {$EXTERNALSYM ber_slen_t}
  71. //
  72. // This constructs a new BerElement structure containing a copy of the
  73. // data in the supplied berval structure.
  74. //
  75. function ber_init(pBerVal: PBerVal): PBerElement; cdecl;
  76. {$EXTERNALSYM ber_init}
  77. //
  78. // This frees a BerElement which is returned from ber_alloc_t()
  79. // or ber_init(). The second argument - fbuf should always be set
  80. // to 1.
  81. //
  82. //
  83. procedure ber_free(pBerElement: PBerElement; fbuf: Integer); cdecl;
  84. {$EXTERNALSYM ber_free}
  85. {$IFNDEF JWA_INCLUDEMODE}
  86. //
  87. // Frees a BERVAL structure. Applications should not call
  88. // this API to free BERVAL structures which they themselves
  89. // have allocated
  90. //
  91. procedure ber_bvfree(pBerVal: PBerVal); cdecl;
  92. {$EXTERNALSYM ber_bvfree}
  93. {$ENDIF JWA_INCLUDEMODE}
  94. //
  95. // Frees an array of BERVAL structures.
  96. //
  97. procedure ber_bvecfree(pBerVal: PBerVal); cdecl;
  98. {$EXTERNALSYM ber_bvecfree}
  99. //
  100. // Returns a copy of a the supplied berval structure
  101. //
  102. function ber_bvdup(pBerVal: PBerVal): PBerVal; cdecl;
  103. {$EXTERNALSYM ber_bvdup}
  104. //
  105. // Constructs and returns a BerElement structure. The options field
  106. // contains a bitwise-or of options which are to be used when generating
  107. // the encoding of the BerElement
  108. //
  109. // The LBER_USE_DER options should always be specified.
  110. //
  111. function ber_alloc_t(options: Integer): PBerElement; cdecl;
  112. {$EXTERNALSYM ber_alloc_t}
  113. //
  114. // This skips over the current tag and returns the tag of the next
  115. // element in the supplied BerElement. The lenght of this element is
  116. // stored in the pLen argument.
  117. //
  118. // LBER_DEFAULT is returned if there is no further data to be read
  119. // else the tag of the next element is returned.
  120. //
  121. // The difference between ber_skip_tag() and ber_peek_tag() is that the
  122. // state pointer is advanced past the first tag+lenght and is pointed to
  123. // the value part of the next element
  124. //
  125. function ber_skip_tag(pBerElement: PBerElement; var pLen: ULONG): ULONG; cdecl;
  126. {$EXTERNALSYM ber_skip_tag}
  127. //
  128. // This returns the tag of the next element to be parsed in the
  129. // supplied BerElement. The length of this element is stored in the
  130. // pLen argument.
  131. //
  132. // LBER_DEFAULT is returned if there is no further data to be read
  133. // else the tag of the next element is returned.
  134. //
  135. function ber_peek_tag(pBerElement: PBerElement; var pLen: ULONG): ULONG; cdecl;
  136. {$EXTERNALSYM ber_peek_tag}
  137. //
  138. // This returns the tag and length of the first element in a SET, SET OF
  139. // or SEQUENCE OF data value.
  140. //
  141. // LBER_DEFAULT is returned if the constructed value is empty else, the tag
  142. // is returned. It also returns an opaque cookie which has to be passed to
  143. // subsequent invocations of ber_next_element().
  144. //
  145. function ber_first_element(pBerElement: PBerElement; var pLen: ULONG; var ppOpaque: PChar): ULONG; cdecl;
  146. {$EXTERNALSYM ber_first_element}
  147. //
  148. // This positions the state at the start of the next element in the
  149. // constructed type.
  150. //
  151. // LBER_DEFAULT is returned if the constructed value is empty else, the tag
  152. // is returned.
  153. //
  154. function ber_next_element(pBerElement: PBerElement; var pLen: ULONG; opaque: PChar): ULONG; cdecl;
  155. {$EXTERNALSYM ber_next_element}
  156. //
  157. // This allocates a BerVal structure whose contents are taken from the
  158. // supplied BerElement structure.
  159. //
  160. // The return values are 0 on success and -1 on error.
  161. //
  162. function ber_flatten(pBerElement: PBerElement; var pBerVal: PBerVal): Integer; cdecl;
  163. {$EXTERNALSYM ber_flatten}
  164. (*
  165. The ber_printf() routine is used to encode a BER element in much the
  166. same way that sprintf() works. One important difference, though, is
  167. that state information is kept in the ber argument so that multiple
  168. calls can be made to ber_printf() to append to the end of the BER ele-
  169. ment. ber MUST be a pointer to a BerElement returned by ber_alloc_t().
  170. ber_printf() interprets and formats its arguments according to the for-
  171. mat string fmt. ber_printf() returns -1 if there is an error during
  172. encoding and a non-negative number if successful. As with sprintf(),
  173. each character in fmt refers to an argument to ber_printf().
  174. The format string can contain the following format characters:
  175. 't' Tag. The next argument is a ber_tag_t specifying the tag to
  176. override the next element to be written to the ber. This works
  177. across calls. The integer tag value SHOULD contain the tag
  178. class, constructed bit, and tag value. For example, a tag of
  179. "[3]" for a constructed type is 0xA3U. All implementations MUST
  180. support tags that fit in a single octet (i.e., where the tag
  181. value is less than 32) and they MAY support larger tags.
  182. 'b' Boolean. The next argument is an ber_int_t, containing either 0
  183. for FALSE or 0xff for TRUE. A boolean element is output. If
  184. this format character is not preceded by the 't' format modif-
  185. ier, the tag 0x01U is used for the element.
  186. 'e' Enumerated. The next argument is a ber_int_t, containing the
  187. enumerated value in the host's byte order. An enumerated ele-
  188. ment is output. If this format character is not preceded by the
  189. 't' format modifier, the tag 0x0AU is used for the element.
  190. 'i' Integer. The next argument is a ber_int_t, containing the
  191. integer in the host's byte order. An integer element is output.
  192. If this format character is not preceded by the 't' format
  193. modifier, the tag 0x02U is used for the element.
  194. 'n' Null. No argument is needed. An ASN.1 NULL element is output.
  195. If this format character is not preceded by the 't' format
  196. modifier, the tag 0x05U is used for the element.
  197. 'o' Octet string. The next two arguments are a char *, followed by
  198. a ber_len_t with the length of the string. The string MAY con-
  199. tain null bytes and are do not have to be zero-terminated. An
  200. octet string element is output, in primitive form. If this for-
  201. mat character is not preceded by the 't' format modifier, the
  202. tag 0x04U is used for the element.
  203. 's' Octet string. The next argument is a char * pointing to a
  204. zero-terminated string. An octet string element in primitive
  205. form is output, which does not include the trailing '\0' (null)
  206. byte. If this format character is not preceded by the 't' format
  207. modifier, the tag 0x04U is used for the element.
  208. 'v' Several octet strings. The next argument is a char **, an array
  209. of char * pointers to zero-terminated strings. The last element
  210. in the array MUST be a NULL pointer. The octet strings do not
  211. include the trailing '\0' (null) byte. Note that a construct
  212. like '{v}' is used to get an actual SEQUENCE OF octet strings.
  213. The 't' format modifier cannot be used with this format charac-
  214. ter.
  215. 'V' Several octet strings. A NULL-terminated array of struct berval
  216. *'s is supplied. Note that a construct like '{V}' is used to
  217. get an actual SEQUENCE OF octet strings. The 't' format modifier
  218. cannot be used with this format character.
  219. '{' Begin sequence. No argument is needed. If this format charac-
  220. ter is not preceded by the 't' format modifier, the tag 0x30U is
  221. used.
  222. '}' End sequence. No argument is needed. The 't' format modifier
  223. cannot be used with this format character.
  224. '[' Begin set. No argument is needed. If this format character is
  225. not preceded by the 't' format modifier, the tag 0x31U is used.
  226. ']' End set. No argument is needed. The 't' format modifier cannot
  227. be used with this format character.
  228. *)
  229. //WINBERAPI INT BERAPI ber_printf( BerElement *pBerElement, PCHAR fmt, ... );
  230. (*
  231. The ber_scanf() routine is used to decode a BER element in much the same
  232. way that sscanf() works. One important difference, though, is that some
  233. state information is kept with the ber argument so that multiple calls
  234. can be made to ber_scanf() to sequentially read from the BER element.
  235. The ber argument SHOULD be a pointer to a BerElement returned by
  236. ber_init(). ber_scanf interprets the bytes according to the format
  237. string fmt, and stores the results in its additional arguments.
  238. ber_scanf() returns LBER_ERROR on error, and a different value on suc-
  239. cess.
  240. The format string contains conversion specifications which are used to
  241. direct the interpretation of the BER element. The format string can
  242. contain the following characters:
  243. 'a' Octet string. A char ** argument MUST be supplied. Memory is
  244. allocated, filled with the contents of the octet string, zero-
  245. terminated, and the pointer to the string is stored in the argu-
  246. ment. The returned value SHOULD be freed using ldap_memfree.
  247. The tag of the element MUST indicate the primitive form
  248. (constructed strings are not supported) but is otherwise ignored
  249. and discarded during the decoding. This format cannot be used
  250. with octet strings which could contain null bytes.
  251. 'O' Octet string. A struct berval ** argument MUST be supplied,
  252. which upon return points to an allocated struct berval contain-
  253. ing the octet string and its length. ber_bvfree() SHOULD be
  254. called to free the allocated memory. The tag of the element
  255. MUST indicate the primitive form (constructed strings are not
  256. supported) but is otherwise ignored during the decoding.
  257. 'b' Boolean. A pointer to a ber_int_t MUST be supplied. The
  258. ber_int_t value stored will be 0 for FALSE or nonzero for TRUE.
  259. The tag of the element MUST indicate the primitive form but is
  260. otherwise ignored during the decoding.
  261. 'e' Enumerated. A pointer to a ber_int_t MUST be supplied. The
  262. enumerated value stored will be in host byte order. The tag of
  263. the element MUST indicate the primitive form but is otherwise
  264. ignored during the decoding. ber_scanf() will return an error
  265. if the value of the enumerated value cannot be stored in a
  266. ber_int_t.
  267. 'i' Integer. A pointer to a ber_int_t MUST be supplied. The
  268. ber_int_t value stored will be in host byte order. The tag of
  269. the element MUST indicate the primitive form but is otherwise
  270. ignored during the decoding. ber_scanf() will return an error
  271. if the integer cannot be stored in a ber_int_t.
  272. 'B' Bitstring. A char ** argument MUST be supplied which will point
  273. to the allocated bits, followed by a ber_len_t * argument, which
  274. will point to the length (in bits) of the bitstring returned.
  275. ldap_memfree SHOULD be called to free the bitstring. The tag of
  276. the element MUST indicate the primitive form (constructed bit-
  277. strings are not supported) but is otherwise ignored during the
  278. decoding.
  279. 'n' Null. No argument is needed. The element is verified to have a
  280. zero-length value and is skipped. The tag is ignored.
  281. 'v' Several octet strings. A char *** argument MUST be supplied,
  282. which upon return points to an allocated NULL-terminated array
  283. of char *'s containing the octet strings. NULL is stored if the
  284. sequence is empty. ldap_memfree SHOULD be called to free each
  285. element of the array and the array itself. The tag of the
  286. sequence and of the octet strings are ignored.
  287. 'V' Several octet strings (which could contain null bytes). A
  288. struct berval *** MUST be supplied, which upon return points to
  289. a allocated NULL-terminated array of struct berval *'s contain-
  290. ing the octet strings and their lengths. NULL is stored if the
  291. sequence is empty. ber_bvecfree() can be called to free the
  292. allocated memory. The tag of the sequence and of the octet
  293. strings are ignored.
  294. 'x' Skip element. The next element is skipped. No argument is
  295. needed.
  296. '{' Begin sequence. No argument is needed. The initial sequence
  297. tag and length are skipped.
  298. '}' End sequence. No argument is needed.
  299. '[' Begin set. No argument is needed. The initial set tag and
  300. length are skipped.
  301. ']' End set. No argument is needed.
  302. *)
  303. //WINBERAPI ULONG BERAPI ber_scanf( BerElement *pBerElement, PCHAR fmt, ... );
  304. {$ENDIF JWA_IMPLEMENTATIONSECTION}
  305. {$IFNDEF JWA_OMIT_SECTIONS}
  306. implementation
  307. //uses ...
  308. {$ENDIF JWA_OMIT_SECTIONS}
  309. {$IFNDEF JWA_INTERFACESECTION}
  310. {$IFNDEF JWA_INCLUDEMODE}
  311. const
  312. winberapi = 'wldap32.dll';
  313. {$ENDIF JWA_INCLUDEMODE}
  314. function ber_init; external winberapi name 'ber_init';
  315. procedure ber_free; external winberapi name 'ber_free';
  316. {$IFNDEF JWA_INCLUDEMODE}
  317. procedure ber_bvfree; external winberapi name 'ber_bvfree';
  318. {$ENDIF JWA_INCLUDEMODE}
  319. procedure ber_bvecfree; external winberapi name 'ber_bvecfree';
  320. function ber_bvdup; external winberapi name 'ber_bvdup';
  321. function ber_alloc_t; external winberapi name 'ber_alloc_t';
  322. function ber_skip_tag; external winberapi name 'ber_skip_tag';
  323. function ber_peek_tag; external winberapi name 'ber_peek_tag';
  324. function ber_first_element; external winberapi name 'ber_first_element';
  325. function ber_next_element; external winberapi name 'ber_next_element';
  326. function ber_flatten; external winberapi name 'ber_flatten';
  327. //function ber_printf; external winberapi name 'ber_printf';
  328. //function ber_scanf; external winberapi name 'ber_scanf';
  329. {$ENDIF JWA_INTERFACESECTION}
  330. {$IFNDEF JWA_OMIT_SECTIONS}
  331. end.
  332. {$ENDIF JWA_OMIT_SECTIONS}