Debian Codegraph @

src/spec/extra_field.rs

raw symbols SCIP
1 // Copyright Cognite AS, 2023
2
3 use crate::error::{Result as ZipResult, ZipError};
4 use crate::spec::header::{
5 ExtraField, HeaderId, InfoZipUnicodeCommentExtraField, InfoZipUnicodePathExtraField, UnknownExtraField,
6 Zip64ExtendedInformationExtraField,
7 };
8
9 use super::consts::NON_ZIP64_MAX_SIZE;
10
11 pub(crate) trait ExtraFieldAsBytes {
12 fn as_bytes(&self) -> Vec<u8>;
13
14 fn count_bytes(&self) -> usize;
15 }
16
17 impl ExtraFieldAsBytes for &[ExtraField] {
18 fn as_bytes(&self) -> Vec<u8> {
19 let mut buffer = Vec::new();
20 for field in self.iter() {
21 buffer.append(&mut field.as_bytes());
22 }
23 buffer
24 }
25
26 fn count_bytes(&self) -> usize {
27 self.iter().map(|field| field.count_bytes()).sum()
28 }
29 }
30
31 impl ExtraFieldAsBytes for ExtraField {
32 fn as_bytes(&self) -> Vec<u8> {
33 match self {
34 ExtraField::Zip64ExtendedInformation(field) => field.as_bytes(),
35 ExtraField::InfoZipUnicodeComment(field) => field.as_bytes(),
36 ExtraField::InfoZipUnicodePath(field) => field.as_bytes(),
37 ExtraField::Unknown(field) => field.as_bytes(),
38 }
39 }
40
41 fn count_bytes(&self) -> usize {
42 match self {
43 ExtraField::Zip64ExtendedInformation(field) => field.count_bytes(),
44 ExtraField::InfoZipUnicodeComment(field) => field.count_bytes(),
45 ExtraField::InfoZipUnicodePath(field) => field.count_bytes(),
46 ExtraField::Unknown(field) => field.count_bytes(),
47 }
48 }
49 }
50
51 impl ExtraFieldAsBytes for UnknownExtraField {
52 fn as_bytes(&self) -> Vec<u8> {
53 let mut bytes = Vec::new();
54 let header_id: u16 = self.header_id.into();
55 bytes.append(&mut header_id.to_le_bytes().to_vec());
56 bytes.append(&mut self.data_size.to_le_bytes().to_vec());
57 bytes.append(&mut self.content.clone());
58
59 bytes
60 }
61
62 fn count_bytes(&self) -> usize {
63 4 + self.content.len()
64 }
65 }
66
67 impl ExtraFieldAsBytes for Zip64ExtendedInformationExtraField {
68 fn as_bytes(&self) -> Vec<u8> {
69 let mut bytes = Vec::new();
70 let header_id: u16 = self.header_id.into();
71 bytes.append(&mut header_id.to_le_bytes().to_vec());
72 bytes.append(&mut (self.content_size() as u16).to_le_bytes().to_vec());
73 if let Some(uncompressed_size) = &self.uncompressed_size {
74 bytes.append(&mut uncompressed_size.to_le_bytes().to_vec());
75 }
76 if let Some(compressed_size) = &self.compressed_size {
77 bytes.append(&mut compressed_size.to_le_bytes().to_vec());
78 }
79 if let Some(relative_header_offset) = &self.relative_header_offset {
80 bytes.append(&mut relative_header_offset.to_le_bytes().to_vec());
81 }
82 if let Some(disk_start_number) = &self.disk_start_number {
83 bytes.append(&mut disk_start_number.to_le_bytes().to_vec());
84 }
85
86 bytes
87 }
88
89 fn count_bytes(&self) -> usize {
90 4 + self.content_size()
91 }
92 }
93
94 impl ExtraFieldAsBytes for InfoZipUnicodeCommentExtraField {
95 fn as_bytes(&self) -> Vec<u8> {
96 let mut bytes = Vec::new();
97 let header_id: u16 = HeaderId::INFO_ZIP_UNICODE_COMMENT_EXTRA_FIELD.into();
98 bytes.append(&mut header_id.to_le_bytes().to_vec());
99 match self {
100 InfoZipUnicodeCommentExtraField::V1 { crc32, unicode } => {
101 let data_size: u16 = (5 + unicode.len()).try_into().unwrap();
102 bytes.append(&mut data_size.to_le_bytes().to_vec());
103 bytes.push(1);
104 bytes.append(&mut crc32.to_le_bytes().to_vec());
105 bytes.append(&mut unicode.clone());
106 }
107 InfoZipUnicodeCommentExtraField::Unknown { version, data } => {
108 let data_size: u16 = (1 + data.len()).try_into().unwrap();
109 bytes.append(&mut data_size.to_le_bytes().to_vec());
110 bytes.push(*version);
111 bytes.append(&mut data.clone());
112 }
113 }
114 bytes
115 }
116
117 fn count_bytes(&self) -> usize {
118 match self {
119 InfoZipUnicodeCommentExtraField::V1 { unicode, .. } => 9 + unicode.len(),
120 InfoZipUnicodeCommentExtraField::Unknown { data, .. } => 5 + data.len(),
121 }
122 }
123 }
124
125 impl ExtraFieldAsBytes for InfoZipUnicodePathExtraField {
126 fn as_bytes(&self) -> Vec<u8> {
127 let mut bytes = Vec::new();
128 let header_id: u16 = HeaderId::INFO_ZIP_UNICODE_PATH_EXTRA_FIELD.into();
129 bytes.append(&mut header_id.to_le_bytes().to_vec());
130 match self {
131 InfoZipUnicodePathExtraField::V1 { crc32, unicode } => {
132 let data_size: u16 = (5 + unicode.len()).try_into().unwrap();
133 bytes.append(&mut data_size.to_le_bytes().to_vec());
134 bytes.push(1);
135 bytes.append(&mut crc32.to_le_bytes().to_vec());
136 bytes.append(&mut unicode.clone());
137 }
138 InfoZipUnicodePathExtraField::Unknown { version, data } => {
139 let data_size: u16 = (1 + data.len()).try_into().unwrap();
140 bytes.append(&mut data_size.to_le_bytes().to_vec());
141 bytes.push(*version);
142 bytes.append(&mut data.clone());
143 }
144 }
145 bytes
146 }
147
148 fn count_bytes(&self) -> usize {
149 match self {
150 InfoZipUnicodePathExtraField::V1 { unicode, .. } => 9 + unicode.len(),
151 InfoZipUnicodePathExtraField::Unknown { data, .. } => 5 + data.len(),
152 }
153 }
154 }
155
156 /// Parse a zip64 extra field from bytes.
157 /// The content of "data" should exclude the header.
158 fn zip64_extended_information_field_from_bytes(
159 header_id: HeaderId,
160 data: &[u8],
161 uncompressed_size: u32,
162 compressed_size: u32,
163 ) -> ZipResult<Zip64ExtendedInformationExtraField> {
164 // slice.take is nightly-only so we'll just use an index to track the current position
165 let mut current_idx = 0;
166 let uncompressed_size = if uncompressed_size == NON_ZIP64_MAX_SIZE && data.len() >= current_idx + 8 {
167 let val = Some(u64::from_le_bytes(data[current_idx..current_idx + 8].try_into().unwrap()));
168 current_idx += 8;
169 val
170 } else {
171 None
172 };
173
174 let compressed_size = if compressed_size == NON_ZIP64_MAX_SIZE && data.len() >= current_idx + 8 {
175 let val = Some(u64::from_le_bytes(data[current_idx..current_idx + 8].try_into().unwrap()));
176 current_idx += 8;
177 val
178 } else {
179 None
180 };
181
182 let relative_header_offset = if data.len() >= current_idx + 8 {
183 let val = Some(u64::from_le_bytes(data[current_idx..current_idx + 8].try_into().unwrap()));
184 current_idx += 8;
185 val
186 } else {
187 None
188 };
189
190 #[allow(unused_assignments)]
191 let disk_start_number = if data.len() >= current_idx + 4 {
192 let val = Some(u32::from_le_bytes(data[current_idx..current_idx + 4].try_into().unwrap()));
193 current_idx += 4;
194 val
195 } else {
196 None
197 };
198
199 Ok(Zip64ExtendedInformationExtraField {
200 header_id,
201 uncompressed_size,
202 compressed_size,
203 relative_header_offset,
204 disk_start_number,
205 })
206 }
207
208 fn info_zip_unicode_comment_extra_field_from_bytes(
209 _header_id: HeaderId,
210 data_size: u16,
211 data: &[u8],
212 ) -> ZipResult<InfoZipUnicodeCommentExtraField> {
213 if data.is_empty() {
214 return Err(ZipError::InfoZipUnicodeCommentFieldIncomplete);
215 }
216 let version = data[0];
217 match version {
218 1 => {
219 if data.len() < 5 {
220 return Err(ZipError::InfoZipUnicodeCommentFieldIncomplete);
221 }
222 let crc32 = u32::from_le_bytes(data[1..5].try_into().unwrap());
223 let unicode = data[5..(data_size as usize)].to_vec();
224 Ok(InfoZipUnicodeCommentExtraField::V1 { crc32, unicode })
225 }
226 _ => Ok(InfoZipUnicodeCommentExtraField::Unknown { version, data: data[1..(data_size as usize)].to_vec() }),
227 }
228 }
229
230 fn info_zip_unicode_path_extra_field_from_bytes(
231 _header_id: HeaderId,
232 data_size: u16,
233 data: &[u8],
234 ) -> ZipResult<InfoZipUnicodePathExtraField> {
235 if data.is_empty() {
236 return Err(ZipError::InfoZipUnicodePathFieldIncomplete);
237 }
238 let version = data[0];
239 match version {
240 1 => {
241 if data.len() < 5 {
242 return Err(ZipError::InfoZipUnicodePathFieldIncomplete);
243 }
244 let crc32 = u32::from_le_bytes(data[1..5].try_into().unwrap());
245 let unicode = data[5..(data_size as usize)].to_vec();
246 Ok(InfoZipUnicodePathExtraField::V1 { crc32, unicode })
247 }
248 _ => Ok(InfoZipUnicodePathExtraField::Unknown { version, data: data[1..(data_size as usize)].to_vec() }),
249 }
250 }
251
252 pub(crate) fn extra_field_from_bytes(
253 header_id: HeaderId,
254 data_size: u16,
255 data: &[u8],
256 uncompressed_size: u32,
257 compressed_size: u32,
258 ) -> ZipResult<ExtraField> {
259 match header_id {
260 HeaderId::ZIP64_EXTENDED_INFORMATION_EXTRA_FIELD => Ok(ExtraField::Zip64ExtendedInformation(
261 zip64_extended_information_field_from_bytes(header_id, data, uncompressed_size, compressed_size)?,
262 )),
263 HeaderId::INFO_ZIP_UNICODE_COMMENT_EXTRA_FIELD => Ok(ExtraField::InfoZipUnicodeComment(
264 info_zip_unicode_comment_extra_field_from_bytes(header_id, data_size, data)?,
265 )),
266 HeaderId::INFO_ZIP_UNICODE_PATH_EXTRA_FIELD => Ok(ExtraField::InfoZipUnicodePath(
267 info_zip_unicode_path_extra_field_from_bytes(header_id, data_size, data)?,
268 )),
269 _ => Ok(ExtraField::Unknown(UnknownExtraField { header_id, data_size, content: data.to_vec() })),
270 }
271 }
272
273 pub struct Zip64ExtendedInformationExtraFieldBuilder {
274 field: Zip64ExtendedInformationExtraField,
275 }
276
277 impl Zip64ExtendedInformationExtraFieldBuilder {
278 pub fn new() -> Self {
279 Self {
280 field: Zip64ExtendedInformationExtraField {
281 header_id: HeaderId::ZIP64_EXTENDED_INFORMATION_EXTRA_FIELD,
282 uncompressed_size: None,
283 compressed_size: None,
284 relative_header_offset: None,
285 disk_start_number: None,
286 },
287 }
288 }
289
290 pub fn sizes(mut self, compressed_size: u64, uncompressed_size: u64) -> Self {
291 self.field.compressed_size = Some(compressed_size);
292 self.field.uncompressed_size = Some(uncompressed_size);
293 self
294 }
295
296 pub fn relative_header_offset(mut self, relative_header_offset: u64) -> Self {
297 self.field.relative_header_offset = Some(relative_header_offset);
298 self
299 }
300
301 #[allow(dead_code)]
302 pub fn disk_start_number(mut self, disk_start_number: u32) -> Self {
303 self.field.disk_start_number = Some(disk_start_number);
304 self
305 }
306
307 pub fn eof_only(&self) -> bool {
308 (self.field.uncompressed_size.is_none() && self.field.compressed_size.is_none())
309 && (self.field.relative_header_offset.is_some() || self.field.disk_start_number.is_some())
310 }
311
312 pub fn build(self) -> ZipResult<Zip64ExtendedInformationExtraField> {
313 let field = self.field;
314
315 if field.content_size() == 0 {
316 return Err(ZipError::Zip64ExtendedFieldIncomplete);
317 }
318 Ok(field)
319 }
320 }