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:
94 case RESOURCE_IN_STAGING_FILE:
96 case RESOURCE_IN_FILE_ON_DISK:
97 BUILD_BUG_ON((void*)&old->file_on_disk !=
98 (void*)&old->staging_file_name);
99 new->staging_file_name = TSTRDUP(old->staging_file_name);
100 if (!new->staging_file_name)
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) {
147 case RESOURCE_IN_STAGING_FILE:
148 case RESOURCE_IN_ATTACHED_BUFFER:
149 case RESOURCE_IN_FILE_ON_DISK:
153 BUILD_BUG_ON((void*)<e->file_on_disk !=
154 (void*)<e->staging_file_name);
155 BUILD_BUG_ON((void*)<e->file_on_disk !=
156 (void*)<e->attached_buffer);
157 FREE(lte->file_on_disk);
160 case RESOURCE_IN_NTFS_VOLUME:
162 FREE(lte->ntfs_loc->path);
163 FREE(lte->ntfs_loc->stream_name);
176 do_free_lookup_table_entry(struct wim_lookup_table_entry *entry, void *ignore)
178 free_lookup_table_entry(entry);
184 free_lookup_table(struct wim_lookup_table *table)
186 DEBUG2("Freeing lookup table");
189 for_lookup_table_entry(table,
190 do_free_lookup_table_entry,
199 * Inserts an entry into the lookup table.
201 * @table: A pointer to the lookup table.
202 * @lte: A pointer to the entry to insert.
205 lookup_table_insert(struct wim_lookup_table *table,
206 struct wim_lookup_table_entry *lte)
208 size_t i = lte->hash_short % table->capacity;
209 hlist_add_head(<e->hash_list, &table->array[i]);
211 /* XXX Make the table grow when too many entries have been inserted. */
212 table->num_entries++;
216 finalize_lte(struct wim_lookup_table_entry *lte)
219 if (lte->resource_location == RESOURCE_IN_STAGING_FILE) {
220 unlink(lte->staging_file_name);
221 list_del(<e->unhashed_list);
224 free_lookup_table_entry(lte);
227 /* Decrements the reference count for the lookup table entry @lte. If its
228 * reference count reaches 0, it is unlinked from the lookup table. If,
229 * furthermore, the entry has no opened file descriptors associated with it, the
232 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
233 struct wim_lookup_table *table)
235 wimlib_assert(lte != NULL);
236 wimlib_assert(lte->refcnt != 0);
237 if (--lte->refcnt == 0) {
239 lookup_table_unlink(table, lte);
241 if (lte->num_opened_fds == 0)
249 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte)
251 if (lte->num_opened_fds != 0)
252 if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
257 /* Calls a function on all the entries in the WIM lookup table. Stop early and
258 * return nonzero if any call to the function returns nonzero. */
260 for_lookup_table_entry(struct wim_lookup_table *table,
261 int (*visitor)(struct wim_lookup_table_entry *, void *),
264 struct wim_lookup_table_entry *lte;
265 struct hlist_node *pos, *tmp;
268 for (size_t i = 0; i < table->capacity; i++) {
269 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
272 wimlib_assert2(!(lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA));
273 ret = visitor(lte, arg);
282 cmp_streams_by_wim_position(const void *p1, const void *p2)
284 const struct wim_lookup_table_entry *lte1, *lte2;
285 lte1 = *(const struct wim_lookup_table_entry**)p1;
286 lte2 = *(const struct wim_lookup_table_entry**)p2;
287 if (lte1->resource_entry.offset < lte2->resource_entry.offset)
289 else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
297 add_lte_to_array(struct wim_lookup_table_entry *lte,
300 struct wim_lookup_table_entry ***pp = _pp;
305 /* Iterate through the lookup table entries, but first sort them by stream
306 * offset in the WIM. Caution: this is intended to be used when the stream
307 * offset field has actually been set. */
309 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
310 int (*visitor)(struct wim_lookup_table_entry *,
314 struct wim_lookup_table_entry **lte_array, **p;
315 size_t num_streams = table->num_entries;
318 lte_array = MALLOC(num_streams * sizeof(lte_array[0]));
320 return WIMLIB_ERR_NOMEM;
322 for_lookup_table_entry(table, add_lte_to_array, &p);
324 wimlib_assert(p == lte_array + num_streams);
326 qsort(lte_array, num_streams, sizeof(lte_array[0]),
327 cmp_streams_by_wim_position);
329 for (size_t i = 0; i < num_streams; i++) {
330 ret = visitor(lte_array[i], arg);
339 * Reads the lookup table from a WIM file.
341 * Saves lookup table entries for non-metadata streams in a hash table, and
342 * saves the metadata entry for each image in a special per-image location (the
343 * image_metadata array).
346 read_lookup_table(WIMStruct *w)
349 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
351 struct wim_lookup_table *table;
352 struct wim_lookup_table_entry *cur_entry, *duplicate_entry;
354 if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
355 ERROR("Didn't expect a compressed lookup table!");
356 ERROR("Ask the author to implement support for this.");
357 return WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
360 DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
361 w->hdr.lookup_table_res_entry.offset,
362 w->hdr.lookup_table_res_entry.original_size);
364 if (fseeko(w->fp, w->hdr.lookup_table_res_entry.offset, SEEK_SET) != 0)
366 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
368 w->hdr.lookup_table_res_entry.offset);
369 return WIMLIB_ERR_READ;
372 num_entries = w->hdr.lookup_table_res_entry.original_size /
373 WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
374 table = new_lookup_table(num_entries * 2 + 1);
376 return WIMLIB_ERR_NOMEM;
378 w->current_image = 0;
379 while (num_entries--) {
382 if (fread(buf, 1, sizeof(buf), w->fp) != sizeof(buf)) {
384 ERROR("Unexpected EOF in WIM lookup table!");
386 ERROR_WITH_ERRNO("Error reading WIM lookup "
389 ret = WIMLIB_ERR_READ;
390 goto out_free_lookup_table;
392 cur_entry = new_lookup_table_entry();
394 ret = WIMLIB_ERR_NOMEM;
395 goto out_free_lookup_table;
399 cur_entry->resource_location = RESOURCE_IN_WIM;
400 p = get_resource_entry(buf, &cur_entry->resource_entry);
401 p = get_u16(p, &cur_entry->part_number);
402 p = get_u32(p, &cur_entry->refcnt);
403 p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
405 if (cur_entry->part_number != w->hdr.part_number) {
406 ERROR("A lookup table entry in part %hu of the WIM "
407 "points to part %hu",
408 w->hdr.part_number, cur_entry->part_number);
409 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
410 goto out_free_cur_entry;
413 if (is_zero_hash(cur_entry->hash)) {
414 ERROR("The WIM lookup table contains an entry with a "
415 "SHA1 message digest of all 0's");
416 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
417 goto out_free_cur_entry;
420 if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
421 && (cur_entry->resource_entry.size !=
422 cur_entry->resource_entry.original_size))
424 #ifdef ENABLE_ERROR_MESSAGES
425 ERROR("Found uncompressed resource with original size "
426 "not the same as compressed size");
427 ERROR("The lookup table entry for the resource is as follows:");
428 print_lookup_table_entry(cur_entry, stderr);
430 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
431 goto out_free_cur_entry;
434 if (cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
435 /* Lookup table entry for a metadata resource */
436 if (cur_entry->refcnt != 1) {
437 #ifdef ENABLE_ERROR_MESSAGES
438 ERROR("Found metadata resource with refcnt != 1:");
439 print_lookup_table_entry(cur_entry, stderr);
441 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
442 goto out_free_cur_entry;
445 if (w->hdr.part_number != 1) {
446 ERROR("Found a metadata resource in a "
447 "non-first part of the split WIM!");
448 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
449 goto out_free_cur_entry;
451 if (w->current_image == w->hdr.image_count) {
452 ERROR("The WIM header says there are %u images "
453 "in the WIM, but we found more metadata "
454 "resources than this", w->hdr.image_count);
455 ret = WIMLIB_ERR_IMAGE_COUNT;
456 goto out_free_cur_entry;
459 /* Notice very carefully: We are assigning the metadata
460 * resources in the exact order mirrored by their lookup
461 * table entries on disk, which is the behavior of
462 * Microsoft's software. In particular, this overrides
463 * the actual locations of the metadata resources
464 * themselves in the WIM file as well as any information
465 * written in the XML data. */
466 DEBUG("Found metadata resource for image %u at "
468 w->current_image + 1,
469 cur_entry->resource_entry.offset);
471 w->current_image++]->metadata_lte = cur_entry;
473 /* Lookup table entry for a stream that is not a
474 * metadata resource */
475 duplicate_entry = __lookup_resource(table, cur_entry->hash);
476 if (duplicate_entry) {
477 #ifdef ENABLE_ERROR_MESSAGES
478 ERROR("The WIM lookup table contains two entries with the "
479 "same SHA1 message digest!");
480 ERROR("The first entry is:");
481 print_lookup_table_entry(duplicate_entry, stderr);
482 ERROR("The second entry is:");
483 print_lookup_table_entry(cur_entry, stderr);
485 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
486 goto out_free_cur_entry;
488 lookup_table_insert(table, cur_entry);
492 if (w->hdr.part_number == 1 &&
493 w->current_image != w->hdr.image_count)
495 ERROR("The WIM header says there are %u images "
496 "in the WIM, but we only found %d metadata "
497 "resources!", w->hdr.image_count, w->current_image);
498 ret = WIMLIB_ERR_IMAGE_COUNT;
499 goto out_free_lookup_table;
501 DEBUG("Done reading lookup table.");
502 w->lookup_table = table;
507 out_free_lookup_table:
508 free_lookup_table(table);
510 w->current_image = 0;
516 * Writes a lookup table entry to the output file.
519 write_lookup_table_entry(struct wim_lookup_table_entry *lte, void *_out)
522 u8 buf[WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
527 /* Don't write entries that have not had file resources or metadata
528 * resources written for them. */
529 if (lte->out_refcnt == 0)
532 if (lte->output_resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
533 DEBUG("Writing metadata entry at %"PRIu64" "
534 "(orig size = %"PRIu64")",
535 ftello(out), lte->output_resource_entry.original_size);
538 p = put_resource_entry(buf, <e->output_resource_entry);
539 p = put_u16(p, lte->part_number);
540 p = put_u32(p, lte->out_refcnt);
541 p = put_bytes(p, SHA1_HASH_SIZE, lte->hash);
542 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
543 ERROR_WITH_ERRNO("Failed to write lookup table entry");
544 return WIMLIB_ERR_WRITE;
549 /* Writes the WIM lookup table to the output file. */
551 write_lookup_table(WIMStruct *w, int image, struct resource_entry *out_res_entry)
553 FILE *out = w->out_fp;
554 off_t start_offset, end_offset;
556 int start_image, end_image;
558 start_offset = ftello(out);
559 if (start_offset == -1)
560 return WIMLIB_ERR_WRITE;
562 /* Write lookup table entries for metadata resources */
563 if (image == WIMLIB_ALL_IMAGES) {
565 end_image = w->hdr.image_count;
570 for (int i = start_image; i <= end_image; i++) {
571 struct wim_lookup_table_entry *metadata_lte;
573 metadata_lte = w->image_metadata[i - 1]->metadata_lte;
574 metadata_lte->out_refcnt = 1;
575 metadata_lte->output_resource_entry.flags |= WIM_RESHDR_FLAG_METADATA;
576 ret = write_lookup_table_entry(metadata_lte, out);
581 /* Write lookup table entries for other resources */
582 ret = for_lookup_table_entry(w->lookup_table, write_lookup_table_entry, out);
586 /* Fill in the resource entry for the lookup table itself */
587 end_offset = ftello(out);
588 if (end_offset == -1)
589 return WIMLIB_ERR_WRITE;
591 out_res_entry->offset = start_offset;
592 out_res_entry->size = end_offset - start_offset;
593 out_res_entry->original_size = end_offset - start_offset;
594 out_res_entry->flags = WIM_RESHDR_FLAG_METADATA;
599 lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
601 lte->real_refcnt = 0;
606 lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
613 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *_ignore)
615 if (lte->extracted_file != NULL) {
616 FREE(lte->extracted_file);
617 lte->extracted_file = NULL;
623 print_lookup_table_entry(const struct wim_lookup_table_entry *lte, FILE *out)
629 tfprintf(out, T("Offset = %"PRIu64" bytes\n"),
630 lte->resource_entry.offset);
632 tfprintf(out, T("Size = %"PRIu64" bytes\n"),
633 (u64)lte->resource_entry.size);
635 tfprintf(out, T("Original size = %"PRIu64" bytes\n"),
636 lte->resource_entry.original_size);
638 tfprintf(out, T("Part Number = %hu\n"), lte->part_number);
639 tfprintf(out, T("Reference Count = %u\n"), lte->refcnt);
642 tfprintf(out, T("(Unhashed: inode %p, stream_id = %u)\n"),
643 lte->back_inode, lte->back_stream_id);
645 tfprintf(out, T("Hash = 0x"));
646 print_hash(lte->hash, out);
650 tfprintf(out, T("Flags = "));
651 u8 flags = lte->resource_entry.flags;
652 if (flags & WIM_RESHDR_FLAG_COMPRESSED)
653 tfputs(T("WIM_RESHDR_FLAG_COMPRESSED, "), out);
654 if (flags & WIM_RESHDR_FLAG_FREE)
655 tfputs(T("WIM_RESHDR_FLAG_FREE, "), out);
656 if (flags & WIM_RESHDR_FLAG_METADATA)
657 tfputs(T("WIM_RESHDR_FLAG_METADATA, "), out);
658 if (flags & WIM_RESHDR_FLAG_SPANNED)
659 tfputs(T("WIM_RESHDR_FLAG_SPANNED, "), out);
661 switch (lte->resource_location) {
662 case RESOURCE_IN_WIM:
663 if (lte->wim->filename) {
664 tfprintf(out, T("WIM file = `%"TS"'\n"),
671 case RESOURCE_IN_FILE_ON_DISK:
672 tfprintf(out, T("File on Disk = `%"TS"'\n"),
675 case RESOURCE_IN_STAGING_FILE:
676 tfprintf(out, T("Staging File = `%"TS"'\n"),
677 lte->staging_file_name);
686 do_print_lookup_table_entry(struct wim_lookup_table_entry *lte, void *fp)
688 print_lookup_table_entry(lte, (FILE*)fp);
693 * Prints the lookup table of a WIM file.
696 wimlib_print_lookup_table(WIMStruct *w)
698 for_lookup_table_entry(w->lookup_table,
699 do_print_lookup_table_entry,
703 /* Given a SHA1 message digest, return the corresponding entry in the WIM's
704 * lookup table, or NULL if there is none. */
705 struct wim_lookup_table_entry *
706 __lookup_resource(const struct wim_lookup_table *table, const u8 hash[])
709 struct wim_lookup_table_entry *lte;
710 struct hlist_node *pos;
712 wimlib_assert(table != NULL);
713 wimlib_assert(hash != NULL);
715 i = *(size_t*)hash % table->capacity;
716 hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
717 if (hashes_equal(hash, lte->hash))
724 * Finds the dentry, lookup table entry, and stream index for a WIM file stream,
727 * This is only for pre-resolved inodes.
730 lookup_resource(WIMStruct *w,
733 struct wim_dentry **dentry_ret,
734 struct wim_lookup_table_entry **lte_ret,
737 struct wim_dentry *dentry;
738 struct wim_lookup_table_entry *lte;
740 const tchar *stream_name = NULL;
741 struct wim_inode *inode;
744 if (lookup_flags & LOOKUP_FLAG_ADS_OK) {
745 stream_name = path_stream_name(path);
747 p = (tchar*)stream_name - 1;
752 dentry = get_dentry(w, path);
758 inode = dentry->d_inode;
760 wimlib_assert(inode->i_resolved);
762 if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
763 && inode_is_directory(inode))
767 struct wim_ads_entry *ads_entry;
769 ads_entry = inode_get_ads_entry(inode, stream_name,
772 stream_idx = ads_idx + 1;
773 lte = ads_entry->lte;
784 *dentry_ret = dentry;
788 *stream_idx_ret = stream_idx;
793 /* Resolve an inode's lookup table entries
795 * This replaces the SHA1 hash fields (which are used to lookup an entry in the
796 * lookup table) with pointers directly to the lookup table entries. A circular
797 * linked list of streams sharing the same lookup table entry is created.
799 * This function always succeeds; unresolved lookup table entries are given a
803 inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table)
806 if (!inode->i_resolved) {
807 struct wim_lookup_table_entry *lte;
808 /* Resolve the default file stream */
809 lte = __lookup_resource(table, inode->i_hash);
811 inode->i_resolved = 1;
813 /* Resolve the alternate data streams */
814 for (u16 i = 0; i < inode->i_num_ads; i++) {
815 struct wim_ads_entry *cur_entry = &inode->i_ads_entries[i];
816 lte = __lookup_resource(table, cur_entry->hash);
817 cur_entry->lte = lte;
823 inode_unresolve_ltes(struct wim_inode *inode)
825 if (inode->i_resolved) {
827 copy_hash(inode->i_hash, inode->i_lte->hash);
829 zero_out_hash(inode->i_hash);
831 for (u16 i = 0; i < inode->i_num_ads; i++) {
832 if (inode->i_ads_entries[i].lte)
833 copy_hash(inode->i_ads_entries[i].hash,
834 inode->i_ads_entries[i].lte->hash);
836 zero_out_hash(inode->i_ads_entries[i].hash);
838 inode->i_resolved = 0;
843 * Returns the lookup table entry for stream @stream_idx of the inode, where
844 * stream_idx = 0 means the default un-named file stream, and stream_idx >= 1
845 * corresponds to an alternate data stream.
847 * This works for both resolved and un-resolved inodes.
849 struct wim_lookup_table_entry *
850 inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
851 const struct wim_lookup_table *table)
853 if (inode->i_resolved)
854 return inode_stream_lte_resolved(inode, stream_idx);
856 return inode_stream_lte_unresolved(inode, stream_idx, table);
860 /* Return the lookup table entry for the unnamed data stream of an inode, or
861 * NULL if there is none.
863 * You'd think this would be easier than it actually is, since the unnamed data
864 * stream should be the one referenced from the inode itself. Alas, if there
865 * are named data streams, Microsoft's "imagex.exe" program will put the unnamed
866 * data stream in one of the alternate data streams instead of inside the WIM
867 * dentry itself. So we need to check the alternate data streams too.
869 * Also, note that a dentry may appear to have more than one unnamed stream, but
870 * if the SHA1 message digest is all 0's then the corresponding stream does not
871 * really "count" (this is the case for the inode's own file stream when the
872 * file stream that should be there is actually in one of the alternate stream
873 * entries.). This is despite the fact that we may need to extract such a
874 * missing entry as an empty file or empty named data stream.
876 struct wim_lookup_table_entry *
877 inode_unnamed_lte(const struct wim_inode *inode,
878 const struct wim_lookup_table *table)
880 if (inode->i_resolved)
881 return inode_unnamed_lte_resolved(inode);
883 return inode_unnamed_lte_unresolved(inode, table);
887 lte_add_stream_size(struct wim_lookup_table_entry *lte, void *total_bytes_p)
889 *(u64*)total_bytes_p += lte->resource_entry.size;
894 lookup_table_total_stream_size(struct wim_lookup_table *table)
897 for_lookup_table_entry(table, lte_add_stream_size, &total_size);
901 struct wim_lookup_table_entry **
902 retrieve_lte_pointer(struct wim_lookup_table_entry *lte)
904 wimlib_assert(lte->unhashed);
905 struct wim_inode *inode = lte->back_inode;
906 u32 stream_id = lte->back_stream_id;
908 return &inode->i_lte;
910 for (u16 i = 0; i < inode->i_num_ads; i++)
911 if (inode->i_ads_entries[i].stream_id == stream_id)
912 return &inode->i_ads_entries[i].lte;
917 /* Calculate the SHA1 message digest of a stream and move it from the list of
918 * unhashed streams to the stream lookup table, possibly joining it with an
919 * existing lookup table entry for an identical stream.
921 * @lte: An unhashed lookup table entry.
922 * @lookup_table: Lookup table for the WIM.
923 * @lte_ret: On success, write a pointer to the resulting lookup table
924 * entry to this location. This will be the same as @lte
925 * if it was inserted into the lookup table, or different if
926 * a duplicate stream was found.
928 * Returns 0 on success; nonzero if there is an error reading the stream.
931 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
932 struct wim_lookup_table *lookup_table,
933 struct wim_lookup_table_entry **lte_ret)
936 struct wim_lookup_table_entry *duplicate_lte;
937 struct wim_lookup_table_entry **back_ptr;
939 wimlib_assert(lte->unhashed);
941 /* back_ptr must be saved because @back_inode and @back_stream_id are in
942 * union with the SHA1 message digest and will no longer be valid once
943 * the SHA1 has been calculated. */
944 back_ptr = retrieve_lte_pointer(lte);
946 ret = sha1_resource(lte);
950 /* Look for a duplicate stream */
951 duplicate_lte = __lookup_resource(lookup_table, lte->hash);
952 list_del(<e->unhashed_list);
954 /* We have a duplicate stream. Transfer the reference counts
955 * from this stream to the duplicate, update the reference to
956 * this stream (in an inode or ads_entry) to point to the
957 * duplicate, then free this stream. */
958 wimlib_assert(!(duplicate_lte->unhashed));
959 duplicate_lte->refcnt += lte->refcnt;
960 duplicate_lte->out_refcnt += lte->refcnt;
961 *back_ptr = duplicate_lte;
962 free_lookup_table_entry(lte);
965 /* No duplicate stream, so we need to insert
966 * this stream into the lookup table and treat
967 * it as a hashed stream. */
968 lookup_table_insert(lookup_table, lte);