4 * Lookup table, implemented as a hash table, that maps SHA1 message digests to
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/.
27 #include "wimlib_internal.h"
28 #include "lookup_table.h"
29 #include "buffer_io.h"
37 struct wim_lookup_table *
38 new_lookup_table(size_t capacity)
40 struct wim_lookup_table *table;
41 struct hlist_head *array;
43 table = CALLOC(1, sizeof(struct wim_lookup_table));
45 array = CALLOC(capacity, sizeof(array[0]));
47 table->num_entries = 0;
48 table->capacity = capacity;
53 ERROR("Failed to allocate memory for lookup table with capacity %zu",
60 struct wim_lookup_table_entry *
61 new_lookup_table_entry()
63 struct wim_lookup_table_entry *lte;
65 lte = CALLOC(1, sizeof(struct wim_lookup_table_entry));
70 ERROR("Out of memory (tried to allocate %zu bytes for "
71 "lookup table entry)",
72 sizeof(struct wim_lookup_table_entry));
77 struct wim_lookup_table_entry *
78 clone_lookup_table_entry(const struct wim_lookup_table_entry *old)
80 struct wim_lookup_table_entry *new;
82 new = MALLOC(sizeof(*new));
86 memcpy(new, old, sizeof(*old));
87 new->extracted_file = NULL;
88 switch (new->resource_location) {
91 case RESOURCE_WIN32_ENCRYPTED:
93 case RESOURCE_IN_FILE_ON_DISK:
95 case RESOURCE_IN_STAGING_FILE:
96 BUILD_BUG_ON((void*)&old->file_on_disk !=
97 (void*)&old->staging_file_name);
99 new->file_on_disk = TSTRDUP(old->file_on_disk);
100 if (!new->file_on_disk)
103 case RESOURCE_IN_ATTACHED_BUFFER:
104 new->attached_buffer = MALLOC(wim_resource_size(old));
105 if (!new->attached_buffer)
107 memcpy(new->attached_buffer, old->attached_buffer,
108 wim_resource_size(old));
111 case RESOURCE_IN_NTFS_VOLUME:
113 struct ntfs_location *loc;
114 loc = MALLOC(sizeof(*loc));
117 memcpy(loc, old->ntfs_loc, sizeof(*loc));
119 loc->stream_name = NULL;
121 loc->path = STRDUP(old->ntfs_loc->path);
124 loc->stream_name = MALLOC((loc->stream_name_nchars + 1) * 2);
125 if (!loc->stream_name)
127 memcpy(loc->stream_name,
128 old->ntfs_loc->stream_name,
129 (loc->stream_name_nchars + 1) * 2);
138 free_lookup_table_entry(new);
143 free_lookup_table_entry(struct wim_lookup_table_entry *lte)
146 switch (lte->resource_location) {
149 case RESOURCE_WIN32_ENCRYPTED:
152 case RESOURCE_IN_STAGING_FILE:
153 BUILD_BUG_ON((void*)<e->file_on_disk !=
154 (void*)<e->staging_file_name);
156 case RESOURCE_IN_FILE_ON_DISK:
157 case RESOURCE_IN_ATTACHED_BUFFER:
158 BUILD_BUG_ON((void*)<e->file_on_disk !=
159 (void*)<e->attached_buffer);
160 FREE(lte->file_on_disk);
163 case RESOURCE_IN_NTFS_VOLUME:
165 FREE(lte->ntfs_loc->path);
166 FREE(lte->ntfs_loc->stream_name);
179 do_free_lookup_table_entry(struct wim_lookup_table_entry *entry, void *ignore)
181 free_lookup_table_entry(entry);
187 free_lookup_table(struct wim_lookup_table *table)
189 DEBUG2("Freeing lookup table");
192 for_lookup_table_entry(table,
193 do_free_lookup_table_entry,
202 * Inserts an entry into the lookup table.
204 * @table: A pointer to the lookup table.
205 * @lte: A pointer to the entry to insert.
208 lookup_table_insert(struct wim_lookup_table *table,
209 struct wim_lookup_table_entry *lte)
211 size_t i = lte->hash_short % table->capacity;
212 hlist_add_head(<e->hash_list, &table->array[i]);
214 /* XXX Make the table grow when too many entries have been inserted. */
215 table->num_entries++;
219 finalize_lte(struct wim_lookup_table_entry *lte)
222 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
223 unlink(lte->staging_file_name);
224 list_del(<e->unhashed_list);
227 free_lookup_table_entry(lte);
230 /* Decrements the reference count for the lookup table entry @lte. If its
231 * reference count reaches 0, it is unlinked from the lookup table. If,
232 * furthermore, the entry has no opened file descriptors associated with it, the
235 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
236 struct wim_lookup_table *table)
238 wimlib_assert(lte != NULL);
239 wimlib_assert(lte->refcnt != 0);
240 if (--lte->refcnt == 0) {
242 lookup_table_unlink(table, lte);
244 if (lte->num_opened_fds == 0)
252 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte)
254 if (lte->num_opened_fds != 0)
255 if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
260 /* Calls a function on all the entries in the WIM lookup table. Stop early and
261 * return nonzero if any call to the function returns nonzero. */
263 for_lookup_table_entry(struct wim_lookup_table *table,
264 int (*visitor)(struct wim_lookup_table_entry *, void *),
267 struct wim_lookup_table_entry *lte;
268 struct hlist_node *pos, *tmp;
271 for (size_t i = 0; i < table->capacity; i++) {
272 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
275 wimlib_assert2(!(lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA));
276 ret = visitor(lte, arg);
285 cmp_streams_by_wim_position(const void *p1, const void *p2)
287 const struct wim_lookup_table_entry *lte1, *lte2;
288 lte1 = *(const struct wim_lookup_table_entry**)p1;
289 lte2 = *(const struct wim_lookup_table_entry**)p2;
290 if (lte1->resource_entry.offset < lte2->resource_entry.offset)
292 else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
300 add_lte_to_array(struct wim_lookup_table_entry *lte,
303 struct wim_lookup_table_entry ***pp = _pp;
308 /* Iterate through the lookup table entries, but first sort them by stream
309 * offset in the WIM. Caution: this is intended to be used when the stream
310 * offset field has actually been set. */
312 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
313 int (*visitor)(struct wim_lookup_table_entry *,
317 struct wim_lookup_table_entry **lte_array, **p;
318 size_t num_streams = table->num_entries;
321 lte_array = MALLOC(num_streams * sizeof(lte_array[0]));
323 return WIMLIB_ERR_NOMEM;
325 for_lookup_table_entry(table, add_lte_to_array, &p);
327 wimlib_assert(p == lte_array + num_streams);
329 qsort(lte_array, num_streams, sizeof(lte_array[0]),
330 cmp_streams_by_wim_position);
332 for (size_t i = 0; i < num_streams; i++) {
333 ret = visitor(lte_array[i], arg);
342 * Reads the lookup table from a WIM file.
344 * Saves lookup table entries for non-metadata streams in a hash table, and
345 * saves the metadata entry for each image in a special per-image location (the
346 * image_metadata array).
349 read_lookup_table(WIMStruct *w)
352 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
354 struct wim_lookup_table *table;
355 struct wim_lookup_table_entry *cur_entry, *duplicate_entry;
357 if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
358 ERROR("Didn't expect a compressed lookup table!");
359 ERROR("Ask the author to implement support for this.");
360 return WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
363 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
364 w->hdr.lookup_table_res_entry.offset,
365 w->hdr.lookup_table_res_entry.original_size);
367 if (fseeko(w->fp, w->hdr.lookup_table_res_entry.offset, SEEK_SET) != 0)
369 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
371 w->hdr.lookup_table_res_entry.offset);
372 return WIMLIB_ERR_READ;
375 num_entries = w->hdr.lookup_table_res_entry.original_size /
376 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
377 table = new_lookup_table(num_entries * 2 + 1);
379 return WIMLIB_ERR_NOMEM;
381 w->current_image = 0;
382 while (num_entries--) {
385 if (fread(buf, 1, sizeof(buf), w->fp) != sizeof(buf)) {
387 ERROR("Unexpected EOF in WIM lookup table!");
389 ERROR_WITH_ERRNO("Error reading WIM lookup "
392 ret = WIMLIB_ERR_READ;
393 goto out_free_lookup_table;
395 cur_entry = new_lookup_table_entry();
397 ret = WIMLIB_ERR_NOMEM;
398 goto out_free_lookup_table;
402 cur_entry->resource_location = RESOURCE_IN_WIM;
403 p = get_resource_entry(buf, &cur_entry->resource_entry);
404 p = get_u16(p, &cur_entry->part_number);
405 p = get_u32(p, &cur_entry->refcnt);
406 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
408 if (cur_entry->part_number != w->hdr.part_number) {
409 ERROR("A lookup table entry in part %hu of the WIM "
410 "points to part %hu",
411 w->hdr.part_number, cur_entry->part_number);
412 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
413 goto out_free_cur_entry;
416 if (is_zero_hash(cur_entry->hash)) {
417 ERROR("The WIM lookup table contains an entry with a "
418 "SHA1 message digest of all 0's");
419 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
420 goto out_free_cur_entry;
423 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
424 && (cur_entry->resource_entry.size !=
425 cur_entry->resource_entry.original_size))
427 #ifdef ENABLE_ERROR_MESSAGES
428 ERROR("Found uncompressed resource with original size "
429 "not the same as compressed size");
430 ERROR("The lookup table entry for the resource is as follows:");
431 print_lookup_table_entry(cur_entry, stderr);
433 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
434 goto out_free_cur_entry;
437 if (cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
438 /* Lookup table entry for a metadata resource */
439 if (cur_entry->refcnt != 1) {
440 #ifdef ENABLE_ERROR_MESSAGES
441 ERROR("Found metadata resource with refcnt != 1:");
442 print_lookup_table_entry(cur_entry, stderr);
444 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
445 goto out_free_cur_entry;
448 if (w->hdr.part_number != 1) {
449 ERROR("Found a metadata resource in a "
450 "non-first part of the split WIM!");
451 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
452 goto out_free_cur_entry;
454 if (w->current_image == w->hdr.image_count) {
455 ERROR("The WIM header says there are %u images "
456 "in the WIM, but we found more metadata "
457 "resources than this", w->hdr.image_count);
458 ret = WIMLIB_ERR_IMAGE_COUNT;
459 goto out_free_cur_entry;
462 /* Notice very carefully: We are assigning the metadata
463 * resources in the exact order mirrored by their lookup
464 * table entries on disk, which is the behavior of
465 * Microsoft's software. In particular, this overrides
466 * the actual locations of the metadata resources
467 * themselves in the WIM file as well as any information
468 * written in the XML data. */
469 DEBUG("Found metadata resource for image %u at "
471 w->current_image + 1,
472 cur_entry->resource_entry.offset);
474 w->current_image++]->metadata_lte = cur_entry;
476 /* Lookup table entry for a stream that is not a
477 * metadata resource */
478 duplicate_entry = __lookup_resource(table, cur_entry->hash);
479 if (duplicate_entry) {
480 #ifdef ENABLE_ERROR_MESSAGES
481 ERROR("The WIM lookup table contains two entries with the "
482 "same SHA1 message digest!");
483 ERROR("The first entry is:");
484 print_lookup_table_entry(duplicate_entry, stderr);
485 ERROR("The second entry is:");
486 print_lookup_table_entry(cur_entry, stderr);
488 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
489 goto out_free_cur_entry;
491 lookup_table_insert(table, cur_entry);
495 if (w->hdr.part_number == 1 &&
496 w->current_image != w->hdr.image_count)
498 ERROR("The WIM header says there are %u images "
499 "in the WIM, but we only found %d metadata "
500 "resources!", w->hdr.image_count, w->current_image);
501 ret = WIMLIB_ERR_IMAGE_COUNT;
502 goto out_free_lookup_table;
504 DEBUG("Done reading lookup table.");
505 w->lookup_table = table;
510 out_free_lookup_table:
511 free_lookup_table(table);
513 w->current_image = 0;
519 * Writes a lookup table entry to the output file.
522 write_lookup_table_entry(struct wim_lookup_table_entry *lte, void *_out)
525 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
530 /* Don't write entries that have not had file resources or metadata
531 * resources written for them. */
532 if (lte->out_refcnt == 0)
535 if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
536 DEBUG("Writing metadata entry at %"PRIu64" "
537 "(orig size = %"PRIu64")",
538 ftello(out), lte->output_resource_entry.original_size);
541 p = put_resource_entry(buf, <e->output_resource_entry);
542 p = put_u16(p, lte->part_number);
543 p = put_u32(p, lte->out_refcnt);
544 p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
545 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
546 ERROR_WITH_ERRNO("Failed to write lookup table entry");
547 return WIMLIB_ERR_WRITE;
552 /* Writes the WIM lookup table to the output file. */
554 write_lookup_table(WIMStruct *w, int image, struct resource_entry *out_res_entry)
556 FILE *out = w->out_fp;
557 off_t start_offset, end_offset;
559 int start_image, end_image;
561 start_offset = ftello(out);
562 if (start_offset == -1)
563 return WIMLIB_ERR_WRITE;
565 /* Write lookup table entries for metadata resources */
566 if (image == WIMLIB_ALL_IMAGES) {
568 end_image = w->hdr.image_count;
573 for (int i = start_image; i <= end_image; i++) {
574 struct wim_lookup_table_entry *metadata_lte;
576 metadata_lte = w->image_metadata[i - 1]->metadata_lte;
577 metadata_lte->out_refcnt = 1;
578 metadata_lte->output_resource_entry.flags |= WIM_RESHDR_FLAG_METADATA;
579 ret = write_lookup_table_entry(metadata_lte, out);
584 /* Write lookup table entries for other resources */
585 ret = for_lookup_table_entry(w->lookup_table, write_lookup_table_entry, out);
589 /* Fill in the resource entry for the lookup table itself */
590 end_offset = ftello(out);
591 if (end_offset == -1)
592 return WIMLIB_ERR_WRITE;
594 out_res_entry->offset = start_offset;
595 out_res_entry->size = end_offset - start_offset;
596 out_res_entry->original_size = end_offset - start_offset;
597 out_res_entry->flags = WIM_RESHDR_FLAG_METADATA;
602 lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
604 lte->real_refcnt = 0;
609 lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
616 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *_ignore)
618 if (lte->extracted_file != NULL) {
619 FREE(lte->extracted_file);
620 lte->extracted_file = NULL;
626 print_lookup_table_entry(const struct wim_lookup_table_entry *lte, FILE *out)
632 tfprintf(out, T("Offset = %"PRIu64" bytes\n"),
633 lte->resource_entry.offset);
635 tfprintf(out, T("Size = %"PRIu64" bytes\n"),
636 (u64)lte->resource_entry.size);
638 tfprintf(out, T("Original size = %"PRIu64" bytes\n"),
639 lte->resource_entry.original_size);
641 tfprintf(out, T("Part Number = %hu\n"), lte->part_number);
642 tfprintf(out, T("Reference Count = %u\n"), lte->refcnt);
645 tfprintf(out, T("(Unhashed: inode %p, stream_id = %u)\n"),
646 lte->back_inode, lte->back_stream_id);
648 tfprintf(out, T("Hash = 0x"));
649 print_hash(lte->hash, out);
653 tfprintf(out, T("Flags = "));
654 u8 flags = lte->resource_entry.flags;
655 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
656 tfputs(T("WIM_RESHDR_FLAG_COMPRESSED, "), out);
657 if (flags & WIM_RESHDR_FLAG_FREE)
658 tfputs(T("WIM_RESHDR_FLAG_FREE, "), out);
659 if (flags & WIM_RESHDR_FLAG_METADATA)
660 tfputs(T("WIM_RESHDR_FLAG_METADATA, "), out);
661 if (flags & WIM_RESHDR_FLAG_SPANNED)
662 tfputs(T("WIM_RESHDR_FLAG_SPANNED, "), out);
664 switch (lte->resource_location) {
665 case RESOURCE_IN_WIM:
666 if (lte->wim->filename) {
667 tfprintf(out, T("WIM file = `%"TS"'\n"),
673 case RESOURCE_WIN32_ENCRYPTED:
675 case RESOURCE_IN_FILE_ON_DISK:
676 tfprintf(out, T("File on Disk = `%"TS"'\n"),
680 case RESOURCE_IN_STAGING_FILE:
681 tfprintf(out, T("Staging File = `%"TS"'\n"),
682 lte->staging_file_name);
692 do_print_lookup_table_entry(struct wim_lookup_table_entry *lte, void *fp)
694 print_lookup_table_entry(lte, (FILE*)fp);
699 * Prints the lookup table of a WIM file.
702 wimlib_print_lookup_table(WIMStruct *w)
704 for_lookup_table_entry(w->lookup_table,
705 do_print_lookup_table_entry,
709 /* Given a SHA1 message digest, return the corresponding entry in the WIM's
710 * lookup table, or NULL if there is none. */
711 struct wim_lookup_table_entry *
712 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[])
715 struct wim_lookup_table_entry *lte;
716 struct hlist_node *pos;
718 wimlib_assert(table != NULL);
719 wimlib_assert(hash != NULL);
721 i = *(size_t*)hash % table->capacity;
722 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
723 if (hashes_equal(hash, lte->hash))
730 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
733 * This is only for pre-resolved inodes.
736 lookup_resource(WIMStruct *w,
739 struct wim_dentry **dentry_ret,
740 struct wim_lookup_table_entry **lte_ret,
743 struct wim_dentry *dentry;
744 struct wim_lookup_table_entry *lte;
746 const tchar *stream_name = NULL;
747 struct wim_inode *inode;
750 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
751 stream_name = path_stream_name(path);
753 p = (tchar*)stream_name - 1;
758 dentry = get_dentry(w, path);
764 inode = dentry->d_inode;
766 wimlib_assert(inode->i_resolved);
768 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
769 && inode_is_directory(inode))
773 struct wim_ads_entry *ads_entry;
775 ads_entry = inode_get_ads_entry(inode, stream_name,
778 stream_idx = ads_idx + 1;
779 lte = ads_entry->lte;
790 *dentry_ret = dentry;
794 *stream_idx_ret = stream_idx;
799 /* Resolve an inode's lookup table entries
801 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
802 * lookup table) with pointers directly to the lookup table entries. A circular
803 * linked list of streams sharing the same lookup table entry is created.
805 * This function always succeeds; unresolved lookup table entries are given a
809 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table)
812 if (!inode->i_resolved) {
813 struct wim_lookup_table_entry *lte;
814 /* Resolve the default file stream */
815 lte = __lookup_resource(table, inode->i_hash);
817 inode->i_resolved = 1;
819 /* Resolve the alternate data streams */
820 for (u16 i = 0; i < inode->i_num_ads; i++) {
821 struct wim_ads_entry *cur_entry = &inode->i_ads_entries[i];
822 lte = __lookup_resource(table, cur_entry->hash);
823 cur_entry->lte = lte;
829 inode_unresolve_ltes(struct wim_inode *inode)
831 if (inode->i_resolved) {
833 copy_hash(inode->i_hash, inode->i_lte->hash);
835 zero_out_hash(inode->i_hash);
837 for (u16 i = 0; i < inode->i_num_ads; i++) {
838 if (inode->i_ads_entries[i].lte)
839 copy_hash(inode->i_ads_entries[i].hash,
840 inode->i_ads_entries[i].lte->hash);
842 zero_out_hash(inode->i_ads_entries[i].hash);
844 inode->i_resolved = 0;
849 * Returns the lookup table entry for stream @stream_idx of the inode, where
850 * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
851 * corresponds to an alternate data stream.
853 * This works for both resolved and un-resolved inodes.
855 struct wim_lookup_table_entry *
856 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
857 const struct wim_lookup_table *table)
859 if (inode->i_resolved)
860 return inode_stream_lte_resolved(inode, stream_idx);
862 return inode_stream_lte_unresolved(inode, stream_idx, table);
866 /* Return the lookup table entry for the unnamed data stream of an inode, or
867 * NULL if there is none.
869 * You'd think this would be easier than it actually is, since the unnamed data
870 * stream should be the one referenced from the inode itself. Alas, if there
871 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
872 * data stream in one of the alternate data streams instead of inside the WIM
873 * dentry itself. So we need to check the alternate data streams too.
875 * Also, note that a dentry may appear to have more than one unnamed stream, but
876 * if the SHA1 message digest is all 0's then the corresponding stream does not
877 * really "count" (this is the case for the inode's own file stream when the
878 * file stream that should be there is actually in one of the alternate stream
879 * entries.). This is despite the fact that we may need to extract such a
880 * missing entry as an empty file or empty named data stream.
882 struct wim_lookup_table_entry *
883 inode_unnamed_lte(const struct wim_inode *inode,
884 const struct wim_lookup_table *table)
886 if (inode->i_resolved)
887 return inode_unnamed_lte_resolved(inode);
889 return inode_unnamed_lte_unresolved(inode, table);
893 lte_add_stream_size(struct wim_lookup_table_entry *lte, void *total_bytes_p)
895 *(u64*)total_bytes_p += lte->resource_entry.size;
900 lookup_table_total_stream_size(struct wim_lookup_table *table)
903 for_lookup_table_entry(table, lte_add_stream_size, &total_size);
907 struct wim_lookup_table_entry **
908 retrieve_lte_pointer(struct wim_lookup_table_entry *lte)
910 wimlib_assert(lte->unhashed);
911 struct wim_inode *inode = lte->back_inode;
912 u32 stream_id = lte->back_stream_id;
914 return &inode->i_lte;
916 for (u16 i = 0; i < inode->i_num_ads; i++)
917 if (inode->i_ads_entries[i].stream_id == stream_id)
918 return &inode->i_ads_entries[i].lte;
923 /* Calculate the SHA1 message digest of a stream and move it from the list of
924 * unhashed streams to the stream lookup table, possibly joining it with an
925 * existing lookup table entry for an identical stream.
927 * @lte: An unhashed lookup table entry.
928 * @lookup_table: Lookup table for the WIM.
929 * @lte_ret: On success, write a pointer to the resulting lookup table
930 * entry to this location. This will be the same as @lte
931 * if it was inserted into the lookup table, or different if
932 * a duplicate stream was found.
934 * Returns 0 on success; nonzero if there is an error reading the stream.
937 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
938 struct wim_lookup_table *lookup_table,
939 struct wim_lookup_table_entry **lte_ret)
942 struct wim_lookup_table_entry *duplicate_lte;
943 struct wim_lookup_table_entry **back_ptr;
945 wimlib_assert(lte->unhashed);
947 /* back_ptr must be saved because @back_inode and @back_stream_id are in
948 * union with the SHA1 message digest and will no longer be valid once
949 * the SHA1 has been calculated. */
950 back_ptr = retrieve_lte_pointer(lte);
952 ret = sha1_resource(lte);
956 /* Look for a duplicate stream */
957 duplicate_lte = __lookup_resource(lookup_table, lte->hash);
958 list_del(<e->unhashed_list);
960 /* We have a duplicate stream. Transfer the reference counts
961 * from this stream to the duplicate, update the reference to
962 * this stream (in an inode or ads_entry) to point to the
963 * duplicate, then free this stream. */
964 wimlib_assert(!(duplicate_lte->unhashed));
965 duplicate_lte->refcnt += lte->refcnt;
966 duplicate_lte->out_refcnt += lte->refcnt;
967 *back_ptr = duplicate_lte;
968 free_lookup_table_entry(lte);
971 /* No duplicate stream, so we need to insert
972 * this stream into the lookup table and treat
973 * it as a hashed stream. */
974 lookup_table_insert(lookup_table, lte);