-
Notifications
You must be signed in to change notification settings - Fork 5
/
HexEditorDoc.cpp
335 lines (290 loc) · 7.65 KB
/
HexEditorDoc.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
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
/////////////////////////////////////////////////////////////////////////////
// HexEditorDoc.cpp : implementation of the CHexEditorDoc class
// ------------------------------------------------------------
// This code was written by Paul "Crazy" Frazee, and can be
// used royalty-free in any program. However, if the code is to be
// redistributed (in a modified state or not), I would appreciate
// an acknowledgement that the code was originally mine, or based
// off of mine. Please enjoy!
//
// You can reach me at [email protected], or CrazyFrazee911 (aim).
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "HexEditor.h"
#include "HexEditorDoc.h"
#include "HexEditorView.h"
#include "DLGInsertData.h"
#include "DLGEditData.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CHexEditorDoc
IMPLEMENT_DYNCREATE(CHexEditorDoc, CDocument)
BEGIN_MESSAGE_MAP(CHexEditorDoc, CDocument)
//{{AFX_MSG_MAP(CHexEditorDoc)
ON_COMMAND(ID_EDIT_INSERT, OnEditInsert)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CHexEditorDoc construction/destruction
CHexEditorDoc::CHexEditorDoc()
{
Reset();
}
CHexEditorDoc::~CHexEditorDoc()
{
Reset();
}
BOOL CHexEditorDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// clear out our buffers
Reset();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CHexEditorDoc serialization
void CHexEditorDoc::Serialize(CArchive& ar)
{
// save/load
if (ar.IsStoring())
{
// save
ar.Write( m_szDataBuffer.GetBuffer(0), m_nBufferLength );
}
else
{
// tell view
GetView()->Reset();
// create a progress bar
dlgProgress.Create( IDD_PROGRESS, GetView() );
dlgProgress.SetProgress( 20, "Loading File Into Data Buffer" ); // 20%
dlgProgress.m_wndAbort.EnableWindow( FALSE ); // no abort, sry
// load the data
LPTSTR pszBuffer = m_szDataBuffer.GetBuffer(ar.GetFile()->GetLength());
m_nBufferLength = ar.Read( pszBuffer, ar.GetFile()->GetLength() );
m_szDataBuffer.ReleaseBuffer();
dlgProgress.SetProgress( 100, "Done!" ); // 100%
dlgProgress.DestroyWindow();
}
}
void CHexEditorDoc::Reset()
{
m_szDataBuffer.Empty();
m_nBufferLength = 0;
}
long CHexEditorDoc::GetData( CString szData )
{
long nRet=0;
szData.MakeLower();
if( szData.Right( 1 ) == 'i' )
{
sscanf( szData.GetBuffer(0), "%di", &nRet );
} else if( szData.Right( 1 ) == 's' )
{
sscanf( szData.GetBuffer(0), "%cs", &nRet );
} else
{
if( szData.Right( 1 ) == 'h' )
#if MFC_VER < 0x0600
szData.TrimRight();
#else
szData.TrimRight( 'h' );
#endif
sscanf( szData.GetBuffer(0), "%x", &nRet );
}
return nRet;
}
CString CHexEditorDoc::SetData( long nData, BOOL fHex )
{
CString szRet;
if( fHex )
szRet.Format( _T("%x"), nData );
else
szRet.Format( _T("%d"), nData );
return szRet;
}
void CHexEditorDoc::InsertBytes()
{
// prompt for how many bytes and what pattern to fill it with
CDLGInsertData dlg;
if( dlg.DoModal() == IDOK )
{
// get how many bytes
int nAmt = 0, i;
sscanf( dlg.m_szInsertAmt.GetBuffer(0), "%d", &nAmt );
// get our data
long nData = GetData( dlg.m_szData );
// build the data array
int nDataCount = 0;
if( nData < 256 ) nDataCount = 1;
else if( nData < 65536 ) nDataCount = 2;
else if( nData < 16777216 ) nDataCount = 3;
else nDataCount = 4;
int* nDataArray = new int[nDataCount];
for( i=0; i < nDataCount; i++ )
{
nDataArray[i] = nData >> ( 8 * i );
}
// insert data
unsigned int nStart = GetView()->m_nCurSel;
#if MFC_VER < 0x0600
CString LeftString;
CString RightString;
char *Ptr;
#endif
if( nStart < 0 || nStart >= m_nBufferLength )
nStart = m_nBufferLength-1;
for( i=0; i < nAmt; i++ )
{
#if MFC_VER < 0x0600
//
// Need to write some code to insert here.
// debug error fix this
LeftString = RightString = m_szDataBuffer;
LeftString.Left(nStart);
Ptr = LeftString.GetBuffer(0);
Ptr += strlen(Ptr);
*Ptr++ = nDataArray[(i % nDataCount)];
LeftString.ReleaseBuffer(-1);
RightString.Left(nStart + 1);
m_szDataBuffer = LeftString + RightString;
#else
m_szDataBuffer.Insert( nStart, nDataArray[i%nDataCount] );
#endif
}
// update buffer length
m_nBufferLength += nAmt;
// redraw
GetView()->UpdateScrollbars();
GetView()->Invalidate( TRUE );
delete nDataArray;
}
}
void CHexEditorDoc::EditData( int nStart, int nEnd, BOOL fHex )
{
int nSize = nEnd - nStart + 1;
// get the data
// NOTE - CSTRING::MID DOES NOT WORK DUE TO ITS USE OF GETLENGTH,
// WHICH STOPS AT NULLS, WHICH ARENT THE END OF BUFFER D:
// IN FACT, MOST CSTRING OPERATIONS DONT WORK. I USE CSTRING FOR
// ITS EASY MEMORY MANAGEMENT
long nData = 0;
LPTSTR pData = m_szDataBuffer.GetBuffer(0);
CString szData;
for( int i = nStart; i <= nEnd; i++ )
szData += pData[i];
for( int i = 0; i < nSize; i++ )
nData += szData[nSize-i-1] << ( i * 8 ); //szData[nSize-i-1] << ( i * 8 );
szData = SetData( nData, fHex );
// create the dialog
CDLGEditData dlg;
// set input control
dlg.m_szInput = szData;
// set the max length
if( fHex )
dlg.m_nMaxLength = nSize * 2;
else
dlg.m_nMaxLength = 50;
// go modal
if( dlg.DoModal() != IDOK )
return;
// get the data
if( !fHex )
dlg.m_szInput += 'i';
nData = GetData( dlg.m_szInput );
// create the buffer to insert
szData.Empty();
int nDataCount = 0;
if( nData < 256 ) nDataCount = 1;
else if( nData < 65536 ) nDataCount = 2;
else if( nData < 16777216 ) nDataCount = 3;
else nDataCount = 4;
for(int i=0; i < nDataCount; i++ )
{
szData += (char) ( nData >> ( 8 * (nDataCount - i - 1) ) );
}
// insert the n_ew data
for(int i = 0; i < nSize; i++ )
{
if( i >= nDataCount )
pData[nStart + i] = 0;
else
pData[nStart + i] = szData[i];
}
m_szDataBuffer.ReleaseBuffer();
}
void CHexEditorDoc::EditString( int nStart, int nEnd )
{
// get the data
int nSize = nEnd - nStart + 1;
LPTSTR pData = m_szDataBuffer.GetBuffer(0);
CString szData;
for( int i = nStart; i <= nEnd; i++ )
szData += pData[i];
// create the dialog
CDLGEditData dlg;
dlg.m_szInput = szData;
dlg.m_nMaxLength = nSize;
if( dlg.DoModal() != IDOK )
return;
// insert the n_ew data
for(int i = 0; i < nSize, i < dlg.m_szInput.GetLength(); i++ )
{
pData[nStart + i] = dlg.m_szInput[i];
}
m_szDataBuffer.ReleaseBuffer();
}
void CHexEditorDoc::DeleteData( int nStart, int nEnd )
{
#if MFC_VER < 0x0600
CString LeftString = m_szDataBuffer;
CString RightString = m_szDataBuffer;
LeftString.Left(nStart + 1);
RightString.Left(nStart + nEnd);
m_szDataBuffer = LeftString + RightString;
#else
m_szDataBuffer.Delete( nStart, nEnd );
#endif
m_nBufferLength -= nEnd - nStart + 1;
}
CHexEditorView* CHexEditorDoc::GetView()
{
// find the first view - if there are no views
// we must return NULL
POSITION pos = GetFirstViewPosition();
if (pos == NULL)
return NULL;
// find the first view that is a CRichEditView
CView* pView;
while (pos != NULL)
{
pView = GetNextView(pos);
if (pView->IsKindOf(RUNTIME_CLASS(CHexEditorView)))
return (CHexEditorView*) pView;
}
// can't find one--return NULL
return NULL;
}
/////////////////////////////////////////////////////////////////////////////
// CHexEditorDoc diagnostics
#ifdef _DEBUG
void CHexEditorDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CHexEditorDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CHexEditorDoc commands
void CHexEditorDoc::OnEditInsert()
{
InsertBytes();
}