delharc 0.8.0

A library for parsing and extracting files from LHA/LZH archives.
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
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use core::{fmt::Write, num::Wrapping, slice};
use bytemuck::{NoUninit, AnyBitPattern, bytes_of_mut};
use crate::{
    error::{LhaError, LhaResult, LhaHeaderError},
    stub_io::Read,
    crc::Crc16,
};
use super::*;

/// Raw identifiers of extra headers.
pub mod ext {
    /// The "Common" header's CRC-16 field will always be reset to 0 in the parsed header data.
    /// This is the necessary condition to verify header's checksum.
    pub const EXT_HEADER_COMMON:       u8 = 0x00;
    /// The "File name" header may contain the entry's file name.
    pub const EXT_HEADER_FILENAME:     u8 = 0x01;
    /// The "Directory name" header may contain the directory of the entry.
    pub const EXT_HEADER_PATH:         u8 = 0x02;
    /// The "Multi-disc" header
    pub const EXT_HEADER_MULTI_DISC:   u8 = 0x39;
    /// The "Comment" header
    pub const EXT_HEADER_COMMENT:      u8 = 0x3F;
    /// The MS-DOS ["Attributes"](super::MsDosAttrs) header
    pub const EXT_HEADER_MSDOS_ATTRS:  u8 = 0x40;
    /// The "Windows time stamp" header
    pub const EXT_HEADER_WINDOWS_TIME: u8 = 0x41;
    /// An alias of [`EXT_HEADER_WINDOWS_TIME`]
    #[deprecated(note="please use `EXT_HEADER_WINDOWS_TIME` instead")]
    pub const EXT_HEADER_MSDOS_TIME:   u8 = EXT_HEADER_WINDOWS_TIME;
    /// The "File size" header with 64-bit file size information
    pub const EXT_HEADER_FILE_SIZES:   u8 = 0x42;
    /// An alias of [`EXT_HEADER_FILE_SIZES`]
    #[deprecated(note="please use `EXT_HEADER_FILE_SIZES` instead")]
    pub const EXT_HEADER_MSDOS_SIZE:   u8 = EXT_HEADER_FILE_SIZES;
    /// The UNIX ["Permission"](super::Permissions) header
    pub const EXT_HEADER_UNIX_PERM:    u8 = 0x50;
    /// The UNIX "GID UID" header
    pub const EXT_HEADER_UNIX_UIDGID:  u8 = 0x51;
    /// The UNIX "Group name" header
    pub const EXT_HEADER_UNIX_GROUP:   u8 = 0x52;
    /// The UNIX "User name" header
    pub const EXT_HEADER_UNIX_OWNER:   u8 = 0x53;
    /// The UNIX "Time stamp" header
    pub const EXT_HEADER_UNIX_TIME:    u8 = 0x54;
    /// The Mac "Capsule" header
    pub const EXT_HEADER_MAC_CAPSULE:  u8 = 0x7D;
    /// The OS/2 extended attributes header
    pub const EXT_HEADER_OS2_ATTR1:    u8 = 0x7E;
    /// Level 3 extended attributes header
    pub const EXT_HEADER_EXT_ATTRS:    u8 = 0x7F;
    /// The OS/9 extended attributes header
    pub const EXT_HEADER_OS9:          u8 = 0xCC;
    /// The metadata header, currently used by MorphOS to store file comments
    pub const EXT_HEADER_METADATA:     u8 = 0x71;
}

use ext::*;
/// An iterator through extra headers, yielding the headers' raw content excluding
/// the next header length field.
pub struct ExtraHeaderIter<'a> {
    data: &'a [u8],
    header_length: u32,
    header_len32: bool
}

impl<'a> Iterator for ExtraHeaderIter<'a> {
    type Item = &'a [u8];

    fn next(&mut self) -> Option<Self::Item> {
        let header_length = self.header_length as usize;
        if header_length == 0 {
            return None
        }
        let (res, data) = self.data.split_at(header_length);
        let (res, len) = if self.header_len32 {
            res.split_last_chunk::<4>().map(|(dat, &len)|
                (dat, u32::from_le_bytes(len)))
        }
        else {
            res.split_last_chunk::<2>().map(|(dat, &len)|
                (dat, u16::from_le_bytes(len).into()))
        }.unwrap();
        self.header_length = len;
        self.data = data;
        Some(res)
    }
}

/// Allocate at once this number of bytes maximum when reading variable size fields
const ALLOCATE_LIMIT_MAX: usize = 8*1024;

/// The raw LHA header fragment with a rigid structure
#[derive(Clone, Copy, Debug, Default, NoUninit, AnyBitPattern)]
#[repr(C)]
#[repr(packed)]
struct LhaRawBaseHeader {
    compression: [u8;5],
    compressed_size: [u8;4],
    original_size: [u8;4],
    last_modified: [u8;4],
    msdos_attrs: u8,
    lha_level: u8
}

/// The internal header parser object
struct Parser<'a, R> {
    rd: &'a mut R,
    /// A collected header's CRC-16 checksum
    crc: Crc16,
    /// A collected header's wrapping sum checksum
    csum: Wrapping<u8>,
    /// The number of bytes parsed so far
    len: usize
}

impl<R: Read> Parser<'_, R> {
    /// Read a next byte if there is one more in the stream increasing
    /// the parsed counter and updating the header CRC-16 checksum.
    ///
    /// NOTE: this function does not update the wrapping sum.
    fn read_u8_or_none(&mut self) -> LhaResult<Option<u8>, R> {
        let mut byte = 0u8;
        if 0 == self.rd.read_all(slice::from_mut(&mut byte)).map_err(LhaError::Io)? {
            return Ok(None)
        }
        self.update_checksums_no_wrapping_sum(slice::from_ref(&byte));
        Ok(Some(byte))
    }
    /// Read the next byte, increase the parsed counter and update all checksums
    fn read_u8(&mut self) -> LhaResult<u8, R> {
        let mut byte: u8 = 0;
        self.read_exact(slice::from_mut(&mut byte))?;
        Ok(byte)
    }
    /// Read the next 2 bytes, increase the parsed counter and update all checksums.
    /// Return an LE 16-bit value.
    fn read_u16(&mut self) -> LhaResult<u16, R> {
        let mut buf = [0u8;2];
        self.read_exact(&mut buf)?;
        Ok(u16::from_le_bytes(buf))
    }
    /// Read the next 4 bytes, increase the parsed counter and update all checksums.
    /// Return an LE 32-bit value.
    fn read_u32(&mut self) -> LhaResult<u32, R> {
        let mut buf = [0u8;4];
        self.read_exact(&mut buf)?;
        Ok(u32::from_le_bytes(buf))
    }
    /// Read the exact number of bytes, increase the parsed counter and update all
    /// checksums.
    fn read_exact(&mut self, buf: &mut [u8]) -> LhaResult<(), R> {
        self.rd.read_exact(buf).map_err(LhaError::Io)?;
        self.update_checksums(buf);
        Ok(())
    }
    /// Read the `limit` bytes into an newly allocated boxed slice, increase the
    /// parsed counter and update all checksums.
    fn read_limit(&mut self, limit: usize) -> LhaResult<Box<[u8]>, R> {
        let mut buf = Vec::new();
        self.read_limit_no_checksums(limit, &mut buf)?;
        self.update_checksums(&buf);
        Ok(buf.into_boxed_slice())
    }
    /// Increase the parser counter and update all header checksums from data
    fn update_checksums(&mut self, data: &[u8]) {
        self.update_checksums_no_wrapping_sum(data);
        self.csum = wrapping_csum(self.csum, data);
    }
    /// Increase the parser counter and update only the CRC-16 header checksum
    fn update_checksums_no_wrapping_sum(&mut self, data: &[u8]) {
        self.len += data.len();
        self.crc.digest(data);
    }
    /// Read the `limit` bytes into a vector.
    ///
    /// This function does not increase the parsed counter, nor updates any checksums.
    ///
    /// Take care not to allocate too much memory when doing so, until more data
    /// is read from the stream.
    fn read_limit_no_checksums(&mut self, mut limit: usize, buf: &mut Vec<u8>) -> LhaResult<(), R> {
        while limit != 0 {
            let chunk_size = limit.min(ALLOCATE_LIMIT_MAX);
            buf.try_reserve_exact(chunk_size).map_err(|err| LhaError::HeaderParse(err.into()))?;
            // FIXME: use BorrowedBuf once stabilized
            let spare_uninit = &mut buf.spare_capacity_mut()[..chunk_size];
            // SAFETY: assume read_exact is write-only
            let spare = unsafe { spare_uninit.assume_init_mut() };
            self.rd.read_exact(spare).map_err(LhaError::Io)?;
            // SAFETY: assume chunk_size was read into buf
            // this can't overflow because buf.len() + chunk_size <= buf.capacity()
            unsafe { buf.set_len(buf.len() + chunk_size); }
            limit -= chunk_size;
        }
        Ok(())
    }
}

impl LhaHeader {
    /// Attempt to parse the LHA header. Return `Ok(Some(LhaHeader))` on success. Return `Ok(None)`
    /// if the end of archive marker (a `0` byte) was encountered.
    ///
    /// The method validates all length and checksum fields of the header, but does not parse extra
    /// headers except:
    ///
    /// * The ["Common"][EXT_HEADER_COMMON] header for validating the header's CRC-16 checksum.
    /// * The ["MS-DOS Attributes"][EXT_HEADER_MSDOS_ATTRS] header for reading MS-DOS attributes.
    /// * The ["File size"][EXT_HEADER_FILE_SIZES] header for reading 64-bit file size.
    ///
    /// All extra header data is available as raw bytes and raw extra headers can be easily iterated
    /// with the [`LhaHeader::iter_extra`] function.
    ///
    /// [`LhaHeader`] methods can be further called on the returned object to attempt to parse the
    /// additional properties of an archive entry.
    ///
    /// # Errors
    /// Returns an error from the underlying reading operations or because a malformed header was
    /// encountered.
    pub fn read<R: Read>(rd: &mut R) -> LhaResult<Option<LhaHeader>, R> {
        let mut parser = Parser {
            rd, 
            crc: Crc16::default(),
            csum: Wrapping(0),
            len: 0
        };
        let header_len = match parser.read_u8_or_none()? {
            Some(0)|None => return Ok(None),
            Some(len) => len
        };
        let csum = parser.read_u8()?;
        // reset wrapping checksum which should not include the first 2 bytes
        parser.csum = Wrapping(0);

        // read base header
        let mut raw_header = LhaRawBaseHeader::default();
        parser.read_exact(bytes_of_mut(&mut raw_header))?;
        if raw_header.lha_level > 3 {
            return Err(LhaError::HeaderParse(LhaHeaderError::UnknownLevel))
        }

        // read filename if level 0 or 1
        let filename = if raw_header.lha_level < 2 {
            let filename_len = parser.read_u8()? as usize;
            if (header_len as usize) < parser.len + filename_len {
                return Err(LhaError::HeaderParse(LhaHeaderError::SizeMismatch))
            }
            parser.read_limit(filename_len)?
        }
        else {
            Box::new([])
        };

        // file CRC-16
        let file_crc = parser.read_u16()?;

        // OS-TYPE
        let mut os_type = 0;
        if raw_header.lha_level > 0 {
            os_type = parser.read_u8()?;
        }

        // extended area, only 0 and 1 level
        let mut extended_area: Box<[u8]> = Box::new([]);
        if raw_header.lha_level < 2 {
            let mut min_len = parser.len;
            if raw_header.lha_level == 0 {
                min_len -= 2; // no extra headers
            }
            let extended_len = (header_len as usize).checked_sub(min_len)
                              .ok_or(LhaHeaderError::SizeMismatch)?;
            if extended_len != 0 {
                extended_area = parser.read_limit(extended_len)?;
            }
        };

        // extra headers
        let mut long_header_len: u32 = 0; // a long header length found in level >= 2
        let mut first_header_len: u32 = 0;
        // establish the first extra header length and the long header length
        match raw_header.lha_level {
            1 => {
                first_header_len = parser.read_u16()? as u32;
            }
            2 => {
                long_header_len = u16::from_le_bytes([header_len, csum]) as u32;
                first_header_len = parser.read_u16()? as u32;
            }
            3 => {
                long_header_len = parser.read_u32()?;
                first_header_len = parser.read_u32()?;
                if header_len != 4 || csum != 0 {
                    return Err(LhaError::HeaderParse(LhaHeaderError::Level3Signature))
                }
            }
            _ => {}
        }

        // validate level 0 and 1 header checksum
        if raw_header.lha_level < 2 {
            if csum != parser.csum.0 {
                return Err(LhaError::HeaderParse(LhaHeaderError::WrappingSumMismatch))
            }
        }
        else if (long_header_len.saturating_sub(first_header_len) as usize) < parser.len {
            return Err(LhaError::HeaderParse(LhaHeaderError::LongSizeMismatch))
        }

        let mut extra_headers = Vec::new();
        let mut msdos_attrs = MsDosAttrs::from_bits_retain(raw_header.msdos_attrs as u16);
        let mut original_size = u32::from_le_bytes(raw_header.original_size) as u64;
        let mut compressed_size = u32::from_le_bytes(raw_header.compressed_size) as u64;
        let mut header_crc: Option<u16> = None;
        // read extra headers
        let mut extra_header_len = first_header_len as usize;
        while extra_header_len != 0 {
            // check long header length (level 2, 3)
            if long_header_len != 0 {
                if (long_header_len as usize).saturating_sub(extra_header_len) < parser.len - 2 {
                    return Err(LhaError::HeaderParse(LhaHeaderError::LongSizeMismatch))
                }
            }
            else if compressed_size < (extra_headers.len() as u64) + extra_header_len as u64  {
                // otherwise check skip size (level 1)
                return Err(LhaError::HeaderParse(LhaHeaderError::SkipSizeMismatch))
            }
            // append a single header
            parser.read_limit_no_checksums(extra_header_len, &mut extra_headers)?;
            assert!(extra_header_len <= extra_headers.len());
            let start = extra_headers.len() - extra_header_len;
            // slice off the last header with the length of the next header
            let header_with_len = &mut extra_headers[start..];
            {
                // split off the next header's length from the header body
                let (header, next_header_len) = if raw_header.lha_level == 3 {
                    header_with_len.split_last_chunk_mut::<4>().map(|(h, chunk)|
                        (h, u32::from_le_bytes(*chunk) as usize))
                }
                else {
                    header_with_len.split_last_chunk_mut::<2>().map(|(h, chunk)|
                        (h, u16::from_le_bytes(*chunk) as usize))
                }
                // header body must not be empty
                .filter(|(h, _)| !h.is_empty())
                .ok_or(LhaHeaderError::ExtendedHeaderSize)?;
                // check the header's content
                match header {
                    // we need to extract the CRC-16 from header and clear it in order to calculate checksum
                    [EXT_HEADER_COMMON, data @ ..] => {
                        if header_crc.is_some() {
                            return Err(LhaError::HeaderParse(LhaHeaderError::CommonHeader))
                        }
                        if let Some(crc) = data.get_mut(0..2) {
                            header_crc = read_u16(crc);
                            for p in crc.iter_mut() {
                                *p = 0;
                            }
                        }
                    }
                    [EXT_HEADER_MSDOS_ATTRS, data @ ..]|
                    [EXT_HEADER_EXT_ATTRS,   data @ ..] if data.len() >= 2 => {
                        let attrs = read_u16(&data[0..2]).unwrap();
                        msdos_attrs = MsDosAttrs::from_bits_retain(attrs);
                    }
                    [EXT_HEADER_FILE_SIZES, data @ ..] if raw_header.lha_level >= 2 && data.len() >= 16 => {
                        compressed_size = read_u64(&data[0..8]).unwrap();
                        original_size   = read_u64(&data[8..16]).unwrap();
                    }
                    _ => {}
                }
                // next header
                extra_header_len = next_header_len;
            }
            // update parser length and checksum from the last chunk
            parser.update_checksums_no_wrapping_sum(header_with_len);
        }

        // validate long header length
        if long_header_len != 0 &&
           long_header_len as usize != parser.len
        {
            if raw_header.lha_level == 2 && (long_header_len as usize) - 1 == parser.len
            {
                // read padding byte
                parser.read_u8()?;
            }
            else if raw_header.lha_level != 2 || long_header_len as usize != parser.len - 2
            {
                // some packers (Osk) don't include self in the header length
                return Err(LhaError::HeaderParse(LhaHeaderError::LongSizeMismatch))
            }
        }

        // validate headers CRC
        if let Some(crc) = header_crc && crc != parser.crc.sum16() {
            return Err(LhaError::HeaderParse(LhaHeaderError::Crc16Mismatch))
        }

        // adjust compressed size for level 1
        if raw_header.lha_level == 1 {
            compressed_size = compressed_size.checked_sub(extra_headers.len() as u64)
                .ok_or(LhaError::HeaderParse(LhaHeaderError::SkipSizeMismatch))?
        }

        let compression = raw_header.compression;
        let last_modified = u32::from_le_bytes(raw_header.last_modified);
        let extra_headers = extra_headers.into_boxed_slice();

        Ok(Some(LhaHeader {
            level: raw_header.lha_level,
            compression,
            compressed_size,
            original_size,
            filename,
            os_type,
            msdos_attrs,
            last_modified,
            file_crc,
            extended_area,
            first_header_len,
            extra_headers
        }))
    }

    /// Return an iterator that will iterate through extra headers, yielding the headers' raw
    /// data, excluding the next header length field.
    ///
    /// # Note
    /// Each iterated slice will have at least the size of 1 byte containing the header identifier.
    pub fn iter_extra(&self) -> ExtraHeaderIter<'_> {
        ExtraHeaderIter {
            data: &self.extra_headers,
            header_length: self.first_header_len,
            header_len32: self.level == 3
        }
    }
}

#[inline]
pub(super) fn read_u16(slice: &[u8]) -> Option<u16> {
    slice.as_array::<{size_of::<u16>()}>().copied().map(u16::from_le_bytes)
}

#[inline]
pub(super) fn read_u32(slice: &[u8]) -> Option<u32> {
    slice.as_array::<{size_of::<u32>()}>().copied().map(u32::from_le_bytes)
}

#[inline]
pub(super) fn read_u64(slice: &[u8]) -> Option<u64> {
    slice.as_array::<{size_of::<u64>()}>().copied().map(u64::from_le_bytes)
}

fn wrapping_csum(init: Wrapping<u8>, data: &[u8]) -> Wrapping<u8> {
    let sum: Wrapping<u8> = data.iter().copied().map(Wrapping).sum();
    sum + init
}

pub(super) fn split_data_at_nil_or_end(data: &[u8]) -> (&[u8], Option<&[u8]>) {
    match memchr::memchr(0, data) {
        Some(index) => {
            #[cfg(all(not(feature = "no-unsafe-assertions"), not(debug_assertions)))]
            unsafe {
                // SAFETY: memchr guarantee asserted condition
                core::hint::assert_unchecked(index < data.len());
            }
            (&data[0..index], Some(&data[index + 1..]))
        }
        None => (data, None)
    }
}

#[cfg(feature = "std")]
pub(super) fn parse_pathname(data: &[u8], path: &mut PathBuf) {
    path.reserve(data.len());
    // split by all possible path separators
    for part in data.split(|&c| matches!(c, 0xFF|b'/'|b'\\')) {
        match part {
            b"."|b".."|[] => {} // ignore malicious and empty paths
            name => path.push(parse_str_nilterm(name, false, false).as_ref())
        }
    }
}

pub(super) fn parse_pathname_to_str(data: &[u8], path: &mut String) {
    path.reserve(data.len());
    // split by all possible path separators
    for part in data.split(|&c| matches!(c, 0xFF|b'/'|b'\\')) {
        match part {
            b"."|b".."|[] => {} // ignore malicious and empty paths
            name => {
                if !path.is_empty() {
                    path.push('/');
                }
                path.push_str(parse_str_nilterm(name, false, false).as_ref())
            }
        }
    }
}

#[inline(always)]
pub(super) fn is_separator(c: char) -> bool {
    #[cfg(feature = "std")]
    {
        std::path::is_separator(c)
    }
    #[cfg(not(feature = "std"))]
    {
        matches!(c, '/'|'\\')
    }
}

pub(super) fn parse_str_nilterm(
        data: &[u8], nilterm: bool, ignore_sep: bool
    ) -> Cow<'_, str>
{
    if let Some(index) = data.iter().position(|&c|
            !(0x20..0x7f).contains(&c) ||

            (!ignore_sep && is_separator(c as char))
        )
    {
        let mut out = String::with_capacity(data.len()*3);
        let (head, rest) = data.split_at(index);
        // SAFETY: head was validated to contain ASCII-only characters
        out.push_str(unsafe {
            core::str::from_utf8_unchecked(head)
        });
        for byte in rest.iter() {
            match byte {
                0 if nilterm => break,
                0x00..=0x1f|
                0x7f..=0xff => {
                    write!(out, "%{:02x}", byte).unwrap();
                }
                &ch => {
                    let c = ch as char;
                    if !ignore_sep && is_separator(c) {
                        out.push('_');
                    }
                    else {
                        out.push(c);
                    }
                }
            }
        }
        Cow::Owned(out)
    }
    else {
        // SAFETY: data was validated to contain ASCII-only characters
        unsafe {
            Cow::Borrowed(core::str::from_utf8_unchecked(data))
        }
    }
}

#[cfg(feature = "std")]
#[cfg(test)]
mod tests {
    use super::*;
    use std::path::MAIN_SEPARATOR;

    fn parse_filename(data: &[u8]) -> Cow<'_, str> {
        parse_str_nilterm(data, false, false)
    }

   #[test]
    fn split_data_at_nil_or_end_works() {
        assert_eq!((&b"Foo"[..], None), split_data_at_nil_or_end(b"Foo"));
        assert_eq!((&b"Foo"[..], Some(&b"Bar"[..])), split_data_at_nil_or_end(b"Foo\x00Bar"));
        assert_eq!((&[][..], Some(&b"Bar"[..])), split_data_at_nil_or_end(b"\x00Bar"));
    }

   #[test]
    fn path_parser_works() {
        assert_eq!("", parse_filename(b""));
        assert_eq!("Hello World!", parse_filename(b"Hello World!"));
        assert!(std::path::is_separator('/'));
        assert_eq!("_Hello_World_", parse_filename(b"/Hello/World/"));
        #[cfg(target_family = "windows")]
        if std::path::is_separator('\\') {
            assert_eq!("_Hello_World_", parse_filename(br"\Hello\World\"));
        }
        assert_eq!("Hello%00World%7f", parse_filename(b"Hello\x00World\x7f"));
        assert_eq!("Hello%01World%ff", parse_filename(b"Hello\x01World\xff"));
        assert_eq!("Hello", parse_str_nilterm(b"Hello\x00World\xff", true, false));
        assert_eq!("He_llo", parse_str_nilterm(b"He/llo\x00World\xff", true, false));
        assert_eq!("He/llo", parse_str_nilterm(b"He/llo\x00World\xff", true, true));
        assert_eq!("He/llo%00World%ff", parse_str_nilterm(b"He/llo\x00World\xff", false, true));
        assert_eq!("_Hello%1fWorld%80", parse_filename(b"/Hello\x1fWorld\x80"));
        let mut path = PathBuf::new();
        parse_pathname(b"", &mut path);
        assert!(path.is_relative());
        assert_eq!("", path.to_str().unwrap());
        parse_pathname(b"/", &mut path);
        assert!(path.is_relative());
        assert_eq!("", path.to_str().unwrap());
        parse_pathname(br"\", &mut path);
        assert!(path.is_relative());
        assert_eq!("", path.to_str().unwrap());
        parse_pathname(br".", &mut path);
        assert!(path.is_relative());
        assert_eq!("", path.to_str().unwrap());
        parse_pathname(br"..", &mut path);
        assert!(path.is_relative());
        assert_eq!("", path.to_str().unwrap());
        parse_pathname(br"./..", &mut path);
        assert!(path.is_relative());
        assert_eq!("", path.to_str().unwrap());
        parse_pathname(br".\..", &mut path);
        assert!(path.is_relative());
        assert_eq!("", path.to_str().unwrap());
        parse_pathname(br"/..\./", &mut path);
        assert!(path.is_relative());
        assert_eq!("", path.to_str().unwrap());
        parse_pathname(br"\../.\", &mut path);
        assert!(path.is_relative());
        assert_eq!("", path.to_str().unwrap());
        parse_pathname(br"foo/bar\baz", &mut path);
        assert!(path.is_relative());
        let expect = format!("foo{}bar{}baz", MAIN_SEPARATOR, MAIN_SEPARATOR);
        assert_eq!(expect, path.to_str().unwrap());
        path.clear();
        parse_pathname(br"\foo/bar\baz/", &mut path);
        assert!(path.is_relative());
        let expect = format!("foo{}bar{}baz", MAIN_SEPARATOR, MAIN_SEPARATOR);
        assert_eq!(expect, path.to_str().unwrap());
        path.clear();
        parse_pathname(br"/foo\bar/baz\", &mut path);
        assert!(path.is_relative());
        let expect = format!("foo{}bar{}baz", MAIN_SEPARATOR, MAIN_SEPARATOR);
        assert_eq!(expect, path.to_str().unwrap());
        path.clear();
        parse_pathname(b"foo\xffbar\xffbaz", &mut path);
        assert!(path.is_relative());
        let expect = format!("foo{}bar{}baz", MAIN_SEPARATOR, MAIN_SEPARATOR);
        assert_eq!(expect, path.to_str().unwrap());
        path.clear();
        parse_pathname(b"\xfffoo\xffb\x91ar\xffbaz\xff", &mut path);
        assert!(path.is_relative());
        let expect = format!("foo{}b%91ar{}baz", MAIN_SEPARATOR, MAIN_SEPARATOR);
        assert_eq!(expect, path.to_str().unwrap());
        path.clear();
    }

    #[test]
    fn path_parser_to_str_works() {
        let mut path = String::new();
        parse_pathname_to_str(b"", &mut path);
        assert!(!path.starts_with('/'));
        assert_eq!("", &path);
        parse_pathname_to_str(b"/", &mut path);
        assert!(!path.starts_with('/'));
        assert_eq!("", &path);
        parse_pathname_to_str(br"\", &mut path);
        assert!(!path.starts_with('/'));
        assert_eq!("", &path);
        parse_pathname_to_str(br".", &mut path);
        assert!(!path.starts_with('/'));
        assert_eq!("", &path);
        parse_pathname_to_str(br"..", &mut path);
        assert!(!path.starts_with('/'));
        assert_eq!("", &path);
        parse_pathname_to_str(br"./..", &mut path);
        assert!(!path.starts_with('/'));
        assert_eq!("", &path);
        parse_pathname_to_str(br".\..", &mut path);
        assert!(!path.starts_with('/'));
        assert_eq!("", &path);
        parse_pathname_to_str(br"/..\./", &mut path);
        assert!(!path.starts_with('/'));
        assert_eq!("", &path);
        parse_pathname_to_str(br"\../.\", &mut path);
        assert!(!path.starts_with('/'));
        assert_eq!("", &path);
        parse_pathname_to_str(br"foo/bar\baz", &mut path);
        assert!(!path.starts_with('/'));
        let expect = "foo/bar/baz";
        assert_eq!(expect, &path);
        path.clear();
        parse_pathname_to_str(br"\foo/bar\baz/", &mut path);
        assert!(!path.starts_with('/'));
        let expect = "foo/bar/baz";
        assert_eq!(expect, &path);
        path.clear();
        parse_pathname_to_str(br"/foo\bar/baz\", &mut path);
        assert!(!path.starts_with('/'));
        let expect = "foo/bar/baz";
        assert_eq!(expect, &path);
        path.clear();
        parse_pathname_to_str(b"foo\xffbar\xffbaz", &mut path);
        assert!(!path.starts_with('/'));
        let expect = "foo/bar/baz";
        assert_eq!(expect, &path);
        path.clear();
        parse_pathname_to_str(b"\xfffoo\xffb\x91ar\xffbaz\xff", &mut path);
        assert!(!path.starts_with('/'));
        let expect = "foo/b%91ar/baz";
        assert_eq!(expect, &path);
        path.clear();
    }

    #[test]
    fn header_parse_errors() {
        let mut data: &[u8];
        data = &[]; assert!(LhaHeader::read(&mut data).unwrap().is_none());
        data = &[0]; assert!(LhaHeader::read(&mut data).unwrap().is_none());
        data = &[0, 0xff]; assert!(LhaHeader::read(&mut data).unwrap().is_none());
        let test_error = |mut data: &[u8], expect| {
            let err = LhaHeader::read(&mut data).unwrap_err();
            assert!(matches!(err, LhaError::HeaderParse(ref e) if e == &expect),
                        "{:?} != {:?}", err, expect);
            assert!(err.to_string().starts_with("while parsing LHA header: "));
        };
        let test_error_eof = |mut data: &[u8]| {
            let err = LhaHeader::read(&mut data).unwrap_err();
            assert!(matches!(err, LhaError::Io(ref e) if e.kind() == std::io::ErrorKind::UnexpectedEof),
                    "{:?} != EOF", err);
        };
        test_error(b"\x01\0-lh0-\0\0\0\0\0\0\0\0\0\0\0\0\x20\x04", LhaHeaderError::UnknownLevel);
        test_error(b"\x08\0-lh0-\0\0\0\0\0\0\0\0\0\0\0\0\x20\x03\0\0M\0\0\0\0\0\0\0\0",
                                            LhaHeaderError::Level3Signature);
        test_error(b"\xff\0-lh0-\0\0\0\0\0\0\0\0\0\0\0\0\x20\x02\0\0M\x01\0\0",
                                            LhaHeaderError::ExtendedHeaderSize);
        test_error(b"\xff\0-lh0-\0\0\0\0\0\0\0\0\0\0\0\0\x20\x02\0\0M\x02\0\0\0",
                                            LhaHeaderError::ExtendedHeaderSize);
        test_error(b"\x04\0-lh0-\0\0\0\0\0\0\0\0\0\0\0\0\x20\x03\0\0M\xff\0\0\0\x01\0\0\0\0",
                                            LhaHeaderError::ExtendedHeaderSize);
        test_error(b"\x04\0-lh0-\0\0\0\0\0\0\0\0\0\0\0\0\x20\x03\0\0M\xff\0\0\0\x02\0\0\0\0\0",
                                            LhaHeaderError::ExtendedHeaderSize);
        test_error(b"\x04\0-lh0-\0\0\0\0\0\0\0\0\0\0\0\0\x20\x03\0\0M\xff\0\0\0\x03\0\0\0\0\0\0",
                                            LhaHeaderError::ExtendedHeaderSize);
        test_error(b"\x04\0-lh0-\0\0\0\0\0\0\0\0\0\0\0\0\x20\x03\0\0M\xff\0\0\0\x04\0\0\0\0\0\0\0",
                                            LhaHeaderError::ExtendedHeaderSize);
        test_error(b"\x15\0-lh0-\0\0\0\0\0\0\0\0\0\0\0\0\x20\x00\0\0\0",
                                            LhaHeaderError::SizeMismatch);
        test_error(b"\x18\0-lh0-\0\0\0\0\0\0\0\0\0\0\0\0\x20\x01\0\0\0M\0\0",
                                            LhaHeaderError::SizeMismatch);
        test_error(b"\x19\0-lh0-\0\0\0\0\0\0\0\0\0\0\0\0\x20\x02\0\0M\0\0",
                                            LhaHeaderError::LongSizeMismatch);
        test_error(b"\x1C\0-lh0-\0\0\0\0\0\0\0\0\0\0\0\0\x20\x02\0\0M\0\0",
                                            LhaHeaderError::LongSizeMismatch);
        test_error(b"\x22\0-lh0-\0\0\0\0\0\0\0\0\0\0\0\0\x20\x02\0\0M\x03\0\0\x03\0\0\0\0",
                                            LhaHeaderError::LongSizeMismatch);
        test_error_eof(b"\x21\0-lh0-\0\0\0\0\0\0\0\0\0\0\0\0\x20\x02\0\0M\x03\0\0\x03\0\0\0\0");
        data = b"\x20\0-lh0-\0\0\0\0\0\0\0\0\0\0\0\0\x20\x02\0\0M\x03\0\0\x03\0\0\0\0";
        LhaHeader::read(&mut data).unwrap();
        test_error(b"\x1F\0-lh0-\0\0\0\0\0\0\0\0\0\0\0\0\x20\x02\0\0M\x03\0\0\x03\0\0\0\0",
                                            LhaHeaderError::LongSizeMismatch);
        data = b"\x1E\0-lh0-\0\0\0\0\0\0\0\0\0\0\0\0\x20\x02\0\0M\x03\0\0\x03\0\0\0\0";
        LhaHeader::read(&mut data).unwrap();
        test_error(b"\x1D\0-lh0-\0\0\0\0\0\0\0\0\0\0\0\0\x20\x02\0\0M\x03\0\0\x03\0\0\0\0",
                                            LhaHeaderError::LongSizeMismatch);
        test_error(b"\x16\0-lh0-\0\0\0\0\0\0\0\0\0\0\0\0\x20\x00\0\0\0",
                                            LhaHeaderError::WrappingSumMismatch);
        test_error(b"\x19\xCF-lh0-\0\0\0\0\0\0\0\0\0\0\0\0\x20\x01\0\0\0M\x03\0",
                                            LhaHeaderError::SkipSizeMismatch);
        test_error(b"\x24\0-lh0-\0\0\0\0\0\0\0\0\0\0\0\0\x20\x02\0\0M\x05\0\0\0\0\x05\0\0\0\0\0\0",
                                            LhaHeaderError::CommonHeader);
        test_error(b"\x1F\0-lh0-\0\0\0\0\0\0\0\0\0\0\0\0\x20\x02\0\0M\x05\0\0\0\0\0\0",
                                            LhaHeaderError::Crc16Mismatch);
    }
}