mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-04 12:05:29 -04:00
#489 Fix for Unexpected End of PackBits Stream for padded streams.
This commit is contained in:
parent
e8d1b999a2
commit
f6aa810f8b
@ -71,8 +71,6 @@ public final class PackBitsDecoder implements Decoder {
|
||||
private final boolean disableNoOp;
|
||||
private final byte[] sample;
|
||||
|
||||
private int leftOfRun;
|
||||
private boolean splitRun;
|
||||
private boolean reachedEOF;
|
||||
|
||||
/** Creates a {@code PackBitsDecoder}. */
|
||||
@ -114,65 +112,43 @@ public final class PackBitsDecoder implements Decoder {
|
||||
* @param buffer a byte array, minimum 128 (or 129 if no-op is disabled) bytes long
|
||||
* @return The number of bytes decoded
|
||||
*
|
||||
* @throws java.io.IOException
|
||||
* @throws java.io.IOException if a problem occurs during decoding.
|
||||
*/
|
||||
public int decode(final InputStream stream, final ByteBuffer buffer) throws IOException {
|
||||
if (reachedEOF) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
// TODO: Don't decode more than single runs, because some writers add pad bytes inside the stream...
|
||||
while (buffer.hasRemaining()) {
|
||||
int n;
|
||||
// NOTE: We don't decode more than single runs, because some writers add pad bytes inside the stream...
|
||||
// Start new run
|
||||
int b = stream.read();
|
||||
if (b < 0) {
|
||||
reachedEOF = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (splitRun) {
|
||||
// Continue run
|
||||
n = leftOfRun;
|
||||
splitRun = false;
|
||||
int n = (byte) b;
|
||||
|
||||
try {
|
||||
if (n >= 0) {
|
||||
// Copy next n + 1 bytes literally
|
||||
readFully(stream, buffer, sample.length * (n + 1));
|
||||
}
|
||||
else {
|
||||
// Start new run
|
||||
int b = stream.read();
|
||||
if (b < 0) {
|
||||
reachedEOF = true;
|
||||
break;
|
||||
// Allow -128 for compatibility, see above
|
||||
else if (disableNoOp || n != -128) {
|
||||
// Replicate the next byte -n + 1 times
|
||||
for (int s = 0; s < sample.length; s++) {
|
||||
sample[s] = readByte(stream);
|
||||
}
|
||||
n = (byte) b;
|
||||
}
|
||||
|
||||
// Split run at or before max
|
||||
if (n >= 0 && n + 1 > buffer.remaining()) {
|
||||
leftOfRun = n;
|
||||
splitRun = true;
|
||||
break;
|
||||
}
|
||||
else if (n < 0 && -n + 1 > buffer.remaining()) {
|
||||
leftOfRun = n;
|
||||
splitRun = true;
|
||||
break;
|
||||
}
|
||||
|
||||
try {
|
||||
if (n >= 0) {
|
||||
// Copy next n + 1 bytes literally
|
||||
readFully(stream, buffer, sample.length * (n + 1));
|
||||
for (int i = -n + 1; i > 0; i--) {
|
||||
buffer.put(sample);
|
||||
}
|
||||
// Allow -128 for compatibility, see above
|
||||
else if (disableNoOp || n != -128) {
|
||||
// Replicate the next byte -n + 1 times
|
||||
for (int s = 0; s < sample.length; s++) {
|
||||
sample[s] = readByte(stream);
|
||||
}
|
||||
|
||||
for (int i = -n + 1; i > 0; i--) {
|
||||
buffer.put(sample);
|
||||
}
|
||||
}
|
||||
// else NOOP (-128)
|
||||
}
|
||||
catch (IndexOutOfBoundsException e) {
|
||||
throw new DecodeException("Error in PackBits decompression, data seems corrupt", e);
|
||||
}
|
||||
// else NOOP (-128)
|
||||
}
|
||||
catch (IndexOutOfBoundsException e) {
|
||||
throw new DecodeException("Error in PackBits decompression, data seems corrupt", e);
|
||||
}
|
||||
|
||||
return buffer.position();
|
||||
|
Loading…
x
Reference in New Issue
Block a user