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