zifu 1.0.0

Repair corrupted file names in ZIP archives
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
use ansi_term::ANSIGenericString;
use anyhow::anyhow;
use clap::{crate_authors, crate_description, crate_version, AppSettings, Clap};
use filename_decoder::IDecoder;
use lazy_static::lazy_static;
use rand::rngs::StdRng;
use rand::{RngCore, SeedableRng};
use std::borrow::Cow;
use std::fs::File;
use std::io::{BufReader, BufWriter};
use std::vec;
use zifu_core::InputZIPArchive;
use zifu_core::{filename_decoder, FileNameEncodingType, FileNameEntry, FileNamesDiagnosis};

#[derive(thiserror::Error, Debug)]
enum InvalidArgument {
    #[error("no argument <{arg_name}> was passed")]
    NoArgument { arg_name: String },
    #[error("unknown encoding name: {encoding_name}")]
    InvalidEncodingName { encoding_name: String },
    #[error("you cannot specify the same file for input and output files.")]
    SameInputOutput,
}

/// Global behavior options for this program
#[derive(Debug, Clone, Copy, Default)]
pub struct BehaviorFlags {
    pub verbose: bool,
    pub ask_user: bool,
}

/// Returns reset given ANSI style if non-tty
fn prepare_for_non_tty(style: ansi_term::Style) -> ansi_term::Style {
    if atty::is(atty::Stream::Stdout) {
        style
    } else {
        ansi_term::Style::default()
    }
}

/// Prints messages (`.get_status_primary_message()` & `.get_statius_note()`)
pub fn print_status_message(diagnosis: &FileNamesDiagnosis) {
    use ansi_term::Colour::*;
    println!(
        "{}  {}",
        prepare_for_non_tty(
            (if diagnosis.is_universal_archive() {
                Green
            } else {
                Red
            })
            .bold()
        )
        .paint(diagnosis.get_status_primary_message()),
        prepare_for_non_tty(
            (if diagnosis.is_universal_archive() {
                Green
            } else {
                Yellow
            })
            .bold()
        )
        .paint(diagnosis.get_status_note())
    );
}

fn print_you_do_not_have_to_apply_this_tool(diagnosis: &FileNamesDiagnosis) {
    use ansi_term::Colour::*;
    eprintln!(
        "{}  {}\n{}  {}",
        prepare_for_non_tty(Green.bold()).paint(diagnosis.get_status_primary_message()),
        prepare_for_non_tty(Green.bold()).paint(diagnosis.get_status_note()),
        prepare_for_non_tty(Green.bold()).paint("You do not have to apply this tool."),
        prepare_for_non_tty(Yellow.bold()).paint("Existing.")
    );
}

/// Decodes and prints file names in central directories to stdout
///
/// # Arguments
///
/// * `cd_entries` - Central directories (contains file names)
/// * `utf8_decoder` - UTF-8 decoder (used when explicitly encoded in UTF-8)
/// * `legacy_decoder` - Legacy charset decoder (used otherwise)
fn list_names_in_archive(fie_name_entries: &[FileNameEntry], legacy_decoder: &dyn IDecoder) {
    use ansi_term::Colour::*;
    use FileNameEncodingType::*;
    lazy_static! {
        static ref REGULAR_UTF8: ANSIGenericString<'static, str> =
            prepare_for_non_tty(Green.bold()).paint("REGULAR UTF-8");
        static ref IRREGULAR_UTF8: ANSIGenericString<'static, str> =
            prepare_for_non_tty(Red.bold()).paint("IRREGULAR UTF-8");
        static ref ASCII_GREEN: ANSIGenericString<'static, str> =
            prepare_for_non_tty(Green.bold()).paint("ASCII");
        static ref GUESSED: ANSIGenericString<'static, str> =
            prepare_for_non_tty(Red.bold()).paint("GUESSED");
    }
    for entry in fie_name_entries {
        match entry.encoding_type {
            ExplicitRegularUTF8 => println!("{}:{}", &*REGULAR_UTF8, &entry.name),
            ExplicitIrregularUTF8 => println!("{}:{}", &*IRREGULAR_UTF8, &entry.name),
            ImplicitASCII => println!("{}:{}", &*ASCII_GREEN, &entry.name),
            ImplicitNonASCII => println!(
                "{} {}:{}",
                prepare_for_non_tty(Red.bold()).paint(legacy_decoder.encoding_name()),
                &*GUESSED,
                &entry.name
            ),
        }
    }
}

fn process_answer_default_yes(ans: &str) -> bool {
    return match ans.chars().next() {
        Some('n') | Some('N') => false,
        None | Some(_) => true,
    };
}

/// Returns `Ok(false)` if a line starting with `'n'` (or `'N'`) is input from stdin, otherwise `Ok(true)`.
///
/// Returns `Err(std::io::Error)` if I/O fails.
fn ask_default_yes() -> Result<bool, std::io::Error> {
    let ask_result = (|| {
        let mut ret = String::new();
        match std::io::stdin().read_line(&mut ret) {
            Ok(_) => return Ok(ret),
            Err(e) => return Err(e),
        }
    })()?;
    return Ok(process_answer_default_yes(&ask_result));
}

#[derive(Clap)]
#[clap(name = "ZIP File Names to UTF-8 (ZIFU)", version = crate_version!(), author = crate_authors!(), about = crate_description!())]
#[clap(setting = AppSettings::ColoredHelp)]
struct CLIOptions {
    #[clap(
        about = "Path to the ZIP file where you want to change the encoding of the file name to UTF-8"
    )]
    input: String,
    #[clap(about = "Path to output")]
    output: Option<String>,
    #[clap(
        short,
        long,
        about = "Finds out if its file names are encoded in UTF-8."
    )]
    check: bool,
    #[clap(
        short,
        long,
        about = "Displays the list of file names in the ZIP archive."
    )]
    list: bool,
    #[clap(short, long, about = "Don't show any messages. (implies -y)")]
    silent: bool,
    #[clap(short, long, about = "Don't show any messages. (implies -y)")]
    quiet: bool,
    #[clap(
        short,
        long,
        value_name = "ENCODING",
        about = "Specifies the encoding of file names in the ZIP archive."
    )]
    encoding: Option<String>,
    #[clap(
        short,
        long,
        about = "Treats the encoding of the ZIP archive as UTF-8 first. (Default: try legacy encoding first)"
    )]
    utf8: bool,
    #[clap(short, long, about = "Don't confirm")]
    yes: bool,
    #[clap(
        short,
        long,
        about = "Try to convert even if we don't have to apply this tool."
    )]
    force: bool,
    #[clap(short, long, about = "Replace the archive")]
    in_place: bool,
}

impl CLIOptions {
    pub fn to_behavior_flags(&self) -> BehaviorFlags {
        let verbose = !self.silent && !self.quiet;
        return BehaviorFlags {
            verbose,
            ask_user: verbose && !self.yes,
        };
    }
}

fn main() -> anyhow::Result<()> {
    let cli_options = CLIOptions::parse();

    let behavior_flags = cli_options.to_behavior_flags();
    let mut input_zip_file = InputZIPArchive::new(BufReader::new(File::open(&cli_options.input)?))?;

    input_zip_file.check_unsupported_zip_type()?;

    if cli_options.check {
        let archive_names_type = input_zip_file.diagnose_file_name_encoding();
        print_status_message(&archive_names_type);
        std::process::exit(if archive_names_type.is_universal_archive() {
            0
        } else {
            2
        });
    }

    let legacy_decoder = if let Some(encoding_name) = cli_options.encoding.as_deref() {
        <dyn filename_decoder::IDecoder>::from_encoding_name(encoding_name).ok_or(
            InvalidArgument::InvalidEncodingName {
                encoding_name: encoding_name.to_string(),
            },
        )?
    } else {
        <dyn filename_decoder::IDecoder>::native_oem_encoding()
    };
    let utf8_decoder = <dyn filename_decoder::IDecoder>::utf8();
    let ascii_decoder = <dyn filename_decoder::IDecoder>::ascii();
    let decoders_list = if cli_options.utf8 {
        vec![&*ascii_decoder, &*utf8_decoder, &*legacy_decoder]
    } else {
        vec![&*ascii_decoder, &*legacy_decoder, &*utf8_decoder]
    };
    // Detect encoding by trying decoding all of file names and comments
    let best_fit_decoder_index_ = input_zip_file.get_filename_decoder_index(&decoders_list);
    best_fit_decoder_index_.ok_or(anyhow!(
        "file names & comments are not encoded in UTF-8 or {}.  Try with -e <another encoding> option.",
        legacy_decoder.encoding_name()
    ))?;
    let guessed_encoder = decoders_list[best_fit_decoder_index_.unwrap()];

    if cli_options.list {
        list_names_in_archive(
            &input_zip_file.get_file_names_list(guessed_encoder),
            guessed_encoder,
        );
        return Ok(());
    }
    if behavior_flags.verbose || behavior_flags.ask_user {
        list_names_in_archive(
            &input_zip_file.get_file_names_list(guessed_encoder),
            guessed_encoder,
        );
        if !cli_options.force
            && input_zip_file
                .diagnose_file_name_encoding()
                .is_universal_archive()
        {
            print_you_do_not_have_to_apply_this_tool(&input_zip_file.diagnose_file_name_encoding());
            std::process::exit(2);
        }

        if behavior_flags.ask_user {
            eprint!("Are these file names correct? [Y/n]: ");
            if !(ask_default_yes()?) {
                std::process::exit(1);
            }
        }
    } else if !cli_options.force
        && input_zip_file
            .diagnose_file_name_encoding()
            .is_universal_archive()
    {
        print_you_do_not_have_to_apply_this_tool(&input_zip_file.diagnose_file_name_encoding());
        std::process::exit(2);
    }

    let output_zip_file_path: Cow<str> = if cli_options.in_place {
        // Temporary file name in hte same directory (expecting that rename reuses file contents (& inodes))
        let mut rng = StdRng::from_entropy();
        // I do not know the signal handling to remove the temporary file when interrupted
        Cow::from(format!("{}.{:016x}.tmp", cli_options.input, rng.next_u64()))
    } else {
        let output_zip_file_str =
            cli_options
                .output
                .as_ref()
                .ok_or(InvalidArgument::NoArgument {
                    arg_name: "output".to_string(),
                })?;
        if &(cli_options.input) == output_zip_file_str {
            return Err(InvalidArgument::SameInputOutput.into());
        }
        Cow::from(output_zip_file_str)
    };
    input_zip_file.convert_central_directory_file_names(guessed_encoder);
    let mut output_zip_file = BufWriter::new(File::create(output_zip_file_path.as_ref())?);
    input_zip_file.output_archive_with_central_directory_file_names(&mut output_zip_file)?;
    if cli_options.in_place {
        // Make files closed
        drop(output_zip_file);
        drop(input_zip_file);
        std::fs::remove_file(&cli_options.input)?;
        std::fs::rename(output_zip_file_path.as_ref(), &cli_options.input)?;
    }

    return Ok(());
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn basic_args_parse_test() {
        let cli_options = CLIOptions::parse_from(vec!["zifu", "before.zip", "after.zip"]);
        let global_flags = cli_options.to_behavior_flags();

        assert_eq!(global_flags.ask_user, true);
        assert_eq!(global_flags.verbose, true);

        assert_eq!(cli_options.input, "before.zip");
        assert_eq!(cli_options.output.as_deref(), Some("after.zip"));
    }

    #[test]
    fn extended_args_parse_test1() {
        let cli_options =
            CLIOptions::parse_from(vec!["zifu", "before.zip", "after.zip", "-q", "-u", "-l"]);
        let global_flags = cli_options.to_behavior_flags();

        assert_eq!(global_flags.ask_user, false);
        assert_eq!(global_flags.verbose, false);

        assert_eq!(cli_options.input, "before.zip");
        assert_eq!(cli_options.output.as_deref(), Some("after.zip"));
        assert_eq!(cli_options.encoding.as_deref(), None);
        assert_eq!(cli_options.utf8, true);
        assert_eq!(cli_options.check, false);
        assert_eq!(cli_options.list, true);
        assert_eq!(cli_options.force, false);
        assert_eq!(cli_options.in_place, false);
    }

    #[test]
    fn extended_args_parse_test2() {
        let cli_options = CLIOptions::parse_from(vec![
            "zifu",
            "before.zip",
            "after.zip",
            "-s",
            "-e",
            "sjis",
            "-c",
        ]);
        let global_flags = cli_options.to_behavior_flags();

        assert_eq!(global_flags.ask_user, false);
        assert_eq!(global_flags.verbose, false);

        assert_eq!(cli_options.input, "before.zip");
        assert_eq!(cli_options.output.as_deref(), Some("after.zip"));
        assert_eq!(cli_options.encoding.as_deref(), Some("sjis"));
        assert_eq!(cli_options.utf8, false);
        assert_eq!(cli_options.check, true);
        assert_eq!(cli_options.list, false);
        assert_eq!(cli_options.force, false);
        assert_eq!(cli_options.in_place, false);
    }

    #[test]
    fn extended_args_parse_test3() {
        let cli_options = CLIOptions::parse_from(vec![
            "zifu",
            "before.zip",
            "after.zip",
            "-y",
            "--encoding",
            "cp437",
        ]);
        let global_flags = cli_options.to_behavior_flags();

        assert_eq!(global_flags.ask_user, false);
        assert_eq!(global_flags.verbose, true);

        assert_eq!(cli_options.input, "before.zip");
        assert_eq!(cli_options.output.as_deref(), Some("after.zip"));
        assert_eq!(cli_options.encoding.as_deref(), Some("cp437"));
        assert_eq!(cli_options.utf8, false);
        assert_eq!(cli_options.check, false);
        assert_eq!(cli_options.list, false);
        assert_eq!(cli_options.force, false);
        assert_eq!(cli_options.in_place, false);
    }

    #[test]
    fn extended_args_parse_test4() {
        let cli_options = CLIOptions::parse_from(vec![
            "zifu",
            "before.zip",
            "after.zip",
            "--yes",
            "-e",
            "gbk",
            "-f",
        ]);
        let global_flags = cli_options.to_behavior_flags();

        assert_eq!(global_flags.ask_user, false);
        assert_eq!(global_flags.verbose, true);

        assert_eq!(cli_options.input, "before.zip");
        assert_eq!(cli_options.output.as_deref(), Some("after.zip"));
        assert_eq!(cli_options.encoding.as_deref(), Some("gbk"));
        assert_eq!(cli_options.utf8, false);
        assert_eq!(cli_options.check, false);
        assert_eq!(cli_options.list, false);
        assert_eq!(cli_options.force, true);
        assert_eq!(cli_options.in_place, false);
    }

    #[test]
    fn extended_args_parse_test5() {
        let cli_options = CLIOptions::parse_from(vec!["zifu", "before.zip", "-i"]);
        assert_eq!(cli_options.input, "before.zip");
        assert_eq!(cli_options.output.as_deref(), None);
        assert_eq!(cli_options.encoding.as_deref(), None);
        assert_eq!(cli_options.utf8, false);
        assert_eq!(cli_options.check, false);
        assert_eq!(cli_options.list, false);
        assert_eq!(cli_options.force, false);
        assert_eq!(cli_options.in_place, true);
    }
}