base64.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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
  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_size)
  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_size) {
  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 *out, int out_size, const uint8_t *in, int in_size)
  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 = in_size;
  72. if (in_size >= UINT_MAX / 4 ||
  73. out_size < AV_BASE64_SIZE(in_size))
  74. return NULL;
  75. ret = dst = out;
  76. while (bytes_remaining) {
  77. i_bits = (i_bits << 8) + *in++;
  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. #undef printf
  92. #define MAX_DATA_SIZE 1024
  93. #define MAX_ENCODED_SIZE 2048
  94. static int test_encode_decode(const uint8_t *data, unsigned int data_size,
  95. const char *encoded_ref)
  96. {
  97. char encoded[MAX_ENCODED_SIZE];
  98. uint8_t data2[MAX_DATA_SIZE];
  99. int data2_size, max_data2_size = MAX_DATA_SIZE;
  100. if (!av_base64_encode(encoded, MAX_ENCODED_SIZE, data, data_size)) {
  101. printf("Failed: cannot encode the input data\n");
  102. return 1;
  103. }
  104. if (encoded_ref && strcmp(encoded, encoded_ref)) {
  105. printf("Failed: encoded string differs from reference\n"
  106. "Encoded:\n%s\nReference:\n%s\n", encoded, encoded_ref);
  107. return 1;
  108. }
  109. if ((data2_size = av_base64_decode(data2, encoded, max_data2_size)) < 0) {
  110. printf("Failed: cannot decode the encoded string\n"
  111. "Encoded:\n%s\n", encoded);
  112. return 1;
  113. }
  114. if (memcmp(data2, data, data_size)) {
  115. printf("Failed: encoded/decoded data differs from original data\n");
  116. return 1;
  117. }
  118. printf("Passed!\n");
  119. return 0;
  120. }
  121. int main(void)
  122. {
  123. int i, error_count = 0;
  124. struct test {
  125. const uint8_t *data;
  126. const char *encoded_ref;
  127. } tests[] = {
  128. { "", ""},
  129. { "1", "MQ=="},
  130. { "22", "MjI="},
  131. { "333", "MzMz"},
  132. { "4444", "NDQ0NA=="},
  133. { "55555", "NTU1NTU="},
  134. { "666666", "NjY2NjY2"},
  135. { "abc:def", "YWJjOmRlZg=="},
  136. };
  137. printf("Encoding/decoding tests\n");
  138. for (i = 0; i < FF_ARRAY_ELEMS(tests); i++)
  139. error_count += test_encode_decode(tests[i].data, strlen(tests[i].data), tests[i].encoded_ref);
  140. return error_count;
  141. }
  142. #endif