base64.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Base64 encoding: Copyright (c) 2005-2011, Jouni Malinen <j@w1.fi>
  3. * This software may be distributed under the terms of the BSD license.
  4. *
  5. * Base64 decoding: Copyright (c) 2016, polfosol
  6. * Posted at https://stackoverflow.com/a/37109258 under the CC-BY-SA Creative
  7. * Commons license.
  8. */
  9. #include "base64.h"
  10. #include <open62541/types.h>
  11. static const unsigned char base64_table[65] =
  12. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  13. unsigned char *
  14. UA_base64(const unsigned char *src, size_t len, size_t *out_len) {
  15. if(len == 0) {
  16. *out_len = 0;
  17. return (unsigned char*)UA_EMPTY_ARRAY_SENTINEL;
  18. }
  19. size_t olen = 4*((len + 2) / 3); /* 3-byte blocks to 4-byte */
  20. if(olen < len)
  21. return NULL; /* integer overflow */
  22. unsigned char *out = (unsigned char*)UA_malloc(olen);
  23. if(!out)
  24. return NULL;
  25. const unsigned char *end = src + len;
  26. const unsigned char *in = src;
  27. unsigned char *pos = out;
  28. while(end - in >= 3) {
  29. *pos++ = base64_table[in[0] >> 2];
  30. *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
  31. *pos++ = base64_table[((in[1] & 0x0f) << 2) | (in[2] >> 6)];
  32. *pos++ = base64_table[in[2] & 0x3f];
  33. in += 3;
  34. }
  35. if(end - in) {
  36. *pos++ = base64_table[in[0] >> 2];
  37. if(end - in == 1) {
  38. *pos++ = base64_table[(in[0] & 0x03) << 4];
  39. *pos++ = '=';
  40. } else {
  41. *pos++ = base64_table[((in[0] & 0x03) << 4) | (in[1] >> 4)];
  42. *pos++ = base64_table[(in[1] & 0x0f) << 2];
  43. }
  44. *pos++ = '=';
  45. }
  46. *out_len = (size_t)(pos - out);
  47. return out;
  48. }
  49. static const uint32_t from_b64[256] = {
  50. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  51. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  52. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 63, 62, 62, 63,
  53. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0,
  54. 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  55. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0, 0, 0, 0, 63,
  56. 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  57. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51};
  58. unsigned char *
  59. UA_unbase64(const unsigned char *src, size_t len, size_t *out_len) {
  60. // we need a minimum length
  61. if(len <= 2) {
  62. *out_len = 0;
  63. return (unsigned char*)UA_EMPTY_ARRAY_SENTINEL;
  64. }
  65. const unsigned char *p = src;
  66. size_t pad1 = len % 4 || p[len - 1] == '=';
  67. size_t pad2 = pad1 && (len % 4 > 2 || p[len - 2] != '=');
  68. const size_t last = (len - pad1) / 4 << 2;
  69. unsigned char *str = (unsigned char*)UA_malloc(last / 4 * 3 + pad1 + pad2);
  70. if(!str)
  71. return NULL;
  72. unsigned char *pos = str;
  73. for(size_t i = 0; i < last; i += 4) {
  74. uint32_t n = from_b64[p[i]] << 18 | from_b64[p[i + 1]] << 12 |
  75. from_b64[p[i + 2]] << 6 | from_b64[p[i + 3]];
  76. *pos++ = (unsigned char)(n >> 16);
  77. *pos++ = (unsigned char)(n >> 8 & 0xFF);
  78. *pos++ = (unsigned char)(n & 0xFF);
  79. }
  80. if(pad1) {
  81. uint32_t n = from_b64[p[last]] << 18 | from_b64[p[last + 1]] << 12;
  82. *pos++ = (unsigned char)(n >> 16);
  83. if(pad2) {
  84. n |= from_b64[p[last + 2]] << 6;
  85. *pos++ = (unsigned char)(n >> 8 & 0xFF);
  86. }
  87. }
  88. *out_len = (uintptr_t)(pos - str);
  89. return str;
  90. }