From 9f8792e75bc2878817db88339f2b8e678a089bd7 Mon Sep 17 00:00:00 2001 From: David Blaikie Date: Mon, 24 Jun 2019 20:43:36 +0000 Subject: [PATCH] NFC: DataExtractor: use decodeULEB128 to implement getULEB128 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@364230 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/DataExtractor.cpp | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/lib/Support/DataExtractor.cpp b/lib/Support/DataExtractor.cpp index 18e1423b546..4b26718c68c 100644 --- a/lib/Support/DataExtractor.cpp +++ b/lib/Support/DataExtractor.cpp @@ -10,6 +10,7 @@ #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/Host.h" #include "llvm/Support/SwapByteOrder.h" +#include "llvm/Support/LEB128.h" using namespace llvm; template @@ -145,24 +146,17 @@ StringRef DataExtractor::getCStrRef(uint32_t *OffsetPtr) const { } uint64_t DataExtractor::getULEB128(uint32_t *offset_ptr) const { - uint64_t result = 0; - if (Data.empty()) + assert(*offset_ptr <= Data.size()); + + const char *error; + unsigned bytes_read; + uint64_t result = decodeULEB128( + reinterpret_cast(Data.data() + *offset_ptr), &bytes_read, + reinterpret_cast(Data.data() + Data.size()), &error); + if (error) return 0; - - unsigned shift = 0; - uint32_t offset = *offset_ptr; - uint8_t byte = 0; - - while (isValidOffset(offset)) { - byte = Data[offset++]; - result |= uint64_t(byte & 0x7f) << shift; - shift += 7; - if ((byte & 0x80) == 0) { - *offset_ptr = offset; - return result; - } - } - return 0; + *offset_ptr += bytes_read; + return result; } int64_t DataExtractor::getSLEB128(uint32_t *offset_ptr) const { -- 2.50.1