]> wimlib.net Git - wimlib/blobdiff - src/lookup_table.c
Cache compression format in 'struct wim_resource_spec'
[wimlib] / src / lookup_table.c
index 998eb185f1586e8af2791a58d9bb11fd82247cc9..815d576fe551177200b8b781c3a69621d7210530 100644 (file)
@@ -2,7 +2,7 @@
  * lookup_table.c
  *
  * Lookup table, implemented as a hash table, that maps SHA1 message digests to
- * data streams.
+ * data streams; plus code to read and write the corresponding on-disk data.
  */
 
 /*
  * along with wimlib; if not, see http://www.gnu.org/licenses/.
  */
 
-#include "wimlib_internal.h"
-#include "lookup_table.h"
-#include "buffer_io.h"
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
+#include "wimlib/endianness.h"
+#include "wimlib/error.h"
+#include "wimlib/file_io.h"
+#include "wimlib/glob.h"
+#include "wimlib/lookup_table.h"
+#include "wimlib/metadata.h"
+#include "wimlib/paths.h"
+#include "wimlib/resource.h"
+#include "wimlib/util.h"
+#include "wimlib/write.h"
+
 #include <errno.h>
 #include <stdlib.h>
-
 #ifdef WITH_FUSE
-#include <unistd.h>
+#  include <unistd.h> /* for unlink() */
 #endif
 
 struct wim_lookup_table *
@@ -58,19 +69,19 @@ new_lookup_table(size_t capacity)
 }
 
 struct wim_lookup_table_entry *
-new_lookup_table_entry()
+new_lookup_table_entry(void)
 {
        struct wim_lookup_table_entry *lte;
 
        lte = CALLOC(1, sizeof(struct wim_lookup_table_entry));
-       if (lte) {
-               lte->part_number  = 1;
-               lte->refcnt       = 1;
-       } else {
+       if (lte == NULL) {
                ERROR("Out of memory (tried to allocate %zu bytes for "
                      "lookup table entry)",
                      sizeof(struct wim_lookup_table_entry));
+               return NULL;
        }
+       lte->refcnt = 1;
+       BUILD_BUG_ON(RESOURCE_NONEXISTENT != 0);
        return lte;
 }
 
@@ -79,18 +90,19 @@ clone_lookup_table_entry(const struct wim_lookup_table_entry *old)
 {
        struct wim_lookup_table_entry *new;
 
-       new = MALLOC(sizeof(*new));
-       if (!new)
+       new = memdup(old, sizeof(struct wim_lookup_table_entry));
+       if (new == NULL)
                return NULL;
 
-       memcpy(new, old, sizeof(*old));
        new->extracted_file = NULL;
        switch (new->resource_location) {
+       case RESOURCE_IN_WIM:
+               list_add(&new->rspec_node, &new->rspec->stream_list);
+               break;
+
+       case RESOURCE_IN_FILE_ON_DISK:
 #ifdef __WIN32__
-       case RESOURCE_WIN32:
        case RESOURCE_WIN32_ENCRYPTED:
-#else
-       case RESOURCE_IN_FILE_ON_DISK:
 #endif
 #ifdef WITH_FUSE
        case RESOURCE_IN_STAGING_FILE:
@@ -98,36 +110,33 @@ clone_lookup_table_entry(const struct wim_lookup_table_entry *old)
                             (void*)&old->staging_file_name);
 #endif
                new->file_on_disk = TSTRDUP(old->file_on_disk);
-               if (!new->file_on_disk)
+               if (new->file_on_disk == NULL)
                        goto out_free;
                break;
        case RESOURCE_IN_ATTACHED_BUFFER:
-               new->attached_buffer = MALLOC(wim_resource_size(old));
-               if (!new->attached_buffer)
+               new->attached_buffer = memdup(old->attached_buffer, old->size);
+               if (new->attached_buffer == NULL)
                        goto out_free;
-               memcpy(new->attached_buffer, old->attached_buffer,
-                      wim_resource_size(old));
                break;
 #ifdef WITH_NTFS_3G
        case RESOURCE_IN_NTFS_VOLUME:
                if (old->ntfs_loc) {
                        struct ntfs_location *loc;
-                       loc = MALLOC(sizeof(*loc));
-                       if (!loc)
+                       loc = memdup(old->ntfs_loc, sizeof(struct ntfs_location));
+                       if (loc == NULL)
                                goto out_free;
-                       memcpy(loc, old->ntfs_loc, sizeof(*loc));
                        loc->path = NULL;
                        loc->stream_name = NULL;
                        new->ntfs_loc = loc;
                        loc->path = STRDUP(old->ntfs_loc->path);
-                       if (!loc->path)
-                               goto out_free;
-                       loc->stream_name = MALLOC((loc->stream_name_nchars + 1) * 2);
-                       if (!loc->stream_name)
+                       if (loc->path == NULL)
                                goto out_free;
-                       memcpy(loc->stream_name,
-                              old->ntfs_loc->stream_name,
-                              (loc->stream_name_nchars + 1) * 2);
+                       if (loc->stream_name_nchars != 0) {
+                               loc->stream_name = memdup(old->ntfs_loc->stream_name,
+                                                         loc->stream_name_nchars * 2);
+                               if (loc->stream_name == NULL)
+                                       goto out_free;
+                       }
                }
                break;
 #endif
@@ -145,11 +154,14 @@ free_lookup_table_entry(struct wim_lookup_table_entry *lte)
 {
        if (lte) {
                switch (lte->resource_location) {
+               case RESOURCE_IN_WIM:
+                       list_del(&lte->rspec_node);
+                       if (list_empty(&lte->rspec->stream_list))
+                               FREE(lte->rspec);
+                       break;
+               case RESOURCE_IN_FILE_ON_DISK:
        #ifdef __WIN32__
-               case RESOURCE_WIN32:
                case RESOURCE_WIN32_ENCRYPTED:
-       #else
-               case RESOURCE_IN_FILE_ON_DISK:
        #endif
        #ifdef WITH_FUSE
                case RESOURCE_IN_STAGING_FILE:
@@ -188,7 +200,7 @@ do_free_lookup_table_entry(struct wim_lookup_table_entry *entry, void *ignore)
 void
 free_lookup_table(struct wim_lookup_table *table)
 {
-       DEBUG2("Freeing lookup table");
+       DEBUG("Freeing lookup table.");
        if (table) {
                if (table->array) {
                        for_lookup_table_entry(table,
@@ -200,6 +212,43 @@ free_lookup_table(struct wim_lookup_table *table)
        }
 }
 
+static void
+lookup_table_insert_raw(struct wim_lookup_table *table,
+                       struct wim_lookup_table_entry *lte)
+{
+       size_t i = lte->hash_short % table->capacity;
+
+       hlist_add_head(&lte->hash_list, &table->array[i]);
+}
+
+static void
+enlarge_lookup_table(struct wim_lookup_table *table)
+{
+       size_t old_capacity, new_capacity;
+       struct hlist_head *old_array, *new_array;
+       struct wim_lookup_table_entry *lte;
+       struct hlist_node *cur, *tmp;
+       size_t i;
+
+       old_capacity = table->capacity;
+       new_capacity = old_capacity * 2;
+       new_array = CALLOC(new_capacity, sizeof(struct hlist_head));
+       if (new_array == NULL)
+               return;
+       old_array = table->array;
+       table->array = new_array;
+       table->capacity = new_capacity;
+
+       for (i = 0; i < old_capacity; i++) {
+               hlist_for_each_entry_safe(lte, cur, tmp, &old_array[i], hash_list) {
+                       hlist_del(&lte->hash_list);
+                       lookup_table_insert_raw(table, lte);
+               }
+       }
+       FREE(old_array);
+}
+
+
 /*
  * Inserts an entry into the lookup table.
  *
@@ -210,11 +259,9 @@ void
 lookup_table_insert(struct wim_lookup_table *table,
                    struct wim_lookup_table_entry *lte)
 {
-       size_t i = lte->hash_short % table->capacity;
-       hlist_add_head(&lte->hash_list, &table->array[i]);
-
-       /* XXX Make the table grow when too many entries have been inserted. */
-       table->num_entries++;
+       lookup_table_insert_raw(table, lte);
+       if (++table->num_entries > table->capacity)
+               enlarge_lookup_table(table);
 }
 
 static void
@@ -276,7 +323,6 @@ for_lookup_table_entry(struct wim_lookup_table *table,
                hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
                                          hash_list)
                {
-                       wimlib_assert2(!(lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA));
                        ret = visitor(lte, arg);
                        if (ret)
                                return ret;
@@ -285,18 +331,122 @@ for_lookup_table_entry(struct wim_lookup_table *table,
        return 0;
 }
 
-int
-cmp_streams_by_wim_position(const void *p1, const void *p2)
+/* qsort() callback that sorts streams (represented by `struct
+ * wim_lookup_table_entry's) into an order optimized for reading.
+ *
+ * Sorting is done primarily by resource location, then secondarily by a
+ * per-resource location order.  For example, resources in WIM files are sorted
+ * primarily by part number, then secondarily by offset, as to implement optimal
+ * reading of either a standalone or split WIM.  */
+static int
+cmp_streams_by_sequential_order(const void *p1, const void *p2)
 {
        const struct wim_lookup_table_entry *lte1, *lte2;
+       int v;
+       WIMStruct *wim1, *wim2;
+
        lte1 = *(const struct wim_lookup_table_entry**)p1;
        lte2 = *(const struct wim_lookup_table_entry**)p2;
-       if (lte1->resource_entry.offset < lte2->resource_entry.offset)
-               return -1;
-       else if (lte1->resource_entry.offset > lte2->resource_entry.offset)
-               return 1;
-       else
+
+       v = (int)lte1->resource_location - (int)lte2->resource_location;
+
+       /* Different resource locations?  */
+       if (v)
+               return v;
+
+       switch (lte1->resource_location) {
+       case RESOURCE_IN_WIM:
+               wim1 = lte1->rspec->wim;
+               wim2 = lte2->rspec->wim;
+
+               /* Different (possibly split) WIMs?  */
+               if (wim1 != wim2) {
+                       v = memcmp(wim1->hdr.guid, wim2->hdr.guid, WIM_GID_LEN);
+                       if (v)
+                               return v;
+               }
+
+               /* Different part numbers in the same WIM?  */
+               v = (int)wim1->hdr.part_number - (int)wim2->hdr.part_number;
+               if (v)
+                       return v;
+
+               if (lte1->rspec->offset_in_wim != lte2->rspec->offset_in_wim)
+                       return cmp_u64(lte1->rspec->offset_in_wim,
+                                      lte2->rspec->offset_in_wim);
+
+               return cmp_u64(lte1->offset_in_res, lte2->offset_in_res);
+
+       case RESOURCE_IN_FILE_ON_DISK:
+#ifdef WITH_FUSE
+       case RESOURCE_IN_STAGING_FILE:
+#endif
+#ifdef __WIN32__
+       case RESOURCE_WIN32_ENCRYPTED:
+#endif
+               /* Compare files by path: just a heuristic that will place files
+                * in the same directory next to each other.  */
+               return tstrcmp(lte1->file_on_disk, lte2->file_on_disk);
+#ifdef WITH_NTFS_3G
+       case RESOURCE_IN_NTFS_VOLUME:
+               return tstrcmp(lte1->ntfs_loc->path, lte2->ntfs_loc->path);
+#endif
+       default:
+               /* No additional sorting order defined for this resource
+                * location (e.g. RESOURCE_IN_ATTACHED_BUFFER); simply compare
+                * everything equal to each other.  */
                return 0;
+       }
+}
+
+int
+sort_stream_list(struct list_head *stream_list,
+                size_t list_head_offset,
+                int (*compar)(const void *, const void*))
+{
+       struct list_head *cur;
+       struct wim_lookup_table_entry **array;
+       size_t i;
+       size_t array_size;
+       size_t num_streams = 0;
+
+       list_for_each(cur, stream_list)
+               num_streams++;
+
+       if (num_streams <= 1)
+               return 0;
+
+       array_size = num_streams * sizeof(array[0]);
+       array = MALLOC(array_size);
+       if (array == NULL)
+               return WIMLIB_ERR_NOMEM;
+
+       cur = stream_list->next;
+       for (i = 0; i < num_streams; i++) {
+               array[i] = (struct wim_lookup_table_entry*)((u8*)cur -
+                                                           list_head_offset);
+               cur = cur->next;
+       }
+
+       qsort(array, num_streams, sizeof(array[0]), compar);
+
+       INIT_LIST_HEAD(stream_list);
+       for (i = 0; i < num_streams; i++) {
+               list_add_tail((struct list_head*)
+                              ((u8*)array[i] + list_head_offset),
+                             stream_list);
+       }
+       FREE(array);
+       return 0;
+}
+
+/* Sort the specified list of streams in an order optimized for reading.  */
+int
+sort_stream_list_by_sequential_order(struct list_head *stream_list,
+                                    size_t list_head_offset)
+{
+       return sort_stream_list(stream_list, list_head_offset,
+                               cmp_streams_by_sequential_order);
 }
 
 
@@ -331,7 +481,7 @@ for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
        wimlib_assert(p == lte_array + num_streams);
 
        qsort(lte_array, num_streams, sizeof(lte_array[0]),
-             cmp_streams_by_wim_position);
+             cmp_streams_by_sequential_order);
        ret = 0;
        for (size_t i = 0; i < num_streams; i++) {
                ret = visitor(lte_array[i], arg);
@@ -342,131 +492,308 @@ for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
        return ret;
 }
 
+/* On-disk format of a WIM lookup table entry (stream entry). */
+struct wim_lookup_table_entry_disk {
+       /* Size, offset, and flags of the stream.  */
+       struct wim_reshdr_disk reshdr;
+
+       /* Which part of the split WIM this stream is in; indexed from 1. */
+       le16 part_number;
+
+       /* Reference count of this stream over all WIM images. */
+       le32 refcnt;
+
+       /* SHA1 message digest of the uncompressed data of this stream, or
+        * optionally all zeroes if this stream is of zero length. */
+       u8 hash[SHA1_HASH_SIZE];
+} _packed_attribute;
+
+#define WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE 50
+
+/* Validate the size and location of a WIM resource.  */
+static int
+validate_resource(const struct wim_resource_spec *rspec)
+{
+       struct wim_lookup_table_entry *lte;
+       u64 cur_offset;
+
+       /* Verify that calculating the offset of the end of the resource doesn't
+        * overflow.  */
+       if (rspec->offset_in_wim + rspec->size_in_wim < rspec->size_in_wim)
+               goto invalid;
+
+       /* Verify that each stream in the resource has a valid offset and size,
+        * and that no streams overlap, and that the streams were added in order
+        * of increasing offset.  */
+       cur_offset = 0;
+       list_for_each_entry(lte, &rspec->stream_list, rspec_node) {
+               if (lte->offset_in_res + lte->size < lte->size ||
+                   lte->offset_in_res + lte->size > rspec->uncompressed_size ||
+                   lte->offset_in_res < cur_offset)
+                       goto invalid;
+
+               cur_offset = lte->offset_in_res + lte->size;
+       }
+       return 0;
+
+invalid:
+
+       ERROR("Invalid resource entry!");
+       return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
+}
+
 /*
- * Reads the lookup table from a WIM file.
+ * Reads the lookup table from a WIM file.  Each entry specifies a stream that
+ * the WIM file contains, along with its location and SHA1 message digest.
  *
  * Saves lookup table entries for non-metadata streams in a hash table, and
  * saves the metadata entry for each image in a special per-image location (the
  * image_metadata array).
+ *
+ * Return values:
+ *     WIMLIB_ERR_SUCCESS (0)
+ *     WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY
+ *     WIMLIB_ERR_RESOURCE_NOT_FOUND
+ *
+ *     Or an error code caused by failure to read the lookup table into memory.
  */
 int
-read_lookup_table(WIMStruct *w)
+read_wim_lookup_table(WIMStruct *wim)
 {
        int ret;
+       size_t i;
        size_t num_entries;
        struct wim_lookup_table *table;
        struct wim_lookup_table_entry *cur_entry, *duplicate_entry;
-       u8 table_buf[(BUFFER_SIZE / WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE) *
-                       WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
-       const u8 *p;
-       off_t offset;
-       size_t buf_entries_remaining;
-
-       DEBUG("Reading lookup table: offset %"PRIu64", size %"PRIu64"",
-             w->hdr.lookup_table_res_entry.offset,
-             w->hdr.lookup_table_res_entry.original_size);
-
-       if (resource_is_compressed(&w->hdr.lookup_table_res_entry)) {
-               ERROR("Didn't expect a compressed lookup table!");
-               ERROR("Ask the author to implement support for this.");
-               return WIMLIB_ERR_COMPRESSED_LOOKUP_TABLE;
-       }
+       struct wim_resource_spec *cur_rspec;
+       void *buf;
+       bool back_to_back_pack;
+
+       DEBUG("Reading lookup table.");
 
-       num_entries = w->hdr.lookup_table_res_entry.size /
-                     WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
+       /* Sanity check: lookup table entries are 50 bytes each.  */
+       BUILD_BUG_ON(sizeof(struct wim_lookup_table_entry_disk) !=
+                    WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE);
+
+       /* Calculate number of entries in the lookup table.  */
+       num_entries = wim->hdr.lookup_table_reshdr.uncompressed_size /
+                     sizeof(struct wim_lookup_table_entry_disk);
+
+       /* Read the lookup table into a buffer.  */
+       ret = wim_reshdr_to_data(&wim->hdr.lookup_table_reshdr, wim, &buf);
+       if (ret)
+               goto out;
+
+       /* Allocate a hash table to map SHA1 message digests into stream
+        * specifications.  This is the in-memory "lookup table".  */
        table = new_lookup_table(num_entries * 2 + 1);
-       if (!table)
-               return WIMLIB_ERR_NOMEM;
+       if (table == NULL) {
+               ERROR("Not enough memory to read lookup table.");
+               ret = WIMLIB_ERR_NOMEM;
+               goto out_free_buf;
+       }
+
+       /* Allocate and initalize stream entries from the raw lookup table
+        * buffer.  */
+       wim->current_image = 0;
+       cur_rspec = NULL;
+       for (i = 0; i < num_entries; i++) {
+               const struct wim_lookup_table_entry_disk *disk_entry =
+                       &((const struct wim_lookup_table_entry_disk*)buf)[i];
+               u16 part_number;
+               struct wim_reshdr reshdr;
+
+               get_wim_reshdr(&disk_entry->reshdr, &reshdr);
+
+               DEBUG("reshdr: size_in_wim=%"PRIu64", "
+                     "uncompressed_size=%"PRIu64", "
+                     "offset_in_wim=%"PRIu64", "
+                     "flags=0x%02x",
+                     reshdr.size_in_wim, reshdr.uncompressed_size,
+                     reshdr.offset_in_wim, reshdr.flags);
+
+               if (wim->hdr.wim_version == WIM_VERSION_DEFAULT)
+                       reshdr.flags &= ~WIM_RESHDR_FLAG_PACKED_STREAMS;
 
-       w->current_image = 0;
-       offset = w->hdr.lookup_table_res_entry.offset;
-       buf_entries_remaining = 0;
-       for (; num_entries != 0; num_entries--, buf_entries_remaining--) {
-               if (buf_entries_remaining == 0) {
-                       size_t entries_to_read, bytes_to_read;
-
-                       entries_to_read = min(sizeof(table_buf) /
-                                               WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE,
-                                             num_entries);
-                       bytes_to_read = entries_to_read *
-                                               WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE;
-                       if (full_pread(w->in_fd, table_buf,
-                                      bytes_to_read, offset) != bytes_to_read)
-                       {
-                               ERROR_WITH_ERRNO("Error reading lookup table "
-                                                "(offset=%"PRIu64")", offset);
-                               ret = WIMLIB_ERR_READ;
-                               goto out_free_lookup_table;
-                       }
-                       offset += bytes_to_read;
-                       p = table_buf;
-                       buf_entries_remaining = entries_to_read;
-               }
                cur_entry = new_lookup_table_entry();
-               if (!cur_entry) {
+               if (cur_entry == NULL) {
+                       ERROR("Not enough memory to read lookup table!");
                        ret = WIMLIB_ERR_NOMEM;
-                       goto out_free_lookup_table;
+                       goto err;
                }
 
-               cur_entry->wim = w;
-               cur_entry->resource_location = RESOURCE_IN_WIM;
-               p = get_resource_entry(p, &cur_entry->resource_entry);
-               p = get_u16(p, &cur_entry->part_number);
-               p = get_u32(p, &cur_entry->refcnt);
-               p = get_bytes(p, SHA1_HASH_SIZE, cur_entry->hash);
-
-               if (cur_entry->part_number != w->hdr.part_number) {
-                       ERROR("A lookup table entry in part %hu of the WIM "
-                             "points to part %hu",
-                             w->hdr.part_number, cur_entry->part_number);
-                       ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
-                       goto out_free_cur_entry;
+               part_number = le16_to_cpu(disk_entry->part_number);
+               cur_entry->refcnt = le32_to_cpu(disk_entry->refcnt);
+               copy_hash(cur_entry->hash, disk_entry->hash);
+
+               if (part_number != wim->hdr.part_number) {
+                       WARNING("A lookup table entry in part %hu of the WIM "
+                               "points to part %hu (ignoring it)",
+                               wim->hdr.part_number, part_number);
+                       free_lookup_table_entry(cur_entry);
+                       continue;
                }
 
-               if (is_zero_hash(cur_entry->hash)) {
-                       ERROR("The WIM lookup table contains an entry with a "
-                             "SHA1 message digest of all 0's");
-                       ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
-                       goto out_free_cur_entry;
+               if (!(reshdr.flags & (WIM_RESHDR_FLAG_PACKED_STREAMS |
+                                     WIM_RESHDR_FLAG_COMPRESSED))) {
+                       if (reshdr.uncompressed_size != reshdr.size_in_wim) {
+                               ERROR("Invalid resource entry!");
+                               ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
+                               goto err;
+                       }
                }
 
-               if (!(cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_COMPRESSED)
-                   && (cur_entry->resource_entry.size !=
-                       cur_entry->resource_entry.original_size))
+               back_to_back_pack = false;
+               if (!(reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) ||
+                   cur_rspec == NULL ||
+                   (back_to_back_pack =
+                    ((reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) &&
+                     reshdr.uncompressed_size == WIM_PACK_MAGIC_NUMBER &&
+                     cur_rspec != NULL &&
+                     cur_rspec->size_in_wim != 0)))
                {
-               #ifdef ENABLE_ERROR_MESSAGES
-                       ERROR("Found uncompressed resource with original size "
-                             "not the same as compressed size");
-                       ERROR("The lookup table entry for the resource is as follows:");
-                       print_lookup_table_entry(cur_entry, stderr);
-               #endif
-                       ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
-                       goto out_free_cur_entry;
+                       /* Starting new run of streams that share the same WIM
+                        * resource.  */
+                       struct wim_lookup_table_entry *prev_entry = NULL;
+
+                       if (back_to_back_pack &&
+                           !list_empty(&cur_rspec->stream_list))
+                       {
+                               prev_entry = list_entry(cur_rspec->stream_list.prev,
+                                                       struct wim_lookup_table_entry,
+                                                       rspec_node);
+                               lte_unbind_wim_resource_spec(prev_entry);
+                       }
+                       if (cur_rspec != NULL) {
+                               ret = validate_resource(cur_rspec);
+                               if (ret)
+                                       goto err;
+                       }
+
+                       /* Allocate the resource specification and initialize it
+                        * with values from the current stream entry.  */
+                       cur_rspec = MALLOC(sizeof(*cur_rspec));
+                       if (cur_rspec == NULL) {
+                               ERROR("Not enough memory to read lookup table!");
+                               ret = WIMLIB_ERR_NOMEM;
+                               goto err;
+                       }
+                       wim_res_hdr_to_spec(&reshdr, wim, cur_rspec);
+
+                       if (prev_entry)
+                               lte_bind_wim_resource_spec(prev_entry, cur_rspec);
+               }
+
+               if ((reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) &&
+                   reshdr.uncompressed_size == WIM_PACK_MAGIC_NUMBER)
+               {
+                       /* Found the specification for the packed resource.
+                        * Transfer the values to the `struct
+                        * wim_resource_spec', and discard the current stream
+                        * since this lookup table entry did not, in fact,
+                        * correspond to a "stream".
+                        */
+
+                       /* Uncompressed size of the resource pack is actually
+                        * stored in the header of the resource itself.  Read
+                        * it, and also grab the chunk size and compression type
+                        * (which are not necessarily the defaults from the WIM
+                        * header).  */
+                       struct alt_chunk_table_header_disk hdr;
+
+                       ret = full_pread(&wim->in_fd, &hdr,
+                                        sizeof(hdr), reshdr.offset_in_wim);
+                       if (ret)
+                               goto err;
+
+                       cur_rspec->uncompressed_size = le64_to_cpu(hdr.res_usize);
+                       cur_rspec->offset_in_wim = reshdr.offset_in_wim;
+                       cur_rspec->size_in_wim = reshdr.size_in_wim;
+                       cur_rspec->flags = reshdr.flags;
+
+                       /* Compression format numbers must be the same as in
+                        * WIMGAPI to be compatible here.  */
+                       BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_NONE != 0);
+                       BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_LZX != 1);
+                       BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_XPRESS != 2);
+                       BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_LZMS != 3);
+                       cur_rspec->compression_type = le32_to_cpu(hdr.compression_format);
+
+                       cur_rspec->chunk_size = le32_to_cpu(hdr.chunk_size);
+
+                       DEBUG("Full pack is %"PRIu64" compressed bytes "
+                             "at file offset %"PRIu64" (flags 0x%02x)",
+                             cur_rspec->size_in_wim,
+                             cur_rspec->offset_in_wim,
+                             cur_rspec->flags);
+                       free_lookup_table_entry(cur_entry);
+                       continue;
+               }
+
+               if (is_zero_hash(cur_entry->hash)) {
+                       free_lookup_table_entry(cur_entry);
+                       continue;
+               }
+
+               if (reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
+                       /* Continuing the pack with another stream.  */
+                       DEBUG("Continuing pack with stream: "
+                             "%"PRIu64" uncompressed bytes @ "
+                             "resource offset %"PRIu64")",
+                             reshdr.size_in_wim, reshdr.offset_in_wim);
                }
 
-               if (cur_entry->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) {
+               lte_bind_wim_resource_spec(cur_entry, cur_rspec);
+               if (reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
+                       /* In packed runs, the offset field is used for
+                        * in-resource offset, not the in-WIM offset, and the
+                        * size field is used for the uncompressed size, not the
+                        * compressed size.  */
+                       cur_entry->offset_in_res = reshdr.offset_in_wim;
+                       cur_entry->size = reshdr.size_in_wim;
+                       cur_entry->flags = reshdr.flags;
+               } else {
+                       /* Normal case: The stream corresponds one-to-one with
+                        * the resource entry.  */
+                       cur_entry->offset_in_res = 0;
+                       cur_entry->size = reshdr.uncompressed_size;
+                       cur_entry->flags = reshdr.flags;
+                       cur_rspec = NULL;
+               }
+
+               if (cur_entry->flags & WIM_RESHDR_FLAG_METADATA) {
                        /* Lookup table entry for a metadata resource */
+
+                       /* Metadata entries with no references must be ignored;
+                        * see for example the WinPE WIMs from the WAIK v2.1.
+                        * */
+                       if (cur_entry->refcnt == 0) {
+                               free_lookup_table_entry(cur_entry);
+                               continue;
+                       }
+
                        if (cur_entry->refcnt != 1) {
-                       #ifdef ENABLE_ERROR_MESSAGES
-                               ERROR("Found metadata resource with refcnt != 1:");
-                               print_lookup_table_entry(cur_entry, stderr);
-                       #endif
+                               if (wimlib_print_errors) {
+                                       ERROR("Found metadata resource with refcnt != 1:");
+                                       print_lookup_table_entry(cur_entry, stderr);
+                               }
                                ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
-                               goto out_free_cur_entry;
+                               goto err;
                        }
 
-                       if (w->hdr.part_number != 1) {
-                               ERROR("Found a metadata resource in a "
-                                     "non-first part of the split WIM!");
-                               ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
-                               goto out_free_cur_entry;
+                       if (wim->hdr.part_number != 1) {
+                               WARNING("Ignoring metadata resource found in a "
+                                       "non-first part of the split WIM");
+                               free_lookup_table_entry(cur_entry);
+                               continue;
                        }
-                       if (w->current_image == w->hdr.image_count) {
-                               ERROR("The WIM header says there are %u images "
-                                     "in the WIM, but we found more metadata "
-                                     "resources than this", w->hdr.image_count);
-                               ret = WIMLIB_ERR_IMAGE_COUNT;
-                               goto out_free_cur_entry;
+                       if (wim->current_image == wim->hdr.image_count) {
+                               WARNING("The WIM header says there are %u images "
+                                       "in the WIM, but we found more metadata "
+                                       "resources than this (ignoring the extra)",
+                                       wim->hdr.image_count);
+                               free_lookup_table_entry(cur_entry);
+                               continue;
                        }
 
                        /* Notice very carefully:  We are assigning the metadata
@@ -478,151 +805,169 @@ read_lookup_table(WIMStruct *w)
                         * written in the XML data. */
                        DEBUG("Found metadata resource for image %u at "
                              "offset %"PRIu64".",
-                             w->current_image + 1,
-                             cur_entry->resource_entry.offset);
-                       w->image_metadata[
-                               w->current_image++]->metadata_lte = cur_entry;
-               } else {
-                       /* Lookup table entry for a stream that is not a
-                        * metadata resource */
-                       duplicate_entry = __lookup_resource(table, cur_entry->hash);
-                       if (duplicate_entry) {
-                       #ifdef ENABLE_ERROR_MESSAGES
-                               ERROR("The WIM lookup table contains two entries with the "
+                             wim->current_image + 1,
+                             cur_entry->rspec->offset_in_wim);
+                       wim->image_metadata[
+                               wim->current_image++]->metadata_lte = cur_entry;
+                       continue;
+               }
+
+               /* Lookup table entry for a stream that is not a metadata
+                * resource.  */
+               duplicate_entry = lookup_resource(table, cur_entry->hash);
+               if (duplicate_entry) {
+                       if (wimlib_print_errors) {
+                               WARNING("The WIM lookup table contains two entries with the "
                                      "same SHA1 message digest!");
-                               ERROR("The first entry is:");
+                               WARNING("The first entry is:");
                                print_lookup_table_entry(duplicate_entry, stderr);
-                               ERROR("The second entry is:");
+                               WARNING("The second entry is:");
                                print_lookup_table_entry(cur_entry, stderr);
-                       #endif
-                               ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
-                               goto out_free_cur_entry;
                        }
-                       lookup_table_insert(table, cur_entry);
+                       free_lookup_table_entry(cur_entry);
+                       continue;
                }
+
+               /* Finally, insert the stream into the lookup table, keyed by
+                * its SHA1 message digest.  */
+               lookup_table_insert(table, cur_entry);
        }
+       cur_entry = NULL;
 
-       if (w->hdr.part_number == 1 && w->current_image != w->hdr.image_count)
-       {
-               ERROR("The WIM header says there are %u images "
-                     "in the WIM, but we only found %d metadata "
-                     "resources!", w->hdr.image_count, w->current_image);
-               ret = WIMLIB_ERR_IMAGE_COUNT;
-               goto out_free_lookup_table;
+       /* Validate the last resource.  */
+       if (cur_rspec != NULL) {
+               ret = validate_resource(cur_rspec);
+               if (ret)
+                       goto err;
+       }
+
+       if (wim->hdr.part_number == 1 && wim->current_image != wim->hdr.image_count) {
+               WARNING("The header of \"%"TS"\" says there are %u images in\n"
+                       "          the WIM, but we only found %d metadata resources!  Acting as if\n"
+                       "          the header specified only %d images instead.",
+                       wim->filename, wim->hdr.image_count,
+                       wim->current_image, wim->current_image);
+               for (int i = wim->current_image; i < wim->hdr.image_count; i++)
+                       put_image_metadata(wim->image_metadata[i], NULL);
+               wim->hdr.image_count = wim->current_image;
        }
        DEBUG("Done reading lookup table.");
-       w->lookup_table = table;
+       wim->lookup_table = table;
        ret = 0;
-       goto out;
-out_free_cur_entry:
-       FREE(cur_entry);
-out_free_lookup_table:
+       goto out_free_buf;
+
+err:
+       if (cur_rspec && list_empty(&cur_rspec->stream_list))
+               FREE(cur_rspec);
+       free_lookup_table_entry(cur_entry);
        free_lookup_table(table);
+out_free_buf:
+       FREE(buf);
 out:
-       w->current_image = 0;
+       wim->current_image = 0;
        return ret;
 }
 
-
-static u8 *
-write_lookup_table_entry(const struct wim_lookup_table_entry *lte, u8 *buf_p)
+static void
+put_wim_lookup_table_entry(struct wim_lookup_table_entry_disk *disk_entry,
+                          const struct wim_reshdr *out_reshdr,
+                          u16 part_number, u32 refcnt, const u8 *hash)
 {
-       buf_p = put_resource_entry(buf_p, &lte->output_resource_entry);
-       buf_p = put_u16(buf_p, lte->part_number);
-       buf_p = put_u32(buf_p, lte->out_refcnt);
-       buf_p = put_bytes(buf_p, SHA1_HASH_SIZE, lte->hash);
-       return buf_p;
+       put_wim_reshdr(out_reshdr, &disk_entry->reshdr);
+       disk_entry->part_number = cpu_to_le16(part_number);
+       disk_entry->refcnt = cpu_to_le32(refcnt);
+       copy_hash(disk_entry->hash, hash);
 }
 
 int
-write_lookup_table_from_stream_list(struct list_head *stream_list,
-                                   int out_fd,
-                                   struct resource_entry *out_res_entry)
+write_wim_lookup_table_from_stream_list(struct list_head *stream_list,
+                                       struct filedes *out_fd,
+                                       u16 part_number,
+                                       struct wim_reshdr *out_reshdr,
+                                       int write_resource_flags)
 {
-       int ret;
-       off_t start_offset;
-       u8 table_buf[(BUFFER_SIZE / WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE) *
-                       WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE];
-       u8 *buf_p;
        size_t table_size;
-       size_t bytes_to_write;
        struct wim_lookup_table_entry *lte;
+       struct wim_lookup_table_entry_disk *table_buf;
+       struct wim_lookup_table_entry_disk *table_buf_ptr;
+       int ret;
+       u64 prev_res_offset_in_wim = ~0ULL;
 
-       start_offset = filedes_offset(out_fd);
-       if (start_offset == -1)
-               goto write_error;
-
-       buf_p = table_buf;
        table_size = 0;
        list_for_each_entry(lte, stream_list, lookup_table_list) {
-               if (buf_p == table_buf + sizeof(table_buf)) {
-                       bytes_to_write = sizeof(table_buf);
-                       if (full_write(out_fd, table_buf,
-                                      bytes_to_write) != bytes_to_write)
-                               goto write_error;
-                       table_size += bytes_to_write;
-                       buf_p = table_buf;
+               table_size += sizeof(struct wim_lookup_table_entry_disk);
+
+               if (lte->out_reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS &&
+                   lte->out_res_offset_in_wim != prev_res_offset_in_wim)
+               {
+                       table_size += sizeof(struct wim_lookup_table_entry_disk);
+                       prev_res_offset_in_wim = lte->out_res_offset_in_wim;
                }
-               buf_p = write_lookup_table_entry(lte, buf_p);
        }
-       bytes_to_write = buf_p - table_buf;
-       if (bytes_to_write != 0) {
-               if (full_write(out_fd, table_buf,
-                              bytes_to_write) != bytes_to_write)
-                       goto write_error;
-               table_size += bytes_to_write;
-       }
-       out_res_entry->offset        = start_offset;
-       out_res_entry->size          = table_size;
-       out_res_entry->original_size = table_size;
-       out_res_entry->flags         = WIM_RESHDR_FLAG_METADATA;
-       ret = 0;
-out:
-       return ret;
-write_error:
-       ERROR_WITH_ERRNO("Failed to write lookup table");
-       ret = WIMLIB_ERR_WRITE;
-       goto out;
-}
 
-static int
-append_lookup_table_entry(struct wim_lookup_table_entry *lte, void *_list)
-{
-       if (lte->out_refcnt != 0)
-               list_add_tail(&lte->lookup_table_list, (struct list_head*)_list);
-       return 0;
-}
-
-/* Writes the WIM lookup table to the output file. */
-int
-write_lookup_table(WIMStruct *w, int image, struct resource_entry *out_res_entry)
-{
-       LIST_HEAD(stream_list);
-       int start_image;
-       int end_image;
+       DEBUG("Writing WIM lookup table (size=%zu, offset=%"PRIu64")",
+             table_size, out_fd->offset);
 
-       if (image == WIMLIB_ALL_IMAGES) {
-               start_image = 1;
-               end_image = w->hdr.image_count;
-       } else {
-               start_image = image;
-               end_image = image;
+       table_buf = MALLOC(table_size);
+       if (table_buf == NULL) {
+               ERROR("Failed to allocate %zu bytes for temporary lookup table",
+                     table_size);
+               return WIMLIB_ERR_NOMEM;
        }
+       table_buf_ptr = table_buf;
 
-       for (int i = start_image; i <= end_image; i++) {
-               struct wim_lookup_table_entry *metadata_lte;
+       prev_res_offset_in_wim = ~0ULL;
+       list_for_each_entry(lte, stream_list, lookup_table_list) {
+
+               put_wim_lookup_table_entry(table_buf_ptr++,
+                                          &lte->out_reshdr,
+                                          part_number,
+                                          lte->out_refcnt,
+                                          lte->hash);
+               if (lte->out_reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS &&
+                   lte->out_res_offset_in_wim != prev_res_offset_in_wim)
+               {
+                       /* Put the main resource entry for the pack.  */
+
+                       struct wim_reshdr reshdr;
+
+                       reshdr.offset_in_wim = lte->out_res_offset_in_wim;
+                       reshdr.size_in_wim = lte->out_res_size_in_wim;
+                       reshdr.uncompressed_size = WIM_PACK_MAGIC_NUMBER;
+                       reshdr.flags = WIM_RESHDR_FLAG_PACKED_STREAMS;
+
+                       DEBUG("Putting main entry for pack: "
+                             "size_in_wim=%"PRIu64", "
+                             "offset_in_wim=%"PRIu64", "
+                             "uncompressed_size=%"PRIu64,
+                             reshdr.size_in_wim,
+                             reshdr.offset_in_wim,
+                             reshdr.uncompressed_size);
+
+                       put_wim_lookup_table_entry(table_buf_ptr++,
+                                                  &reshdr,
+                                                  part_number,
+                                                  1, zero_hash);
+                       prev_res_offset_in_wim = lte->out_res_offset_in_wim;
+               }
 
-               metadata_lte = w->image_metadata[i - 1]->metadata_lte;
-               metadata_lte->out_refcnt = 1;
-               metadata_lte->output_resource_entry.flags |= WIM_RESHDR_FLAG_METADATA;
-               append_lookup_table_entry(metadata_lte, &stream_list);
        }
-       for_lookup_table_entry(w->lookup_table,
-                              append_lookup_table_entry,
-                              &stream_list);
-       return write_lookup_table_from_stream_list(&stream_list,
-                                                  w->out_fd,
-                                                  out_res_entry);
+       wimlib_assert((u8*)table_buf_ptr - (u8*)table_buf == table_size);
+
+       /* Write the lookup table uncompressed.  Although wimlib can handle a
+        * compressed lookup table, MS software cannot.  */
+       ret = write_wim_resource_from_buffer(table_buf,
+                                            table_size,
+                                            WIM_RESHDR_FLAG_METADATA,
+                                            out_fd,
+                                            WIMLIB_COMPRESSION_TYPE_NONE,
+                                            0,
+                                            out_reshdr,
+                                            NULL,
+                                            write_resource_flags);
+       FREE(table_buf);
+       DEBUG("ret=%d", ret);
+       return ret;
 }
 
 int
@@ -652,33 +997,47 @@ lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *_ignore)
 void
 print_lookup_table_entry(const struct wim_lookup_table_entry *lte, FILE *out)
 {
-       if (!lte) {
+       if (lte == NULL) {
                tputc(T('\n'), out);
                return;
        }
-       tfprintf(out, T("Offset            = %"PRIu64" bytes\n"),
-                lte->resource_entry.offset);
 
-       tfprintf(out, T("Size              = %"PRIu64" bytes\n"),
-                (u64)lte->resource_entry.size);
 
-       tfprintf(out, T("Original size     = %"PRIu64" bytes\n"),
-                lte->resource_entry.original_size);
+       tprintf(T("Uncompressed size     = %"PRIu64" bytes\n"),
+               lte->size);
+       if (lte->flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
+               tprintf(T("Offset                = %"PRIu64" bytes\n"),
+                       lte->offset_in_res);
+
+               tprintf(T("Raw uncompressed size = %"PRIu64" bytes\n"),
+                       lte->rspec->uncompressed_size);
 
-       tfprintf(out, T("Part Number       = %hu\n"), lte->part_number);
-       tfprintf(out, T("Reference Count   = %u\n"), lte->refcnt);
+               tprintf(T("Raw compressed size   = %"PRIu64" bytes\n"),
+                       lte->rspec->size_in_wim);
+
+               tprintf(T("Raw offset            = %"PRIu64" bytes\n"),
+                       lte->rspec->offset_in_wim);
+       } else if (lte->resource_location == RESOURCE_IN_WIM) {
+               tprintf(T("Compressed size       = %"PRIu64" bytes\n"),
+                       lte->rspec->size_in_wim);
+
+               tprintf(T("Offset                = %"PRIu64" bytes\n"),
+                       lte->rspec->offset_in_wim);
+       }
+
+       tfprintf(out, T("Reference Count       = %u\n"), lte->refcnt);
 
        if (lte->unhashed) {
                tfprintf(out, T("(Unhashed: inode %p, stream_id = %u)\n"),
                         lte->back_inode, lte->back_stream_id);
        } else {
-               tfprintf(out, T("Hash              = 0x"));
+               tfprintf(out, T("Hash                  = 0x"));
                print_hash(lte->hash, out);
                tputc(T('\n'), out);
        }
 
-       tfprintf(out, T("Flags             = "));
-       u8 flags = lte->resource_entry.flags;
+       tfprintf(out, T("Flags                 = "));
+       u8 flags = lte->flags;
        if (flags & WIM_RESHDR_FLAG_COMPRESSED)
                tfputs(T("WIM_RESHDR_FLAG_COMPRESSED, "), out);
        if (flags & WIM_RESHDR_FLAG_FREE)
@@ -687,26 +1046,26 @@ print_lookup_table_entry(const struct wim_lookup_table_entry *lte, FILE *out)
                tfputs(T("WIM_RESHDR_FLAG_METADATA, "), out);
        if (flags & WIM_RESHDR_FLAG_SPANNED)
                tfputs(T("WIM_RESHDR_FLAG_SPANNED, "), out);
+       if (flags & WIM_RESHDR_FLAG_PACKED_STREAMS)
+               tfputs(T("WIM_RESHDR_FLAG_PACKED_STREAMS, "), out);
        tputc(T('\n'), out);
        switch (lte->resource_location) {
        case RESOURCE_IN_WIM:
-               if (lte->wim->filename) {
-                       tfprintf(out, T("WIM file          = `%"TS"'\n"),
-                                lte->wim->filename);
+               if (lte->rspec->wim->filename) {
+                       tfprintf(out, T("WIM file              = `%"TS"'\n"),
+                                lte->rspec->wim->filename);
                }
                break;
 #ifdef __WIN32__
-       case RESOURCE_WIN32:
        case RESOURCE_WIN32_ENCRYPTED:
-#else
-       case RESOURCE_IN_FILE_ON_DISK:
 #endif
-               tfprintf(out, T("File on Disk      = `%"TS"'\n"),
+       case RESOURCE_IN_FILE_ON_DISK:
+               tfprintf(out, T("File on Disk          = `%"TS"'\n"),
                         lte->file_on_disk);
                break;
 #ifdef WITH_FUSE
        case RESOURCE_IN_STAGING_FILE:
-               tfprintf(out, T("Staging File      = `%"TS"'\n"),
+               tfprintf(out, T("Staging File          = `%"TS"'\n"),
                                lte->staging_file_name);
                break;
 #endif
@@ -716,28 +1075,76 @@ print_lookup_table_entry(const struct wim_lookup_table_entry *lte, FILE *out)
        tputc(T('\n'), out);
 }
 
+void
+lte_to_wimlib_resource_entry(const struct wim_lookup_table_entry *lte,
+                            struct wimlib_resource_entry *wentry)
+{
+       memset(wentry, 0, sizeof(*wentry));
+
+       wentry->uncompressed_size = lte->size;
+       if (lte->resource_location == RESOURCE_IN_WIM) {
+               wentry->part_number = lte->rspec->wim->hdr.part_number;
+               if (lte->flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
+                       wentry->compressed_size = 0;
+                       wentry->offset = lte->offset_in_res;
+               } else {
+                       wentry->compressed_size = lte->rspec->size_in_wim;
+                       wentry->offset = lte->rspec->offset_in_wim;
+               }
+               wentry->raw_resource_offset_in_wim = lte->rspec->offset_in_wim;
+               /*wentry->raw_resource_uncompressed_size = lte->rspec->uncompressed_size;*/
+               wentry->raw_resource_compressed_size = lte->rspec->size_in_wim;
+       }
+       copy_hash(wentry->sha1_hash, lte->hash);
+       wentry->reference_count = lte->refcnt;
+       wentry->is_compressed = (lte->flags & WIM_RESHDR_FLAG_COMPRESSED) != 0;
+       wentry->is_metadata = (lte->flags & WIM_RESHDR_FLAG_METADATA) != 0;
+       wentry->is_free = (lte->flags & WIM_RESHDR_FLAG_FREE) != 0;
+       wentry->is_spanned = (lte->flags & WIM_RESHDR_FLAG_SPANNED) != 0;
+       wentry->packed = (lte->flags & WIM_RESHDR_FLAG_PACKED_STREAMS) != 0;
+}
+
+struct iterate_lte_context {
+       wimlib_iterate_lookup_table_callback_t cb;
+       void *user_ctx;
+};
+
 static int
-do_print_lookup_table_entry(struct wim_lookup_table_entry *lte, void *fp)
+do_iterate_lte(struct wim_lookup_table_entry *lte, void *_ctx)
 {
-       print_lookup_table_entry(lte, (FILE*)fp);
-       return 0;
+       struct iterate_lte_context *ctx = _ctx;
+       struct wimlib_resource_entry entry;
+
+       lte_to_wimlib_resource_entry(lte, &entry);
+       return (*ctx->cb)(&entry, ctx->user_ctx);
 }
 
-/*
- * Prints the lookup table of a WIM file.
- */
-WIMLIBAPI void
-wimlib_print_lookup_table(WIMStruct *w)
+/* API function documented in wimlib.h  */
+WIMLIBAPI int
+wimlib_iterate_lookup_table(WIMStruct *wim, int flags,
+                           wimlib_iterate_lookup_table_callback_t cb,
+                           void *user_ctx)
 {
-       for_lookup_table_entry(w->lookup_table,
-                              do_print_lookup_table_entry,
-                              stdout);
+       struct iterate_lte_context ctx = {
+               .cb = cb,
+               .user_ctx = user_ctx,
+       };
+       if (wim->hdr.part_number == 1) {
+               int ret;
+               for (int i = 0; i < wim->hdr.image_count; i++) {
+                       ret = do_iterate_lte(wim->image_metadata[i]->metadata_lte,
+                                            &ctx);
+                       if (ret)
+                               return ret;
+               }
+       }
+       return for_lookup_table_entry(wim->lookup_table, do_iterate_lte, &ctx);
 }
 
 /* Given a SHA1 message digest, return the corresponding entry in the WIM's
  * lookup table, or NULL if there is none.  */
 struct wim_lookup_table_entry *
-__lookup_resource(const struct wim_lookup_table *table, const u8 hash[])
+lookup_resource(const struct wim_lookup_table *table, const u8 hash[])
 {
        size_t i;
        struct wim_lookup_table_entry *lte;
@@ -761,12 +1168,12 @@ __lookup_resource(const struct wim_lookup_table *table, const u8 hash[])
  * This is only for pre-resolved inodes.
  */
 int
-lookup_resource(WIMStruct *w,
-               const tchar *path,
-               int lookup_flags,
-               struct wim_dentry **dentry_ret,
-               struct wim_lookup_table_entry **lte_ret,
-               u16 *stream_idx_ret)
+wim_pathname_to_stream(WIMStruct *wim,
+                      const tchar *path,
+                      int lookup_flags,
+                      struct wim_dentry **dentry_ret,
+                      struct wim_lookup_table_entry **lte_ret,
+                      u16 *stream_idx_ret)
 {
        struct wim_dentry *dentry;
        struct wim_lookup_table_entry *lte;
@@ -783,7 +1190,7 @@ lookup_resource(WIMStruct *w,
                }
        }
 
-       dentry = get_dentry(w, path);
+       dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
        if (p)
                *p = T(':');
        if (!dentry)
@@ -791,7 +1198,9 @@ lookup_resource(WIMStruct *w,
 
        inode = dentry->d_inode;
 
-       wimlib_assert(inode->i_resolved);
+       if (!inode->i_resolved)
+               if (inode_resolve_ltes(inode, wim->lookup_table, false))
+                       return -EIO;
 
        if (!(lookup_flags & LOOKUP_FLAG_DIRECTORY_OK)
              && inode_is_directory(inode))
@@ -810,8 +1219,7 @@ lookup_resource(WIMStruct *w,
                        return -ENOENT;
                }
        } else {
-               lte = inode->i_lte;
-               stream_idx = 0;
+               lte = inode_unnamed_stream_resolved(inode, &stream_idx);
        }
 out:
        if (dentry_ret)
@@ -824,33 +1232,92 @@ out:
 }
 #endif
 
-/* Resolve an inode's lookup table entries
+int
+resource_not_found_error(const struct wim_inode *inode, const u8 *hash)
+{
+       if (wimlib_print_errors) {
+               ERROR("\"%"TS"\": resource not found", inode_first_full_path(inode));
+               tfprintf(stderr, T("        SHA-1 message digest of missing resource:\n        "));
+               print_hash(hash, stderr);
+               tputc(T('\n'), stderr);
+       }
+       return WIMLIB_ERR_RESOURCE_NOT_FOUND;
+}
+
+/*
+ * Resolve an inode's lookup table entries.
  *
  * This replaces the SHA1 hash fields (which are used to lookup an entry in the
- * lookup table) with pointers directly to the lookup table entries.  A circular
- * linked list of streams sharing the same lookup table entry is created.
+ * lookup table) with pointers directly to the lookup table entries.
  *
- * This function always succeeds; unresolved lookup table entries are given a
- * NULL pointer.
+ * If @force is %false:
+ *     If any needed SHA1 message digests are not found in the lookup table,
+ *     WIMLIB_ERR_RESOURCE_NOT_FOUND is returned and the inode is left
+ *     unmodified.
+ * If @force is %true:
+ *     If any needed SHA1 message digests are not found in the lookup table,
+ *     new entries are allocated and inserted into the lookup table.
  */
-void
-inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table)
+int
+inode_resolve_ltes(struct wim_inode *inode, struct wim_lookup_table *table,
+                  bool force)
 {
+       const u8 *hash;
 
        if (!inode->i_resolved) {
-               struct wim_lookup_table_entry *lte;
+               struct wim_lookup_table_entry *lte, *ads_lte;
+
                /* Resolve the default file stream */
-               lte = __lookup_resource(table, inode->i_hash);
-               inode->i_lte = lte;
-               inode->i_resolved = 1;
+               lte = NULL;
+               hash = inode->i_hash;
+               if (!is_zero_hash(hash)) {
+                       lte = lookup_resource(table, hash);
+                       if (!lte) {
+                               if (force) {
+                                       lte = new_lookup_table_entry();
+                                       if (!lte)
+                                               return WIMLIB_ERR_NOMEM;
+                                       copy_hash(lte->hash, hash);
+                                       lookup_table_insert(table, lte);
+                               } else {
+                                       goto resource_not_found;
+                               }
+                       }
+               }
 
                /* Resolve the alternate data streams */
+               struct wim_lookup_table_entry *ads_ltes[inode->i_num_ads];
                for (u16 i = 0; i < inode->i_num_ads; i++) {
-                       struct wim_ads_entry *cur_entry = &inode->i_ads_entries[i];
-                       lte = __lookup_resource(table, cur_entry->hash);
-                       cur_entry->lte = lte;
+                       struct wim_ads_entry *cur_entry;
+
+                       ads_lte = NULL;
+                       cur_entry = &inode->i_ads_entries[i];
+                       hash = cur_entry->hash;
+                       if (!is_zero_hash(hash)) {
+                               ads_lte = lookup_resource(table, hash);
+                               if (!ads_lte) {
+                                       if (force) {
+                                               ads_lte = new_lookup_table_entry();
+                                               if (!ads_lte)
+                                                       return WIMLIB_ERR_NOMEM;
+                                               copy_hash(ads_lte->hash, hash);
+                                               lookup_table_insert(table, ads_lte);
+                                       } else {
+                                               goto resource_not_found;
+                                       }
+                               }
+                       }
+                       ads_ltes[i] = ads_lte;
                }
+               inode->i_lte = lte;
+               for (u16 i = 0; i < inode->i_num_ads; i++)
+                       inode->i_ads_entries[i].lte = ads_ltes[i];
+               inode->i_resolved = 1;
        }
+       return 0;
+
+resource_not_found:
+       return resource_not_found_error(inode, hash);
 }
 
 void
@@ -891,19 +1358,28 @@ inode_stream_lte(const struct wim_inode *inode, unsigned stream_idx,
 }
 
 struct wim_lookup_table_entry *
-inode_unnamed_lte_resolved(const struct wim_inode *inode)
+inode_unnamed_stream_resolved(const struct wim_inode *inode, u16 *stream_idx_ret)
 {
        wimlib_assert(inode->i_resolved);
        for (unsigned i = 0; i <= inode->i_num_ads; i++) {
                if (inode_stream_name_nbytes(inode, i) == 0 &&
                    !is_zero_hash(inode_stream_hash_resolved(inode, i)))
                {
+                       *stream_idx_ret = i;
                        return inode_stream_lte_resolved(inode, i);
                }
        }
+       *stream_idx_ret = 0;
        return NULL;
 }
 
+struct wim_lookup_table_entry *
+inode_unnamed_lte_resolved(const struct wim_inode *inode)
+{
+       u16 stream_idx;
+       return inode_unnamed_stream_resolved(inode, &stream_idx);
+}
+
 struct wim_lookup_table_entry *
 inode_unnamed_lte_unresolved(const struct wim_inode *inode,
                             const struct wim_lookup_table *table)
@@ -945,19 +1421,22 @@ inode_unnamed_lte(const struct wim_inode *inode,
                return inode_unnamed_lte_unresolved(inode, table);
 }
 
-static int
-lte_add_stream_size(struct wim_lookup_table_entry *lte, void *total_bytes_p)
+/* Returns the SHA1 message digest of the unnamed data stream of a WIM inode, or
+ * 'zero_hash' if the unnamed data stream is missing has all zeroes in its SHA1
+ * message digest field.  */
+const u8 *
+inode_unnamed_stream_hash(const struct wim_inode *inode)
 {
-       *(u64*)total_bytes_p += lte->resource_entry.size;
-       return 0;
-}
+       const u8 *hash;
 
-u64
-lookup_table_total_stream_size(struct wim_lookup_table *table)
-{
-       u64 total_size = 0;
-       for_lookup_table_entry(table, lte_add_stream_size, &total_size);
-       return total_size;
+       for (unsigned i = 0; i <= inode->i_num_ads; i++) {
+               if (inode_stream_name_nbytes(inode, i) == 0) {
+                       hash = inode_stream_hash(inode, i);
+                       if (!is_zero_hash(hash))
+                               return hash;
+               }
+       }
+       return zero_hash;
 }
 
 struct wim_lookup_table_entry **
@@ -1005,33 +1484,211 @@ hash_unhashed_stream(struct wim_lookup_table_entry *lte,
         * the SHA1 has been calculated. */
        back_ptr = retrieve_lte_pointer(lte);
 
-       ret = sha1_resource(lte);
+       ret = sha1_stream(lte);
        if (ret)
                return ret;
 
        /* Look for a duplicate stream */
-       duplicate_lte = __lookup_resource(lookup_table, lte->hash);
+       duplicate_lte = lookup_resource(lookup_table, lte->hash);
        list_del(&lte->unhashed_list);
        if (duplicate_lte) {
                /* We have a duplicate stream.  Transfer the reference counts
-                * from this stream to the duplicate, update the reference to
+                * from this stream to the duplicate and update the reference to
                 * this stream (in an inode or ads_entry) to point to the
-                * duplicate, then free this stream. */
+                * duplicate.  The caller is responsible for freeing @lte if
+                * needed.  */
                wimlib_assert(!(duplicate_lte->unhashed));
+               wimlib_assert(duplicate_lte->size == lte->size);
                duplicate_lte->refcnt += lte->refcnt;
-               duplicate_lte->out_refcnt += lte->refcnt;
+               lte->refcnt = 0;
                *back_ptr = duplicate_lte;
-               free_lookup_table_entry(lte);
                lte = duplicate_lte;
        } else {
-               /* No duplicate stream, so we need to insert
-                * this stream into the lookup table and treat
-                * it as a hashed stream. */
+               /* No duplicate stream, so we need to insert this stream into
+                * the lookup table and treat it as a hashed stream. */
                lookup_table_insert(lookup_table, lte);
                lte->unhashed = 0;
        }
-       if (lte_ret)
-               *lte_ret = lte;
+       *lte_ret = lte;
+       return 0;
+}
+
+static int
+lte_clone_if_new(struct wim_lookup_table_entry *lte, void *_lookup_table)
+{
+       struct wim_lookup_table *lookup_table = _lookup_table;
+
+       if (lookup_resource(lookup_table, lte->hash))
+               return 0;  /*  Resource already present.  */
+
+       lte = clone_lookup_table_entry(lte);
+       if (lte == NULL)
+               return WIMLIB_ERR_NOMEM;
+       lte->out_refcnt = 1;
+       lookup_table_insert(lookup_table, lte);
        return 0;
 }
 
+static int
+lte_delete_if_new(struct wim_lookup_table_entry *lte, void *_lookup_table)
+{
+       struct wim_lookup_table *lookup_table = _lookup_table;
+
+       if (lte->out_refcnt) {
+               lookup_table_unlink(lookup_table, lte);
+               free_lookup_table_entry(lte);
+       }
+       return 0;
+}
+
+/* API function documented in wimlib.h  */
+WIMLIBAPI int
+wimlib_reference_resources(WIMStruct *wim,
+                          WIMStruct **resource_wims, unsigned num_resource_wims,
+                          int ref_flags)
+{
+       int ret;
+       unsigned i;
+
+       if (wim == NULL)
+               return WIMLIB_ERR_INVALID_PARAM;
+
+       if (num_resource_wims != 0 && resource_wims == NULL)
+               return WIMLIB_ERR_INVALID_PARAM;
+
+       for (i = 0; i < num_resource_wims; i++)
+               if (resource_wims[i] == NULL)
+                       return WIMLIB_ERR_INVALID_PARAM;
+
+       for_lookup_table_entry(wim->lookup_table, lte_zero_out_refcnt, NULL);
+
+       for (i = 0; i < num_resource_wims; i++) {
+               ret = for_lookup_table_entry(resource_wims[i]->lookup_table,
+                                            lte_clone_if_new,
+                                            wim->lookup_table);
+               if (ret)
+                       goto out_rollback;
+       }
+       return 0;
+
+out_rollback:
+       for_lookup_table_entry(wim->lookup_table, lte_delete_if_new,
+                              wim->lookup_table);
+       return ret;
+}
+
+static int
+reference_resource_paths(WIMStruct *wim,
+                        const tchar * const *resource_wimfiles,
+                        unsigned num_resource_wimfiles,
+                        int ref_flags,
+                        int open_flags,
+                        wimlib_progress_func_t progress_func)
+{
+       WIMStruct **resource_wims;
+       unsigned i;
+       int ret;
+
+       resource_wims = CALLOC(num_resource_wimfiles, sizeof(resource_wims[0]));
+       if (!resource_wims)
+               return WIMLIB_ERR_NOMEM;
+
+       for (i = 0; i < num_resource_wimfiles; i++) {
+               DEBUG("Referencing resources from path \"%"TS"\"",
+                     resource_wimfiles[i]);
+               ret = wimlib_open_wim(resource_wimfiles[i], open_flags,
+                                     &resource_wims[i], progress_func);
+               if (ret)
+                       goto out_free_resource_wims;
+       }
+
+       ret = wimlib_reference_resources(wim, resource_wims,
+                                        num_resource_wimfiles, ref_flags);
+       if (ret)
+               goto out_free_resource_wims;
+
+       for (i = 0; i < num_resource_wimfiles; i++)
+               list_add_tail(&resource_wims[i]->subwim_node, &wim->subwims);
+
+       ret = 0;
+       goto out_free_array;
+
+out_free_resource_wims:
+       for (i = 0; i < num_resource_wimfiles; i++)
+               wimlib_free(resource_wims[i]);
+out_free_array:
+       FREE(resource_wims);
+       return ret;
+}
+
+static int
+reference_resource_glob(WIMStruct *wim, const tchar *refglob,
+                       int ref_flags, int open_flags,
+                       wimlib_progress_func_t progress_func)
+{
+       glob_t globbuf;
+       int ret;
+
+       /* Note: glob() is replaced in Windows native builds.  */
+       ret = tglob(refglob, GLOB_ERR | GLOB_NOSORT, NULL, &globbuf);
+       if (ret) {
+               if (ret == GLOB_NOMATCH) {
+                       if (ref_flags & WIMLIB_REF_FLAG_GLOB_ERR_ON_NOMATCH) {
+                               ERROR("Found no files for glob \"%"TS"\"", refglob);
+                               return WIMLIB_ERR_GLOB_HAD_NO_MATCHES;
+                       } else {
+                               return reference_resource_paths(wim,
+                                                               &refglob,
+                                                               1,
+                                                               ref_flags,
+                                                               open_flags,
+                                                               progress_func);
+                       }
+               } else {
+                       ERROR_WITH_ERRNO("Failed to process glob \"%"TS"\"", refglob);
+                       if (ret == GLOB_NOSPACE)
+                               return WIMLIB_ERR_NOMEM;
+                       else
+                               return WIMLIB_ERR_READ;
+               }
+       }
+
+       ret = reference_resource_paths(wim,
+                                      (const tchar * const *)globbuf.gl_pathv,
+                                      globbuf.gl_pathc,
+                                      ref_flags,
+                                      open_flags,
+                                      progress_func);
+       globfree(&globbuf);
+       return ret;
+}
+
+/* API function documented in wimlib.h  */
+WIMLIBAPI int
+wimlib_reference_resource_files(WIMStruct *wim,
+                               const tchar * const * resource_wimfiles_or_globs,
+                               unsigned count,
+                               int ref_flags,
+                               int open_flags,
+                               wimlib_progress_func_t progress_func)
+{
+       unsigned i;
+       int ret;
+
+       if (ref_flags & WIMLIB_REF_FLAG_GLOB_ENABLE) {
+               for (i = 0; i < count; i++) {
+                       ret = reference_resource_glob(wim,
+                                                     resource_wimfiles_or_globs[i],
+                                                     ref_flags,
+                                                     open_flags,
+                                                     progress_func);
+                       if (ret)
+                               return ret;
+               }
+               return 0;
+       } else {
+               return reference_resource_paths(wim, resource_wimfiles_or_globs,
+                                               count, ref_flags,
+                                               open_flags, progress_func);
+       }
+}