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,
56 #define RINOK(expr) do { \
62 /* Metadata for a resource in a WIM file. */
63 struct resource_entry {
64 /* Size, in bytes, of the resource in the WIM file. */
67 /* Bitwise or of one or more of the WIM_RESHDR_FLAG_* flags. */
70 /* Offset, in bytes, of the resource in the WIM file. */
73 /* Uncompressed size of the resource in the WIM file. Is the same as
74 * @size if the resource is uncompressed. */
78 /* Flags for the `flags' field of the struct resource_entry structure. */
81 #define WIM_RESHDR_FLAG_FREE 0x01
83 /* Indicates that a file resource is a metadata resource. */
84 #define WIM_RESHDR_FLAG_METADATA 0x02
86 /* Indicates that a file resource is compressed. */
87 #define WIM_RESHDR_FLAG_COMPRESSED 0x04
90 #define WIM_RESHDR_FLAG_SPANNED 0x08
93 /* Header at the very beginning of the WIM file. */
95 /* Identifies the file as WIM file. Must be exactly
96 * {'M', 'S', 'W', 'I', 'M', 0, 0, 0} */
97 //u8 magic[WIM_MAGIC_LEN];
99 /* size of WIM header in bytes. */
102 /* Version of the WIM file. M$ provides no documentation about exactly
103 * what this field affects about the file format, other than the fact
104 * that more recent versions have a higher value. */
107 /* Bitwise OR of one or more of the WIM_HDR_FLAG_* defined below. */
110 /* The size of the pieces that the uncompressed files were split up into
111 * when they were compressed. This should be the same as
112 * WIM_CHUNK_SIZE. M$ incorrectly documents this as "the size of the
113 * compressed .wim file in bytes".*/
116 /* A unique identifier for the WIM file. */
117 u8 guid[WIM_GID_LEN];
119 /* Part number of the WIM file in a spanned set. */
122 /* Total number of parts in a spanned set. */
125 /* Number of images in the WIM file. */
128 /* Location, size, and flags of the lookup table of the WIM. */
129 struct resource_entry lookup_table_res_entry;
131 /* Location, size, and flags for the XML data of the WIM. */
132 struct resource_entry xml_res_entry;
134 /* Location, size, and flags for the boot metadata. This means the
135 * metadata resource for the image specified by boot_idx below. Should
136 * be zeroed out if boot_idx is 0. */
137 struct resource_entry boot_metadata_res_entry;
139 /* The index of the bootable image in the WIM file. If 0, there are no
140 * bootable images available. */
143 /* The location of the optional integrity table used to verify the
144 * integrity WIM. Zeroed out if there is no integrity table.*/
145 struct resource_entry integrity;
147 /* Reserved for future disuse */
148 //u8 unused[WIM_UNUSED_LEN];
151 /* Flags for the `flags' field of the struct wim_header. */
154 /* Reserved for future use by M$ */
155 #define WIM_HDR_FLAG_RESERVED 0x00000001
157 /* Files and metadata in the WIM are compressed. */
158 #define WIM_HDR_FLAG_COMPRESSION 0x00000002
160 /* WIM is read-only. */
161 #define WIM_HDR_FLAG_READONLY 0x00000004
163 /* Resource data specified by images in this WIM may be contained in a different
165 #define WIM_HDR_FLAG_SPANNED 0x00000008
167 /* The WIM contains resources only; no filesystem metadata. */
168 #define WIM_HDR_FLAG_RESOURCE_ONLY 0x00000010
170 /* The WIM contains metadata only. */
171 #define WIM_HDR_FLAG_METADATA_ONLY 0x00000020
173 /* Lock field to prevent multiple writers from writing the WIM concurrently. */
174 #define WIM_HDR_FLAG_WRITE_IN_PROGRESS 0x00000040
176 /* Reparse point fixup ??? */
177 #define WIM_HDR_FLAG_RP_FIX 0x00000080
179 /* Unknown compression type */
180 #define WIM_HDR_FLAG_COMPRESS_RESERVED 0x00010000
182 /* Resources within the WIM are compressed using "XPRESS" compression, which is
183 * a LZ77-based compression algorithm. */
184 #define WIM_HDR_FLAG_COMPRESS_XPRESS 0x00020000
186 /* Resources within the WIM are compressed using "LZX" compression. This is also
187 * a LZ77-based algorithm. */
188 #define WIM_HDR_FLAG_COMPRESS_LZX 0x00040000
192 /* Structure for security data. Each image in the WIM file has its own security
194 typedef struct WIMSecurityData {
195 /* The total length of the security data, in bytes. A typical size is
196 * 2048 bytes. If there is no security data, though (as in the WIMs
197 * that wimlib writes, currently), it will be 8 bytes. */
200 /* The number of security descriptors in the array @descriptors, below. */
203 /* Array of sizes of the descriptors in the array @descriptors. */
206 /* Array of descriptors. */
211 /* Metadata resource for an image. */
212 struct image_metadata {
215 /* The security data for the image. */
216 WIMSecurityData security_data;
219 /* The root dentry for the image. */
220 struct dentry *root_dentry;
222 /* True if the filesystem of the image has been modified. If this is
223 * the case, the memory for the filesystem is not freed when switching
224 * to a different WIM image. */
227 /* A pointer to the lookup table entry for this image's metadata
229 struct lookup_table_entry *lookup_table_entry;
232 /* The opaque structure exposed to the wimlib API. */
233 typedef struct WIMStruct {
234 /* The name of the WIM file that has been opened. */
237 /* A pointer to the file indicated by @filename, opened for reading. */
240 /* The currently selected image, indexed starting at 1. If not 0,
241 * subtract 1 from this to get the index of the current image in the
242 * image_metadata array. */
245 /* The lookup table for the WIM file. */
246 struct lookup_table *lookup_table;
248 /* The header of the WIM file. */
249 struct wim_header hdr;
251 /* Pointer to the XML data read from the WIM file. */
254 /* Information retrieved from the XML data, arranged
255 * in an orderly manner. */
256 struct wim_info *wim_info;
258 /* Array of the image metadata of length image_count. Each image in the
259 * WIM has a image metadata associated with it. */
260 struct image_metadata *image_metadata;
262 /* True if files names are to be printed when doing extraction.
263 * May be used for other things later. */
266 /* The type of links to create when extracting files (hard, symbolic, or
270 /* Name of the output directory for extraction. */
273 /* Set to true when extracting multiple images */
274 bool is_multi_image_extraction;
276 /* FILE pointer for the WIM file that is being written. */
281 /* Inline utility functions for WIMStructs. */
283 static inline struct dentry *wim_root_dentry(WIMStruct *w)
285 return w->image_metadata[w->current_image - 1].root_dentry;
288 static inline struct dentry **wim_root_dentry_p(WIMStruct *w)
290 return &w->image_metadata[w->current_image - 1].root_dentry;
294 static inline WIMSecurityData *wim_security_data(WIMStruct *w)
296 return &w->image_metadata[w->current_image - 1].security_data;
300 static inline struct lookup_table_entry*
301 wim_metadata_lookup_table_entry(WIMStruct *w)
303 return w->image_metadata[w->current_image - 1].lookup_table_entry;
306 /* Nonzero if a struct resource_entry indicates a compressed resource. */
307 static inline int resource_is_compressed(const struct resource_entry *entry)
309 return (entry->flags & WIM_RESHDR_FLAG_COMPRESSED);
312 static inline struct image_metadata *wim_get_current_image_metadata(WIMStruct *w)
314 return &w->image_metadata[w->current_image - 1];
317 static inline bool wim_current_image_is_modified(const WIMStruct *w)
319 return w->image_metadata[w->current_image - 1].modified;
322 /* Prints a hash code field. */
323 static inline void print_hash(const u8 hash[])
325 print_byte_field(hash, WIM_HASH_SIZE);
330 extern int read_header(FILE *fp, struct wim_header *hdr);
331 extern int write_header(const struct wim_header *hdr, FILE *out);
332 extern int init_header(struct wim_header *hdr, int ctype);
335 extern int write_integrity_table(FILE *out, u64 end_header_offset,
336 u64 end_lookup_table_offset,
338 extern int check_wim_integrity(WIMStruct *w, int show_progress, int *status);
342 extern const u8 *get_resource_entry(const u8 *p, struct resource_entry *entry);
343 extern u8 *put_resource_entry(u8 *p, const struct resource_entry *entry);
345 extern int read_resource(FILE *fp, u64 resource_size,
346 u64 resource_original_size,
347 u64 resource_offset, int resource_ctype, u64 len,
348 u64 offset, void *contents_ret);
350 extern int read_uncompressed_resource(FILE *fp, u64 offset, u64 len,
354 extern int extract_resource_to_fd(WIMStruct *w,
355 const struct resource_entry *entry,
359 extern int extract_full_resource_to_fd(WIMStruct *w,
360 const struct resource_entry *entry,
363 extern int read_metadata_resource(FILE *fp, const struct resource_entry *metadata,
364 int wim_ctype, /* WIMSecurityData *sd, */
365 struct dentry **root_dentry_p);
367 extern int resource_compression_type(int wim_ctype, int reshdr_flags);
369 static inline int read_full_resource(FILE *fp, u64 resource_size,
370 u64 resource_original_size,
372 int resource_ctype, void *contents_ret)
374 return read_resource(fp, resource_size, resource_original_size,
375 resource_offset, resource_ctype,
376 resource_original_size, 0, contents_ret);
381 bool read_security_data(const u8 metadata_resource[],
382 u64 metadata_resource_len, WIMSecurityData *sd);
384 void print_security_data(const WIMSecurityData *sd);
385 u8 *write_security_data(const WIMSecurityData *sd, u8 *p);
386 void init_security_data(WIMSecurityData *sd);
387 void destroy_security_data(WIMSecurityData *sd);
391 extern int wimlib_select_image(WIMStruct *w, int image);
392 extern void wimlib_destroy(WIMStruct *w);
393 extern int wim_hdr_flags_compression_type(int wim_hdr_flags);
394 extern int wim_resource_compression_type(const WIMStruct *w,
395 const struct resource_entry *entry);
396 extern int for_image(WIMStruct *w, int image, int (*visitor)(WIMStruct *));
399 extern int write_resource_from_memory(const u8 resource[], int out_ctype,
400 u64 resource_original_size, FILE *out,
401 u64 *resource_size_ret);
406 #endif /* _WIMLIB_INTERNAL_H */