Skip to content

Commit

Permalink
b16 fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
abumq committed Aug 25, 2017
1 parent f5c5c40 commit e9b5e9a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
15 changes: 13 additions & 2 deletions src/base16.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ using namespace mine;

const std::string Base16::kValidChars = "0123456789ABCDEF";

const std::unordered_map<int, int> Base16::kDecodeMap = {
{48, 0}, {49, 1}, {50, 2}, {51, 3},
{52, 4}, {53, 5}, {54, 6}, {55, 7},
{56, 8}, {57, 9}, {65, 10}, {66, 11},
{67, 12}, {68, 13},{69, 14}, {70, 15},
};

std::string Base16::encode(const std::string& raw) noexcept
{
std::stringstream ss;
Expand All @@ -42,8 +49,12 @@ std::string Base16::decode(const std::string& enc)
for (auto it = enc.begin(); it != enc.end(); it += 2) {
int b0 = *it & 0xff;
int b1 = *(it + 1) & 0xff;
// fixme: need to fix this!
ss << static_cast<byte>(((b0 & 0xf) << 4) | (b1 & 0xf));
try {
ss << static_cast<byte>((b0 << 4) | kDecodeMap.at(b1));
} catch (const std::exception&) {
throw std::runtime_error("Invalid base-16 encoding");
}

s = ss.str();
}
return ss.str();
Expand Down
7 changes: 7 additions & 0 deletions src/base16.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#define Base16_H

#include <string>
#include <unordered_map>

namespace mine {

Expand All @@ -38,6 +39,12 @@ class Base16 {
///
static const std::string kValidChars;

///
/// \brief Map for fast lookup corresponding character
/// \see Base64::kDecodeMap
///
static const std::unordered_map<int, int> kDecodeMap;

///
/// \brief Encodes input of length to hex encoding
///
Expand Down
5 changes: 3 additions & 2 deletions test/base16-test.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@
namespace mine {

static TestData<std::string, std::string> Base16TestData = {
TestCase("48656C6C6F20576F726C64", "Hello World"),
TestCase("717569636B2062726F776E20666F78206A756D7073206F76657220746865206C617A7920646F6720515549434B2042524F574E20464F58204A554D5053204F56455220544845204C415A5920444F472031323334353637383930", "quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG 1234567890"),
TestCase("68656C6F", "helo"),
TestCase("48656C6C6F20576F726C6421", "Hello World!"),
TestCase("616263313233213F242A262829272D3D407E", "abc123!?$*&()'-=@~"),
};

static TestData<std::string> InvalidBase16EncodingData = {
Expand Down
4 changes: 2 additions & 2 deletions test/base64-test.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ static TestData<std::string, std::string> Base64TestData = {
// some manual examples
TestCase("cGxhaW4gdGV4dA==", "plain text"),
TestCase("SGVsbG8=", "Hello"),
TestCase("YWJjMTIzIT8kKiYoKSctPUB+", "abc123!?$*&()'-=@~"),
// Some unicode examples
TestCase("SGVsbG/nq5w=", "Hello竜"),
TestCase("4oKsNTA=", "€50"),
TestCase("dGhpcyBpcyByb2NrZXQg8J+agCBhbmQgaSBsb3ZlIGl0", "this is rocket 🚀 and i love it"),
TestCase("YWJjMTIzIT8kKiYoKSctPUB+", "abc123!?$*&()'-=@~"),
TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRw==", "quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG"),
TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRyAxMjM0NTY3ODkw", "quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG 1234567890"),
};
Expand All @@ -42,12 +42,12 @@ static TestData<std::string, std::wstring> Base64WStringTestData = {
// some manual examples
TestCase("cGxhaW4gdGV4dA==", L"plain text"),
TestCase("SGVsbG8=", L"Hello"),
TestCase("YWJjMTIzIT8kKiYoKSctPUB+", L"abc123!?$*&()'-=@~"),
// Some unicode examples
TestCase("SGVsbG/nq5w=", L"Hello竜"),
TestCase("4oKsNTA=", L"€50"),
// Commenting and leaving it here on purpose, see note on decodeAsWString
// TestCase("dGhpcyBpcyByb2NrZXQg8J+agCBhbmQgaSBsb3ZlIGl0", L"this is rocket 🚀 and i love it"),
TestCase("YWJjMTIzIT8kKiYoKSctPUB+", L"abc123!?$*&()'-=@~"),
TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRw==", L"quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG"),
TestCase("cXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nIFFVSUNLIEJST1dOIEZPWCBKVU1QUyBPVkVSIFRIRSBMQVpZIERPRyAxMjM0NTY3ODkw", L"quick brown fox jumps over the lazy dog QUICK BROWN FOX JUMPS OVER THE LAZY DOG 1234567890"),
};
Expand Down
4 changes: 2 additions & 2 deletions test/main.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "test.h"
//#include "base16-test.h"
#include "base64-test.h"
#include "base16-test.h"
//#include "base64-test.h"
#include "aes-test.h"
#include "zlib-test.h"
//#include "rsa-test.h"
Expand Down

0 comments on commit e9b5e9a

Please sign in to comment.