CMake: Bump maximum policy version to 3.27.
[xz.git] / debug / sync_flush.c
blob229233313eb25ffe77702737baaf61273fd258ce
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.
11 ///////////////////////////////////////////////////////////////////////////////
13 #include "sysdefs.h"
14 #include "lzma.h"
15 #include <stdio.h>
17 #define CHUNK 64
20 static lzma_stream strm = LZMA_STREAM_INIT;
21 static FILE *file_in;
24 static void
25 encode(size_t size, lzma_action action)
27 uint8_t in[CHUNK];
28 uint8_t out[CHUNK];
29 lzma_ret ret;
31 do {
32 if (strm.avail_in == 0 && size > 0) {
33 const size_t amount = my_min(size, CHUNK);
34 strm.avail_in = fread(in, 1, amount, file_in);
35 strm.next_in = in;
36 size -= amount; // Intentionally not using avail_in.
39 strm.next_out = out;
40 strm.avail_out = CHUNK;
42 ret = lzma_code(&strm, size == 0 ? action : LZMA_RUN);
44 if (ret != LZMA_OK && ret != LZMA_STREAM_END) {
45 fprintf(stderr, "%s:%u: %s: ret == %d\n",
46 __FILE__, __LINE__, __func__, ret);
47 exit(1);
50 fwrite(out, 1, CHUNK - strm.avail_out, stdout);
52 } while (size > 0 || strm.avail_out == 0);
54 if ((action == LZMA_RUN && ret != LZMA_OK)
55 || (action != LZMA_RUN && ret != LZMA_STREAM_END)) {
56 fprintf(stderr, "%s:%u: %s: ret == %d\n",
57 __FILE__, __LINE__, __func__, ret);
58 exit(1);
63 int
64 main(int argc, char **argv)
66 file_in = argc > 1 ? fopen(argv[1], "rb") : stdin;
68 // Config
69 lzma_options_lzma opt_lzma = {
70 .dict_size = 1U << 16,
71 .lc = LZMA_LC_DEFAULT,
72 .lp = LZMA_LP_DEFAULT,
73 .pb = LZMA_PB_DEFAULT,
74 .preset_dict = NULL,
75 .mode = LZMA_MODE_NORMAL,
76 .nice_len = 32,
77 .mf = LZMA_MF_HC3,
78 .depth = 0,
81 lzma_options_delta opt_delta = {
82 .dist = 16
85 lzma_filter filters[LZMA_FILTERS_MAX + 1];
86 filters[0].id = LZMA_FILTER_LZMA2;
87 filters[0].options = &opt_lzma;
88 filters[1].id = LZMA_VLI_UNKNOWN;
90 // Init
91 if (lzma_stream_encoder(&strm, filters, LZMA_CHECK_CRC32) != LZMA_OK) {
92 fprintf(stderr, "init failed\n");
93 exit(1);
96 // Encoding
97 encode(0, LZMA_SYNC_FLUSH);
98 encode(6, LZMA_SYNC_FLUSH);
99 encode(0, LZMA_SYNC_FLUSH);
100 encode(7, LZMA_SYNC_FLUSH);
101 encode(0, LZMA_SYNC_FLUSH);
102 encode(0, LZMA_FINISH);
105 encode(53, LZMA_SYNC_FLUSH);
106 opt_lzma.lc = 2;
107 opt_lzma.lp = 1;
108 opt_lzma.pb = 0;
109 if (lzma_filters_update(&strm, filters) != LZMA_OK) {
110 fprintf(stderr, "update failed\n");
111 exit(1);
113 encode(404, LZMA_FINISH);
116 // Clean up
117 lzma_end(&strm);
119 return 0;
121 // Prevent useless warnings so we don't need to have special CFLAGS
122 // to disable -Werror.
123 (void)opt_lzma;
124 (void)opt_delta;