+/*
+ * write_integrity_table():
+ *
+ * Writes a WIM integrity table (a list of SHA1 message digests of raw 10 MiB
+ * chunks of the file).
+ *
+ * This function can optionally re-use entries from an older integrity table.
+ * To do this, ensure that @wim->hdr.integrity_table_reshdr is the resource
+ * header for the older table (note: this is an input-output parameter), and set
+ * @old_lookup_table_end to the offset of the byte directly following the last
+ * byte checked by the old table. If the old integrity table is invalid or
+ * cannot be read, a warning is printed and the integrity information is
+ * re-calculated.
+ *
+ * @wim:
+ * WIMStruct for the WIM file. @wim->out_fd must be a seekable descriptor
+ * to the new WIM file, opened read-write, positioned at the location at
+ * which the integrity table is to be written. Furthermore,
+ * @wim->hdr.integrity is expected to be a resource entry which will be set
+ * to the integrity table information on success. In addition, if
+ * @old_lookup_table_end != 0, @wim->hdr.integrity must initially contain
+ * information about the old integrity table, and @wim->in_fd must be a
+ * seekable descriptor to the original WIM file opened for reading.
+ *
+ * @new_lookup_table_end:
+ * The offset of the byte directly following the lookup table in the WIM
+ * being written.
+ *
+ * @old_lookup_table_end:
+ * If nonzero, the offset of the byte directly following the old lookup
+ * table in the WIM.
+ *
+ * Return values:
+ * WIMLIB_ERR_SUCCESS (0)
+ * WIMLIB_ERR_NOMEM
+ * WIMLIB_ERR_UNEXPECTED_END_OF_FILE
+ * WIMLIB_ERR_WRITE
+ */
+int
+write_integrity_table(WIMStruct *wim,
+ off_t new_lookup_table_end,
+ off_t old_lookup_table_end)
+{
+ struct integrity_table *old_table;
+ struct integrity_table *new_table;
+ int ret;
+ u32 new_table_size;
+
+ DEBUG("Writing integrity table "
+ "(new_lookup_table_end=%"PRIu64", old_lookup_table_end=%"PRIu64")",
+ new_lookup_table_end, old_lookup_table_end);
+
+ wimlib_assert(old_lookup_table_end <= new_lookup_table_end);
+
+ old_table = NULL;
+ if (wim_has_integrity_table(wim) && old_lookup_table_end != 0) {
+ ret = read_integrity_table(wim,
+ old_lookup_table_end - WIM_HEADER_DISK_SIZE,
+ &old_table);
+ if (ret == WIMLIB_ERR_INVALID_INTEGRITY_TABLE) {
+ WARNING("Old integrity table is invalid! "
+ "Ignoring it");
+ } else if (ret != 0) {
+ WARNING("Can't read old integrity table! "
+ "Ignoring it");
+ }