apr_encode.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * @file apr_encode.h
  18. * @brief APR-UTIL Encoding
  19. */
  20. #ifndef APR_ENCODE_H
  21. #define APR_ENCODE_H
  22. #include "apr.h"
  23. #include "apr_general.h"
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /**
  28. * @defgroup APR_Util_Encode Base64/Base64Url/Base32/Base32Hex/Base16 Encoding
  29. * @ingroup APR_Util
  30. * @{
  31. */
  32. /**
  33. * RFC4648 and RFC7515 compliant BASE64, BASE64URL, BASE32, BASE32HEX
  34. * and BASE16 encode/decode functions.
  35. *
  36. * The following encodings are supported:
  37. *
  38. * - Base 64 Encoding
  39. *
  40. * o Use flag APR_ENCODE_NONE
  41. * o https://tools.ietf.org/html/rfc4648#section-4
  42. *
  43. * - Base 64 Encoding with URL and Filename Safe Alphabet
  44. *
  45. * o Use flag APR_ENCODE_URL
  46. * o https://tools.ietf.org/html/rfc4648#section-5
  47. *
  48. * - Base 64 URL Encoding without Padding
  49. *
  50. * o Use flag APR_ENCODE_BASE64URL
  51. * o https://tools.ietf.org/html/rfc7515#appendix-C
  52. *
  53. * - Base 32 Encoding
  54. *
  55. * o Use flag APR_ENCODE_NONE
  56. * o https://tools.ietf.org/html/rfc4648#section-6
  57. *
  58. * - Base 32 Encoding with Extended Hex Alphabet
  59. *
  60. * o Use flag APR_ENCODE_BASE32HEX
  61. * o https://tools.ietf.org/html/rfc4648#section-7
  62. *
  63. * - Base 16 Encoding
  64. *
  65. * o Use flags APR_ENCODE_NONE/APR_ENCODE_COLON
  66. * o https://tools.ietf.org/html/rfc4648#section-8
  67. *
  68. * If a non valid character of any kind including whitespace is passed to any
  69. * of the decoder functions, APR_BADCH will be returned. In this case decoding
  70. * will still take place, but the results can not be trusted.
  71. *
  72. * If APR_ENCODE_RELAXED is passed to the decoder functions, decoding will be
  73. * attempted up until the first non valid character. If this results in an
  74. * invalid state in the decoder, such as but not limited to an odd number of
  75. * base16 characters, APR_BADCH will still be returned.
  76. *
  77. * If APR_ENCODE_RELAXED is not passed to a decoder function, the decoding will
  78. * be done in constant time regardless of whether the result returns APR_SUCCESS
  79. * or APR_BADCH.
  80. *
  81. * If the dest parameter is NULL, the maximum theoretical buffer size is
  82. * returned in the len field, including space for a terminating zero character
  83. * if the destination is a string. This value can be used to allocate buffers
  84. * of a suitable safe size.
  85. *
  86. * If the dest parameter is provided, the encoding or decoding will take place,
  87. * and the actual number of characters written is returned in the len field,
  88. * ignoring any terminating zero.
  89. *
  90. * Plain strings are not assumed '\0' terminated unless APR_ENCODE_STRING is
  91. * provided.
  92. *
  93. */
  94. /**
  95. * When passing a string to one of the encode functions, this value can be
  96. * passed to indicate a string-valued key, and have the length computed
  97. * automatically.
  98. */
  99. #define APR_ENCODE_STRING (-1)
  100. /**
  101. * Generate RFC4648 base16/base32/base64.
  102. */
  103. #define APR_ENCODE_NONE 0
  104. /**
  105. * If relaxed, decode up until the first non base16/base32/base64 character.
  106. */
  107. #define APR_ENCODE_RELAXED 1
  108. /**
  109. * Omit the padding character (=) while encoding.
  110. */
  111. #define APR_ENCODE_NOPADDING 2
  112. /**
  113. * Generate RFC4648 Base 64 Encoding with URL and Filename Safe Alphabet
  114. */
  115. #define APR_ENCODE_URL 4
  116. /**
  117. * Generate RFC7515 BASE64URL
  118. */
  119. #define APR_ENCODE_BASE64URL (APR_ENCODE_NOPADDING | APR_ENCODE_URL)
  120. /**
  121. * Generate base32hex encoding instead of base32 encoding
  122. */
  123. #define APR_ENCODE_BASE32HEX 8
  124. /**
  125. * Generate base16 with colons between each token.
  126. */
  127. #define APR_ENCODE_COLON 16
  128. /**
  129. * Generate base16 with lower case characters.
  130. */
  131. #define APR_ENCODE_LOWER 32
  132. /**
  133. * Convert text data to base64.
  134. * @param dest The destination string, can be NULL to output in \c len the
  135. * needed buffer length for encoding.
  136. * @param src The original string, can be NULL if \c dest is NULL and \c slen
  137. * is positive or nul.
  138. * @param slen The length of the original string, or APR_ENCODE_STRING if
  139. * the actual length should be computed based on NUL termination.
  140. * @param flags If APR_ENCODE_NONE, emit RFC4648 Base 64 Encoding. If
  141. * APR_ENCODE_NOPADDING, omit the = padding character. If APR_ENCODE_URL,
  142. * use RFC4648 Base 64 Encoding with URL and Filename Safe Alphabet.
  143. * If APR_ENCODE_BASE64URL, use RFC7515 base64url Encoding.
  144. * @param len If not NULL, outputs the length of the buffer needed for encoding
  145. * (including the trailing NUL) if \c dest is NULL, or the actual length of
  146. * the encoding (excluding the trailing NUL) if \c dest is not NULL.
  147. * @return APR_SUCCESS, or APR_EINVAL if \c slen is not APR_ENCODE_STRING and
  148. * negative, or APR_NOTFOUND if \c dest is not NULL and \c src is NULL, or
  149. * APR_ENOSPC if \c dest is NULL and the source length (based on \c slen or
  150. * APR_ENCODE_STRING) is too big to encode.
  151. */
  152. APR_DECLARE(apr_status_t) apr_encode_base64(char *dest, const char *src,
  153. apr_ssize_t slen, int flags, apr_size_t * len);
  154. /**
  155. * Convert binary data to base64.
  156. * @param dest The destination string, can be NULL to output in \c len the
  157. * needed buffer length for encoding.
  158. * @param src The original buffer, can be NULL if \c dest is NULL.
  159. * @param slen The length of the original buffer.
  160. * @param flags If APR_ENCODE_NONE, emit RFC4648 Base 64 Encoding. If
  161. * APR_ENCODE_NOPADDING, omit the = padding character. If APR_ENCODE_URL,
  162. * use RFC4648 Base 64 Encoding with URL and Filename Safe Alphabet.
  163. * If APR_ENCODE_BASE64URL, use RFC7515 base64url Encoding.
  164. * @param len If not NULL, outputs the length of the buffer needed for encoding
  165. * (including the trailing NUL) if \c dest is NULL, or the actual length of
  166. * the encoding (excluding the trailing NUL) if \c dest is not NULL.
  167. * @return APR_SUCCESS, or APR_EINVAL if \c slen is negative, or APR_NOTFOUND
  168. * if \c dest is not NULL and \c src is NULL, or APR_ENOSPC if \c dest is NULL
  169. * and the source length (based on \c slen or APR_ENCODE_STRING) is too big to
  170. * encode.
  171. */
  172. APR_DECLARE(apr_status_t) apr_encode_base64_binary(char *dest, const unsigned char *src,
  173. apr_ssize_t slen, int flags, apr_size_t * len);
  174. /**
  175. * Convert text data to base64, and return the results from a pool.
  176. * @param p Pool to allocate from.
  177. * @param src The original string.
  178. * @param slen The length of the original string, or APR_ENCODE_STRING if
  179. * the actual length should be computed based on NUL termination.
  180. * @param flags If APR_ENCODE_NONE, emit RFC4648 Base 64 Encoding. If
  181. * APR_ENCODE_NOPADDING, omit the = padding character. If APR_ENCODE_URL,
  182. * use RFC4648 Base 64 Encoding with URL and Filename Safe Alphabet.
  183. * If APR_ENCODE_BASE64URL, use RFC7515 base64url Encoding.
  184. * @param len If not NULL, outputs the length of the encoding (excluding the
  185. * trailing NUL).
  186. * @return A NUL terminated string allocated from the pool on success,
  187. * or NULL if src is NULL or allocation failed or the encoding is not
  188. * possible (see apr_encode_base64 errors).
  189. */
  190. APR_DECLARE(const char *)apr_pencode_base64(apr_pool_t * p, const char *src,
  191. apr_ssize_t slen, int flags, apr_size_t * len)__attribute__((nonnull(1)));
  192. /**
  193. * Convert binary data to base64, and return the results from a pool.
  194. * @param p Pool to allocate from.
  195. * @param src The original buffer.
  196. * @param slen The length of the original buffer.
  197. * @param flags If APR_ENCODE_NONE, emit RFC4648 Base 64 Encoding. If
  198. * APR_ENCODE_NOPADDING, omit the = padding character. If APR_ENCODE_URL,
  199. * use RFC4648 Base 64 Encoding with URL and Filename Safe Alphabet.
  200. * If APR_ENCODE_BASE64URL, use RFC7515 base64url Encoding.
  201. * @param len If not NULL, outputs the length of the encoding (excluding the
  202. * trailing NUL).
  203. * @return A NUL terminated string allocated from the pool on success,
  204. * or NULL if src is NULL or allocation failed or the encoding is not
  205. * possible (see apr_encode_base64_binary errors).
  206. */
  207. APR_DECLARE(const char *)apr_pencode_base64_binary(apr_pool_t * p, const unsigned char *src,
  208. apr_ssize_t slen, int flags, apr_size_t * len)__attribute__((nonnull(1)));
  209. /**
  210. * Convert base64 or base64url with or without padding to text data.
  211. * @param dest The destination string, can be NULL to output in \c len the
  212. * needed buffer length for decoding.
  213. * @param src The base64 string, can be NULL if \c dest is NULL and \c slen
  214. * is positive or nul.
  215. * @param slen The length of the base64 string, or APR_ENCODE_STRING if
  216. * the actual length should be computed based on NUL termination.
  217. * @param flags If APR_ENCODE_NONE, attempt to decode the full base64 string,
  218. * and return NULL if any bad character is detected. If APR_ENCODE_RELAXED,
  219. * decode until the first non base64/base64url character.
  220. * @param len If not NULL, outputs the length of the buffer needed for decoding
  221. * (including the trailing NUL) if \c dest is NULL, or the actual length of
  222. * the decoding (excluding the trailing NUL) if \c dest is not NULL.
  223. * @return APR_SUCCESS, or APR_EINVAL if \c slen is not APR_ENCODE_STRING and
  224. * negative, or APR_NOTFOUND if \c dest is not NULL and \c src is NULL, or
  225. * APR_ENOSPC if \c dest is NULL and the source length (based on \c slen or
  226. * APR_ENCODE_STRING) is too big to decode, or APR_EINCOMPLETE if the source
  227. * length (based on \c slen or APR_ENCODE_STRING) is invalid for a base64
  228. * encoding, or APR_BADCH if a non base64 character is present and
  229. * APR_ENCODE_RELAXED is not specified.
  230. */
  231. APR_DECLARE(apr_status_t) apr_decode_base64(char *dest, const char *src,
  232. apr_ssize_t slen, int flags, apr_size_t * len);
  233. /**
  234. * Convert base64 or base64url with or without padding to binary data.
  235. * @param dest The destination string, can be NULL to output in \c len the
  236. * needed buffer length for decoding.
  237. * @param src The base64 string, can be NULL if \c dest is NULL and \c slen
  238. * is positive or nul.
  239. * @param slen The length of the base64 string, or APR_ENCODE_STRING if
  240. * the actual length should be computed based on NUL termination.
  241. * @param flags If APR_ENCODE_NONE, attempt to decode the full base64 string,
  242. * and return NULL if any bad character is detected. If APR_ENCODE_RELAXED,
  243. * decode until the first non base64/base64url character.
  244. * @param len If not NULL, outputs the length of the buffer needed for decoding
  245. * (including the trailing NUL) if \c dest is NULL, or the actual length of
  246. * the decoding (excluding the trailing NUL) if \c dest is not NULL.
  247. * @return APR_SUCCESS, or APR_EINVAL if \c slen is not APR_ENCODE_STRING and
  248. * negative, or APR_NOTFOUND if \c dest is not NULL and \c src is NULL, or
  249. * APR_ENOSPC if \c dest is NULL and the source length (based on \c slen or
  250. * APR_ENCODE_STRING) is too big to decode, or APR_EINCOMPLETE if the source
  251. * length (based on \c slen or APR_ENCODE_STRING) is invalid for a base64
  252. * encoding, or APR_BADCH if a non base64 character is present and
  253. * APR_ENCODE_RELAXED is not specified.
  254. */
  255. APR_DECLARE(apr_status_t) apr_decode_base64_binary(unsigned char *dest,
  256. const char *src, apr_ssize_t slen, int flags, apr_size_t * len);
  257. /**
  258. * Convert base64 or base64url with or without padding to text data, and
  259. * return the results from a pool.
  260. * @param p Pool to allocate from.
  261. * @param src The base64 string to decode.
  262. * @param slen The length of the original string, or APR_ENCODE_STRING if
  263. * the actual length should be computed based on NUL termination.
  264. * @param flags If APR_ENCODE_NONE, attempt to decode the full original buffer,
  265. * and return NULL if any bad character is detected. If APR_ENCODE_RELAXED,
  266. * decode until the first non base64/base64url character.
  267. * @param len If not NULL, outputs the length of the decoding (excluding the
  268. * trailing NUL).
  269. * @return A NUL terminated string allocated from the pool on success,
  270. * or NULL if src is NULL or allocation failed or the decoding is not
  271. * possible (see apr_decode_base64_binary errors).
  272. */
  273. APR_DECLARE(const char *)apr_pdecode_base64(apr_pool_t * p, const char *src,
  274. apr_ssize_t slen, int flags, apr_size_t * len)
  275. __attribute__((nonnull(1)));
  276. /**
  277. * Convert base64 or base64url with or without padding to binary data, and
  278. * return the results from a pool.
  279. * @param p Pool to allocate from.
  280. * @param src The base64 string to decode.
  281. * @param slen The length of the original string, or APR_ENCODE_STRING if
  282. * the actual length should be computed based on NUL termination.
  283. * @param flags If APR_ENCODE_NONE, attempt to decode the full original buffer,
  284. * and return NULL if any bad character is detected. If APR_ENCODE_RELAXED,
  285. * decode until the first non base64/base64url character.
  286. * @param len If not NULL, outputs the length of the decoding (excluding the
  287. * trailing NUL).
  288. * @return A NUL terminated string allocated from the pool on success,
  289. * or NULL if src is NULL or allocation failed or the decoding is not
  290. * possible (see apr_decode_base64_binary errors).
  291. */
  292. APR_DECLARE(const unsigned char *)apr_pdecode_base64_binary(apr_pool_t * p,
  293. const char *src, apr_ssize_t slen, int flags, apr_size_t * len)
  294. __attribute__((nonnull(1)));
  295. /**
  296. * Convert text data to base32.
  297. * @param dest The destination string, can be NULL to output in \c len the
  298. * needed buffer length for encoding.
  299. * @param src The original string, can be NULL if \c dest is NULL and \c slen
  300. * is positive or nul.
  301. * @param slen The length of the original string, or APR_ENCODE_STRING if
  302. * the actual length should be computed based on NUL termination.
  303. * @param flags If APR_ENCODE_NONE, emit RFC4648 Base 32 Encoding. If
  304. * APR_ENCODE_NOPADDING, omit the = padding character. If APR_ENCODE_BASE32HEX,
  305. * use RFC4648 base32hex Encoding.
  306. * @param len If not NULL, outputs the length of the buffer needed for encoding
  307. * (including the trailing NUL) if \c dest is NULL, or the actual length of
  308. * the encoding (excluding the trailing NUL) if \c dest is not NULL.
  309. * @return APR_SUCCESS, or APR_EINVAL if \c slen is not APR_ENCODE_STRING and
  310. * negative, or APR_NOTFOUND if \c dest is not NULL and \c src is NULL, or
  311. * APR_ENOSPC if \c dest is NULL and the source length (based on \c slen or
  312. * APR_ENCODE_STRING) is too big to encode.
  313. */
  314. APR_DECLARE(apr_status_t) apr_encode_base32(char *dest, const char *src,
  315. apr_ssize_t slen, int flags, apr_size_t * len);
  316. /**
  317. * Convert binary data to base32.
  318. * @param dest The destination string, can be NULL to output in \c len the
  319. * needed buffer length for encoding.
  320. * @param src The original buffer, can be NULL if \c dest is NULL.
  321. * @param slen The length of the original buffer.
  322. * @param flags If APR_ENCODE_NONE, emit RFC4648 Base 32 Encoding. If
  323. * APR_ENCODE_NOPADDING, omit the = padding character. If APR_ENCODE_BASE32HEX,
  324. * use RFC4648 base32hex Encoding.
  325. * @param len If not NULL, outputs the length of the buffer needed for encoding
  326. * (including the trailing NUL) if \c dest is NULL, or the actual length of
  327. * the encoding (excluding the trailing NUL) if \c dest is not NULL.
  328. * @return APR_SUCCESS, or APR_EINVAL if \c slen is negative, or APR_NOTFOUND
  329. * if \c dest is not NULL and \c src is NULL, or APR_ENOSPC if \c dest is NULL
  330. * and the source length (based on \c slen or APR_ENCODE_STRING) is too big to
  331. * encode.
  332. */
  333. APR_DECLARE(apr_status_t) apr_encode_base32_binary(char *dest, const unsigned char *src,
  334. apr_ssize_t slen, int flags, apr_size_t * len);
  335. /**
  336. * Convert text data to base32, and return the results from a pool.
  337. * @param p Pool to allocate from.
  338. * @param src The original string.
  339. * @param slen The length of the original string, or APR_ENCODE_STRING if
  340. * the actual length should be computed based on NUL termination.
  341. * @param flags If APR_ENCODE_NONE, emit RFC4648 Base 32 Encoding. If
  342. * APR_ENCODE_NOPADDING, omit the = padding character. If APR_ENCODE_BASE32HEX,
  343. * use RFC4648 base32hex Encoding.
  344. * @param len If not NULL, outputs the length of the encoding (excluding the
  345. * trailing NUL).
  346. * @return A NUL terminated string allocated from the pool on success,
  347. * or NULL if src is NULL or allocation failed or the encoding is not
  348. * possible (see apr_encode_base32 errors).
  349. */
  350. APR_DECLARE(const char *)apr_pencode_base32(apr_pool_t * p, const char *src,
  351. apr_ssize_t slen, int flags, apr_size_t * len)
  352. __attribute__((nonnull(1)));
  353. /**
  354. * Convert binary data to base32, and return the results from a pool.
  355. * @param p Pool to allocate from.
  356. * @param src The original buffer.
  357. * @param slen The length of the original buffer.
  358. * @param flags If APR_ENCODE_NONE, emit RFC4648 Base 32 Encoding. If
  359. * APR_ENCODE_NOPADDING, omit the = padding character. If APR_ENCODE_BASE32HEX,
  360. * use RFC4648 base32hex Encoding.
  361. * @param len If not NULL, outputs the length of the encoding (excluding the
  362. * trailing NUL).
  363. * @return A NUL terminated string allocated from the pool on success,
  364. * or NULL if src is NULL or allocation failed or the encoding is not
  365. * possible (see apr_encode_base32_binary errors).
  366. */
  367. APR_DECLARE(const char *)apr_pencode_base32_binary(apr_pool_t * p, const unsigned char *src,
  368. apr_ssize_t slen, int flags, apr_size_t * len)
  369. __attribute__((nonnull(1)));
  370. /**
  371. * Convert base32 or base32hex with or without padding to text data.
  372. * @param dest The destination string, can be NULL to output in \c len the
  373. * needed buffer length for decoding.
  374. * @param src The base32 string, can be NULL if \c dest is NULL and \c slen
  375. * is positive or nul.
  376. * @param slen The length of the base32 string, or APR_ENCODE_STRING if
  377. * the actual length should be computed based on NUL termination.
  378. * @param flags If APR_ENCODE_NONE, parse RFC4648 Base 32 Encoding. If
  379. * APR_ENCODE_BASE32HEX, use RFC4648 base32hex Encoding.
  380. * @param len If not NULL, outputs the length of the buffer needed for decoding
  381. * (including the trailing NUL) if \c dest is NULL, or the actual length of
  382. * the decoding (excluding the trailing NUL) if \c dest is not NULL.
  383. * @return APR_SUCCESS, or APR_EINVAL if \c slen is not APR_ENCODE_STRING and
  384. * negative, or APR_NOTFOUND if \c dest is not NULL and \c src is NULL, or
  385. * APR_ENOSPC if \c dest is NULL and the source length (based on \c slen or
  386. * APR_ENCODE_STRING) is too big to decode, or APR_EINCOMPLETE if the source
  387. * length (based on \c slen or APR_ENCODE_STRING) is invalid for a base32
  388. * encoding, or APR_BADCH if a non base32 character is present and
  389. * APR_ENCODE_RELAXED is not specified.
  390. */
  391. APR_DECLARE(apr_status_t) apr_decode_base32(char *dest, const char *src,
  392. apr_ssize_t slen, int flags, apr_size_t * len);
  393. /**
  394. * Convert base32 or base32hex with or without padding to binary data.
  395. * @param dest The destination string, can be NULL to output in \c len the
  396. * needed buffer length for decoding.
  397. * @param src The base32 string, can be NULL if \c dest is NULL and \c slen
  398. * is positive or nul.
  399. * @param slen The length of the base32 string, or APR_ENCODE_STRING if
  400. * the actual length should be computed based on NUL termination.
  401. * @param flags If APR_ENCODE_NONE, parse RFC4648 Base 32 Encoding. If
  402. * APR_ENCODE_BASE32HEX, use RFC4648 base32hex Encoding.
  403. * @param len If not NULL, outputs the length of the buffer needed for decoding
  404. * (including the trailing NUL) if \c dest is NULL, or the actual length of
  405. * the decoding (excluding the trailing NUL) if \c dest is not NULL.
  406. * @return APR_SUCCESS, or APR_EINVAL if \c slen is not APR_ENCODE_STRING and
  407. * negative, or APR_NOTFOUND if \c dest is not NULL and \c src is NULL, or
  408. * APR_ENOSPC if \c dest is NULL and the source length (based on \c slen or
  409. * APR_ENCODE_STRING) is too big to decode, or APR_EINCOMPLETE if the source
  410. * length (based on \c slen or APR_ENCODE_STRING) is invalid for a base32
  411. * encoding, or APR_BADCH if a non base32 character is present and
  412. * APR_ENCODE_RELAXED is not specified.
  413. */
  414. APR_DECLARE(apr_status_t) apr_decode_base32_binary(unsigned char *dest,
  415. const char *src, apr_ssize_t slen, int flags, apr_size_t * len);
  416. /**
  417. * Convert base32 or base32hex with or without padding to text data, and
  418. * return the results from a pool.
  419. * @param p Pool to allocate from.
  420. * @param src The base32 string to decode.
  421. * @param slen The length of the original string, or APR_ENCODE_STRING if
  422. * the actual length should be computed based on NUL termination.
  423. * @param flags If APR_ENCODE_NONE, parse RFC4648 Base 32 Encoding. If
  424. * APR_ENCODE_BASE32HEX, use RFC4648 base32hex Encoding.
  425. * @param len If not NULL, outputs the length of the encoding (excluding the
  426. * trailing NUL).
  427. * @return A NUL terminated string allocated from the pool on success,
  428. * or NULL if src is NULL or allocation failed or the decoding is not
  429. * possible (see apr_decode_base32 errors).
  430. */
  431. APR_DECLARE(const char *)apr_pdecode_base32(apr_pool_t * p, const char *src,
  432. apr_ssize_t slen, int flags, apr_size_t * len)
  433. __attribute__((nonnull(1)));
  434. /**
  435. * Convert base32 or base32hex with or without padding to binary data, and
  436. * return the results from a pool.
  437. * @param p Pool to allocate from.
  438. * @param src The base32 string to decode.
  439. * @param slen The length of the original string, or APR_ENCODE_STRING if
  440. * the actual length should be computed based on NUL termination.
  441. * @param flags If APR_ENCODE_NONE, parse RFC4648 Base 32 Encoding. If
  442. * APR_ENCODE_BASE32HEX, use RFC4648 base32hex Encoding.
  443. * @param len If not NULL, outputs the length of the encoding (excluding the
  444. * trailing NUL).
  445. * @return A NUL terminated string allocated from the pool on success,
  446. * or NULL if src is NULL or allocation failed or the decoding is not
  447. * possible (see apr_decode_base32_binary errors).
  448. */
  449. APR_DECLARE(const unsigned char *)apr_pdecode_base32_binary(apr_pool_t * p,
  450. const char *src, apr_ssize_t slen, int flags, apr_size_t * len)
  451. __attribute__((nonnull(1)));
  452. /**
  453. * Convert text data to base16 (hex).
  454. * @param dest The destination string, can be NULL to output in \c len the
  455. * needed buffer length for encoding.
  456. * @param src The original string, can be NULL if \c dest is NULL and \c slen
  457. * is positive or nul.
  458. * @param slen The length of the original string, or APR_ENCODE_STRING if
  459. * the actual length should be computed based on NUL termination.
  460. * @param flags If APR_ENCODE_NONE, emit RFC4648 Base 16 Encoding. If
  461. * APR_ENCODE_COLON, separate each token with a colon.
  462. * @param len If not NULL, outputs the length of the buffer needed for encoding
  463. * (including the trailing NUL) if \c dest is NULL, or the actual length of
  464. * the encoding (excluding the trailing NUL) if \c dest is not NULL.
  465. * @return APR_SUCCESS, or APR_EINVAL if \c slen is not APR_ENCODE_STRING and
  466. * negative, or APR_NOTFOUND if \c dest is not NULL and \c src is NULL, or
  467. * APR_ENOSPC if \c dest is NULL and the source length (based on \c slen or
  468. * APR_ENCODE_STRING) is too big to encode.
  469. */
  470. APR_DECLARE(apr_status_t) apr_encode_base16(char *dest, const char *src,
  471. apr_ssize_t slen, int flags, apr_size_t * len);
  472. /**
  473. * Convert binary data to base16 (hex).
  474. * @param dest The destination string, can be NULL to output in \c len the
  475. * needed buffer length for encoding.
  476. * @param src The original buffer, can be NULL if \c dest is NULL.
  477. * @param slen The length of the original buffer.
  478. * @param flags If APR_ENCODE_NONE, emit RFC4648 Base 16 Encoding. If
  479. * APR_ENCODE_COLON, separate each token with a colon.
  480. * @param len If not NULL, outputs the length of the buffer needed for encoding
  481. * (including the trailing NUL) if \c dest is NULL, or the actual length of
  482. * the encoding (excluding the trailing NUL) if \c dest is not NULL.
  483. * @return APR_SUCCESS, or APR_EINVAL if \c slen is negative, or APR_NOTFOUND
  484. * if \c dest is not NULL and \c src is NULL, or APR_ENOSPC if \c dest is NULL
  485. * and the source length (based on \c slen or APR_ENCODE_STRING) is too big to
  486. * encode.
  487. */
  488. APR_DECLARE(apr_status_t) apr_encode_base16_binary(char *dest,
  489. const unsigned char *src, apr_ssize_t slen, int flags,
  490. apr_size_t * len);
  491. /**
  492. * Convert text data to base16 (hex), and return the results from a
  493. * pool.
  494. * @param p Pool to allocate from.
  495. * @param src The original string.
  496. * @param slen The length of the original string, or APR_ENCODE_STRING if
  497. * the actual length should be computed based on NUL termination.
  498. * @param flags If APR_ENCODE_NONE, emit RFC4648 Base 16 Encoding. If
  499. * APR_ENCODE_COLON, separate each token with a colon.
  500. * @param len If not NULL, outputs the length of the encoding (excluding the
  501. * trailing NUL).
  502. * @return A NUL terminated string allocated from the pool on success,
  503. * or NULL if src is NULL or allocation failed or the encoding is not
  504. * possible (see apr_encode_base16 errors).
  505. */
  506. APR_DECLARE(const char *)apr_pencode_base16(apr_pool_t * p, const char *src,
  507. apr_ssize_t slen, int flags, apr_size_t * len)
  508. __attribute__((nonnull(1)));
  509. /**
  510. * Convert binary data to base16 (hex), and return the results from a
  511. * pool.
  512. * @param p Pool to allocate from.
  513. * @param src The original buffer.
  514. * @param slen The length of the original buffer.
  515. * @param flags If APR_ENCODE_NONE, emit RFC4648 Base 16 Encoding. If
  516. * APR_ENCODE_COLON, separate each token with a colon.
  517. * @param len If not NULL, outputs the length of the encoding (excluding the
  518. * trailing NUL).
  519. * @return A NUL terminated string allocated from the pool on success,
  520. * or NULL if src is NULL or allocation failed or the encoding is not
  521. * possible (see apr_encode_base16_binary errors).
  522. */
  523. APR_DECLARE(const char *)apr_pencode_base16_binary(apr_pool_t * p,
  524. const unsigned char *src, apr_ssize_t slen,
  525. int flags, apr_size_t * len)__attribute__((nonnull(1)));
  526. /**
  527. * Convert base16 (hex) to text data.
  528. * @param dest The destination string, can be NULL to output in \c len the
  529. * needed buffer length for decoding.
  530. * @param src The base16 string, can be NULL if \c dest is NULL and \c slen
  531. * is positive or nul.
  532. * @param slen The length of the base16 string, or APR_ENCODE_STRING if
  533. * the actual length should be computed based on NUL termination.
  534. * @param flags If APR_ENCODE_NONE, parse RFC4648 Base 16 Encoding. If
  535. * APR_ENCODE_COLON, allow tokens to be separated with a colon.
  536. * @param len If not NULL, outputs the length of the buffer needed for decoding
  537. * (including the trailing NUL) if \c dest is NULL, or the actual length of
  538. * the decoding (excluding the trailing NUL) if \c dest is not NULL.
  539. * @return APR_SUCCESS, or APR_EINVAL if \c slen is not APR_ENCODE_STRING and
  540. * negative, or APR_NOTFOUND if \c dest is not NULL and \c src is NULL, or
  541. * APR_ENOSPC if \c dest is NULL and the source length (based on \c slen or
  542. * APR_ENCODE_STRING) is too big to decode, or APR_EINCOMPLETE if the source
  543. * length (based on \c slen or APR_ENCODE_STRING) is invalid for a base16
  544. * encoding, or APR_BADCH if a non base16 character is present and
  545. * APR_ENCODE_RELAXED is not specified.
  546. */
  547. APR_DECLARE(apr_status_t) apr_decode_base16(char *dest, const char *src,
  548. apr_ssize_t slen, int flags, apr_size_t * len);
  549. /**
  550. * Convert base16 (hex) to binary data.
  551. * @param dest The destination string, can be NULL to output in \c len the
  552. * needed buffer length for decoding.
  553. * @param src The base16 string, can be NULL if \c dest is NULL and \c slen
  554. * is positive or nul.
  555. * @param slen The length of the base16 string, or APR_ENCODE_STRING if
  556. * the actual length should be computed based on NUL termination.
  557. * @param flags If APR_ENCODE_NONE, parse RFC4648 Base 16 Encoding. If
  558. * APR_ENCODE_COLON, allow tokens to be separated with a colon.
  559. * @param len If not NULL, outputs the length of the buffer needed for decoding
  560. * (including the trailing NUL) if \c dest is NULL, or the actual length of
  561. * the decoding (excluding the trailing NUL) if \c dest is not NULL.
  562. * @return APR_SUCCESS, or APR_EINVAL if \c slen is not APR_ENCODE_STRING and
  563. * negative, or APR_NOTFOUND if \c dest is not NULL and \c src is NULL, or
  564. * APR_ENOSPC if \c dest is NULL and the source length (based on \c slen or
  565. * APR_ENCODE_STRING) is too big to decode, or APR_EINCOMPLETE if the source
  566. * length (based on \c slen or APR_ENCODE_STRING) is invalid for a base16
  567. * encoding, or APR_BADCH if a non base16 character is present and
  568. * APR_ENCODE_RELAXED is not specified.
  569. */
  570. APR_DECLARE(apr_status_t) apr_decode_base16_binary(unsigned char *dest,
  571. const char *src, apr_ssize_t slen, int flags, apr_size_t * len);
  572. /**
  573. * Convert base16 (hex) and return the results from a pool.
  574. * @param p Pool to allocate from.
  575. * @param src The base16 string to decode.
  576. * @param slen The length of the original string, or APR_ENCODE_STRING if
  577. * the actual length should be computed based on NUL termination.
  578. * @param flags If APR_ENCODE_NONE, parse RFC4648 Base 16 Encoding. If
  579. * APR_ENCODE_COLON, allow tokens to be separated with a colon.
  580. * @param len If not NULL, outputs the length of the encoding (excluding the
  581. * trailing NUL).
  582. * @return A NUL terminated string allocated from the pool on success,
  583. * or NULL if src is NULL or allocation failed or the decoding is not
  584. * possible (see apr_decode_base16 errors).
  585. */
  586. APR_DECLARE(const char *)apr_pdecode_base16(apr_pool_t * p, const char *src,
  587. apr_ssize_t slen, int flags, apr_size_t * len)
  588. __attribute__((nonnull(1)));
  589. /**
  590. * Convert base16 (hex) to binary data, and return the results from a pool.
  591. * @param p Pool to allocate from.
  592. * @param src The base16 string to decode.
  593. * @param slen The length of the original string, or APR_ENCODE_STRING if
  594. * the actual length should be computed based on NUL termination.
  595. * @param flags If APR_ENCODE_NONE, parse RFC4648 Base 16 Encoding. If
  596. * APR_ENCODE_COLON, allow tokens to be separated with a colon.
  597. * @param len If not NULL, outputs the length of the encoding (excluding the
  598. * trailing NUL).
  599. * @return A NUL terminated string allocated from the pool on success,
  600. * or NULL if src is NULL or allocation failed or the decoding is not
  601. * possible (see apr_decode_base16_binary errors).
  602. */
  603. APR_DECLARE(const unsigned char *)apr_pdecode_base16_binary(apr_pool_t * p,
  604. const char *src, apr_ssize_t slen, int flags, apr_size_t * len)
  605. __attribute__((nonnull(1)));
  606. /** @} */
  607. #ifdef __cplusplus
  608. }
  609. #endif
  610. #endif /* !APR_ENCODE_H */