4 * Read, write, or create a WIM header.
8 * Copyright (C) 2010 Carl Thijssen
9 * Copyright (C) 2012 Eric Biggers
11 * This file is part of wimlib, a library for working with WIM files.
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)
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
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/.
27 #include "wimlib_internal.h"
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' };
35 /* Reads the header for a WIM file. */
36 int 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->part_number != 1 || 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.
173 int write_header(const struct wim_header *hdr, FILE *out_fp)
175 u8 buf[WIM_HEADER_DISK_SIZE];
177 DEBUG("Writing WIM header.");
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) ?
187 p = put_bytes(p, WIM_GID_LEN, hdr->guid);
188 p = put_u16(p, hdr->part_number);
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;
204 DEBUG("Done writing WIM header");
209 * Initializes the header for a WIM file.
211 int init_header(struct wim_header *hdr, int ctype)
213 memset(hdr, 0, sizeof(struct wim_header));
215 case WIMLIB_COMPRESSION_TYPE_NONE:
218 case WIMLIB_COMPRESSION_TYPE_LZX:
219 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
220 WIM_HDR_FLAG_COMPRESS_LZX;
222 case WIMLIB_COMPRESSION_TYPE_XPRESS:
223 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
224 WIM_HDR_FLAG_COMPRESS_XPRESS;
227 ERROR("Invalid compression type specified (%d)", ctype);
228 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
230 hdr->total_parts = 1;
231 hdr->part_number = 1;
232 randomize_byte_array(hdr->guid, sizeof(hdr->guid));
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"},
254 /* Prints information from the header of the WIM file associated with @w. */
255 WIMLIBAPI void wimlib_print_header(const WIMStruct *w)
257 const struct wim_header *hdr = &w->hdr;
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);
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);
268 printf("Chunk Size = %u\n", WIM_CHUNK_SIZE);
269 fputs ("GUID = ", stdout);
270 print_byte_field(hdr->guid, WIM_GID_LEN);
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);