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