4 * Lookup table, implemented as a hash table, that maps SHA1 message digests to
5 * data streams; plus code to read and write the corresponding on-disk data.
9 * Copyright (C) 2012, 2013 Eric Biggers
11 * This file is part of wimlib, a library for working with WIM files.
13 * wimlib is free software; you can redistribute it and/or modify it under the
14 * terms of the GNU General Public License as published by the Free
15 * Software Foundation; either version 3 of the License, or (at your option)
18 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 * A PARTICULAR PURPOSE. See the GNU General Public License for more
23 * You should have received a copy of the GNU General Public License
24 * along with wimlib; if not, see http://www.gnu.org/licenses/.
31 #include "wimlib/assert.h"
32 #include "wimlib/endianness.h"
33 #include "wimlib/error.h"
34 #include "wimlib/lookup_table.h"
35 #include "wimlib/metadata.h"
36 #include "wimlib/ntfs_3g.h"
37 #include "wimlib/resource.h"
38 #include "wimlib/util.h"
39 #include "wimlib/write.h"
43 #include <unistd.h> /* for unlink() */
47 * This is a logical mapping from SHA1 message digests to the data streams
50 * Here it is implemented as a hash table.
52 * Note: Everything will break horribly if there is a SHA1 collision.
54 struct wim_lookup_table {
55 struct hlist_head *array;
60 struct wim_lookup_table *
61 new_lookup_table(size_t capacity)
63 struct wim_lookup_table *table;
64 struct hlist_head *array;
66 table = MALLOC(sizeof(struct wim_lookup_table));
70 array = CALLOC(capacity, sizeof(array[0]));
76 table->num_entries = 0;
77 table->capacity = capacity;
82 ERROR("Failed to allocate memory for lookup table "
83 "with capacity %zu", capacity);
88 do_free_lookup_table_entry(struct wim_lookup_table_entry *entry, void *ignore)
90 free_lookup_table_entry(entry);
95 free_lookup_table(struct wim_lookup_table *table)
97 DEBUG("Freeing lookup table.");
102 for_lookup_table_entry(table,
103 do_free_lookup_table_entry,
110 struct wim_lookup_table_entry *
111 new_lookup_table_entry(void)
113 struct wim_lookup_table_entry *lte;
115 lte = CALLOC(1, sizeof(struct wim_lookup_table_entry));
121 /* lte->resource_location = RESOURCE_NONEXISTENT */
122 BUILD_BUG_ON(RESOURCE_NONEXISTENT != 0);
127 struct wim_lookup_table_entry *
128 clone_lookup_table_entry(const struct wim_lookup_table_entry *old)
130 struct wim_lookup_table_entry *new;
132 new = memdup(old, sizeof(struct wim_lookup_table_entry));
136 new->extracted_file = NULL;
137 switch (new->resource_location) {
138 case RESOURCE_IN_WIM:
139 list_add(&new->rspec_node, &new->rspec->stream_list);
142 case RESOURCE_IN_FILE_ON_DISK:
144 case RESOURCE_WIN32_ENCRYPTED:
147 case RESOURCE_IN_STAGING_FILE:
148 BUILD_BUG_ON((void*)&old->file_on_disk !=
149 (void*)&old->staging_file_name);
151 new->file_on_disk = TSTRDUP(old->file_on_disk);
152 if (new->file_on_disk == NULL)
155 case RESOURCE_IN_ATTACHED_BUFFER:
156 new->attached_buffer = memdup(old->attached_buffer, old->size);
157 if (new->attached_buffer == NULL)
161 case RESOURCE_IN_NTFS_VOLUME:
163 struct ntfs_location *loc;
164 loc = memdup(old->ntfs_loc, sizeof(struct ntfs_location));
168 loc->stream_name = NULL;
170 loc->path = STRDUP(old->ntfs_loc->path);
171 if (loc->path == NULL)
173 if (loc->stream_name_nchars != 0) {
174 loc->stream_name = memdup(old->ntfs_loc->stream_name,
175 loc->stream_name_nchars * 2);
176 if (loc->stream_name == NULL)
188 free_lookup_table_entry(new);
193 lte_put_resource(struct wim_lookup_table_entry *lte)
195 switch (lte->resource_location) {
196 case RESOURCE_IN_WIM:
197 list_del(<e->rspec_node);
198 if (list_empty(<e->rspec->stream_list))
201 case RESOURCE_IN_FILE_ON_DISK:
203 case RESOURCE_WIN32_ENCRYPTED:
206 case RESOURCE_IN_STAGING_FILE:
207 BUILD_BUG_ON((void*)<e->file_on_disk !=
208 (void*)<e->staging_file_name);
210 case RESOURCE_IN_ATTACHED_BUFFER:
211 BUILD_BUG_ON((void*)<e->file_on_disk !=
212 (void*)<e->attached_buffer);
213 FREE(lte->file_on_disk);
216 case RESOURCE_IN_NTFS_VOLUME:
218 FREE(lte->ntfs_loc->path);
219 FREE(lte->ntfs_loc->stream_name);
230 free_lookup_table_entry(struct wim_lookup_table_entry *lte)
233 lte_put_resource(lte);
238 /* Should this stream be retained even if it has no references? */
240 should_retain_lte(const struct wim_lookup_table_entry *lte)
242 return lte->resource_location == RESOURCE_IN_WIM;
246 finalize_lte(struct wim_lookup_table_entry *lte)
248 if (!should_retain_lte(lte))
249 free_lookup_table_entry(lte);
253 * Decrements the reference count for the lookup table entry @lte, which must be
254 * inserted in the stream lookup table @table.
256 * If the reference count reaches 0, this may cause @lte to be destroyed.
257 * However, we may retain entries with 0 reference count. This does not affect
258 * correctness, but it prevents the entries for valid streams in a WIM archive,
259 * which will continue to be present after appending to the file, from being
260 * lost merely because we dropped all references to them.
263 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
264 struct wim_lookup_table *table)
266 wimlib_assert(lte->refcnt != 0);
268 if (--lte->refcnt == 0) {
270 list_del(<e->unhashed_list);
272 /* If the stream has been extracted to a staging file
273 * for a FUSE mount, unlink the staging file. (Note
274 * that there still may be open file descriptors to it.)
276 if (lte->resource_location == RESOURCE_IN_STAGING_FILE)
277 unlink(lte->staging_file_name);
280 if (!should_retain_lte(lte))
281 lookup_table_unlink(table, lte);
284 /* If FUSE mounts are enabled, we don't actually free the entry
285 * until the last file descriptor has been closed by
286 * lte_decrement_num_opened_fds(). */
288 if (lte->num_opened_fds == 0)
296 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte)
298 wimlib_assert(lte->num_opened_fds != 0);
300 if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
306 lookup_table_insert_raw(struct wim_lookup_table *table,
307 struct wim_lookup_table_entry *lte)
309 size_t i = lte->hash_short % table->capacity;
311 hlist_add_head(<e->hash_list, &table->array[i]);
315 enlarge_lookup_table(struct wim_lookup_table *table)
317 size_t old_capacity, new_capacity;
318 struct hlist_head *old_array, *new_array;
319 struct wim_lookup_table_entry *lte;
320 struct hlist_node *cur, *tmp;
323 old_capacity = table->capacity;
324 new_capacity = old_capacity * 2;
325 new_array = CALLOC(new_capacity, sizeof(struct hlist_head));
326 if (new_array == NULL)
328 old_array = table->array;
329 table->array = new_array;
330 table->capacity = new_capacity;
332 for (i = 0; i < old_capacity; i++) {
333 hlist_for_each_entry_safe(lte, cur, tmp, &old_array[i], hash_list) {
334 hlist_del(<e->hash_list);
335 lookup_table_insert_raw(table, lte);
341 /* Inserts an entry into the lookup table. */
343 lookup_table_insert(struct wim_lookup_table *table,
344 struct wim_lookup_table_entry *lte)
346 lookup_table_insert_raw(table, lte);
347 if (++table->num_entries > table->capacity)
348 enlarge_lookup_table(table);
351 /* Unlinks a lookup table entry from the table; does not free it. */
353 lookup_table_unlink(struct wim_lookup_table *table,
354 struct wim_lookup_table_entry *lte)
356 wimlib_assert(!lte->unhashed);
357 wimlib_assert(table->num_entries != 0);
359 hlist_del(<e->hash_list);
360 table->num_entries--;
363 /* Given a SHA1 message digest, return the corresponding entry in the WIM's
364 * lookup table, or NULL if there is none. */
365 struct wim_lookup_table_entry *
366 lookup_stream(const struct wim_lookup_table *table, const u8 hash[])
369 struct wim_lookup_table_entry *lte;
370 struct hlist_node *pos;
372 i = *(size_t*)hash % table->capacity;
373 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
374 if (hashes_equal(hash, lte->hash))
379 /* Calls a function on all the entries in the WIM lookup table. Stop early and
380 * return nonzero if any call to the function returns nonzero. */
382 for_lookup_table_entry(struct wim_lookup_table *table,
383 int (*visitor)(struct wim_lookup_table_entry *, void *),
386 struct wim_lookup_table_entry *lte;
387 struct hlist_node *pos, *tmp;
390 for (size_t i = 0; i < table->capacity; i++) {
391 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
394 ret = visitor(lte, arg);
402 /* qsort() callback that sorts streams (represented by `struct
403 * wim_lookup_table_entry's) into an order optimized for reading.
405 * Sorting is done primarily by resource location, then secondarily by a
406 * per-resource location order. For example, resources in WIM files are sorted
407 * primarily by part number, then secondarily by offset, as to implement optimal
408 * reading of either a standalone or split WIM. */
410 cmp_streams_by_sequential_order(const void *p1, const void *p2)
412 const struct wim_lookup_table_entry *lte1, *lte2;
414 WIMStruct *wim1, *wim2;
416 lte1 = *(const struct wim_lookup_table_entry**)p1;
417 lte2 = *(const struct wim_lookup_table_entry**)p2;
419 v = (int)lte1->resource_location - (int)lte2->resource_location;
421 /* Different resource locations? */
425 switch (lte1->resource_location) {
426 case RESOURCE_IN_WIM:
427 wim1 = lte1->rspec->wim;
428 wim2 = lte2->rspec->wim;
430 /* Different (possibly split) WIMs? */
432 v = memcmp(wim1->hdr.guid, wim2->hdr.guid, WIM_GID_LEN);
437 /* Different part numbers in the same WIM? */
438 v = (int)wim1->hdr.part_number - (int)wim2->hdr.part_number;
442 if (lte1->rspec->offset_in_wim != lte2->rspec->offset_in_wim)
443 return cmp_u64(lte1->rspec->offset_in_wim,
444 lte2->rspec->offset_in_wim);
446 return cmp_u64(lte1->offset_in_res, lte2->offset_in_res);
448 case RESOURCE_IN_FILE_ON_DISK:
450 case RESOURCE_IN_STAGING_FILE:
453 case RESOURCE_WIN32_ENCRYPTED:
455 /* Compare files by path: just a heuristic that will place files
456 * in the same directory next to each other. */
457 return tstrcmp(lte1->file_on_disk, lte2->file_on_disk);
459 case RESOURCE_IN_NTFS_VOLUME:
460 return tstrcmp(lte1->ntfs_loc->path, lte2->ntfs_loc->path);
463 /* No additional sorting order defined for this resource
464 * location (e.g. RESOURCE_IN_ATTACHED_BUFFER); simply compare
465 * everything equal to each other. */
471 sort_stream_list(struct list_head *stream_list,
472 size_t list_head_offset,
473 int (*compar)(const void *, const void*))
475 struct list_head *cur;
476 struct wim_lookup_table_entry **array;
479 size_t num_streams = 0;
481 list_for_each(cur, stream_list)
484 if (num_streams <= 1)
487 array_size = num_streams * sizeof(array[0]);
488 array = MALLOC(array_size);
490 return WIMLIB_ERR_NOMEM;
492 cur = stream_list->next;
493 for (i = 0; i < num_streams; i++) {
494 array[i] = (struct wim_lookup_table_entry*)((u8*)cur -
499 qsort(array, num_streams, sizeof(array[0]), compar);
501 INIT_LIST_HEAD(stream_list);
502 for (i = 0; i < num_streams; i++) {
503 list_add_tail((struct list_head*)
504 ((u8*)array[i] + list_head_offset),
511 /* Sort the specified list of streams in an order optimized for reading. */
513 sort_stream_list_by_sequential_order(struct list_head *stream_list,
514 size_t list_head_offset)
516 return sort_stream_list(stream_list, list_head_offset,
517 cmp_streams_by_sequential_order);
522 add_lte_to_array(struct wim_lookup_table_entry *lte,
525 struct wim_lookup_table_entry ***pp = _pp;
530 /* Iterate through the lookup table entries, but first sort them by stream
531 * offset in the WIM. Caution: this is intended to be used when the stream
532 * offset field has actually been set. */
534 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
535 int (*visitor)(struct wim_lookup_table_entry *,
539 struct wim_lookup_table_entry **lte_array, **p;
540 size_t num_streams = table->num_entries;
543 lte_array = MALLOC(num_streams * sizeof(lte_array[0]));
545 return WIMLIB_ERR_NOMEM;
547 for_lookup_table_entry(table, add_lte_to_array, &p);
549 wimlib_assert(p == lte_array + num_streams);
551 qsort(lte_array, num_streams, sizeof(lte_array[0]),
552 cmp_streams_by_sequential_order);
554 for (size_t i = 0; i < num_streams; i++) {
555 ret = visitor(lte_array[i], arg);
563 /* On-disk format of a WIM lookup table entry (stream entry). */
564 struct wim_lookup_table_entry_disk {
565 /* Size, offset, and flags of the stream. */
566 struct wim_reshdr_disk reshdr;
568 /* Which part of the split WIM this stream is in; indexed from 1. */
571 /* Reference count of this stream over all WIM images. */
574 /* SHA1 message digest of the uncompressed data of this stream, or
575 * optionally all zeroes if this stream is of zero length. */
576 u8 hash[SHA1_HASH_SIZE];
579 #define WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE 50
582 cmp_streams_by_offset_in_res(const void *p1, const void *p2)
584 const struct wim_lookup_table_entry *lte1, *lte2;
586 lte1 = *(const struct wim_lookup_table_entry**)p1;
587 lte2 = *(const struct wim_lookup_table_entry**)p2;
589 return cmp_u64(lte1->offset_in_res, lte2->offset_in_res);
592 /* Validate the size and location of a WIM resource. */
594 validate_resource(struct wim_resource_spec *rspec)
596 struct wim_lookup_table_entry *lte;
598 u64 expected_next_offset;
601 /* Verify that the resource itself has a valid offset and size. */
602 if (rspec->offset_in_wim + rspec->size_in_wim < rspec->size_in_wim)
603 goto invalid_due_to_overflow;
605 /* Verify that each stream in the resource has a valid offset and size.
607 expected_next_offset = 0;
608 out_of_order = false;
609 list_for_each_entry(lte, &rspec->stream_list, rspec_node) {
610 if (lte->offset_in_res + lte->size < lte->size ||
611 lte->offset_in_res + lte->size > rspec->uncompressed_size)
612 goto invalid_due_to_overflow;
614 if (lte->offset_in_res >= expected_next_offset)
615 expected_next_offset = lte->offset_in_res + lte->size;
620 /* If the streams were not located at strictly increasing positions (not
621 * allowing for overlap), sort them. Then make sure that none overlap.
624 ret = sort_stream_list(&rspec->stream_list,
625 offsetof(struct wim_lookup_table_entry,
627 cmp_streams_by_offset_in_res);
631 expected_next_offset = 0;
632 list_for_each_entry(lte, &rspec->stream_list, rspec_node) {
633 if (lte->offset_in_res >= expected_next_offset)
634 expected_next_offset = lte->offset_in_res + lte->size;
636 goto invalid_due_to_overlap;
642 invalid_due_to_overflow:
643 ERROR("Invalid resource entry (offset overflow)");
644 return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
646 invalid_due_to_overlap:
647 ERROR("Invalid resource entry (streams in packed resource overlap)");
648 return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
651 /* Validate the resource, or free it if unused. */
653 finish_resource(struct wim_resource_spec *rspec)
655 if (!list_empty(&rspec->stream_list)) {
656 /* This resource contains at least one stream. */
657 return validate_resource(rspec);
659 /* No streams are in this resource. Get rid of it. */
666 * Reads the lookup table from a WIM file. Usually, each entry specifies a
667 * stream that the WIM file contains, along with its location and SHA1 message
670 * Saves lookup table entries for non-metadata streams in a hash table (set to
671 * wim->lookup_table), and saves the metadata entry for each image in a special
672 * per-image location (the wim->image_metadata array).
674 * This works for both version WIM_VERSION_DEFAULT (68864) and version
675 * WIM_VERSION_PACKED_STREAMS (3584) WIMs.
677 * Possible return values:
678 * WIMLIB_ERR_SUCCESS (0)
679 * WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY
682 * Or an error code caused by failure to read the lookup table from the WIM
686 read_wim_lookup_table(WIMStruct *wim)
691 struct wim_lookup_table *table = NULL;
692 struct wim_lookup_table_entry *cur_entry = NULL;
693 struct wim_resource_spec *cur_rspec = NULL;
694 size_t num_duplicate_entries = 0;
695 size_t num_wrong_part_entries = 0;
698 DEBUG("Reading lookup table.");
700 /* Sanity check: lookup table entries are 50 bytes each. */
701 BUILD_BUG_ON(sizeof(struct wim_lookup_table_entry_disk) !=
702 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE);
704 /* Calculate the number of entries in the lookup table. */
705 num_entries = wim->hdr.lookup_table_reshdr.uncompressed_size /
706 sizeof(struct wim_lookup_table_entry_disk);
708 /* Read the lookup table into a buffer. */
709 ret = wim_reshdr_to_data(&wim->hdr.lookup_table_reshdr, wim, &buf);
713 /* Allocate a hash table to map SHA1 message digests into stream
714 * specifications. This is the in-memory "lookup table". */
715 table = new_lookup_table(num_entries * 2 + 1);
719 /* Allocate and initalize stream entries ('struct
720 * wim_lookup_table_entry's) from the raw lookup table buffer. Each of
721 * these entries will point to a 'struct wim_resource_spec' that
722 * describes the underlying resource. In WIMs with version number
723 * WIM_VERSION_PACKED_STREAMS, a resource may contain multiple streams.
725 for (size_t i = 0; i < num_entries; i++) {
726 const struct wim_lookup_table_entry_disk *disk_entry =
727 &((const struct wim_lookup_table_entry_disk*)buf)[i];
728 struct wim_reshdr reshdr;
730 struct wim_lookup_table_entry *duplicate_entry;
732 /* Get the resource header */
733 get_wim_reshdr(&disk_entry->reshdr, &reshdr);
735 DEBUG("reshdr: size_in_wim=%"PRIu64", "
736 "uncompressed_size=%"PRIu64", "
737 "offset_in_wim=%"PRIu64", "
739 reshdr.size_in_wim, reshdr.uncompressed_size,
740 reshdr.offset_in_wim, reshdr.flags);
742 /* Ignore PACKED_STREAMS flag if it isn't supposed to be used in
743 * this WIM version */
744 if (wim->hdr.wim_version == WIM_VERSION_DEFAULT)
745 reshdr.flags &= ~WIM_RESHDR_FLAG_PACKED_STREAMS;
747 /* Allocate a 'struct wim_lookup_table_entry' */
748 cur_entry = new_lookup_table_entry();
752 /* Get the part number, reference count, and hash. */
753 part_number = le16_to_cpu(disk_entry->part_number);
754 cur_entry->refcnt = le32_to_cpu(disk_entry->refcnt);
755 copy_hash(cur_entry->hash, disk_entry->hash);
757 /* Verify that the part number matches that of the underlying
759 if (part_number != wim->hdr.part_number) {
760 num_wrong_part_entries++;
761 goto free_cur_entry_and_continue;
764 /* If resource is uncompressed, check for (unexpected) size
766 if (!(reshdr.flags & (WIM_RESHDR_FLAG_PACKED_STREAMS |
767 WIM_RESHDR_FLAG_COMPRESSED))) {
768 if (reshdr.uncompressed_size != reshdr.size_in_wim) {
769 /* So ... This is an uncompressed resource, but
770 * its uncompressed size is NOT the same as its
771 * "compressed" size (size_in_wim). What to do
774 * Based on a simple test, WIMGAPI seems to
775 * handle this as follows:
777 * if (size_in_wim > uncompressed_size) {
778 * Ignore uncompressed_size; use
779 * size_in_wim instead.
781 * Honor uncompressed_size, but treat the
782 * part of the file data above size_in_wim
786 * So we will do the same.
788 if (reshdr.size_in_wim > reshdr.uncompressed_size)
789 reshdr.uncompressed_size = reshdr.size_in_wim;
794 * Possibly start a new resource.
796 * We need to start a new resource if:
798 * - There is no previous resource (cur_rspec).
802 * - The resource header did not have PACKED_STREAMS set, so it
803 * specifies a new, single-stream resource.
807 * - The resource header had PACKED_STREAMS set, and it's a
808 * special entry that specifies the resource itself as opposed
809 * to a stream, and we already encountered one such entry in
810 * the current resource. We will interpret this as the
811 * beginning of a new packed resource. (However, note that
812 * wimlib does not currently allow create WIMs with multiple
813 * packed resources, as to remain compatible with WIMGAPI.)
815 if (likely(!cur_rspec) ||
816 !(reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) ||
817 (reshdr.uncompressed_size == WIM_PACK_MAGIC_NUMBER &&
818 cur_rspec->size_in_wim != 0))
820 /* Finish previous resource (if existent) */
822 ret = finish_resource(cur_rspec);
828 /* Allocate the resource specification and initialize it
829 * with values from the current stream entry. */
830 cur_rspec = MALLOC(sizeof(*cur_rspec));
834 wim_res_hdr_to_spec(&reshdr, wim, cur_rspec);
836 /* If this is a packed run, the current stream entry may
837 * specify a stream within the resource, and not the
838 * resource itself. Zero possibly irrelevant data until
839 * it is read for certain. */
840 if (reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
841 cur_rspec->size_in_wim = 0;
842 cur_rspec->uncompressed_size = 0;
843 cur_rspec->offset_in_wim = 0;
847 /* Now cur_rspec != NULL. */
849 /* Checked for packed resource specification. */
850 if (unlikely((reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) &&
851 reshdr.uncompressed_size == WIM_PACK_MAGIC_NUMBER))
853 /* Found the specification for the packed resource.
854 * Transfer the values to the `struct
855 * wim_resource_spec', and discard the current stream
856 * since this lookup table entry did not, in fact,
857 * correspond to a "stream". */
859 /* The uncompressed size of the packed resource is
860 * actually stored in the header of the resource itself.
861 * Read it, and also grab the chunk size and compression
862 * type (which are not necessarily the defaults from the
864 struct alt_chunk_table_header_disk hdr;
866 ret = full_pread(&wim->in_fd, &hdr,
867 sizeof(hdr), reshdr.offset_in_wim);
871 cur_rspec->uncompressed_size = le64_to_cpu(hdr.res_usize);
872 cur_rspec->offset_in_wim = reshdr.offset_in_wim;
873 cur_rspec->size_in_wim = reshdr.size_in_wim;
874 cur_rspec->flags = reshdr.flags;
876 /* Compression format numbers must be the same as in
877 * WIMGAPI to be compatible here. */
878 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_NONE != 0);
879 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_XPRESS != 1);
880 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_LZX != 2);
881 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_LZMS != 3);
882 cur_rspec->compression_type = le32_to_cpu(hdr.compression_format);
884 cur_rspec->chunk_size = le32_to_cpu(hdr.chunk_size);
886 DEBUG("Full pack is %"PRIu64" compressed bytes "
887 "at file offset %"PRIu64" (flags 0x%02x)",
888 cur_rspec->size_in_wim,
889 cur_rspec->offset_in_wim,
891 goto free_cur_entry_and_continue;
894 /* Ignore entries with all zeroes in the hash field. */
895 if (is_zero_hash(cur_entry->hash))
896 goto free_cur_entry_and_continue;
898 if (reshdr.flags & WIM_RESHDR_FLAG_METADATA) {
900 /* Lookup table entry for a metadata resource. */
902 /* Metadata entries with no references must be ignored.
903 * See, for example, the WinPE WIMs from the WAIK v2.1.
905 if (cur_entry->refcnt == 0)
906 goto free_cur_entry_and_continue;
908 if (cur_entry->refcnt != 1) {
909 /* We don't currently support this case due to
910 * the complications of multiple images sharing
911 * the same metadata resource or a metadata
912 * resource also being referenced by files.
914 ERROR("Found metadata resource with refcnt != 1");
915 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
919 if (wim->hdr.part_number != 1) {
920 WARNING("Ignoring metadata resource found in a "
921 "non-first part of the split WIM");
922 goto free_cur_entry_and_continue;
925 /* The number of entries in the lookup table with
926 * WIM_RESHDR_FLAG_METADATA set should be the same as
927 * the image_count field in the WIM header. */
928 if (image_index == wim->hdr.image_count) {
929 WARNING("Found more metadata resources than images");
930 goto free_cur_entry_and_continue;
933 /* Notice very carefully: We are assigning the metadata
934 * resources to images in the same order in which their
935 * lookup table entries occur on disk. (This is also
936 * the behavior of Microsoft's software.) In
937 * particular, this overrides the actual locations of
938 * the metadata resources themselves in the WIM file as
939 * well as any information written in the XML data. */
940 DEBUG("Found metadata resource for image %"PRIu32" at "
943 reshdr.offset_in_wim);
945 wim->image_metadata[image_index++]->metadata_lte = cur_entry;
947 /* Lookup table entry for a non-metadata stream. */
949 /* Ignore this stream if it's a duplicate. */
950 duplicate_entry = lookup_stream(table, cur_entry->hash);
951 if (duplicate_entry) {
952 num_duplicate_entries++;
953 goto free_cur_entry_and_continue;
956 /* Insert the stream into the in-memory lookup table,
957 * keyed by its SHA1 message digest. */
958 lookup_table_insert(table, cur_entry);
961 /* Add the stream to the current resource specification. */
962 lte_bind_wim_resource_spec(cur_entry, cur_rspec);
963 if (reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
964 /* In packed runs, the offset field is used for
965 * in-resource offset, not the in-WIM offset, and the
966 * size field is used for the uncompressed size, not the
967 * compressed size. */
968 cur_entry->offset_in_res = reshdr.offset_in_wim;
969 cur_entry->size = reshdr.size_in_wim;
970 cur_entry->flags = reshdr.flags;
971 /* cur_rspec stays the same */
974 /* Normal case: The stream corresponds one-to-one with
975 * the resource entry. */
976 cur_entry->offset_in_res = 0;
977 cur_entry->size = reshdr.uncompressed_size;
978 cur_entry->flags = reshdr.flags;
983 free_cur_entry_and_continue:
984 free_lookup_table_entry(cur_entry);
988 /* Validate the last resource. */
990 ret = finish_resource(cur_rspec);
996 if (wim->hdr.part_number == 1 && image_index != wim->hdr.image_count) {
997 WARNING("Could not find metadata resources for all images");
998 for (u32 i = image_index; i < wim->hdr.image_count; i++)
999 put_image_metadata(wim->image_metadata[i], NULL);
1000 wim->hdr.image_count = image_index;
1003 if (num_duplicate_entries > 0) {
1004 WARNING("Ignoring %zu duplicate streams in the WIM lookup table",
1005 num_duplicate_entries);
1008 if (num_wrong_part_entries > 0) {
1009 WARNING("Ignoring %zu streams with wrong part number",
1010 num_wrong_part_entries);
1013 DEBUG("Done reading lookup table.");
1014 wim->lookup_table = table;
1019 ERROR("Not enough memory to read lookup table!");
1020 ret = WIMLIB_ERR_NOMEM;
1022 if (cur_rspec && list_empty(&cur_rspec->stream_list))
1024 free_lookup_table_entry(cur_entry);
1025 free_lookup_table(table);
1031 put_wim_lookup_table_entry(struct wim_lookup_table_entry_disk *disk_entry,
1032 const struct wim_reshdr *out_reshdr,
1033 u16 part_number, u32 refcnt, const u8 *hash)
1035 put_wim_reshdr(out_reshdr, &disk_entry->reshdr);
1036 disk_entry->part_number = cpu_to_le16(part_number);
1037 disk_entry->refcnt = cpu_to_le32(refcnt);
1038 copy_hash(disk_entry->hash, hash);
1042 write_wim_lookup_table_from_stream_list(struct list_head *stream_list,
1043 struct filedes *out_fd,
1045 struct wim_reshdr *out_reshdr,
1046 int write_resource_flags)
1049 struct wim_lookup_table_entry *lte;
1050 struct wim_lookup_table_entry_disk *table_buf;
1051 struct wim_lookup_table_entry_disk *table_buf_ptr;
1053 u64 prev_res_offset_in_wim = ~0ULL;
1056 list_for_each_entry(lte, stream_list, lookup_table_list) {
1057 table_size += sizeof(struct wim_lookup_table_entry_disk);
1059 if (lte->out_reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS &&
1060 lte->out_res_offset_in_wim != prev_res_offset_in_wim)
1062 table_size += sizeof(struct wim_lookup_table_entry_disk);
1063 prev_res_offset_in_wim = lte->out_res_offset_in_wim;
1067 DEBUG("Writing WIM lookup table (size=%zu, offset=%"PRIu64")",
1068 table_size, out_fd->offset);
1070 table_buf = MALLOC(table_size);
1071 if (table_buf == NULL) {
1072 ERROR("Failed to allocate %zu bytes for temporary lookup table",
1074 return WIMLIB_ERR_NOMEM;
1076 table_buf_ptr = table_buf;
1078 prev_res_offset_in_wim = ~0ULL;
1079 list_for_each_entry(lte, stream_list, lookup_table_list) {
1081 put_wim_lookup_table_entry(table_buf_ptr++,
1086 if (lte->out_reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS &&
1087 lte->out_res_offset_in_wim != prev_res_offset_in_wim)
1089 /* Put the main resource entry for the pack. */
1091 struct wim_reshdr reshdr;
1093 reshdr.offset_in_wim = lte->out_res_offset_in_wim;
1094 reshdr.size_in_wim = lte->out_res_size_in_wim;
1095 reshdr.uncompressed_size = WIM_PACK_MAGIC_NUMBER;
1096 reshdr.flags = WIM_RESHDR_FLAG_PACKED_STREAMS;
1098 DEBUG("Putting main entry for pack: "
1099 "size_in_wim=%"PRIu64", "
1100 "offset_in_wim=%"PRIu64", "
1101 "uncompressed_size=%"PRIu64,
1103 reshdr.offset_in_wim,
1104 reshdr.uncompressed_size);
1106 put_wim_lookup_table_entry(table_buf_ptr++,
1110 prev_res_offset_in_wim = lte->out_res_offset_in_wim;
1114 wimlib_assert((u8*)table_buf_ptr - (u8*)table_buf == table_size);
1116 /* Write the lookup table uncompressed. Although wimlib can handle a
1117 * compressed lookup table, MS software cannot. */
1118 ret = write_wim_resource_from_buffer(table_buf,
1120 WIM_RESHDR_FLAG_METADATA,
1122 WIMLIB_COMPRESSION_TYPE_NONE,
1126 write_resource_flags);
1128 DEBUG("ret=%d", ret);
1133 lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
1135 lte->real_refcnt = 0;
1140 lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
1142 lte->out_refcnt = 0;
1147 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *_ignore)
1149 if (lte->extracted_file != NULL) {
1150 FREE(lte->extracted_file);
1151 lte->extracted_file = NULL;
1156 /* Allocate a stream entry for the contents of the buffer, or re-use an existing
1157 * entry in @lookup_table for the same stream. */
1158 struct wim_lookup_table_entry *
1159 new_stream_from_data_buffer(const void *buffer, size_t size,
1160 struct wim_lookup_table *lookup_table)
1162 u8 hash[SHA1_HASH_SIZE];
1163 struct wim_lookup_table_entry *lte, *existing_lte;
1165 sha1_buffer(buffer, size, hash);
1166 existing_lte = lookup_stream(lookup_table, hash);
1168 wimlib_assert(existing_lte->size == size);
1173 lte = new_lookup_table_entry();
1176 buffer_copy = memdup(buffer, size);
1177 if (buffer_copy == NULL) {
1178 free_lookup_table_entry(lte);
1181 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1182 lte->attached_buffer = buffer_copy;
1184 copy_hash(lte->hash, hash);
1185 lookup_table_insert(lookup_table, lte);
1190 /* Calculate the SHA1 message digest of a stream and move it from the list of
1191 * unhashed streams to the stream lookup table, possibly joining it with an
1192 * existing lookup table entry for an identical stream.
1194 * @lte: An unhashed lookup table entry.
1195 * @lookup_table: Lookup table for the WIM.
1196 * @lte_ret: On success, write a pointer to the resulting lookup table
1197 * entry to this location. This will be the same as @lte
1198 * if it was inserted into the lookup table, or different if
1199 * a duplicate stream was found.
1201 * Returns 0 on success; nonzero if there is an error reading the stream.
1204 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
1205 struct wim_lookup_table *lookup_table,
1206 struct wim_lookup_table_entry **lte_ret)
1209 struct wim_lookup_table_entry *duplicate_lte;
1210 struct wim_lookup_table_entry **back_ptr;
1212 wimlib_assert(lte->unhashed);
1214 /* back_ptr must be saved because @back_inode and @back_stream_id are in
1215 * union with the SHA1 message digest and will no longer be valid once
1216 * the SHA1 has been calculated. */
1217 back_ptr = retrieve_lte_pointer(lte);
1219 ret = sha1_stream(lte);
1223 /* Look for a duplicate stream */
1224 duplicate_lte = lookup_stream(lookup_table, lte->hash);
1225 list_del(<e->unhashed_list);
1226 if (duplicate_lte) {
1227 /* We have a duplicate stream. Transfer the reference counts
1228 * from this stream to the duplicate and update the reference to
1229 * this stream (in an inode or ads_entry) to point to the
1230 * duplicate. The caller is responsible for freeing @lte if
1232 wimlib_assert(!(duplicate_lte->unhashed));
1233 wimlib_assert(duplicate_lte->size == lte->size);
1234 duplicate_lte->refcnt += lte->refcnt;
1236 *back_ptr = duplicate_lte;
1237 lte = duplicate_lte;
1239 /* No duplicate stream, so we need to insert this stream into
1240 * the lookup table and treat it as a hashed stream. */
1241 lookup_table_insert(lookup_table, lte);
1249 lte_to_wimlib_resource_entry(const struct wim_lookup_table_entry *lte,
1250 struct wimlib_resource_entry *wentry)
1252 memset(wentry, 0, sizeof(*wentry));
1254 wentry->uncompressed_size = lte->size;
1255 if (lte->resource_location == RESOURCE_IN_WIM) {
1256 wentry->part_number = lte->rspec->wim->hdr.part_number;
1257 if (lte->flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
1258 wentry->compressed_size = 0;
1259 wentry->offset = lte->offset_in_res;
1261 wentry->compressed_size = lte->rspec->size_in_wim;
1262 wentry->offset = lte->rspec->offset_in_wim;
1264 wentry->raw_resource_offset_in_wim = lte->rspec->offset_in_wim;
1265 /*wentry->raw_resource_uncompressed_size = lte->rspec->uncompressed_size;*/
1266 wentry->raw_resource_compressed_size = lte->rspec->size_in_wim;
1268 copy_hash(wentry->sha1_hash, lte->hash);
1269 wentry->reference_count = lte->refcnt;
1270 wentry->is_compressed = (lte->flags & WIM_RESHDR_FLAG_COMPRESSED) != 0;
1271 wentry->is_metadata = (lte->flags & WIM_RESHDR_FLAG_METADATA) != 0;
1272 wentry->is_free = (lte->flags & WIM_RESHDR_FLAG_FREE) != 0;
1273 wentry->is_spanned = (lte->flags & WIM_RESHDR_FLAG_SPANNED) != 0;
1274 wentry->packed = (lte->flags & WIM_RESHDR_FLAG_PACKED_STREAMS) != 0;
1277 struct iterate_lte_context {
1278 wimlib_iterate_lookup_table_callback_t cb;
1283 do_iterate_lte(struct wim_lookup_table_entry *lte, void *_ctx)
1285 struct iterate_lte_context *ctx = _ctx;
1286 struct wimlib_resource_entry entry;
1288 lte_to_wimlib_resource_entry(lte, &entry);
1289 return (*ctx->cb)(&entry, ctx->user_ctx);
1292 /* API function documented in wimlib.h */
1294 wimlib_iterate_lookup_table(WIMStruct *wim, int flags,
1295 wimlib_iterate_lookup_table_callback_t cb,
1299 return WIMLIB_ERR_INVALID_PARAM;
1301 struct iterate_lte_context ctx = {
1303 .user_ctx = user_ctx,
1305 if (wim->hdr.part_number == 1) {
1307 for (int i = 0; i < wim->hdr.image_count; i++) {
1308 ret = do_iterate_lte(wim->image_metadata[i]->metadata_lte,
1314 return for_lookup_table_entry(wim->lookup_table, do_iterate_lte, &ctx);