1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Encode files using LZMA_FULL_FLUSH
6 // Author: Lasse Collin
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
11 ///////////////////////////////////////////////////////////////////////////////
17 static lzma_stream strm = LZMA_STREAM_INIT;
22 encode(size_t size, lzma_action action)
24 static const size_t CHUNK = 64;
30 if (strm.avail_in == 0 && size > 0) {
31 const size_t amount = MIN(size, CHUNK);
32 strm.avail_in = fread(in, 1, amount, file_in);
34 size -= amount; // Intentionally not using avail_in.
38 strm.avail_out = CHUNK;
40 ret = lzma_code(&strm, size == 0 ? action : LZMA_RUN);
42 if (ret != LZMA_OK && ret != LZMA_STREAM_END) {
43 fprintf(stderr, "%s:%u: %s: ret == %d\n",
44 __FILE__, __LINE__, __func__, ret);
48 fwrite(out, 1, CHUNK - strm.avail_out, stdout);
50 } while (size > 0 || strm.avail_out == 0);
52 if ((action == LZMA_RUN && ret != LZMA_OK)
53 || (action != LZMA_RUN && ret != LZMA_STREAM_END)) {
54 fprintf(stderr, "%s:%u: %s: ret == %d\n",
55 __FILE__, __LINE__, __func__, ret);
62 main(int argc, char **argv)
64 file_in = argc > 1 ? fopen(argv[1], "rb") : stdin;
68 lzma_options_lzma opt_lzma;
69 if (lzma_lzma_preset(&opt_lzma, 1)) {
70 fprintf(stderr, "preset failed\n");
73 lzma_filter filters[LZMA_FILTERS_MAX + 1];
74 filters[0].id = LZMA_FILTER_LZMA2;
75 filters[0].options = &opt_lzma;
76 filters[1].id = LZMA_VLI_UNKNOWN;
79 if (lzma_stream_encoder(&strm, filters, LZMA_CHECK_CRC32) != LZMA_OK) {
80 fprintf(stderr, "init failed\n");
84 // if (lzma_easy_encoder(&strm, 1)) {
85 // fprintf(stderr, "init failed\n");
90 encode(0, LZMA_FULL_FLUSH);
91 encode(6, LZMA_FULL_FLUSH);
92 encode(0, LZMA_FULL_FLUSH);
93 encode(7, LZMA_FULL_FLUSH);
94 encode(0, LZMA_FULL_FLUSH);
95 encode(0, LZMA_FINISH);