Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TAR: added zstd and lzma support #5

Open
wants to merge 19 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
reformated with clang-format
  • Loading branch information
lejcik committed Dec 8, 2023
commit 60d56e374017730e1e0b07870656072e445abd11
2 changes: 2 additions & 0 deletions normalize.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ $utfExcludes = @(
'.\src\plugins\pictview\exif\libjpeg\*',
'.\src\plugins\pictview\twain\*',
'.\src\plugins\portables\wtl\*',
'.\src\plugins\tar\lzma\liblzma\*',
'.\src\plugins\tar\zstd\libzstd\*',
'.\src\plugins\unchm\chmlib\*',
'.\src\plugins\winscp\core\*',
'.\src\plugins\winscp\forms\*',
Expand Down
6 changes: 3 additions & 3 deletions src/plugins/tar/deb/deb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ CDEBArchive::CDEBArchive(const char* fileName, CSalamanderForOperationsAbstract*
pos += sizeof(ARBlock);

// Open the fist subarchive
CArchive *archive = new CArchive(fileName, salamander, pos, CQuadWord(SubArchiveSize, 0));
CArchive* archive = new CArchive(fileName, salamander, pos, CQuadWord(SubArchiveSize, 0));
if (!archive->IsOk())
{
delete archive;
Expand Down Expand Up @@ -322,12 +322,12 @@ BOOL CDEBArchive::UnpackWholeArchive(const char* mask, const char* targetPath)

BOOL CDEBArchive::AssignArchive(const char* archName, CArchive* archive)
{
if (!controlArchive && !strncmp(archName, DEB_STREAM_NAME_CONTROL".", sizeof(DEB_STREAM_NAME_CONTROL".") - 1))
if (!controlArchive && !strncmp(archName, DEB_STREAM_NAME_CONTROL ".", sizeof(DEB_STREAM_NAME_CONTROL ".") - 1))
{
controlArchive = archive;
return TRUE;
}
else if (!dataArchive && !strncmp(archName, DEB_STREAM_NAME_DATA".", sizeof(DEB_STREAM_NAME_DATA".") - 1))
else if (!dataArchive && !strncmp(archName, DEB_STREAM_NAME_DATA ".", sizeof(DEB_STREAM_NAME_DATA ".") - 1))
{
dataArchive = archive;
return TRUE;
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/tar/deb/deb.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ class CDEBArchive : public CArchiveAbstract

BOOL IsOk() { return bOK; };

private:
BOOL AssignArchive(const char *archName, CArchive *archive);
private:
BOOL AssignArchive(const char* archName, CArchive* archive);
};
8 changes: 3 additions & 5 deletions src/plugins/tar/fileio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,8 @@ CDecompressFile::CreateInstance(LPCTSTR fileName, DWORD inputOffset, CQuadWord i
}

// class constructor
CDecompressFile::CDecompressFile(const char* filename, HANDLE file, unsigned char* buffer, unsigned long start, unsigned long read, CQuadWord inputSize) :
FileName(filename), File(file), Buffer(buffer), DataStart(buffer), DataEnd(buffer + read),
OldName(NULL), Ok(TRUE), InputPos(0, 0), StreamPos(start, 0), ErrorCode(0), LastError(0), FreeBufAndFile(TRUE)
CDecompressFile::CDecompressFile(const char* filename, HANDLE file, unsigned char* buffer, unsigned long start, unsigned long read, CQuadWord inputSize) : FileName(filename), File(file), Buffer(buffer), DataStart(buffer), DataEnd(buffer + read),
OldName(NULL), Ok(TRUE), InputPos(0, 0), StreamPos(start, 0), ErrorCode(0), LastError(0), FreeBufAndFile(TRUE)
{
CALL_STACK_MESSAGE3("CDecompressFile::CDecompressFile(%s, , %u)", filename, read);

Expand Down Expand Up @@ -365,8 +364,7 @@ void CDecompressFile::GetFileInfo(FILETIME& lastWrite, CQuadWord& fileSize, DWOR
// CZippedFile
//

CZippedFile::CZippedFile(const char* filename, HANDLE file, unsigned char* buffer, unsigned long start, unsigned long read, CQuadWord inputSize) :
CDecompressFile(filename, file, buffer, start, read, inputSize), Window(NULL), ExtrStart(NULL), ExtrEnd(NULL)
CZippedFile::CZippedFile(const char* filename, HANDLE file, unsigned char* buffer, unsigned long start, unsigned long read, CQuadWord inputSize) : CDecompressFile(filename, file, buffer, start, read, inputSize), Window(NULL), ExtrStart(NULL), ExtrEnd(NULL)
{
// pokud neprosel konstruktor parenta, balime to rovnou
if (!Ok)
Expand Down
183 changes: 90 additions & 93 deletions src/plugins/tar/lzma/unlzma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,113 +11,110 @@
#include "..\tar.rh2"
#include "..\lang\lang.rh"

CLZMa::CLZMa(const char *filename, HANDLE file, unsigned char *buffer, unsigned long start, unsigned long read, CQuadWord inputSize):
CZippedFile(filename, file, buffer, start, read, inputSize)
CLZMa::CLZMa(const char* filename, HANDLE file, unsigned char* buffer, unsigned long start, unsigned long read, CQuadWord inputSize) : CZippedFile(filename, file, buffer, start, read, inputSize)
{
CALL_STACK_MESSAGE2("CLZMa::CLZMa(%s, , , )", filename);

// pokud neprosel konstruktor parenta, balime to rovnou
if (!Ok)
return;
CALL_STACK_MESSAGE2("CLZMa::CLZMa(%s, , , )", filename);

// nemuze to byt lzma, pokud mame min nez hlavicku...
if (DataEnd - DataStart < 6)
{
Ok = FALSE;
FreeBufAndFile = FALSE;
return;
}
// pokud neni "magicke cislo" na zacatku, nejde o lzma
if (DataStart[0] != 0xFD || DataStart[1] != '7' ||
DataStart[2] != 'z' || DataStart[3] != 'X' ||
DataStart[4] != 'Z' || DataStart[5] != 0)
{
Ok = FALSE;
FreeBufAndFile = FALSE;
return;
}
// mame lzma, ale precteny header nepotvrzujeme, knihovna ho bude overovat znovu...
// pokud neprosel konstruktor parenta, balime to rovnou
if (!Ok)
return;

// inicializace streamu
lzma_ret ret = lzma_stream_decoder(&m_strm, UINT64_MAX, LZMA_CONCATENATED);
if (ret != LZMA_OK)
{
Ok = FALSE;
FreeBufAndFile = FALSE;
switch (ret)
// nemuze to byt lzma, pokud mame min nez hlavicku...
if (DataEnd - DataStart < 6)
{
case LZMA_MEM_ERROR:
ErrorCode = IDS_ERR_MEMORY;
break;
case LZMA_OPTIONS_ERROR:
case LZMA_PROG_ERROR:
default:
ErrorCode = IDS_ERR_INTERNAL;
break;
Ok = FALSE;
FreeBufAndFile = FALSE;
return;
}
// pokud neni "magicke cislo" na zacatku, nejde o lzma
if (DataStart[0] != 0xFD || DataStart[1] != '7' ||
DataStart[2] != 'z' || DataStart[3] != 'X' ||
DataStart[4] != 'Z' || DataStart[5] != 0)
{
Ok = FALSE;
FreeBufAndFile = FALSE;
return;
}
// mame lzma, ale precteny header nepotvrzujeme, knihovna ho bude overovat znovu...

// inicializace streamu
lzma_ret ret = lzma_stream_decoder(&m_strm, UINT64_MAX, LZMA_CONCATENATED);
if (ret != LZMA_OK)
{
Ok = FALSE;
FreeBufAndFile = FALSE;
switch (ret)
{
case LZMA_MEM_ERROR:
ErrorCode = IDS_ERR_MEMORY;
break;
case LZMA_OPTIONS_ERROR:
case LZMA_PROG_ERROR:
default:
ErrorCode = IDS_ERR_INTERNAL;
break;
}
return;
}
return;
}

// hotovo
// hotovo
}

CLZMa::~CLZMa()
{
CALL_STACK_MESSAGE1("CLZMa::~CLZMa()");
lzma_end(&m_strm);
CALL_STACK_MESSAGE1("CLZMa::~CLZMa()");
lzma_end(&m_strm);
}

BOOL
CLZMa::DecompressBlock(unsigned short needed)
BOOL CLZMa::DecompressBlock(unsigned short needed)
{
if (EndReached)
return TRUE;
int ret = LZMA_OK;
lzma_action action = LZMA_RUN;
while (ret != LZMA_STREAM_END && ExtrEnd < Window + BUFSIZE)
{
unsigned char *src = DataStart;
// aspon jeden byte musi byt v bufferu
if (DataEnd == DataStart)
src = (unsigned char*)FReadBlock(0);
if (src == NULL)
return FALSE;
if (GetInputPos() == GetStreamSize())
action = LZMA_FINISH;
m_strm.next_in = (uint8_t *)DataStart;
m_strm.avail_in = GetUnreadInputBufferSize();
m_strm.next_out = (uint8_t *)ExtrEnd;
m_strm.avail_out = BUFSIZE - (unsigned int)(ExtrEnd - Window);
if (EndReached)
return TRUE;
int ret = LZMA_OK;
lzma_action action = LZMA_RUN;
while (ret != LZMA_STREAM_END && ExtrEnd < Window + BUFSIZE)
{
unsigned char* src = DataStart;
// aspon jeden byte musi byt v bufferu
if (DataEnd == DataStart)
src = (unsigned char*)FReadBlock(0);
if (src == NULL)
return FALSE;
if (GetInputPos() == GetStreamSize())
action = LZMA_FINISH;
m_strm.next_in = (uint8_t*)DataStart;
m_strm.avail_in = GetUnreadInputBufferSize();
m_strm.next_out = (uint8_t*)ExtrEnd;
m_strm.avail_out = BUFSIZE - (unsigned int)(ExtrEnd - Window);

ret = lzma_code(&m_strm, action);
ret = lzma_code(&m_strm, action);

if (ret != LZMA_OK && ret != LZMA_STREAM_END)
{
Ok = FALSE;
switch (ret)
{
case LZMA_DATA_ERROR:
case LZMA_FORMAT_ERROR:
ErrorCode = IDS_ERR_CORRUPT;
break;
case LZMA_MEM_ERROR:
case LZMA_MEMLIMIT_ERROR:
ErrorCode = IDS_ERR_MEMORY;
break;
case LZMA_PROG_ERROR:
default:
ErrorCode = IDS_ERR_INTERNAL;
break;
}
return FALSE;
if (ret != LZMA_OK && ret != LZMA_STREAM_END)
{
Ok = FALSE;
switch (ret)
{
case LZMA_DATA_ERROR:
case LZMA_FORMAT_ERROR:
ErrorCode = IDS_ERR_CORRUPT;
break;
case LZMA_MEM_ERROR:
case LZMA_MEMLIMIT_ERROR:
ErrorCode = IDS_ERR_MEMORY;
break;
case LZMA_PROG_ERROR:
default:
ErrorCode = IDS_ERR_INTERNAL;
break;
}
return FALSE;
}
// commitnu prectena data ze vstupu
FReadBlock((unsigned int)(m_strm.next_in - (uint8_t*)DataStart));
unsigned short extracted = (unsigned short)((unsigned char*)m_strm.next_out - ExtrEnd);
ExtrEnd = (unsigned char*)m_strm.next_out;
}
// commitnu prectena data ze vstupu
FReadBlock((unsigned int)(m_strm.next_in - (uint8_t *)DataStart));
unsigned short extracted = (unsigned short)((unsigned char *)m_strm.next_out - ExtrEnd);
ExtrEnd = (unsigned char *)m_strm.next_out;
}
if (ret == LZMA_STREAM_END)
EndReached = TRUE;
return TRUE;
if (ret == LZMA_STREAM_END)
EndReached = TRUE;
return TRUE;
}

10 changes: 5 additions & 5 deletions src/plugins/tar/lzma/unlzma.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@

#include <lzma.h>

class CLZMa: public CZippedFile
class CLZMa : public CZippedFile
{
public:
CLZMa(const char *filename, HANDLE file, unsigned char *buffer, unsigned long start, unsigned long read, CQuadWord inputSize);
public:
CLZMa(const char* filename, HANDLE file, unsigned char* buffer, unsigned long start, unsigned long read, CQuadWord inputSize);
~CLZMa() override;

BOOL BuggySize() override { return TRUE; }

protected:
BOOL EndReached = FALSE; // set, when all data was extracted
protected:
BOOL EndReached = FALSE; // set, when all data was extracted

lzma_stream m_strm{};
BOOL DecompressBlock(unsigned short needed) override;
Expand Down
5 changes: 2 additions & 3 deletions src/plugins/tar/tardll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ void CPluginInterface::About(HWND parent)
{
char buf[1000];
_snprintf_s(buf, _TRUNCATE,
"%s " VERSINFO_VERSION "\n"
VERSINFO_COPYRIGHT "\n\n"
"%s " VERSINFO_VERSION "\n" VERSINFO_COPYRIGHT "\n\n"
"bzip2 library Copyright © 1996-2010 Julian R Seward\n"
"Zstandard library Copyright © 2016-2023 Facebook, Inc.\n\n"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

copyright notice, as license of zstd requires

"%s",
Expand Down Expand Up @@ -219,7 +218,7 @@ void CPluginInterface::Connect(HWND parent, CSalamanderConnectAbstract* salamand
salamander->AddCustomUnpacker("TAR-gz (Plugin)", "*.gz;*.tgz;*.tar.gz;*_tar.gz;*_tar_gz;*.tar_gz", upgrade);
salamander->AddCustomUnpacker("TAR (Plugin)", "*.tar;*.rpm;*.cpio;*.deb;*.ipk", upgrade);
salamander->AddPanelArchiver("tgz;tbz;taz;tar;gz;bz;bz2;xz;zst;z;rpm;cpio;deb;ipk", FALSE, FALSE); // pri upgradech pluginu se ignoruje
salamander->AddViewer("*.rpm", FALSE); // pri upgradech pluginu se ignoruje, az na pripad, kdy se upgraduje z verze, ktera jeste viewer nemela (verze pustena s SS 2.0)
salamander->AddViewer("*.rpm", FALSE); // pri upgradech pluginu se ignoruje, az na pripad, kdy se upgraduje z verze, ktera jeste viewer nemela (verze pustena s SS 2.0)

// cast pro upgrady:
if (ConfigVersion < 1) // 1 - pracovni verze pred Servant Salamander 2.5 beta 1, pridan tbz,bz,bz2 a rpm
Expand Down
Loading