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.

147 lines
3.4 KiB

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 <malloc.h>
  9. #include <string.h>
  10. #include "base64.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. unsigned char *out, *pos;
  26. const unsigned char *end, *in;
  27. size_t olen;
  28. int line_len;
  29. olen = len * 4 / 3 + 4; /* 3-byte blocks to 4-byte */
  30. olen += olen / 72; /* line feeds */
  31. olen++; /* nul termination */
  32. if (olen < len)
  33. return NULL; /* integer overflow */
  34. out = (unsigned char *) malloc( olen );
  35. if (out == NULL)
  36. return NULL;
  37. end = src + len;
  38. in = src;
  39. pos = out;
  40. line_len = 0;
  41. while (end - in >= 3) {
  42. *pos++ = base64_table[in[0] >> 2];
  43. *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
  44. *pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
  45. *pos++ = base64_table[in[2] & 0x3f];
  46. in += 3;
  47. line_len += 4;
  48. if (line_len >= 72) {
  49. *pos++ = '\n';
  50. line_len = 0;
  51. }
  52. }
  53. if (end - in) {
  54. *pos++ = base64_table[in[0] >> 2];
  55. if (end - in == 1) {
  56. *pos++ = base64_table[(in[0] & 0x03) << 4];
  57. *pos++ = '=';
  58. } else {
  59. *pos++ = base64_table[((in[0] & 0x03) << 4) |
  60. (in[1] >> 4)];
  61. *pos++ = base64_table[(in[1] & 0x0f) << 2];
  62. }
  63. *pos++ = '=';
  64. line_len += 4;
  65. }
  66. //if (line_len) *pos++ = '\n';
  67. *pos = '\0';
  68. if (out_len)
  69. *out_len = pos - out;
  70. return out;
  71. }
  72. /**
  73. * base64_decode - Base64 decode
  74. * @src: Data to be decoded
  75. * @len: Length of the data to be decoded
  76. * @out_len: Pointer to output length variable
  77. * Returns: Allocated buffer of out_len bytes of decoded data,
  78. * or %NULL on failure
  79. *
  80. * Caller is responsible for freeing the returned buffer.
  81. */
  82. unsigned char * base64_decode(const unsigned char *src, size_t len, size_t *out_len){
  83. unsigned char dtable[256], *out, *pos, block[4], tmp;
  84. size_t i, count, olen;
  85. int pad = 0;
  86. memset( dtable, 0x80, 256 );
  87. for (i = 0; i < sizeof(base64_table) - 1; i++)
  88. dtable[base64_table[i]] = (unsigned char) i;
  89. dtable['='] = 0;
  90. count = 0;
  91. for (i = 0; i < len; i++) {
  92. if (dtable[src[i]] != 0x80)
  93. count++;
  94. }
  95. if (count == 0 || count % 4)
  96. return NULL;
  97. olen = count / 4 * 3;
  98. pos = out = ( unsigned char *) malloc(olen);
  99. if (out == NULL)
  100. return NULL;
  101. count = 0;
  102. for (i = 0; i < len; i++) {
  103. tmp = dtable[src[i]];
  104. if (tmp == 0x80)
  105. continue;
  106. if (src[i] == '=')
  107. pad++;
  108. block[count] = tmp;
  109. count++;
  110. if (count == 4) {
  111. *pos++ = (block[0] << 2) | (block[1] >> 4);
  112. *pos++ = (block[1] << 4) | (block[2] >> 2);
  113. *pos++ = (block[2] << 6) | block[3];
  114. count = 0;
  115. if (pad) {
  116. if (pad == 1)
  117. pos--;
  118. else if (pad == 2)
  119. pos -= 2;
  120. else {
  121. /* Invalid padding */
  122. free(out);
  123. return NULL;
  124. }
  125. break;
  126. }
  127. }
  128. }
  129. *out_len = pos - out;
  130. return out;
  131. }