This repository has been archived by the owner on Mar 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNidSize.cpp
346 lines (292 loc) · 9.04 KB
/
NidSize.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
340
341
342
343
344
345
346
// NidSize.cpp : implementation file
//
#include "stdafx.h"
#include "NDBView.h"
#include "NidSize.h"
// CNidSize dialog
IMPLEMENT_DYNAMIC(CNidSize, CDialog)
CNidSize::CNidSize(NID parentNid, NID nid, NDBViewer * pNDBViewer, CNDBViewDlg * pNDBViewDlg, CWnd* pParent)
: CNDBViewChildDlg(pNDBViewDlg, CNidSize::IDD, pParent)
{
m_nid = nid;
m_nidParent = parentNid;
m_pNDBViewer = pNDBViewer;
m_pBitmap = 0;
m_fLookupError = false;
if(parentNid == 0)
{
if(!m_pNDBViewer->LookupNID(m_nid, m_dataBID, m_subnodeBID))
{
m_dataBID = 0;
m_subnodeBID = 0;
m_fLookupError = true;
}
}
else
{
if(!m_pNDBViewer->LookupSubnodeNID(parentNid, nid, m_dataBID, m_subnodeBID))
{
m_dataBID = 0;
m_subnodeBID = 0;
m_fLookupError = true;
}
}
}
CNidSize::~CNidSize()
{
if(m_pBitmap)
delete m_pBitmap;
}
void CNidSize::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CNidSize, CDialog)
ON_WM_PAINT()
ON_WM_CREATE()
END_MESSAGE_MAP()
// CNidSize message handlers
void MarkSpaceAsOccupied(CB * usedSpace, // array of pixels
CB cbPerPixel, // number of bytes each pixel represents in the file
int, // number of pixels, or the dimension of freeSpace
IB startAddress, // start area to mark allocated
IB endAddress // end of area to mark allocated (not inclusive)
)
{
IB current = startAddress;
if(startAddress >= endAddress) return; // no op
while(current < endAddress)
{
IB end = (endAddress <= ((current / cbPerPixel) * cbPerPixel + cbPerPixel) ? endAddress : ((current / cbPerPixel) * cbPerPixel + cbPerPixel));
usedSpace[current / cbPerPixel] += (end - current);
current = end;
}
}
// recursive function to mark the space occupied by a BID as used, and to
// recurse on child BIDs for S, X and XXBlocks
void CNidSize::EachBID(BID b, CB * pRunningTotal, int * pNumBlocks, CB * usedSpace, CB cbPerPixel, int numPixel)
{
BREF bref;
CB cb;
UINT cRef;
BYTE * pBlock;
BLOCKTRAILER bt;
if(b == 0) return;
// If we can't find the referenced BID, just skip it
if(!m_pNDBViewer->LookupBID(b, bref, cb, cRef))
return;
MarkSpaceAsOccupied(usedSpace, cbPerPixel, numPixel, bref.ib, bref.ib+CbAlignDisk(cb));
*pRunningTotal += CbAlignDisk(cb);
(*pNumBlocks)++;
if(!BIDIsExternal(b) && m_pNDBViewer->FValidBlock(bref.ib, cb, bref.bid))
{
pBlock = new BYTE[(UINT)cb];
m_pNDBViewer->ReadBlock(pBlock, (UINT)cb, &bt, bref.ib, CbAlignDisk(cb));
if(pBlock[0] == btypeXB)
{
// recurse on BID's referenced by this XBlock
XBLOCK * pxb = (XBLOCK*)pBlock;
for(UINT i = 0; i < XBEnt(*pxb, (UINT)cb); i++)
{
EachBID(pxb->rgbid[i], pRunningTotal, pNumBlocks, usedSpace, cbPerPixel, numPixel);
}
}
else
{
ASSERT(pBlock[0] == btypeSB);
// recurse on BID's referenced by this SBlock
SBLOCK * pSBlock = (SBLOCK*)pBlock;
if(pSBlock->cLevel == 0)
{
SLENTRY * slEntry = pSBlock->rgsl;
for(UINT i = 0; i < SBLEnt(*pSBlock, (UINT)cb); i++, slEntry++)
{
EachBID(slEntry->bidData, pRunningTotal, pNumBlocks, usedSpace, cbPerPixel, numPixel);
EachBID(slEntry->bidSub, pRunningTotal, pNumBlocks, usedSpace, cbPerPixel, numPixel);
}
}
else
{
SIENTRY * siEntry = pSBlock->rgsi;
for(UINT i = 0; i < SBIEnt(*pSBlock, (UINT)cb); i++, siEntry++)
{
EachBID(siEntry->bid, pRunningTotal, pNumBlocks, usedSpace, cbPerPixel, numPixel);
}
}
}
delete [] pBlock;
}
}
void CNidSize::OnPaint()
{
CPaintDC dc(this); // device context for painting
CDC dcMem;
RECT client;
CBitmap * pOld = NULL;
GetClientRect(&client);
if(!m_pNDBViewer)
return CDialog::OnPaint();
dcMem.CreateCompatibleDC(&dc);
if(!m_pBitmap)
{
// First paint, we do the actual work
CWaitCursor wait;
WCHAR buffer[255];
int numPixel = client.right * (client.bottom * 3/4);
CB * usedSpace = new CB[numPixel];
CB cbPixel = (m_pNDBViewer->GetHeader().root.ibFileEof / numPixel) + 1; // round up
CB dataBID = 0; // running totals
CB subnodeBID = 0; // running totals
int dataBIDnum = 0;
int subnodeBIDnum = 0;
int iFullR = GetRValue(c_FullNID);
int iFullG = GetGValue(c_FullNID);
int iFullB = GetBValue(c_FullNID);
int iFreeR = GetRValue(c_FreeNID);
int iFreeG = GetGValue(c_FreeNID);
int iFreeB = GetBValue(c_FreeNID);
for(int i = 0; i < numPixel; i++)
usedSpace[i] = CB(0);
EachBID(m_dataBID, &dataBID, &dataBIDnum, usedSpace, cbPixel, numPixel);
EachBID(m_subnodeBID, &subnodeBID, &subnodeBIDnum, usedSpace, cbPixel, numPixel);
if(!m_fLookupError)
{
wsprintf(buffer, L"%I64u %s (%u blocks)", ULLSize(dataBID), WSZSize(dataBID), dataBIDnum);
GetDlgItem(IDC_STATIC1)->SetWindowText(buffer);
wsprintf(buffer, L"%I64u %s (%u blocks)", ULLSize(subnodeBID), WSZSize(subnodeBID), subnodeBIDnum);
GetDlgItem(IDC_STATIC2)->SetWindowText(buffer);
wsprintf(buffer, L"%I64u %s (%u blocks)", ULLSize(dataBID + subnodeBID), WSZSize(dataBID + subnodeBID), dataBIDnum+subnodeBIDnum);
GetDlgItem(IDC_STATIC3)->SetWindowText(buffer);
}
else
{
GetDlgItem(IDC_STATIC1)->SetWindowText(L"Lookup Error");
GetDlgItem(IDC_STATIC2)->SetWindowText(L"Lookup Error");
GetDlgItem(IDC_STATIC3)->SetWindowText(L"Lookup Error");
}
// allocate the bitmap
m_pBitmap = new CBitmap();
m_pBitmap->CreateCompatibleBitmap(&dc, client.right, client.bottom * 3/4);
pOld = dcMem.SelectObject(m_pBitmap);
for(int j = 0; j < (client.bottom*3/4); j++)
{
for(int i = 0; i < client.right; i++)
{
// if this pixel extends past eof, just draw the eof pixel
// the confusing +1 is because we want the end of the region mapped
// by this pixel, which is the start of the region mapped by the next
// pixel
if((j * client.right + i + 1) * cbPixel <= m_pNDBViewer->GetHeader().root.ibFileEof)
{
double diff = (double)usedSpace[(j * client.right) + i] / (double)cbPixel;
if(diff < 0) diff = 0.0;
// make sure even the smallest bit of free space shows up
if(diff > 0.0 && diff < c_minShadeNID) diff = c_minShadeNID;
if(diff > 1.0) diff = 1.0;
COLORREF c = RGB(
(int)((double)iFreeR - (iFreeR*diff) + (iFullR*diff)) & 0x00FF,
(int)((double)iFreeG - (iFreeG*diff) + (iFullG*diff)) & 0x00FF,
(int)((double)iFreeB - (iFreeB*diff) + (iFullB*diff)) & 0x00FF
);
dcMem.SetPixel(i, j, c);
}
else
{
dcMem.SetPixel(i, j, c_NIDPastEOF);
}
}
}
delete [] usedSpace;
}
else
{
pOld = dcMem.SelectObject(m_pBitmap);
}
dc.BitBlt(client.left, client.bottom/4, client.right, client.bottom, &dcMem, 0, 0, SRCCOPY);
dcMem.SelectObject(pOld);
}
int CNidSize::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
WCHAR buffer[255];
RECT client;
int numPixel;
int numPixelNeeded;
int extraLines;
CB cbPixel;
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
if(m_pNDBViewer)
{
GetClientRect(&client);
numPixel = client.right * (client.bottom * 3 / 4);
cbPixel = (m_pNDBViewer->GetHeader().root.ibFileEof / numPixel) + 1;
wsprintf(buffer, L"NID 0x%X Size (1 pixel = %I64u bytes)", m_nid, cbPixel);
SetWindowText(buffer);
// The visual effects of the above integer rounding can be dramatic
// on small files
numPixelNeeded = (int)(m_pNDBViewer->GetHeader().root.ibFileEof / cbPixel);
extraLines = (numPixel - numPixelNeeded) / client.right;
if(extraLines > 0)
{
RECT r;
GetWindowRect(&r);
SetWindowPos(NULL, 0, 0, r.right - r.left, r.bottom - r.top - extraLines, SWP_NOMOVE | SWP_NOZORDER);
}
}
return 0;
}
// NidSize.cpp : implementation file
//
#include "stdafx.h"
#include "NDBView.h"
#include "NidSize.h"
// CGetNid dialog
IMPLEMENT_DYNAMIC(CGetNid, CDialog)
CGetNid::CGetNid(CWnd* pParent /*=NULL*/)
: CDialog(CGetNid::IDD, pParent)
, m_nid(_T(""))
, m_parentNid (_T(""))
, m_hid(_T(""))
{
}
CGetNid::~CGetNid()
{
}
void CGetNid::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Text(pDX, IDC_EDIT1, m_parentNid);
DDX_Text(pDX, IDC_EDIT4, m_nid);
DDX_Text(pDX, IDC_EDIT2, m_hid);
DDX_Control(pDX, IDC_COMBO1, m_ccb);
DDX_CBIndex(pDX, IDC_COMBO1, m_type);
DDX_Control(pDX, IDC_EDIT2, m_hidCtrl);
}
BEGIN_MESSAGE_MAP(CGetNid, CDialog)
ON_CBN_SELCHANGE(IDC_COMBO1, &CGetNid::OnCbnSelchangeCombo1)
END_MESSAGE_MAP()
// CGetNid message handlers
BOOL CGetNid::OnInitDialog()
{
CDialog::OnInitDialog();
// Add list of page inspectors to the drop down box
for(int i = 0; i < inspLTPMaxInspector; i++)
{
m_ccb.InsertString(i, ltplistvalues[i]);
}
m_ccb.SetCurSel(0);
m_hidCtrl.EnableWindow(FALSE);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CGetNid::OnCbnSelchangeCombo1()
{
if(m_ccb.GetCurSel() != inspBTH)
{
m_hidCtrl.EnableWindow(FALSE);
}
else
{
m_hidCtrl.EnableWindow(TRUE);
}
}