summaryrefslogtreecommitdiff
path: root/Build/source/libs/xpdf/xpdf-src/xpdf/JBIG2Stream.cc
diff options
context:
space:
mode:
Diffstat (limited to 'Build/source/libs/xpdf/xpdf-src/xpdf/JBIG2Stream.cc')
-rw-r--r--Build/source/libs/xpdf/xpdf-src/xpdf/JBIG2Stream.cc93
1 files changed, 60 insertions, 33 deletions
diff --git a/Build/source/libs/xpdf/xpdf-src/xpdf/JBIG2Stream.cc b/Build/source/libs/xpdf/xpdf-src/xpdf/JBIG2Stream.cc
index 4c4e5183026..02450b4f816 100644
--- a/Build/source/libs/xpdf/xpdf-src/xpdf/JBIG2Stream.cc
+++ b/Build/source/libs/xpdf/xpdf-src/xpdf/JBIG2Stream.cc
@@ -1147,6 +1147,7 @@ JBIG2CodeTable::~JBIG2CodeTable() {
JBIG2Stream::JBIG2Stream(Stream *strA, Object *globalsStreamA):
FilterStream(strA)
{
+ decoded = gFalse;
pageBitmap = NULL;
arithDecoder = new JArithmeticDecoder();
@@ -1205,40 +1206,9 @@ Stream *JBIG2Stream::copy() {
}
void JBIG2Stream::reset() {
- GList *t;
-
segments = new GList();
globalSegments = new GList();
-
- // read the globals stream
- if (globalsStream.isStream()) {
- curStr = globalsStream.getStream();
- curStr->reset();
- arithDecoder->setStream(curStr);
- huffDecoder->setStream(curStr);
- mmrDecoder->setStream(curStr);
- readSegments();
- curStr->close();
- // swap the newly read segments list into globalSegments
- t = segments;
- segments = globalSegments;
- globalSegments = t;
- }
-
- // read the main stream
- curStr = str;
- curStr->reset();
- arithDecoder->setStream(curStr);
- huffDecoder->setStream(curStr);
- mmrDecoder->setStream(curStr);
- readSegments();
-
- if (pageBitmap) {
- dataPtr = pageBitmap->getDataPtr();
- dataEnd = dataPtr + pageBitmap->getDataSize();
- } else {
- dataPtr = dataEnd = NULL;
- }
+ decoded = gFalse;
}
void JBIG2Stream::close() {
@@ -1259,6 +1229,9 @@ void JBIG2Stream::close() {
}
int JBIG2Stream::getChar() {
+ if (!decoded) {
+ decodeImage();
+ }
if (dataPtr && dataPtr < dataEnd) {
return (*dataPtr++ ^ 0xff) & 0xff;
}
@@ -1266,6 +1239,9 @@ int JBIG2Stream::getChar() {
}
int JBIG2Stream::lookChar() {
+ if (!decoded) {
+ decodeImage();
+ }
if (dataPtr && dataPtr < dataEnd) {
return (*dataPtr ^ 0xff) & 0xff;
}
@@ -1275,6 +1251,9 @@ int JBIG2Stream::lookChar() {
int JBIG2Stream::getBlock(char *blk, int size) {
int n, i;
+ if (!decoded) {
+ decodeImage();
+ }
if (size <= 0) {
return 0;
}
@@ -1298,6 +1277,42 @@ GBool JBIG2Stream::isBinary(GBool last) {
return str->isBinary(gTrue);
}
+void JBIG2Stream::decodeImage() {
+ GList *t;
+
+ // read the globals stream
+ if (globalsStream.isStream()) {
+ curStr = globalsStream.getStream();
+ curStr->reset();
+ arithDecoder->setStream(curStr);
+ huffDecoder->setStream(curStr);
+ mmrDecoder->setStream(curStr);
+ readSegments();
+ curStr->close();
+ // swap the newly read segments list into globalSegments
+ t = segments;
+ segments = globalSegments;
+ globalSegments = t;
+ }
+
+ // read the main stream
+ curStr = str;
+ curStr->reset();
+ arithDecoder->setStream(curStr);
+ huffDecoder->setStream(curStr);
+ mmrDecoder->setStream(curStr);
+ readSegments();
+
+ if (pageBitmap) {
+ dataPtr = pageBitmap->getDataPtr();
+ dataEnd = dataPtr + pageBitmap->getDataSize();
+ } else {
+ dataPtr = dataEnd = NULL;
+ }
+
+ decoded = gTrue;
+}
+
void JBIG2Stream::readSegments() {
Guint segNum, segFlags, segType, page, segLength;
Guint refFlags, nRefSegs;
@@ -2042,7 +2057,14 @@ void JBIG2Stream::readTextRegionSeg(Guint segNum, GBool imm,
for (i = 0; i < nRefSegs; ++i) {
if ((seg = findSegment(refSegs[i]))) {
if (seg->getType() == jbig2SegSymbolDict) {
- numSyms += ((JBIG2SymbolDict *)seg)->getSize();
+ Guint segSize = ((JBIG2SymbolDict *)seg)->getSize();
+ if (segSize > INT_MAX || numSyms > INT_MAX - segSize) {
+ error(errSyntaxError, getPos(),
+ "Too many symbols in JBIG2 text region");
+ delete codeTables;
+ return;
+ }
+ numSyms += segSize;
} else if (seg->getType() == jbig2SegCodeTable) {
codeTables->append(seg);
}
@@ -3933,6 +3955,11 @@ void JBIG2Stream::readPageInfoSeg(Guint length) {
pageDefPixel = (flags >> 2) & 1;
defCombOp = (flags >> 3) & 3;
+ // this will only happen if there are multiple page info segments
+ if (pageBitmap) {
+ delete pageBitmap;
+ }
+
// allocate the page bitmap
if (pageH == 0xffffffff) {
curPageH = striping & 0x7fff;