diff options
author | dim <dim@FreeBSD.org> | 2017-04-02 17:24:58 +0000 |
---|---|---|
committer | dim <dim@FreeBSD.org> | 2017-04-02 17:24:58 +0000 |
commit | 60b571e49a90d38697b3aca23020d9da42fc7d7f (patch) | |
tree | 99351324c24d6cb146b6285b6caffa4d26fce188 /contrib/llvm/tools/lldb/source/Utility/StringExtractor.cpp | |
parent | bea1b22c7a9bce1dfdd73e6e5b65bc4752215180 (diff) | |
download | FreeBSD-src-60b571e49a90d38697b3aca23020d9da42fc7d7f.zip FreeBSD-src-60b571e49a90d38697b3aca23020d9da42fc7d7f.tar.gz |
Update clang, llvm, lld, lldb, compiler-rt and libc++ to 4.0.0 release:
MFC r309142 (by emaste):
Add WITH_LLD_AS_LD build knob
If set it installs LLD as /usr/bin/ld. LLD (as of version 3.9) is not
capable of linking the world and kernel, but can self-host and link many
substantial applications. GNU ld continues to be used for the world and
kernel build, regardless of how this knob is set.
It is on by default for arm64, and off for all other CPU architectures.
Sponsored by: The FreeBSD Foundation
MFC r310840:
Reapply 310775, now it also builds correctly if lldb is disabled:
Move llvm-objdump from CLANG_EXTRAS to installed by default
We currently install three tools from binutils 2.17.50: as, ld, and
objdump. Work is underway to migrate to a permissively-licensed
tool-chain, with one goal being the retirement of binutils 2.17.50.
LLVM's llvm-objdump is intended to be compatible with GNU objdump
although it is currently missing some options and may have formatting
differences. Enable it by default for testing and further investigation.
It may later be changed to install as /usr/bin/objdump, it becomes a
fully viable replacement.
Reviewed by: emaste
Differential Revision: https://reviews.freebsd.org/D8879
MFC r312855 (by emaste):
Rename LLD_AS_LD to LLD_IS_LD, for consistency with CLANG_IS_CC
Reported by: Dan McGregor <dan.mcgregor usask.ca>
MFC r313559 | glebius | 2017-02-10 18:34:48 +0100 (Fri, 10 Feb 2017) | 5 lines
Don't check struct rtentry on FreeBSD, it is an internal kernel structure.
On other systems it may be API structure for SIOCADDRT/SIOCDELRT.
Reviewed by: emaste, dim
MFC r314152 (by jkim):
Remove an assembler flag, which is redundant since r309124. The upstream
took care of it by introducing a macro NO_EXEC_STACK_DIRECTIVE.
http://llvm.org/viewvc/llvm-project?rev=273500&view=rev
Reviewed by: dim
MFC r314564:
Upgrade our copies of clang, llvm, lld, lldb, compiler-rt and libc++ to
4.0.0 (branches/release_40 296509). The release will follow soon.
Please note that from 3.5.0 onwards, clang, llvm and lldb require C++11
support to build; see UPDATING for more information.
Also note that as of 4.0.0, lld should be able to link the base system
on amd64 and aarch64. See the WITH_LLD_IS_LLD setting in src.conf(5).
Though please be aware that this is work in progress.
Release notes for llvm, clang and lld will be available here:
<http://releases.llvm.org/4.0.0/docs/ReleaseNotes.html>
<http://releases.llvm.org/4.0.0/tools/clang/docs/ReleaseNotes.html>
<http://releases.llvm.org/4.0.0/tools/lld/docs/ReleaseNotes.html>
Thanks to Ed Maste, Jan Beich, Antoine Brodin and Eric Fiselier for
their help.
Relnotes: yes
Exp-run: antoine
PR: 215969, 216008
MFC r314708:
For now, revert r287232 from upstream llvm trunk (by Daniil Fukalov):
[SCEV] limit recursion depth of CompareSCEVComplexity
Summary:
CompareSCEVComplexity goes too deep (50+ on a quite a big unrolled
loop) and runs almost infinite time.
Added cache of "equal" SCEV pairs to earlier cutoff of further
estimation. Recursion depth limit was also introduced as a parameter.
Reviewers: sanjoy
Subscribers: mzolotukhin, tstellarAMD, llvm-commits
Differential Revision: https://reviews.llvm.org/D26389
This commit is the cause of excessive compile times on skein_block.c
(and possibly other files) during kernel builds on amd64.
We never saw the problematic behavior described in this upstream commit,
so for now it is better to revert it. An upstream bug has been filed
here: https://bugs.llvm.org/show_bug.cgi?id=32142
Reported by: mjg
MFC r314795:
Reapply r287232 from upstream llvm trunk (by Daniil Fukalov):
[SCEV] limit recursion depth of CompareSCEVComplexity
Summary:
CompareSCEVComplexity goes too deep (50+ on a quite a big unrolled
loop) and runs almost infinite time.
Added cache of "equal" SCEV pairs to earlier cutoff of further
estimation. Recursion depth limit was also introduced as a parameter.
Reviewers: sanjoy
Subscribers: mzolotukhin, tstellarAMD, llvm-commits
Differential Revision: https://reviews.llvm.org/D26389
Pull in r296992 from upstream llvm trunk (by Sanjoy Das):
[SCEV] Decrease the recursion threshold for CompareValueComplexity
Fixes PR32142.
r287232 accidentally increased the recursion threshold for
CompareValueComplexity from 2 to 32. This change reverses that
change by introducing a separate flag for CompareValueComplexity's
threshold.
The latter revision fixes the excessive compile times for skein_block.c.
MFC r314907 | mmel | 2017-03-08 12:40:27 +0100 (Wed, 08 Mar 2017) | 7 lines
Unbreak ARMv6 world.
The new compiler_rt library imported with clang 4.0.0 have several fatal
issues (non-functional __udivsi3 for example) with ARM specific instrict
functions. As temporary workaround, until upstream solve these problems,
disable all thumb[1][2] related feature.
MFC r315016:
Update clang, llvm, lld, lldb, compiler-rt and libc++ to 4.0.0 release.
We were already very close to the last release candidate, so this is a
pretty minor update.
Relnotes: yes
MFC r316005:
Revert r314907, and pull in r298713 from upstream compiler-rt trunk (by
Weiming Zhao):
builtins: Select correct code fragments when compiling for Thumb1/Thum2/ARM ISA.
Summary:
Value of __ARM_ARCH_ISA_THUMB isn't based on the actual compilation
mode (-mthumb, -marm), it reflect's capability of given CPU.
Due to this:
- use __tbumb__ and __thumb2__ insteand of __ARM_ARCH_ISA_THUMB
- use '.thumb' directive consistently in all affected files
- decorate all thumb functions using
DEFINE_COMPILERRT_THUMB_FUNCTION()
---------
Note: This patch doesn't fix broken Thumb1 variant of __udivsi3 !
Reviewers: weimingz, rengolin, compnerd
Subscribers: aemerson, dim
Differential Revision: https://reviews.llvm.org/D30938
Discussed with: mmel
Diffstat (limited to 'contrib/llvm/tools/lldb/source/Utility/StringExtractor.cpp')
-rw-r--r-- | contrib/llvm/tools/lldb/source/Utility/StringExtractor.cpp | 705 |
1 files changed, 312 insertions, 393 deletions
diff --git a/contrib/llvm/tools/lldb/source/Utility/StringExtractor.cpp b/contrib/llvm/tools/lldb/source/Utility/StringExtractor.cpp index 4c0029a..d8ba397 100644 --- a/contrib/llvm/tools/lldb/source/Utility/StringExtractor.cpp +++ b/contrib/llvm/tools/lldb/source/Utility/StringExtractor.cpp @@ -13,84 +13,66 @@ #include <stdlib.h> // C++ Includes +#include <tuple> // Other libraries and framework includes // Project includes -static inline int -xdigit_to_sint (char ch) -{ - if (ch >= 'a' && ch <= 'f') - return 10 + ch - 'a'; - if (ch >= 'A' && ch <= 'F') - return 10 + ch - 'A'; - if (ch >= '0' && ch <= '9') - return ch - '0'; - return -1; +static inline int xdigit_to_sint(char ch) { + if (ch >= 'a' && ch <= 'f') + return 10 + ch - 'a'; + if (ch >= 'A' && ch <= 'F') + return 10 + ch - 'A'; + if (ch >= '0' && ch <= '9') + return ch - '0'; + return -1; } //---------------------------------------------------------------------- // StringExtractor constructor //---------------------------------------------------------------------- -StringExtractor::StringExtractor() : - m_packet(), - m_index (0) -{ -} - +StringExtractor::StringExtractor() : m_packet(), m_index(0) {} -StringExtractor::StringExtractor(const char *packet_cstr) : - m_packet(), - m_index (0) -{ - if (packet_cstr) - m_packet.assign (packet_cstr); +StringExtractor::StringExtractor(llvm::StringRef packet_str) + : m_packet(), m_index(0) { + m_packet.assign(packet_str.begin(), packet_str.end()); } +StringExtractor::StringExtractor(const char *packet_cstr) + : m_packet(), m_index(0) { + if (packet_cstr) + m_packet.assign(packet_cstr); +} //---------------------------------------------------------------------- // StringExtractor copy constructor //---------------------------------------------------------------------- -StringExtractor::StringExtractor(const StringExtractor& rhs) : - m_packet (rhs.m_packet), - m_index (rhs.m_index) -{ - -} +StringExtractor::StringExtractor(const StringExtractor &rhs) + : m_packet(rhs.m_packet), m_index(rhs.m_index) {} //---------------------------------------------------------------------- // StringExtractor assignment operator //---------------------------------------------------------------------- -const StringExtractor& -StringExtractor::operator=(const StringExtractor& rhs) -{ - if (this != &rhs) - { - m_packet = rhs.m_packet; - m_index = rhs.m_index; - - } - return *this; +const StringExtractor &StringExtractor::operator=(const StringExtractor &rhs) { + if (this != &rhs) { + m_packet = rhs.m_packet; + m_index = rhs.m_index; + } + return *this; } //---------------------------------------------------------------------- // Destructor //---------------------------------------------------------------------- -StringExtractor::~StringExtractor() -{ -} - - -char -StringExtractor::GetChar (char fail_value) -{ - if (m_index < m_packet.size()) - { - char ch = m_packet[m_index]; - ++m_index; - return ch; - } - m_index = UINT64_MAX; - return fail_value; +StringExtractor::~StringExtractor() {} + +char StringExtractor::GetChar(char fail_value) { + if (m_index < m_packet.size()) { + char ch = m_packet[m_index]; + ++m_index; + return ch; + } + m_index = UINT64_MAX; + return fail_value; } //---------------------------------------------------------------------- @@ -101,274 +83,220 @@ StringExtractor::GetChar (char fail_value) // 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() -{ - SkipSpaces(); - 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); +int StringExtractor::DecodeHexU8() { + SkipSpaces(); + 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, or return fail_value on failure //---------------------------------------------------------------------- -uint8_t -StringExtractor::GetHexU8 (uint8_t fail_value, bool set_eof_on_fail) -{ - // On success, fail_value will be overwritten with the next - // character in the stream - GetHexU8Ex(fail_value, set_eof_on_fail); - return fail_value; +uint8_t StringExtractor::GetHexU8(uint8_t fail_value, bool set_eof_on_fail) { + // On success, fail_value will be overwritten with the next + // character in the stream + GetHexU8Ex(fail_value, set_eof_on_fail); + return fail_value; } -bool -StringExtractor::GetHexU8Ex (uint8_t& ch, bool set_eof_on_fail) -{ - int byte = DecodeHexU8(); - if (byte == -1) - { - if (set_eof_on_fail || m_index >= m_packet.size()) - m_index = UINT64_MAX; - // ch should not be changed in case of failure - return false; - } - ch = (uint8_t)byte; - return true; +bool StringExtractor::GetHexU8Ex(uint8_t &ch, bool set_eof_on_fail) { + int byte = DecodeHexU8(); + if (byte == -1) { + if (set_eof_on_fail || m_index >= m_packet.size()) + m_index = UINT64_MAX; + // ch should not be changed in case of failure + return false; + } + ch = (uint8_t)byte; + return true; } -uint32_t -StringExtractor::GetU32 (uint32_t fail_value, int base) -{ - if (m_index < m_packet.size()) - { - char *end = nullptr; - const char *start = m_packet.c_str(); - const char *cstr = start + m_index; - uint32_t result = static_cast<uint32_t>(::strtoul (cstr, &end, base)); - - if (end && end != cstr) - { - m_index = end - start; - return result; - } - } - return fail_value; -} +uint32_t StringExtractor::GetU32(uint32_t fail_value, int base) { + if (m_index < m_packet.size()) { + char *end = nullptr; + const char *start = m_packet.c_str(); + const char *cstr = start + m_index; + uint32_t result = static_cast<uint32_t>(::strtoul(cstr, &end, base)); -int32_t -StringExtractor::GetS32 (int32_t fail_value, int base) -{ - if (m_index < m_packet.size()) - { - char *end = nullptr; - const char *start = m_packet.c_str(); - const char *cstr = start + m_index; - int32_t result = static_cast<int32_t>(::strtol (cstr, &end, base)); - - if (end && end != cstr) - { - m_index = end - start; - return result; - } + if (end && end != cstr) { + m_index = end - start; + return result; } - return fail_value; + } + return fail_value; } +int32_t StringExtractor::GetS32(int32_t fail_value, int base) { + if (m_index < m_packet.size()) { + char *end = nullptr; + const char *start = m_packet.c_str(); + const char *cstr = start + m_index; + int32_t result = static_cast<int32_t>(::strtol(cstr, &end, base)); -uint64_t -StringExtractor::GetU64 (uint64_t fail_value, int base) -{ - if (m_index < m_packet.size()) - { - char *end = nullptr; - const char *start = m_packet.c_str(); - const char *cstr = start + m_index; - uint64_t result = ::strtoull (cstr, &end, base); - - if (end && end != cstr) - { - m_index = end - start; - return result; - } + if (end && end != cstr) { + m_index = end - start; + return result; } - return fail_value; + } + return fail_value; } -int64_t -StringExtractor::GetS64 (int64_t fail_value, int base) -{ - if (m_index < m_packet.size()) - { - char *end = nullptr; - const char *start = m_packet.c_str(); - const char *cstr = start + m_index; - int64_t result = ::strtoll (cstr, &end, base); - - if (end && end != cstr) - { - m_index = end - start; - return result; - } +uint64_t StringExtractor::GetU64(uint64_t fail_value, int base) { + if (m_index < m_packet.size()) { + char *end = nullptr; + const char *start = m_packet.c_str(); + const char *cstr = start + m_index; + uint64_t result = ::strtoull(cstr, &end, base); + + if (end && end != cstr) { + m_index = end - start; + return result; } - return fail_value; + } + return fail_value; } +int64_t StringExtractor::GetS64(int64_t fail_value, int base) { + if (m_index < m_packet.size()) { + char *end = nullptr; + const char *start = m_packet.c_str(); + const char *cstr = start + m_index; + int64_t result = ::strtoll(cstr, &end, base); -uint32_t -StringExtractor::GetHexMaxU32 (bool little_endian, uint32_t fail_value) -{ - uint32_t result = 0; - uint32_t nibble_count = 0; - - SkipSpaces(); - if (little_endian) - { - uint32_t shift_amount = 0; - while (m_index < m_packet.size() && ::isxdigit (m_packet[m_index])) - { - // Make sure we don't exceed the size of a uint32_t... - if (nibble_count >= (sizeof(uint32_t) * 2)) - { - m_index = UINT64_MAX; - return fail_value; - } - - uint8_t nibble_lo; - uint8_t nibble_hi = xdigit_to_sint (m_packet[m_index]); - ++m_index; - if (m_index < m_packet.size() && ::isxdigit (m_packet[m_index])) - { - nibble_lo = xdigit_to_sint (m_packet[m_index]); - ++m_index; - result |= ((uint32_t)nibble_hi << (shift_amount + 4)); - result |= ((uint32_t)nibble_lo << shift_amount); - nibble_count += 2; - shift_amount += 8; - } - else - { - result |= ((uint32_t)nibble_hi << shift_amount); - nibble_count += 1; - shift_amount += 4; - } - - } + if (end && end != cstr) { + m_index = end - start; + return result; } - else - { - while (m_index < m_packet.size() && ::isxdigit (m_packet[m_index])) - { - // Make sure we don't exceed the size of a uint32_t... - if (nibble_count >= (sizeof(uint32_t) * 2)) - { - m_index = UINT64_MAX; - return fail_value; - } - - uint8_t nibble = xdigit_to_sint (m_packet[m_index]); - // Big Endian - result <<= 4; - result |= nibble; - - ++m_index; - ++nibble_count; - } - } - return result; + } + return fail_value; } -uint64_t -StringExtractor::GetHexMaxU64 (bool little_endian, uint64_t fail_value) -{ - uint64_t result = 0; - uint32_t nibble_count = 0; - - SkipSpaces(); - if (little_endian) - { - uint32_t shift_amount = 0; - while (m_index < m_packet.size() && ::isxdigit (m_packet[m_index])) - { - // Make sure we don't exceed the size of a uint64_t... - if (nibble_count >= (sizeof(uint64_t) * 2)) - { - m_index = UINT64_MAX; - return fail_value; - } - - uint8_t nibble_lo; - uint8_t nibble_hi = xdigit_to_sint (m_packet[m_index]); - ++m_index; - if (m_index < m_packet.size() && ::isxdigit (m_packet[m_index])) - { - nibble_lo = xdigit_to_sint (m_packet[m_index]); - ++m_index; - result |= ((uint64_t)nibble_hi << (shift_amount + 4)); - result |= ((uint64_t)nibble_lo << shift_amount); - nibble_count += 2; - shift_amount += 8; - } - else - { - result |= ((uint64_t)nibble_hi << shift_amount); - nibble_count += 1; - shift_amount += 4; - } - - } +uint32_t StringExtractor::GetHexMaxU32(bool little_endian, + uint32_t fail_value) { + uint32_t result = 0; + uint32_t nibble_count = 0; + + SkipSpaces(); + if (little_endian) { + uint32_t shift_amount = 0; + while (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) { + // Make sure we don't exceed the size of a uint32_t... + if (nibble_count >= (sizeof(uint32_t) * 2)) { + m_index = UINT64_MAX; + return fail_value; + } + + uint8_t nibble_lo; + uint8_t nibble_hi = xdigit_to_sint(m_packet[m_index]); + ++m_index; + if (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) { + nibble_lo = xdigit_to_sint(m_packet[m_index]); + ++m_index; + result |= ((uint32_t)nibble_hi << (shift_amount + 4)); + result |= ((uint32_t)nibble_lo << shift_amount); + nibble_count += 2; + shift_amount += 8; + } else { + result |= ((uint32_t)nibble_hi << shift_amount); + nibble_count += 1; + shift_amount += 4; + } } - else - { - while (m_index < m_packet.size() && ::isxdigit (m_packet[m_index])) - { - // Make sure we don't exceed the size of a uint64_t... - if (nibble_count >= (sizeof(uint64_t) * 2)) - { - m_index = UINT64_MAX; - return fail_value; - } - - uint8_t nibble = xdigit_to_sint (m_packet[m_index]); - // Big Endian - result <<= 4; - result |= nibble; - - ++m_index; - ++nibble_count; - } + } else { + while (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) { + // Make sure we don't exceed the size of a uint32_t... + if (nibble_count >= (sizeof(uint32_t) * 2)) { + m_index = UINT64_MAX; + return fail_value; + } + + uint8_t nibble = xdigit_to_sint(m_packet[m_index]); + // Big Endian + result <<= 4; + result |= nibble; + + ++m_index; + ++nibble_count; } - return result; + } + return result; } -size_t -StringExtractor::GetHexBytes (void *dst_void, size_t dst_len, uint8_t fail_fill_value) -{ - uint8_t *dst = (uint8_t*)dst_void; - size_t bytes_extracted = 0; - while (bytes_extracted < dst_len && GetBytesLeft ()) - { - dst[bytes_extracted] = GetHexU8 (fail_fill_value); - if (IsGood()) - ++bytes_extracted; - else - break; +uint64_t StringExtractor::GetHexMaxU64(bool little_endian, + uint64_t fail_value) { + uint64_t result = 0; + uint32_t nibble_count = 0; + + SkipSpaces(); + if (little_endian) { + uint32_t shift_amount = 0; + while (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) { + // Make sure we don't exceed the size of a uint64_t... + if (nibble_count >= (sizeof(uint64_t) * 2)) { + m_index = UINT64_MAX; + return fail_value; + } + + uint8_t nibble_lo; + uint8_t nibble_hi = xdigit_to_sint(m_packet[m_index]); + ++m_index; + if (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) { + nibble_lo = xdigit_to_sint(m_packet[m_index]); + ++m_index; + result |= ((uint64_t)nibble_hi << (shift_amount + 4)); + result |= ((uint64_t)nibble_lo << shift_amount); + nibble_count += 2; + shift_amount += 8; + } else { + result |= ((uint64_t)nibble_hi << shift_amount); + nibble_count += 1; + shift_amount += 4; + } } + } else { + while (m_index < m_packet.size() && ::isxdigit(m_packet[m_index])) { + // Make sure we don't exceed the size of a uint64_t... + if (nibble_count >= (sizeof(uint64_t) * 2)) { + m_index = UINT64_MAX; + return fail_value; + } + + uint8_t nibble = xdigit_to_sint(m_packet[m_index]); + // Big Endian + result <<= 4; + result |= nibble; + + ++m_index; + ++nibble_count; + } + } + return result; +} - for (size_t i = bytes_extracted; i < dst_len; ++i) - dst[i] = fail_fill_value; - - return bytes_extracted; +size_t StringExtractor::GetHexBytes(llvm::MutableArrayRef<uint8_t> dest, + uint8_t fail_fill_value) { + size_t bytes_extracted = 0; + while (!dest.empty() && GetBytesLeft() > 0) { + dest[0] = GetHexU8(fail_fill_value); + if (!IsGood()) + break; + ++bytes_extracted; + dest = dest.drop_front(); + } + + if (!dest.empty()) + ::memset(dest.data(), fail_fill_value, dest.size()); + + return bytes_extracted; } //---------------------------------------------------------------------- @@ -377,125 +305,116 @@ StringExtractor::GetHexBytes (void *dst_void, size_t dst_len, uint8_t fail_fill_ // // 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; +size_t StringExtractor::GetHexBytesAvail(llvm::MutableArrayRef<uint8_t> dest) { + size_t bytes_extracted = 0; + while (!dest.empty()) { + int decode = DecodeHexU8(); + if (decode == -1) + break; + dest[0] = (uint8_t)decode; + dest = dest.drop_front(); + ++bytes_extracted; + } + return bytes_extracted; } // Consume ASCII hex nibble character pairs until we have decoded byte_size // bytes of data. -uint64_t -StringExtractor::GetHexWithFixedSize (uint32_t byte_size, bool little_endian, uint64_t fail_value) -{ - if (byte_size <= 8 && GetBytesLeft() >= byte_size * 2) - { - uint64_t result = 0; - uint32_t i; - if (little_endian) - { - // Little Endian - uint32_t shift_amount; - for (i = 0, shift_amount = 0; - i < byte_size && IsGood(); - ++i, shift_amount += 8) - { - result |= ((uint64_t)GetHexU8() << shift_amount); - } - } - else - { - // Big Endian - for (i = 0; i < byte_size && IsGood(); ++i) - { - result <<= 8; - result |= GetHexU8(); - } - } +uint64_t StringExtractor::GetHexWithFixedSize(uint32_t byte_size, + bool little_endian, + uint64_t fail_value) { + if (byte_size <= 8 && GetBytesLeft() >= byte_size * 2) { + uint64_t result = 0; + uint32_t i; + if (little_endian) { + // Little Endian + uint32_t shift_amount; + for (i = 0, shift_amount = 0; i < byte_size && IsGood(); + ++i, shift_amount += 8) { + result |= ((uint64_t)GetHexU8() << shift_amount); + } + } else { + // Big Endian + for (i = 0; i < byte_size && IsGood(); ++i) { + result <<= 8; + result |= GetHexU8(); + } } - m_index = UINT64_MAX; - return fail_value; + } + m_index = UINT64_MAX; + return fail_value; } -size_t -StringExtractor::GetHexByteString (std::string &str) -{ - str.clear(); - char ch; - while ((ch = GetHexU8()) != '\0') - str.append(1, ch); - return str.size(); +size_t StringExtractor::GetHexByteString(std::string &str) { + str.clear(); + str.reserve(GetBytesLeft() / 2); + char ch; + while ((ch = GetHexU8()) != '\0') + str.append(1, ch); + return str.size(); } -size_t -StringExtractor::GetHexByteStringFixedLength (std::string &str, uint32_t nibble_length) -{ - str.clear(); +size_t StringExtractor::GetHexByteStringFixedLength(std::string &str, + uint32_t nibble_length) { + str.clear(); - uint32_t nibble_count = 0; - for (const char *pch = Peek(); (nibble_count < nibble_length) && (pch != nullptr); str.append(1, GetHexU8(0, false)), pch = Peek (), nibble_count += 2) - {} + uint32_t nibble_count = 0; + for (const char *pch = Peek(); + (nibble_count < nibble_length) && (pch != nullptr); + str.append(1, GetHexU8(0, false)), pch = Peek(), nibble_count += 2) { + } - return str.size(); + return str.size(); } -size_t -StringExtractor::GetHexByteStringTerminatedBy (std::string &str, - char terminator) -{ - str.clear(); - char ch; - while ((ch = GetHexU8(0,false)) != '\0') - str.append(1, ch); - if (Peek() && *Peek() == terminator) - return str.size(); - - str.clear(); +size_t StringExtractor::GetHexByteStringTerminatedBy(std::string &str, + char terminator) { + str.clear(); + char ch; + while ((ch = GetHexU8(0, false)) != '\0') + str.append(1, ch); + if (Peek() && *Peek() == terminator) return str.size(); -} -bool -StringExtractor::GetNameColonValue (std::string &name, std::string &value) -{ - // Read something in the form of NNNN:VVVV; where NNNN is any character - // that is not a colon, followed by a ':' character, then a value (one or - // more ';' chars), followed by a ';' - if (m_index < m_packet.size()) - { - const size_t colon_idx = m_packet.find (':', m_index); - if (colon_idx != std::string::npos) - { - const size_t semicolon_idx = m_packet.find (';', colon_idx); - if (semicolon_idx != std::string::npos) - { - name.assign (m_packet, m_index, colon_idx - m_index); - value.assign (m_packet, colon_idx + 1, semicolon_idx - (colon_idx + 1)); - m_index = semicolon_idx + 1; - return true; - } - } - } - m_index = UINT64_MAX; - return false; + str.clear(); + return str.size(); } -void -StringExtractor::SkipSpaces () -{ - const size_t n = m_packet.size(); - while (m_index < n && isspace(m_packet[m_index])) - ++m_index; +bool StringExtractor::GetNameColonValue(llvm::StringRef &name, + llvm::StringRef &value) { + // Read something in the form of NNNN:VVVV; where NNNN is any character + // that is not a colon, followed by a ':' character, then a value (one or + // more ';' chars), followed by a ';' + if (m_index >= m_packet.size()) + return fail(); + + llvm::StringRef view(m_packet); + if (view.empty()) + return fail(); + + llvm::StringRef a, b, c, d; + view = view.substr(m_index); + std::tie(a, b) = view.split(':'); + if (a.empty() || b.empty()) + return fail(); + std::tie(c, d) = b.split(';'); + if (b == c && d.empty()) + return fail(); + + name = a; + value = c; + if (d.empty()) + m_index = m_packet.size(); + else { + size_t bytes_consumed = d.data() - view.data(); + m_index += bytes_consumed; + } + return true; } +void StringExtractor::SkipSpaces() { + const size_t n = m_packet.size(); + while (m_index < n && isspace(m_packet[m_index])) + ++m_index; +} |