chdlady-core 0.1.0

Core container manipulation for CHD v5 format
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
//! CHD v5 container writer with parallel compression support.
use crate::chdfile::ChdParentReader;
use crate::codecs::{compress_hunk, CompressedHunk};
use crate::crc16::crc16;
use crate::error::ChdError;
use crate::header::{ChdHeader, CHD_V5_HEADER_SIZE, CHD_V5_SIGNATURE, CHD_V5_VERSION};
use crate::map::{write_v5_map, write_v5_uncompressed_map, HunkType, MapEntry};
use crate::metadata::{compute_overall_sha1, MetadataEntry};
use crate::progress::{OperationPhase, ProgressStatus};
#[cfg(feature = "rayon")]
use rayon::prelude::*;
use sha1::{Digest, Sha1};
use std::collections::HashMap;
use std::io::{Read, Seek, SeekFrom, Write};

/// Configuration parameters for creating a CHD v5 container.
#[derive(Debug, Clone)]
pub struct ChdWriteConfig {
    /// Logical media size in bytes.
    pub logical_bytes: u64,
    /// Size of each hunk in bytes.
    pub hunk_bytes: u32,
    /// Size of each unit within a hunk in bytes.
    pub unit_bytes: u32,
    /// FourCC compression codecs (up to 4 codecs, 0-terminated).
    pub compressors: [u32; 4],
    /// SHA-1 checksum of parent container, or zeros if standalone.
    pub parent_sha1: [u8; 20],
    /// Optional limit on worker threads for parallel compression.
    pub num_processors: Option<usize>,
}

/// Metadata item to write into the container.
#[derive(Debug, Clone)]
pub struct MetadataItem {
    /// 4-byte FourCC metadata tag.
    pub tag: u32,
    /// Metadata flags (e.g. `CHD_MDFLAGS_CHECKSUM`).
    pub flags: u8,
    /// Metadata payload bytes.
    pub value: Vec<u8>,
}

/// Result of a successful CHD container creation operation.
#[derive(Debug, Clone)]
pub struct CreateResult {
    /// The generated CHD container header.
    pub header: ChdHeader,
    /// Total logical uncompressed size in bytes.
    pub logical_bytes: u64,
    /// Final container file size in bytes.
    pub final_file_size: u64,
    /// Overall compression ratio (final_file_size / logical_bytes).
    pub final_ratio: f64,
}

impl std::ops::Deref for CreateResult {
    type Target = ChdHeader;
    fn deref(&self) -> &Self::Target {
        &self.header
    }
}

/// Writer for creating CHD v5 containers.
pub struct ChdWriter;

impl ChdWriter {
    /// Builds a lookup index of parent units mapping (CRC16, SHA1) to parent unit index.
    pub fn build_parent_map<P: ChdParentReader>(
        parent: &mut P,
        hunk_bytes: u32,
        unit_bytes: u32,
    ) -> Result<HashMap<(u16, [u8; 20]), u64>, ChdError> {
        Self::build_parent_map_with_progress(parent, hunk_bytes, unit_bytes, |_| {})
    }

    /// Builds a lookup index of parent units mapping (CRC16, SHA1) to parent unit index with progress reporting.
    pub fn build_parent_map_with_progress<P: ChdParentReader, F>(
        parent: &mut P,
        hunk_bytes: u32,
        _unit_bytes: u32,
        mut progress: F,
    ) -> Result<HashMap<(u16, [u8; 20]), u64>, ChdError>
    where
        F: FnMut(ProgressStatus),
    {
        let parent_hunk_bytes = parent.header().hunk_bytes;
        let parent_unit_bytes = parent.header().unit_bytes;
        let total_parent_hunks = parent.header().hunk_count();
        let mut map = HashMap::new();

        let mut hunk_buffer = vec![0u8; parent_hunk_bytes as usize];
        let units_per_hunk = parent_hunk_bytes / parent_unit_bytes;

        for h in 0..total_parent_hunks {
            progress(ProgressStatus::new(
                OperationPhase::ExaminingParent,
                h,
                total_parent_hunks,
                None,
            ));
            parent.read_hunk(h, &mut hunk_buffer)?;
            for u in 0..units_per_hunk {
                let unit_idx = h * (units_per_hunk as u64) + (u as u64);
                let start = (u * parent_unit_bytes) as usize;
                if start + (hunk_bytes as usize) <= hunk_buffer.len() {
                    let slice = &hunk_buffer[start..start + (hunk_bytes as usize)];
                    let crc = crc16(slice, 0xffff);
                    let mut sha = Sha1::new();
                    sha.update(slice);
                    let sha1: [u8; 20] = sha.finalize().into();
                    map.entry((crc, sha1)).or_insert(unit_idx);
                }
            }
        }
        progress(ProgressStatus::new(
            OperationPhase::ExaminingParent,
            total_parent_hunks,
            total_parent_hunks,
            None,
        ));
        Ok(map)
    }

    /// Writes a complete CHD container with optional parent unit matching.
    pub fn write_chd_with_parent<R, W, F>(
        reader: &mut R,
        writer: &mut W,
        config: &ChdWriteConfig,
        metadata: &[MetadataItem],
        parent_map: Option<&HashMap<(u16, [u8; 20]), u64>>,
        progress: F,
    ) -> Result<CreateResult, ChdError>
    where
        R: Read,
        W: Write + Seek,
        F: FnMut(ProgressStatus),
    {
        if config.hunk_bytes == 0 || config.unit_bytes == 0 {
            return Err(ChdError::InvalidData(
                "hunk size and unit size must be non-zero".into(),
            ));
        }
        if !config.hunk_bytes.is_multiple_of(config.unit_bytes) {
            return Err(ChdError::InvalidData(
                "hunk size must be a multiple of unit size".into(),
            ));
        }

        let hunk_count = if config.logical_bytes == 0 {
            0
        } else {
            config.logical_bytes.div_ceil(config.hunk_bytes as u64) as usize
        };
        let is_compressed = config.compressors[0] != 0;

        if is_compressed {
            Self::write_compressed_chd(
                reader, writer, config, metadata, hunk_count, parent_map, progress,
            )
        } else {
            Self::write_uncompressed_chd(
                reader, writer, config, metadata, hunk_count, parent_map, progress,
            )
        }
    }

    /// Writes a complete CHD v5 container from the provided input reader.
    pub fn write_chd<R, W, F>(
        reader: &mut R,
        writer: &mut W,
        config: &ChdWriteConfig,
        metadata: &[MetadataItem],
        progress: F,
    ) -> Result<CreateResult, ChdError>
    where
        R: Read,
        W: Write + Seek,
        F: FnMut(ProgressStatus),
    {
        Self::write_chd_with_parent(reader, writer, config, metadata, None, progress)
    }

    fn write_compressed_chd<R, W, F>(
        reader: &mut R,
        writer: &mut W,
        config: &ChdWriteConfig,
        metadata: &[MetadataItem],
        hunk_count: usize,
        parent_map: Option<&HashMap<(u16, [u8; 20]), u64>>,
        mut progress: F,
    ) -> Result<CreateResult, ChdError>
    where
        R: Read,
        W: Write + Seek,
        F: FnMut(ProgressStatus),
    {
        #[cfg(feature = "rayon")]
        let custom_pool = if let Some(np) = config.num_processors {
            if np > 0 {
                Some(
                    rayon::ThreadPoolBuilder::new()
                        .num_threads(np)
                        .build()
                        .map_err(|e| ChdError::Io(std::io::Error::other(e)))?,
                )
            } else {
                None
            }
        } else {
            None
        };

        writer.seek(SeekFrom::Start(0))?;
        writer.write_all(&[0u8; 124])?;

        let (meta_offset, metadata_entries) = write_metadata_chain(writer, metadata, 124)?;

        let mut cur_offset = writer.stream_position()?;
        let mut cur_hunk = 0usize;
        let mut raw_hasher = Sha1::new();
        let mut current_map: HashMap<(u16, [u8; 20]), u64> = HashMap::new();
        let mut map_entries = Vec::with_capacity(hunk_count);

        #[cfg(feature = "rayon")]
        let threads = custom_pool
            .as_ref()
            .map(|p| p.current_num_threads())
            .unwrap_or_else(rayon::current_num_threads);
        #[cfg(not(feature = "rayon"))]
        let threads = 1;

        let batch_size_limit = (threads * 32).clamp(64, 512);

        while cur_hunk < hunk_count {
            let batch_end = (cur_hunk + batch_size_limit).min(hunk_count);
            let batch_size = batch_end - cur_hunk;
            let mut raw_batch = Vec::with_capacity(batch_size);

            for i in 0..batch_size {
                let hunk_idx = cur_hunk + i;
                let mut hunk_buf = vec![0u8; config.hunk_bytes as usize];
                let expected_bytes = if hunk_idx == hunk_count - 1 {
                    let rem = (config.logical_bytes % (config.hunk_bytes as u64)) as usize;
                    if rem == 0 {
                        config.hunk_bytes as usize
                    } else {
                        rem
                    }
                } else {
                    config.hunk_bytes as usize
                };
                reader.read_exact(&mut hunk_buf[..expected_bytes])?;
                raw_hasher.update(&hunk_buf[..expected_bytes]);
                raw_batch.push(hunk_buf);
            }

            #[cfg(feature = "rayon")]
            let keys_task = || {
                raw_batch
                    .par_iter()
                    .map(|buf| {
                        let mut h_sha = Sha1::new();
                        h_sha.update(buf);
                        let sha: [u8; 20] = h_sha.finalize().into();
                        let crc = crate::crc16::crc16(buf, 0xffff);
                        (crc, sha)
                    })
                    .collect()
            };

            #[cfg(feature = "rayon")]
            let batch_keys: Vec<(u16, [u8; 20])> = if let Some(ref pool) = custom_pool {
                pool.install(keys_task)
            } else {
                keys_task()
            };

            #[cfg(not(feature = "rayon"))]
            let batch_keys: Vec<(u16, [u8; 20])> = raw_batch
                .iter()
                .map(|buf| {
                    let mut h_sha = Sha1::new();
                    h_sha.update(buf);
                    let sha: [u8; 20] = h_sha.finalize().into();
                    let crc = crate::crc16::crc16(buf, 0xffff);
                    (crc, sha)
                })
                .collect();

            enum BatchAction {
                SelfRef(u64),
                Parent(u64),
                Compress(usize),
            }

            let mut batch_actions = Vec::with_capacity(batch_size);
            let mut unique_to_compress = Vec::new();
            let mut batch_seen: std::collections::HashMap<(u16, [u8; 20]), u64> =
                std::collections::HashMap::new();

            for (i, &key) in batch_keys.iter().enumerate() {
                let hunk_idx = (cur_hunk + i) as u64;
                if let Some(&ref_hunk) = current_map.get(&key) {
                    batch_actions.push(BatchAction::SelfRef(ref_hunk));
                } else if let Some(&parent_unit) = parent_map.and_then(|pm| pm.get(&key)) {
                    batch_actions.push(BatchAction::Parent(parent_unit));
                } else if let Some(&earlier_hunk) = batch_seen.get(&key) {
                    batch_actions.push(BatchAction::SelfRef(earlier_hunk));
                } else {
                    let comp_idx = unique_to_compress.len();
                    unique_to_compress.push((i, &raw_batch[i]));
                    batch_seen.insert(key, hunk_idx);
                    batch_actions.push(BatchAction::Compress(comp_idx));
                }
            }

            #[cfg(feature = "rayon")]
            let compress_task = || {
                unique_to_compress
                    .par_iter()
                    .map(|(_orig_idx, buf)| compress_hunk(&config.compressors, buf))
                    .collect()
            };

            #[cfg(feature = "rayon")]
            let compressed_unique: Vec<CompressedHunk> = if let Some(ref pool) = custom_pool {
                pool.install(compress_task)
            } else {
                compress_task()
            };

            #[cfg(not(feature = "rayon"))]
            let compressed_unique: Vec<CompressedHunk> = unique_to_compress
                .iter()
                .map(|(_orig_idx, buf)| compress_hunk(&config.compressors, buf))
                .collect();

            for (i, action) in batch_actions.into_iter().enumerate() {
                let hunk_idx = (cur_hunk + i) as u64;
                match action {
                    BatchAction::SelfRef(ref_hunk) => {
                        map_entries.push(MapEntry {
                            hunk_type: HunkType::SelfRef(ref_hunk),
                            length: 0,
                            offset: ref_hunk,
                            crc16: 0,
                        });
                    }
                    BatchAction::Parent(parent_unit) => {
                        map_entries.push(MapEntry {
                            hunk_type: HunkType::Parent(parent_unit),
                            length: 0,
                            offset: parent_unit,
                            crc16: 0,
                        });
                    }
                    BatchAction::Compress(comp_idx) => {
                        let comp = &compressed_unique[comp_idx];
                        let offset = cur_offset;
                        writer.write_all(&comp.data)?;
                        cur_offset += comp.data.len() as u64;

                        let key = batch_keys[i];
                        current_map.insert(key, hunk_idx);
                        map_entries.push(MapEntry {
                            hunk_type: comp.hunk_type,
                            length: comp.data.len() as u32,
                            offset,
                            crc16: comp.crc16,
                        });
                    }
                }
            }

            cur_hunk += batch_size;
            let uncomp_so_far =
                ((cur_hunk as u64) * (config.hunk_bytes as u64)).min(config.logical_bytes);
            let instant_ratio = if uncomp_so_far > 0 {
                cur_offset as f64 / uncomp_so_far as f64
            } else {
                1.0
            };
            progress(ProgressStatus::new(
                OperationPhase::Compressing,
                cur_hunk as u64,
                hunk_count as u64,
                Some(instant_ratio),
            ));
        }

        let raw_sha1: [u8; 20] = raw_hasher.finalize().into();
        let map_offset = cur_offset;
        let (map_bytes, _) = write_v5_map(&map_entries, config.hunk_bytes, config.unit_bytes)?;
        writer.write_all(&map_bytes)?;

        let overall_sha1 = compute_overall_sha1(raw_sha1, &metadata_entries);

        let header = ChdHeader {
            tag: *CHD_V5_SIGNATURE,
            length: CHD_V5_HEADER_SIZE,
            version: CHD_V5_VERSION,
            compressors: config.compressors,
            logical_bytes: config.logical_bytes,
            map_offset,
            meta_offset,
            hunk_bytes: config.hunk_bytes,
            unit_bytes: config.unit_bytes,
            raw_sha1,
            sha1: overall_sha1,
            parent_sha1: config.parent_sha1,
        };
        header.write_v5(writer)?;

        let final_file_size = writer.seek(SeekFrom::End(0))?;
        let final_ratio = if config.logical_bytes > 0 {
            final_file_size as f64 / config.logical_bytes as f64
        } else {
            1.0
        };
        progress(ProgressStatus::new(
            OperationPhase::Compressing,
            hunk_count as u64,
            hunk_count as u64,
            Some(final_ratio),
        ));

        Ok(CreateResult {
            header,
            logical_bytes: config.logical_bytes,
            final_file_size,
            final_ratio,
        })
    }

    fn write_uncompressed_chd<R, W, F>(
        reader: &mut R,
        writer: &mut W,
        config: &ChdWriteConfig,
        metadata: &[MetadataItem],
        hunk_count: usize,
        parent_map: Option<&HashMap<(u16, [u8; 20]), u64>>,
        mut progress: F,
    ) -> Result<CreateResult, ChdError>
    where
        R: Read,
        W: Write + Seek,
        F: FnMut(ProgressStatus),
    {
        writer.seek(SeekFrom::Start(0))?;
        writer.write_all(&[0u8; 124])?;

        let map_offset = 124u64;
        let map_size = hunk_count * 4;
        write_zeros(writer, map_size)?;

        let (meta_offset, metadata_entries) =
            write_metadata_chain(writer, metadata, map_offset + map_size as u64)?;

        let mut cur_offset = writer.stream_position()?;
        // Align to hunk_bytes boundary for uncompressed data blocks
        let data_start = if cur_offset.is_multiple_of(config.hunk_bytes as u64) {
            cur_offset
        } else {
            let aligned =
                ((cur_offset / (config.hunk_bytes as u64)) + 1) * (config.hunk_bytes as u64);
            let pad = (aligned - cur_offset) as usize;
            write_zeros(writer, pad)?;
            aligned
        };
        cur_offset = data_start;

        let mut raw_hasher = Sha1::new();
        let mut map_entries = Vec::with_capacity(hunk_count);
        let mut hunk_buf = vec![0u8; config.hunk_bytes as usize];

        for hunk_idx in 0..hunk_count {
            let expected_bytes = if hunk_idx == hunk_count - 1 {
                let rem = (config.logical_bytes % (config.hunk_bytes as u64)) as usize;
                if rem == 0 {
                    config.hunk_bytes as usize
                } else {
                    rem
                }
            } else {
                config.hunk_bytes as usize
            };
            reader.read_exact(&mut hunk_buf[..expected_bytes])?;
            if expected_bytes < config.hunk_bytes as usize {
                hunk_buf[expected_bytes..].fill(0);
            }
            raw_hasher.update(&hunk_buf[..expected_bytes]);

            let is_all_zero = hunk_buf.iter().all(|&b| b == 0);

            if is_all_zero {
                map_entries.push(MapEntry {
                    hunk_type: HunkType::Parent(hunk_idx as u64),
                    length: 0,
                    offset: 0,
                    crc16: 0,
                });
            } else if let Some(pm) = parent_map {
                let crc = crc16(&hunk_buf, 0xffff);
                let mut sha = Sha1::new();
                sha.update(&hunk_buf);
                let sha1: [u8; 20] = sha.finalize().into();
                let key = (crc, sha1);

                if let Some(&parent_unit) = pm.get(&key) {
                    map_entries.push(MapEntry {
                        hunk_type: HunkType::Parent(parent_unit),
                        length: 0,
                        offset: 0,
                        crc16: 0,
                    });
                } else {
                    let offset = cur_offset;
                    writer.write_all(&hunk_buf)?;
                    cur_offset += config.hunk_bytes as u64;

                    map_entries.push(MapEntry {
                        hunk_type: HunkType::Uncompressed,
                        length: config.hunk_bytes,
                        offset,
                        crc16: 0,
                    });
                }
            } else {
                let offset = cur_offset;
                writer.write_all(&hunk_buf)?;
                cur_offset += config.hunk_bytes as u64;

                map_entries.push(MapEntry {
                    hunk_type: HunkType::Uncompressed,
                    length: config.hunk_bytes,
                    offset,
                    crc16: 0,
                });
            }

            if hunk_idx % 128 == 0 || hunk_idx == hunk_count - 1 {
                progress(ProgressStatus::new(
                    OperationPhase::Compressing,
                    (hunk_idx + 1) as u64,
                    hunk_count as u64,
                    Some(1.0),
                ));
            }
        }

        let uncomp_map_bytes = write_v5_uncompressed_map(&map_entries, config.hunk_bytes);
        writer.seek(SeekFrom::Start(map_offset))?;
        writer.write_all(&uncomp_map_bytes)?;

        let raw_sha1: [u8; 20] = raw_hasher.finalize().into();
        let overall_sha1 = compute_overall_sha1(raw_sha1, &metadata_entries);

        let header = ChdHeader {
            tag: *CHD_V5_SIGNATURE,
            length: CHD_V5_HEADER_SIZE,
            version: CHD_V5_VERSION,
            compressors: [0; 4],
            logical_bytes: config.logical_bytes,
            map_offset,
            meta_offset,
            hunk_bytes: config.hunk_bytes,
            unit_bytes: config.unit_bytes,
            raw_sha1,
            sha1: overall_sha1,
            parent_sha1: config.parent_sha1,
        };
        header.write_v5(writer)?;

        let final_file_size = writer.seek(SeekFrom::End(0))?;
        let final_ratio = if config.logical_bytes > 0 {
            final_file_size as f64 / config.logical_bytes as f64
        } else {
            1.0
        };
        progress(ProgressStatus::new(
            OperationPhase::Compressing,
            hunk_count as u64,
            hunk_count as u64,
            Some(final_ratio),
        ));

        Ok(CreateResult {
            header,
            logical_bytes: config.logical_bytes,
            final_file_size,
            final_ratio,
        })
    }
}

fn write_zeros<W: Write>(writer: &mut W, mut count: usize) -> Result<(), ChdError> {
    let buf = [0u8; 4096];
    while count > 0 {
        let chunk = count.min(buf.len());
        writer.write_all(&buf[..chunk])?;
        count -= chunk;
    }
    Ok(())
}

fn write_metadata_chain<W: Write>(
    writer: &mut W,
    metadata: &[MetadataItem],
    initial_offset: u64,
) -> Result<(u64, Vec<MetadataEntry>), ChdError> {
    if metadata.is_empty() {
        return Ok((0, Vec::new()));
    }
    let meta_offset = initial_offset;
    let mut cur_meta_offs = initial_offset;
    let mut metadata_entries = Vec::with_capacity(metadata.len());

    for (idx, item) in metadata.iter().enumerate() {
        let next_offs = if idx + 1 < metadata.len() {
            cur_meta_offs + 16 + (item.value.len() as u64)
        } else {
            0
        };
        let mut hdr = [0u8; 16];
        hdr[0..4].copy_from_slice(&item.tag.to_be_bytes());
        hdr[4] = item.flags;
        let val_len = (item.value.len() as u32).to_be_bytes();
        hdr[5..8].copy_from_slice(&val_len[1..4]);
        hdr[8..16].copy_from_slice(&next_offs.to_be_bytes());
        writer.write_all(&hdr)?;
        writer.write_all(&item.value)?;

        metadata_entries.push(MetadataEntry {
            metatag: item.tag,
            flags: item.flags,
            offset: cur_meta_offs,
            next: next_offs,
            value: item.value.clone(),
        });
        cur_meta_offs += 16 + (item.value.len() as u64);
    }
    Ok((meta_offset, metadata_entries))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::chdfile::ChdFile;
    use std::io::Cursor;
    use std::process::Command;

    #[test]
    fn test_write_compressed_chd_parity() {
        let logical_bytes = 65536u64;
        let hunk_bytes = 4096u32;
        let unit_bytes = 512u32;
        let test_data: Vec<u8> = (0..logical_bytes).map(|i| ((i * 17) % 256) as u8).collect();

        let config = ChdWriteConfig {
            logical_bytes,
            hunk_bytes,
            unit_bytes,
            compressors: [u32::from_be_bytes(*b"zlib"), 0, 0, 0],
            parent_sha1: [0u8; 20],
            num_processors: None,
        };

        let metadata = vec![MetadataItem {
            tag: u32::from_be_bytes(*b"TEST"),
            flags: crate::metadata::CHD_MDFLAGS_CHECKSUM,
            value: b"test_metadata".to_vec(),
        }];

        let mut input_cursor = Cursor::new(&test_data);
        let mut output_buf = Cursor::new(Vec::new());

        let header = ChdWriter::write_chd(
            &mut input_cursor,
            &mut output_buf,
            &config,
            &metadata,
            |_| {},
        )
        .expect("write_chd");

        assert_eq!(header.logical_bytes, logical_bytes);
        assert_eq!(header.hunk_bytes, hunk_bytes);

        // Verify with our own reader
        output_buf.seek(SeekFrom::Start(0)).unwrap();
        let mut chd = ChdFile::open(&mut output_buf).expect("open CHD");
        let vres = chd.verify(|_| {}).expect("verify CHD");
        assert!(vres.raw_sha1_matched);
        assert!(vres.overall_sha1_matched);

        let mut read_back = vec![0u8; logical_bytes as usize];
        chd.read_bytes(0, &mut read_back).expect("read bytes");
        assert_eq!(test_data, read_back);

        // Verify with official chdman verify if available
        let temp_dir = std::env::temp_dir().join(format!("chdlady_test_{}", std::process::id()));
        let _ = std::fs::create_dir_all(&temp_dir);
        let chd_path = temp_dir.join("test_zlib.chd");
        std::fs::write(&chd_path, output_buf.get_ref()).expect("write to disk");

        let status = Command::new("chdman")
            .arg("verify")
            .arg("-i")
            .arg(&chd_path)
            .output();

        if let Ok(out) = status {
            let stdout = String::from_utf8_lossy(&out.stdout);
            let stderr = String::from_utf8_lossy(&out.stderr);
            assert!(
                out.status.success(),
                "chdman verify failed!\nstdout: {}\nstderr: {}",
                stdout,
                stderr
            );
            assert!(
                stdout.contains("verification successful!") || stdout.contains("verified"),
                "chdman verify did not verify SHA1:\n{}",
                stdout
            );
        }

        let _ = std::fs::remove_dir_all(&temp_dir);
    }

    #[test]
    fn test_write_uncompressed_chd_parity() {
        let logical_bytes = 16384u64;
        let hunk_bytes = 4096u32;
        let unit_bytes = 512u32;
        let mut test_data = vec![0u8; logical_bytes as usize];
        for (i, byte) in test_data[4096..8192].iter_mut().enumerate() {
            let offset = 4096 + i;
            *byte = ((offset * 31) % 256) as u8;
        }

        let config = ChdWriteConfig {
            logical_bytes,
            hunk_bytes,
            unit_bytes,
            compressors: [0; 4],
            parent_sha1: [0u8; 20],
            num_processors: None,
        };

        let mut input_cursor = Cursor::new(&test_data);
        let mut output_buf = Cursor::new(Vec::new());

        ChdWriter::write_chd(&mut input_cursor, &mut output_buf, &config, &[], |_| {})
            .expect("write_chd uncompressed");

        // Verify with our own reader
        output_buf.seek(SeekFrom::Start(0)).unwrap();
        let mut chd = ChdFile::open(&mut output_buf).expect("open uncompressed CHD");
        let vres = chd.verify(|_| {}).expect("verify uncompressed CHD");
        assert!(vres.raw_sha1_matched);

        let mut read_back = vec![0u8; logical_bytes as usize];
        chd.read_bytes(0, &mut read_back).expect("read bytes");
        assert_eq!(test_data, read_back);

        // Verify with official chdman verify
        let temp_dir =
            std::env::temp_dir().join(format!("chdlady_test_uncomp_{}", std::process::id()));
        let _ = std::fs::create_dir_all(&temp_dir);
        let chd_path = temp_dir.join("test_uncomp.chd");
        std::fs::write(&chd_path, output_buf.get_ref()).expect("write to disk");

        let status = Command::new("chdman")
            .arg("verify")
            .arg("-i")
            .arg(&chd_path)
            .output();

        if let Ok(out) = status {
            let stdout = String::from_utf8_lossy(&out.stdout);
            let stderr = String::from_utf8_lossy(&out.stderr);
            assert!(
                out.status.success(),
                "chdman verify uncompressed failed!\nstdout: {}\nstderr: {}",
                stdout,
                stderr
            );
        }

        let _ = std::fs::remove_dir_all(&temp_dir);
    }
}