1
0

b64.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /* /////////////////////////////////////////////////////////////////////////////
  2. * File: b64/b64.h
  3. *
  4. * Purpose: Header file for the b64 library
  5. *
  6. * Created: 18th October 2004
  7. * Updated: 15th March 2008
  8. *
  9. * Thanks: To Adam McLaurin, for ideas regarding the b64_decode2() and
  10. * b64_encode2().
  11. *
  12. * Home: http://synesis.com.au/software/
  13. *
  14. * Copyright (c) 2004-2008, Matthew Wilson and Synesis Software
  15. * All rights reserved.
  16. *
  17. * Redistribution and use in source and binary forms, with or without
  18. * modification, are permitted provided that the following conditions are met:
  19. *
  20. * - Redistributions of source code must retain the above copyright notice, this
  21. * list of conditions and the following disclaimer.
  22. * - Redistributions in binary form must reproduce the above copyright notice,
  23. * this list of conditions and the following disclaimer in the documentation
  24. * and/or other materials provided with the distribution.
  25. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  26. * any contributors may be used to endorse or promote products derived from
  27. * this software without specific prior written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  30. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  31. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  32. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  33. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  34. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  35. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  36. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  37. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  38. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  39. * POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. * ////////////////////////////////////////////////////////////////////////// */
  42. /** \file b64/b64.h
  43. *
  44. * \brief [C/C++] Header file for the b64 library.
  45. */
  46. #ifndef B64_INCL_B64_H_B64
  47. #define B64_INCL_B64_H_B64
  48. /* /////////////////////////////////////////////////////////////////////////////
  49. * Version information
  50. */
  51. #ifndef B64_DOCUMENTATION_SKIP_SECTION
  52. # define B64_VER_B64_H_B64_MAJOR 1
  53. # define B64_VER_B64_H_B64_MINOR 5
  54. # define B64_VER_B64_H_B64_REVISION 1
  55. # define B64_VER_B64_H_B64_EDIT 25
  56. #endif /* !B64_DOCUMENTATION_SKIP_SECTION */
  57. /** \def B64_VER_MAJOR
  58. * The major version number of b64
  59. */
  60. /** \def B64_VER_MINOR
  61. * The minor version number of b64
  62. */
  63. /** \def B64_VER_REVISION
  64. * The revision version number of b64
  65. */
  66. /** \def B64_VER
  67. * The current composite version number of b64
  68. */
  69. #ifndef B64_DOCUMENTATION_SKIP_SECTION
  70. # define B64_VER_1_0_1 0x01000100
  71. # define B64_VER_1_0_2 0x01000200
  72. # define B64_VER_1_0_3 0x01000300
  73. # define B64_VER_1_1_1 0x01010100
  74. # define B64_VER_1_1_2 0x01010200
  75. # define B64_VER_1_1_3 0x01010300
  76. # define B64_VER_1_2_1 0x01020100
  77. # define B64_VER_1_2_2 0x01020200
  78. # define B64_VER_1_2_3 0x01020300
  79. # define B64_VER_1_2_4 0x01020400
  80. # define B64_VER_1_2_5 0x01020500
  81. # define B64_VER_1_2_6 0x01020600
  82. # define B64_VER_1_2_7 0x01020700
  83. # define B64_VER_1_3_1 0x010301ff
  84. # define B64_VER B64_VER_1_3_1
  85. #else /* ? B64_DOCUMENTATION_SKIP_SECTION */
  86. # define B64_VER 0x010301ff
  87. #endif /* !B64_DOCUMENTATION_SKIP_SECTION */
  88. #define B64_VER_MAJOR 1
  89. #define B64_VER_MINOR 3
  90. #define B64_VER_REVISION 1
  91. /* /////////////////////////////////////////////////////////////////////////////
  92. * Includes
  93. */
  94. #include <stddef.h>
  95. /* /////////////////////////////////////////////////////////////////////////////
  96. * Namespace
  97. */
  98. #if !defined(B64_NO_NAMESPACE) && \
  99. !defined(__cplusplus)
  100. # define B64_NO_NAMESPACE
  101. #endif /* !B64_NO_NAMESPACE && !__cplusplus */
  102. #ifdef B64_NAMESPACE
  103. # undef B64_NAMESPACE
  104. #endif /* B64_NAMESPACE */
  105. #ifdef B64_NAMESPACE_QUALIFIER
  106. # undef B64_NAMESPACE_QUALIFIER
  107. #endif /* B64_NAMESPACE_QUALIFIER */
  108. #ifndef B64_NO_NAMESPACE
  109. # ifdef B64_CUSTOM_NAMESPACE
  110. # define B64_NAMESPACE B64_CUSTOM_NAMESPACE
  111. # else /* ? B64_CUSTOM_NAMESPACE */
  112. # define B64_NAMESPACE b64
  113. # endif /* B64_CUSTOM_NAMESPACE */
  114. # if defined(B64_CUSTOM_NAMESPACE) && \
  115. defined(B64_CUSTOM_NAMESPACE_QUALIFIER)
  116. # define B64_NAMESPACE_QUALIFIER B64_CUSTOM_NAMESPACE_QUALIFIER
  117. # else /* B64_CUSTOM_NAMESPACE && B64_CUSTOM_NAMESPACE_QUALIFIER */
  118. # define B64_NAMESPACE_QUALIFIER ::B64_NAMESPACE
  119. # endif /* B64_CUSTOM_NAMESPACE && B64_CUSTOM_NAMESPACE_QUALIFIER */
  120. /** \brief [C/C++] The b64 namespace, within which the core library types and functions
  121. * reside in C++ compilation. In C compilation, they all reside in the global
  122. * namespace.
  123. *
  124. * \htmlonly
  125. * <hr>
  126. * \endhtmlonly
  127. */
  128. namespace B64_NAMESPACE
  129. {
  130. #endif /* !B64_NO_NAMESPACE */
  131. /* /////////////////////////////////////////////////////////////////////////////
  132. * Enumerations
  133. */
  134. /** \brief Return codes (from b64_encode2() / b64_decode2())
  135. */
  136. enum B64_RC
  137. {
  138. B64_RC_OK = 0 /*!< Operation was successful. */
  139. , B64_RC_INSUFFICIENT_BUFFER = 1 /*!< The given translation buffer was not of sufficient size. */
  140. , B64_RC_TRUNCATED_INPUT = 2 /*!< The input did not represent a fully formed stream of octet couplings. */
  141. , B64_RC_DATA_ERROR = 3 /*!< Invalid data. */
  142. #ifndef B64_DOCUMENTATION_SKIP_SECTION
  143. , B64_max_RC_value
  144. #endif /* !B64_DOCUMENTATION_SKIP_SECTION */
  145. };
  146. #ifndef __cplusplus
  147. typedef enum B64_RC B64_RC;
  148. #endif /* !__cplusplus */
  149. /** \brief Coding behaviour modification flags (for b64_encode2() / b64_decode2())
  150. */
  151. enum B64_FLAGS
  152. {
  153. B64_F_LINE_LEN_USE_PARAM = 0x0000 /*!< Uses the lineLen parameter to b64_encode2(). Ignored by b64_decode2(). */
  154. , B64_F_LINE_LEN_INFINITE = 0x0001 /*!< Ignores the lineLen parameter to b64_encode2(). Line length is infinite. Ignored by b64_decode2(). */
  155. , B64_F_LINE_LEN_64 = 0x0002 /*!< Ignores the lineLen parameter to b64_encode2(). Line length is 64. Ignored by b64_decode2(). */
  156. , B64_F_LINE_LEN_76 = 0x0003 /*!< Ignores the lineLen parameter to b64_encode2(). Line length is 76. Ignored by b64_decode2(). */
  157. , B64_F_LINE_LEN_MASK = 0x000f /*!< Mask for testing line length flags to b64_encode2(). Ignored by b64_encode2(). */
  158. , B64_F_STOP_ON_NOTHING = 0x0000 /*!< Decoding ignores all invalid characters in the input data. Ignored by b64_encode2(). */
  159. , B64_F_STOP_ON_UNKNOWN_CHAR = 0x0100 /*!< Causes decoding to break if any non-Base-64 [a-zA-Z0-9=+/], non-whitespace character is encountered. Ignored by b64_encode2(). */
  160. , B64_F_STOP_ON_UNEXPECTED_WS = 0x0200 /*!< Causes decoding to break if any unexpected whitespace is encountered. Ignored by b64_encode2(). */
  161. , B64_F_STOP_ON_BAD_CHAR = 0x0300 /*!< Causes decoding to break if any non-Base-64 [a-zA-Z0-9=+/] character is encountered. Ignored by b64_encode2(). */
  162. };
  163. #ifndef __cplusplus
  164. typedef enum B64_FLAGS B64_FLAGS;
  165. #endif /* !__cplusplus */
  166. /* /////////////////////////////////////////////////////////////////////////////
  167. * Functions
  168. */
  169. #ifdef __cplusplus
  170. extern "C" {
  171. #endif /* __cplusplus */
  172. /** \brief Encodes a block of binary data into Base-64
  173. *
  174. * \param src Pointer to the block to be encoded. May not be NULL, except when
  175. * \c dest is NULL, in which case it is ignored.
  176. * \param srcSize Length of block to be encoded
  177. * \param dest Pointer to the buffer into which the result is to be written. May
  178. * be NULL, in which case the function returns the required length
  179. * \param destLen Length of the buffer into which the result is to be written. Must
  180. * be at least as large as that indicated by the return value from
  181. * \link b64::b64_encode b64_encode(NULL, srcSize, NULL, 0)\endlink.
  182. *
  183. * \return 0 if the size of the buffer was insufficient, or the length of the
  184. * converted buffer was longer than \c destLen
  185. *
  186. * \note The function returns the required length if \c dest is NULL
  187. *
  188. * \note The function returns the required length if \c dest is NULL. The returned size
  189. * might be larger than the actual required size, but will never be smaller.
  190. *
  191. * \note Threading: The function is fully re-entrant.
  192. *
  193. * \see b64::encode()
  194. */
  195. size_t b64_encode(void const *src, size_t srcSize, char *dest, size_t destLen);
  196. /** \brief Encodes a block of binary data into Base-64
  197. *
  198. * \param src Pointer to the block to be encoded. May not be NULL, except when
  199. * \c dest is NULL, in which case it is ignored.
  200. * \param srcSize Length of block to be encoded
  201. * \param dest Pointer to the buffer into which the result is to be written. May
  202. * be NULL, in which case the function returns the required length
  203. * \param destLen Length of the buffer into which the result is to be written. Must
  204. * be at least as large as that indicated by the return value from
  205. * \link b64::b64_encode2 b64_encode2(NULL, srcSize, NULL, 0, flags, lineLen, rc)\endlink.
  206. * \param flags A combination of the B64_FLAGS enumeration, that moderate the
  207. * behaviour of the function
  208. * \param lineLen If the flags parameter contains B64_F_LINE_LEN_USE_PARAM, then
  209. * this parameter represents the length of the lines into which the encoded form is split,
  210. * with a hard line break ('\\r\\n'). If this value is 0, then the line is not
  211. * split. If it is <0, then the RFC-1113 recommended line length of 64 is used
  212. * \param rc The return code representing the status of the operation. May be NULL.
  213. *
  214. * \return 0 if the size of the buffer was insufficient, or the length of the
  215. * converted buffer was longer than \c destLen
  216. *
  217. * \note The function returns the required length if \c dest is NULL. The returned size
  218. * might be larger than the actual required size, but will never be smaller.
  219. *
  220. * \note Threading: The function is fully re-entrant.
  221. *
  222. * \see b64::encode()
  223. */
  224. size_t b64_encode2( void const *src
  225. , size_t srcSize
  226. , char *dest
  227. , size_t destLen
  228. , unsigned flags
  229. , int lineLen /* = 0 */
  230. , B64_RC *rc /* = NULL */);
  231. /** \brief Decodes a sequence of Base-64 into a block of binary data
  232. *
  233. * \param src Pointer to the Base-64 block to be decoded. May not be NULL, except when
  234. * \c dest is NULL, in which case it is ignored. If \c dest is NULL, and \c src is
  235. * <b>not</b> NULL, then the returned value is calculated exactly, otherwise a value
  236. * is returned that is guaranteed to be large enough to hold the decoded block.
  237. *
  238. * \param srcLen Length of block to be encoded. Must be an integral of 4, the Base-64
  239. * encoding quantum, otherwise the Base-64 block is assumed to be invalid
  240. * \param dest Pointer to the buffer into which the result is to be written. May
  241. * be NULL, in which case the function returns the required length
  242. * \param destSize Length of the buffer into which the result is to be written. Must
  243. * be at least as large as that indicated by the return value from
  244. * \c b64_decode(src, srcSize, NULL, 0), even in the case where the encoded form
  245. * contains a number of characters that will be ignored, resulting in a lower total
  246. * length of converted form.
  247. *
  248. * \return 0 if the size of the buffer was insufficient, or the length of the
  249. * converted buffer was longer than \c destSize
  250. *
  251. * \note The function returns the required length if \c dest is NULL. The returned size
  252. * might be larger than the actual required size, but will never be smaller.
  253. *
  254. * \note \anchor anchor__4_characters The behaviour of both
  255. * \link b64::b64_encode2 b64_encode2()\endlink
  256. * and
  257. * \link b64::b64_decode2 b64_decode2()\endlink
  258. * are undefined if the line length is not a multiple of 4.
  259. *
  260. * \note Threading: The function is fully re-entrant.
  261. *
  262. * \see b64::decode()
  263. */
  264. size_t b64_decode(char const *src, size_t srcLen, void *dest, size_t destSize);
  265. /** \brief Decodes a sequence of Base-64 into a block of binary data
  266. *
  267. * \param src Pointer to the Base-64 block to be decoded. May not be NULL, except when
  268. * \c dest is NULL, in which case it is ignored. If \c dest is NULL, and \c src is
  269. * <b>not</b> NULL, then the returned value is calculated exactly, otherwise a value
  270. * is returned that is guaranteed to be large enough to hold the decoded block.
  271. *
  272. * \param srcLen Length of block to be encoded. Must be an integral of 4, the Base-64
  273. * encoding quantum, otherwise the Base-64 block is assumed to be invalid
  274. * \param dest Pointer to the buffer into which the result is to be written. May
  275. * be NULL, in which case the function returns the required length
  276. * \param destSize Length of the buffer into which the result is to be written. Must
  277. * be at least as large as that indicated by the return value from
  278. * \c b64_decode(src, srcSize, NULL, 0), even in the case where the encoded form
  279. * contains a number of characters that will be ignored, resulting in a lower total
  280. * length of converted form.
  281. * \param flags A combination of the B64_FLAGS enumeration, that moderate the
  282. * behaviour of the function.
  283. * \param rc The return code representing the status of the operation. May be NULL.
  284. * \param badChar If the flags parameter does not contain B64_F_STOP_ON_NOTHING, this
  285. * parameter specifies the address of a pointer that will be set to point to any
  286. * character in the sequence that stops the parsing, as dictated by the flags
  287. * parameter. May be NULL.
  288. *
  289. * \return 0 if the size of the buffer was insufficient, or the length of the
  290. * converted buffer was longer than \c destSize, or a bad character stopped parsing.
  291. *
  292. * \note The function returns the required length if \c dest is NULL. The returned size
  293. * might be larger than the actual required size, but will never be smaller.
  294. *
  295. * \note The behaviour of both
  296. * \link b64::b64_encode2 b64_encode2()\endlink
  297. * and
  298. * \link b64::b64_decode2 b64_decode2()\endlink
  299. * are undefined if the line length is not a multiple of 4.
  300. *
  301. * \note Threading: The function is fully re-entrant.
  302. *
  303. * \see b64::decode()
  304. */
  305. size_t b64_decode2( char const *src
  306. , size_t srcLen
  307. , void *dest
  308. , size_t destSize
  309. , unsigned flags
  310. , char const **badChar /* = NULL */
  311. , B64_RC *rc /* = NULL */);
  312. /** \brief Returns the textual description of the error
  313. *
  314. * \param code The \link b64::B64_RC error code\endlink
  315. */
  316. char const *b64_getErrorString(B64_RC code);
  317. /** \brief Returns the length of the textual description of the error
  318. *
  319. * \see b64_getErrorString()
  320. *
  321. * \param code The \link b64::B64_RC error code\endlink
  322. */
  323. size_t b64_getErrorStringLength(B64_RC code);
  324. #ifdef __cplusplus
  325. } /* extern "C" */
  326. #endif /* __cplusplus */
  327. /* /////////////////////////////////////////////////////////////////////////////
  328. * Namespace
  329. */
  330. #ifndef B64_NO_NAMESPACE
  331. } /* namespace B64_NAMESPACE */
  332. # ifndef B64_DOCUMENTATION_SKIP_SECTION
  333. namespace stlsoft
  334. {
  335. inline char const *c_str_data_a( B64_NAMESPACE_QUALIFIER::B64_RC code)
  336. {
  337. return B64_NAMESPACE_QUALIFIER::b64_getErrorString(code);
  338. }
  339. inline char const *c_str_data( B64_NAMESPACE_QUALIFIER::B64_RC code)
  340. {
  341. return B64_NAMESPACE_QUALIFIER::b64_getErrorString(code);
  342. }
  343. inline size_t c_str_len_a( B64_NAMESPACE_QUALIFIER::B64_RC code)
  344. {
  345. return B64_NAMESPACE_QUALIFIER::b64_getErrorStringLength(code);
  346. }
  347. inline size_t c_str_len( B64_NAMESPACE_QUALIFIER::B64_RC code)
  348. {
  349. return B64_NAMESPACE_QUALIFIER::b64_getErrorStringLength(code);
  350. }
  351. inline char const *c_str_ptr_a( B64_NAMESPACE_QUALIFIER::B64_RC code)
  352. {
  353. return B64_NAMESPACE_QUALIFIER::b64_getErrorString(code);
  354. }
  355. inline char const *c_str_ptr( B64_NAMESPACE_QUALIFIER::B64_RC code)
  356. {
  357. return B64_NAMESPACE_QUALIFIER::b64_getErrorString(code);
  358. }
  359. } /* namespace stlsoft */
  360. # endif /* !B64_DOCUMENTATION_SKIP_SECTION */
  361. #endif /* !B64_NO_NAMESPACE */
  362. /* ////////////////////////////////////////////////////////////////////////// */
  363. #endif /* B64_INCL_B64_H_B64 */
  364. /* ////////////////////////////////////////////////////////////////////////// */