blob: 18e1423b54637027288363fba82e0557e8fc2ecd [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"
13using namespace llvm;
14
15template <typename T>
16static T getU(uint32_t *offset_ptr, const DataExtractor *de,
17 bool isLittleEndian, const char *Data) {
18 T val = 0;
19 uint32_t offset = *offset_ptr;
20 if (de->isValidOffsetForDataOfSize(offset, sizeof(val))) {
21 std::memcpy(&val, &Data[offset], sizeof(val));
Rafael Espindola41cb64f2013-04-15 14:44:24 +000022 if (sys::IsLittleEndianHost != isLittleEndian)
Artyom Skrobov9aea8432014-06-14 13:18:07 +000023 sys::swapByteOrder(val);
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +000024
25 // Advance the offset
26 *offset_ptr += sizeof(val);
27 }
28 return val;
29}
30
31template <typename T>
32static T *getUs(uint32_t *offset_ptr, T *dst, uint32_t count,
33 const DataExtractor *de, bool isLittleEndian, const char *Data){
34 uint32_t offset = *offset_ptr;
35
36 if (count > 0 && de->isValidOffsetForDataOfSize(offset, sizeof(*dst)*count)) {
37 for (T *value_ptr = dst, *end = dst + count; value_ptr != end;
38 ++value_ptr, offset += sizeof(*dst))
39 *value_ptr = getU<T>(offset_ptr, de, isLittleEndian, Data);
40 // Advance the offset
41 *offset_ptr = offset;
42 // Return a non-NULL pointer to the converted data as an indicator of
43 // success
44 return dst;
45 }
Craig Topperc10719f2014-04-07 04:17:22 +000046 return nullptr;
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +000047}
48
49uint8_t DataExtractor::getU8(uint32_t *offset_ptr) const {
50 return getU<uint8_t>(offset_ptr, this, IsLittleEndian, Data.data());
51}
52
53uint8_t *
54DataExtractor::getU8(uint32_t *offset_ptr, uint8_t *dst, uint32_t count) const {
55 return getUs<uint8_t>(offset_ptr, dst, count, this, IsLittleEndian,
56 Data.data());
57}
58
59
60uint16_t DataExtractor::getU16(uint32_t *offset_ptr) const {
61 return getU<uint16_t>(offset_ptr, this, IsLittleEndian, Data.data());
62}
63
64uint16_t *DataExtractor::getU16(uint32_t *offset_ptr, uint16_t *dst,
65 uint32_t count) const {
66 return getUs<uint16_t>(offset_ptr, dst, count, this, IsLittleEndian,
67 Data.data());
68}
69
Wolfgang Pieb258927e2017-06-21 19:37:44 +000070uint32_t DataExtractor::getU24(uint32_t *offset_ptr) const {
71 uint24_t ExtractedVal =
72 getU<uint24_t>(offset_ptr, this, IsLittleEndian, Data.data());
73 // The 3 bytes are in the correct byte order for the host.
74 return ExtractedVal.getAsUint32(sys::IsLittleEndianHost);
75}
76
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +000077uint32_t DataExtractor::getU32(uint32_t *offset_ptr) const {
78 return getU<uint32_t>(offset_ptr, this, IsLittleEndian, Data.data());
79}
80
81uint32_t *DataExtractor::getU32(uint32_t *offset_ptr, uint32_t *dst,
82 uint32_t count) const {
83 return getUs<uint32_t>(offset_ptr, dst, count, this, IsLittleEndian,
Chad Rosier5dfe6da2012-02-22 17:25:00 +000084 Data.data());
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +000085}
86
87uint64_t DataExtractor::getU64(uint32_t *offset_ptr) const {
88 return getU<uint64_t>(offset_ptr, this, IsLittleEndian, Data.data());
89}
90
91uint64_t *DataExtractor::getU64(uint32_t *offset_ptr, uint64_t *dst,
92 uint32_t count) const {
93 return getUs<uint64_t>(offset_ptr, dst, count, this, IsLittleEndian,
94 Data.data());
95}
96
97uint64_t
98DataExtractor::getUnsigned(uint32_t *offset_ptr, uint32_t byte_size) const {
99 switch (byte_size) {
100 case 1:
101 return getU8(offset_ptr);
102 case 2:
103 return getU16(offset_ptr);
104 case 4:
105 return getU32(offset_ptr);
106 case 8:
107 return getU64(offset_ptr);
108 }
109 llvm_unreachable("getUnsigned unhandled case!");
110}
111
112int64_t
113DataExtractor::getSigned(uint32_t *offset_ptr, uint32_t byte_size) const {
114 switch (byte_size) {
115 case 1:
116 return (int8_t)getU8(offset_ptr);
117 case 2:
118 return (int16_t)getU16(offset_ptr);
119 case 4:
120 return (int32_t)getU32(offset_ptr);
121 case 8:
122 return (int64_t)getU64(offset_ptr);
123 }
124 llvm_unreachable("getSigned unhandled case!");
125}
126
127const char *DataExtractor::getCStr(uint32_t *offset_ptr) const {
128 uint32_t offset = *offset_ptr;
129 StringRef::size_type pos = Data.find('\0', offset);
130 if (pos != StringRef::npos) {
131 *offset_ptr = pos + 1;
132 return Data.data() + offset;
133 }
Craig Topperc10719f2014-04-07 04:17:22 +0000134 return nullptr;
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +0000135}
136
Paul Robinsonba1c9152017-05-02 17:37:32 +0000137StringRef DataExtractor::getCStrRef(uint32_t *OffsetPtr) const {
138 uint32_t Start = *OffsetPtr;
139 StringRef::size_type Pos = Data.find('\0', Start);
140 if (Pos != StringRef::npos) {
141 *OffsetPtr = Pos + 1;
142 return StringRef(Data.data() + Start, Pos - Start);
143 }
144 return StringRef();
145}
146
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +0000147uint64_t DataExtractor::getULEB128(uint32_t *offset_ptr) const {
148 uint64_t result = 0;
149 if (Data.empty())
150 return 0;
151
152 unsigned shift = 0;
153 uint32_t offset = *offset_ptr;
154 uint8_t byte = 0;
155
156 while (isValidOffset(offset)) {
157 byte = Data[offset++];
Benjamin Kramer1b07ab52012-08-20 10:52:11 +0000158 result |= uint64_t(byte & 0x7f) << shift;
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +0000159 shift += 7;
Pavel Labathbb6d0b82019-06-24 09:11:24 +0000160 if ((byte & 0x80) == 0) {
161 *offset_ptr = offset;
162 return result;
163 }
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +0000164 }
Pavel Labathbb6d0b82019-06-24 09:11:24 +0000165 return 0;
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +0000166}
167
168int64_t DataExtractor::getSLEB128(uint32_t *offset_ptr) const {
169 int64_t result = 0;
170 if (Data.empty())
171 return 0;
172
173 unsigned shift = 0;
174 uint32_t offset = *offset_ptr;
175 uint8_t byte = 0;
176
177 while (isValidOffset(offset)) {
178 byte = Data[offset++];
Benjamin Kramer1b07ab52012-08-20 10:52:11 +0000179 result |= uint64_t(byte & 0x7f) << shift;
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +0000180 shift += 7;
Pavel Labathbb6d0b82019-06-24 09:11:24 +0000181 if ((byte & 0x80) == 0) {
182 // Sign bit of byte is 2nd high order bit (0x40)
183 if (shift < 64 && (byte & 0x40))
184 result |= -(1ULL << shift);
185
186 *offset_ptr = offset;
187 return result;
188 }
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +0000189 }
Pavel Labathbb6d0b82019-06-24 09:11:24 +0000190 return 0;
Benjamin Kramer88a1d9f2011-09-13 19:42:16 +0000191}