[poppler] poppler/JBIG2Stream.cc

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Sun Nov 15 11:43:06 UTC 2020


 poppler/JBIG2Stream.cc |  257 ++++++++++++++++++++++++-------------------------
 1 file changed, 129 insertions(+), 128 deletions(-)

New commits:
commit 95a4ee50304666f299890daa41fdb40ac8e38384
Author: Albert Astals Cid <aacid at kde.org>
Date:   Sun Nov 15 12:37:03 2020 +0100

    Remove #if that was confusing clang-format
    
    Code looks better now

diff --git a/poppler/JBIG2Stream.cc b/poppler/JBIG2Stream.cc
index 3014c9e1..62d7ada8 100644
--- a/poppler/JBIG2Stream.cc
+++ b/poppler/JBIG2Stream.cc
@@ -1705,167 +1705,168 @@ bool JBIG2Stream::readSymbolDictSeg(unsigned int segNum, unsigned int length, un
                         break;
                     }
                 }
-#if 0 //~ This special case was added about a year before the final draft
-      //~ of the JBIG2 spec was released.  I have encountered some old
-      //~ JBIG2 images that predate it.
-	if (0) {
-#else
+                //~ This special case was added about a year before the final draft
+                //~ of the JBIG2 spec was released.  I have encountered some old
+                //~ JBIG2 images that predate it.
+                //~ if (0) {
                 if (refAggNum == 1) {
-#endif
-                if (huff) {
-                    symID = huffDecoder->readBits(symCodeLen);
-                    huffDecoder->decodeInt(&refDX, huffTableO);
-                    huffDecoder->decodeInt(&refDY, huffTableO);
-                    huffDecoder->decodeInt(&bmSize, huffTableA);
-                    huffDecoder->reset();
-                    arithDecoder->start();
-                } else {
-                    if (iaidStats == nullptr) {
+                    if (huff) {
+                        symID = huffDecoder->readBits(symCodeLen);
+                        huffDecoder->decodeInt(&refDX, huffTableO);
+                        huffDecoder->decodeInt(&refDY, huffTableO);
+                        huffDecoder->decodeInt(&bmSize, huffTableA);
+                        huffDecoder->reset();
+                        arithDecoder->start();
+                    } else {
+                        if (iaidStats == nullptr) {
+                            goto syntaxError;
+                        }
+                        symID = arithDecoder->decodeIAID(symCodeLen, iaidStats);
+                        arithDecoder->decodeInt(&refDX, iardxStats);
+                        arithDecoder->decodeInt(&refDY, iardyStats);
+                    }
+                    if (symID >= numInputSyms + i) {
+                        error(errSyntaxError, curStr->getPos(), "Invalid symbol ID in JBIG2 symbol dictionary");
+                        goto syntaxError;
+                    }
+                    refBitmap = bitmaps[symID];
+                    if (unlikely(refBitmap == nullptr)) {
+                        error(errSyntaxError, curStr->getPos(), "Invalid ref bitmap for symbol ID {0:ud} in JBIG2 symbol dictionary", symID);
                         goto syntaxError;
                     }
-                    symID = arithDecoder->decodeIAID(symCodeLen, iaidStats);
-                    arithDecoder->decodeInt(&refDX, iardxStats);
-                    arithDecoder->decodeInt(&refDY, iardyStats);
+                    bitmaps[numInputSyms + i] = readGenericRefinementRegion(symWidth, symHeight, sdrTemplate, false, refBitmap, refDX, refDY, sdrATX, sdrATY);
+                    //~ do we need to use the bmSize value here (in Huffman mode)?
+                } else {
+                    bitmaps[numInputSyms + i] = readTextRegion(huff, true, symWidth, symHeight, refAggNum, 0, numInputSyms + i, nullptr, symCodeLen, bitmaps, 0, 0, 0, 1, 0, huffTableF, huffTableH, huffTableK, huffTableO, huffTableO,
+                                                               huffTableO, huffTableO, huffTableA, sdrTemplate, sdrATX, sdrATY);
                 }
-                if (symID >= numInputSyms + i) {
-                    error(errSyntaxError, curStr->getPos(), "Invalid symbol ID in JBIG2 symbol dictionary");
+
+                // non-ref/agg coding
+            } else {
+                bitmaps[numInputSyms + i] = readGenericBitmap(false, symWidth, symHeight, sdTemplate, false, false, nullptr, sdATX, sdATY, 0);
+            }
+
+            ++i;
+        }
+
+        // read the collective bitmap
+        if (huff && !refAgg) {
+            huffDecoder->decodeInt(&bmSize, huffBMSizeTable);
+            huffDecoder->reset();
+            if (bmSize == 0) {
+                collBitmap = new JBIG2Bitmap(0, totalWidth, symHeight);
+                bmSize = symHeight * ((totalWidth + 7) >> 3);
+                p = collBitmap->getDataPtr();
+                if (unlikely(p == nullptr)) {
+                    delete collBitmap;
                     goto syntaxError;
                 }
-                refBitmap = bitmaps[symID];
-                if (unlikely(refBitmap == nullptr)) {
-                    error(errSyntaxError, curStr->getPos(), "Invalid ref bitmap for symbol ID {0:ud} in JBIG2 symbol dictionary", symID);
-                    goto syntaxError;
+                for (k = 0; k < (unsigned int)bmSize; ++k) {
+                    if ((c = curStr->getChar()) == EOF) {
+                        memset(p, 0, bmSize - k);
+                        break;
+                    }
+                    *p++ = (unsigned char)c;
                 }
-                bitmaps[numInputSyms + i] = readGenericRefinementRegion(symWidth, symHeight, sdrTemplate, false, refBitmap, refDX, refDY, sdrATX, sdrATY);
-                //~ do we need to use the bmSize value here (in Huffman mode)?
             } else {
-                bitmaps[numInputSyms + i] = readTextRegion(huff, true, symWidth, symHeight, refAggNum, 0, numInputSyms + i, nullptr, symCodeLen, bitmaps, 0, 0, 0, 1, 0, huffTableF, huffTableH, huffTableK, huffTableO, huffTableO, huffTableO,
-                                                           huffTableO, huffTableA, sdrTemplate, sdrATX, sdrATY);
+                collBitmap = readGenericBitmap(true, totalWidth, symHeight, 0, false, false, nullptr, nullptr, nullptr, bmSize);
+            }
+            if (likely(collBitmap != nullptr)) {
+                x = 0;
+                for (; j < i; ++j) {
+                    bitmaps[numInputSyms + j] = collBitmap->getSlice(x, 0, symWidths[j], symHeight);
+                    x += symWidths[j];
+                }
+                delete collBitmap;
+            } else {
+                error(errSyntaxError, curStr->getPos(), "collBitmap was null");
+                goto syntaxError;
             }
-
-            // non-ref/agg coding
         }
-        else { bitmaps[numInputSyms + i] = readGenericBitmap(false, symWidth, symHeight, sdTemplate, false, false, nullptr, sdATX, sdATY, 0); }
+    }
 
-        ++i;
+    // create the symbol dict object
+    symbolDict = new JBIG2SymbolDict(segNum, numExSyms);
+    if (!symbolDict->isOk()) {
+        delete symbolDict;
+        goto syntaxError;
     }
 
-    // read the collective bitmap
-    if (huff && !refAgg) {
-        huffDecoder->decodeInt(&bmSize, huffBMSizeTable);
-        huffDecoder->reset();
-        if (bmSize == 0) {
-            collBitmap = new JBIG2Bitmap(0, totalWidth, symHeight);
-            bmSize = symHeight * ((totalWidth + 7) >> 3);
-            p = collBitmap->getDataPtr();
-            if (unlikely(p == nullptr)) {
-                delete collBitmap;
-                goto syntaxError;
-            }
-            for (k = 0; k < (unsigned int)bmSize; ++k) {
-                if ((c = curStr->getChar()) == EOF) {
-                    memset(p, 0, bmSize - k);
-                    break;
-                }
-                *p++ = (unsigned char)c;
-            }
+    // exported symbol list
+    i = j = 0;
+    ex = false;
+    run = 0; // initialize it once in case the first decodeInt fails
+             // we do not want to use uninitialized memory
+    while (i < numInputSyms + numNewSyms) {
+        if (huff) {
+            huffDecoder->decodeInt(&run, huffTableA);
         } else {
-            collBitmap = readGenericBitmap(true, totalWidth, symHeight, 0, false, false, nullptr, nullptr, nullptr, bmSize);
+            arithDecoder->decodeInt(&run, iaexStats);
+        }
+        if (i + run > numInputSyms + numNewSyms || (ex && j + run > numExSyms)) {
+            error(errSyntaxError, curStr->getPos(), "Too many exported symbols in JBIG2 symbol dictionary");
+            for (; j < numExSyms; ++j)
+                symbolDict->setBitmap(j, nullptr);
+            delete symbolDict;
+            goto syntaxError;
         }
-        if (likely(collBitmap != nullptr)) {
-            x = 0;
-            for (; j < i; ++j) {
-                bitmaps[numInputSyms + j] = collBitmap->getSlice(x, 0, symWidths[j], symHeight);
-                x += symWidths[j];
+        if (ex) {
+            for (cnt = 0; cnt < run; ++cnt) {
+                symbolDict->setBitmap(j++, new JBIG2Bitmap(bitmaps[i++]));
             }
-            delete collBitmap;
         } else {
-            error(errSyntaxError, curStr->getPos(), "collBitmap was null");
-            goto syntaxError;
+            i += run;
         }
+        ex = !ex;
     }
-}
-
-// create the symbol dict object
-symbolDict = new JBIG2SymbolDict(segNum, numExSyms);
-if (!symbolDict->isOk()) {
-    delete symbolDict;
-    goto syntaxError;
-}
-
-// exported symbol list
-i = j = 0;
-ex = false;
-run = 0; // initialize it once in case the first decodeInt fails
-         // we do not want to use uninitialized memory
-while (i < numInputSyms + numNewSyms) {
-    if (huff) {
-        huffDecoder->decodeInt(&run, huffTableA);
-    } else {
-        arithDecoder->decodeInt(&run, iaexStats);
-    }
-    if (i + run > numInputSyms + numNewSyms || (ex && j + run > numExSyms)) {
-        error(errSyntaxError, curStr->getPos(), "Too many exported symbols in JBIG2 symbol dictionary");
+    if (j != numExSyms) {
+        error(errSyntaxError, curStr->getPos(), "Too few symbols in JBIG2 symbol dictionary");
         for (; j < numExSyms; ++j)
             symbolDict->setBitmap(j, nullptr);
         delete symbolDict;
         goto syntaxError;
     }
-    if (ex) {
-        for (cnt = 0; cnt < run; ++cnt) {
-            symbolDict->setBitmap(j++, new JBIG2Bitmap(bitmaps[i++]));
-        }
-    } else {
-        i += run;
-    }
-    ex = !ex;
-}
-if (j != numExSyms) {
-    error(errSyntaxError, curStr->getPos(), "Too few symbols in JBIG2 symbol dictionary");
-    for (; j < numExSyms; ++j)
-        symbolDict->setBitmap(j, nullptr);
-    delete symbolDict;
-    goto syntaxError;
-}
 
-for (i = 0; i < numNewSyms; ++i) {
-    delete bitmaps[numInputSyms + i];
-}
-gfree(bitmaps);
-if (symWidths) {
-    gfree(symWidths);
-}
+    for (i = 0; i < numNewSyms; ++i) {
+        delete bitmaps[numInputSyms + i];
+    }
+    gfree(bitmaps);
+    if (symWidths) {
+        gfree(symWidths);
+    }
 
-// save the arithmetic decoder stats
-if (!huff && contextRetained) {
-    symbolDict->setGenericRegionStats(genericRegionStats->copy());
-    if (refAgg) {
-        symbolDict->setRefinementRegionStats(refinementRegionStats->copy());
+    // save the arithmetic decoder stats
+    if (!huff && contextRetained) {
+        symbolDict->setGenericRegionStats(genericRegionStats->copy());
+        if (refAgg) {
+            symbolDict->setRefinementRegionStats(refinementRegionStats->copy());
+        }
     }
-}
 
-// store the new symbol dict
-segments->push_back(symbolDict);
+    // store the new symbol dict
+    segments->push_back(symbolDict);
 
-return true;
+    return true;
 
-codeTableError : error(errSyntaxError, curStr->getPos(), "Missing code table in JBIG2 symbol dictionary");
-delete codeTables;
+codeTableError:
+    error(errSyntaxError, curStr->getPos(), "Missing code table in JBIG2 symbol dictionary");
+    delete codeTables;
 
-syntaxError : for (i = 0; i < numNewSyms; ++i)
-{
-    if (bitmaps[numInputSyms + i]) {
-        delete bitmaps[numInputSyms + i];
+syntaxError:
+    for (i = 0; i < numNewSyms; ++i) {
+        if (bitmaps[numInputSyms + i]) {
+            delete bitmaps[numInputSyms + i];
+        }
     }
-}
-gfree(bitmaps);
-if (symWidths) {
-    gfree(symWidths);
-}
-return false;
+    gfree(bitmaps);
+    if (symWidths) {
+        gfree(symWidths);
+    }
+    return false;
 
-eofError : error(errSyntaxError, curStr->getPos(), "Unexpected EOF in JBIG2 stream");
-return false;
+eofError:
+    error(errSyntaxError, curStr->getPos(), "Unexpected EOF in JBIG2 stream");
+    return false;
 }
 
 void JBIG2Stream::readTextRegionSeg(unsigned int segNum, bool imm, bool lossless, unsigned int length, unsigned int *refSegs, unsigned int nRefSegs)


More information about the poppler mailing list