headers/bsd: Add sys/queue.h.
[haiku.git] / src / kits / debugger / dwarf / DataReader.h
blob3b38e802ea01b4d9d5b63b2fee058d81a29e6ef8
1 /*
2 * Copyright 2009-2012, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5 #ifndef DATA_READER_H
6 #define DATA_READER_H
9 #include <string.h>
11 #include "Types.h"
14 class DataReader {
15 public:
16 DataReader()
18 fData(NULL),
19 fSize(0),
20 fInitialSize(0),
21 fAddressSize(4),
22 fOverflow(false)
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;
36 fOverflow = false;
39 DataReader RestrictedReader()
41 return *this;
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);
54 bool HasData() const
56 return fSize > 0;
59 uint32 AddressSize() const
61 return fAddressSize;
64 void SetAddressSize(uint8 addressSize)
66 fAddressSize = addressSize;
69 bool HasOverflow() const
71 return fOverflow;
74 const void* Data() const
76 return fData;
79 off_t BytesRemaining() const
81 return fSize;
84 off_t Offset() const
86 return fInitialSize - fSize;
89 void SeekAbsolute(off_t offset)
91 if (offset < 0)
92 offset = 0;
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)) {
104 fOverflow = true;
105 fSize = 0;
106 return defaultValue;
109 Type data;
110 memcpy(&data, fData, sizeof(Type));
112 fData += sizeof(Type);
113 fSize -= sizeof(Type);
115 return data;
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)
127 uint64 result = 0;
128 int shift = 0;
129 while (true) {
130 uint8 byte = Read<uint8>(0);
131 result |= uint64(byte & 0x7f) << shift;
132 if ((byte & 0x80) == 0)
133 break;
134 shift += 7;
137 return fOverflow ? defaultValue : result;
140 int64 ReadSignedLEB128(int64 defaultValue)
142 int64 result = 0;
143 int shift = 0;
144 while (true) {
145 uint8 byte = Read<uint8>(0);
146 result |= uint64(byte & 0x7f) << shift;
147 shift += 7;
149 if ((byte & 0x80) == 0) {
150 // sign extend
151 if ((byte & 0x40) != 0 && shift < 64)
152 result |= -((uint64)1 << shift);
153 break;
157 return fOverflow ? defaultValue : result;
160 const char* ReadString()
162 const char* string = (const char*)fData;
163 while (fSize > 0) {
164 fData++;
165 fSize--;
167 if (fData[-1] == 0)
168 return string;
171 fOverflow = true;
172 return NULL;
175 uint64 ReadInitialLength(bool& _dwarf64)
177 uint64 length = Read<uint32>(0);
178 _dwarf64 = (length == 0xffffffff);
179 if (_dwarf64)
180 length = Read<uint64>(0);
181 return length;
184 bool Skip(off_t bytes)
186 if (bytes < 0)
187 return false;
189 if (bytes > fSize) {
190 fSize = 0;
191 fOverflow = true;
192 return false;
195 fData += bytes;
196 fSize -= bytes;
198 return true;
201 private:
202 const uint8* fData;
203 off_t fSize;
204 off_t fInitialSize;
205 uint8 fAddressSize;
206 bool fOverflow;
210 #endif // DATA_READER_H