]> wimlib.net Git - wimlib/blobdiff - src/integrity.c
Rework WIM writing code
[wimlib] / src / integrity.c
index 70ff7ffe991a92f0c561e14d78b466ac1269fcc2..5ad55a5bc62a30a4a2797b082ef2df8a6f1d9118 100644 (file)
 #define INTEGRITY_CHUNK_SIZE 10485760
 
 /*
- * Verifies the integrity of a WIM. 
+ * Verifies the integrity of a WIM.
  *
- * @fp:                   FILE* of the WIM, currently positioned at the end of the header. 
+ * @fp:                   FILE* of the WIM, currently positioned at the end of the header.
  * @num_bytes:    Number of bytes to verify the integrity of.
  * @chunk_size:           Chunk size per SHA1 message digest.
  * @sha1sums:     Array of SHA1 message digests; 20 bytes each, one per chunk.
  * @show_progress: Nonzero if the percent complete is to be printed after every
  *                     chunk.
- * @status:       On success, set to WIM_INTEGRITY_OK or WIM_INTEGRITY_NOT_OK 
- *                     based on whether the WIM is intact or not.
  */
-static int verify_integrity(FILE *fp, u64 num_bytes, u32 chunk_size, 
-                           const u8 *sha1sums, int show_progress,
-                           int *status)
+static int verify_integrity(FILE *fp, u64 num_bytes, u32 chunk_size,
+                           const u8 *sha1sums, int show_progress)
 {
        u8    *chunk_buf;
        u8     resblock[SHA1_HASH_SIZE];
@@ -65,10 +62,10 @@ static int verify_integrity(FILE *fp, u64 num_bytes, u32 chunk_size,
        bytes_remaining = num_bytes;
        while (bytes_remaining != 0) {
                if (show_progress) {
-                       percent_done = (num_bytes - bytes_remaining) * 100 / 
+                       percent_done = (num_bytes - bytes_remaining) * 100 /
                                        num_bytes;
                        printf("Verifying integrity of WIM (%"PRIu64" bytes "
-                                       "remaining, %u%% done)       \r", 
+                                       "remaining, %u%% done)       \r",
                                        bytes_remaining, percent_done);
                        fflush(stdout);
                }
@@ -82,20 +79,18 @@ static int verify_integrity(FILE *fp, u64 num_bytes, u32 chunk_size,
                                                 "verifying integrity of WIM");
                        }
                        ret = WIMLIB_ERR_READ;
-                       goto verify_integrity_error;
+                       goto out;
                }
                sha1_buffer(chunk_buf, bytes_to_read, resblock);
                if (!hashes_equal(resblock, sha1sums)) {
-                       *status = WIM_INTEGRITY_NOT_OK;
-                       goto verify_integrity_done;
+                       ret = WIM_INTEGRITY_NOT_OK;
+                       goto out;
                }
                sha1sums += SHA1_HASH_SIZE;
                bytes_remaining -= bytes_to_read;
        }
-       *status = WIM_INTEGRITY_OK;
-verify_integrity_done:
-       ret = 0;
-verify_integrity_error:
+       ret = WIM_INTEGRITY_OK;
+out:
        FREE(chunk_buf);
        if (show_progress)
                putchar('\n');
@@ -103,17 +98,9 @@ verify_integrity_error:
 }
 
 /*
- * Verifies the integrity of the WIM. 
- *
- * @show_progress: Nonzero if the percent complete is to be printed after every
- *                     chunk.
- * @status:       On success, set to WIM_INTEGRITY_OK, WIM_INTEGRITY_NOT_OK,
- *                     or WIM_INTEGRITY_NONEXISTENT.
- *
- * Returns: 0, WIMLIB_ERR_INVALID_INTEGRITY_TABLE, WIMLIB_ERR_NOMEM, or
- * WIMLIB_ERR_READ.  If nonzero, the boolean pointed to by @ok is not changed.
+ * Verifies the integrity of the WIM.
  */
-int check_wim_integrity(WIMStruct *w, int show_progress, int *status)
+int check_wim_integrity(WIMStruct *w, int show_progress)
 {
 
        struct resource_entry *res_entry;
@@ -131,8 +118,7 @@ int check_wim_integrity(WIMStruct *w, int show_progress, int *status)
        res_entry = &w->hdr.integrity;
        if (res_entry->size == 0) {
                DEBUG("No integrity information.");
-               *status = WIM_INTEGRITY_NONEXISTENT;
-               return 0;
+               return WIM_INTEGRITY_NONEXISTENT;
        }
        if (res_entry->original_size < 12) {
                ERROR("Integrity table is too short");
@@ -144,10 +130,12 @@ int check_wim_integrity(WIMStruct *w, int show_progress, int *status)
        }
 
        /* Read the integrity table into memory. */
-       buf = MALLOC(res_entry->original_size);
-       if (!buf) {
+       if ((sizeof(size_t) < sizeof(u64)
+           && res_entry->original_size > ~(size_t)0)
+           || ((buf = MALLOC(res_entry->original_size)) == NULL))
+       {
                ERROR("Out of memory (needed %zu bytes for integrity table)",
-                     res_entry->original_size);
+                     (size_t)res_entry->original_size);
                ret = WIMLIB_ERR_NOMEM;
                goto out;
        }
@@ -186,7 +174,7 @@ int check_wim_integrity(WIMStruct *w, int show_progress, int *status)
 
        if (integrity_table_size != expected_size) {
                ERROR("Integrity table is %u bytes, but expected %"PRIu64" "
-                     "bytes to hold %u entries", 
+                     "bytes to hold %u entries",
                      integrity_table_size, expected_size, num_entries);
                ret = WIMLIB_ERR_INVALID_INTEGRITY_TABLE;
                goto out;
@@ -201,6 +189,12 @@ int check_wim_integrity(WIMStruct *w, int show_progress, int *status)
        end_lookup_table_offset = w->hdr.lookup_table_res_entry.offset +
                                  w->hdr.lookup_table_res_entry.size;
 
+       if (end_lookup_table_offset < WIM_HEADER_DISK_SIZE) {
+               ERROR("WIM lookup table ends before WIM header ends???");
+               ret = WIMLIB_ERR_INVALID_INTEGRITY_TABLE;
+               goto out;
+       }
+
        bytes_to_check = end_lookup_table_offset - WIM_HEADER_DISK_SIZE;
 
        expected_num_entries = (bytes_to_check + chunk_size - 1) / chunk_size;
@@ -226,36 +220,42 @@ int check_wim_integrity(WIMStruct *w, int show_progress, int *status)
        }
        /* call verify_integrity(), which does the actual checking of the SHA1
         * message digests. */
-       ret = verify_integrity(w->fp, bytes_to_check, chunk_size, p, 
-                              show_progress, status);
+       ret = verify_integrity(w->fp, bytes_to_check, chunk_size, p,
+                              show_progress);
 out:
        FREE(buf);
        return ret;
 }
 
-/* 
+/*
  * Writes integrity information to the output stream for a WIM file being
- * written. 
+ * written.
  *
  * @end_header_offset is the offset of the byte after the header, which is the
  *     beginning of the region that is checksummed.
  *
  * @end_lookup_table_offset is the offset of the byte after the lookup table,
- *     which is the end of the region that is checksummed. 
+ *     which is the end of the region that is checksummed.
  */
-int write_integrity_table(FILE *out, u64 end_header_offset, 
-                         u64 end_lookup_table_offset, int show_progress)
+int write_integrity_table(FILE *out, u64 end_header_offset,
+                         u64 end_lookup_table_offset, int show_progress,
+                         struct resource_entry *out_res_entry)
 {
-       u64   bytes_to_check;
-       u64   bytes_remaining;
-       u8   *buf;
-       u8   *p;
-       u8   *chunk_buf;
-       u32   num_entries;
-       u32   integrity_table_size;
-       int   ret;
-
-       DEBUG("Writing integrity table");
+       u64  bytes_to_check;
+       u64  bytes_remaining;
+       u8  *buf;
+       u8  *p;
+       u8  *chunk_buf;
+       u32  num_entries;
+       u32  integrity_table_size;
+       int  ret;
+       off_t start_offset;
+
+       start_offset = ftello(out);
+       if (start_offset == -1)
+               return WIMLIB_ERR_WRITE;
+
+       DEBUG("Calculating integrity table");
        if (fseeko(out, end_header_offset, SEEK_SET) != 0) {
                ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" of WIM to "
                                 "calculate integrity data", end_header_offset);
@@ -263,12 +263,11 @@ int write_integrity_table(FILE *out, u64 end_header_offset,
        }
 
        bytes_to_check = end_lookup_table_offset - end_header_offset;
-       num_entries = bytes_to_check / INTEGRITY_CHUNK_SIZE +
-                       (bytes_to_check % INTEGRITY_CHUNK_SIZE != 0);
+       num_entries = (bytes_to_check + INTEGRITY_CHUNK_SIZE - 1) /
+                       INTEGRITY_CHUNK_SIZE;
        integrity_table_size = num_entries * SHA1_HASH_SIZE + 3 * sizeof(u32);
 
-       DEBUG("integrity table size = %u", integrity_table_size);
-
+       DEBUG("integrity_table_size = %u", integrity_table_size);
 
        buf = MALLOC(integrity_table_size);
        if (!buf) {
@@ -286,7 +285,7 @@ int write_integrity_table(FILE *out, u64 end_header_offset,
                ERROR("Failed to allocate %u bytes for integrity chunk buffer",
                      INTEGRITY_CHUNK_SIZE);
                ret = WIMLIB_ERR_NOMEM;
-               goto err2;
+               goto out_free_buf;
        }
 
        bytes_remaining = bytes_to_check;
@@ -295,13 +294,13 @@ int write_integrity_table(FILE *out, u64 end_header_offset,
 
        while (bytes_remaining != 0) {
 
-               uint percent_done = (bytes_to_check - bytes_remaining) * 
+               uint percent_done = (bytes_to_check - bytes_remaining) *
                                    100 / bytes_to_check;
 
                if (show_progress) {
                        printf("Calculating integrity checksums for WIM "
                                        "(%"PRIu64" bytes remaining, %u%% "
-                                       "done)      \r", 
+                                       "done)      \r",
                                        bytes_remaining, percent_done);
                        fflush(stdout);
                }
@@ -319,7 +318,7 @@ int write_integrity_table(FILE *out, u64 end_header_offset,
                                                 "checksums");
                        }
                        ret = WIMLIB_ERR_READ;
-                       goto err2;
+                       goto out_free_chunk_buf;
                }
                sha1_buffer(chunk_buf, bytes_read, p);
                p += SHA1_HASH_SIZE;
@@ -330,23 +329,27 @@ int write_integrity_table(FILE *out, u64 end_header_offset,
                                "(0 bytes remaining, 100% done)"
                                "                       ");
 
-       if (fseeko(out, 0, SEEK_END) != 0) {
-               ERROR_WITH_ERRNO("Failed to seek to end of WIM to write "
-                                "integrity table");
+       if (fseeko(out, start_offset, SEEK_SET) != 0) {
+               ERROR_WITH_ERRNO("Failed to seek to end of WIM");
                ret = WIMLIB_ERR_WRITE;
-               goto err1;
+               goto out_free_chunk_buf;
        }
 
        if (fwrite(buf, 1, integrity_table_size, out) != integrity_table_size) {
                ERROR_WITH_ERRNO("Failed to write integrity table to end of "
                                 "WIM");
                ret = WIMLIB_ERR_WRITE;
-               goto err1;
+               goto out_free_chunk_buf;
        }
+
+       out_res_entry->offset        = start_offset;
+       out_res_entry->size          = integrity_table_size;
+       out_res_entry->original_size = integrity_table_size;
+       out_res_entry->flags         = 0;
        ret = 0;
-err1:
+out_free_chunk_buf:
        FREE(chunk_buf);
-err2:
+out_free_buf:
        FREE(buf);
        return ret;
 }