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/.
32 #include "wimlib/assert.h"
33 #include "wimlib/endianness.h"
34 #include "wimlib/error.h"
35 #include "wimlib/file_io.h"
36 #include "wimlib/integrity.h"
37 #include "wimlib/progress.h"
38 #include "wimlib/resource.h"
39 #include "wimlib/sha1.h"
40 #include "wimlib/wim.h"
41 #include "wimlib/write.h"
43 /* Size, in bytes, of each SHA1-summed chunk, when wimlib writes integrity
45 #define INTEGRITY_CHUNK_SIZE 10485760
47 /* Only use a different chunk size for compatiblity with an existing integrity
48 * table if the chunk size is between these two numbers. */
49 #define INTEGRITY_MIN_CHUNK_SIZE 4096
50 #define INTEGRITY_MAX_CHUNK_SIZE 134217728
52 struct integrity_table {
60 calculate_chunk_sha1(struct filedes *in_fd, size_t this_chunk_size,
61 off_t offset, u8 sha1_md[])
65 size_t bytes_remaining;
69 bytes_remaining = this_chunk_size;
72 bytes_to_read = min(bytes_remaining, sizeof(buf));
73 ret = full_pread(in_fd, buf, bytes_to_read, offset);
75 ERROR_WITH_ERRNO("Read error while calculating "
76 "integrity checksums");
79 sha1_update(&ctx, buf, bytes_to_read);
80 bytes_remaining -= bytes_to_read;
81 offset += bytes_to_read;
82 } while (bytes_remaining);
83 sha1_final(sha1_md, &ctx);
89 * read_integrity_table: - Reads the integrity table from a WIM file.
92 * WIMStruct for the WIM file; @wim->hdr.integrity_table_reshdr specifies
93 * the location of the integrity table. @wim->in_fd is expected to be a
94 * seekable file descriptor to the WIM file opened for reading.
97 * Number of bytes of data that should be checked by the integrity table.
100 * On success, a pointer to an in-memory structure containing the integrity
101 * information is written to this location.
104 * WIMLIB_ERR_SUCCESS (0)
105 * WIMLIB_ERR_INVALID_INTEGRITY_TABLE
108 * WIMLIB_ERR_UNEXPECTED_END_OF_FILE
111 read_integrity_table(WIMStruct *wim, u64 num_checked_bytes,
112 struct integrity_table **table_ret)
115 struct integrity_table *table;
118 if (wim->hdr.integrity_table_reshdr.uncompressed_size < 8)
121 DEBUG("Reading integrity table.");
123 ret = wim_reshdr_to_data(&wim->hdr.integrity_table_reshdr, wim, &buf);
128 table->size = le32_to_cpu(table->size);
129 table->num_entries = le32_to_cpu(table->num_entries);
130 table->chunk_size = le32_to_cpu(table->chunk_size);
132 DEBUG("table->size = %u, table->num_entries = %u, "
133 "table->chunk_size = %u",
134 table->size, table->num_entries, table->chunk_size);
136 if (table->size != wim->hdr.integrity_table_reshdr.uncompressed_size ||
137 table->size != (u64)table->num_entries * SHA1_HASH_SIZE + 12 ||
138 table->chunk_size == 0 ||
139 table->num_entries != DIV_ROUND_UP(num_checked_bytes, table->chunk_size))
149 ERROR("Integrity table is invalid");
150 return WIMLIB_ERR_INVALID_INTEGRITY_TABLE;
154 * calculate_integrity_table():
156 * Calculates an integrity table for the data in a file beginning at offset 208
157 * (WIM_HEADER_DISK_SIZE).
160 * File descriptor for the file to be checked, opened for reading. Does
161 * not need to be at any specific location in the file.
164 * Offset of byte after the last byte to be checked.
167 * If non-NULL, a pointer to the table containing the previously calculated
168 * integrity data for a prefix of this file.
171 * If @old_table is non-NULL, the byte after the last byte that was checked
172 * in the old table. Must be less than or equal to new_check_end.
174 * @integrity_table_ret:
175 * On success, a pointer to the calculated integrity table is written into
179 * WIMLIB_ERR_SUCCESS (0)
182 * WIMLIB_ERR_UNEXPECTED_END_OF_FILE
185 calculate_integrity_table(struct filedes *in_fd,
187 const struct integrity_table *old_table,
189 struct integrity_table **integrity_table_ret,
190 wimlib_progress_func_t progfunc,
194 size_t chunk_size = INTEGRITY_CHUNK_SIZE;
196 /* If an old table is provided, set the chunk size to be compatible with
197 * the old chunk size, unless the old chunk size was weird. */
198 if (old_table != NULL) {
199 if (old_table->num_entries == 0 ||
200 old_table->chunk_size < INTEGRITY_MIN_CHUNK_SIZE ||
201 old_table->chunk_size > INTEGRITY_MAX_CHUNK_SIZE)
204 chunk_size = old_table->chunk_size;
208 u64 old_check_bytes = old_check_end - WIM_HEADER_DISK_SIZE;
209 u64 new_check_bytes = new_check_end - WIM_HEADER_DISK_SIZE;
211 u32 old_num_chunks = DIV_ROUND_UP(old_check_bytes, chunk_size);
212 u32 new_num_chunks = DIV_ROUND_UP(new_check_bytes, chunk_size);
214 size_t old_last_chunk_size = MODULO_NONZERO(old_check_bytes, chunk_size);
215 size_t new_last_chunk_size = MODULO_NONZERO(new_check_bytes, chunk_size);
217 size_t new_table_size = 12 + new_num_chunks * SHA1_HASH_SIZE;
219 struct integrity_table *new_table = MALLOC(new_table_size);
221 return WIMLIB_ERR_NOMEM;
222 new_table->num_entries = new_num_chunks;
223 new_table->size = new_table_size;
224 new_table->chunk_size = chunk_size;
226 u64 offset = WIM_HEADER_DISK_SIZE;
227 union wimlib_progress_info progress;
229 progress.integrity.total_bytes = new_check_bytes;
230 progress.integrity.total_chunks = new_num_chunks;
231 progress.integrity.completed_chunks = 0;
232 progress.integrity.completed_bytes = 0;
233 progress.integrity.chunk_size = chunk_size;
234 progress.integrity.filename = NULL;
236 ret = call_progress(progfunc, WIMLIB_PROGRESS_MSG_CALC_INTEGRITY,
239 goto out_free_new_table;
241 for (u32 i = 0; i < new_num_chunks; i++) {
242 size_t this_chunk_size;
243 if (i == new_num_chunks - 1)
244 this_chunk_size = new_last_chunk_size;
246 this_chunk_size = chunk_size;
248 ((this_chunk_size == chunk_size && i < old_num_chunks - 1) ||
249 (i == old_num_chunks - 1 && this_chunk_size == old_last_chunk_size)))
251 /* Can use SHA1 message digest from old integrity table
253 copy_hash(new_table->sha1sums[i], old_table->sha1sums[i]);
255 /* Calculate the SHA1 message digest of this chunk */
256 ret = calculate_chunk_sha1(in_fd, this_chunk_size,
257 offset, new_table->sha1sums[i]);
259 goto out_free_new_table;
261 offset += this_chunk_size;
263 progress.integrity.completed_chunks++;
264 progress.integrity.completed_bytes += this_chunk_size;
265 ret = call_progress(progfunc, WIMLIB_PROGRESS_MSG_CALC_INTEGRITY,
268 goto out_free_new_table;
270 *integrity_table_ret = new_table;
279 * write_integrity_table():
281 * Writes a WIM integrity table (a list of SHA1 message digests of raw 10 MiB
282 * chunks of the file).
284 * This function can optionally re-use entries from an older integrity table.
285 * To do this, ensure that @wim->hdr.integrity_table_reshdr is the resource
286 * header for the older table (note: this is an input-output parameter), and set
287 * @old_lookup_table_end to the offset of the byte directly following the last
288 * byte checked by the old table. If the old integrity table is invalid or
289 * cannot be read, a warning is printed and the integrity information is
293 * WIMStruct for the WIM file. @wim->out_fd must be a seekable descriptor
294 * to the new WIM file, opened read-write, positioned at the location at
295 * which the integrity table is to be written. Furthermore,
296 * @wim->hdr.integrity is expected to be a resource entry which will be set
297 * to the integrity table information on success. In addition, if
298 * @old_lookup_table_end != 0, @wim->hdr.integrity must initially contain
299 * information about the old integrity table, and @wim->in_fd must be a
300 * seekable descriptor to the original WIM file opened for reading.
302 * @new_lookup_table_end:
303 * The offset of the byte directly following the lookup table in the WIM
306 * @old_lookup_table_end:
307 * If nonzero, the offset of the byte directly following the old lookup
311 * WIMLIB_ERR_SUCCESS (0)
313 * WIMLIB_ERR_UNEXPECTED_END_OF_FILE
317 write_integrity_table(WIMStruct *wim,
318 off_t new_lookup_table_end,
319 off_t old_lookup_table_end)
321 struct integrity_table *old_table;
322 struct integrity_table *new_table;
326 DEBUG("Writing integrity table "
327 "(new_lookup_table_end=%"PRIu64", old_lookup_table_end=%"PRIu64")",
328 new_lookup_table_end, old_lookup_table_end);
330 wimlib_assert(old_lookup_table_end <= new_lookup_table_end);
333 if (wim_has_integrity_table(wim) && old_lookup_table_end != 0) {
334 ret = read_integrity_table(wim,
335 old_lookup_table_end - WIM_HEADER_DISK_SIZE,
337 if (ret == WIMLIB_ERR_INVALID_INTEGRITY_TABLE) {
338 WARNING("Old integrity table is invalid! "
340 } else if (ret != 0) {
341 WARNING("Can't read old integrity table! "
346 ret = calculate_integrity_table(&wim->out_fd, new_lookup_table_end,
347 old_table, old_lookup_table_end,
348 &new_table, wim->progfunc, wim->progctx);
350 goto out_free_old_table;
352 new_table_size = new_table->size;
354 new_table->size = cpu_to_le32(new_table->size);
355 new_table->num_entries = cpu_to_le32(new_table->num_entries);
356 new_table->chunk_size = cpu_to_le32(new_table->chunk_size);
358 ret = write_wim_resource_from_buffer(new_table,
362 WIMLIB_COMPRESSION_TYPE_NONE,
364 &wim->hdr.integrity_table_reshdr,
370 DEBUG("ret=%d", ret);
375 * verify_integrity():
377 * Checks a WIM for consistency with the integrity table.
380 * File descriptor to the WIM file, opened for reading.
383 * The integrity table for the WIM, read into memory.
386 * Number of bytes in the WIM that need to be checked (offset of end of the
387 * lookup table minus offset of end of the header).
390 * > 0 (WIMLIB_ERR_READ, WIMLIB_ERR_UNEXPECTED_END_OF_FILE) on error
391 * 0 (WIM_INTEGRITY_OK) if the integrity was checked successfully and there
392 * were no inconsistencies.
393 * -1 (WIM_INTEGRITY_NOT_OK) if the WIM failed the integrity check.
396 verify_integrity(struct filedes *in_fd, const tchar *filename,
397 const struct integrity_table *table,
399 wimlib_progress_func_t progfunc, void *progctx)
402 u64 offset = WIM_HEADER_DISK_SIZE;
403 u8 sha1_md[SHA1_HASH_SIZE];
404 union wimlib_progress_info progress;
406 progress.integrity.total_bytes = bytes_to_check;
407 progress.integrity.total_chunks = table->num_entries;
408 progress.integrity.completed_chunks = 0;
409 progress.integrity.completed_bytes = 0;
410 progress.integrity.chunk_size = table->chunk_size;
411 progress.integrity.filename = filename;
413 ret = call_progress(progfunc, WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY,
418 for (u32 i = 0; i < table->num_entries; i++) {
419 size_t this_chunk_size;
420 if (i == table->num_entries - 1)
421 this_chunk_size = MODULO_NONZERO(bytes_to_check,
424 this_chunk_size = table->chunk_size;
426 ret = calculate_chunk_sha1(in_fd, this_chunk_size, offset, sha1_md);
430 if (!hashes_equal(sha1_md, table->sha1sums[i]))
431 return WIM_INTEGRITY_NOT_OK;
433 offset += this_chunk_size;
434 progress.integrity.completed_chunks++;
435 progress.integrity.completed_bytes += this_chunk_size;
437 ret = call_progress(progfunc, WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY,
442 return WIM_INTEGRITY_OK;
447 * check_wim_integrity():
449 * Verifies the integrity of the WIM by making sure the SHA1 message digests of
450 * ~10 MiB chunks of the WIM match up with the values given in the integrity
454 * The WIM, opened for reading.
457 * > 0 (WIMLIB_ERR_INVALID_INTEGRITY_TABLE, WIMLIB_ERR_READ,
458 * WIMLIB_ERR_UNEXPECTED_END_OF_FILE) on error
459 * 0 (WIM_INTEGRITY_OK) if the integrity was checked successfully and there
460 * were no inconsistencies.
461 * -1 (WIM_INTEGRITY_NOT_OK) if the WIM failed the integrity check.
462 * -2 (WIM_INTEGRITY_NONEXISTENT) if the WIM contains no integrity
466 check_wim_integrity(WIMStruct *wim)
470 struct integrity_table *table;
471 u64 end_lookup_table_offset;
473 if (!wim_has_integrity_table(wim)) {
474 DEBUG("No integrity information.");
475 return WIM_INTEGRITY_NONEXISTENT;
478 end_lookup_table_offset = wim->hdr.lookup_table_reshdr.offset_in_wim +
479 wim->hdr.lookup_table_reshdr.size_in_wim;
481 if (end_lookup_table_offset < WIM_HEADER_DISK_SIZE) {
482 ERROR("WIM lookup table ends before WIM header ends!");
483 return WIMLIB_ERR_INVALID_INTEGRITY_TABLE;
486 bytes_to_check = end_lookup_table_offset - WIM_HEADER_DISK_SIZE;
488 ret = read_integrity_table(wim, bytes_to_check, &table);
491 ret = verify_integrity(&wim->in_fd, wim->filename, table,
492 bytes_to_check, wim->progfunc, wim->progctx);