diff options
Diffstat (limited to 'CPP/7zip/Compress/BZip2Original')
-rwxr-xr-x | CPP/7zip/Compress/BZip2Original/BZip2Decoder.cpp | 131 | ||||
-rwxr-xr-x | CPP/7zip/Compress/BZip2Original/BZip2Decoder.h | 38 | ||||
-rwxr-xr-x | CPP/7zip/Compress/BZip2Original/BZip2Encoder.cpp | 120 | ||||
-rwxr-xr-x | CPP/7zip/Compress/BZip2Original/BZip2Encoder.h | 30 | ||||
-rwxr-xr-x | CPP/7zip/Compress/BZip2Original/BZip2Error.cpp | 10 | ||||
-rwxr-xr-x | CPP/7zip/Compress/BZip2Original/DllExports.cpp | 86 | ||||
-rwxr-xr-x | CPP/7zip/Compress/BZip2Original/StdAfx.cpp | 3 | ||||
-rwxr-xr-x | CPP/7zip/Compress/BZip2Original/StdAfx.h | 8 |
8 files changed, 426 insertions, 0 deletions
diff --git a/CPP/7zip/Compress/BZip2Original/BZip2Decoder.cpp b/CPP/7zip/Compress/BZip2Original/BZip2Decoder.cpp new file mode 100755 index 00000000..5fb77d01 --- /dev/null +++ b/CPP/7zip/Compress/BZip2Original/BZip2Decoder.cpp @@ -0,0 +1,131 @@ +// BZip2Decoder.cpp + +#include "StdAfx.h" + +#include "BZip2Decoder.h" + +#include "../../../Common/Alloc.h" +#include "Original/bzlib.h" + +namespace NCompress { +namespace NBZip2 { + +static const UInt32 kBufferSize = (1 << 20); + +CDecoder::~CDecoder() +{ + BigFree(m_InBuffer); +} + +struct CBZip2Decompressor: public bz_stream +{ + int Init(int verbosity, int small) { return BZ2_bzDecompressInit(this, verbosity, small); } + int Decompress() { return BZ2_bzDecompress(this); } + int End() { return BZ2_bzDecompressEnd(this); } + UInt64 GetTotalIn() const { return (UInt64(total_in_hi32) << 32) + total_in_lo32; } + UInt64 GetTotalOut() const { return (UInt64(total_out_hi32) << 32) + total_out_lo32; } +}; + +class CBZip2DecompressorReleaser +{ + CBZip2Decompressor *m_Decompressor; +public: + CBZip2DecompressorReleaser(CBZip2Decompressor *decompressor): m_Decompressor(decompressor) {} + void Diable() { m_Decompressor = NULL; } + ~CBZip2DecompressorReleaser() { if (m_Decompressor != NULL) m_Decompressor->End(); } +}; + +STDMETHODIMP CDecoder::CodeReal(ISequentialInStream *inStream, + ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize, + ICompressProgressInfo *progress) +{ + m_InSize = 0; + if (m_InBuffer == 0) + { + m_InBuffer = (Byte *)BigAlloc(kBufferSize * 2); + if (m_InBuffer == 0) + return E_OUTOFMEMORY; + } + Byte *outBuffer = m_InBuffer + kBufferSize; + + CBZip2Decompressor bzStream; + bzStream.bzalloc = NULL; + bzStream.bzfree = NULL; + bzStream.opaque = NULL; + + int result = bzStream.Init(0, 0); + switch(result) + { + case BZ_OK: + break; + case BZ_MEM_ERROR: + return E_OUTOFMEMORY; + default: + return E_FAIL; + } + CBZip2DecompressorReleaser releaser(&bzStream); + bzStream.avail_in = 0; + for (;;) + { + if (bzStream.avail_in == 0) + { + bzStream.next_in = (char *)m_InBuffer; + UInt32 processedSize; + RINOK(inStream->Read(m_InBuffer, kBufferSize, &processedSize)); + bzStream.avail_in = processedSize; + } + + bzStream.next_out = (char *)outBuffer; + bzStream.avail_out = kBufferSize; + result = bzStream.Decompress(); + UInt32 numBytesToWrite = kBufferSize - bzStream.avail_out; + if (numBytesToWrite > 0) + { + UInt32 processedSize; + RINOK(outStream->Write(outBuffer, numBytesToWrite, &processedSize)); + if (numBytesToWrite != processedSize) + return E_FAIL; + } + + if (result == BZ_STREAM_END) + break; + switch(result) + { + case BZ_DATA_ERROR: + case BZ_DATA_ERROR_MAGIC: + return S_FALSE; + case BZ_OK: + break; + case BZ_MEM_ERROR: + return E_OUTOFMEMORY; + default: + return E_FAIL; + } + if (progress != NULL) + { + UInt64 totalIn = bzStream.GetTotalIn(); + UInt64 totalOut = bzStream.GetTotalOut(); + RINOK(progress->SetRatioInfo(&totalIn, &totalOut)); + } + } + m_InSize = bzStream.GetTotalIn(); + return S_OK; +} + +STDMETHODIMP CDecoder::Code(ISequentialInStream *inStream, + ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize, + ICompressProgressInfo *progress) +{ + try { return CodeReal(inStream, outStream, inSize, outSize, progress); } + catch(...) { return S_FALSE; } +} + +STDMETHODIMP CDecoder::GetInStreamProcessedSize(UInt64 *value) +{ + if (value == NULL) + return E_INVALIDARG; + *value = m_InSize; + return S_OK; +} + +}} diff --git a/CPP/7zip/Compress/BZip2Original/BZip2Decoder.h b/CPP/7zip/Compress/BZip2Original/BZip2Decoder.h new file mode 100755 index 00000000..e41f730f --- /dev/null +++ b/CPP/7zip/Compress/BZip2Original/BZip2Decoder.h @@ -0,0 +1,38 @@ +// Compress/BZip2/Decoder.h + +#ifndef __COMPRESS_BZIP2_DECODER_H +#define __COMPRESS_BZIP2_DECODER_H + +#include "../../ICoder.h" +#include "../../../Common/MyCom.h" + +namespace NCompress { +namespace NBZip2 { + +class CDecoder : + public ICompressCoder, + public ICompressGetInStreamProcessedSize, + public CMyUnknownImp +{ + Byte *m_InBuffer; + UInt64 m_InSize; +public: + CDecoder(): m_InBuffer(0), m_InSize(0) {}; + ~CDecoder(); + + MY_UNKNOWN_IMP1(ICompressGetInStreamProcessedSize) + + STDMETHOD(CodeReal)(ISequentialInStream *inStream, + ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize, + ICompressProgressInfo *progress); + + STDMETHOD(Code)(ISequentialInStream *inStream, + ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize, + ICompressProgressInfo *progress); + + STDMETHOD(GetInStreamProcessedSize)(UInt64 *value); +}; + +}} + +#endif diff --git a/CPP/7zip/Compress/BZip2Original/BZip2Encoder.cpp b/CPP/7zip/Compress/BZip2Original/BZip2Encoder.cpp new file mode 100755 index 00000000..bcd7dec0 --- /dev/null +++ b/CPP/7zip/Compress/BZip2Original/BZip2Encoder.cpp @@ -0,0 +1,120 @@ +// BZip2Encoder.cpp + +#include "StdAfx.h" + +#include "BZip2Encoder.h" + +#include "../../../Common/Alloc.h" +#include "Original/bzlib.h" + +namespace NCompress { +namespace NBZip2 { + +static const UInt32 kBufferSize = (1 << 20); + +CEncoder::~CEncoder() +{ + BigFree(m_InBuffer); +} + +struct CBZip2Compressor: public bz_stream +{ + int Init(int blockSize100k, int verbosity, int small) + { return BZ2_bzCompressInit(this, blockSize100k, verbosity, small); } + int Compress(int action ) { return BZ2_bzCompress(this, action ); } + int End() { return BZ2_bzCompressEnd(this); } + UInt64 GetTotalIn() const { return (UInt64(total_in_hi32) << 32) + total_in_lo32; } + UInt64 GetTotalOut() const { return (UInt64(total_out_hi32) << 32) + total_out_lo32; } +}; + +class CBZip2CompressorReleaser +{ + CBZip2Compressor *m_Compressor; +public: + CBZip2CompressorReleaser(CBZip2Compressor *compressor): m_Compressor(compressor) {} + void Disable() { m_Compressor = NULL; } + ~CBZip2CompressorReleaser() { if (m_Compressor != NULL) m_Compressor->End(); } +}; + + +STDMETHODIMP CEncoder::Code(ISequentialInStream *inStream, + ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize, + ICompressProgressInfo *progress) +{ + if (m_InBuffer == 0) + { + m_InBuffer = (Byte *)BigAlloc(kBufferSize * 2); + if (m_InBuffer == 0) + return E_OUTOFMEMORY; + } + Byte *outBuffer = m_InBuffer + kBufferSize; + + CBZip2Compressor bzStream; + bzStream.bzalloc = NULL; + bzStream.bzfree = NULL; + bzStream.opaque = NULL; + + int result = bzStream.Init(9, 0, 0); + switch(result) + { + case BZ_OK: + break; + case BZ_MEM_ERROR: + return E_OUTOFMEMORY; + default: + return E_FAIL; + } + CBZip2CompressorReleaser releaser(&bzStream); + bzStream.avail_in = 0; + for (;;) + { + if (bzStream.avail_in == 0) + { + bzStream.next_in = (char *)m_InBuffer; + UInt32 processedSize; + RINOK(inStream->Read(m_InBuffer, kBufferSize, &processedSize)); + bzStream.avail_in = processedSize; + } + + bzStream.next_out = (char *)outBuffer; + bzStream.avail_out = kBufferSize; + bool askFinish = (bzStream.avail_in == 0); + result = bzStream.Compress(askFinish ? BZ_FINISH : BZ_RUN); + UInt32 numBytesToWrite = kBufferSize - bzStream.avail_out; + if (numBytesToWrite > 0) + { + UInt32 processedSize; + RINOK(outStream->Write(outBuffer, numBytesToWrite, &processedSize)); + if (numBytesToWrite != processedSize) + return E_FAIL; + } + + if (result == BZ_STREAM_END) + break; + switch(result) + { + case BZ_RUN_OK: + if (!askFinish) + break; + return E_FAIL; + case BZ_FINISH_OK: + if (askFinish) + break; + return E_FAIL; + case BZ_MEM_ERROR: + return E_OUTOFMEMORY; + default: + return E_FAIL; + } + if (progress != NULL) + { + UInt64 totalIn = bzStream.GetTotalIn(); + UInt64 totalOut = bzStream.GetTotalOut(); + RINOK(progress->SetRatioInfo(&totalIn, &totalOut)); + } + } + // result = bzStream.End(); + return S_OK; +} + +}} diff --git a/CPP/7zip/Compress/BZip2Original/BZip2Encoder.h b/CPP/7zip/Compress/BZip2Original/BZip2Encoder.h new file mode 100755 index 00000000..c451a08d --- /dev/null +++ b/CPP/7zip/Compress/BZip2Original/BZip2Encoder.h @@ -0,0 +1,30 @@ +// Compress/BZip2/Encoder.h + +#ifndef __COMPRESS_BZIP2_ENCODER_H +#define __COMPRESS_BZIP2_ENCODER_H + +#include "../../ICoder.h" +#include "../../../Common/MyCom.h" + +namespace NCompress { +namespace NBZip2 { + +class CEncoder : + public ICompressCoder, + public CMyUnknownImp +{ + Byte *m_InBuffer; +public: + CEncoder(): m_InBuffer(0) {}; + ~CEncoder(); + + MY_UNKNOWN_IMP + + STDMETHOD(Code)(ISequentialInStream *inStream, + ISequentialOutStream *outStream, const UInt64 *inSize, const UInt64 *outSize, + ICompressProgressInfo *progress); +}; + +}} + +#endif diff --git a/CPP/7zip/Compress/BZip2Original/BZip2Error.cpp b/CPP/7zip/Compress/BZip2Original/BZip2Error.cpp new file mode 100755 index 00000000..c8a912dc --- /dev/null +++ b/CPP/7zip/Compress/BZip2Original/BZip2Error.cpp @@ -0,0 +1,10 @@ +#include "StdAfx.h" + +#include "Original/bzlib.h" + +extern "C" + +void bz_internal_error (int errcode) +{ + throw "error"; +}
\ No newline at end of file diff --git a/CPP/7zip/Compress/BZip2Original/DllExports.cpp b/CPP/7zip/Compress/BZip2Original/DllExports.cpp new file mode 100755 index 00000000..572d1fda --- /dev/null +++ b/CPP/7zip/Compress/BZip2Original/DllExports.cpp @@ -0,0 +1,86 @@ +// DLLExports.cpp + +#include "StdAfx.h" + +#include "Common/MyInitGuid.h" +#include "Common/ComTry.h" + +#include "BZip2Encoder.h" +#include "BZip2Decoder.h" + +// {23170F69-40C1-278B-0402-020000000000} +DEFINE_GUID(CLSID_CCompressBZip2Decoder, +0x23170F69, 0x40C1, 0x278B, 0x04, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00); + +// {23170F69-40C1-278B-0402-020000000100} +DEFINE_GUID(CLSID_CCompressBZip2Encoder, +0x23170F69, 0x40C1, 0x278B, 0x04, 0x02, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00); + +extern "C" +BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/) +{ + return TRUE; +} + +STDAPI CreateObject(const GUID *clsid, const GUID *iid, void **outObject) +{ + COM_TRY_BEGIN + *outObject = 0; + int correctInterface = (*iid == IID_ICompressCoder); + CMyComPtr<ICompressCoder> coder; + if (*clsid == CLSID_CCompressBZip2Decoder) + { + if (!correctInterface) + return E_NOINTERFACE; + coder = (ICompressCoder *)new NCompress::NBZip2::CDecoder; + } + else if (*clsid == CLSID_CCompressBZip2Encoder) + { + if (!correctInterface) + return E_NOINTERFACE; + coder = (ICompressCoder *)new NCompress::NBZip2::CEncoder; + } + else + return CLASS_E_CLASSNOTAVAILABLE; + *outObject = coder.Detach(); + COM_TRY_END + return S_OK; +} + +STDAPI GetNumberOfMethods(UINT32 *numMethods) +{ + *numMethods = 1; + return S_OK; +} + +STDAPI GetMethodProperty(UINT32 index, PROPID propID, PROPVARIANT *value) +{ + if (index != 0) + return E_INVALIDARG; + ::VariantClear((tagVARIANT *)value); + switch(propID) + { + case NMethodPropID::kID: + { + const char id[] = { 0x04, 0x02, 0x02 }; + if ((value->bstrVal = ::SysAllocStringByteLen(id, sizeof(id))) != 0) + value->vt = VT_BSTR; + return S_OK; + } + case NMethodPropID::kName: + if ((value->bstrVal = ::SysAllocString(L"BZip2")) != 0) + value->vt = VT_BSTR; + return S_OK; + case NMethodPropID::kDecoder: + if ((value->bstrVal = ::SysAllocStringByteLen( + (const char *)&CLSID_CCompressBZip2Decoder, sizeof(GUID))) != 0) + value->vt = VT_BSTR; + return S_OK; + case NMethodPropID::kEncoder: + if ((value->bstrVal = ::SysAllocStringByteLen( + (const char *)&CLSID_CCompressBZip2Encoder, sizeof(GUID))) != 0) + value->vt = VT_BSTR; + return S_OK; + } + return S_OK; +} diff --git a/CPP/7zip/Compress/BZip2Original/StdAfx.cpp b/CPP/7zip/Compress/BZip2Original/StdAfx.cpp new file mode 100755 index 00000000..d0feea85 --- /dev/null +++ b/CPP/7zip/Compress/BZip2Original/StdAfx.cpp @@ -0,0 +1,3 @@ +// StdAfx.cpp + +#include "StdAfx.h" diff --git a/CPP/7zip/Compress/BZip2Original/StdAfx.h b/CPP/7zip/Compress/BZip2Original/StdAfx.h new file mode 100755 index 00000000..e7fb6986 --- /dev/null +++ b/CPP/7zip/Compress/BZip2Original/StdAfx.h @@ -0,0 +1,8 @@ +// StdAfx.h + +#ifndef __STDAFX_H +#define __STDAFX_H + +#include "../../../Common/MyWindows.h" + +#endif |