4 * A blob table maps SHA-1 message digests to "blobs", which are nonempty
5 * sequences of binary data. Within a WIM file, blobs are single-instanced.
7 * This file also contains code to read and write the corresponding on-disk
8 * representation of this table in the WIM file format.
12 * Copyright (C) 2012, 2013, 2014, 2015 Eric Biggers
14 * This file is free software; you can redistribute it and/or modify it under
15 * the terms of the GNU Lesser General Public License as published by the Free
16 * Software Foundation; either version 3 of the License, or (at your option) any
19 * This file is distributed in the hope that it will be useful, but WITHOUT
20 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
24 * You should have received a copy of the GNU Lesser General Public License
25 * along with this file; if not, see http://www.gnu.org/licenses/.
34 #include <unistd.h> /* for unlink() */
36 #include "wimlib/assert.h"
37 #include "wimlib/blob_table.h"
38 #include "wimlib/encoding.h"
39 #include "wimlib/endianness.h"
40 #include "wimlib/error.h"
41 #include "wimlib/metadata.h"
42 #include "wimlib/ntfs_3g.h"
43 #include "wimlib/resource.h"
44 #include "wimlib/unaligned.h"
45 #include "wimlib/util.h"
46 #include "wimlib/write.h"
48 /* A hash table mapping SHA-1 message digests to blob descriptors */
50 struct hlist_head *array;
56 new_blob_table(size_t capacity)
58 struct blob_table *table;
59 struct hlist_head *array;
61 table = MALLOC(sizeof(struct blob_table));
65 array = CALLOC(capacity, sizeof(array[0]));
72 table->capacity = capacity;
77 ERROR("Failed to allocate memory for blob table "
78 "with capacity %zu", capacity);
83 do_free_blob_descriptor(struct blob_descriptor *blob, void *_ignore)
85 free_blob_descriptor(blob);
90 free_blob_table(struct blob_table *table)
93 for_blob_in_table(table, do_free_blob_descriptor, NULL);
99 struct blob_descriptor *
100 new_blob_descriptor(void)
102 BUILD_BUG_ON(BLOB_NONEXISTENT != 0);
103 return CALLOC(1, sizeof(struct blob_descriptor));
106 struct blob_descriptor *
107 clone_blob_descriptor(const struct blob_descriptor *old)
109 struct blob_descriptor *new;
111 new = memdup(old, sizeof(struct blob_descriptor));
115 switch (new->blob_location) {
117 list_add(&new->rdesc_node, &new->rdesc->blob_list);
120 case BLOB_IN_FILE_ON_DISK:
122 case BLOB_IN_WINNT_FILE_ON_DISK:
123 case BLOB_WIN32_ENCRYPTED:
126 case BLOB_IN_STAGING_FILE:
127 BUILD_BUG_ON((void*)&old->file_on_disk !=
128 (void*)&old->staging_file_name);
130 new->file_on_disk = TSTRDUP(old->file_on_disk);
131 if (new->file_on_disk == NULL)
134 case BLOB_IN_ATTACHED_BUFFER:
135 new->attached_buffer = memdup(old->attached_buffer, old->size);
136 if (new->attached_buffer == NULL)
140 case BLOB_IN_NTFS_VOLUME:
142 new->ntfs_loc = memdup(old->ntfs_loc,
143 sizeof(struct ntfs_location));
144 if (new->ntfs_loc == NULL)
146 new->ntfs_loc->path = STRDUP(old->ntfs_loc->path);
147 new->ntfs_loc->attr_name = NULL;
148 if (new->ntfs_loc->path == NULL)
150 if (new->ntfs_loc->attr_name_nchars != 0) {
151 new->ntfs_loc->attr_name =
152 utf16le_dup(old->ntfs_loc->attr_name);
153 if (new->ntfs_loc->attr_name == NULL)
165 free_blob_descriptor(new);
170 blob_release_location(struct blob_descriptor *blob)
172 switch (blob->blob_location) {
174 list_del(&blob->rdesc_node);
175 if (list_empty(&blob->rdesc->blob_list))
178 case BLOB_IN_FILE_ON_DISK:
180 case BLOB_IN_WINNT_FILE_ON_DISK:
181 case BLOB_WIN32_ENCRYPTED:
184 case BLOB_IN_STAGING_FILE:
185 BUILD_BUG_ON((void*)&blob->file_on_disk !=
186 (void*)&blob->staging_file_name);
188 case BLOB_IN_ATTACHED_BUFFER:
189 BUILD_BUG_ON((void*)&blob->file_on_disk !=
190 (void*)&blob->attached_buffer);
191 FREE(blob->file_on_disk);
194 case BLOB_IN_NTFS_VOLUME:
195 if (blob->ntfs_loc) {
196 FREE(blob->ntfs_loc->path);
197 FREE(blob->ntfs_loc->attr_name);
198 FREE(blob->ntfs_loc);
208 free_blob_descriptor(struct blob_descriptor *blob)
211 blob_release_location(blob);
216 /* Should this blob be retained even if it has no references? */
218 should_retain_blob(const struct blob_descriptor *blob)
220 return blob->blob_location == BLOB_IN_WIM;
224 finalize_blob(struct blob_descriptor *blob)
226 if (!should_retain_blob(blob))
227 free_blob_descriptor(blob);
231 * Decrements the reference count of the specified blob, which must be either
232 * (a) unhashed, or (b) inserted in the specified blob table.
234 * If the blob's reference count reaches 0, we may unlink it from @table and
235 * free it. However, we retain blobs with 0 reference count that originated
236 * from WIM files (BLOB_IN_WIM). We do this for two reasons:
238 * 1. This prevents information about valid blobs in a WIM file --- blobs which
239 * will continue to be present after appending to the WIM file --- from being
240 * lost merely because we dropped all references to them.
242 * 2. Blob reference counts we read from WIM files can't be trusted. It's
243 * possible that a WIM has reference counts that are too low; WIMGAPI
244 * sometimes creates WIMs where this is the case. It's also possible that
245 * blobs have been referenced from an external WIM; those blobs can
246 * potentially have any reference count at all, either lower or higher than
247 * would be expected for this WIM ("this WIM" meaning the owner of @table) if
248 * it were a standalone WIM.
250 * So we can't take the reference counts too seriously. But at least, we do
251 * recalculate by default when writing a new WIM file.
254 blob_decrement_refcnt(struct blob_descriptor *blob, struct blob_table *table)
256 blob_subtract_refcnt(blob, table, 1);
260 blob_subtract_refcnt(struct blob_descriptor *blob, struct blob_table *table,
263 if (unlikely(blob->refcnt < count)) {
264 blob->refcnt = 0; /* See comment above */
268 blob->refcnt -= count;
270 if (blob->refcnt != 0)
273 if (blob->unhashed) {
274 list_del(&blob->unhashed_list);
276 /* If the blob has been extracted to a staging file for a FUSE
277 * mount, unlink the staging file. (Note that there still may
278 * be open file descriptors to it.) */
279 if (blob->blob_location == BLOB_IN_STAGING_FILE)
280 unlinkat(blob->staging_dir_fd,
281 blob->staging_file_name, 0);
284 if (!should_retain_blob(blob))
285 blob_table_unlink(table, blob);
288 /* If FUSE mounts are enabled, then don't actually free the blob
289 * descriptor until the last file descriptor to it has been closed. */
291 if (blob->num_opened_fds == 0)
298 blob_decrement_num_opened_fds(struct blob_descriptor *blob)
300 wimlib_assert(blob->num_opened_fds != 0);
302 if (--blob->num_opened_fds == 0 && blob->refcnt == 0)
308 blob_table_insert_raw(struct blob_table *table, struct blob_descriptor *blob)
310 size_t i = blob->hash_short % table->capacity;
312 hlist_add_head(&blob->hash_list, &table->array[i]);
316 enlarge_blob_table(struct blob_table *table)
318 size_t old_capacity, new_capacity;
319 struct hlist_head *old_array, *new_array;
320 struct blob_descriptor *blob;
321 struct hlist_node *tmp;
324 old_capacity = table->capacity;
325 new_capacity = old_capacity * 2;
326 new_array = CALLOC(new_capacity, sizeof(struct hlist_head));
327 if (new_array == NULL)
329 old_array = table->array;
330 table->array = new_array;
331 table->capacity = new_capacity;
333 for (i = 0; i < old_capacity; i++) {
334 hlist_for_each_entry_safe(blob, tmp, &old_array[i], hash_list) {
335 hlist_del(&blob->hash_list);
336 blob_table_insert_raw(table, blob);
342 /* Insert a blob descriptor into the blob table. */
344 blob_table_insert(struct blob_table *table, struct blob_descriptor *blob)
346 blob_table_insert_raw(table, blob);
347 if (++table->num_blobs > table->capacity)
348 enlarge_blob_table(table);
351 /* Unlinks a blob descriptor from the blob table; does not free it. */
353 blob_table_unlink(struct blob_table *table, struct blob_descriptor *blob)
355 wimlib_assert(!blob->unhashed);
356 wimlib_assert(table->num_blobs != 0);
358 hlist_del(&blob->hash_list);
362 /* Given a SHA-1 message digest, return the corresponding blob descriptor from
363 * the specified blob table, or NULL if there is none. */
364 struct blob_descriptor *
365 lookup_blob(const struct blob_table *table, const u8 *hash)
368 struct blob_descriptor *blob;
370 i = load_size_t_unaligned(hash) % table->capacity;
371 hlist_for_each_entry(blob, &table->array[i], hash_list)
372 if (hashes_equal(hash, blob->hash))
377 /* Call a function on all blob descriptors in the specified blob table. Stop
378 * early and return nonzero if any call to the function returns nonzero. */
380 for_blob_in_table(struct blob_table *table,
381 int (*visitor)(struct blob_descriptor *, void *), void *arg)
383 struct blob_descriptor *blob;
384 struct hlist_node *tmp;
387 for (size_t i = 0; i < table->capacity; i++) {
388 hlist_for_each_entry_safe(blob, tmp, &table->array[i],
391 ret = visitor(blob, arg);
400 * This is a qsort() callback that sorts blobs into an order optimized for
401 * reading. Sorting is done primarily by blob location, then secondarily by a
402 * location-dependent order. For example, blobs in WIM resources are sorted
403 * such that the underlying WIM files will be read sequentially. This is
404 * especially important for WIM files containing solid resources.
407 cmp_blobs_by_sequential_order(const void *p1, const void *p2)
409 const struct blob_descriptor *blob1, *blob2;
411 WIMStruct *wim1, *wim2;
413 blob1 = *(const struct blob_descriptor**)p1;
414 blob2 = *(const struct blob_descriptor**)p2;
416 v = (int)blob1->blob_location - (int)blob2->blob_location;
418 /* Different resource locations? */
422 switch (blob1->blob_location) {
424 wim1 = blob1->rdesc->wim;
425 wim2 = blob2->rdesc->wim;
427 /* Different (possibly split) WIMs? */
429 v = memcmp(wim1->hdr.guid, wim2->hdr.guid, WIM_GUID_LEN);
434 /* Different part numbers in the same WIM? */
435 v = (int)wim1->hdr.part_number - (int)wim2->hdr.part_number;
439 if (blob1->rdesc->offset_in_wim != blob2->rdesc->offset_in_wim)
440 return cmp_u64(blob1->rdesc->offset_in_wim,
441 blob2->rdesc->offset_in_wim);
443 return cmp_u64(blob1->offset_in_res, blob2->offset_in_res);
445 case BLOB_IN_FILE_ON_DISK:
447 case BLOB_IN_STAGING_FILE:
450 case BLOB_IN_WINNT_FILE_ON_DISK:
451 case BLOB_WIN32_ENCRYPTED:
453 /* Compare files by path: just a heuristic that will place files
454 * in the same directory next to each other. */
455 return tstrcmp(blob1->file_on_disk, blob2->file_on_disk);
457 case BLOB_IN_NTFS_VOLUME:
458 return tstrcmp(blob1->ntfs_loc->path, blob2->ntfs_loc->path);
461 /* No additional sorting order defined for this resource
462 * location (e.g. BLOB_IN_ATTACHED_BUFFER); simply compare
463 * everything equal to each other. */
469 sort_blob_list(struct list_head *blob_list, size_t list_head_offset,
470 int (*compar)(const void *, const void*))
472 struct list_head *cur;
473 struct blob_descriptor **array;
476 size_t num_blobs = 0;
478 list_for_each(cur, blob_list)
484 array_size = num_blobs * sizeof(array[0]);
485 array = MALLOC(array_size);
487 return WIMLIB_ERR_NOMEM;
489 cur = blob_list->next;
490 for (i = 0; i < num_blobs; i++) {
491 array[i] = (struct blob_descriptor*)((u8*)cur - list_head_offset);
495 qsort(array, num_blobs, sizeof(array[0]), compar);
497 INIT_LIST_HEAD(blob_list);
498 for (i = 0; i < num_blobs; i++) {
499 list_add_tail((struct list_head*)
500 ((u8*)array[i] + list_head_offset), blob_list);
506 /* Sort the specified list of blobs in an order optimized for sequential
509 sort_blob_list_by_sequential_order(struct list_head *blob_list,
510 size_t list_head_offset)
512 return sort_blob_list(blob_list, list_head_offset,
513 cmp_blobs_by_sequential_order);
517 add_blob_to_array(struct blob_descriptor *blob, void *_pp)
519 struct blob_descriptor ***pp = _pp;
524 /* Iterate through the blob descriptors in the specified blob table in an order
525 * optimized for sequential reading. */
527 for_blob_in_table_sorted_by_sequential_order(struct blob_table *table,
528 int (*visitor)(struct blob_descriptor *, void *),
531 struct blob_descriptor **blob_array, **p;
532 size_t num_blobs = table->num_blobs;
535 blob_array = MALLOC(num_blobs * sizeof(blob_array[0]));
537 return WIMLIB_ERR_NOMEM;
539 for_blob_in_table(table, add_blob_to_array, &p);
541 wimlib_assert(p == blob_array + num_blobs);
543 qsort(blob_array, num_blobs, sizeof(blob_array[0]),
544 cmp_blobs_by_sequential_order);
546 for (size_t i = 0; i < num_blobs; i++) {
547 ret = visitor(blob_array[i], arg);
555 /* On-disk format of a blob descriptor in a WIM file.
557 * Note: if the WIM file contains solid resource(s), then this structure is
558 * sometimes overloaded to describe a "resource" rather than a "blob". See the
559 * code for details. */
560 struct blob_descriptor_disk {
562 /* Size, offset, and flags of the blob. */
563 struct wim_reshdr_disk reshdr;
565 /* Which part of the split WIM this blob is in; indexed from 1. */
568 /* Reference count of this blob over all WIM images. (But see comment
569 * above blob_decrement_refcnt().) */
572 /* SHA-1 message digest of the uncompressed data of this blob, or all
573 * zeroes if this blob is of zero length. */
574 u8 hash[SHA1_HASH_SIZE];
577 /* Given a nonempty run of consecutive blob descriptors with the SOLID flag set,
578 * count how many specify resources (as opposed to blobs within those
581 * Returns the resulting count. */
583 count_solid_resources(const struct blob_descriptor_disk *entries, size_t max)
587 struct wim_reshdr reshdr;
589 get_wim_reshdr(&(entries++)->reshdr, &reshdr);
591 if (!(reshdr.flags & WIM_RESHDR_FLAG_SOLID)) {
592 /* Run was terminated by a stand-alone blob entry. */
596 if (reshdr.uncompressed_size == SOLID_RESOURCE_MAGIC_NUMBER) {
597 /* This is a resource entry. */
605 * Given a run of consecutive blob descriptors with the SOLID flag set and
606 * having @num_rdescs resource entries, load resource information from them into
607 * the resource descriptors in the @rdescs array.
609 * Returns 0 on success, or a nonzero error code on failure.
612 do_load_solid_info(WIMStruct *wim, struct wim_resource_descriptor **rdescs,
614 const struct blob_descriptor_disk *entries)
616 for (size_t i = 0; i < num_rdescs; i++) {
617 struct wim_reshdr reshdr;
618 struct alt_chunk_table_header_disk hdr;
619 struct wim_resource_descriptor *rdesc;
622 /* Advance to next resource entry. */
625 get_wim_reshdr(&(entries++)->reshdr, &reshdr);
626 } while (reshdr.uncompressed_size != SOLID_RESOURCE_MAGIC_NUMBER);
630 wim_res_hdr_to_desc(&reshdr, wim, rdesc);
632 /* For solid resources, the uncompressed size, compression type,
633 * and chunk size are stored in the resource itself, not in the
636 ret = full_pread(&wim->in_fd, &hdr,
637 sizeof(hdr), reshdr.offset_in_wim);
639 ERROR("Failed to read header of solid resource "
640 "(offset_in_wim=%"PRIu64")",
641 reshdr.offset_in_wim);
645 rdesc->uncompressed_size = le64_to_cpu(hdr.res_usize);
647 /* Compression format numbers must be the same as in
648 * WIMGAPI to be compatible here. */
649 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_NONE != 0);
650 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_XPRESS != 1);
651 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_LZX != 2);
652 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_LZMS != 3);
653 rdesc->compression_type = le32_to_cpu(hdr.compression_format);
655 rdesc->chunk_size = le32_to_cpu(hdr.chunk_size);
657 DEBUG("Solid resource %zu/%zu: %"PRIu64" => %"PRIu64" "
658 "(%"TS"/%"PRIu32") @ +%"PRIu64"",
660 rdesc->uncompressed_size,
662 wimlib_get_compression_type_string(rdesc->compression_type),
664 rdesc->offset_in_wim);
670 * Given a nonempty run of consecutive blob descriptors with the SOLID flag set,
671 * allocate a 'struct wim_resource_descriptor' for each resource within that
674 * Returns 0 on success, or a nonzero error code on failure.
675 * Returns the pointers and count in *rdescs_ret and *num_rdescs_ret.
678 load_solid_info(WIMStruct *wim,
679 const struct blob_descriptor_disk *entries,
680 size_t num_remaining_entries,
681 struct wim_resource_descriptor ***rdescs_ret,
682 size_t *num_rdescs_ret)
685 struct wim_resource_descriptor **rdescs;
689 num_rdescs = count_solid_resources(entries, num_remaining_entries);
690 rdescs = CALLOC(num_rdescs, sizeof(rdescs[0]));
692 return WIMLIB_ERR_NOMEM;
694 for (i = 0; i < num_rdescs; i++) {
695 rdescs[i] = MALLOC(sizeof(struct wim_resource_descriptor));
697 ret = WIMLIB_ERR_NOMEM;
698 goto out_free_rdescs;
702 ret = do_load_solid_info(wim, rdescs, num_rdescs, entries);
704 goto out_free_rdescs;
706 *rdescs_ret = rdescs;
707 *num_rdescs_ret = num_rdescs;
711 for (i = 0; i < num_rdescs; i++)
717 /* Given a 'struct blob_descriptor' allocated for an on-disk blob descriptor
718 * with the SOLID flag set, try to assign it to resource in the current solid
721 assign_blob_to_solid_resource(const struct wim_reshdr *reshdr,
722 struct blob_descriptor *blob,
723 struct wim_resource_descriptor **rdescs,
726 u64 offset = reshdr->offset_in_wim;
728 /* XXX: This linear search will be slow in the degenerate case where the
729 * number of solid resources in the run is huge. */
730 blob->size = reshdr->size_in_wim;
731 for (size_t i = 0; i < num_rdescs; i++) {
732 if (offset + blob->size <= rdescs[i]->uncompressed_size) {
733 blob_set_is_located_in_wim_resource(blob, rdescs[i], offset);
736 offset -= rdescs[i]->uncompressed_size;
738 ERROR("blob could not be assigned to a solid resource");
739 return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
743 free_solid_rdescs(struct wim_resource_descriptor **rdescs, size_t num_rdescs)
746 for (size_t i = 0; i < num_rdescs; i++)
747 if (list_empty(&rdescs[i]->blob_list))
754 cmp_blobs_by_offset_in_res(const void *p1, const void *p2)
756 const struct blob_descriptor *blob1, *blob2;
758 blob1 = *(const struct blob_descriptor**)p1;
759 blob2 = *(const struct blob_descriptor**)p2;
761 return cmp_u64(blob1->offset_in_res, blob2->offset_in_res);
764 /* Validate the size and location of a WIM resource. */
766 validate_resource(struct wim_resource_descriptor *rdesc)
768 struct blob_descriptor *blob;
770 u64 expected_next_offset;
773 /* Verify that the resource itself has a valid offset and size. */
774 if (rdesc->offset_in_wim + rdesc->size_in_wim < rdesc->size_in_wim)
775 goto invalid_due_to_overflow;
777 /* Verify that each blob in the resource has a valid offset and size.
779 expected_next_offset = 0;
780 out_of_order = false;
781 list_for_each_entry(blob, &rdesc->blob_list, rdesc_node) {
782 if (blob->offset_in_res + blob->size < blob->size ||
783 blob->offset_in_res + blob->size > rdesc->uncompressed_size)
784 goto invalid_due_to_overflow;
786 if (blob->offset_in_res >= expected_next_offset)
787 expected_next_offset = blob->offset_in_res + blob->size;
792 /* If the blobs were not located at strictly increasing positions (not
793 * allowing for overlap), sort them. Then make sure that none overlap.
796 ret = sort_blob_list(&rdesc->blob_list,
797 offsetof(struct blob_descriptor,
799 cmp_blobs_by_offset_in_res);
803 expected_next_offset = 0;
804 list_for_each_entry(blob, &rdesc->blob_list, rdesc_node) {
805 if (blob->offset_in_res >= expected_next_offset)
806 expected_next_offset = blob->offset_in_res + blob->size;
808 goto invalid_due_to_overlap;
814 invalid_due_to_overflow:
815 ERROR("Invalid blob table (offset overflow)");
816 return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
818 invalid_due_to_overlap:
819 ERROR("Invalid blob table (blobs in solid resource overlap)");
820 return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
824 finish_solid_rdescs(struct wim_resource_descriptor **rdescs, size_t num_rdescs)
827 for (size_t i = 0; i < num_rdescs; i++) {
828 ret = validate_resource(rdescs[i]);
832 free_solid_rdescs(rdescs, num_rdescs);
837 * read_blob_table() -
839 * Read the blob table from a WIM file. Usually, each entry in this table
840 * describes a "blob", or equivalently a "resource", that the WIM file contains,
841 * along with its location and SHA-1 message digest. Descriptors for
842 * non-metadata blobs will be saved in the in-memory blob table
843 * (wim->blob_table), whereas descriptors for metadata blobs will be saved in a
844 * special location per-image (the wim->image_metadata array).
846 * However, in WIM_VERSION_SOLID (3584) WIMs, a resource may contain multiple
847 * blobs that are compressed together. Such a resource is called a "solid
848 * resource". Solid resources are still described in the on-disk "blob table",
849 * although the format is not the most logical. A consecutive sequence of
850 * entries that all have flag WIM_RESHDR_FLAG_SOLID (0x10) set is a "solid run".
851 * A solid run describes a set of solid resources, each of which contains a set
852 * of blobs. In a solid run, a 'struct wim_reshdr_disk' with 'uncompressed_size
853 * = SOLID_RESOURCE_MAGIC_NUMBER (0x100000000)' specifies a solid resource,
854 * whereas any other 'struct wim_reshdr_disk' specifies a blob within a solid
855 * resource. There are some oddities in how we need to determine which solid
856 * resource a blob is actually in; see the code for details.
858 * Possible return values:
859 * WIMLIB_ERR_SUCCESS (0)
860 * WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY
863 * Or an error code caused by failure to read the blob table from the WIM
867 read_blob_table(WIMStruct *wim)
872 struct blob_table *table = NULL;
873 struct blob_descriptor *cur_blob = NULL;
874 size_t num_duplicate_blobs = 0;
875 size_t num_wrong_part_blobs = 0;
877 struct wim_resource_descriptor **cur_solid_rdescs = NULL;
878 size_t cur_num_solid_rdescs = 0;
880 DEBUG("Reading blob table.");
882 /* Calculate the number of entries in the blob table. */
883 num_entries = wim->hdr.blob_table_reshdr.uncompressed_size /
884 sizeof(struct blob_descriptor_disk);
886 /* Read the blob table into a buffer. */
887 ret = wim_reshdr_to_data(&wim->hdr.blob_table_reshdr, wim, &buf);
891 /* Allocate a hash table to map SHA-1 message digests into blob
892 * descriptors. This is the in-memory "blob table". */
893 table = new_blob_table(num_entries * 2 + 1);
897 /* Allocate and initalize blob descriptors from the raw blob table
899 for (size_t i = 0; i < num_entries; i++) {
900 const struct blob_descriptor_disk *disk_entry =
901 &((const struct blob_descriptor_disk*)buf)[i];
902 struct wim_reshdr reshdr;
905 /* Get the resource header */
906 get_wim_reshdr(&disk_entry->reshdr, &reshdr);
908 DEBUG("reshdr: size_in_wim=%"PRIu64", "
909 "uncompressed_size=%"PRIu64", "
910 "offset_in_wim=%"PRIu64", "
912 reshdr.size_in_wim, reshdr.uncompressed_size,
913 reshdr.offset_in_wim, reshdr.flags);
915 /* Ignore SOLID flag if it isn't supposed to be used in this WIM
917 if (wim->hdr.wim_version == WIM_VERSION_DEFAULT)
918 reshdr.flags &= ~WIM_RESHDR_FLAG_SOLID;
920 /* Allocate a new 'struct blob_descriptor'. */
921 cur_blob = new_blob_descriptor();
925 /* Get the part number, reference count, and hash. */
926 part_number = le16_to_cpu(disk_entry->part_number);
927 cur_blob->refcnt = le32_to_cpu(disk_entry->refcnt);
928 copy_hash(cur_blob->hash, disk_entry->hash);
930 if (reshdr.flags & WIM_RESHDR_FLAG_SOLID) {
934 if (!cur_solid_rdescs) {
935 /* Starting new run */
936 ret = load_solid_info(wim, disk_entry,
939 &cur_num_solid_rdescs);
944 if (reshdr.uncompressed_size == SOLID_RESOURCE_MAGIC_NUMBER) {
945 /* Resource entry, not blob entry */
946 goto free_cur_blob_and_continue;
951 ret = assign_blob_to_solid_resource(&reshdr,
954 cur_num_solid_rdescs);
959 /* Normal blob/resource entry; SOLID not set. */
961 struct wim_resource_descriptor *rdesc;
963 if (unlikely(cur_solid_rdescs)) {
964 /* This entry terminated a solid run. */
965 ret = finish_solid_rdescs(cur_solid_rdescs,
966 cur_num_solid_rdescs);
967 cur_solid_rdescs = NULL;
972 /* How to handle an uncompressed resource with its
973 * uncompressed size different from its compressed size?
975 * Based on a simple test, WIMGAPI seems to handle this
978 * if (size_in_wim > uncompressed_size) {
979 * Ignore uncompressed_size; use size_in_wim
982 * Honor uncompressed_size, but treat the part of
983 * the file data above size_in_wim as all zeros.
986 * So we will do the same. */
987 if (unlikely(!(reshdr.flags &
988 WIM_RESHDR_FLAG_COMPRESSED) &&
989 (reshdr.size_in_wim >
990 reshdr.uncompressed_size)))
992 reshdr.uncompressed_size = reshdr.size_in_wim;
995 /* Set up a resource descriptor for this blob. */
997 rdesc = MALLOC(sizeof(struct wim_resource_descriptor));
1001 wim_res_hdr_to_desc(&reshdr, wim, rdesc);
1003 blob_set_is_located_in_nonsolid_wim_resource(cur_blob, rdesc);
1006 /* cur_blob is now a blob bound to a resource. */
1008 /* Ignore entries with all zeroes in the hash field. */
1009 if (is_zero_hash(cur_blob->hash))
1010 goto free_cur_blob_and_continue;
1012 /* Verify that the part number matches that of the underlying
1014 if (part_number != wim->hdr.part_number) {
1015 num_wrong_part_blobs++;
1016 goto free_cur_blob_and_continue;
1019 if (reshdr.flags & WIM_RESHDR_FLAG_METADATA) {
1021 cur_blob->is_metadata = 1;
1023 /* Blob table entry for a metadata resource. */
1025 /* Metadata entries with no references must be ignored.
1026 * See, for example, the WinPE WIMs from the WAIK v2.1.
1028 if (cur_blob->refcnt == 0)
1029 goto free_cur_blob_and_continue;
1031 if (cur_blob->refcnt != 1) {
1032 /* We don't currently support this case due to
1033 * the complications of multiple images sharing
1034 * the same metadata resource or a metadata
1035 * resource also being referenced by files. */
1036 ERROR("Found metadata resource with refcnt != 1");
1037 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
1041 if (wim->hdr.part_number != 1) {
1042 WARNING("Ignoring metadata resource found in a "
1043 "non-first part of the split WIM");
1044 goto free_cur_blob_and_continue;
1047 /* The number of entries in the blob table with
1048 * WIM_RESHDR_FLAG_METADATA set should be the same as
1049 * the image_count field in the WIM header. */
1050 if (image_index == wim->hdr.image_count) {
1051 WARNING("Found more metadata resources than images");
1052 goto free_cur_blob_and_continue;
1055 /* Notice very carefully: We are assigning the metadata
1056 * resources to images in the same order in which their
1057 * blob table entries occur on disk. (This is also the
1058 * behavior of Microsoft's software.) In particular,
1059 * this overrides the actual locations of the metadata
1060 * resources themselves in the WIM file as well as any
1061 * information written in the XML data. */
1062 DEBUG("Found metadata resource for image %"PRIu32" at "
1063 "offset %"PRIu64".",
1065 reshdr.offset_in_wim);
1067 wim->image_metadata[image_index++]->metadata_blob = cur_blob;
1069 /* Blob table entry for a non-metadata blob. */
1071 /* Ignore this blob if it's a duplicate. */
1072 if (lookup_blob(table, cur_blob->hash)) {
1073 num_duplicate_blobs++;
1074 goto free_cur_blob_and_continue;
1077 /* Insert the blob into the in-memory blob table, keyed
1078 * by its SHA-1 message digest. */
1079 blob_table_insert(table, cur_blob);
1084 free_cur_blob_and_continue:
1085 if (cur_solid_rdescs &&
1086 cur_blob->blob_location == BLOB_IN_WIM)
1087 blob_unset_is_located_in_wim_resource(cur_blob);
1088 free_blob_descriptor(cur_blob);
1092 if (cur_solid_rdescs) {
1093 /* End of blob table terminated a solid run. */
1094 ret = finish_solid_rdescs(cur_solid_rdescs, cur_num_solid_rdescs);
1095 cur_solid_rdescs = NULL;
1100 if (wim->hdr.part_number == 1 && image_index != wim->hdr.image_count) {
1101 WARNING("Could not find metadata resources for all images");
1102 for (u32 i = image_index; i < wim->hdr.image_count; i++)
1103 put_image_metadata(wim->image_metadata[i], NULL);
1104 wim->hdr.image_count = image_index;
1107 if (num_duplicate_blobs > 0)
1108 WARNING("Ignoring %zu duplicate blobs", num_duplicate_blobs);
1110 if (num_wrong_part_blobs > 0) {
1111 WARNING("Ignoring %zu blobs with wrong part number",
1112 num_wrong_part_blobs);
1115 DEBUG("Done reading blob table.");
1116 wim->blob_table = table;
1121 ERROR("Not enough memory to read blob table!");
1122 ret = WIMLIB_ERR_NOMEM;
1124 free_solid_rdescs(cur_solid_rdescs, cur_num_solid_rdescs);
1125 free_blob_descriptor(cur_blob);
1126 free_blob_table(table);
1133 write_blob_descriptor(struct blob_descriptor_disk *disk_entry,
1134 const struct wim_reshdr *out_reshdr,
1135 u16 part_number, u32 refcnt, const u8 *hash)
1137 put_wim_reshdr(out_reshdr, &disk_entry->reshdr);
1138 disk_entry->part_number = cpu_to_le16(part_number);
1139 disk_entry->refcnt = cpu_to_le32(refcnt);
1140 copy_hash(disk_entry->hash, hash);
1143 /* Note: the list of blob descriptors must be sorted so that all entries for the
1144 * same solid resource are consecutive. In addition, blob descriptors for
1145 * metadata resources must be in the same order as the indices of the underlying
1148 write_blob_table_from_blob_list(struct list_head *blob_list,
1149 struct filedes *out_fd,
1151 struct wim_reshdr *out_reshdr,
1152 int write_resource_flags)
1155 struct blob_descriptor *blob;
1156 struct blob_descriptor_disk *table_buf;
1157 struct blob_descriptor_disk *table_buf_ptr;
1159 u64 prev_res_offset_in_wim = ~0ULL;
1160 u64 prev_uncompressed_size;
1164 list_for_each_entry(blob, blob_list, blob_table_list) {
1165 table_size += sizeof(struct blob_descriptor_disk);
1167 if (blob->out_reshdr.flags & WIM_RESHDR_FLAG_SOLID &&
1168 blob->out_res_offset_in_wim != prev_res_offset_in_wim)
1170 table_size += sizeof(struct blob_descriptor_disk);
1171 prev_res_offset_in_wim = blob->out_res_offset_in_wim;
1175 DEBUG("Writing WIM blob table (size=%zu, offset=%"PRIu64")",
1176 table_size, out_fd->offset);
1178 table_buf = MALLOC(table_size);
1179 if (table_buf == NULL) {
1180 ERROR("Failed to allocate %zu bytes for temporary blob table",
1182 return WIMLIB_ERR_NOMEM;
1184 table_buf_ptr = table_buf;
1186 prev_res_offset_in_wim = ~0ULL;
1187 prev_uncompressed_size = 0;
1189 list_for_each_entry(blob, blob_list, blob_table_list) {
1190 if (blob->out_reshdr.flags & WIM_RESHDR_FLAG_SOLID) {
1191 struct wim_reshdr tmp_reshdr;
1193 /* Eww. When WIMGAPI sees multiple solid resources, it
1194 * expects the offsets to be adjusted as if there were
1195 * really only one solid resource. */
1197 if (blob->out_res_offset_in_wim != prev_res_offset_in_wim) {
1198 /* Put the resource entry for solid resource */
1199 tmp_reshdr.offset_in_wim = blob->out_res_offset_in_wim;
1200 tmp_reshdr.size_in_wim = blob->out_res_size_in_wim;
1201 tmp_reshdr.uncompressed_size = SOLID_RESOURCE_MAGIC_NUMBER;
1202 tmp_reshdr.flags = WIM_RESHDR_FLAG_SOLID;
1204 write_blob_descriptor(table_buf_ptr++, &tmp_reshdr,
1205 part_number, 1, zero_hash);
1207 logical_offset += prev_uncompressed_size;
1209 prev_res_offset_in_wim = blob->out_res_offset_in_wim;
1210 prev_uncompressed_size = blob->out_res_uncompressed_size;
1212 tmp_reshdr = blob->out_reshdr;
1213 tmp_reshdr.offset_in_wim += logical_offset;
1214 write_blob_descriptor(table_buf_ptr++, &tmp_reshdr,
1215 part_number, blob->out_refcnt, blob->hash);
1217 write_blob_descriptor(table_buf_ptr++, &blob->out_reshdr,
1218 part_number, blob->out_refcnt, blob->hash);
1222 wimlib_assert((u8*)table_buf_ptr - (u8*)table_buf == table_size);
1224 /* Write the blob table uncompressed. Although wimlib can handle a
1225 * compressed blob table, MS software cannot. */
1226 ret = write_wim_resource_from_buffer(table_buf,
1230 WIMLIB_COMPRESSION_TYPE_NONE,
1234 write_resource_flags);
1236 DEBUG("ret=%d", ret);
1240 /* Allocate a blob descriptor for the contents of the buffer, or re-use an
1241 * existing descriptor in @blob_table for an identical blob. */
1242 struct blob_descriptor *
1243 new_blob_from_data_buffer(const void *buffer, size_t size,
1244 struct blob_table *blob_table)
1246 u8 hash[SHA1_HASH_SIZE];
1247 struct blob_descriptor *blob;
1250 sha1_buffer(buffer, size, hash);
1252 blob = lookup_blob(blob_table, hash);
1256 blob = new_blob_descriptor();
1260 buffer_copy = memdup(buffer, size);
1262 free_blob_descriptor(blob);
1265 blob_set_is_located_in_attached_buffer(blob, buffer_copy, size);
1266 copy_hash(blob->hash, hash);
1267 blob_table_insert(blob_table, blob);
1271 struct blob_descriptor *
1272 after_blob_hashed(struct blob_descriptor *blob,
1273 struct blob_descriptor **back_ptr,
1274 struct blob_table *blob_table)
1276 struct blob_descriptor *duplicate_blob;
1278 list_del(&blob->unhashed_list);
1281 /* Look for a duplicate blob */
1282 duplicate_blob = lookup_blob(blob_table, blob->hash);
1283 if (duplicate_blob) {
1284 /* We have a duplicate blob. Transfer the reference counts from
1285 * this blob to the duplicate and update the reference to this
1286 * blob (from a stream) to point to the duplicate. The caller
1287 * is responsible for freeing @blob if needed. */
1288 wimlib_assert(duplicate_blob->size == blob->size);
1289 duplicate_blob->refcnt += blob->refcnt;
1291 *back_ptr = duplicate_blob;
1292 return duplicate_blob;
1294 /* No duplicate blob, so we need to insert this blob into the
1295 * blob table and treat it as a hashed blob. */
1296 blob_table_insert(blob_table, blob);
1302 * Calculate the SHA-1 message digest of a blob and move its descriptor from the
1303 * list of unhashed blobs to the blob table, possibly joining it with an
1309 * The blob table in which the blob needs to be indexed
1311 * On success, a pointer to the resulting blob descriptor is written to
1312 * this location. This will be the same as @blob if it was inserted into
1313 * the blob table, or different if a duplicate blob was found.
1315 * Returns 0 on success; nonzero if there is an error reading the blob data.
1318 hash_unhashed_blob(struct blob_descriptor *blob, struct blob_table *blob_table,
1319 struct blob_descriptor **blob_ret)
1321 struct blob_descriptor **back_ptr;
1324 back_ptr = retrieve_pointer_to_unhashed_blob(blob);
1326 ret = sha1_blob(blob);
1330 *blob_ret = after_blob_hashed(blob, back_ptr, blob_table);
1335 blob_to_wimlib_resource_entry(const struct blob_descriptor *blob,
1336 struct wimlib_resource_entry *wentry)
1338 memset(wentry, 0, sizeof(*wentry));
1340 wentry->uncompressed_size = blob->size;
1341 if (blob->blob_location == BLOB_IN_WIM) {
1342 unsigned res_flags = blob->rdesc->flags;
1344 wentry->part_number = blob->rdesc->wim->hdr.part_number;
1345 if (res_flags & WIM_RESHDR_FLAG_SOLID) {
1346 wentry->offset = blob->offset_in_res;
1348 wentry->compressed_size = blob->rdesc->size_in_wim;
1349 wentry->offset = blob->rdesc->offset_in_wim;
1351 wentry->raw_resource_offset_in_wim = blob->rdesc->offset_in_wim;
1352 wentry->raw_resource_compressed_size = blob->rdesc->size_in_wim;
1353 wentry->raw_resource_uncompressed_size = blob->rdesc->uncompressed_size;
1355 wentry->is_compressed = (res_flags & WIM_RESHDR_FLAG_COMPRESSED) != 0;
1356 wentry->is_free = (res_flags & WIM_RESHDR_FLAG_FREE) != 0;
1357 wentry->is_spanned = (res_flags & WIM_RESHDR_FLAG_SPANNED) != 0;
1358 wentry->packed = (res_flags & WIM_RESHDR_FLAG_SOLID) != 0;
1360 if (!blob->unhashed)
1361 copy_hash(wentry->sha1_hash, blob->hash);
1362 wentry->reference_count = blob->refcnt;
1363 wentry->is_metadata = blob->is_metadata;
1366 struct iterate_blob_context {
1367 wimlib_iterate_lookup_table_callback_t cb;
1372 do_iterate_blob(struct blob_descriptor *blob, void *_ctx)
1374 struct iterate_blob_context *ctx = _ctx;
1375 struct wimlib_resource_entry entry;
1377 blob_to_wimlib_resource_entry(blob, &entry);
1378 return (*ctx->cb)(&entry, ctx->user_ctx);
1381 /* API function documented in wimlib.h */
1383 wimlib_iterate_lookup_table(WIMStruct *wim, int flags,
1384 wimlib_iterate_lookup_table_callback_t cb,
1388 return WIMLIB_ERR_INVALID_PARAM;
1390 struct iterate_blob_context ctx = {
1392 .user_ctx = user_ctx,
1394 if (wim_has_metadata(wim)) {
1396 for (int i = 0; i < wim->hdr.image_count; i++) {
1397 struct blob_descriptor *blob;
1398 struct wim_image_metadata *imd = wim->image_metadata[i];
1400 ret = do_iterate_blob(imd->metadata_blob, &ctx);
1403 image_for_each_unhashed_blob(blob, imd) {
1404 ret = do_iterate_blob(blob, &ctx);
1410 return for_blob_in_table(wim->blob_table, do_iterate_blob, &ctx);