From 6d50525f57243da7b7238560538a901b2c336919 Mon Sep 17 00:00:00 2001 From: Albert Astals Cid Date: Wed, 5 Apr 2006 18:15:15 +0000 Subject: [PATCH] Some fix for JBIG2 PDF stolen from poppler ;-) Fixes http://ia311040.us.archive.org/~rkumar/test1_opt.pdf for instance svn path=/branches/KDE/3.5/kdegraphics/kpdf/; revision=526835 --- xpdf/xpdf/JBIG2Stream.cc | 43 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/xpdf/xpdf/JBIG2Stream.cc b/xpdf/xpdf/JBIG2Stream.cc index 0b2e13c8d..a03aff770 100644 --- a/xpdf/xpdf/JBIG2Stream.cc +++ b/xpdf/xpdf/JBIG2Stream.cc @@ -1227,6 +1227,7 @@ void JBIG2Stream::readSegments() { Guint segNum, segFlags, segType, page, segLength; Guint refFlags, nRefSegs; Guint *refSegs; + int segDataPos; int c1, c2, c3; Guint i; @@ -1294,6 +1295,9 @@ void JBIG2Stream::readSegments() { goto eofError2; } + // keep track of the start of the segment data + segDataPos = getPos(); + // read the segment data switch (segType) { case 0: @@ -1371,6 +1375,45 @@ void JBIG2Stream::readSegments() { break; } + // Make sure the segment handler read all of the bytes in the + // segment data, unless this segment is marked as having an + // unknown length (section 7.2.7 of the JBIG2 Final Committee Draft) + + if (segLength != 0xffffffff) { + + int segExtraBytes = segDataPos + segLength - getPos(); + if (segExtraBytes > 0) { + + // If we didn't read all of the bytes in the segment data, + // indicate an error, and throw away the rest of the data. + + // v.3.1.01.13 of the LuraTech PDF Compressor Server will + // sometimes generate an extraneous NULL byte at the end of + // arithmetic-coded symbol dictionary segments when numNewSyms + // == 0. Segments like this often occur for blank pages. + + error(getPos(), "%d extraneous byte%s after segment", + segExtraBytes, (segExtraBytes > 1) ? "s" : ""); + + // Burn through the remaining bytes -- inefficient, but + // hopefully we're not doing this much + + int trash; + for (int i = segExtraBytes; i > 0; i--) { + readByte(&trash); + } + + } else if (segExtraBytes < 0) { + + // If we read more bytes than we should have, according to the + // segment length field, note an error. + + error(getPos(), "Previous segment handler read too many bytes"); + + } + + } + gfree(refSegs); }