base64-decode.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * This code originally came from here
  3. *
  4. * http://base64.sourceforge.net/b64.c
  5. *
  6. * already with MIT license, which is retained.
  7. *
  8. * LICENCE: Copyright (c) 2001 Bob Trower, Trantor Standard Systems Inc.
  9. *
  10. * Permission is hereby granted, free of charge, to any person
  11. * obtaining a copy of this software and associated
  12. * documentation files (the "Software"), to deal in the
  13. * Software without restriction, including without limitation
  14. * the rights to use, copy, modify, merge, publish, distribute,
  15. * sublicense, and/or sell copies of the Software, and to
  16. * permit persons to whom the Software is furnished to do so,
  17. * subject to the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall
  20. * be included in all copies or substantial portions of the
  21. * Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  24. * KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  25. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  26. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  27. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  28. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  29. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  30. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. *
  32. * VERSION HISTORY:
  33. * Bob Trower 08/04/01 -- Create Version 0.00.00B
  34. *
  35. * I cleaned it up quite a bit to match the (linux kernel) style of the rest
  36. * of libwebsockets
  37. */
  38. #include <libwebsockets.h>
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include "private-lib-core.h"
  42. static const char encode_orig[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  43. "abcdefghijklmnopqrstuvwxyz0123456789+/";
  44. static const char encode_url[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  45. "abcdefghijklmnopqrstuvwxyz0123456789-_";
  46. static const char decode[] = "|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW"
  47. "$$$$$$XYZ[\\]^_`abcdefghijklmnopq";
  48. static int
  49. _lws_b64_encode_string(const char *encode, const char *in, int in_len,
  50. char *out, int out_size)
  51. {
  52. unsigned char triple[3];
  53. int i, done = 0;
  54. while (in_len) {
  55. int len = 0;
  56. for (i = 0; i < 3; i++) {
  57. if (in_len) {
  58. triple[i] = *in++;
  59. len++;
  60. in_len--;
  61. } else
  62. triple[i] = 0;
  63. }
  64. if (done + 4 >= out_size)
  65. return -1;
  66. *out++ = encode[triple[0] >> 2];
  67. *out++ = encode[(((triple[0] & 0x03) << 4) & 0x30) |
  68. (((triple[1] & 0xf0) >> 4) & 0x0f)];
  69. *out++ = (len > 1 ? encode[(((triple[1] & 0x0f) << 2) & 0x3c) |
  70. (((triple[2] & 0xc0) >> 6) & 3)] : '=');
  71. *out++ = (len > 2 ? encode[triple[2] & 0x3f] : '=');
  72. done += 4;
  73. }
  74. if (done + 1 >= out_size)
  75. return -1;
  76. *out++ = '\0';
  77. return done;
  78. }
  79. int
  80. lws_b64_encode_string(const char *in, int in_len, char *out, int out_size)
  81. {
  82. return _lws_b64_encode_string(encode_orig, in, in_len, out, out_size);
  83. }
  84. int
  85. lws_b64_encode_string_url(const char *in, int in_len, char *out, int out_size)
  86. {
  87. return _lws_b64_encode_string(encode_url, in, in_len, out, out_size);
  88. }
  89. void
  90. lws_b64_decode_state_init(struct lws_b64state *state)
  91. {
  92. memset(state, 0, sizeof(*state));
  93. }
  94. int
  95. lws_b64_decode_stateful(struct lws_b64state *s, const char *in, size_t *in_len,
  96. uint8_t *out, size_t *out_size, int final)
  97. {
  98. const char *orig_in = in, *end_in = in + *in_len;
  99. uint8_t *orig_out = out, *end_out = out + *out_size;
  100. while (in < end_in && *in && out + 4 < end_out) {
  101. for (; s->i < 4 && in < end_in && *in; s->i++) {
  102. uint8_t v;
  103. v = 0;
  104. s->c = 0;
  105. while (in < end_in && *in && !v) {
  106. s->c = v = *in++;
  107. /* support the url base64 variant too */
  108. if (v == '-')
  109. s->c = v = '+';
  110. if (v == '_')
  111. s->c = v = '/';
  112. v = (v < 43 || v > 122) ? 0 : decode[v - 43];
  113. if (v)
  114. v = (v == '$') ? 0 : v - 61;
  115. }
  116. if (s->c) {
  117. s->len++;
  118. if (v)
  119. s->quad[s->i] = v - 1;
  120. } else
  121. s->quad[s->i] = 0;
  122. }
  123. if (s->i != 4 && !final)
  124. continue;
  125. s->i = 0;
  126. /*
  127. * "The '==' sequence indicates that the last group contained
  128. * only one byte, and '=' indicates that it contained two
  129. * bytes." (wikipedia)
  130. */
  131. if ((in >= end_in || !*in) && s->c == '=')
  132. s->len--;
  133. if (s->len >= 2)
  134. *out++ = s->quad[0] << 2 | s->quad[1] >> 4;
  135. if (s->len >= 3)
  136. *out++ = s->quad[1] << 4 | s->quad[2] >> 2;
  137. if (s->len >= 4)
  138. *out++ = ((s->quad[2] << 6) & 0xc0) | s->quad[3];
  139. s->done += s->len - 1;
  140. s->len = 0;
  141. }
  142. *out = '\0';
  143. *in_len = in - orig_in;
  144. *out_size = out - orig_out;
  145. return 0;
  146. }
  147. /*
  148. * returns length of decoded string in out, or -1 if out was too small
  149. * according to out_size
  150. *
  151. * Only reads up to in_len chars, otherwise if in_len is -1 on entry reads until
  152. * the first NUL in the input.
  153. */
  154. static size_t
  155. _lws_b64_decode_string(const char *in, int in_len, char *out, int out_size)
  156. {
  157. struct lws_b64state state;
  158. size_t il = (size_t)in_len, ol = out_size;
  159. if (in_len == -1)
  160. il = strlen(in);
  161. lws_b64_decode_state_init(&state);
  162. lws_b64_decode_stateful(&state, in, &il, (uint8_t *)out, &ol, 1);
  163. if (!il)
  164. return 0;
  165. return ol;
  166. }
  167. int
  168. lws_b64_decode_string(const char *in, char *out, int out_size)
  169. {
  170. return (int)_lws_b64_decode_string(in, -1, out, out_size);
  171. }
  172. int
  173. lws_b64_decode_string_len(const char *in, int in_len, char *out, int out_size)
  174. {
  175. return (int)_lws_b64_decode_string(in, in_len, out, out_size);
  176. }
  177. #if 0
  178. static const char * const plaintext[] = {
  179. "any carnal pleasure.",
  180. "any carnal pleasure",
  181. "any carnal pleasur",
  182. "any carnal pleasu",
  183. "any carnal pleas",
  184. "Admin:kloikloi"
  185. };
  186. static const char * const coded[] = {
  187. "YW55IGNhcm5hbCBwbGVhc3VyZS4=",
  188. "YW55IGNhcm5hbCBwbGVhc3VyZQ==",
  189. "YW55IGNhcm5hbCBwbGVhc3Vy",
  190. "YW55IGNhcm5hbCBwbGVhc3U=",
  191. "YW55IGNhcm5hbCBwbGVhcw==",
  192. "QWRtaW46a2xvaWtsb2k="
  193. };
  194. int
  195. lws_b64_selftest(void)
  196. {
  197. char buf[64];
  198. unsigned int n, r = 0;
  199. unsigned int test;
  200. lwsl_notice("%s\n", __func__);
  201. /* examples from https://en.wikipedia.org/wiki/Base64 */
  202. for (test = 0; test < (int)LWS_ARRAY_SIZE(plaintext); test++) {
  203. buf[sizeof(buf) - 1] = '\0';
  204. n = lws_b64_encode_string(plaintext[test],
  205. strlen(plaintext[test]), buf, sizeof buf);
  206. if (n != strlen(coded[test]) || strcmp(buf, coded[test])) {
  207. lwsl_err("Failed lws_b64 encode selftest "
  208. "%d result '%s' %d\n", test, buf, n);
  209. r = -1;
  210. }
  211. buf[sizeof(buf) - 1] = '\0';
  212. n = lws_b64_decode_string(coded[test], buf, sizeof buf);
  213. if (n != strlen(plaintext[test]) ||
  214. strcmp(buf, plaintext[test])) {
  215. lwsl_err("Failed lws_b64 decode selftest "
  216. "%d result '%s' / '%s', %d / %zu\n",
  217. test, buf, plaintext[test], n,
  218. strlen(plaintext[test]));
  219. lwsl_hexdump_err(buf, n);
  220. r = -1;
  221. }
  222. }
  223. if (!r)
  224. lwsl_notice("Base 64 selftests passed\n");
  225. else
  226. lwsl_notice("Base64 selftests failed\n");
  227. return r;
  228. }
  229. #endif