+ 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;
+ u64 prev_uncompressed_size;
+ u64 logical_offset;
+
+ table_size = 0;
+ 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 == NULL) {
+ ERROR("Failed to allocate %zu bytes for temporary lookup table",
+ table_size);
+ return WIMLIB_ERR_NOMEM;
+ }
+ table_buf_ptr = table_buf;
+
+ prev_res_offset_in_wim = ~0ULL;
+ prev_uncompressed_size = 0;
+ logical_offset = 0;
+ list_for_each_entry(lte, stream_list, lookup_table_list) {
+ if (lte->out_reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
+ struct wim_reshdr tmp_reshdr;
+
+ /* Eww. When WIMGAPI sees multiple resource packs, it
+ * expects the offsets to be adjusted as if there were
+ * really only one pack. */
+
+ if (lte->out_res_offset_in_wim != prev_res_offset_in_wim) {
+ /* Put the resource entry for pack */
+ tmp_reshdr.offset_in_wim = lte->out_res_offset_in_wim;
+ tmp_reshdr.size_in_wim = lte->out_res_size_in_wim;
+ tmp_reshdr.uncompressed_size = WIM_PACK_MAGIC_NUMBER;
+ tmp_reshdr.flags = WIM_RESHDR_FLAG_PACKED_STREAMS;
+
+ put_wim_lookup_table_entry(table_buf_ptr++,
+ &tmp_reshdr,
+ part_number,
+ 1, zero_hash);
+
+ logical_offset += prev_uncompressed_size;
+
+ prev_res_offset_in_wim = lte->out_res_offset_in_wim;
+ prev_uncompressed_size = lte->out_res_uncompressed_size;
+ }
+ tmp_reshdr = lte->out_reshdr;
+ tmp_reshdr.offset_in_wim += logical_offset;
+ put_wim_lookup_table_entry(table_buf_ptr++,
+ &tmp_reshdr,
+ part_number,
+ lte->out_refcnt,
+ lte->hash);
+ } else {
+ put_wim_lookup_table_entry(table_buf_ptr++,
+ <e->out_reshdr,
+ part_number,
+ lte->out_refcnt,
+ lte->hash);
+ }
+
+ }
+ 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;