-/*
- * Reads some data from the resource corresponding to a WIM lookup table entry.
- *
- * @lte: The WIM lookup table entry for the resource.
- * @buf: Buffer into which to write the data.
- * @size: Number of bytes to read.
- * @offset: Offset at which to start reading the resource.
- * @raw: If %true, compressed data is read literally rather than being
- * decompressed first.
- *
- * Returns zero on success, nonzero on failure.
- */
-int read_wim_resource(const struct lookup_table_entry *lte, u8 buf[],
- size_t size, u64 offset, bool raw)
-{
- /* We shouldn't be allowing read over-runs in any part of the library.
- * */
- if (raw)
- wimlib_assert(offset + size <= lte->resource_entry.size);
- else
- wimlib_assert(offset + size <= lte->resource_entry.original_size);
-
- int ctype;
- int ret;
- FILE *fp;
- switch (lte->resource_location) {
- case RESOURCE_IN_WIM:
- /* The resource is in a WIM file, and its WIMStruct is given by
- * the lte->wim member. The resource may be either compressed
- * or uncompressed. */
- wimlib_assert(lte->wim);
- wimlib_assert(lte->wim->fp);
- ctype = wim_resource_compression_type(lte);
-
- wimlib_assert(ctype != WIM_COMPRESSION_TYPE_NONE ||
- (lte->resource_entry.original_size ==
- lte->resource_entry.size));
-
- if (raw || ctype == WIM_COMPRESSION_TYPE_NONE)
- return read_uncompressed_resource(lte->wim->fp,
- lte->resource_entry.offset + offset,
- size, buf);
- else
- return read_compressed_resource(lte->wim->fp,
- lte->resource_entry.size,
- lte->resource_entry.original_size,
- lte->resource_entry.offset,
- ctype, size, offset, buf);
- break;
- case RESOURCE_IN_STAGING_FILE:
- case RESOURCE_IN_FILE_ON_DISK:
- /* The resource is in some file on the external filesystem and
- * needs to be read uncompressed */
- wimlib_assert(lte->file_on_disk);
- wimlib_assert(<e->file_on_disk == <e->staging_file_name);
- /* Use existing file pointer if available; otherwise open one
- * temporarily */
- if (lte->file_on_disk_fp) {
- fp = lte->file_on_disk_fp;
- } else {
- fp = fopen(lte->file_on_disk, "rb");
- if (!fp) {
- ERROR_WITH_ERRNO("Failed to open the file "
- "`%s'", lte->file_on_disk);
- return WIMLIB_ERR_OPEN;
- }
- }
- ret = read_uncompressed_resource(fp, offset, size, buf);
- if (fp != lte->file_on_disk_fp)
- fclose(fp);
- return ret;
- break;
- case RESOURCE_IN_ATTACHED_BUFFER:
- /* The resource is directly attached uncompressed in an
- * in-memory buffer. */
- wimlib_assert(lte->attached_buffer);
- memcpy(buf, lte->attached_buffer + offset, size);
- return 0;
- break;
-#ifdef WITH_NTFS_3G
- case RESOURCE_IN_NTFS_VOLUME:
- wimlib_assert(lte->ntfs_loc);
- if (lte->attr) {
- u64 adjusted_offset;
- if (lte->ntfs_loc->is_reparse_point)
- adjusted_offset = offset + 8;