Skip to content

Commit

Permalink
Add NanaZipCodecsLz4Read, NanaZipCodecsLz4Write, NanaZipCodecsLz5Read…
Browse files Browse the repository at this point in the history
… and NanaZipCodecsLz5Write.
  • Loading branch information
MouriNaruto committed Nov 2, 2024
1 parent 28c10da commit ecfd73d
Show file tree
Hide file tree
Showing 13 changed files with 204 additions and 212 deletions.
37 changes: 37 additions & 0 deletions NanaZip.Codecs/NanaZip.Codecs.MultiThreadWrapper.LZ4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* PROJECT: NanaZip
* FILE: NanaZip.Codecs.MultiThreadWrapper.LZ4.cpp
* PURPOSE: Implementation for LZ4 Multi Thread Wrapper
*
* LICENSE: The MIT License
*
* MAINTAINER: MouriNaruto (Kenji.Mouri@outlook.com)
*/

#include "NanaZip.Codecs.MultiThreadWrapper.LZ4.h"

EXTERN_C int NanaZipCodecsLz4Read(
void* Context,
LZ4MT_Buffer* Input)
{
NANAZIP_CODECS_ZSTDMT_BUFFER_CONTEXT ConvertedInput;
ConvertedInput.Buffer = Input->buf;
ConvertedInput.Size = Input->size;
ConvertedInput.Allocated = Input->allocated;
return ::NanaZipCodecsCommonRead(
reinterpret_cast<PNANAZIP_CODECS_ZSTDMT_STREAM_CONTEXT>(Context),
&ConvertedInput);
}

EXTERN_C int NanaZipCodecsLz4Write(
void* Context,
LZ4MT_Buffer* Output)
{
NANAZIP_CODECS_ZSTDMT_BUFFER_CONTEXT ConvertedOutput;
ConvertedOutput.Buffer = Output->buf;
ConvertedOutput.Size = Output->size;
ConvertedOutput.Allocated = Output->allocated;
return ::NanaZipCodecsCommonWrite(
reinterpret_cast<PNANAZIP_CODECS_ZSTDMT_STREAM_CONTEXT>(Context),
&ConvertedOutput);
}
27 changes: 27 additions & 0 deletions NanaZip.Codecs/NanaZip.Codecs.MultiThreadWrapper.LZ4.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* PROJECT: NanaZip
* FILE: NanaZip.Codecs.MultiThreadWrapper.LZ4.h
* PURPOSE: Definition for LZ4 Multi Thread Wrapper
*
* LICENSE: The MIT License
*
* MAINTAINER: MouriNaruto (Kenji.Mouri@outlook.com)
*/

#ifndef NANAZIP_CODECS_MULTI_THREAD_WRAPPER_LZ4
#define NANAZIP_CODECS_MULTI_THREAD_WRAPPER_LZ4

#include "NanaZip.Codecs.MultiThreadWrapper.Common.h"

#include <stdint.h>
#include <lz4-mt.h>

EXTERN_C int NanaZipCodecsLz4Read(
void* Context,
LZ4MT_Buffer* Input);

EXTERN_C int NanaZipCodecsLz4Write(
void* Context,
LZ4MT_Buffer* Output);

#endif // !NANAZIP_CODECS_MULTI_THREAD_WRAPPER_LZ4
37 changes: 37 additions & 0 deletions NanaZip.Codecs/NanaZip.Codecs.MultiThreadWrapper.LZ5.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* PROJECT: NanaZip
* FILE: NanaZip.Codecs.MultiThreadWrapper.LZ5.cpp
* PURPOSE: Implementation for LZ5 Multi Thread Wrapper
*
* LICENSE: The MIT License
*
* MAINTAINER: MouriNaruto (Kenji.Mouri@outlook.com)
*/

#include "NanaZip.Codecs.MultiThreadWrapper.LZ5.h"

EXTERN_C int NanaZipCodecsLz5Read(
void* Context,
LZ5MT_Buffer* Input)
{
NANAZIP_CODECS_ZSTDMT_BUFFER_CONTEXT ConvertedInput;
ConvertedInput.Buffer = Input->buf;
ConvertedInput.Size = Input->size;
ConvertedInput.Allocated = Input->allocated;
return ::NanaZipCodecsCommonRead(
reinterpret_cast<PNANAZIP_CODECS_ZSTDMT_STREAM_CONTEXT>(Context),
&ConvertedInput);
}

EXTERN_C int NanaZipCodecsLz5Write(
void* Context,
LZ5MT_Buffer* Output)
{
NANAZIP_CODECS_ZSTDMT_BUFFER_CONTEXT ConvertedOutput;
ConvertedOutput.Buffer = Output->buf;
ConvertedOutput.Size = Output->size;
ConvertedOutput.Allocated = Output->allocated;
return ::NanaZipCodecsCommonWrite(
reinterpret_cast<PNANAZIP_CODECS_ZSTDMT_STREAM_CONTEXT>(Context),
&ConvertedOutput);
}
27 changes: 27 additions & 0 deletions NanaZip.Codecs/NanaZip.Codecs.MultiThreadWrapper.LZ5.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* PROJECT: NanaZip
* FILE: NanaZip.Codecs.MultiThreadWrapper.LZ5.h
* PURPOSE: Definition for LZ5 Multi Thread Wrapper
*
* LICENSE: The MIT License
*
* MAINTAINER: MouriNaruto (Kenji.Mouri@outlook.com)
*/

#ifndef NANAZIP_CODECS_MULTI_THREAD_WRAPPER_LZ5
#define NANAZIP_CODECS_MULTI_THREAD_WRAPPER_LZ5

#include "NanaZip.Codecs.MultiThreadWrapper.Common.h"

#include <stdint.h>
#include <lz5-mt.h>

EXTERN_C int NanaZipCodecsLz5Read(
void* Context,
LZ5MT_Buffer* Input);

EXTERN_C int NanaZipCodecsLz5Write(
void* Context,
LZ5MT_Buffer* Output);

#endif // !NANAZIP_CODECS_MULTI_THREAD_WRAPPER_LZ5
4 changes: 4 additions & 0 deletions NanaZip.Codecs/NanaZip.Codecs.def
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ NanaZipCodecsBrotliRead
NanaZipCodecsBrotliWrite
NanaZipCodecsLizardRead
NanaZipCodecsLizardWrite
NanaZipCodecsLz4Read
NanaZipCodecsLz4Write
NanaZipCodecsLz5Read
NanaZipCodecsLz5Write

LZ4_decompress_safe

Expand Down
4 changes: 4 additions & 0 deletions NanaZip.Codecs/NanaZip.Codecs.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
<ClCompile Include="NanaZip.Codecs.MultiThreadWrapper.Brotli.cpp" />
<ClCompile Include="NanaZip.Codecs.MultiThreadWrapper.Common.cpp" />
<ClCompile Include="NanaZip.Codecs.MultiThreadWrapper.Lizard.cpp" />
<ClCompile Include="NanaZip.Codecs.MultiThreadWrapper.LZ4.cpp" />
<ClCompile Include="NanaZip.Codecs.MultiThreadWrapper.LZ5.cpp" />
<ClCompile Include="Zstandard\common\debug.c" />
<ClCompile Include="Zstandard\common\entropy_common.c" />
<ClCompile Include="Zstandard\common\error_private.c" />
Expand Down Expand Up @@ -267,6 +269,8 @@
<ClInclude Include="NanaZip.Codecs.MultiThreadWrapper.Brotli.h" />
<ClInclude Include="NanaZip.Codecs.MultiThreadWrapper.Common.h" />
<ClInclude Include="NanaZip.Codecs.MultiThreadWrapper.Lizard.h" />
<ClInclude Include="NanaZip.Codecs.MultiThreadWrapper.LZ4.h" />
<ClInclude Include="NanaZip.Codecs.MultiThreadWrapper.LZ5.h" />
<ClInclude Include="Zstandard\common\allocations.h" />
<ClInclude Include="Zstandard\common\bits.h" />
<ClInclude Include="Zstandard\common\bitstream.h" />
Expand Down
4 changes: 4 additions & 0 deletions NanaZip.Codecs/NanaZip.Codecs.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@
<ClCompile Include="NanaZip.Codecs.MultiThreadWrapper.Common.cpp" />
<ClCompile Include="NanaZip.Codecs.MultiThreadWrapper.Brotli.cpp" />
<ClCompile Include="NanaZip.Codecs.MultiThreadWrapper.Lizard.cpp" />
<ClCompile Include="NanaZip.Codecs.MultiThreadWrapper.LZ4.cpp" />
<ClCompile Include="NanaZip.Codecs.MultiThreadWrapper.LZ5.cpp" />
</ItemGroup>
<ItemGroup>
<None Include="NanaZip.Codecs.def" />
Expand Down Expand Up @@ -1028,5 +1030,7 @@
<ClInclude Include="NanaZip.Codecs.MultiThreadWrapper.Common.h" />
<ClInclude Include="NanaZip.Codecs.MultiThreadWrapper.Brotli.h" />
<ClInclude Include="NanaZip.Codecs.MultiThreadWrapper.Lizard.h" />
<ClInclude Include="NanaZip.Codecs.MultiThreadWrapper.LZ4.h" />
<ClInclude Include="NanaZip.Codecs.MultiThreadWrapper.LZ5.h" />
</ItemGroup>
</Project>
88 changes: 12 additions & 76 deletions NanaZip.Core/Extensions/ZSCodecs/Lz4Decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,6 @@
#include "../../SevenZip/CPP/7zip/Compress/StdAfx.h"
#include "Lz4Decoder.h"

int Lz4Read(void *arg, LZ4MT_Buffer * in)
{
struct Lz4Stream *x = (struct Lz4Stream*)arg;
size_t size = in->size;

HRESULT res = ReadStream(x->inStream, in->buf, &size);

/* catch errors */
switch (res) {
case E_ABORT:
return -2;
case E_OUTOFMEMORY:
return -3;
}

/* some other error -> read_fail */
if (res != S_OK)
return -1;

in->size = size;
*x->processedIn += size;

return 0;
}

int Lz4Write(void *arg, LZ4MT_Buffer * out)
{
struct Lz4Stream *x = (struct Lz4Stream*)arg;
UInt32 todo = (UInt32)out->size;
UInt32 done = 0;

while (todo != 0)
{
UInt32 block;
HRESULT res = x->outStream->Write((char*)out->buf + done, todo, &block);

/* catch errors */
switch (res) {
case E_ABORT:
return -2;
case E_OUTOFMEMORY:
return -3;
}

done += block;
if (res == k_My_HRESULT_WritingWasCut)
break;
/* some other error -> write_fail */
if (res != S_OK)
return -1;

if (block == 0)
return -1;
todo -= block;
}

*x->processedOut += done;
/* we need no lock here, cause only one thread can write... */
if (x->progress)
x->progress->SetRatioInfo(x->processedIn, x->processedOut);

return 0;
}

namespace NCompress {
namespace NLZ4 {

Expand Down Expand Up @@ -128,21 +64,21 @@ HRESULT CDecoder::CodeSpec(ISequentialInStream * inStream,
size_t result;
HRESULT res = S_OK;

struct Lz4Stream Rd;
Rd.inStream = inStream;
Rd.processedIn = &_processedIn;
NANAZIP_CODECS_ZSTDMT_STREAM_CONTEXT ReadContext = { 0 };
ReadContext.InputStream = inStream;
ReadContext.ProcessedInputSize = &_processedIn;

struct Lz4Stream Wr;
Wr.progress = progress;
Wr.outStream = outStream;
Wr.processedIn = &_processedIn;
Wr.processedOut = &_processedOut;
NANAZIP_CODECS_ZSTDMT_STREAM_CONTEXT WriteContext = { 0 };
WriteContext.Progress = progress;
WriteContext.OutputStream = outStream;
WriteContext.ProcessedInputSize = &_processedIn;
WriteContext.ProcessedOutputSize = &_processedOut;

/* 1) setup read/write functions */
rdwr.fn_read = ::Lz4Read;
rdwr.fn_write = ::Lz4Write;
rdwr.arg_read = (void *)&Rd;
rdwr.arg_write = (void *)&Wr;
rdwr.fn_read = ::NanaZipCodecsLz4Read;
rdwr.fn_write = ::NanaZipCodecsLz4Write;
rdwr.arg_read = reinterpret_cast<void*>(&ReadContext);
rdwr.arg_write = reinterpret_cast<void*>(&WriteContext);

/* 2) create decompression context */
LZ4MT_DCtx *ctx = LZ4MT_createDCtx(_numThreads, _inputSize);
Expand Down
12 changes: 1 addition & 11 deletions NanaZip.Core/Extensions/ZSCodecs/Lz4Decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include "../../SevenZip/C/Alloc.h"
#include "../../SevenZip/C/Threads.h"
#include <lz4.h>
#include <lz4-mt.h>

#include "../../SevenZip/CPP/Windows/System.h"
#include "../../SevenZip/CPP/Common/Common.h"
Expand All @@ -14,16 +13,7 @@
#include "../../SevenZip/CPP/7zip/Common/RegisterCodec.h"
#include "../../SevenZip/CPP/7zip/Common/ProgressMt.h"

struct Lz4Stream {
ISequentialInStream *inStream;
ISequentialOutStream *outStream;
ICompressProgressInfo *progress;
UInt64 *processedIn;
UInt64 *processedOut;
};

extern int Lz4Read(void *Stream, LZ4MT_Buffer * in);
extern int Lz4Write(void *Stream, LZ4MT_Buffer * in);
#include <NanaZip.Codecs.MultiThreadWrapper.LZ4.h>

namespace NCompress {
namespace NLZ4 {
Expand Down
38 changes: 19 additions & 19 deletions NanaZip.Core/Extensions/ZSCodecs/Lz4Encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,27 @@ STDMETHODIMP CEncoder::Code(ISequentialInStream *inStream,
size_t result;
HRESULT res = S_OK;

struct Lz4Stream Rd;
Rd.inStream = inStream;
Rd.outStream = outStream;
Rd.processedIn = &_processedIn;
Rd.processedOut = &_processedOut;

struct Lz4Stream Wr;
if (_processedIn == 0)
Wr.progress = progress;
else
Wr.progress = 0;
Wr.inStream = inStream;
Wr.outStream = outStream;
Wr.processedIn = &_processedIn;
Wr.processedOut = &_processedOut;
NANAZIP_CODECS_ZSTDMT_STREAM_CONTEXT ReadContext = { 0 };
ReadContext.InputStream = inStream;
ReadContext.OutputStream = outStream;
ReadContext.ProcessedInputSize = &_processedIn;
ReadContext.ProcessedOutputSize = &_processedOut;

NANAZIP_CODECS_ZSTDMT_STREAM_CONTEXT WriteContext = { 0 };
if (0 == _processedIn)
{
WriteContext.Progress = progress;
}
WriteContext.InputStream = inStream;
WriteContext.OutputStream = outStream;
WriteContext.ProcessedInputSize = &_processedIn;
WriteContext.ProcessedOutputSize = &_processedOut;

/* 1) setup read/write functions */
rdwr.fn_read = ::Lz4Read;
rdwr.fn_write = ::Lz4Write;
rdwr.arg_read = (void *)&Rd;
rdwr.arg_write = (void *)&Wr;
rdwr.fn_read = ::NanaZipCodecsLz4Read;
rdwr.fn_write = ::NanaZipCodecsLz4Write;
rdwr.arg_read = reinterpret_cast<void*>(&ReadContext);
rdwr.arg_write = reinterpret_cast<void*>(&WriteContext);

/* 2) create compression context, if needed */
if (!_ctx)
Expand Down
Loading

0 comments on commit ecfd73d

Please sign in to comment.