Initial commit

This commit is contained in:
tobiaslocker 2019-05-27 23:47:26 +01:00
commit 0a0a1b247c
4 changed files with 59 additions and 0 deletions

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
main.cpp
build
*.user
*.swp
scripts/build.sh

10
CMakeLists.txt Normal file
View File

@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.10)
project(base64)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include_directories(include)
add_executable(${PROJECT_NAME} main.cpp)

40
include/base64.hpp Normal file
View File

@ -0,0 +1,40 @@
#include <algorithm>
#include <string>
std::string to_base64(std::string const &data) {
int counter = 0;
uint32_t bit_stream = 0;
std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
std::string base64;
int offset = 0;
std::for_each(data.begin(), data.end(), [&](unsigned char c) {
auto num_val = static_cast<unsigned int>(c);
offset = 16 - counter % 3 * 8;
bit_stream += num_val << offset;
if (offset == 0 && counter != 3) {
base64 += base64_chars.at(bit_stream >> 18 & 0x3f);
base64 += base64_chars.at(bit_stream >> 12 & 0x3f);
base64 += base64_chars.at(bit_stream >> 6 & 0x3f);
base64 += base64_chars.at(bit_stream & 0x3f);
bit_stream = 0;
}
counter++;
});
if (offset == 16) {
base64 += base64_chars.at(bit_stream >> 18 & 0x3f);
base64 += base64_chars.at(bit_stream >> 12 & 0x3f);
base64 += '=';
base64 += '=';
}
if (offset == 8) {
base64 += base64_chars.at(bit_stream >> 18 & 0x3f);
base64 += base64_chars.at(bit_stream >> 12 & 0x3f);
base64 += base64_chars.at(bit_stream >> 6 & 0x3f);
base64 += '=';
}
return base64;
}

4
scripts/run-clang-format.sh Executable file
View File

@ -0,0 +1,4 @@
#!/bin/bash
#clang-format -verbose -i -style=file src/*.cpp
#clang-format -verbose -i -style=file test/*.cpp
clang-format -verbose -i -style=file include/*.hpp