]> wimlib.net Git - wimlib/blob - include/wimlib/resource.h
Clean up pipable WIM extraction
[wimlib] / include / wimlib / resource.h
1 #ifndef _WIMLIB_RESOURCE_H
2 #define _WIMLIB_RESOURCE_H
3
4 #include "wimlib/callback.h"
5 #include "wimlib/list.h"
6 #include "wimlib/sha1.h"
7 #include "wimlib/types.h"
8
9 struct blob_descriptor;
10 struct filedes;
11 struct wim_image_metadata;
12
13 /*
14  * Description of a "resource" in a WIM file.  A "resource" is a standalone,
15  * possibly compressed region of data.  Normally, there is a one-to-one
16  * correspondence between "blobs" (each of which may be the contents of a file,
17  * for example) and resources.  However, a resource with the
18  * WIM_RESHDR_FLAG_SOLID flag set is a "solid" resource that contains multiple
19  * blobs compressed together.
20  */
21 struct wim_resource_descriptor {
22         /* The WIM containing this resource.  @wim->in_fd is expected to be a
23          * file descriptor to the underlying WIM file, opened for reading.  */
24         WIMStruct *wim;
25
26         /* The offset, in bytes, from the start of WIM file at which this
27          * resource starts.  */
28         u64 offset_in_wim;
29
30         /* The size of this resource in the WIM file.  For compressed resources
31          * this is the compressed size, including overhead such as the chunk
32          * table.  */
33         u64 size_in_wim;
34
35         /* The number of bytes of uncompressed data this resource decompresses
36          * to.  */
37         u64 uncompressed_size;
38
39         /* The list of blobs this resource contains.  */
40         struct list_head blob_list;
41
42         /* Flags for this resource (WIM_RESHDR_FLAG_*).  */
43         u32 flags : 8;
44
45         /* [wimlib extension] This flag will be set if the WIM is pipable.  In
46          * such cases, the resource will be in a slightly different format if it
47          * is compressed.  */
48         u32 is_pipable : 1;
49
50         /* Temporary flag.  */
51         u32 raw_copy_ok : 1;
52
53         /* Compression type of this resource.  */
54         u32 compression_type : 22;
55
56         /* Compression chunk size of this resource.  Irrelevant if the resource
57          * is uncompressed.  */
58         u32 chunk_size;
59 };
60
61 /* On-disk version of a WIM resource header.  */
62 struct wim_reshdr_disk {
63         /* Size of the resource as it appears in the WIM file (possibly
64          * compressed).  */
65         u8 size_in_wim[7];
66
67         /* Zero or more of the WIM_RESHDR_FLAG_* flags.  These indicate, for
68          * example, whether the resource is compressed or not.  */
69         u8 flags;
70
71         /* Offset of the resource from the start of the WIM file, in bytes.  */
72         le64 offset_in_wim;
73
74         /* Uncompressed size of the resource, in bytes.  */
75         le64 uncompressed_size;
76 } _packed_attribute;
77
78 /* In-memory version of a WIM resource header (`struct wim_reshdr_disk').  */
79 struct wim_reshdr {
80         u64 size_in_wim : 56;
81         u64 flags : 8;
82         u64 offset_in_wim;
83         u64 uncompressed_size;
84 };
85
86 /* Flags for the `flags' field of WIM resource headers (`struct wim_reshdr').
87  */
88
89 /* Unknown meaning; currently ignored by wimlib.  */
90 #define WIM_RESHDR_FLAG_FREE            0x01
91
92 /* The resource is a metadata resource for a WIM image, or is the blob table or
93  * XML data for the WIM.  */
94 #define WIM_RESHDR_FLAG_METADATA        0x02
95
96 /* The resource is a non-solid resource compressed using the WIM's default
97  * compression type.  */
98 #define WIM_RESHDR_FLAG_COMPRESSED      0x04
99
100 /* Unknown meaning; currently ignored by wimlib.  */
101 #define WIM_RESHDR_FLAG_SPANNED         0x08
102
103 /* The resource is a solid compressed resource which may contain multiple blobs.
104  * This flag is only allowed if the WIM version number is WIM_VERSION_SOLID.  */
105 #define WIM_RESHDR_FLAG_SOLID           0x10
106
107 /* Magic number in the 'uncompressed_size' field of the resource header that
108  * identifies the main entry for a solid resource.  */
109 #define SOLID_RESOURCE_MAGIC_NUMBER     0x100000000ULL
110
111 /* Returns true if the specified WIM resource is compressed (may be either solid
112  * or non-solid)  */
113 static inline bool
114 resource_is_compressed(const struct wim_resource_descriptor *rdesc)
115 {
116         return (rdesc->flags & (WIM_RESHDR_FLAG_COMPRESSED |
117                                 WIM_RESHDR_FLAG_SOLID));
118 }
119
120 static inline void
121 copy_reshdr(struct wim_reshdr *dest, const struct wim_reshdr *src)
122 {
123         memcpy(dest, src, sizeof(struct wim_reshdr));
124 }
125
126 static inline void
127 zero_reshdr(struct wim_reshdr *reshdr)
128 {
129         memset(reshdr, 0, sizeof(struct wim_reshdr));
130 }
131
132 extern void
133 wim_res_hdr_to_desc(const struct wim_reshdr *reshdr, WIMStruct *wim,
134                     struct wim_resource_descriptor *rdesc);
135
136 extern void
137 get_wim_reshdr(const struct wim_reshdr_disk *disk_reshdr,
138                struct wim_reshdr *reshdr);
139
140 void
141 put_wim_reshdr(const struct wim_reshdr *reshdr,
142                struct wim_reshdr_disk *disk_reshdr);
143
144 /* Alternate chunk table format for resources with WIM_RESHDR_FLAG_SOLID set.
145  */
146 struct alt_chunk_table_header_disk {
147         /* Uncompressed size of the resource in bytes.  */
148         le64 res_usize;
149
150         /* Number of bytes each compressed chunk decompresses into, except
151          * possibly the last which decompresses into the remainder.  This
152          * overrides the chunk size specified by the WIM header.  */
153         le32 chunk_size;
154
155         /* Compression format used for compressed chunks:
156          * 0 = None
157          * 1 = XPRESS
158          * 2 = LZX
159          * 3 = LZMS
160          *
161          * This overrides the compression type specified by the WIM header.  */
162         le32 compression_format;
163
164         /* This header is directly followed by a table of compressed sizes of
165          * the chunks (4 bytes per entry).  */
166 } _packed_attribute;
167
168 static inline unsigned int
169 get_chunk_entry_size(u64 res_size, bool is_alt)
170 {
171         if (res_size <= UINT32_MAX || is_alt)
172                 return 4;
173         else
174                 return 8;
175 }
176
177 /* Functions to read blobs  */
178
179 extern int
180 read_partial_wim_blob_into_buf(const struct blob_descriptor *blob,
181                                size_t size, u64 offset, void *buf);
182
183 extern int
184 read_full_blob_into_buf(const struct blob_descriptor *blob, void *buf);
185
186 extern int
187 read_full_blob_into_alloc_buf(const struct blob_descriptor *blob, void **buf_ret);
188
189 extern int
190 wim_reshdr_to_data(const struct wim_reshdr *reshdr,
191                    WIMStruct *wim, void **buf_ret);
192
193 extern int
194 wim_reshdr_to_hash(const struct wim_reshdr *reshdr, WIMStruct *wim,
195                    u8 hash[SHA1_HASH_SIZE]);
196
197 extern int
198 skip_wim_resource(struct wim_resource_descriptor *rdesc);
199
200 /*
201  * Type of callback function for beginning to read a blob.
202  *
203  * @blob:
204  *      Blob that is about to be read.
205  *
206  * @ctx:
207  *      User-provided context.
208  *
209  * Must return 0 on success, a positive error code on failure, or the special
210  * value BEGIN_BLOB_STATUS_SKIP_BLOB to indicate that the blob should not be
211  * read, and read_blob_list() should continue on to the next blob (without
212  * calling @consume_chunk or @end_blob).
213  */
214 typedef int (*read_blob_list_begin_blob_t)(struct blob_descriptor *blob, void *ctx);
215
216 #define BEGIN_BLOB_STATUS_SKIP_BLOB     -1
217
218 /*
219  * Type of callback function for finishing reading a blob.
220  *
221  * @blob:
222  *      Blob that has been fully read, or blob that started being read but could
223  *      not be fully read due to a read error.
224  *
225  * @status:
226  *      0 if reading the blob was successful; otherwise a nonzero error code
227  *      that specifies the return status.
228  *
229  * @ctx:
230  *      User-provided context.
231  */
232 typedef int (*read_blob_list_end_blob_t)(struct blob_descriptor *blob,
233                                          int status,
234                                          void *ctx);
235
236
237 /* Callback functions and contexts for read_blob_list().  */
238 struct read_blob_list_callbacks {
239
240         /* Called when a blob is about to be read.  */
241         read_blob_list_begin_blob_t begin_blob;
242
243         /* Called when a chunk of data has been read.  */
244         consume_data_callback_t consume_chunk;
245
246         /* Called when a blob has been fully read.  A successful call to
247          * @begin_blob will always be matched by a call to @end_blob.  */
248         read_blob_list_end_blob_t end_blob;
249
250         /* Parameter passed to @begin_blob.  */
251         void *begin_blob_ctx;
252
253         /* Parameter passed to @consume_chunk.  */
254         void *consume_chunk_ctx;
255
256         /* Parameter passed to @end_blob.  */
257         void *end_blob_ctx;
258 };
259
260 /* Flags for read_blob_list()  */
261 #define VERIFY_BLOB_HASHES              0x1
262 #define COMPUTE_MISSING_BLOB_HASHES     0x2
263 #define BLOB_LIST_ALREADY_SORTED        0x4
264
265 extern int
266 read_blob_list(struct list_head *blob_list, size_t list_head_offset,
267                const struct read_blob_list_callbacks *cbs, int flags);
268
269 /* Functions to extract blobs.  */
270
271 extern int
272 extract_blob(struct blob_descriptor *blob, u64 size,
273              consume_data_callback_t extract_chunk, void *extract_chunk_arg);
274
275 extern int
276 extract_blob_to_fd(struct blob_descriptor *blob, struct filedes *fd, u64 size);
277
278 extern int
279 extract_full_blob_to_fd(struct blob_descriptor *blob, struct filedes *fd);
280
281 /* Miscellaneous blob functions.  */
282
283 extern int
284 sha1_blob(struct blob_descriptor *blob);
285
286 /* Functions to read/write metadata resources.  */
287
288 extern int
289 read_metadata_resource(struct wim_image_metadata *imd);
290
291 extern int
292 write_metadata_resource(WIMStruct *wim, int image, int write_resource_flags);
293
294 /* Definitions specific to pipable WIM resources.  */
295
296 /* Arbitrary number to begin each blob in the pipable WIM, used for sanity
297  * checking.  */
298 #define PWM_BLOB_MAGIC 0x2b9b9ba2443db9d8ULL
299
300 /* Header that precedes each blob in a pipable WIM.  */
301 struct pwm_blob_hdr {
302         le64 magic;                     /* +0   */
303         le64 uncompressed_size;         /* +8   */
304         u8 hash[SHA1_HASH_SIZE];        /* +16  */
305         le32 flags;                     /* +36  */
306                                         /* +40  */
307 } _packed_attribute;
308
309 /* Header that precedes each chunk of a compressed resource in a pipable WIM.
310  */
311 struct pwm_chunk_hdr {
312         le32 compressed_size;
313 } _packed_attribute;
314
315 #endif /* _WIMLIB_RESOURCE_H */