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   //------------------------------------------------------------------
22   // Constructors and Destructors
23   //------------------------------------------------------------------
24   StdStringExtractor();
25   StdStringExtractor(const char *packet_cstr);
26   StdStringExtractor(const StdStringExtractor &rhs);
27   virtual ~StdStringExtractor();
28 
29   //------------------------------------------------------------------
30   // Operators
31   //------------------------------------------------------------------
32   const StdStringExtractor &operator=(const StdStringExtractor &rhs);
33 
34   // Returns true if the file position is still valid for the data
35   // contained in this string extractor object.
36   bool IsGood() const { return m_index != UINT64_MAX; }
37 
38   uint64_t GetFilePos() const { return m_index; }
39 
40   void SetFilePos(uint32_t idx) { m_index = idx; }
41 
42   void Clear() {
43     m_packet.clear();
44     m_index = 0;
45   }
46 
47   void SkipSpaces();
48 
49   std::string &GetStringRef() { return m_packet; }
50 
51   const std::string &GetStringRef() const { return m_packet; }
52 
53   bool Empty() { return m_packet.empty(); }
54 
55   size_t GetBytesLeft() {
56     if (m_index < m_packet.size())
57       return m_packet.size() - m_index;
58     return 0;
59   }
60 
61   char GetChar(char fail_value = '\0');
62 
63   char PeekChar(char fail_value = '\0') {
64     const char *cstr = Peek();
65     if (cstr)
66       return cstr[0];
67     return fail_value;
68   }
69 
70   int DecodeHexU8();
71 
72   uint8_t GetHexU8(uint8_t fail_value = 0, bool set_eof_on_fail = true);
73 
74   bool GetHexU8Ex(uint8_t &ch, bool set_eof_on_fail = true);
75 
76   bool GetNameColonValue(std::string &name, std::string &value);
77 
78   int32_t GetS32(int32_t fail_value, int base = 0);
79 
80   uint32_t GetU32(uint32_t fail_value, int base = 0);
81 
82   int64_t GetS64(int64_t fail_value, int base = 0);
83 
84   uint64_t GetU64(uint64_t fail_value, int base = 0);
85 
86   uint32_t GetHexMaxU32(bool little_endian, uint32_t fail_value);
87 
88   uint64_t GetHexMaxU64(bool little_endian, uint64_t fail_value);
89 
90   size_t GetHexBytes(void *dst, size_t dst_len, uint8_t fail_fill_value);
91 
92   size_t GetHexBytesAvail(void *dst, size_t dst_len);
93 
94   uint64_t GetHexWithFixedSize(uint32_t byte_size, bool little_endian,
95                                uint64_t fail_value);
96 
97   size_t GetHexByteString(std::string &str);
98 
99   size_t GetHexByteStringFixedLength(std::string &str, uint32_t nibble_length);
100 
101   size_t GetHexByteStringTerminatedBy(std::string &str, char terminator);
102 
103   const char *Peek() {
104     if (m_index < m_packet.size())
105       return m_packet.c_str() + m_index;
106     return nullptr;
107   }
108 
109 protected:
110   //------------------------------------------------------------------
111   // For StdStringExtractor only
112   //------------------------------------------------------------------
113   std::string m_packet; // The string in which to extract data.
114   uint64_t m_index;     // When extracting data from a packet, this index
115                         // will march along as things get extracted. If set
116                         // to UINT64_MAX the end of the packet data was
117                         // reached when decoding information
118 };
119 
120 #endif // utility_StringExtractor_h_
121