Botan
1.11.15
|
00001 /* 00002 * Hex Encoder/Decoder 00003 * (C) 1999-2010 Jack Lloyd 00004 * 00005 * Botan is released under the Simplified BSD License (see license.txt) 00006 */ 00007 00008 #ifndef BOTAN_HEX_FILTER_H__ 00009 #define BOTAN_HEX_FILTER_H__ 00010 00011 #include <botan/filter.h> 00012 00013 namespace Botan { 00014 00015 /** 00016 * Converts arbitrary binary data to hex strings, optionally with 00017 * newlines inserted 00018 */ 00019 class BOTAN_DLL Hex_Encoder : public Filter 00020 { 00021 public: 00022 /** 00023 * Whether to use uppercase or lowercase letters for the encoded string. 00024 */ 00025 enum Case { Uppercase, Lowercase }; 00026 00027 std::string name() const { return "Hex_Encoder"; } 00028 00029 void write(const byte in[], size_t length); 00030 void end_msg(); 00031 00032 /** 00033 * Create a hex encoder. 00034 * @param the_case the case to use in the encoded strings. 00035 */ 00036 Hex_Encoder(Case the_case); 00037 00038 /** 00039 * Create a hex encoder. 00040 * @param newlines should newlines be used 00041 * @param line_length if newlines are used, how long are lines 00042 * @param the_case the case to use in the encoded strings 00043 */ 00044 Hex_Encoder(bool newlines = false, 00045 size_t line_length = 72, 00046 Case the_case = Uppercase); 00047 private: 00048 void encode_and_send(const byte[], size_t); 00049 00050 const Case casing; 00051 const size_t line_length; 00052 std::vector<byte> in, out; 00053 size_t position, counter; 00054 }; 00055 00056 /** 00057 * Converts hex strings to bytes 00058 */ 00059 class BOTAN_DLL Hex_Decoder : public Filter 00060 { 00061 public: 00062 std::string name() const { return "Hex_Decoder"; } 00063 00064 void write(const byte[], size_t); 00065 void end_msg(); 00066 00067 /** 00068 * Construct a Hex Decoder using the specified 00069 * character checking. 00070 * @param checking the checking to use during decoding. 00071 */ 00072 Hex_Decoder(Decoder_Checking checking = NONE); 00073 private: 00074 const Decoder_Checking checking; 00075 std::vector<byte> in, out; 00076 size_t position; 00077 }; 00078 00079 } 00080 00081 #endif