hdf5-pure-rust 0.2.2

Pure Rust implementation of the HDF5 file format. No C dependencies.
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
use std::collections::HashMap;
use std::io::{Seek, SeekFrom, Write};

use crate::engine::allocator::FileAllocator;
use crate::error::{Error, Result};
use crate::format::checksum::checksum_metadata;
use crate::format::object_header::*;
use crate::format::superblock::Superblock;
use crate::io::reader::UNDEF_ADDR;

/// A writable HDF5 file under construction.
pub struct HdfFileWriter<W: Write + Seek> {
    writer: W,
    allocator: FileAllocator,
    sizeof_addr: u8,
    sizeof_size: u8,
    /// Map of group path -> object header address.
    groups: HashMap<String, u64>,
    /// Pending links: (parent_path, child_name, child_addr).
    links: Vec<(String, String, u64)>,
    /// Pending attribute messages for the root group.
    pending_root_attrs: Vec<(u16, Vec<u8>)>,
    /// Pre-encoded link messages (for soft/external links): (parent_path, encoded_link_msg).
    special_links: Vec<(String, Vec<u8>)>,
}

/// Describes an attribute to attach.
pub struct AttrSpec<'a> {
    pub name: &'a str,
    pub shape: &'a [u64],
    pub dtype: DtypeSpec,
    pub data: &'a [u8],
}

/// Describes a dataset to create.
pub struct DatasetSpec<'a> {
    pub name: &'a str,
    pub shape: &'a [u64],
    pub dtype: DtypeSpec,
    pub data: &'a [u8],
}

/// Describes a datatype.
#[derive(Debug, Clone, Copy)]
pub enum DtypeSpec {
    F64,
    F32,
    I64,
    I32,
    I16,
    I8,
    U64,
    U32,
    U16,
    U8,
}

impl DtypeSpec {
    pub fn size(&self) -> u32 {
        match self {
            DtypeSpec::F64 | DtypeSpec::I64 | DtypeSpec::U64 => 8,
            DtypeSpec::F32 | DtypeSpec::I32 | DtypeSpec::U32 => 4,
            DtypeSpec::I16 | DtypeSpec::U16 => 2,
            DtypeSpec::I8 | DtypeSpec::U8 => 1,
        }
    }

    /// Encode as HDF5 datatype message bytes.
    pub fn encode(&self) -> Vec<u8> {
        let mut buf = Vec::new();

        match self {
            DtypeSpec::F32 | DtypeSpec::F64 => {
                let size = self.size();
                // class_and_version: version=1, class=1 (floating-point)
                let class_and_version = 0x11u8;
                // class bit fields:
                // bf0: bit0=byte order(0=LE), bits1-2=low pad, bits3-4=high pad, bits5-6=internal pad(2=mantissa norm implied 1)
                // bf1: sign bit location
                // bf2: reserved
                if size == 4 {
                    buf.push(class_and_version);
                    buf.extend_from_slice(&[0x20, 31, 0x00]); // bf0=0x20(norm=2,LE), bf1=31(sign bit), bf2=0
                    buf.extend_from_slice(&size.to_le_bytes());
                    // Properties: bit_offset(2) + bit_precision(2) + epos(1) + esize(1) + mpos(1) + msize(1) + ebias(4)
                    buf.extend_from_slice(&0u16.to_le_bytes());   // bit offset
                    buf.extend_from_slice(&32u16.to_le_bytes());  // bit precision
                    buf.push(23);  // exponent position
                    buf.push(8);   // exponent size
                    buf.push(0);   // mantissa position
                    buf.push(23);  // mantissa size
                    buf.extend_from_slice(&127u32.to_le_bytes()); // exponent bias
                } else {
                    buf.push(class_and_version);
                    buf.extend_from_slice(&[0x20, 63, 0x00]); // bf0=0x20(norm=2,LE), bf1=63(sign bit), bf2=0
                    buf.extend_from_slice(&size.to_le_bytes());
                    buf.extend_from_slice(&0u16.to_le_bytes());    // bit offset
                    buf.extend_from_slice(&64u16.to_le_bytes());   // bit precision
                    buf.push(52);   // exponent position
                    buf.push(11);   // exponent size
                    buf.push(0);    // mantissa position
                    buf.push(52);   // mantissa size
                    buf.extend_from_slice(&1023u32.to_le_bytes()); // exponent bias
                }
            }
            _ => {
                let size = self.size();
                let is_signed = matches!(self, DtypeSpec::I8 | DtypeSpec::I16 | DtypeSpec::I32 | DtypeSpec::I64);
                // class_and_version: version=1, class=0 (fixed-point)
                let class_and_version = 0x10u8; // version 1, class 0
                // class bit fields: byte order = LE (bit 0 = 0), sign = signed (bit 3)
                let bf0 = if is_signed { 0x08u8 } else { 0x00u8 };
                buf.push(class_and_version);
                buf.extend_from_slice(&[bf0, 0x00, 0x00]);
                buf.extend_from_slice(&size.to_le_bytes());

                // Properties: bit offset(2) + bit precision(2)
                buf.extend_from_slice(&0u16.to_le_bytes()); // bit offset
                buf.extend_from_slice(&(size as u16 * 8).to_le_bytes()); // bit precision
            }
        }

        buf
    }
}

/// Encode a dataspace message.
fn encode_dataspace(shape: &[u64]) -> Vec<u8> {
    let mut buf = Vec::new();

    if shape.is_empty() {
        // Scalar
        buf.push(2); // version 2
        buf.push(0); // ndims
        buf.push(0); // flags
        buf.push(0); // type = scalar
    } else {
        buf.push(2); // version 2
        buf.push(shape.len() as u8); // ndims
        buf.push(0); // flags (no max dims)
        buf.push(1); // type = simple
        for &d in shape {
            buf.extend_from_slice(&d.to_le_bytes());
        }
    }

    buf
}

/// Encode a link message (v1, hard link).
fn encode_link_message(name: &str, target_addr: u64, sizeof_addr: u8) -> Vec<u8> {
    let mut buf = Vec::new();

    let name_bytes = name.as_bytes();
    let name_len = name_bytes.len();

    // Determine size-of-length encoding
    let (size_flag, len_bytes) = if name_len < 256 {
        (0u8, 1usize)
    } else if name_len < 65536 {
        (1u8, 2usize)
    } else {
        (2u8, 4usize)
    };

    // Version
    buf.push(1);

    // Flags: size_flag | has_char_encoding(0x10)
    let flags = size_flag | 0x10;
    buf.push(flags);

    // Character encoding: UTF-8 = 1
    buf.push(1);

    // Name length
    match len_bytes {
        1 => buf.push(name_len as u8),
        2 => buf.extend_from_slice(&(name_len as u16).to_le_bytes()),
        4 => buf.extend_from_slice(&(name_len as u32).to_le_bytes()),
        _ => unreachable!(),
    }

    // Name
    buf.extend_from_slice(name_bytes);

    // Hard link target address
    let addr_bytes = target_addr.to_le_bytes();
    buf.extend_from_slice(&addr_bytes[..sizeof_addr as usize]);

    buf
}

/// Encode a soft link message (v1).
fn encode_soft_link_message(name: &str, target_path: &str) -> Vec<u8> {
    let mut buf = Vec::new();
    let name_bytes = name.as_bytes();
    let name_len = name_bytes.len();
    let target_bytes = target_path.as_bytes();

    let (size_flag, len_bytes) = if name_len < 256 { (0u8, 1) } else if name_len < 65536 { (1u8, 2) } else { (2u8, 4) };

    buf.push(1); // version
    buf.push(size_flag | 0x08 | 0x10); // flags: size_flag + has_link_type + has_char_encoding

    buf.push(1); // link type = soft
    buf.push(1); // char encoding = UTF-8

    match len_bytes {
        1 => buf.push(name_len as u8),
        2 => buf.extend_from_slice(&(name_len as u16).to_le_bytes()),
        4 => buf.extend_from_slice(&(name_len as u32).to_le_bytes()),
        _ => unreachable!(),
    }
    buf.extend_from_slice(name_bytes);

    // Soft link value: target_length(2) + target_path
    buf.extend_from_slice(&(target_bytes.len() as u16).to_le_bytes());
    buf.extend_from_slice(target_bytes);

    buf
}

/// Encode an external link message (v1).
fn encode_external_link_message(name: &str, filename: &str, obj_path: &str) -> Vec<u8> {
    let mut buf = Vec::new();
    let name_bytes = name.as_bytes();
    let name_len = name_bytes.len();

    let (size_flag, len_bytes) = if name_len < 256 { (0u8, 1) } else if name_len < 65536 { (1u8, 2) } else { (2u8, 4) };

    buf.push(1); // version
    buf.push(size_flag | 0x08 | 0x10); // flags: size_flag + has_link_type + has_char_encoding

    buf.push(64); // link type = external
    buf.push(1); // char encoding = UTF-8

    match len_bytes {
        1 => buf.push(name_len as u8),
        2 => buf.extend_from_slice(&(name_len as u16).to_le_bytes()),
        4 => buf.extend_from_slice(&(name_len as u32).to_le_bytes()),
        _ => unreachable!(),
    }
    buf.extend_from_slice(name_bytes);

    // External link value: info_length(2) + version(1) + filename(null-term) + obj_path(null-term)
    // Version 0: no flags byte
    let info_len = 1 + filename.len() + 1 + obj_path.len() + 1;
    buf.extend_from_slice(&(info_len as u16).to_le_bytes());
    buf.push(0); // ext version = 0 (no flags byte)
    buf.extend_from_slice(filename.as_bytes());
    buf.push(0); // null terminator
    buf.extend_from_slice(obj_path.as_bytes());
    buf.push(0); // null terminator

    buf
}

/// Encode a data layout message (v3, contiguous).
fn encode_contiguous_layout(data_addr: u64, data_size: u64, sizeof_addr: u8, sizeof_size: u8) -> Vec<u8> {
    let mut buf = Vec::new();
    buf.push(3); // version 3
    buf.push(1); // layout class = contiguous

    let addr_bytes = data_addr.to_le_bytes();
    buf.extend_from_slice(&addr_bytes[..sizeof_addr as usize]);

    let size_bytes = data_size.to_le_bytes();
    buf.extend_from_slice(&size_bytes[..sizeof_size as usize]);

    buf
}

/// Encode a data layout message (v3, chunked).
fn encode_chunked_layout_v3(
    btree_addr: u64,
    chunk_dims: &[u64],
    element_size: u32,
    sizeof_addr: u8,
) -> Vec<u8> {
    let mut buf = Vec::new();
    buf.push(3); // version 3
    buf.push(2); // layout class = chunked

    // ndims = chunk_dims.len() + 1 (extra dim for element size)
    let ndims = chunk_dims.len() + 1;
    buf.push(ndims as u8);

    // B-tree address
    let addr_bytes = btree_addr.to_le_bytes();
    buf.extend_from_slice(&addr_bytes[..sizeof_addr as usize]);

    // Chunk dimensions (each 4 bytes) + element size as last dim
    for &d in chunk_dims {
        buf.extend_from_slice(&(d as u32).to_le_bytes());
    }
    buf.extend_from_slice(&element_size.to_le_bytes());

    buf
}

/// Encode a filter pipeline message.
fn encode_filter_pipeline(compression_level: Option<u32>, shuffle: bool) -> Vec<u8> {
    let mut filters = Vec::new();

    if shuffle {
        filters.push((2u16, Vec::<u32>::new())); // SHUFFLE, no params
    }
    if let Some(level) = compression_level {
        filters.push((1u16, vec![level])); // DEFLATE, 1 param = level
    }

    if filters.is_empty() {
        return Vec::new();
    }

    let mut buf = Vec::new();
    buf.push(2); // version 2
    buf.push(filters.len() as u8); // number of filters

    for (id, params) in &filters {
        buf.extend_from_slice(&id.to_le_bytes()); // filter ID
        // v2: skip name_length for known filter IDs (< 256)
        buf.extend_from_slice(&0u16.to_le_bytes()); // flags
        buf.extend_from_slice(&(params.len() as u16).to_le_bytes()); // number of client data values
        for &p in params {
            buf.extend_from_slice(&p.to_le_bytes());
        }
    }

    buf
}

/// Encode an attribute message (v3).
fn encode_attribute_message(name: &str, dtype: &DtypeSpec, shape: &[u64], data: &[u8]) -> Vec<u8> {
    let mut buf = Vec::new();

    let dtype_bytes = dtype.encode();
    let ds_bytes = encode_dataspace(shape);
    let name_bytes = name.as_bytes();
    let name_with_null = name_bytes.len() + 1; // include null terminator

    buf.push(3); // version 3
    buf.push(0); // flags
    buf.extend_from_slice(&(name_with_null as u16).to_le_bytes());
    buf.extend_from_slice(&(dtype_bytes.len() as u16).to_le_bytes());
    buf.extend_from_slice(&(ds_bytes.len() as u16).to_le_bytes());
    buf.push(0); // character encoding: ASCII

    buf.extend_from_slice(name_bytes);
    buf.push(0); // null terminator

    buf.extend_from_slice(&dtype_bytes);
    buf.extend_from_slice(&ds_bytes);
    buf.extend_from_slice(data);

    buf
}

/// Build a v2 object header from a list of messages.
fn build_v2_object_header(messages: &[(u16, &[u8])], flags: u8) -> Vec<u8> {
    // Calculate chunk 0 data size
    let mut chunk_data_size: usize = 0;
    for (_, data) in messages {
        // Message header: type(1) + size(2) + flags(1) = 4
        chunk_data_size += 4 + data.len();
    }

    // Determine chunk0 size encoding
    let (chunk0_flag, chunk0_bytes) = if chunk_data_size < 256 {
        (0u8, 1usize)
    } else if chunk_data_size < 65536 {
        (1u8, 2usize)
    } else {
        (2u8, 4usize)
    };

    let oh_flags = flags | chunk0_flag;

    let mut buf = Vec::new();

    // Magic
    buf.extend_from_slice(b"OHDR");
    // Version
    buf.push(2);
    // Flags
    buf.push(oh_flags);

    // Optional timestamps (if HDR_STORE_TIMES)
    if oh_flags & HDR_STORE_TIMES != 0 {
        let now = 0u32; // placeholder
        buf.extend_from_slice(&now.to_le_bytes()); // atime
        buf.extend_from_slice(&now.to_le_bytes()); // mtime
        buf.extend_from_slice(&now.to_le_bytes()); // ctime
        buf.extend_from_slice(&now.to_le_bytes()); // btime
    }

    // Chunk 0 data size
    match chunk0_bytes {
        1 => buf.push(chunk_data_size as u8),
        2 => buf.extend_from_slice(&(chunk_data_size as u16).to_le_bytes()),
        4 => buf.extend_from_slice(&(chunk_data_size as u32).to_le_bytes()),
        8 => buf.extend_from_slice(&(chunk_data_size as u64).to_le_bytes()),
        _ => unreachable!(),
    }

    // Messages
    for (msg_type, data) in messages {
        buf.push(*msg_type as u8); // type (1 byte in v2)
        buf.extend_from_slice(&(data.len() as u16).to_le_bytes()); // size
        buf.push(0); // flags
        buf.extend_from_slice(data);
    }

    // Checksum over everything so far
    let checksum = checksum_metadata(&buf);
    buf.extend_from_slice(&checksum.to_le_bytes());

    buf
}

impl<W: Write + Seek> HdfFileWriter<W> {
    /// Create a new HDF5 file writer.
    pub fn new(writer: W) -> Self {
        Self {
            writer,
            allocator: FileAllocator::new(0),
            sizeof_addr: 8,
            sizeof_size: 8,
            groups: HashMap::new(),
            links: Vec::new(),
            pending_root_attrs: Vec::new(),
            special_links: Vec::new(),
        }
    }

    /// Write the initial file structure: superblock placeholder.
    /// Call finalize() when done to write the superblock with correct EOF.
    pub fn begin(&mut self) -> Result<()> {
        // Reserve space for superblock (v2 with 8-byte addresses: 48 bytes)
        let sb_size = 12 + 4 * self.sizeof_addr as u64 + 4;
        self.allocator = FileAllocator::new(sb_size);

        // Write placeholder bytes for superblock
        let zeros = vec![0u8; sb_size as usize];
        self.write_at(0, &zeros)?;

        Ok(())
    }

    /// Write an empty group object header (will be rewritten with links in finalize).
    fn write_group_object_header(&mut self, extra_messages: &[(u16, &[u8])]) -> Result<u64> {
        let messages: Vec<(u16, &[u8])> = extra_messages.to_vec();
        let oh_bytes = build_v2_object_header(&messages, 0);
        let oh_addr = self.allocator.allocate(oh_bytes.len() as u64, 8);
        self.write_at(oh_addr, &oh_bytes)?;
        Ok(oh_addr)
    }

    /// Create the root group.
    pub fn create_root_group(&mut self) -> Result<u64> {
        let addr = self.write_group_object_header(&[])?;
        self.groups.insert("/".to_string(), addr);
        Ok(addr)
    }

    /// Create a sub-group.
    pub fn create_group(&mut self, parent: &str, name: &str) -> Result<u64> {
        let addr = self.write_group_object_header(&[])?;
        let full_path = if parent == "/" {
            format!("/{name}")
        } else {
            format!("{parent}/{name}")
        };
        self.groups.insert(full_path.clone(), addr);
        self.links.push((parent.to_string(), name.to_string(), addr));
        Ok(addr)
    }

    /// Create a soft link in a group.
    pub fn create_soft_link(&mut self, parent: &str, name: &str, target_path: &str) {
        let msg = encode_soft_link_message(name, target_path);
        self.special_links.push((parent.to_string(), msg));
    }

    /// Create an external link in a group.
    pub fn create_external_link(&mut self, parent: &str, name: &str, filename: &str, obj_path: &str) {
        let msg = encode_external_link_message(name, filename, obj_path);
        self.special_links.push((parent.to_string(), msg));
    }

    /// Create a dataset with compact storage (data embedded in the object header).
    /// Best for small datasets (< ~64KB).
    pub fn create_compact_dataset(
        &mut self,
        parent: &str,
        spec: &DatasetSpec,
    ) -> Result<u64> {
        let dtype_bytes = spec.dtype.encode();
        let ds_bytes = encode_dataspace(spec.shape);

        // Compact layout: version 3, class 0, size(2) + data
        let mut layout_bytes = Vec::new();
        layout_bytes.push(3); // version 3
        layout_bytes.push(0); // class = compact
        layout_bytes.extend_from_slice(&(spec.data.len() as u16).to_le_bytes());
        layout_bytes.extend_from_slice(spec.data);

        let fill_value_bytes = vec![3u8, 0x09];

        let messages: Vec<(u16, &[u8])> = vec![
            (MSG_DATASPACE, &ds_bytes),
            (MSG_DATATYPE, &dtype_bytes),
            (MSG_FILL_VALUE, &fill_value_bytes),
            (MSG_LAYOUT, &layout_bytes),
        ];

        let oh_bytes = build_v2_object_header(&messages, 0);
        let oh_addr = self.allocator.allocate(oh_bytes.len() as u64, 8);
        self.write_at(oh_addr, &oh_bytes)?;

        self.links.push((parent.to_string(), spec.name.to_string(), oh_addr));

        Ok(oh_addr)
    }

    /// Create a dataset with contiguous storage.
    pub fn create_dataset(
        &mut self,
        parent: &str,
        spec: &DatasetSpec,
    ) -> Result<u64> {
        let dtype_bytes = spec.dtype.encode();
        let ds_bytes = encode_dataspace(spec.shape);

        // Allocate space for the data
        let data_size = spec.data.len() as u64;
        let data_addr = if data_size > 0 {
            let addr = self.allocator.allocate(data_size, 8);
            self.write_at(addr, spec.data)?;
            addr
        } else {
            UNDEF_ADDR
        };

        let layout_bytes = encode_contiguous_layout(
            data_addr,
            data_size,
            self.sizeof_addr,
            self.sizeof_size,
        );

        // Fill value message (v3, "undefined" fill value)
        let fill_value_bytes = vec![3u8, 0x09]; // version 3, flags=0x09 (never write, undefined)

        let messages: Vec<(u16, &[u8])> = vec![
            (MSG_DATASPACE, &ds_bytes),
            (MSG_DATATYPE, &dtype_bytes),
            (MSG_FILL_VALUE, &fill_value_bytes),
            (MSG_LAYOUT, &layout_bytes),
        ];

        let oh_bytes = build_v2_object_header(&messages, 0);
        let oh_addr = self.allocator.allocate(oh_bytes.len() as u64, 8);
        self.write_at(oh_addr, &oh_bytes)?;

        self.links.push((parent.to_string(), spec.name.to_string(), oh_addr));

        Ok(oh_addr)
    }

    /// Create a dataset with attributes.
    pub fn create_dataset_with_attrs(
        &mut self,
        parent: &str,
        spec: &DatasetSpec,
        attrs: &[AttrSpec],
    ) -> Result<u64> {
        let dtype_bytes = spec.dtype.encode();
        let ds_bytes = encode_dataspace(spec.shape);

        let data_size = spec.data.len() as u64;
        let data_addr = if data_size > 0 {
            let addr = self.allocator.allocate(data_size, 8);
            self.write_at(addr, spec.data)?;
            addr
        } else {
            UNDEF_ADDR
        };

        let layout_bytes = encode_contiguous_layout(data_addr, data_size, self.sizeof_addr, self.sizeof_size);
        let fill_value_bytes = vec![3u8, 0x09];

        let mut messages: Vec<(u16, Vec<u8>)> = vec![
            (MSG_DATASPACE, ds_bytes),
            (MSG_DATATYPE, dtype_bytes),
            (MSG_FILL_VALUE, fill_value_bytes),
            (MSG_LAYOUT, layout_bytes),
        ];

        for attr in attrs {
            let attr_bytes = encode_attribute_message(attr.name, &attr.dtype, attr.shape, attr.data);
            messages.push((MSG_ATTRIBUTE, attr_bytes));
        }

        let msg_refs: Vec<(u16, &[u8])> = messages.iter().map(|(t, d)| (*t, d.as_slice())).collect();
        let oh_bytes = build_v2_object_header(&msg_refs, 0);
        let oh_addr = self.allocator.allocate(oh_bytes.len() as u64, 8);
        self.write_at(oh_addr, &oh_bytes)?;

        self.links.push((parent.to_string(), spec.name.to_string(), oh_addr));

        Ok(oh_addr)
    }

    /// Add attributes to the root group (call before finalize).
    pub fn set_root_attrs(&mut self, attrs: Vec<(u16, Vec<u8>)>) {
        // Store as pending attribute messages for the root group
        // These will be included when finalize rewrites the root group
        for (msg_type, data) in attrs {
            self.pending_root_attrs.push((msg_type, data));
        }
    }

    /// Create a root group attribute from spec.
    pub fn add_root_attr(&mut self, attr: &AttrSpec) {
        let attr_bytes = encode_attribute_message(attr.name, &attr.dtype, attr.shape, attr.data);
        self.pending_root_attrs.push((MSG_ATTRIBUTE, attr_bytes));
    }

    /// Create a chunked dataset with optional compression.
    pub fn create_chunked_dataset(
        &mut self,
        parent: &str,
        spec: &DatasetSpec,
        chunk_dims: &[u64],
        compression_level: Option<u32>,
        shuffle: bool,
    ) -> Result<u64> {
        let dtype_bytes = spec.dtype.encode();
        let ds_bytes = encode_dataspace(spec.shape);
        let element_size = spec.dtype.size() as usize;
        let ndims = spec.shape.len();

        // Split data into chunks, apply filters, write each chunk
        let chunk_elements: usize = chunk_dims.iter().map(|&d| d as usize).product();
        let chunk_raw_bytes = chunk_elements * element_size;

        // Calculate number of chunks per dimension
        let mut n_chunks_per_dim = Vec::with_capacity(ndims);
        for i in 0..ndims {
            n_chunks_per_dim.push(((spec.shape[i] as usize) + chunk_dims[i] as usize - 1) / chunk_dims[i] as usize);
        }
        let total_chunks: usize = n_chunks_per_dim.iter().product();

        // Write each chunk and collect (coords, addr, compressed_size)
        let mut chunk_entries: Vec<(Vec<u64>, u64, u32)> = Vec::new();

        for chunk_idx in 0..total_chunks {
            // Calculate chunk coordinates
            let mut coords = vec![0u64; ndims];
            let mut rem = chunk_idx;
            for d in (0..ndims).rev() {
                coords[d] = (rem % n_chunks_per_dim[d]) as u64 * chunk_dims[d];
                rem /= n_chunks_per_dim[d];
            }

            // Extract chunk data from the source array
            let mut chunk_buf = vec![0u8; chunk_raw_bytes];
            self.extract_chunk(spec.data, spec.shape, &coords, chunk_dims, element_size, &mut chunk_buf);

            // Apply filters in forward order
            let mut filtered = chunk_buf;
            if shuffle {
                filtered = crate::filters::shuffle::shuffle(&filtered, element_size)?;
            }
            if let Some(level) = compression_level {
                filtered = crate::filters::deflate::compress(&filtered, level)?;
            }

            let compressed_size = filtered.len() as u32;
            let addr = self.allocator.allocate(filtered.len() as u64, 1);
            self.write_at(addr, &filtered)?;

            chunk_entries.push((coords, addr, compressed_size));
        }

        // Write v1 B-tree for chunk index
        let btree_addr = self.write_chunk_btree_v1(&chunk_entries, ndims, element_size as u32)?;

        // Encode layout message (v3 chunked)
        let layout_bytes = encode_chunked_layout_v3(
            btree_addr,
            chunk_dims,
            element_size as u32,
            self.sizeof_addr,
        );

        // Encode filter pipeline message
        let pipeline_bytes = encode_filter_pipeline(compression_level, shuffle);

        // Fill value message
        let fill_value_bytes = vec![3u8, 0x09];

        let mut messages: Vec<(u16, &[u8])> = vec![
            (MSG_DATASPACE, &ds_bytes),
            (MSG_DATATYPE, &dtype_bytes),
            (MSG_FILL_VALUE, &fill_value_bytes),
            (MSG_LAYOUT, &layout_bytes),
        ];
        if !pipeline_bytes.is_empty() {
            messages.push((MSG_FILTER_PIPELINE, &pipeline_bytes));
        }

        let oh_bytes = build_v2_object_header(&messages, 0);
        let oh_addr = self.allocator.allocate(oh_bytes.len() as u64, 8);
        self.write_at(oh_addr, &oh_bytes)?;

        self.links.push((parent.to_string(), spec.name.to_string(), oh_addr));

        Ok(oh_addr)
    }

    /// Extract chunk data from a flat array.
    fn extract_chunk(
        &self,
        data: &[u8],
        shape: &[u64],
        chunk_start: &[u64],
        chunk_dims: &[u64],
        element_size: usize,
        out: &mut [u8],
    ) {
        let ndims = shape.len();
        if ndims == 1 {
            // Fast path for 1D
            let start = chunk_start[0] as usize;
            let chunk_len = chunk_dims[0] as usize;
            let data_len = shape[0] as usize;
            let copy_len = chunk_len.min(data_len - start);
            let src_start = start * element_size;
            let src_end = src_start + copy_len * element_size;
            out[..copy_len * element_size].copy_from_slice(&data[src_start..src_end]);
        } else {
            // General N-D: iterate over elements
            let chunk_elements: usize = chunk_dims.iter().map(|&d| d as usize).product();
            let mut idx = vec![0usize; ndims];

            for elem in 0..chunk_elements {
                // Convert linear index within chunk to N-D
                let mut rem = elem;
                for d in (0..ndims).rev() {
                    idx[d] = rem % chunk_dims[d] as usize;
                    rem /= chunk_dims[d] as usize;
                }

                // Global position
                let mut in_bounds = true;
                let mut src_linear = 0usize;
                let mut stride = 1usize;
                for d in (0..ndims).rev() {
                    let global = chunk_start[d] as usize + idx[d];
                    if global >= shape[d] as usize {
                        in_bounds = false;
                        break;
                    }
                    src_linear += global * stride;
                    stride *= shape[d] as usize;
                }

                if in_bounds {
                    let src_offset = src_linear * element_size;
                    let dst_offset = elem * element_size;
                    if src_offset + element_size <= data.len() && dst_offset + element_size <= out.len() {
                        out[dst_offset..dst_offset + element_size]
                            .copy_from_slice(&data[src_offset..src_offset + element_size]);
                    }
                }
            }
        }
    }

    /// Write a v1 B-tree leaf node for chunk index.
    fn write_chunk_btree_v1(
        &mut self,
        chunks: &[(Vec<u64>, u64, u32)],
        ndims: usize,
        element_size: u32,
    ) -> Result<u64> {
        let sa = self.sizeof_addr as usize;
        let nchunks = chunks.len();

        // Key = chunk_size(4) + filter_mask(4) + (ndims+1)*8
        let key_size = 4 + 4 + (ndims + 1) * 8;

        // The C library allocates B-tree node read buffer based on max capacity (2*K+1 keys, 2*K children).
        // Default chunk btree K = 32, so max entries = 64.
        // Max node size = header(4+1+1+2+sa*2) + (2K+1)*key_size + (2K)*sa
        let btree_k: usize = 32; // HDF5_BTREE_CHUNK_IK_DEF
        let max_entries = 2 * btree_k;
        let header_size = 4 + 1 + 1 + 2 + sa * 2;
        let max_node_size = header_size + (max_entries + 1) * key_size + max_entries * sa;

        let mut buf = Vec::with_capacity(max_node_size);

        // Magic
        buf.extend_from_slice(b"TREE");
        // Type = 1 (raw data chunks)
        buf.push(1);
        // Level = 0 (leaf)
        buf.push(0);
        // Number of entries
        buf.extend_from_slice(&(nchunks as u16).to_le_bytes());
        // Left sibling = UNDEF
        let undef = UNDEF_ADDR.to_le_bytes();
        buf.extend_from_slice(&undef[..sa]);
        // Right sibling = UNDEF
        buf.extend_from_slice(&undef[..sa]);

        // Entries: key + child alternating
        for (coords, addr, compressed_size) in chunks {
            // Key: chunk_size(4) + filter_mask(4) + coords(ndims*8) + extra_dim(8)
            buf.extend_from_slice(&compressed_size.to_le_bytes());
            buf.extend_from_slice(&0u32.to_le_bytes()); // filter_mask = 0 (all filters applied)
            for &c in coords {
                buf.extend_from_slice(&c.to_le_bytes());
            }
            buf.extend_from_slice(&0u64.to_le_bytes()); // extra dimension = 0

            // Child: chunk address
            let addr_bytes = addr.to_le_bytes();
            buf.extend_from_slice(&addr_bytes[..sa]);
        }

        // Final key (sentinel)
        buf.extend_from_slice(&0u32.to_le_bytes()); // chunk_size = 0
        buf.extend_from_slice(&0u32.to_le_bytes()); // filter_mask = 0
        // Final coords = last chunk's coords (or data shape end)
        if let Some((last_coords, _, _)) = chunks.last() {
            for &c in last_coords {
                buf.extend_from_slice(&c.to_le_bytes());
            }
        } else {
            for _ in 0..ndims {
                buf.extend_from_slice(&0u64.to_le_bytes());
            }
        }
        // Extra dim for final key = element size (required by C library)
        buf.extend_from_slice(&(element_size as u64).to_le_bytes());

        // Pad to max node size so the C library can read the full expected buffer
        buf.resize(max_node_size, 0);

        let btree_addr = self.allocator.allocate(buf.len() as u64, 8);
        self.write_at(btree_addr, &buf)?;

        Ok(btree_addr)
    }

    /// Finalize the file: update root group with links and write superblock.
    pub fn finalize(&mut self) -> Result<()> {
        // Sort groups by depth (deepest first) so child groups are written
        // before their parents, and parent links point to correct addresses.
        let mut group_paths: Vec<String> = self.groups.keys().cloned().collect();
        group_paths.sort_by(|a, b| {
            let depth_a = a.split('/').filter(|s| !s.is_empty()).count();
            let depth_b = b.split('/').filter(|s| !s.is_empty()).count();
            depth_b.cmp(&depth_a) // deepest first
        });

        for path in group_paths {
            // Collect links for this group, using CURRENT addresses
            let group_links: Vec<(String, u64)> = self.links
                .iter()
                .filter(|(parent, _, _)| *parent == path)
                .map(|(_, name, addr)| {
                    // If the target is a group, use its updated address
                    let target_path = if path == "/" {
                        format!("/{name}")
                    } else {
                        format!("{path}/{name}")
                    };
                    let current_addr = self.groups.get(&target_path).copied().unwrap_or(*addr);
                    (name.clone(), current_addr)
                })
                .collect();

            if group_links.is_empty() && path != "/" {
                continue;
            }

            // Build messages: link info + link messages
            let mut messages: Vec<(u16, Vec<u8>)> = Vec::new();

            // Link info message (v0): flags=0, fractal_heap=UNDEF, name_btree=UNDEF
            let mut link_info = Vec::new();
            link_info.push(0); // version
            link_info.push(0); // flags
            let undef_bytes = UNDEF_ADDR.to_le_bytes();
            link_info.extend_from_slice(&undef_bytes[..self.sizeof_addr as usize]); // fractal heap
            link_info.extend_from_slice(&undef_bytes[..self.sizeof_addr as usize]); // name btree
            messages.push((MSG_LINK_INFO, link_info));

            for (name, addr) in &group_links {
                let link_bytes = encode_link_message(name, *addr, self.sizeof_addr);
                messages.push((MSG_LINK, link_bytes));
            }

            // Add special links (soft/external) for this group
            for (parent, link_data) in &self.special_links {
                if *parent == path {
                    messages.push((MSG_LINK, link_data.clone()));
                }
            }

            // Add pending root attributes
            if path == "/" {
                for (msg_type, attr_data) in &self.pending_root_attrs {
                    messages.push((*msg_type, attr_data.clone()));
                }
            }

            let msg_refs: Vec<(u16, &[u8])> = messages
                .iter()
                .map(|(t, d)| (*t, d.as_slice()))
                .collect();

            let oh_bytes = build_v2_object_header(&msg_refs, 0);
            let oh_addr = self.allocator.allocate(oh_bytes.len() as u64, 8);
            self.write_at(oh_addr, &oh_bytes)?;

            // Update the group address
            self.groups.insert(path.clone(), oh_addr);
        }

        // Write superblock
        let root_addr = *self.groups.get("/").ok_or_else(|| {
            Error::Other("no root group".into())
        })?;
        let eof = self.allocator.eof();

        let sb = Superblock {
            version: 2,
            sizeof_addr: self.sizeof_addr,
            sizeof_size: self.sizeof_size,
            status_flags: 0,
            base_addr: 0,
            ext_addr: UNDEF_ADDR,
            eof_addr: eof,
            root_addr,
            ..Default::default()
        };

        let mut sb_bytes = Vec::new();
        sb.write_v2(&mut sb_bytes);
        self.write_at(0, &sb_bytes)?;

        self.writer.flush()?;

        Ok(())
    }

    fn write_at(&mut self, offset: u64, data: &[u8]) -> Result<()> {
        self.writer.seek(SeekFrom::Start(offset))?;
        self.writer.write_all(data)?;
        Ok(())
    }
}