• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // C Includes
14 // C++ Includes
15 #include <string>
16 #include <stdint.h>
17 
18 // Other libraries and framework includes
19 // Project includes
20 
21 class StringExtractor
22 {
23 public:
24 
25     enum {
26         BigEndian = 0,
27         LittleEndian = 1
28     };
29     //------------------------------------------------------------------
30     // Constructors and Destructors
31     //------------------------------------------------------------------
32     StringExtractor();
33     StringExtractor(const char *packet_cstr);
34     StringExtractor(const StringExtractor& rhs);
35     virtual ~StringExtractor();
36 
37     //------------------------------------------------------------------
38     // Operators
39     //------------------------------------------------------------------
40     const StringExtractor&
41     operator=(const StringExtractor& rhs);
42 
43     // Returns true if the file position is still valid for the data
44     // contained in this string extractor object.
45     bool
IsGood()46     IsGood() const
47     {
48         return m_index != UINT64_MAX;
49     }
50 
51     uint64_t
GetFilePos()52     GetFilePos () const
53     {
54         return m_index;
55     }
56 
57     void
SetFilePos(uint32_t idx)58     SetFilePos (uint32_t idx)
59     {
60         m_index = idx;
61     }
62 
63     void
Clear()64     Clear ()
65     {
66         m_packet.clear();
67         m_index = 0;
68     }
69 
70     std::string &
GetStringRef()71     GetStringRef ()
72     {
73         return m_packet;
74     }
75 
76     const std::string &
GetStringRef()77     GetStringRef () const
78     {
79         return m_packet;
80     }
81 
82     bool
Empty()83     Empty()
84     {
85         return m_packet.empty();
86     }
87 
88     size_t
GetBytesLeft()89     GetBytesLeft ()
90     {
91         if (m_index < m_packet.size())
92             return m_packet.size() - m_index;
93         return 0;
94     }
95     char
96     GetChar (char fail_value = '\0');
97 
98     uint8_t
99     GetHexU8 (uint8_t fail_value = 0, bool set_eof_on_fail = true);
100 
101     bool
102     GetNameColonValue (std::string &name, std::string &value);
103 
104     uint32_t
105     GetU32 (uint32_t fail_value, int base = 0);
106 
107     uint32_t
108     GetHexMaxU32 (bool little_endian, uint32_t fail_value);
109 
110     uint64_t
111     GetHexMaxU64 (bool little_endian, uint64_t fail_value);
112 
113     size_t
114     GetHexBytes (void *dst, size_t dst_len, uint8_t fail_fill_value);
115 
116     uint64_t
117     GetHexWithFixedSize (uint32_t byte_size, bool little_endian, uint64_t fail_value);
118 
119     size_t
120     GetHexByteString (std::string &str);
121 
122     const char *
Peek()123     Peek ()
124     {
125         if (m_index < m_packet.size())
126             return m_packet.c_str() + m_index;
127         return NULL;
128     }
129 
130 protected:
131     //------------------------------------------------------------------
132     // For StringExtractor only
133     //------------------------------------------------------------------
134     std::string m_packet;   // The string in which to extract data.
135     uint64_t m_index;       // When extracting data from a packet, this index
136                             // will march along as things get extracted. If set
137                             // to UINT64_MAX the end of the packet data was
138                             // reached when decoding information
139 };
140 
141 #endif  // utility_StringExtractor_h_
142