Debian Codegraph @

src/spec/header.rs

raw symbols SCIP
1 // Copyright (c) 2021 Harry [Majored] [[email protected]]
2 // MIT License (https://github.com/Majored/rs-async-zip/blob/main/LICENSE)
3
4 // https://github.com/Majored/rs-async-zip/blob/main/SPECIFICATION.md#437
5 pub struct LocalFileHeader {
6 pub version: u16,
7 pub flags: GeneralPurposeFlag,
8 pub compression: u16,
9 pub mod_time: u16,
10 pub mod_date: u16,
11 pub crc: u32,
12 pub compressed_size: u32,
13 pub uncompressed_size: u32,
14 pub file_name_length: u16,
15 pub extra_field_length: u16,
16 }
17
18 // https://github.com/Majored/rs-async-zip/blob/main/SPECIFICATION.md#444
19 #[derive(Copy, Clone)]
20 pub struct GeneralPurposeFlag {
21 pub encrypted: bool,
22 pub data_descriptor: bool,
23 pub filename_unicode: bool,
24 }
25
26 /// 2 byte header ids
27 /// Ref https://github.com/Majored/rs-async-zip/blob/main/SPECIFICATION.md#452
28 #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
29 pub struct HeaderId(pub u16);
30
31 impl HeaderId {
32 pub const ZIP64_EXTENDED_INFORMATION_EXTRA_FIELD: HeaderId = HeaderId(0x0001);
33 pub const INFO_ZIP_UNICODE_COMMENT_EXTRA_FIELD: HeaderId = HeaderId(0x6375);
34 pub const INFO_ZIP_UNICODE_PATH_EXTRA_FIELD: HeaderId = HeaderId(0x7075);
35 }
36
37 impl From<u16> for HeaderId {
38 fn from(value: u16) -> Self {
39 HeaderId(value)
40 }
41 }
42
43 impl From<HeaderId> for u16 {
44 fn from(value: HeaderId) -> Self {
45 value.0
46 }
47 }
48
49 /// Represents each extra field.
50 /// Not strictly part of the spec, but is the most useful way to represent the data.
51 #[derive(Clone, Debug)]
52 #[non_exhaustive]
53 pub enum ExtraField {
54 Zip64ExtendedInformation(Zip64ExtendedInformationExtraField),
55 InfoZipUnicodeComment(InfoZipUnicodeCommentExtraField),
56 InfoZipUnicodePath(InfoZipUnicodePathExtraField),
57 Unknown(UnknownExtraField),
58 }
59
60 /// An extended information header for Zip64.
61 /// This field is used both for local file headers and central directory records.
62 /// https://github.com/Majored/rs-async-zip/blob/main/SPECIFICATION.md#453
63 #[derive(Clone, Debug)]
64 pub struct Zip64ExtendedInformationExtraField {
65 pub header_id: HeaderId,
66 pub uncompressed_size: Option<u64>,
67 pub compressed_size: Option<u64>,
68 // While not specified in the spec, these two fields are often left out in practice.
69 pub relative_header_offset: Option<u64>,
70 pub disk_start_number: Option<u32>,
71 }
72
73 impl Zip64ExtendedInformationExtraField {
74 pub(crate) fn content_size(&self) -> usize {
75 self.uncompressed_size.map(|_| 8).unwrap_or_default()
76 + self.compressed_size.map(|_| 8).unwrap_or_default()
77 + self.relative_header_offset.map(|_| 8).unwrap_or_default()
78 + self.disk_start_number.map(|_| 8).unwrap_or_default()
79 }
80 }
81
82 /// Stores the UTF-8 version of the file comment as stored in the central directory header.
83 /// https://github.com/Majored/rs-async-zip/blob/main/SPECIFICATION.md#468
84 #[derive(Clone, Debug)]
85 pub enum InfoZipUnicodeCommentExtraField {
86 V1 { crc32: u32, unicode: Vec<u8> },
87 Unknown { version: u8, data: Vec<u8> },
88 }
89
90 /// Stores the UTF-8 version of the file name field as stored in the local header and central directory header.
91 /// https://github.com/Majored/rs-async-zip/blob/main/SPECIFICATION.md#469
92 #[derive(Clone, Debug)]
93 pub enum InfoZipUnicodePathExtraField {
94 V1 { crc32: u32, unicode: Vec<u8> },
95 Unknown { version: u8, data: Vec<u8> },
96 }
97
98 /// Represents any unparsed extra field.
99 #[derive(Clone, Debug)]
100 pub struct UnknownExtraField {
101 pub header_id: HeaderId,
102 pub data_size: u16,
103 pub content: Vec<u8>,
104 }
105
106 // https://github.com/Majored/rs-async-zip/blob/main/SPECIFICATION.md#4312
107 pub struct CentralDirectoryRecord {
108 pub v_made_by: u16,
109 pub v_needed: u16,
110 pub flags: GeneralPurposeFlag,
111 pub compression: u16,
112 pub mod_time: u16,
113 pub mod_date: u16,
114 pub crc: u32,
115 pub compressed_size: u32,
116 pub uncompressed_size: u32,
117 pub file_name_length: u16,
118 pub extra_field_length: u16,
119 pub file_comment_length: u16,
120 pub disk_start: u16,
121 pub inter_attr: u16,
122 pub exter_attr: u32,
123 pub lh_offset: u32,
124 }
125
126 // https://github.com/Majored/rs-async-zip/blob/main/SPECIFICATION.md#4316
127 #[derive(Debug)]
128 pub struct EndOfCentralDirectoryHeader {
129 pub(crate) disk_num: u16,
130 pub(crate) start_cent_dir_disk: u16,
131 pub(crate) num_of_entries_disk: u16,
132 pub(crate) num_of_entries: u16,
133 pub(crate) size_cent_dir: u32,
134 pub(crate) cent_dir_offset: u32,
135 pub(crate) file_comm_length: u16,
136 }
137
138 // https://github.com/Majored/rs-async-zip/blob/main/SPECIFICATION.md#4314
139 #[derive(Debug, PartialEq)]
140 pub struct Zip64EndOfCentralDirectoryRecord {
141 /// The size of this Zip64EndOfCentralDirectoryRecord.
142 /// This is specified because there is a variable-length extra zip64 information sector.
143 /// However, we will gleefully ignore this sector because it is reserved for use by PKWare.
144 pub size_of_zip64_end_of_cd_record: u64,
145 pub version_made_by: u16,
146 pub version_needed_to_extract: u16,
147 pub disk_number: u32,
148 pub disk_number_start_of_cd: u32,
149 pub num_entries_in_directory_on_disk: u64,
150 pub num_entries_in_directory: u64,
151 pub directory_size: u64,
152 pub offset_of_start_of_directory: u64,
153 }
154
155 // https://github.com/Majored/rs-async-zip/blob/main/SPECIFICATION.md#4315
156 #[derive(Debug, PartialEq)]
157 pub struct Zip64EndOfCentralDirectoryLocator {
158 pub number_of_disk_with_start_of_zip64_end_of_central_directory: u32,
159 pub relative_offset: u64,
160 pub total_number_of_disks: u32,
161 }