hdf5-core 0.9.0

Shared HDF5 format types, constants, and validation helpers
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
//! Shared HDF5 format model used by reader and writer crates.

use std::fmt;

/// Errors produced by shared HDF5 model helpers.
#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error("invalid data: {0}")]
    InvalidData(String),
}

pub type Result<T> = std::result::Result<T, Error>;

/// Jenkins lookup3 `hashlittle2` used by HDF5 metadata checksums.
///
/// HDF5 uses this checksum for superblock v2/v3, object header v2,
/// B-tree v2, extensible-array, fixed-array, fractal-heap, and shared-message
/// metadata blocks. This is a direct translation of Bob Jenkins'
/// lookup3.c `hashlittle2` with both init values set to zero.
pub fn jenkins_lookup3(data: &[u8]) -> u32 {
    let len = data.len();
    let mut a: u32 = 0xdeadbeef_u32.wrapping_add(len as u32);
    let mut b: u32 = a;
    let mut c: u32 = a;
    let mut offset = 0;

    while offset + 12 < len {
        a = a.wrapping_add(u32::from_le_bytes([
            data[offset],
            data[offset + 1],
            data[offset + 2],
            data[offset + 3],
        ]));
        b = b.wrapping_add(u32::from_le_bytes([
            data[offset + 4],
            data[offset + 5],
            data[offset + 6],
            data[offset + 7],
        ]));
        c = c.wrapping_add(u32::from_le_bytes([
            data[offset + 8],
            data[offset + 9],
            data[offset + 10],
            data[offset + 11],
        ]));
        jenkins_mix(&mut a, &mut b, &mut c);
        offset += 12;
    }

    let remaining = len - offset;
    if remaining > 0 {
        let mut tail_a: u32 = 0;
        let mut tail_b: u32 = 0;
        let mut tail_c: u32 = 0;

        for i in 0..remaining.min(4) {
            tail_a |= (data[offset + i] as u32) << (i * 8);
        }
        if remaining > 4 {
            for i in 4..remaining.min(8) {
                tail_b |= (data[offset + i] as u32) << ((i - 4) * 8);
            }
        }
        if remaining > 8 {
            for i in 8..remaining {
                tail_c |= (data[offset + i] as u32) << ((i - 8) * 8);
            }
        }

        a = a.wrapping_add(tail_a);
        b = b.wrapping_add(tail_b);
        c = c.wrapping_add(tail_c);
        jenkins_final_mix(&mut a, &mut b, &mut c);
    }

    c
}

/// Fletcher-32 checksum used by the HDF5 filter pipeline.
///
/// Matches HDF5's `H5_checksum_fletcher32`: reads 16-bit big-endian words,
/// accumulates in batches of 360, and reduces with `(x & 0xffff) + (x >> 16)`.
pub fn fletcher32(data: &[u8]) -> u32 {
    let mut sum1: u32 = 0;
    let mut sum2: u32 = 0;
    let total_words = data.len() / 2;

    let mut offset = 0usize;
    let mut remaining = total_words;

    while remaining > 0 {
        let batch = remaining.min(360);
        remaining -= batch;

        for _ in 0..batch {
            let word = ((data[offset] as u32) << 8) | (data[offset + 1] as u32);
            sum1 += word;
            sum2 += sum1;
            offset += 2;
        }

        sum1 = (sum1 & 0xffff) + (sum1 >> 16);
        sum2 = (sum2 & 0xffff) + (sum2 >> 16);
    }

    // A trailing odd byte contributes as the high half of a final word.
    if data.len() % 2 == 1 {
        sum1 += (data[data.len() - 1] as u32) << 8;
        sum2 += sum1;
        sum1 = (sum1 & 0xffff) + (sum1 >> 16);
        sum2 = (sum2 & 0xffff) + (sum2 >> 16);
    }

    sum1 = (sum1 & 0xffff) + (sum1 >> 16);
    sum2 = (sum2 & 0xffff) + (sum2 >> 16);

    (sum2 << 16) | sum1
}

#[inline]
fn jenkins_mix(a: &mut u32, b: &mut u32, c: &mut u32) {
    *a = a.wrapping_sub(*c);
    *a ^= c.rotate_left(4);
    *c = c.wrapping_add(*b);
    *b = b.wrapping_sub(*a);
    *b ^= a.rotate_left(6);
    *a = a.wrapping_add(*c);
    *c = c.wrapping_sub(*b);
    *c ^= b.rotate_left(8);
    *b = b.wrapping_add(*a);
    *a = a.wrapping_sub(*c);
    *a ^= c.rotate_left(16);
    *c = c.wrapping_add(*b);
    *b = b.wrapping_sub(*a);
    *b ^= a.rotate_left(19);
    *a = a.wrapping_add(*c);
    *c = c.wrapping_sub(*b);
    *c ^= b.rotate_left(4);
    *b = b.wrapping_add(*a);
}

#[inline]
fn jenkins_final_mix(a: &mut u32, b: &mut u32, c: &mut u32) {
    *c ^= *b;
    *c = c.wrapping_sub(b.rotate_left(14));
    *a ^= *c;
    *a = a.wrapping_sub(c.rotate_left(11));
    *b ^= *a;
    *b = b.wrapping_sub(a.rotate_left(25));
    *c ^= *b;
    *c = c.wrapping_sub(b.rotate_left(16));
    *a ^= *c;
    *a = a.wrapping_sub(c.rotate_left(4));
    *b ^= *a;
    *b = b.wrapping_sub(a.rotate_left(14));
    *c ^= *b;
    *c = c.wrapping_sub(b.rotate_left(24));
}

/// Byte order for numeric data.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ByteOrder {
    LittleEndian,
    BigEndian,
}

impl fmt::Display for ByteOrder {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ByteOrder::LittleEndian => write!(f, "little-endian"),
            ByteOrder::BigEndian => write!(f, "big-endian"),
        }
    }
}

/// How a string's length is determined.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StringSize {
    /// Fixed-length, padded to `n` bytes.
    Fixed(u32),
    /// Variable-length.
    Variable,
}

/// String character encoding.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StringEncoding {
    Ascii,
    Utf8,
}

/// String padding type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StringPadding {
    NullTerminate,
    NullPad,
    SpacePad,
}

/// HDF5 variable-length datatype flavor.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VarLenKind {
    /// Variable-length sequence of values.
    Sequence,
    /// Variable-length string.
    String,
    /// Unknown HDF5 vlen kind; retained for metadata fidelity.
    Unknown(u8),
}

/// A field within a compound datatype.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CompoundField {
    pub name: String,
    pub byte_offset: u32,
    pub datatype: Datatype,
}

/// A member of an enumeration.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EnumMember {
    pub name: String,
    pub value: Vec<u8>,
}

/// HDF5 reference type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReferenceType {
    /// Object reference.
    Object,
    /// Dataset region reference.
    DatasetRegion,
}

/// Describes the element type of a dataset or attribute.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Datatype {
    FixedPoint {
        size: u8,
        signed: bool,
        byte_order: ByteOrder,
    },
    FloatingPoint {
        size: u8,
        byte_order: ByteOrder,
    },
    String {
        size: StringSize,
        encoding: StringEncoding,
        padding: StringPadding,
    },
    Compound {
        size: u32,
        fields: Vec<CompoundField>,
    },
    Array {
        base: Box<Datatype>,
        dims: Vec<u64>,
    },
    Enum {
        base: Box<Datatype>,
        members: Vec<EnumMember>,
    },
    VarLen {
        base: Box<Datatype>,
        kind: VarLenKind,
        encoding: StringEncoding,
        padding: StringPadding,
    },
    Opaque {
        size: u32,
        tag: String,
    },
    Reference {
        ref_type: ReferenceType,
        size: u8,
    },
    Bitfield {
        size: u8,
        byte_order: ByteOrder,
    },
}

/// Datatype message payload plus element size from the message header.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DatatypeMessage {
    pub datatype: Datatype,
    pub size: u32,
}

/// Unlimited dimension sentinel value.
pub const UNLIMITED: u64 = u64::MAX;

/// HDF5 dataspace kind.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DataspaceType {
    Null,
    Scalar,
    Simple,
}

/// Parsed dataspace message.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DataspaceMessage {
    pub rank: u8,
    pub dims: Vec<u64>,
    pub max_dims: Option<Vec<u64>>,
    pub dataspace_type: DataspaceType,
}

impl DataspaceMessage {
    /// Total number of elements in the dataspace.
    pub fn num_elements(&self) -> Result<u64> {
        if self.dims.is_empty() {
            return Ok(match self.dataspace_type {
                DataspaceType::Scalar => 1,
                _ => 0,
            });
        }
        self.dims.iter().try_fold(1u64, |acc, &dim| {
            acc.checked_mul(dim).ok_or_else(|| {
                Error::InvalidData("dataspace element count overflows u64".to_string())
            })
        })
    }
}

/// Chunk indexing method for v4/v5 layout messages.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ChunkIndexing {
    SingleChunk {
        filtered_size: u64,
        filters: u32,
    },
    Implicit,
    FixedArray {
        page_bits: u8,
        chunk_size_len: u8,
    },
    ExtensibleArray {
        max_bits: u8,
        index_bits: u8,
        min_pointers: u8,
        min_elements: u8,
        chunk_size_len: u8,
    },
    BTreeV2,
}

/// Raw data storage layout for a dataset.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DataLayout {
    Compact {
        data: Vec<u8>,
    },
    Contiguous {
        address: u64,
        size: u64,
    },
    Chunked {
        address: u64,
        dims: Vec<u32>,
        element_size: u32,
        chunk_indexing: Option<ChunkIndexing>,
    },
}

/// Parsed data layout message.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DataLayoutMessage {
    pub layout: DataLayout,
}

/// When to write the fill value.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FillTime {
    IfSet,
    Always,
    Never,
}

/// Parsed fill value message.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FillValueMessage {
    pub defined: bool,
    pub fill_time: FillTime,
    pub value: Option<Vec<u8>>,
}

/// Standard HDF5 filter IDs.
pub const FILTER_DEFLATE: u16 = 1;
pub const FILTER_SHUFFLE: u16 = 2;
pub const FILTER_FLETCHER32: u16 = 3;
pub const FILTER_SZIP: u16 = 4;
pub const FILTER_NBIT: u16 = 5;
pub const FILTER_SCALEOFFSET: u16 = 6;
pub const FILTER_LZ4: u16 = 32004;

/// A single filter in a filter pipeline.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FilterDescription {
    pub id: u16,
    pub name: Option<String>,
    pub client_data: Vec<u32>,
}

/// Parsed filter pipeline message.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FilterPipelineMessage {
    pub filters: Vec<FilterDescription>,
}

/// A single external raw data file slot.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExternalFileSlot {
    pub name_offset: u64,
    pub offset: u64,
    pub size: u64,
}

/// Parsed external raw data files message.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExternalFilesMessage {
    pub heap_address: u64,
    pub slots: Vec<ExternalFileSlot>,
}

/// Where an HDF5 link points.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LinkTarget {
    Hard { address: u64 },
    Soft { path: String },
    External { filename: String, path: String },
}

/// Parsed link message.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LinkMessage {
    pub name: String,
    pub target: LinkTarget,
    pub creation_order: Option<u64>,
}

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

    #[test]
    fn jenkins_lookup3_is_deterministic() {
        assert_eq!(jenkins_lookup3(b"hello"), jenkins_lookup3(b"hello"));
        assert_ne!(jenkins_lookup3(b"hello"), jenkins_lookup3(b"world"));
    }

    #[test]
    fn jenkins_lookup3_handles_twelve_byte_boundary_like_lookup3() {
        let twelve = [0x5au8; 12];
        let thirteen = [0x5au8; 13];

        assert_ne!(jenkins_lookup3(&twelve), jenkins_lookup3(&thirteen));
        assert_eq!(jenkins_lookup3(&twelve), jenkins_lookup3(&twelve));
    }

    #[test]
    fn fletcher32_is_deterministic() {
        let data = [0x01, 0x02, 0x03, 0x04];

        assert_eq!(fletcher32(&data), fletcher32(&data));
    }

    #[test]
    fn fletcher32_matches_known_reference() {
        let data = [0x00, 0x01, 0x00, 0x02];

        assert_eq!(fletcher32(&data), 0x0004_0003);
    }

    #[test]
    fn fletcher32_handles_trailing_odd_byte_like_libhdf5() {
        // H5_checksum_fletcher32 folds a trailing odd byte in as the high
        // half of a final 16-bit word.
        let data = [0x01, 0x02, 0x03];

        assert_eq!(fletcher32(&data), 0x0504_0402);
    }
}