-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmydata.h
613 lines (495 loc) · 17.9 KB
/
mydata.h
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#ifndef _MYDATA_H_
#define _MYDATA_H_
#include "thex.h"
class DataSource;
class Segment;
class HexDoc;
class UndoManager;
class HexWnd;
class HexWndSettings;
//*****************************************************************************
//*****************************************************************************
// thSize
//*****************************************************************************
//*****************************************************************************
inline bool CanAdd(THSIZE target, THSIZE other)
{
return target <= 0xFFFFFFFFFFFFFFFFLL - other;
}
inline bool CanAdd(THSIZE target, THSIZE other, THSIZE ubound)
{
return (target + other < ubound) &&
(target + other >= target); // check for overflow
}
inline bool CanSubtract(THSIZE target, THSIZE other)
{
return target >= other;
}
inline THSIZE Add(THSIZE target, THSIZE other)
{
if (CanAdd(target, other))
return target + other;
return 0xFFFFFFFFFFFFFFFFULL;
}
inline THSIZE Add(THSIZE target, THSIZE other, THSIZE ubound)
{
if (CanAdd(target, other, ubound))
return target + other;
return ubound;
}
inline THSIZE Subtract(THSIZE target, THSIZE other)
{
if (CanSubtract(target, other))
return target - other;
return 0;
}
//*****************************************************************************
//*****************************************************************************
// ByteRange
//*****************************************************************************
//*****************************************************************************
class ByteRange
{
public:
THSIZE start;
THSIZE end;
//ByteRange(THSIZE start, THSIZE end, THSIZE size) : start(start), end(start + size) { }
// named constructors
static ByteRange fromSize(THSIZE start, THSIZE size) { return ByteRange(start, start + size); }
static ByteRange fromEnd(THSIZE start, THSIZE end) { return ByteRange(start, end); }
// member functions
bool contains(THSIZE nAddress) { return (nAddress >= start && nAddress < end); }
THSIZE size() { return end - start; }
private:
ByteRange(THSIZE start, THSIZE end) : start(start), end(end) { }
};
//*****************************************************************************
//*****************************************************************************
// SerialData
//*****************************************************************************
//*****************************************************************************
#pragma warning(disable: 4200) // nonstandard extension used: zero-size array
#pragma pack(push, 1)
typedef struct {
uint8 endianMode;
int nSegments, nSources;
} SerialDataHeader;
typedef struct {
THSIZE offset, size;
int src; // src < 0 means immediate data follows
uint8 data[0];
} SerialDataSegment;
typedef struct {
DataSource *pDS;
} SerialDataSource;
#pragma pack(pop)
class SerialData
{
public:
SerialData(const uint8* data, int len);
SerialData(wxString data);
void Init(const uint8* data, int len);
wxString strData;
const uint8 *data;
int len;
bool loaded;
SerialDataHeader hdr;
int offset, m_nSourceOffset;
THSIZE m_nTotalSize;
void Clear(); // reset all data to 0 if corrupted
bool Ok() { return len ? loaded : true; }
bool GetSegment(int nSegment, SerialDataSegment &seg, const uint8 **ppData = NULL);
bool GetSource(int nSource, SerialDataSource &src);
bool Read(void *target, int size);
bool ReadNumber(void *target, int size);
wxString GetData() { return strData.IsEmpty() ? wxString((const char*)data, len) : strData; }
//bool Extract(uint8 *target,
};
//*****************************************************************************
//*****************************************************************************
// Segment
//*****************************************************************************
//*****************************************************************************
class Segment
{
public:
enum _type {FILE, MEM, FILL, NONE} type;
uint64 size, stored_offset;
DataSource *pDS;
uint8 *pData;
Segment *next, *prev;
//void InsertAfter(Segment *ts);
//void InsertBefore(Segment *ts);
Segment(uint64 size, uint64 stored_offset, DataSource *pDS);
Segment(uint64 size);
Segment(uint64 size, const uint8 *pData); // copies the data into a new buffer
// do we need a constructor that takes ownership of a pointer? Not for now...
Segment(uint64 size, uint8 fillData);
~Segment();
Segment* Split(uint64 nOffset) { return RemoveMid(nOffset, 0); }
void RemoveRight(uint64 nNewSize);
bool RemoveLeft(uint64 nRemoveSize);
Segment* RemoveMid(THSIZE nIndex, THSIZE nSize);
bool contains(uint64 nIndex, uint64 base)
{
return (nIndex >= base) &&
(nIndex < base + this->size);
}
bool contains(uint64 nIndex, uint64 nSize, uint64 base)
{
return (nIndex >= base) &&
(nIndex + nSize <= base + this->size);
}
int Compare(THSIZE address, THSIZE base)
{
if (address < base)
return 1; // address comes before this
if (address >= base + size)
return -1; // address comes after this
return 0; // address is inside this
}
bool Read(THSIZE nOffset, THSIZE nSize, uint8 *target);
int GetSerializedLength(THSIZE nOffset, THSIZE nSize);
void Serialize(THSIZE nOffset, THSIZE nSize, int nSource, uint8 *target);
static Segment* Unserialize(SerialData sdata, int iSeg);
uint8 Get(THSIZE offset); // added for ByteIterator in search.cpp
};
//*****************************************************************************
//*****************************************************************************
// Selection
//*****************************************************************************
//*****************************************************************************
class Selection
{
public:
THSIZE nStart, nEnd;
int iRegion, iDigit;
Selection()
{
Set(0, 0, -1, -1);
}
Selection(THSIZE start)
{
Set(start);
}
Selection(THSIZE start, THSIZE end, int region = 0, int digit = 0)
{
Set(start, end, region, digit);
}
void Set(THSIZE start) { Set(start, start, 0, 0); }
void Set(THSIZE start, THSIZE end, int region = 0, int digit = 0)
{
nStart = start;
nEnd = end;
iRegion = region;
iDigit = digit;
}
void Get(THSIZE &first, THSIZE &size)
{
if (nEnd < nStart)
first = nEnd, size = nStart - nEnd;
else
first = nStart, size = nEnd - nStart;
}
THSIZE GetFirst() { return wxMin(nStart, nEnd); }
THSIZE GetLast() { return wxMax(nStart, nEnd); }
THSIZE GetSize() { return GetLast() - GetFirst(); }
void SetSize(THSIZE size) { nEnd = nStart + size; }
bool IsSet() { return (iDigit >= 0); }
bool operator==(const Selection &other)
{
return
nStart == other.nStart &&
nEnd == other.nEnd &&
(iRegion == other.iRegion || iRegion == 0 || other.iRegion == 0) &&
iDigit == other.iDigit;
}
};
//*****************************************************************************
//*****************************************************************************
// HexDoc
//*****************************************************************************
//*****************************************************************************
class HexDoc
{
public:
HexDoc();
~HexDoc();
HexWndSettings *pSettings;
enum { SUPPRESS_UPDATE = 1 }; // for InsertAt/RemoveAt flags
inline THSIZE GetSize() const { return size; }
uint64 size, display_address;
wxString info;
Segment *m_curSeg;
// Tweaking any balanced tree structure I could find to look up nodes by
// position only turned out to be more complicated than I wanted to mess
// with. I decided to go with vector<> for now, for its ease of use.
// You can do a binary search on a vector if you have bases and sizes.
// While it may be technically possible to store Segments in a tree
// where the base is never stored directly (only the size of the left and
// right subtrees), rebalancing such a tree after insertions or deletions
// would be a complete pain. So we do it this way.
// Using a vector means you still have to update N items when the size of
// one Segment changes, but the data structure is simple and gives the best
// possible search performance. I think that's fair.
// (And it's better than a linked list.)
typedef std::vector<Segment*> SEGMENTVEC;
typedef std::vector<THSIZE> THSIZEVEC;
SEGMENTVEC segments;
THSIZEVEC bases;
THSIZE m_iCurSegOffset;
bool bWriteable;
bool bCanChangeSize;
DataSource *m_pDS;
HexDoc *next;
DWORD dwFlags; //! so far this holds protection flags from VirtualQueryEx()
//! this really shouldn't be here -- where does it belong? HexView class?
Selection m_sel;
THSIZE m_iFirstLine;
bool IsWriteable() { return bWriteable; }
bool IsReadOnly() { return !bWriteable; }
bool CanChangeSize() const { return bCanChangeSize; }
bool Read(uint64 nOffset, uint32 nSize, uint8 *target, uint8 *pModified = NULL);
const inline uint8 operator[](uint64 nIndex) { return GetAt(nIndex); }
//uint8& operator[](uint64 nIndex);
uint8 GetAt(uint64 nIndex);
bool IsModified(uint64 address);
bool IsModified();
void MarkModified(const ByteRange& range);
void MarkUnmodified(const ByteRange& range);
std::vector<ByteRange> modRanges;
int FindModified(THSIZE nAddress, bool wantIndexAnyway = false);
int FindModified(const ByteRange& range, int& count, bool includeAdjacent);
bool InsertAt(uint64 nIndex, uint8 src, THSIZE nCount = 1);
bool InsertAt(uint64 nIndex, const uint8* psrc, int32 nSize, THSIZE nCount = 1, int flags = 0);
bool RemoveAt(uint64 nIndex, uint64 nSize, int flags = 0);
bool ReplaceAt(uint64 ToReplaceIndex, uint8 ReplaceWith);
bool ReplaceAt(uint64 ToReplaceIndex, uint32 ToReplaceLength, const uint8* pReplaceWith, uint32 ReplaceWithLength);
//bool ReplaceAt(uint64 ToReplaceIndex, uint32 ToReplaceLength, uint8 pReplaceWith, int nCount = 1);
bool CanReplaceAt(THSIZE nIndex, THSIZE nOldSize, THSIZE nNewSize);
bool CanReplaceAt(THSIZE nIndex, THSIZE nSize) { return CanReplaceAt(nIndex, nSize, nSize); }
uint8* LoadWriteable(uint64 nIndex, uint32 nSize);
//const uint8* Load(uint64 nIndex, uint32 nSize);
bool Load(uint64 nIndex);
virtual bool Save();
virtual bool SaveRange(LPCSTR filename, uint64 begin, uint64 length);
virtual bool WriteRange(HANDLE hOutput, uint64 begin, uint64 length);
/*inline bool Save()
{
if (!OK()) return false;
}
inline bool SaveRange(LPCSTR filename, uint64 begin, uint64 length)
{
if (!OK()) return false;
}*/
inline uint16 Read16(uint64 nIndex) { uint16 t=0; ReadNumber(nIndex, sizeof(t), &t); return t; }
inline uint32 Read32(uint64 nIndex) { uint32 t=0; ReadNumber(nIndex, sizeof(t), &t); return t; }
inline uint64 Read64(uint64 nIndex) { uint64 t=0; ReadNumber(nIndex, sizeof(t), &t); return t; }
inline double ReadDouble(uint64 nIndex) { double t=0; ReadNumber(nIndex, sizeof(t), &t); return t; }
inline float ReadFloat(uint64 nIndex) { float t=0; ReadNumber(nIndex, sizeof(t), &t); return t; }
bool ReadNumber(uint64 nIndex, int nBytes, void *target);
wxString ReadString(THSIZE nIndex, size_t nSize);
wxString ReadStringW(THSIZE nIndex, size_t nSize);
int find_bytes(uint64 nStart, int ls, const uint8* pb, int lb, int mode, uint8 (*cmp) (uint8, uint8));
bool Find(const char *text, THSIZE length, int type, bool caseSensitive, THSIZE &start, THSIZE &end);
bool FindHex(const wxByte *hex, size_t count, /*IN_OUT*/THSIZE &startpos, /*IN_OUT*/THSIZE &endpos);
Segment *GetSegment(uint64 nIndex, uint64 *pSegmentStart = NULL);
int FindSegment(THSIZE nIndex);
Segment* SegmentAt(size_t n) { return (n < segments.size()) ? segments[n] : NULL; }
void UpdateSegmentPointers();
int GetSerializedLength(THSIZE nOffset, THSIZE nSize);
void Serialize(THSIZE nOffset, THSIZE nSize, uint8 *target);
wxString Serialize(THSIZE nOffset, THSIZE nSize)
{
wxString str;
int len = GetSerializedLength(nOffset, nSize);
Serialize(nOffset, nSize, (uint8*)str.GetWriteBuf(len));
str.UngetWriteBuf(len);
return str;
}
bool ReplaceSerialized(THSIZE nAddress, THSIZE ToReplaceLength, SerialData &sInsert);
wxString SerializeData(uint8* pData, size_t nSize);
HexWnd *hw;
UndoManager *undo;
bool Undo();
bool Redo();
DWORD GetChange() const { return m_iChangeIndex; }
DWORD m_iChangeIndex; // not decremented on undo
protected:
bool IsByteLoaded(uint64 nIndex) const
{
return m_curSeg && m_curSeg->contains(nIndex, m_iCurSegOffset);
}
bool IsRangeLoaded(uint64 nIndex, uint64 nSize) const
{
return m_curSeg && m_curSeg->contains(nIndex, nSize, m_iCurSegOffset);
}
bool ReadFromSegment(uint64 nIndex, uint32 nSize, uint8 *target, Segment *seg);
//Segment* InsertSegment(uint64 nIndex, uint64 nSize, Segment *prev, Segment *next);
bool InsertSegment(THSIZE nAddress, Segment *ts); //! always returns true for now
bool InsertSegment2(size_t n, THSIZE base, Segment* s);
bool RemoveSegment(size_t n);
bool CanWriteInPlace();
friend class DataSource;
// features for std::map
public:
bool operator<(const HexDoc &d) const;
};
class ModifiedRange
{
public:
ModifiedRange *next;
uint64 size;
bool bModified;
};
//*****************************************************************************
//*****************************************************************************
// ByteIterator2
//*****************************************************************************
//*****************************************************************************
struct ByteIterator2
{
typedef std::bidirectional_iterator_tag iterator_category;
typedef wxByte value_type;
typedef wxFileOffset difference_type;
typedef const value_type *pointer;
typedef const value_type &reference;
THSIZE pos;
//wxFileOffset linepos;
HexDoc *doc;
uint8 *buffer;
ByteIterator2() { buffer = new uint8[0x4000]; doc = NULL; }
~ByteIterator2() { delete [] buffer; }
ByteIterator2(const ByteIterator2 &it)
{
this->operator =( it );
}
ByteIterator2(wxFileOffset pos0, HexDoc *doc0)
:pos(pos0), doc(doc0)
{
buffer = new uint8[0x4000];
Read();
}
ByteIterator2 & operator=(const ByteIterator2 & it)
{
bool read = true;
if (doc != NULL && ((pos ^ it.pos) >> 12) == 0)
read = false;
pos = it.pos;
doc = it.doc;
if (read)
Read();
return *this;
}
const value_type operator*()
{
//wxASSERT(pos < doc->GetSize());
return buffer[pos & 0x3FFF];
}
// pre-increment operator
ByteIterator2 & operator++()
{
++pos;
if (((DWORD)pos & 0x3FFF) == 0)
Read();
return *this;
}
// pre-decrement operator
ByteIterator2 & operator--()
{
wxASSERT(pos>0);
--pos;
if ((pos & 0x3FFF) == 0x3FFF)
Read();
return *this;
}
bool operator==(const ByteIterator2 & it) const
{
return (pos == it.pos);
}
bool operator!=(const ByteIterator2 & it) const
{
return ! (this->operator==(it)) ;
}
void Read()
{
int size = 0x4000;
THSIZE offset = pos & (THSIZE)~0x3FFF;
if (offset + size > doc->GetSize())
size = doc->GetSize() - offset;
doc->Read(offset, size, buffer);
}
bool AtEnd() const { return pos == doc->GetSize(); }
};
struct WordIterator2
{
typedef uint16 value_type;
THSIZE pos;
//wxFileOffset linepos;
HexDoc *doc;
uint16 *buffer;
enum {BUFFER_COUNT = 0x4000, BUFFER_MAX = 0x3FFF};
WordIterator2() { buffer = new value_type[BUFFER_COUNT]; doc = NULL; }
~WordIterator2() { delete [] buffer; }
WordIterator2(const WordIterator2 &it)
{
this->operator =( it );
}
WordIterator2(wxFileOffset pos0, HexDoc *doc0)
:pos(pos0), doc(doc0)
{
buffer = new value_type[BUFFER_COUNT];
Read();
}
WordIterator2 & operator=(const WordIterator2 & it)
{
bool read = true;
if (doc != NULL && ((pos ^ it.pos) >> 12) == 0)
read = false;
pos = it.pos;
doc = it.doc;
if (read)
Read();
return *this;
}
const value_type operator*()
{
//wxASSERT(pos < doc->GetSize()); //! Not relevant since pos is word position. Could fix it, but let's not.
return buffer[pos & BUFFER_MAX];
}
// pre-increment operator
WordIterator2 & operator++()
{
++pos;
if (((DWORD)pos & BUFFER_MAX) == 0)
Read();
return *this;
}
// pre-decrement operator
WordIterator2 & operator--()
{
wxASSERT(pos>0);
--pos;
if ((pos & BUFFER_MAX) == BUFFER_MAX)
Read();
return *this;
}
bool operator==(const WordIterator2 & it) const
{
return (pos == it.pos);
}
bool operator!=(const WordIterator2 & it) const
{
return ! (this->operator==(it)) ;
}
void Read()
{
int size = BUFFER_COUNT * sizeof(value_type);
THSIZE offset = (pos & (THSIZE)~BUFFER_MAX) * sizeof(value_type);
if (offset + size > doc->GetSize())
size = doc->GetSize() - offset;
doc->Read(offset, size, (uint8*)buffer);
}
bool AtEnd() const { return (pos * 2) + 1 >= doc->GetSize(); }
};
#endif // _MYDATA_H_