1 #ifndef _WIMLIB_RESOURCE_H
2 #define _WIMLIB_RESOURCE_H
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"
10 struct wim_lookup_table_entry;
11 struct wim_image_metadata;
13 /* Specification of a resource in a WIM file.
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.
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. */
29 /* The offset, in bytes, from the start of WIM file at which this
33 /* The size of this resource in the WIM file. For compressed resources
34 * this is the compressed size. */
37 /* The number of bytes of uncompressed data this resource decompresses
39 u64 uncompressed_size;
41 /* The list of streams this resource contains. */
42 struct list_head stream_list;
44 /* Flags for this resource (WIM_RESHDR_FLAG_*) */
47 /* This flag will be set if the WIM is pipable. In such cases, the
48 * resource will be in a slightly different format if it is compressed.
49 * This is a wimlib extension. */
53 /* On-disk version of a WIM resource header. */
54 struct wim_reshdr_disk {
55 /* Size of the resource as it appears in the WIM file (possibly
59 /* Zero or more of the WIM_RESHDR_FLAG_* flags. These indicate, for
60 * example, whether the resource is compressed or not. */
63 /* Offset of the resource from the start of the WIM file, in bytes. */
66 /* Uncompressed size of the resource, in bytes. */
67 le64 uncompressed_size;
70 /* In-memory version of a WIM resource header. */
75 u64 uncompressed_size;
78 /* Flags for the `flags' field of WIM resource headers. */
80 /* Unknown meaning; may be intended to indicate spaces in the WIM that are free
81 * to overwrite. Currently ignored by wimlib. */
82 #define WIM_RESHDR_FLAG_FREE 0x01
84 /* The resource is a metadata resource for a WIM image, or is the lookup table
85 * or XML data for the WIM. */
86 #define WIM_RESHDR_FLAG_METADATA 0x02
88 /* The resource is compressed using the WIM's default compression type and uses
89 * the regular chunk table format. */
90 #define WIM_RESHDR_FLAG_COMPRESSED 0x04
92 /* Unknown meaning; may be intended to indicate a partial stream. Currently
93 * ignored by wimlib. */
94 #define WIM_RESHDR_FLAG_SPANNED 0x08
96 /* The resource is packed in a special format that may contain multiple
97 * underlying streams, or this resource entry represents a stream packed into
98 * one such resource. When resources have this flag set, the WIM version number
99 * should be WIM_VERSION_PACKED_STREAMS. */
100 #define WIM_RESHDR_FLAG_PACKED_STREAMS 0x10
102 /* Returns true if the specified WIM resource is compressed, using either the
103 * original chunk table layout or the alternate layout for resources that may
104 * contain multiple packed streams. */
106 resource_is_compressed(const struct wim_resource_spec *rspec)
108 return (rspec->flags & (WIM_RESHDR_FLAG_COMPRESSED |
109 WIM_RESHDR_FLAG_PACKED_STREAMS));
113 copy_reshdr(struct wim_reshdr *dest, const struct wim_reshdr *src)
115 memcpy(dest, src, sizeof(struct wim_reshdr));
119 zero_reshdr(struct wim_reshdr *reshdr)
121 memset(reshdr, 0, sizeof(struct wim_reshdr));
125 wim_res_hdr_to_spec(const struct wim_reshdr *reshdr, WIMStruct *wim,
126 struct wim_resource_spec *rspec);
129 wim_res_spec_to_hdr(const struct wim_resource_spec *rspec,
130 struct wim_reshdr *reshdr);
133 get_wim_reshdr(const struct wim_reshdr_disk *disk_reshdr,
134 struct wim_reshdr *reshdr);
137 put_wim_reshdr(const struct wim_reshdr *reshdr,
138 struct wim_reshdr_disk *disk_reshdr);
140 /* wimlib internal flags used when reading or writing resources. */
141 #define WIMLIB_WRITE_RESOURCE_FLAG_RECOMPRESS 0x00000001
142 #define WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE 0x00000002
143 #define WIMLIB_WRITE_RESOURCE_MASK 0x0000ffff
145 #define WIMLIB_READ_RESOURCE_FLAG_RAW_FULL 0x80000000
146 #define WIMLIB_READ_RESOURCE_FLAG_RAW_CHUNKS 0x40000000
147 #define WIMLIB_READ_RESOURCE_FLAG_RAW (WIMLIB_READ_RESOURCE_FLAG_RAW_FULL | \
148 WIMLIB_READ_RESOURCE_FLAG_RAW_CHUNKS)
149 #define WIMLIB_READ_RESOURCE_MASK 0xffff0000
152 /* Functions to read streams */
155 read_partial_wim_stream_into_buf(const struct wim_lookup_table_entry *lte,
156 size_t size, u64 offset, void *buf);
159 read_full_stream_into_buf(const struct wim_lookup_table_entry *lte, void *buf);
162 read_full_stream_into_alloc_buf(const struct wim_lookup_table_entry *lte,
166 wim_reshdr_to_data(const struct wim_reshdr *reshdr,
167 WIMStruct *wim, void **buf_ret);
170 skip_wim_stream(struct wim_lookup_table_entry *lte);
173 read_stream_prefix(const struct wim_lookup_table_entry *lte,
174 u64 size, consume_data_callback_t cb,
175 void *cb_ctx, int flags);
177 typedef int (*read_stream_list_begin_stream_t)(struct wim_lookup_table_entry *lte,
180 typedef int (*read_stream_list_end_stream_t)(struct wim_lookup_table_entry *lte,
184 /* Callback functions and contexts for read_stream_list(). */
185 struct read_stream_list_callbacks {
187 /* Called when a stream is about to be read. */
188 read_stream_list_begin_stream_t begin_stream;
190 /* Called when a chunk of data has been read. */
191 consume_data_callback_t consume_chunk;
193 /* Called when a stream has been fully read. */
194 read_stream_list_end_stream_t end_stream;
196 /* Parameter passed to @begin_stream. */
197 void *begin_stream_ctx;
199 /* Parameter passed to @consume_chunk. */
200 void *consume_chunk_ctx;
202 /* Parameter passed to @end_stream. */
203 void *end_stream_ctx;
207 read_stream_list(struct list_head *stream_list,
208 size_t list_head_offset,
210 const struct read_stream_list_callbacks *cbs);
212 /* Functions to extract streams. */
215 extract_stream(struct wim_lookup_table_entry *lte,
217 consume_data_callback_t extract_chunk,
218 void *extract_chunk_arg);
221 extract_stream_to_fd(struct wim_lookup_table_entry *lte,
222 struct filedes *fd, u64 size);
225 extract_chunk_to_fd(const void *chunk, size_t size, void *_fd_p);
227 /* Miscellaneous stream functions. */
230 sha1_stream(struct wim_lookup_table_entry *lte);
232 /* Functions to read/write metadata resources. */
235 read_metadata_resource(WIMStruct *wim,
236 struct wim_image_metadata *image_metadata);
239 write_metadata_resource(WIMStruct *wim, int image, int write_resource_flags);
241 /* Definitions specific to pipable WIM resources. */
243 /* Arbitrary number to begin each stream in the pipable WIM, used for sanity
245 #define PWM_STREAM_MAGIC 0x2b9b9ba2443db9d8ULL
247 /* Header that precedes each resource in a pipable WIM. */
248 struct pwm_stream_hdr {
250 le64 uncompressed_size; /* +8 */
251 u8 hash[SHA1_HASH_SIZE]; /* +16 */
252 le32 flags; /* +36 */
256 /* Extra flag for the @flags field in `struct pipable_wim_stream_hdr': Indicates
257 * that the SHA1 message digest of the stream has not been calculated.
258 * Currently only used for the XML data. */
259 #define PWM_RESHDR_FLAG_UNHASHED 0x100
261 /* Header that precedes each chunk of a compressed resource in a pipable WIM.
263 struct pwm_chunk_hdr {
264 le32 compressed_size;
267 #endif /* _WIMLIB_RESOURCE_H */