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
diff --git a/lldb/source/Utility/StringExtractor.cpp b/lldb/source/Utility/StringExtractor.cpp
index 8747853..a2cbe6c 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 @@
         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 @@
 }
 
 //----------------------------------------------------------------------
+// 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 @@
     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.