diff options
author | Adrian Kummerlaender | 2017-10-06 14:26:44 +0200 |
---|---|---|
committer | Adrian Kummerlaender | 2017-10-06 14:26:44 +0200 |
commit | 538236772ce6edde390079c32533c1e70cf8bdca (patch) | |
tree | 1303d22cb092bcf8bb5fadbe4b0ea886eceb1ffa /src/util | |
parent | c953c72c86c281d650b2a8ff856e3d614664e11a (diff) | |
download | DictzipQuery-538236772ce6edde390079c32533c1e70cf8bdca.tar DictzipQuery-538236772ce6edde390079c32533c1e70cf8bdca.tar.gz DictzipQuery-538236772ce6edde390079c32533c1e70cf8bdca.tar.bz2 DictzipQuery-538236772ce6edde390079c32533c1e70cf8bdca.tar.lz DictzipQuery-538236772ce6edde390079c32533c1e70cf8bdca.tar.xz DictzipQuery-538236772ce6edde390079c32533c1e70cf8bdca.tar.zst DictzipQuery-538236772ce6edde390079c32533c1e70cf8bdca.zip |
Implement BASE64 decoding of locations given by dictionary index
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/base64.cc | 29 | ||||
-rw-r--r-- | src/util/base64.h | 10 |
2 files changed, 39 insertions, 0 deletions
diff --git a/src/util/base64.cc b/src/util/base64.cc new file mode 100644 index 0000000..4e8d0a4 --- /dev/null +++ b/src/util/base64.cc @@ -0,0 +1,29 @@ +#include "base64.h" + +#include <vector> +#include <stdexcept> + +namespace dictzip { + +std::size_t base64_decode(const std::string& encoded) { + std::vector<std::int8_t> map(256,-1); + + for ( int i = 0; i < 64; ++i ) { + map["ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[i]] = i; + } + + std::size_t value = 0; + + for ( std::uint8_t c : encoded ) { + if ( map[c] == -1 ) { + throw std::invalid_argument("Invalid character in BASE64 string."); + } else { + value *= 64; + value += map[c]; + } + } + + return value; +} + +} diff --git a/src/util/base64.h b/src/util/base64.h new file mode 100644 index 0000000..448609d --- /dev/null +++ b/src/util/base64.h @@ -0,0 +1,10 @@ +#pragma once + +#include <cstdint> +#include <string> + +namespace dictzip { + +std::size_t base64_decode(const std::string& encoded); + +} |