base64.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Copyright (C) 2004-2008 René Nyffenegger
  3. This source code is provided 'as-is', without any express or implied
  4. warranty. In no event will the author be held liable for any damages
  5. arising from the use of this software.
  6. Permission is granted to anyone to use this software for any purpose,
  7. including commercial applications, and to alter it and redistribute it
  8. freely, subject to the following restrictions:
  9. 1. The origin of this source code must not be misrepresented; you must not
  10. claim that you wrote the original source code. If you use this source code
  11. in a product, an acknowledgment in the product documentation would be
  12. appreciated but is not required.
  13. 2. Altered source versions must be plainly marked as such, and must not be
  14. misrepresented as being the original source code.
  15. 3. This notice may not be removed or altered from any source distribution.
  16. René Nyffenegger rene.nyffenegger@adp-gmbh.ch
  17. */
  18. #ifndef CEREAL_EXTERNAL_BASE64_HPP_
  19. #define CEREAL_EXTERNAL_BASE64_HPP_
  20. #ifdef __GNUC__
  21. #pragma GCC diagnostic push
  22. #pragma GCC diagnostic ignored "-Wconversion"
  23. #endif
  24. #include <string>
  25. namespace cereal
  26. {
  27. namespace base64
  28. {
  29. static const std::string chars =
  30. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  31. "abcdefghijklmnopqrstuvwxyz"
  32. "0123456789+/";
  33. static inline bool is_base64(unsigned char c) {
  34. return (isalnum(c) || (c == '+') || (c == '/'));
  35. }
  36. inline std::string encode(unsigned char const* bytes_to_encode, size_t in_len) {
  37. std::string ret;
  38. int i = 0;
  39. int j = 0;
  40. unsigned char char_array_3[3];
  41. unsigned char char_array_4[4];
  42. while (in_len--) {
  43. char_array_3[i++] = *(bytes_to_encode++);
  44. if (i == 3) {
  45. char_array_4[0] = static_cast<unsigned char>((char_array_3[0] & 0xfc) >> 2);
  46. char_array_4[1] = static_cast<unsigned char>( ( ( char_array_3[0] & 0x03 ) << 4 ) + ( ( char_array_3[1] & 0xf0 ) >> 4 ) );
  47. char_array_4[2] = static_cast<unsigned char>( ( ( char_array_3[1] & 0x0f ) << 2 ) + ( ( char_array_3[2] & 0xc0 ) >> 6 ) );
  48. char_array_4[3] = static_cast<unsigned char>( char_array_3[2] & 0x3f );
  49. for(i = 0; (i <4) ; i++)
  50. ret += chars[char_array_4[i]];
  51. i = 0;
  52. }
  53. }
  54. if (i)
  55. {
  56. for(j = i; j < 3; j++)
  57. char_array_3[j] = '\0';
  58. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  59. char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  60. char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  61. char_array_4[3] = char_array_3[2] & 0x3f;
  62. for (j = 0; (j < i + 1); j++)
  63. ret += chars[char_array_4[j]];
  64. while((i++ < 3))
  65. ret += '=';
  66. }
  67. return ret;
  68. }
  69. inline std::string decode(std::string const& encoded_string) {
  70. size_t in_len = encoded_string.size();
  71. size_t i = 0;
  72. size_t j = 0;
  73. int in_ = 0;
  74. unsigned char char_array_4[4], char_array_3[3];
  75. std::string ret;
  76. while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
  77. char_array_4[i++] = encoded_string[in_]; in_++;
  78. if (i ==4) {
  79. for (i = 0; i <4; i++)
  80. char_array_4[i] = static_cast<unsigned char>(chars.find( char_array_4[i] ));
  81. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  82. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  83. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  84. for (i = 0; (i < 3); i++)
  85. ret += char_array_3[i];
  86. i = 0;
  87. }
  88. }
  89. if (i) {
  90. for (j = i; j <4; j++)
  91. char_array_4[j] = 0;
  92. for (j = 0; j <4; j++)
  93. char_array_4[j] = static_cast<unsigned char>(chars.find( char_array_4[j] ));
  94. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  95. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  96. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  97. for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
  98. }
  99. return ret;
  100. }
  101. } // namespace base64
  102. } // namespace cereal
  103. #ifdef __GNUC__
  104. #pragma GCC diagnostic pop
  105. #endif
  106. #endif // CEREAL_EXTERNAL_BASE64_HPP_