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"
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. */
35 int read_header(FILE *fp, struct wim_header *hdr, int split_ok)
38 u8 buf[WIM_HEADER_DISK_SIZE];
46 DEBUG("Reading WIM header.");
48 bytes_read = fread(buf, 1, WIM_MAGIC_LEN, fp);
50 if (bytes_read != WIM_MAGIC_LEN)
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;
60 bytes_read = fread(&hdr_size, 1, sizeof(u32), fp);
61 if (bytes_read != sizeof(u32))
68 if (hdr_size != WIM_HEADER_DISK_SIZE) {
69 DEBUG("ERROR: Header is size %u (expected %u)",
70 hdr_size, WIM_HEADER_DISK_SIZE);
71 return WIMLIB_ERR_INVALID_HEADER_SIZE;
74 /* Read the rest of the header into a buffer. */
76 hdr_rem_size = WIM_HEADER_DISK_SIZE - WIM_MAGIC_LEN - sizeof(u32);
78 bytes_read = fread(buf + WIM_MAGIC_LEN + sizeof(u32), 1,
80 if (bytes_read != hdr_rem_size)
83 p = get_u32(buf + WIM_MAGIC_LEN + sizeof(u32), &wim_version);
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;
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.",
99 ERROR("(Or it might just be that the WIM header is invalid.)");
100 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
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);
107 if (!split_ok && (hdr->part_number != 1 || hdr->total_parts != 1)) {
108 ERROR("This WIM is part %u of a %u-part WIM",
109 hdr->part_number, hdr->total_parts);
110 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
113 p = get_u32(p, &hdr->image_count);
115 DEBUG("part_number = %u, total_parts = %u, image_count = %u",
116 hdr->part_number, hdr->total_parts, hdr->image_count);
120 p = get_resource_entry(p, &hdr->lookup_table_res_entry);
121 p = get_resource_entry(p, &hdr->xml_res_entry);
122 p = get_resource_entry(p, &hdr->boot_metadata_res_entry);
126 p = get_u32(p, &hdr->boot_idx);
130 p = get_resource_entry(p, &hdr->integrity);
134 /* 60 bytes of unused stuff. */
142 ERROR("Unexpected EOF while reading WIM header");
144 ERROR_WITH_ERRNO("Error reading WIM header");
145 return WIMLIB_ERR_READ;
149 * Writes the header for a WIM file.
151 * @hdr: A pointer to a struct wim_header structure that describes the header.
152 * @out: The FILE* for the output file, positioned at the appropriate
153 * place (the beginning of the file).
154 * @return: Zero on success, nonzero on failure.
156 int write_header(const struct wim_header *hdr, FILE *out_fp)
158 u8 buf[WIM_HEADER_DISK_SIZE];
160 DEBUG("Writing WIM header.");
162 p = put_bytes(buf, WIM_MAGIC_LEN, wim_magic_chars);
163 p = put_u32(p, WIM_HEADER_DISK_SIZE);
164 p = put_u32(p, WIM_VERSION);
165 p = put_u32(p, hdr->flags);
166 p = put_u32(p, (hdr->flags & WIM_HDR_FLAG_COMPRESSION) ?
170 p = put_bytes(p, WIM_GID_LEN, hdr->guid);
171 p = put_u16(p, hdr->part_number);
175 p = put_u16(p, hdr->total_parts);
176 p = put_u32(p, hdr->image_count);
177 p = put_resource_entry(p, &hdr->lookup_table_res_entry);
178 p = put_resource_entry(p, &hdr->xml_res_entry);
179 p = put_resource_entry(p, &hdr->boot_metadata_res_entry);
180 p = put_u32(p, hdr->boot_idx);
181 p = put_resource_entry(p, &hdr->integrity);
182 memset(p, 0, WIM_UNUSED_LEN);
183 if (fwrite(buf, 1, sizeof(buf), out_fp) != sizeof(buf)) {
184 ERROR_WITH_ERRNO("Failed to write WIM header");
185 return WIMLIB_ERR_WRITE;
187 DEBUG("Done writing WIM header");
192 * Initializes the header for a WIM file.
194 int init_header(struct wim_header *hdr, int ctype)
196 memset(hdr, 0, sizeof(struct wim_header));
198 case WIM_COMPRESSION_TYPE_NONE:
201 case WIM_COMPRESSION_TYPE_LZX:
202 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
203 WIM_HDR_FLAG_COMPRESS_LZX;
205 case WIM_COMPRESSION_TYPE_XPRESS:
206 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
207 WIM_HDR_FLAG_COMPRESS_XPRESS;
210 ERROR("Invalid compression type specified (%d)", ctype);
211 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
213 hdr->total_parts = 1;
214 hdr->part_number = 1;
215 randomize_byte_array(hdr->guid, sizeof(hdr->guid));
223 struct hdr_flag hdr_flags[] = {
224 {WIM_HDR_FLAG_RESERVED, "RESERVED"},
225 {WIM_HDR_FLAG_COMPRESSION, "COMPRESSION"},
226 {WIM_HDR_FLAG_READONLY, "READONLY"},
227 {WIM_HDR_FLAG_SPANNED, "SPANNED"},
228 {WIM_HDR_FLAG_RESOURCE_ONLY, "RESOURCE_ONLY"},
229 {WIM_HDR_FLAG_METADATA_ONLY, "METADATA_ONLY"},
230 {WIM_HDR_FLAG_WRITE_IN_PROGRESS,"WRITE_IN_PROGRESS"},
231 {WIM_HDR_FLAG_RP_FIX, "RP_FIX"},
232 {WIM_HDR_FLAG_COMPRESS_RESERVED,"COMPRESS_RESERVED"},
233 {WIM_HDR_FLAG_COMPRESS_LZX, "COMPRESS_LZX"},
234 {WIM_HDR_FLAG_COMPRESS_XPRESS, "COMPRESS_XPRESS"},
237 /* Prints information from the header of the WIM file associated with @w. */
238 WIMLIBAPI void wimlib_print_header(const WIMStruct *w)
240 const struct wim_header *hdr = &w->hdr;
243 printf("Magic Characters = MSWIM\\000\\000\\000\n");
244 printf("Header Size = %u\n", WIM_HEADER_DISK_SIZE);
245 printf("Version = 0x%x\n", WIM_VERSION);
247 printf("Flags = 0x%x\n", hdr->flags);
248 for (i = 0; i < ARRAY_LEN(hdr_flags); i++)
249 if (hdr_flags[i].flag & hdr->flags)
250 printf(" WIM_HDR_FLAG_%s is set\n", hdr_flags[i].name);
252 printf("Chunk Size = %u\n", WIM_CHUNK_SIZE);
253 fputs ("GUID = ", stdout);
254 print_byte_field(hdr->guid, WIM_GID_LEN);
256 printf("Part Number = %hu\n", 1);
257 printf("Total Parts = %hu\n", 1);
258 printf("Image Count = %u\n", hdr->image_count);
259 printf("Lookup Table Size = %"PRIu64"\n",
260 (u64)hdr->lookup_table_res_entry.size);
261 printf("Lookup Table Flags = 0x%hhx\n",
262 hdr->lookup_table_res_entry.flags);
263 printf("Lookup Table Offset = %"PRIu64"\n",
264 hdr->lookup_table_res_entry.offset);
265 printf("Lookup Table Original_size = %"PRIu64"\n",
266 hdr->lookup_table_res_entry.original_size);
267 printf("XML Data Size = %"PRIu64"\n",
268 (u64)hdr->xml_res_entry.size);
269 printf("XML Data Flags = 0x%hhx\n",
270 hdr->xml_res_entry.flags);
271 printf("XML Data Offset = %"PRIu64"\n",
272 hdr->xml_res_entry.offset);
273 printf("XML Data Original Size = %"PRIu64"\n",
274 hdr->xml_res_entry.original_size);
275 printf("Boot Metadata Size = %"PRIu64"\n",
276 (u64)hdr->boot_metadata_res_entry.size);
277 printf("Boot Metadata Flags = 0x%hhx\n",
278 hdr->boot_metadata_res_entry.flags);
279 printf("Boot Metadata Offset = %"PRIu64"\n",
280 hdr->boot_metadata_res_entry.offset);
281 printf("Boot Metadata Original Size = %"PRIu64"\n",
282 hdr->boot_metadata_res_entry.original_size);
283 printf("Boot Index = %u\n", hdr->boot_idx);
284 printf("Integrity Size = %"PRIu64"\n",
285 (u64)hdr->integrity.size);
286 printf("Integrity Flags = 0x%hhx\n",
287 hdr->integrity.flags);
288 printf("Integrity Offset = %"PRIu64"\n",
289 hdr->integrity.offset);
290 printf("Integrity Original_size = %"PRIu64"\n",
291 hdr->integrity.original_size);