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