Skip to content

Commit 440f19b

Browse files
authored
Add Base64 encoding and decoding (#1894)
1 parent 8a363aa commit 440f19b

File tree

6 files changed

+747
-0
lines changed

6 files changed

+747
-0
lines changed

Common++/header/GeneralUtils.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <string>
44
#include <cstdint>
55
#include <type_traits>
6+
#include <vector>
67

78
/// @file
89

@@ -55,6 +56,65 @@ namespace pcpp
5556
return (number + mask) & ~mask;
5657
}
5758

59+
/// @class Base64
60+
/// A class for encoding and decoding strings/data using Base64 algorithm
61+
/// This implementation is based on the work by Tobias Locker, available at https://github.com/tobiaslocker/base64
62+
class Base64
63+
{
64+
public:
65+
/// Encode an array of bytes to a Base64 string
66+
/// @param[in] input The array of bytes to be encoded
67+
/// @param[in] inputLen The length of the input array [in bytes]
68+
/// @return The encoded string
69+
static std::string encode(const uint8_t* input, size_t inputLen);
70+
71+
/// Encode a string to a Base64 string
72+
/// @param[in] input The string to be encoded
73+
/// @return The encoded string
74+
static std::string encode(const std::string& input);
75+
76+
/// Encode a hex string to a Base64 string
77+
/// @param[in] hexStringInput The hex string to be encoded
78+
/// @return The encoded string
79+
static std::string encodeHexString(const std::string& hexStringInput);
80+
81+
/// Encode a vector of bytes to a Base64 string
82+
/// @param[in] input The vector of bytes to be encoded
83+
/// @return The encoded string
84+
static std::string encode(const std::vector<uint8_t>& input);
85+
86+
/// Decode a Base64 string to a vector of bytes
87+
/// @param[in] input The Base64 string to be decoded
88+
/// @return The decoded vector of bytes
89+
static std::vector<uint8_t> decodeToByteVector(const std::string& input);
90+
91+
/// Decode a Base64 string to a hex string
92+
/// @param[in] input The Base64 string to be decoded
93+
/// @return The decoded hex string
94+
static std::string decodeToHexString(const std::string& input);
95+
96+
/// Decode a Base64 string to a regular string
97+
/// @param[in] input The Base64 string to be decoded
98+
/// @return The decoded string
99+
static std::string decodeToString(const std::string& input);
100+
101+
/// Decode a Base64 string to a byte array
102+
/// @param[in] input The Base64 string to be decoded
103+
/// @param[out] resultByteArr A pre-allocated byte array where the result will be written to
104+
/// @param[in] resultByteArrSize The size of the pre-allocated byte array
105+
/// @return The size of the decoded data
106+
static size_t decodeToByteArray(const std::string& input, uint8_t* resultByteArr, size_t resultByteArrSize);
107+
108+
/// Get the expected decoded size of a Base64 string without actually decoding it
109+
/// @param[in] input The Base64 string to be decoded
110+
/// @return The expected size of the decoded data
111+
static size_t getDecodedSize(const std::string& input);
112+
113+
private:
114+
static constexpr uint32_t badChar = 0x01ffffff;
115+
static constexpr char paddingChar = '=';
116+
};
117+
58118
/// A template class to calculate enum class hash
59119
/// @tparam EnumClass
60120
template <typename EnumClass, std::enable_if_t<std::is_enum<EnumClass>::value, bool> = false> struct EnumClassHash

0 commit comments

Comments
 (0)