]> wimlib.net Git - wimlib/blobdiff - src/resource.c
Factor out LZ match-choosing code
[wimlib] / src / resource.c
index 1a46e751184377863f53f7b11582db44e6662d00..f61ecd5ac209c457fbb6d59b1324b62557d15fca 100644 (file)
@@ -141,7 +141,7 @@ read_compressed_wim_resource(const struct wim_resource_spec * const rspec,
        bool chunk_offsets_malloced = false;
        bool ubuf_malloced = false;
        bool cbuf_malloced = false;
-       struct wimlib_decompressor *decompressor;
+       struct wimlib_decompressor *decompressor = NULL;
 
        /* Sanity checks  */
        wimlib_assert(rspec != NULL);
@@ -175,9 +175,9 @@ read_compressed_wim_resource(const struct wim_resource_spec * const rspec,
 
        /* Get the maximum size of uncompressed chunks in this resource, which
         * we require be a power of 2.  */
-       u32 chunk_size;
+       u32 chunk_size = 0;
        u64 cur_read_offset = rspec->offset_in_wim;
-       int ctype;
+       int ctype = WIMLIB_COMPRESSION_TYPE_NONE;
        if (alt_chunk_table) {
                /* Alternate chunk table format.  Its header specifies the chunk
                 * size and compression format.  */
@@ -443,7 +443,7 @@ read_compressed_wim_resource(const struct wim_resource_spec * const rspec,
                        ERROR("Invalid chunk size in compressed resource!");
                        errno = EINVAL;
                        ret = WIMLIB_ERR_DECOMPRESSION;
-                       goto out_save_decompressor;
+                       goto out_free_memory;
                }
                if (rspec->is_pipable)
                        cur_read_offset += sizeof(struct pwm_chunk_hdr);
@@ -496,7 +496,7 @@ read_compressed_wim_resource(const struct wim_resource_spec * const rspec,
                                        ERROR("Failed to decompress data!");
                                        ret = WIMLIB_ERR_DECOMPRESSION;
                                        errno = EINVAL;
-                                       goto out_save_decompressor;
+                                       goto out_free_memory;
                                }
                        }
                        cur_read_offset += chunk_csize;
@@ -517,7 +517,7 @@ read_compressed_wim_resource(const struct wim_resource_spec * const rspec,
                                ret = (*cb)(&ubuf[start], size, cb_ctx);
 
                                if (ret)
-                                       goto out_save_decompressor;
+                                       goto out_free_memory;
 
                                cur_range_pos += size;
                                if (cur_range_pos == cur_range_end) {
@@ -548,13 +548,15 @@ read_compressed_wim_resource(const struct wim_resource_spec * const rspec,
                        goto read_error;
        }
        ret = 0;
-out_save_decompressor:
-       wimlib_free_decompressor(rspec->wim->decompressor);
-       rspec->wim->decompressor = decompressor;
-       rspec->wim->decompressor_ctype = ctype;
-       rspec->wim->decompressor_max_block_size = chunk_size;
+
 out_free_memory:
        errno_save = errno;
+       if (decompressor) {
+               wimlib_free_decompressor(rspec->wim->decompressor);
+               rspec->wim->decompressor = decompressor;
+               rspec->wim->decompressor_ctype = ctype;
+               rspec->wim->decompressor_max_block_size = chunk_size;
+       }
        if (chunk_offsets_malloced)
                FREE(chunk_offsets);
        if (ubuf_malloced)
@@ -945,6 +947,8 @@ streamifier_cb(const void *chunk, size_t size, void *_ctx)
        if (ctx->cur_stream_offset == ctx->cur_stream->size) {
                /* Finished reading all the data for a stream.  */
 
+               ctx->cur_stream_offset = 0;
+
                DEBUG("End stream (size=%"PRIu64").", ctx->cur_stream->size);
                ret = (*ctx->cbs.end_stream)(ctx->cur_stream, 0,
                                             ctx->cbs.end_stream_ctx);
@@ -960,7 +964,6 @@ streamifier_cb(const void *chunk, size_t size, void *_ctx)
                        else
                                ctx->next_stream = NULL;
                }
-               ctx->cur_stream_offset = 0;
        }
        return 0;
 }
@@ -1320,6 +1323,15 @@ extract_stream_to_fd(struct wim_lookup_table_entry *lte,
        return extract_stream(lte, size, extract_chunk_to_fd, fd);
 }
 
+/* Extract the full uncompressed contents of the specified stream to the
+ * specified file descriptor.  */
+int
+extract_full_stream_to_fd(struct wim_lookup_table_entry *lte,
+                         struct filedes *fd)
+{
+       return extract_stream_to_fd(lte, fd, lte->size);
+}
+
 /* Calculate the SHA1 message digest of a stream and store it in @lte->hash.  */
 int
 sha1_stream(struct wim_lookup_table_entry *lte)