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/sha1.h"
10 struct wim_lookup_table_entry;
11 struct wim_image_metadata;
13 /* Description of the location, size, and compression status of a WIM resource
14 * (stream). This is the in-memory version of `struct resource_entry_disk'. */
15 struct resource_entry {
16 /* Size, in bytes, of the resource as it appears in the WIM file. If
17 * the resource is uncompressed, this will be the same as
18 * @original_size. If the resource is compressed, this will be the
19 * compressed size of the resource, including all compressed chunks as
20 * well as the chunk table.
22 * Note: if the WIM is "pipable", this value does not include the stream
26 /* Bitwise OR of one or more of the WIM_RESHDR_FLAG_* flags. */
29 /* Offset, in bytes, of the resource from the start of the WIM file. */
32 /* Uncompressed size, in bytes, of the resource (stream). */
36 /* On-disk version of `struct resource_entry'. See `struct resource_entry' for
37 * description of fields. */
38 struct resource_entry_disk {
45 /* Flags for the `flags' field of the struct resource_entry structure. */
47 /* I haven't seen this flag used in any of the WIMs I have examined. I assume
48 * it means that there are no references to the stream, so the space is free.
49 * However, even after deleting files from a WIM mounted with `imagex.exe
50 * /mountrw', I could not see this flag being used. Either way, wimlib doesn't
51 * actually use this flag for anything. */
52 #define WIM_RESHDR_FLAG_FREE 0x01
54 /* Indicates that the stream is a metadata resource for a WIM image. This flag
55 * is also set in the resource entry for the lookup table in the WIM header. */
56 #define WIM_RESHDR_FLAG_METADATA 0x02
58 /* Indicates that the stream is compressed (using the WIM's set compression
60 #define WIM_RESHDR_FLAG_COMPRESSED 0x04
62 /* I haven't seen this flag used in any of the WIMs I have examined. Perhaps it
63 * means that a stream could possibly be split among multiple split WIM parts.
64 * However, `imagex.exe /split' does not seem to create any WIMs like this.
65 * Either way, wimlib doesn't actually use this flag for anything. */
66 #define WIM_RESHDR_FLAG_SPANNED 0x08
68 /* Functions that operate directly on `struct resource_entry's. */
71 resource_is_compressed(const struct resource_entry *entry)
73 return (entry->flags & WIM_RESHDR_FLAG_COMPRESSED);
77 copy_resource_entry(struct resource_entry *dst,
78 const struct resource_entry *src)
80 memcpy(dst, src, sizeof(struct resource_entry));
84 zero_resource_entry(struct resource_entry *entry)
86 memset(entry, 0, sizeof(struct resource_entry));
90 get_resource_entry(const struct resource_entry_disk *disk_entry,
91 struct resource_entry *entry);
94 put_resource_entry(const struct resource_entry *entry,
95 struct resource_entry_disk *disk_entry);
97 /* wimlib internal flags used when reading or writing resources. */
98 #define WIMLIB_WRITE_RESOURCE_FLAG_RECOMPRESS 0x00000001
99 #define WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE 0x00000002
100 #define WIMLIB_WRITE_RESOURCE_MASK 0x0000ffff
102 #define WIMLIB_READ_RESOURCE_FLAG_RAW_FULL 0x80000000
103 #define WIMLIB_READ_RESOURCE_FLAG_RAW_CHUNKS 0x40000000
104 #define WIMLIB_READ_RESOURCE_FLAG_RAW (WIMLIB_READ_RESOURCE_FLAG_RAW_FULL | \
105 WIMLIB_READ_RESOURCE_FLAG_RAW_CHUNKS)
106 #define WIMLIB_READ_RESOURCE_MASK 0xffff0000
109 /* Functions to read a resource. */
112 read_partial_wim_resource(const struct wim_lookup_table_entry *lte,
113 u64 size, consume_data_callback_t cb,
114 u32 in_chunk_size, void *ctx_or_buf,
115 int flags, u64 offset);
118 read_partial_wim_resource_into_buf(const struct wim_lookup_table_entry *lte,
119 size_t size, u64 offset, void *buf);
121 read_full_resource_into_buf(const struct wim_lookup_table_entry *lte, void *buf);
124 read_full_resource_into_alloc_buf(const struct wim_lookup_table_entry *lte,
128 res_entry_to_data(const struct resource_entry *res_entry,
129 WIMStruct *wim, void **buf_ret);
132 read_resource_prefix(const struct wim_lookup_table_entry *lte,
133 u64 size, consume_data_callback_t cb,
134 u32 in_chunk_size, void *ctx_or_buf, int flags);
136 /* Functions to write a resource. */
139 write_wim_resource_from_buffer(const void *buf, size_t buf_size,
140 int reshdr_flags, struct filedes *out_fd,
143 struct resource_entry *out_res_entry,
144 u8 *hash_ret, int write_resource_flags,
145 struct wimlib_lzx_context **comp_ctx);
147 /* Functions to extract a resource. */
150 extract_wim_resource(const struct wim_lookup_table_entry *lte,
152 consume_data_callback_t extract_chunk,
153 void *extract_chunk_arg);
156 extract_wim_resource_to_fd(const struct wim_lookup_table_entry *lte,
157 struct filedes *fd, u64 size);
159 /* Miscellaneous resource functions. */
162 sha1_resource(struct wim_lookup_table_entry *lte);
164 /* Functions to read/write metadata resources. */
167 read_metadata_resource(WIMStruct *wim,
168 struct wim_image_metadata *image_metadata);
171 write_metadata_resource(WIMStruct *wim, int image, int write_resource_flags);
173 /* Definitions specific to pipable WIM resources. */
175 /* Arbitrary number to begin each stream in the pipable WIM, used for sanity
177 #define PWM_STREAM_MAGIC 0x2b9b9ba2443db9d8ULL
179 /* Header that precedes each resource in a pipable WIM. */
180 struct pwm_stream_hdr {
182 le64 uncompressed_size; /* +8 */
183 u8 hash[SHA1_HASH_SIZE]; /* +16 */
184 le32 flags; /* +36 */
188 /* Extra flag for the @flags field in `struct pipable_wim_stream_hdr': Indicates
189 * that the SHA1 message digest of the stream has not been calculated.
190 * Currently only used for the XML data. */
191 #define PWM_RESHDR_FLAG_UNHASHED 0x100
193 /* Header that precedes each chunk of a compressed resource in a pipable WIM.
195 struct pwm_chunk_hdr {
196 le32 compressed_size;
200 #endif /* _WIMLIB_RESOURCE_H */