aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorlloyd <[email protected]>2010-10-28 19:38:09 +0000
committerlloyd <[email protected]>2010-10-28 19:38:09 +0000
commitd1d41321c8d3d6f6273659342e93f02e865ab6bf (patch)
tree5028af1f2b9d1a985f57dec241de3b471a2a674a /src
parent576c5c901375d1ba5bfdfc5873562c63228abab5 (diff)
Consolidate hex and base64 filters into a single sub-module.
Use the standalone base64 functions for most of the Base64 logic
Diffstat (limited to 'src')
-rw-r--r--src/filters/codec_filt/b64_filt.cpp (renamed from src/filters/b64_filt/b64_filt.cpp)79
-rw-r--r--src/filters/codec_filt/b64_filt.h (renamed from src/filters/b64_filt/b64_filt.h)19
-rw-r--r--src/filters/codec_filt/hex_filt.cpp (renamed from src/filters/hex_filt/hex_filt.cpp)0
-rw-r--r--src/filters/codec_filt/hex_filt.h (renamed from src/filters/hex_filt/hex_filt.h)0
-rw-r--r--src/filters/codec_filt/info.txt (renamed from src/filters/b64_filt/info.txt)2
-rw-r--r--src/filters/filters.h5
-rw-r--r--src/filters/hex_filt/info.txt5
7 files changed, 44 insertions, 66 deletions
diff --git a/src/filters/b64_filt/b64_filt.cpp b/src/filters/codec_filt/b64_filt.cpp
index 080848d18..2db6ac3ca 100644
--- a/src/filters/b64_filt/b64_filt.cpp
+++ b/src/filters/codec_filt/b64_filt.cpp
@@ -6,6 +6,7 @@
*/
#include <botan/b64_filt.h>
+#include <botan/base64.h>
#include <botan/charset.h>
#include <botan/exceptn.h>
#include <algorithm>
@@ -13,19 +14,10 @@
namespace Botan {
/*
-* Base64 Encoder Lookup Table
-*/
-const byte Base64_Encoder::BIN_TO_BASE64[64] = {
-0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D,
-0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A,
-0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D,
-0x6E, 0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A,
-0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2B, 0x2F };
-
-/*
* Base64 Decoder Lookup Table
+* Warning: assumes ASCII encodings
*/
-const byte Base64_Decoder::BASE64_TO_BIN[256] = {
+static const byte BASE64_TO_BIN[256] = {
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
@@ -51,34 +43,32 @@ const byte Base64_Decoder::BASE64_TO_BIN[256] = {
* Base64_Encoder Constructor
*/
Base64_Encoder::Base64_Encoder(bool breaks, size_t length, bool t_n) :
- line_length(breaks ? length : 0), trailing_newline(t_n)
- {
- in.resize(48);
- out.resize(4);
-
- counter = position = 0;
- }
-
-/*
-* Base64 Encoding Operation
-*/
-void Base64_Encoder::encode(const byte in[3], byte out[4])
+ line_length(breaks ? length : 0),
+ trailing_newline(t_n && breaks),
+ in(48),
+ out(64),
+ position(0),
+ out_position(0)
{
- 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) )];
}
/*
* Encode and send a block
*/
-void Base64_Encoder::encode_and_send(const byte block[], size_t length)
+void Base64_Encoder::encode_and_send(const byte input[], size_t length)
{
- for(size_t j = 0; j != length; j += 3)
+ while(length)
{
- encode(block + j, &out[0]);
- do_output(&out[0], 4);
+ const size_t proc = std::min(length, in.size());
+
+ size_t consumed = 0;
+ size_t produced = base64_encode(reinterpret_cast<char*>(&out[0]), input,
+ proc, consumed, false);
+
+ do_output(&out[0], produced);
+
+ input += proc;
+ length -= proc;
}
}
@@ -94,15 +84,15 @@ void Base64_Encoder::do_output(const byte input[], size_t length)
size_t remaining = length, offset = 0;
while(remaining)
{
- size_t sent = std::min(line_length - counter, remaining);
+ size_t sent = std::min(line_length - out_position, remaining);
send(input + offset, sent);
- counter += sent;
+ out_position += sent;
remaining -= sent;
offset += sent;
- if(counter == line_length)
+ if(out_position == line_length)
{
send('\n');
- counter = 0;
+ out_position = 0;
}
}
}
@@ -145,7 +135,8 @@ void Base64_Encoder::end_msg()
SecureVector<byte> remainder(3);
copy_mem(&remainder[0], &in[start_of_last_block], left_over);
- encode(&remainder[0], &out[0]);
+ size_t consumed;
+ base64_encode(reinterpret_cast<char*>(&out[0]), &remainder[0], 3, consumed, false);
size_t empty_bits = 8 * (3 - left_over), index = 4 - 1;
while(empty_bits >= 8)
@@ -157,10 +148,10 @@ void Base64_Encoder::end_msg()
do_output(&out[0], 4);
}
- if(trailing_newline || (counter && line_length))
+ if(trailing_newline || (out_position && line_length))
send('\n');
- counter = position = 0;
+ out_position = position = 0;
}
/*
@@ -196,9 +187,9 @@ void Base64_Decoder::decode(const byte in[4], byte out[3])
*/
void Base64_Decoder::decode_and_send(const byte block[], size_t length)
{
- for(size_t j = 0; j != length; j += 4)
+ for(size_t i = 0; i != length; i += 4)
{
- decode(block + j, &out[0]);
+ decode(block + i, &out[0]);
send(out, 3);
}
}
@@ -225,12 +216,12 @@ void Base64_Decoder::handle_bad_char(byte c)
*/
void Base64_Decoder::write(const byte input[], size_t length)
{
- for(size_t j = 0; j != length; ++j)
+ for(size_t i = 0; i != length; ++i)
{
- if(is_valid(input[j]))
- in[position++] = input[j];
+ if(is_valid(input[i]))
+ in[position++] = input[i];
else
- handle_bad_char(input[j]);
+ handle_bad_char(input[i]);
if(position == in.size())
{
diff --git a/src/filters/b64_filt/b64_filt.h b/src/filters/codec_filt/b64_filt.h
index ae5fa26ce..8918049a2 100644
--- a/src/filters/b64_filt/b64_filt.h
+++ b/src/filters/codec_filt/b64_filt.h
@@ -5,8 +5,8 @@
* Distributed under the terms of the Botan license
*/
-#ifndef BOTAN_BASE64_H__
-#define BOTAN_BASE64_H__
+#ifndef BOTAN_BASE64_FILTER_H__
+#define BOTAN_BASE64_FILTER_H__
#include <botan/filter.h>
@@ -18,8 +18,6 @@ namespace Botan {
class BOTAN_DLL Base64_Encoder : public Filter
{
public:
- static void encode(const byte in[3], byte out[4]);
-
std::string name() const { return "Base64_Encoder"; }
/**
@@ -36,7 +34,7 @@ class BOTAN_DLL Base64_Encoder : public Filter
/**
* Create a base64 encoder.
- * @param breaks whether to use line breaks in the Streamcipheroutput
+ * @param breaks whether to use line breaks in the output
* @param length the length of the lines of the output
* @param t_n whether to use a trailing newline
*/
@@ -45,12 +43,11 @@ class BOTAN_DLL Base64_Encoder : public Filter
private:
void encode_and_send(const byte[], size_t);
void do_output(const byte[], size_t);
- static const byte BIN_TO_BASE64[64];
const size_t line_length;
const bool trailing_newline;
SecureVector<byte> in, out;
- size_t position, counter;
+ size_t position, out_position;
};
/**
@@ -59,10 +56,6 @@ class BOTAN_DLL Base64_Encoder : public Filter
class BOTAN_DLL Base64_Decoder : public Filter
{
public:
- static void decode(const byte input[4], byte output[3]);
-
- static bool is_valid(byte);
-
std::string name() const { return "Base64_Decoder"; }
/**
@@ -84,9 +77,11 @@ class BOTAN_DLL Base64_Decoder : public Filter
*/
Base64_Decoder(Decoder_Checking checking = NONE);
private:
+ static void decode(const byte input[4], byte output[3]);
+ static bool is_valid(byte);
+
void decode_and_send(const byte[], size_t);
void handle_bad_char(byte);
- static const byte BASE64_TO_BIN[256];
const Decoder_Checking checking;
SecureVector<byte> in, out;
diff --git a/src/filters/hex_filt/hex_filt.cpp b/src/filters/codec_filt/hex_filt.cpp
index 3d56beec4..3d56beec4 100644
--- a/src/filters/hex_filt/hex_filt.cpp
+++ b/src/filters/codec_filt/hex_filt.cpp
diff --git a/src/filters/hex_filt/hex_filt.h b/src/filters/codec_filt/hex_filt.h
index cfbb818d3..cfbb818d3 100644
--- a/src/filters/hex_filt/hex_filt.h
+++ b/src/filters/codec_filt/hex_filt.h
diff --git a/src/filters/b64_filt/info.txt b/src/filters/codec_filt/info.txt
index 8fff2f181..080cc8f44 100644
--- a/src/filters/b64_filt/info.txt
+++ b/src/filters/codec_filt/info.txt
@@ -1,4 +1,4 @@
-define BASE64_FILTER
+define CODEC_FILTERS
<requires>
filters
diff --git a/src/filters/filters.h b/src/filters/filters.h
index 49c04e430..b409e78f5 100644
--- a/src/filters/filters.h
+++ b/src/filters/filters.h
@@ -20,11 +20,8 @@
#include <botan/scan_name.h>
-#if defined(BOTAN_HAS_BASE64_FILTER)
+#if defined(BOTAN_HAS_CODEC_FILTERS)
#include <botan/b64_filt.h>
-#endif
-
-#if defined(BOTAN_HAS_HEX_FILTER)
#include <botan/hex_filt.h>
#endif
diff --git a/src/filters/hex_filt/info.txt b/src/filters/hex_filt/info.txt
deleted file mode 100644
index 7f5f47fa1..000000000
--- a/src/filters/hex_filt/info.txt
+++ /dev/null
@@ -1,5 +0,0 @@
-define HEX_FILTER
-
-<requires>
-filters
-</requires>