mirror of
https://github.com/tobiaslocker/base64.git
synced 2024-11-27 07:59:52 +08:00
Initial commit
This commit is contained in:
commit
0a0a1b247c
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
main.cpp
|
||||
build
|
||||
*.user
|
||||
*.swp
|
||||
scripts/build.sh
|
10
CMakeLists.txt
Normal file
10
CMakeLists.txt
Normal 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
40
include/base64.hpp
Normal 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
4
scripts/run-clang-format.sh
Executable 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
|
Loading…
Reference in New Issue
Block a user