1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Encode files using LZMA_SYNC_FLUSH
6 // Copyright (C) 2008 Lasse Collin
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
24 static lzma_stream strm = LZMA_STREAM_INIT;
29 encode(size_t size, lzma_action action)
31 static const size_t CHUNK = 64;
37 if (strm.avail_in == 0 && size > 0) {
38 const size_t amount = MIN(size, CHUNK);
39 strm.avail_in = fread(in, 1, amount, file_in);
41 size -= amount; // Intentionally not using avail_in.
45 strm.avail_out = CHUNK;
47 ret = lzma_code(&strm, size == 0 ? action : LZMA_RUN);
49 if (ret != LZMA_OK && ret != LZMA_STREAM_END) {
50 fprintf(stderr, "%s:%u: %s: ret == %d\n",
51 __FILE__, __LINE__, __func__, ret);
55 fwrite(out, 1, CHUNK - strm.avail_out, stdout);
57 } while (size > 0 || strm.avail_out == 0);
59 if ((action == LZMA_RUN && ret != LZMA_OK)
60 || (action != LZMA_RUN && ret != LZMA_STREAM_END)) {
61 fprintf(stderr, "%s:%u: %s: ret == %d\n",
62 __FILE__, __LINE__, __func__, ret);
69 main(int argc, char **argv)
73 file_in = argc > 1 ? fopen(argv[1], "rb") : stdin;
76 lzma_options_lzma opt_lzma = {
77 .dictionary_size = 1 << 16,
78 .literal_context_bits = LZMA_LITERAL_CONTEXT_BITS_DEFAULT,
79 .literal_pos_bits = LZMA_LITERAL_POS_BITS_DEFAULT,
80 .pos_bits = LZMA_POS_BITS_DEFAULT,
81 .preset_dictionary = NULL,
83 .mode = LZMA_MODE_NORMAL,
85 .match_finder = LZMA_MF_HC3,
86 .match_finder_cycles = 0,
89 lzma_options_delta opt_delta = {
93 lzma_options_subblock opt_subblock = {
94 .allow_subfilters = true,
95 .alignment = 8, // LZMA_SUBBLOCK_ALIGNMENT_DEFAULT,
96 .subblock_data_size = LZMA_SUBBLOCK_DATA_SIZE_DEFAULT,
97 .rle = 1, // LZMA_SUBBLOCK_RLE_OFF,
98 .subfilter_mode = LZMA_SUBFILTER_SET,
100 opt_subblock.subfilter_options.id = LZMA_FILTER_LZMA;
101 opt_subblock.subfilter_options.options = &opt_lzma;
102 opt_subblock.subfilter_options.id = LZMA_FILTER_DELTA;
103 opt_subblock.subfilter_options.options = &opt_delta;
105 lzma_filter filters[LZMA_BLOCK_FILTERS_MAX + 1];
106 filters[0].id = LZMA_FILTER_LZMA2;
107 filters[0].options = &opt_lzma;
108 filters[1].id = LZMA_VLI_VALUE_UNKNOWN;
111 if (lzma_stream_encoder(&strm, filters, LZMA_CHECK_CRC32) != LZMA_OK) {
112 fprintf(stderr, "init failed\n");
118 encode(0, LZMA_SYNC_FLUSH);
119 encode(6, LZMA_SYNC_FLUSH);
120 encode(0, LZMA_SYNC_FLUSH);
121 encode(7, LZMA_SYNC_FLUSH);
122 encode(0, LZMA_SYNC_FLUSH);
123 encode(0, LZMA_FINISH);
125 encode(53, LZMA_SYNC_FLUSH);
126 // opt_lzma.literal_context_bits = 2;
127 // opt_lzma.literal_pos_bits = 1;
128 // opt_lzma.pos_bits = 0;
129 encode(404, LZMA_FINISH);
136 // Prevent useless warnings so we don't need to have special CFLAGS
137 // to disable -Werror.