Clean up file headers
[wimlib] / src / integrity.c
1 /*
2  * integrity.c
3  *
4  * WIM files can optionally contain an array of SHA1 message digests at the end,
5  * one digest for each 1 MB of the file.  This file implements the checking of
6  * the digests, and the writing of the digests for new WIM files.
7  */
8
9 /*
10  * Copyright (C) 2012 Eric Biggers
11  *
12  * This file is part of wimlib, a library for working with WIM files.
13  *
14  * wimlib is free software; you can redistribute it and/or modify it under the
15  * terms of the GNU Lesser General Public License as published by the Free
16  * Software Foundation; either version 2.1 of the License, or (at your option)
17  * any later version.
18  *
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 Lesser General Public License for more
22  * details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with wimlib; if not, see http://www.gnu.org/licenses/.
26  */
27
28 #include "wimlib_internal.h"
29 #include "io.h"
30 #include "sha1.h"
31
32 /* Size, in bytes, of each SHA1-summed chunk, when wimlib writes integrity
33  * information. */
34 #define INTEGRITY_CHUNK_SIZE 10485760
35
36 /*
37  * Verifies the integrity of a WIM. 
38  *
39  * @fp:            FILE* of the WIM, currently positioned at the end of the header. 
40  * @num_bytes:     Number of bytes to verify the integrity of.
41  * @chunk_size:    Chunk size per SHA1 message digest.
42  * @sha1sums:      Array of SHA1 message digests; 20 bytes each, one per chunk.
43  * @show_progress: Nonzero if the percent complete is to be printed after every
44  *                      chunk.
45  * @status:        On success, set to WIM_INTEGRITY_OK or WIM_INTEGRITY_NOT_OK 
46  *                      based on whether the WIM is intact or not.
47  */
48 static int verify_integrity(FILE *fp, u64 num_bytes, u32 chunk_size, 
49                             const u8 *sha1sums, int show_progress,
50                             int *status)
51 {
52         char  *chunk_buf;
53         u8     resblock[WIM_HASH_SIZE];
54         u64    bytes_remaining;
55         size_t bytes_to_read;
56         uint   percent_done;
57         int    ret;
58
59         chunk_buf = MALLOC(chunk_size);
60         if (!chunk_buf) {
61                 ERROR("Failed to allocate %u byte buffer for integrity "
62                                 "chunks\n", chunk_size);
63                 return WIMLIB_ERR_NOMEM;
64         }
65         bytes_remaining = num_bytes;
66         while (bytes_remaining != 0) {
67                 if (show_progress) {
68                         percent_done = (num_bytes - bytes_remaining) * 100 / 
69                                         num_bytes;
70                         printf("Verifying integrity of WIM (%"PRIu64" bytes "
71                                         "remaining, %u%% done)       \r", 
72                                         bytes_remaining, percent_done);
73                         fflush(stdout);
74                 }
75                 bytes_to_read = min(chunk_size, bytes_remaining);
76                 if (fread(chunk_buf, 1, bytes_to_read, fp) != bytes_to_read) {
77                         if (feof(fp)) {
78                                 ERROR("Unexpected EOF while verifying "
79                                                 "integrity of WIM!\n");
80                         } else {
81                                 ERROR("File stream error while verifying "
82                                                 "integrity of WIM: %m\n");
83                         }
84                         ret = WIMLIB_ERR_READ;
85                         goto verify_integrity_error;
86                 }
87                 sha1_buffer(chunk_buf, bytes_to_read, resblock);
88                 if (memcmp(resblock, sha1sums, WIM_HASH_SIZE) != 0) {
89                         *status = WIM_INTEGRITY_NOT_OK;
90                         goto verify_integrity_done;
91                 }
92                 sha1sums += WIM_HASH_SIZE;
93                 bytes_remaining -= bytes_to_read;
94         }
95         *status = WIM_INTEGRITY_OK;
96 verify_integrity_done:
97         ret = 0;
98 verify_integrity_error:
99         FREE(chunk_buf);
100         if (show_progress)
101                 putchar('\n');
102         return ret;
103 }
104
105 /*
106  * Verifies the integrity of the WIM. 
107  *
108  * @show_progress: Nonzero if the percent complete is to be printed after every
109  *                      chunk.
110  * @status:        On success, set to WIM_INTEGRITY_OK, WIM_INTEGRITY_NOT_OK,
111  *                      or WIM_INTEGRITY_NONEXISTENT.
112  *
113  * Returns: 0, WIMLIB_ERR_INVALID_INTEGRITY_TABLE, WIMLIB_ERR_NOMEM, or
114  * WIMLIB_ERR_READ.  If nonzero, the boolean pointed to by @ok is not changed.
115  */
116 int check_wim_integrity(WIMStruct *w, int show_progress, int *status)
117 {
118
119         struct resource_entry *res_entry;
120         int ctype;
121         u8 *buf = NULL;
122         int ret;
123         u32 integrity_table_size;
124         u32 num_entries;
125         u32 chunk_size;
126         const u8 *p;
127         u64 expected_size;
128         u64 end_lookup_table_offset;
129         u64 bytes_to_check;
130         u64 expected_num_entries;
131
132         res_entry = &w->hdr.integrity;
133         if (res_entry->size == 0) {
134                 DEBUG("No integrity information.\n");
135                 *status = WIM_INTEGRITY_NONEXISTENT;
136                 return 0;
137         }
138         ctype = wim_resource_compression_type(w, res_entry);
139         if (res_entry->original_size < 12) {
140                 ERROR("Integrity table resource is too short!\n");
141                 return WIMLIB_ERR_INVALID_INTEGRITY_TABLE;
142         }
143
144         /* Read the integrity table into memory. */
145         buf = MALLOC(res_entry->original_size);
146         if (!buf) {
147                 ERROR("Out of memory (needed %zu bytes for integrity table)!\n",
148                                                 res_entry->original_size);
149                 ret = WIMLIB_ERR_NOMEM;
150                 goto check_integrity_error;
151         }
152         ret = read_full_resource(w->fp, res_entry->size, res_entry->original_size,
153                                  res_entry->offset, ctype, buf);
154         if (ret != 0) {
155                 ERROR("Failed to read integrity table (size = %"PRIu64", "
156                                 "original_size = %"PRIu64", offset = "
157                                 "%"PRIu64", ctype = %d\n",
158                                 (u64)res_entry->size, res_entry->original_size,
159                                 res_entry->offset, ctype);
160                 goto check_integrity_error;
161         }
162
163         p = get_u32(buf, &integrity_table_size);
164         p = get_u32(p, &num_entries);
165         p = get_u32(p, &chunk_size);
166
167         /* p now points to the array of SHA1 message digests for the WIM. */
168
169         /* Make sure the integrity table is the right size. */
170         if (integrity_table_size != res_entry->original_size) {
171                 ERROR("Inconsistent integrity table sizes: header says %u "
172                                 "bytes but resource entry says "
173                                 "%"PRIu64" bytes\n", integrity_table_size, 
174                                 res_entry->original_size);
175
176                 ret = WIMLIB_ERR_INVALID_INTEGRITY_TABLE;
177                 goto check_integrity_error;
178         }
179
180         DEBUG("integrity_table_size = %u, num_entries = %u, chunk_size = %u\n",
181                         integrity_table_size, num_entries, chunk_size);
182
183
184         expected_size = num_entries * WIM_HASH_SIZE + 12;
185
186         if (integrity_table_size != expected_size) {
187                 ERROR("Integrity table is %u bytes, but expected %"PRIu64" "
188                                 "bytes to hold %u entries!\n", 
189                                 integrity_table_size,
190                                 expected_size, num_entries);
191                 ret = WIMLIB_ERR_INVALID_INTEGRITY_TABLE;
192                 goto check_integrity_error;
193         }
194
195         end_lookup_table_offset = w->hdr.lookup_table_res_entry.offset +
196                                   w->hdr.lookup_table_res_entry.size;
197
198         bytes_to_check = end_lookup_table_offset - WIM_HEADER_DISK_SIZE;
199
200         expected_num_entries = bytes_to_check / chunk_size + 
201                                (bytes_to_check % chunk_size != 0);
202
203         if (num_entries != expected_num_entries) {
204                 ERROR("%"PRIu64 " entries would be required to checksum "
205                         "the %"PRIu64" bytes from the end of the header to the\n"
206                         "end of the lookup table with a chunk size of %u, but "
207                         "there were only %u entries!\n", 
208                         expected_num_entries, bytes_to_check, chunk_size,
209                         num_entries);
210                 ret = WIMLIB_ERR_INVALID_INTEGRITY_TABLE;
211                 goto check_integrity_error;
212         }
213
214         /* The integrity checking starts after the header, so seek to the offset
215          * in the WIM after the header. */
216
217         if (fseeko(w->fp, WIM_HEADER_DISK_SIZE, SEEK_SET) != 0) {
218                 ERROR("Failed to seek to byte %u of WIM to check "
219                                 "integrity: %m\n", WIM_HEADER_DISK_SIZE);
220                 ret = WIMLIB_ERR_READ;
221                 goto check_integrity_error;
222         }
223         /* call verify_integrity(), which does the actual checking of the SHA1
224          * message digests. */
225         ret = verify_integrity(w->fp, bytes_to_check, chunk_size, p, 
226                                show_progress, status);
227 check_integrity_error:
228         FREE(buf);
229         return ret;
230 }
231
232 /* 
233  * Writes integrity information to the output stream for a WIM file being
234  * written. 
235  *
236  * @end_header_offset is the offset of the byte after the header, which is the
237  *      beginning of the region that is checksummed.
238  *
239  * @end_lookup_table_offset is the offset of the byte after the lookup table,
240  *      which is the end of the region that is checksummed. 
241  */
242 int write_integrity_table(FILE *out, u64 end_header_offset, 
243                           u64 end_lookup_table_offset, int show_progress)
244 {
245         u64   bytes_to_check;
246         u64   bytes_remaining;
247         u8   *buf;
248         u8   *p;
249         char *chunk_buf;
250         u32   num_entries;
251         u32   integrity_table_size;
252         int   ret;
253
254         DEBUG("Writing integrity table\n");
255         if (fseeko(out, end_header_offset, SEEK_SET) != 0) {
256                 ERROR("Failed to seek to byte %"PRIu64" of WIM "
257                                 "to calculate integrity data: %m\n",
258                                 end_header_offset);
259                 return WIMLIB_ERR_WRITE;
260         }
261
262         bytes_to_check = end_lookup_table_offset - end_header_offset;
263         num_entries = bytes_to_check / INTEGRITY_CHUNK_SIZE +
264                         (bytes_to_check % INTEGRITY_CHUNK_SIZE != 0);
265         integrity_table_size = num_entries * WIM_HASH_SIZE + 3 * sizeof(u32);
266
267         DEBUG("integrity table size = %u\n", integrity_table_size);
268
269
270         buf = MALLOC(integrity_table_size);
271         if (!buf) {
272                 ERROR("Failed to allocate %u bytes for integrity table!\n",
273                                 integrity_table_size);
274                 return WIMLIB_ERR_NOMEM;
275         }
276
277         p = put_u32(buf, integrity_table_size);
278         p = put_u32(p, num_entries);
279         p = put_u32(p, INTEGRITY_CHUNK_SIZE);
280
281         chunk_buf = MALLOC(INTEGRITY_CHUNK_SIZE);
282         if (!chunk_buf) {
283                 ERROR("Failed to allocate %u bytes for integrity chunk "
284                                 "buffer!\n", INTEGRITY_CHUNK_SIZE);
285                 ret = WIMLIB_ERR_NOMEM;
286                 goto err2;
287         }
288
289         bytes_remaining = bytes_to_check;
290
291         DEBUG("Bytes to check = %"PRIu64"\n", bytes_to_check);
292
293         while (bytes_remaining != 0) {
294
295                 uint percent_done = (bytes_to_check - bytes_remaining) * 
296                                     100 / bytes_to_check;
297
298                 if (show_progress) {
299                         printf("Calculating integrity checksums for WIM "
300                                         "(%"PRIu64" bytes remaining, %u%% "
301                                         "done)      \r", 
302                                         bytes_remaining, percent_done);
303                         fflush(stdout);
304                 }
305
306
307                 size_t bytes_to_read = min(INTEGRITY_CHUNK_SIZE, bytes_remaining);
308                 size_t bytes_read = fread(chunk_buf, 1, bytes_to_read, out);
309                 if (bytes_read != bytes_to_read) {
310                         if (feof(out)) {
311                                 ERROR("Unexpected EOF while calculating "
312                                                 "integrity checksums!\n");
313                         } else {
314                                 ERROR("File stream error while calculating "
315                                                 "integrity checksums: %m\n");
316                         }
317                         ret = WIMLIB_ERR_READ;
318                         goto err2;
319                 }
320                 sha1_buffer(chunk_buf, bytes_read, p);
321                 p += WIM_HASH_SIZE;
322                 bytes_remaining -= bytes_read;
323         }
324         if (show_progress)
325                 puts("Calculating integrity checksums for WIM "
326                                 "(0 bytes remaining, 100% done)"
327                                 "                       ");
328
329         if (fseeko(out, 0, SEEK_END) != 0) {
330                 ERROR("Failed to seek to end of WIM to write integrity "
331                                 "table: %m\n");
332                 ret = WIMLIB_ERR_WRITE;
333                 goto err1;
334         }
335
336         if (fwrite(buf, 1, integrity_table_size, out) != integrity_table_size) {
337                 ERROR("Failed to write integrity table to end of WIM: %m\n");
338                 ret = WIMLIB_ERR_WRITE;
339                 goto err1;
340         }
341         ret = 0;
342 err1:
343         FREE(chunk_buf);
344 err2:
345         FREE(buf);
346         return ret;
347 }