1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright (C) 2024 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef ENCODE_H
#define ENCODE_H

#include <stdint.h><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <vector><--- Include file:  not found. Please note: Cppcheck does not need standard library headers to get proper results.

namespace isc {
namespace util {
namespace encode {

/// @brief Class for encoding and decoding binary data using an algorithm
/// described in RFC 4648.
class BaseNEncoder {
public:

    /// @brief Constructor
    ///
    /// @param algorithm name of the algorithm, used for logging
    /// @param digit_set set of digits (i.e. alphabet) used for encoding
    /// @param bits_table table to translate digits to data used during decoding
    /// @param bits_per_digit number of data bits represented by a digit
    /// @param digits_per_group number of digits contained in a group
    /// @param pad_char character used for padding out to group size (0 means no
    /// padding)
    /// @param max_pad maximum number of pad characters in a group
    /// @param case_sensitive indicates if the algorithm's digit set is
    /// case sensitive
    BaseNEncoder(const std::string& algorithm,
                 const char* digit_set,
                 const std::vector<uint8_t>& bits_table,
                 size_t bits_per_digit,
                 size_t digits_per_group,
                 const char pad_char,
                 size_t max_pad,
                 bool case_sensitive);

    /// @brief Destructor
    virtual ~BaseNEncoder() = default;

    /// @brief Encodes binary data using the encoder's algorithm
    ///
    /// @param input binary data to encode
    ///
    /// @return resultant encoded data string
    /// @throw BadValue if an error occurs during encoding
    std::string encode(const std::vector<uint8_t>& input);

    /// @brief Decodes an encoded string using the encoder's algorithm
    ///
    /// @param encoded_str encoded string to decode
    /// @param[out] output vector into which the decoded data is stored
    ///
    /// @throw BadValue if an error occurs during decoding
    void decode(const std::string& encoded_str, std::vector<uint8_t>& output);

    /// @brief Translate a byte of binary data into the appropriate algorithm digit
    ///
    /// @param bits binary value to translate
    ///
    /// @return char containing the digit corresponding to the binary value
    /// @throw BadValue if the bits value is out of range
    char bitsToDigit(uint8_t bits);

    /// @brief Translate a digit into the appropriate algorithm bit value
    ///
    /// Function maps all 256 ASCII chars to their corresponding algorithm-specific
    /// data value.  A data value of 0xee marks a char as whitespace, 0xff marks a
    /// char is invalid.
    ///
    /// @param digit the algorithm digit to translate
    ///
    /// @return byte containing the binary value corresponding to the digit
    uint8_t digitToBits(uint8_t digit);

    /// @brief Get the algorithm name
    ///
    /// @return string containing the algorithm name
    std::string getAlgorithm() const {
        return (algorithm_);
    }

    /// @brief Get the digit set
    ///
    /// @return string containing the set of digits
    const char* getDigitSet() const {
        return (digit_set_);
    }

    /// @brief Get the digit lookup table
    ///
    /// @return vector containing the lookup table
    const std::vector<uint8_t>& getBitsTable() const {
        return (bits_table_);
    }

    /// @brief Get the number of data bits represented by a digit
    ///
    /// @return number of data bits per digit
    size_t getBitsPerDigit() {
        return (bits_per_digit_);
    }

    /// @brief Get the number of digits contained in a group
    ///
    /// @return number of digits per group
    size_t getDigitsPerGroup() const {
        return (digits_per_group_);
    }

    /// @brief Get the character used for padding out to group size (0 means no padding)
    ///
    /// @return Character used as a pad byte
    uint8_t getPadChar() const {
        return (pad_char_);
    }

    /// @brief Get the maximum number of pad characters in a group
    ///
    /// @return Maximum number of pad characters
    size_t getMaxPad() {
        return (max_pad_);
    }

    /// @brief Get the maxium index value of the digit set
    ///
    /// @return Maxium index value of the digit set
    size_t getMaxBitsToDigit() {
        return (max_bits_to_digit_);
    }

    /// @brief Get the maxium index value of the algorithm bit table
    ///
    /// @return Maxium index value of the algorithm bit table
    size_t getMaxDigitToBits() {
        return (max_digit_to_bits_);
    }

    /// @brief Indicates whether or not the algorithm's digit set
    /// is case-sensitive.
    ///
    /// @return true if the digit set is case-sensitive, false otherwise
    bool isCaseSensitive() {
        return (case_sensitive_);
    }

protected:
    /// @brief Name of the algorithm, used for logging
    std::string algorithm_;

    /// @brief Set of digits (i.e. alphabet) used for encoding
    const char* digit_set_;

    /// @brief Table to translate digits to data used during decoding
    ///
    /// The table must map all 256 ASCII chars to their corresponding
    /// algorithm-specific data value.  A data value of 0xee marks
    /// a char as whitespace, 0xff marks a char is invalid
    std::vector<uint8_t>bits_table_;

    /// @brief Number of data bits represented by a digit
    size_t bits_per_digit_;

    /// @brief Number of digits contained in a group
    size_t digits_per_group_;

    /// @brief Character used for padding out to group size (0 means no padding)
    const char pad_char_;

    /// @brief Maximum number of pad characters in a group
    size_t max_pad_;

    /// @brief Indicates whether or not the algorithm's digit set is case-sensitive
    bool case_sensitive_;

    /// @brief Maxium index value of the digit set
    size_t max_bits_to_digit_;

    /// @brief Maxium index value of the algorithm bit table
    size_t max_digit_to_bits_;
};

/// @brief Class for encoding and decoding binary data using Base64
/// as described in RFC 4648.
class Base64Encoder : public BaseNEncoder {
public:
    /// @brief Set of digits used for encoding in Base64
    static const char* DIGIT_SET;

    /// @brief Table that maps Base64 digits to their binary data value
    static const std::vector<uint8_t> BITS_TABLE;

    /// @brief Constructor
    Base64Encoder()
     : BaseNEncoder("base64", DIGIT_SET, BITS_TABLE, 6, 4, '=', 2, true) {
    }

    /// @brief Destructor
    ~Base64Encoder() = default;
};

/// @brief Class for encoding and decoding binary data using Base32Hex
/// as described in RFC 4648.
class Base32HexEncoder : public BaseNEncoder {
public:
    /// @brief Set of digits used for encoding in Base32Hex
    static const char* DIGIT_SET;

    /// @brief Table that maps Base32Hex digits to their binary data value
    static const std::vector<uint8_t> BITS_TABLE;

    /// @brief Constructor
    Base32HexEncoder()
     : BaseNEncoder("base32Hex", DIGIT_SET, BITS_TABLE, 5, 8, '=', 6, false) {
    }

    /// @brief Destructor
    ~Base32HexEncoder() = default;
};

/// @brief Class for encoding and decoding binary data using Base16 (aka Hex)
/// as described in RFC 4648.
class Base16Encoder : public BaseNEncoder {
public:
    /// @brief Set of digits used for encoding in Base16
    static const char* DIGIT_SET;

    /// @brief Table that maps Base16 digits to their binary data value
    static const std::vector<uint8_t> BITS_TABLE;

    /// @brief Constructor
    Base16Encoder()
     : BaseNEncoder("base16", DIGIT_SET, BITS_TABLE, 4, 2, '=', 0, false) {
    }

    /// @brief Destructor
    ~Base16Encoder() = default;
};

/// @brief Encode binary data in the base32-hex format.
///
/// @param binary vector object storing the data to be encoded.
/// @return string containing the base32-hex encoded value.
std::string encodeBase32Hex(const std::vector<uint8_t>& binary);

/// @brief Decode a base32-hex encoded string into binary data.
///
/// @param encoded_str string containing a base32-hex encoded value.
/// @param[out] output vector into which the decoded binary data is stored.
///
/// @throw BadValue if the input string is invalid.
void decodeBase32Hex(const std::string& encoded_str, std::vector<uint8_t>& output);

/// @brief Encode binary data in the base64 format.
///
/// @param binary vector object storing the data to be encoded.
/// @return string containing the base64 encoded value.
std::string encodeBase64(const std::vector<uint8_t>& binary);

/// @brief Decode a base64 encoded string into binary data.
///
/// @param encoded_str string containing a base64 encoded value.
/// @param[out] output vector into which the decoded binary data is stored.
///
/// @throw BadValue if the input string is invalid.
void decodeBase64(const std::string& encoded_str, std::vector<uint8_t>& output);

/// @brief Encode binary data in the base16 format.
///
/// @param binary vector object containing the data to be encoded.
/// @return string containing the base16 encoded value.
std::string encodeHex(const std::vector<uint8_t>& binary);

/// @brief Decode a base16 encoded string into binary data.
///
/// @param encoded_str string containing a base16 encoded value.
/// @param[out] output vector into which the decoded binary data is stored.
///
/// @throw BadValue if the input string is invalid.
void decodeHex(const std::string& encoded_str, std::vector<uint8_t>& output);

/// @brief Encode in hexadecimal inline.
///
/// @param value the value to encode.
///
/// @return 0x followed by the value encoded in hex.
inline std::string toHex(std::string value) {
    std::vector<uint8_t> bin(value.begin(), value.end());
    return ("0x" + encodeHex(bin));
}

} // namespace encode
} // namespace util
} // namespace isc

#endif  // ENCODE_H