+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);
+}
+
+
+static int
+add_lte_to_array(struct wim_lookup_table_entry *lte,
+ void *_pp)
+{
+ struct wim_lookup_table_entry ***pp = _pp;
+ *(*pp)++ = lte;
+ return 0;
+}
+
+/* Iterate through the lookup table entries, but first sort them by stream
+ * offset in the WIM. Caution: this is intended to be used when the stream
+ * offset field has actually been set. */
+int
+for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
+ int (*visitor)(struct wim_lookup_table_entry *,
+ void *),
+ void *arg)
+{
+ struct wim_lookup_table_entry **lte_array, **p;
+ size_t num_streams = table->num_entries;
+ int ret;
+
+ lte_array = MALLOC(num_streams * sizeof(lte_array[0]));
+ if (!lte_array)
+ return WIMLIB_ERR_NOMEM;
+ p = lte_array;
+ for_lookup_table_entry(table, add_lte_to_array, &p);
+
+ wimlib_assert(p == lte_array + num_streams);
+
+ qsort(lte_array, num_streams, sizeof(lte_array[0]),
+ cmp_streams_by_sequential_order);
+ ret = 0;
+ for (size_t i = 0; i < num_streams; i++) {
+ ret = visitor(lte_array[i], arg);
+ if (ret)
+ break;
+ }
+ FREE(lte_array);
+ 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
+
+/* Given a nonempty run of consecutive lookup table entries with the
+ * PACKED_STREAMS flag set, count how many specify resources (as opposed to
+ * streams within those resources).
+ *
+ * Returns the resulting count. */
+static size_t
+count_subpacks(const struct wim_lookup_table_entry_disk *entries, size_t max)
+{
+ size_t count = 0;
+ do {
+ struct wim_reshdr reshdr;
+
+ get_wim_reshdr(&(entries++)->reshdr, &reshdr);
+
+ if (!(reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS)) {
+ /* Run was terminated by a stand-alone stream entry. */
+ break;
+ }
+
+ if (reshdr.uncompressed_size == WIM_PACK_MAGIC_NUMBER) {
+ /* This is a resource entry. */
+ count++;
+ }
+ } while (--max);
+ return count;
+}
+
+/* Given a run of consecutive lookup table entries with the PACKED_STREAMS flag
+ * set and having @num_subpacks resource entries, load resource information from
+ * them into the resource specifications in the @subpacks array.
+ *
+ * Returns 0 on success, or a nonzero error code on failure. */
+static int
+do_load_subpack_info(WIMStruct *wim, struct wim_resource_spec **subpacks,
+ size_t num_subpacks,
+ const struct wim_lookup_table_entry_disk *entries)
+{
+ for (size_t i = 0; i < num_subpacks; i++) {
+ struct wim_reshdr reshdr;
+ struct alt_chunk_table_header_disk hdr;
+ struct wim_resource_spec *rspec;
+ int ret;
+
+ /* Advance to next resource entry. */
+
+ do {
+ get_wim_reshdr(&(entries++)->reshdr, &reshdr);
+ } while (reshdr.uncompressed_size != WIM_PACK_MAGIC_NUMBER);
+
+ rspec = subpacks[i];
+
+ wim_res_hdr_to_spec(&reshdr, wim, rspec);
+
+ /* For packed resources, the uncompressed size, compression
+ * type, and chunk size are stored in the resource itself, not
+ * in the lookup table. */
+
+ ret = full_pread(&wim->in_fd, &hdr,
+ sizeof(hdr), reshdr.offset_in_wim);
+ if (ret) {
+ ERROR("Failed to read header of packed resource "
+ "(offset_in_wim=%"PRIu64")",
+ reshdr.offset_in_wim);
+ return ret;
+ }
+
+ rspec->uncompressed_size = le64_to_cpu(hdr.res_usize);
+
+ /* 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_XPRESS != 1);
+ BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_LZX != 2);
+ BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_LZMS != 3);
+ rspec->compression_type = le32_to_cpu(hdr.compression_format);
+
+ rspec->chunk_size = le32_to_cpu(hdr.chunk_size);
+
+ DEBUG("Subpack %zu/%zu: %"PRIu64" => %"PRIu64" "
+ "(%"TS"/%"PRIu32") @ +%"PRIu64"",
+ i + 1, num_subpacks,
+ rspec->uncompressed_size,
+ rspec->size_in_wim,
+ wimlib_get_compression_type_string(rspec->compression_type),
+ rspec->chunk_size,
+ rspec->offset_in_wim);
+
+ }
+ return 0;
+}
+
+/* Given a nonempty run of consecutive lookup table entries with the
+ * PACKED_STREAMS flag set, allocate a 'struct wim_resource_spec' for each
+ * resource within that run.
+ *
+ * Returns 0 on success, or a nonzero error code on failure.
+ * Returns the pointers and count in *subpacks_ret and *num_subpacks_ret.