4 * Read uncompressed and compressed metadata and file resources.
8 * Copyright (C) 2010 Carl Thijssen
9 * Copyright (C) 2012 Eric Biggers
11 * This file is part of wimlib, a library for working with WIM files.
13 * wimlib is free software; you can redistribute it and/or modify it under the
14 * terms of the GNU Lesser General Public License as published by the Free
15 * Software Foundation; either version 2.1 of the License, or (at your option)
18 * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20 * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
23 * You should have received a copy of the GNU Lesser General Public License
24 * along with wimlib; if not, see http://www.gnu.org/licenses/.
27 #include "wimlib_internal.h"
28 #include "lookup_table.h"
41 * Reads all or part of a compressed resource into an in-memory buffer.
43 * @fp: The FILE* for the WIM file.
44 * @resource_compressed_size: The compressed size of the resource.
45 * @resource_uncompressed_size: The uncompressed size of the resource.
46 * @resource_offset: The offset of the start of the resource from
47 * the start of the stream @fp.
48 * @resource_ctype: The compression type of the resource.
49 * @len: The number of bytes of uncompressed data to read from
51 * @offset: The offset of the bytes to read within the uncompressed
53 * @contents_len: An array into which the uncompressed data is written.
54 * It must be at least @len bytes long.
56 * Returns zero on success, nonzero on failure.
58 static int read_compressed_resource(FILE *fp, u64 resource_compressed_size,
59 u64 resource_uncompressed_size,
60 u64 resource_offset, int resource_ctype,
61 u64 len, u64 offset, u8 contents_ret[])
64 DEBUG2("comp size = %"PRIu64", uncomp size = %"PRIu64", "
65 "res offset = %"PRIu64"",
66 resource_compressed_size,
67 resource_uncompressed_size,
69 DEBUG2("resource_ctype = %s, len = %"PRIu64", offset = %"PRIu64"",
70 wimlib_get_compression_type_string(resource_ctype), len, offset);
75 int (*decompress)(const void *, uint, void *, uint);
76 /* Set the appropriate decompress function. */
77 if (resource_ctype == WIM_COMPRESSION_TYPE_LZX)
78 decompress = lzx_decompress;
80 decompress = xpress_decompress;
82 /* The structure of a compressed resource consists of a table of chunk
83 * offsets followed by the chunks themselves. Each chunk consists of
84 * compressed data, and there is one chunk for each WIM_CHUNK_SIZE =
85 * 32768 bytes of the uncompressed file, with the last chunk having any
88 * The chunk offsets are measured relative to the end of the chunk
89 * table. The first chunk is omitted from the table in the WIM file
90 * because its offset is implicitly given by the fact that it directly
91 * follows the chunk table and therefore must have an offset of 0.
94 /* Calculate how many chunks the resource conists of in its entirety. */
95 u64 num_chunks = (resource_uncompressed_size + WIM_CHUNK_SIZE - 1) /
97 /* As mentioned, the first chunk has no entry in the chunk table. */
98 u64 num_chunk_entries = num_chunks - 1;
101 /* The index of the chunk that the read starts at. */
102 u64 start_chunk = offset / WIM_CHUNK_SIZE;
103 /* The byte offset at which the read starts, within the start chunk. */
104 u64 start_chunk_offset = offset % WIM_CHUNK_SIZE;
106 /* The index of the chunk that contains the last byte of the read. */
107 u64 end_chunk = (offset + len - 1) / WIM_CHUNK_SIZE;
108 /* The byte offset of the last byte of the read, within the end chunk */
109 u64 end_chunk_offset = (offset + len - 1) % WIM_CHUNK_SIZE;
111 /* Number of chunks that are actually needed to read the requested part
113 u64 num_needed_chunks = end_chunk - start_chunk + 1;
115 /* If the end chunk is not the last chunk, an extra chunk entry is
116 * needed because we need to know the offset of the chunk after the last
117 * chunk read to figure out the size of the last read chunk. */
118 if (end_chunk != num_chunks - 1)
121 /* Declare the chunk table. It will only contain offsets for the chunks
122 * that are actually needed for this read. */
123 u64 chunk_offsets[num_needed_chunks];
125 /* Set the implicit offset of the first chunk if it is included in the
128 * Note: M$'s documentation includes a picture that shows the first
129 * chunk starting right after the chunk entry table, labeled as offset
130 * 0x10. However, in the actual file format, the offset is measured
131 * from the end of the chunk entry table, so the first chunk has an
133 if (start_chunk == 0)
134 chunk_offsets[0] = 0;
136 /* According to M$'s documentation, if the uncompressed size of
137 * the file is greater than 4 GB, the chunk entries are 8-byte
138 * integers. Otherwise, they are 4-byte integers. */
139 u64 chunk_entry_size = (resource_uncompressed_size >= (u64)1 << 32) ?
142 /* Size of the full chunk table in the WIM file. */
143 u64 chunk_table_size = chunk_entry_size * num_chunk_entries;
145 /* Read the needed chunk offsets from the table in the WIM file. */
147 /* Index, in the WIM file, of the first needed entry in the
149 u64 start_table_idx = (start_chunk == 0) ? 0 : start_chunk - 1;
151 /* Number of entries we need to actually read from the chunk
152 * table (excludes the implicit first chunk). */
153 u64 num_needed_chunk_entries = (start_chunk == 0) ?
154 num_needed_chunks - 1 : num_needed_chunks;
156 /* Skip over unneeded chunk table entries. */
157 u64 file_offset_of_needed_chunk_entries = resource_offset +
158 start_table_idx * chunk_entry_size;
159 if (fseeko(fp, file_offset_of_needed_chunk_entries, SEEK_SET) != 0) {
160 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
161 "chunk table of compressed resource",
162 file_offset_of_needed_chunk_entries);
163 return WIMLIB_ERR_READ;
166 /* Number of bytes we need to read from the chunk table. */
167 size_t size = num_needed_chunk_entries * chunk_entry_size;
169 u8 chunk_tab_buf[size];
171 if (fread(chunk_tab_buf, 1, size, fp) != size)
174 /* Now fill in chunk_offsets from the entries we have read in
177 u64 *chunk_tab_p = chunk_offsets;
178 if (start_chunk == 0)
181 if (chunk_entry_size == 4) {
182 u32 *entries = (u32*)chunk_tab_buf;
183 while (num_needed_chunk_entries--)
184 *chunk_tab_p++ = to_le32(*entries++);
186 u64 *entries = (u64*)chunk_tab_buf;
187 while (num_needed_chunk_entries--)
188 *chunk_tab_p++ = to_le64(*entries++);
191 /* Done with the chunk table now. We must now seek to the first chunk
192 * that is needed for the read. */
194 u64 file_offset_of_first_needed_chunk = resource_offset +
195 chunk_table_size + chunk_offsets[0];
196 if (fseeko(fp, file_offset_of_first_needed_chunk, SEEK_SET) != 0) {
197 ERROR_WITH_ERRNO("Failed to seek to byte %"PRIu64" to read "
198 "first chunk of compressed resource",
199 file_offset_of_first_needed_chunk);
200 return WIMLIB_ERR_READ;
203 /* Pointer to current position in the output buffer for uncompressed
205 u8 *out_p = (u8*)contents_ret;
207 /* Buffer for compressed data. While most compressed chunks will have a
208 * size much less than WIM_CHUNK_SIZE, WIM_CHUNK_SIZE - 1 is the maximum
209 * size in the worst-case. This assumption is valid only if chunks that
210 * happen to compress to more than the uncompressed size (i.e. a
211 * sequence of random bytes) are always stored uncompressed. But this seems
212 * to be the case in M$'s WIM files, even though it is undocumented. */
213 u8 compressed_buf[WIM_CHUNK_SIZE - 1];
216 /* Decompress all the chunks. */
217 for (u64 i = start_chunk; i <= end_chunk; i++) {
219 DEBUG2("Chunk %"PRIu64" (start %"PRIu64", end %"PRIu64").",
220 i, start_chunk, end_chunk);
222 /* Calculate the sizes of the compressed chunk and of the
223 * uncompressed chunk. */
224 uint compressed_chunk_size, uncompressed_chunk_size;
225 if (i != num_chunks - 1) {
226 /* All the chunks except the last one in the resource
227 * expand to WIM_CHUNK_SIZE uncompressed, and the amount
228 * of compressed data for the chunk is given by the
229 * difference of offsets in the chunk offset table. */
230 compressed_chunk_size = chunk_offsets[i + 1 - start_chunk] -
231 chunk_offsets[i - start_chunk];
232 uncompressed_chunk_size = WIM_CHUNK_SIZE;
234 /* The last compressed chunk consists of the remaining
235 * bytes in the file resource, and the last uncompressed
236 * chunk has size equal to however many bytes are left-
237 * that is, the remainder of the uncompressed size when
238 * divided by WIM_CHUNK_SIZE.
240 * Note that the resource_compressed_size includes the
241 * chunk table, so the size of it must be subtracted. */
242 compressed_chunk_size = resource_compressed_size -
244 chunk_offsets[i - start_chunk];
246 uncompressed_chunk_size = resource_uncompressed_size %
249 /* If the remainder is 0, the last chunk actually
250 * uncompresses to a full WIM_CHUNK_SIZE bytes. */
251 if (uncompressed_chunk_size == 0)
252 uncompressed_chunk_size = WIM_CHUNK_SIZE;
255 DEBUG2("compressed_chunk_size = %u, "
256 "uncompressed_chunk_size = %u",
257 compressed_chunk_size, uncompressed_chunk_size);
260 /* Figure out how much of this chunk we actually need to read */
262 if (i == start_chunk)
263 start_offset = start_chunk_offset;
268 end_offset = end_chunk_offset;
270 end_offset = WIM_CHUNK_SIZE - 1;
272 u64 partial_chunk_size = end_offset + 1 - start_offset;
273 bool is_partial_chunk = (partial_chunk_size !=
274 uncompressed_chunk_size);
276 DEBUG2("start_offset = %u, end_offset = %u", start_offset,
278 DEBUG2("partial_chunk_size = %u", partial_chunk_size);
280 /* This is undocumented, but chunks can be uncompressed. This
281 * appears to always be the case when the compressed chunk size
282 * is equal to the uncompressed chunk size. */
283 if (compressed_chunk_size == uncompressed_chunk_size) {
284 /* Probably an uncompressed chunk */
286 if (start_offset != 0) {
287 if (fseeko(fp, start_offset, SEEK_CUR) != 0) {
288 ERROR_WITH_ERRNO("Uncompressed partial "
289 "chunk fseek() error");
290 return WIMLIB_ERR_READ;
293 if (fread(out_p, 1, partial_chunk_size, fp) !=
297 /* Compressed chunk */
300 /* Read the compressed data into compressed_buf. */
301 if (fread(compressed_buf, 1, compressed_chunk_size,
302 fp) != compressed_chunk_size)
305 /* For partial chunks we must buffer the uncompressed
306 * data because we don't need all of it. */
307 if (is_partial_chunk) {
308 u8 uncompressed_buf[uncompressed_chunk_size];
310 ret = decompress(compressed_buf,
311 compressed_chunk_size,
313 uncompressed_chunk_size);
315 return WIMLIB_ERR_DECOMPRESSION;
316 memcpy(out_p, uncompressed_buf + start_offset,
319 ret = decompress(compressed_buf,
320 compressed_chunk_size,
322 uncompressed_chunk_size);
324 return WIMLIB_ERR_DECOMPRESSION;
328 /* Advance the pointer into the uncompressed output data by the
329 * number of uncompressed bytes that were written. */
330 out_p += partial_chunk_size;
337 ERROR("Unexpected EOF in compressed file resource");
339 ERROR_WITH_ERRNO("Error reading compressed file resource");
340 return WIMLIB_ERR_READ;
344 * Reads uncompressed data from an open file stream.
346 int read_uncompressed_resource(FILE *fp, u64 offset, u64 len,
349 if (fseeko(fp, offset, SEEK_SET) != 0) {
350 ERROR("Failed to seek to byte %"PRIu64" of input file "
351 "to read uncompressed resource (len = %"PRIu64")",
353 return WIMLIB_ERR_READ;
355 if (fread(contents_ret, 1, len, fp) != len) {
357 ERROR("Unexpected EOF in uncompressed file resource");
359 ERROR("Failed to read %"PRIu64" bytes from "
360 "uncompressed resource at offset %"PRIu64,
363 return WIMLIB_ERR_READ;
371 /* Reads the contents of a struct resource_entry, as represented in the on-disk
372 * format, from the memory pointed to by @p, and fills in the fields of @entry.
373 * A pointer to the byte after the memory read at @p is returned. */
374 const u8 *get_resource_entry(const u8 *p, struct resource_entry *entry)
379 p = get_u56(p, &size);
380 p = get_u8(p, &flags);
382 entry->flags = flags;
383 p = get_u64(p, &entry->offset);
384 p = get_u64(p, &entry->original_size);
388 /* Copies the struct resource_entry @entry to the memory pointed to by @p in the
389 * on-disk format. A pointer to the byte after the memory written at @p is
391 u8 *put_resource_entry(u8 *p, const struct resource_entry *entry)
393 p = put_u56(p, entry->size);
394 p = put_u8(p, entry->flags);
395 p = put_u64(p, entry->offset);
396 p = put_u64(p, entry->original_size);
400 static int __read_wim_resource(const struct lookup_table_entry *lte,
401 u8 buf[], size_t size, u64 offset, bool raw)
403 wimlib_assert(offset + size <= wim_resource_size(lte));
407 switch (lte->resource_location) {
408 case RESOURCE_IN_WIM:
409 wimlib_assert(lte->wim);
410 wimlib_assert(lte->wim->fp);
411 ctype = wim_resource_compression_type(lte);
412 if (ctype == WIM_COMPRESSION_TYPE_NONE &&
413 lte->resource_entry.original_size !=
414 lte->resource_entry.size) {
415 ERROR("WIM resource at offset %"PRIu64", size %"PRIu64
416 "has an original size of %"PRIu64", but is "
418 lte->resource_entry.offset,
419 lte->resource_entry.size,
420 lte->resource_entry.original_size);
421 return WIMLIB_ERR_INVALID_RESOURCE_SIZE;
423 if (raw || ctype == WIM_COMPRESSION_TYPE_NONE)
424 return read_uncompressed_resource(lte->wim->fp,
427 return read_compressed_resource(lte->wim->fp,
428 lte->resource_entry.size,
429 lte->resource_entry.original_size,
430 lte->resource_entry.offset,
431 ctype, size, offset, buf);
433 case RESOURCE_IN_STAGING_FILE:
434 wimlib_assert(lte->staging_file_name);
437 case RESOURCE_IN_FILE_ON_DISK:
438 wimlib_assert(lte->file_on_disk);
439 if (lte->file_on_disk_fp) {
440 fp = lte->file_on_disk_fp;
442 fp = fopen(lte->file_on_disk, "rb");
444 ERROR_WITH_ERRNO("Failed to open the file "
445 "`%s'", lte->file_on_disk);
448 ret = read_uncompressed_resource(lte->file_on_disk_fp,
450 if (fp != lte->file_on_disk_fp)
454 case RESOURCE_IN_ATTACHED_BUFFER:
455 wimlib_assert(lte->attached_buffer);
456 memcpy(buf, lte->attached_buffer + offset, size);
464 int read_wim_resource(const struct lookup_table_entry *lte, u8 buf[],
465 size_t size, u64 offset)
467 return __read_wim_resource(lte, buf, size, offset, false);
470 int read_full_wim_resource(const struct lookup_table_entry *lte, u8 buf[])
472 return __read_wim_resource(lte, buf, lte->resource_entry.original_size,
479 u64 original_resource_size;
480 u64 bytes_per_chunk_entry;
488 begin_wim_resource_chunk_tab(const struct lookup_table_entry *lte,
490 struct chunk_table **chunk_tab_ret)
492 u64 size = wim_resource_size(lte);
493 u64 num_chunks = (size + WIM_CHUNK_SIZE - 1) / WIM_CHUNK_SIZE;
494 struct chunk_table *chunk_tab = MALLOC(sizeof(struct chunk_table) +
495 num_chunks * sizeof(u64));
499 ERROR("Failed to allocate chunk table for %"PRIu64" byte "
501 ret = WIMLIB_ERR_NOMEM;
504 chunk_tab->file_offset = ftello(out_fp);
505 if (chunk_tab->file_offset == -1) {
506 ERROR_WITH_ERRNO("Failed to get file offset in output WIM");
507 ret = WIMLIB_ERR_WRITE;
510 chunk_tab->num_chunks = num_chunks;
511 chunk_tab->cur_offset_p = chunk_tab->offsets;
512 chunk_tab->original_resource_size = lte->resource_entry.original_size;
513 chunk_tab->bytes_per_chunk_entry =
514 (lte->resource_entry.original_size >= (1ULL << 32))
516 chunk_tab->table_disk_size = chunk_tab->bytes_per_chunk_entry *
519 if (fwrite(chunk_tab, 1, chunk_tab->table_disk_size, out_fp) !=
520 chunk_tab->table_disk_size)
522 ERROR_WITH_ERRNO("Failed to write chunk table in compressed "
524 ret = WIMLIB_ERR_WRITE;
528 *chunk_tab_ret = chunk_tab;
533 static int compress_chunk(const u8 chunk[], unsigned chunk_sz,
534 u8 compressed_chunk[],
535 unsigned *compressed_chunk_len_ret,
538 unsigned compressed_chunk_sz;
539 int (*compress)(const void *, unsigned, void *, unsigned *);
540 if (ctype == WIM_COMPRESSION_TYPE_LZX)
541 compress = lzx_compress;
543 compress = xpress_compress;
544 return (*compress)(chunk, chunk_sz, compressed_chunk,
545 compressed_chunk_len_ret);
548 static int write_wim_resource_chunk(const u8 chunk[], unsigned chunk_size,
549 FILE *out_fp, int out_ctype,
550 struct chunk_table *chunk_tab)
553 unsigned out_chunk_size;
555 if (out_ctype == WIM_COMPRESSION_TYPE_NONE) {
557 out_chunk_size = chunk_size;
559 u8 *compressed_chunk = alloca(chunk_size);
561 unsigned compressed_chunk_len;
563 wimlib_assert(chunk_tab != NULL);
565 ret = compress_chunk(chunk, chunk_size, compressed_chunk,
566 &out_chunk_size, out_ctype);
571 out_chunk_size = chunk_size;
573 out_chunk = compressed_chunk;
575 *chunk_tab->cur_offset_p++ = chunk_tab->cur_offset;
576 chunk_tab->cur_offset += out_chunk_size;
579 if (fwrite(out_chunk, 1, out_chunk_size, out_fp) != out_chunk_size) {
580 ERROR_WITH_ERRNO("Failed to write WIM resource chunk");
581 return WIMLIB_ERR_WRITE;
587 finish_wim_resource_chunk_tab(const struct chunk_table *chunk_tab,
590 if (fseeko(out_fp, chunk_tab->file_offset, SEEK_SET) != 0) {
591 ERROR_WITH_ERRNO("Failed to seet to byte "PRIu64" of output "
592 "WIM file", chunk_tab->file_offset);
593 return WIMLIB_ERR_WRITE;
596 if (chunk_tab->bytes_per_chunk_entry == 8) {
597 array_to_le64(chunk_tab->offsets, chunk_tab->num_chunks - 1);
599 for (u64 i = 0; i < chunk_tab->num_chunks - 1; i++)
600 ((u32*)chunk_tab->offsets)[i] =
601 to_le32(chunk_tab->offsets[i]);
603 if (fwrite(chunk_tab->offsets, 1, chunk_tab->table_disk_size, out_fp) !=
604 chunk_tab->table_disk_size)
606 ERROR_WITH_ERRNO("Failed to write chunk table in compressed "
608 return WIMLIB_ERR_WRITE;
610 if (fseeko(out_fp, chunk_tab->file_offset, SEEK_SET) != 0) {
611 ERROR_WITH_ERRNO("Failed to seet to byte "PRIu64" of output "
612 "WIM file", chunk_tab->file_offset);
613 return WIMLIB_ERR_WRITE;
618 static int write_wim_resource(const struct lookup_table_entry *lte,
619 FILE *out_fp, int out_ctype)
621 u64 bytes_remaining = wim_resource_size(lte);
622 char buf[min(WIM_CHUNK_SIZE, bytes_remaining)];
625 bool raw = wim_resource_compression_type(lte) == out_ctype;
626 struct chunk_table *chunk_tab = NULL;
629 out_ctype = WIM_COMPRESSION_TYPE_NONE;
631 if (out_ctype != WIM_COMPRESSION_TYPE_NONE) {
632 ret = begin_wim_resource_chunk_tab(lte, out_fp,
638 while (bytes_remaining) {
639 u64 to_read = min(bytes_remaining, WIM_CHUNK_SIZE);
640 ret = __read_wim_resource(lte, buf, to_read, offset, raw);
643 ret = write_wim_resource_chunk(buf, to_read, out_fp,
644 out_ctype, chunk_tab);
647 bytes_remaining -= to_read;
650 if (out_ctype != WIM_COMPRESSION_TYPE_NONE)
651 ret = finish_wim_resource_chunk_tab(chunk_tab, out_fp);
655 static int write_wim_resource_from_buffer(const u8 *buf, u64 buf_size,
656 FILE *out_fp, int out_ctype)
658 struct lookup_table_entry lte;
659 lte.resource_entry.flags = 0;
660 lte.resource_entry.original_size = buf_size;
661 lte.resource_entry.size = buf_size;
662 lte.resource_entry.offset = 0;
663 lte.resource_location = RESOURCE_IN_ATTACHED_BUFFER;
664 lte.attached_buffer = (u8*)buf;
665 return write_wim_resource(<e, out_fp, out_ctype);
669 * Extracts the first @size bytes of the resource specified by @lte to the open
670 * file @fd. Returns nonzero on error.
672 int extract_wim_resource_to_fd(const struct lookup_table_entry *lte, int fd,
675 u64 bytes_remaining = size;
676 char buf[min(WIM_CHUNK_SIZE, bytes_remaining)];
680 while (bytes_remaining) {
681 u64 to_read = min(bytes_remaining, WIM_CHUNK_SIZE);
682 ret = read_wim_resource(lte, buf, to_read, offset);
685 if (full_write(fd, buf, to_read) < 0) {
686 ERROR_WITH_ERRNO("Error extracting WIM resource");
687 return WIMLIB_ERR_WRITE;
689 bytes_remaining -= to_read;
695 int extract_full_wim_resource_to_fd(const struct lookup_table_entry *lte, int fd)
697 return extract_wim_resource_to_fd(lte, fd,
698 lte->resource_entry.original_size);
702 * Copies the file resource specified by the lookup table entry @lte from the
703 * input WIM, pointed to by the fp field of the WIMStruct, to the output WIM,
704 * pointed to by the out_fp field of the WIMStruct.
706 * The output_resource_entry, out_refcnt, and part_number fields of @lte are
709 * Metadata resources are not copied (they are handled elsewhere for joining and
712 int copy_resource(struct lookup_table_entry *lte, void *wim)
718 if ((lte->resource_entry.flags & WIM_RESHDR_FLAG_METADATA) &&
722 new_offset = ftello(w->out_fp);
723 if (new_offset == -1) {
724 ERROR_WITH_ERRNO("Could not get offset in output WIM");
725 return WIMLIB_ERR_WRITE;
729 ret = write_wim_resource(lte, w->out_fp,
730 wimlib_get_compression_type((WIMStruct*)w));
734 memcpy(<e->output_resource_entry, <e->resource_entry,
735 sizeof(struct resource_entry));
737 lte->output_resource_entry.offset = new_offset;
738 lte->out_refcnt = lte->refcnt;
739 lte->part_number = w->hdr.part_number;
744 * Writes a dentry's resources, including the main file resource as well as all
745 * alternate data streams, to the output file.
747 * @dentry: The dentry for the file.
748 * @wim_p: A pointer to the WIMStruct. The fields of interest to this
749 * function are the input and output file streams and the lookup
752 * @return zero on success, nonzero on failure.
754 int write_dentry_resources(struct dentry *dentry, void *wim_p)
756 WIMStruct *w = wim_p;
758 struct lookup_table_entry *lte;
759 int ctype = wimlib_get_compression_type(w);
761 for (unsigned i = 0; i <= dentry->num_ads; i++) {
762 lte = dentry_stream_lte(dentry, i, w->lookup_table);
763 if (lte && ++lte->out_refcnt == 1) {
764 ret = write_wim_resource(lte, w->out_fp, ctype);
773 * Reads the metadata metadata resource from the WIM file. The metadata
774 * resource consists of the security data, followed by the directory entry for
775 * the root directory, followed by all the other directory entries in the
776 * filesystem. The subdir_offset field of each directory entry gives the start
777 * of its child entries from the beginning of the metadata resource. An
778 * end-of-directory is signaled by a directory entry of length '0', really of
779 * length 8, because that's how long the 'length' field is.
781 * @fp: The FILE* for the input WIM file.
782 * @wim_ctype: The compression type of the WIM file.
783 * @imd: Pointer to the image metadata structure. Its
784 * `lookup_table_entry' member specifies the lookup table entry for
785 * the metadata resource. The rest of the image metadata entry
786 * will be filled in by this function.
788 * @return: Zero on success, nonzero on failure.
790 int read_metadata_resource(FILE *fp, int wim_ctype, struct image_metadata *imd)
796 struct dentry *dentry;
797 struct wim_security_data *sd;
798 struct link_group_table *lgt;
799 const struct lookup_table_entry *metadata_lte;
800 const struct resource_entry *res_entry;
802 metadata_lte = imd->metadata_lte;
803 res_entry = &metadata_lte->resource_entry;
805 DEBUG("Reading metadata resource: length = %"PRIu64", "
806 "offset = %"PRIu64"",
807 res_entry->original_size, res_entry->offset);
809 if (res_entry->original_size < 8) {
810 ERROR("Expected at least 8 bytes for the metadata resource");
811 return WIMLIB_ERR_INVALID_RESOURCE_SIZE;
814 /* Allocate memory for the uncompressed metadata resource. */
815 buf = MALLOC(res_entry->original_size);
818 ERROR("Failed to allocate %"PRIu64" bytes for uncompressed "
819 "metadata resource", res_entry->original_size);
820 return WIMLIB_ERR_NOMEM;
823 /* Determine the compression type of the metadata resource. */
825 /* Read the metadata resource into memory. (It may be compressed.) */
826 ret = read_full_wim_resource(metadata_lte, buf);
830 DEBUG("Finished reading metadata resource into memory.");
832 /* The root directory entry starts after security data, on an 8-byte
835 * The security data starts with a 4-byte integer giving its total
838 /* Read the security data into a wim_security_data structure. */
839 ret = read_security_data(buf, res_entry->original_size, &sd);
843 dentry = MALLOC(sizeof(struct dentry));
845 ERROR("Failed to allocate %zu bytes for root dentry",
846 sizeof(struct dentry));
847 ret = WIMLIB_ERR_NOMEM;
848 goto out_free_security_data;
851 get_u32(buf, &dentry_offset);
852 if (dentry_offset == 0)
854 dentry_offset = (dentry_offset + 7) & ~7;
856 ret = read_dentry(buf, res_entry->original_size, dentry_offset, dentry);
857 /* This is the root dentry, so set its pointers correctly. */
858 dentry->parent = dentry;
859 dentry->next = dentry;
860 dentry->prev = dentry;
862 goto out_free_dentry_tree;
864 DEBUG("Reading dentry tree");
865 /* Now read the entire directory entry tree. */
866 ret = read_dentry_tree(buf, res_entry->original_size, dentry);
868 goto out_free_dentry_tree;
870 DEBUG("Calculating dentry full paths");
871 /* Calculate the full paths in the dentry tree. */
872 ret = for_dentry_in_tree(dentry, calculate_dentry_full_path, NULL);
874 goto out_free_dentry_tree;
876 DEBUG("Building link group table");
877 /* Build hash table that maps hard link group IDs to dentry sets */
878 lgt = new_link_group_table(9001);
880 goto out_free_dentry_tree;
881 ret = for_dentry_in_tree(dentry, link_group_table_insert, lgt);
885 DEBUG("Freeing duplicate ADS entries in link group table");
886 ret = link_groups_free_duplicate_data(lgt);
889 DEBUG("Done reading image metadata");
892 imd->security_data = sd;
893 imd->root_dentry = dentry;
896 free_link_group_table(lgt);
897 out_free_dentry_tree:
898 free_dentry_tree(dentry, NULL);
899 out_free_security_data:
900 free_security_data(sd);
906 /* Write the metadata resource for the current image. */
907 int write_metadata_resource(WIMStruct *w)
915 struct lookup_table_entry *lte;
916 off_t metadata_offset;
917 u64 metadata_original_size;
918 u64 metadata_compressed_size;
920 u8 hash[SHA1_HASH_SIZE];
922 DEBUG("Writing metadata resource for image %d", w->current_image);
925 root = wim_root_dentry(w);
926 metadata_ctype = wimlib_get_compression_type(w);
927 metadata_offset = ftello(out);
928 if (metadata_offset == -1)
929 return WIMLIB_ERR_WRITE;
931 struct wim_security_data *sd = wim_security_data(w);
933 subdir_offset = sd->total_length + root->length + 8;
935 subdir_offset = 8 + root->length + 8;
936 calculate_subdir_offsets(root, &subdir_offset);
937 metadata_original_size = subdir_offset;
938 buf = MALLOC(metadata_original_size);
940 ERROR("Failed to allocate %"PRIu64" bytes for "
941 "metadata resource", metadata_original_size);
942 return WIMLIB_ERR_NOMEM;
945 p = write_security_data(sd, buf);
947 DEBUG("Writing dentry tree.");
948 p = write_dentry_tree(root, p);
950 /* Like file resources, the lookup table entry for a metadata resource
951 * uses for the hash code a SHA1 message digest of its uncompressed
953 sha1_buffer(buf, metadata_original_size, hash);
956 ret = write_wim_resource_from_buffer(buf, metadata_original_size,
957 out, metadata_ctype);
962 DEBUG("Updating metadata lookup table entry (size %zu)",
963 metadata_original_size);
965 /* Update the lookup table entry, including the hash and output resource
966 * entry fields, for this image's metadata resource. */
967 lte = wim_metadata_lookup_table_entry(w);
969 if (!hashes_equal(hash, lte->hash)) {
970 lookup_table_unlink(w->lookup_table, lte);
971 copy_hash(lte->hash, hash);
972 lookup_table_insert(w->lookup_table, lte);
974 lte->output_resource_entry.original_size = metadata_original_size;
975 lte->output_resource_entry.offset = metadata_offset;
976 lte->output_resource_entry.size = metadata_compressed_size;
977 lte->output_resource_entry.flags = WIM_RESHDR_FLAG_METADATA;
978 if (metadata_ctype != WIM_COMPRESSION_TYPE_NONE)
979 lte->output_resource_entry.flags |= WIM_RESHDR_FLAG_COMPRESSED;