|
3 | 3 | #include <string>
|
4 | 4 | #include <cstdint>
|
5 | 5 | #include <type_traits>
|
| 6 | +#include <vector> |
6 | 7 |
|
7 | 8 | /// @file
|
8 | 9 |
|
@@ -55,6 +56,65 @@ namespace pcpp
|
55 | 56 | return (number + mask) & ~mask;
|
56 | 57 | }
|
57 | 58 |
|
| 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 | + |
58 | 118 | /// A template class to calculate enum class hash
|
59 | 119 | /// @tparam EnumClass
|
60 | 120 | template <typename EnumClass, std::enable_if_t<std::is_enum<EnumClass>::value, bool> = false> struct EnumClassHash
|
|
0 commit comments