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
use byteorder::{ReadBytesExt, WriteBytesExt};
use filename_decoder::{ASCIIDecoder, IDecoder};
use hfs_nfd::compose_from_hfs_nfd;
use zip_structs::{
zip_central_directory::ZipCDEntry, zip_eocd::ZipEOCD, zip_error::ZipReadError,
zip_local_file_header,
};
pub mod filename_decoder;
static ASCII_DECODER: ASCIIDecoder = ASCIIDecoder {};
#[derive(Clone, Debug)]
pub struct FileNameEntry {
pub name: String,
pub encoding_type: FileNameEncodingType,
}
#[derive(Debug, Clone)]
pub enum FileNameEncodingType {
ExplicitRegularUTF8,
ExplicitIrregularUTF8,
ImplicitASCII,
ImplicitNonASCII,
}
impl FileNameEncodingType {
pub fn is_universal(&self) -> bool {
use FileNameEncodingType::*;
match self {
ExplicitRegularUTF8 | ImplicitASCII => true,
_ => false,
}
}
}
#[derive(Clone, Debug)]
pub struct FileNamesDiagnosis {
pub has_implicit_non_ascii_names: bool,
pub has_non_nfc_explicit_utf8_names: bool,
}
impl FileNamesDiagnosis {
pub fn get_status_primary_message(&self) -> &'static str {
match (self.has_implicit_non_ascii_names, self.has_non_nfc_explicit_utf8_names) {
(false, false) => "All file names are encoded in ASCII or explicitly in UTF-8.",
(true, false) => "Some files are encoded implicitly in a multibyte encoding.",
(false, true) => "Some file names use irregular unicode normalization.",
(true, true) => "Some files use irregular unicode normalization and others are encoded implicitly in a multibyte encoding.",
}
}
pub fn get_status_note(&self) -> &'static str {
match (self.has_implicit_non_ascii_names, self.has_non_nfc_explicit_utf8_names) {
(false, false) => "Almost all devices can decode its file names correctly.",
(true, _) => "Apply this tool, or the receiver may not be able to see the correct file names.",
(false, true) => "Apply this tool, or the receiver may not deal with the pericular file name normalization.",
}
}
pub fn is_universal_archive(&self) -> bool {
return !self.has_implicit_non_ascii_names && !self.has_non_nfc_explicit_utf8_names;
}
}
pub struct InputZIPArchive<F: ReadBytesExt + std::io::Seek> {
file_handler: F,
eocd: ZipEOCD,
cd_entries: Vec<ZipCDEntry>,
}
impl<F> InputZIPArchive<F>
where
F: ReadBytesExt + std::io::Seek,
{
pub fn new(mut handler: F) -> anyhow::Result<Self> {
let eocd = ZipEOCD::from_reader(&mut handler)?;
let cd_entries = ZipCDEntry::all_from_eocd(&mut handler, &eocd)?;
return Ok(Self {
file_handler: handler,
eocd,
cd_entries,
});
}
pub fn diagnose_file_name_encoding(&self) -> FileNamesDiagnosis {
FileNamesDiagnosis {
has_implicit_non_ascii_names: self
.cd_entries
.iter()
.filter(|cd| !cd.is_encoded_in_utf8())
.any(|cd| !ASCII_DECODER.can_decode(&cd.file_name_raw)),
has_non_nfc_explicit_utf8_names: self
.cd_entries
.iter()
.filter(|cd| cd.is_encoded_in_utf8())
.any(|cd| {
let original_name = String::from_utf8_lossy(&cd.file_name_raw);
let nfc_name = compose_from_hfs_nfd(&original_name);
&original_name != &nfc_name
}),
}
}
pub fn get_filename_decoder_index(&self, decoders_list: &[&dyn IDecoder]) -> Option<usize> {
return filename_decoder::decide_decoder(
decoders_list,
&(&self
.cd_entries
.iter()
.flat_map(|cd| vec![&cd.file_name_raw, &cd.file_comment])
.collect::<Vec<&Vec<u8>>>()),
);
}
pub fn get_file_names_list(&self, legacy_decoder: &dyn IDecoder) -> Vec<FileNameEntry> {
use FileNameEncodingType::*;
self.cd_entries
.iter()
.map(|cd| {
if cd.is_encoded_in_utf8() {
let original_file_name = String::from_utf8_lossy(&*(cd.file_name_raw));
let nfc_file_name = compose_from_hfs_nfd(&original_file_name);
return FileNameEntry {
encoding_type: if &original_file_name == &nfc_file_name {
ExplicitRegularUTF8
} else {
ExplicitIrregularUTF8
},
name: nfc_file_name,
};
}
if let Some(ascii_file_name) = ASCII_DECODER.to_string_lossless(&cd.file_name_raw) {
return FileNameEntry {
encoding_type: ImplicitASCII,
name: ascii_file_name,
};
}
return FileNameEntry {
encoding_type: ImplicitNonASCII,
name: legacy_decoder.to_string_lossy(&cd.file_name_raw),
};
})
.collect()
}
pub fn convert_central_directory_file_names(&mut self, legacy_decoder: &dyn IDecoder) {
self.cd_entries.iter_mut().for_each(|cd| {
if cd.is_encoded_in_utf8() {
let original_file_name = String::from_utf8_lossy(&cd.file_name_raw);
let nfc_file_name = compose_from_hfs_nfd(&original_file_name);
if original_file_name != nfc_file_name {
cd.set_file_name_from_slice(&nfc_file_name.as_bytes().to_vec());
}
return;
}
cd.set_file_name_from_slice(
&legacy_decoder
.to_string_lossy(&cd.file_name_raw)
.as_bytes()
.to_vec(),
);
cd.set_file_coment_from_slice(
&legacy_decoder
.to_string_lossy(&cd.file_comment)
.as_bytes()
.to_vec(),
);
cd.set_utf8_encoded_flag();
});
}
pub fn output_archive_with_central_directory_file_names<G: WriteBytesExt>(
&mut self,
dest_handler: &mut G,
) -> anyhow::Result<()> {
let mut pos: u64 = 0;
for cd in self.cd_entries.iter_mut() {
let mut local_header =
zip_local_file_header::ZipLocalFileHeader::from_central_directory(
&mut self.file_handler,
cd,
)?;
if local_header.file_name_length != cd.file_name_length {
local_header.set_file_name_from_slice(&cd.file_name_raw);
if cd.is_encoded_in_utf8() {
local_header.set_utf8_encoded_flag();
}
}
cd.local_header_position = pos as u32;
pos += local_header.write(dest_handler)?;
}
self.eocd.cd_starting_position = pos as u32;
let mut cd_new_size: u64 = 0;
for cd in self.cd_entries.iter_mut() {
cd_new_size += cd.write(dest_handler)?;
}
self.eocd.cd_size = cd_new_size as u32;
self.eocd.write(dest_handler)?;
return Ok(());
}
pub fn check_unsupported_zip_type(&self) -> Result<(), ZipReadError> {
return self.eocd.check_unsupported_zip_type();
}
}