+int
+read_wim_lookup_table(WIMStruct *wim)
+{
+ int ret;
+ size_t num_entries;
+ void *buf = NULL;
+ struct wim_lookup_table *table = NULL;
+ struct wim_lookup_table_entry *cur_entry = NULL;
+ size_t num_duplicate_entries = 0;
+ size_t num_wrong_part_entries = 0;
+ u32 image_index = 0;
+ struct wim_resource_spec **cur_subpacks = NULL;
+ size_t cur_num_subpacks = 0;
+
+ 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);
+
+ /* Calculate the 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)
+ goto oom;
+
+ /* Allocate and initalize stream entries ('struct
+ * wim_lookup_table_entry's) from the raw lookup table buffer. Each of
+ * these entries will point to a 'struct wim_resource_spec' that
+ * describes the underlying resource. In WIMs with version number
+ * WIM_VERSION_PACKED_STREAMS, a resource may contain multiple streams.
+ */
+ for (size_t i = 0; i < num_entries; i++) {
+ const struct wim_lookup_table_entry_disk *disk_entry =
+ &((const struct wim_lookup_table_entry_disk*)buf)[i];
+ struct wim_reshdr reshdr;
+ u16 part_number;
+
+ /* Get the resource header */
+ 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);
+
+ /* Ignore PACKED_STREAMS flag if it isn't supposed to be used in
+ * this WIM version. */
+ if (wim->hdr.wim_version == WIM_VERSION_DEFAULT)
+ reshdr.flags &= ~WIM_RESHDR_FLAG_PACKED_STREAMS;
+
+ /* Allocate a new 'struct wim_lookup_table_entry'. */
+ cur_entry = new_lookup_table_entry();
+ if (!cur_entry)
+ goto oom;
+
+ /* Get the part number, reference count, and hash. */
+ 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 (reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
+
+ /* PACKED_STREAMS entry */
+
+ if (!cur_subpacks) {
+ /* Starting new run */
+ ret = load_subpack_info(wim, disk_entry,
+ num_entries - i,
+ &cur_subpacks,
+ &cur_num_subpacks);
+ if (ret)
+ goto out;
+ }
+
+ if (reshdr.uncompressed_size == WIM_PACK_MAGIC_NUMBER) {
+ /* Resource entry, not stream entry */
+ goto free_cur_entry_and_continue;
+ }
+
+ /* Stream entry */
+
+ ret = bind_stream_to_subpack(&reshdr,
+ cur_entry,
+ cur_subpacks,
+ cur_num_subpacks);
+ if (ret)
+ goto out;
+
+ } else {
+ /* Normal stream/resource entry; PACKED_STREAMS not set.
+ */
+
+ struct wim_resource_spec *rspec;
+
+ if (unlikely(cur_subpacks)) {
+ /* This entry terminated a packed run. */
+ ret = finish_subpacks(cur_subpacks,
+ cur_num_subpacks);
+ cur_subpacks = NULL;
+ if (ret)
+ goto out;
+ }
+
+ /* How to handle an uncompressed resource with its
+ * uncompressed size different from its compressed size?
+ *
+ * Based on a simple test, WIMGAPI seems to handle this
+ * as follows:
+ *
+ * if (size_in_wim > uncompressed_size) {
+ * Ignore uncompressed_size; use size_in_wim
+ * instead.
+ * } else {
+ * Honor uncompressed_size, but treat the part of
+ * the file data above size_in_wim as all zeros.
+ * }
+ *
+ * So we will do the same. */
+ if (unlikely(!(reshdr.flags &
+ WIM_RESHDR_FLAG_COMPRESSED) &&
+ (reshdr.size_in_wim >
+ reshdr.uncompressed_size)))
+ {
+ reshdr.uncompressed_size = reshdr.size_in_wim;
+ }
+
+ /* Set up a resource specification for this stream. */
+
+ rspec = MALLOC(sizeof(struct wim_resource_spec));
+ if (!rspec)
+ goto oom;
+
+ wim_res_hdr_to_spec(&reshdr, wim, rspec);
+
+ cur_entry->offset_in_res = 0;
+ cur_entry->size = reshdr.uncompressed_size;
+ cur_entry->flags = reshdr.flags;
+
+ lte_bind_wim_resource_spec(cur_entry, rspec);