@@ -15,6 +15,230 @@ namespace isc {
1515namespace util {
1616namespace encode {
1717
18+ // / @brief Class for encoding and decoding binary data using an algorithm
19+ // / described in RFC 4648.
20+ class BaseNEncoder {
21+ public:
22+
23+ // / @Brief Constructor
24+ // /
25+ // / @param algorithm name of the algorithm, used for logging
26+ // / @param digit_set set of digits (i.e. alphabet) used for encoding
27+ // / @param bits_table table to translate digits to data used during decoding
28+ // / @param bits_per_digit number of data bits represented by a digit
29+ // / @param digits_per_group number of digits contained in a group
30+ // / @param pad_char character used for padding out to group size (0 means no
31+ // / padding)
32+ // / @param max_pad maximum number of pad characters in a group
33+ // / @param case_sensitive indicates if the algorithm's digit set is
34+ // / case sensitive.
35+ BaseNEncoder (const std::string& algorithm,
36+ const char * digit_set,
37+ const std::vector<uint8_t >& bits_table,
38+ size_t bits_per_digit,
39+ size_t digits_per_group,
40+ const char pad_char,
41+ size_t max_pad,
42+ bool case_sensitive);
43+
44+ // / @Brief Destructor
45+ virtual ~BaseNEncoder () = default ;
46+
47+ // / @brief Encodes binary data using the encoder's algorithm
48+ // /
49+ // / @param input binary data to encode
50+ // / @return resultant encoded data string
51+ // / @throw BadValue if an error occurs during encoding
52+ std::string encode (const std::vector<uint8_t >& input);
53+
54+ // / @brief Decodes an encoded string using the encoder's algorithm
55+ // /
56+ // / @param encoded_str encoded string to decode
57+ // / @param[out] output vector into which the decoded data is stored
58+ // / @throw BadValue if an error occurs during decoding
59+ void decode (const std::string& encoded_str, std::vector<uint8_t >& output);
60+
61+ // / @brief Translate a byte of binary data into the appropriate algorithm digit
62+ // /
63+ // / @param bits binary value to translate
64+ // / @return char containing the digit corresponding to the binary value
65+ // / @isc_throw BadValue if the bits value is out of range
66+ char bitsToDigit (uint8_t bits);
67+
68+ // / @brief Translate a digit into the appropriate algorithm bit value
69+ // /
70+ // / Function maps all 256 ASCII chars to their corresponding algorithm-specific
71+ // / data value. A data value of 0xee marks a char as whitespace, 0xff marks a
72+ // / char is invalid.
73+ // /
74+ // / @param digit the algorithm digit to translate
75+ // / @return byte containing the binary value corresponding to the digit
76+ uint8_t digitToBits (uint8_t digit);
77+
78+ // / @brief Get the algorithm name
79+ // /
80+ // / @return string containing the algorithm name
81+ std::string getAlgorithm () const {
82+ return (algorithm_);
83+ }
84+
85+ // / @brief Get the digit set
86+ // /
87+ // / @return string containing the set of digits
88+ const char * getDigitSet () const {
89+ return (digit_set_);
90+ }
91+
92+ // / @brief Get the digit lookup table
93+ // /
94+ // / @return vector containing the lookup table
95+ const std::vector<uint8_t >& getBitsTable () const {
96+ return (bits_table_);
97+ }
98+
99+ // / @brief Get the number of data bits represented by a digit
100+ // /
101+ // / @return number of data bits per digit
102+ size_t getBitsPerDigit () {
103+ return (bits_per_digit_);
104+ }
105+
106+ // / @brief Get the number of digits contained in a group
107+ // /
108+ // / @return number of digits per group
109+ size_t getDigitsPerGroup () const {
110+ return (digits_per_group_);
111+ }
112+
113+ // / @brief Get the character used for padding out to group size (0 means no padding)
114+ // /
115+ // / @return Character used as a pad byte
116+ uint8_t getPadChar () const {
117+ return (pad_char_);
118+ }
119+
120+ // / @brief Get the maximum number of pad characters in a group
121+ // /
122+ // / @return Maximum number of pad characters
123+ size_t getMaxPad () {
124+ return (max_pad_);
125+ }
126+
127+ // / @brief Get the maxium index value of the digit set
128+ // /
129+ // / @return Maxium index value of the digit set
130+ size_t getMaxBitsToDigit () {
131+ return (max_bits_to_digit_);
132+ }
133+
134+ // / @brief Get the maxium index value of the algorithm bit table
135+ // /
136+ // / @return Maxium index value of the algorithm bit table
137+ size_t getMaxDigitToBits () {
138+ return (max_digit_to_bits_);
139+ }
140+
141+ // / @brief Indicates whether or not the algorithm's digit set
142+ // / is case-sensitive.
143+ // /
144+ // / @return true if the digit set is case-sensitive.
145+ bool isCaseSensitive () {
146+ return (case_sensitive_);
147+ }
148+
149+ protected:
150+ // / @brief Name of the algorithm, used for logging
151+ std::string algorithm_;
152+
153+ // / @brief Set of digits (i.e. alphabet) used for encoding
154+ const char * digit_set_;
155+
156+ // / @brief Table to translate digits to data used during decoding
157+ // /
158+ // / The table must map all 256 ASCII chars to their corresponding
159+ // / algorithm-specific data value. A data value of 0xee marks
160+ // / a char as whitespace, 0xff marks a char is invalid.
161+ std::vector<uint8_t >bits_table_;
162+
163+ // / @brief Number of data bits represented by a digit
164+ size_t bits_per_digit_;
165+
166+ // / @brief Number of digits contained in a group
167+ size_t digits_per_group_;
168+
169+ // / @brief Character used for padding out to group size (0 means no padding)
170+ const char pad_char_;
171+
172+ // / @brief Maximum number of pad characters in a group
173+ size_t max_pad_;
174+
175+ // / @brief Indicates whether or not the algorithm's digit set is case-sensitive.
176+ bool case_sensitive_;
177+
178+ // / @brief Maxium index value of the digit set
179+ size_t max_bits_to_digit_;
180+
181+ // / @brief Maxium index value of the algorithm bit table
182+ size_t max_digit_to_bits_;
183+ };
184+
185+ // / @brief Class for encoding and decoding binary data using Base64
186+ // / as described in RFC 4648.
187+ class Base64Encoder : public BaseNEncoder {
188+ public:
189+ // / @brief Set of digits used for encoding in Base64
190+ static const char * DIGIT_SET;
191+
192+ // / @brief Table that maps Base64 digits to their binary data value
193+ static const std::vector<uint8_t > BITS_TABLE;
194+
195+ // / @brief Constructor
196+ Base64Encoder ()
197+ : BaseNEncoder(" base64" , DIGIT_SET, BITS_TABLE, 6 , 4 , ' =' , 2 , true ) {
198+ }
199+
200+ // / @brief Destructor
201+ ~Base64Encoder () = default ;
202+ };
203+
204+ // / @brief Class for encoding and decoding binary data using Base32Hex
205+ // / as described in RFC 4648.
206+ class Base32HexEncoder : public BaseNEncoder {
207+ public:
208+ // / @brief Set of digits used for encoding in Base32Hex
209+ static const char * DIGIT_SET;
210+
211+ // / @brief Table that maps Base32Hex digits to their binary data value
212+ static const std::vector<uint8_t > BITS_TABLE;
213+
214+ // / @brief Constructor
215+ Base32HexEncoder ()
216+ : BaseNEncoder(" base32Hex" , DIGIT_SET, BITS_TABLE, 5 , 8 , ' =' , 6 , false ) {
217+ }
218+
219+ // / @brief Destructor
220+ ~Base32HexEncoder () = default ;
221+ };
222+
223+ // / @brief Class for encoding and decoding binary data using Base16 (aka Hex)
224+ // / as described in RFC 4648.
225+ class Base16Encoder : public BaseNEncoder {
226+ public:
227+ // / @brief Set of digits used for encoding in Base16
228+ static const char * DIGIT_SET;
229+
230+ // / @brief Table that maps Base16 digits to their binary data value
231+ static const std::vector<uint8_t > BITS_TABLE;
232+
233+ // / @brief Constructor
234+ Base16Encoder ()
235+ : BaseNEncoder(" base16" , DIGIT_SET, BITS_TABLE, 4 , 2 , ' =' , 0 , false ) {
236+ }
237+
238+ // / @brief Destructor
239+ ~Base16Encoder () = default ;
240+ };
241+
18242// / @brief Encode binary data in the base32-hex format.
19243// /
20244// / @param binary vector object storing the data to be encoded.
@@ -57,7 +281,7 @@ void decodeHex(const std::string& encoded_str, std::vector<uint8_t>& output);
57281// / @brief Encode in hexadecimal inline
58282// /
59283// / @param value the value to encode
60- // / @return 0x followed by the value encoded in hexa
284+ // / @return 0x followed by the value encoded in hex
61285inline std::string toHex (std::string value) {
62286 std::vector<uint8_t > bin (value.begin (), value.end ());
63287 return (" 0x" + encodeHex (bin));
0 commit comments