forked from Dax89/QHexView
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qhexeditdata.cpp
247 lines (192 loc) · 5.93 KB
/
qhexeditdata.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
237
238
239
240
241
242
243
244
245
246
247
#include "qhexeditdata.h"
const qint64 QHexEditData::BUFFER_SIZE = 8192;
QHexEditData::QHexEditData(QIODevice *iodevice, QObject *parent): QObject(parent), _iodevice(iodevice), _length(iodevice->size()), _devicelength(iodevice->size()), _lastpos(-1), _lastaction(QHexEditData::None)
{
qRegisterMetaType<QHexEditData::ActionType>("QHexEditData::ActionType");
this->_modlist.append(new ModifiedItem(0, iodevice->size(), false));
}
QHexEditData::~QHexEditData()
{
foreach(ModifiedItem* mi, this->_modlist)
delete mi;
this->_modlist.clear();
if(this->_iodevice->isOpen())
this->_iodevice->close();
}
QUndoStack *QHexEditData::undoStack()
{
return &this->_undostack;
}
qint64 QHexEditData::length() const
{
return this->_length;
}
bool QHexEditData::isReadOnly() const
{
return !this->_iodevice->isWritable();
}
bool QHexEditData::save()
{
return this->saveTo(this->_iodevice);
}
bool QHexEditData::saveTo(QIODevice *iodevice)
{
if(!iodevice->isOpen())
iodevice->open(QIODevice::WriteOnly);
if(!iodevice->isWritable())
return false;
qint64 outseekpos = 0;
this->_iodevice->reset();
for(int i = 0; i < this->_modlist.length(); i++)
{
ModifiedItem* mi = this->_modlist[i];
iodevice->seek(outseekpos);
if(mi->modified())
iodevice->write(this->_modbuffer.mid(mi->pos(), mi->length()));
else if(iodevice != this->_iodevice)
{
this->_iodevice->seek(mi->pos());
QByteArray ba = this->_iodevice->read(mi->length());
iodevice->write(ba);
}
outseekpos += mi->length();
}
return true;
}
QHexEditData *QHexEditData::fromDevice(QIODevice *iodevice)
{
if(!iodevice->isOpen())
iodevice->open(QFile::ReadWrite);
return new QHexEditData(iodevice);
}
QHexEditData *QHexEditData::fromFile(QString filename)
{
QFileInfo fi(filename);
QFile* f = new QFile(filename);
if(fi.isWritable())
f->open(QFile::ReadWrite);
else
f->open(QFile::ReadOnly);
return QHexEditData::fromDevice(f);
}
QHexEditData *QHexEditData::fromMemory(const QByteArray &ba)
{
QBuffer* b = new QBuffer();
b->setData(ba);
b->open(QFile::ReadOnly);
return QHexEditData::fromDevice(b);
}
QHexEditData::InsertCommand* QHexEditData::internalInsert(qint64 pos, const QByteArray &ba, QHexEditData::ActionType act)
{
if(pos < 0 || pos > this->length() || !ba.length())
return nullptr;
int i;
qint64 datapos;
ModifiedItem* mi;
if(!this->_modlist.isEmpty())
{
mi = this->modifiedItem(pos, &datapos, &i);
if(!mi)
return nullptr;
}
else
{
pos = 0;
datapos = 0;
}
bool optimize = false;
ModifyList oldml, newml;
qint64 modoffset = pos - datapos;
qint64 buffoffset = this->updateBuffer(ba);
if(!modoffset)
{
optimize = (act == QHexEditData::Insert) && this->canOptimize(act, pos);
newml.append(new ModifiedItem(buffoffset, ba.length()));
}
else
{
oldml.append(mi);
newml.append(new ModifiedItem(mi->pos(), modoffset, mi->modified()));
newml.append(new ModifiedItem(buffoffset, ba.length()));
newml.append(new ModifiedItem(mi->pos() + modoffset, mi->length() - modoffset, mi->modified()));
}
this->_length += ba.length();
emit dataChanged(pos, ba.length(), act);
return new InsertCommand(i, pos, oldml, newml, this, optimize);
}
QHexEditData::RemoveCommand* QHexEditData::internalRemove(qint64 pos, qint64 len, QHexEditData::ActionType act)
{
if(pos < 0 || !len || pos >= this->length() || pos > (this->length() - len))
return nullptr;
int i;
qint64 datapos;
ModifiedItem* mi = this->modifiedItem(pos, &datapos, &i);
if(!mi)
return nullptr;
QList<ModifiedItem*> olditems, newitems;
qint64 remoffset = pos - datapos, remlength = len;
if(remoffset)
{
newitems.append(new ModifiedItem(mi->pos(), remoffset, mi->modified()));
if((remoffset + remlength) < mi->length())
newitems.append(new ModifiedItem(mi->pos() + remoffset + remlength, mi->length() - remoffset - remlength, mi->modified()));
remlength -= qMin(remlength, mi->length() - remoffset);
olditems.append(mi);
i++;
}
while(remlength && (i < this->_modlist.length()))
{
mi = this->_modlist[i];
if(remlength < mi->length())
newitems.append(new ModifiedItem(mi->pos() + remlength, mi->length() - remlength, mi->modified()));
remlength -= qMin(remlength, mi->length());
olditems.append(mi);
i++;
}
this->_length -= len;
emit dataChanged(pos, len, act);
return new RemoveCommand(i - olditems.length(), pos, olditems, newitems, this);
}
bool QHexEditData::canOptimize(QHexEditData::ActionType at, qint64 pos)
{
return (this->_lastaction == at) && (this->_lastpos == pos);
}
void QHexEditData::recordAction(QHexEditData::ActionType at, qint64 pos)
{
this->_lastpos = pos;
this->_lastaction = at;
}
qint64 QHexEditData::updateBuffer(const QByteArray &ba)
{
qint64 pos = this->_modbuffer.length();
this->_modbuffer.append(ba);
return pos;
}
QHexEditData::ModifiedItem* QHexEditData::modifiedItem(qint64 pos, qint64* datapos, int *index)
{
int i = -1;
qint64 currpos = 0;
ModifiedItem* mi = nullptr;
for(i = 0; i < this->_modlist.length(); i++)
{
mi = this->_modlist[i];
if((pos >= currpos) && (pos < (currpos + mi->length())))
{
if(datapos)
*datapos = currpos;
if(index)
*index = i;
return mi;
}
currpos += mi->length();
}
if(mi && (pos == currpos)) /* Return Last Item for Append */
{
if(datapos)
*datapos = currpos;
if(index)
*index = i;
return mi;
}
return nullptr;
}