jwawinber.pas 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. unit JwaWinBer;
  43. {$WEAKPACKAGEUNIT}
  44. {$HPPEMIT ''}
  45. {$HPPEMIT '#include "WinBer.h"'}
  46. {$HPPEMIT ''}
  47. {$I jediapilib.inc}
  48. interface
  49. uses
  50. JwaWinLDAP, JwaWinType;
  51. const
  52. LBER_ERROR = DWORD($ffffffff);
  53. {$EXTERNALSYM LBER_ERROR}
  54. LBER_DEFAULT = DWORD($ffffffff);
  55. {$EXTERNALSYM LBER_DEFAULT}
  56. type
  57. ber_tag_t = Cardinal; // for BER tags
  58. {$EXTERNALSYM ber_tag_t}
  59. ber_int_t = Integer; // for BER ints, enums, and Booleans
  60. {$EXTERNALSYM ber_int_t}
  61. ber_uint_t = Cardinal; // unsigned equivalent of ber_int_t
  62. {$EXTERNALSYM ber_uint_t}
  63. ber_slen_t = Integer; // signed equivalent of ber_len_t
  64. {$EXTERNALSYM ber_slen_t}
  65. //
  66. // This constructs a new BerElement structure containing a copy of the
  67. // data in the supplied berval structure.
  68. //
  69. function ber_init(pBerVal: PBerVal): PBerElement; cdecl;
  70. {$EXTERNALSYM ber_init}
  71. //
  72. // This frees a BerElement which is returned from ber_alloc_t()
  73. // or ber_init(). The second argument - fbuf should always be set
  74. // to 1.
  75. //
  76. //
  77. procedure ber_free(pBerElement: PBerElement; fbuf: Integer); cdecl;
  78. {$EXTERNALSYM ber_free}
  79. //
  80. // Frees a BERVAL structure. Applications should not call
  81. // this API to free BERVAL structures which they themselves
  82. // have allocated
  83. //
  84. procedure ber_bvfree(pBerVal: PBerVal); cdecl;
  85. {$EXTERNALSYM ber_bvfree}
  86. //
  87. // Frees an array of BERVAL structures.
  88. //
  89. procedure ber_bvecfree(pBerVal: PBerVal); cdecl;
  90. {$EXTERNALSYM ber_bvecfree}
  91. //
  92. // Returns a copy of a the supplied berval structure
  93. //
  94. function ber_bvdup(pBerVal: PBerVal): PBerVal; cdecl;
  95. {$EXTERNALSYM ber_bvdup}
  96. //
  97. // Constructs and returns a BerElement structure. The options field
  98. // contains a bitwise-or of options which are to be used when generating
  99. // the encoding of the BerElement
  100. //
  101. // The LBER_USE_DER options should always be specified.
  102. //
  103. function ber_alloc_t(options: Integer): PBerElement; cdecl;
  104. {$EXTERNALSYM ber_alloc_t}
  105. //
  106. // This skips over the current tag and returns the tag of the next
  107. // element in the supplied BerElement. The lenght of this element is
  108. // stored in the pLen argument.
  109. //
  110. // LBER_DEFAULT is returned if there is no further data to be read
  111. // else the tag of the next element is returned.
  112. //
  113. // The difference between ber_skip_tag() and ber_peek_tag() is that the
  114. // state pointer is advanced past the first tag+lenght and is pointed to
  115. // the value part of the next element
  116. //
  117. function ber_skip_tag(pBerElement: PBerElement; var pLen: ULONG): ULONG; cdecl;
  118. {$EXTERNALSYM ber_skip_tag}
  119. //
  120. // This returns the tag of the next element to be parsed in the
  121. // supplied BerElement. The length of this element is stored in the
  122. // pLen argument.
  123. //
  124. // LBER_DEFAULT is returned if there is no further data to be read
  125. // else the tag of the next element is returned.
  126. //
  127. function ber_peek_tag(pBerElement: PBerElement; var pLen: ULONG): ULONG; cdecl;
  128. {$EXTERNALSYM ber_peek_tag}
  129. //
  130. // This returns the tag and length of the first element in a SET, SET OF
  131. // or SEQUENCE OF data value.
  132. //
  133. // LBER_DEFAULT is returned if the constructed value is empty else, the tag
  134. // is returned. It also returns an opaque cookie which has to be passed to
  135. // subsequent invocations of ber_next_element().
  136. //
  137. function ber_first_element(pBerElement: PBerElement; var pLen: ULONG; var ppOpaque: PChar): ULONG; cdecl;
  138. {$EXTERNALSYM ber_first_element}
  139. //
  140. // This positions the state at the start of the next element in the
  141. // constructed type.
  142. //
  143. // LBER_DEFAULT is returned if the constructed value is empty else, the tag
  144. // is returned.
  145. //
  146. function ber_next_element(pBerElement: PBerElement; var pLen: ULONG; opaque: PChar): ULONG; cdecl;
  147. {$EXTERNALSYM ber_next_element}
  148. //
  149. // This allocates a BerVal structure whose contents are taken from the
  150. // supplied BerElement structure.
  151. //
  152. // The return values are 0 on success and -1 on error.
  153. //
  154. function ber_flatten(pBerElement: PBerElement; var pBerVal: PBerVal): Integer; cdecl;
  155. {$EXTERNALSYM ber_flatten}
  156. (*
  157. The ber_printf() routine is used to encode a BER element in much the
  158. same way that sprintf() works. One important difference, though, is
  159. that state information is kept in the ber argument so that multiple
  160. calls can be made to ber_printf() to append to the end of the BER ele-
  161. ment. ber MUST be a pointer to a BerElement returned by ber_alloc_t().
  162. ber_printf() interprets and formats its arguments according to the for-
  163. mat string fmt. ber_printf() returns -1 if there is an error during
  164. encoding and a non-negative number if successful. As with sprintf(),
  165. each character in fmt refers to an argument to ber_printf().
  166. The format string can contain the following format characters:
  167. 't' Tag. The next argument is a ber_tag_t specifying the tag to
  168. override the next element to be written to the ber. This works
  169. across calls. The integer tag value SHOULD contain the tag
  170. class, constructed bit, and tag value. For example, a tag of
  171. "[3]" for a constructed type is 0xA3U. All implementations MUST
  172. support tags that fit in a single octet (i.e., where the tag
  173. value is less than 32) and they MAY support larger tags.
  174. 'b' Boolean. The next argument is an ber_int_t, containing either 0
  175. for FALSE or 0xff for TRUE. A boolean element is output. If
  176. this format character is not preceded by the 't' format modif-
  177. ier, the tag 0x01U is used for the element.
  178. 'e' Enumerated. The next argument is a ber_int_t, containing the
  179. enumerated value in the host's byte order. An enumerated ele-
  180. ment is output. If this format character is not preceded by the
  181. 't' format modifier, the tag 0x0AU is used for the element.
  182. 'i' Integer. The next argument is a ber_int_t, containing the
  183. integer in the host's byte order. An integer element is output.
  184. If this format character is not preceded by the 't' format
  185. modifier, the tag 0x02U is used for the element.
  186. 'n' Null. No argument is needed. An ASN.1 NULL element is output.
  187. If this format character is not preceded by the 't' format
  188. modifier, the tag 0x05U is used for the element.
  189. 'o' Octet string. The next two arguments are a char *, followed by
  190. a ber_len_t with the length of the string. The string MAY con-
  191. tain null bytes and are do not have to be zero-terminated. An
  192. octet string element is output, in primitive form. If this for-
  193. mat character is not preceded by the 't' format modifier, the
  194. tag 0x04U is used for the element.
  195. 's' Octet string. The next argument is a char * pointing to a
  196. zero-terminated string. An octet string element in primitive
  197. form is output, which does not include the trailing '\0' (null)
  198. byte. If this format character is not preceded by the 't' format
  199. modifier, the tag 0x04U is used for the element.
  200. 'v' Several octet strings. The next argument is a char **, an array
  201. of char * pointers to zero-terminated strings. The last element
  202. in the array MUST be a NULL pointer. The octet strings do not
  203. include the trailing '\0' (null) byte. Note that a construct
  204. like '{v}' is used to get an actual SEQUENCE OF octet strings.
  205. The 't' format modifier cannot be used with this format charac-
  206. ter.
  207. 'V' Several octet strings. A NULL-terminated array of struct berval
  208. *'s is supplied. Note that a construct like '{V}' is used to
  209. get an actual SEQUENCE OF octet strings. The 't' format modifier
  210. cannot be used with this format character.
  211. '{' Begin sequence. No argument is needed. If this format charac-
  212. ter is not preceded by the 't' format modifier, the tag 0x30U is
  213. used.
  214. '}' End sequence. No argument is needed. The 't' format modifier
  215. cannot be used with this format character.
  216. '[' Begin set. No argument is needed. If this format character is
  217. not preceded by the 't' format modifier, the tag 0x31U is used.
  218. ']' End set. No argument is needed. The 't' format modifier cannot
  219. be used with this format character.
  220. *)
  221. //WINBERAPI INT BERAPI ber_printf( BerElement *pBerElement, PCHAR fmt, ... );
  222. (*
  223. The ber_scanf() routine is used to decode a BER element in much the same
  224. way that sscanf() works. One important difference, though, is that some
  225. state information is kept with the ber argument so that multiple calls
  226. can be made to ber_scanf() to sequentially read from the BER element.
  227. The ber argument SHOULD be a pointer to a BerElement returned by
  228. ber_init(). ber_scanf interprets the bytes according to the format
  229. string fmt, and stores the results in its additional arguments.
  230. ber_scanf() returns LBER_ERROR on error, and a different value on suc-
  231. cess.
  232. The format string contains conversion specifications which are used to
  233. direct the interpretation of the BER element. The format string can
  234. contain the following characters:
  235. 'a' Octet string. A char ** argument MUST be supplied. Memory is
  236. allocated, filled with the contents of the octet string, zero-
  237. terminated, and the pointer to the string is stored in the argu-
  238. ment. The returned value SHOULD be freed using ldap_memfree.
  239. The tag of the element MUST indicate the primitive form
  240. (constructed strings are not supported) but is otherwise ignored
  241. and discarded during the decoding. This format cannot be used
  242. with octet strings which could contain null bytes.
  243. 'O' Octet string. A struct berval ** argument MUST be supplied,
  244. which upon return points to an allocated struct berval contain-
  245. ing the octet string and its length. ber_bvfree() SHOULD be
  246. called to free the allocated memory. The tag of the element
  247. MUST indicate the primitive form (constructed strings are not
  248. supported) but is otherwise ignored during the decoding.
  249. 'b' Boolean. A pointer to a ber_int_t MUST be supplied. The
  250. ber_int_t value stored will be 0 for FALSE or nonzero for TRUE.
  251. The tag of the element MUST indicate the primitive form but is
  252. otherwise ignored during the decoding.
  253. 'e' Enumerated. A pointer to a ber_int_t MUST be supplied. The
  254. enumerated value stored will be in host byte order. The tag of
  255. the element MUST indicate the primitive form but is otherwise
  256. ignored during the decoding. ber_scanf() will return an error
  257. if the value of the enumerated value cannot be stored in a
  258. ber_int_t.
  259. 'i' Integer. A pointer to a ber_int_t MUST be supplied. The
  260. ber_int_t value stored will be in host byte order. The tag of
  261. the element MUST indicate the primitive form but is otherwise
  262. ignored during the decoding. ber_scanf() will return an error
  263. if the integer cannot be stored in a ber_int_t.
  264. 'B' Bitstring. A char ** argument MUST be supplied which will point
  265. to the allocated bits, followed by a ber_len_t * argument, which
  266. will point to the length (in bits) of the bitstring returned.
  267. ldap_memfree SHOULD be called to free the bitstring. The tag of
  268. the element MUST indicate the primitive form (constructed bit-
  269. strings are not supported) but is otherwise ignored during the
  270. decoding.
  271. 'n' Null. No argument is needed. The element is verified to have a
  272. zero-length value and is skipped. The tag is ignored.
  273. 'v' Several octet strings. A char *** argument MUST be supplied,
  274. which upon return points to an allocated NULL-terminated array
  275. of char *'s containing the octet strings. NULL is stored if the
  276. sequence is empty. ldap_memfree SHOULD be called to free each
  277. element of the array and the array itself. The tag of the
  278. sequence and of the octet strings are ignored.
  279. 'V' Several octet strings (which could contain null bytes). A
  280. struct berval *** MUST be supplied, which upon return points to
  281. a allocated NULL-terminated array of struct berval *'s contain-
  282. ing the octet strings and their lengths. NULL is stored if the
  283. sequence is empty. ber_bvecfree() can be called to free the
  284. allocated memory. The tag of the sequence and of the octet
  285. strings are ignored.
  286. 'x' Skip element. The next element is skipped. No argument is
  287. needed.
  288. '{' Begin sequence. No argument is needed. The initial sequence
  289. tag and length are skipped.
  290. '}' End sequence. No argument is needed.
  291. '[' Begin set. No argument is needed. The initial set tag and
  292. length are skipped.
  293. ']' End set. No argument is needed.
  294. *)
  295. //WINBERAPI ULONG BERAPI ber_scanf( BerElement *pBerElement, PCHAR fmt, ... );
  296. implementation
  297. const
  298. winberapi = 'wldap32.dll';
  299. function ber_init; external winberapi name 'ber_init';
  300. procedure ber_free; external winberapi name 'ber_free';
  301. procedure ber_bvfree; external winberapi name 'ber_bvfree';
  302. procedure ber_bvecfree; external winberapi name 'ber_bvecfree';
  303. function ber_bvdup; external winberapi name 'ber_bvdup';
  304. function ber_alloc_t; external winberapi name 'ber_alloc_t';
  305. function ber_skip_tag; external winberapi name 'ber_skip_tag';
  306. function ber_peek_tag; external winberapi name 'ber_peek_tag';
  307. function ber_first_element; external winberapi name 'ber_first_element';
  308. function ber_next_element; external winberapi name 'ber_next_element';
  309. function ber_flatten; external winberapi name 'ber_flatten';
  310. //function ber_printf; external winberapi name 'ber_printf';
  311. //function ber_scanf; external winberapi name 'ber_scanf';
  312. end.