From d1d41321c8d3d6f6273659342e93f02e865ab6bf Mon Sep 17 00:00:00 2001 From: lloyd Date: Thu, 28 Oct 2010 19:38:09 +0000 Subject: Consolidate hex and base64 filters into a single sub-module. Use the standalone base64 functions for most of the Base64 logic --- src/filters/b64_filt/b64_filt.cpp | 265 ------------------------------------ src/filters/b64_filt/b64_filt.h | 98 ------------- src/filters/b64_filt/info.txt | 5 - src/filters/codec_filt/b64_filt.cpp | 256 ++++++++++++++++++++++++++++++++++ src/filters/codec_filt/b64_filt.h | 93 +++++++++++++ src/filters/codec_filt/hex_filt.cpp | 172 +++++++++++++++++++++++ src/filters/codec_filt/hex_filt.h | 81 +++++++++++ src/filters/codec_filt/info.txt | 5 + src/filters/filters.h | 5 +- src/filters/hex_filt/hex_filt.cpp | 172 ----------------------- src/filters/hex_filt/hex_filt.h | 81 ----------- src/filters/hex_filt/info.txt | 5 - 12 files changed, 608 insertions(+), 630 deletions(-) delete mode 100644 src/filters/b64_filt/b64_filt.cpp delete mode 100644 src/filters/b64_filt/b64_filt.h delete mode 100644 src/filters/b64_filt/info.txt create mode 100644 src/filters/codec_filt/b64_filt.cpp create mode 100644 src/filters/codec_filt/b64_filt.h create mode 100644 src/filters/codec_filt/hex_filt.cpp create mode 100644 src/filters/codec_filt/hex_filt.h create mode 100644 src/filters/codec_filt/info.txt delete mode 100644 src/filters/hex_filt/hex_filt.cpp delete mode 100644 src/filters/hex_filt/hex_filt.h delete mode 100644 src/filters/hex_filt/info.txt (limited to 'src') diff --git a/src/filters/b64_filt/b64_filt.cpp b/src/filters/b64_filt/b64_filt.cpp deleted file mode 100644 index 080848d18..000000000 --- a/src/filters/b64_filt/b64_filt.cpp +++ /dev/null @@ -1,265 +0,0 @@ -/* -* Base64 Encoder/Decoder -* (C) 1999-2008 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#include -#include -#include -#include - -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 -*/ -const byte Base64_Decoder::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, -0x80, 0x80, 0x80, 0x80, 0x3E, 0x80, 0x80, 0x80, 0x3F, 0x34, 0x35, 0x36, 0x37, -0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, -0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, -0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, -0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, -0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, -0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 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, 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, 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, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, -0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 }; - -/* -* 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]) - { - 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) - { - for(size_t j = 0; j != length; j += 3) - { - encode(block + j, &out[0]); - do_output(&out[0], 4); - } - } - -/* -* Handle the output -*/ -void Base64_Encoder::do_output(const byte input[], size_t length) - { - if(line_length == 0) - send(input, length); - else - { - size_t remaining = length, offset = 0; - while(remaining) - { - size_t sent = std::min(line_length - counter, remaining); - send(input + offset, sent); - counter += sent; - remaining -= sent; - offset += sent; - if(counter == line_length) - { - send('\n'); - counter = 0; - } - } - } - } - -/* -* Convert some data into Base64 -*/ -void Base64_Encoder::write(const byte input[], size_t length) - { - in.copy(position, input, length); - if(position + length >= in.size()) - { - encode_and_send(&in[0], in.size()); - input += (in.size() - position); - length -= (in.size() - position); - while(length >= in.size()) - { - encode_and_send(input, in.size()); - input += in.size(); - length -= in.size(); - } - in.copy(input, length); - position = 0; - } - position += length; - } - -/* -* Flush buffers -*/ -void Base64_Encoder::end_msg() - { - size_t start_of_last_block = 3 * (position / 3), - left_over = position % 3; - encode_and_send(&in[0], start_of_last_block); - - if(left_over) - { - SecureVector remainder(3); - copy_mem(&remainder[0], &in[start_of_last_block], left_over); - - encode(&remainder[0], &out[0]); - - size_t empty_bits = 8 * (3 - left_over), index = 4 - 1; - while(empty_bits >= 8) - { - out[index--] = '='; - empty_bits -= 6; - } - - do_output(&out[0], 4); - } - - if(trailing_newline || (counter && line_length)) - send('\n'); - - counter = position = 0; - } - -/* -* Base64_Decoder Constructor -*/ -Base64_Decoder::Base64_Decoder(Decoder_Checking c) : checking(c) - { - in.resize(48); - out.resize(3); - position = 0; - } - -/* -* Check if a character is a valid Base64 char -*/ -bool Base64_Decoder::is_valid(byte in) - { - return (BASE64_TO_BIN[in] != 0x80); - } - -/* -* Base64 Decoding Operation -*/ -void Base64_Decoder::decode(const byte in[4], byte out[3]) - { - out[0] = ((BASE64_TO_BIN[in[0]] << 2) | (BASE64_TO_BIN[in[1]] >> 4)); - out[1] = ((BASE64_TO_BIN[in[1]] << 4) | (BASE64_TO_BIN[in[2]] >> 2)); - out[2] = ((BASE64_TO_BIN[in[2]] << 6) | (BASE64_TO_BIN[in[3]])); - } - -/* -* Decode and send a block -*/ -void Base64_Decoder::decode_and_send(const byte block[], size_t length) - { - for(size_t j = 0; j != length; j += 4) - { - decode(block + j, &out[0]); - send(out, 3); - } - } - -/* -* Handle processing an invalid character -*/ -void Base64_Decoder::handle_bad_char(byte c) - { - if(c == '=' || checking == NONE) - return; - - if((checking == IGNORE_WS) && Charset::is_space(c)) - return; - - throw Decoding_Error( - std::string("Base64_Decoder: Invalid base64 character '") + - static_cast(c) + "'" - ); - } - -/* -* Convert some data from Base64 -*/ -void Base64_Decoder::write(const byte input[], size_t length) - { - for(size_t j = 0; j != length; ++j) - { - if(is_valid(input[j])) - in[position++] = input[j]; - else - handle_bad_char(input[j]); - - if(position == in.size()) - { - decode_and_send(&in[0], in.size()); - position = 0; - } - } - } - -/* -* Flush buffers -*/ -void Base64_Decoder::end_msg() - { - if(position != 0) - { - size_t start_of_last_block = 4 * (position / 4), - left_over = position % 4; - decode_and_send(&in[0], start_of_last_block); - - if(left_over) - { - SecureVector remainder(4); - copy_mem(&remainder[0], &in[start_of_last_block], left_over); - decode(&remainder[0], &out[0]); - send(out, ((left_over == 1) ? (1) : (left_over - 1))); - } - } - position = 0; - } - -} diff --git a/src/filters/b64_filt/b64_filt.h b/src/filters/b64_filt/b64_filt.h deleted file mode 100644 index ae5fa26ce..000000000 --- a/src/filters/b64_filt/b64_filt.h +++ /dev/null @@ -1,98 +0,0 @@ -/* -* Base64 Encoder/Decoder -* (C) 1999-2007 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#ifndef BOTAN_BASE64_H__ -#define BOTAN_BASE64_H__ - -#include - -namespace Botan { - -/** -* This class represents a Base64 encoder. -*/ -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"; } - - /** - * Input a part of a message to the encoder. - * @param input the message to input as a byte array - * @param length the length of the byte array input - */ - void write(const byte input[], size_t length); - - /** - * Inform the Encoder that the current message shall be closed. - */ - void end_msg(); - - /** - * Create a base64 encoder. - * @param breaks whether to use line breaks in the Streamcipheroutput - * @param length the length of the lines of the output - * @param t_n whether to use a trailing newline - */ - Base64_Encoder(bool breaks = false, size_t length = 72, - bool t_n = false); - 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 in, out; - size_t position, counter; - }; - -/** -* This object represents a Base64 decoder. -*/ -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"; } - - /** - * Input a part of a message to the decoder. - * @param input the message to input as a byte array - * @param length the length of the byte array input - */ - void write(const byte input[], size_t length); - - /** - * Inform the Encoder that the current message shall be closed. - */ - void end_msg(); - - /** - * Create a base64 encoder. - * @param checking the type of checking that shall be performed by - * the decoder - */ - Base64_Decoder(Decoder_Checking checking = NONE); - private: - 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 in, out; - size_t position; - }; - -} - -#endif diff --git a/src/filters/b64_filt/info.txt b/src/filters/b64_filt/info.txt deleted file mode 100644 index 8fff2f181..000000000 --- a/src/filters/b64_filt/info.txt +++ /dev/null @@ -1,5 +0,0 @@ -define BASE64_FILTER - - -filters - diff --git a/src/filters/codec_filt/b64_filt.cpp b/src/filters/codec_filt/b64_filt.cpp new file mode 100644 index 000000000..2db6ac3ca --- /dev/null +++ b/src/filters/codec_filt/b64_filt.cpp @@ -0,0 +1,256 @@ +/* +* Base64 Encoder/Decoder +* (C) 1999-2008 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include +#include +#include +#include +#include + +namespace Botan { + +/* +* Base64 Decoder Lookup Table +* Warning: assumes ASCII encodings +*/ +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, +0x80, 0x80, 0x80, 0x80, 0x3E, 0x80, 0x80, 0x80, 0x3F, 0x34, 0x35, 0x36, 0x37, +0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, +0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, +0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, +0x2E, 0x2F, 0x30, 0x31, 0x32, 0x33, 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, 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, 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, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80 }; + +/* +* Base64_Encoder Constructor +*/ +Base64_Encoder::Base64_Encoder(bool breaks, size_t length, bool t_n) : + line_length(breaks ? length : 0), + trailing_newline(t_n && breaks), + in(48), + out(64), + position(0), + out_position(0) + { + } + +/* +* Encode and send a block +*/ +void Base64_Encoder::encode_and_send(const byte input[], size_t length) + { + while(length) + { + const size_t proc = std::min(length, in.size()); + + size_t consumed = 0; + size_t produced = base64_encode(reinterpret_cast(&out[0]), input, + proc, consumed, false); + + do_output(&out[0], produced); + + input += proc; + length -= proc; + } + } + +/* +* Handle the output +*/ +void Base64_Encoder::do_output(const byte input[], size_t length) + { + if(line_length == 0) + send(input, length); + else + { + size_t remaining = length, offset = 0; + while(remaining) + { + size_t sent = std::min(line_length - out_position, remaining); + send(input + offset, sent); + out_position += sent; + remaining -= sent; + offset += sent; + if(out_position == line_length) + { + send('\n'); + out_position = 0; + } + } + } + } + +/* +* Convert some data into Base64 +*/ +void Base64_Encoder::write(const byte input[], size_t length) + { + in.copy(position, input, length); + if(position + length >= in.size()) + { + encode_and_send(&in[0], in.size()); + input += (in.size() - position); + length -= (in.size() - position); + while(length >= in.size()) + { + encode_and_send(input, in.size()); + input += in.size(); + length -= in.size(); + } + in.copy(input, length); + position = 0; + } + position += length; + } + +/* +* Flush buffers +*/ +void Base64_Encoder::end_msg() + { + size_t start_of_last_block = 3 * (position / 3), + left_over = position % 3; + encode_and_send(&in[0], start_of_last_block); + + if(left_over) + { + SecureVector remainder(3); + copy_mem(&remainder[0], &in[start_of_last_block], left_over); + + size_t consumed; + base64_encode(reinterpret_cast(&out[0]), &remainder[0], 3, consumed, false); + + size_t empty_bits = 8 * (3 - left_over), index = 4 - 1; + while(empty_bits >= 8) + { + out[index--] = '='; + empty_bits -= 6; + } + + do_output(&out[0], 4); + } + + if(trailing_newline || (out_position && line_length)) + send('\n'); + + out_position = position = 0; + } + +/* +* Base64_Decoder Constructor +*/ +Base64_Decoder::Base64_Decoder(Decoder_Checking c) : checking(c) + { + in.resize(48); + out.resize(3); + position = 0; + } + +/* +* Check if a character is a valid Base64 char +*/ +bool Base64_Decoder::is_valid(byte in) + { + return (BASE64_TO_BIN[in] != 0x80); + } + +/* +* Base64 Decoding Operation +*/ +void Base64_Decoder::decode(const byte in[4], byte out[3]) + { + out[0] = ((BASE64_TO_BIN[in[0]] << 2) | (BASE64_TO_BIN[in[1]] >> 4)); + out[1] = ((BASE64_TO_BIN[in[1]] << 4) | (BASE64_TO_BIN[in[2]] >> 2)); + out[2] = ((BASE64_TO_BIN[in[2]] << 6) | (BASE64_TO_BIN[in[3]])); + } + +/* +* Decode and send a block +*/ +void Base64_Decoder::decode_and_send(const byte block[], size_t length) + { + for(size_t i = 0; i != length; i += 4) + { + decode(block + i, &out[0]); + send(out, 3); + } + } + +/* +* Handle processing an invalid character +*/ +void Base64_Decoder::handle_bad_char(byte c) + { + if(c == '=' || checking == NONE) + return; + + if((checking == IGNORE_WS) && Charset::is_space(c)) + return; + + throw Decoding_Error( + std::string("Base64_Decoder: Invalid base64 character '") + + static_cast(c) + "'" + ); + } + +/* +* Convert some data from Base64 +*/ +void Base64_Decoder::write(const byte input[], size_t length) + { + for(size_t i = 0; i != length; ++i) + { + if(is_valid(input[i])) + in[position++] = input[i]; + else + handle_bad_char(input[i]); + + if(position == in.size()) + { + decode_and_send(&in[0], in.size()); + position = 0; + } + } + } + +/* +* Flush buffers +*/ +void Base64_Decoder::end_msg() + { + if(position != 0) + { + size_t start_of_last_block = 4 * (position / 4), + left_over = position % 4; + decode_and_send(&in[0], start_of_last_block); + + if(left_over) + { + SecureVector remainder(4); + copy_mem(&remainder[0], &in[start_of_last_block], left_over); + decode(&remainder[0], &out[0]); + send(out, ((left_over == 1) ? (1) : (left_over - 1))); + } + } + position = 0; + } + +} diff --git a/src/filters/codec_filt/b64_filt.h b/src/filters/codec_filt/b64_filt.h new file mode 100644 index 000000000..8918049a2 --- /dev/null +++ b/src/filters/codec_filt/b64_filt.h @@ -0,0 +1,93 @@ +/* +* Base64 Encoder/Decoder +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_BASE64_FILTER_H__ +#define BOTAN_BASE64_FILTER_H__ + +#include + +namespace Botan { + +/** +* This class represents a Base64 encoder. +*/ +class BOTAN_DLL Base64_Encoder : public Filter + { + public: + std::string name() const { return "Base64_Encoder"; } + + /** + * Input a part of a message to the encoder. + * @param input the message to input as a byte array + * @param length the length of the byte array input + */ + void write(const byte input[], size_t length); + + /** + * Inform the Encoder that the current message shall be closed. + */ + void end_msg(); + + /** + * Create a base64 encoder. + * @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 + */ + Base64_Encoder(bool breaks = false, size_t length = 72, + bool t_n = false); + private: + void encode_and_send(const byte[], size_t); + void do_output(const byte[], size_t); + + const size_t line_length; + const bool trailing_newline; + SecureVector in, out; + size_t position, out_position; + }; + +/** +* This object represents a Base64 decoder. +*/ +class BOTAN_DLL Base64_Decoder : public Filter + { + public: + std::string name() const { return "Base64_Decoder"; } + + /** + * Input a part of a message to the decoder. + * @param input the message to input as a byte array + * @param length the length of the byte array input + */ + void write(const byte input[], size_t length); + + /** + * Inform the Encoder that the current message shall be closed. + */ + void end_msg(); + + /** + * Create a base64 encoder. + * @param checking the type of checking that shall be performed by + * the decoder + */ + 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); + + const Decoder_Checking checking; + SecureVector in, out; + size_t position; + }; + +} + +#endif diff --git a/src/filters/codec_filt/hex_filt.cpp b/src/filters/codec_filt/hex_filt.cpp new file mode 100644 index 000000000..3d56beec4 --- /dev/null +++ b/src/filters/codec_filt/hex_filt.cpp @@ -0,0 +1,172 @@ +/* +* Hex Encoder/Decoder +* (C) 1999-2007 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#include +#include +#include +#include +#include +#include + +namespace Botan { + +/** +* Size used for internal buffer in hex encoder/decoder +*/ +const size_t HEX_CODEC_BUFFER_SIZE = 256; + +/* +* Hex_Encoder Constructor +*/ +Hex_Encoder::Hex_Encoder(bool breaks, size_t length, Case c) : + casing(c), line_length(breaks ? length : 0) + { + in.resize(HEX_CODEC_BUFFER_SIZE); + out.resize(2*in.size()); + counter = position = 0; + } + +/* +* Hex_Encoder Constructor +*/ +Hex_Encoder::Hex_Encoder(Case c) : casing(c), line_length(0) + { + in.resize(HEX_CODEC_BUFFER_SIZE); + out.resize(2*in.size()); + counter = position = 0; + } + +/* +* Encode and send a block +*/ +void Hex_Encoder::encode_and_send(const byte block[], size_t length) + { + hex_encode(reinterpret_cast(&out[0]), + block, length, + casing == Uppercase); + + if(line_length == 0) + send(out, 2*length); + else + { + size_t remaining = 2*length, offset = 0; + while(remaining) + { + size_t sent = std::min(line_length - counter, remaining); + send(&out[offset], sent); + counter += sent; + remaining -= sent; + offset += sent; + if(counter == line_length) + { + send('\n'); + counter = 0; + } + } + } + } + +/* +* Convert some data into hex format +*/ +void Hex_Encoder::write(const byte input[], size_t length) + { + in.copy(position, input, length); + if(position + length >= in.size()) + { + encode_and_send(&in[0], in.size()); + input += (in.size() - position); + length -= (in.size() - position); + while(length >= in.size()) + { + encode_and_send(input, in.size()); + input += in.size(); + length -= in.size(); + } + in.copy(input, length); + position = 0; + } + position += length; + } + +/* +* Flush buffers +*/ +void Hex_Encoder::end_msg() + { + encode_and_send(&in[0], position); + if(counter && line_length) + send('\n'); + counter = position = 0; + } + +/* +* Hex_Decoder Constructor +*/ +Hex_Decoder::Hex_Decoder(Decoder_Checking c) : checking(c) + { + in.resize(HEX_CODEC_BUFFER_SIZE); + out.resize(in.size() / 2); + position = 0; + } + +/* +* Convert some data from hex format +*/ +void Hex_Decoder::write(const byte input[], size_t length) + { + while(length) + { + size_t to_copy = std::min(length, in.size() - position); + copy_mem(&in[position], input, to_copy); + position += to_copy; + + size_t consumed = 0; + size_t written = hex_decode(&out[0], + reinterpret_cast(&in[0]), + position, + consumed, + checking != FULL_CHECK); + + send(out, written); + + if(consumed != position) + { + copy_mem(&in[0], &in[consumed], position - consumed); + position = position - consumed; + } + else + position = 0; + + length -= to_copy; + input += to_copy; + } + } + +/* +* Flush buffers +*/ +void Hex_Decoder::end_msg() + { + size_t consumed = 0; + size_t written = hex_decode(&out[0], + reinterpret_cast(&in[0]), + position, + consumed, + checking != FULL_CHECK); + + send(out, written); + + const bool not_full_bytes = consumed != position; + + position = 0; + + if(not_full_bytes) + throw std::invalid_argument("Hex_Decoder: Input not full bytes"); + } + +} diff --git a/src/filters/codec_filt/hex_filt.h b/src/filters/codec_filt/hex_filt.h new file mode 100644 index 000000000..cfbb818d3 --- /dev/null +++ b/src/filters/codec_filt/hex_filt.h @@ -0,0 +1,81 @@ +/* +* Hex Encoder/Decoder +* (C) 1999-2010 Jack Lloyd +* +* Distributed under the terms of the Botan license +*/ + +#ifndef BOTAN_HEX_FILTER_H__ +#define BOTAN_HEX_FILTER_H__ + +#include + +namespace Botan { + +/** +* Converts arbitrary binary data to hex strings, optionally with +* newlines inserted +*/ +class BOTAN_DLL Hex_Encoder : public Filter + { + public: + /** + * Whether to use uppercase or lowercase letters for the encoded string. + */ + enum Case { Uppercase, Lowercase }; + + std::string name() const { return "Hex_Encoder"; } + + void write(const byte in[], size_t length); + void end_msg(); + + /** + * Create a hex encoder. + * @param the_case the case to use in the encoded strings. + */ + Hex_Encoder(Case the_case); + + /** + * Create a hex encoder. + * @param newlines should newlines be used + * @param line_length if newlines are used, how long are lines + * @param the_case the case to use in the encoded strings + */ + Hex_Encoder(bool newlines = false, + size_t line_length = 72, + Case the_case = Uppercase); + private: + void encode_and_send(const byte[], size_t); + + const Case casing; + const size_t line_length; + SecureVector in, out; + size_t position, counter; + }; + +/** +* Converts hex strings to bytes +*/ +class BOTAN_DLL Hex_Decoder : public Filter + { + public: + std::string name() const { return "Hex_Decoder"; } + + void write(const byte[], size_t); + void end_msg(); + + /** + * Construct a Hex Decoder using the specified + * character checking. + * @param checking the checking to use during decoding. + */ + Hex_Decoder(Decoder_Checking checking = NONE); + private: + const Decoder_Checking checking; + SecureVector in, out; + size_t position; + }; + +} + +#endif diff --git a/src/filters/codec_filt/info.txt b/src/filters/codec_filt/info.txt new file mode 100644 index 000000000..080cc8f44 --- /dev/null +++ b/src/filters/codec_filt/info.txt @@ -0,0 +1,5 @@ +define CODEC_FILTERS + + +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 -#if defined(BOTAN_HAS_BASE64_FILTER) +#if defined(BOTAN_HAS_CODEC_FILTERS) #include -#endif - -#if defined(BOTAN_HAS_HEX_FILTER) #include #endif diff --git a/src/filters/hex_filt/hex_filt.cpp b/src/filters/hex_filt/hex_filt.cpp deleted file mode 100644 index 3d56beec4..000000000 --- a/src/filters/hex_filt/hex_filt.cpp +++ /dev/null @@ -1,172 +0,0 @@ -/* -* Hex Encoder/Decoder -* (C) 1999-2007 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#include -#include -#include -#include -#include -#include - -namespace Botan { - -/** -* Size used for internal buffer in hex encoder/decoder -*/ -const size_t HEX_CODEC_BUFFER_SIZE = 256; - -/* -* Hex_Encoder Constructor -*/ -Hex_Encoder::Hex_Encoder(bool breaks, size_t length, Case c) : - casing(c), line_length(breaks ? length : 0) - { - in.resize(HEX_CODEC_BUFFER_SIZE); - out.resize(2*in.size()); - counter = position = 0; - } - -/* -* Hex_Encoder Constructor -*/ -Hex_Encoder::Hex_Encoder(Case c) : casing(c), line_length(0) - { - in.resize(HEX_CODEC_BUFFER_SIZE); - out.resize(2*in.size()); - counter = position = 0; - } - -/* -* Encode and send a block -*/ -void Hex_Encoder::encode_and_send(const byte block[], size_t length) - { - hex_encode(reinterpret_cast(&out[0]), - block, length, - casing == Uppercase); - - if(line_length == 0) - send(out, 2*length); - else - { - size_t remaining = 2*length, offset = 0; - while(remaining) - { - size_t sent = std::min(line_length - counter, remaining); - send(&out[offset], sent); - counter += sent; - remaining -= sent; - offset += sent; - if(counter == line_length) - { - send('\n'); - counter = 0; - } - } - } - } - -/* -* Convert some data into hex format -*/ -void Hex_Encoder::write(const byte input[], size_t length) - { - in.copy(position, input, length); - if(position + length >= in.size()) - { - encode_and_send(&in[0], in.size()); - input += (in.size() - position); - length -= (in.size() - position); - while(length >= in.size()) - { - encode_and_send(input, in.size()); - input += in.size(); - length -= in.size(); - } - in.copy(input, length); - position = 0; - } - position += length; - } - -/* -* Flush buffers -*/ -void Hex_Encoder::end_msg() - { - encode_and_send(&in[0], position); - if(counter && line_length) - send('\n'); - counter = position = 0; - } - -/* -* Hex_Decoder Constructor -*/ -Hex_Decoder::Hex_Decoder(Decoder_Checking c) : checking(c) - { - in.resize(HEX_CODEC_BUFFER_SIZE); - out.resize(in.size() / 2); - position = 0; - } - -/* -* Convert some data from hex format -*/ -void Hex_Decoder::write(const byte input[], size_t length) - { - while(length) - { - size_t to_copy = std::min(length, in.size() - position); - copy_mem(&in[position], input, to_copy); - position += to_copy; - - size_t consumed = 0; - size_t written = hex_decode(&out[0], - reinterpret_cast(&in[0]), - position, - consumed, - checking != FULL_CHECK); - - send(out, written); - - if(consumed != position) - { - copy_mem(&in[0], &in[consumed], position - consumed); - position = position - consumed; - } - else - position = 0; - - length -= to_copy; - input += to_copy; - } - } - -/* -* Flush buffers -*/ -void Hex_Decoder::end_msg() - { - size_t consumed = 0; - size_t written = hex_decode(&out[0], - reinterpret_cast(&in[0]), - position, - consumed, - checking != FULL_CHECK); - - send(out, written); - - const bool not_full_bytes = consumed != position; - - position = 0; - - if(not_full_bytes) - throw std::invalid_argument("Hex_Decoder: Input not full bytes"); - } - -} diff --git a/src/filters/hex_filt/hex_filt.h b/src/filters/hex_filt/hex_filt.h deleted file mode 100644 index cfbb818d3..000000000 --- a/src/filters/hex_filt/hex_filt.h +++ /dev/null @@ -1,81 +0,0 @@ -/* -* Hex Encoder/Decoder -* (C) 1999-2010 Jack Lloyd -* -* Distributed under the terms of the Botan license -*/ - -#ifndef BOTAN_HEX_FILTER_H__ -#define BOTAN_HEX_FILTER_H__ - -#include - -namespace Botan { - -/** -* Converts arbitrary binary data to hex strings, optionally with -* newlines inserted -*/ -class BOTAN_DLL Hex_Encoder : public Filter - { - public: - /** - * Whether to use uppercase or lowercase letters for the encoded string. - */ - enum Case { Uppercase, Lowercase }; - - std::string name() const { return "Hex_Encoder"; } - - void write(const byte in[], size_t length); - void end_msg(); - - /** - * Create a hex encoder. - * @param the_case the case to use in the encoded strings. - */ - Hex_Encoder(Case the_case); - - /** - * Create a hex encoder. - * @param newlines should newlines be used - * @param line_length if newlines are used, how long are lines - * @param the_case the case to use in the encoded strings - */ - Hex_Encoder(bool newlines = false, - size_t line_length = 72, - Case the_case = Uppercase); - private: - void encode_and_send(const byte[], size_t); - - const Case casing; - const size_t line_length; - SecureVector in, out; - size_t position, counter; - }; - -/** -* Converts hex strings to bytes -*/ -class BOTAN_DLL Hex_Decoder : public Filter - { - public: - std::string name() const { return "Hex_Decoder"; } - - void write(const byte[], size_t); - void end_msg(); - - /** - * Construct a Hex Decoder using the specified - * character checking. - * @param checking the checking to use during decoding. - */ - Hex_Decoder(Decoder_Checking checking = NONE); - private: - const Decoder_Checking checking; - SecureVector in, out; - size_t position; - }; - -} - -#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 - - -filters - -- cgit v1.2.3