1 //===-- StringExtractor.cpp -------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/Utility/StringExtractor.h"
17 // Other libraries and framework includes
20 static inline int xdigit_to_sint(char ch) {
21 if (ch >= 'a' && ch <= 'f')
23 if (ch >= 'A' && ch <= 'F')
25 if (ch >= '0' && ch <= '9')
30 //----------------------------------------------------------------------
31 // StringExtractor constructor
32 //----------------------------------------------------------------------
33 StringExtractor::StringExtractor() : m_packet(), m_index(0) {}
35 StringExtractor::StringExtractor(llvm::StringRef packet_str)
36 : m_packet(), m_index(0) {
37 m_packet.assign(packet_str.begin(), packet_str.end());
40 StringExtractor::StringExtractor(const char *packet_cstr)
41 : m_packet(), m_index(0) {
43 m_packet.assign(packet_cstr);
46 //----------------------------------------------------------------------
47 // StringExtractor copy constructor
48 //----------------------------------------------------------------------
49 StringExtractor::StringExtractor(const StringExtractor &rhs)
50 : m_packet(rhs.m_packet), m_index(rhs.m_index) {}
52 //----------------------------------------------------------------------
53 // StringExtractor assignment operator
54 //----------------------------------------------------------------------
55 const StringExtractor &StringExtractor::operator=(const StringExtractor &rhs) {
57 m_packet = rhs.m_packet;
58 m_index = rhs.m_index;
63 //----------------------------------------------------------------------
65 //----------------------------------------------------------------------
66 StringExtractor::~StringExtractor() {}
68 char StringExtractor::GetChar(char fail_value) {
69 if (m_index < m_packet.size()) {
70 char ch = m_packet[m_index];
78 //----------------------------------------------------------------------
79 // If a pair of valid hex digits exist at the head of the
80 // StringExtractor they are decoded into an unsigned byte and returned
83 // If there is not a pair of valid hex digits at the head of the
84 // StringExtractor, it is left unchanged and -1 is returned
85 //----------------------------------------------------------------------
86 int StringExtractor::DecodeHexU8() {
88 if (GetBytesLeft() < 2) {
91 const int hi_nibble = xdigit_to_sint(m_packet[m_index]);
92 const int lo_nibble = xdigit_to_sint(m_packet[m_index + 1]);
93 if (hi_nibble == -1 || lo_nibble == -1) {
97 return (uint8_t)((hi_nibble << 4) + lo_nibble);
100 //----------------------------------------------------------------------
101 // Extract an unsigned character from two hex ASCII chars in the packet
102 // string, or return fail_value on failure
103 //----------------------------------------------------------------------
104 uint8_t StringExtractor::GetHexU8(uint8_t fail_value, bool set_eof_on_fail) {
105 // On success, fail_value will be overwritten with the next
106 // character in the stream
107 GetHexU8Ex(fail_value, set_eof_on_fail);
111 bool StringExtractor::GetHexU8Ex(uint8_t &ch, bool set_eof_on_fail) {
112 int byte = DecodeHexU8();
114 if (set_eof_on_fail || m_index >= m_packet.size())
115 m_index = UINT64_MAX;
116 // ch should not be changed in case of failure
123 uint32_t StringExtractor::GetU32(uint32_t fail_value, int base) {
124 if (m_index < m_packet.size()) {
126 const char *start = m_packet.c_str();
127 const char *cstr = start + m_index;
128 uint32_t result = static_cast<uint32_t>(::strtoul(cstr, &end, base));
130 if (end && end != cstr) {
131 m_index = end - start;
138 int32_t StringExtractor::GetS32(int32_t fail_value, int base) {
139 if (m_index < m_packet.size()) {
141 const char *start = m_packet.c_str();
142 const char *cstr = start + m_index;
143 int32_t result = static_cast<int32_t>(::strtol(cstr, &end, base));
145 if (end && end != cstr) {
146 m_index = end - start;
153 uint64_t StringExtractor::GetU64(uint64_t fail_value, int base) {
154 if (m_index < m_packet.size()) {
156 const char *start = m_packet.c_str();
157 const char *cstr = start + m_index;
158 uint64_t result = ::strtoull(cstr, &end, base);
160 if (end && end != cstr) {
161 m_index = end - start;
168 int64_t StringExtractor::GetS64(int64_t fail_value, int base) {
169 if (m_index < m_packet.size()) {
171 const char *start = m_packet.c_str();
172 const char *cstr = start + m_index;
173 int64_t result = ::strtoll(cstr, &end, base);
175 if (end && end != cstr) {
176 m_index = end - start;
183 uint32_t StringExtractor::GetHexMaxU32(bool little_endian,
184 uint32_t fail_value) {
186 uint32_t nibble_count = 0;
190 uint32_t shift_amount = 0;
191 while (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) {
192 // Make sure we don't exceed the size of a uint32_t...
193 if (nibble_count >= (sizeof(uint32_t) * 2)) {
194 m_index = UINT64_MAX;
199 uint8_t nibble_hi = xdigit_to_sint(m_packet[m_index]);
201 if (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) {
202 nibble_lo = xdigit_to_sint(m_packet[m_index]);
204 result |= ((uint32_t)nibble_hi << (shift_amount + 4));
205 result |= ((uint32_t)nibble_lo << shift_amount);
209 result |= ((uint32_t)nibble_hi << shift_amount);
215 while (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) {
216 // Make sure we don't exceed the size of a uint32_t...
217 if (nibble_count >= (sizeof(uint32_t) * 2)) {
218 m_index = UINT64_MAX;
222 uint8_t nibble = xdigit_to_sint(m_packet[m_index]);
234 uint64_t StringExtractor::GetHexMaxU64(bool little_endian,
235 uint64_t fail_value) {
237 uint32_t nibble_count = 0;
241 uint32_t shift_amount = 0;
242 while (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) {
243 // Make sure we don't exceed the size of a uint64_t...
244 if (nibble_count >= (sizeof(uint64_t) * 2)) {
245 m_index = UINT64_MAX;
250 uint8_t nibble_hi = xdigit_to_sint(m_packet[m_index]);
252 if (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) {
253 nibble_lo = xdigit_to_sint(m_packet[m_index]);
255 result |= ((uint64_t)nibble_hi << (shift_amount + 4));
256 result |= ((uint64_t)nibble_lo << shift_amount);
260 result |= ((uint64_t)nibble_hi << shift_amount);
266 while (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) {
267 // Make sure we don't exceed the size of a uint64_t...
268 if (nibble_count >= (sizeof(uint64_t) * 2)) {
269 m_index = UINT64_MAX;
273 uint8_t nibble = xdigit_to_sint(m_packet[m_index]);
285 size_t StringExtractor::GetHexBytes(llvm::MutableArrayRef<uint8_t> dest,
286 uint8_t fail_fill_value) {
287 size_t bytes_extracted = 0;
288 while (!dest.empty() && GetBytesLeft() > 0) {
289 dest[0] = GetHexU8(fail_fill_value);
293 dest = dest.drop_front();
297 ::memset(dest.data(), fail_fill_value, dest.size());
299 return bytes_extracted;
302 //----------------------------------------------------------------------
303 // Decodes all valid hex encoded bytes at the head of the
304 // StringExtractor, limited by dst_len.
306 // Returns the number of bytes successfully decoded
307 //----------------------------------------------------------------------
308 size_t StringExtractor::GetHexBytesAvail(llvm::MutableArrayRef<uint8_t> dest) {
309 size_t bytes_extracted = 0;
310 while (!dest.empty()) {
311 int decode = DecodeHexU8();
314 dest[0] = (uint8_t)decode;
315 dest = dest.drop_front();
318 return bytes_extracted;
321 // Consume ASCII hex nibble character pairs until we have decoded byte_size
324 uint64_t StringExtractor::GetHexWithFixedSize(uint32_t byte_size,
326 uint64_t fail_value) {
327 if (byte_size <= 8 && GetBytesLeft() >= byte_size * 2) {
332 uint32_t shift_amount;
333 for (i = 0, shift_amount = 0; i < byte_size && IsGood();
334 ++i, shift_amount += 8) {
335 result |= ((uint64_t)GetHexU8() << shift_amount);
339 for (i = 0; i < byte_size && IsGood(); ++i) {
341 result |= GetHexU8();
345 m_index = UINT64_MAX;
349 size_t StringExtractor::GetHexByteString(std::string &str) {
351 str.reserve(GetBytesLeft() / 2);
353 while ((ch = GetHexU8()) != '\0')
358 size_t StringExtractor::GetHexByteStringFixedLength(std::string &str,
359 uint32_t nibble_length) {
362 uint32_t nibble_count = 0;
363 for (const char *pch = Peek();
364 (nibble_count < nibble_length) && (pch != nullptr);
365 str.append(1, GetHexU8(0, false)), pch = Peek(), nibble_count += 2) {
371 size_t StringExtractor::GetHexByteStringTerminatedBy(std::string &str,
375 while ((ch = GetHexU8(0, false)) != '\0')
377 if (Peek() && *Peek() == terminator)
384 bool StringExtractor::GetNameColonValue(llvm::StringRef &name,
385 llvm::StringRef &value) {
386 // Read something in the form of NNNN:VVVV; where NNNN is any character
387 // that is not a colon, followed by a ':' character, then a value (one or
388 // more ';' chars), followed by a ';'
389 if (m_index >= m_packet.size())
392 llvm::StringRef view(m_packet);
396 llvm::StringRef a, b, c, d;
397 view = view.substr(m_index);
398 std::tie(a, b) = view.split(':');
399 if (a.empty() || b.empty())
401 std::tie(c, d) = b.split(';');
402 if (b == c && d.empty())
408 m_index = m_packet.size();
410 size_t bytes_consumed = d.data() - view.data();
411 m_index += bytes_consumed;
416 void StringExtractor::SkipSpaces() {
417 const size_t n = m_packet.size();
418 while (m_index < n && isspace(m_packet[m_index]))