aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/codec
diff options
context:
space:
mode:
authorWambou <[email protected]>2018-07-08 15:06:32 +0200
committerWambou <[email protected]>2018-08-04 19:51:36 +0200
commit96919426b862d11156f150dc0fc3d80abb88467e (patch)
tree9f8308fe5341ebd54d0b9a53b585eb4286be2be2 /src/lib/codec
parentfc1b230be2ef80c8071169e29607992fed114632 (diff)
Use codec_base for Base64 encoding
Diffstat (limited to 'src/lib/codec')
-rw-r--r--src/lib/codec/base64/base64.cpp307
-rw-r--r--src/lib/codec/base64/base64.h4
2 files changed, 153 insertions, 158 deletions
diff --git a/src/lib/codec/base64/base64.cpp b/src/lib/codec/base64/base64.cpp
index 7f52fb5af..da666fe34 100644
--- a/src/lib/codec/base64/base64.cpp
+++ b/src/lib/codec/base64/base64.cpp
@@ -6,30 +6,156 @@
*/
#include <botan/base64.h>
+#include <botan/internal/codec_base.h>
#include <botan/exceptn.h>
-#include <botan/mem_ops.h>
#include <botan/internal/rounding.h>
namespace Botan {
namespace {
-static const uint8_t BIN_TO_BASE64[64] = {
+class Base64
+ {
+ public:
+ static inline size_t encoding_bytes_in() BOTAN_NOEXCEPT
+ {
+ return m_encoding_bytes_in;
+ }
+ static inline size_t encoding_bytes_out() BOTAN_NOEXCEPT
+ {
+ return m_encoding_bytes_out;
+ }
+
+ static inline size_t decoding_bytes_in() BOTAN_NOEXCEPT
+ {
+ return m_encoding_bytes_out;
+ }
+ static inline size_t decoding_bytes_out() BOTAN_NOEXCEPT
+ {
+ return m_encoding_bytes_in;
+ }
+
+ static inline size_t bits_consumed() BOTAN_NOEXCEPT
+ {
+ return m_encoding_bits;
+ }
+ static inline size_t remaining_bits_before_padding() BOTAN_NOEXCEPT
+ {
+ return m_remaining_bits_before_padding;
+ }
+
+ static inline size_t encode_max_output(size_t input_length)
+ {
+ return (round_up(input_length, m_encoding_bytes_in) / m_encoding_bytes_in) * m_encoding_bytes_out;
+ }
+ static inline size_t decode_max_output(size_t input_length)
+ {
+ return (round_up(input_length, m_encoding_bytes_out) * m_encoding_bytes_in) / m_encoding_bytes_out;
+ }
+
+ static void encode(char out[8], const uint8_t in[5]) BOTAN_NOEXCEPT
+ {
+ out[0] = Base64::m_bin_to_base64[(in[0] & 0xFC) >> 2];
+ out[1] = Base64::m_bin_to_base64[((in[0] & 0x03) << 4) | (in[1] >> 4)];
+ out[2] = Base64::m_bin_to_base64[((in[1] & 0x0F) << 2) | (in[2] >> 6)];
+ out[3] = Base64::m_bin_to_base64[in[2] & 0x3F];
+ }
+
+ static inline uint8_t lookup_binary_value(char input) BOTAN_NOEXCEPT
+ {
+ return Base64::m_base64_to_bin[static_cast<uint8_t>(input)];
+ }
+
+ static inline bool check_bad_char(uint8_t bin, char input, bool ignore_ws)
+ {
+ if(bin <= 0x3F)
+ {
+ return true;
+ }
+ else if(!(bin == 0x81 || (bin == 0x80 && ignore_ws)))
+ {
+ std::string bad_char(1, input);
+ if(bad_char == "\t")
+ { bad_char = "\\t"; }
+ else if(bad_char == "\n")
+ { bad_char = "\\n"; }
+ else if(bad_char == "\r")
+ { bad_char = "\\r"; }
+
+ throw Invalid_Argument(
+ std::string("base64_decode: invalid base64 character '") +
+ bad_char + "'");
+ }
+ return false;
+ }
+
+ static void decode(uint8_t* out_ptr, const uint8_t decode_buf[4])
+ {
+ out_ptr[0] = (decode_buf[0] << 2) | (decode_buf[1] >> 4);
+ out_ptr[1] = (decode_buf[1] << 4) | (decode_buf[2] >> 2);
+ out_ptr[2] = (decode_buf[2] << 6) | decode_buf[3];
+ }
+
+ static inline size_t bytes_to_remove(size_t final_truncate)
+ {
+ return final_truncate;
+ }
+
+ private:
+ static const size_t m_encoding_bits = 6;
+ static const size_t m_remaining_bits_before_padding = 8;
+
+
+ static const size_t m_encoding_bytes_in = 3;
+ static const size_t m_encoding_bytes_out = 4;
+
+
+ static const uint8_t m_bin_to_base64[64];
+ static const uint8_t m_base64_to_bin[256];
+ };
+
+const uint8_t Base64::m_bin_to_base64[64] =
+ {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
-};
+ };
-void do_base64_encode(char out[4], const uint8_t in[3])
+/*
+* base64 Decoder Lookup Table
+* Warning: assumes ASCII encodings
+*/
+const uint8_t Base64::m_base64_to_bin[256] =
{
- out[0] = BIN_TO_BASE64[(in[0] & 0xFC) >> 2];
- out[1] = BIN_TO_BASE64[((in[0] & 0x03) << 4) | (in[1] >> 4)];
- out[2] = BIN_TO_BASE64[((in[1] & 0x0F) << 2) | (in[2] >> 6)];
- out[3] = BIN_TO_BASE64[in[2] & 0x3F];
- }
-
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80,
+ 0x80, 0xFF, 0xFF, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0x3E, 0xFF, 0xFF, 0xFF, 0x3F, 0x34, 0x35,
+ 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0xFF, 0xFF,
+ 0xFF, 0x81, 0xFF, 0xFF, 0xFF, 0x00, 0x01, 0x02, 0x03, 0x04,
+ 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
+ 0x0F, 0x10, 0x11, 0x12, 0x13, 0x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 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
+ };
}
size_t base64_encode(char out[],
@@ -38,58 +164,24 @@ size_t base64_encode(char out[],
size_t& input_consumed,
bool final_inputs)
{
- input_consumed = 0;
-
- size_t input_remaining = input_length;
- size_t output_produced = 0;
-
- while(input_remaining >= 3)
- {
- do_base64_encode(out + output_produced, in + input_consumed);
-
- input_consumed += 3;
- output_produced += 4;
- input_remaining -= 3;
- }
-
- if(final_inputs && input_remaining)
- {
- uint8_t remainder[3] = { 0 };
- for(size_t i = 0; i != input_remaining; ++i)
- remainder[i] = in[input_consumed + i];
-
- do_base64_encode(out + output_produced, remainder);
-
- size_t empty_bits = 8 * (3 - input_remaining);
- size_t index = output_produced + 4 - 1;
- while(empty_bits >= 8)
- {
- out[index--] = '=';
- empty_bits -= 6;
- }
-
- input_consumed += input_remaining;
- output_produced += 4;
- }
-
- return output_produced;
+ return base_encode(Base64(), out, in, input_length, input_consumed, final_inputs);
}
std::string base64_encode(const uint8_t input[],
size_t input_length)
{
- const size_t output_length = base64_encode_max_output(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)
- {
+ 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");
@@ -97,111 +189,14 @@ std::string base64_encode(const uint8_t input[],
return output;
}
-size_t base64_decode(uint8_t output[],
- const char input[],
+size_t base64_decode(uint8_t out[],
+ const char in[],
size_t input_length,
size_t& input_consumed,
bool final_inputs,
bool ignore_ws)
{
- /*
- * Base64 Decoder Lookup Table
- * Warning: assumes ASCII encodings
- */
- static const uint8_t BASE64_TO_BIN[256] = {
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80,
- 0x80, 0xFF, 0xF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, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
- 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
-
- uint8_t* out_ptr = output;
- uint8_t decode_buf[4];
- size_t decode_buf_pos = 0;
- size_t final_truncate = 0;
-
- clear_mem(output, input_length * 3 / 4);
-
- for(size_t i = 0; i != input_length; ++i)
- {
- const uint8_t bin = BASE64_TO_BIN[static_cast<uint8_t>(input[i])];
-
- if(bin <= 0x3F)
- {
- decode_buf[decode_buf_pos] = bin;
- decode_buf_pos += 1;
- }
- else if(!(bin == 0x81 || (bin == 0x80 && ignore_ws)))
- {
- std::string bad_char(1, input[i]);
- if(bad_char == "\t")
- bad_char = "\\t";
- else if(bad_char == "\n")
- bad_char = "\\n";
- else if(bad_char == "\r")
- bad_char = "\\r";
-
- throw Invalid_Argument(
- std::string("base64_decode: invalid base64 character '") +
- bad_char + "'");
- }
-
- /*
- * If we're at the end of the input, pad with 0s and truncate
- */
- if(final_inputs && (i == input_length - 1))
- {
- if(decode_buf_pos)
- {
- for(size_t j = decode_buf_pos; j != 4; ++j)
- decode_buf[j] = 0;
- final_truncate = (4 - decode_buf_pos);
- decode_buf_pos = 4;
- }
- }
-
- if(decode_buf_pos == 4)
- {
- out_ptr[0] = (decode_buf[0] << 2) | (decode_buf[1] >> 4);
- out_ptr[1] = (decode_buf[1] << 4) | (decode_buf[2] >> 2);
- out_ptr[2] = (decode_buf[2] << 6) | decode_buf[3];
-
- out_ptr += 3;
- decode_buf_pos = 0;
- input_consumed = i+1;
- }
- }
-
- while(input_consumed < input_length &&
- BASE64_TO_BIN[static_cast<uint8_t>(input[input_consumed])] == 0x80)
- {
- ++input_consumed;
- }
-
- size_t written = (out_ptr - output) - final_truncate;
-
- return written;
+ return base_decode(Base64(), out, in, input_length, input_consumed, final_inputs, ignore_ws);
}
size_t base64_decode(uint8_t output[],
@@ -214,7 +209,7 @@ size_t base64_decode(uint8_t output[],
consumed, true, ignore_ws);
if(consumed != input_length)
- throw Invalid_Argument("base64_decode: input did not have full bytes");
+ { throw Invalid_Argument("base64_decode: input did not have full bytes"); }
return written;
}
@@ -227,10 +222,10 @@ size_t base64_decode(uint8_t output[],
}
secure_vector<uint8_t> base64_decode(const char input[],
- size_t input_length,
- bool ignore_ws)
+ size_t input_length,
+ bool ignore_ws)
{
- const size_t output_length = base64_decode_max_output(input_length);
+ 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(),
@@ -243,19 +238,19 @@ secure_vector<uint8_t> base64_decode(const char input[],
}
secure_vector<uint8_t> base64_decode(const std::string& input,
- bool ignore_ws)
+ bool ignore_ws)
{
return base64_decode(input.data(), input.size(), ignore_ws);
}
size_t base64_encode_max_output(size_t input_length)
{
- return (round_up(input_length, 3) / 3) * 4;
+ return Base64::encode_max_output(input_length);
}
size_t base64_decode_max_output(size_t input_length)
{
- return (round_up(input_length, 4) * 3) / 4;
+ return Base64::decode_max_output(input_length);
}
}
diff --git a/src/lib/codec/base64/base64.h b/src/lib/codec/base64/base64.h
index a20d03b0f..715598e9c 100644
--- a/src/lib/codec/base64/base64.h
+++ b/src/lib/codec/base64/base64.h
@@ -67,8 +67,8 @@ std::string base64_encode(const std::vector<uint8_t, Alloc>& input)
exception if whitespace is encountered
* @return number of bytes written to output
*/
-size_t BOTAN_PUBLIC_API(2,0) base64_decode(uint8_t output[],
- const char input[],
+size_t BOTAN_PUBLIC_API(2,0) base64_decode(uint8_t out[],
+ const char in[],
size_t input_length,
size_t& input_consumed,
bool final_inputs,