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 g_hex_ascii_to_hex_integer[256] = {
22 255, 255, 255, 255, 255, 255, 255, 255,
23 255, 255, 255, 255, 255, 255, 255, 255,
24 255, 255, 255, 255, 255, 255, 255, 255,
25 255, 255, 255, 255, 255, 255, 255, 255,
26 255, 255, 255, 255, 255, 255, 255, 255,
27 255, 255, 255, 255, 255, 255, 255, 255,
28 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
29 0x8, 0x9, 255, 255, 255, 255, 255, 255,
30 255, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 255,
31 255, 255, 255, 255, 255, 255, 255, 255,
32 255, 255, 255, 255, 255, 255, 255, 255,
33 255, 255, 255, 255, 255, 255, 255, 255,
34 255, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 255,
35 255, 255, 255, 255, 255, 255, 255, 255,
36 255, 255, 255, 255, 255, 255, 255, 255,
37 255, 255, 255, 255, 255, 255, 255, 255,
38 255, 255, 255, 255, 255, 255, 255, 255,
39 255, 255, 255, 255, 255, 255, 255, 255,
40 255, 255, 255, 255, 255, 255, 255, 255,
41 255, 255, 255, 255, 255, 255, 255, 255,
42 255, 255, 255, 255, 255, 255, 255, 255,
43 255, 255, 255, 255, 255, 255, 255, 255,
44 255, 255, 255, 255, 255, 255, 255, 255,
45 255, 255, 255, 255, 255, 255, 255, 255,
46 255, 255, 255, 255, 255, 255, 255, 255,
47 255, 255, 255, 255, 255, 255, 255, 255,
48 255, 255, 255, 255, 255, 255, 255, 255,
49 255, 255, 255, 255, 255, 255, 255, 255,
50 255, 255, 255, 255, 255, 255, 255, 255,
51 255, 255, 255, 255, 255, 255, 255, 255,
52 255, 255, 255, 255, 255, 255, 255, 255,
53 255, 255, 255, 255, 255, 255, 255, 255,
57 xdigit_to_sint (char ch)
59 if (ch >= 'a' && ch <= 'f')
61 if (ch >= 'A' && ch <= 'F')
66 static inline unsigned int
67 xdigit_to_uint (uint8_t ch)
69 if (ch >= 'a' && ch <= 'f')
70 return 10u + ch - 'a';
71 if (ch >= 'A' && ch <= 'F')
72 return 10u + ch - 'A';
76 //----------------------------------------------------------------------
77 // StringExtractor constructor
78 //----------------------------------------------------------------------
79 StringExtractor::StringExtractor() :
86 StringExtractor::StringExtractor(const char *packet_cstr) :
91 m_packet.assign (packet_cstr);
95 //----------------------------------------------------------------------
96 // StringExtractor copy constructor
97 //----------------------------------------------------------------------
98 StringExtractor::StringExtractor(const StringExtractor& rhs) :
99 m_packet (rhs.m_packet),
100 m_index (rhs.m_index)
105 //----------------------------------------------------------------------
106 // StringExtractor assignment operator
107 //----------------------------------------------------------------------
108 const StringExtractor&
109 StringExtractor::operator=(const StringExtractor& rhs)
113 m_packet = rhs.m_packet;
114 m_index = rhs.m_index;
120 //----------------------------------------------------------------------
122 //----------------------------------------------------------------------
123 StringExtractor::~StringExtractor()
129 StringExtractor::GetChar (char fail_value)
131 if (m_index < m_packet.size())
133 char ch = m_packet[m_index];
137 m_index = UINT64_MAX;
141 //----------------------------------------------------------------------
142 // Extract an unsigned character from two hex ASCII chars in the packet
144 //----------------------------------------------------------------------
146 StringExtractor::GetHexU8 (uint8_t fail_value, bool set_eof_on_fail)
148 uint32_t i = m_index;
149 if ((i + 2) <= m_packet.size())
151 const uint8_t hi_nibble = g_hex_ascii_to_hex_integer[static_cast<uint8_t>(m_packet[i])];
152 const uint8_t lo_nibble = g_hex_ascii_to_hex_integer[static_cast<uint8_t>(m_packet[i+1])];
153 if (hi_nibble < 16 && lo_nibble < 16)
156 return (hi_nibble << 4) + lo_nibble;
159 if (set_eof_on_fail || m_index >= m_packet.size())
160 m_index = UINT64_MAX;
165 StringExtractor::GetU32 (uint32_t fail_value, int base)
167 if (m_index < m_packet.size())
170 const char *start = m_packet.c_str();
171 const char *uint_cstr = start + m_index;
172 uint32_t result = ::strtoul (uint_cstr, &end, base);
174 if (end && end != uint_cstr)
176 m_index = end - start;
185 StringExtractor::GetHexMaxU32 (bool little_endian, uint32_t fail_value)
188 uint32_t nibble_count = 0;
192 uint32_t shift_amount = 0;
193 while (m_index < m_packet.size() && ::isxdigit (m_packet[m_index]))
195 // Make sure we don't exceed the size of a uint32_t...
196 if (nibble_count >= (sizeof(uint32_t) * 2))
198 m_index = UINT64_MAX;
203 uint8_t nibble_hi = xdigit_to_sint (m_packet[m_index]);
205 if (m_index < m_packet.size() && ::isxdigit (m_packet[m_index]))
207 nibble_lo = xdigit_to_sint (m_packet[m_index]);
209 result |= ((uint32_t)nibble_hi << (shift_amount + 4));
210 result |= ((uint32_t)nibble_lo << shift_amount);
216 result |= ((uint32_t)nibble_hi << shift_amount);
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;
234 uint8_t nibble = xdigit_to_sint (m_packet[m_index]);
247 StringExtractor::GetHexMaxU64 (bool little_endian, uint64_t fail_value)
250 uint32_t nibble_count = 0;
254 uint32_t shift_amount = 0;
255 while (m_index < m_packet.size() && ::isxdigit (m_packet[m_index]))
257 // Make sure we don't exceed the size of a uint64_t...
258 if (nibble_count >= (sizeof(uint64_t) * 2))
260 m_index = UINT64_MAX;
265 uint8_t nibble_hi = xdigit_to_sint (m_packet[m_index]);
267 if (m_index < m_packet.size() && ::isxdigit (m_packet[m_index]))
269 nibble_lo = xdigit_to_sint (m_packet[m_index]);
271 result |= ((uint64_t)nibble_hi << (shift_amount + 4));
272 result |= ((uint64_t)nibble_lo << shift_amount);
278 result |= ((uint64_t)nibble_hi << shift_amount);
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;
296 uint8_t nibble = xdigit_to_sint (m_packet[m_index]);
309 StringExtractor::GetHexBytes (void *dst_void, size_t dst_len, uint8_t fail_fill_value)
311 uint8_t *dst = (uint8_t*)dst_void;
312 size_t bytes_extracted = 0;
313 while (bytes_extracted < dst_len && GetBytesLeft ())
315 dst[bytes_extracted] = GetHexU8 (fail_fill_value);
322 for (size_t i = bytes_extracted; i < dst_len; ++i)
323 dst[i] = fail_fill_value;
325 return bytes_extracted;
329 // Consume ASCII hex nibble character pairs until we have decoded byte_size
333 StringExtractor::GetHexWithFixedSize (uint32_t byte_size, bool little_endian, uint64_t fail_value)
335 if (byte_size <= 8 && GetBytesLeft() >= byte_size * 2)
342 uint32_t shift_amount;
343 for (i = 0, shift_amount = 0;
344 i < byte_size && IsGood();
345 ++i, shift_amount += 8)
347 result |= ((uint64_t)GetHexU8() << shift_amount);
353 for (i = 0; i < byte_size && IsGood(); ++i)
356 result |= GetHexU8();
360 m_index = UINT64_MAX;
365 StringExtractor::GetHexByteString (std::string &str)
369 while ((ch = GetHexU8()) != '\0')
375 StringExtractor::GetNameColonValue (std::string &name, std::string &value)
377 // Read something in the form of NNNN:VVVV; where NNNN is any character
378 // that is not a colon, followed by a ':' character, then a value (one or
379 // more ';' chars), followed by a ';'
380 if (m_index < m_packet.size())
382 const size_t colon_idx = m_packet.find (':', m_index);
383 if (colon_idx != std::string::npos)
385 const size_t semicolon_idx = m_packet.find (';', colon_idx);
386 if (semicolon_idx != std::string::npos)
388 name.assign (m_packet, m_index, colon_idx - m_index);
389 value.assign (m_packet, colon_idx + 1, semicolon_idx - (colon_idx + 1));
390 m_index = semicolon_idx + 1;
395 m_index = UINT64_MAX;