jwawinber.pas 17 KB

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