binlog-inspect 0.6.0

Offline inspector for plaintext MySQL binary logs with JSON Lines, text and CSV output
Documentation
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
// Copyright (c) 2026 houseme
// SPDX-License-Identifier: Apache-2.0

//! Convert mysql_common row values without exposing its types in the public event model.
//!
//! This module also pre-validates hostile table map optional metadata before it
//! reaches the underlying library: the library allocates variant vectors from
//! declared counts without bounding them, which aborts the process on crafted
//! input. The manual TLV walk in [`validate_table_map_metadata`] mirrors the
//! library's wire format exactly so no valid table map is rejected.
use crate::event::{CellValue, ColumnValue, RowImage, Signedness};
use mysql_common::{
    binlog::{events::TableMapEvent, jsonb, row::BinlogRow, value::BinlogValue},
    constants::ColumnType,
    proto::MySerialize,
    value::Value,
};
use std::io;

/// Optional metadata field types (TLV type codes) accepted by the
/// pre-validation walk.
///
/// The underlying library only knows the types 1..=13; the partial-JSON types
/// 14 and 15 (WL#2955) are accepted here and stripped from the event body
/// before the library parses the table map, because its metadata iterator
/// rejects unknown types. Anything else is rejected.
const KNOWN_TLV_TYPES: [u8; 15] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
/// `SIGNEDNESS`: unsigned flags of numeric columns (MSB-first bitmap).
const TLV_SIGNEDNESS: u8 = 1;
/// `SET_STR_VALUE`: variant names of SET columns.
const TLV_SET_STR_VALUE: u8 = 5;
/// `ENUM_STR_VALUE`: variant names of ENUM columns.
const TLV_ENUM_STR_VALUE: u8 = 6;
/// `PARTIAL_JSON_UPDATE`: bitmap of columns logged as partial JSON updates.
const TLV_PARTIAL_JSON_UPDATE: u8 = 14;
/// `UPDATED_COLUMN_TYPES`: actual data types of updated columns.
const TLV_UPDATED_COLUMN_TYPES: u8 = 15;

/// Lowercase hex digits used for byte rendering.
const HEX: [u8; 16] = *b"0123456789abcdef";

/// Maximum jsonb nesting depth accepted by the conversion.
///
/// MySQL's JSON implementation rejects documents nested deeper than 100
/// levels, so values written by a real server never exceed this; anything
/// deeper is hostile input. The cap also bounds the recursion of
/// [`jsonb_to_serde`], so over-deep input returns a clean `InvalidData`
/// error instead of overflowing the stack.
const MAX_JSONB_DEPTH: usize = 100;

/// Default budget on the serde nodes a single jsonb conversion may produce.
///
/// The jsonb format stores a child offset per container entry, so crafted
/// containers can point every entry at the same child subtree and amplify a
/// linear-size wire value into an exponentially larger decoded tree: the
/// reproduction that motivated this budget decodes 221 wire bytes (22 aliased
/// small-array containers) into 8,388,607 serde nodes, and a ~331-byte chain
/// demands ~275 GB before the [`MAX_JSONB_DEPTH`] cap can fire. Every node
/// the conversion produces — scalar or container, containers counting
/// themselves plus, through the recursion, their children — is charged one
/// against the counter threaded through [`jsonb_to_serde`]; the counter is
/// monotone across the whole conversion, so aliasing cannot reset it.
/// Exceeding the budget returns a clean `InvalidData` error instead of
/// allocating towards an allocator abort (R48).
///
/// Two budgets apply to every conversion, and both stay in force (R48 and
/// the R48-refinement):
///
/// - The per-value budget of [`DEFAULT_JSONB_NODE_BUDGET`] nodes bounds one
///   decoded value — one JSON cell of a row image, or the whole diff
///   sequence of one partial-JSON cell. It caps a single huge legitimate
///   document independently of the event carrying it.
/// - The per-event budget of [`EventNodeBudget`] is shared by every value of
///   one rows event (all rows, both images of an updated row): legitimate
///   jsonb is at most ~0.25 nodes per wire byte, so
///   `min(16 × event_size, 4_000_000)` leaves a 64× density headroom while
///   bounding a hostile event's decoded tree at ~128 MB. It closes the gap
///   the per-value budget leaves open: thousands of individually
///   budget-clean hostile columns in one event used to allocate linearly
///   with the wire size.
const DEFAULT_JSONB_NODE_BUDGET: usize = 1_000_000;

/// Absolute cap on the per-event jsonb node budget (R48-refinement).
///
/// [`EventNodeBudget`] scales the shared budget with the event's wire size,
/// so the absolute cap bounds the worst-case decoded tree of one event at
/// roughly 4M nodes (~128 MB of serde values) even for the largest event the
/// framing accepts.
const MAX_PER_EVENT_JSONB_NODES: usize = 4_000_000;

/// Wire-byte density factor of the per-event jsonb node budget
/// (R48-refinement): legitimate jsonb decodes to at most ~0.25 nodes per
/// wire byte, so 16 nodes per byte leaves a 64× headroom before
/// [`EventNodeBudget`] trips.
const PER_EVENT_NODE_DENSITY_FACTOR: u64 = 16;

/// The per-event shared jsonb node counter (R48-refinement).
///
/// One instance is created per rows event and threaded through every value
/// conversion of that event: all rows, and the before/after images of each
/// updated row, charge the same monotone counter. Aliasing cannot reset it,
/// and a hostile multi-column event trips the shared budget instead of
/// allocating once per column within each column's per-value budget.
pub(crate) struct EventNodeBudget {
    /// Nodes decoded so far in this event.
    nodes: usize,
    /// `min(PER_EVENT_NODE_DENSITY_FACTOR × event_size,
    /// MAX_PER_EVENT_JSONB_NODES)`.
    limit: usize,
}

impl EventNodeBudget {
    /// Creates the shared budget for one event of `event_size` wire bytes
    /// (the framed event size, header and checksum included).
    pub(crate) fn new(event_size: u32) -> Self {
        let limit =
            usize::try_from(u64::from(event_size).saturating_mul(PER_EVENT_NODE_DENSITY_FACTOR))
                .unwrap_or(usize::MAX)
                .min(MAX_PER_EVENT_JSONB_NODES);
        Self { nodes: 0, limit }
    }

    /// Charges one decoded node against the shared budget.
    fn charge(&mut self) -> io::Result<()> {
        self.nodes = self.nodes.saturating_add(1);
        if self.nodes > self.limit {
            return Err(jsonb_over_event_node_budget(self.limit));
        }
        Ok(())
    }
}

/// Converts a library jsonb value into the serde_json model.
///
/// Containers are converted here so the conversion recursion is capped at
/// [`MAX_JSONB_DEPTH`]; deeper (hostile) input returns `InvalidData` instead
/// of overflowing the stack. Every scalar — including the opaque values
/// MySQL writes for decimal literals and DECIMAL column values, and for
/// DATE/TIME/DATETIME values inside JSON — is converted through the
/// library's own [`jsonb::Value::parse`] rendering, whose documented rules
/// turn `NEWDECIMAL` opaques into their decimal text form,
/// DATE/TIME/DATETIME opaques into their text form, and every other opaque
/// into a `base64:type<id>:<data>` string. Legitimate data therefore never
/// fails the event.
///
/// `nodes` is the per-value decoded-node counter shared by the whole
/// conversion: every converted node charges one against it, and exceeding
/// [`DEFAULT_JSONB_NODE_BUDGET`] returns `InvalidData`. Each value
/// conversion entry point (the row-value path and the diff-value path) owns
/// one counter and must never reset it mid-conversion. `event` is the
/// per-event counter every entry point of one rows event shares
/// (R48-refinement); both budgets apply to every node.
fn jsonb_to_serde(
    value: &jsonb::Value<'_>,
    depth: usize,
    nodes: &mut usize,
    event: &mut EventNodeBudget,
) -> io::Result<serde_json::Value> {
    *nodes += 1;
    if *nodes > DEFAULT_JSONB_NODE_BUDGET {
        return Err(jsonb_over_node_budget());
    }
    event.charge()?;
    match value {
        jsonb::Value::SmallArray(container) => {
            if depth > MAX_JSONB_DEPTH {
                return Err(jsonb_too_deep());
            }
            // Untrusted count; the vector grows on demand.
            let mut items = Vec::with_capacity(container.element_count().min(4096) as usize);
            for item in container.iter() {
                items.push(jsonb_to_serde(&item?, depth + 1, nodes, event)?);
            }
            Ok(serde_json::Value::Array(items))
        }
        jsonb::Value::LargeArray(container) => {
            if depth > MAX_JSONB_DEPTH {
                return Err(jsonb_too_deep());
            }
            // Untrusted count; the vector grows on demand.
            let mut items = Vec::with_capacity(container.element_count().min(4096) as usize);
            for item in container.iter() {
                items.push(jsonb_to_serde(&item?, depth + 1, nodes, event)?);
            }
            Ok(serde_json::Value::Array(items))
        }
        jsonb::Value::SmallObject(container) => {
            if depth > MAX_JSONB_DEPTH {
                return Err(jsonb_too_deep());
            }
            let mut members = serde_json::Map::new();
            for entry in container.iter() {
                let (key, value) = entry?;
                members.insert(
                    key.value().into_owned(),
                    jsonb_to_serde(&value, depth + 1, nodes, event)?,
                );
            }
            Ok(serde_json::Value::Object(members))
        }
        jsonb::Value::LargeObject(container) => {
            if depth > MAX_JSONB_DEPTH {
                return Err(jsonb_too_deep());
            }
            let mut members = serde_json::Map::new();
            for entry in container.iter() {
                let (key, value) = entry?;
                members.insert(
                    key.value().into_owned(),
                    jsonb_to_serde(&value, depth + 1, nodes, event)?,
                );
            }
            Ok(serde_json::Value::Object(members))
        }
        // Scalars carry no recursion, so they are converted through the
        // library's own JsonDom rendering (including its opaque string rules).
        // NEWDECIMAL opaques get a hand validation first: the library's
        // packed-decimal reader trusts the header (and panics/fails open on
        // hostile shapes), so the crate owns the contract (R47b fuzz
        // findings + round-2 verification).
        jsonb::Value::Opaque(opaque)
            if opaque.value_type() == ColumnType::MYSQL_TYPE_NEWDECIMAL =>
        {
            let data = opaque.data_raw();
            validate_packed_decimal(data)?;
            Ok(serde_json::Value::from(
                jsonb::Value::Opaque(opaque.clone()).parse()?,
            ))
        }
        other => Ok(serde_json::Value::from(other.clone().parse()?)),
    }
}

/// Validates a packed binlog DECIMAL (the bytes after the two precision/scale
/// header bytes) before the library touches it.
///
/// The dependency's reader trusts `precision`/`scale`: `precision - scale`
/// underflows when scale is larger, a hostile digit group overflows its
/// `x * 10^(9 - digits)` scaling multiply, and a release build wraps that
/// overflow into silently wrong output. The layout mirrors the library's
/// `Decimal::read_bin`: a sign bit in the high bit of the first packed byte,
/// then the integer groups (partial-width prefix of `precision - scale % 9`
/// digits, then full 9-digit groups) and the fractional groups (full, then a
/// partial-width suffix of `scale % 9` digits), every group big-endian with
/// non-negative digit values below `10^digits` after the sign masks are
/// reapplied.
///
/// The check is shared by both hostile-decimal routes: the jsonb opaque
/// wrapper ([`validate_packed_decimal`], which carries the precision/scale
/// header inside the opaque data) and the NEWDECIMAL column-value route of
/// the row-layout walk ([`validate_column_packed_decimal`], which takes them
/// from the table-map metadata).
fn validate_packed_decimal(data: &[u8]) -> io::Result<()> {
    const INVALID: &str = "malformed jsonb opaque NEWDECIMAL";
    let Some(&precision) = data.first() else {
        return Err(io::Error::new(io::ErrorKind::InvalidData, INVALID));
    };
    let Some(&scale) = data.get(1) else {
        return Err(io::Error::new(io::ErrorKind::InvalidData, INVALID));
    };
    validate_packed_decimal_groups(precision, scale, &data[2..]).map_err(|reason| {
        io::Error::new(io::ErrorKind::InvalidData, format!("{INVALID}: {reason}"))
    })
}

/// Validates the packed digits of a NEWDECIMAL column value during the
/// row-layout walk, with the R50 column wording.
///
/// `precision` and `scale` come from the table-map metadata, which the
/// registration check keeps inside the server ranges; the packed digits are
/// exactly [`decimal_column_bin_size`] bytes, which the walk reads in full
/// before calling.
fn validate_column_packed_decimal(
    column: usize,
    precision: u8,
    scale: u8,
    packed: &[u8],
) -> io::Result<()> {
    validate_packed_decimal_groups(precision, scale, packed).map_err(|reason| {
        io::Error::new(
            io::ErrorKind::InvalidData,
            format!("malformed NEWDECIMAL value in column {column}: {reason}"),
        )
    })
}

/// Validates the packed digit groups of a binlog DECIMAL and returns the
/// plain reason on failure; the wrappers format it into their route-specific
/// message. See [`validate_packed_decimal`] for the layout.
fn validate_packed_decimal_groups(precision: u8, scale: u8, packed: &[u8]) -> Result<(), String> {
    const DIG_PER_DEC: usize = 9;
    const DIG_TO_BYTES: [usize; DIG_PER_DEC + 1] = [0, 1, 1, 2, 2, 3, 3, 4, 4, 4];
    const LENGTH: &str = "packed length does not match precision and scale";

    if scale > precision {
        return Err("scale exceeds precision".to_owned());
    }
    // The writer flips the high bit of the first packed byte; treat it as
    // flipped without copying the value (the library reads it the same way).
    let first_flipped = packed.first().map(|byte| byte ^ 0x80);
    let mask = match first_flipped {
        Some(byte) if byte & 0x80 != 0 => -1i32,
        _ => 0i32,
    };
    let intg = usize::from(precision) - usize::from(scale);
    // (digit count per group, group count): a partial-width prefix group of
    // `intg % 9` digits, full 9-digit groups, full fractional groups, and a
    // partial-width suffix of `scale % 9` digits.
    let groups: [(usize, usize); 4] = [
        (
            intg % DIG_PER_DEC,
            usize::from(!intg.is_multiple_of(DIG_PER_DEC)),
        ),
        (DIG_PER_DEC, intg / DIG_PER_DEC),
        (DIG_PER_DEC, usize::from(scale) / DIG_PER_DEC),
        (
            usize::from(scale) % DIG_PER_DEC,
            usize::from(!usize::from(scale).is_multiple_of(DIG_PER_DEC)),
        ),
    ];
    let mut offset = 0usize;
    for (digits, count) in groups {
        let width = DIG_TO_BYTES[digits];
        for _ in 0..count {
            if offset + width > packed.len() {
                return Err(LENGTH.to_owned());
            }
            // Byte accessor applying the first byte's sign flip in place, so
            // the walk never copies the packed value. The group is read at
            // `base` before the cursor advances past it.
            let base = offset;
            let at = |index: usize| -> u8 {
                if index == 0 {
                    first_flipped.unwrap_or(0)
                } else {
                    packed[index]
                }
            };
            // Sign-extend the written group (the writer stored
            // `digit ^ mask` in `width` bytes) and re-apply the mask: the
            // result is the non-negative digit value for valid input.
            let widened = match width {
                1 => i32::from(at(base) as i8),
                2 => i32::from(i16::from_be_bytes([at(base), at(base + 1)])),
                3 => {
                    let sign_extend = at(base) & 0x80 != 0;
                    let mut value = i32::from(at(base + 2))
                        | (i32::from(at(base + 1)) << 8)
                        | (i32::from(at(base)) << 16);
                    if sign_extend {
                        value |= !0 << 24;
                    }
                    value
                }
                4 => i32::from_be_bytes([at(base), at(base + 1), at(base + 2), at(base + 3)]),
                _ => 0,
            } ^ mask;
            offset += width;
            // `digits` is at most 9, so 10^digits always fits an i32.
            let limit = 10i32.pow(digits as u32);
            if widened < 0 || widened >= limit {
                return Err(format!("digit group exceeds {digits} digits"));
            }
        }
    }
    if offset != packed.len() {
        return Err(LENGTH.to_owned());
    }
    Ok(())
}

/// The rejection error for jsonb input nested deeper than [`MAX_JSONB_DEPTH`].
fn jsonb_too_deep() -> io::Error {
    io::Error::new(
        io::ErrorKind::InvalidData,
        format!("jsonb nesting depth exceeds the MySQL limit of {MAX_JSONB_DEPTH} levels"),
    )
}

/// The rejection error for jsonb input decoding beyond
/// [`DEFAULT_JSONB_NODE_BUDGET`].
fn jsonb_over_node_budget() -> io::Error {
    io::Error::new(
        io::ErrorKind::InvalidData,
        format!("jsonb decoding exceeds the node budget of {DEFAULT_JSONB_NODE_BUDGET} nodes"),
    )
}

/// The rejection error for one event's shared jsonb decoding beyond the
/// per-event budget (R48-refinement).
fn jsonb_over_event_node_budget(limit: usize) -> io::Error {
    io::Error::new(
        io::ErrorKind::InvalidData,
        format!("jsonb decoding in this event exceeds the per-event node budget of {limit} nodes"),
    )
}

pub(crate) fn decode_row(
    row: &BinlogRow,
    present: &[bool],
    signedness: &[Option<bool>],
    event_budget: &mut EventNodeBudget,
) -> io::Result<RowImage> {
    let mut offset = 0;
    let mut columns = Vec::with_capacity(present.len());
    for (index, &included) in present.iter().enumerate() {
        if !included {
            columns.push(ColumnValue {
                index,
                name: None,
                mysql_type: None,
                signedness: None,
                value: CellValue::Absent,
            });
            continue;
        }
        let column = row.columns_ref().get(offset).ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                "row bitmap and column count differ",
            )
        })?;
        let value = row
            .as_ref(offset)
            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "row value missing"))?;
        let value = match value {
            BinlogValue::Value(value) => match value {
                Value::NULL => CellValue::Null,
                Value::Bytes(bytes) => {
                    let mut encoded = String::with_capacity(bytes.len() * 2);
                    for byte in bytes {
                        encoded.push(HEX[usize::from(byte >> 4)] as char);
                        encoded.push(HEX[usize::from(byte & 0x0f)] as char);
                    }
                    CellValue::Bytes(encoded)
                }
                Value::Int(n) => CellValue::Int(*n),
                Value::UInt(n) => CellValue::UInt(*n),
                Value::Float(n) => CellValue::Float(n.to_string()),
                Value::Double(n) => CellValue::Double(n.to_string()),
                Value::Date(year, month, day, hour, minute, second, micros) => CellValue::Date {
                    year: *year,
                    month: *month,
                    day: *day,
                    hour: *hour,
                    minute: *minute,
                    second: *second,
                    micros: *micros,
                },
                Value::Time(negative, days, hours, minutes, seconds, micros) => CellValue::Time {
                    negative: *negative,
                    days: *days,
                    hours: *hours,
                    minutes: *minutes,
                    seconds: *seconds,
                    micros: *micros,
                },
            },
            BinlogValue::Jsonb(value) => {
                // The per-value counter is fresh per cell; the per-event
                // counter is shared by every cell of the event
                // (R48-refinement). A failure here aborts the row.
                let mut nodes = 0usize;
                CellValue::Json(jsonb_to_serde(value, 1, &mut nodes, event_budget)?)
            }
            BinlogValue::JsonDiff(diffs) => {
                // Without a paired before image (see [`decode_rows_pair`])
                // there is nothing to apply the diffs to; they are preserved
                // as a recorded loss instead of failing the whole image.
                let (diffs, problem) = json_diff::parse_diffs(diffs, event_budget);
                CellValue::PartialJson {
                    diffs,
                    reason: problem
                        .unwrap_or_else(|| "no before image to apply diffs against".to_owned()),
                }
            }
        };
        let mysql_type = column.column_type();
        let signedness = if mysql_type.is_numeric_type() {
            Some(match signedness.get(index).copied().flatten() {
                Some(true) => Signedness::Unsigned,
                Some(false) => Signedness::Signed,
                None => Signedness::Unknown,
            })
        } else {
            None
        };
        columns.push(ColumnValue {
            index,
            name: Some(column.name_str().into_owned()),
            mysql_type: Some(column_type_name(mysql_type).to_owned()),
            signedness,
            value,
        });
        offset += 1;
    }
    if offset != row.len() {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "row bitmap and value count differ",
        ));
    }
    Ok(RowImage { columns })
}

/// Decodes the before and after images of an updated row as a pair.
///
/// Partial-JSON after images (WL#2955) carry a sequence of binlog JSON diffs
/// per JSON column instead of a full document. Each such sequence is applied
/// to the before image's decoded value at the same column index: on success
/// the after cell becomes [`CellValue::Json`] with the full new document, on
/// any failure it becomes [`CellValue::PartialJson`] with the preserved diffs
/// and the reason. Images without diffs decode exactly like [`decode_row`].
///
/// Both images share the caller's per-event [`EventNodeBudget`]
/// (R48-refinement), so an update row cannot double its jsonb allocation by
/// decoding two images.
pub(crate) fn decode_rows_pair(
    before_row: Option<&BinlogRow>,
    after_row: Option<&BinlogRow>,
    before_present: Option<&[bool]>,
    after_present: Option<&[bool]>,
    signedness: &[Option<bool>],
    event_budget: &mut EventNodeBudget,
) -> io::Result<(Option<RowImage>, Option<RowImage>)> {
    let before = before_row
        .map(|row| {
            decode_row(
                row,
                before_present.ok_or_else(|| {
                    io::Error::new(io::ErrorKind::InvalidData, "missing before bitmap")
                })?,
                signedness,
                event_budget,
            )
        })
        .transpose()?;
    let mut after = after_row
        .map(|row| {
            decode_row(
                row,
                after_present.ok_or_else(|| {
                    io::Error::new(io::ErrorKind::InvalidData, "missing after bitmap")
                })?,
                signedness,
                event_budget,
            )
        })
        .transpose()?;
    if let (Some(before), Some(after)) = (&before, after.as_mut()) {
        apply_pair(before, after);
    }
    Ok((before, after))
}

/// Replaces the provisional [`CellValue::PartialJson`] cells of the after
/// image by applying their diffs against the before image's values.
///
/// Cells whose diffs were already resolved (or whose before counterpart
/// offers no JSON value to apply against) keep their state.
///
/// Both images are in table-column order, so the before cursor advances
/// monotonically — a linear merge instead of a per-cell search (R42).
fn apply_pair(before: &RowImage, after: &mut RowImage) {
    let mut cursor = before.columns.iter();
    let mut candidate = cursor.next();
    for after_cell in after.columns.iter_mut() {
        // Advance the before cursor to this column's index; both images are
        // sorted by column index, so the cursor never moves backwards.
        while candidate.is_some_and(|cell| cell.index < after_cell.index) {
            candidate = cursor.next();
        }
        if !matches!(after_cell.value, CellValue::PartialJson { .. }) {
            continue;
        }
        let CellValue::PartialJson { diffs, .. } = &after_cell.value else {
            continue;
        };
        // A gap (no before cell at this index) keeps the loss-marked value,
        // matching the previous search-based behavior; the clone happens only
        // once a before counterpart exists.
        let Some(before_cell) = candidate.filter(|cell| cell.index == after_cell.index) else {
            continue;
        };
        let diffs = diffs.clone();
        let replacement = match &before_cell.value {
            CellValue::Json(before_value) => {
                match json_diff::apply_json_diffs(before_value.clone(), &diffs) {
                    Ok(applied) => CellValue::Json(applied),
                    Err(error) => CellValue::PartialJson {
                        diffs,
                        reason: error.0,
                    },
                }
            }
            CellValue::Absent => CellValue::PartialJson {
                diffs,
                reason: "before column is absent".to_owned(),
            },
            _ => CellValue::PartialJson {
                diffs,
                reason: "before value is not JSON".to_owned(),
            },
        };
        after_cell.value = replacement;
    }
}

/// Collects the present-column bitmap, validating that it covers all `n` columns.
///
/// Bits beyond `n` are legitimate padding; fewer than `n` bits means a
/// truncated bitmap, which must not be silently accepted.
pub(crate) fn present_bits(bits: impl Iterator<Item = bool>, n: usize) -> io::Result<Vec<bool>> {
    let collected: Vec<bool> = bits.take(n).collect();
    if collected.len() != n {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "row bitmap length does not match column count",
        ));
    }
    Ok(collected)
}

/// Canonical stable name for a MySQL column type.
///
/// The library's `Debug` output must not leak into the output model, so every
/// [`ColumnType`] variant maps to its short constant name.
pub(crate) fn column_type_name(column_type: ColumnType) -> &'static str {
    match column_type {
        ColumnType::MYSQL_TYPE_DECIMAL => "DECIMAL",
        ColumnType::MYSQL_TYPE_TINY => "TINY",
        ColumnType::MYSQL_TYPE_SHORT => "SHORT",
        ColumnType::MYSQL_TYPE_LONG => "LONG",
        ColumnType::MYSQL_TYPE_FLOAT => "FLOAT",
        ColumnType::MYSQL_TYPE_DOUBLE => "DOUBLE",
        ColumnType::MYSQL_TYPE_NULL => "NULL",
        ColumnType::MYSQL_TYPE_TIMESTAMP => "TIMESTAMP",
        ColumnType::MYSQL_TYPE_LONGLONG => "LONGLONG",
        ColumnType::MYSQL_TYPE_INT24 => "INT24",
        ColumnType::MYSQL_TYPE_DATE => "DATE",
        ColumnType::MYSQL_TYPE_TIME => "TIME",
        ColumnType::MYSQL_TYPE_DATETIME => "DATETIME",
        ColumnType::MYSQL_TYPE_YEAR => "YEAR",
        ColumnType::MYSQL_TYPE_NEWDATE => "NEWDATE",
        ColumnType::MYSQL_TYPE_VARCHAR => "VARCHAR",
        ColumnType::MYSQL_TYPE_BIT => "BIT",
        ColumnType::MYSQL_TYPE_TIMESTAMP2 => "TIMESTAMP2",
        ColumnType::MYSQL_TYPE_DATETIME2 => "DATETIME2",
        ColumnType::MYSQL_TYPE_TIME2 => "TIME2",
        ColumnType::MYSQL_TYPE_TYPED_ARRAY => "TYPED_ARRAY",
        ColumnType::MYSQL_TYPE_VECTOR => "VECTOR",
        ColumnType::MYSQL_TYPE_UNKNOWN => "UNKNOWN",
        ColumnType::MYSQL_TYPE_JSON => "JSON",
        ColumnType::MYSQL_TYPE_NEWDECIMAL => "NEWDECIMAL",
        ColumnType::MYSQL_TYPE_ENUM => "ENUM",
        ColumnType::MYSQL_TYPE_SET => "SET",
        ColumnType::MYSQL_TYPE_TINY_BLOB => "TINY_BLOB",
        ColumnType::MYSQL_TYPE_MEDIUM_BLOB => "MEDIUM_BLOB",
        ColumnType::MYSQL_TYPE_LONG_BLOB => "LONG_BLOB",
        ColumnType::MYSQL_TYPE_BLOB => "BLOB",
        ColumnType::MYSQL_TYPE_VAR_STRING => "VAR_STRING",
        ColumnType::MYSQL_TYPE_STRING => "STRING",
        ColumnType::MYSQL_TYPE_GEOMETRY => "GEOMETRY",
    }
}

/// Extract signedness in table-column order; None means metadata is unavailable.
///
/// The SIGNEDNESS TLV is located with a direct walk of the re-serialized
/// table map body instead of the library's metadata extractor: the library's
/// iterator hard-errors on the partial-JSON metadata types (14, 15) that
/// partial-JSON binlogs carry, which would defeat their handling.
pub(crate) fn signedness(map: &TableMapEvent<'_>) -> io::Result<Vec<Option<bool>>> {
    validate_column_metadata(map)?;
    let bits = signedness_bits(map)?;
    let mut numeric = 0usize;
    let mut result = Vec::new();
    for index in 0..map.columns_count() as usize {
        let ty = map
            .get_column_type(index)
            .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?
            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "missing column type"))?;
        result.push(if ty.is_numeric_type() {
            let bit = bitmap_bit(bits.as_deref(), numeric);
            numeric += 1;
            bit
        } else {
            None
        });
    }
    Ok(result)
}

/// Validates the value ranges of one column's metadata (R50).
///
/// A NEWDECIMAL metadata pair outside the server ranges makes the
/// dependency's packed-size arithmetic underflow (`precision - scale` with
/// `scale > precision`: a debug panic, a release allocator abort), and a
/// fractional-second precision above six shifts every later column of a row
/// by bytes the dependency reads past its own declared widths. Both are
/// rejected here, at every table-map registration, before any row can
/// reference them. A column whose metadata cannot be located at all is left
/// to the truncated-metadata check below.
fn validate_column_metadata_values(
    index: usize,
    column_type: ColumnType,
    map: &TableMapEvent<'_>,
) -> io::Result<()> {
    let metadata = map.get_column_metadata(index).unwrap_or(&[]);
    match column_type {
        ColumnType::MYSQL_TYPE_NEWDECIMAL => {
            let (Some(&precision), Some(&scale)) = (metadata.first(), metadata.get(1)) else {
                return Ok(());
            };
            if scale > precision {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!(
                        "table map column metadata for column {index}: \
                         NEWDECIMAL scale {scale} exceeds precision {precision}"
                    ),
                ));
            }
            if !(1..=65).contains(&precision) {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!(
                        "table map column metadata for column {index}: \
                         NEWDECIMAL precision {precision} outside 1..=65"
                    ),
                ));
            }
            if scale > 30 {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!(
                        "table map column metadata for column {index}: \
                         NEWDECIMAL scale {scale} outside 0..=30"
                    ),
                ));
            }
        }
        ColumnType::MYSQL_TYPE_TIMESTAMP2
        | ColumnType::MYSQL_TYPE_DATETIME2
        | ColumnType::MYSQL_TYPE_TIME2 => {
            if let Some(&fsp) = metadata.first()
                && fsp > 6
            {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!(
                        "table map column metadata for column {index}: \
                         fractional-second precision {fsp} outside 0..=6"
                    ),
                ));
            }
        }
        _ => {}
    }
    Ok(())
}

/// Validate metadata before the dependency substitutes an empty slice for
/// missing metadata and indexes it during row decoding.
fn validate_column_metadata(map: &TableMapEvent<'_>) -> io::Result<()> {
    let count = usize::try_from(map.columns_count())
        .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "column count overflow"))?;
    for index in 0..count {
        let column_type = map
            .get_raw_column_type(index)
            .map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?
            .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "missing column type"))?;
        // mysql_common gives VAR_STRING zero metadata bytes but its value
        // decoder requires two. TYPED_ARRAY strips the element type before
        // its value decoder tries to read it again. Reject both explicitly.
        if matches!(
            column_type,
            ColumnType::MYSQL_TYPE_VAR_STRING | ColumnType::MYSQL_TYPE_TYPED_ARRAY
        ) {
            return Err(io::Error::new(
                io::ErrorKind::Unsupported,
                format!(
                    "unsupported binlog column type {}",
                    column_type_name(column_type)
                ),
            ));
        }
        validate_column_metadata_values(index, column_type, map)?;
    }
    // The getter walks every preceding column, so checking the last column
    // once validates the complete metadata span in linear time.
    if let Some(last) = count.checked_sub(1)
        && map.get_column_metadata(last).is_none()
    {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "table map column metadata is truncated",
        ));
    }
    Ok(())
}

/// One row image's layout as declared by the rows event (R50).
///
/// `bitmap_ones` counts the set bits of the image's column bitmap —
/// including any set bits beyond the declared column count, which still size
/// the row's null bitmap exactly as the library derives it — and `present`
/// holds the present flags of the declared columns in table order.
/// `partial_header` marks the after image of a partial update rows event,
/// which carries the WL#2955 value-options encoding before the null bitmap.
pub(crate) struct RowImageLayout<'a> {
    pub(crate) bitmap_ones: usize,
    pub(crate) present: &'a [bool],
    pub(crate) partial_header: bool,
}

/// The rejection error for row bytes that do not form a complete row image.
///
/// The library's row iterator parses rows until the data runs out; a
/// remainder that cannot cover the next image is rejected there with an
/// opaque end-of-stream error. The walk names the shape instead — without
/// rejecting anything the library accepts, because the library errors on the
/// same shortfall.
fn trailing_bytes_error() -> io::Error {
    io::Error::new(
        io::ErrorKind::InvalidData,
        "trailing bytes after row data in rows event",
    )
}

/// Prevalidates the row layout of a rows event before the library iterates
/// it (R50).
///
/// Per row, the walk skips the WL#2955 value-options header of a partial
/// after image, skips the null bitmap, and advances each present, non-null
/// column by the width the library's value reader consumes. It reuses
/// [`validate_packed_decimal_groups`] on every NEWDECIMAL span (the
/// dependency's reader panics on hostile digit groups in a debug build and
/// renders silently wrong output in a release build) and range-checks the
/// fraction bytes of TIMESTAMP2/DATETIME2/TIME2 values (the dependency
/// recombines an overflowing fraction through a signed integer and renders
/// a wrong time). A byte shortfall is reported as trailing bytes after the
/// row data, matching the library's own deterministic rejection of the same
/// shape.
///
/// The walk never rejects what the library accepts: when a present column's
/// value width cannot be resolved from the metadata (an unknown or
/// unsupported type the library rejects deterministically), the walk stops
/// for the whole event and defers to the library — continuing would read
/// the remaining columns from the un-advanced offset and misattribute the
/// error (R50 follow-up). An image that consumes no bytes at all defers as
/// well — the ordinary rows arm rejects that no-progress shape before the
/// walk (R49). `images` holds the before and after layouts in the order the
/// library reads them; a missing image is `None` (write rows carry only an
/// after image, delete rows only a before image). `json_column_count` is
/// the table map's count of JSON columns, which sizes the partial-update
/// bitmap.
pub(crate) fn prevalidate_row_layout(
    rows_data: &[u8],
    images: &[Option<RowImageLayout<'_>>],
    json_column_count: usize,
    map: &TableMapEvent<'_>,
) -> io::Result<()> {
    // An empty row body decodes as zero rows; nothing to walk.
    if rows_data.is_empty() {
        return Ok(());
    }
    // Column types and metadata are resolved once, lazily, as the walk
    // reaches them: the library's metadata getter re-walks every preceding
    // column, so a naive per-column call would be quadratic per row.
    let mut types: Vec<Option<ColumnType>> = Vec::new();
    let mut metas: Vec<&[u8]> = Vec::new();
    let mut pos = 0usize;
    while pos < rows_data.len() {
        let start = pos;
        for image in images.iter().flatten() {
            if !skip_row_image(
                rows_data,
                &mut pos,
                image,
                json_column_count,
                map,
                &mut types,
                &mut metas,
            )? {
                // Some column value's width does not resolve from the
                // metadata: the library rejects the shape deterministically,
                // and walking on would read the later columns from the
                // un-advanced offset, misattributing the error. Defer the
                // event wholly to the library instead of guessing.
                return Ok(());
            }
        }
        if pos == start {
            // Neither image consumed a byte: the library would loop on this
            // shape. The ordinary rows arm rejects it before the walk (R49);
            // defer to the library for anything else instead of guessing.
            return Ok(());
        }
    }
    Ok(())
}

/// Advances `pos` past one row image, mirroring the library's `BinlogRow`
/// deserialization: the partial-update value options (when
/// `partial_header`), the null bitmap over the image's set column bits, and
/// one value per present, non-null column.
///
/// Returns `false` when a present column's value width cannot be resolved
/// (see [`skip_column_value`]): `pos` still points at that value, and the
/// caller stops walking instead of continuing from it.
fn skip_row_image<'a>(
    data: &[u8],
    pos: &mut usize,
    image: &RowImageLayout<'_>,
    json_column_count: usize,
    map: &'a TableMapEvent<'a>,
    types: &mut Vec<Option<ColumnType>>,
    metas: &mut Vec<&'a [u8]>,
) -> io::Result<bool> {
    if image.partial_header {
        // WL#2955: a length-encoded value-options field, then a bitmap over
        // the table's JSON columns when PARTIAL_JSON_UPDATES is set.
        let value_options = read_row_lenenc(data, pos)?;
        if value_options & 1 != 0 {
            span(data, pos, json_column_count.div_ceil(8))?;
        }
    }
    // The null bitmap covers every set bit of the image's column bitmap,
    // including set bits beyond the declared column count.
    let null_bits = span(data, pos, image.bitmap_ones.div_ceil(8))?;
    let mut image_index = 0usize;
    for (index, &present) in image.present.iter().enumerate() {
        if !present {
            continue;
        }
        // Lsb0 bit order, as the library's `BitVec<u8>` reads it; a bit
        // missing from the bitmap counts as NULL, like the library's
        // `unwrap_or(true)` fallback.
        let is_null = null_bits
            .get(image_index / 8)
            .is_none_or(|byte| byte & (1 << (image_index % 8)) != 0);
        image_index += 1;
        if is_null {
            continue;
        }
        if !skip_column_value(data, pos, index, map, types, metas)? {
            return Ok(false);
        }
    }
    Ok(true)
}

/// How a column value's width is derived from its type and metadata.
enum ValueWidth {
    /// A fixed number of bytes.
    Fixed(usize),
    /// A length-prefixed value: the prefix of the given width holds the
    /// value's byte length, little-endian.
    Prefixed(usize),
    /// The width cannot be derived from the metadata; the library decides.
    Deferred,
}

/// Advances `pos` past one present, non-null column value, deriving the
/// width the library's value reader consumes.
///
/// Returns `false` when the width cannot be resolved (an unknown or
/// unsupported type the library rejects deterministically): the walk then
/// defers to the library instead of guessing.
fn skip_column_value<'a>(
    data: &[u8],
    pos: &mut usize,
    column: usize,
    map: &'a TableMapEvent<'a>,
    types: &mut Vec<Option<ColumnType>>,
    metas: &mut Vec<&'a [u8]>,
) -> io::Result<bool> {
    while types.len() <= column {
        let index = types.len();
        let ty = map.get_column_type(index).ok().flatten();
        let meta = map.get_column_metadata(index).unwrap_or(&[]);
        types.push(ty);
        metas.push(meta);
    }
    let Some(column_type) = types[column] else {
        // Unknown or unsupported type byte: the library rejects it
        // deterministically.
        return Ok(false);
    };
    let meta = metas[column];
    let width = match column_type {
        ColumnType::MYSQL_TYPE_TINY => ValueWidth::Fixed(1),
        ColumnType::MYSQL_TYPE_SHORT => ValueWidth::Fixed(2),
        ColumnType::MYSQL_TYPE_INT24 => ValueWidth::Fixed(3),
        ColumnType::MYSQL_TYPE_LONG => ValueWidth::Fixed(4),
        ColumnType::MYSQL_TYPE_LONGLONG => ValueWidth::Fixed(8),
        ColumnType::MYSQL_TYPE_FLOAT => ValueWidth::Fixed(4),
        ColumnType::MYSQL_TYPE_DOUBLE => ValueWidth::Fixed(8),
        ColumnType::MYSQL_TYPE_TIMESTAMP => ValueWidth::Fixed(4),
        ColumnType::MYSQL_TYPE_YEAR => ValueWidth::Fixed(1),
        ColumnType::MYSQL_TYPE_NEWDATE => ValueWidth::Fixed(3),
        ColumnType::MYSQL_TYPE_TIME => ValueWidth::Fixed(3),
        ColumnType::MYSQL_TYPE_DATETIME => ValueWidth::Fixed(8),
        ColumnType::MYSQL_TYPE_TIMESTAMP2
        | ColumnType::MYSQL_TYPE_DATETIME2
        | ColumnType::MYSQL_TYPE_TIME2 => {
            // An integer part of 4/5/3 bytes (timestamp/datetime/time) plus
            // the fraction bytes.
            let int_bytes = match column_type {
                ColumnType::MYSQL_TYPE_TIMESTAMP2 => 4,
                ColumnType::MYSQL_TYPE_DATETIME2 => 5,
                _ => 3,
            };
            return skip_temporal2(data, pos, column, column_type, int_bytes, meta);
        }
        ColumnType::MYSQL_TYPE_BIT => {
            let (Some(&low), Some(&high)) = (meta.first(), meta.get(1)) else {
                return Ok(false);
            };
            ValueWidth::Fixed((usize::from(high) * 8 + usize::from(low)).div_ceil(8))
        }
        ColumnType::MYSQL_TYPE_ENUM => {
            let Some(&pack) = meta.get(1) else {
                return Ok(false);
            };
            match pack {
                // The library reads a u8 or u16 by the declared pack length
                // and errors on anything else.
                1 | 2 => ValueWidth::Fixed(usize::from(pack)),
                _ => ValueWidth::Deferred,
            }
        }
        ColumnType::MYSQL_TYPE_SET => {
            let Some(&pack) = meta.get(1) else {
                return Ok(false);
            };
            ValueWidth::Fixed(usize::from(pack))
        }
        ColumnType::MYSQL_TYPE_JSON => ValueWidth::Prefixed(4),
        ColumnType::MYSQL_TYPE_NEWDECIMAL => {
            let (Some(&precision), Some(&scale)) = (meta.first(), meta.get(1)) else {
                return Ok(false);
            };
            if scale > precision {
                // Rejected at registration; defer for defense in depth, as
                // the dependency's packed-size arithmetic underflows here.
                return Ok(false);
            }
            let packed = span(data, pos, decimal_column_bin_size(precision, scale))?;
            validate_column_packed_decimal(column, precision, scale, packed)?;
            return Ok(true);
        }
        ColumnType::MYSQL_TYPE_TINY_BLOB
        | ColumnType::MYSQL_TYPE_MEDIUM_BLOB
        | ColumnType::MYSQL_TYPE_LONG_BLOB
        | ColumnType::MYSQL_TYPE_BLOB
        | ColumnType::MYSQL_TYPE_GEOMETRY
        | ColumnType::MYSQL_TYPE_VECTOR => {
            let Some(&prefix) = meta.first() else {
                return Ok(false);
            };
            // The library errors on a prefix width outside 1..=4.
            match prefix {
                1..=4 => ValueWidth::Prefixed(usize::from(prefix)),
                _ => ValueWidth::Deferred,
            }
        }
        ColumnType::MYSQL_TYPE_VARCHAR => {
            let (Some(&low), Some(&high)) = (meta.first(), meta.get(1)) else {
                return Ok(false);
            };
            let type_len = u16::from(low) | (u16::from(high) << 8);
            ValueWidth::Prefixed(if type_len < 256 { 1 } else { 2 })
        }
        ColumnType::MYSQL_TYPE_STRING => {
            let (Some(&byte0), Some(&byte1)) = (meta.first(), meta.get(1)) else {
                return Ok(false);
            };
            // The dependency's CHAR length encoding: the high length bits
            // live in the first metadata byte unless the old-style two-byte
            // form is used.
            let length = if byte0 >= 1 {
                if (byte0 & 0x30) != 0x30 {
                    usize::from(byte1) | ((usize::from(byte0 & 0x30) ^ 0x30) << 4)
                } else {
                    usize::from(byte1)
                }
            } else {
                usize::from(byte1) << 8
            };
            ValueWidth::Prefixed(if length < 256 { 1 } else { 2 })
        }
        // DATE resolves to NEWDATE above, and everything else (old DECIMAL,
        // NULL, TYPED_ARRAY, VAR_STRING, unknown bytes) is rejected by the
        // library or the registration checks deterministically.
        _ => ValueWidth::Deferred,
    };
    match width {
        ValueWidth::Deferred => Ok(false),
        ValueWidth::Fixed(count) => {
            span(data, pos, count)?;
            Ok(true)
        }
        ValueWidth::Prefixed(prefix) => {
            let raw = span(data, pos, prefix)?;
            span(data, pos, prefixed_length(raw))?;
            Ok(true)
        }
    }
}

/// Advances past one TIMESTAMP2/DATETIME2/TIME2 value: the integer part,
/// then the fraction bytes, whose big-endian value must stay below 10^fsp.
///
/// The server never writes an overflowing fraction, and the dependency
/// recombines one through a signed integer, rendering a wrong (negative)
/// time. Returns `false` when the metadata does not resolve the width, and
/// errors on a fraction byte the server could not have written.
fn skip_temporal2(
    data: &[u8],
    pos: &mut usize,
    column: usize,
    column_type: ColumnType,
    int_bytes: usize,
    meta: &[u8],
) -> io::Result<bool> {
    let Some(&fsp) = meta.first() else {
        return Ok(false);
    };
    if fsp > 6 {
        // Rejected at registration; defer for defense in depth.
        return Ok(false);
    }
    span(data, pos, int_bytes)?;
    let frac_bytes = usize::from(fsp).div_ceil(2);
    if frac_bytes == 0 {
        return Ok(true);
    }
    let fraction = span(data, pos, frac_bytes)?;
    let value = match frac_bytes {
        1 => u32::from(fraction[0]),
        2 => u32::from(u16::from_be_bytes([fraction[0], fraction[1]])),
        _ => u32::from_be_bytes([0, fraction[0], fraction[1], fraction[2]]),
    };
    let limit: u32 = match fsp {
        1..=2 => 99,
        3..=4 => 9_999,
        _ => 999_999,
    };
    if value > limit {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            format!(
                "malformed {} fraction in column {column}",
                column_type_name(column_type)
            ),
        ));
    }
    Ok(true)
}

/// Decodes a little-endian length prefix of one to four bytes.
fn prefixed_length(raw: &[u8]) -> usize {
    match raw.len() {
        1 => usize::from(raw[0]),
        2 => usize::from(u16::from_le_bytes(raw.try_into().unwrap())),
        3 => u32::from_le_bytes([raw[0], raw[1], raw[2], 0]) as usize,
        _ => u32::from_le_bytes(raw.try_into().unwrap()) as usize,
    }
}

/// The packed byte size of a NEWDECIMAL column value, mirroring the
/// dependency's `decimal_bin_size` for metadata the registration checks keep
/// valid (`scale <= precision` inside the server ranges).
fn decimal_column_bin_size(precision: u8, scale: u8) -> usize {
    const DIG_PER_DEC: usize = 9;
    const DIG_TO_BYTES: [usize; DIG_PER_DEC + 1] = [0, 1, 1, 2, 2, 3, 3, 4, 4, 4];
    let intg = usize::from(precision) - usize::from(scale);
    intg / DIG_PER_DEC * 4
        + DIG_TO_BYTES[intg % DIG_PER_DEC]
        + usize::from(scale) / DIG_PER_DEC * 4
        + DIG_TO_BYTES[usize::from(scale) % DIG_PER_DEC]
}

/// Returns the `count` bytes at `pos`, advancing `pos`, or a trailing-bytes
/// error when they do not fit: the library's row parser reads the same
/// spans and errors deterministically on the same shortfall.
fn span<'a>(data: &'a [u8], pos: &mut usize, count: usize) -> io::Result<&'a [u8]> {
    let end = pos
        .checked_add(count)
        .filter(|end| *end <= data.len())
        .ok_or_else(trailing_bytes_error)?;
    let spanned = &data[*pos..end];
    *pos = end;
    Ok(spanned)
}

/// Reads one length-encoded integer with the library's `ParseBuf` semantics
/// (`0xfb`/`0xff` decode to zero), as `BinlogRow` reads the WL#2955
/// value-options field. A truncated field means the row data ends mid-value.
fn read_row_lenenc(data: &[u8], pos: &mut usize) -> io::Result<u64> {
    let Some(&first) = data.get(*pos) else {
        return Err(trailing_bytes_error());
    };
    let extra = match first {
        0xfc => 2usize,
        0xfd => 3,
        0xfe => 8,
        // The canonical short forms and the non-canonical 0xfb/0xff prefixes
        // (which the library decodes to zero) consume one byte.
        _ => 0,
    };
    let raw = span(data, pos, 1 + extra)?;
    Ok(match extra {
        0 if first == 0xfb || first == 0xff => 0,
        0 => u64::from(first),
        2 => u64::from(u16::from_le_bytes(raw[1..3].try_into().unwrap())),
        3 => u64::from(u32::from_le_bytes([raw[1], raw[2], raw[3], 0])),
        _ => u64::from_le_bytes(raw[1..9].try_into().unwrap()),
    })
}

/// Reads the SIGNEDNESS (type 1) TLV value of a table map event, if any.
///
/// The event is serialized back into its wire body (always the current
/// 6-byte-table-id form) and the optional metadata block is located with the
/// same structural walk the pre-validation uses, so the raw TLV bytes can be
/// scanned without going through the library's metadata iterator. The last
/// SIGNEDNESS entry wins, mirroring the library's extraction.
fn signedness_bits(map: &TableMapEvent<'_>) -> io::Result<Option<Vec<u8>>> {
    let mut body = Vec::new();
    map.serialize(&mut body);
    let pos = walk_table_map_header(&body, 8)
        .ok_or_else(|| io::Error::new(io::ErrorKind::InvalidData, "table map does not fit"))?;
    let mut signedness = None;
    for entry in collect_tlvs(&body[pos..])? {
        if entry.field_type == TLV_SIGNEDNESS {
            signedness = Some(body[pos + entry.value.start..pos + entry.value.end].to_vec());
        }
    }
    Ok(signedness)
}

/// Reads one bit of an MSB-first bitmap (`0x80 >> (index % 8)` of byte
/// `index / 8`, one bit per column of the covered kind in table order).
///
/// Bits beyond the available bytes mean the metadata was absent or shorter
/// than the column count; the library reports unknown signedness then, too.
fn bitmap_bit(bytes: Option<&[u8]>, index: usize) -> Option<bool> {
    let byte = *bytes?.get(index / 8)?;
    Some(byte & (0x80 >> (index % 8)) != 0)
}

/// One scanned optional metadata TLV entry: its field type, the byte range of
/// the whole entry (type byte, length prefix, and value), and the byte range
/// of the value alone — both relative to the metadata block.
struct TlvEntry {
    field_type: u8,
    entry: std::ops::Range<usize>,
    value: std::ops::Range<usize>,
}

/// Scans the optional metadata block into its TLV entries.
///
/// Length prefixes are decoded with the library's strict reader, so a
/// non-canonical `0xfb`/`0xff` prefix or a truncated prefix is an error, and a
/// declared length that exceeds the metadata block is an error — mirroring the
/// previous inline walk exactly.
fn collect_tlvs(meta: &[u8]) -> io::Result<Vec<TlvEntry>> {
    let mut pos = 0;
    let mut entries = Vec::new();
    while pos < meta.len() {
        let start = pos;
        let field_type = meta[pos];
        pos += 1;
        let len = match read_lenenc(meta, &mut pos)? {
            Some(len) => len as usize,
            // Invalid length-encoded integer; the library rejects it as well.
            None => {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "invalid optional metadata length",
                ));
            }
        };
        let value_start = pos;
        let Some(value_end) = pos.checked_add(len).filter(|end| *end <= meta.len()) else {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "optional metadata TLV exceeds table map bounds",
            ));
        };
        entries.push(TlvEntry {
            field_type,
            entry: start..value_end,
            value: value_start..value_end,
        });
        pos = value_end;
    }
    Ok(entries)
}

/// Validates table map optional metadata against the raw event bytes before
/// the library parses it.
///
/// Walks the same structure the library's `TableMapEvent` deserializer walks
/// (identical field order and encodings), then checks every optional metadata
/// TLV: declared lengths must fit the remaining bytes, only field types known
/// to the library or belonging to the partial-JSON extension are accepted, and
/// ENUM/SET variant counts must fit the TLV value (the library allocates from
/// the declared count without bounding it).
///
/// Structural shortfalls deliberately return `Ok`: the library rejects those
/// deterministically with an end-of-stream error, so there is nothing to
/// prevent. Only cases that could abort the process are rejected here.
pub(crate) fn validate_table_map_metadata(body: &[u8], tme_header_length: u8) -> io::Result<()> {
    let pos = match walk_table_map_header(body, tme_header_length) {
        Some(pos) => pos,
        // The structure does not fit; the library will reject this event.
        None => return Ok(()),
    };
    for entry in collect_tlvs(&body[pos..])? {
        if !KNOWN_TLV_TYPES.contains(&entry.field_type) {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "unknown optional metadata field type",
            ));
        }
        if entry.field_type == TLV_SET_STR_VALUE || entry.field_type == TLV_ENUM_STR_VALUE {
            validate_enum_set_variants(&body[pos + entry.value.start..pos + entry.value.end])?;
        }
    }
    Ok(())
}

/// Byte ranges (relative to the table map event body) of the optional
/// metadata entries the underlying library cannot iterate.
///
/// The library stores a table map's optional metadata raw and iterates it
/// lazily when decoding rows; its iterator rejects the partial-JSON types 14
/// (`PARTIAL_JSON_UPDATE`) and 15 (`UPDATED_COLUMN_TYPES`) with a hard error,
/// so those entries must be removed from the event body before the library
/// parses the table map. Returns `None` when the event structure does not fit
/// the expected layout — the library rejects such events deterministically,
/// so there is nothing to strip.
pub(crate) fn unsupported_tlv_ranges(
    body: &[u8],
    tme_header_length: u8,
) -> io::Result<Option<Vec<std::ops::Range<usize>>>> {
    let Some(pos) = walk_table_map_header(body, tme_header_length) else {
        return Ok(None);
    };
    let mut ranges = Vec::new();
    for entry in collect_tlvs(&body[pos..])? {
        if entry.field_type == TLV_PARTIAL_JSON_UPDATE
            || entry.field_type == TLV_UPDATED_COLUMN_TYPES
        {
            ranges.push((pos + entry.entry.start)..(pos + entry.entry.end));
        }
    }
    Ok(Some(ranges))
}

/// Walks the fixed part of a table map event and returns the offset of the
/// optional metadata block, or `None` if the event is too short.
fn walk_table_map_header(body: &[u8], tme_header_length: u8) -> Option<usize> {
    let mut pos = 0;
    // Old servers declare a 6-byte post-header and carry a 4-byte table id.
    pos += if tme_header_length == 6 { 4 } else { 6 };
    pos += 2; // flags
    for _ in 0..2 {
        let name_len = *body.get(pos)?;
        pos += 1 + usize::from(name_len) + 1; // length byte, name, NUL
    }
    // The library reads both counts through ParseBuf, whose lenenc decoding
    // maps the non-canonical 0xfb/0xff prefixes to zero instead of failing;
    // mirroring that keeps the walk running for events the library accepts.
    let columns_count = read_lenenc_lenient(body, &mut pos).ok()? as usize;
    let types_end = pos.checked_add(columns_count)?;
    if types_end > body.len() {
        return None;
    }
    pos = types_end;
    let meta_len = read_lenenc_lenient(body, &mut pos).ok()? as usize;
    let meta_end = pos.checked_add(meta_len)?;
    if meta_end > body.len() {
        return None;
    }
    pos = meta_end;
    let null_bytes = columns_count.div_ceil(8);
    let null_end = pos.checked_add(null_bytes)?;
    if null_end > body.len() {
        return None;
    }
    Some(null_end)
}

/// Validates every column variant list in a SET/ENUM optional metadata TLV.
///
/// The library allocates a vector with the declared variant count before
/// reading anything, so an inflated count must be rejected here. The count is
/// decoded through the library's `ParseBuf` lenenc semantics (`0xfb`/`0xff`
/// mean zero), mirroring `RawInt<LenEnc>`.
fn validate_enum_set_variants(value: &[u8]) -> io::Result<()> {
    let mut pos = 0;
    loop {
        let num_variants = read_lenenc_lenient(value, &mut pos)? as usize;
        // Every variant needs at least one length byte, so a count larger than the
        // remaining bytes can never be satisfied.
        if num_variants > value.len() - pos {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "enum/set variant count exceeds TLV bounds",
            ));
        }
        for _ in 0..num_variants {
            let len = match read_lenenc(value, &mut pos)? {
                Some(len) => len as usize,
                None => {
                    return Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        "invalid variant length",
                    ));
                }
            };
            let Some(end) = pos.checked_add(len) else {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "variant length exceeds TLV bounds",
                ));
            };
            if end > value.len() {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    "variant name exceeds TLV bounds",
                ));
            }
            pos = end;
        }
        if pos == value.len() {
            break;
        }
    }
    Ok(())
}

/// Reads a MySQL length-encoded integer the way the library's `ReadMysqlExt`
/// does for optional metadata TLV lengths: the non-canonical `0xfb`/`0xff`
/// prefixes are errors. Returns `None` for them.
fn read_lenenc(buf: &[u8], pos: &mut usize) -> io::Result<Option<u64>> {
    let Some(&first) = buf.get(*pos) else {
        return Err(io::Error::new(
            io::ErrorKind::InvalidData,
            "unexpected end of length-encoded integer",
        ));
    };
    let extra = match first {
        x if x <= 0xfa => {
            *pos += 1;
            return Ok(Some(u64::from(x)));
        }
        0xfc => 2,
        0xfd => 3,
        0xfe => 8,
        0xfb | 0xff => return Ok(None),
        _ => unreachable!(),
    };
    let end = pos
        .checked_add(1 + extra)
        .filter(|end| *end <= buf.len())
        .ok_or_else(|| {
            io::Error::new(
                io::ErrorKind::InvalidData,
                "unexpected end of length-encoded integer",
            )
        })?;
    let mut raw = [0u8; 8];
    raw[..extra].copy_from_slice(&buf[*pos + 1..end]);
    *pos = end;
    Ok(Some(u64::from_le_bytes(raw)))
}

/// Reads a length-encoded integer through the library's `ParseBuf` semantics,
/// where the `0xfb`/`0xff` prefix byte is consumed and decodes to zero
/// instead of failing.
fn read_lenenc_lenient(buf: &[u8], pos: &mut usize) -> io::Result<u64> {
    match read_lenenc(buf, pos)? {
        Some(value) => Ok(value),
        None => {
            *pos += 1; // consume the prefix the way the library does
            Ok(0)
        }
    }
}

/// Application of WL#2955 binlog JSON diffs to a before value.
///
/// A partial JSON after image stores, per JSON column, a sequence of logical
/// changes (`replace`/`insert`/`remove`) that the MySQL applier replays
/// against the before image's full document. This module parses the MySQL
/// JSON path text form of each diff and applies the operations to a
/// [`serde_json::Value`], matching the server's behavior of failing the whole
/// application when a path cannot be traversed or a precondition (existing
/// member, array bounds) does not hold.
mod json_diff {
    use crate::event::JsonDiffRecord;
    use mysql_common::binlog::jsondiff::{JsonDiff, JsonDiffOperation};

    /// Why a sequence of binlog JSON diffs could not be applied.
    ///
    /// The message is user-facing (it becomes the `reason` of
    /// [`CellValue::PartialJson`]), so it always names the offending path.
    #[derive(Debug, Clone, PartialEq)]
    pub(super) struct ApplyError(pub(super) String);

    /// The operation kind of a binlog JSON diff.
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub(super) enum Operation {
        /// Set the value at the path.
        Replace,
        /// Add a new member or array element at the path.
        Insert,
        /// Delete the member or array element at the path.
        Remove,
    }

    impl Operation {
        fn name(self) -> &'static str {
            match self {
                Self::Replace => "replace",
                Self::Insert => "insert",
                Self::Remove => "remove",
            }
        }

        fn parse(name: &str) -> Result<Self, ApplyError> {
            match name {
                "replace" => Ok(Self::Replace),
                "insert" => Ok(Self::Insert),
                "remove" => Ok(Self::Remove),
                other => Err(ApplyError(format!("unknown diff operation `{other}`"))),
            }
        }
    }

    /// One segment of a concrete MySQL JSON path.
    #[derive(Debug, Clone, PartialEq, Eq)]
    enum Segment {
        /// An object member, resolved by name.
        Member(String),
        /// An array element, resolved by zero-based index.
        Index(usize),
    }

    /// Converts the library's parsed diffs of one column into model records.
    ///
    /// A diff whose binary JSON value cannot be converted at all (invalid
    /// UTF-8 in a string member, or nesting beyond the jsonb depth cap or one
    /// of the node budgets) yields `None` for its value and is reported in
    /// the returned problem note instead of failing the whole column; opaque
    /// values convert like any other scalar (see [`super::jsonb_to_serde`]).
    ///
    /// `event_budget` is the calling row event's shared per-event counter
    /// (R48-refinement): the whole sequence charges it monotonically, like
    /// any other value of the event.
    pub(super) fn parse_diffs(
        diffs: &[JsonDiff<'_>],
        event_budget: &mut super::EventNodeBudget,
    ) -> (Vec<JsonDiffRecord>, Option<String>) {
        let mut parsed = Vec::with_capacity(diffs.len());
        let mut problem = None;
        // The whole diff sequence shares one monotone per-value node budget
        // (R48): an unrepresentable value does not abort the loop, so a fresh
        // budget per diff would let hostile values each re-amplify to the
        // limit.
        let mut nodes = 0usize;
        for diff in diffs {
            let operation = match diff.operation() {
                JsonDiffOperation::REPLACE => Operation::Replace,
                JsonDiffOperation::INSERT => Operation::Insert,
                JsonDiffOperation::REMOVE => Operation::Remove,
            };
            let value = match diff.value() {
                Some(jsonb) => match super::jsonb_to_serde(jsonb, 1, &mut nodes, event_budget) {
                    Ok(value) => Some(value),
                    Err(error) => {
                        problem.get_or_insert_with(|| {
                            format!("diff value is not representable as JSON ({error})")
                        });
                        None
                    }
                },
                None => None,
            };
            parsed.push(JsonDiffRecord {
                path: diff.path_str().into_owned(),
                operation: operation.name().to_owned(),
                value,
            });
        }
        (parsed, problem)
    }

    /// Applies a sequence of diffs to a before value, in order.
    ///
    /// Mirrors the MySQL applier: the first failure aborts the whole
    /// application, leaving no partial result behind.
    pub(super) fn apply_json_diffs(
        before: serde_json::Value,
        diffs: &[JsonDiffRecord],
    ) -> Result<serde_json::Value, ApplyError> {
        let mut value = before;
        for diff in diffs {
            apply_one(&mut value, diff)?;
        }
        // Applying a ≤100-deep diff value at a deep path composes documents
        // whose depth no input cap bounds: a REPLACE can re-insert a
        // 100-level chain at a leaf, adding ~99 levels per diff. The output
        // serializer recurses, so an unbounded composite would abort the
        // process; a composed document beyond MySQL's limit is a loss record
        // instead (real server diffs never compose beyond it).
        if json_depth(&value) > super::MAX_JSONB_DEPTH {
            return Err(ApplyError(
                "the composed document exceeds the nesting limit of 100 levels".to_owned(),
            ));
        }
        Ok(value)
    }

    /// Maximum container depth of a JSON value, matching jsonb_to_serde.
    /// Scalars add no container level. The explicit stack bounds recursion
    /// while inspecting a potentially hostile composed document.
    fn json_depth(value: &serde_json::Value) -> usize {
        let mut max = 0;
        let mut stack = vec![(value, 1usize)];
        while let Some((current, depth)) = stack.pop() {
            match current {
                serde_json::Value::Array(items) => {
                    max = max.max(depth);
                    stack.extend(items.iter().map(|item| (item, depth + 1)));
                }
                serde_json::Value::Object(members) => {
                    max = max.max(depth);
                    stack.extend(members.values().map(|member| (member, depth + 1)));
                }
                _ => {}
            }
        }
        max
    }

    /// Applies a single diff to a value in place.
    ///
    /// Edge-case semantics, pinned to the server's applier (MySQL
    /// `sql-common/json_diff.cc`): an object REPLACE behaves as an upsert (it also
    /// adds the member when it is absent), and an array INSERT accepts
    /// index == length (append) but not beyond. Real server diffs always
    /// target existing paths, so these edges only decide how malformed or
    /// hostile diff sequences render; the tests in this module and in
    /// `tests/partial_json.rs` pin them.
    fn apply_one(value: &mut serde_json::Value, diff: &JsonDiffRecord) -> Result<(), ApplyError> {
        let at = |what: &'static str| ApplyError(format!("path `{}`: {what}", diff.path));
        let operation = Operation::parse(&diff.operation)?;
        let segments = parse_path(&diff.path)?;
        // Depth bound enforced BEFORE any mutation: an inserted value of
        // depth `json_depth(value)` at a path `segments.len()` levels down
        // composes a document that deep. The composite is dropped
        // recursively and serialized recursively, so it must never be built
        // past the limit — rejecting the diff early keeps every reachable
        // state within it (R44 verification finding D1).
        if let Some(new_value) = &diff.value
            && segments.len() + json_depth(new_value) > super::MAX_JSONB_DEPTH
        {
            return Err(at(
                "the composed document would exceed the nesting limit of 100 levels",
            ));
        }
        let Some((last, parents)) = segments.split_last() else {
            // The bare `$` path targets the whole document.
            return match operation {
                Operation::Replace => {
                    let Some(new_value) = &diff.value else {
                        return Err(at("the operation carries no value"));
                    };
                    *value = new_value.clone();
                    Ok(())
                }
                Operation::Insert | Operation::Remove => {
                    Err(at("cannot insert or remove the document root"))
                }
            };
        };
        let mut current = value;
        for segment in parents {
            current = match segment {
                Segment::Member(key) => match current {
                    serde_json::Value::Object(map) => map
                        .get_mut(key)
                        .ok_or_else(|| at("parent path element does not exist"))?,
                    _ => return Err(at("parent path element is not an object")),
                },
                Segment::Index(index) => match current {
                    serde_json::Value::Array(items) => items
                        .get_mut(*index)
                        .ok_or_else(|| at("parent path element does not exist"))?,
                    _ => return Err(at("parent path element is not an array")),
                },
            };
        }
        let new_value = || {
            diff.value
                .clone()
                .ok_or_else(|| at("the operation carries no value"))
        };
        match operation {
            Operation::Replace => match (last, current) {
                (Segment::Member(key), serde_json::Value::Object(map)) => {
                    map.insert(key.clone(), new_value()?);
                    Ok(())
                }
                (Segment::Index(index), serde_json::Value::Array(items))
                    if *index < items.len() =>
                {
                    items[*index] = new_value()?;
                    Ok(())
                }
                _ => Err(at("replace target does not exist")),
            },
            Operation::Insert => match (last, current) {
                (Segment::Member(key), serde_json::Value::Object(map)) => {
                    if map.contains_key(key) {
                        return Err(at("insert on an existing member"));
                    }
                    map.insert(key.clone(), new_value()?);
                    Ok(())
                }
                (Segment::Index(index), serde_json::Value::Array(items))
                    if *index <= items.len() =>
                {
                    items.insert(*index, new_value()?);
                    Ok(())
                }
                _ => Err(at("insert target is not applicable")),
            },
            Operation::Remove => match (last, current) {
                (Segment::Member(key), serde_json::Value::Object(map)) => map
                    .remove(key)
                    .map(|_| ())
                    .ok_or_else(|| at("remove target does not exist")),
                (Segment::Index(index), serde_json::Value::Array(items))
                    if *index < items.len() =>
                {
                    items.remove(*index);
                    Ok(())
                }
                _ => Err(at("remove target does not exist")),
            },
        }
    }

    /// Parses the MySQL JSON path text form of a diff.
    ///
    /// Accepted accessors after the required `$`: bare member names
    /// (`[A-Za-z0-9_$]+`, the MySQL bare-key set), quoted member names
    /// (`."name"` and `["name"]`, double or single quotes, with backslash
    /// escapes), and array indices (`[0]`). Wildcards (`$.*`, `$**`) are
    /// rejected: binlog diffs always carry concrete paths.
    fn parse_path(path: &str) -> Result<Vec<Segment>, ApplyError> {
        let invalid =
            |what: &'static str| ApplyError(format!("invalid JSON path `{path}`: {what}"));
        let rest = path
            .strip_prefix('$')
            .ok_or_else(|| invalid("must start with `$`"))?;
        let bytes = rest.as_bytes();
        let mut pos = 0usize;
        let mut segments = Vec::new();
        while pos < bytes.len() {
            match bytes[pos] {
                b'.' => {
                    pos += 1;
                    if matches!(bytes.get(pos), Some(b'"') | Some(b'\'')) {
                        let quote = bytes[pos];
                        let key = read_quoted_member(rest, &mut pos, quote)?;
                        segments.push(Segment::Member(key));
                    } else {
                        let start = pos;
                        while bytes.get(pos).is_some_and(|byte| {
                            byte.is_ascii_alphanumeric() || *byte == b'_' || *byte == b'$'
                        }) {
                            pos += 1;
                        }
                        if pos == start {
                            return Err(invalid("empty member name"));
                        }
                        // Only ASCII was consumed, so slicing is valid UTF-8.
                        segments.push(Segment::Member(rest[start..pos].to_owned()));
                    }
                }
                b'[' => {
                    pos += 1;
                    if matches!(bytes.get(pos), Some(b'"') | Some(b'\'')) {
                        let quote = bytes[pos];
                        let key = read_quoted_member(rest, &mut pos, quote)?;
                        if bytes.get(pos) != Some(&b']') {
                            return Err(invalid("unterminated bracketed member"));
                        }
                        pos += 1;
                        segments.push(Segment::Member(key));
                    } else if bytes.get(pos).is_some_and(|byte| byte.is_ascii_digit()) {
                        let start = pos;
                        while bytes.get(pos).is_some_and(|byte| byte.is_ascii_digit()) {
                            pos += 1;
                        }
                        let index: usize = rest[start..pos]
                            .parse()
                            .map_err(|_| invalid("array index overflow"))?;
                        if bytes.get(pos) != Some(&b']') {
                            return Err(invalid("unterminated array index"));
                        }
                        pos += 1;
                        segments.push(Segment::Index(index));
                    } else {
                        return Err(invalid("expected a quoted member name or an array index"));
                    }
                }
                _ => return Err(invalid("unsupported path syntax")),
            }
        }
        Ok(segments)
    }

    /// Reads a quoted member name starting at the opening quote byte, which
    /// may be `"` or `'`, advancing `pos` past the closing quote.
    ///
    /// Double-quoted members use JSON string decoding, including UTF-16
    /// surrogate pairs and validation of escape sequences, as in MySQL's
    /// `sql-common/json_path.cc`. The single-quoted extension retains its
    /// historical escape handling.
    fn read_quoted_member(rest: &str, pos: &mut usize, quote: u8) -> Result<String, ApplyError> {
        if quote == b'"' {
            // Decode just the string token so subsequent path segments stay
            // available to parse_path. Reuse serde's escape validation.
            let mut strings =
                serde_json::Deserializer::from_str(&rest[*pos..]).into_iter::<String>();
            let key = strings
                .next()
                .ok_or_else(|| {
                    ApplyError(format!(
                        "invalid JSON path `${rest}`: missing quoted member"
                    ))
                })?
                .map_err(|error| ApplyError(format!("invalid JSON path `${rest}`: {error}")))?;
            *pos += strings.byte_offset();
            return Ok(key);
        }
        let bytes = rest.as_bytes();
        *pos += 1; // opening quote
        let mut key = Vec::new();
        loop {
            let Some(&byte) = bytes.get(*pos) else {
                return Err(ApplyError(format!(
                    "invalid JSON path `${rest}`: unterminated quoted member name"
                )));
            };
            *pos += 1;
            match byte {
                byte if byte == quote => break,
                b'\\' => {
                    let Some(&escape) = bytes.get(*pos) else {
                        return Err(ApplyError(format!(
                            "invalid JSON path `${rest}`: unterminated escape sequence"
                        )));
                    };
                    *pos += 1;
                    match escape {
                        b'n' => key.push(b'\n'),
                        b'r' => key.push(b'\r'),
                        b't' => key.push(b'\t'),
                        b'b' => key.push(b'\x08'),
                        b'0' => key.push(0),
                        b'u' => {
                            let code = bytes
                                .get(*pos..*pos + 4)
                                .and_then(|raw| std::str::from_utf8(raw).ok())
                                .and_then(|raw| u32::from_str_radix(raw, 16).ok());
                            let Some(code) = code else {
                                return Err(ApplyError(format!(
                                    "invalid JSON path `${rest}`: invalid \\u escape"
                                )));
                            };
                            *pos += 4;
                            let Some(character) = char::from_u32(code) else {
                                return Err(ApplyError(format!(
                                    "invalid JSON path `${rest}`: invalid \\u escape"
                                )));
                            };
                            let mut encoded = [0; 4];
                            key.extend_from_slice(character.encode_utf8(&mut encoded).as_bytes());
                        }
                        other => key.push(other),
                    }
                }
                other => key.push(other),
            }
        }
        String::from_utf8(key).map_err(|_| {
            ApplyError(format!(
                "invalid JSON path `${rest}`: member name is not valid UTF-8"
            ))
        })
    }

    #[cfg(test)]
    mod tests {
        use super::*;
        use crate::event::JsonDiffRecord;

        /// Builds a diff record the way the model stores them.
        fn rec(path: &str, operation: &str, value: Option<serde_json::Value>) -> JsonDiffRecord {
            JsonDiffRecord {
                path: path.to_owned(),
                operation: operation.to_owned(),
                value,
            }
        }

        #[test]
        fn replaces_inserts_and_removes_members_and_elements() {
            let doc = serde_json::json!({"a": 1, "b": [1, 2]});
            let diffs = vec![
                rec("$.b[1]", "replace", Some(serde_json::json!(99))),
                rec("$.c", "insert", Some(serde_json::json!("x"))),
                rec("$.a", "remove", None),
            ];
            let applied = apply_json_diffs(doc, &diffs).unwrap();
            assert_eq!(applied, serde_json::json!({"b": [1, 99], "c": "x"}));
        }

        #[test]
        fn array_insert_shifts_and_accepts_end_position() {
            let doc = serde_json::json!([1, 2, 3]);
            let insert = |index: usize| {
                let diffs = [rec(
                    &format!("$[{index}]"),
                    "insert",
                    Some(serde_json::json!(9)),
                )];
                apply_json_diffs(doc.clone(), &diffs)
            };
            assert_eq!(insert(1).unwrap(), serde_json::json!([1, 9, 2, 3]));
            // Inserting at the length appends.
            assert_eq!(insert(3).unwrap(), serde_json::json!([1, 2, 3, 9]));
            assert!(insert(4).is_err());
        }

        #[test]
        fn root_replace_swaps_the_document() {
            let doc = serde_json::json!({"a": 1});
            let diffs = [rec("$", "replace", Some(serde_json::json!([true])))];
            assert_eq!(
                apply_json_diffs(doc, &diffs).unwrap(),
                serde_json::json!([true])
            );
            let remove_root = [rec("$", "remove", None)];
            assert!(apply_json_diffs(serde_json::json!({}), &remove_root).is_err());
        }

        #[test]
        fn diff_depth_limit_counts_containers_consistently_with_jsonb() {
            let depth = super::super::MAX_JSONB_DEPTH;
            let nested =
                |leaf| (0..depth).fold(leaf, |child, _| serde_json::Value::Array(vec![child]));
            let before = nested(serde_json::json!(1));
            assert_eq!(apply_json_diffs(before.clone(), &[]).unwrap(), before);
            let path = format!("${}", "[0]".repeat(depth));
            assert_eq!(
                apply_json_diffs(
                    before.clone(),
                    &[rec(&path, "replace", Some(serde_json::json!(2)))]
                )
                .unwrap(),
                nested(serde_json::json!(2))
            );
            let error = apply_json_diffs(
                before,
                &[rec(&path, "replace", Some(serde_json::json!([])))],
            )
            .unwrap_err();
            assert!(error.0.contains("nesting limit"), "{}", error.0);
            assert_eq!(
                apply_json_diffs(
                    serde_json::json!(null),
                    &[rec("$", "replace", Some(nested(serde_json::json!(3))))]
                )
                .unwrap(),
                nested(serde_json::json!(3))
            );
        }

        #[test]
        fn failures_name_the_offending_path() {
            let doc = serde_json::json!({"a": {"b": 1}});
            let remove = [rec("$.a.zz", "remove", None)];
            let error = apply_json_diffs(doc.clone(), &remove).unwrap_err();
            assert_eq!(error.0, "path `$.a.zz`: remove target does not exist");
            // A missing intermediate element is a failure, not a creation.
            let insert = [rec("$.zz.c", "insert", Some(serde_json::json!(1)))];
            let error = apply_json_diffs(doc, &insert).unwrap_err();
            assert!(
                error.0.contains("parent path element does not exist"),
                "{}",
                error.0
            );
        }

        #[test]
        fn insert_rejects_existing_members_and_replace_missing_ones() {
            let doc = serde_json::json!({"a": 1});
            let insert = [rec("$.a", "insert", Some(serde_json::json!(2)))];
            assert!(
                apply_json_diffs(doc.clone(), &insert)
                    .unwrap_err()
                    .0
                    .contains("insert on an existing member")
            );
            // Object replace is an upsert, like the task's applier contract.
            let replace = [rec("$.a", "replace", Some(serde_json::json!(2)))];
            assert_eq!(
                apply_json_diffs(doc, &replace).unwrap(),
                serde_json::json!({"a": 2})
            );
            // Array replace past the end is a failure.
            let doc = serde_json::json!([1]);
            let replace = [rec("$[3]", "replace", Some(serde_json::json!(2)))];
            assert!(
                apply_json_diffs(doc, &replace)
                    .unwrap_err()
                    .0
                    .contains("replace target does not exist")
            );
        }

        #[test]
        fn quoted_and_indexed_member_forms_are_accepted() {
            let doc = serde_json::json!({"a b": {"c.d": 1}});
            let diffs = [rec(
                r#"$."a b"["c.d"]"#,
                "replace",
                Some(serde_json::json!(2)),
            )];
            let applied = apply_json_diffs(doc, &diffs).unwrap();
            assert_eq!(applied, serde_json::json!({"a b": {"c.d": 2}}));
        }

        #[test]
        fn escapes_are_decoded_in_quoted_members() {
            let doc = serde_json::json!({"q\"k": 1});
            let diffs = [rec(r#"$."q\"k""#, "replace", Some(serde_json::json!(2)))];
            assert_eq!(
                apply_json_diffs(doc, &diffs).unwrap(),
                serde_json::json!({"q\"k": 2})
            );
        }

        #[test]
        fn json_quoted_paths_preserve_member_identity() {
            for (path, key) in [
                (r#"$."\f""#, "\u{000c}"),
                (r#"$["\uD83D\uDE00"]"#, "😀"),
                (r#"$."\u0000""#, "\0"),
                (r#"$."\"\\\/\b\n\r\t""#, "\"\\/\u{0008}\n\r\t"),
                (r#"$."中文""#, "中文"),
                (r"$.'legacy\'key'", "legacy'key"),
            ] {
                let mut doc = serde_json::json!({"f": 10});
                doc[key] = serde_json::json!({"child": 1});
                let diffs = [rec(
                    &format!("{path}.child"),
                    "replace",
                    Some(serde_json::json!(2)),
                )];
                let applied = apply_json_diffs(doc.clone(), &diffs).unwrap();
                doc[key]["child"] = serde_json::json!(2);
                assert_eq!(applied, doc, "{path}");
            }
        }

        #[test]
        fn invalid_json_quoted_paths_are_rejected() {
            for path in [
                r#"$."\uD800""#,
                r#"$."\uDC00""#,
                r#"$."\uD800\u0041""#,
                r#"$."\u12""#,
                r#"$."\u+123""#,
                r#"$."\x41""#,
                "$.\"line\nfeed\"",
            ] {
                let diffs = [rec(path, "replace", Some(serde_json::json!(2)))];
                let error = apply_json_diffs(serde_json::json!({}), &diffs).unwrap_err();
                assert!(error.0.contains("invalid JSON path"), "{path}: {}", error.0);
            }
        }

        #[test]
        fn unsupported_paths_are_rejected() {
            for path in [
                "a.b",                     // missing `$`
                "$.a-b",                   // `-` needs the quoted form
                "$..a",                    // ellipsis wildcard
                "$.a*",                    // wildcard
                "$[x]",                    // not an index
                "$.a[",                    // truncated
                "$.'a",                    // unterminated quote
                "$[99999999999999999999]", // index overflow
                "$.",                      // empty member
                "$.a.",                    // trailing accessor
            ] {
                let diffs = [rec(path, "replace", Some(serde_json::json!(1)))];
                let error = apply_json_diffs(serde_json::json!({}), &diffs).unwrap_err();
                assert!(
                    error.0.starts_with("invalid JSON path"),
                    "{path}: {}",
                    error.0
                );
            }
        }

        #[test]
        fn unknown_operation_names_are_rejected() {
            let diffs = [rec("$.a", "set", Some(serde_json::json!(1)))];
            let error = apply_json_diffs(serde_json::json!({}), &diffs).unwrap_err();
            assert!(error.0.contains("unknown diff operation"), "{}", error.0);
        }

        #[test]
        fn records_match_the_model_shape() {
            let records = [rec("$.a", "insert", Some(serde_json::json!(1)))];
            let serialized = serde_json::to_value(&records).unwrap();
            assert_eq!(serialized[0]["path"], "$.a");
            assert_eq!(serialized[0]["operation"], "insert");
            assert_eq!(serialized[0]["value"], 1);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use mysql_common::{
        binlog::{jsonb::Value as JsonbValue, jsondiff::JsonDiff},
        io::ParseBuf,
        proto::MyDeserialize,
    };
    use mysql_common::{constants::ColumnType, packets::Column};

    /// Parses binary jsonb bytes with the library, as a row value would be.
    fn jsonb(bytes: &[u8]) -> JsonbValue<'static> {
        let mut buf = ParseBuf(bytes);
        JsonbValue::deserialize((), &mut buf).unwrap().into_owned()
    }

    /// Parses one binary JSON diff (operation byte, path, optional value).
    fn diff(bytes: &[u8]) -> JsonDiff<'static> {
        let mut buf = ParseBuf(bytes);
        JsonDiff::deserialize((), &mut buf).unwrap().into_owned()
    }

    /// The maximum per-event budget (4M nodes), for the unit tests that
    /// exercise the per-value budget in isolation: the aliased-bomb cases
    /// decode up to 2^23 - 1 nodes, which only the per-value budget of one
    /// million may stop here.
    fn budget() -> EventNodeBudget {
        EventNodeBudget::new(u32::MAX)
    }

    fn json_column() -> Column {
        Column::new(ColumnType::MYSQL_TYPE_JSON).with_name(b"doc")
    }

    /// Binary jsonb for `{"a": 1, "b": [1, 2]}` (small object form).
    ///
    /// jsonb offsets are relative to the container start (its element-count
    /// field), and the size field counts the container from that point.
    const JSONB_DOC: &[u8] = &[
        0x00, // small object
        0x02, 0x00, // two members
        0x1e, 0x00, // 30 bytes from the element-count field on
        0x12, 0x00, 0x01, 0x00, // key entry: "a" at offset 18, length 1
        0x13, 0x00, 0x01, 0x00, // key entry: "b" at offset 19, length 1
        0x05, 0x01, 0x00, // value entry at offset 12: int16 1 (inlined)
        0x02, 0x14, 0x00, // value entry at offset 15: small array at offset 20
        b'a', b'b', // key bytes at offsets 18, 19
        0x02, 0x00, 0x0a, 0x00, // small array: two elements, 10 bytes
        0x05, 0x01, 0x00, // int16 1 (inlined)
        0x05, 0x02, 0x00, // int16 2 (inlined)
    ];

    #[test]
    fn jsonb_fixture_parses_as_expected() {
        let value = serde_json::Value::try_from(jsonb(JSONB_DOC)).unwrap();
        assert_eq!(value, serde_json::json!({"a": 1, "b": [1, 2]}));
    }

    /// Binary jsonb for a top-level opaque `NEWDECIMAL` value (what MySQL
    /// writes for a decimal literal or a DECIMAL column value inside JSON):
    /// type byte 0x0f, the column type (NEWDECIMAL = 246), a variable-length
    /// data length, and the packed binlog decimal (precision, scale, packed
    /// digits).
    fn opaque_decimal_jsonb() -> Vec<u8> {
        let dec = mysql_common::binlog::decimal::Decimal::parse_str_bytes(b"12.5").unwrap();
        let mut out = vec![0x0f, 0xf6, 4]; // OPAQUE, NEWDECIMAL, data length
        out.extend_from_slice(&[3, 1]); // precision 3, scale 1
        dec.write_bin(&mut out).unwrap();
        out
    }

    #[test]
    fn opaque_decimal_jsonb_renders_as_a_string_not_a_failure() {
        let mut nodes = 0usize;
        let value = jsonb_to_serde(
            &jsonb(&opaque_decimal_jsonb()),
            1,
            &mut nodes,
            &mut budget(),
        )
        .unwrap();
        assert_eq!(value, serde_json::json!("12.5"));
    }

    /// A hostile NEWDECIMAL opaque declaring scale > precision (the library
    /// computes the packed size as `precision - scale` and underflows) is a
    /// clean `InvalidData`, not a panic — found by the R47b fuzz scaffold.
    #[test]
    fn malformed_opaque_decimal_is_a_clean_error() {
        let dec = mysql_common::binlog::decimal::Decimal::parse_str_bytes(b"12.5").unwrap();
        let mut out = vec![0x0f, 0xf6, 4]; // OPAQUE, NEWDECIMAL, data length
        out.extend_from_slice(&[1, 3]); // precision 1, scale 3 (scale > precision)
        dec.write_bin(&mut out).unwrap();
        let mut nodes = 0usize;
        let error = jsonb_to_serde(&jsonb(&out), 1, &mut nodes, &mut budget()).unwrap_err();
        assert!(
            error.to_string().contains("scale exceeds precision"),
            "{error}"
        );
        // A truncated header (no precision/scale bytes at all) is the same.
        let truncated = vec![0x0f, 0xf6, 0];
        let error = jsonb_to_serde(&jsonb(&truncated), 1, &mut nodes, &mut budget()).unwrap_err();
        assert!(
            error
                .to_string()
                .contains("malformed jsonb opaque NEWDECIMAL"),
            "{error}"
        );
    }

    /// With a valid header, hostile packed digit groups still panic the
    /// dependency (a group value scaled by `10^(9 - digits)` overflows i32)
    /// and a release build wraps the overflow into silently wrong output —
    /// the round-2 verifier reproduced both from the fuzz target. Every
    /// group must stay below 10^digits after the sign masks are reapplied;
    /// legitimate positives and negatives pass byte-for-byte.
    #[test]
    fn hostile_packed_decimal_groups_are_rejected_legitimates_pass() {
        let mut nodes = 0usize;
        // Legitimate positive decimal: precision 9 scale 0, one full group
        // 999999999 (0x3b9ac9ff with the first byte's high bit set on the
        // wire).
        let ok = vec![0x0f, 0xf6, 6, 9, 0, 0xbb, 0x9a, 0xc9, 0xff];
        let value = jsonb_to_serde(&jsonb(&ok), 1, &mut nodes, &mut budget()).unwrap();
        assert_eq!(value, serde_json::json!("999999999"));
        // Legitimate negative decimal: all group bytes inverted, then the
        // first byte's high bit flipped again on the wire.
        let neg = vec![0x0f, 0xf6, 6, 9, 0, 0x44, 0x65, 0x36, 0x00];
        let value = jsonb_to_serde(&jsonb(&neg), 1, &mut nodes, &mut budget()).unwrap();
        assert_eq!(value, serde_json::json!("-999999999"));
        // One over the limit in the same group: 10^9 with the high bit set
        // (0xba9aca00) is rejected.
        let over = vec![0x0f, 0xf6, 6, 9, 0, 0xc0, 0x00, 0x00, 0x00];
        let error = jsonb_to_serde(&jsonb(&over), 1, &mut nodes, &mut budget()).unwrap_err();
        assert!(
            error.to_string().contains("digit group exceeds 9 digits"),
            "{error}"
        );
        // The verifier's panic shape: precision 5 scale 5 (suffix group of 5
        // digits, 3 bytes) with a group value whose scaling multiply
        // overflows i32 (first byte 0x83 = 0x03 with the sign bit set).
        let overflow = vec![0x0f, 0xf6, 5, 5, 5, 0x83, 0x46, 0xdc];
        let error = jsonb_to_serde(&jsonb(&overflow), 1, &mut nodes, &mut budget()).unwrap_err();
        assert!(
            error.to_string().contains("digit group exceeds 5 digits"),
            "{error}"
        );
        // Packed length disagreeing with precision/scale is rejected too.
        let short = vec![0x0f, 0xf6, 4, 9, 0, 0xbb, 0x9a];
        let error = jsonb_to_serde(&jsonb(&short), 1, &mut nodes, &mut budget()).unwrap_err();
        assert!(
            error
                .to_string()
                .contains("malformed jsonb opaque NEWDECIMAL"),
            "{error}"
        );
        // Precision 0 scale 0 packs zero bytes; the validator accepts it (the
        // library's jsonb framing rejects such opaques outright, so the
        // validator is asserted directly).
        validate_packed_decimal(&[0, 0]).unwrap();
    }

    /// A nested large jsonb array holding three elements: element 0 is the
    /// child container (or, at the bottom of a chain, another inlined
    /// int16), elements 1 and 2 are inlined int16s. Nested values carry no
    /// type byte of their own — the value entry names the type and points
    /// at the payload, so the payload starts with the element count field.
    fn nested_large_array(child: Option<&[u8]>) -> Vec<u8> {
        let mut out = vec![0x03, 0x00, 0x00, 0x00]; // payload: element count = 3
        let size_pos = out.len();
        out.extend(0u32.to_le_bytes()); // size counts from the element-count field
        if child.is_some() {
            out.push(0x03); // element 0: the child container
            out.extend(23u32.to_le_bytes()); // offset from the element-count field
        } else {
            out.push(0x05); // element 0: inlined int16
            out.extend(1i16.to_le_bytes());
            out.extend([0, 0]);
        }
        out.push(0x05); // elements 1 and 2: inlined int16s
        out.extend(1i16.to_le_bytes());
        out.extend([0, 0]);
        out.push(0x05);
        out.extend(1i16.to_le_bytes());
        out.extend([0, 0]);
        if let Some(child) = child {
            out.extend(child);
        }
        let total = out.len() as u32;
        out[size_pos..size_pos + 4].copy_from_slice(&total.to_le_bytes());
        out
    }

    /// A top-level large array wrapping a chain of `depth - 1` nested
    /// containers, so `depth` counts the containers in total. Large arrays
    /// carry 4-byte offsets and sizes, so deep chains cannot be built from
    /// the small format.
    fn deep_jsonb(depth: usize) -> Vec<u8> {
        let mut chain = nested_large_array(None);
        for _ in 1..depth - 1 {
            chain = nested_large_array(Some(&chain));
        }
        let mut out = vec![0x03];
        out.extend(3u32.to_le_bytes()); // real element count
        let size_pos = out.len();
        out.extend(0u32.to_le_bytes()); // size counts from the count field
        out.push(0x03); // element 0: the nested chain
        out.extend(23u32.to_le_bytes()); // offset from the count field
        out.push(0x05); // elements 1 and 2: inlined int16s
        out.extend(1i16.to_le_bytes());
        out.extend([0, 0]);
        out.push(0x05);
        out.extend(1i16.to_le_bytes());
        out.extend([0, 0]);
        out.extend(chain);
        let total = (out.len() - 1) as u32;
        out[size_pos..size_pos + 4].copy_from_slice(&total.to_le_bytes());
        out
    }

    #[test]
    fn jsonb_at_the_depth_cap_converts_and_beyond_is_rejected() {
        // MySQL's own limit is 100 nesting levels: 100 containers convert.
        // Every container holds its child plus two int16 scalars.
        let at_cap = jsonb(&deep_jsonb(MAX_JSONB_DEPTH));
        let mut expected = serde_json::json!([1, 1, 1]);
        for _ in 1..MAX_JSONB_DEPTH {
            expected = serde_json::Value::Array(vec![
                expected,
                serde_json::json!(1),
                serde_json::json!(1),
            ]);
        }
        let mut nodes = 0usize;
        assert_eq!(
            jsonb_to_serde(&at_cap, 1, &mut nodes, &mut budget()).unwrap(),
            expected
        );
        // One level deeper is hostile input: a clean InvalidData error, not
        // a stack overflow.
        let over = jsonb(&deep_jsonb(MAX_JSONB_DEPTH + 1));
        let mut nodes = 0usize;
        let err = jsonb_to_serde(&over, 1, &mut nodes, &mut budget()).unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::InvalidData);
        assert!(err.to_string().contains("nesting depth"), "{}", err);
    }

    /// The R48 aliased-jsonb bomb: `depth` chained small-array containers
    /// whose two entries both point at the same next container; the last
    /// container holds two inlined int16 leaves. Ten wire bytes per container
    /// plus the top-level type byte decode into `2^(depth+1) - 1` serde nodes.
    fn aliased_jsonb_bomb(depth: usize) -> Vec<u8> {
        let mut chain = [0x02, 0x00, 0x0a, 0x00, 0x05, 0x01, 0x00, 0x05, 0x01, 0x00].to_vec();
        for _ in 1..depth {
            let mut container = 2u16.to_le_bytes().to_vec();
            container.extend(((10 + chain.len()) as u16).to_le_bytes());
            // Both entries are small-array references to the same child at
            // offset 10 (relative to the element-count field).
            container.extend([0x02, 10, 0x00, 0x02, 10, 0x00]);
            container.extend_from_slice(&chain);
            chain = container;
        }
        let mut out = vec![0x02];
        out.extend_from_slice(&chain);
        out
    }

    #[test]
    fn node_budget_counts_containers_and_their_children() {
        let mut nodes = 0usize;
        jsonb_to_serde(&jsonb(JSONB_DOC), 1, &mut nodes, &mut budget()).unwrap();
        // The root object, its two member values, and the array's two
        // elements.
        assert_eq!(nodes, 5);
    }

    #[test]
    fn aliased_containers_amplify_until_the_node_budget_stops_them() {
        // 221 wire bytes used to decode into 2^23 - 1 = 8,388,607 serde
        // nodes (the R48 reproduction); the conversion now stops at the
        // budget with a clean InvalidData error.
        let bomb = aliased_jsonb_bomb(22);
        assert_eq!(bomb.len(), 221);
        let mut nodes = 0usize;
        let err = jsonb_to_serde(&jsonb(&bomb), 1, &mut nodes, &mut budget()).unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::InvalidData);
        assert!(
            err.to_string().contains("node budget of 1000000 nodes"),
            "{}",
            err
        );
        // An aliased document within the budget still converts, and the
        // counter reports the decoded node count exactly.
        let mut nodes = 0usize;
        jsonb_to_serde(
            &jsonb(&aliased_jsonb_bomb(18)),
            1,
            &mut nodes,
            &mut budget(),
        )
        .unwrap();
        assert_eq!(nodes, (1 << 19) - 1);
    }

    #[test]
    fn parse_diffs_shares_one_node_budget_across_the_sequence() {
        // The first diff's aliased value exhausts the shared budget; the
        // later legitimate value is dropped with it, so a sequence of
        // hostile diffs cannot each re-amplify to the limit. The losses are
        // reported through the problem note instead of failing.
        let mut bomb_diff = vec![0x00, 3, b'$', b'.', b'b']; // REPLACE $.b
        let bomb = aliased_jsonb_bomb(22);
        bomb_diff.push(bomb.len() as u8);
        bomb_diff.extend_from_slice(&bomb);
        let leaf_diff = [0x01, 3, b'$', b'.', b'c', 3, 0x0c, 1, b'x']; // INSERT $.c = "x"
        let diffs = [diff(&bomb_diff), diff(&leaf_diff)];
        let (parsed, problem) = json_diff::parse_diffs(&diffs, &mut budget());
        assert_eq!(parsed.len(), 2);
        assert_eq!(parsed[0].path, "$.b");
        assert_eq!(parsed[0].value, None);
        assert_eq!(parsed[1].value, None);
        let problem = problem.expect("the budget breach must be reported");
        assert!(
            problem.contains("node budget of 1000000 nodes"),
            "{problem}"
        );
    }

    /// REPLACE `$.b[1]` with 99, as written into a partial JSON after image.
    fn replace_b1_with_99() -> JsonDiff<'static> {
        diff(&[
            0x00, // REPLACE
            6, b'$', b'.', b'b', b'[', b'1', b']', // path
            3, 0x05, 99, 0x00, // int16 99
        ])
    }

    #[test]
    fn pair_applies_a_replace_diff_to_the_before_value() {
        let before = BinlogRow::new(
            vec![Some(BinlogValue::Jsonb(jsonb(JSONB_DOC)))],
            vec![json_column()].into(),
        );
        let after = BinlogRow::new(
            vec![Some(BinlogValue::JsonDiff(vec![replace_b1_with_99()]))],
            vec![json_column()].into(),
        );
        let (before, after) = decode_rows_pair(
            Some(&before),
            Some(&after),
            Some(&[true]),
            Some(&[true]),
            &[],
            &mut budget(),
        )
        .unwrap();
        assert_eq!(
            before.unwrap().columns[0].value,
            CellValue::Json(serde_json::json!({"a": 1, "b": [1, 2]}))
        );
        assert_eq!(
            after.unwrap().columns[0].value,
            CellValue::Json(serde_json::json!({"a": 1, "b": [1, 99]}))
        );
    }

    #[test]
    fn pair_applies_a_multi_diff_sequence_in_order() {
        let before = BinlogRow::new(
            vec![Some(BinlogValue::Jsonb(jsonb(JSONB_DOC)))],
            vec![json_column()].into(),
        );
        let after = BinlogRow::new(
            vec![Some(BinlogValue::JsonDiff(vec![
                replace_b1_with_99(),
                // INSERT `$.c` = "x": string jsonb (type 0x0c, varlen "x").
                diff(&[0x01, 3, b'$', b'.', b'c', 3, 0x0c, 1, b'x']),
                // REMOVE `$.a`.
                diff(&[0x02, 3, b'$', b'.', b'a']),
            ]))],
            vec![json_column()].into(),
        );
        let (_, after) = decode_rows_pair(
            Some(&before),
            Some(&after),
            Some(&[true]),
            Some(&[true]),
            &[],
            &mut budget(),
        )
        .unwrap();
        assert_eq!(
            after.unwrap().columns[0].value,
            CellValue::Json(serde_json::json!({"b": [1, 99], "c": "x"}))
        );
    }

    #[test]
    fn pair_records_a_loss_when_the_diff_cannot_apply() {
        let before = BinlogRow::new(
            vec![Some(BinlogValue::Jsonb(jsonb(JSONB_DOC)))],
            vec![json_column()].into(),
        );
        let after = BinlogRow::new(
            vec![Some(BinlogValue::JsonDiff(vec![
                // REMOVE `$.zz`: the member does not exist in the before value.
                diff(&[0x02, 4, b'$', b'.', b'z', b'z']),
            ]))],
            vec![json_column()].into(),
        );
        let (_, after) = decode_rows_pair(
            Some(&before),
            Some(&after),
            Some(&[true]),
            Some(&[true]),
            &[],
            &mut budget(),
        )
        .unwrap();
        let CellValue::PartialJson { diffs, reason } = after.unwrap().columns[0].value.clone()
        else {
            panic!("expected a partial JSON record");
        };
        assert_eq!(reason, "path `$.zz`: remove target does not exist");
        assert_eq!(diffs.len(), 1);
        assert_eq!(diffs[0].path, "$.zz");
        assert_eq!(diffs[0].operation, "remove");
        assert_eq!(diffs[0].value, None);
    }

    #[test]
    fn pair_records_a_loss_for_a_missing_or_non_json_before_value() {
        let diffs = || vec![replace_b1_with_99()];
        // The before image lacks the column entirely.
        let before = BinlogRow::new(vec![], vec![json_column()].into());
        let after = BinlogRow::new(
            vec![Some(BinlogValue::JsonDiff(diffs()))],
            vec![json_column()].into(),
        );
        let (_, after) = decode_rows_pair(
            Some(&before),
            Some(&after),
            Some(&[false]),
            Some(&[true]),
            &[],
            &mut budget(),
        )
        .unwrap();
        let CellValue::PartialJson { reason, .. } = after.unwrap().columns[0].value.clone() else {
            panic!("expected a partial JSON record");
        };
        assert_eq!(reason, "before column is absent");

        // The before image has no row at all.
        let after = BinlogRow::new(
            vec![Some(BinlogValue::JsonDiff(diffs()))],
            vec![json_column()].into(),
        );
        let (_, after) =
            decode_rows_pair(None, Some(&after), None, Some(&[true]), &[], &mut budget()).unwrap();
        let CellValue::PartialJson { reason, .. } = after.unwrap().columns[0].value.clone() else {
            panic!("expected a partial JSON record");
        };
        assert_eq!(reason, "no before image to apply diffs against");

        // The before value is an integer, not JSON.
        let before = BinlogRow::new(
            vec![Some(BinlogValue::Value(Value::Int(7)))],
            vec![json_column()].into(),
        );
        let after = BinlogRow::new(
            vec![Some(BinlogValue::JsonDiff(diffs()))],
            vec![json_column()].into(),
        );
        let (_, after) = decode_rows_pair(
            Some(&before),
            Some(&after),
            Some(&[true]),
            Some(&[true]),
            &[],
            &mut budget(),
        )
        .unwrap();
        let CellValue::PartialJson { reason, .. } = after.unwrap().columns[0].value.clone() else {
            panic!("expected a partial JSON record");
        };
        assert_eq!(reason, "before value is not JSON");
    }

    #[test]
    fn without_a_pair_context_diffs_are_recorded_not_fatal() {
        let row = BinlogRow::new(
            vec![Some(BinlogValue::JsonDiff(vec![replace_b1_with_99()]))],
            vec![json_column()].into(),
        );
        let image = decode_row(&row, &[true], &[], &mut budget()).unwrap();
        let CellValue::PartialJson { diffs, reason } = image.columns[0].value.clone() else {
            panic!("expected a partial JSON record");
        };
        assert_eq!(diffs.len(), 1);
        assert_eq!(diffs[0].operation, "replace");
        assert_eq!(diffs[0].value, Some(serde_json::json!(99)));
        assert_eq!(reason, "no before image to apply diffs against");
    }

    #[test]
    fn distinguishes_absent_null_and_arbitrary_bytes() {
        let columns = vec![
            Column::new(ColumnType::MYSQL_TYPE_LONG).with_name(b"nil"),
            Column::new(ColumnType::MYSQL_TYPE_BLOB).with_name(b"payload"),
        ];
        let row = BinlogRow::new(
            vec![
                Some(BinlogValue::Value(Value::NULL)),
                Some(BinlogValue::Value(Value::Bytes(vec![0, 255, 34]))),
            ],
            columns.into(),
        );
        let image = decode_row(
            &row,
            &[false, true, true],
            &[None, None, None],
            &mut budget(),
        )
        .unwrap();
        let json = serde_json::to_value(image).unwrap();
        assert_eq!(json["columns"][0]["value"]["type"], "absent");
        assert_eq!(json["columns"][1]["value"]["type"], "null");
        assert_eq!(json["columns"][1]["signedness"], "unknown");
        assert_eq!(json["columns"][2]["value"]["value"], "00ff22");
        assert_eq!(json["columns"][2]["index"], 2);
        assert_eq!(json["columns"][1]["mysql_type"], "LONG");
        assert_eq!(json["columns"][2]["mysql_type"], "BLOB");
    }
    #[test]
    fn rejects_inconsistent_bitmap() {
        let row = BinlogRow::new(
            vec![Some(BinlogValue::Value(Value::Int(4)))],
            vec![Column::new(ColumnType::MYSQL_TYPE_LONG)].into(),
        );
        assert!(decode_row(&row, &[false], &[None], &mut budget()).is_err());
    }
    #[test]
    fn present_bits_must_cover_all_columns() {
        assert!(present_bits([true, false].into_iter(), 2).is_ok());
        let err = present_bits([true].into_iter(), 2).unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::InvalidData);
        // Padding bits beyond n are ignored, not an error.
        assert!(present_bits([true, false, true, false].into_iter(), 2).is_ok());
    }
    #[test]
    fn column_type_names_are_canonical() {
        assert_eq!(
            column_type_name(ColumnType::MYSQL_TYPE_LONGLONG),
            "LONGLONG"
        );
        assert_eq!(column_type_name(ColumnType::MYSQL_TYPE_JSON), "JSON");
        assert_eq!(column_type_name(ColumnType::MYSQL_TYPE_VECTOR), "VECTOR");
    }
    #[test]
    fn enum_tlv_with_bogus_variant_count_is_rejected() {
        // ENUM_STR_VALUE TLV declaring an absurd variant count.
        let mut tlv = vec![6u8];
        tlv.push(4); // TLV length
        tlv.push(0xfe); // variant count: 8-byte length-encoded integer follows
        tlv.extend_from_slice(&u64::MAX.to_le_bytes()[..3]);
        let mut body = vec![0u8; 6 + 2]; // table id + flags
        body.push(1); // database name length
        body.extend_from_slice(b"d\0");
        body.push(1); // table name length
        body.extend_from_slice(b"t\0");
        body.push(1); // one column
        body.push(0x03); // LONG
        body.push(0); // no column metadata
        body.push(1); // one null-bitmap byte
        body.extend_from_slice(&tlv);
        let err = validate_table_map_metadata(&body, 8).unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::InvalidData);
    }

    #[test]
    fn enum_set_metadata_validates_every_column_list() {
        for field_type in [TLV_SET_STR_VALUE, TLV_ENUM_STR_VALUE] {
            // Two column lists, including empty names and a zero-variant
            // list, follow the same length-encoded representation.
            let valid = [1, 1, b'a', 2, 0, 1, b'b', 0];
            let mut tlv = vec![field_type, valid.len() as u8];
            tlv.extend(valid);
            validate_table_map_metadata(&body_with_lenient_header(&tlv), 8).unwrap();

            for suffix in [vec![0xfe], vec![2, 0], vec![1, 3, b'b']] {
                let mut value = vec![1, 1, b'a']; // valid first column
                value.extend(suffix);
                let mut tlv = vec![field_type, value.len() as u8];
                tlv.extend(value);
                let error =
                    validate_table_map_metadata(&body_with_lenient_header(&tlv), 8).unwrap_err();
                assert_eq!(error.kind(), io::ErrorKind::InvalidData);
            }
        }
    }
    #[test]
    fn well_formed_tlv_block_is_accepted() {
        // SIGNEDNESS for two numeric columns, plus ENUM_STR_VALUE with a
        // single variant "a".
        let mut tlv = vec![1u8, 1, 0];
        tlv.extend_from_slice(&[6, 3, 1, 1, b'a']);
        let mut body = vec![0u8; 6 + 2]; // table id + flags
        body.push(1);
        body.extend_from_slice(b"d\0");
        body.push(1);
        body.extend_from_slice(b"t\0");
        body.push(2); // two columns
        body.extend_from_slice(&[0x03, 0x03]); // two LONG columns
        body.push(0); // no column metadata
        body.push(1); // one null-bitmap byte
        body.extend_from_slice(&tlv);
        validate_table_map_metadata(&body, 8).unwrap();
    }
    #[test]
    fn unknown_tlv_type_is_rejected() {
        let tlv = vec![99u8, 1, 0];
        let mut body = vec![0u8; 6 + 2];
        body.push(1);
        body.extend_from_slice(b"d\0");
        body.push(1);
        body.extend_from_slice(b"t\0");
        body.push(1);
        body.push(0x03);
        body.push(0);
        body.push(1);
        body.extend_from_slice(&tlv);
        let err = validate_table_map_metadata(&body, 8).unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::InvalidData);
    }

    /// Builds a table map body whose optional metadata block is `tlv`, with a
    /// zero-prefix column count like the library's `ParseBuf` decoding yields.
    fn body_with_lenient_header(tlv: &[u8]) -> Vec<u8> {
        let mut body = vec![0u8; 6 + 2]; // table id + flags
        body.push(1);
        body.extend_from_slice(b"d\0");
        body.push(1);
        body.extend_from_slice(b"t\0");
        body.push(0xfb); // non-canonical zero column count
        body.push(0); // no column metadata
        body.extend_from_slice(tlv);
        body
    }

    #[test]
    fn noncanonical_lenenc_prefixes_mirror_the_library() {
        // The library decodes a 0xfb column count as zero and keeps parsing,
        // so the pre-walk must continue too: the unknown TLV type after the
        // header is what gets rejected, not the prefix.
        let tlv = vec![99u8, 1, 0];
        let err = validate_table_map_metadata(&body_with_lenient_header(&tlv), 8).unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::InvalidData);
        assert!(err.to_string().contains("optional metadata field type"));
    }

    #[test]
    fn enum_variant_count_lenient_prefix_is_accepted() {
        // A 0xff variant count decodes to zero variants in the library.
        let tlv = vec![6u8, 1, 0xff];
        let mut body = vec![0u8; 6 + 2];
        body.push(1);
        body.extend_from_slice(b"d\0");
        body.push(1);
        body.extend_from_slice(b"t\0");
        body.push(1);
        body.push(0x03);
        body.push(0);
        body.push(1);
        body.extend_from_slice(&tlv);
        validate_table_map_metadata(&body, 8).unwrap();
    }

    #[test]
    fn tlv_length_strict_prefix_is_rejected() {
        // TLV lengths go through the library's strict reader, so 0xff there
        // is an error, not a zero.
        let tlv = vec![1u8, 0xff, 0];
        let mut body = vec![0u8; 6 + 2]; // table id + flags
        body.push(1);
        body.extend_from_slice(b"d\0");
        body.push(1);
        body.extend_from_slice(b"t\0");
        body.push(1);
        body.push(0x03);
        body.push(0);
        body.push(1);
        body.extend_from_slice(&tlv);
        let err = validate_table_map_metadata(&body, 8).unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::InvalidData);
    }

    /// Builds a table map body with one LONG and one JSON column whose
    /// optional metadata block is `tlv`.
    fn body_with_long_and_json(tlv: &[u8]) -> Vec<u8> {
        let mut body = vec![0u8; 6 + 2]; // table id + flags
        body.push(1);
        body.extend_from_slice(b"d\0");
        body.push(1);
        body.extend_from_slice(b"t\0");
        body.push(2); // two columns
        body.extend_from_slice(&[0x03, 0xf5]); // LONG, JSON
        body.extend_from_slice(&[1, 4]); // JSON uses a four-byte length prefix
        body.push(1); // one null-bitmap byte
        body.extend_from_slice(tlv);
        body
    }

    #[test]
    fn partial_json_tlv_types_are_accepted() {
        // SIGNEDNESS (one numeric column, unsigned), PARTIAL_JSON_UPDATE
        // (one bit per column: only the JSON column is partial), and
        // UPDATED_COLUMN_TYPES (an opaque per-column type list).
        let mut tlv = vec![1u8, 1, 0x80];
        tlv.extend_from_slice(&[14, 1, 0x40]);
        tlv.extend_from_slice(&[15, 2, 0xf9, 0x00]);
        let body = body_with_long_and_json(&tlv);
        validate_table_map_metadata(&body, 8).unwrap();
        let ranges = unsupported_tlv_ranges(&body, 8).unwrap().unwrap();
        assert_eq!(ranges.len(), 2);
        // First range: the PARTIAL_JSON_UPDATE entry (type, length, value).
        let tlv_offset = body.len() - tlv.len();
        assert_eq!(ranges[0].start, tlv_offset + 3);
        assert_eq!(ranges[0].end, tlv_offset + 6);
        // Second range: the UPDATED_COLUMN_TYPES entry.
        assert_eq!(ranges[1].start, tlv_offset + 6);
        assert_eq!(ranges[1].end, tlv_offset + 6 + 4);
    }

    #[test]
    fn hostile_partial_json_tlv_lengths_are_rejected() {
        // PARTIAL_JSON_UPDATE declaring an 8-byte length-encoded size that
        // exceeds the body; the value length prefix must never overflow.
        let mut tlv = vec![14u8, 0xfe];
        tlv.extend_from_slice(&u64::MAX.to_le_bytes());
        let err = validate_table_map_metadata(&body_with_long_and_json(&tlv), 8).unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::InvalidData);
        assert!(err.to_string().contains("exceeds table map bounds"));
        // Same abuse for UPDATED_COLUMN_TYPES.
        let mut tlv = vec![15u8, 0xfe];
        tlv.extend_from_slice(&u64::MAX.to_le_bytes());
        let err = validate_table_map_metadata(&body_with_long_and_json(&tlv), 8).unwrap_err();
        assert_eq!(err.kind(), io::ErrorKind::InvalidData);
    }

    #[test]
    fn signedness_is_read_from_the_walk_not_the_library() {
        // Serialize a parsed table map whose metadata block carries
        // SIGNEDNESS plus the partial-JSON types the library rejects, then
        // check that signedness() still extracts the unsigned flag.
        let mut tlv = vec![1u8, 1, 0x80];
        tlv.extend_from_slice(&[14, 1, 0x40]);
        tlv.extend_from_slice(&[15, 2, 0xf9, 0x00]);
        let body = body_with_long_and_json(&tlv);
        let mut reader = mysql_common::binlog::EventStreamReader::new(
            mysql_common::binlog::consts::BinlogVersion::Version4,
        );
        let mut event = Vec::new();
        event.extend_from_slice(&0u32.to_le_bytes());
        event.push(19); // TABLE_MAP_EVENT
        event.extend_from_slice(&1u32.to_le_bytes()); // server id
        event.extend_from_slice(&(19 + body.len() as u32).to_le_bytes());
        event.extend_from_slice(&0u32.to_le_bytes()); // log pos
        event.extend_from_slice(&0u16.to_le_bytes()); // flags
        event.extend_from_slice(&body);
        reader
            .read(&mut std::io::Cursor::new(event))
            .unwrap()
            .unwrap();
        let map = reader.get_tme(0).unwrap();
        let signedness = signedness(map).unwrap();
        assert_eq!(signedness, vec![Some(true), None]);
    }
}