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:
141 new->ntfs_loc = clone_ntfs_location(old->ntfs_loc);
150 free_blob_descriptor(new);
155 blob_release_location(struct blob_descriptor *blob)
157 switch (blob->blob_location) {
159 list_del(&blob->rdesc_node);
160 if (list_empty(&blob->rdesc->blob_list))
163 case BLOB_IN_FILE_ON_DISK:
165 case BLOB_IN_WINNT_FILE_ON_DISK:
166 case BLOB_WIN32_ENCRYPTED:
169 case BLOB_IN_STAGING_FILE:
170 BUILD_BUG_ON((void*)&blob->file_on_disk !=
171 (void*)&blob->staging_file_name);
173 case BLOB_IN_ATTACHED_BUFFER:
174 BUILD_BUG_ON((void*)&blob->file_on_disk !=
175 (void*)&blob->attached_buffer);
176 FREE(blob->file_on_disk);
179 case BLOB_IN_NTFS_VOLUME:
181 free_ntfs_location(blob->ntfs_loc);
188 free_blob_descriptor(struct blob_descriptor *blob)
191 blob_release_location(blob);
196 /* Should this blob be retained even if it has no references? */
198 should_retain_blob(const struct blob_descriptor *blob)
200 return blob->blob_location == BLOB_IN_WIM;
204 finalize_blob(struct blob_descriptor *blob)
206 if (!should_retain_blob(blob))
207 free_blob_descriptor(blob);
211 * Decrements the reference count of the specified blob, which must be either
212 * (a) unhashed, or (b) inserted in the specified blob table.
214 * If the blob's reference count reaches 0, we may unlink it from @table and
215 * free it. However, we retain blobs with 0 reference count that originated
216 * from WIM files (BLOB_IN_WIM). We do this for two reasons:
218 * 1. This prevents information about valid blobs in a WIM file --- blobs which
219 * will continue to be present after appending to the WIM file --- from being
220 * lost merely because we dropped all references to them.
222 * 2. Blob reference counts we read from WIM files can't be trusted. It's
223 * possible that a WIM has reference counts that are too low; WIMGAPI
224 * sometimes creates WIMs where this is the case. It's also possible that
225 * blobs have been referenced from an external WIM; those blobs can
226 * potentially have any reference count at all, either lower or higher than
227 * would be expected for this WIM ("this WIM" meaning the owner of @table) if
228 * it were a standalone WIM.
230 * So we can't take the reference counts too seriously. But at least, we do
231 * recalculate by default when writing a new WIM file.
234 blob_decrement_refcnt(struct blob_descriptor *blob, struct blob_table *table)
236 blob_subtract_refcnt(blob, table, 1);
240 blob_subtract_refcnt(struct blob_descriptor *blob, struct blob_table *table,
243 if (unlikely(blob->refcnt < count)) {
244 blob->refcnt = 0; /* See comment above */
248 blob->refcnt -= count;
250 if (blob->refcnt != 0)
253 if (blob->unhashed) {
254 list_del(&blob->unhashed_list);
256 /* If the blob has been extracted to a staging file for a FUSE
257 * mount, unlink the staging file. (Note that there still may
258 * be open file descriptors to it.) */
259 if (blob->blob_location == BLOB_IN_STAGING_FILE)
260 unlinkat(blob->staging_dir_fd,
261 blob->staging_file_name, 0);
264 if (!should_retain_blob(blob))
265 blob_table_unlink(table, blob);
268 /* If FUSE mounts are enabled, then don't actually free the blob
269 * descriptor until the last file descriptor to it has been closed. */
271 if (blob->num_opened_fds == 0)
278 blob_decrement_num_opened_fds(struct blob_descriptor *blob)
280 wimlib_assert(blob->num_opened_fds != 0);
282 if (--blob->num_opened_fds == 0 && blob->refcnt == 0)
288 blob_table_insert_raw(struct blob_table *table, struct blob_descriptor *blob)
290 size_t i = blob->hash_short % table->capacity;
292 hlist_add_head(&blob->hash_list, &table->array[i]);
296 enlarge_blob_table(struct blob_table *table)
298 size_t old_capacity, new_capacity;
299 struct hlist_head *old_array, *new_array;
300 struct blob_descriptor *blob;
301 struct hlist_node *tmp;
304 old_capacity = table->capacity;
305 new_capacity = old_capacity * 2;
306 new_array = CALLOC(new_capacity, sizeof(struct hlist_head));
307 if (new_array == NULL)
309 old_array = table->array;
310 table->array = new_array;
311 table->capacity = new_capacity;
313 for (i = 0; i < old_capacity; i++)
314 hlist_for_each_entry_safe(blob, tmp, &old_array[i], hash_list)
315 blob_table_insert_raw(table, blob);
319 /* Insert a blob descriptor into the blob table. */
321 blob_table_insert(struct blob_table *table, struct blob_descriptor *blob)
323 blob_table_insert_raw(table, blob);
324 if (++table->num_blobs > table->capacity)
325 enlarge_blob_table(table);
328 /* Unlinks a blob descriptor from the blob table; does not free it. */
330 blob_table_unlink(struct blob_table *table, struct blob_descriptor *blob)
332 wimlib_assert(!blob->unhashed);
333 wimlib_assert(table->num_blobs != 0);
335 hlist_del(&blob->hash_list);
339 /* Given a SHA-1 message digest, return the corresponding blob descriptor from
340 * the specified blob table, or NULL if there is none. */
341 struct blob_descriptor *
342 lookup_blob(const struct blob_table *table, const u8 *hash)
345 struct blob_descriptor *blob;
347 i = load_size_t_unaligned(hash) % table->capacity;
348 hlist_for_each_entry(blob, &table->array[i], hash_list)
349 if (hashes_equal(hash, blob->hash))
354 /* Call a function on all blob descriptors in the specified blob table. Stop
355 * early and return nonzero if any call to the function returns nonzero. */
357 for_blob_in_table(struct blob_table *table,
358 int (*visitor)(struct blob_descriptor *, void *), void *arg)
360 struct blob_descriptor *blob;
361 struct hlist_node *tmp;
364 for (size_t i = 0; i < table->capacity; i++) {
365 hlist_for_each_entry_safe(blob, tmp, &table->array[i],
368 ret = visitor(blob, arg);
377 * This is a qsort() callback that sorts blobs into an order optimized for
378 * reading. Sorting is done primarily by blob location, then secondarily by a
379 * location-dependent order. For example, blobs in WIM resources are sorted
380 * such that the underlying WIM files will be read sequentially. This is
381 * especially important for WIM files containing solid resources.
384 cmp_blobs_by_sequential_order(const void *p1, const void *p2)
386 const struct blob_descriptor *blob1, *blob2;
388 WIMStruct *wim1, *wim2;
390 blob1 = *(const struct blob_descriptor**)p1;
391 blob2 = *(const struct blob_descriptor**)p2;
393 v = (int)blob1->blob_location - (int)blob2->blob_location;
395 /* Different locations? */
399 switch (blob1->blob_location) {
401 wim1 = blob1->rdesc->wim;
402 wim2 = blob2->rdesc->wim;
404 /* Different (possibly split) WIMs? */
406 v = cmp_guids(wim1->hdr.guid, wim2->hdr.guid);
411 /* Different part numbers in the same WIM? */
412 v = (int)wim1->hdr.part_number - (int)wim2->hdr.part_number;
416 if (blob1->rdesc->offset_in_wim != blob2->rdesc->offset_in_wim)
417 return cmp_u64(blob1->rdesc->offset_in_wim,
418 blob2->rdesc->offset_in_wim);
420 return cmp_u64(blob1->offset_in_res, blob2->offset_in_res);
422 case BLOB_IN_FILE_ON_DISK:
424 case BLOB_IN_STAGING_FILE:
427 case BLOB_IN_WINNT_FILE_ON_DISK:
428 case BLOB_WIN32_ENCRYPTED:
429 /* Windows: compare by starting LCN (logical cluster number) */
430 v = cmp_u64(blob1->sort_key, blob2->sort_key);
434 /* Compare files by path: just a heuristic that will place files
435 * in the same directory next to each other. */
436 return tstrcmp(blob1->file_on_disk, blob2->file_on_disk);
438 case BLOB_IN_NTFS_VOLUME:
439 return cmp_ntfs_locations(blob1->ntfs_loc, blob2->ntfs_loc);
442 /* No additional sorting order defined for this resource
443 * location (e.g. BLOB_IN_ATTACHED_BUFFER); simply compare
444 * everything equal to each other. */
450 sort_blob_list(struct list_head *blob_list, size_t list_head_offset,
451 int (*compar)(const void *, const void*))
453 struct list_head *cur;
454 struct blob_descriptor **array;
457 size_t num_blobs = 0;
459 list_for_each(cur, blob_list)
465 array_size = num_blobs * sizeof(array[0]);
466 array = MALLOC(array_size);
468 return WIMLIB_ERR_NOMEM;
470 cur = blob_list->next;
471 for (i = 0; i < num_blobs; i++) {
472 array[i] = (struct blob_descriptor*)((u8*)cur - list_head_offset);
476 qsort(array, num_blobs, sizeof(array[0]), compar);
478 INIT_LIST_HEAD(blob_list);
479 for (i = 0; i < num_blobs; i++) {
480 list_add_tail((struct list_head*)
481 ((u8*)array[i] + list_head_offset), blob_list);
487 /* Sort the specified list of blobs in an order optimized for sequential
490 sort_blob_list_by_sequential_order(struct list_head *blob_list,
491 size_t list_head_offset)
493 return sort_blob_list(blob_list, list_head_offset,
494 cmp_blobs_by_sequential_order);
498 add_blob_to_array(struct blob_descriptor *blob, void *_pp)
500 struct blob_descriptor ***pp = _pp;
505 /* Iterate through the blob descriptors in the specified blob table in an order
506 * optimized for sequential reading. */
508 for_blob_in_table_sorted_by_sequential_order(struct blob_table *table,
509 int (*visitor)(struct blob_descriptor *, void *),
512 struct blob_descriptor **blob_array, **p;
513 size_t num_blobs = table->num_blobs;
516 blob_array = MALLOC(num_blobs * sizeof(blob_array[0]));
518 return WIMLIB_ERR_NOMEM;
520 for_blob_in_table(table, add_blob_to_array, &p);
522 wimlib_assert(p == blob_array + num_blobs);
524 qsort(blob_array, num_blobs, sizeof(blob_array[0]),
525 cmp_blobs_by_sequential_order);
527 for (size_t i = 0; i < num_blobs; i++) {
528 ret = visitor(blob_array[i], arg);
536 /* On-disk format of a blob descriptor in a WIM file.
538 * Note: if the WIM file contains solid resource(s), then this structure is
539 * sometimes overloaded to describe a "resource" rather than a "blob". See the
540 * code for details. */
541 struct blob_descriptor_disk {
543 /* Size, offset, and flags of the blob. */
544 struct wim_reshdr_disk reshdr;
546 /* Which part of the split WIM this blob is in; indexed from 1. */
549 /* Reference count of this blob over all WIM images. (But see comment
550 * above blob_decrement_refcnt().) */
553 /* SHA-1 message digest of the uncompressed data of this blob, or all
554 * zeroes if this blob is of zero length. */
555 u8 hash[SHA1_HASH_SIZE];
558 /* Given a nonempty run of consecutive blob descriptors with the SOLID flag set,
559 * count how many specify resources (as opposed to blobs within those
562 * Returns the resulting count. */
564 count_solid_resources(const struct blob_descriptor_disk *entries, size_t max)
568 struct wim_reshdr reshdr;
570 get_wim_reshdr(&(entries++)->reshdr, &reshdr);
572 if (!(reshdr.flags & WIM_RESHDR_FLAG_SOLID)) {
573 /* Run was terminated by a stand-alone blob entry. */
577 if (reshdr.uncompressed_size == SOLID_RESOURCE_MAGIC_NUMBER) {
578 /* This is a resource entry. */
586 * Given a run of consecutive blob descriptors with the SOLID flag set and
587 * having @num_rdescs resource entries, load resource information from them into
588 * the resource descriptors in the @rdescs array.
590 * Returns 0 on success, or a nonzero error code on failure.
593 do_load_solid_info(WIMStruct *wim, struct wim_resource_descriptor **rdescs,
595 const struct blob_descriptor_disk *entries)
597 for (size_t i = 0; i < num_rdescs; i++) {
598 struct wim_reshdr reshdr;
599 struct alt_chunk_table_header_disk hdr;
600 struct wim_resource_descriptor *rdesc;
603 /* Advance to next resource entry. */
606 get_wim_reshdr(&(entries++)->reshdr, &reshdr);
607 } while (reshdr.uncompressed_size != SOLID_RESOURCE_MAGIC_NUMBER);
611 wim_reshdr_to_desc(&reshdr, wim, rdesc);
613 /* For solid resources, the uncompressed size, compression type,
614 * and chunk size are stored in the resource itself, not in the
617 ret = full_pread(&wim->in_fd, &hdr,
618 sizeof(hdr), reshdr.offset_in_wim);
620 ERROR("Failed to read header of solid resource "
621 "(offset_in_wim=%"PRIu64")",
622 reshdr.offset_in_wim);
626 rdesc->uncompressed_size = le64_to_cpu(hdr.res_usize);
628 /* Compression format numbers must be the same as in
629 * WIMGAPI to be compatible here. */
630 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_NONE != 0);
631 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_XPRESS != 1);
632 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_LZX != 2);
633 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_LZMS != 3);
634 rdesc->compression_type = le32_to_cpu(hdr.compression_format);
635 rdesc->chunk_size = le32_to_cpu(hdr.chunk_size);
641 * Given a nonempty run of consecutive blob descriptors with the SOLID flag set,
642 * allocate a 'struct wim_resource_descriptor' for each resource within that
645 * Returns 0 on success, or a nonzero error code on failure.
646 * Returns the pointers and count in *rdescs_ret and *num_rdescs_ret.
649 load_solid_info(WIMStruct *wim,
650 const struct blob_descriptor_disk *entries,
651 size_t num_remaining_entries,
652 struct wim_resource_descriptor ***rdescs_ret,
653 size_t *num_rdescs_ret)
656 struct wim_resource_descriptor **rdescs;
660 num_rdescs = count_solid_resources(entries, num_remaining_entries);
661 rdescs = CALLOC(num_rdescs, sizeof(rdescs[0]));
663 return WIMLIB_ERR_NOMEM;
665 for (i = 0; i < num_rdescs; i++) {
666 rdescs[i] = MALLOC(sizeof(struct wim_resource_descriptor));
668 ret = WIMLIB_ERR_NOMEM;
669 goto out_free_rdescs;
673 ret = do_load_solid_info(wim, rdescs, num_rdescs, entries);
675 goto out_free_rdescs;
677 *rdescs_ret = rdescs;
678 *num_rdescs_ret = num_rdescs;
682 for (i = 0; i < num_rdescs; i++)
688 /* Given a 'struct blob_descriptor' allocated for an on-disk blob descriptor
689 * with the SOLID flag set, try to assign it to resource in the current solid
692 assign_blob_to_solid_resource(const struct wim_reshdr *reshdr,
693 struct blob_descriptor *blob,
694 struct wim_resource_descriptor **rdescs,
697 u64 offset = reshdr->offset_in_wim;
699 /* XXX: This linear search will be slow in the degenerate case where the
700 * number of solid resources in the run is huge. */
701 blob->size = reshdr->size_in_wim;
702 for (size_t i = 0; i < num_rdescs; i++) {
703 if (offset + blob->size <= rdescs[i]->uncompressed_size) {
704 blob_set_is_located_in_wim_resource(blob, rdescs[i], offset);
707 offset -= rdescs[i]->uncompressed_size;
709 ERROR("blob could not be assigned to a solid resource");
710 return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
714 free_solid_rdescs(struct wim_resource_descriptor **rdescs, size_t num_rdescs)
717 for (size_t i = 0; i < num_rdescs; i++)
718 if (list_empty(&rdescs[i]->blob_list))
725 cmp_blobs_by_offset_in_res(const void *p1, const void *p2)
727 const struct blob_descriptor *blob1, *blob2;
729 blob1 = *(const struct blob_descriptor**)p1;
730 blob2 = *(const struct blob_descriptor**)p2;
732 return cmp_u64(blob1->offset_in_res, blob2->offset_in_res);
735 /* Validate the size and location of a WIM resource. */
737 validate_resource(struct wim_resource_descriptor *rdesc)
739 struct blob_descriptor *blob;
741 u64 expected_next_offset;
744 /* Verify that the resource itself has a valid offset and size. */
745 if (rdesc->offset_in_wim + rdesc->size_in_wim < rdesc->size_in_wim)
746 goto invalid_due_to_overflow;
748 /* Verify that each blob in the resource has a valid offset and size.
750 expected_next_offset = 0;
751 out_of_order = false;
752 list_for_each_entry(blob, &rdesc->blob_list, rdesc_node) {
753 if (blob->offset_in_res + blob->size < blob->size ||
754 blob->offset_in_res + blob->size > rdesc->uncompressed_size)
755 goto invalid_due_to_overflow;
757 if (blob->offset_in_res >= expected_next_offset)
758 expected_next_offset = blob->offset_in_res + blob->size;
763 /* If the blobs were not located at strictly increasing positions (not
764 * allowing for overlap), sort them. Then make sure that none overlap.
767 ret = sort_blob_list(&rdesc->blob_list,
768 offsetof(struct blob_descriptor,
770 cmp_blobs_by_offset_in_res);
774 expected_next_offset = 0;
775 list_for_each_entry(blob, &rdesc->blob_list, rdesc_node) {
776 if (blob->offset_in_res >= expected_next_offset)
777 expected_next_offset = blob->offset_in_res + blob->size;
779 goto invalid_due_to_overlap;
785 invalid_due_to_overflow:
786 ERROR("Invalid blob table (offset overflow)");
787 return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
789 invalid_due_to_overlap:
790 ERROR("Invalid blob table (blobs in solid resource overlap)");
791 return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
795 finish_solid_rdescs(struct wim_resource_descriptor **rdescs, size_t num_rdescs)
798 for (size_t i = 0; i < num_rdescs; i++) {
799 ret = validate_resource(rdescs[i]);
803 free_solid_rdescs(rdescs, num_rdescs);
808 * read_blob_table() -
810 * Read the blob table from a WIM file. Usually, each entry in this table
811 * describes a "blob", or equivalently a "resource", that the WIM file contains,
812 * along with its location and SHA-1 message digest. Descriptors for
813 * non-metadata blobs will be saved in the in-memory blob table
814 * (wim->blob_table), whereas descriptors for metadata blobs will be saved in a
815 * special location per-image (the wim->image_metadata array).
817 * However, in WIM_VERSION_SOLID (3584) WIMs, a resource may contain multiple
818 * blobs that are compressed together. Such a resource is called a "solid
819 * resource". Solid resources are still described in the on-disk "blob table",
820 * although the format is not the most logical. A consecutive sequence of
821 * entries that all have flag WIM_RESHDR_FLAG_SOLID (0x10) set is a "solid run".
822 * A solid run describes a set of solid resources, each of which contains a set
823 * of blobs. In a solid run, a 'struct wim_reshdr_disk' with 'uncompressed_size
824 * = SOLID_RESOURCE_MAGIC_NUMBER (0x100000000)' specifies a solid resource,
825 * whereas any other 'struct wim_reshdr_disk' specifies a blob within a solid
826 * resource. There are some oddities in how we need to determine which solid
827 * resource a blob is actually in; see the code for details.
829 * Possible return values:
830 * WIMLIB_ERR_SUCCESS (0)
831 * WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY
834 * Or an error code caused by failure to read the blob table from the WIM
838 read_blob_table(WIMStruct *wim)
843 struct blob_table *table = NULL;
844 struct blob_descriptor *cur_blob = NULL;
845 size_t num_duplicate_blobs = 0;
846 size_t num_empty_blobs = 0;
847 size_t num_wrong_part_blobs = 0;
849 struct wim_resource_descriptor **cur_solid_rdescs = NULL;
850 size_t cur_num_solid_rdescs = 0;
852 /* Calculate the number of entries in the blob table. */
853 num_entries = wim->hdr.blob_table_reshdr.uncompressed_size /
854 sizeof(struct blob_descriptor_disk);
856 /* Read the blob table into a buffer. */
857 ret = wim_reshdr_to_data(&wim->hdr.blob_table_reshdr, wim, &buf);
861 /* Allocate a hash table to map SHA-1 message digests into blob
862 * descriptors. This is the in-memory "blob table". */
863 table = new_blob_table(num_entries * 2 + 1);
867 /* Allocate and initalize blob descriptors from the raw blob table
869 for (size_t i = 0; i < num_entries; i++) {
870 const struct blob_descriptor_disk *disk_entry =
871 &((const struct blob_descriptor_disk*)buf)[i];
872 struct wim_reshdr reshdr;
875 /* Get the resource header */
876 get_wim_reshdr(&disk_entry->reshdr, &reshdr);
878 /* Ignore SOLID flag if it isn't supposed to be used in this WIM
880 if (wim->hdr.wim_version == WIM_VERSION_DEFAULT)
881 reshdr.flags &= ~WIM_RESHDR_FLAG_SOLID;
883 /* Allocate a new 'struct blob_descriptor'. */
884 cur_blob = new_blob_descriptor();
888 /* Get the part number, reference count, and hash. */
889 part_number = le16_to_cpu(disk_entry->part_number);
890 cur_blob->refcnt = le32_to_cpu(disk_entry->refcnt);
891 copy_hash(cur_blob->hash, disk_entry->hash);
893 if (reshdr.flags & WIM_RESHDR_FLAG_SOLID) {
897 if (!cur_solid_rdescs) {
898 /* Starting new run */
899 ret = load_solid_info(wim, disk_entry,
902 &cur_num_solid_rdescs);
907 if (reshdr.uncompressed_size == SOLID_RESOURCE_MAGIC_NUMBER) {
908 /* Resource entry, not blob entry */
909 goto free_cur_blob_and_continue;
914 ret = assign_blob_to_solid_resource(&reshdr,
917 cur_num_solid_rdescs);
922 /* Normal blob/resource entry; SOLID not set. */
924 struct wim_resource_descriptor *rdesc;
926 if (unlikely(cur_solid_rdescs)) {
927 /* This entry terminated a solid run. */
928 ret = finish_solid_rdescs(cur_solid_rdescs,
929 cur_num_solid_rdescs);
930 cur_solid_rdescs = NULL;
935 if (unlikely(!(reshdr.flags & WIM_RESHDR_FLAG_COMPRESSED) &&
936 (reshdr.size_in_wim != reshdr.uncompressed_size)))
938 ERROR("Uncompressed resource has "
939 "size_in_wim != uncompressed_size");
940 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
944 /* Set up a resource descriptor for this blob. */
946 rdesc = MALLOC(sizeof(struct wim_resource_descriptor));
950 wim_reshdr_to_desc_and_blob(&reshdr, wim, rdesc, cur_blob);
953 /* cur_blob is now a blob bound to a resource. */
955 /* Ignore entries with all zeroes in the hash field. */
956 if (unlikely(is_zero_hash(cur_blob->hash)))
957 goto free_cur_blob_and_continue;
959 /* Verify that the blob has nonzero size. */
960 if (unlikely(cur_blob->size == 0)) {
962 goto free_cur_blob_and_continue;
965 /* Verify that the part number matches that of the underlying
967 if (unlikely(part_number != wim->hdr.part_number)) {
968 num_wrong_part_blobs++;
969 goto free_cur_blob_and_continue;
972 if (reshdr.flags & WIM_RESHDR_FLAG_METADATA) {
974 cur_blob->is_metadata = 1;
976 /* Blob table entry for a metadata resource. */
978 /* Metadata entries with no references must be ignored.
979 * See, for example, the WinPE WIMs from the WAIK v2.1.
981 if (cur_blob->refcnt == 0)
982 goto free_cur_blob_and_continue;
984 if (cur_blob->refcnt != 1) {
985 /* We don't currently support this case due to
986 * the complications of multiple images sharing
987 * the same metadata resource or a metadata
988 * resource also being referenced by files. */
989 ERROR("Found metadata resource with refcnt != 1");
990 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
994 if (reshdr.flags & WIM_RESHDR_FLAG_SOLID) {
995 ERROR("Image metadata in solid resources "
997 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
1001 if (wim->hdr.part_number != 1) {
1002 WARNING("Ignoring metadata resource found in a "
1003 "non-first part of the split WIM");
1004 goto free_cur_blob_and_continue;
1007 /* The number of entries in the blob table with
1008 * WIM_RESHDR_FLAG_METADATA set should be the same as
1009 * the image_count field in the WIM header. */
1010 if (image_index == wim->hdr.image_count) {
1011 WARNING("Found more metadata resources than images");
1012 goto free_cur_blob_and_continue;
1015 /* Notice very carefully: We are assigning the metadata
1016 * resources to images in the same order in which their
1017 * blob table entries occur on disk. (This is also the
1018 * behavior of Microsoft's software.) In particular,
1019 * this overrides the actual locations of the metadata
1020 * resources themselves in the WIM file as well as any
1021 * information written in the XML data. */
1022 wim->image_metadata[image_index++]->metadata_blob = cur_blob;
1024 /* Blob table entry for a non-metadata blob. */
1026 /* Ignore this blob if it's a duplicate. */
1027 if (lookup_blob(table, cur_blob->hash)) {
1028 num_duplicate_blobs++;
1029 goto free_cur_blob_and_continue;
1032 /* Insert the blob into the in-memory blob table, keyed
1033 * by its SHA-1 message digest. */
1034 blob_table_insert(table, cur_blob);
1039 free_cur_blob_and_continue:
1040 if (cur_solid_rdescs &&
1041 cur_blob->blob_location == BLOB_IN_WIM)
1042 blob_unset_is_located_in_wim_resource(cur_blob);
1043 free_blob_descriptor(cur_blob);
1047 if (cur_solid_rdescs) {
1048 /* End of blob table terminated a solid run. */
1049 ret = finish_solid_rdescs(cur_solid_rdescs, cur_num_solid_rdescs);
1050 cur_solid_rdescs = NULL;
1055 if (wim->hdr.part_number == 1 && image_index != wim->hdr.image_count) {
1056 WARNING("Could not find metadata resources for all images");
1057 for (u32 i = image_index; i < wim->hdr.image_count; i++)
1058 put_image_metadata(wim->image_metadata[i], NULL);
1059 wim->hdr.image_count = image_index;
1062 if (num_duplicate_blobs > 0)
1063 WARNING("Ignoring %zu duplicate blobs", num_duplicate_blobs);
1065 if (num_empty_blobs > 0)
1066 WARNING("Ignoring %zu empty blobs", num_empty_blobs);
1068 if (num_wrong_part_blobs > 0) {
1069 WARNING("Ignoring %zu blobs with wrong part number",
1070 num_wrong_part_blobs);
1073 wim->blob_table = table;
1078 ERROR("Not enough memory to read blob table!");
1079 ret = WIMLIB_ERR_NOMEM;
1081 free_solid_rdescs(cur_solid_rdescs, cur_num_solid_rdescs);
1082 free_blob_descriptor(cur_blob);
1083 free_blob_table(table);
1090 write_blob_descriptor(struct blob_descriptor_disk *disk_entry,
1091 const struct wim_reshdr *out_reshdr,
1092 u16 part_number, u32 refcnt, const u8 *hash)
1094 put_wim_reshdr(out_reshdr, &disk_entry->reshdr);
1095 disk_entry->part_number = cpu_to_le16(part_number);
1096 disk_entry->refcnt = cpu_to_le32(refcnt);
1097 copy_hash(disk_entry->hash, hash);
1100 /* Note: the list of blob descriptors must be sorted so that all entries for the
1101 * same solid resource are consecutive. In addition, blob descriptors for
1102 * metadata resources must be in the same order as the indices of the underlying
1105 write_blob_table_from_blob_list(struct list_head *blob_list,
1106 struct filedes *out_fd,
1108 struct wim_reshdr *out_reshdr,
1109 int write_resource_flags)
1112 struct blob_descriptor *blob;
1113 struct blob_descriptor_disk *table_buf;
1114 struct blob_descriptor_disk *table_buf_ptr;
1116 u64 prev_res_offset_in_wim = ~0ULL;
1117 u64 prev_uncompressed_size;
1121 list_for_each_entry(blob, blob_list, blob_table_list) {
1122 table_size += sizeof(struct blob_descriptor_disk);
1124 if (blob->out_reshdr.flags & WIM_RESHDR_FLAG_SOLID &&
1125 blob->out_res_offset_in_wim != prev_res_offset_in_wim)
1127 table_size += sizeof(struct blob_descriptor_disk);
1128 prev_res_offset_in_wim = blob->out_res_offset_in_wim;
1132 table_buf = MALLOC(table_size);
1133 if (table_buf == NULL) {
1134 ERROR("Failed to allocate %zu bytes for temporary blob table",
1136 return WIMLIB_ERR_NOMEM;
1138 table_buf_ptr = table_buf;
1140 prev_res_offset_in_wim = ~0ULL;
1141 prev_uncompressed_size = 0;
1143 list_for_each_entry(blob, blob_list, blob_table_list) {
1144 if (blob->out_reshdr.flags & WIM_RESHDR_FLAG_SOLID) {
1145 struct wim_reshdr tmp_reshdr;
1147 /* Eww. When WIMGAPI sees multiple solid resources, it
1148 * expects the offsets to be adjusted as if there were
1149 * really only one solid resource. */
1151 if (blob->out_res_offset_in_wim != prev_res_offset_in_wim) {
1152 /* Put the resource entry for solid resource */
1153 tmp_reshdr.offset_in_wim = blob->out_res_offset_in_wim;
1154 tmp_reshdr.size_in_wim = blob->out_res_size_in_wim;
1155 tmp_reshdr.uncompressed_size = SOLID_RESOURCE_MAGIC_NUMBER;
1156 tmp_reshdr.flags = WIM_RESHDR_FLAG_SOLID;
1158 write_blob_descriptor(table_buf_ptr++, &tmp_reshdr,
1159 part_number, 1, zero_hash);
1161 logical_offset += prev_uncompressed_size;
1163 prev_res_offset_in_wim = blob->out_res_offset_in_wim;
1164 prev_uncompressed_size = blob->out_res_uncompressed_size;
1166 tmp_reshdr = blob->out_reshdr;
1167 tmp_reshdr.offset_in_wim += logical_offset;
1168 write_blob_descriptor(table_buf_ptr++, &tmp_reshdr,
1169 part_number, blob->out_refcnt, blob->hash);
1171 write_blob_descriptor(table_buf_ptr++, &blob->out_reshdr,
1172 part_number, blob->out_refcnt, blob->hash);
1176 wimlib_assert((u8*)table_buf_ptr - (u8*)table_buf == table_size);
1178 /* Write the blob table uncompressed. Although wimlib can handle a
1179 * compressed blob table, MS software cannot. */
1180 ret = write_wim_resource_from_buffer(table_buf,
1184 WIMLIB_COMPRESSION_TYPE_NONE,
1188 write_resource_flags);
1193 /* Allocate a blob descriptor for the contents of the buffer, or re-use an
1194 * existing descriptor in @blob_table for an identical blob. */
1195 struct blob_descriptor *
1196 new_blob_from_data_buffer(const void *buffer, size_t size,
1197 struct blob_table *blob_table)
1199 u8 hash[SHA1_HASH_SIZE];
1200 struct blob_descriptor *blob;
1203 sha1_buffer(buffer, size, hash);
1205 blob = lookup_blob(blob_table, hash);
1209 blob = new_blob_descriptor();
1213 buffer_copy = memdup(buffer, size);
1215 free_blob_descriptor(blob);
1218 blob_set_is_located_in_attached_buffer(blob, buffer_copy, size);
1219 copy_hash(blob->hash, hash);
1220 blob_table_insert(blob_table, blob);
1224 struct blob_descriptor *
1225 after_blob_hashed(struct blob_descriptor *blob,
1226 struct blob_descriptor **back_ptr,
1227 struct blob_table *blob_table)
1229 struct blob_descriptor *duplicate_blob;
1231 list_del(&blob->unhashed_list);
1234 /* Look for a duplicate blob */
1235 duplicate_blob = lookup_blob(blob_table, blob->hash);
1236 if (duplicate_blob) {
1237 /* We have a duplicate blob. Transfer the reference counts from
1238 * this blob to the duplicate and update the reference to this
1239 * blob (from a stream) to point to the duplicate. The caller
1240 * is responsible for freeing @blob if needed. */
1241 wimlib_assert(duplicate_blob->size == blob->size);
1242 duplicate_blob->refcnt += blob->refcnt;
1244 *back_ptr = duplicate_blob;
1245 return duplicate_blob;
1247 /* No duplicate blob, so we need to insert this blob into the
1248 * blob table and treat it as a hashed blob. */
1249 blob_table_insert(blob_table, blob);
1255 * Calculate the SHA-1 message digest of a blob and move its descriptor from the
1256 * list of unhashed blobs to the blob table, possibly joining it with an
1262 * The blob table in which the blob needs to be indexed
1264 * On success, a pointer to the resulting blob descriptor is written to
1265 * this location. This will be the same as @blob if it was inserted into
1266 * the blob table, or different if a duplicate blob was found.
1268 * Returns 0 on success; nonzero if there is an error reading the blob data.
1271 hash_unhashed_blob(struct blob_descriptor *blob, struct blob_table *blob_table,
1272 struct blob_descriptor **blob_ret)
1274 struct blob_descriptor **back_ptr;
1277 back_ptr = retrieve_pointer_to_unhashed_blob(blob);
1279 ret = sha1_blob(blob);
1283 *blob_ret = after_blob_hashed(blob, back_ptr, blob_table);
1288 blob_to_wimlib_resource_entry(const struct blob_descriptor *blob,
1289 struct wimlib_resource_entry *wentry)
1291 memset(wentry, 0, sizeof(*wentry));
1293 wentry->uncompressed_size = blob->size;
1294 if (blob->blob_location == BLOB_IN_WIM) {
1295 unsigned res_flags = blob->rdesc->flags;
1297 wentry->part_number = blob->rdesc->wim->hdr.part_number;
1298 if (res_flags & WIM_RESHDR_FLAG_SOLID) {
1299 wentry->offset = blob->offset_in_res;
1301 wentry->compressed_size = blob->rdesc->size_in_wim;
1302 wentry->offset = blob->rdesc->offset_in_wim;
1304 wentry->raw_resource_offset_in_wim = blob->rdesc->offset_in_wim;
1305 wentry->raw_resource_compressed_size = blob->rdesc->size_in_wim;
1306 wentry->raw_resource_uncompressed_size = blob->rdesc->uncompressed_size;
1308 wentry->is_compressed = (res_flags & WIM_RESHDR_FLAG_COMPRESSED) != 0;
1309 wentry->is_free = (res_flags & WIM_RESHDR_FLAG_FREE) != 0;
1310 wentry->is_spanned = (res_flags & WIM_RESHDR_FLAG_SPANNED) != 0;
1311 wentry->packed = (res_flags & WIM_RESHDR_FLAG_SOLID) != 0;
1313 if (!blob->unhashed)
1314 copy_hash(wentry->sha1_hash, blob->hash);
1315 wentry->reference_count = blob->refcnt;
1316 wentry->is_metadata = blob->is_metadata;
1319 struct iterate_blob_context {
1320 wimlib_iterate_lookup_table_callback_t cb;
1325 do_iterate_blob(struct blob_descriptor *blob, void *_ctx)
1327 struct iterate_blob_context *ctx = _ctx;
1328 struct wimlib_resource_entry entry;
1330 blob_to_wimlib_resource_entry(blob, &entry);
1331 return (*ctx->cb)(&entry, ctx->user_ctx);
1334 /* API function documented in wimlib.h */
1336 wimlib_iterate_lookup_table(WIMStruct *wim, int flags,
1337 wimlib_iterate_lookup_table_callback_t cb,
1341 return WIMLIB_ERR_INVALID_PARAM;
1343 struct iterate_blob_context ctx = {
1345 .user_ctx = user_ctx,
1347 if (wim_has_metadata(wim)) {
1349 for (int i = 0; i < wim->hdr.image_count; i++) {
1350 struct blob_descriptor *blob;
1351 struct wim_image_metadata *imd = wim->image_metadata[i];
1353 ret = do_iterate_blob(imd->metadata_blob, &ctx);
1356 image_for_each_unhashed_blob(blob, imd) {
1357 ret = do_iterate_blob(blob, &ctx);
1363 return for_blob_in_table(wim->blob_table, do_iterate_blob, &ctx);