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 free_lookup_table_entry(struct wim_lookup_table_entry *lte)
196 switch (lte->resource_location) {
197 case RESOURCE_IN_WIM:
198 list_del(<e->rspec_node);
199 if (list_empty(<e->rspec->stream_list))
202 case RESOURCE_IN_FILE_ON_DISK:
204 case RESOURCE_WIN32_ENCRYPTED:
207 case RESOURCE_IN_STAGING_FILE:
208 BUILD_BUG_ON((void*)<e->file_on_disk !=
209 (void*)<e->staging_file_name);
211 case RESOURCE_IN_ATTACHED_BUFFER:
212 BUILD_BUG_ON((void*)<e->file_on_disk !=
213 (void*)<e->attached_buffer);
214 FREE(lte->file_on_disk);
217 case RESOURCE_IN_NTFS_VOLUME:
219 FREE(lte->ntfs_loc->path);
220 FREE(lte->ntfs_loc->stream_name);
232 /* Decrements the reference count for the lookup table entry @lte. If its
233 * reference count reaches 0, it is unlinked from the lookup table. If,
234 * furthermore, the entry has no opened file descriptors associated with it, the
237 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
238 struct wim_lookup_table *table)
240 wimlib_assert(lte->refcnt != 0);
242 if (--lte->refcnt == 0) {
244 list_del(<e->unhashed_list);
246 /* If the stream has been extracted to a staging file
247 * for a FUSE mount, unlink the staging file. (Note
248 * that there still may be open file descriptors to it.)
250 if (lte->resource_location == RESOURCE_IN_STAGING_FILE)
251 unlink(lte->staging_file_name);
254 lookup_table_unlink(table, lte);
257 /* If FUSE mounts are enabled, we don't actually free the entry
258 * until the last file descriptor has been closed by
259 * lte_decrement_num_opened_fds(). */
261 if (lte->num_opened_fds == 0)
263 free_lookup_table_entry(lte);
269 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte)
271 wimlib_assert(lte->num_opened_fds != 0);
273 if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
274 free_lookup_table_entry(lte);
279 lookup_table_insert_raw(struct wim_lookup_table *table,
280 struct wim_lookup_table_entry *lte)
282 size_t i = lte->hash_short % table->capacity;
284 hlist_add_head(<e->hash_list, &table->array[i]);
288 enlarge_lookup_table(struct wim_lookup_table *table)
290 size_t old_capacity, new_capacity;
291 struct hlist_head *old_array, *new_array;
292 struct wim_lookup_table_entry *lte;
293 struct hlist_node *cur, *tmp;
296 old_capacity = table->capacity;
297 new_capacity = old_capacity * 2;
298 new_array = CALLOC(new_capacity, sizeof(struct hlist_head));
299 if (new_array == NULL)
301 old_array = table->array;
302 table->array = new_array;
303 table->capacity = new_capacity;
305 for (i = 0; i < old_capacity; i++) {
306 hlist_for_each_entry_safe(lte, cur, tmp, &old_array[i], hash_list) {
307 hlist_del(<e->hash_list);
308 lookup_table_insert_raw(table, lte);
314 /* Inserts an entry into the lookup table. */
316 lookup_table_insert(struct wim_lookup_table *table,
317 struct wim_lookup_table_entry *lte)
319 lookup_table_insert_raw(table, lte);
320 if (++table->num_entries > table->capacity)
321 enlarge_lookup_table(table);
324 /* Unlinks a lookup table entry from the table; does not free it. */
326 lookup_table_unlink(struct wim_lookup_table *table,
327 struct wim_lookup_table_entry *lte)
329 wimlib_assert(!lte->unhashed);
330 wimlib_assert(table->num_entries != 0);
332 hlist_del(<e->hash_list);
333 table->num_entries--;
336 /* Given a SHA1 message digest, return the corresponding entry in the WIM's
337 * lookup table, or NULL if there is none. */
338 struct wim_lookup_table_entry *
339 lookup_stream(const struct wim_lookup_table *table, const u8 hash[])
342 struct wim_lookup_table_entry *lte;
343 struct hlist_node *pos;
345 i = *(size_t*)hash % table->capacity;
346 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
347 if (hashes_equal(hash, lte->hash))
352 /* Calls a function on all the entries in the WIM lookup table. Stop early and
353 * return nonzero if any call to the function returns nonzero. */
355 for_lookup_table_entry(struct wim_lookup_table *table,
356 int (*visitor)(struct wim_lookup_table_entry *, void *),
359 struct wim_lookup_table_entry *lte;
360 struct hlist_node *pos, *tmp;
363 for (size_t i = 0; i < table->capacity; i++) {
364 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
367 ret = visitor(lte, arg);
375 /* qsort() callback that sorts streams (represented by `struct
376 * wim_lookup_table_entry's) into an order optimized for reading.
378 * Sorting is done primarily by resource location, then secondarily by a
379 * per-resource location order. For example, resources in WIM files are sorted
380 * primarily by part number, then secondarily by offset, as to implement optimal
381 * reading of either a standalone or split WIM. */
383 cmp_streams_by_sequential_order(const void *p1, const void *p2)
385 const struct wim_lookup_table_entry *lte1, *lte2;
387 WIMStruct *wim1, *wim2;
389 lte1 = *(const struct wim_lookup_table_entry**)p1;
390 lte2 = *(const struct wim_lookup_table_entry**)p2;
392 v = (int)lte1->resource_location - (int)lte2->resource_location;
394 /* Different resource locations? */
398 switch (lte1->resource_location) {
399 case RESOURCE_IN_WIM:
400 wim1 = lte1->rspec->wim;
401 wim2 = lte2->rspec->wim;
403 /* Different (possibly split) WIMs? */
405 v = memcmp(wim1->hdr.guid, wim2->hdr.guid, WIM_GID_LEN);
410 /* Different part numbers in the same WIM? */
411 v = (int)wim1->hdr.part_number - (int)wim2->hdr.part_number;
415 if (lte1->rspec->offset_in_wim != lte2->rspec->offset_in_wim)
416 return cmp_u64(lte1->rspec->offset_in_wim,
417 lte2->rspec->offset_in_wim);
419 return cmp_u64(lte1->offset_in_res, lte2->offset_in_res);
421 case RESOURCE_IN_FILE_ON_DISK:
423 case RESOURCE_IN_STAGING_FILE:
426 case RESOURCE_WIN32_ENCRYPTED:
428 /* Compare files by path: just a heuristic that will place files
429 * in the same directory next to each other. */
430 return tstrcmp(lte1->file_on_disk, lte2->file_on_disk);
432 case RESOURCE_IN_NTFS_VOLUME:
433 return tstrcmp(lte1->ntfs_loc->path, lte2->ntfs_loc->path);
436 /* No additional sorting order defined for this resource
437 * location (e.g. RESOURCE_IN_ATTACHED_BUFFER); simply compare
438 * everything equal to each other. */
444 sort_stream_list(struct list_head *stream_list,
445 size_t list_head_offset,
446 int (*compar)(const void *, const void*))
448 struct list_head *cur;
449 struct wim_lookup_table_entry **array;
452 size_t num_streams = 0;
454 list_for_each(cur, stream_list)
457 if (num_streams <= 1)
460 array_size = num_streams * sizeof(array[0]);
461 array = MALLOC(array_size);
463 return WIMLIB_ERR_NOMEM;
465 cur = stream_list->next;
466 for (i = 0; i < num_streams; i++) {
467 array[i] = (struct wim_lookup_table_entry*)((u8*)cur -
472 qsort(array, num_streams, sizeof(array[0]), compar);
474 INIT_LIST_HEAD(stream_list);
475 for (i = 0; i < num_streams; i++) {
476 list_add_tail((struct list_head*)
477 ((u8*)array[i] + list_head_offset),
484 /* Sort the specified list of streams in an order optimized for reading. */
486 sort_stream_list_by_sequential_order(struct list_head *stream_list,
487 size_t list_head_offset)
489 return sort_stream_list(stream_list, list_head_offset,
490 cmp_streams_by_sequential_order);
495 add_lte_to_array(struct wim_lookup_table_entry *lte,
498 struct wim_lookup_table_entry ***pp = _pp;
503 /* Iterate through the lookup table entries, but first sort them by stream
504 * offset in the WIM. Caution: this is intended to be used when the stream
505 * offset field has actually been set. */
507 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
508 int (*visitor)(struct wim_lookup_table_entry *,
512 struct wim_lookup_table_entry **lte_array, **p;
513 size_t num_streams = table->num_entries;
516 lte_array = MALLOC(num_streams * sizeof(lte_array[0]));
518 return WIMLIB_ERR_NOMEM;
520 for_lookup_table_entry(table, add_lte_to_array, &p);
522 wimlib_assert(p == lte_array + num_streams);
524 qsort(lte_array, num_streams, sizeof(lte_array[0]),
525 cmp_streams_by_sequential_order);
527 for (size_t i = 0; i < num_streams; i++) {
528 ret = visitor(lte_array[i], arg);
536 /* On-disk format of a WIM lookup table entry (stream entry). */
537 struct wim_lookup_table_entry_disk {
538 /* Size, offset, and flags of the stream. */
539 struct wim_reshdr_disk reshdr;
541 /* Which part of the split WIM this stream is in; indexed from 1. */
544 /* Reference count of this stream over all WIM images. */
547 /* SHA1 message digest of the uncompressed data of this stream, or
548 * optionally all zeroes if this stream is of zero length. */
549 u8 hash[SHA1_HASH_SIZE];
552 #define WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE 50
554 /* Validate the size and location of a WIM resource. */
556 validate_resource(const struct wim_resource_spec *rspec)
558 struct wim_lookup_table_entry *lte;
561 /* Verify that calculating the offset of the end of the resource doesn't
563 if (rspec->offset_in_wim + rspec->size_in_wim < rspec->size_in_wim)
566 /* Verify that each stream in the resource has a valid offset and size,
567 * and that no streams overlap, and that the streams were added in order
568 * of increasing offset. */
570 list_for_each_entry(lte, &rspec->stream_list, rspec_node) {
571 if (lte->offset_in_res + lte->size < lte->size ||
572 lte->offset_in_res + lte->size > rspec->uncompressed_size ||
573 lte->offset_in_res < cur_offset)
576 cur_offset = lte->offset_in_res + lte->size;
582 ERROR("Invalid resource entry!");
583 return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
587 * Reads the lookup table from a WIM file. Each entry specifies a stream that
588 * the WIM file contains, along with its location and SHA1 message digest.
590 * Saves lookup table entries for non-metadata streams in a hash table, and
591 * saves the metadata entry for each image in a special per-image location (the
592 * image_metadata array).
595 * WIMLIB_ERR_SUCCESS (0)
596 * WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY
597 * WIMLIB_ERR_RESOURCE_NOT_FOUND
599 * Or an error code caused by failure to read the lookup table into memory.
602 read_wim_lookup_table(WIMStruct *wim)
607 struct wim_lookup_table *table;
608 struct wim_lookup_table_entry *cur_entry, *duplicate_entry;
609 struct wim_resource_spec *cur_rspec;
611 bool back_to_back_pack;
613 DEBUG("Reading lookup table.");
615 /* Sanity check: lookup table entries are 50 bytes each. */
616 BUILD_BUG_ON(sizeof(struct wim_lookup_table_entry_disk) !=
617 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE);
619 /* Calculate number of entries in the lookup table. */
620 num_entries = wim->hdr.lookup_table_reshdr.uncompressed_size /
621 sizeof(struct wim_lookup_table_entry_disk);
623 /* Read the lookup table into a buffer. */
624 ret = wim_reshdr_to_data(&wim->hdr.lookup_table_reshdr, wim, &buf);
628 /* Allocate a hash table to map SHA1 message digests into stream
629 * specifications. This is the in-memory "lookup table". */
630 table = new_lookup_table(num_entries * 2 + 1);
632 ERROR("Not enough memory to read lookup table.");
633 ret = WIMLIB_ERR_NOMEM;
637 /* Allocate and initalize stream entries from the raw lookup table
639 wim->current_image = 0;
641 for (i = 0; i < num_entries; i++) {
642 const struct wim_lookup_table_entry_disk *disk_entry =
643 &((const struct wim_lookup_table_entry_disk*)buf)[i];
645 struct wim_reshdr reshdr;
647 get_wim_reshdr(&disk_entry->reshdr, &reshdr);
649 DEBUG("reshdr: size_in_wim=%"PRIu64", "
650 "uncompressed_size=%"PRIu64", "
651 "offset_in_wim=%"PRIu64", "
653 reshdr.size_in_wim, reshdr.uncompressed_size,
654 reshdr.offset_in_wim, reshdr.flags);
656 if (wim->hdr.wim_version == WIM_VERSION_DEFAULT)
657 reshdr.flags &= ~WIM_RESHDR_FLAG_PACKED_STREAMS;
659 cur_entry = new_lookup_table_entry();
660 if (cur_entry == NULL) {
661 ERROR("Not enough memory to read lookup table!");
662 ret = WIMLIB_ERR_NOMEM;
666 part_number = le16_to_cpu(disk_entry->part_number);
667 cur_entry->refcnt = le32_to_cpu(disk_entry->refcnt);
668 copy_hash(cur_entry->hash, disk_entry->hash);
670 if (part_number != wim->hdr.part_number) {
671 WARNING("A lookup table entry in part %hu of the WIM "
672 "points to part %hu (ignoring it)",
673 wim->hdr.part_number, part_number);
674 free_lookup_table_entry(cur_entry);
678 if (!(reshdr.flags & (WIM_RESHDR_FLAG_PACKED_STREAMS |
679 WIM_RESHDR_FLAG_COMPRESSED))) {
680 if (reshdr.uncompressed_size != reshdr.size_in_wim) {
681 ERROR("Invalid resource entry!");
682 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
687 back_to_back_pack = false;
688 if (!(reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) ||
691 ((reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) &&
692 reshdr.uncompressed_size == WIM_PACK_MAGIC_NUMBER &&
694 cur_rspec->size_in_wim != 0)))
696 /* Starting new run of streams that share the same WIM
698 struct wim_lookup_table_entry *prev_entry = NULL;
700 if (back_to_back_pack &&
701 !list_empty(&cur_rspec->stream_list))
703 prev_entry = list_entry(cur_rspec->stream_list.prev,
704 struct wim_lookup_table_entry,
706 lte_unbind_wim_resource_spec(prev_entry);
708 if (cur_rspec != NULL) {
709 ret = validate_resource(cur_rspec);
714 /* Allocate the resource specification and initialize it
715 * with values from the current stream entry. */
716 cur_rspec = MALLOC(sizeof(*cur_rspec));
717 if (cur_rspec == NULL) {
718 ERROR("Not enough memory to read lookup table!");
719 ret = WIMLIB_ERR_NOMEM;
722 wim_res_hdr_to_spec(&reshdr, wim, cur_rspec);
725 lte_bind_wim_resource_spec(prev_entry, cur_rspec);
728 if ((reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) &&
729 reshdr.uncompressed_size == WIM_PACK_MAGIC_NUMBER)
731 /* Found the specification for the packed resource.
732 * Transfer the values to the `struct
733 * wim_resource_spec', and discard the current stream
734 * since this lookup table entry did not, in fact,
735 * correspond to a "stream".
738 /* Uncompressed size of the resource pack is actually
739 * stored in the header of the resource itself. Read
740 * it, and also grab the chunk size and compression type
741 * (which are not necessarily the defaults from the WIM
743 struct alt_chunk_table_header_disk hdr;
745 ret = full_pread(&wim->in_fd, &hdr,
746 sizeof(hdr), reshdr.offset_in_wim);
750 cur_rspec->uncompressed_size = le64_to_cpu(hdr.res_usize);
751 cur_rspec->offset_in_wim = reshdr.offset_in_wim;
752 cur_rspec->size_in_wim = reshdr.size_in_wim;
753 cur_rspec->flags = reshdr.flags;
755 /* Compression format numbers must be the same as in
756 * WIMGAPI to be compatible here. */
757 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_NONE != 0);
758 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_LZX != 1);
759 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_XPRESS != 2);
760 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_LZMS != 3);
761 cur_rspec->compression_type = le32_to_cpu(hdr.compression_format);
763 cur_rspec->chunk_size = le32_to_cpu(hdr.chunk_size);
765 DEBUG("Full pack is %"PRIu64" compressed bytes "
766 "at file offset %"PRIu64" (flags 0x%02x)",
767 cur_rspec->size_in_wim,
768 cur_rspec->offset_in_wim,
770 free_lookup_table_entry(cur_entry);
774 if (is_zero_hash(cur_entry->hash)) {
775 free_lookup_table_entry(cur_entry);
779 if (reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
780 /* Continuing the pack with another stream. */
781 DEBUG("Continuing pack with stream: "
782 "%"PRIu64" uncompressed bytes @ "
783 "resource offset %"PRIu64")",
784 reshdr.size_in_wim, reshdr.offset_in_wim);
787 lte_bind_wim_resource_spec(cur_entry, cur_rspec);
788 if (reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
789 /* In packed runs, the offset field is used for
790 * in-resource offset, not the in-WIM offset, and the
791 * size field is used for the uncompressed size, not the
792 * compressed size. */
793 cur_entry->offset_in_res = reshdr.offset_in_wim;
794 cur_entry->size = reshdr.size_in_wim;
795 cur_entry->flags = reshdr.flags;
797 /* Normal case: The stream corresponds one-to-one with
798 * the resource entry. */
799 cur_entry->offset_in_res = 0;
800 cur_entry->size = reshdr.uncompressed_size;
801 cur_entry->flags = reshdr.flags;
805 if (cur_entry->flags & WIM_RESHDR_FLAG_METADATA) {
806 /* Lookup table entry for a metadata resource */
808 /* Metadata entries with no references must be ignored;
809 * see for example the WinPE WIMs from the WAIK v2.1.
811 if (cur_entry->refcnt == 0) {
812 free_lookup_table_entry(cur_entry);
816 if (cur_entry->refcnt != 1) {
817 if (wimlib_print_errors) {
818 ERROR("Found metadata resource with refcnt != 1:");
819 print_lookup_table_entry(cur_entry, stderr);
821 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
825 if (wim->hdr.part_number != 1) {
826 WARNING("Ignoring metadata resource found in a "
827 "non-first part of the split WIM");
828 free_lookup_table_entry(cur_entry);
831 if (wim->current_image == wim->hdr.image_count) {
832 WARNING("The WIM header says there are %u images "
833 "in the WIM, but we found more metadata "
834 "resources than this (ignoring the extra)",
835 wim->hdr.image_count);
836 free_lookup_table_entry(cur_entry);
840 /* Notice very carefully: We are assigning the metadata
841 * resources in the exact order mirrored by their lookup
842 * table entries on disk, which is the behavior of
843 * Microsoft's software. In particular, this overrides
844 * the actual locations of the metadata resources
845 * themselves in the WIM file as well as any information
846 * written in the XML data. */
847 DEBUG("Found metadata resource for image %u at "
849 wim->current_image + 1,
850 cur_entry->rspec->offset_in_wim);
852 wim->current_image++]->metadata_lte = cur_entry;
856 /* Lookup table entry for a stream that is not a metadata
858 duplicate_entry = lookup_stream(table, cur_entry->hash);
859 if (duplicate_entry) {
860 if (wimlib_print_errors) {
861 WARNING("The WIM lookup table contains two entries with the "
862 "same SHA1 message digest!");
863 WARNING("The first entry is:");
864 print_lookup_table_entry(duplicate_entry, stderr);
865 WARNING("The second entry is:");
866 print_lookup_table_entry(cur_entry, stderr);
868 free_lookup_table_entry(cur_entry);
872 /* Finally, insert the stream into the lookup table, keyed by
873 * its SHA1 message digest. */
874 lookup_table_insert(table, cur_entry);
878 /* Validate the last resource. */
879 if (cur_rspec != NULL) {
880 ret = validate_resource(cur_rspec);
885 if (wim->hdr.part_number == 1 && wim->current_image != wim->hdr.image_count) {
886 WARNING("The header of \"%"TS"\" says there are %u images in\n"
887 " the WIM, but we only found %d metadata resources! Acting as if\n"
888 " the header specified only %d images instead.",
889 wim->filename, wim->hdr.image_count,
890 wim->current_image, wim->current_image);
891 for (int i = wim->current_image; i < wim->hdr.image_count; i++)
892 put_image_metadata(wim->image_metadata[i], NULL);
893 wim->hdr.image_count = wim->current_image;
895 DEBUG("Done reading lookup table.");
896 wim->lookup_table = table;
901 if (cur_rspec && list_empty(&cur_rspec->stream_list))
903 free_lookup_table_entry(cur_entry);
904 free_lookup_table(table);
908 wim->current_image = 0;
913 put_wim_lookup_table_entry(struct wim_lookup_table_entry_disk *disk_entry,
914 const struct wim_reshdr *out_reshdr,
915 u16 part_number, u32 refcnt, const u8 *hash)
917 put_wim_reshdr(out_reshdr, &disk_entry->reshdr);
918 disk_entry->part_number = cpu_to_le16(part_number);
919 disk_entry->refcnt = cpu_to_le32(refcnt);
920 copy_hash(disk_entry->hash, hash);
924 write_wim_lookup_table_from_stream_list(struct list_head *stream_list,
925 struct filedes *out_fd,
927 struct wim_reshdr *out_reshdr,
928 int write_resource_flags)
931 struct wim_lookup_table_entry *lte;
932 struct wim_lookup_table_entry_disk *table_buf;
933 struct wim_lookup_table_entry_disk *table_buf_ptr;
935 u64 prev_res_offset_in_wim = ~0ULL;
938 list_for_each_entry(lte, stream_list, lookup_table_list) {
939 table_size += sizeof(struct wim_lookup_table_entry_disk);
941 if (lte->out_reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS &&
942 lte->out_res_offset_in_wim != prev_res_offset_in_wim)
944 table_size += sizeof(struct wim_lookup_table_entry_disk);
945 prev_res_offset_in_wim = lte->out_res_offset_in_wim;
949 DEBUG("Writing WIM lookup table (size=%zu, offset=%"PRIu64")",
950 table_size, out_fd->offset);
952 table_buf = MALLOC(table_size);
953 if (table_buf == NULL) {
954 ERROR("Failed to allocate %zu bytes for temporary lookup table",
956 return WIMLIB_ERR_NOMEM;
958 table_buf_ptr = table_buf;
960 prev_res_offset_in_wim = ~0ULL;
961 list_for_each_entry(lte, stream_list, lookup_table_list) {
963 put_wim_lookup_table_entry(table_buf_ptr++,
968 if (lte->out_reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS &&
969 lte->out_res_offset_in_wim != prev_res_offset_in_wim)
971 /* Put the main resource entry for the pack. */
973 struct wim_reshdr reshdr;
975 reshdr.offset_in_wim = lte->out_res_offset_in_wim;
976 reshdr.size_in_wim = lte->out_res_size_in_wim;
977 reshdr.uncompressed_size = WIM_PACK_MAGIC_NUMBER;
978 reshdr.flags = WIM_RESHDR_FLAG_PACKED_STREAMS;
980 DEBUG("Putting main entry for pack: "
981 "size_in_wim=%"PRIu64", "
982 "offset_in_wim=%"PRIu64", "
983 "uncompressed_size=%"PRIu64,
985 reshdr.offset_in_wim,
986 reshdr.uncompressed_size);
988 put_wim_lookup_table_entry(table_buf_ptr++,
992 prev_res_offset_in_wim = lte->out_res_offset_in_wim;
996 wimlib_assert((u8*)table_buf_ptr - (u8*)table_buf == table_size);
998 /* Write the lookup table uncompressed. Although wimlib can handle a
999 * compressed lookup table, MS software cannot. */
1000 ret = write_wim_resource_from_buffer(table_buf,
1002 WIM_RESHDR_FLAG_METADATA,
1004 WIMLIB_COMPRESSION_TYPE_NONE,
1008 write_resource_flags);
1010 DEBUG("ret=%d", ret);
1015 lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
1017 lte->real_refcnt = 0;
1022 lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
1024 lte->out_refcnt = 0;
1029 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *_ignore)
1031 if (lte->extracted_file != NULL) {
1032 FREE(lte->extracted_file);
1033 lte->extracted_file = NULL;
1038 /* Allocate a stream entry for the contents of the buffer, or re-use an existing
1039 * entry in @lookup_table for the same stream. */
1040 struct wim_lookup_table_entry *
1041 new_stream_from_data_buffer(const void *buffer, size_t size,
1042 struct wim_lookup_table *lookup_table)
1044 u8 hash[SHA1_HASH_SIZE];
1045 struct wim_lookup_table_entry *lte, *existing_lte;
1047 sha1_buffer(buffer, size, hash);
1048 existing_lte = lookup_stream(lookup_table, hash);
1050 wimlib_assert(existing_lte->size == size);
1055 lte = new_lookup_table_entry();
1058 buffer_copy = memdup(buffer, size);
1059 if (buffer_copy == NULL) {
1060 free_lookup_table_entry(lte);
1063 lte->resource_location = RESOURCE_IN_ATTACHED_BUFFER;
1064 lte->attached_buffer = buffer_copy;
1066 copy_hash(lte->hash, hash);
1067 lookup_table_insert(lookup_table, lte);
1072 /* Calculate the SHA1 message digest of a stream and move it from the list of
1073 * unhashed streams to the stream lookup table, possibly joining it with an
1074 * existing lookup table entry for an identical stream.
1076 * @lte: An unhashed lookup table entry.
1077 * @lookup_table: Lookup table for the WIM.
1078 * @lte_ret: On success, write a pointer to the resulting lookup table
1079 * entry to this location. This will be the same as @lte
1080 * if it was inserted into the lookup table, or different if
1081 * a duplicate stream was found.
1083 * Returns 0 on success; nonzero if there is an error reading the stream.
1086 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
1087 struct wim_lookup_table *lookup_table,
1088 struct wim_lookup_table_entry **lte_ret)
1091 struct wim_lookup_table_entry *duplicate_lte;
1092 struct wim_lookup_table_entry **back_ptr;
1094 wimlib_assert(lte->unhashed);
1096 /* back_ptr must be saved because @back_inode and @back_stream_id are in
1097 * union with the SHA1 message digest and will no longer be valid once
1098 * the SHA1 has been calculated. */
1099 back_ptr = retrieve_lte_pointer(lte);
1101 ret = sha1_stream(lte);
1105 /* Look for a duplicate stream */
1106 duplicate_lte = lookup_stream(lookup_table, lte->hash);
1107 list_del(<e->unhashed_list);
1108 if (duplicate_lte) {
1109 /* We have a duplicate stream. Transfer the reference counts
1110 * from this stream to the duplicate and update the reference to
1111 * this stream (in an inode or ads_entry) to point to the
1112 * duplicate. The caller is responsible for freeing @lte if
1114 wimlib_assert(!(duplicate_lte->unhashed));
1115 wimlib_assert(duplicate_lte->size == lte->size);
1116 duplicate_lte->refcnt += lte->refcnt;
1118 *back_ptr = duplicate_lte;
1119 lte = duplicate_lte;
1121 /* No duplicate stream, so we need to insert this stream into
1122 * the lookup table and treat it as a hashed stream. */
1123 lookup_table_insert(lookup_table, lte);
1131 print_lookup_table_entry(const struct wim_lookup_table_entry *lte, FILE *out)
1134 tputc(T('\n'), out);
1139 tprintf(T("Uncompressed size = %"PRIu64" bytes\n"),
1141 if (lte->flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
1142 tprintf(T("Offset = %"PRIu64" bytes\n"),
1143 lte->offset_in_res);
1145 tprintf(T("Raw uncompressed size = %"PRIu64" bytes\n"),
1146 lte->rspec->uncompressed_size);
1148 tprintf(T("Raw compressed size = %"PRIu64" bytes\n"),
1149 lte->rspec->size_in_wim);
1151 tprintf(T("Raw offset = %"PRIu64" bytes\n"),
1152 lte->rspec->offset_in_wim);
1153 } else if (lte->resource_location == RESOURCE_IN_WIM) {
1154 tprintf(T("Compressed size = %"PRIu64" bytes\n"),
1155 lte->rspec->size_in_wim);
1157 tprintf(T("Offset = %"PRIu64" bytes\n"),
1158 lte->rspec->offset_in_wim);
1161 tfprintf(out, T("Reference Count = %u\n"), lte->refcnt);
1163 if (lte->unhashed) {
1164 tfprintf(out, T("(Unhashed: inode %p, stream_id = %u)\n"),
1165 lte->back_inode, lte->back_stream_id);
1167 tfprintf(out, T("Hash = 0x"));
1168 print_hash(lte->hash, out);
1169 tputc(T('\n'), out);
1172 tfprintf(out, T("Flags = "));
1173 u8 flags = lte->flags;
1174 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
1175 tfputs(T("WIM_RESHDR_FLAG_COMPRESSED, "), out);
1176 if (flags & WIM_RESHDR_FLAG_FREE)
1177 tfputs(T("WIM_RESHDR_FLAG_FREE, "), out);
1178 if (flags & WIM_RESHDR_FLAG_METADATA)
1179 tfputs(T("WIM_RESHDR_FLAG_METADATA, "), out);
1180 if (flags & WIM_RESHDR_FLAG_SPANNED)
1181 tfputs(T("WIM_RESHDR_FLAG_SPANNED, "), out);
1182 if (flags & WIM_RESHDR_FLAG_PACKED_STREAMS)
1183 tfputs(T("WIM_RESHDR_FLAG_PACKED_STREAMS, "), out);
1184 tputc(T('\n'), out);
1185 switch (lte->resource_location) {
1186 case RESOURCE_IN_WIM:
1187 if (lte->rspec->wim->filename) {
1188 tfprintf(out, T("WIM file = `%"TS"'\n"),
1189 lte->rspec->wim->filename);
1193 case RESOURCE_WIN32_ENCRYPTED:
1195 case RESOURCE_IN_FILE_ON_DISK:
1196 tfprintf(out, T("File on Disk = `%"TS"'\n"),
1200 case RESOURCE_IN_STAGING_FILE:
1201 tfprintf(out, T("Staging File = `%"TS"'\n"),
1202 lte->staging_file_name);
1208 tputc(T('\n'), out);
1212 lte_to_wimlib_resource_entry(const struct wim_lookup_table_entry *lte,
1213 struct wimlib_resource_entry *wentry)
1215 memset(wentry, 0, sizeof(*wentry));
1217 wentry->uncompressed_size = lte->size;
1218 if (lte->resource_location == RESOURCE_IN_WIM) {
1219 wentry->part_number = lte->rspec->wim->hdr.part_number;
1220 if (lte->flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
1221 wentry->compressed_size = 0;
1222 wentry->offset = lte->offset_in_res;
1224 wentry->compressed_size = lte->rspec->size_in_wim;
1225 wentry->offset = lte->rspec->offset_in_wim;
1227 wentry->raw_resource_offset_in_wim = lte->rspec->offset_in_wim;
1228 /*wentry->raw_resource_uncompressed_size = lte->rspec->uncompressed_size;*/
1229 wentry->raw_resource_compressed_size = lte->rspec->size_in_wim;
1231 copy_hash(wentry->sha1_hash, lte->hash);
1232 wentry->reference_count = lte->refcnt;
1233 wentry->is_compressed = (lte->flags & WIM_RESHDR_FLAG_COMPRESSED) != 0;
1234 wentry->is_metadata = (lte->flags & WIM_RESHDR_FLAG_METADATA) != 0;
1235 wentry->is_free = (lte->flags & WIM_RESHDR_FLAG_FREE) != 0;
1236 wentry->is_spanned = (lte->flags & WIM_RESHDR_FLAG_SPANNED) != 0;
1237 wentry->packed = (lte->flags & WIM_RESHDR_FLAG_PACKED_STREAMS) != 0;
1240 struct iterate_lte_context {
1241 wimlib_iterate_lookup_table_callback_t cb;
1246 do_iterate_lte(struct wim_lookup_table_entry *lte, void *_ctx)
1248 struct iterate_lte_context *ctx = _ctx;
1249 struct wimlib_resource_entry entry;
1251 lte_to_wimlib_resource_entry(lte, &entry);
1252 return (*ctx->cb)(&entry, ctx->user_ctx);
1255 /* API function documented in wimlib.h */
1257 wimlib_iterate_lookup_table(WIMStruct *wim, int flags,
1258 wimlib_iterate_lookup_table_callback_t cb,
1261 struct iterate_lte_context ctx = {
1263 .user_ctx = user_ctx,
1265 if (wim->hdr.part_number == 1) {
1267 for (int i = 0; i < wim->hdr.image_count; i++) {
1268 ret = do_iterate_lte(wim->image_metadata[i]->metadata_lte,
1274 return for_lookup_table_entry(wim->lookup_table, do_iterate_lte, &ctx);