Switch package kit to BZlibCompressionAlgorithm
... and remove the Zlib{Compressor,Decompressor} API.
This commit is contained in:
@@ -1 +0,0 @@
|
||||
#include <../private/support/ZlibCompressionBase.h>
|
||||
@@ -1 +0,0 @@
|
||||
#include <../private/support/ZlibCompressor.h>
|
||||
@@ -1 +0,0 @@
|
||||
#include <../private/support/ZlibDecompressor.h>
|
||||
@@ -6,8 +6,11 @@
|
||||
#define _PACKAGE__HPKG__PRIVATE__PACKAGE_FILE_HEAP_ACCESSOR_BASE_H_
|
||||
|
||||
|
||||
#include <SupportDefs.h>
|
||||
#include <new>
|
||||
|
||||
#include <Referenceable.h>
|
||||
|
||||
#include <CompressionAlgorithm.h>
|
||||
#include <package/hpkg/DataReader.h>
|
||||
|
||||
|
||||
@@ -22,6 +25,46 @@ class BErrorOutput;
|
||||
namespace BPrivate {
|
||||
|
||||
|
||||
template<typename Parameters>
|
||||
struct GenericCompressionAlgorithmOwner : BReferenceable {
|
||||
BCompressionAlgorithm* algorithm;
|
||||
Parameters* parameters;
|
||||
|
||||
GenericCompressionAlgorithmOwner(BCompressionAlgorithm* algorithm,
|
||||
Parameters* parameters)
|
||||
:
|
||||
algorithm(algorithm),
|
||||
parameters(parameters)
|
||||
{
|
||||
}
|
||||
|
||||
~GenericCompressionAlgorithmOwner()
|
||||
{
|
||||
delete algorithm;
|
||||
delete parameters;
|
||||
}
|
||||
|
||||
static GenericCompressionAlgorithmOwner* Create(
|
||||
BCompressionAlgorithm* algorithm, Parameters* parameters)
|
||||
{
|
||||
GenericCompressionAlgorithmOwner* owner
|
||||
= new(std::nothrow) GenericCompressionAlgorithmOwner(algorithm,
|
||||
parameters);
|
||||
if (owner == NULL) {
|
||||
delete algorithm;
|
||||
delete parameters;
|
||||
}
|
||||
|
||||
return owner;
|
||||
}
|
||||
};
|
||||
|
||||
typedef GenericCompressionAlgorithmOwner<BCompressionParameters>
|
||||
CompressionAlgorithmOwner;
|
||||
typedef GenericCompressionAlgorithmOwner<BDecompressionParameters>
|
||||
DecompressionAlgorithmOwner;
|
||||
|
||||
|
||||
class PackageFileHeapAccessorBase : public BAbstractBufferedDataReader {
|
||||
public:
|
||||
class OffsetArray;
|
||||
@@ -29,7 +72,9 @@ public:
|
||||
public:
|
||||
PackageFileHeapAccessorBase(
|
||||
BErrorOutput* errorOutput, int fd,
|
||||
off_t heapOffset);
|
||||
off_t heapOffset,
|
||||
DecompressionAlgorithmOwner*
|
||||
decompressionAlgorithm);
|
||||
virtual ~PackageFileHeapAccessorBase();
|
||||
|
||||
off_t HeapOffset() const
|
||||
@@ -77,6 +122,7 @@ protected:
|
||||
off_t fHeapOffset;
|
||||
uint64 fCompressedHeapSize;
|
||||
uint64 fUncompressedHeapSize;
|
||||
DecompressionAlgorithmOwner* fDecompressionAlgorithm;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -27,7 +27,9 @@ public:
|
||||
PackageFileHeapReader(BErrorOutput* errorOutput,
|
||||
int fd, off_t heapOffset,
|
||||
off_t compressedHeapSize,
|
||||
uint64 uncompressedHeapSize);
|
||||
uint64 uncompressedHeapSize,
|
||||
DecompressionAlgorithmOwner*
|
||||
decompressionAlgorithm);
|
||||
~PackageFileHeapReader();
|
||||
|
||||
status_t Init();
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
#include <package/hpkg/PackageFileHeapAccessorBase.h>
|
||||
|
||||
|
||||
class BCompressionParameters;
|
||||
|
||||
namespace BPrivate {
|
||||
template<typename Value> class RangeArray;
|
||||
}
|
||||
@@ -34,7 +36,10 @@ class PackageFileHeapWriter : public PackageFileHeapAccessorBase {
|
||||
public:
|
||||
PackageFileHeapWriter(BErrorOutput* errorOutput,
|
||||
int fd, off_t heapOffset,
|
||||
int32 compressionLevel);
|
||||
CompressionAlgorithmOwner*
|
||||
compressionAlgorithm,
|
||||
DecompressionAlgorithmOwner*
|
||||
decompressionAlgorithm);
|
||||
~PackageFileHeapWriter();
|
||||
|
||||
void Init();
|
||||
@@ -81,7 +86,7 @@ private:
|
||||
void* fCompressedDataBuffer;
|
||||
size_t fPendingDataSize;
|
||||
Array<uint64> fOffsets;
|
||||
int32 fCompressionLevel;
|
||||
CompressionAlgorithmOwner* fCompressionAlgorithm;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -22,6 +22,10 @@
|
||||
#include <package/hpkg/PackageInfoAttributeValue.h>
|
||||
|
||||
|
||||
class BCompressionAlgorithm;
|
||||
class BDecompressionParameters;
|
||||
|
||||
|
||||
namespace BPackageKit {
|
||||
|
||||
namespace BHPKG {
|
||||
|
||||
@@ -10,12 +10,18 @@
|
||||
|
||||
#include <package/hpkg/HPKGDefsPrivate.h>
|
||||
|
||||
#include <package/hpkg/PackageFileHeapWriter.h>
|
||||
#include <package/hpkg/PackageWriter.h>
|
||||
#include <package/hpkg/Strings.h>
|
||||
|
||||
#include <package/PackageInfo.h>
|
||||
|
||||
#include <package/hpkg/PackageFileHeapWriter.h>
|
||||
|
||||
|
||||
class BCompressionAlgorithm;
|
||||
class BCompressionParameters;
|
||||
class BDecompressionParameters;
|
||||
|
||||
|
||||
namespace BPackageKit {
|
||||
|
||||
@@ -156,6 +162,10 @@ protected:
|
||||
|
||||
protected:
|
||||
PackageFileHeapWriter* fHeapWriter;
|
||||
BCompressionAlgorithm* fCompressionAlgorithm;
|
||||
BCompressionParameters* fCompressionParameters;
|
||||
BCompressionAlgorithm* fDecompressionAlgorithm;
|
||||
BDecompressionParameters* fDecompressionParameters;
|
||||
|
||||
private:
|
||||
static const BHPKGAttributeID kDefaultVersionAttributeID
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
/*
|
||||
* Copyright 2009-2014, Ingo Weinhold, [email protected].
|
||||
* 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_
|
||||
@@ -1,43 +0,0 @@
|
||||
/*
|
||||
* Copyright 2009-2014, Ingo Weinhold, [email protected].
|
||||
* 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_
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* Copyright 2009-2014, Ingo Weinhold, [email protected].
|
||||
* 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_
|
||||
@@ -103,7 +103,7 @@ HAIKU_PACKAGE_FS_PACKAGE_READER_SOURCES_V1 =
|
||||
ReaderImplBaseV1.cpp
|
||||
;
|
||||
|
||||
Includes [ FGristFiles ZlibCompressionBasePrivate.h ]
|
||||
Includes [ FGristFiles ZlibCompressionAlgorithm.cpp ]
|
||||
: [ BuildFeatureAttribute zlib : headers ] ;
|
||||
|
||||
local libSharedSources =
|
||||
@@ -111,8 +111,8 @@ local libSharedSources =
|
||||
;
|
||||
|
||||
local supportKitSources =
|
||||
ZlibCompressionBase.cpp
|
||||
ZlibDecompressor.cpp
|
||||
CompressionAlgorithm.cpp
|
||||
ZlibCompressionAlgorithm.cpp
|
||||
;
|
||||
|
||||
KernelAddon packagefs
|
||||
|
||||
@@ -20,7 +20,4 @@ BuildPlatformMergeObjectPIC <libbe_build>support_kit.o :
|
||||
String.cpp
|
||||
StringList.cpp
|
||||
ZlibCompressionAlgorithm.cpp
|
||||
ZlibCompressionBase.cpp
|
||||
ZlibCompressor.cpp
|
||||
ZlibDecompressor.cpp
|
||||
;
|
||||
|
||||
@@ -18,7 +18,7 @@
|
||||
#include <package/hpkg/ErrorOutput.h>
|
||||
|
||||
#include <AutoDeleter.h>
|
||||
#include <ZlibDecompressor.h>
|
||||
#include <CompressionAlgorithm.h>
|
||||
|
||||
|
||||
namespace BPackageKit {
|
||||
@@ -122,19 +122,25 @@ PackageFileHeapAccessorBase::OffsetArray::Init(size_t totalChunkCount,
|
||||
|
||||
|
||||
PackageFileHeapAccessorBase::PackageFileHeapAccessorBase(
|
||||
BErrorOutput* errorOutput, int fd, off_t heapOffset)
|
||||
BErrorOutput* errorOutput, int fd, off_t heapOffset,
|
||||
DecompressionAlgorithmOwner* decompressionAlgorithm)
|
||||
:
|
||||
fErrorOutput(errorOutput),
|
||||
fFD(fd),
|
||||
fHeapOffset(heapOffset),
|
||||
fCompressedHeapSize(0),
|
||||
fUncompressedHeapSize(0)
|
||||
fUncompressedHeapSize(0),
|
||||
fDecompressionAlgorithm(decompressionAlgorithm)
|
||||
{
|
||||
if (fDecompressionAlgorithm != NULL)
|
||||
fDecompressionAlgorithm->AcquireReference();
|
||||
}
|
||||
|
||||
|
||||
PackageFileHeapAccessorBase::~PackageFileHeapAccessorBase()
|
||||
{
|
||||
if (fDecompressionAlgorithm != NULL)
|
||||
fDecompressionAlgorithm->ReleaseReference();
|
||||
}
|
||||
|
||||
|
||||
@@ -213,9 +219,9 @@ PackageFileHeapAccessorBase::DecompressChunkData(void* compressedDataBuffer,
|
||||
size_t uncompressedSize)
|
||||
{
|
||||
size_t actualSize;
|
||||
status_t error = BZlibDecompressor::DecompressSingleBuffer(
|
||||
status_t error = fDecompressionAlgorithm->algorithm->DecompressBuffer(
|
||||
compressedDataBuffer, compressedSize, uncompressedDataBuffer,
|
||||
uncompressedSize, actualSize);
|
||||
uncompressedSize, actualSize, fDecompressionAlgorithm->parameters);
|
||||
if (error != B_OK) {
|
||||
fErrorOutput->PrintError("Failed to decompress chunk data: %s\n",
|
||||
strerror(error));
|
||||
|
||||
@@ -24,9 +24,11 @@ namespace BPrivate {
|
||||
|
||||
|
||||
PackageFileHeapReader::PackageFileHeapReader(BErrorOutput* errorOutput, int fd,
|
||||
off_t heapOffset, off_t compressedHeapSize, uint64 uncompressedHeapSize)
|
||||
off_t heapOffset, off_t compressedHeapSize, uint64 uncompressedHeapSize,
|
||||
DecompressionAlgorithmOwner* decompressionAlgorithm)
|
||||
:
|
||||
PackageFileHeapAccessorBase(errorOutput, fd, heapOffset),
|
||||
PackageFileHeapAccessorBase(errorOutput, fd, heapOffset,
|
||||
decompressionAlgorithm),
|
||||
fOffsets()
|
||||
{
|
||||
fCompressedHeapSize = compressedHeapSize;
|
||||
@@ -87,7 +89,7 @@ PackageFileHeapReader::Clone() const
|
||||
{
|
||||
PackageFileHeapReader* clone = new(std::nothrow) PackageFileHeapReader(
|
||||
fErrorOutput, fFD, fHeapOffset, fCompressedHeapSize,
|
||||
fUncompressedHeapSize);
|
||||
fUncompressedHeapSize, fDecompressionAlgorithm);
|
||||
if (clone == NULL)
|
||||
return NULL;
|
||||
|
||||
|
||||
@@ -20,11 +20,11 @@
|
||||
#include <package/hpkg/DataReader.h>
|
||||
#include <package/hpkg/PackageFileHeapReader.h>
|
||||
#include <RangeArray.h>
|
||||
#include <ZlibCompressor.h>
|
||||
#include <CompressionAlgorithm.h>
|
||||
|
||||
|
||||
// minimum length of data we require before trying to zlib compress them
|
||||
static const size_t kZlibCompressionSizeThreshold = 64;
|
||||
// minimum length of data we require before trying to compress them
|
||||
static const size_t kCompressionSizeThreshold = 64;
|
||||
|
||||
|
||||
namespace BPackageKit {
|
||||
@@ -197,21 +197,28 @@ private:
|
||||
|
||||
|
||||
PackageFileHeapWriter::PackageFileHeapWriter(BErrorOutput* errorOutput, int fd,
|
||||
off_t heapOffset, int32 compressionLevel)
|
||||
off_t heapOffset, CompressionAlgorithmOwner* compressionAlgorithm,
|
||||
DecompressionAlgorithmOwner* decompressionAlgorithm)
|
||||
:
|
||||
PackageFileHeapAccessorBase(errorOutput, fd, heapOffset),
|
||||
PackageFileHeapAccessorBase(errorOutput, fd, heapOffset,
|
||||
decompressionAlgorithm),
|
||||
fPendingDataBuffer(NULL),
|
||||
fCompressedDataBuffer(NULL),
|
||||
fPendingDataSize(0),
|
||||
fOffsets(),
|
||||
fCompressionLevel(compressionLevel)
|
||||
fCompressionAlgorithm(compressionAlgorithm)
|
||||
{
|
||||
if (fCompressionAlgorithm != NULL)
|
||||
fCompressionAlgorithm->AcquireReference();
|
||||
}
|
||||
|
||||
|
||||
PackageFileHeapWriter::~PackageFileHeapWriter()
|
||||
{
|
||||
_Uninit();
|
||||
|
||||
if (fCompressionAlgorithm != NULL)
|
||||
fCompressionAlgorithm->ReleaseReference();
|
||||
}
|
||||
|
||||
|
||||
@@ -522,8 +529,8 @@ PackageFileHeapWriter::_WriteChunk(const void* data, size_t size,
|
||||
return B_NO_MEMORY;
|
||||
}
|
||||
|
||||
// Try to use zlib compression only for data large enough.
|
||||
bool compress = mayCompress && size >= (off_t)kZlibCompressionSizeThreshold;
|
||||
// Try to use compression only for data large enough.
|
||||
bool compress = mayCompress && size >= (off_t)kCompressionSizeThreshold;
|
||||
if (compress) {
|
||||
status_t error = _WriteDataCompressed(data, size);
|
||||
if (error != B_OK) {
|
||||
@@ -547,12 +554,13 @@ PackageFileHeapWriter::_WriteChunk(const void* data, size_t size,
|
||||
status_t
|
||||
PackageFileHeapWriter::_WriteDataCompressed(const void* data, size_t size)
|
||||
{
|
||||
if (fCompressionLevel == B_HPKG_COMPRESSION_LEVEL_NONE)
|
||||
if (fCompressionAlgorithm == NULL)
|
||||
return B_BUFFER_OVERFLOW;
|
||||
|
||||
size_t compressedSize;
|
||||
status_t error = BZlibCompressor::CompressSingleBuffer(data, size,
|
||||
fCompressedDataBuffer, size, compressedSize, fCompressionLevel);
|
||||
status_t error = fCompressionAlgorithm->algorithm->CompressBuffer(data,
|
||||
size, fCompressedDataBuffer, size, compressedSize,
|
||||
fCompressionAlgorithm->parameters);
|
||||
if (error != B_OK) {
|
||||
if (error != B_BUFFER_OVERFLOW) {
|
||||
fErrorOutput->PrintError("Failed to compress chunk data: %s\n",
|
||||
|
||||
@@ -19,10 +19,10 @@
|
||||
#include <ByteOrder.h>
|
||||
#include <DataIO.h>
|
||||
|
||||
#include <package/hpkg/HPKGDefsPrivate.h>
|
||||
#include <ZlibCompressionAlgorithm.h>
|
||||
|
||||
#include <package/hpkg/HPKGDefsPrivate.h>
|
||||
#include <package/hpkg/PackageFileHeapReader.h>
|
||||
#include <ZlibDecompressor.h>
|
||||
|
||||
|
||||
namespace BPackageKit {
|
||||
@@ -814,8 +814,21 @@ ReaderImplBase::InitHeapReader(uint32 compression, uint32 chunkSize,
|
||||
return B_BAD_DATA;
|
||||
}
|
||||
|
||||
DecompressionAlgorithmOwner* decompressionAlgorithm
|
||||
= DecompressionAlgorithmOwner::Create(
|
||||
new(std::nothrow) BZlibCompressionAlgorithm,
|
||||
new(std::nothrow) BZlibDecompressionParameters);
|
||||
BReference<DecompressionAlgorithmOwner> decompressionAlgorithmReference(
|
||||
decompressionAlgorithm, true);
|
||||
|
||||
if (decompressionAlgorithm == NULL
|
||||
|| decompressionAlgorithm->algorithm == NULL
|
||||
|| decompressionAlgorithm->parameters == NULL) {
|
||||
return B_NO_MEMORY;
|
||||
}
|
||||
|
||||
fRawHeapReader = new(std::nothrow) PackageFileHeapReader(fErrorOutput, fFD,
|
||||
offset, compressedSize, uncompressedSize);
|
||||
offset, compressedSize, uncompressedSize, decompressionAlgorithm);
|
||||
if (fRawHeapReader == NULL)
|
||||
return B_NO_MEMORY;
|
||||
|
||||
|
||||
@@ -18,12 +18,12 @@
|
||||
#include <ByteOrder.h>
|
||||
|
||||
#include <AutoDeleter.h>
|
||||
#include <ZlibCompressionAlgorithm.h>
|
||||
|
||||
#include <package/hpkg/DataReader.h>
|
||||
#include <package/hpkg/ErrorOutput.h>
|
||||
|
||||
#include <package/hpkg/HPKGDefsPrivate.h>
|
||||
#include <package/hpkg/PackageFileHeapWriter.h>
|
||||
|
||||
|
||||
namespace BPackageKit {
|
||||
@@ -218,6 +218,10 @@ WriterImplBase::PackageAttribute::_DeleteChildren()
|
||||
WriterImplBase::WriterImplBase(const char* fileType, BErrorOutput* errorOutput)
|
||||
:
|
||||
fHeapWriter(NULL),
|
||||
fCompressionAlgorithm(NULL),
|
||||
fCompressionParameters(NULL),
|
||||
fDecompressionAlgorithm(NULL),
|
||||
fDecompressionParameters(NULL),
|
||||
fFileType(fileType),
|
||||
fErrorOutput(errorOutput),
|
||||
fFileName(NULL),
|
||||
@@ -231,6 +235,10 @@ WriterImplBase::WriterImplBase(const char* fileType, BErrorOutput* errorOutput)
|
||||
WriterImplBase::~WriterImplBase()
|
||||
{
|
||||
delete fHeapWriter;
|
||||
delete fCompressionAlgorithm;
|
||||
delete fCompressionParameters;
|
||||
delete fDecompressionAlgorithm;
|
||||
delete fDecompressionParameters;
|
||||
|
||||
if (fFD >= 0)
|
||||
close(fFD);
|
||||
@@ -265,9 +273,40 @@ WriterImplBase::Init(const char* fileName, size_t headerSize,
|
||||
|
||||
fFileName = fileName;
|
||||
|
||||
DecompressionAlgorithmOwner* decompressionAlgorithm
|
||||
= DecompressionAlgorithmOwner::Create(
|
||||
new(std::nothrow) BZlibCompressionAlgorithm,
|
||||
new(std::nothrow) BZlibDecompressionParameters);
|
||||
BReference<DecompressionAlgorithmOwner> decompressionAlgorithmReference(
|
||||
decompressionAlgorithm, true);
|
||||
|
||||
if (decompressionAlgorithm == NULL
|
||||
|| decompressionAlgorithm->algorithm == NULL
|
||||
|| decompressionAlgorithm->parameters == NULL) {
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
|
||||
CompressionAlgorithmOwner* compressionAlgorithm = NULL;
|
||||
|
||||
if (fParameters.CompressionLevel() != B_HPKG_COMPRESSION_LEVEL_NONE) {
|
||||
compressionAlgorithm = CompressionAlgorithmOwner::Create(
|
||||
new(std::nothrow) BZlibCompressionAlgorithm,
|
||||
new(std::nothrow) BZlibCompressionParameters(
|
||||
fParameters.CompressionLevel()));
|
||||
|
||||
if (compressionAlgorithm == NULL
|
||||
|| compressionAlgorithm->algorithm == NULL
|
||||
|| compressionAlgorithm->parameters == NULL) {
|
||||
throw std::bad_alloc();
|
||||
}
|
||||
}
|
||||
|
||||
BReference<CompressionAlgorithmOwner> compressionAlgorithmReference(
|
||||
compressionAlgorithm, true);
|
||||
|
||||
// create heap writer
|
||||
fHeapWriter = new PackageFileHeapWriter(fErrorOutput, FD(), headerSize,
|
||||
fParameters.CompressionLevel());
|
||||
compressionAlgorithm, decompressionAlgorithm);
|
||||
fHeapWriter->Init();
|
||||
|
||||
return B_OK;
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
#include <package/hpkg/PoolBuffer.h>
|
||||
#include <package/hpkg/v1/HPKGDefsPrivate.h>
|
||||
#include <package/hpkg/v1/PackageData.h>
|
||||
#include <ZlibDecompressor.h>
|
||||
#include <ZlibCompressionAlgorithm.h>
|
||||
|
||||
|
||||
namespace BPackageKit {
|
||||
@@ -294,7 +294,7 @@ private:
|
||||
return error;
|
||||
|
||||
size_t actuallyUncompressedSize;
|
||||
error = BZlibDecompressor::DecompressSingleBuffer(
|
||||
BZlibCompressionAlgorithm().DecompressBuffer(
|
||||
readBuffer->Buffer(), compressedSize,
|
||||
fUncompressBuffer->Buffer(), uncompressedSize,
|
||||
actuallyUncompressedSize);
|
||||
|
||||
@@ -19,10 +19,11 @@
|
||||
#include <ByteOrder.h>
|
||||
#include <DataIO.h>
|
||||
|
||||
#include <package/hpkg/v1/HPKGDefsPrivate.h>
|
||||
|
||||
#include <package/hpkg/ErrorOutput.h>
|
||||
#include <ZlibDecompressor.h>
|
||||
|
||||
#include <AutoDeleter.h>
|
||||
#include <package/hpkg/v1/HPKGDefsPrivate.h>
|
||||
#include <ZlibCompressionAlgorithm.h>
|
||||
|
||||
|
||||
namespace BPackageKit {
|
||||
@@ -1028,13 +1029,17 @@ ReaderImplBase::ReadCompressedBuffer(const SectionInfo& section)
|
||||
|
||||
case B_HPKG_COMPRESSION_ZLIB:
|
||||
{
|
||||
// init the decompressor
|
||||
// create the decompression stream
|
||||
BMemoryIO bufferOutput(section.data, section.uncompressedLength);
|
||||
BZlibDecompressor decompressor(&bufferOutput);
|
||||
status_t error = decompressor.Init();
|
||||
BZlibCompressionAlgorithm algorithm;
|
||||
BDataIO* zlibOutput;
|
||||
status_t error = algorithm.CreateDecompressingOutputStream(
|
||||
&bufferOutput, NULL, zlibOutput);
|
||||
if (error != B_OK)
|
||||
return error;
|
||||
|
||||
ObjectDeleter<BDataIO> zlibOutputDeleter(zlibOutput);
|
||||
|
||||
while (compressedSize > 0) {
|
||||
// read compressed buffer
|
||||
size_t toRead = std::min((size_t)compressedSize,
|
||||
@@ -1044,7 +1049,7 @@ ReaderImplBase::ReadCompressedBuffer(const SectionInfo& section)
|
||||
return error;
|
||||
|
||||
// uncompress
|
||||
error = decompressor.DecompressNext(fScratchBuffer, toRead);
|
||||
error = zlibOutput->WriteExactly(fScratchBuffer, toRead);
|
||||
if (error != B_OK)
|
||||
return error;
|
||||
|
||||
@@ -1052,7 +1057,7 @@ ReaderImplBase::ReadCompressedBuffer(const SectionInfo& section)
|
||||
offset += toRead;
|
||||
}
|
||||
|
||||
error = decompressor.Finish();
|
||||
error = zlibOutput->Flush();
|
||||
if (error != B_OK)
|
||||
return error;
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ for architectureObject in [ MultiArchSubDirSetup ] {
|
||||
|
||||
UseBuildFeatureHeaders zlib ;
|
||||
|
||||
Includes [ FGristFiles ZlibCompressionBasePrivate.h ]
|
||||
Includes [ FGristFiles ZlibCompressionAlgorithm.cpp ]
|
||||
: [ BuildFeatureAttribute zlib : headers ] ;
|
||||
|
||||
MergeObject <libbe!$(architecture)>support_kit.o :
|
||||
@@ -38,9 +38,6 @@ for architectureObject in [ MultiArchSubDirSetup ] {
|
||||
Url.cpp
|
||||
Uuid.cpp
|
||||
ZlibCompressionAlgorithm.cpp
|
||||
ZlibCompressionBase.cpp
|
||||
ZlibCompressor.cpp
|
||||
ZlibDecompressor.cpp
|
||||
;
|
||||
|
||||
StaticLibrary [ MultiArchDefaultGristFiles libreferenceable.a ]
|
||||
|
||||
@@ -232,7 +232,11 @@ struct BZlibCompressionAlgorithm::Stream : BaseClass {
|
||||
BDataIO*& _stream)
|
||||
{
|
||||
const typename Strategy::Parameters* parameters
|
||||
#ifdef _BOOT_MODE
|
||||
= static_cast<const typename Strategy::Parameters*>(_parameters);
|
||||
#else
|
||||
= dynamic_cast<const typename Strategy::Parameters*>(_parameters);
|
||||
#endif
|
||||
Stream* stream = new(std::nothrow) Stream(io);
|
||||
if (stream == NULL)
|
||||
return B_NO_MEMORY;
|
||||
|
||||
@@ -1,80 +0,0 @@
|
||||
/*
|
||||
* Copyright 2009-2014, Ingo Weinhold, ingo_weinhold@gmx.de.
|
||||
* 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;
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
/*
|
||||
* Copyright 2014, Ingo Weinhold, ingo_weinhold@gmx.de.
|
||||
* 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
|
||||
@@ -1,150 +0,0 @@
|
||||
/*
|
||||
* Copyright 2009-2014, Ingo Weinhold, ingo_weinhold@gmx.de.
|
||||
* 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;
|
||||
}
|
||||
@@ -1,155 +0,0 @@
|
||||
/*
|
||||
* Copyright 2009-2014, Ingo Weinhold, ingo_weinhold@gmx.de.
|
||||
* 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;
|
||||
}
|
||||
@@ -43,12 +43,12 @@ BootStaticLibrary boot_packagefs :
|
||||
|
||||
# support kit
|
||||
|
||||
ZlibCompressionBase.cpp
|
||||
ZlibDecompressor.cpp
|
||||
CompressionAlgorithm.cpp
|
||||
ZlibCompressionAlgorithm.cpp
|
||||
|
||||
: -fno-pic
|
||||
;
|
||||
|
||||
Includes [ FGristFiles ZlibCompressionBasePrivate.h ]
|
||||
Includes [ FGristFiles CompressionAlgorithm.cpp ZlibCompressionAlgorithm.cpp ]
|
||||
: [ BuildFeatureAttribute zlib : headers ] ;
|
||||
|
||||
|
||||
@@ -14,11 +14,12 @@ SubDirC++Flags -fno-rtti $(defines) ;
|
||||
|
||||
|
||||
local zlibSources =
|
||||
inflate.c
|
||||
inffast.c
|
||||
inftrees.c
|
||||
adler32.c
|
||||
crc32.c
|
||||
inffast.c
|
||||
inflate.c
|
||||
inftrees.c
|
||||
uncompr.c
|
||||
zutil.c
|
||||
;
|
||||
|
||||
|
||||
@@ -5,11 +5,12 @@ UseHeaders $(zlibSourceDirectory) ;
|
||||
UseHeaders $(zlibSourceDirectory) : true ;
|
||||
|
||||
local zlibSources =
|
||||
inflate.c
|
||||
inffast.c
|
||||
inftrees.c
|
||||
adler32.c
|
||||
crc32.c
|
||||
inffast.c
|
||||
inflate.c
|
||||
inftrees.c
|
||||
uncompr.c
|
||||
zutil.c
|
||||
;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user