/src/PcapPlusPlus/Common++/src/GeneralUtils.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | #define LOG_MODULE CommonLogModuleGenericUtils |
2 | | |
3 | | #include "GeneralUtils.h" |
4 | | #include "Logger.h" |
5 | | #include <sstream> |
6 | | #include <iomanip> |
7 | | #include <string.h> |
8 | | #include <stdlib.h> |
9 | | |
10 | | namespace pcpp |
11 | | { |
12 | | |
13 | | std::string byteArrayToHexString(const uint8_t* byteArr, size_t byteArrSize, int stringSizeLimit) |
14 | 0 | { |
15 | 0 | if (stringSizeLimit <= 0) |
16 | 0 | stringSizeLimit = byteArrSize; |
17 | |
|
18 | 0 | std::stringstream dataStream; |
19 | 0 | dataStream << std::hex; |
20 | 0 | for (size_t i = 0; i < byteArrSize; ++i) |
21 | 0 | { |
22 | 0 | if (i >= (size_t)stringSizeLimit) |
23 | 0 | break; |
24 | | |
25 | 0 | dataStream << std::setw(2) << std::setfill('0') << (int)byteArr[i]; |
26 | 0 | } |
27 | |
|
28 | 0 | return dataStream.str(); |
29 | 0 | } |
30 | | |
31 | | static int char2int(char input) |
32 | 0 | { |
33 | 0 | if(input >= '0' && input <= '9') |
34 | 0 | return input - '0'; |
35 | 0 | if(input >= 'A' && input <= 'F') |
36 | 0 | return input - 'A' + 10; |
37 | 0 | if(input >= 'a' && input <= 'f') |
38 | 0 | return input - 'a' + 10; |
39 | 0 | return -1; |
40 | 0 | } |
41 | | |
42 | | size_t hexStringToByteArray(const std::string& hexString, uint8_t* resultByteArr, size_t resultByteArrSize) |
43 | 0 | { |
44 | 0 | if (hexString.size() % 2 != 0) |
45 | 0 | { |
46 | 0 | PCPP_LOG_ERROR("Input string is in odd size"); |
47 | 0 | return 0; |
48 | 0 | } |
49 | | |
50 | 0 | memset(resultByteArr, 0, resultByteArrSize); |
51 | 0 | for (size_t i = 0; i < hexString.length(); i += 2) |
52 | 0 | { |
53 | 0 | if (i >= resultByteArrSize * 2) |
54 | 0 | return resultByteArrSize; |
55 | | |
56 | 0 | int firstChar = char2int(hexString[i]); |
57 | 0 | int secondChar = char2int(hexString[i + 1]); |
58 | 0 | if (firstChar < 0 || secondChar < 0) |
59 | 0 | { |
60 | 0 | PCPP_LOG_ERROR("Input string has an illegal character"); |
61 | 0 | resultByteArr[0] = '\0'; |
62 | 0 | return 0; |
63 | 0 | } |
64 | | |
65 | 0 | resultByteArr[i / 2] = (firstChar << 4) | secondChar; |
66 | 0 | } |
67 | | |
68 | 0 | return hexString.length() / 2; |
69 | 0 | } |
70 | | |
71 | | |
72 | | char* cross_platform_memmem(const char* haystack, size_t haystackLen, const char* needle, size_t needleLen) |
73 | 17.7k | { |
74 | 17.7k | char* ptr = (char*)haystack; |
75 | 43.7k | while (needleLen <= (haystackLen - (ptr - haystack))) |
76 | 39.8k | { |
77 | 39.8k | if (NULL != (ptr = (char*)memchr(ptr, (int)(*needle), haystackLen - (ptr - haystack)))) |
78 | 34.3k | { |
79 | | // check if there is room to do a memcmp |
80 | 34.3k | if(needleLen > (haystackLen - (ptr - haystack))) |
81 | 762 | { |
82 | 762 | return NULL; |
83 | 762 | } |
84 | | |
85 | 33.6k | if (0 == memcmp(ptr, needle, needleLen)) |
86 | 7.67k | return ptr; |
87 | 25.9k | else |
88 | 25.9k | ++ptr; |
89 | 33.6k | } |
90 | 5.49k | else |
91 | 5.49k | break; |
92 | 39.8k | } |
93 | | |
94 | 9.35k | return NULL; |
95 | 17.7k | } |
96 | | |
97 | | } |