/src/PcapPlusPlus/Common++/header/GeneralUtils.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include <string> |
4 | | #include <cstdint> |
5 | | #include <type_traits> |
6 | | #include <vector> |
7 | | |
8 | | /// @file |
9 | | |
10 | | /// @namespace pcpp |
11 | | /// @brief The main namespace for the PcapPlusPlus lib |
12 | | namespace pcpp |
13 | | { |
14 | | /// Convert a byte array into a string of hex characters. For example: for the array { 0xaa, 0x2b, 0x10 } the string |
15 | | /// "aa2b10" will be returned |
16 | | /// @param[in] byteArr A byte array |
17 | | /// @param[in] byteArrSize The size of the byte array [in bytes] |
18 | | /// @param[in] stringSizeLimit An optional parameter that enables to limit the returned string size. If set to a |
19 | | /// positive integer value the returned string size will be equal or less than this value. If the string |
20 | | /// representation of the whole array is longer than this size then only part of the array will be read. The default |
21 | | /// value is -1 which means no string size limitation |
22 | | /// @return A string of hex characters representing the byte array |
23 | | std::string byteArrayToHexString(const uint8_t* byteArr, size_t byteArrSize, int stringSizeLimit = -1); |
24 | | |
25 | | /// Convert a string of hex characters into a byte array. For example: for the string "aa2b10" an array of values |
26 | | /// { 0xaa, 0x2b, 0x10 } will be returned |
27 | | /// @param[in] hexString A string of hex characters |
28 | | /// @param[out] resultByteArr A pre-allocated byte array where the result will be written to |
29 | | /// @param[in] resultByteArrSize The size of the pre-allocated byte array |
30 | | /// @return The size of the result array. If the string represents an array that is longer than the pre-allocated |
31 | | /// size (resultByteArrSize) then the result array will contain only the part of the string that managed to fit into |
32 | | /// the array, and the returned size will be resultByteArrSize. However if the string represents an array that is |
33 | | /// shorter than the pre-allocated size then some of the cells will remain empty and contain zeros, and the returned |
34 | | /// size will be the part of the array that contain data. If the input is an illegal hex string 0 will be returned. |
35 | | /// Illegal hex string means odd number of characters or a string that contains non-hex characters |
36 | | size_t hexStringToByteArray(const std::string& hexString, uint8_t* resultByteArr, size_t resultByteArrSize); |
37 | | |
38 | | /// This is a cross platform version of memmem (https://man7.org/linux/man-pages/man3/memmem.3.html) which is not |
39 | | /// supported on all platforms. |
40 | | /// @param[in] haystack A pointer to the buffer to be searched |
41 | | /// @param[in] haystackLen Length of the haystack buffer |
42 | | /// @param[in] needle A pointer to a buffer that will be searched for |
43 | | /// @param[in] needleLen Length of the needle buffer |
44 | | /// @return A pointer to the beginning of the substring, or nullptr if the substring is not found |
45 | | char* cross_platform_memmem(const char* haystack, size_t haystackLen, const char* needle, size_t needleLen); |
46 | | |
47 | | /// Calculates alignment. |
48 | | /// @param[in] number Given number |
49 | | /// @return The aligned number |
50 | | template <int alignment> static int align(int number) |
51 | 15.2k | { |
52 | | // Only works for alignment with power of 2 |
53 | 15.2k | constexpr bool isPowerOfTwo = alignment && ((alignment & (alignment - 1)) == 0); |
54 | 15.2k | static_assert(isPowerOfTwo, "Alignment must be a power of 2"); |
55 | 15.2k | int mask = alignment - 1; |
56 | 15.2k | return (number + mask) & ~mask; |
57 | 15.2k | } Unexecuted instantiation: Packet.cpp:int pcpp::align<4>(int) NflogLayer.cpp:int pcpp::align<4>(int) Line | Count | Source | 51 | 15.2k | { | 52 | | // Only works for alignment with power of 2 | 53 | 15.2k | constexpr bool isPowerOfTwo = alignment && ((alignment & (alignment - 1)) == 0); | 54 | 15.2k | static_assert(isPowerOfTwo, "Alignment must be a power of 2"); | 55 | 15.2k | int mask = alignment - 1; | 56 | 15.2k | return (number + mask) & ~mask; | 57 | 15.2k | } |
|
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 | | |
118 | | /// A template class to calculate enum class hash |
119 | | /// @tparam EnumClass |
120 | | template <typename EnumClass, std::enable_if_t<std::is_enum<EnumClass>::value, bool> = false> struct EnumClassHash |
121 | | { |
122 | | size_t operator()(EnumClass value) const |
123 | 19.4k | { |
124 | 19.4k | return static_cast<std::underlying_type_t<EnumClass>>(value); |
125 | 19.4k | } pcpp::EnumClassHash<pcpp::Asn1TagClass, false>::operator()(pcpp::Asn1TagClass) const Line | Count | Source | 123 | 8 | { | 124 | 8 | return static_cast<std::underlying_type_t<EnumClass>>(value); | 125 | 8 | } |
pcpp::EnumClassHash<pcpp::Asn1UniversalTagType, false>::operator()(pcpp::Asn1UniversalTagType) const Line | Count | Source | 123 | 76 | { | 124 | 76 | return static_cast<std::underlying_type_t<EnumClass>>(value); | 125 | 76 | } |
pcpp::EnumClassHash<pcpp::LdapOperationType::Value, false>::operator()(pcpp::LdapOperationType::Value) const Line | Count | Source | 123 | 14.1k | { | 124 | 14.1k | return static_cast<std::underlying_type_t<EnumClass>>(value); | 125 | 14.1k | } |
pcpp::EnumClassHash<pcpp::LdapResultCode::Value, false>::operator()(pcpp::LdapResultCode::Value) const Line | Count | Source | 123 | 4.18k | { | 124 | 4.18k | return static_cast<std::underlying_type_t<EnumClass>>(value); | 125 | 4.18k | } |
pcpp::EnumClassHash<pcpp::LdapSearchRequestLayer::SearchRequestScope::Value, false>::operator()(pcpp::LdapSearchRequestLayer::SearchRequestScope::Value) const Line | Count | Source | 123 | 1.01k | { | 124 | 1.01k | return static_cast<std::underlying_type_t<EnumClass>>(value); | 125 | 1.01k | } |
pcpp::EnumClassHash<pcpp::LdapSearchRequestLayer::DerefAliases::Value, false>::operator()(pcpp::LdapSearchRequestLayer::DerefAliases::Value) const Line | Count | Source | 123 | 10 | { | 124 | 10 | return static_cast<std::underlying_type_t<EnumClass>>(value); | 125 | 10 | } |
|
126 | | }; |
127 | | } // namespace pcpp |