1 #ifndef _WIMLIB_RESOURCE_H
2 #define _WIMLIB_RESOURCE_H
4 #include "wimlib/types.h"
5 #include "wimlib/endianness.h"
6 #include "wimlib/callback.h"
7 #include "wimlib/file_io.h"
8 #include "wimlib/list.h"
9 #include "wimlib/sha1.h"
11 struct wim_lookup_table_entry;
12 struct wim_image_metadata;
14 /* Specification of a resource in a WIM file.
16 * If a `struct wim_lookup_table_entry' lte has
17 * (lte->resource_location == RESOURCE_IN_WIM), then lte->wim_res_spec points to
18 * an instance of this structure.
20 * Normally, there is a one-to-one correspondence between WIM lookup table
21 * entries ("streams") and WIM resources. However, the flag
22 * WIM_RESHDR_FLAG_CONCAT can be used to specify that two streams be combined
23 * into the same resource and compressed together. Caveats about this flag are
24 * noted in the comment above the definition of WIM_VERSION_STREAM_CONCAT. */
25 struct wim_resource_spec {
26 /* The WIM file containing this resource. */
29 /* Offset, in bytes, from the start of WIM file at which this resource
33 /* The size of this resource in the WIM file. For compressed resources
34 * this is the compressed size. */
37 /* Number of bytes of uncompressed data this resource uncompresses to.
39 u64 uncompressed_size;
41 /* List of streams this resource contains. */
42 struct list_head lte_list;
47 /* This flag will be set if the WIM is pipable and therefore the
48 * resource will be in a slightly different format if it is compressed
49 * (wimlib extension). */
52 /* Compression type of this resource as one of WIMLIB_COMPRESSION_TYPE_*
56 /* Compression chunk size. */
61 /* On-disk version of a WIM resource header: This structure specifies the
62 * location, size, and flags (e.g. compressed or not compressed) for a resource
64 struct wim_reshdr_disk {
65 /* Size of the resource as it appears in the WIM file (possibly
69 /* WIM_RESHDR_FLAG_* flags. */
72 /* Offset of the resource from the start of the WIM file. */
75 /* Uncompressed size of the resource. */
76 le64 uncompressed_size;
79 /* In-memory version of wim_reshdr_disk. */
84 u64 uncompressed_size;
87 /* Flags for the `flags' field of the struct resource_entry structure. */
89 /* I haven't seen this flag used in any of the WIMs I have examined. I assume
90 * it means that there are no references to the stream, so the space is free.
91 * However, even after deleting files from a WIM mounted with `imagex.exe
92 * /mountrw', I could not see this flag being used. Either way, wimlib doesn't
93 * actually use this flag for anything. */
94 #define WIM_RESHDR_FLAG_FREE 0x01
96 /* Indicates that the stream is a metadata resource for a WIM image. This flag
97 * is also set in the resource entry for the lookup table in the WIM header. */
98 #define WIM_RESHDR_FLAG_METADATA 0x02
100 /* Indicates that the stream is compressed (using the WIM's set compression
102 #define WIM_RESHDR_FLAG_COMPRESSED 0x04
104 /* I haven't seen this flag used in any of the WIMs I have examined. Perhaps it
105 * means that a stream could possibly be split among multiple split WIM parts.
106 * However, `imagex.exe /split' does not seem to create any WIMs like this.
107 * Either way, wimlib doesn't actually use this flag for anything. */
108 #define WIM_RESHDR_FLAG_SPANNED 0x08
111 #define WIM_RESHDR_FLAG_CONCAT 0x10
114 copy_reshdr(struct wim_reshdr *dest, const struct wim_reshdr *src)
116 memcpy(dest, src, sizeof(struct wim_reshdr));
120 zero_reshdr(struct wim_reshdr *reshdr)
122 memset(reshdr, 0, sizeof(struct wim_reshdr));
127 wim_res_hdr_to_spec(const struct wim_reshdr *reshdr, WIMStruct *wim,
128 struct wim_resource_spec *rspec);
131 wim_res_spec_to_hdr(const struct wim_resource_spec *rspec,
132 struct wim_reshdr *reshdr);
135 get_wim_reshdr(const struct wim_reshdr_disk *disk_reshdr,
136 struct wim_reshdr *reshdr);
139 put_wim_reshdr(const struct wim_reshdr *reshdr,
140 struct wim_reshdr_disk *disk_reshdr);
142 /* wimlib internal flags used when reading or writing resources. */
143 #define WIMLIB_WRITE_RESOURCE_FLAG_RECOMPRESS 0x00000001
144 #define WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE 0x00000002
145 #define WIMLIB_WRITE_RESOURCE_MASK 0x0000ffff
147 #define WIMLIB_READ_RESOURCE_FLAG_RAW_FULL 0x80000000
148 #define WIMLIB_READ_RESOURCE_FLAG_RAW_CHUNKS 0x40000000
149 #define WIMLIB_READ_RESOURCE_FLAG_RAW (WIMLIB_READ_RESOURCE_FLAG_RAW_FULL | \
150 WIMLIB_READ_RESOURCE_FLAG_RAW_CHUNKS)
151 #define WIMLIB_READ_RESOURCE_MASK 0xffff0000
154 /* Functions to read a resource. */
157 read_partial_wim_resource(const struct wim_lookup_table_entry *lte,
158 u64 size, consume_data_callback_t cb,
159 u32 in_chunk_size, void *ctx_or_buf,
160 int flags, u64 offset);
163 read_partial_wim_resource_into_buf(const struct wim_lookup_table_entry *lte,
164 size_t size, u64 offset, void *buf);
166 read_full_resource_into_buf(const struct wim_lookup_table_entry *lte, void *buf);
169 read_full_resource_into_alloc_buf(const struct wim_lookup_table_entry *lte,
173 wim_reshdr_to_data(const struct wim_reshdr *reshdr,
174 WIMStruct *wim, void **buf_ret);
177 read_resource_prefix(const struct wim_lookup_table_entry *lte,
178 u64 size, consume_data_callback_t cb,
179 u32 in_chunk_size, void *ctx_or_buf, int flags);
181 /* Functions to extract a resource. */
184 extract_wim_resource(const struct wim_lookup_table_entry *lte,
186 consume_data_callback_t extract_chunk,
187 void *extract_chunk_arg);
190 extract_wim_resource_to_fd(const struct wim_lookup_table_entry *lte,
191 struct filedes *fd, u64 size);
193 /* Miscellaneous resource functions. */
196 sha1_resource(struct wim_lookup_table_entry *lte);
198 /* Functions to read/write metadata resources. */
201 read_metadata_resource(WIMStruct *wim,
202 struct wim_image_metadata *image_metadata);
205 write_metadata_resource(WIMStruct *wim, int image, int write_resource_flags);
207 /* Definitions specific to pipable WIM resources. */
209 /* Arbitrary number to begin each stream in the pipable WIM, used for sanity
211 #define PWM_STREAM_MAGIC 0x2b9b9ba2443db9d8ULL
213 /* Header that precedes each resource in a pipable WIM. */
214 struct pwm_stream_hdr {
216 le64 uncompressed_size; /* +8 */
217 u8 hash[SHA1_HASH_SIZE]; /* +16 */
218 le32 flags; /* +36 */
222 /* Extra flag for the @flags field in `struct pipable_wim_stream_hdr': Indicates
223 * that the SHA1 message digest of the stream has not been calculated.
224 * Currently only used for the XML data. */
225 #define PWM_RESHDR_FLAG_UNHASHED 0x100
227 /* Header that precedes each chunk of a compressed resource in a pipable WIM.
229 struct pwm_chunk_hdr {
230 le32 compressed_size;
234 #endif /* _WIMLIB_RESOURCE_H */