2
0
mirror of https://github.com/vasi/pixz synced 2024-11-18 15:26:46 +00:00
pixz/write.c

456 lines
13 KiB
C
Raw Normal View History

2010-01-13 03:16:25 +00:00
#include "pixz.h"
2010-01-12 06:57:11 +00:00
#include <archive.h>
#include <archive_entry.h>
2010-10-11 04:05:36 +00:00
#include <getopt.h>
2010-01-12 06:57:11 +00:00
#pragma mark TYPES
2010-01-12 06:57:11 +00:00
2010-01-13 05:40:07 +00:00
typedef struct io_block_t io_block_t;
struct io_block_t {
lzma_block block;
uint8_t *input, *output;
size_t insize, outsize;
2010-01-13 05:40:07 +00:00
};
#pragma mark GLOBALS
2010-01-23 05:54:05 +00:00
#define DEBUG 0
2010-10-11 04:05:36 +00:00
static bool gTar = true;
2010-10-11 04:46:48 +00:00
static uint32_t gPreset = LZMA_PRESET_DEFAULT;
2010-10-11 04:05:36 +00:00
static size_t gBlockInSize = 0, gBlockOutSize = 0;
2010-01-12 06:57:11 +00:00
2010-01-13 03:16:25 +00:00
static off_t gMultiHeaderStart = 0;
static bool gMultiHeader = false;
static off_t gTotalRead = 0;
2010-10-13 02:03:45 +00:00
static pipeline_item_t *gReadItem = NULL;
static io_block_t *gReadBlock = NULL;
2010-10-13 02:03:45 +00:00
static size_t gReadItemCount = 0;
static lzma_filter gFilters[LZMA_FILTERS_MAX + 1];
2010-01-13 03:16:25 +00:00
static uint8_t gFileIndexBuf[CHUNKSIZE];
static size_t gFileIndexBufPos = 0;
2010-01-12 06:57:11 +00:00
#pragma mark FUNCTION DECLARATIONS
2010-01-23 05:54:05 +00:00
#if DEBUG
#define debug(str, ...) fprintf(stderr, str "\n", ##__VA_ARGS__)
#else
#define debug(...)
#endif
2010-10-13 02:03:45 +00:00
static void read_thread();
static void encode_thread(size_t thnum);
static void *block_create();
static void block_free(void *data);
2010-01-12 06:57:11 +00:00
static void add_file(off_t offset, const char *name);
static archive_read_callback tar_read;
static archive_open_callback tar_ok;
static archive_close_callback tar_ok;
static void block_init(lzma_block *block);
static void stream_edge(lzma_vli backward_size);
2010-10-13 02:03:45 +00:00
static void write_block(pipeline_item_t *pi);
static void encode_index(void);
static void write_file_index(void);
static void write_file_index_bytes(size_t size, uint8_t *buf);
static void write_file_index_buf(lzma_action action);
2010-01-12 06:57:11 +00:00
#pragma mark FUNCTION DEFINITIONS
int main(int argc, char **argv) {
2010-10-12 06:19:55 +00:00
char *progname = argv[0];
2010-01-23 05:54:05 +00:00
debug("launch");
2010-10-11 04:05:36 +00:00
int ch;
2010-10-11 04:46:48 +00:00
while ((ch = getopt(argc, argv, "t0123456789")) != -1) {
2010-10-11 04:05:36 +00:00
switch (ch) {
case 't':
gTar = false;
break;
default:
2010-10-11 04:46:48 +00:00
if (optopt >= '0' && optopt <= '9') {
gPreset = optopt - '0';
} else {
die("Unknown option");
}
2010-10-11 04:05:36 +00:00
}
}
argc -= optind - 1;
argv += optind - 1;
2010-10-12 06:16:28 +00:00
if (argc == 1) {
gInFile = stdin;
gOutFile = stdout;
} else if (argc == 3) {
if (!(gInFile = fopen(argv[1], "r")))
die("Can't open input file");
if (!(gOutFile = fopen(argv[2], "w")))
die("Can't open output file");
} else {
2010-10-12 06:19:55 +00:00
die("Usage: %s [-t] [INPUT OUTPUT]", progname);
2010-10-12 06:16:28 +00:00
}
2010-01-12 06:57:11 +00:00
// xz options
lzma_options_lzma lzma_opts;
2010-10-11 04:46:48 +00:00
if (lzma_lzma_preset(&lzma_opts, gPreset))
2010-01-12 06:57:11 +00:00
die("Error setting lzma options");
gFilters[0] = (lzma_filter){ .id = LZMA_FILTER_LZMA2,
.options = &lzma_opts };
gFilters[1] = (lzma_filter){ .id = LZMA_VLI_UNKNOWN, .options = NULL };
gBlockInSize = lzma_opts.dict_size * 1.0;
gBlockOutSize = lzma_block_buffer_bound(gBlockInSize);
2010-10-13 02:03:45 +00:00
pipeline_create(block_create, block_free, read_thread, encode_thread);
2010-01-23 05:54:05 +00:00
debug("writer: start");
// pre-block setup: header, index
2010-10-11 00:07:13 +00:00
if (!(gIndex = lzma_index_init(NULL)))
2010-01-12 06:57:11 +00:00
die("Error creating index");
stream_edge(LZMA_VLI_UNKNOWN);
// write blocks
while (true) {
2010-10-13 02:03:45 +00:00
pipeline_item_t *pi = pipeline_merged();
if (!pi)
break;
2010-10-13 02:03:45 +00:00
debug("writer: received %zu", pi->seq);
write_block(pi);
queue_push(gPipelineStartQ, PIPELINE_ITEM, pi);
}
// file index
2010-10-11 04:05:36 +00:00
if (gTar) {
write_file_index();
free_file_index();
}
// post-block cleanup: index, footer
encode_index();
stream_edge(lzma_index_size(gIndex));
lzma_index_end(gIndex, NULL);
fclose(gOutFile);
2010-01-23 05:54:05 +00:00
debug("writer: cleaning up reader");
2010-10-13 02:03:45 +00:00
pipeline_destroy();
2010-01-23 05:54:05 +00:00
debug("exit");
return 0;
}
#pragma mark READING
2010-10-13 02:03:45 +00:00
static void read_thread() {
2010-01-23 05:54:05 +00:00
debug("reader: start");
2010-01-12 06:57:11 +00:00
struct archive *ar = archive_read_new();
archive_read_support_compression_none(ar);
2010-10-11 04:05:36 +00:00
if (gTar)
archive_read_support_format_tar(ar);
archive_read_support_format_raw(ar);
2010-01-12 06:57:11 +00:00
archive_read_open(ar, NULL, tar_ok, tar_read, tar_ok);
struct archive_entry *entry;
while (true) {
int aerr = archive_read_next_header(ar, &entry);
if (aerr == ARCHIVE_EOF) {
// TODO
break;
} else if (aerr != ARCHIVE_OK && aerr != ARCHIVE_WARN) {
// Some charset translations warn spuriously
fprintf(stderr, "%s\n", archive_error_string(ar));
die("Error reading archive entry");
}
2010-10-11 04:05:36 +00:00
if (archive_format(ar) == ARCHIVE_FORMAT_RAW)
gTar = false;
if (gTar) {
add_file(archive_read_header_position(ar),
archive_entry_pathname(entry));
}
}
2010-01-12 06:57:11 +00:00
archive_read_finish(ar);
fclose(gInFile);
2010-10-11 04:05:36 +00:00
if (gTar)
add_file(gTotalRead, NULL);
// write last block, if necessary
2010-10-13 02:03:45 +00:00
if (gReadItem) {
// if this block had only one read, and it was EOF, it's waste
2010-10-13 02:03:45 +00:00
debug("reader: handling last block %zu", gReadItemCount);
if (gReadBlock->insize)
pipeline_split(gReadItem);
else
2010-10-13 03:40:27 +00:00
queue_push(gPipelineStartQ, PIPELINE_ITEM, gReadItem);
2010-10-13 02:03:45 +00:00
gReadItem = NULL;
}
2010-01-12 06:57:11 +00:00
2010-01-13 05:40:07 +00:00
// stop the other threads
2010-01-23 05:54:05 +00:00
debug("reader: cleaning up encoders");
2010-10-13 02:03:45 +00:00
pipeline_stop();
2010-01-23 05:54:05 +00:00
debug("reader: end");
2010-01-12 06:57:11 +00:00
}
static ssize_t tar_read(struct archive *ar, void *ref, const void **bufp) {
2010-10-13 02:03:45 +00:00
if (!gReadItem) {
queue_pop(gPipelineStartQ, (void**)&gReadItem);
gReadBlock = (io_block_t*)(gReadItem->data);
gReadBlock->insize = 0;
2010-10-13 02:03:45 +00:00
debug("reader: reading %zu", gReadItemCount);
}
2010-01-12 06:57:11 +00:00
size_t space = gBlockInSize - gReadBlock->insize;
if (space > CHUNKSIZE)
space = CHUNKSIZE;
uint8_t *buf = gReadBlock->input + gReadBlock->insize;
size_t rd = fread(buf, 1, space, gInFile);
if (ferror(gInFile))
2010-01-12 06:57:11 +00:00
die("Error reading input file");
gReadBlock->insize += rd;
gTotalRead += rd;
2010-01-12 06:57:11 +00:00
*bufp = buf;
if (gReadBlock->insize == gBlockInSize) {
2010-10-13 02:03:45 +00:00
debug("reader: sending %zu", gReadItemCount);
pipeline_split(gReadItem);
++gReadItemCount;
gReadItem = NULL;
}
2010-01-12 06:57:11 +00:00
return rd;
}
static int tar_ok(struct archive *ar, void *ref) {
2010-01-12 06:57:11 +00:00
return ARCHIVE_OK;
}
static void add_file(off_t offset, const char *name) {
if (name && is_multi_header(name)) {
if (!gMultiHeader)
gMultiHeaderStart = offset;
gMultiHeader = true;
return;
}
2010-01-12 06:57:11 +00:00
file_index_t *f = malloc(sizeof(file_index_t));
f->offset = gMultiHeader ? gMultiHeaderStart : offset;
gMultiHeader = false;
2010-01-17 02:23:23 +00:00
f->name = name ? xstrdup(name) : NULL;
f->next = NULL;
if (gLastFile) {
gLastFile->next = f;
} else { // new index
gFileIndex = f;
}
gLastFile = f;
2010-01-12 06:57:11 +00:00
}
2010-10-13 02:03:45 +00:00
static void block_free(void *data) {
io_block_t *ib = (io_block_t*)data;
free(ib->input);
free(ib->output);
free(ib);
}
static void *block_create() {
io_block_t *ib = malloc(sizeof(io_block_t));
ib->input = malloc(gBlockInSize);
ib->output = malloc(gBlockOutSize);
return ib;
}
#pragma mark ENCODING
2010-10-13 02:03:45 +00:00
static void encode_thread(size_t thnum) {
lzma_stream stream = LZMA_STREAM_INIT;
while (true) {
2010-10-13 02:03:45 +00:00
pipeline_item_t *pi;
int msg = queue_pop(gPipelineSplitQ, (void**)&pi);
2010-10-13 03:40:27 +00:00
if (msg == PIPELINE_STOP)
break;
2010-10-13 02:03:45 +00:00
debug("encoder %zu: received %zu", thnum, pi->seq);
io_block_t *ib = (io_block_t*)(pi->data);
2010-10-11 00:37:14 +00:00
block_init(&ib->block);
2010-10-11 00:37:14 +00:00
if (lzma_block_header_encode(&ib->block, ib->output) != LZMA_OK)
die("Error encoding block header");
ib->outsize = ib->block.header_size;
if (lzma_block_encoder(&stream, &ib->block) != LZMA_OK)
die("Error creating block encoder");
stream.next_in = ib->input;
stream.avail_in = ib->insize;
stream.next_out = ib->output + ib->outsize;
stream.avail_out = gBlockOutSize - ib->outsize;
lzma_ret err = LZMA_OK;
while (err != LZMA_STREAM_END) {
err = lzma_code(&stream, LZMA_FINISH);
if (err != LZMA_OK && err != LZMA_STREAM_END)
die("Error encoding block");
}
ib->outsize = stream.next_out - ib->output;
2010-10-13 02:03:45 +00:00
debug("encoder %zu: sending %zu", thnum, pi->seq);
queue_push(gPipelineMergeQ, PIPELINE_ITEM, pi);
}
2010-10-11 00:37:14 +00:00
lzma_end(&stream);
}
#pragma mark WRITING
static void block_init(lzma_block *block) {
block->version = 0;
block->check = CHECK;
block->filters = gFilters;
block->compressed_size = block->uncompressed_size = LZMA_VLI_UNKNOWN;
2010-01-12 06:57:11 +00:00
if (lzma_block_header_size(block) != LZMA_OK)
2010-01-12 06:57:11 +00:00
die("Error getting block header size");
}
static void stream_edge(lzma_vli backward_size) {
lzma_stream_flags flags = { .version = 0, .check = CHECK,
.backward_size = backward_size };
uint8_t buf[LZMA_STREAM_HEADER_SIZE];
2010-01-12 06:57:11 +00:00
lzma_ret (*encoder)(const lzma_stream_flags *flags, uint8_t *buf);
encoder = backward_size == LZMA_VLI_UNKNOWN
? &lzma_stream_header_encode
: &lzma_stream_footer_encode;
if ((*encoder)(&flags, buf) != LZMA_OK)
die("Error encoding stream edge");
2010-01-12 06:57:11 +00:00
if (fwrite(buf, LZMA_STREAM_HEADER_SIZE, 1, gOutFile) != 1)
die("Error writing stream edge");
2010-01-12 06:57:11 +00:00
}
2010-10-13 02:03:45 +00:00
static void write_block(pipeline_item_t *pi) {
debug("writer: writing %zu", pi->seq);
io_block_t *ib = (io_block_t*)(pi->data);
// Does it make sense to chunk this?
size_t written = 0;
while (ib->outsize > written) {
size_t size = ib->outsize - written;
if (size > CHUNKSIZE)
size = CHUNKSIZE;
if (fwrite(ib->output + written, size, 1, gOutFile) != 1)
die("Error writing block data");
written += size;
}
if (lzma_index_append(gIndex, NULL,
lzma_block_unpadded_size(&ib->block),
ib->block.uncompressed_size) != LZMA_OK)
die("Error adding to index");
2010-01-23 05:54:05 +00:00
2010-10-13 02:03:45 +00:00
debug("writer: writing %zu complete", pi->seq);
2010-01-13 05:40:07 +00:00
}
static void encode_index(void) {
2010-01-12 06:57:11 +00:00
if (lzma_index_encoder(&gStream, gIndex) != LZMA_OK)
die("Error creating index encoder");
uint8_t obuf[CHUNKSIZE];
lzma_ret err = LZMA_OK;
while (err != LZMA_STREAM_END) {
gStream.next_out = obuf;
gStream.avail_out = CHUNKSIZE;
err = lzma_code(&gStream, LZMA_RUN);
if (err != LZMA_OK && err != LZMA_STREAM_END)
die("Error encoding index");
if (gStream.avail_out != CHUNKSIZE) {
if (fwrite(obuf, CHUNKSIZE - gStream.avail_out, 1, gOutFile) != 1)
die("Error writing index data");
}
}
lzma_end(&gStream);
}
static void write_file_index(void) {
lzma_block block;
block_init(&block);
uint8_t hdrbuf[block.header_size];
if (lzma_block_header_encode(&block, hdrbuf) != LZMA_OK)
die("Error encoding file index header");
if (fwrite(hdrbuf, block.header_size, 1, gOutFile) != 1)
die("Error writing file index header");
if (lzma_block_encoder(&gStream, &block) != LZMA_OK)
die("Error creating file index encoder");
2010-10-11 03:48:34 +00:00
uint8_t offbuf[sizeof(uint64_t)];
xle64enc(offbuf, PIXZ_INDEX_MAGIC);
write_file_index_bytes(sizeof(offbuf), offbuf);
for (file_index_t *f = gFileIndex; f != NULL; f = f->next) {
char *name = f->name ? f->name : "";
size_t len = strlen(name);
write_file_index_bytes(len + 1, (uint8_t*)name);
2010-01-17 02:23:23 +00:00
xle64enc(offbuf, f->offset);
write_file_index_bytes(sizeof(offbuf), offbuf);
}
write_file_index_buf(LZMA_FINISH);
if (lzma_index_append(gIndex, NULL, lzma_block_unpadded_size(&block),
block.uncompressed_size) != LZMA_OK)
die("Error adding file-index to index");
lzma_end(&gStream);
}
static void write_file_index_bytes(size_t size, uint8_t *buf) {
size_t bufpos = 0;
while (bufpos < size) {
size_t len = size - bufpos;
size_t space = CHUNKSIZE - gFileIndexBufPos;
if (len > space)
len = space;
memcpy(gFileIndexBuf + gFileIndexBufPos, buf + bufpos, len);
gFileIndexBufPos += len;
bufpos += len;
if (gFileIndexBufPos == CHUNKSIZE) {
write_file_index_buf(LZMA_RUN);
gFileIndexBufPos = 0;
}
2010-01-12 06:57:11 +00:00
}
}
static void write_file_index_buf(lzma_action action) {
uint8_t obuf[CHUNKSIZE];
gStream.avail_in = gFileIndexBufPos;
gStream.next_in = gFileIndexBuf;
lzma_ret err = LZMA_OK;
while (err != LZMA_STREAM_END && (action == LZMA_FINISH || gStream.avail_in)) {
gStream.avail_out = CHUNKSIZE;
gStream.next_out = obuf;
err = lzma_code(&gStream, action);
if (err != LZMA_OK && err != LZMA_STREAM_END)
die("Error encoding file index");
if (gStream.avail_out != CHUNKSIZE) {
if (fwrite(obuf, CHUNKSIZE - gStream.avail_out, 1, gOutFile) != 1)
die("Error writing file index");
}
}
gFileIndexBufPos = 0;
}