~ubuntu-branches/ubuntu/lucid/swftools/lucid

« back to all changes in this revision

Viewing changes to lib/pdf/xpdf/Stream.cc

  • Committer: Bazaar Package Importer
  • Author(s): Fabrice Coutadeur
  • Date: 2009-04-30 05:22:19 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090430052219-l1n64qofzeq5pej8
Tags: 0.9.0-0ubuntu1
* New upstream release (LP: #369931)
  - patches/01_manpages: edited to match updated version of src/pdf2swf.1 and
    src/wav2swf.1
  - patches/02_faq: edited to match updated version of FAQ
  - patches/04_makefile: edited to delete the patch on lib/Makefile.in and 
    src/Makefile.in (applied upstream)
  - deleted patch 99_configure_for_python2.5_and_2.6 (applied upstream)
  - debian/swftools.doc: deleted installation of TODO and 
    pdf2swf/HOWTO_pdf2swf as they don't exist anymore

Show diffs side-by-side

added added

removed removed

Lines of Context:
412
412
  ok = gFalse;
413
413
 
414
414
  nVals = width * nComps;
415
 
  if (width <= 0 || nComps <= 0 || nBits <= 0 ||
416
 
      nComps >= INT_MAX / nBits ||
417
 
      width >= INT_MAX / nComps / nBits ||
418
 
      nVals * nBits + 7 < 0) {
419
 
    return;
420
 
  }
421
415
  pixBytes = (nComps * nBits + 7) >> 3;
422
416
  rowBytes = ((nVals * nBits + 7) >> 3) + pixBytes;
423
 
  if (rowBytes <= 0) {
 
417
  if (width <= 0 || nComps <= 0 || nBits <= 0 ||
 
418
      nComps > gfxColorMaxComps ||
 
419
      nBits > 16 ||
 
420
      width >= INT_MAX / nComps ||      // check for overflow in nVals 
 
421
      nVals >= (INT_MAX - 7) / nBits) { // check for overflow in rowBytes
424
422
    return;
425
423
  }
426
424
  predLine = (Guchar *)gmalloc(rowBytes);
1247
1245
  columns = columnsA;
1248
1246
  if (columns < 1) {
1249
1247
    columns = 1;
1250
 
  }
1251
 
  if (columns + 4 <= 0) {
1252
 
    columns = INT_MAX - 4;
 
1248
  } else if (columns > INT_MAX - 2) {
 
1249
    columns = INT_MAX - 2;
1253
1250
  }
1254
1251
  rows = rowsA;
1255
1252
  endOfBlock = endOfBlockA;
1256
1253
  black = blackA;
1257
 
  refLine = (short *)gmallocn(columns + 3, sizeof(short));
1258
 
  codingLine = (short *)gmallocn(columns + 2, sizeof(short));
 
1254
  // 0 <= codingLine[0] < codingLine[1] < ... < codingLine[n] = columns
 
1255
  // ---> max codingLine size = columns + 1
 
1256
  // refLine has one extra guard entry at the end
 
1257
  // ---> max refLine size = columns + 2
 
1258
  codingLine = (int *)gmallocn(columns + 1, sizeof(int));
 
1259
  refLine = (int *)gmallocn(columns + 2, sizeof(int));
1259
1260
 
1260
1261
  eof = gFalse;
1261
1262
  row = 0;
1262
1263
  nextLine2D = encoding < 0;
1263
1264
  inputBits = 0;
1264
 
  codingLine[0] = 0;
1265
 
  codingLine[1] = refLine[2] = columns;
1266
 
  a0 = 1;
 
1265
  codingLine[0] = columns;
 
1266
  a0i = 0;
 
1267
  outputBits = 0;
1267
1268
 
1268
1269
  buf = EOF;
1269
1270
}
1282
1283
  row = 0;
1283
1284
  nextLine2D = encoding < 0;
1284
1285
  inputBits = 0;
1285
 
  codingLine[0] = 0;
1286
 
  codingLine[1] = columns;
1287
 
  a0 = 1;
 
1286
  codingLine[0] = columns;
 
1287
  a0i = 0;
 
1288
  outputBits = 0;
1288
1289
  buf = EOF;
1289
1290
 
1290
1291
  // skip any initial zero bits and end-of-line marker, and get the 2D
1301
1302
  }
1302
1303
}
1303
1304
 
 
1305
inline void CCITTFaxStream::addPixels(int a1, int blackPixels) {
 
1306
  if (a1 > codingLine[a0i]) {
 
1307
    if (a1 > columns) {
 
1308
      error(getPos(), "CCITTFax row is wrong length (%d)", a1);
 
1309
      err = gTrue;
 
1310
      a1 = columns;
 
1311
    }
 
1312
    if ((a0i & 1) ^ blackPixels) {
 
1313
      ++a0i;
 
1314
    }
 
1315
    codingLine[a0i] = a1;
 
1316
  }
 
1317
}
 
1318
 
 
1319
inline void CCITTFaxStream::addPixelsNeg(int a1, int blackPixels) {
 
1320
  if (a1 > codingLine[a0i]) {
 
1321
    if (a1 > columns) {
 
1322
      error(getPos(), "CCITTFax row is wrong length (%d)", a1);
 
1323
      err = gTrue;
 
1324
      a1 = columns;
 
1325
    }
 
1326
    if ((a0i & 1) ^ blackPixels) {
 
1327
      ++a0i;
 
1328
    }
 
1329
    codingLine[a0i] = a1;
 
1330
  } else if (a1 < codingLine[a0i]) {
 
1331
    if (a1 < 0) {
 
1332
      error(getPos(), "Invalid CCITTFax code");
 
1333
      err = gTrue;
 
1334
      a1 = 0;
 
1335
    }
 
1336
    while (a0i > 0 && a1 <= codingLine[a0i - 1]) {
 
1337
      --a0i;
 
1338
    }
 
1339
    codingLine[a0i] = a1;
 
1340
  }
 
1341
}
 
1342
 
1304
1343
int CCITTFaxStream::lookChar() {
1305
1344
  short code1, code2, code3;
1306
 
  int a0New;
1307
 
  GBool err, gotEOL;
1308
 
  int ret;
1309
 
  int bits, i;
 
1345
  int b1i, blackPixels, i, bits;
 
1346
  GBool gotEOL;
1310
1347
 
1311
 
  // if at eof just return EOF
1312
 
  if (eof && codingLine[a0] >= columns) {
1313
 
    return EOF;
 
1348
  if (buf != EOF) {
 
1349
    return buf;
1314
1350
  }
1315
1351
 
1316
1352
  // read the next row
1317
 
  err = gFalse;
1318
 
  if (codingLine[a0] >= columns) {
 
1353
  if (outputBits == 0) {
 
1354
 
 
1355
    // if at eof just return EOF
 
1356
    if (eof) {
 
1357
      return EOF;
 
1358
    }
 
1359
 
 
1360
    err = gFalse;
1319
1361
 
1320
1362
    // 2-D encoding
1321
1363
    if (nextLine2D) {
1322
 
      // state:
1323
 
      //   a0New = current position in coding line (0 <= a0New <= columns)
1324
 
      //   codingLine[a0] = last change in coding line
1325
 
      //                    (black-to-white if a0 is even,
1326
 
      //                     white-to-black if a0 is odd)
1327
 
      //   refLine[b1] = next change in reference line of opposite color
1328
 
      //                 to a0
1329
 
      // invariants:
1330
 
      //   0 <= codingLine[a0] <= a0New
1331
 
      //           <= refLine[b1] <= refLine[b1+1] <= columns
1332
 
      //   0 <= a0 <= columns+1
1333
 
      //   refLine[0] = 0
1334
 
      //   refLine[n] = refLine[n+1] = columns
1335
 
      //     -- for some 1 <= n <= columns+1
1336
 
      // end condition:
1337
 
      //   0 = codingLine[0] <= codingLine[1] < codingLine[2] < ...
1338
 
      //     < codingLine[n-1] < codingLine[n] = columns
1339
 
      //     -- where 1 <= n <= columns+1
1340
1364
      for (i = 0; codingLine[i] < columns; ++i) {
1341
1365
        refLine[i] = codingLine[i];
1342
1366
      }
1343
 
      refLine[i] = refLine[i + 1] = columns;
1344
 
      b1 = 1;
1345
 
      a0New = codingLine[a0 = 0] = 0;
1346
 
      do {
 
1367
      refLine[i++] = columns;
 
1368
      refLine[i] = columns;
 
1369
      codingLine[0] = 0;
 
1370
      a0i = 0;
 
1371
      b1i = 0;
 
1372
      blackPixels = 0;
 
1373
      // invariant:
 
1374
      // refLine[b1i-1] <= codingLine[a0i] < refLine[b1i] < refLine[b1i+1]
 
1375
      //                                                             <= columns
 
1376
      // exception at left edge:
 
1377
      //   codingLine[a0i = 0] = refLine[b1i = 0] = 0 is possible
 
1378
      // exception at right edge:
 
1379
      //   refLine[b1i] = refLine[b1i+1] = columns is possible
 
1380
      while (codingLine[a0i] < columns) {
1347
1381
        code1 = getTwoDimCode();
1348
1382
        switch (code1) {
1349
1383
        case twoDimPass:
1350
 
          if (refLine[b1] < columns) {
1351
 
            a0New = refLine[b1 + 1];
1352
 
            b1 += 2;
 
1384
          addPixels(refLine[b1i + 1], blackPixels);
 
1385
          if (refLine[b1i + 1] < columns) {
 
1386
            b1i += 2;
1353
1387
          }
1354
1388
          break;
1355
1389
        case twoDimHoriz:
1356
 
          if ((a0 & 1) == 0) {
1357
 
            code1 = code2 = 0;
 
1390
          code1 = code2 = 0;
 
1391
          if (blackPixels) {
 
1392
            do {
 
1393
              code1 += code3 = getBlackCode();
 
1394
            } while (code3 >= 64);
 
1395
            do {
 
1396
              code2 += code3 = getWhiteCode();
 
1397
            } while (code3 >= 64);
 
1398
          } else {
1358
1399
            do {
1359
1400
              code1 += code3 = getWhiteCode();
1360
1401
            } while (code3 >= 64);
1361
1402
            do {
1362
1403
              code2 += code3 = getBlackCode();
1363
1404
            } while (code3 >= 64);
1364
 
          } else {
1365
 
            code1 = code2 = 0;
1366
 
            do {
1367
 
              code1 += code3 = getBlackCode();
1368
 
            } while (code3 >= 64);
1369
 
            do {
1370
 
              code2 += code3 = getWhiteCode();
1371
 
            } while (code3 >= 64);
1372
 
          }
1373
 
          if (code1 > 0 || code2 > 0) {
1374
 
            if (a0New + code1 <= columns) {
1375
 
              codingLine[a0 + 1] = a0New + code1;
1376
 
            } else {
1377
 
              codingLine[a0 + 1] = columns;
1378
 
            }
1379
 
            ++a0;
1380
 
            if (codingLine[a0] + code2 <= columns) {
1381
 
              codingLine[a0 + 1] = codingLine[a0] + code2;
1382
 
            } else {
1383
 
              codingLine[a0 + 1] = columns;
1384
 
            }
1385
 
            ++a0;
1386
 
            a0New = codingLine[a0];
1387
 
            while (refLine[b1] <= a0New && refLine[b1] < columns) {
1388
 
              b1 += 2;
 
1405
          }
 
1406
          addPixels(codingLine[a0i] + code1, blackPixels);
 
1407
          if (codingLine[a0i] < columns) {
 
1408
            addPixels(codingLine[a0i] + code2, blackPixels ^ 1);
 
1409
          }
 
1410
          while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
 
1411
            b1i += 2;
 
1412
          }
 
1413
          break;
 
1414
        case twoDimVertR3:
 
1415
          addPixels(refLine[b1i] + 3, blackPixels);
 
1416
          blackPixels ^= 1;
 
1417
          if (codingLine[a0i] < columns) {
 
1418
            ++b1i;
 
1419
            while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
 
1420
              b1i += 2;
 
1421
            }
 
1422
          }
 
1423
          break;
 
1424
        case twoDimVertR2:
 
1425
          addPixels(refLine[b1i] + 2, blackPixels);
 
1426
          blackPixels ^= 1;
 
1427
          if (codingLine[a0i] < columns) {
 
1428
            ++b1i;
 
1429
            while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
 
1430
              b1i += 2;
 
1431
            }
 
1432
          }
 
1433
          break;
 
1434
        case twoDimVertR1:
 
1435
          addPixels(refLine[b1i] + 1, blackPixels);
 
1436
          blackPixels ^= 1;
 
1437
          if (codingLine[a0i] < columns) {
 
1438
            ++b1i;
 
1439
            while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
 
1440
              b1i += 2;
1389
1441
            }
1390
1442
          }
1391
1443
          break;
1392
1444
        case twoDimVert0:
1393
 
          if (refLine[b1] < columns) {
1394
 
            a0New = codingLine[++a0] = refLine[b1];
1395
 
            ++b1;
1396
 
            while (refLine[b1] <= a0New && refLine[b1] < columns) {
1397
 
              b1 += 2;
1398
 
            }
1399
 
          } else {
1400
 
            a0New = codingLine[++a0] = columns;
1401
 
          }
1402
 
          break;
1403
 
        case twoDimVertR1:
1404
 
          if (refLine[b1] + 1 < columns) {
1405
 
            a0New = codingLine[++a0] = refLine[b1] + 1;
1406
 
            ++b1;
1407
 
            while (refLine[b1] <= a0New && refLine[b1] < columns) {
1408
 
              b1 += 2;
1409
 
            }
1410
 
          } else {
1411
 
            a0New = codingLine[++a0] = columns;
 
1445
          addPixels(refLine[b1i], blackPixels);
 
1446
          blackPixels ^= 1;
 
1447
          if (codingLine[a0i] < columns) {
 
1448
            ++b1i;
 
1449
            while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
 
1450
              b1i += 2;
 
1451
            }
 
1452
          }
 
1453
          break;
 
1454
        case twoDimVertL3:
 
1455
          addPixelsNeg(refLine[b1i] - 3, blackPixels);
 
1456
          blackPixels ^= 1;
 
1457
          if (codingLine[a0i] < columns) {
 
1458
            if (b1i > 0) {
 
1459
              --b1i;
 
1460
            } else {
 
1461
              ++b1i;
 
1462
            }
 
1463
            while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
 
1464
              b1i += 2;
 
1465
            }
 
1466
          }
 
1467
          break;
 
1468
        case twoDimVertL2:
 
1469
          addPixelsNeg(refLine[b1i] - 2, blackPixels);
 
1470
          blackPixels ^= 1;
 
1471
          if (codingLine[a0i] < columns) {
 
1472
            if (b1i > 0) {
 
1473
              --b1i;
 
1474
            } else {
 
1475
              ++b1i;
 
1476
            }
 
1477
            while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
 
1478
              b1i += 2;
 
1479
            }
1412
1480
          }
1413
1481
          break;
1414
1482
        case twoDimVertL1:
1415
 
          if (refLine[b1] - 1 > a0New || (a0 == 0 && refLine[b1] == 1)) {
1416
 
            a0New = codingLine[++a0] = refLine[b1] - 1;
1417
 
            --b1;
1418
 
            while (refLine[b1] <= a0New && refLine[b1] < columns) {
1419
 
              b1 += 2;
1420
 
            }
1421
 
          }
1422
 
          break;
1423
 
        case twoDimVertR2:
1424
 
          if (refLine[b1] + 2 < columns) {
1425
 
            a0New = codingLine[++a0] = refLine[b1] + 2;
1426
 
            ++b1;
1427
 
            while (refLine[b1] <= a0New && refLine[b1] < columns) {
1428
 
              b1 += 2;
1429
 
            }
1430
 
          } else {
1431
 
            a0New = codingLine[++a0] = columns;
1432
 
          }
1433
 
          break;
1434
 
        case twoDimVertL2:
1435
 
          if (refLine[b1] - 2 > a0New || (a0 == 0 && refLine[b1] == 2)) {
1436
 
            a0New = codingLine[++a0] = refLine[b1] - 2;
1437
 
            --b1;
1438
 
            while (refLine[b1] <= a0New && refLine[b1] < columns) {
1439
 
              b1 += 2;
1440
 
            }
1441
 
          }
1442
 
          break;
1443
 
        case twoDimVertR3:
1444
 
          if (refLine[b1] + 3 < columns) {
1445
 
            a0New = codingLine[++a0] = refLine[b1] + 3;
1446
 
            ++b1;
1447
 
            while (refLine[b1] <= a0New && refLine[b1] < columns) {
1448
 
              b1 += 2;
1449
 
            }
1450
 
          } else {
1451
 
            a0New = codingLine[++a0] = columns;
1452
 
          }
1453
 
          break;
1454
 
        case twoDimVertL3:
1455
 
          if (refLine[b1] - 3 > a0New || (a0 == 0 && refLine[b1] == 3)) {
1456
 
            a0New = codingLine[++a0] = refLine[b1] - 3;
1457
 
            --b1;
1458
 
            while (refLine[b1] <= a0New && refLine[b1] < columns) {
1459
 
              b1 += 2;
 
1483
          addPixelsNeg(refLine[b1i] - 1, blackPixels);
 
1484
          blackPixels ^= 1;
 
1485
          if (codingLine[a0i] < columns) {
 
1486
            if (b1i > 0) {
 
1487
              --b1i;
 
1488
            } else {
 
1489
              ++b1i;
 
1490
            }
 
1491
            while (refLine[b1i] <= codingLine[a0i] && refLine[b1i] < columns) {
 
1492
              b1i += 2;
1460
1493
            }
1461
1494
          }
1462
1495
          break;
1463
1496
        case EOF:
 
1497
          addPixels(columns, 0);
1464
1498
          eof = gTrue;
1465
 
          codingLine[a0 = 0] = columns;
1466
 
          return EOF;
 
1499
          break;
1467
1500
        default:
1468
1501
          error(getPos(), "Bad 2D code %04x in CCITTFax stream", code1);
 
1502
          addPixels(columns, 0);
1469
1503
          err = gTrue;
1470
1504
          break;
1471
1505
        }
1472
 
      } while (codingLine[a0] < columns);
 
1506
      }
1473
1507
 
1474
1508
    // 1-D encoding
1475
1509
    } else {
1476
 
      codingLine[a0 = 0] = 0;
1477
 
      while (1) {
 
1510
      codingLine[0] = 0;
 
1511
      a0i = 0;
 
1512
      blackPixels = 0;
 
1513
      while (codingLine[a0i] < columns) {
1478
1514
        code1 = 0;
1479
 
        do {
1480
 
          code1 += code3 = getWhiteCode();
1481
 
        } while (code3 >= 64);
1482
 
        codingLine[a0+1] = codingLine[a0] + code1;
1483
 
        ++a0;
1484
 
        if (codingLine[a0] >= columns) {
1485
 
          break;
1486
 
        }
1487
 
        code2 = 0;
1488
 
        do {
1489
 
          code2 += code3 = getBlackCode();
1490
 
        } while (code3 >= 64);
1491
 
        codingLine[a0+1] = codingLine[a0] + code2;
1492
 
        ++a0;
1493
 
        if (codingLine[a0] >= columns) {
1494
 
          break;
1495
 
        }
1496
 
      }
1497
 
    }
1498
 
 
1499
 
    if (codingLine[a0] != columns) {
1500
 
      error(getPos(), "CCITTFax row is wrong length (%d)", codingLine[a0]);
1501
 
      // force the row to be the correct length
1502
 
      while (codingLine[a0] > columns) {
1503
 
        --a0;
1504
 
      }
1505
 
      codingLine[++a0] = columns;
1506
 
      err = gTrue;
 
1515
        if (blackPixels) {
 
1516
          do {
 
1517
            code1 += code3 = getBlackCode();
 
1518
          } while (code3 >= 64);
 
1519
        } else {
 
1520
          do {
 
1521
            code1 += code3 = getWhiteCode();
 
1522
          } while (code3 >= 64);
 
1523
        }
 
1524
        addPixels(codingLine[a0i] + code1, blackPixels);
 
1525
        blackPixels ^= 1;
 
1526
      }
1507
1527
    }
1508
1528
 
1509
1529
    // byte-align the row
1564
1584
    // this if we know the stream contains end-of-line markers because
1565
1585
    // the "just plow on" technique tends to work better otherwise
1566
1586
    } else if (err && endOfLine) {
1567
 
      do {
 
1587
      while (1) {
 
1588
        code1 = lookBits(13);
1568
1589
        if (code1 == EOF) {
1569
1590
          eof = gTrue;
1570
1591
          return EOF;
1571
1592
        }
 
1593
        if ((code1 >> 1) == 0x001) {
 
1594
          break;
 
1595
        }
1572
1596
        eatBits(1);
1573
 
        code1 = lookBits(13);
1574
 
      } while ((code1 >> 1) != 0x001);
 
1597
      }
1575
1598
      eatBits(12); 
1576
1599
      if (encoding > 0) {
1577
1600
        eatBits(1);
1579
1602
      }
1580
1603
    }
1581
1604
 
1582
 
    a0 = 0;
1583
 
    outputBits = codingLine[1] - codingLine[0];
1584
 
    if (outputBits == 0) {
1585
 
      a0 = 1;
1586
 
      outputBits = codingLine[2] - codingLine[1];
 
1605
    // set up for output
 
1606
    if (codingLine[0] > 0) {
 
1607
      outputBits = codingLine[a0i = 0];
 
1608
    } else {
 
1609
      outputBits = codingLine[a0i = 1];
1587
1610
    }
1588
1611
 
1589
1612
    ++row;
1591
1614
 
1592
1615
  // get a byte
1593
1616
  if (outputBits >= 8) {
1594
 
    ret = ((a0 & 1) == 0) ? 0xff : 0x00;
1595
 
    if ((outputBits -= 8) == 0) {
1596
 
      ++a0;
1597
 
      if (codingLine[a0] < columns) {
1598
 
        outputBits = codingLine[a0 + 1] - codingLine[a0];
1599
 
      }
 
1617
    buf = (a0i & 1) ? 0x00 : 0xff;
 
1618
    outputBits -= 8;
 
1619
    if (outputBits == 0 && codingLine[a0i] < columns) {
 
1620
      ++a0i;
 
1621
      outputBits = codingLine[a0i] - codingLine[a0i - 1];
1600
1622
    }
1601
1623
  } else {
1602
1624
    bits = 8;
1603
 
    ret = 0;
 
1625
    buf = 0;
1604
1626
    do {
1605
1627
      if (outputBits > bits) {
1606
 
        i = bits;
 
1628
        buf <<= bits;
 
1629
        if (!(a0i & 1)) {
 
1630
          buf |= 0xff >> (8 - bits);
 
1631
        }
 
1632
        outputBits -= bits;
1607
1633
        bits = 0;
1608
 
        if ((a0 & 1) == 0) {
1609
 
          ret |= 0xff >> (8 - i);
 
1634
      } else {
 
1635
        buf <<= outputBits;
 
1636
        if (!(a0i & 1)) {
 
1637
          buf |= 0xff >> (8 - outputBits);
1610
1638
        }
1611
 
        outputBits -= i;
1612
 
      } else {
1613
 
        i = outputBits;
1614
1639
        bits -= outputBits;
1615
 
        if ((a0 & 1) == 0) {
1616
 
          ret |= (0xff >> (8 - i)) << bits;
1617
 
        }
1618
1640
        outputBits = 0;
1619
 
        ++a0;
1620
 
        if (codingLine[a0] < columns) {
1621
 
          outputBits = codingLine[a0 + 1] - codingLine[a0];
 
1641
        if (codingLine[a0i] < columns) {
 
1642
          ++a0i;
 
1643
          outputBits = codingLine[a0i] - codingLine[a0i - 1];
 
1644
        } else if (bits > 0) {
 
1645
          buf <<= bits;
 
1646
          bits = 0;
1622
1647
        }
1623
1648
      }
1624
 
    } while (bits > 0 && codingLine[a0] < columns);
1625
 
  }
1626
 
  buf = black ? (ret ^ 0xff) : ret;
 
1649
    } while (bits);
 
1650
  }
 
1651
  if (black) {
 
1652
    buf ^= 0xff;
 
1653
  }
1627
1654
  return buf;
1628
1655
}
1629
1656
 
1665
1692
  code = 0; // make gcc happy
1666
1693
  if (endOfBlock) {
1667
1694
    code = lookBits(12);
 
1695
    if (code == EOF) {
 
1696
      return 1;
 
1697
    }
1668
1698
    if ((code >> 5) == 0) {
1669
1699
      p = &whiteTab1[code];
1670
1700
    } else {
1677
1707
  } else {
1678
1708
    for (n = 1; n <= 9; ++n) {
1679
1709
      code = lookBits(n);
 
1710
      if (code == EOF) {
 
1711
        return 1;
 
1712
      }
1680
1713
      if (n < 9) {
1681
1714
        code <<= 9 - n;
1682
1715
      }
1688
1721
    }
1689
1722
    for (n = 11; n <= 12; ++n) {
1690
1723
      code = lookBits(n);
 
1724
      if (code == EOF) {
 
1725
        return 1;
 
1726
      }
1691
1727
      if (n < 12) {
1692
1728
        code <<= 12 - n;
1693
1729
      }
1713
1749
  code = 0; // make gcc happy
1714
1750
  if (endOfBlock) {
1715
1751
    code = lookBits(13);
 
1752
    if (code == EOF) {
 
1753
      return 1;
 
1754
    }
1716
1755
    if ((code >> 7) == 0) {
1717
1756
      p = &blackTab1[code];
1718
 
    } else if ((code >> 9) == 0) {
 
1757
    } else if ((code >> 9) == 0 && (code >> 7) != 0) {
1719
1758
      p = &blackTab2[(code >> 1) - 64];
1720
1759
    } else {
1721
1760
      p = &blackTab3[code >> 7];
1727
1766
  } else {
1728
1767
    for (n = 2; n <= 6; ++n) {
1729
1768
      code = lookBits(n);
 
1769
      if (code == EOF) {
 
1770
        return 1;
 
1771
      }
1730
1772
      if (n < 6) {
1731
1773
        code <<= 6 - n;
1732
1774
      }
1738
1780
    }
1739
1781
    for (n = 7; n <= 12; ++n) {
1740
1782
      code = lookBits(n);
 
1783
      if (code == EOF) {
 
1784
        return 1;
 
1785
      }
1741
1786
      if (n < 12) {
1742
1787
        code <<= 12 - n;
1743
1788
      }
1751
1796
    }
1752
1797
    for (n = 10; n <= 13; ++n) {
1753
1798
      code = lookBits(n);
 
1799
      if (code == EOF) {
 
1800
        return 1;
 
1801
      }
1754
1802
      if (n < 13) {
1755
1803
        code <<= 13 - n;
1756
1804
      }
1965
2013
    // allocate a buffer for the whole image
1966
2014
    bufWidth = ((width + mcuWidth - 1) / mcuWidth) * mcuWidth;
1967
2015
    bufHeight = ((height + mcuHeight - 1) / mcuHeight) * mcuHeight;
 
2016
    if (bufWidth <= 0 || bufHeight <= 0 ||
 
2017
        bufWidth > INT_MAX / bufWidth / (int)sizeof(int)) {
 
2018
      error(getPos(), "Invalid image size in DCT stream");
 
2019
      y = height;
 
2020
      return;
 
2021
    }
1968
2022
    for (i = 0; i < numComps; ++i) {
1969
2023
      frameBuf[i] = (int *)gmallocn(bufWidth * bufHeight, sizeof(int));
1970
2024
      memset(frameBuf[i], 0, bufWidth * bufHeight * sizeof(int));
3040
3094
  }
3041
3095
  scanInfo.firstCoeff = str->getChar();
3042
3096
  scanInfo.lastCoeff = str->getChar();
 
3097
  if (scanInfo.firstCoeff < 0 || scanInfo.lastCoeff > 63 ||
 
3098
      scanInfo.firstCoeff > scanInfo.lastCoeff) {
 
3099
    error(getPos(), "Bad DCT coefficient numbers in scan info block");
 
3100
    return gFalse;
 
3101
  }
3043
3102
  c = str->getChar();
3044
3103
  scanInfo.ah = (c >> 4) & 0x0f;
3045
3104
  scanInfo.al = c & 0x0f;