aboutsummaryrefslogtreecommitdiffstats
path: root/src/codec
diff options
context:
space:
mode:
authorlloyd <[email protected]>2010-09-03 13:15:08 +0000
committerlloyd <[email protected]>2010-09-03 13:15:08 +0000
commit466548a644640fc73b24c8a4185a317f780a698f (patch)
tree27e29d709e334d4a6ece30d51b3be57e4aedd3bb /src/codec
parentdaad01ef80771fb709e7cb6f2f7d5f6fcd6fd52f (diff)
Add a standalone version of hex encoding and decoding, defining the filters
in terms of these calls. The header for the hex filter is renamed hex_filt.h. This probably won't affect people because filters.h (included by botan.h) already included hex.h, and now just includes hex_filt.h instead.
Diffstat (limited to 'src/codec')
-rw-r--r--src/codec/hex/hex.cpp180
-rw-r--r--src/codec/hex/hex.h108
-rw-r--r--src/codec/hex/info.txt1
3 files changed, 289 insertions, 0 deletions
diff --git a/src/codec/hex/hex.cpp b/src/codec/hex/hex.cpp
new file mode 100644
index 000000000..70e819906
--- /dev/null
+++ b/src/codec/hex/hex.cpp
@@ -0,0 +1,180 @@
+/*
+* Hex Encoding and Decoding
+* (C) 2010 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#include <botan/hex.h>
+#include <botan/mem_ops.h>
+#include <stdexcept>
+
+namespace Botan {
+
+void hex_encode(char output[],
+ const byte input[],
+ u32bit input_length,
+ bool uppercase)
+ {
+ static const byte BIN_TO_HEX_UPPER[16] = {
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+ 'A', 'B', 'C', 'D', 'E', 'F' };
+
+ static const byte BIN_TO_HEX_LOWER[16] = {
+ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
+ 'a', 'b', 'c', 'd', 'e', 'f' };
+
+ const byte* tbl = uppercase ? BIN_TO_HEX_UPPER : BIN_TO_HEX_LOWER;
+
+ for(u32bit i = 0; i != input_length; ++i)
+ {
+ byte x = input[i];
+ output[2*i ] = tbl[(x >> 4) & 0x0F];
+ output[2*i+1] = tbl[(x ) & 0x0F];
+ }
+ }
+
+std::string hex_encode(const byte input[],
+ u32bit input_length,
+ bool uppercase)
+ {
+ std::string output(2 * input_length, 0);
+ hex_encode(&output[0], input, input_length, uppercase);
+ return output;
+ }
+
+u32bit hex_decode(byte output[],
+ const char input[],
+ u32bit input_length,
+ u32bit& input_consumed,
+ bool ignore_ws)
+ {
+ /*
+ * Mapping of hex characters to either their binary equivalent
+ * or to an error code.
+ * If valid hex (0-9 A-F a-f), the value.
+ * If whitespace, then 0x80
+ * Otherwise 0xFF
+ * Warning: this table assumes ASCII character encodings
+ */
+
+ static const byte HEX_TO_BIN[256] = {
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x80,
+ 0x80, 0xFF, 0xFF, 0xFF, 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, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x01,
+ 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
+ 0x0F, 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, 0x0A, 0x0B, 0x0C,
+ 0x0D, 0x0E, 0x0F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
+ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 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::string bad_char(1, input[i]);
+ if(bad_char == "\t")
+ bad_char = "\\t";
+ else if(bad_char == "\n")
+ bad_char = "\\n";
+
+ throw std::invalid_argument(
+ std::string("hex_decode: invalid hex character '") +
+ bad_char + "'");
+ }
+
+ *out_ptr |= bin << (top_nibble*4);
+
+ top_nibble = !top_nibble;
+ if(top_nibble)
+ ++out_ptr;
+ }
+
+ input_consumed = input_length;
+ u32bit written = (out_ptr - output);
+
+ /*
+ * We only got half of a byte at the end; zap the half-written
+ * output and mark it as unread
+ */
+ if(!top_nibble)
+ {
+ *out_ptr = 0;
+ input_consumed -= 1;
+ }
+
+ return written;
+ }
+
+u32bit hex_decode(byte output[],
+ const char input[],
+ u32bit input_length,
+ bool ignore_ws)
+ {
+ u32bit consumed = 0;
+ u32bit written = hex_decode(output, input, input_length,
+ consumed, ignore_ws);
+
+ if(consumed != input_length)
+ throw std::invalid_argument("hex_decode: input did not have full bytes");
+
+ return written;
+ }
+
+u32bit hex_decode(byte output[],
+ const std::string& input,
+ bool ignore_ws)
+ {
+ return hex_decode(output, &input[0], input.length(), ignore_ws);
+ }
+
+SecureVector<byte> hex_decode(const char input[],
+ u32bit input_length,
+ bool ignore_ws)
+ {
+ SecureVector<byte> bin(input_length / 2);
+
+ u32bit written = hex_decode(&bin[0],
+ input,
+ input_length,
+ ignore_ws);
+
+ bin.truncate(written);
+ return bin;
+ }
+
+SecureVector<byte> hex_decode(const std::string& input,
+ bool ignore_ws)
+ {
+ return hex_decode(&input[0], input.size(), ignore_ws);
+ }
+
+}
diff --git a/src/codec/hex/hex.h b/src/codec/hex/hex.h
new file mode 100644
index 000000000..91a743b45
--- /dev/null
+++ b/src/codec/hex/hex.h
@@ -0,0 +1,108 @@
+/*
+* Hex Encoding and Decoding
+* (C) 2010 Jack Lloyd
+*
+* Distributed under the terms of the Botan license
+*/
+
+#ifndef BOTAN_HEX_CODEC_H__
+#define BOTAN_HEX_CODEC_H__
+
+#include <botan/secmem.h>
+#include <string>
+
+namespace Botan {
+
+/**
+* Perform hex encoding
+* @param output an array of at least input_length*2 bytes
+* @param input is some binary data
+* @param input_length length of input in bytes
+* @param uppercase should output be upper or lower case?
+*/
+void BOTAN_DLL hex_encode(char output[],
+ const byte input[],
+ u32bit input_length,
+ bool uppercase = true);
+
+/**
+* Perform hex encoding
+* @param input some input
+* @param input_length length of input in bytes
+* @param uppercase should output be upper or lower case?
+* @return hexadecimal representation of input
+*/
+std::string BOTAN_DLL hex_encode(const byte input[],
+ u32bit input_length,
+ bool uppercase = true);
+
+/**
+* Perform hex decoding
+* @param output an array of at least input_length/2 bytes
+* @param input some hex input
+* @param input_length length of input in bytes
+* @param input_consumed is an output parameter which says how many
+* bytes of input were actually consumed. If less than
+* input_length, then the range input[consumed:length]
+* should be passed in later along with more input.
+* @param ignore_ws ignore whitespace on input; if false, throw an
+ exception if whitespace is encountered
+* @return number of bytes written to output
+*/
+u32bit BOTAN_DLL hex_decode(byte output[],
+ const char input[],
+ u32bit input_length,
+ u32bit& input_consumed,
+ bool ignore_ws = true);
+
+/**
+* Perform hex decoding
+* @param output an array of at least input_length/2 bytes
+* @param input some hex input
+* @param input_length length of input in bytes
+* @param ignore_ws ignore whitespace on input; if false, throw an
+ exception if whitespace is encountered
+* @return number of bytes written to output
+*/
+u32bit BOTAN_DLL hex_decode(byte output[],
+ const char input[],
+ u32bit input_length,
+ bool ignore_ws = true);
+
+/**
+* Perform hex decoding
+* @param output an array of at least input_length/2 bytes
+* @param input some hex input
+* @param ignore_ws ignore whitespace on input; if false, throw an
+ exception if whitespace is encountered
+* @return number of bytes written to output
+*/
+u32bit BOTAN_DLL hex_decode(byte output[],
+ const std::string& input,
+ bool ignore_ws = true);
+
+/**
+* Perform hex decoding
+* @param input some hex input
+* @param input_length the length of input in bytes
+* @param ignore_ws ignore whitespace on input; if false, throw an
+ exception if whitespace is encountered
+* @return decoded hex output
+*/
+SecureVector<byte> BOTAN_DLL hex_decode(const char input[],
+ u32bit input_length,
+ bool ignore_ws = true);
+
+/**
+* Perform hex decoding
+* @param input some hex input
+* @param ignore_ws ignore whitespace on input; if false, throw an
+ exception if whitespace is encountered
+* @return decoded hex output
+*/
+SecureVector<byte> BOTAN_DLL hex_decode(const std::string& input,
+ bool ignore_ws = true);
+
+}
+
+#endif
diff --git a/src/codec/hex/info.txt b/src/codec/hex/info.txt
new file mode 100644
index 000000000..c9eb8fb73
--- /dev/null
+++ b/src/codec/hex/info.txt
@@ -0,0 +1 @@
+define HEX_CODEC