2008-01-23 19:21:21 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file full_flush.c
|
|
|
|
/// \brief Encode files using LZMA_FULL_FLUSH
|
|
|
|
//
|
2009-04-13 08:27:40 +00:00
|
|
|
// Author: Lasse Collin
|
2008-01-23 19:21:21 +00:00
|
|
|
//
|
2009-04-13 08:27:40 +00:00
|
|
|
// This file has been put into the public domain.
|
|
|
|
// You can do whatever you want with this file.
|
2008-01-23 19:21:21 +00:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "sysdefs.h"
|
2009-08-27 14:00:22 +00:00
|
|
|
#include "lzma.h"
|
2008-01-23 19:21:21 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2014-12-21 16:05:03 +00:00
|
|
|
#define CHUNK 64
|
|
|
|
|
2008-01-23 19:21:21 +00:00
|
|
|
|
|
|
|
static lzma_stream strm = LZMA_STREAM_INIT;
|
|
|
|
static FILE *file_in;
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
encode(size_t size, lzma_action action)
|
|
|
|
{
|
|
|
|
uint8_t in[CHUNK];
|
|
|
|
uint8_t out[CHUNK];
|
|
|
|
lzma_ret ret;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (strm.avail_in == 0 && size > 0) {
|
2010-05-26 07:36:46 +00:00
|
|
|
const size_t amount = my_min(size, CHUNK);
|
2008-01-23 19:21:21 +00:00
|
|
|
strm.avail_in = fread(in, 1, amount, file_in);
|
|
|
|
strm.next_in = in;
|
|
|
|
size -= amount; // Intentionally not using avail_in.
|
|
|
|
}
|
|
|
|
|
|
|
|
strm.next_out = out;
|
|
|
|
strm.avail_out = CHUNK;
|
|
|
|
|
|
|
|
ret = lzma_code(&strm, size == 0 ? action : LZMA_RUN);
|
|
|
|
|
|
|
|
if (ret != LZMA_OK && ret != LZMA_STREAM_END) {
|
|
|
|
fprintf(stderr, "%s:%u: %s: ret == %d\n",
|
|
|
|
__FILE__, __LINE__, __func__, ret);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
fwrite(out, 1, CHUNK - strm.avail_out, stdout);
|
|
|
|
|
|
|
|
} while (size > 0 || strm.avail_out == 0);
|
|
|
|
|
|
|
|
if ((action == LZMA_RUN && ret != LZMA_OK)
|
|
|
|
|| (action != LZMA_RUN && ret != LZMA_STREAM_END)) {
|
|
|
|
fprintf(stderr, "%s:%u: %s: ret == %d\n",
|
|
|
|
__FILE__, __LINE__, __func__, ret);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
file_in = argc > 1 ? fopen(argv[1], "rb") : stdin;
|
|
|
|
|
2008-08-28 19:53:15 +00:00
|
|
|
|
2008-01-23 19:21:21 +00:00
|
|
|
// Config
|
2008-09-27 16:09:21 +00:00
|
|
|
lzma_options_lzma opt_lzma;
|
2008-11-19 18:46:52 +00:00
|
|
|
if (lzma_lzma_preset(&opt_lzma, 1)) {
|
2008-09-27 16:09:21 +00:00
|
|
|
fprintf(stderr, "preset failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
2008-11-19 18:46:52 +00:00
|
|
|
lzma_filter filters[LZMA_FILTERS_MAX + 1];
|
2008-08-28 19:53:15 +00:00
|
|
|
filters[0].id = LZMA_FILTER_LZMA2;
|
2008-09-27 16:09:21 +00:00
|
|
|
filters[0].options = &opt_lzma;
|
2008-09-13 09:10:43 +00:00
|
|
|
filters[1].id = LZMA_VLI_UNKNOWN;
|
2008-01-23 19:21:21 +00:00
|
|
|
|
|
|
|
// Init
|
2008-11-19 18:46:52 +00:00
|
|
|
if (lzma_stream_encoder(&strm, filters, LZMA_CHECK_CRC32) != LZMA_OK) {
|
2008-01-23 19:21:21 +00:00
|
|
|
fprintf(stderr, "init failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2008-08-28 19:53:15 +00:00
|
|
|
// if (lzma_easy_encoder(&strm, 1)) {
|
|
|
|
// fprintf(stderr, "init failed\n");
|
|
|
|
// exit(1);
|
|
|
|
// }
|
|
|
|
|
2008-01-23 19:21:21 +00:00
|
|
|
// Encoding
|
|
|
|
encode(0, LZMA_FULL_FLUSH);
|
|
|
|
encode(6, LZMA_FULL_FLUSH);
|
|
|
|
encode(0, LZMA_FULL_FLUSH);
|
|
|
|
encode(7, LZMA_FULL_FLUSH);
|
|
|
|
encode(0, LZMA_FULL_FLUSH);
|
|
|
|
encode(0, LZMA_FINISH);
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
lzma_end(&strm);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|