diff options
-rw-r--r-- | CMakeLists.txt | 7 | ||||
-rw-r--r-- | example.cc | 11 | ||||
-rw-r--r-- | src/util/base64.cc | 29 | ||||
-rw-r--r-- | src/util/base64.h | 10 |
4 files changed, 53 insertions, 4 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 2514635..da53cc3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,10 +13,16 @@ include_directories( add_library( DictzipQuery SHARED + src/util/base64.cc src/istream/stream.cc src/istream/buffer.cc ) +target_link_libraries( + DictzipQuery + z +) + add_executable( example example.cc @@ -25,7 +31,6 @@ add_executable( target_link_libraries( example DictzipQuery - z ) install( @@ -1,4 +1,5 @@ #include "istream/stream.h" +#include "util/base64.h" #include <string> #include <iostream> @@ -6,12 +7,16 @@ int main() { dictzip::Istream stream("gcide.dict.dz"); + // Decode location of _Accession_ + const std::size_t offset = dictzip::base64_decode("8Aw"); + const std::size_t length = dictzip::base64_decode("Wt"); + // Print the GCIDE definition of _Accession_ std::string data; - data.reserve(1453); + data.reserve(length); - stream.seekg(245808); - stream.read(const_cast<char*>(data.data()), 1453); + stream.seekg(offset); + stream.read(const_cast<char*>(data.data()), length); std::cout << data.c_str() << std::endl; } 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); + +} |