2004-05-29 22:48:08 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Name: FileUnpacker.cpp
|
|
|
|
// Author: Rich Cross
|
|
|
|
// Contact: rich@wildfiregames.com
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2004-06-03 20:38:14 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2004-05-29 22:48:08 +02:00
|
|
|
#include "FileUnpacker.h"
|
2004-08-11 22:18:49 +02:00
|
|
|
#include "res/vfs.h"
|
2004-05-29 22:48:08 +02:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// CFileUnpacker constructor
|
2004-09-19 20:51:19 +02:00
|
|
|
CFileUnpacker::CFileUnpacker()
|
2004-05-29 22:48:08 +02:00
|
|
|
{
|
2004-09-19 20:51:19 +02:00
|
|
|
m_Buf = 0;
|
|
|
|
m_Size = 0;
|
|
|
|
m_UnpackPos = 0;
|
|
|
|
m_Version = 0;
|
2004-05-29 22:48:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Read: open and read in given file, check magic bits against those given; throw
|
|
|
|
// variety of exceptions for missing files etc
|
|
|
|
void CFileUnpacker::Read(const char* filename,const char magicstr[4])
|
|
|
|
{
|
2004-08-11 22:18:49 +02:00
|
|
|
// load the whole thing into memory
|
2004-09-19 20:51:19 +02:00
|
|
|
Handle hm = vfs_load(filename, m_Buf, m_Size);
|
2004-08-11 22:18:49 +02:00
|
|
|
if(hm <= 0)
|
2004-09-19 20:51:19 +02:00
|
|
|
{
|
|
|
|
m_Buf = 0;
|
|
|
|
m_Size = 0;
|
2004-08-11 22:18:49 +02:00
|
|
|
throw CFileOpenError();
|
2004-09-19 20:51:19 +02:00
|
|
|
}
|
2004-08-11 22:18:49 +02:00
|
|
|
|
|
|
|
// make sure we read enough for the header
|
2004-09-19 20:51:19 +02:00
|
|
|
if(m_Size < 12)
|
2004-08-11 22:18:49 +02:00
|
|
|
{
|
|
|
|
mem_free_h(hm);
|
2004-09-19 20:51:19 +02:00
|
|
|
m_Buf = 0;
|
|
|
|
m_Size = 0;
|
2004-08-11 22:18:49 +02:00
|
|
|
throw CFileReadError();
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract data from header
|
2004-09-19 20:51:19 +02:00
|
|
|
u8* header = (u8*)m_Buf;
|
2004-08-11 22:18:49 +02:00
|
|
|
char* magic = (char*)(header+0);
|
|
|
|
m_Version = *(u32*)(header+4);
|
|
|
|
u32 datasize = *(u32*)(header+8);
|
|
|
|
void* data = (header+12);
|
|
|
|
|
|
|
|
// check we've got the right kind of file
|
|
|
|
// .. and that we read exactly headersize+datasize
|
|
|
|
if(strncmp(magic, magicstr, 4) != 0 ||
|
2004-09-19 20:51:19 +02:00
|
|
|
m_Size != 12+datasize)
|
2004-08-11 22:18:49 +02:00
|
|
|
{
|
|
|
|
mem_free_h(hm);
|
2004-09-19 20:51:19 +02:00
|
|
|
m_Buf = 0;
|
|
|
|
m_Size = 0;
|
2004-08-11 22:18:49 +02:00
|
|
|
throw CFileTypeError();
|
|
|
|
}
|
2004-05-29 22:48:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// UnpackRaw: unpack given number of bytes from the input stream into the given array
|
|
|
|
// - throws CFileEOFError if the end of the data stream is reached before the given
|
|
|
|
// number of bytes have been read
|
|
|
|
void CFileUnpacker::UnpackRaw(void* rawdata,u32 rawdatalen)
|
|
|
|
{
|
|
|
|
// got enough data to unpack?
|
2004-09-19 20:51:19 +02:00
|
|
|
if (m_UnpackPos+rawdatalen<=m_Size)
|
|
|
|
{
|
2004-05-29 22:48:08 +02:00
|
|
|
// yes .. copy over
|
2004-09-19 20:51:19 +02:00
|
|
|
void* src = (char*)m_Buf + m_UnpackPos;
|
|
|
|
memcpy(rawdata, src, rawdatalen);
|
|
|
|
m_UnpackPos += rawdatalen;
|
|
|
|
}
|
|
|
|
else
|
2004-05-29 22:48:08 +02:00
|
|
|
// nope - throw exception
|
|
|
|
throw CFileEOFError();
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// UnpackString: unpack a string from the raw data stream
|
|
|
|
void CFileUnpacker::UnpackString(CStr& result)
|
|
|
|
{
|
|
|
|
// get string length
|
|
|
|
u32 length;
|
|
|
|
UnpackRaw(&length,sizeof(length));
|
|
|
|
|
|
|
|
// read string into temporary buffer
|
|
|
|
std::vector<char> tmp;
|
|
|
|
tmp.resize(length+1);
|
|
|
|
UnpackRaw(&tmp[0],length);
|
|
|
|
tmp[length]='\0';
|
|
|
|
|
|
|
|
// assign to output
|
|
|
|
result=&tmp[0];
|
|
|
|
}
|
|
|
|
|