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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! Native image export with independently owned, lazily decoded source snapshots.

use crate::engine::collections::FallibleCollections as _;
use crate::engine::collections::FallibleMap as _;
use crate::engine::collections::FallibleSet as _;
use crate::engine::{
    TChar,
    handles::{
        HandleImage, OwnedBlob, OwnedImage, OwnedResource, WimHandle, handle_mut, image_identity,
        image_metadata_bytes, image_metadata_hash, pending_metadata,
    },
};
use std::collections::HashMap;
use std::ffi::c_int;
use std::sync::Arc;
use wim_format::{ParseError, metadata::Metadata};

unsafe fn text(pointer: *const TChar) -> Result<Option<Vec<u8>>, ParseError> {
    if pointer.is_null() {
        return Ok(None);
    }
    #[cfg(not(windows))]
    {
        // SAFETY: Caller supplies readable NUL-terminated platform text.
        Ok(Some(
            unsafe { std::ffi::CStr::from_ptr(pointer) }
                .to_bytes()
                .to_vec(),
        ))
    }
    #[cfg(windows)]
    {
        let mut length = 0;
        // SAFETY: Caller supplies readable NUL-terminated platform text.
        while unsafe { *pointer.add(length) } != 0 {
            length += 1;
        }
        // SAFETY: Scan identified the readable UTF-16 slice.
        String::from_utf16(unsafe { std::slice::from_raw_parts(pointer, length) })
            .map(|s| Some(s.into_bytes()))
            .map_err(|_| ParseError::InvalidUtf16String)
    }
}
fn has_metadata(handle: &WimHandle) -> bool {
    handle.header.image_count == 0 || handle.images.len() == handle.header.image_count as usize
}
fn copy_bytes(bytes: &[u8]) -> Result<Vec<u8>, ParseError> {
    let mut copy = Vec::new();
    copy.try_reserve_exact(bytes.len())
        .map_err(|_| ParseError::Nomem)?;
    copy.extend_from_slice(bytes);
    Ok(copy)
}
fn metadata_descriptor(
    handle: &WimHandle,
    image: &HandleImage,
) -> Result<Option<OwnedResource>, ParseError> {
    match image {
        HandleImage::Source(index) => {
            let lookup = handle.lookup.as_ref().ok_or(ParseError::MetadataNotFound)?;
            let blob = lookup
                .metadata
                .get((*index - 1) as usize)
                .ok_or(ParseError::MetadataNotFound)?
                .clone();
            let resource = lookup
                .resources
                .get(blob.resource_index)
                .ok_or(ParseError::InvalidLookupTableEntry)?
                .clone();
            Ok(Some(OwnedResource {
                blob,
                resource,
                part: handle.header.part_number,
            }))
        }
        HandleImage::Empty(_) => Ok(None),
        HandleImage::Owned(image) => Ok(image.descriptor.clone()),
    }
}
fn contains_blob(handle: &WimHandle, hash: &[u8; 20]) -> bool {
    !handle.removed_blobs.contains(hash)
        && (handle.owned_blobs.contains_key(hash)
            || handle
                .lookup
                .as_ref()
                .is_some_and(|lookup| lookup.find(hash).is_some()))
}
fn clone_blob(
    handle: &WimHandle,
    hash: &[u8; 20],
    snapshot: &mut Option<Arc<crate::engine::backing::Backing>>,
) -> Result<OwnedBlob, ParseError> {
    if handle.removed_blobs.contains(hash) {
        return Err(ParseError::ResourceNotFound);
    }
    if let Some(blob) = handle.owned_blobs.get(hash) {
        let mut descriptor = blob.descriptor.clone();
        descriptor.blob.reference_count = 0;
        return Ok(OwnedBlob {
            bytes: blob.bytes.try_clone()?,
            backing: blob.backing.clone(),
            captured: blob.captured.clone(),
            descriptor,
        });
    }
    let lookup = handle.lookup.as_ref().ok_or(ParseError::ResourceNotFound)?;
    let mut blob = lookup
        .find(hash)
        .ok_or(ParseError::ResourceNotFound)?
        .clone();
    let resource = lookup
        .resources
        .get(blob.resource_index)
        .ok_or(ParseError::InvalidLookupTableEntry)?
        .clone();
    if snapshot.is_none() {
        *snapshot = Some(
            handle
                .backing
                .as_ref()
                .ok_or(ParseError::ResourceNotFound)?
                .clone(),
        );
    }
    blob.reference_count = 0;
    Ok(OwnedBlob {
        bytes: crate::engine::handles::OwnedBlobData::Decoded(Vec::new()),
        backing: snapshot.clone(),
        captured: None,
        descriptor: OwnedResource {
            blob,
            resource,
            part: handle.header.part_number,
        },
    })
}
fn export_images(
    source: &mut WimHandle,
    image: c_int,
    destination: &mut WimHandle,
    name: Option<&[u8]>,
    description: Option<&[u8]>,
    flags: c_int,
) -> Result<(), ParseError> {
    if !has_metadata(source) || !has_metadata(destination) {
        return Err(ParseError::MetadataNotFound);
    }
    let all = image == -1;
    if all && ((flags & 2 == 0 && name.is_some()) || (flags & 4 == 0 && description.is_some())) {
        return Err(ParseError::InvalidParam);
    }
    let mut indices = Vec::new();
    if all {
        indices
            .try_reserve_exact(source.images.len())
            .map_err(|_| ParseError::Nomem)?;
        indices.extend(1..=source.header.image_count);
    } else {
        if image < 1 || image as u32 > source.header.image_count {
            return Err(ParseError::InvalidImage);
        }
        indices.push(image as u32);
    }
    for index in &indices {
        let identity = image_identity(source, &source.images[(*index - 1) as usize]);
        if destination
            .images
            .iter()
            .any(|image| image_identity(destination, image) == identity)
        {
            return Err(ParseError::DuplicateExportedImage);
        }
    }
    #[cfg(unix)]
    {
        crate::engine::capture::checksum_pending(source)?;
        crate::engine::capture::checksum_pending(destination)?;
    }
    if flags & 2 == 0 {
        for index in &indices {
            let name = name.or_else(|| source.xml.name_bytes(*index as i32));
            if name.is_some_and(|name| destination.xml.name_in_use_bytes(name)) {
                return Err(ParseError::ImageNameCollision);
            }
        }
    }
    if destination
        .images
        .len()
        .checked_add(indices.len())
        .is_none_or(|count| count > 65535)
    {
        return Err(ParseError::ImageCount);
    }
    let mut staged_xml = destination
        .xml
        .select_images(&(1..=destination.header.image_count).collect::<Vec<_>>())?;
    let mut staged_images = Vec::new();
    staged_images
        .try_reserve_exact(indices.len())
        .map_err(|_| ParseError::Nomem)?;
    let mut increments = HashMap::<[u8; 20], u32>::new();
    let mut new_blobs = HashMap::new();
    let mut publication_order = Vec::new();
    let mut snapshot = None;
    let captured = crate::engine::lookup::captured_resources(source)?;
    for index in &indices {
        let current = &source.images[(*index - 1) as usize];
        let descriptor = metadata_descriptor(source, current)?;
        let identity = image_identity(source, current);
        let hash = image_metadata_hash(source, current)?;
        let pending = pending_metadata(current).cloned();
        let metadata_bytes = copy_bytes(&image_metadata_bytes(source, (*index - 1) as usize)?)?;
        let metadata = Metadata::parse(&metadata_bytes)?;
        for node in 0..metadata.nodes.len() {
            let entry = metadata
                .inode_entry(node)
                .ok_or(ParseError::InvalidMetadataResource)?;
            for stream in &entry.streams {
                if stream.hash == [0; 20] {
                    continue;
                }
                if !increments.contains_key(&stream.hash) {
                    increments.try_reserve(1).map_err(|_| ParseError::Nomem)?;
                }
                let increment = increments.entry(stream.hash).or_default();
                *increment = increment.wrapping_add(1);
                if destination.blob_index.owner(&stream.hash).is_none()
                    && !new_blobs.contains_key(&stream.hash)
                {
                    publication_order
                        .try_push(stream.hash)
                        .map_err(|_| ParseError::Nomem)?;
                    if !contains_blob(source, &stream.hash)
                        && captured.iter().any(|resource| resource.hash == stream.hash)
                    {
                        if flags & 8 != 0 {
                            source.blob_index.unlink(&stream.hash);
                        }
                        // Pending metadata retains the actual stream Arc, including
                        // its file path or inline payload, after the source is freed.
                        continue;
                    }
                    new_blobs.try_reserve(1).map_err(|_| ParseError::Nomem)?;
                    let blob = clone_blob(source, &stream.hash, &mut snapshot)?;
                    if flags & 8 != 0 {
                        source
                            .removed_blobs
                            .try_reserve(1)
                            .map_err(|_| ParseError::Nomem)?;
                        // Original GIFT unlinks source blobs immediately and does
                        // not restore them when a later XML/export step fails.
                        source
                            .removed_blobs
                            .try_insert(stream.hash)
                            .map_err(|_| ParseError::Nomem)?;
                        source.blob_index.unlink(&stream.hash);
                        source.owned_blobs.remove(&stream.hash);
                    }
                    new_blobs.insert(stream.hash, blob);
                }
            }
        }
        drop(metadata);
        staged_images.push(HandleImage::Owned(OwnedImage {
            metadata: metadata_bytes,
            hash,
            pending,
            descriptor,
            identity,
        }));
        let mut xml = source.xml.select_images(&[*index])?;
        let next_name = if flags & 2 != 0 {
            None
        } else {
            name.or_else(|| source.xml.name_bytes(*index as i32))
        };
        let next_description = if flags & 4 != 0 {
            None
        } else {
            description.or_else(|| source.xml.description_bytes(*index as i32))
        };
        xml.set_property_bytes(1, b"NAME", next_name)?;
        xml.set_property_bytes(1, b"DESCRIPTION", next_description)?;
        if flags & 16 != 0 {
            xml.set_property_bytes(1, b"WIMBOOT", Some(b"1"))?;
        }
        staged_xml.append_images(&xml, &[1])?;
    }
    let staged_xml = crate::engine::handles::own_xml(staged_xml)?;
    destination
        .images
        .try_reserve(staged_images.len())
        .map_err(|_| ParseError::Nomem)?;
    destination
        .image_owners
        .try_reserve(staged_images.len())
        .map_err(|_| ParseError::Nomem)?;
    destination
        .dirty_images
        .try_reserve(indices.len())
        .map_err(|_| ParseError::Nomem)?;
    destination
        .owned_blobs
        .try_reserve(new_blobs.len())
        .map_err(|_| ParseError::Nomem)?;
    if flags & 8 != 0 {
        source
            .removed_blobs
            .try_reserve(new_blobs.len())
            .map_err(|_| ParseError::Nomem)?;
    }
    let staged_index = destination.blob_index.try_clone()?;
    for hash in &publication_order {
        staged_index.insert(
            *hash,
            if new_blobs.contains_key(hash) {
                crate::engine::blob_index::BlobOwner::Owned
            } else {
                crate::engine::blob_index::BlobOwner::Captured
            },
        )?;
    }
    for &hash in &publication_order {
        let Some(mut blob) = new_blobs.remove(&hash) else {
            continue;
        };
        blob.descriptor.blob.reference_count = increments.remove(&hash).unwrap_or(0);
        if flags & 8 != 0 {
            source
                .removed_blobs
                .try_insert(hash)
                .map_err(|_| ParseError::Nomem)?;
            source.blob_index.unlink(&hash);
            source.owned_blobs.remove(&hash);
        }
        destination.removed_blobs.remove(&hash);
        destination
            .owned_blobs
            .try_insert_reserved(hash, blob)
            .map_err(|_| ParseError::Nomem)?;
    }
    for (hash, count) in increments {
        if let Some(blob) = destination.owned_blobs.get_mut(&hash) {
            blob.descriptor.blob.reference_count =
                blob.descriptor.blob.reference_count.wrapping_add(count);
        } else if let Some(lookup) = destination.lookup.as_mut()
            && let Some(blob) = lookup.blobs.iter_mut().find(|blob| blob.hash == hash)
        {
            blob.reference_count = blob.reference_count.wrapping_add(count);
        }
    }
    let original_count = destination.header.image_count;
    destination.xml = staged_xml;
    for index in &indices {
        let identity = image_identity(source, &source.images[*index as usize - 1]);
        if source.dirty_images.contains(&identity) {
            destination
                .dirty_images
                .try_insert(identity)
                .map_err(|_| ParseError::Nomem)?;
        }
    }
    let owners = indices
        .iter()
        .map(|index| source.image_owners[*index as usize - 1].clone());
    destination
        .image_owners
        .try_extend(owners)
        .map_err(|_| ParseError::Nomem)?;
    destination
        .images
        .try_extend(staged_images)
        .map_err(|_| ParseError::Nomem)?;
    destination.blob_index = staged_index;
    destination.header.image_count = destination.images.len() as u32;
    if source.header.flags & 0x80 != 0 {
        destination.header.flags |= 0x80;
    }
    if flags & 1 != 0 {
        for (position, index) in indices.iter().enumerate() {
            if !all || *index == source.header.boot_index {
                destination.header.boot_index = original_count + position as u32 + 1;
            }
        }
    }
    Ok(())
}

/// Export images between typed archive handles with optional UTF-8 properties.
pub(crate) fn export_archive(
    source: &mut WimHandle,
    image: c_int,
    destination: &mut WimHandle,
    name: Option<&str>,
    description: Option<&str>,
    flags: c_int,
) -> Result<(), ParseError> {
    export_archive_bytes(
        source,
        image,
        destination,
        name.map(str::as_bytes),
        description.map(str::as_bytes),
        flags,
    )
}

fn export_archive_bytes(
    source: &mut WimHandle,
    image: c_int,
    destination: &mut WimHandle,
    name: Option<&[u8]>,
    description: Option<&[u8]>,
    flags: c_int,
) -> Result<(), ParseError> {
    if flags & !31 != 0 {
        return Err(ParseError::InvalidParam);
    }
    export_images(
        source,
        image,
        destination,
        if flags & 2 != 0 { None } else { name },
        if flags & 4 != 0 { None } else { description },
        flags,
    )
}

/// Export one image or all images (-1) into an independently owned destination.
/// File payload decoding remains deferred until verification or writing.
///
/// # Safety
/// Nonnull handles must be live and exclusively accessed. Optional text pointers
/// must be readable NUL-terminated platform strings. Source and destination may
/// alias; duplicate-image validation rejects exports into the same handle.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn wimlib_export_image(
    source: *mut WimHandle,
    image: c_int,
    destination: *mut WimHandle,
    name: *const TChar,
    description: *const TChar,
    flags: c_int,
) -> c_int {
    if flags & !31 != 0 || source.is_null() || destination.is_null() {
        return 24;
    }
    if source == destination {
        // SAFETY: Nonnull handle validity is a caller requirement.
        let handle = unsafe { &*source };
        if !has_metadata(handle) {
            return 36;
        }
        if image == -1
            && ((flags & 2 == 0 && !name.is_null()) || (flags & 4 == 0 && !description.is_null()))
        {
            return 24;
        }
        if image == -1 && handle.images.is_empty() {
            return 0;
        }
        if image != -1 && (image < 1 || image as u32 > handle.header.image_count) {
            return 18;
        }
        return 87;
    }
    // SAFETY: Nonaliasing live handles are exclusively accessible by the caller.
    let source = unsafe { handle_mut(source) };
    // SAFETY: As above, source/destination pointers were checked distinct.
    let destination = unsafe { handle_mut(destination) };
    let (Some(source), Some(destination)) = (source, destination) else {
        return 24;
    };
    // Ignored text pointers need not be read when their suppressing flags are set.
    // SAFETY: Caller guarantees readable optional platform text.
    let name = if flags & 2 != 0 {
        None
    } else {
        match unsafe { text(name) } {
            Ok(v) => v,
            Err(e) => return e as c_int,
        }
    };
    // SAFETY: Same text requirements as above.
    let description = if flags & 4 != 0 {
        None
    } else {
        match unsafe { text(description) } {
            Ok(v) => v,
            Err(e) => return e as c_int,
        }
    };
    match export_archive_bytes(
        source,
        image,
        destination,
        name.as_deref(),
        description.as_deref(),
        flags,
    ) {
        Ok(()) => 0,
        Err(error) => error as c_int,
    }
}