]> wimlib.net Git - wimlib/blob - src/wimlib_internal.h
e9a10798b273a90f9e732dcf442bc14ef7376e85
[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
56 #define RINOK(expr) do { \
57         int __ret = (expr); \
58         if (__ret != 0) \
59                 return __ret; \
60 } while (0) 
61
62 /* Metadata for a resource in a WIM file. */
63 struct resource_entry {
64         /* Size, in bytes, of the resource in the WIM file. */
65         u64 size  : 56;
66
67         /* Bitwise or of one or more of the WIM_RESHDR_FLAG_* flags. */
68         u64 flags : 8;
69
70         /* Offset, in bytes, of the resource in the WIM file. */
71         u64 offset;
72
73         /* Uncompressed size of the resource in the WIM file.  Is the same as
74          * @size if the resource is uncompressed. */
75         u64 original_size;
76 };
77
78 /* Flags for the `flags' field of the struct resource_entry structure. */
79
80 /* ??? */
81 #define WIM_RESHDR_FLAG_FREE            0x01
82
83 /* Indicates that a file resource is a metadata resource. */
84 #define WIM_RESHDR_FLAG_METADATA        0x02
85
86 /* Indicates that a file resource is compressed. */
87 #define WIM_RESHDR_FLAG_COMPRESSED      0x04
88
89 /* ??? */
90 #define WIM_RESHDR_FLAG_SPANNED         0x08
91
92
93 /* Header at the very beginning of the WIM file. */
94 struct wim_header { 
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];     
98
99         /* size of WIM header in bytes. */
100         //u32 hdr_size;
101
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. */
105         //u32 version;
106
107         /* Bitwise OR of one or more of the WIM_HDR_FLAG_* defined below. */
108         u32 flags;
109
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".*/
114         //u32 chunk_size;
115         
116         /* A unique identifier for the WIM file. */
117         u8  guid[WIM_GID_LEN];
118
119         /* Part number of the WIM file in a spanned set. */
120         //u16 part_number;
121
122         /* Total number of parts in a spanned set. */
123         //u16 total_parts;
124
125         /* Number of images in the WIM file. */
126         u32 image_count;
127
128         /* Location, size, and flags of the lookup table of the WIM. */
129         struct resource_entry lookup_table_res_entry;
130
131         /* Location, size, and flags for the XML data of the WIM. */
132         struct resource_entry xml_res_entry;
133
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;
138
139         /* The index of the bootable image in the WIM file. If 0, there are no
140          * bootable images available. */
141         u32 boot_idx; 
142
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;
146
147         /* Reserved for future disuse */
148         //u8 unused[WIM_UNUSED_LEN];
149 };
150
151 /* Flags for the `flags' field of the struct wim_header. */
152
153
154 /* Reserved for future use by M$ */
155 #define WIM_HDR_FLAG_RESERVED           0x00000001
156
157 /* Files and metadata in the WIM are compressed. */
158 #define WIM_HDR_FLAG_COMPRESSION        0x00000002
159
160 /* WIM is read-only. */
161 #define WIM_HDR_FLAG_READONLY           0x00000004
162
163 /* Resource data specified by images in this WIM may be contained in a different
164  * WIM */
165 #define WIM_HDR_FLAG_SPANNED            0x00000008
166
167 /* The WIM contains resources only; no filesystem metadata. */
168 #define WIM_HDR_FLAG_RESOURCE_ONLY      0x00000010
169
170 /* The WIM contains metadata only. */
171 #define WIM_HDR_FLAG_METADATA_ONLY      0x00000020
172
173 /* Lock field to prevent multiple writers from writing the WIM concurrently. */
174 #define WIM_HDR_FLAG_WRITE_IN_PROGRESS  0x00000040 
175
176 /* Reparse point fixup ??? */
177 #define WIM_HDR_FLAG_RP_FIX             0x00000080
178
179 /* Unknown compression type */
180 #define WIM_HDR_FLAG_COMPRESS_RESERVED  0x00010000
181
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
185
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
189
190
191 #if 0
192 /* Structure for security data.  Each image in the WIM file has its own security
193  * data. */
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. */
198         u32 total_length;
199
200         /* The number of security descriptors in the array @descriptors, below. */
201         u32 num_entries;
202
203         /* Array of sizes of the descriptors in the array @descriptors. */
204         u64 *sizes;
205
206         /* Array of descriptors. */
207         u8 **descriptors;
208 } WIMSecurityData;
209 #endif
210
211 /* Metadata resource for an image. */
212 struct image_metadata {
213
214 #if 0
215         /* The security data for the image. */
216         WIMSecurityData      security_data;
217 #endif
218
219         /* The root dentry for the image. */
220         struct dentry    *root_dentry;
221
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. */
225         bool modified;
226
227         /* A pointer to the lookup table entry for this image's metadata
228          * resource. */
229         struct lookup_table_entry *lookup_table_entry;
230 };
231
232 /* The opaque structure exposed to the wimlib API. */
233 typedef struct WIMStruct {
234         /* The name of the WIM file that has been opened. */
235         char                *filename;
236
237         /* A pointer to the file indicated by @filename, opened for reading. */
238         FILE                *fp;
239
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. */
243         int                  current_image;
244
245         /* The lookup table for the WIM file. */ 
246         struct lookup_table *lookup_table;
247
248         /* The header of the WIM file. */
249         struct wim_header    hdr;
250
251         /* Pointer to the XML data read from the WIM file. */
252         u8                  *xml_data;
253
254         /* Information retrieved from the XML data, arranged
255          * in an orderly manner. */
256         struct wim_info      *wim_info;
257
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;
261
262         /* True if files names are to be printed when doing extraction. 
263          * May be used for other things later. */
264         bool   verbose;
265
266         /* The type of links to create when extracting files (hard, symbolic, or
267          * none.) */
268         int    link_type;
269
270         /* Name of the output directory for extraction. */
271         char  *output_dir;
272
273         /* Set to true when extracting multiple images */
274         bool   is_multi_image_extraction;
275
276         /* FILE pointer for the WIM file that is being written. */
277         FILE  *out_fp;
278 } WIMStruct;
279
280
281 /* Inline utility functions for WIMStructs. */
282
283 static inline struct dentry *wim_root_dentry(WIMStruct *w)
284 {
285         return w->image_metadata[w->current_image - 1].root_dentry;
286 }
287
288 static inline struct dentry **wim_root_dentry_p(WIMStruct *w)
289 {
290         return &w->image_metadata[w->current_image - 1].root_dentry;
291 }
292
293 #if 0
294 static inline WIMSecurityData *wim_security_data(WIMStruct *w)
295 {
296         return &w->image_metadata[w->current_image - 1].security_data;
297 }
298 #endif
299
300 static inline struct lookup_table_entry*
301 wim_metadata_lookup_table_entry(WIMStruct *w)
302 {
303         return w->image_metadata[w->current_image - 1].lookup_table_entry;
304 }
305
306 /* Nonzero if a struct resource_entry indicates a compressed resource. */
307 static inline int resource_is_compressed(const struct resource_entry *entry)
308 {
309         return (entry->flags & WIM_RESHDR_FLAG_COMPRESSED);
310 }
311
312 static inline struct image_metadata *wim_get_current_image_metadata(WIMStruct *w)
313 {
314         return &w->image_metadata[w->current_image - 1];
315 }
316
317 static inline bool wim_current_image_is_modified(const WIMStruct *w)
318 {
319         return w->image_metadata[w->current_image - 1].modified;
320 }
321
322 /* Prints a hash code field. */
323 static inline void print_hash(const u8 hash[])
324 {
325         print_byte_field(hash, WIM_HASH_SIZE);
326 }
327
328
329 /* header.c */
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);
333
334 /* integrity.c */
335 extern int write_integrity_table(FILE *out, u64 end_header_offset, 
336                                  u64 end_lookup_table_offset,
337                                  int show_progress);
338 extern int check_wim_integrity(WIMStruct *w, int show_progress, int *status);
339
340
341 /* resource.c */
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);
344
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);
349
350 extern int read_uncompressed_resource(FILE *fp, u64 offset, u64 len, 
351                                         u8 contents_ret[]);
352
353
354 extern int extract_resource_to_fd(WIMStruct *w, 
355                                   const struct resource_entry *entry, 
356                                   int fd, 
357                                   u64 size);
358
359 extern int extract_full_resource_to_fd(WIMStruct *w, 
360                                        const struct resource_entry *entry, 
361                                        int fd);
362
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);
366
367 extern int resource_compression_type(int wim_ctype, int reshdr_flags);
368
369 static inline int read_full_resource(FILE *fp, u64 resource_size, 
370                                 u64 resource_original_size,
371                                 u64 resource_offset, 
372                                 int resource_ctype, void *contents_ret)
373 {
374         return read_resource(fp, resource_size, resource_original_size, 
375                                 resource_offset, resource_ctype,
376                                 resource_original_size, 0, contents_ret);
377 }
378
379 #if 0
380 /* security.c */
381 bool read_security_data(const u8 metadata_resource[], 
382                 u64 metadata_resource_len, WIMSecurityData *sd);
383
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);
388 #endif
389
390 /* wim.c */
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 *));
397
398 /* write.c */
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);
402
403
404 #include "wimlib.h"
405
406 #endif /* _WIMLIB_INTERNAL_H */
407