4 * WIM files can optionally contain a table of SHA1 message digests at the end,
5 * one digest for each chunk of the file of some specified size (often 10 MB).
6 * This file implements the checking and writing of this table.
10 * Copyright (C) 2012, 2013 Eric Biggers
12 * This file is part of wimlib, a library for working with WIM files.
14 * wimlib is free software; you can redistribute it and/or modify it under the
15 * terms of the GNU General Public License as published by the Free
16 * Software Foundation; either version 3 of the License, or (at your option)
19 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
20 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
21 * A PARTICULAR PURPOSE. See the GNU General Public License for more
24 * You should have received a copy of the GNU General Public License
25 * along with wimlib; if not, see http://www.gnu.org/licenses/.
28 #include "wimlib_internal.h"
29 #include "buffer_io.h"
32 /* Size, in bytes, of each SHA1-summed chunk, when wimlib writes integrity
34 #define INTEGRITY_CHUNK_SIZE 10485760
36 /* Only use a different chunk size for compatiblity with an existing integrity
37 * table if the chunk size is between these two numbers. */
38 #define INTEGRITY_MIN_CHUNK_SIZE 4096
39 #define INTEGRITY_MAX_CHUNK_SIZE 134217728
41 struct integrity_table {
49 calculate_chunk_sha1(FILE *fp, size_t this_chunk_size,
50 off_t offset, u8 sha1_md[])
55 size_t bytes_remaining;
59 ret = fseeko(fp, offset, SEEK_SET);
61 ERROR_WITH_ERRNO("Can't seek to offset "
62 "%"PRIu64" in WIM", offset);
63 return WIMLIB_ERR_READ;
65 bytes_remaining = this_chunk_size;
68 bytes_to_read = min(bytes_remaining, sizeof(buf));
69 bytes_read = fread(buf, 1, bytes_to_read, fp);
70 if (bytes_read != bytes_to_read) {
72 ERROR("Unexpected EOF while calculating "
73 "integrity checksums");
75 ERROR_WITH_ERRNO("File stream error while "
76 "calculating integrity "
79 return WIMLIB_ERR_READ;
81 sha1_update(&ctx, buf, bytes_read);
82 bytes_remaining -= bytes_read;
83 } while (bytes_remaining);
84 sha1_final(sha1_md, &ctx);
90 * read_integrity_table: - Reads the integrity table from a WIM file.
93 * The resource entry that specifies the location of the integrity table.
94 * The integrity table must exist (i.e. res_entry->offset must not be 0).
97 * FILE * to the WIM file, opened for reading.
100 * Number of bytes of data that should be checked by the integrity table.
103 * On success, a pointer to an in-memory structure containing the integrity
104 * information is written to this location.
106 * Returns 0 on success; nonzero on failure. The possible error codes are:
108 * * WIMLIB_ERR_INVALID_INTEGRITY_TABLE: The integrity table is invalid.
109 * * WIMLIB_ERR_NOMEM: Could not allocate memory to store the integrity
111 * * WIMLIB_ERR_READ: Could not read the integrity data from the WIM file.
114 read_integrity_table(const struct resource_entry *res_entry,
116 u64 num_checked_bytes,
117 struct integrity_table **table_ret)
119 struct integrity_table *table = NULL;
122 u64 expected_num_entries;
124 if (resource_is_compressed(res_entry)) {
125 ERROR("Didn't expect a compressed integrity table");
126 return WIMLIB_ERR_INVALID_INTEGRITY_TABLE;
129 if (res_entry->size < 8 || res_entry->size > 0xffffffff) {
130 ERROR("Integrity table resource header is invalid");
131 return WIMLIB_ERR_INVALID_INTEGRITY_TABLE;
134 /* Read the integrity table into memory. */
135 if ((table = MALLOC(res_entry->size)) == NULL) {
136 ERROR("Can't allocate %"PRIu64" bytes for integrity table",
137 (u64)res_entry->size);
138 return WIMLIB_ERR_NOMEM;
141 ret = read_uncompressed_resource(fp, res_entry->offset,
142 res_entry->size, (void*)table);
145 ERROR("Failed to read integrity table (size = %u, "
146 " offset = %"PRIu64")",
147 (unsigned)res_entry->size, res_entry->offset);
151 table->size = le32_to_cpu(table->size);
152 table->num_entries = le32_to_cpu(table->num_entries);
153 table->chunk_size = le32_to_cpu(table->chunk_size);
155 if (table->size != res_entry->size) {
156 ERROR("Inconsistent integrity table sizes: Table header says "
157 "%u bytes but resource entry says %u bytes",
158 table->size, (unsigned)res_entry->size);
159 ret = WIMLIB_ERR_INVALID_INTEGRITY_TABLE;
163 DEBUG("table->size = %u, table->num_entries = %u, "
164 "table->chunk_size = %u",
165 table->size, table->num_entries, table->chunk_size);
167 expected_size = (u64)table->num_entries * SHA1_HASH_SIZE + 12;
169 if (table->size != expected_size) {
170 ERROR("Integrity table is %u bytes, but expected %"PRIu64" "
171 "bytes to hold %u entries",
172 table->size, expected_size, table->num_entries);
173 ret = WIMLIB_ERR_INVALID_INTEGRITY_TABLE;
177 if (table->chunk_size == 0) {
178 ERROR("Cannot use integrity chunk size of 0");
179 ret = WIMLIB_ERR_INVALID_INTEGRITY_TABLE;
183 expected_num_entries = DIV_ROUND_UP(num_checked_bytes, table->chunk_size);
185 if (table->num_entries != expected_num_entries) {
186 ERROR("%"PRIu64" integrity table entries would be required "
187 "to checksum the %"PRIu64" bytes from the end of the "
189 expected_num_entries, num_checked_bytes);
190 ERROR("end of the lookup table with a chunk size of %u, but "
191 "there were only %u entries",
192 table->chunk_size, table->num_entries);
193 ret = WIMLIB_ERR_INVALID_INTEGRITY_TABLE;
204 * calculate_integrity_table():
206 * Calculates an integrity table for the data in a file beginning at offset 208
207 * (WIM_HEADER_DISK_SIZE).
210 * FILE * for the file to be checked, opened for reading. Does not need to
211 * be at any specific location in the file.
214 * Offset of byte after the last byte to be checked.
217 * If non-NULL, a pointer to the table containing the previously calculated
218 * integrity data for a prefix of this file.
221 * If @old_table is non-NULL, the byte after the last byte that was checked
222 * in the old table. Must be less than or equal to new_check_end.
225 * If non-NULL, a progress function that will be called after every
228 * @integrity_table_ret:
229 * On success, a pointer to the calculated integrity table is written into
232 * Returns 0 on success; nonzero on failure.
235 calculate_integrity_table(FILE *fp,
237 const struct integrity_table *old_table,
239 wimlib_progress_func_t progress_func,
240 struct integrity_table **integrity_table_ret)
243 size_t chunk_size = INTEGRITY_CHUNK_SIZE;
245 /* If an old table is provided, set the chunk size to be compatible with
246 * the old chunk size, unless the old chunk size was weird. */
247 if (old_table != NULL) {
248 if (old_table->num_entries == 0 ||
249 old_table->chunk_size < INTEGRITY_MIN_CHUNK_SIZE ||
250 old_table->chunk_size > INTEGRITY_MAX_CHUNK_SIZE)
253 chunk_size = old_table->chunk_size;
257 u64 old_check_bytes = old_check_end - WIM_HEADER_DISK_SIZE;
258 u64 new_check_bytes = new_check_end - WIM_HEADER_DISK_SIZE;
260 u32 old_num_chunks = DIV_ROUND_UP(old_check_bytes, chunk_size);
261 u32 new_num_chunks = DIV_ROUND_UP(new_check_bytes, chunk_size);
263 size_t old_last_chunk_size = MODULO_NONZERO(old_check_bytes, chunk_size);
264 size_t new_last_chunk_size = MODULO_NONZERO(new_check_bytes, chunk_size);
266 size_t new_table_size = 12 + new_num_chunks * SHA1_HASH_SIZE;
268 struct integrity_table *new_table = MALLOC(new_table_size);
270 return WIMLIB_ERR_NOMEM;
271 new_table->num_entries = new_num_chunks;
272 new_table->size = new_table_size;
273 new_table->chunk_size = chunk_size;
275 u64 offset = WIM_HEADER_DISK_SIZE;
276 union wimlib_progress_info progress;
279 progress.integrity.total_bytes = new_check_bytes;
280 progress.integrity.total_chunks = new_num_chunks;
281 progress.integrity.completed_chunks = 0;
282 progress.integrity.completed_bytes = 0;
283 progress.integrity.chunk_size = chunk_size;
284 progress.integrity.filename = NULL;
285 progress_func(WIMLIB_PROGRESS_MSG_CALC_INTEGRITY,
289 for (u32 i = 0; i < new_num_chunks; i++) {
290 size_t this_chunk_size;
291 if (i == new_num_chunks - 1)
292 this_chunk_size = new_last_chunk_size;
294 this_chunk_size = chunk_size;
296 ((this_chunk_size == chunk_size && i < old_num_chunks - 1) ||
297 (i == old_num_chunks - 1 && this_chunk_size == old_last_chunk_size)))
299 /* Can use SHA1 message digest from old integrity table
301 copy_hash(new_table->sha1sums[i], old_table->sha1sums[i]);
303 /* Calculate the SHA1 message digest of this chunk */
304 ret = calculate_chunk_sha1(fp, this_chunk_size,
305 offset, new_table->sha1sums[i]);
309 offset += this_chunk_size;
311 progress.integrity.completed_chunks++;
312 progress.integrity.completed_bytes += this_chunk_size;
313 progress_func(WIMLIB_PROGRESS_MSG_CALC_INTEGRITY,
318 *integrity_table_ret = new_table;
325 * write_integrity_table():
327 * Writes a WIM integrity table (a list of SHA1 message digests of raw 10 MiB
328 * chunks of the file).
330 * This function can optionally re-use entries from an older integrity table.
331 * To do this, make @integrity_res_entry point to the resource entry for the
332 * older table (note: this is an input-output parameter), and set
333 * @old_lookup_table_end to the offset of the byte directly following the last
334 * byte checked by the old table. If the old integrity table is invalid or
335 * cannot be read, a warning is printed and the integrity information is
339 * FILE * to the WIM file, opened read-write, positioned at the location at
340 * which the integrity table is to be written.
342 * @integrity_res_entry:
343 * Resource entry which will be set to point to the integrity table on
344 * success. In addition, if @old_lookup_table_end != 0, this initially
345 * must point to the resource entry for the old integrity table for the
348 * @new_lookup_table_end:
349 * The offset of the byte directly following the lookup table in the WIM
352 * @old_lookup_table_end:
353 * If nonzero, the offset of the byte directly following the old lookup
357 * If non-NULL, a progress function that will be called after every
361 * 0 on success, nonzero on failure. The possible error codes are:
362 * * WIMLIB_ERR_WRITE: Could not write the integrity table.
363 * * WIMLIB_ERR_READ: Could not read a chunk of data that needed
367 write_integrity_table(FILE *fp,
368 struct resource_entry *integrity_res_entry,
369 off_t new_lookup_table_end,
370 off_t old_lookup_table_end,
371 wimlib_progress_func_t progress_func)
373 struct integrity_table *old_table;
374 struct integrity_table *new_table;
379 wimlib_assert(old_lookup_table_end <= new_lookup_table_end);
381 cur_offset = ftello(fp);
382 if (cur_offset == -1)
383 return WIMLIB_ERR_WRITE;
385 if (integrity_res_entry->offset == 0 || old_lookup_table_end == 0) {
388 ret = read_integrity_table(integrity_res_entry, fp,
389 old_lookup_table_end - WIM_HEADER_DISK_SIZE,
391 if (ret == WIMLIB_ERR_INVALID_INTEGRITY_TABLE) {
392 WARNING("Old integrity table is invalid! "
394 } else if (ret != 0) {
395 WARNING("Can't read old integrity table! "
400 ret = calculate_integrity_table(fp, new_lookup_table_end,
401 old_table, old_lookup_table_end,
402 progress_func, &new_table);
404 goto out_free_old_table;
406 new_table_size = new_table->size;
408 new_table->size = cpu_to_le32(new_table->size);
409 new_table->num_entries = cpu_to_le32(new_table->num_entries);
410 new_table->chunk_size = cpu_to_le32(new_table->chunk_size);
412 if (fseeko(fp, cur_offset, SEEK_SET) != 0) {
413 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" of WIM to "
414 "write integrity table", cur_offset);
415 ret = WIMLIB_ERR_WRITE;
416 goto out_free_new_table;
419 if (fwrite(new_table, 1, new_table_size, fp) != new_table_size) {
420 ERROR_WITH_ERRNO("Failed to write WIM integrity table");
421 ret = WIMLIB_ERR_WRITE;
423 integrity_res_entry->offset = cur_offset;
424 integrity_res_entry->size = new_table_size;
425 integrity_res_entry->original_size = new_table_size;
426 integrity_res_entry->flags = 0;
437 * verify_integrity():
439 * Checks a WIM for consistency with the integrity table.
442 * FILE * to the WIM file, opened for reading.
445 * The integrity table for the WIM, read into memory.
448 * Number of bytes in the WIM that need to be checked (offset of end of the
449 * lookup table minus offset of end of the header).
452 * If non-NULL, a progress function that will be called after every
456 * > 0 (WIMLIB_ERR_*) on error
457 * 0 (WIM_INTEGRITY_OK) if the integrity was checked successfully and there
458 * were no inconsistencies.
459 * -1 (WIM_INTEGRITY_NOT_OK) if the WIM failed the integrity check.
462 verify_integrity(FILE *fp, const tchar *filename,
463 const struct integrity_table *table,
465 wimlib_progress_func_t progress_func)
468 u64 offset = WIM_HEADER_DISK_SIZE;
469 u8 sha1_md[SHA1_HASH_SIZE];
470 union wimlib_progress_info progress;
473 progress.integrity.total_bytes = bytes_to_check;
474 progress.integrity.total_chunks = table->num_entries;
475 progress.integrity.completed_chunks = 0;
476 progress.integrity.completed_bytes = 0;
477 progress.integrity.chunk_size = table->chunk_size;
478 progress.integrity.filename = filename;
479 progress_func(WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY,
482 for (u32 i = 0; i < table->num_entries; i++) {
483 size_t this_chunk_size;
484 if (i == table->num_entries - 1)
485 this_chunk_size = MODULO_NONZERO(bytes_to_check,
488 this_chunk_size = table->chunk_size;
490 ret = calculate_chunk_sha1(fp, this_chunk_size, offset, sha1_md);
494 if (!hashes_equal(sha1_md, table->sha1sums[i]))
495 return WIM_INTEGRITY_NOT_OK;
497 offset += this_chunk_size;
499 progress.integrity.completed_chunks++;
500 progress.integrity.completed_bytes += this_chunk_size;
501 progress_func(WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY,
505 return WIM_INTEGRITY_OK;
510 * check_wim_integrity():
512 * Verifies the integrity of the WIM by making sure the SHA1 message digests of
513 * ~10 MiB chunks of the WIM match up with the values given in the integrity
517 * The WIM, opened for reading, and with the header already read.
520 * If non-NULL, a progress function that will be called after every
524 * > 0 (WIMLIB_ERR_*) on error
525 * 0 (WIM_INTEGRITY_OK) if the integrity was checked successfully and there
526 * were no inconsistencies.
527 * -1 (WIM_INTEGRITY_NOT_OK) if the WIM failed the integrity check.
528 * -2 (WIM_INTEGRITY_NONEXISTENT) if the WIM contains no integrity
532 check_wim_integrity(WIMStruct *w, wimlib_progress_func_t progress_func)
536 struct integrity_table *table;
537 u64 end_lookup_table_offset;
539 if (w->hdr.integrity.offset == 0) {
540 DEBUG("No integrity information.");
541 return WIM_INTEGRITY_NONEXISTENT;
544 end_lookup_table_offset = w->hdr.lookup_table_res_entry.offset +
545 w->hdr.lookup_table_res_entry.size;
547 if (end_lookup_table_offset < WIM_HEADER_DISK_SIZE) {
548 ERROR("WIM lookup table ends before WIM header ends!");
549 return WIMLIB_ERR_INVALID_INTEGRITY_TABLE;
552 bytes_to_check = end_lookup_table_offset - WIM_HEADER_DISK_SIZE;
554 ret = read_integrity_table(&w->hdr.integrity, w->fp,
555 bytes_to_check, &table);
558 ret = verify_integrity(w->fp, w->filename, table,
559 bytes_to_check, progress_func);