File: process.c

package info (click to toggle)
exifprobe 2.0.1%2Bgit20170416.3c2b769-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 2,904 kB
  • sloc: ansic: 34,799; sh: 413; makefile: 82
file content (4751 lines) | stat: -rw-r--r-- 212,349 bytes parent folder | download | duplicates (2)
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
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
/*          EXIFPROBE - TIFF/JPEG/EXIF image file probe               */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */
/* Copyright (C) 2002, 2005 by Duane H. Hesser. All rights reserved.  */
/*                                                                    */
/* See the file LICENSE.EXIFPROBE for terms of use.                   */
/* =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- */

#ifndef lint
static char *ModuleId = "@(#) $Id: process.c,v 1.47 2005/07/25 22:05:10 alex Exp $";
#endif

/* Process segment types such as TIFF IFD, JPEG APP segments, and     */
/* JPEG basic segments.                                               */

#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>

#include "defs.h"
#include "summary.h"
#include "maker.h"
#include "datadefs.h"
#include "misc.h"
#include "tags.h"
#include "extern.h"
#include "ciff.h"

/* Read and decode a TIFF IFD, attempting to describe the location    */
/* and structure of the data as it is read. Entry values which live   */
/* at an offset from the entry are displayed following the last entry */
/* in the IFD (actually after the "next ifd offset") if               */
/* VALUE_AT_OFFSET is set in Print_options, so that most IFDS will    */
/* have a VALUES section following the entry lines, in just the       */
/* manner that the file is laid out by spec. Tag names for offset     */
/* values are repeated when the value is printed. If VALUE_AT_OFFSET  */
/* is not set, the value is printed immediately, unless the tag       */
/* represents a subsection, in which case the offset of the           */
/* subsection is printed as the tag value, and the subsection handled */
/* in a second pass, after all entries are done.                      */

/* Returns a file offset which represents the "highest" position of   */
/* the file pointer read by this routine and its subroutines, which   */
/* may be used by the caller to read the remainder of the file. This  */
/* number (less one) is printed as the offset of the end of the IFD.  */

/* At the same time, the function notices non-zero values of "next    */
/* ifd offset" which may be contained in this ifd, and passes it as   */
/* "max_offset" to routines which read subifds, on the assumption     */
/* that this ifd should not write into the next ifd. That value is    */
/* used to mark segments which appear to be out of order in the file. */
/* If the routine detects by this means that *this* ifd is outside    */
/* its natural bounds, and cannot be contained within its parent, it  */
/* returns 0, rather than the maximum value offset, so that the       */
/* parent routine will not improperly alter its maximum offset.       */

/* The arguments are the byteorder to be used when reading integer    */
/* data (usually taken from the TIFF header), the offset of the start */
/* of the directory, relative to the TIFF header, the offset of the   */
/* TIFF header from the beginning of the file, the sequence number of */
/* the IFD, and formatting values (indent and address width) to be    */
/* used when printing.                                                */

/* The routine also attempts to record the location, size, image type */
/* and compression method used for actual image data contained within */
/* or referenced by a tiff IFD. This information is printed at the    */
/* end, as a summary of images found in the file.                     */

unsigned long
process_tiff_ifd(FILE *inptr,unsigned short byteorder,unsigned long ifd_offset,
                    unsigned long fileoffset_base,unsigned long max_offset,
                    struct image_summary *summary_entry,char *parent_name,
                    int ifdtype,int ifdnum,int subifdnum,int indent)
{
    struct ifd_entry *entry_ptr = NULL;
    struct image_summary *tmp_summary_entry;
    unsigned long cur_ifd_offset,next_ifd_offset,current_offset;
    unsigned long start_entry_offset,entry_offset,value_offset;
    unsigned long value_start_offset = 0UL;
    unsigned long offset_limit = 0UL;
    unsigned long max_value_offset = 0UL;
    unsigned long max_ifd_offset = 0UL;
    unsigned long thn_offset = 0UL;     /* JPegInterchangeFormat      */
    unsigned long thn_length = 0UL;     /* JpegInterChangeFormat      */
    unsigned long tmp_length = 0L;
    unsigned long alt_length = 0L;
    unsigned long limit_offset = 0L;
    unsigned long filesize = 0UL;
    unsigned short marker,alt_byteorder = 0;
    int invalid_entry = 0;
    int value_is_offset = 0;
    int use_second_pass = 0;
    int status = 0;
    int chpr = 0;
    int entry_num,num_entries,i;
    char dirnamebuf[16];
    char *ifdname,*prefix,*dirname,*nameoftag,*tprefix;
    char *fulldirname = CNULL;
    char *listname = CNULL;

    if(inptr == (FILE *)0)
    {
        fprintf(stderr,"%s: no open file pointer to read TIFF IFD\n",
                Progname);
        return(0L);
    }
    if(Debug & OUT_DEBUG)
    {
        printf("PS=%d,",(Print_options & PRINT_SECTION) > 0);
        printf("PV=%d,",(Print_options & PRINT_VALUE) > 0);
        printf("PO=%d,",(Print_options & PRINT_OFFSET) > 0);
        printf("PVAO=%d\n",(Print_options & PRINT_VALUE_AT_OFFSET) > 0);
    }

    filesize = get_filesize(inptr);
    next_ifd_offset = ifd_offset + fileoffset_base;
    while(next_ifd_offset)
    {
        clearerr(inptr);
        /* max_offset, if set, is the maximum offset which the parent */
        /* is willing to claim. max_ifd_offset is the maximum offset  */
        /* reached by this ifd, which may be constrained by the       */
        /* "next_ifd_offset" of chained ifds.                         */
        cur_ifd_offset = next_ifd_offset;
        if(max_offset && (cur_ifd_offset > max_offset))
            prefix = ">";   /* outside the parent                     */
        else
            prefix = "@";

        print_tag_address(SECTION,cur_ifd_offset,indent,prefix);
        switch(ifdtype)
        {
            case TIFF_IFD:
                if(PRINT_SECTION)
                    chpr += printf("<IFD %d>",ifdnum);
                ifdname = "IFD";
                sprintf(dirnamebuf,"Ifd%d",ifdnum);
                dirname = dirnamebuf;
                break;
            case TIFF_SUBIFD:
                if(PRINT_SECTION)
                    chpr += printf("<SubIFD %d of IFD %d>",subifdnum,ifdnum);
                ifdname = "SubIFD";
                sprintf(dirnamebuf,"SubIfd%d",subifdnum);
                dirname = dirnamebuf;
                break;
            case INTEROP_IFD:
                if(PRINT_SECTION)
                    chpr += printf("<Interoperability SubIFD>");
                ifdname = "Interoperability SubIFD";
                dirname = "Interop";
                break;
            case GPS_IFD:
                if(PRINT_SECTION)
                    chpr += printf("<GPS SubIFD>");
                ifdname = "GPS SubIFD";
                dirname = "Gps";
                break;
            case EXIF_IFD:  /* This shouldn't happen                  */
                if(PRINT_SECTION)
                    chpr += printf("<EXIF IFD>");   
                ifdname = "EXIF IFD";
                dirname = "Exif";
                break;
            case MAKER_SUBIFD:
                ifdname = "MakerNote SubIFD";
                if(PRINT_SECTION)
                    chpr += printf("<%s>",ifdname);
                dirname = CNULL;
                break;
            default:
                if(PRINT_SECTION)
                    chpr += printf("<UNKNOWN IFD TYPE %d>",ifdtype);    /* SECTION */
                ifdname = "UNKNOWN IFD TYPE";
                dirname = CNULL;
                break;
        }
        if(dirname)
            listname = fulldirname = splice(parent_name,".",dirname);
        else
            listname = parent_name;

        num_entries = read_ushort(inptr,byteorder,cur_ifd_offset);
        max_value_offset = max_ifd_offset = 0L;
        if(ferror(inptr) || feof(inptr))
        {
            chpr = newline(chpr);
            printred("#========= WARNING: FAILED to read number of entries for IFD at offset ");
            chpr += printf("%lu",cur_ifd_offset);
            if(ferror(inptr))
                why(stdout);
            chpr = newline(chpr);
            goto blewit;
        }

        /* The file pointer is now at the start of the IFD entries    */
        current_offset = entry_offset = start_entry_offset = ftell(inptr);
        if(PRINT_SECTION)
        {
            chpr += printf(" %d entries ",num_entries);
            chpr += printf("starting at file offset %#lx=%lu",
                                            start_entry_offset,start_entry_offset);
            chpr = newline(chpr);
        }

        if((summary_entry == NULL) || summary_entry->entry_lock)
            summary_entry = new_summary_entry(summary_entry,0,ifdtype);
        else
            summary_entry = last_summary_entry(summary_entry);
        if(summary_entry)
        {
            summary_entry->datatype = ifdtype;
            summary_entry->imageformat = IMGFMT_NOIMAGE;
            summary_entry->filesubformat |= FILESUBFMT_TIFF;
            if((ifdtype == TIFF_IFD) || (ifdtype == TIFF_SUBIFD) || (ifdtype == MAKER_SUBIFD))
            {
                summary_entry->ifdnum = ifdnum;
                summary_entry->subifdnum = subifdnum;
            }
        }
        use_second_pass = value_is_offset = 0;

        indent += SMALLINDENT;
        for(entry_num = 0; entry_num < num_entries; ++entry_num)
        {
            entry_ptr = read_ifd_entry(inptr,byteorder,entry_offset);

            if((entry_ptr->value_type == 0) || (entry_ptr->value_type > DOUBLE) ||
                ferror(inptr) || feof(inptr))
            {
                print_tag_address(ENTRY,entry_offset,indent,prefix);
                print_taginfo(entry_ptr,listname,SMALLINDENT,ifdtype,ifdnum,subifdnum);
                if((PRINT_ENTRY))
                    printred(" INVALID ENTRY");
                chpr = newline(chpr);

                /* If there are a few invalid entries in an otherwise */
                /* valid IFD, the invalid entries should be reported. */
                /* An invalid IFD, with 50,000 entries or so, all     */
                /* invalid, should be nipped in the bud.              */

                clearerr(inptr);
                current_offset = ftell(inptr);
                if(max_offset > 0)
                    limit_offset = max_offset;
                else
                {
                    if(fseek(inptr,0L,SEEK_END) != -1)
                    {
                        limit_offset = ftell(inptr);
                        fseek(inptr,current_offset,SEEK_SET);
                    }
                }
                /* If there's an error on input, or we can't check    */
                /* for absurd num_entries, give up.                   */
                if(!ferror(inptr) && !feof(inptr) && (limit_offset > 0))
                {
                    /* If the number of entries would read past the   */
                    /* size of the IFD, or past EOF, give up          */
                    if((ifd_offset + (12 * num_entries)) < limit_offset)
                    {
                        /* Limit the number of consecutive failures.  */
                        /* An apparently valid entry resets the count */
                        /* to 0.                                      */
                        if(invalid_entry++ < MAX_INVALID_ENTRIES)
                        {
                            entry_offset = current_offset;
                            clearerr(inptr);
                            continue;
                        }
                    }
                }
                chpr = newline(chpr);
                goto blewit;
            }
            invalid_entry = 0;
            current_offset = ftell(inptr);

            switch(entry_ptr->tag)
            {
                case TIFFTAG_OldSubFileType:   /* old, deprecated        */
                    if(summary_entry)
                    {
                        summary_entry->filesubformat |= FILESUBFMT_TIFFEP;
                        if(entry_ptr->value == 1)
                            summary_entry->subfiletype = PRIMARY_TYPE;
                        else if(entry_ptr->value == 2)
                            summary_entry->subfiletype = REDUCED_RES_TYPE;
                        else if(entry_ptr->value == 3)
                            summary_entry->subfiletype = PAGE_TYPE;
                    }
                    break;
                case TIFFTAG_NewSubFileType:
                    if(summary_entry)
                    {
                        summary_entry->filesubformat |= FILESUBFMT_TIFFEP;
                        if(entry_ptr->value == 0)
                            summary_entry->subfiletype = PRIMARY_TYPE;
                        else if(entry_ptr->value == 1)
                            summary_entry->subfiletype = REDUCED_RES_TYPE;
                        else if(entry_ptr->value == 2)
                            summary_entry->subfiletype = PAGE_TYPE;
                        else if(entry_ptr->value == 3)
                            summary_entry->subfiletype = MASK_TYPE;
                    }
                    break;
                case INTEROPTAG_RelatedImageWidth: /* ###%%% ???      */
                case TIFFTAG_ImageWidth:
                    if(summary_entry)
                        summary_entry->pixel_width = entry_ptr->value;
                    break;
                case INTEROPTAG_RelatedImageLength: /* ###%%% ???     */
                case TIFFTAG_ImageLength:
                    if(summary_entry)
                        summary_entry->pixel_height = entry_ptr->value;
                    break;
                case TIFFTAG_Compression:
                    if(summary_entry)
                    {
                        summary_entry->compression = entry_ptr->value;
                        if((entry_ptr->value == 7) || (entry_ptr->value == 6))
                        {
                            if(entry_ptr->value == 6)
                                summary_entry->filesubformat |= FILESUBFMT_TIFFOLD;
                            /* Some of this may have to be undone     */
                            /* later, if we find out they lied        */
                            summary_entry->entry_lock = lock_number(summary_entry);
                            if(summary_entry->imageformat == 0)
                                summary_entry->imageformat = IMGFMT_JPEG;
                        }
                        else
                        {
                            summary_entry->entry_lock = lock_number(summary_entry);

                            if(summary_entry->imageformat == 0)
                                summary_entry->imageformat = IMGFMT_TIFF;
                            if(summary_entry->compression == 34713)
                                summary_entry->filesubformat |= FILESUBFMT_NEF;
                            /* If there is no subfiletype tag, this   */
                            /* could be the primary, or it could be a */
                            /* thumbnail                              */
                            if(summary_entry->subfiletype <= POSSIBLE_PRIMARY_TYPE)
                                summary_entry->subfiletype = POSSIBLE_PRIMARY_TYPE;
                        }
                    }
                    break;
                case TIFFTAG_PhotometricInterpretation:
                    /* It appears that the PMI values used in Olympus */
                    /* ORF files (1 or 2 for the primary image) do    */
                    /* not correspond to legitimate TIFF PMI values.  */
                    /* Record them "as is" here, but they shouldn't   */
                    /* be *interpreted* in the same way as TIFF files */
                    if(summary_entry)
                        summary_entry->imagesubformat = entry_ptr->value | IMGSUBFMT_VALUE_IS_PMI;
                    break;
                case TIFFTAG_JPEGInterchangeFormat:
                    /* actually an offset...                          */
                    /* This method of JPEG-in-TIFF is discouraged by  */
                    /* the TIFF spec (after Technote 2), for good and */
                    /* sufficient reason>                             */
                    thn_offset = entry_ptr->value + fileoffset_base;
                    break;
                case TIFFTAG_JPEGInterchangeFormatLength:
                    thn_length = entry_ptr->value;
                    break;
                case TIFFTAG_StripOffsets:
                case TIFFTAG_TileOffsets:
                    /* If there are multiple strips, this will be     */
                    /* wrong, but will be overwritten in the second   */
                    /* pass.                                          */
                    if(summary_entry)
                    {
                        summary_entry->offset = entry_ptr->value;
                        if((summary_entry->compression != 6) &&
                                        (summary_entry->compression != 7))
                        {
                            summary_entry->entry_lock = lock_number(summary_entry);
                        }
                    }
                    break;
                case TIFFTAG_StripByteCounts:
                case TIFFTAG_TileByteCounts:
                    /* This may also be overwritten in the second     */
                    /* pass                                           */
                    if(summary_entry)
                    {
                        summary_entry->length = entry_ptr->value;
                        if((summary_entry->compression != 6) &&
                                        (summary_entry->compression != 7))
                        {
                            summary_entry->entry_lock = lock_number(summary_entry);
                        }
                    }
                    break;
                case TIFFEPTAG_TIFF_EPStandardID:
                    if(summary_entry)
                        summary_entry->filesubformat |= FILESUBFMT_TIFFEP;
                    break;
                case TIFFTAG_Make:
                case TIFFTAG_Model:
                case TIFFTAG_Software:
                    {
                        char *makename,*modelname,*swname;

                        /* These items will be read and printed in    */
                        /* the second pass, if one is made. This      */
                        /* grabs them and records them in globals,    */
                        /* which may be used later by the makernote   */
                        /* code.                                      */
                        switch(entry_ptr->tag)
                        {
                            case TIFFTAG_Make:
                                if(Make_name == NULL)
                                {
                                    Make_name = strdup_value(entry_ptr,
                                                             inptr,
                                                             fileoffset_base);
                                }
                                break;
                            case TIFFTAG_Model:
                                if(Model_name == NULL)
                                {
                                    Model_name = strdup_value(entry_ptr,
                                                              inptr,
                                                              fileoffset_base);
                                }
                                break;
                            case TIFFTAG_Software:
                                if(Software_name == NULL)
                                {
                                    Software_name = strdup_value(entry_ptr,
                                                                 inptr,
                                                                 fileoffset_base);
                                }
                                break;
                        }
                    }
                    break;
                    case TIFFTAG_BitsPerSample:
                        if(summary_entry)
                        {
                            /* Do not override information from       */
                            /* earlier sections; the tiff section in  */
                            /* e.g. MRW sections can lie if the image */
                            /* has been improperly handled by         */
                            /* other-party software.                  */
                            if(summary_entry->bps[0] == 0)
                            {
                                if(entry_ptr->count < 3)
                                {
                                    summary_entry->bps[0] = (unsigned short)entry_ptr->value & 0xffff;
                                    if(entry_ptr->count == 2)
                                        summary_entry->bps[1] =
                                            (unsigned short)((entry_ptr->value & 0xffff0000) >> 16);
                                }
                                else
                                {
                                    summary_entry->bps[0] = read_ushort(inptr,byteorder,entry_ptr->value);
                                    for(i = 1; (i < entry_ptr->count) && (i < MAXSAMPLE); ++i)
                                        summary_entry->bps[i] = read_ushort(inptr,byteorder,HERE);
                                }
                            }
                        }
                        break;
                    case TIFFTAG_SamplesPerPixel:
                        if((summary_entry) && (summary_entry->spp == 0))
                            summary_entry->spp = entry_ptr->value;
                        break;
                    case DNGTAG_DNGVersion:    /* A DNG-specific tag */
                        if(summary_entry)
                            summary_entry->filesubformat |= FILESUBFMT_DNG;
                        break;
                    case TIFFTAG_CR2_0xc5d9:    /* A CR2-specific tag */
                        if(summary_entry)
                        {
                            summary_entry->filesubformat |= FILESUBFMT_CR2;
                            if((summary_entry->compression == 6) &&
                                            (summary_entry->imagesubformat == IMGSUBFMT_RGB))
                            {
                                summary_entry->subfiletype = REDUCED_RES_TYPE;
                            }
                        }
                        break;
                    case TIFFTAG_CR2_0xc5d8:
                    case TIFFTAG_CR2_0xc5e0:
                    case TIFFTAG_CR2_SLICE:
                        /* These tags appear (so far) only in the     */
                        /* weird IFD containing the lossless jpeg     */
                        /* primary. It is possible to check for other */
                        /* things, e.g. just compression, strip       */
                        /* offset and bytecount (1 each), but for now */
                        /* just assume that any of these tags means   */
                        /* CR2. The compression at this point will be */
                        /* 6, but will be re-marked JPEG_SOF_3 when   */
                        /* the image is scanned.                      */
                        if(summary_entry)
                        {
                            summary_entry->filesubformat |= FILESUBFMT_CR2;
                            summary_entry->subfiletype = PRIMARY_TYPE;
                        }
                        break;
                case TIFFTAG_SubIFDtag:
                case EXIFTAG_ExifIFDPointer:
                case EXIFTAG_GPSInfoIFDPointer:
                case EXIFTAG_Interoperability: 
                case TIFFTAG_PrintIM:
                    use_second_pass++;
                    break;
                default:
                    break;
            }
            print_tag_address(ENTRY,entry_offset,indent,prefix);
            value_offset = print_entry(inptr,byteorder,entry_ptr,fileoffset_base,
                                                summary_entry,listname,ifdtype,
                                                ifdnum, subifdnum, SMALLINDENT);

            /* Keep track of how far into the file reading progresses */
            if(value_offset == 0UL)
                value_offset = current_offset;
            if(value_offset > max_value_offset)
                max_value_offset = value_offset;
            if((is_offset(entry_ptr)))
                ++use_second_pass;
            entry_offset = current_offset;
        }

        next_ifd_offset = read_ulong(inptr,byteorder,current_offset);
        if(next_ifd_offset > 0UL)
        {
            if((PRINT_VALUE) && (PRINT_SECTION) && (PRINT_VALUE_AT_OFFSET))
            {
                print_tag_address(SECTION,current_offset,indent,prefix);
                chpr += printf("**** next IFD offset %lu",next_ifd_offset);
                next_ifd_offset += fileoffset_base;
                chpr += printf("(+ %lu = %#lx/%lu)",fileoffset_base,
                            next_ifd_offset,next_ifd_offset);
            }
            else
                next_ifd_offset += fileoffset_base;

            /* Corrupt file will cause infinite loops. So we abort.
             * It is possible this can be worked around better.
             * See Issue #9 https://github.com/hfiguiere/exifprobe/issues/9
             */
            if(next_ifd_offset < ftell(inptr)) {
              printred("\nReading IFD backwards. INVALID FILE. ABORTING.\n");
              exit(1);
            } else if (next_ifd_offset > filesize) {
              printred("\nReading IFD past EOF. INVALID FILE. ABORTING.\n");
              exit(1);
            }
            /* We should be able to tolerate these */
            if ((ifdtype != TIFF_IFD) && (ifdtype != TIFF_SUBIFD))
            {
                if(PRINT_SECTION)
                    printred(" INVALID NEXT IFD OFFSET ");
                else
                {
                    chpr = newline(chpr);
                    printred("# ========= WARNING: INVALID NEXT IFD OFFSET ");
                    chpr += printf("%ld in ",next_ifd_offset);
                    /* Until someone creates a subifd in a subifd,    */
                    /* this is enough.                                */
                    if(ifdtype == TIFF_IFD)
                        chpr += printf("IFD %d =========",ifdnum);
                    else if(ifdtype == TIFF_SUBIFD)
                        chpr += printf("SUBIFD %d of IFD %d =========",subifdnum,ifdnum);
                    else
                        chpr += printf("%s =========",ifdname);
                }
                next_ifd_offset = 0L;
            }
            /* This is the limit of offsets which should be part of   */
            /* this ifd; a limit may have been passed in from the     */
            /* parent, if it had a length of this section. This will  */
            /* replace that number unconditionally, although it may   */
            /* be that one should be checked against the other...     */
            /* This can be checked against max_value_offset, and      */
            /* value_offset in the second pass.                       */
            max_offset = next_ifd_offset;
            chpr = newline(chpr);
        }
        else
        {
            if((PRINT_VALUE) && (PRINT_SECTION) && (PRINT_VALUE_AT_OFFSET))
            {
                print_tag_address(SECTION,current_offset,indent,prefix);
                chpr += printf("**** next IFD offset 0");
                chpr = newline(chpr);
            }
        }

        if(max_value_offset < current_offset)
            max_ifd_offset = current_offset;    
        else
            max_ifd_offset = max_value_offset;

        if(Debug & END_OF_SECTION_DEBUG)
            printf("mo=%lu, mvo=%lu, mio=%lu, nio=%lu, ol=%lu\n",max_offset,max_value_offset,max_ifd_offset,next_ifd_offset,offset_limit);

        /* If we made it through the first pass, we should be able to */
        /* get some info from the second pass, so don't let a failure */
        /* here stop us. It will fail later.                          */
        if(ferror(inptr) || feof(inptr))
        {
            chpr += printf("#========= WARNING: READ NEXT IFD OFFSET FAILED =========");
            chpr = newline(chpr);
            why(stdout);
            clearerr(inptr);    /* keep going...                      */
        }
        else
            current_offset = ftell(inptr);

        value_offset = current_offset;
        if(use_second_pass)
        {
            if((PRINT_VALUE) && (PRINT_SECTION) && (PRINT_VALUE_AT_OFFSET))
            {
                print_tag_address(SECTION,value_offset,indent,prefix);
                chpr += printf("============= VALUES, ");

                if(ifdtype == TIFF_IFD)
                    chpr += printf("IFD %d",ifdnum);
                else if(ifdtype == TIFF_SUBIFD)
                    chpr += printf("SubIFD %d of IFD %d",subifdnum,ifdnum);
                else if(ifdtype == INTEROP_IFD)
                    chpr += printf("Interoperability SubIFD");
                else if(ifdtype == EXIF_IFD)    /* shouldn't happen   */
                    chpr += printf("EXIF IFD");
                else if(ifdtype == MAKER_SUBIFD)
                    chpr += printf("%s",ifdname);
                else
                    chpr += printf("UNKNOWN IFD TYPE %d",ifdtype);
                chpr += printf(" ============");
                chpr = newline(chpr);
            }

            /* Second pass, to evaluate entries which are stored      */
            /* indirectly (the value requires more than 4 bytes).     */

            /* This time we have to explicitly seek to each entry,    */
            /* since the value processing may send the file pointer   */
            /* off to exotic places.                                  */
            entry_offset = start_entry_offset;
            for(entry_num = 0; entry_num < num_entries; ++entry_num)
            {
                entry_ptr = read_ifd_entry(inptr,byteorder,entry_offset);
                if((entry_ptr->value_type == 0) || (entry_ptr->value_type > DOUBLE) ||
                    ferror(inptr) || feof(inptr))
                {
                    /* If the first pass made it through invalid      */
                    /* entries, this pass should just ignore them and */
                    /* quietly continue.                              */
                    clearerr(inptr);
                    entry_offset = current_offset = ftell(inptr);
                    continue;
                }
                entry_offset = current_offset = ftell(inptr);
                value_is_offset = is_offset(entry_ptr);
                switch(entry_ptr->tag)
                {
                    case TIFFTAG_SubIFDtag:
                        value_offset = process_subifd(inptr,byteorder,
                                            entry_ptr, fileoffset_base,0L,
                                            summary_entry,listname,
                                            ifdnum,++subifdnum,TIFF_SUBIFD,
                                            indent);
                        /* subifds are not part of the IFD; throw     */
                        /* away this value for now.                   */
                        value_offset = 0;
                        break;
                    case EXIFTAG_ExifIFDPointer:
                        value_start_offset = entry_ptr->value + fileoffset_base;
                        value_offset = process_exif_ifd(inptr,byteorder,
                                            entry_ptr->value,fileoffset_base,
                                            next_ifd_offset,summary_entry,listname,
                                            ifdnum,indent);
                        if(max_offset && (value_offset > max_offset))
                            offset_limit = value_offset;
                        break;
                    case EXIFTAG_GPSInfoIFDPointer:
                        value_offset = process_gps_ifd(inptr,byteorder,
                                            entry_ptr->value,fileoffset_base,
                                            next_ifd_offset,summary_entry,listname,
                                            ifdnum,indent);
                        break;
                    case EXIFTAG_Interoperability: 
                        /* This doesn't belong in a TIFF IFD, but     */
                        /* be prepared.                               */
                        /* Also we make sure we are not calling on    */
                        /* the same ifd offset                        */
                        PUSHCOLOR(INTEROP_COLOR);
                        if (entry_ptr->value != ifd_offset) {
                            value_offset = process_tiff_ifd(inptr,byteorder,
                                                entry_ptr->value,fileoffset_base,
                                                next_ifd_offset,summary_entry,
                                                listname,INTEROP_IFD,ifdnum,0,
                                                indent);
                        }
                        value_offset = 0;
                        POPCOLOR();
                        break;
                    case TIFFTAG_StripOffsets:
                    case TIFFTAG_TileOffsets:
                        /* Update the summary values set in the first */
                        /* pass                                       */
                        /* Bad strip offsets seem to be one of the    */
                        /* more common errors, especially when images */
                        /* have been processed by editing software.   */
                        /* Offsets greater than filesize are easy to  */
                        /* detect, so check for them, and test to see */
                        /* if they're just written with the wrong     */
                        /* byteorder.                                 */
                        
                        /* ###%%% need to record number of offsets    */
                        /* and location of offsets for multi-tile     */
                        /* (multi-strip?) images jpeg-compressed by   */
                        /* tile (seen in DNG)                         */

                       if((value_is_offset) && summary_entry)
                        {
                            alt_byteorder = byteorder;
                            summary_entry->chunkhow = entry_ptr->tag;
                            summary_entry->chunktype = entry_ptr->value_type;
                            /* ###%%% combine short and long cases; just switch read_xxx() */
                            if(entry_ptr->value_type == LONG)
                            {
                                unsigned long tmp_offset;

                                tmp_offset = read_ulong(inptr,byteorder,entry_ptr->value + fileoffset_base);
                                summary_entry->offset = tmp_offset;
                                summary_entry->offset_loc = entry_ptr->value +fileoffset_base;
                                summary_entry->noffsets = entry_ptr->count;
                                if(tmp_offset > filesize)
                                {
                                    chpr = newline(chpr);
                                    PUSHCOLOR(HI_RED);
                                    print_filename();
                                    chpr += printf("# WARNING: initial stripoffset (%#lx/%lu) > filesize...wrong byteorder? ",
                                                    tmp_offset,tmp_offset);
                                    print_byteorder(byteorder,1);
                                    chpr = newline(chpr);
                                    if(byteorder == TIFF_MOTOROLA)
                                        alt_byteorder = TIFF_INTEL;
                                    else
                                        alt_byteorder = TIFF_MOTOROLA;
                                    tmp_offset = read_ulong(inptr,alt_byteorder,
                                            entry_ptr->value + fileoffset_base);
                                    print_filename();
                                    chpr += printf("# WARNING: initial stripoffset using alt byteorder [");
                                    print_byteorder(alt_byteorder,1);
                                    chpr += printf("] is %#lx/%lu ",
                                                    tmp_offset,tmp_offset);
                                    chpr = newline(chpr);
                                    if(tmp_offset < filesize)
                                        summary_entry->offset = tmp_offset;
                                    POPCOLOR();
                                }
                            }
                            else
                            {
                                unsigned short tmp_offset;

                                tmp_offset = read_ushort(inptr,byteorder,entry_ptr->value + fileoffset_base);
                                summary_entry->offset = tmp_offset;
                                summary_entry->offset_loc = entry_ptr->value +fileoffset_base;
                                summary_entry->noffsets = entry_ptr->count;
                                if((unsigned long)tmp_offset > filesize)
                                {
                                    chpr = newline(chpr);
                                    PUSHCOLOR(HI_RED);
                                    print_filename();
                                    chpr += printf("# WARNING: initial stripoffset (%#x/%u) > filesize...wrong byteorder? ",
                                                    tmp_offset,tmp_offset);
                                    print_byteorder(byteorder,1);
                                    chpr = newline(chpr);
                                    if(byteorder == TIFF_MOTOROLA)
                                        alt_byteorder = TIFF_INTEL;
                                    else
                                        alt_byteorder = TIFF_MOTOROLA;
                                    tmp_offset = read_ushort(inptr,alt_byteorder,
                                                entry_ptr->value + fileoffset_base);
                                    print_filename();
                                    chpr += printf("# WARNING: initial stripoffset using alt byteorder  [");
                                    print_byteorder(alt_byteorder,1);
                                    chpr += printf("] is %#x/%u ",
                                                    tmp_offset,tmp_offset);
                                    chpr = newline(chpr);
                                    if(tmp_offset < filesize)
                                        summary_entry->offset = tmp_offset;
                                    POPCOLOR();
                                }
                            }
                        }
                        if((PRINT_VALUE) && ((PRINT_VALUE_AT_OFFSET) && value_is_offset))
                        {
                            print_tag_address(ENTRY,fileoffset_base + entry_ptr->value,
                                                indent,prefix);
                            print_tagid(entry_ptr,SMALLINDENT,ifdtype);
                            value_offset = 
                                print_offset_value(inptr,byteorder,entry_ptr,
                                                fileoffset_base,listname,
                                                ifdtype,indent,1);
                        }
                        break;
                    case TIFFTAG_StripByteCounts:
                    case TIFFTAG_TileByteCounts:
                        if((value_is_offset) && summary_entry)
                        {
                            tmp_length =
                                sum_strip_bytecounts(inptr,byteorder,
                                            entry_ptr->value + fileoffset_base,
                                            entry_ptr->count,entry_ptr->value_type);

                            summary_entry->length = tmp_length;
                            summary_entry->length_loc = entry_ptr->value + fileoffset_base;
                            if(summary_entry->noffsets != entry_ptr->count)
                                printred(" # Warning: number of bytecounts != number of offsets");
                            if(tmp_length > filesize)
                            {
                                chpr = newline(chpr);
                                PUSHCOLOR(HI_RED);
                                    print_filename();
                                chpr += printf("# WARNING: total stripbytecount (%#lx/%lu) > filesize (%lu) ... wrong byteorder? ",
                                                tmp_length,tmp_length,filesize);
                                print_byteorder(byteorder,1);
                                chpr = newline(chpr);
                                if(byteorder == TIFF_MOTOROLA)
                                    alt_byteorder = TIFF_INTEL;
                                else
                                    alt_byteorder = TIFF_MOTOROLA;
                                alt_length =
                                    sum_strip_bytecounts(inptr,alt_byteorder,
                                                entry_ptr->value + fileoffset_base,
                                                entry_ptr->count,entry_ptr->value_type);
                                print_filename();
                                chpr += printf("# WARNING: total stripbytecount using alt byteorder [");
                                print_byteorder(alt_byteorder,1);
                                chpr += printf("] is %#lx/%lu ",
                                                alt_length,alt_length);
                                chpr = newline(chpr);
                                if(alt_length < filesize)
                                    summary_entry->length = alt_length;
                                else
                                {
                                    print_filename();
                                    chpr += printf("# WARNING: file may be truncated");
                                    chpr = newline(chpr);
                                }
                                POPCOLOR();
                            }
                        }
                        if((PRINT_VALUE) && ((PRINT_VALUE_AT_OFFSET) && value_is_offset))
                        {
                            print_tag_address(ENTRY,fileoffset_base + entry_ptr->value,
                                                indent,prefix);
                            print_tagid(entry_ptr,SMALLINDENT,ifdtype);
                            value_offset =
                                print_offset_value(inptr,byteorder,entry_ptr,
                                                fileoffset_base,listname,
                                                ifdtype,indent,1);
                            /* ###%%% find a way to print total       */
                            /* ByteCount, at least in LIST mode       */
                        }
                        break;
                    case TIFFTAG_JPEGTables:
                        nameoftag = tagname(entry_ptr->tag);
                        if((PRINT_VALUE) && ((PRINT_VALUE_AT_OFFSET) && value_is_offset))
                        {
                            if(PRINT_SECTION)
                            {
                                chpr = newline(chpr);
                                print_tag_address(ENTRY,fileoffset_base + entry_ptr->value,
                                                    indent,prefix);
                                chpr += printf("# Start of %s length %lu",nameoftag,
                                                            entry_ptr->count);
                                chpr = newline(chpr);
                            }
                        }
                        marker = read_ushort(inptr,TIFF_MOTOROLA,fileoffset_base + entry_ptr->value);
                        /* Need a new entry; this IFD's entry may be  */
                        /* used for an image, but may not be locked   */
                        /* yet.                                       */
                        tmp_summary_entry = new_summary_entry(summary_entry,0,IMGFMT_JPEG);
                        value_offset = process_jpeg_segments(inptr,
                                                    fileoffset_base + entry_ptr->value,
                                                    marker,entry_ptr->count,
                                                    tmp_summary_entry,listname,
                                                    prefix,indent+SMALLINDENT);
                        if(tmp_summary_entry)
                            tmp_summary_entry->imagesubformat = TIFFTAG_JPEGTables;
                        if((PRINT_VALUE) && ((PRINT_VALUE_AT_OFFSET) && value_is_offset))
                        {
                            if((PRINT_SECTION))
                            {
                                /* ###%% check for early EOI;         */
                                if((status = jpeg_status(0) == JPEG_EARLY_EOI))
                                    chpr = newline(chpr);
                                jpeg_status(status);
                                print_tag_address(ENTRY,fileoffset_base + entry_ptr->value + entry_ptr->count + 1,
                                                    indent,prefix);
                                chpr += printf("# End of %s",nameoftag);
                                print_jpeg_status();
                                chpr = newline(chpr);
                            }
                        }
                        break;
                    case TIFFTAG_PrintIM:
                        if(next_ifd_offset && (cur_ifd_offset > next_ifd_offset))
                            prefix = "+";
                        else
                            prefix = "@";
                        process_pim(inptr,byteorder,entry_ptr->value,fileoffset_base,
                                    entry_ptr->count,tagname(entry_ptr->tag),
                                    listname,prefix,indent);
                        chpr = newline(0);
                        break;
                    default:
                        if((PRINT_VALUE) && ((PRINT_VALUE_AT_OFFSET) && value_is_offset))
                        {
                            print_tag_address(ENTRY,fileoffset_base + entry_ptr->value,
                                                    indent,prefix);
                            print_tagid(entry_ptr,SMALLINDENT,ifdtype);
                            value_offset =
                                print_offset_value(inptr,byteorder,entry_ptr,
                                                fileoffset_base,listname,
                                                ifdtype,indent,1);
                        }
                        break;
                }
                if(value_offset == 0L) /* bad entry; try the next one */
                    clearerr(inptr);
                else if(value_offset > filesize)
                    goto blewit;
                else if(value_offset > max_value_offset)
                    max_value_offset = value_offset;
            }
        }
        else if(current_offset > max_value_offset)
            max_value_offset = current_offset;
        max_ifd_offset = max_value_offset;

        indent -= SMALLINDENT;

        /* max_ifd_offset is the maximum offset we've read so far;    */
        /* max_offset, if set, is the highest offset of this ifd,     */
        /* according to either the parent or the recently-read        */
        /* next_ifd_offset.                                           */

        if(max_offset && (max_ifd_offset > max_offset))
            prefix = "?";
        if(Debug & END_OF_SECTION_DEBUG)
            printf("mo=%lu, mvo=%lu, mio=%lu, nio=%lu, ol=%lu, prefix=%s\n",
                                max_offset,max_value_offset,max_ifd_offset,
                                next_ifd_offset,offset_limit,prefix);

        /* ================== show jpeg thumbnail =================== */
        if(thn_offset || thn_length)
        {
            int startindent;

            if(Debug & END_OF_SECTION_DEBUG)
                printf("mo=%lu, ol=%lu, thno=%lu\n",max_offset,offset_limit,thn_offset);
            /* This will be set only if a JpegInterchangeFormat tag   */
            /* has been seen, so this must be a JPEG thumbnail. The   */
            /* Exif spec indicates that the data should be written    */
            /* within the values section of the IFD, so scan and      */
            /* report here. If it appears to be written outside the   */
            /* bounds of the IFD, mark it. "max_offset" is the        */
            /* parent's idea (if any) of the extent of the IFD.       */
            /* "offset_limit" is the beginning of the Exif section,   */
            /* if present.                                            */

            if(max_offset && (thn_offset > max_offset))
                tprefix = ">";
            else if(offset_limit && (thn_offset > offset_limit))
                tprefix = "+";
            else
                tprefix = prefix;
            startindent = charsprinted();
            print_tag_address(SECTION,thn_offset,indent + SMALLINDENT,tprefix);
            if(PRINT_SECTION)
            {
                if(thn_length)
                    chpr += printf("#### Start of JPEG thumbnail data for IFD %d",ifdnum);
                else
                {
                    PUSHCOLOR(RED);
                    chpr += printf("#### ZERO LENGTH JPEG thumbnail for IFD %d",ifdnum);
                    POPCOLOR();
                }
                if((ifdtype == TIFF_SUBIFD) || (subifdnum >= 0))
                    chpr += printf(" SubIFD %d",subifdnum);
                if(ifdtype == MAKER_IFD)
                    chpr += printf(" %s",ifdname);
                else if(ifdtype == MAKER_SUBIFD)
                    chpr += printf(" %s",ifdname);
                if(thn_length)
                    chpr += printf(", length %lu ####",thn_length);
                else
                {
                    if(summary_entry && (summary_entry->imageformat == IMGFMT_JPEG)
                        && ((summary_entry->compression == 6) || (summary_entry->compression == 7)))
                    {
                        summary_entry->offset = thn_offset;
                    }
                }
                chpr = newline(chpr);
            } 

            if(thn_length)
            {
                unsigned short tmp_marker;

                marker = read_ushort(inptr,TIFF_MOTOROLA,thn_offset);
                if(marker != JPEG_SOI)
                {
                    if((marker & 0xff) != 0xd) /* possibly BAD_SOI    */
                    {
                        tmp_marker = read_ushort(inptr,TIFF_MOTOROLA,thn_offset - fileoffset_base);
                        if(tmp_marker == JPEG_SOI)
                        {
                            chpr = newline(chpr);
                            print_filename();
                            putchar('#');
                            if((PRINT_SECTION))
                                putindent(startindent - 1);
                            PUSHCOLOR(HI_RED);
                            chpr += printf(" WARNING: JPEGInterChangeFormat offset is botched");
                            chpr = newline(chpr);
                            if((PRINT_SECTION))
                            {
                                print_filename();
                                putchar('#');
                                putindent(startindent - 1);
                                chpr += printf(" WARNING: (possibly) correct offset follows:");
                                chpr = newline(chpr);
                            }
                            marker = tmp_marker;
                            thn_offset -= fileoffset_base;
                            print_tag_address(SECTION,thn_offset,indent + SMALLINDENT,tprefix);
                            if((LIST_MODE))
                            {
                                /* mock up the entry; the last read   */
                                /* entry is finished, so it can be    */
                                /* overwritten                        */
                                entry_ptr->tag = TIFFTAG_JPEGInterchangeFormat;
                                entry_ptr->value_type = LONG;
                                entry_ptr->count = 1;
                                entry_ptr->value = thn_offset - fileoffset_base;
                                value_offset = print_entry(inptr,byteorder,entry_ptr,fileoffset_base,
                                                            summary_entry,parent_name,ifdtype,
                                                            ifdnum, subifdnum,MEDIUMINDENT);
                            }
                            else if((PRINT_SECTION))
                                chpr += printf("#### Start of JPEG thumbnail data for IFD %d",ifdnum);
                            POPCOLOR();
                            chpr = newline(chpr);
                        }
                    }
                }
                /* Is this the image described by this ifd, or an     */
                /* add-on?  ...or both...                             */
                if(summary_entry &&
                    ((summary_entry->compression == 7) || (summary_entry->compression == 6)))
                {
                    /* If there's a length set in the summary, assume */
                    /* there's already an image associated with this  */
                    /* entry.                                         */
                    if((summary_entry->length == 0UL))
                    {
                        /* Othersise, make sure the jpeg processor    */
                        /* uses the current summary entry             */
                        summary_entry->entry_lock = 0;
                        summary_entry->imageformat = IMGFMT_NOIMAGE;
                        if(Debug & SCAN_DEBUG)
                        {
                            chpr = newline(chpr);
                            printred("DEBUG:");
                            chpr += printf(" UNLOCK **** ");
                            chpr += printf(" offset=%lu, length=%lu ",summary_entry->offset,
                                                                    summary_entry->length);
                            chpr = newline(chpr);
                        }
                    }
                }

                value_offset = 0;
                if(marker)
                    value_offset = process_jpeg_segments(inptr,thn_offset,marker,
                                                thn_length,summary_entry,parent_name,
                                                tprefix,indent+SMALLINDENT);

                /* Even if the jpeg was bad, there will be a summary  */
                /* entry for it.  It may not be the current one.      */
                tmp_summary_entry = last_summary_entry(summary_entry);
                tmp_summary_entry->ifdnum = ifdnum;
                tmp_summary_entry->subifdnum = subifdnum;
                tmp_summary_entry->datatype = ifdtype;
                    
                if(PRINT_SECTION)
                {
                    if((status = jpeg_status(0) == JPEG_EARLY_EOI))
                        chpr = newline(chpr);
                    jpeg_status(status);
                    print_tag_address(SECTION,thn_offset + thn_length - 1,indent + SMALLINDENT,tprefix);
                    chpr += printf("#### End of JPEG thumbnail data for IFD %d",ifdnum);
                    if((ifdtype == TIFF_SUBIFD) || (subifdnum >= 0))
                        chpr += printf(" SubIFD %d",subifdnum);
                    if(ifdtype == MAKER_IFD)
                        chpr += printf(" %s",ifdname);
                    else if(ifdtype == MAKER_SUBIFD)
                        chpr += printf(" %s",ifdname);
                    chpr += printf(", length %lu ####",thn_length);
                    print_jpeg_status();
                    chpr = newline(chpr);
                }

                if(Debug & END_OF_SECTION_DEBUG)
                    printf("vo=%lu, mo=%lu, mvo=%lu, mio=%lu, nio=%lu, ol=%lu\n",value_offset,
                            max_offset,max_value_offset,max_ifd_offset,next_ifd_offset,offset_limit);
                if((value_offset == 0L) || (value_offset > (thn_offset + thn_length)))
                    value_offset = thn_offset + thn_length;
                if(value_offset > max_value_offset)
                    max_value_offset = value_offset;
            }
            /* done with this thumbnail                               */
            thn_offset = thn_length = 0;
            if((max_value_offset > max_ifd_offset) && 
                                    ((offset_limit == 0) ||
                                        (max_value_offset < offset_limit)))
            {
                max_ifd_offset = max_value_offset;
            }
        }
        /* ================== end show jpeg thumbnail =============== */

        /* The TIFF spec does not specify, directly, where an IFD     */
        /* ends. If there is a next IFD, the start of that IFD        */
        /* (next_ifd_offset) gives an upper bound for the current     */
        /* IFD, but for the last IFD, guessing is necessary. The IFD  */
        /* should end when the last offset value given in the         */
        /* directory entries is written; MANY camera files which use  */
        /* this format write items, particularly thumbnails, at       */
        /* arbitrary offsets, and the TIFF spec itself allows offsets */
        /* to point anywhere in the file. The Exif spec declares that */
        /* "thumbnails" should be written within the values section   */
        /* of the IFD, but (oddly) not the "primary". Add subifds,    */
        /* disagreements between TIFF and TIFFEP about which IFD      */
        /* should describe the primary, multiple "thumbnails" of      */
        /* various sizes, etc. and it is very difficult to think of   */
        /* an IFD as a "container" (as it should be) or to decide     */
        /* where it ends.                                             */

        /* The thumbnails written above should be a part of the IFD,  */
        /* according to the Exif spec and common sense. All other     */
        /* offset values for the IFD have been written, so it is time */
        /* to declare that the IFD ends. If there is information      */
        /* giving the maximum size of this IFD, items outside the     */
        /* bounds have already been marked so.                        */

        if(Debug & END_OF_SECTION_DEBUG)
            printf("vo=%lu, mo=%lu, mvo=%lu, mio=%lu, nio=%lu, ol=%lu\n",value_offset,
                    max_offset,max_value_offset,max_ifd_offset,next_ifd_offset,offset_limit);

        /* ========= show end of ifd ===========                      */
        if((prefix && (*prefix == '>')) || (max_offset == 0) ||
                                                    (max_ifd_offset < max_offset))
        {
            display_end_of_ifd(max_ifd_offset,ifdtype,ifdnum,subifdnum,indent,
                                                            ifdname,prefix);
        }
        else
            display_end_of_ifd(max_offset,ifdtype,ifdnum,subifdnum,indent,
                                                            ifdname,prefix);

        /* =  ================= show ifd image ========  =========== */
        /* ###%%% not showing offset and length in LIST MODE */
        if(summary_entry &&
                    ((summary_entry->fileformat == TIFF_MAGIC) ||
                        (summary_entry->fileformat == ORF1_MAGIC) ||
                            (summary_entry->fileformat == ORF2_MAGIC)) &&
                            ((ifdtype == TIFF_IFD) || (ifdtype == TIFF_SUBIFD)) &&
                                (summary_entry->length > 0))
        {
            if(Debug & END_OF_SECTION_DEBUG)
                printf("mo=%lu, ol=%lu, seo=%lu\n",max_offset,offset_limit,summary_entry->offset);
            /* These are being displayed "inline" with the IFD that   */
            /* describes them; try to mark them as shown out of       */
            /* place.                                                 */
            if(max_offset && (summary_entry->offset > max_offset))
                tprefix = ">";
            else if(offset_limit && (summary_entry->offset > offset_limit))
                tprefix = "+";
            else
                tprefix = prefix;
            /* Scan jpeg images and check for mismarked compression   */
            /* types                                                  */
            switch(summary_entry->compression)
            {
            case 6:
                if(summary_entry->imagesubformat & IMGSUBFMT_RGB)
                {
                    /* Perhaps this should be done unconditionally... */
                    max_value_offset = summary_entry->offset + summary_entry->length;
                    if(summary_entry->filesubformat & FILESUBFMT_CR2)
                    {
                        /* Actually uncompressed RGB                  */
                        summary_entry->compression = 1;
                        summary_entry->imageformat = IMGFMT_JPEG | IMGFMT_MISMARKED;
                    }
                    print_tag_address(SECTION,summary_entry->offset,indent,tprefix);
                    if(PRINT_SECTION)
                    {
                        chpr += printf("<=-=-=> Start of RGB data for IFD %d,",
                                                            summary_entry->ifdnum);
                        if(summary_entry->datatype == TIFF_SUBIFD)
                            chpr += printf(" SubIFD %d,",summary_entry->subifdnum);
                        chpr += printf(" data length %lu",summary_entry->length);
                        chpr = newline(chpr);

                        chpr = newline(chpr);
                        if((PRINT_VALUE))
                            dumpsection(inptr,summary_entry->offset,summary_entry->length,
                                                                    indent + SMALLINDENT);
                        print_tag_address(SECTION,max_value_offset - 1,indent,"-");
                        chpr += printf("</=-=-=> End of RGB data");
                        if((max_value_offset - 1) > filesize)
                        {
                            PUSHCOLOR(RED);
                            chpr += printf(" (TRUNCATED at %lu)",filesize);
                            POPCOLOR();
                            summary_entry->imagesubformat |= IMGSUBFMT_TRUNCATED;
                        }
                        chpr = newline(chpr);
                    }
                    else if((PRINT_ENTRY))
                    {
                        /* ###%%% pseudo-tags fpr LIST Mode here */
                        print_tag_address(ENTRY,HERE,indent,"*");
                        if((PRINT_TAGINFO))
                        {
                            if((PRINT_LONGNAMES))
                                chpr += printf("%s.",listname);
                            chpr += printf("%-*.*s",TAGWIDTH,TAGWIDTH,"RGBImageData");
                        }
                        if((PRINT_VALUE))
                            chpr += printf(" = @%lu:%-9lu",summary_entry->offset,summary_entry->length);
                        chpr = newline(chpr);
                    }
                    /* done                                           */
                    break;

                }
                /* fall through */
            case 7:
                print_tag_address(SECTION,summary_entry->offset,indent,tprefix);
                if(PRINT_SECTION)
                {
                    chpr += printf("<=====> Start of JPEG data for IFD %d,",summary_entry->ifdnum);
                    if(summary_entry->datatype == TIFF_SUBIFD)
                        chpr += printf(" SubIFD %d,",summary_entry->subifdnum);
                    chpr += printf(" data length %lu",summary_entry->length);
                    chpr = newline(chpr);
                }
                else if((PRINT_ENTRY))
                {
                    /* ###%%% pseudo-tags fpr LIST Mode here */
                    print_tag_address(ENTRY,HERE,indent,"*");
                    if((PRINT_TAGINFO))
                    {
                        if((PRINT_LONGNAMES))
                            chpr += printf("%s.",listname);
                        chpr += printf("%-*.*s",TAGWIDTH,TAGWIDTH,"JpegImageData");
                    }
                    if((PRINT_VALUE))
                        chpr += printf(" = @%lu:%-9lu",summary_entry->offset,summary_entry->length);
                    chpr = newline(chpr);
                }

                /* If this IFD describes a JPEG image, the    */
                /* entry must go unlocked to the jpeg         */
                /* processor, which will lock it. This may    */
                /* cause problems if the IFD *also* contains  */
                /* JpegInterchangeFormat tags. It happens.    */
                if(Debug & SCAN_DEBUG)
                {
                    chpr = newline(chpr);
                    printred("DEBUG:");
                    chpr += printf(" UNLOCK **** ");
                    chpr += printf(" offset=%lu, length=%lu ",summary_entry->offset,
                                                            summary_entry->length);
                    chpr = newline(chpr);
                }
                summary_entry->entry_lock = 0;
                summary_entry->imageformat = IMGFMT_NOIMAGE;
                value_offset = 0;

                /* If compression == 7, PMI is CFA, and there are     */
                /* multiple tile offsets, each tile may be separately */
                /* compressed.                                        */
                marker = read_ushort(inptr,TIFF_MOTOROLA,summary_entry->offset);
                if((PRINT_SECTION) && (summary_entry->compression == 7) &&
                        (summary_entry->chunkhow == TIFFTAG_TileOffsets) &&
                                (summary_entry->noffsets > 1))
                {
                    unsigned long tmp_offset;
                    unsigned long offset_loc;
                    unsigned long length_loc;

                    /* Individually compressed tiles Show the first   */
                    /* and last, and just check for JPEG_SOI for      */
                    /* those in the middle. There should be an option */
                    /* to display them all and/or dump the section    */
                    /* (although '-O' can be used for that).          */
                    offset_loc = summary_entry->offset_loc;
                    length_loc = summary_entry->length_loc;
                    if(marker)
                    {
                        tmp_length = read_ulong(inptr,byteorder,length_loc);
                        value_offset = process_jpeg_segments(inptr,summary_entry->offset,marker,
                                            tmp_length,summary_entry,parent_name,
                                            tprefix,indent+MEDIUMINDENT);
                    }
                    for(i = 1; i < summary_entry->noffsets; ++i)
                    {
                        if(summary_entry->chunktype == LONG)
                        {
                            offset_loc += 4;
                            length_loc += 4;
                            tmp_offset = read_ulong(inptr,byteorder,offset_loc);
                            tmp_length = read_ulong(inptr,byteorder,length_loc);
                        }
                        else
                        {
                            offset_loc += 2;
                            length_loc += 2;
                            tmp_offset = read_ushort(inptr,byteorder,offset_loc);
                            tmp_length = read_ushort(inptr,byteorder,length_loc);
                        }
                        marker = read_ushort(inptr,TIFF_MOTOROLA,tmp_offset);
                        if(feof(inptr) || ferror(inptr))
                        {
                            value_offset = summary_entry->offset + summary_entry->length;
                            clearerr(inptr);
                            break;
                        }
                        if((i + 1) == summary_entry->noffsets)
                        {
                            /* Note that this one goes into the       */
                            /* summary                                */
                            chpr = newline(chpr);
                            value_offset = process_jpeg_segments(inptr,tmp_offset,marker,
                                                        tmp_length,summary_entry,parent_name,
                                                        tprefix,indent+MEDIUMINDENT);
                        }
                        else if(marker == JPEG_SOI)
                        {
                            /* This checks that there is at least an  */
                            /* SOI where it is supposed to be, but    */
                            /* avoids putting every tile in the       */
                            /* summary, which would be very boring.   */
                            if(i != 1)
                                chpr += printf(",");
                            if((i == 1) || ((i % 10) == 0))
                            {
                                chpr = newline(chpr);
                                print_tag_address(SECTION,HERE,indent + MEDIUMINDENT,"JPEG TILES");
                            }
                            chpr += printf("[%d]",i + 1);
                        }
                        else
                        {
                            ;
                        }   
                        if(feof(inptr) || ferror(inptr))
                        {
                            clearerr(inptr);
                            value_offset = summary_entry->offset + summary_entry->length;
                            break;
                        }
                    }
                    
                }
                else
                {
                    if(marker)
                        value_offset = process_jpeg_segments(inptr,summary_entry->offset,marker,
                                            summary_entry->length,summary_entry,parent_name,
                                            tprefix,indent+MEDIUMINDENT);
                }
                if(PRINT_SECTION)
                {
                    if((status = jpeg_status(0) == JPEG_EARLY_EOI))
                        chpr = newline(chpr);
                    jpeg_status(status);
                    print_tag_address(SECTION,summary_entry->offset + summary_entry->length - 1,
                                        indent,tprefix);
                    chpr += printf("</=====> End of JPEG data ====");
                    if(value_offset > (summary_entry->offset + summary_entry->length))
                        value_offset = summary_entry->offset + summary_entry->length;
                    print_jpeg_status();
                    if((value_offset - 1) > filesize)
                    {
                        PUSHCOLOR(RED);
                        chpr += printf(" (TRUNCATED at %lu)",filesize);
                        POPCOLOR();
                        summary_entry->imagesubformat |= IMGSUBFMT_TRUNCATED;
                    }
                    chpr = newline(chpr);
                }
                if(value_offset == 0L)
                    clearerr(inptr);
                else if(value_offset > max_value_offset)
                    max_value_offset = value_offset;
                if((max_value_offset - 1) > filesize)
                    summary_entry->imagesubformat |= IMGSUBFMT_TRUNCATED;
                break;
            case 0:     /* this shouldn't happen...                   */
            default:
                if((summary_entry) && (summary_entry->imageformat != IMGFMT_JPEG))
                {
                    max_value_offset = summary_entry->offset + summary_entry->length;
                    if(PRINT_SECTION)
                    {
                        print_tag_address(SECTION,summary_entry->offset,indent,tprefix);
                        chpr += printf("<=-=-=> Start of ");
                        print_imageformat(summary_entry);
                        print_imagesubformat(summary_entry);
                        print_imagecompression(summary_entry);
                        chpr += printf(" image data for IFD %d,",
                                                            summary_entry->ifdnum);
                        if(summary_entry->datatype == TIFF_SUBIFD)
                            chpr += printf(" SubIFD %d,",summary_entry->subifdnum);
                        chpr += printf(" data length %lu",summary_entry->length);
                        chpr = newline(chpr);

                        chpr = newline(chpr);
                        if((PRINT_VALUE))
                            dumpsection(inptr,summary_entry->offset,summary_entry->length,
                                                                    indent + SMALLINDENT);
                        print_tag_address(SECTION,max_value_offset - 1,indent,tprefix);
                        chpr += printf("</=-=-=> End of image data");
                        if((max_value_offset - 1) > filesize)
                        {
                            PUSHCOLOR(RED);
                            chpr += printf(" (TRUNCATED at %lu)",filesize);
                            POPCOLOR();
                            summary_entry->imagesubformat |= IMGSUBFMT_TRUNCATED;
                        }
                        chpr = newline(chpr);
                    }
                    else if((PRINT_ENTRY))
                    {
                        /* ###%%% pseudo-tags fpr LIST Mode here */
                        print_tag_address(ENTRY,HERE,indent,"*");
                        if((PRINT_TAGINFO))
                        {
                            if((PRINT_LONGNAMES))
                                chpr += printf("%s.",listname);
                            chpr += printf("%-*.*s",TAGWIDTH,TAGWIDTH,"ImageData");
                        }
                        if((PRINT_VALUE))
                            chpr += printf(" = @%lu:%-9lu",summary_entry->offset,summary_entry->length);
                        if((max_value_offset - 1) > filesize)
                        {
                            PUSHCOLOR(RED);
                            chpr += printf(" # TRUNCATED at %lu",filesize);
                            POPCOLOR();
                            summary_entry->imagesubformat |= IMGSUBFMT_TRUNCATED;
                        }
                        chpr = newline(chpr);
                    }
                    else if((max_value_offset -1) > filesize)
                            summary_entry->imagesubformat |= IMGSUBFMT_TRUNCATED;
                }
                break;
            }
            if((max_value_offset > max_ifd_offset) && 
                    ((offset_limit == 0) || (max_value_offset < offset_limit)))
                max_ifd_offset = max_value_offset;
        }
        /* =========== end show ifd image ============                */

        /* ###%%% temporary debug                                     */
        if(Debug & END_OF_SECTION_DEBUG)
            printf("mo=%lu, mvo=%lu, mio=%lu, nio=%lu, ol=%lu\n",
                    max_offset,max_value_offset,max_ifd_offset,next_ifd_offset,offset_limit);

        /* lock the summary entry for this ifd, even if it doesn't    */
        /* describe an actual image                                   */
        if(summary_entry && (summary_entry->entry_lock  <= 0))
            summary_entry->entry_lock = lock_number(summary_entry);

        /* Prepare for the next ifd                                   */
        if(ifdtype == TIFF_IFD)
        {
            ++ifdnum;
            subifdnum = -1;
        }
        else if(ifdtype == TIFF_SUBIFD)
            ++subifdnum;
        if(fulldirname)
            free(fulldirname);
        fulldirname = CNULL;

        /* If there is a next ifd, "max_offset" must be reset.        */
        if(next_ifd_offset)
            max_offset = 0;
    }
    newline(chpr);

    return(max_ifd_offset);
blewit:
    clearerr(inptr);
    if(PRINT_SECTION)
    {
        /* Indicate the end of the IFD                                    */
        indent -= SMALLINDENT;
        current_offset = ftell(inptr);
        if((prefix && (*prefix == '>')) || (max_offset == 0) ||
                                                    (max_ifd_offset < max_offset))
        {
            display_end_of_ifd(max_ifd_offset,ifdtype,ifdnum,subifdnum,indent,
                                                            ifdname,prefix);
        }
        else
            display_end_of_ifd(max_offset,ifdtype,ifdnum,subifdnum,indent,
                                                            ifdname,prefix);
    }
    if(fulldirname)
        free(fulldirname);
    return(0L);
}

/* Create a summary entry and link it into a list of image            */
/* descriptions to be printed at the end of the file output.          */

struct image_summary *
new_summary_entry(struct image_summary *prev_entry,int fileformat,int datatype)
{
    struct image_summary *new = NULL;
    int chpr = 0;

    /* Make entries only for types which describe image data, i.e.    */
    /* not EXIF, Interop, etc.                                        */
    switch(datatype)
    {
        case EXIF_IFD:
        case INTEROP_IFD:
        case GPS_IFD:
            break;
        default:
            if(Debug & SCAN_DEBUG)
            {
                chpr = newline(1);
                printred("DEBUG: NEW SUMMARY ");
            }
            new = (struct image_summary *)malloc(sizeof(struct image_summary));
            if(new)
            {
                if(Debug & SCAN_DEBUG)
                    printf("%#lx\n",(unsigned long)new);
                memset(new,0,sizeof(struct image_summary));
                new->datatype = datatype;
                new->ifdnum = -1;
                new->subifdnum = -1;
                new->length = 0;
                new->offset = 0;
                new->subfiletype = -1;
                memset(new->filesubformatAPPN,0,16);
                if(prev_entry)
                {
                    if(Debug & SCAN_DEBUG)
                    {
                        printred("DEBUG: PREV ENTRY ");
                        chpr += printf("%#x, next=%#x, offset=%ld, lock=%d",
                                        (unsigned int)prev_entry,
                                        (unsigned int)prev_entry->next_entry,
                                        prev_entry->offset,prev_entry->entry_lock);
                        chpr = newline(chpr);
                    }
                    /* make sure to attach at the end of the chain        */
                    while(prev_entry->next_entry)
                    {
                        prev_entry = prev_entry->next_entry;
                        if(Debug & SCAN_DEBUG)
                        {
                            printred(" PREV ENTRY ");
                            chpr += printf("%#x, next=%#x, offset=%ld, lock=%d",
                                            (unsigned int)prev_entry,
                                            (unsigned int)prev_entry->next_entry,
                                            prev_entry->offset,prev_entry->entry_lock);
                            chpr = newline(chpr);
                        }
                    }
                    new->prev_entry = prev_entry;
                    if(Debug & SCAN_DEBUG)
                    {
                        printred("DEBUG: FOUND PREV ENTRY ");
                        chpr += printf("%#x, next=%#x, offset=%ld, lock=%d",
                                            (unsigned int)prev_entry,
                                            (unsigned int)prev_entry->next_entry,
                                            prev_entry->offset,prev_entry->entry_lock);
                        chpr = newline(chpr);
                    }
                    if(prev_entry->fileformat)
                        new->fileformat = prev_entry->fileformat;
                    prev_entry->next_entry = new;
                    if(Debug & SCAN_DEBUG)
                    {
                        printred("DEBUG: NEW ENTRY ");
                        chpr += printf(" next=%#x, prev offset=%d, prev lock=%d",
                                            (unsigned int)prev_entry->next_entry,
                                            (unsigned int)prev_entry->offset,prev_entry->entry_lock);
                        chpr = newline(chpr);
                    }
                }
                else
                    new->fileformat = fileformat;
            }
            else
            {
                printred("Memory allocation for image summary failed");
                if(prev_entry)
                    chpr += printf(" summary will be incomplete");
                else
                    chpr += printf(" no summary will be printed");
                newline(chpr);
            }
            if(Debug & SCAN_DEBUG)
                chpr = newline(1);
            break;
    }
    return(new);
}

/* Move to the end of the summary entry chain passed as argument      */
struct image_summary *
last_summary_entry(struct image_summary *summary_entry)
{
    if(summary_entry)
    {
        while(summary_entry->next_entry)
            summary_entry = summary_entry->next_entry;
    }
    return(summary_entry);
}

/* Lock an entry with a sequence number, primarily so entries can be  */
/* tracked while debugging. A locked entry is "in use', requiring     */
/* creation of a new entry for a new image                            */

int
lock_number(struct image_summary *summary_entry)
{
    int number = 0;
    struct image_summary *prev_entry,*last_entry;

    if(summary_entry)
    {
        if(summary_entry->entry_lock)
            number = summary_entry->entry_lock;  /* don't change it */
        else if((last_entry = last_summary_entry(summary_entry)))
        {
            /* Find the highest existing lock number; go to the end   */
            /* and scan back to the head                              */
            if(last_entry->entry_lock > 0)
                number = last_entry->entry_lock + 1;
            prev_entry = last_entry->prev_entry;
            while(prev_entry)
            {
                if(prev_entry->entry_lock >= number)
                    number = prev_entry->entry_lock + 1;
                prev_entry = prev_entry->prev_entry;
            }
            if(number <= 0)
                number = 1;
        }
        else
            number = 1;
    }
    if(Debug & SCAN_DEBUG)
    {
        (void)newline(1);
        printred("DEBUG: LOCK SUMMARY");
        printf(" %#x ==> %d",(unsigned int)summary_entry,number);
        (void)newline(1);
        (void)newline(1);
    }
    return(number);
}
               

struct image_summary *
destroy_summary(struct image_summary *summary_entry)
{
    struct image_summary *prev_entry = NULL;

    /* start at the end and work back to the head                     */
    if(Debug & SCAN_DEBUG)
        printred("DEBUG: DESTROY SUMMARY ");
    if(summary_entry)
    {
        while(summary_entry->next_entry)
            summary_entry = summary_entry->next_entry;
        while(summary_entry)
        {
            prev_entry = summary_entry->prev_entry;
            free(summary_entry);
            summary_entry = prev_entry;
        }
    }
    return(prev_entry);
}


/* Read, decode, and print an EXIF IFD at 'exif_offset', noticing     */
/* MakerNotes and "Interoperability" IFDs as they go by. The offset   */
/* of each entry and offset value is recorded and printed, and the    */
/* offset of the last byte read is reported as the end of the IFD. If */
/* all goes well, the offset at which the caller should next read     */
/* (just past the end of the offset values) is returned. If an        */
/* unrecoverable error occurs, 0UL is returned.                       */

/* This looks remoarkably like process_tiff_ifd(), except that it     */
/* expects EXIF tags, and doesn't expect further IFDs to be chained   */
/* from it. If it does find a chained IFD, it processes it as a       */
/* normal IFD (not another EXIF IFD). It should probably just warn    */
/* and ignore it.                                                     */

unsigned long
process_exif_ifd(FILE *inptr,unsigned short byteorder,
                    unsigned long exif_offset,unsigned long fileoffset_base,
                    unsigned long max_offset,struct image_summary *summary_entry,
                    char *parent_name,int ifdnum,int indent)
{
    struct ifd_entry *entry_ptr;
    unsigned long max_value_offset = 0L;
    unsigned long next_ifd_offset,current_offset,max_exif_offset;
    unsigned long start_entry_offset,entry_offset,value_offset;
    unsigned long value_start_offset = 0UL;
    unsigned long next_max_offset = 0UL;
    int entry_num,num_entries,value_is_offset,use_second_pass;
    int chpr = 0;
    char *prefix;
    char *fulldirname = CNULL;

    if(inptr == (FILE *)0)
    {
        fprintf(stderr,"%s: no open file pointer to read EXIF IFD\n",
                Progname);
        return(0L);
    }

    PUSHCOLOR(EXIF_COLOR);
    /* If the exif segment appears to be beyond the end of the parent */
    /* ifd, mark the start address to call attention.                 */
    if(max_offset && (exif_offset > max_offset))
        prefix = "+";
    else
        prefix = "@";
    if(summary_entry)
        summary_entry->filesubformat |= FILESUBFMT_EXIF;
    max_exif_offset = max_offset;
    print_tag_address(SECTION,exif_offset + fileoffset_base,indent,prefix);
    if(PRINT_SECTION)
        chpr += printf("<EXIF IFD> (in IFD %d)",ifdnum);

    num_entries = read_ushort(inptr,byteorder,exif_offset + fileoffset_base);
    if(ferror(inptr) || feof(inptr))
        goto blewit;
    current_offset = entry_offset = start_entry_offset = ftell(inptr);
    if(PRINT_SECTION)
    {
        chpr += printf(" %d entries ",num_entries);
        chpr += printf("starting at file offset %#lx=%lu",
                                            current_offset,current_offset);
            chpr = newline(chpr);
    }
    use_second_pass = value_is_offset = 0;
    fulldirname = splice(parent_name,".","Exif");

    indent += SMALLINDENT;
    /* The direct entries                                             */
    for(entry_num = 0; entry_num < num_entries; ++entry_num)
    {
        entry_ptr = read_ifd_entry(inptr,byteorder,entry_offset);
        if((entry_ptr->value_type == 0) || (entry_ptr->value_type > DOUBLE) ||
            ferror(inptr) || feof(inptr))
        {
            int invalid_entry = 0;
            unsigned long limit_offset = 0L;

            print_tag_address(ENTRY,entry_offset,indent,prefix);
            chpr += printf(" INVALID EXIF ENTRY (%lu)",entry_ptr->value);
            chpr = newline(chpr);
            clearerr(inptr);
            current_offset = ftell(inptr);
            if(max_offset > 0)
                limit_offset = max_offset;
            else
            {
                if(fseek(inptr,0L,SEEK_END) != -1)
                {
                    limit_offset = ftell(inptr);
                    fseek(inptr,current_offset,SEEK_SET);
                }
            }
            /* If there's an error on input, or we can't check    */
            /* for absurd num_entries, give up.                   */
            if(!ferror(inptr) && !feof(inptr) && (limit_offset > 0))
            {
                /* If the number of entries would read past the   */
                /* size of the IFD, or past EOF, give up          */
                if((entry_offset + (12 * num_entries)) < limit_offset)
                {
                    /* Limit the number of consecutive failures.  */
                    /* An apparently valid entry resets the count */
                    /* to 0.                                      */
                    if(invalid_entry++ < MAX_INVALID_ENTRIES)
                    {
                        entry_offset = current_offset;
                        continue;
                    }
                }
            }
            chpr = newline(chpr);
            goto blewit;
        }
        current_offset = ftell(inptr);
        switch(entry_ptr->tag)
        {
            case EXIFTAG_PixelXDimension:
                if(summary_entry)
                    summary_entry->primary_width = entry_ptr->value;
                break;
            case EXIFTAG_PixelYDimension:
                if(summary_entry)
                    summary_entry->primary_height = entry_ptr->value;
                break;
            case EXIFTAG_Interoperability: 
            case EXIFTAG_MakerNote: 
                use_second_pass++;
                break;
            default:
                break;
        }
     
        print_tag_address(ENTRY,entry_offset,indent,prefix);
        value_offset = print_entry(inptr,byteorder,entry_ptr,fileoffset_base,
                                        summary_entry,fulldirname,EXIF_IFD,
                                        ifdnum,-1,SMALLINDENT);
        if(value_offset == 0UL)
            value_offset = current_offset;
        if(value_offset > max_value_offset)
            max_value_offset = value_offset;
        if((PRINT_VALUE_AT_OFFSET) && (is_offset(entry_ptr)))
            ++use_second_pass;
        entry_offset = current_offset;
    }

    next_ifd_offset = read_ulong(inptr,byteorder,current_offset);

    /* There should be no next_ifd_offset for the Exif Ifd.           */
    /* I am not confident, however, that someone won't eventually     */
    /* chain Exif ifds, so just handle them, and let the error        */
    /* messages fly.                                                  */
    if(next_ifd_offset > 0L)
    {
        if((PRINT_VALUE) && (PRINT_SECTION) && (PRINT_VALUE_AT_OFFSET))
        {
            print_tag_address(SECTION,current_offset,indent + SMALLINDENT,prefix);
            chpr += printf("**** next IFD offset %lu  ",next_ifd_offset);
            next_ifd_offset += fileoffset_base;
            if(next_ifd_offset < ftell(inptr))
            {
                printred("BAD NEXT IFD OFFSET");
                next_ifd_offset = 0L;
            }
            else
                chpr += printf("(+ %lu = %#lx/%lu)",fileoffset_base,
                                        next_ifd_offset,next_ifd_offset);
            chpr = newline(chpr);
        }
        else
            next_ifd_offset += fileoffset_base;
        /* This should never happen                                   */
        max_exif_offset = next_ifd_offset;
    }
    else
    {
        if((PRINT_VALUE) && (PRINT_SECTION) && (PRINT_VALUE_AT_OFFSET))
        {
            print_tag_address(SECTION,current_offset,indent,prefix);
            chpr += printf("**** next IFD offset 0");
            chpr = newline(chpr);
        }
    }

    if(ferror(inptr) || feof(inptr))
    {
        chpr += printf(" READ NEXT IFD OFFSET FAILED ");
        chpr = newline(chpr);
        why(stdout);
        clearerr(inptr);    /* keep going...                          */
    }
    else
        current_offset = ftell(inptr);

    value_offset = current_offset;

    /* Second pass, to evaluate entries which are stored indirectly.  */
    /* This occurs when the value requires more than 4 bytes, AND for */
    /* certain LONG values which are intended to be used as offsets   */
    /* (and should have their own type defined in the spec),          */
    if(use_second_pass)
    {
        if((PRINT_VALUE) && (PRINT_SECTION) && (PRINT_VALUE_AT_OFFSET))
        {
            print_tag_address(SECTION,value_offset,indent,prefix);
            chpr += printf("============= VALUES, EXIF IFD ============");
            chpr = newline(chpr);
        }

        entry_offset = start_entry_offset;
        for(entry_num = 0; entry_num < num_entries; ++entry_num)
        {
            entry_ptr = read_ifd_entry(inptr,byteorder,entry_offset);
            if((entry_ptr->value_type == 0) || (entry_ptr->value_type > DOUBLE) ||
                ferror(inptr) || feof(inptr))
            {
                /* If the first pass made it through invalid entries, */
                /* this pass should just ignore them and quietly      */
                /* continue.                                          */
                entry_offset = current_offset = ftell(inptr);
                continue;
            }
            current_offset = entry_offset = ftell(inptr);
            switch(entry_ptr->tag)
            {
                case EXIFTAG_Interoperability: 
                    PUSHCOLOR(INTEROP_COLOR);
                    value_offset = process_tiff_ifd(inptr,byteorder,
                                            entry_ptr->value,fileoffset_base,
                                            max_exif_offset,summary_entry,
                                            fulldirname,INTEROP_IFD,ifdnum,0,
                                            indent);
                    POPCOLOR();
                    break;
                case EXIFTAG_MakerNote: 
        /* ###%%% temporary debug                                     */
                    if(Debug & END_OF_SECTION_DEBUG)
                        printf("mo=%lu, mvo=%lu, ",
                                max_offset,max_value_offset);
                    value_start_offset = entry_ptr->value + fileoffset_base;
                    if(max_offset && (value_start_offset > max_offset) &&
                            (entry_ptr->count > 4))
                    {
                        next_max_offset = value_start_offset;
                    }
                    if(Debug & END_OF_SECTION_DEBUG)
                        printf("vso=%lu, nmo=%lu\n",value_start_offset,next_max_offset);
                    value_offset = process_makernote(inptr,byteorder,
                                            entry_ptr,fileoffset_base,max_exif_offset,
                                                    summary_entry,fulldirname,indent);
                    if(Debug & END_OF_SECTION_DEBUG)
                        printf("mo=%lu, mvo=%lu, ",max_offset,max_value_offset);
                    if(Debug & END_OF_SECTION_DEBUG)
                        printf("MNvo=%lu, mo=%lu\n",value_offset,max_offset);
                    break;
                default:
                    if((PRINT_VALUE) && ((PRINT_VALUE_AT_OFFSET) && is_offset(entry_ptr)))
                    {
                        print_tag_address(ENTRY,fileoffset_base + entry_ptr->value,
                                            indent,prefix);
                        print_tagid(entry_ptr,SMALLINDENT,EXIF_IFD);
                        value_offset =
                                    print_offset_value(inptr,byteorder,entry_ptr,
                                                    fileoffset_base,fulldirname,
                                                    EXIF_IFD,indent,1);
                    }
                    break;
            }
            if(value_offset > max_value_offset)
                max_value_offset = value_offset;
            if(max_offset && (value_offset > max_offset))
            {
                next_max_offset = value_start_offset;
                if(Debug & END_OF_SECTION_DEBUG)
                    printf("nmo=%lu\n",next_max_offset);
            }
        }
    }
    else if(current_offset > max_value_offset)
        max_value_offset = current_offset;

    indent -= SMALLINDENT;
    if(max_offset && (max_value_offset > max_offset))
        print_tag_address(SECTION,max_offset - 1,indent,"<");
    else
        print_tag_address(SECTION,max_value_offset - 1,indent,"-");
    if(PRINT_SECTION)
    {
        chpr += printf("</EXIF IFD>");
        chpr = newline(chpr);
    }
    POPCOLOR();

    /* This shouldn't happen.                                         */
    if(next_ifd_offset)
    {
        printred("#========= WARNING! CHAINING NEXT IFD FROM EXIF IFD =========");
        chpr = newline(1);
        value_offset = process_tiff_ifd(inptr,byteorder,next_ifd_offset,
                                fileoffset_base,max_offset,summary_entry,fulldirname,
                                EXIF_IFD,++ifdnum,-1,indent);
        if(value_offset > max_value_offset)
            max_value_offset = value_offset;
    }
    if(fulldirname)
        free(fulldirname);
    if(next_max_offset)
        max_value_offset = next_max_offset;
    if(Debug & END_OF_SECTION_DEBUG)
        printf("exmvo=%lu\n",max_value_offset);
    return(max_value_offset);
blewit:
    clearerr(inptr);
    current_offset = ftell(inptr);
    print_tag_address(SECTION,current_offset - 1,indent,"-");
    if(PRINT_SECTION)
    {
        chpr += printf("</EXIF IFD>");
        chpr = newline(chpr);
    }
    if(fulldirname)
        free(fulldirname);
    POPCOLOR();
    return(0L);
}

/* Read an ifd entry describing one or more subifd offsets, and       */
/* process the IFDs found at those offsets as TIFF image file         */
/* directories. This is used for TIFF6/TIFFEP SubIFDtags. The return  */
/* is 0L if something went wrong.                                     */

unsigned long
process_subifd(FILE *inptr,unsigned short byteorder,
                    struct ifd_entry *subfile_entry,
                    unsigned long fileoffset_base,unsigned long max_offset,
                    struct image_summary *summary_entry,char *parent_name,
                    int ifdnum,int subifdnum,int ifdtype,int indent)
{
    unsigned long entry_offset;
    unsigned long subifd_offset;
    unsigned long max_value_offset = 0L;
    int chpr = 0;
    int nsubifds,num;

    if(inptr && subfile_entry)
    {
        entry_offset = subfile_entry->value + fileoffset_base;
        nsubifds = subfile_entry->count;

        if(subifdnum >= 0)
        {
            if(nsubifds > 1)
            {
                if((PRINT_VALUE_AT_OFFSET))
                {
                    if(max_offset && (entry_offset > max_offset))
                        print_tag_address(SECTION,entry_offset,indent,"+");
                    else
                        print_tag_address(SECTION,entry_offset,indent,"@");
                    if(PRINT_SECTION)
                    {
                        chpr += printf("%s - %lu offsets: [max parent offset = %lu]",
                                tagname(subfile_entry->tag),subfile_entry->count,
                                max_offset);
                    }
                    for(num = 0; num < nsubifds; ++num)
                    {
                        subifd_offset = read_ulong(inptr,byteorder,entry_offset)
                                                            + fileoffset_base;
                        entry_offset = ftell(inptr); /* where to get next one */
                        if((PRINT_SECTION))
                            chpr += printf(" %lu",subifd_offset);
                        if(ferror(inptr) || feof(inptr))
                            break;
                        /* goto blewit;                               */
                    }
                    if(PRINT_SECTION)
                        chpr = newline(chpr);
                }

                entry_offset = subfile_entry->value + fileoffset_base;
                for(num = 0; num < nsubifds; ++num)
                {
                    subifd_offset = read_ulong(inptr,byteorder,entry_offset) +
                                                        fileoffset_base;
                    /* where to get next one                          */
                    entry_offset = ftell(inptr);
                    /* The next subifd offset suggests a maximum for  */
                    /* this subifd; at least until some nitwit writes */
                    /* the second one first. Doesn't help for the     */
                    /* last one, though.                              */
                    if(num < (nsubifds - 1))
                        max_offset = read_ulong(inptr,byteorder,HERE);
                    else
                        max_offset = 0;
                    if(ferror(inptr) || feof(inptr))
                        goto blewit;
                    max_value_offset = process_tiff_ifd(inptr,byteorder,
                                            subifd_offset,fileoffset_base,
                                            max_offset,summary_entry,parent_name,
                                            ifdtype,ifdnum,subifdnum + num,
                                            indent);
                    /* ###%%% don't we check for errors here???           */
                }
            }
            else
            {
                max_value_offset = process_tiff_ifd(inptr,byteorder,
                                            subfile_entry->value,fileoffset_base,
                                            max_offset,summary_entry,parent_name,
                                            ifdtype,ifdnum,subifdnum,
                                            indent);
            }
        }
        else
        {
            printred("Negative subifd number");
            chpr += printf(" %d, ifdtype %d not processed for IFD %d",subifdnum,ifdtype,ifdnum);
            chpr = newline(chpr);
            goto blewit;
        }
    }
    else
    {
        fprintf(stderr,"%s: no open file pointer to read EXIF\n",Progname);
        max_value_offset = 0L;
    }
    setcharsprinted(chpr);
    return(max_value_offset);
blewit:
    /* we're going to keep trying, because the caller's next offset   */
    /* may be ok.                                                     */
    clearerr(inptr);
    return(0L);
}

/* Just a bare bones GPS routine; no interpretation is done.          */

unsigned long
process_gps_ifd(FILE *inptr,unsigned short byteorder,
                    unsigned long gps_offset,unsigned long fileoffset_base,
                    unsigned long max_offset,struct image_summary *summary_entry,
                    char *parent_name,int ifdnum,int indent)
{
    struct ifd_entry *entry_ptr;
    unsigned long max_value_offset = 0L;
    unsigned long next_ifd_offset,current_offset,max_gps_offset;
    unsigned long start_entry_offset,entry_offset,value_offset;
    int entry_num,num_entries,value_is_offset,use_second_pass;
    int chpr = 0;
    char *fulldirname = CNULL;

    if(inptr == (FILE *)0)
    {
        fprintf(stderr,"%s: no open file pointer to read GPS IFD\n",
                Progname);
        return(0L);
    }

    PUSHCOLOR(GPS_COLOR);
    /* If the gps segment appears to be beyond the start of the       */
    /* parent ifd, mark the start address funny.                      */
    if(max_offset && (gps_offset > max_offset))
        print_tag_address(SECTION,gps_offset + fileoffset_base,indent,"^");
    else
        print_tag_address(SECTION,gps_offset + fileoffset_base,indent,"@");
    if(PRINT_SECTION)
        chpr += printf("<GPS IFD> (in IFD %d)",ifdnum);

    num_entries = read_ushort(inptr,byteorder,gps_offset + fileoffset_base);
    if(ferror(inptr) || feof(inptr))
        goto blewit;
    current_offset = entry_offset = start_entry_offset = ftell(inptr);
    if(PRINT_SECTION)
    {
        chpr += printf(" %d entries ",num_entries);
        chpr += printf("starting at file offset %#lx=%lu",
                                        current_offset,current_offset);
        chpr = newline(chpr);
    }
    use_second_pass = value_is_offset = 0;
    fulldirname = splice(parent_name,".","Gps");

    indent += SMALLINDENT;
    /* The direct entries                                             */
    for(entry_num = 0; entry_num < num_entries; ++entry_num)
    {
        print_tag_address(ENTRY,entry_offset,indent,"@");
        entry_ptr = read_ifd_entry(inptr,byteorder,entry_offset);
        if((entry_ptr->value_type == 0) || (entry_ptr->value_type > DOUBLE) ||
            ferror(inptr) || feof(inptr))
        {
            print_tag_address(ENTRY,entry_offset,indent,"@");
            chpr += printf(" INVALID (%lu)",entry_ptr->value);
            chpr = newline(chpr);
            /* ###%%% replace with check code from process_tiff_ifd() */
            goto blewit;
        }
        entry_offset = current_offset = ftell(inptr);
        value_offset = print_entry(inptr,byteorder,entry_ptr,fileoffset_base,
                                            summary_entry,fulldirname,GPS_IFD,
                                            ifdnum,-1,SMALLINDENT);
        if(value_offset == 0UL)
            value_offset = entry_offset;
        if(value_offset > max_value_offset)
            max_value_offset = value_offset;
        if((PRINT_VALUE_AT_OFFSET) && (is_offset(entry_ptr)))
            ++use_second_pass;
    }

    next_ifd_offset = read_ulong(inptr,byteorder,current_offset);

    /* offsets found in the next pass should be within the bounds of  */
    /* the ifd. The following helps to detect chunks that are written */
    /* "out of place".                                                */
    if(next_ifd_offset > 0L)
    {
        if((PRINT_VALUE) && (PRINT_SECTION) && (PRINT_VALUE_AT_OFFSET))
        {
            print_tag_address(SECTION,current_offset,indent,"@");
            chpr += printf("**** next IFD offset %lu  ",next_ifd_offset);
            next_ifd_offset += fileoffset_base;
            if(next_ifd_offset < ftell(inptr))
            {
                printred("BAD NEXT IFD OFFSET");
                next_ifd_offset = 0L;
            }
            else
                chpr += printf("(+ %lu = %#lx/%lu)",fileoffset_base,
                            next_ifd_offset,next_ifd_offset);
            chpr = newline(chpr);
        }
        else
            next_ifd_offset += fileoffset_base;
        /* This should never happen                                   */
        max_gps_offset = next_ifd_offset;
        if(max_offset && (max_gps_offset > max_offset))
            max_gps_offset = max_offset;
    }
    else
    {
        max_gps_offset = max_offset;    
        if((PRINT_VALUE) && (PRINT_SECTION) && (PRINT_VALUE_AT_OFFSET))
        {
            print_tag_address(SECTION,current_offset,indent,"@");
            chpr += printf("**** next IFD offset 0");
            chpr = newline(chpr);
        }
    }

    if(ferror(inptr) || feof(inptr))
    {
        chpr += printf(" READ NEXT IFD OFFSET FAILED ");
        chpr = newline(chpr);
        why(stdout);
        clearerr(inptr);    /* keep going...                          */
    }
    else
        current_offset = ftell(inptr);

    value_offset = current_offset;

    /* Make a second pass to print offset values.                     */
    if(use_second_pass)
    {
        if((PRINT_VALUE) && (PRINT_SECTION) && (PRINT_VALUE_AT_OFFSET))
        {
            print_tag_address(SECTION,value_offset,indent,"@");
            chpr += printf("============= VALUES, GPS IFD ============");
            chpr = newline(chpr);
        }

        /* Second pass, to evaluate entries which are stored          */
        /* indirectly (the value requires more than 4 bytes).         */
        entry_offset = start_entry_offset;
        for(entry_num = 0; entry_num < num_entries; ++entry_num)
        {
            entry_ptr = read_ifd_entry(inptr,byteorder,entry_offset);
            if((entry_ptr->value_type == 0) || (entry_ptr->value_type > DOUBLE) ||
                ferror(inptr) || feof(inptr))
            {
                print_tag_address(ENTRY,entry_offset,indent,"@");
                chpr += printf(" INVALID (%lu)",entry_ptr->value);
                chpr = newline(chpr);
                goto blewit;
            }
            current_offset = entry_offset = ftell(inptr);
            switch(entry_ptr->tag)
            {
                default:
                    if((PRINT_VALUE) && ((PRINT_VALUE_AT_OFFSET) && is_offset(entry_ptr)))
                    {
                        print_tag_address(ENTRY,fileoffset_base + entry_ptr->value,
                                            indent,"@");
                        print_tagid(entry_ptr,SMALLINDENT,GPS_IFD);
                        value_offset =
                                    print_offset_value(inptr,byteorder,entry_ptr,
                                                    fileoffset_base,fulldirname,
                                                    GPS_IFD,indent,1);
                    }
                    break;
            }
            if(value_offset > max_value_offset)
                max_value_offset = value_offset;
        }
    }
    else if(current_offset > max_value_offset)
        max_value_offset = current_offset;

    indent -= SMALLINDENT;
    if(max_offset && (gps_offset > max_offset))
    {
        print_tag_address(SECTION,max_value_offset - 1,indent,">");
        max_value_offset = 0L;  /* let the caller know.               */
    }
    else
        print_tag_address(SECTION,max_value_offset - 1,indent,"-");
    if(PRINT_SECTION)
    {
        chpr += printf("</GPS IFD>");
        chpr = newline(chpr);
    }
    POPCOLOR();
    if(next_ifd_offset)
    {
        printred("#========= WARNING! CHAINING NEXT IFD FROM GPS IFD =========");
        chpr = newline(1);
        value_offset = process_tiff_ifd(inptr,byteorder,next_ifd_offset,
                                fileoffset_base,max_offset,NULL,fulldirname,
                                GPS_IFD,-1,-1,indent);
        if(value_offset > max_value_offset)
            max_value_offset = value_offset;
    }
    if(fulldirname)
        free(fulldirname);
    return(max_value_offset);
blewit:
    clearerr(inptr);
    current_offset = ftell(inptr);
    print_tag_address(SECTION,current_offset - 1,indent,"-");
    if(PRINT_SECTION)
    {
        chpr += printf("</GPS IFD>");
        chpr = newline(chpr);
    }
    if(fulldirname)
        free(fulldirname);
    POPCOLOR();
    return(0L);
}

/* Process JPEG the marker segments between JPEG SOI and EOI.         */
/* For the moment, that means just printing them (offset, id, length) */
/* and then moving to the next tag.                                   */

/* The first marker has been read, it's identity and offset are       */
/* passed in as arguments. No assumptions are made about the current  */
/* file pointer; the first read always begins at the passed           */
/* marker_offset.                                                     */

/* APP section are noticed, and APP0, APP1, and APP12 section         */
/* processed appropriately. Other APP section are reported without    */
/* expansion of the section, but may be hex/ascii dumped by option.   */

extern unsigned long forward_scan_for_eoi(FILE *,unsigned long,
                            unsigned long,char *,int);

unsigned long
process_jpeg_segments(FILE *inptr,unsigned long marker_offset,unsigned short tag,
                        unsigned long data_length,struct image_summary *summary_entry,
                        char *parent_name,char *prefix,int indent)
{
    static unsigned long img_pixels = 0L;
    unsigned long max_offset = 0L;
    unsigned long dumplength = 0L;
    unsigned long start_of_jpeg_data;
    unsigned long found_eoi_offset,after_sos,end_of_section,eof;
    unsigned short seg_length,tmp;
    unsigned short img_height = 0;
    unsigned short img_width = 0;
    unsigned short bad_soi = 0;
    int chpr = 0;
    int had_soi = 0;
    int status = 0;
    int i;
    char *name;
    static unsigned long max_eoi = 0UL;

    if(inptr)
    {
        start_of_jpeg_data = marker_offset;
        if(start_of_jpeg_data == 0UL)
            max_eoi = 0UL;
        if((summary_entry == NULL) ||
                summary_entry->entry_lock ||
                        (summary_entry->imageformat != IMGFMT_NOIMAGE))
        {
            summary_entry = new_summary_entry(summary_entry,0,IMGFMT_JPEG);
        }
        if(summary_entry)
        {
            if(summary_entry->length <= 0)
                summary_entry->length = data_length;
            if(summary_entry->offset <= 0)
                summary_entry->offset = start_of_jpeg_data;
            summary_entry->imageformat = IMGFMT_JPEG;
            summary_entry->entry_lock = lock_number(summary_entry);
            if(tag == 0)
            {
                clearerr(inptr);
                (void)jpeg_status(JPEG_NO_SOI);
                if(summary_entry)
                    summary_entry->imagesubformat |= IMGSUBFMT_NO_JPEG;
            }
        }
        PUSHCOLOR(JPEG_COLOR);
        while(tag != 0)
        {
            switch(tag)
            {
                case JPEG_SOS:  /* image data                         */
                    name = tagname(tag);
                    if(PRINT_SEGMENT)
                    {
                        print_tag_address(SEGMENT,marker_offset,indent + SMALLINDENT,prefix);
                        chpr += printf("<%s>",name);
                    }
                    seg_length = read_ushort(inptr,TIFF_MOTOROLA,marker_offset + 2);
                    if(!ferror(inptr) && !feof(inptr))
                    {
                        unsigned char ns,c,t,td,ta,ss,se,a,ah,al;
                        int i;

                        /* number of components in scan               */
                        ns = read_ubyte(inptr,HERE);
                        if(PRINT_SEGMENT)
                        {
                            chpr += printf(" length %u",seg_length);
                            chpr += printf("  start of JPEG data, %d components %lu pixels",
                                                                      ns & 0xff,img_pixels);
                            chpr = newline(chpr);
                        }
                        if(Debug & JPEG_MARKER_DEBUG)
                        {
                            for(i = 0; i < ns; ++i)
                            {
                                /* ###%%% */
                                putindent(6 + (2 * ADDRWIDTH) + indent + 16);
                                /* component selector                 */
                                c = read_ubyte(inptr,HERE); 
                                t = read_ubyte(inptr,HERE);
                                td = t & 0xf;
                                ta = ((t & 0xf0) >> 4) & 0xf;
                                if((ferror(inptr) == 0) && (feof(inptr) == 0) && (PRINT_SEGMENT))
                                {
                                    chpr += printf("Cs%d=%d, Td%d=%#04x, Ta%d=%#04x",i,c,i,td,i,ta);
                                    chpr = newline(chpr);
                                }
                            }
                            ss = read_ubyte(inptr,HERE); 
                            se = read_ubyte(inptr,HERE); 
                            a = read_ubyte(inptr,HERE);
                            ah = a & 0xf;
                            al = ((a & 0xf0) >> 4) & 0xf;
                            putindent(6 + (2 * ADDRWIDTH) + indent + 16);
                            chpr += printf("Ss=%u, Se=%u, Ah=%u, Al=%u",ss,se,ah,al);
                            chpr = newline(chpr);
                        }
                    }
                    else
                    {
                        tag = 0;
                        chpr += printf("ERROR reading length of SOS");
                        chpr = newline(chpr);
                        why(stdout);
                        (void)jpeg_status(JPEG_HAD_ERROR);
                        if(summary_entry)
                            summary_entry->imagesubformat |= IMGSUBFMT_JPEGFAILED;
                        continue;
                    }
                    if(data_length > 0L)
                    {   

                        after_sos = marker_offset + 2 + seg_length;
                        marker_offset = start_of_jpeg_data + data_length - 2L;
                        tag = read_ushort(inptr,TIFF_MOTOROLA,marker_offset);
                        if(ferror(inptr) || feof(inptr))
                        {
                            tag = 0;
                            printred("# WARNING: ERROR reading JPEG_EOI at end of section");
                            chpr = newline(chpr);
                            clearerr(inptr);
                            /* Skip the backward scan and check       */
                            /* forward from the end of the SOS        */
                            /* section, to see where the file is      */
                            /* truncated.                             */
                            status = JPEG_HAD_ERROR;
                            (void)jpeg_status(status);
                            if(summary_entry)
                                summary_entry->imagesubformat |= IMGSUBFMT_JPEGFAILED;
#if 0
/* Wrong if multiple compressed tiles/strips in passed length; status */
/* set to HAD_ERROR allows skippping backward scan and going direct   */
/* to forward scan. Should pass just tile/strip length and handle     */
/* multiples in parent.                                               */
                            continue;
#endif
                        }
                        if(tag == JPEG_EOI)
                        {
                            max_offset = ftell(inptr);
                            continue;
                        }
                        else    /* Try a little harder to find EOI    */
                        {
                            int i;

                            end_of_section = start_of_jpeg_data + data_length;
                            if(status == 0)
                            {
                                /* Try a quick scan backward from the end */
                                /* of the section, in case there's just a */
                                /* little padding.                        */
                                for(i = 1; i < 512; ++i)
                                {
                                    tag = read_ushort(inptr,TIFF_MOTOROLA,marker_offset);
                                    if(Debug & JPEG_EOI_DEBUG)
                                        printf("DEBUG: %#lx/%ld: %#04x\n",marker_offset,marker_offset,tag);
                                    if(ferror(inptr) || feof(inptr))
                                        break;
                                    status = 0;
                                    if(tag == JPEG_EOI)
                                        break;
                                    --marker_offset;
                                }
                                if(ferror(inptr) || feof(inptr))
                                {
                                    tag = 0;
                                    printred("# WARNING: ERROR reading JPEG data looking for JPEG_EOI");
                                    chpr = newline(chpr);
                                    clearerr(inptr);
                                    (void)jpeg_status(JPEG_HAD_ERROR);
                                    if(summary_entry)
                                        summary_entry->imagesubformat |= IMGSUBFMT_JPEGFAILED;
                                    continue;
                                }
                                if(tag == JPEG_EOI)
                                {
                                    if(start_of_jpeg_data > 0UL)
                                    {
                                        if(feof(inptr) || ((end_of_section + 5) >= get_filesize(inptr)))
                                            max_eoi = start_of_jpeg_data;
                                    }
                                    max_offset = ftell(inptr);
                                    (void)jpeg_status(JPEG_EARLY_EOI);
                                    if(summary_entry)
                                        summary_entry->imagesubformat |= IMGSUBFMT_JPEG_EARLY_EOI;
                                    if(Debug & JPEG_EOI_DEBUG)
                                        printf("DEBUG: start_of_jpeg_data=%lu, max_eoi=%lu\n",start_of_jpeg_data,max_eoi);
                                    continue;
                                }
                            }
                            /* If the quick scan didn't work, try a   */
                            /* forward scan from the offset given by  */
                            /* the start of scan header.              */
                            found_eoi_offset = forward_scan_for_eoi(inptr,after_sos,
                                                            end_of_section,"!",indent);
                            if(start_of_jpeg_data > 0UL)
                            {
                                if(feof(inptr) || ((found_eoi_offset + 5) >= get_filesize(inptr)))
                                    max_eoi = start_of_jpeg_data;
                            }
                            if(Debug & JPEG_EOI_DEBUG)
                                printf("DEBUG: start_of_jpeg_data=%lu, max_eoi=%lu\n",start_of_jpeg_data,max_eoi);
                            if(found_eoi_offset)
                            {
                                max_offset = found_eoi_offset + 2;
                                (void)jpeg_status(JPEG_EARLY_EOI);
                                if(summary_entry)
                                    summary_entry->imagesubformat |= IMGSUBFMT_JPEG_EARLY_EOI;
                                marker_offset = max_offset - 2UL;
                                tag = JPEG_EOI;
                                continue;
                            }
                            else
                            {
                                if(ferror(inptr) || feof(inptr))
                                {
                                    tag = 0;
                                    clearerr(inptr);
                                    (void)jpeg_status(status);
                                    if(summary_entry)
                                        summary_entry->imagesubformat |= IMGSUBFMT_JPEGFAILED;
                                    continue;
                                }
                                else
                                {
                                    max_offset = 0;
                                    (void)fseek(inptr,start_of_jpeg_data + data_length - 2L,SEEK_SET);
                                    (void)jpeg_status(JPEG_NO_EOI);
                                }
                            }
                        }
                    }
                    else
                    {
                        /* See if there's a JPEG_EOI at (or somewhere */
                        /* near) EOF. If a previous subimage was      */
                        /* found at or very near eof, indicating a    */
                        /* thumbnail tacked on to the end of the      */
                        /* file, use the start of that image as the   */
                        /* limit for the search,                      */
                        if(Debug & JPEG_EOI_DEBUG)
                            printf("DEBUG: max_eoi=%lu\n",max_eoi);
                        if(fseek(inptr,0L,SEEK_END) == 0)
                        {
                            /* Values to use if a forward scan is     */
                            /* required.                              */
                            after_sos = marker_offset + 2 + seg_length;
                            eof = ftell(inptr);
                            if(max_eoi)
                            {
                                end_of_section = max_eoi - 1;
                                marker_offset = max_eoi - 2;
                            }
                            else
                            {
                                end_of_section = eof - 1;
                                marker_offset = eof - 2;
                            }
                            tag = read_ushort(inptr,TIFF_MOTOROLA,marker_offset);
                            if(ferror(inptr) || feof(inptr))
                            {
                                tag = 0;
                                printred("# WARNING: ERROR reading JPEG data looking for JPEG_EOI");
                                chpr = newline(chpr);
                                clearerr(inptr);
                                (void)jpeg_status(JPEG_HAD_ERROR);
                                if(summary_entry)
                                    summary_entry->imagesubformat |= IMGSUBFMT_JPEGFAILED;
                                continue;
                            }
                            if(Debug & JPEG_EOI_DEBUG)
                                printf("DEBUG: %#lx/%ld: %#04x\n",marker_offset,marker_offset,tag);
                            if(tag != JPEG_EOI) /* try harder         */
                            {
                                for(i = 1; i < 512; ++i)
                                {
                                    --marker_offset;
                                    tag = read_ushort(inptr,TIFF_MOTOROLA,marker_offset);
                                    if(ferror(inptr) || feof(inptr))
                                        break;
                                    if(Debug & JPEG_EOI_DEBUG)
                                        printf("DEBUG: %#lx/%ld: %#04x\n",marker_offset,marker_offset,tag);
                                    if(tag == JPEG_EOI)
                                        break;
                                }
                            }
                            if(ferror(inptr) || feof(inptr))
                            {
                                tag = 0;
                                printred("# WARNING: ERROR reading JPEG data looking for JPEG_EOI");
                                chpr = newline(chpr);
                                clearerr(inptr);
                                (void)jpeg_status(JPEG_HAD_ERROR);
                                if(summary_entry)
                                    summary_entry->imagesubformat |= IMGSUBFMT_JPEGFAILED;
                                continue;
                            }
                            if(tag == JPEG_EOI)
                            {
                                max_offset = ftell(inptr);
                                if(Debug & JPEG_EOI_DEBUG)
                                    printf("DEBUG: max_offset=%lu, eof=%lu, marker_offset=%lu\n",max_offset,eof,marker_offset);
                                if(marker_offset < (eof - 2))
                                {
                                    (void)jpeg_status(JPEG_EARLY_EOI);
                                    if(summary_entry)
                                    {
                                        summary_entry->imagesubformat |= IMGSUBFMT_JPEG_EARLY_EOI;
                                        if(summary_entry->length <= 0)
                                            summary_entry->length = eof - start_of_jpeg_data;
                                    }
                                }
                                continue;
                            }
                            else
                            {
                                found_eoi_offset = forward_scan_for_eoi(inptr,after_sos,end_of_section,
                                                                                            "!",indent);
                                if(found_eoi_offset)
                                {
                                    max_offset = found_eoi_offset + 2;
                                    marker_offset = found_eoi_offset;
                                    if(Debug & JPEG_EOI_DEBUG)
                                    {
                                        printf("DEBUG: max_offset=%lu, eof=%lu, marker_offset=%lu\n",max_offset,eof,marker_offset);
                                        printf("found_eof_offset=%lu, ftell=%lu\n",found_eoi_offset,ftell(inptr));
                                    }
                                    if(found_eoi_offset < (eof - 2))
                                    {
                                        (void)jpeg_status(JPEG_EARLY_EOI);
                                        if(summary_entry)
                                            summary_entry->imagesubformat |= IMGSUBFMT_JPEG_EARLY_EOI;
                                    }
                                    if((summary_entry) && (summary_entry->length <= 0))
                                        summary_entry->length = eof - start_of_jpeg_data;
                                    tag = JPEG_EOI;
                                    continue;
                                }
                                else
                                {
                                    if((summary_entry) && (summary_entry->length <= 0))
                                        summary_entry->length = eof - start_of_jpeg_data;
                                    print_tag_address(SEGMENT,eof - 1,indent + SMALLINDENT,prefix);
                                    printred(" # WARNING: at EOF: JPEG_EOI not found");
                                    if(!(LIST_MODE))
                                        chpr = newline(chpr);
                                    max_offset = 0;
                                    /* ###%%% this may not be needed, */
                                    /* since it's already been        */
                                    /* reported                       */
                                    (void)jpeg_status(JPEG_NO_EOI);
                                }
                            }
                        }
                        if(ferror(inptr) || feof(inptr))
                        {
                            (void)jpeg_status(JPEG_HAD_ERROR);
                            if(summary_entry)
                                summary_entry->imagesubformat |= IMGSUBFMT_JPEGFAILED;
                        }
                    }
                    tag = 0;    /* break the loop                     */
                    break;
                case JPEG_DHT:  /* image data                         */
                    name = tagname(tag);
                    if(PRINT_SEGMENT)
                    {
                        print_tag_address(SEGMENT,marker_offset,indent + SMALLINDENT,prefix);
                        chpr += printf("<%s>",name);
                    }
                    seg_length = read_ushort(inptr,TIFF_MOTOROLA,marker_offset + 2);
                    if(!ferror(inptr) && !feof(inptr))
                    {
                        unsigned char t,tc,th,l;
                        int i;

                        t = read_ubyte(inptr,HERE);
                        tc = t & 0xf;
                        th = ((t & 0xf0) >> 4) & 0xf;
                        if(PRINT_SEGMENT)
                        {
                            chpr += printf(" length %u",seg_length);
                            chpr += printf(" table class = %u",tc & 0xff);
                            chpr += printf(" table id = %u",th & 0xff);
                            chpr = newline(chpr);
                        }
                        if(Debug & JPEG_MARKER_DEBUG)
                        {
                            for(i = 0; i < 16; ++i)
                            {
                                if((i % 5) == 0)
                                {
                                    chpr = newline(chpr);
                                    putindent(6 + (2 * ADDRWIDTH) + indent + 16);
                                }
                                /* component selector                 */
                                l = read_ubyte(inptr,HERE); 
                                if((ferror(inptr) == 0) && (feof(inptr) == 0) && (PRINT_SEGMENT))
                                {
                                    chpr += printf("l%-2d=%u",i,l);
                                    if(i < 15)
                                        chpr += printf(", ");
                                }
                            }
                            chpr = newline(chpr);
                        }

                        /* next tag                                       */
                        marker_offset += seg_length + 2;
                        if(marker_offset > 0L)
                            tag = read_ushort(inptr,TIFF_MOTOROLA,marker_offset);
                        else
                            tag = 0;
                        if(ferror(inptr) || feof(inptr))
                        {
                            tag = 0;
                            clearerr(inptr);
                            (void)jpeg_status(JPEG_HAD_ERROR);
                            if(summary_entry)
                                summary_entry->imagesubformat |= IMGSUBFMT_JPEGFAILED;
                            continue;
                        }
                    }
                    else
                    {
                        chpr += printf("ERROR reading length of DHT");
                        chpr = newline(chpr);
                        why(stdout);
                        (void)jpeg_status(JPEG_HAD_ERROR);
                        if(summary_entry)
                            summary_entry->imagesubformat |= IMGSUBFMT_JPEGFAILED;
                        return 0;
                    }
                    break;
                case JPEG_COM:
                    name = tagname(tag);
                    if(PRINT_SEGMENT)
                    {
                        print_tag_address(SEGMENT,marker_offset,indent + SMALLINDENT,prefix);
                        chpr += printf("<%s>",name);
                    }
                    seg_length = read_ushort(inptr,TIFF_MOTOROLA,marker_offset + 2);

                    if(!ferror(inptr) && !feof(inptr))
                    {
                        if(PRINT_SEGMENT)
                        {
                            chpr += printf(" length %u: \'",seg_length);
                            (void)print_ascii(inptr,seg_length - 2,HERE);
                            chpr += printf("\'");
                            chpr = newline(chpr);
                        }
                        marker_offset += seg_length + 2;
                        /* next tag                                   */
                        tag = read_ushort(inptr,TIFF_MOTOROLA,marker_offset);
                        if(ferror(inptr) || feof(inptr))
                        {
                            tag = 0;
                            clearerr(inptr);
                            (void)jpeg_status(JPEG_HAD_ERROR);
                            if(summary_entry)
                                summary_entry->imagesubformat |= IMGSUBFMT_JPEGFAILED;
                        }
                        else
                            max_offset = ftell(inptr);
                    }
                    else
                    {
                        tag = 0;
                        clearerr(inptr);
                        (void)jpeg_status(JPEG_HAD_ERROR);
                        if(summary_entry)
                            summary_entry->imagesubformat |= IMGSUBFMT_JPEGFAILED;
                    }
                    break;
                case JPEG_SOF_0:
                case JPEG_SOF_1:
                case JPEG_SOF_2:
                case JPEG_SOF_3:
                case JPEG_SOF_5:
                case JPEG_SOF_6:
                case JPEG_SOF_7:
                case JPEG_SOF_9:
                case JPEG_SOF_10:
                case JPEG_SOF_11:
                case JPEG_SOF_13:
                case JPEG_SOF_14:
                case JPEG_SOF_15:
                    name = tagname(tag);
                    /* If this is a TIFF format file, record the      */
                    /* actual compression type as indicated by the    */
                    /* SOF tag, for printing in the image summary at  */
                    /* the end.                                       */
                    if(summary_entry)
                        summary_entry->compression = tag;
                    if(PRINT_SEGMENT)
                    {
                        print_tag_address(SEGMENT,marker_offset,indent + SMALLINDENT,prefix);
                        chpr += printf("<%s>",name);
                    }
                    seg_length = read_ushort(inptr,TIFF_MOTOROLA,marker_offset + 2);
                    if(!ferror(inptr) && !feof(inptr))
                    {
                        unsigned char nf = 0;
                        unsigned char c,h,v,t,p;
                        int i;
                        int iserror = 0;

                        if(PRINT_SEGMENT)
                            chpr += printf(" length %u",seg_length);
                        p = read_ubyte(inptr,HERE); /* precision    */
                        if(!iserror && !ferror(inptr) && !feof(inptr))
                            img_height = read_ushort(inptr,TIFF_MOTOROLA,HERE);
                        if(!iserror && !ferror(inptr) && !feof(inptr))
                            img_width = read_ushort(inptr,TIFF_MOTOROLA,HERE);
                        if(!iserror && !ferror(inptr) && !feof(inptr))
                            img_pixels = img_height * img_width;
                        if(!iserror && !ferror(inptr) && !feof(inptr))
                        {
                            nf = read_ubyte(inptr,HERE); /* components */
                            if (nf > MAXSAMPLE)
                            {
                                // See https://github.com/hfiguiere/exifprobe/issues/2
                                // we shouldn't need to clamp nf value.
                                nf = MAXSAMPLE;
                                iserror = 1;
                            }
                        }
                        if(!iserror && !ferror(inptr) && !feof(inptr))
                        {
                            max_offset = ftell(inptr);
                            if(PRINT_SEGMENT)
                                chpr += printf(", %d bits/sample, components=%d, width=%d, height=%d",
                                                            p,(int)nf,(int)img_width,(int)img_height);
                            if(summary_entry)
                            {
                                summary_entry->spp = nf;
                                for(i = 0; i < nf; i++)
                                    summary_entry->bps[i] = p;
                            }
                        }
                        if(iserror || ferror(inptr) || feof(inptr))
                        {
                            tag = 0;
                            clearerr(inptr);
                            (void)jpeg_status(JPEG_HAD_ERROR);
                            if(summary_entry)
                                summary_entry->imagesubformat |= IMGSUBFMT_JPEGFAILED;
                            continue;
                        }
                        if(PRINT_SEGMENT)
                            chpr = newline(chpr);
                        if(Debug & JPEG_MARKER_DEBUG)
                        {
                            for(i = 0; i < nf; ++i)
                            {
                                putindent(6 + (2 * ADDRWIDTH) + indent + 16);
                                c = read_ubyte(inptr,HERE); /* component id */
                                tmp = read_ubyte(inptr,HERE); /* H | V    */
                                h = tmp & 0xf;
                                v = ((tmp & 0xf0) >> 4) & 0xf;
                                t = read_ubyte(inptr,HERE); /* Q tbl sel    */
                                if((ferror(inptr) == 0) && (PRINT_SEGMENT))
                                {
                                    chpr += printf("C%d=%d, H%d=%d, V%d=%d, T%d=%#04x",i,c,i,h,i,v,i,t);
                                    chpr = newline(chpr);
                                }
                            }
                        }
                        if(summary_entry && (img_height > 0) &&
                                                (summary_entry->pixel_height < img_height))
                        {
                            summary_entry->pixel_height = img_height;
                        }
                        if(summary_entry && (img_width > 0) &&
                                                (summary_entry->pixel_width < img_width))
                        {
                            summary_entry->pixel_width = img_width;
                        }
                        switch(tag)
                        {
                            /* lossless types are likely primary;     */
                            /* mark them for scan_summary()           */
                            case JPEG_SOF_3:
                            case JPEG_SOF_7:
                            case JPEG_SOF_11:
                            case JPEG_SOF_15:
                                summary_entry->subfiletype = POSSIBLE_PRIMARY_TYPE;
                                break;
                            default:
                                break;
                        }
                    }

                    if(!ferror(inptr) && !feof(inptr))
                    {
                        max_offset = ftell(inptr);
                        marker_offset += seg_length + 2;
                        /* next tag                                   */
                        tag = read_ushort(inptr,TIFF_MOTOROLA,marker_offset);
                        if(ferror(inptr) || feof(inptr))
                        {
                            tag = 0;
                            clearerr(inptr);
                            (void)jpeg_status(JPEG_HAD_ERROR);
                            if(summary_entry)
                                summary_entry->imagesubformat |= IMGSUBFMT_JPEGFAILED;
                        }
                        else
                            max_offset = ftell(inptr);
                    }
                    else
                    {
                        tag = 0;
                        clearerr(inptr);
                        (void)jpeg_status(JPEG_HAD_ERROR);
                        if(summary_entry)
                            summary_entry->imagesubformat |= IMGSUBFMT_JPEGFAILED;
                    }
                    break;
                    
                case JPEG_BADSOI:   /* minolta                        */
                    if(bad_soi == 0)
                        bad_soi = tag;
                    /* fall through                                   */
                case JPEG_SOI:
                    name = tagname(tag);
                    if((PRINT_SECTION) || (PRINT_SEGMENT))
                    {
                        print_tag_address(SECTION|SEGMENT,marker_offset,indent,prefix);
                        if(bad_soi)
                        {
                            printred("<JPEG_BADSOI>");
                            chpr += printf(" (%#06x)",bad_soi);
                        }
                        else
                            chpr += printf("<%s>",name);
                        chpr = newline(chpr);
                    }
                    start_of_jpeg_data = marker_offset;
                    if(bad_soi == 0)
                        ++had_soi;  
                    /* next tag                                       */
                    tag = read_ushort(inptr,TIFF_MOTOROLA,marker_offset + 2);
                    if(!ferror(inptr) && !feof(inptr))
                    {
                        marker_offset += 2;
                        max_offset = ftell(inptr);
                    }
                    else
                    {
                        tag = 0;
                        clearerr(inptr);
                        (void)jpeg_status(JPEG_HAD_ERROR);
                        if(summary_entry)
                            summary_entry->imagesubformat |= IMGSUBFMT_JPEGFAILED;
                    }
                    break;
                case JPEG_EOI:
                    name = tagname(tag);
                    max_offset = ftell(inptr);
                    if((PRINT_SECTION) || (PRINT_SEGMENT))
                    {
                        print_tag_address(SECTION|SEGMENT,marker_offset,indent,prefix);
                        if((status = jpeg_status(0)) == JPEG_EARLY_EOI)
                        {
                            PUSHCOLOR(RED);
                            chpr += printf("<%s> JPEG length %lu",name,max_offset - start_of_jpeg_data);
                            POPCOLOR();
                        }
                        else
                            chpr += printf("<%s> JPEG length %lu",name,max_offset - start_of_jpeg_data);
                        /* re-set the cleared status                  */
                        jpeg_status(status);
                        if((PRINT_SECTION) && (status != JPEG_EARLY_EOI))
                            chpr = newline(chpr);
                    }
                    if((summary_entry) && (summary_entry->length <= 0))
                    {
                        /* Use specified length, if any, even if      */
                        /* early EOI                                  */
                        if(data_length > 0)
                        {
                            summary_entry->length = data_length;
                            max_offset = start_of_jpeg_data + data_length;
                        }
                        else
                            summary_entry->length = max_offset - start_of_jpeg_data;
                    }
                    tag = 0;  /* break the loop                       */
                    break;
                case JPEG_APP0: 
                    marker_offset = process_app0(inptr,marker_offset,tag,summary_entry,
                                                            parent_name,indent + SMALLINDENT);
                    /* next tag                                       */
                    if(marker_offset > 0L)
                        tag = read_ushort(inptr,TIFF_MOTOROLA,marker_offset);
                    else
                        tag = 0;
                    if(ferror(inptr) || feof(inptr))
                    {
                        clearerr(inptr);
                        (void)jpeg_status(JPEG_HAD_ERROR);
                        if(summary_entry)
                            summary_entry->imagesubformat |= IMGSUBFMT_JPEGFAILED;
                    }
                    else
                        max_offset = ftell(inptr);
                    break;
                case JPEG_APP1:
                    marker_offset = process_app1(inptr,marker_offset,tag,summary_entry,
                                                            parent_name,indent + SMALLINDENT);
                    /* next tag                                       */
                    if(marker_offset > 0L)
                        tag = read_ushort(inptr,TIFF_MOTOROLA,marker_offset);
                    else
                        tag = 0;
                    if(ferror(inptr) || feof(inptr))
                    {
                        clearerr(inptr);
                        (void)jpeg_status(JPEG_HAD_ERROR);
                        if(summary_entry)
                            summary_entry->imagesubformat |= IMGSUBFMT_JPEGFAILED;
                    }
                    break;
                case JPEG_APP3:
                    marker_offset = process_app3(inptr,marker_offset,tag,summary_entry,
                                                            parent_name,indent + SMALLINDENT);
                    /* next tag                                       */
                    if(marker_offset > 0L)
                        tag = read_ushort(inptr,TIFF_MOTOROLA,marker_offset);
                    else
                        tag = 0;
                    if(ferror(inptr) || feof(inptr))
                    {
                        clearerr(inptr);
                        (void)jpeg_status(JPEG_HAD_ERROR);
                        if(summary_entry)
                            summary_entry->imagesubformat |= IMGSUBFMT_JPEGFAILED;
                    }
                    break;
                case JPEG_APP12:
                    marker_offset = process_app12(inptr,marker_offset,tag,
                                            summary_entry,parent_name,indent + SMALLINDENT);
                    /* next tag                                       */
                    if(marker_offset > 0L)
                        tag = read_ushort(inptr,TIFF_MOTOROLA,marker_offset);
                    else
                        tag = 0;
                    if(ferror(inptr) || feof(inptr))
                    {
                        clearerr(inptr);
                        (void)jpeg_status(JPEG_HAD_ERROR);
                        if(summary_entry)
                            summary_entry->imagesubformat |= IMGSUBFMT_JPEGFAILED;
                    }
                    else
                        max_offset = ftell(inptr);
                    break;
                case JPEG_APP2:
                case JPEG_APP4:
                case JPEG_APP5:
                case JPEG_APP6:
                case JPEG_APP7:
                case JPEG_APP8:
                case JPEG_APP9:
                case JPEG_APP10:
                case JPEG_APP11:
                case JPEG_APP13:
                case JPEG_APP14:
                case JPEG_APP15:
                    marker_offset = process_appn(inptr,marker_offset,tag,
                                            summary_entry,parent_name,indent + SMALLINDENT);
                    /* next tag                                       */
                    if(marker_offset > 0L)
                        tag = read_ushort(inptr,TIFF_MOTOROLA,marker_offset);
                    else
                        tag = 0;
                    if(ferror(inptr) || feof(inptr))
                    {
                        tag = 0;
                        clearerr(inptr);
                    }
                    else
                        max_offset = ftell(inptr);
                    break;
                default:
                    /* Minolta sometimes writes garbage in the high   */
                    /* byte of SOI tags; assume that 0xd8 in the low  */
                    /* byte is a bad SOI.                             */
                    if(((tag & 0xff00) != 0xff00) && ((tag & 0x00ff) == 0xd8))
                    {
                        /* Only if no SOI yet                         */
                        if((had_soi == 0) && (bad_soi == 0))
                        {
                            bad_soi = tag;
                            tag &= 0x00ff;  /* This will match        */
                                            /* JPEG_BADSOI above.     */
                            continue;
                        }
                        else if(bad_soi)
                            had_soi = 0;
                        /* ###%%% this should be handled better;      */
                        /* don't set had_soi on a bad_soi but set it  */
                        /* after a legitimate marker is found NEXT;   */
                        /* I.e. we have a legitimate jpeg only if a   */
                        /* good marker is found, after which an error */
                        /* indicates corrupted jpeg data, otherwise   */
                        /* an error should indicate no jpeg data      */
                        /* found.                                     */
                    }
                    name = tagname(tag);
                    if(PRINT_SEGMENT)
                    {
                        print_tag_address(SEGMENT,marker_offset,indent + SMALLINDENT,prefix);
                        chpr += printf("<%s>",name);
                    }
                    if((tag & 0xff00) != 0xff00)
                    {
                        if(PRINT_SEGMENT)
                        {
                            printred(" INVALID JPEG TAG");
                            chpr = newline(chpr);
                        }
                        tag = 0;
                        if(had_soi)
                            (void)jpeg_status(JPEG_HAD_ERROR);
                        else
                            (void)jpeg_status(JPEG_NO_SOI);
                        if(summary_entry)
                        {
                            /* If this is the first tag, we have no   */
                            /* reason to believe that this is really  */
                            /* jpeg data. If not, data is corrupted   */
                            if(had_soi)
                                summary_entry->imagesubformat |= IMGSUBFMT_JPEGFAILED;
                            else
                            {
                                summary_entry->imagesubformat |= IMGSUBFMT_NO_JPEG;
                                /* ###%%% hmmm...                     */
                                /* summary_entry->compression = 0;    */
                            }
                        }
                        else
                            printred("JPEG: NO SUMMARY ENTRY ");
                        clearerr(inptr);
                        if(Max_imgdump > 0)
                        {
                            /* If the length of the data is unknown,  */
                            /* limit the dump to something reasonable */
                            if(Max_imgdump == DUMPALL)
                            {
                                if(data_length > 0)
                                    dumplength = data_length;
                                else
                                    data_length = dumplength = 1024;
                            }
                            else if(Max_imgdump > data_length)
                            {
                                if(data_length > 0)
                                    dumplength = data_length;
                                else
                                    data_length = dumplength = 1024;
                            }
                            else
                                dumplength = Max_imgdump;
                            chpr = newline(0);
                            hexdump(inptr,start_of_jpeg_data,data_length,
                                        dumplength,16,indent,SMALLINDENT);
                            chpr = newline(1);
                        }
                        max_offset = start_of_jpeg_data + data_length;
                    }
                    else
                    {
                        seg_length = read_ushort(inptr,TIFF_MOTOROLA,marker_offset + 2);
                        if(!ferror(inptr) && !feof(inptr))
                        {
                            if(PRINT_SEGMENT)
                            {
                                chpr += printf(" length %u",seg_length);
                                chpr = newline(chpr);
                            }
                            marker_offset = marker_offset + 2 + seg_length;
                            tag = read_ushort(inptr,TIFF_MOTOROLA,marker_offset);
                            if(ferror(inptr) || feof(inptr))
                            {
                                tag = 0;
                                clearerr(inptr);
                                (void)jpeg_status(JPEG_HAD_ERROR);
                                if(summary_entry)
                                    summary_entry->imagesubformat |= IMGSUBFMT_JPEGFAILED;
                            }
                            else
                                max_offset = ftell(inptr);
                        }
                        else
                        {
                            tag = 0;
                            clearerr(inptr);
                            (void)jpeg_status(JPEG_HAD_ERROR);
                            if(summary_entry)
                                summary_entry->imagesubformat |= IMGSUBFMT_JPEGFAILED;
                        }
                    }
                    break;
            }
        }
        POPCOLOR();
    }
    setcharsprinted(chpr);
    return(max_offset);
}

/* set and report errors in jpeg files "out of band".                 */

int
jpeg_status(int setstatus)
{
    static int status = 0;
    int retvalue;

    retvalue = status;
    status = setstatus;
    return(retvalue);
}

#define JPEG_HIBYTE 0xff
#define EOI_LOBYTE  0xd9

unsigned long
forward_scan_for_eoi(FILE *inptr,unsigned long start_of_data,
                            unsigned long end_of_section,char *prefix,int indent)
{
    int highbyte,lobyte;
    unsigned long eoi_offset = 0UL;
    unsigned long tagloc;
    int chpr = 0;

    if(inptr && (fseek(inptr,start_of_data,SEEK_SET) != -1))
    {
        if(Debug & JPEG_EOI_DEBUG)
            printf("DEBUG: start scan at %lu\n",start_of_data);
        while(((highbyte = fgetc(inptr)) != EOF) && (highbyte != EOF) && (ftell(inptr) < end_of_section))
        {
            if(highbyte != JPEG_HIBYTE)
                continue;
            tagloc = ftell(inptr) - 1;
            lobyte = fgetc(inptr);
            if(lobyte == EOI_LOBYTE)
            {
                eoi_offset = tagloc;
                if(Debug & JPEG_EOI_DEBUG)
                {
                    if(!(LIST_MODE) && (PRINT_SEGMENT))
                    {
                        print_tag_address(SEGMENT,eoi_offset,indent + SMALLINDENT,prefix);
                        PUSHCOLOR(RED);
                        putindent(2);
                        chpr += printf("possible JPEG_EOI found at %lu",eoi_offset);
                    }
                    POPCOLOR();
                    chpr = newline(chpr);
                }
                break;
            }
        }
    }
    if(Debug & JPEG_EOI_DEBUG)
        printf("DEBUG: end scan at %lu\n",ftell(inptr));
    return(eoi_offset);
}


/* JFIF format files                                                  */

unsigned long
process_app0(FILE *inptr,unsigned long app0_offset,unsigned short tag,
                struct image_summary *summary_entry,char *parent_name,
                int indent)
{
    unsigned long max_offset = 0L;
    unsigned long data_offset,dumplength;
    unsigned long filesize = 0UL;
    char *app_string,*name;
    unsigned short app_length = 0L;
    int status = 0;
    int chpr = 0;
    unsigned short marker,xt,yt,tmp;
    struct fileheader *header = NULL;
    char *fulldirname = CNULL;
    char *subdirname = CNULL;

    if(inptr)
    {
        filesize = get_filesize(inptr);
        if(summary_entry)
        {
            summary_entry->filesubformat |= FILESUBFMT_APPN;
            summary_entry->filesubformatAPPN[0] = 1;
        }
        name = tagname(tag);
        app_length = read_ushort(inptr,TIFF_MOTOROLA,app0_offset + 2);
        if((PRINT_SECTION))
            print_tag_address(SECTION,app0_offset,indent,"@");
        data_offset = app0_offset + 4L;
        app_string = read_appstring(inptr,JPEG_APP0,data_offset);
        app_string = app_string ? app_string : QSTRING;
        if(PRINT_SECTION)
            chpr += printf("<%s> %#x length %u, ",name,tag,app_length);
        else if((PRINT_ENTRY))
        {
            /* ###%%% pseudo-tags fpr LIST Mode here */
            print_tag_address(ENTRY,HERE,indent,"*");
            if((PRINT_TAGINFO))
            {
                if((PRINT_LONGNAMES))
                    chpr += printf("%s.",parent_name);
                chpr += printf("%-14.14s","APP0");
            }
            if((PRINT_VALUE))
                chpr += printf(" = @%lu:%-9u",app0_offset,app_length);
            chpr = newline(chpr);
        }

        fulldirname = splice(parent_name,".","APP0");
        if((strncmp(app_string,"JFIF",4) == 0) && (app_string[4] == '\0'))
        {
            if(summary_entry)
                summary_entry->filesubformat |= FILESUBFMT_JFIF;
            data_offset += 5;
            if((PRINT_SECTION))
            {
                chpr += printf("\'%s\'",app_string);
                chpr = newline(chpr);
            }
            tmp = read_ushort(inptr,TIFF_MOTOROLA,data_offset);
            if((PRINT_APPENTRY) && (PRINT_ENTRY))
            {
                print_tag_address(APP_ENTRY,data_offset,indent,"@");
                if(PRINT_SECTION)
                    extraindent(SMALLINDENT);
                if((PRINT_TAGINFO))
                {
                    if((PRINT_LONGNAMES))
                        chpr += printf("%s.",fulldirname);
                    chpr += printf("%-14.14s","Version");
                }
                if((PRINT_VALUE))
                    chpr += printf("= %d.%d",(int)((tmp >> 8) & 0xff),(int)(tmp & 0xff));
                chpr = newline(chpr);
            }
            data_offset = ftell(inptr);
            tmp = read_ubyte(inptr,data_offset);
            if((PRINT_APPENTRY) && (PRINT_ENTRY))
            {
                print_tag_address(APP_ENTRY,data_offset,indent,"@");
                if(PRINT_SECTION)
                    extraindent(SMALLINDENT);
                if((PRINT_TAGINFO))
                {
                    if((PRINT_LONGNAMES))
                        chpr += printf("%s.",fulldirname);
                    chpr += printf("%-14.14s","Units");
                }
                if((PRINT_VALUE))
                    chpr += printf("= %s",tmp? tmp == 1 ? "'dots/inch'" : "'dots/cm'" : "'aspect ratio'");
                chpr = newline(chpr);
            }
            data_offset = ftell(inptr);
            tmp = read_ushort(inptr,TIFF_MOTOROLA,data_offset);
            if((PRINT_APPENTRY) && (PRINT_ENTRY))
            {
                print_tag_address(APP_ENTRY,data_offset,indent,"@");
                if(PRINT_SECTION)
                    extraindent(SMALLINDENT);
                if((PRINT_TAGINFO))
                {
                    if((PRINT_LONGNAMES))
                        chpr += printf("%s.",fulldirname);
                    chpr += printf("%-14.14s","Xdensity");
                }
                if((PRINT_VALUE))
                    chpr += printf("= %u",tmp);
                chpr = newline(chpr);
            }
            data_offset = ftell(inptr);
            tmp = read_ushort(inptr,TIFF_MOTOROLA,data_offset);
            if((PRINT_APPENTRY) && (PRINT_ENTRY))
            {
                print_tag_address(APP_ENTRY,data_offset,indent,"@");
                if(PRINT_SECTION)
                    extraindent(SMALLINDENT);
                if((PRINT_TAGINFO))
                {
                    if((PRINT_LONGNAMES))
                        chpr += printf("%s.",fulldirname);
                    chpr += printf("%-14.14s","Ydensity");
                }
                if((PRINT_VALUE))
                    chpr += printf("= %u",tmp);
                chpr = newline(chpr);
            }
            data_offset = ftell(inptr);
            xt = read_ubyte(inptr,data_offset);
            if((PRINT_APPENTRY) && (PRINT_ENTRY))
            {
                print_tag_address(APP_ENTRY,data_offset,indent,"@");
                if(PRINT_SECTION)
                    extraindent(SMALLINDENT);
                if((PRINT_TAGINFO))
                {
                    if((PRINT_LONGNAMES))
                        chpr += printf("%s.",fulldirname);
                    chpr += printf("%-14.14s","XThumbnail");
                }
                if((PRINT_VALUE))
                    chpr += printf("= %d",(int)xt);
                chpr = newline(chpr);
            }
            data_offset = ftell(inptr);
            yt = read_ubyte(inptr,data_offset);
            if((PRINT_APPENTRY) && (PRINT_ENTRY))
            {
                print_tag_address(APP_ENTRY,data_offset,indent,"@");
                if(PRINT_SECTION)
                    extraindent(SMALLINDENT);
                if((PRINT_TAGINFO))
                {
                    if((PRINT_LONGNAMES))
                        chpr += printf("%s.",fulldirname);
                    chpr += printf("%-14.14s","YThumbnail");
                }
                if((PRINT_VALUE))
                    chpr += printf("= %d",(int)yt);
                chpr = newline(chpr);
            }
            if(xt && yt)
            {
                data_offset = ftell(inptr);
                print_tag_address(APP_ENTRY,data_offset,indent,"@");
                if((PRINT_APPENTRY) && (PRINT_SECTION))
                    extraindent(SMALLINDENT);
                if((PRINT_TAGINFO))
                {
                    if(PRINT_LONGNAMES)
                        chpr += printf("%s.",fulldirname);
                    chpr += printf("%-14.14s","Thumbnail");
                }
                if((PRINT_VALUE))
                {
                    chpr += printf(" = '%u RGB bytes (%u) pixels'",3 * xt * yt,xt * yt);
                    chpr = newline(chpr);
                }
            }
            max_offset = data_offset + 1 + (3 * xt * yt);
        }
        else if((strncmp(app_string,"JFXX",4) == 0) && (app_string[4] == '\0'))
        {
            if(summary_entry)
                summary_entry->filesubformat |= FILESUBFMT_JFXX;
            data_offset += 5;
            tmp = read_ubyte(inptr,data_offset);
            if((PRINT_SECTION))
            {
                chpr += printf("\'%s\'",app_string);
                chpr = newline(chpr);
                print_tag_address(SECTION,data_offset,indent,"@");
                if((PRINT_SECTION))
                {
                    extraindent(SMALLINDENT);
                    chpr += printf(" extension code %#x",tmp);
                }
            }
            else
                chpr = newline(chpr);
            ++data_offset;
            switch(tmp)
            {
            case 0x10:  if((PRINT_SECTION))
                        {
                            chpr += printf(" - JPEG thumbnail");
                            chpr = newline(chpr);
                        }
                        marker = read_ushort(inptr,TIFF_MOTOROLA,HERE);
                        summary_entry = new_summary_entry(summary_entry,0,IMGFMT_JPEG);
                        if(summary_entry)
                        {
                            summary_entry->filesubformat |= FILESUBFMT_JFXX;
                            summary_entry->subfiletype = REDUCED_RES_TYPE;
                        }
                        if((LIST_MODE))
                        {
                            if((PRINT_SEGMENT))
                                chpr = newline(chpr);
                            print_tag_address(ENTRY,data_offset,indent + SMALLINDENT,"@");
                            if((PRINT_TAGINFO))
                            {
                                if(PRINT_LONGNAMES)
                                    chpr += printf("%s.JFXX.",fulldirname);
                                chpr += printf("%-14.14s","JpegThumbnail");
                            }
                            if((PRINT_VALUE))
                            {
                                max_offset = app0_offset + app_length + 2;
                                chpr += printf(" = @%lu:%lu",data_offset,max_offset - data_offset);
                                chpr = newline(chpr);
                            }
                        }
                        max_offset = process_jpeg_segments(inptr,data_offset,marker,
                                            app_length - 8,summary_entry,parent_name,
                                            "@",indent + SMALLINDENT);
                        if((max_offset - 1) > filesize)
                        {
                            PUSHCOLOR(RED);
                            chpr += printf(" (TRUNCATED at %lu)",filesize);
                            POPCOLOR();
                            summary_entry->imagesubformat |= IMGSUBFMT_TRUNCATED;
                        }
                        max_offset = app0_offset + app_length + 2;
                        if((PRINT_SECTION))
                        {
                            if((status = jpeg_status(0) == JPEG_EARLY_EOI))
                                chpr = newline(chpr);
                            jpeg_status(status);
                            print_tag_address(SECTION,max_offset - 1,indent + SMALLINDENT,"@");
                            chpr += printf("#### End of JPEG thumbnail data for APP0");
                            chpr += printf(", length %lu ####",max_offset - data_offset);
                            print_jpeg_status();
                            chpr = newline(chpr);
                        }
                        break;
            case 0x11:  if((PRINT_APPENTRY) && (PRINT_SECTION))
                            chpr += printf(" - thumbnail stored using 1 byte/pixel"); 
                        xt = read_ubyte(inptr,data_offset++);
                        yt = read_ubyte(inptr,data_offset++);
                        /* The RGB palette is 768 bytes               */
                        max_offset = data_offset + 768 + (xt * yt) ;
                        summary_entry = new_summary_entry(summary_entry,0,IMGFMT_JPEG);
                        if(summary_entry)
                        {
                            summary_entry->filesubformat |= FILESUBFMT_JFXX;
                            summary_entry->subfiletype = REDUCED_RES_TYPE;
                            summary_entry->imagesubformat = IMGSUBFMT_PALETTE;
                            summary_entry->offset = data_offset;
                            summary_entry->length = xt * yt + 768;
                            summary_entry->pixel_width = xt;
                            summary_entry->pixel_height = yt;
                            summary_entry->compression = 1;
                            summary_entry->spp = 1;
                            summary_entry->sample_size = 8;
                            summary_entry->datatype = JPEG_APP0;
                            summary_entry->entry_lock = lock_number(summary_entry);
                        }
                        break;
            case 0x13:  if((PRINT_APPENTRY) && (PRINT_SECTION))
                            chpr += printf(" - thumbnail stored using 3 bytes/pixel");
                        xt = read_ubyte(inptr,data_offset++);
                        yt = read_ubyte(inptr,data_offset++);
                        max_offset = data_offset + (3 * xt * yt) ;
                        summary_entry = new_summary_entry(summary_entry,0,IMGFMT_JPEG);
                        if(summary_entry)
                        {
                            summary_entry->filesubformat |= FILESUBFMT_JFXX;
                            summary_entry->subfiletype = REDUCED_RES_TYPE;
                            summary_entry->imagesubformat = IMGSUBFMT_RGB;
                            summary_entry->offset = data_offset;
                            summary_entry->length = xt * yt * 3;
                            summary_entry->pixel_width = xt;
                            summary_entry->pixel_height = yt;
                            summary_entry->compression = 1;
                            summary_entry->spp = 3;
                            summary_entry->sample_size = 8;
                            summary_entry->datatype = JPEG_APP0;
                            summary_entry->entry_lock = lock_number(summary_entry);
                        }
                        break;
            default:    
                        if((PRINT_APPENTRY) && (PRINT_SECTION))
                            chpr += printf(" - UNKNOWN JFXX extension");
                        max_offset = data_offset;
                        break;
            }
            chpr = newline(chpr);
        }
        else if(((strncmp(app_string,"II",2) == 0) || (strncmp(app_string,"MM",2) == 0)) &&
                    (app_string[2] == 0x1a))    /* maybe CIFF     */
        {
            if((header = read_imageheader(inptr,data_offset)))
            {
                if(summary_entry)
                    summary_entry->filesubformat |= FILESUBFMT_CIFF;
                if((PRINT_APPENTRY) && (PRINT_SECTION))
                    chpr = newline(chpr);
                if((PRINT_SECTION))
                {
                    print_tag_address(SECTION,data_offset,indent+SMALLINDENT,"@");
                    print_header(header,SECTION);
                }
                if(header->probe_magic == PROBE_CIFFMAGIC)
                {
                    subdirname = splice(fulldirname,".","CIFF");
                    if(!(PRINT_SECTION))
                    {
                        print_tag_address(ENTRY|VALUE,data_offset,indent+SMALLINDENT,"@");
                        if((PRINT_TAGINFO))
                        {
                            if((PRINT_LONGNAMES))
                                chpr += printf("%s.",subdirname);
                            chpr += printf("%-*.*s",CIFFTAGWIDTH,CIFFTAGWIDTH,"HeaderOffset");
                        }
                        if((PRINT_VALUE))
                        {
                            if(PRINT_BOTH_OFFSET)
                                chpr += printf(" = @%#lx=%lu",data_offset,data_offset);
                            else if(PRINT_HEX_OFFSET)
                                chpr += printf(" = @%#lx",data_offset);
                            else 
                                chpr += printf(" = @%lu",data_offset);
                        }
                        chpr = newline(chpr);
                    }
                    max_offset = process_ciff(inptr,header,data_offset,app_length - 2,
                                        summary_entry,subdirname,0,indent+SMALLINDENT);
                    if(subdirname)
                        free(subdirname);
                    subdirname = CNULL;
                }
                else
                    goto appdump;
            }
            else
                goto appdump;
        }
        else
            goto appdump;

        if(fulldirname)
            free(fulldirname);
    /* If there's data unaccounted for, allow the data that isn't */
        /* accounted for to be dumped                                 */
        if(max_offset < (app0_offset + app_length + 2))
        {
            print_tag_address(SEGMENT,max_offset,indent,"@");
            extraindent(SMALLINDENT);
            dumplength = app0_offset + app_length + 1 - max_offset;
            chpr += printf("---- End of data before end of APP0 (%lu bytes)",dumplength);
            chpr = newline(chpr);
            /* dump at least the first couple of rows, just to see... */
            if(PRINT_APPNDUMP)
            {
                if((Max_appdump == DUMPALL) || (Max_appdump > (app_length + 2)))
                    dumplength = app_length - 2;
                else if(Max_appdump > 0L)
                    dumplength = Max_appdump;
                else if(Max_undefined > 0L)
                    dumplength = Max_undefined;
                else
                    dumplength = app_length - 2;
            }
            else
                dumplength = dumplength > 48 ? 48 : dumplength;
            if(dumplength)
            {
                hexdump(inptr,app0_offset + 4,app_length - 2,dumplength,16,
                            indent + SMALLINDENT,SMALLINDENT);
                chpr = newline(1);
            }
        }
        /* ###%%% if max_offset >, warn */
            
        /* close off the section.                                     */
        if((PRINT_SECTION))
        {
            print_tag_address(SECTION,app0_offset + app_length + 1,indent,"@");
            chpr += printf("</%s>",name);
            chpr = newline(chpr);
        }
    }

    return(app0_offset + app_length + 2);
appdump:
    /* Something went haywire...dump starting at the marker           */
    /* At the moment, this happens only for imaginative APP0 markers  */
    if(app_length > 0L)
    {
        if(PRINT_APPNDUMP)
        {
            if((Max_appdump == DUMPALL) || (Max_appdump > (app_length + 2)))
                dumplength = app_length + 2;
            else if(Max_appdump > 0L)
                dumplength = Max_appdump;
            else
                dumplength = Max_undefined;
            if(dumplength)
            {
                chpr = newline(1);
                hexdump(inptr,app0_offset,app_length + 2,dumplength,16,
                            indent + SMALLINDENT,SMALLINDENT);
                chpr = newline(1);
            }
        }
        else
        {
            if(PRINT_SECTION)
            {
                chpr += printf(" - (not dumped: use -A)");
                chpr = newline(chpr);
            }
        }
        max_offset = app0_offset + app_length + 2;
        /* close off the segment; a start "tag" was printed           */
        if(PRINT_SECTION)
        {
            print_tag_address(SECTION,app0_offset + app_length + 1,indent,"@");
            chpr += printf("</%s>",name);
            chpr = newline(chpr);
        }
    }
    setcharsprinted(chpr);
    return(max_offset);
}

/* Process (print) an APP1 (EXIF) section                             */

unsigned long
process_app1(FILE *inptr,unsigned long app1_offset,unsigned short tag,
                struct image_summary *summary_entry,char *parent_name,
                int indent)
{
    unsigned long max_offset = 0UL;
    unsigned long ifd_offset,fileoffset_base;
    unsigned long dumplength = 0UL;
    unsigned short app_length = 0;
    int chpr = 0;
    struct fileheader *header;
    char *app_string,*name;
    char *fulldirname = CNULL;

    if(inptr)
    {
        if(summary_entry)
        {
            summary_entry->filesubformat |= FILESUBFMT_APPN;
            summary_entry->filesubformatAPPN[1] = 1;
        }
        /* display the APP1 header                                    */
        name = tagname(tag);    /* never null                         */
        if(PRINT_SECTION)
        {
            print_tag_address(SECTION,app1_offset,indent,"@");
            chpr += printf("<%s> %#x ",name,tag);
        }

        app_length = read_ushort(inptr,TIFF_MOTOROLA,HERE);
        if(ferror(inptr) || feof(inptr))
        {
            clearerr(inptr);
            goto appdump;
        }
        if(PRINT_SECTION)
            chpr += printf("length %u, ",app_length);
        else if((PRINT_ENTRY))
        {
            /* ###%%% pseudo-tags fpr LIST Mode here */
            print_tag_address(ENTRY,HERE,indent,"*");
            if((PRINT_TAGINFO))
            {
                if((PRINT_LONGNAMES))
                    chpr += printf("%s.",parent_name);
                chpr += printf("%-14.14s","APP1");
            }
            if((PRINT_VALUE))
                chpr += printf(" = @%lu:%-9u",app1_offset,app_length);
            chpr = newline(chpr);
        }

        app_string = read_appstring(inptr,JPEG_APP1,HERE);
        if(ferror(inptr) || feof(inptr))
        {
            clearerr(inptr);
            goto appdump;
        }

        if(app_string)
        {
            if(PRINT_SECTION)
                chpr += printf("\'%s\'",app_string);

            /* The next thing we see should be a TIFF header tag      */
            /* indicating byte order used in the IFD, followed by the */
            /* offset from the start of the header to the start of    */
            /* the IFD (number of entries).                           */

            fileoffset_base = ftell(inptr);
            /* If this is a valid Exif segment, skip the pad byte.    */
            if(strncmp(app_string,"Exif",4) == 0)
            {
                fulldirname = splice(parent_name,".","APP1");
                ++fileoffset_base;
                if(PRINT_SECTION)
                {
                    chpr = newline(chpr);
                    print_tag_address(SECTION,fileoffset_base,indent + SMALLINDENT,"@");
                }
                header = read_imageheader(inptr,fileoffset_base);
                if(header && (print_header(header,SECTION) == 0) &&
                    (header->probe_magic == TIFF_MAGIC))
                {
                    ifd_offset = read_ulong(inptr,header->file_marker,HERE);
                    if(PRINT_SECTION)
                    {
                        chpr += printf(" ifd offset = %lu (+ %lu = %#lx/%lu)",ifd_offset,
                                    fileoffset_base,fileoffset_base + ifd_offset,
                                    fileoffset_base + ifd_offset);
                        chpr = newline(chpr);
                    }
                    /* ###%%% maybe should unlock the summary entry,  */
                    /* so that the tiff processor will use it? not    */
                    /* sure if that's appropriate for APP1 found in   */
                    /* jpeg sub-images.                               */
                    max_offset = process_tiff_ifd(inptr,header->file_marker,ifd_offset,
                                            fileoffset_base,0L,summary_entry,fulldirname,
                                            TIFF_IFD,0,-1,indent + SMALLINDENT);

                    if(max_offset < app1_offset + app_length + 2)
                        max_offset = app1_offset + app_length +2;
                }
                else
                {
                    max_offset = app1_offset + app_length + 2;
                    if(PRINT_SECTION)
                    {
                        extraindent(indent + ADDRWIDTH);
                        if(header)
                        {
                            chpr += printf(" INVALID MAGIC %lu (%s) where TIFF header should be",
                                         header->probe_magic,tagname(header->probe_magic));
                        }
                        else
                        {
                            chpr += printf(" INVALID HEADER for TIFF");
                        }
#define PRINT_A_BIT 48
                        if(app_length > PRINT_A_BIT)
                            dumplength = PRINT_A_BIT;
                        else
                            dumplength = app_length;
                        goto appdump;
                    }
                }
                if(fulldirname)
                    free(fulldirname);
            }
            else
            {
                max_offset = app1_offset + app_length + 2;
                if(PRINT_SECTION)
                {
                    chpr += printf(" - unknown format");
                    goto appdump;
                }
            }
        }
        else if(PRINT_SECTION)
        {
            chpr += printf("  No APP1 header name string found");
            chpr = newline(chpr);
        }
        /* offset of last byte of segment                             */
        if(PRINT_SECTION)
        {
            print_tag_address(SECTION,max_offset - 1,indent,"-");
            chpr += printf("</%s>",name);
            chpr = newline(chpr);
        }
    }
    return(max_offset);

appdump:
    if(app_length > 0L)
    {
        if((PRINT_APPNDUMP) || (dumplength > 0))
        {
            if((Max_appdump == DUMPALL) || (Max_appdump > (app_length + 2)))
                dumplength = app_length + 2;
            else if(Max_appdump > 0L)
                dumplength = Max_appdump;
            else if(dumplength == 0)
                dumplength = Max_undefined;
            if(dumplength)
            {
                chpr = newline(1);
                hexdump(inptr,app1_offset,app_length + 2,dumplength,16,
                            indent + SMALLINDENT,SMALLINDENT);
                chpr = newline(1);
            }
        }
        else
        {
            if(PRINT_SEGMENT)
            {
                chpr += printf(" - (not dumped: use -A)");
                chpr = newline(chpr);
            }
        }
        max_offset = app1_offset + app_length + 2;
        /* close off the segment; a start "tag" was printed           */
        print_tag_address(SECTION,app1_offset + app_length + 1,indent,"-");
        if(PRINT_SECTION)
        {
            chpr += printf("</%s>",name);
            chpr = newline(chpr);
        }
    }
    setcharsprinted(chpr);
    return(max_offset);
}

/* Process (print) an APP3 (Meta) section                             */

unsigned long
process_app3(FILE *inptr,unsigned long app3_offset,unsigned short tag,
                struct image_summary *summary_entry,char *parent_name,
                int indent)
{
    unsigned long max_offset = 0UL;
    unsigned long ifd_offset,fileoffset_base;
    unsigned long dumplength = 0UL;
    unsigned short app_length = 0;
    int chpr = 0;
    struct fileheader *header;
    char *app_string,*name;
    char *fulldirname = CNULL;

    if(inptr)
    {
        if(summary_entry)
        {
            summary_entry->filesubformat |= FILESUBFMT_APPN;
            summary_entry->filesubformatAPPN[3] = 1;
        }
        /* display the APP3 header                                    */
        name = tagname(tag);    /* never null                         */
        if(PRINT_SECTION)
        {
            print_tag_address(SECTION,app3_offset,indent,"@");
            chpr += printf("<%s> %#x ",name,tag);
        }

        app_length = read_ushort(inptr,TIFF_MOTOROLA,HERE);
        if(ferror(inptr) || feof(inptr))
        {
            clearerr(inptr);
            goto appdump;
        }
        if(PRINT_SECTION)
            chpr += printf("length %u, ",app_length);
        else if((PRINT_ENTRY))
        {
            /* ###%%% pseudo-tags fpr LIST Mode here */
            print_tag_address(ENTRY,HERE,indent,"*");
            if((PRINT_TAGINFO))
            {
                if((PRINT_LONGNAMES))
                    chpr += printf("%s.",parent_name);
                chpr += printf("%-14.14s","APP3");
            }
            if((PRINT_VALUE))
                chpr += printf(" = @%lu:%-9u",app3_offset,app_length);
            chpr = newline(chpr);
        }

        app_string = read_appstring(inptr,JPEG_APP3,HERE);
        if(ferror(inptr) || feof(inptr))
        {
            clearerr(inptr);
            goto appdump;
        }

        if(app_string)
        {
            if(PRINT_SECTION)
                chpr += printf("\'%s\'",app_string);

            /* The next thing we see should be a TIFF header tag      */
            /* indicating byte order used in the IFD, followed by the */
            /* offset from the start of the header to the start of    */
            /* the IFD (number of entries).                           */

            fileoffset_base = ftell(inptr);
            /* If this is a valid Meta segment, skip the pad byte.    */
            if(strncmp(app_string,"Meta",4) == 0)
            {
                fulldirname = splice(parent_name,".","APP3");
                ++fileoffset_base;
                if(PRINT_SECTION)
                {
                    chpr = newline(chpr);
                    print_tag_address(SECTION,fileoffset_base,indent + SMALLINDENT,"@");
                }
                header = read_imageheader(inptr,fileoffset_base);
                if(header && (print_header(header,SECTION) == 0) &&
                    (header->probe_magic == TIFF_MAGIC))
                {
                    ifd_offset = read_ulong(inptr,header->file_marker,HERE);
                    if(PRINT_SECTION)
                    {
                        chpr += printf(" ifd offset = %lu (+ %lu = %#lx/%lu)",ifd_offset,
                                    fileoffset_base,fileoffset_base + ifd_offset,
                                    fileoffset_base + ifd_offset);
                        chpr = newline(chpr);
                    }
                    max_offset = process_tiff_ifd(inptr,header->file_marker,ifd_offset,
                                            fileoffset_base,0L,summary_entry,fulldirname,
                                            TIFF_IFD,0,-1,indent + SMALLINDENT);

                    if(max_offset < app3_offset + app_length + 2)
                        max_offset = app3_offset + app_length +2;
                }
                else
                {
                    max_offset = app3_offset + app_length + 2;
                    if(PRINT_SECTION)
                    {
                        extraindent(indent + ADDRWIDTH);
                        if(header)
                        {
                            chpr += printf(" INVALID MAGIC %lu (%s) where TIFF header should be",
                                           header->probe_magic,tagname(header->probe_magic));
                        }
                        else
                        {
                            chpr += printf(" INVALID HEADER for TIFF");
                        }
#define PRINT_A_BIT 48
                        if(app_length > PRINT_A_BIT)
                            dumplength = PRINT_A_BIT;
                        else
                            dumplength = app_length;
                        goto appdump;
                    }
                }
                if(fulldirname)
                    free(fulldirname);
            }
            else
            {
                max_offset = app3_offset + app_length + 2;
                if(PRINT_SECTION)
                {
                    chpr += printf(" - unknown format");
                    goto appdump;
                }
            }
        }
        else if(PRINT_SECTION)
        {
            chpr += printf("  No APP3 header name string found");
            chpr = newline(chpr);
        }
        /* offset of last byte of segment                             */
        if(PRINT_SECTION)
        {
            print_tag_address(SECTION,max_offset - 1,indent,"-");
            chpr += printf("</%s>",name);
            chpr = newline(chpr);
        }
    }
    return(max_offset);

appdump:
    if(app_length > 0L)
    {
        if((PRINT_APPNDUMP) || (dumplength > 0))
        {
            if((Max_appdump == DUMPALL) || (Max_appdump > (app_length + 2)))
                dumplength = app_length + 2;
            else if(Max_appdump > 0L)
                dumplength = Max_appdump;
            else if(dumplength == 0)
                dumplength = Max_undefined;
            if(dumplength)
            {
                chpr = newline(1);
                hexdump(inptr,app3_offset,app_length + 2,dumplength,16,
                            indent + SMALLINDENT,SMALLINDENT);
                chpr = newline(1);
            }
        }
        else
        {
            if(PRINT_SEGMENT)
            {
                chpr += printf(" - (not dumped: use -A)");
                chpr = newline(chpr);
            }
        }
        max_offset = app3_offset + app_length + 2;
        /* close off the segment; a start "tag" was printed           */
        print_tag_address(SECTION,app3_offset + app_length + 1,indent,"-");
        if(PRINT_SECTION)
        {
            chpr += printf("</%s>",name);
            chpr = newline(chpr);
        }
    }
    setcharsprinted(chpr);
    return(max_offset);
}
#undef PRINT_A_BIT


/* Unknown APPn markers; hex dump them and move on.                   */

unsigned long
process_appn(FILE *inptr,unsigned long appn_offset,unsigned short tag,
                struct image_summary *summary_entry,char *parent_name,
                int indent)
{
    char *app_string,*name;
    unsigned short app_length = 0;
    unsigned long dumplength = 0L;
    int chpr = 0;
    int tagwidth = 13;

    if(inptr)
    {
        if(summary_entry)
        {
            summary_entry->filesubformat |= FILESUBFMT_APPN;
            summary_entry->filesubformatAPPN[tag & 0xf] = 1;
        }
        name = tagname(tag);    /* never null                         */
        app_length = read_ushort(inptr,TIFF_MOTOROLA,appn_offset + 2);
        print_tag_address(SECTION,appn_offset,indent,"@");
        if(PRINT_SECTION)
            chpr += printf("<%s> %#x length %u, ",name,tag,app_length);
        app_string = read_appstring(inptr,tag,HERE);
        if(PRINT_SECTION)
            chpr += printf("%s",app_string ? app_string : "NULL ID");
        else if((PRINT_ENTRY))
        {
            /* ###%%% pseudo-tags fpr LIST Mode here */
            print_tag_address(ENTRY,HERE,indent,"*");
            if((PRINT_TAGINFO))
            {
                if((PRINT_LONGNAMES))
                    chpr += printf("%s.",parent_name);
                tagwidth = 13;
                if((tag & 0xf) > 9)
                    --tagwidth;
                chpr += printf("%-*.*s%d",tagwidth,tagwidth,"APP",tag & 0xf);
            }
            if((PRINT_VALUE))
                chpr += printf(" = @%lu:%-9u",appn_offset,app_length);
            chpr = newline(chpr);
        }

        if(PRINT_APPNDUMP)
        {
            if((Max_appdump == DUMPALL) || (Max_appdump > (app_length + 2)))
                dumplength = app_length + 2;
            else if(Max_appdump > 0L)
                dumplength = Max_appdump;
            else
                dumplength = Max_undefined;
            if(dumplength)
            {
                chpr = newline(1);
                hexdump(inptr,appn_offset,app_length + 2,dumplength,16,
                            indent,SMALLINDENT);
                chpr = newline(1);
            }
        }
        else if(PRINT_SECTION)
        {
            chpr += printf(" - unknown format (not dumped: use -A)");
            chpr = newline(chpr);
        }

        print_tag_address(SECTION,appn_offset + app_length + 1,indent,"-");
        if(PRINT_SECTION)
        {
            chpr += printf("</%s>",name);
            chpr = newline(chpr);
        }
        fseek(inptr,appn_offset + app_length,SEEK_SET);
    }
    setcharsprinted(chpr);
    return(appn_offset + app_length + 2);
}

/* Print the string data from an APP12 segment, if hexdump hasn't     */
/* been asked. The "[fileinfo]" stuff is unknown format, but this     */
/* part should be useful.                                             */

unsigned long
process_app12(FILE *inptr,unsigned long app12_offset,unsigned short tag,
                struct image_summary *summary_entry,char *parent_name,
                int indent)
{
    char *app_string,*name;
    unsigned short app_length = 0;
    unsigned long dumplength = 0UL;
    unsigned long offset = 0UL;
    unsigned long end_offset = 0UL;
    unsigned long size = 0UL;
    int chpr = 0;
    int nextch;

    if(inptr)
    {
        if(summary_entry)
        {
            summary_entry->filesubformat |= FILESUBFMT_APPN;
            summary_entry->filesubformatAPPN[12] = 1;
        }
        name = tagname(tag);    /* never null                         */
        app_length = read_ushort(inptr,TIFF_MOTOROLA,app12_offset + 2);
        print_tag_address(SECTION,app12_offset,indent,"@");
        if(PRINT_SECTION)
            chpr += printf("<%s> %#x length %u, ",name,tag,app_length);
        else if((PRINT_ENTRY))
        {
            /* ###%%% pseudo-tags fpr LIST Mode here */
            print_tag_address(ENTRY,HERE,indent,"*");
            if((PRINT_TAGINFO))
            {
                if((PRINT_LONGNAMES))
                    chpr += printf("%s.",parent_name);
                chpr += printf("%-14.14s","APP12");
            }
            if((PRINT_VALUE))
                chpr += printf(" = @%lu:%-9u",app12_offset,app_length);
            chpr = newline(chpr);
        }
        app_string = read_appstring(inptr,tag,HERE);
        if(PRINT_SECTION)
            chpr += printf("\'%s\'",app_string ? app_string : "NULL ID");

        if(PRINT_APPNDUMP)
        {
            if((Max_appdump == DUMPALL) || (Max_appdump > (app_length + 2)))
                dumplength = app_length + 2;
            else if(Max_appdump > 0L)
                dumplength = Max_appdump;
            else
                dumplength = Max_undefined;
            if(dumplength)
            {
                if(PRINT_SECTION)
                    chpr = newline(chpr);
                hexdump(inptr,app12_offset,app_length + 2,dumplength,16,
                            indent + SMALLINDENT,SMALLINDENT);
                chpr = newline(1);
            }
        }
        else if((PRINT_APPENTRY) && (PRINT_ENTRY))
        {
            /* ###%%% in LIST mode, these should get an "APP12." prefix */
            end_offset = app12_offset + app_length + 1;
            offset = ftell(inptr);
            nextch = skip_past_newline(inptr,end_offset);
            offset = ftell(inptr);
            if(PRINT_SECTION)
                chpr = newline(chpr);
            if(offset < end_offset);
                nextch = putword(inptr,nextch,end_offset,indent);
            chpr = newline(chpr);
            offset = ftell(inptr);
            while(nextch && (nextch != EOF) && (offset < end_offset))
            {
                nextch = putword(inptr,nextch,end_offset,indent);
                if(nextch)
                    chpr = newline(chpr);
                offset = ftell(inptr);
            }
            while(offset < end_offset)
            {
                nextch = skip_to_bracket(inptr,end_offset);
                if(nextch == EOF)
                {
                    break;
                }
                size = ftell(inptr) - offset;
                if(nextch == '[')
                    --size;
                if(size > 1)
                {
                    chpr += printf(" length %lu",size);
                    if(Max_undefined)
                    {
                        chpr = newline(chpr);
                        hexdump(inptr,offset,size, Max_undefined,16,
                                    indent + SMALLINDENT,
                                    SMALLINDENT);
                        chpr = newline(1);
                        nextch = fgetc(inptr);
                        if((nextch == EOF) || (ftell(inptr) >= end_offset))
                            break;
                    }
                    else
                    {
                        printred(" (not dumped; use -U)");
                        chpr = newline(chpr);
                    }
                }
                else
                    chpr = newline(chpr);
                if(nextch)
                    nextch = putword(inptr,nextch,end_offset,indent);
                offset = ftell(inptr);
            }
        }

        print_tag_address(SECTION,app12_offset + app_length + 1,indent,"-");
        if(PRINT_SECTION)
        {
            chpr += printf("</%s>",name);
            chpr = newline(chpr);
        }
    }
    setcharsprinted(chpr);
    return(app12_offset + app_length + 2);
}

/* Read and discard until the next newline in the input stream, or    */
/* until 'max_offset' is reached. Return the last character read.     */

int
skip_past_newline(FILE *inptr,unsigned long max_offset)
{
    int ch;
    while(((unsigned long)ftell(inptr) < max_offset) && ((ch = fgetc(inptr)) != '\n') && ch != EOF)
        continue;
    return(fgetc(inptr));
}

/* Read and discard until the next '[' in the input stream, or until  */
/* 'max_offset' is reached. Return the last character read.           */

int
skip_to_bracket(FILE *inptr,unsigned long max_offset)
{
    int ch = EOF;

    while(((unsigned long)ftell(inptr) < max_offset) && ((ch = fgetc(inptr)) != '[') && ch != EOF)
        continue;
    if(ftell(inptr) >= max_offset)
        ch = 0;
    return(ch);
}

/* Print a word from an APP12 segment. The first character of the     */
/* word is passed in by the caller; if it's a left square bracket, it */
/* is assumed to start a "section" and is indented a little less.     */
/* This can print a lone open-bracket, but what the heck.             */

/* CRs are stripped out and nulls end a word. Newlines are stripped   */
/* as well, but cause the next character to be read and passed back   */
/* to the caller, to be used as the start of a new word. Otherwise    */
/* the last character read is returned.                               */

/* The routine is not allowed to read past 'max_offset', which should */
/* be the end of the segment.                                         */

int
putword(FILE *inptr,int firstch,unsigned long max_offset,int indent)
{
    int eol = 0;
    int chpr = 0;
    int ch = firstch;

    if(firstch)
        print_tag_address(APP_ENTRY,ftell(inptr),indent + SMALLINDENT,"@");
    if(firstch)
    {
        if(firstch != '[')
            extraindent(SMALLINDENT);
        putchar(firstch);
        ++chpr;
    }
    else
        ++eol;
    while(((unsigned long)ftell(inptr) < max_offset) && !eol)
    {
        ch = fgetc(inptr);
        switch(ch)
        {
        case '\r':
            break;
        case 0:
            ++eol;
            break;
        case '\n':
            ++eol;
            ch = fgetc(inptr);
            break;
        case EOF:
            ++eol;
            break;
        default:
            if(isprint(ch))
                putchar(ch);
            ++chpr;
            break;
        }
    }
    setcharsprinted(chpr);
    return(ch);
}



/* Determine if the values for an IFD entry are recorded at an        */
/* offset. Returns 0 if the value is recorded directly in the entry,  */
/* 1 if the value is offset.                                          */

int
is_offset(struct ifd_entry *entry_ptr)
{
    if((value_type_size(entry_ptr->value_type) * entry_ptr->count) > 4)
        return(1);
    return(0);
}

/* Process an Epson Print Image Matching section, included in many    */
/* cameras for the purpose of adjusting image parameters when         */
/* printing the image, according to values pre-determined by the      */
/* device manufacturer. According to Epson, parametes which may be    */
/* adjusted include:                                                  */

/* Gamma Level                                                        */
/* Color Space                                                        */
/* Color Balance                                                      */
/* Contrast                                                           */
/* Brightness                                                         */
/* Sharpness                                                          */
/* Saturation                                                         */
/* Shadow                                                             */
/* Highlight                                                          */

/* Pim version 2 also handles:                                        */

/* Noise Reduction                                                    */
/* Custom Scene setting                                               */

/* This routine knows just enough about the structure of PIM to print */
/* tag numbers and values, but interpretation of tags is not known.   */
/* Values are printed in decimal and hex.                             */

unsigned long
process_pim(FILE *inptr,unsigned short byteorder,unsigned long pim_offset,
                    unsigned long fileoffset_base, unsigned long count,
                    char *tagname,char *dirname,char *prefix,
                    int indent)
{
    unsigned long entry_offset,extra,value;
    unsigned long max_offset = 0L;
    unsigned long max_value_offset = 0L;
    unsigned char *pim_id,*pim_version,*valueptr;
    unsigned short tag;
    int tagwidth = PIMTAGWIDTH;
    int chpr = 0;
    int entry_num,num_entries;

    if(inptr == (FILE *)0)
    {
        fprintf(stderr,"%s: no open file pointer to read Print Image data\n",
                Progname);
        max_offset = 0L;
        return(0L);
    }

    entry_offset = pim_offset + fileoffset_base;
    max_offset = entry_offset + count;

    /* Always starts with "PrintIM\0"; read it as a sanity check      */
    pim_id = read_bytes(inptr,8,entry_offset);
    if((pim_id == (unsigned char *)0) || strncasecmp((char *)pim_id,"PrintIM",8) != 0)
    {
        /* Print the value as a comment here, since we are processing */
        /* no further.                                                */
        if(PRINT_SECTION)
        {
            print_tag_address(SECTION,entry_offset,indent,prefix);
            chpr += printf("%s",tagname);
            if((PRINT_VALUE))
                chpr += printf(": length %lu",count);
        }
        if(!(PRINT_SECTION))
        {
            chpr = newline(chpr);
            printred("# WARNING:");
        }
        printred(" INVALID PrintImage data...");
        return(0L);
    }
    chpr = newline(0);

    pim_version = read_bytes(inptr,6,HERE);
    num_entries = read_ushort(inptr,byteorder,HERE);

    if(PRINT_SECTION)
    {
        print_tag_address(SECTION,entry_offset,indent,prefix);
        chpr += printf("<%s> Version %s, size %lu, %d entries",tagname,pim_version,
                                                                count,num_entries);
        if((num_entries * 6) > (count - 16))
        {
            chpr = newline(chpr);
            num_entries = (count - 16) / 6;
            PUSHCOLOR(HI_RED);
            chpr += printf("# WARNING: PIM claims too many entries for section size, printing %d",num_entries);
            POPCOLOR();
        }
    }
    else
    {

        /* Make pseudo-tags for these                                 */
        if((PRINT_TAGINFO) || (PRINT_VALUE))
            print_tag_address(ENTRY,HERE,indent+SMALLINDENT,"*");
        if(PRINT_TAGINFO)
        {
            if((PRINT_LONGNAMES))
                chpr += printf("%s.",dirname);
            chpr += printf("%s.",tagname);
            chpr += printf("%-*.*s",tagwidth,tagwidth,"Offset");
        }
        if((PRINT_VALUE))
            chpr += printf(" = @%lu",entry_offset);
        chpr = newline(chpr);
        if((PRINT_TAGINFO) || (PRINT_VALUE))
            print_tag_address(ENTRY,HERE,indent+SMALLINDENT,"*");
        if(PRINT_TAGINFO)
        {
            if((PRINT_LONGNAMES))
                chpr += printf("%s.",dirname);
            chpr += printf("%s.",tagname);
            chpr += printf("%-*.*s",tagwidth,tagwidth,"Length");
        }
        if((PRINT_VALUE))
            chpr += printf(" = %lu",count);
        chpr = newline(chpr);
        if((PRINT_TAGINFO) || (PRINT_VALUE))
            print_tag_address(ENTRY,HERE,indent+SMALLINDENT,"*");
        if(PRINT_TAGINFO)
        {
            if((PRINT_LONGNAMES))
                chpr += printf("%s.",dirname);
            chpr += printf("%s.",tagname);
            chpr += printf("%-*.*s",tagwidth,tagwidth,"Version");
        }
        if((PRINT_VALUE))
            chpr += printf(" = %s",pim_version);
        chpr = newline(chpr);
        if((num_entries * 6) > (count - 16))
        {
            chpr = newline(chpr);
            chpr += printf("# WARNING: PIM claims too many entries (%d) for section size; ",num_entries);
            num_entries = (count - 16) / 6;
            PUSHCOLOR(HI_RED);
            chpr += printf("printing %d",num_entries);
            POPCOLOR();
        }
    }
    
    max_value_offset = entry_offset = ftell(inptr);

    indent += SMALLINDENT;
    for(entry_num = 0; entry_num < num_entries; ++entry_num)
    {
        int i;

        chpr = newline(chpr);
        tag = read_ushort(inptr,byteorder,HERE);
        if(ferror(inptr) || feof(inptr))
            goto blewit;
        if((PRINT_ENTRY))
        {
            print_tag_address(ENTRY,entry_offset,indent,prefix);
            if((PRINT_TAGINFO))
            {
                if((PRINT_LONGNAMES))
                    chpr += printf("%s.%s.",dirname,tagname);
                else if((LIST_MODE))
                    chpr += printf("%s.",tagname);
                chpr += printf("PIM_%#06X",(int)tag);
                if((PRINT_VALUE))
                {
                    putindent(tagwidth - 10);
                    chpr += printf(" = ");
                }
            }
        }
        valueptr = read_bytes(inptr,4,HERE);
        value = read_ulong(inptr,byteorder,entry_offset + 2);

        if(PRINT_SECTION)
        {
            if((PRINT_VALUE))
            {
                for(i = 0; i < 4; ++i)
                    chpr += printf("%02x ",(unsigned int)(valueptr[i] & 0xff));
                chpr += printf(" |");
                for(i = 0; i < 4; ++i)
                {
                    if(isprint(valueptr[i]))
                        putchar(valueptr[i]);
                    else
                        putchar('.');  /* can't win 'em all  */
                }
                chpr += printf("| = %#lx/%lu",value,value);
            }
        }
        else if((PRINT_VALUE))
            chpr += printf("%#lx/%lu",value,value);

        max_value_offset = entry_offset = ftell(inptr);
    }

    if(PRINT_SECTION)
    {
        chpr = newline(chpr);
        if((max_value_offset < max_offset) && PRINT_VALUE_AT_OFFSET)
        {
            int printlength = 0;

            print_tag_address(SECTION,max_value_offset,indent,"*");
            PUSHCOLOR(RED);
            chpr += printf("---- End of values before end of %s section",tagname);
            POPCOLOR();
            chpr = newline(chpr);
            extra = max_offset - max_value_offset;
            /* dump the extra as UNDEFINED                            */
            if(Max_undefined)
            {
                if((Max_undefined == DUMPALL) || (Max_undefined > extra))
                    printlength = extra;
                else if(Max_undefined > 0L)
                    printlength = Max_undefined;
                hexdump(inptr,max_value_offset,extra,printlength,6,indent,
                                                    SMALLINDENT+8);
            }
            chpr = newline(chpr);
        }
        indent -= SMALLINDENT;
        print_tag_address(SECTION,max_offset - 1,indent,"-");
        chpr += printf("</%s>",tagname);
    }

    setcharsprinted(chpr);
    return(max_value_offset - 1);
blewit:
    clearerr(inptr);
    return(0L);
}