diff --git a/NanaZip.Codecs/NanaZip.Codecs.MultiThreadWrapper.LZ4.cpp b/NanaZip.Codecs/NanaZip.Codecs.MultiThreadWrapper.LZ4.cpp new file mode 100644 index 000000000..3d01eb4a0 --- /dev/null +++ b/NanaZip.Codecs/NanaZip.Codecs.MultiThreadWrapper.LZ4.cpp @@ -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(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(Context), + &ConvertedOutput); +} diff --git a/NanaZip.Codecs/NanaZip.Codecs.MultiThreadWrapper.LZ4.h b/NanaZip.Codecs/NanaZip.Codecs.MultiThreadWrapper.LZ4.h new file mode 100644 index 000000000..10c318520 --- /dev/null +++ b/NanaZip.Codecs/NanaZip.Codecs.MultiThreadWrapper.LZ4.h @@ -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 +#include + +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 diff --git a/NanaZip.Codecs/NanaZip.Codecs.MultiThreadWrapper.LZ5.cpp b/NanaZip.Codecs/NanaZip.Codecs.MultiThreadWrapper.LZ5.cpp new file mode 100644 index 000000000..6dcf1e621 --- /dev/null +++ b/NanaZip.Codecs/NanaZip.Codecs.MultiThreadWrapper.LZ5.cpp @@ -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(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(Context), + &ConvertedOutput); +} diff --git a/NanaZip.Codecs/NanaZip.Codecs.MultiThreadWrapper.LZ5.h b/NanaZip.Codecs/NanaZip.Codecs.MultiThreadWrapper.LZ5.h new file mode 100644 index 000000000..9990f50d9 --- /dev/null +++ b/NanaZip.Codecs/NanaZip.Codecs.MultiThreadWrapper.LZ5.h @@ -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 +#include + +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 diff --git a/NanaZip.Codecs/NanaZip.Codecs.def b/NanaZip.Codecs/NanaZip.Codecs.def index 009748627..d368722c5 100644 --- a/NanaZip.Codecs/NanaZip.Codecs.def +++ b/NanaZip.Codecs/NanaZip.Codecs.def @@ -8,6 +8,10 @@ NanaZipCodecsBrotliRead NanaZipCodecsBrotliWrite NanaZipCodecsLizardRead NanaZipCodecsLizardWrite +NanaZipCodecsLz4Read +NanaZipCodecsLz4Write +NanaZipCodecsLz5Read +NanaZipCodecsLz5Write LZ4_decompress_safe diff --git a/NanaZip.Codecs/NanaZip.Codecs.vcxproj b/NanaZip.Codecs/NanaZip.Codecs.vcxproj index db740ceb6..c4b0464c8 100644 --- a/NanaZip.Codecs/NanaZip.Codecs.vcxproj +++ b/NanaZip.Codecs/NanaZip.Codecs.vcxproj @@ -96,6 +96,8 @@ + + @@ -267,6 +269,8 @@ + + diff --git a/NanaZip.Codecs/NanaZip.Codecs.vcxproj.filters b/NanaZip.Codecs/NanaZip.Codecs.vcxproj.filters index 347c50cc6..a6a944a1f 100644 --- a/NanaZip.Codecs/NanaZip.Codecs.vcxproj.filters +++ b/NanaZip.Codecs/NanaZip.Codecs.vcxproj.filters @@ -413,6 +413,8 @@ + + @@ -1028,5 +1030,7 @@ + + \ No newline at end of file diff --git a/NanaZip.Core/Extensions/ZSCodecs/Lz4Decoder.cpp b/NanaZip.Core/Extensions/ZSCodecs/Lz4Decoder.cpp index 9e371a84f..654e47ea3 100644 --- a/NanaZip.Core/Extensions/ZSCodecs/Lz4Decoder.cpp +++ b/NanaZip.Core/Extensions/ZSCodecs/Lz4Decoder.cpp @@ -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 { @@ -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(&ReadContext); + rdwr.arg_write = reinterpret_cast(&WriteContext); /* 2) create decompression context */ LZ4MT_DCtx *ctx = LZ4MT_createDCtx(_numThreads, _inputSize); diff --git a/NanaZip.Core/Extensions/ZSCodecs/Lz4Decoder.h b/NanaZip.Core/Extensions/ZSCodecs/Lz4Decoder.h index ce370acf6..9b721272c 100644 --- a/NanaZip.Core/Extensions/ZSCodecs/Lz4Decoder.h +++ b/NanaZip.Core/Extensions/ZSCodecs/Lz4Decoder.h @@ -4,7 +4,6 @@ #include "../../SevenZip/C/Alloc.h" #include "../../SevenZip/C/Threads.h" #include -#include #include "../../SevenZip/CPP/Windows/System.h" #include "../../SevenZip/CPP/Common/Common.h" @@ -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 namespace NCompress { namespace NLZ4 { diff --git a/NanaZip.Core/Extensions/ZSCodecs/Lz4Encoder.cpp b/NanaZip.Core/Extensions/ZSCodecs/Lz4Encoder.cpp index 6662a3d8b..2579a5fae 100644 --- a/NanaZip.Core/Extensions/ZSCodecs/Lz4Encoder.cpp +++ b/NanaZip.Core/Extensions/ZSCodecs/Lz4Encoder.cpp @@ -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(&ReadContext); + rdwr.arg_write = reinterpret_cast(&WriteContext); /* 2) create compression context, if needed */ if (!_ctx) diff --git a/NanaZip.Core/Extensions/ZSCodecs/Lz5Decoder.cpp b/NanaZip.Core/Extensions/ZSCodecs/Lz5Decoder.cpp index af19f5e67..d9984363a 100644 --- a/NanaZip.Core/Extensions/ZSCodecs/Lz5Decoder.cpp +++ b/NanaZip.Core/Extensions/ZSCodecs/Lz5Decoder.cpp @@ -3,70 +3,6 @@ #include "../../SevenZip/CPP/7zip/Compress/StdAfx.h" #include "Lz5Decoder.h" -int Lz5Read(void *arg, LZ5MT_Buffer * in) -{ - struct Lz5Stream *x = (struct Lz5Stream*)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 Lz5Write(void *arg, LZ5MT_Buffer * out) -{ - struct Lz5Stream *x = (struct Lz5Stream*)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 NLZ5 { @@ -128,21 +64,21 @@ HRESULT CDecoder::CodeSpec(ISequentialInStream * inStream, size_t result; HRESULT res = S_OK; - struct Lz5Stream Rd; - Rd.inStream = inStream; - Rd.processedIn = &_processedIn; + NANAZIP_CODECS_ZSTDMT_STREAM_CONTEXT ReadContext = { 0 }; + ReadContext.InputStream = inStream; + ReadContext.ProcessedInputSize = &_processedIn; - struct Lz5Stream 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 = ::Lz5Read; - rdwr.fn_write = ::Lz5Write; - rdwr.arg_read = (void *)&Rd; - rdwr.arg_write = (void *)&Wr; + rdwr.fn_read = ::NanaZipCodecsLz5Read; + rdwr.fn_write = ::NanaZipCodecsLz5Write; + rdwr.arg_read = reinterpret_cast(&ReadContext); + rdwr.arg_write = reinterpret_cast(&WriteContext); /* 2) create decompression context */ LZ5MT_DCtx *ctx = LZ5MT_createDCtx(_numThreads, _inputSize); diff --git a/NanaZip.Core/Extensions/ZSCodecs/Lz5Decoder.h b/NanaZip.Core/Extensions/ZSCodecs/Lz5Decoder.h index 13be28745..f1c96895c 100644 --- a/NanaZip.Core/Extensions/ZSCodecs/Lz5Decoder.h +++ b/NanaZip.Core/Extensions/ZSCodecs/Lz5Decoder.h @@ -4,7 +4,6 @@ #include "../../SevenZip/C/Alloc.h" #include "../../SevenZip/C/Threads.h" #include -#include #include "../../SevenZip/CPP/Windows/System.h" #include "../../SevenZip/CPP/Common/Common.h" @@ -14,16 +13,7 @@ #include "../../SevenZip/CPP/7zip/Common/RegisterCodec.h" #include "../../SevenZip/CPP/7zip/Common/ProgressMt.h" -struct Lz5Stream { - ISequentialInStream *inStream; - ISequentialOutStream *outStream; - ICompressProgressInfo *progress; - UInt64 *processedIn; - UInt64 *processedOut; -}; - -extern int Lz5Read(void *Stream, LZ5MT_Buffer * in); -extern int Lz5Write(void *Stream, LZ5MT_Buffer * in); +#include namespace NCompress { namespace NLZ5 { diff --git a/NanaZip.Core/Extensions/ZSCodecs/Lz5Encoder.cpp b/NanaZip.Core/Extensions/ZSCodecs/Lz5Encoder.cpp index a223ca840..e12afb54e 100644 --- a/NanaZip.Core/Extensions/ZSCodecs/Lz5Encoder.cpp +++ b/NanaZip.Core/Extensions/ZSCodecs/Lz5Encoder.cpp @@ -75,27 +75,27 @@ STDMETHODIMP CEncoder::Code(ISequentialInStream *inStream, size_t result; HRESULT res = S_OK; - struct Lz5Stream Rd; - Rd.inStream = inStream; - Rd.outStream = outStream; - Rd.processedIn = &_processedIn; - Rd.processedOut = &_processedOut; - - struct Lz5Stream 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 = ::Lz5Read; - rdwr.fn_write = ::Lz5Write; - rdwr.arg_read = (void *)&Rd; - rdwr.arg_write = (void *)&Wr; + rdwr.fn_read = ::NanaZipCodecsLz5Read; + rdwr.fn_write = ::NanaZipCodecsLz5Write; + rdwr.arg_read = reinterpret_cast(&ReadContext); + rdwr.arg_write = reinterpret_cast(&WriteContext); /* 2) create compression context, if needed */ if (!_ctx)