Commit 0ae2c1ed authored by Brad King's avatar Brad King
Browse files

vtk(|ZLib)DataCompressor: Use size_t for buffer length arguments

This will handle buffers as large as the architecture can fit in memory.
Cast types inside vtkZLibDataCompressor to match zlib API.

Change-Id: I81d943d12dab981070c5171f0e6a96c54fb5b23d
parent 6214dd05
......@@ -29,8 +29,8 @@ int TestCompress(int argc, char *argv[])
unsigned char buffer[start_size];
unsigned char* cbuffer;
unsigned char* ucbuffer;
unsigned long nlen;
unsigned long rlen;
size_t nlen;
size_t rlen;
vtkZLibDataCompressor* compressor = vtkZLibDataCompressor::New();
for ( cc = 0; cc < start_size; cc ++ )
......
......@@ -33,22 +33,22 @@ void vtkDataCompressor::PrintSelf(ostream& os, vtkIndent indent)
}
//----------------------------------------------------------------------------
unsigned long
vtkDataCompressor::Compress(const unsigned char* uncompressedData,
unsigned long uncompressedSize,
size_t
vtkDataCompressor::Compress(unsigned char const* uncompressedData,
size_t uncompressedSize,
unsigned char* compressedData,
unsigned long compressionSpace)
size_t compressionSpace)
{
return this->CompressBuffer(uncompressedData, uncompressedSize,
compressedData, compressionSpace);
}
//----------------------------------------------------------------------------
unsigned long
vtkDataCompressor::Uncompress(const unsigned char* compressedData,
unsigned long compressedSize,
size_t
vtkDataCompressor::Uncompress(unsigned char const* compressedData,
size_t compressedSize,
unsigned char* uncompressedData,
unsigned long uncompressedSize)
size_t uncompressedSize)
{
return this->UncompressBuffer(compressedData, compressedSize,
uncompressedData, uncompressedSize);
......@@ -56,11 +56,11 @@ vtkDataCompressor::Uncompress(const unsigned char* compressedData,
//----------------------------------------------------------------------------
vtkUnsignedCharArray*
vtkDataCompressor::Compress(const unsigned char* uncompressedData,
unsigned long uncompressedSize)
vtkDataCompressor::Compress(unsigned char const* uncompressedData,
size_t uncompressedSize)
{
// Get the amount of space needed for compressed data.
unsigned long compressionSpace =
size_t compressionSpace =
this->GetMaximumCompressionSpace(uncompressedSize);
// Allocate a buffer.
......@@ -70,7 +70,7 @@ vtkDataCompressor::Compress(const unsigned char* uncompressedData,
unsigned char* compressedData = outputArray->GetPointer(0);
// Compress the data.
unsigned long compressedSize =
size_t compressedSize =
this->CompressBuffer(uncompressedData, uncompressedSize,
compressedData, compressionSpace);
......@@ -89,9 +89,9 @@ vtkDataCompressor::Compress(const unsigned char* uncompressedData,
//----------------------------------------------------------------------------
vtkUnsignedCharArray*
vtkDataCompressor::Uncompress(const unsigned char* compressedData,
unsigned long compressedSize,
unsigned long uncompressedSize)
vtkDataCompressor::Uncompress(unsigned char const* compressedData,
size_t compressedSize,
size_t uncompressedSize)
{
// Allocate a buffer.
vtkUnsignedCharArray* outputArray = vtkUnsignedCharArray::New();
......@@ -100,7 +100,7 @@ vtkDataCompressor::Uncompress(const unsigned char* compressedData,
unsigned char* uncompressedData = outputArray->GetPointer(0);
// Decompress the data.
unsigned long decSize =
size_t decSize =
this->UncompressBuffer(compressedData, compressedSize,
uncompressedData, uncompressedSize);
......
......@@ -38,33 +38,33 @@ public:
// given uncompressed size after compression. This is the minimum
// size of the output buffer that can be passed to the four-argument
// Compress method.
virtual unsigned long GetMaximumCompressionSpace(unsigned long size)=0;
virtual size_t GetMaximumCompressionSpace(size_t size)=0;
// Description:
// Compress the given input data buffer into the given output
// buffer. The size of the output buffer must be at least as large
// as the value given by GetMaximumCompressionSpace for the given
// input size.
unsigned long Compress(const unsigned char* uncompressedData,
unsigned long uncompressedSize,
unsigned char* compressedData,
unsigned long compressionSpace);
size_t Compress(unsigned char const* uncompressedData,
size_t uncompressedSize,
unsigned char* compressedData,
size_t compressionSpace);
// Description:
// Uncompress the given input data into the given output buffer.
// The size of the uncompressed data must be known by the caller.
// It should be transmitted from the compressor by a means outside
// of this class.
unsigned long Uncompress(const unsigned char* compressedData,
unsigned long compressedSize,
unsigned char* uncompressedData,
unsigned long uncompressedSize);
size_t Uncompress(unsigned char const* compressedData,
size_t compressedSize,
unsigned char* uncompressedData,
size_t uncompressedSize);
// Description:
// Compress the given data. A vtkUnsignedCharArray containing the
// compressed data is returned with a reference count of 1.
vtkUnsignedCharArray* Compress(const unsigned char* uncompressedData,
unsigned long uncompressedSize);
vtkUnsignedCharArray* Compress(unsigned char const* uncompressedData,
size_t uncompressedSize);
// Description:
// Uncompress the given data. A vtkUnsignedCharArray containing the
......@@ -72,25 +72,25 @@ public:
// size of the uncompressed data must be known by the caller. It
// should be transmitted from the compressor by a means outside of
// this class.
vtkUnsignedCharArray* Uncompress(const unsigned char* compressedData,
unsigned long compressedSize,
unsigned long uncompressedSize);
vtkUnsignedCharArray* Uncompress(unsigned char const* compressedData,
size_t compressedSize,
size_t uncompressedSize);
protected:
vtkDataCompressor();
~vtkDataCompressor();
// Actual compression method. This must be provided by a subclass.
// Must return the size of the compressed data, or zero on error.
virtual unsigned long CompressBuffer(const unsigned char* uncompressedData,
unsigned long uncompressedSize,
unsigned char* compressedData,
unsigned long compressionSpace)=0;
virtual size_t CompressBuffer(unsigned char const* uncompressedData,
size_t uncompressedSize,
unsigned char* compressedData,
size_t compressionSpace)=0;
// Actual decompression method. This must be provided by a subclass.
// Must return the size of the uncompressed data, or zero on error.
virtual unsigned long UncompressBuffer(const unsigned char* compressedData,
unsigned long compressedSize,
unsigned char* uncompressedData,
unsigned long uncompressedSize)=0;
virtual size_t UncompressBuffer(unsigned char const* compressedData,
size_t compressedSize,
unsigned char* uncompressedData,
size_t uncompressedSize)=0;
private:
vtkDataCompressor(const vtkDataCompressor&); // Not implemented.
void operator=(const vtkDataCompressor&); // Not implemented.
......
......@@ -38,58 +38,60 @@ void vtkZLibDataCompressor::PrintSelf(ostream& os, vtkIndent indent)
}
//----------------------------------------------------------------------------
unsigned long
vtkZLibDataCompressor::CompressBuffer(const unsigned char* uncompressedData,
unsigned long uncompressedSize,
size_t
vtkZLibDataCompressor::CompressBuffer(unsigned char const* uncompressedData,
size_t uncompressedSize,
unsigned char* compressedData,
unsigned long compressionSpace)
size_t compressionSpace)
{
unsigned long compressedSize = compressionSpace;
uLongf cs = static_cast<uLongf>(compressionSpace);
Bytef* cd = reinterpret_cast<Bytef*>(compressedData);
const Bytef* ud = reinterpret_cast<const Bytef*>(uncompressedData);
uLong us = static_cast<uLong>(uncompressedSize);
// Call zlib's compress function.
if(compress2(cd, &compressedSize, ud, uncompressedSize, this->CompressionLevel) != Z_OK)
if(compress2(cd, &cs, ud, us, this->CompressionLevel) != Z_OK)
{
vtkErrorMacro("Zlib error while compressing data.");
return 0;
}
return compressedSize;
return static_cast<size_t>(cs);
}
//----------------------------------------------------------------------------
unsigned long
vtkZLibDataCompressor::UncompressBuffer(const unsigned char* compressedData,
unsigned long compressedSize,
size_t
vtkZLibDataCompressor::UncompressBuffer(unsigned char const* compressedData,
size_t compressedSize,
unsigned char* uncompressedData,
unsigned long uncompressedSize)
size_t uncompressedSize)
{
unsigned long decSize = uncompressedSize;
uLongf us = static_cast<uLongf>(uncompressedSize);
Bytef* ud = reinterpret_cast<Bytef*>(uncompressedData);
const Bytef* cd = reinterpret_cast<const Bytef*>(compressedData);
uLong cs = static_cast<uLong>(compressedSize);
// Call zlib's uncompress function.
if(uncompress(ud, &decSize, cd, compressedSize) != Z_OK)
if(uncompress(ud, &us, cd, cs) != Z_OK)
{
vtkErrorMacro("Zlib error while uncompressing data.");
return 0;
}
// Make sure the output size matched that expected.
if(decSize != uncompressedSize)
if(us != static_cast<uLongf>(uncompressedSize))
{
vtkErrorMacro("Decompression produced incorrect size.\n"
"Expected " << uncompressedSize << " and got " << decSize);
"Expected " << uncompressedSize << " and got " << us);
return 0;
}
return decSize;
return static_cast<size_t>(us);
}
//----------------------------------------------------------------------------
unsigned long
vtkZLibDataCompressor::GetMaximumCompressionSpace(unsigned long size)
size_t
vtkZLibDataCompressor::GetMaximumCompressionSpace(size_t size)
{
// ZLib specifies that destination buffer must be 0.1% larger + 12 bytes.
return size + (size+999)/1000 + 12;
......
......@@ -35,7 +35,7 @@ public:
// given uncompressed size after compression. This is the minimum
// size of the output buffer that can be passed to the four-argument
// Compress method.
unsigned long GetMaximumCompressionSpace(unsigned long size);
size_t GetMaximumCompressionSpace(size_t size);
// Description:
// Get/Set the compression level.
......@@ -49,15 +49,15 @@ protected:
int CompressionLevel;
// Compression method required by vtkDataCompressor.
unsigned long CompressBuffer(const unsigned char* uncompressedData,
unsigned long uncompressedSize,
unsigned char* compressedData,
unsigned long compressionSpace);
size_t CompressBuffer(unsigned char const* uncompressedData,
size_t uncompressedSize,
unsigned char* compressedData,
size_t compressionSpace);
// Decompression method required by vtkDataCompressor.
unsigned long UncompressBuffer(const unsigned char* compressedData,
unsigned long compressedSize,
unsigned char* uncompressedData,
unsigned long uncompressedSize);
size_t UncompressBuffer(unsigned char const* compressedData,
size_t compressedSize,
unsigned char* uncompressedData,
size_t uncompressedSize);
private:
vtkZLibDataCompressor(const vtkZLibDataCompressor&); // Not implemented.
void operator=(const vtkZLibDataCompressor&); // Not implemented.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment