]> wimlib.net Git - wimlib/blob - src/header.c
mount_image.c: add fallback definitions of RENAME_* constants
[wimlib] / src / header.c
1 /*
2  * header.c
3  *
4  * Read, write, or print a WIM header.
5  */
6
7 /*
8  * Copyright 2012-2023 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 https://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 __attribute__((aligned(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         STATIC_ASSERT(sizeof(struct wim_header_disk) == WIM_HEADER_DISK_SIZE);
80
81         ret = full_read(in_fd, &disk_hdr, sizeof(disk_hdr));
82         if (ret)
83                 goto read_error;
84
85         hdr->magic = le64_to_cpu(disk_hdr.magic);
86
87         if (hdr->magic != WIM_MAGIC) {
88                 if (hdr->magic == PWM_MAGIC) {
89                         /* Pipable WIM:  Use header at end instead, unless
90                          * actually reading from a pipe.  */
91                         if (!in_fd->is_pipe) {
92                                 ret = WIMLIB_ERR_READ;
93                                 if (-1 == lseek(in_fd->fd, -WIM_HEADER_DISK_SIZE, SEEK_END))
94                                         goto read_error;
95                                 ret = full_read(in_fd, &disk_hdr, sizeof(disk_hdr));
96                                 if (ret)
97                                         goto read_error;
98                         }
99                 } else {
100                         ERROR("\"%"TS"\": Invalid magic characters in header", filename);
101                         return WIMLIB_ERR_NOT_A_WIM_FILE;
102                 }
103         }
104
105         if (le32_to_cpu(disk_hdr.hdr_size) != sizeof(struct wim_header_disk)) {
106                 ERROR("\"%"TS"\": Header size is invalid (%u bytes)",
107                       filename, le32_to_cpu(disk_hdr.hdr_size));
108                 return WIMLIB_ERR_INVALID_HEADER;
109         }
110
111         hdr->wim_version = le32_to_cpu(disk_hdr.wim_version);
112         if (hdr->wim_version != WIM_VERSION_DEFAULT &&
113             hdr->wim_version != WIM_VERSION_SOLID)
114         {
115                 ERROR("\"%"TS"\": Unknown WIM version: %u",
116                       filename, hdr->wim_version);
117                 return WIMLIB_ERR_UNKNOWN_VERSION;
118         }
119
120         hdr->flags = le32_to_cpu(disk_hdr.wim_flags);
121         hdr->chunk_size = le32_to_cpu(disk_hdr.chunk_size);
122         copy_guid(hdr->guid, disk_hdr.guid);
123         hdr->part_number = le16_to_cpu(disk_hdr.part_number);
124         hdr->total_parts = le16_to_cpu(disk_hdr.total_parts);
125
126         if (hdr->total_parts == 0 || hdr->part_number == 0 ||
127             hdr->part_number > hdr->total_parts)
128         {
129                 ERROR("\"%"TS"\": Invalid WIM part number: %hu of %hu",
130                       filename, hdr->part_number, hdr->total_parts);
131                 return WIMLIB_ERR_INVALID_PART_NUMBER;
132         }
133
134         hdr->image_count = le32_to_cpu(disk_hdr.image_count);
135
136         if (unlikely(hdr->image_count > MAX_IMAGES)) {
137                 ERROR("\"%"TS"\": Invalid image count (%u)",
138                       filename, hdr->image_count);
139                 return WIMLIB_ERR_IMAGE_COUNT;
140         }
141
142         get_wim_reshdr(&disk_hdr.blob_table_reshdr, &hdr->blob_table_reshdr);
143         get_wim_reshdr(&disk_hdr.xml_data_reshdr, &hdr->xml_data_reshdr);
144         get_wim_reshdr(&disk_hdr.boot_metadata_reshdr, &hdr->boot_metadata_reshdr);
145         hdr->boot_idx = le32_to_cpu(disk_hdr.boot_idx);
146         get_wim_reshdr(&disk_hdr.integrity_table_reshdr, &hdr->integrity_table_reshdr);
147
148         /*
149          * Prevent huge memory allocations when processing fuzzed files.  The
150          * blob table, XML data, and integrity table are all uncompressed, so
151          * they should never be larger than the WIM file itself.
152          */
153         if (wim->file_size > 0 &&
154             (hdr->blob_table_reshdr.uncompressed_size > wim->file_size ||
155              hdr->xml_data_reshdr.uncompressed_size > wim->file_size ||
156              hdr->integrity_table_reshdr.uncompressed_size > wim->file_size))
157                 return WIMLIB_ERR_INVALID_HEADER;
158
159         return 0;
160
161 read_error:
162         ERROR_WITH_ERRNO("\"%"TS"\": Error reading header", filename);
163         return ret;
164 }
165
166 /* Writes the header for a WIM file at the specified offset.  If the offset
167  * specified is the current one, the position is advanced by the size of the
168  * header.  */
169 int
170 write_wim_header(const struct wim_header *hdr, struct filedes *out_fd,
171                  off_t offset)
172 {
173         struct wim_header_disk disk_hdr __attribute__((aligned(8)));
174         int ret;
175
176         disk_hdr.magic = cpu_to_le64(hdr->magic);
177         disk_hdr.hdr_size = cpu_to_le32(sizeof(struct wim_header_disk));
178         disk_hdr.wim_version = cpu_to_le32(hdr->wim_version);
179         disk_hdr.wim_flags = cpu_to_le32(hdr->flags);
180         disk_hdr.chunk_size = cpu_to_le32(hdr->chunk_size);
181         copy_guid(disk_hdr.guid, hdr->guid);
182         disk_hdr.part_number = cpu_to_le16(hdr->part_number);
183         disk_hdr.total_parts = cpu_to_le16(hdr->total_parts);
184         disk_hdr.image_count = cpu_to_le32(hdr->image_count);
185         put_wim_reshdr(&hdr->blob_table_reshdr, &disk_hdr.blob_table_reshdr);
186         put_wim_reshdr(&hdr->xml_data_reshdr, &disk_hdr.xml_data_reshdr);
187         put_wim_reshdr(&hdr->boot_metadata_reshdr, &disk_hdr.boot_metadata_reshdr);
188         disk_hdr.boot_idx = cpu_to_le32(hdr->boot_idx);
189         put_wim_reshdr(&hdr->integrity_table_reshdr, &disk_hdr.integrity_table_reshdr);
190         memset(disk_hdr.unused, 0, sizeof(disk_hdr.unused));
191
192         if (offset == out_fd->offset)
193                 ret = full_write(out_fd, &disk_hdr, sizeof(disk_hdr));
194         else
195                 ret = full_pwrite(out_fd, &disk_hdr, sizeof(disk_hdr), offset);
196         if (ret)
197                 ERROR_WITH_ERRNO("Failed to write WIM header");
198         return ret;
199 }
200
201 /* Update just the wim_flags field. */
202 int
203 write_wim_header_flags(u32 hdr_flags, struct filedes *out_fd)
204 {
205         le32 flags = cpu_to_le32(hdr_flags);
206
207         return full_pwrite(out_fd, &flags, sizeof(flags),
208                            offsetof(struct wim_header_disk, wim_flags));
209 }
210
211 static const struct {
212         u32 flag;
213         const char *name;
214 } hdr_flags[] = {
215         {WIM_HDR_FLAG_RESERVED,         "RESERVED"},
216         {WIM_HDR_FLAG_COMPRESSION,      "COMPRESSION"},
217         {WIM_HDR_FLAG_READONLY,         "READONLY"},
218         {WIM_HDR_FLAG_SPANNED,          "SPANNED"},
219         {WIM_HDR_FLAG_RESOURCE_ONLY,    "RESOURCE_ONLY"},
220         {WIM_HDR_FLAG_METADATA_ONLY,    "METADATA_ONLY"},
221         {WIM_HDR_FLAG_WRITE_IN_PROGRESS,"WRITE_IN_PROGRESS"},
222         {WIM_HDR_FLAG_RP_FIX,           "RP_FIX"},
223         {WIM_HDR_FLAG_COMPRESS_RESERVED,"COMPRESS_RESERVED"},
224         {WIM_HDR_FLAG_COMPRESS_LZX,     "COMPRESS_LZX"},
225         {WIM_HDR_FLAG_COMPRESS_XPRESS,  "COMPRESS_XPRESS"},
226         {WIM_HDR_FLAG_COMPRESS_LZMS,    "COMPRESS_LZMS"},
227         {WIM_HDR_FLAG_COMPRESS_XPRESS_2,"COMPRESS_XPRESS_2"},
228 };
229
230 /* API function documented in wimlib.h  */
231 WIMLIBAPI void
232 wimlib_print_header(const WIMStruct *wim)
233 {
234         const struct wim_header *hdr = &wim->hdr;
235
236         tprintf(T("Magic Characters            = "));
237         for (int i = 0; i < sizeof(hdr->magic); i++) {
238                 tchar c = (u8)(hdr->magic >> ((8 * i)));
239                 if (istalpha(c))
240                         tputchar(c);
241                 else
242                         tprintf(T("\\%o"), c);
243         }
244         tputchar(T('\n'));
245         tprintf(T("Header Size                 = %u\n"), WIM_HEADER_DISK_SIZE);
246         tprintf(T("Version                     = 0x%x\n"), hdr->wim_version);
247
248         tprintf(T("Flags                       = 0x%x\n"), hdr->flags);
249         for (size_t i = 0; i < ARRAY_LEN(hdr_flags); i++)
250                 if (hdr_flags[i].flag & hdr->flags)
251                         tprintf(T("    WIM_HDR_FLAG_%s is set\n"), hdr_flags[i].name);
252
253         tprintf(T("Chunk Size                  = %u\n"), hdr->chunk_size);
254         tfputs (T("GUID                        = "), stdout);
255         print_byte_field(hdr->guid, GUID_SIZE, stdout);
256         tputchar(T('\n'));
257         tprintf(T("Part Number                 = %hu\n"), hdr->part_number);
258         tprintf(T("Total Parts                 = %hu\n"), hdr->total_parts);
259         tprintf(T("Image Count                 = %u\n"), hdr->image_count);
260         tprintf(T("Blob Table Size             = %"PRIu64"\n"),
261                                 (u64)hdr->blob_table_reshdr.size_in_wim);
262         tprintf(T("Blob Table Flags            = 0x%hhx\n"),
263                                 (u8)hdr->blob_table_reshdr.flags);
264         tprintf(T("Blob Table Offset           = %"PRIu64"\n"),
265                                 hdr->blob_table_reshdr.offset_in_wim);
266         tprintf(T("Blob Table Original_size    = %"PRIu64"\n"),
267                                 hdr->blob_table_reshdr.uncompressed_size);
268         tprintf(T("XML Data Size               = %"PRIu64"\n"),
269                                 (u64)hdr->xml_data_reshdr.size_in_wim);
270         tprintf(T("XML Data Flags              = 0x%hhx\n"),
271                                 (u8)hdr->xml_data_reshdr.flags);
272         tprintf(T("XML Data Offset             = %"PRIu64"\n"),
273                                 hdr->xml_data_reshdr.offset_in_wim);
274         tprintf(T("XML Data Original Size      = %"PRIu64"\n"),
275                                 hdr->xml_data_reshdr.uncompressed_size);
276         tprintf(T("Boot Metadata Size          = %"PRIu64"\n"),
277                                 (u64)hdr->boot_metadata_reshdr.size_in_wim);
278         tprintf(T("Boot Metadata Flags         = 0x%hhx\n"),
279                                 (u8)hdr->boot_metadata_reshdr.flags);
280         tprintf(T("Boot Metadata Offset        = %"PRIu64"\n"),
281                                 hdr->boot_metadata_reshdr.offset_in_wim);
282         tprintf(T("Boot Metadata Original Size = %"PRIu64"\n"),
283                                 hdr->boot_metadata_reshdr.uncompressed_size);
284         tprintf(T("Boot Index                  = %u\n"), hdr->boot_idx);
285         tprintf(T("Integrity Size              = %"PRIu64"\n"),
286                                 (u64)hdr->integrity_table_reshdr.size_in_wim);
287         tprintf(T("Integrity Flags             = 0x%hhx\n"),
288                                 (u8)hdr->integrity_table_reshdr.flags);
289         tprintf(T("Integrity Offset            = %"PRIu64"\n"),
290                                 hdr->integrity_table_reshdr.offset_in_wim);
291         tprintf(T("Integrity Original_size     = %"PRIu64"\n"),
292                                 hdr->integrity_table_reshdr.uncompressed_size);
293 }