aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2018-10-15 19:19:35 -0400
committerJack Lloyd <[email protected]>2018-10-15 19:19:35 -0400
commit7f296b44e06b29b01d04ba638c3f179a81f3201c (patch)
treeac42d1b617a90b0fcc90aa03e59663a2e36cfcdf
parent0536fe92bbaf45ff681032f898b755d0a81031bd (diff)
parentb1d11d143b2d414e537689aefd6c1326c660afa3 (diff)
Merge GH #1707 Refactor base32/base64
-rw-r--r--src/lib/codec/base32/base32.cpp45
-rw-r--r--src/lib/codec/base64/base64.cpp43
-rw-r--r--src/lib/utils/codec_base.h57
3 files changed, 73 insertions, 72 deletions
diff --git a/src/lib/codec/base32/base32.cpp b/src/lib/codec/base32/base32.cpp
index e2eaf4095..fc9883c86 100644
--- a/src/lib/codec/base32/base32.cpp
+++ b/src/lib/codec/base32/base32.cpp
@@ -1,13 +1,13 @@
/*
* Base32 Encoding and Decoding
* (C) 2018 Erwan Chaussy
+* (C) 2018 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include <botan/base32.h>
#include <botan/internal/codec_base.h>
-#include <botan/exceptn.h>
#include <botan/internal/rounding.h>
namespace Botan {
@@ -17,6 +17,11 @@ namespace {
class Base32 final
{
public:
+ static inline std::string name() noexcept
+ {
+ return "base32";
+ }
+
static inline size_t encoding_bytes_in() noexcept
{
return m_encoding_bytes_in;
@@ -175,23 +180,7 @@ size_t base32_encode(char out[],
std::string base32_encode(const uint8_t input[],
size_t input_length)
{
- const size_t output_length = Base32::encode_max_output(input_length);
- std::string output(output_length, 0);
-
- size_t consumed = 0;
- size_t produced = 0;
-
- if(output_length > 0)
- {
- produced = base32_encode(&output.front(),
- input, input_length,
- consumed, true);
- }
-
- BOTAN_ASSERT_EQUAL(consumed, input_length, "Consumed the entire input");
- BOTAN_ASSERT_EQUAL(produced, output.size(), "Produced expected size");
-
- return output;
+ return base_encode_to_string(Base32(), input, input_length);
}
size_t base32_decode(uint8_t out[],
@@ -209,14 +198,7 @@ size_t base32_decode(uint8_t output[],
size_t input_length,
bool ignore_ws)
{
- size_t consumed = 0;
- size_t written = base32_decode(output, input, input_length,
- consumed, true, ignore_ws);
-
- if(consumed != input_length)
- { throw Invalid_Argument("base32_decode: input did not have full bytes"); }
-
- return written;
+ return base_decode_full(Base32(), output, input, input_length, ignore_ws);
}
size_t base32_decode(uint8_t output[],
@@ -230,16 +212,7 @@ secure_vector<uint8_t> base32_decode(const char input[],
size_t input_length,
bool ignore_ws)
{
- const size_t output_length = Base32::decode_max_output(input_length);
- secure_vector<uint8_t> bin(output_length);
-
- size_t written = base32_decode(bin.data(),
- input,
- input_length,
- ignore_ws);
-
- bin.resize(written);
- return bin;
+ return base_decode_to_vec<secure_vector<uint8_t>>(Base32(), input, input_length, ignore_ws);
}
secure_vector<uint8_t> base32_decode(const std::string& input,
diff --git a/src/lib/codec/base64/base64.cpp b/src/lib/codec/base64/base64.cpp
index 1f7937727..b4f78bca0 100644
--- a/src/lib/codec/base64/base64.cpp
+++ b/src/lib/codec/base64/base64.cpp
@@ -17,6 +17,11 @@ namespace {
class Base64 final
{
public:
+ static inline std::string name() noexcept
+ {
+ return "base64";
+ }
+
static inline size_t encoding_bytes_in() noexcept
{
return m_encoding_bytes_in;
@@ -170,23 +175,7 @@ size_t base64_encode(char out[],
std::string base64_encode(const uint8_t input[],
size_t input_length)
{
- const size_t output_length = Base64::encode_max_output(input_length);
- std::string output(output_length, 0);
-
- size_t consumed = 0;
- size_t produced = 0;
-
- if(output_length > 0)
- {
- produced = base64_encode(&output.front(),
- input, input_length,
- consumed, true);
- }
-
- BOTAN_ASSERT_EQUAL(consumed, input_length, "Consumed the entire input");
- BOTAN_ASSERT_EQUAL(produced, output.size(), "Produced expected size");
-
- return output;
+ return base_encode_to_string(Base64(), input, input_length);
}
size_t base64_decode(uint8_t out[],
@@ -204,14 +193,7 @@ size_t base64_decode(uint8_t output[],
size_t input_length,
bool ignore_ws)
{
- size_t consumed = 0;
- size_t written = base64_decode(output, input, input_length,
- consumed, true, ignore_ws);
-
- if(consumed != input_length)
- { throw Invalid_Argument("base64_decode: input did not have full bytes"); }
-
- return written;
+ return base_decode_full(Base64(), output, input, input_length, ignore_ws);
}
size_t base64_decode(uint8_t output[],
@@ -225,16 +207,7 @@ secure_vector<uint8_t> base64_decode(const char input[],
size_t input_length,
bool ignore_ws)
{
- const size_t output_length = Base64::decode_max_output(input_length);
- secure_vector<uint8_t> bin(output_length);
-
- size_t written = base64_decode(bin.data(),
- input,
- input_length,
- ignore_ws);
-
- bin.resize(written);
- return bin;
+ return base_decode_to_vec<secure_vector<uint8_t>>(Base64(), input, input_length, ignore_ws);
}
secure_vector<uint8_t> base64_decode(const std::string& input,
diff --git a/src/lib/utils/codec_base.h b/src/lib/utils/codec_base.h
index e7dbc33e4..2338fdfd7 100644
--- a/src/lib/utils/codec_base.h
+++ b/src/lib/utils/codec_base.h
@@ -1,6 +1,7 @@
/*
* Base Encoding and Decoding
* (C) 2018 Erwan Chaussy
+* (C) 2018 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
@@ -9,6 +10,7 @@
#define BOTAN_BASE_CODEC_H_
#include <botan/secmem.h>
+#include <botan/exceptn.h>
#include <vector>
#include <string>
@@ -79,10 +81,33 @@ size_t base_encode(Base&& base,
return output_produced;
}
+
+template <typename Base>
+std::string base_encode_to_string(Base&& base, const uint8_t input[], size_t input_length)
+ {
+ const size_t output_length = base.encode_max_output(input_length);
+ std::string output(output_length, 0);
+
+ size_t consumed = 0;
+ size_t produced = 0;
+
+ if(output_length > 0)
+ {
+ produced = base_encode(base, &output.front(),
+ input, input_length,
+ consumed, true);
+ }
+
+ BOTAN_ASSERT_EQUAL(consumed, input_length, "Consumed the entire input");
+ BOTAN_ASSERT_EQUAL(produced, output.size(), "Produced expected size");
+
+ return output;
+ }
+
/**
* Perform decoding using the base provided
* @param base object giving access to the encodings specifications
-* @param output an array of at least base.decode_max_output bytes
+* @param output an array of at least Base::decode_max_output bytes
* @param input some base input
* @param input_length length of input in bytes
* @param input_consumed is an output parameter which says how many
@@ -160,6 +185,36 @@ size_t base_decode(Base&& base,
return written;
}
+template<typename Base>
+size_t base_decode_full(Base&& base, uint8_t output[], const char input[], size_t input_length, bool ignore_ws)
+ {
+ size_t consumed = 0;
+ const size_t written = base_decode(base, output, input, input_length, consumed, true, ignore_ws);
+
+ if(consumed != input_length)
+ {
+ throw Invalid_Argument(base.name() + " decoding failed, input did not have full bytes");
+ }
+
+ return written;
+ }
+
+template<typename Vector, typename Base>
+Vector base_decode_to_vec(Base&& base,
+ const char input[],
+ size_t input_length,
+ bool ignore_ws)
+ {
+ const size_t output_length = base.decode_max_output(input_length);
+ Vector bin(output_length);
+
+ const size_t written =
+ base_decode_full(base, bin.data(), input, input_length, ignore_ws);
+
+ bin.resize(written);
+ return bin;
+ }
+
}
#endif