11 Commits

Author SHA1 Message Date
ff888a88f4 fix(base64): encode: incorrect padding if size % 3 == 2 2024-09-28 13:21:42 +03:00
bce26d3a4b submodule(update)
Some checks failed
Deploy documentation / deploy (push) Has been cancelled
2024-09-26 21:02:55 +03:00
44120b7ebb fix(docs.yml): on push main; update(Doxyfile): version 2024-09-26 20:55:30 +03:00
87a0cce29d update(AUTHORS) 2024-09-26 20:24:55 +03:00
9f939fea24 Merge pull request #2 from vSEK1RO/v1.0.2
V1.0.2
2024-09-26 20:23:15 +03:00
8da5ce49ef fix(base64): coverage 2024-09-26 20:05:38 +03:00
b0e645679d fix(hex): coverage 2024-09-26 17:17:01 +03:00
6d689cb9b3 fix(README.md): coverage 2024-09-26 12:26:07 +03:00
119bca6c87 fix(docs.yml): on realease published 2024-09-26 12:06:51 +03:00
5396abe202 feat(cover): gcov, gcovr 2024-09-26 12:00:23 +03:00
5f2cffdd82 submodule(distrib/arch): v1.0.1 2024-09-23 21:20:06 +03:00
10 changed files with 80 additions and 57 deletions

View File

@ -4,8 +4,6 @@ on:
push:
branches: ["main"]
workflow_dispatch:
permissions:
contents: read
pages: write

1
.gitignore vendored
View File

@ -43,6 +43,7 @@ bin
obj
lib
doc
cov
# IDE
.vscode

View File

@ -1,5 +1,5 @@
libbasen project authors:
commits | username
62 | vSEK1RO
70 | vSEK1RO

View File

@ -48,7 +48,7 @@ PROJECT_NAME = libbasen
# could be handy for archiving the generated documentation or if some version
# control system is used.
PROJECT_NUMBER = v1.0.1
PROJECT_NUMBER = v1.0.3
# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a

View File

@ -5,7 +5,7 @@ DEBUG ?= false
USRDIR ?= /usr
.PHONY: build i install uni uninstall\
tools library tests docs clean
tools library tests docs cov clean
LIB = basen
OBJS =\
@ -80,8 +80,19 @@ uninstall:
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 -lgcov ${CFLAGS}
clean:
rm -rf ${OBJDIR}/* ${LIBDIR}/* ${BINDIR}/*
rm -rf ${DIRS} doc cov
ifneq (${OBJS},)

View File

@ -30,18 +30,6 @@ Uninstall:
```
sudo make uni USRDIR=(Your installation dir)
```
For build with with debug flags:
```
make -j $(nproc) DEBUG=
```
For build tests (needed gtest package as dependency):
```
make tests -j $(nproc) DEBUG=
```
And also flag for dynamic linking (if possible):
```
make tools -j $(nproc) SHARED=
```
## Documentation
@ -56,4 +44,17 @@ Now we would like to implement the following features:
- 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)

View File

@ -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] << 2 & 0x3F];
str[last_idx + 3] = '=';
break;
default:
break;
}
}
std::string encode(std::span<const uint8_t> data) noexcept

View File

@ -21,13 +21,19 @@ TEST(base64, isValid)
std::vector<std::pair<std::string, std::string>> tests = {
{"", ""},
{"BKUEpQ==", "04a504a5"},
{"BKUEpQA=", "04a504a500"},
{"BKUEpQAA", "04a504a50000"},
{"aGVsbG8=", "68656c6c6f"},
{"aGVsbG9v", "68656c6c6f6f"},
};
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)
{

View File

@ -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)
{