]> wimlib.net Git - wimlib/blob - src/header.c
bb17209117d6f74e03198447bad08981accfa8e4
[wimlib] / src / header.c
1 /*
2  * header.c
3  *
4  * Read, write, or create a WIM header.
5  */
6
7 /*
8  * Copyright (C) 2010 Carl Thijssen
9  * Copyright (C) 2012 Eric Biggers
10  *
11  * This file is part of wimlib, a library for working with WIM files.
12  *
13  * wimlib is free software; you can redistribute it and/or modify it under the
14  * terms of the GNU General Public License as published by the Free
15  * Software Foundation; either version 3 of the License, or (at your option)
16  * any later version.
17  *
18  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20  * A PARTICULAR PURPOSE. See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with wimlib; if not, see http://www.gnu.org/licenses/.
25  */
26
27 #include "wimlib_internal.h"
28 #include "io.h"
29 #include <limits.h>
30
31 /* First 8 bytes in every WIM file. */
32 static const u8 wim_magic_chars[WIM_MAGIC_LEN] = {
33                         'M', 'S', 'W', 'I', 'M', '\0', '\0', '\0' };
34
35 /* Reads the header for a WIM file.  */
36 int 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->part_number != 1 || 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 write_header(const struct wim_header *hdr, FILE *out_fp)
174 {
175         u8 buf[WIM_HEADER_DISK_SIZE];
176         u8 *p;
177         DEBUG("Writing WIM header.");
178
179         p = put_bytes(buf, WIM_MAGIC_LEN, wim_magic_chars);
180         p = put_u32(p, WIM_HEADER_DISK_SIZE);
181         p = put_u32(p, WIM_VERSION);
182         p = put_u32(p, hdr->flags);
183         p = put_u32(p, (hdr->flags & WIM_HDR_FLAG_COMPRESSION) ?
184                                 WIM_CHUNK_SIZE : 0);
185         /* byte 24 */
186
187         p = put_bytes(p, WIM_GID_LEN, hdr->guid);
188         p = put_u16(p, hdr->part_number);
189
190         /* byte 40 */
191
192         p = put_u16(p, hdr->total_parts);
193         p = put_u32(p, hdr->image_count);
194         p = put_resource_entry(p, &hdr->lookup_table_res_entry);
195         p = put_resource_entry(p, &hdr->xml_res_entry);
196         p = put_resource_entry(p, &hdr->boot_metadata_res_entry);
197         p = put_u32(p, hdr->boot_idx);
198         p = put_resource_entry(p, &hdr->integrity);
199         memset(p, 0, WIM_UNUSED_LEN);
200         if (fwrite(buf, 1, sizeof(buf), out_fp) != sizeof(buf)) {
201                 ERROR_WITH_ERRNO("Failed to write WIM header");
202                 return WIMLIB_ERR_WRITE;
203         }
204         DEBUG("Done writing WIM header");
205         return 0;
206 }
207
208 /*
209  * Initializes the header for a WIM file.
210  */
211 int init_header(struct wim_header *hdr, int ctype)
212 {
213         memset(hdr, 0, sizeof(struct wim_header));
214         switch (ctype) {
215         case WIMLIB_COMPRESSION_TYPE_NONE:
216                 hdr->flags = 0;
217                 break;
218         case WIMLIB_COMPRESSION_TYPE_LZX:
219                 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
220                              WIM_HDR_FLAG_COMPRESS_LZX;
221                 break;
222         case WIMLIB_COMPRESSION_TYPE_XPRESS:
223                 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
224                              WIM_HDR_FLAG_COMPRESS_XPRESS;
225                 break;
226         default:
227                 ERROR("Invalid compression type specified (%d)", ctype);
228                 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
229         }
230         hdr->total_parts = 1;
231         hdr->part_number = 1;
232         randomize_byte_array(hdr->guid, sizeof(hdr->guid));
233         return 0;
234 }
235
236 struct hdr_flag {
237         u32 flag;
238         const char *name;
239 };
240 struct hdr_flag hdr_flags[] = {
241         {WIM_HDR_FLAG_RESERVED,         "RESERVED"},
242         {WIM_HDR_FLAG_COMPRESSION,      "COMPRESSION"},
243         {WIM_HDR_FLAG_READONLY,         "READONLY"},
244         {WIM_HDR_FLAG_SPANNED,          "SPANNED"},
245         {WIM_HDR_FLAG_RESOURCE_ONLY,    "RESOURCE_ONLY"},
246         {WIM_HDR_FLAG_METADATA_ONLY,    "METADATA_ONLY"},
247         {WIM_HDR_FLAG_WRITE_IN_PROGRESS,"WRITE_IN_PROGRESS"},
248         {WIM_HDR_FLAG_RP_FIX,           "RP_FIX"},
249         {WIM_HDR_FLAG_COMPRESS_RESERVED,"COMPRESS_RESERVED"},
250         {WIM_HDR_FLAG_COMPRESS_LZX,     "COMPRESS_LZX"},
251         {WIM_HDR_FLAG_COMPRESS_XPRESS,  "COMPRESS_XPRESS"},
252 };
253
254 /* Prints information from the header of the WIM file associated with @w. */
255 WIMLIBAPI void wimlib_print_header(const WIMStruct *w)
256 {
257         const struct wim_header *hdr = &w->hdr;
258
259         printf("Magic Characters            = MSWIM\\000\\000\\000\n");
260         printf("Header Size                 = %u\n", WIM_HEADER_DISK_SIZE);
261         printf("Version                     = 0x%x\n", WIM_VERSION);
262
263         printf("Flags                       = 0x%x\n", hdr->flags);
264         for (size_t i = 0; i < ARRAY_LEN(hdr_flags); i++)
265                 if (hdr_flags[i].flag & hdr->flags)
266                         printf("    WIM_HDR_FLAG_%s is set\n", hdr_flags[i].name);
267
268         printf("Chunk Size                  = %u\n", WIM_CHUNK_SIZE);
269         fputs ("GUID                        = ", stdout);
270         print_byte_field(hdr->guid, WIM_GID_LEN);
271         putchar('\n');
272         printf("Part Number                 = %hu\n", w->hdr.part_number);
273         printf("Total Parts                 = %hu\n", w->hdr.total_parts);
274         printf("Image Count                 = %u\n", hdr->image_count);
275         printf("Lookup Table Size           = %"PRIu64"\n",
276                                 (u64)hdr->lookup_table_res_entry.size);
277         printf("Lookup Table Flags          = 0x%hhx\n",
278                                 hdr->lookup_table_res_entry.flags);
279         printf("Lookup Table Offset         = %"PRIu64"\n",
280                                 hdr->lookup_table_res_entry.offset);
281         printf("Lookup Table Original_size  = %"PRIu64"\n",
282                                 hdr->lookup_table_res_entry.original_size);
283         printf("XML Data Size               = %"PRIu64"\n",
284                                 (u64)hdr->xml_res_entry.size);
285         printf("XML Data Flags              = 0x%hhx\n",
286                                 hdr->xml_res_entry.flags);
287         printf("XML Data Offset             = %"PRIu64"\n",
288                                 hdr->xml_res_entry.offset);
289         printf("XML Data Original Size      = %"PRIu64"\n",
290                                 hdr->xml_res_entry.original_size);
291         printf("Boot Metadata Size          = %"PRIu64"\n",
292                                 (u64)hdr->boot_metadata_res_entry.size);
293         printf("Boot Metadata Flags         = 0x%hhx\n",
294                                 hdr->boot_metadata_res_entry.flags);
295         printf("Boot Metadata Offset        = %"PRIu64"\n",
296                                 hdr->boot_metadata_res_entry.offset);
297         printf("Boot Metadata Original Size = %"PRIu64"\n",
298                                 hdr->boot_metadata_res_entry.original_size);
299         printf("Boot Index                  = %u\n", hdr->boot_idx);
300         printf("Integrity Size              = %"PRIu64"\n",
301                                         (u64)hdr->integrity.size);
302         printf("Integrity Flags             = 0x%hhx\n",
303                                         hdr->integrity.flags);
304         printf("Integrity Offset            = %"PRIu64"\n",
305                                         hdr->integrity.offset);
306         printf("Integrity Original_size     = %"PRIu64"\n",
307                                         hdr->integrity.original_size);
308 }