~ubuntu-branches/ubuntu/lucid/xpdf/lucid-updates

« back to all changes in this revision

Viewing changes to xpdf/JBIG2Stream.cc

  • Committer: Bazaar Package Importer
  • Author(s): Andy Price
  • Date: 2007-05-17 22:04:33 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20070517220433-gzcx2lrvllkbl7mr
Tags: 3.02-1ubuntu1
* Merge from Debian unstable (LP: #113365), remaining changes:
  - Added back 09_xpdfrc_manpage.dpatch (LP #71753)
  - Set Ubuntu maintainer

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
#endif
14
14
 
15
15
#include <stdlib.h>
 
16
#include <limits.h>
16
17
#include "GList.h"
17
18
#include "Error.h"
18
19
#include "JArithmeticDecoder.h"
681
682
  w = wA;
682
683
  h = hA;
683
684
  line = (wA + 7) >> 3;
 
685
  if (w <= 0 || h <= 0 || line <= 0 || h >= (INT_MAX - 1) / line) {
 
686
    data = NULL;
 
687
    return;
 
688
  }
684
689
  // need to allocate one extra guard byte for use in combine()
685
690
  data = (Guchar *)gmalloc(h * line + 1);
686
691
  data[h * line] = 0;
692
697
  w = bitmap->w;
693
698
  h = bitmap->h;
694
699
  line = bitmap->line;
 
700
  if (w <= 0 || h <= 0 || line <= 0 || h >= (INT_MAX - 1) / line) {
 
701
    data = NULL;
 
702
    return;
 
703
  }
695
704
  // need to allocate one extra guard byte for use in combine()
696
705
  data = (Guchar *)gmalloc(h * line + 1);
697
706
  memcpy(data, bitmap->data, h * line);
720
729
}
721
730
 
722
731
void JBIG2Bitmap::expand(int newH, Guint pixel) {
723
 
  if (newH <= h) {
 
732
  if (newH <= h || line <= 0 || newH >= (INT_MAX - 1) / line) {
724
733
    return;
725
734
  }
726
735
  // need to allocate one extra guard byte for use in combine()
1090
1099
// JBIG2Stream
1091
1100
//------------------------------------------------------------------------
1092
1101
 
1093
 
JBIG2Stream::JBIG2Stream(Stream *strA, Object *globalsStream):
 
1102
JBIG2Stream::JBIG2Stream(Stream *strA, Object *globalsStreamA):
1094
1103
  FilterStream(strA)
1095
1104
{
1096
1105
  pageBitmap = NULL;
1115
1124
  huffDecoder = new JBIG2HuffmanDecoder();
1116
1125
  mmrDecoder = new JBIG2MMRDecoder();
1117
1126
 
1118
 
  segments = globalSegments = new GList();
1119
 
  if (globalsStream->isStream()) {
1120
 
    curStr = globalsStream->getStream();
1121
 
    curStr->reset();
1122
 
    arithDecoder->setStream(curStr);
1123
 
    huffDecoder->setStream(curStr);
1124
 
    mmrDecoder->setStream(curStr);
1125
 
    readSegments();
1126
 
  }
1127
 
 
1128
 
  segments = NULL;
 
1127
  globalsStreamA->copy(&globalsStream);
 
1128
  segments = globalSegments = NULL;
1129
1129
  curStr = NULL;
1130
1130
  dataPtr = dataEnd = NULL;
1131
1131
}
1132
1132
 
1133
1133
JBIG2Stream::~JBIG2Stream() {
 
1134
  close();
 
1135
  globalsStream.free();
1134
1136
  delete arithDecoder;
1135
1137
  delete genericRegionStats;
1136
1138
  delete refinementRegionStats;
1150
1152
  delete iaidStats;
1151
1153
  delete huffDecoder;
1152
1154
  delete mmrDecoder;
1153
 
  if (pageBitmap) {
1154
 
    delete pageBitmap;
1155
 
  }
1156
 
  if (segments) {
1157
 
    deleteGList(segments, JBIG2Segment);
1158
 
  }
1159
 
  if (globalSegments) {
1160
 
    deleteGList(globalSegments, JBIG2Segment);
1161
 
  }
1162
1155
  delete str;
1163
1156
}
1164
1157
 
1165
1158
void JBIG2Stream::reset() {
1166
 
  if (pageBitmap) {
1167
 
    delete pageBitmap;
1168
 
    pageBitmap = NULL;
1169
 
  }
1170
 
  if (segments) {
1171
 
    deleteGList(segments, JBIG2Segment);
1172
 
  }
 
1159
  // read the globals stream
 
1160
  globalSegments = new GList();
 
1161
  if (globalsStream.isStream()) {
 
1162
    segments = globalSegments;
 
1163
    curStr = globalsStream.getStream();
 
1164
    curStr->reset();
 
1165
    arithDecoder->setStream(curStr);
 
1166
    huffDecoder->setStream(curStr);
 
1167
    mmrDecoder->setStream(curStr);
 
1168
    readSegments();
 
1169
    curStr->close();
 
1170
  }
 
1171
 
 
1172
  // read the main stream
1173
1173
  segments = new GList();
1174
 
 
1175
1174
  curStr = str;
1176
1175
  curStr->reset();
1177
1176
  arithDecoder->setStream(curStr);
1183
1182
    dataPtr = pageBitmap->getDataPtr();
1184
1183
    dataEnd = dataPtr + pageBitmap->getDataSize();
1185
1184
  } else {
1186
 
    dataPtr = NULL;
1187
 
  }
 
1185
    dataPtr = dataEnd = NULL;
 
1186
  }
 
1187
}
 
1188
 
 
1189
void JBIG2Stream::close() {
 
1190
  if (pageBitmap) {
 
1191
    delete pageBitmap;
 
1192
    pageBitmap = NULL;
 
1193
  }
 
1194
  if (segments) {
 
1195
    deleteGList(segments, JBIG2Segment);
 
1196
    segments = NULL;
 
1197
  }
 
1198
  if (globalSegments) {
 
1199
    deleteGList(globalSegments, JBIG2Segment);
 
1200
    globalSegments = NULL;
 
1201
  }
 
1202
  dataPtr = dataEnd = NULL;
 
1203
  FilterStream::close();
1188
1204
}
1189
1205
 
1190
1206
int JBIG2Stream::getChar() {
2294
2310
      !readUWord(&stepX) || !readUWord(&stepY)) {
2295
2311
    goto eofError;
2296
2312
  }
 
2313
  if (w == 0 || h == 0 || w >= INT_MAX / h) {
 
2314
    error(getPos(), "Bad bitmap size in JBIG2 halftone segment");
 
2315
    return;
 
2316
  }
 
2317
  if (gridH == 0 || gridW >= INT_MAX / gridH) {
 
2318
    error(getPos(), "Bad grid size in JBIG2 halftone segment");
 
2319
    return;
 
2320
  }
2297
2321
 
2298
2322
  // get pattern dictionary
2299
2323
  if (nRefSegs != 1) {
2335
2359
    skipBitmap = new JBIG2Bitmap(0, gridW, gridH);
2336
2360
    skipBitmap->clearToZero();
2337
2361
    for (m = 0; m < gridH; ++m) {
2338
 
      xx = gridX + m * stepY;
2339
 
      yy = gridY + m * stepX;
2340
2362
      for (n = 0; n < gridW; ++n) {
 
2363
        xx = gridX + m * stepY + n * stepX;
 
2364
        yy = gridY + m * stepX - n * stepY;
2341
2365
        if (((xx + (int)patW) >> 8) <= 0 || (xx >> 8) >= (int)w ||
2342
2366
            ((yy + (int)patH) >> 8) <= 0 || (yy >> 8) >= (int)h) {
2343
2367
          skipBitmap->setPixel(n, m);
2384
2408
  }
2385
2409
 
2386
2410
  gfree(grayImg);
 
2411
  if (skipBitmap) {
 
2412
    delete skipBitmap;
 
2413
  }
2387
2414
 
2388
2415
  // combine the region bitmap into the page bitmap
2389
2416
  if (imm) {
3314
3341
  iardwStats->reset();
3315
3342
  iardhStats->reset();
3316
3343
  iariStats->reset();
3317
 
  if (iaidStats->getContextSize() == symCodeLen + 1) {
 
3344
  if (iaidStats->getContextSize() == 1 << (symCodeLen + 1)) {
3318
3345
    iaidStats->reset();
3319
3346
  } else {
3320
3347
    delete iaidStats;