summaryrefslogtreecommitdiff
path: root/samples/source/common/DumpFile.cpp
blob: dba55dd6abc0d247e06fa2a2353d8fe2dcfa602a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
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
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
// =================================================================================================
// Copyright 2008 Adobe Systems Incorporated
// All Rights Reserved. 
//
// NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the terms
// of the Adobe license agreement accompanying it.
//
// =================================================================================================
//
// DumpFile is suitable for both dumping an entire file structure to screen _as well as_ access to 
// specific to specific legacy fields (as much required for auto-testing). 
// 
//  Currently supports
//     - JPEG
//     - TIFF 
//     - PHOTOSHOP
//     - JPEG2K
//     - WMAV (ASF/WMA/WMV)
//     - IFF/RIFF (AVI/WAV/RF64/AIFF/AIFF-C)
//     - PNG
//     - InDesign
//     - MP3
//     - MOV (Quicktime)
//     - UCF (done, including commented zips and zip64 (>4GB))
//     - SWF
//     - FLV
//     - MPEG-4
//
// DumpFile does depend on XMPCore and the packetscanner from XMPFiles. 


#include <stdarg.h>

#include "source/ExpatAdapter.hpp"

#include "samples/source/common/globals.h"
#include "samples/source/common/DumpFile.h"
#include "samples/source/common/LargeFileAccess.hpp"
static const XMP_Uns32 CONST_INFINITE = (XMP_Uns32)(-1);

// converts a (supposed) 8Bit-encoded String to Buginese
// - suitable for UTF-8 or any other encoding
// - stopOnNull does just that, exceeding length is declared as |R:1234 at the end of string
// - len is the max parsing size, defaults to unlimited
// - having set neither stopOnNull nor max parsing size is a bad idea (checked and yields error)
std::string convert8Bit(void* str, bool stopOnNull, XMP_Uns32 byteLen)
{
	std::string r;	//result
	r.clear();	// ...I have become cautious... :-)

	if (byteLen == 0)
		return r; //nothing to do

				  //provoke access violation:
				  //if invalid length leads to access violation, I want it here and now...
	if (byteLen != CONST_INFINITE) { //if not "CONST_INFINITE"
		char tmp = ((char*)str)[byteLen - 1];
	}
	if (!stopOnNull && (byteLen == CONST_INFINITE))
		Log::error("must set either stopOnNULL or specify length of string");

	bool outside = false;	// toggle-flag: outside ASCII ?
	XMP_Uns32 remainder = 0;
	char buffer[200];  //changed from 20 to 200 (whatever reason there was to have it so small)

	for (XMP_Uns32 i = 0;
		i<byteLen;  //either byteLen==0 or run forever (read: till 'break')
		i++)
	{
		XMP_Uns8 ch = ((char*)str)[i];
		if ((0x20 <= ch) && (ch <= 0x7E))
		{	//outside-case
			if (outside)
				r += ">";
			r += ch;
			outside = false;
		}
		else {
			if (!outside)
				r += "<";	//first inside-case
			else
				r += " ";
			sprintf(buffer, "%.2X", ch);
			r += buffer;
			outside = true;
		}

		if (stopOnNull && (ch == 0)) {
			if (byteLen != CONST_INFINITE) remainder = byteLen - i - 2;
			break;
		}
	}

	if (outside) r += ">";
	if (remainder>0) {
		sprintf(buffer, "|R:%d", remainder);
		r += buffer;
	}
	return r;
}


//same story, but for (UTF-)16BE characters
//note: length still to be specified in byte, thus must be even (verified)
std::string convert16Bit(bool bigEndian, XMP_Uns8* str, bool stopOnNull, XMP_Uns32 byteLen)
{
	//if invalid length leads to access violation, I want it here and now...
	if (byteLen != CONST_INFINITE) {
		char tmp = str[byteLen - 1];
	}

	if (!stopOnNull && (byteLen == CONST_INFINITE))
		Log::error("must set either stopOnNULL or specify length of string");
	if ((byteLen != CONST_INFINITE) && (byteLen % 2 != 0))  //if neither CONST_INFINITE or even...#
		Log::error("convert16BitToBuginese: byteLen must be even");


	bool outside = false;	// toggle-flag: outside ASCII ?
	XMP_Uns32 remainder = 0;
	char buffer[20];

	std::string r;	//result
	r.clear();	// ...I have become cautious... :-)

	for (XMP_Uns32 i = 0;
		i<byteLen;  //either byteLen==0 or run forever (read: till 'break')
		i = i + 2)
	{
		XMP_Uns16 ch = (bigEndian) ? GetUns16BE(&str[i]) : GetUns16LE(&str[i]);

		if ((0x20 <= ch) && (ch <= 0x7E))
		{	//outside-case
			if (outside)
				r += ">";
			r += (char)ch;
			outside = false;
		}
		else {
			if (!outside)
				r += "<";	//first inside-case
			else
				r += " ";
			//I want to reflect actual byte order when dumping, thus byte-wise here and no endian-fork
			sprintf(buffer, "%.2X %.2X", str[i], str[i + 1]);
			r += buffer;
			outside = true;
		}

		if (stopOnNull && (ch == 0)) {
			if (byteLen != CONST_INFINITE) remainder = byteLen - i - 2;
			break;
		}
	}

	if (outside) r += ">";
	if (remainder>0) {
		sprintf(buffer, "|R:%d", remainder);
		r += buffer;
	}
	return r;
}


std::string fromArgs(const char* format, ...)
{
	//note: format and ... are somehow "used up", i.e. dumping them 
	//      via vsprintf _and_ via printf brought up errors on Mac (only)
	//      i.e. %d %X stuff looking odd (roughly like signed vs unsigned...)
	//      buffer reuse is fine, just dont use format/... twice.

	char buffer[4096];  //should be big enough but no guarantees..
	va_list args;
	va_start(args, format);
	vsprintf(buffer, format, args);
	va_end(args);

	return std::string(buffer);
}

#include <iostream>
#include <stdarg.h>	//fpr va_list et al

DumpFileException::DumpFileException(const char *format, ...) : std::runtime_error("dumpfile exception")
{
	va_list args; va_start(args, format);
	vsnprintf(buffer, XMPQE_MAX_ERROR_LENGTH, format, args);
	va_end(args);
}

// overriding, since the buffer needs to be constructed first...
const char* DumpFileException::what_dumpfile_reason()
{
	return buffer;
}

// REMOVED ON PURPOSE: #include <assert.h>
// define two assert macros, /w and w/o msg
// (we don't want to slip malformed files through. Neither in release mode.)
#undef assertMsg
#undef assert

//TODO: integrate file pos in parse failure description (LFA_Tell ()...)

// this method just 
#define assertNoThrowMsg(msg,c)                                                             \
try { c }                                                                                   \
catch ( ... ) {                                                                             \
	throw DumpFileException( "- assert:   %s\n- message:  %s\n- location: " __FILE__ ":%u", \
                            #c, std::string( msg ).c_str(), __LINE__ );                     \
}

#define assertMsg(msg,c)																				\
if ( ! (c) ) {																							\
	throw DumpFileException( "- assert:   %s\n- message:  %s\n- location: " __FILE__ ":%u", #c, std::string( msg ).c_str(), __LINE__ );		\
}

#define assert(c)																				\
if ( ! (c) ) {																					\
	throw DumpFileException( "- assert:   %s\n- location: " __FILE__ ":%u", #c, __LINE__ );		\
}

#define assertEOF(file)		\
if ( ! LFA_isEof(file) ) {	\
	throw DumpFileException( "- assert:   feof(file)\n- message:  end of file not reached, still at 0x%X\n- location: " __FILE__ ":%u", LFA_Tell (file), __LINE__ );		\
}

#define fail(msg)																				\
	throw DumpFileException( "- failure\n- message:  %s\n- location: " __FILE__ ":%u", std::string( msg ).c_str(), __LINE__ );

using namespace std;

//XMPCore related
//! no use of XMPFiles
//! no "XMP.incl_cpp" here, happens in Dumpfile/main.cpp resp. CppUnit/main.cpp
#define TXMP_STRING_TYPE std::string
#include "public/include/XMP.hpp"
#include "public/include/XMP_Const.h"

#include "samples/source/common/XMPScanner.hpp"
#include "samples/source/common/Log.h"
//disabled warning (take-over)
#if XMP_WinBuild
#pragma warning (disable : 4996)	// '...' was declared deprecated
#pragma warning (disable : 4244)	// conversion from '__w64 int' to 'XMP_Uns32', possible loss of data
#pragma warning (disable : 4267)	// conversion from 'size_t' to 'int', possible loss of data
#endif

#pragma pack (1)

//the tag tree to be build up, 
//  then dumped (dumpfile.exe) resp.
//  resp. queried (testrunner)
static TagTree* tree;

// specifc 'state machine' for QT/MPEG4 dumping
// * false by default (set in DumpISO() stub)
static bool TimeCodeTrack;

// =================================================================================================

long kOne = 1;
char firstByte = *((char*)&kOne);

const bool sBigEndianHost = (firstByte == 0);
const bool sLittleEndianHost = (firstByte == 1);
static bool beTIFF;

typedef const char * ChPtr;
#define CheckBytes(left,right,len)	(memcmp (((ChPtr)(left)), ((ChPtr)(right)), len) == 0)

static XMP_Uns8* sDataPtr = 0;	// Used via CaptureFileData for variable length data.
static XMP_Uns32 sDataMax = 0;
static XMP_Uns32 sDataLen = 0;

// storing XMP Info 'globally' for a later dump...
static XMP_Uns8* sXMPPtr = 0;	// Used via CaptureXMP for the main XMP.
static XMP_Uns32 sXMPMax = 0;
static XMP_Uns32 sXMPLen = 0;
static XMP_Int64 sXMPPos = 0;

typedef XMP_Uns16(*GetUns16_Proc) (const void * addr);
typedef XMP_Uns32(*GetUns32_Proc) (const void * addr);
typedef XMP_Uns64(*GetUns64_Proc) (const void * addr);

static XMP_Uns16 GetUns16BE(const void * addr);
static XMP_Uns16 GetUns16LE(const void * addr);
static XMP_Uns32 GetUns32BE(const void * addr);
static XMP_Uns32 GetUns32LE(const void * addr);
static XMP_Uns64 GetUns64BE(const void * addr);
static XMP_Uns64 GetUns64LE(const void * addr);

#define High32(u64) ((XMP_Uns32)((u64) >> 32))
#define Low32(u64)  ((XMP_Uns32)((u64) & 0xFFFFFFFFUL))

// =================================================================================================

// ahead declarations
struct JpegMarker {
	XMP_Uns8 * jpegMarkerPtr;
	XMP_Uns16 jpegMarkerLen;
};
typedef std::vector<JpegMarker> JpegMarkers;

static void DumpTIFF(XMP_Uns8 * tiffContent, XMP_Uns32 tiffLen, XMP_Uns32 fileOffset, const char * label, std::string path, bool isHeaderAbsent = false);
static void DumpTIFF(const JpegMarkers& psirMarkers, XMP_Uns8 * dataStart, const char * label, std::string path);
static void DumpIPTC(XMP_Uns8 * iptcOrigin, XMP_Uns32 iptcLen, XMP_Uns32 fileOffset, const char * label);
static void DumpImageResources(XMP_Uns8 * psirOrigin, XMP_Uns32 psirLen, XMP_Uns32 fileOffset, const char * label);
static void DumpImageResources(const JpegMarkers& psirMarkers, XMP_Uns8 * dataStart, const char * label);
static void DumpIFDChain(XMP_Uns8 * startPtr, XMP_Uns8 * endPtr, XMP_Uns8 * tiffContent, XMP_Uns32 fileOffset, const char * label, std::string path, bool isHeaderAbsent = false);

// =================================================================================================

static GetUns16_Proc TIFF_GetUns16 = 0;	// Keeps endian procs for the "current" TIFF.
static GetUns32_Proc TIFF_GetUns32 = 0;
static GetUns64_Proc TIFF_GetUns64 = 0;

enum {	// Special TIFF tags
	kTIFF_XMP = 700,
	kTIFF_IPTC = 33723,
	kTIFF_PSIR = 34377,
	kTIFF_Exif = 34665,
	kTIFF_GPS = 34853,
	kTIFF_MakerNote = 37500,
	kTIFF_Interop = 40965
};

enum {	// Special Photoshop image resource IDs
	kPSIR_OldCaption = 1008,
	kPSIR_PrintCaption = 1020,
	kPSIR_IPTC = 1028,
	kPSIR_CopyrightFlag = 1034,
	kPSIR_CopyrightURL = 1035,
	kPSIR_Exif_1 = 1058,
	kPSIR_Exif_3 = 1059,
	kPSIR_XMP = 1060,
	kPSIR_IPTC_Digest = 1061
};

struct IPTC_DataSet {	// The 5 byte header of an IPTC DataSet.
	XMP_Uns8 tagMarker;
	XMP_Uns8 recordNumber;
	XMP_Uns8 dataSetNumber;
	XMP_Uns8 octetCountHigh;
	XMP_Uns8 octetCountLow;
};

enum {	// IPTC DataSet IDs
	kIPTC_IntellectualGenre = 4,
	kIPTC_Title = 5,
	kIPTC_Urgency = 10,
	kIPTC_SubjectCode = 12,
	kIPTC_Category = 15,
	kIPTC_SuppCategory = 20,
	kIPTC_Keyword = 25,
	kIPTC_Instructions = 40,
	kIPTC_DateCreated = 55,
	kIPTC_TimeCreated = 60,
	kIPTC_DigitalCreationDate = 62,
	kIPTC_DigitalCreationTime = 63,
	kIPTC_Creator = 80,
	kIPTC_CreatorJobtitle = 85,
	kIPTC_City = 90,
	kIPTC_Location = 92,
	kIPTC_State = 95,
	kIPTC_CountryCode = 100,
	kIPTC_Country = 101,
	kIPTC_JobID = 103,
	kIPTC_Headline = 105,
	kIPTC_Provider = 110,
	kIPTC_Source = 115,
	kIPTC_CopyrightNotice = 116,
	kIPTC_Description = 120,
	kIPTC_DescriptionWriter = 122
};

struct DataSetInfo {
	XMP_Uns8 id;
	const char * name;
};

static const DataSetInfo kDataSetNames[] =
{ { kIPTC_IntellectualGenre, "Intellectual Genre" },
{ kIPTC_Title, "Title" },
{ kIPTC_Urgency, "Urgency" },
{ kIPTC_SubjectCode, "Subject Code" },
{ kIPTC_Category, "Category" },
{ kIPTC_SuppCategory, "Supplemental Category" },
{ kIPTC_Keyword, "Keyword" },
{ kIPTC_Instructions, "Instructions" },
{ kIPTC_DateCreated, "Date Created" },
{ kIPTC_TimeCreated, "Time Created" },
{ kIPTC_DigitalCreationDate, "Digital Creation Date" },
{ kIPTC_DigitalCreationTime, "Digital Creation Time" },
{ kIPTC_Creator, "Creator" },
{ kIPTC_CreatorJobtitle, "Creator Jobtitle" },
{ kIPTC_City, "City" },
{ kIPTC_Location, "Location" },
{ kIPTC_State, "Province-State" },
{ kIPTC_CountryCode, "Country Code" },
{ kIPTC_Country, "Country" },
{ kIPTC_JobID, "Job ID" },
{ kIPTC_Headline, "Headline" },
{ kIPTC_Provider, "Provider" },
{ kIPTC_Source, "Source" },
{ kIPTC_CopyrightNotice, "Copyright Notice" },
{ kIPTC_Description, "Description" },
{ kIPTC_DescriptionWriter, "Description Writer" },
{ 0, 0 } };

enum {
	kTIFF_Uns8 = 1,
	kTIFF_ASCII = 2,
	kTIFF_Uns16 = 3,
	kTIFF_Uns32 = 4,
	kTIFF_URational = 5,
	kTIFF_Int8 = 6,
	kTIFF_Undef8 = 7,
	kTIFF_Int16 = 8,
	kTIFF_Int32 = 9,
	kTIFF_SRational = 10,
	kTIFF_Float = 11,
	kTIFF_Double = 12,
	kTIFF_IFD = 13,
	kTIFF_TypeEnd = kTIFF_IFD
};

static const int sTIFF_TypeSizes[] = { 0, 1, 1, 2, 4, 8, 1, 1, 2, 4, 8, 4, 8, 4 };
static const char * sTIFF_TypeNames[] = { "", "BYTE", "ASCII", "SHORT", "LONG", "RATIONAL",
"SBYTE", "UNDEFINED", "SSHORT", "SLONG", "SRATIONAL",
"FLOAT", "DOUBLE" };

struct TagNameInfo {
	long	tag;
	const char * name;
};

static const TagNameInfo sTIFF_TagNames[] =
{ { 256, "ImageWidth" },
{ 257, "ImageLength" },
{ 258, "BitsPerSample" },
{ 259, "Compression" },
{ 262, "PhotometricInterpretation" },
{ 270, "ImageDescription" },
{ 271, "Make" },
{ 272, "Model" },
{ 274, "Orientation" },
{ 282, "XResolution" },
{ 283, "YResolution" },
{ 284, "PlanarConfiguration" },
{ 296, "ResolutionUnit" },
{ 301, "TransferFunction" },
{ 305, "Software" },
{ 306, "DateTime" },
{ 315, "Artist" },
{ 318, "WhitePoint" },
{ 319, "PrimaryChromaticities" },
{ 529, "YCbCrCoefficients" },
{ 530, "YCbCrSubSampling" },
{ 531, "YCbCrPositioning" },
{ 532, "ReferenceBlackWhite" },
{ 33432, "Copyright" },
{ 33434, "ExposureTime" },
{ 33437, "FNumber" },
{ 34850, "ExposureProgram" },
{ 34852, "SpectralSensitivity" },
{ 34855, "ISOSpeedRatings" },
{ 34856, "OECF" },
{ 36864, "ExifVersion" },
{ 36867, "DateTimeOriginal" },
{ 36868, "DateTimeDigitized" },
{ 37121, "ComponentsConfiguration" },
{ 37122, "CompressedBitsPerPixel" },
{ 37377, "ShutterSpeedValue" },
{ 37378, "ApertureValue" },
{ 37379, "BrightnessValue" },
{ 37380, "ExposureBiasValue" },
{ 37381, "MaxApertureValue" },
{ 37382, "SubjectDistance" },
{ 37383, "MeteringMode" },
{ 37384, "LightSource" },
{ 37385, "Flash" },
{ 37386, "FocalLength" },
{ 37396, "SubjectArea" },
{ 37500, "MakerNote" },
{ 37510, "UserComment" },
{ 37520, "SubSecTime" },
{ 37521, "SubSecTimeOriginal" },
{ 37522, "SubSecTimeDigitized" },
{ 40960, "FlashpixVersion" },
{ 40961, "ColorSpace" },
{ 40962, "PixelXDimension" },
{ 40963, "PixelYDimension" },
{ 40964, "RelatedSoundFile" },
{ 41483, "FlashEnergy" },
{ 41484, "SpatialFrequencyResponse" },
{ 41486, "FocalPlaneXResolution" },
{ 41487, "FocalPlaneYResolution" },
{ 41488, "FocalPlaneResolutionUnit" },
{ 41492, "SubjectLocation" },
{ 41493, "ExposureIndex" },
{ 41495, "SensingMethod" },
{ 41728, "FileSource" },
{ 41729, "SceneType" },
{ 41730, "CFAPattern" },
{ 41985, "CustomRendered" },
{ 41986, "ExposureMode" },
{ 41987, "WhiteBalance" },
{ 41988, "DigitalZoomRatio" },
{ 41989, "FocalLengthIn35mmFilm" },
{ 41990, "SceneCaptureType" },
{ 41991, "GainControl" },
{ 41992, "Contrast" },
{ 41993, "Saturation" },
{ 41994, "Sharpness" },
{ 41995, "DeviceSettingDescription" },
{ 41996, "SubjectDistanceRange" },
{ 42016, "ImageUniqueID" },
{ 50706, "DNGVersion" },
{ 50707, "DNGBackwardVersion" },
{ 50708, "DNG UniqueCameraModel" },
{ 0, "" } };

// =================================================================================================

struct ASF_GUID {

	XMP_Uns32 part1;	// Written little endian.
	XMP_Uns16 part2;	// Written little endian.
	XMP_Uns16 part3;	// Written little endian.
	XMP_Uns16 part4;	// Written big endian.
	XMP_Uns8  part5[6];	// Written in order.

	ASF_GUID() {};
	ASF_GUID(XMP_Uns32 p1, XMP_Uns16 p2, XMP_Uns16 p3, XMP_Uns16 p4, const void* p5)
	{
		part1 = GetUns32LE(&p1);
		part2 = GetUns16LE(&p2);
		part3 = GetUns16LE(&p3);
		part4 = GetUns16BE(&p4);
		memcpy(&part5, p5, 6);
	};

};

enum {	// Objects for which we have special knowledge.
	kASFObj_Unknown = 0,
	kASFObj_Header,			// Special top level objects.
	kASFObj_Data,
	kASFObj_XMP,
	kASFObj_FileProperties,	// Children of the Header Object.
	kASFObj_ContentDesc,
	kASFObj_ContentBrand,
	kASFObj_ContentEncrypt,
	kASFObj_HeaderExtension,
	kASFObj_Padding

};

static const ASF_GUID kASF_HeaderGUID(0x75B22630, 0x668E, 0x11CF, 0xA6D9, "\x00\xAA\x00\x62\xCE\x6C");

struct ASF_ObjectInfo {
	ASF_GUID     guid;
	const char * name;
	XMP_Uns8     kind;
};

static const ASF_ObjectInfo kASF_KnownObjects[] =
{
	{ ASF_GUID(0x75B22630, 0x668E, 0x11CF, 0xA6D9, "\x00\xAA\x00\x62\xCE\x6C"), "Header", kASFObj_Header },
{ ASF_GUID(0x75B22636, 0x668E, 0x11CF, 0xA6D9, "\x00\xAA\x00\x62\xCE\x6C"), "Data", kASFObj_Data },
{ ASF_GUID(0xBE7ACFCB, 0x97A9, 0x42E8, 0x9C71, "\x99\x94\x91\xE3\xAF\xAC"), "XMP", kASFObj_XMP },

{ ASF_GUID(0x33000890, 0xE5B1, 0x11CF, 0x89F4, "\x00\xA0\xC9\x03\x49\xCB"), "Simple_Index", 0 },
{ ASF_GUID(0xD6E229D3, 0x35DA, 0x11D1, 0x9034, "\x00\xA0\xC9\x03\x49\xBE"), "Index", 0 },
{ ASF_GUID(0xFEB103F8, 0x12AD, 0x4C64, 0x840F, "\x2A\x1D\x2F\x7A\xD4\x8C"), "Media_Object_Index", 0 },
{ ASF_GUID(0x3CB73FD0, 0x0C4A, 0x4803, 0x953D, "\xED\xF7\xB6\x22\x8F\x0C"), "Timecode_Index", 0 },

{ ASF_GUID(0x8CABDCA1, 0xA947, 0x11CF, 0x8EE4, "\x00\xC0\x0C\x20\x53\x65"), "File_Properties", kASFObj_FileProperties },
{ ASF_GUID(0xB7DC0791, 0xA9B7, 0x11CF, 0x8EE6, "\x00\xC0\x0C\x20\x53\x65"), "Stream_Properties", 0 },
{ ASF_GUID(0x5FBF03B5, 0xA92E, 0x11CF, 0x8EE3, "\x00\xC0\x0C\x20\x53\x65"), "Header_Extension", kASFObj_HeaderExtension },
{ ASF_GUID(0x86D15240, 0x311D, 0x11D0, 0xA3A4, "\x00\xA0\xC9\x03\x48\xF6"), "Codec_List", 0 },
{ ASF_GUID(0x1EFB1A30, 0x0B62, 0x11D0, 0xA39B, "\x00\xA0\xC9\x03\x48\xF6"), "Script_Command", 0 },
{ ASF_GUID(0xF487CD01, 0xA951, 0x11CF, 0x8EE6, "\x00\xC0\x0C\x20\x53\x65"), "Marker", 0 },
{ ASF_GUID(0xD6E229DC, 0x35DA, 0x11D1, 0x9034, "\x00\xA0\xC9\x03\x49\xBE"), "Bitrate_Mutual_Exclusion", 0 },
{ ASF_GUID(0x75B22635, 0x668E, 0x11CF, 0xA6D9, "\x00\xAA\x00\x62\xCE\x6C"), "Error_Correction", 0 },
{ ASF_GUID(0x75B22633, 0x668E, 0x11CF, 0xA6D9, "\x00\xAA\x00\x62\xCE\x6C"), "Content_Description", kASFObj_ContentDesc },
{ ASF_GUID(0xD2D0A440, 0xE307, 0x11D2, 0x97F0, "\x00\xA0\xC9\x5E\xA8\x50"), "Extended_Content_Description", 0 },
{ ASF_GUID(0x2211B3FA, 0xBD23, 0x11D2, 0xB4B7, "\x00\xA0\xC9\x55\xFC\x6E"), "Content_Branding", kASFObj_ContentBrand },
{ ASF_GUID(0x7BF875CE, 0x468D, 0x11D1, 0x8D82, "\x00\x60\x97\xC9\xA2\xB2"), "Stream_Bitrate_Properties", 0 },
{ ASF_GUID(0x2211B3FB, 0xBD23, 0x11D2, 0xB4B7, "\x00\xA0\xC9\x55\xFC\x6E"), "Content_Encryption", kASFObj_ContentEncrypt },
{ ASF_GUID(0x298AE614, 0x2622, 0x4C17, 0xB935, "\xDA\xE0\x7E\xE9\x28\x9C"), "Extended_Content_Encryption", 0 },
{ ASF_GUID(0x2211B3FC, 0xBD23, 0x11D2, 0xB4B7, "\x00\xA0\xC9\x55\xFC\x6E"), "Digital_Signature", 0 },
{ ASF_GUID(0x1806D474, 0xCADF, 0x4509, 0xA4BA, "\x9A\xAB\xCB\x96\xAA\xE8"), "Padding", kASFObj_Padding },

{ ASF_GUID(0x14E6A5CB, 0xC672, 0x4332, 0x8399, "\xA9\x69\x52\x06\x5B\x5A"), "Extended_Stream_Properties", 0 },
{ ASF_GUID(0xA08649CF, 0x4775, 0x4670, 0x8A16, "\x6E\x35\x35\x75\x66\xCD"), "Advanced_Mutual_Exclusion", 0 },
{ ASF_GUID(0xD1465A40, 0x5A79, 0x4338, 0xB71B, "\xE3\x6B\x8F\xD6\xC2\x49"), "Group_Mutual_Exclusion", 0 },
{ ASF_GUID(0xD4FED15B, 0x88D3, 0x454F, 0x81F0, "\xED\x5C\x45\x99\x9E\x24"), "Stream_Prioritization", 0 },
{ ASF_GUID(0xA69609E6, 0x517B, 0x11D2, 0xB6AF, "\x00\xC0\x4F\xD9\x08\xE9"), "Bandwidth_Sharing", 0 },
{ ASF_GUID(0x7C4346A9, 0xEFE0, 0x4BFC, 0xB229, "\x39\x3E\xDE\x41\x5C\x85"), "Language_List", 0 },
{ ASF_GUID(0xC5F8CBEA, 0x5BAF, 0x4877, 0x8467, "\xAA\x8C\x44\xFA\x4C\xCA"), "Metadata", 0 },
{ ASF_GUID(0x44231C94, 0x9498, 0x49D1, 0xA141, "\x1D\x13\x4E\x45\x70\x54"), "Metadata_Library", 0 },
{ ASF_GUID(0xD6E229DF, 0x35DA, 0x11D1, 0x9034, "\x00\xA0\xC9\x03\x49\xBE"), "Index_Parameters", 0 },
{ ASF_GUID(0x6B203BAD, 0x3F11, 0x48E4, 0xACA8, "\xD7\x61\x3D\xE2\xCF\xA7"), "Media_Object_Index_Parameters", 0 },
{ ASF_GUID(0xF55E496D, 0x9797, 0x4B5D, 0x8C8B, "\x60\x4D\xFE\x9B\xFB\x24"), "Timecode_Index_Parameters", 0 },
{ ASF_GUID(0x75B22630, 0x668E, 0x11CF, 0xA6D9, "\x00\xAA\x00\x62\xCE\x6C"), "Compatibility", 0 },
{ ASF_GUID(0x43058533, 0x6981, 0x49E6, 0x9B74, "\xAD\x12\xCB\x86\xD5\x8C"), "Advanced_Content_Encryption", 0 },

{ ASF_GUID(0x00000000, 0x0000, 0x0000, 0x0000, "\x00\x00\x00\x00\x00\x00"), 0, 0 }
};

struct ASF_ObjHeader {
	ASF_GUID  guid;
	XMP_Int64 size;
};

struct ASF_FileProperties {
	ASF_GUID  guid;
	XMP_Int64 size;
	ASF_GUID  fileID;
	XMP_Int64 fileSize;
	XMP_Int64 creationDate;	// Number of 100-nanosecond intervals since January 1, 1601.
	XMP_Int64 dataPacketsCount;
	XMP_Int64 playDuration;
	XMP_Int64 sendDuration;
	XMP_Int64 preroll;
	XMP_Uns32 flags;	// The Broadcast flag is bit 0 (lsb).
	XMP_Uns32 minDataPacketSize;
	XMP_Uns32 maxDataPacketSize;
	XMP_Uns32 maxBitrate;
};
#define kASF_FilePropertiesSize	(16 + 8 + 16 + 6*8 + 4*4)

struct ASF_ContentDescription {
	ASF_GUID  guid;
	XMP_Int64 size;
	XMP_Uns16 titleLen;
	XMP_Uns16 authorLen;
	XMP_Uns16 copyrightLen;
	XMP_Uns16 descriptionLen;
	XMP_Uns16 ratingLen;
	// Little endian UTF-16 strings follow, no BOM, possible nul terminator.
};
#define kASF_ContentDescriptionSize	(16 + 8 + 5*2)

#if 0	// ! Has embedded variable length fields!
struct ASF_ContentBranding {
	ASF_GUID  guid;
	XMP_Int64 size;
	XMP_Uns32 bannerType;
	XMP_Uns32 bannerDataSize;
	// The banner data is here.
	XMP_Uns32 bannerURLSize;
	// The banner URL string is here, an ASCII string.
	XMP_Uns32 copyrightURLSize;
	// The copyright URL string is here, an ASCII string.
};
#endif

#if 0	// ! Has embedded variable length fields!
struct ASF_ContentEncryption {
	ASF_GUID  guid;
	XMP_Int64 size;
	XMP_Uns32 secretDataSize;
	// The secret data is here.
	XMP_Uns32 protectionTypeSize;
	// The protection type is here, an ASCII string.
	XMP_Uns32 keyIDSize;
	// The key ID is here, an ASCII string.
	XMP_Uns32 licenseURLSize;
	// The licensed URL is here, an ASCII string.
};
#endif

struct ASF_HeaderExtension {
	ASF_GUID  guid;
	XMP_Int64 size;
	ASF_GUID  reserved1;
	XMP_Uns16 reserved2;
	XMP_Uns32 dataLen;
	// The header extension data is a sequence of nested objects.
};
#define kASF_HeaderExtensionSize (16 + 8 + 16 + 2 + 4)

// =================================================================================================

enum {
	kINDD_PageSize = 4096,
	kINDD_LittleEndian = 1,
	kINDD_BigEndian = 2,
	kInDesignGUIDSize = 16
};

struct InDesignMasterPage {
	XMP_Uns8  fGUID[kInDesignGUIDSize];
	XMP_Uns8  fMagicBytes[8];
	XMP_Uns8  fObjectStreamEndian;
	XMP_Uns8  fIrrelevant1[239];
	XMP_Uns64 fSequenceNumber;
	XMP_Uns8  fIrrelevant2[8];
	XMP_Uns32 fFilePages;
	XMP_Uns8  fIrrelevant3[3812];
};

static const XMP_Uns8 kInDesign_MasterPageGUID[kInDesignGUIDSize] =
{ 0x06, 0x06, 0xED, 0xF5, 0xD8, 0x1D, 0x46, 0xE5, 0xBD, 0x31, 0xEF, 0xE7, 0xFE, 0x74, 0xB7, 0x1D };

struct InDesignContigObjMarker {
	XMP_Uns8  fGUID[kInDesignGUIDSize];
	XMP_Uns32 fObjectUID;
	XMP_Uns32 fObjectClassID;
	XMP_Uns32 fStreamLength;
	XMP_Uns32 fChecksum;
};

static const XMP_Uns8 kINDDContigObjHeaderGUID[kInDesignGUIDSize] =
{ 0xDE, 0x39, 0x39, 0x79, 0x51, 0x88, 0x4B, 0x6C, 0x8E, 0x63, 0xEE, 0xF8, 0xAE, 0xE0, 0xDD, 0x38 };

// =================================================================================================

struct FileExtMapping {
	XMP_StringPtr  ext;
	XMP_FileFormat format;
};

const FileExtMapping kFileExtMap[] =	// Add all known mappings, multiple mappings (tif, tiff) are OK.
{ { "pdf",  kXMP_PDFFile },
{ "ps",   kXMP_PostScriptFile },
{ "eps",  kXMP_EPSFile },

{ "jpg",  kXMP_JPEGFile },
{ "jpeg", kXMP_JPEGFile },
{ "jpx",  kXMP_JPEG2KFile },
{ "tif",  kXMP_TIFFFile },
{ "tiff", kXMP_TIFFFile },
{ "gif",  kXMP_GIFFile },
{ "giff", kXMP_GIFFile },
{ "png",  kXMP_PNGFile },

{ "swf",  kXMP_SWFFile },
{ "flv",  kXMP_FLVFile },

{ "aif",  kXMP_AIFFFile },

{ "mov",  kXMP_MOVFile },
{ "avi",  kXMP_AVIFile },
{ "cin",  kXMP_CINFile },
{ "wav",  kXMP_WAVFile },
{ "mp3",  kXMP_MP3File },
{ "mp4",  kXMP_MPEG4File },
{ "ses",  kXMP_SESFile },
{ "cel",  kXMP_CELFile },
{ "wma",  kXMP_WMAVFile },
{ "wmv",  kXMP_WMAVFile },

{ "mpg",  kXMP_MPEGFile },
{ "mpeg", kXMP_MPEGFile },
{ "mp2",  kXMP_MPEGFile },
{ "mod",  kXMP_MPEGFile },
{ "m2v",  kXMP_MPEGFile },
{ "mpa",  kXMP_MPEGFile },
{ "mpv",  kXMP_MPEGFile },
{ "m2p",  kXMP_MPEGFile },
{ "m2a",  kXMP_MPEGFile },
{ "m2t",  kXMP_MPEGFile },
{ "mpe",  kXMP_MPEGFile },
{ "vob",  kXMP_MPEGFile },
{ "ms-pvr", kXMP_MPEGFile },
{ "dvr-ms", kXMP_MPEGFile },

{ "html", kXMP_HTMLFile },
{ "xml",  kXMP_XMLFile },
{ "txt",  kXMP_TextFile },
{ "text", kXMP_TextFile },

{ "psd",  kXMP_PhotoshopFile },
{ "ai",   kXMP_IllustratorFile },
{ "indd", kXMP_InDesignFile },
{ "indt", kXMP_InDesignFile },
{ "aep",  kXMP_AEProjectFile },
{ "aet",  kXMP_AEProjTemplateFile },
{ "ffx",  kXMP_AEFilterPresetFile },
{ "ncor", kXMP_EncoreProjectFile },
{ "prproj", kXMP_PremiereProjectFile },
{ "prtl", kXMP_PremiereTitleFile },

{ 0, kXMP_UnknownFile } };	// ! Must be last as a sentinel.

							// Vector of keys in quicktime file in the order in which they appear
vector <string> ISOMetaKeys;

XMP_Int32 entryCount_dref;
XMP_Uns16 exif_item_id;
XMP_Uns16 mime_item_id;
std::string item_name;
XMP_Uns32 item_type;
std::string content_type;
std::string url_location;
std::string content_encoding;


// File convenience wrappers (now LFA-based) ====================================

// skip forward by <size> bytes and verify not beyond EOF
void static Skip(LFA_FileRef file, XMP_Int64 size)
{
	// assert no more, since LFA_Seek does not return 0 to say o.k., but actual filePos
	// OLD	assertMsg("unexpected end of file", 0 == LFA_Seek (file, size, SEEK_CUR) );
	LFA_Seek(file, size, SEEK_CUR);
}

// going back in the file (use positive values!)
// (yes redundant to above but "makes a better read")
void static Rewind(LFA_FileRef file, XMP_Int64 size)
{
	assertMsg("use positive values", size > 0);
	LFA_Seek(file, -size, SEEK_CUR); // ditto to above
}

// overload, no size parameter, rewinds to start
void static Rewind(LFA_FileRef file)
{
	LFA_Seek(file, 0, SEEK_SET);
}

XMP_Uns32 static Peek32u(LFA_FileRef file, bool bigEndian = false)
{
	XMP_Uns32 value = tree->digest32u(file, "", bigEndian);
	Rewind(file, 4);
	return value;
}


// =================================================================================================

static XMP_FileFormat
LookupFileExtMapping(const char * filePath)
{
	std::string fileExt;
	size_t extPos = strlen(filePath);
	for (--extPos; extPos > 0; --extPos) if (filePath[extPos] == '.') break;

	if (filePath[extPos] != '.') return kXMP_UnknownFile;

	++extPos;
	fileExt.assign(&filePath[extPos]);
	for (size_t i = 0; i < fileExt.size(); ++i) {
		if (('A' <= fileExt[i]) && (fileExt[i] <= 'Z')) fileExt[i] += 0x20;
	}

	size_t mapPos;
	for (mapPos = 0; kFileExtMap[mapPos].ext != 0; ++mapPos) {
		if (fileExt == kFileExtMap[mapPos].ext) break;
	}

	return kFileExtMap[mapPos].format;

}	// LookupFileExtMapping

	// =================================================================================================

	//*** used by in-RAM code? needs replacement?
static void
CaptureFileData(LFA_FileRef file, XMP_Int64 offset, XMP_Uns32 length)
{

	if (length > sDataMax) {
		if (sDataPtr != 0) free(sDataPtr);
		sDataPtr = (XMP_Uns8*)malloc(length);
		sDataMax = length;
	}

	if (offset != 0) LFA_Seek(file, (long)offset, SEEK_SET);
	LFA_Read(file, sDataPtr, length, true);
	sDataLen = length;

}	// CaptureFileData

	// -------------------------------------------------------------------------------------------------

	//*** used by in-RAM code? needs replacement !!!
static void
CaptureXMPF(LFA_FileRef file, XMP_Int64 offset, XMP_Uns32 length)
{

	if (length > sXMPMax) {
		if (sXMPPtr != 0) free(sXMPPtr);
		sXMPPtr = (XMP_Uns8*)malloc(length);
		sXMPMax = length;
	}

	if (offset != 0) LFA_Seek(file, (long)offset, SEEK_SET);
	LFA_Read(file, sXMPPtr, length, true);
	sXMPLen = length;
	sXMPPos = offset;

}	// CaptureXMPF

	// -------------------------------------------------------------------------------------------------

static void
CaptureXMP(const XMP_Uns8 * xmpPtr, const XMP_Uns32 xmpLen, XMP_Int64 fileOffset)
{

	if (xmpLen > sXMPMax) {
		if (sXMPPtr != 0) free(sXMPPtr);
		sXMPPtr = (XMP_Uns8*)malloc(xmpLen);
		sXMPMax = xmpLen;
	}

	memcpy(sXMPPtr, xmpPtr, xmpLen);
	sXMPLen = xmpLen;
	sXMPPos = fileOffset;

}	// CaptureXMP

	// -------------------------------------------------------------------------------------------------

static void PrintOnlyASCII_8(XMP_Uns8 * strPtr, XMP_Uns32 strLen, bool stopOnNUL = true)
{
	//wrapping to QEBuginese
	// - NB: remainder (zero termination earlier then length) is catered for...

	tree->addComment(convert8Bit(strPtr, stopOnNUL, strLen));
}

// -------------------------------------------------------------------------------------------------

// this wrap and the LE counterpart can only be inferior, since
// its always added as a comment, even if value was more appropriate.
// ==> callers should make use of convert16Bit directly.
static void PrintOnlyASCII_16BE(XMP_Uns16 * u16Ptr, XMP_Uns32 u16Bytes, bool stopOnNUL = true)
{
	tree->addComment(convert16Bit(true, (XMP_Uns8*)u16Ptr, stopOnNUL, u16Bytes));
}	// PrintOnlyASCII_16BE

	// -------------------------------------------------------------------------------------------------

static void PrintOnlyASCII_16LE(XMP_Uns16 * u16Ptr, XMP_Uns32 u16Bytes, bool stopOnNUL = true)
{
	tree->addComment(convert16Bit(false, (XMP_Uns8*)u16Ptr, stopOnNUL, u16Bytes));
}	// PrintOnlyASCII_16LE

	// =================================================================================================

static const XMP_Int64 kJPEGMinSize = 4;	// At least the SOI and EOI markers.
static const XMP_Uns8  kJPEGStart[] = { 0xFF, 0xD8, 0xFF };	// 0xFFD8 is SOI, plus 0xFF for next marker.

static const XMP_Int64 kPhotoshopMinSize = 26 + 4 * 4;	// At least the file header and 4 section lengths.
static const XMP_Uns8  kPhotoshopV1Start[] = { 0x38, 0x42, 0x50, 0x53, 0x00, 0x01 };	// 0x38425053 is "8BPS".
static const XMP_Uns8  kPhotoshopV2Start[] = { 0x38, 0x42, 0x50, 0x53, 0x00, 0x02 };

static const XMP_Int64 kTIFFMinSize = 8 + 2 + 12 + 4;	// At least the header plus 1 minimal IFD.
static const XMP_Uns8  kTIFFBigStart[] = { 0x4D, 0x4D, 0x00, 0x2A };	// 0x4D is 'M', 0x2A is 42.
static const XMP_Uns8  kTIFFLittleStart[] = { 0x49, 0x49, 0x2A, 0x00 };	// 0x49 is 'I'.

static const XMP_Int64 kJPEG2KMinSize = 12 + 16;	// At least the signature and minimal file type boxes.
static const XMP_Uns8  kJPEG2KStart[] = { 0x00, 0x00, 0x00, 0x0C, 0x6A, 0x50, 0x20, 0x20, 0x0D, 0x0A, 0x87, 0x0A };

static const XMP_Int64 kPNGMinSize = 8 + (12 + 13) + 12;	// At least the file header plus IHDR and IEND chunks.
static const XMP_Uns8  kPNGStart[] = { 137, 80, 78, 71, 13, 10, 26, 10 };

static const XMP_Int64 kASFMinSize = 16;	// ! Not really accurate, but covers the header GUID.

static const XMP_Int64 kRIFFMinSize = 12;

static const XMP_Int64 kPostScriptMinSize = 49;


static const XMP_Int64 kInDesignMinSize = 2 * kINDD_PageSize;	// Two master pages.

static const XMP_Int64 kISOMediaMinSize = 16;	// At least a minimal file type box.
static const XMP_Uns8  kISOMediaFTyp[] = { 0x66, 0x74, 0x79, 0x70 };	// "ftyp"
static const XMP_Uns32 kISOTag_ftyp = 0x66747970UL;
static const XMP_Uns32 kISOBrand_mp41 = 0x6D703431UL;
static const XMP_Uns32 kISOBrand_mp42 = 0x6D703432UL;
static const XMP_Uns32 kISOBrand_avc1 = 0x61766331UL;
static const XMP_Uns32 kISOBrand_f4v = 0x66347620UL;
static const XMP_Uns32 kISOBrand_isom = 0x69736F6DUL;
static const XMP_Uns32 kISOBrand_3gp4 = 0x33677034UL;
static const XMP_Uns32 kISOBrand_3g2a = 0x33673261UL;
static const XMP_Uns32 kISOBrand_3g2b = 0x33673262UL;
static const XMP_Uns32 kISOBrand_3g2c = 0x33673263UL;
static const XMP_Uns32 kISOBrand_mif1 = 0x6D696631UL;


static const XMP_Uns32 kQTTag_XMP_ = 0x584D505FUL;

static const XMP_Int64 kSWFMinSize = (8 + 2 + 4 + 2);	// Header with minimal rectangle and an End tag.

static const XMP_Int64 kFLVMinSize = 9;	// Header with zero length data.

static const XMP_Uns8  kPostScriptStart[] = { 0xC5, 0xD0, 0xD3, 0xC6 };

static XMP_FileFormat
CheckFileFormat(const char * filePath, XMP_Uns8 * fileContent, XMP_Int64 fileSize)
{
	// ! The buffer passed to CheckFileFormat is just the first 4K bytes of the file.

	if ((fileSize >= kJPEGMinSize) && CheckBytes(fileContent, kJPEGStart, 3)) {
		return kXMP_JPEGFile;
	}

	if ((fileSize >= kPhotoshopMinSize) &&
		(CheckBytes(fileContent, kPhotoshopV1Start, 6) || CheckBytes(fileContent, kPhotoshopV2Start, 6))) {
		return kXMP_PhotoshopFile;
	}

	if ((fileSize >= kTIFFMinSize) &&
		(CheckBytes(fileContent, kTIFFBigStart, 4) || CheckBytes(fileContent, kTIFFLittleStart, 4))) {
		return kXMP_TIFFFile;
	}

	if ((fileSize >= kJPEG2KMinSize) && CheckBytes(fileContent, kJPEG2KStart, 12)) {
		return kXMP_JPEG2KFile;
	}

	if ((fileSize >= kASFMinSize) && CheckBytes(fileContent, &kASF_HeaderGUID, 16)) {
		return kXMP_WMAVFile;
	}

	if ((fileSize >= kPNGMinSize) && CheckBytes(fileContent, kPNGStart, 8)) {
		return kXMP_PNGFile;
	}

	if ((fileSize >= kRIFFMinSize) && CheckBytes(fileContent, "RIFF", 4)) {
		if (CheckBytes(fileContent + 8, "AVI ", 4)) return kXMP_AVIFile;
		if (CheckBytes(fileContent + 8, "WAVE", 4)) return kXMP_WAVFile;
	}

	if ((fileSize >= kRIFFMinSize) && CheckBytes(fileContent, "RF64", 4)) {
		if (CheckBytes(fileContent + 8, "WAVE", 4)) return kXMP_WAVFile;
	}

	if ((fileSize >= kRIFFMinSize) && CheckBytes(fileContent, "FORM", 4)) {
		if (CheckBytes(fileContent + 8, "AIFF ", 4)) return kXMP_AIFFFile;
		if (CheckBytes(fileContent + 8, "AIFC", 4)) return kXMP_AIFFFile;
	}

	if ((fileSize >= kPostScriptMinSize) && CheckBytes(fileContent, kPostScriptStart, 4)) {
		return kXMP_PostScriptFile;
	}

	if ((fileSize >= kInDesignMinSize) && CheckBytes(fileContent, kInDesign_MasterPageGUID, kInDesignGUIDSize)) {
		return kXMP_InDesignFile;
	}

	if ((fileSize >= kSWFMinSize) &&
		(CheckBytes(fileContent, "FWS", 3) || CheckBytes(fileContent, "CWS", 3)) &&
		(fileContent[3] <= 10 /* 2007 latest is 8 */)) {
		return kXMP_SWFFile;
	}

	if ((fileSize >= kFLVMinSize) &&
		CheckBytes(fileContent, "FLV", 3) &&
		(fileContent[3] <= 10 /* 2007 latest is 1 */)) {
		return kXMP_FLVFile;
	}

	if ((fileSize >= kISOMediaMinSize) && CheckBytes(fileContent + 4, kISOMediaFTyp, 4)) {

		XMP_Uns32 ftypLen = GetUns32BE(fileContent);
		if (ftypLen == 0) ftypLen = fileSize;
		if ((ftypLen < kISOMediaMinSize) || (ftypLen > fileSize) || (ftypLen > 4096)) return kXMP_UnknownFile;

		XMP_Uns8 * compatPtr = fileContent + 16;
		XMP_Uns8 * compatEnd = fileContent + ftypLen;

		for (; compatPtr < compatEnd; compatPtr += 4) {
			XMP_Uns32 compatBrand = GetUns32BE(compatPtr);
			switch (compatBrand) {
			case kISOBrand_mp41:
			case kISOBrand_mp42:
			case kISOBrand_avc1:
			case kISOBrand_isom:
			case kISOBrand_3gp4:
			case kISOBrand_3g2a:
			case kISOBrand_3g2b:
			case kISOBrand_3g2c:
				return kXMP_MPEG4File;
				break;
			case kISOBrand_mif1:
				return kXMP_HEIFFile;
				break;
			default:
				break;

			}

		}

	}

	if ((fileSize > 30) && CheckBytes(fileContent, "\x50\x4B\x03\x04", 4)) {  // "PK 03 04"
		return kXMP_UCFFile;
	}

	// ! Do MP3 next to last. It uses the file extension if there is no ID3.
	if (CheckBytes(fileContent, "ID3", 3) ||
		(LookupFileExtMapping(filePath) == kXMP_MP3File)) return kXMP_MP3File;

	// ! Do MPEG (MP2) and MOV last. They use just the file extension, not content.
	if (LookupFileExtMapping(filePath) == kXMP_MPEGFile) return kXMP_MPEGFile;
	if (LookupFileExtMapping(filePath) == kXMP_MOVFile) return kXMP_MOVFile;

	std::string fileData = (char*)fileContent;
	if ((fileSize > 30) && (int)fileData.find("<svg") >= 0) {
		return kXMP_SVGFile;
	}

	return kXMP_UnknownFile;

}	// CheckFileFormat

	// =================================================================================================
	// DumpXMP
	// =======

static void
DumpXMP(XMP_Uns8 * xmpPtr, XMP_Uns32 xmpLen, XMP_Int64 xmpOffset, const char * label)
{
	if (xmpOffset <= 0xFFFFFFFFL) {
		tree->pushNode("XMP");
		tree->addComment("from %s, offset %u (0x%X), size %d",
			label, (XMP_Uns32)xmpOffset, (XMP_Uns32)xmpOffset, xmpLen);
	}
	else {
		tree->pushNode("XMP");
		tree->addComment("from %s, offset %ll (0x%X-%.8X), size %d",
			label, High32(xmpOffset), Low32(xmpOffset), xmpLen);
	}

	//bool atStart = true;
	SXMPMeta xmp((XMP_StringPtr)xmpPtr, xmpLen);
	xmp.Sort();

	//FNO: could be reactived, but makes the dump naturally very long - harder to read for dev work
	//xmp.DumpObject( (DumpCallback, &atStart);
	tree->popNode();
}

// =================================================================================================
// DumpXMP
// =======

// an (old) wrapper for above function relying on static, "global" variables
static void
DumpXMP(const char * label)
{
	DumpXMP(sXMPPtr, sXMPLen, sXMPPos, label);
}	// DumpXMP

	// =================================================================================================
	// DumpIPTC
	// ========
	//
	// The IPTC (IIM, NAA) values are in a sequence of "data sets". Each has a 5 byte header followed
	// by the value. There is no overall length in the sequence itself. Photoshop writes this in TIFF
	// as LONGs (4 byte chunks), so there might be padding at the end.

static void
DumpIPTC(XMP_Uns8 * iptcOrigin, XMP_Uns32 iptcLen, XMP_Uns32 fileOffset, const char * label)
{
	tree->pushNode("IPTC data");
	tree->addComment("from %s, offset %d (0x%X), size %d",
		label, fileOffset, fileOffset, iptcLen);

	// ** Compute and print the MD5 digest.
	XMP_Uns8 * iptcPtr = iptcOrigin;
	XMP_Uns8 * iptcEnd = iptcPtr + iptcLen;
	XMP_Uns8 * valuePtr;
	XMP_Uns32 valueLen;

	while (iptcPtr < (iptcEnd - 4)) {	// ! The -4 is to skip terminal padding.
		IPTC_DataSet * currDS = (IPTC_DataSet*)iptcPtr;
		if (currDS->tagMarker != 0x1C) {
			tree->comment("** invalid IPTC marker **");
			break;
		}
		valuePtr = iptcPtr + 5;
		valueLen = (currDS->octetCountHigh << 8) + currDS->octetCountLow;

		if ((valueLen >> 15) == 1) {
			int count = valueLen & 0x7FFF;
			valueLen = 0;
			for (int i = 0; i < count; ++i) valueLen = (valueLen << 8) + valuePtr[i];
			valuePtr += count;
		}

		XMP_Uns32 dsOffset = fileOffset + (iptcPtr - iptcOrigin);

		//key come here ===================
		tree->setKeyValue(
			fromArgs("IPTC:%d:%d", currDS->recordNumber, currDS->dataSetNumber), "");
		tree->addComment("offset %d (0x%X), size %d", dsOffset, dsOffset, valueLen);


		if ((currDS->recordNumber != 1) && (currDS->recordNumber != 2)) {

			//LF only 1:** and 2:** bother us

		}
		else if (currDS->recordNumber == 1) {

			switch (currDS->dataSetNumber) {
			case 0:
			{
				XMP_Uns16 version = GetUns16BE(valuePtr);
				tree->addComment("version = 0x%.4X", version);
				break;
			}
			case 90:
				if (valueLen == 3) {
					tree->addComment("encoding = 0x%.2X%.2X%.2X", valuePtr[0], valuePtr[1], valuePtr[2]);
					if (memcmp(valuePtr, "\x1B\x25\x47", 3) == 0) tree->addComment(" (UTF-8)");
				}
				break;
			default:
				break;
			}

		}
		else if (currDS->dataSetNumber == 0) {

			XMP_Uns16 version = GetUns16BE(valuePtr);
			tree->addComment(",Version = 0x%.4X", version);

		}
		else {

			int ds;
			for (ds = 0; kDataSetNames[ds].name != 0; ++ds) {
				if (currDS->dataSetNumber == kDataSetNames[ds].id) break;
			}
			if (kDataSetNames[ds].name == 0) {
				//LF
			}
			else {
				tree->addComment("%s", kDataSetNames[ds].name);
				tree->changeValue(convert8Bit(valuePtr, false, valueLen));
			}

		}

		iptcPtr = valuePtr + valueLen;

	}

	//LF
	if (iptcPtr > iptcEnd) {
		tree->comment("** Too much IPTC data, delta %d", (long)(iptcEnd - iptcPtr));
	}
	else {
		while ((iptcPtr < iptcEnd) && (*iptcPtr == 0)) ++iptcPtr;
		if (iptcPtr != iptcEnd) tree->comment("** Too little IPTC data, delta %d", (long)(iptcPtr - iptcEnd));
	}

	tree->popNode();
}	// DumpIPTC

	// =================================================================================================

static void
DumpImageResources(const JpegMarkers& psirMarkers, XMP_Uns8 * dataStart, const char * label)
{

	XMP_Uns32 i = 0, size = psirMarkers.size();
	std::string combinedPSIRData;
	for (i = 0; i < size; i++) {
		combinedPSIRData.append((const char *)psirMarkers[i].jpegMarkerPtr, psirMarkers[i].jpegMarkerLen);
	}


	XMP_Uns8 * psirPtr = (XMP_Uns8 *)combinedPSIRData.data();
	XMP_Uns8 * psirEnd = psirPtr + combinedPSIRData.size();

	XMP_Uns8 * irPtr;
	XMP_Uns32  irLen, irOffset; //irType replaced by irTypeStr below

	XMP_Uns8 * iptcPtr = 0;
	XMP_Uns8 * xmpPtr = 0;
	XMP_Uns8 * exif1Ptr = 0;
	XMP_Uns8 * exif3Ptr = 0;
	XMP_Uns32  iptcLen, xmpLen, exif1Len, exif3Len;
	XMP_Int32 lastIndexUsed = -1;
	while (psirPtr < psirEnd) {
		// calculate fileOffset and psirOrigin
		size_t currentOffset = (const char *)psirPtr - combinedPSIRData.data();
		XMP_Uns32 length = 0;
		for (i = 0; i < size; i++) {
			length += psirMarkers[i].jpegMarkerLen;
			if (currentOffset <= length)
				break;
		}
		if (lastIndexUsed != (XMP_Int32)i) {
			if (lastIndexUsed != -1)
				tree->popNode();
			// time to push a new node
			tree->pushNode("Photoshop Image Resources %d", i + 1);
			XMP_Uns32 fileOffset = psirMarkers[i].jpegMarkerPtr - dataStart;
			tree->addComment("from %s, offset %d (0x%X), size %d",
				label, fileOffset, fileOffset, psirMarkers[i].jpegMarkerLen);
			lastIndexUsed = i;
		}
		XMP_Uns32 fileOffset = psirMarkers[i].jpegMarkerPtr - dataStart;
		XMP_Uns8 * psirOrigin = psirMarkers[i].jpegMarkerPtr;

		std::string irTypeStr = convert8Bit(psirPtr, false, 4); //get in an endian neutral way
		XMP_Uns16 irID = GetUns16BE(psirPtr + 4);	// The image resource ID.

		const char* irName = (XMP_StringPtr)psirPtr + 6;	// A Pascal string.
		irOffset = 6 + ((*irName + 2) & 0xFFFFFFFE);	// Offset to the image resource data length.
		irLen = GetUns32BE(psirPtr + irOffset);
		irPtr = psirPtr + irOffset + 4;

		irOffset = fileOffset + ((psirPtr - (XMP_Uns8 *)combinedPSIRData.data()) - (length - psirMarkers[i].jpegMarkerLen));

		if (irTypeStr != "8BIM") {
			tree->setKeyValue(fromArgs("PSIR:%s:#%u", irTypeStr.c_str(), irID), "");
			tree->comment("(non-8BIM encountered and tolerated, see bug 1454756)");
		}
		else if (irID == kPSIR_IPTC) {			//****************
			tree->setKeyValue("PSIR:IPTC", "");
			iptcPtr = irPtr;
			iptcLen = irLen;
			if (iptcPtr != 0) {
				XMP_Uns32 offset = fileOffset + ((iptcPtr - (XMP_Uns8 *)combinedPSIRData.data()) - (length - psirMarkers[i].jpegMarkerLen));
				DumpIPTC(iptcPtr, iptcLen, offset, "PSIR #1028");
			}
		}
		else if (irID == kPSIR_XMP) {				//****************
			tree->setKeyValue("PSIR:XMP", "");
			xmpPtr = irPtr;
			xmpLen = irLen;
			if (xmpPtr != 0) {
				XMP_Uns32 offset = fileOffset + ((xmpPtr - (XMP_Uns8 *)combinedPSIRData.data()) - (length - psirMarkers[i].jpegMarkerLen));
				DumpXMP(xmpPtr, xmpLen, offset, "PSIR #1060");
			}
		}
		else if (irID == kPSIR_Exif_1) {			//****************
			tree->setKeyValue("PSIR:Exif-1", "");
			exif1Ptr = irPtr;
			exif1Len = irLen;
			XMP_Uns32 offset = fileOffset + ((exif1Ptr - (XMP_Uns8 *)combinedPSIRData.data()) - (length - psirMarkers[i].jpegMarkerLen));
			DumpTIFF(exif1Ptr, exif1Len, offset, "PSIR #1058 (Exif 1)", "PSIR:Exif-1");
		}
		else if (irID == kPSIR_Exif_3) {			//****************
			tree->setKeyValue("PSIR:Exif-3", "");
			exif3Ptr = irPtr;
			exif3Len = irLen;
			XMP_Uns32 offset = fileOffset + ((exif3Ptr - (XMP_Uns8 *)combinedPSIRData.data()) - (length - psirMarkers[i].jpegMarkerLen));
			if (exif3Ptr != 0) DumpTIFF(exif3Ptr, exif3Len, offset, "PSIR #1059 (Exif 3)", "PSIR:Exif-3");
		}
		else if (irID == kPSIR_IPTC_Digest) {
			tree->setKeyValue("PSIR:IPTC digest",
				fromArgs("%.8X-%.8X-%.8X-%.8X",
					GetUns32BE(irPtr),
					GetUns32BE(irPtr + 4),
					GetUns32BE(irPtr + 8),
					GetUns32BE(irPtr + 12))
			);
		}
		else if (irID == kPSIR_CopyrightFlag) {
			bool copyrighted = (*irPtr != 0);
			tree->setKeyValue("PSIR:copyrighted", (copyrighted ? "yes" : "no"));
		}
		else if (irID == kPSIR_CopyrightURL) {
			tree->setKeyValue("PSIR:copyright URL", convert8Bit(irPtr, true, irLen));
		}
		else if (irID == kPSIR_OldCaption) {
			tree->setKeyValue("PSIR:old caption", convert8Bit(irPtr, true, irLen));
		}
		else if (irID == kPSIR_PrintCaption) {
			tree->comment("** obsolete print caption **");
		}
		else {
			tree->setKeyValue(
				fromArgs("PSIR:%s:#%d", irTypeStr.c_str(), irID),
				""
			);
		}
		if (irOffset + irLen > (psirMarkers[i].jpegMarkerPtr - dataStart) + psirMarkers[i].jpegMarkerLen) {
			//merged from two markers
			tree->addComment("offset %d (0x%X), size %d - split in multiple markers", irOffset, irOffset, irLen);
		}
		else {
			tree->addComment("offset %d (0x%X), size %d", irOffset, irOffset, irLen);
		}
		if (*irName != 0) tree->addComment("\"%.*s\"", (int)(*irName), (irName + 1));
		psirPtr = irPtr + ((irLen + 1) & 0xFFFFFFFE);	// Round the length to be even.
	} //while-loop

	if (psirPtr != psirEnd) {
		tree->addComment("** Unexpected end of image resources, delta %d", (long)(psirPtr - psirEnd));
	}

	//NB: dump routines moved up into if-else's	
	tree->popNode();
}	// DumpImageResources

	// =================================================================================================

static void
DumpImageResources(XMP_Uns8 * psirOrigin, XMP_Uns32 psirLen, XMP_Uns32 fileOffset, const char * label)
{
	tree->pushNode("Photoshop Image Resources");
	tree->addComment("from %s, offset %d (0x%X), size %d",
		label, fileOffset, fileOffset, psirLen);

	XMP_Uns8 * psirPtr = psirOrigin;
	XMP_Uns8 * psirEnd = psirPtr + psirLen;
	XMP_Uns8 * irPtr;
	XMP_Uns32  irLen, irOffset; //irType replaced by irTypeStr below

	XMP_Uns8 * iptcPtr = 0;
	XMP_Uns8 * xmpPtr = 0;
	XMP_Uns8 * exif1Ptr = 0;
	XMP_Uns8 * exif3Ptr = 0;
	XMP_Uns32  iptcLen, xmpLen, exif1Len, exif3Len;

	while (psirPtr < psirEnd) {
		std::string irTypeStr = convert8Bit(psirPtr, false, 4); //get in an endian neutral way
		XMP_Uns16 irID = GetUns16BE(psirPtr + 4);	// The image resource ID.

		const char* irName = (XMP_StringPtr)psirPtr + 6;	// A Pascal string.
		irOffset = 6 + ((*irName + 2) & 0xFFFFFFFE);	// Offset to the image resource data length.
		irLen = GetUns32BE(psirPtr + irOffset);
		irPtr = psirPtr + irOffset + 4;

		irOffset = fileOffset + (psirPtr - psirOrigin);

		if (irTypeStr != "8BIM") {
			tree->setKeyValue(fromArgs("PSIR:%s:#%u", irTypeStr.c_str(), irID), "");
			tree->comment("(non-8BIM encountered and tolerated, see bug 1454756)");
		}
		else if (irID == kPSIR_IPTC) {			//****************
			tree->setKeyValue("PSIR:IPTC", "");
			iptcPtr = irPtr;
			iptcLen = irLen;
			if (iptcPtr != 0) DumpIPTC(iptcPtr, iptcLen, (fileOffset + (iptcPtr - psirOrigin)), "PSIR #1028");
		}
		else if (irID == kPSIR_XMP) {				//****************
			tree->setKeyValue("PSIR:XMP", "");
			xmpPtr = irPtr;
			xmpLen = irLen;
			if (xmpPtr != 0) DumpXMP(xmpPtr, xmpLen, (fileOffset + (xmpPtr - psirOrigin)), "PSIR #1060");
		}
		else if (irID == kPSIR_Exif_1) {			//****************
			tree->setKeyValue("PSIR:Exif-1", "");
			exif1Ptr = irPtr;
			exif1Len = irLen;
			DumpTIFF(exif1Ptr, exif1Len, (fileOffset + (exif1Ptr - psirOrigin)), "PSIR #1058 (Exif 1)", "PSIR:Exif-1");
		}
		else if (irID == kPSIR_Exif_3) {			//****************
			tree->setKeyValue("PSIR:Exif-3", "");
			exif3Ptr = irPtr;
			exif3Len = irLen;
			if (exif3Ptr != 0) DumpTIFF(exif3Ptr, exif3Len, (fileOffset + (exif3Ptr - psirOrigin)), "PSIR #1059 (Exif 3)", "PSIR:Exif-3");
		}
		else if (irID == kPSIR_IPTC_Digest) {
			tree->setKeyValue("PSIR:IPTC digest",
				fromArgs("%.8X-%.8X-%.8X-%.8X",
					GetUns32BE(irPtr),
					GetUns32BE(irPtr + 4),
					GetUns32BE(irPtr + 8),
					GetUns32BE(irPtr + 12))
			);
		}
		else if (irID == kPSIR_CopyrightFlag) {
			bool copyrighted = (*irPtr != 0);
			tree->setKeyValue("PSIR:copyrighted", (copyrighted ? "yes" : "no"));
		}
		else if (irID == kPSIR_CopyrightURL) {
			tree->setKeyValue("PSIR:copyright URL", convert8Bit(irPtr, true, irLen));
		}
		else if (irID == kPSIR_OldCaption) {
			tree->setKeyValue("PSIR:old caption", convert8Bit(irPtr, true, irLen));
		}
		else if (irID == kPSIR_PrintCaption) {
			tree->comment("** obsolete print caption **");
		}
		else {
			tree->setKeyValue(
				fromArgs("PSIR:%s:#%d", irTypeStr.c_str(), irID),
				""
			);
		}
		tree->addComment("offset %d (0x%X), size %d", irOffset, irOffset, irLen);
		if (*irName != 0) tree->addComment("\"%.*s\"", (int)(*irName), (irName + 1));
		psirPtr = irPtr + ((irLen + 1) & 0xFFFFFFFE);	// Round the length to be even.
	} //while-loop

	if (psirPtr != psirEnd) {
		tree->addComment("** Unexpected end of image resources, delta %d", (long)(psirPtr - psirEnd));
	}

	//NB: dump routines moved up into if-else's	

	tree->popNode();
}	// DumpImageResources

	// =================================================================================================

static void
DumpOneIFD(int ifdIndex, XMP_Uns8 * ifdPtr, XMP_Uns8 * endPtr,
	XMP_Uns8 * tiffContent, XMP_Uns32 fileOffset, const char * label, std::string path)
{
	XMP_Uns8 * exifPtr = 0;
	XMP_Uns8 * gpsPtr = 0;
	XMP_Uns8 * interopPtr = 0;
	XMP_Uns8 * makerNotePtr = 0;
	XMP_Uns8 * psirPtr = 0;
	XMP_Uns8 * iptcPtr = 0;
	XMP_Uns8 * xmpPtr = 0;
	XMP_Uns32 psirLen = 0;
	XMP_Uns32 iptcLen = 0;
	XMP_Uns32 xmpLen = 0;

	XMP_Uns32 ifdOffset = ifdPtr - tiffContent;
	XMP_Uns16 fieldCount = TIFF_GetUns16(ifdPtr);
	XMP_Uns32 ifdLen = 2 + (12 * fieldCount) + 4;
	XMP_Uns32 nextIFD = TIFF_GetUns32(ifdPtr + ifdLen - 4);

	tree->pushNode("TIFF IFD #%d from %s", ifdIndex, label);
	tree->addComment("offset %d (0x%X), tag count %d",
		(ifdOffset + fileOffset), (ifdOffset + fileOffset), fieldCount);

	if (nextIFD == 0) {
		tree->comment("end of IFD chain");
	}
	else {
		tree->comment("next IFD offset %d (0x%X)", (nextIFD + fileOffset), (nextIFD + fileOffset));
	}

	XMP_Uns16 prevTag = 0;
	XMP_Uns8 * fieldPtr = tiffContent + ifdOffset + 2;

	if (!path.empty())
		path.append("/");

	path.append(fromArgs("IFD%d", ifdIndex));

	for (int i = 0; i < fieldCount; ++i, fieldPtr += 12) {

		XMP_Uns16 fieldTag = TIFF_GetUns16(fieldPtr);
		XMP_Uns16 fieldType = TIFF_GetUns16(fieldPtr + 2);
		XMP_Uns32 valueCount = TIFF_GetUns32(fieldPtr + 4);
		XMP_Uns32 valueOffset = TIFF_GetUns32(fieldPtr + 8);

		XMP_Uns8 * valuePtr = ifdPtr - ifdOffset + valueOffset;
		XMP_Uns32 valueLen = 0;
		if (fieldType < kTIFF_TypeEnd) valueLen = valueCount * sTIFF_TypeSizes[fieldType];
		if (valueLen <= 4) valuePtr = fieldPtr + 8;

		//===================== adding key here
		tree->setKeyValue(fromArgs("%s/TIFF:%d", path.c_str(), fieldTag));

		if ((fieldType < 1) || (fieldType >= kTIFF_TypeEnd)) {
			tree->addComment("type %d", fieldType);
		}
		else {
			tree->addComment("%s", sTIFF_TypeNames[fieldType]);
		}

		tree->addComment("count %d, value size %d", valueCount, valueLen);

		if (valueLen > 4) {
			tree->addComment("value offset %d (0x%X)", (valueOffset + fileOffset), (valueOffset + fileOffset));
		}
		else {
			XMP_Uns32 rawValue = GetUns32BE(fieldPtr + 8);
			tree->addComment("value in IFD (0x%.8X)", rawValue);
		}

		if (fieldTag == kTIFF_Exif) {
			tree->addComment("Exif IFD offset");
			exifPtr = tiffContent + TIFF_GetUns32(valuePtr);	// Value is Exif IFD offset.
		}
		else if (fieldTag == kTIFF_GPS) {
			tree->addComment("GPS IFD offset");
			gpsPtr = tiffContent + TIFF_GetUns32(valuePtr);	// Value is GPS IFD offset.
		}
		else if (fieldTag == kTIFF_Interop) {
			tree->addComment("Interoperability IFD offset");
			interopPtr = tiffContent + TIFF_GetUns32(valuePtr);	// Value is Interoperability IFD offset.
		}
		else if (fieldTag == kTIFF_MakerNote) {	// Decide if the Maker Note might be formatted as an IFD.
			tree->addComment("Maker Note");
			XMP_Uns32 itemCount = (valueLen - 6) / 12;
			if ((valueLen >= 18) && (valueLen == (6 + itemCount * 12)) &&
				(itemCount == TIFF_GetUns16(valuePtr)) &&
				(TIFF_GetUns32(valuePtr + 2 + (12 * itemCount)) == 0)) {
				makerNotePtr = valuePtr;
			}
		}
		else if (fieldTag == kTIFF_PSIR) {
			tree->addComment("PSIR");
			psirPtr = valuePtr;
			psirLen = valueLen;
		}
		else if (fieldTag == kTIFF_IPTC) {
			tree->addComment("IPTC");
			iptcPtr = valuePtr;
			iptcLen = valueLen;
		}
		else if (fieldTag == kTIFF_XMP) {
			tree->addComment("XMP");
			if (fieldType == kTIFF_ASCII) fieldType = kTIFF_Uns8;	// Avoid displaying the raw packet for mis-typed XMP.
			xmpPtr = valuePtr;
			xmpLen = valueLen;
		}
		else {
			for (int j = 0; sTIFF_TagNames[j].tag != 0; ++j) {
				if (sTIFF_TagNames[j].tag == fieldTag) {
					tree->addComment("%s", sTIFF_TagNames[j].name);
					break;
				}
			}
		}

		XMP_Uns8 value8;
		XMP_Uns16 value16;
		XMP_Uns32 value32;
		XMP_Uns32 denom;
		std::string tempStr;
		char cs[31];

		switch (fieldType) {

		case kTIFF_Uns8:
			if (valueCount == 1) {
				value8 = *valuePtr;
				tree->addComment("hex value = 0x%.2X", value8);
				tree->changeValue("%u", value8);
			}
			break;

		case kTIFF_ASCII:
			tree->changeValue(convert8Bit(valuePtr, false /* internal NULs OK*/, valueLen));
			break;

		case kTIFF_Uns16:
			if (valueCount == 1) {
				value16 = TIFF_GetUns16(valuePtr);
				tree->addComment("hex value = 0x%.4X", value16);
				tree->changeValue("%u", value16);
			}
			break;

		case kTIFF_Uns32:
			if (valueCount == 1) {
				value32 = TIFF_GetUns32(valuePtr);
				tree->addComment("hex value = 0x%.8X", value32);
				tree->changeValue("%u", value32);
			}
			break;

		case kTIFF_URational:
			for (unsigned int j = 0; j < valueCount; j++) {
				value32 = TIFF_GetUns32(valuePtr + (j * 8));
				denom = TIFF_GetUns32(valuePtr + (j * 8) + 4);
				snprintf(cs, 30, "%u", value32);
				tempStr += cs;
				tempStr += "/";
				snprintf(cs, 30, "%u", denom);
				tempStr += cs;
				if (j < valueCount - 1)
					tempStr += ";";
			}
			if (tempStr.length() > 0)
				tree->changeValue(tempStr);
			break;

		case kTIFF_Int8:
			if (valueCount == 1) {
				value8 = *valuePtr;
				//fno: show the hex value unsigned (memory representation) and the decimal signed
				tree->addComment("hex value 0x%.2X", value8);
				tree->changeValue("%d", *((XMP_Int8*)&value8));
			}
			break;

		case kTIFF_Undef8:
			if (valueCount == 1) {
				value8 = *valuePtr;
				tree->changeValue("0x%.2X", value8);
			}
			else if (fieldTag == 36864) {	// ExifVersion
				tree->changeValue("%.*s", valueCount, valuePtr);
			}
			else if (fieldTag == 37510) {	// UserComment
				XMP_Uns8 * encPtr = valuePtr;
				valuePtr += 8;
				valueCount -= 8;
				sprintf(cs, "encoding = %.8s", encPtr);
				tempStr += cs;
				if (!CheckBytes(encPtr, "UNICODE\0", 8)) {
					tree->changeValue(convert8Bit(valuePtr, false, valueCount));
				}
				else {
					bool doBE = beTIFF;
					if (CheckBytes(valuePtr, "\xFE\xFF", 2)) {
						doBE = true;
						valuePtr += 2;
						valueCount -= 2;
						tempStr += ", BE BOM";
					}
					if (CheckBytes(valuePtr, "\xFF\xFE", 2)) {
						doBE = false;
						valuePtr += 2;
						valueCount -= 2;
						tempStr += ", LE BOM";
					}
					if (doBE) {
						tree->changeValue(convert16Bit(true, (XMP_Uns8*)valuePtr, false, valueCount));
						//PrintOnlyASCII_16BE ( (XMP_Uns16*)valuePtr, valueCount, ", value =", false /* ! stopOnNUL */ );
					}
					else {
						tree->changeValue(convert16Bit(false, (XMP_Uns8*)valuePtr, false, valueCount));
						//PrintOnlyASCII_16LE ( (XMP_Uns16*)valuePtr, valueCount, ", value =", false /* ! stopOnNUL */ );
					}
				}
			}

			if (tempStr.length() > 0)
				tree->addComment(tempStr);
			break;

		case kTIFF_Int16:
			if (valueCount == 1) {
				value16 = TIFF_GetUns16(valuePtr);
				tree->changeValue("%d (0x%.4X)", *((XMP_Int16*)&value16), value16);
			}
			break;

		case kTIFF_Int32:
			if (valueCount == 1) {
				value32 = TIFF_GetUns32(valuePtr);
				tree->changeValue("%d (0x%.8X)", *((XMP_Int32*)&value32), value32);
			}
			break;

		case kTIFF_SRational:
			if (valueCount == 1) {
				value32 = TIFF_GetUns32(valuePtr);
				denom = TIFF_GetUns32(valuePtr + 4);
				tree->changeValue("%d/%d", *((XMP_Int32*)&value32), *((XMP_Int32*)&denom));
			}
			break;

		case kTIFF_Float:
			break;

		case kTIFF_Double:
			break;

		case kTIFF_IFD:
			if (valueCount == 1) {
				value32 = TIFF_GetUns32(valuePtr);
				tree->addComment("hex value = 0x%.8X", value32);
				tree->changeValue("%u", value32);
			}
			break;

		default:
			tree->addComment("** unknown type **");
			break;

		}

		if (fieldTag == prevTag) {
			tree->addComment("** Repeated tag **");
		}
		else if (fieldTag < prevTag) {
			tree->addComment("** Out of order tag **");
		}

		prevTag = fieldTag;

	}

	if (exifPtr != 0) {
		DumpIFDChain(exifPtr, endPtr, tiffContent,
			(fileOffset + (exifPtr - tiffContent)), "TIFF tag #34665 (Exif IFD)", path + "/TIFF:34665");
	}

	if (gpsPtr != 0) {
		DumpIFDChain(gpsPtr, endPtr, tiffContent,
			(fileOffset + (gpsPtr - tiffContent)), "TIFF tag #34853 (GPS Info IFD)", path + "/TIFF:34853");
	}

	if (interopPtr != 0) {
		DumpIFDChain(interopPtr, endPtr, tiffContent,
			(fileOffset + (interopPtr - tiffContent)), "TIFF tag #40965 (Interoperability IFD)", path + "/TIFF:40965");
	}

	if (makerNotePtr != 0) {
		DumpIFDChain(makerNotePtr, endPtr, tiffContent,
			(fileOffset + (makerNotePtr - tiffContent)), "TIFF tag #37500 (Maker Note)", path + "/TIFF:37500");
	}

	if (iptcPtr != 0) {
		DumpIPTC(iptcPtr, iptcLen, (fileOffset + (iptcPtr - tiffContent)), "TIFF tag #33723");
	}

	if (psirPtr != 0) {
		DumpImageResources(psirPtr, psirLen, (fileOffset + (psirPtr - tiffContent)), "TIFF tag #34377");
	}

	if (xmpPtr != 0) {
		DumpXMP(xmpPtr, xmpLen, (fileOffset + (xmpPtr - tiffContent)), "TIFF tag #700");
	}

	tree->popNode();
}	// DumpOneIFD

	// =================================================================================================


static void
DumpIFDChain(XMP_Uns8 * startPtr, XMP_Uns8 * endPtr,
	XMP_Uns8 * tiffContent, XMP_Uns32 fileOrigin, const char * label, std::string path, bool isHeaderAbsent)
{
	XMP_Uns8 * ifdPtr = startPtr;
	XMP_Uns32  ifdOffset = startPtr - tiffContent;

	if (isHeaderAbsent) // It's a kind of hack to iterate all the ifdboxes at least once.
		ifdOffset = 1;

	for (size_t ifdIndex = 0; ifdOffset != 0; ++ifdIndex) {

		if ((ifdPtr < tiffContent) || (ifdPtr >= endPtr)) {
			ifdOffset = fileOrigin + (ifdPtr - tiffContent);
			tree->comment("** Invalid IFD offset, %d (0x%X) tree.", ifdOffset, ifdOffset);
			return;
		}

		XMP_Uns16 fieldCount = TIFF_GetUns16(ifdPtr);
		DumpOneIFD(ifdIndex, ifdPtr, endPtr, tiffContent, fileOrigin, label, path);
		ifdOffset = TIFF_GetUns32(ifdPtr + 2 + (12 * fieldCount));
		ifdPtr = tiffContent + ifdOffset;

	}

}	// DumpIFDChain

	// =================================================================================================

static void
DumpTIFF(XMP_Uns8 * tiffContent, XMP_Uns32 tiffLen, XMP_Uns32 fileOffset, const char * label, std::string path, bool isHeaderAbsent)
{
	tree->pushNode("TIFF content from %s", label);
	// ! TIFF can be nested because of the Photoshop 6 weiredness. Save and restore the procs.
	GetUns16_Proc save_GetUns16 = TIFF_GetUns16;
	GetUns32_Proc save_GetUns32 = TIFF_GetUns32;
	GetUns64_Proc save_GetUns64 = TIFF_GetUns64;

	XMP_Uns32 ifdOffset = 0;

	if (!isHeaderAbsent)
	{
		if (CheckBytes(tiffContent, "II\x2A\x00", 4)) {
			beTIFF = false;
			TIFF_GetUns16 = GetUns16LE;
			TIFF_GetUns32 = GetUns32LE;
			TIFF_GetUns64 = GetUns64LE;
			tree->addComment("Little endian ");
		}
		else if (CheckBytes(tiffContent, "MM\x00\x2A", 4)) {
			beTIFF = true;
			TIFF_GetUns16 = GetUns16BE;
			TIFF_GetUns32 = GetUns32BE;
			TIFF_GetUns64 = GetUns64BE;
			tree->addComment("Big endian ");
		}
		else {
			tree->comment("** Missing TIFF image file header tree.");
			return;
		}

		tree->addComment("TIFF from %s, offset %d (0x%X), size %d", label, fileOffset, fileOffset, tiffLen);

		ifdOffset = TIFF_GetUns32(tiffContent + 4);
	}
	else
	{
		beTIFF = false;
		TIFF_GetUns16 = GetUns16LE;
		TIFF_GetUns32 = GetUns32LE;
		TIFF_GetUns64 = GetUns64LE;
		tree->addComment("Little endian ");
	}

	DumpIFDChain(tiffContent + ifdOffset, tiffContent + tiffLen, tiffContent, fileOffset, label, path, isHeaderAbsent);

	TIFF_GetUns16 = save_GetUns16;
	TIFF_GetUns32 = save_GetUns32;
	TIFF_GetUns64 = save_GetUns64;

	tree->popNode();
}	// DumpTIFF


	// =================================================================================================

static void DumpTIFF(const JpegMarkers& exifMarkers, XMP_Uns8 * dataStart, const char * label, std::string path)
{
	XMP_Uns32 i = 0, size = exifMarkers.size();
	std::string combinedExifData;
	tree->pushNode("Combined EXIF Markers from %s", path.c_str());
	for (i = 0; i < size; i++) {
		tree->pushNode("EXIF Marker %d", i + 1);
		tree->addComment("offset %d (0x%X), size %d", exifMarkers[i].jpegMarkerPtr - dataStart,
			exifMarkers[i].jpegMarkerPtr - dataStart, exifMarkers[i].jpegMarkerLen);
		combinedExifData.append((const char *)exifMarkers[i].jpegMarkerPtr, exifMarkers[i].jpegMarkerLen);
		tree->popNode();
	}

	DumpTIFF((XMP_Uns8 *)combinedExifData.data(), combinedExifData.length(), exifMarkers[0].jpegMarkerPtr - dataStart, label, path);

	tree->popNode();
}	// DumpTIFF

	// =================================================================================================

static void
DumpPhotoshop(XMP_Uns8 * psdContent, XMP_Uns32 psdLen)
{
	psdLen = psdLen;	// Avoid unused parameter warning.

	XMP_Uns32  psirOffset = 26 + 4 + GetUns32BE(psdContent + 26);
	XMP_Uns8 * psirSect = psdContent + psirOffset;
	XMP_Uns8 * psirPtr = psirSect + 4;
	XMP_Uns32  psirLen = GetUns32BE(psirSect);

	DumpImageResources(psirPtr, psirLen, (psirPtr - psdContent), "Photoshop file");

}	// DumpPhotoshop

	// =================================================================================================

static void
DumpJPEG(XMP_Uns8 * jpegContent, XMP_Uns32 jpegLen)
{
	XMP_Uns8 * endPtr = jpegContent + jpegLen;
	XMP_Uns8 * segPtr = jpegContent;
	XMP_Uns32  segOffset;

	XMP_Uns8 * xmpPtr = 0;
	XMP_Uns16 xmpLen = 0;

	JpegMarkers psirMarkers, exifMarkers;

	while (segPtr < endPtr) { // ----------------------------------------------------------------

		XMP_Uns16  segMark = GetUns16BE(segPtr);
		if (segMark == 0xFFFF) {
			segPtr += 1;	// Skip leading 0xFF pad byte.
			continue;
		}

		XMP_Uns16 minorKind = segMark & 0x000F;
		segOffset = segPtr - jpegContent;

		tree->pushNode("JPEG:%.4X", segMark);
		tree->addComment("offset %d (0x%X)", segOffset, segOffset);

		if (((segMark >> 8) != 0xFF) || (segMark == 0xFF00)) {
			tree->addComment("** invalid JPEG marker **");
			tree->popNode();
			break;
		}

		// Check for standalone markers first, only fetch the length for marker segments.

		if (segMark == 0xFF01) {
			tree->addComment("** TEM **");
			segPtr += 2;	// A standalone marker.
			tree->popNode();
			continue;
		}
		else if ((0xFFD0 <= segMark) && (segMark <= 0xFFD7)) {
			tree->addComment(fromArgs("RST%d  ** unexpected **", minorKind));
			segPtr += 2;	// A standalone marker.
			tree->popNode();
			continue;
		}
		else if (segMark == 0xFFD8) {
			tree->addComment("SOI");
			segPtr += 2;	// A standalone marker.
			tree->popNode();
			continue;
		}
		else if (segMark == 0xFFD9) {
			tree->addComment("EOI");
			segPtr += 2;	// A standalone marker.
			tree->popNode();
			break;	// Exit on EOI.
		}

		XMP_Uns16 segLen = GetUns16BE(segPtr + 2);

		// figure out Exif vs PSIR vs XMP
		if ((0xFFE0 <= segMark) && (segMark <= 0xFFEF)) {
			const char* segName = (const char *)(segPtr + 4);
			tree->addComment(fromArgs("size %d, APP%d, \"%s\"", segLen, minorKind, segName));
			if ((minorKind == 1) &&
				((memcmp(segName, "Exif\0\0", 6) == 0) || (memcmp(segName, "Exif\0\xFF", 6) == 0))) {
				tree->addComment("EXIF");
				tree->changeValue("EXIF");
				JpegMarker exifMarker;
				exifMarker.jpegMarkerPtr = segPtr + 4 + 6;
				exifMarker.jpegMarkerLen = segLen - 2 - 6;
				exifMarkers.push_back(exifMarker);
			}
			else if ((minorKind == 13) && (strcmp(segName, "Photoshop 3.0") == 0)) {
				tree->addComment("PSIR");
				tree->changeValue("PSIR");
				JpegMarker psirMarker;
				psirMarker.jpegMarkerPtr = segPtr + 4 + strlen(segName) + 1;
				psirMarker.jpegMarkerLen = (XMP_Uns16)(segLen - 2 - strlen(segName) - 1);
				psirMarkers.push_back(psirMarker);
			}
			else if ((minorKind == 1) && (strcmp(segName, "http://ns.adobe.com/xap/1.0/") == 0)) {
				tree->addComment("XMP");
				tree->changeValue("XMP");
				xmpPtr = segPtr + 4 + strlen(segName) + 1;
				xmpLen = (XMP_Uns16)(segLen - 2 - strlen(segName) - 1);
			}
			segPtr += 2 + segLen;
			tree->popNode();
			continue;
		}

		if (segMark == 0xFFDA) {
			tree->addComment(fromArgs("size %d, SOS", segLen));
			segPtr += 2 + segLen;	// Skip the SOS marker segment itself
			long rstCount = 0;
			while (segPtr < endPtr) {	// Skip the entropy-coded data and RSTn markers.
				if (*segPtr != 0xFF) {
					segPtr += 1;	// General data byte.
				}
				else {
					segMark = GetUns16BE(segPtr);
					if (segMark == 0xFF00) {
						segPtr += 2;	// Padded 0xFF data byte.
					}
					else if ((segMark < 0xFFD0) || (segMark > 0xFFD7)) {
						segLen = 0;
						segPtr -= 2;	// Prepare for the increment in the outer loop.
						break;	// Exit, non-RSTn marker.
					}
					else {
						++rstCount;
						segPtr += 2;
					}
				}
			}
			tree->addComment(fromArgs("%d restart markers", rstCount));

			segPtr += 2 + segLen;
			tree->popNode();
			continue;
		}


		if ((0xFF02 <= segMark) && (segMark <= 0xFFBF)) {
			tree->addComment(fromArgs("size %d, ** RES **", segLen));
		}
		else if ((0xFFC0 <= segMark) && (segMark <= 0xFFC3)) {
			tree->addComment(fromArgs("size %d, SOF%d", segLen, minorKind));
		}
		else if (segMark == 0xFFC4) {
			tree->addComment(fromArgs("size %d, DHT", segLen));
		}
		else if ((0xFFC5 <= segMark) && (segMark <= 0xFFC7)) {
			tree->addComment(fromArgs("size %d, SOF%d", segLen, minorKind));
		}
		else if (segMark == 0xFFC8) {
			tree->addComment(fromArgs("size %d, JPG", segLen));
		}
		else if ((0xFFC9 <= segMark) && (segMark <= 0xFFCB)) {
			tree->addComment(fromArgs("size %d, SOF%d", segLen, minorKind));
		}
		else if (segMark == 0xFFCC) {
			tree->addComment(fromArgs("size %d, DAC", segLen));
		}
		else if ((0xFFCD <= segMark) && (segMark <= 0xFFCF)) {
			tree->addComment(fromArgs("size %d, SOF%d", segLen, minorKind));
		}
		else if (segMark == 0xFFDB) {
			tree->addComment(fromArgs("size %d, DQT", segLen));
		}
		else if (segMark == 0xFFDC) {
			tree->addComment(fromArgs("size %d, DNL", segLen));
		}
		else if (segMark == 0xFFDD) {
			tree->addComment(fromArgs("size %d, DRI", segLen));
		}
		else if (segMark == 0xFFDE) {
			tree->addComment(fromArgs("size %d, DHP", segLen));
		}
		else if (segMark == 0xFFDF) {
			tree->addComment(fromArgs("size %d, EXP", segLen));
		}
		else if ((0xFFF0 <= segMark) && (segMark <= 0xFFFD)) {
			tree->addComment(fromArgs("size %d, JPG%d", segLen, minorKind));
		}
		else if (segMark == 0xFFFE) {
			tree->addComment(fromArgs("size %d, COM", segLen));
		}
		else {
			tree->addComment("** UNRECOGNIZED MARKER **");
		}

		segPtr += 2 + segLen;

		tree->popNode();
	} // ------------------------------------------------------------------------------------

	if (segPtr != endPtr) {
		segOffset = segPtr - jpegContent;
		tree->addComment(fromArgs(
			"** Unexpected end of JPEG markers at offset %d (0x%X), delta %d tree.",
			segOffset, segOffset, (long)(endPtr - segPtr)
		));
	}

	if (exifMarkers.size() > 0) DumpTIFF(exifMarkers, jpegContent, "JPEG Exif APP1", "JPEG:APP1");
	if (psirMarkers.size() > 0) DumpImageResources(psirMarkers, jpegContent, "JPEG Photoshop APP13");
	if (xmpPtr != 0) DumpXMP(xmpPtr, xmpLen, (xmpPtr - jpegContent), "JPEG XMP APP1");

}	// DumpJPEG

	// =================================================================================================
	//#if !IOS_ENV
static const XMP_Uns8 kUUID_XMP[16] =
{ 0xBE, 0x7A, 0xCF, 0xCB, 0x97, 0xA9, 0x42, 0xE8, 0x9C, 0x71, 0x99, 0x94, 0x91, 0xE3, 0xAF, 0xAC };
/*#else
static const XMP_Uns8 kUUID_XMP[16]  =
{ 0xFFFFFFBE, 0x0000007A, 0xFFFFFFCF, 0xFFFFFFCB, 0xFFFFFF97, 0xFFFFFFA9, 0x00000042, 0xFFFFFFE8, 0xFFFFFF9C, 0x00000071, 0xFFFFFF99, 0xFFFFFF94, 0xFFFFFF91, 0xFFFFFFE3, 0xFFFFFFAF, 0xFFFFFFAC };
#endif */
static const XMP_Uns8 kUUID_Exif[16] =
{ 0x05, 0x37, 0xCD, 0xAB, 0x9D, 0x0C, 0x44, 0x31, 0xA7, 0x2A, 0xFA, 0x56, 0x1F, 0x2A, 0x11, 0x3E };
static const XMP_Uns8 kUUID_IPTC[16] =
{ 0x09, 0xA1, 0x4E, 0x97, 0xC0, 0xB4, 0x42, 0xE0, 0xBE, 0xBF, 0x36, 0xDF, 0x6F, 0x0C, 0xE3, 0x6F };
static const XMP_Uns8 kUUID_PSIR[16] =
{ 0x2C, 0x4C, 0x01, 0x00, 0x85, 0x04, 0x40, 0xB9, 0xA0, 0x3E, 0x56, 0x21, 0x48, 0xD6, 0xDF, 0xEB };
// -------------------------------------------------------------------------------------------------

/**
* helper routine to get past the version and flags field...
*/
static void
digestISOFullBoxExtension(LFA_FileRef file, std::string isoPath, XMP_Int64& remainingSize, XMP_Uns8& version, XMP_Uns32& flags)
{
	version = LFA_ReadUns8(file);
	flags = 0;

	LFA_Read(file, &flags, 3, true); // read only 3 byte!
	flags = flags >> 8; // (move to bit 0-23)
	remainingSize -= 4;

	tree->setKeyValue(isoPath + "version", fromArgs("%d", version));
	tree->setKeyValue(isoPath + "flags", fromArgs("0x%.8X", flags));
}

static void
digestInternationalTextSequence(LFA_FileRef file, std::string isoPath, XMP_Int64* remainingSize)
{
	XMP_Int64 miniBoxStringSize = tree->digest16u(file, isoPath + "size", true, true);
	tree->digest16u(file, isoPath + "language code", true, true);
	(*remainingSize) -= 4;
	if ((*remainingSize) != miniBoxStringSize)
		tree->addComment("WARNING: boxSize and miniBoxSize differ!");
	tree->digestString(file, isoPath + "value", miniBoxStringSize, false);
}

/**
* dumps one *or several* (ohter while loop) ISO Boxes within the indicated space:
*
* maxBoxLen is :== fileLen on top-level, otherwise available length of outer box (exluding header size naturally)
*
* (NB: reading (and displaying) box types, compat brands and other 4-letter stuff
*  as LE is somehow easier (might need adjustment for PPC though)
*
* practices:
* compensate endianess using MakeUns32BE() prior to use as string, NOT for numeric compare
*/
static void
DumpISOBoxes(LFA_FileRef file, XMP_Uns32 maxBoxLen, std::string _isoPath)
{
	XMP_Int64 endOfThisLevel = LFA_Tell(file) + maxBoxLen;
	std::string origIsoPath(_isoPath);
	std::string isoPath(_isoPath);
	std::list<int> keys;
	while (LFA_Tell(file) < endOfThisLevel)
	{
		XMP_Int64 boxHeaderSize = 8;

		//assertMsg("No space for ISO box header", boxHeaderSize <= maxBoxLen ); 

		//// certainly not enough room for another box?
		// could be a 32bit zero trailing a udta
		// or, uhm, something garbage-ish...
		if (LFA_Tell(file) + boxHeaderSize > endOfThisLevel)
		{
			XMP_Int64 numUnusedBytes = (endOfThisLevel - LFA_Tell(file));
			tree->digestString(file, isoPath + "unused", numUnusedBytes, false);
			tree->addComment("'free' since too small for a box");

			bool ok;
			LFA_Seek(file, endOfThisLevel, SEEK_SET, &ok);
			assertMsg("skippind to-small space failed (truncated file?)", ok);
			continue; // could just as well: return
		}

		XMP_Int64 boxPos = LFA_Tell(file); // store here, output below
		XMP_Int64 boxSize = tree->digest32u(file, "", true); // NB: 32bit <- 64bit
		XMP_Uns32 boxType = tree->digest32u(file, "", false);

		switch (boxSize)
		{
		case 0:
			// A value of zero says that the box extends to the end of the file.
			boxSize = (maxBoxLen - boxPos); // *** could be errorneous below top-level
			break;
		case 1:
			// A value of 1 says that a 64-bit big endian size is written after the box type field, the data follows.
			boxSize = LFA_ReadUns64_BE(file);
			boxHeaderSize += 8;
			break;
		default:
			break;
		}

		XMP_Uns32 tempBoxType = GetUns32LE(&boxType);
		std::string boxString(fromArgs("%.4s", &tempBoxType));

		if (boxString.size() != 0)
		{
			// substitute mac-copyright signs with an easier-to-handle "(c)"
#if WIN_UNIVERSAL_ENV
			if (boxString.at(0) == 0xffffffffffffffa9)
#elif !IOS_ENV
			if (boxString.at(0) == 0xa9)
#else
			if (boxString.at(0) == 0xffffffa9)
#endif
				boxString = std::string("(c)") + boxString.substr(1);
		}
		else
			break;

		isoPath = origIsoPath + boxString + "/";

		// TEMP
		// Log::info("pushing %s, endOfThisLevel: 0x%X", isoPath.c_str(), endOfThisLevel );
		// printf ("%s \n", isoPath.c_str());
		tree->pushNode(isoPath);
#if ANDROID
		tree->addComment("offset 0x%llX, size 0x%llX", boxPos, boxSize);
#else
		tree->addComment("offset 0x%I64X, size 0x%I64X", boxPos, boxSize);
#endif


		// endOfBoxPos saves the hassle of keeping the remainingSize up-to-date
		// (which is only needed and only done, if usefull for the specific box)
		XMP_Int64 remainingSize = boxSize - boxHeaderSize;
		XMP_Int64 endOfBoxPos = LFA_Tell(file) + remainingSize;

		// ---------------------------------------------
		// for FullBoxes:
		XMP_Uns8 version = 255;
		XMP_Uns32 flags = 0xFFFFFF;

		switch (boxType)
		{
			// container boxes (FULL), that contain (relevant) boxes:
		case 0x6174656D: // meta, FULLBOX
			if (isoPath == "moov/udta/meta/")
			{
				digestISOFullBoxExtension(file, isoPath, remainingSize, version, flags);
				DumpISOBoxes(file, remainingSize, isoPath);
				break;
			}
			else if (isoPath == "moov/meta/")
			{
				DumpISOBoxes(file, remainingSize, isoPath);
				break;
			}
			else if (isoPath == "meta/") // for HEIF
			{
				digestISOFullBoxExtension(file, isoPath, remainingSize, version, flags);
				DumpISOBoxes(file, remainingSize, isoPath);
				break;
			}
			else
				break; //no area of interest (and navigate around some malformed files)

					   // container boxes (all non-FULL), that contain (relevant) boxes:
		case 0x666E6964: //dinf
			if (isoPath == "meta/dinf/")
			{
				bool ok;
				XMP_Int64 keep = LFA_Tell(file);
				DumpISOBoxes(file, remainingSize, isoPath);
				LFA_Seek(file, keep, SEEK_SET, &ok);
				assertMsg("seek failed", ok);
			}
			break;
		case 0x66657264: // dref
			digestISOFullBoxExtension(file, isoPath, remainingSize, version, flags);
			tree->digest32u(file, isoPath + "entry_count", true, true);
			DumpISOBoxes(file, remainingSize, isoPath);
			break;
		case 0x206C7275: // for 'url' field, for 'urn' support to be added later
			digestISOFullBoxExtension(file, isoPath, remainingSize, version, flags);
			if (remainingSize > 0)
			{
				tree->digestString(file, isoPath + "location", 0);
			}
			break;
		case 0x6D746970: //pitm
			digestISOFullBoxExtension(file, isoPath, remainingSize, version, flags);
			tree->digest16u(file, isoPath + "item_ID", true, true);
			break;
		case 0x666E6969: //iinf
			digestISOFullBoxExtension(file, isoPath, remainingSize, version, flags);
			if (version == 0)
			{
				tree->digest16u(file, isoPath + "entry_count", true, true);
			}
			else
			{
				tree->digest32u(file, isoPath + "entry_count", true, true);
			}
			
			DumpISOBoxes(file, remainingSize, isoPath);
			break;
		case 0x65666E69: //infe
			XMP_Uns16 item_id;
			digestISOFullBoxExtension(file, isoPath, remainingSize, version, flags);
			if ((version == 0) || (version == 1))
			{
				item_id = tree->digest16u(file, isoPath + "item_id", true, false);
				remainingSize -= 2;
				tree->digest16u(file, isoPath + "item_protection_index", true, true);
				remainingSize -= 2;
				item_name = tree->digestString(file, isoPath + "item_name", 0);
				if (0 == item_name.compare(0, 4, "Exif"))
				{
					exif_item_id = item_id;
				}
				if (0 == item_name.compare(0, 4, "mime"))
				{
					mime_item_id = item_id;
				}
				remainingSize -= (item_name.size() + 1);
				if (remainingSize == 0)
				{
					//there are no fields to process further
				}
				else if (remainingSize > 0)
				{
					//check for other fields here. If control reaches here, it means we have content_type field also
					content_type = tree->digestString(file, isoPath + "content_type", 0);
					remainingSize -= (content_type.size() + 1);
					if (remainingSize == 0)
					{
						//there are no fields to process further
					}
					else if (remainingSize > 0)
					{
						// we have content_encoding present also
						content_encoding = tree->digestString(file, isoPath + "content_encoding", 0);
					}
					else
					{
						//cout << "Some probem occurred when parsing infe box (content_encoding).Please check." << endl;
					}
					break;

				}
				else
				{
					//cout << "Some probem occurred when parsing infe box.Please check." << endl;
				}
			}
			if (version >= 2)
			{
				if (version == 2)
				{
					item_id = tree->digest16u(file, isoPath + "item_id", true, false);
					remainingSize -= 2;
				}
				else if (version == 3)
				{
					item_id = tree->digest32u(file, isoPath + "item_id", true, false);
					remainingSize -= 4;
				}
				tree->digest16u(file, isoPath + "item_protection_index", true, true);
				remainingSize -= 2;
				item_type=tree->digest32u(file, isoPath + "item_type", true, true);
				remainingSize -= 4;
				item_name = tree->digestString(file, isoPath + "item_name", 0);
				remainingSize -= (item_name.size() + 1);

				if (0x45786966 == item_type)
				{
					exif_item_id = item_id; 
				}
				if (0x6D696D65 == item_type)
				{
					mime_item_id = item_id;
				}
				else if (0x75726900 == item_type)
				{
					string item_uri_type=tree->digestString(file, isoPath + "item_uri_type", 0);
					remainingSize -= (item_uri_type.size() + 1);
				}


				if (remainingSize == 0)
				{
					//there are no fields to process further
				}
				else if (remainingSize > 0)
				{
					//check for other fields here. If control reaches here, it means we have content_type field also
					content_type = tree->digestString(file, isoPath + "content_type", 0);
					remainingSize -= (content_type.size() + 1);
					if (remainingSize == 0)
					{
						//there are no fields to process further
					}
					else if (remainingSize > 0)
					{
						// we have content_encoding present also
						content_encoding = tree->digestString(file, isoPath + "content_encoding", 0);
					}
					else
					{
						cout << "Some probem occurred when parsing infe box (content_encoding).Please check." << endl;
					}
				}
			}

			//Get the data for exif - using value from exif_item_id
			// Dump the exif metadata here
			{
				if (exif_item_id != 0 && tree->hasNode("meta/iloc/item[" + to_string(exif_item_id) + "]/extent_offset"))
				{
					bool ok;
					XMP_Int64 keep = LFA_Tell(file);
					XMP_Uns32 offset = stoi(tree->getValue("meta/iloc/item[" + to_string(exif_item_id) + "]/extent_offset"));	// Here tiff stream starts after 16476 ( 16388 + 84 + 4 ) bytes from starting of PANA atom's content
																																//XMP_Uns8 *ExifSize = (XMP_Uns8*)malloc(4);
																																//Now point file to offset value
					LFA_Seek(file, offset, SEEK_SET, &ok);
					XMP_Uns32 blocksize = LFA_ReadUns32_BE(file);
					LFA_Seek(file, offset + 4 + blocksize, SEEK_SET, &ok);
					XMP_Uns64 tiffLength1 = (stoi(tree->getValue("meta/iloc/item[" + to_string(exif_item_id) + "]/extent_length")));
					XMP_Uns64 tiffLength = (stoi(tree->getValue("meta/iloc/item[" + to_string(exif_item_id) + "]/extent_length")) - blocksize - 4);
					XMP_Uns8 *tiffContent = (XMP_Uns8*)malloc(tiffLength);
					LFA_Read(file, tiffContent, tiffLength, true);
					DumpTIFF(tiffContent, tiffLength, offset, "HEIF Exif", "HEIF:Exif");
					LFA_Seek(file, keep, SEEK_SET, &ok);
					assertMsg("seek failed", ok);
					exif_item_id = NULL;
				}
			}
			//Get the data for xmp - using value from mime_item_id
			// Dump the xmp metadata here
			{
				if (mime_item_id != 0  && tree->hasNode("meta/iloc/item[" + to_string(mime_item_id) + "]/extent_offset"))
				{
					bool ok;
					XMP_Int64 keep = LFA_Tell(file);
					XMP_Uns32 offset = stoi(tree->getValue("meta/iloc/item[" + to_string(mime_item_id) + "]/extent_offset"));
					XMP_Uns64 xmpLength = stoi(tree->getValue("meta/iloc/item[" + to_string(mime_item_id) + "]/extent_length"));
					LFA_Seek(file, offset, SEEK_SET, &ok);
					XMP_Uns8 *xmpContent = (XMP_Uns8*)malloc(xmpLength);
					LFA_Read(file, xmpContent, xmpLength, true);
					DumpXMP(xmpContent, xmpLength, offset, "XMP");
					LFA_Seek(file, keep, SEEK_SET, &ok);
					assertMsg("seek failed", ok);
					mime_item_id = NULL;
				}
			}
			break;
		case 0x636F6C69: //iloc
			XMP_Uns16 deriveValues, item_count, extent_count;
			XMP_Uns8 offset_size, length_size, base_offset_size, index_size;
			digestISOFullBoxExtension(file, isoPath, remainingSize, version, flags);
			// Read the contents here, do not add them to tree, move the seek to 16bits forward
			deriveValues = LFA_ReadInt16_BE(file);
			//add these values in the tree
			offset_size = (deriveValues & 0xF000) >> 12;
			length_size = (deriveValues & 0x0F00) >> 8;
			base_offset_size = (deriveValues & 0x00F0) >> 4;
			tree->setKeyValue(isoPath + "offset_size", fromArgs("%d", offset_size));
			tree->setKeyValue(isoPath + "length_size", fromArgs("%d", length_size));
			tree->setKeyValue(isoPath + "base_offset_size", fromArgs("%d", base_offset_size));
			if (version == 1 || version == 2)
			{
				index_size = (deriveValues & 0x000F);
				tree->setKeyValue(isoPath + "index_size", fromArgs("%d", index_size));
			}
			else
				index_size = 0;


			if (version < 2)
			{
				item_count = tree->digest16u(file, isoPath + "item_count", true, true);
			}
			else if (version == 2) {
				item_count = tree->digest32u(file, isoPath + "item_count", true, true);
			}
			for (int i = 0; i < item_count; i++)
			{
				XMP_Uns32 item_id;
				bool ok;
				XMP_Int64 keep = LFA_Tell(file);
				if (version < 2)
				{
					item_id = LFA_ReadUns16_BE(file);
				}
				else if (version == 2) {
					item_id = LFA_ReadUns32_BE(file);
				}
				LFA_Seek(file, keep, SEEK_SET, &ok);
				assertMsg("seek failed", ok);
				
				string modifiedisoPath = isoPath + "item[" + to_string(item_id) + "]/";
				if (version < 2)
				{
					item_id=tree->digest16u(file, modifiedisoPath + "item_ID", true, true);
				}
				else if (version == 2) {
					item_id=tree->digest32u(file, modifiedisoPath + "item_ID", true, true);
				}

				if (version == 1 || version == 2)
				{
					//construction method
					XMP_Uns16 deriveConstructionMethod = LFA_ReadInt16_BE(file);
					XMP_Uns8 constructionMethod;
					constructionMethod = (deriveConstructionMethod & 0xF000) >> 12;
					tree->setKeyValue(isoPath + "construction_method", fromArgs("%d", constructionMethod));
				}
				tree->digest16u(file, modifiedisoPath + "data_reference_index", true, true);
				if (base_offset_size != 0)
				{
					if (base_offset_size == 4) {
						tree->digest32u(file, modifiedisoPath + "base_offset", true, true);
					}
					else if (base_offset_size == 8)
					{
						tree->digest64u(file, modifiedisoPath + "base_offset", true, true);
					}
					else
					{
						cout << "wrong value of base_offset received!! It should be either 0 or 4 or 8" << endl;
					}
				}

				extent_count = tree->digest16u(file, modifiedisoPath + "extent_count", true, false);
				for (int j = 0; j < extent_count; j++)
				{
					if ((version == 1 || version == 2) && (index_size > 0))
					{

						if (index_size == 4) {
							tree->digest32u(file, modifiedisoPath + "index_size", true, false);
						}
						else if (index_size == 8)
						{
							tree->digest64u(file, modifiedisoPath + "index_size", true, false);
						}
						else
						{
							cout << "wrong value of index_size received!! It should be either 0 or 4 or 8" << endl;
						}
					}

					if (offset_size > 0)
					{
						if (offset_size == 4) {
							tree->digest32u(file, modifiedisoPath + "extent_offset", true, false);
						}
						else if (offset_size == 8)
						{
							tree->digest64u(file, modifiedisoPath + "extent_offset", true, false);
						}
						else
						{
							cout << "wrong value of offset_size received!! It should be either 0 or 4 or 8" << endl;
						}
					}

					if (length_size > 0)
					{
						if (length_size == 4) {
							tree->digest32u(file, modifiedisoPath + "extent_length", true, false);
						}
						else if (length_size == 8)
						{
							tree->digest64u(file, modifiedisoPath + "extent_length", true, false);
						}
						else
						{
							cout << "wrong value of length_size received!! It should be either 0 or 4 or 8" << endl;
						}
					}

				}
			}
			//Get the data for exif - using value from exif_item_id
			// Dump the exif metadata here
			{
				if (exif_item_id != 0 && tree->hasNode("meta/iloc/item[" + to_string(exif_item_id) + "]/extent_offset"))
				{
					bool ok;
					XMP_Int64 keep = LFA_Tell(file);
					XMP_Uns32 offset = stoi(tree->getValue("meta/iloc/item[" + to_string(exif_item_id) + "]/extent_offset"));	// Here tiff stream starts after 16476 ( 16388 + 84 + 4 ) bytes from starting of PANA atom's content
																																//XMP_Uns8 *ExifSize = (XMP_Uns8*)malloc(4);
																																//Now point file to offset value
					LFA_Seek(file, offset, SEEK_SET, &ok);
					XMP_Uns32 blocksize = LFA_ReadUns32_BE(file);
					LFA_Seek(file, offset + 4 + blocksize, SEEK_SET, &ok);
					XMP_Uns64 tiffLength1 = (stoi(tree->getValue("meta/iloc/item[" + to_string(exif_item_id) + "]/extent_length")));
					XMP_Uns64 tiffLength = (stoi(tree->getValue("meta/iloc/item[" + to_string(exif_item_id) + "]/extent_length")) - blocksize - 4);
					XMP_Uns8 *tiffContent = (XMP_Uns8*)malloc(tiffLength);
					LFA_Read(file, tiffContent, tiffLength, true);
					DumpTIFF(tiffContent, tiffLength, offset, "HEIF Exif", "HEIF:Exif");
					LFA_Seek(file, keep, SEEK_SET, &ok);
					assertMsg("seek failed", ok);
					exif_item_id = NULL;
				}
			}
			//Get the data for xmp - using value from mime_item_id
			// Dump the xmp metadata here
			{
				if (mime_item_id != 0 && tree->hasNode("meta/iloc/item[" + to_string(exif_item_id) + "]/extent_offset"))
				{
					bool ok;
					XMP_Int64 keep = LFA_Tell(file);
					XMP_Uns32 offset = stoi(tree->getValue("meta/iloc/item[" + to_string(mime_item_id) + "]/extent_offset"));
					XMP_Uns64 xmpLength = stoi(tree->getValue("meta/iloc/item[" + to_string(mime_item_id) + "]/extent_length"));
					LFA_Seek(file, offset, SEEK_SET, &ok);
					XMP_Uns8 *xmpContent = (XMP_Uns8*)malloc(xmpLength);
					LFA_Read(file, xmpContent, xmpLength, true);
					DumpXMP(xmpContent, xmpLength, offset, "XMP");
					LFA_Seek(file, keep, SEEK_SET, &ok);
					assertMsg("seek failed", ok);
					mime_item_id = NULL;
				}
			}

			break;
		case 0x66657269: //iref
			digestISOFullBoxExtension(file, isoPath, remainingSize, version, flags);
			DumpISOBoxes(file, remainingSize, isoPath);
			break;
		case 0x676D6964: //dimg
			XMP_Uns16 refCount;
			tree->digest16u(file, isoPath + "from_item_ID", true, true);
			refCount = tree->digest16u(file, isoPath + "reference_count", true, true);
			for (int i = 0; i < refCount; i++)
			{
				tree->digest16u(file, isoPath + "to_item_ID", true, true);
			}
			break;
		case 0x626D6874: //thmb
			XMP_Uns16 refCount_thmb;
			tree->digest16u(file, isoPath + "from_item_ID", true, true);
			refCount_thmb = tree->digest16u(file, isoPath + "reference_count", true, true);
			for (int i = 0; i < refCount_thmb; i++)
			{
				tree->digest16u(file, isoPath + "to_item_ID", true, true);
			}
			break;
		case 0x63736463: //cdsc
			XMP_Uns16 refCount_cdsc;
			tree->digest16u(file, isoPath + "from_item_ID", true, true);
			refCount_cdsc = tree->digest16u(file, isoPath + "reference_count", true, true);
			for (int i = 0; i < refCount_cdsc; i++)
			{
				tree->digest16u(file, isoPath + "to_item_ID", true, true);
			}
			DumpISOBoxes(file, remainingSize, isoPath);
			break;
		case 0x70727069: //iprp
			DumpISOBoxes(file, remainingSize, isoPath);
			break;
			/*case 0x6F637069: //ipco
			DumpISOBoxes(file, remainingSize, isoPath);
			break;
			case 0x726c6f63: //colr
			XMP_Uns32 colour_type;
			colour_type=tree->digest32u(file, isoPath + "colour_type", true, true);
			if (colour_type == 'nclx')
			{
			tree->digest16u(file, isoPath + "colour_primaries", true, true);
			tree->digest16u(file, isoPath + "transfer_characteristics", true, true);
			tree->digest16u(file, isoPath + "matrix_coefficients", true, true);
			XMP_Uns8 full_range_flag = LFA_ReadUns8(file);
			full_range_flag = ((full_range_flag & 0xF) >> 7);
			tree->setKeyValue(isoPath + "full_range_flag", fromArgs("%d", full_range_flag));
			}
			else if (colour_type == 'rICC')
			{

			}
			else if (colour_type == 'prof')
			{
			//unrestricted icc profile handling

			}

			break;*/
		case 0x666E696D: // minf - "simple container, no direct content"
			if (boxString == "minf" && isoPath != "moov/trak/mdia/minf/")
				break;
		case 0x6C627473: // stbl is a simple container, no direct content
		{
			TimeCodeTrack = false; // assume until we known better by a relevant
								   // moov/trak/mdia/minf/stbl/stsd/ of format tmcd
			if (boxString == "stbl" && isoPath != "moov/trak/mdia/minf/stbl/")
				break;
		}
		case 0x766F6F6D: // moov
		case 0x6169646D: // mdia
		case 0x61746475: // udta - user data
		case 0x6B617274: // trak - track
		case 0x74736C69: // ilst (contains cprt box)
		{
			// store and restore current position to not depend
			// on sub-level mischief...
			if (isoPath == "moov/meta/ilst/")
			{
				while (remainingSize > 0)
				{
					XMP_Uns32 metaItemSize = tree->digest32u(file, "", true);
					XMP_Uns32 keyIndex = tree->digest32u(file, "", true);
					string key_name = ISOMetaKeys[keyIndex - 1];
					string temp_Path = isoPath + key_name + "/";
					metaItemSize -= 4 + 4;
					DumpISOBoxes(file, metaItemSize, temp_Path);
					remainingSize -= (metaItemSize + 8);
				}
			}
			else
			{
				bool ok;
				XMP_Int64 keep = LFA_Tell(file);
				DumpISOBoxes(file, remainingSize, isoPath);
				LFA_Seek(file, keep, SEEK_SET, &ok);
				assertMsg("seek failed", ok);
			}
		}
		break;

		// known boxes, that need content extraction
		case 0x70797466: // ftyp - file type
		{
			XMP_Uns32 majorBrand = LFA_ReadUns32_LE(file);
			XMP_Uns32 minorVersion = LFA_ReadUns32_LE(file);

			//data has been read in LE make it in BE
			majorBrand = GetUns32LE(&majorBrand);
			minorVersion = GetUns32LE(&minorVersion);

			//Log::info( fromArgs( "major Brand:   '%.4s' (0x%.8X)" , &majorBrand, MakeUns32BE(majorBrand) ));
			//Log::info( fromArgs( "minor Version: 0x%.8X" , MakeUns32BE(minorVersion) ) );
			tree->setKeyValue(isoPath + "majorBrand",
				fromArgs("%.4s", &majorBrand),
				fromArgs("0x%.8X", MakeUns32BE(majorBrand)));
			tree->setKeyValue(isoPath + "minorVersion",
				fromArgs("0x%.8X", MakeUns32BE(minorVersion)));

			remainingSize -= 4 + 4;
			//Log::info( fromArgs( "remaining Size: %d" , remainingSize ) );

			while (remainingSize >= 4)
			{
				LFA_ReadUns32_LE(file);
				// TODO: Concatenate for KeyValue...
				//XMP_Uns32 compatVersion = LFA_ReadUns32_LE( file );
				//Log::info( fromArgs( "compatible brand: '%.4s' (0x%.8X)" , &compatVersion, MakeUns32BE(compatVersion) ));
				remainingSize -= 4;
			}

			// odd bytes left?
			if (remainingSize > 0)
				tree->addComment("WARNING: %d bytes left, considering FREE", remainingSize);

		}
		break;
		case 0x61746164: // data (within itunes Metadata)
						 // all data atoms start with two common fields: a type indicator, and a locale indicator.
						 // each of these fields is four bytes long:
			tree->digest32u(file, isoPath + "type", true, true);
			tree->digest32u(file, isoPath + "locale", true, true);
			remainingSize -= 8;
			// rest is actual contents:
			tree->digestString(file, isoPath + "value", remainingSize, false);
			break;
		case 0x64697575: // uuid
			XMP_Uns8 uid[16];
			tree->digest(file, isoPath + "uuidValue", uid, 16);
			if (!strncmp((const char*)kUUID_XMP, (const char*)uid, 16))
				tree->addComment(" - the XMP UUID !");

			break;

		case 0x65657266: // free
			tree->addComment("free space");
			break;

			// FULL BOXES (w/o container boxes, above) **********************************
		case 0x6468766D: // mvhd, FULLBOX, movie-header-box
		{
			digestISOFullBoxExtension(file, isoPath, remainingSize, version, flags);
			if (version == 1)
			{
				tree->digest64u(file, isoPath + "creation_time", true, true);
				tree->digest64u(file, isoPath + "modification_time", true, true);
				tree->digest32u(file, isoPath + "timescale", true, true);
				tree->digest64u(file, isoPath + "duration", true, true);
			}
			else if (version == 0)
			{
				tree->digest32u(file, isoPath + "creation_time", true, true);
				tree->digest32u(file, isoPath + "modification_time", true, true);
				tree->digest32u(file, isoPath + "timescale", true, true);
				tree->digest32u(file, isoPath + "duration", true, true);
			}
			else
			{
				tree->addComment("WARNING: unknown mvhd version!");
			}
			// COULDDO more fields, but not needed right now.
		}
		break;
		case 0x726C6468: // hdlr - handler reference
		{
			if (isoPath == "moov/trak/mdia/hdlr/")
			{
				if (remainingSize < 4 * 4)
					break; // box too small...

				digestISOFullBoxExtension(file, isoPath, remainingSize, version, flags);

				tree->digestString(file, isoPath + "quickTimeType", 4, false);	// expecting: 'mhlr' - media handler
				tree->digestString(file, isoPath + "subType", 4, false);		// expecting: 'tmcd' - timecode
				tree->digestString(file, isoPath + "manufacturer", 4, false);		// e.g. 'appl'
				break;
			}
			else if (isoPath == "moov/meta/hdlr/")
			{
				assertMsg("hdlr: version and flags must be zero", 0 == tree->digest32u(file, "", true));
				LFA_Seek(file, 4, SEEK_CUR);

				XMP_Uns32 hndlrTyp = LFA_ReadUns32_BE(file);
				if (hndlrTyp == 0x6D647461)
					tree->addComment("Handler Type = mdta");
				else
					break;

				remainingSize -= 12;
				XMP_Uns32 pos = LFA_Tell(file);
				LFA_Seek(file, pos + remainingSize, SEEK_SET);
				break;
			}
			else if (isoPath == "meta/hdlr/")
			{
				digestISOFullBoxExtension(file, isoPath, remainingSize, version, flags);
				LFA_Seek(file, 4, SEEK_CUR);
				tree->digestString(file, isoPath + "handler_type", 4, false);
				break;
			}
			// rest doesn't bother us...
		}
		case 0x64737473: // stsd - timecode sample description table
		{
			if (isoPath != "moov/trak/mdia/minf/stbl/stsd/")
				break;

			// version (1 byte), flags (3 byte) - must be 0
			assertMsg("stbl: version and flags must be zero", 0 == tree->digest32u(file, "", true));
			// entryCount - must be 0
			assertMsg("stbl: at least one entry needed", 1 <= tree->digest32u(file, "", true));
			remainingSize -= 8;

			// only dump first occurence 
			// ensure there's enough bytes for at least on stbl:
			if (remainingSize < 29)
				break;	// MPEG4/al_sbr_twi_22_1_fsaac22.mv4 has a box that is smaller than this...
						// hence can only break, not throw
			XMP_Int64 entrySize = (XMP_Int64)tree->digest32u(file, "", true, true);
			std::string format = tree->digestString(file, "", 4, false); // must be 'tmcd'
			tree->pushNode(isoPath + format + "/");
			if (format != "tmcd")
			{
				tree->addComment("irrelevant node");
				tree->popNode();
				break; // different party...
			}

			TimeCodeTrack = true; // we're in the right track

			Skip(file, 6); // [6] reserved bytes
			tree->digest16u(file, isoPath + "dataReferenceIndex", true, true); // (ignored)
			tree->addOffset(file);
			Skip(file, 4); // uint32 reserved
			tree->digest32u(file, isoPath + "flags", true, true);
			tree->digest32u(file, isoPath + "timeScale", true, true);
			tree->digest32u(file, isoPath + "frameDuration", true, true);
			Skip(file, 2); // skip ignored frame count, reserved

						   // ////////////////////////////// dig out 'trailing boxes'
						   // comparing "atom remains" vs. "entry" (probably must be '>='

						   // deduct the already digested...
			entrySize -= 34; // ( 4+4+6+2+4+4+4+4+1+1 )
			remainingSize -= 34; // (the atom-level value)

			assertMsg("entry Size must be 0 or positive", entrySize >= 0);
			assertMsg("must not overreach atom", entrySize <= remainingSize);

			XMP_Int64 endOfTrailingBoxes = LFA_Tell(file) + remainingSize;
			while (LFA_Tell(file) < endOfTrailingBoxes)
			{
				LFA_Tell(file);
				DumpISOBoxes(file, entrySize, isoPath);
				LFA_Tell(file);
			}

			assertMsg("did not boil down to zero", LFA_Tell(file) == endOfTrailingBoxes);

			tree->popNode();
			break;
		}

		case 0x63737473: // stsc - timecode sample description table
		{
			if (isoPath != "moov/trak/mdia/minf/stbl/stsc/")
				break;
			if (!TimeCodeTrack)
			{
				tree->addComment("not tcmd -> not of interest");
				break; // not of interest
			}

			// version (1 byte), flags (3 byte) - must be 0
			assertMsg("stbl: version and flags must be zero", 0 == tree->digest32u(file, "", true));
			// entryCount - must be 0
			assertMsg("stbl: at least one entry needed", 1 <= tree->digest32u(file, "", true));
			remainingSize -= 8;

			LFA_Tell(file);

			tree->digest32u(file, isoPath + "firstChunkNo", true, true);
			tree->digest32u(file, isoPath + "numSamplesPerChunk", true, true);
			tree->digest32u(file, isoPath + "sampleDescriptionID", true, true);

			break;
		}

		case 0x6F637473: // stco - timecode sample description table
		{
			if (isoPath != "moov/trak/mdia/minf/stbl/stco/")
				break;
			if (!TimeCodeTrack)
				break; // not of interest

					   // version (1 byte), flags (3 byte) - must be 0
			assertMsg("stbl: version and flags must be zero", 0 == tree->digest32u(file, "", true));
			// entryCount - must be 0
			assertMsg("stbl: at least one entry needed", 1 <= tree->digest32u(file, "", true));
			remainingSize -= 8;

			XMP_Int64 absOffset = tree->digest32u(file, isoPath + "absFileOffset32", true, true);

			// recklessly navigate to that timecode media sample, grab value, return to old position...
			XMP_Int64 oldPos = LFA_Tell(file);
			LFA_Seek(file, absOffset, SEEK_SET, 0);
			tree->digest32u(file, isoPath + "timecodeMediaSample", true, true);
			LFA_Seek(file, oldPos, SEEK_SET, 0);

		}
		case 0x34366F63: // co64 - timecode sample description table -> 64 bit offset
		{
			if (isoPath != "moov/trak/mdia/minf/stbl/co64/")
				break;

			tree->digest64u(file, isoPath + "absFileOffset64", true, true);
			break;
		}

		case 0x74727063: // cprt, FULLBOX
			if (isoPath == "moov/udta/cprt/" || isoPath == "moov/uuid/udta/cprt/")
			{

				digestISOFullBoxExtension(file, isoPath, remainingSize, version, flags);

				// 1/8 byte ISO language padding = 1-bit value set to 0
				// 1 7/8 bytes content language = 3 * 5-bits ISO 639-2 language code less 0x60
				// - example code for english = 0x15C7
				tree->digest16u(file, isoPath + "content language", true, true);
				tree->addComment("(0x15C7 == english)");
				// zero-terminated, actual string:
				tree->digestString(file, isoPath + "value", 0);
			}
			else
			{
				// ISO - copyright (?)					
				// a container box, hunt for 'data' atom by recursion:
				bool ok;
				XMP_Int64 keep = LFA_Tell(file);
				DumpISOBoxes(file, remainingSize, isoPath);
				LFA_Seek(file, keep, SEEK_SET, &ok);
				assertMsg("seek failed", ok);
			}
			break;
		case 0x64686B74: // tkhd, FULLBOX
		case 0x6E61656D: // mean, FULLBOX
		case 0x656D616E: // name, FULLBOX
		{
			if (isoPath == "moov/trak/mdia/minf/stbl/stsd/name/") // this regrettably is a diffrent animal (international text sequence)
				digestInternationalTextSequence(file, isoPath, &remainingSize);
			else
				digestISOFullBoxExtension(file, isoPath, remainingSize, version, flags);
		}
		break;
		case 0x7379656B:    //moov/meta/keys 
		{
			LFA_Seek(file, 4, SEEK_CUR);
			XMP_Uns32 keysCount = tree->digest32u(file, "", true);
			tree->addComment("Number of Keys = %d", keysCount);
			XMP_Uns32 index;
			for (index = 1; index <= keysCount; index++)
			{
				XMP_Uns32 keySize = tree->digest32u(file, "", true);
				keySize -= 4 + 4;  // 4 bytes of key_size and key_namespace
				string key_namespace = tree->digestString(file, "", 4);
				if ((strcmp(key_namespace.c_str(), "mdta") == 0) || (strcmp(key_namespace.c_str(), "udta") == 0))
				{
					string key_value = tree->digestString(file, "", keySize);
					ISOMetaKeys.push_back(key_value);
					tree->pushNode(isoPath + key_value);
					tree->addComment("Key Namespace = %s", key_namespace.c_str());
					tree->popNode();
				}
				else
					LFA_Seek(file, keySize, SEEK_CUR);
			}
		}
		break;
			// (c)-style quicktime boxes and boxes of no interest:
		default:
			if ((boxType & 0xA9) == 0xA9) // (c)something
			{
					if (0 == isoPath.compare(0, 10, "moov/udta/"))
					{ // => Quicktime metadata "international text sequence" ( size, language code, value )
						digestInternationalTextSequence(file, isoPath, &remainingSize);
					}
					else
					{
						tree->addComment("WARNING: unknown flavor of (c)*** boxes, neither QT nor iTunes");
					}
				break;
			}
			//boxes of no interest:
			break;
		}

		bool ok;
		LFA_Seek(file, endOfBoxPos, SEEK_SET, &ok);
		assertMsg("End-of-Box Seek failed (truncated file?)", ok);

		tree->popNode();

	} // while

} // DumpISOBoxes()

  // attempt to combine dumping of mpeg-4 and quicktime (mov) into one routine...
static void
DumpISO(LFA_FileRef file, XMP_Uns32 fileLen)
{
	TimeCodeTrack = false;

	// see specificition at https://zerowing.corp.adobe.com/display/XMP/Embedding+Spec+MPEG4
	DumpISOBoxes(file, fileLen, "");
	assertMsg("truncated file/last box reached beyond end?", LFA_Tell(file) == fileLen);
}

// =================================================================================================

static size_t GetASFObjectInfo(LFA_FileRef file, XMP_Uns32 objOffset, ASF_ObjHeader* objHeader, size_t nesting)
{
	LFA_Seek(file, objOffset, SEEK_SET);
	LFA_Read(file, objHeader, 24, true);

	objHeader->size = GetUns64LE(&objHeader->size);
	XMP_Uns32 size32 = (XMP_Uns32)objHeader->size;

	if (objHeader->size > 0xFFFFFFFF) {
		tree->addComment("** ASF Object at offset 0x%X is over 4GB: 0x%.8X 0x%.8X",
			objOffset, High32(objHeader->size), Low32(objHeader->size));
	}

	size_t infoIndex;
	for (infoIndex = 0; kASF_KnownObjects[infoIndex].name != 0; ++infoIndex) {
		if (memcmp(&objHeader->guid, &kASF_KnownObjects[infoIndex].guid, 16) == 0) break;
	}

	std::string indent(3 * nesting, ' ');

	if (kASF_KnownObjects[infoIndex].name != 0) {
		tree->addComment("%s   %s Object, offset %u (0x%X), size %u",
			indent.c_str(), kASF_KnownObjects[infoIndex].name, objOffset, objOffset, size32);
	}
	else {
		tree->addComment("%s   <<unknown object>>, offset %u (0x%X), size %u",
			indent.c_str(), objOffset, objOffset, size32);
		ASF_GUID guid;
		guid.part1 = GetUns32LE(&objHeader->guid.part1);
		guid.part2 = GetUns16LE(&objHeader->guid.part2);
		guid.part3 = GetUns16LE(&objHeader->guid.part3);
		guid.part4 = GetUns16LE(&objHeader->guid.part4);
		tree->addComment("GUID %.8X-%.4X-%.4X-%.4X-%.4X%.8X",
			guid.part1, guid.part2, guid.part3, guid.part4,
			*(XMP_Uns16*)(&guid.part5[0]), *(XMP_Uns32*)(&guid.part5[2]));
	}

	if (objOffset != 0) tree->addComment("");	// Don't print newline for the real header.

	return infoIndex;

}	// GetASFObjectInfo

	// =================================================================================================

static void PrinfASF_UTF16(LFA_FileRef file, XMP_Uns16 byteCount, const char * label)
{
	size_t filePos = LFA_Tell(file);
	//FNO: note: has sideeffect on sDataPtr
	CaptureFileData(file, 0, byteCount);

	tree->setKeyValue(
		label,
		convert16Bit(false, sDataPtr, false, byteCount),
		fromArgs("offset %d (0x%X), size %d", filePos, filePos, byteCount)
	);
}

static void DumpASFFileProperties(LFA_FileRef file, XMP_Uns32 objOffset, XMP_Uns32 objLen)
{
	ASF_FileProperties fileProps;

	if (objLen < kASF_FilePropertiesSize) {
		tree->comment("** File Properties Object is too short");
		return;
	}

	LFA_Seek(file, objOffset, SEEK_SET);
	LFA_Read(file, &fileProps, kASF_FilePropertiesSize, true);

	fileProps.flags = GetUns32LE(&fileProps.flags);	// Only care about flags and create date.
	fileProps.creationDate = GetUns64LE(&fileProps.creationDate);

	bool bcast = (bool)(fileProps.flags & 1);
	tree->setKeyValue("ASF:broadcast",
		(bcast ? "set" : "not set")
	);

	XMP_Int64 totalSecs = fileProps.creationDate / (10 * 1000 * 1000);
	XMP_Int32 nanoSec = ((XMP_Int32)(fileProps.creationDate - (totalSecs * 10 * 1000 * 1000))) * 100;
	XMP_Int32 days = (XMP_Int32)(totalSecs / 86400);
	totalSecs -= ((XMP_Int64)days * 86400);
	XMP_Int32 hour = (XMP_Int32)(totalSecs / 3600);
	totalSecs -= ((XMP_Int64)hour * 3600);
	XMP_Int32 minute = (XMP_Int32)(totalSecs / 60);
	totalSecs -= ((XMP_Int64)minute * 60);
	XMP_Int32 second = (XMP_Int32)totalSecs;
	XMP_DateTime binDate;
	memset(&binDate, 0, sizeof(binDate));

	binDate.year = 1601;
	binDate.month = 1;
	binDate.day = 1;

	binDate.day += days;
	binDate.hour = hour;
	binDate.minute = minute;
	binDate.second = second;
	binDate.nanoSecond = nanoSec;

	SXMPUtils::ConvertToUTCTime(&binDate);
	std::string strDate;
	SXMPUtils::ConvertFromDate(binDate, &strDate);

	tree->setKeyValue("ASF:creation date",
		fromArgs("%s (0x%.8X-%.8X)",
			strDate.c_str(), High32(fileProps.creationDate), Low32(fileProps.creationDate))
	);
}	// DumpASFFileProperties

	// =================================================================================================

static void DumpASFContentDescription(LFA_FileRef file, XMP_Uns32 objOffset, XMP_Uns32 objLen)
{
	ASF_ContentDescription contentDesc;	// ! The lengths are in bytes.

	if (objLen < kASF_ContentDescriptionSize) {
		tree->comment("** Content Description Object is too short");
		return;
	}

	LFA_Seek(file, objOffset, SEEK_SET);
	LFA_Read(file, &contentDesc, kASF_ContentDescriptionSize, true);

	contentDesc.titleLen = GetUns16LE(&contentDesc.titleLen);
	contentDesc.authorLen = GetUns16LE(&contentDesc.authorLen);
	contentDesc.copyrightLen = GetUns16LE(&contentDesc.copyrightLen);
	contentDesc.descriptionLen = GetUns16LE(&contentDesc.descriptionLen);
	contentDesc.ratingLen = GetUns16LE(&contentDesc.ratingLen);

	PrinfASF_UTF16(file, contentDesc.titleLen, "ASF:title");
	PrinfASF_UTF16(file, contentDesc.authorLen, "ASF:author");
	PrinfASF_UTF16(file, contentDesc.copyrightLen, "ASF:copyright");
	PrinfASF_UTF16(file, contentDesc.descriptionLen, "ASF:description");
	PrinfASF_UTF16(file, contentDesc.ratingLen, "ASF:rating");

}	// DumpASFContentDescription

	// =================================================================================================

static void DumpASFContentBranding(LFA_FileRef file, XMP_Uns32 objOffset, XMP_Uns32 objLen)
{
	XMP_Uns32 fieldSize;

	if (objLen < (16 + 8 + 4 * 4)) {
		tree->comment("** Content Branding Object is too short");
		return;
	}

	XMP_Uns32 fieldOffset = objOffset + 16 + 8 + 4;
	LFA_Seek(file, fieldOffset, SEEK_SET);

	LFA_Read(file, &fieldSize, 4, true);
	fieldSize = GetUns32LE(&fieldSize);
	fieldOffset += fieldSize;
	LFA_Seek(file, fieldSize, SEEK_CUR);	// Skip the banner data.

	LFA_Read(file, &fieldSize, 4, true);
	fieldSize = GetUns32LE(&fieldSize);
	fieldOffset += fieldSize;
	tree->setKeyValue("ASF:banner URL",
		"",
		fromArgs("offset %d (0x%X), size %d", fieldOffset, fieldOffset, fieldSize)
	);

	if (fieldSize != 0) {
		CaptureFileData(file, 0, fieldSize);
		//NB: not yet tested..., not sure if stopOnNull needed, thus using false
		tree->changeValue(convert8Bit(sDataPtr, false, fieldSize));
	}

	LFA_Read(file, &fieldSize, 4, true);
	fieldSize = GetUns32LE(&fieldSize);
	fieldOffset += fieldSize;
	tree->setKeyValue("ASF:copyright URL",
		"",
		fromArgs("offset %d (0x%X), size %d", fieldOffset, fieldOffset, fieldSize)
	);

	if (fieldSize != 0) {
		CaptureFileData(file, 0, fieldSize);
		//NB: not yet tested..., not sure if stopOnNull needed, thus using false
		tree->changeValue(convert8Bit(sDataPtr, false, fieldSize));
	}

}	// DumpASFContentBranding

	// =================================================================================================

static void DumpASFContentEncryption(LFA_FileRef file, XMP_Uns32 objOffset, XMP_Uns32 objLen)
{
	XMP_Uns32 fieldSize;

	if (objLen < (16 + 8 + 4 * 4)) {
		tree->addComment("** Content Encryption Object is too short");
		return;
	}

	LFA_Seek(file, (objOffset + 16 + 8), SEEK_SET);

	LFA_Read(file, &fieldSize, 4, true);
	fieldSize = GetUns32LE(&fieldSize);
	LFA_Seek(file, fieldSize, SEEK_CUR);	// Skip the secret data.

	LFA_Read(file, &fieldSize, 4, true);
	fieldSize = GetUns32LE(&fieldSize);
	LFA_Seek(file, fieldSize, SEEK_CUR);	// Skip the protection type.

	LFA_Read(file, &fieldSize, 4, true);
	fieldSize = GetUns32LE(&fieldSize);
	CaptureFileData(file, 0, fieldSize);
	PrintOnlyASCII_8(sDataPtr, fieldSize);

	LFA_Read(file, &fieldSize, 4, true);
	fieldSize = GetUns32LE(&fieldSize);
	CaptureFileData(file, 0, fieldSize);
	PrintOnlyASCII_8(sDataPtr, fieldSize);

}	// DumpASFContentEncryption

	// =================================================================================================

static void DumpASFHeaderExtension(LFA_FileRef file, XMP_Uns32 extOffset, XMP_Uns32 extLen)
{
	// The Header Extension Object is a child of the Header Object and the parent of nested objects.

	XMP_Uns32 extEnd = extOffset + extLen;

	XMP_Uns32 childLen;
	XMP_Uns32 childOffset;

	ASF_ObjHeader childHeader;

	for (childOffset = (extOffset + kASF_HeaderExtensionSize); childOffset < extEnd; childOffset += childLen) {
		(void)GetASFObjectInfo(file, childOffset, &childHeader, 2);
		childLen = (XMP_Uns32)childHeader.size;
	}

	if (childOffset != extEnd) {
		tree->addComment("** Invalid end to nested Header Extension objects, offset %u", childOffset);
	}

}	// DumpASFHeaderExtension

	// =================================================================================================

static void
DumpASF(LFA_FileRef file, XMP_Uns32 asfLen)
{
	// An ASF file contains objects of the form:
	//   A 16 byte GUID giving the object's type and use
	//   A little endian 64-bit object length, includes the GUID and length (== 24 + data size)
	//   The object's data
	// Objects can be nested. The top level of a file is a Header Object, followed by a Data Object,
	// followed by any number of "other" top level objects, followed by any number of index objects.
	// There is legacy metadata in certain nested objects within the Header Object. The XMP is an
	// "other" top level object.

	size_t infoIndex;

	XMP_Uns32  objLen;
	XMP_Uns32  objOffset;
	ASF_ObjHeader objHeader;

	tree->comment("ASF file Object layout");

	// Dump the Header Object's content, looking for legacy metadata.
	infoIndex = GetASFObjectInfo(file, 0, &objHeader, 0);
	XMP_Uns32 headerLen = (XMP_Uns32)objHeader.size;

	if (kASF_KnownObjects[infoIndex].kind != kASFObj_Header) {
		tree->comment("** First object is not the Header Object");
		return;
	}

	XMP_Uns32 nestedCount;
	LFA_Seek(file, 24, SEEK_SET);
	LFA_Read(file, &nestedCount, 4, true);
	nestedCount = GetUns32LE(&nestedCount);
	tree->addComment("%u nested objects", nestedCount);

	for (objOffset = (16 + 8 + 4 + 2); objOffset < headerLen; objOffset += objLen, --nestedCount) {
		infoIndex = GetASFObjectInfo(file, objOffset, &objHeader, 1);
		objLen = (XMP_Uns32)objHeader.size;

		switch (kASF_KnownObjects[infoIndex].kind) {
		case kASFObj_FileProperties:
			DumpASFFileProperties(file, objOffset, objLen);
			break;
		case kASFObj_ContentDesc:
			DumpASFContentDescription(file, objOffset, objLen);
			break;
		case kASFObj_ContentBrand:
			DumpASFContentBranding(file, objOffset, objLen);
			break;
		case kASFObj_ContentEncrypt:
			DumpASFContentEncryption(file, objOffset, objLen);
			break;
		case kASFObj_HeaderExtension:
			DumpASFHeaderExtension(file, objOffset, objLen);
			break;
		default:
			break;
		}
	}

	if ((objOffset != headerLen) || (nestedCount != 0)) {
		tree->comment("** Invalid end to nested Header objects, offset %u, count %u",
			objOffset, nestedCount);
		objOffset = headerLen;
	}

	// Dump the basic info for the remaining objects, looking for the XMP along the way.
	infoIndex = GetASFObjectInfo(file, objOffset, &objHeader, 0);
	objLen = (XMP_Uns32)objHeader.size;

	if (kASF_KnownObjects[infoIndex].kind != kASFObj_Data) {
		tree->addComment("** Second object is not the Data Object");
		if (kASF_KnownObjects[infoIndex].kind == kASFObj_XMP) {
			if (sXMPPtr == 0)
				CaptureXMPF(file, (objOffset + 24), (objLen - 24));
			else
				tree->addComment("** Multiple XMP objects");
		}
	}

	for (objOffset += objLen; objOffset < asfLen; objOffset += objLen) {
		GetASFObjectInfo(file, objOffset, &objHeader, 0);
		objLen = (XMP_Uns32)objHeader.size;

		if (kASF_KnownObjects[infoIndex].kind == kASFObj_XMP) {
			if (sXMPPtr == 0)
				CaptureXMPF(file, (objOffset + 24), (objLen - 24));
			else
				tree->addComment("** Multiple XMP objects");
		}
	}

	if (objOffset != asfLen) tree->addComment("** Invalid end to top level objects: %u", objOffset);
	if (sXMPPtr != 0) DumpXMP("ASF XMP object");

}	// DumpASF

	// =================================================================================================

static void
DumpUCF(LFA_FileRef file, XMP_Int64 len)
{
	//////////////////////////////////////////////////////////////////////
	// constants
	const static XMP_Uns32 UCF_HS_CONTENTFILE = 0x04034b50;
	const static XMP_Uns32 UCF_CD_FILE_HEADER = 0x02014b50;		//central directory - file header
	const static XMP_Uns32 UCF_ZIP64_END_OF_CD_RECORD = 0x06064b50;
	const static XMP_Uns32 UCF_ZIP64_END_OF_CD_LOCATOR = 0x07064b50;
	const static XMP_Uns32 UCF_CD_END = 0x06054b50;

	const static XMP_Int32 UCF_COMMENT_MAX = 0xFFFF;
	const static XMP_Int32 UCF_EOD_FIXED_SIZE = 22;

	const static XMP_Int32 UCF_ZIP64_LOCATOR_FIXED_SIZE = 20;
	const static XMP_Int32 UCF_ZIP64_RECORD_FIXED_SIZE = 56;

	//////////////////////////////////////////////////////////////////////
	// variables:
	XMP_Int64 curPos = 0;
	bool isZip64;

	XMP_Uns32 numDirEntries;

	XMP_Uns64 size_CD;
	XMP_Uns64 offset_Zip64_EndOfCD_Record = 0;
	XMP_Uns64 offset_CD;

	typedef std::list<XMP_Int64> OffsetStack;
	OffsetStack contentFiles;
	contentFiles.clear(); //precaution for mac

						  //////////////////////////////////////////////////////////////////////
						  // prolog:
	tree->comment("len is 0x%I64X", len);
	tree->comment("inherently parsing bottom-up");
	if (len > 0xFFFFFFFFl)
		tree->comment("info: >4GB ==> most like zip64 !");

	//////////////////////////////////////////////////////////////////////
	// parse bottom up:

	/////////////////////////////////////////////////////////////////////
	// zip comment:
	XMP_Int32 zipCommentLen = 0;
	for (; zipCommentLen <= UCF_COMMENT_MAX; zipCommentLen++)
	{
		LFA_Seek(file, -zipCommentLen - 2, SEEK_END);
		if (LFA_ReadUns16_LE(file) == zipCommentLen)	//found it?
		{
			//double check, might just look like comment length (actually be 'evil' comment)
			LFA_Seek(file, -UCF_EOD_FIXED_SIZE, SEEK_CUR);
			if (LFA_ReadUns32_LE(file) == UCF_CD_END) break; //heureka, directory ID
															 // 'else': just go on
		}
	}
	tree->comment(fromArgs("zip Comment length: %d", zipCommentLen));

	//was it a break or just not found ?
	assertMsg("zip broken near end or invalid comment", zipCommentLen < UCF_COMMENT_MAX);

	/////////////////////////////////////////////////////////////////////
	// End of CDR:
	LFA_Seek(file, -UCF_EOD_FIXED_SIZE - zipCommentLen, SEEK_END);
	curPos = LFA_Tell(file);
	tree->pushNode("End of Central Directory");
	tree->addOffset(file);
	{
		assertMsg("expected 'end of central directory record'", UCF_CD_END == tree->digest32u(file));
		assertMsg("UCF allow single-volume zips only", 0 == tree->digest16u(file)); //volume number (0,1,..)
		assertMsg("UCF allow single-volume zips only(thus directory must be in 0)", 0 == tree->digest16u(file, "")); //volume number (0,1,..)

		numDirEntries = tree->digest16u(file, "number of directory entries");
		tree->digest16u(numDirEntries, file, "number of total directory entries");

		size_CD = tree->digest32u(file, "size of central directory", false, true);
		offset_CD = tree->digest32u(file, "offset of central directory", false, true);
		if (offset_CD == 0xFFFFFFFF) tree->addComment("apparently zip-64");

		XMP_Uns16 zipCommentLengReverify = tree->digest16u(file, "zip comment length");
		assertMsg("zipCommentLengReverify failed", zipCommentLengReverify == zipCommentLen);
	}
	tree->popNode();
	/////////////////////////////////////////////////////////////////////
	// Zip64 End Of CD Locator
	LFA_Seek(file, curPos - UCF_ZIP64_LOCATOR_FIXED_SIZE, SEEK_SET);

	//tree->comment("offset is %X", LFA_Tell(file) );
	//tree->comment("peek is %X", Peek32u(file) );

	if (Peek32u(file) != UCF_ZIP64_END_OF_CD_LOCATOR)
	{
		tree->comment("no Zip64 CDL -> no Zip64");
		assertMsg("offset FFFF FFFF indicates zip-64, but no Zip64 CDL found", offset_CD != 0xFFFFFFFF);
		isZip64 = false;
	}
	else
	{
		isZip64 = true;
		tree->pushNode("Zip64 End-Of-CD Locator");
		tree->addOffset(file);

		tree->digest32u(file, "sig", false, true);
		assertMsg("'numOfDisk with central start dir' must be 0",
			0 == tree->digest32u(file, "disk with start dir"));
		tree->digest64u(&offset_Zip64_EndOfCD_Record, file, "Zip64 End Of CD Offset", false, true);

		tree->digest32u( /* deactived while bug #1742179:   1,*/ file, "total num of disks", false, true);
		tree->popNode();
	}

	/////////////////////////////////////////////////////////////////////
	// Zip64 End of CD Record
	if (isZip64)
	{
		XMP_Uns64 size_Zip64_EndOfCD_Record;
		tree->pushNode("Zip64 End of CD Record");
		LFA_Seek(file, offset_Zip64_EndOfCD_Record, SEEK_SET);
		tree->addOffset(file);

		tree->digest32u(UCF_ZIP64_END_OF_CD_RECORD, file, "sig", false, true);
		tree->digest64u(&size_Zip64_EndOfCD_Record, file, "size of zip64 CDR", false, true);
		tree->digest16u(file, "made by", false, true);
		tree->digest16u(file, "needed to extract", false, true);
		tree->digest32u((XMP_Uns32)0, file, "number of this disk", false, true);
		tree->digest32u((XMP_Uns32)0, file, "disk that contains start of CD", false, true);
		tree->digest64u((XMP_Uns64)numDirEntries, file, "total Num of Entries This Disk", false, false);
		tree->digest64u((XMP_Uns64)numDirEntries, file, "total Num of Entries", false, false);
		//TODO assert agtainst each other and above
		tree->digest64u(&size_CD, file, "size_CD", false, true);
		tree->digest64u(&offset_CD, file, "offset_CD", false, true);
		XMP_Int64 lenExtensibleSector = UCF_ZIP64_RECORD_FIXED_SIZE - size_Zip64_EndOfCD_Record;
		tree->comment("zip64 extensible data sector (%d bytes)", lenExtensibleSector);
		//sanity test:
		Skip(file, lenExtensibleSector);
		assertMsg("numbers don't add up", Peek32u(file) != UCF_ZIP64_END_OF_CD_LOCATOR);

		tree->popNode();
	}

	/////////////////////////////////////////////////////////////////////
	// parse Central directory structure: content file 1..n
	tree->pushNode("Central directory structure:");
	LFA_Seek(file, offset_CD, SEEK_SET);
	tree->addOffset(file);

	for (XMP_Uns32 contentFileNo = 1; contentFileNo <= numDirEntries; contentFileNo++)
	{
		tree->pushNode("File Header No %d:", contentFileNo);
		tree->addOffset(file);

		XMP_Uns16 version, flags, cmethod;
		XMP_Uns32 crc, compressed_size, uncompressed_size32;
		XMP_Uns64 offsetLocalHeader = 0;
		bool usesDescriptionHeader;

		tree->digest32u(UCF_CD_FILE_HEADER, file, "sig", false, true);

		tree->digest16u(file, "version made by", false, true);
		tree->digest16u(&version, file, "version needed to extract");
		assertMsg(fromArgs("illegal 'version needed to extract' (must be 10,20 or 45, was %u)", version),
			(version == 10 || version == 20 || version == 45));

		tree->digest16u(&flags, file, "general purpose bit flags", false, true);
		assertMsg("no zip encryption must be used", (flags & 0x1) == 0);
		usesDescriptionHeader = ((flags & 0x8) != 0);
		if (usesDescriptionHeader) tree->addComment("uses description header");

		tree->digest16u(&cmethod, file, "compression method");
		assertMsg("illegal compression method (must be 0 or 8(flate))!", (cmethod == 0 || cmethod == 8));

		tree->digest(file, "last mod file time", 0, 2);
		tree->digest(file, "last mod file date", 0, 2);

		tree->digest32u(&crc, file); //crc-32
		tree->digest32u(&compressed_size, file, "compressed size");
		tree->digest32u(&uncompressed_size32, file, "uncompressed size");

		XMP_Uns16 size_filename, size_extra, size_comment;

		tree->digest16u(&size_filename, file, "size filename");
		assertMsg("unusual name length length (broken file?)", size_filename>0 && size_filename < 500); //discover parsing nonsense...
		tree->digest16u(&size_extra, file, "size extra field");
		tree->digest16u(&size_comment, file, "size file comment");
		tree->digest16u((XMP_Uns16)0, file, "disk start no");

		tree->digest16u(file, "internal attribs");
		tree->digest32u(file, "external attribs");

		offsetLocalHeader = tree->digest32u(file, "relative offset local header", false, true); // Int64 <== Uns32

																								// name of file, optional relative path, strictly forward slashes.
		assert(size_filename != 0);
		std::string filename = tree->digestString(file, "filename", size_filename); //NOT zero-terminated

		if (contentFileNo == 1)
		{
			assert(size_extra == 0); //spec guarantes mimetype content at 38 <=> extraFieldLen == 0
			assertMsg(
				fromArgs("first file in UCF must be called mimetype, was %s", filename.c_str()),
				(size_filename == 8) && (filename == "mimetype"));
		}

		if (size_extra != 0)
		{
			tree->pushNode("extraField");
			XMP_Int32 remaining = size_extra;
			while (remaining > 0)
			{
				assertMsg("need 4 bytes for next header ID+len", remaining >= 4);
				XMP_Uns16 headerID = tree->digest16u(file, "headerID", false, true);
				XMP_Uns16 dataSize = tree->digest16u(file, "data size", false, true);
				remaining -= 4;
				assertMsg("actual field lenght not given", remaining >= dataSize);
				if (headerID == 0x1) //we only care about "Zip64 extended information extra field"
				{
					tree->digest64u(&offsetLocalHeader, file, "64bit offset", false, true);
					remaining -= 8;
				}
				else
				{
					Skip(file, dataSize);
					remaining -= dataSize;
				}

			}
			tree->popNode();
		}

		//now that regular 32 bit and zip-64 is through...
		if (contentFileNo == 1)
		{
			assertMsg("first header offset (aka content file offset) must (naturally) be 0", offsetLocalHeader == 0);
		}
		else
		{
			assertMsg("local header offset (aka content file offset) must not be 0", offsetLocalHeader != 0);
		}
		contentFiles.push_back(offsetLocalHeader);

		if (size_comment != 0)
		{
			tree->digest(file, "file comment", 0, size_comment);
		}
		tree->popNode(); //file header
	}
	tree->popNode(); //central directory structure

					 /////////////////////////////////////////////////////////////////////
					 // Content Files (incl. Headers, etc)
	for (XMP_Uns16 cfNo = 1; cfNo <= numDirEntries; cfNo++)
	{
		//vars
		XMP_Uns32 compressed_size, uncompressed_size, crc32;
		XMP_Uns16 version, nameLen, extraFieldLen;

		tree->pushNode("Content File %d:", cfNo);

		assert(!contentFiles.empty());
		XMP_Int64 fileHeaderOffset = contentFiles.front();
		contentFiles.pop_front();

		bool ok;
		LFA_Seek(file, fileHeaderOffset, SEEK_SET, &ok);
		tree->addOffset(file);
		assert(ok);

		//local file header
		tree->digest32u(UCF_HS_CONTENTFILE, file, "sig", false, true);
		tree->digest16u(&version, file, "version");
		assertMsg("illegal 'version needed to extract' (must be 10,20 or 45, was %u)",
			(version == 10 || version == 20 || version == 45));
		Skip(file, 8);
		tree->digest32u(&crc32, file, "crc-32", false, true);
		tree->digest32u(&compressed_size, file, "compressed", false, false);
		tree->digest32u(&uncompressed_size, file, "uncompressed", false, false);
		tree->digest16u(&nameLen, file, "file name length", false, false);
		tree->digest16u(&extraFieldLen, file, "extra field length", false, false);

		assert(nameLen != 0);
		assertMsg("unusual name length length (broken file?)", nameLen>0 && nameLen < 500); //discover parsing nonsense...

																							// name of file, optional relative path, strictly forward slashes.
		std::string filename = tree->digestString(file, "filename", nameLen); //NOT zero-terminated
		if (cfNo == 1)
		{
			assertMsg("first file in UCF muste be called mimetype", filename == "mimetype");
			assert(extraFieldLen == 0); //spec guarantes mimetype content at 38 <=> extraFieldLen == 0
			assert(LFA_Tell(file) == 38);
			tree->digestString(file, "file data (mimetype)", compressed_size);
		}
		else //not the first mimetype file thing
		{
			// FILE DATA =============================================================
			if (extraFieldLen != 0)		// may indeed not exist, and lenght=0 must not be passed into digestString()
				tree->digestString(file, "extra field", extraFieldLen); //NOT zero-terminated
			tree->setKeyValue("file data", "", fromArgs("skipping %u bytes", compressed_size));
			Skip(file, compressed_size);
		}
		tree->popNode();
	}

	tree->pushNode("");
	tree->popNode();
}	// DumpUCF

	// =================================================================================================

	// AVI and WAV files are RIFF based. Although they have usage differences, we can have a common
	// dumper. This might need changes for other RIFF-based files, e.g. for specific legacy
	// metadata. RIFF is a chunky format. AVI and WAV have an outermost RIFF chunk. The XMP is in a
	// top level "_PMX" chunk. Legacy metadata for WAV is in a top level LIST/INFO chunk.  Legacy
	// metadata for AVI is in a variety of places, don't have specs at present. Free space can be
	// JUNK or JUNQ.
	//
	// A RIFF chunk contains:
	//   - A 4 byte chunk ID, typically ASCII letters
	//   - A little endian UInt32 size of the chunk data
	//   - The chunk data
	//	 - Pad byte if the chunk data length is odd (added on 2007-03-22)

	// The ID is written in "reading order", e.g. the 'R' in "RIFF" is first in the file. Chunks
	// must start on even offsets. A pad byte of 0 is written after the data if necessary. The size
	// does not include the pad, nor the ID and size fields. Some chunks contain nested chunks,
	// notably the RIFF and LIST chunks do. These have the layout:
	//   - A 4 byte chunk ID, typically ASCII letters
	//   - A little endian UInt32 size of the chunk data
	//   - A 4 byte usage ID, typically ASCII letters
	//   - The nested chunks

	// reads maxSize bytes from file (not "up to", exactly fullSize)
	// puts it into a string, sets respective tree property
static void setFixedBEXTField(LFA_FileRef file, std::string propName, XMP_Int64 fullSize)
{
	char* descriptionBuffer = new char[fullSize + 2];
	LFA_Read(file, descriptionBuffer, fullSize, true);
	descriptionBuffer[fullSize] = '\0'; // tack on, in case not contained
										// parse till first \0
	std::string description(descriptionBuffer);
	tree->setKeyValue(propName, description);
	delete[] descriptionBuffer;
}

struct ChunkSize64 // declare ChunkSize64 structure
{
	XMP_Uns32 chunkId; // chunk ID (i.e. "big1" - this chunk is a big one)
	XMP_Uns64 chunkSize; //
};

struct DataSize64Chunk // declare DataSize64Chunk structure
{
	XMP_Uns32 chunkId; // ds64
	XMP_Uns32 chunkSize; // 4 byte size of the ds64 chunk
	XMP_Uns64 riffSize; // size of RF64 block
	XMP_Uns64 dataSize; // size of data chunk
	XMP_Uns64 sampleCount; // sample count of fact chunk	
	XMP_Uns32 tableLength; // number of valid entries in array "table"
	std::vector< ChunkSize64 > table;
};



static XMP_Uns64 parseRF64(LFA_FileRef file, DataSize64Chunk* rf64Sizes)
{
	XMP_Int64 chunkPos = LFA_Tell(file);
	rf64Sizes->chunkId = tree->digest32u(file, "", false);
	std::string ds64ChunkID_ST(fromArgs("%.4s", &rf64Sizes->chunkId));
	assertMsg("Not a valid RF64 file!", ds64ChunkID_ST == "ds64");

	rf64Sizes->chunkSize = tree->digest32u(file, "", false);

	XMP_Uns32 bitCnt = 0;
	rf64Sizes->riffSize = tree->digest64u(file, "", false);
	rf64Sizes->dataSize = tree->digest64u(file, "", false);
	rf64Sizes->sampleCount = tree->digest64u(file, "", false);

	rf64Sizes->tableLength = tree->digest32u(file, "", false);

	bitCnt = 28;

	for (XMP_Uns32 i = 0; i < rf64Sizes->tableLength; i++)
	{
		ChunkSize64 tmp;
		tmp.chunkId = tree->digest32u(file, "", false);
		tmp.chunkSize = tree->digest64u(file, "", false);
		rf64Sizes->table.push_back(tmp);

		bitCnt += 12;
	}

	// is there a rest to skip?
	XMP_Uns32 rest = rf64Sizes->chunkSize - bitCnt;
	if (rest != 0)
	{
		Skip(file, rest);
	}
	// return correct RIFF size
	return rf64Sizes->riffSize;

}

static XMP_Uns64 getRealSize(bool isOutermost, std::string chunkID, LFA_FileRef file, DataSize64Chunk* sizeChunk)
{
	if (isOutermost)
	{
		return parseRF64(file, sizeChunk);
	}
	else
	{
		if (chunkID == "data")
		{
			return sizeChunk->dataSize;
		}
		else
		{
			// search table
			for (XMP_Uns32 i = 0; i < sizeChunk->tableLength; i++)
			{
				std::string idString(fromArgs("%.4s", &sizeChunk->table[i].chunkId));
				if (idString == chunkID)
				{
					return sizeChunk->table[i].chunkSize;
				}
			}
		}
	}
	return 0;
}

static void
DumpRIFFChunk(LFA_FileRef file, XMP_Int64 parentEnd, std::string origChunkPath, bool bigEndian = false, DataSize64Chunk* rf64Sizes = NULL)
{

	while (LFA_Tell(file) < parentEnd)
	{
		bool isOutermost = origChunkPath.empty();

		XMP_Int64 chunkPos = LFA_Tell(file);
		XMP_Int64 fileSize = LFA_Measure(file);
		XMP_Int64 fileTail = fileSize - chunkPos;

		if (fileTail < 8)
		{
			tree->pushNode("** unknown bytes **");
			tree->addOffset(file);
			tree->addComment("size: 0x%llX", fileTail);
			Skip(file, fileTail);	// Already read the 8 byte header.
			tree->popNode();
		}
		else
		{
			XMP_Uns32 tmp = tree->digest32u(file, "", true);
			XMP_Uns32 chunkID = GetUns32BE(&tmp); // flip if necessary for LE systems

			std::string idString(fromArgs("%.4s", &chunkID));

			XMP_Int64 chunkSizeWOHeader = tree->digest32u(file, "", bigEndian);
			XMP_Int64 validRF64Header = chunkSizeWOHeader;
			XMP_Uns32 chunkType = 0;
			std::string typeString = "";
			// only RIFF and LIST contain subchunks...
			bool hasSubChunks = (idString == "RIFF") || (idString == "RF64") || (idString == "FORM") || (idString == "LIST") || (idString == "APPL");

			if (hasSubChunks)
			{
				XMP_Uns32 tmp = tree->digest32u(file, "", true);
				chunkType = GetUns32BE(&tmp); // flip if necessary for LE systems
				typeString = fromArgs("%.4s", &chunkType);
			}
			//get inner ID 'type' as in 'listType', 'fileType', ...
			//XMP_Uns32 chunkType = tree->digest32u( file );

			if (chunkSizeWOHeader == 0xFFFFFFFF)  //RF64 size for children
			{

				chunkSizeWOHeader = getRealSize(isOutermost, idString, file, rf64Sizes);
			}

			XMP_Int64 chunkSize = chunkSizeWOHeader + 8;// NB: XMPInt64 <- XMPUns32
														//adding size of id and length field itself

														// calculate size if size field seems broken
			if (chunkSize > parentEnd)
				chunkSize = parentEnd - chunkPos;


			std::string chunkPath = isOutermost ? (idString) : (origChunkPath + "/" + idString);


			// check special case of trailing bytes not in a valid RIFF structure
			if (isOutermost && idString != "RIFF"&& idString != "FORM" && idString != "RF64")
			{
				//dump undefined bytes till the end of the file
				tree->pushNode("** unknown bytes **");
				chunkSize = parentEnd - chunkPos; // get size through calculation (and not from size bytes) 
				tree->addComment("offset 0x%llX, size: 0x%llX", chunkPos, chunkSize);
				Skip(file, chunkSize - 8);	// Already read the 8 byte header.
				tree->popNode();
			}
			else
			{

				bool skipper = false;
				if (hasSubChunks)
				{
					if (isOutermost)
					{
						assertMsg("level-0 chunk must be AVI, AVIX, WAVE, AIFF, AIFC",
							(typeString == "AVI ") || (typeString == "AVIX") || (typeString == "WAVE")
							|| (typeString == "AIFF") || (typeString == "AIFC"));
					}

					chunkPath = chunkPath + ":" + typeString;
					XMP_Uns64 dsHeaderSize = 0xFFFFFFFF;
					if (chunkSize != fileSize && idString == "RF64")
					{
						XMP_Uns32 tmp = tree->digest32u(file, "", true);
						XMP_Uns32 chunkID = GetUns32BE(&tmp); // flip if necessary for LE systems

						std::string dsString(fromArgs("%.4s", &chunkID));
						if (dsString == "ds64")
						{
							//skip 4 bytes to read size of file in ds64 header
							tmp = tree->digest32u(file, "", true);
							XMP_Uns64 dsHeader = tree->digest64u(file, "", true);
							dsHeaderSize = GetUns64BE(&dsHeader);
						}
						tree->pushNode(chunkPath);
						tree->addComment("offset 0x%llX, size 0x%llX, size(w/o header) 0x%llX", chunkPos, chunkSize, chunkSizeWOHeader);

						tree->setKeyValue("dsChunkSize", fromArgs("0x%llX", dsHeaderSize + 8));

					}
					else
					{
						tree->pushNode(chunkPath);
						tree->addComment("offset 0x%llX, size 0x%llX, size(w/o header) 0x%llX", chunkPos, chunkSize, chunkSizeWOHeader);

					}
					tree->setKeyValue("fileSize", fromArgs("0x%llX", fileSize));
					tree->setKeyValue("validRF64Header", fromArgs("0x%llX", validRF64Header));


					if (isOutermost && idString == "RF64")
					{
						tree->pushNode("RF64/ds64");
						tree->addComment("offset 0x%llX, size 0x%X, size(w/o header) 0x%X", chunkPos + 12, rf64Sizes->chunkSize + 8, rf64Sizes->chunkSize);
						tree->setKeyValue("riffSize", fromArgs("0x%llX", rf64Sizes->riffSize));
						tree->setKeyValue("dataSize", fromArgs("0x%llX", rf64Sizes->dataSize));
						tree->setKeyValue("sampleCount", fromArgs("0x%llX", rf64Sizes->sampleCount));
						tree->setKeyValue("tableLength", fromArgs("0x%X", rf64Sizes->tableLength));
						tree->popNode();

						DumpRIFFChunk(file, LFA_Tell(file) + chunkSize - 12 - rf64Sizes->chunkSize - 8 /* filesize + riff chunk size - riff header(12) - rf64 header(8) */, chunkPath, bigEndian, rf64Sizes);	// recurse!
					}
					if ((idString + ":" + typeString == "LIST:INFO") ||
						(idString + ":" + typeString == "LIST:Tdat") ||
						(idString + ":" + typeString == "RIFF:AVI ") ||
						(idString + ":" + typeString == "RIFF:AVIX") ||
						(idString + ":" + typeString == "RIFF:WAVE") ||
						(idString + ":" + typeString == "FORM:AIFF") ||
						(idString + ":" + typeString == "FORM:AIFC") ||
						(idString + ":" + typeString == "LIST:hdrl") ||
						(idString + ":" + typeString == "LIST:strl") ||
						(idString + ":" + typeString == "LIST:movi")
						)
					{
						DumpRIFFChunk(file, LFA_Tell(file) + chunkSize - 12, chunkPath, bigEndian, rf64Sizes);	// recurse!
					}
					else
					{
						Skip(file, chunkSize - 12); // skip it !
					}
					tree->popNode();
				}
				else if (idString.length() == 4) // check that we got a valid idString
				{

					// now that LIST:movi gets dumped,
					// skip some very frequent, irrelevant chunks, 
					// otherwise the dump becomes unusably long...
					std::string firstTwo = idString.substr(0, 2);
					std::string secondTwo = idString.substr(2, 2);
					if (secondTwo == "db" || secondTwo == "dc" || secondTwo == "wb") // nb: _could_ colidde, requiring additional numeric test
					{
						skipper = true;
					}

					if (!skipper)
					{
						tree->pushNode(chunkPath);
						//Log::info( chunkPath );
						tree->addComment("offset 0x%llX, size 0x%llX, size(w/o header) 0x%llX", chunkPos, chunkSize, chunkSizeWOHeader);
					}

					// tackle chunks of interest //////////////////////////////////////////////
					bool isListInfo =
						((origChunkPath == "RIFF:WAVE/LIST:INFO" || origChunkPath == "RIFF:AVI /LIST:INFO")
							&& idString.at(0) == 'I'); // so far all mapping relevant props begin with "I"

					bool isListTdat = (origChunkPath == "RIFF:WAVE/LIST:Tdat" || origChunkPath == "RIFF:AVI /LIST:Tdat")
						&& idString.at(0) != 'J'; // just exclude JUNK/Q

					bool isDispChunk =
						((origChunkPath == "RIFF:WAVE" || origChunkPath == "RIFF:AVI ")
							&& idString == "DISP");

					bool isBextChunk =
						((origChunkPath == "RIFF:WAVE" || origChunkPath == "RIFF:AVI ")
							&& idString == "bext");

					bool isIXMLChunk =
						((origChunkPath == "RIFF:WAVE")
							&& idString == "iXML");

					bool isXMPchunk = false; //assume beforehand
					if (idString == "_PMX")
					{	// detour first, to detect xmp in wrong places
						assertMsg("XMP packet found in wrong place!",
							(origChunkPath == "RIFF:WAVE" || "RIFF:AVI" || "RIFF:AVIX")); //be very linient here.
						isXMPchunk = true;
					}

					bool isIDITChunk =
						((origChunkPath == "RIFF:AVI/LIST:hdrl" || origChunkPath == "RIFF:AVI /LIST:hdrl")
							&& idString == "IDIT");

					// deal with chunks of interest /////////////////////////////////////////////
					// a little prelude for disp chunk
					if (isDispChunk)
					{
						XMP_Uns32 dispChunkType = LFA_ReadUns32_LE(file);
						// only dispChunks starting with a 0x0001 are of interest to us.
						// others do exist and are not an error

						if (dispChunkType != 0x0001)
							isDispChunk = false;

						chunkSize -= 4;
					}

					if (isListInfo || isListTdat || isDispChunk || isIDITChunk)
					{
						// dump that string:
						std::string value;

						if (chunkSize > 8) // aka skip for empty chunks
						{
							// first check if the string is zero terminated
							LFA_Seek(file, chunkSize - 8 - 1, SEEK_CUR); // jump to last char
							bool zeroTerm = (LFA_ReadUns8(file) == 0);
							LFA_Seek(file, -(chunkSize - 8), SEEK_CUR); //jump back
																		// some strings are zero-terminated (so despite initial length they are "c-strings"
																		// others are not ( "pascal strings" if you will.
																		// must cater to both: zero-terminated-ness should not affect resulting value.
							if (zeroTerm)
							{
								// read string without zero (last char)
								value = tree->digestString(file, "", chunkSize - 8 - 1, false);
								tree->addComment(" zero terminated");
								LFA_ReadUns8(file); // skip the zero
							}
							else
							{
								// read string including last char
								value = tree->digestString(file, "", chunkSize - 8, false);
								tree->addComment(" not zero terminated");
							}
							tree->changeValue(value);
						}

						tree->changeValue(value);
					}
					else if (isXMPchunk)
					{
						tree->pushNode("XMP packet");

						tree->addOffset(file);
						tree->addComment("packet size: 0x%llX", chunkSize - 8);
						Skip(file, chunkSize - 8);
						tree->addComment("packet end: 0x%llX", LFA_Tell(file));

						tree->popNode();
					}
					else if (isBextChunk)
					{
						tree->pushNode("bext chunk");
						tree->addOffset(file);
						tree->addComment("packet size: 0x%llX", chunkSize - 8);

						// I assume that the minimum BEXT chunk size is 602:
						// > 8 + ( 256+32+32+10+8+4+4+2+64+190+0 )
						// ans = 610
						const XMP_Int64 MIN_BEXT_SIZE = 610;
						assertMsg("minimum Berx Chunk Size", chunkSize >= MIN_BEXT_SIZE);
						XMP_Int64 BEXT_CodingHistorySize = chunkSize - MIN_BEXT_SIZE;

						setFixedBEXTField(file, chunkPath + ".Description", 256);
						setFixedBEXTField(file, chunkPath + ".Originator", 32);
						setFixedBEXTField(file, chunkPath + ".OriginatorReference", 32);
						setFixedBEXTField(file, chunkPath + ".OriginationDate", 10);
						setFixedBEXTField(file, chunkPath + ".OriginationTime", 8);

						tree->digest32u(file, chunkPath + ".TimeReferenceLow", false, true);  // DWORD == 32 Bit
						tree->digest32u(file, chunkPath + ".TimeReferenceHigh", false, true); // DWORD == 32 Bit

						tree->digest16u(file, chunkPath + ".Version", false, true);

						// UMID has 64 bytes:
						tree->digestString(file, chunkPath + ".UMID", 64);
						//tree->digest32u( file, chunkPath+".UMID_0-4", false, true );
						//tree->setKeyValue( "UMID_5-59" );
						//Skip( file, 64 - 4 - 4 );
						//tree->digest32u( file, chunkPath+".UMID_60-63", false, true );

						tree->setKeyValue(chunkPath + ".Reserved");
						Skip(file, 190);

						if (BEXT_CodingHistorySize)
						{
							setFixedBEXTField(file, chunkPath + ".CodingHistory", BEXT_CodingHistorySize);

							//tree->setKeyValue( chunkPath+".CodingHistory" ); // not bothering details.
							tree->addComment("( 0x%llx bytes ) ", BEXT_CodingHistorySize);
							//Skip( file, BEXT_CodingHistorySize );
						}

						tree->addComment("packet end: 0x%llX", LFA_Tell(file));
						tree->popNode();
					}
					else if (isIXMLChunk) {
						tree->pushNode("iXML packet");

						tree->addOffset(file);
						tree->addComment("packet size: 0x%llX", chunkSize - 8);
						//Skip( file, chunkSize - 8 );

						size_t sizeofIXMLValue = chunkSize - 8;
						char* descriptionBuffer = new char[sizeofIXMLValue + 2];
						LFA_Read(file, descriptionBuffer, sizeofIXMLValue, true);
						descriptionBuffer[sizeofIXMLValue] = '\0'; // tack on, in case not contained
																   // parse till first \0
						std::string description(descriptionBuffer);

						// Dumping the iXML chunk. Needed for testing
						// Add iXML chunk as a node to tree
						tree->setKeyValue(chunkPath + ".ValueOfIXMLChunk", description);

						delete[] descriptionBuffer;
						tree->addComment("packet end: 0x%llX", LFA_Tell(file));

						tree->popNode();

					}
					else
					{
						Skip(file, chunkSize - 8); // skip remainder of chunk ( id, length already digested )
						assertMsg(fromArgs("inner chunk size too big, curPos:0x%llx, parentEnd:0x%llx",
							LFA_Tell(file),
							parentEnd),
							LFA_Tell(file) <= parentEnd);
					}

					if (!skipper)
						tree->popNode();
				}
				else
				{
					//dump undefined bytes in LIST
					tree->pushNode("** unknown bytes **");
					tree->addOffset(file);
					tree->addComment("size: 0x%llX", chunkSize);
					Skip(file, chunkSize - 8);
					tree->popNode();
				}

				if (LFA_Tell(file) % 2 == 1) // if odd file position, add pad byte.
				{
					if (LFA_Tell(file) == parentEnd)
					{
						// last pad byte is missing
						tree->addComment(" (pad byte missing [bug 1521093])");
					}
					else
					{
						XMP_Uns8 padByte = LFA_ReadUns8(file);
						if (!skipper)
						{
							if (0 != padByte)
								tree->addComment(" (non-zero pad byte!)");
							else
								tree->addComment(" (pad byte)");
						}
					}
				}
			}
		}
	} // while


}	// DumpRIFFChunk

	// =================================================================================================

static void
DumpRIFF(LFA_FileRef file, XMP_Int64 fileLen)
{
	DataSize64Chunk rf64Sizes;
	DumpRIFFChunk(file, fileLen, "", false, &rf64Sizes);
}

static void
DumpAIFF(LFA_FileRef file, XMP_Int64 fileLen)
{
	DumpRIFFChunk(file, fileLen, "", true);
}
// =================================================================================================

static XMP_Uns32 crcTable[256];
static bool crcTableInited = false;

static XMP_Uns32 ComputeCRCforPNG(LFA_FileRef file, XMP_Uns32 crcOffset, XMP_Uns32 crcLen)
{
	if (!crcTableInited) {
		for (int n = 0; n < 256; ++n) {
			XMP_Uns32 c = n;
			for (int k = 0; k < 8; ++k) {
				XMP_Uns32 lowBit = c & 1;
				c = c >> 1;
				if (lowBit != 0) c = c ^ 0xEDB88320;
			}
			crcTable[n] = c;
		}
		crcTableInited = true;
	}

	XMP_Uns32 crc = 0xFFFFFFFF;
	CaptureFileData(file, crcOffset, crcLen);

	for (XMP_Uns32 i = 0; i < crcLen; ++i) {	// ! The CRC includes the chunk type and data.
		XMP_Uns8 byte = sDataPtr[i];
		XMP_Uns8 index = (XMP_Uns8)((crc ^ byte) & 0xFF);
		crc = crcTable[index] ^ (crc >> 8);
	}

	return crc ^ 0xFFFFFFFF;

}	// ComputeCRCforPNG

	// =================================================================================================

static const XMP_Uns32 kPNG_iTXt = 0x69545874;
static const XMP_Uns32 kPNG_tEXt = 0x74455874;
static const XMP_Uns32 kPNG_zTXt = 0x7A545874;

static XMP_Uns32
DumpPNGChunk(LFA_FileRef file, XMP_Uns32 pngLen, XMP_Uns32 chunkOffset)
{
	// A PNG chunk contains:
	//   A big endian UInt32 length for the data portion. Zero is OK.
	//   A 4 byte chunk type, should be 4 ASCII letters, lower or upper case.
	//   The chunk data.
	//   A big endian UInt32 CRC.
	// There are no alignment constraints.
	//
	// Chunks of type tEXt, iTXt, and zTXt have text values. Each text form has a leading "usage
	// keyword" followed by the data string. The keywords must be visible Latin-1, 0x20..0x7E and
	// 0xA1..0xFF. They are limited to 1 to 79 characters, plus a terminating nul.
	//
	// A tEXt chunk has 0 or more bytes of Latin-1 characters. The data is not nul terminated, and
	// embedded nuls are not allowed. A zTXt chunk is like tEXt but the data string is zlib compressed.
	// An iTXt chunk has a variety of "info tags" followed by a UTF-8 data string.
	//
	// The XMP is in an iTXt chunk with the keyword XML:com.adobe.xmp and 4 bytes of 0 for the info.

	XMP_Uns32 chunkLen;
	XMP_Uns32 chunkType;
	XMP_Uns32 chunkCRC;

	if ((pngLen - chunkOffset) < 12) {
		tree->addComment("** Unexpected end of PNG file, %ul bytes remaining **", (pngLen - chunkOffset));
		return (pngLen - chunkOffset);
	}

	LFA_Seek(file, chunkOffset, SEEK_SET);
	LFA_Read(file, &chunkLen, 4, true);
	chunkLen = GetUns32BE(&chunkLen);

	if (chunkLen > (pngLen - chunkOffset)) {
		tree->addComment("** No room for PNG chunk, need %u, have %u **", chunkLen, pngLen - chunkOffset);
		return (pngLen - chunkOffset);	// ! Not chunkLen, might be bad and cause wrap-around.
	}

	LFA_Read(file, &chunkType, 4, true);	// After read, memory is in file order.

	LFA_Seek(file, (chunkOffset + 8 + chunkLen), SEEK_SET);
	LFA_Read(file, &chunkCRC, 4, true);
	chunkCRC = GetUns32BE(&chunkCRC);

	tree->addComment("   '%.4s', offset %u (0x%X), size %d, CRC 0x%.8X",
		&chunkType, chunkOffset, chunkOffset, chunkLen, chunkCRC);

	XMP_Uns32 newCRC = ComputeCRCforPNG(file, (chunkOffset + 4), (chunkLen + 4));

	if (chunkCRC != newCRC) tree->addComment("** CRC should be 0x%.8X **", newCRC);

	chunkType = GetUns32BE(&chunkType);	// Reorder the type to compare with constants.

	if ((chunkType == kPNG_iTXt) || (chunkType == kPNG_tEXt) || (chunkType == kPNG_zTXt)) {

		CaptureFileData(file, (chunkOffset + 8), chunkLen);

		XMP_Uns8 * keywordPtr = sDataPtr;
		size_t     keywordLen = strlen((char*)keywordPtr);

		PrintOnlyASCII_8(keywordPtr, keywordLen);

		if ((chunkType == kPNG_iTXt) && (keywordLen == 17) && CheckBytes(keywordPtr, "XML:com.adobe.xmp", 18)) {

			if (sXMPPtr != 0) {
				tree->addComment("      ** Redundant XMP **");
			}
			else {
				CaptureXMP((keywordPtr + 22), (chunkLen - 22), (chunkOffset + 8 + 22));
				XMP_Uns32 otherFlags = GetUns32BE(keywordPtr + 18);
				if (otherFlags != 0) tree->addComment("** bad flags %.8X **", otherFlags);
			}

		}

	}

	return (8 + chunkLen + 4);

}	// DumpPNGChunk

	// =================================================================================================

static void
DumpPS(LFA_FileRef file, XMP_Uns32 fileLen)
{
	XMP_Int32 psOffset;
	size_t psLength;

	LFA_Seek(file, 4, SEEK_SET); // skip fileheader bytes
	LFA_Read(file, &psOffset, 4, true);
	LFA_Read(file, &psLength, 4, true);

	tree->addComment(" psOffset: %d, psLength: %d", psOffset, psLength);

	// jump to psOffset
	Skip(file, (psOffset - 12));

	// get the header (everything till first %

	XMP_Int64 offset = LFA_Tell(file);
	std::string key, value;
	char byte = LFA_GetChar(file);
	bool eof = false;
	while (!eof)
	{
		key.clear();
		key += byte; // add the first %
		byte = LFA_GetChar(file);

		while (byte != ' ' && byte != '\r') // get everthing until next space or LF
		{
			key += byte;
			byte = LFA_GetChar(file);

		}

		//if (CheckBytes( key.c_str(), "%%EOF", 5))
		if (key == "%%EOF")
		{
			eof = true;
		}
		else
		{
			byte = LFA_GetChar(file);
			value.clear();
			while (byte != '%') // get everthing until next %
			{
				value += byte;
				byte = LFA_GetChar(file);
			}
		}
		tree->pushNode(key);
		tree->addOffset(file);

		//for now only store value for header 
		if (key == "%!PS-Adobe-3.0")
		{
			tree->changeValue(value);
		}

		tree->addComment("offset: %d", offset);
		tree->addComment("size: 0x%llX", LFA_Tell(file) - offset);
		tree->popNode();

		offset = LFA_Tell(file);
	}
	// Now just get everything else and store all keys that start with %


	// get the key 
	// start of the PostScript DSC header comment

	/*XMP_Uns8 buffer [11];
	LFA_Read ( file,  &buffer, sizeof(buffer), true );

	if (!CheckBytes( buffer, "%!PS-Adobe-", 11))
	{
	tree->comment ( "** Invalid PS, unknown PS file tag." );
	return;
	}

	// Check the PostScript DSC major version number.
	XMP_Uns8 byte;
	LFA_Read ( file,  &byte, sizeof(byte), true );

	psMajorVer = 0;
	while ( IsNumeric( byte ) )
	{
	psMajorVer = (psMajorVer * 10) + (byte - '0');
	if ( psMajorVer > 1000 ) {
	tree->comment ( "** Invalid PS, Overflow." );
	return;
	};	// Overflow.
	LFA_Read ( file,  &byte, sizeof(byte), true );
	}
	if ( psMajorVer < 3 ){
	tree->comment ( "** Invalid PS, The version must be at least 3.0." );
	return;
	};	// The version must be at least 3.0.

	if ( byte != '.' ){
	tree->comment ( "** Invalid PS, No minor number" );
	return;
	};	// No minor number.
	LFA_Read ( file,  &byte, sizeof(byte), true );

	// Check the PostScript DSC minor version number.

	psMinorVer = 0;
	while ( IsNumeric( byte ) )
	{
	psMinorVer = (psMinorVer * 10) + (byte - '0');
	if ( psMinorVer > 1000 ) {
	tree->comment ( "** Invalid PS, Overflow." );
	return;
	};	// Overflow.
	LFA_Read ( file,  &byte, sizeof(byte), true );
	}

	tree->addComment(" psMajor Version: %d, psMinor Version: %d", psMajorVer, psMinorVer);*/
}

// =================================================================================================

static void
DumpPNG(LFA_FileRef file, XMP_Uns32 pngLen)
{
	// A PNG file contains an 8 byte signature followed by a sequence of chunks.

	XMP_Uns32  chunkOffset = 8;

	while (chunkOffset < pngLen) {
		XMP_Uns32 chunkLen = DumpPNGChunk(file, pngLen, chunkOffset);
		chunkOffset += chunkLen;
	}

	if (sXMPPtr != 0) DumpXMP("PNG XMP 'iTXt' chunk");

}	// DumpPNG

	// =================================================================================================

static void
DumpInDesign(LFA_FileRef file, XMP_Uns32 inddLen)
{
	InDesignMasterPage masters[2];
	size_t	 dbPages;
	XMP_Uns8 cobjEndian;

	// FIgure out which master page to use.

	LFA_Seek(file, 0, SEEK_SET);
	LFA_Read(file, &masters, sizeof(masters), true);

	XMP_Uns64 seq0 = GetUns64LE((XMP_Uns8 *)&masters[0].fSequenceNumber);
	XMP_Uns64 seq1 = GetUns64LE((XMP_Uns8 *)&masters[1].fSequenceNumber);

	if (seq0 > seq1) {
		dbPages = GetUns32LE((XMP_Uns8 *)&masters[0].fFilePages);
		cobjEndian = masters[0].fObjectStreamEndian;
		tree->addComment("   Using master page 0");
	}
	else {
		dbPages = GetUns32LE((XMP_Uns8 *)&masters[1].fFilePages);
		cobjEndian = masters[1].fObjectStreamEndian;
		tree->addComment("   Using master page 1");
	}

	bool bigEndian = (cobjEndian == kINDD_BigEndian);

	tree->addComment("%d pages, %s endian", dbPages, (bigEndian ? "big" : "little"));

	// Look for the XMP contiguous object.

	// *** XMP_Int64 cobjPos = (XMP_Int64)dbPages * kINDD_PageSize;	// ! Use a 64 bit multiply!
	XMP_Uns32 cobjPos = dbPages * kINDD_PageSize;
	XMP_Uns32 cobjLen;

	for (; cobjPos < inddLen; cobjPos += cobjLen) {

		InDesignContigObjMarker cobjHead;

		LFA_Seek(file, cobjPos, SEEK_SET);
		LFA_Read(file, &cobjHead, sizeof(cobjHead), true);

		if (!CheckBytes(&cobjHead.fGUID, kINDDContigObjHeaderGUID, kInDesignGUIDSize)) {

			// No Contiguous Object header. Could be in zero padding for the last page.

			XMP_Uns8 fileTail[kINDD_PageSize];
			size_t   tailLen = inddLen - cobjPos;
			bool endOK = (tailLen < kINDD_PageSize);

			if (endOK) {
				LFA_Seek(file, cobjPos, SEEK_SET);
				LFA_Read(file, fileTail, sizeof(fileTail), true);
				for (size_t i = 0; i < tailLen; ++i) {
					if (fileTail[i] != 0) {
						endOK = false;
						break;
					}
				}
			}

			if (endOK) break;
			tree->addComment("   ** No Contiguous Object GUID at offset %u (0x%X) tree.", cobjPos, cobjPos);
			return;

		}

		cobjHead.fObjectUID = GetUns32LE(&cobjHead.fObjectUID);
		cobjHead.fObjectClassID = GetUns32LE(&cobjHead.fObjectClassID);
		cobjHead.fStreamLength = GetUns32LE(&cobjHead.fStreamLength);
		cobjHead.fChecksum = GetUns32LE(&cobjHead.fChecksum);

		cobjLen = cobjHead.fStreamLength + (2 * sizeof(InDesignContigObjMarker));

		tree->addComment("   ContigObj offset %d (0x%X), size %d, Object UID %.8X, class ID %.8X, checksum %.8X",
			cobjPos, cobjPos, cobjHead.fStreamLength, cobjHead.fObjectUID, cobjHead.fObjectClassID, cobjHead.fChecksum);

		if ((cobjHead.fObjectClassID & 0x40000000) == 0) tree->addComment("read only");

		XMP_Uns32 xmpLen;
		LFA_Read(file, &xmpLen, 4, true);
		if (bigEndian) {
			xmpLen = GetUns32BE(&xmpLen);
		}
		else {
			xmpLen = GetUns32LE(&xmpLen);
		}

		XMP_Uns8 xmpStart[16];	// Enough for "<?xpacket begin=".
		LFA_Read(file, &xmpStart, sizeof(xmpStart), true);

		if ((cobjHead.fStreamLength > (4 + 16)) && ((xmpLen + 4) == cobjHead.fStreamLength) &&
			CheckBytes(xmpStart, "<?xpacket begin=", 16)) {

			if (sXMPPtr != 0) {
				tree->addComment("** redundant XMP **");
			}
			else {
				tree->addComment("XMP");
				CaptureXMPF(file, (cobjPos + sizeof(InDesignContigObjMarker) + 4), xmpLen);
			}

		}

	}

	if (sXMPPtr != 0) DumpXMP("InDesign XMP Contiguous Object");

}	// DumpInDesign

	// =================================================================================================

static void
DumpSVGTag(std::string basePath, XML_NodePtr currentNode)
{
	if (currentNode)
	{
		tree->pushNode(basePath + currentNode->name);

		// Iterating over all XML children.
		XML_NodeVector currNodeVector = currentNode->content;
		for (size_t i = 0; i < currNodeVector.size(); i++)
		{
			// Dump all children who are element nodes.
			if (currNodeVector[i]->kind == kElemNode)
				DumpSVGTag(basePath + currentNode->name + "/", currNodeVector[i]);

			// Extract the value from datanodes and put in TagMap if it's not yet available.
			if (currNodeVector[i]->kind == kCDataNode && tree->getValue(basePath + currentNode->name) == "")
				tree->updateKeyValue(basePath + currentNode->name, currNodeVector[i]->value);
		}
	}

}	// DumpSVGTag

	// =================================================================================================

static void
DumpSVG(LFA_FileRef file, XMP_Uns32 svgLen)
{
	// SVG is an XML based format.We consider any file as SVG file if the given file contains a SVG tag.
	// Hence CheckFileFormat looks for presence of "<svg" in the file.
	//
	// For Dumping SVG elements we are using ExpatAdapter. Below code will parse given file using this 
	// adapter and add different tags in the TagMap tree.
	//
	// Below is the currently supported structure of known tags for this format.
	//	<svg>
	//		<title/>
	//		<desc/>
	//		<metadata>
	//			<x:xmpmeta/>
	//			<...>
	//			<...>
	//		</metadata>
	//		<...>
	//		<...>
	//	</svg>

	ExpatAdapter * pExpatAdapter = XMP_NewExpatAdapter(false);

	if (pExpatAdapter == 0)
	{
		tree->comment("ExpatAdapter initialization failed. Cann't parse SVG file.");
		return;
	}

	// Allocating big enough memory on heap to read file contents.
	XMP_Uns8 *fileContent = new XMP_Uns8[svgLen + 1];
	memset(fileContent, 0, (svgLen + 1) * sizeof(XMP_Uns8));

	// Reading total file in buffer (fileContent)
	LFA_Seek(file, 0, SEEK_SET);
	LFA_Read(file, fileContent, svgLen + 1, false);

	// Parsing the file with ExpatAdapter
	pExpatAdapter->ParseBuffer(fileContent, svgLen + 1, false /* not the end */);

	// Finding <svg> element and adding to TagMap tree.
	XML_NodePtr svgNode = pExpatAdapter->tree.GetNamedElement("http://www.w3.org/2000/svg", "svg");
	DumpSVGTag("", svgNode);

	// De-allocating all the resources.
	if (fileContent)
	{
		delete[] fileContent;
		fileContent = NULL;
	}

	if (pExpatAdapter)
	{
		delete pExpatAdapter;
		pExpatAdapter = NULL;
	}

}	// DumpSVG

	// =================================================================================================

#define kSWF_FileAttributesTag	69
#define kSWF_MetadataTag		77

static void
DumpSWF(LFA_FileRef file, XMP_Uns32 swfLen)
{
	// SWF is a chunky format, the chunks are called tags. The data of a tag cannot contain an
	// offset to another tag, so tags can generally be freely inserted, removed, etc. Each tag has a
	// header followed by data. There are short (2 byte) and long (6 byte) headers. A short header
	// is a UInt16 with a type code in the upper 10 bits and data length in the lower 6 bits. The
	// length does not include the header. A length of 63 (0x3F) indicates a long header. This adds
	// an SInt32 data length.
	//
	// All multi-byte integers in SWF are little endian. Strings use byte storage and are null
	// terminated. In SWF 5 or earlier strings use ASCII or shift-JIS encoding with no indication in
	// the file. In SWF 6 or later strings use UTF-8.
	//
	// The overall structure of a SWF file:
	//   File header
	//   FileAttributes tag, optional before SWF 8
	//   other tags
	//   End tag
	//
	// The End tag is #0. No data is defined, but a reader should process the length normally. The
	// last tag must be an End tag, but End tags can also be used elsewhere (e.g. to end a sprite
	// definition). There is no standard tag for free or unused space.
	//
	// SWF file header:
	//   0  3 - signature, "FWS"=uncompressed, 'CWS'=compressed (zlib, SWF6 or later)
	//   3  1 - UInt8 major version
	//   4  4 - UInt32 uncompressed file length
	//   8  v - frame rectangle, variable size
	//   ?  2 - UInt16 frame rate
	//   ?  2 - UInt16 frame count
	//
	// FileAttributes tag, #69:
	//   0  3 - reserved, must be 0
	//   3  1 - HasMetadata, 0/1 Boolean
	//   4  3 - reserved, must be 0
	//   7  1 - UseNetwork, 0/1 Boolean
	//   8 24 - reserved, must be 0
	//
	// The Metadata tag is #77. If present, the FileAttributes tag must also be present and
	// HasMetadata must be set. The data is a string, must be XMP, should be as compact as possible.
	//
	// The frame rectangle is a packed sequence of 5 bit fields, with zero bits add as padding to a
	// byte boundary. The first field is 5 bits long and gives the number of bits in each of the
	// other 4 fields (0..31). The others are signed integers for the X min/max and Y min/max
	// coordinates. The frame rectangle field is at least 2 bytes long, and at most 17 bytes long.

	XMP_Uns8 buffer[100];	// Big enough, need 32 for file header and 38 for FileAttributes.
	size_t   ioCount;

	// Dump the file header.

	bool isCompressed = false;
	bool hasMetadata = false;

	XMP_Uns8  fileVersion;
	XMP_Uns32 fullLength;
	XMP_Uns8  rectBits;
	XMP_Uns16 frameRate, frameCount;

	ioCount = LFA_Read(file, buffer, sizeof(buffer), false);
	if (ioCount < 14) {
		tree->comment("** Invalid SWF, file header is too short.");
		return;
	}

	if (CheckBytes(buffer, "CWS", 3)) {
		isCompressed = true;
	}
	else if (!CheckBytes(buffer, "FWS", 3)) {
		tree->comment("** Invalid SWF, unknown file header signature.");
		return;
	}

	fileVersion = buffer[3];
	fullLength = GetUns32LE(&buffer[4]);
	rectBits = buffer[8] >> 3;

	XMP_Uns32 rectBytes = ((5 + (4 * rectBits)) / 8) + 1;
	XMP_Uns32 headerBytes = 8 + rectBytes + 4;

	if (ioCount < headerBytes) {
		tree->comment("** Invalid SWF, file header is too short.");
		return;
	}

	frameRate = GetUns16LE(&buffer[8 + rectBytes]);
	frameCount = GetUns16LE(&buffer[8 + rectBytes + 2]);

	// *** Someday decode the frame rectangle.

	tree->pushNode("File Header");
	tree->addComment("%scompressed, version %d, full length %d, frame rate %d, frame count %d",
		(isCompressed ? "" : "un"), fileVersion, fullLength, frameRate, frameCount);
	tree->popNode();
	if (isCompressed) {
		// *** Add support to decompress into a temp file.
		tree->comment("** Ignoring compressed SWF contents.");
		return;
	}

	// Dump the tags in the body of the file.

	XMP_Uns16 tagType;
	XMP_Uns32 tagOffset, tagLength, headerLength, dataLength;

	for (tagOffset = headerBytes; (tagOffset < swfLen); tagOffset += tagLength) {

		// Read the tag header, extract the type and data length.

		LFA_Seek(file, tagOffset, SEEK_SET);
		ioCount = LFA_Read(file, buffer, sizeof(buffer), false);


		if (ioCount < 2) {
			tree->comment("** Invalid SWF, tag header is too short at offset %u (0x%X).", tagOffset, tagOffset);
			break;
		}

		tagType = GetUns16LE(&buffer[0]);
		dataLength = tagType & 0x3F;
		tagType = tagType >> 6;

		if (dataLength < 63) {
			headerLength = 2;
		}
		else {
			if (ioCount < 6) {
				tree->comment("** Invalid SWF, tag header is too short at offset %u (0x%X).", tagOffset, tagOffset);
				break;
			}
			headerLength = 6;
			dataLength = GetUns32LE(&buffer[2]);
		}
		tagLength = headerLength + dataLength;

		// Make sure the tag fits in the file, being careful about arithmetic overflow.

		if (tagLength > (swfLen - tagOffset)) {
			tree->comment("** Invalid SWF, tag is too long at offset %u (0x%X).", tagOffset, tagOffset);
			break;
		}

		// See if this is the FileAttributes tag or the Metadata tag.

		if ((tagOffset == headerBytes) && (tagType != kSWF_FileAttributesTag) && (fileVersion >= 8)) {
			tree->comment("** Invalid SWF, first tag is not FileAttributes.");
		}

		if (tagType == kSWF_FileAttributesTag) {

			if (dataLength < 4) {
				tree->comment("** Invalid SWF, FileAttributes tag is too short at offset %u (0x%X).", tagOffset, tagOffset);
				continue;
			}

			XMP_Uns32 xmpFlag = GetUns32LE(&(buffer[headerLength])) & 0x10;
			if (xmpFlag != 0) {
				hasMetadata = true;
			}

			tree->pushNode("FileAttributes tag");
			tree->addComment("Offset %d (0x%X), %s XMP", tagOffset, tagOffset, (hasMetadata ? "has" : "no"));
			tree->popNode();

		}
		else if (tagType == kSWF_MetadataTag) {

			if (!hasMetadata) {
				tree->comment("** Invalid SWF, Metadata tag without HasMetadata flag at offset %u (0x%X).", tagOffset, tagOffset);
				continue;
			}

			tree->pushNode("Metadata tag");
			tree->addComment("Offset %d (0x%X)", tagOffset, tagOffset);
			tree->popNode();

			if (sXMPPtr != 0) {
				tree->comment("  ** Redundant Metadata tag");
			}
			else {
				CaptureXMPF(file, (tagOffset + headerLength), dataLength);
			}

			//if ( sXMPPtr != 0 ) DumpXMP ( "SWF Metadata tag (#77) XMP" );

		}
		else {
			tree->pushNode("tag #%d", tagType);
			tree->addComment("Offset %d (0x%X)", tagOffset, tagOffset);
			tree->popNode();
		}

	}

}	// DumpSWF

	// =================================================================================================

static XMP_Uns32 DumpScriptDataArray(LFA_FileRef file, XMP_Uns32 sdOffset, XMP_Uns32 count,
	bool isStrict, bool isOnXMP = false);
static XMP_Uns32 DumpScriptDataObject(LFA_FileRef file, XMP_Uns32 sdOffset);
static XMP_Uns32 DumpScriptDataObjectList(LFA_FileRef file, XMP_Uns32 sdOffset);

static inline XMP_Uns32 GetUns24BE(XMP_Uns8 * ptr)
{
	return (GetUns32BE(ptr) >> 8);
}

#define ReadSDValue(len)	\
	ioCount = LFA_Read ( file,  buffer, len,true);									\
	if ( ioCount != len ) {																\
		tree->comment ( "** Failure reading ScriptDataValue, ioCount = %d", ioCount );	\
		return (sdOffset + 1 + ioCount);												\
	}


static XMP_Uns32 DumpScriptDataValue(LFA_FileRef file, XMP_Uns32 sdOffset, bool isOnXMP = false)
{
	XMP_Uns8 buffer[64 * 1024];
	size_t   ioCount;

	XMP_Uns8  kind;
	XMP_Uns16 u16;
	XMP_Uns32 u32;
	XMP_Uns64 u64;

	LFA_Seek(file, sdOffset, SEEK_SET);
	ioCount = LFA_Read(file, &kind, 1, true);
	if (ioCount != 1) {
		tree->comment("** Failure reading ScriptDataValue kind, ioCount = %d", ioCount);
		return sdOffset;
	}

	if (isOnXMP) {
		if ((kind != 8) && (kind != 2) && (kind != 0xC)) {
			tree->comment("** Invalid kind for onXMPData tag **");
		}
	}

	XMP_Uns64 time;
	XMP_Int16 tz;

	switch (kind) {

	case 0x00:	// A number, IEEE double.
		ReadSDValue(8);
		u64 = GetUns64BE(&buffer[0]);
		tree->addComment("float = %f", *((double*)(&u64)));
		return (sdOffset + 1 + 8);

	case 0x01:	// A 0/1 Boolean. (??? general Uns8?)
		ReadSDValue(1);
		tree->addComment("bool = %d", buffer[0]);
		return (sdOffset + 1 + 1);

	case 0x02:	// A short UTF-8 string, leading 2 byte count.
		ReadSDValue(2);
		u16 = GetUns16BE(&buffer[0]);
		ReadSDValue(u16);
		if (int(u16) < 4096)
		{
			tree->addComment("string (%d) = \"%.*s\"", u16, u16, buffer);
		}
		else {
			tree->addComment("string (%d) ", u16);
		}
		if (buffer[u16 - 1] != 0) tree->addComment("value lacks trailing nul");
		if (isOnXMP) CaptureXMPF(file, (sdOffset + 1 + 2), u16);
		return (sdOffset + 1 + 2 + u16);

	case 0x03:	// An object list, triples of 0x02/key/value, ends at 0x02000009.
		tree->addComment("object list");
		return DumpScriptDataObjectList(file, sdOffset + 1);

	case 0x04:	// A movie clip path as short UTF-8 string
		ReadSDValue(2);
		u16 = GetUns16BE(&buffer[0]);
		ReadSDValue(u16);
		tree->addComment("movie (%d) = \"%.*s\"", u16, u16, buffer);
		if (buffer[u16 - 1] != 0) tree->addComment("value lacks trailing nul");
		return (sdOffset + 1 + 2 + u16);

	case 0x05:	// A null, single byte.
		tree->addComment("null");
		return (sdOffset + 1);

	case 0x06:	// A undefined, single byte.
		tree->addComment("undefined");
		return (sdOffset + 1);

	case 0x07:	// A reference, Uns16.
		ReadSDValue(2);
		u16 = GetUns16BE(&buffer[0]);
		tree->addComment("reference = %d", u16);
		return (sdOffset + 1 + 2);

	case 0x08:	// An ECMA array, 32-bit count then any number of key/value pairs. Has 0x000009 terminator.
		ReadSDValue(4);
		u32 = GetUns32BE(&buffer[0]);
		tree->addComment("ECMA array [%d]", u32);
		return DumpScriptDataArray(file, sdOffset + 1 + 4, u32, false, isOnXMP);

	case 0x09:	// End of object or array. Should not see this here!
		tree->addComment("** end **");
		return (sdOffset + 1);

	case 0x0A:	// A strict array, count then that many key/value pairs, no 0x000009 terminator.
		ReadSDValue(4);
		u32 = GetUns32BE(&buffer[0]);
		tree->addComment("strict array [%d]", u32);
		return DumpScriptDataArray(file, sdOffset + 1 + 4, u32, true);

	case 0x0B:	// A date, Uns64 milliseconds since Jan 1 1970, Int16 TZ offset in minutes.
		ReadSDValue(10);
		time = GetUns64BE(&buffer[0]);
		tz = (XMP_Int16)GetUns16BE(&buffer[8]);
		tree->addComment("date, time=%ULL, tz=%d", time, tz);
		return (sdOffset + 1 + 10);

	case 0x0C:	// A long UTF-8 string, leading 4 byte count.
		ReadSDValue(4);
		u32 = GetUns32BE(&buffer[0]);
		if (u32 < sizeof(buffer)) {
			ReadSDValue(u32);
			tree->addComment("long string (%d) = \"%.*s\"", u32, u32, buffer);
			if (buffer[u32 - 1] != 0) tree->addComment("value lacks trailing nul");
		}
		else {
			ReadSDValue(sizeof(buffer));
			tree->addComment("long string (%d) = \"%.*s\"", u32, sizeof(buffer), buffer);
			tree->comment("** truncated long string output **");
		}
		if (isOnXMP) CaptureXMPF(file, (sdOffset + 1 + 4), u32);
		return (sdOffset + 1 + 4 + u32);

	case 0x0D:	// Unsupported, single byte.
		tree->addComment("unsupported");
		return (sdOffset + 1);

	case 0x0E:	// A RecordSet. (???)
		tree->addComment("** record set ?? **");
		return (sdOffset + 1);

	case 0x0F:	// XML as a long UTF-8 string
		ReadSDValue(4);
		u32 = GetUns32BE(&buffer[0]);
		if (u32 < sizeof(buffer)) {
			ReadSDValue(u32);
			tree->addComment("XML (%d) = \"%.*s\"", u32, u32, buffer);
			if (buffer[u32 - 1] != 0) tree->addComment("value lacks trailing nul");
		}
		else {
			ReadSDValue(sizeof(buffer));
			tree->addComment("XML (%d) = \"%.*s\"", u32, sizeof(buffer), buffer);
			tree->comment("** truncated long string output **");
		}
		if (isOnXMP) CaptureXMPF(file, (sdOffset + 1 + 4), u32);
		return (sdOffset + 1 + 4 + u32);

	case 0x10:	// A typed object list, short string class name, object list (like case 0x03).
		ReadSDValue(2);
		u16 = GetUns16BE(&buffer[0]);
		ReadSDValue(u16);
		tree->addComment("class, name = %.*s", u16, u16, buffer);
		if (buffer[u16 - 1] == 0) tree->addComment("name has trailing nul");
		return DumpScriptDataObjectList(file, (sdOffset + 1 + 2 + u16));

	case 0x11:	// AMF 3 data. (???)
		tree->addComment("** AMF 3 data ?? **");
		return (sdOffset + 1);

	default:
		tree->addComment("** unknown kind = %d **", kind);
		return (sdOffset + 1);

	}

}	// DumpScriptDataValue

	// =================================================================================================

static XMP_Uns32 DumpScriptVariable(LFA_FileRef file, XMP_Uns32 sdOffset, bool isOnXMP = false)
{
	XMP_Uns8 buffer[64 * 1024];
	size_t   ioCount;

	LFA_Seek(file, sdOffset, SEEK_SET);
	ioCount = LFA_Read(file, buffer, 2, true);
	if (ioCount != 2) {
		tree->comment("** Failure reading DumpScriptVariable start, ioCount = %d", ioCount);
		return (sdOffset + ioCount);
	}

	XMP_Uns16 nameLen = GetUns16BE(&buffer[0]);
	ioCount = LFA_Read(file, buffer, nameLen, true);
	if (ioCount != nameLen) {
		tree->comment("** Failure reading ScriptDataObject name, ioCount = %d", ioCount);
		return (sdOffset + 3 + ioCount);
	}

	tree->pushNode("%.*s @ 0x%X", nameLen, buffer, sdOffset);
	if (buffer[nameLen - 1] == 0) tree->addComment("name has trailing nul");
	if (strncmp((char*)buffer, "liveXML", nameLen) != 0) isOnXMP = false;	// ! Else keep the input value.
	XMP_Uns32 nextOffset = DumpScriptDataValue(file, (sdOffset + 2 + nameLen), isOnXMP);
	tree->popNode();

	return nextOffset;

}	// DumpScriptVariable

	// =================================================================================================

static XMP_Uns32 DumpScriptDataArray(LFA_FileRef file, XMP_Uns32 sdOffset, XMP_Uns32 headerCount,
	bool isStrict, bool isOnXMP /* = false */)
{
	XMP_Uns8 buffer[3];
	size_t   ioCount;

	XMP_Uns32 actualCount = 0;
	XMP_Uns32 currOffset = sdOffset;

	if (isStrict) {

		for (; headerCount > 0; --headerCount) {
			XMP_Uns32 nextOffset = DumpScriptVariable(file, currOffset);
			if (nextOffset == currOffset) {
				tree->comment("** Failure reading DumpScriptDataArray, no progress");
				return currOffset;
			}
			currOffset = nextOffset;
		}

	}
	else {

		while (true) {

			LFA_Seek(file, currOffset, SEEK_SET);
			ioCount = LFA_Read(file, buffer, 3, true);
			if (ioCount != 3) {
				tree->comment("** Failure check DumpScriptDataArray, ioCount = %d", ioCount);
				return (currOffset + ioCount);
			}
			if (GetUns24BE(&buffer[0]) == 9) break;

			XMP_Uns32 nextOffset = DumpScriptVariable(file, currOffset, isOnXMP);
			if (nextOffset == currOffset) {
				tree->comment("** Failure reading DumpScriptDataArray, no progress");
				return currOffset;
			}

			++actualCount;
			currOffset = nextOffset;

		}

		if ((headerCount != (XMP_Uns32)(-1)) && (headerCount != actualCount)) {
			tree->comment("Count mismatch, actual = %d", actualCount);	// ! Not an error!
		}

		currOffset += 3;	// ! Include the 0x000009 terminator.

	}

	return currOffset;

}	// DumpScriptDataArray

	// =================================================================================================

static XMP_Uns32 DumpScriptDataObject(LFA_FileRef file, XMP_Uns32 sdOffset)
{
	XMP_Uns8 buffer[64 * 1024];
	size_t   ioCount;

	LFA_Seek(file, sdOffset, SEEK_SET);
	ioCount = LFA_Read(file, buffer, 2, true);
	if (ioCount != 2) {
		tree->comment("** Failure reading ScriptDataObject name length, ioCount = %d", ioCount);
		return (sdOffset + ioCount);
	}

	XMP_Uns16 nameLen = GetUns16BE(&buffer[1]);
	ioCount = LFA_Read(file, buffer, nameLen, true);
	if (ioCount != nameLen) {
		tree->comment("** Failure reading ScriptDataObject name, ioCount = %d", ioCount);
		return (sdOffset + 2 + ioCount);
	}

	tree->pushNode("%.*s @ 0x%X", nameLen, buffer, sdOffset);
	if (buffer[nameLen - 1] == 0) tree->addComment("name has trailing nul");
	XMP_Uns32 nextOffset = DumpScriptDataValue(file, (sdOffset + 2 + nameLen));
	tree->popNode();

	return nextOffset;

}	// DumpScriptDataObject

	// =================================================================================================

static XMP_Uns32 DumpScriptDataObjectList(LFA_FileRef file, XMP_Uns32 sdOffset)
{
	XMP_Uns8 buffer[3];
	size_t   ioCount;

	XMP_Uns32 currOffset = sdOffset;

	while (true) {

		LFA_Seek(file, currOffset, SEEK_SET);
		ioCount = LFA_Read(file, buffer, 3, true);
		if (ioCount != 3) {
			tree->comment("** Failure check ScriptDataObjectList, ioCount = %d", ioCount);
			return (currOffset + ioCount);
		}

		XMP_Uns32 endFlag = GetUns24BE(&buffer[0]);
		if (endFlag == 9) return (currOffset + 3);

		XMP_Uns32 nextOffset = DumpScriptDataObject(file, currOffset);
		if (nextOffset == currOffset) {
			tree->comment("** Failure reading ScriptDataObjectList, no progress");
			return currOffset;
		}

		currOffset = nextOffset;

	}

}	// DumpScriptDataObjectList

	// =================================================================================================

static XMP_Uns32 DumpScriptDataTagContent(LFA_FileRef file, XMP_Uns32 sdOffset, XMP_Uns32 tagTime)
{
	XMP_Uns8 buffer[64 * 1024];
	size_t   ioCount;

	LFA_Seek(file, sdOffset, SEEK_SET);
	ioCount = LFA_Read(file, buffer, 3, true);
	if ((ioCount != 3) || (buffer[0] != 2)) {
		tree->comment("** Failure reading ScriptDataObject start, ioCount = %d, buffer[0]=0x%X", ioCount, buffer[0]);
		return (sdOffset + ioCount);
	}

	XMP_Uns16 nameLen = GetUns16BE(&buffer[1]);
	ioCount = LFA_Read(file, buffer, nameLen, true);
	if (ioCount != nameLen) {
		tree->comment("** Failure reading ScriptDataObject name, ioCount = %d", ioCount);
		return (sdOffset + 3 + ioCount);
	}

	tree->addComment("%.*s @ 0x%X", nameLen, buffer, sdOffset);
	if (buffer[nameLen - 1] == 0) tree->addComment("name has trailing nul");

	bool isOnXMP = (tagTime == 0) && (nameLen == 9) && (strncmp((char*)buffer, "onXMPData", 9) == 0);
	return DumpScriptDataValue(file, (sdOffset + 1 + 2 + nameLen), isOnXMP);

}	// DumpScriptDataTagContent

	// =================================================================================================

static void
DumpFLV(LFA_FileRef file, XMP_Uns32 flvLen)
{
	// FLV must not be confused with SWF, they are quite different internally. FLV is a chunky
	// format, the chunks are called tags. All multi-byte integers in FLV are stored in big endian
	// order. An FLV file begins with a variable length file header followed by a sequence of tags
	// (the file body).
	//
	// Each tag contains a header, content, and back-pointer. The size in a tag header is just the
	// data size. The back-pointer is the full size of the preceeding tag, not just the data length.
	// The first tag is preceeded by a 0 back-pointer (not by the length of the header). The last
	// tagk is followed by a back pointer.
	//
	// The FLV file header:
	//   0  3 - signature, "FLV"
	//   3  1 - UInt8 major version
	//   4  1 - UInt8 flags
	//   5  4 - UInt32 size of header (offset to body)
	//
	// The FLV tag header:
	//   0  1 - UInt8 tag type
	//   1  3 - UInt24 length of data
	//   4  3 - UInt24 timestamp, milliseconds into the playback
	//   7  1 - UInt8 timestamp high, for a full UInt32 playback time
	//   8  3 - UInt24 stream ID, must be 0
	//
	// Only 3 tag types are defined by SWF-FLV-8, 8 = audio, 9 = video, 18 = script data. There is
	// confusion or missing information in the spec about script data tags. In one place it uses the
	// term "script data", in another SCRIPTDATAOBJECT for type 18. Then within the "Data Tags"
	// section it talks about SCRIPTDATAOBJECT, SCRIPTDATAOBJECTEND, SCRIPTDATASTRING,
	// SCRIPTDATALONGSTRING, SCRIPTDATAVALUE, SCRIPTDATAVARIABLE, SCRIPTDATAVARIABLEEND, and
	// SCRIPTDATADATE. It isn't clear if these SCRIPTDATA* things are FLV tags, or substructure
	// within the data of tag type 18.

	XMP_Uns8 buffer[100];
	size_t   ioCount;
	XMP_Uns32 size, time, stream, backSize;

	ioCount = LFA_Read(file, buffer, 9 + 4, true);
	if (ioCount != 9 + 4) {
		tree->comment("** Failure reading FLV header, ioCount = %d", ioCount);
		return;
	}

	size = GetUns32BE(&buffer[5]);
	tree->addComment("FLV header: \"%.3s\", version %d, flags 0x%.2X, size %d (0x%X)",
		&buffer[0], buffer[3], buffer[4], size);

	LFA_Seek(file, size, SEEK_SET);
	ioCount = LFA_Read(file, buffer, 4, true);
	if (ioCount != 4) {
		tree->comment("** Failure reading leading backSize, ioCount = %d", ioCount);
		return;
	}
	backSize = GetUns32BE(&buffer[0]);
	if (backSize != 0) {
		tree->comment("** Bad leading backSize = %d", backSize);
		return;
	}

	for (XMP_Uns32 tagOffset = (size + 4); tagOffset < flvLen; tagOffset += (11 + size + 4)) {

		LFA_Seek(file, tagOffset, SEEK_SET);
		ioCount = LFA_Read(file, buffer, 11, true);	// Back pointer plus tag header.
		if (ioCount != 11) {
			tree->comment("** Failure reading FLV tag, ioCount = %d", ioCount);
			return;
		}

		size = GetUns24BE(&buffer[1]);
		time = GetUns24BE(&buffer[4]);
		time += ((XMP_Uns32)buffer[7] << 24);
		stream = GetUns24BE(&buffer[8]);

		if (time != 0) break;	// ! Just do the time 0 tags for this tool.

		char label[100];
		char comment[1000];

		XMP_Uns8 kind = buffer[0];
		if (kind == 8) {
			if (time == 0) sprintf(label, "Audio");	// Don't normally print, there are too many.
		}
		else if (kind == 9) {
			if (time == 0) sprintf(label, "Video");	// Don't normally print, there are too many.
		}
		else if (kind == 18) {
			sprintf(label, "ScriptData");
		}
		else {
			sprintf(label, "<other-%d>", kind);
		}
		sprintf(comment, "%s @ 0x%X, size=%d, time=%d, stream=%d", label, tagOffset, size, time, stream);

		tree->pushNode(label);
		tree->addComment(comment);

		LFA_Seek(file, (tagOffset + 11 + size), SEEK_SET);
		ioCount = LFA_Read(file, buffer, 4, true);	// Back pointer plus tag header.
		if (ioCount != 4) {
			tree->comment("** Failure reading backSize, ioCount = %d", ioCount);
			return;
		}

		backSize = GetUns32BE(&buffer[0]);
		if (backSize != (11 + size)) tree->comment("** Bad backSize= %d", backSize);

		if (kind == 18) {
			XMP_Uns32 endOffset = DumpScriptDataTagContent(file, (tagOffset + 11), time);
			if (endOffset != (tagOffset + 11 + size)) {
				tree->comment("** Bad endOffset = 0x%X", endOffset);
			}
		}

		tree->popNode();

	}

	if (sXMPPtr != 0) DumpXMP("FLV onXMPData tag");

}	// DumpFLV

	// =================================================================================================

static bool
PrintID3Encoding(XMP_Uns8 encoding, XMP_Uns8 * strPtr)
{
	bool bigEndian = true;

	switch (encoding) {
	case 0:
		tree->addComment("Latin1");
		break;
	case 1:
		if (*strPtr == 0xFF) bigEndian = false;
		tree->addComment("UTF-16 with BOM, %s)", (bigEndian ? "BE" : "LE"));
		break;
	case 2:
		tree->addComment("UTF-16 BE");
		break;
	case 3:
		tree->addComment("UTF-8");
		break;
	default:
		tree->addComment("** unknown **");
		break;
	}
	return bigEndian;
}	// PrintID3Encoding

	// =================================================================================================

	//static void
	//PrintID3EncodedText (XMP_Uns8 encoding, void * _strPtr, const char * label)
	//{
	//}	// PrintID3EncodedText

	// =================================================================================================

struct ID3_Header {
	char id3[3];
	XMP_Uns8 vMajor, vMinor, flags;
	XMP_Uns8 splitSize[4];
};

struct ID3_v22_FrameHeader {
	char id[3];
	XMP_Uns8 sizeHigh;
	XMP_Uns16 sizeLow;
};

struct ID3_v23_FrameHeader {
	char id[4];
	XMP_Uns32 size;
	XMP_Uns16 flags;
};

#define _U32b(ptr,n) ((XMP_Uns32) (((XMP_Uns8*)(ptr))[n]))
#define GetSyncSafe32(ptr)	((_U32b(ptr,0) << 21) | (_U32b(ptr,1) << 14) | (_U32b(ptr,2) <<  7) | _U32b(ptr,3))

#define GetID3Size(ver,ptr)	((ver == 3) ? GetUns32BE(ptr) : GetSyncSafe32(ptr))

// =================================================================================================

static void DumpID3v22Frames(LFA_FileRef file, XMP_Uns8 vMajor, XMP_Uns32 framePos, XMP_Uns32 frameEnd) {

	// Dump the frames in an ID3 v2.2 tag.

	while ((framePos < frameEnd) && ((frameEnd - framePos) >= 6)) {

		ID3_v22_FrameHeader frameHead;
		LFA_Seek(file, framePos, SEEK_SET);
		LFA_Read(file, &frameHead, sizeof(frameHead), true);

		if (CheckBytes(frameHead.id, "\x0", 1)) break;	// Assume into padding.
														// FIXED: there could be just 1 or 2 or 3 bytes of padding total !!

		XMP_Uns32 frameSize = ((XMP_Uns32)frameHead.sizeHigh << 16) + GetUns16BE(&frameHead.sizeLow);

		tree->setKeyValue(
			fromArgs("ID3v2:%.3s", frameHead.id), "",	//no value yet, tree->changeValue() below
			fromArgs("offset %d (0x%X), size %d", framePos, framePos, frameSize));

		if (frameSize == 0) {

			// NOTHING TO DO HERE.
			// i.e. on 0-byte frames, including known ones...
			// ( i.e. the testcase of a (errorneous) TCON 0 byte frame )

		}
		else if ((frameHead.id[0] == 'T') || (frameHead.id[0] == 'W')) { // Text and URL fields

			CaptureFileData(file, 0, frameSize);
			XMP_Uns8 encoding = 0;
			XMP_Uns8 skip = 0;
			if (frameHead.id[0] == 'T') {	// URL field has no encoding byte
				encoding = sDataPtr[0];
				skip = 1;
			}

			bool bigEndian = PrintID3Encoding(encoding, (sDataPtr + skip));
			if (encoding == 0) {
				tree->changeValue(convert8Bit(sDataPtr + skip, false, frameSize - skip));
			}
			else {
				tree->changeValue(convert16Bit(bigEndian, sDataPtr + skip, false, (frameSize - skip)));
			}

		}
		else if (CheckBytes(frameHead.id, "PRV", 3) && (frameSize >= 4)) {

			// checking on the XMP packet
			CaptureFileData(file, 0, frameSize); //NB: has side effect: sDataLen, sDataMax, sDataPtr
			tree->changeValue(convert8Bit(sDataPtr, false, strlen((char*)sDataPtr)));
			if (CheckBytes(sDataPtr, "XMP\x0", 4)) {
				CaptureXMPF(file, (framePos + sizeof(frameHead) + 4), (frameSize - 4));
			}

		}
		else if (CheckBytes(frameHead.id, "COM", 3) || CheckBytes(frameHead.id, "ULT", 3)) {

			const char * descrLabel = "ID3v2:COM-descr";
			if (CheckBytes(frameHead.id, "ULT", 3)) descrLabel = "ID3v2:ULT-descr";

			CaptureFileData(file, 0, frameSize);
			XMP_Uns8 * frameEnd2 = sDataPtr + frameSize;

			XMP_Uns8   encoding = sDataPtr[0];
			char *     lang = (char*)(sDataPtr + 1);

			tree->addComment("lang '%.3s'", lang);
			bool bigEndian = PrintID3Encoding(encoding, (sDataPtr + 4));

			if (encoding == 0) {

				XMP_Uns8 * descrPtr = sDataPtr + 4;
				XMP_Uns8 * valuePtr = descrPtr;

				while (*valuePtr != 0) ++valuePtr;
				++valuePtr;

				size_t descrBytes = valuePtr - descrPtr - 1;
				tree->changeValue(convert8Bit(valuePtr, false, frameEnd2 - valuePtr));
				tree->setKeyValue(descrLabel, convert8Bit(descrPtr, false, descrBytes).c_str());

			}
			else {

				XMP_Uns16 * descrPtr = (XMP_Uns16*)(sDataPtr + 4);
				XMP_Uns16 * valuePtr = descrPtr;
				while (*valuePtr != 0) ++valuePtr;
				++valuePtr;

				size_t descrBytes = 2 * (valuePtr - descrPtr - 1);
				size_t valueBytes = 2 * ((XMP_Uns16*)frameEnd2 - valuePtr);

				tree->changeValue(convert16Bit(bigEndian, (XMP_Uns8*)valuePtr, false, valueBytes));
				tree->setKeyValue(descrLabel, convert16Bit(bigEndian, (XMP_Uns8*)descrPtr, false, descrBytes).c_str());

			}

		}

		framePos += (sizeof(frameHead) + frameSize);

	}

	if (framePos < frameEnd) {
		tree->setKeyValue("", "",
			fromArgs("Padding assumed, offset %d (0x%X), size %d", framePos, framePos, (frameEnd - framePos)));
	}

}	// DumpID3v22Frames

	// =================================================================================================

static void DumpID3v23Frames(LFA_FileRef file, XMP_Uns8 vMajor, XMP_Uns32 framePos, XMP_Uns32 frameEnd) {

	// Dump the frames in an ID3 v2.3 or v2.4 tag.
	int iIterator = 0;

	while ((framePos < frameEnd) && ((frameEnd - framePos) >= 10)) {

		ID3_v23_FrameHeader frameHead;
		LFA_Seek(file, framePos, SEEK_SET);
		LFA_Read(file, &frameHead, sizeof(frameHead), true);

		if (CheckBytes(frameHead.id, "\x0", 1)) break;	// Assume into padding.
														// FIXED: there could be just 1 or 2 or 3 bytes of padding total !!

		frameHead.size = GetID3Size(vMajor, &frameHead.size);
		frameHead.flags = GetUns16BE(&frameHead.flags);

		tree->setKeyValue(
			fromArgs("ID3v2:%.4s", frameHead.id), "",	//no value yet, tree->changeValue() below
			fromArgs("offset %d (0x%X), size %d, flags 0x%.2X", framePos, framePos, frameHead.size, frameHead.flags));

		if (frameHead.size == 0) {

			// NOTHING TO DO HERE.
			// i.e. on 0-byte frames, including known ones...
			// ( i.e. the testcase of a (errorneous) TCON 0 byte frame )

		}
		else if ((frameHead.id[0] == 'T') || (frameHead.id[0] == 'W')) { // Text and URL fields

			CaptureFileData(file, 0, frameHead.size);
			XMP_Uns8 encoding = 0;
			XMP_Uns8 skip = 0;
			if (frameHead.id[0] == 'T') {	// URL field has no encoding byte
				encoding = sDataPtr[0];
				skip = 1;
			}

			bool bigEndian = PrintID3Encoding(encoding, (sDataPtr + skip));
			if ((encoding == 0) || (encoding == 3)) {
				tree->changeValue(convert8Bit(sDataPtr + skip, false, frameHead.size - skip));
			}
			else if ((encoding == 1) || (encoding == 2)) {
				tree->changeValue(convert16Bit(bigEndian, sDataPtr + skip, false, (frameHead.size - skip)));
			}

		}
		else if (CheckBytes(frameHead.id, "PRIV", 4) && (frameHead.size >= 4)) {

			// checking on the XMP packet
			CaptureFileData(file, 0, frameHead.size); //NB: has side effect: sDataLen, sDataMax, sDataPtr
			tree->changeValue(convert8Bit(sDataPtr, false, strlen((char*)sDataPtr)));
			if (CheckBytes(sDataPtr, "XMP\x0", 4)) {
				CaptureXMPF(file, (framePos + sizeof(frameHead) + 4), (frameHead.size - 4));
			}

		}
		else if (CheckBytes(frameHead.id, "COMM", 4) || CheckBytes(frameHead.id, "USLT", 4)) {

			const char * descrLabel = "ID3v2:COMM-descr";
			if (CheckBytes(frameHead.id, "USLT", 4)) descrLabel = "ID3v2:USLT-descr";

			CaptureFileData(file, 0, frameHead.size);
			XMP_Uns8 * frameEnd2 = sDataPtr + frameHead.size;

			XMP_Uns8   encoding = sDataPtr[0];
			char *     lang = (char*)(sDataPtr + 1);

			tree->addComment("lang '%.3s'", lang);
			bool bigEndian = PrintID3Encoding(encoding, (sDataPtr + 4));

			if ((encoding == 0) || (encoding == 3)) {

				XMP_Uns8 * descrPtr = sDataPtr + 4;
				XMP_Uns8 * valuePtr = descrPtr;

				while (*valuePtr != 0) ++valuePtr;
				++valuePtr;

				size_t descrBytes = valuePtr - descrPtr - 1;
				tree->changeValue(convert8Bit(valuePtr, false, frameEnd2 - valuePtr));
				tree->setKeyValue(descrLabel, convert8Bit(descrPtr, false, descrBytes).c_str());

			}
			else if ((encoding == 1) || (encoding == 2)) {

				XMP_Uns16 * descrPtr = (XMP_Uns16*)(sDataPtr + 4);
				XMP_Uns16 * valuePtr = descrPtr;
				while (*valuePtr != 0) ++valuePtr;
				++valuePtr;

				size_t descrBytes = 2 * (valuePtr - descrPtr - 1);
				size_t valueBytes = 2 * ((XMP_Uns16*)frameEnd2 - valuePtr);

				tree->changeValue(convert16Bit(bigEndian, (XMP_Uns8*)valuePtr, false, valueBytes));
				tree->setKeyValue(descrLabel, convert16Bit(bigEndian, (XMP_Uns8*)descrPtr, false, descrBytes).c_str());

			}

		}

		else if (CheckBytes(frameHead.id, "APIC", 4)) {

			++iIterator;
			unsigned int iOffset = 0;
			CaptureFileData(file, 0, frameHead.size);

			char encoding[2];
			memset(encoding, 0x0, 2);
			encoding[0] = sDataPtr[iOffset++];
			tree->setKeyValue(fromArgs("ID3v2:APIC-encodingType_%d", iIterator), encoding);

			char * mimeType = (char*)(sDataPtr + iOffset);
			iOffset += strlen(mimeType) + 1;	//1 is for null termination 
			tree->setKeyValue(fromArgs("ID3v2:APIC-mimeType_%d", iIterator), mimeType);

			char pictureType[2];
			memset(pictureType, 0x0, 2);
			pictureType[0] = sDataPtr[iOffset++];
			tree->setKeyValue(fromArgs("ID3v2:APIC-pictureType_%1d", iIterator), pictureType);

			bool bigEndian = PrintID3Encoding(encoding[0], (sDataPtr + iOffset));
			if (encoding[0] == 0x00) {

				XMP_Uns8 * descrPtr = sDataPtr + iOffset;
				XMP_Uns8 * valuePtr = descrPtr;

				while (*valuePtr != 0) ++valuePtr;
				++valuePtr;	//Null termination

				size_t descrBytes = valuePtr - descrPtr;
				tree->setKeyValue(fromArgs("ID3v2:APIC-descr_%d", iIterator), convert8Bit(descrPtr, false, descrBytes - 1).c_str());
				iOffset += descrBytes;
			}
			else if (encoding[0] == 0x01) {

				XMP_Uns16 * descrPtr = (XMP_Uns16*)(sDataPtr + iOffset);
				XMP_Uns16 * valuePtr = descrPtr;

				while (*valuePtr != 0) ++valuePtr;
				++valuePtr;	//Null termination

				size_t descrBytes = 2 * (valuePtr - descrPtr);
				tree->setKeyValue(fromArgs("ID3v2:APIC-descr_%d", iIterator), convert16Bit(bigEndian, (XMP_Uns8*)(descrPtr + 1), false, descrBytes - 4).c_str());
				iOffset += descrBytes;
			}

			XMP_Uns8 *picPtr = (sDataPtr + iOffset);
			unsigned long size_PictureData = frameHead.size - iOffset;

			char picDataSize[8];
			memset(picDataSize, 0x0, 8);
			sprintf(picDataSize, "%lu", size_PictureData);

			std::string picData;
			picData.assign((char*)picPtr, size_PictureData);

			tree->setKeyValue(fromArgs("ID3v2:APIC-pictureData_%d", iIterator), picData);
			tree->setKeyValue(fromArgs("ID3v2:APIC-pictureDataSize_%d", iIterator), picDataSize);
		}

		framePos += (sizeof(frameHead) + frameHead.size);

	}

	if (iIterator) {
		char noOfAPICs[2];
		memset(noOfAPICs, 0x0, 2);
		sprintf(noOfAPICs, "%d", iIterator);
		tree->setKeyValue("ID3v2:NoOfAPIC", noOfAPICs);
	}

	if (framePos < frameEnd) {
		tree->setKeyValue("", "",
			fromArgs("Padding assumed, offset %d (0x%X), size %d", framePos, framePos, (frameEnd - framePos)));
	}

}	// DumpID3v23Frames

	// =================================================================================================

static void
DumpMP3(LFA_FileRef file, XMP_Uns32 /*mp3Len*/)
{
	// ** We're ignoring the effects of the unsync flag, and not checking the CRC (if present).
	assert(sizeof(ID3_Header) == 10);
	assert(sizeof(ID3_v23_FrameHeader) == 10);

	// Detect ID3v1 header:
	if (LFA_Measure(file) > 128)
	{
		LFA_Seek(file, -128, SEEK_END);
		XMP_Uns32 tagID = 0xFFFFFF00 & LFA_ReadUns32_BE(file);
		if (tagID == 0x54414700) // must be "TAG"
		{
			// Dump ID3v1 header:
			tree->pushNode("ID3v1");
			Rewind(file, 1); // read one byte too many...

			tree->digestString(file, "ID3v1:title", 30, false, true);
			tree->digestString(file, "ID3v1:artist", 30, false, true);
			tree->digestString(file, "ID3v1:album", 30, false, true);
			tree->digestString(file, "ID3v1:year", 4, false, true);
			tree->digestString(file, "ID3v1:comment", 30, false, true);
			tree->digest(file, "ID3v1:genreNo", 0, 1);

			// ID3v1.1 trackNo byte dance:
			Rewind(file, 3);
			LFA_Tell(file);
			if (LFA_ReadUns8(file) == 0)
				tree->digest(file, "ID3v1:trackNo", 0, 1);

			tree->popNode();
		}
	}

	// Dump ID3v2 header:
	ID3_Header id3Head;
	LFA_Seek(file, 0, SEEK_SET);
	LFA_Read(file, &id3Head, sizeof(id3Head), true);

	if (!CheckBytes(id3Head.id3, "ID3", 3)) {
		tree->setKeyValue("No ID3v2 tag");
		return;
	}

	XMP_Uns32 id3Len = GetSyncSafe32(id3Head.splitSize);
	XMP_Uns32 framePos = sizeof(id3Head);	// The offset of the next (first) ID3 frame.
	XMP_Uns32 frameEnd = framePos + id3Len;

	tree->pushNode("ID3v2.%d.%d, size %d, flags 0x%.2X", id3Head.vMajor, id3Head.vMinor, id3Len, id3Head.flags);

	if (id3Head.flags != 0) {
		tree->addComment("%s%s%s%s",
			((id3Head.flags & 0x80) ? ", unsync" : ""),
			((id3Head.flags & 0x40) ? ", extended header" : ""),
			((id3Head.flags & 0x20) ? ", experimental" : ""),
			((id3Head.flags & 0x10) ? ", has footer" : ""));
	}

	if ((id3Head.vMajor < 2) || (id3Head.vMajor > 4)) {
		tree->addComment("   ** Unrecognized major version tree.");
		tree->popNode();
		return;
	}

	bool hasExtHeader = ((id3Head.flags & 0x40) != 0);

	// Dump the extended header if present.
	if (hasExtHeader) {
		XMP_Uns32 extHeaderLen;

		extHeaderLen = tree->digest32u(file);
		extHeaderLen = GetID3Size(id3Head.vMajor, &extHeaderLen);
		framePos += (4 + extHeaderLen);

		switch (id3Head.vMajor) {

		case 2: {
			// #error "implement"
			break;
		}

		case 3: {
			XMP_Uns16 extHeaderFlags;
			LFA_Read(file, &extHeaderFlags, 2, true);
			extHeaderFlags = GetUns16BE(&extHeaderFlags);

			XMP_Uns32 padLen;
			LFA_Read(file, &padLen, 4, true);
			padLen = GetUns32BE(&padLen);

			frameEnd -= padLen;

			tree->pushNode("Extended header MajorV3 size %d, flags 0x%.4X, pad size %d",
				extHeaderLen, extHeaderFlags, padLen);

			if (extHeaderFlags & 0x8000) {
				XMP_Uns32 crc;
				LFA_Read(file, &crc, 4, true);
				crc = GetUns32BE(&crc);
				tree->setKeyValue("CRC", fromArgs("0x%.8X", crc));
			}
			tree->popNode();
			break;
		}

		case 4: {
			XMP_Uns8 flagCount;
			LFA_Read(file, &flagCount, 1, true);

			tree->pushNode("Extended header MajorV4 size %d, flag count %d", extHeaderLen, flagCount);

			for (size_t i = 0; i < flagCount; ++i) {
				XMP_Uns8 flag;
				LFA_Read(file, &flag, 1, true);
				tree->setKeyValue(fromArgs("Flag %.2d", flag), fromArgs("0x%.2X", flag));
			}
			tree->popNode();
			break;
		}

		default:
			tree->addComment("unknown major version !");
			break;

		}

	}

	////////////////////////////////////////////////////
	// Dump the ID3 frames

	if (id3Head.vMajor == 2) {
		DumpID3v22Frames(file, id3Head.vMajor, framePos, frameEnd);
	}
	else {
		DumpID3v23Frames(file, id3Head.vMajor, framePos, frameEnd);
	}

	if (sXMPPtr != 0) DumpXMP("ID3 'PRIV' \"XMP\" frame");

	tree->popNode();
}	// DumpMP3

	// =================================================================================================

static void
PacketScan(LFA_FileRef file, XMP_Int64 fileLen)
{
	try {

		XMPScanner scanner(fileLen);
		LFA_Seek(file, 0, SEEK_SET);

		XMP_Uns8 buffer[64 * 1024];
		XMP_Uns32 filePos, readLen;

		for (filePos = 0; filePos < fileLen; filePos += readLen) {
			readLen = LFA_Read(file, buffer, sizeof(buffer), false);
			if (readLen == 0) throw std::logic_error("Empty read");
			scanner.Scan(buffer, filePos, readLen);
		}

		size_t snipCount = scanner.GetSnipCount();
		XMPScanner::SnipInfoVector snips(snipCount);
		scanner.Report(snips);

		size_t packetCount = 0;

		for (size_t s = 0; s < snipCount; ++s) {
			if (snips[s].fState == XMPScanner::eValidPacketSnip) {
				++packetCount;
				CaptureXMPF(file, (XMP_Uns32)snips[s].fOffset, (XMP_Uns32)snips[s].fLength);
				DumpXMP("packet scan");
			}
		}

		if (packetCount == 0) tree->addComment("   No packets found");

	}
	catch (...) {

		tree->addComment("** Scanner failure tree.");

	}

}	// PacketScan

	// =================================================================================================
	// External Routines

namespace DumpFile_NS {
	// ! Xcode compiler warns about normal offsetof macro.
#define SafeOffsetOf(type,field)	((size_t)(&(((type*)1000)->field)) - 1000)

	//assure that packing is 100% tight
	//see above:
	// - #pragma pack (1)
	// - SafeOffsetOf  macro definition
	//
	// calling this at least once is an extremly good idea, 
	// because among other reasons, the #pragma pack directive
	// is not ANSI-C thus things could go wrong on one platform or another...
	//
	// returns nothing, but asserts will be triggered if something is wrong.
	static bool selfTestDone = false;
	void selfTest() {
		//only very first call at each runtime runs the selfTest (mostly verify about structPacking etc...)
		if (DumpFile_NS::selfTestDone) return;

		assert(sizeof(ASF_GUID) == 16);
		assert(SafeOffsetOf(ASF_GUID, part1) == 0);

		assert(SafeOffsetOf(ASF_GUID, part2) == 4);
		assert(SafeOffsetOf(ASF_GUID, part3) == 6);
		assert(SafeOffsetOf(ASF_GUID, part4) == 8);
		assert(SafeOffsetOf(ASF_GUID, part5) == 10);

		assert(sizeof(ASF_ObjHeader) == (16 + 8));
		assert(SafeOffsetOf(ASF_ObjHeader, guid) == 0);
		assert(SafeOffsetOf(ASF_ObjHeader, size) == 16);

		assert(sizeof(ASF_FileProperties) == kASF_FilePropertiesSize);
		assert(SafeOffsetOf(ASF_FileProperties, guid) == 0);
		assert(SafeOffsetOf(ASF_FileProperties, size) == 16);
		assert(SafeOffsetOf(ASF_FileProperties, fileID) == 24);
		assert(SafeOffsetOf(ASF_FileProperties, fileSize) == 40);
		assert(SafeOffsetOf(ASF_FileProperties, creationDate) == 48);
		assert(SafeOffsetOf(ASF_FileProperties, dataPacketsCount) == 56);
		assert(SafeOffsetOf(ASF_FileProperties, playDuration) == 64);
		assert(SafeOffsetOf(ASF_FileProperties, sendDuration) == 72);
		assert(SafeOffsetOf(ASF_FileProperties, preroll) == 80);
		assert(SafeOffsetOf(ASF_FileProperties, flags) == 88);
		assert(SafeOffsetOf(ASF_FileProperties, minDataPacketSize) == 92);
		assert(SafeOffsetOf(ASF_FileProperties, maxDataPacketSize) == 96);
		assert(SafeOffsetOf(ASF_FileProperties, maxBitrate) == 100);

		assert(sizeof(ASF_ContentDescription) == kASF_ContentDescriptionSize);
		assert(SafeOffsetOf(ASF_ContentDescription, guid) == 0);
		assert(SafeOffsetOf(ASF_ContentDescription, size) == 16);
		assert(SafeOffsetOf(ASF_ContentDescription, titleLen) == 24);
		assert(SafeOffsetOf(ASF_ContentDescription, authorLen) == 26);
		assert(SafeOffsetOf(ASF_ContentDescription, copyrightLen) == 28);
		assert(SafeOffsetOf(ASF_ContentDescription, descriptionLen) == 30);
		assert(SafeOffsetOf(ASF_ContentDescription, ratingLen) == 32);

		assert(sizeof(InDesignMasterPage) == kINDD_PageSize);
		assert(SafeOffsetOf(InDesignMasterPage, fGUID) == 0);
		assert(SafeOffsetOf(InDesignMasterPage, fMagicBytes) == 16);
		assert(SafeOffsetOf(InDesignMasterPage, fObjectStreamEndian) == 24);
		assert(SafeOffsetOf(InDesignMasterPage, fIrrelevant1) == 25);
		assert(SafeOffsetOf(InDesignMasterPage, fSequenceNumber) == 264);
		assert(SafeOffsetOf(InDesignMasterPage, fIrrelevant2) == 272);
		assert(SafeOffsetOf(InDesignMasterPage, fFilePages) == 280);
		assert(SafeOffsetOf(InDesignMasterPage, fIrrelevant3) == 284);

		assert(sizeof(InDesignContigObjMarker) == 32);
		assert(SafeOffsetOf(InDesignContigObjMarker, fGUID) == 0);
		assert(SafeOffsetOf(InDesignContigObjMarker, fObjectUID) == 16);
		assert(SafeOffsetOf(InDesignContigObjMarker, fObjectClassID) == 20);
		assert(SafeOffsetOf(InDesignContigObjMarker, fStreamLength) == 24);
		assert(SafeOffsetOf(InDesignContigObjMarker, fChecksum) == 28);

		selfTestDone = true;
	} // selfTest

} /*namespace DumpFile_NS*/

  // -------------------------------------------------------------------------------------------------

void DumpFile::Scan(std::string filename, TagTree &tagTree, bool resetTree)
{
	DumpFile_NS::selfTest(); //calls selftest (will happen only once per runtime, optimization done)

	if (resetTree)
	{
		tagTree.reset();
	}

	tree = &tagTree; // static "global" helper to avoid looping throug 'tree' 24x7

					 // Read the first 4K of the file into a local buffer and determine the file format.
					 // ! We're using ANSI C calls that don't handle files over 2GB.
					 // ! Should switch to copies of the "LFA" routines used inside XMP.

	LFA_FileRef fileRef = LFA_Open(filename.c_str(), 'r');

	assertMsg(std::string("can't open ") + filename, fileRef != 0);

	LFA_Seek(fileRef, 0, SEEK_END);
	XMP_Int64 fileLen = LFA_Tell(fileRef);

	XMP_Uns8 first4K[4096];

	LFA_Seek(fileRef, 0, SEEK_SET);
	LFA_Read(fileRef, first4K, 4096, false);

	LFA_Seek(fileRef, 0, SEEK_SET); //rewinds
									// (remains rewinded behind CheckFileDFormat, since that call does not get the fileRef handle)

	XMP_FileFormat format = CheckFileFormat(filename.c_str(), first4K, fileLen);

	if (sXMPPtr != 0) free(sXMPPtr);
	sXMPPtr = 0;
	sXMPMax = 0;
	sXMPLen = 0;
	sXMPPos = 0;

	//TODO refactor-out
	XMP_Uns8 * fileContent = 0;	// *** Hack for old file-in-RAM code.

	if (format == kXMP_JPEGFile) {

		tagTree.pushNode("Dumping JPEG file");
		tagTree.addComment("size %lld (0x%llx)", fileLen, fileLen);
		{
			fileContent = (XMP_Uns8*)malloc(fileLen);
			LFA_Seek(fileRef, 0, SEEK_SET);
			LFA_Read(fileRef, fileContent, fileLen, true);
			DumpJPEG(fileContent, fileLen);
		}
		tagTree.popNode();

	}
	else if (format == kXMP_PhotoshopFile) {

		tagTree.pushNode("Dumping Photoshop file");
		tagTree.addComment("size %lld (0x%llx)", fileLen, fileLen);
		{
			fileContent = (XMP_Uns8*)malloc(fileLen);
			LFA_Seek(fileRef, 0, SEEK_SET);
			LFA_Read(fileRef, fileContent, fileLen, true);
			DumpPhotoshop(fileContent, fileLen);
		}
		tagTree.popNode();

	}
	else if (format == kXMP_TIFFFile) {

		tagTree.pushNode("Dumping TIFF file");
		tagTree.addComment("size %lld (0x%llx)", fileLen, fileLen);
		{
			fileContent = (XMP_Uns8*)malloc(fileLen);
			LFA_Seek(fileRef, 0, SEEK_SET);
			LFA_Read(fileRef, fileContent, fileLen, true);
			DumpTIFF(fileContent, fileLen, 0, "TIFF file", "");
		}
		tagTree.popNode();

	}
	else if (format == kXMP_WMAVFile) {

		tagTree.pushNode("Dumping ASF (WMA/WMV) file");
		tagTree.addComment("size %lld (0x%llx)", fileLen, fileLen);
		DumpASF(fileRef, fileLen);
		tagTree.popNode();

	}
	else if (format == kXMP_AVIFile) {

		tagTree.pushNode("Dumping AVI file");
		tagTree.addComment("size %lld (0x%llx)", fileLen, fileLen);
		DumpRIFF(fileRef, fileLen);
		tagTree.popNode();

	}
	else if (format == kXMP_WAVFile) {

		tagTree.pushNode("Dumping WAV file");
		tagTree.addComment("size %lld (0x%llx)", fileLen, fileLen);
		DumpRIFF(fileRef, fileLen);
		tagTree.popNode();

	}
	else if (format == kXMP_AIFFFile) {

		tagTree.pushNode("Dumping AIFF file");
		tagTree.addComment("size %lld (0x%llx)", fileLen, fileLen);
		DumpAIFF(fileRef, fileLen);
		tagTree.popNode();

	}
	else if (format == kXMP_MPEG4File || format == kXMP_MOVFile || format == kXMP_JPEG2KFile || format == kXMP_HEIFFile) {
		// all ISO formats ( MPEG4, MOV, JPEG2000,HEIF) handled jointly,
		// - no longer relying on any advance "isQT" flagging
		tagTree.pushNode("ISO file");
		Log::info("size: %d", fileLen);
		//	tagTree.addComment ( "size %I64d (0x%I64X)", fileLen, fileLen );
		tagTree.addComment("size %lld (0x%llX)", fileLen, fileLen);
		ISOMetaKeys.clear();
		DumpISO(fileRef, fileLen);
		tagTree.popNode();

	}
	else if (format == kXMP_PNGFile) {

		tagTree.pushNode("Dumping PNG file");
		tagTree.addComment("size %lld (0x%llx)", fileLen, fileLen);
		DumpPNG(fileRef, fileLen);
		tagTree.popNode();

	}
	else if (format == kXMP_InDesignFile) {

		tagTree.pushNode("Dumping InDesign file");
		tagTree.addComment("size %lld (0x%llx)", fileLen, fileLen);
		DumpInDesign(fileRef, fileLen);
		tagTree.popNode();

	}
	else if (format == kXMP_SWFFile) {

		tagTree.pushNode("Dumping SWF file");
		tagTree.addComment("size %lld (0x%llx)", fileLen, fileLen);
		DumpSWF(fileRef, fileLen);
		tagTree.popNode();

	}
	else if (format == kXMP_FLVFile) {

		tagTree.pushNode("Dumping FLV file");
		tagTree.addComment("size %lld (0x%llx)", fileLen, fileLen);
		DumpFLV(fileRef, fileLen);
		tagTree.popNode();

	}
	else if (format == kXMP_MP3File) {

		tagTree.pushNode("Dumping MP3 file");
		tagTree.addComment("size %lld (0x%llx)", fileLen, fileLen);
		DumpMP3(fileRef, fileLen);
		tagTree.popNode();

	}
	else if (format == kXMP_UCFFile) {

		tagTree.pushNode("Dumping UCF (Universal Container Format) file");
		tagTree.addComment("size %lld (0x%llx)", fileLen, fileLen);
		DumpUCF(fileRef, fileLen);
		tagTree.popNode();

	}
	else if (format == kXMP_MPEGFile) {

		tagTree.comment("** Recognized MPEG-2 file type, but this is a pure sidecar solution. No legacy dump available at this time.");

	}
	else if (format == kXMP_PostScriptFile) {

		tagTree.pushNode("Dumping PostScript file");
		tagTree.addComment("size %lld (0x%llx)", fileLen, fileLen);
		DumpPS(fileRef, fileLen);
		tagTree.popNode();

	}
	else if (format == kXMP_SVGFile) {

		tagTree.pushNode("Dumping SVG file");
		tagTree.addComment("size %lld (0x%llx)", fileLen, fileLen);
		DumpSVG(fileRef, fileLen);
		tagTree.popNode();
	}
	else if (format == kXMP_UnknownFile) {

		tagTree.pushNode("Unknown format. packet scanning, size %d (0x%X)", fileLen, fileLen);
		PacketScan(fileRef, fileLen);
		tagTree.popNode();

	}
	else {
		tagTree.comment("** Recognized file type, '%.4s', but no smart dumper for it.", &format);
	}

	if (fileContent != 0) free(fileContent);
	LFA_Close(fileRef);

}	// DumpFile


void DumpFile::dumpFile(std::string filename)
{
	TagTree localTree;
	DumpFile::Scan(filename, localTree); // (important test in itself for validity)
	localTree.dumpTree();
}