feat(573file): add headered LZ file reader

This commit is contained in:
decafcode 2023-11-24 11:16:23 -05:00 committed by decafcode
parent fbfd826782
commit cb8b1eebd2
3 changed files with 102 additions and 0 deletions

93
573file/lz-file.c Normal file
View File

@ -0,0 +1,93 @@
#include <assert.h>
#include <errno.h>
#include <string.h>
#include "573file/lz.h"
#include "util/iobuf.h"
#include "util/log.h"
int lz_file_read(struct const_iobuf *src, void **out_bytes,
size_t *out_nbytes) {
struct iobuf dest;
uint32_t h_orig_size;
uint32_t h_comp_size;
size_t orig_size;
size_t comp_size;
const void *payload;
void *bytes;
int r;
assert(src != NULL);
assert(out_bytes != NULL);
assert(out_nbytes != NULL);
bytes = NULL;
*out_bytes = NULL;
*out_nbytes = 0;
r = iobuf_read_be32(src, &h_orig_size);
if (r < 0) {
log_error(r);
goto end;
}
r = iobuf_read_be32(src, &h_comp_size);
if (r < 0) {
log_error(r);
goto end;
}
comp_size = src->nbytes - src->pos;
if (comp_size != h_comp_size) {
r = -EBADMSG;
log_write(
"Compressed size mismatch: Header says %#x bytes, actual size is %#lx",
h_comp_size, (unsigned long)comp_size);
goto end;
}
payload = src->bytes + src->pos;
memset(&dest, 0, sizeof(dest));
lz_dec_decompress(payload, comp_size, &dest);
orig_size = dest.pos;
if (orig_size != h_orig_size) {
r = -EBADMSG;
log_write(
"Original size mismatch: Header says %#x bytes, actual size is #%lx",
h_orig_size, (unsigned long)orig_size);
goto end;
}
bytes = malloc(orig_size);
if (bytes == NULL) {
r = -EBADMSG;
goto end;
}
dest.bytes = bytes;
dest.nbytes = orig_size;
dest.pos = 0;
lz_dec_decompress(payload, comp_size, &dest);
*out_bytes = bytes;
*out_nbytes = orig_size;
bytes = NULL;
end:
free(bytes);
return r;
}

7
573file/lz-file.h Normal file
View File

@ -0,0 +1,7 @@
#pragma once
#include <stddef.h>
#include "util/iobuf.h"
int lz_file_read(struct const_iobuf *src, void **out_bytes, size_t *out_nbytes);

View File

@ -7,6 +7,8 @@ _573file_lib = static_library(
'ifs.h',
'lz.c',
'lz.h',
'lz-file.c',
'lz-file.h',
'prop-binary-reader.c',
'prop-binary-reader.h',
'prop-type.c',