You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

227 lines
7.0KB

  1. /*
  2. * Copyright (c) 2006 Ryan Martell. (rdm4@martellventures.com)
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file base64.c
  22. * @brief Base64 encode/decode
  23. * @author Ryan Martell <rdm4@martellventures.com> (with lots of Michael)
  24. */
  25. #include "common.h"
  26. #include "base64.h"
  27. /* ---------------- private code */
  28. static const uint8_t map2[] =
  29. {
  30. 0x3e, 0xff, 0xff, 0xff, 0x3f, 0x34, 0x35, 0x36,
  31. 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff,
  32. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x01,
  33. 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,
  34. 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
  35. 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19,
  36. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1a, 0x1b,
  37. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
  38. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
  39. 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33
  40. };
  41. int av_base64_decode(uint8_t * out, const char *in, int out_length)
  42. {
  43. int i, v;
  44. uint8_t *dst = out;
  45. v = 0;
  46. for (i = 0; in[i] && in[i] != '='; i++) {
  47. unsigned int index= in[i]-43;
  48. if (index>=FF_ARRAY_ELEMS(map2) || map2[index] == 0xff)
  49. return -1;
  50. v = (v << 6) + map2[index];
  51. if (i & 3) {
  52. if (dst - out < out_length) {
  53. *dst++ = v >> (6 - 2 * (i & 3));
  54. }
  55. }
  56. }
  57. return dst - out;
  58. }
  59. /*****************************************************************************
  60. * b64_encode: Stolen from VLC's http.c.
  61. * Simplified by Michael.
  62. * Fixed edge cases and made it work from data (vs. strings) by Ryan.
  63. *****************************************************************************/
  64. char *av_base64_encode(char * buf, int buf_len, const uint8_t * src, int len)
  65. {
  66. static const char b64[] =
  67. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  68. char *ret, *dst;
  69. unsigned i_bits = 0;
  70. int i_shift = 0;
  71. int bytes_remaining = len;
  72. if (len >= UINT_MAX / 4 ||
  73. buf_len < len * 4 / 3 + 12)
  74. return NULL;
  75. ret = dst = buf;
  76. while (bytes_remaining) {
  77. i_bits = (i_bits << 8) + *src++;
  78. bytes_remaining--;
  79. i_shift += 8;
  80. do {
  81. *dst++ = b64[(i_bits << 6 >> i_shift) & 0x3f];
  82. i_shift -= 6;
  83. } while (i_shift > 6 || (bytes_remaining == 0 && i_shift > 0));
  84. }
  85. while ((dst - ret) & 3)
  86. *dst++ = '=';
  87. *dst = '\0';
  88. return ret;
  89. }
  90. #ifdef TEST
  91. #include "log.h"
  92. #include "mem.h"
  93. int main(void)
  94. {
  95. int numerr = 0;
  96. int len;
  97. int numtest = 1;
  98. uint8_t decode[1000];
  99. struct test {
  100. void *data;
  101. int len;
  102. const char *result;
  103. } *t, tests[] = {
  104. {
  105. "", 0, ""}, {
  106. "1", 1, "MQ=="}, {
  107. "22", 2, "MjI="}, {
  108. "333", 3, "MzMz"}, {
  109. "4444", 4, "NDQ0NA=="}, {
  110. "55555", 5, "NTU1NTU="}, {
  111. "abc:def", 7, "YWJjOmRlZg=="}, {
  112. NULL}
  113. };
  114. for (t = tests; t->data; t++) {
  115. char *str;
  116. av_log(NULL, AV_LOG_ERROR, "Encoding %s...\n", (char *) t->data);
  117. str = av_base64_encode(t->data, t->len);
  118. if (str) {
  119. av_log(NULL, AV_LOG_ERROR, "Encoded to %s...\n", str);
  120. if (strcmp(str, t->result) != 0) {
  121. av_log(NULL, AV_LOG_ERROR, "failed test %d: %s != %s\n",
  122. numtest, str, t->result);
  123. numerr++;
  124. }
  125. av_free(str);
  126. }
  127. av_log(NULL, AV_LOG_ERROR, "Done encoding, about to decode...\n");
  128. len = av_base64_decode(decode, t->result, sizeof(decode));
  129. if (len != t->len) {
  130. av_log(NULL, AV_LOG_ERROR, "failed test %d: len %d != %d\n",
  131. numtest, len, t->len);
  132. numerr++;
  133. } else if (memcmp(decode, t->data, t->len) != 0) {
  134. av_log(NULL, AV_LOG_ERROR, "failed test %d: data\n", numtest);
  135. numerr++;
  136. } else {
  137. av_log(NULL, AV_LOG_ERROR, "Decoded to %s\n",
  138. (char *) t->data);
  139. }
  140. numtest++;
  141. }
  142. #undef srand
  143. #undef rand
  144. {
  145. int test_count;
  146. srand(123141); // time(NULL));
  147. for (test_count = 0; test_count < 100; test_count++) {
  148. int size = rand() % 1024;
  149. int ii;
  150. uint8_t *data;
  151. char *encoded_result;
  152. av_log(NULL, AV_LOG_ERROR, "Test %d: Size %d bytes...",
  153. test_count, size);
  154. data = (uint8_t *) av_malloc(size);
  155. for (ii = 0; ii < size; ii++) {
  156. data[ii] = rand() % 255;
  157. }
  158. encoded_result = av_base64_encode(data, size);
  159. if (encoded_result) {
  160. int decode_buffer_size = size + 10; // try without 10 as well
  161. uint8_t *decode_buffer = av_malloc(decode_buffer_size);
  162. if (decode_buffer) {
  163. int decoded_size =
  164. av_base64_decode(decode_buffer, encoded_result,
  165. decode_buffer_size);
  166. if (decoded_size != size) {
  167. av_log(NULL, AV_LOG_ERROR,
  168. "Decoded/Encoded size mismatch (%d != %d)\n",
  169. decoded_size, size);
  170. } else {
  171. if (memcmp(decode_buffer, data, decoded_size) == 0) {
  172. av_log(NULL, AV_LOG_ERROR, "Passed!\n");
  173. } else {
  174. av_log(NULL, AV_LOG_ERROR,
  175. "Failed (Data differs)!\n");
  176. }
  177. }
  178. av_free(decode_buffer);
  179. }
  180. av_free(encoded_result);
  181. }
  182. }
  183. }
  184. // these are invalid strings, that it currently decodes (which it probably shouldn't?)
  185. {
  186. uint8_t str[32];
  187. if (av_base64_decode(str, "M=M=", sizeof(str)) != -1) {
  188. av_log(NULL, AV_LOG_ERROR,
  189. "failed test %d: successful decode of `M=M='\n",
  190. numtest++);
  191. numerr++;
  192. }
  193. if (av_base64_decode(str, "MQ===", sizeof(str)) != -1) {
  194. av_log(NULL, AV_LOG_ERROR,
  195. "failed test %d: successful decode of `MQ==='\n",
  196. numtest++);
  197. numerr++;
  198. }
  199. }
  200. return numerr;
  201. }
  202. #endif