modp_b64.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. /* project header */
  45. #include "stdafx.h"
  46. /* public header */
  47. #include "modp_b64.h"
  48. /*
  49. * If you are ripping this out of the library, comment out the next
  50. * line and uncomment the next lines as approrpiate
  51. */
  52. //#include "config.h"
  53. /* if on motoral, sun, ibm; uncomment this */
  54. /* #define WORDS_BIGENDIAN 1 */
  55. /* else for Intel, Amd; uncomment this */
  56. /* #undef WORDS_BIGENDIAN */
  57. #include "modp_b64_data.h"
  58. #define BADCHAR 0x01FFFFFF
  59. /**
  60. * you can control if we use padding by commenting out this
  61. * next line. However, I highly recommend you use padding and not
  62. * using it should only be for compatability with a 3rd party.
  63. * Also, 'no padding' is not tested!
  64. */
  65. #define DOPAD 1
  66. /*
  67. * if we aren't doing padding
  68. * set the pad character to NULL
  69. */
  70. #ifndef DOPAD
  71. #undef CHARPAD
  72. #define CHARPAD '\0'
  73. #endif
  74. int modp_b64_encode(char* dest, const char* str, int len)
  75. {
  76. int i;
  77. uint8_t* p = (uint8_t*) dest;
  78. /* unsigned here is important! */
  79. uint8_t t1, t2, t3;
  80. for (i = 0; i < len - 2; i += 3) {
  81. t1 = str[i]; t2 = str[i+1]; t3 = str[i+2];
  82. *p++ = e0[t1];
  83. *p++ = e1[((t1 & 0x03) << 4) | ((t2 >> 4) & 0x0F)];
  84. *p++ = e1[((t2 & 0x0F) << 2) | ((t3 >> 6) & 0x03)];
  85. *p++ = e2[t3];
  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 -1;
  116. if (src[len-1] == CHARPAD) {
  117. len--;
  118. if (src[len -1] == CHARPAD) {
  119. len--;
  120. }
  121. }
  122. #endif /* DOPAD */
  123. int i;
  124. int leftover = len % 4;
  125. int 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 -1;
  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 -1;
  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 -1;
  165. return 3*chunks + (6*leftover)/8;
  166. }
  167. #else /* LITTLE ENDIAN -- INTEL AND FRIENDS */
  168. int modp_b64_decode(char* dest, const char* src, int 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 -1; /* 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. int i;
  186. int leftover = len % 4;
  187. int chunks = (leftover == 0) ? len / 4 - 1 : len /4;
  188. uint8_t* p = (uint8_t*)dest;
  189. uint32_t x = 0;
  190. uint32_t* destInt = (uint32_t*) p;
  191. uint32_t* srcInt = (uint32_t*) src;
  192. uint32_t y = *srcInt++;
  193. for (i = 0; i < chunks; ++i) {
  194. x = d0[y & 0xff] |
  195. d1[(y >> 8) & 0xff] |
  196. d2[(y >> 16) & 0xff] |
  197. d3[(y >> 24) & 0xff];
  198. if (x >= BADCHAR) return -1;
  199. *destInt = x ;
  200. p += 3;
  201. destInt = (uint32_t*)p;
  202. y = *srcInt++;}
  203. switch (leftover) {
  204. case 0:
  205. x = d0[y & 0xff] |
  206. d1[(y >> 8) & 0xff] |
  207. d2[(y >> 16) & 0xff] |
  208. d3[(y >> 24) & 0xff];
  209. if (x >= BADCHAR) return -1;
  210. *p++ = ((uint8_t*)(&x))[0];
  211. *p++ = ((uint8_t*)(&x))[1];
  212. *p = ((uint8_t*)(&x))[2];
  213. return (chunks+1)*3;
  214. break;
  215. case 1: /* with padding this is an impossible case */
  216. x = d0[y & 0xff];
  217. *p = *((uint8_t*)(&x)); // i.e. first char/byte in int
  218. break;
  219. case 2: // * case 2, 1 output byte */
  220. x = d0[y & 0xff] | d1[y >> 8 & 0xff];
  221. *p = *((uint8_t*)(&x)); // i.e. first char
  222. break;
  223. default: /* case 3, 2 output bytes */
  224. x = d0[y & 0xff] |
  225. d1[y >> 8 & 0xff ] |
  226. d2[y >> 16 & 0xff]; /* 0x3c */
  227. *p++ = ((uint8_t*)(&x))[0];
  228. *p = ((uint8_t*)(&x))[1];
  229. break;
  230. }
  231. if (x >= BADCHAR) return -1;
  232. return 3*chunks + (6*leftover)/8;
  233. }
  234. #endif /* if bigendian / else / endif */