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/bitops.h"
38 #include "wimlib/blob_table.h"
39 #include "wimlib/encoding.h"
40 #include "wimlib/endianness.h"
41 #include "wimlib/error.h"
42 #include "wimlib/metadata.h"
43 #include "wimlib/ntfs_3g.h"
44 #include "wimlib/resource.h"
45 #include "wimlib/unaligned.h"
46 #include "wimlib/util.h"
47 #include "wimlib/write.h"
49 /* A hash table mapping SHA-1 message digests to blob descriptors */
51 struct hlist_head *array;
53 size_t mask; /* capacity - 1; capacity is a power of 2 */
57 next_power_of_2(size_t n)
61 return (size_t)1 << (1 + flsw(n - 1));
65 new_blob_table(size_t capacity)
67 struct blob_table *table;
68 struct hlist_head *array;
70 capacity = next_power_of_2(capacity);
72 table = MALLOC(sizeof(struct blob_table));
76 array = CALLOC(capacity, sizeof(array[0]));
83 table->mask = capacity - 1;
88 ERROR("Failed to allocate memory for blob table "
89 "with capacity %zu", capacity);
94 do_free_blob_descriptor(struct blob_descriptor *blob, void *_ignore)
96 free_blob_descriptor(blob);
101 free_blob_table(struct blob_table *table)
104 for_blob_in_table(table, do_free_blob_descriptor, NULL);
110 struct blob_descriptor *
111 new_blob_descriptor(void)
113 STATIC_ASSERT(BLOB_NONEXISTENT == 0);
114 return CALLOC(1, sizeof(struct blob_descriptor));
117 struct blob_descriptor *
118 clone_blob_descriptor(const struct blob_descriptor *old)
120 struct blob_descriptor *new;
122 new = memdup(old, sizeof(struct blob_descriptor));
126 switch (new->blob_location) {
128 list_add(&new->rdesc_node, &new->rdesc->blob_list);
131 case BLOB_IN_FILE_ON_DISK:
133 case BLOB_IN_WINNT_FILE_ON_DISK:
134 case BLOB_WIN32_ENCRYPTED:
137 case BLOB_IN_STAGING_FILE:
138 STATIC_ASSERT((void*)&old->file_on_disk ==
139 (void*)&old->staging_file_name);
141 new->file_on_disk = TSTRDUP(old->file_on_disk);
142 if (new->file_on_disk == NULL)
145 case BLOB_IN_ATTACHED_BUFFER:
146 new->attached_buffer = memdup(old->attached_buffer, old->size);
147 if (new->attached_buffer == NULL)
151 case BLOB_IN_NTFS_VOLUME:
152 new->ntfs_loc = clone_ntfs_location(old->ntfs_loc);
161 free_blob_descriptor(new);
166 blob_release_location(struct blob_descriptor *blob)
168 switch (blob->blob_location) {
170 list_del(&blob->rdesc_node);
171 if (list_empty(&blob->rdesc->blob_list))
174 case BLOB_IN_FILE_ON_DISK:
176 case BLOB_IN_WINNT_FILE_ON_DISK:
177 case BLOB_WIN32_ENCRYPTED:
180 case BLOB_IN_STAGING_FILE:
181 STATIC_ASSERT((void*)&blob->file_on_disk ==
182 (void*)&blob->staging_file_name);
184 case BLOB_IN_ATTACHED_BUFFER:
185 STATIC_ASSERT((void*)&blob->file_on_disk ==
186 (void*)&blob->attached_buffer);
187 FREE(blob->file_on_disk);
190 case BLOB_IN_NTFS_VOLUME:
192 free_ntfs_location(blob->ntfs_loc);
199 free_blob_descriptor(struct blob_descriptor *blob)
202 blob_release_location(blob);
207 /* Should this blob be retained even if it has no references? */
209 should_retain_blob(const struct blob_descriptor *blob)
211 return blob->blob_location == BLOB_IN_WIM;
215 finalize_blob(struct blob_descriptor *blob)
217 if (!should_retain_blob(blob))
218 free_blob_descriptor(blob);
222 * Decrements the reference count of the specified blob, which must be either
223 * (a) unhashed, or (b) inserted in the specified blob table.
225 * If the blob's reference count reaches 0, we may unlink it from @table and
226 * free it. However, we retain blobs with 0 reference count that originated
227 * from WIM files (BLOB_IN_WIM). We do this for two reasons:
229 * 1. This prevents information about valid blobs in a WIM file --- blobs which
230 * will continue to be present after appending to the WIM file --- from being
231 * lost merely because we dropped all references to them.
233 * 2. Blob reference counts we read from WIM files can't be trusted. It's
234 * possible that a WIM has reference counts that are too low; WIMGAPI
235 * sometimes creates WIMs where this is the case. It's also possible that
236 * blobs have been referenced from an external WIM; those blobs can
237 * potentially have any reference count at all, either lower or higher than
238 * would be expected for this WIM ("this WIM" meaning the owner of @table) if
239 * it were a standalone WIM.
241 * So we can't take the reference counts too seriously. But at least, we do
242 * recalculate by default when writing a new WIM file.
245 blob_decrement_refcnt(struct blob_descriptor *blob, struct blob_table *table)
247 blob_subtract_refcnt(blob, table, 1);
251 blob_subtract_refcnt(struct blob_descriptor *blob, struct blob_table *table,
254 if (unlikely(blob->refcnt < count)) {
255 blob->refcnt = 0; /* See comment above */
259 blob->refcnt -= count;
261 if (blob->refcnt != 0)
264 if (blob->unhashed) {
265 list_del(&blob->unhashed_list);
267 /* If the blob has been extracted to a staging file for a FUSE
268 * mount, unlink the staging file. (Note that there still may
269 * be open file descriptors to it.) */
270 if (blob->blob_location == BLOB_IN_STAGING_FILE)
271 unlinkat(blob->staging_dir_fd,
272 blob->staging_file_name, 0);
275 if (!should_retain_blob(blob))
276 blob_table_unlink(table, blob);
279 /* If FUSE mounts are enabled, then don't actually free the blob
280 * descriptor until the last file descriptor to it has been closed. */
282 if (blob->num_opened_fds == 0)
289 blob_decrement_num_opened_fds(struct blob_descriptor *blob)
291 wimlib_assert(blob->num_opened_fds != 0);
293 if (--blob->num_opened_fds == 0 && blob->refcnt == 0)
299 blob_table_insert_raw(struct blob_table *table, struct blob_descriptor *blob)
301 size_t i = blob->hash_short & table->mask;
303 hlist_add_head(&blob->hash_list, &table->array[i]);
307 enlarge_blob_table(struct blob_table *table)
309 size_t old_capacity, new_capacity;
310 struct hlist_head *old_array, *new_array;
311 struct blob_descriptor *blob;
312 struct hlist_node *tmp;
315 old_capacity = table->mask + 1;
316 new_capacity = old_capacity * 2;
317 new_array = CALLOC(new_capacity, sizeof(struct hlist_head));
318 if (new_array == NULL)
320 old_array = table->array;
321 table->array = new_array;
322 table->mask = new_capacity - 1;
324 for (i = 0; i < old_capacity; i++)
325 hlist_for_each_entry_safe(blob, tmp, &old_array[i], hash_list)
326 blob_table_insert_raw(table, blob);
330 /* Insert a blob descriptor into the blob table. */
332 blob_table_insert(struct blob_table *table, struct blob_descriptor *blob)
334 blob_table_insert_raw(table, blob);
335 if (table->num_blobs++ > table->mask)
336 enlarge_blob_table(table);
339 /* Unlinks a blob descriptor from the blob table; does not free it. */
341 blob_table_unlink(struct blob_table *table, struct blob_descriptor *blob)
343 wimlib_assert(!blob->unhashed);
344 wimlib_assert(table->num_blobs != 0);
346 hlist_del(&blob->hash_list);
350 /* Given a SHA-1 message digest, return the corresponding blob descriptor from
351 * the specified blob table, or NULL if there is none. */
352 struct blob_descriptor *
353 lookup_blob(const struct blob_table *table, const u8 *hash)
356 struct blob_descriptor *blob;
358 i = load_size_t_unaligned(hash) & table->mask;
359 hlist_for_each_entry(blob, &table->array[i], hash_list)
360 if (hashes_equal(hash, blob->hash))
365 /* Call a function on all blob descriptors in the specified blob table. Stop
366 * early and return nonzero if any call to the function returns nonzero. */
368 for_blob_in_table(struct blob_table *table,
369 int (*visitor)(struct blob_descriptor *, void *), void *arg)
371 struct blob_descriptor *blob;
372 struct hlist_node *tmp;
375 for (size_t i = 0; i <= table->mask; i++) {
376 hlist_for_each_entry_safe(blob, tmp, &table->array[i],
379 ret = visitor(blob, arg);
388 * This is a qsort() callback that sorts blobs into an order optimized for
389 * reading. Sorting is done primarily by blob location, then secondarily by a
390 * location-dependent order. For example, blobs in WIM resources are sorted
391 * such that the underlying WIM files will be read sequentially. This is
392 * especially important for WIM files containing solid resources.
395 cmp_blobs_by_sequential_order(const void *p1, const void *p2)
397 const struct blob_descriptor *blob1, *blob2;
399 WIMStruct *wim1, *wim2;
401 blob1 = *(const struct blob_descriptor**)p1;
402 blob2 = *(const struct blob_descriptor**)p2;
404 v = (int)blob1->blob_location - (int)blob2->blob_location;
406 /* Different locations? */
410 switch (blob1->blob_location) {
412 wim1 = blob1->rdesc->wim;
413 wim2 = blob2->rdesc->wim;
415 /* Different WIM files? */
418 /* Different split WIMs? */
419 v = cmp_guids(wim1->hdr.guid, wim2->hdr.guid);
423 /* Different part numbers in the same split WIM? */
424 v = (int)wim1->hdr.part_number - (int)wim2->hdr.part_number;
428 /* Probably two WIMStructs for the same on-disk file.
429 * Just sort by pointer. */
430 return wim1 < wim2 ? -1 : 1;
435 /* Sort by increasing resource offset */
436 if (blob1->rdesc->offset_in_wim != blob2->rdesc->offset_in_wim)
437 return cmp_u64(blob1->rdesc->offset_in_wim,
438 blob2->rdesc->offset_in_wim);
440 /* The blobs are in the same solid resource. Sort by increasing
441 * offset in the resource. */
442 return cmp_u64(blob1->offset_in_res, blob2->offset_in_res);
444 case BLOB_IN_FILE_ON_DISK:
446 case BLOB_IN_STAGING_FILE:
449 case BLOB_IN_WINNT_FILE_ON_DISK:
450 case BLOB_WIN32_ENCRYPTED:
451 /* Windows: compare by starting LCN (logical cluster number) */
452 v = cmp_u64(blob1->sort_key, blob2->sort_key);
456 /* Compare files by path: just a heuristic that will place files
457 * in the same directory next to each other. */
458 return tstrcmp(blob1->file_on_disk, blob2->file_on_disk);
460 case BLOB_IN_NTFS_VOLUME:
461 return cmp_ntfs_locations(blob1->ntfs_loc, blob2->ntfs_loc);
464 /* No additional sorting order defined for this resource
465 * location (e.g. BLOB_IN_ATTACHED_BUFFER); simply compare
466 * everything equal to each other. */
472 sort_blob_list(struct list_head *blob_list, size_t list_head_offset,
473 int (*compar)(const void *, const void*))
475 struct list_head *cur;
476 struct blob_descriptor **array;
479 size_t num_blobs = 0;
481 list_for_each(cur, blob_list)
487 array_size = num_blobs * sizeof(array[0]);
488 array = MALLOC(array_size);
490 return WIMLIB_ERR_NOMEM;
492 cur = blob_list->next;
493 for (i = 0; i < num_blobs; i++) {
494 array[i] = (struct blob_descriptor*)((u8*)cur - list_head_offset);
498 qsort(array, num_blobs, sizeof(array[0]), compar);
500 INIT_LIST_HEAD(blob_list);
501 for (i = 0; i < num_blobs; i++) {
502 list_add_tail((struct list_head*)
503 ((u8*)array[i] + list_head_offset), blob_list);
509 /* Sort the specified list of blobs in an order optimized for sequential
512 sort_blob_list_by_sequential_order(struct list_head *blob_list,
513 size_t list_head_offset)
515 return sort_blob_list(blob_list, list_head_offset,
516 cmp_blobs_by_sequential_order);
520 add_blob_to_array(struct blob_descriptor *blob, void *_pp)
522 struct blob_descriptor ***pp = _pp;
527 /* Iterate through the blob descriptors in the specified blob table in an order
528 * optimized for sequential reading. */
530 for_blob_in_table_sorted_by_sequential_order(struct blob_table *table,
531 int (*visitor)(struct blob_descriptor *, void *),
534 struct blob_descriptor **blob_array, **p;
535 size_t num_blobs = table->num_blobs;
538 blob_array = MALLOC(num_blobs * sizeof(blob_array[0]));
540 return WIMLIB_ERR_NOMEM;
542 for_blob_in_table(table, add_blob_to_array, &p);
544 wimlib_assert(p == blob_array + num_blobs);
546 qsort(blob_array, num_blobs, sizeof(blob_array[0]),
547 cmp_blobs_by_sequential_order);
549 for (size_t i = 0; i < num_blobs; i++) {
550 ret = visitor(blob_array[i], arg);
558 /* On-disk format of a blob descriptor in a WIM file.
560 * Note: if the WIM file contains solid resource(s), then this structure is
561 * sometimes overloaded to describe a "resource" rather than a "blob". See the
562 * code for details. */
563 struct blob_descriptor_disk {
565 /* Size, offset, and flags of the blob. */
566 struct wim_reshdr_disk reshdr;
568 /* Which part of the split WIM this blob is in; indexed from 1. */
571 /* Reference count of this blob over all WIM images. (But see comment
572 * above blob_decrement_refcnt().) */
575 /* SHA-1 message digest of the uncompressed data of this blob, or all
576 * zeroes if this blob is of zero length. */
577 u8 hash[SHA1_HASH_SIZE];
580 /* Given a nonempty run of consecutive blob descriptors with the SOLID flag set,
581 * count how many specify resources (as opposed to blobs within those
584 * Returns the resulting count. */
586 count_solid_resources(const struct blob_descriptor_disk *entries, size_t max)
590 struct wim_reshdr reshdr;
592 get_wim_reshdr(&(entries++)->reshdr, &reshdr);
594 if (!(reshdr.flags & WIM_RESHDR_FLAG_SOLID)) {
595 /* Run was terminated by a stand-alone blob entry. */
599 if (reshdr.uncompressed_size == SOLID_RESOURCE_MAGIC_NUMBER) {
600 /* This is a resource entry. */
608 * Given a run of consecutive blob descriptors with the SOLID flag set and
609 * having @num_rdescs resource entries, load resource information from them into
610 * the resource descriptors in the @rdescs array.
612 * Returns 0 on success, or a nonzero error code on failure.
615 do_load_solid_info(WIMStruct *wim, struct wim_resource_descriptor **rdescs,
617 const struct blob_descriptor_disk *entries)
619 for (size_t i = 0; i < num_rdescs; i++) {
620 struct wim_reshdr reshdr;
621 struct alt_chunk_table_header_disk hdr;
622 struct wim_resource_descriptor *rdesc;
625 /* Advance to next resource entry. */
628 get_wim_reshdr(&(entries++)->reshdr, &reshdr);
629 } while (reshdr.uncompressed_size != SOLID_RESOURCE_MAGIC_NUMBER);
633 wim_reshdr_to_desc(&reshdr, wim, rdesc);
635 /* For solid resources, the uncompressed size, compression type,
636 * and chunk size are stored in the resource itself, not in the
639 ret = full_pread(&wim->in_fd, &hdr,
640 sizeof(hdr), reshdr.offset_in_wim);
642 ERROR("Failed to read header of solid resource "
643 "(offset_in_wim=%"PRIu64")",
644 reshdr.offset_in_wim);
648 rdesc->uncompressed_size = le64_to_cpu(hdr.res_usize);
650 /* Compression format numbers must be the same as in
651 * WIMGAPI to be compatible here. */
652 STATIC_ASSERT(WIMLIB_COMPRESSION_TYPE_NONE == 0);
653 STATIC_ASSERT(WIMLIB_COMPRESSION_TYPE_XPRESS == 1);
654 STATIC_ASSERT(WIMLIB_COMPRESSION_TYPE_LZX == 2);
655 STATIC_ASSERT(WIMLIB_COMPRESSION_TYPE_LZMS == 3);
656 rdesc->compression_type = le32_to_cpu(hdr.compression_format);
657 rdesc->chunk_size = le32_to_cpu(hdr.chunk_size);
663 * Given a nonempty run of consecutive blob descriptors with the SOLID flag set,
664 * allocate a 'struct wim_resource_descriptor' for each resource within that
667 * Returns 0 on success, or a nonzero error code on failure.
668 * Returns the pointers and count in *rdescs_ret and *num_rdescs_ret.
671 load_solid_info(WIMStruct *wim,
672 const struct blob_descriptor_disk *entries,
673 size_t num_remaining_entries,
674 struct wim_resource_descriptor ***rdescs_ret,
675 size_t *num_rdescs_ret)
678 struct wim_resource_descriptor **rdescs;
682 num_rdescs = count_solid_resources(entries, num_remaining_entries);
683 rdescs = CALLOC(num_rdescs, sizeof(rdescs[0]));
685 return WIMLIB_ERR_NOMEM;
687 for (i = 0; i < num_rdescs; i++) {
688 rdescs[i] = MALLOC(sizeof(struct wim_resource_descriptor));
690 ret = WIMLIB_ERR_NOMEM;
691 goto out_free_rdescs;
695 ret = do_load_solid_info(wim, rdescs, num_rdescs, entries);
697 goto out_free_rdescs;
699 *rdescs_ret = rdescs;
700 *num_rdescs_ret = num_rdescs;
704 for (i = 0; i < num_rdescs; i++)
710 /* Given a 'struct blob_descriptor' allocated for an on-disk blob descriptor
711 * with the SOLID flag set, try to assign it to resource in the current solid
714 assign_blob_to_solid_resource(const struct wim_reshdr *reshdr,
715 struct blob_descriptor *blob,
716 struct wim_resource_descriptor **rdescs,
719 u64 offset = reshdr->offset_in_wim;
721 /* XXX: This linear search will be slow in the degenerate case where the
722 * number of solid resources in the run is huge. */
723 blob->size = reshdr->size_in_wim;
724 for (size_t i = 0; i < num_rdescs; i++) {
725 if (offset + blob->size <= rdescs[i]->uncompressed_size) {
726 blob_set_is_located_in_wim_resource(blob, rdescs[i], offset);
729 offset -= rdescs[i]->uncompressed_size;
731 ERROR("blob could not be assigned to a solid resource");
732 return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
736 free_solid_rdescs(struct wim_resource_descriptor **rdescs, size_t num_rdescs)
739 for (size_t i = 0; i < num_rdescs; i++)
740 if (list_empty(&rdescs[i]->blob_list))
747 cmp_blobs_by_offset_in_res(const void *p1, const void *p2)
749 const struct blob_descriptor *blob1, *blob2;
751 blob1 = *(const struct blob_descriptor**)p1;
752 blob2 = *(const struct blob_descriptor**)p2;
754 return cmp_u64(blob1->offset_in_res, blob2->offset_in_res);
757 /* Validate the size and location of a WIM resource. */
759 validate_resource(struct wim_resource_descriptor *rdesc)
761 struct blob_descriptor *blob;
763 u64 expected_next_offset;
766 /* Verify that the resource itself has a valid offset and size. */
767 if (rdesc->offset_in_wim + rdesc->size_in_wim < rdesc->size_in_wim)
768 goto invalid_due_to_overflow;
770 /* Verify that each blob in the resource has a valid offset and size.
772 expected_next_offset = 0;
773 out_of_order = false;
774 list_for_each_entry(blob, &rdesc->blob_list, rdesc_node) {
775 if (blob->offset_in_res + blob->size < blob->size ||
776 blob->offset_in_res + blob->size > rdesc->uncompressed_size)
777 goto invalid_due_to_overflow;
779 if (blob->offset_in_res >= expected_next_offset)
780 expected_next_offset = blob->offset_in_res + blob->size;
785 /* If the blobs were not located at strictly increasing positions (not
786 * allowing for overlap), sort them. Then make sure that none overlap.
789 ret = sort_blob_list(&rdesc->blob_list,
790 offsetof(struct blob_descriptor,
792 cmp_blobs_by_offset_in_res);
796 expected_next_offset = 0;
797 list_for_each_entry(blob, &rdesc->blob_list, rdesc_node) {
798 if (blob->offset_in_res >= expected_next_offset)
799 expected_next_offset = blob->offset_in_res + blob->size;
801 goto invalid_due_to_overlap;
807 invalid_due_to_overflow:
808 ERROR("Invalid blob table (offset overflow)");
809 return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
811 invalid_due_to_overlap:
812 ERROR("Invalid blob table (blobs in solid resource overlap)");
813 return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
817 finish_solid_rdescs(struct wim_resource_descriptor **rdescs, size_t num_rdescs)
820 for (size_t i = 0; i < num_rdescs; i++) {
821 ret = validate_resource(rdescs[i]);
825 free_solid_rdescs(rdescs, num_rdescs);
830 * read_blob_table() -
832 * Read the blob table from a WIM file. Usually, each entry in this table
833 * describes a "blob", or equivalently a "resource", that the WIM file contains,
834 * along with its location and SHA-1 message digest. Descriptors for
835 * non-metadata blobs will be saved in the in-memory blob table
836 * (wim->blob_table), whereas descriptors for metadata blobs will be saved in a
837 * special location per-image (the wim->image_metadata array).
839 * However, in WIM_VERSION_SOLID (3584) WIMs, a resource may contain multiple
840 * blobs that are compressed together. Such a resource is called a "solid
841 * resource". Solid resources are still described in the on-disk "blob table",
842 * although the format is not the most logical. A consecutive sequence of
843 * entries that all have flag WIM_RESHDR_FLAG_SOLID (0x10) set is a "solid run".
844 * A solid run describes a set of solid resources, each of which contains a set
845 * of blobs. In a solid run, a 'struct wim_reshdr_disk' with 'uncompressed_size
846 * = SOLID_RESOURCE_MAGIC_NUMBER (0x100000000)' specifies a solid resource,
847 * whereas any other 'struct wim_reshdr_disk' specifies a blob within a solid
848 * resource. There are some oddities in how we need to determine which solid
849 * resource a blob is actually in; see the code for details.
851 * Possible return values:
852 * WIMLIB_ERR_SUCCESS (0)
853 * WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY
856 * Or an error code caused by failure to read the blob table from the WIM
860 read_blob_table(WIMStruct *wim)
865 struct blob_table *table = NULL;
866 struct blob_descriptor *cur_blob = NULL;
867 size_t num_duplicate_blobs = 0;
868 size_t num_empty_blobs = 0;
869 size_t num_wrong_part_blobs = 0;
871 struct wim_resource_descriptor **cur_solid_rdescs = NULL;
872 size_t cur_num_solid_rdescs = 0;
874 /* Calculate the number of entries in the blob table. */
875 num_entries = wim->hdr.blob_table_reshdr.uncompressed_size /
876 sizeof(struct blob_descriptor_disk);
878 /* Read the blob table into a buffer. */
879 ret = wim_reshdr_to_data(&wim->hdr.blob_table_reshdr, wim, &buf);
883 /* Allocate a hash table to map SHA-1 message digests into blob
884 * descriptors. This is the in-memory "blob table". */
885 table = new_blob_table(num_entries);
889 /* Allocate and initalize blob descriptors from the raw blob table
891 for (size_t i = 0; i < num_entries; i++) {
892 const struct blob_descriptor_disk *disk_entry =
893 &((const struct blob_descriptor_disk*)buf)[i];
894 struct wim_reshdr reshdr;
897 /* Get the resource header */
898 get_wim_reshdr(&disk_entry->reshdr, &reshdr);
900 /* Ignore SOLID flag if it isn't supposed to be used in this WIM
902 if (wim->hdr.wim_version == WIM_VERSION_DEFAULT)
903 reshdr.flags &= ~WIM_RESHDR_FLAG_SOLID;
905 /* Allocate a new 'struct blob_descriptor'. */
906 cur_blob = new_blob_descriptor();
910 /* Get the part number, reference count, and hash. */
911 part_number = le16_to_cpu(disk_entry->part_number);
912 cur_blob->refcnt = le32_to_cpu(disk_entry->refcnt);
913 copy_hash(cur_blob->hash, disk_entry->hash);
915 if (reshdr.flags & WIM_RESHDR_FLAG_SOLID) {
919 if (!cur_solid_rdescs) {
920 /* Starting new run */
921 ret = load_solid_info(wim, disk_entry,
924 &cur_num_solid_rdescs);
929 if (reshdr.uncompressed_size == SOLID_RESOURCE_MAGIC_NUMBER) {
930 /* Resource entry, not blob entry */
931 goto free_cur_blob_and_continue;
936 ret = assign_blob_to_solid_resource(&reshdr,
939 cur_num_solid_rdescs);
944 /* Normal blob/resource entry; SOLID not set. */
946 struct wim_resource_descriptor *rdesc;
948 if (unlikely(cur_solid_rdescs)) {
949 /* This entry terminated a solid run. */
950 ret = finish_solid_rdescs(cur_solid_rdescs,
951 cur_num_solid_rdescs);
952 cur_solid_rdescs = NULL;
957 if (unlikely(!(reshdr.flags & WIM_RESHDR_FLAG_COMPRESSED) &&
958 (reshdr.size_in_wim != reshdr.uncompressed_size)))
960 ERROR("Uncompressed resource has "
961 "size_in_wim != uncompressed_size");
962 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
966 /* Set up a resource descriptor for this blob. */
968 rdesc = MALLOC(sizeof(struct wim_resource_descriptor));
972 wim_reshdr_to_desc_and_blob(&reshdr, wim, rdesc, cur_blob);
975 /* cur_blob is now a blob bound to a resource. */
977 /* Ignore entries with all zeroes in the hash field. */
978 if (unlikely(is_zero_hash(cur_blob->hash)))
979 goto free_cur_blob_and_continue;
981 /* Verify that the blob has nonzero size. */
982 if (unlikely(cur_blob->size == 0)) {
984 goto free_cur_blob_and_continue;
987 /* Verify that the part number matches that of the underlying
989 if (unlikely(part_number != wim->hdr.part_number)) {
990 num_wrong_part_blobs++;
991 goto free_cur_blob_and_continue;
994 if (reshdr.flags & WIM_RESHDR_FLAG_METADATA) {
996 cur_blob->is_metadata = 1;
998 /* Blob table entry for a metadata resource. */
1000 /* Metadata entries with no references must be ignored.
1001 * See, for example, the WinPE WIMs from the WAIK v2.1.
1003 if (cur_blob->refcnt == 0)
1004 goto free_cur_blob_and_continue;
1006 if (cur_blob->refcnt != 1) {
1007 /* We don't currently support this case due to
1008 * the complications of multiple images sharing
1009 * the same metadata resource or a metadata
1010 * resource also being referenced by files. */
1011 ERROR("Found metadata resource with refcnt != 1");
1012 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
1016 if (reshdr.flags & WIM_RESHDR_FLAG_SOLID) {
1017 ERROR("Image metadata in solid resources "
1019 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
1023 if (wim->hdr.part_number != 1) {
1024 WARNING("Ignoring metadata resource found in a "
1025 "non-first part of the split WIM");
1026 goto free_cur_blob_and_continue;
1029 /* The number of entries in the blob table with
1030 * WIM_RESHDR_FLAG_METADATA set should be the same as
1031 * the image_count field in the WIM header. */
1032 if (image_index == wim->hdr.image_count) {
1033 WARNING("Found more metadata resources than images");
1034 goto free_cur_blob_and_continue;
1037 /* Notice very carefully: We are assigning the metadata
1038 * resources to images in the same order in which their
1039 * blob table entries occur on disk. (This is also the
1040 * behavior of Microsoft's software.) In particular,
1041 * this overrides the actual locations of the metadata
1042 * resources themselves in the WIM file as well as any
1043 * information written in the XML data. */
1044 wim->image_metadata[image_index++]->metadata_blob = cur_blob;
1046 /* Blob table entry for a non-metadata blob. */
1048 /* Ignore this blob if it's a duplicate. */
1049 if (lookup_blob(table, cur_blob->hash)) {
1050 num_duplicate_blobs++;
1051 goto free_cur_blob_and_continue;
1054 /* Insert the blob into the in-memory blob table, keyed
1055 * by its SHA-1 message digest. */
1056 blob_table_insert(table, cur_blob);
1061 free_cur_blob_and_continue:
1062 if (cur_solid_rdescs &&
1063 cur_blob->blob_location == BLOB_IN_WIM)
1064 blob_unset_is_located_in_wim_resource(cur_blob);
1065 free_blob_descriptor(cur_blob);
1069 if (cur_solid_rdescs) {
1070 /* End of blob table terminated a solid run. */
1071 ret = finish_solid_rdescs(cur_solid_rdescs, cur_num_solid_rdescs);
1072 cur_solid_rdescs = NULL;
1077 if (wim->hdr.part_number == 1 && image_index != wim->hdr.image_count) {
1078 WARNING("Could not find metadata resources for all images");
1079 for (u32 i = image_index; i < wim->hdr.image_count; i++)
1080 put_image_metadata(wim->image_metadata[i], NULL);
1081 wim->hdr.image_count = image_index;
1084 if (num_duplicate_blobs > 0)
1085 WARNING("Ignoring %zu duplicate blobs", num_duplicate_blobs);
1087 if (num_empty_blobs > 0)
1088 WARNING("Ignoring %zu empty blobs", num_empty_blobs);
1090 if (num_wrong_part_blobs > 0) {
1091 WARNING("Ignoring %zu blobs with wrong part number",
1092 num_wrong_part_blobs);
1095 wim->blob_table = table;
1100 ERROR("Not enough memory to read blob table!");
1101 ret = WIMLIB_ERR_NOMEM;
1103 free_solid_rdescs(cur_solid_rdescs, cur_num_solid_rdescs);
1104 free_blob_descriptor(cur_blob);
1105 free_blob_table(table);
1112 write_blob_descriptor(struct blob_descriptor_disk *disk_entry,
1113 const struct wim_reshdr *out_reshdr,
1114 u16 part_number, u32 refcnt, const u8 *hash)
1116 put_wim_reshdr(out_reshdr, &disk_entry->reshdr);
1117 disk_entry->part_number = cpu_to_le16(part_number);
1118 disk_entry->refcnt = cpu_to_le32(refcnt);
1119 copy_hash(disk_entry->hash, hash);
1122 /* Note: the list of blob descriptors must be sorted so that all entries for the
1123 * same solid resource are consecutive. In addition, blob descriptors for
1124 * metadata resources must be in the same order as the indices of the underlying
1127 write_blob_table_from_blob_list(struct list_head *blob_list,
1128 struct filedes *out_fd,
1130 struct wim_reshdr *out_reshdr,
1131 int write_resource_flags)
1134 struct blob_descriptor *blob;
1135 struct blob_descriptor_disk *table_buf;
1136 struct blob_descriptor_disk *table_buf_ptr;
1138 u64 prev_res_offset_in_wim = ~0ULL;
1139 u64 prev_uncompressed_size;
1143 list_for_each_entry(blob, blob_list, blob_table_list) {
1144 table_size += sizeof(struct blob_descriptor_disk);
1146 if (blob->out_reshdr.flags & WIM_RESHDR_FLAG_SOLID &&
1147 blob->out_res_offset_in_wim != prev_res_offset_in_wim)
1149 table_size += sizeof(struct blob_descriptor_disk);
1150 prev_res_offset_in_wim = blob->out_res_offset_in_wim;
1154 table_buf = MALLOC(table_size);
1155 if (table_buf == NULL) {
1156 ERROR("Failed to allocate %zu bytes for temporary blob table",
1158 return WIMLIB_ERR_NOMEM;
1160 table_buf_ptr = table_buf;
1162 prev_res_offset_in_wim = ~0ULL;
1163 prev_uncompressed_size = 0;
1165 list_for_each_entry(blob, blob_list, blob_table_list) {
1166 if (blob->out_reshdr.flags & WIM_RESHDR_FLAG_SOLID) {
1167 struct wim_reshdr tmp_reshdr;
1169 /* Eww. When WIMGAPI sees multiple solid resources, it
1170 * expects the offsets to be adjusted as if there were
1171 * really only one solid resource. */
1173 if (blob->out_res_offset_in_wim != prev_res_offset_in_wim) {
1174 /* Put the resource entry for solid resource */
1175 tmp_reshdr.offset_in_wim = blob->out_res_offset_in_wim;
1176 tmp_reshdr.size_in_wim = blob->out_res_size_in_wim;
1177 tmp_reshdr.uncompressed_size = SOLID_RESOURCE_MAGIC_NUMBER;
1178 tmp_reshdr.flags = WIM_RESHDR_FLAG_SOLID;
1180 write_blob_descriptor(table_buf_ptr++, &tmp_reshdr,
1181 part_number, 1, zero_hash);
1183 logical_offset += prev_uncompressed_size;
1185 prev_res_offset_in_wim = blob->out_res_offset_in_wim;
1186 prev_uncompressed_size = blob->out_res_uncompressed_size;
1188 tmp_reshdr = blob->out_reshdr;
1189 tmp_reshdr.offset_in_wim += logical_offset;
1190 write_blob_descriptor(table_buf_ptr++, &tmp_reshdr,
1191 part_number, blob->out_refcnt, blob->hash);
1193 write_blob_descriptor(table_buf_ptr++, &blob->out_reshdr,
1194 part_number, blob->out_refcnt, blob->hash);
1198 wimlib_assert((u8*)table_buf_ptr - (u8*)table_buf == table_size);
1200 /* Write the blob table uncompressed. Although wimlib can handle a
1201 * compressed blob table, MS software cannot. */
1202 ret = write_wim_resource_from_buffer(table_buf,
1206 WIMLIB_COMPRESSION_TYPE_NONE,
1210 write_resource_flags);
1215 /* Allocate a blob descriptor for the contents of the buffer, or re-use an
1216 * existing descriptor in @blob_table for an identical blob. */
1217 struct blob_descriptor *
1218 new_blob_from_data_buffer(const void *buffer, size_t size,
1219 struct blob_table *blob_table)
1221 u8 hash[SHA1_HASH_SIZE];
1222 struct blob_descriptor *blob;
1225 sha1_buffer(buffer, size, hash);
1227 blob = lookup_blob(blob_table, hash);
1231 blob = new_blob_descriptor();
1235 buffer_copy = memdup(buffer, size);
1237 free_blob_descriptor(blob);
1240 blob_set_is_located_in_attached_buffer(blob, buffer_copy, size);
1241 copy_hash(blob->hash, hash);
1242 blob_table_insert(blob_table, blob);
1246 struct blob_descriptor *
1247 after_blob_hashed(struct blob_descriptor *blob,
1248 struct blob_descriptor **back_ptr,
1249 struct blob_table *blob_table)
1251 struct blob_descriptor *duplicate_blob;
1253 list_del(&blob->unhashed_list);
1256 /* Look for a duplicate blob */
1257 duplicate_blob = lookup_blob(blob_table, blob->hash);
1258 if (duplicate_blob) {
1259 /* We have a duplicate blob. Transfer the reference counts from
1260 * this blob to the duplicate and update the reference to this
1261 * blob (from a stream) to point to the duplicate. The caller
1262 * is responsible for freeing @blob if needed. */
1263 wimlib_assert(duplicate_blob->size == blob->size);
1264 duplicate_blob->refcnt += blob->refcnt;
1266 *back_ptr = duplicate_blob;
1267 return duplicate_blob;
1269 /* No duplicate blob, so we need to insert this blob into the
1270 * blob table and treat it as a hashed blob. */
1271 blob_table_insert(blob_table, blob);
1277 * Calculate the SHA-1 message digest of a blob and move its descriptor from the
1278 * list of unhashed blobs to the blob table, possibly joining it with an
1284 * The blob table in which the blob needs to be indexed
1286 * On success, a pointer to the resulting blob descriptor is written to
1287 * this location. This will be the same as @blob if it was inserted into
1288 * the blob table, or different if a duplicate blob was found.
1290 * Returns 0 on success; nonzero if there is an error reading the blob data.
1293 hash_unhashed_blob(struct blob_descriptor *blob, struct blob_table *blob_table,
1294 struct blob_descriptor **blob_ret)
1296 struct blob_descriptor **back_ptr;
1299 back_ptr = retrieve_pointer_to_unhashed_blob(blob);
1301 ret = sha1_blob(blob);
1305 *blob_ret = after_blob_hashed(blob, back_ptr, blob_table);
1310 blob_to_wimlib_resource_entry(const struct blob_descriptor *blob,
1311 struct wimlib_resource_entry *wentry)
1313 memset(wentry, 0, sizeof(*wentry));
1315 wentry->uncompressed_size = blob->size;
1316 if (blob->blob_location == BLOB_IN_WIM) {
1317 unsigned res_flags = blob->rdesc->flags;
1319 wentry->part_number = blob->rdesc->wim->hdr.part_number;
1320 if (res_flags & WIM_RESHDR_FLAG_SOLID) {
1321 wentry->offset = blob->offset_in_res;
1323 wentry->compressed_size = blob->rdesc->size_in_wim;
1324 wentry->offset = blob->rdesc->offset_in_wim;
1326 wentry->raw_resource_offset_in_wim = blob->rdesc->offset_in_wim;
1327 wentry->raw_resource_compressed_size = blob->rdesc->size_in_wim;
1328 wentry->raw_resource_uncompressed_size = blob->rdesc->uncompressed_size;
1330 wentry->is_compressed = (res_flags & WIM_RESHDR_FLAG_COMPRESSED) != 0;
1331 wentry->is_free = (res_flags & WIM_RESHDR_FLAG_FREE) != 0;
1332 wentry->is_spanned = (res_flags & WIM_RESHDR_FLAG_SPANNED) != 0;
1333 wentry->packed = (res_flags & WIM_RESHDR_FLAG_SOLID) != 0;
1335 if (!blob->unhashed)
1336 copy_hash(wentry->sha1_hash, blob->hash);
1337 wentry->reference_count = blob->refcnt;
1338 wentry->is_metadata = blob->is_metadata;
1341 struct iterate_blob_context {
1342 wimlib_iterate_lookup_table_callback_t cb;
1347 do_iterate_blob(struct blob_descriptor *blob, void *_ctx)
1349 struct iterate_blob_context *ctx = _ctx;
1350 struct wimlib_resource_entry entry;
1352 blob_to_wimlib_resource_entry(blob, &entry);
1353 return (*ctx->cb)(&entry, ctx->user_ctx);
1356 /* API function documented in wimlib.h */
1358 wimlib_iterate_lookup_table(WIMStruct *wim, int flags,
1359 wimlib_iterate_lookup_table_callback_t cb,
1363 return WIMLIB_ERR_INVALID_PARAM;
1365 struct iterate_blob_context ctx = {
1367 .user_ctx = user_ctx,
1369 if (wim_has_metadata(wim)) {
1371 for (int i = 0; i < wim->hdr.image_count; i++) {
1372 struct blob_descriptor *blob;
1373 struct wim_image_metadata *imd = wim->image_metadata[i];
1375 ret = do_iterate_blob(imd->metadata_blob, &ctx);
1378 image_for_each_unhashed_blob(blob, imd) {
1379 ret = do_iterate_blob(blob, &ctx);
1385 return for_blob_in_table(wim->blob_table, do_iterate_blob, &ctx);