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.

185 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
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 "includes.h"
  10. #include "os.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 = os_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)
  77. {
  78. *pos++ = '\n';
  79. }
  80. *pos = '\0';
  81. if (out_len)
  82. {
  83. *out_len = pos - out;
  84. }
  85. return out;
  86. }
  87. /**
  88. * base64_decode - Base64 decode
  89. * @src: Data to be decoded
  90. * @len: Length of the data to be decoded
  91. * @out_len: Pointer to output length variable
  92. * Returns: Allocated buffer of out_len bytes of decoded data,
  93. * or %NULL on failure
  94. *
  95. * Caller is responsible for freeing the returned buffer.
  96. */
  97. unsigned char * base64_decode(const unsigned char * src, size_t len, size_t * out_len)
  98. {
  99. unsigned char dtable[256], *out, *pos, block[4], tmp;
  100. size_t i, count, olen;
  101. int pad = 0;
  102. os_memset(dtable, 0x80, 256);
  103. for (i = 0; i < sizeof(base64_table) - 1; i++)
  104. {
  105. dtable[base64_table[i]] = (unsigned char) i;
  106. }
  107. dtable['='] = 0;
  108. count = 0;
  109. for (i = 0; i < len; i++)
  110. {
  111. if (dtable[src[i]] != 0x80)
  112. {
  113. count++;
  114. }
  115. }
  116. if (count == 0 || count % 4)
  117. {
  118. return NULL;
  119. }
  120. olen = count / 4 * 3;
  121. pos = out = os_malloc(olen);
  122. if (out == NULL)
  123. {
  124. return NULL;
  125. }
  126. count = 0;
  127. for (i = 0; i < len; i++)
  128. {
  129. tmp = dtable[src[i]];
  130. if (tmp == 0x80)
  131. {
  132. continue;
  133. }
  134. if (src[i] == '=')
  135. {
  136. pad++;
  137. }
  138. block[count] = tmp;
  139. count++;
  140. if (count == 4)
  141. {
  142. *pos++ = (block[0] << 2) | (block[1] >> 4);
  143. *pos++ = (block[1] << 4) | (block[2] >> 2);
  144. *pos++ = (block[2] << 6) | block[3];
  145. count = 0;
  146. if (pad)
  147. {
  148. if (pad == 1)
  149. {
  150. pos--;
  151. }
  152. else if (pad == 2)
  153. {
  154. pos -= 2;
  155. }
  156. else
  157. {
  158. /* Invalid padding */
  159. os_free(out);
  160. return NULL;
  161. }
  162. break;
  163. }
  164. }
  165. }
  166. *out_len = pos - out;
  167. return out;
  168. }