]> wimlib.net Git - wimlib/blob - src/header.c
wimlib_print_header(): fix printing of magic number
[wimlib] / src / header.c
1 /*
2  * header.c
3  *
4  * Read, write, or print a WIM header.
5  */
6
7 /*
8  * Copyright (C) 2012, 2013, 2015 Eric Biggers
9  *
10  * This file is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU Lesser General Public License as published by the Free
12  * Software Foundation; either version 3 of the License, or (at your option) any
13  * later version.
14  *
15  * This file is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this file; if not, see http://www.gnu.org/licenses/.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include <ctype.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32
33 #include "wimlib.h"
34 #include "wimlib/alloca.h"
35 #include "wimlib/assert.h"
36 #include "wimlib/endianness.h"
37 #include "wimlib/error.h"
38 #include "wimlib/file_io.h"
39 #include "wimlib/header.h"
40 #include "wimlib/util.h"
41 #include "wimlib/wim.h"
42
43 /*
44  * Reads the header from a WIM file.
45  *
46  * @wim
47  *      WIM to read the header from.  @wim->in_fd must be positioned at the
48  *      beginning of the file.
49  *
50  * @hdr
51  *      Structure to read the header into.
52  *
53  * Return values:
54  *      WIMLIB_ERR_SUCCESS (0)
55  *      WIMLIB_ERR_IMAGE_COUNT
56  *      WIMLIB_ERR_INVALID_PART_NUMBER
57  *      WIMLIB_ERR_NOT_A_WIM_FILE
58  *      WIMLIB_ERR_READ
59  *      WIMLIB_ERR_UNEXPECTED_END_OF_FILE
60  *      WIMLIB_ERR_UNKNOWN_VERSION
61  */
62 int
63 read_wim_header(WIMStruct *wim, struct wim_header *hdr)
64 {
65         struct wim_header_disk disk_hdr _aligned_attribute(8);
66         struct filedes *in_fd = &wim->in_fd;
67         const tchar *filename = wim->filename;
68         int ret;
69         tchar *pipe_str;
70
71         wimlib_assert(in_fd->offset == 0);
72
73         if (filename == NULL) {
74                 pipe_str = alloca(40);
75                 tsprintf(pipe_str, T("[fd %d]"), in_fd->fd);
76                 filename = pipe_str;
77         }
78
79         BUILD_BUG_ON(sizeof(struct wim_header_disk) != WIM_HEADER_DISK_SIZE);
80
81         DEBUG("Reading WIM header from \"%"TS"\"", filename);
82
83         ret = full_read(in_fd, &disk_hdr, sizeof(disk_hdr));
84         if (ret)
85                 goto read_error;
86
87         hdr->magic = le64_to_cpu(disk_hdr.magic);
88
89         if (hdr->magic != WIM_MAGIC) {
90                 if (hdr->magic == PWM_MAGIC) {
91                         /* Pipable WIM:  Use header at end instead, unless
92                          * actually reading from a pipe.  */
93                         if (!in_fd->is_pipe) {
94                                 ret = WIMLIB_ERR_READ;
95                                 if (-1 == lseek(in_fd->fd, -WIM_HEADER_DISK_SIZE, SEEK_END))
96                                         goto read_error;
97                                 ret = full_read(in_fd, &disk_hdr, sizeof(disk_hdr));
98                                 if (ret)
99                                         goto read_error;
100                         }
101                 } else {
102                         ERROR("\"%"TS"\": Invalid magic characters in header", filename);
103                         return WIMLIB_ERR_NOT_A_WIM_FILE;
104                 }
105         }
106
107         if (le32_to_cpu(disk_hdr.hdr_size) != sizeof(struct wim_header_disk)) {
108                 ERROR("\"%"TS"\": Header size is invalid (%u bytes)",
109                       filename, le32_to_cpu(disk_hdr.hdr_size));
110                 return WIMLIB_ERR_INVALID_HEADER;
111         }
112
113         hdr->wim_version = le32_to_cpu(disk_hdr.wim_version);
114         if (hdr->wim_version != WIM_VERSION_DEFAULT &&
115             hdr->wim_version != WIM_VERSION_SOLID)
116         {
117                 ERROR("\"%"TS"\": Unknown WIM version: %u",
118                       filename, hdr->wim_version);
119                 return WIMLIB_ERR_UNKNOWN_VERSION;
120         }
121
122         hdr->flags = le32_to_cpu(disk_hdr.wim_flags);
123         hdr->chunk_size = le32_to_cpu(disk_hdr.chunk_size);
124         memcpy(hdr->guid, disk_hdr.guid, WIM_GUID_LEN);
125         hdr->part_number = le16_to_cpu(disk_hdr.part_number);
126         hdr->total_parts = le16_to_cpu(disk_hdr.total_parts);
127
128         if (hdr->total_parts == 0 || hdr->part_number == 0 ||
129             hdr->part_number > hdr->total_parts)
130         {
131                 ERROR("\"%"TS"\": Invalid WIM part number: %hu of %hu",
132                       filename, hdr->part_number, hdr->total_parts);
133                 return WIMLIB_ERR_INVALID_PART_NUMBER;
134         }
135
136         hdr->image_count = le32_to_cpu(disk_hdr.image_count);
137
138         DEBUG("part_number = %u, total_parts = %u, image_count = %u",
139               hdr->part_number, hdr->total_parts, hdr->image_count);
140
141         if (unlikely(hdr->image_count > MAX_IMAGES)) {
142                 ERROR("\"%"TS"\": Invalid image count (%u)",
143                       filename, hdr->image_count);
144                 return WIMLIB_ERR_IMAGE_COUNT;
145         }
146
147         get_wim_reshdr(&disk_hdr.blob_table_reshdr, &hdr->blob_table_reshdr);
148         get_wim_reshdr(&disk_hdr.xml_data_reshdr, &hdr->xml_data_reshdr);
149         get_wim_reshdr(&disk_hdr.boot_metadata_reshdr, &hdr->boot_metadata_reshdr);
150         hdr->boot_idx = le32_to_cpu(disk_hdr.boot_idx);
151         get_wim_reshdr(&disk_hdr.integrity_table_reshdr, &hdr->integrity_table_reshdr);
152         return 0;
153
154 read_error:
155         ERROR_WITH_ERRNO("\"%"TS"\": Error reading header", filename);
156         return ret;
157 }
158
159 /* Writes the header for a WIM file at the specified offset.  If the offset
160  * specified is the current one, the position is advanced by the size of the
161  * header.  */
162 int
163 write_wim_header_at_offset(const struct wim_header *hdr, struct filedes *out_fd,
164                            off_t offset)
165 {
166         struct wim_header_disk disk_hdr _aligned_attribute(8);
167         int ret;
168
169         DEBUG("Writing %sWIM header at offset %"PRIu64,
170               ((hdr->magic == PWM_MAGIC) ? "pipable " : ""),
171               offset);
172
173         disk_hdr.magic = cpu_to_le64(hdr->magic);
174         disk_hdr.hdr_size = cpu_to_le32(sizeof(struct wim_header_disk));
175         disk_hdr.wim_version = cpu_to_le32(hdr->wim_version);
176         disk_hdr.wim_flags = cpu_to_le32(hdr->flags);
177         if (hdr->flags & WIM_HDR_FLAG_COMPRESSION)
178                 disk_hdr.chunk_size = cpu_to_le32(hdr->chunk_size);
179         else
180                 disk_hdr.chunk_size = 0;
181         memcpy(disk_hdr.guid, hdr->guid, WIM_GUID_LEN);
182
183         disk_hdr.part_number = cpu_to_le16(hdr->part_number);
184         disk_hdr.total_parts = cpu_to_le16(hdr->total_parts);
185         disk_hdr.image_count = cpu_to_le32(hdr->image_count);
186         put_wim_reshdr(&hdr->blob_table_reshdr, &disk_hdr.blob_table_reshdr);
187         put_wim_reshdr(&hdr->xml_data_reshdr, &disk_hdr.xml_data_reshdr);
188         put_wim_reshdr(&hdr->boot_metadata_reshdr, &disk_hdr.boot_metadata_reshdr);
189         disk_hdr.boot_idx = cpu_to_le32(hdr->boot_idx);
190         put_wim_reshdr(&hdr->integrity_table_reshdr, &disk_hdr.integrity_table_reshdr);
191         memset(disk_hdr.unused, 0, sizeof(disk_hdr.unused));
192
193         if (offset == out_fd->offset)
194                 ret = full_write(out_fd, &disk_hdr, sizeof(disk_hdr));
195         else
196                 ret = full_pwrite(out_fd, &disk_hdr, sizeof(disk_hdr), offset);
197         if (ret)
198                 ERROR_WITH_ERRNO("Failed to write WIM header");
199         return ret;
200 }
201
202 /* Writes the header for a WIM file at the output file descriptor's current
203  * offset.  */
204 int
205 write_wim_header(const struct wim_header *hdr, struct filedes *out_fd)
206 {
207         return write_wim_header_at_offset(hdr, out_fd, out_fd->offset);
208 }
209
210 /* Update just the wim_flags field. */
211 int
212 write_wim_header_flags(u32 hdr_flags, struct filedes *out_fd)
213 {
214         le32 flags = cpu_to_le32(hdr_flags);
215
216         return full_pwrite(out_fd, &flags, sizeof(flags),
217                            offsetof(struct wim_header_disk, wim_flags));
218 }
219
220 void
221 set_wim_hdr_cflags(enum wimlib_compression_type ctype, struct wim_header *hdr)
222 {
223         hdr->flags &= ~(WIM_HDR_FLAG_COMPRESSION |
224                         WIM_HDR_FLAG_COMPRESS_RESERVED |
225                         WIM_HDR_FLAG_COMPRESS_XPRESS |
226                         WIM_HDR_FLAG_COMPRESS_LZX |
227                         WIM_HDR_FLAG_COMPRESS_LZMS |
228                         WIM_HDR_FLAG_COMPRESS_XPRESS_2);
229         switch (ctype) {
230         case WIMLIB_COMPRESSION_TYPE_NONE:
231                 return;
232         case WIMLIB_COMPRESSION_TYPE_XPRESS:
233                 hdr->flags |= WIM_HDR_FLAG_COMPRESSION | WIM_HDR_FLAG_COMPRESS_XPRESS;
234                 return;
235         case WIMLIB_COMPRESSION_TYPE_LZX:
236                 hdr->flags |= WIM_HDR_FLAG_COMPRESSION | WIM_HDR_FLAG_COMPRESS_LZX;
237                 return;
238         case WIMLIB_COMPRESSION_TYPE_LZMS:
239                 hdr->flags |= WIM_HDR_FLAG_COMPRESSION | WIM_HDR_FLAG_COMPRESS_LZMS;
240                 return;
241         }
242         wimlib_assert(0);
243 }
244
245 /* Initialize the header for a WIM file.  */
246 void
247 init_wim_header(struct wim_header *hdr,
248                 enum wimlib_compression_type ctype, u32 chunk_size)
249 {
250         memset(hdr, 0, sizeof(struct wim_header));
251         hdr->magic = WIM_MAGIC;
252         if (ctype == WIMLIB_COMPRESSION_TYPE_LZMS)
253                 hdr->wim_version = WIM_VERSION_SOLID;
254         else
255                 hdr->wim_version = WIM_VERSION_DEFAULT;
256         set_wim_hdr_cflags(ctype, hdr);
257         hdr->chunk_size = chunk_size;
258         hdr->total_parts = 1;
259         hdr->part_number = 1;
260         randomize_byte_array(hdr->guid, sizeof(hdr->guid));
261 }
262
263 static const struct {
264         u32 flag;
265         const char *name;
266 } hdr_flags[] = {
267         {WIM_HDR_FLAG_RESERVED,         "RESERVED"},
268         {WIM_HDR_FLAG_COMPRESSION,      "COMPRESSION"},
269         {WIM_HDR_FLAG_READONLY,         "READONLY"},
270         {WIM_HDR_FLAG_SPANNED,          "SPANNED"},
271         {WIM_HDR_FLAG_RESOURCE_ONLY,    "RESOURCE_ONLY"},
272         {WIM_HDR_FLAG_METADATA_ONLY,    "METADATA_ONLY"},
273         {WIM_HDR_FLAG_WRITE_IN_PROGRESS,"WRITE_IN_PROGRESS"},
274         {WIM_HDR_FLAG_RP_FIX,           "RP_FIX"},
275         {WIM_HDR_FLAG_COMPRESS_RESERVED,"COMPRESS_RESERVED"},
276         {WIM_HDR_FLAG_COMPRESS_LZX,     "COMPRESS_LZX"},
277         {WIM_HDR_FLAG_COMPRESS_XPRESS,  "COMPRESS_XPRESS"},
278         {WIM_HDR_FLAG_COMPRESS_LZMS,    "COMPRESS_LZMS"},
279         {WIM_HDR_FLAG_COMPRESS_XPRESS_2,"COMPRESS_XPRESS_2"},
280 };
281
282 /* API function documented in wimlib.h  */
283 WIMLIBAPI void
284 wimlib_print_header(const WIMStruct *wim)
285 {
286         const struct wim_header *hdr = &wim->hdr;
287
288         tprintf(T("Magic Characters            = "));
289         for (int i = 0; i < sizeof(hdr->magic); i++) {
290                 tchar c = (u8)(hdr->magic >> ((8 * i)));
291                 if (istalpha(c))
292                         tputchar(c);
293                 else
294                         tprintf(T("\\%o"), c);
295         }
296         tputchar(T('\n'));
297         tprintf(T("Header Size                 = %u\n"), WIM_HEADER_DISK_SIZE);
298         tprintf(T("Version                     = 0x%x\n"), hdr->wim_version);
299
300         tprintf(T("Flags                       = 0x%x\n"), hdr->flags);
301         for (size_t i = 0; i < ARRAY_LEN(hdr_flags); i++)
302                 if (hdr_flags[i].flag & hdr->flags)
303                         tprintf(T("    WIM_HDR_FLAG_%s is set\n"), hdr_flags[i].name);
304
305         tprintf(T("Chunk Size                  = %u\n"), hdr->chunk_size);
306         tfputs (T("GUID                        = "), stdout);
307         print_byte_field(hdr->guid, WIM_GUID_LEN, stdout);
308         tputchar(T('\n'));
309         tprintf(T("Part Number                 = %hu\n"), hdr->part_number);
310         tprintf(T("Total Parts                 = %hu\n"), hdr->total_parts);
311         tprintf(T("Image Count                 = %u\n"), hdr->image_count);
312         tprintf(T("Blob Table Size             = %"PRIu64"\n"),
313                                 (u64)hdr->blob_table_reshdr.size_in_wim);
314         tprintf(T("Blob Table Flags            = 0x%hhx\n"),
315                                 (u8)hdr->blob_table_reshdr.flags);
316         tprintf(T("Blob Table Offset           = %"PRIu64"\n"),
317                                 hdr->blob_table_reshdr.offset_in_wim);
318         tprintf(T("Blob Table Original_size    = %"PRIu64"\n"),
319                                 hdr->blob_table_reshdr.uncompressed_size);
320         tprintf(T("XML Data Size               = %"PRIu64"\n"),
321                                 (u64)hdr->xml_data_reshdr.size_in_wim);
322         tprintf(T("XML Data Flags              = 0x%hhx\n"),
323                                 (u8)hdr->xml_data_reshdr.flags);
324         tprintf(T("XML Data Offset             = %"PRIu64"\n"),
325                                 hdr->xml_data_reshdr.offset_in_wim);
326         tprintf(T("XML Data Original Size      = %"PRIu64"\n"),
327                                 hdr->xml_data_reshdr.uncompressed_size);
328         tprintf(T("Boot Metadata Size          = %"PRIu64"\n"),
329                                 (u64)hdr->boot_metadata_reshdr.size_in_wim);
330         tprintf(T("Boot Metadata Flags         = 0x%hhx\n"),
331                                 (u8)hdr->boot_metadata_reshdr.flags);
332         tprintf(T("Boot Metadata Offset        = %"PRIu64"\n"),
333                                 hdr->boot_metadata_reshdr.offset_in_wim);
334         tprintf(T("Boot Metadata Original Size = %"PRIu64"\n"),
335                                 hdr->boot_metadata_reshdr.uncompressed_size);
336         tprintf(T("Boot Index                  = %u\n"), hdr->boot_idx);
337         tprintf(T("Integrity Size              = %"PRIu64"\n"),
338                                 (u64)hdr->integrity_table_reshdr.size_in_wim);
339         tprintf(T("Integrity Flags             = 0x%hhx\n"),
340                                 (u8)hdr->integrity_table_reshdr.flags);
341         tprintf(T("Integrity Offset            = %"PRIu64"\n"),
342                                 hdr->integrity_table_reshdr.offset_in_wim);
343         tprintf(T("Integrity Original_size     = %"PRIu64"\n"),
344                                 hdr->integrity_table_reshdr.uncompressed_size);
345 }