hrev47449 adds 9 changesets to branch 'master' old head: 955ace81fdbd75c66d0d9b75b0709e732f19015d new head: b3263ad3e1662cc419849a2d2cf539f9636613dc overview: http://cgit.haiku-os.org/haiku/log/?qt=range&q=b3263ad+%5E955ace8 ---------------------------------------------------------------------------- f1a12bc: ReaderImplBase::ReadSection(): Small simplification 0a62e01: PackageWriterImpl: Remove superfluous method 0dab9e5: package kit: ReaderImplBase: Remove superfluous scratch buffer 60ccc11: packagefs: CachedDataReader: Don't implement ReadData() It's already done the same way in the base class. b773d89: BDataIO: Add Flush() 6a89a36: Move package kit Zlib* classes to support kit Also move to B* namespace and no longer expose the zlib dependency in the headers. dcdc33b: Add B[Zlib]CompressionAlgorithm * BCompressionAlgorithm is a base class for classes that provide compression/decompression functionality. There are methods for compressing/decompressing a single buffer and factory methods for a compressing/decompressing input/output BDataIO. * BZlibCompressionAlgorithm is a BCompressionAlgorithm implementation using zlib. 377ecc1: Test program for the B[Zlib]CompressionAlgorithm functionality b3263ad: Switch package kit to BZlibCompressionAlgorithm ... and remove the Zlib{Compressor,Decompressor} API. [ Ingo Weinhold <ingo_weinhold@xxxxxx> ] ---------------------------------------------------------------------------- 49 files changed, 1457 insertions(+), 712 deletions(-) docs/user/support/DataIO.dox | 20 +- .../private/package/hpkg/ZlibCompressionBase.h | 1 - .../build/private/package/hpkg/ZlibCompressor.h | 1 - .../private/package/hpkg/ZlibDecompressor.h | 1 - .../build/private/support/CompressionAlgorithm.h | 1 + .../private/support/ZlibCompressionAlgorithm.h | 1 + headers/os/support/DataIO.h | 3 +- .../package/hpkg/PackageFileHeapAccessorBase.h | 50 ++- .../private/package/hpkg/PackageFileHeapReader.h | 4 +- .../private/package/hpkg/PackageFileHeapWriter.h | 9 +- headers/private/package/hpkg/PackageWriterImpl.h | 6 - headers/private/package/hpkg/ReaderImplBase.h | 7 +- headers/private/package/hpkg/WriterImplBase.h | 13 +- .../private/package/hpkg/ZlibCompressionBase.h | 32 -- headers/private/package/hpkg/ZlibCompressor.h | 53 --- headers/private/package/hpkg/ZlibDecompressor.h | 54 --- headers/private/support/CompressionAlgorithm.h | 121 ++++++ .../private/support/ZlibCompressionAlgorithm.h | 96 +++++ .../kernel/file_systems/packagefs/Jamfile | 17 +- .../packagefs/package/CachedDataReader.cpp | 8 - .../packagefs/package/CachedDataReader.h | 2 - src/build/libbe/Jamfile | 2 +- src/build/libbe/support/Jamfile | 2 + src/build/libpackage/Jamfile | 9 +- src/kits/Jamfile | 8 +- src/kits/package/Jamfile | 10 +- src/kits/package/hpkg/PackageDataReader.cpp | 1 - .../package/hpkg/PackageFileHeapAccessorBase.cpp | 16 +- src/kits/package/hpkg/PackageFileHeapReader.cpp | 14 +- src/kits/package/hpkg/PackageFileHeapWriter.cpp | 30 +- src/kits/package/hpkg/PackageReaderImpl.cpp | 3 +- src/kits/package/hpkg/PackageWriterImpl.cpp | 4 - src/kits/package/hpkg/ReaderImplBase.cpp | 39 +- src/kits/package/hpkg/WriterImplBase.cpp | 44 +- src/kits/package/hpkg/ZlibCompressionBase.cpp | 54 --- src/kits/package/hpkg/ZlibCompressor.cpp | 186 --------- src/kits/package/hpkg/ZlibDecompressor.cpp | 192 --------- src/kits/package/hpkg/v1/PackageDataReaderV1.cpp | 5 +- src/kits/package/hpkg/v1/PackageReaderImplV1.cpp | 1 - src/kits/package/hpkg/v1/ReaderImplBaseV1.cpp | 25 +- src/kits/support/CompressionAlgorithm.cpp | 328 +++++++++++++++ src/kits/support/DataIO.cpp | 33 +- src/kits/support/Jamfile | 7 + src/kits/support/ZlibCompressionAlgorithm.cpp | 411 +++++++++++++++++++ .../boot/loader/file_systems/packagefs/Jamfile | 15 +- .../boot/loader/file_systems/tarfs/Jamfile | 7 +- src/system/kernel/lib/zlib/Jamfile | 7 +- src/tests/kits/support/Jamfile | 3 + src/tests/kits/support/compression_test.cpp | 213 ++++++++++ ############################################################################ Commit: f1a12bcfc00e89fda10935eb607b210bde74976f URL: http://cgit.haiku-os.org/haiku/commit/?id=f1a12bc Author: Ingo Weinhold <ingo_weinhold@xxxxxx> Date: Tue Jun 24 04:09:00 2014 UTC ReaderImplBase::ReadSection(): Small simplification ---------------------------------------------------------------------------- diff --git a/src/kits/package/hpkg/ReaderImplBase.cpp b/src/kits/package/hpkg/ReaderImplBase.cpp index 0a421b0..1deb091 100644 --- a/src/kits/package/hpkg/ReaderImplBase.cpp +++ b/src/kits/package/hpkg/ReaderImplBase.cpp @@ -1358,9 +1358,8 @@ ReaderImplBase::ReadBuffer(off_t offset, void* buffer, size_t size) status_t ReaderImplBase::ReadSection(const PackageFileSection& section) { - BMemoryIO output(section.data, section.uncompressedLength); - return fHeapReader->ReadDataToOutput(section.offset, - section.uncompressedLength, &output); + return fHeapReader->ReadData(section.offset, + section.data, section.uncompressedLength); } ############################################################################ Commit: 0a62e0190298a7930a1baf27a4d72de077fbb051 URL: http://cgit.haiku-os.org/haiku/commit/?id=0a62e01 Author: Ingo Weinhold <ingo_weinhold@xxxxxx> Date: Mon Jun 30 19:18:10 2014 UTC PackageWriterImpl: Remove superfluous method ---------------------------------------------------------------------------- diff --git a/headers/private/package/hpkg/PackageWriterImpl.h b/headers/private/package/hpkg/PackageWriterImpl.h index c9499c8..f74c1ba 100644 --- a/headers/private/package/hpkg/PackageWriterImpl.h +++ b/headers/private/package/hpkg/PackageWriterImpl.h @@ -36,7 +36,6 @@ namespace BPrivate { struct hpkg_header; -class PackageFileHeapWriter; class PackageWriterImpl : public WriterImplBase { @@ -116,11 +115,6 @@ private: status_t _AddData(BDataReader& dataReader, off_t size); - status_t _WriteZlibCompressedData( - BDataReader& dataReader, - off_t size, uint64 writeOffset, - uint64& _compressedSize); - private: BPackageWriterListener* fListener; ############################################################################ Commit: 0dab9e5cb4e244814a39d562cef87f9508780760 URL: http://cgit.haiku-os.org/haiku/commit/?id=0dab9e5 Author: Ingo Weinhold <ingo_weinhold@xxxxxx> Date: Sun Jun 29 16:35:29 2014 UTC package kit: ReaderImplBase: Remove superfluous scratch buffer ---------------------------------------------------------------------------- diff --git a/headers/private/package/hpkg/ReaderImplBase.h b/headers/private/package/hpkg/ReaderImplBase.h index 9d04175..aea50e8 100644 --- a/headers/private/package/hpkg/ReaderImplBase.h +++ b/headers/private/package/hpkg/ReaderImplBase.h @@ -197,9 +197,6 @@ private: PackageFileSection* fCurrentSection; AttributeHandlerList fAttributeHandlerStack; - - uint8* fScratchBuffer; - size_t fScratchBufferSize; }; diff --git a/src/kits/package/hpkg/ReaderImplBase.cpp b/src/kits/package/hpkg/ReaderImplBase.cpp index 1deb091..17302c0 100644 --- a/src/kits/package/hpkg/ReaderImplBase.cpp +++ b/src/kits/package/hpkg/ReaderImplBase.cpp @@ -767,9 +767,7 @@ ReaderImplBase::ReaderImplBase(const char* fileType, BErrorOutput* errorOutput) fOwnsFD(false), fRawHeapReader(NULL), fHeapReader(NULL), - fCurrentSection(NULL), - fScratchBuffer(NULL), - fScratchBufferSize(0) + fCurrentSection(NULL) { } @@ -782,8 +780,6 @@ ReaderImplBase::~ReaderImplBase() if (fOwnsFD && fFD >= 0) close(fFD); - - delete[] fScratchBuffer; } @@ -1054,14 +1050,6 @@ ReaderImplBase::_Init(int fd, bool keepFD) fFD = fd; fOwnsFD = keepFD; - // allocate a scratch buffer - fScratchBuffer = new(std::nothrow) uint8[kScratchBufferSize]; - if (fScratchBuffer == NULL) { - fErrorOutput->PrintError("Error: Out of memory!\n"); - return B_NO_MEMORY; - } - fScratchBufferSize = kScratchBufferSize; - return B_OK; } ############################################################################ Commit: 60ccc119b0fab7e8814236b2f0dab6e0eda4410d URL: http://cgit.haiku-os.org/haiku/commit/?id=60ccc11 Author: Ingo Weinhold <ingo_weinhold@xxxxxx> Date: Tue Jun 24 04:14:46 2014 UTC packagefs: CachedDataReader: Don't implement ReadData() It's already done the same way in the base class. ---------------------------------------------------------------------------- diff --git a/src/add-ons/kernel/file_systems/packagefs/package/CachedDataReader.cpp b/src/add-ons/kernel/file_systems/packagefs/package/CachedDataReader.cpp index 59bd8bc..187e44f 100644 --- a/src/add-ons/kernel/file_systems/packagefs/package/CachedDataReader.cpp +++ b/src/add-ons/kernel/file_systems/packagefs/package/CachedDataReader.cpp @@ -124,14 +124,6 @@ CachedDataReader::Init(BAbstractBufferedDataReader* reader, off_t size) status_t -CachedDataReader::ReadData(off_t offset, void* buffer, size_t size) -{ - BMemoryIO output(buffer, size); - return ReadDataToOutput(offset, size, &output); -} - - -status_t CachedDataReader::ReadDataToOutput(off_t offset, size_t size, BDataIO* output) { diff --git a/src/add-ons/kernel/file_systems/packagefs/package/CachedDataReader.h b/src/add-ons/kernel/file_systems/packagefs/package/CachedDataReader.h index 8cb87c6..d61490a 100644 --- a/src/add-ons/kernel/file_systems/packagefs/package/CachedDataReader.h +++ b/src/add-ons/kernel/file_systems/packagefs/package/CachedDataReader.h @@ -26,8 +26,6 @@ public: status_t Init(BAbstractBufferedDataReader* reader, off_t size); - virtual status_t ReadData(off_t offset, void* buffer, - size_t size); virtual status_t ReadDataToOutput(off_t offset, size_t size, BDataIO* output); ############################################################################ Commit: b773d89eba723039d3d293df77e106c128efa878 URL: http://cgit.haiku-os.org/haiku/commit/?id=b773d89 Author: Ingo Weinhold <ingo_weinhold@xxxxxx> Date: Sun Jun 29 16:14:07 2014 UTC BDataIO: Add Flush() ---------------------------------------------------------------------------- diff --git a/docs/user/support/DataIO.dox b/docs/user/support/DataIO.dox index 7a2f382..52f2015 100644 --- a/docs/user/support/DataIO.dox +++ b/docs/user/support/DataIO.dox @@ -102,8 +102,24 @@ /*! - \fn virtual status_t BDataIO::ReadExactly(void* buffer, size_t size, - size_t* _bytesRead) + \fn virtual status_t BDataIO::Flush() + \brief Writes pending data to underlying storage. + + This method is relevant for BDataIO implementations that buffer data passed + to Write(). The Flush() implementation should make sure that all such data + are written to the underlying storage. + + The default implementation is a no-op returning \c B_OK. + + \return An error code indicating whether flushing the buffered data + succeeded. + + \since Haiku R1 +*/ + + +/*! + \fn virtual status_t BDataIO::ReadExactly(void* buffer, size_t size, size_t* _bytesRead) \brief Reads an exact amount of data from the object into a buffer. This is a convenience wrapper method for Read() for code that expects the diff --git a/headers/os/support/DataIO.h b/headers/os/support/DataIO.h index e39bdcf..5e3d9bc 100644 --- a/headers/os/support/DataIO.h +++ b/headers/os/support/DataIO.h @@ -17,6 +17,8 @@ public: virtual ssize_t Read(void* buffer, size_t size); virtual ssize_t Write(const void* buffer, size_t size); + virtual status_t Flush(); + status_t ReadExactly(void* buffer, size_t size, size_t* _bytesRead = NULL); status_t WriteExactly(const void* buffer, size_t size, @@ -26,7 +28,6 @@ private: BDataIO(const BDataIO&); BDataIO& operator=(const BDataIO&); - virtual void _ReservedDataIO1(); virtual void _ReservedDataIO2(); virtual void _ReservedDataIO3(); virtual void _ReservedDataIO4(); diff --git a/src/kits/support/DataIO.cpp b/src/kits/support/DataIO.cpp index 90c4b8c..55e65b4 100644 --- a/src/kits/support/DataIO.cpp +++ b/src/kits/support/DataIO.cpp @@ -41,6 +41,13 @@ BDataIO::Write(const void* buffer, size_t size) status_t +BDataIO::Flush() +{ + return B_OK; +} + + +status_t BDataIO::ReadExactly(void* buffer, size_t size, size_t* _bytesRead) { uint8* out = (uint8*)buffer; @@ -116,8 +123,32 @@ BDataIO::operator=(const BDataIO &) } +#if __GNUC__ == 2 + + +extern "C" status_t +_ReservedDataIO1__7BDataIO(BDataIO* self) +{ + return self->BDataIO::Flush(); +} + + +#else + + +// TODO: RELEASE: Remove! + +extern "C" status_t +_ZN7BDataIO16_ReservedDataIO1Ev(BDataIO* self) +{ + return self->BDataIO::Flush(); +} + + +#endif + + // FBC -void BDataIO::_ReservedDataIO1(){} void BDataIO::_ReservedDataIO2(){} void BDataIO::_ReservedDataIO3(){} void BDataIO::_ReservedDataIO4(){} ############################################################################ Commit: 6a89a36aa0ce6a53f9758ba767863ac6b4c6156c URL: http://cgit.haiku-os.org/haiku/commit/?id=6a89a36 Author: Ingo Weinhold <ingo_weinhold@xxxxxx> Date: Tue Jun 24 06:59:29 2014 UTC Move package kit Zlib* classes to support kit Also move to B* namespace and no longer expose the zlib dependency in the headers. ---------------------------------------------------------------------------- diff --git a/headers/build/private/package/hpkg/ZlibCompressionBase.h b/headers/build/private/package/hpkg/ZlibCompressionBase.h deleted file mode 100644 index 052cfd0..0000000 --- a/headers/build/private/package/hpkg/ZlibCompressionBase.h +++ /dev/null @@ -1 +0,0 @@ -#include <../private/package/hpkg/ZlibCompressionBase.h> diff --git a/headers/build/private/package/hpkg/ZlibCompressor.h b/headers/build/private/package/hpkg/ZlibCompressor.h deleted file mode 100644 index 5386520..0000000 --- a/headers/build/private/package/hpkg/ZlibCompressor.h +++ /dev/null @@ -1 +0,0 @@ -#include <../private/package/hpkg/ZlibCompressor.h> diff --git a/headers/build/private/package/hpkg/ZlibDecompressor.h b/headers/build/private/package/hpkg/ZlibDecompressor.h deleted file mode 100644 index 698aca6..0000000 --- a/headers/build/private/package/hpkg/ZlibDecompressor.h +++ /dev/null @@ -1 +0,0 @@ -#include <../private/package/hpkg/ZlibDecompressor.h> diff --git a/headers/build/private/support/ZlibCompressionBase.h b/headers/build/private/support/ZlibCompressionBase.h new file mode 100644 index 0000000..7f5ffaa --- /dev/null +++ b/headers/build/private/support/ZlibCompressionBase.h @@ -0,0 +1 @@ +#include <../private/support/ZlibCompressionBase.h> diff --git a/headers/build/private/support/ZlibCompressor.h b/headers/build/private/support/ZlibCompressor.h new file mode 100644 index 0000000..f25a79f --- /dev/null +++ b/headers/build/private/support/ZlibCompressor.h @@ -0,0 +1 @@ +#include <../private/support/ZlibCompressor.h> diff --git a/headers/build/private/support/ZlibDecompressor.h b/headers/build/private/support/ZlibDecompressor.h new file mode 100644 index 0000000..daee4ca --- /dev/null +++ b/headers/build/private/support/ZlibDecompressor.h @@ -0,0 +1 @@ +#include <../private/support/ZlibDecompressor.h> diff --git a/headers/private/package/hpkg/WriterImplBase.h b/headers/private/package/hpkg/WriterImplBase.h index 907932c..f8429d0 100644 --- a/headers/private/package/hpkg/WriterImplBase.h +++ b/headers/private/package/hpkg/WriterImplBase.h @@ -13,7 +13,6 @@ #include <package/hpkg/PackageFileHeapWriter.h> #include <package/hpkg/PackageWriter.h> #include <package/hpkg/Strings.h> -#include <package/hpkg/ZlibCompressor.h> #include <package/PackageInfo.h> diff --git a/headers/private/package/hpkg/ZlibCompressionBase.h b/headers/private/package/hpkg/ZlibCompressionBase.h deleted file mode 100644 index 786e579..0000000 --- a/headers/private/package/hpkg/ZlibCompressionBase.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright 2009, Ingo Weinhold, ingo_weinhold@xxxxxx. - * Distributed under the terms of the MIT License. - */ -#ifndef _PACKAGE__HPKG__PRIVATE__ZLIB_COMPRESSION_BASE_H_ -#define _PACKAGE__HPKG__PRIVATE__ZLIB_COMPRESSION_BASE_H_ - - -#include <SupportDefs.h> - - -namespace BPackageKit { - -namespace BHPKG { - -namespace BPrivate { - - -class ZlibCompressionBase { -public: - static status_t TranslateZlibError(int error); -}; - - -} // namespace BPrivate - -} // namespace BHPKG - -} // namespace BPackageKit - - -#endif // _PACKAGE__HPKG__PRIVATE__ZLIB_COMPRESSION_BASE_H_ diff --git a/headers/private/package/hpkg/ZlibCompressor.h b/headers/private/package/hpkg/ZlibCompressor.h deleted file mode 100644 index baefd3b..0000000 --- a/headers/private/package/hpkg/ZlibCompressor.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 2009-2014, Ingo Weinhold, ingo_weinhold@xxxxxx. - * Distributed under the terms of the MIT License. - */ -#ifndef _PACKAGE__HPKG__PRIVATE__ZLIB_COMPRESSOR_H_ -#define _PACKAGE__HPKG__PRIVATE__ZLIB_COMPRESSOR_H_ - - -#include <zlib.h> - -#include <package/hpkg/ZlibCompressionBase.h> - - -class BDataIO; - - -namespace BPackageKit { - -namespace BHPKG { - -namespace BPrivate { - - -class ZlibCompressor : public ZlibCompressionBase { -public: - ZlibCompressor(BDataIO* output); - ~ZlibCompressor(); - - status_t Init(int compressionLevel = Z_BEST_COMPRESSION); - status_t CompressNext(const void* input, - size_t inputSize); - status_t Finish(); - - static status_t CompressSingleBuffer(const void* input, - size_t inputSize, void* output, - size_t outputSize, size_t& _compressedSize, - int compressionLevel = Z_BEST_COMPRESSION); - -private: - z_stream fStream; - BDataIO* fOutput; - bool fStreamInitialized; -}; - - -} // namespace BPrivate - -} // namespace BHPKG - -} // namespace BPackageKit - - -#endif // _PACKAGE__HPKG__PRIVATE__ZLIB_COMPRESSOR_H_ diff --git a/headers/private/package/hpkg/ZlibDecompressor.h b/headers/private/package/hpkg/ZlibDecompressor.h deleted file mode 100644 index e045b4e..0000000 --- a/headers/private/package/hpkg/ZlibDecompressor.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2009-2014, Ingo Weinhold, ingo_weinhold@xxxxxx. - * Distributed under the terms of the MIT License. - */ -#ifndef _PACKAGE__HPKG__PRIVATE__ZLIB_DECOMPRESSOR_H_ -#define _PACKAGE__HPKG__PRIVATE__ZLIB_DECOMPRESSOR_H_ - - -#include <zlib.h> - -#include <package/hpkg/ZlibCompressionBase.h> - - -class BDataIO; - - -namespace BPackageKit { - -namespace BHPKG { - -namespace BPrivate { - - -class ZlibDecompressor : public ZlibCompressionBase { -public: - ZlibDecompressor(BDataIO* output); - ~ZlibDecompressor(); - - status_t Init(); - status_t DecompressNext(const void* input, - size_t inputSize); - status_t Finish(); - - static status_t DecompressSingleBuffer(const void* input, - size_t inputSize, void* output, - size_t outputSize, - size_t& _uncompressedSize); - -private: - z_stream fStream; - BDataIO* fOutput; - bool fStreamInitialized; - bool fFinished; -}; - - -} // namespace BPrivate - -} // namespace BHPKG - -} // namespace BPackageKit - - -#endif // _PACKAGE__HPKG__PRIVATE__ZLIB_DECOMPRESSOR_H_ diff --git a/headers/private/support/ZlibCompressionBase.h b/headers/private/support/ZlibCompressionBase.h new file mode 100644 index 0000000..c38eb40 --- /dev/null +++ b/headers/private/support/ZlibCompressionBase.h @@ -0,0 +1,35 @@ +/* + * Copyright 2009-2014, Ingo Weinhold, ingo_weinhold@xxxxxx. + * Distributed under the terms of the MIT License. + */ +#ifndef _ZLIB_COMPRESSION_BASE_H_ +#define _ZLIB_COMPRESSION_BASE_H_ + + +#include <SupportDefs.h> + + +class BDataIO; + + +class BZlibCompressionBase { +public: + BZlibCompressionBase(BDataIO* output); + ~BZlibCompressionBase(); + + static status_t TranslateZlibError(int error); + +protected: + struct ZStream; + +protected: + status_t CreateStream(); + void DeleteStream(); + +protected: + BDataIO* fOutput; + ZStream* fStream; +}; + + +#endif // _ZLIB_COMPRESSION_BASE_H_ diff --git a/headers/private/support/ZlibCompressor.h b/headers/private/support/ZlibCompressor.h new file mode 100644 index 0000000..793f719 --- /dev/null +++ b/headers/private/support/ZlibCompressor.h @@ -0,0 +1,43 @@ +/* + * Copyright 2009-2014, Ingo Weinhold, ingo_weinhold@xxxxxx. + * Distributed under the terms of the MIT License. + */ +#ifndef _ZLIB_COMPRESSOR_H_ +#define _ZLIB_COMPRESSOR_H_ + + +#include <ZlibCompressionBase.h> + + +class BDataIO; + + +// compression level +enum { + B_ZLIB_COMPRESSION_NONE = 0, + B_ZLIB_COMPRESSION_FASTEST = 1, + B_ZLIB_COMPRESSION_BEST = 9, + B_ZLIB_COMPRESSION_DEFAULT = -1, +}; + + +class BZlibCompressor : public BZlibCompressionBase { +public: + BZlibCompressor(BDataIO* output); + ~BZlibCompressor(); + + status_t Init(int compressionLevel + = B_ZLIB_COMPRESSION_BEST); + status_t CompressNext(const void* input, + size_t inputSize); + status_t Finish(); + + static status_t CompressSingleBuffer(const void* input, + size_t inputSize, void* output, + size_t outputSize, size_t& _compressedSize, + int compressionLevel + = B_ZLIB_COMPRESSION_BEST); +}; + + +#endif // _ZLIB_COMPRESSOR_H_ diff --git a/headers/private/support/ZlibDecompressor.h b/headers/private/support/ZlibDecompressor.h new file mode 100644 index 0000000..256122f --- /dev/null +++ b/headers/private/support/ZlibDecompressor.h @@ -0,0 +1,32 @@ +/* + * Copyright 2009-2014, Ingo Weinhold, ingo_weinhold@xxxxxx. + * Distributed under the terms of the MIT License. + */ +#ifndef _ZLIB_DECOMPRESSOR_H_ +#define _ZLIB_DECOMPRESSOR_H_ + + +#include <ZlibCompressionBase.h> + + +class BZlibDecompressor : public BZlibCompressionBase { +public: + BZlibDecompressor(BDataIO* output); + ~BZlibDecompressor(); + + status_t Init(); + status_t DecompressNext(const void* input, + size_t inputSize); + status_t Finish(); + + static status_t DecompressSingleBuffer(const void* input, + size_t inputSize, void* output, + size_t outputSize, + size_t& _uncompressedSize); + +private: + bool fFinished; +}; + + +#endif // _ZLIB_DECOMPRESSOR_H_ diff --git a/src/add-ons/kernel/file_systems/packagefs/Jamfile b/src/add-ons/kernel/file_systems/packagefs/Jamfile index ae95075..66b0b64 100644 --- a/src/add-ons/kernel/file_systems/packagefs/Jamfile +++ b/src/add-ons/kernel/file_systems/packagefs/Jamfile @@ -3,7 +3,7 @@ SubDir HAIKU_TOP src add-ons kernel file_systems packagefs ; UseBuildFeatureHeaders zlib ; UsePrivateKernelHeaders ; -UsePrivateHeaders package shared storage ; +UsePrivateHeaders package shared storage support ; local subDirs = @@ -90,10 +90,6 @@ HAIKU_PACKAGE_FS_PACKAGE_READER_SOURCES = PackageFileHeapReader.cpp PackageReaderImpl.cpp ReaderImplBase.cpp - - # compression - ZlibCompressionBase.cpp - ZlibDecompressor.cpp ; HAIKU_PACKAGE_FS_PACKAGE_READER_SOURCES_V1 = @@ -107,15 +103,17 @@ HAIKU_PACKAGE_FS_PACKAGE_READER_SOURCES_V1 = ReaderImplBaseV1.cpp ; -Includes - [ FGristFiles $(HAIKU_PACKAGE_FS_PACKAGE_READER_SOURCES) - $(HAIKU_PACKAGE_FS_PACKAGE_READER_SOURCES_V1) ] +Includes [ FGristFiles ZlibCompressionBasePrivate.h ] : [ BuildFeatureAttribute zlib : headers ] ; local libSharedSources = NaturalCompare.cpp ; +local supportKitSources = + ZlibCompressionBase.cpp + ZlibDecompressor.cpp +; KernelAddon packagefs : @@ -124,6 +122,7 @@ KernelAddon packagefs $(HAIKU_PACKAGE_FS_PACKAGE_READER_SOURCES) $(HAIKU_PACKAGE_FS_PACKAGE_READER_SOURCES_V1) $(libSharedSources) + $(supportKitSources) : $(TARGET_KERNEL_LIBSUPC++) kernel_libz.a ; @@ -137,3 +136,5 @@ SEARCH on [ FGristFiles $(HAIKU_PACKAGE_FS_PACKAGE_READER_SOURCES_V1) ] += [ FDirName $(HAIKU_TOP) src kits package hpkg v1 ] ; SEARCH on [ FGristFiles $(libSharedSources) ] += [ FDirName $(HAIKU_TOP) src kits shared ] ; +SEARCH on [ FGristFiles $(supportKitSources) ] + += [ FDirName $(HAIKU_TOP) src kits support ] ; diff --git a/src/build/libbe/Jamfile b/src/build/libbe/Jamfile index 80c84ea..70f487e 100644 --- a/src/build/libbe/Jamfile +++ b/src/build/libbe/Jamfile @@ -14,7 +14,7 @@ BuildPlatformSharedLibrary libbe_build.so : <libbe_build>storage_kit.o <libbe_build>support_kit.o - $(HOST_LIBSUPC++) $(HOST_LIBSTDC++) + z $(HOST_LIBSUPC++) $(HOST_LIBSTDC++) ; SubInclude HAIKU_TOP src build libbe app ; diff --git a/src/build/libbe/support/Jamfile b/src/build/libbe/support/Jamfile index 495ea54..9ec8591 100644 --- a/src/build/libbe/support/Jamfile +++ b/src/build/libbe/support/Jamfile @@ -18,4 +18,7 @@ BuildPlatformMergeObjectPIC <libbe_build>support_kit.o : Referenceable.cpp String.cpp StringList.cpp + ZlibCompressionBase.cpp + ZlibCompressor.cpp + ZlibDecompressor.cpp ; diff --git a/src/build/libpackage/Jamfile b/src/build/libpackage/Jamfile index 953bab1..73a4d08 100644 --- a/src/build/libpackage/Jamfile +++ b/src/build/libpackage/Jamfile @@ -1,6 +1,6 @@ SubDir HAIKU_TOP src build libpackage ; -UsePrivateBuildHeaders kernel package shared storage ; +UsePrivateBuildHeaders kernel package shared storage support ; SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src kits package ] ; SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src kits package hpkg ] ; @@ -73,11 +73,6 @@ HPKG_SOURCES = PackageReaderImplV1.cpp PackageReaderV1.cpp ReaderImplBaseV1.cpp - - # compression - ZlibCompressionBase.cpp - ZlibCompressor.cpp - ZlibDecompressor.cpp ; # TODO: remove this hack once gcc2 has -iquote implemented @@ -143,7 +138,7 @@ BuildPlatformSharedLibrary libpackage_build.so SolverRepository.cpp SolverResult.cpp : - libshared_build.a $(HOST_LIBBE) curl z $(HOST_LIBSTDC++) + libshared_build.a $(HOST_LIBBE) curl $(HOST_LIBSTDC++) ; HaikuSubInclude solver ; diff --git a/src/kits/Jamfile b/src/kits/Jamfile index 015e83a..7355df6 100644 --- a/src/kits/Jamfile +++ b/src/kits/Jamfile @@ -45,7 +45,9 @@ for architectureObject in [ MultiArchSubDirSetup ] { [ MultiArchDefaultGristFiles libicon.a ] [ MultiArchDefaultGristFiles libagg.a ] - $(TARGET_LIBSTDC++) [ BuildFeatureAttribute icu : libraries ] + $(TARGET_LIBSTDC++) + [ BuildFeatureAttribute icu : libraries ] + [ BuildFeatureAttribute zlib : library ] ; } } @@ -71,7 +73,9 @@ SharedLibrary libbe_test.so : [ MultiArchDefaultGristFiles libicon.a ] [ MultiArchDefaultGristFiles libagg.a ] - $(TARGET_LIBSTDC++) [ BuildFeatureAttribute icu : libraries ] + $(TARGET_LIBSTDC++) + [ BuildFeatureAttribute icu : libraries ] + [ BuildFeatureAttribute zlib : library ] ; SEARCH_SOURCE += [ FDirName $(SUBDIR) interface ] ; diff --git a/src/kits/package/Jamfile b/src/kits/package/Jamfile index 0a55b7c..b5d4fe9 100644 --- a/src/kits/package/Jamfile +++ b/src/kits/package/Jamfile @@ -5,6 +5,7 @@ UsePrivateHeaders kernel shared storage + support ; UsePrivateSystemHeaders ; @@ -54,18 +55,12 @@ HPKG_SOURCES = PackageReaderImplV1.cpp PackageReaderV1.cpp ReaderImplBaseV1.cpp - - # compression - ZlibCompressionBase.cpp - ZlibCompressor.cpp - ZlibDecompressor.cpp ; local architectureObject ; for architectureObject in [ MultiArchSubDirSetup ] { on $(architectureObject) { UseBuildFeatureHeaders curl ; - UseBuildFeatureHeaders zlib ; SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src kits package hpkg ] ; SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src kits package hpkg v1 ] ; @@ -76,8 +71,6 @@ for architectureObject in [ MultiArchSubDirSetup ] { : [ BuildFeatureAttribute curl : headers ] ; Includes [ FGristFiles InitTerminateLibPackage.cpp ] : [ BuildFeatureAttribute curl : headers ] ; - Includes [ FGristFiles $(HPKG_SOURCES) ] - : [ BuildFeatureAttribute zlib : headers ] ; SharedLibrary [ MultiArchDefaultGristFiles libpackage.so ] : @@ -139,7 +132,6 @@ for architectureObject in [ MultiArchSubDirSetup ] { [ MultiArchDefaultGristFiles libshared.a ] be [ BuildFeatureAttribute curl : library ] - [ BuildFeatureAttribute zlib : library ] $(TARGET_LIBSTDC++) ; } diff --git a/src/kits/package/hpkg/PackageDataReader.cpp b/src/kits/package/hpkg/PackageDataReader.cpp index af3ee05..d87993b 100644 --- a/src/kits/package/hpkg/PackageDataReader.cpp +++ b/src/kits/package/hpkg/PackageDataReader.cpp @@ -13,7 +13,6 @@ #include <package/hpkg/HPKGDefsPrivate.h> #include <package/hpkg/PackageData.h> -#include <package/hpkg/ZlibDecompressor.h> namespace BPackageKit { diff --git a/src/kits/package/hpkg/PackageFileHeapAccessorBase.cpp b/src/kits/package/hpkg/PackageFileHeapAccessorBase.cpp index 9c308d9..74374e7 100644 --- a/src/kits/package/hpkg/PackageFileHeapAccessorBase.cpp +++ b/src/kits/package/hpkg/PackageFileHeapAccessorBase.cpp @@ -18,7 +18,7 @@ #include <package/hpkg/ErrorOutput.h> #include <AutoDeleter.h> -#include <package/hpkg/ZlibDecompressor.h> +#include <ZlibDecompressor.h> namespace BPackageKit { @@ -213,7 +213,7 @@ PackageFileHeapAccessorBase::DecompressChunkData(void* compressedDataBuffer, size_t uncompressedSize) { size_t actualSize; - status_t error = ZlibDecompressor::DecompressSingleBuffer( + status_t error = BZlibDecompressor::DecompressSingleBuffer( compressedDataBuffer, compressedSize, uncompressedDataBuffer, uncompressedSize, actualSize); if (error != B_OK) { diff --git a/src/kits/package/hpkg/PackageFileHeapReader.cpp b/src/kits/package/hpkg/PackageFileHeapReader.cpp index e02313b..e4c30f7 100644 --- a/src/kits/package/hpkg/PackageFileHeapReader.cpp +++ b/src/kits/package/hpkg/PackageFileHeapReader.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2013, Ingo Weinhold, ingo_weinhold@xxxxxx. + * Copyright 2013-2014, Ingo Weinhold, ingo_weinhold@xxxxxx. * Distributed under the terms of the MIT License. */ @@ -16,10 +16,6 @@ #include <package/hpkg/PoolBuffer.h> -// minimum length of data we require before trying to zlib compress them -static const size_t kZlibCompressionSizeThreshold = 64; - - namespace BPackageKit { namespace BHPKG { diff --git a/src/kits/package/hpkg/PackageFileHeapWriter.cpp b/src/kits/package/hpkg/PackageFileHeapWriter.cpp index f112f2e..82627ca 100644 --- a/src/kits/package/hpkg/PackageFileHeapWriter.cpp +++ b/src/kits/package/hpkg/PackageFileHeapWriter.cpp @@ -19,8 +19,8 @@ #include <AutoDeleter.h> #include <package/hpkg/DataReader.h> #include <package/hpkg/PackageFileHeapReader.h> -#include <package/hpkg/ZlibCompressor.h> #include <RangeArray.h> +#include <ZlibCompressor.h> // minimum length of data we require before trying to zlib compress them @@ -551,7 +551,7 @@ PackageFileHeapWriter::_WriteDataCompressed(const void* data, size_t size) return B_BUFFER_OVERFLOW; size_t compressedSize; - status_t error = ZlibCompressor::CompressSingleBuffer(data, size, + status_t error = BZlibCompressor::CompressSingleBuffer(data, size, fCompressedDataBuffer, size, compressedSize, fCompressionLevel); if (error != B_OK) { if (error != B_BUFFER_OVERFLOW) { diff --git a/src/kits/package/hpkg/PackageReaderImpl.cpp b/src/kits/package/hpkg/PackageReaderImpl.cpp index d1337b3..06d4069 100644 --- a/src/kits/package/hpkg/PackageReaderImpl.cpp +++ b/src/kits/package/hpkg/PackageReaderImpl.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2009, Ingo Weinhold, ingo_weinhold@xxxxxx. + * Copyright 2009-2014, Ingo Weinhold, ingo_weinhold@xxxxxx. * Copyright 2011, Oliver Tappe <zooey@xxxxxxxxxxxxxxx> * Distributed under the terms of the MIT License. */ @@ -25,7 +25,6 @@ #include <package/hpkg/PackageData.h> #include <package/hpkg/PackageEntry.h> #include <package/hpkg/PackageEntryAttribute.h> -#include <package/hpkg/ZlibDecompressor.h> namespace BPackageKit { diff --git a/src/kits/package/hpkg/PackageWriterImpl.cpp b/src/kits/package/hpkg/PackageWriterImpl.cpp index 5d291dc..8f5cadd 100644 --- a/src/kits/package/hpkg/PackageWriterImpl.cpp +++ b/src/kits/package/hpkg/PackageWriterImpl.cpp @@ -58,10 +58,6 @@ namespace BHPKG { namespace BPrivate { -// minimum length of data we require before trying to zlib compress them -static const size_t kZlibCompressionSizeThreshold = 64; - - // #pragma mark - Attributes diff --git a/src/kits/package/hpkg/ReaderImplBase.cpp b/src/kits/package/hpkg/ReaderImplBase.cpp index 17302c0..57a4037 100644 --- a/src/kits/package/hpkg/ReaderImplBase.cpp +++ b/src/kits/package/hpkg/ReaderImplBase.cpp @@ -11,6 +11,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> #include <algorithm> #include <new> @@ -21,7 +22,7 @@ #include <package/hpkg/HPKGDefsPrivate.h> #include <package/hpkg/PackageFileHeapReader.h> -#include <package/hpkg/ZlibDecompressor.h> +#include <ZlibDecompressor.h> namespace BPackageKit { diff --git a/src/kits/package/hpkg/WriterImplBase.cpp b/src/kits/package/hpkg/WriterImplBase.cpp index 7df5416..ccc6335 100644 --- a/src/kits/package/hpkg/WriterImplBase.cpp +++ b/src/kits/package/hpkg/WriterImplBase.cpp @@ -10,6 +10,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> #include <algorithm> #include <new> diff --git a/src/kits/package/hpkg/ZlibCompressionBase.cpp b/src/kits/package/hpkg/ZlibCompressionBase.cpp deleted file mode 100644 index 4fe4337..0000000 --- a/src/kits/package/hpkg/ZlibCompressionBase.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/* - * Copyright 2009, Ingo Weinhold, ingo_weinhold@xxxxxx. - * Distributed under the terms of the MIT License. - */ - - -#include <package/hpkg/ZlibCompressionBase.h> - -#include <errno.h> - -#include <zlib.h> - - -namespace BPackageKit { - -namespace BHPKG { - -namespace BPrivate { - - -/*static*/ status_t -ZlibCompressionBase::TranslateZlibError(int error) -{ - switch (error) { - case Z_OK: - return B_OK; - case Z_STREAM_END: - case Z_NEED_DICT: - // a special event (no error), but the caller doesn't seem to handle - // it - return B_ERROR; - case Z_ERRNO: - return errno; - case Z_STREAM_ERROR: - return B_BAD_VALUE; - case Z_DATA_ERROR: - return B_BAD_DATA; - case Z_MEM_ERROR: - return B_NO_MEMORY; - case Z_BUF_ERROR: - return B_BUFFER_OVERFLOW; - case Z_VERSION_ERROR: - return B_BAD_VALUE; - default: - return B_ERROR; - } -} - - -} // namespace BPrivate - -} // namespace BHPKG - -} // namespace BPackageKit diff --git a/src/kits/package/hpkg/ZlibCompressor.cpp b/src/kits/package/hpkg/ZlibCompressor.cpp deleted file mode 100644 index deef959..0000000 --- a/src/kits/package/hpkg/ZlibCompressor.cpp +++ /dev/null @@ -1,186 +0,0 @@ -/* - * Copyright 2009-2014, Ingo Weinhold, ingo_weinhold@xxxxxx. - * Distributed under the terms of the MIT License. - */ - - -#include <package/hpkg/ZlibCompressor.h> - -#include <errno.h> -#include <stdio.h> - -#include <DataIO.h> - - -namespace BPackageKit { - -namespace BHPKG { - -namespace BPrivate { - - -static const size_t kOutputBufferSize = 1024; - - -ZlibCompressor::ZlibCompressor(BDataIO* output) - : - fOutput(output), - fStreamInitialized(false) -{ -} - - -ZlibCompressor::~ZlibCompressor() -{ - if (fStreamInitialized) - deflateEnd(&fStream); -} - - -status_t -ZlibCompressor::Init(int compressionLevel) -{ - // initialize the stream - fStream.next_in = NULL; - fStream.avail_in = 0; - fStream.total_in = 0; - fStream.next_out = NULL; - fStream.avail_out = 0; - fStream.total_out = 0; - fStream.msg = 0; - fStream.state = 0; - fStream.zalloc = Z_NULL; - fStream.zfree = Z_NULL; - fStream.opaque = Z_NULL; - fStream.data_type = 0; - fStream.adler = 0; - fStream.reserved = 0; - - int zlibError = deflateInit(&fStream, compressionLevel); - if (zlibError != Z_OK) - return TranslateZlibError(zlibError); - - fStreamInitialized = true; - - return B_OK; -} - - -status_t -ZlibCompressor::CompressNext(const void* input, size_t inputSize) -{ - fStream.next_in = (Bytef*)input; - fStream.avail_in = inputSize; - - while (fStream.avail_in > 0) { - uint8 outputBuffer[kOutputBufferSize]; - fStream.next_out = (Bytef*)outputBuffer; - fStream.avail_out = sizeof(outputBuffer); - - int zlibError = deflate(&fStream, 0); - if (zlibError != Z_OK) - return TranslateZlibError(zlibError); - - if (fStream.avail_out < sizeof(outputBuffer)) { - status_t error = fOutput->WriteExactly(outputBuffer, - sizeof(outputBuffer) - fStream.avail_out); - if (error != B_OK) - return error; - } - } - - return B_OK; -} - - -status_t -ZlibCompressor::Finish() -{ - fStream.next_in = (Bytef*)NULL; - fStream.avail_in = 0; - - while (true) { - uint8 outputBuffer[kOutputBufferSize]; - fStream.next_out = (Bytef*)outputBuffer; - fStream.avail_out = sizeof(outputBuffer); - - int zlibError = deflate(&fStream, Z_FINISH); - if (zlibError != Z_OK && zlibError != Z_STREAM_END) - return TranslateZlibError(zlibError); - - if (fStream.avail_out < sizeof(outputBuffer)) { - status_t error = fOutput->WriteExactly(outputBuffer, - sizeof(outputBuffer) - fStream.avail_out); - if (error != B_OK) - return error; - } - - if (zlibError == Z_STREAM_END) - break; - } - - deflateEnd(&fStream); - fStreamInitialized = false; - - return B_OK; -} - - -/*static*/ status_t -ZlibCompressor::CompressSingleBuffer(const void* input, size_t inputSize, - void* output, size_t outputSize, size_t& _compressedSize, - int compressionLevel) -{ - if (inputSize == 0 || outputSize == 0) - return B_BAD_VALUE; - - // prepare stream - z_stream zStream = { - (Bytef*)input, // next_in - uInt(inputSize), // avail_in - 0, // total_in - (Bytef*)output, // next_out - uInt(outputSize), // avail_out - 0, // total_out - 0, // msg - 0, // state; - Z_NULL, // zalloc - Z_NULL, // zfree - Z_NULL, // opaque - 0, // data_type - 0, // adler - 0 // reserved - }; - - int zlibError = deflateInit(&zStream, compressionLevel); - if (zlibError != Z_OK) - return TranslateZlibError(zlibError); - - // deflate - status_t error = B_OK; - zlibError = deflate(&zStream, Z_FINISH); - if (zlibError != Z_STREAM_END) { - if (zlibError == Z_OK) - error = B_BUFFER_OVERFLOW; - else - error = TranslateZlibError(zlibError); - } - - // clean up - zlibError = deflateEnd(&zStream); - if (zlibError != Z_OK && error == B_OK) - error = TranslateZlibError(zlibError); - - if (error != B_OK) - return error; - - _compressedSize = zStream.total_out; - return B_OK; -} - - -} // namespace BPrivate - -} // namespace BHPKG - -} // namespace BPackageKit diff --git a/src/kits/package/hpkg/ZlibDecompressor.cpp b/src/kits/package/hpkg/ZlibDecompressor.cpp deleted file mode 100644 index 0858dcad..0000000 --- a/src/kits/package/hpkg/ZlibDecompressor.cpp +++ /dev/null @@ -1,192 +0,0 @@ -/* - * Copyright 2009-2014, Ingo Weinhold, ingo_weinhold@xxxxxx. - * Distributed under the terms of the MIT License. - */ - - -#include <package/hpkg/ZlibDecompressor.h> - -#include <errno.h> -#include <stdio.h> - -#include <DataIO.h> - - -namespace BPackageKit { - -namespace BHPKG { - -namespace BPrivate { - - -// TODO: For the kernel the buffer shouldn't be allocated on the stack. -static const size_t kOutputBufferSize = 1024; - - -ZlibDecompressor::ZlibDecompressor(BDataIO* output) - : - fOutput(output), - fStreamInitialized(false), - fFinished(false) -{ -} - - -ZlibDecompressor::~ZlibDecompressor() -{ - if (fStreamInitialized) - inflateEnd(&fStream); -} - - -status_t -ZlibDecompressor::Init() -{ - // initialize the stream - fStream.next_in = NULL; - fStream.avail_in = 0; - fStream.total_in = 0; - fStream.next_out = NULL; - fStream.avail_out = 0; - fStream.total_out = 0; - fStream.msg = 0; - fStream.state = 0; - fStream.zalloc = Z_NULL; - fStream.zfree = Z_NULL; - fStream.opaque = Z_NULL; - fStream.data_type = 0; - fStream.adler = 0; - fStream.reserved = 0; - - int zlibError = inflateInit(&fStream); - if (zlibError != Z_OK) - return TranslateZlibError(zlibError); - - fStreamInitialized = true; - - return B_OK; -} - - -status_t -ZlibDecompressor::DecompressNext(const void* input, size_t inputSize) -{ - fStream.next_in = (Bytef*)input; - fStream.avail_in = inputSize; - - while (fStream.avail_in > 0) { - if (fFinished) - return B_BAD_DATA; - - uint8 outputBuffer[kOutputBufferSize]; - fStream.next_out = (Bytef*)outputBuffer; - fStream.avail_out = sizeof(outputBuffer); - - int zlibError = inflate(&fStream, 0); - if (zlibError == Z_STREAM_END) - fFinished = true; - else if (zlibError != Z_OK) - return TranslateZlibError(zlibError); - - if (fStream.avail_out < sizeof(outputBuffer)) { - status_t error = fOutput->WriteExactly(outputBuffer, - sizeof(outputBuffer) - fStream.avail_out); - if (error != B_OK) - return error; - } - } - - return B_OK; -} - - -status_t -ZlibDecompressor::Finish() -{ - fStream.next_in = (Bytef*)NULL; - fStream.avail_in = 0; - - while (!fFinished) { - uint8 outputBuffer[kOutputBufferSize]; - fStream.next_out = (Bytef*)outputBuffer; - fStream.avail_out = sizeof(outputBuffer); - - int zlibError = inflate(&fStream, Z_FINISH); - if (zlibError == Z_STREAM_END) - fFinished = true; - else if (zlibError != Z_OK) - return TranslateZlibError(zlibError); - - if (fStream.avail_out < sizeof(outputBuffer)) { - status_t error = fOutput->WriteExactly(outputBuffer, - sizeof(outputBuffer) - fStream.avail_out); - if (error != B_OK) - return error; - } - } - - inflateEnd(&fStream); - fStreamInitialized = false; - - return B_OK; -} - - -/*static*/ status_t -ZlibDecompressor::DecompressSingleBuffer(const void* input, size_t inputSize, - void* output, size_t outputSize, size_t& _uncompressedSize) -{ - if (inputSize == 0 || outputSize == 0) - return B_BAD_VALUE; - - // prepare stream - z_stream zStream = { - (Bytef*)input, // next_in - uInt(inputSize), // avail_in - 0, // total_in - (Bytef*)output, // next_out - uInt(outputSize), // avail_out - 0, // total_out - 0, // msg - 0, // state; - Z_NULL, // zalloc - Z_NULL, // zfree - Z_NULL, // opaque - 0, // data_type - 0, // adler - 0 // reserved - }; - - int zlibError = inflateInit(&zStream); - if (zlibError != Z_OK) - return TranslateZlibError(zlibError); - - - // inflate - status_t error = B_OK; - zlibError = inflate(&zStream, Z_FINISH); - if (zlibError != Z_STREAM_END) { - if (zlibError == Z_OK) - error = B_BUFFER_OVERFLOW; - else - error = TranslateZlibError(zlibError); - } - - // clean up - zlibError = inflateEnd(&zStream); - if (zlibError != Z_OK && error == B_OK) - error = TranslateZlibError(zlibError); - - if (error != B_OK) - return error; - - _uncompressedSize = zStream.total_out; - return B_OK; -} - - -} // namespace BPrivate - -} // namespace BHPKG - -} // namespace BPackageKit diff --git a/src/kits/package/hpkg/v1/PackageDataReaderV1.cpp b/src/kits/package/hpkg/v1/PackageDataReaderV1.cpp index 4b85a01..9d30c90 100644 --- a/src/kits/package/hpkg/v1/PackageDataReaderV1.cpp +++ b/src/kits/package/hpkg/v1/PackageDataReaderV1.cpp @@ -16,7 +16,7 @@ #include <package/hpkg/PoolBuffer.h> #include <package/hpkg/v1/HPKGDefsPrivate.h> #include <package/hpkg/v1/PackageData.h> -#include <package/hpkg/ZlibDecompressor.h> +#include <ZlibDecompressor.h> namespace BPackageKit { @@ -27,7 +27,6 @@ namespace V1 { using BHPKG::BPrivate::PoolBufferPutter; -using BHPKG::BPrivate::ZlibDecompressor; // minimum/maximum zlib chunk size we consider sane @@ -295,7 +294,7 @@ private: return error; size_t actuallyUncompressedSize; - error = ZlibDecompressor::DecompressSingleBuffer( + error = BZlibDecompressor::DecompressSingleBuffer( readBuffer->Buffer(), compressedSize, fUncompressBuffer->Buffer(), uncompressedSize, actuallyUncompressedSize); diff --git a/src/kits/package/hpkg/v1/PackageReaderImplV1.cpp b/src/kits/package/hpkg/v1/PackageReaderImplV1.cpp index 5178e3b..881d47a 100644 --- a/src/kits/package/hpkg/v1/PackageReaderImplV1.cpp +++ b/src/kits/package/hpkg/v1/PackageReaderImplV1.cpp @@ -26,7 +26,6 @@ #include <package/hpkg/v1/PackageData.h> #include <package/hpkg/v1/PackageEntry.h> #include <package/hpkg/v1/PackageEntryAttribute.h> -#include <package/hpkg/ZlibDecompressor.h> namespace BPackageKit { diff --git a/src/kits/package/hpkg/v1/ReaderImplBaseV1.cpp b/src/kits/package/hpkg/v1/ReaderImplBaseV1.cpp index d15657d..fb69b0d 100644 --- a/src/kits/package/hpkg/v1/ReaderImplBaseV1.cpp +++ b/src/kits/package/hpkg/v1/ReaderImplBaseV1.cpp @@ -11,6 +11,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <unistd.h> #include <algorithm> #include <new> @@ -21,7 +22,7 @@ #include <package/hpkg/v1/HPKGDefsPrivate.h> #include <package/hpkg/ErrorOutput.h> -#include <package/hpkg/ZlibDecompressor.h> +#include <ZlibDecompressor.h> namespace BPackageKit { @@ -33,9 +34,6 @@ namespace V1 { namespace BPrivate { -using BHPKG::BPrivate::ZlibDecompressor; - - static const size_t kScratchBufferSize = 64 * 1024; @@ -1032,7 +1030,7 @@ ReaderImplBase::ReadCompressedBuffer(const SectionInfo& section) { // init the decompressor BMemoryIO bufferOutput(section.data, section.uncompressedLength); - ZlibDecompressor decompressor(&bufferOutput); + BZlibDecompressor decompressor(&bufferOutput); status_t error = decompressor.Init(); if (error != B_OK) return error; diff --git a/src/kits/support/Jamfile b/src/kits/support/Jamfile index aed31d5..84c421f 100644 --- a/src/kits/support/Jamfile +++ b/src/kits/support/Jamfile @@ -9,6 +9,11 @@ for architectureObject in [ MultiArchSubDirSetup ] { on $(architectureObject) { local architecture = $(TARGET_PACKAGING_ARCH) ; + UseBuildFeatureHeaders zlib ; + + Includes [ FGristFiles ZlibCompressionBasePrivate.h ] + : [ BuildFeatureAttribute zlib : headers ] ; + MergeObject <libbe!$(architecture)>support_kit.o : Architecture.cpp Archivable.cpp @@ -31,6 +36,9 @@ for architectureObject in [ MultiArchSubDirSetup ] { StringList.cpp Url.cpp Uuid.cpp + ZlibCompressionBase.cpp + ZlibCompressor.cpp + ZlibDecompressor.cpp ; StaticLibrary [ MultiArchDefaultGristFiles libreferenceable.a ] diff --git a/src/kits/support/ZlibCompressionBase.cpp b/src/kits/support/ZlibCompressionBase.cpp new file mode 100644 index 0000000..4a3233b --- /dev/null +++ b/src/kits/support/ZlibCompressionBase.cpp @@ -0,0 +1,80 @@ +/* + * Copyright 2009-2014, Ingo Weinhold, ingo_weinhold@xxxxxx. + * Distributed under the terms of the MIT License. + */ + + +#include <ZlibCompressionBase.h> + +#include <errno.h> +#include <string.h> + +#include <new> + +#include "ZlibCompressionBasePrivate.h" + + +BZlibCompressionBase::BZlibCompressionBase(BDataIO* output) + : + fOutput(output), + fStream(NULL) +{ +} + + +BZlibCompressionBase::~BZlibCompressionBase() +{ + DeleteStream(); +} + + +/*static*/ status_t +BZlibCompressionBase::TranslateZlibError(int error) +{ + switch (error) { + case Z_OK: + return B_OK; + case Z_STREAM_END: + case Z_NEED_DICT: + // a special event (no error), but the caller doesn't seem to handle + // it + return B_ERROR; + case Z_ERRNO: + return errno; + case Z_STREAM_ERROR: + return B_BAD_VALUE; + case Z_DATA_ERROR: + return B_BAD_DATA; + case Z_MEM_ERROR: + return B_NO_MEMORY; + case Z_BUF_ERROR: + return B_BUFFER_OVERFLOW; + case Z_VERSION_ERROR: + return B_BAD_VALUE; + default: + return B_ERROR; + } +} + + +status_t +BZlibCompressionBase::CreateStream() +{ + if (fStream != NULL) + return B_BAD_VALUE; + + fStream = new(std::nothrow) ZStream; + if (fStream == NULL) + return B_NO_MEMORY; + + memset(fStream, 0, sizeof(*fStream)); + return B_OK; +} + + +void +BZlibCompressionBase::DeleteStream() +{ + delete fStream; + fStream = NULL; +} diff --git a/src/kits/support/ZlibCompressionBasePrivate.h b/src/kits/support/ZlibCompressionBasePrivate.h new file mode 100644 index 0000000..8ce7083 --- /dev/null +++ b/src/kits/support/ZlibCompressionBasePrivate.h @@ -0,0 +1,16 @@ +/* + * Copyright 2014, Ingo Weinhold, ingo_weinhold@xxxxxx. + * Distributed under the terms of the MIT License. + */ +#ifndef ZLIB_COMPRESSION_BASE_PRIVATE_H +#define ZLIB_COMPRESSION_BASE_PRIVATE_H + + +#include <zlib.h> + + +struct BZlibCompressionBase::ZStream : z_stream { +}; + + +#endif // ZLIB_COMPRESSION_BASE_PRIVATE_H diff --git a/src/kits/support/ZlibCompressor.cpp b/src/kits/support/ZlibCompressor.cpp new file mode 100644 index 0000000..a91cfc0 --- /dev/null +++ b/src/kits/support/ZlibCompressor.cpp @@ -0,0 +1,150 @@ +/* + * Copyright 2009-2014, Ingo Weinhold, ingo_weinhold@xxxxxx. + * Distributed under the terms of the MIT License. + */ + + +#include <ZlibCompressor.h> + +#include <string.h> + +#include <DataIO.h> + +#include "ZlibCompressionBasePrivate.h" + + +static const size_t kOutputBufferSize = 1024; + + +BZlibCompressor::BZlibCompressor(BDataIO* output) + : + BZlibCompressionBase(output) +{ +} + + +BZlibCompressor::~BZlibCompressor() +{ + if (fStream != NULL) + deflateEnd(fStream); +} + + +status_t +BZlibCompressor::Init(int compressionLevel) +{ + status_t error = CreateStream(); + if (error != B_OK) + return error; + + int zlibError = deflateInit(fStream, compressionLevel); + if (zlibError != Z_OK) { + DeleteStream(); + return TranslateZlibError(zlibError); + } + + return B_OK; +} + + +status_t +BZlibCompressor::CompressNext(const void* input, size_t inputSize) +{ + fStream->next_in = (Bytef*)input; + fStream->avail_in = inputSize; + + while (fStream->avail_in > 0) { + uint8 outputBuffer[kOutputBufferSize]; + fStream->next_out = (Bytef*)outputBuffer; + fStream->avail_out = sizeof(outputBuffer); + + int zlibError = deflate(fStream, 0); + if (zlibError != Z_OK) + return TranslateZlibError(zlibError); + + if (fStream->avail_out < sizeof(outputBuffer)) { + status_t error = fOutput->WriteExactly(outputBuffer, + sizeof(outputBuffer) - fStream->avail_out); + if (error != B_OK) + return error; + } + } + + return B_OK; +} + + +status_t +BZlibCompressor::Finish() +{ + fStream->next_in = (Bytef*)NULL; + fStream->avail_in = 0; + + while (true) { + uint8 outputBuffer[kOutputBufferSize]; + fStream->next_out = (Bytef*)outputBuffer; + fStream->avail_out = sizeof(outputBuffer); + + int zlibError = deflate(fStream, Z_FINISH); + if (zlibError != Z_OK && zlibError != Z_STREAM_END) + return TranslateZlibError(zlibError); + + if (fStream->avail_out < sizeof(outputBuffer)) { + status_t error = fOutput->WriteExactly(outputBuffer, + sizeof(outputBuffer) - fStream->avail_out); + if (error != B_OK) + return error; + } + + if (zlibError == Z_STREAM_END) + break; + } + + deflateEnd(fStream); + DeleteStream(); + + return B_OK; +} + + +/*static*/ status_t +BZlibCompressor::CompressSingleBuffer(const void* input, size_t inputSize, + void* output, size_t outputSize, size_t& _compressedSize, + int compressionLevel) +{ + if (inputSize == 0 || outputSize == 0) + return B_BAD_VALUE; + + // prepare stream + z_stream zStream; + memset(&zStream, 0, sizeof(zStream)); + zStream.next_in = (Bytef*)input; + zStream.avail_in = uInt(inputSize); + zStream.next_out = (Bytef*)output; + zStream.avail_out = uInt(outputSize); + + int zlibError = deflateInit(&zStream, compressionLevel); + if (zlibError != Z_OK) + return TranslateZlibError(zlibError); + + // deflate + status_t error = B_OK; + zlibError = deflate(&zStream, Z_FINISH); + if (zlibError != Z_STREAM_END) { + if (zlibError == Z_OK) + error = B_BUFFER_OVERFLOW; + else + error = TranslateZlibError(zlibError); + } + + // clean up + zlibError = deflateEnd(&zStream); + if (zlibError != Z_OK && error == B_OK) + error = TranslateZlibError(zlibError); + + if (error != B_OK) + return error; + + _compressedSize = zStream.total_out; + return B_OK; +} diff --git a/src/kits/support/ZlibDecompressor.cpp b/src/kits/support/ZlibDecompressor.cpp new file mode 100644 index 0000000..61a0f27 --- /dev/null +++ b/src/kits/support/ZlibDecompressor.cpp @@ -0,0 +1,155 @@ +/* + * Copyright 2009-2014, Ingo Weinhold, ingo_weinhold@xxxxxx. + * Distributed under the terms of the MIT License. + */ + + +#include <ZlibDecompressor.h> + +#include <string.h> + +#include <DataIO.h> + +#include "ZlibCompressionBasePrivate.h" + + +// TODO: For the kernel the buffer shouldn't be allocated on the stack. +static const size_t kOutputBufferSize = 1024; + + +BZlibDecompressor::BZlibDecompressor(BDataIO* output) + : + BZlibCompressionBase(output), + fFinished(false) +{ +} + + +BZlibDecompressor::~BZlibDecompressor() +{ + if (fStream != NULL) + inflateEnd(fStream); +} + + +status_t +BZlibDecompressor::Init() +{ + status_t error = CreateStream(); + if (error != B_OK) + return error; + + int zlibError = inflateInit(fStream); + if (zlibError != Z_OK) { + DeleteStream(); + return TranslateZlibError(zlibError); + } + + return B_OK; +} + + +status_t +BZlibDecompressor::DecompressNext(const void* input, size_t inputSize) +{ + fStream->next_in = (Bytef*)input; + fStream->avail_in = inputSize; + + while (fStream->avail_in > 0) { + if (fFinished) + return B_BAD_DATA; + + uint8 outputBuffer[kOutputBufferSize]; + fStream->next_out = (Bytef*)outputBuffer; + fStream->avail_out = sizeof(outputBuffer); + + int zlibError = inflate(fStream, 0); + if (zlibError == Z_STREAM_END) + fFinished = true; + else if (zlibError != Z_OK) + return TranslateZlibError(zlibError); + + if (fStream->avail_out < sizeof(outputBuffer)) { + status_t error = fOutput->WriteExactly(outputBuffer, + sizeof(outputBuffer) - fStream->avail_out); + if (error != B_OK) + return error; + } + } + + return B_OK; +} + + +status_t +BZlibDecompressor::Finish() +{ + fStream->next_in = (Bytef*)NULL; + fStream->avail_in = 0; + + while (!fFinished) { + uint8 outputBuffer[kOutputBufferSize]; + fStream->next_out = (Bytef*)outputBuffer; + fStream->avail_out = sizeof(outputBuffer); + + int zlibError = inflate(fStream, Z_FINISH); + if (zlibError == Z_STREAM_END) + fFinished = true; + else if (zlibError != Z_OK) + return TranslateZlibError(zlibError); + + if (fStream->avail_out < sizeof(outputBuffer)) { + status_t error = fOutput->WriteExactly(outputBuffer, + sizeof(outputBuffer) - fStream->avail_out); + if (error != B_OK) + return error; + } + } + + inflateEnd(fStream); + DeleteStream(); + + return B_OK; +} + + +/*static*/ status_t +BZlibDecompressor::DecompressSingleBuffer(const void* input, size_t inputSize, + void* output, size_t outputSize, size_t& _uncompressedSize) +{ + if (inputSize == 0 || outputSize == 0) + return B_BAD_VALUE; + + // prepare stream + z_stream zStream; + memset(&zStream, 0, sizeof(zStream)); + zStream.next_in = (Bytef*)input; + zStream.avail_in = uInt(inputSize); + zStream.next_out = (Bytef*)output; + zStream.avail_out = uInt(outputSize); + + int zlibError = inflateInit(&zStream); + if (zlibError != Z_OK) + return TranslateZlibError(zlibError); + + // inflate + status_t error = B_OK; + zlibError = inflate(&zStream, Z_FINISH); + if (zlibError != Z_STREAM_END) { + if (zlibError == Z_OK) + error = B_BUFFER_OVERFLOW; + else + error = TranslateZlibError(zlibError); + } + + // clean up + zlibError = inflateEnd(&zStream); + if (zlibError != Z_OK && error == B_OK) + error = TranslateZlibError(zlibError); + + if (error != B_OK) + return error; + + _uncompressedSize = zStream.total_out; + return B_OK; +} diff --git a/src/system/boot/loader/file_systems/packagefs/Jamfile b/src/system/boot/loader/file_systems/packagefs/Jamfile index 6929d56..edd4e0f 100644 --- a/src/system/boot/loader/file_systems/packagefs/Jamfile +++ b/src/system/boot/loader/file_systems/packagefs/Jamfile @@ -1,7 +1,7 @@ SubDir HAIKU_TOP src system boot loader file_systems packagefs ; UsePrivateHeaders [ FDirName kernel boot platform $(TARGET_BOOT_PLATFORM) ] ; -UsePrivateHeaders kernel shared ; +UsePrivateHeaders kernel shared support ; UseBuildFeatureHeaders zlib ; DEFINES += _BOOT_MODE ; @@ -13,6 +13,7 @@ SubDirC++Flags -fno-rtti -include $(kernelC++Header) ; SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src kits package ] ; SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src kits package hpkg ] ; +SEARCH_SOURCE += [ FDirName $(HAIKU_TOP) src kits support ] ; BootStaticLibrary boot_packagefs : @@ -40,16 +41,14 @@ BootStaticLibrary boot_packagefs : PackageReaderImpl.cpp ReaderImplBase.cpp - # compression + # support kit + ZlibCompressionBase.cpp ZlibDecompressor.cpp : -fno-pic ; -Includes [ - FGristFiles ZlibCompressionBase.cpp package/hpkg/ZlibCompressor.h - package/hpkg/ZlibDecompressor.h - ] +Includes [ FGristFiles ZlibCompressionBasePrivate.h ] : [ BuildFeatureAttribute zlib : headers ] ; ############################################################################ Commit: dcdc33b0afd3a7731c40df8a3c8ed496fb25b1ac URL: http://cgit.haiku-os.org/haiku/commit/?id=dcdc33b Author: Ingo Weinhold <ingo_weinhold@xxxxxx> Date: Sun Jun 29 16:21:44 2014 UTC Add B[Zlib]CompressionAlgorithm * BCompressionAlgorithm is a base class for classes that provide compression/decompression functionality. There are methods for compressing/decompressing a single buffer and factory methods for a compressing/decompressing input/output BDataIO. * BZlibCompressionAlgorithm is a BCompressionAlgorithm implementation using zlib. ---------------------------------------------------------------------------- diff --git a/headers/build/private/support/CompressionAlgorithm.h b/headers/build/private/support/CompressionAlgorithm.h new file mode 100644 index 0000000..d219a23 --- /dev/null +++ b/headers/build/private/support/CompressionAlgorithm.h @@ -0,0 +1 @@ +#include <../private/support/CompressionAlgorithm.h> diff --git a/headers/build/private/support/ZlibCompressionAlgorithm.h b/headers/build/private/support/ZlibCompressionAlgorithm.h new file mode 100644 index 0000000..f62554e --- /dev/null +++ b/headers/build/private/support/ZlibCompressionAlgorithm.h @@ -0,0 +1 @@ +#include <../private/support/ZlibCompressionAlgorithm.h> diff --git a/headers/private/support/CompressionAlgorithm.h b/headers/private/support/CompressionAlgorithm.h new file mode 100644 index 0000000..f2ca69c --- /dev/null +++ b/headers/private/support/CompressionAlgorithm.h @@ -0,0 +1,121 @@ +/* + * Copyright 2014, Ingo Weinhold, ingo_weinhold@xxxxxx. + * Distributed under the terms of the MIT License. + */ +#ifndef _COMPRESSION_ALGORITHM_H_ +#define _COMPRESSION_ALGORITHM_H_ + + +#include <DataIO.h> + + +class BCompressionParameters { +public: + BCompressionParameters(); + virtual ~BCompressionParameters(); +}; + + +class BDecompressionParameters { +public: + BDecompressionParameters(); + virtual ~BDecompressionParameters(); +}; + + +class BCompressionAlgorithm { +public: + BCompressionAlgorithm(); + virtual ~BCompressionAlgorithm(); + + virtual status_t CreateCompressingInputStream(BDataIO* input, + const BCompressionParameters* parameters, + BDataIO*& _stream); + virtual status_t CreateCompressingOutputStream(BDataIO* output, + const BCompressionParameters* parameters, + BDataIO*& _stream); + virtual status_t CreateDecompressingInputStream(BDataIO* input, + const BDecompressionParameters* parameters, + BDataIO*& _stream); + virtual status_t CreateDecompressingOutputStream(BDataIO* output, + const BDecompressionParameters* parameters, + BDataIO*& _stream); + + virtual status_t CompressBuffer(const void* input, + size_t inputSize, void* output, + size_t outputSize, size_t& _compressedSize, + const BCompressionParameters* parameters + = NULL); + virtual status_t DecompressBuffer(const void* input, + size_t inputSize, void* output, + size_t outputSize, + size_t& _uncompressedSize, + const BDecompressionParameters* parameters + = NULL); + +protected: + class BAbstractStream; + class BAbstractInputStream; + class BAbstractOutputStream; +}; + + +class BCompressionAlgorithm::BAbstractStream : public BDataIO { +public: + BAbstractStream(); + virtual ~BAbstractStream(); + + status_t Init(size_t bufferSize); + +protected: + virtual status_t ProcessData(const void* input, size_t inputSize, + void* output, size_t outputSize, + size_t& bytesConsumed, + size_t& bytesProduced) = 0; + // must consume or produce at least 1 byte + // or return an error + virtual status_t FlushPendingData(void* output, + size_t outputSize, + size_t& bytesProduced) = 0; + +protected: + uint8* fBuffer; + size_t fBufferCapacity; + size_t fBufferOffset; + size_t fBufferSize; +}; + + +class BCompressionAlgorithm::BAbstractInputStream : public BAbstractStream { +public: + BAbstractInputStream(BDataIO* input); + virtual ~BAbstractInputStream(); + + virtual ssize_t Read(void* buffer, size_t size); + +private: + BDataIO* fInput; + bool fEndOfInput; + bool fNoMorePendingData; +}; + + +class BCompressionAlgorithm::BAbstractOutputStream : public BAbstractStream { +public: + BAbstractOutputStream(BDataIO* output); + virtual ~BAbstractOutputStream(); + + virtual ssize_t Write(const void* buffer, size_t size); + + virtual status_t Flush(); + +private: + ssize_t _Write(const void* buffer, size_t size, + bool flush); + +private: + BDataIO* fOutput; +}; + + +#endif // _COMPRESSION_ALGORITHM_H_ diff --git a/headers/private/support/ZlibCompressionAlgorithm.h b/headers/private/support/ZlibCompressionAlgorithm.h new file mode 100644 index 0000000..6e8099d --- /dev/null +++ b/headers/private/support/ZlibCompressionAlgorithm.h @@ -0,0 +1,96 @@ +/* + * Copyright 2014, Ingo Weinhold, ingo_weinhold@xxxxxx. + * Distributed under the terms of the MIT License. + */ +#ifndef _ZLIB_COMPRESSION_ALGORITHM_H_ +#define _ZLIB_COMPRESSION_ALGORITHM_H_ + + +#include <CompressionAlgorithm.h> + + +// compression level +enum { + B_ZLIB_COMPRESSION_NONE = 0, + B_ZLIB_COMPRESSION_FASTEST = 1, + B_ZLIB_COMPRESSION_BEST = 9, + B_ZLIB_COMPRESSION_DEFAULT = -1, +}; + + +class BZlibCompressionParameters : public BCompressionParameters { +public: + BZlibCompressionParameters( + int compressionLevel + = B_ZLIB_COMPRESSION_DEFAULT); + virtual ~BZlibCompressionParameters(); + + int32 CompressionLevel() const; + void SetCompressionLevel(int32 level); + + size_t BufferSize() const; + void SetBufferSize(size_t size); + +private: + int32 fCompressionLevel; + size_t fBufferSize; +}; + + +class BZlibDecompressionParameters : public BDecompressionParameters { +public: + BZlibDecompressionParameters(); + virtual ~BZlibDecompressionParameters(); + + size_t BufferSize() const; + void SetBufferSize(size_t size); + +private: + size_t fBufferSize; +}; + + +class BZlibCompressionAlgorithm : public BCompressionAlgorithm { +public: + BZlibCompressionAlgorithm(); + virtual ~BZlibCompressionAlgorithm(); + + virtual status_t CreateCompressingInputStream(BDataIO* input, + const BCompressionParameters* parameters, + BDataIO*& _stream); + virtual status_t CreateCompressingOutputStream(BDataIO* output, + const BCompressionParameters* parameters, + BDataIO*& _stream); + virtual status_t CreateDecompressingInputStream(BDataIO* input, + const BDecompressionParameters* parameters, + BDataIO*& _stream); + virtual status_t CreateDecompressingOutputStream(BDataIO* output, + const BDecompressionParameters* parameters, + BDataIO*& _stream); + + virtual status_t CompressBuffer(const void* input, + size_t inputSize, void* output, + size_t outputSize, size_t& _compressedSize, + const BCompressionParameters* parameters + = NULL); + virtual status_t DecompressBuffer(const void* input, + size_t inputSize, void* output, + size_t outputSize, + size_t& _uncompressedSize, + const BDecompressionParameters* parameters + = NULL); + +private: + struct CompressionStrategy; + struct DecompressionStrategy; + + template<typename BaseClass, typename Strategy> struct Stream; + template<typename BaseClass, typename Strategy> + friend struct Stream; + +private: + static status_t _TranslateZlibError(int error); +}; + + +#endif // _ZLIB_COMPRESSION_ALGORITHM_H_ diff --git a/src/build/libbe/support/Jamfile b/src/build/libbe/support/Jamfile index 9ec8591..0833b06 100644 --- a/src/build/libbe/support/Jamfile +++ b/src/build/libbe/support/Jamfile @@ -10,6 +10,7 @@ BuildPlatformMergeObjectPIC <libbe_build>support_kit.o : Archivable.cpp BlockCache.cpp ByteOrder.cpp + CompressionAlgorithm.cpp DataIO.cpp Flattenable.cpp List.cpp @@ -18,6 +19,7 @@ BuildPlatformMergeObjectPIC <libbe_build>support_kit.o : Referenceable.cpp String.cpp StringList.cpp + ZlibCompressionAlgorithm.cpp ZlibCompressionBase.cpp ZlibCompressor.cpp ZlibDecompressor.cpp diff --git a/src/kits/support/CompressionAlgorithm.cpp b/src/kits/support/CompressionAlgorithm.cpp new file mode 100644 index 0000000..ab7eb2c --- /dev/null +++ b/src/kits/support/CompressionAlgorithm.cpp @@ -0,0 +1,328 @@ +/* + * Copyright 2014, Ingo Weinhold, ingo_weinhold@xxxxxx. + * Distributed under the terms of the MIT License. + */ + + +#include <CompressionAlgorithm.h> + +#include <stdlib.h> +#include <string.h> + +#include <Errors.h> + + +// #pragma mark - BCompressionParameters + + +BCompressionParameters::BCompressionParameters() +{ +} + + +BCompressionParameters::~BCompressionParameters() +{ +} + + +// #pragma mark - BDecompressionParameters + + +BDecompressionParameters::BDecompressionParameters() +{ +} + + +BDecompressionParameters::~BDecompressionParameters() +{ +} + + +// #pragma mark - BCompressionAlgorithm + + +BCompressionAlgorithm::BCompressionAlgorithm() +{ +} + + +BCompressionAlgorithm::~BCompressionAlgorithm() +{ +} + + +status_t +BCompressionAlgorithm::CreateCompressingInputStream(BDataIO* input, + const BCompressionParameters* parameters, BDataIO*& _stream) +{ + return B_NOT_SUPPORTED; +} + + +status_t +BCompressionAlgorithm::CreateCompressingOutputStream(BDataIO* output, + const BCompressionParameters* parameters, BDataIO*& _stream) +{ + return B_NOT_SUPPORTED; +} + + +status_t +BCompressionAlgorithm::CreateDecompressingInputStream(BDataIO* input, + const BDecompressionParameters* parameters, BDataIO*& _stream) +{ + return B_NOT_SUPPORTED; +} + + +status_t +BCompressionAlgorithm::CreateDecompressingOutputStream(BDataIO* output, + const BDecompressionParameters* parameters, BDataIO*& _stream) +{ + return B_NOT_SUPPORTED; +} + + +status_t +BCompressionAlgorithm::CompressBuffer(const void* input, size_t inputSize, + void* output, size_t outputSize, size_t& _compressedSize, + const BCompressionParameters* parameters) +{ + return B_NOT_SUPPORTED; +} + + +status_t +BCompressionAlgorithm::DecompressBuffer(const void* input, + size_t inputSize, void* output, size_t outputSize, + size_t& _uncompressedSize, const BDecompressionParameters* parameters) +{ + return B_NOT_SUPPORTED; +} + + +// #pragma mark - BAbstractStream + + +BCompressionAlgorithm::BAbstractStream::BAbstractStream() + : + BDataIO(), + fBuffer(NULL), + fBufferCapacity(0), + fBufferOffset(0), + fBufferSize(0) +{ +} + + +BCompressionAlgorithm::BAbstractStream::~BAbstractStream() +{ + free(fBuffer); +} + + +status_t +BCompressionAlgorithm::BAbstractStream::Init(size_t bufferSize) +{ + fBuffer = (uint8*)malloc(bufferSize); + fBufferCapacity = bufferSize; + + return fBuffer != NULL ? B_OK : B_NO_MEMORY; +} + + +// #pragma mark - BAbstractInputStream + + +BCompressionAlgorithm::BAbstractInputStream::BAbstractInputStream( + BDataIO* input) + : + BAbstractStream(), + fInput(input), + fEndOfInput(false), + fNoMorePendingData(false) +{ +} + + +BCompressionAlgorithm::BAbstractInputStream::~BAbstractInputStream() +{ +} + + +ssize_t +BCompressionAlgorithm::BAbstractInputStream::Read(void* buffer, size_t size) +{ + if (size == 0) + return 0; + + size_t bytesRemaining = size; + uint8* output = (uint8*)buffer; + + while (bytesRemaining > 0) { + // process the data still in the input buffer + if (fBufferSize > 0) { + size_t bytesConsumed; + size_t bytesProduced; + status_t error = ProcessData(fBuffer + fBufferOffset, fBufferSize, + output, bytesRemaining, bytesConsumed, bytesProduced); + if (error != B_OK) + return error; + + fBufferOffset += bytesConsumed; + fBufferSize -= bytesConsumed; + output += bytesProduced; + bytesRemaining -= bytesProduced; + continue; + } + + // We couldn't process anything, because we don't have any or not enough + // bytes in the input buffer. + + if (fEndOfInput) + break; + + // Move any remaining data to the start of the buffer. + if (fBufferSize > 0) { + if (fBufferSize == fBufferCapacity) + return B_ERROR; + + if (fBufferOffset > 0) + memmove(fBuffer, fBuffer + fBufferOffset, fBufferSize); + } + + fBufferOffset = 0; + + // read from the source + ssize_t bytesRead = fInput->Read(fBuffer + fBufferSize, + fBufferCapacity - fBufferSize); + if (bytesRead < 0) + return bytesRead; + if (bytesRead == 0) { + fEndOfInput = true; + break; + } + + fBufferSize += bytesRead; + } + + // If we've reached the end of the input and still have room in the output + // buffer, we have consumed all input data and want to flush all pending + // data, now. + if (fEndOfInput && bytesRemaining > 0 && !fNoMorePendingData) { + size_t bytesProduced; + status_t error = FlushPendingData(output, bytesRemaining, + bytesProduced); + if (error != B_OK) + return error; + + if (bytesProduced < bytesRemaining) + fNoMorePendingData = true; + + output += bytesProduced; + bytesRemaining -= bytesProduced; + } + + return size - bytesRemaining; +} + + +// #pragma mark - BAbstractOutputStream + + +BCompressionAlgorithm::BAbstractOutputStream::BAbstractOutputStream( + BDataIO* output) + : + BAbstractStream(), + fOutput(output) +{ +} + + +BCompressionAlgorithm::BAbstractOutputStream::~BAbstractOutputStream() +{ +} + + +ssize_t +BCompressionAlgorithm::BAbstractOutputStream::Write(const void* buffer, + size_t size) +{ + if (size == 0) + return 0; + + size_t bytesRemaining = size; + uint8* input = (uint8*)buffer; + + while (bytesRemaining > 0) { + // try to process more data + if (fBufferSize < fBufferCapacity) { + size_t bytesConsumed; + size_t bytesProduced; + status_t error = ProcessData(input, bytesRemaining, + fBuffer + fBufferSize, fBufferCapacity - fBufferSize, + bytesConsumed, bytesProduced); + if (error != B_OK) + return error; + + input += bytesConsumed; + bytesRemaining -= bytesConsumed; + fBufferSize += bytesProduced; + continue; + } + + // We couldn't process anything, because we don't have any or not enough + // room in the output buffer. + + if (fBufferSize == 0) + return B_ERROR; + + // write to the target + ssize_t bytesWritten = fOutput->Write(fBuffer, fBufferSize); + if (bytesWritten < 0) + return bytesWritten; + if (bytesWritten == 0) + break; + + // Move any remaining data to the start of the buffer. + fBufferSize -= bytesWritten; + if (fBufferSize > 0) + memmove(fBuffer, fBuffer + bytesWritten, fBufferSize); + } + + return size - bytesRemaining; +} + + +status_t +BCompressionAlgorithm::BAbstractOutputStream::Flush() +{ + bool noMorePendingData = false; + + for (;;) { [ *** diff truncated: 459 lines dropped *** ] ############################################################################ Commit: 377ecc1e5571d7a52b2e6d789bf70c585faf63f1 URL: http://cgit.haiku-os.org/haiku/commit/?id=377ecc1 Author: Ingo Weinhold <ingo_weinhold@xxxxxx> Date: Sun Jun 29 16:22:48 2014 UTC Test program for the B[Zlib]CompressionAlgorithm functionality ---------------------------------------------------------------------------- ############################################################################ Revision: hrev47449 Commit: b3263ad3e1662cc419849a2d2cf539f9636613dc URL: http://cgit.haiku-os.org/haiku/commit/?id=b3263ad Author: Ingo Weinhold <ingo_weinhold@xxxxxx> Date: Mon Jun 30 19:35:03 2014 UTC Switch package kit to BZlibCompressionAlgorithm ... and remove the Zlib{Compressor,Decompressor} API. ----------------------------------------------------------------------------