1 //===-- DataExtractor.cpp -------------------------------------------------===// 2 // 3 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/Support/DataExtractor.h" 10 #include "llvm/Support/Errc.h" 11 #include "llvm/Support/ErrorHandling.h" 12 #include "llvm/Support/Host.h" 13 #include "llvm/Support/LEB128.h" 14 #include "llvm/Support/SwapByteOrder.h" 15 16 using namespace llvm; 17 18 static void unexpectedEndReached(Error *E, uint64_t Offset) { 19 if (E) 20 *E = createStringError(errc::illegal_byte_sequence, 21 "unexpected end of data at offset 0x%" PRIx64, 22 Offset); 23 } 24 25 static bool isError(Error *E) { return E && *E; } 26 27 template <typename T> 28 static T getU(uint64_t *offset_ptr, const DataExtractor *de, 29 bool isLittleEndian, const char *Data, llvm::Error *Err) { 30 ErrorAsOutParameter ErrAsOut(Err); 31 T val = 0; 32 if (isError(Err)) 33 return val; 34 35 uint64_t offset = *offset_ptr; 36 if (!de->isValidOffsetForDataOfSize(offset, sizeof(T))) { 37 unexpectedEndReached(Err, offset); 38 return val; 39 } 40 std::memcpy(&val, &Data[offset], sizeof(val)); 41 if (sys::IsLittleEndianHost != isLittleEndian) 42 sys::swapByteOrder(val); 43 44 // Advance the offset 45 *offset_ptr += sizeof(val); 46 return val; 47 } 48 49 template <typename T> 50 static T *getUs(uint64_t *offset_ptr, T *dst, uint32_t count, 51 const DataExtractor *de, bool isLittleEndian, const char *Data, 52 llvm::Error *Err) { 53 ErrorAsOutParameter ErrAsOut(Err); 54 if (isError(Err)) 55 return nullptr; 56 57 uint64_t offset = *offset_ptr; 58 59 if (!de->isValidOffsetForDataOfSize(offset, sizeof(*dst) * count)) { 60 unexpectedEndReached(Err, offset); 61 return nullptr; 62 } 63 for (T *value_ptr = dst, *end = dst + count; value_ptr != end; 64 ++value_ptr, offset += sizeof(*dst)) 65 *value_ptr = getU<T>(offset_ptr, de, isLittleEndian, Data, Err); 66 // Advance the offset 67 *offset_ptr = offset; 68 // Return a non-NULL pointer to the converted data as an indicator of 69 // success 70 return dst; 71 } 72 73 uint8_t DataExtractor::getU8(uint64_t *offset_ptr, llvm::Error *Err) const { 74 return getU<uint8_t>(offset_ptr, this, IsLittleEndian, Data.data(), Err); 75 } 76 77 uint8_t * 78 DataExtractor::getU8(uint64_t *offset_ptr, uint8_t *dst, uint32_t count) const { 79 return getUs<uint8_t>(offset_ptr, dst, count, this, IsLittleEndian, 80 Data.data(), nullptr); 81 } 82 83 uint8_t *DataExtractor::getU8(Cursor &C, uint8_t *Dst, uint32_t Count) const { 84 return getUs<uint8_t>(&C.Offset, Dst, Count, this, IsLittleEndian, 85 Data.data(), &C.Err); 86 } 87 88 uint16_t DataExtractor::getU16(uint64_t *offset_ptr, llvm::Error *Err) const { 89 return getU<uint16_t>(offset_ptr, this, IsLittleEndian, Data.data(), Err); 90 } 91 92 uint16_t *DataExtractor::getU16(uint64_t *offset_ptr, uint16_t *dst, 93 uint32_t count) const { 94 return getUs<uint16_t>(offset_ptr, dst, count, this, IsLittleEndian, 95 Data.data(), nullptr); 96 } 97 98 uint32_t DataExtractor::getU24(uint64_t *offset_ptr) const { 99 uint24_t ExtractedVal = 100 getU<uint24_t>(offset_ptr, this, IsLittleEndian, Data.data(), nullptr); 101 // The 3 bytes are in the correct byte order for the host. 102 return ExtractedVal.getAsUint32(sys::IsLittleEndianHost); 103 } 104 105 uint32_t DataExtractor::getU32(uint64_t *offset_ptr, llvm::Error *Err) const { 106 return getU<uint32_t>(offset_ptr, this, IsLittleEndian, Data.data(), Err); 107 } 108 109 uint32_t *DataExtractor::getU32(uint64_t *offset_ptr, uint32_t *dst, 110 uint32_t count) const { 111 return getUs<uint32_t>(offset_ptr, dst, count, this, IsLittleEndian, 112 Data.data(), nullptr); 113 } 114 115 uint64_t DataExtractor::getU64(uint64_t *offset_ptr, llvm::Error *Err) const { 116 return getU<uint64_t>(offset_ptr, this, IsLittleEndian, Data.data(), Err); 117 } 118 119 uint64_t *DataExtractor::getU64(uint64_t *offset_ptr, uint64_t *dst, 120 uint32_t count) const { 121 return getUs<uint64_t>(offset_ptr, dst, count, this, IsLittleEndian, 122 Data.data(), nullptr); 123 } 124 125 uint64_t DataExtractor::getUnsigned(uint64_t *offset_ptr, uint32_t byte_size, 126 llvm::Error *Err) const { 127 switch (byte_size) { 128 case 1: 129 return getU8(offset_ptr, Err); 130 case 2: 131 return getU16(offset_ptr, Err); 132 case 4: 133 return getU32(offset_ptr, Err); 134 case 8: 135 return getU64(offset_ptr, Err); 136 } 137 llvm_unreachable("getUnsigned unhandled case!"); 138 } 139 140 int64_t 141 DataExtractor::getSigned(uint64_t *offset_ptr, uint32_t byte_size) const { 142 switch (byte_size) { 143 case 1: 144 return (int8_t)getU8(offset_ptr); 145 case 2: 146 return (int16_t)getU16(offset_ptr); 147 case 4: 148 return (int32_t)getU32(offset_ptr); 149 case 8: 150 return (int64_t)getU64(offset_ptr); 151 } 152 llvm_unreachable("getSigned unhandled case!"); 153 } 154 155 const char *DataExtractor::getCStr(uint64_t *offset_ptr) const { 156 uint64_t offset = *offset_ptr; 157 StringRef::size_type pos = Data.find('\0', offset); 158 if (pos != StringRef::npos) { 159 *offset_ptr = pos + 1; 160 return Data.data() + offset; 161 } 162 return nullptr; 163 } 164 165 StringRef DataExtractor::getCStrRef(uint64_t *offset_ptr) const { 166 uint64_t Start = *offset_ptr; 167 StringRef::size_type Pos = Data.find('\0', Start); 168 if (Pos != StringRef::npos) { 169 *offset_ptr = Pos + 1; 170 return StringRef(Data.data() + Start, Pos - Start); 171 } 172 return StringRef(); 173 } 174 175 StringRef DataExtractor::getFixedLengthString(uint64_t *OffsetPtr, 176 uint64_t Length, 177 StringRef TrimChars) const { 178 StringRef Bytes(getBytes(OffsetPtr, Length)); 179 return Bytes.trim(TrimChars); 180 } 181 182 StringRef DataExtractor::getBytes(uint64_t *OffsetPtr, uint64_t Length) const { 183 if (!isValidOffsetForDataOfSize(*OffsetPtr, Length)) 184 return StringRef(); 185 StringRef Result = Data.substr(*OffsetPtr, Length); 186 *OffsetPtr += Length; 187 return Result; 188 } 189 190 uint64_t DataExtractor::getULEB128(uint64_t *offset_ptr, 191 llvm::Error *Err) const { 192 assert(*offset_ptr <= Data.size()); 193 ErrorAsOutParameter ErrAsOut(Err); 194 if (isError(Err)) 195 return 0; 196 197 const char *error; 198 unsigned bytes_read; 199 uint64_t result = decodeULEB128( 200 reinterpret_cast<const uint8_t *>(Data.data() + *offset_ptr), &bytes_read, 201 reinterpret_cast<const uint8_t *>(Data.data() + Data.size()), &error); 202 if (error) { 203 if (Err) 204 *Err = createStringError(errc::illegal_byte_sequence, error); 205 return 0; 206 } 207 *offset_ptr += bytes_read; 208 return result; 209 } 210 211 int64_t DataExtractor::getSLEB128(uint64_t *offset_ptr) const { 212 assert(*offset_ptr <= Data.size()); 213 214 const char *error; 215 unsigned bytes_read; 216 int64_t result = decodeSLEB128( 217 reinterpret_cast<const uint8_t *>(Data.data() + *offset_ptr), &bytes_read, 218 reinterpret_cast<const uint8_t *>(Data.data() + Data.size()), &error); 219 if (error) 220 return 0; 221 *offset_ptr += bytes_read; 222 return result; 223 } 224 225 void DataExtractor::skip(Cursor &C, uint64_t Length) const { 226 ErrorAsOutParameter ErrAsOut(&C.Err); 227 if (isError(&C.Err)) 228 return; 229 230 if (isValidOffsetForDataOfSize(C.Offset, Length)) 231 C.Offset += Length; 232 else 233 unexpectedEndReached(&C.Err, C.Offset); 234 } 235