base64.c 3.4 KB

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