//===-- DataExtractor.cpp -------------------------------------------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// #include "lldb/Utility/DataExtractor.h" #include "lldb/lldb-defines.h" #include "lldb/lldb-enumerations.h" #include "lldb/lldb-forward.h" #include "lldb/lldb-types.h" #include "lldb/Utility/DataBuffer.h" #include "lldb/Utility/DataBufferHeap.h" #include "lldb/Utility/LLDBAssert.h" #include "lldb/Utility/Log.h" #include "lldb/Utility/Stream.h" #include "lldb/Utility/StreamString.h" #include "lldb/Utility/UUID.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/LEB128.h" #include "llvm/Support/MD5.h" #include "llvm/Support/MathExtras.h" #include <algorithm> #include <array> #include <cassert> #include <cstdint> #include <string> #include <cctype> #include <cinttypes> #include <cstring> usingnamespacelldb; usingnamespacelldb_private; static inline uint16_t ReadInt16(const unsigned char *ptr, offset_t offset) { … } static inline uint32_t ReadInt32(const unsigned char *ptr, offset_t offset = 0) { … } static inline uint64_t ReadInt64(const unsigned char *ptr, offset_t offset = 0) { … } static inline uint16_t ReadInt16(const void *ptr) { … } static inline uint16_t ReadSwapInt16(const unsigned char *ptr, offset_t offset) { … } static inline uint32_t ReadSwapInt32(const unsigned char *ptr, offset_t offset) { … } static inline uint64_t ReadSwapInt64(const unsigned char *ptr, offset_t offset) { … } static inline uint16_t ReadSwapInt16(const void *ptr) { … } static inline uint32_t ReadSwapInt32(const void *ptr) { … } static inline uint64_t ReadSwapInt64(const void *ptr) { … } static inline uint64_t ReadMaxInt64(const uint8_t *data, size_t byte_size, ByteOrder byte_order) { … } DataExtractor::DataExtractor() : … { … } // This constructor allows us to use data that is owned by someone else. The // data must stay around as long as this object is valid. DataExtractor::DataExtractor(const void *data, offset_t length, ByteOrder endian, uint32_t addr_size, uint32_t target_byte_size /*=1*/) : … { … } // Make a shared pointer reference to the shared data in "data_sp" and set the // endian swapping setting to "swap", and the address size to "addr_size". The // shared data reference will ensure the data lives as long as any // DataExtractor objects exist that have a reference to this data. DataExtractor::DataExtractor(const DataBufferSP &data_sp, ByteOrder endian, uint32_t addr_size, uint32_t target_byte_size /*=1*/) : … { … } // Initialize this object with a subset of the data bytes in "data". If "data" // contains shared data, then a reference to this shared data will added and // the shared data will stay around as long as any object contains a reference // to that data. The endian swap and address size settings are copied from // "data". DataExtractor::DataExtractor(const DataExtractor &data, offset_t offset, offset_t length, uint32_t target_byte_size /*=1*/) : … { … } DataExtractor::DataExtractor(const DataExtractor &rhs) : … { … } // Assignment operator const DataExtractor &DataExtractor::operator=(const DataExtractor &rhs) { … } DataExtractor::~DataExtractor() = default; // Clears the object contents back to a default invalid state, and release any // references to shared data that this object may contain. void DataExtractor::Clear() { … } // If this object contains shared data, this function returns the offset into // that shared data. Else zero is returned. size_t DataExtractor::GetSharedDataOffset() const { … } // Set the data with which this object will extract from to data starting at // BYTES and set the length of the data to LENGTH bytes long. The data is // externally owned must be around at least as long as this object points to // the data. No copy of the data is made, this object just refers to this data // and can extract from it. If this object refers to any shared data upon // entry, the reference to that data will be released. Is SWAP is set to true, // any data extracted will be endian swapped. lldb::offset_t DataExtractor::SetData(const void *bytes, offset_t length, ByteOrder endian) { … } // Assign the data for this object to be a subrange in "data" starting // "data_offset" bytes into "data" and ending "data_length" bytes later. If // "data_offset" is not a valid offset into "data", then this object will // contain no bytes. If "data_offset" is within "data" yet "data_length" is too // large, the length will be capped at the number of bytes remaining in "data". // If "data" contains a shared pointer to other data, then a ref counted // pointer to that data will be made in this object. If "data" doesn't contain // a shared pointer to data, then the bytes referred to in "data" will need to // exist at least as long as this object refers to those bytes. The address // size and endian swap settings are copied from the current values in "data". lldb::offset_t DataExtractor::SetData(const DataExtractor &data, offset_t data_offset, offset_t data_length) { … } // Assign the data for this object to be a subrange of the shared data in // "data_sp" starting "data_offset" bytes into "data_sp" and ending // "data_length" bytes later. If "data_offset" is not a valid offset into // "data_sp", then this object will contain no bytes. If "data_offset" is // within "data_sp" yet "data_length" is too large, the length will be capped // at the number of bytes remaining in "data_sp". A ref counted pointer to the // data in "data_sp" will be made in this object IF the number of bytes this // object refers to in greater than zero (if at least one byte was available // starting at "data_offset") to ensure the data stays around as long as it is // needed. The address size and endian swap settings will remain unchanged from // their current settings. lldb::offset_t DataExtractor::SetData(const DataBufferSP &data_sp, offset_t data_offset, offset_t data_length) { … } // Extract a single unsigned char from the binary data and update the offset // pointed to by "offset_ptr". // // RETURNS the byte that was extracted, or zero on failure. uint8_t DataExtractor::GetU8(offset_t *offset_ptr) const { … } // Extract "count" unsigned chars from the binary data and update the offset // pointed to by "offset_ptr". The extracted data is copied into "dst". // // RETURNS the non-nullptr buffer pointer upon successful extraction of // all the requested bytes, or nullptr when the data is not available in the // buffer due to being out of bounds, or insufficient data. void *DataExtractor::GetU8(offset_t *offset_ptr, void *dst, uint32_t count) const { … } // Extract a single uint16_t from the data and update the offset pointed to by // "offset_ptr". // // RETURNS the uint16_t that was extracted, or zero on failure. uint16_t DataExtractor::GetU16(offset_t *offset_ptr) const { … } uint16_t DataExtractor::GetU16_unchecked(offset_t *offset_ptr) const { … } uint32_t DataExtractor::GetU32_unchecked(offset_t *offset_ptr) const { … } uint64_t DataExtractor::GetU64_unchecked(offset_t *offset_ptr) const { … } // Extract "count" uint16_t values from the binary data and update the offset // pointed to by "offset_ptr". The extracted data is copied into "dst". // // RETURNS the non-nullptr buffer pointer upon successful extraction of // all the requested bytes, or nullptr when the data is not available in the // buffer due to being out of bounds, or insufficient data. void *DataExtractor::GetU16(offset_t *offset_ptr, void *void_dst, uint32_t count) const { … } // Extract a single uint32_t from the data and update the offset pointed to by // "offset_ptr". // // RETURNS the uint32_t that was extracted, or zero on failure. uint32_t DataExtractor::GetU32(offset_t *offset_ptr) const { … } // Extract "count" uint32_t values from the binary data and update the offset // pointed to by "offset_ptr". The extracted data is copied into "dst". // // RETURNS the non-nullptr buffer pointer upon successful extraction of // all the requested bytes, or nullptr when the data is not available in the // buffer due to being out of bounds, or insufficient data. void *DataExtractor::GetU32(offset_t *offset_ptr, void *void_dst, uint32_t count) const { … } // Extract a single uint64_t from the data and update the offset pointed to by // "offset_ptr". // // RETURNS the uint64_t that was extracted, or zero on failure. uint64_t DataExtractor::GetU64(offset_t *offset_ptr) const { … } // GetU64 // // Get multiple consecutive 64 bit values. Return true if the entire read // succeeds and increment the offset pointed to by offset_ptr, else return // false and leave the offset pointed to by offset_ptr unchanged. void *DataExtractor::GetU64(offset_t *offset_ptr, void *void_dst, uint32_t count) const { … } uint32_t DataExtractor::GetMaxU32(offset_t *offset_ptr, size_t byte_size) const { … } uint64_t DataExtractor::GetMaxU64(offset_t *offset_ptr, size_t byte_size) const { … } uint64_t DataExtractor::GetMaxU64_unchecked(offset_t *offset_ptr, size_t byte_size) const { … } int64_t DataExtractor::GetMaxS64(offset_t *offset_ptr, size_t byte_size) const { … } uint64_t DataExtractor::GetMaxU64Bitfield(offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const { … } int64_t DataExtractor::GetMaxS64Bitfield(offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const { … } float DataExtractor::GetFloat(offset_t *offset_ptr) const { … } double DataExtractor::GetDouble(offset_t *offset_ptr) const { … } long double DataExtractor::GetLongDouble(offset_t *offset_ptr) const { … } // Extract a single address from the data and update the offset pointed to by // "offset_ptr". The size of the extracted address comes from the // "this->m_addr_size" member variable and should be set correctly prior to // extracting any address values. // // RETURNS the address that was extracted, or zero on failure. uint64_t DataExtractor::GetAddress(offset_t *offset_ptr) const { … } uint64_t DataExtractor::GetAddress_unchecked(offset_t *offset_ptr) const { … } size_t DataExtractor::ExtractBytes(offset_t offset, offset_t length, ByteOrder dst_byte_order, void *dst) const { … } // Extract data as it exists in target memory lldb::offset_t DataExtractor::CopyData(offset_t offset, offset_t length, void *dst) const { … } // Extract data and swap if needed when doing the copy lldb::offset_t DataExtractor::CopyByteOrderedData(offset_t src_offset, offset_t src_len, void *dst_void_ptr, offset_t dst_len, ByteOrder dst_byte_order) const { … } // Extracts a variable length NULL terminated C string from the data at the // offset pointed to by "offset_ptr". The "offset_ptr" will be updated with // the offset of the byte that follows the NULL terminator byte. // // If the offset pointed to by "offset_ptr" is out of bounds, or if "length" is // non-zero and there aren't enough available bytes, nullptr will be returned // and "offset_ptr" will not be updated. const char *DataExtractor::GetCStr(offset_t *offset_ptr) const { … } // Extracts a NULL terminated C string from the fixed length field of length // "len" at the offset pointed to by "offset_ptr". The "offset_ptr" will be // updated with the offset of the byte that follows the fixed length field. // // If the offset pointed to by "offset_ptr" is out of bounds, or if the offset // plus the length of the field is out of bounds, or if the field does not // contain a NULL terminator byte, nullptr will be returned and "offset_ptr" // will not be updated. const char *DataExtractor::GetCStr(offset_t *offset_ptr, offset_t len) const { … } // Peeks at a string in the contained data. No verification is done to make // sure the entire string lies within the bounds of this object's data, only // "offset" is verified to be a valid offset. // // Returns a valid C string pointer if "offset" is a valid offset in this // object's data, else nullptr is returned. const char *DataExtractor::PeekCStr(offset_t offset) const { … } // Extracts an unsigned LEB128 number from this object's data starting at the // offset pointed to by "offset_ptr". The offset pointed to by "offset_ptr" // will be updated with the offset of the byte following the last extracted // byte. // // Returned the extracted integer value. uint64_t DataExtractor::GetULEB128(offset_t *offset_ptr) const { … } // Extracts an signed LEB128 number from this object's data starting at the // offset pointed to by "offset_ptr". The offset pointed to by "offset_ptr" // will be updated with the offset of the byte following the last extracted // byte. // // Returned the extracted integer value. int64_t DataExtractor::GetSLEB128(offset_t *offset_ptr) const { … } // Skips a ULEB128 number (signed or unsigned) from this object's data starting // at the offset pointed to by "offset_ptr". The offset pointed to by // "offset_ptr" will be updated with the offset of the byte following the last // extracted byte. // // Returns the number of bytes consumed during the extraction. uint32_t DataExtractor::Skip_LEB128(offset_t *offset_ptr) const { … } // Dumps bytes from this object's data to the stream "s" starting // "start_offset" bytes into this data, and ending with the byte before // "end_offset". "base_addr" will be added to the offset into the dumped data // when showing the offset into the data in the output information. // "num_per_line" objects of type "type" will be dumped with the option to // override the format for each object with "type_format". "type_format" is a // printf style formatting string. If "type_format" is nullptr, then an // appropriate format string will be used for the supplied "type". If the // stream "s" is nullptr, then the output will be send to Log(). lldb::offset_t DataExtractor::PutToLog(Log *log, offset_t start_offset, offset_t length, uint64_t base_addr, uint32_t num_per_line, DataExtractor::Type type) const { … } size_t DataExtractor::Copy(DataExtractor &dest_data) const { … } bool DataExtractor::Append(DataExtractor &rhs) { … } bool DataExtractor::Append(void *buf, offset_t length) { … } void DataExtractor::Checksum(llvm::SmallVectorImpl<uint8_t> &dest, uint64_t max_data) { … }