2 * Copyright 2009-2012, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
26 DataReader(const void* data
, off_t size
, uint8 addressSize
)
28 SetTo(data
, size
, addressSize
);
31 void SetTo(const void* data
, off_t size
, uint8 addressSize
)
33 fData
= (const uint8
*)data
;
34 fInitialSize
= fSize
= size
;
35 fAddressSize
= addressSize
;
39 DataReader
RestrictedReader()
44 DataReader
RestrictedReader(off_t maxLength
)
46 return DataReader(fData
, maxLength
, fAddressSize
);
49 DataReader
RestrictedReader(off_t relativeOffset
, off_t maxLength
)
51 return DataReader(fData
+ relativeOffset
, maxLength
, fAddressSize
);
59 uint32
AddressSize() const
64 void SetAddressSize(uint8 addressSize
)
66 fAddressSize
= addressSize
;
69 bool HasOverflow() const
74 const void* Data() const
79 off_t
BytesRemaining() const
86 return fInitialSize
- fSize
;
89 void SeekAbsolute(off_t offset
)
93 else if (offset
> fInitialSize
)
94 offset
= fInitialSize
;
96 fData
+= offset
- Offset();
97 fSize
= fInitialSize
- offset
;
100 template<typename Type
>
101 Type
Read(const Type
& defaultValue
)
103 if (fSize
< (off_t
)sizeof(Type
)) {
110 memcpy(&data
, fData
, sizeof(Type
));
112 fData
+= sizeof(Type
);
113 fSize
-= sizeof(Type
);
118 target_addr_t
ReadAddress(target_addr_t defaultValue
)
120 return fAddressSize
== 4
121 ? (target_addr_t
)Read
<uint32
>(defaultValue
)
122 : (target_addr_t
)Read
<uint64
>(defaultValue
);
125 uint64
ReadUnsignedLEB128(uint64 defaultValue
)
130 uint8 byte
= Read
<uint8
>(0);
131 result
|= uint64(byte
& 0x7f) << shift
;
132 if ((byte
& 0x80) == 0)
137 return fOverflow
? defaultValue
: result
;
140 int64
ReadSignedLEB128(int64 defaultValue
)
145 uint8 byte
= Read
<uint8
>(0);
146 result
|= uint64(byte
& 0x7f) << shift
;
149 if ((byte
& 0x80) == 0) {
151 if ((byte
& 0x40) != 0 && shift
< 64)
152 result
|= -((uint64
)1 << shift
);
157 return fOverflow
? defaultValue
: result
;
160 const char* ReadString()
162 const char* string
= (const char*)fData
;
175 uint64
ReadInitialLength(bool& _dwarf64
)
177 uint64 length
= Read
<uint32
>(0);
178 _dwarf64
= (length
== 0xffffffff);
180 length
= Read
<uint64
>(0);
184 bool Skip(off_t bytes
)
210 #endif // DATA_READER_H