if (chunk_entry_size == 4) {
u32 *entries = (u32*)chunk_tab_buf;
while (num_needed_chunk_entries--)
- *chunk_tab_p++ = to_le32(*entries++);
+ *chunk_tab_p++ = le32_to_cpu(*entries++);
} else {
u64 *entries = (u64*)chunk_tab_buf;
while (num_needed_chunk_entries--)
- *chunk_tab_p++ = to_le64(*entries++);
+ *chunk_tab_p++ = le64_to_cpu(*entries++);
}
/* Done with the chunk table now. We must now seek to the first chunk
p = get_u8(p, &flags);
entry->size = size;
entry->flags = flags;
+
+ /* offset and original_size are truncated to 62 bits to avoid possible
+ * overflows, when converting to a signed 64-bit integer (off_t) or when
+ * adding size or original_size. This is okay since no one would ever
+ * actually have a WIM bigger than 4611686018427387903 bytes... */
p = get_u64(p, &entry->offset);
+ if (entry->offset & 0xc000000000000000ULL) {
+ WARNING("Truncating offset in resource entry");
+ entry->offset &= 0x3fffffffffffffffULL;
+ }
p = get_u64(p, &entry->original_size);
+ if (entry->original_size & 0xc000000000000000ULL) {
+ WARNING("Truncating original_size in resource entry");
+ entry->original_size &= 0x3fffffffffffffffULL;
+ }
return p;
}
{
u64 size = wim_resource_size(lte);
u64 num_chunks = (size + WIM_CHUNK_SIZE - 1) / WIM_CHUNK_SIZE;
- struct chunk_table *chunk_tab = MALLOC(sizeof(struct chunk_table) +
- num_chunks * sizeof(u64));
- int ret = 0;
+ size_t alloc_size = sizeof(struct chunk_table) + num_chunks * sizeof(u64);
+ struct chunk_table *chunk_tab = CALLOC(1, alloc_size);
+ int ret;
- wimlib_assert(size != 0);
if (!chunk_tab) {
ERROR("Failed to allocate chunk table for %"PRIu64" byte "
goto out;
}
- *chunk_tab_ret = chunk_tab;
+ ret = 0;
out:
+ *chunk_tab_ret = chunk_tab;
return ret;
}
}
if (chunk_tab->bytes_per_chunk_entry == 8) {
- array_to_le64(chunk_tab->offsets, chunk_tab->num_chunks);
+ array_cpu_to_le64(chunk_tab->offsets, chunk_tab->num_chunks);
} else {
for (u64 i = 0; i < chunk_tab->num_chunks; i++)
((u32*)chunk_tab->offsets)[i] =
- to_le32(chunk_tab->offsets[i]);
+ cpu_to_le32(chunk_tab->offsets[i]);
}
bytes_written = fwrite((u8*)chunk_tab->offsets +
chunk_tab->bytes_per_chunk_entry,
if (ret != 0)
break;
sha1_update(&ctx, buf, to_read);
- if (full_write(fd, buf, to_read) < 0) {
+ if (full_write(fd, buf, to_read) < to_read) {
ERROR_WITH_ERRNO("Error extracting WIM resource");
return WIMLIB_ERR_WRITE;
}
if (ret != 0)
goto out;
- /* It's very likely the SHA1 message digest of the metadata resource, so
- * re-insert the lookup table entry into the lookup table. */
+ /* It's very likely the SHA1 message digest of the metadata resource
+ * changed, so re-insert the lookup table entry into the lookup table.
+ * */
lookup_table_unlink(w->lookup_table, lte);
lookup_table_insert(w->lookup_table, lte);