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
use crate::error::MdfError;
use crate::parsing::raw_channel_group::RawChannelGroup;
use crate::parsing::record_stream::RecordSections;
use crate::blocks::{
data_block::DataBlock,
data_group_block::DataGroupBlock,
data_list_block::DataListBlock,
common::BlockHeader,
common::BlockParse,
};
#[derive(Debug)]
pub struct RawDataGroup {
pub block: DataGroupBlock,
pub channel_groups: Vec<RawChannelGroup>,
}
impl RawDataGroup {
/// The records of channel group `cg` (one of this data group's), read as
/// one continuous stream across all data blocks: records split between
/// two blocks of a `##DL` list are reassembled.
pub(crate) fn record_sections<'a>(
&self,
cg: &RawChannelGroup,
mmap: &'a [u8],
) -> Result<RecordSections<'a>, MdfError> {
let record_size = self.block.record_id_len as usize
+ cg.block.samples_byte_nr as usize
+ cg.block.invalidation_bytes_nr as usize;
if record_size == 0 {
return Ok(RecordSections::new(Vec::new(), 0));
}
Ok(RecordSections::new(self.data_blocks(mmap)?, record_size))
}
/// Collect all data blocks referenced by this data group.
///
/// The returned vector contains the `DT` or `DV` blocks in the order they
/// appear on disk, transparently following any `DL` list chains.
///
/// # Arguments
/// * `mmap` - Memory mapped file containing the MDF data
///
/// # Returns
/// A vector of [`DataBlock`] objects or an [`MdfError`] if parsing fails.
pub fn data_blocks<'a>(
&self,
mmap: &'a [u8],
) -> Result<Vec<DataBlock<'a>>, MdfError> {
// Unsorted data groups interleave records of several channel groups
// (each prefixed by a record id). Framing them as fixed-size records
// of a single group silently mis-decodes the data, so refuse loudly.
// Metadata access (names, channels) is unaffected — only record/data
// access goes through here.
if self.block.record_id_len > 0 && self.channel_groups.len() > 1 {
return Err(MdfError::BlockSerializationError(
"unsorted data groups (multiple channel groups per data group) are not supported"
.to_string(),
));
}
let mut collected_blocks = Vec::new();
// Start at the group’s primary data pointer
let mut current_block_address = self.block.data_block_addr;
let mut visited = std::collections::HashSet::new();
while current_block_address != 0 {
if !visited.insert(current_block_address) {
return Err(MdfError::BlockLinkError(format!(
"cycle detected in data block chain at address {:#x}",
current_block_address
)));
}
let byte_offset = current_block_address as usize;
// Read the block header (bounds-checked)
let header_bytes = mmap
.get(byte_offset..byte_offset.saturating_add(24))
.ok_or(MdfError::TooShortBuffer {
actual: mmap.len(),
expected: byte_offset.saturating_add(24),
file: file!(),
line: line!(),
})?;
let block_header = BlockHeader::from_bytes(header_bytes)?;
match block_header.id.as_str() {
"##DT" | "##DV" => {
// Single contiguous DataBlock
let data_block = DataBlock::from_bytes(&mmap[byte_offset..])?;
collected_blocks.push(data_block);
// No list to follow, we’re done
current_block_address = 0;
}
"##DL" => {
// Fragmented list of data blocks
let data_list_block = DataListBlock::from_bytes(&mmap[byte_offset..])?;
// Parse each fragment in this list
for &fragment_address in &data_list_block.data_links {
if fragment_address == 0 {
continue; // null link
}
let fragment_offset = fragment_address as usize;
let fragment_bytes =
mmap.get(fragment_offset..).ok_or(MdfError::TooShortBuffer {
actual: mmap.len(),
expected: fragment_offset.saturating_add(24),
file: file!(),
line: line!(),
})?;
let fragment_block = DataBlock::from_bytes(fragment_bytes)?;
collected_blocks.push(fragment_block);
}
// Move to the next DLBLOCK in the chain (0 = end)
current_block_address = data_list_block.next;
}
unexpected_id => {
return Err(MdfError::BlockIDError {
actual: unexpected_id.to_string(),
expected: "##DT / ##DV / ##DL".to_string(),
});
}
}
}
Ok(collected_blocks)
}
}