summaryrefslogtreecommitdiff
path: root/lib/pdf/xpdf-changes.patch
blob: 5525f81f25f0f936d266622633ca54bf4d631e9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
--- xpdf/Catalog.cc.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/Catalog.cc	2010-08-16 14:02:38.000000000 -0700
@@ -193,7 +193,7 @@
   if (!kids.isArray()) {
     error(-1, "Kids object (page %d) is wrong type (%s)",
 	  start+1, kids.getTypeName());
-    goto err1;
+    return start;
   }
   for (i = 0; i < kids.arrayGetLength(); ++i) {
     kids.arrayGetNF(i, &kidRef);
--- xpdf/CharCodeToUnicode.cc.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/CharCodeToUnicode.cc	2010-08-16 14:02:38.000000000 -0700
@@ -208,13 +208,13 @@
 				   int nBits) {
   PSTokenizer *pst;
   char tok1[256], tok2[256], tok3[256];
-  int nDigits, n1, n2, n3;
+  int maxCode, n1, n2, n3;
   CharCode i;
   CharCode code1, code2;
   GString *name;
   FILE *f;
 
-  nDigits = nBits / 4;
+  maxCode = (nBits == 8) ? 0xff : (nBits == 16) ? 0xffff : 0xffffffff;
   pst = new PSTokenizer(getCharFunc, data);
   pst->getToken(tok1, sizeof(tok1), &n1);
   while (pst->getToken(tok2, sizeof(tok2), &n2)) {
@@ -241,9 +241,9 @@
 	  error(-1, "Illegal entry in bfchar block in ToUnicode CMap");
 	  break;
 	}
-	if (!(n1 == 2 + nDigits && tok1[0] == '<' && tok1[n1 - 1] == '>' &&
+	if (!(tok1[0] == '<' && tok1[n1 - 1] == '>' &&
 	      tok2[0] == '<' && tok2[n2 - 1] == '>')) {
-	  error(-1, "Illegal entry in bfchar block in ToUnicode CMap");
+	  error(-1, "Illegal entry in bfchar block in ToUnicode CMap.");
 	  continue;
 	}
 	tok1[n1 - 1] = tok2[n2 - 1] = '\0';
@@ -251,6 +251,9 @@
 	  error(-1, "Illegal entry in bfchar block in ToUnicode CMap");
 	  continue;
 	}
+        if (code1 > maxCode) {
+          error(-1, "Invalid entry in bfchar block in ToUnicode CMap");
+        }
 	addMapping(code1, tok2 + 1, n2 - 2, 0);
       }
       pst->getToken(tok1, sizeof(tok1), &n1);
@@ -266,8 +269,8 @@
 	  error(-1, "Illegal entry in bfrange block in ToUnicode CMap");
 	  break;
 	}
-	if (!(n1 == 2 + nDigits && tok1[0] == '<' && tok1[n1 - 1] == '>' &&
-	      n2 == 2 + nDigits && tok2[0] == '<' && tok2[n2 - 1] == '>')) {
+	if (!(tok1[0] == '<' && tok1[n1 - 1] == '>' &&
+	      tok2[0] == '<' && tok2[n2 - 1] == '>')) {
 	  error(-1, "Illegal entry in bfrange block in ToUnicode CMap");
 	  continue;
 	}
@@ -277,6 +280,10 @@
 	  error(-1, "Illegal entry in bfrange block in ToUnicode CMap");
 	  continue;
 	}
+        if (code1 > maxCode || code2 > maxCode) {
+	    error(-1, "Invalid entry in bfrange block in ToUnicode CMap");
+	}
+
 	if (!strcmp(tok3, "[")) {
 	  i = 0;
 	  while (pst->getToken(tok1, sizeof(tok1), &n1) &&
@@ -320,7 +327,13 @@
   if (code >= mapLen) {
     oldLen = mapLen;
     mapLen = (code + 256) & ~255;
+    Unicode *oldmap;
     map = (Unicode *)greallocn(map, mapLen, sizeof(Unicode));
+    if(!map) {
+       /* we sometimes get overflows for files trying to use 0xffffffff as charcode */
+       map = oldmap;
+       return;
+    }
     for (i = oldLen; i < mapLen; ++i) {
       map[i] = 0;
     }
--- xpdf/Decrypt.cc.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/Decrypt.cc	2010-10-19 12:21:16.000000000 -0700
@@ -596,6 +596,7 @@
   s->bufIdx = 0;
   if (last) {
     n = s->buf[15];
+    if(!n || n>16) n=16;
     for (i = 15; i >= n; --i) {
       s->buf[i] = s->buf[i-n];
     }
--- xpdf/Function.cc.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/Function.cc	2010-10-19 12:21:32.000000000 -0700
@@ -401,7 +401,7 @@
       for (k = 0, t = j; k < m; ++k, t >>= 1) {
 	idx += idxMul[k] * (e[k][t & 1]);
       }
-      sBuf[j] = samples[idx];
+      sBuf[j] = (idx>=0&&idx<nSamples)? samples[idx] : 0;
     }
 
     // do m sets of interpolations
--- xpdf/Gfx.cc.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/Gfx.cc	2010-10-19 12:20:23.000000000 -0700
@@ -444,6 +444,7 @@
   xref = xrefA;
   subPage = gFalse;
   printCommands = globalParams->getPrintCommands();
+  parser = NULL;
 
   // start the resource stack
   res = new GfxResources(xref, resDict, NULL);
@@ -465,6 +466,7 @@
   abortCheckCbkData = abortCheckCbkDataA;
 
   // set crop box
+#ifdef XPDFEXE
   if (cropBox) {
     state->moveTo(cropBox->x1, cropBox->y1);
     state->lineTo(cropBox->x2, cropBox->y1);
@@ -475,6 +477,7 @@
     out->clip(state);
     state->clearPath();
   }
+#endif
 }
 
 Gfx::Gfx(XRef *xrefA, OutputDev *outA, Dict *resDict,
@@ -3182,8 +3185,11 @@
 			    u, (int)(sizeof(u) / sizeof(Unicode)), &uLen,
 			    &dx, &dy, &originX, &originY);
       dx = dx * state->getFontSize() + state->getCharSpace();
-      if (n == 1 && *p == ' ') {
-	dx += state->getWordSpace();
+      if (n == 1 && (*p == ' ' || *p == 0)) {
+        double w=state->getWordSpace();
+        if (w==0 && dx==0)
+          w=state->getFontSize()/3; // workaround for zero word space
+        dx += w;
       }
       dx *= state->getHorizScaling();
       dy *= state->getFontSize();
@@ -3476,11 +3482,13 @@
       }
     }
     if (!obj1.isNull()) {
-      colorSpace = GfxColorSpace::parse(&obj1);
+      colorSpace = GfxColorSpace::parse(&obj1, csMode);
     } else if (csMode == streamCSDeviceGray) {
       colorSpace = new GfxDeviceGrayColorSpace();
     } else if (csMode == streamCSDeviceRGB) {
       colorSpace = new GfxDeviceRGBColorSpace();
+    } else if (csMode == streamCSDeviceRGBX) {
+      colorSpace = new GfxDeviceRGBXColorSpace();
     } else if (csMode == streamCSDeviceCMYK) {
       colorSpace = new GfxDeviceCMYKColorSpace();
     } else {
@@ -3824,6 +3832,7 @@
     out->beginTransparencyGroup(state, bbox, blendingColorSpace,
 				isolated, knockout, softMask);
   }
+  GfxState*old_state = state;
 
   // set new base matrix
   for (i = 0; i < 6; ++i) {
@@ -3835,6 +3844,9 @@
   display(str, gFalse);
 
   if (softMask || transpGroup) {
+    // restore graphics state
+    while(state != old_state)
+	restoreState();
     out->endTransparencyGroup(state);
   }
 
@@ -3921,6 +3933,10 @@
   obj.free();
 
   // make stream
+  if(!parser->getStream()) {
+      error(getPos(), "Couldn't get stream");
+      return NULL;
+  }
   str = new EmbedStream(parser->getStream(), &dict, gFalse, 0);
   str = str->addFilters(&dict);
 
--- xpdf/GfxFont.cc.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/GfxFont.cc	2010-08-16 14:02:38.000000000 -0700
@@ -194,7 +194,7 @@
       embFontID = obj2.getRef();
       if (type != fontType1) {
 	error(-1, "Mismatch between font type and embedded font file");
-	type = fontType1;
+	type = isCIDFont() ? fontCIDType0 : fontType1;
       }
     }
     obj2.free();
@@ -919,6 +919,10 @@
   return 1;
 }
 
+CharCodeToUnicode* Gfx8BitFont::getCTU() {
+    return ctu;
+}
+
 CharCodeToUnicode *Gfx8BitFont::getToUnicode() {
   ctu->incRefCnt();
   return ctu;
@@ -1411,6 +1415,10 @@
   }
 }
 
+CharCodeToUnicode* GfxCIDFont::getCTU() {
+    return ctu;
+}
+
 int GfxCIDFont::getNextChar(char *s, int len, CharCode *code,
 			    Unicode *u, int uSize, int *uLen,
 			    double *dx, double *dy, double *ox, double *oy) {
--- xpdf/GfxFont.h.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/GfxFont.h	2010-08-16 14:02:38.000000000 -0700
@@ -164,6 +164,7 @@
   virtual int getNextChar(char *s, int len, CharCode *code,
 			  Unicode *u, int uSize, int *uLen,
 			  double *dx, double *dy, double *ox, double *oy) = 0;
+  virtual CharCodeToUnicode* getCTU() = 0;
 
 protected:
 
@@ -204,6 +205,7 @@
   virtual int getNextChar(char *s, int len, CharCode *code,
 			  Unicode *u, int uSize, int *uLen,
 			  double *dx, double *dy, double *ox, double *oy);
+  virtual CharCodeToUnicode* getCTU();
 
   // Return the encoding.
   char **getEncoding() { return enc; }
@@ -212,7 +214,7 @@
   CharCodeToUnicode *getToUnicode();
 
   // Return the character name associated with <code>.
-  char *getCharName(int code) { return enc[code]; }
+  char *getCharName(int code) { return code>=256?0:enc[code]; }
 
   // Returns true if the PDF font specified an encoding.
   GBool getHasEncoding() { return hasEncoding; }
@@ -266,6 +268,7 @@
   virtual int getNextChar(char *s, int len, CharCode *code,
 			  Unicode *u, int uSize, int *uLen,
 			  double *dx, double *dy, double *ox, double *oy);
+  virtual CharCodeToUnicode* getCTU();
 
   // Return the writing mode (0=horizontal, 1=vertical).
   virtual int getWMode();
--- xpdf/GfxState.cc.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/GfxState.cc	2010-08-16 14:02:38.000000000 -0700
@@ -21,6 +21,7 @@
 #include "Array.h"
 #include "Page.h"
 #include "GfxState.h"
+#include "cmyk.h"
 
 //------------------------------------------------------------------------
 
@@ -92,7 +93,7 @@
 GfxColorSpace::~GfxColorSpace() {
 }
 
-GfxColorSpace *GfxColorSpace::parse(Object *csObj) {
+GfxColorSpace *GfxColorSpace::parse(Object *csObj, StreamColorSpaceMode csMode) {
   GfxColorSpace *cs;
   Object obj1;
 
@@ -101,7 +102,10 @@
     if (csObj->isName("DeviceGray") || csObj->isName("G")) {
       cs = new GfxDeviceGrayColorSpace();
     } else if (csObj->isName("DeviceRGB") || csObj->isName("RGB")) {
-      cs = new GfxDeviceRGBColorSpace();
+      if(csMode == streamCSDeviceRGBX)
+	cs = new GfxDeviceRGBXColorSpace();
+      else
+	cs = new GfxDeviceRGBColorSpace();
     } else if (csObj->isName("DeviceCMYK") || csObj->isName("CMYK")) {
       cs = new GfxDeviceCMYKColorSpace();
     } else if (csObj->isName("Pattern")) {
@@ -114,7 +118,10 @@
     if (obj1.isName("DeviceGray") || obj1.isName("G")) {
       cs = new GfxDeviceGrayColorSpace();
     } else if (obj1.isName("DeviceRGB") || obj1.isName("RGB")) {
-      cs = new GfxDeviceRGBColorSpace();
+      if(csMode == streamCSDeviceRGBX)
+        cs = new GfxDeviceRGBColorSpace();
+      else
+	cs = new GfxDeviceRGBColorSpace();
     } else if (obj1.isName("DeviceCMYK") || obj1.isName("CMYK")) {
       cs = new GfxDeviceCMYKColorSpace();
     } else if (obj1.isName("CalGray")) {
@@ -333,6 +340,17 @@
 }
 
 //------------------------------------------------------------------------
+// GfxDeviceRGBXColorSpace
+//------------------------------------------------------------------------
+
+GfxDeviceRGBXColorSpace::GfxDeviceRGBXColorSpace() {
+}
+
+GfxColorSpace *GfxDeviceRGBXColorSpace::copy() {
+  return new GfxDeviceRGBXColorSpace();
+}
+
+//------------------------------------------------------------------------
 // GfxCalRGBColorSpace
 //------------------------------------------------------------------------
 
@@ -490,6 +508,18 @@
 				- 0.11 * color->c[2] + 0.5));
 }
 
+/*void GfxDeviceCMYKColorSpace::getRGB(GfxColor *color, GfxRGB *rgb) {
+    unsigned char r,g,b;
+    float c = color->c[0];
+    float m = color->c[1];
+    float y = color->c[2];
+    float k = color->c[3];
+    convert_cmyk2rgb(c,m,y,k, &r,&g,&b);
+    rgb->r = r/255.0;
+    rgb->g = g/255.0;
+    rgb->b = b/255.0;
+}*/
+
 void GfxDeviceCMYKColorSpace::getRGB(GfxColor *color, GfxRGB *rgb) {
   double c, m, y, k, c1, m1, y1, k1, r, g, b, x;
 
@@ -3187,6 +3217,7 @@
   GfxIndexedColorSpace *indexedCS;
   GfxSeparationColorSpace *sepCS;
   int maxPixel, indexHigh;
+  int maxPixelForAlloc;
   Guchar *lookup2;
   Function *sepFunc;
   Object obj;
@@ -3199,6 +3230,7 @@
   // bits per component and color space
   bits = bitsA;
   maxPixel = (1 << bits) - 1;
+  maxPixelForAlloc = (1 << (bits>8?bits:8));
   colorSpace = colorSpaceA;
 
   // initialize
@@ -3253,7 +3285,7 @@
     lookup2 = indexedCS->getLookup();
     colorSpace2->getDefaultRanges(x, y, indexHigh);
     for (k = 0; k < nComps2; ++k) {
-      lookup[k] = (GfxColorComp *)gmallocn(maxPixel + 1,
+      lookup[k] = (GfxColorComp *)gmallocn(maxPixelForAlloc + 1,
 					   sizeof(GfxColorComp));
       for (i = 0; i <= maxPixel; ++i) {
 	j = (int)(decodeLow[0] + (i * decodeRange[0]) / maxPixel + 0.5);
@@ -3272,7 +3304,7 @@
     nComps2 = colorSpace2->getNComps();
     sepFunc = sepCS->getFunc();
     for (k = 0; k < nComps2; ++k) {
-      lookup[k] = (GfxColorComp *)gmallocn(maxPixel + 1,
+      lookup[k] = (GfxColorComp *)gmallocn(maxPixelForAlloc + 1,
 					   sizeof(GfxColorComp));
       for (i = 0; i <= maxPixel; ++i) {
 	x[0] = decodeLow[0] + (i * decodeRange[0]) / maxPixel;
@@ -3282,7 +3314,7 @@
     }
   } else {
     for (k = 0; k < nComps; ++k) {
-      lookup[k] = (GfxColorComp *)gmallocn(maxPixel + 1,
+      lookup[k] = (GfxColorComp *)gmallocn(maxPixelForAlloc + 1,
 					   sizeof(GfxColorComp));
       for (i = 0; i <= maxPixel; ++i) {
 	lookup[k][i] = dblToCol(decodeLow[k] +
@@ -3754,7 +3786,10 @@
 }
 
 void GfxState::setPath(GfxPath *pathA) {
+  if(pathA) {
+      if(path)
   delete path;
+  }
   path = pathA;
 }
 
--- xpdf/GfxState.h.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/GfxState.h	2010-08-16 14:02:38.000000000 -0700
@@ -138,7 +138,7 @@
   virtual GfxColorSpaceMode getMode() = 0;
 
   // Construct a color space.  Returns NULL if unsuccessful.
-  static GfxColorSpace *parse(Object *csObj);
+  static GfxColorSpace *parse(Object *csObj, StreamColorSpaceMode csMode = streamCSNone);
 
   // Convert to gray, RGB, or CMYK.
   virtual void getGray(GfxColor *color, GfxGray *gray) = 0;
@@ -251,6 +251,19 @@
 private:
 };
 
+//------------------------------------------------------------------------
+// GfxDeviceRGBXColorSpace
+//------------------------------------------------------------------------
+
+class GfxDeviceRGBXColorSpace: public GfxDeviceRGBColorSpace {
+public:
+
+  GfxDeviceRGBXColorSpace();
+  virtual GfxColorSpace *copy();
+  virtual int getNComps() { return 4; }
+private:
+};
+
 //------------------------------------------------------------------------
 // GfxCalRGBColorSpace
 //------------------------------------------------------------------------
--- xpdf/GlobalParams.cc.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/GlobalParams.cc	2010-08-16 14:02:38.000000000 -0700
@@ -914,6 +914,29 @@
   int line;
   char buf[512];
 
+  /* extract path */
+  if(fileName) {
+    char* cfgFileName = fileName->getCString();
+    char* pos1 = strrchr(cfgFileName, '/');
+    char* pos2 = strrchr(cfgFileName, '\\');
+    char* p = pos1>pos2?pos1:pos2;
+    int pos = p ? p-cfgFileName : -1;
+    GString*path = new GString(new GString(cfgFileName), 0, (pos < 0 ? strlen(cfgFileName): pos));
+    if(pos1>=0)
+	path->append('/');
+    else if(pos2>=0)
+	path->append('\\');
+    else
+#ifdef WIN32
+	path->append('\\');
+#else
+	path->append('/');
+#endif
+    this->path = path;
+  } else {
+    this->path = new GString();
+  }
+  
   line = 1;
   while (getLine(buf, sizeof(buf) - 1, f)) {
     parseLine(buf, fileName, line);
@@ -1114,6 +1137,42 @@
   deleteGList(tokens, GString);
 }
 
+static char is_absolute(char*filename)
+{
+    int l = strlen(filename);
+    if(filename[0] == '/' || filename[0] == '\\') 
+	return 1;
+    if(l>2 && filename[1]==':' && (filename[2]=='\\' || filename[2]=='/'))
+	return 1;
+    return 0;
+}
+
+static GString* qualify_filename(GString*path, GString*filename)
+{
+  GString*fullpath = 0;
+  char*prefix = "/usr/local/share/xpdf/";
+
+  if (!is_absolute(filename->getCString())) {
+    /* relative path */
+    fullpath = path->copy();
+    fullpath->append(filename);
+  } else if (!strncmp(filename->getCString(), prefix, strlen(prefix))) {
+    /* xpdf default path */
+    char*s = strchr(filename->getCString()+strlen(prefix), '/');
+    if(s) {
+	fullpath = path->copy();
+	fullpath->append(s+1);
+    } else {
+	fullpath = filename->copy();
+    }
+  } else {
+    /* absolute path */
+    fullpath = filename->copy();
+  }
+  //printf("%s -%s-> %s\n", filename->getCString(), path->getCString(), fullpath->getCString());
+  return fullpath;
+}
+
 void GlobalParams::parseNameToUnicode(GList *tokens, GString *fileName,
 					 int line) {
   GString *name;
@@ -1128,10 +1187,10 @@
 	  fileName->getCString(), line);
     return;
   }
-  name = (GString *)tokens->get(1);
+  name = qualify_filename(this->path, (GString *)tokens->get(1));
   if (!(f = fopen(name->getCString(), "r"))) {
-    error(-1, "Couldn't open 'nameToUnicode' file '%s'",
-	  name->getCString());
+    error(-1, "Couldn't open 'nameToUnicode' file '%s' using path '%s'",
+	  name->getCString(), path->getCString());
     return;
   }
   line2 = 1;
@@ -1160,10 +1219,12 @@
   }
   collection = (GString *)tokens->get(1);
   name = (GString *)tokens->get(2);
+
   if ((old = (GString *)cidToUnicodes->remove(collection))) {
     delete old;
   }
-  cidToUnicodes->add(collection->copy(), name->copy());
+
+  cidToUnicodes->add(collection->copy(), qualify_filename(this->path, name));
 }
 
 void GlobalParams::parseUnicodeToUnicode(GList *tokens, GString *fileName,
@@ -1180,7 +1241,8 @@
   if ((old = (GString *)unicodeToUnicodes->remove(font))) {
     delete old;
   }
-  unicodeToUnicodes->add(font->copy(), file->copy());
+
+  unicodeToUnicodes->add(font->copy(), qualify_filename(this->path, file));
 }
 
 void GlobalParams::parseUnicodeMap(GList *tokens, GString *fileName,
@@ -1197,7 +1259,8 @@
   if ((old = (GString *)unicodeMaps->remove(encodingName))) {
     delete old;
   }
-  unicodeMaps->add(encodingName->copy(), name->copy());
+
+  unicodeMaps->add(encodingName->copy(), qualify_filename(this->path, name));
 }
 
 void GlobalParams::parseCMapDir(GList *tokens, GString *fileName, int line) {
@@ -1215,23 +1278,30 @@
     list = new GList();
     cMapDirs->add(collection->copy(), list);
   }
-  list->append(dir->copy());
+
+  list->append(qualify_filename(this->path, dir));
 }
 
 void GlobalParams::parseToUnicodeDir(GList *tokens, GString *fileName,
 				     int line) {
+  GString *dir;
+
   if (tokens->getLength() != 2) {
     error(-1, "Bad 'toUnicodeDir' config file command (%s:%d)",
 	  fileName->getCString(), line);
     return;
   }
-  toUnicodeDirs->append(((GString *)tokens->get(1))->copy());
+
+  dir = (GString *)tokens->get(1);
+
+  toUnicodeDirs->append(qualify_filename(this->path, dir));
 }
 
 void GlobalParams::parseDisplayFont(GList *tokens, GHash *fontHash,
 				    DisplayFontParamKind kind,
 				    GString *fileName, int line) {
   DisplayFontParam *param, *old;
+  GString *file;
 
   if (tokens->getLength() < 2) {
     goto err1;
@@ -1243,13 +1313,15 @@
     if (tokens->getLength() != 3) {
       goto err2;
     }
-    param->t1.fileName = ((GString *)tokens->get(2))->copy();
+    file = (GString *)tokens->get(2);
+    param->t1.fileName = qualify_filename(this->path, file);
     break;
   case displayFontTT:
     if (tokens->getLength() != 3) {
       goto err2;
     }
-    param->tt.fileName = ((GString *)tokens->get(2))->copy();
+    file = (GString *)tokens->get(2);
+    param->tt.fileName = qualify_filename(this->path, file);
     break;
   }
 
--- xpdf/GlobalParams.h.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/GlobalParams.h	2010-08-16 14:02:38.000000000 -0700
@@ -196,7 +196,7 @@
   // file.
   GlobalParams(char *cfgFileName);
 
-  ~GlobalParams();
+  virtual ~GlobalParams();
 
   void setBaseDir(char *dir);
   void setupBaseFonts(char *dir);
@@ -213,8 +213,8 @@
   FILE *getUnicodeMapFile(GString *encodingName);
   FILE *findCMapFile(GString *collection, GString *cMapName);
   FILE *findToUnicodeFile(GString *name);
-  DisplayFontParam *getDisplayFont(GString *fontName);
-  DisplayFontParam *getDisplayCIDFont(GString *fontName, GString *collection);
+  virtual DisplayFontParam *getDisplayFont(GString *fontName);
+  virtual DisplayFontParam *getDisplayCIDFont(GString *fontName, GString *collection);
   GString *getPSFile();
   int getPSPaperWidth();
   int getPSPaperHeight();
@@ -316,7 +316,7 @@
 private:
 
   void createDefaultKeyBindings();
-  void parseFile(GString *fileName, FILE *f);
+public: void parseFile(GString *fileName, FILE *f); private:
   void parseNameToUnicode(GList *tokens, GString *fileName, int line);
   void parseCIDToUnicode(GList *tokens, GString *fileName, int line);
   void parseUnicodeToUnicode(GList *tokens, GString *fileName, int line);
@@ -358,6 +358,10 @@
   GBool loadPlugin(char *type, char *name);
 #endif
 
+  //----- config file base path
+
+  GString*path;
+
   //----- static tables
 
   NameToCharCode *		// mapping from char name to
--- xpdf/JBIG2Stream.cc.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/JBIG2Stream.cc	2010-08-16 14:02:38.000000000 -0700
@@ -6,7 +6,24 @@
 //
 //========================================================================
 
-#include <aconf.h>
+//========================================================================
+//
+// Modified under the Poppler project - http://poppler.freedesktop.org
+//
+// All changes made under the Poppler project to this file are licensed
+// under GPL version 2 or later
+//
+// Copyright (C) 2006 Raj Kumar <rkumar@archive.org>
+// Copyright (C) 2006 Paul Walmsley <paul@booyaka.com>
+// Copyright (C) 2006-2009 Albert Astals Cid <aacid@kde.org>
+// Copyright (C) 2009 David Benjamin <davidben@mit.edu>
+//
+// To see a description of the changes please see the Changelog file that
+// came with your tarball or type make ChangeLog if you are building from git
+//
+//========================================================================
+
+#include <config.h>
 
 #ifdef USE_GCC_PRAGMAS
 #pragma implementation
@@ -24,8 +41,8 @@
 
 //------------------------------------------------------------------------
 
-static int contextSize[4] = { 16, 13, 10, 10 };
-static int refContextSize[2] = { 13, 10 };
+static const int contextSize[4] = { 16, 13, 10, 10 };
+static const int refContextSize[2] = { 13, 10 };
 
 //------------------------------------------------------------------------
 // JBIG2HuffmanTable
@@ -42,7 +59,7 @@
   Guint prefix;
 };
 
-JBIG2HuffmanTable huffTableA[] = {
+static JBIG2HuffmanTable huffTableA[] = {
   {     0, 1,  4,              0x000 },
   {    16, 2,  8,              0x002 },
   {   272, 3, 16,              0x006 },
@@ -50,7 +67,7 @@
   {     0, 0, jbig2HuffmanEOT, 0     }
 };
 
-JBIG2HuffmanTable huffTableB[] = {
+static JBIG2HuffmanTable huffTableB[] = {
   {     0, 1,  0,              0x000 },
   {     1, 2,  0,              0x002 },
   {     2, 3,  0,              0x006 },
@@ -61,7 +78,7 @@
   {     0, 0, jbig2HuffmanEOT, 0     }
 };
 
-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];
   }
   gfree(bitmaps);
   if (genericRegionStats) {
@@ -1247,6 +1265,7 @@
   Guint segNum, segFlags, segType, page, segLength;
   Guint refFlags, nRefSegs;
   Guint *refSegs;
+  int segDataPos;
   int c1, c2, c3;
   Guint i;
 
@@ -1314,6 +1333,9 @@
       goto eofError2;
     }
 
+    // keep track of the start of the segment data 
+    segDataPos = getPos();
+
     // check for missing page information segment
     if (!pageBitmap && ((segType >= 4 && segType <= 7) ||
 			(segType >= 20 && segType <= 43))) {
@@ -1398,6 +1420,45 @@
       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);
   }
 
@@ -1493,6 +1554,9 @@
   codeTables = new GList();
   numInputSyms = 0;
   for (i = 0; i < nRefSegs; ++i) {
+    // This is need by bug 12014, returning gFalse makes it not crash
+    // but we end up with a empty page while acroread is able to render
+    // part of it
     if ((seg = findSegment(refSegs[i]))) {
       if (seg->getType() == jbig2SegSymbolDict) {
 	j = ((JBIG2SymbolDict *)seg)->getSize();
@@ -1503,8 +1567,11 @@
 	}
 	numInputSyms += j;
       } else if (seg->getType() == jbig2SegCodeTable) {
-	codeTables->append(seg);
+        codeTables->append(seg);
       }
+    } else {
+      delete codeTables;
+      return gFalse;
     }
   }
   if (numInputSyms > UINT_MAX - numNewSyms) {
@@ -1514,11 +1581,14 @@
   }
 
   // compute symbol code length
-  symCodeLen = 1;
-  i = (numInputSyms + numNewSyms) >> 1;
-  while (i) {
-    ++symCodeLen;
-    i >>= 1;
+  symCodeLen = 0;
+  i = 1;
+  while (i < numInputSyms + numNewSyms) {
+   ++symCodeLen;
+   i <<= 1;
+  }
+  if (huff && symCodeLen == 0) {
+   symCodeLen = 1;
   }
 
   // get the input symbol bitmaps
@@ -1530,12 +1600,11 @@
   k = 0;
   inputSymbolDict = NULL;
   for (i = 0; i < nRefSegs; ++i) {
-    if ((seg = findSegment(refSegs[i]))) {
-      if (seg->getType() == jbig2SegSymbolDict) {
-	inputSymbolDict = (JBIG2SymbolDict *)seg;
-	for (j = 0; j < inputSymbolDict->getSize(); ++j) {
-	  bitmaps[k++] = inputSymbolDict->getBitmap(j);
-	}
+    seg = findSegment(refSegs[i]);
+    if (seg != NULL && seg->getType() == jbig2SegSymbolDict) {
+      inputSymbolDict = (JBIG2SymbolDict *)seg;
+      for (j = 0; j < inputSymbolDict->getSize(); ++j) {
+	bitmaps[k++] = inputSymbolDict->getBitmap(j);
       }
     }
   }
@@ -1753,6 +1822,10 @@
 
   // create the symbol dict object
   symbolDict = new JBIG2SymbolDict(segNum, numExSyms);
+  if (!symbolDict->isOk()) {
+    delete symbolDict;
+    goto syntaxError;
+  }
 
   // exported symbol list
   i = j = 0;
@@ -1766,6 +1839,7 @@
     if (i + run > numInputSyms + numNewSyms ||
 	(ex && j + run > numExSyms)) {
       error(getPos(), "Too many exported symbols in JBIG2 symbol dictionary");
+      for ( ; j < numExSyms; ++j) symbolDict->setBitmap(j, NULL);
       delete symbolDict;
       goto syntaxError;
     }
@@ -1780,6 +1854,7 @@
   }
   if (j != numExSyms) {
     error(getPos(), "Too few symbols in JBIG2 symbol dictionary");
+    for ( ; j < numExSyms; ++j) symbolDict->setBitmap(j, NULL);
     delete symbolDict;
     goto syntaxError;
   }
@@ -1921,6 +1996,9 @@
     ++symCodeLen;
     i <<= 1;
   }
+  if (huff && symCodeLen == 0) {
+    symCodeLen = 1;
+  }
 
   // get the symbol bitmaps
   syms = (JBIG2Bitmap **)gmallocn(numSyms, sizeof(JBIG2Bitmap *));
@@ -2095,18 +2173,20 @@
 
   gfree(syms);
 
-  // combine the region bitmap into the page bitmap
-  if (imm) {
-    if (pageH == 0xffffffff && y + h > curPageH) {
-      pageBitmap->expand(y + h, pageDefPixel);
-    }
-    pageBitmap->combine(bitmap, x, y, extCombOp);
-    delete bitmap;
+  if (bitmap) {
+    // combine the region bitmap into the page bitmap
+    if (imm) {
+      if (pageH == 0xffffffff && y + h > curPageH) {
+        pageBitmap->expand(y + h, pageDefPixel);
+      }
+      pageBitmap->combine(bitmap, x, y, extCombOp);
+      delete bitmap;
 
-  // store the region bitmap
-  } else {
-    bitmap->setSegNum(segNum);
-    segments->append(bitmap);
+    // store the region bitmap
+    } else {
+      bitmap->setSegNum(segNum);
+      segments->append(bitmap);
+    }
   }
 
   // clean up the Huffman decoder
@@ -2159,6 +2239,10 @@
 
   // allocate the bitmap
   bitmap = new JBIG2Bitmap(0, w, h);
+  if (!bitmap->isOk()) {
+    delete bitmap;
+    return NULL;
+  }
   if (defPixel) {
     bitmap->clearToOne();
   } else {
@@ -2235,73 +2319,84 @@
 	  ri = 0;
 	}
 	if (ri) {
+	  GBool decodeSuccess;
 	  if (huff) {
-	    huffDecoder->decodeInt(&rdw, huffRDWTable);
-	    huffDecoder->decodeInt(&rdh, huffRDHTable);
-	    huffDecoder->decodeInt(&rdx, huffRDXTable);
-	    huffDecoder->decodeInt(&rdy, huffRDYTable);
-	    huffDecoder->decodeInt(&bmSize, huffRSizeTable);
+	    decodeSuccess = huffDecoder->decodeInt(&rdw, huffRDWTable);
+	    decodeSuccess = decodeSuccess && huffDecoder->decodeInt(&rdh, huffRDHTable);
+	    decodeSuccess = decodeSuccess && huffDecoder->decodeInt(&rdx, huffRDXTable);
+	    decodeSuccess = decodeSuccess && huffDecoder->decodeInt(&rdy, huffRDYTable);
+	    decodeSuccess = decodeSuccess && huffDecoder->decodeInt(&bmSize, huffRSizeTable);
 	    huffDecoder->reset();
 	    arithDecoder->start();
 	  } else {
-	    arithDecoder->decodeInt(&rdw, iardwStats);
-	    arithDecoder->decodeInt(&rdh, iardhStats);
-	    arithDecoder->decodeInt(&rdx, iardxStats);
-	    arithDecoder->decodeInt(&rdy, iardyStats);
+	    decodeSuccess = arithDecoder->decodeInt(&rdw, iardwStats);
+	    decodeSuccess = decodeSuccess && arithDecoder->decodeInt(&rdh, iardhStats);
+	    decodeSuccess = decodeSuccess && arithDecoder->decodeInt(&rdx, iardxStats);
+	    decodeSuccess = decodeSuccess && arithDecoder->decodeInt(&rdy, iardyStats);
+	  }
+	  
+	  if (decodeSuccess && syms[symID])
+	  {
+	    refDX = ((rdw >= 0) ? rdw : rdw - 1) / 2 + rdx;
+	    refDY = ((rdh >= 0) ? rdh : rdh - 1) / 2 + rdy;
+
+	    symbolBitmap =
+	      readGenericRefinementRegion(rdw + syms[symID]->getWidth(),
+					  rdh + syms[symID]->getHeight(),
+					  templ, gFalse, syms[symID],
+					  refDX, refDY, atx, aty);
 	  }
-	  refDX = ((rdw >= 0) ? rdw : rdw - 1) / 2 + rdx;
-	  refDY = ((rdh >= 0) ? rdh : rdh - 1) / 2 + rdy;
-
-	  symbolBitmap =
-	    readGenericRefinementRegion(rdw + syms[symID]->getWidth(),
-					rdh + syms[symID]->getHeight(),
-					templ, gFalse, syms[symID],
-					refDX, refDY, atx, aty);
 	  //~ do we need to use the bmSize value here (in Huffman mode)?
 	} else {
 	  symbolBitmap = syms[symID];
 	}
 
-	// combine the symbol bitmap into the region bitmap
-	//~ something is wrong here - refCorner shouldn't degenerate into
-	//~   two cases
-	bw = symbolBitmap->getWidth() - 1;
-	bh = symbolBitmap->getHeight() - 1;
-	if (transposed) {
-	  switch (refCorner) {
-	  case 0: // bottom left
-	    bitmap->combine(symbolBitmap, tt, s, combOp);
-	    break;
-	  case 1: // top left
-	    bitmap->combine(symbolBitmap, tt, s, combOp);
-	    break;
-	  case 2: // bottom right
-	    bitmap->combine(symbolBitmap, tt - bw, s, combOp);
-	    break;
-	  case 3: // top right
-	    bitmap->combine(symbolBitmap, tt - bw, s, combOp);
-	    break;
+	if (symbolBitmap) {
+	  // combine the symbol bitmap into the region bitmap
+	  //~ something is wrong here - refCorner shouldn't degenerate into
+	  //~   two cases
+	  bw = symbolBitmap->getWidth() - 1;
+	  bh = symbolBitmap->getHeight() - 1;
+	  if (transposed) {
+	    switch (refCorner) {
+	    case 0: // bottom left
+	      bitmap->combine(symbolBitmap, tt, s, combOp);
+	      break;
+	    case 1: // top left
+	      bitmap->combine(symbolBitmap, tt, s, combOp);
+	      break;
+	    case 2: // bottom right
+	      bitmap->combine(symbolBitmap, tt - bw, s, combOp);
+	      break;
+	    case 3: // top right
+	      bitmap->combine(symbolBitmap, tt - bw, s, combOp);
+	      break;
+	    }
+	    s += bh;
+	  } else {
+	    switch (refCorner) {
+	    case 0: // bottom left
+	      bitmap->combine(symbolBitmap, s, tt - bh, combOp);
+	      break;
+	    case 1: // top left
+	      bitmap->combine(symbolBitmap, s, tt, combOp);
+	      break;
+	    case 2: // bottom right
+	      bitmap->combine(symbolBitmap, s, tt - bh, combOp);
+	      break;
+	    case 3: // top right
+	      bitmap->combine(symbolBitmap, s, tt, combOp);
+	      break;
+	    }
+	    s += bw;
 	  }
-	  s += bh;
-	} else {
-	  switch (refCorner) {
-	  case 0: // bottom left
-	    bitmap->combine(symbolBitmap, s, tt - bh, combOp);
-	    break;
-	  case 1: // top left
-	    bitmap->combine(symbolBitmap, s, tt, combOp);
-	    break;
-	  case 2: // bottom right
-	    bitmap->combine(symbolBitmap, s, tt - bh, combOp);
-	    break;
-	  case 3: // top right
-	    bitmap->combine(symbolBitmap, s, tt, combOp);
-	    break;
+	  if (ri) {
+	    delete symbolBitmap;
 	  }
-	  s += bw;
-	}
-	if (ri) {
-	  delete symbolBitmap;
+	} else {
+	  // NULL symbolBitmap only happens on error
+	  delete bitmap;
+	  return NULL;
 	}
       }
 
@@ -2431,11 +2526,12 @@
     error(getPos(), "Bad symbol dictionary reference in JBIG2 halftone segment");
     return;
   }
-  if (!(seg = findSegment(refSegs[0])) ||
-      seg->getType() != jbig2SegPatternDict) {
+  seg = findSegment(refSegs[0]);
+  if (seg == NULL || seg->getType() != jbig2SegPatternDict) {
     error(getPos(), "Bad symbol dictionary reference in JBIG2 halftone segment");
     return;
   }
+
   patternDict = (JBIG2PatternDict *)seg;
   bpp = 0;
   i = 1;
@@ -2591,6 +2687,8 @@
   // read the bitmap
   bitmap = readGenericBitmap(mmr, w, h, templ, tpgdOn, gFalse,
 			     NULL, atx, aty, mmr ? length - 18 : 0);
+  if (!bitmap)
+    return;
 
   // combine the region bitmap into the page bitmap
   if (imm) {
@@ -2616,7 +2714,7 @@
 				      int *codingLine, int *a0i, int w) {
   if (a1 > codingLine[*a0i]) {
     if (a1 > w) {
-      error(getPos(), "JBIG2 MMR row is wrong length ({0:d})", a1);
+      error(getPos(), "JBIG2 MMR row is wrong length (%d)", a1);
       a1 = w;
     }
     if ((*a0i & 1) ^ blackPixels) {
@@ -2630,7 +2728,7 @@
 					 int *codingLine, int *a0i, int w) {
   if (a1 > codingLine[*a0i]) {
     if (a1 > w) {
-      error(getPos(), "JBIG2 MMR row is wrong length ({0:d})", a1);
+      error(getPos(), "JBIG2 MMR row is wrong length (%d)", a1);
       a1 = w;
     }
     if ((*a0i & 1) ^ blackPixels) {
@@ -2657,13 +2755,17 @@
   JBIG2Bitmap *bitmap;
   GBool ltp;
   Guint ltpCX, cx, cx0, cx1, cx2;
-  JBIG2BitmapPtr cxPtr0, cxPtr1;
-  JBIG2BitmapPtr atPtr0, atPtr1, atPtr2, atPtr3;
+  JBIG2BitmapPtr cxPtr0 = {0}, cxPtr1 = {0};
+  JBIG2BitmapPtr atPtr0 = {0}, atPtr1 = {0}, atPtr2 = {0}, atPtr3 = {0};
   int *refLine, *codingLine;
   int code1, code2, code3;
   int x, y, a0i, b1i, blackPixels, pix, i;
 
   bitmap = new JBIG2Bitmap(0, w, h);
+  if (!bitmap->isOk()) {
+    delete bitmap;
+    return NULL;
+  }
   bitmap->clearToZero();
 
   //----- MMR decode
@@ -2682,7 +2784,7 @@
     // ---> max refLine size = w + 2
     codingLine = (int *)gmallocn(w + 1, sizeof(int));
     refLine = (int *)gmallocn(w + 2, sizeof(int));
-    codingLine[0] = w;
+    for (i = 0; i < w + 1; ++i) codingLine[i] = w;
 
     for (y = 0; y < h; ++y) {
 
@@ -3093,8 +3195,8 @@
     return;
   }
   if (nRefSegs == 1) {
-    if (!(seg = findSegment(refSegs[0])) ||
-	seg->getType() != jbig2SegBitmap) {
+    seg = findSegment(refSegs[0]);
+    if (seg == NULL || seg->getType() != jbig2SegBitmap) {
       error(getPos(), "Bad bitmap reference in JBIG2 generic refinement segment");
       return;
     }
@@ -3143,11 +3245,24 @@
   JBIG2Bitmap *bitmap;
   GBool ltp;
   Guint ltpCX, cx, cx0, cx2, cx3, cx4, tpgrCX0, tpgrCX1, tpgrCX2;
-  JBIG2BitmapPtr cxPtr0, cxPtr1, cxPtr2, cxPtr3, cxPtr4, cxPtr5, cxPtr6;
-  JBIG2BitmapPtr tpgrCXPtr0, tpgrCXPtr1, tpgrCXPtr2;
+  JBIG2BitmapPtr cxPtr0 = {0};
+  JBIG2BitmapPtr cxPtr1 = {0};
+  JBIG2BitmapPtr cxPtr2 = {0};
+  JBIG2BitmapPtr cxPtr3 = {0};
+  JBIG2BitmapPtr cxPtr4 = {0};
+  JBIG2BitmapPtr cxPtr5 = {0};
+  JBIG2BitmapPtr cxPtr6 = {0};
+  JBIG2BitmapPtr tpgrCXPtr0 = {0};
+  JBIG2BitmapPtr tpgrCXPtr1 = {0};
+  JBIG2BitmapPtr tpgrCXPtr2 = {0};
   int x, y, pix;
 
   bitmap = new JBIG2Bitmap(0, w, h);
+  if (!bitmap->isOk())
+  {
+    delete bitmap;
+    return NULL;
+  }
   bitmap->clearToZero();
 
   // set up the typical row context
@@ -3332,6 +3447,12 @@
   }
   pageBitmap = new JBIG2Bitmap(0, pageW, curPageH);
 
+  if (!pageBitmap->isOk()) {
+    delete pageBitmap;
+    pageBitmap = NULL;
+    return;
+  }
+  
   // default pixel value
   if (pageDefPixel) {
     pageBitmap->clearToOne();
--- xpdf/JPXStream.cc.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/JPXStream.cc	2010-08-16 14:02:38.000000000 -0700
@@ -241,6 +241,12 @@
   haveCompMap = gFalse;
   haveChannelDefn = gFalse;
 
+  img.xSize = 0;
+  img.ySize = 0;
+  img.xOffset = 0;
+  img.yOffset = 0;
+  curX = 0;
+  curY = 0;
   img.tiles = NULL;
   bitBuf = 0;
   bitBufLen = 0;
@@ -450,6 +456,7 @@
   GBool haveBPC, haveCSMode;
 
   csPrec = 0; // make gcc happy
+  Guint num_components = 0;
   haveBPC = haveCSMode = gFalse;
   str->reset();
   if (str->lookChar() == 0xff) {
@@ -463,7 +470,7 @@
 	cover(1);
 	if (readULong(&dummy) &&
 	    readULong(&dummy) &&
-	    readUWord(&dummy) &&
+	    readUWord(&num_components) &&
 	    readUByte(&bpc1) &&
 	    readUByte(&dummy) &&
 	    readUByte(&dummy) &&
@@ -519,6 +526,9 @@
       }
     }
   }
+  if(*csMode == streamCSDeviceRGB && num_components == 4) {
+    *csMode = streamCSDeviceRGBX;
+  }
   str->close();
 }
 
--- xpdf/Lexer.cc.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/Lexer.cc	2010-08-16 14:02:38.000000000 -0700
@@ -74,6 +74,7 @@
     curStr.streamReset();
   }
 }
+static int illegalChars = 0;
 
 Lexer::~Lexer() {
   if (!curStr.isNone()) {
@@ -83,6 +84,9 @@
   if (freeArray) {
     delete streams;
   }
+  if(illegalChars)
+      error(0, "Illegal characters in hex string (%d)", illegalChars);
+  illegalChars = 0;
 }
 
 int Lexer::getChar() {
@@ -330,7 +334,8 @@
 	} else if (c2 >= 'a' && c2 <= 'f') {
 	  c += c2 - 'a' + 10;
 	} else {
-	  error(getPos(), "Illegal digit in hex char in name");
+	  illegalChars++;
+	  //error(getPos(), "Illegal digit in hex char in name");
 	}
       }
      notEscChar:
@@ -384,8 +389,10 @@
 	    c2 += c - 'A' + 10;
 	  else if (c >= 'a' && c <= 'f')
 	    c2 += c - 'a' + 10;
-	  else
-	    error(getPos(), "Illegal character <%02x> in hex string", c);
+	  else {
+	    illegalChars++;
+	    //error(getPos(), "Illegal character <%02x> in hex string", c);
+	  }
 	  if (++m == 2) {
 	    if (n == tokBufSize) {
 	      if (!s)
@@ -421,7 +428,8 @@
       tokBuf[2] = '\0';
       obj->initCmd(tokBuf);
     } else {
-      error(getPos(), "Illegal character '>'");
+      illegalChars++;
+      //error(getPos(), "Illegal character '>'");
       obj->initError();
     }
     break;
@@ -430,7 +438,8 @@
   case ')':
   case '{':
   case '}':
-    error(getPos(), "Illegal character '%c'", c);
+    //error(getPos(), "Illegal character '%c'", c);
+    illegalChars++;
     obj->initError();
     break;
 
@@ -459,7 +468,6 @@
     }
     break;
   }
-
   return obj;
 }
 
--- xpdf/Link.cc.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/Link.cc	2010-08-16 14:02:38.000000000 -0700
@@ -430,10 +430,9 @@
       delete dest;
       dest = NULL;
     }
-
   // error
   } else {
-    error(-1, "Illegal annotation destination");
+    error(-1, "Illegal annotation destination %d", destObj->getType());
   }
 }
 
@@ -468,10 +467,9 @@
       delete dest;
       dest = NULL;
     }
-
   // error
   } else {
-    error(-1, "Illegal annotation destination");
+    error(-1, "Illegal annotation destination %d", destObj->getType());
   }
 }
 
--- xpdf/OutputDev.h.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/OutputDev.h	2010-08-16 14:02:38.000000000 -0700
@@ -17,6 +17,7 @@
 
 #include "gtypes.h"
 #include "CharTypes.h"
+#include "Object.h"
 
 class GString;
 class GfxState;
--- xpdf/SplashFTFont.cc.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/SplashFTFont.cc	2010-08-16 14:02:38.000000000 -0700
@@ -46,6 +46,7 @@
   int x, y;
 
   face = fontFileA->face;
+  
   if (FT_New_Size(face, &sizeObj)) {
     return;
   }
@@ -54,6 +55,10 @@
   if (FT_Set_Pixel_Sizes(face, 0, (int)size)) {
     return;
   }
+ 
+  this->ascender = face->ascender;
+  this->descender = face->descender;
+
   // if the textMat values are too small, FreeType's fixed point
   // arithmetic doesn't work so well
   textScale = splashSqrt(textMat[2]*textMat[2] + textMat[3]*textMat[3]) / size;
@@ -228,6 +233,12 @@
   GBool needClose;
 };
 
+int SplashFTFont::getNumChars()
+{
+  SplashFTFontFile* ff = (SplashFTFontFile *)fontFile;
+  return ff->face->num_glyphs;
+}
+
 SplashPath *SplashFTFont::getGlyphPath(int c) {
   static FT_Outline_Funcs outlineFuncs = {
 #if FREETYPE_MINOR <= 1
@@ -249,6 +260,8 @@
   FT_UInt gid;
   FT_Glyph glyph;
 
+  this->last_advance = -1;
+
   ff = (SplashFTFontFile *)fontFile;
   ff->face->size = sizeObj;
   FT_Set_Transform(ff->face, &textMatrix, NULL);
@@ -262,17 +275,24 @@
     // skip the TrueType notdef glyph
     return NULL;
   }
-  if (FT_Load_Glyph(ff->face, gid, FT_LOAD_NO_BITMAP)) {
+  int error = 0;
+  if ((error=FT_Load_Glyph(ff->face, gid, FT_LOAD_NO_BITMAP|FT_LOAD_NO_HINTING))) {
+      fprintf(stderr, "Truetype wasn't able to load glyph %d, error %d\n", gid, error);
     return NULL;
   }
   if (FT_Get_Glyph(slot, &glyph)) {
     return NULL;
   }
+  this->last_advance = glyph->advance.x/65536.0;
+
   path.path = new SplashPath();
   path.textScale = textScale;
   path.needClose = gFalse;
-  FT_Outline_Decompose(&((FT_OutlineGlyph)glyph)->outline,
+  error = FT_Outline_Decompose(&((FT_OutlineGlyph)glyph)->outline,
 		       &outlineFuncs, &path);
+  if(error) {
+      fprintf(stderr, "Truetype wasn't able to read glyph %d, error %d\n", gid, error);
+  }
   if (path.needClose) {
     path.path->close();
   }
--- xpdf/SplashFTFont.h.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/SplashFTFont.h	2010-08-16 14:02:38.000000000 -0700
@@ -42,6 +42,9 @@
   virtual GBool makeGlyph(int c, int xFrac, int yFrac,
 			  SplashGlyphBitmap *bitmap);
 
+  // return the number of characters in this font
+  virtual int getNumChars();
+
   // Return the path for a glyph.
   virtual SplashPath *getGlyphPath(int c);
 
--- xpdf/SplashFTFontEngine.cc.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/SplashFTFontEngine.cc	2010-08-16 14:02:38.000000000 -0700
@@ -13,9 +13,7 @@
 #endif
 
 #include <stdio.h>
-#ifndef WIN32
 #  include <unistd.h>
-#endif
 #include "gmem.h"
 #include "GString.h"
 #include "gfile.h"
--- xpdf/SplashFont.cc.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/SplashFont.cc	2010-08-16 14:02:38.000000000 -0700
@@ -48,6 +48,10 @@
   cacheTags = NULL;
 
   xMin = yMin = xMax = yMax = 0;
+
+  last_advance = -1;
+  ascender = -1;
+  descender = -1;
 }
 
 void SplashFont::initCache() {
--- xpdf/SplashFont.h.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/SplashFont.h	2010-08-16 14:02:38.000000000 -0700
@@ -73,6 +73,9 @@
   virtual GBool makeGlyph(int c, int xFrac, int yFrac,
 			  SplashGlyphBitmap *bitmap) = 0;
 
+  // return the number of characters in this font
+  virtual int getNumChars() = 0;
+
   // Return the path for a glyph.
   virtual SplashPath *getGlyphPath(int c) = 0;
 
@@ -83,6 +86,9 @@
   void getBBox(int *xMinA, int *yMinA, int *xMaxA, int *yMaxA)
     { *xMinA = xMin; *yMinA = yMin; *xMaxA = xMax; *yMaxA = yMax; }
 
+  double ascender;
+  double descender;
+  double last_advance; //set after getGlyphPath()
 protected:
 
   SplashFontFile *fontFile;
--- xpdf/SplashFontFile.cc.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/SplashFontFile.cc	2010-08-16 14:02:38.000000000 -0700
@@ -11,9 +11,7 @@
 #endif
 
 #include <stdio.h>
-#ifndef WIN32
-#  include <unistd.h>
-#endif
+#include <unistd.h>
 #include "GString.h"
 #include "SplashFontFile.h"
 #include "SplashFontFileID.h"
--- xpdf/SplashOutputDev.cc.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/SplashOutputDev.cc	2010-08-16 14:02:38.000000000 -0700
@@ -13,6 +13,7 @@
 #endif
 
 #include <string.h>
+#include <unistd.h>
 #include <math.h>
 #include "gfile.h"
 #include "GlobalParams.h"
@@ -2646,9 +2647,9 @@
 
   softMask = new SplashBitmap(bitmap->getWidth(), bitmap->getHeight(),
 			      1, splashModeMono8, gFalse);
-  memset(softMask->getDataPtr(), 0,
-	 softMask->getRowSize() * softMask->getHeight());
+  memset(softMask->getDataPtr(), 0x00, softMask->getRowSize()*softMask->getHeight());
   p = softMask->getDataPtr() + ty * softMask->getRowSize() + tx;
+  if (tx<softMask->getWidth() && ty<softMask->getHeight())
   for (y = 0; y < tBitmap->getHeight(); ++y) {
     for (x = 0; x < tBitmap->getWidth(); ++x) {
       tBitmap->getPixel(x, y, color);
--- xpdf/SplashOutputDev.h.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/SplashOutputDev.h	2010-08-16 14:02:38.000000000 -0700
@@ -196,6 +196,9 @@
   virtual void setVectorAntialias(GBool vaa);
 #endif
 
+  void doUpdateFont(GfxState *state);
+  
+  SplashPath *convertPath(GfxState *state, GfxPath *path);
 private:
 
   void setupScreenParams(double hDPI, double vDPI);
@@ -204,8 +207,6 @@
 #else
   SplashPattern *getColor(GfxGray gray, GfxRGB *rgb);
 #endif
-  SplashPath *convertPath(GfxState *state, GfxPath *path);
-  void doUpdateFont(GfxState *state);
   void drawType3Glyph(T3FontCache *t3Font,
 		      T3FontCacheTag *tag, Guchar *data);
   static GBool imageMaskSrc(void *data, SplashColorPtr line);
--- xpdf/SplashScreen.cc.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/SplashScreen.cc	2010-08-16 14:02:38.000000000 -0700
@@ -363,6 +363,8 @@
 int SplashScreen::test(int x, int y, Guchar value) {
   int xx, yy;
 
+  return 1;
+
   if (value < minVal) {
     return 0;
   }
--- xpdf/SplashXPathScanner.cc.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/SplashXPathScanner.cc	2010-08-16 14:02:38.000000000 -0700
@@ -394,10 +394,10 @@
 	  *p++ &= mask;
 	  xx = (xx & ~7) + 8;
 	}
-	for (; xx + 7 <= xx0; xx += 8) {
+	for (; xx + 7 < xx0; xx += 8) {
 	  *p++ = 0x00;
 	}
-	if (xx <= xx0) {
+	if (xx < xx0) {
 	  *p &= 0xff >> (xx0 & 7);
 	}
       }
@@ -417,10 +417,10 @@
 	*p++ &= mask;
 	xx = (xx & ~7) + 8;
       }
-      for (; xx + 7 <= xx0; xx += 8) {
+      for (; xx + 7 < xx0; xx += 8) {
 	*p++ = 0x00;
       }
-      if (xx <= xx0) {
+      if (xx < xx0) {
 	*p &= 0xff >> (xx0 & 7);
       }
     }
--- xpdf/Stream.cc.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/Stream.cc	2010-08-16 14:02:38.000000000 -0700
@@ -18,6 +18,8 @@
 #include <limits.h>
 #ifndef WIN32
 #include <unistd.h>
+#else
+extern "C" int unlink(char *filename);
 #endif
 #include <string.h>
 #include <ctype.h>
@@ -2456,6 +2458,9 @@
   // check for an EOB run
   if (eobRun > 0) {
     while (i <= scanInfo.lastCoeff) {
+      if(i>=64) {
+	  return gFalse;
+      }
       j = dctZigZag[i++];
       if (data[j] != 0) {
 	if ((bit = readBit()) == EOF) {
@@ -2480,6 +2485,9 @@
     if (c == 0xf0) {
       k = 0;
       while (k < 16) {
+        if(i>=64) {
+	  return gFalse;
+	}
 	j = dctZigZag[i++];
 	if (data[j] == 0) {
 	  ++k;
@@ -2505,6 +2513,9 @@
       }
       eobRun += 1 << j;
       while (i <= scanInfo.lastCoeff) {
+	if(i>=64) {
+	  return gFalse;
+	}
 	j = dctZigZag[i++];
 	if (data[j] != 0) {
 	  if ((bit = readBit()) == EOF) {
@@ -2527,6 +2538,9 @@
       }
       k = 0;
       do {
+	if(i>=64) {
+	  return gFalse;
+	}
 	j = dctZigZag[i++];
 	while (data[j] != 0) {
 	  if ((bit = readBit()) == EOF) {
@@ -2535,6 +2549,9 @@
 	  if (bit) {
 	    data[j] += 1 << scanInfo.al;
 	  }
+	  if(i>=64) {
+	    return gFalse;
+	  }
 	  j = dctZigZag[i++];
 	}
 	++k;
--- xpdf/Stream.h.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/Stream.h	2010-08-16 14:02:38.000000000 -0700
@@ -41,7 +41,8 @@
   streamCSNone,
   streamCSDeviceGray,
   streamCSDeviceRGB,
-  streamCSDeviceCMYK
+  streamCSDeviceCMYK,
+  streamCSDeviceRGBX
 };
 
 //------------------------------------------------------------------------
--- xpdf/TextOutputDev.h.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/TextOutputDev.h	2010-08-16 14:02:38.000000000 -0700
@@ -170,6 +170,7 @@
   friend class TextFlow;
   friend class TextWordList;
   friend class TextPage;
+  friend class XMLOutputDev;
 };
 
 //------------------------------------------------------------------------
--- xpdf/gfile.cc.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/gfile.cc	2010-08-16 14:02:38.000000000 -0700
@@ -439,6 +439,52 @@
 #endif
 }
 
+static char* getTempDir()
+{
+#ifdef WIN32
+    char*dir = getenv("TMP");
+    if(!dir) dir = getenv("TEMP");
+    if(!dir) dir = getenv("tmp");
+    if(!dir) dir = getenv("temp");
+    if(!dir) dir = "C:\\";
+#else
+    char* dir = "/tmp/";
+#endif
+    return dir;
+}
+
+char* mktmpname(char*ptr) {
+    static char tmpbuf[128];
+    char*dir = getTempDir();
+    int l = strlen(dir);
+    char*sep = "";
+    if(!ptr)
+	ptr = tmpbuf;
+    if(l && dir[l-1]!='/' && dir[l-1]!='\\') {
+#ifdef WIN32
+	sep = "\\";
+#else
+	sep = "/";
+#endif
+    }
+
+ //   used to be mktemp. This does remove the warnings, but
+ //   It's not exactly an improvement.
+#ifdef HAVE_LRAND48
+    sprintf(ptr, "%s%s%08x%08x",dir,sep,(unsigned int)lrand48(),(unsigned int)lrand48());
+#else
+#   ifdef HAVE_RAND
+	sprintf(ptr, "%s%s%08x%08x",dir,sep,rand(),rand());
+#   else
+	static int count = 1;
+	sprintf(ptr, "%s%s%08x%04x%04x",dir,sep,time(0),(unsigned int)tmpbuf^((unsigned int)tmpbuf)>>16,count);
+	count ++;
+#   endif
+#endif
+     return ptr;
+}
+
+
 GBool openTempFile(GString **name, FILE **f, char *mode, char *ext) {
 #if defined(WIN32)
   //---------- Win32 ----------
@@ -460,7 +506,7 @@
   s->append("x");
   t = (int)time(NULL);
   for (i = 0; i < 1000; ++i) {
-    sprintf(buf, "%d", t + i);
+    sprintf(buf, "%08x-%08x", t + i, GetCurrentThreadId());
     s2 = s->copy()->append(buf);
     if (ext) {
       s2->append(ext);
@@ -468,8 +514,7 @@
     if (!(f2 = fopen(s2->getCString(), "r"))) {
       if (!(f2 = fopen(s2->getCString(), mode))) {
 	delete s2;
-	delete s;
-	return gFalse;
+        continue;
       }
       *name = s2;
       *f = f2;
@@ -479,6 +524,7 @@
     fclose(f2);
     delete s2;
   }
+  fprintf(stderr, "Couldn't create temporary file\n");
   delete s;
   return gFalse;
 #elif defined(VMS) || defined(__EMX__) || defined(ACORN) || defined(MACOS)
@@ -489,7 +535,7 @@
   // with this file name after the tmpnam call and before the fopen
   // call.  I will happily accept fixes to this function for non-Unix
   // OSs.
-  if (!(s = tmpnam(NULL))) {
+  if (!(s = mktmpname(NULL))) {
     return gFalse;
   }
   *name = new GString(s);
@@ -516,7 +562,7 @@
     (*name)->append("/XXXXXX")->append(ext);
     fd = mkstemps((*name)->getCString(), strlen(ext));
 #else
-    if (!(s = tmpnam(NULL))) {
+    if (!(s = mktmpname(NULL))) {
       return gFalse;
     }
     *name = new GString(s);
@@ -533,7 +579,7 @@
     (*name)->append("/XXXXXX");
     fd = mkstemp((*name)->getCString());
 #else // HAVE_MKSTEMP
-    if (!(s = tmpnam(NULL))) {
+    if (!(s = mktmpname(NULL))) {
       return gFalse;
     }
     *name = new GString(s);
--- xpdf/gfile.h.orig	2010-08-16 14:02:38.000000000 -0700
+++ xpdf/gfile.h	2010-08-16 14:02:38.000000000 -0700
@@ -58,6 +58,9 @@
 // Get current directory.
 extern GString *getCurrentDir();
 
+/* create a temporary filename */
+char* mktmpname(char*ptr);
+
 // Append a file name to a path string.  <path> may be an empty
 // string, denoting the current directory).  Returns <path>.
 extern GString *appendToPath(GString *path, char *fileName);