1 //===-- StringExtractor.cpp -------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #include "lldb/Utility/StringExtractor.h"
17 static inline int xdigit_to_sint(char ch) {
18 if (ch >= 'a' && ch <= 'f')
20 if (ch >= 'A' && ch <= 'F')
22 if (ch >= '0' && ch <= '9')
27 // StringExtractor constructor
28 StringExtractor::StringExtractor() : m_packet(), m_index(0) {}
30 StringExtractor::StringExtractor(llvm::StringRef packet_str)
31 : m_packet(), m_index(0) {
32 m_packet.assign(packet_str.begin(), packet_str.end());
35 StringExtractor::StringExtractor(const char *packet_cstr)
36 : m_packet(), m_index(0) {
38 m_packet.assign(packet_cstr);
42 StringExtractor::~StringExtractor() {}
44 char StringExtractor::GetChar(char fail_value) {
45 if (m_index < m_packet.size()) {
46 char ch = m_packet[m_index];
54 // If a pair of valid hex digits exist at the head of the StringExtractor they
55 // are decoded into an unsigned byte and returned by this function
57 // If there is not a pair of valid hex digits at the head of the
58 // StringExtractor, it is left unchanged and -1 is returned
59 int StringExtractor::DecodeHexU8() {
61 if (GetBytesLeft() < 2) {
64 const int hi_nibble = xdigit_to_sint(m_packet[m_index]);
65 const int lo_nibble = xdigit_to_sint(m_packet[m_index + 1]);
66 if (hi_nibble == -1 || lo_nibble == -1) {
70 return static_cast<uint8_t>((hi_nibble << 4) + lo_nibble);
73 // Extract an unsigned character from two hex ASCII chars in the packet string,
74 // or return fail_value on failure
75 uint8_t StringExtractor::GetHexU8(uint8_t fail_value, bool set_eof_on_fail) {
76 // On success, fail_value will be overwritten with the next character in the
78 GetHexU8Ex(fail_value, set_eof_on_fail);
82 bool StringExtractor::GetHexU8Ex(uint8_t &ch, bool set_eof_on_fail) {
83 int byte = DecodeHexU8();
85 if (set_eof_on_fail || m_index >= m_packet.size())
87 // ch should not be changed in case of failure
90 ch = static_cast<uint8_t>(byte);
94 uint32_t StringExtractor::GetU32(uint32_t fail_value, int base) {
95 if (m_index < m_packet.size()) {
97 const char *start = m_packet.c_str();
98 const char *cstr = start + m_index;
99 uint32_t result = static_cast<uint32_t>(::strtoul(cstr, &end, base));
101 if (end && end != cstr) {
102 m_index = end - start;
109 int32_t StringExtractor::GetS32(int32_t fail_value, int base) {
110 if (m_index < m_packet.size()) {
112 const char *start = m_packet.c_str();
113 const char *cstr = start + m_index;
114 int32_t result = static_cast<int32_t>(::strtol(cstr, &end, base));
116 if (end && end != cstr) {
117 m_index = end - start;
124 uint64_t StringExtractor::GetU64(uint64_t fail_value, int base) {
125 if (m_index < m_packet.size()) {
127 const char *start = m_packet.c_str();
128 const char *cstr = start + m_index;
129 uint64_t result = ::strtoull(cstr, &end, base);
131 if (end && end != cstr) {
132 m_index = end - start;
139 int64_t StringExtractor::GetS64(int64_t fail_value, int base) {
140 if (m_index < m_packet.size()) {
142 const char *start = m_packet.c_str();
143 const char *cstr = start + m_index;
144 int64_t result = ::strtoll(cstr, &end, base);
146 if (end && end != cstr) {
147 m_index = end - start;
154 uint32_t StringExtractor::GetHexMaxU32(bool little_endian,
155 uint32_t fail_value) {
157 uint32_t nibble_count = 0;
161 uint32_t shift_amount = 0;
162 while (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) {
163 // Make sure we don't exceed the size of a uint32_t...
164 if (nibble_count >= (sizeof(uint32_t) * 2)) {
165 m_index = UINT64_MAX;
170 uint8_t nibble_hi = xdigit_to_sint(m_packet[m_index]);
172 if (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) {
173 nibble_lo = xdigit_to_sint(m_packet[m_index]);
175 result |= (static_cast<uint32_t>(nibble_hi) << (shift_amount + 4));
176 result |= (static_cast<uint32_t>(nibble_lo) << shift_amount);
180 result |= (static_cast<uint32_t>(nibble_hi) << shift_amount);
186 while (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) {
187 // Make sure we don't exceed the size of a uint32_t...
188 if (nibble_count >= (sizeof(uint32_t) * 2)) {
189 m_index = UINT64_MAX;
193 uint8_t nibble = xdigit_to_sint(m_packet[m_index]);
205 uint64_t StringExtractor::GetHexMaxU64(bool little_endian,
206 uint64_t fail_value) {
208 uint32_t nibble_count = 0;
212 uint32_t shift_amount = 0;
213 while (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) {
214 // Make sure we don't exceed the size of a uint64_t...
215 if (nibble_count >= (sizeof(uint64_t) * 2)) {
216 m_index = UINT64_MAX;
221 uint8_t nibble_hi = xdigit_to_sint(m_packet[m_index]);
223 if (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) {
224 nibble_lo = xdigit_to_sint(m_packet[m_index]);
226 result |= (static_cast<uint64_t>(nibble_hi) << (shift_amount + 4));
227 result |= (static_cast<uint64_t>(nibble_lo) << shift_amount);
231 result |= (static_cast<uint64_t>(nibble_hi) << shift_amount);
237 while (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) {
238 // Make sure we don't exceed the size of a uint64_t...
239 if (nibble_count >= (sizeof(uint64_t) * 2)) {
240 m_index = UINT64_MAX;
244 uint8_t nibble = xdigit_to_sint(m_packet[m_index]);
256 bool StringExtractor::ConsumeFront(const llvm::StringRef &str) {
257 llvm::StringRef S = GetStringRef();
258 if (!S.startswith(str))
261 m_index += str.size();
265 size_t StringExtractor::GetHexBytes(llvm::MutableArrayRef<uint8_t> dest,
266 uint8_t fail_fill_value) {
267 size_t bytes_extracted = 0;
268 while (!dest.empty() && GetBytesLeft() > 0) {
269 dest[0] = GetHexU8(fail_fill_value);
273 dest = dest.drop_front();
277 ::memset(dest.data(), fail_fill_value, dest.size());
279 return bytes_extracted;
282 // Decodes all valid hex encoded bytes at the head of the StringExtractor,
283 // limited by dst_len.
285 // Returns the number of bytes successfully decoded
286 size_t StringExtractor::GetHexBytesAvail(llvm::MutableArrayRef<uint8_t> dest) {
287 size_t bytes_extracted = 0;
288 while (!dest.empty()) {
289 int decode = DecodeHexU8();
292 dest[0] = static_cast<uint8_t>(decode);
293 dest = dest.drop_front();
296 return bytes_extracted;
299 // Consume ASCII hex nibble character pairs until we have decoded byte_size
302 uint64_t StringExtractor::GetHexWithFixedSize(uint32_t byte_size,
304 uint64_t fail_value) {
305 if (byte_size <= 8 && GetBytesLeft() >= byte_size * 2) {
310 uint32_t shift_amount;
311 for (i = 0, shift_amount = 0; i < byte_size && IsGood();
312 ++i, shift_amount += 8) {
313 result |= (static_cast<uint64_t>(GetHexU8()) << shift_amount);
317 for (i = 0; i < byte_size && IsGood(); ++i) {
319 result |= GetHexU8();
323 m_index = UINT64_MAX;
327 size_t StringExtractor::GetHexByteString(std::string &str) {
329 str.reserve(GetBytesLeft() / 2);
331 while ((ch = GetHexU8()) != '\0')
336 size_t StringExtractor::GetHexByteStringFixedLength(std::string &str,
337 uint32_t nibble_length) {
340 uint32_t nibble_count = 0;
341 for (const char *pch = Peek();
342 (nibble_count < nibble_length) && (pch != nullptr);
343 str.append(1, GetHexU8(0, false)), pch = Peek(), nibble_count += 2) {
349 size_t StringExtractor::GetHexByteStringTerminatedBy(std::string &str,
353 while ((ch = GetHexU8(0, false)) != '\0')
355 if (Peek() && *Peek() == terminator)
362 bool StringExtractor::GetNameColonValue(llvm::StringRef &name,
363 llvm::StringRef &value) {
364 // Read something in the form of NNNN:VVVV; where NNNN is any character that
365 // is not a colon, followed by a ':' character, then a value (one or more ';'
366 // chars), followed by a ';'
367 if (m_index >= m_packet.size())
370 llvm::StringRef view(m_packet);
374 llvm::StringRef a, b, c, d;
375 view = view.substr(m_index);
376 std::tie(a, b) = view.split(':');
377 if (a.empty() || b.empty())
379 std::tie(c, d) = b.split(';');
380 if (b == c && d.empty())
386 m_index = m_packet.size();
388 size_t bytes_consumed = d.data() - view.data();
389 m_index += bytes_consumed;
394 void StringExtractor::SkipSpaces() {
395 const size_t n = m_packet.size();
396 while (m_index < n && isspace(m_packet[m_index]))