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

check for valid Huf code lengths #849

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
9 changes: 9 additions & 0 deletions OpenEXR/IlmImf/ImfHuf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,11 @@ hufDecode
//

lc -= pl.len;

if ( lc < 0 )
{
invalidCode(); // code length too long
}
getCode (pl.lit, rlc, c, lc, in, out, outb, oe);
}
else
Expand Down Expand Up @@ -967,6 +972,10 @@ hufDecode
if (pl.len)
{
lc -= pl.len;
if ( lc < 0 )
{
invalidCode(); // code length too long
}
getCode (pl.lit, rlc, c, lc, in, out, outb, oe);
}
else
Expand Down
144 changes: 80 additions & 64 deletions OpenEXR/IlmImfTest/testHuf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,70 +245,86 @@ testHuf (const std::string&)

IMATH_NAMESPACE::Rand48 rand48 (0);

const int N = 1000000;
Array <unsigned short> raw (N);

fill1 (raw, N, 1, rand48); // test various symbol distributions
compressUncompress (raw, N);
compressUncompressSubset (raw, N);
fill1 (raw, N, 10, rand48);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);
fill1 (raw, N, 100, rand48);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);
fill1 (raw, N, 1000, rand48);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);

fill2 (raw, N, 1, rand48);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);
fill2 (raw, N, 10, rand48);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);
fill2 (raw, N, 100, rand48);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);
fill2 (raw, N, 1000, rand48);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);

fill3 (raw, N, 0);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);
fill3 (raw, N, 1);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);
fill3 (raw, N, USHRT_MAX - 1);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);
fill3 (raw, N, USHRT_MAX);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);

fill4 (raw, USHRT_MAX + 1);
compressVerify(raw, USHRT_MAX + 1, HUF_COMPRESS_DEK_HASH_FOR_FILL4_USHRT_MAX_PLUS_ONE);
compressUncompress (raw, USHRT_MAX + 1);
compressUncompressSubset (raw, USHRT_MAX + 1);
fill4 (raw, N);
compressVerify(raw, N, HUF_COMPRESS_DEK_HASH_FOR_FILL4_N);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);

fill4 (raw, 0);
compressUncompress (raw, 0); // test small input data sets
fill4 (raw, 1);
compressUncompress (raw, 1);
fill4 (raw, 2);
compressUncompress (raw, 2);
fill4 (raw, 3);
compressUncompress (raw, 3);

fill5 (raw, N); // test run-length coding of code table
compressVerify(raw, N, HUF_COMPRESS_DEK_HASH_FOR_FILL5_N);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);
//
// FastHufDecoder is used for more than 128 bits, so first test with fewer than 128 bits,
// then test FastHufDecoder
//
for (int pass = 0 ; pass < 2 ; ++pass)
{

int N = pass==0 ? 12 : 1000000;
Array <unsigned short> raw (N);

fill1 (raw, N, 1, rand48); // test various symbol distributions
compressUncompress (raw, N);
compressUncompressSubset (raw, N);
fill1 (raw, N, 10, rand48);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);
fill1 (raw, N, 100, rand48);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);
fill1 (raw, N, 1000, rand48);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);

fill2 (raw, N, 1, rand48);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);
fill2 (raw, N, 10, rand48);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);
fill2 (raw, N, 100, rand48);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);
fill2 (raw, N, 1000, rand48);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);

fill3 (raw, N, 0);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);
fill3 (raw, N, 1);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);
fill3 (raw, N, USHRT_MAX - 1);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);
fill3 (raw, N, USHRT_MAX);
compressUncompress (raw, N);
compressUncompressSubset (raw, N);

if (pass==1)
{
fill4 (raw, USHRT_MAX + 1);
compressVerify(raw, USHRT_MAX + 1, HUF_COMPRESS_DEK_HASH_FOR_FILL4_USHRT_MAX_PLUS_ONE);

compressUncompress (raw, USHRT_MAX + 1);
compressUncompressSubset (raw, USHRT_MAX + 1);
fill4 (raw, N);
compressVerify(raw, N, HUF_COMPRESS_DEK_HASH_FOR_FILL4_N);
}
compressUncompress (raw, N);
compressUncompressSubset (raw, N);

fill4 (raw, 0);
compressUncompress (raw, 0); // test small input data sets
fill4 (raw, 1);
compressUncompress (raw, 1);
fill4 (raw, 2);
compressUncompress (raw, 2);
fill4 (raw, 3);
compressUncompress (raw, 3);

fill5 (raw, N); // test run-length coding of code table
if (pass==1)
{
compressVerify(raw, N, HUF_COMPRESS_DEK_HASH_FOR_FILL5_N);
}
compressUncompress (raw, N);
compressUncompressSubset (raw, N);

}

cout << "ok\n" << endl;
}
Expand Down