]> wimlib.net Git - wimlib/blob - include/wimlib/resource.h
extract.c: Pass orig stream to callbacks when reading tmpfile
[wimlib] / include / wimlib / resource.h
1 #ifndef _WIMLIB_RESOURCE_H
2 #define _WIMLIB_RESOURCE_H
3
4 #include "wimlib/callback.h"
5 #include "wimlib/file_io.h"
6 #include "wimlib/list.h"
7 #include "wimlib/sha1.h"
8 #include "wimlib/types.h"
9
10 struct wim_lookup_table_entry;
11 struct wim_image_metadata;
12
13 /* Specification of a resource in a WIM file.
14  *
15  * If a `struct wim_lookup_table_entry' lte has
16  * (lte->resource_location == RESOURCE_IN_WIM), then lte->wim_res_spec points to
17  * an instance of this structure.
18  *
19  * Normally, there is a one-to-one correspondence between WIM lookup table
20  * entries ("streams", each of which may be the contents of a file, for example)
21  * and WIM resources.  However, WIM resources with the
22  * WIM_RESHDR_FLAG_PACKED_STREAMS flag set may actually contain multiple streams
23  * compressed together.  */
24 struct wim_resource_spec {
25         /* The WIM containing this resource.  @wim->in_fd is expected to be a
26          * file descriptor to the underlying WIM file, opened for reading.  */
27         WIMStruct *wim;
28
29         /* The offset, in bytes, from the start of WIM file at which this
30          * resource starts.  */
31         u64 offset_in_wim;
32
33         /* The size of this resource in the WIM file.  For compressed resources
34          * this is the compressed size, including overhead such as the chunk
35          * table.  */
36         u64 size_in_wim;
37
38         /* The number of bytes of uncompressed data this resource decompresses
39          * to.  */
40         u64 uncompressed_size;
41
42         /* The list of streams this resource contains.  */
43         struct list_head stream_list;
44
45         /* Flags for this resource (WIM_RESHDR_FLAG_*).  */
46         u32 flags : 8;
47
48         /* [wimlib extension] This flag will be set if the WIM is pipable.  In
49          * such cases, the resource will be in a slightly different format if it
50          * is compressed.  */
51         u32 is_pipable : 1;
52
53         /* Temporary flag.  */
54         u32 raw_copy_ok : 1;
55
56         /* Compression type of this resource.  */
57         u32 compression_type : 22;
58
59         /* Compression chunk size of this resource.  Irrelevant if the resource
60          * is uncompressed.  */
61         u32 chunk_size;
62 };
63
64 /* On-disk version of a WIM resource header.  */
65 struct wim_reshdr_disk {
66         /* Size of the resource as it appears in the WIM file (possibly
67          * compressed).  */
68         u8 size_in_wim[7];
69
70         /* Zero or more of the WIM_RESHDR_FLAG_* flags.  These indicate, for
71          * example, whether the resource is compressed or not.  */
72         u8 flags;
73
74         /* Offset of the resource from the start of the WIM file, in bytes.  */
75         le64 offset_in_wim;
76
77         /* Uncompressed size of the resource, in bytes.  */
78         le64 uncompressed_size;
79 } _packed_attribute;
80
81 /* In-memory version of a WIM resource header (`struct wim_reshdr_disk').  */
82 struct wim_reshdr {
83         u64 size_in_wim : 56;
84         u64 flags : 8;
85         u64 offset_in_wim;
86         u64 uncompressed_size;
87 };
88
89 /* Flags for the `flags' field of WIM resource headers (`struct wim_reshdr').
90  */
91
92 /* Unknown meaning; may be intended to indicate spaces in the WIM that are free
93  * to overwrite.  Currently ignored by wimlib.  */
94 #define WIM_RESHDR_FLAG_FREE            0x01
95
96 /* The resource is a metadata resource for a WIM image, or is the lookup table
97  * or XML data for the WIM.  */
98 #define WIM_RESHDR_FLAG_METADATA        0x02
99
100 /* The resource is compressed using the WIM's default compression type and uses
101  * the regular chunk table format.  */
102 #define WIM_RESHDR_FLAG_COMPRESSED      0x04
103
104 /* Unknown meaning; may be intended to indicate a partial stream.  Currently
105  * ignored by wimlib.  */
106 #define WIM_RESHDR_FLAG_SPANNED         0x08
107
108 /* The resource is packed in a special format that may contain multiple
109  * underlying streams, or this resource entry represents a stream packed into
110  * one such resource.  When resources have this flag set, the WIM version number
111  * should be WIM_VERSION_PACKED_STREAMS.  */
112 #define WIM_RESHDR_FLAG_PACKED_STREAMS  0x10
113
114 /* Magic number in the 'uncompressed_size' field of the resource header that
115  * identifies the main entry for a pack.  */
116 #define WIM_PACK_MAGIC_NUMBER           0x100000000ULL
117
118 /* Returns true if the specified WIM resource is compressed, using either the
119  * original chunk table layout or the alternate layout for resources that may
120  * contain multiple packed streams.  */
121 static inline bool
122 resource_is_compressed(const struct wim_resource_spec *rspec)
123 {
124         return (rspec->flags & (WIM_RESHDR_FLAG_COMPRESSED |
125                                 WIM_RESHDR_FLAG_PACKED_STREAMS));
126 }
127
128 static inline void
129 copy_reshdr(struct wim_reshdr *dest, const struct wim_reshdr *src)
130 {
131         memcpy(dest, src, sizeof(struct wim_reshdr));
132 }
133
134 static inline void
135 zero_reshdr(struct wim_reshdr *reshdr)
136 {
137         memset(reshdr, 0, sizeof(struct wim_reshdr));
138 }
139
140 extern void
141 wim_res_hdr_to_spec(const struct wim_reshdr *reshdr, WIMStruct *wim,
142                     struct wim_resource_spec *rspec);
143
144 extern void
145 wim_res_spec_to_hdr(const struct wim_resource_spec *rspec,
146                     struct wim_reshdr *reshdr);
147
148 extern void
149 get_wim_reshdr(const struct wim_reshdr_disk *disk_reshdr,
150                struct wim_reshdr *reshdr);
151
152 void
153 put_wim_reshdr(const struct wim_reshdr *reshdr,
154                struct wim_reshdr_disk *disk_reshdr);
155
156 /* Alternate chunk table format for resources with
157  * WIM_RESHDR_FLAG_PACKED_STREAMS set.  */
158 struct alt_chunk_table_header_disk {
159         /* Uncompressed size of the resource in bytes.  */
160         le64 res_usize;
161
162         /* Number of bytes each compressed chunk decompresses into, except
163          * possibly the last which decompresses into the remainder.  This
164          * overrides the chunk size specified by the WIM header.  */
165         le32 chunk_size;
166
167         /* Compression format used for compressed chunks:
168          * 0 = None
169          * 1 = XPRESS
170          * 2 = LZX
171          * 3 = LZMS
172          *
173          * This overrides the compression type specified by the WIM header.  */
174         le32 compression_format;
175
176         /* This header is directly followed by a table of compressed sizes of
177          * the chunks (4 bytes per entry).  */
178 } _packed_attribute;
179
180 static inline unsigned int
181 get_chunk_entry_size(u64 res_size, bool is_alt)
182 {
183         if (res_size <= UINT32_MAX || is_alt)
184                 return 4;
185         else
186                 return 8;
187 }
188
189 /* Functions to read streams  */
190
191 extern int
192 read_partial_wim_stream_into_buf(const struct wim_lookup_table_entry *lte,
193                                  size_t size, u64 offset, void *buf);
194
195 extern int
196 read_full_stream_into_buf(const struct wim_lookup_table_entry *lte, void *buf);
197
198 extern int
199 read_full_stream_into_alloc_buf(const struct wim_lookup_table_entry *lte,
200                                 void **buf_ret);
201
202 extern int
203 wim_reshdr_to_data(const struct wim_reshdr *reshdr,
204                    WIMStruct *wim, void **buf_ret);
205
206 extern int
207 wim_reshdr_to_hash(const struct wim_reshdr *reshdr, WIMStruct *wim,
208                    u8 hash[SHA1_HASH_SIZE]);
209
210 extern int
211 skip_wim_stream(struct wim_lookup_table_entry *lte);
212
213 /*
214  * Type of callback function for beginning to read a stream.
215  *
216  * @lte:
217  *      Stream that is about to be read.
218  *
219  * @flags:
220  *      Bitwise OR of BEGIN_STREAM_FLAG_PARTIAL_RESOURCE and/or
221  *      BEGIN_STREAM_FLAG_WHOLE_STREAM.
222  *
223  * @ctx:
224  *      User-provided context.
225  *
226  * Must return 0 on success, a positive error code on failure, or the special
227  * value BEGIN_STREAM_STATUS_SKIP_STREAM to indicate that the stream should not
228  * be read, and read_stream_list() should continue on to the next stream
229  * (without calling @consume_chunk or @end_stream).
230  */
231 typedef int (*read_stream_list_begin_stream_t)(struct wim_lookup_table_entry *lte,
232                                                u32 flags,
233                                                void *ctx);
234
235 /* Set to true if the stream is just one of several being read from a single
236  * pack and therefore would be extra expensive to read independently.  */
237 #define BEGIN_STREAM_FLAG_PARTIAL_RESOURCE      0x00000001
238
239 /* This is purely advisory and indicates that the entire stream data will be
240  * provided in one call to consume_chunk().  */
241 #define BEGIN_STREAM_FLAG_WHOLE_STREAM          0x00000002
242
243 #define BEGIN_STREAM_STATUS_SKIP_STREAM -1
244
245 /*
246  * Type of callback function for finishing reading a stream.
247  *
248  * @lte:
249  *      Stream that has been fully read, or stream that started being read but
250  *      could not be fully read due to a read error.
251  *
252  * @status:
253  *      0 if reading the stream was successful; otherwise a nonzero error code
254  *      that specifies the return status.
255  *
256  * @ctx:
257  *      User-provided context.
258  */
259 typedef int (*read_stream_list_end_stream_t)(struct wim_lookup_table_entry *lte,
260                                              int status,
261                                              void *ctx);
262
263
264 /* Callback functions and contexts for read_stream_list().  */
265 struct read_stream_list_callbacks {
266
267         /* Called when a stream is about to be read.  */
268         read_stream_list_begin_stream_t begin_stream;
269
270         /* Called when a chunk of data has been read.  */
271         consume_data_callback_t consume_chunk;
272
273         /* Called when a stream has been fully read.  A successful call to
274          * @begin_stream will always be matched by a call to @end_stream.  */
275         read_stream_list_end_stream_t end_stream;
276
277         /* Parameter passed to @begin_stream.  */
278         void *begin_stream_ctx;
279
280         /* Parameter passed to @consume_chunk.  */
281         void *consume_chunk_ctx;
282
283         /* Parameter passed to @end_stream.  */
284         void *end_stream_ctx;
285 };
286
287 /* Flags for read_stream_list()  */
288 #define VERIFY_STREAM_HASHES            0x1
289 #define COMPUTE_MISSING_STREAM_HASHES   0x2
290 #define STREAM_LIST_ALREADY_SORTED      0x4
291
292 extern int
293 read_stream_list(struct list_head *stream_list,
294                  size_t list_head_offset,
295                  const struct read_stream_list_callbacks *cbs,
296                  int flags);
297
298 /* Functions to extract streams.  */
299
300 extern int
301 extract_stream(struct wim_lookup_table_entry *lte,
302                u64 size,
303                consume_data_callback_t extract_chunk,
304                void *extract_chunk_arg);
305
306 extern int
307 extract_stream_to_fd(struct wim_lookup_table_entry *lte,
308                      struct filedes *fd, u64 size);
309
310 extern int
311 extract_full_stream_to_fd(struct wim_lookup_table_entry *lte,
312                           struct filedes *fd);
313
314 extern int
315 extract_chunk_to_fd(const void *chunk, size_t size, void *_fd_p);
316
317 /* Miscellaneous stream functions.  */
318
319 extern int
320 sha1_stream(struct wim_lookup_table_entry *lte);
321
322 /* Functions to read/write metadata resources.  */
323
324 extern int
325 read_metadata_resource(WIMStruct *wim,
326                        struct wim_image_metadata *image_metadata);
327
328 extern int
329 write_metadata_resource(WIMStruct *wim, int image, int write_resource_flags);
330
331 /* Definitions specific to pipable WIM resources.  */
332
333 /* Arbitrary number to begin each stream in the pipable WIM, used for sanity
334  * checking.  */
335 #define PWM_STREAM_MAGIC 0x2b9b9ba2443db9d8ULL
336
337 /* Header that precedes each resource in a pipable WIM.  */
338 struct pwm_stream_hdr {
339         le64 magic;                     /* +0   */
340         le64 uncompressed_size;         /* +8   */
341         u8 hash[SHA1_HASH_SIZE];        /* +16  */
342         le32 flags;                     /* +36  */
343                                         /* +40  */
344 } _packed_attribute;
345
346 /* Extra flag for the @flags field in `struct pipable_wim_stream_hdr': Indicates
347  * that the SHA1 message digest of the stream has not been calculated.
348  * Currently only used for the XML data.  */
349 #define PWM_RESHDR_FLAG_UNHASHED         0x100
350
351 /* Header that precedes each chunk of a compressed resource in a pipable WIM.
352  */
353 struct pwm_chunk_hdr {
354         le32 compressed_size;
355 } _packed_attribute;
356
357 #endif /* _WIMLIB_RESOURCE_H */