summaryrefslogtreecommitdiffstats
path: root/tools/h5recover/h5recover.c
blob: f8ca64a1203a072cbd23337d3d3aa7cb6ee55493 (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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright by The HDF Group.                                               *
 * Copyright by the Board of Trustees of the University of Illinois.         *
 * All rights reserved.                                                      *
 *                                                                           *
 * This file is part of HDF5.  The full HDF5 copyright notice, including     *
 * terms governing use, modification, and redistribution, is contained in    *
 * the files COPYING and Copyright.html.  COPYING can be found at the root   *
 * of the source code distribution tree; Copyright.html can be found at the  *
 * root level of an installed copy of the electronic HDF5 document set and   *
 * is linked from the top-level documents page.  It can also be found at     *
 * http://hdfgroup.org/HDF5/doc/Copyright.html.  If you do not have          *
 * access to either file, you may request a copy from help@hdfgroup.org.     *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#define H5F_PACKAGE		/* suppress error about including H5Fpkg   */
#define H5O_PACKAGE             /* suppress error about including H5Opkg   */
#define H5C2_PACKAGE            /* suppress error about including H5C2pkg  */

#include "hdf5.h"
#include "h5tools.h"
#include "h5tools_utils.h"
#include "H5private.h"
#include "H5Iprivate.h"
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include "H5Fpkg.h"
#include "H5Opkg.h"
#include "H5C2pkg.h"

#define H5F_MAX_SUPERBLOCK_SIZE 134

#define MAX_PATH_LEN	(2 * 1024)

const char *    progname="h5recover";
int             d_status;

/* Define valid command line options. */
static const char *s_opts = "hb:j:fvnVx";
static struct long_options l_opts[] = {
    { "help", no_arg, 'h' },
    { "backup", require_arg, 'b' },
    { "journal", require_arg, 'j' },
    { "force", no_arg, 'f' },
    { "verbose", no_arg, 'v' },
    { "nocopy", no_arg, 'n' },
    { "version", no_arg, 'V' },
    { "examine", no_arg, 'x' },
    { NULL, 0, '\0' }
};


#define MAX_ADDR_SIZE	16
#define MAX_LENGTH_SIZE	16

/* Utility Function Declarations: */

void address_decode(size_t sizeof_addr, 
                    const uint8_t **pp /* in, out */, 
                    haddr_t *addr_p/*out*/);

void address_encode(size_t sizeof_addr, 
                    uint8_t **pp /* in, out */, 
                    haddr_t addr);

void length_decode(size_t sizeof_length, 
                   const uint8_t **pp /* in, out */, 
                   hsize_t *len_p /*out*/);

void length_encode(size_t sizeof_length, 
                   uint8_t **pp /* in, out */, 
                   hsize_t length);

hbool_t load_buf_from_file(int fd,
                           off_t file_len,
                           off_t offset,
                           size_t buf_len,
                           uint8_t *buf_ptr,
                           hbool_t verbose,
                           const char * desc);


/* HDF5 File Investigation Function Declarations: */

hbool_t find_super_block(int fd, 
                         off_t file_len, 
                         off_t * sb_offset_ptr);

hbool_t get_sb_base_addr(int fd, 
                         off_t file_len,
                         off_t sb_offset, 
                         int sb_ver, 
                         int offset_size,
                         haddr_t * sb_base_addr_ptr);

hbool_t get_journaling_status(char * file_path_ptr,
                              off_t * sb_offset_ptr,
                              haddr_t * base_addr_ptr,
                              int * offset_size_ptr,
                              int * length_size_ptr,
                              off_t * eoa_offset_ptr,
                              int32_t * magic_ptr,
                              char * jnl_file_name_ptr,
                              hbool_t * error_ptr,
                              hbool_t examine,
                              unsigned verbosity);

hbool_t get_file_len(char * file_path_ptr,
                     off_t * file_len_ptr);

hbool_t get_mdj_msg_addr(int fd,
                         off_t file_len,
                         int ohdr_ver,
                         int addr_size,
                         int length_size,
                         haddr_t base_addr,
                         haddr_t sb_ext_addr,
                         haddr_t * mdj_msg_addr_ptr,
                         hbool_t examine,
                         unsigned verbosity);

hbool_t get_mdj_msg_addr__scan_chunk(int fd,
                                     off_t file_len,
                                     int ohdr_ver,
                                     int addr_size,
                                     int length_size,
                                     int num_msgs,
                                     int * msg_count_ptr,
                                     int chunk_num,
                                     hbool_t creation_order,
                                     haddr_t base_addr,
                                     haddr_t chunk_addr,
                                     haddr_t chunk_size,
                                     haddr_t * mdj_msg_addr_ptr,
                                     hbool_t examine, 
                                     unsigned verbosity);

hbool_t get_mdj_msg_data(int fd,
                         off_t file_len,
                         haddr_t base_addr,
                         haddr_t mdb_msg_addr,
                         hbool_t * enabled_ptr,
                         int32_t * magic_ptr,
                         char * jnl_file_name,
                         hbool_t examine,
                         unsigned verbosity);

hbool_t get_offset_and_length_size(int fd,
                                   off_t file_len,
                                   off_t sb_offset,
                                   int sb_ver,
                                   int * offset_size_ptr,
                                   int * length_size_ptr);

hbool_t get_sb_extension_addr(int fd,
                              off_t file_len,
                              off_t sb_offset,
                              int sb_ver,
                              int offset_size,
                              haddr_t * sb_ext_addr_ptr);

hbool_t get_sb_ext_obj_hdr_ver(int fd,
                               off_t file_len,
                               haddr_t base_addr,
                               haddr_t sb_ext_addr,
                               int * obj_hdr_ver_ptr);

hbool_t get_sb_version(int fd,
                       off_t file_len,
                       off_t sb_offset,
                       int * sb_ver_ptr);


/* HDF5 Journal File Investigation Function Declarations: */

hbool_t get_jnl_header_info(char * file_path_ptr,
                            off_t journal_file_len,
                            int32_t * version_ptr,
                            char * target_file_name_ptr,
                            int32_t * magic_ptr,
                            hbool_t * human_readable_ptr,
                            hbool_t examine,
                            unsigned verbosity);


/* functions supporting the examine option */

void examine_files(char * hdf5_file_name,
                   char * journal_file_name,
		   unsigned verbosity);


/* functions supporting recovery proper */

hbool_t verify_files(char * hdf5_file_name,
                     char * journal_file_name,
                     hbool_t force,
                     unsigned verbosity,
                     hbool_t * error_ptr);


/**************************************************************************/
/********************* HDF5 File Investigation Code ***********************/
/**************************************************************************/
/* The following functions are used to examing the target HDF5 file to    */
/* determine if the supplied journal file can be applied to it, and if so */
/* the offset of the end of file addess field in the superblock.          */
/**************************************************************************/

/**************************************************************************
 *
 * Function:	find_super_block()
 *
 * Purpose:	Scan the supplied file to determine if it is an HDF5 
 *		file, and if it is, load the offset into the file of 
 *		the superblock in *sb_offset_ptr, and return TRUE.
 *
 *		If the function fails, either because the supplied 
 *		file is not an HDF5 file, or for any other reason,
 *		*sb_offset_ptr is undefined, and the function returns
 *		FALSE
 *
 * Return:	Success: TRUE
 *		Failure: FALSE
 *
 * Programmer:	JRM -- 2/24/09
 *
 * Changes:	None.
 *
 **************************************************************************/

hbool_t
find_super_block(int fd,
                 off_t file_len,
                 off_t * sb_offset_ptr)
{
    const char * fcn_name = "find_super_block()";
    uint8_t sig_buf[H5F_SIGNATURE_LEN + 1];
    hbool_t found = FALSE;
    hbool_t success = TRUE;
    hbool_t verbose = FALSE;
    off_t offset = 0;

    if ( ( fd < 0 ) || 
         ( file_len <= 0 ) || 
         ( sb_offset_ptr == NULL ) ) {

        success = FALSE;
	HDfprintf(stderr, "%s bad params on entry.\n", fcn_name);
    }

    while ( ( success ) && ( ! found ) ) 
    {
        if ( (offset + H5F_SIGNATURE_LEN) >= file_len ) {

            success = FALSE;

            if ( verbose ) {

                HDfprintf(stderr, "%s: Target file is not an HDF5 file.\n");
            }
        } else if ( ! load_buf_from_file(fd, file_len, offset, 
                                         (size_t)H5F_SIGNATURE_LEN, sig_buf, 
                                         verbose, fcn_name) ) {

            success = FALSE;

            if ( verbose ) {

                HDfprintf(stderr, "%s: load_buf_from_file() failed.\n", 
                          fcn_name);
            }
        } else if ( HDmemcmp(sig_buf, 
                             H5F_SIGNATURE, 
                             (size_t)H5F_SIGNATURE_LEN) == 0 ) {

            found = TRUE;
            *sb_offset_ptr = offset;

        } else if ( offset == 0 ) {

            offset = 512;

        } else {

            offset *= 2;

        }
    }

    return(success);

} /* find_super_block() */


/**************************************************************************
 *
 * Function:	get_sb_base_addr()
 *
 * Purpose:	Read the base address from the super block in the supplied
 *		file using the given superblock offset, load that value 
 *		into *sb_base_addr_ptr, and return TRUE.
 *
 *		If the function fails for any other reason, 
 *		*sb_base_addr_ptr is undefined, and the function returns 
 *		FALSE.
 *
 * Return:	Success: TRUE
 *		Failure: FALSE
 *
 * Programmer:	JRM -- 2/24/09
 *
 * Changes:	None.
 *
 **************************************************************************/

hbool_t
get_sb_base_addr(int fd,
                 off_t file_len,
                 off_t sb_offset,
                 int sb_ver,
                 int offset_size,
                 haddr_t * sb_base_addr_ptr)
{
    const char * fcn_name = "get_sb_base_addr(): ";
    uint8_t addr_buf[MAX_ADDR_SIZE + 1];
    const uint8_t * buf_ptr = addr_buf;
    hbool_t success = TRUE;
    hbool_t verbose = FALSE;
    off_t sb_base_addr_offset;
    haddr_t sb_base_addr;

    if ( ( fd < 0 ) || 
         ( (sb_offset % 512) != 0 ) || 
         ( offset_size <= 0 ) ||
         ( sizeof(haddr_t) > (size_t)offset_size ) ||
         ( offset_size > MAX_ADDR_SIZE ) ||
         ( sb_base_addr_ptr == NULL ) ) {

        success = FALSE;
	HDfprintf(stderr, "%s bad params on entry.\n", fcn_name);
    }

    if ( sb_ver != 2 ) {

        success = FALSE;
	HDfprintf(stderr, "%s Unknown SB version (%d).\n", fcn_name, sb_ver);
    }

    if ( success ) {

        sb_base_addr_offset = sb_offset + H5F_SIGNATURE_LEN + 4;

        if ( ! load_buf_from_file(fd, file_len, sb_base_addr_offset, 
                                  (size_t)offset_size, addr_buf, 
                                  verbose, fcn_name) ) {

            success = FALSE;

            if ( verbose ) {

                HDfprintf(stderr, "%s: load_buf_from_file() failed.\n", 
                          fcn_name);
            }
        } else {

            address_decode((size_t)offset_size, &buf_ptr, &sb_base_addr);
 
            if ( buf_ptr != &(addr_buf[offset_size]) ) {

                success = FALSE;

                if ( verbose ) {

                    HDfprintf(stdout, 
                            "%s: Unexpected buf_ptr after address_decode().\n",
                             fcn_name);
                }
            } else {

                *sb_base_addr_ptr = sb_base_addr;

            }
        }
    }

    return(success);

} /* get_sb_base_addr() */


/**************************************************************************
 *
 * Function:	get_journaling_status()
 *
 * Purpose:	Determine if the target file exists -- if it doesn't, 
 *              issue a diagnostic, and return FALSE.
 *
 *		If it exists, determine if it is an HDF5 file -- if it 
 *		isn't, issue a diagnostic, and return FALSE.
 *
 *              If it is an HDF5 file, determine whether it uses a super
 *		block that supports journaling -- if it doesn't, issue a
 *		diagnostic and return FALSE.
 *
 *		If the HDF5 file uses a superblock that supports 
 *		journaling, determine whether the file is marked as 
 *		having journaling in progress -- if it is not, issue a 
 *		diagnostic and return FALSE.
 *
 *		If the target file is an HDF5 file that is marked as 
 *		having journaling in progress, determine its:
 *
 *			absolute superblock offset in the file
 *
 *			base address
 *
 *			offset size
 *
 *			length size
 *
 *			absolute offset of the end of file address in
 *			the super block.
 *
 *			magic number assigned to the journal file
 *
 *			name of the journal file
 *
 *		load these values into the locations indicated by the
 *		associated parameters, and return TRUE.
 *
 *		Set error_ptr to TRUE and return FALSE if the function 
 *		fails for any reason.
 *
 *		Note that the jnl_file_name_ptr parameter is presumed to 
 *		point to a buffer of char of length at least
 *		H5C2__MAX_JOURNAL_FILE_NAME_LEN + 1.
 *
 * Return:	TRUE  if the supplied path references an HDF5 file that
 *		      is marked as having journaling in progress.
 *
 *		FALSE if the supplied path references a file that does
 *		      not exist, doesn't reference an HDF5 file, 
 *		      references an HDF5 file that doesn't support 
 *		      journaling, references an HDF5 file that supports
 *		      journaling but that is not marked as having 
 *		      journaling in progress, or if the function fails 
 *		      for any reason.
 *
 * Programmer:	JRM -- 2/24/09
 *
 * Changes:	JRM -- 3/11/09
 *		Added the examine parameter.  This doesn't change
 *		the flow of execution, but it does cause the function to
 *		keep up a running commentary to stdout, and to suppress
 *		much of output to stderr that would normally be generated 
 *		when errors are detected.
 *
 *		JRM -- 3/19/09
 *		Added the verbosity parameter and associated code.  It
 *		doesn't change the execution, but it does control the 
 *		amount of comentary generated.
 *
 **************************************************************************/

hbool_t
get_journaling_status(char * file_path_ptr,
                    off_t * sb_offset_ptr,
                    haddr_t * base_addr_ptr,
                    int * offset_size_ptr,
                    int * length_size_ptr,
                    off_t * eoa_offset_ptr,
                    int32_t * magic_ptr,
                    char * jnl_file_name_ptr,
                    hbool_t * error_ptr,
                    hbool_t examine,
                    unsigned verbosity)
{
    const char * fcn_name = "get_journaling_status():";
    const char * indent1 = "";
    const char * indent2 = "\t";
    char jnl_file_name[H5C2__MAX_JOURNAL_FILE_NAME_LEN + 1];
    hbool_t journaling_enabled = FALSE;
    hbool_t success = TRUE;
    FILE * err_file_ptr = stderr;
    int fd = -1;
    int offset_size;
    int length_size;
    int sb_version;
    int ohdr_version;
    int32_t magic;
    off_t file_len;
    off_t sb_offset;
    off_t eoa_offset;
    haddr_t base_addr;
    haddr_t eoa_addr;
    haddr_t sb_ext_addr;
    haddr_t mdj_msg_addr = HADDR_UNDEF;

    if ( ( file_path_ptr == NULL ) || 
         ( sb_offset_ptr == NULL ) ||
         ( base_addr_ptr == NULL ) ||
         ( offset_size_ptr == NULL ) ||
         ( length_size_ptr == NULL ) ||
         ( eoa_offset_ptr == NULL ) ||
         ( magic_ptr == NULL ) ||
         ( jnl_file_name == NULL ) ||
         ( error_ptr == NULL ) ) {

        success = FALSE;
	HDfprintf(stderr, "%s: bad params on entry.\n", fcn_name);
    }

    if ( examine ) {

        HDfprintf(stdout, "Examining the putative HDF5 file %s:\n", 
                  file_path_ptr);

        /* We don't want to generate output to stderr when doing an examine
         * unless something is clearly wrong.
         */
        err_file_ptr = stdout;

        /* to make examine output a bit prettier */
	indent1 = "\t";
	indent2 = "\t\t";
    }

    if ( success ) {

        if ( verbosity > 0 ) {

            HDfprintf(stdout, "%sattempting to get the length of %s...\n", 
                      indent1, file_path_ptr);
        }

        success = get_file_len(file_path_ptr, &file_len);

        if ( ! success ) {

            *error_ptr = TRUE;

            HDfprintf(stderr, "Can't stat() %s.  Does the HDF5 file exist?\n",
                      file_path_ptr);

        } else {

            if ( verbosity > 0 ) {

                HDfprintf(stdout, "%sfile length = %lld\n", 
                         indent2, (long long)file_len);

            } 

            if ( file_len == 0 ) {

                success = FALSE;
                *error_ptr = TRUE;
                HDfprintf(err_file_ptr, "%s%s is empty.\n", 
                          indent1, file_path_ptr);
            }
        }
    }

    if ( success ) {

        if ( verbosity > 0 ) {

            HDfprintf(stdout, "%sattempting to open %s...\n", 
                      indent1, file_path_ptr);
        }


        /* In the following call, the mode should be ignored. */
        fd = HDopen(file_path_ptr, O_RDONLY, S_IRUSR);

        if ( fd == -1 ) {

            success = FALSE;
            *error_ptr = TRUE;
            HDfprintf(err_file_ptr, "%scan't open %s.\n", 
                      indent1, file_path_ptr);

        } else {

            if ( verbosity > 0 ) {

                HDfprintf(stdout, "%sopened file successfully.\n", indent2);
            }
        }
    }
 

    if ( success ) {

        if ( verbosity > 0 ) {

            HDfprintf(stdout, "%ssearching for super block...\n", indent1);
        }

        success = find_super_block(fd, file_len, &sb_offset);

        if ( ! success ) {

            *error_ptr = TRUE;

            HDfprintf(err_file_ptr, 
		      "%ssuper block not found in %s -- not an HDF5 file?\n",
		      indent1, file_path_ptr);

        } else {

            if ( verbosity > 0 ) {

                HDfprintf(stdout, "%ssuperblock found at 0x%llx.\n",
                          indent2, (unsigned long long)sb_offset);
            }
        }
    }

    if ( success ) {

        if ( verbosity > 0 ) {

            HDfprintf(stdout, "%sreading super block version...\n", indent1);
        }

        success = get_sb_version(fd, file_len, sb_offset, &sb_version);

        if ( ! success ) {

            *error_ptr = TRUE;

            HDfprintf(err_file_ptr, 
		     "%scan't get super block verison in %s -- file corrupt?\n",
                     indent1, file_path_ptr);

        } else {

            if ( verbosity > 0 ) {

                HDfprintf(stdout, "%ssuper block version = %d.\n",
                          indent2, sb_version);
            }

            if ( ( sb_version == 0 ) || ( sb_version == 1 ) ) {

                success = FALSE;
                *error_ptr = TRUE;

                HDfprintf(err_file_ptr, 
                        "%sjournaling is not supported in the HDF5 file %s.\n",
                        indent1, file_path_ptr);
                HDfprintf(err_file_ptr,
                        "%sthe file may or may not be corrupt, but its super\n",
                        indent1);
                HDfprintf(err_file_ptr,
                        "%sblock doesn't support journaling.\n",
                        indent1);

            } else if ( sb_version != 2 ) {

                success = FALSE;
                *error_ptr = TRUE;

                HDfprintf(err_file_ptr,
                          "%sthe HDF5 file \"%s\" uses an unknown super\n",
                          indent1, file_path_ptr);
                HDfprintf(err_file_ptr,
                          "%sblock version.\n",
                          indent1);
            } 
        }
    }

    if ( success ) {

        if ( verbosity > 0 ) {

            HDfprintf(stdout, 
		      "%sreading offset and length sizes from super block...\n",
                      indent1);
        }

        success = get_offset_and_length_size(fd, file_len, sb_offset, 
                                             sb_version, &offset_size, 
                                             &length_size);

        if ( ! success ) {

            *error_ptr = TRUE;

            HDfprintf(err_file_ptr, 
                      "%scan't get offset & length sizes from %s.\n",
                      indent1, file_path_ptr);
            HDfprintf(err_file_ptr, "%ssuper block corrupt?\n", indent1);

        } else {

            if ( verbosity > 0 ) {

                HDfprintf(stdout, "%soffset_size = %d.\n", 
                          indent2, offset_size);
                HDfprintf(stdout, "%slength_size = %d.\n", 
                          indent2, length_size);
            }
        }
    }

    if ( success ) { /* make sure offset size is OK */

        if ( sizeof(haddr_t) < (size_t)offset_size ) {

            /* we have a problem -- current definition of haddr_t is too
             * small to represent offsets in the target hdf5 file.  Must 
             * abort.
             */

            success = FALSE;
            *error_ptr = TRUE;

            HDfprintf(err_file_ptr, 
                      "%sOffsets in %s are too big to fit in haddr_t.\n",
                      indent1, file_path_ptr);

            HDfprintf(err_file_ptr, "%ssizeof(haddr_t) = %d.\n", 
                      indent2, (int)(sizeof(haddr_t)));

        } else if ( offset_size > MAX_ADDR_SIZE ) {

            success = FALSE;
            *error_ptr = TRUE;

            HDfprintf(err_file_ptr, 
                      "Offsets in %s are too big for the address buffers in\n",
                      file_path_ptr);

            HDfprintf(err_file_ptr, 
                     "this program.  Increase MAX_ADDR_SIZE and recompile.\n");
        }
    }

    if ( success ) { /* make sure length size is OK */

        if ( sizeof(hsize_t) < (size_t)length_size ) {

            /* we have a problem -- current definition of hsize_t is too
             * small to represent object lengths in the target hdf5 file.  
             * Must abort.
             */

            success = FALSE;
            *error_ptr = TRUE;

            HDfprintf(err_file_ptr, 
                      "%sLengths in %s are too big to fit in hsize_t.\n",
                      indent1, file_path_ptr);

            HDfprintf(err_file_ptr, "%ssizeof(hsize_t) = %d.\n", 
                      indent2, (int)(sizeof(hsize_t)));

        } else if ( length_size > MAX_LENGTH_SIZE ) {

            success = FALSE;
            *error_ptr = TRUE;

            HDfprintf(stderr, 
                      "Lengths in %s are too big for the length buffers in\n",
                      file_path_ptr);

            HDfprintf(stderr, 
                   "this program.  Increase MAX_LENGTH_SIZE and recompile.\n");
        }
    }

    if ( success ) { /* compute eoa offset */

        if ( verbosity > 0 ) {

            HDfprintf(stdout, "%scomputing eoa addr and offset...\n", indent1);
        }

        switch ( sb_version ) 
        {
            case 2:
                eoa_addr = (haddr_t)(12 + (2 * offset_size));
                eoa_offset = sb_offset + (off_t)(eoa_addr);

                if ( verbosity > 0 ) {

                    HDfprintf(stdout, 
			      "%seoa_addr = 0x%llx\n%seoa_offset = 0x%llx\n",
                              indent2, (long long)eoa_addr, 
                              indent2, (long long)eoa_offset);
                }
                break;

            default:
                success = FALSE;
                *error_ptr = TRUE;
                HDfprintf(stderr, "unknown/unsupported SB version.\n");
                HDfprintf(stderr, "This statement should be unreachable.\n");
                break;
        }
    }


    if ( success ) {

        if ( verbosity > 0 ) {

            HDfprintf(stdout, "%sreading base addr from super block...\n",
                      indent1);

        }

        success = get_sb_base_addr(fd, file_len, sb_offset, sb_version, 
                                   offset_size, &base_addr);

        if ( ! success ) {

            *error_ptr = TRUE;

            HDfprintf(err_file_ptr, "%scan't read base addr from %s.\n",
                      indent1, file_path_ptr);

            HDfprintf(err_file_ptr, "%ssuper block corrupt?\n", indent2);

        } else {

            if ( verbosity > 0 ) {

                HDfprintf(stdout, "%sbase addr = 0x%llx\n",
                          indent2, (unsigned long long)base_addr);
            }
        }
    }


    if ( success ) {

        if ( verbosity > 0 ) {

            HDfprintf(stdout, "%sreading super block extension addr...\n",
                      indent1);
        }

        success = get_sb_extension_addr(fd, file_len, sb_offset, sb_version, 
                                        offset_size, &sb_ext_addr);

        if ( ! success ) {

            *error_ptr = TRUE;

            HDfprintf(err_file_ptr, 
	 	      "%scan't read SB extension addr from %s.\n",
                      indent1, file_path_ptr);

            HDfprintf(err_file_ptr, "%ssuper block corrupt?\n", indent2);

        } else {

            if ( verbosity > 0 ) {

                HDfprintf(stdout, "%ssuper block extension addr = 0x%llx\n",
                          indent2, (unsigned long long)sb_ext_addr);
            }

            if ( sb_ext_addr == HADDR_UNDEF ) {

                success = FALSE;

                HDfprintf(stdout, 
                          "%sThe file %s has no super block extension.\n",
                           indent1, file_path_ptr);
                HDfprintf(stdout, "%s%s %s\n", indent1,
                          "Thus either journaling was not enabled,",
                          "or the file was closed properly.");
            }
        }
    }

    if ( success ) {

        if ( verbosity > 0 ) {

            HDfprintf(stdout, 
                      "%sreading SB extension object header version...\n",
                      indent1);
        }

        success = get_sb_ext_obj_hdr_ver(fd, file_len, base_addr, 
                                         sb_ext_addr, &ohdr_version);

        if ( ! success ) {

            *error_ptr = TRUE;

            HDfprintf(err_file_ptr, 
                      "%scan't read SB extension ohdr ver from %s\n",
                      indent1, file_path_ptr);

            HDfprintf(err_file_ptr, "%sbad address or corrupt ohdr?\n",
                      indent2);

        } else {

            if ( verbosity > 0 ) {

                HDfprintf(stdout, "%sobject header version = %d.\n", 
                          indent2, ohdr_version);
            }

            if ( ( ohdr_version != 1 ) && ( ohdr_version != 2 ) ) {

                success = FALSE;

                *error_ptr = TRUE;

                HDfprintf(err_file_ptr, "%s%s %s\n",
                          indent1,
                          "%sread unknown super block extension",
                          "object header version");
                HDfprintf(err_file_ptr, "%sfrom %s.  %s\n",
                          indent1,
                          file_path_ptr,
                          "Don't know how to find journaling message.");
            }
        }
    }

    if ( success ) {

        if ( verbosity > 0 ) {

            HDfprintf(stdout, 
		    "%sscanning SB extension for metadata journaling msg...\n",
                    indent1);
        }

        success = get_mdj_msg_addr(fd, file_len, ohdr_version, offset_size,
                                   length_size, base_addr, sb_ext_addr, 
                                   &mdj_msg_addr, examine, verbosity);

        if ( ! success ) {

            *error_ptr = TRUE;

            if ( verbosity > 1 ) {

                HDfprintf(stderr, "%s: get_mdj_msg_addr() reports an error.\n",
                          fcn_name);
            } 
        } else {

            if ( ( verbosity > 0 ) && ( mdj_msg_addr != HADDR_UNDEF ) ) {

                HDfprintf(stdout, 
			  "%smetadata journaling msg addr = 0x%llx\n",
                          indent2, (unsigned long long)mdj_msg_addr);
            }

            if ( mdj_msg_addr == HADDR_UNDEF ) {

		/* the metadata journaling message doesn't exist -- this 
                 * implies that metadata journaling is not enabled and that
		 * it is therefore impossible to run a journal.
                 *
                 * Issue a message to this effect and set success to FALSE.
                 * 
                 * Note that this is not an error, so we don't set 
                 * *error_ptr to TRUE.
                 */

		success = FALSE;
            
                HDfprintf(stdout, 
		          "%sno metadata journaling message found in %s.\n",
                          indent1, file_path_ptr);
                HDfprintf(stdout, "%s%s %s\n", indent1,
                          "Thus either journaling was not enabled,",
                          "or the file was closed properly.");
            }
        }
    }

    if ( success ) {

        if ( verbosity > 0 ) {

            HDfprintf(stdout, "%sreading metadata journaling message...\n",
                      indent1);
        }

        success = 
	    get_mdj_msg_data(fd, file_len, base_addr, mdj_msg_addr, 
                             &journaling_enabled, &magic, jnl_file_name, 
                             examine, verbosity);

        if ( ! success ) {

            *error_ptr = TRUE;
            
            HDfprintf(err_file_ptr, 
                      "%sCan't read metadata journal msg data from %s.\n",
                      indent1, file_path_ptr);
        }
    }

    if ( fd != -1 ) {

        if ( verbosity > 0 ) {

            HDfprintf(stdout, "%sattempting to close %s...\n", 
                      indent1, file_path_ptr);
        }

        if ( HDclose(fd) != 0 ) {

            HDfprintf(stderr, "%s: Error closing %s.  errno = %s (%s).\n",
                      fcn_name, file_path_ptr, errno, HDstrerror(errno));

        } else if ( verbosity > 0 ) {

            HDfprintf(stdout, "%sclosed file successfully\n", indent2);
        }
    }

    if ( success ) {

        /* copy relevant data into the locations pointed to by the 
         * supplied parameters.
         */
        if ( journaling_enabled ) {

            *sb_offset_ptr    = sb_offset;
            *base_addr_ptr    = base_addr;
            *offset_size_ptr  = offset_size;
            *length_size_ptr  = length_size;
            *eoa_offset_ptr   = eoa_offset;
            *magic_ptr        = magic;
            strcpy(jnl_file_name_ptr, jnl_file_name);

            if ( examine ) {

                HDfprintf(stdout, 
                          "%s%s was journaled and not closed properly.\n",
                          indent1, file_path_ptr);
                HDfprintf(stdout, "%s%s %s\n",
                          indent1,
		          "It is probably corrupt, and it cannot be",
                          "opened by the HDF5 library.");
                HDfprintf(stdout, "%sjournaling enabled = %d\n", 
                          indent2, (int)journaling_enabled);
                HDfprintf(stdout, "%smagic = 0x%lx\n", 
                          indent2, (unsigned long)magic);
                HDfprintf(stdout, "%sjournal file name = \"%s\".\n",
                          indent2, jnl_file_name);
            }
        } else { 

            /* nothing to do -- set success to FALSE */
            success = FALSE;

            if ( examine ) {

                HDfprintf(stdout, "%scan't run a journal on %s.\n",
                           indent1, file_path_ptr);
            }
        }
    }

    return(success);

} /* get_journaling_status() */


/**************************************************************************
 *
 * Function:	get_file_len()
 *
 * Purpose:	Stat the supplied file, load its size (in bytes)
 *		in *file_len_ptr, and return TRUE.
 *
 *		If the file does not exist, or if the function fails
 *		for any other reason, *file_len_ptr is undefined, and 
 *		the function returns FALSE.
 *
 * Return:	Success: TRUE
 *		Failure: FALSE
 *
 * Programmer:	JRM -- 2/24/09
 *
 * Changes:	None.
 *		
 *
 **************************************************************************/

hbool_t
get_file_len(char * file_path_ptr,
             off_t * file_len_ptr)
{
    const char * fcn_name = "get_file_len(): ";
    hbool_t success = TRUE;
    hbool_t verbose = FALSE;
    h5_stat_t buf;

    if ( ( file_path_ptr == NULL ) || ( file_len_ptr == NULL ) ) {

        success = FALSE;
	HDfprintf(stderr, "%s: bad params on entry.\n", fcn_name);
    }

    if ( success ) {

        if ( HDstat(file_path_ptr, &buf) != 0 ) {

            success = FALSE;

            if ( verbose ) {
            
                HDfprintf(stderr, "%s: Can't stat %s.\n", 
                          fcn_name, file_path_ptr);
            }
        } else {

            *file_len_ptr = buf.st_size;

        }
    }

    return(success);

} /* get_file_len() */


/**************************************************************************
 *
 * Function:	get_mdj_msg_addr
 *
 * Purpose:	Given the base address of the HDF5 file and the relative
 *		address of the object header containing the super block 
 *		extension messages, try to find the address of the metadata 
 *		journaling message if it exists.  
 *
 *		If it exists, set *mdj_msg_addr_ptr to the relative address
 *		of the message in the file, and return TRUE.
 *
 *		It it does not, set *mdj_msg_addr_ptr to HADDR_UNDEF and 
 *		return TRUE.
 *
 *		If any errors are detected, return FALSE.  
 *              In this case, *mdj_msg_addr_ptr is undefined.
 *
 * Return:	Success: TRUE if no errors are detected.
 *		Failure: FALSE if any error is detected.
 *
 * Programmer:	JRM -- 2/24/09
 *
 * Changes:	JRM -- 3/12/09
 *		Added the examine parameter.  This boolean flag doesn't
 *		change processing, but does cause the function to maintain
 *		a running commentary on its activities.
 *
 *		JRM -- 3/19/09
 *		Added the verbosity parameter and associated code.  It
 *		doesn't change the execution, but it does control the 
 *		amount of comentary generated.
 *
 **************************************************************************/

hbool_t
get_mdj_msg_addr(int fd,
                 off_t file_len,
                 int ohdr_ver,
                 int addr_size,
                 int length_size,
                 haddr_t base_addr,
                 haddr_t sb_ext_addr,
                 haddr_t * mdj_msg_addr_ptr,
                 hbool_t examine,
                 unsigned verbosity)
{
    const char * fcn_name = "get_mdj_msg_addr()";
    const char * indent1 = "";
    const char * indent2 = "\t";
    hbool_t creation_order_stored;	/* version 2 header only */
    hbool_t phase_change_values_stored;	/* version 2 header only */
    hbool_t times_stored;		/* version 2 header only */
    hbool_t success = TRUE;
    hbool_t verbose = FALSE;
    uint8_t buf[64];
    const uint8_t * p = buf;
    uint8_t flags;
    int msg_count = 0;
    uint16_t num_hdr_msgs;		/* version 1 header only */
    uint32_t junk;    
    uint32_t ohdr_size;			/* version 1 header only */
    uint64_t chunk_0_size;
    int size_of_chunk_0_size;		/* version 2 header only */
    haddr_t chunk_0_addr;
    haddr_t mdj_msg_addr = HADDR_UNDEF;
    off_t offset;
    FILE * err_file_ptr = stderr;

    if ( ( fd < 0 )  || 
         ( ( ohdr_ver != 1 ) && ( ohdr_ver != 2 ) ) ||
         ( mdj_msg_addr_ptr == NULL ) ) {

        success = FALSE;
	HDfprintf(stderr, "%s: bad params on entry.\n", fcn_name);
    }

    if ( examine ) {

        /* We don't want to generate output to stderr when doing an examine
         * unless something is clearly wrong.
         */
        err_file_ptr = stdout;

        /* to make examine output a bit prettier */
	indent1 = "\t";
	indent2 = "\t\t";
    }


    switch ( ohdr_ver ) {

        case 1:

            if ( success ) {

                if ( verbosity > 0 ) {

                    HDfprintf(stdout, "%spreparing to examine v1 SBE ohdr...\n",
                              indent1);
                }

                /* set the offset to point to the number of messages */
                offset = (off_t)(base_addr + sb_ext_addr + 2);

                success = load_buf_from_file(fd, file_len, offset, (size_t)10, 
                                             buf, verbose, fcn_name);

                if ( ! success ) {

                    HDfprintf(err_file_ptr, 
		      "%scan't load section of ohdr -- file corruption?\n",
                      indent1);
                }
            }

            if ( success ) {

                p = buf;
                UINT16DECODE(p, num_hdr_msgs);
                UINT32DECODE(p, junk);
                UINT32DECODE(p, ohdr_size);

                if ( verbosity > 0 ) {
                
                    HDfprintf(stdout, "%snum hdr msgs = %d.\n",
			      indent2, (int)num_hdr_msgs);
		    HDfprintf(stdout, "%schunk 0 base addr = 0x%llx.\n",
                              indent2, (unsigned long long)(sb_ext_addr + 12));
		    HDfprintf(stdout, "%schunk 0 length = 0x%llx.\n",
                              indent2, (unsigned long long)ohdr_size);
                }
            }

            if ( success ) {

                success = get_mdj_msg_addr__scan_chunk(fd,
                                                   file_len,
                                                   ohdr_ver,
                                                   addr_size,
                                                   length_size,
                                                   num_hdr_msgs,
                                                   &msg_count,
                                                   0,
                                                   FALSE,
                                                   base_addr,
                                                   (haddr_t)(sb_ext_addr + 12),
                                                   (haddr_t)ohdr_size,
                                                   &mdj_msg_addr,
                                                   examine,
                                                   verbosity);
            }
            break;

        case 2:

            if ( ( success ) && ( verbosity > 0 ) ) {

                HDfprintf(stdout, "%spreparing to examine v2 SBE ohdr...\n",
                          indent1);
            }

            if ( success ) { /* read the object header flags */

                offset = (off_t)(base_addr + sb_ext_addr + 5);

                success = load_buf_from_file(fd, file_len, offset, (size_t)1, 
                                              &flags, verbose, fcn_name);
            }

            if ( success ) { /* decode the flags we care about */

                switch ( flags & 0x3 )
                {
                    case 0:
                        size_of_chunk_0_size = 1;
                        break;

                    case 1:
                        size_of_chunk_0_size = 2;
                        break;

                    case 2:
                        size_of_chunk_0_size = 4;
                        break;

                    case 3:
                        size_of_chunk_0_size = 8;
                        break;

                    default:
                        /* this case is unreachable -- included to keep the
                         * compiler happy.
                         */
                       HDfprintf(stderr, "%s: reached the unreachable.\n",
                                 fcn_name);
                       break;
                }

                if ( verbosity > 0 ) {

                    HDfprintf(stdout, "%ssize of chunk size = %d\n",
                              indent2, size_of_chunk_0_size);
                }

                if ( (flags & 0x4) != 0 ) {

                    creation_order_stored = TRUE;

                } else {

                    creation_order_stored = FALSE;

                }

                if ( verbosity > 0 ) {

                    HDfprintf(stdout, "%screation order stored = %d\n",
                              indent2, (int)creation_order_stored);
                }

                if ( (flags & 0x10) != 0 ) {

		    phase_change_values_stored = TRUE;

                } else {

		    phase_change_values_stored = FALSE;

                }

                if ( verbosity > 0 ) {

                    HDfprintf(stdout, "%sphase change values stored = %d\n",
                              indent2, (int)phase_change_values_stored);
                }

                if ( (flags & 0x20) != 0 ) {

		    times_stored = TRUE;

		} else {

		    times_stored = FALSE;

		}

                if ( verbosity > 0 ) {

                    HDfprintf(stdout, "%stimes stored = %d\n",
                              indent2, (int)times_stored);
                }
            }

            if ( success ) { /* get the chunk 0 size */

                offset = (off_t)(base_addr + sb_ext_addr + 6);

                if ( times_stored ) {

		    offset += (off_t)16;
		}

                if ( phase_change_values_stored ) {

		    offset += (off_t)4;
                }

                success = load_buf_from_file(fd, file_len, offset, 
                                             (size_t)size_of_chunk_0_size, 
                                              buf, verbose, fcn_name);
            }

            if ( success ) {

		p = buf;
                chunk_0_size = 0;

                switch ( size_of_chunk_0_size )
                {
                    case 1:
                        chunk_0_size = (uint64_t)(*p);
                        HDassert( chunk_0_size < 256 );
                        break;

                    case 2:
                        {
                            uint16_t tmp;
                            UINT16DECODE(p, tmp);
                            chunk_0_size = (uint64_t)tmp;
                            HDassert( chunk_0_size < 65536 );
                        }
                        break;

                    case 4:
                        {
                            uint32_t tmp;
                            UINT32DECODE(p, tmp);
                            chunk_0_size = (uint64_t)tmp;
                            HDassert( chunk_0_size <= 4294967295UL );
                        }
                        break;

                    case 8:
                        UINT64DECODE(p, chunk_0_size);
                        break;

                    default:
                        success = FALSE;
                        HDfprintf(stderr, "%s: unknown chunk 0 size (%d).\n",
                                  fcn_name, size_of_chunk_0_size);
                        break;
                }

                if ( verbosity > 0 ) {

                    HDfprintf(stdout, "%schunk 0 size = 0x%llx\n",
                              indent2, (unsigned long long)chunk_0_size);
                }
            }

            if ( success ) {

                if ( size_of_chunk_0_size > addr_size ) {

                    success = FALSE;
                    HDfprintf(err_file_ptr, 
                              "%s%s %s\n",
                              indent1,
                              "size of chunk 0 size larger than file"
                              "address size!?!");
                    HDfprintf(err_file_ptr, 
                              "%ssize_of_chunk_0_size = %d > addr_size = %d.\n",
                              indent1, size_of_chunk_0_size, addr_size);

                } else {

                    chunk_0_addr = (haddr_t)(sb_ext_addr + 6);

                    if ( times_stored ) {

		        chunk_0_addr += (haddr_t)16;
		    }

                    if ( phase_change_values_stored ) {

		        chunk_0_addr += (haddr_t)4;
                    }

                    chunk_0_addr += (haddr_t)size_of_chunk_0_size;


                    if ( verbosity > 0 ) {

                        HDfprintf(stdout, "%schunk 0 addr = 0x%llx\n",
                                  indent2, (unsigned long long)chunk_0_addr);
                    }
                }
            }

            if ( success ) {

                success = get_mdj_msg_addr__scan_chunk(fd,
                                                       file_len,
                                                       ohdr_ver,
                                                       addr_size,
                                                       length_size,
                                                       num_hdr_msgs,
                                                       &msg_count,
                                                       0,
                                                       creation_order_stored,
                                                       base_addr,
                                                       chunk_0_addr,
                                                       (haddr_t)chunk_0_size,
                                                       &mdj_msg_addr,
                                                       examine,
                                                       verbosity);
            }
            break;

        default:
	    HDfprintf(stderr, "%s: ohdr_ver = %d out of range.\n", 
                      fcn_name, ohdr_ver);
            break;
    }

    if ( ( success ) && ( mdj_msg_addr != HADDR_UNDEF ) ) {

        *mdj_msg_addr_ptr = mdj_msg_addr;
    }

    return(success);

} /* get_mdj_msg_addr() */


/**************************************************************************
 *
 * Function:	get_mdj_msg_addr__scan_chunk()
 *
 * Purpose:	Given the address of a chunk of messages associated with 
 *		the object header containing the super block extension
 *		of the supplied file, scan the chunk for a metadata 
 *		journaling message, and return its address in 
 *		*mdj_msg_addr_ptr if one is found.
 *
 *		*mdj_msg_addr_ptr is undefined if no metadata journaling
 *		message is found.
 *
 *		If any errors are detected, return FALSE.  
 *              In this case, *mdj_msg_addr_ptr is undefined.
 *
 * Return:	Success: TRUE if no errors are detected.
 *		Failure: FALSE if any error is detected.
 *
 * Programmer:	JRM -- 2/24/09
 *
 * Changes:	JRM -- 3/12/09
 *		Added the examine parameter.  This boolean flag doesn't
 *		change processing, but does cause the function to maintain
 *		a running commentary on its activities.
 *
 *		JRM -- 3/19/09
 *		Added the verbosity parameter and associated code.  It
 *		doesn't change the execution, but it does control the 
 *		amount of comentary generated.
 *
 **************************************************************************/

hbool_t
get_mdj_msg_addr__scan_chunk(int fd,
                             off_t file_len,
                             int ohdr_ver,
                             int addr_size,
                             int length_size,
                             int num_msgs,  /* ohdr version 1 only */
                             int * msg_count_ptr,  /* ohdr version 1 only */
                             int chunk_num,
                             hbool_t creation_order, /* ohdr version 2 only */
                             haddr_t base_addr,
                             haddr_t chunk_addr,
                             haddr_t chunk_size,
                             haddr_t * mdj_msg_addr_ptr,
                             hbool_t examine, 
                             unsigned verbosity)
{
    const char * fcn_name = "get_mdj_msg_addr__scan_chunk()";
    const char * indent1 = "";
    const char * indent2 = "\t";
    uint8_t flags;
    uint8_t buf[64];
    const uint8_t * p = buf;
    uint16_t num_hdr_msgs; /* version 1 header only */
    uint16_t msg_type_num;
    uint16_t msg_data_size;
    int null_msg_count = 0;
    hbool_t success = TRUE;
    hbool_t verbose = FALSE;
    haddr_t cum_msg_size = 0;
    haddr_t signature_size = 0; /* version 2 header only */
    haddr_t msg_hdr_size;
    haddr_t msg_addr;
    haddr_t msg_data_addr;
    haddr_t new_chunk_addr;
    hsize_t new_chunk_len;
    off_t offset;
    FILE * err_file_ptr = stderr;

    /* make sure the buffer is big enough */
    HDassert( (MAX_ADDR_SIZE + MAX_LENGTH_SIZE) <= 64 );

    if ( ( fd < 0 )  || 
         ( ( ohdr_ver != 1 ) && ( ohdr_ver != 2 ) ) ||
         ( mdj_msg_addr_ptr == NULL ) || 
         ( *mdj_msg_addr_ptr != HADDR_UNDEF ) ) {

        success = FALSE;
	HDfprintf(stderr, "%s: bad params on entry.\n", fcn_name);
    }

    if ( examine ) {

        /* We don't want to generate output to stderr when doing an examine
         * unless something is clearly wrong.
         */
        err_file_ptr = stdout;

        /* to make examine output a bit prettier */
	indent1 = "\t";
	indent2 = "\t\t";
    }

    if ( success ) {

        if ( verbosity > 0 ) {

            HDfprintf(stdout, "%sscanning SB ext chunk %d.\n", 
                      indent1, chunk_num);
        }

        switch ( ohdr_ver )
        {
            case 1:
                msg_hdr_size = 8;
                break;

            case 2:
                if ( creation_order ) {

                    msg_hdr_size = 5;

                } else {

                    msg_hdr_size = 4;

                }

                if ( chunk_num != 0 ) { 
                    /* continuation chunk -- check signature */

                    signature_size = H5O_SIZEOF_MAGIC;

                    offset = (off_t)(base_addr + chunk_addr);

                    success = load_buf_from_file(fd, file_len, offset, 
                                                 (size_t)H5O_SIZEOF_MAGIC, 
                                                 buf, verbose, fcn_name);

                    if ( success ) {

                        if ( HDmemcmp(buf, 
                                      H5O_CHK_MAGIC, 
                                      (size_t)H5O_SIZEOF_MAGIC) != 0 ) {

                            success = FALSE;

                            HDfprintf(err_file_ptr, "%s%s %d %s %s\n",
                                       indent1,
                                       "SB extension continuation chunk",
                                       chunk_num,
                                       "has bad signature --",
                                       "file corruption?");
                        } else if ( verbosity > 1 ) {

                            HDfprintf(stdout, 
                                      "%sContinuation message signature OK.\n",
                                      indent2);
                        }
                    }
                }
                break;

            default: /* just keeping the compiler happy -- this can't happen */
                success = FALSE;
                HDfprintf(stderr, "%s: reached the unreachable.\n", fcn_name);
                break;
        }
    }

    /* scan the messages */
    while ( ( success ) && 
            ( cum_msg_size < chunk_size ) ) {

        msg_addr = chunk_addr + signature_size + cum_msg_size;

        if ( verbosity > 1 ) {

            HDfprintf(stdout, "%schunk_size = 0x%llx\n", 
                      indent2, (unsigned long long)chunk_size);
            HDfprintf(stdout, "%scum_msg_size = 0x%llx\n",
                      indent2, (unsigned long long)cum_msg_size);
        }

        /* set the offset to point to the header of the next message */
        offset = (off_t)(base_addr + msg_addr);

        /* load the message type, size, flags, and possibly creation order */
        success = load_buf_from_file(fd, file_len, offset, 
                                     (size_t)msg_hdr_size, 
                                     buf, verbose, fcn_name);

        if ( success ) {

            p = buf;

            switch ( ohdr_ver )
            {
                case 1:
                    UINT16DECODE(p, msg_type_num);
                    UINT16DECODE(p, msg_data_size);
                    flags = (uint8_t)(*p);
                    break;

                case 2:
                    msg_type_num = (uint16_t)(*p++);
                    UINT16DECODE(p, msg_data_size);
                    flags = (uint8_t)(*p);
                    break;

                default: /* keeping the compiler happy -- this can't happen */
                    success = FALSE;
                    HDfprintf(stderr, "%s: reached the unreachable.\n", 
                    fcn_name);
                    break;
            }
        }

        if ( ( success ) && ( verbosity > 1 ) ) {

            HDfprintf(stdout, 
                      "%smsg: %d  type = %d  dsize = %d flags = 0x%x\n",
                      indent2, *msg_count_ptr, (int)msg_type_num, 
                      (int)msg_data_size, (unsigned)flags);
        }

        msg_data_addr = msg_addr + msg_hdr_size;

        if ( success ) {

            switch ( msg_type_num ) 
            {

                case H5O_MDJ_MSG_ID: 
                    if ( verbosity > 0 ) {

                        HDfprintf(stdout, 
				  "%sfound journaling message at 0x%llx\n",
                                  indent1,
                                  (unsigned long long)msg_data_addr);
                    }

                    if ( *mdj_msg_addr_ptr == HADDR_UNDEF ) {

                        *mdj_msg_addr_ptr = msg_data_addr;

                    } else {

                        /* found a duplicate metadata journaling 
                         * message -- scream and die.
                         */

                        success = FALSE;

                        HDfprintf(err_file_ptr, "%s%s %s",
                                  indent1,
                                  "Found duplicate metadata jouraling",
                                  "message in super block extension.\n");
                        HDfprintf(err_file_ptr, "%s%s %s",
                                  indent1,
                                  "Probably a bug in the library. ",
                                  "Exiting without action.\n");
                    }
                    break;

                case H5O_NULL_ID:
                    null_msg_count++;
                    break;

                case H5O_SHMESG_ID:
                case H5O_BTREEK_ID:
		case H5O_DRVINFO_ID:
                    /* These messages are allowed in superblock 
		     * extension messages -- just skip over them.
                     */
		    break;

                case H5O_CONT_ID:
                    /* read the message and then scan the chunk it
                     * points to.
                     */
                    if ( verbosity > 0 ) {

                        HDfprintf(stdout, 
                                 "%dfound continuation msg -- chunk %d\n",
                                 indent1,
                                 chunk_num + 1);
                    }

                    success = load_buf_from_file(fd, file_len, offset, 
                                         (size_t)(addr_size + length_size + 1), 
                                         buf, verbose, fcn_name);

                    if ( success ) {

                        address_decode((size_t)addr_size, &p, &new_chunk_addr);
 
                        if ( p != &(buf[addr_size]) ) {

                            success = FALSE;

                            if ( verbose ) {

                                HDfprintf(stdout, "%s: %s %s\n",
                                          fcn_name,
                                          "Unexpected p after",
                                          "address_decode().\n");
                            }
                        }
                    }

                    if ( success ) {

                        length_decode((size_t)length_size, &p, &new_chunk_len);
 
                        if ( p != &(buf[addr_size + length_size]) ) {

                            success = FALSE;

                            if ( verbose ) {

                                HDfprintf(stdout, "%s: %s %s\n",
                                          fcn_name,
                                          "Unexpected p after",
                                          "length_decode().\n");
                            }
                        }
                    }
                    
                    if ( ( success ) && ( verbosity > 0 ) ) {

                        HDfprintf(stdout, "%schunk %d addr = 0x%llx\n",
                                  indent2,
                                  chunk_num + 1,
                                  (unsigned long long)new_chunk_addr);
                        HDfprintf(stdout, "%schunk %d length = 0x%llx\n",
                                  indent2,
                                  chunk_num + 1,
                                  (unsigned long long)new_chunk_len);
                    }

                    if ( success ) {

                        success = get_mdj_msg_addr__scan_chunk
                                  (
                                      fd,
                                      file_len,
                                      ohdr_ver,
                                      addr_size,
                                      length_size,
                                      num_msgs,
                                      msg_count_ptr,
                                      chunk_num + 1,
                                      creation_order,
                                      base_addr,
                                      new_chunk_addr,
                                      new_chunk_len,
                                      mdj_msg_addr_ptr,
                                      examine,
                                      verbosity
                                  );
                    }
                    break;

                case H5O_SDSPACE_ID:
                case H5O_LINFO_ID:
                case H5O_DTYPE_ID:
                case H5O_FILL_ID:
                case H5O_FILL_NEW_ID:
                case H5O_LINK_ID:
                case H5O_EFL_ID:
                case H5O_LAYOUT_ID:
                case H5O_BOGUS_ID:
                case H5O_GINFO_ID:
                case H5O_PLINE_ID:
                case H5O_ATTR_ID:
                case H5O_NAME_ID:
                case H5O_MTIME_ID:
                case H5O_STAB_ID:
                case H5O_MTIME_NEW_ID:
                case H5O_AINFO_ID:
                case H5O_REFCOUNT_ID:
                    /* these messages clearly shouldn't be in 
                     * the superblock extension -- scream and die.
                     */
                    success = FALSE;

                    HDfprintf(err_file_ptr, 
                            "%sSB ext chunk %d contains a dis-allowed mssg.\n",
                            indent1, chunk_num);
                    HDfprintf(stdout, "%sprobably a bug in library\n", indent1);
                    break;

                default:
                    /* Unknown message -- scream, but don't die. */
                    HDfprintf(err_file_ptr, "%sSB ext chunk %d %s\n",
                              indent1, chunk_num,
                              "appears to contain an unknown mssg.");
                    break;
            }

            cum_msg_size += (haddr_t)(msg_hdr_size + msg_data_size);

            (*msg_count_ptr)++;
        }

        /* no gaps possible in version 1, but must check for version 2 */
        if ( ( success ) && ( ohdr_ver == 2 ) ) {

            if ( ( cum_msg_size < chunk_size ) &&
                 ( (chunk_size - cum_msg_size) <= msg_hdr_size ) ) {

                if ( null_msg_count > 0 ) {

                    success = FALSE;

                    HDfprintf(err_file_ptr, 
			      "%sfound gap in SB ext chunk with null msg.\n",
                              indent1);
                    HDfprintf(err_file_ptr, "%slibrary bug?.\n", indent1);

                }
                cum_msg_size = chunk_size;
            }
        }

        /* check for too many messages -- version 1 ohdr only */
        if ( ( success ) && 
             ( ohdr_ver == 1 ) &&
             ( *msg_count_ptr > num_hdr_msgs ) ) {

            success = FALSE;

            HDfprintf(err_file_ptr, 
                      "%sSB extension has too many messages.\n",
                      indent1);
            HDfprintf(err_file_ptr, "%slibrary bug?.\n", indent1);

        }
    } /* end while */

    if ( verbose ) {

        HDfprintf(stdout, "%s: exiting while loop.\n", fcn_name);
        HDfprintf(stdout, "%s: chunk_size = 0x%llx\n", fcn_name,
                  (unsigned long long)chunk_size);
        HDfprintf(stdout, "%s: cum_msg_size = 0x%llx\n", fcn_name,
                  (unsigned long long)cum_msg_size);
    }


    return(success);

} /* get_mdj_msg_addr__scan_chunk() */


/**************************************************************************
 *
 * Function:	get_mdj_msg_data
 *
 * Purpose:	Given the base address of the HDF5 file and the relative
 *		address of the metadata journaling super block extension
 *		message, read the message, copy its contents into the
 *		locations indicated by the enabled_ptr, magic_ptr, and
 *		jnl_file_name parameters, and return TRUE.  
 *
 *		Note that only * enabled_ptr is defined if journaling is
 *		not enabled.
 * 
 *		Note also that jnl_file_name is presumed to point to a 
 *		buffer of char of length at least 
 *		H5C2__MAX_JOURNAL_FILE_NAME_LEN + 1.
 *
 *		If any errors are detected, return FALSE.  
 *              In this case, *enabled_ptr, *magic_ptr and the 
 *		contents of jnl_file_name are undefined.
 *
 * Return:	Success: TRUE if no errors are detected.
 *		Failure: FALSE if any error is detected.
 *
 * Programmer:	JRM -- 2/24/09
 *
 * Changes:	JRM -- 3/12/09
 *		Added the examine parameter.  This boolean flag doesn't
 *		change processing, but does cause the function to maintain
 *		a running commentary on its activities.
 *
 *		JRM -- 3/19/09
 *		Added the verbosity parameter and associated code.  It
 *		doesn't change the execution, but it does control the 
 *		amount of comentary generated.
 *
 **************************************************************************/

#define MDJ_MSG_HDR_LEN	( 1 + 	/* version number */			\
			  2 +	/* flags */				\
			  4 +	/* magic -- sizeof(int32_t) */		\
			  4 )	/* jnl file path len -- sizeof(int32_t) */

#define MAX_MDJ_MSG_LEN	(MDJ_MSG_HDR_LEN + H5C2__MAX_JOURNAL_FILE_NAME_LEN + 1)

hbool_t
get_mdj_msg_data(int fd,
                 off_t file_len,
                 haddr_t base_addr,
                 haddr_t mdj_msg_addr,
                 hbool_t * enabled_ptr,
                 int32_t * magic_ptr,
                 char * jnl_file_name,
                 hbool_t examine,
                 unsigned verbosity)
{
    const char * fcn_name = "get_mdj_msg_data()";
    const char * indent1 = "";
    const char * indent2 = "\t";
    hbool_t journaling_enabled = FALSE;
    hbool_t success = TRUE;
    hbool_t verbose = FALSE;
    uint8_t buf[MAX_MDJ_MSG_LEN + 1];
    uint8_t * p;
    uint16_t flags;
    int32_t magic;
    uint32_t path_len;
    int version;
    off_t offset;
    FILE * err_file_ptr = stderr;

    if ( ( fd < 0 ) || 
         ( enabled_ptr == NULL ) ||
         ( magic_ptr == NULL ) ||
         ( jnl_file_name == NULL ) ) {

        success = FALSE;
	HDfprintf(stderr, "%s: bad params on entry.\n", fcn_name);

    }

    if ( examine ) {

        /* We don't want to generate output to stderr when doing an examine
         * unless something is clearly wrong.
         */
        err_file_ptr = stdout;

        /* to make examine output a bit prettier */
	indent1 = "\t";
	indent2 = "\t\t";
    }


    if ( ( success ) && ( verbosity > 0 ) ) {

        HDfprintf(stdout, "%sreading metadata journaling message.\n", indent1);
    }

    if ( success ) {

        offset = (off_t)(base_addr + mdj_msg_addr);

        success = load_buf_from_file(fd, file_len, offset, 
                                     (size_t)MDJ_MSG_HDR_LEN, 
                                     buf, verbose, fcn_name);
    }

    if ( success ) {

        version = (int)(buf[0]);

        if ( verbosity > 0 ) {

            HDfprintf(stdout, "%sMDJ msg version = %d\n", indent2, version);
        }

        /* Only know about one version -- scream and die if unknown version */
        if ( version != H5O_MDJ_CONF_VERSION ) {

            success = FALSE;

            HDfprintf(err_file_ptr, "%s%s %s (%d) %s\n",
                      indent1,
                      "found metadata journaling message with",
                      "unkown version",
                      version,
                      "-- file corruption?");
        }
    }

    if ( success ) {

        p = &(buf[1]);
        UINT16DECODE(p, flags);
        INT32DECODE(p, magic);
        UINT32DECODE(p, path_len);

        if ( (flags & MDJ_MSG__JOURNALING_ENABLED_FLAG) == 0 ) {

            /* journaling is disabled -- this case shouldn't occur so we 
             * we will print a warning.
             */
            journaling_enabled = FALSE;

            HDfprintf(err_file_ptr, 
                      "%s%s %s\n",
                      indent1,
                      "Found a metadata journaling message saying that",
            	      "journaling is disabled.");

        } else {
       
            journaling_enabled = TRUE;

            if ( verbosity > 0 ) {

                HDfprintf(stdout, "%sjournaling enabled = TRUE\n", indent2);
                HDfprintf(stdout, "%smagic = 0x%x\n", indent2, (unsigned)magic);
                HDfprintf(stdout, "%sjournal file path len = %d\n", 
                          indent2, (int)path_len);
            }

            if ( ( path_len <= 0 ) || 
                 ( path_len > H5C2__MAX_JOURNAL_FILE_NAME_LEN ) ) {

                success = FALSE;

                HDfprintf(err_file_ptr, "%s%s %s (%d).\n",
                          indent1,
                          "Encountered metadata journaling message with",
                          "path len our of range", 
                          path_len);
            }
        }
    }

    if ( ( success ) && ( journaling_enabled ) ) {

        offset = (off_t)(base_addr + mdj_msg_addr + MDJ_MSG_HDR_LEN);

        success = load_buf_from_file(fd, file_len, offset, 
                                     (size_t)(path_len + 1), 
                                     buf, verbose, fcn_name);
    }

    if ( ( success ) && ( journaling_enabled ) ) {

        if ( strlen((char *)buf) != (size_t)path_len ) {

            success = FALSE;

            HDfprintf(err_file_ptr, 
                      "Encountered metadata journaling message with\n");
            HDfprintf(err_file_ptr, 
                      "path_len not matching actual path len.\n");
        }
    }

    if ( success ) {

        if ( journaling_enabled ) {

            *enabled_ptr = TRUE;
            *magic_ptr = magic;
            strcpy(jnl_file_name, (char *)buf);

            if ( verbosity > 0 ) {

                HDfprintf(stdout, "	journal file = \"%s\"\n", buf);
            }
        } else {

            *enabled_ptr = FALSE;

        }
    }

    return(success);

} /* get_mdj_msg_data() */


/**************************************************************************
 *
 * Function:	get_offset_and_length_size
 *
 * Purpose:	Obtain the number of bytes required to represent offsets
 *		and lengths in the target HDF5 file, load these values into 
 *		*offset_size_ptr and *length_size_ptr, and return TRUE.
 *
 *		If the function fails for any other reason, 
 *		*offset_size_ptr is undefined, and the function 
 *		returns FALSE.
 *
 *		Note that at present, this function works only with 
 *		the HDF5 version 2 super block, as that is the only
 *		version (for now) that supports journaling.
 *
 * Return:	Success: TRUE
 *		Failure: FALSE
 *
 * Programmer:	JRM -- 2/24/09
 *
 * Changes:	None.
 *
 **************************************************************************/

hbool_t
get_offset_and_length_size(int fd,
                           off_t file_len,
                           off_t sb_offset,
                           int sb_ver,
                           int * offset_size_ptr,
                           int * length_size_ptr)
{
    const char * fcn_name = "get_offset_and_length_size()";
    uint8_t buf[2];
    hbool_t success = TRUE;
    hbool_t verbose = FALSE;
    off_t offset;
    int offset_size;
    int length_size;

    if ( ( fd < 0 ) || 
         ( (sb_offset % 512) != 0 ) || 
         ( offset_size_ptr == NULL ) ||
         ( length_size_ptr == NULL ) ) {

        success = FALSE;
	HDfprintf(stderr, "%s bad params on entry.\n", fcn_name);

    } else if ( sb_ver != 2 ) {

        success = FALSE;
	HDfprintf(stderr, "%s: Unsupported superblock version (%d).\n",
                  fcn_name, sb_ver);
    }

    if ( success ) {

        offset = sb_offset + H5F_SIGNATURE_LEN + 1;

        if ( ! load_buf_from_file(fd, file_len, offset, (size_t)2, 
                                  buf, verbose, fcn_name) ) {

            success = FALSE;

            if ( verbose ) {

                HDfprintf(stderr, "%s: load_buf_from_file() failed.\n", 
                          fcn_name);
            }
        } else {

            offset_size = (int)buf[0];
            length_size = (int)buf[1];

        }
    }

    if ( success ) {

        if ( offset_size <= 0 ) {

            success = FALSE;

            if ( verbose ) {
                HDfprintf(stderr, "%s found non-positive offset size: %d\n",
                          fcn_name, offset_size);

            }
        } else {

            *offset_size_ptr = offset_size;

        }
    }

    if ( success ) {

        if ( length_size <= 0 ) {

            success = FALSE;

            if ( verbose ) {
                HDfprintf(stderr, "%s found non-positive length size: %d\n",
                          fcn_name, length_size);

            }
        } else {

            *length_size_ptr = length_size;

        }
    }

    return(success);

} /* get_offset_and_length_size() */


/**************************************************************************
 *
 * Function:	get_sb_extension_addr()
 *
 * Purpose:	Read the super block extension address from the supplied
 *		file using the given superblock offset, load that value 
 *		into *sb_ext_addr_ptr, and return TRUE.
 *
 *		If the function fails for any other reason, 
 *		*sb_ext_addr_ptr is undefined, and the function returns 
 *		FALSE.
 *
 * Return:	Success: TRUE
 *		Failure: FALSE
 *
 * Programmer:	JRM -- 2/24/09
 *
 * Changes:	None.
 *
 **************************************************************************/

hbool_t
get_sb_extension_addr(int fd,
                      off_t file_len,
                      off_t sb_offset,
                      int sb_ver,
                      int offset_size,
                      haddr_t * sb_ext_addr_ptr)
{
    const char * fcn_name = "get_sb_extension_addr(): ";
    uint8_t addr_buf[MAX_ADDR_SIZE + 1];
    const uint8_t * buf_ptr = addr_buf;
    hbool_t success = TRUE;
    hbool_t verbose = FALSE;
    off_t sb_ext_offset;
    haddr_t sb_ext_addr;

    if ( ( fd < 0 ) || 
         ( (sb_offset % 512) != 0 ) || 
         ( offset_size <= 0 ) ||
         ( sizeof(haddr_t) > (size_t)offset_size ) ||
         ( offset_size > MAX_ADDR_SIZE ) ||
         ( sb_ext_addr_ptr == NULL ) ) {

        success = FALSE;
	HDfprintf(stderr, "%s bad params on entry.\n", fcn_name);
    }

    if ( sb_ver != 2 ) {

        success = FALSE;
	HDfprintf(stderr, "%s Unknown SB version (%d).\n", fcn_name, sb_ver);
    }

    if ( success ) {

        sb_ext_offset = sb_offset + H5F_SIGNATURE_LEN + 4 + offset_size;

        if ( ! load_buf_from_file(fd, file_len, sb_ext_offset, 
                                  (size_t)offset_size, addr_buf, 
                                  verbose, fcn_name) ) {

            success = FALSE;

            if ( verbose ) {

                HDfprintf(stderr, "%s: load_buf_from_file() failed.\n", 
                          fcn_name);
            }
        } else {

            address_decode(sizeof(haddr_t), &buf_ptr, &sb_ext_addr);
 
	    if ( buf_ptr != &(addr_buf[offset_size]) ) {

                success = FALSE;

                if ( verbose ) {

                    HDfprintf(stdout, 
                            "%s: Unexpected buf_ptr after address_decode().\n",
                            fcn_name);
                }
            } else {

                *sb_ext_addr_ptr = sb_ext_addr;

            }
        }
    }

    return(success);

} /* get_sb_extension_addr() */


/**************************************************************************
 *
 * Function:	get_sb_ext_obj_hdr_ver()
 *
 * Purpose:	Given the supplied HDF5 file base address and super block
 *		extension relative address, attempt to determin the 
 *		version of object header being used to store the super 
 *		block extension messages, and return this value in 
 *		*obj_hdr_type_ptr.
 *
 *		If successful, return TRUE.  On failure for any reason,
 *		return FALSE.
 *
 * Return:	Success: TRUE
 *		Failure: FALSE
 *
 * Programmer:	JRM -- 2/24/09
 *
 * Changes:	None.
 *
 **************************************************************************/

hbool_t
get_sb_ext_obj_hdr_ver(int fd,
                       off_t file_len,
                       haddr_t base_addr,
                       haddr_t sb_ext_addr,
                       int * ohdr_ver_ptr)
{
    const char * fcn_name = "get_sb_ext_obj_hdr_ver()";
    /* the buffer must be large enough for the object header magic and 
     * its version -- note that version 1 doesn't have magic.
     */
    uint8_t buf[H5O_SIZEOF_MAGIC + 2];
    hbool_t success = TRUE;
    hbool_t verbose = FALSE;
    int ohdr_ver;
    off_t ohdr_offset;

    if ( ( fd < 0 ) || 
         ( ohdr_ver_ptr == NULL ) ) {

        success = FALSE;
	HDfprintf(stderr, "%s bad params on entry.\n", fcn_name);
    }

    ohdr_offset = (off_t)(base_addr + sb_ext_addr);

    if ( success ) {

        if ( ! load_buf_from_file(fd, file_len, ohdr_offset, 
                                  (size_t)(H5O_SIZEOF_MAGIC + 1), buf, 
                                  verbose, fcn_name) ) {

            success = FALSE;

            if ( verbose ) {

                HDfprintf(stderr, "%s: load_buf_from_file() failed.\n",
                          fcn_name);
            }
        }
    }

    if ( success ) {

        if ( buf[0] == 1 ) {

            ohdr_ver = 1;

        } else if ( HDmemcmp(buf, 
                             H5O_HDR_MAGIC, 
                             (size_t)H5O_SIZEOF_MAGIC) == 0 ) {

            ohdr_ver = (int)(buf[H5O_SIZEOF_MAGIC]);

        } else {

            success = FALSE;

            if ( verbose ) {

                HDfprintf(stderr, 
                          "%s: sb_ext_addr doesn't seem to reference\n",
                          fcn_name);
                HDfprintf(stderr, "	an object header.\n");
                HDfprintf(stderr, "	base_addr = 0x%llx\n", 
                          (unsigned long long)base_addr);
                HDfprintf(stderr, "	sb_ext_addr = 0x%llx\n", 
                          (unsigned long long)sb_ext_addr);
            }
        }
    }

    if ( success ) {

        if ( verbose ) {

            HDfprintf(stdout, "%s: ohdr_ver = %d.\n", fcn_name, (int)ohdr_ver);
        }

        *ohdr_ver_ptr = ohdr_ver;
    }

    return(success);

} /* get_sb_ext_obj_hdr_ver() */


/**************************************************************************
 *
 * Function:	get_sb_version
 *
 * Purpose:	Read the super block version from the supplied file with 
 *		the given superblock offset, load that value into 
 *		*sb_ver_ptr, and return TRUE.
 *
 *		If the function fails for any other reason, *sb_ver_ptr 
 *		is undefined, and the function returns FALSE.
 *
 * Return:	Success: TRUE
 *		Failure: FALSE
 *
 * Programmer:	JRM -- 2/24/09
 *
 * Changes:	None.
 *
 **************************************************************************/

hbool_t
get_sb_version(int fd,
               off_t file_len,
               off_t sb_offset,
               int * sb_ver_ptr)
{
    const char * fcn_name = "get_sb_version(): ";
    uint8_t ver_buf;
    hbool_t success = TRUE;
    hbool_t verbose = FALSE;
    off_t version_offset;
    int version;

    if ( ( fd < 0 ) || 
         ( (sb_offset % 512) != 0 ) || 
         ( sb_ver_ptr == NULL ) ) {

        success = FALSE;
	HDfprintf(stderr, "%s bad params on entry.\n", fcn_name);
    }

    if ( success ) {

        version_offset = sb_offset + H5F_SIGNATURE_LEN;

        if ( ! load_buf_from_file(fd, file_len, version_offset, 
                                  (size_t)1, &ver_buf, 
                                  verbose, fcn_name) ) {

            success = FALSE;

            if ( verbose ) {

                HDfprintf(stderr, "%s: load_buf_from_file() failed.\n", 
                          fcn_name);
            }
        } else {

            version = (int)ver_buf;

        }
    }

    if ( success ) {

        if ( verbose ) {

            HDfprintf(stdout, "%s: version = %d.\n", fcn_name, (int)version);
        }

        *sb_ver_ptr = version;
    }

    return(success);

} /* get_sb_version() */


/**************************************************************************
 *
 * Function:	load_buf_from_file()
 *
 * Purpose:	Seek to the specified location in the supplied file and 
 *		then read the indicated number of bytes into the supplied
 *		buffer.
 *
 *		Return TRUE if completely successful and FALSE otherwise.
 *		*obj_hdr_type_ptr.
 *
 *		If successful, return TRUE.  On failure for any reason,
 *		return FALSE.
 *
 * Return:	Success: TRUE
 *		Failure: FALSE
 *
 * Programmer:	JRM -- 2/26/09
 *
 * Changes:	None.
 *
 **************************************************************************/

hbool_t
load_buf_from_file(int fd,
                   off_t file_len,
                   off_t offset,
                   size_t buf_len,
                   uint8_t *buf_ptr,
                   hbool_t verbose,
                   const char * desc)
{
    const char * fcn_name = "load_buf_from_file()";
    hbool_t success = TRUE;
    ssize_t result;

    if ( ( fd < 0 ) || 
         ( file_len <= 0 ) ||
         ( buf_len <= 0 ) ||
         ( buf_ptr == NULL ) ||
         ( desc == NULL ) ) {

        success = FALSE;
	HDfprintf(stderr, "%s bad params on entry.\n", fcn_name);

    } else if ( file_len < offset + (off_t)buf_len ) {

        success = FALSE;

        if ( verbose ) {

            HDfprintf(stderr, "%s:%s: file too short.\n", fcn_name, desc);
        }
    }

    if ( success ) {

        if ( offset != HDlseek(fd, offset, SEEK_SET) ) {

            success = FALSE;

            if ( verbose ) {

                HDfprintf(stderr, 
                        "%s:%s: lseek() to 0x%llx failed with errno %d(%s).\n",
                        fcn_name, 
                        desc,
                        (unsigned long long)offset,
                        errno,
                        strerror(errno));
            }
        } else {

	    result = HDread(fd, (void *)buf_ptr, buf_len);

            if ( result == -1 ) {

                success = FALSE;

                if ( verbose ) {

                    HDfprintf(stderr,
                          "%s:%s: read() at 0x%llx failed with errno %d(%s).\n",
                          fcn_name,
                          desc,
                          (unsigned long long)offset,
                          errno,
                          strerror(errno));
                }
            } else if ( result != (ssize_t)buf_len ) {

                success = FALSE;

                if ( verbose ) {

                    HDfprintf(stderr,
                     "%s:%s: read() at 0x%llx read wrong # of bytes: %d(%d).\n",
                     fcn_name,
                     desc,
                     (unsigned long long)offset,
                     (int)result,
                     (int)buf_len);
                }
            } 
        }
    }

    return(success);

} /* load_buf_from_file() */


/**************************************************************************/
/***************** HDF5 Journal File Investigation Code *******************/
/**************************************************************************/
/* The following functions are used to examing the target HDF5 journal    */
/* file to determine if it is a valid journal file, if it matches the     */
/* supplied HDF5 file, and if it contains any complete transactions.      */
/**************************************************************************/

/**************************************************************************
 *
 * Function:	get_jnl_header_info
 *
 * Purpose:	Attempt to open the indicated HDF5 journal file, read its
 *		header information, and return this data to the caller
 *		in the indicated locations after closing the file.
 *
 *		If examine is TRUE, do this while maintaining a running 
 *		commentary.
 *
 *		If successful, return TRUE.  On failure for any reason,
 *		return FALSE.
 *
 *		Note that the function assumes that target_file_name_ptr
 *		points to a string that is at least MAX_PATH_LEN + 1 
 *		bytes long.
 *
 * Return:	Success: TRUE
 *		Failure: FALSE
 *
 * Programmer:	JRM -- 3/17/09
 *
 * Changes:	JRM -- 3/19/09
 *		Added the verbosity parameter and associated code.  It
 *		doesn't change the execution, but it does control the 
 *		amount and format of comentary generated.
 *
 *		Also added file_len parameter -- now we are guaranteed 
 *		that the file exists and has positive length on entry.
 *
 **************************************************************************/

hbool_t 
get_jnl_header_info(char * file_path_ptr,
                    off_t file_len,
                    int32_t * version_ptr,
                    char * target_file_name_ptr,
                    int32_t * magic_ptr,
                    hbool_t * human_readable_ptr,
                    hbool_t examine,
                    unsigned verbosity)
{
    const char * fcn_name = "get_jnl_header_info()";
    const char * indent1 = "";
    const char * indent2 = "\t";
    char buf[MAX_PATH_LEN + 256 + 1];
    char target_file_name[MAX_PATH_LEN + 1];
    hbool_t human_readable;
    hbool_t success = TRUE;
    int version;
    int32_t magic;
    FILE * file_ptr = NULL;
    FILE * err_file_ptr = stderr;

    if ( ( file_path_ptr == NULL ) ||
         ( file_len <= 0 ) ||
         ( version_ptr == NULL ) ||
         ( target_file_name_ptr == NULL ) ||
         ( magic_ptr == NULL ) ||
         ( human_readable_ptr == NULL ) ) {
    
        success = FALSE;
        HDfprintf(stderr, "%s: bad param(s) on entry.\n", fcn_name);
    }

    if ( examine ) {

        indent1 = "\t";
        indent2 = "\t\t";

        /* don't generate output to stderr during examing unless a major 
         * error or a bug in the program is detected.
         */
         
        err_file_ptr = stdout;

    }

    if ( success ) {

        if ( verbosity > 0 ) {

            HDfprintf(stdout, "%sattempting to open %s...\n",
                      indent1, file_path_ptr);
        }

        // In the following call, the mode should be ignored.
        file_ptr = HDfopen(file_path_ptr, "r");

        if ( file_ptr == NULL ) {

            success = FALSE;

            HDfprintf(err_file_ptr, "%sCan't open %s -- permissions?.\n", 
                      indent1, file_path_ptr);

        } else {

            if ( verbosity > 0 ) {

                HDfprintf(stdout, "%sopened file successfully.\n", indent2);
            }
        }
    }

    if ( success ) {

        if ( verbosity > 0 ) {

            HDfprintf(stdout, "%sattempting to read header.\n", indent1);
        }

        if ( HDfgets(buf, MAX_PATH_LEN + 256, file_ptr) == NULL ) {

            success = FALSE;

            HDfprintf(err_file_ptr, "%sCan't read first line of %s.\n", 
                      indent1, file_path_ptr);

        } else if ( buf[0] != '0' ) {

            success = FALSE;

            HDfprintf(err_file_ptr, 
		      "%sbad first char of header -- not a journal file.\n",
                      indent1);
        }
    }

    if ( success ) { /* get version */

        char * version_tag = NULL;
        char * version_str = NULL;

        version_tag = HDstrtok(&(buf[1]), " ");

        if ( ( version_tag == NULL ) ||
             ( strcmp(version_tag, H5C2_JNL__VER_NUM_TAG) != 0 ) ) {

            success = FALSE;

            HDfprintf(err_file_ptr, "%sunexpected version tag \"%s\".\n",
                      indent1, version_tag );

        } else if ( (version_str = HDstrtok(NULL, " ")) == NULL ) {

            success = FALSE;

            HDfprintf(err_file_ptr, "%sno version string in header.\n",
                      indent1);

        } else if ( (version = atoi(version_str)) != 1 ) {

            success = FALSE;

            HDfprintf(err_file_ptr, 
                      "%sunknown journal file version \"%s\"(%d).\n", 
                      indent1, version_str, version);

        } else if ( verbosity > 0 ) {
    
            HDfprintf(stdout, "%sjournal file version = %d.\n", 
                      indent2, version);
        }
    }

    if ( success ) { /* get target file name */

        char * target_file_tag = NULL;
        char * target_file_str = NULL;

        target_file_tag = HDstrtok(NULL, " ");

        if ( ( target_file_tag == NULL ) ||
             ( strcmp(target_file_tag, H5C2_JNL__TGT_FILE_NAME_TAG) != 0 ) ) {

            success = FALSE;

            HDfprintf(err_file_ptr, "%sUnexpected target file tag \"%s\".\n",
                      indent1, target_file_tag );

        } else if ( (target_file_str = HDstrtok(NULL, " ")) == NULL ) {

            success = FALSE;

            HDfprintf(err_file_ptr, "%sno target file string in header.\n",
                      indent1);

        } else if ( strlen(target_file_str) > MAX_PATH_LEN ) {

            success = FALSE;

            HDfprintf(err_file_ptr, 
                          "%starget file path exceeds MAX_PATH_LEN.\n",
                          indent1);

        } else {

            HDstrcpy(target_file_name, target_file_str);

            if ( verbosity > 0 ) {
    
                HDfprintf(stdout, "%starget file = \"%s\"\n", 
                          indent2, target_file_name);
            }
        }
    }

    if ( success ) { /* get journal magic */

        char * journal_magic_tag = NULL;
        char * journal_magic_str = NULL;
        long tmp_magic;

        journal_magic_tag = HDstrtok(NULL, " ");

        if ( ( journal_magic_tag == NULL ) ||
             ( strcmp(journal_magic_tag, H5C2_JNL__JNL_MAGIC_TAG) != 0 ) ) {

            success = FALSE;

            HDfprintf(err_file_ptr, "%sUnexpected journal magic tag \"%s\".\n",
                      indent1, journal_magic_tag );

        } else if ( (journal_magic_str = HDstrtok(NULL, " ")) == NULL ) {

            success = FALSE;


            HDfprintf(err_file_ptr, "%sno journal magic string in header.\n",
                      indent1);

        } else if ( ( (tmp_magic = HDstrtol(journal_magic_str, NULL, 10))
                      == LONG_MIN ) ||
                    ( tmp_magic == LONG_MAX ) ) {

            success = FALSE;

            HDfprintf(err_file_ptr, "%serror reading magic %s (%d)\n",
                      indent1, errno, strerror(errno));

        } else {

            magic = (int32_t)tmp_magic;

            if ( verbosity > 0 ) {
    
                HDfprintf(stdout, "%smagic = 0x%x\n", indent2, magic);
            }
        }
    }

    if ( success ) { /* get creation date */

        char * creation_date_tag = NULL;
        char * creation_date_str = NULL;

        creation_date_tag = HDstrtok(NULL, " ");

        if ( ( creation_date_tag == NULL ) ||
             ( strcmp(creation_date_tag, H5C2_JNL__CREATION_DATE_TAG) != 0 ) ) {

            success = FALSE;

            HDfprintf(err_file_ptr, 
                      "%sUnexpected journal creation date tag \"%s\".\n",
                      indent1, creation_date_tag );

        } else if ( (creation_date_str = HDstrtok(NULL, " ")) == NULL ) {

            success = FALSE;

            HDfprintf(err_file_ptr, "%sno creation date string in header.\n",
                      indent1);

        } else if ( verbosity > 0 ) {
    
            HDfprintf(stdout, "%screation date = %s\n", 
                      indent2, creation_date_str);
        }
    }

    if ( success ) { /* get human readable */

        char * human_readable_tag = NULL;
        char * human_readable_str = NULL;
        long tmp_human_readable;

        human_readable_tag = HDstrtok(NULL, " ");

        if ( ( human_readable_tag == NULL ) ||
             ( strcmp(human_readable_tag, H5C2_JNL__HUMAN_READABLE_TAG) 
               != 0 ) ) {

            success = FALSE;

            HDfprintf(err_file_ptr, 
                      "%sUnexpected human readable tag \"%s\".\n",
                      indent1, human_readable_tag );

        } else if ( (human_readable_str = HDstrtok(NULL, " ")) == NULL ) {

            success = FALSE;

            HDfprintf(err_file_ptr, "%sno human readable string in header.\n",
                      indent1);
 
        } else if ( ( (tmp_human_readable = 
                       HDstrtol(human_readable_str, NULL, 10)) == LONG_MIN ) ||
                    ( tmp_human_readable == LONG_MAX ) ) {

            success = FALSE;

            HDfprintf(err_file_ptr, 
                      "%serror decoding human readable string: %d (%s)\n",
                      indent1, errno, strerror(errno));

        } else if ( ( tmp_human_readable != 0 ) &&
                    ( tmp_human_readable != 1 ) ) {

            success = FALSE;

            HDfprintf(err_file_ptr, 
                      "%shuman readable value out of range (%ld).\n",
                      indent1, tmp_human_readable);

        } else {

            human_readable = (hbool_t)tmp_human_readable;

            if ( verbosity > 0 ) {
    
                HDfprintf(stdout, "%shuman readable = %d\n", 
                          indent2, human_readable);
            }
        }
    }

    if ( file_ptr != NULL ) {

        if ( verbosity > 0 ) {

            HDfprintf(stdout, "%sattempting to close %s...\n",
                      indent1, file_path_ptr);
        }

        if ( HDfclose(file_ptr) != 0 ) {

            HDfprintf(stderr, "%serror closing %s.\n", 
                      indent1, file_path_ptr);

        } else if ( verbosity > 0 ) {

            HDfprintf(stdout, "%sclosed file successfully.\n", indent2);
        }
    }

    if ( success ) { /* copy results into provided locations */

        *version_ptr = version;
        strcpy(target_file_name_ptr, target_file_name);
        *magic_ptr = magic;
        *human_readable_ptr = human_readable;

    }

    return(success);

} /* get_jnl_header_info() */


/**************************************************************************/
/*********************** File Examination Code ****************************/
/**************************************************************************/
/* The following functions are used to support the examine function -- in */
/* which the supplied files are examined to determine their status, but   */
/* no action is taken.                                                    */
/**************************************************************************/

/**************************************************************************
 *
 * Function:	examine_files()
 *
 * Purpose:	Scan the supplied file(s) to determine their status, 
 *		and report.  Do nothing.
 *
 * Return:	void
 *
 * Programmer:	JRM -- 3/17/09
 *
 * Changes:	None.
 *
 **************************************************************************/

void
examine_files(char * hdf5_file_name,
              char * journal_file_name,
              unsigned verbosity)
{
    /* const char * fcn_name = "examine_files()"; */
    const char * indent1 = "\t";
    const char * indent2 = "\t\t";
    char jnl_file_name[H5C2__MAX_JOURNAL_FILE_NAME_LEN + 1];
    char jnl_target_file_name[MAX_PATH_LEN + 1];
    hbool_t error = FALSE;
    hbool_t journal_can_be_run = FALSE;
    hbool_t journal_file_exists = FALSE;
    hbool_t journal_file_ok = FALSE;
    hbool_t jnl_human_readable;
    int offset_size;
    int length_size;
    int jnl_file_version;
    int32_t magic;
    int32_t jnl_magic;
    off_t eoa_offset;
    off_t sb_offset;
    off_t journal_file_len = 0;
    haddr_t base_addr;

    if ( hdf5_file_name == NULL ) {

        HDfprintf(stdout, "No target HDF5 file supplied.\n");

    } else {

        journal_can_be_run = get_journaling_status(hdf5_file_name,
                                                   &sb_offset,
                                                   &base_addr,
                                                   &offset_size,
                                                   &length_size,
                                                   &eoa_offset,
                                                   &magic,
                                                   jnl_file_name,
                                                   &error,
                                                   TRUE,
                                                   verbosity);
        if ( error ) {

            HDfprintf(stdout, 
                "\nEncountered errors examining %s -- can't run journal.\n", 
                hdf5_file_name);
        }
    }

    if ( journal_file_name == NULL ) {

        HDfprintf(stdout, "\nNo target journal file supplied -- exiting.\n");

    } else {

        HDfprintf(stdout, "\nExamining the putative HDF5 journal file %s:\n", 
                  journal_file_name);

        if ( verbosity > 0 ) {

            HDfprintf(stdout, 
                      "%sattempting to determine journal file length...\n",
                      indent1);
        }

        if ( ! get_file_len(journal_file_name, &journal_file_len) ) {

            journal_file_exists = FALSE;
            journal_file_ok = FALSE;
            HDfprintf(stdout, 
                      "%sCan't get journal file length -- does it exist?\n\n",
                      indent1);

        } else if ( journal_file_len == 0 ) {

            journal_file_exists = TRUE;

            HDfprintf(stdout, "%sthe file %s is empty.\n\n", 
                      indent1, journal_file_name);
        
        } else {

            journal_file_exists = TRUE;

            if ( verbosity > 0 ) {

                HDfprintf(stdout, "%sfile length = %lld\n", 
                          indent2, (long long)journal_file_len);
            }
        }
    }

    if ( ( journal_file_exists) && ( journal_file_len > 0 ) ) {


        journal_file_ok = get_jnl_header_info(journal_file_name,
                                              journal_file_len,
                                              &jnl_file_version,
                                              jnl_target_file_name,
                                              &jnl_magic,
                                              &jnl_human_readable,
                                              TRUE,
                                              verbosity);

        if ( ! journal_file_ok ) {

            HDfprintf(stdout, "\nerrors reading %s.  Journal un-useable.\n\n",
                      journal_file_name);

        } else {

            HDfprintf(stdout, "%sheader in %s looks OK.\n\n", 
                      indent1, journal_file_name);

        }
    }

    if ( journal_can_be_run ) {

        if ( journal_file_ok ) {

            if ( magic == jnl_magic ) {

                HDfprintf(stdout, "magics match -- %s seems to be the\n",
                          journal_file_name);
                HDfprintf(stdout, "journal file generated by %s.\n",
                          hdf5_file_name);
                HDfprintf(stdout, "Recomend running %s on %s.\n\n",
                          journal_file_name, hdf5_file_name);

            } else {

                HDfprintf(stdout, "magic mismatch!!! Don't run %s on %s.\n\n",
                          journal_file_name, hdf5_file_name);
            }
        } else if ( ( journal_file_exists ) && 
                    ( journal_file_len == 0 ) ) {

            HDfprintf(stdout, 
"Since %s is empty, either %s\n\
already has consistent metadata and need only be marked as not having\n\
journaling in progress so it can opened with the hdf5 library, or\n\
%s is not really its journal file.\n\n\
Unfortunately, which is true can't be determined by this program.\n\
If you are sure you have the correct journal file, go ahead and run\n\
%s on %s.\n\n",
                     journal_file_name,
                     hdf5_file_name,
                     journal_file_name,
                     journal_file_name,
                     hdf5_file_name);
        }
    }

    return;

} /* examine_files() */


/**************************************************************************/
/*********************** File Verification Code ***************************/
/**************************************************************************/
/* The following function(s) are used to verify that the supplied journal */
/* file matches the supplied HDF5 file, and cancel the recovery if this   */
/* is not the case.                                                       */
/**************************************************************************/

/**************************************************************************
 *
 * Function:	verify_files()
 *
 * Purpose:	Examine the supplied HDF5 file, and determine if it is:
 *
 *			a) really an HDF5 file, 
 *
 *			b) uses a version of the HDF5 file format that 
 *			   supports journaling, and
 *
 *			c) is marked as having journaling in progress.
 *
 *		If any of these conditions fails to hold, there is nothing
 *		to be done -- issue a diagnostic and return false.
 *
 *		If all the above conditions hold, examine the supplied 
 *		journal file and determine if it is either:
 *
 *			a) and empty file, or 
 *
 *			b) an HDF5 journal file with magic number matching
 *			   that of the supplied HDF5 file.
 *
 *		In the first case, if the force flag is not set, notify the 
 *		user that the journal file is empty, that we therefore 
 *		cannot verify that it is the correct journal file for the 
 *		supplied HDF5 file, and give the user the chance to abort 
 *		the recovery.  Return false if the the recovery is aborted,
 *		and true if it is allowed to proceed.
 *
 *		In the second case, just return true
 *
 *		If neither condition holds, issue an diagnostic and return
 *		false.
 *
 * Return:	true if the recovery is to proceed, and false otherwise.
 *
 * Programmer:	JRM -- 3/23/09
 *
 * Changes:	None.
 *
 **************************************************************************/

hbool_t
verify_files(char * hdf5_file_name,
             char * journal_file_name,
             hbool_t force,
             unsigned verbosity,
             hbool_t * error_ptr)
{
    const char * fcn_name = "verify_files()";
    char jnl_file_name[H5C2__MAX_JOURNAL_FILE_NAME_LEN + 1];
    char jnl_target_file_name[MAX_PATH_LEN + 1];
    hbool_t proceed = TRUE;
    hbool_t journal_file_exists = FALSE;
    hbool_t jnl_human_readable;
    int offset_size;
    int length_size;
    int jnl_file_version;
    int32_t magic;
    int32_t jnl_magic;
    off_t eoa_offset;
    off_t sb_offset;
    off_t journal_file_len = 0;
    haddr_t base_addr;

    if ( ( hdf5_file_name == NULL ) ||
         ( journal_file_name == NULL ) ||
         ( error_ptr == NULL ) ||
         ( *error_ptr != FALSE ) ) {

        proceed = FALSE;
        HDfprintf(stderr, "%s: bad param(s) on entry.\n", fcn_name);

    } 

    if ( proceed ) {

        proceed = get_journaling_status(hdf5_file_name,
                                        &sb_offset,
                                        &base_addr,
                                        &offset_size,
                                        &length_size,
                                        &eoa_offset,
                                        &magic,
                                        jnl_file_name,
                                        error_ptr,
                                        FALSE,
                                        verbosity);

        if ( *error_ptr ) {

            HDfprintf(stderr, 
                  "\nEncountered errors examining %s -- can't run journal.\n\n",
                  hdf5_file_name);

        } else if ( ! proceed ) {

            HDfprintf(stderr, 
                      "\nJournaling not enabled and/or supported on %s\n",
                      hdf5_file_name);
            HDfprintf(stderr, "Can't run journal.\n\n");

        } else {

            HDfprintf(stdout, 
                      "\n%s was journaled and not closed properly.\n",
                      hdf5_file_name);

            if ( verbosity > 0 ) {

                HDfprintf(stdout, "\tmagic = 0x%lx\n", (unsigned long)magic);
                HDfprintf(stdout, "\tjournal file name = \"%s\".\n", 
                          jnl_file_name);
            }
        }
    }

    if ( proceed ) {

        if ( ! get_file_len(journal_file_name, &journal_file_len) ) {

            proceed = FALSE;
            *error_ptr = TRUE;
            journal_file_exists = FALSE;
            HDfprintf(stdout, 
                      "\nCan't get journal file length -- does it exist?\n\n");

        } else {

            journal_file_exists = TRUE;

        }
    }

    if ( ( proceed ) && 
         ( journal_file_exists) ) {

        if ( journal_file_len > 0 ) {

            proceed = get_jnl_header_info(journal_file_name,
                                          journal_file_len,
                                          &jnl_file_version,
                                          jnl_target_file_name,
                                          &jnl_magic,
                                          &jnl_human_readable,
                                          FALSE,
                                          verbosity);

            if ( ! proceed ) {

                *error_ptr = TRUE;
                HDfprintf(stderr, 
                          "\nerrors reading %s.  Journal un-useable.\n\n",
                          journal_file_name);

            } else {

                if ( magic == jnl_magic ) {

                    HDfprintf(stdout, 
                               "\nthe journal file %s has matching magic.\n\n",
                               journal_file_name);

                } else {

                    proceed = FALSE;
                    *error_ptr = TRUE;
                    HDfprintf(stderr, "\n%s %s\n\n",
                              "journal and file magic don't match --",
                              "recovery canceled.");
                }
            }
        } else {

            HDfprintf(stdout, "The journal file %s is empty.\n", 
                      journal_file_name);
            HDfprintf(stdout, "%s %s\n",
                      "Thus is is impossible to verify that",
                      "it is the matching journal file.");

            if ( force ) {

                HDfprintf(stdout, "Recovery forced.\n");

            } else {

                char reply_buf[128];
                hbool_t done = FALSE;
                int i;
                
                while ( ! done ) {

                    HDfprintf(stdout, "Proceed with the recovery? (y/n): ");

                    if ( HDfgets(reply_buf, 127, stdin) != 0 ) {

                        i = 0;
                        while ( ( i < 128 ) && ( isspace(reply_buf[i]) ) )
                        {
                            i++;
                        }

                        if ( ( reply_buf[i] == 'y' ) || 
                             ( reply_buf[i] == 'Y' ) ) {

                            done = TRUE;
                            HDfprintf(stdout, "Proceeding with recovery.\n");

                        } else if ( ( reply_buf[i] == 'n' ) ||
                                    ( reply_buf[i] == 'N' ) ) {

                            done = TRUE;
                            proceed = FALSE;
                            HDfprintf(stdout, "Recovery canceled.\n");

                        }
                    }
                }
            }
        }
    }

    return(proceed);

} /* verify_files() */


/*-------------------------------------------------------------------------
 * Function:     usage()
 *
 * Purpose:      Prints a usage message and then returns.
 *
 * Return:       void
 *
 * Programmer:   Mike McGreevy <mamcgree@hdfgroup.org>
 *               Monday, March 10, 2008
 *
 *-------------------------------------------------------------------------
 */
static void
usage (void)
{
 fprintf(stdout, "\
usage: h5recover [OPTIONS] [OBJECTS] [HDF5_FILE]\n\
   OBJECTS\n\
      -j, --journal [JOURNAL_FILE]      journal file name\n\
   OPTIONS\n\
      -b, --backup [BACKUP_NAME]        Specify a name for the backup copy\n\
                                            of the HDF5 file.\n\
                                            default = '[HDF5_FILE].backup'\n\
      -f  --force                       Recover without confirmation if the\n\
                                            journal file is empty.\n\
      -n, --nocopy                      Do not create a backup copy.\n\
      -h, --help                        Print a usage message and exit\n\
      -v, --verbose                     Generate more verbose output\n\
                                            (repeat for increased verbosity)\n\
      -V, --version                     Print version number and exit\n\
      -x, --examine                     Examine the supplied file(s), report,\n\
                                            and exit without action.\n");
}

/*-------------------------------------------------------------------------
 * Function:    leave
 *
 * Purpose:     Shutdown MPI & HDF5 and call exit()
 *
 * Return:      Does not return
 *
 * Programmer:  Quincey Koziol
 *              Saturday, 31. January 2004
 *
 * Modifications:
 *
 *-------------------------------------------------------------------------
 */
static void
leave(int ret)
{
 h5tools_close();
 
 exit(ret);
}

/*-------------------------------------------------------------------------
 * Function:     file_copy()
 *
 * Purpose:      Makes a back up copy of a file.
 *
 * Return:       0 on success
 *
 * Programmer:   Mike McGreevy <mamcgree@hdfgroup.org>
 *               Monday, March 10, 2008
 *
 *-------------------------------------------------------------------------
 */
static int 
file_copy(char * file_from, char * file_to)
{

    FILE * from;
    FILE * to;
    char ch;
    
    /* open source file */
    if((from = fopen(file_from, "rb")) == NULL) {
        error_msg(progname, "Could not open source file\n");
        leave( EXIT_FAILURE );
    }

    /* open destination file */
    if((to = fopen(file_to, "wb")) == NULL) {
        error_msg(progname, "Could not open destination file\n");
        leave( EXIT_FAILURE );
    }

    /* copy the file */
    while( !feof(from) ) {

        ch = fgetc(from);

        if( ferror(from) ) {
            error_msg(progname, "Error reading source file\n");
            leave( EXIT_FAILURE );
        } /* end if */

        if( !feof(from) ) 
            fputc(ch, to);

        if( ferror(to) ) {
            error_msg(progname, "Error writing destination file\n");
            leave( EXIT_FAILURE );
        } /* end if */

    } /* end while */

    /* close source file */
    if( fclose(from) == EOF ) {
        error_msg(progname, "Error closing source file\n");
        leave( EXIT_FAILURE );
    } /* end if */

    /* close destination file */
    if( fclose(to) == EOF ) {
        error_msg(progname, "Error closing destination file\n");
        leave( EXIT_FAILURE );
    } /* end if */

    return 0;

}

/*-------------------------------------------------------------------------
 * Function:     address_decode
 *
 * Purpose:      Decodes an address from the buffer pointed to by *pp and
 *               updates the pointer to point to the next byte after the
 *               address.
 *
 *               If the value read is all 1's, then the address is 
 *               returned with an undefined value.
 *
 * Programmer:   Mike McGreevy (utilizing code from Robb Matzke)
 *               Thursday, August 7, 2008
 *
 *-------------------------------------------------------------------------
 */
void
address_decode(size_t sizeof_addr, 
               const uint8_t **pp /* in, out */, 
               haddr_t *addr_p/*out*/)
{

    unsigned i;
    haddr_t tmp;
    uint8_t c;
    hbool_t all_zero = TRUE;

    assert(pp && *pp);
    assert(addr_p);

    *addr_p = 0;
    
    for (i=0; i<sizeof_addr; i++) {
        c = *(*pp)++;
        if (c != 0xff)
            all_zero = FALSE;

        if (i<sizeof(*addr_p)) {
            tmp = c;
            tmp <<= (i * 8);    /*use tmp to get casting right */
            *addr_p |= tmp;
        } else if (!all_zero) {
            assert(0 == **pp);  /*overflow */
        }
    }

    if (all_zero)
        *addr_p = HADDR_UNDEF;

    return;

} /* address_decode() */

/*-------------------------------------------------------------------------
 * Function:    address_encode
 *
 * Purpose:     Encodes an address into the buffer pointed to by *pp and
 *              then increments the pointer to the first byte after the
 *              address. An undefined value is stored as all 1's.
 *
 * Programmer:  Mike McGreevy (utilizing code from Robb Matzke)
 *              Thursday, August 7, 2008
 *
 *-------------------------------------------------------------------------
 */
void
address_encode(size_t sizeof_addr, 
               uint8_t **pp /* in, out */, 
               haddr_t addr)
{
    unsigned u;
    
    assert(pp && *pp);
    
    for(u = 0; u < sizeof_addr; u++) {
        *(*pp)++ = (uint8_t)(addr & 0xff);
        addr >>= 8;
    } /* end for */

    assert("overflow" && 0 == addr);

    return;

} /* address_encode() */


/*-------------------------------------------------------------------------
 * Function:     length_decode
 *
 * Purpose:      Decodes a length from the buffer pointed to by *pp and
 *               updates the pointer to point to the next byte after the
 *               length.
 *
 * Programmer:   JRM -- 3/6/09 (rework of address_decode())
 *
 *-------------------------------------------------------------------------
 */
void
length_decode(size_t sizeof_length, 
              const uint8_t **pp /* in, out */, 
              hsize_t *len_p /*out*/)
{

    unsigned i;
    hsize_t tmp;
    uint8_t c;

    HDassert( pp && *pp );
    HDassert( len_p );
    HDassert( sizeof_length <= sizeof(hsize_t) );

    *len_p = 0;
    
    for ( i = 0; i < sizeof_length; i++ ) {

        c = *(*pp)++;

        if ( i < sizeof(*len_p)) {

            tmp = c;
            tmp <<= (i * 8);    /*use tmp to get casting right */
            *len_p |= tmp;
        }
    }

    return;

} /* length_decode() */

/*-------------------------------------------------------------------------
 * Function:    length_encode
 *
 * Purpose:     Encodes a length into the buffer pointed to by *pp and
 *              then increments the pointer to the first byte after the
 *              length. 
 *
 * Programmer:  JRM -- 3/6/09 (rework of address_encode())
 *
 *-------------------------------------------------------------------------
 */

void
length_encode(size_t sizeof_length, 
              uint8_t **pp /* in, out */, 
              hsize_t length)
{
    unsigned u;
    
    HDassert(pp && *pp);
    HDassert( sizeof_length <= sizeof(hsize_t) );
    
    for ( u = 0; u < sizeof_length; u++ ) {

        *(*pp)++ = (uint8_t)(length & 0xff);
        length >>= 8;

    } /* end for */

    assert("overflow" && 0 == length);

    return;

} /* length_encode() */

/*-------------------------------------------------------------------------
 * Function:     main()
 *
 * Purpose:      main h5recover program.
 *
 * Programmer:   Mike McGreevy <mamcgree@hdfgroup.org>
 *               Monday, March 10, 2008
 *
 * Modifications: 
 *               Mike McGreevy, August 7, 2008
 *               Parses superblock to update EOA value
 *
 *-------------------------------------------------------------------------
 */

int
main(int argc, 
     const char *argv[])
{

    /* ===================== */
    /* Variable Declarations */
    /* ===================== */

    hid_t            fid = -1; /* file id number */
    hid_t            fapl = -1; /* file access property list */
    char *           file_name = NULL; /* file name */
    char *           file_name_backup = NULL; /* name of backup file */
    char *           journal_name = NULL; /* journal name */
    unsigned         verbose = 0; /* verbose boolean */
    int              opt; /* option number */
    FILE *           journal_fp; /* journal file pointer */
    int              hdf5_fd; /* hdf5 file descriptor */
    char *           readback;   /* array to read into from journal */
    char *           last_trans_msg; /* array to keep last end_trans msg */
    int              last_trans_found = 0; /* bool */
    char *           tok[11];     /* string tokens */
    int              i; /* iterator */
    off_t            address; /* address to write to */
    haddr_t          eoa = 0; /* end of address of file */
    haddr_t          update_eoa = 0; /* new end of address */
    uint8_t *        body; /* body of journal entry */
    size_t           size; /* size of journal entry body */
    size_t           max_size; /* maximum size of journal entry body */
    char *           p; /* pointer */
    hbool_t          custom_name = 0; /* bool indicating custom backup name */
    hbool_t          no_copy = 0; /* bool indicating not to make backup */
    H5AC2_jnl_config_t config; /* journaling configuration */
    hbool_t          check_file = 1; /* boolean indicating whether to check */
    uint8_t *        compare_buf; /* buffer to read into from hdf5 file */
    hid_t            status = -1; /* status indicator for retval checking */
    char             c_old,c_new; /* temporary storage for single characters */
    long             pos; /* file descriptor position indicator */
    long             pos_end; /* file descriptor position indicator */
    char             temp[100]; /* temporary buffer */
    H5F_t *          f; /* File pointer */
    hbool_t          jrnl_has_transactions = TRUE;              
    hbool_t          examine_files_only = FALSE;
    hbool_t          force = FALSE;
    hbool_t          error = FALSE;

    /* ================================================ */
    /* Variables needed for superblock parse and update */
    /* ================================================ */

    int              n; /* iterator */
    haddr_t          sb_addr; /* address of superblock */
    uint8_t          buf[H5F_SIGNATURE_LEN]; /* buf to get superblock signature */
    uint8_t          sbuf[H5F_MAX_SUPERBLOCK_SIZE]; /* buf to store superblock */
    int              super_vers = 0; /* superblock version number */
    int              sizeof_addr = 0; /* size of addresses */
    haddr_t          addr = NULL; /* address buffer to hold haddr_t values */
    uint8_t *  p_front = NULL; /* reference pointer into superblock buffer */
    uint8_t *  p_end = NULL; /* reference pointer into superblock buffer */
    haddr_t          new_eoa; /* new value of EOA to be written into superblock */
    uint32_t         chksum; /* calculated checksum value */
    uint32_t         read_chksum; /* checksum value read from superblock */

    /* ==================== */
    /* Command Line Parsing */
    /* ==================== */

    /* initialize h5tools lib */
    h5tools_init();

    /* Check for no command line parameters */
    if ( argc == 1 ) {

        usage();
        leave( EXIT_SUCCESS );

    } /* end if */

    /* parse command line options */
    while ( (opt = get_option(argc, argv, s_opts, l_opts) ) != EOF) {

        switch ((char)opt) {

            case 'h':
                usage();
                leave( EXIT_SUCCESS );
                break;
            
            case 'b':
                custom_name = 1;
                file_name_backup = strdup ( opt_arg );
                break;

            case 'f':
                force = TRUE;
                break;

            case 'n':
                no_copy = 1;
                break;

            case 'j':
                journal_name = strdup( opt_arg );
                break;

            case 'V':
                print_version(progname);
                leave( EXIT_SUCCESS );
                break;

            case 'v':
                verbose++;
                break;

            case 'x':
                examine_files_only = 1;
                break;

            default:
                usage();
                leave( EXIT_FAILURE );

        } /* end switch */

    } /* end while */

    /* check for missing file name */
    if (argc <= opt_ind) {
        error_msg(progname, "HDF5 file name missing\n");
        usage();
        leave( EXIT_FAILURE );
    }

    file_name = HDstrdup(argv[opt_ind]);

    /* check for missing journal name */
    if ( journal_name == NULL ) {

        error_msg(progname, "Journal file name missing\n");
        usage();
        leave( EXIT_FAILURE );

    } /* end if */

    /* check for missing backup file name */
    if ( custom_name == 1) {
        
        if ( file_name_backup == NULL ) {

            error_msg(progname, "Journal file backup name missing\n");
            usage();
            leave( EXIT_FAILURE );

        } /* end if */

    } /* end if */

    /* if we are only to examine the files, do so now and exit */
    if ( examine_files_only ) {

        examine_files(file_name, journal_name, verbose);

        leave( EXIT_SUCCESS );
    }

    /* ================================================================ */
    /* Verify that the supplied HDF5 file is marked as being journaled, */
    /* and that the supplied journal file matches the HDF5 file.  Exit  */
    /* if this is not the case.                                         */
    /* ================================================================ */

    if ( ! verify_files(file_name, journal_name, force, verbose, &error) ) {

        if ( error ) {

            leave( EXIT_FAILURE );

        } else {

            leave( EXIT_SUCCESS );
        }
    }


    /* =============================== */
    /* Make a Backup Copy of HDF5 File */
    /* =============================== */

    if ( verbose ) printf("\n==============================================\n");

    if (no_copy == 0) {

        /* make a backup copy of HDF5 file before recovery */
        if (custom_name == 1) {

            if ( verbose ) printf("Copying HDF5 file <%s> into file <%s>\n", file_name, file_name_backup);

            file_copy( /* from */ file_name, /* to */ file_name_backup);

        } /* end if */

        else {
            file_name_backup = HDmalloc(HDstrlen(file_name) + (size_t)10);    

            HDsnprintf(file_name_backup, HDstrlen(file_name) + (size_t)8, "%s.backup", file_name);
            if ( verbose ) printf("Copying HDF5 file <%s> into file <%s>\n", file_name, file_name_backup);
            file_copy( /* from */ file_name, /* to */ file_name_backup);
    
        } /* end else */

    } /* end if */

    /* =========================== */
    /* Open HDF5 and Journal Files */
    /* =========================== */

    /* open the journal file for reading */
    journal_fp = fopen(journal_name, "r");
    
    if (journal_fp == NULL) {

        error_msg(progname, "Could not open specified journal file\n");
        usage();
        leave( EXIT_FAILURE );

    } /* end if */
    
    /* open hdf5 file for reading and writing */
    hdf5_fd = open(file_name, O_RDWR);
    
    if (hdf5_fd == -1) {
    
        error_msg(progname, "Could not open specified hdf5 file\n");
        usage();
        leave( EXIT_FAILURE );

    } /* end if */

    /* allocate temporary space to store and transaction messages */
    last_trans_msg = HDmalloc((size_t)50);
    if (last_trans_msg == NULL) {

        error_msg(progname, "Could not allocate space\n");
        leave( EXIT_FAILURE);

    } /* end if */

    if ( verbose ) printf("Recovering file <%s> from journal <%s>\n\n", 
                          file_name, journal_name);

    /* ====================================================== */
    /* Find the last complete transaction in the journal file */
    /* ====================================================== */

    fseek(journal_fp, 0, SEEK_END);

    while (last_trans_found == 0) {

        while (fgetc(journal_fp) != '\n') {
            
            if (ftell(journal_fp) <= 1) {

                jrnl_has_transactions = FALSE;
                printf("Journal file has no complete transactions. Nothing to recover!\n");
                break;
        
            } /* end if */

            fseek(journal_fp, -2, SEEK_CUR);

        } /* end while */

        if (jrnl_has_transactions == FALSE) break;

        if ( fgetc(journal_fp) == '3' ) {

            last_trans_found = 1;
            pos_end = ftell(journal_fp);
            fseek(journal_fp, -1, SEEK_CUR);
            fgets(last_trans_msg, 50, journal_fp);

        } /* end if */

        else {

            fseek(journal_fp, -3, SEEK_CUR);

        } /* end else */

    } /* end while */

    /* ================================================================== */
    /* Only do the recovery procedure if there is something to recover in */
    /* the journal file. Otherwise, skip over these steps and mark        */
    /* the file as recovered.                                             */
    /* ================================================================== */
    
    if (jrnl_has_transactions == TRUE) {

        /* ================================================================= */
        /* Pre-parse of journal file to pull information needed before doing */
        /* the recovery.                                                     */
        /*    - max journal size (for buffer allocation)                     */
        /*    - max EOA size (for superblock update, to preserve raw data)   */
        /* ================================================================= */

        fseek(journal_fp, 0, SEEK_SET);
    
        c_new = 0;
        c_old = 0;
        max_size = 0;

        if ( verbose ) printf("Pre-parsing journal file to pull needed data ... \n");

        /* while journal is not at end of file */
        while (ftell(journal_fp) != pos_end) {

            c_old = c_new;
            c_new = fgetc(journal_fp);
        
            /* if position is at the start of a line */
            if (c_old == '\n') {

                /* ========================================================== */
                /* if the line is a journal entry, determine its size. update */
                /* max size value if needed.                                  */
                /* ========================================================== */
                if (c_new == '2') {

                    pos = ftell(journal_fp);

                    fgets(temp, 100, journal_fp); 
                    tok[0] = HDstrtok(temp, " ");
                    if (tok[0] == NULL) {

                        error_msg(progname, "Could not tokenize entry\n");
                        leave( EXIT_FAILURE);
    
                    } /* end if */
                    for (i=1; i<8; i++) {

                        tok[i] = HDstrtok(NULL, " ");
                        if (tok[i] == NULL) {

                            error_msg(progname, "Could not tokenize entry\n");
                            leave( EXIT_FAILURE);

                        } /* end if */

                    } /* end for */
                
                    size = (size_t)HDstrtoll(tok[3], NULL, 10);

                    if (max_size < size) {

                        max_size = size;

                    } /* end if */

                    /* jump back to start of line */
                    fseek(journal_fp, pos, SEEK_SET);

                } /* end if */

                /* =========================================================== */
                /* If the line is an EOA entry, determine its value and update */
                /* if it exceeds the current max length                        */
                /* =========================================================== */

                if (c_new == 'E') {
            
                    pos = ftell(journal_fp);

                    fgets(temp, 100, journal_fp);
                    p = &temp[11];

		    /* according to the man page, strtoll() should accept a 
		     * "0x" prefix on any base 16 value -- seems this is 
		     * not the case.  Deal with this by incrementing p
		     * past the prefix.
		     */

		    while ( HDisspace(*(p)) ) { p++; }

		    if ( ( *(p) == '0' ) &&
		         ( *(p + 1) == 'x' ) ) {

		            p += 2;
		    }

                    eoa = (haddr_t)HDstrtoll(p, NULL, 16);
                    if (eoa == 0) {
    
                        error_msg(progname, 
				  "Could not convert eoa to integer\n");
                        leave( EXIT_FAILURE);

                    } /* end if */

                    if (update_eoa < eoa) {

                        update_eoa = eoa;

                    } /* end if */

                    /* jump back to start of line */
                    fseek(journal_fp, pos, SEEK_SET);

                } /* end if */

            } /* end if */

        } /* end while */

        if ( verbose ) printf(" - Maximum journal entry size = %d\n", max_size);
        if ( verbose ) printf(" - Journaled EOA value = 0x%llx\n", update_eoa);

        /* =================================== */
        /* Update EOA value in HDF5 superblock */
        /* =================================== */

        if (update_eoa != 0) {

            if ( verbose ) printf("\nLooking for HDF5 superblock ... \n");
            /* Jump through possible locations of superblock */
            for(n = 8; n < 16; n++) {
    
                sb_addr = (8 == n) ? 0 : 1 << n;
    
                /* read from HDF5 file */
                pread(hdf5_fd, buf, H5F_SIGNATURE_LEN, sb_addr);    
    
                /* Check to see if superblock has been found. */
                if(!HDmemcmp(buf, H5F_SIGNATURE, (size_t)H5F_SIGNATURE_LEN)) {
    
                    if ( verbose ) printf(" - Superblock signature found at location %d\n", sb_addr);
    
                    if ( verbose ) printf(" - Reading in entire superblock\n");
    
                    /* Read in entire superblock */
                    pread(hdf5_fd, sbuf, H5F_MAX_SUPERBLOCK_SIZE, sb_addr/* + H5F_SIGNATURE_LEN */);
    
                    /* Use p as a pointer into superblock buffer */
                    p = sbuf;
    
                    /* Skip over signature */
                    p += H5F_SIGNATURE_LEN;

                    /* Get superblock version number */   
                    super_vers = *p++;
    
                    /* add printfs to verbose */
                    if ( verbose ) printf(" - Superblock version number = %d\n", super_vers);
        
                    /* ==================================== */
                    /* Point to EOA value in the superblock */ 
                    /* ==================================== */

                    /* First part of superblock may be of differing versions */
                    if(super_vers < 2) {
                        /* skip over unneeded data */
                        p += 1 +  /* freespace version */
                             1 +  /* root group version */
                             1 +  /* reserved byte */
                             1;   /* shared header version */

                        sizeof_addr = *p++; /* size of file addresses */

                        p += 1 +  /* size of file sizes */
                             1 +  /* reserved byte */
                             2 +  /* 1/2 rank for symtable leaf nodes */
                             2 +  /* 1/2 rank for btree internal nodes */
                             4 +  /* file status flags */
                             2;   /* b-tree internal k value */
                
                        if (super_vers == 1)
                            p += 2; /* reserved bytes */

                    } /* end if */

                    /* Superblock version number > 2 */
                    else {
            
                        sizeof_addr = *p++; /* size of file addresses */
                
                        p += 1 + /* size of file sizes */
                             1;  /* file status flags */
 
                        p_front = p;
                        p_end = p;
                   
                    } /* end else */
                   
                    /* Skip over various variable portions of superblock */
                    address_decode((size_t)sizeof_addr, &p_end, &addr); /* base address */
                    address_decode((size_t)sizeof_addr, &p_end, &addr); /* extension address */

                    /* ============================ */
                    /* Update EOA in the superblock */
                    /* ============================ */

                    p_front = p_end;

                    /* Decode the EOA address */
                    address_decode((size_t)sizeof_addr, &p_end, &addr);

                    if ( verbose ) printf(" - Current value of EOA in superblock is 0x%llx\n", addr);
                
                    /* Set the EOA to the address pulled from the journal */
                    addr = (haddr_t)update_eoa;
                    p_end = p_front;

                    /* encode new EOA value into superblock buffer */
                    address_encode((size_t)sizeof_addr, &p_end, addr);

                    if ( verbose ) printf(" - EOA value has been updated to 0x%llx in superblock\n", update_eoa);

                    /* skip over root group object header */
                    address_decode((size_t)sizeof_addr, &p_end, &addr); /* root group object header */

                    p_front = p_end;

                    if ( verbose ) printf(" - Updating checksum value of superblock\n");

                    /* decode checksum */
                    read_chksum = 0;
                    UINT32DECODE(p_end, read_chksum);

                    p_end = p_front;

                    /* Update the CHECKSUM VALUE */
                    /* Compute superblock checksum */
                    chksum = H5_checksum_metadata(sbuf, (size_t)(p_end - sbuf), 0);

                    /* Superblock checksum */
                    UINT32ENCODE(p_end, chksum);

                    new_eoa = (haddr_t)update_eoa;

                    /* verify new EOA value in buffer is correct */
                    address_decode((size_t)sizeof_addr, &p_front, &addr);
                
                    /* Extend file to be EOA bytes */
                    HDftruncate(hdf5_fd, new_eoa);
            
                    /* Write out new updated superblock to the file */
                    status = pwrite(hdf5_fd, sbuf, (size_t)H5F_MAX_SUPERBLOCK_SIZE, sb_addr /*+ H5F_SIGNATURE_LEN */);

                    if (status == -1) {
                        error_msg(progname, "pwrite failed when trying to update superblock\n");
                        leave( EXIT_FAILURE );
                    } 
            
                    if (status == 0) {
                        error_msg(progname, "pwrite did not write anything to superblock!\n");
                        leave( EXIT_FAILURE);
                    }

                    if ( verbose ) printf(" - New superblock written to HDF5 file\n");
                
                } /* end if */
    
            } /* end for */

        } /* end if (update_eoa != 0)*/

        if ( verbose ) printf("\nBeginning recovery process ... \n\n");

        /* ==================================================================== */
        /* Main Recovery Procedure:                                             */
        /* Read through the journal file and recover any journal entries found. */
        /* ==================================================================== */

        max_size = max_size * 3 + 200;

        /* allocate space large enough to hold largest journal entry */
        readback = HDmalloc( max_size );
        if (readback == NULL) {

            error_msg(progname, "Could not allocate space to hold entries\n");
            leave( EXIT_FAILURE);

        } /* end if */

        /* read through journal file. recover any journal entries found, up
         * through the last transaction number */
        fseek(journal_fp, 0, SEEK_SET);

        while ( fgets(readback, max_size, journal_fp) != NULL ) {
    
            if (HDstrcmp(readback, last_trans_msg) == 0) {
    
                /* done reading from file */
                break;

            } /* end if */
    
            /* ===================================================== */
            /* If journal entry is found, write entry into HDF5 file */
            /* ===================================================== */

            if ( readback[0] == '2') { /* journal entry found */

                if ( verbose ) printf("Journal entry found.\n");
                if ( verbose ) printf("Tokenizing journal entry.\n");

                /* ================================================= */
                /* Tokenize the journal entry in order to grab data */
                /* ================================================= */

                /* divide the journal entry into tokens */
                tok[0] = HDstrtok(readback, " ");
                if (tok[0] == NULL) {

                    error_msg(progname, "Could not tokenize journal entry\n");
                    leave( EXIT_FAILURE);
    
                } /* end if */

                if ( verbose ) printf("  token[0] : <%s>\n", tok[0]);

                for (i=1; i<8; i++) {

                    tok[i] = HDstrtok(NULL, " ");
                    if (tok[i] == NULL) {

                        error_msg(progname, "Could not tokenize journal entry\n");
                        leave( EXIT_FAILURE);

                    } /* end if */

                    if ( verbose ) printf("  token[%d] : <%s>\n", i, tok[i]);

                } /* end for */

                /* put all remaining data into last token. */ 
                /* This contains all of the journal entry body */
                tok[8] = HDstrtok(NULL, "\n");
                if (tok[8] == NULL) {

                    error_msg(progname, "Could not tokenize journal entry\n");
                    leave( EXIT_FAILURE);

                } /* end if */

                if ( verbose ) printf("  token[8] : <hexadecimal body data>\n");

                /* =================================== */
                /* Convert Items from Character Arrays */
                /* =================================== */

                if ( verbose ) printf("Converting data from character strings.\n");

                /* convert address from character character string */

		/* according to the man page, strtoll() should accept a 
		 * "0x" prefix on any base 16 value -- seems this is 
		 * not the case.  Deal with this by incrementing tok[6]
		 * past the prefix.
		 */
		while ( HDisspace(*(tok[6])) ) { (tok[6])++; }
		if ( ( *(tok[6]) == '0' ) &&
		     ( *(tok[6] + 1) == 'x' ) ) {

		        (tok[6]) += 2;
		}
                address = (off_t)HDstrtoll(tok[6], NULL, 16);
                if (address == 0) {
    
                    error_msg(progname, "Could not convert address to integer\n");
                    leave( EXIT_FAILURE);

                } /* end if */

                if ( verbose ) printf("  address  : %llx\n", address);

                /* convert size from character string*/
                size = (size_t)HDstrtoll(tok[4], NULL, 10);
                if (size == 0) {

                    error_msg(progname, "Could not convert size to double\n");
                    leave( EXIT_FAILURE);

                } /* end if */

                if ( verbose ) printf("  length   : %d\n", size);

                /* transform body out of hexadecimal character string */
                body = HDmalloc(size + 1);
                if (body == NULL) {

                    error_msg(progname, "Could not allocate space for body\n");
                    leave( EXIT_FAILURE);

                } /* end if */
            
                p = &(tok[8])[0];
            
                for (i = 0; i < size; i++) {
            
                    body[i] = HDstrtoul(p, NULL, 16);
                    p = &p[3];

                } /* end for */

                body[i] = 0;

                if ( verbose ) printf("  body     : binary body data\n");

                /* ================================================ */
                /* Write into HDF5 file the recovered journal entry */
                /* ================================================ */

                if ( verbose ) printf("Writing entry to HDF5 file.\n");
 
                /* perform a write */
                status = pwrite(hdf5_fd, body, size, address);

                if (status == -1) {
                    error_msg(progname, "pwrite failed\n");
                    leave( EXIT_FAILURE );
                }
            
                if (status == 0) {
                    error_msg(progname, "pwrite did not write anything!\n");
                    leave( EXIT_FAILURE);
                }

                /* Verify that write occurred correctly */
                if ( check_file == 1) {

                    if ( verbose ) printf("Verifying success of write");
            
                    compare_buf = HDmalloc(size + 1);
    
                    if (compare_buf == NULL) {
                        error_msg(progname, "Could not allocate space\n");
                        leave( EXIT_FAILURE);
                    } /* end if */
                
                    pread(hdf5_fd, compare_buf, size, address);

                    /* do a quick string compare on two items */
                    if (HDstrcmp((const char *)body, (const char *)compare_buf) != 0) {
                        error_msg(progname, "Entry incorrectly written into HDF5 file. Exiting.\n");
                        printf("Address %llx:\n", (unsigned long_long)address);
                        printf(" -- from journal:   '%s'\n", body);
                        printf(" -- from HDF5 file: '%s'\n", compare_buf);
                        leave( EXIT_FAILURE );
                    } /* end if */

                    /* compare each individual value of entry */
                    for (i=0; i<size; i++) {
                        if (body[i] != compare_buf[i]) {
                            error_msg(progname, "Entry incorrectly written into HDF5 file. Exiting.\n");
                            printf("Address %llx\n", (unsigned long_long)(address + i));
                            printf(" -- from journal:   %d\n", body[i]);
                            printf(" -- from HDF5 file: %d\n", compare_buf[i]);
                            leave( EXIT_FAILURE );
                        }
                    }

                    if ( verbose ) printf(" .... SUCCESS!\n\n");
                    free(compare_buf);

                }

                free(body);

            } /* end if */

        } /* end while */

        free(readback);

    } /* end if jrnl_has_transactions */

    fclose(journal_fp);
    close(hdf5_fd);
    free(last_trans_msg);
    free(journal_name);
    if (file_name_backup != NULL) HDfree(file_name_backup);

    /* =========================== */
    /* Mark HDF5 File as Recovered */
    /* =========================== */

    /* set up appropriate fapl */
    fapl = H5Pcreate(H5P_FILE_ACCESS);
    
    if ( fapl == -1 ) {
    
        error_msg(progname, "Could not create FAPL.\n");
        leave( EXIT_FAILURE );
    
    } /* end if */
    
    config.version = 1; /* should be H5C2__CURR_AUTO_SIZE_CTL_VER */
    
    /* get H5AC_cache_config_t configuration from fapl */
    if ( H5Pget_jnl_config(fapl, &config) == -1) {
    
        error_msg(progname, "Could not get mdc config from FAPL.\n");
        leave( EXIT_FAILURE );
    
    }
            
    /* make sure journal recovered field is set to TRUE in mdc_config */
    config.journal_recovered = TRUE;
    
    /* set H5AC_cache_config_t configuration with file recovered */
    if ( H5Pset_jnl_config(fapl, &config) == -1) {
    
        error_msg(progname, "Could not set mdc config on FAPL.\n");
        leave( EXIT_FAILURE );
    
    } /* end if */

    /* open HDF5 file with provided fapl */
    fid = H5Fopen(file_name, H5F_ACC_RDWR, fapl);    
       
    if ( fid == -1 ) {
    
        error_msg(progname, "Could not open recovered HDF5 file.\n");
        leave( EXIT_FAILURE );
    
    } /* end if */

    /* close HDF5 file */
    if ( H5Fclose(fid) == -1 ) {
    
        error_msg(progname, "Could not close recovered HDF5 file.\n");
        leave( EXIT_FAILURE );
    
    } /* end if */

    /* ================ */
    /* Cleanup and Exit */
    /* ================ */

    if (jrnl_has_transactions == TRUE) 
        printf("HDF5 file successfuly recovered.\n");
    else
        printf("File marked as recovered.\n");

    if ( verbose ) printf("==============================================\n\n");
    free(file_name);

    return 0;
    
} /* end main */