+/* 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
+
+static int
+cmp_streams_by_offset_in_res(const void *p1, const void *p2)
+{
+ const struct wim_lookup_table_entry *lte1, *lte2;
+
+ lte1 = *(const struct wim_lookup_table_entry**)p1;
+ lte2 = *(const struct wim_lookup_table_entry**)p2;
+
+ return cmp_u64(lte1->offset_in_res, lte2->offset_in_res);
+}
+
+/* Validate the size and location of a WIM resource. */
+static int
+validate_resource(struct wim_resource_spec *rspec)
+{
+ struct wim_lookup_table_entry *lte;
+ bool out_of_order;
+ u64 expected_next_offset;
+ int ret;
+
+ /* Verify that the resource itself has a valid offset and size. */
+ if (rspec->offset_in_wim + rspec->size_in_wim < rspec->size_in_wim)
+ goto invalid_due_to_overflow;
+
+ /* Verify that each stream in the resource has a valid offset and size.
+ */
+ expected_next_offset = 0;
+ out_of_order = false;
+ 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)
+ goto invalid_due_to_overflow;
+
+ if (lte->offset_in_res >= expected_next_offset)
+ expected_next_offset = lte->offset_in_res + lte->size;
+ else
+ out_of_order = true;
+ }
+
+ /* If the streams were not located at strictly increasing positions (not
+ * allowing for overlap), sort them. Then make sure that none overlap.
+ */
+ if (out_of_order) {
+ ret = sort_stream_list(&rspec->stream_list,
+ offsetof(struct wim_lookup_table_entry,
+ rspec_node),
+ cmp_streams_by_offset_in_res);
+ if (ret)
+ return ret;
+
+ expected_next_offset = 0;
+ list_for_each_entry(lte, &rspec->stream_list, rspec_node) {
+ if (lte->offset_in_res >= expected_next_offset)
+ expected_next_offset = lte->offset_in_res + lte->size;
+ else
+ goto invalid_due_to_overlap;
+ }
+ }
+
+ return 0;
+
+invalid_due_to_overflow:
+ ERROR("Invalid resource entry (offset overflow)");
+ return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
+
+invalid_due_to_overlap:
+ ERROR("Invalid resource entry (streams in packed resource overlap)");
+ return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
+}
+
+/* Validate the resource, or free it if unused. */
+static int
+finish_resource(struct wim_resource_spec *rspec)
+{
+ if (!list_empty(&rspec->stream_list)) {
+ /* This resource contains at least one stream. */
+ return validate_resource(rspec);
+ } else {
+ /* No streams are in this resource. Get rid of it. */
+ FREE(rspec);
+ return 0;
+ }
+}
+