Base64.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. base64.cpp and base64.h
  3. Copyright (C) 2004-2008 René Nyffenegger
  4. This source code is provided 'as-is', without any express or implied
  5. warranty. In no event will the author be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this source code must not be misrepresented; you must not
  11. claim that you wrote the original source code. If you use this source code
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original source code.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. René Nyffenegger rene.nyffenegger@adp-gmbh.ch
  18. */
  19. /// Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c
  20. /// Originally by René Nyffenegger, modified by some other guy and then devified by Gav Wood.
  21. #include "Base64.h"
  22. using namespace std;
  23. using namespace dev;
  24. static inline bool is_base64(byte c)
  25. {
  26. return (isalnum(c) || (c == '+') || (c == '/'));
  27. }
  28. static inline byte find_base64_char_index(byte c)
  29. {
  30. if ('A' <= c && c <= 'Z') return c - 'A';
  31. else if ('a' <= c && c <= 'z') return c - 'a' + 1 + find_base64_char_index('Z');
  32. else if ('0' <= c && c <= '9') return c - '0' + 1 + find_base64_char_index('z');
  33. else if (c == '+') return 1 + find_base64_char_index('9');
  34. else if (c == '/') return 1 + find_base64_char_index('+');
  35. else return 1 + find_base64_char_index('/');
  36. }
  37. string dev::toBase64(bytesConstRef _in)
  38. {
  39. static const char base64_chars[] =
  40. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  41. "abcdefghijklmnopqrstuvwxyz"
  42. "0123456789+/";
  43. string ret;
  44. int i = 0;
  45. int j = 0;
  46. byte char_array_3[3];
  47. byte char_array_4[4];
  48. auto buf = _in.data();
  49. auto bufLen = _in.size();
  50. while (bufLen--)
  51. {
  52. char_array_3[i++] = *(buf++);
  53. if (i == 3)
  54. {
  55. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  56. char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  57. char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  58. char_array_4[3] = char_array_3[2] & 0x3f;
  59. for (i = 0; i < 4; i++)
  60. ret += base64_chars[char_array_4[i]];
  61. i = 0;
  62. }
  63. }
  64. if (i)
  65. {
  66. for (j = i; j < 3; j++)
  67. char_array_3[j] = '\0';
  68. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  69. char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  70. char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  71. char_array_4[3] = char_array_3[2] & 0x3f;
  72. for (j = 0; j < i + 1; j++)
  73. ret += base64_chars[char_array_4[j]];
  74. while (i++ < 3)
  75. ret += '=';
  76. }
  77. return ret;
  78. }
  79. bytes dev::fromBase64(string const& encoded_string)
  80. {
  81. auto in_len = encoded_string.size();
  82. int i = 0;
  83. int j = 0;
  84. int in_ = 0;
  85. byte char_array_3[3];
  86. byte char_array_4[4];
  87. bytes ret;
  88. while (in_len-- && encoded_string[in_] != '=' && is_base64(encoded_string[in_]))
  89. {
  90. char_array_4[i++] = encoded_string[in_]; in_++;
  91. if (i == 4)
  92. {
  93. for (i = 0; i < 4; i++)
  94. char_array_4[i] = find_base64_char_index(char_array_4[i]);
  95. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  96. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  97. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  98. for (i = 0; (i < 3); i++)
  99. ret.push_back(char_array_3[i]);
  100. i = 0;
  101. }
  102. }
  103. if (i)
  104. {
  105. for (j = i; j < 4; j++)
  106. char_array_4[j] = 0;
  107. for (j = 0; j < 4; j++)
  108. char_array_4[j] = find_base64_char_index(char_array_4[j]);
  109. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  110. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  111. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  112. for (j = 0; j < i - 1; j++)
  113. ret.push_back(char_array_3[j]);
  114. }
  115. return ret;
  116. }