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