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