4 * Internal header for wimlib.
6 * wimlib - Library for working with WIM files
8 * Copyright (C) 2010 Carl Thijssen
9 * Copyright (C) 2012 Eric Biggers
11 * This library is free software; you can redistribute it and/or modify it under
12 * the terms of the GNU Lesser General Public License as published by the Free
13 * Software Foundation; either version 2.1 of the License, or (at your option) any
16 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
17 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
18 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License along
21 * with this library; if not, write to the Free Software Foundation, Inc., 59
22 * Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #ifndef _WIMLIB_INTERNAL_H
26 #define _WIMLIB_INTERNAL_H
32 #define WIM_HASH_SIZE 20
33 #define WIM_MAGIC_LEN 8
34 #define WIM_GID_LEN 16
35 #define WIM_UNUSED_LEN 60
38 /* Length of the WIM header on disk. */
39 #define WIM_HEADER_DISK_SIZE (148 + WIM_UNUSED_LEN)
41 /* Compressed resources in the WIM are divided into separated compressed chunks
43 #define WIM_CHUNK_SIZE 32768
45 /* Version of the WIM file. I don't know if there has ever been a different
47 #define WIM_VERSION 0x10d00
49 enum wim_integrity_status {
52 WIM_INTEGRITY_NONEXISTENT,
55 /* Metadata for a resource in a WIM file. */
56 struct resource_entry {
57 /* Size, in bytes, of the resource in the WIM file. */
60 /* Bitwise or of one or more of the WIM_RESHDR_FLAG_* flags. */
63 /* Offset, in bytes, of the resource in the WIM file. */
66 /* Uncompressed size of the resource in the WIM file. Is the same as
67 * @size if the resource is uncompressed. */
71 /* Flags for the `flags' field of the struct resource_entry structure. */
74 #define WIM_RESHDR_FLAG_FREE 0x01
76 /* Indicates that a file resource is a metadata resource. */
77 #define WIM_RESHDR_FLAG_METADATA 0x02
79 /* Indicates that a file resource is compressed. */
80 #define WIM_RESHDR_FLAG_COMPRESSED 0x04
83 #define WIM_RESHDR_FLAG_SPANNED 0x08
86 /* Header at the very beginning of the WIM file. */
88 /* Identifies the file as WIM file. Must be exactly
89 * {'M', 'S', 'W', 'I', 'M', 0, 0, 0} */
90 //u8 magic[WIM_MAGIC_LEN];
92 /* size of WIM header in bytes. */
95 /* Version of the WIM file. M$ provides no documentation about exactly
96 * what this field affects about the file format, other than the fact
97 * that more recent versions have a higher value. */
100 /* Bitwise OR of one or more of the WIM_HDR_FLAG_* defined below. */
103 /* The size of the pieces that the uncompressed files were split up into
104 * when they were compressed. This should be the same as
105 * WIM_CHUNK_SIZE. M$ incorrectly documents this as "the size of the
106 * compressed .wim file in bytes".*/
109 /* A unique identifier for the WIM file. */
110 u8 guid[WIM_GID_LEN];
112 /* Part number of the WIM file in a spanned set. */
115 /* Total number of parts in a spanned set. */
118 /* Number of images in the WIM file. */
121 /* Location, size, and flags of the lookup table of the WIM. */
122 struct resource_entry lookup_table_res_entry;
124 /* Location, size, and flags for the XML data of the WIM. */
125 struct resource_entry xml_res_entry;
127 /* Location, size, and flags for the boot metadata. This means the
128 * metadata resource for the image specified by boot_idx below. Should
129 * be zeroed out if boot_idx is 0. */
130 struct resource_entry boot_metadata_res_entry;
132 /* The index of the bootable image in the WIM file. If 0, there are no
133 * bootable images available. */
136 /* The location of the optional integrity table used to verify the
137 * integrity WIM. Zeroed out if there is no integrity table.*/
138 struct resource_entry integrity;
140 /* Reserved for future disuse */
141 //u8 unused[WIM_UNUSED_LEN];
144 /* Flags for the `flags' field of the struct wim_header. */
147 /* Reserved for future use by M$ */
148 #define WIM_HDR_FLAG_RESERVED 0x00000001
150 /* Files and metadata in the WIM are compressed. */
151 #define WIM_HDR_FLAG_COMPRESSION 0x00000002
153 /* WIM is read-only. */
154 #define WIM_HDR_FLAG_READONLY 0x00000004
156 /* Resource data specified by images in this WIM may be contained in a different
158 #define WIM_HDR_FLAG_SPANNED 0x00000008
160 /* The WIM contains resources only; no filesystem metadata. */
161 #define WIM_HDR_FLAG_RESOURCE_ONLY 0x00000010
163 /* The WIM contains metadata only. */
164 #define WIM_HDR_FLAG_METADATA_ONLY 0x00000020
166 /* Lock field to prevent multiple writers from writing the WIM concurrently. */
167 #define WIM_HDR_FLAG_WRITE_IN_PROGRESS 0x00000040
169 /* Reparse point fixup ??? */
170 #define WIM_HDR_FLAG_RP_FIX 0x00000080
172 /* Unknown compression type */
173 #define WIM_HDR_FLAG_COMPRESS_RESERVED 0x00010000
175 /* Resources within the WIM are compressed using "XPRESS" compression, which is
176 * a LZ77-based compression algorithm. */
177 #define WIM_HDR_FLAG_COMPRESS_XPRESS 0x00020000
179 /* Resources within the WIM are compressed using "LZX" compression. This is also
180 * a LZ77-based algorithm. */
181 #define WIM_HDR_FLAG_COMPRESS_LZX 0x00040000
184 #ifdef ENABLE_SECURITY_DATA
185 /* Structure for security data. Each image in the WIM file has its own security
187 struct wim_security_data {
188 /* The total length of the security data, in bytes. A typical size is
189 * 2048 bytes. If there is no security data, though (as in the WIMs
190 * that wimlib writes, currently), it will be 8 bytes. */
193 /* The number of security descriptors in the array @descriptors, below. */
196 /* Array of sizes of the descriptors in the array @descriptors. */
199 /* Array of descriptors. */
202 /* keep track of how many WIMs reference this security data (used when
203 * exporting images between WIMs) */
208 /* Metadata resource for an image. */
209 struct image_metadata {
210 /* Pointer to the root dentry for the image. */
211 struct dentry *root_dentry;
213 #ifdef ENABLE_SECURITY_DATA
214 /* Pointer to the security data for the image. */
215 struct wim_security_data *security_data;
217 /* A pointer to the lookup table entry for this image's metadata
219 struct lookup_table_entry *lookup_table_entry;
221 /* True if the filesystem of the image has been modified. If this is
222 * the case, the memory for the filesystem is not freed when switching
223 * to a different WIM image. */
228 /* The opaque structure exposed to the wimlib API. */
229 typedef struct WIMStruct {
231 /* A pointer to the file indicated by @filename, opened for reading. */
234 /* FILE pointer for the WIM file that is being written. */
237 /* The name of the WIM file that has been opened. */
240 /* The lookup table for the WIM file. */
241 struct lookup_table *lookup_table;
243 /* Pointer to the XML data read from the WIM file. */
246 /* Information retrieved from the XML data, arranged
247 * in an orderly manner. */
248 struct wim_info *wim_info;
250 /* Array of the image metadata of length image_count. Each image in the
251 * WIM has a image metadata associated with it. */
252 struct image_metadata *image_metadata;
254 /* Name of the output directory for extraction. */
257 /* The header of the WIM file. */
258 struct wim_header hdr;
260 /* The type of links to create when extracting files (hard, symbolic, or
264 /* The currently selected image, indexed starting at 1. If not 0,
265 * subtract 1 from this to get the index of the current image in the
266 * image_metadata array. */
269 /* True if files names are to be printed when doing extraction.
270 * May be used for other things later. */
274 /* Set to true when extracting multiple images */
275 bool is_multi_image_extraction;
282 /* Inline utility functions for WIMStructs. */
284 static inline struct dentry *wim_root_dentry(WIMStruct *w)
286 return w->image_metadata[w->current_image - 1].root_dentry;
289 static inline struct dentry **wim_root_dentry_p(WIMStruct *w)
291 return &w->image_metadata[w->current_image - 1].root_dentry;
294 #ifdef ENABLE_SECURITY_DATA
295 static inline struct wim_security_data *wim_security_data(WIMStruct *w)
297 return w->image_metadata[w->current_image - 1].security_data;
301 static inline struct lookup_table_entry*
302 wim_metadata_lookup_table_entry(WIMStruct *w)
304 return w->image_metadata[w->current_image - 1].lookup_table_entry;
307 /* Nonzero if a struct resource_entry indicates a compressed resource. */
308 static inline int resource_is_compressed(const struct resource_entry *entry)
310 return (entry->flags & WIM_RESHDR_FLAG_COMPRESSED);
313 static inline struct image_metadata *wim_get_current_image_metadata(WIMStruct *w)
315 return &w->image_metadata[w->current_image - 1];
318 static inline bool wim_current_image_is_modified(const WIMStruct *w)
320 return w->image_metadata[w->current_image - 1].modified;
323 /* Prints a hash code field. */
324 static inline void print_hash(const u8 hash[])
326 print_byte_field(hash, WIM_HASH_SIZE);
331 extern int read_header(FILE *fp, struct wim_header *hdr, int split_ok);
332 extern int write_header(const struct wim_header *hdr, FILE *out);
333 extern int init_header(struct wim_header *hdr, int ctype);
336 extern int write_integrity_table(FILE *out, u64 end_header_offset,
337 u64 end_lookup_table_offset,
339 extern int check_wim_integrity(WIMStruct *w, int show_progress, int *status);
343 extern const u8 *get_resource_entry(const u8 *p, struct resource_entry *entry);
344 extern u8 *put_resource_entry(u8 *p, const struct resource_entry *entry);
346 extern int read_resource(FILE *fp, u64 resource_size,
347 u64 resource_original_size,
348 u64 resource_offset, int resource_ctype, u64 len,
349 u64 offset, void *contents_ret);
351 extern int read_uncompressed_resource(FILE *fp, u64 offset, u64 len,
355 extern int extract_resource_to_fd(WIMStruct *w,
356 const struct resource_entry *entry,
360 extern int extract_full_resource_to_fd(WIMStruct *w,
361 const struct resource_entry *entry,
364 extern int read_metadata_resource(FILE *fp,
365 const struct resource_entry *metadata,
367 struct image_metadata *image_metadata);
369 extern int resource_compression_type(int wim_ctype, int reshdr_flags);
371 static inline int read_full_resource(FILE *fp, u64 resource_size,
372 u64 resource_original_size,
374 int resource_ctype, void *contents_ret)
376 return read_resource(fp, resource_size, resource_original_size,
377 resource_offset, resource_ctype,
378 resource_original_size, 0, contents_ret);
381 extern int write_file_resource(struct dentry *dentry, void *wim_p);
382 extern int copy_resource(struct lookup_table_entry *lte, void *w);
383 extern int copy_between_files(FILE *in, off_t in_offset, FILE *out, size_t len);
384 extern int write_resource_from_memory(const u8 resource[], int out_ctype,
385 u64 resource_original_size, FILE *out,
386 u64 *resource_size_ret);
387 extern int write_metadata_resource(WIMStruct *w);
389 #ifdef ENABLE_SECURITY_DATA
390 int read_security_data(const u8 metadata_resource[],
391 u64 metadata_resource_len, struct wim_security_data **sd_p);
393 void print_security_data(const struct wim_security_data *sd);
394 u8 *write_security_data(const struct wim_security_data *sd, u8 *p);
395 void free_security_data(struct wim_security_data *sd);
399 extern WIMStruct *new_wim_struct();
400 extern int wimlib_select_image(WIMStruct *w, int image);
401 extern int wim_hdr_flags_compression_type(int wim_hdr_flags);
402 extern int wim_resource_compression_type(const WIMStruct *w,
403 const struct resource_entry *entry);
404 extern int for_image(WIMStruct *w, int image, int (*visitor)(WIMStruct *));
407 extern int finish_write(WIMStruct *w, int image, int flags,
408 int write_lookup_table);
410 extern int begin_write(WIMStruct *w, const char *path, int flags);
415 #endif /* _WIMLIB_INTERNAL_H */