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 "Utility/StringExtractor.h"
16 // Other libraries and framework includes
20 xdigit_to_sint (char ch)
22 if (ch >= 'a' && ch <= 'f')
24 if (ch >= 'A' && ch <= 'F')
26 if (ch >= '0' && ch <= '9')
31 //----------------------------------------------------------------------
32 // StringExtractor constructor
33 //----------------------------------------------------------------------
34 StringExtractor::StringExtractor() :
41 StringExtractor::StringExtractor(const char *packet_cstr) :
46 m_packet.assign (packet_cstr);
50 //----------------------------------------------------------------------
51 // StringExtractor copy constructor
52 //----------------------------------------------------------------------
53 StringExtractor::StringExtractor(const StringExtractor& rhs) :
54 m_packet (rhs.m_packet),
60 //----------------------------------------------------------------------
61 // StringExtractor assignment operator
62 //----------------------------------------------------------------------
63 const StringExtractor&
64 StringExtractor::operator=(const StringExtractor& rhs)
68 m_packet = rhs.m_packet;
69 m_index = rhs.m_index;
75 //----------------------------------------------------------------------
77 //----------------------------------------------------------------------
78 StringExtractor::~StringExtractor()
84 StringExtractor::GetChar (char fail_value)
86 if (m_index < m_packet.size())
88 char ch = m_packet[m_index];
96 //----------------------------------------------------------------------
97 // If a pair of valid hex digits exist at the head of the
98 // StringExtractor they are decoded into an unsigned byte and returned
101 // If there is not a pair of valid hex digits at the head of the
102 // StringExtractor, it is left unchanged and -1 is returned
103 //----------------------------------------------------------------------
105 StringExtractor::DecodeHexU8()
107 if (GetBytesLeft() < 2)
111 const int hi_nibble = xdigit_to_sint(m_packet[m_index]);
112 const int lo_nibble = xdigit_to_sint(m_packet[m_index+1]);
113 if (hi_nibble == -1 || lo_nibble == -1)
118 return (uint8_t)((hi_nibble << 4) + lo_nibble);
121 //----------------------------------------------------------------------
122 // Extract an unsigned character from two hex ASCII chars in the packet
124 //----------------------------------------------------------------------
126 StringExtractor::GetHexU8 (uint8_t fail_value, bool set_eof_on_fail)
128 int byte = DecodeHexU8();
131 if (set_eof_on_fail || m_index >= m_packet.size())
132 m_index = UINT64_MAX;
135 return (uint8_t)byte;
139 StringExtractor::GetU32 (uint32_t fail_value, int base)
141 if (m_index < m_packet.size())
144 const char *start = m_packet.c_str();
145 const char *cstr = start + m_index;
146 uint32_t result = ::strtoul (cstr, &end, base);
148 if (end && end != cstr)
150 m_index = end - start;
158 StringExtractor::GetS32 (int32_t fail_value, int base)
160 if (m_index < m_packet.size())
163 const char *start = m_packet.c_str();
164 const char *cstr = start + m_index;
165 int32_t result = ::strtol (cstr, &end, base);
167 if (end && end != cstr)
169 m_index = end - start;
178 StringExtractor::GetU64 (uint64_t fail_value, int base)
180 if (m_index < m_packet.size())
183 const char *start = m_packet.c_str();
184 const char *cstr = start + m_index;
185 uint64_t result = ::strtoull (cstr, &end, base);
187 if (end && end != cstr)
189 m_index = end - start;
197 StringExtractor::GetS64 (int64_t fail_value, int base)
199 if (m_index < m_packet.size())
202 const char *start = m_packet.c_str();
203 const char *cstr = start + m_index;
204 int64_t result = ::strtoll (cstr, &end, base);
206 if (end && end != cstr)
208 m_index = end - start;
217 StringExtractor::GetHexMaxU32 (bool little_endian, uint32_t fail_value)
220 uint32_t nibble_count = 0;
224 uint32_t shift_amount = 0;
225 while (m_index < m_packet.size() && ::isxdigit (m_packet[m_index]))
227 // Make sure we don't exceed the size of a uint32_t...
228 if (nibble_count >= (sizeof(uint32_t) * 2))
230 m_index = UINT64_MAX;
235 uint8_t nibble_hi = xdigit_to_sint (m_packet[m_index]);
237 if (m_index < m_packet.size() && ::isxdigit (m_packet[m_index]))
239 nibble_lo = xdigit_to_sint (m_packet[m_index]);
241 result |= ((uint32_t)nibble_hi << (shift_amount + 4));
242 result |= ((uint32_t)nibble_lo << shift_amount);
248 result |= ((uint32_t)nibble_hi << shift_amount);
257 while (m_index < m_packet.size() && ::isxdigit (m_packet[m_index]))
259 // Make sure we don't exceed the size of a uint32_t...
260 if (nibble_count >= (sizeof(uint32_t) * 2))
262 m_index = UINT64_MAX;
266 uint8_t nibble = xdigit_to_sint (m_packet[m_index]);
279 StringExtractor::GetHexMaxU64 (bool little_endian, uint64_t fail_value)
282 uint32_t nibble_count = 0;
286 uint32_t shift_amount = 0;
287 while (m_index < m_packet.size() && ::isxdigit (m_packet[m_index]))
289 // Make sure we don't exceed the size of a uint64_t...
290 if (nibble_count >= (sizeof(uint64_t) * 2))
292 m_index = UINT64_MAX;
297 uint8_t nibble_hi = xdigit_to_sint (m_packet[m_index]);
299 if (m_index < m_packet.size() && ::isxdigit (m_packet[m_index]))
301 nibble_lo = xdigit_to_sint (m_packet[m_index]);
303 result |= ((uint64_t)nibble_hi << (shift_amount + 4));
304 result |= ((uint64_t)nibble_lo << shift_amount);
310 result |= ((uint64_t)nibble_hi << shift_amount);
319 while (m_index < m_packet.size() && ::isxdigit (m_packet[m_index]))
321 // Make sure we don't exceed the size of a uint64_t...
322 if (nibble_count >= (sizeof(uint64_t) * 2))
324 m_index = UINT64_MAX;
328 uint8_t nibble = xdigit_to_sint (m_packet[m_index]);
341 StringExtractor::GetHexBytes (void *dst_void, size_t dst_len, uint8_t fail_fill_value)
343 uint8_t *dst = (uint8_t*)dst_void;
344 size_t bytes_extracted = 0;
345 while (bytes_extracted < dst_len && GetBytesLeft ())
347 dst[bytes_extracted] = GetHexU8 (fail_fill_value);
354 for (size_t i = bytes_extracted; i < dst_len; ++i)
355 dst[i] = fail_fill_value;
357 return bytes_extracted;
360 //----------------------------------------------------------------------
361 // Decodes all valid hex encoded bytes at the head of the
362 // StringExtractor, limited by dst_len.
364 // Returns the number of bytes successfully decoded
365 //----------------------------------------------------------------------
367 StringExtractor::GetHexBytesAvail (void *dst_void, size_t dst_len)
369 uint8_t *dst = (uint8_t*)dst_void;
370 size_t bytes_extracted = 0;
371 while (bytes_extracted < dst_len)
373 int decode = DecodeHexU8();
378 dst[bytes_extracted++] = (uint8_t)decode;
380 return bytes_extracted;
383 // Consume ASCII hex nibble character pairs until we have decoded byte_size
387 StringExtractor::GetHexWithFixedSize (uint32_t byte_size, bool little_endian, uint64_t fail_value)
389 if (byte_size <= 8 && GetBytesLeft() >= byte_size * 2)
396 uint32_t shift_amount;
397 for (i = 0, shift_amount = 0;
398 i < byte_size && IsGood();
399 ++i, shift_amount += 8)
401 result |= ((uint64_t)GetHexU8() << shift_amount);
407 for (i = 0; i < byte_size && IsGood(); ++i)
410 result |= GetHexU8();
414 m_index = UINT64_MAX;
419 StringExtractor::GetHexByteString (std::string &str)
423 while ((ch = GetHexU8()) != '\0')
429 StringExtractor::GetHexByteStringFixedLength (std::string &str, uint32_t nibble_length)
433 uint32_t nibble_count = 0;
434 for (const char *pch = Peek(); (nibble_count < nibble_length) && (pch != nullptr); str.append(1, GetHexU8(0, false)), pch = Peek (), nibble_count += 2)
441 StringExtractor::GetHexByteStringTerminatedBy (std::string &str,
446 while ((ch = GetHexU8(0,false)) != '\0')
448 if (Peek() && *Peek() == terminator)
456 StringExtractor::GetNameColonValue (std::string &name, std::string &value)
458 // Read something in the form of NNNN:VVVV; where NNNN is any character
459 // that is not a colon, followed by a ':' character, then a value (one or
460 // more ';' chars), followed by a ';'
461 if (m_index < m_packet.size())
463 const size_t colon_idx = m_packet.find (':', m_index);
464 if (colon_idx != std::string::npos)
466 const size_t semicolon_idx = m_packet.find (';', colon_idx);
467 if (semicolon_idx != std::string::npos)
469 name.assign (m_packet, m_index, colon_idx - m_index);
470 value.assign (m_packet, colon_idx + 1, semicolon_idx - (colon_idx + 1));
471 m_index = semicolon_idx + 1;
476 m_index = UINT64_MAX;