project64/Source/Project64-core/3rdParty/7zip.cpp

242 lines
6.0 KiB
C++
Raw Normal View History

typedef const char * LPCSTR;
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <memory.h>
#include "7zip.h"
#include <Common/StdString.h>
C7zip::C7zip(const char * FileName) :
2015-12-23 19:34:47 +00:00
m_FileSize(0),
m_CurrentFile(-1),
m_blockIndex(0xFFFFFFFF),
m_outBuffer(0),
m_outBufferSize(0),
m_NotfyCallback(NotfyCallbackDefault),
m_NotfyCallbackInfo(NULL),
m_db(NULL),
m_Opened(false)
{
2015-12-23 19:34:47 +00:00
memset(&m_FileName, 0, sizeof(m_FileName));
memset(&m_archiveLookStream, 0, sizeof(m_archiveLookStream));
m_db = new CSzArEx;
memset(m_db, 0, sizeof(CSzArEx));
m_archiveStream.s.Read = SzFileReadImp;
m_archiveStream.s.Seek = SzFileSeekImp;
m_allocImp.Alloc = AllocAllocImp;
m_allocImp.Free = AllocFreeImp;
m_allocTempImp.Alloc = AllocAllocImp;
m_allocTempImp.Free = AllocFreeImp;
InFile_Open(&m_archiveStream.file, FileName);
if (m_archiveStream.file.handle == INVALID_HANDLE_VALUE)
{
//PrintError("can not open input file");
return;
}
m_FileSize = GetFileSize(m_archiveStream.file.handle, NULL);
char drive[_MAX_DRIVE], dir[_MAX_DIR], ext[_MAX_EXT];
_splitpath(FileName, drive, dir, m_FileName, ext);
CrcGenerateTable();
SzArEx_Init(m_db);
LookToRead_Init(&m_archiveLookStream);
LookToRead_CreateVTable(&m_archiveLookStream, False);
m_archiveLookStream.realStream = &m_archiveStream.s;
SRes res = SzArEx_Open(m_db, &m_archiveLookStream.s, &m_allocImp, &m_allocTempImp);
if (res == SZ_OK)
{
m_Opened = true;
}
else
{
//SzArEx_Open will delete the passed db if it fails
m_db = NULL;
}
}
2015-12-23 19:34:47 +00:00
C7zip::~C7zip(void)
{
2015-12-23 19:34:47 +00:00
if (m_db)
{
delete m_db;
m_db = NULL;
}
2015-12-15 06:01:06 +00:00
#ifdef legacycode
2015-12-23 19:34:47 +00:00
SetNotificationCallback(NULL,NULL);
SzArDbExFree(&m_db, m_allocImp.Free);
if (m_archiveStream.File)
{
fclose(m_archiveStream.File);
}
if (m_outBuffer)
{
m_allocImp.Free(m_outBuffer);
}
2015-02-28 05:05:57 +00:00
#endif
}
2015-12-23 19:34:47 +00:00
void C7zip::SetNotificationCallback(LP7ZNOTIFICATION NotfyFnc, void * CBInfo)
{
2015-12-23 19:34:47 +00:00
m_NotfyCallback = NotfyFnc ? NotfyFnc : NotfyCallbackDefault;
m_NotfyCallbackInfo = CBInfo;
}
2015-12-15 06:01:06 +00:00
#ifdef legacycode
void C7zip::StatusUpdate(_7Z_STATUS status, int Value1, int Value2, C7zip * _this )
{
2015-12-23 19:34:47 +00:00
CFileItem * File = _this->m_CurrentFile >= 0 ? _this->FileItem(_this->m_CurrentFile) : NULL;
switch (status)
{
case LZMADECODE_START: _this->m_NotfyCallback("Start decoding",_this->m_NotfyCallbackInfo); break;
case LZMADECODE_UPDATE:
{
char Msg[200];
sprintf(Msg,"decoding %s: %0.2f%%",File->Name, (Value1/(float)Value2) * 100);
_this->m_NotfyCallback(Msg,_this->m_NotfyCallbackInfo);
}
break;
case LZMADECODE_DONE: _this->m_NotfyCallback("Finished decoding",_this->m_NotfyCallbackInfo); break;
}
}
2015-02-28 05:05:57 +00:00
#endif
2015-12-23 19:34:47 +00:00
bool C7zip::GetFile(int index, Byte * Data, size_t DataLen)
{
2015-12-23 19:34:47 +00:00
m_CurrentFile = -1;
if (Data == NULL || DataLen == 0)
{
return false;
}
if (m_archiveStream.file.handle == INVALID_HANDLE_VALUE)
{
return false;
}
m_CurrentFile = index;
size_t offset;
size_t outSizeProcessed;
2015-12-23 19:34:47 +00:00
char Msg[200];
2015-12-23 19:34:47 +00:00
std::wstring FileName = FileNameIndex(index);
_snprintf(Msg, sizeof(Msg) / sizeof(Msg[0]), "extracting %s", stdstr().FromUTF16(FileName.c_str()).c_str());
2015-12-23 19:34:47 +00:00
m_NotfyCallback(Msg, m_NotfyCallbackInfo);
SRes res = SzArEx_Extract(m_db, &m_archiveLookStream.s, index,
&m_blockIndex, &m_outBuffer, &m_outBufferSize,
&offset, &outSizeProcessed,
&m_allocImp, &m_allocTempImp);
if (res != SZ_OK)
2015-12-23 19:34:47 +00:00
{
m_CurrentFile = -1;
return false;
}
if (DataLen < outSizeProcessed)
{
outSizeProcessed = DataLen;
}
memcpy(Data, m_outBuffer + offset, outSizeProcessed);
m_NotfyCallback("", m_NotfyCallbackInfo);
2015-12-23 19:34:47 +00:00
m_CurrentFile = -1;
return true;
}
2015-12-23 19:34:47 +00:00
void * C7zip::AllocAllocImp(void * /*p*/, size_t size)
{
2015-12-23 19:34:47 +00:00
return malloc(size);
//return new BYTE[size];
}
2015-12-23 19:34:47 +00:00
void C7zip::AllocFreeImp(void * /*p*/, void *address)
{
2015-12-23 19:34:47 +00:00
if (address != NULL)
{
free(address);
}
2015-02-28 05:05:57 +00:00
}
SRes C7zip::SzFileReadImp(void *object, void *buffer, size_t *processedSize)
{
2015-12-23 19:34:47 +00:00
CFileInStream *p = (CFileInStream *)object;
DWORD dwRead;
if (!ReadFile(p->file.handle, buffer, *processedSize, &dwRead, NULL))
{
return SZ_ERROR_FAIL;
}
//p->s.curpos += read_sz;
*processedSize = dwRead;
return SZ_OK;
2015-02-28 05:05:57 +00:00
}
SRes C7zip::SzFileSeekImp(void *p, Int64 *pos, ESzSeek origin)
{
2015-12-23 19:34:47 +00:00
CFileInStream *s = (CFileInStream *)p;
DWORD dwMoveMethod;
switch (origin)
{
case SZ_SEEK_SET:
dwMoveMethod = FILE_BEGIN;
break;
case SZ_SEEK_CUR:
dwMoveMethod = FILE_CURRENT;
break;
case SZ_SEEK_END:
dwMoveMethod = FILE_END;
break;
default:
return SZ_ERROR_FAIL;
}
*pos = SetFilePointer(s->file.handle, (LONG)*pos, NULL, dwMoveMethod);
if (*pos == INVALID_SET_FILE_POINTER)
{
return SZ_ERROR_FAIL;
}
return SZ_OK;
}
2015-12-23 19:34:47 +00:00
const char * C7zip::FileName(char * FileName, int SizeOfFileName) const
{
2015-12-23 19:34:47 +00:00
if (FileName == NULL)
{
return NULL;
}
int Len = strlen(m_FileName);
if (Len > (SizeOfFileName - 1))
{
Len = (SizeOfFileName - 1);
}
strncpy(FileName, m_FileName, Len);
FileName[Len] = 0;
return FileName;
}
2015-02-28 05:05:57 +00:00
2015-12-23 19:34:47 +00:00
std::wstring C7zip::FileNameIndex(int index)
2015-02-28 05:05:57 +00:00
{
2015-12-23 19:34:47 +00:00
std::wstring filename;
2015-02-28 05:05:57 +00:00
if (m_db == NULL || m_db->FileNameOffsets == 0)
2015-12-23 19:34:47 +00:00
{
/* no filename */
return filename;
}
int namelen = SzArEx_GetFileNameUtf16(m_db, index, NULL);
if (namelen <= 0)
{
/* no filename */
return filename;
}
filename.resize(namelen);
SzArEx_GetFileNameUtf16(m_db, index, (UInt16 *)filename.c_str());
return filename;
2015-12-15 06:01:06 +00:00
}