base64.c 3.9 KB

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