diff options
| author | Vince Harron <vharron@google.com> | 2014-12-01 22:19:33 +0000 |
|---|---|---|
| committer | Vince Harron <vharron@google.com> | 2014-12-01 22:19:33 +0000 |
| commit | 6eddf8df2a3fd73c549482fae9adf226b2dd4f22 (patch) | |
| tree | 4cbd011f9069bc45acdc19f93da5fcdf7ebdba86 /lldb/source/Utility | |
| parent | e5a9eaaaedf429e665a6d2632c171613f0eca068 (diff) | |
| download | bcm5719-llvm-6eddf8df2a3fd73c549482fae9adf226b2dd4f22.tar.gz bcm5719-llvm-6eddf8df2a3fd73c549482fae9adf226b2dd4f22.zip | |
Added StringExtractor::DecodeHexU8 && GetHexBytesAvail
DecodeHexU8 returns a decoded hex character pair, returns -1 if a
valid hex pair is not available.
GetHexBytesAvail decodes all available hex pairs.
llvm-svn: 223081
Diffstat (limited to 'lldb/source/Utility')
| -rw-r--r-- | lldb/source/Utility/StringExtractor.cpp | 105 | ||||
| -rw-r--r-- | lldb/source/Utility/StringExtractor.h | 7 |
2 files changed, 63 insertions, 49 deletions
diff --git a/lldb/source/Utility/StringExtractor.cpp b/lldb/source/Utility/StringExtractor.cpp index 8747853213c..a2cbe6cd486 100644 --- a/lldb/source/Utility/StringExtractor.cpp +++ b/lldb/source/Utility/StringExtractor.cpp @@ -16,43 +16,6 @@ // Other libraries and framework includes // Project includes -static const uint8_t -g_hex_ascii_to_hex_integer[256] = { - - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, - 0x8, 0x9, 255, 255, 255, 255, 255, 255, - 255, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, - 255, 255, 255, 255, 255, 255, 255, 255, -}; - static inline int xdigit_to_sint (char ch) { @@ -60,7 +23,9 @@ xdigit_to_sint (char ch) return 10 + ch - 'a'; if (ch >= 'A' && ch <= 'F') return 10 + ch - 'A'; - return ch - '0'; + if (ch >= '0' && ch <= '9') + return ch - '0'; + return -1; } //---------------------------------------------------------------------- @@ -129,25 +94,45 @@ StringExtractor::GetChar (char fail_value) } //---------------------------------------------------------------------- +// If a pair of valid hex digits exist at the head of the +// StringExtractor they are decoded into an unsigned byte and returned +// by this function +// +// If there is not a pair of valid hex digits at the head of the +// StringExtractor, it is left unchanged and -1 is returned +//---------------------------------------------------------------------- +int +StringExtractor::DecodeHexU8() +{ + if (GetBytesLeft() < 2) + { + return -1; + } + const int hi_nibble = xdigit_to_sint(m_packet[m_index]); + const int lo_nibble = xdigit_to_sint(m_packet[m_index+1]); + if (hi_nibble == -1 || lo_nibble == -1) + { + return -1; + } + m_index += 2; + return (uint8_t)((hi_nibble << 4) + lo_nibble); +} + +//---------------------------------------------------------------------- // Extract an unsigned character from two hex ASCII chars in the packet // string //---------------------------------------------------------------------- uint8_t StringExtractor::GetHexU8 (uint8_t fail_value, bool set_eof_on_fail) { - if (GetBytesLeft() >= 2) + int byte = DecodeHexU8(); + if (byte == -1) { - const uint8_t hi_nibble = g_hex_ascii_to_hex_integer[static_cast<uint8_t>(m_packet[m_index])]; - const uint8_t lo_nibble = g_hex_ascii_to_hex_integer[static_cast<uint8_t>(m_packet[m_index+1])]; - if (hi_nibble < 16 && lo_nibble < 16) - { - m_index += 2; - return (hi_nibble << 4) + lo_nibble; - } + if (set_eof_on_fail || m_index >= m_packet.size()) + m_index = UINT64_MAX; + return fail_value; } - if (set_eof_on_fail || m_index >= m_packet.size()) - m_index = UINT64_MAX; - return fail_value; + return (uint8_t)byte; } uint32_t @@ -372,6 +357,28 @@ StringExtractor::GetHexBytes (void *dst_void, size_t dst_len, uint8_t fail_fill_ return bytes_extracted; } +//---------------------------------------------------------------------- +// Decodes all valid hex encoded bytes at the head of the +// StringExtractor, limited by dst_len. +// +// Returns the number of bytes successfully decoded +//---------------------------------------------------------------------- +size_t +StringExtractor::GetHexBytesAvail (void *dst_void, size_t dst_len) +{ + uint8_t *dst = (uint8_t*)dst_void; + size_t bytes_extracted = 0; + while (bytes_extracted < dst_len) + { + int decode = DecodeHexU8(); + if (decode == -1) + { + break; + } + dst[bytes_extracted++] = (uint8_t)decode; + } + return bytes_extracted; +} // Consume ASCII hex nibble character pairs until we have decoded byte_size // bytes of data. diff --git a/lldb/source/Utility/StringExtractor.h b/lldb/source/Utility/StringExtractor.h index 697499309ce..49dfe99bd35 100644 --- a/lldb/source/Utility/StringExtractor.h +++ b/lldb/source/Utility/StringExtractor.h @@ -92,9 +92,13 @@ public: return m_packet.size() - m_index; return 0; } + char GetChar (char fail_value = '\0'); + int + DecodeHexU8(); + uint8_t GetHexU8 (uint8_t fail_value = 0, bool set_eof_on_fail = true); @@ -122,6 +126,9 @@ public: size_t GetHexBytes (void *dst, size_t dst_len, uint8_t fail_fill_value); + size_t + GetHexBytesAvail (void *dst, size_t dst_len); + uint64_t GetHexWithFixedSize (uint32_t byte_size, bool little_endian, uint64_t fail_value); |

