Browse Source

Base64 encoding/decoding on daemon

anon 4 years ago
parent
commit
34bdf0aaf3
4 changed files with 50 additions and 1 deletions
  1. 1 1
      daemon/CMakeLists.txt
  2. 26 0
      daemon/include/base64.h
  3. 2 0
      daemon/src/Server.cpp
  4. 21 0
      daemon/src/base64.cpp

+ 1 - 1
daemon/CMakeLists.txt

@@ -5,7 +5,7 @@ set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
 
 project(ccats)
 
-add_executable(ccats src/main.cpp src/Sniffer.cpp src/Server.cpp)
+add_executable(ccats src/main.cpp src/Sniffer.cpp src/Server.cpp src/base64.cpp)
 
 # use pkg-config to fix building on debian unstable
 find_package(PkgConfig REQUIRED)

+ 26 - 0
daemon/include/base64.h

@@ -0,0 +1,26 @@
+#ifndef BASE64_H
+#define BASE64_H
+
+#include <string>
+
+namespace base64 {
+  /**
+   * Decodes base64 encoded strings.
+   *
+   * @param val base64 encoded string
+   *
+   * @return normal string
+   */
+  std::string decode(const std::string &val);
+
+  /**
+   * Encodes base64 encoded strings.
+   *
+   * @param val normal string
+   *
+   * @return base64 encoded string
+   */
+  std::string encode(const std::string &val);
+}
+
+#endif

+ 2 - 0
daemon/src/Server.cpp

@@ -1,4 +1,6 @@
 #include "../include/Server.h"
+#include "../include/base64.h"
+
 #include <iostream>
 #include <jsoncpp/json/json.h>
 

+ 21 - 0
daemon/src/base64.cpp

@@ -0,0 +1,21 @@
+#include "../include/base64.h"
+
+#include <boost/archive/iterators/binary_from_base64.hpp>
+#include <boost/archive/iterators/base64_from_binary.hpp>
+#include <boost/archive/iterators/transform_width.hpp>
+#include <boost/algorithm/string.hpp>
+
+std::string base64::decode(const std::string &val) {
+  using namespace boost::archive::iterators;
+  using It = transform_width<binary_from_base64<std::string::const_iterator>, 8, 6>;
+  return boost::algorithm::trim_right_copy_if(std::string(It(std::begin(val)), It(std::end(val))), [](char c) {
+    return c == '\0';
+  });
+}
+
+std::string base64::encode(const std::string &val) {
+  using namespace boost::archive::iterators;
+  using It = base64_from_binary<transform_width<std::string::const_iterator, 6, 8>>;
+  auto tmp = std::string(It(std::begin(val)), It(std::end(val)));
+  return tmp.append((3 - val.size() % 3) % 3, '=');
+}