]> wimlib.net Git - wimlib/blob - src/header.c
overwrite_wim_inplace(): cleanup
[wimlib] / src / header.c
1 /*
2  * header.c
3  *
4  * Read, write, or create a WIM header.
5  */
6
7 /*
8  * Copyright (C) 2012, 2013 Eric Biggers
9  *
10  * This file is part of wimlib, a library for working with WIM files.
11  *
12  * wimlib is free software; you can redistribute it and/or modify it under the
13  * terms of the GNU General Public License as published by the Free
14  * Software Foundation; either version 3 of the License, or (at your option)
15  * any later version.
16  *
17  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
18  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
19  * A PARTICULAR PURPOSE. See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with wimlib; if not, see http://www.gnu.org/licenses/.
24  */
25
26 #include "wimlib_internal.h"
27 #include "buffer_io.h"
28 #include <limits.h>
29
30 /* First 8 bytes in every WIM file. */
31 static const u8 wim_magic_chars[WIM_MAGIC_LEN] = {
32                         'M', 'S', 'W', 'I', 'M', '\0', '\0', '\0' };
33
34 /* Reads the header for a WIM file.  */
35 int
36 read_header(FILE *fp, struct wim_header *hdr, int open_flags)
37 {
38         size_t bytes_read;
39         u8 buf[WIM_HEADER_DISK_SIZE];
40         size_t hdr_rem_size;
41         const u8 *p;
42
43         u32 hdr_size;
44         u32 wim_version;
45         u32 chunk_size;
46
47         DEBUG("Reading WIM header.");
48
49         bytes_read = fread(buf, 1, WIM_MAGIC_LEN, fp);
50
51         if (bytes_read != WIM_MAGIC_LEN)
52                 goto err;
53
54         /* Byte 8 */
55
56         if (memcmp(buf, wim_magic_chars, WIM_MAGIC_LEN) != 0) {
57                 ERROR("Invalid magic characters in WIM header");
58                 return WIMLIB_ERR_NOT_A_WIM_FILE;
59         }
60
61         bytes_read = fread(&hdr_size, 1, sizeof(u32), fp);
62         if (bytes_read != sizeof(u32))
63                 goto err;
64
65         hdr_size = le32_to_cpu(hdr_size);
66
67         /* Byte 12 */
68
69         if (hdr_size != WIM_HEADER_DISK_SIZE) {
70                 ERROR("Header is %u bytes (expected %u bytes)",
71                       hdr_size, WIM_HEADER_DISK_SIZE);
72                 return WIMLIB_ERR_INVALID_HEADER_SIZE;
73         }
74
75         /* Read the rest of the header into a buffer. */
76
77         hdr_rem_size = WIM_HEADER_DISK_SIZE - WIM_MAGIC_LEN - sizeof(u32);
78
79         bytes_read = fread(buf + WIM_MAGIC_LEN + sizeof(u32), 1,
80                            hdr_rem_size, fp);
81         if (bytes_read != hdr_rem_size)
82                 goto err;
83
84         p = get_u32(buf + WIM_MAGIC_LEN + sizeof(u32), &wim_version);
85
86         if (wim_version != WIM_VERSION) {
87                 ERROR("The WIM header says the WIM version is %u, but wimlib "
88                       "only knows about version %u",
89                       wim_version, WIM_VERSION);
90                 return WIMLIB_ERR_UNKNOWN_VERSION;
91         }
92
93         p = get_u32(p, &hdr->flags);
94         p = get_u32(p, &chunk_size);
95         if (chunk_size != WIM_CHUNK_SIZE &&
96             (hdr->flags & WIM_HDR_FLAG_COMPRESSION)) {
97                 ERROR("Unexpected chunk size of %u! Ask the author to "
98                       "implement support for other chunk sizes.",
99                       chunk_size);
100                 ERROR("(Or it might just be that the WIM header is invalid.)");
101                 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
102         }
103
104         p = get_bytes(p, WIM_GID_LEN, hdr->guid);
105         p = get_u16(p, &hdr->part_number);
106         p = get_u16(p, &hdr->total_parts);
107
108         if (hdr->total_parts == 0 ||
109             hdr->part_number == 0 ||
110             hdr->part_number > hdr->total_parts)
111         {
112                 ERROR("Invalid WIM part number: %hu of %hu",
113                       hdr->part_number, hdr->total_parts);
114                 return WIMLIB_ERR_INVALID_PART_NUMBER;
115         }
116
117         if (!(open_flags & WIMLIB_OPEN_FLAG_SPLIT_OK) &&
118             hdr->total_parts != 1)
119         {
120                 ERROR("This WIM is part %u of a %u-part WIM",
121                       hdr->part_number, hdr->total_parts);
122                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
123         }
124
125         p = get_u32(p, &hdr->image_count);
126
127         DEBUG("part_number = %u, total_parts = %u, image_count = %u",
128               hdr->part_number, hdr->total_parts, hdr->image_count);
129
130         if (hdr->image_count >= INT_MAX) {
131                 ERROR("Invalid image count (%u)", hdr->image_count);
132                 return WIMLIB_ERR_IMAGE_COUNT;
133         }
134
135         /* Byte 48 */
136
137         p = get_resource_entry(p, &hdr->lookup_table_res_entry);
138         p = get_resource_entry(p, &hdr->xml_res_entry);
139         p = get_resource_entry(p, &hdr->boot_metadata_res_entry);
140
141         /* Byte 120 */
142
143         p = get_u32(p, &hdr->boot_idx);
144
145         /* Byte 124 */
146
147         p = get_resource_entry(p, &hdr->integrity);
148
149         /* Byte 148 */
150
151         /* 60 bytes of unused stuff. */
152
153         /* Byte 208 */
154
155         return 0;
156
157 err:
158         if (feof(fp))
159                 ERROR("Unexpected EOF while reading WIM header");
160         else
161                 ERROR_WITH_ERRNO("Error reading WIM header");
162         return WIMLIB_ERR_READ;
163 }
164
165 /*
166  * Writes the header for a WIM file.
167  *
168  * @hdr:        A pointer to a struct wim_header structure that describes the header.
169  * @out:        The FILE* for the output file, positioned at the appropriate
170  *              place (the beginning of the file).
171  * @return:     Zero on success, nonzero on failure.
172  */
173 int
174 write_header(const struct wim_header *hdr, FILE *out_fp)
175 {
176         u8 buf[WIM_HEADER_DISK_SIZE];
177         u8 *p;
178         DEBUG("Writing WIM header.");
179
180         p = put_bytes(buf, WIM_MAGIC_LEN, wim_magic_chars);
181         p = put_u32(p, WIM_HEADER_DISK_SIZE);
182         p = put_u32(p, WIM_VERSION);
183         p = put_u32(p, hdr->flags);
184         p = put_u32(p, (hdr->flags & WIM_HDR_FLAG_COMPRESSION) ?
185                                 WIM_CHUNK_SIZE : 0);
186         /* byte 24 */
187
188         p = put_bytes(p, WIM_GID_LEN, hdr->guid);
189         p = put_u16(p, hdr->part_number);
190
191         /* byte 40 */
192
193         p = put_u16(p, hdr->total_parts);
194         p = put_u32(p, hdr->image_count);
195         p = put_resource_entry(p, &hdr->lookup_table_res_entry);
196         p = put_resource_entry(p, &hdr->xml_res_entry);
197         p = put_resource_entry(p, &hdr->boot_metadata_res_entry);
198         p = put_u32(p, hdr->boot_idx);
199         p = put_resource_entry(p, &hdr->integrity);
200         memset(p, 0, WIM_UNUSED_LEN);
201         if (fwrite(buf, 1, sizeof(buf), out_fp) != sizeof(buf)) {
202                 ERROR_WITH_ERRNO("Failed to write WIM header");
203                 return WIMLIB_ERR_WRITE;
204         }
205         DEBUG("Done writing WIM header");
206         return 0;
207 }
208
209 /*
210  * Initializes the header for a WIM file.
211  */
212 int
213 init_header(struct wim_header *hdr, int ctype)
214 {
215         memset(hdr, 0, sizeof(struct wim_header));
216         switch (ctype) {
217         case WIMLIB_COMPRESSION_TYPE_NONE:
218                 hdr->flags = 0;
219                 break;
220         case WIMLIB_COMPRESSION_TYPE_LZX:
221                 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
222                              WIM_HDR_FLAG_COMPRESS_LZX;
223                 break;
224         case WIMLIB_COMPRESSION_TYPE_XPRESS:
225                 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
226                              WIM_HDR_FLAG_COMPRESS_XPRESS;
227                 break;
228         default:
229                 ERROR("Invalid compression type specified (%d)", ctype);
230                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
231         }
232         hdr->total_parts = 1;
233         hdr->part_number = 1;
234         randomize_byte_array(hdr->guid, sizeof(hdr->guid));
235         return 0;
236 }
237
238 struct hdr_flag {
239         u32 flag;
240         const char *name;
241 };
242 struct hdr_flag hdr_flags[] = {
243         {WIM_HDR_FLAG_RESERVED,         "RESERVED"},
244         {WIM_HDR_FLAG_COMPRESSION,      "COMPRESSION"},
245         {WIM_HDR_FLAG_READONLY,         "READONLY"},
246         {WIM_HDR_FLAG_SPANNED,          "SPANNED"},
247         {WIM_HDR_FLAG_RESOURCE_ONLY,    "RESOURCE_ONLY"},
248         {WIM_HDR_FLAG_METADATA_ONLY,    "METADATA_ONLY"},
249         {WIM_HDR_FLAG_WRITE_IN_PROGRESS,"WRITE_IN_PROGRESS"},
250         {WIM_HDR_FLAG_RP_FIX,           "RP_FIX"},
251         {WIM_HDR_FLAG_COMPRESS_RESERVED,"COMPRESS_RESERVED"},
252         {WIM_HDR_FLAG_COMPRESS_LZX,     "COMPRESS_LZX"},
253         {WIM_HDR_FLAG_COMPRESS_XPRESS,  "COMPRESS_XPRESS"},
254 };
255
256 /* Prints information from the header of the WIM file associated with @w. */
257 WIMLIBAPI void
258 wimlib_print_header(const WIMStruct *w)
259 {
260         const struct wim_header *hdr = &w->hdr;
261
262         tprintf(T("Magic Characters            = MSWIM\\000\\000\\000\n"));
263         tprintf(T("Header Size                 = %u\n"), WIM_HEADER_DISK_SIZE);
264         tprintf(T("Version                     = 0x%x\n"), WIM_VERSION);
265
266         tprintf(T("Flags                       = 0x%x\n"), hdr->flags);
267         for (size_t i = 0; i < ARRAY_LEN(hdr_flags); i++)
268                 if (hdr_flags[i].flag & hdr->flags)
269                         tprintf(T("    WIM_HDR_FLAG_%s is set\n"), hdr_flags[i].name);
270
271         tprintf(T("Chunk Size                  = %u\n"), WIM_CHUNK_SIZE);
272         tfputs (T("GUID                        = "), stdout);
273         print_byte_field(hdr->guid, WIM_GID_LEN, stdout);
274         tputchar(T('\n'));
275         tprintf(T("Part Number                 = %hu\n"), w->hdr.part_number);
276         tprintf(T("Total Parts                 = %hu\n"), w->hdr.total_parts);
277         tprintf(T("Image Count                 = %u\n"), hdr->image_count);
278         tprintf(T("Lookup Table Size           = %"PRIu64"\n"),
279                                 (u64)hdr->lookup_table_res_entry.size);
280         tprintf(T("Lookup Table Flags          = 0x%hhx\n"),
281                                 (u8)hdr->lookup_table_res_entry.flags);
282         tprintf(T("Lookup Table Offset         = %"PRIu64"\n"),
283                                 hdr->lookup_table_res_entry.offset);
284         tprintf(T("Lookup Table Original_size  = %"PRIu64"\n"),
285                                 hdr->lookup_table_res_entry.original_size);
286         tprintf(T("XML Data Size               = %"PRIu64"\n"),
287                                 (u64)hdr->xml_res_entry.size);
288         tprintf(T("XML Data Flags              = 0x%hhx\n"),
289                                 (u8)hdr->xml_res_entry.flags);
290         tprintf(T("XML Data Offset             = %"PRIu64"\n"),
291                                 hdr->xml_res_entry.offset);
292         tprintf(T("XML Data Original Size      = %"PRIu64"\n"),
293                                 hdr->xml_res_entry.original_size);
294         tprintf(T("Boot Metadata Size          = %"PRIu64"\n"),
295                                 (u64)hdr->boot_metadata_res_entry.size);
296         tprintf(T("Boot Metadata Flags         = 0x%hhx\n"),
297                                 (u8)hdr->boot_metadata_res_entry.flags);
298         tprintf(T("Boot Metadata Offset        = %"PRIu64"\n"),
299                                 hdr->boot_metadata_res_entry.offset);
300         tprintf(T("Boot Metadata Original Size = %"PRIu64"\n"),
301                                 hdr->boot_metadata_res_entry.original_size);
302         tprintf(T("Boot Index                  = %u\n"), hdr->boot_idx);
303         tprintf(T("Integrity Size              = %"PRIu64"\n"),
304                                 (u64)hdr->integrity.size);
305         tprintf(T("Integrity Flags             = 0x%hhx\n"),
306                                 (u8)hdr->integrity.flags);
307         tprintf(T("Integrity Offset            = %"PRIu64"\n"),
308                                 hdr->integrity.offset);
309         tprintf(T("Integrity Original_size     = %"PRIu64"\n"),
310                                 hdr->integrity.original_size);
311 }