blob: 673bbb4d06f4aa0b7053d839006eb217a6ead522 [file] [log] [blame]
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +00001//===-- DataExtractor.cpp -------------------------------------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Support/DataExtractor.h"
10#include "llvm/Support/ErrorHandling.h"
11#include "llvm/Support/Host.h"
12#include "llvm/Support/SwapByteOrder.h"
David Blaikie8242f352019-06-24 20:43:36 +000013#include "llvm/Support/LEB128.h"
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +000014using namespace llvm;
15
16template <typename T>
17static T getU(uint32_t *offset_ptr, const DataExtractor *de,
18 bool isLittleEndian, const char *Data) {
19 T val = 0;
20 uint32_t offset = *offset_ptr;
21 if (de->isValidOffsetForDataOfSize(offset, sizeof(val))) {
22 std::memcpy(&val, &Data[offset], sizeof(val));
Rafael Espindola41cb64f2013-04-15 14:44:24 +000023 if (sys::IsLittleEndianHost != isLittleEndian)
Artyom Skrobov9aea8432014-06-14 13:18:07 +000024 sys::swapByteOrder(val);
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +000025
26 // Advance the offset
27 *offset_ptr += sizeof(val);
28 }
29 return val;
30}
31
32template <typename T>
33static 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;
36
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);
41 // Advance the offset
42 *offset_ptr = offset;
43 // Return a non-NULL pointer to the converted data as an indicator of
44 // success
45 return dst;
46 }
Craig Topperc10719f2014-04-07 04:17:22 +000047 return nullptr;
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +000048}
49
50uint8_t DataExtractor::getU8(uint32_t *offset_ptr) const {
51 return getU<uint8_t>(offset_ptr, this, IsLittleEndian, Data.data());
52}
53
54uint8_t *
55DataExtractor::getU8(uint32_t *offset_ptr, uint8_t *dst, uint32_t count) const {
56 return getUs<uint8_t>(offset_ptr, dst, count, this, IsLittleEndian,
57 Data.data());
58}
59
60
61uint16_t DataExtractor::getU16(uint32_t *offset_ptr) const {
62 return getU<uint16_t>(offset_ptr, this, IsLittleEndian, Data.data());
63}
64
65uint16_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,
68 Data.data());
69}
70
Wolfgang Pieb258927e2017-06-21 19:37:44 +000071uint32_t DataExtractor::getU24(uint32_t *offset_ptr) const {
72 uint24_t ExtractedVal =
73 getU<uint24_t>(offset_ptr, this, IsLittleEndian, Data.data());
74 // The 3 bytes are in the correct byte order for the host.
75 return ExtractedVal.getAsUint32(sys::IsLittleEndianHost);
76}
77
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +000078uint32_t DataExtractor::getU32(uint32_t *offset_ptr) const {
79 return getU<uint32_t>(offset_ptr, this, IsLittleEndian, Data.data());
80}
81
82uint32_t *DataExtractor::getU32(uint32_t *offset_ptr, uint32_t *dst,
83 uint32_t count) const {
84 return getUs<uint32_t>(offset_ptr, dst, count, this, IsLittleEndian,
Chad Rosier5dfe6da2012-02-22 17:25:00 +000085 Data.data());
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +000086}
87
88uint64_t DataExtractor::getU64(uint32_t *offset_ptr) const {
89 return getU<uint64_t>(offset_ptr, this, IsLittleEndian, Data.data());
90}
91
92uint64_t *DataExtractor::getU64(uint32_t *offset_ptr, uint64_t *dst,
93 uint32_t count) const {
94 return getUs<uint64_t>(offset_ptr, dst, count, this, IsLittleEndian,
95 Data.data());
96}
97
98uint64_t
99DataExtractor::getUnsigned(uint32_t *offset_ptr, uint32_t byte_size) const {
100 switch (byte_size) {
101 case 1:
102 return getU8(offset_ptr);
103 case 2:
104 return getU16(offset_ptr);
105 case 4:
106 return getU32(offset_ptr);
107 case 8:
108 return getU64(offset_ptr);
109 }
110 llvm_unreachable("getUnsigned unhandled case!");
111}
112
113int64_t
114DataExtractor::getSigned(uint32_t *offset_ptr, uint32_t byte_size) const {
115 switch (byte_size) {
116 case 1:
117 return (int8_t)getU8(offset_ptr);
118 case 2:
119 return (int16_t)getU16(offset_ptr);
120 case 4:
121 return (int32_t)getU32(offset_ptr);
122 case 8:
123 return (int64_t)getU64(offset_ptr);
124 }
125 llvm_unreachable("getSigned unhandled case!");
126}
127
128const char *DataExtractor::getCStr(uint32_t *offset_ptr) const {
129 uint32_t offset = *offset_ptr;
130 StringRef::size_type pos = Data.find('\0', offset);
131 if (pos != StringRef::npos) {
132 *offset_ptr = pos + 1;
133 return Data.data() + offset;
134 }
Craig Topperc10719f2014-04-07 04:17:22 +0000135 return nullptr;
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +0000136}
137
Paul Robinsonba1c9152017-05-02 17:37:32 +0000138StringRef DataExtractor::getCStrRef(uint32_t *OffsetPtr) const {
139 uint32_t Start = *OffsetPtr;
140 StringRef::size_type Pos = Data.find('\0', Start);
141 if (Pos != StringRef::npos) {
142 *OffsetPtr = Pos + 1;
143 return StringRef(Data.data() + Start, Pos - Start);
144 }
145 return StringRef();
146}
147
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +0000148uint64_t DataExtractor::getULEB128(uint32_t *offset_ptr) const {
David Blaikie8242f352019-06-24 20:43:36 +0000149 assert(*offset_ptr <= Data.size());
150
151 const char *error;
152 unsigned bytes_read;
153 uint64_t result = decodeULEB128(
154 reinterpret_cast<const uint8_t *>(Data.data() + *offset_ptr), &bytes_read,
155 reinterpret_cast<const uint8_t *>(Data.data() + Data.size()), &error);
156 if (error)
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +0000157 return 0;
David Blaikie8242f352019-06-24 20:43:36 +0000158 *offset_ptr += bytes_read;
159 return result;
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +0000160}
161
162int64_t DataExtractor::getSLEB128(uint32_t *offset_ptr) const {
David Blaikief895e1b2019-06-24 23:45:18 +0000163 assert(*offset_ptr <= Data.size());
164
165 const char *error;
166 unsigned bytes_read;
167 int64_t result = decodeSLEB128(
168 reinterpret_cast<const uint8_t *>(Data.data() + *offset_ptr), &bytes_read,
169 reinterpret_cast<const uint8_t *>(Data.data() + Data.size()), &error);
170 if (error)
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +0000171 return 0;
David Blaikief895e1b2019-06-24 23:45:18 +0000172 *offset_ptr += bytes_read;
173 return result;
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +0000174}