modp_b64.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*- */
  2. /* vi: set expandtab shiftwidth=4 tabstop=4: */
  3. /**
  4. * \file
  5. * <PRE>
  6. * MODP_B64 - High performance base64 encoder/decoder
  7. * Version 1.3 -- 17-Mar-2006
  8. * http://modp.com/release/base64
  9. *
  10. * Copyright &copy; 2005, 2006 Nick Galbreath -- nickg [at] modp [dot] com
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are
  15. * met:
  16. *
  17. * Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * Redistributions in binary form must reproduce the above copyright
  21. * notice, this list of conditions and the following disclaimer in the
  22. * documentation and/or other materials provided with the distribution.
  23. *
  24. * Neither the name of the modp.com nor the names of its
  25. * contributors may be used to endorse or promote products derived from
  26. * this software without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  30. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  31. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  32. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  33. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  34. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  35. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  36. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  37. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  38. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * This is the standard "new" BSD license:
  41. * http://www.opensource.org/licenses/bsd-license.php
  42. * </PRE>
  43. */
  44. /* public header */
  45. #include "modp_b64.h"
  46. /*
  47. * If you are ripping this out of the library, comment out the next
  48. * line and uncomment the next lines as approrpiate
  49. */
  50. //#include "config.h"
  51. /* if on motoral, sun, ibm; uncomment this */
  52. /* #define WORDS_BIGENDIAN 1 */
  53. /* else for Intel, Amd; uncomment this */
  54. /* #undef WORDS_BIGENDIAN */
  55. #include "modp_b64_data.h"
  56. #define BADCHAR 0x01FFFFFF
  57. /**
  58. * you can control if we use padding by commenting out this
  59. * next line. However, I highly recommend you use padding and not
  60. * using it should only be for compatability with a 3rd party.
  61. * Also, 'no padding' is not tested!
  62. */
  63. #define DOPAD 1
  64. /*
  65. * if we aren't doing padding
  66. * set the pad character to NULL
  67. */
  68. #ifndef DOPAD
  69. #undef CHARPAD
  70. #define CHARPAD '\0'
  71. #endif
  72. size_t modp_b64_encode(char* dest, const char* str, size_t len)
  73. {
  74. size_t i = 0;
  75. uint8_t* p = (uint8_t*) dest;
  76. /* unsigned here is important! */
  77. uint8_t t1, t2, t3;
  78. if (len > 2) {
  79. for (; i < len - 2; i += 3) {
  80. t1 = str[i]; t2 = str[i+1]; t3 = str[i+2];
  81. *p++ = e0[t1];
  82. *p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)];
  83. *p++ = e1[((t2 & 0x0F) << 2) | ((t3 >> 6) & 0x03)];
  84. *p++ = e2[t3];
  85. }
  86. }
  87. switch (len - i) {
  88. case 0:
  89. break;
  90. case 1:
  91. t1 = str[i];
  92. *p++ = e0[t1];
  93. *p++ = e1[(t1 & 0x03) << 4];
  94. *p++ = CHARPAD;
  95. *p++ = CHARPAD;
  96. break;
  97. default: /* case 2 */
  98. t1 = str[i]; t2 = str[i+1];
  99. *p++ = e0[t1];
  100. *p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)];
  101. *p++ = e2[(t2 & 0x0F) << 2];
  102. *p++ = CHARPAD;
  103. }
  104. *p = '\0';
  105. return p - (uint8_t*)dest;
  106. }
  107. #ifdef WORDS_BIGENDIAN /* BIG ENDIAN -- SUN / IBM / MOTOROLA */
  108. int modp_b64_decode(char* dest, const char* src, int len)
  109. {
  110. if (len == 0) return 0;
  111. #ifdef DOPAD
  112. /* if padding is used, then the message must be at least
  113. 4 chars and be a multiple of 4.
  114. there can be at most 2 pad chars at the end */
  115. if (len < 4 || (len % 4 != 0)) return MODP_B64_ERROR;
  116. if (src[len-1] == CHARPAD) {
  117. len--;
  118. if (src[len -1] == CHARPAD) {
  119. len--;
  120. }
  121. }
  122. #endif /* DOPAD */
  123. size_t i;
  124. int leftover = len % 4;
  125. size_t chunks = (leftover == 0) ? len / 4 - 1 : len /4;
  126. uint8_t* p = (uint8_t*) dest;
  127. uint32_t x = 0;
  128. uint32_t* destInt = (uint32_t*) p;
  129. uint32_t* srcInt = (uint32_t*) src;
  130. uint32_t y = *srcInt++;
  131. for (i = 0; i < chunks; ++i) {
  132. x = d0[y >> 24 & 0xff] | d1[y >> 16 & 0xff] |
  133. d2[y >> 8 & 0xff] | d3[y & 0xff];
  134. if (x >= BADCHAR) return MODP_B64_ERROR;
  135. *destInt = x << 8;
  136. p += 3;
  137. destInt = (uint32_t*)p;
  138. y = *srcInt++;
  139. }
  140. switch (leftover) {
  141. case 0:
  142. x = d0[y >> 24 & 0xff] | d1[y >> 16 & 0xff] |
  143. d2[y >> 8 & 0xff] | d3[y & 0xff];
  144. if (x >= BADCHAR) return MODP_B64_ERROR;
  145. *p++ = ((uint8_t*)&x)[1];
  146. *p++ = ((uint8_t*)&x)[2];
  147. *p = ((uint8_t*)&x)[3];
  148. return (chunks+1)*3;
  149. case 1:
  150. x = d3[y >> 24];
  151. *p = (uint8_t)x;
  152. break;
  153. case 2:
  154. x = d3[y >> 24] *64 + d3[(y >> 16) & 0xff];
  155. *p = (uint8_t)(x >> 4);
  156. break;
  157. default: /* case 3 */
  158. x = (d3[y >> 24] *64 + d3[(y >> 16) & 0xff])*64 +
  159. d3[(y >> 8) & 0xff];
  160. *p++ = (uint8_t) (x >> 10);
  161. *p = (uint8_t) (x >> 2);
  162. break;
  163. }
  164. if (x >= BADCHAR) return MODP_B64_ERROR;
  165. return 3*chunks + (6*leftover)/8;
  166. }
  167. #else /* LITTLE ENDIAN -- INTEL AND FRIENDS */
  168. size_t modp_b64_decode(char* dest, const char* src, size_t len)
  169. {
  170. if (len == 0) return 0;
  171. #ifdef DOPAD
  172. /*
  173. * if padding is used, then the message must be at least
  174. * 4 chars and be a multiple of 4
  175. */
  176. if (len < 4 || (len % 4 != 0)) return MODP_B64_ERROR; /* error */
  177. /* there can be at most 2 pad chars at the end */
  178. if (src[len-1] == CHARPAD) {
  179. len--;
  180. if (src[len -1] == CHARPAD) {
  181. len--;
  182. }
  183. }
  184. #endif
  185. size_t i;
  186. int leftover = len % 4;
  187. size_t chunks = (leftover == 0) ? len / 4 - 1 : len /4;
  188. uint8_t* p = (uint8_t*)dest;
  189. uint32_t x = 0;
  190. const uint8_t* y = (uint8_t*)src;
  191. for (i = 0; i < chunks; ++i, y += 4) {
  192. x = d0[y[0]] | d1[y[1]] | d2[y[2]] | d3[y[3]];
  193. if (x >= BADCHAR) return MODP_B64_ERROR;
  194. *p++ = ((uint8_t*)(&x))[0];
  195. *p++ = ((uint8_t*)(&x))[1];
  196. *p++ = ((uint8_t*)(&x))[2];
  197. }
  198. switch (leftover) {
  199. case 0:
  200. x = d0[y[0]] | d1[y[1]] | d2[y[2]] | d3[y[3]];
  201. if (x >= BADCHAR) return MODP_B64_ERROR;
  202. *p++ = ((uint8_t*)(&x))[0];
  203. *p++ = ((uint8_t*)(&x))[1];
  204. *p = ((uint8_t*)(&x))[2];
  205. return (chunks+1)*3;
  206. case 1: /* with padding this is an impossible case */
  207. x = d0[y[0]];
  208. *p = *((uint8_t*)(&x)); // i.e. first char/byte in int
  209. break;
  210. case 2: // * case 2, 1 output byte */
  211. x = d0[y[0]] | d1[y[1]];
  212. *p = *((uint8_t*)(&x)); // i.e. first char
  213. break;
  214. default: /* case 3, 2 output bytes */
  215. x = d0[y[0]] | d1[y[1]] | d2[y[2]]; /* 0x3c */
  216. *p++ = ((uint8_t*)(&x))[0];
  217. *p = ((uint8_t*)(&x))[1];
  218. break;
  219. }
  220. if (x >= BADCHAR) return MODP_B64_ERROR;
  221. return 3*chunks + (6*leftover)/8;
  222. }
  223. #endif /* if bigendian / else / endif */