]> wimlib.net Git - wimlib/blob - src/blob_table.c
1d87a71761ed219846e5d6395a3f912df83786fa
[wimlib] / src / blob_table.c
1 /*
2  * blob_table.c
3  *
4  * A blob table maps SHA-1 message digests to "blobs", which are nonempty
5  * sequences of binary data.  Within a WIM file, blobs are single-instanced.
6  *
7  * This file also contains code to read and write the corresponding on-disk
8  * representation of this table in the WIM file format.
9  */
10
11 /*
12  * Copyright (C) 2012, 2013, 2014, 2015 Eric Biggers
13  *
14  * This file is free software; you can redistribute it and/or modify it under
15  * the terms of the GNU Lesser General Public License as published by the Free
16  * Software Foundation; either version 3 of the License, or (at your option) any
17  * later version.
18  *
19  * This file is distributed in the hope that it will be useful, but WITHOUT
20  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
21  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
22  * details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with this file; if not, see http://www.gnu.org/licenses/.
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #  include "config.h"
30 #endif
31
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h> /* for unlink()  */
35
36 #include "wimlib/assert.h"
37 #include "wimlib/blob_table.h"
38 #include "wimlib/encoding.h"
39 #include "wimlib/endianness.h"
40 #include "wimlib/error.h"
41 #include "wimlib/metadata.h"
42 #include "wimlib/ntfs_3g.h"
43 #include "wimlib/resource.h"
44 #include "wimlib/unaligned.h"
45 #include "wimlib/util.h"
46 #include "wimlib/write.h"
47
48 /* A hash table mapping SHA-1 message digests to blob descriptors  */
49 struct blob_table {
50         struct hlist_head *array;
51         size_t num_blobs;
52         size_t capacity;
53 };
54
55 struct blob_table *
56 new_blob_table(size_t capacity)
57 {
58         struct blob_table *table;
59         struct hlist_head *array;
60
61         table = MALLOC(sizeof(struct blob_table));
62         if (table == NULL)
63                 goto oom;
64
65         array = CALLOC(capacity, sizeof(array[0]));
66         if (array == NULL) {
67                 FREE(table);
68                 goto oom;
69         }
70
71         table->num_blobs = 0;
72         table->capacity = capacity;
73         table->array = array;
74         return table;
75
76 oom:
77         ERROR("Failed to allocate memory for blob table "
78               "with capacity %zu", capacity);
79         return NULL;
80 }
81
82 static int
83 do_free_blob_descriptor(struct blob_descriptor *blob, void *_ignore)
84 {
85         free_blob_descriptor(blob);
86         return 0;
87 }
88
89 void
90 free_blob_table(struct blob_table *table)
91 {
92         if (table) {
93                 for_blob_in_table(table, do_free_blob_descriptor, NULL);
94                 FREE(table->array);
95                 FREE(table);
96         }
97 }
98
99 struct blob_descriptor *
100 new_blob_descriptor(void)
101 {
102         BUILD_BUG_ON(BLOB_NONEXISTENT != 0);
103         return CALLOC(1, sizeof(struct blob_descriptor));
104 }
105
106 struct blob_descriptor *
107 clone_blob_descriptor(const struct blob_descriptor *old)
108 {
109         struct blob_descriptor *new;
110
111         new = memdup(old, sizeof(struct blob_descriptor));
112         if (new == NULL)
113                 return NULL;
114
115         switch (new->blob_location) {
116         case BLOB_IN_WIM:
117                 list_add(&new->rdesc_node, &new->rdesc->blob_list);
118                 break;
119
120         case BLOB_IN_FILE_ON_DISK:
121 #ifdef __WIN32__
122         case BLOB_IN_WINNT_FILE_ON_DISK:
123         case BLOB_WIN32_ENCRYPTED:
124 #endif
125 #ifdef WITH_FUSE
126         case BLOB_IN_STAGING_FILE:
127                 BUILD_BUG_ON((void*)&old->file_on_disk !=
128                              (void*)&old->staging_file_name);
129 #endif
130                 new->file_on_disk = TSTRDUP(old->file_on_disk);
131                 if (new->file_on_disk == NULL)
132                         goto out_free;
133                 break;
134         case BLOB_IN_ATTACHED_BUFFER:
135                 new->attached_buffer = memdup(old->attached_buffer, old->size);
136                 if (new->attached_buffer == NULL)
137                         goto out_free;
138                 break;
139 #ifdef WITH_NTFS_3G
140         case BLOB_IN_NTFS_VOLUME:
141                 new->ntfs_loc = clone_ntfs_location(old->ntfs_loc);
142                 if (!new->ntfs_loc)
143                         goto out_free;
144                 break;
145 #endif
146         }
147         return new;
148
149 out_free:
150         free_blob_descriptor(new);
151         return NULL;
152 }
153
154 static void
155 blob_release_location(struct blob_descriptor *blob)
156 {
157         switch (blob->blob_location) {
158         case BLOB_IN_WIM:
159                 list_del(&blob->rdesc_node);
160                 if (list_empty(&blob->rdesc->blob_list))
161                         FREE(blob->rdesc);
162                 break;
163         case BLOB_IN_FILE_ON_DISK:
164 #ifdef __WIN32__
165         case BLOB_IN_WINNT_FILE_ON_DISK:
166         case BLOB_WIN32_ENCRYPTED:
167 #endif
168 #ifdef WITH_FUSE
169         case BLOB_IN_STAGING_FILE:
170                 BUILD_BUG_ON((void*)&blob->file_on_disk !=
171                              (void*)&blob->staging_file_name);
172 #endif
173         case BLOB_IN_ATTACHED_BUFFER:
174                 BUILD_BUG_ON((void*)&blob->file_on_disk !=
175                              (void*)&blob->attached_buffer);
176                 FREE(blob->file_on_disk);
177                 break;
178 #ifdef WITH_NTFS_3G
179         case BLOB_IN_NTFS_VOLUME:
180                 if (blob->ntfs_loc)
181                         free_ntfs_location(blob->ntfs_loc);
182                 break;
183 #endif
184         }
185 }
186
187 void
188 free_blob_descriptor(struct blob_descriptor *blob)
189 {
190         if (blob) {
191                 blob_release_location(blob);
192                 FREE(blob);
193         }
194 }
195
196 /* Should this blob be retained even if it has no references?  */
197 static bool
198 should_retain_blob(const struct blob_descriptor *blob)
199 {
200         return blob->blob_location == BLOB_IN_WIM;
201 }
202
203 static void
204 finalize_blob(struct blob_descriptor *blob)
205 {
206         if (!should_retain_blob(blob))
207                 free_blob_descriptor(blob);
208 }
209
210 /*
211  * Decrements the reference count of the specified blob, which must be either
212  * (a) unhashed, or (b) inserted in the specified blob table.
213  *
214  * If the blob's reference count reaches 0, we may unlink it from @table and
215  * free it.  However, we retain blobs with 0 reference count that originated
216  * from WIM files (BLOB_IN_WIM).  We do this for two reasons:
217  *
218  * 1. This prevents information about valid blobs in a WIM file --- blobs which
219  *    will continue to be present after appending to the WIM file --- from being
220  *    lost merely because we dropped all references to them.
221  *
222  * 2. Blob reference counts we read from WIM files can't be trusted.  It's
223  *    possible that a WIM has reference counts that are too low; WIMGAPI
224  *    sometimes creates WIMs where this is the case.  It's also possible that
225  *    blobs have been referenced from an external WIM; those blobs can
226  *    potentially have any reference count at all, either lower or higher than
227  *    would be expected for this WIM ("this WIM" meaning the owner of @table) if
228  *    it were a standalone WIM.
229  *
230  * So we can't take the reference counts too seriously.  But at least, we do
231  * recalculate by default when writing a new WIM file.
232  */
233 void
234 blob_decrement_refcnt(struct blob_descriptor *blob, struct blob_table *table)
235 {
236         blob_subtract_refcnt(blob, table, 1);
237 }
238
239 void
240 blob_subtract_refcnt(struct blob_descriptor *blob, struct blob_table *table,
241                      u32 count)
242 {
243         if (unlikely(blob->refcnt < count)) {
244                 blob->refcnt = 0; /* See comment above  */
245                 return;
246         }
247
248         blob->refcnt -= count;
249
250         if (blob->refcnt != 0)
251                 return;
252
253         if (blob->unhashed) {
254                 list_del(&blob->unhashed_list);
255         #ifdef WITH_FUSE
256                 /* If the blob has been extracted to a staging file for a FUSE
257                  * mount, unlink the staging file.  (Note that there still may
258                  * be open file descriptors to it.)  */
259                 if (blob->blob_location == BLOB_IN_STAGING_FILE)
260                         unlinkat(blob->staging_dir_fd,
261                                  blob->staging_file_name, 0);
262         #endif
263         } else {
264                 if (!should_retain_blob(blob))
265                         blob_table_unlink(table, blob);
266         }
267
268         /* If FUSE mounts are enabled, then don't actually free the blob
269          * descriptor until the last file descriptor to it has been closed.  */
270 #ifdef WITH_FUSE
271         if (blob->num_opened_fds == 0)
272 #endif
273                 finalize_blob(blob);
274 }
275
276 #ifdef WITH_FUSE
277 void
278 blob_decrement_num_opened_fds(struct blob_descriptor *blob)
279 {
280         wimlib_assert(blob->num_opened_fds != 0);
281
282         if (--blob->num_opened_fds == 0 && blob->refcnt == 0)
283                 finalize_blob(blob);
284 }
285 #endif
286
287 static void
288 blob_table_insert_raw(struct blob_table *table, struct blob_descriptor *blob)
289 {
290         size_t i = blob->hash_short % table->capacity;
291
292         hlist_add_head(&blob->hash_list, &table->array[i]);
293 }
294
295 static void
296 enlarge_blob_table(struct blob_table *table)
297 {
298         size_t old_capacity, new_capacity;
299         struct hlist_head *old_array, *new_array;
300         struct blob_descriptor *blob;
301         struct hlist_node *tmp;
302         size_t i;
303
304         old_capacity = table->capacity;
305         new_capacity = old_capacity * 2;
306         new_array = CALLOC(new_capacity, sizeof(struct hlist_head));
307         if (new_array == NULL)
308                 return;
309         old_array = table->array;
310         table->array = new_array;
311         table->capacity = new_capacity;
312
313         for (i = 0; i < old_capacity; i++) {
314                 hlist_for_each_entry_safe(blob, tmp, &old_array[i], hash_list) {
315                         hlist_del(&blob->hash_list);
316                         blob_table_insert_raw(table, blob);
317                 }
318         }
319         FREE(old_array);
320 }
321
322 /* Insert a blob descriptor into the blob table.  */
323 void
324 blob_table_insert(struct blob_table *table, struct blob_descriptor *blob)
325 {
326         blob_table_insert_raw(table, blob);
327         if (++table->num_blobs > table->capacity)
328                 enlarge_blob_table(table);
329 }
330
331 /* Unlinks a blob descriptor from the blob table; does not free it.  */
332 void
333 blob_table_unlink(struct blob_table *table, struct blob_descriptor *blob)
334 {
335         wimlib_assert(!blob->unhashed);
336         wimlib_assert(table->num_blobs != 0);
337
338         hlist_del(&blob->hash_list);
339         table->num_blobs--;
340 }
341
342 /* Given a SHA-1 message digest, return the corresponding blob descriptor from
343  * the specified blob table, or NULL if there is none.  */
344 struct blob_descriptor *
345 lookup_blob(const struct blob_table *table, const u8 *hash)
346 {
347         size_t i;
348         struct blob_descriptor *blob;
349
350         i = load_size_t_unaligned(hash) % table->capacity;
351         hlist_for_each_entry(blob, &table->array[i], hash_list)
352                 if (hashes_equal(hash, blob->hash))
353                         return blob;
354         return NULL;
355 }
356
357 /* Call a function on all blob descriptors in the specified blob table.  Stop
358  * early and return nonzero if any call to the function returns nonzero.  */
359 int
360 for_blob_in_table(struct blob_table *table,
361                   int (*visitor)(struct blob_descriptor *, void *), void *arg)
362 {
363         struct blob_descriptor *blob;
364         struct hlist_node *tmp;
365         int ret;
366
367         for (size_t i = 0; i < table->capacity; i++) {
368                 hlist_for_each_entry_safe(blob, tmp, &table->array[i],
369                                           hash_list)
370                 {
371                         ret = visitor(blob, arg);
372                         if (ret)
373                                 return ret;
374                 }
375         }
376         return 0;
377 }
378
379 /*
380  * This is a qsort() callback that sorts blobs into an order optimized for
381  * reading.  Sorting is done primarily by blob location, then secondarily by a
382  * location-dependent order.  For example, blobs in WIM resources are sorted
383  * such that the underlying WIM files will be read sequentially.  This is
384  * especially important for WIM files containing solid resources.
385  */
386 int
387 cmp_blobs_by_sequential_order(const void *p1, const void *p2)
388 {
389         const struct blob_descriptor *blob1, *blob2;
390         int v;
391         WIMStruct *wim1, *wim2;
392
393         blob1 = *(const struct blob_descriptor**)p1;
394         blob2 = *(const struct blob_descriptor**)p2;
395
396         v = (int)blob1->blob_location - (int)blob2->blob_location;
397
398         /* Different resource locations?  */
399         if (v)
400                 return v;
401
402         switch (blob1->blob_location) {
403         case BLOB_IN_WIM:
404                 wim1 = blob1->rdesc->wim;
405                 wim2 = blob2->rdesc->wim;
406
407                 /* Different (possibly split) WIMs?  */
408                 if (wim1 != wim2) {
409                         v = memcmp(wim1->hdr.guid, wim2->hdr.guid, WIM_GUID_LEN);
410                         if (v)
411                                 return v;
412                 }
413
414                 /* Different part numbers in the same WIM?  */
415                 v = (int)wim1->hdr.part_number - (int)wim2->hdr.part_number;
416                 if (v)
417                         return v;
418
419                 if (blob1->rdesc->offset_in_wim != blob2->rdesc->offset_in_wim)
420                         return cmp_u64(blob1->rdesc->offset_in_wim,
421                                        blob2->rdesc->offset_in_wim);
422
423                 return cmp_u64(blob1->offset_in_res, blob2->offset_in_res);
424
425         case BLOB_IN_FILE_ON_DISK:
426 #ifdef WITH_FUSE
427         case BLOB_IN_STAGING_FILE:
428 #endif
429 #ifdef __WIN32__
430         case BLOB_IN_WINNT_FILE_ON_DISK:
431         case BLOB_WIN32_ENCRYPTED:
432 #endif
433                 /* Compare files by path: just a heuristic that will place files
434                  * in the same directory next to each other.  */
435                 return tstrcmp(blob1->file_on_disk, blob2->file_on_disk);
436 #ifdef WITH_NTFS_3G
437         case BLOB_IN_NTFS_VOLUME:
438                 return cmp_ntfs_locations(blob1->ntfs_loc, blob2->ntfs_loc);
439 #endif
440         default:
441                 /* No additional sorting order defined for this resource
442                  * location (e.g. BLOB_IN_ATTACHED_BUFFER); simply compare
443                  * everything equal to each other.  */
444                 return 0;
445         }
446 }
447
448 int
449 sort_blob_list(struct list_head *blob_list, size_t list_head_offset,
450                int (*compar)(const void *, const void*))
451 {
452         struct list_head *cur;
453         struct blob_descriptor **array;
454         size_t i;
455         size_t array_size;
456         size_t num_blobs = 0;
457
458         list_for_each(cur, blob_list)
459                 num_blobs++;
460
461         if (num_blobs <= 1)
462                 return 0;
463
464         array_size = num_blobs * sizeof(array[0]);
465         array = MALLOC(array_size);
466         if (array == NULL)
467                 return WIMLIB_ERR_NOMEM;
468
469         cur = blob_list->next;
470         for (i = 0; i < num_blobs; i++) {
471                 array[i] = (struct blob_descriptor*)((u8*)cur - list_head_offset);
472                 cur = cur->next;
473         }
474
475         qsort(array, num_blobs, sizeof(array[0]), compar);
476
477         INIT_LIST_HEAD(blob_list);
478         for (i = 0; i < num_blobs; i++) {
479                 list_add_tail((struct list_head*)
480                                ((u8*)array[i] + list_head_offset), blob_list);
481         }
482         FREE(array);
483         return 0;
484 }
485
486 /* Sort the specified list of blobs in an order optimized for sequential
487  * reading.  */
488 int
489 sort_blob_list_by_sequential_order(struct list_head *blob_list,
490                                    size_t list_head_offset)
491 {
492         return sort_blob_list(blob_list, list_head_offset,
493                               cmp_blobs_by_sequential_order);
494 }
495
496 static int
497 add_blob_to_array(struct blob_descriptor *blob, void *_pp)
498 {
499         struct blob_descriptor ***pp = _pp;
500         *(*pp)++ = blob;
501         return 0;
502 }
503
504 /* Iterate through the blob descriptors in the specified blob table in an order
505  * optimized for sequential reading.  */
506 int
507 for_blob_in_table_sorted_by_sequential_order(struct blob_table *table,
508                                              int (*visitor)(struct blob_descriptor *, void *),
509                                              void *arg)
510 {
511         struct blob_descriptor **blob_array, **p;
512         size_t num_blobs = table->num_blobs;
513         int ret;
514
515         blob_array = MALLOC(num_blobs * sizeof(blob_array[0]));
516         if (!blob_array)
517                 return WIMLIB_ERR_NOMEM;
518         p = blob_array;
519         for_blob_in_table(table, add_blob_to_array, &p);
520
521         wimlib_assert(p == blob_array + num_blobs);
522
523         qsort(blob_array, num_blobs, sizeof(blob_array[0]),
524               cmp_blobs_by_sequential_order);
525         ret = 0;
526         for (size_t i = 0; i < num_blobs; i++) {
527                 ret = visitor(blob_array[i], arg);
528                 if (ret)
529                         break;
530         }
531         FREE(blob_array);
532         return ret;
533 }
534
535 /* On-disk format of a blob descriptor in a WIM file.
536  *
537  * Note: if the WIM file contains solid resource(s), then this structure is
538  * sometimes overloaded to describe a "resource" rather than a "blob".  See the
539  * code for details.  */
540 struct blob_descriptor_disk {
541
542         /* Size, offset, and flags of the blob.  */
543         struct wim_reshdr_disk reshdr;
544
545         /* Which part of the split WIM this blob is in; indexed from 1. */
546         le16 part_number;
547
548         /* Reference count of this blob over all WIM images.  (But see comment
549          * above blob_decrement_refcnt().)  */
550         le32 refcnt;
551
552         /* SHA-1 message digest of the uncompressed data of this blob, or all
553          * zeroes if this blob is of zero length.  */
554         u8 hash[SHA1_HASH_SIZE];
555 } _packed_attribute;
556
557 /* Given a nonempty run of consecutive blob descriptors with the SOLID flag set,
558  * count how many specify resources (as opposed to blobs within those
559  * resources).
560  *
561  * Returns the resulting count.  */
562 static size_t
563 count_solid_resources(const struct blob_descriptor_disk *entries, size_t max)
564 {
565         size_t count = 0;
566         do {
567                 struct wim_reshdr reshdr;
568
569                 get_wim_reshdr(&(entries++)->reshdr, &reshdr);
570
571                 if (!(reshdr.flags & WIM_RESHDR_FLAG_SOLID)) {
572                         /* Run was terminated by a stand-alone blob entry.  */
573                         break;
574                 }
575
576                 if (reshdr.uncompressed_size == SOLID_RESOURCE_MAGIC_NUMBER) {
577                         /* This is a resource entry.  */
578                         count++;
579                 }
580         } while (--max);
581         return count;
582 }
583
584 /*
585  * Given a run of consecutive blob descriptors with the SOLID flag set and
586  * having @num_rdescs resource entries, load resource information from them into
587  * the resource descriptors in the @rdescs array.
588  *
589  * Returns 0 on success, or a nonzero error code on failure.
590  */
591 static int
592 do_load_solid_info(WIMStruct *wim, struct wim_resource_descriptor **rdescs,
593                    size_t num_rdescs,
594                    const struct blob_descriptor_disk *entries)
595 {
596         for (size_t i = 0; i < num_rdescs; i++) {
597                 struct wim_reshdr reshdr;
598                 struct alt_chunk_table_header_disk hdr;
599                 struct wim_resource_descriptor *rdesc;
600                 int ret;
601
602                 /* Advance to next resource entry.  */
603
604                 do {
605                         get_wim_reshdr(&(entries++)->reshdr, &reshdr);
606                 } while (reshdr.uncompressed_size != SOLID_RESOURCE_MAGIC_NUMBER);
607
608                 rdesc = rdescs[i];
609
610                 wim_res_hdr_to_desc(&reshdr, wim, rdesc);
611
612                 /* For solid resources, the uncompressed size, compression type,
613                  * and chunk size are stored in the resource itself, not in the
614                  * blob table.  */
615
616                 ret = full_pread(&wim->in_fd, &hdr,
617                                  sizeof(hdr), reshdr.offset_in_wim);
618                 if (ret) {
619                         ERROR("Failed to read header of solid resource "
620                               "(offset_in_wim=%"PRIu64")",
621                               reshdr.offset_in_wim);
622                         return ret;
623                 }
624
625                 rdesc->uncompressed_size = le64_to_cpu(hdr.res_usize);
626
627                 /* Compression format numbers must be the same as in
628                  * WIMGAPI to be compatible here.  */
629                 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_NONE != 0);
630                 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_XPRESS != 1);
631                 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_LZX != 2);
632                 BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_LZMS != 3);
633                 rdesc->compression_type = le32_to_cpu(hdr.compression_format);
634
635                 rdesc->chunk_size = le32_to_cpu(hdr.chunk_size);
636
637                 DEBUG("Solid resource %zu/%zu: %"PRIu64" => %"PRIu64" "
638                       "(%"TS"/%"PRIu32") @ +%"PRIu64"",
639                       i + 1, num_rdescs,
640                       rdesc->uncompressed_size,
641                       rdesc->size_in_wim,
642                       wimlib_get_compression_type_string(rdesc->compression_type),
643                       rdesc->chunk_size,
644                       rdesc->offset_in_wim);
645         }
646         return 0;
647 }
648
649 /*
650  * Given a nonempty run of consecutive blob descriptors with the SOLID flag set,
651  * allocate a 'struct wim_resource_descriptor' for each resource within that
652  * run.
653  *
654  * Returns 0 on success, or a nonzero error code on failure.
655  * Returns the pointers and count in *rdescs_ret and *num_rdescs_ret.
656  */
657 static int
658 load_solid_info(WIMStruct *wim,
659                 const struct blob_descriptor_disk *entries,
660                 size_t num_remaining_entries,
661                 struct wim_resource_descriptor ***rdescs_ret,
662                 size_t *num_rdescs_ret)
663 {
664         size_t num_rdescs;
665         struct wim_resource_descriptor **rdescs;
666         size_t i;
667         int ret;
668
669         num_rdescs = count_solid_resources(entries, num_remaining_entries);
670         rdescs = CALLOC(num_rdescs, sizeof(rdescs[0]));
671         if (!rdescs)
672                 return WIMLIB_ERR_NOMEM;
673
674         for (i = 0; i < num_rdescs; i++) {
675                 rdescs[i] = MALLOC(sizeof(struct wim_resource_descriptor));
676                 if (!rdescs[i]) {
677                         ret = WIMLIB_ERR_NOMEM;
678                         goto out_free_rdescs;
679                 }
680         }
681
682         ret = do_load_solid_info(wim, rdescs, num_rdescs, entries);
683         if (ret)
684                 goto out_free_rdescs;
685
686         *rdescs_ret = rdescs;
687         *num_rdescs_ret = num_rdescs;
688         return 0;
689
690 out_free_rdescs:
691         for (i = 0; i < num_rdescs; i++)
692                 FREE(rdescs[i]);
693         FREE(rdescs);
694         return ret;
695 }
696
697 /* Given a 'struct blob_descriptor' allocated for an on-disk blob descriptor
698  * with the SOLID flag set, try to assign it to resource in the current solid
699  * run.  */
700 static int
701 assign_blob_to_solid_resource(const struct wim_reshdr *reshdr,
702                               struct blob_descriptor *blob,
703                               struct wim_resource_descriptor **rdescs,
704                               size_t num_rdescs)
705 {
706         u64 offset = reshdr->offset_in_wim;
707
708         /* XXX: This linear search will be slow in the degenerate case where the
709          * number of solid resources in the run is huge.  */
710         blob->size = reshdr->size_in_wim;
711         for (size_t i = 0; i < num_rdescs; i++) {
712                 if (offset + blob->size <= rdescs[i]->uncompressed_size) {
713                         blob_set_is_located_in_wim_resource(blob, rdescs[i], offset);
714                         return 0;
715                 }
716                 offset -= rdescs[i]->uncompressed_size;
717         }
718         ERROR("blob could not be assigned to a solid resource");
719         return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
720 }
721
722 static void
723 free_solid_rdescs(struct wim_resource_descriptor **rdescs, size_t num_rdescs)
724 {
725         if (rdescs) {
726                 for (size_t i = 0; i < num_rdescs; i++)
727                         if (list_empty(&rdescs[i]->blob_list))
728                                 FREE(rdescs[i]);
729                 FREE(rdescs);
730         }
731 }
732
733 static int
734 cmp_blobs_by_offset_in_res(const void *p1, const void *p2)
735 {
736         const struct blob_descriptor *blob1, *blob2;
737
738         blob1 = *(const struct blob_descriptor**)p1;
739         blob2 = *(const struct blob_descriptor**)p2;
740
741         return cmp_u64(blob1->offset_in_res, blob2->offset_in_res);
742 }
743
744 /* Validate the size and location of a WIM resource.  */
745 static int
746 validate_resource(struct wim_resource_descriptor *rdesc)
747 {
748         struct blob_descriptor *blob;
749         bool out_of_order;
750         u64 expected_next_offset;
751         int ret;
752
753         /* Verify that the resource itself has a valid offset and size.  */
754         if (rdesc->offset_in_wim + rdesc->size_in_wim < rdesc->size_in_wim)
755                 goto invalid_due_to_overflow;
756
757         /* Verify that each blob in the resource has a valid offset and size.
758          */
759         expected_next_offset = 0;
760         out_of_order = false;
761         list_for_each_entry(blob, &rdesc->blob_list, rdesc_node) {
762                 if (blob->offset_in_res + blob->size < blob->size ||
763                     blob->offset_in_res + blob->size > rdesc->uncompressed_size)
764                         goto invalid_due_to_overflow;
765
766                 if (blob->offset_in_res >= expected_next_offset)
767                         expected_next_offset = blob->offset_in_res + blob->size;
768                 else
769                         out_of_order = true;
770         }
771
772         /* If the blobs were not located at strictly increasing positions (not
773          * allowing for overlap), sort them.  Then make sure that none overlap.
774          */
775         if (out_of_order) {
776                 ret = sort_blob_list(&rdesc->blob_list,
777                                      offsetof(struct blob_descriptor,
778                                               rdesc_node),
779                                      cmp_blobs_by_offset_in_res);
780                 if (ret)
781                         return ret;
782
783                 expected_next_offset = 0;
784                 list_for_each_entry(blob, &rdesc->blob_list, rdesc_node) {
785                         if (blob->offset_in_res >= expected_next_offset)
786                                 expected_next_offset = blob->offset_in_res + blob->size;
787                         else
788                                 goto invalid_due_to_overlap;
789                 }
790         }
791
792         return 0;
793
794 invalid_due_to_overflow:
795         ERROR("Invalid blob table (offset overflow)");
796         return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
797
798 invalid_due_to_overlap:
799         ERROR("Invalid blob table (blobs in solid resource overlap)");
800         return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
801 }
802
803 static int
804 finish_solid_rdescs(struct wim_resource_descriptor **rdescs, size_t num_rdescs)
805 {
806         int ret = 0;
807         for (size_t i = 0; i < num_rdescs; i++) {
808                 ret = validate_resource(rdescs[i]);
809                 if (ret)
810                         break;
811         }
812         free_solid_rdescs(rdescs, num_rdescs);
813         return ret;
814 }
815
816 /*
817  * read_blob_table() -
818  *
819  * Read the blob table from a WIM file.  Usually, each entry in this table
820  * describes a "blob", or equivalently a "resource", that the WIM file contains,
821  * along with its location and SHA-1 message digest.  Descriptors for
822  * non-metadata blobs will be saved in the in-memory blob table
823  * (wim->blob_table), whereas descriptors for metadata blobs will be saved in a
824  * special location per-image (the wim->image_metadata array).
825  *
826  * However, in WIM_VERSION_SOLID (3584) WIMs, a resource may contain multiple
827  * blobs that are compressed together.  Such a resource is called a "solid
828  * resource".  Solid resources are still described in the on-disk "blob table",
829  * although the format is not the most logical.  A consecutive sequence of
830  * entries that all have flag WIM_RESHDR_FLAG_SOLID (0x10) set is a "solid run".
831  * A solid run describes a set of solid resources, each of which contains a set
832  * of blobs.  In a solid run, a 'struct wim_reshdr_disk' with 'uncompressed_size
833  * = SOLID_RESOURCE_MAGIC_NUMBER (0x100000000)' specifies a solid resource,
834  * whereas any other 'struct wim_reshdr_disk' specifies a blob within a solid
835  * resource.  There are some oddities in how we need to determine which solid
836  * resource a blob is actually in; see the code for details.
837  *
838  * Possible return values:
839  *      WIMLIB_ERR_SUCCESS (0)
840  *      WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY
841  *      WIMLIB_ERR_NOMEM
842  *
843  *      Or an error code caused by failure to read the blob table from the WIM
844  *      file.
845  */
846 int
847 read_blob_table(WIMStruct *wim)
848 {
849         int ret;
850         size_t num_entries;
851         void *buf = NULL;
852         struct blob_table *table = NULL;
853         struct blob_descriptor *cur_blob = NULL;
854         size_t num_duplicate_blobs = 0;
855         size_t num_wrong_part_blobs = 0;
856         u32 image_index = 0;
857         struct wim_resource_descriptor **cur_solid_rdescs = NULL;
858         size_t cur_num_solid_rdescs = 0;
859
860         DEBUG("Reading blob table.");
861
862         /* Calculate the number of entries in the blob table.  */
863         num_entries = wim->hdr.blob_table_reshdr.uncompressed_size /
864                       sizeof(struct blob_descriptor_disk);
865
866         /* Read the blob table into a buffer.  */
867         ret = wim_reshdr_to_data(&wim->hdr.blob_table_reshdr, wim, &buf);
868         if (ret)
869                 goto out;
870
871         /* Allocate a hash table to map SHA-1 message digests into blob
872          * descriptors.  This is the in-memory "blob table".  */
873         table = new_blob_table(num_entries * 2 + 1);
874         if (!table)
875                 goto oom;
876
877         /* Allocate and initalize blob descriptors from the raw blob table
878          * buffer.  */
879         for (size_t i = 0; i < num_entries; i++) {
880                 const struct blob_descriptor_disk *disk_entry =
881                         &((const struct blob_descriptor_disk*)buf)[i];
882                 struct wim_reshdr reshdr;
883                 u16 part_number;
884
885                 /* Get the resource header  */
886                 get_wim_reshdr(&disk_entry->reshdr, &reshdr);
887
888                 DEBUG("reshdr: size_in_wim=%"PRIu64", "
889                       "uncompressed_size=%"PRIu64", "
890                       "offset_in_wim=%"PRIu64", "
891                       "flags=0x%02x",
892                       reshdr.size_in_wim, reshdr.uncompressed_size,
893                       reshdr.offset_in_wim, reshdr.flags);
894
895                 /* Ignore SOLID flag if it isn't supposed to be used in this WIM
896                  * version.  */
897                 if (wim->hdr.wim_version == WIM_VERSION_DEFAULT)
898                         reshdr.flags &= ~WIM_RESHDR_FLAG_SOLID;
899
900                 /* Allocate a new 'struct blob_descriptor'.  */
901                 cur_blob = new_blob_descriptor();
902                 if (!cur_blob)
903                         goto oom;
904
905                 /* Get the part number, reference count, and hash.  */
906                 part_number = le16_to_cpu(disk_entry->part_number);
907                 cur_blob->refcnt = le32_to_cpu(disk_entry->refcnt);
908                 copy_hash(cur_blob->hash, disk_entry->hash);
909
910                 if (reshdr.flags & WIM_RESHDR_FLAG_SOLID) {
911
912                         /* SOLID entry  */
913
914                         if (!cur_solid_rdescs) {
915                                 /* Starting new run  */
916                                 ret = load_solid_info(wim, disk_entry,
917                                                       num_entries - i,
918                                                       &cur_solid_rdescs,
919                                                       &cur_num_solid_rdescs);
920                                 if (ret)
921                                         goto out;
922                         }
923
924                         if (reshdr.uncompressed_size == SOLID_RESOURCE_MAGIC_NUMBER) {
925                                 /* Resource entry, not blob entry  */
926                                 goto free_cur_blob_and_continue;
927                         }
928
929                         /* Blob entry  */
930
931                         ret = assign_blob_to_solid_resource(&reshdr,
932                                                             cur_blob,
933                                                             cur_solid_rdescs,
934                                                             cur_num_solid_rdescs);
935                         if (ret)
936                                 goto out;
937
938                 } else {
939                         /* Normal blob/resource entry; SOLID not set.  */
940
941                         struct wim_resource_descriptor *rdesc;
942
943                         if (unlikely(cur_solid_rdescs)) {
944                                 /* This entry terminated a solid run.  */
945                                 ret = finish_solid_rdescs(cur_solid_rdescs,
946                                                           cur_num_solid_rdescs);
947                                 cur_solid_rdescs = NULL;
948                                 if (ret)
949                                         goto out;
950                         }
951
952                         /* How to handle an uncompressed resource with its
953                          * uncompressed size different from its compressed size?
954                          *
955                          * Based on a simple test, WIMGAPI seems to handle this
956                          * as follows:
957                          *
958                          * if (size_in_wim > uncompressed_size) {
959                          *      Ignore uncompressed_size; use size_in_wim
960                          *      instead.
961                          * } else {
962                          *      Honor uncompressed_size, but treat the part of
963                          *      the file data above size_in_wim as all zeros.
964                          * }
965                          *
966                          * So we will do the same.  */
967                         if (unlikely(!(reshdr.flags &
968                                        WIM_RESHDR_FLAG_COMPRESSED) &&
969                                      (reshdr.size_in_wim >
970                                       reshdr.uncompressed_size)))
971                         {
972                                 reshdr.uncompressed_size = reshdr.size_in_wim;
973                         }
974
975                         /* Set up a resource descriptor for this blob.  */
976
977                         rdesc = MALLOC(sizeof(struct wim_resource_descriptor));
978                         if (!rdesc)
979                                 goto oom;
980
981                         wim_res_hdr_to_desc(&reshdr, wim, rdesc);
982
983                         blob_set_is_located_in_nonsolid_wim_resource(cur_blob, rdesc);
984                 }
985
986                 /* cur_blob is now a blob bound to a resource.  */
987
988                 /* Ignore entries with all zeroes in the hash field.  */
989                 if (is_zero_hash(cur_blob->hash))
990                         goto free_cur_blob_and_continue;
991
992                 /* Verify that the part number matches that of the underlying
993                  * WIM file.  */
994                 if (part_number != wim->hdr.part_number) {
995                         num_wrong_part_blobs++;
996                         goto free_cur_blob_and_continue;
997                 }
998
999                 if (reshdr.flags & WIM_RESHDR_FLAG_METADATA) {
1000
1001                         cur_blob->is_metadata = 1;
1002
1003                         /* Blob table entry for a metadata resource.  */
1004
1005                         /* Metadata entries with no references must be ignored.
1006                          * See, for example, the WinPE WIMs from the WAIK v2.1.
1007                          */
1008                         if (cur_blob->refcnt == 0)
1009                                 goto free_cur_blob_and_continue;
1010
1011                         if (cur_blob->refcnt != 1) {
1012                                 /* We don't currently support this case due to
1013                                  * the complications of multiple images sharing
1014                                  * the same metadata resource or a metadata
1015                                  * resource also being referenced by files.  */
1016                                 ERROR("Found metadata resource with refcnt != 1");
1017                                 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
1018                                 goto out;
1019                         }
1020
1021                         if (wim->hdr.part_number != 1) {
1022                                 WARNING("Ignoring metadata resource found in a "
1023                                         "non-first part of the split WIM");
1024                                 goto free_cur_blob_and_continue;
1025                         }
1026
1027                         /* The number of entries in the blob table with
1028                          * WIM_RESHDR_FLAG_METADATA set should be the same as
1029                          * the image_count field in the WIM header.  */
1030                         if (image_index == wim->hdr.image_count) {
1031                                 WARNING("Found more metadata resources than images");
1032                                 goto free_cur_blob_and_continue;
1033                         }
1034
1035                         /* Notice very carefully:  We are assigning the metadata
1036                          * resources to images in the same order in which their
1037                          * blob table entries occur on disk.  (This is also the
1038                          * behavior of Microsoft's software.)  In particular,
1039                          * this overrides the actual locations of the metadata
1040                          * resources themselves in the WIM file as well as any
1041                          * information written in the XML data.  */
1042                         DEBUG("Found metadata resource for image %"PRIu32" at "
1043                               "offset %"PRIu64".",
1044                               image_index + 1,
1045                               reshdr.offset_in_wim);
1046
1047                         wim->image_metadata[image_index++]->metadata_blob = cur_blob;
1048                 } else {
1049                         /* Blob table entry for a non-metadata blob.  */
1050
1051                         /* Ignore this blob if it's a duplicate.  */
1052                         if (lookup_blob(table, cur_blob->hash)) {
1053                                 num_duplicate_blobs++;
1054                                 goto free_cur_blob_and_continue;
1055                         }
1056
1057                         /* Insert the blob into the in-memory blob table, keyed
1058                          * by its SHA-1 message digest.  */
1059                         blob_table_insert(table, cur_blob);
1060                 }
1061
1062                 continue;
1063
1064         free_cur_blob_and_continue:
1065                 if (cur_solid_rdescs &&
1066                     cur_blob->blob_location == BLOB_IN_WIM)
1067                         blob_unset_is_located_in_wim_resource(cur_blob);
1068                 free_blob_descriptor(cur_blob);
1069         }
1070         cur_blob = NULL;
1071
1072         if (cur_solid_rdescs) {
1073                 /* End of blob table terminated a solid run.  */
1074                 ret = finish_solid_rdescs(cur_solid_rdescs, cur_num_solid_rdescs);
1075                 cur_solid_rdescs = NULL;
1076                 if (ret)
1077                         goto out;
1078         }
1079
1080         if (wim->hdr.part_number == 1 && image_index != wim->hdr.image_count) {
1081                 WARNING("Could not find metadata resources for all images");
1082                 for (u32 i = image_index; i < wim->hdr.image_count; i++)
1083                         put_image_metadata(wim->image_metadata[i], NULL);
1084                 wim->hdr.image_count = image_index;
1085         }
1086
1087         if (num_duplicate_blobs > 0)
1088                 WARNING("Ignoring %zu duplicate blobs", num_duplicate_blobs);
1089
1090         if (num_wrong_part_blobs > 0) {
1091                 WARNING("Ignoring %zu blobs with wrong part number",
1092                         num_wrong_part_blobs);
1093         }
1094
1095         DEBUG("Done reading blob table.");
1096         wim->blob_table = table;
1097         ret = 0;
1098         goto out_free_buf;
1099
1100 oom:
1101         ERROR("Not enough memory to read blob table!");
1102         ret = WIMLIB_ERR_NOMEM;
1103 out:
1104         free_solid_rdescs(cur_solid_rdescs, cur_num_solid_rdescs);
1105         free_blob_descriptor(cur_blob);
1106         free_blob_table(table);
1107 out_free_buf:
1108         FREE(buf);
1109         return ret;
1110 }
1111
1112 static void
1113 write_blob_descriptor(struct blob_descriptor_disk *disk_entry,
1114                       const struct wim_reshdr *out_reshdr,
1115                       u16 part_number, u32 refcnt, const u8 *hash)
1116 {
1117         put_wim_reshdr(out_reshdr, &disk_entry->reshdr);
1118         disk_entry->part_number = cpu_to_le16(part_number);
1119         disk_entry->refcnt = cpu_to_le32(refcnt);
1120         copy_hash(disk_entry->hash, hash);
1121 }
1122
1123 /* Note: the list of blob descriptors must be sorted so that all entries for the
1124  * same solid resource are consecutive.  In addition, blob descriptors for
1125  * metadata resources must be in the same order as the indices of the underlying
1126  * images.  */
1127 int
1128 write_blob_table_from_blob_list(struct list_head *blob_list,
1129                                 struct filedes *out_fd,
1130                                 u16 part_number,
1131                                 struct wim_reshdr *out_reshdr,
1132                                 int write_resource_flags)
1133 {
1134         size_t table_size;
1135         struct blob_descriptor *blob;
1136         struct blob_descriptor_disk *table_buf;
1137         struct blob_descriptor_disk *table_buf_ptr;
1138         int ret;
1139         u64 prev_res_offset_in_wim = ~0ULL;
1140         u64 prev_uncompressed_size;
1141         u64 logical_offset;
1142
1143         table_size = 0;
1144         list_for_each_entry(blob, blob_list, blob_table_list) {
1145                 table_size += sizeof(struct blob_descriptor_disk);
1146
1147                 if (blob->out_reshdr.flags & WIM_RESHDR_FLAG_SOLID &&
1148                     blob->out_res_offset_in_wim != prev_res_offset_in_wim)
1149                 {
1150                         table_size += sizeof(struct blob_descriptor_disk);
1151                         prev_res_offset_in_wim = blob->out_res_offset_in_wim;
1152                 }
1153         }
1154
1155         DEBUG("Writing WIM blob table (size=%zu, offset=%"PRIu64")",
1156               table_size, out_fd->offset);
1157
1158         table_buf = MALLOC(table_size);
1159         if (table_buf == NULL) {
1160                 ERROR("Failed to allocate %zu bytes for temporary blob table",
1161                       table_size);
1162                 return WIMLIB_ERR_NOMEM;
1163         }
1164         table_buf_ptr = table_buf;
1165
1166         prev_res_offset_in_wim = ~0ULL;
1167         prev_uncompressed_size = 0;
1168         logical_offset = 0;
1169         list_for_each_entry(blob, blob_list, blob_table_list) {
1170                 if (blob->out_reshdr.flags & WIM_RESHDR_FLAG_SOLID) {
1171                         struct wim_reshdr tmp_reshdr;
1172
1173                         /* Eww.  When WIMGAPI sees multiple solid resources, it
1174                          * expects the offsets to be adjusted as if there were
1175                          * really only one solid resource.  */
1176
1177                         if (blob->out_res_offset_in_wim != prev_res_offset_in_wim) {
1178                                 /* Put the resource entry for solid resource  */
1179                                 tmp_reshdr.offset_in_wim = blob->out_res_offset_in_wim;
1180                                 tmp_reshdr.size_in_wim = blob->out_res_size_in_wim;
1181                                 tmp_reshdr.uncompressed_size = SOLID_RESOURCE_MAGIC_NUMBER;
1182                                 tmp_reshdr.flags = WIM_RESHDR_FLAG_SOLID;
1183
1184                                 write_blob_descriptor(table_buf_ptr++, &tmp_reshdr,
1185                                                       part_number, 1, zero_hash);
1186
1187                                 logical_offset += prev_uncompressed_size;
1188
1189                                 prev_res_offset_in_wim = blob->out_res_offset_in_wim;
1190                                 prev_uncompressed_size = blob->out_res_uncompressed_size;
1191                         }
1192                         tmp_reshdr = blob->out_reshdr;
1193                         tmp_reshdr.offset_in_wim += logical_offset;
1194                         write_blob_descriptor(table_buf_ptr++, &tmp_reshdr,
1195                                               part_number, blob->out_refcnt, blob->hash);
1196                 } else {
1197                         write_blob_descriptor(table_buf_ptr++, &blob->out_reshdr,
1198                                               part_number, blob->out_refcnt, blob->hash);
1199                 }
1200
1201         }
1202         wimlib_assert((u8*)table_buf_ptr - (u8*)table_buf == table_size);
1203
1204         /* Write the blob table uncompressed.  Although wimlib can handle a
1205          * compressed blob table, MS software cannot.  */
1206         ret = write_wim_resource_from_buffer(table_buf,
1207                                              table_size,
1208                                              true,
1209                                              out_fd,
1210                                              WIMLIB_COMPRESSION_TYPE_NONE,
1211                                              0,
1212                                              out_reshdr,
1213                                              NULL,
1214                                              write_resource_flags);
1215         FREE(table_buf);
1216         DEBUG("ret=%d", ret);
1217         return ret;
1218 }
1219
1220 /* Allocate a blob descriptor for the contents of the buffer, or re-use an
1221  * existing descriptor in @blob_table for an identical blob.  */
1222 struct blob_descriptor *
1223 new_blob_from_data_buffer(const void *buffer, size_t size,
1224                           struct blob_table *blob_table)
1225 {
1226         u8 hash[SHA1_HASH_SIZE];
1227         struct blob_descriptor *blob;
1228         void *buffer_copy;
1229
1230         sha1_buffer(buffer, size, hash);
1231
1232         blob = lookup_blob(blob_table, hash);
1233         if (blob)
1234                 return blob;
1235
1236         blob = new_blob_descriptor();
1237         if (!blob)
1238                 return NULL;
1239
1240         buffer_copy = memdup(buffer, size);
1241         if (!buffer_copy) {
1242                 free_blob_descriptor(blob);
1243                 return NULL;
1244         }
1245         blob_set_is_located_in_attached_buffer(blob, buffer_copy, size);
1246         copy_hash(blob->hash, hash);
1247         blob_table_insert(blob_table, blob);
1248         return blob;
1249 }
1250
1251 struct blob_descriptor *
1252 after_blob_hashed(struct blob_descriptor *blob,
1253                   struct blob_descriptor **back_ptr,
1254                   struct blob_table *blob_table)
1255 {
1256         struct blob_descriptor *duplicate_blob;
1257
1258         list_del(&blob->unhashed_list);
1259         blob->unhashed = 0;
1260
1261         /* Look for a duplicate blob  */
1262         duplicate_blob = lookup_blob(blob_table, blob->hash);
1263         if (duplicate_blob) {
1264                 /* We have a duplicate blob.  Transfer the reference counts from
1265                  * this blob to the duplicate and update the reference to this
1266                  * blob (from a stream) to point to the duplicate.  The caller
1267                  * is responsible for freeing @blob if needed.  */
1268                 wimlib_assert(duplicate_blob->size == blob->size);
1269                 duplicate_blob->refcnt += blob->refcnt;
1270                 blob->refcnt = 0;
1271                 *back_ptr = duplicate_blob;
1272                 return duplicate_blob;
1273         } else {
1274                 /* No duplicate blob, so we need to insert this blob into the
1275                  * blob table and treat it as a hashed blob.  */
1276                 blob_table_insert(blob_table, blob);
1277                 return blob;
1278         }
1279 }
1280
1281 /*
1282  * Calculate the SHA-1 message digest of a blob and move its descriptor from the
1283  * list of unhashed blobs to the blob table, possibly joining it with an
1284  * identical blob.
1285  *
1286  * @blob:
1287  *      The blob to hash
1288  * @blob_table:
1289  *      The blob table in which the blob needs to be indexed
1290  * @blob_ret:
1291  *      On success, a pointer to the resulting blob descriptor is written to
1292  *      this location.  This will be the same as @blob if it was inserted into
1293  *      the blob table, or different if a duplicate blob was found.
1294  *
1295  * Returns 0 on success; nonzero if there is an error reading the blob data.
1296  */
1297 int
1298 hash_unhashed_blob(struct blob_descriptor *blob, struct blob_table *blob_table,
1299                    struct blob_descriptor **blob_ret)
1300 {
1301         struct blob_descriptor **back_ptr;
1302         int ret;
1303
1304         back_ptr = retrieve_pointer_to_unhashed_blob(blob);
1305
1306         ret = sha1_blob(blob);
1307         if (ret)
1308                 return ret;
1309
1310         *blob_ret = after_blob_hashed(blob, back_ptr, blob_table);
1311         return 0;
1312 }
1313
1314 void
1315 blob_to_wimlib_resource_entry(const struct blob_descriptor *blob,
1316                               struct wimlib_resource_entry *wentry)
1317 {
1318         memset(wentry, 0, sizeof(*wentry));
1319
1320         wentry->uncompressed_size = blob->size;
1321         if (blob->blob_location == BLOB_IN_WIM) {
1322                 unsigned res_flags = blob->rdesc->flags;
1323
1324                 wentry->part_number = blob->rdesc->wim->hdr.part_number;
1325                 if (res_flags & WIM_RESHDR_FLAG_SOLID) {
1326                         wentry->offset = blob->offset_in_res;
1327                 } else {
1328                         wentry->compressed_size = blob->rdesc->size_in_wim;
1329                         wentry->offset = blob->rdesc->offset_in_wim;
1330                 }
1331                 wentry->raw_resource_offset_in_wim = blob->rdesc->offset_in_wim;
1332                 wentry->raw_resource_compressed_size = blob->rdesc->size_in_wim;
1333                 wentry->raw_resource_uncompressed_size = blob->rdesc->uncompressed_size;
1334
1335                 wentry->is_compressed = (res_flags & WIM_RESHDR_FLAG_COMPRESSED) != 0;
1336                 wentry->is_free = (res_flags & WIM_RESHDR_FLAG_FREE) != 0;
1337                 wentry->is_spanned = (res_flags & WIM_RESHDR_FLAG_SPANNED) != 0;
1338                 wentry->packed = (res_flags & WIM_RESHDR_FLAG_SOLID) != 0;
1339         }
1340         if (!blob->unhashed)
1341                 copy_hash(wentry->sha1_hash, blob->hash);
1342         wentry->reference_count = blob->refcnt;
1343         wentry->is_metadata = blob->is_metadata;
1344 }
1345
1346 struct iterate_blob_context {
1347         wimlib_iterate_lookup_table_callback_t cb;
1348         void *user_ctx;
1349 };
1350
1351 static int
1352 do_iterate_blob(struct blob_descriptor *blob, void *_ctx)
1353 {
1354         struct iterate_blob_context *ctx = _ctx;
1355         struct wimlib_resource_entry entry;
1356
1357         blob_to_wimlib_resource_entry(blob, &entry);
1358         return (*ctx->cb)(&entry, ctx->user_ctx);
1359 }
1360
1361 /* API function documented in wimlib.h  */
1362 WIMLIBAPI int
1363 wimlib_iterate_lookup_table(WIMStruct *wim, int flags,
1364                             wimlib_iterate_lookup_table_callback_t cb,
1365                             void *user_ctx)
1366 {
1367         if (flags != 0)
1368                 return WIMLIB_ERR_INVALID_PARAM;
1369
1370         struct iterate_blob_context ctx = {
1371                 .cb = cb,
1372                 .user_ctx = user_ctx,
1373         };
1374         if (wim_has_metadata(wim)) {
1375                 int ret;
1376                 for (int i = 0; i < wim->hdr.image_count; i++) {
1377                         struct blob_descriptor *blob;
1378                         struct wim_image_metadata *imd = wim->image_metadata[i];
1379
1380                         ret = do_iterate_blob(imd->metadata_blob, &ctx);
1381                         if (ret)
1382                                 return ret;
1383                         image_for_each_unhashed_blob(blob, imd) {
1384                                 ret = do_iterate_blob(blob, &ctx);
1385                                 if (ret)
1386                                         return ret;
1387                         }
1388                 }
1389         }
1390         return for_blob_in_table(wim->blob_table, do_iterate_blob, &ctx);
1391 }