1
0

b64.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /* /////////////////////////////////////////////////////////////////////////////
  2. * File: b64.c
  3. *
  4. * Purpose: Implementation file for the b64 library
  5. *
  6. * Created: 18th October 2004
  7. * Updated: 9th February 2008
  8. *
  9. * Home: http://synesis.com.au/software/
  10. *
  11. * Copyright (c) 2004-2008, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////////// */
  39. /** \file b64.c Implementation file for the b64 library
  40. */
  41. /* /////////////////////////////////////////////////////////////////////////////
  42. * Version information
  43. */
  44. #ifndef B64_DOCUMENTATION_SKIP_SECTION
  45. # define B64_VER_C_B64_MAJOR 1
  46. # define B64_VER_C_B64_MINOR 2
  47. # define B64_VER_C_B64_REVISION 2
  48. # define B64_VER_C_B64_EDIT 16
  49. #endif /* !B64_DOCUMENTATION_SKIP_SECTION */
  50. /* /////////////////////////////////////////////////////////////////////////////
  51. * Includes
  52. */
  53. #include "b64.h"
  54. #include <assert.h>
  55. #include <string.h>
  56. /* /////////////////////////////////////////////////////////////////////////////
  57. * Constants and definitions
  58. */
  59. #ifndef B64_DOCUMENTATION_SKIP_SECTION
  60. # define NUM_PLAIN_DATA_BYTES (3)
  61. # define NUM_ENCODED_DATA_BYTES (4)
  62. #endif /* !B64_DOCUMENTATION_SKIP_SECTION */
  63. /* /////////////////////////////////////////////////////////////////////////////
  64. * Macros
  65. */
  66. #ifndef NUM_ELEMENTS
  67. # define NUM_ELEMENTS(x) (sizeof(x) / sizeof(x[0]))
  68. #endif /* !NUM_ELEMENTS */
  69. /* /////////////////////////////////////////////////////////////////////////////
  70. * Warnings
  71. */
  72. #if defined(_MSC_VER) && \
  73. _MSC_VER < 1000
  74. # pragma warning(disable : 4705)
  75. #endif /* _MSC_VER < 1000 */
  76. /* /////////////////////////////////////////////////////////////////////////////
  77. * Data
  78. */
  79. static const char b64_chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  80. static const signed char b64_indexes[] =
  81. {
  82. /* 0 - 31 / 0x00 - 0x1f */
  83. -1, -1, -1, -1, -1, -1, -1, -1
  84. , -1, -1, -1, -1, -1, -1, -1, -1
  85. , -1, -1, -1, -1, -1, -1, -1, -1
  86. , -1, -1, -1, -1, -1, -1, -1, -1
  87. /* 32 - 63 / 0x20 - 0x3f */
  88. , -1, -1, -1, -1, -1, -1, -1, -1
  89. , -1, -1, -1, 62, -1, -1, -1, 63 /* ... , '+', ... '/' */
  90. , 52, 53, 54, 55, 56, 57, 58, 59 /* '0' - '7' */
  91. , 60, 61, -1, -1, -1, -1, -1, -1 /* '8', '9', ... */
  92. /* 64 - 95 / 0x40 - 0x5f */
  93. , -1, 0, 1, 2, 3, 4, 5, 6 /* ..., 'A' - 'G' */
  94. , 7, 8, 9, 10, 11, 12, 13, 14 /* 'H' - 'O' */
  95. , 15, 16, 17, 18, 19, 20, 21, 22 /* 'P' - 'W' */
  96. , 23, 24, 25, -1, -1, -1, -1, -1 /* 'X', 'Y', 'Z', ... */
  97. /* 96 - 127 / 0x60 - 0x7f */
  98. , -1, 26, 27, 28, 29, 30, 31, 32 /* ..., 'a' - 'g' */
  99. , 33, 34, 35, 36, 37, 38, 39, 40 /* 'h' - 'o' */
  100. , 41, 42, 43, 44, 45, 46, 47, 48 /* 'p' - 'w' */
  101. , 49, 50, 51, -1, -1, -1, -1, -1 /* 'x', 'y', 'z', ... */
  102. , -1, -1, -1, -1, -1, -1, -1, -1
  103. , -1, -1, -1, -1, -1, -1, -1, -1
  104. , -1, -1, -1, -1, -1, -1, -1, -1
  105. , -1, -1, -1, -1, -1, -1, -1, -1
  106. , -1, -1, -1, -1, -1, -1, -1, -1
  107. , -1, -1, -1, -1, -1, -1, -1, -1
  108. , -1, -1, -1, -1, -1, -1, -1, -1
  109. , -1, -1, -1, -1, -1, -1, -1, -1
  110. , -1, -1, -1, -1, -1, -1, -1, -1
  111. , -1, -1, -1, -1, -1, -1, -1, -1
  112. , -1, -1, -1, -1, -1, -1, -1, -1
  113. , -1, -1, -1, -1, -1, -1, -1, -1
  114. , -1, -1, -1, -1, -1, -1, -1, -1
  115. , -1, -1, -1, -1, -1, -1, -1, -1
  116. , -1, -1, -1, -1, -1, -1, -1, -1
  117. , -1, -1, -1, -1, -1, -1, -1, -1
  118. };
  119. /* /////////////////////////////////////////////////////////////////////////////
  120. * Helper functions
  121. */
  122. /** This function reads in 3 bytes at a time, and translates them into 4
  123. * characters.
  124. */
  125. static size_t b64_encode_( unsigned char const *src
  126. , size_t srcSize
  127. , char *const dest
  128. , size_t destLen
  129. , unsigned lineLen
  130. , B64_RC *rc)
  131. {
  132. size_t total = ((srcSize + (NUM_PLAIN_DATA_BYTES - 1)) / NUM_PLAIN_DATA_BYTES) * NUM_ENCODED_DATA_BYTES;
  133. assert(NULL != rc);
  134. *rc = B64_RC_OK;
  135. if(lineLen > 0)
  136. {
  137. unsigned numLines = (unsigned int)(total + (lineLen - 1)) / lineLen;
  138. total += 2 * (numLines - 1);
  139. }
  140. if(NULL == dest)
  141. {
  142. return total;
  143. }
  144. else if(destLen < total)
  145. {
  146. *rc = B64_RC_INSUFFICIENT_BUFFER;
  147. return 0;
  148. }
  149. else
  150. {
  151. char *p = dest;
  152. char *end = dest + destLen;
  153. size_t len = 0;
  154. for(; NUM_PLAIN_DATA_BYTES <= srcSize; srcSize -= NUM_PLAIN_DATA_BYTES)
  155. {
  156. char characters[NUM_ENCODED_DATA_BYTES] = {0};
  157. /*
  158. *
  159. * | 0 | 1 | 2 |
  160. *
  161. * | | | |
  162. * | | | | | | |
  163. * | | | | | | | | | | | | |
  164. * | | | | | | | | | | | | | | | | | | | | | | | | |
  165. *
  166. * | 0 | 1 | 2 | 3 |
  167. *
  168. */
  169. /* characters[0] is the 6 left-most bits of src[0] */
  170. characters[0] = (char)((src[0] & 0xfc) >> 2);
  171. /* characters[0] is the right-most 2 bits of src[0] and the left-most 4 bits of src[1] */
  172. characters[1] = (char)(((src[0] & 0x03) << 4) + ((src[1] & 0xf0) >> 4));
  173. /* characters[0] is the right-most 4 bits of src[1] and the 2 left-most bits of src[2] */
  174. characters[2] = (char)(((src[1] & 0x0f) << 2) + ((src[2] & 0xc0) >> 6));
  175. /* characters[3] is the right-most 6 bits of src[2] */
  176. characters[3] = (char)(src[2] & 0x3f);
  177. #ifndef __WATCOMC__
  178. assert(characters[0] >= 0 && characters[0] < 64);
  179. assert(characters[1] >= 0 && characters[1] < 64);
  180. assert(characters[2] >= 0 && characters[2] < 64);
  181. assert(characters[3] >= 0 && characters[3] < 64);
  182. #endif /* __WATCOMC__ */
  183. src += NUM_PLAIN_DATA_BYTES;
  184. *p++ = b64_chars[(unsigned char)characters[0]];
  185. assert(NULL != strchr(b64_chars, *(p-1)));
  186. ++len;
  187. assert(len != lineLen);
  188. *p++ = b64_chars[(unsigned char)characters[1]];
  189. assert(NULL != strchr(b64_chars, *(p-1)));
  190. ++len;
  191. assert(len != lineLen);
  192. *p++ = b64_chars[(unsigned char)characters[2]];
  193. assert(NULL != strchr(b64_chars, *(p-1)));
  194. ++len;
  195. assert(len != lineLen);
  196. *p++ = b64_chars[(unsigned char)characters[3]];
  197. assert(NULL != strchr(b64_chars, *(p-1)));
  198. if( ++len == lineLen &&
  199. p != end)
  200. {
  201. *p++ = '\r';
  202. *p++ = '\n';
  203. len = 0;
  204. }
  205. }
  206. if(0 != srcSize)
  207. {
  208. /* Deal with the overspill, by boosting it up to three bytes (using 0s)
  209. * and then appending '=' for any missing characters.
  210. *
  211. * This is done into a temporary buffer, so we can call ourselves and
  212. * have the output continue to be written direct to the destination.
  213. */
  214. unsigned char dummy[NUM_PLAIN_DATA_BYTES] = {0};
  215. size_t i;
  216. for(i = 0; i < srcSize; ++i)
  217. {
  218. dummy[i] = *src++;
  219. }
  220. for(; i < NUM_PLAIN_DATA_BYTES; ++i)
  221. {
  222. dummy[i] = '\0';
  223. }
  224. b64_encode_(&dummy[0], NUM_PLAIN_DATA_BYTES, p, NUM_ENCODED_DATA_BYTES * (1 + 2), 0, rc);
  225. for(p += 1 + srcSize; srcSize++ < NUM_PLAIN_DATA_BYTES; )
  226. {
  227. *p++ = '=';
  228. }
  229. }
  230. return total;
  231. }
  232. }
  233. /** This function reads in a character string in 4-character chunks, and writes
  234. * out the converted form in 3-byte chunks to the destination.
  235. */
  236. static size_t b64_decode_( char const *src
  237. , size_t srcLen
  238. , unsigned char *dest
  239. , size_t destSize
  240. , unsigned flags
  241. , char const **badChar
  242. , B64_RC *rc)
  243. {
  244. const size_t wholeChunks = (srcLen / NUM_ENCODED_DATA_BYTES);
  245. const size_t remainderBytes = (srcLen % NUM_ENCODED_DATA_BYTES);
  246. size_t maxTotal = (wholeChunks + (0 != remainderBytes)) * NUM_PLAIN_DATA_BYTES;
  247. unsigned char *dest_ = dest;
  248. ((void)remainderBytes);
  249. assert(NULL != badChar);
  250. assert(NULL != rc);
  251. *badChar = NULL;
  252. *rc = B64_RC_OK;
  253. if(NULL == dest)
  254. {
  255. return maxTotal;
  256. }
  257. else if(destSize < maxTotal)
  258. {
  259. *rc = B64_RC_INSUFFICIENT_BUFFER;
  260. return 0;
  261. }
  262. else
  263. {
  264. /* Now we iterate through the src, collecting together four characters
  265. * at a time from the Base-64 alphabet, until the end-point is reached.
  266. *
  267. *
  268. */
  269. char const *begin = src;
  270. char const *const end = begin + srcLen;
  271. size_t currIndex = 0;
  272. size_t numPads = 0;
  273. signed char indexes[NUM_ENCODED_DATA_BYTES] = {0}; /* 4 */
  274. for(; begin != end; ++begin)
  275. {
  276. const char ch = *begin;
  277. if('=' == ch)
  278. {
  279. assert(currIndex < NUM_ENCODED_DATA_BYTES);
  280. indexes[currIndex++] = '\0';
  281. ++numPads;
  282. }
  283. else
  284. {
  285. /* NOTE: Had to rename 'index' to 'ix', due to name clash with GCC on 64-bit Linux. */
  286. signed char ix = b64_indexes[(unsigned char)ch];
  287. if(-1 == ix)
  288. {
  289. switch(ch)
  290. {
  291. case ' ':
  292. case '\t':
  293. case '\b':
  294. case '\v':
  295. if(B64_F_STOP_ON_UNEXPECTED_WS & flags)
  296. {
  297. *rc = B64_RC_DATA_ERROR;
  298. *badChar = begin;
  299. return 0;
  300. }
  301. else
  302. {
  303. /* Fall through */
  304. }
  305. case '\r':
  306. case '\n':
  307. continue;
  308. default:
  309. if(B64_F_STOP_ON_UNKNOWN_CHAR & flags)
  310. {
  311. *rc = B64_RC_DATA_ERROR;
  312. *badChar = begin;
  313. return 0;
  314. }
  315. else
  316. {
  317. continue;
  318. }
  319. }
  320. }
  321. else
  322. {
  323. numPads = 0;
  324. assert(currIndex < NUM_ENCODED_DATA_BYTES);
  325. indexes[currIndex++] = ix;
  326. }
  327. }
  328. if(NUM_ENCODED_DATA_BYTES == currIndex)
  329. {
  330. unsigned char bytes[NUM_PLAIN_DATA_BYTES] = {0}; /* 3 */
  331. bytes[0] = (unsigned char)((indexes[0] << 2) + ((indexes[1] & 0x30) >> 4));
  332. currIndex = 0;
  333. *dest++ = bytes[0];
  334. if(2 != numPads)
  335. {
  336. bytes[1] = (unsigned char)(((indexes[1] & 0xf) << 4) + ((indexes[2] & 0x3c) >> 2));
  337. *dest++ = bytes[1];
  338. if(1 != numPads)
  339. {
  340. bytes[2] = (unsigned char)(((indexes[2] & 0x3) << 6) + indexes[3]);
  341. *dest++ = bytes[2];
  342. }
  343. }
  344. if(0 != numPads)
  345. {
  346. break;
  347. }
  348. }
  349. }
  350. return (size_t)(dest - dest_);
  351. }
  352. }
  353. /* /////////////////////////////////////////////////////////////////////////////
  354. * API functions
  355. */
  356. size_t b64_encode(void const *src, size_t srcSize, char *dest, size_t destLen)
  357. {
  358. /* Use Null Object (Variable) here for rc, so do not need to check
  359. * elsewhere.
  360. */
  361. B64_RC rc_;
  362. return b64_encode_((unsigned char const*)src, srcSize, dest, destLen, 0, &rc_);
  363. }
  364. size_t b64_encode2( void const *src
  365. , size_t srcSize
  366. , char *dest
  367. , size_t destLen
  368. , unsigned flags
  369. , int lineLen /* = -1 */
  370. , B64_RC *rc /* = NULL */)
  371. {
  372. /* Use Null Object (Variable) here for rc, so do not need to check
  373. * elsewhere
  374. */
  375. B64_RC rc_;
  376. if(NULL == rc)
  377. {
  378. rc = &rc_;
  379. }
  380. switch(B64_F_LINE_LEN_MASK & flags)
  381. {
  382. case B64_F_LINE_LEN_USE_PARAM:
  383. if(lineLen >= 0)
  384. {
  385. break;
  386. }
  387. /* Fall through to 64 */
  388. case B64_F_LINE_LEN_64:
  389. lineLen = 64;
  390. break;
  391. case B64_F_LINE_LEN_76:
  392. lineLen = 76;
  393. break;
  394. default:
  395. assert(!"Bad line length flag specified to b64_encode2()");
  396. case B64_F_LINE_LEN_INFINITE:
  397. lineLen = 0;
  398. break;
  399. }
  400. assert(0 == (lineLen % 4));
  401. return b64_encode_((unsigned char const*)src, srcSize, dest, destLen, (unsigned)lineLen, rc);
  402. }
  403. size_t b64_decode(char const *src, size_t srcLen, void *dest, size_t destSize)
  404. {
  405. /* Use Null Object (Variable) here for rc and badChar, so do not need to
  406. * check elsewhere.
  407. */
  408. char const *badChar_;
  409. B64_RC rc_;
  410. return b64_decode_(src, srcLen, (unsigned char*)dest, destSize, B64_F_STOP_ON_NOTHING, &badChar_, &rc_);
  411. }
  412. size_t b64_decode2( char const *src
  413. , size_t srcLen
  414. , void *dest
  415. , size_t destSize
  416. , unsigned flags
  417. , char const **badChar /* = NULL */
  418. , B64_RC *rc /* = NULL */)
  419. {
  420. char const *badChar_;
  421. B64_RC rc_;
  422. /* Use Null Object (Variable) here for rc and badChar, so do not need to
  423. * check elsewhere.
  424. */
  425. if(NULL == badChar)
  426. {
  427. badChar = &badChar_;
  428. }
  429. if(NULL == rc)
  430. {
  431. rc = &rc_;
  432. }
  433. return b64_decode_(src, srcLen, (unsigned char*)dest, destSize, flags, badChar, rc);
  434. }
  435. /* ////////////////////////////////////////////////////////////////////////// */
  436. #ifdef B64_DOCUMENTATION_SKIP_SECTION
  437. struct b64ErrorString_t_
  438. #else /* !B64_DOCUMENTATION_SKIP_SECTION */
  439. typedef struct b64ErrorString_t_ b64ErrorString_t_;
  440. struct b64ErrorString_t_
  441. #endif /* !B64_DOCUMENTATION_SKIP_SECTION */
  442. {
  443. int code; /*!< The error code. */
  444. char const *str; /*!< The string. */
  445. size_t len; /*!< The string length. */
  446. };
  447. #define SEVERITY_STR_DECL(rc, desc) \
  448. \
  449. static const char s_str##rc[] = desc; \
  450. static const b64ErrorString_t_ s_rct##rc = { rc, s_str##rc, NUM_ELEMENTS(s_str##rc) - 1 }
  451. #define SEVERITY_STR_ENTRY(rc) \
  452. \
  453. &s_rct##rc
  454. static char const *b64_LookupCodeA_(int code, b64ErrorString_t_ const **mappings, size_t cMappings, size_t *len)
  455. {
  456. /* Use Null Object (Variable) here for len, so do not need to check
  457. * elsewhere.
  458. */
  459. size_t len_;
  460. if(NULL == len)
  461. {
  462. len = &len_;
  463. }
  464. /* Checked, indexed search. */
  465. if( code >= 0 &&
  466. code < B64_max_RC_value)
  467. {
  468. if(code == mappings[code]->code)
  469. {
  470. return (*len = mappings[code]->len, mappings[code]->str);
  471. }
  472. }
  473. /* Linear search. Should only be needed if order in
  474. * b64_LookupErrorStringA_() messed up.
  475. */
  476. { size_t i; for(i = 0; i < cMappings; ++i)
  477. {
  478. if(code == mappings[i]->code)
  479. {
  480. return (*len = mappings[i]->len, mappings[i]->str);
  481. }
  482. }}
  483. return (*len = 0, "");
  484. }
  485. static char const *b64_LookupErrorStringA_(int error, size_t *len)
  486. {
  487. SEVERITY_STR_DECL(B64_RC_OK , "Operation was successful" );
  488. SEVERITY_STR_DECL(B64_RC_INSUFFICIENT_BUFFER , "The given translation buffer was not of sufficient size" );
  489. SEVERITY_STR_DECL(B64_RC_TRUNCATED_INPUT , "The input did not represent a fully formed stream of octet couplings" );
  490. SEVERITY_STR_DECL(B64_RC_DATA_ERROR , "Invalid data" );
  491. static const b64ErrorString_t_ *s_strings[] =
  492. {
  493. SEVERITY_STR_ENTRY(B64_RC_OK),
  494. SEVERITY_STR_ENTRY(B64_RC_INSUFFICIENT_BUFFER),
  495. SEVERITY_STR_ENTRY(B64_RC_TRUNCATED_INPUT),
  496. SEVERITY_STR_ENTRY(B64_RC_DATA_ERROR),
  497. };
  498. return b64_LookupCodeA_(error, s_strings, NUM_ELEMENTS(s_strings), len);
  499. }
  500. char const *b64_getErrorString(B64_RC code)
  501. {
  502. return b64_LookupErrorStringA_((int)code, NULL);
  503. }
  504. size_t b64_getErrorStringLength(B64_RC code)
  505. {
  506. size_t len;
  507. return (b64_LookupErrorStringA_((int)code, &len), len);
  508. }
  509. /* ////////////////////////////////////////////////////////////////////////// */