mf4-rs 4.2.0

mf4-rs is a Rust library for working with ASAM MDF 4 (Measurement Data Format) files.
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
//! Convert any MDF 4 file into a **column-oriented** MDF 4.20 file that is
//! optimised for reading a single signal from cloud storage.
//!
//! # Output layout
//!
//! ```text
//! ┌──────────────────────────────────────────────────────────────┐
//! │ ##ID  ##HD                                                   │  metadata:
//! │ ##FH / ##MD history, then per output group:                  │  one contiguous
//! │   ##DG ##CG ##CN (+ ##TX/##MD/##CC/##SI they reference)      │  region at the
//! │   ##DL listing the signal's data blocks                      │  front
//! ├──────────────────────────────────────────────────────────────┤
//! │ time 0:     ##DV ##DV ##DV …   (contiguous)                  │
//! │ signal A:   ##DV ##DV …        (contiguous)                  │  data: every
//! │ signal B:   ##DT …  ##SD       (VLSD stream right after)     │  signal's blocks
//! │ time 1 …                                                     │  back to back
//! ├──────────────────────────────────────────────────────────────┤
//! │ ##AT attachments (if any)                                    │
//! └──────────────────────────────────────────────────────────────┘
//! ```
//!
//! * Every signal lives in its **own data group / channel group** holding just
//!   that one channel ("column storage"). Its samples are written as a run of
//!   data blocks of about [`ColumnOptions::block_size`] bytes, placed directly
//!   after each other and listed by an equal-length `##DL` block, so one HTTP
//!   range request fetches the whole signal.
//! * Timestamps are **shared** using the MDF 4.2 *remote master* feature: the
//!   master (time) channel of each source group is written once into its own
//!   group, and every signal group points at it via `cg_cg_master` (channel
//!   group flag bit 3). With [`ColumnOptions::share_identical_time`] source
//!   groups whose time axes are byte-identical share one time group.
//! * Every non-data block sits in one region at the front of the file, so a
//!   reader learns the whole structure from a single prefix fetch. Since the
//!   data-block sizes are all derivable from the metadata (equal-length `##DL`
//!   lists plus the exact cycle counts), an index build never has to touch the
//!   data region.
//!
//! # Streaming
//!
//! The output layout is fully computed from the source metadata before any
//! sample is copied. The source data is then streamed **once** (in
//! [`ColumnOptions::read_chunk_size`] pieces) and each channel's bytes are
//! appended to a small per-channel buffer that is flushed straight to its
//! pre-computed position in the output. Memory stays bounded by
//! [`ColumnOptions::memory_budget`] regardless of file size; nothing is
//! decoded, so the conversion is lossless (raw bytes, conversions, units,
//! comments and sources are carried over).
//!
//! # Supported input
//!
//! Sorted and unsorted data groups, `##DT`/`##DV`/`##DL`/`##HL` data chains,
//! invalidation bits, VLSD (`##SD`) channels, sources, conversions (any
//! chain), file history and attachments. Not supported: compressed `##DZ`
//! blocks (rejected with an error). Channels that cannot be represented as a
//! single column — array (`##CA`) compositions and VLSD channels stored in a
//! VLSD channel group — are skipped and listed in
//! [`ColumnReport::skipped_channels`], as are structure (`##CN` composition)
//! and synchronization channels.
//!
//! Not carried over: events (`##EV`), the channel hierarchy (`##CH`),
//! channel attachment references and bus-event group flags. A group whose
//! remote master has a different record count is written without a time
//! axis (noted in [`ColumnReport::skipped_channels`]).

use std::collections::{HashMap, HashSet};
use std::hash::Hasher;

use crate::blocks::common::{read_string_block_via_reader, BlockHeader};
use crate::blocks::data_list_block::DataListBlock;
use crate::blocks::identification_block::IdentificationBlock;
use crate::error::MdfError;
use crate::index::ByteRangeReader;

/// Default payload size of one data block (4 MiB).
pub const DEFAULT_BLOCK_SIZE: u64 = 4 * 1024 * 1024;
/// Default cap on the memory used by the per-channel write buffers (64 MiB).
pub const DEFAULT_MEMORY_BUDGET: u64 = 64 * 1024 * 1024;
/// Default size of each sequential read from the source (4 MiB).
pub const DEFAULT_READ_CHUNK: u64 = 4 * 1024 * 1024;

/// Channel-group flag: the group stores VLSD data for another group's channel.
const CG_FLAG_VLSD: u16 = 1;
/// Channel-group flags: bus event / plain bus event group (a property of the
/// source group's channel set, which a single-column group no longer has).
const CG_FLAG_BUS_EVENTS: u16 = (1 << 1) | (1 << 2);
pub use crate::blocks::channel_group_block::CG_FLAG_REMOTE_MASTER;
const CN_FLAG_ALL_INVALID: u32 = 1;
const CN_FLAG_INVAL_BIT_VALID: u32 = 1 << 1;
const CN_FLAG_DEFAULT_X: u32 = 1 << 12;
/// Upper bound for a single metadata block (sanity check against corrupt links).
const MAX_METADATA_BLOCK: u64 = 256 * 1024 * 1024;
/// Smallest per-channel write buffer, whatever the memory budget.
const MIN_STREAM_BUFFER: usize = 4096;
/// Header of every MDF block.
const HDR: u64 = 24;

/// Tuning for [`to_column_oriented`] / [`to_column_oriented_with`].
#[derive(Debug, Clone)]
pub struct ColumnOptions {
    /// Target payload size of one data block, in bytes. Each block holds a
    /// whole number of records; a signal larger than this is split into
    /// several equally sized blocks written back to back and listed by a
    /// `##DL` block. Default [`DEFAULT_BLOCK_SIZE`] (4 MiB).
    pub block_size: u64,
    /// Upper bound for the per-channel write buffers in bytes (each channel
    /// gets `memory_budget / channels_in_group`, at least 4 KiB and at most
    /// one block). Default [`DEFAULT_MEMORY_BUDGET`] (64 MiB).
    ///
    /// Because of the 4 KiB floor, a data group with more than
    /// `memory_budget / 4096` channels uses more than `memory_budget`
    /// (4 KiB per channel).
    pub memory_budget: u64,
    /// Size of each sequential read from the source. Default 4 MiB.
    pub read_chunk_size: u64,
    /// Merge time channels of different source groups when they hold exactly
    /// the same raw timestamps (same layout, conversion and bytes), so all
    /// their signals reference a single time group. Costs one extra read of
    /// the candidate groups' data. Default `true`.
    pub share_identical_time: bool,
}

impl Default for ColumnOptions {
    fn default() -> Self {
        Self {
            block_size: DEFAULT_BLOCK_SIZE,
            memory_budget: DEFAULT_MEMORY_BUDGET,
            read_chunk_size: DEFAULT_READ_CHUNK,
            share_identical_time: true,
        }
    }
}

/// Summary of a column-oriented conversion.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct ColumnReport {
    /// Channel groups found in the source (excluding VLSD storage groups).
    pub source_groups: usize,
    /// Time (master) groups written.
    pub time_groups: usize,
    /// Signal groups written (one per signal).
    pub signal_groups: usize,
    /// Source groups whose time axis was identical to an earlier group's and
    /// therefore shares that group's time channel.
    pub shared_time_groups: usize,
    /// Data blocks written (`##DV`/`##DT`/`##SD`).
    pub data_blocks: usize,
    /// Byte offset where the data region starts: everything before it is
    /// metadata, so a reader fetching this prefix sees the whole structure.
    pub metadata_size: u64,
    /// Total size of the output file in bytes.
    pub file_size: u64,
    /// Channels that could not be represented as a column, as
    /// `"group/channel: reason"` (or `"group: reason"` for a group whose
    /// remote master had to be dropped).
    pub skipped_channels: Vec<String>,
}

/// Destination of a column-oriented conversion.
///
/// The output layout is computed up front, so data is written at known
/// absolute offsets rather than strictly sequentially.
pub trait ColumnSink {
    /// Write `bytes` at absolute file position `offset`. Positions never
    /// written before must read back as zero (true for a fresh file or a
    /// zero-filled buffer).
    fn write_at(&mut self, offset: u64, bytes: &[u8]) -> Result<(), MdfError>;

    /// Called once, before any write, with the final output size.
    fn set_size(&mut self, _size: u64) -> Result<(), MdfError> {
        Ok(())
    }
}

impl ColumnSink for Vec<u8> {
    fn write_at(&mut self, offset: u64, bytes: &[u8]) -> Result<(), MdfError> {
        let start = usize::try_from(offset)
            .map_err(|_| err("output offset exceeds the address space"))?;
        let end = start + bytes.len();
        if self.len() < end {
            self.resize(end, 0);
        }
        self[start..end].copy_from_slice(bytes);
        Ok(())
    }

    fn set_size(&mut self, size: u64) -> Result<(), MdfError> {
        let size = usize::try_from(size).map_err(|_| err("output exceeds the address space"))?;
        self.clear();
        self.resize(size, 0);
        Ok(())
    }
}

/// [`ColumnSink`] over anything that is `Write + Seek` (e.g. a `File`).
///
/// Seeks only when the next write is not at the current position.
pub struct SeekSink<W: std::io::Write + std::io::Seek> {
    inner: W,
    pos: Option<u64>,
}

impl<W: std::io::Write + std::io::Seek> SeekSink<W> {
    pub fn new(inner: W) -> Self {
        Self { inner, pos: None }
    }

    /// Flush and return the wrapped writer.
    pub fn into_inner(mut self) -> Result<W, MdfError> {
        self.inner.flush()?;
        Ok(self.inner)
    }
}

impl<W: std::io::Write + std::io::Seek> ColumnSink for SeekSink<W> {
    fn write_at(&mut self, offset: u64, bytes: &[u8]) -> Result<(), MdfError> {
        if self.pos != Some(offset) {
            self.inner.seek(std::io::SeekFrom::Start(offset))?;
        }
        self.inner.write_all(bytes)?;
        self.pos = Some(offset + bytes.len() as u64);
        Ok(())
    }

    /// Extends the output to `size` up front (by writing its last byte), so
    /// trailing alignment padding that no block covers is still present.
    fn set_size(&mut self, size: u64) -> Result<(), MdfError> {
        if size > 0 {
            self.write_at(size - 1, &[0])?;
        }
        Ok(())
    }
}

/// Convert the MDF file at `input` into a column-oriented MDF 4.20 file at
/// `output` (see the [module docs](self) for the layout).
///
/// The source is streamed once with positional reads; memory use is bounded by
/// [`ColumnOptions::memory_budget`]. Not available on `wasm32`; use
/// [`to_column_oriented_with`] there.
#[cfg(not(target_arch = "wasm32"))]
pub fn to_column_oriented(
    input: impl AsRef<std::path::Path>,
    output: impl AsRef<std::path::Path>,
    options: &ColumnOptions,
) -> Result<ColumnReport, MdfError> {
    let input = input.as_ref();
    let output = output.as_ref();
    if same_file(input, output) {
        return Err(err("input and output must be different files"));
    }
    let path = input
        .to_str()
        .ok_or_else(|| err("input path is not valid UTF-8"))?;
    // Plain positional reads rather than a memory map: the data is copied out
    // in chunks either way, and this keeps the resident set small.
    let mut reader = crate::index::FileRangeReader::new(path)?;
    let file = std::fs::File::create(output)?;
    let mut sink = SeekSink::new(std::io::BufWriter::with_capacity(1 << 20, file));
    let result = to_column_oriented_with(&mut reader, &mut sink, options)
        .and_then(|report| sink.into_inner().map(|_| report));
    if result.is_err() {
        // Best effort: do not leave a partial output file behind.
        let _ = std::fs::remove_file(output);
    }
    result
}

#[cfg(not(target_arch = "wasm32"))]
fn same_file(a: &std::path::Path, b: &std::path::Path) -> bool {
    match (std::fs::canonicalize(a), std::fs::canonicalize(b)) {
        (Ok(a), Ok(b)) => a == b,
        _ => false,
    }
}

/// Convert an in-memory MDF file into an in-memory column-oriented file.
pub fn to_column_oriented_bytes(
    input: &[u8],
    options: &ColumnOptions,
) -> Result<(Vec<u8>, ColumnReport), MdfError> {
    let mut reader = SliceReader { data: input };
    let mut out = Vec::new();
    let report = to_column_oriented_with(&mut reader, &mut out, options)?;
    Ok((out, report))
}

/// Core conversion over any [`ByteRangeReader`] source and [`ColumnSink`]
/// destination.
///
/// Metadata is read first; then the sample data of each data group is
/// streamed sequentially (plus one extra pass over the data groups whose
/// time axes are candidates for sharing, and a counting pass over unsorted
/// data groups).
pub fn to_column_oriented_with<R, S>(
    reader: &mut R,
    sink: &mut S,
    options: &ColumnOptions,
) -> Result<ColumnReport, MdfError>
where
    R: ByteRangeReader<Error = MdfError>,
    S: ColumnSink + ?Sized,
{
    if options.block_size == 0 {
        return Err(err("block_size must be greater than zero"));
    }
    let chunk = options.read_chunk_size.clamp(64 * 1024, 1 << 30) as usize;

    let mut src = walk_source(reader)?;
    for dg in 0..src.dgs.len() {
        count_records(reader, &mut src, dg, chunk)?;
    }
    let mut report = ColumnReport {
        source_groups: src.groups.iter().filter(|g| !g.is_vlsd_storage()).count(),
        ..Default::default()
    };
    let plan = plan_output(reader, &src, options, chunk, &mut report)?;
    let layout = build_layout(reader, &src, plan, options)?;

    sink.set_size(layout.file_size)?;
    for (addr, bytes) in &layout.fixed_writes {
        sink.write_at(*addr, bytes)?;
    }
    stream_data(reader, &src, &layout, options, chunk, sink)?;
    for c in &layout.copies {
        copy_range(reader, sink, c.from, c.to, c.len, chunk)?;
    }

    report.data_blocks = layout.groups.iter().map(|g| g.n_blocks as usize + usize::from(g.sd_len.is_some())).sum();
    report.metadata_size = layout.metadata_end;
    report.file_size = layout.file_size;
    Ok(report)
}

// ---------------------------------------------------------------------------
// Small helpers
// ---------------------------------------------------------------------------

fn err(msg: impl Into<String>) -> MdfError {
    MdfError::BlockSerializationError(msg.into())
}

fn align8(v: u64) -> u64 {
    (v + 7) & !7
}

fn u16_at(b: &[u8], o: usize) -> u16 {
    u16::from_le_bytes([b[o], b[o + 1]])
}

fn u32_at(b: &[u8], o: usize) -> u32 {
    u32::from_le_bytes(b[o..o + 4].try_into().unwrap())
}

fn u64_at(b: &[u8], o: usize) -> u64 {
    u64::from_le_bytes(b[o..o + 8].try_into().unwrap())
}

/// Borrowing in-memory reader.
struct SliceReader<'a> {
    data: &'a [u8],
}

impl ByteRangeReader for SliceReader<'_> {
    type Error = MdfError;

    fn read_range(&mut self, offset: u64, length: u64) -> Result<Vec<u8>, MdfError> {
        let end = offset
            .checked_add(length)
            .filter(|&e| e <= self.data.len() as u64)
            .ok_or(MdfError::TooShortBuffer {
                actual: self.data.len(),
                expected: offset.saturating_add(length).min(usize::MAX as u64) as usize,
                file: file!(),
                line: line!(),
            })?;
        Ok(self.data[offset as usize..end as usize].to_vec())
    }
}

/// A raw block split into its parts.
struct RawBlock {
    id: [u8; 4],
    links: Vec<u64>,
    data: Vec<u8>,
}

fn read_header<R>(reader: &mut R, addr: u64) -> Result<BlockHeader, MdfError>
where
    R: ByteRangeReader<Error = MdfError>,
{
    BlockHeader::from_bytes(&reader.read_range(addr, HDR)?)
}

fn read_block<R>(reader: &mut R, addr: u64) -> Result<RawBlock, MdfError>
where
    R: ByteRangeReader<Error = MdfError>,
{
    let header = read_header(reader, addr)?;
    let links_len = header
        .links_nr
        .checked_mul(8)
        .and_then(|l| l.checked_add(HDR))
        .filter(|&l| l <= header.block_len)
        .ok_or_else(|| err(format!("block at {addr:#x} has an inconsistent link count")))?;
    if header.block_len > MAX_METADATA_BLOCK {
        return Err(err(format!(
            "block {} at {addr:#x} is implausibly large ({} bytes)",
            header.id, header.block_len
        )));
    }
    let bytes = reader.read_range(addr, header.block_len)?;
    let mut id = [0u8; 4];
    id.copy_from_slice(&bytes[0..4]);
    let links = (0..header.links_nr as usize).map(|i| u64_at(&bytes, 24 + i * 8)).collect();
    Ok(RawBlock { id, links, data: bytes[links_len as usize..].to_vec() })
}

fn assemble(id: &[u8; 4], links: &[u64], data: &[u8]) -> Vec<u8> {
    let len = HDR as usize + links.len() * 8 + data.len();
    let mut out = Vec::with_capacity(len);
    out.extend_from_slice(id);
    out.extend_from_slice(&0u32.to_le_bytes());
    out.extend_from_slice(&(len as u64).to_le_bytes());
    out.extend_from_slice(&(links.len() as u64).to_le_bytes());
    for l in links {
        out.extend_from_slice(&l.to_le_bytes());
    }
    out.extend_from_slice(data);
    out
}

/// A `##TX`/`##MD` block holding `text` (NUL-terminated, 8-byte padded).
fn text_block(id: &[u8; 4], text: &str) -> Vec<u8> {
    let mut data = text.as_bytes().to_vec();
    data.push(0);
    data.resize(align8(HDR + data.len() as u64) as usize - HDR as usize, 0);
    assemble(id, &[], &data)
}

fn block_header_bytes(id: &[u8; 4], block_len: u64, links_nr: u64) -> [u8; 24] {
    let mut h = [0u8; 24];
    h[0..4].copy_from_slice(id);
    h[8..16].copy_from_slice(&block_len.to_le_bytes());
    h[16..24].copy_from_slice(&links_nr.to_le_bytes());
    h
}

// ---------------------------------------------------------------------------
// Source model
// ---------------------------------------------------------------------------

#[derive(Clone, Copy, PartialEq, Eq)]
enum BlockKind {
    /// `##DT`: records with record id and invalidation bytes.
    Dt,
    /// `##DV`: records with data bytes only.
    Dv,
}

#[derive(Clone)]
struct SrcBlock {
    data_offset: u64,
    len: u64,
    kind: BlockKind,
}

struct SrcChannel {
    name: Option<String>,
    links: Vec<u64>,
    /// Data section (after the links): 72 bytes for MDF 4.
    data: Vec<u8>,
    channel_type: u8,
    bit_offset: u8,
    byte_offset: u32,
    bit_count: u32,
    flags: u32,
    inval_pos: u32,
}

impl SrcChannel {
    fn is_master(&self) -> bool {
        self.channel_type == 2 || self.channel_type == 3
    }

    fn is_virtual(&self) -> bool {
        self.channel_type == 3 || self.channel_type == 6
    }

    /// Bytes this channel occupies in a record (in `u64`, so corrupt bit
    /// counts cannot overflow).
    fn byte_len(&self) -> u64 {
        if self.is_virtual() {
            0
        } else {
            (self.bit_offset as u64 + self.bit_count as u64).div_ceil(8)
        }
    }

    /// Whether the channel's bytes lie inside a record of `samples` data bytes.
    fn fits(&self, samples: u32) -> bool {
        self.bit_offset <= 7 && self.byte_offset as u64 + self.byte_len() <= samples as u64
    }
}

struct SrcGroup {
    dg: usize,
    cg_addr: u64,
    links: Vec<u64>,
    record_id: u64,
    flags: u16,
    path_separator: u16,
    samples: u32,
    inval_bytes: u32,
    name: Option<String>,
    channels: Vec<SrcChannel>,
    /// Records actually present in the data (filled by `count_records`).
    records: u64,
}

impl SrcGroup {
    fn is_vlsd_storage(&self) -> bool {
        self.flags & CG_FLAG_VLSD != 0
    }

    fn remote_master_link(&self) -> u64 {
        if self.flags & CG_FLAG_REMOTE_MASTER != 0 {
            self.links.get(6).copied().unwrap_or(0)
        } else {
            0
        }
    }

    fn label(&self) -> String {
        self.name.clone().unwrap_or_else(|| format!("group@{:#x}", self.cg_addr))
    }
}

struct SrcDg {
    rec_id_len: u8,
    comment: u64,
    blocks: Vec<SrcBlock>,
    groups: Vec<usize>,
}

struct Source {
    hd: RawBlock,
    dgs: Vec<SrcDg>,
    groups: Vec<SrcGroup>,
}

fn walk_source<R>(reader: &mut R) -> Result<Source, MdfError>
where
    R: ByteRangeReader<Error = MdfError>,
{
    IdentificationBlock::from_bytes(&reader.read_range(0, 64)?)?;
    let hd = read_block(reader, 64)?;
    if &hd.id != b"##HD" || hd.links.len() < 6 {
        return Err(MdfError::BlockIDError {
            actual: String::from_utf8_lossy(&hd.id).into_owned(),
            expected: "##HD".into(),
        });
    }

    let mut dgs = Vec::new();
    let mut groups = Vec::new();
    let mut visited = HashSet::new();
    let mut dg_addr = hd.links[0];
    while dg_addr != 0 {
        if !visited.insert(dg_addr) {
            return Err(MdfError::BlockLinkError(format!("cycle in ##DG chain at {dg_addr:#x}")));
        }
        let dg = read_block(reader, dg_addr)?;
        if &dg.id != b"##DG" || dg.links.len() < 4 || dg.data.is_empty() {
            return Err(err(format!("expected a ##DG block at {dg_addr:#x}")));
        }
        let dg_index = dgs.len();
        let mut src_dg = SrcDg {
            rec_id_len: dg.data[0],
            comment: dg.links[3],
            blocks: collect_data_blocks(reader, dg.links[2])?,
            groups: Vec::new(),
        };
        if !matches!(src_dg.rec_id_len, 0 | 1 | 2 | 4 | 8) {
            return Err(err(format!("invalid record id size {} at {dg_addr:#x}", src_dg.rec_id_len)));
        }

        let mut cg_addr = dg.links[1];
        while cg_addr != 0 {
            if !visited.insert(cg_addr) {
                return Err(MdfError::BlockLinkError(format!("cycle in ##CG chain at {cg_addr:#x}")));
            }
            let cg = read_block(reader, cg_addr)?;
            if &cg.id != b"##CG" || cg.links.len() < 6 || cg.data.len() < 32 {
                return Err(err(format!("expected a ##CG block at {cg_addr:#x}")));
            }
            let name = read_string_block_via_reader(reader, cg.links[2])?;
            let next_cg = cg.links[0];
            let mut channels = Vec::new();
            let mut cn_addr = cg.links[1];
            while cn_addr != 0 {
                if !visited.insert(cn_addr) {
                    return Err(MdfError::BlockLinkError(format!("cycle in ##CN chain at {cn_addr:#x}")));
                }
                let cn = read_block(reader, cn_addr)?;
                if &cn.id != b"##CN" || cn.links.len() < 8 || cn.data.len() < 72 {
                    return Err(err(format!("expected a ##CN block at {cn_addr:#x}")));
                }
                let d = &cn.data;
                channels.push(SrcChannel {
                    name: read_string_block_via_reader(reader, cn.links[2])?,
                    channel_type: d[0],
                    bit_offset: d[3],
                    byte_offset: u32_at(d, 4),
                    bit_count: u32_at(d, 8),
                    flags: u32_at(d, 12),
                    inval_pos: u32_at(d, 16),
                    links: cn.links.clone(),
                    data: cn.data.clone(),
                });
                cn_addr = cn.links[0];
            }
            src_dg.groups.push(groups.len());
            groups.push(SrcGroup {
                dg: dg_index,
                cg_addr,
                record_id: u64_at(&cg.data, 0),
                flags: u16_at(&cg.data, 16),
                path_separator: u16_at(&cg.data, 18),
                samples: u32_at(&cg.data, 24),
                inval_bytes: u32_at(&cg.data, 28),
                links: cg.links,
                name,
                channels,
                records: 0,
            });
            cg_addr = next_cg;
        }
        dgs.push(src_dg);
        dg_addr = dg.links[0];
    }
    Ok(Source { hd, dgs, groups })
}

/// Follow a data group's `##DT`/`##DV`/`##DL`/`##HL` chain.
fn collect_data_blocks<R>(reader: &mut R, addr: u64) -> Result<Vec<SrcBlock>, MdfError>
where
    R: ByteRangeReader<Error = MdfError>,
{
    let mut out = Vec::new();
    collect_chain(reader, addr, &mut out, |id| match id {
        b"##DT" => Some(BlockKind::Dt),
        b"##DV" => Some(BlockKind::Dv),
        _ => None,
    })?;
    Ok(out)
}

/// Follow a VLSD channel's `##SD`/`##DL`/`##HL` chain.
fn collect_sd_blocks<R>(reader: &mut R, addr: u64) -> Result<Vec<SrcBlock>, MdfError>
where
    R: ByteRangeReader<Error = MdfError>,
{
    let mut out = Vec::new();
    collect_chain(reader, addr, &mut out, |id| (id == b"##SD").then_some(BlockKind::Dt))?;
    Ok(out)
}

fn collect_chain<R>(
    reader: &mut R,
    mut addr: u64,
    out: &mut Vec<SrcBlock>,
    leaf: impl Fn(&[u8]) -> Option<BlockKind>,
) -> Result<(), MdfError>
where
    R: ByteRangeReader<Error = MdfError>,
{
    let mut visited = HashSet::new();
    while addr != 0 {
        if !visited.insert(addr) {
            return Err(MdfError::BlockLinkError(format!("cycle in data chain at {addr:#x}")));
        }
        let header = read_header(reader, addr)?;
        let id = header.id.as_bytes();
        if let Some(kind) = leaf(id) {
            out.push(SrcBlock { data_offset: addr + HDR, len: header.block_len.saturating_sub(HDR), kind });
            return Ok(());
        }
        match id {
            b"##DL" => {
                let dl = read_block(reader, addr)?;
                for &frag in dl.links.iter().skip(1) {
                    if frag == 0 {
                        continue;
                    }
                    let fh = read_header(reader, frag)?;
                    match leaf(fh.id.as_bytes()) {
                        Some(kind) => out.push(SrcBlock {
                            data_offset: frag + HDR,
                            len: fh.block_len.saturating_sub(HDR),
                            kind,
                        }),
                        None => return Err(unsupported_block(&fh.id, frag)),
                    }
                }
                addr = dl.links.first().copied().unwrap_or(0);
            }
            b"##HL" => {
                let hl = read_block(reader, addr)?;
                addr = hl.links.first().copied().unwrap_or(0);
            }
            _ => return Err(unsupported_block(&header.id, addr)),
        }
    }
    Ok(())
}

fn unsupported_block(id: &str, addr: u64) -> MdfError {
    if id == "##DZ" {
        err(format!(
            "compressed ##DZ data block at {addr:#x}: compressed MDF files are not supported"
        ))
    } else {
        MdfError::BlockIDError { actual: id.to_string(), expected: "a data block".into() }
    }
}

// ---------------------------------------------------------------------------
// Record streaming
// ---------------------------------------------------------------------------

/// Framing information for the records of one data group.
struct Framing {
    id_len: usize,
    /// `(record id, group position in dg.groups, record bytes after the id, is VLSD storage)`
    by_id: HashMap<u64, (usize, usize, bool)>,
    sorted: bool,
}

impl Framing {
    fn new(src: &Source, dg: usize) -> Result<Self, MdfError> {
        let d = &src.dgs[dg];
        let sorted = d.groups.len() == 1;
        let mut by_id = HashMap::new();
        for (pos, &g) in d.groups.iter().enumerate() {
            let grp = &src.groups[g];
            let size = grp.samples as usize + grp.inval_bytes as usize;
            if !sorted && by_id.insert(grp.record_id, (pos, size, grp.is_vlsd_storage())).is_some() {
                return Err(err(format!("duplicate record id {} in an unsorted data group", grp.record_id)));
            }
            if sorted {
                by_id.insert(0, (pos, size, grp.is_vlsd_storage()));
            }
        }
        if !sorted && d.rec_id_len == 0 {
            return Err(err("data group with several channel groups but no record ids"));
        }
        Ok(Self { id_len: d.rec_id_len as usize, by_id, sorted })
    }

    /// Length of the record at the start of `buf` and its group position, or
    /// `None` if `buf` does not hold a complete record yet. `avail` is the
    /// number of bytes left in the data group counting from the start of
    /// `buf`; a record that cannot fit in them is an error (a truncated record
    /// or a corrupt VLSD length), so nothing is ever buffered beyond the data.
    fn next(
        &self,
        buf: &[u8],
        avail: u64,
        kind: BlockKind,
        dv_size: usize,
    ) -> Result<Option<(usize, usize, usize, bool)>, MdfError> {
        let need = |len: usize| -> Result<bool, MdfError> {
            if len as u64 > avail {
                return Err(err(format!(
                    "data ends inside a record ({len}-byte record, {avail} bytes left; truncated data or corrupt record length)"
                )));
            }
            Ok(buf.len() >= len)
        };
        if kind == BlockKind::Dv {
            return Ok(need(dv_size)?.then_some((dv_size, 0, 0, false)));
        }
        if self.sorted {
            let (pos, size, _) = self.by_id[&0];
            let len = self.id_len + size;
            return Ok(need(len)?.then_some((len, pos, self.id_len, false)));
        }
        if !need(self.id_len)? {
            return Ok(None);
        }
        let id = match self.id_len {
            1 => buf[0] as u64,
            2 => u16_at(buf, 0) as u64,
            4 => u32_at(buf, 0) as u64,
            _ => u64_at(buf, 0),
        };
        let &(pos, size, vlsd) = self
            .by_id
            .get(&id)
            .ok_or_else(|| err(format!("unknown record id {id} in unsorted data group")))?;
        if vlsd {
            if !need(self.id_len + 4)? {
                return Ok(None);
            }
            let len = self.id_len + 4 + u32_at(buf, self.id_len) as usize;
            return Ok(need(len)?.then_some((len, pos, self.id_len, true)));
        }
        let len = self.id_len + size;
        Ok(need(len)?.then_some((len, pos, self.id_len, false)))
    }
}

/// Stream every record of data group `dg` once, calling `f(group_position,
/// record_without_id)` for each fixed-length record (VLSD storage records are
/// skipped). Records may straddle data-block boundaries. Returns the record
/// count per group position.
fn stream_records<R, F>(
    reader: &mut R,
    src: &Source,
    dg: usize,
    chunk: usize,
    mut f: F,
) -> Result<Vec<u64>, MdfError>
where
    R: ByteRangeReader<Error = MdfError>,
    F: FnMut(usize, &[u8]) -> Result<(), MdfError>,
{
    let d = &src.dgs[dg];
    let mut counts = vec![0u64; d.groups.len()];
    if d.groups.is_empty() {
        return Ok(counts);
    }
    let framing = Framing::new(src, dg)?;
    let first = &src.groups[d.groups[0]];
    let dv_size = first.samples as usize;
    if d.blocks.iter().any(|b| b.kind == BlockKind::Dv) {
        if !framing.sorted || first.inval_bytes != 0 {
            return Err(err(
                "##DV data blocks are only supported for sorted groups without invalidation bytes",
            ));
        }
        if dv_size == 0 {
            return Ok(counts);
        }
    }
    if framing.sorted && framing.id_len + first.samples as usize + first.inval_bytes as usize == 0 {
        return Ok(counts);
    }

    // Sorted data: read whole records per chunk so records only need
    // re-assembly where they straddle a data-block boundary.
    let chunk = if framing.sorted {
        let dt_len = framing.id_len + first.samples as usize + first.inval_bytes as usize;
        let rec = dt_len.max(dv_size).max(1);
        (chunk / rec).max(1) * rec
    } else {
        chunk
    };
    let mut pending: Vec<u8> = Vec::new();
    let mut kind = BlockKind::Dt;
    // Data-group bytes not read yet.
    let mut unread: u64 = d.blocks.iter().map(|b| b.len).sum();
    for blk in &d.blocks {
        if blk.kind != kind && !pending.is_empty() {
            return Err(err("a record straddles a ##DT/##DV block boundary"));
        }
        kind = blk.kind;
        let mut off = 0u64;
        while off < blk.len {
            let n = (blk.len - off).min(chunk as u64);
            let bytes = reader.read_range(blk.data_offset + off, n)?;
            off += n;
            unread -= n;
            let buf: &[u8] = if pending.is_empty() {
                &bytes
            } else {
                pending.extend_from_slice(&bytes);
                &pending
            };
            let mut pos = 0usize;
            while pos < buf.len() {
                let avail = (buf.len() - pos) as u64 + unread;
                let Some((len, gpos, id_len, vlsd)) = framing.next(&buf[pos..], avail, kind, dv_size)? else {
                    break;
                };
                if !vlsd {
                    f(gpos, &buf[pos + id_len..pos + len])?;
                }
                counts[gpos] += 1;
                pos += len;
            }
            pending = buf[pos..].to_vec();
        }
    }
    Ok(counts)
}

/// Determine the number of records of every group in data group `dg`.
///
/// Sorted groups are counted from the data-block sizes (no data is read);
/// unsorted groups need a pass over the records.
fn count_records<R>(reader: &mut R, src: &mut Source, dg: usize, chunk: usize) -> Result<(), MdfError>
where
    R: ByteRangeReader<Error = MdfError>,
{
    let d = &src.dgs[dg];
    if d.groups.len() == 1 {
        let g = &src.groups[d.groups[0]];
        let dt_size = d.rec_id_len as u64 + g.samples as u64 + g.inval_bytes as u64;
        let (mut dt, mut dv) = (0u64, 0u64);
        for b in &d.blocks {
            match b.kind {
                BlockKind::Dt => dt += b.len,
                BlockKind::Dv => dv += b.len,
            }
        }
        let n = dt.checked_div(dt_size).unwrap_or(0) + dv.checked_div(g.samples as u64).unwrap_or(0);
        let gi = d.groups[0];
        src.groups[gi].records = n;
        return Ok(());
    }
    let counts = stream_records(reader, src, dg, chunk, |_, _| Ok(()))?;
    let groups = src.dgs[dg].groups.clone();
    for (pos, g) in groups.into_iter().enumerate() {
        src.groups[g].records = counts[pos];
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// Planning
// ---------------------------------------------------------------------------

/// One output data group (= one channel group = one channel).
struct OutGroup {
    src_group: usize,
    channel: usize,
    is_time: bool,
    /// Index (into the output groups) of the time group, for signal groups.
    master: Option<usize>,
    /// Bytes copied from each source record.
    data_bytes: u32,
    /// Whether an invalidation byte follows the data bytes.
    inval: bool,
    records: u64,
    /// VLSD `##SD` source fragments (empty = fixed-length channel).
    sd_blocks: Vec<SrcBlock>,
    vlsd: bool,
}

impl OutGroup {
    fn record_size(&self) -> u64 {
        self.data_bytes as u64 + u64::from(self.inval)
    }
}

/// Decide which output groups to write and which time group each signal uses.
fn plan_output<R>(
    reader: &mut R,
    src: &Source,
    options: &ColumnOptions,
    chunk: usize,
    report: &mut ColumnReport,
) -> Result<Vec<OutGroup>, MdfError>
where
    R: ByteRangeReader<Error = MdfError>,
{
    // Local master channel of each source group.
    let master_of: Vec<Option<usize>> = src
        .groups
        .iter()
        .map(|g| {
            if g.is_vlsd_storage() {
                return None;
            }
            g.channels.iter().position(|c| c.is_master() && c.fits(g.samples))
        })
        .collect();

    // Group representing each source group's time axis (identical axes merge).
    let mut rep: Vec<usize> = (0..src.groups.len()).collect();
    if options.share_identical_time {
        merge_identical_time(reader, src, &master_of, chunk, &mut rep)?;
    }
    report.shared_time_groups =
        (0..src.groups.len()).filter(|&g| master_of[g].is_some() && rep[g] != g).count();

    let mut out: Vec<OutGroup> = Vec::new();
    let mut time_out: HashMap<usize, usize> = HashMap::new();
    let addr_to_group: HashMap<u64, usize> =
        src.groups.iter().enumerate().map(|(i, g)| (g.cg_addr, i)).collect();

    for (gi, g) in src.groups.iter().enumerate() {
        if g.is_vlsd_storage() {
            continue;
        }
        // Which source group provides this group's time axis? A remote master
        // is only usable when it has one sample per record of this group.
        let time_src = match master_of[gi] {
            Some(_) => Some(rep[gi]),
            None => {
                let remote = addr_to_group.get(&g.remote_master_link()).copied().filter(|&m| master_of[m].is_some());
                match remote {
                    Some(m) if src.groups[m].records == g.records => Some(rep[m]),
                    Some(m) => {
                        report.skipped_channels.push(format!(
                            "{}: remote master dropped ({} records, master group has {})",
                            g.label(),
                            g.records,
                            src.groups[m].records
                        ));
                        None
                    }
                    None => None,
                }
            }
        };
        // The time group is written the first time any group needs it.
        let master = time_src.map(|t| {
            *time_out.entry(t).or_insert_with(|| {
                let tg = &src.groups[t];
                let ch = &tg.channels[master_of[t].unwrap()];
                out.push(OutGroup {
                    src_group: t,
                    channel: master_of[t].unwrap(),
                    is_time: true,
                    master: None,
                    data_bytes: ch.byte_len() as u32, // fits in the u32 record
                    inval: has_inval(tg, ch),
                    records: tg.records,
                    sd_blocks: Vec::new(),
                    vlsd: false,
                });
                out.len() - 1
            })
        });

        for (ci, ch) in g.channels.iter().enumerate() {
            if Some(ci) == master_of[gi] {
                continue;
            }
            let label = || format!("{}/{}", g.label(), ch.name.as_deref().unwrap_or("<unnamed>"));
            if !ch.fits(g.samples) {
                report.skipped_channels.push(format!("{}: lies outside the record", label()));
                continue;
            }
            if ch.is_master() {
                report.skipped_channels.push(format!("{}: additional master channel", label()));
                continue;
            }
            if ch.channel_type == 4 {
                // Its cn_data link (attachment/stream reference) is not carried over.
                report.skipped_channels.push(format!("{}: synchronization channels are not supported", label()));
                continue;
            }
            if ch.links[1] != 0 {
                let id = read_header(reader, ch.links[1])?.id;
                if id == "##CA" || id == "##CN" {
                    let kind = if id == "##CA" { "array (##CA)" } else { "structure (##CN composition)" };
                    report.skipped_channels.push(format!("{}: {kind} channels are not supported", label()));
                    continue;
                }
            }
            let mut sd_blocks = Vec::new();
            let mut vlsd = false;
            if ch.channel_type == 1 && ch.links[5] != 0 {
                let target = read_header(reader, ch.links[5])?;
                if target.id == "##CG" {
                    report.skipped_channels.push(format!(
                        "{}: VLSD data stored in a VLSD channel group is not supported",
                        label()
                    ));
                    continue;
                }
                sd_blocks = collect_sd_blocks(reader, ch.links[5])?;
                vlsd = true;
            }
            out.push(OutGroup {
                src_group: gi,
                channel: ci,
                is_time: false,
                master,
                data_bytes: ch.byte_len() as u32,
                inval: has_inval(g, ch),
                records: g.records,
                sd_blocks,
                vlsd,
            });
        }
    }
    report.time_groups = out.iter().filter(|o| o.is_time).count();
    report.signal_groups = out.len() - report.time_groups;
    Ok(out)
}

fn has_inval(g: &SrcGroup, ch: &SrcChannel) -> bool {
    ch.flags & CN_FLAG_ALL_INVALID == 0
        && ch.flags & CN_FLAG_INVAL_BIT_VALID != 0
        && (ch.inval_pos / 8) < g.inval_bytes
}

/// `(byte index in the record after the id, bit mask)` of the channel's
/// invalidation bit, when it uses one.
fn inval_bit(g: &SrcGroup, ch: &SrcChannel) -> Option<(usize, u8)> {
    has_inval(g, ch).then(|| {
        let bit = ch.inval_pos as usize;
        (g.samples as usize + bit / 8, 1u8 << (bit % 8))
    })
}

/// Point `rep[g]` at the first group whose time axis is byte-identical to
/// `g`'s. Only groups with the same record count and master layout are
/// compared, so files without shareable axes pay nothing.
fn merge_identical_time<R>(
    reader: &mut R,
    src: &Source,
    master_of: &[Option<usize>],
    chunk: usize,
    rep: &mut [usize],
) -> Result<(), MdfError>
where
    R: ByteRangeReader<Error = MdfError>,
{
    type Key = (u64, u8, u8, u8, u32, u64, u32, u64, Option<String>, bool, Vec<u8>);
    let mut by_key: HashMap<Key, Vec<usize>> = HashMap::new();
    for (g, m) in master_of.iter().enumerate() {
        let Some(m) = *m else { continue };
        let grp = &src.groups[g];
        let ch = &grp.channels[m];
        let key = (
            grp.records,
            ch.channel_type,
            ch.data[2],
            ch.bit_offset,
            ch.bit_count,
            ch.byte_len(),
            ch.flags,
            ch.links[4], // same conversion block
            read_string_block_via_reader(reader, ch.links[6])?, // same unit
            has_inval(grp, ch),
            ch.data[..2].to_vec(),
        );
        by_key.entry(key).or_default().push(g);
    }
    let candidates: Vec<Vec<usize>> = by_key.into_values().filter(|v| v.len() > 1).collect();
    if candidates.is_empty() {
        return Ok(());
    }

    // Hash the raw master bytes of every candidate, one pass per data group.
    let mut hashes: HashMap<usize, (u64, u64)> = HashMap::new();
    let wanted: HashSet<usize> = candidates.iter().flatten().copied().collect();
    for (dgi, d) in src.dgs.iter().enumerate() {
        let targets: Vec<(usize, usize)> = d
            .groups
            .iter()
            .enumerate()
            .filter(|(_, g)| wanted.contains(g))
            .map(|(pos, &g)| (pos, g))
            .collect();
        if targets.is_empty() {
            continue;
        }
        let mut hashers: HashMap<usize, ChunkedHasher> =
            targets.iter().map(|&(pos, _)| (pos, ChunkedHasher::default())).collect();
        // (byte offset, byte length, invalidation bit) of each master.
        type Span = (usize, usize, Option<(usize, u8)>);
        let spans: HashMap<usize, Span> = targets
            .iter()
            .map(|&(pos, g)| {
                let grp = &src.groups[g];
                let ch = &grp.channels[master_of[g].unwrap()];
                (pos, (ch.byte_offset as usize, ch.byte_len() as usize, inval_bit(grp, ch)))
            })
            .collect();
        stream_records(reader, src, dgi, chunk, |pos, rec| {
            if let (Some(h), Some(&(o, l, inval))) = (hashers.get_mut(&pos), spans.get(&pos)) {
                h.write(&rec[o..o + l]);
                if let Some((byte, mask)) = inval {
                    h.write(&[u8::from(rec[byte] & mask != 0)]);
                }
            }
            Ok(())
        })?;
        for (pos, g) in targets {
            hashes.insert(g, hashers.remove(&pos).unwrap().finish());
        }
    }

    for group in candidates {
        let mut firsts: Vec<usize> = Vec::new();
        for g in group {
            match firsts.iter().find(|&&f| hashes[&f] == hashes[&g]) {
                Some(&f) => rep[g] = f,
                None => firsts.push(g),
            }
        }
    }
    Ok(())
}

/// 128-bit content hash that does not depend on how the input is split.
#[derive(Default)]
struct ChunkedHasher {
    buf: Vec<u8>,
    a: std::collections::hash_map::DefaultHasher,
    b: std::collections::hash_map::DefaultHasher,
    len: u64,
}

impl ChunkedHasher {
    const BLOCK: usize = 64 * 1024;

    fn write(&mut self, bytes: &[u8]) {
        self.buf.extend_from_slice(bytes);
        self.len += bytes.len() as u64;
        if self.buf.len() >= Self::BLOCK {
            self.drain();
        }
    }

    fn drain(&mut self) {
        self.a.write(&self.buf);
        self.b.write_u64(0x9e37_79b9_7f4a_7c15);
        self.b.write(&self.buf);
        self.buf.clear();
    }

    fn finish(mut self) -> (u64, u64) {
        self.drain();
        self.a.write_u64(self.len);
        (self.a.finish(), self.b.finish())
    }
}

// ---------------------------------------------------------------------------
// Layout
// ---------------------------------------------------------------------------

/// Growable metadata region starting at absolute offset `base`.
struct MetaBuf {
    base: u64,
    buf: Vec<u8>,
}

impl MetaBuf {
    fn new(base: u64) -> Self {
        Self { base, buf: Vec::new() }
    }

    fn end(&self) -> u64 {
        self.base + self.buf.len() as u64
    }

    fn reserve(&mut self, len: u64) -> u64 {
        let addr = align8(self.end());
        self.buf.resize((addr + len - self.base) as usize, 0);
        addr
    }

    fn alloc(&mut self, bytes: &[u8]) -> u64 {
        let addr = self.reserve(bytes.len() as u64);
        self.put(addr, bytes);
        addr
    }

    fn put(&mut self, addr: u64, bytes: &[u8]) {
        let start = (addr - self.base) as usize;
        self.buf[start..start + bytes.len()].copy_from_slice(bytes);
    }
}

/// Deep-copies metadata blocks (`##TX`, `##MD`, `##CC`, `##SI`, `##FH`),
/// remapping their links; each source block is copied once.
#[derive(Default)]
struct Copier {
    memo: HashMap<u64, u64>,
    visiting: HashSet<u64>,
}

impl Copier {
    fn copy<R>(&mut self, reader: &mut R, buf: &mut MetaBuf, addr: u64) -> Result<u64, MdfError>
    where
        R: ByteRangeReader<Error = MdfError>,
    {
        self.copy_inner(reader, buf, addr, 0)
    }

    fn copy_inner<R>(&mut self, reader: &mut R, buf: &mut MetaBuf, addr: u64, depth: u32) -> Result<u64, MdfError>
    where
        R: ByteRangeReader<Error = MdfError>,
    {
        if addr == 0 {
            return Ok(0);
        }
        if let Some(&out) = self.memo.get(&addr) {
            return Ok(out);
        }
        if depth > 64 || self.visiting.contains(&addr) {
            return Err(MdfError::ConversionChainCycle { address: addr });
        }
        let mut blk = read_block(reader, addr)?;
        match &blk.id {
            b"##TX" | b"##MD" | b"##CC" | b"##SI" => {}
            // Chains (`next` links) are walked iteratively by the caller.
            // (`##AT` blocks are streamed by `copy_attachments` instead.)
            b"##FH" => {
                if let Some(next) = blk.links.first_mut() {
                    *next = 0;
                }
            }
            _ => return Ok(0),
        }
        self.visiting.insert(addr);
        for l in blk.links.iter_mut() {
            *l = self.copy_inner(reader, buf, *l, depth + 1)?;
        }
        self.visiting.remove(&addr);
        let out = buf.alloc(&assemble(&blk.id, &blk.links, &blk.data));
        self.memo.insert(addr, out);
        Ok(out)
    }

    /// Copy a `next`-linked `##FH` chain; returns the first and last
    /// copied addresses.
    fn copy_chain<R>(&mut self, reader: &mut R, buf: &mut MetaBuf, mut addr: u64) -> Result<(u64, u64), MdfError>
    where
        R: ByteRangeReader<Error = MdfError>,
    {
        let (mut first, mut last) = (0u64, 0u64);
        let mut seen = HashSet::new();
        while addr != 0 && seen.insert(addr) {
            let next = read_block(reader, addr)?.links.first().copied().unwrap_or(0);
            let out = self.copy(reader, buf, addr)?;
            if out == 0 {
                break;
            }
            if last != 0 {
                buf.put(last + HDR, &out.to_le_bytes());
            } else {
                first = out;
            }
            last = out;
            addr = next;
        }
        Ok((first, last))
    }
}

/// Final placement of one output group.
struct PlacedGroup {
    out: OutGroup,
    /// Payload bytes per data block (a whole number of records, 8-byte aligned).
    block_payload: u64,
    n_blocks: u64,
    total_bytes: u64,
    data_addr: u64,
    block_id: [u8; 4],
    sd_addr: u64,
    sd_len: Option<u64>,
}

impl PlacedGroup {
    fn block_addr(&self, i: u64) -> u64 {
        self.data_addr + i * (HDR + self.block_payload)
    }

    fn block_len(&self, i: u64) -> u64 {
        if i + 1 < self.n_blocks {
            self.block_payload
        } else {
            self.total_bytes - i * self.block_payload
        }
    }
}

/// Source bytes copied verbatim to the output in chunks.
struct StreamCopy {
    from: u64,
    to: u64,
    len: u64,
}

struct Layout {
    groups: Vec<PlacedGroup>,
    /// Pre-built metadata to write verbatim: `(address, bytes)`.
    fixed_writes: Vec<(u64, Vec<u8>)>,
    /// Large payloads (embedded attachments) streamed from the source.
    copies: Vec<StreamCopy>,
    metadata_end: u64,
    file_size: u64,
}

fn build_layout<R>(
    reader: &mut R,
    src: &Source,
    plan: Vec<OutGroup>,
    options: &ColumnOptions,
) -> Result<Layout, MdfError>
where
    R: ByteRangeReader<Error = MdfError>,
{
    const ID_LEN: u64 = 64;
    const HD_LEN: u64 = 104;
    let mut meta = MetaBuf::new(ID_LEN + HD_LEN);
    let mut copier = Copier::default();

    // File-level metadata: header comment, file history (+ our own entry).
    let hd_comment = copier.copy(reader, &mut meta, src.hd.links[5])?;
    let (mut fh_first, fh_last) = copier.copy_chain(reader, &mut meta, src.hd.links[1])?;
    let fh_md = meta.alloc(&text_block(
        b"##MD",
        &format!(
            "<FHcomment xmlns=\"http://www.asam.net/mdf/v4\"><TX>Converted to column-oriented MDF 4.20 \
             (one signal per channel group, shared time channels)</TX><tool_id>mf4-rs</tool_id>\
             <tool_vendor>mf4-rs</tool_vendor><tool_version>{}</tool_version></FHcomment>",
            env!("CARGO_PKG_VERSION")
        ),
    ));
    let mut fh_data = Vec::with_capacity(16);
    fh_data.extend_from_slice(&now_ns().to_le_bytes());
    fh_data.extend_from_slice(&[0u8; 8]); // tz/dst offsets, time flags (UTC), reserved
    let our_fh = meta.alloc(&assemble(b"##FH", &[0, fh_md], &fh_data));
    if fh_last != 0 {
        meta.put(fh_last + HDR, &our_fh.to_le_bytes());
    } else {
        fh_first = our_fh;
    }

    // Metadata referenced by each output group (shared blocks copied once).
    struct GroupMeta {
        dg_comment: u64,
        cg_links: [u64; 3], // acq name, acq source, comment
        cn_links: [u64; 5], // name, source, conversion, unit, comment
    }
    let mut metas = Vec::with_capacity(plan.len());
    for og in &plan {
        let g = &src.groups[og.src_group];
        let ch = &g.channels[og.channel];
        let mut cg_links = [0u64; 3];
        for (slot, &l) in cg_links.iter_mut().zip(&[g.links[2], g.links[3], g.links[5]]) {
            *slot = copier.copy(reader, &mut meta, l)?;
        }
        let mut cn_links = [0u64; 5];
        for (slot, &l) in cn_links.iter_mut().zip(&[ch.links[2], ch.links[3], ch.links[4], ch.links[6], ch.links[7]]) {
            *slot = copier.copy(reader, &mut meta, l)?;
        }
        metas.push(GroupMeta {
            dg_comment: copier.copy(reader, &mut meta, src.dgs[g.dg].comment)?,
            cg_links,
            cn_links,
        });
    }

    // Data placement: compute block geometry per group.
    let mut placed: Vec<PlacedGroup> = plan
        .into_iter()
        .map(|out| {
            let rs = out.record_size();
            let total = rs * out.records;
            let (payload, n) = if total == 0 {
                (0, 0)
            } else {
                // Whole records per block, payload a multiple of 8 so the
                // blocks of one signal sit back to back without padding.
                let step = 8 / gcd(rs, 8);
                let per_block = ((options.block_size / rs) / step * step).max(step);
                let payload = per_block * rs;
                if total <= payload { (total, 1) } else { (payload, total.div_ceil(payload)) }
            };
            PlacedGroup {
                block_id: if out.inval { *b"##DT" } else { *b"##DV" },
                out,
                block_payload: payload,
                n_blocks: n,
                total_bytes: total,
                data_addr: 0,
                sd_addr: 0,
                sd_len: None,
            }
        })
        .collect();

    // Structural blocks, in output order: DG, CG, CN (+ DL).
    struct Addrs {
        dg: u64,
        cg: u64,
        cn: u64,
        dl: u64,
    }
    let mut addrs = Vec::with_capacity(placed.len());
    for p in &placed {
        let dg = meta.reserve(64);
        let cg = meta.reserve(if p.out.master.is_some() { 112 } else { 104 });
        let cn = meta.reserve(160);
        // Data is always referenced through an equal-length ##DL (even a
        // single block), so readers learn every block size from the metadata
        // prefix and never have to read a header in the data region.
        let dl = if p.n_blocks > 0 { meta.reserve(HDR + (p.n_blocks + 1) * 8 + 16) } else { 0 };
        addrs.push(Addrs { dg, cg, cn, dl });
    }
    let metadata_end = align8(meta.end());

    let mut cursor = metadata_end;
    for p in placed.iter_mut() {
        if p.n_blocks > 0 {
            p.data_addr = cursor;
            cursor = align8(p.block_addr(p.n_blocks - 1) + HDR + p.block_len(p.n_blocks - 1));
        }
        if p.out.vlsd {
            let len: u64 = p.out.sd_blocks.iter().map(|b| b.len).sum();
            p.sd_addr = cursor;
            p.sd_len = Some(len);
            cursor = align8(cursor + HDR + len);
        }
    }

    // Attachments go after the data so they never bloat the metadata prefix.
    let mut tail = Vec::new();
    let mut copies = Vec::new();
    let (at_first, file_size) = copy_attachments(reader, &mut copier, src.hd.links[3], cursor, &mut tail, &mut copies)?;

    // Fill in the structural blocks.
    for (i, (p, a)) in placed.iter().zip(&addrs).enumerate() {
        let m = &metas[i];
        let g = &src.groups[p.out.src_group];
        let ch = &g.channels[p.out.channel];
        let next_dg = addrs.get(i + 1).map_or(0, |n| n.dg);
        let data_link = a.dl;

        let mut dg_data = [0u8; 8];
        dg_data[0] = 0; // record id size
        meta.put(a.dg, &assemble(b"##DG", &[next_dg, a.cg, data_link, m.dg_comment], &dg_data));

        let mut cg_links = vec![0, a.cn, m.cg_links[0], m.cg_links[1], 0, m.cg_links[2]];
        let mut flags = g.flags & !(CG_FLAG_VLSD | CG_FLAG_REMOTE_MASTER | CG_FLAG_BUS_EVENTS);
        if let Some(t) = p.out.master {
            cg_links.push(addrs[t].cg);
            flags |= CG_FLAG_REMOTE_MASTER;
        }
        let mut cg_data = Vec::with_capacity(32);
        cg_data.extend_from_slice(&0u64.to_le_bytes()); // record id
        cg_data.extend_from_slice(&p.out.records.to_le_bytes());
        cg_data.extend_from_slice(&flags.to_le_bytes());
        cg_data.extend_from_slice(&g.path_separator.to_le_bytes());
        cg_data.extend_from_slice(&0u32.to_le_bytes());
        cg_data.extend_from_slice(&p.out.data_bytes.to_le_bytes());
        cg_data.extend_from_slice(&u32::from(p.out.inval).to_le_bytes());
        meta.put(a.cg, &assemble(b"##CG", &cg_links, &cg_data));

        let mut cn_data = ch.data[..72].to_vec();
        let mut cn_type = ch.channel_type;
        if cn_type == 5 {
            cn_type = 0; // maximum-length channel: keep the full bytes, drop the size link
        }
        cn_data[0] = cn_type;
        cn_data[4..8].copy_from_slice(&0u32.to_le_bytes()); // byte offset
        let mut cn_flags = ch.flags & !CN_FLAG_DEFAULT_X;
        if !p.out.inval {
            cn_flags &= !CN_FLAG_INVAL_BIT_VALID;
        }
        cn_data[12..16].copy_from_slice(&cn_flags.to_le_bytes());
        cn_data[16..20].copy_from_slice(&0u32.to_le_bytes()); // invalidation bit position
        cn_data[22..24].copy_from_slice(&0u16.to_le_bytes()); // attachment count
        let sd_link = if p.sd_len.is_some() { p.sd_addr } else { 0 };
        let cn_links = [0, 0, m.cn_links[0], m.cn_links[1], m.cn_links[2], sd_link, m.cn_links[3], m.cn_links[4]];
        meta.put(a.cn, &assemble(b"##CN", &cn_links, &cn_data));

        if p.n_blocks > 0 {
            let links = (0..p.n_blocks).map(|b| p.block_addr(b)).collect();
            let dl = DataListBlock::new_equal(links, p.block_payload);
            meta.put(a.dl, &dl.to_bytes()?);
        }
    }

    // ##ID + ##HD.
    let id = IdentificationBlock {
        version_identifier: "4.20    ".into(),
        version_number: 420,
        ..Default::default()
    };
    let first_dg = addrs.first().map_or(0, |a| a.dg);
    // Always the 104-byte MDF 4 ##HD (32-byte data section): the metadata
    // region is placed right after it.
    let mut hd_data = src.hd.data[..src.hd.data.len().min(32)].to_vec();
    hd_data.resize(32, 0);
    let hd = assemble(b"##HD", &[first_dg, fh_first, 0, at_first, 0, hd_comment], &hd_data);

    let mut fixed_writes = vec![(0, id.to_bytes()?), (64, hd)];
    let MetaBuf { base, buf } = meta;
    fixed_writes.push((base, buf));
    fixed_writes.extend(tail);
    Ok(Layout { groups: placed, fixed_writes, copies, metadata_end, file_size })
}

/// Size of the fixed part of an `##AT` data section (flags, creator index,
/// reserved, MD5, original and embedded size) before the embedded payload.
const AT_FIXED_DATA: u64 = 40;

/// Lay out the `##AT` chain starting at `addr` from output offset `cursor`.
///
/// Each attachment's header, links and fixed fields (plus the `##TX`/`##MD`
/// blocks it references) go to `writes`; its embedded payload, which can be
/// arbitrarily large, is recorded in `copies` and streamed later. Returns the
/// first copied attachment's address and the end of the laid-out region.
fn copy_attachments<R>(
    reader: &mut R,
    copier: &mut Copier,
    mut addr: u64,
    mut cursor: u64,
    writes: &mut Vec<(u64, Vec<u8>)>,
    copies: &mut Vec<StreamCopy>,
) -> Result<(u64, u64), MdfError>
where
    R: ByteRangeReader<Error = MdfError>,
{
    let mut first = 0u64;
    // Index into `writes` of the previous attachment (to patch its `next`).
    let mut prev: Option<usize> = None;
    let mut seen = HashSet::new();
    while addr != 0 && seen.insert(addr) {
        let header = read_header(reader, addr)?;
        if header.id != "##AT" {
            break;
        }
        let links_len = header
            .links_nr
            .checked_mul(8)
            .and_then(|l| l.checked_add(HDR))
            .filter(|&l| header.links_nr >= 4 && l <= header.block_len && l <= MAX_METADATA_BLOCK)
            .ok_or_else(|| err(format!("##AT block at {addr:#x} has an inconsistent link count")))?;
        let fixed_len = header.block_len.min(links_len + AT_FIXED_DATA);
        let mut fixed = reader.read_range(addr, fixed_len)?;
        let next = u64_at(&fixed, HDR as usize);

        // Referenced metadata (file name, MIME type, comment) first.
        let mut refs = MetaBuf::new(cursor);
        fixed[HDR as usize..HDR as usize + 8].copy_from_slice(&0u64.to_le_bytes());
        for i in 1..header.links_nr as usize {
            let at = HDR as usize + i * 8;
            let out = copier.copy(reader, &mut refs, u64_at(&fixed, at))?;
            fixed[at..at + 8].copy_from_slice(&out.to_le_bytes());
        }
        cursor = align8(refs.end());
        if !refs.buf.is_empty() {
            writes.push((refs.base, refs.buf));
        }

        let out = cursor;
        if let Some(p) = prev {
            writes[p].1[HDR as usize..HDR as usize + 8].copy_from_slice(&out.to_le_bytes());
        } else {
            first = out;
        }
        prev = Some(writes.len());
        writes.push((out, fixed));
        if header.block_len > fixed_len {
            copies.push(StreamCopy { from: addr + fixed_len, to: out + fixed_len, len: header.block_len - fixed_len });
        }
        cursor = align8(out + header.block_len);
        addr = next;
    }
    Ok((first, cursor))
}

fn gcd(a: u64, b: u64) -> u64 {
    if b == 0 { a } else { gcd(b, a % b) }
}

/// Current time in ns since the Unix epoch (for the `##FH` entry).
fn now_ns() -> u64 {
    #[cfg(not(target_arch = "wasm32"))]
    {
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_nanos() as u64)
            .unwrap_or(0)
    }
    #[cfg(all(target_arch = "wasm32", feature = "wasm"))]
    {
        (js_sys::Date::now() * 1e6) as u64
    }
    #[cfg(all(target_arch = "wasm32", not(feature = "wasm")))]
    {
        0
    }
}

// ---------------------------------------------------------------------------
// Data streaming
// ---------------------------------------------------------------------------

/// Buffered writer for one output group's logical data stream; inserts the
/// data-block headers at the pre-computed positions.
struct ColumnStream<'a> {
    group: &'a PlacedGroup,
    written: u64,
    buf: Vec<u8>,
    cap: usize,
}

impl ColumnStream<'_> {
    #[inline]
    fn push<S: ColumnSink + ?Sized>(&mut self, bytes: &[u8], sink: &mut S) -> Result<(), MdfError> {
        self.buf.extend_from_slice(bytes);
        if self.buf.len() >= self.cap {
            self.flush(sink)?;
        }
        Ok(())
    }

    fn flush<S: ColumnSink + ?Sized>(&mut self, sink: &mut S) -> Result<(), MdfError> {
        let g = self.group;
        if self.written + self.buf.len() as u64 > g.total_bytes {
            return Err(err("more records than counted while streaming"));
        }
        let mut rest: &[u8] = &self.buf;
        while !rest.is_empty() {
            let block = self.written / g.block_payload;
            let pos = self.written % g.block_payload;
            let addr = g.block_addr(block);
            if pos == 0 {
                sink.write_at(addr, &block_header_bytes(&g.block_id, HDR + g.block_len(block), 0))?;
            }
            let n = ((g.block_len(block) - pos) as usize).min(rest.len());
            sink.write_at(addr + HDR + pos, &rest[..n])?;
            self.written += n as u64;
            rest = &rest[n..];
        }
        self.buf.clear();
        Ok(())
    }
}

/// How to extract one output column from a source record.
struct Extract {
    stream: usize,
    offset: usize,
    len: usize,
    /// `(byte index in the record after the id, bit mask)` of the
    /// invalidation bit.
    inval: Option<(usize, u8)>,
}

fn stream_data<R, S>(
    reader: &mut R,
    src: &Source,
    layout: &Layout,
    options: &ColumnOptions,
    chunk: usize,
    sink: &mut S,
) -> Result<(), MdfError>
where
    R: ByteRangeReader<Error = MdfError>,
    S: ColumnSink + ?Sized,
{
    // Output groups fed by each source group (time groups once, by their
    // representative source group).
    let mut by_src: Vec<Vec<&PlacedGroup>> = (0..src.groups.len()).map(|_| Vec::new()).collect();
    for p in &layout.groups {
        by_src[p.out.src_group].push(p);
    }
    for (dgi, d) in src.dgs.iter().enumerate() {
        let mut streams: Vec<ColumnStream> = Vec::new();
        let mut extracts: Vec<Vec<Extract>> = (0..d.groups.len()).map(|_| Vec::new()).collect();
        for (pos, &p) in d.groups.iter().enumerate().flat_map(|(pos, &g)| by_src[g].iter().map(move |p| (pos, p))) {
            if p.total_bytes == 0 {
                continue;
            }
            let g = &src.groups[p.out.src_group];
            let ch = &g.channels[p.out.channel];
            let inval = if p.out.inval { inval_bit(g, ch) } else { None };
            extracts[pos].push(Extract {
                stream: streams.len(),
                offset: ch.byte_offset as usize,
                len: p.out.data_bytes as usize,
                inval,
            });
            streams.push(ColumnStream { group: p, written: 0, buf: Vec::new(), cap: 0 });
        }

        if !streams.is_empty() {
            let cap = (options.memory_budget / streams.len() as u64)
                .clamp(MIN_STREAM_BUFFER as u64, (options.block_size.max(MIN_STREAM_BUFFER as u64)).min(1 << 30))
                as usize;
            for s in streams.iter_mut() {
                s.cap = cap;
                s.buf.reserve(cap.min(s.group.total_bytes as usize) + 64);
            }
            stream_records(reader, src, dgi, chunk, |pos, rec| {
                for e in &extracts[pos] {
                    let s = &mut streams[e.stream];
                    s.push(&rec[e.offset..e.offset + e.len], sink)?;
                    if let Some((byte, mask)) = e.inval {
                        s.push(&[u8::from(rec[byte] & mask != 0)], sink)?;
                    }
                }
                Ok(())
            })?;
            for s in streams.iter_mut() {
                s.flush(sink)?;
                if s.written != s.group.total_bytes {
                    return Err(err(format!(
                        "record count changed while streaming ({} of {} bytes)",
                        s.written, s.group.total_bytes
                    )));
                }
            }
        }

        // VLSD signal data: copy each channel's ##SD stream verbatim right
        // after its records, so the inline offsets stay valid.
        for p in d.groups.iter().flat_map(|&g| &by_src[g]) {
            let Some(sd_len) = p.sd_len else { continue };
            sink.write_at(p.sd_addr, &block_header_bytes(b"##SD", HDR + sd_len, 0))?;
            let mut at = p.sd_addr + HDR;
            for b in &p.out.sd_blocks {
                copy_range(reader, sink, b.data_offset, at, b.len, chunk)?;
                at += b.len;
            }
        }
    }
    Ok(())
}

/// Copy `len` source bytes at `from` to output offset `to`, `chunk` at a time.
fn copy_range<R, S>(reader: &mut R, sink: &mut S, from: u64, to: u64, len: u64, chunk: usize) -> Result<(), MdfError>
where
    R: ByteRangeReader<Error = MdfError>,
    S: ColumnSink + ?Sized,
{
    let mut off = 0;
    while off < len {
        let n = (len - off).min(chunk as u64);
        sink.write_at(to + off, &reader.read_range(from + off, n)?)?;
        off += n;
    }
    Ok(())
}