]> wimlib.net Git - wimlib/blobdiff - src/lookup_table.c
Cache compression format in 'struct wim_resource_spec'
[wimlib] / src / lookup_table.c
index 005d5ce839e1ca89e1b091df6734139cec95ec19..815d576fe551177200b8b781c3a69621d7210530 100644 (file)
@@ -82,7 +82,6 @@ new_lookup_table_entry(void)
        }
        lte->refcnt = 1;
        BUILD_BUG_ON(RESOURCE_NONEXISTENT != 0);
-       BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_NONE != 0);
        return lte;
 }
 
@@ -92,13 +91,13 @@ clone_lookup_table_entry(const struct wim_lookup_table_entry *old)
        struct wim_lookup_table_entry *new;
 
        new = memdup(old, sizeof(struct wim_lookup_table_entry));
-       if (!new)
+       if (new == NULL)
                return NULL;
 
        new->extracted_file = NULL;
        switch (new->resource_location) {
        case RESOURCE_IN_WIM:
-               list_add(&new->wim_resource_list, &new->rspec->lte_list);
+               list_add(&new->rspec_node, &new->rspec->stream_list);
                break;
 
        case RESOURCE_IN_FILE_ON_DISK:
@@ -111,12 +110,12 @@ 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 = memdup(old->attached_buffer, old->size);
-               if (!new->attached_buffer)
+               if (new->attached_buffer == NULL)
                        goto out_free;
                break;
 #ifdef WITH_NTFS_3G
@@ -124,18 +123,18 @@ clone_lookup_table_entry(const struct wim_lookup_table_entry *old)
                if (old->ntfs_loc) {
                        struct ntfs_location *loc;
                        loc = memdup(old->ntfs_loc, sizeof(struct ntfs_location));
-                       if (!loc)
+                       if (loc == NULL)
                                goto out_free;
                        loc->path = NULL;
                        loc->stream_name = NULL;
                        new->ntfs_loc = loc;
                        loc->path = STRDUP(old->ntfs_loc->path);
-                       if (!loc->path)
+                       if (loc->path == NULL)
                                goto out_free;
-                       if (loc->stream_name_nchars) {
+                       if (loc->stream_name_nchars != 0) {
                                loc->stream_name = memdup(old->ntfs_loc->stream_name,
                                                          loc->stream_name_nchars * 2);
-                               if (!loc->stream_name)
+                               if (loc->stream_name == NULL)
                                        goto out_free;
                        }
                }
@@ -156,8 +155,8 @@ free_lookup_table_entry(struct wim_lookup_table_entry *lte)
        if (lte) {
                switch (lte->resource_location) {
                case RESOURCE_IN_WIM:
-                       list_del(&lte->wim_resource_list);
-                       if (list_empty(&lte->rspec->lte_list))
+                       list_del(&lte->rspec_node);
+                       if (list_empty(&lte->rspec->stream_list))
                                FREE(lte->rspec);
                        break;
                case RESOURCE_IN_FILE_ON_DISK:
@@ -201,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,
@@ -213,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.
  *
@@ -223,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
@@ -289,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;
@@ -299,7 +332,7 @@ for_lookup_table_entry(struct wim_lookup_table *table,
 }
 
 /* qsort() callback that sorts streams (represented by `struct
- * wim_lookup_table_entry's) into an order optimized for reading and writing.
+ * 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
@@ -338,12 +371,12 @@ cmp_streams_by_sequential_order(const void *p1, const void *p2)
                if (v)
                        return v;
 
-               /* Compare by offset.  */
-               if (lte1->rspec->offset_in_wim < lte2->rspec->offset_in_wim)
-                       return -1;
-               if (lte1->rspec->offset_in_wim > lte2->rspec->offset_in_wim)
-                       return 1;
-               return 0;
+               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:
@@ -367,8 +400,9 @@ cmp_streams_by_sequential_order(const void *p1, const void *p2)
 }
 
 int
-sort_stream_list_by_sequential_order(struct list_head *stream_list,
-                                    size_t list_head_offset)
+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;
@@ -379,10 +413,14 @@ sort_stream_list_by_sequential_order(struct list_head *stream_list,
        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)
+       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 -
@@ -390,8 +428,7 @@ sort_stream_list_by_sequential_order(struct list_head *stream_list,
                cur = cur->next;
        }
 
-       qsort(array, num_streams, sizeof(array[0]),
-             cmp_streams_by_sequential_order);
+       qsort(array, num_streams, sizeof(array[0]), compar);
 
        INIT_LIST_HEAD(stream_list);
        for (i = 0; i < num_streams; i++) {
@@ -403,6 +440,15 @@ sort_stream_list_by_sequential_order(struct list_head *stream_list,
        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);
+}
+
 
 static int
 add_lte_to_array(struct wim_lookup_table_entry *lte,
@@ -464,35 +510,41 @@ struct wim_lookup_table_entry_disk {
 
 #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,
-                 u64 offset_save, u64 size_save)
+validate_resource(const struct wim_resource_spec *rspec)
 {
        struct wim_lookup_table_entry *lte;
-       if (!list_is_singular(&rspec->lte_list)) {
-               list_for_each_entry(lte, &rspec->lte_list, wim_resource_list) {
-                       if (rspec->flags & WIM_RESHDR_FLAG_COMPRESSED)
-                               lte->flags |= WIM_RESHDR_FLAG_COMPRESSED;
-                       else
-                               lte->flags &= ~WIM_RESHDR_FLAG_COMPRESSED;
-
-                       if (!(lte->flags & WIM_RESHDR_FLAG_CONCAT)) {
-                               lte->offset_in_res = offset_save;
-                               lte->size = size_save;
-                       }
-
-                       if (lte->offset_in_res + lte->size < lte->size ||
-                           lte->offset_in_res + lte->size > rspec->uncompressed_size)
-                       {
-                               return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
-                       }
-               }
+       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
@@ -502,6 +554,8 @@ validate_resource(const struct wim_resource_spec *rspec,
  *     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_wim_lookup_table(WIMStruct *wim)
@@ -512,15 +566,15 @@ read_wim_lookup_table(WIMStruct *wim)
        struct wim_lookup_table *table;
        struct wim_lookup_table_entry *cur_entry, *duplicate_entry;
        struct wim_resource_spec *cur_rspec;
-       u64 size_save;
-       u64 offset_save;
        void *buf;
+       bool back_to_back_pack;
 
+       DEBUG("Reading lookup table.");
+
+       /* 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);
 
-       DEBUG("Reading lookup table.");
-
        /* Calculate number of entries in the lookup table.  */
        num_entries = wim->hdr.lookup_table_reshdr.uncompressed_size /
                      sizeof(struct wim_lookup_table_entry_disk);
@@ -530,7 +584,8 @@ read_wim_lookup_table(WIMStruct *wim)
        if (ret)
                goto out;
 
-       /* Allocate hash table.  */
+       /* 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 == NULL) {
                ERROR("Not enough memory to read lookup table.");
@@ -538,8 +593,8 @@ read_wim_lookup_table(WIMStruct *wim)
                goto out_free_buf;
        }
 
-       /* Allocate and initalize `struct wim_lookup_table_entry's from the
-        * on-disk lookup table.  */
+       /* 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++) {
@@ -548,11 +603,7 @@ read_wim_lookup_table(WIMStruct *wim)
                u16 part_number;
                struct wim_reshdr reshdr;
 
-               ret = get_wim_reshdr(&disk_entry->reshdr, &reshdr);
-               if (ret) {
-                       ERROR("Resource header is invalid!");
-                       goto out_free_lookup_table;
-               }
+               get_wim_reshdr(&disk_entry->reshdr, &reshdr);
 
                DEBUG("reshdr: size_in_wim=%"PRIu64", "
                      "uncompressed_size=%"PRIu64", "
@@ -561,11 +612,14 @@ read_wim_lookup_table(WIMStruct *wim)
                      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;
+
                cur_entry = new_lookup_table_entry();
                if (cur_entry == NULL) {
                        ERROR("Not enough memory to read lookup table!");
                        ret = WIMLIB_ERR_NOMEM;
-                       goto out_free_lookup_table;
+                       goto err;
                }
 
                part_number = le16_to_cpu(disk_entry->part_number);
@@ -580,52 +634,118 @@ read_wim_lookup_table(WIMStruct *wim)
                        continue;
                }
 
-               if (cur_rspec == NULL ||
-                   !(reshdr.flags & WIM_RESHDR_FLAG_CONCAT))
+               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;
+                       }
+               }
+
+               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)))
                {
-                       /* Starting new run of stream entries that all share the
-                        * same WIM resource (streams concatenated together); or
-                        * simply a single normal entry by itself.  */
+                       /* 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, offset_save,
-                                                       size_save);
+                               ret = validate_resource(cur_rspec);
                                if (ret)
-                                       goto out_free_cur_entry;
+                                       goto err;
                        }
 
-                       cur_rspec = MALLOC(sizeof(struct wim_resource_spec));
+                       /* 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 out_free_cur_entry;
+                               goto err;
                        }
-                       offset_save = reshdr.offset_in_wim;
-                       size_save = reshdr.size_in_wim;
                        wim_res_hdr_to_spec(&reshdr, wim, cur_rspec);
-               } else if (is_zero_hash(cur_entry->hash)) {
-                       /* Found the resource specification for the run.  */
+
+                       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;
-                       DEBUG("Full run is %"PRIu64" compressed bytes "
+
+                       /* 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;
-               } else {
-                       /* Continuing the run with another stream.  */
-                       DEBUG("Continuing concat run with stream: "
-                             "%"PRIu64" uncompressed bytes @ resource offset %"PRIu64")",
+               }
+
+               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);
-                       cur_rspec->uncompressed_size += reshdr.size_in_wim;
                }
 
                lte_bind_wim_resource_spec(cur_entry, cur_rspec);
-               if (reshdr.flags & WIM_RESHDR_FLAG_CONCAT) {
-                       /* In concatenation runs, the offset field is used for
+               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.  */
@@ -633,36 +753,32 @@ read_wim_lookup_table(WIMStruct *wim)
                        cur_entry->size = reshdr.size_in_wim;
                        cur_entry->flags = reshdr.flags;
                } else {
-                       /* These may be overwritten in validate_resource() if
-                        * the run turns out to be a concatenation.  */
+                       /* 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;
-               }
-
-               if (is_zero_hash(cur_entry->hash)) {
-                       WARNING("The WIM lookup table contains an entry with a "
-                               "SHA1 message digest of all 0's (ignoring it)");
-                       free_lookup_table_entry(cur_entry);
-                       continue;
+                       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) {
-                               /* Metadata entries with no references must be
-                                * ignored.  See for example the WinPE WIMs from
-                                * WAIK v2.1.  */
-                               if (cur_entry->refcnt == 0) {
-                                       free_lookup_table_entry(cur_entry);
-                                       continue;
-                               }
                                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 (wim->hdr.part_number != 1) {
@@ -693,31 +809,36 @@ read_wim_lookup_table(WIMStruct *wim)
                              cur_entry->rspec->offset_in_wim);
                        wim->image_metadata[
                                wim->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) {
-                               if (wimlib_print_errors) {
-                                       WARNING("The WIM lookup table contains two entries with the "
-                                             "same SHA1 message digest!");
-                                       WARNING("The first entry is:");
-                                       print_lookup_table_entry(duplicate_entry, stderr);
-                                       WARNING("The second entry is:");
-                                       print_lookup_table_entry(cur_entry, stderr);
-                               }
-                               free_lookup_table_entry(cur_entry);
-                               continue;
-                       } else {
-                               lookup_table_insert(table, 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!");
+                               WARNING("The first entry is:");
+                               print_lookup_table_entry(duplicate_entry, stderr);
+                               WARNING("The second entry is:");
+                               print_lookup_table_entry(cur_entry, stderr);
                        }
+                       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;
 
+       /* Validate the last resource.  */
        if (cur_rspec != NULL) {
-               ret = validate_resource(cur_rspec, offset_save, size_save);
+               ret = validate_resource(cur_rspec);
                if (ret)
-                       goto out_free_cur_entry;
+                       goto err;
        }
 
        if (wim->hdr.part_number == 1 && wim->current_image != wim->hdr.image_count) {
@@ -735,9 +856,10 @@ read_wim_lookup_table(WIMStruct *wim)
        ret = 0;
        goto out_free_buf;
 
-out_free_cur_entry:
-       FREE(cur_entry);
-out_free_lookup_table:
+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);
@@ -746,48 +868,91 @@ out:
        return ret;
 }
 
-
 static void
-write_wim_lookup_table_entry(const struct wim_lookup_table_entry *lte,
-                            struct wim_lookup_table_entry_disk *disk_entry,
-                            u16 part_number)
+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)
 {
-       put_wim_reshdr(&lte->out_reshdr, &disk_entry->reshdr);
+       put_wim_reshdr(out_reshdr, &disk_entry->reshdr);
        disk_entry->part_number = cpu_to_le16(part_number);
-       disk_entry->refcnt = cpu_to_le32(lte->out_refcnt);
-       copy_hash(disk_entry->hash, lte->hash);
+       disk_entry->refcnt = cpu_to_le32(refcnt);
+       copy_hash(disk_entry->hash, hash);
 }
 
-static int
+int
 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,
-                                       struct wimlib_lzx_context **comp_ctx)
+                                       int write_resource_flags)
 {
        size_t table_size;
        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;
 
        table_size = 0;
-       list_for_each_entry(lte, stream_list, lookup_table_list)
+       list_for_each_entry(lte, stream_list, lookup_table_list) {
                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;
+               }
+       }
+
        DEBUG("Writing WIM lookup table (size=%zu, offset=%"PRIu64")",
              table_size, out_fd->offset);
 
        table_buf = MALLOC(table_size);
-       if (!table_buf) {
+       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;
-       list_for_each_entry(lte, stream_list, lookup_table_list)
-               write_wim_lookup_table_entry(lte, table_buf_ptr++, part_number);
+
+       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;
+               }
+
+       }
+       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.  */
@@ -799,101 +964,12 @@ write_wim_lookup_table_from_stream_list(struct list_head *stream_list,
                                             0,
                                             out_reshdr,
                                             NULL,
-                                            write_resource_flags,
-                                            comp_ctx);
+                                            write_resource_flags);
        FREE(table_buf);
        DEBUG("ret=%d", ret);
        return ret;
 }
 
-static int
-append_lookup_table_entry(struct wim_lookup_table_entry *lte, void *_list)
-{
-       /* Lookup table entries with 'out_refcnt' == 0 correspond to streams not
-        * written and not present in the resulting WIM file, and should not be
-        * included in the lookup table.
-        *
-        * Lookup table entries marked as filtered (EXTERNAL_WIM) with
-        * 'out_refcnt != 0' were referenced as part of the logical write but
-        * correspond to streams that were not in fact written, and should not
-        * be included in the lookup table.
-        *
-        * Lookup table entries marked as filtered (SAME_WIM) with 'out_refcnt
-        * != 0' were referenced as part of the logical write but correspond to
-        * streams that were not in fact written, but nevertheless were already
-        * present in the WIM being overwritten in-place.  These entries must be
-        * included in the lookup table, and the resource information to write
-        * needs to be copied from the resource information read originally.
-        */
-       if (lte->out_refcnt != 0 && !(lte->filtered & FILTERED_EXTERNAL_WIM)) {
-               if (lte->filtered & FILTERED_SAME_WIM)
-                       wim_res_spec_to_hdr(lte->rspec, &lte->out_reshdr);
-               list_add_tail(&lte->lookup_table_list, (struct list_head*)_list);
-       }
-       return 0;
-}
-
-int
-write_wim_lookup_table(WIMStruct *wim, int image, int write_flags,
-                      struct wim_reshdr *out_reshdr,
-                      struct list_head *stream_list_override)
-{
-       int write_resource_flags;
-       struct list_head _stream_list;
-       struct list_head *stream_list;
-
-       if (stream_list_override) {
-               stream_list = stream_list_override;
-       } else {
-               stream_list = &_stream_list;
-               INIT_LIST_HEAD(stream_list);
-       }
-
-       if (!(write_flags & WIMLIB_WRITE_FLAG_NO_METADATA)) {
-               int start_image;
-               int end_image;
-
-               if (image == WIMLIB_ALL_IMAGES) {
-                       start_image = 1;
-                       end_image = wim->hdr.image_count;
-               } else {
-                       start_image = image;
-                       end_image = image;
-               }
-
-               /* Push metadata resource lookup table entries onto the front of
-                * the list in reverse order, so that they're written in order.
-                */
-               for (int i = end_image; i >= start_image; i--) {
-                       struct wim_lookup_table_entry *metadata_lte;
-
-                       metadata_lte = wim->image_metadata[i - 1]->metadata_lte;
-                       metadata_lte->out_refcnt = 1;
-                       metadata_lte->out_reshdr.flags |= WIM_RESHDR_FLAG_METADATA;
-                       list_add(&metadata_lte->lookup_table_list, stream_list);
-               }
-       }
-
-       /* Append additional lookup table entries that need to be written, with
-        * some special handling for streams that have been marked as filtered.
-        */
-       if (!stream_list_override) {
-               for_lookup_table_entry(wim->lookup_table,
-                                      append_lookup_table_entry, stream_list);
-       }
-
-       write_resource_flags = 0;
-       if (write_flags & WIMLIB_WRITE_FLAG_PIPABLE)
-               write_resource_flags |= WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE;
-       return write_wim_lookup_table_from_stream_list(stream_list,
-                                                      &wim->out_fd,
-                                                      wim->hdr.part_number,
-                                                      out_reshdr,
-                                                      write_resource_flags,
-                                                      &wim->lzx_context);
-}
-
-
 int
 lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
 {
@@ -929,7 +1005,7 @@ print_lookup_table_entry(const struct wim_lookup_table_entry *lte, FILE *out)
 
        tprintf(T("Uncompressed size     = %"PRIu64" bytes\n"),
                lte->size);
-       if (lte_is_partial(lte)) {
+       if (lte->flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
                tprintf(T("Offset                = %"PRIu64" bytes\n"),
                        lte->offset_in_res);
 
@@ -970,8 +1046,8 @@ 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_CONCAT)
-               tfputs(T("WIM_RESHDR_FLAG_CONCAT, "), 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:
@@ -1008,7 +1084,7 @@ lte_to_wimlib_resource_entry(const struct wim_lookup_table_entry *lte,
        wentry->uncompressed_size = lte->size;
        if (lte->resource_location == RESOURCE_IN_WIM) {
                wentry->part_number = lte->rspec->wim->hdr.part_number;
-               if (lte_is_partial(lte)) {
+               if (lte->flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
                        wentry->compressed_size = 0;
                        wentry->offset = lte->offset_in_res;
                } else {
@@ -1016,7 +1092,7 @@ lte_to_wimlib_resource_entry(const struct wim_lookup_table_entry *lte,
                        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_uncompressed_size = lte->rspec->uncompressed_size;*/
                wentry->raw_resource_compressed_size = lte->rspec->size_in_wim;
        }
        copy_hash(wentry->sha1_hash, lte->hash);
@@ -1025,7 +1101,7 @@ lte_to_wimlib_resource_entry(const struct wim_lookup_table_entry *lte,
        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->is_partial = lte_is_partial(lte);
+       wentry->packed = (lte->flags & WIM_RESHDR_FLAG_PACKED_STREAMS) != 0;
 }
 
 struct iterate_lte_context {
@@ -1114,7 +1190,7 @@ wim_pathname_to_stream(WIMStruct *wim,
                }
        }
 
-       dentry = get_dentry(wim, path);
+       dentry = get_dentry(wim, path, WIMLIB_CASE_SENSITIVE);
        if (p)
                *p = T(':');
        if (!dentry)
@@ -1417,24 +1493,23 @@ hash_unhashed_stream(struct wim_lookup_table_entry *lte,
        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->out_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;
 }
 
@@ -1447,7 +1522,7 @@ lte_clone_if_new(struct wim_lookup_table_entry *lte, void *_lookup_table)
                return 0;  /*  Resource already present.  */
 
        lte = clone_lookup_table_entry(lte);
-       if (!lte)
+       if (lte == NULL)
                return WIMLIB_ERR_NOMEM;
        lte->out_refcnt = 1;
        lookup_table_insert(lookup_table, lte);