]> wimlib.net Git - wimlib/blob - src/resource.c
74c9bb8b9a9570aa52110139e73cc9a0907982bb
[wimlib] / src / resource.c
1 /*
2  * resource.c
3  *
4  * Code for reading blobs and resources, including compressed WIM resources.
5  */
6
7 /*
8  * Copyright (C) 2012, 2013, 2015 Eric Biggers
9  *
10  * This file is free software; you can redistribute it and/or modify it under
11  * the terms of the GNU Lesser General Public License as published by the Free
12  * Software Foundation; either version 3 of the License, or (at your option) any
13  * later version.
14  *
15  * This file is distributed in the hope that it will be useful, but WITHOUT
16  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this file; if not, see http://www.gnu.org/licenses/.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31
32 #include "wimlib/alloca.h"
33 #include "wimlib/assert.h"
34 #include "wimlib/bitops.h"
35 #include "wimlib/blob_table.h"
36 #include "wimlib/endianness.h"
37 #include "wimlib/error.h"
38 #include "wimlib/file_io.h"
39 #include "wimlib/ntfs_3g.h"
40 #include "wimlib/resource.h"
41 #include "wimlib/sha1.h"
42 #include "wimlib/wim.h"
43 #include "wimlib/win32.h"
44
45 /*
46  *                         Compressed WIM resources
47  *
48  * A compressed resource in a WIM consists of a sequence of chunks.  Each chunk
49  * decompresses to the same size except possibly for the last, which
50  * decompresses to the remaining size.  Chunks that did not compress to less
51  * than their original size are stored uncompressed.
52  *
53  * We support three variations on this resource format, independently of the
54  * compression type and chunk size which can vary as well:
55  *
56  * - Original resource format: immediately before the compressed chunks, the
57  *   "chunk table" provides the offset, in bytes relative to the end of the
58  *   chunk table, of the start of each compressed chunk, except for the first
59  *   chunk which is omitted as it always has an offset of 0.  Chunk table
60  *   entries are 32-bit for resources <= 4 GiB uncompressed and 64-bit for
61  *   resources > 4 GiB uncompressed.
62  *
63  * - Solid resource format (distinguished by the use of WIM_RESHDR_FLAG_SOLID
64  *   instead of WIM_RESHDR_FLAG_COMPRESSED): similar to the original format, but
65  *   the resource begins with a 16-byte header which specifies the uncompressed
66  *   size of the resource, the compression type, and the chunk size.  (In the
67  *   original format, these values were instead determined from outside the
68  *   resource itself, from the blob table and the WIM file header.) In addition,
69  *   in this format the entries in the chunk table contain compressed chunk
70  *   sizes rather than offsets.  As a consequence of this, the chunk table
71  *   entries are always 32-bit and there is an entry for chunk 0.
72  *
73  * - Pipable resource format (wimlib extension; all resources in a pipable WIM
74  *   have this format): similar to the original format, but the chunk table is
75  *   at the end of the resource rather than the beginning, and each compressed
76  *   chunk is prefixed with its compressed size as a 32-bit integer.  This
77  *   format allows a resource to be written without rewinding.
78  */
79
80
81 struct data_range {
82         u64 offset;
83         u64 size;
84 };
85
86 /*
87  * Read data from a compressed WIM resource.
88  *
89  * @rdesc
90  *      Description of the compressed WIM resource to read from.
91  * @ranges
92  *      Nonoverlapping, nonempty ranges of the uncompressed resource data to
93  *      read, sorted by increasing offset.
94  * @num_ranges
95  *      Number of ranges in @ranges; must be at least 1.
96  * @cbs
97  *      Structure which provides the consume_chunk() callback to feed the data
98  *      being read.  Each call provides the next chunk of the requested data,
99  *      uncompressed.  Each chunk will be nonempty and will not cross range
100  *      boundaries but otherwise will be of unspecified size.
101  *
102  * Possible return values:
103  *
104  *      WIMLIB_ERR_SUCCESS (0)
105  *      WIMLIB_ERR_READ                   (errno set)
106  *      WIMLIB_ERR_UNEXPECTED_END_OF_FILE (errno set to 0)
107  *      WIMLIB_ERR_NOMEM                  (errno set to ENOMEM)
108  *      WIMLIB_ERR_DECOMPRESSION          (errno set to EINVAL)
109  *      WIMLIB_ERR_INVALID_CHUNK_SIZE     (errno set to EINVAL)
110  *
111  *      or other error code returned by the cbs->consume_chunk() function.
112  */
113 static int
114 read_compressed_wim_resource(const struct wim_resource_descriptor * const rdesc,
115                              const struct data_range * const ranges,
116                              const size_t num_ranges,
117                              const struct read_blob_callbacks *cbs)
118 {
119         int ret;
120         u64 *chunk_offsets = NULL;
121         u8 *ubuf = NULL;
122         void *cbuf = NULL;
123         bool chunk_offsets_malloced = false;
124         bool ubuf_malloced = false;
125         bool cbuf_malloced = false;
126         struct wimlib_decompressor *decompressor = NULL;
127
128         /* Sanity checks  */
129         wimlib_assert(num_ranges != 0);
130         for (size_t i = 0; i < num_ranges; i++) {
131                 wimlib_assert(ranges[i].offset + ranges[i].size > ranges[i].offset &&
132                               ranges[i].offset + ranges[i].size <= rdesc->uncompressed_size);
133         }
134         for (size_t i = 0; i < num_ranges - 1; i++)
135                 wimlib_assert(ranges[i].offset + ranges[i].size <= ranges[i + 1].offset);
136
137         /* Get the offsets of the first and last bytes of the read.  */
138         const u64 first_offset = ranges[0].offset;
139         const u64 last_offset = ranges[num_ranges - 1].offset + ranges[num_ranges - 1].size - 1;
140
141         /* Get the file descriptor for the WIM.  */
142         struct filedes * const in_fd = &rdesc->wim->in_fd;
143
144         /* Determine if we're reading a pipable resource from a pipe or not.  */
145         const bool is_pipe_read = (rdesc->is_pipable && !filedes_is_seekable(in_fd));
146
147         /* Determine if the chunk table is in an alternate format.  */
148         const bool alt_chunk_table = (rdesc->flags & WIM_RESHDR_FLAG_SOLID)
149                                         && !is_pipe_read;
150
151         /* Get the maximum size of uncompressed chunks in this resource, which
152          * we require be a power of 2.  */
153         u64 cur_read_offset = rdesc->offset_in_wim;
154         int ctype = rdesc->compression_type;
155         u32 chunk_size = rdesc->chunk_size;
156         if (alt_chunk_table) {
157                 /* Alternate chunk table format.  Its header specifies the chunk
158                  * size and compression format.  Note: it could be read here;
159                  * however, the relevant data was already loaded into @rdesc by
160                  * read_blob_table().  */
161                 cur_read_offset += sizeof(struct alt_chunk_table_header_disk);
162         }
163
164         if (unlikely(!is_power_of_2(chunk_size))) {
165                 ERROR("Invalid compressed resource: "
166                       "expected power-of-2 chunk size (got %"PRIu32")",
167                       chunk_size);
168                 ret = WIMLIB_ERR_INVALID_CHUNK_SIZE;
169                 errno = EINVAL;
170                 goto out_cleanup;
171         }
172
173         /* Get valid decompressor.  */
174         if (likely(ctype == rdesc->wim->decompressor_ctype &&
175                    chunk_size == rdesc->wim->decompressor_max_block_size))
176         {
177                 /* Cached decompressor.  */
178                 decompressor = rdesc->wim->decompressor;
179                 rdesc->wim->decompressor_ctype = WIMLIB_COMPRESSION_TYPE_NONE;
180                 rdesc->wim->decompressor = NULL;
181         } else {
182                 ret = wimlib_create_decompressor(ctype, chunk_size,
183                                                  &decompressor);
184                 if (unlikely(ret)) {
185                         if (ret != WIMLIB_ERR_NOMEM)
186                                 errno = EINVAL;
187                         goto out_cleanup;
188                 }
189         }
190
191         const u32 chunk_order = fls32(chunk_size);
192
193         /* Calculate the total number of chunks the resource is divided into.  */
194         const u64 num_chunks = (rdesc->uncompressed_size + chunk_size - 1) >> chunk_order;
195
196         /* Calculate the 0-based indices of the first and last chunks containing
197          * data that needs to be passed to the callback.  */
198         const u64 first_needed_chunk = first_offset >> chunk_order;
199         const u64 last_needed_chunk = last_offset >> chunk_order;
200
201         /* Calculate the 0-based index of the first chunk that actually needs to
202          * be read.  This is normally first_needed_chunk, but for pipe reads we
203          * must always start from the 0th chunk.  */
204         const u64 read_start_chunk = (is_pipe_read ? 0 : first_needed_chunk);
205
206         /* Calculate the number of chunk offsets that are needed for the chunks
207          * being read.  */
208         const u64 num_needed_chunk_offsets =
209                 last_needed_chunk - read_start_chunk + 1 +
210                 (last_needed_chunk < num_chunks - 1);
211
212         /* Calculate the number of entries in the chunk table.  Normally, it's
213          * one less than the number of chunks, since the first chunk has no
214          * entry.  But in the alternate chunk table format, the chunk entries
215          * contain chunk sizes, not offsets, and there is one per chunk.  */
216         const u64 num_chunk_entries = (alt_chunk_table ? num_chunks : num_chunks - 1);
217
218         /* Set the size of each chunk table entry based on the resource's
219          * uncompressed size.  */
220         const u64 chunk_entry_size = get_chunk_entry_size(rdesc->uncompressed_size,
221                                                           alt_chunk_table);
222
223         /* Calculate the size of the chunk table in bytes.  */
224         const u64 chunk_table_size = num_chunk_entries * chunk_entry_size;
225
226         /* Calculate the size of the chunk table in bytes, including the header
227          * in the case of the alternate chunk table format.  */
228         const u64 chunk_table_full_size =
229                 (alt_chunk_table) ? chunk_table_size + sizeof(struct alt_chunk_table_header_disk)
230                                   : chunk_table_size;
231
232         if (!is_pipe_read) {
233                 /* Read the needed chunk table entries into memory and use them
234                  * to initialize the chunk_offsets array.  */
235
236                 u64 first_chunk_entry_to_read;
237                 u64 last_chunk_entry_to_read;
238
239                 if (alt_chunk_table) {
240                         /* The alternate chunk table contains chunk sizes, not
241                          * offsets, so we always must read all preceding entries
242                          * in order to determine offsets.  */
243                         first_chunk_entry_to_read = 0;
244                         last_chunk_entry_to_read = last_needed_chunk;
245                 } else {
246                         /* Here we must account for the fact that the first
247                          * chunk has no explicit chunk table entry.  */
248
249                         if (read_start_chunk == 0)
250                                 first_chunk_entry_to_read = 0;
251                         else
252                                 first_chunk_entry_to_read = read_start_chunk - 1;
253
254                         if (last_needed_chunk == 0)
255                                 last_chunk_entry_to_read = 0;
256                         else
257                                 last_chunk_entry_to_read = last_needed_chunk - 1;
258
259                         if (last_needed_chunk < num_chunks - 1)
260                                 last_chunk_entry_to_read++;
261                 }
262
263                 const u64 num_chunk_entries_to_read =
264                         last_chunk_entry_to_read - first_chunk_entry_to_read + 1;
265
266                 const u64 chunk_offsets_alloc_size =
267                         max(num_chunk_entries_to_read,
268                             num_needed_chunk_offsets) * sizeof(chunk_offsets[0]);
269
270                 if (unlikely((size_t)chunk_offsets_alloc_size != chunk_offsets_alloc_size)) {
271                         errno = ENOMEM;
272                         goto oom;
273                 }
274
275                 if (likely(chunk_offsets_alloc_size <= STACK_MAX)) {
276                         chunk_offsets = alloca(chunk_offsets_alloc_size);
277                 } else {
278                         chunk_offsets = MALLOC(chunk_offsets_alloc_size);
279                         if (unlikely(!chunk_offsets))
280                                 goto oom;
281                         chunk_offsets_malloced = true;
282                 }
283
284                 const size_t chunk_table_size_to_read =
285                         num_chunk_entries_to_read * chunk_entry_size;
286
287                 const u64 file_offset_of_needed_chunk_entries =
288                         cur_read_offset
289                         + (first_chunk_entry_to_read * chunk_entry_size)
290                         + (rdesc->is_pipable ? (rdesc->size_in_wim - chunk_table_size) : 0);
291
292                 void * const chunk_table_data =
293                         (u8*)chunk_offsets +
294                         chunk_offsets_alloc_size -
295                         chunk_table_size_to_read;
296
297                 ret = full_pread(in_fd, chunk_table_data, chunk_table_size_to_read,
298                                  file_offset_of_needed_chunk_entries);
299                 if (unlikely(ret))
300                         goto read_error;
301
302                 /* Now fill in chunk_offsets from the entries we have read in
303                  * chunk_tab_data.  We break aliasing rules here to avoid having
304                  * to allocate yet another array.  */
305                 typedef le64 _may_alias_attribute aliased_le64_t;
306                 typedef le32 _may_alias_attribute aliased_le32_t;
307                 u64 * chunk_offsets_p = chunk_offsets;
308
309                 if (alt_chunk_table) {
310                         u64 cur_offset = 0;
311                         aliased_le32_t *raw_entries = chunk_table_data;
312
313                         for (size_t i = 0; i < num_chunk_entries_to_read; i++) {
314                                 u32 entry = le32_to_cpu(raw_entries[i]);
315                                 if (i >= read_start_chunk)
316                                         *chunk_offsets_p++ = cur_offset;
317                                 cur_offset += entry;
318                         }
319                         if (last_needed_chunk < num_chunks - 1)
320                                 *chunk_offsets_p = cur_offset;
321                 } else {
322                         if (read_start_chunk == 0)
323                                 *chunk_offsets_p++ = 0;
324
325                         if (chunk_entry_size == 4) {
326                                 aliased_le32_t *raw_entries = chunk_table_data;
327                                 for (size_t i = 0; i < num_chunk_entries_to_read; i++)
328                                         *chunk_offsets_p++ = le32_to_cpu(raw_entries[i]);
329                         } else {
330                                 aliased_le64_t *raw_entries = chunk_table_data;
331                                 for (size_t i = 0; i < num_chunk_entries_to_read; i++)
332                                         *chunk_offsets_p++ = le64_to_cpu(raw_entries[i]);
333                         }
334                 }
335
336                 /* Set offset to beginning of first chunk to read.  */
337                 cur_read_offset += chunk_offsets[0];
338                 if (rdesc->is_pipable)
339                         cur_read_offset += read_start_chunk * sizeof(struct pwm_chunk_hdr);
340                 else
341                         cur_read_offset += chunk_table_size;
342         }
343
344         /* Allocate buffer for holding the uncompressed data of each chunk.  */
345         if (chunk_size <= STACK_MAX) {
346                 ubuf = alloca(chunk_size);
347         } else {
348                 ubuf = MALLOC(chunk_size);
349                 if (unlikely(!ubuf))
350                         goto oom;
351                 ubuf_malloced = true;
352         }
353
354         /* Allocate a temporary buffer for reading compressed chunks, each of
355          * which can be at most @chunk_size - 1 bytes.  This excludes compressed
356          * chunks that are a full @chunk_size bytes, which are actually stored
357          * uncompressed.  */
358         if (chunk_size - 1 <= STACK_MAX) {
359                 cbuf = alloca(chunk_size - 1);
360         } else {
361                 cbuf = MALLOC(chunk_size - 1);
362                 if (unlikely(!cbuf))
363                         goto oom;
364                 cbuf_malloced = true;
365         }
366
367         /* Set current data range.  */
368         const struct data_range *cur_range = ranges;
369         const struct data_range * const end_range = &ranges[num_ranges];
370         u64 cur_range_pos = cur_range->offset;
371         u64 cur_range_end = cur_range->offset + cur_range->size;
372
373         /* Read and process each needed chunk.  */
374         for (u64 i = read_start_chunk; i <= last_needed_chunk; i++) {
375
376                 /* Calculate uncompressed size of next chunk.  */
377                 u32 chunk_usize;
378                 if ((i == num_chunks - 1) && (rdesc->uncompressed_size & (chunk_size - 1)))
379                         chunk_usize = (rdesc->uncompressed_size & (chunk_size - 1));
380                 else
381                         chunk_usize = chunk_size;
382
383                 /* Calculate compressed size of next chunk.  */
384                 u32 chunk_csize;
385                 if (is_pipe_read) {
386                         struct pwm_chunk_hdr chunk_hdr;
387
388                         ret = full_pread(in_fd, &chunk_hdr,
389                                          sizeof(chunk_hdr), cur_read_offset);
390                         if (unlikely(ret))
391                                 goto read_error;
392                         chunk_csize = le32_to_cpu(chunk_hdr.compressed_size);
393                 } else {
394                         if (i == num_chunks - 1) {
395                                 chunk_csize = rdesc->size_in_wim -
396                                               chunk_table_full_size -
397                                               chunk_offsets[i - read_start_chunk];
398                                 if (rdesc->is_pipable)
399                                         chunk_csize -= num_chunks * sizeof(struct pwm_chunk_hdr);
400                         } else {
401                                 chunk_csize = chunk_offsets[i + 1 - read_start_chunk] -
402                                               chunk_offsets[i - read_start_chunk];
403                         }
404                 }
405                 if (unlikely(chunk_csize == 0 || chunk_csize > chunk_usize)) {
406                         ERROR("Invalid chunk size in compressed resource!");
407                         errno = EINVAL;
408                         ret = WIMLIB_ERR_DECOMPRESSION;
409                         goto out_cleanup;
410                 }
411                 if (rdesc->is_pipable)
412                         cur_read_offset += sizeof(struct pwm_chunk_hdr);
413
414                 /* Offsets in the uncompressed resource at which this chunk
415                  * starts and ends.  */
416                 const u64 chunk_start_offset = i << chunk_order;
417                 const u64 chunk_end_offset = chunk_start_offset + chunk_usize;
418
419                 if (chunk_end_offset <= cur_range_pos) {
420
421                         /* The next range does not require data in this chunk,
422                          * so skip it.  */
423                         cur_read_offset += chunk_csize;
424                         if (is_pipe_read) {
425                                 u8 dummy;
426
427                                 ret = full_pread(in_fd, &dummy, 1, cur_read_offset - 1);
428                                 if (unlikely(ret))
429                                         goto read_error;
430                         }
431                 } else {
432
433                         /* Read the chunk and feed data to the callback
434                          * function.  */
435                         u8 *read_buf;
436
437                         if (chunk_csize == chunk_usize)
438                                 read_buf = ubuf;
439                         else
440                                 read_buf = cbuf;
441
442                         ret = full_pread(in_fd,
443                                          read_buf,
444                                          chunk_csize,
445                                          cur_read_offset);
446                         if (unlikely(ret))
447                                 goto read_error;
448
449                         if (read_buf == cbuf) {
450                                 ret = wimlib_decompress(cbuf,
451                                                         chunk_csize,
452                                                         ubuf,
453                                                         chunk_usize,
454                                                         decompressor);
455                                 if (unlikely(ret)) {
456                                         ERROR("Failed to decompress data!");
457                                         ret = WIMLIB_ERR_DECOMPRESSION;
458                                         errno = EINVAL;
459                                         goto out_cleanup;
460                                 }
461                         }
462                         cur_read_offset += chunk_csize;
463
464                         /* At least one range requires data in this chunk.  */
465                         do {
466                                 size_t start, end, size;
467
468                                 /* Calculate how many bytes of data should be
469                                  * sent to the callback function, taking into
470                                  * account that data sent to the callback
471                                  * function must not overlap range boundaries.
472                                  */
473                                 start = cur_range_pos - chunk_start_offset;
474                                 end = min(cur_range_end, chunk_end_offset) - chunk_start_offset;
475                                 size = end - start;
476
477                                 ret = call_consume_chunk(&ubuf[start], size, cbs);
478                                 if (unlikely(ret))
479                                         goto out_cleanup;
480
481                                 cur_range_pos += size;
482                                 if (cur_range_pos == cur_range_end) {
483                                         /* Advance to next range.  */
484                                         if (++cur_range == end_range) {
485                                                 cur_range_pos = ~0ULL;
486                                         } else {
487                                                 cur_range_pos = cur_range->offset;
488                                                 cur_range_end = cur_range->offset + cur_range->size;
489                                         }
490                                 }
491                         } while (cur_range_pos < chunk_end_offset);
492                 }
493         }
494
495         if (is_pipe_read &&
496             last_offset == rdesc->uncompressed_size - 1 &&
497             chunk_table_size)
498         {
499                 u8 dummy;
500                 /* If reading a pipable resource from a pipe and the full data
501                  * was requested, skip the chunk table at the end so that the
502                  * file descriptor is fully clear of the resource after this
503                  * returns.  */
504                 cur_read_offset += chunk_table_size;
505                 ret = full_pread(in_fd, &dummy, 1, cur_read_offset - 1);
506                 if (unlikely(ret))
507                         goto read_error;
508         }
509         ret = 0;
510
511 out_cleanup:
512         if (decompressor) {
513                 wimlib_free_decompressor(rdesc->wim->decompressor);
514                 rdesc->wim->decompressor = decompressor;
515                 rdesc->wim->decompressor_ctype = ctype;
516                 rdesc->wim->decompressor_max_block_size = chunk_size;
517         }
518         if (chunk_offsets_malloced)
519                 FREE(chunk_offsets);
520         if (ubuf_malloced)
521                 FREE(ubuf);
522         if (cbuf_malloced)
523                 FREE(cbuf);
524         return ret;
525
526 oom:
527         ERROR("Out of memory while reading compressed WIM resource");
528         ret = WIMLIB_ERR_NOMEM;
529         goto out_cleanup;
530
531 read_error:
532         ERROR_WITH_ERRNO("Error reading data from WIM file");
533         goto out_cleanup;
534 }
535
536 /* Read raw data from a file descriptor at the specified offset, feeding the
537  * data in nonempty chunks into the cbs->consume_chunk() function.  */
538 static int
539 read_raw_file_data(struct filedes *in_fd, u64 offset, u64 size,
540                    const struct read_blob_callbacks *cbs)
541 {
542         u8 buf[BUFFER_SIZE];
543         size_t bytes_to_read;
544         int ret;
545
546         while (size) {
547                 bytes_to_read = min(sizeof(buf), size);
548                 ret = full_pread(in_fd, buf, bytes_to_read, offset);
549                 if (unlikely(ret)) {
550                         ERROR_WITH_ERRNO("Read error");
551                         return ret;
552                 }
553                 ret = call_consume_chunk(buf, bytes_to_read, cbs);
554                 if (unlikely(ret))
555                         return ret;
556                 size -= bytes_to_read;
557                 offset += bytes_to_read;
558         }
559         return 0;
560 }
561
562 /* A consume_chunk() implementation that simply concatenates all chunks into an
563  * in-memory buffer.  */
564 static int
565 bufferer_cb(const void *chunk, size_t size, void *_ctx)
566 {
567         void **buf_p = _ctx;
568
569         *buf_p = mempcpy(*buf_p, chunk, size);
570         return 0;
571 }
572
573 /*
574  * Read @size bytes at @offset in the WIM resource described by @rdesc and feed
575  * the data into the @cbs->consume_chunk callback function.
576  *
577  * @offset and @size are assumed to have already been validated against the
578  * resource's uncompressed size.
579  *
580  * Returns 0 on success; or the first nonzero value returned by the callback
581  * function; or a nonzero wimlib error code with errno set as well.
582  */
583 static int
584 read_partial_wim_resource(const struct wim_resource_descriptor *rdesc,
585                           const u64 offset, const u64 size,
586                           const struct read_blob_callbacks *cbs)
587 {
588         if (rdesc->flags & (WIM_RESHDR_FLAG_COMPRESSED |
589                             WIM_RESHDR_FLAG_SOLID))
590         {
591                 /* Compressed resource  */
592                 if (unlikely(!size))
593                         return 0;
594                 struct data_range range = {
595                         .offset = offset,
596                         .size = size,
597                 };
598                 return read_compressed_wim_resource(rdesc, &range, 1, cbs);
599         }
600
601         /* Uncompressed resource  */
602         return read_raw_file_data(&rdesc->wim->in_fd,
603                                   rdesc->offset_in_wim + offset,
604                                   size, cbs);
605 }
606
607 /* Read the specified range of uncompressed data from the specified blob, which
608  * must be located in a WIM file, into the specified buffer.  */
609 int
610 read_partial_wim_blob_into_buf(const struct blob_descriptor *blob,
611                                u64 offset, size_t size, void *buf)
612 {
613         struct read_blob_callbacks cbs = {
614                 .consume_chunk  = bufferer_cb,
615                 .ctx            = &buf,
616         };
617         return read_partial_wim_resource(blob->rdesc,
618                                          blob->offset_in_res + offset,
619                                          size,
620                                          &cbs);
621 }
622
623 /* Skip over the data of the specified WIM resource.  */
624 int
625 skip_wim_resource(const struct wim_resource_descriptor *rdesc)
626 {
627         struct read_blob_callbacks cbs = {
628         };
629         return read_partial_wim_resource(rdesc, 0,
630                                          rdesc->uncompressed_size, &cbs);
631 }
632
633 static int
634 read_wim_blob_prefix(const struct blob_descriptor *blob, u64 size,
635                      const struct read_blob_callbacks *cbs)
636 {
637         return read_partial_wim_resource(blob->rdesc, blob->offset_in_res,
638                                          size, cbs);
639 }
640
641 /* This function handles reading blob data that is located in an external file,
642  * such as a file that has been added to the WIM image through execution of a
643  * wimlib_add_command.
644  *
645  * This assumes the file can be accessed using the standard POSIX open(),
646  * read(), and close().  On Windows this will not necessarily be the case (since
647  * the file may need FILE_FLAG_BACKUP_SEMANTICS to be opened, or the file may be
648  * encrypted), so Windows uses its own code for its equivalent case.  */
649 static int
650 read_file_on_disk_prefix(const struct blob_descriptor *blob, u64 size,
651                          const struct read_blob_callbacks *cbs)
652 {
653         int ret;
654         int raw_fd;
655         struct filedes fd;
656
657         raw_fd = topen(blob->file_on_disk, O_BINARY | O_RDONLY);
658         if (unlikely(raw_fd < 0)) {
659                 ERROR_WITH_ERRNO("Can't open \"%"TS"\"", blob->file_on_disk);
660                 return WIMLIB_ERR_OPEN;
661         }
662         filedes_init(&fd, raw_fd);
663         ret = read_raw_file_data(&fd, 0, size, cbs);
664         filedes_close(&fd);
665         return ret;
666 }
667
668 #ifdef WITH_FUSE
669 static int
670 read_staging_file_prefix(const struct blob_descriptor *blob, u64 size,
671                          const struct read_blob_callbacks *cbs)
672 {
673         int raw_fd;
674         struct filedes fd;
675         int ret;
676
677         raw_fd = openat(blob->staging_dir_fd, blob->staging_file_name,
678                         O_RDONLY | O_NOFOLLOW);
679         if (unlikely(raw_fd < 0)) {
680                 ERROR_WITH_ERRNO("Can't open staging file \"%s\"",
681                                  blob->staging_file_name);
682                 return WIMLIB_ERR_OPEN;
683         }
684         filedes_init(&fd, raw_fd);
685         ret = read_raw_file_data(&fd, 0, size, cbs);
686         filedes_close(&fd);
687         return ret;
688 }
689 #endif
690
691 /* This function handles the trivial case of reading blob data that is, in fact,
692  * already located in an in-memory buffer.  */
693 static int
694 read_buffer_prefix(const struct blob_descriptor *blob,
695                    u64 size, const struct read_blob_callbacks *cbs)
696 {
697         if (unlikely(!size))
698                 return 0;
699         return call_consume_chunk(blob->attached_buffer, size, cbs);
700 }
701
702 typedef int (*read_blob_prefix_handler_t)(const struct blob_descriptor *blob,
703                                           u64 size,
704                                           const struct read_blob_callbacks *cbs);
705
706 /*
707  * Read the first @size bytes from a generic "blob", which may be located in any
708  * one of several locations, such as in a WIM resource (possibly compressed), in
709  * an external file, or directly in an in-memory buffer.  The blob data will be
710  * fed to the cbs->consume_chunk() callback function in chunks that are nonempty
711  * but otherwise are of unspecified size.
712  *
713  * Returns 0 on success; nonzero on error.  A nonzero value will be returned if
714  * the blob data cannot be successfully read (for a number of different reasons,
715  * depending on the blob location), or if cbs->consume_chunk() returned nonzero
716  * in which case that error code will be returned.
717  */
718 static int
719 read_blob_prefix(const struct blob_descriptor *blob, u64 size,
720                  const struct read_blob_callbacks *cbs)
721 {
722         static const read_blob_prefix_handler_t handlers[] = {
723                 [BLOB_IN_WIM] = read_wim_blob_prefix,
724                 [BLOB_IN_FILE_ON_DISK] = read_file_on_disk_prefix,
725                 [BLOB_IN_ATTACHED_BUFFER] = read_buffer_prefix,
726         #ifdef WITH_FUSE
727                 [BLOB_IN_STAGING_FILE] = read_staging_file_prefix,
728         #endif
729         #ifdef WITH_NTFS_3G
730                 [BLOB_IN_NTFS_VOLUME] = read_ntfs_attribute_prefix,
731         #endif
732         #ifdef __WIN32__
733                 [BLOB_IN_WINNT_FILE_ON_DISK] = read_winnt_stream_prefix,
734                 [BLOB_WIN32_ENCRYPTED] = read_win32_encrypted_file_prefix,
735         #endif
736         };
737         wimlib_assert(blob->blob_location < ARRAY_LEN(handlers)
738                       && handlers[blob->blob_location] != NULL);
739         wimlib_assert(size <= blob->size);
740         return handlers[blob->blob_location](blob, size, cbs);
741 }
742
743 /* Read the full data of the specified blob, passing the data into the specified
744  * callbacks (all of which are optional).  */
745 int
746 read_blob_with_cbs(struct blob_descriptor *blob,
747                    const struct read_blob_callbacks *cbs)
748 {
749         int ret;
750
751         ret = call_begin_blob(blob, cbs);
752         if (unlikely(ret))
753                 return ret;
754
755         ret = read_blob_prefix(blob, blob->size, cbs);
756
757         return call_end_blob(blob, ret, cbs);
758 }
759
760 /* Read the full uncompressed data of the specified blob into the specified
761  * buffer, which must have space for at least blob->size bytes.  The SHA-1
762  * message digest is *not* checked.  */
763 int
764 read_blob_into_buf(const struct blob_descriptor *blob, void *buf)
765 {
766         struct read_blob_callbacks cbs = {
767                 .consume_chunk  = bufferer_cb,
768                 .ctx            = &buf,
769         };
770         return read_blob_prefix(blob, blob->size, &cbs);
771 }
772
773 /* Retrieve the full uncompressed data of the specified blob.  A buffer large
774  * enough hold the data is allocated and returned in @buf_ret.  The SHA-1
775  * message digest is *not* checked.  */
776 int
777 read_blob_into_alloc_buf(const struct blob_descriptor *blob, void **buf_ret)
778 {
779         int ret;
780         void *buf;
781
782         if (unlikely((size_t)blob->size != blob->size)) {
783                 ERROR("Can't read %"PRIu64" byte blob into memory", blob->size);
784                 return WIMLIB_ERR_NOMEM;
785         }
786
787         buf = MALLOC(blob->size);
788         if (unlikely(!buf))
789                 return WIMLIB_ERR_NOMEM;
790
791         ret = read_blob_into_buf(blob, buf);
792         if (unlikely(ret)) {
793                 FREE(buf);
794                 return ret;
795         }
796
797         *buf_ret = buf;
798         return 0;
799 }
800
801 /* Retrieve the full uncompressed data of a WIM resource specified as a raw
802  * `wim_reshdr' and the corresponding WIM file.  A buffer large enough hold the
803  * data is allocated and returned in @buf_ret.  */
804 int
805 wim_reshdr_to_data(const struct wim_reshdr *reshdr, WIMStruct *wim,
806                    void **buf_ret)
807 {
808         struct wim_resource_descriptor rdesc;
809         struct blob_descriptor blob;
810
811         wim_reshdr_to_desc(reshdr, wim, &rdesc);
812         blob_set_is_located_in_nonsolid_wim_resource(&blob, &rdesc);
813
814         return read_blob_into_alloc_buf(&blob, buf_ret);
815 }
816
817 /* Calculate the SHA-1 message digest of the uncompressed data of the specified
818  * WIM resource.  */
819 int
820 wim_reshdr_to_hash(const struct wim_reshdr *reshdr, WIMStruct *wim,
821                    u8 hash[SHA1_HASH_SIZE])
822 {
823         struct wim_resource_descriptor rdesc;
824         struct blob_descriptor blob;
825         int ret;
826
827         wim_reshdr_to_desc(reshdr, wim, &rdesc);
828         blob_set_is_located_in_nonsolid_wim_resource(&blob, &rdesc);
829         blob.unhashed = 1;
830
831         ret = sha1_blob(&blob);
832         if (unlikely(ret))
833                 return ret;
834
835         copy_hash(hash, blob.hash);
836         return 0;
837 }
838
839 struct blobifier_context {
840         struct read_blob_callbacks cbs;
841         struct blob_descriptor *cur_blob;
842         struct blob_descriptor *next_blob;
843         u64 cur_blob_offset;
844         struct blob_descriptor *final_blob;
845         size_t list_head_offset;
846 };
847
848 static struct blob_descriptor *
849 next_blob(struct blob_descriptor *blob, size_t list_head_offset)
850 {
851         struct list_head *cur;
852
853         cur = (struct list_head*)((u8*)blob + list_head_offset);
854
855         return (struct blob_descriptor*)((u8*)cur->next - list_head_offset);
856 }
857
858 /* A consume_chunk() implementation that translates raw resource data into
859  * blobs, calling the begin_blob, consume_chunk, and end_blob callbacks as
860  * appropriate.  */
861 static int
862 blobifier_cb(const void *chunk, size_t size, void *_ctx)
863 {
864         struct blobifier_context *ctx = _ctx;
865         int ret;
866
867         wimlib_assert(ctx->cur_blob != NULL);
868         wimlib_assert(size <= ctx->cur_blob->size - ctx->cur_blob_offset);
869
870         if (ctx->cur_blob_offset == 0) {
871                 /* Starting a new blob.  */
872                 ret = call_begin_blob(ctx->cur_blob, &ctx->cbs);
873                 if (ret)
874                         return ret;
875         }
876
877         ctx->cur_blob_offset += size;
878
879         ret = call_consume_chunk(chunk, size, &ctx->cbs);
880         if (ret)
881                 return ret;
882
883         if (ctx->cur_blob_offset == ctx->cur_blob->size) {
884                 /* Finished reading all the data for a blob.  */
885
886                 ctx->cur_blob_offset = 0;
887
888                 ret = call_end_blob(ctx->cur_blob, 0, &ctx->cbs);
889                 if (ret)
890                         return ret;
891
892                 /* Advance to next blob.  */
893                 ctx->cur_blob = ctx->next_blob;
894                 if (ctx->cur_blob != NULL) {
895                         if (ctx->cur_blob != ctx->final_blob)
896                                 ctx->next_blob = next_blob(ctx->cur_blob,
897                                                            ctx->list_head_offset);
898                         else
899                                 ctx->next_blob = NULL;
900                 }
901         }
902         return 0;
903 }
904
905 struct hasher_context {
906         SHA_CTX sha_ctx;
907         int flags;
908         struct read_blob_callbacks cbs;
909 };
910
911 /* Callback for starting to read a blob while calculating its SHA-1 message
912  * digest.  */
913 static int
914 hasher_begin_blob(struct blob_descriptor *blob, void *_ctx)
915 {
916         struct hasher_context *ctx = _ctx;
917
918         sha1_init(&ctx->sha_ctx);
919
920         return call_begin_blob(blob, &ctx->cbs);
921 }
922
923 /* A consume_chunk() implementation that continues calculating the SHA-1 message
924  * digest of the blob being read, then optionally passes the data on to another
925  * consume_chunk() implementation.  This allows checking the SHA-1 message
926  * digest of a blob being extracted, for example.  */
927 static int
928 hasher_consume_chunk(const void *chunk, size_t size, void *_ctx)
929 {
930         struct hasher_context *ctx = _ctx;
931
932         sha1_update(&ctx->sha_ctx, chunk, size);
933
934         return call_consume_chunk(chunk, size, &ctx->cbs);
935 }
936
937 /* Callback for finishing reading a blob while calculating its SHA-1 message
938  * digest.  */
939 static int
940 hasher_end_blob(struct blob_descriptor *blob, int status, void *_ctx)
941 {
942         struct hasher_context *ctx = _ctx;
943         u8 hash[SHA1_HASH_SIZE];
944         int ret;
945
946         if (unlikely(status)) {
947                 /* Error occurred; the full blob may not have been read.  */
948                 ret = status;
949                 goto out_next_cb;
950         }
951
952         /* Retrieve the final SHA-1 message digest.  */
953         sha1_final(hash, &ctx->sha_ctx);
954
955         /* Set the SHA-1 message digest of the blob, or compare the calculated
956          * value with stored value.  */
957         if (blob->unhashed) {
958                 if (ctx->flags & COMPUTE_MISSING_BLOB_HASHES)
959                         copy_hash(blob->hash, hash);
960         } else if ((ctx->flags & VERIFY_BLOB_HASHES) &&
961                    unlikely(!hashes_equal(hash, blob->hash)))
962         {
963                 if (wimlib_print_errors) {
964                         tchar expected_hashstr[SHA1_HASH_SIZE * 2 + 1];
965                         tchar actual_hashstr[SHA1_HASH_SIZE * 2 + 1];
966                         sprint_hash(blob->hash, expected_hashstr);
967                         sprint_hash(hash, actual_hashstr);
968                         ERROR("The data is corrupted!\n"
969                               "        (Expected SHA-1=%"TS", got SHA-1=%"TS")",
970                               expected_hashstr, actual_hashstr);
971                 }
972                 ret = WIMLIB_ERR_INVALID_RESOURCE_HASH;
973                 goto out_next_cb;
974         }
975         ret = 0;
976 out_next_cb:
977         return call_end_blob(blob, ret, &ctx->cbs);
978 }
979
980 /* Read the full data of the specified blob, passing the data into the specified
981  * callbacks (all of which are optional) and either checking or computing the
982  * SHA-1 message digest of the blob.  */
983 int
984 read_blob_with_sha1(struct blob_descriptor *blob,
985                     const struct read_blob_callbacks *cbs)
986 {
987         struct hasher_context hasher_ctx = {
988                 .flags = VERIFY_BLOB_HASHES | COMPUTE_MISSING_BLOB_HASHES,
989                 .cbs = *cbs,
990         };
991         struct read_blob_callbacks hasher_cbs = {
992                 .begin_blob     = hasher_begin_blob,
993                 .consume_chunk  = hasher_consume_chunk,
994                 .end_blob       = hasher_end_blob,
995                 .ctx            = &hasher_ctx,
996         };
997         return read_blob_with_cbs(blob, &hasher_cbs);
998 }
999
1000 static int
1001 read_blobs_in_solid_resource(struct blob_descriptor *first_blob,
1002                              struct blob_descriptor *last_blob,
1003                              size_t blob_count,
1004                              size_t list_head_offset,
1005                              const struct read_blob_callbacks *sink_cbs)
1006 {
1007         struct data_range *ranges;
1008         bool ranges_malloced;
1009         struct blob_descriptor *cur_blob;
1010         size_t i;
1011         int ret;
1012         u64 ranges_alloc_size;
1013
1014         /* Setup data ranges array (one range per blob to read); this way
1015          * read_compressed_wim_resource() does not need to be aware of blobs.
1016          */
1017
1018         ranges_alloc_size = (u64)blob_count * sizeof(ranges[0]);
1019
1020         if (unlikely((size_t)ranges_alloc_size != ranges_alloc_size))
1021                 goto oom;
1022
1023         if (ranges_alloc_size <= STACK_MAX) {
1024                 ranges = alloca(ranges_alloc_size);
1025                 ranges_malloced = false;
1026         } else {
1027                 ranges = MALLOC(ranges_alloc_size);
1028                 if (unlikely(!ranges))
1029                         goto oom;
1030                 ranges_malloced = true;
1031         }
1032
1033         for (i = 0, cur_blob = first_blob;
1034              i < blob_count;
1035              i++, cur_blob = next_blob(cur_blob, list_head_offset))
1036         {
1037                 ranges[i].offset = cur_blob->offset_in_res;
1038                 ranges[i].size = cur_blob->size;
1039         }
1040
1041         struct blobifier_context blobifier_ctx = {
1042                 .cbs                    = *sink_cbs,
1043                 .cur_blob               = first_blob,
1044                 .next_blob              = next_blob(first_blob, list_head_offset),
1045                 .cur_blob_offset        = 0,
1046                 .final_blob             = last_blob,
1047                 .list_head_offset       = list_head_offset,
1048         };
1049         struct read_blob_callbacks cbs = {
1050                 .consume_chunk  = blobifier_cb,
1051                 .ctx            = &blobifier_ctx,
1052         };
1053
1054         ret = read_compressed_wim_resource(first_blob->rdesc, ranges,
1055                                            blob_count, &cbs);
1056
1057         if (ranges_malloced)
1058                 FREE(ranges);
1059
1060         if (unlikely(ret && blobifier_ctx.cur_blob_offset != 0)) {
1061                 ret = call_end_blob(blobifier_ctx.cur_blob, ret,
1062                                     &blobifier_ctx.cbs);
1063         }
1064         return ret;
1065
1066 oom:
1067         ERROR("Too many blobs in one resource!");
1068         return WIMLIB_ERR_NOMEM;
1069 }
1070
1071 /*
1072  * Read a list of blobs, each of which may be in any supported location (e.g.
1073  * in a WIM or in an external file).  This function optimizes the case where
1074  * multiple blobs are combined into a single solid compressed WIM resource by
1075  * reading the blobs in sequential order, only decompressing the solid resource
1076  * one time.
1077  *
1078  * @blob_list
1079  *      List of blobs to read.
1080  * @list_head_offset
1081  *      Offset of the `struct list_head' within each `struct blob_descriptor'
1082  *      that makes up the @blob_list.
1083  * @cbs
1084  *      Callback functions to accept the blob data.
1085  * @flags
1086  *      Bitwise OR of zero or more of the following flags:
1087  *
1088  *      VERIFY_BLOB_HASHES:
1089  *              For all blobs being read that have already had SHA-1 message
1090  *              digests computed, calculate the SHA-1 message digest of the read
1091  *              data and compare it with the previously computed value.  If they
1092  *              do not match, return WIMLIB_ERR_INVALID_RESOURCE_HASH.
1093  *
1094  *      COMPUTE_MISSING_BLOB_HASHES
1095  *              For all blobs being read that have not yet had their SHA-1
1096  *              message digests computed, calculate and save their SHA-1 message
1097  *              digests.
1098  *
1099  *      BLOB_LIST_ALREADY_SORTED
1100  *              @blob_list is already sorted in sequential order for reading.
1101  *
1102  * The callback functions are allowed to delete the current blob from the list
1103  * if necessary.
1104  *
1105  * Returns 0 on success; a nonzero error code on failure.  Failure can occur due
1106  * to an error reading the data or due to an error status being returned by any
1107  * of the callback functions.
1108  */
1109 int
1110 read_blob_list(struct list_head *blob_list, size_t list_head_offset,
1111                const struct read_blob_callbacks *cbs, int flags)
1112 {
1113         int ret;
1114         struct list_head *cur, *next;
1115         struct blob_descriptor *blob;
1116         struct hasher_context *hasher_ctx;
1117         struct read_blob_callbacks *sink_cbs;
1118
1119         if (!(flags & BLOB_LIST_ALREADY_SORTED)) {
1120                 ret = sort_blob_list_by_sequential_order(blob_list,
1121                                                          list_head_offset);
1122                 if (ret)
1123                         return ret;
1124         }
1125
1126         if (flags & (VERIFY_BLOB_HASHES | COMPUTE_MISSING_BLOB_HASHES)) {
1127                 hasher_ctx = alloca(sizeof(*hasher_ctx));
1128                 *hasher_ctx = (struct hasher_context) {
1129                         .flags  = flags,
1130                         .cbs    = *cbs,
1131                 };
1132                 sink_cbs = alloca(sizeof(*sink_cbs));
1133                 *sink_cbs = (struct read_blob_callbacks) {
1134                         .begin_blob     = hasher_begin_blob,
1135                         .consume_chunk  = hasher_consume_chunk,
1136                         .end_blob       = hasher_end_blob,
1137                         .ctx            = hasher_ctx,
1138                 };
1139         } else {
1140                 sink_cbs = (struct read_blob_callbacks *)cbs;
1141         }
1142
1143         for (cur = blob_list->next, next = cur->next;
1144              cur != blob_list;
1145              cur = next, next = cur->next)
1146         {
1147                 blob = (struct blob_descriptor*)((u8*)cur - list_head_offset);
1148
1149                 if (blob->blob_location == BLOB_IN_WIM &&
1150                     blob->size != blob->rdesc->uncompressed_size)
1151                 {
1152                         struct blob_descriptor *blob_next, *blob_last;
1153                         struct list_head *next2;
1154                         size_t blob_count;
1155
1156                         /* The next blob is a proper sub-sequence of a WIM
1157                          * resource.  See if there are other blobs in the same
1158                          * resource that need to be read.  Since
1159                          * sort_blob_list_by_sequential_order() sorted the blobs
1160                          * by offset in the WIM, this can be determined by
1161                          * simply scanning forward in the list.  */
1162
1163                         blob_last = blob;
1164                         blob_count = 1;
1165                         for (next2 = next;
1166                              next2 != blob_list
1167                              && (blob_next = (struct blob_descriptor*)
1168                                                 ((u8*)next2 - list_head_offset),
1169                                  blob_next->blob_location == BLOB_IN_WIM
1170                                  && blob_next->rdesc == blob->rdesc);
1171                              next2 = next2->next)
1172                         {
1173                                 blob_last = blob_next;
1174                                 blob_count++;
1175                         }
1176                         if (blob_count > 1) {
1177                                 /* Reading multiple blobs combined into a single
1178                                  * WIM resource.  They are in the blob list,
1179                                  * sorted by offset; @blob specifies the first
1180                                  * blob in the resource that needs to be read
1181                                  * and @blob_last specifies the last blob in the
1182                                  * resource that needs to be read.  */
1183                                 next = next2;
1184                                 ret = read_blobs_in_solid_resource(blob, blob_last,
1185                                                                    blob_count,
1186                                                                    list_head_offset,
1187                                                                    sink_cbs);
1188                                 if (ret)
1189                                         return ret;
1190                                 continue;
1191                         }
1192                 }
1193
1194                 ret = read_blob_with_cbs(blob, sink_cbs);
1195                 if (unlikely(ret && ret != BEGIN_BLOB_STATUS_SKIP_BLOB))
1196                         return ret;
1197         }
1198         return 0;
1199 }
1200
1201 static int
1202 extract_chunk_to_fd(const void *chunk, size_t size, void *_fd)
1203 {
1204         struct filedes *fd = _fd;
1205         int ret = full_write(fd, chunk, size);
1206         if (unlikely(ret))
1207                 ERROR_WITH_ERRNO("Error writing to file descriptor");
1208         return ret;
1209 }
1210
1211 /* Extract the first @size bytes of the specified blob to the specified file
1212  * descriptor.  This does *not* check the SHA-1 message digest.  */
1213 int
1214 extract_blob_prefix_to_fd(struct blob_descriptor *blob, u64 size,
1215                           struct filedes *fd)
1216 {
1217         struct read_blob_callbacks cbs = {
1218                 .consume_chunk  = extract_chunk_to_fd,
1219                 .ctx            = fd,
1220         };
1221         return read_blob_prefix(blob, size, &cbs);
1222 }
1223
1224 /* Extract the full uncompressed contents of the specified blob to the specified
1225  * file descriptor.  This checks the SHA-1 message digest.  */
1226 int
1227 extract_blob_to_fd(struct blob_descriptor *blob, struct filedes *fd)
1228 {
1229         struct read_blob_callbacks cbs = {
1230                 .consume_chunk  = extract_chunk_to_fd,
1231                 .ctx            = fd,
1232         };
1233         return read_blob_with_sha1(blob, &cbs);
1234 }
1235
1236 /* Calculate the SHA-1 message digest of a blob and store it in @blob->hash.  */
1237 int
1238 sha1_blob(struct blob_descriptor *blob)
1239 {
1240         struct read_blob_callbacks cbs = {
1241         };
1242         return read_blob_with_sha1(blob, &cbs);
1243 }
1244
1245 /*
1246  * Convert a short WIM resource header to a stand-alone WIM resource descriptor.
1247  *
1248  * Note: for solid resources some fields still need to be overridden.
1249  */
1250 void
1251 wim_reshdr_to_desc(const struct wim_reshdr *reshdr, WIMStruct *wim,
1252                    struct wim_resource_descriptor *rdesc)
1253 {
1254         rdesc->wim = wim;
1255         rdesc->offset_in_wim = reshdr->offset_in_wim;
1256         rdesc->size_in_wim = reshdr->size_in_wim;
1257         rdesc->uncompressed_size = reshdr->uncompressed_size;
1258         INIT_LIST_HEAD(&rdesc->blob_list);
1259         rdesc->flags = reshdr->flags;
1260         rdesc->is_pipable = wim_is_pipable(wim);
1261         if (rdesc->flags & WIM_RESHDR_FLAG_COMPRESSED) {
1262                 rdesc->compression_type = wim->compression_type;
1263                 rdesc->chunk_size = wim->chunk_size;
1264         } else {
1265                 rdesc->compression_type = WIMLIB_COMPRESSION_TYPE_NONE;
1266                 rdesc->chunk_size = 0;
1267         }
1268 }
1269
1270 /* Import a WIM resource header from the on-disk format.  */
1271 void
1272 get_wim_reshdr(const struct wim_reshdr_disk *disk_reshdr,
1273                struct wim_reshdr *reshdr)
1274 {
1275         reshdr->offset_in_wim = le64_to_cpu(disk_reshdr->offset_in_wim);
1276         reshdr->size_in_wim = (((u64)disk_reshdr->size_in_wim[0] <<  0) |
1277                                ((u64)disk_reshdr->size_in_wim[1] <<  8) |
1278                                ((u64)disk_reshdr->size_in_wim[2] << 16) |
1279                                ((u64)disk_reshdr->size_in_wim[3] << 24) |
1280                                ((u64)disk_reshdr->size_in_wim[4] << 32) |
1281                                ((u64)disk_reshdr->size_in_wim[5] << 40) |
1282                                ((u64)disk_reshdr->size_in_wim[6] << 48));
1283         reshdr->uncompressed_size = le64_to_cpu(disk_reshdr->uncompressed_size);
1284         reshdr->flags = disk_reshdr->flags;
1285 }
1286
1287 /* Export a WIM resource header to the on-disk format.  */
1288 void
1289 put_wim_reshdr(const struct wim_reshdr *reshdr,
1290                struct wim_reshdr_disk *disk_reshdr)
1291 {
1292         disk_reshdr->size_in_wim[0] = reshdr->size_in_wim  >>  0;
1293         disk_reshdr->size_in_wim[1] = reshdr->size_in_wim  >>  8;
1294         disk_reshdr->size_in_wim[2] = reshdr->size_in_wim  >> 16;
1295         disk_reshdr->size_in_wim[3] = reshdr->size_in_wim  >> 24;
1296         disk_reshdr->size_in_wim[4] = reshdr->size_in_wim  >> 32;
1297         disk_reshdr->size_in_wim[5] = reshdr->size_in_wim  >> 40;
1298         disk_reshdr->size_in_wim[6] = reshdr->size_in_wim  >> 48;
1299         disk_reshdr->flags = reshdr->flags;
1300         disk_reshdr->offset_in_wim = cpu_to_le64(reshdr->offset_in_wim);
1301         disk_reshdr->uncompressed_size = cpu_to_le64(reshdr->uncompressed_size);
1302 }