]> wimlib.net Git - wimlib/blob - include/wimlib/resource.h
Merge branch with pipable WIM support
[wimlib] / include / wimlib / resource.h
1 #ifndef _WIMLIB_RESOURCE_H
2 #define _WIMLIB_RESOURCE_H
3
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"
9
10 struct wim_lookup_table_entry;
11 struct wim_image_metadata;
12
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.
21          *
22          * Note: if the WIM is "pipable", this value does not include the stream
23          * header.  */
24         u64 size  : 56;
25
26         /* Bitwise OR of one or more of the WIM_RESHDR_FLAG_* flags.  */
27         u64 flags : 8;
28
29         /* Offset, in bytes, of the resource from the start of the WIM file.  */
30         u64 offset;
31
32         /* Uncompressed size, in bytes, of the resource (stream).  */
33         u64 original_size;
34 };
35
36 /* On-disk version of `struct resource_entry'.  See `struct resource_entry' for
37  * description of fields.  */
38 struct resource_entry_disk {
39         u8 size[7];
40         u8 flags;
41         le64 offset;
42         le64 original_size;
43 } _packed_attribute;
44
45 /* Flags for the `flags' field of the struct resource_entry structure. */
46
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
53
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
57
58 /* Indicates that the stream is compressed (using the WIM's set compression
59  * type).  */
60 #define WIM_RESHDR_FLAG_COMPRESSED      0x04
61
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
67
68 /* Functions that operate directly on `struct resource_entry's.  */
69
70 static inline int
71 resource_is_compressed(const struct resource_entry *entry)
72 {
73         return (entry->flags & WIM_RESHDR_FLAG_COMPRESSED);
74 }
75
76 static inline void copy_resource_entry(struct resource_entry *dst,
77                                        const struct resource_entry *src)
78 {
79         memcpy(dst, src, sizeof(struct resource_entry));
80 }
81
82 static inline void zero_resource_entry(struct resource_entry *entry)
83 {
84         memset(entry, 0, sizeof(struct resource_entry));
85 }
86
87 extern void
88 get_resource_entry(const struct resource_entry_disk *disk_entry,
89                    struct resource_entry *entry);
90
91 extern void
92 put_resource_entry(const struct resource_entry *entry,
93                    struct resource_entry_disk *disk_entry);
94
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
99
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
106
107
108 /* Functions to read a resource.  */
109
110 extern int
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);
114
115 extern int
116 read_partial_wim_resource_into_buf(const struct wim_lookup_table_entry *lte,
117                                    size_t size, u64 offset, void *buf);
118 extern int
119 read_full_resource_into_buf(const struct wim_lookup_table_entry *lte, void *buf);
120
121 extern int
122 read_full_resource_into_alloc_buf(const struct wim_lookup_table_entry *lte,
123                                   void **buf_ret);
124
125 extern int
126 res_entry_to_data(const struct resource_entry *res_entry,
127                   WIMStruct *wim, void **buf_ret);
128
129 extern int
130 read_resource_prefix(const struct wim_lookup_table_entry *lte,
131                      u64 size, consume_data_callback_t cb, void *ctx_or_buf,
132                      int flags);
133
134 /* Functions to write a resource.  */
135
136 extern int
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);
140
141 extern int
142 write_wim_resource_from_buffer(const void *buf, size_t buf_size,
143                                int reshdr_flags, struct filedes *out_fd,
144                                int out_ctype,
145                                struct resource_entry *out_res_entry,
146                                u8 *hash_ret, int write_resource_flags);
147
148 /* Functions to extract a resource.  */
149
150 extern int
151 extract_wim_resource(const struct wim_lookup_table_entry *lte,
152                      u64 size,
153                      consume_data_callback_t extract_chunk,
154                      void *extract_chunk_arg);
155
156 extern int
157 extract_wim_resource_to_fd(const struct wim_lookup_table_entry *lte,
158                            struct filedes *fd, u64 size);
159
160 /* Miscellaneous resource functions.  */
161
162 extern int
163 sha1_resource(struct wim_lookup_table_entry *lte);
164
165 /* Functions to read/write metadata resources.  */
166
167 extern int
168 read_metadata_resource(WIMStruct *wim,
169                        struct wim_image_metadata *image_metadata);
170
171 extern int
172 write_metadata_resource(WIMStruct *wim, int image, int write_resource_flags);
173
174 /* Definitions specific to pipable WIM resources.  */
175
176 /* Arbitrary number to begin each stream in the pipable WIM, used for sanity
177  * checking.  */
178 #define PWM_STREAM_MAGIC 0x2b9b9ba2443db9d8ULL
179
180 /* Header that precedes each resource in a pipable WIM.  */
181 struct pwm_stream_hdr {
182         le64 magic;                     /* +0   */
183         le64 uncompressed_size;         /* +8   */
184         u8 hash[SHA1_HASH_SIZE];        /* +16  */
185         le32 flags;                     /* +36  */
186                                         /* +40  */
187 } _packed_attribute;
188
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
193
194 /* Header that precedes each chunk of a compressed resource in a pipable WIM.
195  */
196 struct pwm_chunk_hdr {
197         le32 compressed_size;
198 } _packed_attribute;
199
200
201 #endif /* _WIMLIB_RESOURCE_H */