use crate::engine::blob_index::BlobOwner;
use crate::engine::collections::FallibleSet as _;
use crate::engine::handles::{HandleImage, WimHandle, image_metadata_hash};
use std::ffi::{c_int, c_void};
use wim_format::ParseError;
use wim_format::lookup::{LookupBlob, LookupResource};
#[repr(C)]
#[derive(Debug, Default)]
pub struct WimResourceEntry {
pub uncompressed_size: u64,
pub compressed_size: u64,
pub offset: u64,
pub sha1_hash: [u8; 20],
pub part_number: u32,
pub reference_count: u32,
pub flags: u32,
pub raw_resource_offset_in_wim: u64,
pub raw_resource_compressed_size: u64,
pub raw_resource_uncompressed_size: u64,
pub reserved: [u64; 1],
}
pub(crate) fn resource_entry(
blob: &LookupBlob,
resource: &LookupResource,
part: u16,
) -> WimResourceEntry {
let raw = resource.header;
WimResourceEntry {
uncompressed_size: blob.size,
compressed_size: if resource.solid { 0 } else { raw.size_in_wim },
offset: if resource.solid {
blob.offset
} else {
raw.offset_in_wim
},
sha1_hash: blob.hash,
part_number: part.into(),
reference_count: blob.reference_count,
flags: u32::from(raw.flags & 4 != 0)
| (u32::from(blob.flags & 2 != 0) << 1)
| (u32::from(raw.flags & 1 != 0) << 2)
| (u32::from(raw.flags & 8 != 0) << 3)
| (u32::from(resource.solid) << 5),
raw_resource_offset_in_wim: raw.offset_in_wim,
raw_resource_compressed_size: raw.size_in_wim,
raw_resource_uncompressed_size: resource.uncompressed_size,
reserved: [0],
}
}
pub(crate) struct CapturedResource {
pub hash: [u8; 20],
pub stream: std::sync::Arc<crate::engine::capture::CapturedStream>,
pub references: u32,
pub image_index: usize,
}
impl CapturedResource {
pub fn entry(&self) -> WimResourceEntry {
WimResourceEntry {
uncompressed_size: self.stream.size,
sha1_hash: self.hash,
reference_count: self.references,
..WimResourceEntry::default()
}
}
}
pub(crate) fn prepare_capture_index(
handle: &WimHandle,
replaced: usize,
replacement: Option<&crate::engine::capture::CapturePlan>,
) -> Result<crate::engine::blob_index::BlobIndex, ParseError> {
let index = handle.blob_index.try_clone()?;
let mut live = hashbrown::HashSet::new();
for (image_index, image) in handle.images.iter().enumerate() {
let retained;
let plan = if image_index == replaced {
replacement
} else {
retained = crate::engine::handles::image_capture_plan(image)?;
retained.as_ref()
};
let Some(plan) = plan else {
continue;
};
for binding in &plan.bindings {
let node = plan
.tree
.nodes
.get(binding.node)
.ok_or(ParseError::InvalidMetadataResource)?;
let hash = if binding.slot == 0 {
node.main_hash
} else {
node.extra_streams
.get(binding.slot - 1)
.ok_or(ParseError::InvalidMetadataResource)?
.hash
};
if hash != [0; 20] {
live.try_insert(hash).map_err(|_| ParseError::Nomem)?;
if image_index == replaced {
index.insert(hash, BlobOwner::Captured)?;
}
}
}
}
let mut cursor = crate::engine::blob_index::BlobCursor::default();
while let Some((hash, owner)) = index.next(&mut cursor) {
if owner == BlobOwner::Captured && !live.contains(&hash) {
if handle.owned_blobs.contains_key(&hash) {
index.set_owner(&hash, BlobOwner::Owned);
} else if handle
.lookup
.as_ref()
.is_some_and(|table| table.find(&hash).is_some())
{
index.set_owner(&hash, BlobOwner::Stored);
} else {
index.unlink(&hash);
}
}
}
Ok(index)
}
pub(crate) fn captured_resources(handle: &WimHandle) -> Result<Vec<CapturedResource>, ParseError> {
let mut resources: Vec<CapturedResource> = Vec::new();
let mut positions = std::collections::HashMap::new();
for (image_index, image) in handle.images.iter().enumerate() {
let Some(plan) = crate::engine::handles::image_capture_plan(image)? else {
continue;
};
for binding in plan.bindings {
let node = plan
.tree
.nodes
.get(binding.node)
.ok_or(ParseError::InvalidMetadataResource)?;
let hash = if binding.slot == 0 {
node.main_hash
} else {
node.extra_streams
.get(binding.slot - 1)
.ok_or(ParseError::InvalidMetadataResource)?
.hash
};
let key = (
hash,
if hash == [0; 20] {
std::sync::Arc::as_ptr(&binding.stream) as usize
} else {
0
},
);
if let Some(&index) = positions.get(&key) {
let existing: &mut CapturedResource = &mut resources[index];
if hash != [0; 20]
&& let (
crate::engine::capture::CapturedSource::File(current),
crate::engine::capture::CapturedSource::File(candidate),
) = (&existing.stream.source, &binding.stream.source)
&& candidate < current
{
existing.stream = binding.stream.clone();
}
existing.references = existing
.references
.checked_add(1)
.ok_or(ParseError::InvalidMetadataResource)?;
} else {
resources.try_reserve(1).map_err(|_| ParseError::Nomem)?;
positions.try_reserve(1).map_err(|_| ParseError::Nomem)?;
positions.insert(key, resources.len());
resources.push(CapturedResource {
hash,
stream: binding.stream,
references: 1,
image_index,
});
}
}
}
Ok(resources)
}
pub type LookupCallback = unsafe extern "C" fn(*const WimResourceEntry, *mut c_void) -> c_int;
fn metadata_entry(handle: &WimHandle, index: usize) -> Result<WimResourceEntry, ParseError> {
let image = handle
.images
.get(index)
.ok_or(ParseError::MetadataNotFound)?;
Ok(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)?;
resource_entry(
blob,
&lookup.resources[blob.resource_index],
handle.header.part_number,
)
}
HandleImage::Owned(owned) if owned.descriptor.is_some() => {
let descriptor = owned.descriptor.as_ref().expect("matched descriptor");
resource_entry(&descriptor.blob, &descriptor.resource, descriptor.part)
}
HandleImage::Empty(_) | HandleImage::Owned(_) => WimResourceEntry {
sha1_hash: image_metadata_hash(handle, image)?,
reference_count: 1,
flags: 2,
..WimResourceEntry::default()
},
})
}
fn hashed_entry(
handle: &WimHandle,
hash: &[u8; 20],
owner: crate::engine::blob_index::BlobOwner,
captured: &[CapturedResource],
) -> Result<Option<WimResourceEntry>, ParseError> {
if handle.removed_blobs.contains(hash) {
return Ok(None);
}
Ok(Some(match owner {
BlobOwner::Stored => {
let table = handle.lookup.as_ref().ok_or(ParseError::ResourceNotFound)?;
let blob = table.find(hash).ok_or(ParseError::ResourceNotFound)?;
resource_entry(
blob,
&table.resources[blob.resource_index],
handle.header.part_number,
)
}
BlobOwner::Owned => {
let blob = handle
.owned_blobs
.get(hash)
.ok_or(ParseError::ResourceNotFound)?;
if blob.captured.is_some() || blob.bytes.is_memory() {
WimResourceEntry {
sha1_hash: *hash,
uncompressed_size: blob
.captured
.as_ref()
.map_or(blob.bytes.len() as u64, |stream| stream.size),
reference_count: blob.descriptor.blob.reference_count,
..WimResourceEntry::default()
}
} else {
let descriptor = &blob.descriptor;
resource_entry(&descriptor.blob, &descriptor.resource, descriptor.part)
}
}
BlobOwner::Captured => captured
.iter()
.find(|resource| resource.hash == *hash)
.ok_or(ParseError::ResourceNotFound)?
.entry(),
}))
}
unsafe fn call(
callback: Option<LookupCallback>,
context: *mut c_void,
entry: &WimResourceEntry,
) -> Result<(), c_int> {
let callback = callback.ok_or(24)?;
let result = unsafe { callback(entry, context) };
if result == 0 { Ok(()) } else { Err(result) }
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn wimlib_iterate_lookup_table(
handle: *mut WimHandle,
flags: c_int,
callback: Option<LookupCallback>,
context: *mut c_void,
) -> c_int {
if flags != 0 {
return 24;
}
let images = match unsafe { handle.as_ref() } {
Some(h) => h.images.len(),
None => return 24,
};
let mut captured = None;
for index in 0..images {
let entry = {
let h = unsafe { &*handle };
match metadata_entry(h, index) {
Ok(entry) => entry,
Err(error) => return error as c_int,
}
};
if let Err(result) = unsafe { call(callback, context, &entry) } {
return result;
}
if captured.is_none() {
captured = Some(match captured_resources(unsafe { &*handle }) {
Ok(resources) => resources,
Err(error) => return error as c_int,
});
}
for resource in captured
.as_ref()
.expect("initialized resources")
.iter()
.filter(|resource| resource.image_index == index && resource.hash == [0; 20])
{
let entry = resource.entry();
if let Err(result) = unsafe { call(callback, context, &entry) } {
return result;
}
}
}
if unsafe { &*handle }.blob_index.is_empty() {
return 0;
}
let captured = match captured {
Some(resources) => resources,
None => match captured_resources(unsafe { &*handle }) {
Ok(resources) => resources,
Err(error) => return error as c_int,
},
};
let mut cursor = crate::engine::blob_index::BlobCursor::default();
loop {
let entry = {
let h = unsafe { &*handle };
let Some((hash, owner)) = h.blob_index.next(&mut cursor) else {
break;
};
match hashed_entry(h, &hash, owner, &captured) {
Ok(Some(entry)) => entry,
Ok(None) => continue,
Err(error) => return error as c_int,
}
};
if let Err(result) = unsafe { call(callback, context, &entry) } {
return result;
}
}
0
}