v0.2.2 release
[yandexfotkisp] / src / base64.cpp
diff --git a/src/base64.cpp b/src/base64.cpp
new file mode 100644 (file)
index 0000000..fdd0645
--- /dev/null
@@ -0,0 +1,123 @@
+/*\r
+   base64.cpp and base64.h\r
+\r
+   Copyright (C) 2004-2008 RenĂ© Nyffenegger\r
+\r
+   This source code is provided 'as-is', without any express or implied\r
+   warranty. In no event will the author be held liable for any damages\r
+   arising from the use of this software.\r
+\r
+   Permission is granted to anyone to use this software for any purpose,\r
+   including commercial applications, and to alter it and redistribute it\r
+   freely, subject to the following restrictions:\r
+\r
+   1. The origin of this source code must not be misrepresented; you must not\r
+      claim that you wrote the original source code. If you use this source code\r
+      in a product, an acknowledgment in the product documentation would be\r
+      appreciated but is not required.\r
+\r
+   2. Altered source versions must be plainly marked as such, and must not be\r
+      misrepresented as being the original source code.\r
+\r
+   3. This notice may not be removed or altered from any source distribution.\r
+\r
+   RenĂ© Nyffenegger rene.nyffenegger@adp-gmbh.ch\r
+\r
+*/\r
+\r
+#include "base64.h"\r
+#include <iostream>\r
+\r
+static const std::string base64_chars =\r
+             "ABCDEFGHIJKLMNOPQRSTUVWXYZ"\r
+             "abcdefghijklmnopqrstuvwxyz"\r
+             "0123456789+/";\r
+\r
+\r
+static inline bool is_base64(unsigned char c) {\r
+  return (isalnum(c) || (c == '+') || (c == '/'));\r
+}\r
+\r
+std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len) {\r
+  std::string ret;\r
+  int i = 0;\r
+  int j = 0;\r
+  unsigned char char_array_3[3];\r
+  unsigned char char_array_4[4];\r
+\r
+  while (in_len--) {\r
+    char_array_3[i++] = *(bytes_to_encode++);\r
+    if (i == 3) {\r
+      char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;\r
+      char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);\r
+      char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);\r
+      char_array_4[3] = char_array_3[2] & 0x3f;\r
+\r
+      for(i = 0; (i <4) ; i++)\r
+        ret += base64_chars[char_array_4[i]];\r
+      i = 0;\r
+    }\r
+  }\r
+\r
+  if (i)\r
+  {\r
+    for(j = i; j < 3; j++)\r
+      char_array_3[j] = '\0';\r
+\r
+    char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;\r
+    char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);\r
+    char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);\r
+    char_array_4[3] = char_array_3[2] & 0x3f;\r
+\r
+    for (j = 0; (j < i + 1); j++)\r
+      ret += base64_chars[char_array_4[j]];\r
+\r
+    while((i++ < 3))\r
+      ret += '=';\r
+\r
+  }\r
+\r
+  return ret;\r
+\r
+}\r
+\r
+std::string base64_decode(std::string const& encoded_string) {\r
+  int in_len = encoded_string.size();\r
+  int i = 0;\r
+  int j = 0;\r
+  int in_ = 0;\r
+  unsigned char char_array_4[4], char_array_3[3];\r
+  std::string ret;\r
+\r
+  while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {\r
+    char_array_4[i++] = encoded_string[in_]; in_++;\r
+    if (i ==4) {\r
+      for (i = 0; i <4; i++)\r
+        char_array_4[i] = base64_chars.find(char_array_4[i]);\r
+\r
+      char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);\r
+      char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);\r
+      char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];\r
+\r
+      for (i = 0; (i < 3); i++)\r
+                 ret.append (1,(char)char_array_3[i]);\r
+      i = 0;\r
+    }\r
+  }\r
+\r
+  if (i) {\r
+    for (j = i; j <4; j++)\r
+      char_array_4[j] = 0;\r
+\r
+    for (j = 0; j <4; j++)\r
+      char_array_4[j] = base64_chars.find(char_array_4[j]);\r
+\r
+    char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);\r
+    char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);\r
+    char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];\r
+\r
+       for (j = 0; (j < i - 1); j++) ret.append (1,char_array_3[j]);\r
+  }\r
+\r
+  return ret;\r
+}\r