Skip to content

Commit

Permalink
Add explicit casts around the usage of zlib datatypes to avoid warnin…
Browse files Browse the repository at this point in the history
…gs on Windows (#1259)

* Add vscode to .gitignore

Signed-off-by: Chris Kulla <ckulla@gmail.com>

* Add explicit casts around the usage of zlib datatypes to avoid warnings on Windows

Windows defines 'long' to be 32-bits, even for 64-bit builds. This causes MSVC to complain about most of the code surrounding calls to the zlib API.

Luckily OpenEXR uses a minimal set of calls from zlib:
  * compress2
  * compressBound
  * uncompress

This change focuses on preserving the current behavior, but adds explicit casts to the appropriate types. Future attemps to address this should grep for uLong and the three functions above.

As part of this cleanup, I also found a few places that had bespoke implementations of compressBound which I replaced with a call the real function.

Cleaned up a stray use of halfLimits.h which is a deprecated header and is not required.

Signed-off-by: Chris Kulla <ckulla@gmail.com>
  • Loading branch information
fpsunflower authored May 15, 2022
1 parent 91c4ae1 commit 74645c5
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 67 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,4 @@ build/
build-win/
build-nuget/
*~
.vscode
64 changes: 33 additions & 31 deletions src/lib/OpenEXR/ImfDwaCompressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@
#include "ImathFun.h"
#include "ImathVec.h"
#include "half.h"
#include "halfLimits.h"

#include <algorithm>
#include <cassert>
Expand Down Expand Up @@ -2074,13 +2073,13 @@ DwaCompressor::compress (

if (*unknownUncompressedSize > 0)
{
uLongf inSize = (uLongf) (*unknownUncompressedSize);
uLongf outSize = compressBound (inSize);
uLong inSize = static_cast<uLong> (*unknownUncompressedSize);
uLong outSize = compressBound (inSize);

if (Z_OK != ::compress2 (
(Bytef*) outDataPtr,
reinterpret_cast<Bytef*> (outDataPtr),
&outSize,
(const Bytef*) _planarUncBuffer[UNKNOWN],
reinterpret_cast<const Bytef*> (_planarUncBuffer[UNKNOWN]),
inSize,
9))
{
Expand Down Expand Up @@ -2114,15 +2113,15 @@ DwaCompressor::compress (
case DEFLATE:

{
uLongf destLen = compressBound (
(*totalAcUncompressedCount) * sizeof (unsigned short));
uLong sourceLen = static_cast<uLong> (*totalAcUncompressedCount * sizeof (unsigned short));
uLong destLen = compressBound (sourceLen);

if (Z_OK !=
::compress2 (
(Bytef*) outDataPtr,
reinterpret_cast<Bytef*> (outDataPtr),
&destLen,
(Bytef*) _packedAcBuffer,
(uLong) (*totalAcUncompressedCount * sizeof (unsigned short)),
reinterpret_cast<Bytef*> (_packedAcBuffer),
sourceLen,
9))
{
throw IEX_NAMESPACE::InputExc (
Expand Down Expand Up @@ -2166,13 +2165,14 @@ DwaCompressor::compress (
_planarUncBuffer[RLE],
(signed char*) _rleBuffer);

uLongf dstLen = compressBound ((uLongf) *rleUncompressedSize);
uLong srcLen = static_cast<uLong> (*rleUncompressedSize);
uLong dstLen = compressBound (srcLen);

if (Z_OK != ::compress2 (
(Bytef*) outDataPtr,
reinterpret_cast<Bytef*> (outDataPtr),
&dstLen,
(Bytef*) _rleBuffer,
(uLong) (*rleUncompressedSize),
reinterpret_cast<const Bytef*> (_rleBuffer),
srcLen,
9))
{
throw IEX_NAMESPACE::BaseExc ("Error compressing RLE'd data.");
Expand Down Expand Up @@ -2402,13 +2402,14 @@ DwaCompressor::uncompress (
"(corrupt header).");
}

uLongf outSize = (uLongf) unknownUncompressedSize;
uLong inSize = static_cast<uLong> (unknownCompressedSize);
uLong outSize = static_cast<uLong> (unknownUncompressedSize);

if (Z_OK != ::uncompress (
(Bytef*) _planarUncBuffer[UNKNOWN],
reinterpret_cast<Bytef*> (_planarUncBuffer[UNKNOWN]),
&outSize,
(Bytef*) compressedUnknownBuf,
(uLong) unknownCompressedSize))
reinterpret_cast<const Bytef*> (compressedUnknownBuf),
inSize))
{
throw IEX_NAMESPACE::BaseExc ("Error uncompressing UNKNOWN data.");
}
Expand Down Expand Up @@ -2445,14 +2446,14 @@ DwaCompressor::uncompress (
break;

case DEFLATE: {
uLongf destLen =
(int) (totalAcUncompressedCount) * sizeof (unsigned short);
uLong destLen = static_cast<uLong> (totalAcUncompressedCount * sizeof (unsigned short));
uLong sourceLen = static_cast<uLong> (acCompressedSize);

if (Z_OK != ::uncompress (
(Bytef*) _packedAcBuffer,
reinterpret_cast<Bytef*> (_packedAcBuffer),
&destLen,
(Bytef*) compressedAcBuf,
(uLong) acCompressedSize))
reinterpret_cast<const Bytef*> (compressedAcBuf),
sourceLen))
{
throw IEX_NAMESPACE::InputExc (
"Data decompression (zlib) failed.");
Expand Down Expand Up @@ -2516,13 +2517,14 @@ DwaCompressor::uncompress (
"(corrupt header).");
}

uLongf dstLen = (uLongf) rleUncompressedSize;
uLong dstLen = static_cast<uLong> (rleUncompressedSize);
uLong srcLen = static_cast<uLong> (rleCompressedSize);

if (Z_OK != ::uncompress (
(Bytef*) _rleBuffer,
reinterpret_cast<Bytef*> (_rleBuffer),
&dstLen,
(Bytef*) compressedRleBuf,
(uLong) rleCompressedSize))
reinterpret_cast<const Bytef*> (compressedRleBuf),
srcLen))
{
throw IEX_NAMESPACE::BaseExc ("Error uncompressing RLE data.");
}
Expand Down Expand Up @@ -2870,7 +2872,7 @@ DwaCompressor::initializeBuffers (size_t& outBufferSize)

maxOutBufferSize += std::max (
2lu * maxLossyDctAcSize + 65536lu,
static_cast<uint64_t> (compressBound (maxLossyDctAcSize)));
static_cast<uint64_t> (compressBound (static_cast<uLong> (maxLossyDctAcSize))));
numLossyDctChans++;
break;

Expand Down Expand Up @@ -2910,14 +2912,14 @@ DwaCompressor::initializeBuffers (size_t& outBufferSize)
// which could take slightly more space
//

maxOutBufferSize += static_cast<uint64_t> (compressBound (rleBufferSize));
maxOutBufferSize += static_cast<uint64_t> (compressBound (static_cast<uLong> (rleBufferSize)));

//
// And the same goes for the UNKNOWN data
//

maxOutBufferSize +=
static_cast<uint64_t> (compressBound (unknownBufferSize));
static_cast<uint64_t> (compressBound (static_cast<uLong> (unknownBufferSize)));

//
// Allocate a zip/deflate compressor big enought to hold the DC data
Expand Down Expand Up @@ -3036,7 +3038,7 @@ DwaCompressor::initializeBuffers (size_t& outBufferSize)
if (planarUncBufferSize[UNKNOWN] > 0)
{
planarUncBufferSize[UNKNOWN] = static_cast<uint64_t> (
compressBound (planarUncBufferSize[UNKNOWN]));
compressBound (static_cast<uLong> (planarUncBufferSize[UNKNOWN])));
}

for (int i = 0; i < NUM_COMPRESSOR_SCHEMES; ++i)
Expand Down
15 changes: 8 additions & 7 deletions src/lib/OpenEXR/ImfIDManifest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,12 +537,13 @@ IDManifest::IDManifest (const CompressedIDManifest& compressed)
//

vector<Bytef> uncomp (compressed._uncompressedDataSize);
uLongf outSize = compressed._uncompressedDataSize;
uLong outSize = static_cast<uLong> (compressed._uncompressedDataSize);
uLong inSize = static_cast<uLong> (compressed._compressedDataSize);
if (Z_OK != ::uncompress (
&uncomp[0],
uncomp.data(),
&outSize,
(const Bytef*) compressed._data,
compressed._compressedDataSize))
reinterpret_cast<const Bytef*> (compressed._data),
inSize))
{
throw IEX_NAMESPACE::InputExc (
"IDManifest decompression (zlib) failed.");
Expand Down Expand Up @@ -1061,16 +1062,16 @@ CompressedIDManifest::CompressedIDManifest (const IDManifest& manifest)

manifest.serialize (serial);

uLong outputSize = serial.size ();
uLong outputSize = static_cast<uLong> (serial.size ());

//
// allocate a buffer which is guaranteed to be big enough for compression
//
uLongf compressedDataSize = compressBound (outputSize);
uLong compressedDataSize = compressBound (outputSize);
_data = (unsigned char*) malloc (compressedDataSize);
if (Z_OK !=
::compress (
_data, &compressedDataSize, (Bytef*) &serial[0], outputSize))
_data, &compressedDataSize, reinterpret_cast<Bytef*> (serial.data ()), outputSize))
{
throw IEX_NAMESPACE::InputExc ("ID manifest compression failed");
}
Expand Down
24 changes: 14 additions & 10 deletions src/lib/OpenEXR/ImfPxr24Compressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,13 +341,14 @@ Pxr24Compressor::compress (
}
}

uLongf outSize = int (ceil ((tmpBufferEnd - _tmpBuffer) * 1.01)) + 100;
uLong inBufferSize = static_cast<uLong> (tmpBufferEnd - _tmpBuffer);
uLong outSize = compressBound (inBufferSize);

if (Z_OK != ::compress (
(Bytef*) _outBuffer,
reinterpret_cast<Bytef*> (_outBuffer),
&outSize,
(const Bytef*) _tmpBuffer,
tmpBufferEnd - _tmpBuffer))
reinterpret_cast<const Bytef*> (_tmpBuffer),
inBufferSize))
{
throw IEX_NAMESPACE::BaseExc ("Data compression (zlib) failed.");
}
Expand All @@ -366,11 +367,14 @@ Pxr24Compressor::uncompress (
return 0;
}

uLongf tmpSize = _maxScanLineSize * _numScanLines;
uLong tmpSize = static_cast<uLong> (_maxScanLineSize * _numScanLines);

if (Z_OK !=
::uncompress (
(Bytef*) _tmpBuffer, &tmpSize, (const Bytef*) inPtr, inSize))
reinterpret_cast<Bytef*> (_tmpBuffer),
&tmpSize,
reinterpret_cast<const Bytef*> (inPtr),
inSize))
{
throw IEX_NAMESPACE::InputExc ("Data decompression (zlib) failed.");
}
Expand Down Expand Up @@ -408,7 +412,7 @@ Pxr24Compressor::uncompress (
ptr[3] = ptr[2] + n;
tmpBufferEnd = ptr[3] + n;

if ((uLongf) (tmpBufferEnd - _tmpBuffer) > tmpSize)
if (static_cast<uLong> (tmpBufferEnd - _tmpBuffer) > tmpSize)
notEnoughData ();

for (int j = 0; j < n; ++j)
Expand All @@ -433,7 +437,7 @@ Pxr24Compressor::uncompress (
ptr[1] = ptr[0] + n;
tmpBufferEnd = ptr[1] + n;

if ((uLongf) (tmpBufferEnd - _tmpBuffer) > tmpSize)
if (static_cast<uLong> (tmpBufferEnd - _tmpBuffer) > tmpSize)
notEnoughData ();

for (int j = 0; j < n; ++j)
Expand All @@ -456,7 +460,7 @@ Pxr24Compressor::uncompress (
ptr[2] = ptr[1] + n;
tmpBufferEnd = ptr[2] + n;

if ((uLongf) (tmpBufferEnd - _tmpBuffer) > tmpSize)
if (static_cast<uLong> (tmpBufferEnd - _tmpBuffer) > tmpSize)
notEnoughData ();

for (int j = 0; j < n; ++j)
Expand All @@ -479,7 +483,7 @@ Pxr24Compressor::uncompress (
}
}

if ((uLongf) (tmpBufferEnd - _tmpBuffer) < tmpSize) tooMuchData ();
if (static_cast<uLong> (tmpBufferEnd - _tmpBuffer) < tmpSize) tooMuchData ();

outPtr = _outBuffer;
return writePtr - _outBuffer;
Expand Down
18 changes: 10 additions & 8 deletions src/lib/OpenEXR/ImfZip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,14 @@ Zip::compress (const char* raw, int rawSize, char* compressed)
// Compress the data using zlib
//

uLongf outSize = int (ceil (rawSize * 1.01)) + 100;
uLong inSize = static_cast<uLong> (rawSize);
uLong outSize = compressBound (inSize);

if (Z_OK != ::compress2 (
(Bytef*) compressed,
reinterpret_cast<Bytef*> (compressed),
&outSize,
(const Bytef*) _tmpBuffer,
rawSize,
reinterpret_cast<const Bytef*> (_tmpBuffer),
inSize,
_zipLevel))
{
throw IEX_NAMESPACE::BaseExc ("Data compression (zlib) failed.");
Expand Down Expand Up @@ -240,13 +241,14 @@ Zip::uncompress (const char* compressed, int compressedSize, char* raw)
// Decompress the data using zlib
//

uLongf outSize = _maxRawSize;
uLong outSize = static_cast<uLong> (_maxRawSize);
uLong inSize = static_cast<uLong> (compressedSize);

if (Z_OK != ::uncompress (
(Bytef*) _tmpBuffer,
reinterpret_cast<Bytef*> (_tmpBuffer),
&outSize,
(const Bytef*) compressed,
compressedSize))
reinterpret_cast<const Bytef*> (compressed),
inSize))
{
throw IEX_NAMESPACE::InputExc ("Data decompression (zlib) failed.");
}
Expand Down
8 changes: 4 additions & 4 deletions src/lib/OpenEXRCore/internal_pxr24.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ apply_pxr24_impl (exr_encode_pipeline_t* encode)
uint8_t* out = encode->scratch_buffer_1;
uint64_t nOut = 0;
const uint8_t* lastIn = encode->packed_buffer;
uLongf compbufsz = encode->compressed_alloc_size;
uLong compbufsz = (uLong) encode->compressed_alloc_size;

for (int y = 0; y < encode->chunk.height; ++y)
{
Expand Down Expand Up @@ -219,7 +219,7 @@ apply_pxr24_impl (exr_encode_pipeline_t* encode)
(Bytef*) encode->compressed_buffer,
&compbufsz,
(const Bytef*) encode->scratch_buffer_1,
nOut))
(uLong) nOut))
{
return EXR_ERR_CORRUPT_CHUNK;
}
Expand All @@ -229,7 +229,7 @@ apply_pxr24_impl (exr_encode_pipeline_t* encode)
encode->compressed_buffer,
encode->packed_buffer,
encode->packed_bytes);
compbufsz = encode->packed_bytes;
compbufsz = (uLong) encode->packed_bytes;
}
encode->compressed_bytes = compbufsz;
return EXR_ERR_SUCCESS;
Expand Down Expand Up @@ -262,7 +262,7 @@ undo_pxr24_impl (
void* scratch_data,
uint64_t scratch_size)
{
uLongf outSize = (uLongf) uncompressed_size;
uLong outSize = (uLong) uncompressed_size;
int rstat;
uint8_t* out = uncompressed_data;
uint64_t nOut = 0;
Expand Down
6 changes: 3 additions & 3 deletions src/lib/OpenEXRCore/internal_zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ undo_zip_impl (
void* scratch_data,
uint64_t scratch_size)
{
uLongf outSize = (uLongf) uncompressed_size;
uLong outSize = (uLong) uncompressed_size;
int rstat;

if (scratch_size < uncompressed_size) return EXR_ERR_INVALID_ARGUMENT;
Expand Down Expand Up @@ -226,7 +226,7 @@ apply_zip_impl (exr_encode_pipeline_t* encode)
const uint8_t* raw = encode->packed_buffer;
const uint8_t* stop = raw + encode->packed_bytes;
int p, level;
uLongf compbufsz = encode->compressed_alloc_size;
uLong compbufsz = (uLong) encode->compressed_alloc_size;
exr_result_t rv = EXR_ERR_SUCCESS;

rv = exr_get_zip_compression_level (
Expand Down Expand Up @@ -257,7 +257,7 @@ apply_zip_impl (exr_encode_pipeline_t* encode)
(Bytef*) encode->compressed_buffer,
&compbufsz,
(const Bytef*) encode->scratch_buffer_1,
encode->packed_bytes,
(uLong) encode->packed_bytes,
level))
{
return EXR_ERR_CORRUPT_CHUNK;
Expand Down
9 changes: 5 additions & 4 deletions src/test/OpenEXRTest/testIDManifest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,14 +136,15 @@ doReadWriteManifest (const IDManifest& mfst, const string& fn, bool dump)
//
// allocate a buffer which is guaranteed to be big enough for compression
//
uLongf compressedDataSize = compressBound (str.str ().size ());
uLong sourceDataSize = static_cast<uLong> (str.str ().size ());
uLong compressedDataSize = compressBound (sourceDataSize);
vector<char> compressed (compressedDataSize);

::compress (
(Bytef*) &compressed[0],
reinterpret_cast<Bytef*> (compressed.data ()),
&compressedDataSize,
(const Bytef*) str.str ().c_str (),
str.str ().size ());
reinterpret_cast<const Bytef*> (str.str ().c_str ()),
sourceDataSize);

cerr << "simple zip size: " << compressedDataSize << ' ';
#endif
Expand Down

0 comments on commit 74645c5

Please sign in to comment.