-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCVolReader.cpp
428 lines (366 loc) · 11.1 KB
/
CVolReader.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
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
#include "stdafx.h"
#include "CVolReader.h"
#include "CMemoryStreamReader.h"
#include "GlobalFunctions.h"
extern int g_cLocks;
ULONG __stdcall CVolReader::AddRef()
{
return ++m_cRef;
}
ULONG __stdcall CVolReader::Release()
{
if (--m_cRef != 0)
return m_cRef;
delete this;
return 0;
}
HRESULT __stdcall CVolReader::QueryInterface(REFIID riid, void** ppv)
{
if(riid == IID_IUnknown)
*ppv = (IUnknown*)this;
else if(riid == IID_ArchiveReader)
*ppv = (ArchiveReader*)this;
else
{
*ppv = NULL;
return E_NOINTERFACE;
}
AddRef();
return S_OK;
}
// ArchiveReader
// *************
HRESULT CVolReader::get_NumFiles(int *numFiles)
{
*numFiles = this->numFiles;
return S_OK;
}
HRESULT CVolReader::get_FileName(int index, BSTR *fileName)
{
char *string;
int stringLength;
// Cache string address
string = (char*)stringBuffer + indexTable[index].fileNameOffset;
stringLength = strlen(string);
// Allocate space for the string
SysReAllocStringLen(fileName, NULL, stringLength);
// Convert the string to Unicode
MultiByteToWideChar(CP_ACP, 0, string, stringLength,
*fileName, stringLength);
return S_OK;
}
HRESULT CVolReader::OpenStreamRead(BSTR fileName, StreamReader **stream)
{
int fileIndex;
HRESULT hr;
int bAttachToBuffer = 0;
LPVOID buffer;
// Initialize returned stream pointer to NULL
*stream = NULL;
// Lookup the filename in the index
fileIndex = GetFileIndex(fileName);
// Check if the file was not found
if (fileIndex == -1)
{
// File was not found
PostErrorMsg(L"File not found in archive.");
return E_INVALIDARG;
}
// Store a pointer to the data buffer
buffer = dataBuffer[fileIndex];
// Check if data is loaded into memory
if (buffer == NULL)
{
// Data is not loaded into memory.
// Check if attached stream does not exist
if (attachedStream == NULL)
{
// No data source. Failed.
PostErrorMsg(L"No data source.");
return E_FAIL;
}
// Load the data from the stream
// *****************************
SectionHeader sectHead;
int numBytesRead;
// Seek to begining of internal file data
attachedStream->Seek(streamStartOffset + indexTable[fileIndex].dataOffset);
// Read the "VBLK" section header
attachedStream->Read(sizeof(sectHead), (int)§Head, &numBytesRead);
if (sectHead.tag != 'KLBV') // "VBLK"
{
PostErrorMsg(L"Invalid format. VBLK not found.");
return E_FAIL; // Error. Format error.
}
//if (sectHead.sectionSize != indexTable[fileIndex].fileSize)
// return 1; // Error. Format error.
// Allocate space for the file data
buffer = new char[sectHead.sectionSize];
// Read the file data
attachedStream->Read(sectHead.sectionSize, (int)buffer, &numBytesRead);
// Mark the buffer as discardable
bAttachToBuffer = 1;
}
try
{
// Determine which type of stream reader to construct to read the file
switch(indexTable[fileIndex].encoding)
{
case 0: // Raw uncompressed data
// Copy the data into a memory stream
CMemoryStreamReader *rawStream;
rawStream = new CMemoryStreamReader(indexTable[fileIndex].fileSize,
(char*)buffer,
bAttachToBuffer);
if (rawStream == NULL)
{
if (bAttachToBuffer == 1)
delete [] buffer; // Delete discardable buffer
PostErrorMsg(L"Could not create memory stream reader. Out of memory.");
return E_OUTOFMEMORY;
}
hr = rawStream->QueryInterface(IID_StreamReader, (void**)stream);
rawStream->Release();
return hr;
break;
case 1: // Unknown **TODO** Find out
break;
case 2: // Unknown **TODO** Find out
break;
case 3: // AdaptiveHuffman+RepeatedBlocks
// **TODO** Implement
break;
default:
// **TODO** Implement
break;
}
}
catch(...)
{
// Failed to create a StreamReader object
// Check if buffer is discardable
if (bAttachToBuffer == 1)
delete [] buffer;
PostErrorMsg(L"Could not create memory stream reader.");
return E_FAIL;
}
// Failed to open the stream
if (bAttachToBuffer == 1)
delete [] buffer; // Release the discardable buffer
PostErrorMsg(L"Could not open stream.");
return E_FAIL;
}
// Class specific
// **************
CVolReader::CVolReader(SeekableStreamReader *inStream, int bAttachToStream) : m_cRef(1)
{
numFiles = 0;
stringBuffer = NULL;
indexTable = NULL;
dataBuffer = NULL;
streamStartOffset = 0;
// Check if stream is to be attached or loaded completely
if (bAttachToStream)
{
attachedStream = inStream; // Record attached stream
inStream->AddRef();
}
else
attachedStream = NULL; // No attached stream
if (LoadVol(inStream, bAttachToStream) != 0)
{
// Error loading file
FreeMemory();
throw 0; // Abort object construction
}
g_cLocks++;
}
CVolReader::~CVolReader()
{
FreeMemory();
g_cLocks--;
// Check if an attached stream exists
if (attachedStream)
{
// Release attached stream
attachedStream->Release();
}
}
void CVolReader::FreeMemory()
{
unsigned int i;
if (stringBuffer != NULL)
delete [] stringBuffer;
if (indexTable != NULL)
delete [] indexTable;
if (dataBuffer != NULL)
{
for (i = 0; i < numFiles; i++)
{
// Check if file data is buffered in memory
if (dataBuffer[i] != NULL)
delete [] dataBuffer[i]; // Delete buffered memory
}
// Delete list of memory buffers
delete [] dataBuffer;
}
// Reset variables
stringBuffer = NULL;
indexTable = NULL;
dataBuffer = NULL;
numFiles = 0;
}
// - Reads in VOL file data from the input stream.
// - Returns 0 if successful and nonzero if an error occured.
// - Memory cleanup after errors is not supported by this function
// so if an error is detected, FreeMemory should be called after
// this function returns. Note that this requires memory pointers
// to be initialized to NULL before calling LoadVol.
// - bAttachToStream will cause only the index to be read from the
// stream since the stream will later be available when loading
// internal archive file data.
int CVolReader::LoadVol(SeekableStreamReader *inStream, int bAttachToStream)
{
SectionHeader sectHead;
int numBytesRead;
unsigned int totalHeaderSize;
unsigned int stringTableSize;
unsigned int indexTableSize;
unsigned int maxOffset;
unsigned int i, j;
try{
// Record the starting stream position for later seeking
// (Note: This allows VOL contents to be part of a larger stream)
inStream->get_ReadOffset(&streamStartOffset);
// Read the "VOL " section
inStream->Read(sizeof(sectHead), (int)§Head, &numBytesRead);
if (sectHead.tag != ' LOV') // "Vol "
throw L"Format error. 'VOL ' tag not found.";
totalHeaderSize = sectHead.sectionSize;
// Read the "volh" section
inStream->Read(sizeof(sectHead), (int)§Head, &numBytesRead);
if (sectHead.tag != 'hlov') // "volh"
throw L"Format error. 'volh' tag not found";
if (sectHead.sectionSize != 0)
throw L"Format error. 'volh' section size is not 0.";
// Read the "vols" section
inStream->Read(sizeof(sectHead), (int)§Head, &numBytesRead);
if (sectHead.tag != 'slov') // "vols"
throw L"Format error. 'vols' section not found.";
stringTableSize = sectHead.sectionSize - 4;
// Read the actual size of the string data
inStream->Read(4, (int)&actualStringDataLen, &numBytesRead);
if (actualStringDataLen > stringTableSize)
throw L"Format error. String data exceeds string section size.";
// Allocate space for the string table
stringBuffer = new char[stringTableSize];
// Read the string table
inStream->Read(stringTableSize, (int)stringBuffer, &numBytesRead);
// Read the "voli" section
inStream->Read(sizeof(sectHead), (int)§Head, &numBytesRead);
if (sectHead.tag != 'ilov') // "voli"
throw L"Format error. 'voli' tag not found.";
indexTableSize = sectHead.sectionSize;
//if (totalHeaderSize != stringTableSize + 4 + indexTableSize + sizeof(sectHead)*3)
// return 1; // Error. Format error.
// Calculate number of index entries stored in Vol file (round up)
numIndexEntries = (sectHead.sectionSize + sizeof(IndexEntry) - 1) / sizeof(IndexEntry);
// Allocate space for the index table
indexTable = new IndexEntry[numIndexEntries];
// Read in the index table
inStream->Read(sectHead.sectionSize, (int)indexTable, &numBytesRead);
// Fnid the number of used entries
numFiles = sectHead.sectionSize / sizeof(IndexEntry);
while ((numFiles > 0) && (indexTable[numFiles-1].boolUsed != 1))
numFiles--;
// Allocate space for data buffer pointers
dataBuffer = new LPVOID[numFiles];
memset(dataBuffer, 0, numFiles*4);
// Read in file data blocks
for (i = 0; i < numFiles; i++)
{
// Seek to begining of internal file data
inStream->Seek(streamStartOffset + indexTable[i].dataOffset);
// Read the "VBLK" section header
inStream->Read(sizeof(sectHead), (int)§Head, &numBytesRead);
if (sectHead.tag != 'KLBV') // "VBLK"
throw L"Format error. 'VBLK' tag not found at data offset";
//if (sectHead.sectionSize != indexTable[i].fileSize)
// return 1; // Error. Format error.
if (!bAttachToStream)
{
// Allocate space for the file data
dataBuffer[i] = new char[sectHead.sectionSize];
// Read the file data
inStream->Read(sectHead.sectionSize, (int)dataBuffer[i], &numBytesRead);
}
}
// Determine maximum offset of VOL data
maxOffset = 0;
j = 0;
for (i = 0; i < numFiles; i++)
{
j = indexTable[i].dataOffset + indexTable[i].fileSize;
if (j > maxOffset)
maxOffset = j;
}
if (maxOffset < totalHeaderSize + sizeof(sectHead) + 1)
maxOffset = totalHeaderSize + sizeof(sectHead) + 1;
// Seek to end of VOL data
inStream->Seek(streamStartOffset + maxOffset);
}
catch(WCHAR *errorMsg)
{
PostErrorMsg(errorMsg);
return -1; // Error
}
catch(...)
{
return -1; // Error
}
return 0; // Success
}
int CVolReader::GetFileIndex(BSTR fileName)
{
int startIndex, endIndex, middleIndex;
int result;
int stringLength;
char *tempString;
// Allocate space for a temporary ASCII fileName string
stringLength = SysStringLen(fileName);
tempString = new char[stringLength+1];
tempString[stringLength] = 0; // NULL terminate it
// Convert the BSTR to ASCII
WideCharToMultiByte(CP_ACP, 0, fileName, stringLength, tempString, stringLength, 0, 0);
// Search index for file
startIndex = 0;
endIndex = numFiles - 1;
while (startIndex <= endIndex)
{
// Get the midpoint
middleIndex = (startIndex + endIndex) >> 1;
// Check if strings match
result = strncmp((char*)stringBuffer + indexTable[middleIndex].fileNameOffset,
tempString,
stringLength);
if (result == 0)
{
// String match
delete [] tempString;
return middleIndex;
}
if (result < 0)
{
// Search top half of index table
startIndex = middleIndex + 1;
}
else // result > 0
{
// Search bottom half of index table
endIndex = middleIndex - 1;
}
}
// File not found
delete [] tempString;
return -1;
}