- /* Empty resource; nothing needs to be done, so just return success. */
- if (bytes_remaining == 0)
- return 0;
-
- /* Buffer for reading chunks for the resource */
- u8 buf[min(WIM_CHUNK_SIZE, bytes_remaining)];
-
- /* If we are writing a compressed resource and not doing a raw copy, we
- * need to initialize the chunk table */
- if (out_ctype != WIMLIB_COMPRESSION_TYPE_NONE && !raw) {
- ret = begin_wim_resource_chunk_tab(lte, out_fp, file_offset,
- &chunk_tab);
- if (ret != 0)
- goto out;
- }
-
- /* If the WIM resource is in an external file, open a FILE * to it so we
- * don't have to open a temporary one in read_wim_resource() for each
- * chunk. */
-#ifdef WITH_NTFS_3G
- ret = prepare_resource_for_read(lte, &ni);
-#else
- ret = prepare_resource_for_read(lte);
-#endif
- if (ret != 0)
- goto out;
-
- /* If we aren't doing a raw copy, we will compute the SHA1 message
- * digest of the resource as we read it, and verify it's the same as the
- * hash given in the lookup table entry once we've finished reading the
- * resource. */
- SHA_CTX ctx;
- if (!raw) {
- sha1_init(&ctx);
- compress = get_compress_func(out_ctype);
- }
- offset = 0;
-
- /* While there are still bytes remaining in the WIM resource, read a
- * chunk of the resource, update SHA1, then write that chunk using the
- * desired compression type. */
- do {
- u64 to_read = min(bytes_remaining, WIM_CHUNK_SIZE);
- ret = read_wim_resource(lte, buf, to_read, offset, flags);
- if (ret != 0)
- goto out_fclose;
- if (!raw)
- sha1_update(&ctx, buf, to_read);
- ret = write_wim_resource_chunk(buf, to_read, out_fp,
- compress, chunk_tab);
- if (ret != 0)
- goto out_fclose;
- bytes_remaining -= to_read;
- offset += to_read;
- } while (bytes_remaining);
-
- /* Raw copy: The new compressed size is the same as the old compressed
- * size
- *
- * Using WIMLIB_COMPRESSION_TYPE_NONE: The new compressed size is the
- * original size
- *
- * Using a different compression type: Call
- * finish_wim_resource_chunk_tab() and it will provide the new
- * compressed size.
- */
- if (raw) {
- new_compressed_size = old_compressed_size;