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 (AcademySoftwareFoundation#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 and cary-ilm committed Mar 2, 2023
1 parent 5f67f8d commit c6be7f6
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 136 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
119 changes: 61 additions & 58 deletions src/lib/OpenEXR/ImfDwaCompressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@
#include "ImathBox.h"
#include "ImathVec.h"
#include "half.h"
#include "halfLimits.h"

#include <vector>
#include <string>
Expand Down Expand Up @@ -2166,14 +2165,15 @@ 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,
&outSize,
(const Bytef *)_planarUncBuffer[UNKNOWN],
inSize,
9))
if (Z_OK != ::compress2 (
reinterpret_cast<Bytef*> (outDataPtr),
&outSize,
reinterpret_cast<const Bytef*> (_planarUncBuffer[UNKNOWN]),
inSize,
9))
{
throw IEX_NAMESPACE::BaseExc ("Data compression (zlib) failed.");
}
Expand Down Expand Up @@ -2205,16 +2205,16 @@ DwaCompressor::compress
case DEFLATE:

{
uLongf destLen = compressBound (
(*totalAcUncompressedCount) * sizeof (unsigned short));

if (Z_OK != ::compress2
((Bytef *)outDataPtr,
&destLen,
(Bytef *)_packedAcBuffer,
(uLong)(*totalAcUncompressedCount
* sizeof (unsigned short)),
9))
uLong sourceLen = static_cast<uLong> (*totalAcUncompressedCount * sizeof (unsigned short));
uLong destLen = compressBound (sourceLen);

if (Z_OK !=
::compress2 (
reinterpret_cast<Bytef*> (outDataPtr),
&destLen,
reinterpret_cast<Bytef*> (_packedAcBuffer),
sourceLen,
9))
{
throw IEX_NAMESPACE::InputExc ("Data compression (zlib) failed.");
}
Expand Down Expand Up @@ -2258,14 +2258,15 @@ 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,
&dstLen,
(Bytef *)_rleBuffer,
(uLong)(*rleUncompressedSize),
9))
if (Z_OK != ::compress2 (
reinterpret_cast<Bytef*> (outDataPtr),
&dstLen,
reinterpret_cast<const Bytef*> (_rleBuffer),
srcLen,
9))
{
throw IEX_NAMESPACE::BaseExc ("Error compressing RLE'd data.");
}
Expand Down Expand Up @@ -2508,13 +2509,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],
&outSize,
(Bytef *)compressedUnknownBuf,
(uLong)unknownCompressedSize))
if (Z_OK != ::uncompress (
reinterpret_cast<Bytef*> (_planarUncBuffer[UNKNOWN]),
&outSize,
reinterpret_cast<const Bytef*> (compressedUnknownBuf),
inSize))
{
throw IEX_NAMESPACE::BaseExc("Error uncompressing UNKNOWN data.");
}
Expand Down Expand Up @@ -2548,16 +2550,15 @@ DwaCompressor::uncompress

break;

case DEFLATE:
{
uLongf destLen =
(int)(totalAcUncompressedCount) * sizeof (unsigned short);

if (Z_OK != ::uncompress
((Bytef *)_packedAcBuffer,
&destLen,
(Bytef *)compressedAcBuf,
(uLong)acCompressedSize))
case DEFLATE: {
uLong destLen = static_cast<uLong> (totalAcUncompressedCount * sizeof (unsigned short));
uLong sourceLen = static_cast<uLong> (acCompressedSize);

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

uLongf dstLen = (uLongf)rleUncompressedSize;

if (Z_OK != ::uncompress
((Bytef *)_rleBuffer,
&dstLen,
(Bytef *)compressedRleBuf,
(uLong)rleCompressedSize))
uLong dstLen = static_cast<uLong> (rleUncompressedSize);
uLong srcLen = static_cast<uLong> (rleCompressedSize);

if (Z_OK != ::uncompress (
reinterpret_cast<Bytef*> (_rleBuffer),
&dstLen,
reinterpret_cast<const Bytef*> (compressedRleBuf),
srcLen))
{
throw IEX_NAMESPACE::BaseExc("Error uncompressing RLE data.");
}
Expand Down Expand Up @@ -2960,11 +2962,11 @@ DwaCompressor::initializeBuffers (size_t &outBufferSize)
// or for zlib compression (for DEFLATE)
//

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

case RLE:
{
Expand Down Expand Up @@ -2999,13 +3001,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));
maxOutBufferSize +=
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 @@ -3125,8 +3128,8 @@ DwaCompressor::initializeBuffers (size_t &outBufferSize)

if (planarUncBufferSize[UNKNOWN] > 0)
{
planarUncBufferSize[UNKNOWN] =
static_cast<uint64_t>( compressBound (planarUncBufferSize[UNKNOWN]) );
planarUncBufferSize[UNKNOWN] = static_cast<uint64_t> (
compressBound (static_cast<uLong> (planarUncBufferSize[UNKNOWN])));
}

for (int i = 0; i < NUM_COMPRESSOR_SCHEMES; ++i)
Expand Down
80 changes: 42 additions & 38 deletions src/lib/OpenEXR/ImfIDManifest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,26 +554,29 @@ void IDManifest::init(const char* data, const char* endOfData)

IDManifest::IDManifest(const CompressedIDManifest& compressed)
{
//
// decompress the compressed manifest
//


vector<Bytef> uncomp(compressed._uncompressedDataSize);
uLongf outSize = compressed._uncompressedDataSize;
if(Z_OK != ::uncompress(&uncomp[0] , &outSize , (const Bytef*) compressed._data, compressed._compressedDataSize))
{
throw IEX_NAMESPACE::InputExc ("IDManifest decompression (zlib) failed.");
}
if(outSize!=compressed._uncompressedDataSize)
{
throw IEX_NAMESPACE::InputExc ("IDManifest decompression (zlib) failed: mismatch in decompressed data size");
}

init((const char*) &uncomp[0],(const char*) &uncomp[0] + outSize);



//
// decompress the compressed manifest
//

vector<Bytef> uncomp (compressed._uncompressedDataSize);
uLong outSize = static_cast<uLong> (compressed._uncompressedDataSize);
uLong inSize = static_cast<uLong> (compressed._compressedDataSize);
if (Z_OK != ::uncompress (
uncomp.data(),
&outSize,
reinterpret_cast<const Bytef*> (compressed._data),
inSize))
{
throw IEX_NAMESPACE::InputExc (
"IDManifest decompression (zlib) failed.");
}
if (outSize != compressed._uncompressedDataSize)
{
throw IEX_NAMESPACE::InputExc (
"IDManifest decompression (zlib) failed: mismatch in decompressed data size");
}

init ((const char*) &uncomp[0], (const char*) &uncomp[0] + outSize);
}


Expand Down Expand Up @@ -1092,27 +1095,28 @@ CompressedIDManifest::CompressedIDManifest(const IDManifest& manifest)


std::vector<char> serial;
manifest.serialize(serial);
uLong outputSize = serial.size();

manifest.serialize (serial);

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

//
// allocate a buffer which is guaranteed to be big enough for compression
//
uLongf compressedDataSize = compressBound(outputSize);
_data = (unsigned char*) malloc(compressedDataSize);
if(Z_OK != ::compress(_data,&compressedDataSize,(Bytef*) &serial[0],outputSize))
{
throw IEX_NAMESPACE::InputExc("ID manifest compression failed");
}

// now call realloc to reallocate the buffer to a smaller size - this might free up memory
_data = (unsigned char*) realloc(_data,compressedDataSize);

_uncompressedDataSize = outputSize;
_compressedDataSize = compressedDataSize;

uLong compressedDataSize = compressBound (outputSize);
_data = (unsigned char*) malloc (compressedDataSize);
if (Z_OK !=
::compress (
_data, &compressedDataSize, reinterpret_cast<Bytef*> (serial.data ()), outputSize))
{
throw IEX_NAMESPACE::InputExc ("ID manifest compression failed");
}

// now call realloc to reallocate the buffer to a smaller size - this might free up memory
_data = (unsigned char*) realloc (_data, compressedDataSize);

_uncompressedDataSize = outputSize;
_compressedDataSize = compressedDataSize;
}

IDManifest::ChannelGroupManifest::ChannelGroupManifest() : _lifeTime(IDManifest::LIFETIME_STABLE) , _hashScheme(IDManifest::UNKNOWN) , _encodingScheme(IDManifest::UNKNOWN) , _insertingEntry(false)
Expand Down
41 changes: 22 additions & 19 deletions src/lib/OpenEXR/ImfPxr24Compressor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,14 @@ Pxr24Compressor::compress (const char *inPtr,
}
}

uLongf outSize = int (ceil ((tmpBufferEnd - _tmpBuffer) * 1.01)) + 100;

if (Z_OK != ::compress ((Bytef *) _outBuffer,
&outSize,
(const Bytef *) _tmpBuffer,
tmpBufferEnd - _tmpBuffer))
uLong inBufferSize = static_cast<uLong> (tmpBufferEnd - _tmpBuffer);
uLong outSize = compressBound (inBufferSize);

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

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

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

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

for (int j = 0; j < n; ++j)
{
Expand All @@ -466,8 +470,8 @@ Pxr24Compressor::uncompress (const char *inPtr,
ptr[1] = ptr[0] + n;
tmpBufferEnd = ptr[1] + n;

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

for (int j = 0; j < n; ++j)
{
Expand All @@ -490,8 +494,8 @@ Pxr24Compressor::uncompress (const char *inPtr,
ptr[2] = ptr[1] + n;
tmpBufferEnd = ptr[2] + n;

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

for (int j = 0; j < n; ++j)
{
Expand All @@ -515,8 +519,7 @@ Pxr24Compressor::uncompress (const char *inPtr,
}
}

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

outPtr = _outBuffer;
return writePtr - _outBuffer;
Expand Down
Loading

0 comments on commit c6be7f6

Please sign in to comment.