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
use std::borrow::Cow;

use ownable::{IntoOwned, ToOwned};
use tracing::trace;
use winnow::{
    binary::{le_u16, le_u32},
    prelude::PResult,
    token::{literal, take},
    Parser, Partial,
};

use crate::{
    encoding::detect_utf8,
    encoding::Encoding,
    error::{Error, FormatError},
    parse::{
        zero_datetime, Entry, ExtraField, ExtraFieldSettings, HostSystem, Mode, MsdosMode,
        MsdosTimestamp, UnixMode, Version,
    },
};

use super::Method;

/// 4.3.12 Central directory structure: File header
#[derive(IntoOwned, ToOwned)]
pub struct CentralDirectoryFileHeader<'a> {
    /// version made by
    pub creator_version: Version,

    /// version needed to extract
    pub reader_version: Version,

    /// general purpose bit flag
    pub flags: u16,

    /// compression method
    pub method: Method,

    /// last mod file datetime
    pub modified: MsdosTimestamp,

    /// crc32 hash
    pub crc32: u32,

    /// compressed size
    pub compressed_size: u32,

    /// uncompressed size
    pub uncompressed_size: u32,

    /// disk number start
    pub disk_nbr_start: u16,

    /// internal file attributes
    pub internal_attrs: u16,

    /// external file attributes
    pub external_attrs: u32,

    /// relative offset of local header
    pub header_offset: u32,

    /// name field
    pub name: Cow<'a, [u8]>,

    /// extra field
    pub extra: Cow<'a, [u8]>,

    /// comment field
    pub comment: Cow<'a, [u8]>,
}

impl<'a> CentralDirectoryFileHeader<'a> {
    const SIGNATURE: &'static str = "PK\x01\x02";

    /// Parser for the central directory file header
    pub fn parser(i: &mut Partial<&'a [u8]>) -> PResult<Self> {
        _ = literal(Self::SIGNATURE).parse_next(i)?;
        let creator_version = Version::parser.parse_next(i)?;
        let reader_version = Version::parser.parse_next(i)?;
        let flags = le_u16.parse_next(i)?;
        let method = Method::parser.parse_next(i)?;
        let modified = MsdosTimestamp::parser.parse_next(i)?;
        let crc32 = le_u32.parse_next(i)?;
        let compressed_size = le_u32.parse_next(i)?;
        let uncompressed_size = le_u32.parse_next(i)?;
        let name_len = le_u16.parse_next(i)?;
        let extra_len = le_u16.parse_next(i)?;
        let comment_len = le_u16.parse_next(i)?;
        let disk_nbr_start = le_u16.parse_next(i)?;
        let internal_attrs = le_u16.parse_next(i)?;
        let external_attrs = le_u32.parse_next(i)?;
        let header_offset = le_u32.parse_next(i)?;

        let name = take(name_len).parse_next(i)?;
        let extra = take(extra_len).parse_next(i)?;
        let comment = take(comment_len).parse_next(i)?;

        Ok(Self {
            creator_version,
            reader_version,
            flags,
            method,
            modified,
            crc32,
            compressed_size,
            uncompressed_size,
            disk_nbr_start,
            internal_attrs,
            external_attrs,
            header_offset,
            name: Cow::Borrowed(name),
            extra: Cow::Borrowed(extra),
            comment: Cow::Borrowed(comment),
        })
    }
}

impl CentralDirectoryFileHeader<'_> {
    /// Returns true if the name or comment is not valid UTF-8
    pub fn is_non_utf8(&self) -> bool {
        let (valid1, require1) = detect_utf8(&self.name[..]);
        let (valid2, require2) = detect_utf8(&self.comment[..]);
        if !valid1 || !valid2 {
            // definitely not utf-8
            return true;
        }

        if !require1 && !require2 {
            // name and comment only use single-byte runes that overlap with UTF-8
            return false;
        }

        // Might be UTF-8, might be some other encoding; preserve existing flag.
        // Some ZIP writers use UTF-8 encoding without setting the UTF-8 flag.
        // Since it is impossible to always distinguish valid UTF-8 from some
        // other encoding (e.g., GBK or Shift-JIS), we trust the flag.
        self.flags & 0x800 == 0
    }

    /// Converts the directory header into a entry: this involves
    /// parsing the extra fields and converting the timestamps.
    pub fn as_entry(&self, encoding: Encoding, global_offset: u64) -> Result<Entry, Error> {
        let mut entry = Entry {
            name: encoding.decode(&self.name[..])?,
            method: self.method,
            comment: encoding.decode(&self.comment[..])?,
            modified: self.modified.to_datetime().unwrap_or_else(zero_datetime),
            created: None,
            accessed: None,
            header_offset: (self.header_offset as u64)
                .checked_add(global_offset)
                .ok_or(FormatError::InvalidHeaderOffset)?,
            reader_version: self.reader_version,
            flags: self.flags,
            uid: None,
            gid: None,
            crc32: self.crc32,
            compressed_size: self.compressed_size as _,
            uncompressed_size: self.uncompressed_size as _,
            mode: Mode(0),
        };

        entry.mode = match self.creator_version.host_system {
            HostSystem::Unix | HostSystem::Osx => UnixMode(self.external_attrs >> 16).into(),
            HostSystem::WindowsNtfs | HostSystem::Vfat | HostSystem::MsDos => {
                MsdosMode(self.external_attrs).into()
            }
            _ => Mode(0),
        };
        if entry.name.ends_with('/') {
            // believe it or not, this is straight from the APPNOTE
            entry.mode |= Mode::DIR
        };

        let settings = ExtraFieldSettings {
            uncompressed_size_u32: self.uncompressed_size,
            compressed_size_u32: self.compressed_size,
            header_offset_u32: self.header_offset,
        };

        let mut slice = Partial::new(&self.extra[..]);
        while !slice.is_empty() {
            match ExtraField::mk_parser(settings).parse_next(&mut slice) {
                Ok(ef) => {
                    entry.set_extra_field(&ef);
                }
                Err(e) => {
                    trace!("extra field error: {:#?}", e);
                    return Err(FormatError::InvalidExtraField.into());
                }
            }
        }

        Ok(entry)
    }
}