pklib 0.2.0

Pure Rust implementation of PKWare Data Compression Library (DCL) with full PKLib compatibility
Documentation
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
//! blast-cli - Command-line interface for PKLib
//!
//! A command-line tool for compressing and decompressing files using the PKWare DCL format.

use clap::{Parser, Subcommand, ValueEnum};
use indicatif::{ProgressBar, ProgressStyle};
use pklib::{explode_bytes, implode_bytes, CompressionMode, DictionarySize};
use std::fs;
use std::path::PathBuf;
use std::time::Instant;

#[derive(Parser)]
#[command(name = "blast-cli")]
#[command(about = "A CLI tool for PKWare DCL compression and decompression")]
#[command(version)]
struct Cli {
    #[command(subcommand)]
    command: Commands,

    /// Verbose output
    #[arg(short, long)]
    verbose: bool,

    /// Quiet mode (suppress non-error output)
    #[arg(short, long)]
    quiet: bool,
}

#[derive(Subcommand)]
enum Commands {
    /// Compress a file using PKLib format
    Compress {
        /// Input file to compress
        input: PathBuf,

        /// Output compressed file
        output: PathBuf,

        /// Compression mode
        #[arg(short, long, value_enum, default_value_t = CliCompressionMode::Binary)]
        mode: CliCompressionMode,

        /// Dictionary size
        #[arg(short, long, value_enum, default_value_t = CliDictionarySize::Size2K)]
        dict_size: CliDictionarySize,

        /// Force overwrite of output file
        #[arg(short, long)]
        force: bool,
    },

    /// Decompress a PKLib-compressed file
    Decompress {
        /// Input compressed file
        input: PathBuf,

        /// Output decompressed file
        output: PathBuf,

        /// Force overwrite of output file
        #[arg(short, long)]
        force: bool,
    },

    /// Get information about a compressed file
    Info {
        /// Compressed file to analyze
        input: PathBuf,
    },
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
enum CliCompressionMode {
    /// Binary compression mode (optimized for binary data)
    Binary,
    /// ASCII compression mode (optimized for text data)
    Ascii,
}

impl From<CliCompressionMode> for CompressionMode {
    fn from(mode: CliCompressionMode) -> Self {
        match mode {
            CliCompressionMode::Binary => CompressionMode::Binary,
            CliCompressionMode::Ascii => CompressionMode::ASCII,
        }
    }
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
enum CliDictionarySize {
    /// 1KB dictionary (1024 bytes)
    Size1K,
    /// 2KB dictionary (2048 bytes) - Default
    Size2K,
    /// 4KB dictionary (4096 bytes)
    Size4K,
}

impl From<CliDictionarySize> for DictionarySize {
    fn from(size: CliDictionarySize) -> Self {
        match size {
            CliDictionarySize::Size1K => DictionarySize::Size1K,
            CliDictionarySize::Size2K => DictionarySize::Size2K,
            CliDictionarySize::Size4K => DictionarySize::Size4K,
        }
    }
}

fn main() {
    let cli = Cli::parse();

    let result = match cli.command {
        Commands::Compress {
            input,
            output,
            mode,
            dict_size,
            force,
        } => compress_file(
            &input,
            &output,
            mode.into(),
            dict_size.into(),
            force,
            cli.verbose,
            cli.quiet,
        ),
        Commands::Decompress {
            input,
            output,
            force,
        } => decompress_file(&input, &output, force, cli.verbose, cli.quiet),
        Commands::Info { input } => show_file_info(&input, cli.verbose),
    };

    if let Err(e) = result {
        eprintln!("Error: {e}");
        std::process::exit(1);
    }
}

fn compress_file(
    input: &PathBuf,
    output: &PathBuf,
    mode: CompressionMode,
    dict_size: DictionarySize,
    force: bool,
    verbose: bool,
    quiet: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    // Check if input file exists
    if !input.exists() {
        return Err(format!("Input file '{}' does not exist", input.display()).into());
    }

    // Check if output file exists and force flag
    if output.exists() && !force {
        return Err(format!(
            "Output file '{}' already exists. Use --force to overwrite",
            output.display()
        )
        .into());
    }

    if verbose {
        println!(
            "Compressing '{}' to '{}'",
            input.display(),
            output.display()
        );
        println!("Mode: {mode:?}, Dictionary: {dict_size:?}");
    }

    let start_time = Instant::now();

    // Read input file
    let input_data = fs::read(input)?;
    let input_size = input_data.len();

    if verbose {
        println!("Input size: {input_size} bytes");
    }

    // Show progress bar for large files
    let progress = if !quiet && input_size > 1024 * 1024 {
        let pb = ProgressBar::new(2);
        pb.set_style(
            ProgressStyle::default_bar()
                .template(
                    "{spinner:.green} [{elapsed_precise}] {bar:40.cyan/blue} {pos}/{len} {msg}",
                )
                .unwrap()
                .progress_chars("#>-"),
        );
        pb.set_message("Compressing...");
        Some(pb)
    } else {
        None
    };

    if let Some(ref pb) = progress {
        pb.inc(1);
    }

    // Compress data
    let compressed_data = implode_bytes(&input_data, mode, dict_size)
        .map_err(|e| format!("Compression failed: {e}"))?;

    if let Some(ref pb) = progress {
        pb.inc(1);
        pb.finish_with_message("Compression complete");
    }

    // Write output file
    fs::write(output, &compressed_data)?;

    let compression_time = start_time.elapsed();
    let output_size = compressed_data.len();
    let compression_ratio = (output_size as f64 / input_size as f64) * 100.0;

    if !quiet {
        println!("✓ Compression successful!");
        println!("  Input:  {input_size} bytes");
        println!("  Output: {output_size} bytes");
        println!("  Ratio:  {compression_ratio:.1}%");
        println!("  Time:   {compression_time:.2?}");

        if compression_ratio > 100.0 {
            println!("  Note: File expanded during compression (common for small/random data)");
        }
    }

    Ok(())
}

fn decompress_file(
    input: &PathBuf,
    output: &PathBuf,
    force: bool,
    verbose: bool,
    quiet: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    // Check if input file exists
    if !input.exists() {
        return Err(format!("Input file '{}' does not exist", input.display()).into());
    }

    // Check if output file exists and force flag
    if output.exists() && !force {
        return Err(format!(
            "Output file '{}' already exists. Use --force to overwrite",
            output.display()
        )
        .into());
    }

    if verbose {
        println!(
            "Decompressing '{}' to '{}'",
            input.display(),
            output.display()
        );
    }

    let start_time = Instant::now();

    // Read input file
    let compressed_data = fs::read(input)?;
    let input_size = compressed_data.len();

    if verbose {
        println!("Compressed size: {input_size} bytes");
    }

    // Show progress bar for large files
    let progress = if !quiet && input_size > 1024 * 1024 {
        let pb = ProgressBar::new(2);
        pb.set_style(
            ProgressStyle::default_bar()
                .template(
                    "{spinner:.green} [{elapsed_precise}] {bar:40.cyan/blue} {pos}/{len} {msg}",
                )
                .unwrap()
                .progress_chars("#>-"),
        );
        pb.set_message("Decompressing...");
        Some(pb)
    } else {
        None
    };

    if let Some(ref pb) = progress {
        pb.inc(1);
    }

    // Decompress data
    let decompressed_data =
        explode_bytes(&compressed_data).map_err(|e| format!("Decompression failed: {e}"))?;

    if let Some(ref pb) = progress {
        pb.inc(1);
        pb.finish_with_message("Decompression complete");
    }

    // Write output file
    fs::write(output, &decompressed_data)?;

    let decompression_time = start_time.elapsed();
    let output_size = decompressed_data.len();
    let compression_ratio = (input_size as f64 / output_size as f64) * 100.0;

    if !quiet {
        println!("✓ Decompression successful!");
        println!("  Input:  {input_size} bytes");
        println!("  Output: {output_size} bytes");
        println!("  Ratio:  {compression_ratio:.1}%");
        println!("  Time:   {decompression_time:.2?}");
    }

    Ok(())
}

fn show_file_info(input: &PathBuf, verbose: bool) -> Result<(), Box<dyn std::error::Error>> {
    // Check if input file exists
    if !input.exists() {
        return Err(format!("Input file '{}' does not exist", input.display()).into());
    }

    // Read the file
    let data = fs::read(input)?;
    let file_size = data.len();

    if data.len() < 3 {
        return Err("File too small to be a valid PKLib compressed file".into());
    }

    // Parse PKLib header
    let compression_type = data[0];
    let dict_bits = data[1];

    let mode_str = match compression_type {
        0 => "Binary",
        1 => "ASCII",
        _ => "Unknown",
    };

    let dict_size_str = match dict_bits {
        4 => "1KB (1024 bytes)",
        5 => "2KB (2048 bytes)",
        6 => "4KB (4096 bytes)",
        _ => "Unknown",
    };

    println!("PKLib File Information:");
    println!("  File: {}", input.display());
    println!("  Size: {file_size} bytes");
    println!("  Compression Mode: {mode_str} ({compression_type})");
    println!("  Dictionary Size: {dict_size_str} ({dict_bits} bits)");

    if verbose {
        println!(
            "  Header bytes: {:02x} {:02x} {:02x}",
            data[0], data[1], data[2]
        );
    }

    // Try to get decompressed size by attempting decompression
    match explode_bytes(&data) {
        Ok(decompressed) => {
            let decompressed_size = decompressed.len();
            let compression_ratio = (file_size as f64 / decompressed_size as f64) * 100.0;
            println!("  Decompressed Size: {decompressed_size} bytes");
            println!("  Compression Ratio: {compression_ratio:.1}%");
            println!("  Status: ✓ Valid PKLib file");
        }
        Err(e) => {
            println!("  Status: ✗ Invalid or corrupted PKLib file");
            if verbose {
                println!("  Error: {e}");
            }
        }
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::tempdir;

    #[test]
    fn test_round_trip() -> Result<(), Box<dyn std::error::Error>> {
        let dir = tempdir()?;
        let input_path = dir.path().join("input.txt");
        let compressed_path = dir.path().join("compressed.pklib");
        let output_path = dir.path().join("output.txt");

        // Create test data
        let test_data = b"Hello, World! This is a test of the PKLib CLI tool.";
        fs::write(&input_path, test_data)?;

        // Compress
        compress_file(
            &input_path,
            &compressed_path,
            CompressionMode::ASCII,
            DictionarySize::Size2K,
            false,
            false,
            true,
        )?;

        // Decompress
        decompress_file(&compressed_path, &output_path, false, false, true)?;

        // Verify
        let result_data = fs::read(&output_path)?;
        assert_eq!(test_data, &result_data[..]);

        Ok(())
    }
}