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);
76 static inline void copy_resource_entry(struct resource_entry *dst,
77 const struct resource_entry *src)
79 memcpy(dst, src, sizeof(struct resource_entry));
82 static inline void zero_resource_entry(struct resource_entry *entry)
84 memset(entry, 0, sizeof(struct resource_entry));
88 get_resource_entry(const struct resource_entry_disk *disk_entry,
89 struct resource_entry *entry);
92 put_resource_entry(const struct resource_entry *entry,
93 struct resource_entry_disk *disk_entry);
95 /* wimlib internal flags used when reading or writing resources. */
96 #define WIMLIB_WRITE_RESOURCE_FLAG_RECOMPRESS 0x00000001
97 #define WIMLIB_WRITE_RESOURCE_FLAG_PIPABLE 0x00000002
98 #define WIMLIB_WRITE_RESOURCE_MASK 0x0000ffff
100 #define WIMLIB_READ_RESOURCE_FLAG_RAW_FULL 0x80000000
101 #define WIMLIB_READ_RESOURCE_FLAG_RAW_CHUNKS 0x40000000
102 #define WIMLIB_READ_RESOURCE_FLAG_SEEK_ONLY 0x20000000
103 #define WIMLIB_READ_RESOURCE_FLAG_RAW (WIMLIB_READ_RESOURCE_FLAG_RAW_FULL | \
104 WIMLIB_READ_RESOURCE_FLAG_RAW_CHUNKS)
105 #define WIMLIB_READ_RESOURCE_MASK 0xffff0000
108 /* Functions to read a resource. */
111 read_partial_wim_resource(const struct wim_lookup_table_entry *lte,
112 u64 size, consume_data_callback_t cb,
113 void *ctx_or_buf, int flags, u64 offset);
116 read_partial_wim_resource_into_buf(const struct wim_lookup_table_entry *lte,
117 size_t size, u64 offset, void *buf);
119 read_full_resource_into_buf(const struct wim_lookup_table_entry *lte, void *buf);
122 read_full_resource_into_alloc_buf(const struct wim_lookup_table_entry *lte,
126 res_entry_to_data(const struct resource_entry *res_entry,
127 WIMStruct *wim, void **buf_ret);
130 read_resource_prefix(const struct wim_lookup_table_entry *lte,
131 u64 size, consume_data_callback_t cb, void *ctx_or_buf,
134 /* Functions to write a resource. */
137 write_wim_resource(struct wim_lookup_table_entry *lte, struct filedes *out_fd,
138 int out_ctype, struct resource_entry *out_res_entry,
139 int write_resource_flags);
142 write_wim_resource_from_buffer(const void *buf, size_t buf_size,
143 int reshdr_flags, struct filedes *out_fd,
145 struct resource_entry *out_res_entry,
146 u8 *hash_ret, int write_resource_flags);
148 /* Functions to extract a resource. */
151 extract_wim_resource(const struct wim_lookup_table_entry *lte,
153 consume_data_callback_t extract_chunk,
154 void *extract_chunk_arg);
157 extract_wim_resource_to_fd(const struct wim_lookup_table_entry *lte,
158 struct filedes *fd, u64 size);
160 /* Miscellaneous resource functions. */
163 sha1_resource(struct wim_lookup_table_entry *lte);
165 /* Functions to read/write metadata resources. */
168 read_metadata_resource(WIMStruct *wim,
169 struct wim_image_metadata *image_metadata);
172 write_metadata_resource(WIMStruct *wim, int image, int write_resource_flags);
174 /* Definitions specific to pipable WIM resources. */
176 /* Arbitrary number to begin each stream in the pipable WIM, used for sanity
178 #define PWM_STREAM_MAGIC 0x2b9b9ba2443db9d8ULL
180 /* Header that precedes each resource in a pipable WIM. */
181 struct pwm_stream_hdr {
183 le64 uncompressed_size; /* +8 */
184 u8 hash[SHA1_HASH_SIZE]; /* +16 */
185 le32 flags; /* +36 */
189 /* Extra flag for the @flags field in `struct pipable_wim_stream_hdr': Indicates
190 * that the SHA1 message digest of the stream has not been calculated.
191 * Currently only used for the XML data. */
192 #define PWM_RESHDR_FLAG_UNHASHED 0x100
194 /* Header that precedes each chunk of a compressed resource in a pipable WIM.
196 struct pwm_chunk_hdr {
197 le32 compressed_size;
201 #endif /* _WIMLIB_RESOURCE_H */