Compare commits
5 Commits
470c018828
...
19ad6abf83
| Author | SHA1 | Date | |
|---|---|---|---|
| 19ad6abf83 | |||
| 62d651fc6c | |||
| 029efbbf5e | |||
| f7cdbf4882 | |||
| 82a9243e2b |
2
.gitignore
vendored
2
.gitignore
vendored
@ -39,11 +39,13 @@ WORKSPACE.bazel
|
|||||||
BUILD.bazel
|
BUILD.bazel
|
||||||
|
|
||||||
# Dirs
|
# Dirs
|
||||||
|
build
|
||||||
bin
|
bin
|
||||||
obj
|
obj
|
||||||
lib
|
lib
|
||||||
doc
|
doc
|
||||||
cov
|
cov
|
||||||
|
usr
|
||||||
|
|
||||||
# IDE
|
# IDE
|
||||||
.vscode
|
.vscode
|
||||||
|
|||||||
33
CMakeLists.txt
Normal file
33
CMakeLists.txt
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.21)
|
||||||
|
set(CMAKE_CXX_STANDARD 20)
|
||||||
|
project(libbasen
|
||||||
|
VERSION 1.1.1
|
||||||
|
DESCRIPTION "c++20 encoding/decoding from arbitrary base"
|
||||||
|
LANGUAGES CXX
|
||||||
|
)
|
||||||
|
|
||||||
|
set(CXX_FLAGS "-Wall -Wextra -Werror -Wno-unused-result -O3")
|
||||||
|
|
||||||
|
if(DEFINED BASEN_SHARED_LIBS)
|
||||||
|
set(BUILD_SHARED_LIBS ${BASEN_SHARED_LIBS})
|
||||||
|
endif()
|
||||||
|
|
||||||
|
set(OBJS
|
||||||
|
base58
|
||||||
|
base64
|
||||||
|
baseN
|
||||||
|
hex
|
||||||
|
Exception
|
||||||
|
hash/sha256
|
||||||
|
)
|
||||||
|
set(SRCS)
|
||||||
|
foreach(OBJ ${OBJS})
|
||||||
|
list(APPEND SRCS "src/${OBJ}.cpp")
|
||||||
|
endforeach()
|
||||||
|
|
||||||
|
add_library(basen ${SRCS})
|
||||||
|
add_library(basen::basen ALIAS basen)
|
||||||
|
target_include_directories(basen PUBLIC
|
||||||
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||||
|
$<INSTALL_INTERFACE:include>
|
||||||
|
)
|
||||||
16
Makefile
16
Makefile
@ -8,6 +8,9 @@ USRDIR ?= /usr
|
|||||||
tools library tests docs cov clean
|
tools library tests docs cov clean
|
||||||
|
|
||||||
LIB = basen
|
LIB = basen
|
||||||
|
|
||||||
|
TOOLS = ${LIB}
|
||||||
|
|
||||||
OBJS =\
|
OBJS =\
|
||||||
hex\
|
hex\
|
||||||
baseN\
|
baseN\
|
||||||
@ -16,8 +19,6 @@ OBJS =\
|
|||||||
hash/sha256\
|
hash/sha256\
|
||||||
Exception
|
Exception
|
||||||
|
|
||||||
TOOLS = ${LIB}
|
|
||||||
|
|
||||||
TESTS =\
|
TESTS =\
|
||||||
test-hex\
|
test-hex\
|
||||||
test-baseN\
|
test-baseN\
|
||||||
@ -30,7 +31,7 @@ ifeq (${origin CC}, default)
|
|||||||
CC = g++
|
CC = g++
|
||||||
endif
|
endif
|
||||||
|
|
||||||
CFLAGS = -std=c++23 -Wall -Wextra -Werror -Wno-unused-result -fPIC
|
CFLAGS = -std=c++20 -Wall -Wextra -Werror -Wno-unused-result -fPIC
|
||||||
|
|
||||||
ifneq (${DEBUG}, false)
|
ifneq (${DEBUG}, false)
|
||||||
CFLAGS += -fsanitize=address,undefined -g -O0
|
CFLAGS += -fsanitize=address,undefined -g -O0
|
||||||
@ -58,6 +59,7 @@ USRINC = ${USRDIR}/include
|
|||||||
DIRS =\
|
DIRS =\
|
||||||
${BINDIR}\
|
${BINDIR}\
|
||||||
${BINDIR}/hash\
|
${BINDIR}/hash\
|
||||||
|
${BINDIR}/tools\
|
||||||
${OBJDIR}\
|
${OBJDIR}\
|
||||||
${OBJDIR}/hash\
|
${OBJDIR}/hash\
|
||||||
${LIBDIR}\
|
${LIBDIR}\
|
||||||
@ -119,19 +121,19 @@ ${USRLIB}/lib${LIB}${-g}%: ${LIBDIR}/lib${LIB}${-g}%
|
|||||||
endif
|
endif
|
||||||
ifneq (${TOOLS},)
|
ifneq (${TOOLS},)
|
||||||
|
|
||||||
tools: library ${DIRS} ${patsubst %, ${BINDIR}/%${-g}, ${TOOLS}}
|
tools: library ${DIRS} ${patsubst %, ${BINDIR}/tools/%${-g}, ${TOOLS}}
|
||||||
|
|
||||||
${BINDIR}/%${-g}: ${SRCDIR}/%.cpp ${patsubst %, ${OBJDIR}/%${-g}.o, ${OBJS}}
|
${BINDIR}/tools/%${-g}: ${SRCDIR}/tools/%.cpp ${patsubst %, ${OBJDIR}/%${-g}.o, ${OBJS}}
|
||||||
${CC} -o $@ $< -I${INCDIR} -L${LIBDIR} ${-l} ${-lLIB} ${CFLAGS}
|
${CC} -o $@ $< -I${INCDIR} -L${LIBDIR} ${-l} ${-lLIB} ${CFLAGS}
|
||||||
|
|
||||||
${USRBIN}/%${-g}: ${BINDIR}/%${-g}
|
${USRBIN}/%${-g}: ${BINDIR}/tools/%${-g}
|
||||||
install -Dm755 $< $@
|
install -Dm755 $< $@
|
||||||
|
|
||||||
endif
|
endif
|
||||||
ifneq (${TESTS},)
|
ifneq (${TESTS},)
|
||||||
|
|
||||||
tests: library ${DIRS} ${patsubst %, ${BINDIR}/%${-g}, ${TESTS}}
|
tests: library ${DIRS} ${patsubst %, ${BINDIR}/%${-g}, ${TESTS}}
|
||||||
${patsubst %, ./${BINDIR}/%${-g};, ${TESTS}}
|
echo ${patsubst %, && ./${BINDIR}/%${-g}, ${TESTS}}
|
||||||
|
|
||||||
${BINDIR}/%${-g}: ${TESTDIR}/%.cpp ${patsubst %, ${OBJDIR}/%${-g}.o, ${OBJS}}
|
${BINDIR}/%${-g}: ${TESTDIR}/%.cpp ${patsubst %, ${OBJDIR}/%${-g}.o, ${OBJS}}
|
||||||
${CC} -o $@ $< -I${INCDIR} -L${LIBDIR} ${-l} ${-lLIB} -lgtest ${CFLAGS}
|
${CC} -o $@ $< -I${INCDIR} -L${LIBDIR} ${-l} ${-lLIB} -lgtest ${CFLAGS}
|
||||||
|
|||||||
30
README.md
30
README.md
@ -1,6 +1,6 @@
|
|||||||
# libbasen
|
# libbasen
|
||||||
|
|
||||||
c++ encoding/decoding from arbitrary base
|
c++20 encoding/decoding from arbitrary base
|
||||||
|
|
||||||
<a href="https://repology.org/project/libbasen/versions">
|
<a href="https://repology.org/project/libbasen/versions">
|
||||||
<img src="https://repology.org/badge/tiny-repos/libbasen.svg" alt="Packaging status">
|
<img src="https://repology.org/badge/tiny-repos/libbasen.svg" alt="Packaging status">
|
||||||
@ -12,17 +12,24 @@ c++ encoding/decoding from arbitrary base
|
|||||||
## Contents
|
## Contents
|
||||||
|
|
||||||
- [Installation](#installation)
|
- [Installation](#installation)
|
||||||
|
- [Package](#package)
|
||||||
|
- [Make](#make)
|
||||||
|
- [CMake](#cmake)
|
||||||
- [Documentation](#documentation)
|
- [Documentation](#documentation)
|
||||||
- [Usage](#usage)
|
- [Usage](#usage)
|
||||||
- [Contributing](#contributing)
|
- [Contributing](#contributing)
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
Automatically from specified repositories:
|
### Package
|
||||||
|
|
||||||
[](https://repology.org/project/libbasen/versions)
|
[](https://repology.org/project/libbasen/versions)
|
||||||
|
|
||||||
Manually using make:
|
### Make
|
||||||
|
|
||||||
|
For cli tool you should have [argparse](https://github.com/p-ranav/argparse) as make dependency.
|
||||||
|
|
||||||
|
Install:
|
||||||
```
|
```
|
||||||
make -j $(nproc)
|
make -j $(nproc)
|
||||||
sudo make i USRDIR=(Your installation dir)
|
sudo make i USRDIR=(Your installation dir)
|
||||||
@ -32,6 +39,23 @@ Uninstall:
|
|||||||
sudo make uni USRDIR=(Your installation dir)
|
sudo make uni USRDIR=(Your installation dir)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### CMake
|
||||||
|
|
||||||
|
For using as dependency only (without cli tool, test, coverage, docs)
|
||||||
|
|
||||||
|
```
|
||||||
|
set(BASEN_SHARED_LIBS ON)
|
||||||
|
include(FetchContent)
|
||||||
|
FetchContent_Declare(
|
||||||
|
basen
|
||||||
|
GIT_REPOSITORY https://github.com/vSEK1RO/libbasen.git
|
||||||
|
GIT_TAG latest
|
||||||
|
)
|
||||||
|
FetchContent_MakeAvailable(basen)
|
||||||
|
|
||||||
|
target_link_libraries(MyProject PRIVATE basen::basen)
|
||||||
|
```
|
||||||
|
|
||||||
## Documentation
|
## Documentation
|
||||||
|
|
||||||
Available [here](https://vsek1ro.github.io/libbasen)
|
Available [here](https://vsek1ro.github.io/libbasen)
|
||||||
|
|||||||
Submodule distrib/arch updated: 3453e4c5cd...fb10835e09
@ -13,39 +13,72 @@ namespace base58
|
|||||||
extern const char digits[59];
|
extern const char digits[59];
|
||||||
extern const uint8_t map[256];
|
extern const uint8_t map[256];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return checks what contains only alphabet chars
|
||||||
|
* @warning doesn't validate checksum, use decodeCheck instead
|
||||||
|
*/
|
||||||
bool isValid(const char *str, size_t str_size) noexcept;
|
bool isValid(const char *str, size_t str_size) noexcept;
|
||||||
|
/**
|
||||||
|
* @param str string or string_view which you want to decode
|
||||||
|
* @return checks what contains only alphabet chars
|
||||||
|
* @warning doesn't validate checksum, use decodeCheck instead
|
||||||
|
*/
|
||||||
bool isValid(std::string_view str) noexcept;
|
bool isValid(std::string_view str) noexcept;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throw basen::Exception(OVERFLOW) if there is an overflow
|
* @param data vector or span of data which you want to encode
|
||||||
|
* @return estimated size after encoding (for non-powers of two it is obviously larger)
|
||||||
|
* @throw basen::Exception(OVERFLOW) from baseN::sizeEncoded
|
||||||
*/
|
*/
|
||||||
size_t sizeEncoded(std::span<const uint8_t> data);
|
size_t sizeEncoded(std::span<const uint8_t> data);
|
||||||
|
/**
|
||||||
|
* @param str string or string_view which you want to decode
|
||||||
|
* @return estimated size after decoding (for non-powers of two it is obviously larger)
|
||||||
|
*/
|
||||||
size_t sizeDecoded(std::string_view str) noexcept;
|
size_t sizeDecoded(std::string_view str) noexcept;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param data [in] pointer to data which you want encode
|
||||||
|
* @param str [out] pointer to string for encoded data output
|
||||||
* @return number of leading chars, which should be trimmed
|
* @return number of leading chars, which should be trimmed
|
||||||
|
* @throw basen::Exception(OVERFLOW) from base58::sizeEncoded
|
||||||
* @warning contain leading zeros, returns count of them
|
* @warning contain leading zeros, returns count of them
|
||||||
*/
|
*/
|
||||||
size_t encode(const uint8_t *data, size_t data_size, char *str, size_t str_size);
|
size_t encode(const uint8_t *data, size_t data_size, char *str, size_t str_size);
|
||||||
|
/**
|
||||||
|
* @param data vector or span of data which you want to encode
|
||||||
|
* @return encoded string
|
||||||
|
* @throw basen::Exception(OVERFLOW) from base58::sizeEncoded
|
||||||
|
*/
|
||||||
std::string encode(std::span<const uint8_t> data);
|
std::string encode(std::span<const uint8_t> data);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param str [in] pointer to string which you want decode
|
||||||
|
* @param data [out] pointer to data for encoded string output
|
||||||
* @return number of leading chars, which should be trimmed
|
* @return number of leading chars, which should be trimmed
|
||||||
|
* @throw basen::Exception(OUT_OF_ALPH) from baseN::decode
|
||||||
* @warning contain leading zeros, returns count of them
|
* @warning contain leading zeros, returns count of them
|
||||||
*/
|
*/
|
||||||
size_t decode(const char *str, size_t str_size, uint8_t *data, size_t data_size);
|
size_t decode(const char *str, size_t str_size, uint8_t *data, size_t data_size);
|
||||||
|
/**
|
||||||
|
* @param str string or string_view which you want to decode
|
||||||
|
* @return decoded data
|
||||||
|
* @throw basen::Exception(OUT_OF_ALPH) from baseN::decode
|
||||||
|
*/
|
||||||
std::vector<uint8_t> decode(std::string_view str);
|
std::vector<uint8_t> decode(std::string_view str);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param data vector or span of data which you want to encode
|
* @param data vector or span of data which you want to encode
|
||||||
* @return encoded string + 4 first bytes of double sha256
|
* @return encoded string + 4 first bytes of double sha256
|
||||||
|
* @throw basen::Exception(OVERFLOW) from base58::encode
|
||||||
*/
|
*/
|
||||||
std::string encodeCheck(std::span<const uint8_t> data);
|
std::string encodeCheck(std::span<const uint8_t> data);
|
||||||
/**
|
/**
|
||||||
* @param str string or string_view which you want to decode
|
* @param str encoded string or string_view + 4 first bytes of double sha256
|
||||||
* @return decoded data without 4 first bytes of double sha256
|
* @return decoded data without 4 first bytes of double sha256
|
||||||
|
* @throw basen::Exception(OUT_OF_ALPH) from base58::decode
|
||||||
* @throw basen::Exception(PADDING) if str size < 4
|
* @throw basen::Exception(PADDING) if str size < 4
|
||||||
* @throw basen::Exception(CHECKSUM) checksum incorrect
|
* @throw basen::Exception(CHECKSUM) if checksum incorrect
|
||||||
*/
|
*/
|
||||||
std::vector<uint8_t> decodeCheck(std::string_view str);
|
std::vector<uint8_t> decodeCheck(std::string_view str);
|
||||||
}
|
}
|
||||||
@ -10,28 +10,57 @@ namespace base64
|
|||||||
extern const char digits[65];
|
extern const char digits[65];
|
||||||
extern const uint8_t map[256];
|
extern const uint8_t map[256];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return checks what contains only alphabet chars and ends with no more than two '=' chars
|
||||||
|
*/
|
||||||
bool isValid(const char *str, size_t str_size) noexcept;
|
bool isValid(const char *str, size_t str_size) noexcept;
|
||||||
|
/**
|
||||||
|
* @param str string or string_view which you want to decode
|
||||||
|
* @return checks what contains only alphabet chars and ends with no more than two '=' chars
|
||||||
|
*/
|
||||||
bool isValid(std::string_view str) noexcept;
|
bool isValid(std::string_view str) noexcept;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param data vector or span of data which you want to encode
|
||||||
|
* @return exact size after encoding
|
||||||
* @throw basen::Exception(OVERFLOW) if there is an overflow
|
* @throw basen::Exception(OVERFLOW) if there is an overflow
|
||||||
*/
|
*/
|
||||||
size_t sizeEncoded(std::span<const uint8_t> data);
|
size_t sizeEncoded(std::span<const uint8_t> data);
|
||||||
|
/**
|
||||||
|
* @param str string or string_view which you want to decode
|
||||||
|
* @return exact size after decoding
|
||||||
|
*/
|
||||||
size_t sizeDecoded(std::string_view str) noexcept;
|
size_t sizeDecoded(std::string_view str) noexcept;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param data [in] pointer to data which you want encode
|
||||||
|
* @param str [out] pointer to string for encoded data output
|
||||||
|
* @throw basen::Exception(OVERFLOW) from base64::sizeEncoded
|
||||||
* @throw basen::Exception(LENGTH) if not enough allocated length
|
* @throw basen::Exception(LENGTH) if not enough allocated length
|
||||||
* @warning contain leading zeros, returns count of them
|
|
||||||
*/
|
*/
|
||||||
void encode(const uint8_t *data, size_t data_size, char *str, size_t str_size);
|
void encode(const uint8_t *data, size_t data_size, char *str, size_t str_size);
|
||||||
|
/**
|
||||||
|
* @param data vector or span of data which you want to encode
|
||||||
|
* @return encoded string
|
||||||
|
* @throw basen::Exception(OVERFLOW) from base64::sizeEncoded
|
||||||
|
* @throw basen::Exception(LENGTH) if not enough allocated length
|
||||||
|
*/
|
||||||
std::string encode(std::span<const uint8_t> data);
|
std::string encode(std::span<const uint8_t> data);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param str [in] pointer to string which you want decode
|
||||||
|
* @param data [out] pointer to data for encoded string output
|
||||||
* @throw basen::Exception(LENGTH) if not enough allocated length
|
* @throw basen::Exception(LENGTH) if not enough allocated length
|
||||||
* @throw basen::Exception(OUT_OF_ALPH) if out of digits map
|
* @throw basen::Exception(OUT_OF_ALPH) if out of digits map
|
||||||
* @throw basen::Exception(PADDING) if incorrect padding
|
* @throw basen::Exception(PADDING) if incorrect padding
|
||||||
* @warning contain leading zeros, returns count of them
|
|
||||||
*/
|
*/
|
||||||
void decode(const char *str, size_t str_size, uint8_t *data, size_t data_size);
|
void decode(const char *str, size_t str_size, uint8_t *data, size_t data_size);
|
||||||
|
/**
|
||||||
|
* @param str string or string_view which you want to decode
|
||||||
|
* @return decoded data
|
||||||
|
* @throw basen::Exception(LENGTH) if not enough allocated length
|
||||||
|
* @throw basen::Exception(OUT_OF_ALPH) if out of digits map
|
||||||
|
* @throw basen::Exception(PADDING) if incorrect padding
|
||||||
|
*/
|
||||||
std::vector<uint8_t> decode(std::string_view str);
|
std::vector<uint8_t> decode(std::string_view str);
|
||||||
}
|
}
|
||||||
@ -8,38 +8,36 @@
|
|||||||
namespace baseN
|
namespace baseN
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @param digits char[base] array of digits
|
* @param digits [in] alphabet of encoding
|
||||||
* @param digits_size size of digits array. Equals to base
|
|
||||||
* @param map [out] uint8_t[256] array, where at an index equal to the value of the symbol is the index of this symbol in the digits array. 255 if there is no symbol
|
* @param map [out] uint8_t[256] array, where at an index equal to the value of the symbol is the index of this symbol in the digits array. 255 if there is no symbol
|
||||||
|
* @return map of alphabet of encoding
|
||||||
* @throw basen::Exception(ALPH_COLLISION) if alphabet contain same chars
|
* @throw basen::Exception(ALPH_COLLISION) if alphabet contain same chars
|
||||||
*/
|
*/
|
||||||
void digitsMap(const char *digits, uint8_t digits_size, uint8_t *map);
|
void digitsMap(const char *digits, uint8_t digits_size, uint8_t *map);
|
||||||
/**
|
/**
|
||||||
* @param str [in] pointer to string
|
* @param map map of alphabet of encoding, which is returned by baseN::digitsMap
|
||||||
* @param str_size
|
* @return checks what contains only alphabet chars
|
||||||
* @param map uint8_t[256] array, where at an index equal to the value of the symbol is the index of this symbol in the digits array. 255 if there is no symbol
|
|
||||||
* @return that string doesn't contain unnecessary symbols
|
|
||||||
*/
|
*/
|
||||||
bool isValid(const char *str, size_t str_size, const uint8_t *map) noexcept;
|
bool isValid(const char *str, size_t str_size, const uint8_t *map) noexcept;
|
||||||
/**
|
/**
|
||||||
* @param str string or string_view which you want to decode
|
* @param str string or string_view which you want to decode
|
||||||
* @param map uint8_t[256] array, where at an index equal to the value of the symbol is the index of this symbol in the digits array. 255 if there is no symbol
|
* @param map map of alphabet of encoding, which is returned by baseN::digitsMap
|
||||||
* @return that string doesn't contain unnecessary symbols
|
* @return checks what contains only alphabet chars
|
||||||
*/
|
*/
|
||||||
bool isValid(std::string_view str, const uint8_t *map) noexcept;
|
bool isValid(std::string_view str, const uint8_t *map) noexcept;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param data vector or span of data which you want to encode
|
* @param data vector or span of data which you want to encode
|
||||||
* @param base from 1 to 255
|
* @param base radix of encoding from [2 to 254]
|
||||||
* @return estimated size after encoding
|
* @return estimated size after encoding (obviously larger)
|
||||||
* @throw basen::Exception(BASE) if base < 2 || base > 254
|
* @throw basen::Exception(BASE) if base < 2 || base > 254
|
||||||
*/
|
*/
|
||||||
size_t sizeEncoded(std::span<const uint8_t> data, uint8_t base);
|
size_t sizeEncoded(std::span<const uint8_t> data, uint8_t base);
|
||||||
/**
|
/**
|
||||||
* @param str string or string_view which you want to decode
|
* @param str string or string_view which you want to decode
|
||||||
* @param base from 1 to 255
|
* @param base radix of encoding from [2 to 254]
|
||||||
* @param digits char[base] array of digits
|
* @param digits alphabet of encoding
|
||||||
* @return estimated size after decoding
|
* @return estimated size after decoding (obviously larger)
|
||||||
* @throw basen::Exception(BASE) if base < 2 || base > 254
|
* @throw basen::Exception(BASE) if base < 2 || base > 254
|
||||||
* @throw basen::Exception(OVERFLOW) if if there is an overflow
|
* @throw basen::Exception(OVERFLOW) if if there is an overflow
|
||||||
*/
|
*/
|
||||||
@ -47,19 +45,9 @@ namespace baseN
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param data [in] pointer to data which you want encode
|
* @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 [out] pointer to string for encoded data output
|
||||||
* @param str_size
|
* @param base radix of encoding from [2 to 254]
|
||||||
* @param base from 1 to 255
|
* @param digits alphabet of encoding
|
||||||
* @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
|
* @return number of leading chars, which should be trimmed
|
||||||
* @throw basen::Exception(BASE) if base < 2 || base > 254
|
* @throw basen::Exception(BASE) if base < 2 || base > 254
|
||||||
* @warning contain leading zeros, returns count of them
|
* @warning contain leading zeros, returns count of them
|
||||||
@ -67,32 +55,19 @@ namespace baseN
|
|||||||
size_t encode(const uint8_t *data, size_t data_size, char *str, size_t str_size, uint8_t base, const char *digits);
|
size_t encode(const uint8_t *data, size_t data_size, char *str, size_t str_size, uint8_t base, const char *digits);
|
||||||
/**
|
/**
|
||||||
* @param data vector or span of data which you want to encode
|
* @param data vector or span of data which you want to encode
|
||||||
* @param base from 1 to 255
|
* @param base radix of encoding from [2 to 254]
|
||||||
* @param digits char[base] array of digits
|
* @param digits alphabet of encoding
|
||||||
* @code{cpp}
|
|
||||||
* std::vector<uint8_t> data;
|
|
||||||
* auto str = baseN::encode(data, 58, base58::digits);
|
|
||||||
* @endcode
|
|
||||||
* @return encoded string
|
* @return encoded string
|
||||||
|
* @throw basen::Exception(BASE) if base < 2 || base > 254
|
||||||
*/
|
*/
|
||||||
std::string encode(std::span<const uint8_t> data, uint8_t base, const char *digits);
|
std::string encode(std::span<const uint8_t> data, uint8_t base, const char *digits);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param str [in] pointer to string which you want decode
|
* @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 [out] pointer to data for encoded string output
|
||||||
* @param data_size
|
* @param base radix of encoding from [2 to 254]
|
||||||
* @param base from 1 to 255
|
* @param digits alphabet of encoding
|
||||||
* @param digits char[base] array of digits
|
* @param map map of alphabet of encoding, which is returned by baseN::digitsMap
|
||||||
* @param map uint8_t[256] array, where at an index equal to the value of the symbol is the index of this symbol in the digits array. 255 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
|
* @return number of leading chars, which should be trimmed
|
||||||
* @throw basen::Exception(BASE) if base < 2 || base > 254
|
* @throw basen::Exception(BASE) if base < 2 || base > 254
|
||||||
* @throw basen::Exception(OUT_OF_ALPH) if out of alphabet
|
* @throw basen::Exception(OUT_OF_ALPH) if out of alphabet
|
||||||
@ -101,14 +76,12 @@ namespace baseN
|
|||||||
size_t decode(const char *str, size_t str_size, uint8_t *data, size_t data_size, uint8_t base, const char *digits, const uint8_t *map);
|
size_t decode(const char *str, size_t str_size, uint8_t *data, size_t data_size, uint8_t base, const char *digits, const uint8_t *map);
|
||||||
/**
|
/**
|
||||||
* @param str string or string_view which you want to decode
|
* @param str string or string_view which you want to decode
|
||||||
* @param base from 1 to 255
|
* @param base radix of encoding from [2 to 254]
|
||||||
* @param digits char[base] array of digits
|
* @param digits alphabet of encoding
|
||||||
* @param map uint8_t[256] array, where at an index equal to the value of the symbol is the index of this symbol in the digits array. 255 if there is no symbol
|
* @param map map of alphabet of encoding, which is returned by baseN::digitsMap
|
||||||
* @code{cpp}
|
|
||||||
* std::string str;
|
|
||||||
* auto data = baseN::decode(str, 58, base58::digits, base58::map);
|
|
||||||
* @endcode
|
|
||||||
* @return decoded data
|
* @return decoded data
|
||||||
|
* @throw basen::Exception(BASE) if base < 2 || base > 254
|
||||||
|
* @throw basen::Exception(OUT_OF_ALPH) if out of alphabet
|
||||||
*/
|
*/
|
||||||
std::vector<uint8_t> decode(std::string_view str, uint8_t base, const char *digits, const uint8_t *map);
|
std::vector<uint8_t> decode(std::string_view str, uint8_t base, const char *digits, const uint8_t *map);
|
||||||
}
|
}
|
||||||
@ -7,31 +7,62 @@
|
|||||||
|
|
||||||
namespace hex
|
namespace hex
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @brief lower case chars
|
||||||
|
*/
|
||||||
extern const char digits[17];
|
extern const char digits[17];
|
||||||
extern const uint8_t map[256];
|
extern const uint8_t map[256];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return checks what contains only alphabet chars
|
||||||
|
*/
|
||||||
bool isValid(const char *str, size_t str_size) noexcept;
|
bool isValid(const char *str, size_t str_size) noexcept;
|
||||||
|
/**
|
||||||
|
* @param str string or string_view which you want to decode
|
||||||
|
* @return checks what contains only alphabet chars
|
||||||
|
*/
|
||||||
bool isValid(std::string_view str) noexcept;
|
bool isValid(std::string_view str) noexcept;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param data vector or span of data which you want to encode
|
||||||
|
* @return exact size after encoding
|
||||||
* @throw basen::Exception(OVERFLOW) if there is an overflow
|
* @throw basen::Exception(OVERFLOW) if there is an overflow
|
||||||
*/
|
*/
|
||||||
size_t sizeEncoded(std::span<const uint8_t> data);
|
size_t sizeEncoded(std::span<const uint8_t> data);
|
||||||
|
/**
|
||||||
|
* @param str string or string_view which you want to decode
|
||||||
|
* @return exact size after decoding
|
||||||
|
*/
|
||||||
size_t sizeDecoded(std::string_view str) noexcept;
|
size_t sizeDecoded(std::string_view str) noexcept;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param data [in] pointer to data which you want encode
|
||||||
|
* @param str [out] pointer to string for encoded data output
|
||||||
|
* @throw basen::Exception(OVERFLOW) from hex::sizeEncoded
|
||||||
* @throw basen::Exception(LENGTH) if not enough allocated length
|
* @throw basen::Exception(LENGTH) if not enough allocated length
|
||||||
* @warning contain leading zeros, returns count of them
|
|
||||||
*/
|
*/
|
||||||
void encode(const uint8_t *data, size_t data_size, char *str, size_t str_size);
|
void encode(const uint8_t *data, size_t data_size, char *str, size_t str_size);
|
||||||
|
/**
|
||||||
|
* @param data vector or span of data which you want to encode
|
||||||
|
* @return encoded string
|
||||||
|
* @throw basen::Exception(OVERFLOW) from hex::sizeEncoded
|
||||||
|
* @throw basen::Exception(LENGTH) if not enough allocated length
|
||||||
|
*/
|
||||||
std::string encode(std::span<const uint8_t> data);
|
std::string encode(std::span<const uint8_t> data);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* @param str [in] pointer to string which you want decode
|
||||||
|
* @param data [out] pointer to data for encoded string output
|
||||||
* @throw basen::Exception(LENGTH) if not enough allocated length
|
* @throw basen::Exception(LENGTH) if not enough allocated length
|
||||||
* @throw basen::Exception(OUT_OF_ALPH) if out of digits map
|
* @throw basen::Exception(OUT_OF_ALPH) if out of digits map
|
||||||
* @throw basen::Exception(PADDING) if str_size %2 != 0 (isn't hex)
|
* @throw basen::Exception(PADDING) if str_size %2 != 0 (isn't hex)
|
||||||
* @warning contain leading zeros, returns count of them
|
|
||||||
*/
|
*/
|
||||||
void decode(const char *str, size_t str_size, uint8_t *data, size_t data_size);
|
void decode(const char *str, size_t str_size, uint8_t *data, size_t data_size);
|
||||||
|
/**
|
||||||
|
* @param str string or string_view which you want to decode
|
||||||
|
* @throw basen::Exception(LENGTH) if not enough allocated length
|
||||||
|
* @throw basen::Exception(OUT_OF_ALPH) if out of digits map
|
||||||
|
* @throw basen::Exception(PADDING) if str_size %2 != 0 (isn't hex)
|
||||||
|
*/
|
||||||
std::vector<uint8_t> decode(std::string_view str);
|
std::vector<uint8_t> decode(std::string_view str);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user