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.
 
 
 
 

160 lines
4.1 KiB

  1. #include "base64.h"
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <stdbool.h>
  6. #include <assert.h>
  7. #include <errno.h>
  8. #define LOG_MODULE "base64"
  9. #define LOG_ENABLE_DBG 0
  10. #include "log.h"
  11. static const uint8_t reverse_lookup[256] = {
  12. ['A'] = 0, ['B'] = 1, ['C'] = 2, ['D'] = 3, ['E'] = 4, ['F'] = 5, ['G'] = 6,
  13. ['H'] = 7, ['I'] = 8, ['J'] = 9, ['K'] = 10, ['L'] = 11, ['M'] = 12, ['N'] = 13,
  14. ['O'] = 14, ['P'] = 15, ['Q'] = 16, ['R'] = 17, ['S'] = 18, ['T'] = 19, ['U'] = 20,
  15. ['V'] = 21, ['W'] = 22, ['X'] = 23, ['Y'] = 24, ['Z'] = 25,
  16. ['a'] = 26, ['b'] = 27, ['c'] = 28, ['d'] = 29, ['e'] = 30, ['f'] = 31, ['g'] = 32,
  17. ['h'] = 33, ['i'] = 34, ['j'] = 35, ['k'] = 36, ['l'] = 37, ['m'] = 38, ['n'] = 39,
  18. ['o'] = 40, ['p'] = 41, ['q'] = 42, ['r'] = 43, ['s'] = 44, ['t'] = 45, ['u'] = 46,
  19. ['v'] = 47, ['w'] = 48, ['x'] = 49, ['y'] = 50, ['z'] = 51,
  20. ['0'] = 52, ['1'] = 53, ['2'] = 54, ['3'] = 55, ['4'] = 56, ['5'] = 57,
  21. ['6'] = 58, ['7'] = 59, ['8'] = 60, ['9'] = 61,
  22. ['+'] = 62, ['/'] = 63,
  23. };
  24. static const char lookup[64] = {
  25. 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
  26. 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
  27. 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
  28. 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
  29. '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  30. '+', '/',
  31. };
  32. static inline bool
  33. is_valid(unsigned char c)
  34. {
  35. return reverse_lookup[c] != 0 || c == 'A' || c == '=';
  36. }
  37. char *
  38. base64_decode(const char *s)
  39. {
  40. const size_t len = strlen(s);
  41. if (len % 4 != 0) {
  42. errno = EINVAL;
  43. return NULL;
  44. }
  45. char *ret = malloc(len / 4 * 3 + 1);
  46. if (unlikely(ret == NULL))
  47. return NULL;
  48. for (size_t i = 0, o = 0; i < len; i += 4, o += 3) {
  49. unsigned char c0 = s[i + 0];
  50. unsigned char c1 = s[i + 1];
  51. unsigned char c2 = s[i + 2];
  52. unsigned char c3 = s[i + 3];
  53. if (!is_valid(c0) || !is_valid(c1) || !is_valid(c2) || !is_valid(c3)) {
  54. free(ret);
  55. errno = EINVAL;
  56. return NULL;
  57. }
  58. unsigned a = reverse_lookup[c0];
  59. unsigned b = reverse_lookup[c1];
  60. unsigned c = reverse_lookup[c2];
  61. unsigned d = reverse_lookup[c3];
  62. uint32_t v = a << 18 | b << 12 | c << 6 | d << 0;
  63. char x = (v >> 16) & 0xff;
  64. char y = (v >> 8) & 0xff;
  65. char z = (v >> 0) & 0xff;
  66. LOG_DBG("%c%c%c", x, y, z);
  67. ret[o + 0] = x;
  68. ret[o + 1] = y;
  69. ret[o + 2] = z;
  70. }
  71. ret[len / 4 * 3] = '\0';
  72. return ret;
  73. }
  74. char *
  75. base64_encode(const uint8_t *data, size_t size)
  76. {
  77. assert(size % 3 == 0);
  78. if (unlikely(size % 3 != 0))
  79. return NULL;
  80. char *ret = malloc(size / 3 * 4 + 1);
  81. if (unlikely(ret == NULL))
  82. return NULL;
  83. for (size_t i = 0, o = 0; i < size; i += 3, o += 4) {
  84. int x = data[i + 0];
  85. int y = data[i + 1];
  86. int z = data[i + 2];
  87. uint32_t v = x << 16 | y << 8 | z << 0;
  88. unsigned a = (v >> 18) & 0x3f;
  89. unsigned b = (v >> 12) & 0x3f;
  90. unsigned c = (v >> 6) & 0x3f;
  91. unsigned d = (v >> 0) & 0x3f;
  92. char c0 = lookup[a];
  93. char c1 = lookup[b];
  94. char c2 = lookup[c];
  95. char c3 = lookup[d];
  96. ret[o + 0] = c0;
  97. ret[o + 1] = c1;
  98. ret[o + 2] = c2;
  99. ret[o + 3] = c3;
  100. LOG_DBG("base64: encode: %c%c%c%c", c0, c1, c2, c3);
  101. }
  102. ret[size / 3 * 4] = '\0';
  103. return ret;
  104. }
  105. void
  106. base64_encode_final(const uint8_t *data, size_t size, char result[4])
  107. {
  108. assert(size > 0);
  109. assert(size < 3);
  110. uint32_t v = 0;
  111. if (size >= 1)
  112. v |= data[0] << 16;
  113. if (size >= 2)
  114. v |= data[1] << 8;
  115. unsigned a = (v >> 18) & 0x3f;
  116. unsigned b = (v >> 12) & 0x3f;
  117. unsigned c = (v >> 6) & 0x3f;
  118. char c0 = lookup[a];
  119. char c1 = lookup[b];
  120. char c2 = size == 2 ? lookup[c] : '=';
  121. char c3 = '=';
  122. result[0] = c0;
  123. result[1] = c1;
  124. result[2] = c2;
  125. result[3] = c3;
  126. LOG_DBG("base64: encode: %c%c%c%c", c0, c1, c2, c3);
  127. }