Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove 'long' overloads for Xdr::read and Xdr::write functions #952

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/lib/OpenEXR/ImfIDManifestAttribute.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ template <>
void
IDManifestAttribute::writeValueTo (OPENEXR_IMF_INTERNAL_NAMESPACE::OStream &os, int version) const
{
Xdr::write<StreamIO>(os,_value._uncompressedDataSize);
uint64_t uncompressedDataSize = _value._uncompressedDataSize;
Xdr::write<StreamIO>(os,uncompressedDataSize);
const char* output = (const char*) _value._data;
Xdr::write <StreamIO> (os, output,_value._compressedDataSize);

Expand All @@ -47,11 +48,13 @@ IDManifestAttribute::readValueFrom (OPENEXR_IMF_INTERNAL_NAMESPACE::IStream &is,
_value._data = nullptr;
}


uint64_t uncompressedDataSize;
//
// first four bytes: data size once data is uncompressed
// first eight bytes: data size once data is uncompressed
//
Xdr::read<StreamIO>(is,_value._uncompressedDataSize);
Xdr::read<StreamIO>(is,uncompressedDataSize);

_value._uncompressedDataSize = uncompressedDataSize;

//
// allocate memory for compressed storage and read data
Expand Down
208 changes: 32 additions & 176 deletions src/lib/OpenEXR/ImfXdr.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,19 +153,12 @@ write (T &out, unsigned int v);

template <class S, class T>
void
write (T &out, signed long v);
write (T &out, int64_t v);

template <class S, class T>
void
write (T &out, unsigned long v);
write (T &out, uint64_t v);

#if ULONG_MAX != 18446744073709551615LU

template <class S, class T>
void
write (T &out, uint64_t v);

#endif

template <class S, class T>
void
Expand Down Expand Up @@ -236,19 +229,11 @@ read (T &in, unsigned int &v);

template <class S, class T>
void
read (T &in, signed long &v);
read (T &in, int64_t &v);

template <class S, class T>
void
read (T &in, unsigned long &v);

#if ULONG_MAX != 18446744073709551615LU

template <class S, class T>
void
read (T &in, uint64_t &v);

#endif
read (T &in, uint64_t &v);

template <class S, class T>
void
Expand Down Expand Up @@ -418,105 +403,41 @@ write (T &out, unsigned int v)

template <class S, class T>
void
write (T &out, signed long v)
write (T &out, int64_t v)
{
signed char b[8];

b[0] = (signed char) (v);
b[1] = (signed char) (v >> 8);
b[2] = (signed char) (v >> 16);
b[3] = (signed char) (v >> 24);

#if LONG_MAX == 2147483647

if (v >= 0)
{
b[4] = 0;
b[5] = 0;
b[6] = 0;
b[7] = 0;
}
else
{
b[4] = ~0;
b[5] = ~0;
b[6] = ~0;
b[7] = ~0;
}

#elif LONG_MAX == 9223372036854775807L

b[4] = (signed char) (v >> 32);
b[5] = (signed char) (v >> 40);
b[6] = (signed char) (v >> 48);
b[7] = (signed char) (v >> 56);

#else

#error write<T> (T &out, signed long v) not implemented

#endif
b[4] = (signed char) (v >> 32);
b[5] = (signed char) (v >> 40);
b[6] = (signed char) (v >> 48);
b[7] = (signed char) (v >> 56);

writeSignedChars<S> (out, b, 8);
}


template <class S, class T>
void
write (T &out, unsigned long v)
write (T &out, uint64_t v)
{
unsigned char b[8];

b[0] = (unsigned char) (v);
b[1] = (unsigned char) (v >> 8);
b[2] = (unsigned char) (v >> 16);
b[3] = (unsigned char) (v >> 24);

#if ULONG_MAX == 4294967295U

b[4] = 0;
b[5] = 0;
b[6] = 0;
b[7] = 0;

#elif ULONG_MAX == 18446744073709551615LU

b[4] = (unsigned char) (v >> 32);
b[5] = (unsigned char) (v >> 40);
b[6] = (unsigned char) (v >> 48);
b[7] = (unsigned char) (v >> 56);

#else

#error write<T> (T &out, unsigned long v) not implemented

#endif
b[4] = (unsigned char) (v >> 32);
b[5] = (unsigned char) (v >> 40);
b[6] = (unsigned char) (v >> 48);
b[7] = (unsigned char) (v >> 56);

writeUnsignedChars<S> (out, b, 8);
}


#if ULONG_MAX != 18446744073709551615LU

template <class S, class T>
void
write (T &out, uint64_t v)
{
unsigned char b[8];

b[0] = (unsigned char) (v);
b[1] = (unsigned char) (v >> 8);
b[2] = (unsigned char) (v >> 16);
b[3] = (unsigned char) (v >> 24);
b[4] = (unsigned char) (v >> 32);
b[5] = (unsigned char) (v >> 40);
b[6] = (unsigned char) (v >> 48);
b[7] = (unsigned char) (v >> 56);

writeUnsignedChars<S> (out, b, 8);
}

#endif


template <class S, class T>
Expand Down Expand Up @@ -699,108 +620,43 @@ read (T &in, unsigned int &v)

template <class S, class T>
void
read (T &in, signed long &v)
read (T &in, int64_t &v)
{
signed char b[8];

readSignedChars<S> (in, b, 8);

#if LONG_MAX == 2147483647

v = (static_cast <unsigned long> (b[0]) & 0x000000ff) |
((static_cast <unsigned long> (b[1]) << 8) & 0x0000ff00) |
((static_cast <unsigned long> (b[2]) << 16) & 0x00ff0000) |
(static_cast <unsigned long> (b[3]) << 24);

if (( b[4] || b[5] || b[6] || b[7]) &&
(~b[4] || ~b[5] || ~b[6] || ~b[7]))
{
throw IEX_NAMESPACE::OverflowExc ("Long int overflow - read a large "
"64-bit integer in a 32-bit process.");
}

#elif LONG_MAX == 9223372036854775807L

v = (static_cast <unsigned long> (b[0]) & 0x00000000000000ff) |
((static_cast <unsigned long> (b[1]) << 8) & 0x000000000000ff00) |
((static_cast <unsigned long> (b[2]) << 16) & 0x0000000000ff0000) |
((static_cast <unsigned long> (b[3]) << 24) & 0x00000000ff000000) |
((static_cast <unsigned long> (b[4]) << 32) & 0x000000ff00000000) |
((static_cast <unsigned long> (b[5]) << 40) & 0x0000ff0000000000) |
((static_cast <unsigned long> (b[6]) << 48) & 0x00ff000000000000) |
(static_cast <unsigned long> (b[7]) << 56);
v = (static_cast <int64_t> (b[0]) & 0x00000000000000ff) |
((static_cast <int64_t> (b[1]) << 8) & 0x000000000000ff00) |
((static_cast <int64_t> (b[2]) << 16) & 0x0000000000ff0000) |
((static_cast <int64_t> (b[3]) << 24) & 0x00000000ff000000) |
((static_cast <int64_t> (b[4]) << 32) & 0x000000ff00000000) |
((static_cast <int64_t> (b[5]) << 40) & 0x0000ff0000000000) |
((static_cast <int64_t> (b[6]) << 48) & 0x00ff000000000000) |
(static_cast <int64_t> (b[7]) << 56);

#else

#error read<T> (T &in, signed long &v) not implemented

#endif
}


template <class S, class T>
void
read (T &in, unsigned long &v)
read (T &in, uint64_t &v)
{
unsigned char b[8];

readUnsignedChars<S> (in, b, 8);

#if ULONG_MAX == 4294967295U

v = (b[0] & 0x000000ff) |
((b[1] << 8) & 0x0000ff00) |
((b[2] << 16) & 0x00ff0000) |
(b[3] << 24);

if (b[4] || b[5] || b[6] || b[7])
{
throw IEX_NAMESPACE::OverflowExc ("Long int overflow - read a large "
"64-bit integer in a 32-bit process.");
}

#elif ULONG_MAX == 18446744073709551615LU

v = ((unsigned long) b[0] & 0x00000000000000ff) |
(((unsigned long) b[1] << 8) & 0x000000000000ff00) |
(((unsigned long) b[2] << 16) & 0x0000000000ff0000) |
(((unsigned long) b[3] << 24) & 0x00000000ff000000) |
(((unsigned long) b[4] << 32) & 0x000000ff00000000) |
(((unsigned long) b[5] << 40) & 0x0000ff0000000000) |
(((unsigned long) b[6] << 48) & 0x00ff000000000000) |
((unsigned long) b[7] << 56);

#else

#error read<T> (T &in, unsigned long &v) not implemented

#endif
v = ((uint64_t) b[0] & 0x00000000000000ffLL) |
(((uint64_t) b[1] << 8) & 0x000000000000ff00LL) |
(((uint64_t) b[2] << 16) & 0x0000000000ff0000LL) |
(((uint64_t) b[3] << 24) & 0x00000000ff000000LL) |
(((uint64_t) b[4] << 32) & 0x000000ff00000000LL) |
(((uint64_t) b[5] << 40) & 0x0000ff0000000000LL) |
(((uint64_t) b[6] << 48) & 0x00ff000000000000LL) |
((uint64_t) b[7] << 56);
}


#if ULONG_MAX != 18446744073709551615LU

template <class S, class T>
void
read (T &in, uint64_t &v)
{
unsigned char b[8];

readUnsignedChars<S> (in, b, 8);

v = ((uint64_t) b[0] & 0x00000000000000ffLL) |
(((uint64_t) b[1] << 8) & 0x000000000000ff00LL) |
(((uint64_t) b[2] << 16) & 0x0000000000ff0000LL) |
(((uint64_t) b[3] << 24) & 0x00000000ff000000LL) |
(((uint64_t) b[4] << 32) & 0x000000ff00000000LL) |
(((uint64_t) b[5] << 40) & 0x0000ff0000000000LL) |
(((uint64_t) b[6] << 48) & 0x00ff000000000000LL) |
((uint64_t) b[7] << 56);
}

#endif


template <class S, class T>
void
read (T &in, float &v)
Expand Down
16 changes: 8 additions & 8 deletions src/test/OpenEXRTest/testXdr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ writeData (ostream &os)
Xdr::write<CharIO> (os, (signed int) -2012345678);
Xdr::write<CharIO> (os, (unsigned int) 1234567890u);
Xdr::write<CharIO> (os, (unsigned int) 2345678901u);
Xdr::write<CharIO> (os, (signed long) 2034567890);
Xdr::write<CharIO> (os, (signed long) -2045678901);
Xdr::write<CharIO> (os, (unsigned long) 1345678901u);
Xdr::write<CharIO> (os, (unsigned long) 2456789012u);
Xdr::write<CharIO> (os, (int64_t) 2034567890);
Xdr::write<CharIO> (os, (int64_t) -2045678901);
Xdr::write<CharIO> (os, (uint64_t) 1345678901u);
Xdr::write<CharIO> (os, (uint64_t) 2456789012u);
Xdr::write<CharIO> (os, (uint64_t) 0x1122334455667788ll);
Xdr::write<CharIO> (os, (uint64_t) 0xf1f2f3f4f5f6f7f8ll);
Xdr::write<CharIO> (os, (float) 0.0f);
Expand Down Expand Up @@ -195,10 +195,10 @@ readData (istream &is)
check (is, (signed int) -2012345678);
check (is, (unsigned int) 1234567890u);
check (is, (unsigned int) 2345678901u);
check (is, (signed long) 2034567890);
check (is, (signed long) -2045678901);
check (is, (unsigned long) 1345678901u);
check (is, (unsigned long) 2456789012u);
check (is, (int64_t) 2034567890);
check (is, (int64_t) -2045678901);
check (is, (uint64_t) 1345678901u);
check (is, (uint64_t) 2456789012u);
check (is, (uint64_t) 0x1122334455667788ll);
check (is, (uint64_t) 0xf1f2f3f4f5f6f7f8ll);
check (is, (float) 0.0f);
Expand Down