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