]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - pdftops/Stream.cxx
Load cups into easysw/current.
[thirdparty/cups.git] / pdftops / Stream.cxx
index 2e9dfb7d15e9c26b8327397c5c4f327b05d0e025..e2c2cab11b70240cb002591206ca2d1fda646c60 100644 (file)
@@ -7,7 +7,6 @@
 //========================================================================
 
 #include <config.h>
-#include <limits.h>
 
 #ifdef USE_GCC_PRAGMAS
 #pragma implementation
@@ -16,6 +15,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <stddef.h>
+#include <limits.h>
 #ifndef WIN32
 #include <unistd.h>
 #endif
@@ -402,8 +402,6 @@ void ImageStream::skipLine() {
 
 StreamPredictor::StreamPredictor(Stream *strA, int predictorA,
                                 int widthA, int nCompsA, int nBitsA) {
-  int totalBits;
-
   str = strA;
   predictor = predictorA;
   width = widthA;
@@ -412,21 +410,16 @@ StreamPredictor::StreamPredictor(Stream *strA, int predictorA,
   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);
@@ -1281,9 +1274,11 @@ CCITTFaxStream::CCITTFaxStream(Stream *strA, int encodingA, GBool endOfLineA,
   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;
@@ -2926,8 +2921,8 @@ GBool DCTStream::readBaselineSOF() {
   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) {
@@ -2956,6 +2951,11 @@ GBool DCTStream::readProgressiveSOF() {
   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;
@@ -2978,6 +2978,11 @@ GBool DCTStream::readScanInfo() {
 
   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");
@@ -3062,7 +3067,7 @@ GBool DCTStream::readHuffmanTables() {
   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;
     }
@@ -3072,6 +3077,7 @@ GBool DCTStream::readHuffmanTables() {
        numACHuffTables = index+1;
       tbl = &acHuffTables[index];
     } else {
+      index &= 0x0f;
       if (index >= numDCHuffTables)
        numDCHuffTables = index+1;
       tbl = &dcHuffTables[index];
@@ -3188,7 +3194,6 @@ int DCTStream::readMarker() {
     } while (c != 0xff && c != EOF);
     do {
       c = str->getChar();
-      if (c == EOF) return EOF;
     } while (c == 0xff);
   } while (c == 0x00);
   return c;