biodream 0.2.7

Zero-copy, streaming-capable toolkit for reading and writing BIOPAC AcqKnowledge (.acq) 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
//! Marker header and journal section parser.
//!
//! Layout (follows the channel data section in uncompressed files, and follows
//! channel dtype headers in compressed files):
//!
//! 1. Marker header: `lLength` (total marker section bytes), `lNumMarkers` (count).
//! 2. Per-marker record: `lSample`, `nChannel`, `szStyle[4]`, `lMarkerTextLen`,
//!    then `lMarkerTextLen` bytes of label text (may contain embedded NULLs).
//!    In v4.4.0+ (revision ≥ 77) an additional 8-byte i64 timestamp follows.
//! 3. Journal: `lJournalLen` (i32) followed by that many bytes of text.
//!
//! The journal parser is fault-tolerant: corruption produces a
//! [`Warning`], not an error, and the
//! [`Datafile`](crate::domain::Datafile) is still returned.

use alloc::{string::String, vec, vec::Vec};
use std::io::{Read, Seek};

use crate::{
    domain::{Journal, Marker, MarkerStyle, Timestamp},
    error::{BiopacError, Warning},
};

/// Minimum revision that stores a per-marker creation timestamp.
const REVISION_TIMESTAMP: i32 = 77;

/// Size of the fixed marker section header (lLength + lNumMarkers).
const MARKER_HDR_BYTES: i32 = 8;

/// Size of the fixed per-marker record fields before the variable text.
/// lSample(4) + nChannel(2) + szStyle(4) + lMarkerTextLen(4) = 14
const MARKER_FIXED_BYTES: usize = 14;

/// Extra bytes added per-marker in v4.4.0+ for the creation timestamp.
const MARKER_TIMESTAMP_BYTES: usize = 8;

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

fn read_i16_le<R: Read>(r: &mut R) -> Result<i16, BiopacError> {
    let mut buf = [0u8; 2];
    r.read_exact(&mut buf).map_err(BiopacError::Io)?;
    Ok(i16::from_le_bytes(buf))
}

fn read_i32_le<R: Read>(r: &mut R) -> Result<i32, BiopacError> {
    let mut buf = [0u8; 4];
    r.read_exact(&mut buf).map_err(BiopacError::Io)?;
    Ok(i32::from_le_bytes(buf))
}

fn read_i64_le<R: Read>(r: &mut R) -> Result<i64, BiopacError> {
    let mut buf = [0u8; 8];
    r.read_exact(&mut buf).map_err(BiopacError::Io)?;
    Ok(i64::from_le_bytes(buf))
}

fn read_bytes<R: Read>(r: &mut R, n: usize) -> Result<Vec<u8>, BiopacError> {
    let mut buf = vec![0u8; n];
    r.read_exact(&mut buf).map_err(BiopacError::Io)?;
    Ok(buf)
}

/// Decode raw bytes to a String: try UTF-8, fall back to Latin-1.
/// Text is terminated at the first NUL byte if present.
fn decode_text(bytes: &[u8]) -> String {
    // Trim at first NUL.
    let trimmed = bytes
        .iter()
        .position(|&b| b == 0)
        .map_or(bytes, |pos| bytes.get(..pos).unwrap_or(bytes));

    core::str::from_utf8(trimmed).map_or_else(
        |_| trimmed.iter().map(|&b| char::from(b)).collect(),
        Into::into,
    )
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Result of parsing the marker + journal sections.
pub(crate) struct MarkersAndJournal {
    pub markers: Vec<Marker>,
    pub journal: Option<Journal>,
    pub warnings: Vec<Warning>,
}

/// Parse the marker section and optional journal from the current reader position.
///
/// `file_revision` controls whether per-marker timestamps are read.
/// `channel_display_orders` maps channel data-index → display order; the
/// marker's `nChannel` value is a display-order index, so this slice is used
/// to find the correct channel position.
///
/// If the journal section is corrupt or missing, a warning is emitted and
/// `journal` is `None`.
pub(crate) fn parse_markers_and_journal<R: Read + Seek>(
    reader: &mut R,
    file_revision: i32,
    channel_display_orders: &[u16],
) -> Result<MarkersAndJournal, BiopacError> {
    let mut warnings: Vec<Warning> = Vec::new();

    // --- Marker section header ---------------------------------------------
    let total_length = read_i32_le(reader)?; // includes the 8-byte header itself
    let num_markers = read_i32_le(reader)?;

    if num_markers < 0 {
        return Err(BiopacError::Validation(alloc::format!(
            "invalid lNumMarkers: {num_markers}"
        )));
    }

    #[expect(clippy::cast_sign_loss, reason = "num_markers checked >= 0 above")]
    let num_markers = num_markers as usize;

    // Compute the byte offset at which the journal begins.
    // total_length includes the 8-byte header. We've read 8 bytes so far.
    let has_timestamp = file_revision >= REVISION_TIMESTAMP;

    // --- Parse individual markers ------------------------------------------
    let mut markers = Vec::with_capacity(num_markers);
    let mut bytes_consumed: i32 = MARKER_HDR_BYTES;

    for _ in 0..num_markers {
        let sample = read_i32_le(reader)?;
        let n_channel = read_i16_le(reader)?;
        let style_bytes = read_bytes(reader, 4)?;
        let text_len = read_i32_le(reader)?;

        if text_len < 0 {
            return Err(BiopacError::Validation(alloc::format!(
                "invalid lMarkerTextLen: {text_len}"
            )));
        }
        #[expect(clippy::cast_sign_loss, reason = "text_len checked >= 0 above")]
        let text_len = text_len as usize;

        let text_bytes = read_bytes(reader, text_len)?;

        let created_at = if has_timestamp {
            let ts = read_i64_le(reader)?;
            Some(Timestamp::from_secs(ts))
        } else {
            None
        };

        // style_bytes is always ASCII; decode as Latin-1 for safety.
        let style_code: String = style_bytes.iter().map(|&b| char::from(b)).collect();
        let style = MarkerStyle::from_code(&style_code);
        let label = decode_text(&text_bytes);

        // Map nChannel: -1 = global (None), otherwise find the channel with
        // the matching display_order.
        let channel = if n_channel < 0 {
            None
        } else {
            #[expect(
                clippy::cast_sign_loss,
                reason = "n_channel >= 0 checked by the if branch"
            )]
            let display_order = n_channel as u16;
            channel_display_orders
                .iter()
                .position(|&d| d == display_order)
        };

        #[expect(
            clippy::cast_sign_loss,
            reason = "sample is a byte index/sample index; negative values map to 0"
        )]
        let global_sample_index = sample.max(0) as usize;

        markers.push(Marker {
            label,
            global_sample_index,
            channel,
            style,
            created_at,
        });

        // Track bytes consumed to know where the journal starts.
        #[expect(
            clippy::cast_possible_truncation,
            clippy::cast_possible_wrap,
            reason = "text_len fits in i32 in practice (marker text is tiny); FIXED/TIMESTAMP constants also tiny"
        )]
        {
            bytes_consumed += MARKER_FIXED_BYTES as i32 + text_len as i32;
            if has_timestamp {
                bytes_consumed += MARKER_TIMESTAMP_BYTES as i32;
            }
        }
    }

    // Consume any padding between end-of-markers and journal.
    // total_length is measured from the start of the marker section header.
    let remaining = total_length - bytes_consumed;
    if remaining > 0 {
        #[expect(clippy::cast_sign_loss, reason = "remaining checked > 0 above")]
        let skip = remaining as u64;
        if let Err(e) = std::io::copy(&mut reader.by_ref().take(skip), &mut std::io::sink()) {
            warnings.push(Warning::new(alloc::format!(
                "Failed to skip {skip} padding bytes after markers: {e}"
            )));
        }
    } else if remaining < 0 {
        warnings.push(Warning::new(alloc::format!(
            "Marker section overrun by {} bytes; journal position may be wrong",
            -remaining
        )));
    }

    // --- Journal -----------------------------------------------------------
    let journal = match parse_journal(reader) {
        Ok(j) => j,
        Err(e) => {
            warnings.push(Warning::new(alloc::format!(
                "Journal parse failed (recording data still intact): {e}"
            )));
            None
        }
    };

    Ok(MarkersAndJournal {
        markers,
        journal,
        warnings,
    })
}

fn parse_journal<R: Read>(reader: &mut R) -> Result<Option<Journal>, BiopacError> {
    // Journal length prefix
    let Ok(len) = read_i32_le(reader) else {
        return Ok(None); // EOF before journal — fine
    };

    if len <= 0 {
        return Ok(None);
    }

    #[expect(clippy::cast_sign_loss, reason = "len checked > 0 above")]
    let len = len as usize;

    let bytes = read_bytes(reader, len)?;
    let text = decode_text(&bytes);

    // Detect HTML by presence of a DOCTYPE or opening <html tag.
    let lower_head: String = text
        .chars()
        .take(100)
        .flat_map(char::to_lowercase)
        .collect();
    let is_html = lower_head.contains("<!doctype") || lower_head.contains("<html");

    if is_html {
        Ok(Some(Journal::Html(text)))
    } else {
        Ok(Some(Journal::Plain(text)))
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use alloc::{boxed::Box, vec::Vec};
    use std::io::Cursor;

    fn write_i16_le(v: i16) -> [u8; 2] {
        v.to_le_bytes()
    }
    fn write_i32_le(v: i32) -> [u8; 4] {
        v.to_le_bytes()
    }
    fn write_i64_le(v: i64) -> [u8; 8] {
        v.to_le_bytes()
    }

    /// Build a complete marker section for testing.
    ///
    /// `markers`: list of (sample, channel, style, text).
    /// `file_revision`: controls whether timestamps are written.
    fn build_marker_section(
        markers: &[(i32, i16, &str, &str, Option<i64>)],
        file_revision: i32,
    ) -> Vec<u8> {
        let has_ts = file_revision >= REVISION_TIMESTAMP;
        let mut body = Vec::<u8>::new();
        for &(sample, channel, style, text, ts) in markers {
            body.extend_from_slice(&write_i32_le(sample));
            body.extend_from_slice(&write_i16_le(channel));
            // style is 4 bytes, padded with NUL
            let mut style_bytes = [0u8; 4];
            for (i, b) in style.bytes().take(4).enumerate() {
                if let Some(slot) = style_bytes.get_mut(i) {
                    *slot = b;
                }
            }
            body.extend_from_slice(&style_bytes);
            #[expect(
                clippy::cast_possible_truncation,
                clippy::cast_possible_wrap,
                reason = "test text is tiny"
            )]
            body.extend_from_slice(&write_i32_le(text.len() as i32));
            body.extend_from_slice(text.as_bytes());
            if has_ts {
                body.extend_from_slice(&write_i64_le(ts.unwrap_or(0)));
            }
        }
        #[expect(
            clippy::cast_possible_truncation,
            clippy::cast_possible_wrap,
            reason = "test body size is tiny"
        )]
        let total = MARKER_HDR_BYTES + body.len() as i32;
        let mut out = Vec::<u8>::new();
        out.extend_from_slice(&write_i32_le(total));
        #[expect(
            clippy::cast_possible_truncation,
            clippy::cast_possible_wrap,
            reason = "test marker count is tiny"
        )]
        out.extend_from_slice(&write_i32_le(markers.len() as i32));
        out.extend_from_slice(&body);
        out
    }

    fn build_journal_section(text: &str) -> Vec<u8> {
        let mut out = Vec::<u8>::new();
        #[expect(
            clippy::cast_possible_truncation,
            clippy::cast_possible_wrap,
            reason = "test journal text is small"
        )]
        out.extend_from_slice(&write_i32_le(text.len() as i32));
        out.extend_from_slice(text.as_bytes());
        out
    }

    // -----------------------------------------------------------------------
    // Marker parsing tests
    // -----------------------------------------------------------------------

    #[test]
    fn three_markers_global_and_channel() -> Result<(), Box<dyn std::error::Error>> {
        // 1 global marker + 2 channel-specific
        let items = [
            (100i32, -1i16, "apnd", "start", None),
            (200i32, 0i16, "usr1", "event A", None),
            (300i32, 1i16, "glbl", "end", None),
        ];
        let rev = 73; // pre-4.4, no timestamps
        let mut bytes = build_marker_section(&items, rev);
        bytes.extend_from_slice(&build_journal_section(""));

        let display_orders = [0u16, 1u16];
        let mut cur = Cursor::new(&bytes[..]);
        let result = parse_markers_and_journal(&mut cur, rev, &display_orders)?;

        assert_eq!(result.markers.len(), 3);
        let m0 = result.markers.first().ok_or("missing marker 0")?;
        let m1 = result.markers.get(1).ok_or("missing marker 1")?;
        let m2 = result.markers.get(2).ok_or("missing marker 2")?;

        assert_eq!(m0.channel, None);
        assert_eq!(m0.global_sample_index, 100);
        assert_eq!(m0.style, MarkerStyle::Append);
        assert_eq!(m0.label, "start");

        assert_eq!(m1.channel, Some(0));
        assert_eq!(m1.global_sample_index, 200);
        assert_eq!(m1.style, MarkerStyle::UserEvent);

        assert_eq!(m2.channel, Some(1));
        assert_eq!(m2.global_sample_index, 300);
        assert_eq!(m2.style, MarkerStyle::GlobalEvent);
        Ok(())
    }

    #[test]
    fn global_marker_maps_to_none() -> Result<(), Box<dyn std::error::Error>> {
        let items = [(50i32, -1i16, "apnd", "seg", None)];
        let mut bytes = build_marker_section(&items, 73);
        bytes.extend_from_slice(&build_journal_section(""));

        let mut cur = Cursor::new(&bytes[..]);
        let result = parse_markers_and_journal(&mut cur, 73, &[0u16])?;
        let m = result.markers.first().ok_or("missing marker")?;
        assert_eq!(m.channel, None);
        Ok(())
    }

    #[test]
    fn marker_text_with_embedded_null() -> Result<(), Box<dyn std::error::Error>> {
        // Text "hello\0world" — should be trimmed to "hello"
        let text = "hello\0world";
        let items = [(0i32, -1i16, "apnd", text, None)];
        let mut bytes = build_marker_section(&items, 73);
        bytes.extend_from_slice(&build_journal_section(""));

        let mut cur = Cursor::new(&bytes[..]);
        let result = parse_markers_and_journal(&mut cur, 73, &[])?;
        let m = result.markers.first().ok_or("missing marker")?;
        assert_eq!(m.label, "hello");
        Ok(())
    }

    #[test]
    fn marker_created_at_timestamp_v44() -> Result<(), Box<dyn std::error::Error>> {
        let epoch_secs = 1_700_000_000i64;
        let items = [(0i32, -1i16, "apnd", "ts", Some(epoch_secs))];
        let mut bytes = build_marker_section(&items, 77); // rev 77 = v4.4
        bytes.extend_from_slice(&build_journal_section(""));

        let mut cur = Cursor::new(&bytes[..]);
        let result = parse_markers_and_journal(&mut cur, 77, &[])?;
        let m = result.markers.first().ok_or("missing marker")?;
        assert_eq!(m.created_at, Some(Timestamp::from_secs(epoch_secs)));
        Ok(())
    }

    // -----------------------------------------------------------------------
    // Journal tests
    // -----------------------------------------------------------------------

    #[test]
    fn plain_text_journal() -> Result<(), Box<dyn std::error::Error>> {
        let mut bytes = build_marker_section(&[], 73);
        bytes.extend_from_slice(&build_journal_section("notes about session"));

        let mut cur = Cursor::new(&bytes[..]);
        let result = parse_markers_and_journal(&mut cur, 73, &[])?;
        match result.journal.ok_or("no journal")? {
            Journal::Plain(s) => assert_eq!(s, "notes about session"),
            Journal::Html(_) => return Err("expected plain".into()),
        }
        Ok(())
    }

    #[test]
    fn html_journal_detected() -> Result<(), Box<dyn std::error::Error>> {
        let html = "<!DOCTYPE html><html><body>notes</body></html>";
        let mut bytes = build_marker_section(&[], 74);
        bytes.extend_from_slice(&build_journal_section(html));

        let mut cur = Cursor::new(&bytes[..]);
        let result = parse_markers_and_journal(&mut cur, 74, &[])?;
        assert!(result.journal.ok_or("no journal")?.is_html());
        Ok(())
    }

    #[test]
    fn corrupted_journal_produces_warning() -> Result<(), Box<dyn std::error::Error>> {
        // marker section is fine, but journal length claims more bytes than available
        let mut bytes = build_marker_section(&[], 73);
        // Write journal length of 1000 but provide 0 bytes
        bytes.extend_from_slice(&write_i32_le(1000));

        let mut cur = Cursor::new(&bytes[..]);
        let result = parse_markers_and_journal(&mut cur, 73, &[])?;
        assert!(result.journal.is_none());
        assert!(
            !result.warnings.is_empty(),
            "expected a warning for corrupt journal"
        );
        Ok(())
    }

    #[test]
    fn empty_marker_section_no_journal() -> Result<(), Box<dyn std::error::Error>> {
        // Build an 8-byte marker header with 0 markers and no journal data.
        let mut bytes = Vec::<u8>::new();
        bytes.extend_from_slice(&write_i32_le(8)); // total length = just the header
        bytes.extend_from_slice(&write_i32_le(0)); // 0 markers
        // No journal — EOF

        let mut cur = Cursor::new(&bytes[..]);
        let result = parse_markers_and_journal(&mut cur, 73, &[])?;
        assert!(result.markers.is_empty());
        assert!(result.journal.is_none());
        Ok(())
    }
}