-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDiskDataDoc.cpp
340 lines (252 loc) · 7.86 KB
/
DiskDataDoc.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
336
337
338
339
// DiskDataDoc.cpp : implementation of the CDiskDataDoc class
//
#include "stdafx.h"
#include "DiskData.h"
#include "DiskDataDoc.h"
#include "CntrItem.h"
#include "Ntmsapi.h"
#include "winioctl.h"
#include "DiskHexCtrl.h" // Specific Control Entry
#include "DiskSelect.h" // Disk/Drive Select OnNewDocument
#include "DiskAnalysis.h"
#include "GoToSector.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
//#define DOC_EMPTY_STR "this is an empty document... open a file!"
// CDiskDataDoc
IMPLEMENT_DYNCREATE(CDiskDataDoc, COleDocument)
BEGIN_MESSAGE_MAP(CDiskDataDoc, COleDocument)
// Enable default OLE container implementation
ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE, COleDocument::OnUpdatePasteMenu)
ON_UPDATE_COMMAND_UI(ID_EDIT_PASTE_LINK, COleDocument::OnUpdatePasteLinkMenu)
ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_CONVERT, COleDocument::OnUpdateObjectVerbMenu)
ON_COMMAND(ID_OLE_EDIT_CONVERT, COleDocument::OnEditConvert)
ON_UPDATE_COMMAND_UI(ID_OLE_EDIT_LINKS, COleDocument::OnUpdateEditLinksMenu)
ON_COMMAND(ID_OLE_EDIT_LINKS, COleDocument::OnEditLinks)
ON_UPDATE_COMMAND_UI_RANGE(ID_OLE_VERB_FIRST, ID_OLE_VERB_LAST, COleDocument::OnUpdateObjectVerbMenu)
ON_COMMAND(ID_VIEW_NEXTSECTOR, OnViewNextsector)
ON_COMMAND(ID_VIEW_PREVIOUSSECTOR, OnViewPrevioussector)
ON_COMMAND(ID_VIEW_GOTOSECTOR, OnViewGotosector)
END_MESSAGE_MAP()
// CDiskDataDoc construction/destruction
CDiskDataDoc::CDiskDataDoc() : m_strData(NULL), m_nSize(512)
{
// Use OLE compound files
EnableCompoundFile();
m_strData = NULL;
m_bShowSelectDialog = true;
m_dwLastBytesRead = 0;
m_dwLastReq = 0;
m_iOffset = 0;
}
CDiskDataDoc::~CDiskDataDoc()
{
}
// if OpenDocument() is called with a string path name this is called...!! (see OnNewDocument() )
BOOL CDiskDataDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
//return TRUE;
//if (!COleDocument::OnOpenDocument(lpszPathName))
// return FALSE;
//CDiskAnalysis disk;
//BYTE byData[1024];
//disk.GetMFTRecord('C', 23437, byData, 1024);
hDisk = CreateFile(lpszPathName, GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0,0);
if( hDisk == INVALID_HANDLE_VALUE )
{
AfxMessageBox(IDS_INVALID_HANDLE, MB_ICONSTOP,0);
return FALSE;
}
CString str;
str.AppendFormat("'%s'", lpszPathName);
this->m_strTitle = lpszPathName;
m_iCurrentOffset = 0; // Default As Start Of File Here
//m_nSize = 2048;
return ReadDiskData(m_nSize, 0);
//return TRUE;
}
// if OpenDocument is called with a NULL path name this is called (see OnOpenDocument() )
BOOL CDiskDataDoc::OnNewDocument()
{
if (!COleDocument::OnNewDocument())
return FALSE;
ASSERT(false); // should never be called in this app at least at the moment !! OnOpenDocument !!
//hDisk = CreateFile(m_strOpenPath, GENERIC_READ,
// FILE_SHARE_READ|FILE_SHARE_WRITE,
// NULL, OPEN_EXISTING, 0,0);
//if( hDisk == INVALID_HANDLE_VALUE )
//{
// AfxMessageBox(IDS_INVALID_HANDLE, MB_ICONSTOP,0);
// return FALSE;
//}
//m_iCurrentOffset = 0; // Default As Start Of File Here
//return ReadDiskData(m_nSize, 0);
return true;
}
// CDiskDataDoc serialization
void CDiskDataDoc::Serialize(CArchive& ar)
{
CFile *pFile = ar.GetFile();
ASSERT(pFile != NULL);
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
}
// Calling the base class COleDocument enables serialization
// of the container document's COleClientItem objects.
COleDocument::Serialize(ar);
// activate the first one
if (!ar.IsStoring())
{
POSITION posItem = GetStartPosition();
if (posItem != NULL)
{
CDocItem* pItem = GetNextItem(posItem);
POSITION posView = GetFirstViewPosition();
COleDocObjectItem *pDocObjectItem = DYNAMIC_DOWNCAST(COleDocObjectItem, pItem);
if (posView != NULL && pDocObjectItem != NULL)
{
CView* pView = GetNextView(posView);
pDocObjectItem->DoVerb(OLEIVERB_SHOW, pView);
}
}
}
}
// CDiskDataDoc diagnostics
#ifdef _DEBUG
void CDiskDataDoc::AssertValid() const
{
COleDocument::AssertValid();
}
void CDiskDataDoc::Dump(CDumpContext& dc) const
{
COleDocument::Dump(dc);
}
#endif //_DEBUG
// CDiskDataDoc commands
void CDiskDataDoc::OnCloseDocument()
{
// Close Handle To Disks
delete []m_strData;
BOOL bClosed = CloseHandle(hDisk);
//if (!bClosed) { AfxMessageBox("Failed To Close Disk Handle", 0,0); }
COleDocument::OnCloseDocument();
}
// Read in data from file starting from iStartOffset of size iReadSize
bool CDiskDataDoc::ReadDiskData(int iReadSize, __int64 iStartOffset, BOOL bAbsolute)
{
DWORD lpdBytes = 0;
BOOL bSuccess;
CString strText = "";
CString strDat = "\n"; // Hex String
LARGE_INTEGER liMoveTo;
PLARGE_INTEGER p;
p = (LARGE_INTEGER*)VirtualAlloc(NULL, 1*sizeof(LARGE_INTEGER), MEM_COMMIT, PAGE_READWRITE);
// we are trying to go past EOF - not allowed so return quietly :-))
if((( m_dwLastBytesRead != m_dwLastReq ) ) // || bAbsolute
&& ( iStartOffset >= 0 ) )
return false;
// we are trying to go before SOF - not allowed so return quietly
p->HighPart = 0;
p->LowPart = 0;
p->QuadPart = 0;
liMoveTo.HighPart = 0;
liMoveTo.LowPart = 0;
liMoveTo.QuadPart = 0;
SetFilePointerEx(hDisk, liMoveTo, p, FILE_CURRENT);
if( (p->QuadPart + (iStartOffset*iReadSize)) < 0)
return false;
// Set The File Pointer To iStartOffset
liMoveTo.HighPart = 0;
liMoveTo.LowPart = 0;
// if we read less than requested last time and we are going back
// only read what we read last time
if(( m_dwLastBytesRead != m_dwLastReq ) && ( iStartOffset < 0 ))
{
DWORD dwDiff = (m_dwLastReq - m_dwLastBytesRead);
liMoveTo.QuadPart = (iStartOffset*iReadSize); // bytes
liMoveTo.QuadPart += dwDiff;
}
else
{
liMoveTo.QuadPart = (iStartOffset*iReadSize);
}
delete []m_strData;
m_strData = new BYTE[iReadSize];
memset(m_strData, ' ', iReadSize);
if( (m_iCurrentOffset*m_nSize)+(liMoveTo.QuadPart) < 0 )
return false;
if(iStartOffset < 0) // relative move back
{
if(!(SetFilePointerEx(hDisk, liMoveTo, NULL, FILE_CURRENT)))
{
AfxMessageBox("Failed to set file pointer");
DWORD dwErr = GetLastError();
TRACE1("\nSet file pointer failed in ReadDiskData : Error %i", dwErr);
return false;
}
}
if(bAbsolute) // absolute move from start of file
{
if(!(SetFilePointerEx(hDisk, liMoveTo, NULL, FILE_BEGIN)))
{
AfxMessageBox("Failed to set file pointer");
DWORD dwErr = GetLastError();
TRACE1("\nSet file pointer failed in ReadDiskData : Error %i", dwErr);
return false;
}
}
// Read in 512bytes from the open disk
bSuccess = ReadFile(hDisk, m_strData, iReadSize, &lpdBytes, NULL);
if(!bSuccess) { AfxMessageBox("Failed To Read Data", 0,0); return FALSE; }
if((lpdBytes == 0) && bSuccess) // EOF
{
liMoveTo.HighPart = p->HighPart;
liMoveTo.LowPart = p->LowPart;
liMoveTo.QuadPart = (p->QuadPart - m_dwLastBytesRead);
SetFilePointerEx(hDisk, liMoveTo, NULL, FILE_BEGIN);
bSuccess = ReadFile(hDisk, m_strData, m_dwLastBytesRead, &lpdBytes, NULL);
// restore org file pointer
return false;
}
//if(lpdBytes < iReadSize) // EOF possibly - so fill the empty
// Get Current Position
p->HighPart = 0;
p->LowPart = 0;
p->QuadPart = 0;
liMoveTo.HighPart = 0;
liMoveTo.LowPart = 0;
liMoveTo.QuadPart = 0;
SetFilePointerEx(hDisk, liMoveTo, p, FILE_CURRENT);
m_iCurrentOffset = (p->QuadPart/m_nSize);
m_dwLastBytesRead = lpdBytes; // save in case we read less than requested
m_dwLastReq = iReadSize;
m_iOffset = (((p->QuadPart%m_nSize) != 0) ? 1 : 0);
m_iOffset += (p->QuadPart/m_nSize);
return true;
}
void CDiskDataDoc::OnViewNextsector()
{
if(ReadDiskData(m_nSize, 0 ) )
UpdateAllViews(NULL, -1);
}
void CDiskDataDoc::OnViewPrevioussector()
{
if(ReadDiskData(m_nSize,-2))
UpdateAllViews(NULL, -1);
}
void CDiskDataDoc::OnViewGotosector()
{
CGoToSector CSector;
if(CSector.DoModal() == IDOK)
{
if(ReadDiskData(m_nSize, CSector.iSectorNo, true))
{ UpdateAllViews(NULL, -1); }
}
}