1 //===-- StdStringExtractor.h ------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #ifndef utility_StdStringExtractor_h_ 10 #define utility_StdStringExtractor_h_ 11 12 #include <stdint.h> 13 #include <string> 14 15 16 // Based on StringExtractor, with the added limitation that this file should not 17 // take a dependency on LLVM, as it is used from debugserver. 18 class StdStringExtractor { 19 public: 20 enum { BigEndian = 0, LittleEndian = 1 }; 21 // Constructors and Destructors 22 StdStringExtractor(); 23 StdStringExtractor(const char *packet_cstr); 24 StdStringExtractor(const StdStringExtractor &rhs); 25 virtual ~StdStringExtractor(); 26 27 // Operators 28 const StdStringExtractor &operator=(const StdStringExtractor &rhs); 29 30 // Returns true if the file position is still valid for the data 31 // contained in this string extractor object. 32 bool IsGood() const { return m_index != UINT64_MAX; } 33 34 uint64_t GetFilePos() const { return m_index; } 35 36 void SetFilePos(uint32_t idx) { m_index = idx; } 37 38 void Clear() { 39 m_packet.clear(); 40 m_index = 0; 41 } 42 43 void SkipSpaces(); 44 45 std::string &GetStringRef() { return m_packet; } 46 47 const std::string &GetStringRef() const { return m_packet; } 48 49 bool Empty() { return m_packet.empty(); } 50 51 size_t GetBytesLeft() { 52 if (m_index < m_packet.size()) 53 return m_packet.size() - m_index; 54 return 0; 55 } 56 57 char GetChar(char fail_value = '\0'); 58 59 char PeekChar(char fail_value = '\0') { 60 const char *cstr = Peek(); 61 if (cstr) 62 return cstr[0]; 63 return fail_value; 64 } 65 66 int DecodeHexU8(); 67 68 uint8_t GetHexU8(uint8_t fail_value = 0, bool set_eof_on_fail = true); 69 70 bool GetHexU8Ex(uint8_t &ch, bool set_eof_on_fail = true); 71 72 bool GetNameColonValue(std::string &name, std::string &value); 73 74 int32_t GetS32(int32_t fail_value, int base = 0); 75 76 uint32_t GetU32(uint32_t fail_value, int base = 0); 77 78 int64_t GetS64(int64_t fail_value, int base = 0); 79 80 uint64_t GetU64(uint64_t fail_value, int base = 0); 81 82 uint32_t GetHexMaxU32(bool little_endian, uint32_t fail_value); 83 84 uint64_t GetHexMaxU64(bool little_endian, uint64_t fail_value); 85 86 size_t GetHexBytes(void *dst, size_t dst_len, uint8_t fail_fill_value); 87 88 size_t GetHexBytesAvail(void *dst, size_t dst_len); 89 90 uint64_t GetHexWithFixedSize(uint32_t byte_size, bool little_endian, 91 uint64_t fail_value); 92 93 size_t GetHexByteString(std::string &str); 94 95 size_t GetHexByteStringFixedLength(std::string &str, uint32_t nibble_length); 96 97 size_t GetHexByteStringTerminatedBy(std::string &str, char terminator); 98 99 const char *Peek() { 100 if (m_index < m_packet.size()) 101 return m_packet.c_str() + m_index; 102 return nullptr; 103 } 104 105 protected: 106 // For StdStringExtractor only 107 std::string m_packet; // The string in which to extract data. 108 uint64_t m_index; // When extracting data from a packet, this index 109 // will march along as things get extracted. If set 110 // to UINT64_MAX the end of the packet data was 111 // reached when decoding information 112 }; 113 114 #endif // utility_StringExtractor_h_ 115