base64.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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) & 0x3f];
  46. *pos++ = base64_table[(((in[0] & 0x03) << 4) |
  47. (in[1] >> 4)) & 0x3f];
  48. *pos++ = base64_table[(((in[1] & 0x0f) << 2) |
  49. (in[2] >> 6)) & 0x3f];
  50. *pos++ = base64_table[in[2] & 0x3f];
  51. in += 3;
  52. line_len += 4;
  53. if (line_len >= 72) {
  54. *pos++ = '\n';
  55. line_len = 0;
  56. }
  57. }
  58. if (end - in) {
  59. *pos++ = base64_table[(in[0] >> 2) & 0x3f];
  60. if (end - in == 1) {
  61. *pos++ = base64_table[((in[0] & 0x03) << 4) & 0x3f];
  62. *pos++ = '=';
  63. } else {
  64. *pos++ = base64_table[(((in[0] & 0x03) << 4) |
  65. (in[1] >> 4)) & 0x3f];
  66. *pos++ = base64_table[((in[1] & 0x0f) << 2) & 0x3f];
  67. }
  68. *pos++ = '=';
  69. line_len += 4;
  70. }
  71. if (line_len)
  72. *pos++ = '\n';
  73. *pos = '\0';
  74. if (out_len)
  75. *out_len = pos - out;
  76. return out;
  77. }
  78. /**
  79. * base64_decode - Base64 decode
  80. * @src: Data to be decoded
  81. * @len: Length of the data to be decoded
  82. * @out_len: Pointer to output length variable
  83. * Returns: Allocated buffer of out_len bytes of decoded data,
  84. * or %NULL on failure
  85. *
  86. * Caller is responsible for freeing the returned buffer.
  87. */
  88. unsigned char * base64_decode(const unsigned char *src, size_t len,
  89. size_t *out_len)
  90. {
  91. unsigned char dtable[256], *out, *pos, block[4], tmp;
  92. size_t i, count, olen;
  93. int pad = 0;
  94. os_memset(dtable, 0x80, 256);
  95. for (i = 0; i < sizeof(base64_table) - 1; i++)
  96. dtable[base64_table[i]] = (unsigned char) i;
  97. dtable['='] = 0;
  98. count = 0;
  99. for (i = 0; i < len; i++) {
  100. if (dtable[src[i]] != 0x80)
  101. count++;
  102. }
  103. if (count == 0 || count % 4)
  104. return NULL;
  105. olen = count / 4 * 3;
  106. pos = out = os_malloc(olen);
  107. if (out == NULL)
  108. return NULL;
  109. count = 0;
  110. for (i = 0; i < len; i++) {
  111. tmp = dtable[src[i]];
  112. if (tmp == 0x80)
  113. continue;
  114. if (src[i] == '=')
  115. pad++;
  116. block[count] = tmp;
  117. count++;
  118. if (count == 4) {
  119. *pos++ = (block[0] << 2) | (block[1] >> 4);
  120. *pos++ = (block[1] << 4) | (block[2] >> 2);
  121. *pos++ = (block[2] << 6) | block[3];
  122. count = 0;
  123. if (pad) {
  124. if (pad == 1)
  125. pos--;
  126. else if (pad == 2)
  127. pos -= 2;
  128. else {
  129. /* Invalid padding */
  130. os_free(out);
  131. return NULL;
  132. }
  133. break;
  134. }
  135. }
  136. }
  137. *out_len = pos - out;
  138. return out;
  139. }