4 * Read, write, or create a WIM header.
8 * Copyright (C) 2012, 2013 Eric Biggers
10 * This file is part of wimlib, a library for working with WIM files.
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)
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
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/.
26 #include "wimlib_internal.h"
27 #include "buffer_io.h"
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' };
34 /* Reads the header for a WIM file. */
36 read_header(FILE *fp, struct wim_header *hdr, int open_flags)
39 u8 buf[WIM_HEADER_DISK_SIZE];
47 DEBUG("Reading WIM header.");
49 bytes_read = fread(buf, 1, WIM_MAGIC_LEN, fp);
51 if (bytes_read != WIM_MAGIC_LEN)
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;
61 bytes_read = fread(&hdr_size, 1, sizeof(u32), fp);
62 if (bytes_read != sizeof(u32))
65 hdr_size = le32_to_cpu(hdr_size);
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;
75 /* Read the rest of the header into a buffer. */
77 hdr_rem_size = WIM_HEADER_DISK_SIZE - WIM_MAGIC_LEN - sizeof(u32);
79 bytes_read = fread(buf + WIM_MAGIC_LEN + sizeof(u32), 1,
81 if (bytes_read != hdr_rem_size)
84 p = get_u32(buf + WIM_MAGIC_LEN + sizeof(u32), &wim_version);
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;
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.",
100 ERROR("(Or it might just be that the WIM header is invalid.)");
101 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
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);
108 if (hdr->total_parts == 0 ||
109 hdr->part_number == 0 ||
110 hdr->part_number > hdr->total_parts)
112 ERROR("Invalid WIM part number: %hu of %hu",
113 hdr->part_number, hdr->total_parts);
114 return WIMLIB_ERR_INVALID_PART_NUMBER;
117 if (!(open_flags & WIMLIB_OPEN_FLAG_SPLIT_OK) &&
118 hdr->total_parts != 1)
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;
125 p = get_u32(p, &hdr->image_count);
127 DEBUG("part_number = %u, total_parts = %u, image_count = %u",
128 hdr->part_number, hdr->total_parts, hdr->image_count);
130 if (hdr->image_count >= INT_MAX) {
131 ERROR("Invalid image count (%u)", hdr->image_count);
132 return WIMLIB_ERR_IMAGE_COUNT;
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);
143 p = get_u32(p, &hdr->boot_idx);
147 p = get_resource_entry(p, &hdr->integrity);
151 /* 60 bytes of unused stuff. */
159 ERROR("Unexpected EOF while reading WIM header");
161 ERROR_WITH_ERRNO("Error reading WIM header");
162 return WIMLIB_ERR_READ;
166 * Writes the header for a WIM file.
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.
174 write_header(const struct wim_header *hdr, FILE *out_fp)
176 u8 buf[WIM_HEADER_DISK_SIZE];
178 DEBUG("Writing WIM header.");
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) ?
188 p = put_bytes(p, WIM_GID_LEN, hdr->guid);
189 p = put_u16(p, hdr->part_number);
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;
205 DEBUG("Done writing WIM header");
210 * Initializes the header for a WIM file.
213 init_header(struct wim_header *hdr, int ctype)
215 memset(hdr, 0, sizeof(struct wim_header));
217 case WIMLIB_COMPRESSION_TYPE_NONE:
220 case WIMLIB_COMPRESSION_TYPE_LZX:
221 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
222 WIM_HDR_FLAG_COMPRESS_LZX;
224 case WIMLIB_COMPRESSION_TYPE_XPRESS:
225 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
226 WIM_HDR_FLAG_COMPRESS_XPRESS;
229 ERROR("Invalid compression type specified (%d)", ctype);
230 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
232 hdr->total_parts = 1;
233 hdr->part_number = 1;
234 randomize_byte_array(hdr->guid, sizeof(hdr->guid));
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"},
256 /* Prints information from the header of the WIM file associated with @w. */
258 wimlib_print_header(const WIMStruct *w)
260 const struct wim_header *hdr = &w->hdr;
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);
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);
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);
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);