Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 8da5ce49ef | |||
| b0e645679d | |||
| 6d689cb9b3 | |||
| 119bca6c87 | |||
| 5396abe202 | |||
| 5f2cffdd82 | |||
| 93baa83f87 | |||
| b6780fe572 | |||
| bb98575fb9 | |||
| 98fa49bd06 | |||
| e28d103da0 | |||
| 5b76028d8a | |||
| 9d9b7adcc3 | |||
| c511dd2fa8 | |||
| f6e17b4f7b | |||
| b61d8396f5 | |||
| 53279581aa | |||
| f4b5dc0ce0 | |||
| a5374daa6f | |||
| baf52bb404 | |||
| f47f367045 | |||
| fd660f3b56 | |||
| 78276e8c88 |
41
.github/workflows/docs.yml
vendored
Normal file
41
.github/workflows/docs.yml
vendored
Normal file
@ -0,0 +1,41 @@
|
||||
name: Deploy documentation
|
||||
|
||||
on:
|
||||
release:
|
||||
types: [published]
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
pages: write
|
||||
id-token: write
|
||||
|
||||
concurrency:
|
||||
group: "pages"
|
||||
cancel-in-progress: false
|
||||
|
||||
jobs:
|
||||
deploy:
|
||||
environment:
|
||||
name: github-pages
|
||||
url: ${{ steps.deployment.outputs.page_url }}
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
with:
|
||||
submodules: 'true'
|
||||
- name: Setup Pages
|
||||
uses: actions/configure-pages@v5
|
||||
- name: Install Doxygen
|
||||
run: |
|
||||
wget https://github.com/doxygen/doxygen/releases/download/Release_1_12_0/doxygen-1.12.0.linux.bin.tar.gz
|
||||
tar -xf doxygen-1.12.0.linux.bin.tar.gz
|
||||
- name: Generate docs
|
||||
run: ./doxygen-1.12.0/bin/doxygen
|
||||
- name: Upload artifact
|
||||
uses: actions/upload-pages-artifact@v3
|
||||
with:
|
||||
path: 'doc/html'
|
||||
- name: Deploy to GitHub Pages
|
||||
id: deployment
|
||||
uses: actions/deploy-pages@v4
|
||||
2
.gitignore
vendored
2
.gitignore
vendored
@ -42,6 +42,8 @@ BUILD.bazel
|
||||
bin
|
||||
obj
|
||||
lib
|
||||
doc
|
||||
cov
|
||||
|
||||
# IDE
|
||||
.vscode
|
||||
|
||||
6
.gitmodules
vendored
6
.gitmodules
vendored
@ -0,0 +1,6 @@
|
||||
[submodule "distrib/arch"]
|
||||
path = distrib/arch
|
||||
url = https://aur.archlinux.org/libbasen.git
|
||||
[submodule "deps/doxygen-awesome-css"]
|
||||
path = deps/doxygen-awesome-css
|
||||
url = https://github.com/jothepro/doxygen-awesome-css.git
|
||||
|
||||
4
AUTHORS
4
AUTHORS
@ -1,5 +1,5 @@
|
||||
libbasen authors:
|
||||
libbasen project authors:
|
||||
|
||||
commits | username
|
||||
45 | vSEK1RO
|
||||
62 | vSEK1RO
|
||||
|
||||
|
||||
20
Makefile
20
Makefile
@ -5,7 +5,7 @@ DEBUG ?= false
|
||||
USRDIR ?= /usr
|
||||
|
||||
.PHONY: build i install uni uninstall\
|
||||
tools library tests clean
|
||||
tools library tests docs cov clean
|
||||
|
||||
LIB = basen
|
||||
OBJS =\
|
||||
@ -59,6 +59,8 @@ DIRS =\
|
||||
${OBJDIR}\
|
||||
${OBJDIR}/hash\
|
||||
${LIBDIR}\
|
||||
doc\
|
||||
cov
|
||||
|
||||
build: library tools
|
||||
|
||||
@ -77,8 +79,22 @@ uninstall:
|
||||
rm -f ${patsubst %, ${USRLIB}/lib${LIB}${-g}%, .so .a}
|
||||
rm -f ${patsubst %, ${USRBIN}/%${-g}, ${TOOLS}}
|
||||
|
||||
docs:
|
||||
doxygen Doxyfile
|
||||
|
||||
cover: ${DIRS} ${patsubst %, ${BINDIR}/%${-g}-cov, ${TESTS}}
|
||||
rm -f **/*.gcda
|
||||
${patsubst %, ./${BINDIR}/%${-g}-cov;, ${TESTS}}
|
||||
gcovr --html-nested cov/index.html --txt --exclude-throw-branches
|
||||
|
||||
${OBJDIR}/%${-g}-cov.o: ${SRCDIR}/%.cpp ${INCDIR}/${LIB}/%.hpp
|
||||
${CC} -o $@ -c $< -I${INCDIR} ${-l} ${CFLAGS} --coverage
|
||||
|
||||
${BINDIR}/%${-g}-cov: ${TESTDIR}/%.cpp ${patsubst %, ${OBJDIR}/%${-g}-cov.o, ${OBJS}}
|
||||
${CC} -o $@ $^ -I${INCDIR} ${-l} -lgtest ${CFLAGS} --coverage
|
||||
|
||||
clean:
|
||||
rm -rf ${OBJDIR}/* ${LIBDIR}/* ${BINDIR}/*
|
||||
rm -rf ${DIRS}
|
||||
|
||||
ifneq (${OBJS},)
|
||||
|
||||
|
||||
61
README.md
61
README.md
@ -1,3 +1,60 @@
|
||||
# libbase
|
||||
# libbasen
|
||||
|
||||
encoding/decoding from arbitrary base
|
||||
c++ encoding/decoding from arbitrary base
|
||||
|
||||
<a href="https://repology.org/project/libbasen/versions">
|
||||
<img src="https://repology.org/badge/tiny-repos/libbasen.svg" alt="Packaging status">
|
||||
</a>
|
||||
<a href="https://repology.org/project/libbasen/versions">
|
||||
<img src="https://repology.org/badge/latest-versions/libbasen.svg" alt="latest packaged version(s)">
|
||||
</a>
|
||||
|
||||
## Contents
|
||||
|
||||
- [Installation](#installation)
|
||||
- [Documentation](#documentation)
|
||||
- [Contributing](#contributing)
|
||||
|
||||
## Installation
|
||||
|
||||
Automatically from specified repositories:
|
||||
|
||||
[](https://repology.org/project/libbasen/versions)
|
||||
|
||||
Manually using make:
|
||||
```
|
||||
make -j $(nproc)
|
||||
sudo make i USRDIR=(Your installation dir)
|
||||
```
|
||||
Uninstall:
|
||||
```
|
||||
sudo make uni USRDIR=(Your installation dir)
|
||||
```
|
||||
|
||||
## Documentation
|
||||
|
||||
Available [here](https://vsek1ro.github.io/libbasen)
|
||||
|
||||
## Contributing
|
||||
|
||||
Main branch is under protection rules, so you should create pull request. After merging will be runned [authors.sh](https://github.com/vSEK1RO/libbasen/blob/main/authors.sh) for adding all contributors usernames and their number of commits to [AUTHORS](https://github.com/vSEK1RO/libbasen/blob/main/AUTHORS) file.
|
||||
|
||||
Now we would like to implement the following features:
|
||||
- Base32
|
||||
- BCH
|
||||
- Bech32
|
||||
|
||||
For build with with debug flags:
|
||||
```
|
||||
make -j $(nproc) DEBUG=
|
||||
```
|
||||
For build tests (needed gtest package as dependency):
|
||||
```
|
||||
make tests -j $(nproc) DEBUG=
|
||||
```
|
||||
For generating coverage:
|
||||
```
|
||||
make cover -j $(nproc) DEBUG=
|
||||
```
|
||||
|
||||
[⬆️ Contents](#contents)
|
||||
|
||||
1
deps/doxygen-awesome-css
vendored
Submodule
1
deps/doxygen-awesome-css
vendored
Submodule
Submodule deps/doxygen-awesome-css added at cc1bee0804
1
distrib/arch
Submodule
1
distrib/arch
Submodule
Submodule distrib/arch added at a829d9746a
87
header.html
Normal file
87
header.html
Normal file
@ -0,0 +1,87 @@
|
||||
<!-- HTML header for doxygen 1.12.0-->
|
||||
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" lang="$langISO">
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
||||
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
|
||||
<meta name="generator" content="Doxygen $doxygenversion"/>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1"/>
|
||||
<!--BEGIN PROJECT_NAME--><title>$projectname: $title</title><!--END PROJECT_NAME-->
|
||||
<!--BEGIN !PROJECT_NAME--><title>$title</title><!--END !PROJECT_NAME-->
|
||||
<!--BEGIN PROJECT_ICON-->
|
||||
<link rel="icon" href="$relpath^$projecticon" type="image/x-icon" />
|
||||
<!--END PROJECT_ICON-->
|
||||
<link href="$relpath^tabs.css" rel="stylesheet" type="text/css"/>
|
||||
<!--BEGIN DISABLE_INDEX-->
|
||||
<!--BEGIN FULL_SIDEBAR-->
|
||||
<script type="text/javascript">var page_layout=1;</script>
|
||||
<!--END FULL_SIDEBAR-->
|
||||
<!--END DISABLE_INDEX-->
|
||||
<script type="text/javascript" src="$relpath^jquery.js"></script>
|
||||
<script type="text/javascript" src="$relpath^dynsections.js"></script>
|
||||
<!--BEGIN COPY_CLIPBOARD-->
|
||||
<script type="text/javascript" src="$relpath^clipboard.js"></script>
|
||||
<!--END COPY_CLIPBOARD-->
|
||||
$treeview
|
||||
$search
|
||||
$mathjax
|
||||
$darkmode
|
||||
<link href="$relpath^$stylesheet" rel="stylesheet" type="text/css" />
|
||||
$extrastylesheet
|
||||
<script type="text/javascript" src="$relpath^doxygen-awesome-darkmode-toggle.js"></script>
|
||||
<script type="text/javascript" src="$relpath^doxygen-awesome-fragment-copy-button.js"></script>
|
||||
<script type="text/javascript">
|
||||
DoxygenAwesomeDarkModeToggle.init()
|
||||
DoxygenAwesomeFragmentCopyButton.init()
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<!--BEGIN DISABLE_INDEX-->
|
||||
<!--BEGIN FULL_SIDEBAR-->
|
||||
<div id="side-nav" class="ui-resizable side-nav-resizable"><!-- do not remove this div, it is closed by doxygen! -->
|
||||
<!--END FULL_SIDEBAR-->
|
||||
<!--END DISABLE_INDEX-->
|
||||
|
||||
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
||||
|
||||
<!--BEGIN TITLEAREA-->
|
||||
<a href="https://github.com/vSEK1RO/libbasen" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill: #4779ac; color:#151513; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
|
||||
<div id="titlearea">
|
||||
<table cellspacing="0" cellpadding="0">
|
||||
<tbody>
|
||||
<tr id="projectrow">
|
||||
<!--BEGIN PROJECT_LOGO-->
|
||||
<td id="projectlogo"><img alt="Logo" src="$relpath^$projectlogo"$logosize/></td>
|
||||
<!--END PROJECT_LOGO-->
|
||||
<!--BEGIN PROJECT_NAME-->
|
||||
<td id="projectalign">
|
||||
<div id="projectname">$projectname<!--BEGIN PROJECT_NUMBER--><span id="projectnumber"> $projectnumber</span><!--END PROJECT_NUMBER-->
|
||||
</div>
|
||||
<!--BEGIN PROJECT_BRIEF--><div id="projectbrief">$projectbrief</div><!--END PROJECT_BRIEF-->
|
||||
</td>
|
||||
<!--END PROJECT_NAME-->
|
||||
<!--BEGIN !PROJECT_NAME-->
|
||||
<!--BEGIN PROJECT_BRIEF-->
|
||||
<td>
|
||||
<div id="projectbrief">$projectbrief</div>
|
||||
</td>
|
||||
<!--END PROJECT_BRIEF-->
|
||||
<!--END !PROJECT_NAME-->
|
||||
<!--BEGIN DISABLE_INDEX-->
|
||||
<!--BEGIN SEARCHENGINE-->
|
||||
<!--BEGIN !FULL_SIDEBAR-->
|
||||
<td>$searchbox</td>
|
||||
<!--END !FULL_SIDEBAR-->
|
||||
<!--END SEARCHENGINE-->
|
||||
<!--END DISABLE_INDEX-->
|
||||
</tr>
|
||||
<!--BEGIN SEARCHENGINE-->
|
||||
<!--BEGIN FULL_SIDEBAR-->
|
||||
<tr><td colspan="2">$searchbox</td></tr>
|
||||
<!--END FULL_SIDEBAR-->
|
||||
<!--END SEARCHENGINE-->
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
<!--END TITLEAREA-->
|
||||
<!-- end header part -->
|
||||
@ -7,21 +7,44 @@
|
||||
|
||||
namespace base58
|
||||
{
|
||||
/**
|
||||
* @brief bitcoin alphabet
|
||||
*/
|
||||
extern const char digits[59];
|
||||
extern const int8_t map[256];
|
||||
|
||||
bool isValid(const char *str, uint64_t str_size) noexcept;
|
||||
bool isValid(std::string_view str) noexcept;
|
||||
|
||||
/**
|
||||
* @throw std::overflow_error if if there is an overflow
|
||||
*/
|
||||
uint64_t sizeEncoded(std::span<const uint8_t> data) noexcept;
|
||||
uint64_t sizeDecoded(std::string_view str) noexcept;
|
||||
|
||||
void encode(const uint8_t *data, uint64_t data_size, char *str, uint64_t str_size) noexcept;
|
||||
/**
|
||||
* @return number of leading chars, which should be trimmed
|
||||
* @warning contain leading zeros, returns count of them
|
||||
*/
|
||||
uint64_t encode(const uint8_t *data, uint64_t data_size, char *str, uint64_t str_size) noexcept;
|
||||
std::string encode(std::span<const uint8_t> data) noexcept;
|
||||
|
||||
void decode(const char *str, uint64_t str_size, uint8_t *data, uint64_t data_size) noexcept;
|
||||
/**
|
||||
* @return number of leading chars, which should be trimmed
|
||||
* @warning contain leading zeros, returns count of them
|
||||
*/
|
||||
uint64_t decode(const char *str, uint64_t str_size, uint8_t *data, uint64_t data_size) noexcept;
|
||||
std::vector<uint8_t> decode(std::string_view str) noexcept;
|
||||
|
||||
/**
|
||||
* @param data vector or span of data which you want to encode
|
||||
* @return encoded string + 4 first bytes of double sha256
|
||||
*/
|
||||
std::string encodeCheck(std::span<const uint8_t> data) noexcept;
|
||||
/**
|
||||
* @param str string or string_view which you want to decode
|
||||
* @return decoded data without 4 first bytes of double sha256
|
||||
* @throw std::logic_error checksum incorrect
|
||||
*/
|
||||
std::vector<uint8_t> decodeCheck(std::string_view str);
|
||||
}
|
||||
@ -13,12 +13,25 @@ namespace base64
|
||||
bool isValid(const char *str, uint64_t str_size) noexcept;
|
||||
bool isValid(std::string_view str) noexcept;
|
||||
|
||||
/**
|
||||
* @throw std::overflow_error if if there is an overflow
|
||||
*/
|
||||
uint64_t sizeEncoded(std::span<const uint8_t> data);
|
||||
uint64_t sizeDecoded(std::string_view str) noexcept;
|
||||
|
||||
/**
|
||||
* @throw std::length_error if not enough allocated length
|
||||
* @warning contain leading zeros, returns count of them
|
||||
*/
|
||||
void encode(const uint8_t *data, uint64_t data_size, char *str, uint64_t str_size);
|
||||
std::string encode(std::span<const uint8_t> data) noexcept;
|
||||
|
||||
/**
|
||||
* @throw std::length_error if not enough allocated length
|
||||
* @throw std::logic_error if out of digits map
|
||||
* @throw std::logic_error if incorrect padding
|
||||
* @warning contain leading zeros, returns count of them
|
||||
*/
|
||||
void decode(const char *str, uint64_t str_size, uint8_t *data, uint64_t data_size);
|
||||
std::vector<uint8_t> decode(std::string_view str) noexcept;
|
||||
}
|
||||
@ -7,15 +7,96 @@
|
||||
|
||||
namespace baseN
|
||||
{
|
||||
/**
|
||||
* @param str [in] pointer to string
|
||||
* @param str_size
|
||||
* @param map int8_t[256] array, where at an index equal to the value of the symbol is the index of this symbol in the digits array. -1 if there is no symbol
|
||||
* @return that string doesn't contain unnecessary symbols
|
||||
*/
|
||||
bool isValid(const char *str, uint64_t str_size, const int8_t *map) noexcept;
|
||||
/**
|
||||
* @param str string or string_view which you want to decode
|
||||
* @param map int8_t[256] array, where at an index equal to the value of the symbol is the index of this symbol in the digits array. -1 if there is no symbol
|
||||
* @return that string doesn't contain unnecessary symbols
|
||||
*/
|
||||
bool isValid(std::string_view str, const int8_t *map) noexcept;
|
||||
|
||||
/**
|
||||
* @param data vector or span of data which you want to encode
|
||||
* @param base from 1 to 255
|
||||
* @return estimated size after encoding
|
||||
*/
|
||||
uint64_t sizeEncoded(std::span<const uint8_t> data, uint8_t base);
|
||||
uint64_t sizeDecoded(std::string_view str, uint8_t base, const char* digits) noexcept;
|
||||
/**
|
||||
* @param str string or string_view which you want to decode
|
||||
* @param base from 1 to 255
|
||||
* @param digits char[base] array of digits
|
||||
* @return estimated size after decoding
|
||||
* @throw std::overflow_error if if there is an overflow
|
||||
*/
|
||||
uint64_t sizeDecoded(std::string_view str, uint8_t base, const char *digits) noexcept;
|
||||
|
||||
/**
|
||||
* @param data [in] pointer to data which you want encode
|
||||
* @param data_size
|
||||
* @param str [out] pointer to string for encoded data output
|
||||
* @param str_size
|
||||
* @param base from 1 to 255
|
||||
* @param digits char[base] array of digits
|
||||
* @code{cpp}
|
||||
* std::vector<uint8_t> data;
|
||||
* std::string str(baseN::sizeEncoded(data, 58), ' ');
|
||||
*
|
||||
* auto offset = baseN::encode(data.data(), data.size(), str.data(), str.size(), 58, base58::digits);
|
||||
* // deleting leading zeroes
|
||||
* str.erase(str.begin(), str.begin() + offset);
|
||||
* @endcode
|
||||
* @return number of leading chars, which should be trimmed
|
||||
* @warning contain leading zeros, returns count of them
|
||||
*/
|
||||
uint64_t encode(const uint8_t *data, uint64_t data_size, char *str, uint64_t str_size, uint8_t base, const char *digits);
|
||||
/**
|
||||
* @param data vector or span of data which you want to encode
|
||||
* @param base from 1 to 255
|
||||
* @param digits char[base] array of digits
|
||||
* @code{cpp}
|
||||
* std::vector<uint8_t> data;
|
||||
* auto str = baseN::encode(data, 58, base58::digits);
|
||||
* @endcode
|
||||
* @return encoded string
|
||||
*/
|
||||
std::string encode(std::span<const uint8_t> data, uint8_t base, const char *digits) noexcept;
|
||||
|
||||
/**
|
||||
* @param str [in] pointer to string which you want decode
|
||||
* @param str_size
|
||||
* @param data [out] pointer to data for encoded string output
|
||||
* @param data_size
|
||||
* @param base from 1 to 255
|
||||
* @param digits char[base] array of digits
|
||||
* @param map int8_t[256] array, where at an index equal to the value of the symbol is the index of this symbol in the digits array. -1 if there is no symbol
|
||||
* @code{cpp}
|
||||
* std::string str;
|
||||
* std::vector<uint8_t> data(baseN::sizeDecoded(str, 58));
|
||||
*
|
||||
* auto offset = baseN::decode(str.data(), str.size(), data.data(), data.size(), 58, base58::digits, base58::map);
|
||||
* // deleting leading zeroes
|
||||
* data.erase(data.begin(), data.begin() + offset);
|
||||
* @endcode
|
||||
* @return number of leading chars, which should be trimmed
|
||||
* @warning contain leading zeros, returns count of them
|
||||
*/
|
||||
uint64_t decode(const char *str, uint64_t str_size, uint8_t *data, uint64_t data_size, uint8_t base, const char *digits, const int8_t *map);
|
||||
/**
|
||||
* @param str string or string_view which you want to decode
|
||||
* @param base from 1 to 255
|
||||
* @param digits char[base] array of digits
|
||||
* @param map int8_t[256] array, where at an index equal to the value of the symbol is the index of this symbol in the digits array. -1 if there is no symbol
|
||||
* @code{cpp}
|
||||
* std::string str;
|
||||
* auto data = baseN::decode(str, 58, base58::digits, base58::map);
|
||||
* @endcode
|
||||
* @return decoded data
|
||||
*/
|
||||
std::vector<uint8_t> decode(std::string_view str, uint8_t base, const char *digits, const int8_t *map) noexcept;
|
||||
}
|
||||
@ -4,9 +4,8 @@
|
||||
#include <span>
|
||||
#include <vector>
|
||||
|
||||
#define SHA256_DIGEST_LENGTH 32
|
||||
|
||||
namespace hash {
|
||||
namespace hash
|
||||
{
|
||||
void sha256(const uint8_t *data, uint64_t data_size, uint8_t *hash) noexcept;
|
||||
std::vector<uint8_t> sha256(std::span<const uint8_t> data) noexcept;
|
||||
}
|
||||
@ -13,12 +13,25 @@ namespace hex
|
||||
bool isValid(const char *str, uint64_t str_size) noexcept;
|
||||
bool isValid(std::string_view str) noexcept;
|
||||
|
||||
/**
|
||||
* @throw std::overflow_error if if there is an overflow
|
||||
*/
|
||||
uint64_t sizeEncoded(std::span<const uint8_t> data);
|
||||
uint64_t sizeDecoded(std::string_view str) noexcept;
|
||||
|
||||
/**
|
||||
* @throw std::length_error if not enough allocated length
|
||||
* @warning contain leading zeros, returns count of them
|
||||
*/
|
||||
void encode(const uint8_t *data, uint64_t data_size, char *str, uint64_t str_size);
|
||||
std::string encode(std::span<const uint8_t> data) noexcept;
|
||||
|
||||
/**
|
||||
* @throw std::length_error if not enough allocated length
|
||||
* @throw std::logic_error if out of digits map
|
||||
* @throw std::logic_error if str_size %2 != 0 (isn't hex)
|
||||
* @warning contain leading zeros, returns count of them
|
||||
*/
|
||||
void decode(const char *str, uint64_t str_size, uint8_t *data, uint64_t data_size);
|
||||
std::vector<uint8_t> decode(std::string_view str) noexcept;
|
||||
}
|
||||
@ -43,17 +43,17 @@ namespace base58
|
||||
{
|
||||
return baseN::sizeDecoded(str, 58, digits);
|
||||
}
|
||||
void encode(const uint8_t *data, uint64_t data_size, char *str, uint64_t str_size) noexcept
|
||||
uint64_t encode(const uint8_t *data, uint64_t data_size, char *str, uint64_t str_size) noexcept
|
||||
{
|
||||
baseN::encode(data, data_size, str, str_size, 58, digits);
|
||||
return baseN::encode(data, data_size, str, str_size, 58, digits);
|
||||
}
|
||||
std::string encode(std::span<const uint8_t> data) noexcept
|
||||
{
|
||||
return baseN::encode(data, 58, digits);
|
||||
}
|
||||
void decode(const char *str, uint64_t str_size, uint8_t *data, uint64_t data_size) noexcept
|
||||
uint64_t decode(const char *str, uint64_t str_size, uint8_t *data, uint64_t data_size) noexcept
|
||||
{
|
||||
baseN::decode(str, str_size, data, data_size, 58, digits, map);
|
||||
return baseN::decode(str, str_size, data, data_size, 58, digits, map);
|
||||
}
|
||||
std::vector<uint8_t> decode(std::string_view str) noexcept
|
||||
{
|
||||
|
||||
@ -64,7 +64,7 @@ namespace base64
|
||||
{
|
||||
if (str_size < base64::sizeEncoded(std::span<const uint8_t>(data, data_size)))
|
||||
{
|
||||
throw std::logic_error("base64::encode: not enough allocated length");
|
||||
throw std::length_error("base64::encode: not enough allocated length");
|
||||
}
|
||||
for (uint64_t i = 0; i < data_size / 3; i++)
|
||||
{
|
||||
@ -74,25 +74,22 @@ namespace base64
|
||||
str[i * 4 + 3] = digits[data[i * 3 + 2] & 0x3F];
|
||||
}
|
||||
uint64_t last_idx = data_size / 3 * 4;
|
||||
if (last_idx + 3 < str_size)
|
||||
switch (data_size % 3)
|
||||
{
|
||||
switch (data_size % 3)
|
||||
{
|
||||
case 1:
|
||||
str[last_idx] = digits[data[data_size - 1] >> 2];
|
||||
str[last_idx + 1] = digits[data[data_size - 1] << 4 & 0x30];
|
||||
str[last_idx + 2] = '=';
|
||||
str[last_idx + 3] = '=';
|
||||
break;
|
||||
case 2:
|
||||
str[last_idx] = digits[data[data_size - 2] >> 2];
|
||||
str[last_idx + 1] = digits[(data[data_size - 2] << 4 | data[data_size - 1] >> 4) & 0x3F];
|
||||
str[last_idx + 2] = digits[data[data_size - 1] & 0x0F];
|
||||
str[last_idx + 3] = '=';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
case 1:
|
||||
str[last_idx] = digits[data[data_size - 1] >> 2];
|
||||
str[last_idx + 1] = digits[data[data_size - 1] << 4 & 0x30];
|
||||
str[last_idx + 2] = '=';
|
||||
str[last_idx + 3] = '=';
|
||||
break;
|
||||
case 2:
|
||||
str[last_idx] = digits[data[data_size - 2] >> 2];
|
||||
str[last_idx + 1] = digits[(data[data_size - 2] << 4 | data[data_size - 1] >> 4) & 0x3F];
|
||||
str[last_idx + 2] = digits[data[data_size - 1] & 0x0F];
|
||||
str[last_idx + 3] = '=';
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
std::string encode(std::span<const uint8_t> data) noexcept
|
||||
@ -106,7 +103,7 @@ namespace base64
|
||||
std::string_view sv(str, str_size);
|
||||
if (data_size < base64::sizeDecoded(sv))
|
||||
{
|
||||
throw std::logic_error("base64::decode: not enough allocated length");
|
||||
throw std::length_error("base64::decode: not enough allocated length");
|
||||
}
|
||||
if (!base64::isValid(sv))
|
||||
{
|
||||
|
||||
@ -174,7 +174,7 @@ namespace hash
|
||||
}
|
||||
std::vector<uint8_t> sha256(std::span<const uint8_t> data) noexcept
|
||||
{
|
||||
std::vector<uint8_t> hash(SHA256_DIGEST_LENGTH);
|
||||
std::vector<uint8_t> hash(32);
|
||||
sha256(data.data(), data.size(), hash.data());
|
||||
return hash;
|
||||
}
|
||||
|
||||
@ -50,7 +50,7 @@ namespace hex
|
||||
{
|
||||
if (str_size < hex::sizeEncoded(std::span<const uint8_t>(data, data_size)))
|
||||
{
|
||||
throw std::logic_error("hex::encode: not enough allocated length");
|
||||
throw std::length_error("hex::encode: not enough allocated length");
|
||||
}
|
||||
for (uint64_t i = 0; i < data_size; i++)
|
||||
{
|
||||
@ -72,7 +72,7 @@ namespace hex
|
||||
}
|
||||
if (data_size < hex::sizeDecoded(std::string_view(str, str_size)))
|
||||
{
|
||||
throw std::logic_error("hex::decode: not enough allocated length");
|
||||
throw std::length_error("hex::decode: not enough allocated length");
|
||||
}
|
||||
if (!hex::isValid(str, str_size))
|
||||
{
|
||||
|
||||
@ -28,6 +28,12 @@ TEST(base64, encode)
|
||||
{
|
||||
for (auto it : tests)
|
||||
EXPECT_EQ(it.first, encode(hex::decode(it.second)));
|
||||
|
||||
std::vector<uint8_t> data = {0x74, 0x65, 0x73, 0x74};
|
||||
std::string str = "";
|
||||
EXPECT_THROW(encode(data.data(), std::numeric_limits<uint64_t>::max(), str.data(), str.size()), std::overflow_error);
|
||||
EXPECT_THROW(encode(data.data(), data.size(), str.data(), str.size()), std::length_error);
|
||||
EXPECT_NO_THROW(encode(data.data(), 0, str.data(), str.size()));
|
||||
}
|
||||
TEST(base64, encode_1e7)
|
||||
{
|
||||
@ -38,6 +44,12 @@ TEST(base64, decode)
|
||||
{
|
||||
for (auto it : tests)
|
||||
EXPECT_EQ(hex::encode(decode(it.first)), it.second);
|
||||
|
||||
std::vector<uint8_t> data = {0x61, 0x6e, 0x6f};
|
||||
EXPECT_THROW(decode("FFF", 3, data.data(), data.size()), std::logic_error);
|
||||
EXPECT_THROW(decode("!@#!", 4, data.data(), data.size()), std::logic_error);
|
||||
EXPECT_THROW(decode("FF==", 2, data.data(), 0), std::length_error);
|
||||
EXPECT_NO_THROW(decode("" , 0, data.data(), 0));
|
||||
}
|
||||
TEST(base64, decode_1e7)
|
||||
{
|
||||
|
||||
@ -1,21 +1,28 @@
|
||||
#include <utility>
|
||||
|
||||
#include <basen/hex.hpp>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using namespace hex;
|
||||
|
||||
TEST(hex, isValid)
|
||||
{
|
||||
std::vector<std::pair<bool, std::string>> tests = {
|
||||
{true, "1234"},
|
||||
{false, "!@/"},
|
||||
};
|
||||
for (auto it : tests)
|
||||
EXPECT_EQ(it.first, isValid(it.second));
|
||||
}
|
||||
TEST(hex, encode)
|
||||
{
|
||||
std::vector<uint8_t> data = {0x74, 0x65, 0x73, 0x74};
|
||||
EXPECT_EQ("74657374", encode(data));
|
||||
try
|
||||
{
|
||||
std::string str = "";
|
||||
encode(data.data(), data.size(), str.data(), str.size());
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
EXPECT_STREQ(e.what(), "hex::encode: not enough allocated length");
|
||||
}
|
||||
|
||||
std::string str = "";
|
||||
EXPECT_THROW(encode(data.data(), std::numeric_limits<uint64_t>::max(), str.data(), str.size()), std::overflow_error);
|
||||
EXPECT_THROW(encode(data.data(), data.size(), str.data(), str.size()), std::length_error);
|
||||
EXPECT_NO_THROW(encode(data.data(), 0, str.data(), str.size()));
|
||||
}
|
||||
TEST(hex, encode_1e7)
|
||||
{
|
||||
@ -26,15 +33,11 @@ TEST(hex, decode)
|
||||
{
|
||||
std::vector<uint8_t> data = {0x61, 0x6e, 0x6f};
|
||||
EXPECT_EQ(decode("616e6f"), data);
|
||||
try
|
||||
{
|
||||
std::string str = "";
|
||||
decode("616", 3, data.data(), data.size());
|
||||
}
|
||||
catch (const std::exception &e)
|
||||
{
|
||||
EXPECT_STREQ(e.what(), "hex::decode: isn't hex");
|
||||
}
|
||||
|
||||
EXPECT_THROW(decode("FFF", 3, data.data(), data.size()), std::logic_error);
|
||||
EXPECT_THROW(decode("!@#!", 4, data.data(), data.size()), std::logic_error);
|
||||
EXPECT_THROW(decode("FF", 2, data.data(), 0), std::length_error);
|
||||
EXPECT_NO_THROW(decode("", 0, data.data(), 0));
|
||||
}
|
||||
TEST(hex, decode_1e7)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user