forked from Dax89/QHexView
-
Notifications
You must be signed in to change notification settings - Fork 1
/
qhexeditdatareader.cpp
236 lines (185 loc) · 6.23 KB
/
qhexeditdatareader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include "qhexeditdatareader.h"
QHexEditDataReader::QHexEditDataReader(QHexEditData *hexeditdata, QObject *parent): QObject(parent), _dirtybuffer(false), _bufferpos(-1), _hexeditdata(hexeditdata)
{
connect(hexeditdata, SIGNAL(dataChanged(qint64,qint64,QHexEditData::ActionType)), this, SLOT(onDataChanged(qint64,qint64,QHexEditData::ActionType)));
this->_buffereddata.resize(QHexEditData::BUFFER_SIZE);
}
const QHexEditData *QHexEditDataReader::hexEditData() const
{
return this->_hexeditdata;
}
uchar QHexEditDataReader::at(qint64 pos)
{
if(pos < 0)
pos = 0;
else if(pos >= this->_hexeditdata->_length)
pos = this->_hexeditdata->_length - 1;
if(this->needsBuffering(pos))
this->bufferizeData(pos);
return static_cast<uchar>(this->_buffereddata.at(pos - this->_bufferpos));
}
qint64 QHexEditDataReader::indexOf(const QByteArray &ba, qint64 start)
{
if(!ba.length())
return -1;
for(qint64 pos = start; pos < this->_hexeditdata->_length; pos++)
{
QByteArray cba = this->read(pos, ba.length());
if(cba == ba)
return pos;
}
return -1;
}
QByteArray QHexEditDataReader::read(qint64 pos, qint64 len)
{
if(pos >= this->_hexeditdata->_length)
return QByteArray();
else if(len > this->_hexeditdata->_length)
len = this->_hexeditdata->_length;
QByteArray resba(len, Qt::Uninitialized);
qint64 currpos = pos, copied = 0;
while(len > 0)
{
if(this->needsBuffering(currpos))
this->bufferizeData(currpos);
qint64 bufferedpos = currpos - this->_bufferpos, copylen = qMin(len, QHexEditData::BUFFER_SIZE);
memcpy(resba.data() + copied, this->_buffereddata.constData() + bufferedpos, copylen);
copied += copylen;
currpos += copylen;
len -= copylen;
}
return resba;
}
QString QHexEditDataReader::readString(qint64 pos, qint64 maxlen)
{
QString s;
if(maxlen == -1)
maxlen = this->_hexeditdata->length();
else
maxlen = qMin(this->_hexeditdata->length(), maxlen);
for(qint64 i = 0; i < maxlen; i++)
{
if((pos + i) >= this->_hexeditdata->length())
break;
QChar ch(this->at(pos + i));
if(!ch.unicode() || ch.unicode() >= 128)
break;
s.append(ch);
}
return s;
}
quint16 QHexEditDataReader::readUInt16(qint64 pos, QSysInfo::Endian endian)
{
QByteArray ba = this->read(pos, 2);
QDataStream ds(ba);
if(endian == QSysInfo::LittleEndian)
ds.setByteOrder(QDataStream::LittleEndian);
else
ds.setByteOrder(QDataStream::BigEndian);
quint16 val;
ds >> val;
return val;
}
quint32 QHexEditDataReader::readUInt32(qint64 pos, QSysInfo::Endian endian)
{
QByteArray ba = this->read(pos, 4);
QDataStream ds(ba);
if(endian == QSysInfo::LittleEndian)
ds.setByteOrder(QDataStream::LittleEndian);
else
ds.setByteOrder(QDataStream::BigEndian);
quint32 val;
ds >> val;
return val;
}
quint64 QHexEditDataReader::readUInt64(qint64 pos, QSysInfo::Endian endian)
{
QByteArray ba = this->read(pos, 8);
QDataStream ds(ba);
if(endian == QSysInfo::LittleEndian)
ds.setByteOrder(QDataStream::LittleEndian);
else
ds.setByteOrder(QDataStream::BigEndian);
quint64 val;
ds >> val;
return val;
}
qint16 QHexEditDataReader::readInt16(qint64 pos, QSysInfo::Endian endian)
{
QByteArray ba = this->read(pos, 2);
QDataStream ds(ba);
if(endian == QSysInfo::LittleEndian)
ds.setByteOrder(QDataStream::LittleEndian);
else
ds.setByteOrder(QDataStream::BigEndian);
qint16 val;
ds >> val;
return val;
}
qint32 QHexEditDataReader::readInt32(qint64 pos, QSysInfo::Endian endian)
{
QByteArray ba = this->read(pos, 4);
QDataStream ds(ba);
if(endian == QSysInfo::LittleEndian)
ds.setByteOrder(QDataStream::LittleEndian);
else
ds.setByteOrder(QDataStream::BigEndian);
qint32 val;
ds >> val;
return val;
}
qint64 QHexEditDataReader::readInt64(qint64 pos, QSysInfo::Endian endian)
{
QByteArray ba = this->read(pos, 8);
QDataStream ds(ba);
if(endian == QSysInfo::LittleEndian)
ds.setByteOrder(QDataStream::LittleEndian);
else
ds.setByteOrder(QDataStream::BigEndian);
qint64 val;
ds >> val;
return val;
}
void QHexEditDataReader::onDataChanged(qint64 pos, qint64, QHexEditData::ActionType)
{
if(this->inBuffer(pos))
this->_dirtybuffer = true;
}
bool QHexEditDataReader::needsBuffering(qint64 pos)
{
return this->_dirtybuffer || (this->_bufferpos == -1) || (pos < this->_bufferpos) || (pos >= (this->_bufferpos + QHexEditData::BUFFER_SIZE));
}
void QHexEditDataReader::bufferizeData(qint64 pos)
{
// Adjust pos in order to fit in buffer (if the loaded data cannot be buffer entirely: datasize > 8KB)
if((this->_hexeditdata->_length > QHexEditData::BUFFER_SIZE) && (pos + QHexEditData::BUFFER_SIZE) >= this->_hexeditdata->_length)
pos = this->_hexeditdata->_length - QHexEditData::BUFFER_SIZE;
this->_bufferpos = pos;
int i = -1;
qint64 currpos = pos, mipos = 0, copied = 0;
if(!this->_hexeditdata->modifiedItem(pos, &mipos, &i))
return;
while((copied < QHexEditData::BUFFER_SIZE) && (i < this->_hexeditdata->_modlist.length()))
{
QHexEditData::ModifiedItem* mi = this->_hexeditdata->_modlist[i];
qint64 bufferoffset = currpos - mipos, copylen = qMin(mi->length() - bufferoffset, QHexEditData::BUFFER_SIZE - copied);
if(mi->modified())
memcpy(this->_buffereddata.data() + copied, this->_hexeditdata->_modbuffer.data() + bufferoffset + mi->pos(), copylen);
else
{
this->_hexeditdata->_mutex.lock();
this->_hexeditdata->_iodevice->seek(bufferoffset + mi->pos());
this->_hexeditdata->_iodevice->read(this->_buffereddata.data() + copied, copylen);
this->_hexeditdata->_mutex.unlock();
}
mipos += mi->length();
currpos += copylen;
copied += copylen;
i++;
}
this->_dirtybuffer = false;
}
bool QHexEditDataReader::inBuffer(qint64 pos)
{
return (pos >= this->_bufferpos) && (pos < (this->_bufferpos + QHexEditData::BUFFER_SIZE));
}