blob: 502f468da3ccc52bcc3ee8e764dab373dd18f0c6 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:241//===-- StringExtractor.cpp -------------------------------------*- C++ -*-===//
2//
Chandler Carruth2946cd72019-01-19 08:50:563// 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
Chris Lattner30fdc8d2010-06-08 16:52:246//
7//===----------------------------------------------------------------------===//
8
Pavel Labathf805e192015-07-07 10:08:419#include "lldb/Utility/StringExtractor.h"
Chris Lattner30fdc8d2010-06-08 16:52:2410
Zachary Turner54695a32016-08-29 19:58:1411#include <tuple>
Zachary Turner4479ac12017-04-06 18:12:2412
Jonas Devlieghere672d2c12018-11-11 23:16:4313#include <ctype.h>
Zachary Turner4479ac12017-04-06 18:12:2414#include <stdlib.h>
Jonas Devlieghere672d2c12018-11-11 23:16:4315#include <string.h>
Chris Lattner30fdc8d2010-06-08 16:52:2416
Kate Stoneb9c1b512016-09-06 20:57:5017static inline int xdigit_to_sint(char ch) {
18 if (ch >= 'a' && ch <= 'f')
19 return 10 + ch - 'a';
20 if (ch >= 'A' && ch <= 'F')
21 return 10 + ch - 'A';
22 if (ch >= '0' && ch <= '9')
23 return ch - '0';
24 return -1;
Chris Lattner30fdc8d2010-06-08 16:52:2425}
26
Chris Lattner30fdc8d2010-06-08 16:52:2427// StringExtractor constructor
Kate Stoneb9c1b512016-09-06 20:57:5028StringExtractor::StringExtractor() : m_packet(), m_index(0) {}
29
30StringExtractor::StringExtractor(llvm::StringRef packet_str)
31 : m_packet(), m_index(0) {
32 m_packet.assign(packet_str.begin(), packet_str.end());
Chris Lattner30fdc8d2010-06-08 16:52:2433}
34
Kate Stoneb9c1b512016-09-06 20:57:5035StringExtractor::StringExtractor(const char *packet_cstr)
36 : m_packet(), m_index(0) {
37 if (packet_cstr)
38 m_packet.assign(packet_cstr);
Zachary Turner54695a32016-08-29 19:58:1439}
Chris Lattner30fdc8d2010-06-08 16:52:2440
Chris Lattner30fdc8d2010-06-08 16:52:2441// Destructor
Kate Stoneb9c1b512016-09-06 20:57:5042StringExtractor::~StringExtractor() {}
Chris Lattner30fdc8d2010-06-08 16:52:2443
Kate Stoneb9c1b512016-09-06 20:57:5044char StringExtractor::GetChar(char fail_value) {
45 if (m_index < m_packet.size()) {
46 char ch = m_packet[m_index];
47 ++m_index;
48 return ch;
49 }
50 m_index = UINT64_MAX;
51 return fail_value;
Chris Lattner30fdc8d2010-06-08 16:52:2452}
53
Adrian Prantl05097242018-04-30 16:49:0454// 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
Vince Harron6eddf8d2014-12-01 22:19:3356//
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
Kate Stoneb9c1b512016-09-06 20:57:5059int StringExtractor::DecodeHexU8() {
60 SkipSpaces();
61 if (GetBytesLeft() < 2) {
62 return -1;
63 }
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) {
67 return -1;
68 }
69 m_index += 2;
Jonas Devlieghere24374ae2019-05-23 05:12:1170 return static_cast<uint8_t>((hi_nibble << 4) + lo_nibble);
Vince Harron6eddf8d2014-12-01 22:19:3371}
72
Adrian Prantl05097242018-04-30 16:49:0473// Extract an unsigned character from two hex ASCII chars in the packet string,
74// or return fail_value on failure
Kate Stoneb9c1b512016-09-06 20:57:5075uint8_t StringExtractor::GetHexU8(uint8_t fail_value, bool set_eof_on_fail) {
Adrian Prantl05097242018-04-30 16:49:0476 // On success, fail_value will be overwritten with the next character in the
77 // stream
Kate Stoneb9c1b512016-09-06 20:57:5078 GetHexU8Ex(fail_value, set_eof_on_fail);
79 return fail_value;
Dawn Perchik554a8572015-09-17 17:55:3280}
81
Kate Stoneb9c1b512016-09-06 20:57:5082bool StringExtractor::GetHexU8Ex(uint8_t &ch, bool set_eof_on_fail) {
83 int byte = DecodeHexU8();
84 if (byte == -1) {
85 if (set_eof_on_fail || m_index >= m_packet.size())
86 m_index = UINT64_MAX;
87 // ch should not be changed in case of failure
88 return false;
89 }
Jonas Devlieghere24374ae2019-05-23 05:12:1190 ch = static_cast<uint8_t>(byte);
Kate Stoneb9c1b512016-09-06 20:57:5091 return true;
Chris Lattner30fdc8d2010-06-08 16:52:2492}
93
Kate Stoneb9c1b512016-09-06 20:57:5094uint32_t StringExtractor::GetU32(uint32_t fail_value, int base) {
95 if (m_index < m_packet.size()) {
96 char *end = nullptr;
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));
Greg Clayton32e0a752011-03-30 18:16:51100
Kate Stoneb9c1b512016-09-06 20:57:50101 if (end && end != cstr) {
102 m_index = end - start;
103 return result;
Daniel Maleae0f8f572013-08-26 23:57:52104 }
Kate Stoneb9c1b512016-09-06 20:57:50105 }
106 return fail_value;
Daniel Maleae0f8f572013-08-26 23:57:52107}
108
Kate Stoneb9c1b512016-09-06 20:57:50109int32_t StringExtractor::GetS32(int32_t fail_value, int base) {
110 if (m_index < m_packet.size()) {
111 char *end = nullptr;
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));
115
116 if (end && end != cstr) {
117 m_index = end - start;
118 return result;
Daniel Maleae0f8f572013-08-26 23:57:52119 }
Kate Stoneb9c1b512016-09-06 20:57:50120 }
121 return fail_value;
Daniel Maleae0f8f572013-08-26 23:57:52122}
123
Kate Stoneb9c1b512016-09-06 20:57:50124uint64_t StringExtractor::GetU64(uint64_t fail_value, int base) {
125 if (m_index < m_packet.size()) {
126 char *end = nullptr;
127 const char *start = m_packet.c_str();
128 const char *cstr = start + m_index;
129 uint64_t result = ::strtoull(cstr, &end, base);
Daniel Maleae0f8f572013-08-26 23:57:52130
Kate Stoneb9c1b512016-09-06 20:57:50131 if (end && end != cstr) {
132 m_index = end - start;
133 return result;
Daniel Maleae0f8f572013-08-26 23:57:52134 }
Kate Stoneb9c1b512016-09-06 20:57:50135 }
136 return fail_value;
Daniel Maleae0f8f572013-08-26 23:57:52137}
138
Kate Stoneb9c1b512016-09-06 20:57:50139int64_t StringExtractor::GetS64(int64_t fail_value, int base) {
140 if (m_index < m_packet.size()) {
141 char *end = nullptr;
142 const char *start = m_packet.c_str();
143 const char *cstr = start + m_index;
144 int64_t result = ::strtoll(cstr, &end, base);
145
146 if (end && end != cstr) {
147 m_index = end - start;
148 return result;
Greg Clayton32e0a752011-03-30 18:16:51149 }
Kate Stoneb9c1b512016-09-06 20:57:50150 }
151 return fail_value;
Greg Clayton32e0a752011-03-30 18:16:51152}
153
Kate Stoneb9c1b512016-09-06 20:57:50154uint32_t StringExtractor::GetHexMaxU32(bool little_endian,
155 uint32_t fail_value) {
156 uint32_t result = 0;
157 uint32_t nibble_count = 0;
Pavel Labathb9739d42016-08-31 08:43:37158
Kate Stoneb9c1b512016-09-06 20:57:50159 SkipSpaces();
160 if (little_endian) {
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;
166 return fail_value;
167 }
Pavel Labathb9739d42016-08-31 08:43:37168
Kate Stoneb9c1b512016-09-06 20:57:50169 uint8_t nibble_lo;
170 uint8_t nibble_hi = xdigit_to_sint(m_packet[m_index]);
171 ++m_index;
172 if (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) {
173 nibble_lo = xdigit_to_sint(m_packet[m_index]);
174 ++m_index;
Jonas Devlieghere24374ae2019-05-23 05:12:11175 result |= (static_cast<uint32_t>(nibble_hi) << (shift_amount + 4));
176 result |= (static_cast<uint32_t>(nibble_lo) << shift_amount);
Kate Stoneb9c1b512016-09-06 20:57:50177 nibble_count += 2;
178 shift_amount += 8;
179 } else {
Jonas Devlieghere24374ae2019-05-23 05:12:11180 result |= (static_cast<uint32_t>(nibble_hi) << shift_amount);
Kate Stoneb9c1b512016-09-06 20:57:50181 nibble_count += 1;
182 shift_amount += 4;
183 }
Pavel Labathb9739d42016-08-31 08:43:37184 }
Kate Stoneb9c1b512016-09-06 20:57:50185 } else {
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;
190 return fail_value;
191 }
Pavel Labathb9739d42016-08-31 08:43:37192
Kate Stoneb9c1b512016-09-06 20:57:50193 uint8_t nibble = xdigit_to_sint(m_packet[m_index]);
194 // Big Endian
195 result <<= 4;
196 result |= nibble;
Pavel Labathb9739d42016-08-31 08:43:37197
Kate Stoneb9c1b512016-09-06 20:57:50198 ++m_index;
199 ++nibble_count;
Chris Lattner30fdc8d2010-06-08 16:52:24200 }
Kate Stoneb9c1b512016-09-06 20:57:50201 }
202 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24203}
204
Kate Stoneb9c1b512016-09-06 20:57:50205uint64_t StringExtractor::GetHexMaxU64(bool little_endian,
206 uint64_t fail_value) {
207 uint64_t result = 0;
208 uint32_t nibble_count = 0;
Pavel Labathb9739d42016-08-31 08:43:37209
Kate Stoneb9c1b512016-09-06 20:57:50210 SkipSpaces();
211 if (little_endian) {
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;
217 return fail_value;
218 }
Pavel Labathb9739d42016-08-31 08:43:37219
Kate Stoneb9c1b512016-09-06 20:57:50220 uint8_t nibble_lo;
221 uint8_t nibble_hi = xdigit_to_sint(m_packet[m_index]);
222 ++m_index;
223 if (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) {
224 nibble_lo = xdigit_to_sint(m_packet[m_index]);
225 ++m_index;
Jonas Devlieghere24374ae2019-05-23 05:12:11226 result |= (static_cast<uint64_t>(nibble_hi) << (shift_amount + 4));
227 result |= (static_cast<uint64_t>(nibble_lo) << shift_amount);
Kate Stoneb9c1b512016-09-06 20:57:50228 nibble_count += 2;
229 shift_amount += 8;
230 } else {
Jonas Devlieghere24374ae2019-05-23 05:12:11231 result |= (static_cast<uint64_t>(nibble_hi) << shift_amount);
Kate Stoneb9c1b512016-09-06 20:57:50232 nibble_count += 1;
233 shift_amount += 4;
234 }
Pavel Labathb9739d42016-08-31 08:43:37235 }
Kate Stoneb9c1b512016-09-06 20:57:50236 } else {
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;
241 return fail_value;
242 }
Pavel Labathb9739d42016-08-31 08:43:37243
Kate Stoneb9c1b512016-09-06 20:57:50244 uint8_t nibble = xdigit_to_sint(m_packet[m_index]);
245 // Big Endian
246 result <<= 4;
247 result |= nibble;
Pavel Labathb9739d42016-08-31 08:43:37248
Kate Stoneb9c1b512016-09-06 20:57:50249 ++m_index;
250 ++nibble_count;
Chris Lattner30fdc8d2010-06-08 16:52:24251 }
Kate Stoneb9c1b512016-09-06 20:57:50252 }
253 return result;
Chris Lattner30fdc8d2010-06-08 16:52:24254}
255
Ravitheja Addepallye714c4f2017-05-26 11:46:27256bool StringExtractor::ConsumeFront(const llvm::StringRef &str) {
257 llvm::StringRef S = GetStringRef();
258 if (!S.startswith(str))
259 return false;
260 else
261 m_index += str.size();
262 return true;
263}
264
Kate Stoneb9c1b512016-09-06 20:57:50265size_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);
270 if (!IsGood())
271 break;
272 ++bytes_extracted;
273 dest = dest.drop_front();
274 }
Chris Lattner30fdc8d2010-06-08 16:52:24275
Kate Stoneb9c1b512016-09-06 20:57:50276 if (!dest.empty())
277 ::memset(dest.data(), fail_fill_value, dest.size());
Chris Lattner30fdc8d2010-06-08 16:52:24278
Kate Stoneb9c1b512016-09-06 20:57:50279 return bytes_extracted;
Chris Lattner30fdc8d2010-06-08 16:52:24280}
281
Adrian Prantl05097242018-04-30 16:49:04282// Decodes all valid hex encoded bytes at the head of the StringExtractor,
283// limited by dst_len.
Vince Harron6eddf8d2014-12-01 22:19:33284//
285// Returns the number of bytes successfully decoded
Kate Stoneb9c1b512016-09-06 20:57:50286size_t StringExtractor::GetHexBytesAvail(llvm::MutableArrayRef<uint8_t> dest) {
287 size_t bytes_extracted = 0;
288 while (!dest.empty()) {
289 int decode = DecodeHexU8();
290 if (decode == -1)
291 break;
Jonas Devlieghere24374ae2019-05-23 05:12:11292 dest[0] = static_cast<uint8_t>(decode);
Kate Stoneb9c1b512016-09-06 20:57:50293 dest = dest.drop_front();
294 ++bytes_extracted;
295 }
296 return bytes_extracted;
Vince Harron6eddf8d2014-12-01 22:19:33297}
Chris Lattner30fdc8d2010-06-08 16:52:24298
Pavel Labathb9739d42016-08-31 08:43:37299// Consume ASCII hex nibble character pairs until we have decoded byte_size
300// bytes of data.
301
Kate Stoneb9c1b512016-09-06 20:57:50302uint64_t StringExtractor::GetHexWithFixedSize(uint32_t byte_size,
303 bool little_endian,
304 uint64_t fail_value) {
305 if (byte_size <= 8 && GetBytesLeft() >= byte_size * 2) {
306 uint64_t result = 0;
307 uint32_t i;
308 if (little_endian) {
309 // Little Endian
310 uint32_t shift_amount;
311 for (i = 0, shift_amount = 0; i < byte_size && IsGood();
312 ++i, shift_amount += 8) {
Jonas Devlieghere24374ae2019-05-23 05:12:11313 result |= (static_cast<uint64_t>(GetHexU8()) << shift_amount);
Kate Stoneb9c1b512016-09-06 20:57:50314 }
315 } else {
316 // Big Endian
317 for (i = 0; i < byte_size && IsGood(); ++i) {
318 result <<= 8;
319 result |= GetHexU8();
320 }
Pavel Labathb9739d42016-08-31 08:43:37321 }
Kate Stoneb9c1b512016-09-06 20:57:50322 }
323 m_index = UINT64_MAX;
324 return fail_value;
Pavel Labathb9739d42016-08-31 08:43:37325}
326
Kate Stoneb9c1b512016-09-06 20:57:50327size_t StringExtractor::GetHexByteString(std::string &str) {
328 str.clear();
329 str.reserve(GetBytesLeft() / 2);
330 char ch;
331 while ((ch = GetHexU8()) != '\0')
332 str.append(1, ch);
333 return str.size();
334}
335
336size_t StringExtractor::GetHexByteStringFixedLength(std::string &str,
337 uint32_t nibble_length) {
338 str.clear();
339
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) {
344 }
345
346 return str.size();
347}
348
349size_t StringExtractor::GetHexByteStringTerminatedBy(std::string &str,
350 char terminator) {
351 str.clear();
352 char ch;
353 while ((ch = GetHexU8(0, false)) != '\0')
354 str.append(1, ch);
355 if (Peek() && *Peek() == terminator)
Greg Claytonde9d0492011-01-08 03:17:57356 return str.size();
Kate Stoneb9c1b512016-09-06 20:57:50357
358 str.clear();
359 return str.size();
Greg Claytonde9d0492011-01-08 03:17:57360}
361
Kate Stoneb9c1b512016-09-06 20:57:50362bool StringExtractor::GetNameColonValue(llvm::StringRef &name,
363 llvm::StringRef &value) {
Adrian Prantl05097242018-04-30 16:49:04364 // 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 ';'
Kate Stoneb9c1b512016-09-06 20:57:50367 if (m_index >= m_packet.size())
368 return fail();
Pavel Labathb9739d42016-08-31 08:43:37369
Kate Stoneb9c1b512016-09-06 20:57:50370 llvm::StringRef view(m_packet);
371 if (view.empty())
372 return fail();
Pavel Labathb9739d42016-08-31 08:43:37373
Kate Stoneb9c1b512016-09-06 20:57:50374 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())
378 return fail();
379 std::tie(c, d) = b.split(';');
380 if (b == c && d.empty())
381 return fail();
382
383 name = a;
384 value = c;
385 if (d.empty())
386 m_index = m_packet.size();
387 else {
388 size_t bytes_consumed = d.data() - view.data();
389 m_index += bytes_consumed;
390 }
391 return true;
Todd Fialaaf245d12014-06-30 21:05:18392}
393
Kate Stoneb9c1b512016-09-06 20:57:50394void StringExtractor::SkipSpaces() {
395 const size_t n = m_packet.size();
396 while (m_index < n && isspace(m_packet[m_index]))
397 ++m_index;
Daniel Maleae0f8f572013-08-26 23:57:52398}