sfparse.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /*
  2. * sfparse
  3. *
  4. * Copyright (c) 2023 sfparse contributors
  5. * Copyright (c) 2019 nghttp3 contributors
  6. * Copyright (c) 2015 nghttp2 contributors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining
  9. * a copy of this software and associated documentation files (the
  10. * "Software"), to deal in the Software without restriction, including
  11. * without limitation the rights to use, copy, modify, merge, publish,
  12. * distribute, sublicense, and/or sell copies of the Software, and to
  13. * permit persons to whom the Software is furnished to do so, subject to
  14. * the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be
  17. * included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. */
  27. #ifndef SFPARSE_H
  28. #define SFPARSE_H
  29. /* Define WIN32 when build target is Win32 API (borrowed from
  30. libcurl) */
  31. #if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
  32. # define WIN32
  33. #endif
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. #if defined(_MSC_VER) && (_MSC_VER < 1800)
  38. /* MSVC < 2013 does not have inttypes.h because it is not C99
  39. compliant. See compiler macros and version number in
  40. https://sourceforge.net/p/predef/wiki/Compilers/ */
  41. # include <stdint.h>
  42. #else /* !defined(_MSC_VER) || (_MSC_VER >= 1800) */
  43. # include <inttypes.h>
  44. #endif /* !defined(_MSC_VER) || (_MSC_VER >= 1800) */
  45. #include <sys/types.h>
  46. #include <stddef.h>
  47. /**
  48. * @enum
  49. *
  50. * :type:`sf_type` defines value type.
  51. */
  52. typedef enum sf_type {
  53. /**
  54. * :enum:`SF_TYPE_BOOLEAN` indicates boolean type.
  55. */
  56. SF_TYPE_BOOLEAN,
  57. /**
  58. * :enum:`SF_TYPE_INTEGER` indicates integer type.
  59. */
  60. SF_TYPE_INTEGER,
  61. /**
  62. * :enum:`SF_TYPE_DECIMAL` indicates decimal type.
  63. */
  64. SF_TYPE_DECIMAL,
  65. /**
  66. * :enum:`SF_TYPE_STRING` indicates string type.
  67. */
  68. SF_TYPE_STRING,
  69. /**
  70. * :enum:`SF_TYPE_TOKEN` indicates token type.
  71. */
  72. SF_TYPE_TOKEN,
  73. /**
  74. * :enum:`SF_TYPE_BYTESEQ` indicates byte sequence type.
  75. */
  76. SF_TYPE_BYTESEQ,
  77. /**
  78. * :enum:`SF_TYPE_INNER_LIST` indicates inner list type.
  79. */
  80. SF_TYPE_INNER_LIST,
  81. /**
  82. * :enum:`SF_TYPE_DATE` indicates date type.
  83. */
  84. SF_TYPE_DATE
  85. } sf_type;
  86. /**
  87. * @macro
  88. *
  89. * :macro:`SF_ERR_PARSE_ERROR` indicates fatal parse error has
  90. * occurred, and it is not possible to continue the processing.
  91. */
  92. #define SF_ERR_PARSE_ERROR -1
  93. /**
  94. * @macro
  95. *
  96. * :macro:`SF_ERR_EOF` indicates that there is nothing left to read.
  97. * The context of this error varies depending on the function that
  98. * returns this error code.
  99. */
  100. #define SF_ERR_EOF -2
  101. /**
  102. * @struct
  103. *
  104. * :type:`sf_vec` stores sequence of bytes.
  105. */
  106. typedef struct sf_vec {
  107. /**
  108. * :member:`base` points to the beginning of the sequence of bytes.
  109. */
  110. uint8_t *base;
  111. /**
  112. * :member:`len` is the number of bytes contained in this sequence.
  113. */
  114. size_t len;
  115. } sf_vec;
  116. /**
  117. * @macro
  118. *
  119. * :macro:`SF_VALUE_FLAG_NONE` indicates no flag set.
  120. */
  121. #define SF_VALUE_FLAG_NONE 0x0u
  122. /**
  123. * @macro
  124. *
  125. * :macro:`SF_VALUE_FLAG_ESCAPED_STRING` indicates that a string
  126. * contains escaped character(s).
  127. */
  128. #define SF_VALUE_FLAG_ESCAPED_STRING 0x1u
  129. /**
  130. * @struct
  131. *
  132. * :type:`sf_decimal` contains decimal value.
  133. */
  134. typedef struct sf_decimal {
  135. /**
  136. * :member:`numer` contains numerator of the decimal value.
  137. */
  138. int64_t numer;
  139. /**
  140. * :member:`denom` contains denominator of the decimal value.
  141. */
  142. int64_t denom;
  143. } sf_decimal;
  144. /**
  145. * @struct
  146. *
  147. * :type:`sf_value` stores a Structured Field item. For Inner List,
  148. * only type is set to :enum:`sf_type.SF_TYPE_INNER_LIST`. In order
  149. * to read the items contained in an inner list, call
  150. * `sf_parser_inner_list`.
  151. */
  152. typedef struct sf_value {
  153. /**
  154. * :member:`type` is the type of the value contained in this
  155. * particular object.
  156. */
  157. sf_type type;
  158. /**
  159. * :member:`flags` is bitwise OR of one or more of
  160. * :macro:`SF_VALUE_FLAG_* <SF_VALUE_FLAG_NONE>`.
  161. */
  162. uint32_t flags;
  163. /**
  164. * @anonunion_start
  165. *
  166. * @sf_value_value
  167. */
  168. union {
  169. /**
  170. * :member:`boolean` contains boolean value if :member:`type` ==
  171. * :enum:`sf_type.SF_TYPE_BOOLEAN`. 1 indicates true, and 0
  172. * indicates false.
  173. */
  174. int boolean;
  175. /**
  176. * :member:`integer` contains integer value if :member:`type` is
  177. * either :enum:`sf_type.SF_TYPE_INTEGER` or
  178. * :enum:`sf_type.SF_TYPE_DATE`.
  179. */
  180. int64_t integer;
  181. /**
  182. * :member:`decimal` contains decimal value if :member:`type` ==
  183. * :enum:`sf_type.SF_TYPE_DECIMAL`.
  184. */
  185. sf_decimal decimal;
  186. /**
  187. * :member:`vec` contains sequence of bytes if :member:`type` is
  188. * either :enum:`sf_type.SF_TYPE_STRING`,
  189. * :enum:`sf_type.SF_TYPE_TOKEN`, or
  190. * :enum:`sf_type.SF_TYPE_BYTESEQ`.
  191. *
  192. * For :enum:`sf_type.SF_TYPE_STRING`, this field contains one or
  193. * more escaped characters if :member:`flags` has
  194. * :macro:`SF_VALUE_FLAG_ESCAPED_STRING` set. To unescape the
  195. * string, use `sf_unescape`.
  196. *
  197. * For :enum:`sf_type.SF_TYPE_BYTESEQ`, this field contains base64
  198. * encoded string. To decode this byte string, use
  199. * `sf_base64decode`.
  200. *
  201. * If :member:`vec.len <sf_vec.len>` == 0, :member:`vec.base
  202. * <sf_vec.base>` is guaranteed to be NULL.
  203. */
  204. sf_vec vec;
  205. /**
  206. * @anonunion_end
  207. */
  208. };
  209. } sf_value;
  210. /**
  211. * @struct
  212. *
  213. * :type:`sf_parser` is the Structured Field Values parser. Use
  214. * `sf_parser_init` to initialize it.
  215. */
  216. typedef struct sf_parser {
  217. /* all fields are private */
  218. const uint8_t *pos;
  219. const uint8_t *end;
  220. uint32_t state;
  221. } sf_parser;
  222. /**
  223. * @function
  224. *
  225. * `sf_parser_init` initializes |sfp| with the given buffer pointed by
  226. * |data| of length |datalen|.
  227. */
  228. void sf_parser_init(sf_parser *sfp, const uint8_t *data, size_t datalen);
  229. /**
  230. * @function
  231. *
  232. * `sf_parser_param` reads a parameter. If this function returns 0,
  233. * it stores parameter key and value in |dest_key| and |dest_value|
  234. * respectively, if they are not NULL.
  235. *
  236. * This function does no effort to find duplicated keys. Same key may
  237. * be reported more than once.
  238. *
  239. * Caller should keep calling this function until it returns negative
  240. * error code. If it returns :macro:`SF_ERR_EOF`, all parameters have
  241. * read, and caller can continue to read rest of the values. If it
  242. * returns :macro:`SF_ERR_PARSE_ERROR`, it encountered fatal error
  243. * while parsing field value.
  244. */
  245. int sf_parser_param(sf_parser *sfp, sf_vec *dest_key, sf_value *dest_value);
  246. /**
  247. * @function
  248. *
  249. * `sf_parser_dict` reads the next dictionary key and value pair. If
  250. * this function returns 0, it stores the key and value in |dest_key|
  251. * and |dest_value| respectively, if they are not NULL.
  252. *
  253. * Caller can optionally read parameters attached to the pair by
  254. * calling `sf_parser_param`.
  255. *
  256. * This function does no effort to find duplicated keys. Same key may
  257. * be reported more than once.
  258. *
  259. * Caller should keep calling this function until it returns negative
  260. * error code. If it returns :macro:`SF_ERR_EOF`, all key and value
  261. * pairs have been read, and there is nothing left to read.
  262. *
  263. * This function returns 0 if it succeeds, or one of the following
  264. * negative error codes:
  265. *
  266. * :macro:`SF_ERR_EOF`
  267. * All values in the dictionary have read.
  268. * :macro:`SF_ERR_PARSE_ERROR`
  269. * It encountered fatal error while parsing field value.
  270. */
  271. int sf_parser_dict(sf_parser *sfp, sf_vec *dest_key, sf_value *dest_value);
  272. /**
  273. * @function
  274. *
  275. * `sf_parser_list` reads the next list item. If this function
  276. * returns 0, it stores the item in |dest| if it is not NULL.
  277. *
  278. * Caller can optionally read parameters attached to the item by
  279. * calling `sf_parser_param`.
  280. *
  281. * Caller should keep calling this function until it returns negative
  282. * error code. If it returns :macro:`SF_ERR_EOF`, all values in the
  283. * list have been read, and there is nothing left to read.
  284. *
  285. * This function returns 0 if it succeeds, or one of the following
  286. * negative error codes:
  287. *
  288. * :macro:`SF_ERR_EOF`
  289. * All values in the list have read.
  290. * :macro:`SF_ERR_PARSE_ERROR`
  291. * It encountered fatal error while parsing field value.
  292. */
  293. int sf_parser_list(sf_parser *sfp, sf_value *dest);
  294. /**
  295. * @function
  296. *
  297. * `sf_parser_item` reads a single item. If this function returns 0,
  298. * it stores the item in |dest| if it is not NULL.
  299. *
  300. * This function is only used for the field value that consists of a
  301. * single item.
  302. *
  303. * Caller can optionally read parameters attached to the item by
  304. * calling `sf_parser_param`.
  305. *
  306. * Caller should call this function again to make sure that there is
  307. * nothing left to read. If this 2nd function call returns
  308. * :macro:`SF_ERR_EOF`, all data have been processed successfully.
  309. *
  310. * This function returns 0 if it succeeds, or one of the following
  311. * negative error codes:
  312. *
  313. * :macro:`SF_ERR_EOF`
  314. * There is nothing left to read.
  315. * :macro:`SF_ERR_PARSE_ERROR`
  316. * It encountered fatal error while parsing field value.
  317. */
  318. int sf_parser_item(sf_parser *sfp, sf_value *dest);
  319. /**
  320. * @function
  321. *
  322. * `sf_parser_inner_list` reads the next inner list item. If this
  323. * function returns 0, it stores the item in |dest| if it is not NULL.
  324. *
  325. * Caller can optionally read parameters attached to the item by
  326. * calling `sf_parser_param`.
  327. *
  328. * Caller should keep calling this function until it returns negative
  329. * error code. If it returns :macro:`SF_ERR_EOF`, all values in this
  330. * inner list have been read, and caller can optionally read
  331. * parameters attached to this inner list by calling
  332. * `sf_parser_param`. Then caller can continue to read rest of the
  333. * values.
  334. *
  335. * This function returns 0 if it succeeds, or one of the following
  336. * negative error codes:
  337. *
  338. * :macro:`SF_ERR_EOF`
  339. * All values in the inner list have read.
  340. * :macro:`SF_ERR_PARSE_ERROR`
  341. * It encountered fatal error while parsing field value.
  342. */
  343. int sf_parser_inner_list(sf_parser *sfp, sf_value *dest);
  344. /**
  345. * @function
  346. *
  347. * `sf_unescape` copies |src| to |dest| by removing escapes (``\``).
  348. * |src| should be the pointer to :member:`sf_value.vec` of type
  349. * :enum:`sf_type.SF_TYPE_STRING` produced by either `sf_parser_dict`,
  350. * `sf_parser_list`, `sf_parser_inner_list`, `sf_parser_item`, or
  351. * `sf_parser_param`, otherwise the behavior is undefined.
  352. *
  353. * :member:`dest->base <sf_vec.base>` must point to the buffer that
  354. * has sufficient space to store the unescaped string.
  355. *
  356. * If there is no escape character in |src|, |*src| is assigned to
  357. * |*dest|. This includes the case that :member:`src->len
  358. * <sf_vec.len>` == 0.
  359. *
  360. * This function sets the length of unescaped string to
  361. * :member:`dest->len <sf_vec.len>`.
  362. */
  363. void sf_unescape(sf_vec *dest, const sf_vec *src);
  364. /**
  365. * @function
  366. *
  367. * `sf_base64decode` decodes Base64 encoded string |src| and writes
  368. * the result into |dest|. |src| should be the pointer to
  369. * :member:`sf_value.vec` of type :enum:`sf_type.SF_TYPE_BYTESEQ`
  370. * produced by either `sf_parser_dict`, `sf_parser_list`,
  371. * `sf_parser_inner_list`, `sf_parser_item`, or `sf_parser_param`,
  372. * otherwise the behavior is undefined.
  373. *
  374. * :member:`dest->base <sf_vec.base>` must point to the buffer that
  375. * has sufficient space to store the decoded byte string.
  376. *
  377. * If :member:`src->len <sf_vec.len>` == 0, |*src| is assigned to
  378. * |*dest|.
  379. *
  380. * This function sets the length of decoded byte string to
  381. * :member:`dest->len <sf_vec.len>`.
  382. */
  383. void sf_base64decode(sf_vec *dest, const sf_vec *src);
  384. #ifdef __cplusplus
  385. }
  386. #endif
  387. #endif /* SFPARSE_H */