dpp-tool 0.5.2

CLI to explore Apple DMG disk images: DMG → HFS+/APFS → PKG → PBZX → files
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//! dpp-tool — Fancy CLI for the full Apple DMG pipeline
//!
//! A cross-platform tool to explore DMG disk images end-to-end:
//! DMG → HFS+/APFS → PKG → PBZX → files

mod cmd_apfs;
mod cmd_bench;
mod cmd_dmg;
mod cmd_fs;
mod cmd_hfs;
mod cmd_info;
mod cmd_payload;
mod cmd_pkg;
mod extract;
mod pipeline;
mod style;

use std::io;
use std::path::PathBuf;
use std::process;

use clap::{Args, CommandFactory, Parser, Subcommand};
use clap_complete::{Shell, generate};

// ── Top-level CLI ────────────────────────────────────────────────────────

#[derive(Parser)]
#[command(
    name = "dpp-tool",
    about = "Apple DMG pipeline explorer — navigate DMG → HFS+/APFS → PKG → PBZX → files",
    version,
    after_help = "\
EXAMPLES:
    dpp-tool info Kernel_Debug_Kit.dmg
    dpp-tool --in-memory fs info small.dmg
    dpp-tool dmg ls Kernel_Debug_Kit.dmg
    dpp-tool fs tree Kernel_Debug_Kit.dmg /Library
    dpp-tool fs find Kernel_Debug_Kit.dmg -n \"*.kext\" -t d
    dpp-tool pkg ls Kernel_Debug_Kit.dmg /KernelDebugKit.pkg
    dpp-tool payload ls Kernel_Debug_Kit.dmg /path.pkg com.apple.pkg.KDK /"
)]
struct Cli {
    /// Extract partitions via temp file (default, low memory)
    #[arg(long, global = true, conflicts_with = "in_memory")]
    temp_file: bool,

    /// Buffer partitions in memory (faster for small DMGs)
    #[arg(long, global = true)]
    in_memory: bool,

    /// Disable colored output
    #[arg(long, global = true)]
    no_color: bool,

    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    /// Full pipeline overview
    Info {
        /// Path to the DMG file
        dmg: PathBuf,
    },

    /// Benchmark pipeline stages
    #[command(alias = "benchmark")]
    Bench {
        /// Path to the DMG file
        dmg: PathBuf,
    },

    /// DMG (UDIF) container commands
    Dmg {
        #[command(subcommand)]
        command: DmgCommand,
    },

    /// Filesystem commands (auto-detect HFS+/APFS)
    Fs {
        #[command(subcommand)]
        command: FsCommand,
    },

    /// HFS+ filesystem commands
    Hfs {
        #[command(subcommand)]
        command: HfsCommand,
    },

    /// APFS filesystem commands
    Apfs {
        #[command(subcommand)]
        command: ApfsCommand,
    },

    /// PKG (XAR) archive commands
    Pkg {
        #[command(subcommand)]
        command: PkgCommand,
    },

    /// Component payload (PBZX/CPIO) commands
    Payload {
        #[command(subcommand)]
        command: PayloadCommand,
    },

    /// Generate shell completions
    #[command(hide = true)]
    Completions {
        /// Shell to generate completions for
        shell: Shell,
    },
}

// ── DMG subcommands ──────────────────────────────────────────────────────

#[derive(Subcommand)]
enum DmgCommand {
    /// Format & compression stats
    Info {
        /// Path to the DMG file
        dmg: PathBuf,
    },
    /// List partitions
    Ls {
        /// Path to the DMG file
        dmg: PathBuf,
    },
    /// Raw partition data to stdout
    Cat {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Partition ID (default: main partition)
        partition_id: Option<i32>,
    },
}

// ── Filesystem subcommands (fs, hfs, apfs share a shape) ────────────────

#[derive(Args)]
struct FindArgs {
    /// Glob pattern to match file names
    #[arg(short, long)]
    name: Option<String>,

    /// Filter by type: f (file), d (directory), l (symlink)
    #[arg(short = 't', long = "type")]
    file_type: Option<char>,
}

#[derive(Subcommand)]
enum FsCommand {
    /// Volume info (auto-detect HFS+/APFS)
    Info {
        /// Path to the DMG file
        dmg: PathBuf,
    },
    /// List directory contents
    Ls {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Filesystem path to list
        path: String,
    },
    /// Browse filesystem tree
    Tree {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Root path for tree (default: /)
        path: Option<String>,
        /// Maximum tree depth
        #[arg(short, long, default_value_t = 3)]
        depth: usize,
    },
    /// Extract file to stdout
    Cat {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Filesystem path to extract
        path: String,
    },
    /// File metadata
    Stat {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Filesystem path to stat
        path: String,
    },
    /// Find files (default: *.pkg)
    Find {
        /// Path to the DMG file
        dmg: PathBuf,
        #[command(flatten)]
        args: FindArgs,
    },
    /// Extract files to a local directory
    Extract {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Filesystem path to extract (default: /)
        path: Option<String>,
        /// Output directory
        #[arg(short, long, default_value = ".")]
        output: PathBuf,
    },
}

#[derive(Subcommand)]
enum HfsCommand {
    /// HFS+ volume header
    Info {
        /// Path to the DMG file
        dmg: PathBuf,
    },
    /// List directory contents
    Ls {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Filesystem path to list
        path: String,
    },
    /// Browse filesystem tree
    Tree {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Root path for tree (default: /)
        path: Option<String>,
        /// Maximum tree depth
        #[arg(short, long, default_value_t = 3)]
        depth: usize,
    },
    /// Extract file to stdout
    Cat {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Filesystem path to extract
        path: String,
    },
    /// File metadata (CNID, perms, dates, forks)
    Stat {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Filesystem path to stat
        path: String,
    },
    /// Find files (default: *.pkg)
    Find {
        /// Path to the DMG file
        dmg: PathBuf,
        #[command(flatten)]
        args: FindArgs,
    },
    /// Extract files to a local directory
    Extract {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Filesystem path to extract (default: /)
        path: Option<String>,
        /// Output directory
        #[arg(short, long, default_value = ".")]
        output: PathBuf,
    },
}

#[derive(Subcommand)]
enum ApfsCommand {
    /// APFS volume info
    Info {
        /// Path to the DMG file
        dmg: PathBuf,
    },
    /// List directory contents
    Ls {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Filesystem path to list
        path: String,
    },
    /// Browse filesystem tree
    Tree {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Root path for tree (default: /)
        path: Option<String>,
        /// Maximum tree depth
        #[arg(short, long, default_value_t = 3)]
        depth: usize,
    },
    /// Extract file to stdout
    Cat {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Filesystem path to extract
        path: String,
    },
    /// File metadata (OID, perms, dates)
    Stat {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Filesystem path to stat
        path: String,
    },
    /// Find files (default: *.pkg)
    Find {
        /// Path to the DMG file
        dmg: PathBuf,
        #[command(flatten)]
        args: FindArgs,
    },
    /// Extract files to a local directory
    Extract {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Filesystem path to extract (default: /)
        path: Option<String>,
        /// Output directory
        #[arg(short, long, default_value = ".")]
        output: PathBuf,
    },
}

// ── PKG subcommands ──────────────────────────────────────────────────────

#[derive(Subcommand)]
enum PkgCommand {
    /// Package stats
    Info {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Filesystem path to the .pkg file
        pkg_path: String,
    },
    /// List XAR contents
    Ls {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Filesystem path to the .pkg file
        pkg_path: String,
    },
    /// Find entries (default: *.pkg components)
    Find {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Filesystem path to the .pkg file
        pkg_path: String,
        #[command(flatten)]
        args: FindArgs,
    },
    /// XAR entry to stdout
    Cat {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Filesystem path to the .pkg file
        pkg_path: String,
        /// File within the XAR to extract
        file: String,
    },
    /// Extract XAR archive contents to a local directory
    Extract {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Filesystem path to the .pkg file
        pkg_path: String,
        /// Path within the archive to extract (default: all)
        path: Option<String>,
        /// Output directory
        #[arg(short, long, default_value = ".")]
        output: PathBuf,
    },
}

// ── Payload subcommands ──────────────────────────────────────────────────

#[derive(Subcommand)]
enum PayloadCommand {
    /// Payload (PBZX/CPIO) stats
    Info {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Filesystem path to the .pkg file
        pkg_path: String,
        /// Component identifier
        component: String,
    },
    /// List payload files
    Ls {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Filesystem path to the .pkg file
        pkg_path: String,
        /// Component identifier
        component: String,
        /// Path within the payload (default: /)
        path: Option<String>,
    },
    /// Browse payload tree
    Tree {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Filesystem path to the .pkg file
        pkg_path: String,
        /// Component identifier
        component: String,
        /// Root path for tree (default: /)
        path: Option<String>,
        /// Maximum tree depth
        #[arg(short, long, default_value_t = 3)]
        depth: usize,
    },
    /// Find payload files
    Find {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Filesystem path to the .pkg file
        pkg_path: String,
        /// Component identifier
        component: String,
        #[command(flatten)]
        args: FindArgs,
    },
    /// Extract payload file to stdout
    Cat {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Filesystem path to the .pkg file
        pkg_path: String,
        /// Component identifier
        component: String,
        /// File within the payload to extract
        file: String,
    },
    /// Extract payload contents to a local directory
    Extract {
        /// Path to the DMG file
        dmg: PathBuf,
        /// Filesystem path to the .pkg file
        pkg_path: String,
        /// Component identifier
        component: String,
        /// Path within the payload to extract (default: all)
        path: Option<String>,
        /// Output directory
        #[arg(short, long, default_value = ".")]
        output: PathBuf,
    },
}

// ── main ─────────────────────────────────────────────────────────────────

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

    // Initialize color support
    style::init_color(cli.no_color);

    let mode = if cli.in_memory {
        dpp::ExtractMode::InMemory
    } else {
        dpp::ExtractMode::default()
    };

    let result = match cli.command {
        Command::Info { dmg } => cmd_info::run(&dmg, mode),
        Command::Bench { dmg } => cmd_bench::run(&dmg, mode),
        Command::Dmg { command } => cmd_dmg::run(command, mode),
        Command::Fs { command } => cmd_fs::run(command, mode),
        Command::Hfs { command } => cmd_hfs::run(command, mode),
        Command::Apfs { command } => cmd_apfs::run(command, mode),
        Command::Pkg { command } => cmd_pkg::run(command, mode),
        Command::Payload { command } => cmd_payload::run(command, mode),
        Command::Completions { shell } => {
            let mut cmd = Cli::command();
            generate(shell, &mut cmd, "dpp-tool", &mut io::stdout());
            Ok(())
        }
    };

    if let Err(e) = result {
        eprintln!("{}error:{} {e}", style::red(), style::reset());
        process::exit(1);
    }
}