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
//! `RecordReader` — reads a Riegeli file from any `Read + Seek` source.
//!
//! ## Reading algorithm
//!
//! The reader maintains a "next chunk file position" cursor. On each `read_record()` call:
//! 1. If the current chunk decoder has records remaining, yield the next one.
//! 2. Otherwise, advance to the next chunk: skip any block header at block boundaries,
//! read a 40-byte `ChunkHeader`, read `data_size` bytes, validate, decode.
//! 3. If a hash validation fails:
//! - Without recovery: return `Err`.
//! - With recovery: compute the invalid region, call the callback with
//! it, and — if the callback returns `true` — resume reading at the
//! region's end (exactly one bad chunk when its header is trustworthy,
//! the next block boundary otherwise).
use std::cmp::Ordering;
use std::io::{Read, Seek, SeekFrom};
use crate::block_arithmetic::{is_block_boundary, round_down_to_block_boundary};
use crate::block_header::BlockHeader;
use crate::chunk_header::{ChunkHeader, ChunkType};
use crate::constants::{BLOCK_HEADER_SIZE, BLOCK_SIZE, CHUNK_HEADER_SIZE};
use crate::error::RiegeliError;
use crate::field_projection::FieldProjection;
use crate::record_position::RecordPosition;
use crate::simple_chunk::{Chunk, SimpleChunkDecoder};
use crate::transpose::decoder::TransposeChunkDecoder;
/// Type alias for the optional recovery callback.
///
/// C++ equivalent: `std::function<bool(const SkippedRegion&, RecordReaderBase&)>`.
/// The Rust callback does not receive the reader (it is owned by the reader,
/// so a mutable reference would alias); after a cancelled operation returns,
/// the caller repositions the reader itself, and
/// [`RecordReader::last_skipped_region`] exposes the region that was
/// reported.
type RecoveryCallback = Option<Box<dyn FnMut(&crate::SkippedRegion) -> bool>>;
/// Options for configuring a [`RecordReader`].
pub struct ReaderOptions {
recovery: RecoveryCallback,
/// Optional field projection for column pruning in transpose chunks.
field_projection: Option<FieldProjection>,
}
impl ReaderOptions {
/// Create `ReaderOptions` with default settings (no recovery, no projection).
pub fn new() -> Self {
Self {
recovery: None,
field_projection: None,
}
}
/// Set a recovery callback invoked when a corrupted region is
/// encountered.
///
/// The callback receives the [`SkippedRegion`](crate::SkippedRegion)
/// about to be skipped. Returning `true` skips the region and continues
/// the operation; returning `false` cancels it, and the operation
/// returns the original error.
///
/// The region's `end()` is exactly where the reader resumes: one bad
/// chunk when its header is trustworthy (hash-valid with
/// stream-bounded claims), the next block boundary otherwise.
///
/// Divergences from the C++ `set_recovery`
/// (`std::function<bool(const SkippedRegion&, RecordReaderBase&)>`):
///
/// - No reader parameter: the reader owns the callback, so passing it a
/// mutable reference would alias. Use
/// [`RecordReader::last_skipped_region`] after a cancelled call.
/// - Cancelling follows the C++ shape: the region is already skipped
/// when the callback runs, so returning `false` reports the error
/// once and the NEXT operation continues past the rejected region —
/// the callback is never re-invoked for a region it rejected, and a
/// retry loop makes progress. A caller that wants to stop AT the
/// damage can take [`RecordReader::last_skipped_region`] and
/// `seek` to its `begin()`.
pub fn recovery<F: FnMut(&crate::SkippedRegion) -> bool + 'static>(mut self, f: F) -> Self {
self.recovery = Some(Box::new(f));
self
}
/// Set a `FieldProjection` to enable column pruning for transpose chunks.
///
/// When set to a non-`all()` projection, the `TransposeChunkDecoder` will
/// skip data buffers for fields not in the projection and filter decoded
/// records to contain only the projected fields.
///
/// Non-proto records and simple (non-transpose) chunks are returned unchanged.
pub fn field_projection(mut self, proj: FieldProjection) -> Self {
self.field_projection = Some(proj);
self
}
}
impl Default for ReaderOptions {
fn default() -> Self {
Self::new()
}
}
/// Active chunk decoder — either simple or transposed.
enum ActiveDecoder {
Simple(SimpleChunkDecoder),
Transposed(TransposeChunkDecoder),
}
impl ActiveDecoder {
// NOTE: the recovery design relies on decoders being structurally
// infallible after construction — both variants slice pre-validated
// ranges out of pre-decoded buffers, so the Result below has no
// reachable Err today. That is what lets every recoverable failure be
// position-stable at a chunk boundary (the C++ kRecoverChunkDecoder
// sub-chunk case collapses to the chunk case here). If a streaming or
// lazy decoder is ever introduced, mid-chunk failures become possible
// and need their own position-stability and SkippedRegion story.
fn read_record(&mut self) -> Result<Option<Vec<u8>>, RiegeliError> {
match self {
ActiveDecoder::Simple(d) => d.read_record(),
ActiveDecoder::Transposed(d) => d.read_record(),
}
}
}
/// A reader that parses a Riegeli file record by record.
pub struct RecordReader<R: Read + Seek> {
/// The underlying I/O source.
reader: R,
/// Optional recovery callback for corrupted regions.
recovery: RecoveryCallback,
/// Optional field projection for column pruning in transpose chunks.
field_projection: Option<FieldProjection>,
/// File position of the chunk currently being decoded (its `ChunkHeader` starts here).
current_chunk_begin: u64,
/// File position where the NEXT chunk header will be read from.
next_chunk_file_pos: u64,
/// The decoder for the current chunk, if one has been loaded.
current_decoder: Option<ActiveDecoder>,
/// How many records have been yielded from the current chunk.
current_record_index: u64,
/// Logical read-cursor position: points at the next record to be returned.
pos: RecordPosition,
/// Position of the last successfully read record.
last_pos: RecordPosition,
/// True once we've hit EOF (no more chunks).
at_eof: bool,
/// True if the last record was read from a valid (non-recovered) chunk.
last_record_is_valid: bool,
/// Stream length as last measured (re-measured on demand if a chunk's
/// claims exceed it, so a file growing between reads keeps working).
/// Bounds header-claimed sizes before they drive arithmetic or
/// allocation — the header hash proves integrity, not honesty.
stream_len: u64,
/// The most recent region reported to the recovery callback (whether it
/// continued or cancelled). `None` until the callback first fires.
last_skipped_region: Option<crate::SkippedRegion>,
/// Failure-time classification for recovery: `Some(chunk_end)` when the
/// failing chunk's header was hash-valid with stream-bounded claims at
/// the MOMENT of failure (its extent is trustworthy), `None` otherwise.
/// Set by the error sites, consumed by try_recover_at — never derived
/// by re-reading, which is a time-of-check/time-of-failure hazard (a
/// stream that grows between failure and recovery could reclassify an
/// untrusted failure as trusted and skip a readable chunk).
pending_trusted_end: Option<u64>,
}
impl<R: Read + Seek> RecordReader<R> {
/// Open a Riegeli file.
///
/// Validates the initial block header and signature chunk, then positions
/// the reader at the first data chunk.
pub fn new(mut reader: R, options: ReaderOptions) -> Result<Self, RiegeliError> {
let stream_len = reader.seek(SeekFrom::End(0))?;
reader.seek(SeekFrom::Start(0))?;
// Read and validate the first block header at offset 0.
let mut bh_bytes = [0u8; 24]; // BLOCK_HEADER_SIZE
reader.read_exact(&mut bh_bytes)?;
let block_hdr = BlockHeader::from_bytes(bh_bytes);
if !block_hdr.is_valid() {
return Err(RiegeliError::MalformedData(
"invalid block header hash at offset 0".into(),
));
}
// Validate the signature chunk at offset 24 by exact comparison: the
// riegeli file signature is a fixed 40-byte constant (empty data,
// zero records). Comparing bytes — rather than checking the hash and
// type and then trusting the header's claimed sizes — does no
// arithmetic on attacker-controlled values at all: a hash-valid
// signature header claiming a huge data_size used to overflow the
// position sum in debug and seek backward through the i64 cast in
// release. This matches the C++ reader, which verifies the
// signature bytes.
let mut ch_bytes = [0u8; 40]; // CHUNK_HEADER_SIZE
reader.read_exact(&mut ch_bytes)?;
let canonical = ChunkHeader::from_parts(&[], ChunkType::FileSignature, 0, 0).to_bytes();
if ch_bytes != canonical {
return Err(RiegeliError::MalformedData(
"invalid file signature chunk at offset 24".into(),
));
}
// File position after the signature chunk: 24 (BH) + 40 (CH) + 0 = 64.
let next_chunk_file_pos = BLOCK_HEADER_SIZE + CHUNK_HEADER_SIZE;
// Initial position matches the C++ reference: numeric 0 (the
// beginning of the file), not the first chunk's canonical address
// 24 — verified by the differential harness (an earlier criterion
// documented 24; the reference disagrees and wins).
let initial_pos = RecordPosition::new(0, 0);
Ok(Self {
reader,
recovery: options.recovery,
field_projection: options.field_projection,
current_chunk_begin: BLOCK_HEADER_SIZE,
next_chunk_file_pos,
current_decoder: None,
current_record_index: 0,
pos: initial_pos,
last_pos: initial_pos,
at_eof: false,
last_record_is_valid: true,
stream_len,
last_skipped_region: None,
pending_trusted_end: None,
})
}
/// Read the next record from the file.
///
/// Returns `Ok(Some(bytes))` for a record, `Ok(None)` at EOF, or `Err` on
/// unrecoverable corruption (when no recovery callback is set).
pub fn read_record(&mut self) -> Result<Option<Vec<u8>>, RiegeliError> {
self.pending_trusted_end = None;
loop {
if self.at_eof {
return Ok(None);
}
// If we have an active decoder, try to get a record from it.
if let Some(decoder) = &mut self.current_decoder {
match decoder.read_record()? {
Some(rec) => {
// Record successfully read.
let rec_pos = RecordPosition::new(
self.current_chunk_begin,
self.current_record_index,
);
self.last_pos = rec_pos;
self.current_record_index += 1;
self.pos = RecordPosition::new(
self.current_chunk_begin,
self.current_record_index,
);
self.last_record_is_valid = true;
return Ok(Some(rec));
}
None => {
// Current chunk exhausted; fall through to load next chunk.
self.current_decoder = None;
}
}
}
// Try to load the next chunk.
match self.load_next_chunk() {
Ok(true) => {
// Chunk loaded; loop back to read from it.
}
Ok(false) => {
// EOF reached.
self.at_eof = true;
return Ok(None);
}
Err(e) => {
// Corruption detected. With recovery: report the region
// and — on `true` — resume at its end (C++: ReadRecord is
// retried). On `false` or without recovery, return the
// original error (the reader stays position-stable at
// the bad region; a bare retry re-reports it).
let at = self.next_chunk_file_pos;
if self.try_recover_at(at, &e) {
if self.at_eof {
// Resync seek failed (region end past a
// shrunken stream) — clean end of file.
return Ok(None);
}
// Continue reading from the region end.
} else {
return Err(e);
}
}
}
}
}
/// Returns the current logical read position.
///
/// Before the first `read_record()` call, returns `{ chunk_begin: 24, record_index: 0 }`.
/// After reading records, points at the next record to be returned.
pub fn pos(&self) -> RecordPosition {
self.pos
}
/// Returns the position of the last successfully read record.
///
/// Before any records have been read, returns `{ chunk_begin: 24, record_index: 0 }`.
pub fn last_pos(&self) -> RecordPosition {
self.last_pos
}
/// Seek to a specific record position.
///
/// Loads the chunk at `pos.chunk_begin` and skips `pos.record_index` records.
pub fn seek(&mut self, pos: RecordPosition) -> Result<(), RiegeliError> {
self.pending_trusted_end = None;
// Seek to the chunk_begin, accounting for block headers.
let chunk_file_pos = pos.chunk_begin;
self.reader.seek(SeekFrom::Start(chunk_file_pos))?;
// Load that chunk.
self.current_decoder = None;
self.at_eof = false;
self.next_chunk_file_pos = chunk_file_pos;
self.current_chunk_begin = chunk_file_pos;
self.current_record_index = 0;
// Load the chunk at this position.
match self.load_chunk_at(chunk_file_pos) {
Ok(Some(decoder)) => {
self.current_decoder = Some(decoder);
}
Ok(None) => {
self.at_eof = true;
self.pos = pos;
// last_pos deliberately NOT updated: it tracks the last
// successfully READ record (seek_back's target), and a seek
// reads nothing.
return Ok(());
}
Err(e) => {
// C++: Seek returns the result of the recovery function.
// On `true` the reader is positioned at the region end.
let at = self.next_chunk_file_pos;
if self.try_recover_at(at, &e) {
self.pos = RecordPosition::new(self.next_chunk_file_pos, 0);
return Ok(());
}
return Err(e);
}
}
// Skip record_index records.
for _ in 0..pos.record_index {
if let Some(ref mut dec) = self.current_decoder {
match dec.read_record()? {
Some(_) => {
self.current_record_index += 1;
}
None => {
return Err(RiegeliError::MalformedData(
format!(
"seek: record_index {} is out of range for chunk at {}",
pos.record_index, chunk_file_pos
)
.into(),
));
}
}
}
}
self.pos = pos;
// last_pos deliberately NOT updated — see the EOF arm above.
Ok(())
}
/// Seek to the record at or after file position `numeric`.
///
/// Interprets `numeric` as `chunk_begin + record_index` (from `RecordPosition::numeric()`).
/// Scans forward through the file to find the chunk where `chunk_begin <= numeric`
/// and returns positioned at `record_index = numeric - chunk_begin` within that chunk.
pub fn seek_numeric(&mut self, numeric: u64) -> Result<(), RiegeliError> {
self.pending_trusted_end = None;
// Scan from the first data chunk (offset 64) to find the right chunk.
// We need to find a chunk where chunk_begin <= numeric < chunk_begin + num_records.
// If no such chunk exists, seek to the first chunk at/after numeric.
let first_data_chunk = BLOCK_HEADER_SIZE + CHUNK_HEADER_SIZE; // 64
// Start scan from the beginning of data chunks.
let mut scan_pos = first_data_chunk;
loop {
// peek_chunk_header canonicalizes and skips block headers itself,
// so scan_pos stays a canonical chunk address. (Pre-skipping here
// would turn a boundary-coincident chunk's address into the
// boundary+24 alias and shift its numeric positions by 24.)
match self.peek_chunk_header(scan_pos) {
Ok(None) => {
// EOF — seek to end.
self.at_eof = true;
self.pos = RecordPosition::new(scan_pos, 0);
// last_pos keeps the last successfully read record.
self.current_decoder = None;
return Ok(());
}
Err(e) if self.recovery.is_some() => {
// Skip the invalid region and keep scanning, or cancel
// with the original error.
if self.try_recover_at(scan_pos, &e) {
scan_pos = self.next_chunk_file_pos;
continue;
}
return Err(e);
}
Ok(Some(ch)) => {
let chunk_begin = scan_pos;
let num_records = ch.num_records();
let data_size = ch.data_size();
if matches!(
ch.chunk_type(),
Ok(ChunkType::Simple) | Ok(ChunkType::Transposed)
) {
if chunk_begin <= numeric && numeric < chunk_begin + num_records {
let record_index = numeric - chunk_begin;
return self.seek(RecordPosition::new(chunk_begin, record_index));
} else if chunk_begin > numeric {
return self.seek(RecordPosition::new(chunk_begin, 0));
}
}
// Advance to the next chunk.
let chunk_header_file_pos = scan_pos;
scan_pos = crate::block_arithmetic::chunk_end(
chunk_header_file_pos,
data_size,
num_records,
);
}
Err(e) => return Err(e),
}
}
}
/// Returns `true` since file-based I/O supports seeking.
pub fn supports_random_access(&self) -> bool {
true
}
/// Read the file metadata chunk as a typed `RecordsMetadata` proto, if present.
///
/// Peeks at the chunk immediately after the file signature (offset 64) to check
/// if it is a `ChunkType::FileMetadata` chunk. If so, parses and returns the
/// `RecordsMetadata` message. Does not change the current read position.
pub fn read_metadata(&mut self) -> Result<Option<crate::RecordsMetadata>, RiegeliError> {
use protobuf::Parse;
match self.read_serialized_metadata()? {
Some(bytes) => {
let msg = crate::RecordsMetadata::parse(&bytes).map_err(|e| {
RiegeliError::MalformedData(
format!("failed to parse RecordsMetadata: {e}").into(),
)
})?;
Ok(Some(msg))
}
None => Ok(None),
}
}
/// Read the file metadata chunk as raw bytes, if present.
///
/// Like [`read_metadata`](Self::read_metadata), but returns the raw serialized
/// proto bytes without parsing. Does not change the current read position —
/// including under recovery: corruption at the metadata position is
/// reported to the callback but the region is never consumed, so a
/// repeated call reports the same region again (the one deliberate
/// exception to the consumed-once cancel contract, because consuming
/// would move a position this method promises not to touch).
pub fn read_serialized_metadata(&mut self) -> Result<Option<Vec<u8>>, RiegeliError> {
self.pending_trusted_end = None;
// The metadata chunk, if present, is at offset 64 (right after signature).
let metadata_chunk_pos = BLOCK_HEADER_SIZE + CHUNK_HEADER_SIZE; // = 64
// Peek at the chunk header at offset 64. A clean EOF (file ends
// before any chunk) means no metadata; a real error — hash-invalid
// header, impossible claims, I/O failure — is corruption and must
// not be reported as "no metadata": a caller inspecting metadata
// first would proceed as if the file were clean.
let ch = match self.peek_chunk_header(metadata_chunk_pos) {
Ok(Some(ch)) => ch,
Ok(None) => return Ok(None),
Err(e) => {
// C++: ReadMetadata returns the result of the recovery
// function — on `true` the file simply has no (readable)
// metadata. REPORT-ONLY: this method's contract is that it
// does not change the read position (C++'s ReadMetadata is
// a sequential read with no such promise, so there is no
// reference position behavior to match — ours must honor
// our own documentation). Repositioning here rewound a
// mid-stream reader to the skipped region's end, replaying
// or dropping records.
if self.report_region_at(metadata_chunk_pos, &e) == Some(true) {
return Ok(None);
}
return Err(e);
}
};
if !matches!(ch.chunk_type(), Ok(ChunkType::FileMetadata)) {
return Ok(None);
}
// Read the chunk data. The metadata chunk header is at offset 64, far
// from any block boundary, so its data always begins at 64 + 40.
let data = self.read_chunk_data(metadata_chunk_pos + CHUNK_HEADER_SIZE, ch.data_size())?;
// Validate data hash.
if !ch.is_data_valid(&data) {
return Err(RiegeliError::MalformedData(
"metadata chunk data hash mismatch".into(),
));
}
Ok(Some(data))
}
/// Change the active field projection, taking effect at the next chunk boundary.
///
/// The current chunk decoder (if any) continues with the old projection until
/// it is exhausted. New chunks loaded after this call will use the new projection.
///
/// To switch back to returning all fields, pass `FieldProjection::all()`.
pub fn set_field_projection(&mut self, proj: FieldProjection) {
self.field_projection = if proj.is_all() { None } else { Some(proj) };
}
/// Binary search for a record in a sorted file.
///
/// `test` is called with the bytes of individual records; it should return
/// `Ordering::Less` if the target is after this record, `Ordering::Greater`
/// if before, and `Ordering::Equal` if this is the target record.
///
/// After a successful search, the reader is positioned so that the next
/// `read_record()` returns the found record.
///
/// Returns `Ok(true)` if a record for which `test` returns `Equal` was found,
/// `Ok(false)` if the target does not exist in the file.
///
/// The search reads at most O(log N) records where N is the total number of records.
pub fn search<F>(&mut self, mut test: F) -> Result<bool, RiegeliError>
where
F: FnMut(&[u8]) -> Ordering,
{
// Collect all data chunk positions and their record counts.
let chunks = self.collect_data_chunks()?;
if chunks.is_empty() {
self.at_eof = true;
return Ok(false);
}
// Binary search over chunks using the first record of each chunk as a pivot.
// Invariant: if the target exists, it is in chunks[lo..hi].
let mut lo = 0usize;
let mut hi = chunks.len();
while lo < hi {
let mid = lo + (hi - lo) / 2;
let (chunk_pos, _num_records) = chunks[mid];
// Read just the first record of this chunk to probe.
let first_record = self.read_record_at(chunk_pos, 0)?;
match test(&first_record) {
Ordering::Less => {
// Target is after this chunk's first record → search right half.
lo = mid + 1;
}
Ordering::Greater => {
// Target is before this chunk's first record → search left half.
hi = mid;
}
Ordering::Equal => {
// First record of this chunk matches. Seek to it and return.
let target = crate::record_position::RecordPosition::new(chunk_pos, 0);
self.seek(target)?;
return Ok(true);
}
}
}
// lo == hi: the target might be inside chunks[lo-1].
// That chunk's first record is < target (test returned Less), but a later
// record in that chunk might equal the target.
if lo > 0 {
let (chunk_pos, num_records) = chunks[lo - 1];
let found = self.binary_search_within_chunk(chunk_pos, num_records, &mut test)?;
if found {
return Ok(true);
}
}
// Target not found in the file.
self.at_eof = true;
self.current_decoder = None;
Ok(false)
}
/// Collect (file_pos, num_records) for all Simple and Transposed data chunks.
///
/// Scans the entire file, reading only chunk headers (no data decompression).
fn collect_data_chunks(&mut self) -> Result<Vec<(u64, u64)>, RiegeliError> {
self.pending_trusted_end = None;
let first_data_chunk = BLOCK_HEADER_SIZE + CHUNK_HEADER_SIZE; // = 64
let mut scan_pos = first_data_chunk;
let mut chunks = Vec::new();
// Read chunk headers until EOF (skipping leading and interleaved
// block headers). With recovery set, invalid regions are skipped and
// the scan continues (C++: Search skips invalid regions while the
// recovery function returns true).
loop {
let (ch, chunk_begin, _) = match self.read_chunk_header_at(scan_pos) {
Ok(Some(v)) => v,
Ok(None) => break,
Err(e) => {
if self.try_recover_at(scan_pos, &e) {
scan_pos = self.next_chunk_file_pos;
continue;
}
return Err(e);
}
};
if !ch.is_header_valid() {
let e = RiegeliError::MalformedData(
format!("invalid chunk header at {chunk_begin} during search scan").into(),
);
if self.try_recover_at(scan_pos, &e) {
scan_pos = self.next_chunk_file_pos;
continue;
}
return Err(e);
}
let data_size = ch.data_size();
let num_records = ch.num_records();
// Zero-record chunks cannot serve as search pivots: probing one
// via read_record_at falls through to the NEXT chunk's first
// record, so the comparator sees a misattributed record and the
// search can report a present target as absent.
if num_records > 0
&& matches!(
ch.chunk_type(),
Ok(ChunkType::Simple) | Ok(ChunkType::Transposed)
)
{
chunks.push((chunk_begin, num_records));
}
scan_pos = crate::block_arithmetic::chunk_end(chunk_begin, data_size, num_records);
}
Ok(chunks)
}
/// Read the record at `record_index` within the chunk at `chunk_pos`.
///
/// Uses `seek()` to position at the exact record. Does NOT preserve reader state.
fn read_record_at(
&mut self,
chunk_pos: u64,
record_index: u64,
) -> Result<Vec<u8>, RiegeliError> {
let target = crate::record_position::RecordPosition::new(chunk_pos, record_index);
self.seek(target)?;
match self.read_record()? {
// Guard against misattribution: if the read fell through to a
// different chunk (e.g. the requested chunk had no records),
// report absence rather than another chunk's record.
Some(rec) if self.current_chunk_begin == chunk_pos => Ok(rec),
_ => Ok(Vec::new()),
}
}
/// Binary search within a single chunk for a matching record.
///
/// Uses O(log num_records) reads by seeking to specific record indices.
/// On success, positions the reader at the matching record.
fn binary_search_within_chunk<F>(
&mut self,
chunk_pos: u64,
num_records: u64,
test: &mut F,
) -> Result<bool, RiegeliError>
where
F: FnMut(&[u8]) -> Ordering,
{
if num_records == 0 {
return Ok(false);
}
// Binary search over record indices [0, num_records).
// Invariant: if the target is in this chunk, it is at index [lo, hi).
// We already know record 0 gives test == Less (from the outer binary search).
let mut lo = 1u64; // record 0 was already checked and returned Less
let mut hi = num_records;
while lo < hi {
let mid = lo + (hi - lo) / 2;
let rec = self.read_record_at(chunk_pos, mid)?;
match test(&rec) {
Ordering::Less => {
lo = mid + 1;
}
Ordering::Greater => {
hi = mid;
}
Ordering::Equal => {
// Found the target. Position the reader at this record.
let target = crate::record_position::RecordPosition::new(chunk_pos, mid);
self.seek(target)?;
return Ok(true);
}
}
}
// Target not in this chunk.
Ok(false)
}
/// Returns `true` if the most recently returned record came from a valid
/// (non-recovered) chunk.
///
/// Returns `true` initially (before any record is read) and after each
/// successful record read. Returns `false` after a recovery callback fires
/// due to a corrupted chunk.
pub fn last_record_is_valid(&self) -> bool {
self.last_record_is_valid
}
/// Seek to the previous record.
///
/// After this call, the next `read_record()` returns the same record that
/// was most recently returned by `read_record()`.
///
/// Returns `Ok(true)` if there is a previous record to seek to.
/// Returns `Ok(false)` if positioned at or before the first record.
pub fn seek_back(&mut self) -> Result<bool, RiegeliError> {
// The initial position: chunk_begin=24 (BLOCK_HEADER_SIZE), record_index=0.
// If last_pos is the initial position, there is no previous record.
// The initial position (no record read yet) is numeric 0.
if self.last_pos.chunk_begin == 0 && self.last_pos.record_index == 0 {
return Ok(false);
}
// Seek to the last successfully read record position.
let target = self.last_pos;
self.seek(target)?;
Ok(true)
}
/// Return the total number of records in the file.
///
/// Scans all chunk headers summing `num_records` without decompressing any
/// record data. The current read position is preserved — the next
/// `read_record()` after `size()` returns the same record it would have
/// without the `size()` call.
pub fn size(&mut self) -> Result<u64, RiegeliError> {
// Save the current read state.
let saved_pos = self.pos;
let saved_last_pos = self.last_pos;
let saved_next_chunk_file_pos = self.next_chunk_file_pos;
let saved_current_chunk_begin = self.current_chunk_begin;
let saved_current_record_index = self.current_record_index;
let saved_at_eof = self.at_eof;
let saved_last_record_is_valid = self.last_record_is_valid;
// Scan from the first data chunk (offset 64).
let first_data_chunk = BLOCK_HEADER_SIZE + CHUNK_HEADER_SIZE; // = 64
let mut scan_pos = first_data_chunk;
let mut total_records: u64 = 0;
loop {
// Read chunk header (skipping leading and interleaved block headers).
let (ch, chunk_begin, _) = match self.read_chunk_header_at(scan_pos) {
Ok(Some(v)) => v,
Ok(None) => break, // EOF
Err(e) => {
self.restore_state(
saved_pos,
saved_last_pos,
saved_next_chunk_file_pos,
saved_current_chunk_begin,
saved_current_record_index,
saved_at_eof,
saved_last_record_is_valid,
);
return Err(e);
}
};
if !ch.is_header_valid() {
self.restore_state(
saved_pos,
saved_last_pos,
saved_next_chunk_file_pos,
saved_current_chunk_begin,
saved_current_record_index,
saved_at_eof,
saved_last_record_is_valid,
);
return Err(RiegeliError::MalformedData(
format!("invalid chunk header at {chunk_begin} during size scan").into(),
));
}
let data_size = ch.data_size();
let num_records = ch.num_records();
if matches!(
ch.chunk_type(),
Ok(ChunkType::Simple) | Ok(ChunkType::Transposed)
) {
total_records += num_records;
}
// Advance past this chunk.
scan_pos = crate::block_arithmetic::chunk_end(chunk_begin, data_size, num_records);
}
// Restore state.
self.restore_state(
saved_pos,
saved_last_pos,
saved_next_chunk_file_pos,
saved_current_chunk_begin,
saved_current_record_index,
saved_at_eof,
saved_last_record_is_valid,
);
// Also reset current_decoder to None (we disrupted the internal state).
// Seek back to restore the active decoder.
if !saved_at_eof {
// Special-case: if saved_pos is the initial position (before any
// records have been read), seek() would try to load the chunk at
// chunk_begin=24 (the signature chunk), which sets at_eof=true and
// breaks subsequent reads. Instead, directly restore the initial state.
let is_initial_position =
saved_pos.chunk_begin == BLOCK_HEADER_SIZE && saved_pos.record_index == 0;
if is_initial_position {
// Restore directly without calling seek().
self.next_chunk_file_pos = BLOCK_HEADER_SIZE + CHUNK_HEADER_SIZE; // = 64
self.at_eof = false;
self.current_decoder = None;
} else {
// Re-seek to restore decoder state.
let _ = self.seek(saved_pos);
// Restore last_pos and valid flag after seek changes them.
self.last_pos = saved_last_pos;
self.last_record_is_valid = saved_last_record_is_valid;
}
}
Ok(total_records)
}
/// Validate all block and chunk headers and data hashes in the file.
///
/// Does not decompress any record data — only validates the raw (possibly
/// compressed) chunk data against the stored hash. Returns `Ok(())` if all
/// headers and data hashes are valid, or `Err(RiegeliError::MalformedData(_))`
/// on the first validation failure.
///
/// The current read position is not changed by this method.
pub fn check_file_format(&mut self) -> Result<(), RiegeliError> {
// Validate the initial block header at offset 0.
self.reader.seek(SeekFrom::Start(0))?;
let mut bh_bytes = [0u8; 24];
self.reader.read_exact(&mut bh_bytes)?;
let bh = BlockHeader::from_bytes(bh_bytes);
if !bh.is_valid() {
return Err(RiegeliError::MalformedData(
"invalid block header hash at offset 0".into(),
));
}
// Scan all chunks starting from the signature chunk (offset 24).
let mut scan_pos: u64 = BLOCK_HEADER_SIZE; // = 24
// Read chunk headers until EOF (skipping leading and interleaved block headers).
while let Some((ch, chunk_begin, data_begin)) = self.read_chunk_header_at(scan_pos)? {
if !ch.is_header_valid() {
return Err(RiegeliError::MalformedData(
format!("invalid chunk header hash at offset {chunk_begin}").into(),
));
}
let data_size = ch.data_size();
let num_records = ch.num_records();
// Read the raw chunk data (without decompressing) and validate data hash.
let chunk_data = self.read_chunk_data(data_begin, data_size)?;
if !ch.is_data_valid(&chunk_data) {
return Err(RiegeliError::MalformedData(
format!("chunk data hash mismatch at offset {chunk_begin}").into(),
));
}
// Advance past this chunk.
scan_pos = crate::block_arithmetic::chunk_end(chunk_begin, data_size, num_records);
}
Ok(())
}
// Helper to restore reader state after a non-destructive scan.
#[allow(clippy::too_many_arguments)]
fn restore_state(
&mut self,
pos: RecordPosition,
last_pos: RecordPosition,
next_chunk_file_pos: u64,
current_chunk_begin: u64,
current_record_index: u64,
at_eof: bool,
last_record_is_valid: bool,
) {
self.pos = pos;
self.last_pos = last_pos;
self.next_chunk_file_pos = next_chunk_file_pos;
self.current_chunk_begin = current_chunk_begin;
self.current_record_index = current_record_index;
self.at_eof = at_eof;
self.last_record_is_valid = last_record_is_valid;
self.current_decoder = None;
}
// -------------------------------------------------------------------------
// Internal helpers
// -------------------------------------------------------------------------
/// The most recent region reported to the recovery callback, whether the
/// callback continued or cancelled. `None` if the callback never fired.
///
/// This is the Rust stand-in for the reader parameter the C++ recovery
/// callback receives: after a cancelled operation returns its error, the
/// caller can inspect the region here and reposition explicitly.
pub fn last_skipped_region(&self) -> Option<&crate::SkippedRegion> {
self.last_skipped_region.as_ref()
}
/// Attempt recovery for `error`, which left the reader position-stable
/// at `at` (every error path rewinds to the failed chunk — the
/// persistence invariant).
///
/// Computes the skipped region — COUPLED to the resync target by
/// construction: `region.end` is exactly where reading resumes.
/// A readable, hash-valid header (whose claims passed the stream bound)
/// gives a trustworthy extent, so exactly that chunk is skipped
/// (preserving siblings in the same block — more precise than a
/// boundary skip). Otherwise the claims cannot be trusted and the
/// resync is the next block boundary.
///
/// Returns `true` if a callback is set, it returned `true`, and the
/// reader was repositioned to the region end; `false` otherwise (the
/// caller returns the original error).
/// Compute the region for `error` and invoke the callback WITHOUT any
/// repositioning side effects — for operations whose contract promises
/// not to move the read cursor (metadata reads). Returns `None` when no
/// callback is set, otherwise `Some(callback verdict)`.
fn report_region_at(&mut self, at: u64, error: &RiegeliError) -> Option<bool> {
// Consume the failure-time classification unconditionally so it can
// never go stale across attempts or operations.
let trusted_end = self.pending_trusted_end.take();
self.recovery.as_ref()?;
let begin = crate::block_arithmetic::canonical_chunk_address(at);
let end = match trusted_end {
// The failing chunk's extent was trustworthy WHEN it failed
// (hash-valid header, stream-bounded claims): skip exactly it.
Some(end) => end,
// Header unreadable, hash-invalid, claims unvalidated, or I/O
// failure: nothing about the extent can be trusted — resync at
// the next block boundary, clamped to the stream length (a
// region cannot extend past the file — except by the minimal
// progress margin when corruption sits at EOF — and C++
// reports EOF-ended regions the same way).
None => {
let boundary = next_block_boundary(begin);
let boundary = if boundary == begin {
begin + BLOCK_SIZE
} else {
boundary
};
boundary.min(self.stream_len.max(begin))
}
};
// Forward progress no matter what the arithmetic said — measured
// from the RESYNC ORIGIN `at`, not just `begin`: when `at` is the
// boundary+24 alias of a canonical `begin` 24 bytes earlier, an
// EOF-clamped end can satisfy end > begin while still equaling
// `at`, and the reader would spin on the same region forever (a
// past-EOF end is fine — the resync seek lands at EOF and reads
// terminate).
let end = end.max(begin + 1).max(at.saturating_add(1));
// The resume position must never be a canonical ALIAS of this
// region's own begin: an EOF-clamped end landing on begin's
// boundary+24 alias would re-read the same corrupt bytes at
// canonical begin and double-report overlapping regions (breaking
// begin-monotonicity). The condition only triggers for that exact
// alias, so the bump is at most one byte and cannot loop.
let end = if crate::block_arithmetic::canonical_chunk_address(end) <= begin {
end + 1
} else {
end
};
let region = crate::SkippedRegion::new(begin, end, error.to_string());
self.last_skipped_region = Some(region);
let cb = self.recovery.as_mut().expect("checked above");
let region_ref = self.last_skipped_region.as_ref().expect("just set");
Some(cb(region_ref))
}
fn try_recover_at(&mut self, at: u64, error: &RiegeliError) -> bool {
let Some(go) = self.report_region_at(at, error) else {
// No callback configured: leave the reader untouched so the
// error stays position-stable and persistent on retry.
return false;
};
// The region is consumed REGARDLESS of the verdict, matching the
// C++ reference (Recover() repositions before the callback is
// consulted): cancel reports the error once, but the next
// operation continues past the rejected region. This is what
// makes a naive retry loop around a cancelling callback make
// progress instead of re-firing the same region forever.
let end = self
.last_skipped_region
.as_ref()
.expect("set by report_region_at")
.end();
self.last_record_is_valid = false;
self.current_decoder = None;
self.at_eof = false;
self.next_chunk_file_pos = end;
if self.reader.seek(SeekFrom::Start(end)).is_err() {
self.at_eof = true;
}
go
}
/// Load the next chunk from `self.next_chunk_file_pos`.
///
/// Returns `Ok(true)` if a chunk was loaded into `self.current_decoder`.
/// Returns `Ok(false)` at EOF.
/// Returns `Err` on corruption (without recovery).
fn load_next_chunk(&mut self) -> Result<bool, RiegeliError> {
loop {
// Each chunk attempt re-classifies from scratch.
self.pending_trusted_end = None;
let pos = self.next_chunk_file_pos;
// Read the chunk header, skipping any leading block header and any
// block header interleaved within the 40-byte header span.
// `chunk_begin` is the position of the header's first byte — the
// chunk's canonical address.
let (ch, chunk_begin, data_begin) = match self.read_chunk_header_at(pos)? {
Some(v) => v,
None => return Ok(false), // EOF
};
if !ch.is_header_valid() {
return Err(RiegeliError::MalformedData(
format!("invalid chunk header hash at file position {chunk_begin}").into(),
));
}
let data_size = ch.data_size();
let num_records = ch.num_records();
// Compute where the chunk data ends in the file (accounting for block headers).
let data_file_end =
crate::block_arithmetic::chunk_end(chunk_begin, data_size, num_records);
// Read the chunk data (skipping block headers).
let chunk_data = self.read_chunk_data(data_begin, data_size)?;
// Past this point the header is hash-valid, its claims are
// stream-bounded, and the data bytes were physically present:
// the chunk's extent is trustworthy at failure time, whatever
// the failure (bad data hash, unknown type, construction).
self.pending_trusted_end = Some(data_file_end);
// Validate data hash.
if !ch.is_data_valid(&chunk_data) {
// Leave next_chunk_file_pos at the chunk start so the error
// persists on retry and recovery scans from the right place.
self.next_chunk_file_pos = chunk_begin;
return Err(RiegeliError::MalformedData(
format!("chunk data hash mismatch at file position {chunk_begin}").into(),
));
}
// Update state for the next chunk.
self.next_chunk_file_pos = data_file_end;
// Resolve the chunk type only after next_chunk_file_pos has been
// advanced: an unknown type must skip the whole chunk (forward
// compatibility), and skipping requires the loop to make progress.
//
// Matching the C++ ChunkDecoder: an unknown chunk type is ignored
// only when it carries no records; skipping a chunk with records
// would lose data silently, so that case is an error.
let chunk_type = match ch.chunk_type() {
Ok(ct) => ct,
Err(_) if num_records == 0 => continue,
Err(e) => {
// Mirror the hash-mismatch convention: reset to the chunk
// start so the error is persistent — a bare retry must
// re-hit this chunk, not silently resume past its
// (dropped) records at the next chunk.
self.next_chunk_file_pos = chunk_begin;
return Err(e);
}
};
match chunk_type {
ChunkType::Simple => {
let chunk = Chunk {
header: ch,
data: chunk_data,
};
// Persistence: construction failures (malformed chunk
// interior behind valid hashes) must rewind like the
// hash-mismatch and unknown-type paths do — a bare retry
// must re-hit this chunk, not silently skip its records.
let decoder = match SimpleChunkDecoder::new(chunk) {
Ok(d) => d,
Err(e) => {
self.next_chunk_file_pos = chunk_begin;
return Err(e);
}
};
self.current_chunk_begin = chunk_begin;
self.current_record_index = 0;
self.pos = RecordPosition::new(chunk_begin, 0);
self.current_decoder = Some(ActiveDecoder::Simple(decoder));
let _ = num_records;
return Ok(true);
}
ChunkType::Transposed => {
let chunk = Chunk {
header: ch,
data: chunk_data,
};
// Same persistence convention as the Simple arm above.
let decoder = match TransposeChunkDecoder::new_with_projection(
chunk,
self.field_projection.as_ref(),
) {
Ok(d) => d,
Err(e) => {
self.next_chunk_file_pos = chunk_begin;
return Err(e);
}
};
self.current_chunk_begin = chunk_begin;
self.current_record_index = 0;
self.pos = RecordPosition::new(chunk_begin, 0);
self.current_decoder = Some(ActiveDecoder::Transposed(decoder));
let _ = num_records;
return Ok(true);
}
ChunkType::FileSignature | ChunkType::Padding => {
continue;
}
ChunkType::FileMetadata => {
continue;
}
}
}
}
/// Read the 40-byte chunk header at `pos`, skipping and validating the
/// block headers that the writer interleaves at every block boundary —
/// both a block header directly at `pos` and one falling inside the
/// 40-byte span (a chunk header may straddle a block boundary).
///
/// Returns `Ok(None)` on a clean EOF. Otherwise returns
/// `(header, chunk_begin, data_begin)`: `chunk_begin` is the position of
/// the header's first byte (after any leading block header) — the value
/// to use for record positions and `advance_past_chunk` — and
/// `data_begin` is the position of the first chunk-data byte.
fn read_chunk_header_at(
&mut self,
pos: u64,
) -> Result<Option<(ChunkHeader, u64, u64)>, RiegeliError> {
// Canonicalize: a chunk whose header physically follows a block
// header is addressed AT the block boundary; the first-header-byte
// form (boundary + 24) is accepted as an alias of the same chunk.
let chunk_begin = crate::block_arithmetic::canonical_chunk_address(pos);
let mut file_pos = chunk_begin;
let mut bytes = [0u8; 40]; // CHUNK_HEADER_SIZE
let mut filled: usize = 0;
while filled < bytes.len() {
if is_block_boundary(file_pos) {
self.reader.seek(SeekFrom::Start(file_pos))?;
let mut bh_bytes = [0u8; 24]; // BLOCK_HEADER_SIZE
match self.reader.read_exact(&mut bh_bytes) {
Ok(()) => {}
Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => return Ok(None),
Err(e) => return Err(e.into()),
}
let bh = BlockHeader::from_bytes(bh_bytes);
if !bh.is_valid() {
return Err(RiegeliError::MalformedData(
format!("invalid block header hash at file position {file_pos}").into(),
));
}
file_pos += BLOCK_HEADER_SIZE;
}
let until_boundary = BLOCK_SIZE - (file_pos % BLOCK_SIZE);
let to_read = ((bytes.len() - filled) as u64).min(until_boundary) as usize;
self.reader.seek(SeekFrom::Start(file_pos))?;
match self.reader.read_exact(&mut bytes[filled..filled + to_read]) {
Ok(()) => {}
Err(e) if e.kind() == std::io::ErrorKind::UnexpectedEof => return Ok(None),
Err(e) => return Err(e.into()),
}
filled += to_read;
file_pos += to_read as u64;
}
let ch = ChunkHeader::from_bytes(bytes);
// Validate header-claimed sizes against the physical stream before
// any caller does arithmetic, allocation, or overhead walking with
// them. The header hash only proves integrity, not honesty — anyone
// authoring a file can hash arbitrary claims, and unchecked claims
// reach u64 arithmetic (overflow), Vec::with_capacity (allocation
// bombs), and an O(claim) block-overhead walk. No well-formed file
// is rejected: chunk data cannot extend past end of file, and the
// format guarantees a chunk spans at least num_records file bytes.
// Claims of a hash-invalid header are not checked here — callers
// report those with their own header-hash errors.
if ch.is_header_valid() {
let data_begin = file_pos;
if ch.data_size() > self.stream_len.saturating_sub(data_begin)
|| ch.num_records() > self.stream_len.saturating_sub(chunk_begin)
{
// Re-measure before rejecting: a seek resets EOF, so a file
// that grew since the last measurement is a supported way
// to keep reading — the bound must track the growth.
self.stream_len = self.reader.seek(SeekFrom::End(0))?;
if ch.data_size() > self.stream_len.saturating_sub(data_begin) {
return Err(RiegeliError::MalformedData(format!(
"chunk at {chunk_begin} claims {} data bytes with only {} bytes left in the stream",
ch.data_size(),
self.stream_len.saturating_sub(data_begin)
).into()));
}
if ch.num_records() > self.stream_len.saturating_sub(chunk_begin) {
return Err(RiegeliError::MalformedData(format!(
"chunk at {chunk_begin} claims {} records with only {} bytes left in the stream",
ch.num_records(),
self.stream_len.saturating_sub(chunk_begin)
).into()));
}
}
}
Ok(Some((ch, chunk_begin, file_pos)))
}
/// Read `data_size` bytes of chunk data starting at `data_begin`,
/// skipping block headers at boundaries. `data_begin` must be the
/// position of the first data byte (as returned by
/// `read_chunk_header_at`), which is not `chunk_begin + 40` when the
/// chunk header straddles a block boundary.
fn read_chunk_data(
&mut self,
data_begin: u64,
data_size: u64,
) -> Result<Vec<u8>, RiegeliError> {
// data_size is validated against the stream by read_chunk_header_at;
// the min is defense in depth for any future unvalidated caller.
let mut result = Vec::with_capacity(data_size.min(self.stream_len) as usize);
let mut remaining = data_size;
let mut file_pos = data_begin;
// Always position explicitly: callers cannot guarantee the reader's
// physical position (the header read may have re-measured the stream
// length against a growing file, which seeks to the end).
self.reader.seek(SeekFrom::Start(file_pos))?;
while remaining > 0 {
// Skip block header if at boundary.
if is_block_boundary(file_pos) {
let mut bh_bytes = [0u8; 24]; // BLOCK_HEADER_SIZE
self.reader.seek(SeekFrom::Start(file_pos))?;
self.reader.read_exact(&mut bh_bytes)?;
let bh = BlockHeader::from_bytes(bh_bytes);
if !bh.is_valid() {
return Err(RiegeliError::MalformedData(format!(
"invalid block header hash at file position {file_pos} (during data read)"
).into()));
}
file_pos += BLOCK_HEADER_SIZE;
// Seek to data position after the block header.
self.reader.seek(SeekFrom::Start(file_pos))?;
}
// How many bytes can we read before hitting the next block boundary?
let bytes_until_boundary = BLOCK_SIZE - (file_pos % BLOCK_SIZE);
let to_read = remaining.min(bytes_until_boundary) as usize;
let old_len = result.len();
result.resize(old_len + to_read, 0);
self.reader.read_exact(&mut result[old_len..])?;
file_pos += to_read as u64;
remaining -= to_read as u64;
}
Ok(result)
}
/// Load and decode a chunk at the given file position, returning the decoder.
///
/// Returns `Ok(None)` at EOF.
fn load_chunk_at(&mut self, file_pos: u64) -> Result<Option<ActiveDecoder>, RiegeliError> {
let mut file_pos = file_pos;
loop {
// Each chunk attempt re-classifies from scratch.
self.pending_trusted_end = None;
// Read the chunk header (skipping leading and interleaved block headers).
let (ch, chunk_begin, data_begin) = match self.read_chunk_header_at(file_pos)? {
Some(v) => v,
None => return Ok(None), // EOF
};
if !ch.is_header_valid() {
return Err(RiegeliError::MalformedData(
format!("invalid chunk header hash at file position {chunk_begin}").into(),
));
}
let data_size = ch.data_size();
let chunk_data = self.read_chunk_data(data_begin, data_size)?;
// Advance only on success: if decoder construction below fails, the
// position stays at this chunk so the error is persistent on retry
// (same convention as load_next_chunk).
let chunk_end_pos =
crate::block_arithmetic::chunk_end(chunk_begin, data_size, ch.num_records());
// Header valid, claims bounded, data present: trustworthy extent
// for any failure from here on (see load_next_chunk).
self.pending_trusted_end = Some(chunk_end_pos);
if !ch.is_data_valid(&chunk_data) {
return Err(RiegeliError::MalformedData(
format!("chunk data hash mismatch at file position {chunk_begin}").into(),
));
}
self.current_chunk_begin = chunk_begin;
self.current_record_index = 0;
match ch.chunk_type() {
Ok(ChunkType::Simple) => {
let chunk = Chunk {
header: ch,
data: chunk_data,
};
let decoder = SimpleChunkDecoder::new(chunk)?;
self.next_chunk_file_pos = chunk_end_pos;
return Ok(Some(ActiveDecoder::Simple(decoder)));
}
Ok(ChunkType::Transposed) => {
let chunk = Chunk {
header: ch,
data: chunk_data,
};
let decoder = TransposeChunkDecoder::new_with_projection(
chunk,
self.field_projection.as_ref(),
)?;
self.next_chunk_file_pos = chunk_end_pos;
return Ok(Some(ActiveDecoder::Transposed(decoder)));
}
_ => {
// A non-data chunk (signature, metadata, padding) is not
// EOF: scan forward to the next data chunk, the same way
// seek_numeric does. Conflating the two wedged the reader
// at EOF for every read after a seek to such an address.
// Iterative on purpose: a long run of tiny padding chunks
// must cost O(file size) scanning, not a stack frame per
// chunk (a crafted 2 MB padding run overflowed the stack
// when this was recursive).
self.next_chunk_file_pos = chunk_end_pos;
file_pos = chunk_end_pos;
}
}
}
}
/// Peek at the chunk header at file_pos without advancing state.
fn peek_chunk_header(&mut self, file_pos: u64) -> Result<Option<ChunkHeader>, RiegeliError> {
match self.read_chunk_header_at(file_pos)? {
None => Ok(None),
// Hash-invalid headers carry unvalidated claims (the stream-bound
// check in read_chunk_header_at only covers hash-valid headers,
// since the other callers report hash failures themselves). A
// peek must not hand such claims to seek scans or metadata reads
// — a corrupted header claiming a huge size would drive an
// O(claim) overhead walk or an oversized read.
Some((ch, chunk_begin, _)) => {
if !ch.is_header_valid() {
return Err(RiegeliError::MalformedData(
format!("invalid chunk header hash at file position {chunk_begin}").into(),
));
}
Ok(Some(ch))
}
}
}
}
/// Return the next block boundary strictly after `pos`.
fn next_block_boundary(pos: u64) -> u64 {
if is_block_boundary(pos) {
pos
} else {
round_down_to_block_boundary(pos) + BLOCK_SIZE
}
}
#[cfg(test)]
mod tests {
use std::cell::RefCell;
use std::io::Cursor;
use std::rc::Rc;
use super::*;
use crate::compression::CompressionType;
use crate::record_writer::{RecordWriter, WriterOptions};
/// Write records to a Vec<u8> and return the bytes.
fn write_records(records: &[&[u8]], opts: WriterOptions) -> Vec<u8> {
let mut buf = Cursor::new(Vec::<u8>::new());
{
let mut w = RecordWriter::new(&mut buf, opts).expect("new ok");
for rec in records {
w.write_record(rec).expect("write ok");
}
w.flush().expect("flush ok");
}
buf.into_inner()
}
// -------------------------------------------------------------------------
// Criterion 6.1: read back a RecordWriter-written file
// -------------------------------------------------------------------------
#[test]
fn roundtrip_basic() {
let records: &[&[u8]] = &[b"hello", b"world", b"riegeli"];
let data = write_records(records, WriterOptions::new());
let cursor = Cursor::new(data);
let mut reader = RecordReader::new(cursor, ReaderOptions::new()).expect("reader new ok");
let mut got = Vec::new();
while let Some(rec) = reader.read_record().expect("read ok") {
got.push(rec);
}
assert_eq!(got.len(), records.len());
for (i, (got, expected)) in got.iter().zip(records.iter()).enumerate() {
assert_eq!(got.as_slice(), *expected, "record {i} mismatch");
}
}
// -------------------------------------------------------------------------
// Criterion 6.1: 100 records
// -------------------------------------------------------------------------
#[test]
fn roundtrip_100_records() {
let record_data: Vec<u8> = (0..100u8).collect();
let records: Vec<&[u8]> = (0..100).map(|_| record_data.as_slice()).collect();
let data = write_records(&records, WriterOptions::new());
let cursor = Cursor::new(data);
let mut reader = RecordReader::new(cursor, ReaderOptions::new()).expect("reader new ok");
let mut count = 0usize;
while let Some(rec) = reader.read_record().expect("read ok") {
assert_eq!(rec, record_data, "record {count} mismatch");
count += 1;
}
assert_eq!(count, 100);
}
// -------------------------------------------------------------------------
// Criterion 6.3: pos() at start
// -------------------------------------------------------------------------
#[test]
fn pos_at_start() {
let data = write_records(&[b"test"], WriterOptions::new());
let cursor = Cursor::new(data);
let reader = RecordReader::new(cursor, ReaderOptions::new()).expect("reader new ok");
let pos = reader.pos();
// Matches the C++ reference: initial position is numeric 0 (the
// earlier criterion said 24; the differential harness showed the
// reference returns 0 and the reference wins).
assert_eq!(pos.numeric(), 0, "initial position is numeric 0");
assert_eq!(pos.record_index, 0, "record_index should be 0");
}
// -------------------------------------------------------------------------
// Criterion 6.4: last_pos().numeric() → seek_numeric → same record
// -------------------------------------------------------------------------
#[test]
fn seek_numeric_roundtrip() {
let records: Vec<Vec<u8>> = (0..10u8).map(|i| vec![i; 50]).collect();
let record_refs: Vec<&[u8]> = records.iter().map(|r| r.as_slice()).collect();
let data = write_records(&record_refs, WriterOptions::new().chunk_size(200));
let data = std::sync::Arc::new(data);
let cursor = Cursor::new((*data).clone());
let mut reader = RecordReader::new(cursor, ReaderOptions::new()).expect("reader new ok");
// Read a few records and verify seek_numeric can re-read them.
let mut positions = Vec::new();
let mut read_records_vec = Vec::new();
while let Some(rec) = reader.read_record().expect("read ok") {
positions.push(reader.last_pos());
read_records_vec.push(rec);
}
// Now for each position, seek_numeric and re-read.
for (i, (&pos, expected)) in positions.iter().zip(read_records_vec.iter()).enumerate() {
let cursor2 = Cursor::new((*data).clone());
let mut reader2 =
RecordReader::new(cursor2, ReaderOptions::new()).expect("reader new ok");
reader2
.seek_numeric(pos.numeric())
.expect("seek_numeric ok");
let rec = reader2
.read_record()
.expect("read ok after seek")
.expect("should have record");
assert_eq!(&rec, expected, "record {i} mismatch after seek_numeric");
}
}
// -------------------------------------------------------------------------
// Criterion 6.5: corruption handling
// -------------------------------------------------------------------------
#[test]
fn corruption_no_recovery() {
let records: &[&[u8]] = &[b"before", b"during", b"after"];
let mut data = write_records(records, WriterOptions::new().chunk_size(10));
// Corrupt the second chunk's data (skip header at 0, sig chunk at 24..64, first data chunk starts at 64).
// The first data chunk header is at 64 (40 bytes), data starts at 104.
// Let's find the second data chunk by reading the first chunk's size.
// For simplicity, just corrupt some bytes in the middle of the file.
let mid = data.len() / 2;
// Flip some bytes in the middle, making sure we're not in a block header.
for i in mid..mid + 4 {
if i < data.len() {
data[i] ^= 0xFF;
}
}
let cursor = Cursor::new(data);
let mut reader = RecordReader::new(cursor, ReaderOptions::new()).expect("reader new ok");
// Without recovery, should return Err at some point.
let mut found_err = false;
for _ in 0..10 {
match reader.read_record() {
Err(_) => {
found_err = true;
break;
}
Ok(None) => break,
Ok(Some(_)) => {}
}
}
assert!(
found_err,
"expected an error when reading corrupted file without recovery"
);
}
#[test]
fn corruption_with_recovery() {
// Write many records spread across multiple chunks.
let records: Vec<Vec<u8>> = (0..50u8).map(|i| vec![i; 100]).collect();
let record_refs: Vec<&[u8]> = records.iter().map(|r| r.as_slice()).collect();
let mut data = write_records(&record_refs, WriterOptions::new().chunk_size(200));
// Corrupt the middle of the file (past the first block of data, so
// there are records before and after the corruption).
// Find a good spot: skip initial headers and corrupt something in the data area.
// We need to corrupt inside a chunk (not a block header) to trigger recovery.
let mid = (data.len() / 2).max(100);
// Make sure we're not corrupting a block header position.
let mid = if mid % 65536 < 24 { mid + 24 } else { mid };
if mid + 8 < data.len() {
for i in mid..mid + 8 {
data[i] ^= 0xFF;
}
}
let recovered_positions: Rc<RefCell<Vec<u64>>> = Rc::new(RefCell::new(Vec::new()));
let recovered_clone = Rc::clone(&recovered_positions);
let cursor = Cursor::new(data);
let opts = ReaderOptions::new().recovery(move |region| {
recovered_clone.borrow_mut().push(region.begin());
true
});
let mut reader = RecordReader::new(cursor, opts).expect("reader new ok");
// Read all records (with recovery, should not return Err).
let mut all_records = Vec::new();
loop {
match reader.read_record() {
Ok(Some(rec)) => all_records.push(rec),
Ok(None) => break,
Err(e) => panic!("unexpected error with recovery: {e}"),
}
}
// Recovery should have been triggered (some records recovered or skipped).
// We should have read at least some records.
assert!(
!all_records.is_empty(),
"should have read some records with recovery"
);
// Recovery callback should have been called at least once.
assert!(
!recovered_positions.borrow().is_empty(),
"recovery callback should have been called"
);
}
// -------------------------------------------------------------------------
// Criterion 6.6: seek_numeric to middle of chunk
// -------------------------------------------------------------------------
#[test]
fn seek_numeric_mid_chunk() {
// Write records into a single chunk (large chunk_size so all go in one).
let records: Vec<Vec<u8>> = (0..10u8).map(|i| vec![i; 20]).collect();
let record_refs: Vec<&[u8]> = records.iter().map(|r| r.as_slice()).collect();
let data = write_records(&record_refs, WriterOptions::new().chunk_size(1 << 20));
// All 10 records are in one chunk starting at 64.
// chunk_begin = 64, record_index 0..9.
// numeric for record 5 = 64 + 5 = 69.
// seek_numeric(67) should resolve to the record at chunk_begin=64, record_index=3 (67-64=3).
// That is records[3] = vec![3; 20].
let cursor = Cursor::new(data);
let mut reader = RecordReader::new(cursor, ReaderOptions::new()).expect("reader new ok");
reader.seek_numeric(64 + 3).expect("seek_numeric ok");
let rec = reader
.read_record()
.expect("read ok")
.expect("should have record");
assert_eq!(rec, vec![3u8; 20], "expected record[3]");
}
// -------------------------------------------------------------------------
// Criterion 6.7: read_metadata returns None
// -------------------------------------------------------------------------
#[test]
fn read_metadata_returns_none() {
let data = write_records(&[b"x"], WriterOptions::new());
let cursor = Cursor::new(data);
let mut reader = RecordReader::new(cursor, ReaderOptions::new()).expect("reader new ok");
let meta = reader.read_metadata().expect("read_metadata ok");
assert!(meta.is_none(), "expected None from read_metadata");
}
// -------------------------------------------------------------------------
// Criterion 6.8: EOF returns Ok(None), then Ok(None) again
// -------------------------------------------------------------------------
#[test]
fn eof_returns_none_repeatedly() {
let data = write_records(&[b"only"], WriterOptions::new());
let cursor = Cursor::new(data);
let mut reader = RecordReader::new(cursor, ReaderOptions::new()).expect("reader new ok");
// Read the one record.
let rec = reader
.read_record()
.expect("first read ok")
.expect("should have record");
assert_eq!(rec, b"only");
// EOF.
let r1 = reader.read_record().expect("second read ok");
assert!(r1.is_none(), "expected None at EOF");
// EOF again.
let r2 = reader.read_record().expect("third read ok");
assert!(r2.is_none(), "expected None again");
}
// -------------------------------------------------------------------------
// Multi-block roundtrip
// -------------------------------------------------------------------------
#[test]
fn roundtrip_multi_block() {
// Write enough data to span multiple blocks.
let record: Vec<u8> = vec![0xAB; 1000];
let records: Vec<&[u8]> = (0..100).map(|_| record.as_slice()).collect();
let data = write_records(&records, WriterOptions::new().chunk_size(4096));
let cursor = Cursor::new(data);
let mut reader = RecordReader::new(cursor, ReaderOptions::new()).expect("reader new ok");
let mut count = 0usize;
while let Some(rec) = reader.read_record().expect("read ok") {
assert_eq!(rec, record, "record {count} mismatch");
count += 1;
}
assert_eq!(count, 100, "should read exactly 100 records");
}
// -------------------------------------------------------------------------
// Criterion 9.8: Interleaved simple and transposed chunks
// -------------------------------------------------------------------------
#[test]
fn interleaved_simple_and_transposed() {
// Build a file by hand: signature + simple chunk + transposed chunk.
// We use the record_writer to write a normal file (simple chunks only),
// then manually splice in a transposed chunk.
//
// For simplicity, we write a file with simple chunk records, then create
// a separate transposed chunk and concatenate them into a valid file.
use crate::block_header::BlockHeader;
use crate::chunk_header::{ChunkHeader, ChunkType};
use crate::simple_chunk::SimpleChunkEncoder;
use crate::transpose::internal::message_id;
use crate::varint::{encode_u32, encode_u64};
// Build the file manually:
// [BlockHeader at 0] [FileSignature ChunkHeader at 24] [Simple ChunkHeader] [Simple Data] [Transposed ChunkHeader] [Transposed Data]
let mut file_data: Vec<u8> = Vec::new();
// Block header at offset 0.
// We'll fill it in later once we know sizes.
let bh_placeholder = [0u8; 24];
file_data.extend_from_slice(&bh_placeholder);
// File signature chunk.
let sig_header = ChunkHeader::from_parts(&[], ChunkType::FileSignature, 0, 0);
file_data.extend_from_slice(&sig_header.to_bytes());
// Simple chunk with 2 records.
let mut simple_enc = SimpleChunkEncoder::new();
simple_enc.add_record(b"simple_one");
simple_enc.add_record(b"simple_two");
let simple_chunk = simple_enc.encode().unwrap();
file_data.extend_from_slice(&simple_chunk.header.to_bytes());
file_data.extend_from_slice(&simple_chunk.data);
// Transposed chunk with 1 nonproto record "transposed".
let nonproto_data = b"transposed".to_vec();
let mut nonproto_lengths = Vec::new();
nonproto_lengths.extend_from_slice(&encode_u32(10));
// Build transpose header.
let mut header_bytes: Vec<u8> = Vec::new();
header_bytes.extend_from_slice(&encode_u32(1)); // num_buckets
header_bytes.extend_from_slice(&encode_u32(2)); // num_buffers
let total_buf: usize = nonproto_data.len() + nonproto_lengths.len();
header_bytes.extend_from_slice(&encode_u64(total_buf as u64)); // bucket compressed size
header_bytes.extend_from_slice(&encode_u64(nonproto_data.len() as u64)); // buf 0 size
header_bytes.extend_from_slice(&encode_u64(nonproto_lengths.len() as u64)); // buf 1 size
header_bytes.extend_from_slice(&encode_u32(1)); // num_states
header_bytes.extend_from_slice(&encode_u32(message_id::NON_PROTO)); // tag for state 0
header_bytes.extend_from_slice(&encode_u32(0)); // next_node for state 0
// NonProto reads buffer_index:
header_bytes.extend_from_slice(&encode_u32(0)); // buffer_index = 0 (nonproto data)
header_bytes.extend_from_slice(&encode_u32(0)); // first_node
let mut trans_data: Vec<u8> = Vec::new();
trans_data.push(0x00); // CompressionType::None
trans_data.extend_from_slice(&encode_u64(header_bytes.len() as u64));
trans_data.extend_from_slice(&header_bytes);
trans_data.extend_from_slice(&nonproto_data);
trans_data.extend_from_slice(&nonproto_lengths);
// no transitions
let trans_header = ChunkHeader::from_parts(&trans_data, ChunkType::Transposed, 1, 10);
file_data.extend_from_slice(&trans_header.to_bytes());
file_data.extend_from_slice(&trans_data);
// Fix the block header.
// next_chunk = distance from 0 to end of signature chunk = 64.
// previous_chunk = 0.
let bh = BlockHeader::from_parts(0, 64);
let bh_bytes = bh.to_bytes();
file_data[..24].copy_from_slice(&bh_bytes);
// Read all records.
let cursor = Cursor::new(file_data);
let mut reader = RecordReader::new(cursor, ReaderOptions::new()).expect("reader new ok");
let mut got = Vec::new();
while let Some(rec) = reader.read_record().expect("read ok") {
got.push(rec);
}
assert_eq!(got.len(), 3, "should have 3 records total");
assert_eq!(got[0], b"simple_one");
assert_eq!(got[1], b"simple_two");
assert_eq!(got[2], b"transposed");
}
// -------------------------------------------------------------------------
// Brotli roundtrip (when feature enabled)
// -------------------------------------------------------------------------
#[test]
#[cfg(feature = "brotli")]
fn roundtrip_brotli() {
let records: &[&[u8]] = &[b"compressed1", b"compressed2", b"compressed3"];
let data = write_records(
records,
WriterOptions::new().compression(CompressionType::Brotli),
);
let cursor = Cursor::new(data);
let mut reader = RecordReader::new(cursor, ReaderOptions::new()).expect("reader new ok");
let mut got = Vec::new();
while let Some(rec) = reader.read_record().expect("read ok") {
got.push(rec);
}
assert_eq!(got.len(), records.len());
for (i, (got, expected)) in got.iter().zip(records.iter()).enumerate() {
assert_eq!(got.as_slice(), *expected, "brotli record {i} mismatch");
}
}
// -------------------------------------------------------------------------
// Unknown chunk types must be skipped, not re-read forever
// -------------------------------------------------------------------------
/// A well-formed 40-byte chunk header whose type byte is not any known
/// `ChunkType` discriminant, with zero data bytes. Both hashes are valid,
/// so only the type is unrecognized — the forward-compatibility case.
fn unknown_type_chunk() -> Vec<u8> {
unknown_type_chunk_with_records(0)
}
/// A 40-byte Simple-chunk header with valid hashes but hostile claimed
/// sizes, and no data bytes. The hash proves integrity, not honesty —
/// these claims must be rejected against the physical stream.
fn hostile_simple_chunk(data_size: u64, num_records: u64) -> Vec<u8> {
let data_hash = crate::hash::highway_hash_64(&[]);
let chunk_type_and_num_records: u64 = (num_records << 8) | (ChunkType::Simple as u8 as u64);
let decoded_data_size: u64 = 0;
let mut body = [0u8; 32];
body[0..8].copy_from_slice(&data_size.to_le_bytes());
body[8..16].copy_from_slice(&data_hash.to_le_bytes());
body[16..24].copy_from_slice(&chunk_type_and_num_records.to_le_bytes());
body[24..32].copy_from_slice(&decoded_data_size.to_le_bytes());
let header_hash = crate::hash::highway_hash_64(&body);
let mut out = Vec::with_capacity(40);
out.extend_from_slice(&header_hash.to_le_bytes());
out.extend_from_slice(&body);
out
}
fn unknown_type_chunk_with_records(num_records: u64) -> Vec<u8> {
let data_size: u64 = 0;
let data_hash = crate::hash::highway_hash_64(&[]);
let chunk_type_and_num_records: u64 = (num_records << 8) | b'z' as u64; // 'z' is not a known type
let decoded_data_size: u64 = 0;
let mut body = [0u8; 32];
body[0..8].copy_from_slice(&data_size.to_le_bytes());
body[8..16].copy_from_slice(&data_hash.to_le_bytes());
body[16..24].copy_from_slice(&chunk_type_and_num_records.to_le_bytes());
body[24..32].copy_from_slice(&decoded_data_size.to_le_bytes());
let header_hash = crate::hash::highway_hash_64(&body);
let mut out = Vec::with_capacity(40);
out.extend_from_slice(&header_hash.to_le_bytes());
out.extend_from_slice(&body);
out
}
#[test]
fn unknown_chunk_type_at_end_is_skipped() {
let mut data = write_records(&[b"only"], WriterOptions::new());
data.extend_from_slice(&unknown_type_chunk());
let mut reader =
RecordReader::new(Cursor::new(data), ReaderOptions::new()).expect("reader new ok");
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(&b"only"[..])
);
// Before the fix this call never returned: load_next_chunk() hit the
// unknown chunk and retried the same file position forever.
assert_eq!(reader.read_record().expect("read ok"), None);
}
// -------------------------------------------------------------------------
// Chunk headers that straddle a 64 KiB block boundary
// -------------------------------------------------------------------------
/// Write `n` single-record chunks of `rec_size` incompressible-layout
/// bytes (flush per record, no compression) and return the file bytes.
/// With rec_size around 16 KiB the fourth chunk's header lands near the
/// first 64 KiB block boundary.
fn write_chunks_past_first_block(rec_size: usize, n: usize) -> Vec<u8> {
let mut buf = Cursor::new(Vec::<u8>::new());
{
let mut w = RecordWriter::new(
&mut buf,
WriterOptions::new().compression(CompressionType::None),
)
.expect("writer new ok");
for i in 0..n {
let rec = vec![(i % 251) as u8; rec_size];
w.write_record(&rec).expect("write ok");
w.flush().expect("flush ok");
}
}
buf.into_inner()
}
/// Returns true if any chunk in the file has a header straddling a block
/// boundary (header begins within 40 bytes below a 64 KiB multiple).
///
/// Detected by direct inspection of the raw block-header bytes the
/// writer emitted — deliberately NOT via the reader under test, whose
/// position bookkeeping is part of what the straddle tests exercise.
/// The block header at a boundary stores the distance back to the start
/// of the chunk in progress there; a 40-byte chunk header straddles the
/// boundary iff that distance is in (0, 40).
fn has_straddling_chunk_header(data: &[u8]) -> bool {
let block = BLOCK_SIZE as usize;
let mut boundary = block;
let mut straddles = false;
while boundary + BLOCK_HEADER_SIZE as usize <= data.len() {
let prev = u64::from_le_bytes(data[boundary + 8..boundary + 16].try_into().unwrap());
if 0 < prev && prev < CHUNK_HEADER_SIZE {
straddles = true;
}
boundary += block;
}
straddles
}
/// The writer interleaves a 24-byte block header inside the 40-byte chunk
/// header when a chunk begins within 40 bytes of a block boundary. Before
/// the fix, every read path fetched the header with one contiguous
/// read_exact and failed with "invalid chunk header hash" on such files.
#[test]
fn chunk_header_straddling_block_boundary_roundtrip() {
let n = 6;
let mut exercised_straddle = false;
// Sweep the record size so the fourth chunk's header walks across the
// 64 KiB boundary; the exact straddling sizes shift if writer overhead
// changes, which is why this is a sweep and not a single size.
for rec_size in 16300..16340usize {
let data = write_chunks_past_first_block(rec_size, n);
exercised_straddle |= has_straddling_chunk_header(&data);
let mut reader =
RecordReader::new(Cursor::new(data), ReaderOptions::new()).expect("reader new ok");
let mut count = 0;
while let Some(rec) = reader.read_record().expect("read ok") {
assert_eq!(rec.len(), rec_size, "rec_size={rec_size} record {count}");
// The fill byte identifies the record, so a read that comes
// back the right length but from the wrong place still fails.
let fill = (count % 251) as u8;
assert!(
rec.iter().all(|&b| b == fill),
"rec_size={rec_size} record {count}: content mismatch"
);
count += 1;
}
assert_eq!(count, n, "rec_size={rec_size}: wrong record count");
}
assert!(
exercised_straddle,
"sweep never produced a straddling chunk header; widen the range"
);
}
/// size() and seek() walk chunk headers with their own scan loops; they
/// must handle straddling headers too.
#[test]
fn chunk_header_straddling_block_boundary_size_and_seek() {
let n = 6;
// Find a straddling layout within the sweep window.
let data = (16300..16340usize)
.map(|rec_size| write_chunks_past_first_block(rec_size, n))
.find(|data| has_straddling_chunk_header(data))
.expect("no straddling layout found in sweep; widen the range");
let mut reader =
RecordReader::new(Cursor::new(data), ReaderOptions::new()).expect("reader new ok");
assert_eq!(reader.size().expect("size ok"), n as u64);
// Collect record positions, then seek back to each and re-read.
let mut positions = Vec::new();
while let Some(_) = reader.read_record().expect("read ok") {
positions.push(reader.last_pos());
}
assert_eq!(positions.len(), n);
for (i, pos) in positions.into_iter().enumerate() {
reader.seek(pos).expect("seek ok");
let rec = reader
.read_record()
.expect("read after seek ok")
.unwrap_or_else(|| panic!("record {i} missing after seek"));
assert_eq!(rec[0], (i % 251) as u8, "record {i} content after seek");
}
}
#[test]
fn unknown_chunk_type_mid_stream_is_skipped() {
// Build two single-record files and splice an unknown chunk between
// the first file and the second file's record chunk. Layout of each
// writer output: block header (24) | signature chunk (40) | record chunk.
let first = write_records(&[b"first"], WriterOptions::new());
let second = write_records(&[b"second"], WriterOptions::new());
let mut data = first;
data.extend_from_slice(&unknown_type_chunk());
data.extend_from_slice(&second[64..]); // strip block header + signature chunk
let mut reader =
RecordReader::new(Cursor::new(data), ReaderOptions::new()).expect("reader new ok");
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(&b"first"[..])
);
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(&b"second"[..])
);
assert_eq!(reader.read_record().expect("read ok"), None);
}
/// Matching the C++ ChunkDecoder: an unknown chunk type that claims to
/// carry records cannot be skipped — its records would be silently lost —
/// so it is an error rather than a forward-compatibility skip.
#[test]
fn unknown_chunk_type_with_records_is_an_error() {
let mut data = write_records(&[b"only"], WriterOptions::new());
data.extend_from_slice(&unknown_type_chunk_with_records(3));
let mut reader =
RecordReader::new(Cursor::new(data), ReaderOptions::new()).expect("reader new ok");
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(&b"only"[..])
);
let err = reader
.read_record()
.expect_err("unknown chunk with records must error");
assert!(
err.to_string().contains("unknown chunk type")
|| err.to_string().contains("chunk type"),
"unexpected error: {err}"
);
}
/// The unknown-chunk-with-records error must be persistent: a caller
/// that ignores the error and calls `read_record` again must hit the
/// same error, not silently resume at the next chunk — that would skip
/// the unknown chunk's claimed records after all, defeating the guard.
/// Layout: [chunk "first"] [unknown type, num_records=3] [chunk "second"].
#[test]
fn unknown_chunk_type_error_is_persistent_and_does_not_skip_to_next_chunk() {
// chunk_size(1) forces one chunk per record, so the two-record file
// is [block hdr][signature][chunk "first"][chunk "second"] and the
// one-record file is the same minus the last chunk. Their shared
// prefix lets us splice an unknown chunk between the two chunks;
// chunks are position-independent below the first block boundary.
let one = write_records(&[b"first"], WriterOptions::new().chunk_size(1));
let two = write_records(&[b"first", b"second"], WriterOptions::new().chunk_size(1));
assert_eq!(
&two[..one.len()],
&one[..],
"one-record file must be a prefix"
);
let second_chunk = &two[one.len()..];
let mut data = one.clone();
data.extend_from_slice(&unknown_type_chunk_with_records(3));
data.extend_from_slice(second_chunk);
assert!(
data.len() < 65536,
"test assumes no block boundary is crossed"
);
let mut reader =
RecordReader::new(Cursor::new(data), ReaderOptions::new()).expect("reader new ok");
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(&b"first"[..])
);
// First attempt errors on the unknown chunk.
let err = reader
.read_record()
.expect_err("unknown chunk with records must error");
assert!(
err.to_string().contains("chunk type"),
"unexpected error: {err}"
);
// Retries must keep erroring on the same chunk; "second" must never
// surface past the guard.
for attempt in 0..3 {
match reader.read_record() {
Err(e) => assert!(
e.to_string().contains("chunk type"),
"attempt {attempt}: unexpected error: {e}"
),
Ok(rec) => panic!(
"attempt {attempt}: error was not persistent; got {:?}",
rec.as_deref()
.map(|r| String::from_utf8_lossy(r).into_owned())
),
}
}
}
/// Cancel semantics (C++ shape, adopted after the empirical trace):
/// the region is consumed BEFORE the callback runs — `false` makes the
/// failing operation return the ORIGINAL error once, but the next
/// operation continues past the rejected region and the callback is
/// never re-invoked for it. A LATER corrupt region fires the callback
/// afresh.
#[test]
fn recovery_cancel_reports_once_and_consumes_the_region() {
// [a][b CORRUPT][c][d CORRUPT][e]
let mut lens = Vec::new();
let recs: Vec<&[u8]> = vec![b"a", b"b", b"c", b"d", b"e"];
for k in 1..=recs.len() {
lens.push(write_records(&recs[..k], WriterOptions::new().chunk_size(1)).len());
}
let mut data = write_records(&recs, WriterOptions::new().chunk_size(1));
data[lens[1] - 1] ^= 0xFF; // chunk "b" data
data[lens[3] - 1] ^= 0xFF; // chunk "d" data
let regions: Rc<RefCell<Vec<crate::SkippedRegion>>> = Rc::new(RefCell::new(Vec::new()));
let rc = Rc::clone(®ions);
let opts = ReaderOptions::new().recovery(move |region| {
rc.borrow_mut().push(region.clone());
false // cancel every region
});
let mut reader = RecordReader::new(Cursor::new(data), opts).expect("reader new ok");
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(&b"a"[..])
);
let e1 = reader
.read_record()
.expect_err("cancel returns the original error");
// The region was consumed: the next read continues past "b" to "c".
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(&b"c"[..]),
"rejected region is already skipped; reading continues"
);
let e2 = reader
.read_record()
.expect_err("the LATER corrupt region errors afresh");
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(&b"e"[..])
);
assert_eq!(reader.read_record().expect("read ok"), None);
let regions = regions.borrow();
assert_eq!(regions.len(), 2, "one callback per region — never re-fired");
assert_eq!(regions[0].begin(), lens[0] as u64, "region 1 = chunk b");
assert_eq!(regions[0].end(), lens[1] as u64);
assert_eq!(regions[1].begin(), lens[2] as u64, "region 2 = chunk d");
assert_eq!(regions[1].end(), lens[3] as u64);
assert_ne!(e1.to_string(), "", "errors carry messages");
let _ = e2;
// The accessor exposes the most recent reported region.
assert_eq!(reader.last_skipped_region(), Some(®ions[1]));
}
/// Coupled region/resync precision: corrupting one chunk's DATA (its
/// header stays hash-valid, so its extent is trustworthy) must skip
/// exactly that chunk — the region is [chunk_begin, chunk_end) and the
/// SIBLING chunk in the same block is recovered. The old boundary skip
/// threw the sibling away.
#[test]
fn recovery_skips_exactly_one_chunk_when_header_is_valid() {
let one = write_records(&[b"a"], WriterOptions::new().chunk_size(1));
let three = write_records(&[b"a", b"b", b"c"], WriterOptions::new().chunk_size(1));
let two = write_records(&[b"a", b"b"], WriterOptions::new().chunk_size(1));
assert_eq!(&three[..two.len()], &two[..]);
let mut data = three.clone();
data[two.len() - 1] ^= 0xFF; // corrupt chunk "b"'s final data byte only
let chunk_b_begin = one.len() as u64;
let chunk_c_begin = two.len() as u64;
let regions: Rc<RefCell<Vec<crate::SkippedRegion>>> = Rc::new(RefCell::new(Vec::new()));
let rc = Rc::clone(®ions);
let opts = ReaderOptions::new().recovery(move |region| {
rc.borrow_mut().push(region.clone());
true
});
let mut reader = RecordReader::new(Cursor::new(data), opts).expect("reader new ok");
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(&b"a"[..])
);
// "b" is skipped; "c" — a sibling in the same block — is recovered.
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(&b"c"[..]),
"sibling after the bad chunk must be recovered"
);
assert_eq!(reader.read_record().expect("read ok"), None);
// last_record_is_valid is per-record: "c" came from a valid chunk,
// so the flag is true again after it returns.
assert!(reader.last_record_is_valid());
let regions = regions.borrow();
assert_eq!(regions.len(), 1);
assert_eq!(
regions[0].begin(),
chunk_b_begin,
"region begins at the bad chunk"
);
assert_eq!(
regions[0].end(),
chunk_c_begin,
"region ends exactly where the next chunk begins (the resync position)"
);
assert!(regions[0].message().contains("hash mismatch"));
}
/// When the chunk HEADER is hash-invalid, its claims cannot be trusted
/// to compute an extent — the region ends at the next block boundary.
#[test]
fn recovery_resyncs_to_boundary_when_header_is_invalid() {
let one = write_records(&[b"first"], WriterOptions::new().chunk_size(1));
let two = write_records(&[b"first", b"second"], WriterOptions::new().chunk_size(1));
let mut data = two.clone();
data[one.len()] ^= 0xFF; // corrupt chunk 2's HEADER hash
let data_len = data.len() as u64;
let regions: Rc<RefCell<Vec<crate::SkippedRegion>>> = Rc::new(RefCell::new(Vec::new()));
let rc = Rc::clone(®ions);
let opts = ReaderOptions::new().recovery(move |region| {
rc.borrow_mut().push(region.clone());
true
});
let mut reader = RecordReader::new(Cursor::new(data), opts).expect("reader new ok");
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(&b"first"[..])
);
assert_eq!(
reader.read_record().expect("read ok"),
None,
"rest of block skipped"
);
let regions = regions.borrow();
assert_eq!(regions.len(), 1);
assert_eq!(regions[0].begin(), one.len() as u64);
// Boundary-class, clamped to the stream length (the file ends well
// before the next 64 KiB boundary; a region cannot extend past the
// file — C++ reports EOF-ended regions the same way).
assert_eq!(
regions[0].end(),
data_len,
"boundary resync clamps to EOF for untrusted claims"
);
}
/// Classification must happen at FAILURE time, not recovery time: a
/// stream that grows between the two (every stream-length probe is a
/// growth opportunity) must not reclassify an untrusted failure as
/// trusted — that mislabeled a readable chunk as a precisely-skipped
/// region. With failure-time classification the claim failure stays
/// untrusted and the resync is the boundary, never a chunk_end computed
/// from claims that were unvalidatable when the error happened.
#[test]
fn recovery_classifies_at_failure_time_not_recovery_time() {
struct GrowOnNthEndSeek {
full: Vec<u8>,
visible: usize,
pos: u64,
end_seeks: u32,
grow_at: u32,
}
impl std::io::Read for GrowOnNthEndSeek {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let avail = &self.full[..self.visible];
let start = (self.pos as usize).min(avail.len());
let n = buf.len().min(avail.len() - start);
buf[..n].copy_from_slice(&avail[start..start + n]);
self.pos += n as u64;
Ok(n)
}
}
impl std::io::Seek for GrowOnNthEndSeek {
fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
match pos {
std::io::SeekFrom::Start(p) => self.pos = p,
std::io::SeekFrom::End(off) => {
self.end_seeks += 1;
if self.end_seeks >= self.grow_at {
self.visible = self.full.len();
}
self.pos = (self.visible as i64 + off).max(0) as u64;
}
std::io::SeekFrom::Current(off) => {
self.pos = (self.pos as i64 + off).max(0) as u64;
}
}
Ok(self.pos)
}
}
let one = write_records(&[b"first"], WriterOptions::new().chunk_size(1));
let full = write_records(&[b"first", b"second"], WriterOptions::new().chunk_size(1));
let cut = one.len() + 40; // chunk B header readable, data truncated
let regions: Rc<RefCell<Vec<crate::SkippedRegion>>> = Rc::new(RefCell::new(Vec::new()));
let rc = Rc::clone(®ions);
// Grow generously late so construction-time probes don't trigger it,
// but any recovery-time re-read (the bug) would.
for grow_at in [3u32, 4, 5] {
regions.borrow_mut().clear();
let rc2 = Rc::clone(&rc);
let reader_src = GrowOnNthEndSeek {
full: full.clone(),
visible: cut,
pos: 0,
end_seeks: 0,
grow_at,
};
let opts = ReaderOptions::new().recovery(move |region| {
rc2.borrow_mut().push(region.clone());
true
});
let mut reader = RecordReader::new(reader_src, opts).expect("reader new ok");
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(&b"first"[..])
);
let _ = reader.read_record(); // recovery fires on the truncated chunk
for region in regions.borrow().iter() {
assert!(
region.end() == BLOCK_SIZE || region.end() <= cut as u64 + BLOCK_SIZE,
"grow_at={grow_at}: untrusted failure must resync at a boundary"
);
assert_ne!(
region.end(),
full.len() as u64,
"grow_at={grow_at}: region end must not be a chunk_end derived \
from claims that were unvalidatable at failure time"
);
}
}
}
/// A long run of corrupt-data chunks must produce one callback per
/// chunk with contiguous, exactly-one-chunk regions (the coupling
/// invariant universally, not just for a single chunk), terminating at
/// EOF — no loop, no boundary fallback for trusted failures.
#[test]
fn recovery_walks_corrupt_chain_with_contiguous_regions() {
const N: usize = 200; // corrupt chunks between two good ones
// Record k..=N+1 prefix files give every chunk span.
let mut lens = Vec::with_capacity(N + 3);
let mut recs: Vec<Vec<u8>> = Vec::new();
for k in 0..(N + 2) {
recs.push(format!("r{k:03}").into_bytes());
let refs: Vec<&[u8]> = recs.iter().map(|r| r.as_slice()).collect();
lens.push(write_records(&refs, WriterOptions::new().chunk_size(1)).len());
}
let refs: Vec<&[u8]> = recs.iter().map(|r| r.as_slice()).collect();
let mut data = write_records(&refs, WriterOptions::new().chunk_size(1));
assert!(
data.len() < BLOCK_SIZE as usize,
"keep it single-block for span math"
);
// Corrupt the final data byte of chunks 1..=N (leave first and last).
for k in 1..=N {
let idx = lens[k] - 1;
data[idx] ^= 0xFF;
}
let regions: Rc<RefCell<Vec<crate::SkippedRegion>>> = Rc::new(RefCell::new(Vec::new()));
let rc = Rc::clone(®ions);
let opts = ReaderOptions::new().recovery(move |region| {
rc.borrow_mut().push(region.clone());
true
});
let mut reader = RecordReader::new(Cursor::new(data), opts).expect("reader new ok");
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(&b"r000"[..])
);
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(format!("r{:03}", N + 1).as_bytes()),
"the good chunk after the corrupt run must be reached"
);
assert_eq!(reader.read_record().expect("read ok"), None);
let regions = regions.borrow();
assert_eq!(regions.len(), N, "exactly one callback per corrupt chunk");
for (i, region) in regions.iter().enumerate() {
let k = i + 1;
assert_eq!(region.begin(), lens[k - 1] as u64, "chunk {k} begin");
assert_eq!(region.end(), lens[k] as u64, "chunk {k} end == next begin");
}
}
/// A corrupt 24-byte block-header tail at an exact block boundary: the
/// reader's raw position (boundary+24) canonicalizes 24 bytes BACK, and
/// the EOF-clamped region end equals the raw position — forward
/// progress must be measured from the raw position or the reader spins
/// forever re-reporting the same region (CWE-835, found in review).
#[test]
fn recovery_terminates_on_corrupt_block_header_tail_at_boundary() {
// Fill block 0 exactly: a padded file of BLOCK_SIZE bytes, then a
// corrupt 24-byte pseudo block header.
let mut data = write_records(&[b"a"], WriterOptions::new().final_padding(BLOCK_SIZE));
assert_eq!(data.len() as u64, BLOCK_SIZE);
data.extend_from_slice(&[0xFFu8; 24]); // hash-invalid block header
assert_eq!(data.len() as u64, BLOCK_SIZE + 24);
let regions: Rc<RefCell<Vec<crate::SkippedRegion>>> = Rc::new(RefCell::new(Vec::new()));
let rr = Rc::clone(®ions);
let opts = ReaderOptions::new().recovery(move |r| {
rr.borrow_mut().push(r.clone());
true
});
let mut reader = RecordReader::new(Cursor::new(data), opts).expect("reader new ok");
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(&b"a"[..])
);
// Must terminate (the old clamp spun forever here).
assert_eq!(reader.read_record().expect("read ok"), None, "clean EOF");
let regions = regions.borrow();
assert_eq!(
regions.len(),
1,
"exactly ONE region — the alias-end double-report is repaired (got {regions:?})"
);
assert_eq!(regions[0].begin(), BLOCK_SIZE, "canonical begin");
assert_eq!(
regions[0].end(),
BLOCK_SIZE + 25,
"minimal progress margin: one byte past the 24-byte corrupt tail"
);
}
/// Metadata reads promise not to move the read position — recovery
/// firing inside one must be report-only. Repositioning here rewound a
/// mid-stream reader to the region end (replaying records) or skipped
/// block-0 chunks at the start.
#[test]
fn metadata_recovery_does_not_move_the_read_position() {
// [sig][chunkA@64][padding to 64 KiB][fileB: sig][chunkB "b"]
let mut data = write_records(&[b"a"], WriterOptions::new().final_padding(BLOCK_SIZE));
assert_eq!(data.len() as u64, BLOCK_SIZE);
data.extend_from_slice(&write_records(&[b"b"], WriterOptions::new()));
data[64] ^= 0xFF; // corrupt chunk A's header (the metadata position)
let count: Rc<RefCell<u32>> = Rc::new(RefCell::new(0));
let rc = Rc::clone(&count);
let opts = ReaderOptions::new().recovery(move |_r| {
*rc.borrow_mut() += 1;
true
});
let mut reader = RecordReader::new(Cursor::new(data), opts).expect("reader new ok");
let pos_before = reader.pos();
assert_eq!(
reader.read_serialized_metadata().expect("metadata ok"),
None,
"skipped region reads as absent metadata"
);
assert_eq!(*count.borrow(), 1, "callback reported the region");
assert_eq!(
reader.pos(),
pos_before,
"metadata read must not move the read position"
);
// The record stream is undisturbed: reading proceeds from the start,
// recovers past the corrupt chunk normally, and reaches "b".
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(&b"b"[..])
);
assert_eq!(*count.borrow(), 2, "read-path recovery fired separately");
}
/// Seek with recovery: C++ `Seek` returns the result of the recovery
/// function — on `true` the reader is positioned past the region.
#[test]
fn seek_recovers_past_invalid_region() {
let one = write_records(&[b"a"], WriterOptions::new().chunk_size(1));
let three = write_records(&[b"a", b"b", b"c"], WriterOptions::new().chunk_size(1));
let two = write_records(&[b"a", b"b"], WriterOptions::new().chunk_size(1));
let mut data = three.clone();
data[two.len() - 1] ^= 0xFF; // corrupt chunk "b"'s final data byte
let opts = ReaderOptions::new().recovery(|_region| true);
let mut reader = RecordReader::new(Cursor::new(data), opts).expect("reader new ok");
reader
.seek(RecordPosition::new(one.len() as u64, 0))
.expect("seek with recovery must succeed");
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(&b"c"[..]),
"positioned past the skipped region"
);
let _ = two;
}
/// Search with recovery skips invalid regions during the scan (C++
/// contract) and still finds targets beyond them.
#[test]
fn search_recovers_past_invalid_region() {
let one = write_records(&[b"a"], WriterOptions::new().chunk_size(1));
let three = write_records(&[b"a", b"b", b"c"], WriterOptions::new().chunk_size(1));
let mut data = three;
data[one.len()] ^= 0xFF; // corrupt chunk "b"'s HEADER (scan-visible)
let opts = ReaderOptions::new().recovery(|_region| true);
let mut reader = RecordReader::new(Cursor::new(data), opts).expect("reader new ok");
let found = reader.search(|rec| rec.cmp(&b"c"[..])).expect("search ok");
// "c" sits in the region skipped by the boundary resync? No: the
// corrupt header forces a boundary skip during the scan, and "c"
// lives below the boundary too, so the honest outcome is NOT
// FOUND without error — the scan completed, the region was
// skipped, and the target was inside it.
assert!(
!found,
"target inside the skipped region is reported absent"
);
// A target in an intact chunk before the corruption is still found.
let mut reader2 = {
let one = write_records(&[b"a"], WriterOptions::new().chunk_size(1));
let three = write_records(&[b"a", b"b", b"c"], WriterOptions::new().chunk_size(1));
let mut data = three;
data[one.len()] ^= 0xFF;
RecordReader::new(
Cursor::new(data),
ReaderOptions::new().recovery(|_region| true),
)
.expect("reader new ok")
};
assert!(reader2.search(|rec| rec.cmp(&b"a"[..])).expect("search ok"));
}
/// Seeking to a non-data chunk's address (here: the signature chunk of
/// a concatenated second file) must scan forward to the next data chunk
/// — it used to wedge the reader at EOF, hiding all following records.
#[test]
fn seek_to_non_data_chunk_scans_forward() {
let mut data = write_records(&[b"a"], WriterOptions::new().final_padding(BLOCK_SIZE));
assert_eq!(data.len() as u64, BLOCK_SIZE);
let second = write_records(&[b"b"], WriterOptions::new());
data.extend_from_slice(&second);
let mut reader =
RecordReader::new(Cursor::new(data), ReaderOptions::new()).expect("reader new ok");
// The concatenated file's signature chunk is addressed at the block
// boundary — a non-data chunk.
reader
.seek(RecordPosition::new(BLOCK_SIZE, 0))
.expect("seek ok");
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(&b"b"[..]),
"seek to a non-data chunk must land on the next data chunk, not EOF"
);
}
/// A long run of consecutive tiny padding chunks must be scanned
/// iteratively: the forward scan over non-data chunks used to recurse
/// once per chunk, and a crafted ~2 MB padding run overflowed the
/// stack (SIGABRT). With the loop this completes in milliseconds.
#[test]
fn seek_across_long_padding_run_does_not_overflow_stack() {
fn padding_run_file(n: usize) -> Vec<u8> {
let mut file = write_records(&[], WriterOptions::new());
assert_eq!(file.len() as u64, BLOCK_HEADER_SIZE + CHUNK_HEADER_SIZE);
let header =
crate::chunk_header::ChunkHeader::from_parts(&[], ChunkType::Padding, 0, 0)
.to_bytes();
for _ in 0..n {
let chunk_begin =
crate::block_arithmetic::canonical_chunk_address(file.len() as u64);
let chunk_end = crate::block_arithmetic::chunk_end(chunk_begin, 0, 0);
let mut i = 0usize;
while i < header.len() {
let pos = file.len() as u64;
if pos % BLOCK_SIZE == 0 {
let bh = crate::block_header::BlockHeader::from_parts(
pos - chunk_begin,
chunk_end - pos,
);
file.extend_from_slice(&bh.to_bytes());
continue;
}
let until = (BLOCK_SIZE - pos % BLOCK_SIZE) as usize;
let take = until.min(header.len() - i);
file.extend_from_slice(&header[i..i + take]);
i += take;
}
}
file
}
let data = padding_run_file(50_000);
let mut reader =
RecordReader::new(Cursor::new(data), ReaderOptions::new()).expect("reader new ok");
reader
.seek(RecordPosition::new(
BLOCK_HEADER_SIZE + CHUNK_HEADER_SIZE,
0,
))
.expect("seek across the padding run must not crash");
assert_eq!(reader.read_record().expect("read ok"), None, "clean EOF");
}
/// Zero-record data chunks must not serve as binary-search pivots:
/// probing one falls through to the NEXT chunk's record, corrupting
/// pivot decisions (a present target could be reported absent).
#[test]
fn search_skips_zero_record_chunks() {
/// A valid empty Simple chunk: zero records, uncompressed, empty
/// sizes section.
fn empty_simple_chunk() -> Vec<u8> {
let data = [0x00u8, 0x00]; // compression none, sizes_byte_len 0
let data_hash = crate::hash::highway_hash_64(&data);
let chunk_type_and_num_records: u64 = ChunkType::Simple as u8 as u64; // 0 records
let mut body = [0u8; 32];
body[0..8].copy_from_slice(&(data.len() as u64).to_le_bytes());
body[8..16].copy_from_slice(&data_hash.to_le_bytes());
body[16..24].copy_from_slice(&chunk_type_and_num_records.to_le_bytes());
body[24..32].copy_from_slice(&0u64.to_le_bytes());
let header_hash = crate::hash::highway_hash_64(&body);
let mut out = Vec::with_capacity(42);
out.extend_from_slice(&header_hash.to_le_bytes());
out.extend_from_slice(&body);
out.extend_from_slice(&data);
out
}
// Convergence layout: the target lives in the LAST real chunk and a
// trailing empty chunk becomes the final pivot — probing it at EOF
// yields an empty record, the comparator steers into the empty
// chunk, and the within-chunk search reports the target absent.
let mut data = write_records(&[b"a", b"m"], WriterOptions::new());
data.extend_from_slice(&empty_simple_chunk());
let mut reader =
RecordReader::new(Cursor::new(data), ReaderOptions::new()).expect("reader new ok");
let found = reader.search(|rec| rec.cmp(&b"m"[..])).expect("search ok");
assert!(
found,
"present record must be found despite an empty pivot chunk"
);
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(&b"m"[..])
);
}
/// seek() must not clobber last_pos — it tracks the last successfully
/// READ record, and seek_back() is documented to return there.
#[test]
fn seek_back_returns_to_last_read_record_not_seek_target() {
let data = write_records(&[b"first", b"second"], WriterOptions::new().chunk_size(1));
let mut reader =
RecordReader::new(Cursor::new(data), ReaderOptions::new()).expect("reader new ok");
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(&b"first"[..])
);
let second_pos = {
// Find the second record's position by reading it, then rewind.
let p = reader.pos();
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(&b"second"[..])
);
let sp = reader.last_pos();
// Restore: last read should again be "first" for the real test.
reader.seek(p).expect("seek ok");
sp
};
// Fresh reader: read "first", then seek AWAY to second's position
// without reading, then seek_back — must land on "first".
let data2 = write_records(&[b"first", b"second"], WriterOptions::new().chunk_size(1));
let mut reader2 =
RecordReader::new(Cursor::new(data2), ReaderOptions::new()).expect("reader new ok");
assert_eq!(
reader2.read_record().expect("read ok").as_deref(),
Some(&b"first"[..])
);
reader2.seek(second_pos).expect("seek ok");
assert!(reader2.seek_back().expect("seek_back ok"));
assert_eq!(
reader2.read_record().expect("read ok").as_deref(),
Some(&b"first"[..]),
"seek_back must return to the last READ record, not the last seek target"
);
}
/// Decoder-construction failures (malformed chunk interior behind valid
/// hashes) must be persistent like every other read error: a bare retry
/// must re-hit the same chunk, not silently resume at the next one.
/// Layout: [chunk "first"][valid-hash chunk with hostile interior]
/// [chunk "second"].
#[test]
fn decoder_construction_error_is_persistent() {
// A Simple chunk whose hashes are valid but whose single data byte
// is an unknown compression type — SimpleChunkDecoder::new fails.
fn hostile_interior_chunk() -> Vec<u8> {
let data = [0xFFu8]; // unknown compression byte
let data_hash = crate::hash::highway_hash_64(&data);
let chunk_type_and_num_records: u64 = (1u64 << 8) | ChunkType::Simple as u8 as u64;
let decoded_data_size: u64 = 0;
let mut body = [0u8; 32];
body[0..8].copy_from_slice(&(data.len() as u64).to_le_bytes());
body[8..16].copy_from_slice(&data_hash.to_le_bytes());
body[16..24].copy_from_slice(&chunk_type_and_num_records.to_le_bytes());
body[24..32].copy_from_slice(&decoded_data_size.to_le_bytes());
let header_hash = crate::hash::highway_hash_64(&body);
let mut out = Vec::with_capacity(41);
out.extend_from_slice(&header_hash.to_le_bytes());
out.extend_from_slice(&body);
out.extend_from_slice(&data);
out
}
let one = write_records(&[b"first"], WriterOptions::new().chunk_size(1));
let two = write_records(&[b"first", b"second"], WriterOptions::new().chunk_size(1));
assert_eq!(&two[..one.len()], &one[..]);
let second_chunk = &two[one.len()..];
let mut data = one.clone();
data.extend_from_slice(&hostile_interior_chunk());
data.extend_from_slice(second_chunk);
assert!(data.len() < 65536);
let mut reader =
RecordReader::new(Cursor::new(data), ReaderOptions::new()).expect("reader new ok");
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(&b"first"[..])
);
assert!(
reader.read_record().is_err(),
"construction failure must error"
);
for attempt in 0..3 {
match reader.read_record() {
Err(_) => {}
Ok(rec) => panic!(
"attempt {attempt}: construction error was not persistent; got {:?}",
rec.as_deref()
.map(|r| String::from_utf8_lossy(r).into_owned())
),
}
}
}
/// A corrupt chunk header where the metadata chunk would live must
/// surface as an error from the metadata APIs, not as "no metadata" —
/// a caller inspecting metadata first must not conclude the file is
/// clean.
#[test]
fn metadata_peek_propagates_corruption() {
let mut data = write_records(&[b"only"], WriterOptions::new());
data[64] ^= 0xFF; // corrupt the first chunk header after the signature
let mut reader =
RecordReader::new(Cursor::new(data), ReaderOptions::new()).expect("reader new ok");
assert!(
reader.read_serialized_metadata().is_err(),
"corruption at the metadata position must not read as absent metadata"
);
// Sanity: an intact file without a metadata chunk still reports None.
let clean = write_records(&[b"only"], WriterOptions::new());
let mut reader =
RecordReader::new(Cursor::new(clean), ReaderOptions::new()).expect("reader new ok");
assert!(reader.read_serialized_metadata().expect("ok").is_none());
}
/// The signature chunk is a fixed constant; a hash-valid signature
/// header with nonzero claimed sizes must be rejected by the exact
/// byte comparison — trusting its data_size used to overflow the
/// position arithmetic (debug panic) or seek backward through the
/// i64 cast (release).
#[test]
fn hostile_signature_chunk_claims_are_rejected() {
fn hostile_signature(data_size: u64) -> Vec<u8> {
let data_hash = crate::hash::highway_hash_64(&[]);
let chunk_type_and_num_records: u64 = ChunkType::FileSignature as u8 as u64;
let decoded_data_size: u64 = 0;
let mut body = [0u8; 32];
body[0..8].copy_from_slice(&data_size.to_le_bytes());
body[8..16].copy_from_slice(&data_hash.to_le_bytes());
body[16..24].copy_from_slice(&chunk_type_and_num_records.to_le_bytes());
body[24..32].copy_from_slice(&decoded_data_size.to_le_bytes());
let header_hash = crate::hash::highway_hash_64(&body);
let mut out = Vec::with_capacity(40);
out.extend_from_slice(&header_hash.to_le_bytes());
out.extend_from_slice(&body);
out
}
let valid = write_records(&[b"x"], WriterOptions::new());
for data_size in [u64::MAX, 1000u64] {
let mut data = valid[..24].to_vec(); // keep the valid block header
data.extend_from_slice(&hostile_signature(data_size));
let err = RecordReader::new(Cursor::new(data), ReaderOptions::new())
.err()
.expect("hostile signature chunk must be rejected");
assert!(
err.to_string().contains("file signature"),
"data_size={data_size}: unexpected error: {err}"
);
}
// Sanity: a writer-produced file still opens (its signature IS the
// canonical constant).
RecordReader::new(Cursor::new(valid), ReaderOptions::new()).expect("valid file opens");
}
/// A reader over a file that grows between reads is supported: hitting
/// a chunk whose claims exceed the current length re-measures the
/// stream, and a read after the file has grown must succeed. The
/// re-measure seeks to the end, so the subsequent data read must
/// position itself explicitly rather than assume the header read left
/// the reader at the data start.
#[test]
fn growing_file_read_resumes_after_remeasure() {
struct SharedReader(Rc<RefCell<Cursor<Vec<u8>>>>);
impl std::io::Read for SharedReader {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
self.0.borrow_mut().read(buf)
}
}
impl std::io::Seek for SharedReader {
fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> {
self.0.borrow_mut().seek(pos)
}
}
use std::io::Read as _;
let one = write_records(&[b"first"], WriterOptions::new().chunk_size(1));
let full = write_records(&[b"first", b"second"], WriterOptions::new().chunk_size(1));
assert_eq!(&full[..one.len()], &one[..]);
// Truncate right after the second chunk's 40-byte header: the header
// parses, but its claimed data extends past the current end.
let cut = one.len() + 40;
assert!(cut < full.len());
let shared = Rc::new(RefCell::new(Cursor::new(full[..cut].to_vec())));
let mut reader = RecordReader::new(SharedReader(Rc::clone(&shared)), ReaderOptions::new())
.expect("reader new ok");
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(&b"first"[..])
);
// Before the file grows, the claim exceeds the stream even after
// re-measurement: clean error.
assert!(reader.read_record().is_err(), "truncated read must error");
// Grow the underlying file to its full contents (position preserved)
// and retry: the re-measure must accept it AND the data read must
// land on the chunk, not wherever the re-measure seek ended up.
{
let mut c = shared.borrow_mut();
let pos = c.position();
*c = Cursor::new(full.clone());
c.set_position(pos);
}
assert_eq!(
reader
.read_record()
.expect("read after growth ok")
.as_deref(),
Some(&b"second"[..]),
"growing-file read must resume correctly after re-measure"
);
assert_eq!(reader.read_record().expect("read ok"), None);
}
/// A hash-INVALID header's claims are just as hostile as a hash-valid
/// one's: seek scans peek headers without reporting hash errors inline,
/// and must not feed unvalidated claims into chunk-end arithmetic (a
/// u64::MAX claim drives an O(claim) overhead walk — an effective hang).
#[test]
fn seek_numeric_rejects_hash_invalid_header_claims() {
let base = write_records(&[b"only"], WriterOptions::new());
let mut hostile = hostile_simple_chunk(u64::MAX, 1);
hostile[0] ^= 0xFF; // corrupt the header hash
let mut data = base.clone();
data.extend_from_slice(&hostile);
let mut reader =
RecordReader::new(Cursor::new(data), ReaderOptions::new()).expect("reader new ok");
// Without the peek validity check this call never returns (the
// overhead walk runs ~2^48 iterations); with it, a clean error.
let err = reader
.seek_numeric(u64::MAX / 2)
.expect_err("corrupt header in scan path must error");
assert!(
err.to_string().contains("invalid chunk header hash"),
"unexpected error: {err}"
);
}
/// Header-claimed sizes beyond the physical stream must produce a clean,
/// persistent error — no arithmetic overflow (debug panic / release
/// wrap), no claim-sized allocation, no O(claim) overhead walk. Sweeps
/// the overflow, mid-range, and barely-past-EOF regimes for both the
/// data-size and record-count claims.
#[test]
fn hostile_header_claims_are_rejected() {
let base = write_records(&[b"only"], WriterOptions::new());
for (data_size, num_records) in [
(u64::MAX, 1u64), // overflow regime (wraps without saturation)
(u64::MAX - 40, 1), // offset overflow variant
(1u64 << 40, 1), // 1 TiB claim: allocation / walk regime
(4096, 1), // modest claim, still past EOF
(0, u64::MAX >> 8), // maximal record-count claim
(0, 1u64 << 40), // mid-range record-count claim
] {
let mut data = base.clone();
data.extend_from_slice(&hostile_simple_chunk(data_size, num_records));
let mut reader =
RecordReader::new(Cursor::new(data), ReaderOptions::new()).expect("reader new ok");
assert_eq!(
reader.read_record().expect("read ok").as_deref(),
Some(&b"only"[..]),
"data_size={data_size} num_records={num_records}"
);
let err = reader
.read_record()
.expect_err("hostile claim must be rejected");
assert!(
err.to_string().contains("claims"),
"data_size={data_size} num_records={num_records}: unexpected error: {err}"
);
// The rejection must be persistent, like every other read error.
assert!(
reader.read_record().is_err(),
"data_size={data_size} num_records={num_records}: error not persistent"
);
}
}
}