]> wimlib.net Git - wimlib/blob - src/header.c
Clean up file headers
[wimlib] / src / header.c
1 /*
2  * header.c
3  *
4  * Read, write, or create a WIM header.
5  */
6
7 /*
8  * Copyright (C) 2010 Carl Thijssen
9  * Copyright (C) 2012 Eric Biggers
10  *
11  * This file is part of wimlib, a library for working with WIM files.
12  *
13  * wimlib is free software; you can redistribute it and/or modify it under the
14  * terms of the GNU Lesser General Public License as published by the Free
15  * Software Foundation; either version 2.1 of the License, or (at your option)
16  * any later version.
17  *
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 Lesser General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License
24  * along with wimlib; if not, see http://www.gnu.org/licenses/.
25  */
26
27 #include "wimlib_internal.h"
28 #include "io.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 for a WIM file.  */
35 int read_header(FILE *fp, struct wim_header *hdr, int split_ok)
36 {
37         size_t bytes_read;
38         u8 buf[WIM_HEADER_DISK_SIZE];
39         size_t hdr_rem_size;
40         const u8 *p;
41
42         u32 hdr_size;
43         u32 wim_version;
44         u32 chunk_size;
45         u16 part_number;
46         u16 total_parts;
47
48         DEBUG("Reading WIM header.\n");
49         
50         bytes_read = fread(buf, 1, WIM_MAGIC_LEN, fp);
51
52         if (bytes_read != WIM_MAGIC_LEN)
53                 goto err;
54
55         /* Byte 8 */
56
57         if (memcmp(buf, wim_magic_chars, WIM_MAGIC_LEN) != 0) {
58                 ERROR("Invalid magic characters in WIM header\n");
59                 return WIMLIB_ERR_NOT_A_WIM_FILE;
60         }
61
62         bytes_read = fread(&hdr_size, 1, sizeof(u32), fp);
63         if (bytes_read != sizeof(u32))
64                 goto err;
65
66         TO_LE32(hdr_size);
67
68         /* Byte 12 */
69
70         if (hdr_size != WIM_HEADER_DISK_SIZE) {
71                 DEBUG("ERROR: Header is size %u (expected %u)\n",
72                                 hdr_size, WIM_HEADER_DISK_SIZE);
73                 return WIMLIB_ERR_INVALID_HEADER_SIZE;
74         }
75
76         /* Read the rest of the header into a buffer. */
77
78         hdr_rem_size = WIM_HEADER_DISK_SIZE - WIM_MAGIC_LEN - sizeof(u32);
79
80         bytes_read = fread(buf + WIM_MAGIC_LEN + sizeof(u32), 1, 
81                            hdr_rem_size, fp);
82         if (bytes_read != hdr_rem_size)
83                 goto err;
84
85         p = get_u32(buf + WIM_MAGIC_LEN + sizeof(u32), &wim_version);
86
87         if (wim_version != WIM_VERSION) {
88                 ERROR("The WIM header says the WIM version is %u, but Wimlib "
89                         "only knows about version %u.\n", wim_version, 
90                                                                 WIM_VERSION);
91                 return WIMLIB_ERR_UNKNOWN_VERSION;
92         }
93
94         p = get_u32(p, &hdr->flags);
95         p = get_u32(p, &chunk_size);
96         if (chunk_size != WIM_CHUNK_SIZE && 
97                         (hdr->flags & WIM_HDR_FLAG_COMPRESSION)) {
98                 ERROR("Unexpected chunk size of %u! Ask the author to "
99                                 "implement support for other chunk sizes. "
100                                 "(Or it might just be that the WIM header is "
101                                 "invalid.)\n", chunk_size);
102                 return WIMLIB_ERR_INVALID_CHUNK_SIZE;
103         }
104
105         p = get_bytes(p, WIM_GID_LEN, hdr->guid);
106         p = get_u16(p, &hdr->part_number);
107         p = get_u16(p, &hdr->total_parts);
108
109         if (!split_ok && (hdr->part_number != 1 || hdr->total_parts != 1)) {
110                 ERROR("This WIM is part %u of a %u-part WIM.\n",
111                         hdr->part_number, hdr->total_parts);
112                 return WIMLIB_ERR_SPLIT_UNSUPPORTED;
113         }
114
115         p = get_u32(p, &hdr->image_count);
116
117         DEBUG("part_number = %u, total_parts = %u, image_count = %u\n",
118                         hdr->part_number, hdr->total_parts, hdr->image_count);
119
120         /* Byte 48 */
121
122         p = get_resource_entry(p, &hdr->lookup_table_res_entry);
123         p = get_resource_entry(p, &hdr->xml_res_entry);
124         p = get_resource_entry(p, &hdr->boot_metadata_res_entry);
125
126         /* Byte 120 */
127
128         p = get_u32(p, &hdr->boot_idx);
129
130         /* Byte 124 */
131
132         p = get_resource_entry(p, &hdr->integrity);
133
134         /* Byte 148 */
135
136         /* 60 bytes of unused stuff. */
137
138         /* Byte 208 */
139
140         return 0;
141
142 err:
143         if (feof(fp))
144                 ERROR("Unexpected EOF while reading WIM header!\n");
145         else
146                 ERROR("Error reading WIM header: %m\n");
147         return WIMLIB_ERR_READ;
148 }
149
150 /* 
151  * Writes the header for a WIM file.
152  *
153  * @hdr:        A pointer to a struct wim_header structure that describes the header.
154  * @out:        The FILE* for the output file, positioned at the appropriate
155  *              place (the beginning of the file).
156  * @return:     True on success, false on failure.
157  */
158 int write_header(const struct wim_header *hdr, FILE *out)
159 {
160         u8 buf[WIM_HEADER_DISK_SIZE];
161         u8 *p;
162         DEBUG("Writing WIM header.\n");
163
164         p = put_bytes(buf, WIM_MAGIC_LEN, wim_magic_chars);
165         p = put_u32(p, WIM_HEADER_DISK_SIZE);
166         p = put_u32(p, WIM_VERSION);
167         p = put_u32(p, hdr->flags);
168         p = put_u32(p, (hdr->flags & WIM_HDR_FLAG_COMPRESSION) ? 
169                                 WIM_CHUNK_SIZE : 0);
170         /* byte 24 */
171
172         p = put_bytes(p, WIM_GID_LEN, hdr->guid);
173         p = put_u16(p, hdr->part_number);
174
175         /* byte 40 */
176
177         p = put_u16(p, hdr->total_parts);
178         p = put_u32(p, hdr->image_count);
179         p = put_resource_entry(p, &hdr->lookup_table_res_entry);
180         p = put_resource_entry(p, &hdr->xml_res_entry);
181         p = put_resource_entry(p, &hdr->boot_metadata_res_entry);
182         p = put_u32(p, hdr->boot_idx);
183         p = put_resource_entry(p, &hdr->integrity);
184         memset(p, 0, WIM_UNUSED_LEN);
185         if (fwrite(buf, 1, sizeof(buf), out) != sizeof(buf)) {
186                 DEBUG("Failed to write WIM header: %m\n");
187                 return WIMLIB_ERR_WRITE;
188         }
189         return 0;
190 }
191
192 /*
193  * Initializes the header for a WIM file.
194  */
195 int init_header(struct wim_header *hdr, int ctype)
196 {
197         memset(hdr, 0, sizeof(struct wim_header));
198         switch (ctype) {
199         case WIM_COMPRESSION_TYPE_NONE:
200                 hdr->flags = 0;
201                 break;
202         case WIM_COMPRESSION_TYPE_LZX:
203                 hdr->flags = WIM_HDR_FLAG_COMPRESSION | 
204                         WIM_HDR_FLAG_COMPRESS_LZX;
205                 break;
206         case WIM_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)!\n", 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 wimlib_print_header(const WIMStruct *w)
240 {
241         const struct wim_header *hdr = &w->hdr;
242         uint i;
243
244         printf("Magic Characters            = MSWIM\\000\\000\\000\n");
245         printf("Header Size                 = %u\n", WIM_HEADER_DISK_SIZE);
246         printf("Version                     = 0x%x\n", WIM_VERSION);
247
248         printf("Flags                       = 0x%x\n", hdr->flags);
249         for (i = 0; i < ARRAY_LEN(hdr_flags); i++)
250                 if (hdr_flags[i].flag & hdr->flags)
251                         printf("    WIM_HDR_FLAG_%s is set\n", hdr_flags[i].name);
252
253         printf("Chunk Size                  = %u\n", WIM_CHUNK_SIZE);
254         fputs ("GUID                        = ", stdout);
255         print_byte_field(hdr->guid, WIM_GID_LEN);
256         putchar('\n');
257         printf("Part Number                 = %hu\n", 1);
258         printf("Total Parts                 = %hu\n", 1);
259         printf("Image Count                 = %u\n", hdr->image_count);
260         printf("Lookup Table Size           = %"PRIu64"\n", 
261                                 (u64)hdr->lookup_table_res_entry.size);
262         printf("Lookup Table Flags          = 0x%hhx\n", 
263                                 hdr->lookup_table_res_entry.flags);
264         printf("Lookup Table Offset         = %"PRIu64"\n",
265                                 hdr->lookup_table_res_entry.offset);
266         printf("Lookup Table Original_size  = %"PRIu64"\n", 
267                                 hdr->lookup_table_res_entry.original_size);
268         printf("XML Data Size               = %"PRIu64"\n", 
269                                 (u64)hdr->xml_res_entry.size);
270         printf("XML Data Flags              = 0x%hhx\n", 
271                                 hdr->xml_res_entry.flags);
272         printf("XML Data Offset             = %"PRIu64"\n", 
273                                 hdr->xml_res_entry.offset);
274         printf("XML Data Original Size      = %"PRIu64"\n", 
275                                 hdr->xml_res_entry.original_size);
276         printf("Boot Metadata Size          = %"PRIu64"\n", 
277                                 (u64)hdr->boot_metadata_res_entry.size);
278         printf("Boot Metadata Flags         = 0x%hhx\n", 
279                                 hdr->boot_metadata_res_entry.flags);
280         printf("Boot Metadata Offset        = %"PRIu64"\n", 
281                                 hdr->boot_metadata_res_entry.offset);
282         printf("Boot Metadata Original Size = %"PRIu64"\n", 
283                                 hdr->boot_metadata_res_entry.original_size);
284         printf("Boot Index                  = %u\n", hdr->boot_idx);
285         printf("Integrity Size              = %"PRIu64"\n", 
286                                         (u64)hdr->integrity.size);
287         printf("Integrity Flags             = 0x%hhx\n", 
288                                         hdr->integrity.flags);
289         printf("Integrity Offset            = %"PRIu64"\n", 
290                                         hdr->integrity.offset);
291         printf("Integrity Original_size     = %"PRIu64"\n", 
292                                         hdr->integrity.original_size);
293 }