//========================================================================
#include <config.h>
-#include <limits.h>
#ifdef USE_GCC_PRAGMAS
#pragma implementation
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
+#include <limits.h>
#ifndef WIN32
#include <unistd.h>
#endif
StreamPredictor::StreamPredictor(Stream *strA, int predictorA,
int widthA, int nCompsA, int nBitsA) {
- int totalBits;
-
str = strA;
predictor = predictorA;
width = widthA;
predLine = NULL;
ok = gFalse;
- if (width <= 0 || nComps <= 0 || nBits <= 0 ||
- nComps >= INT_MAX/nBits ||
- width >= INT_MAX/nComps/nBits) {
- return;
- }
nVals = width * nComps;
- totalBits = nVals * nBits;
- if (totalBits == 0 ||
- (totalBits / nBits) / nComps != width ||
- totalBits + 7 < 0) {
+ if (width <= 0 || nComps <= 0 || nBits <= 0 ||
+ nComps >= INT_MAX / nBits ||
+ width >= INT_MAX / nComps / nBits ||
+ nVals * nBits + 7 < 0) {
return;
}
pixBytes = (nComps * nBits + 7) >> 3;
- rowBytes = ((totalBits + 7) >> 3) + pixBytes;
- if (rowBytes < 0) {
+ rowBytes = ((nVals * nBits + 7) >> 3) + pixBytes;
+ if (rowBytes <= 0) {
return;
}
predLine = (Guchar *)gmalloc(rowBytes);
endOfLine = endOfLineA;
byteAlign = byteAlignA;
columns = columnsA;
- if (columns < 1 || columns >= (INT_MAX / sizeof(short))) {
- error(getPos(), "Bad number of columns: %d in CCITTFaxStream", columns);
- exit(1);
+ if (columns < 1) {
+ columns = 1;
+ }
+ if (columns + 4 <= 0) {
+ columns = INT_MAX - 4;
}
rows = rowsA;
endOfBlock = endOfBlockA;
width = read16();
numComps = str->getChar();
if (numComps <= 0 || numComps > 4) {
+ error(getPos(), "Bad number of components in DCT stream");
numComps = 0;
- error(getPos(), "Bad number of components in DCT stream", prec);
return gFalse;
}
if (prec != 8) {
height = read16();
width = read16();
numComps = str->getChar();
+ if (numComps <= 0 || numComps > 4) {
+ error(getPos(), "Bad number of components in DCT stream");
+ numComps = 0;
+ return gFalse;
+ }
if (prec != 8) {
error(getPos(), "Bad DCT precision %d", prec);
return gFalse;
length = read16() - 2;
scanInfo.numComps = str->getChar();
+ if (scanInfo.numComps <= 0 || scanInfo.numComps > 4) {
+ error(getPos(), "Bad number of components in DCT stream");
+ scanInfo.numComps = 0;
+ return gFalse;
+ }
--length;
if (length != 2 * scanInfo.numComps + 3) {
error(getPos(), "Bad DCT scan info block");
while (length > 0) {
index = str->getChar();
--length;
- if ((index & 0x0f) >= 4 || (index & ~0x1f)) {
+ if ((index & 0x0f) >= 4) {
error(getPos(), "Bad DCT Huffman table");
return gFalse;
}
numACHuffTables = index+1;
tbl = &acHuffTables[index];
} else {
+ index &= 0x0f;
if (index >= numDCHuffTables)
numDCHuffTables = index+1;
tbl = &dcHuffTables[index];
} while (c != 0xff && c != EOF);
do {
c = str->getChar();
- if (c == EOF) return EOF;
} while (c == 0xff);
} while (c == 0x00);
return c;