1 //===-- DataExtractor.cpp -------------------------------------------------===//
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 "llvm/Support/DataExtractor.h"
11 #include "llvm/Support/ErrorHandling.h"
12 #include "llvm/Support/Host.h"
13 #include "llvm/Support/SwapByteOrder.h"
17 static T getU(uint32_t *offset_ptr, const DataExtractor *de,
18 bool isLittleEndian, const char *Data) {
20 uint32_t offset = *offset_ptr;
21 if (de->isValidOffsetForDataOfSize(offset, sizeof(val))) {
22 std::memcpy(&val, &Data[offset], sizeof(val));
23 if (sys::isLittleEndianHost() != isLittleEndian)
24 val = sys::SwapByteOrder(val);
27 *offset_ptr += sizeof(val);
33 static T *getUs(uint32_t *offset_ptr, T *dst, uint32_t count,
34 const DataExtractor *de, bool isLittleEndian, const char *Data){
35 uint32_t offset = *offset_ptr;
37 if (count > 0 && de->isValidOffsetForDataOfSize(offset, sizeof(*dst)*count)) {
38 for (T *value_ptr = dst, *end = dst + count; value_ptr != end;
39 ++value_ptr, offset += sizeof(*dst))
40 *value_ptr = getU<T>(offset_ptr, de, isLittleEndian, Data);
43 // Return a non-NULL pointer to the converted data as an indicator of
50 uint8_t DataExtractor::getU8(uint32_t *offset_ptr) const {
51 return getU<uint8_t>(offset_ptr, this, IsLittleEndian, Data.data());
55 DataExtractor::getU8(uint32_t *offset_ptr, uint8_t *dst, uint32_t count) const {
56 return getUs<uint8_t>(offset_ptr, dst, count, this, IsLittleEndian,
61 uint16_t DataExtractor::getU16(uint32_t *offset_ptr) const {
62 return getU<uint16_t>(offset_ptr, this, IsLittleEndian, Data.data());
65 uint16_t *DataExtractor::getU16(uint32_t *offset_ptr, uint16_t *dst,
66 uint32_t count) const {
67 return getUs<uint16_t>(offset_ptr, dst, count, this, IsLittleEndian,
71 uint32_t DataExtractor::getU32(uint32_t *offset_ptr) const {
72 return getU<uint32_t>(offset_ptr, this, IsLittleEndian, Data.data());
75 uint32_t *DataExtractor::getU32(uint32_t *offset_ptr, uint32_t *dst,
76 uint32_t count) const {
77 return getUs<uint32_t>(offset_ptr, dst, count, this, IsLittleEndian,
81 uint64_t DataExtractor::getU64(uint32_t *offset_ptr) const {
82 return getU<uint64_t>(offset_ptr, this, IsLittleEndian, Data.data());
85 uint64_t *DataExtractor::getU64(uint32_t *offset_ptr, uint64_t *dst,
86 uint32_t count) const {
87 return getUs<uint64_t>(offset_ptr, dst, count, this, IsLittleEndian,
92 DataExtractor::getUnsigned(uint32_t *offset_ptr, uint32_t byte_size) const {
95 return getU8(offset_ptr);
97 return getU16(offset_ptr);
99 return getU32(offset_ptr);
101 return getU64(offset_ptr);
103 llvm_unreachable("getUnsigned unhandled case!");
107 DataExtractor::getSigned(uint32_t *offset_ptr, uint32_t byte_size) const {
110 return (int8_t)getU8(offset_ptr);
112 return (int16_t)getU16(offset_ptr);
114 return (int32_t)getU32(offset_ptr);
116 return (int64_t)getU64(offset_ptr);
118 llvm_unreachable("getSigned unhandled case!");
121 const char *DataExtractor::getCStr(uint32_t *offset_ptr) const {
122 uint32_t offset = *offset_ptr;
123 StringRef::size_type pos = Data.find('\0', offset);
124 if (pos != StringRef::npos) {
125 *offset_ptr = pos + 1;
126 return Data.data() + offset;
131 uint64_t DataExtractor::getULEB128(uint32_t *offset_ptr) const {
137 uint32_t offset = *offset_ptr;
140 while (isValidOffset(offset)) {
141 byte = Data[offset++];
142 result |= uint64_t(byte & 0x7f) << shift;
144 if ((byte & 0x80) == 0)
148 *offset_ptr = offset;
152 int64_t DataExtractor::getSLEB128(uint32_t *offset_ptr) const {
158 uint32_t offset = *offset_ptr;
161 while (isValidOffset(offset)) {
162 byte = Data[offset++];
163 result |= uint64_t(byte & 0x7f) << shift;
165 if ((byte & 0x80) == 0)
169 // Sign bit of byte is 2nd high order bit (0x40)
170 if (shift < 64 && (byte & 0x40))
171 result |= -(1ULL << shift);
173 *offset_ptr = offset;