1 // SPDX-License-Identifier: 0BSD
3 ///////////////////////////////////////////////////////////////////////////////
5 /// \file known_sizes.c
6 /// \brief Encodes .lzma Stream with sizes known in Block Header
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 ///////////////////////////////////////////////////////////////////////////////
18 #include <sys/types.h>
20 #include <sys/unistd.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
27 #define BUFFER_SIZE (1U << 20)
33 // Allocate the buffers.
34 uint8_t *in
= malloc(BUFFER_SIZE
);
35 uint8_t *out
= malloc(BUFFER_SIZE
);
36 if (in
== NULL
|| out
== NULL
)
39 // Fill the input buffer.
40 const size_t in_size
= fread(in
, 1, BUFFER_SIZE
, stdin
);
43 lzma_options_lzma opt_lzma
;
44 if (lzma_lzma_preset(&opt_lzma
, 1))
47 lzma_filter filters
[] = {
49 .id
= LZMA_FILTER_LZMA2
,
53 .id
= LZMA_VLI_UNKNOWN
58 .check
= LZMA_CHECK_CRC32
,
59 .compressed_size
= BUFFER_SIZE
, // Worst case reserve
60 .uncompressed_size
= in_size
,
64 lzma_stream strm
= LZMA_STREAM_INIT
;
65 if (lzma_block_encoder(&strm
, &block
) != LZMA_OK
)
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
)
73 size_t out_size
= LZMA_STREAM_HEADER_SIZE
+ block
.header_size
;
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
)
83 out_size
+= strm
.total_out
;
85 if (lzma_block_header_encode(&block
, out
+ LZMA_STREAM_HEADER_SIZE
)
89 lzma_index
*idx
= lzma_index_init(NULL
);
93 if (lzma_index_append(idx
, NULL
, block
.header_size
+ strm
.total_out
,
94 strm
.total_in
) != LZMA_OK
)
97 if (lzma_index_encoder(&strm
, idx
) != LZMA_OK
)
100 if (lzma_code(&strm
, LZMA_RUN
) != LZMA_STREAM_END
)
103 out_size
+= strm
.total_out
;
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
)
119 if (lzma_stream_footer_encode(&sf
, out
+ out_size
) != LZMA_OK
)
122 out_size
+= LZMA_STREAM_HEADER_SIZE
;
124 // Write out the file.
125 fwrite(out
, 1, out_size
, stdout
);