read_wim_lookup_table(): Cleanup
[wimlib] / src / lookup_table.c
1 /*
2  * lookup_table.c
3  *
4  * Lookup table, implemented as a hash table, that maps SHA1 message digests to
5  * data streams; plus code to read and write the corresponding on-disk data.
6  */
7
8 /*
9  * Copyright (C) 2012, 2013 Eric Biggers
10  *
11  * This file is part of wimlib, a library for working with WIM files.
12  *
13  * wimlib is free software; you can redistribute it and/or modify it under the
14  * terms of the GNU General Public License as published by the Free
15  * Software Foundation; either version 3 of the License, or (at your option)
16  * any later version.
17  *
18  * wimlib is distributed in the hope that it will be useful, but WITHOUT ANY
19  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
20  * A PARTICULAR PURPOSE. See the GNU General Public License for more
21  * details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with wimlib; if not, see http://www.gnu.org/licenses/.
25  */
26
27 #ifdef HAVE_CONFIG_H
28 #  include "config.h"
29 #endif
30
31 #include "wimlib/assert.h"
32 #include "wimlib/endianness.h"
33 #include "wimlib/error.h"
34 #include "wimlib/lookup_table.h"
35 #include "wimlib/metadata.h"
36 #include "wimlib/ntfs_3g.h"
37 #include "wimlib/resource.h"
38 #include "wimlib/util.h"
39 #include "wimlib/write.h"
40
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h> /* for unlink()  */
44
45 /* WIM lookup table:
46  *
47  * This is a logical mapping from SHA1 message digests to the data streams
48  * contained in a WIM.
49  *
50  * Here it is implemented as a hash table.
51  *
52  * Note: Everything will break horribly if there is a SHA1 collision.
53  */
54 struct wim_lookup_table {
55         struct hlist_head *array;
56         size_t num_entries;
57         size_t capacity;
58 };
59
60 struct wim_lookup_table *
61 new_lookup_table(size_t capacity)
62 {
63         struct wim_lookup_table *table;
64         struct hlist_head *array;
65
66         table = MALLOC(sizeof(struct wim_lookup_table));
67         if (table == NULL)
68                 goto oom;
69
70         array = CALLOC(capacity, sizeof(array[0]));
71         if (array == NULL) {
72                 FREE(table);
73                 goto oom;
74         }
75
76         table->num_entries = 0;
77         table->capacity = capacity;
78         table->array = array;
79         return table;
80
81 oom:
82         ERROR("Failed to allocate memory for lookup table "
83               "with capacity %zu", capacity);
84         return NULL;
85 }
86
87 static int
88 do_free_lookup_table_entry(struct wim_lookup_table_entry *entry, void *ignore)
89 {
90         free_lookup_table_entry(entry);
91         return 0;
92 }
93
94 void
95 free_lookup_table(struct wim_lookup_table *table)
96 {
97         DEBUG("Freeing lookup table.");
98         if (table == NULL)
99                 return;
100
101         if (table->array) {
102                 for_lookup_table_entry(table,
103                                        do_free_lookup_table_entry,
104                                        NULL);
105                 FREE(table->array);
106         }
107         FREE(table);
108 }
109
110 struct wim_lookup_table_entry *
111 new_lookup_table_entry(void)
112 {
113         struct wim_lookup_table_entry *lte;
114
115         lte = CALLOC(1, sizeof(struct wim_lookup_table_entry));
116         if (lte == NULL)
117                 return NULL;
118
119         lte->refcnt = 1;
120
121         /* lte->resource_location = RESOURCE_NONEXISTENT  */
122         BUILD_BUG_ON(RESOURCE_NONEXISTENT != 0);
123
124         return lte;
125 }
126
127 struct wim_lookup_table_entry *
128 clone_lookup_table_entry(const struct wim_lookup_table_entry *old)
129 {
130         struct wim_lookup_table_entry *new;
131
132         new = memdup(old, sizeof(struct wim_lookup_table_entry));
133         if (new == NULL)
134                 return NULL;
135
136         new->extracted_file = NULL;
137         switch (new->resource_location) {
138         case RESOURCE_IN_WIM:
139                 list_add(&new->rspec_node, &new->rspec->stream_list);
140                 break;
141
142         case RESOURCE_IN_FILE_ON_DISK:
143 #ifdef __WIN32__
144         case RESOURCE_WIN32_ENCRYPTED:
145 #endif
146 #ifdef WITH_FUSE
147         case RESOURCE_IN_STAGING_FILE:
148                 BUILD_BUG_ON((void*)&old->file_on_disk !=
149                              (void*)&old->staging_file_name);
150 #endif
151                 new->file_on_disk = TSTRDUP(old->file_on_disk);
152                 if (new->file_on_disk == NULL)
153                         goto out_free;
154                 break;
155         case RESOURCE_IN_ATTACHED_BUFFER:
156                 new->attached_buffer = memdup(old->attached_buffer, old->size);
157                 if (new->attached_buffer == NULL)
158                         goto out_free;
159                 break;
160 #ifdef WITH_NTFS_3G
161         case RESOURCE_IN_NTFS_VOLUME:
162                 if (old->ntfs_loc) {
163                         struct ntfs_location *loc;
164                         loc = memdup(old->ntfs_loc, sizeof(struct ntfs_location));
165                         if (loc == NULL)
166                                 goto out_free;
167                         loc->path = NULL;
168                         loc->stream_name = NULL;
169                         new->ntfs_loc = loc;
170                         loc->path = STRDUP(old->ntfs_loc->path);
171                         if (loc->path == NULL)
172                                 goto out_free;
173                         if (loc->stream_name_nchars != 0) {
174                                 loc->stream_name = memdup(old->ntfs_loc->stream_name,
175                                                           loc->stream_name_nchars * 2);
176                                 if (loc->stream_name == NULL)
177                                         goto out_free;
178                         }
179                 }
180                 break;
181 #endif
182         default:
183                 break;
184         }
185         return new;
186
187 out_free:
188         free_lookup_table_entry(new);
189         return NULL;
190 }
191
192 void
193 lte_put_resource(struct wim_lookup_table_entry *lte)
194 {
195         switch (lte->resource_location) {
196         case RESOURCE_IN_WIM:
197                 list_del(&lte->rspec_node);
198                 if (list_empty(&lte->rspec->stream_list))
199                         FREE(lte->rspec);
200                 break;
201         case RESOURCE_IN_FILE_ON_DISK:
202 #ifdef __WIN32__
203         case RESOURCE_WIN32_ENCRYPTED:
204 #endif
205 #ifdef WITH_FUSE
206         case RESOURCE_IN_STAGING_FILE:
207                 BUILD_BUG_ON((void*)&lte->file_on_disk !=
208                              (void*)&lte->staging_file_name);
209 #endif
210         case RESOURCE_IN_ATTACHED_BUFFER:
211                 BUILD_BUG_ON((void*)&lte->file_on_disk !=
212                              (void*)&lte->attached_buffer);
213                 FREE(lte->file_on_disk);
214                 break;
215 #ifdef WITH_NTFS_3G
216         case RESOURCE_IN_NTFS_VOLUME:
217                 if (lte->ntfs_loc) {
218                         FREE(lte->ntfs_loc->path);
219                         FREE(lte->ntfs_loc->stream_name);
220                         FREE(lte->ntfs_loc);
221                 }
222                 break;
223 #endif
224         default:
225                 break;
226         }
227 }
228
229 void
230 free_lookup_table_entry(struct wim_lookup_table_entry *lte)
231 {
232         if (lte) {
233                 lte_put_resource(lte);
234                 FREE(lte);
235         }
236 }
237
238 /* Should this stream be retained even if it has no references?  */
239 static bool
240 should_retain_lte(const struct wim_lookup_table_entry *lte)
241 {
242         return lte->resource_location == RESOURCE_IN_WIM;
243 }
244
245 static void
246 finalize_lte(struct wim_lookup_table_entry *lte)
247 {
248         if (!should_retain_lte(lte))
249                 free_lookup_table_entry(lte);
250 }
251
252 /*
253  * Decrements the reference count for the lookup table entry @lte, which must be
254  * inserted in the stream lookup table @table.
255  *
256  * If the reference count reaches 0, this may cause @lte to be destroyed.
257  * However, we may retain entries with 0 reference count.  This does not affect
258  * correctness, but it prevents the entries for valid streams in a WIM archive,
259  * which will continue to be present after appending to the file, from being
260  * lost merely because we dropped all references to them.
261  */
262 void
263 lte_decrement_refcnt(struct wim_lookup_table_entry *lte,
264                      struct wim_lookup_table *table)
265 {
266         wimlib_assert(lte->refcnt != 0);
267
268         if (--lte->refcnt == 0) {
269                 if (lte->unhashed) {
270                         list_del(&lte->unhashed_list);
271                 #ifdef WITH_FUSE
272                         /* If the stream has been extracted to a staging file
273                          * for a FUSE mount, unlink the staging file.  (Note
274                          * that there still may be open file descriptors to it.)
275                          * */
276                         if (lte->resource_location == RESOURCE_IN_STAGING_FILE)
277                                 unlink(lte->staging_file_name);
278                 #endif
279                 } else {
280                         if (!should_retain_lte(lte))
281                                 lookup_table_unlink(table, lte);
282                 }
283
284                 /* If FUSE mounts are enabled, we don't actually free the entry
285                  * until the last file descriptor has been closed by
286                  * lte_decrement_num_opened_fds().  */
287 #ifdef WITH_FUSE
288                 if (lte->num_opened_fds == 0)
289 #endif
290                         finalize_lte(lte);
291         }
292 }
293
294 #ifdef WITH_FUSE
295 void
296 lte_decrement_num_opened_fds(struct wim_lookup_table_entry *lte)
297 {
298         wimlib_assert(lte->num_opened_fds != 0);
299
300         if (--lte->num_opened_fds == 0 && lte->refcnt == 0)
301                 finalize_lte(lte);
302 }
303 #endif
304
305 static void
306 lookup_table_insert_raw(struct wim_lookup_table *table,
307                         struct wim_lookup_table_entry *lte)
308 {
309         size_t i = lte->hash_short % table->capacity;
310
311         hlist_add_head(&lte->hash_list, &table->array[i]);
312 }
313
314 static void
315 enlarge_lookup_table(struct wim_lookup_table *table)
316 {
317         size_t old_capacity, new_capacity;
318         struct hlist_head *old_array, *new_array;
319         struct wim_lookup_table_entry *lte;
320         struct hlist_node *cur, *tmp;
321         size_t i;
322
323         old_capacity = table->capacity;
324         new_capacity = old_capacity * 2;
325         new_array = CALLOC(new_capacity, sizeof(struct hlist_head));
326         if (new_array == NULL)
327                 return;
328         old_array = table->array;
329         table->array = new_array;
330         table->capacity = new_capacity;
331
332         for (i = 0; i < old_capacity; i++) {
333                 hlist_for_each_entry_safe(lte, cur, tmp, &old_array[i], hash_list) {
334                         hlist_del(&lte->hash_list);
335                         lookup_table_insert_raw(table, lte);
336                 }
337         }
338         FREE(old_array);
339 }
340
341 /* Inserts an entry into the lookup table.  */
342 void
343 lookup_table_insert(struct wim_lookup_table *table,
344                     struct wim_lookup_table_entry *lte)
345 {
346         lookup_table_insert_raw(table, lte);
347         if (++table->num_entries > table->capacity)
348                 enlarge_lookup_table(table);
349 }
350
351 /* Unlinks a lookup table entry from the table; does not free it.  */
352 void
353 lookup_table_unlink(struct wim_lookup_table *table,
354                     struct wim_lookup_table_entry *lte)
355 {
356         wimlib_assert(!lte->unhashed);
357         wimlib_assert(table->num_entries != 0);
358
359         hlist_del(&lte->hash_list);
360         table->num_entries--;
361 }
362
363 /* Given a SHA1 message digest, return the corresponding entry in the WIM's
364  * lookup table, or NULL if there is none.  */
365 struct wim_lookup_table_entry *
366 lookup_stream(const struct wim_lookup_table *table, const u8 hash[])
367 {
368         size_t i;
369         struct wim_lookup_table_entry *lte;
370         struct hlist_node *pos;
371
372         i = *(size_t*)hash % table->capacity;
373         hlist_for_each_entry(lte, pos, &table->array[i], hash_list)
374                 if (hashes_equal(hash, lte->hash))
375                         return lte;
376         return NULL;
377 }
378
379 /* Calls a function on all the entries in the WIM lookup table.  Stop early and
380  * return nonzero if any call to the function returns nonzero. */
381 int
382 for_lookup_table_entry(struct wim_lookup_table *table,
383                        int (*visitor)(struct wim_lookup_table_entry *, void *),
384                        void *arg)
385 {
386         struct wim_lookup_table_entry *lte;
387         struct hlist_node *pos, *tmp;
388         int ret;
389
390         for (size_t i = 0; i < table->capacity; i++) {
391                 hlist_for_each_entry_safe(lte, pos, tmp, &table->array[i],
392                                           hash_list)
393                 {
394                         ret = visitor(lte, arg);
395                         if (ret)
396                                 return ret;
397                 }
398         }
399         return 0;
400 }
401
402 /* qsort() callback that sorts streams (represented by `struct
403  * wim_lookup_table_entry's) into an order optimized for reading.
404  *
405  * Sorting is done primarily by resource location, then secondarily by a
406  * per-resource location order.  For example, resources in WIM files are sorted
407  * primarily by part number, then secondarily by offset, as to implement optimal
408  * reading of either a standalone or split WIM.  */
409 static int
410 cmp_streams_by_sequential_order(const void *p1, const void *p2)
411 {
412         const struct wim_lookup_table_entry *lte1, *lte2;
413         int v;
414         WIMStruct *wim1, *wim2;
415
416         lte1 = *(const struct wim_lookup_table_entry**)p1;
417         lte2 = *(const struct wim_lookup_table_entry**)p2;
418
419         v = (int)lte1->resource_location - (int)lte2->resource_location;
420
421         /* Different resource locations?  */
422         if (v)
423                 return v;
424
425         switch (lte1->resource_location) {
426         case RESOURCE_IN_WIM:
427                 wim1 = lte1->rspec->wim;
428                 wim2 = lte2->rspec->wim;
429
430                 /* Different (possibly split) WIMs?  */
431                 if (wim1 != wim2) {
432                         v = memcmp(wim1->hdr.guid, wim2->hdr.guid, WIM_GID_LEN);
433                         if (v)
434                                 return v;
435                 }
436
437                 /* Different part numbers in the same WIM?  */
438                 v = (int)wim1->hdr.part_number - (int)wim2->hdr.part_number;
439                 if (v)
440                         return v;
441
442                 if (lte1->rspec->offset_in_wim != lte2->rspec->offset_in_wim)
443                         return cmp_u64(lte1->rspec->offset_in_wim,
444                                        lte2->rspec->offset_in_wim);
445
446                 return cmp_u64(lte1->offset_in_res, lte2->offset_in_res);
447
448         case RESOURCE_IN_FILE_ON_DISK:
449 #ifdef WITH_FUSE
450         case RESOURCE_IN_STAGING_FILE:
451 #endif
452 #ifdef __WIN32__
453         case RESOURCE_WIN32_ENCRYPTED:
454 #endif
455                 /* Compare files by path: just a heuristic that will place files
456                  * in the same directory next to each other.  */
457                 return tstrcmp(lte1->file_on_disk, lte2->file_on_disk);
458 #ifdef WITH_NTFS_3G
459         case RESOURCE_IN_NTFS_VOLUME:
460                 return tstrcmp(lte1->ntfs_loc->path, lte2->ntfs_loc->path);
461 #endif
462         default:
463                 /* No additional sorting order defined for this resource
464                  * location (e.g. RESOURCE_IN_ATTACHED_BUFFER); simply compare
465                  * everything equal to each other.  */
466                 return 0;
467         }
468 }
469
470 int
471 sort_stream_list(struct list_head *stream_list,
472                  size_t list_head_offset,
473                  int (*compar)(const void *, const void*))
474 {
475         struct list_head *cur;
476         struct wim_lookup_table_entry **array;
477         size_t i;
478         size_t array_size;
479         size_t num_streams = 0;
480
481         list_for_each(cur, stream_list)
482                 num_streams++;
483
484         if (num_streams <= 1)
485                 return 0;
486
487         array_size = num_streams * sizeof(array[0]);
488         array = MALLOC(array_size);
489         if (array == NULL)
490                 return WIMLIB_ERR_NOMEM;
491
492         cur = stream_list->next;
493         for (i = 0; i < num_streams; i++) {
494                 array[i] = (struct wim_lookup_table_entry*)((u8*)cur -
495                                                             list_head_offset);
496                 cur = cur->next;
497         }
498
499         qsort(array, num_streams, sizeof(array[0]), compar);
500
501         INIT_LIST_HEAD(stream_list);
502         for (i = 0; i < num_streams; i++) {
503                 list_add_tail((struct list_head*)
504                                ((u8*)array[i] + list_head_offset),
505                               stream_list);
506         }
507         FREE(array);
508         return 0;
509 }
510
511 /* Sort the specified list of streams in an order optimized for reading.  */
512 int
513 sort_stream_list_by_sequential_order(struct list_head *stream_list,
514                                      size_t list_head_offset)
515 {
516         return sort_stream_list(stream_list, list_head_offset,
517                                 cmp_streams_by_sequential_order);
518 }
519
520
521 static int
522 add_lte_to_array(struct wim_lookup_table_entry *lte,
523                  void *_pp)
524 {
525         struct wim_lookup_table_entry ***pp = _pp;
526         *(*pp)++ = lte;
527         return 0;
528 }
529
530 /* Iterate through the lookup table entries, but first sort them by stream
531  * offset in the WIM.  Caution: this is intended to be used when the stream
532  * offset field has actually been set. */
533 int
534 for_lookup_table_entry_pos_sorted(struct wim_lookup_table *table,
535                                   int (*visitor)(struct wim_lookup_table_entry *,
536                                                  void *),
537                                   void *arg)
538 {
539         struct wim_lookup_table_entry **lte_array, **p;
540         size_t num_streams = table->num_entries;
541         int ret;
542
543         lte_array = MALLOC(num_streams * sizeof(lte_array[0]));
544         if (!lte_array)
545                 return WIMLIB_ERR_NOMEM;
546         p = lte_array;
547         for_lookup_table_entry(table, add_lte_to_array, &p);
548
549         wimlib_assert(p == lte_array + num_streams);
550
551         qsort(lte_array, num_streams, sizeof(lte_array[0]),
552               cmp_streams_by_sequential_order);
553         ret = 0;
554         for (size_t i = 0; i < num_streams; i++) {
555                 ret = visitor(lte_array[i], arg);
556                 if (ret)
557                         break;
558         }
559         FREE(lte_array);
560         return ret;
561 }
562
563 /* On-disk format of a WIM lookup table entry (stream entry). */
564 struct wim_lookup_table_entry_disk {
565         /* Size, offset, and flags of the stream.  */
566         struct wim_reshdr_disk reshdr;
567
568         /* Which part of the split WIM this stream is in; indexed from 1. */
569         le16 part_number;
570
571         /* Reference count of this stream over all WIM images. */
572         le32 refcnt;
573
574         /* SHA1 message digest of the uncompressed data of this stream, or
575          * optionally all zeroes if this stream is of zero length. */
576         u8 hash[SHA1_HASH_SIZE];
577 } _packed_attribute;
578
579 #define WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE 50
580
581 static int
582 cmp_streams_by_offset_in_res(const void *p1, const void *p2)
583 {
584         const struct wim_lookup_table_entry *lte1, *lte2;
585
586         lte1 = *(const struct wim_lookup_table_entry**)p1;
587         lte2 = *(const struct wim_lookup_table_entry**)p2;
588
589         return cmp_u64(lte1->offset_in_res, lte2->offset_in_res);
590 }
591
592 /* Validate the size and location of a WIM resource.  */
593 static int
594 validate_resource(struct wim_resource_spec *rspec)
595 {
596         struct wim_lookup_table_entry *lte;
597         bool out_of_order;
598         u64 expected_next_offset;
599         int ret;
600
601         /* Verify that the resource itself has a valid offset and size.  */
602         if (rspec->offset_in_wim + rspec->size_in_wim < rspec->size_in_wim)
603                 goto invalid_due_to_overflow;
604
605         /* Verify that each stream in the resource has a valid offset and size.
606          */
607         expected_next_offset = 0;
608         out_of_order = false;
609         list_for_each_entry(lte, &rspec->stream_list, rspec_node) {
610                 if (lte->offset_in_res + lte->size < lte->size ||
611                     lte->offset_in_res + lte->size > rspec->uncompressed_size)
612                         goto invalid_due_to_overflow;
613
614                 if (lte->offset_in_res >= expected_next_offset)
615                         expected_next_offset = lte->offset_in_res + lte->size;
616                 else
617                         out_of_order = true;
618         }
619
620         /* If the streams were not located at strictly increasing positions (not
621          * allowing for overlap), sort them.  Then make sure that none overlap.
622          */
623         if (out_of_order) {
624                 ret = sort_stream_list(&rspec->stream_list,
625                                        offsetof(struct wim_lookup_table_entry,
626                                                 rspec_node),
627                                        cmp_streams_by_offset_in_res);
628                 if (ret)
629                         return ret;
630
631                 expected_next_offset = 0;
632                 list_for_each_entry(lte, &rspec->stream_list, rspec_node) {
633                         if (lte->offset_in_res >= expected_next_offset)
634                                 expected_next_offset = lte->offset_in_res + lte->size;
635                         else
636                                 goto invalid_due_to_overlap;
637                 }
638         }
639
640         return 0;
641
642 invalid_due_to_overflow:
643         ERROR("Invalid resource entry (offset overflow)");
644         return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
645
646 invalid_due_to_overlap:
647         ERROR("Invalid resource entry (streams in packed resource overlap)");
648         return WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
649 }
650
651 /* Validate the resource, or free it if unused.  */
652 static int
653 finish_resource(struct wim_resource_spec *rspec)
654 {
655         if (!list_empty(&rspec->stream_list)) {
656                 /* This resource contains at least one stream.  */
657                 return validate_resource(rspec);
658         } else {
659                 /* No streams are in this resource.  Get rid of it.  */
660                 FREE(rspec);
661                 return 0;
662         }
663 }
664
665 /*
666  * Reads the lookup table from a WIM file.  Each entry specifies a stream that
667  * the WIM file contains, along with its location and SHA1 message digest.
668  *
669  * Saves lookup table entries for non-metadata streams in a hash table, and
670  * saves the metadata entry for each image in a special per-image location (the
671  * image_metadata array).
672  *
673  * Return values:
674  *      WIMLIB_ERR_SUCCESS (0)
675  *      WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY
676  *      WIMLIB_ERR_RESOURCE_NOT_FOUND
677  *
678  *      Or an error code caused by failure to read the lookup table into memory.
679  */
680 int
681 read_wim_lookup_table(WIMStruct *wim)
682 {
683         int ret;
684         size_t num_entries;
685         void *buf = NULL;
686         struct wim_lookup_table *table = NULL;
687         struct wim_lookup_table_entry *cur_entry = NULL;
688         struct wim_resource_spec *cur_rspec = NULL;
689         size_t num_duplicate_entries = 0;
690         size_t num_wrong_part_entries = 0;
691         u32 image_index = 0;
692
693         DEBUG("Reading lookup table.");
694
695         /* Sanity check: lookup table entries are 50 bytes each.  */
696         BUILD_BUG_ON(sizeof(struct wim_lookup_table_entry_disk) !=
697                      WIM_LOOKUP_TABLE_ENTRY_DISK_SIZE);
698
699         /* Calculate the number of entries in the lookup table.  */
700         num_entries = wim->hdr.lookup_table_reshdr.uncompressed_size /
701                       sizeof(struct wim_lookup_table_entry_disk);
702
703         /* Read the lookup table into a buffer.  */
704         ret = wim_reshdr_to_data(&wim->hdr.lookup_table_reshdr, wim, &buf);
705         if (ret)
706                 goto out;
707
708         /* Allocate a hash table to map SHA1 message digests into stream
709          * specifications.  This is the in-memory "lookup table".  */
710         table = new_lookup_table(num_entries * 2 + 1);
711         if (!table)
712                 goto oom;
713
714         /* Allocate and initalize stream entries ('struct
715          * wim_lookup_table_entry's) from the raw lookup table buffer.  Each of
716          * these entries will point to a 'struct wim_resource_spec' that
717          * describes the underlying resource.  In WIMs with version number
718          * WIM_VERSION_PACKED_STREAMS, a resource may contain multiple streams.
719          */
720         for (size_t i = 0; i < num_entries; i++) {
721                 const struct wim_lookup_table_entry_disk *disk_entry =
722                         &((const struct wim_lookup_table_entry_disk*)buf)[i];
723                 struct wim_reshdr reshdr;
724                 u16 part_number;
725                 struct wim_lookup_table_entry *duplicate_entry;
726
727                 /* Get the resource header  */
728                 get_wim_reshdr(&disk_entry->reshdr, &reshdr);
729
730                 DEBUG("reshdr: size_in_wim=%"PRIu64", "
731                       "uncompressed_size=%"PRIu64", "
732                       "offset_in_wim=%"PRIu64", "
733                       "flags=0x%02x\n",
734                       reshdr.size_in_wim, reshdr.uncompressed_size,
735                       reshdr.offset_in_wim, reshdr.flags);
736
737                 /* Ignore PACKED_STREAMS flag if it isn't supposed to be used in
738                  * this WIM version  */
739                 if (wim->hdr.wim_version == WIM_VERSION_DEFAULT)
740                         reshdr.flags &= ~WIM_RESHDR_FLAG_PACKED_STREAMS;
741
742                 /* Allocate a 'struct wim_lookup_table_entry'  */
743                 cur_entry = new_lookup_table_entry();
744                 if (!cur_entry)
745                         goto oom;
746
747                 /* Get the part number, reference count, and hash.  */
748                 part_number = le16_to_cpu(disk_entry->part_number);
749                 cur_entry->refcnt = le32_to_cpu(disk_entry->refcnt);
750                 copy_hash(cur_entry->hash, disk_entry->hash);
751
752                 /* Verify that the part number matches that of the underlying
753                  * WIM file.  */
754                 if (part_number != wim->hdr.part_number) {
755                         num_wrong_part_entries++;
756                         goto free_cur_entry_and_continue;
757                 }
758
759                 /* If resource is uncompressed, check for (unexpected) size
760                  * mismatch.  */
761                 if (!(reshdr.flags & (WIM_RESHDR_FLAG_PACKED_STREAMS |
762                                       WIM_RESHDR_FLAG_COMPRESSED))) {
763                         if (reshdr.uncompressed_size != reshdr.size_in_wim) {
764                                 /* So ... This is an uncompressed resource, but
765                                  * its uncompressed size is NOT the same as its
766                                  * "compressed" size (size_in_wim).  What to do
767                                  * with it?
768                                  *
769                                  * Based on a simple test, WIMGAPI seems to
770                                  * handle this as follows:
771                                  *
772                                  * if (size_in_wim > uncompressed_size) {
773                                  *      Ignore uncompressed_size; use
774                                  *      size_in_wim instead.
775                                  * } else {
776                                  *      Honor uncompressed_size, but treat the
777                                  *      part of the file data above size_in_wim
778                                  *      as all zeros.
779                                  * }
780                                  *
781                                  * So we will do the same.
782                                  */
783                                 if (reshdr.size_in_wim > reshdr.uncompressed_size)
784                                         reshdr.uncompressed_size = reshdr.size_in_wim;
785                         }
786                 }
787
788                 /*
789                  * Possibly start a new resource.
790                  *
791                  * We need to start a new resource if:
792                  *
793                  * - There is no previous resource (cur_rspec).
794                  *
795                  *   OR
796                  *
797                  * - The resource header did not have PACKED_STREAMS set, so it
798                  *   specifies a new, single-stream resource.
799                  *
800                  *   OR
801                  *
802                  * - The resource header had PACKED_STREAMS set, and it's a
803                  *   special entry that specifies the resource itself as opposed
804                  *   to a stream, and we already encountered one such entry in
805                  *   the current resource.  We will interpret this as the
806                  *   beginning of a new packed resource.  (However, note that
807                  *   wimlib does not currently allow create WIMs with multiple
808                  *   packed resources, as to remain compatible with WIMGAPI.)
809                  */
810                 if (likely(!cur_rspec) ||
811                     !(reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) ||
812                       (reshdr.uncompressed_size == WIM_PACK_MAGIC_NUMBER &&
813                        cur_rspec->size_in_wim != 0))
814                 {
815                         /* Finish previous resource (if existent)  */
816                         if (cur_rspec) {
817                                 ret = finish_resource(cur_rspec);
818                                 cur_rspec = NULL;
819                                 if (ret)
820                                         goto out;
821                         }
822
823                         /* Allocate the resource specification and initialize it
824                          * with values from the current stream entry.  */
825                         cur_rspec = MALLOC(sizeof(*cur_rspec));
826                         if (!cur_rspec)
827                                 goto oom;
828
829                         wim_res_hdr_to_spec(&reshdr, wim, cur_rspec);
830
831                         /* If this is a packed run, the current stream entry may
832                          * specify a stream within the resource, and not the
833                          * resource itself.  Zero possibly irrelevant data until
834                          * it is read for certain.  */
835                         if (reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
836                                 cur_rspec->size_in_wim = 0;
837                                 cur_rspec->uncompressed_size = 0;
838                                 cur_rspec->offset_in_wim = 0;
839                         }
840                 }
841
842                 /* Now cur_rspec != NULL.  */
843
844                 /* Checked for packed resource specification.  */
845                 if (unlikely((reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) &&
846                              reshdr.uncompressed_size == WIM_PACK_MAGIC_NUMBER))
847                 {
848                         /* Found the specification for the packed resource.
849                          * Transfer the values to the `struct
850                          * wim_resource_spec', and discard the current stream
851                          * since this lookup table entry did not, in fact,
852                          * correspond to a "stream".
853                          */
854
855                         /* Uncompressed size of the resource pack is actually
856                          * stored in the header of the resource itself.  Read
857                          * it, and also grab the chunk size and compression type
858                          * (which are not necessarily the defaults from the WIM
859                          * header).  */
860                         struct alt_chunk_table_header_disk hdr;
861
862                         ret = full_pread(&wim->in_fd, &hdr,
863                                          sizeof(hdr), reshdr.offset_in_wim);
864                         if (ret)
865                                 goto out;
866
867                         cur_rspec->uncompressed_size = le64_to_cpu(hdr.res_usize);
868                         cur_rspec->offset_in_wim = reshdr.offset_in_wim;
869                         cur_rspec->size_in_wim = reshdr.size_in_wim;
870                         cur_rspec->flags = reshdr.flags;
871
872                         /* Compression format numbers must be the same as in
873                          * WIMGAPI to be compatible here.  */
874                         BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_NONE != 0);
875                         BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_XPRESS != 1);
876                         BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_LZX != 2);
877                         BUILD_BUG_ON(WIMLIB_COMPRESSION_TYPE_LZMS != 3);
878                         cur_rspec->compression_type = le32_to_cpu(hdr.compression_format);
879
880                         cur_rspec->chunk_size = le32_to_cpu(hdr.chunk_size);
881
882                         DEBUG("Full pack is %"PRIu64" compressed bytes "
883                               "at file offset %"PRIu64" (flags 0x%02x)",
884                               cur_rspec->size_in_wim,
885                               cur_rspec->offset_in_wim,
886                               cur_rspec->flags);
887                         goto free_cur_entry_and_continue;
888                 }
889
890                 /* Ignore streams with zero hash.  */
891                 if (is_zero_hash(cur_entry->hash))
892                         goto free_cur_entry_and_continue;
893
894                 if (reshdr.flags & WIM_RESHDR_FLAG_METADATA) {
895                         /* Lookup table entry for a metadata resource.  */
896
897                         /* Metadata entries with no references must be ignored;
898                          * see, for example, the WinPE WIMs from the WAIK v2.1.
899                          */
900                         if (cur_entry->refcnt == 0)
901                                 goto free_cur_entry_and_continue;
902
903                         if (cur_entry->refcnt != 1) {
904                                 ERROR("Found metadata resource with refcnt != 1");
905                                 ret = WIMLIB_ERR_INVALID_LOOKUP_TABLE_ENTRY;
906                                 goto out;
907                         }
908
909                         if (wim->hdr.part_number != 1) {
910                                 WARNING("Ignoring metadata resource found in a "
911                                         "non-first part of the split WIM");
912                                 goto free_cur_entry_and_continue;
913                         }
914                         if (image_index == wim->hdr.image_count) {
915                                 WARNING("Found more metadata resources than images");
916                                 goto free_cur_entry_and_continue;
917                         }
918
919                         /* Notice very carefully:  We are assigning the metadata
920                          * resources in the exact order mirrored by their lookup
921                          * table entries on disk, which is the behavior of
922                          * Microsoft's software.  In particular, this overrides
923                          * the actual locations of the metadata resources
924                          * themselves in the WIM file as well as any information
925                          * written in the XML data.  */
926                         DEBUG("Found metadata resource for image %"PRIu32" at "
927                               "offset %"PRIu64".",
928                               image_index + 1,
929                               reshdr.offset_in_wim);
930
931                         wim->image_metadata[image_index++]->metadata_lte = cur_entry;
932                 } else {
933                         /* Lookup table entry for a stream that is not a metadata
934                          * resource.  */
935
936                         /* Ignore this stream if it's a duplicate.  */
937                         duplicate_entry = lookup_stream(table, cur_entry->hash);
938                         if (duplicate_entry) {
939                                 num_duplicate_entries++;
940                                 goto free_cur_entry_and_continue;
941                         }
942
943                         /* Insert the stream into the lookup table (keyed by its
944                          * SHA1 message digest).  */
945                         lookup_table_insert(table, cur_entry);
946                 }
947
948                 /* Add the stream to the current resource specification.  */
949                 lte_bind_wim_resource_spec(cur_entry, cur_rspec);
950                 if (reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
951                         /* In packed runs, the offset field is used for
952                          * in-resource offset, not the in-WIM offset, and the
953                          * size field is used for the uncompressed size, not the
954                          * compressed size.  */
955                         cur_entry->offset_in_res = reshdr.offset_in_wim;
956                         cur_entry->size = reshdr.size_in_wim;
957                         cur_entry->flags = reshdr.flags;
958                         /* cur_rspec stays the same  */
959
960                 } else {
961                         /* Normal case: The stream corresponds one-to-one with
962                          * the resource entry.  */
963                         cur_entry->offset_in_res = 0;
964                         cur_entry->size = reshdr.uncompressed_size;
965                         cur_entry->flags = reshdr.flags;
966                         cur_rspec = NULL;
967                 }
968                 continue;
969
970         free_cur_entry_and_continue:
971                 free_lookup_table_entry(cur_entry);
972         }
973         cur_entry = NULL;
974
975         /* Validate the last resource.  */
976         if (cur_rspec) {
977                 ret = finish_resource(cur_rspec);
978                 cur_rspec = NULL;
979                 if (ret)
980                         goto out;
981         }
982
983         if (wim->hdr.part_number == 1 && image_index != wim->hdr.image_count) {
984                 WARNING("Could not find metadata resources for all images");
985                 for (u32 i = image_index; i < wim->hdr.image_count; i++)
986                         put_image_metadata(wim->image_metadata[i], NULL);
987                 wim->hdr.image_count = image_index;
988         }
989
990         if (num_duplicate_entries > 0) {
991                 WARNING("Ignoring %zu duplicate streams in the WIM lookup table",
992                         num_duplicate_entries);
993         }
994
995         if (num_wrong_part_entries > 0) {
996                 WARNING("Ignoring %zu streams with wrong part number",
997                         num_wrong_part_entries);
998         }
999
1000         DEBUG("Done reading lookup table.");
1001         wim->lookup_table = table;
1002         table = NULL;
1003         ret = 0;
1004         goto out;
1005 oom:
1006         ERROR("Not enough memory to read lookup table!");
1007         ret = WIMLIB_ERR_NOMEM;
1008 out:
1009         if (cur_rspec && list_empty(&cur_rspec->stream_list))
1010                 FREE(cur_rspec);
1011         free_lookup_table_entry(cur_entry);
1012         free_lookup_table(table);
1013         FREE(buf);
1014         return ret;
1015 }
1016
1017 static void
1018 put_wim_lookup_table_entry(struct wim_lookup_table_entry_disk *disk_entry,
1019                            const struct wim_reshdr *out_reshdr,
1020                            u16 part_number, u32 refcnt, const u8 *hash)
1021 {
1022         put_wim_reshdr(out_reshdr, &disk_entry->reshdr);
1023         disk_entry->part_number = cpu_to_le16(part_number);
1024         disk_entry->refcnt = cpu_to_le32(refcnt);
1025         copy_hash(disk_entry->hash, hash);
1026 }
1027
1028 int
1029 write_wim_lookup_table_from_stream_list(struct list_head *stream_list,
1030                                         struct filedes *out_fd,
1031                                         u16 part_number,
1032                                         struct wim_reshdr *out_reshdr,
1033                                         int write_resource_flags)
1034 {
1035         size_t table_size;
1036         struct wim_lookup_table_entry *lte;
1037         struct wim_lookup_table_entry_disk *table_buf;
1038         struct wim_lookup_table_entry_disk *table_buf_ptr;
1039         int ret;
1040         u64 prev_res_offset_in_wim = ~0ULL;
1041
1042         table_size = 0;
1043         list_for_each_entry(lte, stream_list, lookup_table_list) {
1044                 table_size += sizeof(struct wim_lookup_table_entry_disk);
1045
1046                 if (lte->out_reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS &&
1047                     lte->out_res_offset_in_wim != prev_res_offset_in_wim)
1048                 {
1049                         table_size += sizeof(struct wim_lookup_table_entry_disk);
1050                         prev_res_offset_in_wim = lte->out_res_offset_in_wim;
1051                 }
1052         }
1053
1054         DEBUG("Writing WIM lookup table (size=%zu, offset=%"PRIu64")",
1055               table_size, out_fd->offset);
1056
1057         table_buf = MALLOC(table_size);
1058         if (table_buf == NULL) {
1059                 ERROR("Failed to allocate %zu bytes for temporary lookup table",
1060                       table_size);
1061                 return WIMLIB_ERR_NOMEM;
1062         }
1063         table_buf_ptr = table_buf;
1064
1065         prev_res_offset_in_wim = ~0ULL;
1066         list_for_each_entry(lte, stream_list, lookup_table_list) {
1067
1068                 put_wim_lookup_table_entry(table_buf_ptr++,
1069                                            &lte->out_reshdr,
1070                                            part_number,
1071                                            lte->out_refcnt,
1072                                            lte->hash);
1073                 if (lte->out_reshdr.flags & WIM_RESHDR_FLAG_PACKED_STREAMS &&
1074                     lte->out_res_offset_in_wim != prev_res_offset_in_wim)
1075                 {
1076                         /* Put the main resource entry for the pack.  */
1077
1078                         struct wim_reshdr reshdr;
1079
1080                         reshdr.offset_in_wim = lte->out_res_offset_in_wim;
1081                         reshdr.size_in_wim = lte->out_res_size_in_wim;
1082                         reshdr.uncompressed_size = WIM_PACK_MAGIC_NUMBER;
1083                         reshdr.flags = WIM_RESHDR_FLAG_PACKED_STREAMS;
1084
1085                         DEBUG("Putting main entry for pack: "
1086                               "size_in_wim=%"PRIu64", "
1087                               "offset_in_wim=%"PRIu64", "
1088                               "uncompressed_size=%"PRIu64,
1089                               reshdr.size_in_wim,
1090                               reshdr.offset_in_wim,
1091                               reshdr.uncompressed_size);
1092
1093                         put_wim_lookup_table_entry(table_buf_ptr++,
1094                                                    &reshdr,
1095                                                    part_number,
1096                                                    1, zero_hash);
1097                         prev_res_offset_in_wim = lte->out_res_offset_in_wim;
1098                 }
1099
1100         }
1101         wimlib_assert((u8*)table_buf_ptr - (u8*)table_buf == table_size);
1102
1103         /* Write the lookup table uncompressed.  Although wimlib can handle a
1104          * compressed lookup table, MS software cannot.  */
1105         ret = write_wim_resource_from_buffer(table_buf,
1106                                              table_size,
1107                                              WIM_RESHDR_FLAG_METADATA,
1108                                              out_fd,
1109                                              WIMLIB_COMPRESSION_TYPE_NONE,
1110                                              0,
1111                                              out_reshdr,
1112                                              NULL,
1113                                              write_resource_flags);
1114         FREE(table_buf);
1115         DEBUG("ret=%d", ret);
1116         return ret;
1117 }
1118
1119 int
1120 lte_zero_real_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
1121 {
1122         lte->real_refcnt = 0;
1123         return 0;
1124 }
1125
1126 int
1127 lte_zero_out_refcnt(struct wim_lookup_table_entry *lte, void *_ignore)
1128 {
1129         lte->out_refcnt = 0;
1130         return 0;
1131 }
1132
1133 int
1134 lte_free_extracted_file(struct wim_lookup_table_entry *lte, void *_ignore)
1135 {
1136         if (lte->extracted_file != NULL) {
1137                 FREE(lte->extracted_file);
1138                 lte->extracted_file = NULL;
1139         }
1140         return 0;
1141 }
1142
1143 /* Allocate a stream entry for the contents of the buffer, or re-use an existing
1144  * entry in @lookup_table for the same stream.  */
1145 struct wim_lookup_table_entry *
1146 new_stream_from_data_buffer(const void *buffer, size_t size,
1147                             struct wim_lookup_table *lookup_table)
1148 {
1149         u8 hash[SHA1_HASH_SIZE];
1150         struct wim_lookup_table_entry *lte, *existing_lte;
1151
1152         sha1_buffer(buffer, size, hash);
1153         existing_lte = lookup_stream(lookup_table, hash);
1154         if (existing_lte) {
1155                 wimlib_assert(existing_lte->size == size);
1156                 lte = existing_lte;
1157                 lte->refcnt++;
1158         } else {
1159                 void *buffer_copy;
1160                 lte = new_lookup_table_entry();
1161                 if (lte == NULL)
1162                         return NULL;
1163                 buffer_copy = memdup(buffer, size);
1164                 if (buffer_copy == NULL) {
1165                         free_lookup_table_entry(lte);
1166                         return NULL;
1167                 }
1168                 lte->resource_location  = RESOURCE_IN_ATTACHED_BUFFER;
1169                 lte->attached_buffer    = buffer_copy;
1170                 lte->size               = size;
1171                 copy_hash(lte->hash, hash);
1172                 lookup_table_insert(lookup_table, lte);
1173         }
1174         return lte;
1175 }
1176
1177 /* Calculate the SHA1 message digest of a stream and move it from the list of
1178  * unhashed streams to the stream lookup table, possibly joining it with an
1179  * existing lookup table entry for an identical stream.
1180  *
1181  * @lte:  An unhashed lookup table entry.
1182  * @lookup_table:  Lookup table for the WIM.
1183  * @lte_ret:  On success, write a pointer to the resulting lookup table
1184  *            entry to this location.  This will be the same as @lte
1185  *            if it was inserted into the lookup table, or different if
1186  *            a duplicate stream was found.
1187  *
1188  * Returns 0 on success; nonzero if there is an error reading the stream.
1189  */
1190 int
1191 hash_unhashed_stream(struct wim_lookup_table_entry *lte,
1192                      struct wim_lookup_table *lookup_table,
1193                      struct wim_lookup_table_entry **lte_ret)
1194 {
1195         int ret;
1196         struct wim_lookup_table_entry *duplicate_lte;
1197         struct wim_lookup_table_entry **back_ptr;
1198
1199         wimlib_assert(lte->unhashed);
1200
1201         /* back_ptr must be saved because @back_inode and @back_stream_id are in
1202          * union with the SHA1 message digest and will no longer be valid once
1203          * the SHA1 has been calculated. */
1204         back_ptr = retrieve_lte_pointer(lte);
1205
1206         ret = sha1_stream(lte);
1207         if (ret)
1208                 return ret;
1209
1210         /* Look for a duplicate stream */
1211         duplicate_lte = lookup_stream(lookup_table, lte->hash);
1212         list_del(&lte->unhashed_list);
1213         if (duplicate_lte) {
1214                 /* We have a duplicate stream.  Transfer the reference counts
1215                  * from this stream to the duplicate and update the reference to
1216                  * this stream (in an inode or ads_entry) to point to the
1217                  * duplicate.  The caller is responsible for freeing @lte if
1218                  * needed.  */
1219                 wimlib_assert(!(duplicate_lte->unhashed));
1220                 wimlib_assert(duplicate_lte->size == lte->size);
1221                 duplicate_lte->refcnt += lte->refcnt;
1222                 lte->refcnt = 0;
1223                 *back_ptr = duplicate_lte;
1224                 lte = duplicate_lte;
1225         } else {
1226                 /* No duplicate stream, so we need to insert this stream into
1227                  * the lookup table and treat it as a hashed stream. */
1228                 lookup_table_insert(lookup_table, lte);
1229                 lte->unhashed = 0;
1230         }
1231         *lte_ret = lte;
1232         return 0;
1233 }
1234
1235 void
1236 lte_to_wimlib_resource_entry(const struct wim_lookup_table_entry *lte,
1237                              struct wimlib_resource_entry *wentry)
1238 {
1239         memset(wentry, 0, sizeof(*wentry));
1240
1241         wentry->uncompressed_size = lte->size;
1242         if (lte->resource_location == RESOURCE_IN_WIM) {
1243                 wentry->part_number = lte->rspec->wim->hdr.part_number;
1244                 if (lte->flags & WIM_RESHDR_FLAG_PACKED_STREAMS) {
1245                         wentry->compressed_size = 0;
1246                         wentry->offset = lte->offset_in_res;
1247                 } else {
1248                         wentry->compressed_size = lte->rspec->size_in_wim;
1249                         wentry->offset = lte->rspec->offset_in_wim;
1250                 }
1251                 wentry->raw_resource_offset_in_wim = lte->rspec->offset_in_wim;
1252                 /*wentry->raw_resource_uncompressed_size = lte->rspec->uncompressed_size;*/
1253                 wentry->raw_resource_compressed_size = lte->rspec->size_in_wim;
1254         }
1255         copy_hash(wentry->sha1_hash, lte->hash);
1256         wentry->reference_count = lte->refcnt;
1257         wentry->is_compressed = (lte->flags & WIM_RESHDR_FLAG_COMPRESSED) != 0;
1258         wentry->is_metadata = (lte->flags & WIM_RESHDR_FLAG_METADATA) != 0;
1259         wentry->is_free = (lte->flags & WIM_RESHDR_FLAG_FREE) != 0;
1260         wentry->is_spanned = (lte->flags & WIM_RESHDR_FLAG_SPANNED) != 0;
1261         wentry->packed = (lte->flags & WIM_RESHDR_FLAG_PACKED_STREAMS) != 0;
1262 }
1263
1264 struct iterate_lte_context {
1265         wimlib_iterate_lookup_table_callback_t cb;
1266         void *user_ctx;
1267 };
1268
1269 static int
1270 do_iterate_lte(struct wim_lookup_table_entry *lte, void *_ctx)
1271 {
1272         struct iterate_lte_context *ctx = _ctx;
1273         struct wimlib_resource_entry entry;
1274
1275         lte_to_wimlib_resource_entry(lte, &entry);
1276         return (*ctx->cb)(&entry, ctx->user_ctx);
1277 }
1278
1279 /* API function documented in wimlib.h  */
1280 WIMLIBAPI int
1281 wimlib_iterate_lookup_table(WIMStruct *wim, int flags,
1282                             wimlib_iterate_lookup_table_callback_t cb,
1283                             void *user_ctx)
1284 {
1285         if (flags != 0)
1286                 return WIMLIB_ERR_INVALID_PARAM;
1287
1288         struct iterate_lte_context ctx = {
1289                 .cb = cb,
1290                 .user_ctx = user_ctx,
1291         };
1292         if (wim->hdr.part_number == 1) {
1293                 int ret;
1294                 for (int i = 0; i < wim->hdr.image_count; i++) {
1295                         ret = do_iterate_lte(wim->image_metadata[i]->metadata_lte,
1296                                              &ctx);
1297                         if (ret)
1298                                 return ret;
1299                 }
1300         }
1301         return for_lookup_table_entry(wim->lookup_table, do_iterate_lte, &ctx);
1302 }