+-JBIG2HuffmanTable huffTableC[] = {
++static JBIG2HuffmanTable huffTableC[] = {
+ { 0, 1, 0, 0x000 },
+ { 1, 2, 0, 0x002 },
+ { 2, 3, 0, 0x006 },
+@@ -74,7 +91,7 @@
+ { 0, 0, jbig2HuffmanEOT, 0 }
+ };
+
+-JBIG2HuffmanTable huffTableD[] = {
++static JBIG2HuffmanTable huffTableD[] = {
+ { 1, 1, 0, 0x000 },
+ { 2, 2, 0, 0x002 },
+ { 3, 3, 0, 0x006 },
+@@ -84,7 +101,7 @@
+ { 0, 0, jbig2HuffmanEOT, 0 }
+ };
+
+-JBIG2HuffmanTable huffTableE[] = {
++static JBIG2HuffmanTable huffTableE[] = {
+ { 1, 1, 0, 0x000 },
+ { 2, 2, 0, 0x002 },
+ { 3, 3, 0, 0x006 },
+@@ -96,7 +113,7 @@
+ { 0, 0, jbig2HuffmanEOT, 0 }
+ };
+
+-JBIG2HuffmanTable huffTableF[] = {
++static JBIG2HuffmanTable huffTableF[] = {
+ { 0, 2, 7, 0x000 },
+ { 128, 3, 7, 0x002 },
+ { 256, 3, 8, 0x003 },
+@@ -114,7 +131,7 @@
+ { 0, 0, jbig2HuffmanEOT, 0 }
+ };
+
+-JBIG2HuffmanTable huffTableG[] = {
++static JBIG2HuffmanTable huffTableG[] = {
+ { -512, 3, 8, 0x000 },
+ { 256, 3, 8, 0x001 },
+ { 512, 3, 9, 0x002 },
+@@ -133,7 +150,7 @@
+ { 0, 0, jbig2HuffmanEOT, 0 }
+ };
+
+-JBIG2HuffmanTable huffTableH[] = {
++static JBIG2HuffmanTable huffTableH[] = {
+ { 0, 2, 1, 0x000 },
+ { 0, 2, jbig2HuffmanOOB, 0x001 },
+ { 4, 3, 4, 0x004 },
+@@ -158,7 +175,7 @@
+ { 0, 0, jbig2HuffmanEOT, 0 }
+ };
+
+-JBIG2HuffmanTable huffTableI[] = {
++static JBIG2HuffmanTable huffTableI[] = {
+ { 0, 2, jbig2HuffmanOOB, 0x000 },
+ { -1, 3, 1, 0x002 },
+ { 1, 3, 1, 0x003 },
+@@ -184,7 +201,7 @@
+ { 0, 0, jbig2HuffmanEOT, 0 }
+ };
+
+-JBIG2HuffmanTable huffTableJ[] = {
++static JBIG2HuffmanTable huffTableJ[] = {
+ { -2, 2, 2, 0x000 },
+ { 6, 2, 6, 0x001 },
+ { 0, 2, jbig2HuffmanOOB, 0x002 },
+@@ -209,7 +226,7 @@
+ { 0, 0, jbig2HuffmanEOT, 0 }
+ };
+
+-JBIG2HuffmanTable huffTableK[] = {
++static JBIG2HuffmanTable huffTableK[] = {
+ { 1, 1, 0, 0x000 },
+ { 2, 2, 1, 0x002 },
+ { 4, 4, 0, 0x00c },
+@@ -226,7 +243,7 @@
+ { 0, 0, jbig2HuffmanEOT, 0 }
+ };
+
+-JBIG2HuffmanTable huffTableL[] = {
++static JBIG2HuffmanTable huffTableL[] = {
+ { 1, 1, 0, 0x000 },
+ { 2, 2, 0, 0x002 },
+ { 3, 3, 1, 0x006 },
+@@ -243,7 +260,7 @@
+ { 0, 0, jbig2HuffmanEOT, 0 }
+ };
+
+-JBIG2HuffmanTable huffTableM[] = {
++static JBIG2HuffmanTable huffTableM[] = {
+ { 1, 1, 0, 0x000 },
+ { 2, 3, 0, 0x004 },
+ { 7, 3, 3, 0x005 },
+@@ -260,7 +277,7 @@
+ { 0, 0, jbig2HuffmanEOT, 0 }
+ };
+
+-JBIG2HuffmanTable huffTableN[] = {
++static JBIG2HuffmanTable huffTableN[] = {
+ { 0, 1, 0, 0x000 },
+ { -2, 3, 0, 0x004 },
+ { -1, 3, 0, 0x005 },
+@@ -269,7 +286,7 @@
+ { 0, 0, jbig2HuffmanEOT, 0 }
+ };
+
+-JBIG2HuffmanTable huffTableO[] = {
++static JBIG2HuffmanTable huffTableO[] = {
+ { 0, 1, 0, 0x000 },
+ { -1, 3, 0, 0x004 },
+ { 1, 3, 0, 0x005 },
+@@ -473,7 +490,7 @@
+ }
+
+ int JBIG2MMRDecoder::get2DCode() {
+- CCITTCode *p;
++ const CCITTCode *p;
+
+ if (bufLen == 0) {
+ buf = str->getChar() & 0xff;
+@@ -500,7 +517,7 @@
+ }
+
+ int JBIG2MMRDecoder::getWhiteCode() {
+- CCITTCode *p;
++ const CCITTCode *p;
+ Guint code;
+
+ if (bufLen == 0) {
+@@ -543,7 +560,7 @@
+ }
+
+ int JBIG2MMRDecoder::getBlackCode() {
+- CCITTCode *p;
++ const CCITTCode *p;
+ Guint code;
+
+ if (bufLen == 0) {
+@@ -670,6 +687,7 @@
+ void combine(JBIG2Bitmap *bitmap, int x, int y, Guint combOp);
+ Guchar *getDataPtr() { return data; }
+ int getDataSize() { return h * line; }
++ GBool isOk() { return data != NULL; }
+
+ private:
+
+@@ -685,10 +703,11 @@
+ w = wA;
+ h = hA;
+ line = (wA + 7) >> 3;
++
+ if (w <= 0 || h <= 0 || line <= 0 || h >= (INT_MAX - 1) / line) {
+- // force a call to gmalloc(-1), which will throw an exception
+- h = -1;
+- line = 2;
++ error(-1, "invalid width/height");
++ data = NULL;
++ return;
+ }
+ // need to allocate one extra guard byte for use in combine()
+ data = (Guchar *)gmalloc(h * line + 1);
+@@ -701,10 +720,11 @@
+ w = bitmap->w;
+ h = bitmap->h;
+ line = bitmap->line;
++
+ if (w <= 0 || h <= 0 || line <= 0 || h >= (INT_MAX - 1) / line) {
+- // force a call to gmalloc(-1), which will throw an exception
+- h = -1;
+- line = 2;
++ error(-1, "invalid width/height");
++ data = NULL;
++ return;
+ }
+ // need to allocate one extra guard byte for use in combine()
+ data = (Guchar *)gmalloc(h * line + 1);
+@@ -735,6 +755,9 @@
+
+ void JBIG2Bitmap::expand(int newH, Guint pixel) {
+ if (newH <= h || line <= 0 || newH >= (INT_MAX - 1) / line) {
++ error(-1, "invalid width/height");
++ gfree(data);
++ data = NULL;
+ return;
+ }
+ // need to allocate one extra guard byte for use in combine()
+@@ -1002,6 +1025,7 @@
+ Guint getSize() { return size; }
+ void setBitmap(Guint idx, JBIG2Bitmap *bitmap) { bitmaps[idx] = bitmap; }
+ JBIG2Bitmap *getBitmap(Guint idx) { return bitmaps[idx]; }
++ GBool isOk() { return bitmaps != NULL; }
+ void setGenericRegionStats(JArithmeticDecoderStats *stats)
+ { genericRegionStats = stats; }
+ void setRefinementRegionStats(JArithmeticDecoderStats *stats)
+@@ -1022,13 +1046,9 @@
+ JBIG2SymbolDict::JBIG2SymbolDict(Guint segNumA, Guint sizeA):
+ JBIG2Segment(segNumA)
+ {
+- Guint i;
+-
+ size = sizeA;
+ bitmaps = (JBIG2Bitmap **)gmallocn(size, sizeof(JBIG2Bitmap *));
+- for (i = 0; i < size; ++i) {
+- bitmaps[i] = NULL;
+- }
++ if (!bitmaps) size = 0;
+ genericRegionStats = NULL;
+ refinementRegionStats = NULL;
+ }
+@@ -1037,9 +1057,7 @@
+ Guint i;
+
+ for (i = 0; i < size; ++i) {
+- if (bitmaps[i]) {
+- delete bitmaps[i];
+- }
++ delete bitmaps[i];