PcapPlusPlus  Next
GeneralUtils.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <cstdint>
5 #include <type_traits>
6 #include <vector>
7 
9 
12 namespace pcpp
13 {
23  std::string byteArrayToHexString(const uint8_t* byteArr, size_t byteArrSize, int stringSizeLimit = -1);
24 
36  size_t hexStringToByteArray(const std::string& hexString, uint8_t* resultByteArr, size_t resultByteArrSize);
37 
45  char* cross_platform_memmem(const char* haystack, size_t haystackLen, const char* needle, size_t needleLen);
46 
50  template <int alignment> static int align(int number)
51  {
52  // Only works for alignment with power of 2
53  constexpr bool isPowerOfTwo = alignment && ((alignment & (alignment - 1)) == 0);
54  static_assert(isPowerOfTwo, "Alignment must be a power of 2");
55  int mask = alignment - 1;
56  return (number + mask) & ~mask;
57  }
58 
62  class Base64
63  {
64  public:
69  static std::string encode(const uint8_t* input, size_t inputLen);
70 
74  static std::string encode(const std::string& input);
75 
79  static std::string encodeHexString(const std::string& hexStringInput);
80 
84  static std::string encode(const std::vector<uint8_t>& input);
85 
89  static std::vector<uint8_t> decodeToByteVector(const std::string& input);
90 
94  static std::string decodeToHexString(const std::string& input);
95 
99  static std::string decodeToString(const std::string& input);
100 
106  static size_t decodeToByteArray(const std::string& input, uint8_t* resultByteArr, size_t resultByteArrSize);
107 
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 
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  {
124  return static_cast<std::underlying_type_t<EnumClass>>(value);
125  }
126  };
127 } // namespace pcpp
Definition: GeneralUtils.h:63
static std::string decodeToString(const std::string &input)
static std::string encode(const uint8_t *input, size_t inputLen)
static std::string encode(const std::vector< uint8_t > &input)
static size_t decodeToByteArray(const std::string &input, uint8_t *resultByteArr, size_t resultByteArrSize)
static std::vector< uint8_t > decodeToByteVector(const std::string &input)
static size_t getDecodedSize(const std::string &input)
static std::string decodeToHexString(const std::string &input)
static std::string encode(const std::string &input)
static std::string encodeHexString(const std::string &hexStringInput)
The main namespace for the PcapPlusPlus lib.
char * cross_platform_memmem(const char *haystack, size_t haystackLen, const char *needle, size_t needleLen)
std::string byteArrayToHexString(const uint8_t *byteArr, size_t byteArrSize, int stringSizeLimit=-1)
size_t hexStringToByteArray(const std::string &hexString, uint8_t *resultByteArr, size_t resultByteArrSize)
Definition: GeneralUtils.h:121