4 * Read, write, or create a WIM header.
6 * Copyright (C) 2010 Carl Thijssen
7 * Copyright (C) 2012 Eric Biggers
9 * wimlib - Library for working with WIM files
11 * This library is free software; you can redistribute it and/or modify it under
12 * the terms of the GNU Lesser General Public License as published by the Free
13 * Software Foundation; either version 2.1 of the License, or (at your option) any
16 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
17 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
18 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License along
21 * with this library; if not, write to the Free Software Foundation, Inc., 59
22 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wimlib_internal.h"
28 /* First 8 bytes in every WIM file. */
29 static const u8 wim_magic_chars[WIM_MAGIC_LEN] = {
30 'M', 'S', 'W', 'I', 'M', '\0', '\0', '\0' };
32 /* Reads the header for a WIM file. */
33 int read_header(FILE *fp, struct wim_header *hdr)
36 u8 buf[WIM_HEADER_DISK_SIZE];
46 DEBUG("Reading WIM header.\n");
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\n");
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)\n",
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.\n", 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. "
98 "(Or it might just be that the WIM header is "
99 "invalid.)\n", chunk_size);
100 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
103 p = get_bytes(p, WIM_GID_LEN, hdr->guid);
104 p = get_u16(p, &part_number);
105 p = get_u16(p, &total_parts);
107 if (part_number != 1 || total_parts != 1) {
108 ERROR("Error: This WIM is part %u of a %u-part WIM. Wimlib "
109 "does not support multi-part (split/spanned) "
110 "WIMs.\n", part_number, total_parts);
111 return WIMLIB_ERR_SPLIT;
114 p = get_u32(p, &hdr->image_count);
118 p = get_resource_entry(p, &hdr->lookup_table_res_entry);
119 p = get_resource_entry(p, &hdr->xml_res_entry);
120 p = get_resource_entry(p, &hdr->boot_metadata_res_entry);
124 p = get_u32(p, &hdr->boot_idx);
128 p = get_resource_entry(p, &hdr->integrity);
132 /* 60 bytes of unused stuff. */
140 ERROR("Unexpected EOF while reading WIM header!\n");
142 ERROR("Error reading WIM header: %m\n");
143 return WIMLIB_ERR_READ;
147 * Writes the header for a WIM file.
149 * @hdr: A pointer to a struct wim_header structure that describes the header.
150 * @out: The FILE* for the output file, positioned at the appropriate
151 * place (the beginning of the file).
152 * @return: True on success, false on failure.
154 int write_header(const struct wim_header *hdr, FILE *out)
156 u8 buf[WIM_HEADER_DISK_SIZE];
158 DEBUG("Writing WIM header.\n");
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) ?
166 randomize_byte_array(p, WIM_GID_LEN);
170 p = put_u32(p, hdr->image_count);
171 p = put_resource_entry(p, &hdr->lookup_table_res_entry);
172 p = put_resource_entry(p, &hdr->xml_res_entry);
173 p = put_resource_entry(p, &hdr->boot_metadata_res_entry);
174 p = put_u32(p, hdr->boot_idx);
175 p = put_resource_entry(p, &hdr->integrity);
176 memset(p, 0, WIM_UNUSED_LEN);
177 if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
178 DEBUG("Failed to write WIM header: %m\n");
179 return WIMLIB_ERR_WRITE;
185 * Initializes the header for a WIM file.
187 int init_header(struct wim_header *hdr, int ctype)
189 memset(hdr, 0, sizeof(struct wim_header));
191 case WIM_COMPRESSION_TYPE_NONE:
194 case WIM_COMPRESSION_TYPE_LZX:
195 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
196 WIM_HDR_FLAG_COMPRESS_LZX;
198 case WIM_COMPRESSION_TYPE_XPRESS:
199 hdr->flags = WIM_HDR_FLAG_COMPRESSION |
200 WIM_HDR_FLAG_COMPRESS_XPRESS;
203 ERROR("Invalid compression type specified (%d)!\n", ctype);
204 return WIMLIB_ERR_INVALID_COMPRESSION_TYPE;
206 randomize_byte_array(hdr->guid, sizeof(hdr->guid));
210 /* Prints information from the header of the WIM file associated with @w. */
211 WIMLIBAPI void wimlib_print_header(const WIMStruct *w)
213 const struct wim_header *hdr = &w->hdr;
214 printf("Magic Characters = MSWIM\\000\\000\\000\n");
215 printf("Header Size = %u\n", WIM_HEADER_DISK_SIZE);
216 printf("Version = 0x%x\n", WIM_VERSION);
217 printf("Flags = 0x%x\n", hdr->flags);
219 if (hdr->flags & WIM_HDR_FLAG_COMPRESS_XPRESS)
220 printf(" COMPRESS XPRESS FLAG is set\n");
222 if (hdr->flags & WIM_HDR_FLAG_COMPRESS_LZX)
223 printf(" COMPRESS LZX FLAG is set\n");
225 printf("Chunk Size = %u\n", WIM_CHUNK_SIZE);
226 fputs ("GUID = ", stdout);
227 print_byte_field(hdr->guid, WIM_GID_LEN);
229 printf("Part Number = %hu\n", 1);
230 printf("Total Parts = %hu\n", 1);
231 printf("Image Count = %u\n", hdr->image_count);
232 printf("Lookup Table Size = %"PRIu64"\n",
233 (u64)hdr->lookup_table_res_entry.size);
234 printf("Lookup Table Flags = 0x%hhx\n",
235 hdr->lookup_table_res_entry.flags);
236 printf("Lookup Table Offset = %"PRIu64"\n",
237 hdr->lookup_table_res_entry.offset);
238 printf("Lookup Table Original_size = %"PRIu64"\n",
239 hdr->lookup_table_res_entry.original_size);
240 printf("XML Data Size = %"PRIu64"\n",
241 (u64)hdr->xml_res_entry.size);
242 printf("XML Data Flags = 0x%hhx\n",
243 hdr->xml_res_entry.flags);
244 printf("XML Data Offset = %"PRIu64"\n",
245 hdr->xml_res_entry.offset);
246 printf("XML Data Original Size = %"PRIu64"\n",
247 hdr->xml_res_entry.original_size);
248 printf("Boot Metadata Size = %"PRIu64"\n",
249 (u64)hdr->boot_metadata_res_entry.size);
250 printf("Boot Metadata Flags = 0x%hhx\n",
251 hdr->boot_metadata_res_entry.flags);
252 printf("Boot Metadata Offset = %"PRIu64"\n",
253 hdr->boot_metadata_res_entry.offset);
254 printf("Boot Metadata Original Size = %"PRIu64"\n",
255 hdr->boot_metadata_res_entry.original_size);
256 printf("Boot Index = %u\n", hdr->boot_idx);
257 printf("Integrity Size = %"PRIu64"\n",
258 (u64)hdr->integrity.size);
259 printf("Integrity Flags = 0x%hhx\n",
260 hdr->integrity.flags);
261 printf("Integrity Offset = %"PRIu64"\n",
262 hdr->integrity.offset);
263 printf("Integrity Original_size = %"PRIu64"\n",
264 hdr->integrity.original_size);