Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
DataExtractor.cpp
Go to the documentation of this file.
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/ADT/StringExtras.h"
11#include "llvm/Support/Errc.h"
12#include "llvm/Support/ErrorHandling.h"
13#include "llvm/Support/LEB128.h"
14#include "llvm/Support/SwapByteOrder.h"
15
16using namespacellvm;
17
18bool DataExtractor::prepareRead(uint64_tOffset,uint64_tSize,
19Error *E) const{
20if (isValidOffsetForDataOfSize(Offset,Size))
21returntrue;
22if (E) {
23if (Offset <= Data.size())
24 *E =createStringError(
25errc::illegal_byte_sequence,
26"unexpected end of data at offset 0x%zx while reading [0x%" PRIx64
27", 0x%" PRIx64")",
28 Data.size(),Offset,Offset +Size);
29else
30 *E =createStringError(errc::invalid_argument,
31"offset 0x%" PRIx64
32" is beyond the end of data at 0x%zx",
33Offset, Data.size());
34 }
35returnfalse;
36}
37
38staticboolisError(Error *E) {return E && *E; }
39
40template <typename T>
41T DataExtractor::getU(uint64_t *offset_ptr,Error *Err) const{
42ErrorAsOutParameter ErrAsOut(Err);
43T val = 0;
44if (isError(Err))
45return val;
46
47uint64_t offset = *offset_ptr;
48if (!prepareRead(offset,sizeof(T), Err))
49return val;
50 std::memcpy(&val, &Data.data()[offset],sizeof(val));
51if (sys::IsLittleEndianHost != IsLittleEndian)
52sys::swapByteOrder(val);
53
54// Advance the offset
55 *offset_ptr +=sizeof(val);
56return val;
57}
58
59template <typename T>
60T *DataExtractor::getUs(uint64_t *offset_ptr,T *dst,uint32_tcount,
61Error *Err) const{
62ErrorAsOutParameter ErrAsOut(Err);
63if (isError(Err))
64returnnullptr;
65
66uint64_t offset = *offset_ptr;
67
68if (!prepareRead(offset,sizeof(*dst) *count, Err))
69returnnullptr;
70for (T *value_ptr = dst, *end = dst +count; value_ptr !=end;
71 ++value_ptr, offset +=sizeof(*dst))
72 *value_ptr = getU<T>(offset_ptr, Err);
73// Advance the offset
74 *offset_ptr = offset;
75// Return a non-NULL pointer to the converted data as an indicator of
76// success
77return dst;
78}
79
80uint8_tDataExtractor::getU8(uint64_t *offset_ptr,llvm::Error *Err) const{
81return getU<uint8_t>(offset_ptr, Err);
82}
83
84uint8_t *DataExtractor::getU8(uint64_t *offset_ptr,uint8_t *dst,
85uint32_tcount) const{
86return getUs<uint8_t>(offset_ptr, dst,count,nullptr);
87}
88
89uint8_t *DataExtractor::getU8(Cursor &C,uint8_t *Dst,uint32_t Count) const{
90return getUs<uint8_t>(&C.Offset, Dst, Count, &C.Err);
91}
92
93uint16_tDataExtractor::getU16(uint64_t *offset_ptr,llvm::Error *Err) const{
94return getU<uint16_t>(offset_ptr, Err);
95}
96
97uint16_t *DataExtractor::getU16(uint64_t *offset_ptr,uint16_t *dst,
98uint32_tcount) const{
99return getUs<uint16_t>(offset_ptr, dst,count,nullptr);
100}
101
102uint32_tDataExtractor::getU24(uint64_t *OffsetPtr,Error *Err) const{
103uint24_t ExtractedVal = getU<uint24_t>(OffsetPtr, Err);
104// The 3 bytes are in the correct byte order for the host.
105return ExtractedVal.getAsUint32(sys::IsLittleEndianHost);
106}
107
108uint32_tDataExtractor::getU32(uint64_t *offset_ptr,llvm::Error *Err) const{
109return getU<uint32_t>(offset_ptr, Err);
110}
111
112uint32_t *DataExtractor::getU32(uint64_t *offset_ptr,uint32_t *dst,
113uint32_tcount) const{
114return getUs<uint32_t>(offset_ptr, dst,count,nullptr);
115}
116
117uint64_tDataExtractor::getU64(uint64_t *offset_ptr,llvm::Error *Err) const{
118return getU<uint64_t>(offset_ptr, Err);
119}
120
121uint64_t *DataExtractor::getU64(uint64_t *offset_ptr,uint64_t *dst,
122uint32_tcount) const{
123return getUs<uint64_t>(offset_ptr, dst,count,nullptr);
124}
125
126uint64_tDataExtractor::getUnsigned(uint64_t *offset_ptr,uint32_t byte_size,
127llvm::Error *Err) const{
128switch (byte_size) {
129case 1:
130returngetU8(offset_ptr, Err);
131case 2:
132returngetU16(offset_ptr, Err);
133case 4:
134returngetU32(offset_ptr, Err);
135case 8:
136returngetU64(offset_ptr, Err);
137 }
138llvm_unreachable("getUnsigned unhandled case!");
139}
140
141int64_t
142DataExtractor::getSigned(uint64_t *offset_ptr,uint32_t byte_size) const{
143switch (byte_size) {
144case 1:
145return (int8_t)getU8(offset_ptr);
146case 2:
147return (int16_t)getU16(offset_ptr);
148case 4:
149return (int32_t)getU32(offset_ptr);
150case 8:
151return (int64_t)getU64(offset_ptr);
152 }
153llvm_unreachable("getSigned unhandled case!");
154}
155
156StringRefDataExtractor::getCStrRef(uint64_t *OffsetPtr,Error *Err) const{
157ErrorAsOutParameter ErrAsOut(Err);
158if (isError(Err))
159returnStringRef();
160
161uint64_t Start = *OffsetPtr;
162StringRef::size_type Pos = Data.find('\0', Start);
163if (Pos !=StringRef::npos) {
164 *OffsetPtr = Pos + 1;
165returnStringRef(Data.data() + Start, Pos - Start);
166 }
167if (Err)
168 *Err =createStringError(errc::illegal_byte_sequence,
169"no null terminated string at offset 0x%" PRIx64,
170 Start);
171returnStringRef();
172}
173
174StringRefDataExtractor::getFixedLengthString(uint64_t *OffsetPtr,
175uint64_tLength,
176StringRef TrimChars) const{
177StringRef Bytes(getBytes(OffsetPtr,Length));
178return Bytes.trim(TrimChars);
179}
180
181StringRefDataExtractor::getBytes(uint64_t *OffsetPtr,uint64_tLength,
182Error *Err) const{
183ErrorAsOutParameter ErrAsOut(Err);
184if (isError(Err))
185returnStringRef();
186
187if (!prepareRead(*OffsetPtr,Length, Err))
188returnStringRef();
189
190StringRef Result = Data.substr(*OffsetPtr,Length);
191 *OffsetPtr +=Length;
192return Result;
193}
194
195template <typename T>
196staticTgetLEB128(StringRefData,uint64_t *OffsetPtr,Error *Err,
197T (&Decoder)(constuint8_t *p,unsigned *n,
198constuint8_t *end,constchar **error)) {
199ArrayRef<uint8_t> Bytes = arrayRefFromStringRef(Data);
200assert(*OffsetPtr <= Bytes.size());
201ErrorAsOutParameter ErrAsOut(Err);
202if (isError(Err))
203returnT();
204
205constchar *error =nullptr;
206unsigned bytes_read;
207T result =
208 Decoder(Bytes.data() + *OffsetPtr, &bytes_read, Bytes.end(), &error);
209if (error) {
210if (Err)
211 *Err =createStringError(errc::illegal_byte_sequence,
212"unable to decode LEB128 at offset 0x%8.8" PRIx64
213": %s",
214 *OffsetPtr,error);
215returnT();
216 }
217 *OffsetPtr += bytes_read;
218return result;
219}
220
221uint64_tDataExtractor::getULEB128(uint64_t *offset_ptr,Error *Err) const{
222returngetLEB128(Data, offset_ptr, Err,decodeULEB128);
223}
224
225int64_tDataExtractor::getSLEB128(uint64_t *offset_ptr,Error *Err) const{
226returngetLEB128(Data, offset_ptr, Err,decodeSLEB128);
227}
228
229voidDataExtractor::skip(Cursor &C,uint64_tLength) const{
230ErrorAsOutParameter ErrAsOut(C.Err);
231if (isError(&C.Err))
232return;
233
234if (prepareRead(C.Offset,Length, &C.Err))
235C.Offset +=Length;
236}
getLEB128
static T getLEB128(StringRef Data, uint64_t *OffsetPtr, Error *Err, T(&Decoder)(const uint8_t *p, unsigned *n, const uint8_t *end, const char **error))
Definition:DataExtractor.cpp:196
isError
static bool isError(Error *E)
Definition:DataExtractor.cpp:38
DataExtractor.h
Size
uint64_t Size
Definition:ELFObjHandler.cpp:81
Errc.h
LEB128.h
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
StringExtras.h
This file contains some functions that are useful when dealing with strings.
SwapByteOrder.h
error
#define error(X)
Definition:SymbolRecordMapping.cpp:14
T
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition:ArrayRef.h:41
llvm::ArrayRef::end
iterator end() const
Definition:ArrayRef.h:157
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition:ArrayRef.h:168
llvm::ArrayRef::data
const T * data() const
Definition:ArrayRef.h:165
llvm::DataExtractor::Cursor
A class representing a position in a DataExtractor, as well as any error encountered during extractio...
Definition:DataExtractor.h:54
llvm::DataExtractor::getFixedLengthString
StringRef getFixedLengthString(uint64_t *OffsetPtr, uint64_t Length, StringRef TrimChars={"\0", 1}) const
Extract a fixed length string from *OffsetPtr and consume Length bytes.
Definition:DataExtractor.cpp:174
llvm::DataExtractor::getUnsigned
uint64_t getUnsigned(uint64_t *offset_ptr, uint32_t byte_size, Error *Err=nullptr) const
Extract an unsigned integer of size byte_size from *offset_ptr.
Definition:DataExtractor.cpp:126
llvm::DataExtractor::getU32
uint32_t getU32(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint32_t value from *offset_ptr.
Definition:DataExtractor.cpp:108
llvm::DataExtractor::getCStrRef
StringRef getCStrRef(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a C string from *offset_ptr.
Definition:DataExtractor.cpp:156
llvm::DataExtractor::getU8
uint8_t getU8(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint8_t value from *offset_ptr.
Definition:DataExtractor.cpp:80
llvm::DataExtractor::getSigned
int64_t getSigned(uint64_t *offset_ptr, uint32_t size) const
Extract an signed integer of size byte_size from *offset_ptr.
Definition:DataExtractor.cpp:142
llvm::DataExtractor::getULEB128
uint64_t getULEB128(uint64_t *offset_ptr, llvm::Error *Err=nullptr) const
Extract a unsigned LEB128 value from *offset_ptr.
Definition:DataExtractor.cpp:221
llvm::DataExtractor::getSLEB128
int64_t getSLEB128(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a signed LEB128 value from *offset_ptr.
Definition:DataExtractor.cpp:225
llvm::DataExtractor::getU16
uint16_t getU16(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint16_t value from *offset_ptr.
Definition:DataExtractor.cpp:93
llvm::DataExtractor::skip
void skip(Cursor &C, uint64_t Length) const
Advance the Cursor position by the given number of bytes.
Definition:DataExtractor.cpp:229
llvm::DataExtractor::getU64
uint64_t getU64(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint64_t value from *offset_ptr.
Definition:DataExtractor.cpp:117
llvm::DataExtractor::isValidOffsetForDataOfSize
bool isValidOffsetForDataOfSize(uint64_t offset, uint64_t length) const
Test the availability of length bytes of data from offset.
Definition:DataExtractor.h:672
llvm::DataExtractor::getBytes
StringRef getBytes(uint64_t *OffsetPtr, uint64_t Length, Error *Err=nullptr) const
Extract a fixed number of bytes from the specified offset.
Definition:DataExtractor.cpp:181
llvm::DataExtractor::getU24
uint32_t getU24(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a 24-bit unsigned value from *offset_ptr and return it in a uint32_t.
Definition:DataExtractor.cpp:102
llvm::ErrorAsOutParameter
Helper for Errors used as out-parameters.
Definition:Error.h:1130
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition:Error.h:160
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::StringRef::substr
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition:StringRef.h:571
llvm::StringRef::size_type
size_t size_type
Definition:StringRef.h:57
llvm::StringRef::size
constexpr size_t size() const
size - Get the string size.
Definition:StringRef.h:150
llvm::StringRef::data
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition:StringRef.h:144
llvm::StringRef::find
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition:StringRef.h:297
llvm::StringRef::trim
StringRef trim(char Char) const
Return string with consecutive Char characters starting from the left and right removed.
Definition:StringRef.h:815
llvm::StringRef::npos
static constexpr size_t npos
Definition:StringRef.h:53
uint16_t
uint32_t
uint64_t
uint8_t
ErrorHandling.h
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition:ErrorHandling.h:143
llvm::CallingConv::C
@ C
The default llvm calling convention, compatible with C.
Definition:CallingConv.h:34
llvm::sys::path::end
const_iterator end(StringRef path LLVM_LIFETIME_BOUND)
Get end iterator over path.
Definition:Path.cpp:235
llvm::sys::IsLittleEndianHost
static const bool IsLittleEndianHost
Definition:SwapByteOrder.h:29
llvm::sys::swapByteOrder
void swapByteOrder(T &Value)
Definition:SwapByteOrder.h:61
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::Offset
@ Offset
Definition:DWP.cpp:480
llvm::Length
@ Length
Definition:DWP.cpp:480
llvm::decodeULEB128
uint64_t decodeULEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a ULEB128 value.
Definition:LEB128.h:131
llvm::decodeSLEB128
int64_t decodeSLEB128(const uint8_t *p, unsigned *n=nullptr, const uint8_t *end=nullptr, const char **error=nullptr)
Utility function to decode a SLEB128 value.
Definition:LEB128.h:165
llvm::createStringError
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition:Error.h:1291
llvm::errc::illegal_byte_sequence
@ illegal_byte_sequence
llvm::errc::invalid_argument
@ invalid_argument
llvm::count
auto count(R &&Range, const E &Element)
Wrapper function around std::count to count the number of times an element Element occurs in the give...
Definition:STLExtras.h:1938
llvm::Data
@ Data
Definition:SIMachineScheduler.h:55
llvm::Uint24
An auxiliary type to facilitate extraction of 3-byte entities.
Definition:DataExtractor.h:19
llvm::Uint24::getAsUint32
uint32_t getAsUint32(bool IsLittleEndian) const
Definition:DataExtractor.h:27

Generated on Thu Jul 17 2025 12:38:06 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp