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