Skip to main content

hdf5_rust/
messages.rs

1//! Object-header message bodies this crate writes and reads.
2
3use alloc::string::String;
4use alloc::vec::Vec;
5
6use crate::buf::{push_u16, push_u32, push_u64, Reader};
7use crate::error::{HDF5Error, Result};
8use crate::{
9    HDF5_ATTR_VERSION, HDF5_CLASS_COMPOUND, HDF5_CLASS_FLOAT, HDF5_CLASS_INTEGER,
10    HDF5_CLASS_OPAQUE, HDF5_CLASS_STRING, HDF5_CLASS_VLEN, HDF5_DATASPACE_VERSION,
11    HDF5_DTYPE_VERSION, HDF5_FILL_VERSION, HDF5_LAYOUT_CHUNKED, HDF5_LAYOUT_COMPACT,
12    HDF5_LAYOUT_CONTIGUOUS, HDF5_LAYOUT_VERSION, HDF5_LINK_VERSION, HDF5_MAX_DIMS,
13    HDF5_MAX_NAME_LEN, HDF5_OPAQUE_TAG, HDF5_SPACE_SCALAR, HDF5_SPACE_SIMPLE, HDF5_UNDEF_ADDR_8,
14    HDF5_UNLIMITED,
15};
16
17/// In-memory datatype after parsing a datatype message.
18#[derive(Clone, Debug, PartialEq, Eq)]
19pub enum ParsedDType {
20    Float64Le,
21    Float32Le,
22    Int8Le,
23    Int16Le,
24    Int32Le,
25    Int64Le,
26    UInt8Le,
27    UInt16Le,
28    UInt32Le,
29    UInt64Le,
30    /// IEEE binary64 big-endian (normalized to LE lanes on read).
31    Float64Be,
32    /// IEEE binary32 big-endian (normalized to LE lanes on read).
33    Float32Be,
34    Int8Be,
35    Int16Be,
36    Int32Be,
37    Int64Be,
38    UInt8Be,
39    UInt16Be,
40    UInt32Be,
41    UInt64Be,
42    Opaque {
43        size: usize,
44    },
45    FixedString {
46        size: usize,
47    },
48    VlenString,
49    /// Compound / structured type with known field layout.
50    Compound {
51        size: usize,
52        fields: Vec<CompoundField>,
53    },
54    Other {
55        size: usize,
56    },
57}
58
59/// One member of a compound datatype.
60#[derive(Clone, Debug, PartialEq, Eq)]
61pub struct CompoundField {
62    /// Field name from the datatype message.
63    pub name: String,
64    /// Byte offset within the compound element.
65    pub offset: usize,
66    /// Field size in bytes.
67    pub size: usize,
68    /// Simplified member class for introspection (not a full recursive tree).
69    pub kind: CompoundMemberKind,
70}
71
72/// Member kind reported for compound field introspection.
73#[derive(Clone, Copy, Debug, PartialEq, Eq)]
74pub enum CompoundMemberKind {
75    /// IEEE binary64 little-endian.
76    Float64Le,
77    /// IEEE binary32 little-endian.
78    Float32Le,
79    /// Signed integer, little-endian, `size` bytes.
80    IntLe {
81        /// Width in bytes.
82        size: usize,
83    },
84    /// Unsigned integer, little-endian, `size` bytes.
85    UIntLe {
86        /// Width in bytes.
87        size: usize,
88    },
89    /// Any other / nested / BE member.
90    Other {
91        /// Width in bytes.
92        size: usize,
93    },
94}
95
96impl ParsedDType {
97    #[allow(dead_code)]
98    pub fn element_size(&self) -> usize {
99        match *self {
100            Self::Float64Le
101            | Self::Float64Be
102            | Self::Int64Le
103            | Self::Int64Be
104            | Self::UInt64Le
105            | Self::UInt64Be => 8,
106            Self::Float32Le
107            | Self::Float32Be
108            | Self::Int32Le
109            | Self::Int32Be
110            | Self::UInt32Le
111            | Self::UInt32Be => 4,
112            Self::Int16Le | Self::Int16Be | Self::UInt16Le | Self::UInt16Be => 2,
113            Self::Int8Le | Self::Int8Be | Self::UInt8Le | Self::UInt8Be => 1,
114            Self::Opaque { size } => size,
115            Self::FixedString { size } => size,
116            Self::VlenString => 0,
117            Self::Compound { size, .. } | Self::Other { size } => size,
118        }
119    }
120}
121
122pub fn encode_ieee_f64le() -> Vec<u8> {
123    let mut v = Vec::with_capacity(20);
124    v.push((HDF5_DTYPE_VERSION << 4) | HDF5_CLASS_FLOAT);
125    v.push(0x20);
126    v.push(63);
127    v.push(0);
128    push_u32(&mut v, 8);
129    push_u16(&mut v, 0);
130    push_u16(&mut v, 64);
131    v.push(52);
132    v.push(11);
133    v.push(0);
134    v.push(52);
135    push_u32(&mut v, 1023);
136    v
137}
138
139pub fn encode_ieee_f32le() -> Vec<u8> {
140    let mut v = Vec::with_capacity(20);
141    v.push((HDF5_DTYPE_VERSION << 4) | HDF5_CLASS_FLOAT);
142    v.push(0x20);
143    v.push(31);
144    v.push(0);
145    push_u32(&mut v, 4);
146    push_u16(&mut v, 0);
147    push_u16(&mut v, 32);
148    v.push(23);
149    v.push(8);
150    v.push(0);
151    v.push(23);
152    push_u32(&mut v, 127);
153    v
154}
155
156pub fn encode_integer(size: u32, signed: bool) -> Vec<u8> {
157    let mut v = Vec::with_capacity(12);
158    v.push((HDF5_DTYPE_VERSION << 4) | HDF5_CLASS_INTEGER);
159    v.push(if signed { 0x08 } else { 0x00 });
160    v.push(0);
161    v.push(0);
162    push_u32(&mut v, size);
163    push_u16(&mut v, 0);
164    let prec = size.saturating_mul(8);
165    push_u16(&mut v, prec as u16);
166    v
167}
168
169pub fn encode_opaque(elem_size: u32) -> Vec<u8> {
170    let mut v = Vec::with_capacity(16);
171    v.push((HDF5_DTYPE_VERSION << 4) | HDF5_CLASS_OPAQUE);
172    v.push(HDF5_OPAQUE_TAG.len() as u8);
173    v.push(0);
174    v.push(0);
175    push_u32(&mut v, elem_size);
176    v.extend_from_slice(&HDF5_OPAQUE_TAG);
177    v
178}
179
180pub fn encode_fixed_string(nbytes: u32) -> Vec<u8> {
181    let mut v = Vec::with_capacity(8);
182    v.push((HDF5_DTYPE_VERSION << 4) | HDF5_CLASS_STRING);
183    v.push(0);
184    v.push(0);
185    v.push(0);
186    push_u32(&mut v, nbytes);
187    v
188}
189
190pub fn encode_dataspace(dims: &[u64], unlimited_first: bool) -> Result<Vec<u8>> {
191    if dims.len() > HDF5_MAX_DIMS {
192        return Err(HDF5Error::RankNotSupported);
193    }
194    if dims.is_empty() {
195        return Ok(alloc::vec![HDF5_DATASPACE_VERSION, 0, 0, HDF5_SPACE_SCALAR]);
196    }
197    let mut v = alloc::vec![
198        HDF5_DATASPACE_VERSION,
199        dims.len() as u8,
200        1,
201        HDF5_SPACE_SIMPLE,
202    ];
203    for d in dims {
204        push_u64(&mut v, *d);
205    }
206    for (i, d) in dims.iter().enumerate() {
207        if unlimited_first && i == 0 {
208            push_u64(&mut v, HDF5_UNLIMITED);
209        } else {
210            push_u64(&mut v, *d);
211        }
212    }
213    Ok(v)
214}
215
216pub fn encode_layout_contiguous(addr: u64, size: u64) -> Vec<u8> {
217    let mut v = Vec::with_capacity(18);
218    v.push(HDF5_LAYOUT_VERSION);
219    v.push(HDF5_LAYOUT_CONTIGUOUS);
220    push_u64(&mut v, addr);
221    push_u64(&mut v, size);
222    v
223}
224
225pub fn encode_fill() -> Vec<u8> {
226    alloc::vec![HDF5_FILL_VERSION, 0x02]
227}
228
229pub fn encode_link_info() -> Vec<u8> {
230    let mut v = Vec::with_capacity(18);
231    v.push(0);
232    v.push(0);
233    push_u64(&mut v, HDF5_UNDEF_ADDR_8);
234    push_u64(&mut v, HDF5_UNDEF_ADDR_8);
235    v
236}
237
238pub fn encode_group_info() -> Vec<u8> {
239    alloc::vec![0, 0]
240}
241
242pub fn encode_hard_link(name: &str, ohdr: u64) -> Result<Vec<u8>> {
243    if name.len() > HDF5_MAX_NAME_LEN {
244        return Err(HDF5Error::NameTooLong);
245    }
246    let n = name.len();
247    let (flag_len, len_bytes): (u8, Vec<u8>) = if n < 256 {
248        (0, alloc::vec![n as u8])
249    } else if n < 65536 {
250        (1, (n as u16).to_le_bytes().to_vec())
251    } else {
252        (2, (n as u32).to_le_bytes().to_vec())
253    };
254    let mut v = Vec::new();
255    v.push(HDF5_LINK_VERSION);
256    v.push(flag_len);
257    v.extend_from_slice(&len_bytes);
258    v.extend_from_slice(name.as_bytes());
259    push_u64(&mut v, ohdr);
260    Ok(v)
261}
262
263pub fn encode_string_attr(name: &str, value: &str) -> Result<Vec<u8>> {
264    if name.len() > HDF5_MAX_NAME_LEN || value.len() > HDF5_MAX_NAME_LEN {
265        return Err(HDF5Error::NameTooLong);
266    }
267    let name_z = name.len() + 1;
268    let val_bytes = value.len() + 1;
269    let dtype = encode_fixed_string(val_bytes as u32);
270    let space = encode_dataspace(&[], false)?;
271    let mut v = Vec::new();
272    v.push(HDF5_ATTR_VERSION);
273    v.push(0);
274    push_u16(&mut v, name_z as u16);
275    push_u16(&mut v, dtype.len() as u16);
276    push_u16(&mut v, space.len() as u16);
277    v.extend_from_slice(name.as_bytes());
278    v.push(0);
279    v.extend_from_slice(&dtype);
280    v.extend_from_slice(&space);
281    v.extend_from_slice(value.as_bytes());
282    v.push(0);
283    Ok(v)
284}
285
286pub fn parse_datatype(body: &[u8]) -> Result<ParsedDType> {
287    if body.len() < 8 {
288        return Err(HDF5Error::InvalidHeader);
289    }
290    let class_ver = body[0];
291    let class = class_ver & 0x0F;
292    let bit0 = body[1];
293    let _bit1 = body[2];
294    let size = u32::from_le_bytes([body[4], body[5], body[6], body[7]]) as usize;
295    match class {
296        HDF5_CLASS_INTEGER => parse_integer(bit0, size, body),
297        HDF5_CLASS_FLOAT => {
298            // Byte order: bits 0-0 of class bitfield byte1? HDF5: bit 0 = BE when set with bit 6.
299            // (bit0 & 0x41) == 0 means little-endian for the float class bitfield.
300            let le = (bit0 & 0x41) == 0;
301            match (le, size) {
302                (true, 8) => Ok(ParsedDType::Float64Le),
303                (true, 4) => Ok(ParsedDType::Float32Le),
304                (false, 8) => Ok(ParsedDType::Float64Be),
305                (false, 4) => Ok(ParsedDType::Float32Be),
306                _ => Ok(ParsedDType::Other { size }),
307            }
308        }
309        HDF5_CLASS_OPAQUE => Ok(ParsedDType::Opaque { size }),
310        HDF5_CLASS_STRING => Ok(ParsedDType::FixedString { size }),
311        HDF5_CLASS_VLEN => {
312            if (bit0 & 0x0F) == 1 {
313                Ok(ParsedDType::VlenString)
314            } else {
315                Ok(ParsedDType::Other { size })
316            }
317        }
318        HDF5_CLASS_COMPOUND => parse_compound(body, size),
319        _ => Ok(ParsedDType::Other { size }),
320    }
321}
322
323/// Precision and bit offset of an integer datatype, when the message carries them.
324pub struct IntegerBits {
325    /// Significant bits.
326    pub precision: usize,
327    /// Offset of those bits from the least significant bit.
328    pub offset: usize,
329    /// Two's-complement signed integer.
330    pub signed: bool,
331}
332
333/// Integer bit layout from a datatype message, if this is an integer class.
334pub fn integer_bits(body: &[u8]) -> Option<IntegerBits> {
335    if body.len() < 12 {
336        return None;
337    }
338    let class = body[0] & 0x0F;
339    if class != HDF5_CLASS_INTEGER {
340        return None;
341    }
342    let size = u32::from_le_bytes([body[4], body[5], body[6], body[7]]) as usize;
343    let offset = u16::from_le_bytes([body[8], body[9]]) as usize;
344    let precision = u16::from_le_bytes([body[10], body[11]]) as usize;
345    let signed = (body[1] & 0x08) != 0;
346    let width = size.saturating_mul(8);
347    if precision == 0 || precision > width || offset.saturating_add(precision) > width {
348        return None;
349    }
350    Some(IntegerBits {
351        precision,
352        offset,
353        signed,
354    })
355}
356
357fn parse_integer(bit0: u8, size: usize, body: &[u8]) -> Result<ParsedDType> {
358    let le = (bit0 & 0x01) == 0;
359    let signed = (bit0 & 0x08) != 0;
360    if body.len() >= 12 {
361        let offset = u16::from_le_bytes([body[8], body[9]]) as usize;
362        let prec = u16::from_le_bytes([body[10], body[11]]) as usize;
363        let width = size.saturating_mul(8);
364        if prec == 0 || offset.saturating_add(prec) > width {
365            return Ok(ParsedDType::Other { size });
366        }
367    }
368    Ok(match (le, signed, size) {
369        (true, true, 1) => ParsedDType::Int8Le,
370        (true, true, 2) => ParsedDType::Int16Le,
371        (true, true, 4) => ParsedDType::Int32Le,
372        (true, true, 8) => ParsedDType::Int64Le,
373        (true, false, 1) => ParsedDType::UInt8Le,
374        (true, false, 2) => ParsedDType::UInt16Le,
375        (true, false, 4) => ParsedDType::UInt32Le,
376        (true, false, 8) => ParsedDType::UInt64Le,
377        (false, true, 1) => ParsedDType::Int8Be,
378        (false, true, 2) => ParsedDType::Int16Be,
379        (false, true, 4) => ParsedDType::Int32Be,
380        (false, true, 8) => ParsedDType::Int64Be,
381        (false, false, 1) => ParsedDType::UInt8Be,
382        (false, false, 2) => ParsedDType::UInt16Be,
383        (false, false, 4) => ParsedDType::UInt32Be,
384        (false, false, 8) => ParsedDType::UInt64Be,
385        _ => ParsedDType::Other { size },
386    })
387}
388
389fn parse_compound(body: &[u8], size: usize) -> Result<ParsedDType> {
390    if body.len() < 8 {
391        return Err(HDF5Error::InvalidHeader);
392    }
393    let class_ver = body[0];
394    let version = (class_ver >> 4) & 0x0F;
395    let nmembers = body[1] as usize;
396    // Properties start after the 8-byte datatype header.
397    let mut pos = 8usize;
398    let mut fields = Vec::with_capacity(nmembers);
399    for _ in 0..nmembers {
400        if pos >= body.len() {
401            return Err(HDF5Error::Truncated);
402        }
403        // Name is null-terminated; v1 padded to 8 bytes.
404        let rest = &body[pos..];
405        let name_end = rest
406            .iter()
407            .position(|&b| b == 0)
408            .ok_or(HDF5Error::InvalidHeader)?;
409        let name =
410            String::from_utf8(rest[..name_end].to_vec()).map_err(|_| HDF5Error::InvalidHeader)?;
411        pos += name_end + 1;
412        if version == 1 {
413            let pad = (8 - ((name_end + 1) % 8)) % 8;
414            pos += pad;
415        }
416        if pos + 4 > body.len() {
417            return Err(HDF5Error::Truncated);
418        }
419        let offset =
420            u32::from_le_bytes([body[pos], body[pos + 1], body[pos + 2], body[pos + 3]]) as usize;
421        pos += 4;
422        if version == 1 {
423            // v1: ndims(1) + reserved(3) + permutation(4) + reserved(4)
424            // + four dimension sizes (always 4 × u32), even when ndims == 0.
425            if pos + 28 > body.len() {
426                return Err(HDF5Error::Truncated);
427            }
428            let _mdims = body[pos];
429            pos += 28;
430        }
431        if pos + 8 > body.len() {
432            return Err(HDF5Error::Truncated);
433        }
434        // Member datatype is embedded recursively; size at bytes 4..8 of member header.
435        let msize = u32::from_le_bytes([body[pos + 4], body[pos + 5], body[pos + 6], body[pos + 7]])
436            as usize;
437        let member_body = &body[pos..];
438        let member_dt = parse_datatype(member_body)?;
439        let member_total = datatype_message_len(member_body)?;
440        pos += member_total;
441        let kind = compound_member_kind(&member_dt);
442        fields.push(CompoundField {
443            name,
444            offset,
445            size: msize,
446            kind,
447        });
448    }
449    Ok(ParsedDType::Compound { size, fields })
450}
451
452fn compound_member_kind(dt: &ParsedDType) -> CompoundMemberKind {
453    match *dt {
454        ParsedDType::Float64Le => CompoundMemberKind::Float64Le,
455        ParsedDType::Float32Le => CompoundMemberKind::Float32Le,
456        ParsedDType::Int8Le => CompoundMemberKind::IntLe { size: 1 },
457        ParsedDType::Int16Le => CompoundMemberKind::IntLe { size: 2 },
458        ParsedDType::Int32Le => CompoundMemberKind::IntLe { size: 4 },
459        ParsedDType::Int64Le => CompoundMemberKind::IntLe { size: 8 },
460        ParsedDType::UInt8Le => CompoundMemberKind::UIntLe { size: 1 },
461        ParsedDType::UInt16Le => CompoundMemberKind::UIntLe { size: 2 },
462        ParsedDType::UInt32Le => CompoundMemberKind::UIntLe { size: 4 },
463        ParsedDType::UInt64Le => CompoundMemberKind::UIntLe { size: 8 },
464        ParsedDType::Opaque { size }
465        | ParsedDType::FixedString { size }
466        | ParsedDType::Compound { size, .. }
467        | ParsedDType::Other { size } => CompoundMemberKind::Other { size },
468        ParsedDType::Float64Be => CompoundMemberKind::Other { size: 8 },
469        ParsedDType::Float32Be => CompoundMemberKind::Other { size: 4 },
470        ParsedDType::Int8Be | ParsedDType::UInt8Be => CompoundMemberKind::Other { size: 1 },
471        ParsedDType::Int16Be | ParsedDType::UInt16Be => CompoundMemberKind::Other { size: 2 },
472        ParsedDType::Int32Be | ParsedDType::UInt32Be => CompoundMemberKind::Other { size: 4 },
473        ParsedDType::Int64Be | ParsedDType::UInt64Be => CompoundMemberKind::Other { size: 8 },
474        ParsedDType::VlenString => CompoundMemberKind::Other { size: 0 },
475    }
476}
477
478/// Byte length of one datatype message including nested members.
479fn datatype_message_len(body: &[u8]) -> Result<usize> {
480    if body.len() < 8 {
481        return Err(HDF5Error::Truncated);
482    }
483    let class_ver = body[0];
484    let class = class_ver & 0x0F;
485    let version = (class_ver >> 4) & 0x0F;
486    let size = u32::from_le_bytes([body[4], body[5], body[6], body[7]]) as usize;
487    match class {
488        HDF5_CLASS_INTEGER => Ok(12),
489        HDF5_CLASS_FLOAT => Ok(20),
490        HDF5_CLASS_OPAQUE => {
491            let tag_len = body[1] as usize;
492            Ok(8 + tag_len)
493        }
494        HDF5_CLASS_STRING => Ok(8),
495        HDF5_CLASS_COMPOUND => {
496            // Re-parse by walking; expensive but rare.
497            let nmembers = body[1] as usize;
498            let mut pos = 8usize;
499            for _ in 0..nmembers {
500                let rest = &body[pos..];
501                let name_end = rest
502                    .iter()
503                    .position(|&b| b == 0)
504                    .ok_or(HDF5Error::InvalidHeader)?;
505                pos += name_end + 1;
506                if version == 1 {
507                    pos += (8 - ((name_end + 1) % 8)) % 8;
508                    pos += 4; // offset
509                    if pos + 28 > body.len() {
510                        return Err(HDF5Error::Truncated);
511                    }
512                    pos += 28;
513                } else {
514                    pos += 4; // offset
515                }
516                let nested = datatype_message_len(&body[pos..])?;
517                pos += nested;
518            }
519            Ok(pos)
520        }
521        _ => {
522            // Best-effort: header only; compound walker needs accurate nested sizes.
523            let _ = size;
524            Ok(8)
525        }
526    }
527}
528
529pub struct ParsedSpace {
530    pub dims: Vec<u64>,
531}
532
533pub fn parse_dataspace(body: &[u8], length_size: u8) -> Result<ParsedSpace> {
534    if body.len() < 4 {
535        return Err(HDF5Error::InvalidHeader);
536    }
537    let version = body[0];
538    let ndims = body[1] as usize;
539    if ndims > HDF5_MAX_DIMS {
540        return Err(HDF5Error::RankNotSupported);
541    }
542    let flags = body[2];
543    let mut r = Reader::new(body, 8, length_size)?;
544    match version {
545        1 => {
546            r.skip(8)?;
547        }
548        2 => {
549            r.skip(4)?;
550        }
551        _ => return Err(HDF5Error::UnsupportedVersion(version)),
552    }
553    let mut dims = Vec::new();
554    for _ in 0..ndims {
555        dims.push(r.length()?);
556    }
557    if flags & 1 != 0 {
558        for _ in 0..ndims {
559            let _ = r.length()?;
560        }
561    }
562    if version == 1 && flags & 2 != 0 {
563        for _ in 0..ndims {
564            let _ = r.length()?;
565        }
566    }
567    Ok(ParsedSpace { dims })
568}
569
570pub enum ParsedLayout {
571    Contiguous {
572        addr: u64,
573        size: u64,
574    },
575    Compact {
576        data: Vec<u8>,
577    },
578    /// Chunked storage. `index` describes how to find chunks.
579    Chunked {
580        chunk_dims: Vec<u32>,
581        index: crate::chunk_index::ChunkIndex,
582    },
583}
584
585pub fn parse_layout(body: &[u8], offset_size: u8, length_size: u8) -> Result<ParsedLayout> {
586    if body.is_empty() {
587        return Err(HDF5Error::InvalidHeader);
588    }
589    let version = body[0];
590    let mut r = Reader::new(body, offset_size, length_size)?;
591    r.u8()?;
592    match version {
593        1 | 2 => {
594            let ndims = r.u8()? as usize;
595            let class = r.u8()?;
596            r.skip(5)?;
597            match class {
598                HDF5_LAYOUT_CHUNKED => {
599                    let addr = r.addr()?;
600                    let mut chunk_dims = Vec::new();
601                    for _ in 0..ndims {
602                        chunk_dims.push(r.u32()?);
603                    }
604                    Ok(ParsedLayout::Chunked {
605                        chunk_dims,
606                        index: crate::chunk_index::ChunkIndex::BTreeV1 { addr },
607                    })
608                }
609                HDF5_LAYOUT_CONTIGUOUS => {
610                    let addr = r.addr()?;
611                    Ok(ParsedLayout::Contiguous { addr, size: 0 })
612                }
613                HDF5_LAYOUT_COMPACT => {
614                    let sz = r.u32()? as usize;
615                    Ok(ParsedLayout::Compact {
616                        data: r.bytes(sz)?.to_vec(),
617                    })
618                }
619                _ => Err(HDF5Error::InvalidHeader),
620            }
621        }
622        3 => {
623            let class = r.u8()?;
624            match class {
625                HDF5_LAYOUT_CHUNKED => {
626                    let ndims = r.u8()? as usize;
627                    let addr = r.addr()?;
628                    let mut chunk_dims = Vec::new();
629                    for _ in 0..ndims {
630                        chunk_dims.push(r.u32()?);
631                    }
632                    Ok(ParsedLayout::Chunked {
633                        chunk_dims,
634                        index: crate::chunk_index::ChunkIndex::BTreeV1 { addr },
635                    })
636                }
637                HDF5_LAYOUT_CONTIGUOUS => {
638                    let addr = r.addr()?;
639                    let size = r.length()?;
640                    Ok(ParsedLayout::Contiguous { addr, size })
641                }
642                HDF5_LAYOUT_COMPACT => {
643                    let sz = r.u16()? as usize;
644                    Ok(ParsedLayout::Compact {
645                        data: r.bytes(sz)?.to_vec(),
646                    })
647                }
648                _ => Err(HDF5Error::InvalidHeader),
649            }
650        }
651        4 | 5 => {
652            let class = r.u8()?;
653            match class {
654                HDF5_LAYOUT_CHUNKED => {
655                    let flags = r.u8()?;
656                    let ndims = r.u8()? as usize;
657                    let enc = r.u8()? as usize;
658                    if enc == 0 || enc > 8 {
659                        return Err(HDF5Error::InvalidHeader);
660                    }
661                    let mut chunk_dims = Vec::with_capacity(ndims);
662                    for _ in 0..ndims {
663                        chunk_dims.push(r.sized_uint(enc as u8)? as u32);
664                    }
665                    let idx_type = r.u8()?;
666                    // H5D_CHUNK_IDX_*: 1=single, 2=none/implicit, 3=farray, 4=earray, 5=bt2
667                    let index = match idx_type {
668                        1 => {
669                            let (nbytes, filter_mask) = if flags & 0x02 != 0 {
670                                let nb = r.length()?;
671                                let fm = r.u32()?;
672                                (
673                                    Some(u32::try_from(nb).map_err(|_| HDF5Error::InvalidHeader)?),
674                                    fm,
675                                )
676                            } else {
677                                (None, 0u32)
678                            };
679                            let addr = r.addr()?;
680                            crate::chunk_index::ChunkIndex::Single {
681                                addr,
682                                nbytes,
683                                filter_mask,
684                            }
685                        }
686                        3 => {
687                            let _max_bits = r.u8()?;
688                            let addr = r.addr()?;
689                            crate::chunk_index::ChunkIndex::FixedArray { addr }
690                        }
691                        2 => {
692                            let addr = r.addr()?;
693                            crate::chunk_index::ChunkIndex::Implicit { addr }
694                        }
695                        4 => {
696                            // max_nelmts_bits, idx_blk_elmts, sup_blk_min, data_blk_min, page bits.
697                            r.skip(5)?;
698                            let addr = r.addr()?;
699                            crate::chunk_index::ChunkIndex::ExtensibleArray { addr }
700                        }
701                        5 => {
702                            // node size, split percent, merge percent; the header is authoritative.
703                            r.u32()?;
704                            r.u8()?;
705                            r.u8()?;
706                            let addr = r.addr()?;
707                            crate::chunk_index::ChunkIndex::BTreeV2 { addr }
708                        }
709                        _ => return Err(HDF5Error::ChunkedNotSupported),
710                    };
711                    let _ = flags;
712                    Ok(ParsedLayout::Chunked { chunk_dims, index })
713                }
714                HDF5_LAYOUT_CONTIGUOUS => {
715                    let addr = r.addr()?;
716                    let size = r.length()?;
717                    Ok(ParsedLayout::Contiguous { addr, size })
718                }
719                HDF5_LAYOUT_COMPACT => {
720                    let sz = r.u16()? as usize;
721                    Ok(ParsedLayout::Compact {
722                        data: r.bytes(sz)?.to_vec(),
723                    })
724                }
725                _ => Err(HDF5Error::InvalidHeader),
726            }
727        }
728        v => Err(HDF5Error::UnsupportedVersion(v)),
729    }
730}
731
732pub struct ParsedLink {
733    pub name: String,
734    pub ohdr: u64,
735}
736
737pub fn parse_link(body: &[u8], offset_size: u8) -> Result<ParsedLink> {
738    let mut r = Reader::new(body, offset_size, 8)?;
739    let version = r.u8()?;
740    if version != 0 && version != 1 {
741        return Err(HDF5Error::UnsupportedVersion(version));
742    }
743    let flags = r.u8()?;
744    if flags & 0x08 != 0 {
745        let _ty = r.u8()?;
746    }
747    if flags & 0x04 != 0 {
748        let _ = r.u64()?;
749    }
750    if flags & 0x10 != 0 {
751        let _ = r.u8()?;
752    }
753    let nsize = match flags & 0x03 {
754        0 => 1,
755        1 => 2,
756        2 => 4,
757        3 => 8,
758        _ => 1,
759    };
760    let nlen = r.sized_uint(nsize)? as usize;
761    if nlen > HDF5_MAX_NAME_LEN {
762        return Err(HDF5Error::NameTooLong);
763    }
764    let nb = r.bytes(nlen)?;
765    let name = String::from_utf8(nb.to_vec()).map_err(|_| HDF5Error::InvalidHeader)?;
766    let ohdr = r.addr()?;
767    Ok(ParsedLink { name, ohdr })
768}
769
770pub struct ParsedAttr {
771    pub name: String,
772    pub dtype: ParsedDType,
773    pub data: Vec<u8>,
774}
775
776pub fn parse_attribute(body: &[u8], offset_size: u8, length_size: u8) -> Result<ParsedAttr> {
777    if body.len() < 8 {
778        return Err(HDF5Error::InvalidHeader);
779    }
780    let version = body[0];
781    let mut r = Reader::new(body, offset_size, length_size)?;
782    r.u8()?;
783    let mut flags = 0u8;
784    match version {
785        1 => {
786            r.u8()?;
787        }
788        2 | 3 => {
789            flags = r.u8()?;
790        }
791        v => return Err(HDF5Error::UnsupportedVersion(v)),
792    }
793    let name_size = r.u16()? as usize;
794    let dtype_size = r.u16()? as usize;
795    let space_size = r.u16()? as usize;
796    if version == 3 {
797        let _cs = r.u8()?;
798    }
799    let name_raw = r.bytes(name_size)?;
800    let name_end = name_raw
801        .iter()
802        .position(|&b| b == 0)
803        .unwrap_or(name_raw.len());
804    let name =
805        String::from_utf8(name_raw[..name_end].to_vec()).map_err(|_| HDF5Error::InvalidHeader)?;
806    if version == 1 {
807        let pad = (8 - (name_size % 8)) % 8;
808        r.skip(pad)?;
809    }
810    if flags & 1 != 0 {
811        return Err(HDF5Error::InvalidHeader);
812    }
813    let dtype_bytes = r.bytes(dtype_size)?;
814    if version == 1 {
815        let pad = (8 - (dtype_size % 8)) % 8;
816        r.skip(pad)?;
817    }
818    let dtype = parse_datatype(dtype_bytes)?;
819    r.skip(space_size)?;
820    if version == 1 {
821        let pad = (8 - (space_size % 8)) % 8;
822        r.skip(pad)?;
823    }
824    let data = r.bytes(r.remaining())?.to_vec();
825    Ok(ParsedAttr { name, dtype, data })
826}
827
828pub fn parse_link_info_heap(body: &[u8], offset_size: u8) -> Result<Option<u64>> {
829    if body.len() < 2 {
830        return Ok(None);
831    }
832    let mut r = Reader::new(body, offset_size, 8)?;
833    let _ver = r.u8()?;
834    let flags = r.u8()?;
835    if flags & 1 != 0 {
836        let _ = r.u64()?;
837    }
838    let heap = r.addr()?;
839    if r.is_undef(heap) {
840        Ok(None)
841    } else {
842        Ok(Some(heap))
843    }
844}
845
846pub fn parse_symbol_table(body: &[u8], offset_size: u8) -> Result<(u64, u64)> {
847    let mut r = Reader::new(body, offset_size, 8)?;
848    let btree = r.addr()?;
849    let heap = r.addr()?;
850    Ok((btree, heap))
851}
852
853/// Number of filters in a filter-pipeline message. `0` if the body is empty.
854#[allow(dead_code)]
855pub fn parse_filter_count(body: &[u8]) -> Result<u8> {
856    if body.len() < 2 {
857        return Ok(0);
858    }
859    match body[0] {
860        1 | 2 => Ok(body[1]),
861        _ => Err(HDF5Error::UnsupportedVersion(body[0])),
862    }
863}
864
865pub fn cstr_from_heap(heap: &[u8], offset: u64) -> Result<String> {
866    let start = usize::try_from(offset).map_err(|_| HDF5Error::Truncated)?;
867    if start >= heap.len() {
868        return Err(HDF5Error::Truncated);
869    }
870    let rest = &heap[start..];
871    let end = rest.iter().position(|&b| b == 0).unwrap_or(rest.len());
872    String::from_utf8(rest[..end].to_vec()).map_err(|_| HDF5Error::InvalidHeader)
873}