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 free software; you can redistribute it and/or modify it under
13 * the terms of the GNU Lesser General Public License as published by the Free
14 * Software Foundation; either version 3 of the License, or (at your option) any
17 * This file is distributed in the hope that it will be useful, but WITHOUT
18 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this file; if not, see http://www.gnu.org/licenses/.
30 #include "wimlib/assert.h"
31 #include "wimlib/endianness.h"
32 #include "wimlib/error.h"
33 #include "wimlib/file_io.h"
34 #include "wimlib/integrity.h"
35 #include "wimlib/progress.h"
36 #include "wimlib/resource.h"
37 #include "wimlib/sha1.h"
38 #include "wimlib/wim.h"
39 #include "wimlib/write.h"
41 /* Size, in bytes, of each SHA1-summed chunk, when wimlib writes integrity
43 #define INTEGRITY_CHUNK_SIZE 10485760
45 /* Only use a different chunk size for compatibility with an existing integrity
46 * table if the chunk size is between these two numbers. */
47 #define INTEGRITY_MIN_CHUNK_SIZE 4096
48 #define INTEGRITY_MAX_CHUNK_SIZE 134217728
50 struct integrity_table {
58 calculate_chunk_sha1(struct filedes *in_fd, size_t this_chunk_size,
59 off_t offset, u8 sha1_md[])
63 size_t bytes_remaining;
67 bytes_remaining = this_chunk_size;
70 bytes_to_read = min(bytes_remaining, sizeof(buf));
71 ret = full_pread(in_fd, buf, bytes_to_read, offset);
73 ERROR_WITH_ERRNO("Read error while calculating "
74 "integrity checksums");
77 sha1_update(&ctx, buf, bytes_to_read);
78 bytes_remaining -= bytes_to_read;
79 offset += bytes_to_read;
80 } while (bytes_remaining);
81 sha1_final(sha1_md, &ctx);
87 * read_integrity_table: - Reads the integrity table from a WIM file.
90 * WIMStruct for the WIM file; @wim->hdr.integrity_table_reshdr specifies
91 * the location of the integrity table. @wim->in_fd is expected to be a
92 * seekable file descriptor to the WIM file opened for reading.
95 * Number of bytes of data that should be checked by the integrity table.
98 * On success, a pointer to an in-memory structure containing the integrity
99 * information is written to this location.
102 * WIMLIB_ERR_SUCCESS (0)
103 * WIMLIB_ERR_INVALID_INTEGRITY_TABLE
106 * WIMLIB_ERR_UNEXPECTED_END_OF_FILE
109 read_integrity_table(WIMStruct *wim, u64 num_checked_bytes,
110 struct integrity_table **table_ret)
113 struct integrity_table *table;
116 if (wim->hdr.integrity_table_reshdr.uncompressed_size < 8)
119 ret = wim_reshdr_to_data(&wim->hdr.integrity_table_reshdr, wim, &buf);
124 table->size = le32_to_cpu(table->size);
125 table->num_entries = le32_to_cpu(table->num_entries);
126 table->chunk_size = le32_to_cpu(table->chunk_size);
128 if (table->size != wim->hdr.integrity_table_reshdr.uncompressed_size ||
129 table->size != (u64)table->num_entries * SHA1_HASH_SIZE + 12 ||
130 table->chunk_size == 0 ||
131 table->num_entries != DIV_ROUND_UP(num_checked_bytes, table->chunk_size))
141 return WIMLIB_ERR_INVALID_INTEGRITY_TABLE;
145 * calculate_integrity_table():
147 * Calculates an integrity table for the data in a file beginning at offset 208
148 * (WIM_HEADER_DISK_SIZE).
151 * File descriptor for the file to be checked, opened for reading. Does
152 * not need to be at any specific location in the file.
155 * Offset of byte after the last byte to be checked.
158 * If non-NULL, a pointer to the table containing the previously calculated
159 * integrity data for a prefix of this file.
162 * If @old_table is non-NULL, the byte after the last byte that was checked
163 * in the old table. Must be less than or equal to new_check_end.
165 * @integrity_table_ret:
166 * On success, a pointer to the calculated integrity table is written into
170 * WIMLIB_ERR_SUCCESS (0)
173 * WIMLIB_ERR_UNEXPECTED_END_OF_FILE
176 calculate_integrity_table(struct filedes *in_fd,
178 const struct integrity_table *old_table,
180 struct integrity_table **integrity_table_ret,
181 wimlib_progress_func_t progfunc,
185 size_t chunk_size = INTEGRITY_CHUNK_SIZE;
187 /* If an old table is provided, set the chunk size to be compatible with
188 * the old chunk size, unless the old chunk size was weird. */
189 if (old_table != NULL) {
190 if (old_table->num_entries == 0 ||
191 old_table->chunk_size < INTEGRITY_MIN_CHUNK_SIZE ||
192 old_table->chunk_size > INTEGRITY_MAX_CHUNK_SIZE)
195 chunk_size = old_table->chunk_size;
199 u64 old_check_bytes = old_check_end - WIM_HEADER_DISK_SIZE;
200 u64 new_check_bytes = new_check_end - WIM_HEADER_DISK_SIZE;
202 u32 old_num_chunks = DIV_ROUND_UP(old_check_bytes, chunk_size);
203 u32 new_num_chunks = DIV_ROUND_UP(new_check_bytes, chunk_size);
205 size_t old_last_chunk_size = MODULO_NONZERO(old_check_bytes, chunk_size);
206 size_t new_last_chunk_size = MODULO_NONZERO(new_check_bytes, chunk_size);
208 size_t new_table_size = 12 + new_num_chunks * SHA1_HASH_SIZE;
210 struct integrity_table *new_table = MALLOC(new_table_size);
212 return WIMLIB_ERR_NOMEM;
213 new_table->num_entries = new_num_chunks;
214 new_table->size = new_table_size;
215 new_table->chunk_size = chunk_size;
217 u64 offset = WIM_HEADER_DISK_SIZE;
218 union wimlib_progress_info progress;
220 progress.integrity.total_bytes = new_check_bytes;
221 progress.integrity.total_chunks = new_num_chunks;
222 progress.integrity.completed_chunks = 0;
223 progress.integrity.completed_bytes = 0;
224 progress.integrity.chunk_size = chunk_size;
225 progress.integrity.filename = NULL;
227 ret = call_progress(progfunc, WIMLIB_PROGRESS_MSG_CALC_INTEGRITY,
230 goto out_free_new_table;
232 for (u32 i = 0; i < new_num_chunks; i++) {
233 size_t this_chunk_size;
234 if (i == new_num_chunks - 1)
235 this_chunk_size = new_last_chunk_size;
237 this_chunk_size = chunk_size;
239 ((this_chunk_size == chunk_size && i < old_num_chunks - 1) ||
240 (i == old_num_chunks - 1 && this_chunk_size == old_last_chunk_size)))
242 /* Can use SHA1 message digest from old integrity table
244 copy_hash(new_table->sha1sums[i], old_table->sha1sums[i]);
246 /* Calculate the SHA1 message digest of this chunk */
247 ret = calculate_chunk_sha1(in_fd, this_chunk_size,
248 offset, new_table->sha1sums[i]);
250 goto out_free_new_table;
252 offset += this_chunk_size;
254 progress.integrity.completed_chunks++;
255 progress.integrity.completed_bytes += this_chunk_size;
256 ret = call_progress(progfunc, WIMLIB_PROGRESS_MSG_CALC_INTEGRITY,
259 goto out_free_new_table;
261 *integrity_table_ret = new_table;
270 * write_integrity_table():
272 * Writes a WIM integrity table (a list of SHA1 message digests of raw 10 MiB
273 * chunks of the file).
275 * This function can optionally re-use entries from an older integrity table.
276 * To do this, specify old_blob_table_end and old_table.
278 * On success, @wim->out_hdr.integrity_table_reshdr will be filled in with
279 * information about the integrity table that was written.
282 * WIMStruct for the WIM file. @wim->out_fd must be a seekable descriptor
283 * to the new WIM file, opened read-write, positioned at the location at
284 * which the integrity table is to be written.
286 * @new_blob_table_end:
287 * The offset of the byte directly following the blob table in the WIM
290 * @old_blob_table_end:
291 * If nonzero, the offset of the byte directly following the old blob table
295 * Pointer to the old integrity table read into memory, or NULL if not
299 write_integrity_table(WIMStruct *wim,
300 off_t new_blob_table_end,
301 off_t old_blob_table_end,
302 struct integrity_table *old_table)
304 struct integrity_table *new_table;
308 wimlib_assert(old_blob_table_end <= new_blob_table_end);
310 ret = calculate_integrity_table(&wim->out_fd, new_blob_table_end,
311 old_table, old_blob_table_end,
312 &new_table, wim->progfunc, wim->progctx);
316 new_table_size = new_table->size;
318 new_table->size = cpu_to_le32(new_table->size);
319 new_table->num_entries = cpu_to_le32(new_table->num_entries);
320 new_table->chunk_size = cpu_to_le32(new_table->chunk_size);
322 ret = write_wim_resource_from_buffer(new_table,
326 WIMLIB_COMPRESSION_TYPE_NONE,
328 &wim->out_hdr.integrity_table_reshdr,
336 * verify_integrity():
338 * Checks a WIM for consistency with the integrity table.
341 * File descriptor to the WIM file, opened for reading.
344 * The integrity table for the WIM, read into memory.
347 * Number of bytes in the WIM that need to be checked (offset of end of the
348 * blob table minus offset of end of the header).
351 * > 0 (WIMLIB_ERR_READ, WIMLIB_ERR_UNEXPECTED_END_OF_FILE) on error
352 * 0 (WIM_INTEGRITY_OK) if the integrity was checked successfully and there
353 * were no inconsistencies.
354 * -1 (WIM_INTEGRITY_NOT_OK) if the WIM failed the integrity check.
357 verify_integrity(struct filedes *in_fd, const tchar *filename,
358 const struct integrity_table *table,
360 wimlib_progress_func_t progfunc, void *progctx)
363 u64 offset = WIM_HEADER_DISK_SIZE;
364 u8 sha1_md[SHA1_HASH_SIZE];
365 union wimlib_progress_info progress;
367 progress.integrity.total_bytes = bytes_to_check;
368 progress.integrity.total_chunks = table->num_entries;
369 progress.integrity.completed_chunks = 0;
370 progress.integrity.completed_bytes = 0;
371 progress.integrity.chunk_size = table->chunk_size;
372 progress.integrity.filename = filename;
374 ret = call_progress(progfunc, WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY,
379 for (u32 i = 0; i < table->num_entries; i++) {
380 size_t this_chunk_size;
381 if (i == table->num_entries - 1)
382 this_chunk_size = MODULO_NONZERO(bytes_to_check,
385 this_chunk_size = table->chunk_size;
387 ret = calculate_chunk_sha1(in_fd, this_chunk_size, offset, sha1_md);
391 if (!hashes_equal(sha1_md, table->sha1sums[i]))
392 return WIM_INTEGRITY_NOT_OK;
394 offset += this_chunk_size;
395 progress.integrity.completed_chunks++;
396 progress.integrity.completed_bytes += this_chunk_size;
398 ret = call_progress(progfunc, WIMLIB_PROGRESS_MSG_VERIFY_INTEGRITY,
403 return WIM_INTEGRITY_OK;
408 * check_wim_integrity():
410 * Verifies the integrity of the WIM by making sure the SHA1 message digests of
411 * ~10 MiB chunks of the WIM match up with the values given in the integrity
415 * The WIM, opened for reading.
418 * > 0 (WIMLIB_ERR_INVALID_INTEGRITY_TABLE, WIMLIB_ERR_READ,
419 * WIMLIB_ERR_UNEXPECTED_END_OF_FILE) on error
420 * 0 (WIM_INTEGRITY_OK) if the integrity was checked successfully and there
421 * were no inconsistencies.
422 * -1 (WIM_INTEGRITY_NOT_OK) if the WIM failed the integrity check.
423 * -2 (WIM_INTEGRITY_NONEXISTENT) if the WIM contains no integrity
427 check_wim_integrity(WIMStruct *wim)
431 struct integrity_table *table;
432 u64 end_blob_table_offset;
434 if (!wim_has_integrity_table(wim))
435 return WIM_INTEGRITY_NONEXISTENT;
437 end_blob_table_offset = wim->hdr.blob_table_reshdr.offset_in_wim +
438 wim->hdr.blob_table_reshdr.size_in_wim;
440 if (end_blob_table_offset < WIM_HEADER_DISK_SIZE) {
441 ERROR("WIM blob table ends before WIM header ends!");
442 return WIMLIB_ERR_INVALID_INTEGRITY_TABLE;
445 bytes_to_check = end_blob_table_offset - WIM_HEADER_DISK_SIZE;
447 ret = read_integrity_table(wim, bytes_to_check, &table);
450 ret = verify_integrity(&wim->in_fd, wim->filename, table,
451 bytes_to_check, wim->progfunc, wim->progctx);