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 struct blob_descriptor *blob;
104 blob = CALLOC(1, sizeof(struct blob_descriptor));
108 /* blob->refcnt = 0 */
109 /* blob->blob_location = BLOB_NONEXISTENT */
110 BUILD_BUG_ON(BLOB_NONEXISTENT != 0);
115 struct blob_descriptor *
116 clone_blob_descriptor(const struct blob_descriptor *old)
118 struct blob_descriptor *new;
120 new = memdup(old, sizeof(struct blob_descriptor));
124 switch (new->blob_location) {
126 list_add(&new->rdesc_node, &new->rdesc->blob_list);
129 case BLOB_IN_FILE_ON_DISK:
131 case BLOB_IN_WINNT_FILE_ON_DISK:
132 case BLOB_WIN32_ENCRYPTED:
135 case BLOB_IN_STAGING_FILE:
136 BUILD_BUG_ON((void*)&old->file_on_disk !=
137 (void*)&old->staging_file_name);
139 new->file_on_disk = TSTRDUP(old->file_on_disk);
140 if (new->file_on_disk == NULL)
143 case BLOB_IN_ATTACHED_BUFFER:
144 new->attached_buffer = memdup(old->attached_buffer, old->size);
145 if (new->attached_buffer == NULL)
149 case BLOB_IN_NTFS_VOLUME:
151 new->ntfs_loc = memdup(old->ntfs_loc,
152 sizeof(struct ntfs_location));
153 if (new->ntfs_loc == NULL)
155 new->ntfs_loc->path = STRDUP(old->ntfs_loc->path);
156 new->ntfs_loc->attr_name = NULL;
157 if (new->ntfs_loc->path == NULL)
159 if (new->ntfs_loc->attr_name_nchars != 0) {
160 new->ntfs_loc->attr_name =
161 utf16le_dup(old->ntfs_loc->attr_name);
162 if (new->ntfs_loc->attr_name == NULL)
174 free_blob_descriptor(new);
179 blob_release_location(struct blob_descriptor *blob)
181 switch (blob->blob_location) {
183 list_del(&blob->rdesc_node);
184 if (list_empty(&blob->rdesc->blob_list))
187 case BLOB_IN_FILE_ON_DISK:
189 case BLOB_IN_WINNT_FILE_ON_DISK:
190 case BLOB_WIN32_ENCRYPTED:
193 case BLOB_IN_STAGING_FILE:
194 BUILD_BUG_ON((void*)&blob->file_on_disk !=
195 (void*)&blob->staging_file_name);
197 case BLOB_IN_ATTACHED_BUFFER:
198 BUILD_BUG_ON((void*)&blob->file_on_disk !=
199 (void*)&blob->attached_buffer);
200 FREE(blob->file_on_disk);
203 case BLOB_IN_NTFS_VOLUME:
204 if (blob->ntfs_loc) {
205 FREE(blob->ntfs_loc->path);
206 FREE(blob->ntfs_loc->attr_name);
207 FREE(blob->ntfs_loc);
217 free_blob_descriptor(struct blob_descriptor *blob)
220 blob_release_location(blob);
225 /* Should this blob be retained even if it has no references? */
227 should_retain_blob(const struct blob_descriptor *blob)
229 return blob->blob_location == BLOB_IN_WIM;
233 finalize_blob(struct blob_descriptor *blob)
235 if (!should_retain_blob(blob))
236 free_blob_descriptor(blob);
240 * Decrements the reference count of the specified blob, which must be either
241 * (a) unhashed, or (b) inserted in the specified blob table.
243 * If the blob's reference count reaches 0, we may unlink it from @table and
244 * free it. However, we retain blobs with 0 reference count that originated
245 * from WIM files (BLOB_IN_WIM). We do this for two reasons:
247 * 1. This prevents information about valid blobs in a WIM file --- blobs which
248 * will continue to be present after appending to the WIM file --- from being
249 * lost merely because we dropped all references to them.
251 * 2. Blob reference counts we read from WIM files can't be trusted. It's
252 * possible that a WIM has reference counts that are too low; WIMGAPI
253 * sometimes creates WIMs where this is the case. It's also possible that
254 * blobs have been referenced from an external WIM; those blobs can
255 * potentially have any reference count at all, either lower or higher than
256 * would be expected for this WIM ("this WIM" meaning the owner of @table) if
257 * it were a standalone WIM.
259 * So we can't take the reference counts too seriously. But at least, we do
260 * recalculate by default when writing a new WIM file.
263 blob_decrement_refcnt(struct blob_descriptor *blob, struct blob_table *table)
265 blob_subtract_refcnt(blob, table, 1);
269 blob_subtract_refcnt(struct blob_descriptor *blob, struct blob_table *table,
272 if (unlikely(blob->refcnt < count)) {
273 blob->refcnt = 0; /* See comment above */
277 blob->refcnt -= count;
279 if (blob->refcnt != 0)
282 if (blob->unhashed) {
283 list_del(&blob->unhashed_list);
285 /* If the blob has been extracted to a staging file for a FUSE
286 * mount, unlink the staging file. (Note that there still may
287 * be open file descriptors to it.) */
288 if (blob->blob_location == BLOB_IN_STAGING_FILE)
289 unlinkat(blob->staging_dir_fd,
290 blob->staging_file_name, 0);
293 if (!should_retain_blob(blob))
294 blob_table_unlink(table, blob);
297 /* If FUSE mounts are enabled, then don't actually free the blob
298 * descriptor until the last file descriptor to it has been closed. */
300 if (blob->num_opened_fds == 0)
307 blob_decrement_num_opened_fds(struct blob_descriptor *blob)
309 wimlib_assert(blob->num_opened_fds != 0);
311 if (--blob->num_opened_fds == 0 && blob->refcnt == 0)
317 blob_table_insert_raw(struct blob_table *table, struct blob_descriptor *blob)
319 size_t i = blob->hash_short % table->capacity;
321 hlist_add_head(&blob->hash_list, &table->array[i]);
325 enlarge_blob_table(struct blob_table *table)
327 size_t old_capacity, new_capacity;
328 struct hlist_head *old_array, *new_array;
329 struct blob_descriptor *blob;
330 struct hlist_node *cur, *tmp;
333 old_capacity = table->capacity;
334 new_capacity = old_capacity * 2;
335 new_array = CALLOC(new_capacity, sizeof(struct hlist_head));
336 if (new_array == NULL)
338 old_array = table->array;
339 table->array = new_array;
340 table->capacity = new_capacity;
342 for (i = 0; i < old_capacity; i++) {
343 hlist_for_each_entry_safe(blob, cur, tmp, &old_array[i], hash_list) {
344 hlist_del(&blob->hash_list);
345 blob_table_insert_raw(table, blob);
351 /* Insert a blob descriptor into the blob table. */
353 blob_table_insert(struct blob_table *table, struct blob_descriptor *blob)
355 blob_table_insert_raw(table, blob);
356 if (++table->num_blobs > table->capacity)
357 enlarge_blob_table(table);
360 /* Unlinks a blob descriptor from the blob table; does not free it. */
362 blob_table_unlink(struct blob_table *table, struct blob_descriptor *blob)
364 wimlib_assert(!blob->unhashed);
365 wimlib_assert(table->num_blobs != 0);
367 hlist_del(&blob->hash_list);
371 /* Given a SHA-1 message digest, return the corresponding blob descriptor from
372 * the specified blob table, or NULL if there is none. */
373 struct blob_descriptor *
374 lookup_blob(const struct blob_table *table, const u8 *hash)
377 struct blob_descriptor *blob;
378 struct hlist_node *pos;
380 i = load_size_t_unaligned(hash) % table->capacity;
381 hlist_for_each_entry(blob, pos, &table->array[i], hash_list)
382 if (hashes_equal(hash, blob->hash))
387 /* Call a function on all blob descriptors in the specified blob table. Stop
388 * early and return nonzero if any call to the function returns nonzero. */
390 for_blob_in_table(struct blob_table *table,
391 int (*visitor)(struct blob_descriptor *, void *), void *arg)
393 struct blob_descriptor *blob;
394 struct hlist_node *pos, *tmp;
397 for (size_t i = 0; i < table->capacity; i++) {
398 hlist_for_each_entry_safe(blob, pos, tmp, &table->array[i],
401 ret = visitor(blob, arg);
410 * This is a qsort() callback that sorts blobs into an order optimized for
411 * reading. Sorting is done primarily by blob location, then secondarily by a
412 * location-dependent order. For example, blobs in WIM resources are sorted
413 * such that the underlying WIM files will be read sequentially. This is
414 * especially important for WIM files containing solid resources.
417 cmp_blobs_by_sequential_order(const void *p1, const void *p2)
419 const struct blob_descriptor *blob1, *blob2;
421 WIMStruct *wim1, *wim2;
423 blob1 = *(const struct blob_descriptor**)p1;
424 blob2 = *(const struct blob_descriptor**)p2;
426 v = (int)blob1->blob_location - (int)blob2->blob_location;
428 /* Different resource locations? */
432 switch (blob1->blob_location) {
434 wim1 = blob1->rdesc->wim;
435 wim2 = blob2->rdesc->wim;
437 /* Different (possibly split) WIMs? */
439 v = memcmp(wim1->hdr.guid, wim2->hdr.guid, WIM_GUID_LEN);
444 /* Different part numbers in the same WIM? */
445 v = (int)wim1->hdr.part_number - (int)wim2->hdr.part_number;
449 if (blob1->rdesc->offset_in_wim != blob2->rdesc->offset_in_wim)
450 return cmp_u64(blob1->rdesc->offset_in_wim,
451 blob2->rdesc->offset_in_wim);
453 return cmp_u64(blob1->offset_in_res, blob2->offset_in_res);
455 case BLOB_IN_FILE_ON_DISK:
457 case BLOB_IN_STAGING_FILE:
460 case BLOB_IN_WINNT_FILE_ON_DISK:
461 case BLOB_WIN32_ENCRYPTED:
463 /* Compare files by path: just a heuristic that will place files
464 * in the same directory next to each other. */
465 return tstrcmp(blob1->file_on_disk, blob2->file_on_disk);
467 case BLOB_IN_NTFS_VOLUME:
468 return tstrcmp(blob1->ntfs_loc->path, blob2->ntfs_loc->path);
471 /* No additional sorting order defined for this resource
472 * location (e.g. BLOB_IN_ATTACHED_BUFFER); simply compare
473 * everything equal to each other. */
479 sort_blob_list(struct list_head *blob_list, size_t list_head_offset,
480 int (*compar)(const void *, const void*))
482 struct list_head *cur;
483 struct blob_descriptor **array;
486 size_t num_blobs = 0;
488 list_for_each(cur, blob_list)
494 array_size = num_blobs * sizeof(array[0]);
495 array = MALLOC(array_size);
497 return WIMLIB_ERR_NOMEM;
499 cur = blob_list->next;
500 for (i = 0; i < num_blobs; i++) {
501 array[i] = (struct blob_descriptor*)((u8*)cur - list_head_offset);
505 qsort(array, num_blobs, sizeof(array[0]), compar);
507 INIT_LIST_HEAD(blob_list);
508 for (i = 0; i < num_blobs; i++) {
509 list_add_tail((struct list_head*)
510 ((u8*)array[i] + list_head_offset), blob_list);
516 /* Sort the specified list of blobs in an order optimized for sequential
519 sort_blob_list_by_sequential_order(struct list_head *blob_list,
520 size_t list_head_offset)
522 return sort_blob_list(blob_list, list_head_offset,
523 cmp_blobs_by_sequential_order);
527 add_blob_to_array(struct blob_descriptor *blob, void *_pp)
529 struct blob_descriptor ***pp = _pp;
534 /* Iterate through the blob descriptors in the specified blob table in an order
535 * optimized for sequential reading. */
537 for_blob_in_table_sorted_by_sequential_order(struct blob_table *table,
538 int (*visitor)(struct blob_descriptor *, void *),
541 struct blob_descriptor **blob_array, **p;
542 size_t num_blobs = table->num_blobs;
545 blob_array = MALLOC(num_blobs * sizeof(blob_array[0]));
547 return WIMLIB_ERR_NOMEM;
549 for_blob_in_table(table, add_blob_to_array, &p);
551 wimlib_assert(p == blob_array + num_blobs);
553 qsort(blob_array, num_blobs, sizeof(blob_array[0]),
554 cmp_blobs_by_sequential_order);
556 for (size_t i = 0; i < num_blobs; i++) {
557 ret = visitor(blob_array[i], arg);
565 /* On-disk format of a blob descriptor in a WIM file.
567 * Note: if the WIM file contains solid resource(s), then this structure is
568 * sometimes overloaded to describe a "resource" rather than a "blob". See the
569 * code for details. */
570 struct blob_descriptor_disk {
572 /* Size, offset, and flags of the blob. */
573 struct wim_reshdr_disk reshdr;
575 /* Which part of the split WIM this blob is in; indexed from 1. */
578 /* Reference count of this blob over all WIM images. (But see comment
579 * above blob_decrement_refcnt().) */
582 /* SHA-1 message digest of the uncompressed data of this blob, or all
583 * zeroes if this blob is of zero length. */
584 u8 hash[SHA1_HASH_SIZE];
587 /* Given a nonempty run of consecutive blob descriptors with the SOLID flag set,
588 * count how many specify resources (as opposed to blobs within those
591 * Returns the resulting count. */
593 count_solid_resources(const struct blob_descriptor_disk *entries, size_t max)
597 struct wim_reshdr reshdr;
599 get_wim_reshdr(&(entries++)->reshdr, &reshdr);
601 if (!(reshdr.flags & WIM_RESHDR_FLAG_SOLID)) {
602 /* Run was terminated by a stand-alone blob entry. */
606 if (reshdr.uncompressed_size == SOLID_RESOURCE_MAGIC_NUMBER) {
607 /* This is a resource entry. */
615 * Given a run of consecutive blob descriptors with the SOLID flag set and
616 * having @num_rdescs resource entries, load resource information from them into
617 * the resource descriptors in the @rdescs array.
619 * Returns 0 on success, or a nonzero error code on failure.
622 do_load_solid_info(WIMStruct *wim, struct wim_resource_descriptor **rdescs,
624 const struct blob_descriptor_disk *entries)
626 for (size_t i = 0; i < num_rdescs; i++) {
627 struct wim_reshdr reshdr;
628 struct alt_chunk_table_header_disk hdr;
629 struct wim_resource_descriptor *rdesc;
632 /* Advance to next resource entry. */
635 get_wim_reshdr(&(entries++)->reshdr, &reshdr);
636 } while (reshdr.uncompressed_size != SOLID_RESOURCE_MAGIC_NUMBER);
640 wim_res_hdr_to_desc(&reshdr, wim, rdesc);
642 /* For solid resources, the uncompressed size, compression type,
643 * and chunk size are stored in the resource itself, not in the
646 ret = full_pread(&wim->in_fd, &hdr,
647 sizeof(hdr), reshdr.offset_in_wim);
649 ERROR("Failed to read header of solid resource "
650 "(offset_in_wim=%"PRIu64")",
651 reshdr.offset_in_wim);
655 rdesc->uncompressed_size = le64_to_cpu(hdr.res_usize);
657 /* Compression format numbers must be the same as in
658 * WIMGAPI to be compatible here. */
659 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_NONE != 0);
660 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_XPRESS != 1);
661 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_LZX != 2);
662 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_LZMS != 3);
663 rdesc->compression_type = le32_to_cpu(hdr.compression_format);
665 rdesc->chunk_size = le32_to_cpu(hdr.chunk_size);
667 DEBUG("Solid resource %zu/%zu: %"PRIu64" => %"PRIu64" "
668 "(%"TS"/%"PRIu32") @ +%"PRIu64"",
670 rdesc->uncompressed_size,
672 wimlib_get_compression_type_string(rdesc->compression_type),
674 rdesc->offset_in_wim);
680 * Given a nonempty run of consecutive blob descriptors with the SOLID flag set,
681 * allocate a 'struct wim_resource_descriptor' for each resource within that
684 * Returns 0 on success, or a nonzero error code on failure.
685 * Returns the pointers and count in *rdescs_ret and *num_rdescs_ret.
688 load_solid_info(WIMStruct *wim,
689 const struct blob_descriptor_disk *entries,
690 size_t num_remaining_entries,
691 struct wim_resource_descriptor ***rdescs_ret,
692 size_t *num_rdescs_ret)
695 struct wim_resource_descriptor **rdescs;
699 num_rdescs = count_solid_resources(entries, num_remaining_entries);
700 rdescs = CALLOC(num_rdescs, sizeof(rdescs[0]));
702 return WIMLIB_ERR_NOMEM;
704 for (i = 0; i < num_rdescs; i++) {
705 rdescs[i] = MALLOC(sizeof(struct wim_resource_descriptor));
707 ret = WIMLIB_ERR_NOMEM;
708 goto out_free_rdescs;
712 ret = do_load_solid_info(wim, rdescs, num_rdescs, entries);
714 goto out_free_rdescs;
716 *rdescs_ret = rdescs;
717 *num_rdescs_ret = num_rdescs;
721 for (i = 0; i < num_rdescs; i++)
727 /* Given a 'struct blob_descriptor' allocated for an on-disk blob descriptor
728 * with the SOLID flag set, try to assign it to resource in the current solid
731 assign_blob_to_solid_resource(const struct wim_reshdr *reshdr,
732 struct blob_descriptor *blob,
733 struct wim_resource_descriptor **rdescs,
736 u64 offset = reshdr->offset_in_wim;
738 /* XXX: This linear search will be slow in the degenerate case where the
739 * number of solid resources in the run is huge. */
740 blob->size = reshdr->size_in_wim;
741 blob->flags = reshdr->flags;
742 for (size_t i = 0; i < num_rdescs; i++) {
743 if (offset + blob->size <= rdescs[i]->uncompressed_size) {
744 blob->offset_in_res = offset;
745 blob_set_is_located_in_wim_resource(blob, rdescs[i]);
748 offset -= rdescs[i]->uncompressed_size;
750 ERROR("blob could not be assigned to a solid resource");
751 return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
755 free_solid_rdescs(struct wim_resource_descriptor **rdescs, size_t num_rdescs)
758 for (size_t i = 0; i < num_rdescs; i++)
759 if (list_empty(&rdescs[i]->blob_list))
766 cmp_blobs_by_offset_in_res(const void *p1, const void *p2)
768 const struct blob_descriptor *blob1, *blob2;
770 blob1 = *(const struct blob_descriptor**)p1;
771 blob2 = *(const struct blob_descriptor**)p2;
773 return cmp_u64(blob1->offset_in_res, blob2->offset_in_res);
776 /* Validate the size and location of a WIM resource. */
778 validate_resource(struct wim_resource_descriptor *rdesc)
780 struct blob_descriptor *blob;
782 u64 expected_next_offset;
785 /* Verify that the resource itself has a valid offset and size. */
786 if (rdesc->offset_in_wim + rdesc->size_in_wim < rdesc->size_in_wim)
787 goto invalid_due_to_overflow;
789 /* Verify that each blob in the resource has a valid offset and size.
791 expected_next_offset = 0;
792 out_of_order = false;
793 list_for_each_entry(blob, &rdesc->blob_list, rdesc_node) {
794 if (blob->offset_in_res + blob->size < blob->size ||
795 blob->offset_in_res + blob->size > rdesc->uncompressed_size)
796 goto invalid_due_to_overflow;
798 if (blob->offset_in_res >= expected_next_offset)
799 expected_next_offset = blob->offset_in_res + blob->size;
804 /* If the blobs were not located at strictly increasing positions (not
805 * allowing for overlap), sort them. Then make sure that none overlap.
808 ret = sort_blob_list(&rdesc->blob_list,
809 offsetof(struct blob_descriptor,
811 cmp_blobs_by_offset_in_res);
815 expected_next_offset = 0;
816 list_for_each_entry(blob, &rdesc->blob_list, rdesc_node) {
817 if (blob->offset_in_res >= expected_next_offset)
818 expected_next_offset = blob->offset_in_res + blob->size;
820 goto invalid_due_to_overlap;
826 invalid_due_to_overflow:
827 ERROR("Invalid blob table (offset overflow)");
828 return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
830 invalid_due_to_overlap:
831 ERROR("Invalid blob table (blobs in solid resource overlap)");
832 return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
836 finish_solid_rdescs(struct wim_resource_descriptor **rdescs, size_t num_rdescs)
839 for (size_t i = 0; i < num_rdescs; i++) {
840 ret = validate_resource(rdescs[i]);
844 free_solid_rdescs(rdescs, num_rdescs);
849 * read_blob_table() -
851 * Read the blob table from a WIM file. Usually, each entry in this table
852 * describes a "blob", or equivalently a "resource", that the WIM file contains,
853 * along with its location and SHA-1 message digest. Descriptors for
854 * non-metadata blobs will be saved in the in-memory blob table
855 * (wim->blob_table), whereas descriptors for metadata blobs will be saved in a
856 * special location per-image (the wim->image_metadata array).
858 * However, in WIM_VERSION_SOLID (3584) WIMs, a resource may contain multiple
859 * blobs that are compressed together. Such a resource is called a "solid
860 * resource". Solid resources are still described in the on-disk "blob table",
861 * although the format is not the most logical. A consecutive sequence of
862 * entries that all have flag WIM_RESHDR_FLAG_SOLID (0x10) set is a "solid run".
863 * A solid run describes a set of solid resources, each of which contains a set
864 * of blobs. In a solid run, a 'struct wim_reshdr_disk' with 'uncompressed_size
865 * = SOLID_RESOURCE_MAGIC_NUMBER (0x100000000)' specifies a solid resource,
866 * whereas any other 'struct wim_reshdr_disk' specifies a blob within a solid
867 * resource. There are some oddities in how we need to determine which solid
868 * resource a blob is actually in; see the code for details.
870 * Possible return values:
871 * WIMLIB_ERR_SUCCESS (0)
872 * WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY
875 * Or an error code caused by failure to read the blob table from the WIM
879 read_blob_table(WIMStruct *wim)
884 struct blob_table *table = NULL;
885 struct blob_descriptor *cur_blob = NULL;
886 size_t num_duplicate_blobs = 0;
887 size_t num_wrong_part_blobs = 0;
889 struct wim_resource_descriptor **cur_solid_rdescs = NULL;
890 size_t cur_num_solid_rdescs = 0;
892 DEBUG("Reading blob table.");
894 /* Calculate the number of entries in the blob table. */
895 num_entries = wim->hdr.blob_table_reshdr.uncompressed_size /
896 sizeof(struct blob_descriptor_disk);
898 /* Read the blob table into a buffer. */
899 ret = wim_reshdr_to_data(&wim->hdr.blob_table_reshdr, wim, &buf);
903 /* Allocate a hash table to map SHA-1 message digests into blob
904 * descriptors. This is the in-memory "blob table". */
905 table = new_blob_table(num_entries * 2 + 1);
909 /* Allocate and initalize blob descriptors from the raw blob table
911 for (size_t i = 0; i < num_entries; i++) {
912 const struct blob_descriptor_disk *disk_entry =
913 &((const struct blob_descriptor_disk*)buf)[i];
914 struct wim_reshdr reshdr;
917 /* Get the resource header */
918 get_wim_reshdr(&disk_entry->reshdr, &reshdr);
920 DEBUG("reshdr: size_in_wim=%"PRIu64", "
921 "uncompressed_size=%"PRIu64", "
922 "offset_in_wim=%"PRIu64", "
924 reshdr.size_in_wim, reshdr.uncompressed_size,
925 reshdr.offset_in_wim, reshdr.flags);
927 /* Ignore SOLID flag if it isn't supposed to be used in this WIM
929 if (wim->hdr.wim_version == WIM_VERSION_DEFAULT)
930 reshdr.flags &= ~WIM_RESHDR_FLAG_SOLID;
932 /* Allocate a new 'struct blob_descriptor'. */
933 cur_blob = new_blob_descriptor();
937 /* Get the part number, reference count, and hash. */
938 part_number = le16_to_cpu(disk_entry->part_number);
939 cur_blob->refcnt = le32_to_cpu(disk_entry->refcnt);
940 copy_hash(cur_blob->hash, disk_entry->hash);
942 if (reshdr.flags & WIM_RESHDR_FLAG_SOLID) {
946 if (!cur_solid_rdescs) {
947 /* Starting new run */
948 ret = load_solid_info(wim, disk_entry,
951 &cur_num_solid_rdescs);
956 if (reshdr.uncompressed_size == SOLID_RESOURCE_MAGIC_NUMBER) {
957 /* Resource entry, not blob entry */
958 goto free_cur_blob_and_continue;
963 ret = assign_blob_to_solid_resource(&reshdr,
966 cur_num_solid_rdescs);
971 /* Normal blob/resource entry; SOLID not set. */
973 struct wim_resource_descriptor *rdesc;
975 if (unlikely(cur_solid_rdescs)) {
976 /* This entry terminated a solid run. */
977 ret = finish_solid_rdescs(cur_solid_rdescs,
978 cur_num_solid_rdescs);
979 cur_solid_rdescs = NULL;
984 /* How to handle an uncompressed resource with its
985 * uncompressed size different from its compressed size?
987 * Based on a simple test, WIMGAPI seems to handle this
990 * if (size_in_wim > uncompressed_size) {
991 * Ignore uncompressed_size; use size_in_wim
994 * Honor uncompressed_size, but treat the part of
995 * the file data above size_in_wim as all zeros.
998 * So we will do the same. */
999 if (unlikely(!(reshdr.flags &
1000 WIM_RESHDR_FLAG_COMPRESSED) &&
1001 (reshdr.size_in_wim >
1002 reshdr.uncompressed_size)))
1004 reshdr.uncompressed_size = reshdr.size_in_wim;
1007 /* Set up a resource descriptor for this blob. */
1009 rdesc = MALLOC(sizeof(struct wim_resource_descriptor));
1013 wim_res_hdr_to_desc(&reshdr, wim, rdesc);
1015 cur_blob->offset_in_res = 0;
1016 cur_blob->size = reshdr.uncompressed_size;
1017 cur_blob->flags = reshdr.flags;
1019 blob_set_is_located_in_wim_resource(cur_blob, rdesc);
1022 /* cur_blob is now a blob bound to a resource. */
1024 /* Ignore entries with all zeroes in the hash field. */
1025 if (is_zero_hash(cur_blob->hash))
1026 goto free_cur_blob_and_continue;
1028 /* Verify that the part number matches that of the underlying
1030 if (part_number != wim->hdr.part_number) {
1031 num_wrong_part_blobs++;
1032 goto free_cur_blob_and_continue;
1035 if (reshdr.flags & WIM_RESHDR_FLAG_METADATA) {
1037 /* Blob table entry for a metadata resource. */
1039 /* Metadata entries with no references must be ignored.
1040 * See, for example, the WinPE WIMs from the WAIK v2.1.
1042 if (cur_blob->refcnt == 0)
1043 goto free_cur_blob_and_continue;
1045 if (cur_blob->refcnt != 1) {
1046 /* We don't currently support this case due to
1047 * the complications of multiple images sharing
1048 * the same metadata resource or a metadata
1049 * resource also being referenced by files. */
1050 ERROR("Found metadata resource with refcnt != 1");
1051 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
1055 if (wim->hdr.part_number != 1) {
1056 WARNING("Ignoring metadata resource found in a "
1057 "non-first part of the split WIM");
1058 goto free_cur_blob_and_continue;
1061 /* The number of entries in the blob table with
1062 * WIM_RESHDR_FLAG_METADATA set should be the same as
1063 * the image_count field in the WIM header. */
1064 if (image_index == wim->hdr.image_count) {
1065 WARNING("Found more metadata resources than images");
1066 goto free_cur_blob_and_continue;
1069 /* Notice very carefully: We are assigning the metadata
1070 * resources to images in the same order in which their
1071 * blob table entries occur on disk. (This is also the
1072 * behavior of Microsoft's software.) In particular,
1073 * this overrides the actual locations of the metadata
1074 * resources themselves in the WIM file as well as any
1075 * information written in the XML data. */
1076 DEBUG("Found metadata resource for image %"PRIu32" at "
1077 "offset %"PRIu64".",
1079 reshdr.offset_in_wim);
1081 wim->image_metadata[image_index++]->metadata_blob = cur_blob;
1083 /* Blob table entry for a non-metadata blob. */
1085 /* Ignore this blob if it's a duplicate. */
1086 if (lookup_blob(table, cur_blob->hash)) {
1087 num_duplicate_blobs++;
1088 goto free_cur_blob_and_continue;
1091 /* Insert the blob into the in-memory blob table, keyed
1092 * by its SHA-1 message digest. */
1093 blob_table_insert(table, cur_blob);
1098 free_cur_blob_and_continue:
1099 if (cur_solid_rdescs &&
1100 cur_blob->blob_location == BLOB_IN_WIM)
1101 blob_unset_is_located_in_wim_resource(cur_blob);
1102 free_blob_descriptor(cur_blob);
1106 if (cur_solid_rdescs) {
1107 /* End of blob table terminated a solid run. */
1108 ret = finish_solid_rdescs(cur_solid_rdescs, cur_num_solid_rdescs);
1109 cur_solid_rdescs = NULL;
1114 if (wim->hdr.part_number == 1 && image_index != wim->hdr.image_count) {
1115 WARNING("Could not find metadata resources for all images");
1116 for (u32 i = image_index; i < wim->hdr.image_count; i++)
1117 put_image_metadata(wim->image_metadata[i], NULL);
1118 wim->hdr.image_count = image_index;
1121 if (num_duplicate_blobs > 0)
1122 WARNING("Ignoring %zu duplicate blobs", num_duplicate_blobs);
1124 if (num_wrong_part_blobs > 0) {
1125 WARNING("Ignoring %zu blobs with wrong part number",
1126 num_wrong_part_blobs);
1129 DEBUG("Done reading blob table.");
1130 wim->blob_table = table;
1135 ERROR("Not enough memory to read blob table!");
1136 ret = WIMLIB_ERR_NOMEM;
1138 free_solid_rdescs(cur_solid_rdescs, cur_num_solid_rdescs);
1139 free_blob_descriptor(cur_blob);
1140 free_blob_table(table);
1147 write_blob_descriptor(struct blob_descriptor_disk *disk_entry,
1148 const struct wim_reshdr *out_reshdr,
1149 u16 part_number, u32 refcnt, const u8 *hash)
1151 put_wim_reshdr(out_reshdr, &disk_entry->reshdr);
1152 disk_entry->part_number = cpu_to_le16(part_number);
1153 disk_entry->refcnt = cpu_to_le32(refcnt);
1154 copy_hash(disk_entry->hash, hash);
1157 /* Note: the list of blob descriptors must be sorted so that all entries for the
1158 * same solid resource are consecutive. In addition, blob descriptors with
1159 * WIM_RESHDR_FLAG_METADATA set must be in the same order as the indices of the
1160 * underlying images. */
1162 write_blob_table_from_blob_list(struct list_head *blob_list,
1163 struct filedes *out_fd,
1165 struct wim_reshdr *out_reshdr,
1166 int write_resource_flags)
1169 struct blob_descriptor *blob;
1170 struct blob_descriptor_disk *table_buf;
1171 struct blob_descriptor_disk *table_buf_ptr;
1173 u64 prev_res_offset_in_wim = ~0ULL;
1174 u64 prev_uncompressed_size;
1178 list_for_each_entry(blob, blob_list, blob_table_list) {
1179 table_size += sizeof(struct blob_descriptor_disk);
1181 if (blob->out_reshdr.flags & WIM_RESHDR_FLAG_SOLID &&
1182 blob->out_res_offset_in_wim != prev_res_offset_in_wim)
1184 table_size += sizeof(struct blob_descriptor_disk);
1185 prev_res_offset_in_wim = blob->out_res_offset_in_wim;
1189 DEBUG("Writing WIM blob table (size=%zu, offset=%"PRIu64")",
1190 table_size, out_fd->offset);
1192 table_buf = MALLOC(table_size);
1193 if (table_buf == NULL) {
1194 ERROR("Failed to allocate %zu bytes for temporary blob table",
1196 return WIMLIB_ERR_NOMEM;
1198 table_buf_ptr = table_buf;
1200 prev_res_offset_in_wim = ~0ULL;
1201 prev_uncompressed_size = 0;
1203 list_for_each_entry(blob, blob_list, blob_table_list) {
1204 if (blob->out_reshdr.flags & WIM_RESHDR_FLAG_SOLID) {
1205 struct wim_reshdr tmp_reshdr;
1207 /* Eww. When WIMGAPI sees multiple solid resources, it
1208 * expects the offsets to be adjusted as if there were
1209 * really only one solid resource. */
1211 if (blob->out_res_offset_in_wim != prev_res_offset_in_wim) {
1212 /* Put the resource entry for solid resource */
1213 tmp_reshdr.offset_in_wim = blob->out_res_offset_in_wim;
1214 tmp_reshdr.size_in_wim = blob->out_res_size_in_wim;
1215 tmp_reshdr.uncompressed_size = SOLID_RESOURCE_MAGIC_NUMBER;
1216 tmp_reshdr.flags = WIM_RESHDR_FLAG_SOLID;
1218 write_blob_descriptor(table_buf_ptr++, &tmp_reshdr,
1219 part_number, 1, zero_hash);
1221 logical_offset += prev_uncompressed_size;
1223 prev_res_offset_in_wim = blob->out_res_offset_in_wim;
1224 prev_uncompressed_size = blob->out_res_uncompressed_size;
1226 tmp_reshdr = blob->out_reshdr;
1227 tmp_reshdr.offset_in_wim += logical_offset;
1228 write_blob_descriptor(table_buf_ptr++, &tmp_reshdr,
1229 part_number, blob->out_refcnt, blob->hash);
1231 write_blob_descriptor(table_buf_ptr++, &blob->out_reshdr,
1232 part_number, blob->out_refcnt, blob->hash);
1236 wimlib_assert((u8*)table_buf_ptr - (u8*)table_buf == table_size);
1238 /* Write the blob table uncompressed. Although wimlib can handle a
1239 * compressed blob table, MS software cannot. */
1240 ret = write_wim_resource_from_buffer(table_buf,
1242 WIM_RESHDR_FLAG_METADATA,
1244 WIMLIB_COMPRESSION_TYPE_NONE,
1248 write_resource_flags);
1250 DEBUG("ret=%d", ret);
1254 /* Allocate a blob descriptor for the contents of the buffer, or re-use an
1255 * existing descriptor in @blob_table for an identical blob. */
1256 struct blob_descriptor *
1257 new_blob_from_data_buffer(const void *buffer, size_t size,
1258 struct blob_table *blob_table)
1260 u8 hash[SHA1_HASH_SIZE];
1261 struct blob_descriptor *blob, *existing_blob;
1263 sha1_buffer(buffer, size, hash);
1264 existing_blob = lookup_blob(blob_table, hash);
1265 if (existing_blob) {
1266 wimlib_assert(existing_blob->size == size);
1267 blob = existing_blob;
1270 blob = new_blob_descriptor();
1273 buffer_copy = memdup(buffer, size);
1274 if (buffer_copy == NULL) {
1275 free_blob_descriptor(blob);
1278 blob->blob_location = BLOB_IN_ATTACHED_BUFFER;
1279 blob->attached_buffer = buffer_copy;
1281 copy_hash(blob->hash, hash);
1282 blob_table_insert(blob_table, blob);
1287 struct blob_descriptor *
1288 after_blob_hashed(struct blob_descriptor *blob,
1289 struct blob_descriptor **back_ptr,
1290 struct blob_table *blob_table)
1292 struct blob_descriptor *duplicate_blob;
1294 list_del(&blob->unhashed_list);
1297 /* Look for a duplicate blob */
1298 duplicate_blob = lookup_blob(blob_table, blob->hash);
1299 if (duplicate_blob) {
1300 /* We have a duplicate blob. Transfer the reference counts from
1301 * this blob to the duplicate and update the reference to this
1302 * blob (from a stream) to point to the duplicate. The caller
1303 * is responsible for freeing @blob if needed. */
1304 wimlib_assert(duplicate_blob->size == blob->size);
1305 duplicate_blob->refcnt += blob->refcnt;
1307 *back_ptr = duplicate_blob;
1308 return duplicate_blob;
1310 /* No duplicate blob, so we need to insert this blob into the
1311 * blob table and treat it as a hashed blob. */
1312 blob_table_insert(blob_table, blob);
1318 * Calculate the SHA-1 message digest of a blob and move its descriptor from the
1319 * list of unhashed blobs to the blob table, possibly joining it with an
1325 * The blob table in which the blob needs to be indexed
1327 * On success, a pointer to the resulting blob descriptor is written to
1328 * this location. This will be the same as @blob if it was inserted into
1329 * the blob table, or different if a duplicate blob was found.
1331 * Returns 0 on success; nonzero if there is an error reading the blob data.
1334 hash_unhashed_blob(struct blob_descriptor *blob, struct blob_table *blob_table,
1335 struct blob_descriptor **blob_ret)
1337 struct blob_descriptor **back_ptr;
1340 back_ptr = retrieve_pointer_to_unhashed_blob(blob);
1342 ret = sha1_blob(blob);
1346 *blob_ret = after_blob_hashed(blob, back_ptr, blob_table);
1351 blob_to_wimlib_resource_entry(const struct blob_descriptor *blob,
1352 struct wimlib_resource_entry *wentry)
1354 memset(wentry, 0, sizeof(*wentry));
1356 wentry->uncompressed_size = blob->size;
1357 if (blob->blob_location == BLOB_IN_WIM) {
1358 wentry->part_number = blob->rdesc->wim->hdr.part_number;
1359 if (blob->flags & WIM_RESHDR_FLAG_SOLID) {
1360 wentry->offset = blob->offset_in_res;
1362 wentry->compressed_size = blob->rdesc->size_in_wim;
1363 wentry->offset = blob->rdesc->offset_in_wim;
1365 wentry->raw_resource_offset_in_wim = blob->rdesc->offset_in_wim;
1366 wentry->raw_resource_compressed_size = blob->rdesc->size_in_wim;
1367 wentry->raw_resource_uncompressed_size = blob->rdesc->uncompressed_size;
1369 copy_hash(wentry->sha1_hash, blob->hash);
1370 wentry->reference_count = blob->refcnt;
1371 wentry->is_compressed = (blob->flags & WIM_RESHDR_FLAG_COMPRESSED) != 0;
1372 wentry->is_metadata = (blob->flags & WIM_RESHDR_FLAG_METADATA) != 0;
1373 wentry->is_free = (blob->flags & WIM_RESHDR_FLAG_FREE) != 0;
1374 wentry->is_spanned = (blob->flags & WIM_RESHDR_FLAG_SPANNED) != 0;
1375 wentry->packed = (blob->flags & WIM_RESHDR_FLAG_SOLID) != 0;
1378 struct iterate_blob_context {
1379 wimlib_iterate_lookup_table_callback_t cb;
1384 do_iterate_blob(struct blob_descriptor *blob, void *_ctx)
1386 struct iterate_blob_context *ctx = _ctx;
1387 struct wimlib_resource_entry entry;
1389 blob_to_wimlib_resource_entry(blob, &entry);
1390 return (*ctx->cb)(&entry, ctx->user_ctx);
1393 /* API function documented in wimlib.h */
1395 wimlib_iterate_lookup_table(WIMStruct *wim, int flags,
1396 wimlib_iterate_lookup_table_callback_t cb,
1400 return WIMLIB_ERR_INVALID_PARAM;
1402 struct iterate_blob_context ctx = {
1404 .user_ctx = user_ctx,
1406 if (wim_has_metadata(wim)) {
1408 for (int i = 0; i < wim->hdr.image_count; i++) {
1409 ret = do_iterate_blob(wim->image_metadata[i]->metadata_blob,
1415 return for_blob_in_table(wim->blob_table, do_iterate_blob, &ctx);