xzdec: Remove unused short option -M
[xz/debian.git] / debug / sync_flush.c
blob5ce2e57a8643e1459278c600c0ed7714aad6d117
1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file sync_flush.c
6 /// \brief Encode files using LZMA_SYNC_FLUSH
7 //
8 // Author: Lasse Collin
9 //
10 ///////////////////////////////////////////////////////////////////////////////
12 #include "sysdefs.h"
13 #include "lzma.h"
14 #include <stdio.h>
16 #define CHUNK 64
19 static lzma_stream strm = LZMA_STREAM_INIT;
20 static FILE *file_in;
23 static void
24 encode(size_t size, lzma_action action)
26 uint8_t in[CHUNK];
27 uint8_t out[CHUNK];
28 lzma_ret ret;
30 do {
31 if (strm.avail_in == 0 && size > 0) {
32 const size_t amount = my_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.
38 strm.next_out = out;
39 strm.avail_out = CHUNK;
41 ret = lzma_code(&strm, size == 0 ? action : LZMA_RUN);
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);
49 fwrite(out, 1, CHUNK - strm.avail_out, stdout);
51 } while (size > 0 || strm.avail_out == 0);
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);
62 int
63 main(int argc, char **argv)
65 file_in = argc > 1 ? fopen(argv[1], "rb") : stdin;
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 .mode = LZMA_MODE_NORMAL,
75 .nice_len = 32,
76 .mf = LZMA_MF_HC3,
77 .depth = 0,
80 lzma_options_delta opt_delta = {
81 .dist = 16
84 lzma_filter filters[LZMA_FILTERS_MAX + 1];
85 filters[0].id = LZMA_FILTER_LZMA2;
86 filters[0].options = &opt_lzma;
87 filters[1].id = LZMA_VLI_UNKNOWN;
89 // Init
90 if (lzma_stream_encoder(&strm, filters, LZMA_CHECK_CRC32) != LZMA_OK) {
91 fprintf(stderr, "init failed\n");
92 exit(1);
95 // Encoding
96 encode(0, LZMA_SYNC_FLUSH);
97 encode(6, LZMA_SYNC_FLUSH);
98 encode(0, LZMA_SYNC_FLUSH);
99 encode(7, LZMA_SYNC_FLUSH);
100 encode(0, LZMA_SYNC_FLUSH);
101 encode(0, LZMA_FINISH);
104 encode(53, LZMA_SYNC_FLUSH);
105 opt_lzma.lc = 2;
106 opt_lzma.lp = 1;
107 opt_lzma.pb = 0;
108 if (lzma_filters_update(&strm, filters) != LZMA_OK) {
109 fprintf(stderr, "update failed\n");
110 exit(1);
112 encode(404, LZMA_FINISH);
115 // Clean up
116 lzma_end(&strm);
118 return 0;
120 // Prevent useless warnings so we don't need to have special CFLAGS
121 // to disable -Werror.
122 (void)opt_lzma;
123 (void)opt_delta;