]> icculus.org git repositories - icculus/xz.git/blob - debug/sync_flush.c
076fb7e75ffdefe7b09ffec4700c410b6a45160a
[icculus/xz.git] / debug / sync_flush.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       sync_flush.c
4 /// \brief      Encode files using LZMA_SYNC_FLUSH
5 //
6 //  Copyright (C) 2008 Lasse Collin
7 //
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.
12 //
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.
17 //
18 ///////////////////////////////////////////////////////////////////////////////
19
20 #include "sysdefs.h"
21 #include <stdio.h>
22
23
24 static lzma_stream strm = LZMA_STREAM_INIT;
25 static FILE *file_in;
26
27
28 static void
29 encode(size_t size, lzma_action action)
30 {
31         static const size_t CHUNK = 64;
32         uint8_t in[CHUNK];
33         uint8_t out[CHUNK];
34         lzma_ret ret;
35
36         do {
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);
40                         strm.next_in = in;
41                         size -= amount; // Intentionally not using avail_in.
42                 }
43
44                 strm.next_out = out;
45                 strm.avail_out = CHUNK;
46
47                 ret = lzma_code(&strm, size == 0 ? action : LZMA_RUN);
48
49                 if (ret != LZMA_OK && ret != LZMA_STREAM_END) {
50                         fprintf(stderr, "%s:%u: %s: ret == %d\n",
51                                         __FILE__, __LINE__, __func__, ret);
52                         exit(1);
53                 }
54
55                 fwrite(out, 1, CHUNK - strm.avail_out, stdout);
56
57         } while (size > 0 || strm.avail_out == 0);
58
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);
63                 exit(1);
64         }
65 }
66
67
68 int
69 main(int argc, char **argv)
70 {
71         file_in = argc > 1 ? fopen(argv[1], "rb") : stdin;
72
73         // Config
74         lzma_options_lzma opt_lzma = {
75                 .dict_size = 1U << 16,
76                 .lc = LZMA_LC_DEFAULT,
77                 .lp = LZMA_LP_DEFAULT,
78                 .pb = LZMA_PB_DEFAULT,
79                 .preset_dict = NULL,
80                 .persistent = true,
81                 .mode = LZMA_MODE_NORMAL,
82                 .nice_len = 32,
83                 .mf = LZMA_MF_HC3,
84                 .depth = 0,
85         };
86
87         lzma_options_delta opt_delta = {
88                 .dist = 16
89         };
90
91         lzma_options_subblock opt_subblock = {
92                 .allow_subfilters = true,
93                 .alignment = 8, // LZMA_SUBBLOCK_ALIGNMENT_DEFAULT,
94                 .subblock_data_size = LZMA_SUBBLOCK_DATA_SIZE_DEFAULT,
95                 .rle = 1, // LZMA_SUBBLOCK_RLE_OFF,
96                 .subfilter_mode = LZMA_SUBFILTER_SET,
97         };
98         opt_subblock.subfilter_options.id = LZMA_FILTER_LZMA1;
99         opt_subblock.subfilter_options.options = &opt_lzma;
100         opt_subblock.subfilter_options.id = LZMA_FILTER_DELTA;
101         opt_subblock.subfilter_options.options = &opt_delta;
102
103         lzma_filter filters[LZMA_FILTERS_MAX + 1];
104         filters[0].id = LZMA_FILTER_LZMA2;
105         filters[0].options = &opt_lzma;
106         filters[1].id = LZMA_VLI_UNKNOWN;
107
108         // Init
109         if (lzma_stream_encoder(&strm, filters, LZMA_CHECK_CRC32) != LZMA_OK) {
110                 fprintf(stderr, "init failed\n");
111                 exit(1);
112         }
113
114         // Encoding
115
116         encode(0, LZMA_SYNC_FLUSH);
117         encode(6, LZMA_SYNC_FLUSH);
118         encode(0, LZMA_SYNC_FLUSH);
119         encode(7, LZMA_SYNC_FLUSH);
120         encode(0, LZMA_SYNC_FLUSH);
121         encode(0, LZMA_FINISH);
122 /*
123         encode(53, LZMA_SYNC_FLUSH);
124 //      opt_lzma.literal_context_bits = 2;
125 //      opt_lzma.literal_pos_bits = 1;
126 //      opt_lzma.pos_bits = 0;
127         encode(404, LZMA_FINISH);
128 */
129         // Clean up
130         lzma_end(&strm);
131
132         return 0;
133
134         // Prevent useless warnings so we don't need to have special CFLAGS
135         // to disable -Werror.
136         (void)opt_lzma;
137         (void)opt_subblock;
138         (void)opt_delta;
139 }