xzdec: Remove unused short option -M
[xz/debian.git] / debug / known_sizes.c
blob3f5aada0d1cbbe11ff6bdb1055183fabac09beb9
1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
4 //
5 /// \file known_sizes.c
6 /// \brief Encodes .lzma Stream with sizes known in Block Header
7 ///
8 /// The input file is encoded in RAM, and the known Compressed Size
9 /// and/or Uncompressed Size values are stored in the Block Header.
10 /// As of writing there's no such Stream encoder in liblzma.
12 // Author: Lasse Collin
14 ///////////////////////////////////////////////////////////////////////////////
16 #include "sysdefs.h"
17 #include "lzma.h"
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/unistd.h>
21 #include <stdio.h>
24 // Support file sizes up to 1 MiB. We use this for output space too, so files
25 // close to 1 MiB had better compress at least a little or we have a buffer
26 // overflow.
27 #define BUFFER_SIZE (1U << 20)
30 int
31 main(void)
33 // Allocate the buffers.
34 uint8_t *in = malloc(BUFFER_SIZE);
35 uint8_t *out = malloc(BUFFER_SIZE);
36 if (in == NULL || out == NULL)
37 return 1;
39 // Fill the input buffer.
40 const size_t in_size = fread(in, 1, BUFFER_SIZE, stdin);
42 // Filter setup
43 lzma_options_lzma opt_lzma;
44 if (lzma_lzma_preset(&opt_lzma, 1))
45 return 1;
47 lzma_filter filters[] = {
49 .id = LZMA_FILTER_LZMA2,
50 .options = &opt_lzma
53 .id = LZMA_VLI_UNKNOWN
57 lzma_block block = {
58 .check = LZMA_CHECK_CRC32,
59 .compressed_size = BUFFER_SIZE, // Worst case reserve
60 .uncompressed_size = in_size,
61 .filters = filters,
64 lzma_stream strm = LZMA_STREAM_INIT;
65 if (lzma_block_encoder(&strm, &block) != LZMA_OK)
66 return 1;
68 // Reserve space for Stream Header and Block Header. We need to
69 // calculate the size of the Block Header first.
70 if (lzma_block_header_size(&block) != LZMA_OK)
71 return 1;
73 size_t out_size = LZMA_STREAM_HEADER_SIZE + block.header_size;
75 strm.next_in = in;
76 strm.avail_in = in_size;
77 strm.next_out = out + out_size;
78 strm.avail_out = BUFFER_SIZE - out_size;
80 if (lzma_code(&strm, LZMA_FINISH) != LZMA_STREAM_END)
81 return 1;
83 out_size += strm.total_out;
85 if (lzma_block_header_encode(&block, out + LZMA_STREAM_HEADER_SIZE)
86 != LZMA_OK)
87 return 1;
89 lzma_index *idx = lzma_index_init(NULL);
90 if (idx == NULL)
91 return 1;
93 if (lzma_index_append(idx, NULL, block.header_size + strm.total_out,
94 strm.total_in) != LZMA_OK)
95 return 1;
97 if (lzma_index_encoder(&strm, idx) != LZMA_OK)
98 return 1;
100 if (lzma_code(&strm, LZMA_RUN) != LZMA_STREAM_END)
101 return 1;
103 out_size += strm.total_out;
105 lzma_end(&strm);
107 lzma_index_end(idx, NULL);
109 // Encode the Stream Header and Stream Footer. backwards_size is
110 // needed only for the Stream Footer.
111 lzma_stream_flags sf = {
112 .backward_size = strm.total_out,
113 .check = block.check,
116 if (lzma_stream_header_encode(&sf, out) != LZMA_OK)
117 return 1;
119 if (lzma_stream_footer_encode(&sf, out + out_size) != LZMA_OK)
120 return 1;
122 out_size += LZMA_STREAM_HEADER_SIZE;
124 // Write out the file.
125 fwrite(out, 1, out_size, stdout);
127 return 0;