base64.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 [email protected]
  18. */
  19. /*
  20. - This source code was altered by Mario Frescas
  21. This distribution of the source code is provided 'as-is', without any express or implied
  22. warranty. In no event will the author be held liable for any damages
  23. arising from the use of this software.
  24. Permission is granted to anyone to use this software for any purpose,
  25. including commercial applications, and to alter it and redistribute it
  26. freely, subject to the following restrictions:
  27. 1. The distribution of the source code must not be misrepresented; you must not
  28. claim that you wrote the distribution of the source code. If you use this source code
  29. in a product, an acknowledgment in the product documentation would be
  30. appreciated but is not required.
  31. 2. Altered source versions must be plainly marked as such, and must not be
  32. misrepresented as being the original source code.
  33. 3. This notice may not be removed or altered from any source distribution.
  34. Mario Frescas [email protected]
  35. */
  36. #include "base64.h"
  37. static const std::string base64_chars =
  38. "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  39. "abcdefghijklmnopqrstuvwxyz"
  40. "0123456789+/";
  41. static inline bool is_base64(unsigned char c) {
  42. return (isalnum(c) || (c == '+') || (c == '/'));
  43. }
  44. std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {
  45. std::string ret;
  46. int i = 0;
  47. int j = 0;
  48. unsigned char char_array_3[3];
  49. unsigned char char_array_4[4];
  50. while (in_len--) {
  51. char_array_3[i++] = *(bytes_to_encode++);
  52. if (i == 3) {
  53. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  54. char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  55. char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  56. char_array_4[3] = char_array_3[2] & 0x3f;
  57. for(i = 0; (i <4) ; i++)
  58. ret += base64_chars[char_array_4[i]];
  59. i = 0;
  60. }
  61. }
  62. if (i)
  63. {
  64. for(j = i; j < 3; j++)
  65. char_array_3[j] = '\0';
  66. char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
  67. char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
  68. char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
  69. char_array_4[3] = char_array_3[2] & 0x3f;
  70. for (j = 0; (j < i + 1); j++)
  71. ret += base64_chars[char_array_4[j]];
  72. while((i++ < 3))
  73. ret += '=';
  74. }
  75. return ret;
  76. }
  77. std::string base64_decode(std::string const& encoded_string) {
  78. int in_len = encoded_string.size();
  79. int i = 0;
  80. int j = 0;
  81. int in_ = 0;
  82. unsigned char char_array_4[4], char_array_3[3];
  83. std::string ret;
  84. while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
  85. char_array_4[i++] = encoded_string[in_]; in_++;
  86. if (i ==4) {
  87. for (i = 0; i <4; i++)
  88. char_array_4[i] = base64_chars.find(char_array_4[i]);
  89. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  90. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  91. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  92. for (i = 0; (i < 3); i++)
  93. ret += char_array_3[i];
  94. i = 0;
  95. }
  96. }
  97. if (i) {
  98. for (j = i; j <4; j++)
  99. char_array_4[j] = 0;
  100. for (j = 0; j <4; j++)
  101. char_array_4[j] = base64_chars.find(char_array_4[j]);
  102. char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
  103. char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
  104. char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
  105. for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
  106. }
  107. return ret;
  108. }
  109. bool base64_encode_file(std::string path)
  110. {
  111. std::string string_in;
  112. std::string string_out;
  113. std::ifstream file_in(path.c_str(), std::ios::in);
  114. if(file_in.fail())
  115. {
  116. return false;
  117. }
  118. while(std::getline(file_in, string_in))
  119. {
  120. string_out = string_in;
  121. }
  122. file_in.close();
  123. std::ofstream file_out(path.c_str(), std::ios::trunc);
  124. file_out << base64_encode((const unsigned char *)string_out.c_str(), string_out.length());
  125. file_out.close();
  126. return true;
  127. }
  128. bool base64_decode_file(std::string path)
  129. {
  130. std::string string_in;
  131. std::ifstream file_in(path.c_str(), std::ios::in);
  132. if(file_in.fail())
  133. {
  134. return false;
  135. }
  136. while(std::getline(file_in, string_in));
  137. file_in.close();
  138. std::ofstream file_out(path.c_str(), std::ios::trunc);
  139. file_out << base64_decode(string_in);
  140. file_out.close();
  141. return true;
  142. }