1 //===-- StringExtractor.h ---------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef utility_StringExtractor_h_ 11 #define utility_StringExtractor_h_ 12 13 #include "llvm/ADT/ArrayRef.h" 14 #include "llvm/ADT/StringRef.h" 15 16 #include <stddef.h> 17 #include <stdint.h> 18 #include <string> 19 20 class StringExtractor { 21 public: 22 enum { BigEndian = 0, LittleEndian = 1 }; 23 //------------------------------------------------------------------ 24 // Constructors and Destructors 25 //------------------------------------------------------------------ 26 StringExtractor(); 27 StringExtractor(llvm::StringRef packet_str); 28 StringExtractor(const char *packet_cstr); 29 StringExtractor(const StringExtractor &rhs); 30 virtual ~StringExtractor(); 31 32 //------------------------------------------------------------------ 33 // Operators 34 //------------------------------------------------------------------ 35 const StringExtractor &operator=(const StringExtractor &rhs); 36 Reset(llvm::StringRef str)37 void Reset(llvm::StringRef str) { 38 m_packet = str; 39 m_index = 0; 40 } 41 42 // Returns true if the file position is still valid for the data contained in 43 // this string extractor object. IsGood()44 bool IsGood() const { return m_index != UINT64_MAX; } 45 GetFilePos()46 uint64_t GetFilePos() const { return m_index; } 47 SetFilePos(uint32_t idx)48 void SetFilePos(uint32_t idx) { m_index = idx; } 49 Clear()50 void Clear() { 51 m_packet.clear(); 52 m_index = 0; 53 } 54 55 void SkipSpaces(); 56 GetStringRef()57 std::string &GetStringRef() { return m_packet; } 58 GetStringRef()59 const std::string &GetStringRef() const { return m_packet; } 60 Empty()61 bool Empty() { return m_packet.empty(); } 62 GetBytesLeft()63 size_t GetBytesLeft() { 64 if (m_index < m_packet.size()) 65 return m_packet.size() - m_index; 66 return 0; 67 } 68 69 char GetChar(char fail_value = '\0'); 70 71 char PeekChar(char fail_value = '\0') { 72 const char *cstr = Peek(); 73 if (cstr) 74 return cstr[0]; 75 return fail_value; 76 } 77 78 int DecodeHexU8(); 79 80 uint8_t GetHexU8(uint8_t fail_value = 0, bool set_eof_on_fail = true); 81 82 bool GetHexU8Ex(uint8_t &ch, bool set_eof_on_fail = true); 83 84 bool GetNameColonValue(llvm::StringRef &name, llvm::StringRef &value); 85 86 int32_t GetS32(int32_t fail_value, int base = 0); 87 88 uint32_t GetU32(uint32_t fail_value, int base = 0); 89 90 int64_t GetS64(int64_t fail_value, int base = 0); 91 92 uint64_t GetU64(uint64_t fail_value, int base = 0); 93 94 uint32_t GetHexMaxU32(bool little_endian, uint32_t fail_value); 95 96 uint64_t GetHexMaxU64(bool little_endian, uint64_t fail_value); 97 98 size_t GetHexBytes(llvm::MutableArrayRef<uint8_t> dest, 99 uint8_t fail_fill_value); 100 101 size_t GetHexBytesAvail(llvm::MutableArrayRef<uint8_t> dest); 102 103 uint64_t GetHexWithFixedSize(uint32_t byte_size, bool little_endian, 104 uint64_t fail_value); 105 106 size_t GetHexByteString(std::string &str); 107 108 size_t GetHexByteStringFixedLength(std::string &str, uint32_t nibble_length); 109 110 size_t GetHexByteStringTerminatedBy(std::string &str, char terminator); 111 112 bool ConsumeFront(const llvm::StringRef &str); 113 Peek()114 const char *Peek() { 115 if (m_index < m_packet.size()) 116 return m_packet.c_str() + m_index; 117 return nullptr; 118 } 119 120 protected: fail()121 bool fail() { 122 m_index = UINT64_MAX; 123 return false; 124 } 125 //------------------------------------------------------------------ 126 // For StringExtractor only 127 //------------------------------------------------------------------ 128 std::string m_packet; // The string in which to extract data. 129 uint64_t m_index; // When extracting data from a packet, this index 130 // will march along as things get extracted. If set to 131 // UINT64_MAX the end of the packet data was reached 132 // when decoding information 133 }; 134 135 #endif // utility_StringExtractor_h_ 136