Remove boost dependency
[platform/core/appfw/app-installers.git] / src / common / utils / base64.cc
index 1517b79..5e9c75e 100644 (file)
@@ -4,29 +4,63 @@
 
 #include "common/utils/base64.h"
 
-#include <boost/archive/iterators/base64_from_binary.hpp>
-#include <boost/archive/iterators/transform_width.hpp>
-
 #include <algorithm>
 #include <sstream>
 #include <string>
 
-namespace bai = boost::archive::iterators;
-
 namespace {
 
-typedef bai::base64_from_binary<bai::transform_width<const char*, 6, 8>>
-    base64_encode;
+const char kBase64Chars[] =
+    "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
+    "0123456789+/";
 
 }  // namespace
 
 namespace common_installer {
 
+// from https://stackoverflow.com/a/13935718
 std::string EncodeBase64(unsigned char* val, size_t len) {
-  std::stringstream os;
-  std::copy(base64_encode(val), base64_encode(val + len),
-      std::ostream_iterator<char>(os));
-  return os.str();
+  std::string ret;
+  int i = 0;
+  int j = 0;
+  unsigned char char_array_3[3];
+  unsigned char char_array_4[4];
+
+  while (len--) {
+    char_array_3[i++] = *(val++);
+    if (i == 3) {
+      char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
+      char_array_4[1] = ((char_array_3[0] & 0x03) << 4) +
+          ((char_array_3[1] & 0xf0) >> 4);
+      char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) +
+          ((char_array_3[2] & 0xc0) >> 6);
+      char_array_4[3] = char_array_3[2] & 0x3f;
+
+      for(i = 0; (i <4) ; i++)
+        ret += kBase64Chars[char_array_4[i]];
+      i = 0;
+    }
+  }
+
+  if (i) {
+    for(j = i; j < 3; j++)
+      char_array_3[j] = '\0';
+
+    char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
+    char_array_4[1] = ((char_array_3[0] & 0x03) << 4) +
+        ((char_array_3[1] & 0xf0) >> 4);
+    char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) +
+        ((char_array_3[2] & 0xc0) >> 6);
+    char_array_4[3] = char_array_3[2] & 0x3f;
+
+    for (j = 0; (j < i + 1); j++)
+      ret += kBase64Chars[char_array_4[j]];
+
+    while((i++ < 3))
+      ret += '=';
+  }
+
+  return ret;
 }
 
 }  // namespace common_installer