wim 0.1.2

Windows Imaging (WIM) archive reader and writer
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
// SPDX-License-Identifier: LGPL-2.1-or-later
//! Original debug/information output through the host C stdout stream.
use crate::engine::handles::WimHandle;
use std::ffi::{CString, c_int};
use wim_format::{Header, ResourceHeader, xml::XmlInfo};

#[cfg(unix)]
unsafe extern "C" {
    static mut stdout: *mut libc::FILE;
}
#[cfg(windows)]
unsafe extern "C" {
    fn wprintf(format: *const u16, ...) -> c_int;
    fn wcsftime(output: *mut u16, size: usize, format: *const u16, time: *const libc::tm) -> usize;
}

// Preserve each original formatting argument boundary. Windows text-mode
// wprintf can stop converting one %ls argument while still emitting its suffix.
struct PrintField {
    prefix: String,
    value: Vec<u8>,
    suffix: &'static str,
}
#[derive(Default)]
struct ImageOutput {
    fields: Vec<PrintField>,
}
impl ImageOutput {
    fn text(&mut self, value: &[u8]) {
        self.fields.push(PrintField {
            prefix: String::new(),
            value: value.to_vec(),
            suffix: "",
        });
    }
    fn field(&mut self, prefix: String, value: &[u8], suffix: &'static str) {
        self.fields.push(PrintField {
            prefix,
            value: value.to_vec(),
            suffix,
        });
    }
}
fn line(output: &mut Vec<u8>, label: &str, value: impl std::fmt::Display) {
    output.extend_from_slice(format!("{label:<28}= {value}\n").as_bytes());
}
fn resource(output: &mut Vec<u8>, names: [&str; 4], header: &ResourceHeader) {
    line(output, names[0], header.size_in_wim);
    line(output, names[1], format_args!("0x{:x}", header.flags));
    line(output, names[2], header.offset_in_wim);
    line(output, names[3], header.uncompressed_size);
}
fn header_text(header: &Header) -> Vec<u8> {
    let mut output = b"Magic Characters            = ".to_vec();
    for &byte in &header.magic {
        // SAFETY: isalpha accepts values representable by unsigned char.
        if unsafe { libc::isalpha(c_int::from(byte)) } != 0 {
            output.push(byte);
        } else {
            output.extend_from_slice(format!("\\{byte:o}").as_bytes());
        }
    }
    output.push(b'\n');
    line(&mut output, "Header Size", 208);
    line(
        &mut output,
        "Version",
        format_args!("0x{:x}", header.version),
    );
    line(&mut output, "Flags", format_args!("0x{:x}", header.flags));
    for (flag, name) in [
        (1, "RESERVED"),
        (2, "COMPRESSION"),
        (4, "READONLY"),
        (8, "SPANNED"),
        (16, "RESOURCE_ONLY"),
        (32, "METADATA_ONLY"),
        (64, "WRITE_IN_PROGRESS"),
        (128, "RP_FIX"),
        (0x10000, "COMPRESS_RESERVED"),
        (0x40000, "COMPRESS_LZX"),
        (0x20000, "COMPRESS_XPRESS"),
        (0x80000, "COMPRESS_LZMS"),
        (0x200000, "COMPRESS_XPRESS_2"),
    ] {
        if header.flags & flag != 0 {
            output.extend_from_slice(format!("    WIM_HDR_FLAG_{name} is set\n").as_bytes());
        }
    }
    line(&mut output, "Chunk Size", header.chunk_size);
    output.extend_from_slice(b"GUID                        = ");
    for byte in header.guid {
        output.extend_from_slice(format!("{byte:02x}").as_bytes());
    }
    output.push(b'\n');
    line(&mut output, "Part Number", header.part_number);
    line(&mut output, "Total Parts", header.total_parts);
    line(&mut output, "Image Count", header.image_count);
    resource(
        &mut output,
        [
            "Blob Table Size",
            "Blob Table Flags",
            "Blob Table Offset",
            "Blob Table Original_size",
        ],
        &header.blob_table,
    );
    resource(
        &mut output,
        [
            "XML Data Size",
            "XML Data Flags",
            "XML Data Offset",
            "XML Data Original Size",
        ],
        &header.xml_data,
    );
    resource(
        &mut output,
        [
            "Boot Metadata Size",
            "Boot Metadata Flags",
            "Boot Metadata Offset",
            "Boot Metadata Original Size",
        ],
        &header.boot_metadata,
    );
    line(&mut output, "Boot Index", header.boot_index);
    resource(
        &mut output,
        [
            "Integrity Size",
            "Integrity Flags",
            "Integrity Offset",
            "Integrity Original_size",
        ],
        &header.integrity_table,
    );
    output
}
fn text_line(output: &mut ImageOutput, label: &str, value: &[u8]) {
    output.field(format!("{label:<24}"), value, "\n");
}
fn number(value: Option<&[u8]>, base: c_int) -> u64 {
    let Some(value) = value else {
        return 0;
    };
    let Ok(value) = CString::new(value) else {
        return 0;
    };
    let mut end = std::ptr::null_mut();
    // SAFETY: The input is terminated; strtoull writes one valid end pointer.
    let parsed = unsafe { libc::strtoull(value.as_ptr(), &mut end, base) };
    // SAFETY: The end pointer is inside the live CString, including its terminator.
    if end == value.as_ptr().cast_mut() || unsafe { *end } != 0 || parsed == u64::MAX {
        0
    } else {
        parsed
    }
}
fn timestamp(xml: &XmlInfo, image: i32, path: &[u8]) -> Vec<u8> {
    let mut timestamp = 0u64;
    for (name, value) in xml.child_elements(image, path) {
        match name {
            b"HIGHPART" => timestamp |= number(value, 16).wrapping_shl(32),
            b"LOWPART" => timestamp |= number(value, 16),
            _ => {}
        }
    }
    let seconds = (timestamp / 10_000_000).wrapping_sub(11_644_473_600) as libc::time_t;
    // SAFETY: The POSIX broken-down time structure accepts zero initialization.
    let mut time = unsafe { std::mem::zeroed::<libc::tm>() };
    #[cfg(unix)]
    let mut output = [0u8; 64];
    #[cfg(windows)]
    let mut output = [0u16; 64];
    // SAFETY: Both structures are live and strftime receives the writable extent.
    unsafe {
        #[cfg(unix)]
        let failed = libc::gmtime_r(&seconds, &mut time).is_null();
        #[cfg(windows)]
        let failed = libc::gmtime_s(&mut time, &seconds) != 0;
        if failed {
            return Vec::new();
        }
        #[cfg(unix)]
        {
            let len = libc::strftime(
                output.as_mut_ptr().cast(),
                output.len(),
                c"%a %b %d %H:%M:%S %Y UTC".as_ptr(),
                &time,
            );
            output[..len].to_vec()
        }
        #[cfg(windows)]
        {
            let format: Vec<u16> = "%a %b %d %H:%M:%S %Y UTC"
                .encode_utf16()
                .chain(std::iter::once(0))
                .collect();
            let len = wcsftime(output.as_mut_ptr(), output.len(), format.as_ptr(), &time);
            wim_format::platform_text::utf16_to_wtf8(&output[..len]).unwrap_or_default()
        }
    }
}
fn optional(xml: &XmlInfo, image: i32, path: &[u8], label: &str, output: &mut ImageOutput) {
    if let Some(text) = xml.get_property_bytes(image, path) {
        text_line(output, label, text);
    }
}
fn numeric(xml: &XmlInfo, image: i32, path: &[u8], label: &str, output: &mut ImageOutput) {
    text_line(
        output,
        label,
        number(xml.get_property_bytes(image, path), 10)
            .to_string()
            .as_bytes(),
    );
}
fn image_text(xml: &XmlInfo, image: i32, output: &mut ImageOutput) {
    text_line(output, "Index:", image.to_string().as_bytes());
    text_line(output, "Name:", xml.name_bytes(image).unwrap_or(b""));
    text_line(
        output,
        "Description:",
        xml.description_bytes(image).unwrap_or(b""),
    );
    optional(xml, image, b"DISPLAYNAME", "Display Name:", output);
    optional(
        xml,
        image,
        b"DISPLAYDESCRIPTION",
        "Display Description:",
        output,
    );
    for (path, label) in [
        (b"DIRCOUNT".as_slice(), "Directory Count:"),
        (b"FILECOUNT", "File Count:"),
        (b"TOTALBYTES", "Total Bytes:"),
        (b"HARDLINKBYTES", "Hard Link Bytes:"),
    ] {
        numeric(xml, image, path, label, output);
    }
    text_line(
        output,
        "Creation Time:",
        &timestamp(xml, image, b"CREATIONTIME"),
    );
    text_line(
        output,
        "Last Modification Time:",
        &timestamp(xml, image, b"LASTMODIFICATIONTIME"),
    );
    if xml.has_element(image, b"WINDOWS") {
        let architecture = match number(xml.get_property_bytes(image, b"WINDOWS/ARCH"), 10) {
            0 => b"x86".as_slice(),
            1 => b"MIPS",
            5 => b"ARM",
            6 => b"ia64",
            9 => b"x86_64",
            12 => b"ARM64",
            _ => b"unknown",
        };
        text_line(output, "Architecture:", architecture);
        for (path, label) in [
            (b"WINDOWS/PRODUCTNAME".as_slice(), "Product Name:"),
            (b"WINDOWS/EDITIONID", "Edition ID:"),
            (b"WINDOWS/INSTALLATIONTYPE", "Installation Type:"),
            (b"WINDOWS/HAL", "HAL:"),
            (b"WINDOWS/PRODUCTTYPE", "Product Type:"),
            (b"WINDOWS/PRODUCTSUITE", "Product Suite:"),
        ] {
            optional(xml, image, path, label, output);
        }
        if xml.has_element(image, b"WINDOWS/LANGUAGES") {
            output.text(b"Languages:              ");
            for (name, text) in xml.child_elements(image, b"WINDOWS/LANGUAGES") {
                if name == b"LANGUAGE"
                    && let Some(text) = text
                {
                    output.field(String::new(), text, " ");
                }
            }
            output.text(b"\n");
            optional(
                xml,
                image,
                b"WINDOWS/LANGUAGES/DEFAULT",
                "Default Language:",
                output,
            );
        }
        optional(xml, image, b"WINDOWS/SYSTEMROOT", "System Root:", output);
        if xml.has_element(image, b"WINDOWS/VERSION") {
            for (path, label) in [
                (b"WINDOWS/VERSION/MAJOR".as_slice(), "Major Version:"),
                (b"WINDOWS/VERSION/MINOR", "Minor Version:"),
                (b"WINDOWS/VERSION/BUILD", "Build:"),
                (b"WINDOWS/VERSION/SPBUILD", "Service Pack Build:"),
                (b"WINDOWS/VERSION/SPLEVEL", "Service Pack Level:"),
            ] {
                numeric(xml, image, path, label, output);
            }
        }
    }
    optional(xml, image, b"FLAGS", "Flags:", output);
    text_line(
        output,
        "WIMBoot compatible:",
        if number(xml.get_property_bytes(image, b"WIMBOOT"), 10) == 0 {
            b"no"
        } else {
            b"yes"
        },
    );
    output.text(b"\n");
}
fn images_text(handle: &WimHandle, image: c_int) -> ImageOutput {
    let (heading, first, last) = if image == -1 {
        (
            "Available Images:\n".to_owned(),
            1,
            handle.header.image_count as i32,
        )
    } else if image >= 1 && image as u32 <= handle.header.image_count {
        (format!("Information for Image {image}\n"), image, image)
    } else {
        let mut output = ImageOutput::default();
        output.text(format!("wimlib_print_available_images(): Invalid image {image}").as_bytes());
        return output;
    };
    let mut output = ImageOutput::default();
    output.text(heading.as_bytes());
    output.text(&vec![b'-'; heading.len() - 1]);
    output.text(b"\n");
    for image in first..=last {
        image_text(&handle.xml, image, &mut output);
    }
    output
}
#[cfg(unix)]
unsafe fn emit(bytes: &[u8]) {
    // SAFETY: The caller keeps host stdout live; bytes remain readable during fwrite.
    unsafe {
        libc::fwrite(bytes.as_ptr().cast(), 1, bytes.len(), stdout);
    }
}
#[cfg(windows)]
unsafe fn emit(bytes: &[u8]) {
    // SAFETY: UTF-16 buffers and fixed format remain live for this CRT call.
    unsafe {
        emit_field(&PrintField {
            prefix: String::new(),
            value: bytes.to_vec(),
            suffix: "",
        });
    }
}
unsafe fn emit_field(field: &PrintField) {
    #[cfg(unix)]
    {
        // SAFETY: The borrowed stdout and all three byte slices remain live.
        unsafe {
            emit(field.prefix.as_bytes());
            emit(&field.value);
            emit(field.suffix.as_bytes());
        }
    }
    #[cfg(windows)]
    {
        let Ok(mut value) = wim_format::platform_text::wtf8_to_utf16(&field.value) else {
            return;
        };
        value.push(0);
        let format: Vec<u16> = format!("{}%ls{}", field.prefix, field.suffix)
            .encode_utf16()
            .chain(std::iter::once(0))
            .collect();
        // SAFETY: The format has one wide-string argument and both are terminated.
        unsafe {
            wprintf(format.as_ptr(), value.as_ptr());
        }
    }
}
unsafe fn emit_images(output: &ImageOutput) {
    for field in &output.fields {
        // SAFETY: Fields and host CRT stdout remain live during the call.
        unsafe {
            emit_field(field);
        }
    }
}
/// Print the actual stored header with original field labels and flag descriptions.
///
/// # Safety
/// `handle` must be live and readable; the host C stdout stream must remain live.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn wimlib_print_header(handle: *const WimHandle) {
    // SAFETY: The caller supplies a live readable handle when nonnull.
    if let Some(handle) = unsafe { handle.as_ref() } {
        // SAFETY: Caller guarantees stdout ownership; local bytes remain live.
        unsafe {
            emit(&header_text(&handle.header));
        }
    }
}
/// Print current image properties for one image or ALL_IMAGES (-1).
/// No error checking is performed on C stdout, matching the deprecated original API.
///
/// # Safety
/// `handle` must be live and readable; the host C stdout stream must remain live.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn wimlib_print_available_images(handle: *const WimHandle, image: c_int) {
    // SAFETY: The caller supplies a live readable handle when nonnull.
    if let Some(handle) = unsafe { handle.as_ref() } {
        // SAFETY: Caller guarantees stdout ownership; local bytes remain live.
        unsafe {
            emit_images(&images_text(handle, image));
        }
    }
}