]> wimlib.net Git - wimlib/blob - src/wimlib_internal.h
Empty file fix
[wimlib] / src / wimlib_internal.h
1 /*
2  * wimlib_internal.h
3  *
4  * Internal header for wimlib.
5  *
6  * wimlib - Library for working with WIM files 
7  *
8  * Copyright (C) 2010 Carl Thijssen
9  * Copyright (C) 2012 Eric Biggers
10  *
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
14  * later version.
15  *
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.
19  *
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 
23  */
24
25 #ifndef _WIMLIB_INTERNAL_H
26 #define _WIMLIB_INTERNAL_H
27
28 #include "util.h"
29
30 struct stat;
31
32 #define WIM_HASH_SIZE  20
33 #define WIM_MAGIC_LEN  8
34 #define WIM_GID_LEN    16
35 #define WIM_UNUSED_LEN 60
36
37
38 /* Length of the WIM header on disk. */
39 #define WIM_HEADER_DISK_SIZE (148 + WIM_UNUSED_LEN)
40
41 /* Compressed resources in the WIM are divided into separated compressed chunks
42  * of this size. */
43 #define WIM_CHUNK_SIZE 32768
44
45 /* Version of the WIM file.  I don't know if there has ever been a different
46  * version or not. */
47 #define WIM_VERSION 0x10d00
48
49 enum wim_integrity_status {
50         WIM_INTEGRITY_OK,
51         WIM_INTEGRITY_NOT_OK,
52         WIM_INTEGRITY_NONEXISTENT,
53 };
54
55 /* Metadata for a resource in a WIM file. */
56 struct resource_entry {
57         /* Size, in bytes, of the resource in the WIM file. */
58         u64 size  : 56;
59
60         /* Bitwise or of one or more of the WIM_RESHDR_FLAG_* flags. */
61         u64 flags : 8;
62
63         /* Offset, in bytes, of the resource in the WIM file. */
64         u64 offset;
65
66         /* Uncompressed size of the resource in the WIM file.  Is the same as
67          * @size if the resource is uncompressed. */
68         u64 original_size;
69 };
70
71 /* Flags for the `flags' field of the struct resource_entry structure. */
72
73 /* ??? */
74 #define WIM_RESHDR_FLAG_FREE            0x01
75
76 /* Indicates that a file resource is a metadata resource. */
77 #define WIM_RESHDR_FLAG_METADATA        0x02
78
79 /* Indicates that a file resource is compressed. */
80 #define WIM_RESHDR_FLAG_COMPRESSED      0x04
81
82 /* ??? */
83 #define WIM_RESHDR_FLAG_SPANNED         0x08
84
85
86 /* Header at the very beginning of the WIM file. */
87 struct wim_header { 
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];     
91
92         /* size of WIM header in bytes. */
93         //u32 hdr_size;
94
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. */
98         //u32 version;
99
100         /* Bitwise OR of one or more of the WIM_HDR_FLAG_* defined below. */
101         u32 flags;
102
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".*/
107         //u32 chunk_size;
108         
109         /* A unique identifier for the WIM file. */
110         u8  guid[WIM_GID_LEN];
111
112         /* Part number of the WIM file in a spanned set. */
113         u16 part_number;
114
115         /* Total number of parts in a spanned set. */
116         u16 total_parts;
117
118         /* Number of images in the WIM file. */
119         u32 image_count;
120
121         /* Location, size, and flags of the lookup table of the WIM. */
122         struct resource_entry lookup_table_res_entry;
123
124         /* Location, size, and flags for the XML data of the WIM. */
125         struct resource_entry xml_res_entry;
126
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;
131
132         /* The index of the bootable image in the WIM file. If 0, there are no
133          * bootable images available. */
134         u32 boot_idx; 
135
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;
139
140         /* Reserved for future disuse */
141         //u8 unused[WIM_UNUSED_LEN];
142 };
143
144 /* Flags for the `flags' field of the struct wim_header. */
145
146
147 /* Reserved for future use by M$ */
148 #define WIM_HDR_FLAG_RESERVED           0x00000001
149
150 /* Files and metadata in the WIM are compressed. */
151 #define WIM_HDR_FLAG_COMPRESSION        0x00000002
152
153 /* WIM is read-only. */
154 #define WIM_HDR_FLAG_READONLY           0x00000004
155
156 /* Resource data specified by images in this WIM may be contained in a different
157  * WIM */
158 #define WIM_HDR_FLAG_SPANNED            0x00000008
159
160 /* The WIM contains resources only; no filesystem metadata. */
161 #define WIM_HDR_FLAG_RESOURCE_ONLY      0x00000010
162
163 /* The WIM contains metadata only. */
164 #define WIM_HDR_FLAG_METADATA_ONLY      0x00000020
165
166 /* Lock field to prevent multiple writers from writing the WIM concurrently. */
167 #define WIM_HDR_FLAG_WRITE_IN_PROGRESS  0x00000040 
168
169 /* Reparse point fixup ??? */
170 #define WIM_HDR_FLAG_RP_FIX             0x00000080
171
172 /* Unknown compression type */
173 #define WIM_HDR_FLAG_COMPRESS_RESERVED  0x00010000
174
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
178
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
182
183
184 #ifdef ENABLE_SECURITY_DATA
185 /* Structure for security data.  Each image in the WIM file has its own security
186  * data. */
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. */
191         u32 total_length;
192
193         /* The number of security descriptors in the array @descriptors, below. */
194         u32 num_entries;
195
196         /* Array of sizes of the descriptors in the array @descriptors. */
197         u64 *sizes;
198
199         /* Array of descriptors. */
200         u8 **descriptors;
201
202         /* keep track of how many WIMs reference this security data (used when
203          * exporting images between WIMs) */
204         u32 refcnt;
205 } WIMSecurityData;
206 #endif
207
208 /* Metadata resource for an image. */
209 struct image_metadata {
210         /* Pointer to the root dentry for the image. */
211         struct dentry    *root_dentry;
212
213 #ifdef ENABLE_SECURITY_DATA
214         /* Pointer to the security data for the image. */
215         struct wim_security_data *security_data;
216 #endif
217         /* A pointer to the lookup table entry for this image's metadata
218          * resource. */
219         struct lookup_table_entry *lookup_table_entry;
220
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. */
224         bool modified;
225
226 };
227
228 /* The opaque structure exposed to the wimlib API. */
229 typedef struct WIMStruct {
230
231         /* A pointer to the file indicated by @filename, opened for reading. */
232         FILE                *fp;
233
234         /* FILE pointer for the WIM file that is being written. */
235         FILE  *out_fp;
236
237         /* The name of the WIM file that has been opened. */
238         char                *filename;
239
240         /* The lookup table for the WIM file. */ 
241         struct lookup_table *lookup_table;
242
243         /* Pointer to the XML data read from the WIM file. */
244         u8                  *xml_data;
245
246         /* Information retrieved from the XML data, arranged
247          * in an orderly manner. */
248         struct wim_info      *wim_info;
249
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;
253
254         /* Name of the output directory for extraction. */
255         char  *output_dir;
256
257         /* The header of the WIM file. */
258         struct wim_header    hdr;
259
260         /* The type of links to create when extracting files (hard, symbolic, or
261          * none.) */
262         int    link_type;
263
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. */
267         int                  current_image;
268
269         /* True if files names are to be printed when doing extraction. 
270          * May be used for other things later. */
271         bool   verbose;
272
273         union {
274                 /* Set to true when extracting multiple images */
275                 bool is_multi_image_extraction;
276
277                 bool write_metadata;
278         };
279 } WIMStruct;
280
281
282 /* Inline utility functions for WIMStructs. */
283
284 static inline struct dentry *wim_root_dentry(WIMStruct *w)
285 {
286         return w->image_metadata[w->current_image - 1].root_dentry;
287 }
288
289 static inline struct dentry **wim_root_dentry_p(WIMStruct *w)
290 {
291         return &w->image_metadata[w->current_image - 1].root_dentry;
292 }
293
294 #ifdef ENABLE_SECURITY_DATA
295 static inline struct wim_security_data *wim_security_data(WIMStruct *w)
296 {
297         return w->image_metadata[w->current_image - 1].security_data;
298 }
299 #endif
300
301 static inline struct lookup_table_entry*
302 wim_metadata_lookup_table_entry(WIMStruct *w)
303 {
304         return w->image_metadata[w->current_image - 1].lookup_table_entry;
305 }
306
307 /* Nonzero if a struct resource_entry indicates a compressed resource. */
308 static inline int resource_is_compressed(const struct resource_entry *entry)
309 {
310         return (entry->flags & WIM_RESHDR_FLAG_COMPRESSED);
311 }
312
313 static inline struct image_metadata *wim_get_current_image_metadata(WIMStruct *w)
314 {
315         return &w->image_metadata[w->current_image - 1];
316 }
317
318 static inline bool wim_current_image_is_modified(const WIMStruct *w)
319 {
320         return w->image_metadata[w->current_image - 1].modified;
321 }
322
323 /* Prints a hash code field. */
324 static inline void print_hash(const u8 hash[])
325 {
326         print_byte_field(hash, WIM_HASH_SIZE);
327 }
328
329
330 /* header.c */
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);
334
335 /* integrity.c */
336 extern int write_integrity_table(FILE *out, u64 end_header_offset, 
337                                  u64 end_lookup_table_offset,
338                                  int show_progress);
339 extern int check_wim_integrity(WIMStruct *w, int show_progress, int *status);
340
341
342 /* resource.c */
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);
345
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);
350
351 extern int read_uncompressed_resource(FILE *fp, u64 offset, u64 len, 
352                                         u8 contents_ret[]);
353
354
355 extern int extract_resource_to_fd(WIMStruct *w, 
356                                   const struct resource_entry *entry, 
357                                   int fd, 
358                                   u64 size);
359
360 extern int extract_full_resource_to_fd(WIMStruct *w, 
361                                        const struct resource_entry *entry, 
362                                        int fd);
363
364 extern int read_metadata_resource(FILE *fp, 
365                                   const struct resource_entry *metadata,
366                                   int wim_ctype, 
367                                   struct image_metadata *image_metadata);
368
369 extern int resource_compression_type(int wim_ctype, int reshdr_flags);
370
371 static inline int read_full_resource(FILE *fp, u64 resource_size, 
372                                 u64 resource_original_size,
373                                 u64 resource_offset, 
374                                 int resource_ctype, void *contents_ret)
375 {
376         return read_resource(fp, resource_size, resource_original_size, 
377                                 resource_offset, resource_ctype,
378                                 resource_original_size, 0, contents_ret);
379 }
380
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);
388
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);
392
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);
396 #endif
397
398 /* wim.c */
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 *));
405
406 /* write.c */
407 extern int finish_write(WIMStruct *w, int image, int flags, 
408                         int write_lookup_table);
409
410 extern int begin_write(WIMStruct *w, const char *path, int flags);
411
412
413 #include "wimlib.h"
414
415 #endif /* _WIMLIB_INTERNAL_H */
416