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