summaryrefslogtreecommitdiff
path: root/Build/source/texk/dvipdfm-x/pdfobj.c
blob: a1acf1b5e2581612a23842ad8f001e169bc420e8 (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
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
/* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks.

    Copyright (C) 2007-2019 by Jin-Hwan Cho and Shunsaku Hirata,
    the dvipdfmx project team.
    
    Copyright (C) 1998, 1999 by Mark A. Wicks <mwicks@kettering.edu>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <ctype.h>
#include <string.h>
/* floor and abs */
#include <math.h>

#include "system.h"
#include "mem.h"
#include "error.h"
#include "mfileio.h"
#include "dpxconf.h"
#include "dpxutil.h"
#include "pdflimits.h"
#include "pdfencrypt.h"
#include "pdfparse.h"

#ifdef HAVE_ZLIB
#include <zlib.h>
#endif /* HAVE_ZLIB */

#include "pdfobj.h"
#include "pdfdev.h"

#define STREAM_ALLOC_SIZE      4096u
#define ARRAY_ALLOC_SIZE       256
#define IND_OBJECTS_ALLOC_SIZE 512

#define OBJ_NO_OBJSTM   (1 << 0)
/* Objects with this flag will not be put into an object stream.
   For instance, all stream objects have this flag set.          */
#define OBJ_NO_ENCRYPT  (1 << 1)
/* Objects with this flag will not be encrypted.
   This implies OBJ_NO_OBJSTM if encryption is turned on.        */

/* Any of these types can be represented as follows */
struct pdf_obj 
{
  int type;

  unsigned int   label;  /* Only used for indirect objects
			    all other "label" to zero */
  unsigned short generation;  /* Only used if "label" is used */
  unsigned refcount;  /* Number of links to this object */
  int      flags;
  void    *data;

#if defined(PDFOBJ_DEBUG)
  int      obj_id;
#endif
};

#if defined(PDFOBJ_DEBUG)
static pdf_obj *bucket[65535];
static int cur_obj_id = 0;
#endif

struct pdf_boolean
{
  char  value;
};

struct pdf_number
{
  double value;
};

struct pdf_string
{
  unsigned char *string;
  unsigned short length;
};

struct pdf_name
{
  char *name;
};

struct pdf_array
{
  unsigned int max;
  unsigned int size;
  struct pdf_obj **values;
};

struct pdf_dict
{
  struct pdf_obj  *key;
  struct pdf_obj  *value;
  struct pdf_dict *next;
};

/* DecodeParms for FlateDecode */
 struct decode_parms {
  int     predictor;
  int     colors;
  int     bits_per_component;
  int32_t columns;
 };

/* 2015/12/27 Added support for predictor functions
 *
 * There are yet no way to specify the use of predictor functions.
 * Using TIFF2 or PNG predictor usually gives better compression for images
 * but there is a case that compression speed becomes significantly slower.
 * Please use -C 0x20 option to disable the use of predictor functions.
 *
 * See, e.g., for a heuristic approach for selecting filters
 *   http://www.w3.org/TR/PNG-Encoders.html#E.Filter-selection
 */

struct pdf_stream
{
  struct pdf_obj     *dict;
  unsigned char      *stream;
  int                *objstm_data;    /* used for object streams */
  unsigned int        stream_length;
  unsigned int        max_length;
  int32_t             _flags;
  struct decode_parms decodeparms;
};

struct pdf_indirect
{
  pdf_file      *pf;
  pdf_obj       *obj;             /* used when PF == NULL */
  unsigned       label;
  unsigned short generation;
};

typedef void                pdf_null;
typedef struct pdf_boolean  pdf_boolean;
typedef struct pdf_number   pdf_number;
typedef struct pdf_string   pdf_string;
typedef struct pdf_name     pdf_name;
typedef struct pdf_array    pdf_array;
typedef struct pdf_dict     pdf_dict;
typedef struct pdf_stream   pdf_stream;
typedef struct pdf_indirect pdf_indirect;

static FILE *pdf_output_file = NULL;

static int pdf_output_file_position = 0;
static int pdf_output_line_position = 0;
static int compression_saved        = 0;

#define FORMAT_BUF_SIZE 4096
static char format_buffer[FORMAT_BUF_SIZE];

typedef struct xref_entry
{
  unsigned char  type;       /* object storage type              */
  unsigned int   field2;     /* offset in file or object stream  */
  unsigned short field3;     /* generation or index              */
  pdf_obj       *direct;     /* used for imported objects        */
  pdf_obj       *indirect;   /* used for imported objects        */
} xref_entry;

static xref_entry *output_xref;

static unsigned int pdf_max_ind_objects;
static unsigned int next_label;

static unsigned int startxref;

struct pdf_file
{
  FILE       *file;
  pdf_obj    *trailer;
  xref_entry *xref_table;
  pdf_obj    *catalog;
  int         num_obj;
  int         file_size;
  int         version;
};

static pdf_obj *output_stream;

#define OBJSTM_MAX_OBJS  200
/* the limit is only 100 for linearized PDF */

static int enc_mode;
static int doc_enc_mode;

static pdf_obj *trailer_dict;
static pdf_obj *xref_stream;

/* Internal static routines */

static int check_for_pdf_version (FILE *file);

static void pdf_flush_obj (pdf_obj *object, FILE *file);
static void pdf_label_obj (pdf_obj *object);
static void pdf_write_obj (pdf_obj *object, FILE *file);

static void  set_objstm_data (pdf_obj *objstm, int *data);
static int  *get_objstm_data (pdf_obj *objstm);
static void  release_objstm  (pdf_obj *objstm);

static void pdf_out_char (FILE *file, char c);
static void pdf_out      (FILE *file, const void *buffer, int length);

static pdf_obj *pdf_new_ref  (pdf_obj *object);
static void release_indirect (pdf_indirect *data);
static void write_indirect   (pdf_indirect *indirect, FILE *file);

static void release_boolean (pdf_obj *data);
static void write_boolean   (pdf_boolean *data, FILE *file);

static void write_null   (FILE *file);

static void release_number (pdf_number *number);
static void write_number   (pdf_number *number, FILE *file);

static void write_string   (pdf_string *str, FILE *file);
static void release_string (pdf_string *str);

static void write_name   (pdf_name *name, FILE *file);
static void release_name (pdf_name *name);

static void write_array   (pdf_array *array, FILE *file);
static void release_array (pdf_array *array);

static void write_dict   (pdf_dict *dict, FILE *file);
static void release_dict (pdf_dict *dict);

static void write_stream   (pdf_stream *stream, FILE *file);
static void release_stream (pdf_stream *stream);

static char compression_level = 9;
static char compression_use_predictor = 1;

void
pdf_set_compression (int level)
{
#ifndef   HAVE_ZLIB
  ERROR("You don't have compression compiled in. Possibly libz wasn't found by configure.");
#else
#ifndef HAVE_ZLIB_COMPRESS2
  if (level != 0) 
    WARN("Unable to set compression level -- your zlib doesn't have compress2().");
#endif
  if (level >= 0 && level <= 9) 
    compression_level = level;
  else {
    ERROR("set_compression: invalid compression level: %d", level);
  }
#endif /* !HAVE_ZLIB */

  return;
}

FILE *
pdf_get_output_file (void)
{
  return pdf_output_file;
}

static int pdf_version = PDF_VERSION_DEFAULT;

void
pdf_set_version (int version)
{
  /* Don't forget to update CIDFont_stdcc_def[] in cid.c too! */
  if (version >= PDF_VERSION_MIN && version <= PDF_VERSION_MAX) {
    pdf_version = version;
  }
}

int
pdf_get_version (void)
{
  return pdf_version;
}

int
pdf_get_version_major (void)
{
  return pdf_version/10;
}

int
pdf_get_version_minor (void)
{
  return pdf_version%10;
}

int
pdf_check_version (int major, int minor)
{
  return (pdf_version >= major*10+minor) ? 0 : -1;
}

static pdf_obj *current_objstm = NULL;
static int do_objstm;

static void
add_xref_entry (unsigned label, unsigned char type, unsigned int field2, unsigned short field3)
{
  if (label >= pdf_max_ind_objects) {
    pdf_max_ind_objects = (label/IND_OBJECTS_ALLOC_SIZE+1)*IND_OBJECTS_ALLOC_SIZE;
    output_xref = RENEW(output_xref, pdf_max_ind_objects, xref_entry);
  }

  output_xref[label].type   = type;
  output_xref[label].field2 = field2;
  output_xref[label].field3 = field3;
  output_xref[label].direct   = NULL;
  output_xref[label].indirect = NULL;
}

#define BINARY_MARKER "%\344\360\355\370\n"
void
pdf_out_init (const char *filename,
              int do_encryption, int enable_objstm, int enable_predictor)
{
  char v;

  output_xref = NULL;
  pdf_max_ind_objects = 0;
  add_xref_entry(0, 0, 0, 0xffff);
  next_label = 1;

  if (pdf_version >= 15) {
    if (enable_objstm) {
      xref_stream = pdf_new_stream(STREAM_COMPRESS);
      xref_stream->flags |= OBJ_NO_ENCRYPT;
      trailer_dict = pdf_stream_dict(xref_stream);
      pdf_add_dict(trailer_dict, pdf_new_name("Type"), pdf_new_name("XRef"));
      do_objstm = 1;
    } else {
      trailer_dict = pdf_new_dict();
      do_objstm = 0;
    }
  } else {
    xref_stream = NULL;
    trailer_dict = pdf_new_dict();
    do_objstm = 0;
  }

  output_stream = NULL;

  if (filename == NULL) { /* no filename: writing to stdout */
#ifdef WIN32
    setmode(fileno(stdout), _O_BINARY);
#endif
    pdf_output_file = stdout;
  } else {
    pdf_output_file = MFOPEN(filename, FOPEN_WBIN_MODE);
    if (!pdf_output_file) {
      if (strlen(filename) < 128)
        ERROR("Unable to open \"%s\".", filename);
      else
        ERROR("Unable to open file.");
    }
  }
  pdf_out(pdf_output_file, "%PDF-", strlen("%PDF-"));
  v = '0' + (pdf_version / 10);
  pdf_out(pdf_output_file, &v, 1);
  pdf_out(pdf_output_file, ".", 1);
  v = '0' + (pdf_version % 10);
  pdf_out(pdf_output_file, &v, 1);
  pdf_out(pdf_output_file, "\n", 1);
  pdf_out(pdf_output_file, BINARY_MARKER, strlen(BINARY_MARKER));

  enc_mode = 0;
  doc_enc_mode = do_encryption;
  compression_use_predictor = enable_predictor;
}

static void
dump_xref_table (void)
{
  int length;
  unsigned int i;

  pdf_out(pdf_output_file, "xref\n", 5);

  length = sprintf(format_buffer, "%d %u\n", 0, next_label);
  pdf_out(pdf_output_file, format_buffer, length);

  /*
   * Every space counts.  The space after the 'f' and 'n' is * *essential*.
   * The PDF spec says the lines must be 20 characters long including the
   * end of line character.
   */
  for (i = 0; i < next_label; i++) {
    unsigned char type = output_xref[i].type;
    if (type > 1)
      ERROR("object type %hu not allowed in xref table", type);
    length = sprintf(format_buffer, "%010u %05hu %c \n",
		     output_xref[i].field2, output_xref[i].field3,
		     type ? 'n' : 'f');
    pdf_out(pdf_output_file, format_buffer, length);
  }
}

static void
dump_trailer_dict (void)
{
  pdf_out(pdf_output_file, "trailer\n", 8);
  enc_mode = 0;
  write_dict(trailer_dict->data, pdf_output_file);
  pdf_release_obj(trailer_dict);
  pdf_out_char(pdf_output_file, '\n');
}

/*
 * output a PDF 1.5 cross-reference stream;
 * contributed by Matthias Franz (March 21, 2007)
 */
static void
dump_xref_stream (void)
{
  unsigned int pos, i;
  unsigned poslen;
  unsigned char buf[7] = {0, 0, 0, 0, 0};

  pdf_obj *w;

  /* determine the necessary size of the offset field */
  pos = startxref; /* maximal offset value */
  poslen = 1;
  while (pos >>= 8)
    poslen++;

  w = pdf_new_array();
  pdf_add_array(w, pdf_new_number(1));      /* type                */
  pdf_add_array(w, pdf_new_number(poslen)); /* offset (big-endian) */
  pdf_add_array(w, pdf_new_number(2));      /* generation          */
  pdf_add_dict(trailer_dict, pdf_new_name("W"), w);

  /* We need the xref entry for the xref stream right now */
  add_xref_entry(next_label-1, 1, startxref, 0);

  for (i = 0; i < next_label; i++) {
    unsigned j;
    unsigned short f3;
    buf[0] = output_xref[i].type;
    pos = output_xref[i].field2;
    for (j = poslen; j--; ) {
      buf[1+j] = (unsigned char) pos;
      pos >>= 8;
    }
    f3 = output_xref[i].field3;
    buf[poslen+1] = (unsigned char) (f3 >> 8);
    buf[poslen+2] = (unsigned char) (f3);
    pdf_add_stream(xref_stream, &buf, poslen+3);
  }

  pdf_release_obj(xref_stream);
}

#if defined(LIBDPX)
static long pdf_output_file_stats = 0;
long
pdf_output_stats (void)
{
  return pdf_output_file_stats;
}
#endif /* LIBDPX */

void
pdf_out_flush (void)
{
  if (pdf_output_file) {
    int length;

    /* Flush current object stream */
    if (current_objstm) {
      release_objstm(current_objstm);
      current_objstm =NULL;
    }

    /*
     * Label xref stream - we need the number of correct objects
     * for the xref stream dictionary (= trailer).
     * Labelling it in pdf_out_init (with 1)  does not work (why?).
     */
    if (xref_stream)
      pdf_label_obj(xref_stream);

    /* Record where this xref is for trailer */
    startxref = pdf_output_file_position;

    pdf_add_dict(trailer_dict, pdf_new_name("Size"),
		 pdf_new_number(next_label));

    if (xref_stream)
      dump_xref_stream();
    else {
      dump_xref_table();
      dump_trailer_dict();
    }

    /* Done with xref table */
    RELEASE(output_xref);

    pdf_out(pdf_output_file, "startxref\n", 10);
    length = sprintf(format_buffer, "%u\n", startxref);
    pdf_out(pdf_output_file, format_buffer, length);
    pdf_out(pdf_output_file, "%%EOF\n", 6);

#if !defined(LIBDPX)
    MESG("\n");
#endif /* !LIBDPX */
    if (dpx_conf.verbose_level > 0) {
      if (compression_level > 0) {
	MESG("Compression saved %ld bytes%s\n", compression_saved,
	     pdf_version < 15 ? ". Try \"-V 1.5\" for better compression" : "");
      }
    }
#if !defined(LIBDPX)
    MESG("%ld bytes written", pdf_output_file_position);
#else
    pdf_output_file_stats = pdf_output_file_position;
#endif /* !LIBDPX */

    MFCLOSE(pdf_output_file);
    pdf_output_file_position = 0;
    pdf_output_line_position = 0;
    pdf_output_file = NULL;
  }
#if defined(PDFOBJ_DEBUG)
  {
    int i;
    MESG("\ndebug>> %d PDF objects created.", cur_obj_id);
    for (i = 0; i < cur_obj_id; i++) {
      pdf_obj *obj = bucket[i];
      if (obj) {
        if (obj->label > 0) {
          WARN("Object obj_id=<%lu, %u> unreleased...", obj->label, obj->generation);
          WARN("Reference count=%d", obj->refcount);
        } else {
          WARN("Unreleased object found: %d", i);
          pdf_write_obj(obj, stderr);
          MESG("\n");
        }
      }
    }
  }
#endif
}

void
pdf_error_cleanup (void)
{
  /*
   * This routine is the cleanup required for an abnormal exit.
   * For now, simply close the file.
   */
  if (pdf_output_file)
    MFCLOSE(pdf_output_file);
  pdf_output_file = NULL;
}


void
pdf_set_root (pdf_obj *object)
{
  if (pdf_add_dict(trailer_dict, pdf_new_name("Root"), pdf_ref_obj(object))) {
    ERROR("Root object already set!");
  }
  /* Adobe Readers don't like a document catalog inside an encrypted
   * object stream, although the PDF v1.5 spec seems to allow this.
   * Note that we don't set OBJ_NO_ENCRYPT since the name dictionary in
   * a document catalog may contain strings, which should be encrypted.
   */
  if (doc_enc_mode)
    object->flags |= OBJ_NO_OBJSTM;
}

void
pdf_set_info (pdf_obj *object)
{
  if (pdf_add_dict(trailer_dict, pdf_new_name("Info"), pdf_ref_obj(object))) {
    ERROR ("Info object already set!");
  }
}

void
pdf_set_id (pdf_obj *id)
{
  if (pdf_add_dict(trailer_dict, pdf_new_name("ID"), id)) {
    ERROR ("ID already set!");
  }
}

void
pdf_set_encrypt (pdf_obj *encrypt)
{
  if (pdf_add_dict(trailer_dict, pdf_new_name("Encrypt"), pdf_ref_obj(encrypt))) {
    ERROR("Encrypt object already set!");
  }
  encrypt->flags |= OBJ_NO_ENCRYPT;
}

static
void pdf_out_char (FILE *file, char c)
{
  if (output_stream && file ==  pdf_output_file)
    pdf_add_stream(output_stream, &c, 1);
  else {
    fputc(c, file);
    /* Keep tallys for xref table *only* if writing a pdf file. */
    if (file == pdf_output_file) {
      pdf_output_file_position += 1;
      if (c == '\n')
        pdf_output_line_position  = 0;
      else
        pdf_output_line_position += 1;
    }
  }
}

static char xchar[] = "0123456789abcdef";

#define pdf_out_xchar(f,c) do {\
  pdf_out_char((f), xchar[((c) >> 4) & 0x0f]);\
  pdf_out_char((f), xchar[(c) & 0x0f]);\
} while (0)

static
void pdf_out (FILE *file, const void *buffer, int length)
{
  if (output_stream && file ==  pdf_output_file)
    pdf_add_stream(output_stream, buffer, length);
  else {
    fwrite(buffer, 1, length, file);
    /* Keep tallys for xref table *only* if writing a pdf file */
    if (file == pdf_output_file) {
      pdf_output_file_position += length;
      pdf_output_line_position += length;
      /* "foo\nbar\n "... */
      if (length > 0 &&
	((const char *)buffer)[length-1] == '\n')
        pdf_output_line_position = 0;
    }
  }
}

/*  returns 1 if a white-space character is necessary to separate
    an object of type1 followed by an object of type2              */
static
int pdf_need_white (int type1, int type2)
{
  return !(type1 == PDF_STRING || type1 == PDF_ARRAY || type1 == PDF_DICT ||
	   type2 == PDF_STRING || type2 == PDF_NAME ||
	   type2 == PDF_ARRAY || type2 == PDF_DICT);
}

static
void pdf_out_white (FILE *file)
{
  if (file == pdf_output_file && pdf_output_line_position >= 80) {
    pdf_out_char(file, '\n');
  } else {
    pdf_out_char(file, ' ');
  }
}

#define TYPECHECK(o,t) if (!(o) || (o)->type != (t)) {\
  ERROR("typecheck: Invalid object type: %d %d (line %d)", (o) ? (o)->type : -1, (t), __LINE__);\
}

#define INVALIDOBJ(o)  ((o) == NULL || (o)->type <= 0 || (o)->type > PDF_UNDEFINED)

static pdf_obj *
pdf_new_obj(int type)
{
  pdf_obj *result;

  if (type > PDF_UNDEFINED || type < 0)
    ERROR("Invalid object type: %d", type);

  result = NEW(1, pdf_obj);
  result->type  = type;
  result->data  = NULL;
  result->label      = 0;
  result->generation = 0;
  result->refcount   = 1;
  result->flags      = 0;

#if defined(PDFOBJ_DEBUG)
  result->obj_id = cur_obj_id;
  bucket[cur_obj_id] = result;
  cur_obj_id++;
#endif

  return result;
}

int
pdf_obj_typeof (pdf_obj *object)
{
  if (INVALIDOBJ(object))
    return PDF_OBJ_INVALID;

  return object->type;
}

static void
pdf_label_obj (pdf_obj *object)
{
  if (INVALIDOBJ(object))
    ERROR("pdf_label_obj(): passed invalid object.");

  /*
   * Don't change label on an already labeled object. Ignore such calls.
   */
  if (object->label == 0) {
    object->label      = next_label++;
    object->generation = 0;
  }
}

/*
 * Transfer the label assigned to the object src to the object dst.
 * The object dst must not yet have been labeled.
 */
void
pdf_transfer_label (pdf_obj *dst, pdf_obj *src)
{
  ASSERT(dst && !dst->label && src);

  dst->label      = src->label;
  dst->generation = src->generation;
  src->label      = 0;
  src->generation = 0;
}

/*
 * This doesn't really copy the object, but allows it to be used without
 * fear that somebody else will free it.
 */
pdf_obj *
pdf_link_obj (pdf_obj *object)
{
  if (INVALIDOBJ(object))
    ERROR("pdf_link_obj(): passed invalid object.");

  object->refcount += 1;

  return object;
}


pdf_obj *
pdf_ref_obj (pdf_obj *object)
{
  if (INVALIDOBJ(object))
    ERROR("pdf_ref_obj(): passed invalid object.");
  
  if (object->refcount == 0) {
    MESG("\nTrying to refer already released object!!!\n");
    pdf_write_obj(object, stderr);
    ERROR("Cannot continue...");
  }

  if (PDF_OBJ_INDIRECTTYPE(object)) {
    return pdf_link_obj(object);
  } else {
    return pdf_new_ref(object);
  }
}

static void
release_indirect (pdf_indirect *data)
{
  RELEASE(data);
}

static void
write_indirect (pdf_indirect *indirect, FILE *file)
{
  int length;

  ASSERT(!indirect->pf);

  length = sprintf(format_buffer, "%u %hu R", indirect->label, indirect->generation);
  pdf_out(file, format_buffer, length);
}

/* The undefined object is used as a placeholder in pdfnames.c
 * for objects which are referenced before they are defined.
 */
pdf_obj *
pdf_new_undefined (void)
{
  pdf_obj *result;

  result = pdf_new_obj(PDF_UNDEFINED);
  result->data = NULL;

  return result;
}

pdf_obj *
pdf_new_null (void)
{
  pdf_obj *result;

  result = pdf_new_obj(PDF_NULL);
  result->data = NULL;

  return result;
}

static void
write_null (FILE *file)
{
  pdf_out(file, "null", 4);
}

pdf_obj *
pdf_new_boolean (char value)
{
  pdf_obj     *result;
  pdf_boolean *data;

  result = pdf_new_obj(PDF_BOOLEAN);
  data   = NEW(1, pdf_boolean);
  data->value  = value;
  result->data = data;

  return result;
}

static void
release_boolean (pdf_obj *data)
{
  RELEASE (data);
}

static void
write_boolean (pdf_boolean *data, FILE *file)
{
  if (data->value) {
    pdf_out(file, "true", 4);
  } else {
    pdf_out(file, "false", 5);
  }
}

char
pdf_boolean_value (pdf_obj *object)
{
  pdf_boolean *data;

  TYPECHECK(object, PDF_BOOLEAN);

  data = object->data;

  return data->value;
}

pdf_obj *
pdf_new_number (double value)
{
  pdf_obj    *result;
  pdf_number *data;

  result = pdf_new_obj(PDF_NUMBER);
  data   = NEW(1, pdf_number);
  data->value  = value;
  result->data = data;

  return result;
}

static void
release_number (pdf_number *data)
{
  RELEASE (data);
}

static void
write_number (pdf_number *number, FILE *file)
{
  int count;

  count = pdf_sprint_number(format_buffer, number->value);

  pdf_out(file, format_buffer, count);
}


void
pdf_set_number (pdf_obj *object, double value)
{
  pdf_number *data;

  TYPECHECK(object, PDF_NUMBER);

  data = object->data;
  data->value = value;
}

double
pdf_number_value (pdf_obj *object)
{
  pdf_number *data;

  TYPECHECK(object, PDF_NUMBER);

  data = object->data;

  return data->value;
}

pdf_obj *
pdf_new_string (const void *str, unsigned length)
{
  pdf_obj    *result;
  pdf_string *data;

  ASSERT(str);

  result = pdf_new_obj(PDF_STRING);
  data   = NEW(1, pdf_string);
  result->data = data;
  data->length = length;

  if (length) {
    data->string = NEW(length+1, unsigned char);
    memcpy(data->string, str, length);
    /* Shouldn't assume NULL terminated. */
    data->string[length] = '\0';
  } else
    data->string = NULL;

  return result;
}

void *
pdf_string_value (pdf_obj *object)
{
  pdf_string *data;

  TYPECHECK(object, PDF_STRING);

  data = object->data;

  return data->string;
}

unsigned
pdf_string_length (pdf_obj *object)
{
  pdf_string *data;

  TYPECHECK(object, PDF_STRING);

  data = object->data;

  return (unsigned) (data->length);
}

/*
 * This routine escapes non printable characters and control
 * characters in an output string.
 */
int
pdfobj_escape_str (char *buffer, int bufsize, const unsigned char *s, int len)
{
  int result = 0;
  int i;

  for (i = 0; i < len; i++) {
    unsigned char ch;

    ch = s[i];
    if (result > bufsize - 4)
      ERROR("pdfobj_escape_str: Buffer overflow");

    /*
     * We always write three octal digits. Optimization only gives few Kb
     * smaller size for most documents when zlib compressed.
     */
    if (ch < 32 || ch > 126) {
      buffer[result++] = '\\';
#if 0
      if (i < len - 1 && !isdigit(s[i+1]))
	result += sprintf(buffer+result, "%o", ch);
      else
	result += sprintf(buffer+result, "%03o", ch);
#endif
      result += sprintf(buffer+result, "%03o", ch);
    } else {
      switch (ch) {
      case '(':
	buffer[result++] = '\\';
	buffer[result++] = '(';
	break;
      case ')':
	buffer[result++] = '\\';
	buffer[result++] = ')';
	break;
      case '\\':
	buffer[result++] = '\\';
	buffer[result++] = '\\';
	break;
      default:
	buffer[result++] = ch;
	break;
      }
    }
  }

  return result;
}

static void
write_string (pdf_string *str, FILE *file)
{
  unsigned char *s = NULL;
  char wbuf[FORMAT_BUF_SIZE]; /* Shouldn't use format_buffer[]. */
  int  nescc = 0, i, count;
  size_t len = 0;

  if (enc_mode) {
    pdf_encrypt_data(str->string, str->length, &s, &len);
  } else {
  s = str->string;
    len = str->length;
  }

  /*
   * Count all ASCII non-printable characters.
   */
  for (i = 0; i < len; i++) {
    if (!isprint(s[i]))
      nescc++;
  }
  /*
   * If the string contains much escaped chars, then we write it as
   * ASCII hex string.
   */
  if (nescc > len / 3) {
    pdf_out_char(file, '<');
    for (i = 0; i < len; i++) {
      pdf_out_xchar(file, s[i]);
    }
    pdf_out_char(file, '>');
  } else {
    pdf_out_char(file, '(');
    /*
     * This section of code probably isn't speed critical.  Escaping the
     * characters in the string one at a time may seem slow, but it's
     * safe if the formatted string length exceeds FORMAT_BUF_SIZE.
     * Occasionally you see some long strings in PDF.  pdfobj_escape_str
     * is also used for strings of text with no kerning.  These must be
     * handled as quickly as possible since there are so many of them.
     */ 
    for (i = 0; i < len; i++) {
      count = pdfobj_escape_str(wbuf, FORMAT_BUF_SIZE, &(s[i]), 1);
      pdf_out(file, wbuf, count);
    }
    pdf_out_char(file, ')');
  }
  if (enc_mode && s)
    RELEASE(s);
}

static void
release_string (pdf_string *data)
{
  if (data->string != NULL) {
    RELEASE(data->string);
    data->string = NULL;
  }
  RELEASE(data);
}

void
pdf_set_string (pdf_obj *object, unsigned char *str, unsigned length)
{
  pdf_string *data;

  TYPECHECK(object, PDF_STRING);

  data = object->data;
  if (data->string != 0) {
    RELEASE(data->string);
  }
  if (length != 0) {
    data->length = length;
    data->string = NEW(length + 1, unsigned char);
    memcpy(data->string, str, length);
    data->string[length] = '\0';
  } else {
    data->length = 0;
    data->string = NULL;
  }
}

/* Name does *not* include the /. */ 
pdf_obj *
pdf_new_name (const char *name)
{
  pdf_obj  *result;
  unsigned  length;
  pdf_name *data;

  result = pdf_new_obj(PDF_NAME);
  data   = NEW (1, pdf_name);
  result->data = data;
  length = strlen(name);
  if (length != 0) {
    data->name = NEW(length+1, char);
    memcpy(data->name, name, length);
    data->name[length] = '\0';
  } else {
    data->name = NULL;
  }

  return result;
}

static void
write_name (pdf_name *name, FILE *file)
{
  char *s;
  int i, length;

  s      = name->name;
  length = name->name ? strlen(name->name) : 0;
  /*
   * From PDF Reference, 3rd ed., p.33:
   *
   *  Beginning with PDF 1.2, any character except null (character code 0)
   *  may be included in a name by writing its 2-digit hexadecimal code,
   *  preceded bythe number sign character (#); see implementation notes 3
   *  and 4 in Appendix H. This syntax is required in order to represent
   *  any of the delimiter or white-space characters or the number sign
   *  character itself; it is recommended but not required for characters
   *  whose codes are outside the range 33 (!) to 126 (~).
   */
#ifndef is_delim
  /* Avoid '{' and '}' for PostScript compatibility? */
#define is_delim(c) ((c) == '(' || (c) == ')' || \
                     (c) == '/' || \
                     (c) == '<' || (c) == '>' || \
                     (c) == '[' || (c) == ']' || \
                     (c) == '{' || (c) == '}' || \
                     (c) == '%')
#endif
  pdf_out_char(file, '/');
  for (i = 0; i < length; i++) {
    if (s[i] < '!' || s[i] > '~' || s[i] == '#' || is_delim(s[i])) {
      /*     ^ "space" is here. */
      pdf_out_char (file, '#');
      pdf_out_xchar(file, s[i]);
    } else {
      pdf_out_char (file, s[i]);
    }
  }
}

static void
release_name (pdf_name *data)
{
  if (data->name != NULL) {
    RELEASE(data->name);
    data->name = NULL;
  }
  RELEASE(data);
}

char *
pdf_name_value (pdf_obj *object)
{
  pdf_name *data;

  TYPECHECK(object, PDF_NAME);

  data = object->data;

  return data->name;
}

/*
 * We do not have pdf_name_length() since '\0' is not allowed
 * in PDF name object.
 */

pdf_obj *
pdf_new_array (void)
{
  pdf_obj   *result;
  pdf_array *data;

  result = pdf_new_obj(PDF_ARRAY);
  data   = NEW(1, pdf_array);
  data->values = NULL;
  data->max    = 0;
  data->size   = 0;
  result->data = data;

  return result;
}

static void
write_array (pdf_array *array, FILE *file)
{
  pdf_out_char(file, '[');
  if (array->size > 0) {
    unsigned int i;
    int type1 = PDF_UNDEFINED, type2;
    
    for (i = 0; i < array->size; i++) {
      if (array->values[i]) {
	type2 = array->values[i]->type;
	if (type1 != PDF_UNDEFINED && pdf_need_white(type1, type2))
	  pdf_out_white(file);
	type1 = type2;
	pdf_write_obj(array->values[i], file);
      } else
	WARN("PDF array element #ld undefined.", i);
    }
  }
  pdf_out_char(file, ']');
}

pdf_obj *
pdf_get_array (pdf_obj *array, int idx)
{
  pdf_obj   *result = NULL;
  pdf_array *data;

  TYPECHECK(array, PDF_ARRAY);

  data = array->data;
  if (idx < 0)
    result = data->values[idx + data->size];
  else if (idx < data->size) {
    result = data->values[idx];
  }

  return result;
}

unsigned int
pdf_array_length (pdf_obj *array)
{
  pdf_array *data;

  TYPECHECK(array, PDF_ARRAY);

  data = (pdf_array *) array->data;

  return (unsigned int) data->size;
}

static void
release_array (pdf_array *data)
{
  unsigned int i;

  if (data->values) {
    for (i = 0; i < data->size; i++) {
      pdf_release_obj(data->values[i]);
      data->values[i] = NULL;
    }
    RELEASE(data->values);
    data->values = NULL;
  }
  RELEASE(data);
}

/*
 * The name pdf_add_array is misleading. It behaves differently than
 * pdf_add_dict(). This should be pdf_push_array().
 */
void
pdf_add_array (pdf_obj *array, pdf_obj *object)
{
  pdf_array *data;

  TYPECHECK(array, PDF_ARRAY);

  data = array->data;
  if (data->size >= data->max) {
    data->max   += ARRAY_ALLOC_SIZE;
    data->values = RENEW(data->values, data->max, pdf_obj *);
  }
  data->values[data->size] = object;
  data->size++;

  return;
}

#if 0
void
pdf_put_array (pdf_obj *array, unsigned idx, pdf_obj *object)
{
  pdf_array *data;
  int        i;

  TYPECHECK(array, PDF_ARRAY);

  data = array->data;
  if (idx + 1 > data->max) {
    data->max   += ARRAY_ALLOC_SIZE;
    data->values = RENEW(data->values, data->max, pdf_obj *);
  }
  /*
   * Rangecheck error in PostScript interpreters if
   * idx > data->size - 1. But pdf_new_array() doesn't set
   * array size, pdf_add_array() dynamically increases size
   * of array. This might confusing...
   */
  if (idx + 1 > data->size) {
    for (i = data->size; i < idx; i++)
      data->values[i] = pdf_new_null(); /* release_array() won't work without this */
    data->values[idx] = object;
    data->size = idx + 1;
  } else {
    if (data->values[idx])
      pdf_release_obj(data->values[idx]);
    data->values[idx] = object;
  }
}

/* Easily leaks memory... */
pdf_obj *
pdf_shift_array (pdf_obj *array)
{
  pdf_obj   *result = NULL;
  pdf_array *data;

  TYPECHECK(array, PDF_ARRAY);

  data = array->data;
  if (data->size > 0) {
    int i;

    result = data->values[0];
    for (i = 1; i < data->size; i++)
      data->values[i-1] = data->values[i];
    data->size--;
  }

  return result;
}
#endif

/* Prepend an object to an array */
static void
pdf_unshift_array (pdf_obj *array, pdf_obj *object)
{
  pdf_array *data;

  TYPECHECK(array, PDF_ARRAY);

  data = array->data;
  if (data->size >= data->max) {
    data->max   += ARRAY_ALLOC_SIZE;
    data->values = RENEW(data->values, data->max, pdf_obj *);
  }
  memmove(&data->values[1], data->values, data->size * sizeof(pdf_obj *));
  data->values[0] = object;
  data->size++;
}

#if 0
pdf_obj *
pdf_pop_array (pdf_obj *array)
{
  pdf_obj   *result;
  pdf_array *data;

  TYPECHECK(array, PDF_ARRAY);

  data = array->data;
  if (data->size > 0) {
    result = data->values[data->size - 1];
    data->size--;
  } else {
    result = NULL;
  }

  return result;
}
#endif

static void
write_dict (pdf_dict *dict, FILE *file)
{
#if 0
  pdf_out (file, "<<\n", 3); /* dropping \n saves few kb. */
#else
  pdf_out (file, "<<", 2);
#endif
  while (dict->key != NULL) {
    pdf_write_obj(dict->key, file);
    if (pdf_need_white(PDF_NAME, (dict->value)->type)) {
      pdf_out_white(file);
    }
    pdf_write_obj(dict->value, file);
#if 0
    pdf_out_char (file, '\n'); /* removing this saves few kb. */
#endif
    dict = dict->next;
  }
  pdf_out (file, ">>", 2);
}

pdf_obj *
pdf_new_dict (void)
{
  pdf_obj  *result;
  pdf_dict *data;

  result = pdf_new_obj(PDF_DICT);
  data   = NEW(1, pdf_dict);
  data->key    = NULL;
  data->value  = NULL;
  data->next   = NULL;
  result->data = data;

  return result;
}

static void
release_dict (pdf_dict *data)
{
  pdf_dict *next;

  while (data != NULL && data->key != NULL) {
    pdf_release_obj(data->key);
    pdf_release_obj(data->value);
    data->key   = NULL;
    data->value = NULL;
    next = data->next;
    RELEASE(data);
    data = next;
  }
  RELEASE(data);
}

/* Array is ended by a node with NULL this pointer */
/* pdf_add_dict returns 0 if the key is new and non-zero otherwise */
int
pdf_add_dict (pdf_obj *dict, pdf_obj *key, pdf_obj *value)
{
  pdf_dict *data, *new_node;

  TYPECHECK(dict, PDF_DICT);
  TYPECHECK(key,  PDF_NAME);

  /* It seems that NULL is sometimes used for null object... */
  if (value != NULL && INVALIDOBJ(value))
    ERROR("pdf_add_dict(): Passed invalid value");

  /* If this key already exists, simply replace the value */
  for (data = dict->data; data->key != NULL; data = data->next) {
    if (!strcmp(pdf_name_value(key), pdf_name_value(data->key))) {
      /* Release the old value */
      pdf_release_obj(data->value);
      /* Release the new key (we don't need it) */
      pdf_release_obj(key);
      data->value = value;
      return 1;
    }
  }
  /*
   * We didn't find the key. We build a new "end" node and add
   * the new key just before the end
   */
  new_node = NEW (1, pdf_dict);
  new_node->key = NULL;
  new_node->value = NULL;
  new_node->next = NULL;
  data->next  = new_node;
  data->key   = key;
  data->value = value;
  return 0;
}

#if 0
void
pdf_put_dict (pdf_obj *dict, const char *key, pdf_obj *value)
{
  pdf_dict *data;

  TYPECHECK(dict, PDF_DICT);

  if (!key) {
    ERROR("pdf_put_dict(): Passed invalid key.");
  }
  /* It seems that NULL is sometimes used for null object... */
  if (value != NULL && INVALIDOBJ(value)) {
    ERROR("pdf_add_dict(): Passed invalid value.");
  }

  data = dict->data;

  while (data->key != NULL) {
    if (!strcmp(key, pdf_name_value(data->key))) {
      pdf_release_obj(data->value);
      data->value = value;
      break;
    }
    data = data->next;
  }

  /*
   * If we didn't find the key, build a new "end" node and add
   * the new key just before the end
   */
  if (data->key == NULL) {
    pdf_dict *new_node;

    new_node = NEW (1, pdf_dict);
    new_node->key   = NULL;
    new_node->value = NULL;
    new_node->next  = NULL;
    data->next  = new_node;
    data->key   = pdf_new_name(key);
    data->value = value;
  }
}
#endif

/* pdf_merge_dict makes a link for each item in dict2 before stealing it */
void
pdf_merge_dict (pdf_obj *dict1, pdf_obj *dict2)
{
  pdf_dict *data;

  TYPECHECK(dict1, PDF_DICT);
  TYPECHECK(dict2, PDF_DICT);

  data = dict2->data;
  while (data->key != NULL) {
    pdf_add_dict(dict1, pdf_link_obj(data->key), pdf_link_obj(data->value));
    data = data->next;
  }
}

int
pdf_foreach_dict (pdf_obj *dict,
		  int (*proc) (pdf_obj *, pdf_obj *, void *), void *pdata)
{
  int       error = 0;
  pdf_dict *data;

  ASSERT(proc);

  TYPECHECK(dict, PDF_DICT);

  data = dict->data;
  while (!error &&
	 data->key != NULL) {
    error = proc(data->key, data->value, pdata);
    data = data->next;
  }

  return error;
}

#define pdf_match_name(o,s) ((o) && (s) && !strcmp(((pdf_name *)(o)->data)->name, (s)))
pdf_obj *
pdf_lookup_dict (pdf_obj *dict, const char *name)
{
  pdf_dict *data;

  ASSERT(name);

  TYPECHECK(dict, PDF_DICT);

  data = dict->data;
  while (data->key != NULL) {
    if (!strcmp(name, pdf_name_value(data->key))) {
      return data->value;
    }
    data = data->next;
  }

  return NULL;
}

/* Returns array of dictionary keys */
pdf_obj *
pdf_dict_keys (pdf_obj *dict)
{
  pdf_obj  *keys;
  pdf_dict *data;

  TYPECHECK(dict, PDF_DICT);

  keys = pdf_new_array();
  for (data = dict->data; (data &&
			   data->key != NULL); data = data->next) {
    /* We duplicate name object rather than linking keys.
     * If we forget to free keys, broken PDF is generated.
     */
    pdf_add_array(keys, pdf_new_name(pdf_name_value(data->key)));
  }

  return keys;
}

void
pdf_remove_dict (pdf_obj *dict, const char *name)
{
  pdf_dict *data, **data_p;

  TYPECHECK(dict, PDF_DICT);

  data   = dict->data;
  data_p = (pdf_dict **) (void *) &(dict->data);
  while (data->key != NULL) {
    if (pdf_match_name(data->key, name)) {
      pdf_release_obj(data->key);
      pdf_release_obj(data->value);
      *data_p = data->next;
      RELEASE(data);
      break;
    }
    data_p = &(data->next);
    data   = data->next;
  }
}

pdf_obj *
pdf_new_stream (int flags)
{
  pdf_obj    *result;
  pdf_stream *data;

  result = pdf_new_obj(PDF_STREAM);
  data   = NEW(1, pdf_stream);
  /*
   * Although we are using an arbitrary pdf_object here, it must have
   * type=PDF_DICT and cannot be an indirect reference.  This will be
   * checked by the output routine.
   */
  data->dict   = pdf_new_dict();
  data->_flags = flags;
  data->stream = NULL;
  data->stream_length = 0;
  data->max_length    = 0;
  data->objstm_data = NULL;

  data->decodeparms.predictor = 2;
  data->decodeparms.columns   = 0;
  data->decodeparms.bits_per_component = 0;
  data->decodeparms.colors    = 0;

  result->data = data;
  result->flags |= OBJ_NO_OBJSTM;

  return result;
}

void
pdf_stream_set_predictor (pdf_obj *stream,
                          int predictor, int32_t columns, int bpc, int colors)
{
  struct pdf_stream *data;

  if (pdf_obj_typeof(stream) != PDF_STREAM)
    return;
  else if (columns < 0 || bpc < 0 || colors < 0)
    return;

  data = (struct pdf_stream *) stream->data;
  data->decodeparms.predictor = predictor;
  data->decodeparms.columns   = columns;
  data->decodeparms.bits_per_component = bpc;
  data->decodeparms.colors    = colors;
  data->_flags |= STREAM_USE_PREDICTOR;
}

/* Adaptive PNG filter
 * We use the "minimum sum of absolute differences" heuristic approach
 * for finding the most optimal filter to be used.
 *
 * From http://www.libpng.org/pub/png/book/chapter09.html
 *
 *   For grayscale and truecolor images of 8 or more bits per sample, with or
 *   without alpha channels, dynamic filtering is almost always beneficial. The
 *   approach that has by now become standard is known as the minimum sum of
 *   absolute differences heuristic and was first proposed by Lee Daniel
 *   Crocker in February 1995.
 */
static unsigned char *
filter_PNG15_apply_filter (unsigned char *raster,
                           int32_t columns, int32_t rows,
                           int8_t bpc, int8_t colors, int32_t *length)
{
  unsigned char *dst;
  int      bits_per_pixel  = colors * bpc;
  int      bytes_per_pixel = (bits_per_pixel + 7) / 8;
  int32_t  rowbytes = columns * bytes_per_pixel;
  int32_t  i, j;

  ASSERT(raster && length);

  /* Result */
  dst = NEW((rowbytes+1)*rows, unsigned char);
  *length = (rowbytes + 1) * rows;

  for (j = 0; j < rows; j++) {
    int type = 0;
    unsigned char *pp = dst + j * (rowbytes + 1);
    unsigned char *p  = raster + j * rowbytes;
    uint32_t sum[5]   = {0, 0, 0, 0, 0};
    /* First calculated sum of values to make a heuristic guess
     * of optimal predictor function.
     */
    for (i = 0; i < rowbytes; i++) {
      int left  = (i - bytes_per_pixel >= 0) ? p[i - bytes_per_pixel] : 0;
      int up    = (j > 0) ? *(p+i-rowbytes) : 0;
      int uplft = (j > 0) ?
                    ((i - bytes_per_pixel >= 0) ?
                      *(p+i-rowbytes-bytes_per_pixel) : 0) : 0;
      /* Type 0 -- None */
      sum[0] += p[i];
      /* Type 1 -- Sub */
      sum[1] += abs((int) p[i] - left);
      /* Type 2 -- Up */
      sum[2] += abs((int) p[i] - up);
      /* Type 3 -- Average */
      {
        int tmp = floor((up + left) / 2);
        sum[3] += abs((int) p[i] - tmp);
      }
      /* Type 4 -- Peath */
      {
        int q = left + up - uplft;
        int qa = abs(q - left), qb = abs(q - up), qc = abs(q - uplft);
        if (qa <= qb && qa <= qc)
          sum[4] += abs((int) p[i] - left);
        else if (qb <= qc)
          sum[4] += abs((int) p[i] - up);
        else
          sum[4] += abs((int) p[i] - uplft);
      }
    }
    {
      int min = sum[0], min_idx = 0;
      for (i = 0; i < 5; i++) {
        if (sum[i] < min) {
          min = sum[i]; min_idx = i;
        }
      }
      type = min_idx;
    }
    /* Now we actually apply filter. */
    pp[0] = type;
    switch (type) {
    case 0:
      memcpy(pp+1, p, rowbytes);
      break;
    case 1:
      for (i = 0; i < rowbytes; i++) {
        int left = (i - bytes_per_pixel >= 0) ? p[i - bytes_per_pixel] : 0;
        pp[i+1] = p[i] - left;
      }
      break;
    case 2:
      for (i = 0; i < rowbytes; i++) {
        int up  = (j > 0) ? *(p+i - rowbytes) : 0;
        pp[i+1] = p[i] - up;
      }
      break;
    case 3:
      {
        for (i = 0; i < rowbytes; i++) {
          int up   = (j > 0) ? *(p+i-rowbytes) : 0;
          int left = (i - bytes_per_pixel >= 0) ? p[i - bytes_per_pixel] : 0;
          int tmp  = floor((up + left) / 2);
          pp[i+1]  = p[i] - tmp;
        }
      }
      break;
    case 4: /* Peath */
      {
        for (i = 0; i < rowbytes; i++) {
          int up   = (j > 0) ? *(p+i-rowbytes) : 0;
          int left = (i - bytes_per_pixel >= 0) ? p[i - bytes_per_pixel] : 0;
          int uplft = (j > 0) ?
                        ((i - bytes_per_pixel >= 0) ?
                          *(p+i-rowbytes-bytes_per_pixel) : 0) : 0;
          int q = left + up - uplft;
          int qa = abs(q - left), qb = abs(q - up), qc = abs(q - uplft);
          if (qa <= qb && qa <= qc)
            pp[i+1] = p[i] - left;
          else if (qb <= qc)
            pp[i+1] = p[i] - up;
          else
            pp[i+1] = p[i] - uplft;
        }
      }
      break;
    }
  }

  return  dst;
}

/* This modifies "raster" itself! */
static void
apply_filter_TIFF2_1_2_4 (unsigned char *raster,
                          int32_t width, int32_t height,
                          int8_t bpc, int8_t num_comp)
{
  int32_t   rowbytes = (bpc * num_comp * width + 7) / 8;
  uint8_t   mask     = (1 << bpc) - 1;
  uint16_t *prev;
  int32_t   i, j;

  ASSERT(raster);
  ASSERT( bpc > 0 && bpc <= 8 );

  prev = NEW(num_comp, uint16_t);

  /* Generic routine for 1 to 16 bit.
   * It supports, e.g., 7 bpc images too.
   * Actually, it is not necessary to have 16 bit inbuf and outbuf
   * since we only need 1, 2, and 4 bit support here. 8 bit is enough.
   */
  for (j = 0; j < height; j++) {
    int32_t  k, l, inbits, outbits;
    uint16_t inbuf, outbuf;
    int      c;

    memset(prev, 0, sizeof(uint16_t)*num_comp);
    inbuf = outbuf = 0; inbits = outbits = 0;
    l = k = j * rowbytes;
    for (i = 0; i < width; i++) {
      for (c = 0; c < num_comp; c++) {
        uint8_t cur;
        int8_t  sub;
        if (inbits < bpc) { /* need more byte */
          inbuf   = (inbuf << 8) | raster[l]; l++;
          inbits += 8;
        }
        cur     = (inbuf >> (inbits - bpc)) & mask;
        inbits -= bpc; /* consumed bpc bits */
        sub     = cur - prev[c];
        prev[c] = cur;
        if (sub < 0)
          sub += (1 << bpc);
        /* Append newly filtered component value */
        outbuf   = (outbuf << bpc) | sub;
        outbits += bpc;
        /* flush */
        if (outbits >= 8) {
          raster[k] = (outbuf >> (outbits - 8)); k++;
          outbits  -= 8;
        }
      }
    }
    if (outbits > 0) {
      raster[k] = (outbuf << (8 - outbits)); k++;
    }
  }
  RELEASE(prev);
}

static unsigned char *
filter_TIFF2_apply_filter (unsigned char *raster,
                           int32_t columns, int32_t rows,
                           int8_t bpc, int8_t colors, int32_t *length)
{
  unsigned char *dst;
  uint16_t      *prev;
  int32_t        rowbytes = (bpc * colors * columns + 7) / 8;
  int32_t        i, j;

  ASSERT(raster && length);

  dst = NEW(rowbytes*rows, unsigned char);
  memcpy(dst, raster, rowbytes*rows);
  *length = rowbytes * rows;

  switch (bpc) {
  case 1: case 2: case 4:
    apply_filter_TIFF2_1_2_4(dst, columns, rows, bpc, colors);
    break;

  case 8:
    prev = NEW(colors, uint16_t);
    for (j = 0; j < rows; j++) {
      memset(prev, 0, sizeof(uint16_t)*colors);
      for (i = 0; i < columns; i++) {
        int     c;
        int32_t pos = colors * (columns * j + i);
        for (c = 0; c < colors; c++) {
          uint8_t cur = raster[pos+c];
          int32_t sub = cur - prev[c];
          prev[c]     = cur;
          dst[pos+c]  = sub;
        }
      }
    }
    RELEASE(prev);
    break;

  case 16:
    prev = NEW(colors, uint16_t);
    for (j = 0; j < rows; j++) {
      memset(prev, 0, sizeof(uint16_t)*colors);
      for (i = 0; i < columns; i++) {
        int     c;
        int32_t pos = 2 * colors * (columns * j + i);
        for (c = 0; c < colors; c++) {
          uint16_t cur   = ((uint8_t)raster[pos+2*c])*256 +
                             (uint8_t)raster[pos+2*c+1];
          uint16_t sub   = cur - prev[c];
          prev[c]        = cur;
          dst[pos+2*c  ] = (sub >> 8) & 0xff;
          dst[pos+2*c+1] = sub & 0xff;
        }
      }
    }
    RELEASE(prev);
    break;

  }

  return  dst;
}

static pdf_obj *
filter_create_predictor_dict (int predictor, int32_t columns,
                              int bpc, int colors)
{
  pdf_obj *parms;

  parms = pdf_new_dict();
  pdf_add_dict(parms, pdf_new_name("BitsPerComponent"), pdf_new_number(bpc));
  pdf_add_dict(parms, pdf_new_name("Colors"),  pdf_new_number(colors));
  pdf_add_dict(parms, pdf_new_name("Columns"), pdf_new_number(columns));
  pdf_add_dict(parms, pdf_new_name("Predictor"), pdf_new_number(predictor));

  return  parms;
}

static void
write_stream (pdf_stream *stream, FILE *file)
{
  unsigned char *filtered;
  unsigned int   filtered_length;
#ifdef HAVE_ZLIB
  uLong          buffer_length;
#else
  unsigned int   buffer_length;
#endif
  unsigned char *buffer;

  /*
   * Always work from a copy of the stream. All filters read from
   * "filtered" and leave their result in "filtered".
   */
#if 0
  filtered = NEW(stream->stream_length + 1, unsigned char);
#endif
  filtered = NEW(stream->stream_length, unsigned char);
  memcpy(filtered, stream->stream, stream->stream_length);
  filtered_length = stream->stream_length;

  /* PDF/A requires Metadata to be not filtered. */
  {
    pdf_obj *type;
    type = pdf_lookup_dict(stream->dict, "Type");
    if (type && !strcmp("Metadata", pdf_name_value(type))) {
      stream->_flags &= ~STREAM_COMPRESS;
    }
  }

#ifdef HAVE_ZLIB
  /* Apply compression filter if requested */
  if (stream->stream_length > 0 &&
      (stream->_flags & STREAM_COMPRESS) &&
      compression_level > 0) {
    pdf_obj *filters;

    /* First apply predictor filter if requested. */
    if ( compression_use_predictor &&
        (stream->_flags & STREAM_USE_PREDICTOR) &&
        !pdf_lookup_dict(stream->dict, "DecodeParms")) {
      int      bits_per_pixel  = stream->decodeparms.colors *
                                   stream->decodeparms.bits_per_component;
      int32_t  len  = (stream->decodeparms.columns * bits_per_pixel + 7) / 8;
      int32_t  rows = stream->stream_length / len;
      unsigned char *filtered2 = NULL;
      int32_t        length2 = stream->stream_length;
      pdf_obj       *parms;

      parms = filter_create_predictor_dict(stream->decodeparms.predictor,
                                        stream->decodeparms.columns,
                                        stream->decodeparms.bits_per_component,
                                        stream->decodeparms.colors);

      switch (stream->decodeparms.predictor) {
      case 2: /* TIFF2 */
        filtered2 = filter_TIFF2_apply_filter(filtered,
                                         stream->decodeparms.columns,
                                         rows,
                                         stream->decodeparms.bits_per_component,
                                         stream->decodeparms.colors, &length2);
        break;
      case 15: /* PNG optimun */
        filtered2 = filter_PNG15_apply_filter(filtered,
                                         stream->decodeparms.columns,
                                         rows,
                                         stream->decodeparms.bits_per_component,
                                         stream->decodeparms.colors, &length2);
        break;
      default:
        WARN("Unknown/unsupported Predictor function %d.",
             stream->decodeparms.predictor);
        break;
      }
      if (parms && filtered2) {
        RELEASE(filtered);
        filtered = filtered2;
        filtered_length = length2;
        pdf_add_dict(stream->dict, pdf_new_name("DecodeParms"), parms);
      }
    }

    filters = pdf_lookup_dict(stream->dict, "Filter");

    buffer_length = filtered_length + filtered_length/1000 + 14;
    buffer = NEW(buffer_length, unsigned char);
    {
      pdf_obj *filter_name = pdf_new_name("FlateDecode");

      if (filters)
        /*
         * FlateDecode is the first filter to be applied to the stream.
         */
        pdf_unshift_array(filters, filter_name);
      else
        /*
         * Adding the filter as a name instead of a one-element array
         * is crucial because otherwise Adobe Reader cannot read the
         * cross-reference stream any more, cf. the PDF v1.5 Errata.
         */
        pdf_add_dict(stream->dict, pdf_new_name("Filter"), filter_name);
    }
#ifdef HAVE_ZLIB_COMPRESS2    
    if (compress2(buffer, &buffer_length, filtered,
		  filtered_length, compression_level)) {
      ERROR("Zlib error");
    }
#else 
    if (compress(buffer, &buffer_length, filtered,
		 filtered_length)) {
      ERROR ("Zlib error");
    }
#endif /* HAVE_ZLIB_COMPRESS2 */
    RELEASE(filtered);
    compression_saved += filtered_length - buffer_length
      - (filters ? strlen("/FlateDecode "): strlen("/Filter/FlateDecode\n"));

    filtered        = buffer;
    filtered_length = buffer_length;
  }
#endif /* HAVE_ZLIB */

  /* AES will change the size of data! */
  if (enc_mode) {
    unsigned char *cipher = NULL;
    size_t         cipher_len = 0;
    pdf_encrypt_data(filtered, filtered_length, &cipher, &cipher_len);
    RELEASE(filtered);
    filtered        = cipher;
    filtered_length = cipher_len;
  }

  #if 0
  /*
   * An optional end-of-line marker preceding the "endstream" is
   * not part of stream data. See, PDF Reference 4th ed., p. 38.
   */
  /* Add a '\n' if the last character wasn't one */
  if (filtered_length > 0 &&
      filtered[filtered_length-1] != '\n') {
    filtered[filtered_length] = '\n';
    filtered_length++;
  }
#endif

  pdf_add_dict(stream->dict,
	       pdf_new_name("Length"), pdf_new_number(filtered_length));

  pdf_write_obj(stream->dict, file);

  pdf_out(file, "\nstream\n", 8);

  if (filtered_length > 0)
    pdf_out(file, filtered, filtered_length);
  RELEASE(filtered);

  /*
   * This stream length "object" gets reset every time write_stream is
   * called for the stream object.
   * If this stream gets written more than once with different
   * filters, this could be a problem.
   */

  pdf_out(file, "\n", 1);
  pdf_out(file, "endstream", 9);
}

static void
release_stream (pdf_stream *stream)
{
  pdf_release_obj(stream->dict);
  stream->dict = NULL;

  if (stream->stream) {
    RELEASE(stream->stream);
    stream->stream = NULL;
  }

  if (stream->objstm_data) {
    RELEASE(stream->objstm_data);
    stream->objstm_data = NULL;
  }

  RELEASE(stream);
}

pdf_obj *
pdf_stream_dict (pdf_obj *stream)
{
  pdf_stream *data;

  TYPECHECK(stream, PDF_STREAM);

  data = stream->data;

  return data->dict;
}

const void *
pdf_stream_dataptr (pdf_obj *stream)
{
  pdf_stream *data;

  TYPECHECK(stream, PDF_STREAM);

  data = stream->data;

  return (const void *) data->stream;
}

int
pdf_stream_length (pdf_obj *stream)
{
  pdf_stream *data;

  TYPECHECK(stream, PDF_STREAM);

  data = stream->data;

  return (int) data->stream_length;
}

static void
set_objstm_data (pdf_obj *objstm, int *data) {
  TYPECHECK(objstm, PDF_STREAM);

  ((pdf_stream *) objstm->data)->objstm_data = data;
}

static int *
get_objstm_data (pdf_obj *objstm) {
  TYPECHECK(objstm, PDF_STREAM);

  return ((pdf_stream *) objstm->data)->objstm_data;
}

void
pdf_add_stream (pdf_obj *stream, const void *stream_data, int length)
{
  pdf_stream *data;

  TYPECHECK(stream, PDF_STREAM);

  if (length < 1)
    return;
  data = stream->data;
  if (data->stream_length + length > data->max_length) {
    data->max_length += length + STREAM_ALLOC_SIZE;
    data->stream      = RENEW(data->stream, data->max_length, unsigned char);
  }
  memcpy(data->stream + data->stream_length, stream_data, length);
  data->stream_length += length;
}

#if HAVE_ZLIB
#define WBUF_SIZE 4096

static int
filter_get_DecodeParms_FlateDecode (struct decode_parms *parms, pdf_obj *dict)
{
  pdf_obj *tmp;

  ASSERT(dict && parms);
  ASSERT(PDF_OBJ_DICTTYPE(dict));

  /* Fill with default values */
  parms->predictor = 1;
  parms->colors    = 1;
  parms->bits_per_component = 8;
  parms->columns   = 1;

  tmp = pdf_deref_obj(pdf_lookup_dict(dict, "Predictor"));
  if (tmp) {
    parms->predictor = pdf_number_value(tmp);
    pdf_release_obj(tmp);
  }
  tmp = pdf_deref_obj(pdf_lookup_dict(dict, "Colors"));
  if (tmp) {
    parms->colors = pdf_number_value(tmp);
    pdf_release_obj(tmp);
  }
  tmp = pdf_deref_obj(pdf_lookup_dict(dict, "BitsPerComponent"));
  if (tmp) {
    parms->bits_per_component = pdf_number_value(tmp);
    pdf_release_obj(tmp);
  }
  tmp = pdf_deref_obj(pdf_lookup_dict(dict, "Columns"));
  if (tmp) {
    parms->columns = pdf_number_value(tmp);
    pdf_release_obj(tmp);
  }
  if (parms->bits_per_component != 1 &&
      parms->bits_per_component != 2 &&
      parms->bits_per_component != 4 &&
      parms->bits_per_component != 8 &&
      parms->bits_per_component != 16) {
      WARN("Invalid BPC value in DecodeParms: %d", parms->bits_per_component);
      return -1;
  } else if (parms->predictor <= 0 || parms->colors <= 0 ||
             parms->columns <= 0)
    return -1;
  return 0;
}

/* From Xpdf version 3.04
 * I'm not sure if I properly ported... Untested.
 */
static int
filter_row_TIFF2 (unsigned char *dst, const unsigned char *src,
                  struct decode_parms *parms)
{
  const unsigned char *p = src;
  unsigned char *col;
  /* bits_per_component < 8 here */
  int  mask = (1 << parms->bits_per_component) - 1;
  int  inbuf, outbuf; /* 2 bytes buffer */
  int  i, ci, j, k, inbits, outbits;

  col = NEW(parms->colors, unsigned char);
  memset(col, 0, parms->colors);
  inbuf = outbuf = 0; inbits = outbits = 0;
  j = k = 0;
  for (i = 0; i < parms->columns; i++) {
    /* expanding each color component into an 8-bits bytes array */
    for (ci = 0; ci < parms->colors; ci++) {
      if (inbits < parms->bits_per_component) {
         /* need more byte */
         inbuf   = (inbuf << 8) | p[j++];
         inbits += 8;
      }
      /* predict current color component */
      col[ci]  = (unsigned char) ((col[ci] +
                 (inbuf >> (inbits - parms->bits_per_component))) & mask);
      inbits  -= parms->bits_per_component; /* consumed bpc bits */
      /* append newly predicted color component value */
      outbuf   = (outbuf << parms->bits_per_component) | col[ci];
      outbits += parms->bits_per_component;
      if (outbits >= 8) { /* flush */
        dst[k++] = (unsigned char) (outbuf >> (outbits - 8));
        outbits -= 8;
      }
    }
  }
  if (outbits > 0) {
    dst[k] = (unsigned char) (outbuf << (8 - outbits));
  }
  RELEASE(col);

  return 0;
}

/* This routine is inefficient. Length is typically 4 for Xref streams.
 * Especially, calling pdf_add_stream() for each 4 bytes append is highly
 * inefficient.
 */
static pdf_obj *
filter_stream_decode_Predictor (const void *src, size_t srclen, struct decode_parms *parms)
{
  pdf_obj             *dst;
  const unsigned char *p = (const unsigned char *) src;
  const unsigned char *endptr = p + srclen;
  unsigned char       *prev, *buf;
  int                  bits_per_pixel  = parms->colors * parms->bits_per_component;
  int                  bytes_per_pixel = (bits_per_pixel + 7) / 8;
  int                  length = (parms->columns * bits_per_pixel + 7) / 8;
  int                  i, error = 0;

  dst  = pdf_new_stream(0);

  prev = NEW(length, unsigned char);
  buf  = NEW(length, unsigned char);

  memset(prev, 0, length);
  switch (parms->predictor) {
  case 1 : /* No prediction */
    pdf_add_stream(dst, src, srclen); /* Just copy */
    break;
  case 2: /* TIFF Predictor 2 */
    {
      if (parms->bits_per_component == 8) {
        while (p + length < endptr) {
          /* Same as PNG Sub */
          for (i = 0; i < length; i++) {
            int pv = i - bytes_per_pixel >= 0 ? buf[i - bytes_per_pixel] : 0;
            buf[i] = (unsigned char)(((int) p[i] + pv) & 0xff);
          }
          pdf_add_stream(dst, buf, length);
          p += length;
        }
      } else if (parms->bits_per_component == 16) {
        while (p + length < endptr) {
          for (i = 0; i < length; i += 2) {
            int  b  = i - bytes_per_pixel;
            char hi = b >= 0 ? buf[b] : 0;
            char lo = b >= 0 ? buf[b + 1] : 0;
            int  pv = (hi << 8) | lo;
            int  cv = (p[i] << 8) | p[i + 1];
            int  c  = pv + cv;
            buf[i]     = (unsigned char) (c >> 8);
            buf[i + 1] = (unsigned char) (c & 0xff);
          }
          pdf_add_stream(dst, buf, length);
          p += length;
        }
      } else { /* bits per component 1, 2, 4 */
        while (!error && p + length < endptr) {
          error = filter_row_TIFF2(buf, p, parms);
          if (!error) {
            pdf_add_stream(dst, buf, length);
            p += length;
          }
        }
      }
    }
    break;
  /* PNG predictors: first byte of each rows is predictor type */
  case 10: /* PNG None */
  case 11: /* PNG Sub on all rows */
  case 12: /* PNG UP on all rows */
  case 13: /* PNG Average on all rows */
  case 14: /* PNG Paeth on all rows */
  case 15: /* PNG optimun: prediction algorithm can change from line to line. */
    {
      int type = parms->predictor - 10;

      while (!error && p + length < endptr) {
        if (parms->predictor == 15)
          type = *p;
        else if (*p != type) {
          WARN("Mismatched Predictor type in data stream.");
          error = -1;
        }
        p++;
        switch (type) {
        case 0: /* Do nothing just skip first byte */
          memcpy(buf, p, length);
          break;
        case 1:
          for (i = 0; i < length; i++) {
            int pv = i - bytes_per_pixel >= 0 ? buf[i - bytes_per_pixel] : 0;
            buf[i] = (unsigned char)(((int) p[i] + pv) & 0xff);
          }
          break;
        case 2:
          for (i = 0; i < length; i++) {
            buf[i] = (unsigned char)(((int) p[i] + (int) prev[i]) & 0xff);
          }
          break;
        case 3:
          for (i = 0; i < length; i++) {
            int up   = prev[i];
            int left = i - bytes_per_pixel >= 0 ? buf[i - bytes_per_pixel] : 0;
            int tmp  = floor((up + left) / 2);
            buf[i] = (unsigned char)((p[i] + tmp) & 0xff);
          }
          break;
        case 4:
          for (i = 0; i < length; i++) {
            int a = i - bytes_per_pixel >= 0 ? buf[i - bytes_per_pixel] : 0; /* left */
            int b = prev[i]; /* above */
            int c = i - bytes_per_pixel >= 0 ? prev[i - bytes_per_pixel] : 0; /* upper left */
            int q = a + b - c;
            int qa = q - a, qb = q - b, qc = q - c;
            qa = qa < 0 ? -qa : qa;
            qb = qb < 0 ? -qb : qb;
            qc = qc < 0 ? -qc : qc;
            if (qa <= qb && qa <= qc)
              buf[i] = (unsigned char) (((int) p[i] + a) & 0xff);
            else if (qb <= qc)
              buf[i] = (unsigned char) (((int) p[i] + b) & 0xff);
            else
              buf[i] = (unsigned char) (((int) p[i] + c) & 0xff);
          }
          break;
        default:
          WARN("Unknown PNG predictor type: %d", type);
          error = -1;
        }
        if (!error) {
          pdf_add_stream(dst, buf, length); /* highly inefficient */
          memcpy(prev, buf, length);
          p += length;
        }
      }
    }
    break;
  default:
    WARN("Unknown Predictor type value :%d", parms->predictor);
    error = -1;
  }

  RELEASE(prev);
  RELEASE(buf);

  if (error) {
    pdf_release_obj(dst);
    dst = NULL;
  }

  return dst;
}

static pdf_obj *
filter_stream_decode_FlateDecode (const void *data, size_t len, struct decode_parms *parms)
{
  pdf_obj  *dst;
  pdf_obj  *tmp;
  z_stream  z;
  Bytef     wbuf[WBUF_SIZE];

  z.zalloc = Z_NULL; z.zfree = Z_NULL; z.opaque = Z_NULL;

  z.next_in  = (z_const Bytef *) data; z.avail_in  = len;
  z.next_out = (Bytef *) wbuf; z.avail_out = WBUF_SIZE;

  if (inflateInit(&z) != Z_OK) {
    WARN("inflateInit() failed.");
    return NULL;
  }

  tmp = pdf_new_stream(0);
  for (;;) {
    int status;
    status = inflate(&z, Z_NO_FLUSH);
    if (status == Z_STREAM_END)
      break;
    else if (status != Z_OK) {
      WARN("inflate() failed. Broken PDF file?");
      inflateEnd(&z);
      pdf_release_obj(tmp);
      return NULL;
    }

    if (z.avail_out == 0) {
      pdf_add_stream(tmp, wbuf, WBUF_SIZE);
      z.next_out  = wbuf;
      z.avail_out = WBUF_SIZE;
    }
  }

  if (WBUF_SIZE - z.avail_out > 0)
    pdf_add_stream(tmp, wbuf, WBUF_SIZE - z.avail_out);

  if (inflateEnd(&z) == Z_OK) {
    if (parms) { 
      dst = filter_stream_decode_Predictor(pdf_stream_dataptr(tmp), pdf_stream_length(tmp), parms);
    } else {
      dst = pdf_link_obj(tmp);
    }
  } else {
    dst = NULL;
  }
  pdf_release_obj(tmp);

  return dst;
}
#endif

static pdf_obj *
filter_stream_decode_ASCIIHexDecode (const void *data, size_t len)
{
  pdf_obj       *dst;
  int            eod, error;
  const char    *p = (const char *) data;
  const char    *endptr = p + len;
  unsigned char *buf, ch;
  size_t         pos, n;

  buf = NEW((len+1)/2, unsigned char);
  skip_white(&p, endptr);
  ch = 0; n = 0; pos = 0; eod = 0; error = 0;
  while (p < endptr && !error && !eod) {
    char c1, val;
    c1 = p[0];
    if (c1 >= 'A' && c1 <= 'F') {
      val = c1 - 'A' + 10;
    } else if (c1 >= 'a' && c1 <= 'f') {
      val = c1 - 'a' + 10;
    } else if (c1 >= '0' && c1 <= '9') {
      val = c1 - '0';
    } else if (c1 == '>') {
      val = 0;
      eod = 1;
      if ((pos % 2) == 0)
        break;
    } else {
      error = -1;
      break;
    }
    if (pos % 2) {
      buf[n] = ch + val;
      n++;
      ch = 0;
    } else {
      ch = val << 4;
    }
    pos++; p++;
    skip_white(&p, endptr);
  }
  if (error || !eod) {
    WARN("Invalid ASCIIHex data seen: %s", error ? "Invalid character" : "No EOD marker");
    dst = NULL;
  } else {
    dst = pdf_new_stream(0);
    pdf_add_stream(dst, buf, n);
  }
  RELEASE(buf);

  return dst;
}

/* Percent sign is not start of comment here.
 * We need this for reading Ascii85 encoded data.
 */
#define is_space(c) ((c) == ' '  || (c) == '\t' || (c) == '\f' || \
		     (c) == '\r' || (c) == '\n' || (c) == '\0')
static void
skip_white_a85 (const char **p, const char *endptr)
{
  while (*p < endptr && (is_space(**p))) {
    (*p)++;
  } 
}

static pdf_obj *
filter_stream_decode_ASCII85Decode (const void *data, size_t len)
{
  pdf_obj       *dst;
  int            eod, error;
  const char    *p = (const char *) data;
  const char    *endptr = p + len;
  unsigned char *buf;
  size_t         n;

  buf = NEW(((len+4)/5)*4, unsigned char);
  skip_white_a85(&p, endptr);
  n = 0; eod = 0; error = 0;
  while (p < endptr && !error && !eod) {
    char q[5] = {'u', 'u', 'u', 'u', 'u'};
    int  m;
    char ch;

    ch = p[0];
    p++;
    skip_white_a85(&p, endptr);
    if (ch == 'z') {
      memset(buf+n, 0, 4);
      n += 4;
      continue;
    } else if (ch == '~') {
      if (p < endptr && p[0] == '>') {
        eod = 1;
        p++;
      } else {
        error = -1;
      }
      break;
    }
    q[0] = ch;
    for (m = 1; m < 5 && p < endptr; m++) {
      ch = p[0];
      p++;
      skip_white_a85(&p, endptr);
      if (ch == '~') {
        if (p < endptr && p[0] == '>') {
          eod = 1;
          p++;
        } else {
          error = -1;
        }
        break;
      } else if (ch < '!' || ch > 'u') {
        error = -1;
        break;
      } else {
        q[m] = ch;
      }
    }
    if (!error) {
      uint32_t val = 0;
      int      i;
      if (m <= 1) {
        error = -1;
        break;
      }
      val = 85*85*85*(q[0] - '!') + 85*85*(q[1] - '!')
              + 85*(q[2] - '!') + (q[3] - '!');
      /* Check overflow */
      if (val > UINT32_MAX / 85) {
        error = -1;
        break;
      } else {
        val = 85 * val;
        if (val > UINT32_MAX - (q[4] - '!')) {
          error = -1;
          break;
        }
        val += (q[4] - '!');
      }
      if (!error) {
        for (i = 3; i >= 0; i--) {
          buf[n + i] = val & 0xff;
          val /= 256;
        }
        n += m - 1;
      }
    }
  }

  if (error) {
    WARN("Error in reading ASCII85 data.");
    dst = NULL;
  } else if (!eod) {
    WARN("Error in reading ASCII85 data: No EOD");
    dst = NULL;
  } else {
    dst = pdf_new_stream(0);
    pdf_add_stream(dst, buf, n);
  }
  RELEASE(buf);

  return dst;
}

static pdf_obj *
filter_stream_decode (const char *filter_name, pdf_obj *src, pdf_obj *parm)
{
  pdf_obj    *dec;
  const char *stream_data;
  size_t      stream_length;

  if (!filter_name)
    return pdf_link_obj(src);

  stream_data   = pdf_stream_dataptr(src);
  stream_length = pdf_stream_length(src);

  if (!strcmp(filter_name, "ASCIIHexDecode")) {
    dec = filter_stream_decode_ASCIIHexDecode(stream_data, stream_length);
  } else if (!strcmp(filter_name, "ASCII85Decode")) {
    dec = filter_stream_decode_ASCII85Decode(stream_data, stream_length);
#if HAVE_ZLIB
  } else if (!strcmp(filter_name, "FlateDecode")) {
    struct decode_parms decode_parm;
    if (parm)
      filter_get_DecodeParms_FlateDecode(&decode_parm, parm);
    dec = filter_stream_decode_FlateDecode(stream_data, stream_length, parm ? &decode_parm : NULL);
#endif /* HAVE_ZLIB */
  } else {
    WARN("DecodeFilter \"%s\" not supported.", filter_name);
    dec = NULL;
  }

  return dec;
}

int
pdf_concat_stream (pdf_obj *dst, pdf_obj *src)
{
  pdf_obj *filtered;
  pdf_obj *stream_dict;
  pdf_obj *filter, *parms;
  int      error = 0;

  if (!PDF_OBJ_STREAMTYPE(dst) || !PDF_OBJ_STREAMTYPE(src)) {
    WARN("Passed invalid type in pdf_concat_stream().");
    return -1;
  }

  stream_dict = pdf_stream_dict(src);

  filter = pdf_lookup_dict(stream_dict, "Filter");
  if (!filter) {
    pdf_add_stream(dst, pdf_stream_dataptr(src), pdf_stream_length(src));
    return 0;
  }
  if (pdf_lookup_dict(stream_dict, "DecodeParms")) {
    /* Dictionary or array */
    parms = pdf_deref_obj(pdf_lookup_dict(stream_dict, "DecodeParms"));
    if (!parms) {
      WARN("Failed to deref DeocdeParms...");
      return -1;
    } else if (!PDF_OBJ_ARRAYTYPE(parms) && !PDF_OBJ_DICTTYPE(parms)) {
      WARN("PDF dict or array expected for DecodeParms...");
      pdf_release_obj(parms);
      return -1;
    }
  } else {
    parms = NULL;
  }
  if (PDF_OBJ_ARRAYTYPE(filter)) {
    int      i, num;
    pdf_obj *prev = NULL;

    num = pdf_array_length(filter);
    if (parms) {
      if (!PDF_OBJ_ARRAYTYPE(parms) || pdf_array_length(parms) != num) {
        WARN("Invalid DecodeParam object found.");
        pdf_release_obj(parms);
        return -1;
      }
    }
    if (num == 0) {
      filtered = pdf_link_obj(src);
    } else {
      filtered = NULL;
      prev = pdf_link_obj(src);
      for (i = 0; i < num && prev != NULL; i++) {
        pdf_obj *tmp1, *tmp2;

        tmp1 = pdf_deref_obj(pdf_get_array(filter, i));
        if (parms) {
          tmp2 = pdf_deref_obj(pdf_get_array(parms, i));
        } else {
          tmp2 = NULL;
        }
        if (PDF_OBJ_NAMETYPE(tmp1)) {
          filtered = filter_stream_decode(pdf_name_value(tmp1), prev, tmp2);
        } else if (PDF_OBJ_NULLTYPE(tmp1)) {
          filtered = pdf_link_obj(prev);
        } else {
          WARN("Unexpected object found for /Filter...");
          filtered = NULL;
        }
        if (prev)
          pdf_release_obj(prev);
        if (tmp1)
          pdf_release_obj(tmp1);
        if (tmp2)
          pdf_release_obj(tmp2);
        prev = filtered;      
      }
    }
  } else if (PDF_OBJ_NAMETYPE(filter)) {
    filtered = filter_stream_decode(pdf_name_value(filter), src, parms);
  } else {
    WARN("Invalid value for /Filter found.");
    filtered = NULL;
  }
  if (parms)
    pdf_release_obj(parms);
  if (filtered) {
    pdf_add_stream(dst, pdf_stream_dataptr(filtered), pdf_stream_length(filtered));
    pdf_release_obj(filtered);
    error = 0;
  } else {
    error = -1;
  }

  return error;
}

static pdf_obj *
pdf_stream_uncompress (pdf_obj *src) {
  pdf_obj *dst = pdf_new_stream(0);

  TYPECHECK(src, PDF_STREAM);

  pdf_merge_dict(pdf_stream_dict(dst), pdf_stream_dict(src));
  pdf_remove_dict(pdf_stream_dict(dst), "Length");
  pdf_concat_stream(dst, src);

  return dst;
}

#if 0
void
pdf_stream_set_flags (pdf_obj *stream, int flags)
{
  pdf_stream *data;

  TYPECHECK(stream, PDF_STREAM);

  data = stream->data;
  data->_flags = flags;
}

int
pdf_stream_get_flags (pdf_obj *stream)
{
  pdf_stream *data;

  TYPECHECK(stream, PDF_STREAM);

  data = stream->data;

  return data->_flags;
}
#endif

static void
pdf_write_obj (pdf_obj *object, FILE *file)
{
  if (object == NULL) {
    write_null(file);
    return;
  }

  if (INVALIDOBJ(object) || PDF_OBJ_UNDEFINED(object))
    ERROR("pdf_write_obj: Invalid object, type = %d\n", object->type);

  if (file == stderr)
    fprintf(stderr, "{%d}", object->refcount);

  switch (object->type) {
  case PDF_BOOLEAN:
    write_boolean(object->data, file);
    break;
  case PDF_NUMBER:
    write_number (object->data, file);
    break;
  case PDF_STRING:
    write_string (object->data, file);
    break;
  case PDF_NAME:
    write_name(object->data, file);
    break;
  case PDF_ARRAY:
    write_array(object->data, file);
    break;
  case PDF_DICT:
    write_dict (object->data, file);
    break;
  case PDF_STREAM:
    write_stream(object->data, file);
    break;
  case PDF_NULL:
    write_null(file);
    break;
  case PDF_INDIRECT:
    write_indirect(object->data, file);
    break;
  }
}

/* Write the object to the file */ 
static void
pdf_flush_obj (pdf_obj *object, FILE *file)
{
  int length;

  /*
   * Record file position
   */
  add_xref_entry(object->label, 1,
		 pdf_output_file_position, object->generation);
  length = sprintf(format_buffer, "%u %hu obj\n", object->label, object->generation);
  enc_mode = doc_enc_mode && !(object->flags & OBJ_NO_ENCRYPT);
  pdf_enc_set_label(object->label);
  pdf_enc_set_generation(object->generation);
  pdf_out(file, format_buffer, length);
  pdf_write_obj(object, file);
  pdf_out(file, "\nendobj\n", 8);
}

static int
pdf_add_objstm (pdf_obj *objstm, pdf_obj *object)
{
  int *data, pos;

  TYPECHECK(objstm, PDF_STREAM);

  data = get_objstm_data(objstm);
  pos = ++data[0];

  data[2*pos]   = object->label;
  data[2*pos+1] = pdf_stream_length(objstm);

  add_xref_entry(object->label, 2, objstm->label, pos-1);
 
  /* redirect output into objstm */
  output_stream = objstm;
  enc_mode = 0;
  pdf_write_obj(object, pdf_output_file);
  pdf_out_char(pdf_output_file, '\n');
  output_stream = NULL;

  return pos;
}

static void
release_objstm (pdf_obj *objstm)
{
  int *data = get_objstm_data(objstm);
  int pos = data[0];
  pdf_obj *dict;
  pdf_stream *stream;
  unsigned char *old_buf;
  unsigned int old_length;
  stream = (pdf_stream *) objstm->data;

  /* Precede stream data by offset table */
  old_buf = stream->stream;
  old_length = stream->stream_length;
  /* Reserve 22 bytes for each entry (two 10 digit numbers plus two spaces) */
  stream->stream = NEW(old_length + 22*pos, unsigned char);
  stream->stream_length = 0;

  {
    int i = 2*pos, *val = data+2;
    while (i--) {
      int length = sprintf(format_buffer, "%d ", *(val++));
      pdf_add_stream(objstm, format_buffer, length);
    }
  }

  dict = pdf_stream_dict(objstm);
  pdf_add_dict(dict, pdf_new_name("Type"), pdf_new_name("ObjStm"));
  pdf_add_dict(dict, pdf_new_name("N"), pdf_new_number(pos));
  pdf_add_dict(dict, pdf_new_name("First"), pdf_new_number(stream->stream_length));
  
  pdf_add_stream(objstm, old_buf, old_length);
  RELEASE(old_buf);
  pdf_release_obj(objstm);
}

void
pdf_release_obj (pdf_obj *object)
{
  if (object == NULL)
    return;
  if (INVALIDOBJ(object) || object->refcount <= 0) {
    MESG("\npdf_release_obj: object=%p, type=%d, refcount=%d\n",
	 object, object->type, object->refcount);
    pdf_write_obj(object, stderr);
    ERROR("pdf_release_obj:  Called with invalid object.");
  }
  object->refcount -= 1;
  if (object->refcount == 0) {
#if defined(PDFOBJ_DEBUG)
  bucket[object->obj_id] = NULL;
#endif
    /*
     * Nothing is using this object so it's okay to remove it.
     * Nonzero "label" means object needs to be written before it's destroyed.
     */
    if (object->label && pdf_output_file != NULL) {
      if (!do_objstm || object->flags & OBJ_NO_OBJSTM
	  || (doc_enc_mode && object->flags & OBJ_NO_ENCRYPT)
	  || object->generation)
	pdf_flush_obj(object, pdf_output_file);
      else {
        if (!current_objstm) {
	  int *data = NEW(2*OBJSTM_MAX_OBJS+2, int);
	  data[0] = data[1] = 0;
	  current_objstm = pdf_new_stream(STREAM_COMPRESS);
	  set_objstm_data(current_objstm, data);
	  pdf_label_obj(current_objstm);
	}
	if (pdf_add_objstm(current_objstm, object) == OBJSTM_MAX_OBJS) {
	  release_objstm(current_objstm);
	  current_objstm = NULL;
	}
      }
    }
    switch (object->type) {
    case PDF_BOOLEAN:
      release_boolean(object->data);
      break;
    case PDF_NULL:
      break;
    case PDF_NUMBER:
      release_number(object->data);
      break;
    case PDF_STRING:
      release_string(object->data);
      break;
    case PDF_NAME:
      release_name(object->data);
      break;
    case PDF_ARRAY:
      release_array(object->data);
      break;
    case PDF_DICT:
      release_dict(object->data);
      break;
    case PDF_STREAM:
      release_stream(object->data);
      break;
    case PDF_INDIRECT:
      release_indirect(object->data);
      break;
    }
    /* This might help detect freeing already freed objects */
    object->type = -1;
    object->data = NULL;
    RELEASE(object);
  }
}

static int
backup_line (FILE *pdf_input_file)
{
  int ch = -1;

  /*
   * Note: this code should work even if \r\n is eol. It could fail on a
   * machine where \n is eol and there is a \r in the stream --- Highly
   * unlikely in the last few bytes where this is likely to be used.
   */
  if (tell_position(pdf_input_file) > 1)
    do {
      seek_relative (pdf_input_file, -2);
    } while (tell_position(pdf_input_file) > 0 &&
	     (ch = fgetc(pdf_input_file)) >= 0 &&
	     (ch != '\n' && ch != '\r' ));
  if (ch < 0) {
    return 0;
  }

  return 1;
}

static int
find_xref (FILE *pdf_input_file)
{
  int   xref_pos = 0;
  int   len, tries = 10;

  do {
    int currentpos;

    if (!backup_line(pdf_input_file)) {
      tries = 0;
      break;
    }
    currentpos = tell_position(pdf_input_file);
    fread(work_buffer, sizeof(char), strlen("startxref"), pdf_input_file);
    seek_absolute(pdf_input_file, currentpos);
    tries--;
  } while (tries > 0 &&
	   strncmp(work_buffer, "startxref", strlen("startxref")));
  if (tries <= 0)
    return 0;

  /* Skip rest of this line */
  mfgets(work_buffer, WORK_BUFFER_SIZE, pdf_input_file);
  /* Next line of input file should contain actual xref location */
  len = mfreadln(work_buffer, WORK_BUFFER_SIZE, pdf_input_file);
  if (len <= 0)
   WARN("Reading xref location data failed... Not a PDF file?");
  else {
    const char *start, *end;
    char *number;

    start = work_buffer;
    end   = start + len;
    skip_white(&start, end);
    number   = parse_number(&start, end);
    xref_pos = (int) atof(number);
    RELEASE(number);
  }

  return xref_pos;
}

/*
 * This routine must be called with the file pointer located
 * at the start of the trailer.
 */
static pdf_obj *
parse_trailer (pdf_file *pf)
{
  pdf_obj *result;
  /*
   * Fill work_buffer and hope trailer fits. This should
   * be made a bit more robust sometime.
   */
  if (fread(work_buffer, sizeof(char),
	    WORK_BUFFER_SIZE, pf->file) == 0 ||
      strncmp(work_buffer, "trailer", strlen("trailer"))) {
    WARN("No trailer.  Are you sure this is a PDF file?");
    WARN("buffer:\n->%s<-\n", work_buffer);
    result = NULL;
  } else {
    const char *p = work_buffer + strlen("trailer");
    skip_white(&p, work_buffer + WORK_BUFFER_SIZE);
    result = parse_pdf_dict(&p, work_buffer + WORK_BUFFER_SIZE, pf);
  }

  return result;
}

/*
 * This routine tries to estimate an upper bound for character position
 * of the end of the object, so it knows how big the buffer must be.
 * The parsing routines require that the entire object be read into
 * memory. It would be a major pain to rewrite them.  The worst case
 * is that an object before an xref table will grab the whole table
 * :-(
 */
static int
next_object_offset (pdf_file *pf, unsigned int obj_num)
{
  int  next = pf->file_size;  /* Worst case */
  int  i, curr;

  curr = pf->xref_table[obj_num].field2;
  /* Check all other type 1 objects to find next one */
  for (i = 0; i < pf->num_obj; i++) {
    if (pf->xref_table[i].type == 1 &&
        pf->xref_table[i].field2 > curr &&
        pf->xref_table[i].field2 < next)
      next = pf->xref_table[i].field2;
  }

  return  next;
}

#define checklabel(pf, n, g) ((n) > 0 && (n) < (pf)->num_obj && ( \
  ((pf)->xref_table[(n)].type == 1 && (pf)->xref_table[(n)].field3 == (g)) || \
  ((pf)->xref_table[(n)].type == 2 && !(g))))

pdf_obj *
pdf_new_indirect (pdf_file *pf, unsigned obj_num, unsigned short obj_gen)
{
  pdf_obj      *result;
  pdf_indirect *indirect;

  indirect = NEW(1, pdf_indirect);
  indirect->pf         = pf;
  indirect->obj        = NULL;
  indirect->label      = obj_num;
  indirect->generation = obj_gen;

  result   = pdf_new_obj(PDF_INDIRECT);
  result->data = indirect;

  return result;
}

static pdf_obj *
pdf_read_object (unsigned int obj_num, unsigned short obj_gen,
		pdf_file *pf, int offset, int limit)
{
  int      length;
  char    *buffer;
  const char *p, *endptr;
  pdf_obj *result;

  length = limit - offset;

  if (length <= 0)
    return NULL;

  buffer = NEW(length + 1, char);

  seek_absolute(pf->file, offset);
  fread(buffer, sizeof(char), length, pf->file);

  p      = buffer;
  endptr = p + length;

  /* Check for obj_num and obj_gen */
  {
    const char   *q = p; /* <== p */
    char         *sp;
    unsigned int  n, g;

    skip_white(&q, endptr);
    sp = parse_unsigned(&q, endptr);
    if (!sp) {
      RELEASE(buffer);
      return NULL;
    }
    n = strtoul(sp, NULL, 10);
    RELEASE(sp);

    skip_white(&q, endptr);
    sp = parse_unsigned(&q, endptr);
    if (!sp) {
      RELEASE(buffer);
      return NULL;
    }
    g = strtoul(sp, NULL, 10);
    RELEASE(sp);

    if (obj_num && (n != obj_num || g != obj_gen)) {
      RELEASE(buffer);
      return NULL;
    }

    p = q; /* ==> p */
  }


  skip_white(&p, endptr);
  if (memcmp(p, "obj", strlen("obj"))) {
    WARN("Didn't find \"obj\".");
    RELEASE(buffer);
    return NULL;
  }
  p += strlen("obj");

  result = parse_pdf_object(&p, endptr, pf);

  skip_white(&p, endptr);
  if (memcmp(p, "endobj", strlen("endobj"))) {
    WARN("Didn't find \"endobj\".");
    if (result)
      pdf_release_obj(result);
    result = NULL;
  }
  RELEASE(buffer);

  return result;
}

static pdf_obj *
read_objstm (pdf_file *pf, unsigned int num)
{
  unsigned int offset = pf->xref_table[num].field2;
  unsigned short gen = pf->xref_table[num].field3;
  int limit = next_object_offset(pf, num), n, first, *header = NULL;
  char *data = NULL, *q;
  const char *p, *endptr;
  int i;

  pdf_obj *objstm, *dict, *type, *n_obj, *first_obj;

  objstm = pdf_read_object(num, gen, pf, offset, limit);

  if (!PDF_OBJ_STREAMTYPE(objstm))
    goto error;

  {
    pdf_obj *tmp = pdf_stream_uncompress(objstm);
    if (!tmp)
      goto error;
    pdf_release_obj(objstm);
    objstm = tmp;
  }

  dict = pdf_stream_dict(objstm);

  type = pdf_lookup_dict(dict, "Type");
  if (!PDF_OBJ_NAMETYPE(type) ||
      strcmp(pdf_name_value(type), "ObjStm"))
    goto error;

  n_obj = pdf_lookup_dict(dict, "N");
  if (!PDF_OBJ_NUMBERTYPE(n_obj))
    goto error;
  n = (int) pdf_number_value(n_obj);

  first_obj = pdf_lookup_dict(dict, "First");
  if (!PDF_OBJ_NUMBERTYPE(first_obj))
    goto error;
  first = (int) pdf_number_value(first_obj);
  /* reject object streams without object data */
  if (first >= pdf_stream_length(objstm))
    goto error;

  header = NEW(2*(n+1), int);
  set_objstm_data(objstm, header);
  *(header++) = n;
  *(header++) = first;

  /* avoid parsing beyond offset table */
  data = NEW(first + 1, char);
  memcpy(data, pdf_stream_dataptr(objstm), first);
  data[first] = 0;

  p      = data;
  endptr = p + first;
  i = 2*n;
  while (i--) {
    *(header++) = strtoul(p, &q, 10);
    if (q == p)
      goto error;
    p = q;
  }

  /* Any garbage after last entry? */
  skip_white(&p, endptr);
  if (p != endptr)
    goto error;
  RELEASE(data);
  
  return pf->xref_table[num].direct = objstm;

 error:
  WARN("Cannot parse object stream.");
  if (data)
    RELEASE(data);
  if (objstm)
    pdf_release_obj(objstm);
  return NULL;
}

/* Label without corresponding object definition are replaced by the
 * null object, as required by the PDF spec. This is important to parse
 * several cross-reference sections.
 */ 
static pdf_obj *
pdf_get_object (pdf_file *pf, unsigned int obj_num, unsigned short obj_gen)
{
  pdf_obj *result;

  if (!checklabel(pf, obj_num, obj_gen)) {
    WARN("Trying to read nonexistent or deleted object: %lu %u",
         obj_num, obj_gen);
    return pdf_new_null();
  }

  if ((result = pf->xref_table[obj_num].direct)) {
    return pdf_link_obj(result);
  }

  if (pf->xref_table[obj_num].type == 1) {
    /* type == 1 */
    unsigned int offset;
    int limit;
    offset = pf->xref_table[obj_num].field2;
    limit  = next_object_offset(pf, obj_num);
    result = pdf_read_object(obj_num, obj_gen, pf, offset, limit);
  } else {
    /* type == 2 */
    unsigned int   objstm_num = pf->xref_table[obj_num].field2;
    unsigned short index = pf->xref_table[obj_num].field3;
    pdf_obj *objstm;
    int  *data, n, first, length;
    const char *p, *q;

    if (objstm_num >= pf->num_obj ||
	pf->xref_table[objstm_num].type != 1 ||
	!((objstm = pf->xref_table[objstm_num].direct) ||
	  (objstm = read_objstm(pf, objstm_num))))
      goto error;

    data = get_objstm_data(objstm);
    n = *(data++);
    first = *(data++);

    if (index >= n || data[2*index] != obj_num)
      goto error;

    length = pdf_stream_length(objstm);
    p = (const char *) pdf_stream_dataptr(objstm) + first + data[2*index+1];
    q = p + (index == n-1 ? length : first+data[2*index+3]);
    result = parse_pdf_object(&p, q, pf);
    if (!result)
      goto error;
  }

  /* Make sure the caller doesn't free this object */
  pf->xref_table[obj_num].direct = pdf_link_obj(result);

  return result;

 error:
  WARN("Could not read object from object stream.");
  return pdf_new_null();
}

#define OBJ_FILE(o) (((pdf_indirect *)((o)->data))->pf)
#define OBJ_OBJ(o)  (((pdf_indirect *)((o)->data))->obj)
#define OBJ_NUM(o)  (((pdf_indirect *)((o)->data))->label)
#define OBJ_GEN(o)  (((pdf_indirect *)((o)->data))->generation)

static pdf_obj *
pdf_new_ref (pdf_obj *object)
{
  pdf_obj *result;

  if (object->label == 0) {
    pdf_label_obj(object);
  }
  result = pdf_new_indirect(NULL, object->label, object->generation);
  OBJ_OBJ(result) = object;
  return result;
}

/* pdf_deref_obj always returns a link instead of the original   */
/* It never return the null object, but the NULL pointer instead */
pdf_obj *
pdf_deref_obj (pdf_obj *obj)
{
  int count = PDF_OBJ_MAX_DEPTH;

  if (obj)
    obj = pdf_link_obj(obj);

  while (PDF_OBJ_INDIRECTTYPE(obj) && --count) {
    pdf_file *pf = OBJ_FILE(obj);
    if (pf) {
      unsigned int   obj_num = OBJ_NUM(obj);
      unsigned short obj_gen = OBJ_GEN(obj);
      pdf_release_obj(obj);
      obj = pdf_get_object(pf, obj_num, obj_gen);
    } else {
      pdf_obj *next_obj = OBJ_OBJ(obj);
      if (!next_obj) {
        ERROR("Undefined object reference"); 
      }
      pdf_release_obj(obj);
      obj = pdf_link_obj(next_obj);
    }
  }

  if (!count)
    ERROR("Loop in object hierarchy detected. Broken PDF file?");

  if (PDF_OBJ_NULLTYPE(obj)) {
    pdf_release_obj(obj);
    return NULL;
  } else
    return obj;
}

static void
extend_xref (pdf_file *pf, int new_size) 
{
  unsigned int i;

  pf->xref_table = RENEW(pf->xref_table, new_size, xref_entry);
  for (i = pf->num_obj; i < new_size; i++) {
    pf->xref_table[i].direct   = NULL;
    pf->xref_table[i].indirect = NULL;
    pf->xref_table[i].type     = 0;
    pf->xref_table[i].field3 = 0;
    pf->xref_table[i].field2 = 0L;
  }
  pf->num_obj = new_size;
}

/* Returns < 0 for error, 1 for success, and 0 when xref stream found. */
static int
parse_xref_table (pdf_file *pf, int xref_pos)
{
  FILE       *pdf_input_file = pf->file;
  const char *p, *endptr;
  char        buf[256]; /* See, PDF ref. v.1.7, p.91 for "255+1" here. */
  int         len;

  /*
   * This routine reads one xref segment. It may be called multiple times
   * on the same file.  xref tables sometimes come in pieces.
   */
  seek_absolute(pf->file, xref_pos);
  len = mfreadln(buf, 255, pdf_input_file);
  /* We should have already checked that "startxref" section exists.
   * So, EOF here (len = -1) is impossible.  We don't treat too long line
   * case seriously.
   */
  if (len < 0) {
    WARN("Something went wrong while reading xref table...giving up.");
    return -1;
  }
  p      = buf;
  endptr = buf + len;
  /* No skip_white() here. There should not be any white-spaces here. */
  if (memcmp(p, "xref", strlen("xref"))) {
    /* Might be an xref stream and not an xref table */
    return 0;
  }
  p += strlen("xref");
  skip_white(&p, endptr);
  if (p != endptr) {
    WARN("Garbage after \"xref\" keyword found.");
    return -1;
  }

  /* Next line in file has first item and size of table */
  for (;;) {
    char         flag;
    unsigned int current_pos;
    int          i;
    uint32_t     first, size, offset, obj_gen;

    current_pos = tell_position(pdf_input_file);
    len = mfreadln(buf, 255, pdf_input_file);
    if (len == 0) /* empty line... just skip. */
      continue;
    else if (len < 0) {
      WARN("Reading a line failed in xref table.");
      return -1;
    }

    p      = buf;
    endptr = buf + len;
    skip_white(&p, endptr);
    if (p == endptr) { /* Only white-spaces and/or comment found. */
      continue;
    } else if (!strncmp(p, "trailer", strlen ("trailer"))) {
      /*
       * Backup... This is ugly, but it seems like the safest thing to
       * do.  It is possible the trailer dictionary starts on the same
       * logical line as the word trailer.  In that case, the mfgets
       * call might have started to read the trailer dictionary and
       * parse_trailer would fail.
       */
      current_pos += p - buf; /* Jump to the beginning of "trailer" keyword. */
      seek_absolute(pdf_input_file, current_pos);
      break;
    }
    /* Line containing something other than white-space characters found.
     *
     * Start reading xref subsection
     *
     * This section just reads two nusigned integers, namely, the object number
     * of first object and the size of the xref subsection. PDF reference says
     * that only "a space" is allowed between those two numbers but we allow
     * more white-space characters.
     */
    {
      char *q;

      /* Object number of the first object whithin this xref subsection. */
      q = parse_unsigned(&p, endptr);
      if (!q) {
        WARN("An unsigned integer expected but could not find. (xref)");
        return -1;
      }
      first = atoi(q);
      RELEASE(q);
      skip_white(&p, endptr);

      /* Nnumber of objects in this xref subsection. */
      q = parse_unsigned(&p, endptr);
      if (!q) {
        WARN("An unsigned integer expected but could not find. (xref)");
        return -1;
      }
      size = atoi(q);
      RELEASE(q);
      skip_white(&p, endptr);

      /* Check for unrecognized tokens */
      if (p != endptr) {
        WARN("Unexpected token found in xref table.");
        return -1;
      }
    }

    /* The first line of a xref subsection OK. */
    if (pf->num_obj < first + size) {
      extend_xref(pf, first + size);
    }

    /* Start parsing xref subsection body... */
    for (i = first; i < first + size; ) {
     /* PDF spec. requires each xref subsection lines being exactly 20 bytes
      * long [including end-of-line marker(s)], offset 10 decimal digits,
      * generation number being 5 decimal digits, and each entries delimitted
      * by "a single space". However, we don't srtictly follow this rule:
      * More than one "white-spaces" allowed, can be ended with a comment,
      * and so on.
      */
      len = mfreadln(buf, 255, pdf_input_file);
      if (len == 0) /* empty line...just skip. */
        continue;
      else if (len < 0) {
        WARN("Something went wrong while reading xref subsection...");
        return -1;
      }
      p      = buf;
      endptr = buf + len;
      skip_white(&p, endptr);
      if (p == endptr) /* Only white-spaces and/or comment. */
        continue;

      /*
       * Don't overwrite positions that have already been set by a
       * modified xref table.  We are working our way backwards
       * through the reference table, so we only set "position" 
       * if it hasn't been set yet.
       */
      offset = 0UL; obj_gen = 0; flag = 0;
      {
        char *q;

        /* Offset value -- 10 digits (0 padded) */
        q = parse_unsigned(&p, endptr);
        if (!q) {
          WARN("An unsigned integer expected but could not find. (xref)");
          return -1;
        } else if (strlen(q) != 10) { /* exactly 10 digits */
          WARN(("Offset must be a 10 digits number. (xref)"));
          RELEASE(q);
          return -1;
        }
        /* FIXME: Possible overflow here. Consider using strtoll(). */
        offset = atoi(q);
        RELEASE(q);
        skip_white(&p, endptr);

        /* Generation number -- 5 digits (0 padded) */
        q = parse_unsigned(&p, endptr);
        if (!q) {
          WARN("An unsigned integer expected but could not find. (xref)");
          return -1;
        } else if (strlen(q) != 5) { /* exactly 5 digits */
          WARN(("Expecting a 5 digits number. (xref)"));
          RELEASE(q);
          return -1;
        }
        obj_gen = atoi(q);
        RELEASE(q);
        skip_white(&p, endptr);
      }
      if (p == endptr) {
        WARN("Unexpected EOL reached while reading a xref subsection entry.");
        return -1;
      }

      /* Flag -- a char */
      flag = *p; p++;
      skip_white(&p, endptr);
      if (p < endptr) {
        WARN("Garbage in xref subsection entry found...");
        return -1;
      } else if (((flag != 'n' && flag != 'f') ||
                  (flag == 'n' &&
                  (offset >= pf->file_size || (offset > 0 && offset < 4))))) {
        WARN("Invalid xref table entry [%lu]. PDF file is corrupt...", i);
        return -1;
      }

      /* Everything seems to be OK. */
      if (!pf->xref_table[i].field2) {
        pf->xref_table[i].type   = (flag == 'n');
        pf->xref_table[i].field2 = offset;
        pf->xref_table[i].field3 = obj_gen;
      }
      i++;
    }
  }

  return  1;
}

static unsigned int
parse_xrefstm_field (const char **p, int length, unsigned int def)
{
  unsigned int val = 0;

  if (!length)
    return def;

  while (length--) {
    val <<= 8;
    val |= (unsigned char) *((*p)++);
  }

  return val;
}

static int
parse_xrefstm_subsec (pdf_file *pf,
		      const char **p, int *length,
		      int *W, int wsum,
		      int first, int size) {
  xref_entry *e;

  if ((*length -= wsum*size) < 0)
    return -1;

  if (pf->num_obj < first+size)
    extend_xref(pf, first+size);  /* TODO: change! why? */

  e = pf->xref_table + first;
  while (size--) {
    unsigned char  type;
    unsigned int   field2;
    unsigned short field3;

    type = (unsigned char) parse_xrefstm_field(p, W[0], 1);
    if (type > 2)
      WARN("Unknown cross-reference stream entry type.");
#if 0
    /* Not sure */
    else if (!W[1] || (type != 1 && !W[2]))
      return -1;
#endif

    field2 = (unsigned int)  parse_xrefstm_field(p, W[1], 0);
    field3 = (unsigned short) parse_xrefstm_field(p, W[2], 0);

    if (!e->field2) {
      e->type   = type;
      e->field2 = field2;
      e->field3 = field3;	
      }
    e++;
  }

  return 0;
}

static int
parse_xref_stream (pdf_file *pf, int xref_pos, pdf_obj **trailer)
{
  pdf_obj *xrefstm, *size_obj, *W_obj, *index_obj;
  unsigned int size;
  int length;
  int W[3], i, wsum = 0;
  const char *p;

  xrefstm = pdf_read_object(0, 0, pf, xref_pos, pf->file_size);
  if (!PDF_OBJ_STREAMTYPE(xrefstm))
    goto error;

  {
    pdf_obj *tmp = pdf_stream_uncompress(xrefstm);
    if (!tmp)
      goto error;
    pdf_release_obj(xrefstm);
    xrefstm = tmp;
  }

  *trailer = pdf_link_obj(pdf_stream_dict(xrefstm));

  size_obj = pdf_lookup_dict(*trailer, "Size");
  if (!PDF_OBJ_NUMBERTYPE(size_obj))
    goto error;
  size = (unsigned int) pdf_number_value(size_obj);

  length = pdf_stream_length(xrefstm);

  W_obj = pdf_lookup_dict(*trailer, "W");
  if (!PDF_OBJ_ARRAYTYPE(W_obj) || pdf_array_length(W_obj) != 3)
    goto error;

  for (i = 0; i < 3; i++) {
    pdf_obj *tmp = pdf_get_array(W_obj, i);
    if (!PDF_OBJ_NUMBERTYPE(tmp))
      goto error;
    wsum += (W[i] = (int) pdf_number_value(tmp));
  }

  p = pdf_stream_dataptr(xrefstm);

  index_obj = pdf_lookup_dict(*trailer, "Index");
  if (index_obj) {
    unsigned int index_len;
    if (!PDF_OBJ_ARRAYTYPE(index_obj) ||
        ((index_len = pdf_array_length(index_obj)) % 2 ))
      goto error;

    i = 0;
    while (i < index_len) {
      pdf_obj *first = pdf_get_array(index_obj, i++);
      size_obj  = pdf_get_array(index_obj, i++);
      if (!PDF_OBJ_NUMBERTYPE(first) || !PDF_OBJ_NUMBERTYPE(size_obj) ||
          parse_xrefstm_subsec(pf, &p, &length, W, wsum,
            (int) pdf_number_value(first), (int) pdf_number_value(size_obj)))
        goto error;
    }
  } else if (parse_xrefstm_subsec(pf, &p, &length, W, wsum, 0, size))
      goto error;

  if (length)
    WARN("Garbage in xref stream.");

  pdf_release_obj(xrefstm);

  return 1;

 error:
  WARN("Cannot parse cross-reference stream.");
  if (xrefstm)
    pdf_release_obj(xrefstm);
  if (*trailer) {
    pdf_release_obj(*trailer);
    *trailer = NULL;
  }
  return 0;
}

/* TODO: parse Version entry */
static pdf_obj *
read_xref (pdf_file *pf)
{
  pdf_obj *trailer = NULL, *main_trailer = NULL;
  int      xref_pos;

  if (!(xref_pos = find_xref(pf->file)))
    goto error;

  while (xref_pos) {
    pdf_obj *prev;

    int res = parse_xref_table(pf, xref_pos);
    if (res > 0) {
      /* cross-reference table */
      pdf_obj *xrefstm;

      if (!(trailer = parse_trailer(pf)))
	goto error;

      if (!main_trailer)
	main_trailer = pdf_link_obj(trailer);

      if ((xrefstm = pdf_lookup_dict(trailer, "XRefStm"))) {
	pdf_obj *new_trailer = NULL;
	if (PDF_OBJ_NUMBERTYPE(xrefstm) &&
	    parse_xref_stream(pf, (int) pdf_number_value(xrefstm),
			      &new_trailer))
	  pdf_release_obj(new_trailer);
	else
	  WARN("Skipping hybrid reference section.");
	/* Many PDF 1.5 xref streams use DecodeParms, which we cannot
	   parse. This way we can use at least xref tables in hybrid
	   documents. Or should we better stop parsing the file?
	*/
      }

    } else if (!res && parse_xref_stream(pf, xref_pos, &trailer)) {
      /* cross-reference stream */
      if (!main_trailer)
	main_trailer = pdf_link_obj(trailer);
    } else
      goto error;

    if ((prev = pdf_lookup_dict(trailer, "Prev"))) {
      if (PDF_OBJ_NUMBERTYPE(prev))
	xref_pos = (int) pdf_number_value(prev);
      else
	goto error;
    } else
      xref_pos = 0;

    pdf_release_obj(trailer);
  }

#if 0
  if (!pdf_lookup_dict(main_trailer, "Root")) {
      WARN("Trailer doesn't have catalog. Is this a correct PDF file?");
      goto error;
    }
#endif

  return main_trailer;

 error:
  WARN("Error while parsing PDF file.");
  if (trailer)
    pdf_release_obj(trailer);
  if (main_trailer)
    pdf_release_obj(main_trailer);
  return NULL;
}

static struct ht_table *pdf_files = NULL;

static pdf_file *
pdf_file_new (FILE *file)
{
  pdf_file *pf;
  ASSERT(file);
  pf = NEW(1, pdf_file);
  pf->file    = file;
  pf->trailer = NULL;
  pf->xref_table = NULL;
  pf->catalog = NULL;
  pf->num_obj = 0;
  pf->version = 0;

  seek_end(file);
  pf->file_size = tell_position(file);

  return pf;
}

static void
pdf_file_free (pdf_file *pf)
{
  unsigned int i;

  if (!pf) {
    return;
  }

  for (i = 0; i < pf->num_obj; i++) {
    if (pf->xref_table[i].direct)
      pdf_release_obj(pf->xref_table[i].direct);
    if (pf->xref_table[i].indirect)
      pdf_release_obj(pf->xref_table[i].indirect);
  }

  RELEASE(pf->xref_table);
  if (pf->trailer)
    pdf_release_obj(pf->trailer);
  if (pf->catalog)
    pdf_release_obj(pf->catalog);

  RELEASE(pf);  
}

void
pdf_files_init (void)
{
  pdf_files = NEW(1, struct ht_table);
  ht_init_table(pdf_files, (void (*)(void *)) pdf_file_free);
}

int
pdf_file_get_version (pdf_file *pf)
{
  ASSERT(pf);
  return pf->version;
}

pdf_obj *
pdf_file_get_trailer (pdf_file *pf)
{
  ASSERT(pf);
  return pdf_link_obj(pf->trailer);
}

/* FIXME:
 * pdf_file_get_trailer() does pdf_link_obj() but
 * pdf_file_get_catalog() does not. Why?
 */
pdf_obj *
pdf_file_get_catalog (pdf_file *pf)
{
  ASSERT(pf);
  return pf->catalog;
}

pdf_file *
pdf_open (const char *ident, FILE *file)
{
  pdf_file *pf = NULL;

  ASSERT(pdf_files);

  if (ident)
    pf = (pdf_file *) ht_lookup_table(pdf_files, ident, strlen(ident));

  if (pf) {
    pf->file = file;
  } else {
    pdf_obj *new_version;
    int version = check_for_pdf_version(file);

    if (version < 10)
      WARN("Unrecognized PDF version specified for input PDF file: %d.%d",
        pdf_version/10, pdf_version%10);
    else if (version > pdf_version) {
      WARN("Trying to include PDF file with version (%d.%d), which is " \
           "newer than current output PDF setting (%d.%d).",
        version/10, version%10, pdf_version/10, pdf_version%10);
    }

    pf = pdf_file_new(file);
    pf->version = version;

    if (!(pf->trailer = read_xref(pf)))
      goto error;

    if (pdf_lookup_dict(pf->trailer, "Encrypt")) {
      WARN("PDF document is encrypted.");
      goto error;
    }

    pf->catalog = pdf_deref_obj(pdf_lookup_dict(pf->trailer, "Root"));
    if (!PDF_OBJ_DICTTYPE(pf->catalog)) {
      WARN("Cannot read PDF document catalog. Broken PDF file?");
      goto error;
    }

    new_version = pdf_deref_obj(pdf_lookup_dict(pf->catalog, "Version"));
    if (new_version) {
      unsigned int major, minor;

      if (!PDF_OBJ_NAMETYPE(new_version) ||
          sscanf(pdf_name_value(new_version), "%u.%u", &major, &minor) != 2) {
        pdf_release_obj(new_version);
        WARN("Illegal Version entry in document catalog. Broken PDF file?");
        goto error;
      }

      if (pf->version < major*10+minor)
        pf->version = major*10+minor;

      pdf_release_obj(new_version);
    }

    if (ident)
      ht_append_table(pdf_files, ident, strlen(ident), pf);
  }

  return pf;

 error:
  pdf_file_free(pf);
  return NULL;
}

void
pdf_close (pdf_file *pf)
{
  if (pf)
    pf->file = NULL;
}

void
pdf_files_close (void)
{
  ASSERT(pdf_files);
  ht_clear_table(pdf_files);
  RELEASE(pdf_files);
}

static int
check_for_pdf_version (FILE *file) 
{
  unsigned int major, minor;

  rewind(file);

  return (ungetc(fgetc(file), file) == '%' &&
	  fscanf(file, "%%PDF-%u.%u", &major, &minor) == 2) ? (major*10+minor) : -1;
}

int
check_for_pdf (FILE *file) 
{
  int version = check_for_pdf_version(file);

  if (version < 0)  /* not a PDF file */
    return 0;

  if (version <= pdf_version)
    return 1;

  return 1;
}

static inline int
import_dict (pdf_obj *key, pdf_obj *value, void *pdata)
{
  pdf_obj *copy;
  pdf_obj *tmp;

  copy = (pdf_obj *) pdata;

  tmp  = pdf_import_object(value);
  if (!tmp) {
    return -1;
  }
  pdf_add_dict(copy, pdf_link_obj(key), tmp);

  return 0;
}

static pdf_obj *
pdf_import_indirect (pdf_obj *object)
{
  pdf_file *pf = OBJ_FILE(object);
  unsigned int obj_num = OBJ_NUM(object);
  unsigned short obj_gen = OBJ_GEN(object);
  pdf_obj *ref;

  ASSERT(pf);

  if (!checklabel(pf, obj_num, obj_gen)) {
    WARN("Can't resolve object: %lu %u", obj_num, obj_gen);
    return pdf_new_null();
  }

  ref = pf->xref_table[obj_num].indirect;
  if (!ref) {
    pdf_obj *obj, *reserved, *imported;

    obj = pdf_get_object(pf, obj_num, obj_gen);
    if (!obj) {
      WARN("Could not read object: %lu %u", obj_num, obj_gen);
      return NULL;
    }

    /* Fix for circular reference issue
     *
     * Older version of dvipdfmx disallowed the following case of
     * circular reference:
     *   obj #1 --> << /Kids [2 0 R] >>
     *   obj #2 --> << /Parents [1 0 R] >>
     * The problem is in that dvipdfmx gives new labels to objects after they
     * are completely read.
     */
    reserved = pdf_new_null(); /* for reservation of label */
    pf->xref_table[obj_num].indirect = ref = pdf_new_ref(reserved);
    imported = pdf_import_object(obj);
    if (imported) {
      if (imported->label) {
        WARN("Imported object already has a label: obj_id=%lu", imported->label);
      }
      OBJ_OBJ(ref) = imported;
      imported->label = reserved->label;
      imported->generation = reserved->generation;
      reserved->label = 0;
      reserved->generation = 0;
      pdf_release_obj(imported);
    }

    pdf_release_obj(reserved);
    pdf_release_obj(obj);
  }
    
    return  pdf_link_obj(ref);
}

/*
 * pdf_import_object recursively copies the object and those
 * referenced by it and changes the indirect references so that
 * they refer to the current output file. New indirect references
 * are remembered, which avoids duplicating objects when they
 * are imported several times.
 */
pdf_obj *
pdf_import_object (pdf_obj *object)
{
  pdf_obj  *imported;
  pdf_obj  *tmp;
  int       i;

  switch (pdf_obj_typeof(object)) {

  case PDF_INDIRECT:
    if (OBJ_FILE(object)) {
      imported = pdf_import_indirect(object);
    } else {
      imported = pdf_link_obj(object);
    }
    break;

  case PDF_STREAM:
    {
      pdf_obj *stream_dict;

      tmp = pdf_import_object(pdf_stream_dict(object));
      if (!tmp)
	return NULL;

      imported    = pdf_new_stream(0);
      stream_dict = pdf_stream_dict(imported);
      pdf_merge_dict(stream_dict, tmp);
      pdf_release_obj(tmp);
      pdf_add_stream(imported,
		     pdf_stream_dataptr(object),
		     pdf_stream_length(object));
    }
    break;

  case PDF_DICT:

    imported = pdf_new_dict();
    if (pdf_foreach_dict(object, import_dict, imported) < 0) {
      pdf_release_obj(imported);
      return NULL;
    }

    break;

  case PDF_ARRAY:

    imported = pdf_new_array();
    for (i = 0; i < pdf_array_length(object); i++) {
      tmp = pdf_import_object(pdf_get_array(object, i));
      if (!tmp) {
	pdf_release_obj(imported);
	return NULL;
      }
      pdf_add_array(imported, tmp);
    }
    break;

  default:
    imported = pdf_link_obj(object);
  }

  return imported;
}


/* returns 0 if indirect references point to the same object */
int
pdf_compare_reference (pdf_obj *ref1, pdf_obj *ref2)
{
  pdf_indirect *data1, *data2;

  ASSERT(PDF_OBJ_INDIRECTTYPE(ref1) && PDF_OBJ_INDIRECTTYPE(ref2));

  data1 = (pdf_indirect *) ref1->data;
  data2 = (pdf_indirect *) ref2->data;

  return data1->pf != data2->pf || data1->label != data2->label
    || data1->generation != data2->generation;
}