Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

thdat: fix wrong thtk_io_map() usage in th02_read() and th02_write() #119

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions thtk/thdat02.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,21 +122,24 @@ th02_read(
thtk_error_t** error)
{
thdat_entry_t* entry = &thdat->entries[entry_index];
unsigned char* data;
unsigned char* data = malloc(entry->zsize);
ssize_t ret;

#pragma omp critical
{
data = thtk_io_map(thdat->stream, entry->offset, entry->zsize, error);
ret = thtk_io_pread(thdat->stream, data, entry->zsize, entry->offset, error);
}
if (!data)
if (ret != (ssize_t)entry->zsize) {
free(data);
return -1;
}

for (ssize_t i = 0; i < entry->zsize; ++i)
data[i] ^= entry->extra;

if (entry->size == entry->zsize) {
ret = thtk_io_write(output, data, entry->zsize, error);
thtk_io_unmap(thdat->stream, data);
free(data);
} else {
thtk_io_t* data_stream = thtk_io_open_memory(data, entry->zsize, error);
if (!data_stream)
Expand Down Expand Up @@ -199,14 +202,18 @@ th02_write(
output = input;
}

unsigned char* data = thtk_io_map(output, 0, entry->zsize, error);
if (!data)
unsigned char* data = malloc(entry->zsize);
ssize_t ret = thtk_io_pread(output, data, entry->zsize, 0, error);
if (ret != entry->zsize) {
free(data);
if (output != input)
thtk_io_close(output);
return -1;
}

for (ssize_t i = 0; i < entry->zsize; ++i)
data[i] ^= thdat->version <= 2 ? th02_keys[thdat->version - 1] : entry_key;

ssize_t ret = -1;

#pragma omp critical
{
Expand All @@ -219,7 +226,7 @@ th02_write(
thdat->offset += ret;
}

thtk_io_unmap(output, data);
free(data);

if (output != input)
thtk_io_close(output);
Expand Down