LLVM: include/llvm/Support/DataExtractor.h Source File

LLVM 22.0.0git
DataExtractor.h
Go to the documentation of this file.
1//===-- DataExtractor.h -----------------------------------------*- C++ -*-===//
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#ifndef LLVM_SUPPORT_DATAEXTRACTOR_H
10#define LLVM_SUPPORT_DATAEXTRACTOR_H
11
12#include "llvm/ADT/StringRef.h"
13#include "llvm/Support/Compiler.h"
14#include "llvm/Support/DataTypes.h"
15#include "llvm/Support/Error.h"
16
17namespace llvm {
18
19/// An auxiliary type to facilitate extraction of 3-byte entities.
20 struct Uint24 {
22 Uint24(uint8_t U) : Bytes{U, U, U} {}
23 Uint24(uint8_t U0, uint8_t U1, uint8_t U2) : Bytes{U0, U1, U2} {}
24 uint32_t getAsUint32(bool IsLittleEndian) const {
25 int LoIx = IsLittleEndian ? 0 : 2;
26 return Bytes[LoIx] + (Bytes[1] << 8) + (Bytes[2-LoIx] << 16);
27 }
28};
29
30 using uint24_t = Uint24;
31static_assert(sizeof(uint24_t) == 3, "sizeof(uint24_t) != 3");
32
33/// Needed by swapByteOrder().
35 return uint24_t(C.Bytes[2], C.Bytes[1], C.Bytes[0]);
36}
37
39 StringRef Data;
40 uint8_t IsLittleEndian;
41 uint8_t AddressSize;
42public:
43 /// A class representing a position in a DataExtractor, as well as any error
44 /// encountered during extraction. It enables one to extract a sequence of
45 /// values without error-checking and then checking for errors in bulk at the
46 /// end. The class holds an Error object, so failing to check the result of
47 /// the parse will result in a runtime error. The error flag is sticky and
48 /// will cause all subsequent extraction functions to fail without even
49 /// attempting to parse and without updating the Cursor offset. After clearing
50 /// the error flag, one can again use the Cursor object for parsing.
51 class Cursor {
52 uint64_t Offset;
53 Error Err;
54
55 friend class DataExtractor;
56
57 public:
58 /// Construct a cursor for extraction from the given offset.
59 explicit Cursor(uint64_t Offset) : Offset(Offset), Err(Error::success()) {}
60
61 /// Checks whether the cursor is valid (i.e. no errors were encountered). In
62 /// case of errors, this does not clear the error flag -- one must call
63 /// takeError() instead.
64 explicit operator bool() { return !Err; }
65
66 /// Return the current position of this Cursor. In the error state this is
67 /// the position of the Cursor before the first error was encountered.
68 uint64_t tell() const { return Offset; }
69
70 /// Set the cursor to the new offset. This does not impact the error state.
71 void seek(uint64_t NewOffSet) { Offset = NewOffSet; }
72
73 /// Return error contained inside this Cursor, if any. Clears the internal
74 /// Cursor state.
75 Error takeError() { return std::move(Err); }
76 };
77
78 /// Construct with a buffer that is owned by the caller.
79 ///
80 /// This constructor allows us to use data that is owned by the
81 /// caller. The data must stay around as long as this object is
82 /// valid.
83 DataExtractor(StringRef Data, bool IsLittleEndian, uint8_t AddressSize)
84 : Data(Data), IsLittleEndian(IsLittleEndian), AddressSize(AddressSize) {}
85 DataExtractor(ArrayRef<uint8_t> Data, bool IsLittleEndian,
86 uint8_t AddressSize)
87 : Data(StringRef(reinterpret_cast<const char *>(Data.data()),
88 Data.size())),
89 IsLittleEndian(IsLittleEndian), AddressSize(AddressSize) {}
90
91 /// Get the data pointed to by this extractor.
92 StringRef getData() const { return Data; }
93 /// Get the endianness for this extractor.
94 bool isLittleEndian() const { return IsLittleEndian; }
95 /// Get the address size for this extractor.
96 uint8_t getAddressSize() const { return AddressSize; }
97 /// Set the address size for this extractor.
98 void setAddressSize(uint8_t Size) { AddressSize = Size; }
99
100 /// Extract a C string from \a *offset_ptr.
101 ///
102 /// Returns a pointer to a C String from the data at the offset
103 /// pointed to by \a offset_ptr. A variable length NULL terminated C
104 /// string will be extracted and the \a offset_ptr will be
105 /// updated with the offset of the byte that follows the NULL
106 /// terminator byte.
107 ///
108 /// @param[in,out] OffsetPtr
109 /// A pointer to an offset within the data that will be advanced
110 /// by the appropriate number of bytes if the value is extracted
111 /// correctly. If the offset is out of bounds or there are not
112 /// enough bytes to extract this value, the offset will be left
113 /// unmodified.
114 ///
115 /// @param[in,out] Err
116 /// A pointer to an Error object. Upon return the Error object is set to
117 /// indicate the result (success/failure) of the function. If the Error
118 /// object is already set when calling this function, no extraction is
119 /// performed.
120 ///
121 /// @return
122 /// A pointer to the C string value in the data. If the offset
123 /// pointed to by \a offset_ptr is out of bounds, or if the
124 /// offset plus the length of the C string is out of bounds,
125 /// NULL will be returned.
126 const char *getCStr(uint64_t *OffsetPtr, Error *Err = nullptr) const {
127 return getCStrRef(OffsetPtr, Err).data();
128 }
129
130 /// Extract a C string from the location given by the cursor. In case of an
131 /// extraction error, or if the cursor is already in an error state, a
132 /// nullptr is returned.
133 const char *getCStr(Cursor &C) const { return getCStrRef(C).data(); }
134
135 /// Extract a C string from \a *offset_ptr.
136 ///
137 /// Returns a StringRef for the C String from the data at the offset
138 /// pointed to by \a offset_ptr. A variable length NULL terminated C
139 /// string will be extracted and the \a offset_ptr will be
140 /// updated with the offset of the byte that follows the NULL
141 /// terminator byte.
142 ///
143 /// \param[in,out] OffsetPtr
144 /// A pointer to an offset within the data that will be advanced
145 /// by the appropriate number of bytes if the value is extracted
146 /// correctly. If the offset is out of bounds or there are not
147 /// enough bytes to extract this value, the offset will be left
148 /// unmodified.
149 ///
150 /// @param[in,out] Err
151 /// A pointer to an Error object. Upon return the Error object is set to
152 /// indicate the result (success/failure) of the function. If the Error
153 /// object is already set when calling this function, no extraction is
154 /// performed.
155 ///
156 /// \return
157 /// A StringRef for the C string value in the data. If the offset
158 /// pointed to by \a offset_ptr is out of bounds, or if the
159 /// offset plus the length of the C string is out of bounds,
160 /// a default-initialized StringRef will be returned.
162 Error *Err = nullptr) const;
163
164 /// Extract a C string (as a StringRef) from the location given by the cursor.
165 /// In case of an extraction error, or if the cursor is already in an error
166 /// state, a default-initialized StringRef is returned.
168 return getCStrRef(&C.Offset, &C.Err);
169 }
170
171 /// Extract a fixed length string from \a *OffsetPtr and consume \a Length
172 /// bytes.
173 ///
174 /// Returns a StringRef for the string from the data at the offset
175 /// pointed to by \a OffsetPtr. A fixed length C string will be extracted
176 /// and the \a OffsetPtr will be advanced by \a Length bytes.
177 ///
178 /// \param[in,out] OffsetPtr
179 /// A pointer to an offset within the data that will be advanced
180 /// by the appropriate number of bytes if the value is extracted
181 /// correctly. If the offset is out of bounds or there are not
182 /// enough bytes to extract this value, the offset will be left
183 /// unmodified.
184 ///
185 /// \param[in] Length
186 /// The length of the fixed length string to extract. If there are not
187 /// enough bytes in the data to extract the full string, the offset will
188 /// be left unmodified.
189 ///
190 /// \param[in] TrimChars
191 /// A set of characters to trim from the end of the string. Fixed length
192 /// strings are commonly either NULL terminated by one or more zero
193 /// bytes. Some clients have one or more spaces at the end of the string,
194 /// but a good default is to trim the NULL characters.
195 ///
196 /// \return
197 /// A StringRef for the C string value in the data. If the offset
198 /// pointed to by \a OffsetPtr is out of bounds, or if the
199 /// offset plus the length of the C string is out of bounds,
200 /// a default-initialized StringRef will be returned.
202 StringRef TrimChars = {"0円",
203 1}) const;
204
205 /// Extract a fixed number of bytes from the specified offset.
206 ///
207 /// Returns a StringRef for the bytes from the data at the offset
208 /// pointed to by \a OffsetPtr. A fixed length C string will be extracted
209 /// and the \a OffsetPtr will be advanced by \a Length bytes.
210 ///
211 /// \param[in,out] OffsetPtr
212 /// A pointer to an offset within the data that will be advanced
213 /// by the appropriate number of bytes if the value is extracted
214 /// correctly. If the offset is out of bounds or there are not
215 /// enough bytes to extract this value, the offset will be left
216 /// unmodified.
217 ///
218 /// \param[in] Length
219 /// The number of bytes to extract. If there are not enough bytes in the
220 /// data to extract all of the bytes, the offset will be left unmodified.
221 ///
222 /// @param[in,out] Err
223 /// A pointer to an Error object. Upon return the Error object is set to
224 /// indicate the result (success/failure) of the function. If the Error
225 /// object is already set when calling this function, no extraction is
226 /// performed.
227 ///
228 /// \return
229 /// A StringRef for the extracted bytes. If the offset pointed to by
230 /// \a OffsetPtr is out of bounds, or if the offset plus the length
231 /// is out of bounds, a default-initialized StringRef will be returned.
232 LLVM_ABI StringRef getBytes(uint64_t *OffsetPtr, uint64_t Length,
233 Error *Err = nullptr) const;
234
235 /// Extract a fixed number of bytes from the location given by the cursor. In
236 /// case of an extraction error, or if the cursor is already in an error
237 /// state, a default-initialized StringRef is returned.
239 return getBytes(&C.Offset, Length, &C.Err);
240 }
241
242 /// Extract an unsigned integer of size \a byte_size from \a
243 /// *offset_ptr.
244 ///
245 /// Extract a single unsigned integer value and update the offset
246 /// pointed to by \a offset_ptr. The size of the extracted integer
247 /// is specified by the \a byte_size argument. \a byte_size should
248 /// have a value greater than or equal to one and less than or equal
249 /// to eight since the return value is 64 bits wide. Any
250 /// \a byte_size values less than 1 or greater than 8 will result in
251 /// nothing being extracted, and zero being returned.
252 ///
253 /// @param[in,out] offset_ptr
254 /// A pointer to an offset within the data that will be advanced
255 /// by the appropriate number of bytes if the value is extracted
256 /// correctly. If the offset is out of bounds or there are not
257 /// enough bytes to extract this value, the offset will be left
258 /// unmodified.
259 ///
260 /// @param[in] byte_size
261 /// The size in byte of the integer to extract.
262 ///
263 /// @param[in,out] Err
264 /// A pointer to an Error object. Upon return the Error object is set to
265 /// indicate the result (success/failure) of the function. If the Error
266 /// object is already set when calling this function, no extraction is
267 /// performed.
268 ///
269 /// @return
270 /// The unsigned integer value that was extracted, or zero on
271 /// failure.
272 LLVM_ABI uint64_t getUnsigned(uint64_t *offset_ptr, uint32_t byte_size,
273 Error *Err = nullptr) const;
274
275 /// Extract an unsigned integer of the given size from the location given by
276 /// the cursor. In case of an extraction error, or if the cursor is already in
277 /// an error state, zero is returned.
279 return getUnsigned(&C.Offset, Size, &C.Err);
280 }
281
282 /// Extract an signed integer of size \a byte_size from \a *offset_ptr.
283 ///
284 /// Extract a single signed integer value (sign extending if required)
285 /// and update the offset pointed to by \a offset_ptr. The size of
286 /// the extracted integer is specified by the \a byte_size argument.
287 /// \a byte_size should have a value greater than or equal to one
288 /// and less than or equal to eight since the return value is 64
289 /// bits wide. Any \a byte_size values less than 1 or greater than
290 /// 8 will result in nothing being extracted, and zero being returned.
291 ///
292 /// @param[in,out] offset_ptr
293 /// A pointer to an offset within the data that will be advanced
294 /// by the appropriate number of bytes if the value is extracted
295 /// correctly. If the offset is out of bounds or there are not
296 /// enough bytes to extract this value, the offset will be left
297 /// unmodified.
298 ///
299 /// @param[in] size
300 /// The size in bytes of the integer to extract.
301 ///
302 /// @return
303 /// The sign extended signed integer value that was extracted,
304 /// or zero on failure.
305 LLVM_ABI int64_t getSigned(uint64_t *offset_ptr, uint32_t size) const;
306
307 //------------------------------------------------------------------
308 /// Extract an pointer from \a *offset_ptr.
309 ///
310 /// Extract a single pointer from the data and update the offset
311 /// pointed to by \a offset_ptr. The size of the extracted pointer
312 /// is \a getAddressSize(), so the address size has to be
313 /// set correctly prior to extracting any pointer values.
314 ///
315 /// @param[in,out] offset_ptr
316 /// A pointer to an offset within the data that will be advanced
317 /// by the appropriate number of bytes if the value is extracted
318 /// correctly. If the offset is out of bounds or there are not
319 /// enough bytes to extract this value, the offset will be left
320 /// unmodified.
321 ///
322 /// @return
323 /// The extracted pointer value as a 64 integer.
324 uint64_t getAddress(uint64_t *offset_ptr) const {
325 return getUnsigned(offset_ptr, AddressSize);
326 }
327
328 /// Extract a pointer-sized unsigned integer from the location given by the
329 /// cursor. In case of an extraction error, or if the cursor is already in
330 /// an error state, zero is returned.
331 uint64_t getAddress(Cursor &C) const { return getUnsigned(C, AddressSize); }
332
333 /// Extract a uint8_t value from \a *offset_ptr.
334 ///
335 /// Extract a single uint8_t from the binary data at the offset
336 /// pointed to by \a offset_ptr, and advance the offset on success.
337 ///
338 /// @param[in,out] offset_ptr
339 /// A pointer to an offset within the data that will be advanced
340 /// by the appropriate number of bytes if the value is extracted
341 /// correctly. If the offset is out of bounds or there are not
342 /// enough bytes to extract this value, the offset will be left
343 /// unmodified.
344 ///
345 /// @param[in,out] Err
346 /// A pointer to an Error object. Upon return the Error object is set to
347 /// indicate the result (success/failure) of the function. If the Error
348 /// object is already set when calling this function, no extraction is
349 /// performed.
350 ///
351 /// @return
352 /// The extracted uint8_t value.
353 LLVM_ABI uint8_t getU8(uint64_t *offset_ptr, Error *Err = nullptr) const;
354
355 /// Extract a single uint8_t value from the location given by the cursor. In
356 /// case of an extraction error, or if the cursor is already in an error
357 /// state, zero is returned.
358 uint8_t getU8(Cursor &C) const { return getU8(&C.Offset, &C.Err); }
359
360 /// Extract \a count uint8_t values from \a *offset_ptr.
361 ///
362 /// Extract \a count uint8_t values from the binary data at the
363 /// offset pointed to by \a offset_ptr, and advance the offset on
364 /// success. The extracted values are copied into \a dst.
365 ///
366 /// @param[in,out] offset_ptr
367 /// A pointer to an offset within the data that will be advanced
368 /// by the appropriate number of bytes if the value is extracted
369 /// correctly. If the offset is out of bounds or there are not
370 /// enough bytes to extract this value, the offset will be left
371 /// unmodified.
372 ///
373 /// @param[out] dst
374 /// A buffer to copy \a count uint8_t values into. \a dst must
375 /// be large enough to hold all requested data.
376 ///
377 /// @param[in] count
378 /// The number of uint8_t values to extract.
379 ///
380 /// @return
381 /// \a dst if all values were properly extracted and copied,
382 /// NULL otherise.
383 LLVM_ABI uint8_t *getU8(uint64_t *offset_ptr, uint8_t *dst,
384 uint32_t count) const;
385
386 /// Extract \a Count uint8_t values from the location given by the cursor and
387 /// store them into the destination buffer. In case of an extraction error, or
388 /// if the cursor is already in an error state, a nullptr is returned and the
389 /// destination buffer is left unchanged.
390 LLVM_ABI uint8_t *getU8(Cursor &C, uint8_t *Dst, uint32_t Count) const;
391
392 /// Extract \a Count uint8_t values from the location given by the cursor and
393 /// store them into the destination vector. The vector is resized to fit the
394 /// extracted data. In case of an extraction error, or if the cursor is
395 /// already in an error state, the destination vector is left unchanged and
396 /// cursor is placed into an error state.
399 Dst.resize(Count);
400
401 // This relies on the fact that getU8 will not attempt to write to the
402 // buffer if isValidOffsetForDataOfSize(C.Offset, Count) is false.
403 getU8(C, Dst.data(), Count);
404 }
405
406 /// Extract a int8_t value from \a *OffsetPtr. In case of an extraction error,
407 /// or if error is already set, zero is returned and the offset is left
408 /// unmodified.
409 int8_t getS8(uint64_t *OffsetPtr, Error *Err = nullptr) const {
410 return static_cast<int8_t>(getU8(OffsetPtr, Err));
411 }
412
413 /// Extract a int8_t value from \a *OffsetPtr. In case of an extraction error,
414 /// or if the cursor is already in an error state, zero is returned and the
415 /// offset is left unmodified.
416 int8_t getS8(Cursor &C) const { return static_cast<int8_t>(getU8(C)); }
417
418 //------------------------------------------------------------------
419 /// Extract a uint16_t value from \a *offset_ptr.
420 ///
421 /// Extract a single uint16_t from the binary data at the offset
422 /// pointed to by \a offset_ptr, and update the offset on success.
423 ///
424 /// @param[in,out] offset_ptr
425 /// A pointer to an offset within the data that will be advanced
426 /// by the appropriate number of bytes if the value is extracted
427 /// correctly. If the offset is out of bounds or there are not
428 /// enough bytes to extract this value, the offset will be left
429 /// unmodified.
430 ///
431 /// @param[in,out] Err
432 /// A pointer to an Error object. Upon return the Error object is set to
433 /// indicate the result (success/failure) of the function. If the Error
434 /// object is already set when calling this function, no extraction is
435 /// performed.
436 ///
437 /// @return
438 /// The extracted uint16_t value.
439 //------------------------------------------------------------------
440 LLVM_ABI uint16_t getU16(uint64_t *offset_ptr, Error *Err = nullptr) const;
441
442 /// Extract a single uint16_t value from the location given by the cursor. In
443 /// case of an extraction error, or if the cursor is already in an error
444 /// state, zero is returned.
445 uint16_t getU16(Cursor &C) const { return getU16(&C.Offset, &C.Err); }
446
447 /// Extract \a count uint16_t values from \a *offset_ptr.
448 ///
449 /// Extract \a count uint16_t values from the binary data at the
450 /// offset pointed to by \a offset_ptr, and advance the offset on
451 /// success. The extracted values are copied into \a dst.
452 ///
453 /// @param[in,out] offset_ptr
454 /// A pointer to an offset within the data that will be advanced
455 /// by the appropriate number of bytes if the value is extracted
456 /// correctly. If the offset is out of bounds or there are not
457 /// enough bytes to extract this value, the offset will be left
458 /// unmodified.
459 ///
460 /// @param[out] dst
461 /// A buffer to copy \a count uint16_t values into. \a dst must
462 /// be large enough to hold all requested data.
463 ///
464 /// @param[in] count
465 /// The number of uint16_t values to extract.
466 ///
467 /// @return
468 /// \a dst if all values were properly extracted and copied,
469 /// NULL otherise.
470 LLVM_ABI uint16_t *getU16(uint64_t *offset_ptr, uint16_t *dst,
471 uint32_t count) const;
472
473 /// Extract a int16_t value from \a *OffsetPtr. In case of an extraction
474 /// error, or if error is already set, zero is returned and the offset is left
475 /// unmodified.
476 int16_t getS16(uint64_t *OffsetPtr, Error *Err = nullptr) const {
477 return static_cast<int16_t>(getU16(OffsetPtr, Err));
478 }
479
480 /// Extract a int16_t value from \a *OffsetPtr. In case of an extraction
481 /// error, or if the cursor is already in an error state, zero is returned and
482 /// the offset is left unmodified.
483 int16_t getS16(Cursor &C) const { return static_cast<int16_t>(getU16(C)); }
484
485 /// Extract a 24-bit unsigned value from \a *offset_ptr and return it
486 /// in a uint32_t.
487 ///
488 /// Extract 3 bytes from the binary data at the offset pointed to by
489 /// \a offset_ptr, construct a uint32_t from them and update the offset
490 /// on success.
491 ///
492 /// @param[in,out] OffsetPtr
493 /// A pointer to an offset within the data that will be advanced
494 /// by the 3 bytes if the value is extracted correctly. If the offset
495 /// is out of bounds or there are not enough bytes to extract this value,
496 /// the offset will be left unmodified.
497 ///
498 /// @param[in,out] Err
499 /// A pointer to an Error object. Upon return the Error object is set to
500 /// indicate the result (success/failure) of the function. If the Error
501 /// object is already set when calling this function, no extraction is
502 /// performed.
503 ///
504 /// @return
505 /// The extracted 24-bit value represented in a uint32_t.
506 LLVM_ABI uint32_t getU24(uint64_t *OffsetPtr, Error *Err = nullptr) const;
507
508 /// Extract a single 24-bit unsigned value from the location given by the
509 /// cursor. In case of an extraction error, or if the cursor is already in an
510 /// error state, zero is returned.
511 uint32_t getU24(Cursor &C) const { return getU24(&C.Offset, &C.Err); }
512
513 /// Extract a uint32_t value from \a *offset_ptr.
514 ///
515 /// Extract a single uint32_t from the binary data at the offset
516 /// pointed to by \a offset_ptr, and update the offset on success.
517 ///
518 /// @param[in,out] offset_ptr
519 /// A pointer to an offset within the data that will be advanced
520 /// by the appropriate number of bytes if the value is extracted
521 /// correctly. If the offset is out of bounds or there are not
522 /// enough bytes to extract this value, the offset will be left
523 /// unmodified.
524 ///
525 /// @param[in,out] Err
526 /// A pointer to an Error object. Upon return the Error object is set to
527 /// indicate the result (success/failure) of the function. If the Error
528 /// object is already set when calling this function, no extraction is
529 /// performed.
530 ///
531 /// @return
532 /// The extracted uint32_t value.
533 LLVM_ABI uint32_t getU32(uint64_t *offset_ptr, Error *Err = nullptr) const;
534
535 /// Extract a single uint32_t value from the location given by the cursor. In
536 /// case of an extraction error, or if the cursor is already in an error
537 /// state, zero is returned.
538 uint32_t getU32(Cursor &C) const { return getU32(&C.Offset, &C.Err); }
539
540 /// Extract \a count uint32_t values from \a *offset_ptr.
541 ///
542 /// Extract \a count uint32_t values from the binary data at the
543 /// offset pointed to by \a offset_ptr, and advance the offset on
544 /// success. The extracted values are copied into \a dst.
545 ///
546 /// @param[in,out] offset_ptr
547 /// A pointer to an offset within the data that will be advanced
548 /// by the appropriate number of bytes if the value is extracted
549 /// correctly. If the offset is out of bounds or there are not
550 /// enough bytes to extract this value, the offset will be left
551 /// unmodified.
552 ///
553 /// @param[out] dst
554 /// A buffer to copy \a count uint32_t values into. \a dst must
555 /// be large enough to hold all requested data.
556 ///
557 /// @param[in] count
558 /// The number of uint32_t values to extract.
559 ///
560 /// @return
561 /// \a dst if all values were properly extracted and copied,
562 /// NULL otherise.
563 LLVM_ABI uint32_t *getU32(uint64_t *offset_ptr, uint32_t *dst,
564 uint32_t count) const;
565
566 /// Extract a int32_t value from \a *OffsetPtr. In case of an extraction
567 /// error, or if error is already set, zero is returned and the offset is left
568 /// unmodified.
569 int32_t getS32(uint64_t *OffsetPtr, Error *Err = nullptr) const {
570 return static_cast<int32_t>(getU32(OffsetPtr, Err));
571 }
572
573 /// Extract a int32_t value from \a *OffsetPtr. In case of an extraction
574 /// error, or if the cursor is already in an error state, zero is returned and
575 /// the offset is left unmodified.
576 int32_t getS32(Cursor &C) const { return static_cast<int32_t>(getU32(C)); }
577
578 /// Extract a uint64_t value from \a *offset_ptr.
579 ///
580 /// Extract a single uint64_t from the binary data at the offset
581 /// pointed to by \a offset_ptr, and update the offset on success.
582 ///
583 /// @param[in,out] offset_ptr
584 /// A pointer to an offset within the data that will be advanced
585 /// by the appropriate number of bytes if the value is extracted
586 /// correctly. If the offset is out of bounds or there are not
587 /// enough bytes to extract this value, the offset will be left
588 /// unmodified.
589 ///
590 /// @param[in,out] Err
591 /// A pointer to an Error object. Upon return the Error object is set to
592 /// indicate the result (success/failure) of the function. If the Error
593 /// object is already set when calling this function, no extraction is
594 /// performed.
595 ///
596 /// @return
597 /// The extracted uint64_t value.
598 LLVM_ABI uint64_t getU64(uint64_t *offset_ptr, Error *Err = nullptr) const;
599
600 /// Extract a single uint64_t value from the location given by the cursor. In
601 /// case of an extraction error, or if the cursor is already in an error
602 /// state, zero is returned.
603 uint64_t getU64(Cursor &C) const { return getU64(&C.Offset, &C.Err); }
604
605 /// Extract \a count uint64_t values from \a *offset_ptr.
606 ///
607 /// Extract \a count uint64_t values from the binary data at the
608 /// offset pointed to by \a offset_ptr, and advance the offset on
609 /// success. The extracted values are copied into \a dst.
610 ///
611 /// @param[in,out] offset_ptr
612 /// A pointer to an offset within the data that will be advanced
613 /// by the appropriate number of bytes if the value is extracted
614 /// correctly. If the offset is out of bounds or there are not
615 /// enough bytes to extract this value, the offset will be left
616 /// unmodified.
617 ///
618 /// @param[out] dst
619 /// A buffer to copy \a count uint64_t values into. \a dst must
620 /// be large enough to hold all requested data.
621 ///
622 /// @param[in] count
623 /// The number of uint64_t values to extract.
624 ///
625 /// @return
626 /// \a dst if all values were properly extracted and copied,
627 /// NULL otherise.
628 LLVM_ABI uint64_t *getU64(uint64_t *offset_ptr, uint64_t *dst,
629 uint32_t count) const;
630
631 /// Extract a int64_t value from \a *OffsetPtr. In case of an extraction
632 /// error, or if error is already set, zero is returned and the offset is left
633 /// unmodified.
634 int64_t getS64(uint64_t *OffsetPtr, Error *Err = nullptr) const {
635 return static_cast<int64_t>(getU64(OffsetPtr, Err));
636 }
637
638 /// Extract a int64_t value from \a *OffsetPtr. In case of an extraction
639 /// error, or if the cursor is already in an error state, zero is returned and
640 /// the offset is left unmodified.
641 int64_t getS64(Cursor &C) const { return static_cast<int64_t>(getU64(C)); }
642
643 /// Extract a signed LEB128 value from \a *offset_ptr.
644 ///
645 /// Extracts an signed LEB128 number from this object's data
646 /// starting at the offset pointed to by \a offset_ptr. The offset
647 /// pointed to by \a offset_ptr will be updated with the offset of
648 /// the byte following the last extracted byte.
649 ///
650 /// @param[in,out] OffsetPtr
651 /// A pointer to an offset within the data that will be advanced
652 /// by the appropriate number of bytes if the value is extracted
653 /// correctly. If the offset is out of bounds or there are not
654 /// enough bytes to extract this value, the offset will be left
655 /// unmodified.
656 ///
657 /// @param[in,out] Err
658 /// A pointer to an Error object. Upon return the Error object is set to
659 /// indicate the result (success/failure) of the function. If the Error
660 /// object is already set when calling this function, no extraction is
661 /// performed.
662 ///
663 /// @return
664 /// The extracted signed integer value.
665 LLVM_ABI int64_t getSLEB128(uint64_t *OffsetPtr, Error *Err = nullptr) const;
666
667 /// Extract an signed LEB128 value from the location given by the cursor.
668 /// In case of an extraction error, or if the cursor is already in an error
669 /// state, zero is returned.
670 int64_t getSLEB128(Cursor &C) const { return getSLEB128(&C.Offset, &C.Err); }
671
672 /// Extract a unsigned LEB128 value from \a *offset_ptr.
673 ///
674 /// Extracts an unsigned LEB128 number from this object's data
675 /// starting at the offset pointed to by \a offset_ptr. The offset
676 /// pointed to by \a offset_ptr will be updated with the offset of
677 /// the byte following the last extracted byte.
678 ///
679 /// @param[in,out] offset_ptr
680 /// A pointer to an offset within the data that will be advanced
681 /// by the appropriate number of bytes if the value is extracted
682 /// correctly. If the offset is out of bounds or there are not
683 /// enough bytes to extract this value, the offset will be left
684 /// unmodified.
685 ///
686 /// @param[in,out] Err
687 /// A pointer to an Error object. Upon return the Error object is set to
688 /// indicate the result (success/failure) of the function. If the Error
689 /// object is already set when calling this function, no extraction is
690 /// performed.
691 ///
692 /// @return
693 /// The extracted unsigned integer value.
695 llvm::Error *Err = nullptr) const;
696
697 /// Extract an unsigned LEB128 value from the location given by the cursor.
698 /// In case of an extraction error, or if the cursor is already in an error
699 /// state, zero is returned.
700 uint64_t getULEB128(Cursor &C) const { return getULEB128(&C.Offset, &C.Err); }
701
702 /// Advance the Cursor position by the given number of bytes. No-op if the
703 /// cursor is in an error state.
704 LLVM_ABI void skip(Cursor &C, uint64_t Length) const;
705
706 /// Return true iff the cursor is at the end of the buffer, regardless of the
707 /// error state of the cursor. The only way both eof and error states can be
708 /// true is if one attempts a read while the cursor is at the very end of the
709 /// data buffer.
710 bool eof(const Cursor &C) const { return size() == C.Offset; }
711
712 /// Test the validity of \a offset.
713 ///
714 /// @return
715 /// \b true if \a offset is a valid offset into the data in this
716 /// object, \b false otherwise.
717 bool isValidOffset(uint64_t offset) const { return size() > offset; }
718
719 /// Test the availability of \a length bytes of data from \a offset.
720 ///
721 /// @return
722 /// \b true if \a offset is a valid offset and there are \a
723 /// length bytes available at that offset, \b false otherwise.
724 bool isValidOffsetForDataOfSize(uint64_t offset, uint64_t length) const {
725 return offset + length >= offset && isValidOffset(offset + length - 1);
726 }
727
728 /// Test the availability of enough bytes of data for a pointer from
729 /// \a offset. The size of a pointer is \a getAddressSize().
730 ///
731 /// @return
732 /// \b true if \a offset is a valid offset and there are enough
733 /// bytes for a pointer available at that offset, \b false
734 /// otherwise.
736 return isValidOffsetForDataOfSize(offset, AddressSize);
737 }
738
739 /// Return the number of bytes in the underlying buffer.
740 size_t size() const { return Data.size(); }
741
742protected:
743 // Make it possible for subclasses to access these fields without making them
744 // public.
745 static uint64_t &getOffset(Cursor &C) { return C.Offset; }
746 static Error &getError(Cursor &C) { return C.Err; }
747
748private:
749 /// If it is possible to read \a Size bytes at offset \a Offset, returns \b
750 /// true. Otherwise, returns \b false. If \a E is not nullptr, also sets the
751 /// error object to indicate an error.
752 bool prepareRead(uint64_t Offset, uint64_t Size, Error *E) const;
753
754 template <typename T> T getU(uint64_t *OffsetPtr, Error *Err) const;
755 template <typename T>
756 T *getUs(uint64_t *OffsetPtr, T *Dst, uint32_t Count, Error *Err) const;
757};
758
759} // namespace llvm
760
761#endif
aarch64 promote const
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_ABI
Definition Compiler.h:213
T
#define T
static Split data
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
A class representing a position in a DataExtractor, as well as any error encountered during extractio...
Definition DataExtractor.h:51
Cursor(uint64_t Offset)
Construct a cursor for extraction from the given offset.
Definition DataExtractor.h:59
uint64_t tell() const
Return the current position of this Cursor.
Definition DataExtractor.h:68
Error takeError()
Return error contained inside this Cursor, if any.
Definition DataExtractor.h:75
void seek(uint64_t NewOffSet)
Set the cursor to the new offset. This does not impact the error state.
Definition DataExtractor.h:71
LLVM_ABI StringRef getFixedLengthString(uint64_t *OffsetPtr, uint64_t Length, StringRef TrimChars={"0円", 1}) const
Extract a fixed length string from *OffsetPtr and consume Length bytes.
uint32_t getU32(Cursor &C) const
Extract a single uint32_t value from the location given by the cursor.
LLVM_ABI 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.
LLVM_ABI uint32_t getU32(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint32_t value from *offset_ptr.
size_t size() const
Return the number of bytes in the underlying buffer.
const char * getCStr(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a C string from *offset_ptr.
void getU8(Cursor &C, SmallVectorImpl< uint8_t > &Dst, uint32_t Count) const
Extract Count uint8_t values from the location given by the cursor and store them into the destinatio...
int16_t getS16(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a int16_t value from *OffsetPtr.
int8_t getS8(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a int8_t value from *OffsetPtr.
int64_t getSLEB128(Cursor &C) const
Extract an signed LEB128 value from the location given by the cursor.
static uint64_t & getOffset(Cursor &C)
int32_t getS32(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a int32_t value from *OffsetPtr.
bool eof(const Cursor &C) const
Return true iff the cursor is at the end of the buffer, regardless of the error state of the cursor.
LLVM_ABI StringRef getCStrRef(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a C string from *offset_ptr.
uint64_t getAddress(uint64_t *offset_ptr) const
Extract an pointer from *offset_ptr.
LLVM_ABI uint8_t getU8(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint8_t value from *offset_ptr.
DataExtractor(StringRef Data, bool IsLittleEndian, uint8_t AddressSize)
Construct with a buffer that is owned by the caller.
Definition DataExtractor.h:83
LLVM_ABI int64_t getSigned(uint64_t *offset_ptr, uint32_t size) const
Extract an signed integer of size byte_size from *offset_ptr.
int64_t getS64(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a int64_t value from *OffsetPtr.
LLVM_ABI uint64_t getULEB128(uint64_t *offset_ptr, llvm::Error *Err=nullptr) const
Extract a unsigned LEB128 value from *offset_ptr.
uint8_t getAddressSize() const
Get the address size for this extractor.
Definition DataExtractor.h:96
uint64_t getUnsigned(Cursor &C, uint32_t Size) const
Extract an unsigned integer of the given size from the location given by the cursor.
uint64_t getULEB128(Cursor &C) const
Extract an unsigned LEB128 value from the location given by the cursor.
uint32_t getU24(Cursor &C) const
Extract a single 24-bit unsigned value from the location given by the cursor.
DataExtractor(ArrayRef< uint8_t > Data, bool IsLittleEndian, uint8_t AddressSize)
Definition DataExtractor.h:85
StringRef getData() const
Get the data pointed to by this extractor.
Definition DataExtractor.h:92
LLVM_ABI int64_t getSLEB128(uint64_t *OffsetPtr, Error *Err=nullptr) const
Extract a signed LEB128 value from *offset_ptr.
StringRef getCStrRef(Cursor &C) const
Extract a C string (as a StringRef) from the location given by the cursor.
StringRef getBytes(Cursor &C, uint64_t Length)
Extract a fixed number of bytes from the location given by the cursor.
int32_t getS32(Cursor &C) const
Extract a int32_t value from *OffsetPtr.
const char * getCStr(Cursor &C) const
Extract a C string from the location given by the cursor.
LLVM_ABI uint16_t getU16(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint16_t value from *offset_ptr.
uint16_t getU16(Cursor &C) const
Extract a single uint16_t value from the location given by the cursor.
LLVM_ABI void skip(Cursor &C, uint64_t Length) const
Advance the Cursor position by the given number of bytes.
uint64_t getU64(Cursor &C) const
Extract a single uint64_t value from the location given by the cursor.
LLVM_ABI uint64_t getU64(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint64_t value from *offset_ptr.
uint8_t getU8(Cursor &C) const
Extract a single uint8_t value from the location given by the cursor.
void setAddressSize(uint8_t Size)
Set the address size for this extractor.
Definition DataExtractor.h:98
static Error & getError(Cursor &C)
int16_t getS16(Cursor &C) const
Extract a int16_t value from *OffsetPtr.
bool isValidOffset(uint64_t offset) const
Test the validity of offset.
bool isValidOffsetForDataOfSize(uint64_t offset, uint64_t length) const
Test the availability of length bytes of data from offset.
bool isLittleEndian() const
Get the endianness for this extractor.
Definition DataExtractor.h:94
uint64_t getAddress(Cursor &C) const
Extract a pointer-sized unsigned integer from the location given by the cursor.
LLVM_ABI StringRef getBytes(uint64_t *OffsetPtr, uint64_t Length, Error *Err=nullptr) const
Extract a fixed number of bytes from the specified offset.
int8_t getS8(Cursor &C) const
Extract a int8_t value from *OffsetPtr.
LLVM_ABI 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.
int64_t getS64(Cursor &C) const
Extract a int64_t value from *OffsetPtr.
bool isValidOffsetForAddress(uint64_t offset) const
Test the availability of enough bytes of data for a pointer from offset.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition SmallVector.h:574
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition StringRef.h:140
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
This is an optimization pass for GlobalISel generic memory operations.
Definition AddressRanges.h:18
@ Offset
Definition DWP.cpp:477
@ Length
Definition DWP.cpp:477
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1655
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
uint24_t getSwappedBytes(uint24_t C)
Needed by swapByteOrder().
Definition DataExtractor.h:34
Uint24 uint24_t
Definition DataExtractor.h:30
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:1954
LogicalResult success(bool IsSuccess=true)
Utility function to generate a LogicalResult.
Definition LogicalResult.h:55
An auxiliary type to facilitate extraction of 3-byte entities.
Definition DataExtractor.h:20
Uint24(uint8_t U)
Definition DataExtractor.h:22
uint8_t Bytes[3]
Definition DataExtractor.h:21
uint32_t getAsUint32(bool IsLittleEndian) const
Definition DataExtractor.h:24
Uint24(uint8_t U0, uint8_t U1, uint8_t U2)
Definition DataExtractor.h:23

Generated on for LLVM by doxygen 1.14.0

AltStyle によって変換されたページ (->オリジナル) /