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.

181 lines
3.5 KiB

7 years ago
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
7 years ago
7 years ago
​
7 years ago
7 years ago
7 years ago
​
​
7 years ago
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
7 years ago
​
7 years ago
​
​
​
​
7 years ago
​
7 years ago
​
​
7 years ago
​
​
7 years ago
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
7 years ago
7 years ago
7 years ago
​
​
​
7 years ago
7 years ago
7 years ago
​
​
7 years ago
7 years ago
​
7 years ago
7 years ago
7 years ago
​
​
7 years ago
​
7 years ago
7 years ago
​
7 years ago
7 years ago
​
​
7 years ago
​
​
​
​
7 years ago
7 years ago
7 years ago
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Base64 encoding/decoding (RFC1341)
  3. * Copyright (c) 2005-2011, Jouni Malinen <j@w1.fi>
  4. *
  5. * This software may be distributed under the terms of the BSD license.
  6. * See README for more details.
  7. */
  8. #include "base64.h"
  9. #include <malloc.h>
  10. #include <string.h>
  11. static const unsigned char base64_table[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  12. /**
  13. * base64_encode - Base64 encode
  14. * @src: Data to be encoded
  15. * @len: Length of the data to be encoded
  16. * @out_len: Pointer to output length variable, or %NULL if not used
  17. * Returns: Allocated buffer of out_len bytes of encoded data,
  18. * or %NULL on failure
  19. *
  20. * Caller is responsible for freeing the returned buffer. Returned buffer is
  21. * nul terminated to make it easier to use as a C string. The nul terminator is
  22. * not included in out_len.
  23. */
  24. unsigned char * base64_encode(const unsigned char * src, size_t len, size_t * out_len)
  25. {
  26. unsigned char * out, *pos;
  27. const unsigned char *end, *in;
  28. size_t olen;
  29. int line_len;
  30. olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */
  31. olen += olen / 72; /* line feeds */
  32. olen++; /* nul termination */
  33. if (olen < len)
  34. {
  35. return NULL; /* integer overflow */
  36. }
  37. out = (unsigned char *) malloc(olen);
  38. if (out == NULL)
  39. {
  40. return NULL;
  41. }
  42. end = src + len;
  43. in = src;
  44. pos = out;
  45. line_len = 0;
  46. while (end - in >= 3)
  47. {
  48. *pos++ = base64_table[in[0] >> 2];
  49. *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
  50. *pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
  51. *pos++ = base64_table[in[2] & 0x3f];
  52. in += 3;
  53. line_len += 4;
  54. if (line_len >= 72)
  55. {
  56. *pos++ = '\n';
  57. line_len = 0;
  58. }
  59. }
  60. if (end - in)
  61. {
  62. *pos++ = base64_table[in[0] >> 2];
  63. if (end - in == 1)
  64. {
  65. *pos++ = base64_table[(in[0] & 0x03) << 4];
  66. *pos++ = '=';
  67. }
  68. else
  69. {
  70. *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
  71. *pos++ = base64_table[(in[1] & 0x0f) << 2];
  72. }
  73. *pos++ = '=';
  74. line_len += 4;
  75. }
  76. // if (line_len) *pos++ = '\n';
  77. *pos = '\0';
  78. if (out_len)
  79. {
  80. *out_len = pos - out;
  81. }
  82. return out;
  83. }
  84. /**
  85. * base64_decode - Base64 decode
  86. * @src: Data to be decoded
  87. * @len: Length of the data to be decoded
  88. * @out_len: Pointer to output length variable
  89. * Returns: Allocated buffer of out_len bytes of decoded data,
  90. * or %NULL on failure
  91. *
  92. * Caller is responsible for freeing the returned buffer.
  93. */
  94. unsigned char * base64_decode(const unsigned char * src, size_t len, size_t * out_len)
  95. {
  96. unsigned char dtable[256], *out, *pos, block[4], tmp;
  97. size_t i, count, olen;
  98. int pad = 0;
  99. memset(dtable, 0x80, 256);
  100. for (i = 0; i < sizeof(base64_table) - 1; i++)
  101. {
  102. dtable[base64_table[i]] = (unsigned char) i;
  103. }
  104. dtable['='] = 0;
  105. count = 0;
  106. for (i = 0; i < len; i++)
  107. {
  108. if (dtable[src[i]] != 0x80)
  109. {
  110. count++;
  111. }
  112. }
  113. if (count == 0 || count % 4)
  114. {
  115. return NULL;
  116. }
  117. olen = count / 4 * 3;
  118. pos = out = (unsigned char *) malloc(olen);
  119. if (out == NULL)
  120. {
  121. return NULL;
  122. }
  123. count = 0;
  124. for (i = 0; i < len; i++)
  125. {
  126. tmp = dtable[src[i]];
  127. if (tmp == 0x80)
  128. {
  129. continue;
  130. }
  131. if (src[i] == '=')
  132. {
  133. pad++;
  134. }
  135. block[count] = tmp;
  136. count++;
  137. if (count == 4)
  138. {
  139. *pos++ = (block[0] << 2) | (block[1] >> 4);
  140. *pos++ = (block[1] << 4) | (block[2] >> 2);
  141. *pos++ = (block[2] << 6) | block[3];
  142. count = 0;
  143. if (pad)
  144. {
  145. if (pad == 1)
  146. {
  147. pos--;
  148. }
  149. else if (pad == 2)
  150. {
  151. pos -= 2;
  152. }
  153. else
  154. {
  155. /* Invalid padding */
  156. free(out);
  157. return NULL;
  158. }
  159. break;
  160. }
  161. }
  162. }
  163. *out_len = pos - out;
  164. return out;
  165. }