1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file known_sizes.c
4 /// \brief Encodes .lzma Stream with sizes known in Block Header
6 /// The input file is encoded in RAM, and the known Compressed Size
7 /// and/or Uncompressed Size values are stored in the Block Header.
8 /// As of writing there's no such Stream encoder in liblzma.
10 // Author: Lasse Collin
12 // This file has been put into the public domain.
13 // You can do whatever you want with this file.
15 ///////////////////////////////////////////////////////////////////////////////
19 #include <sys/types.h>
21 #include <sys/unistd.h>
25 // Support file sizes up to 1 MiB. We use this for output space too, so files
26 // close to 1 MiB had better compress at least a little or we have a buffer
28 #define BUFFER_SIZE (1U << 20)
34 // Allocate the buffers.
35 uint8_t *in
= malloc(BUFFER_SIZE
);
36 uint8_t *out
= malloc(BUFFER_SIZE
);
37 if (in
== NULL
|| out
== NULL
)
40 // Fill the input buffer.
41 const size_t in_size
= fread(in
, 1, BUFFER_SIZE
, stdin
);
44 lzma_options_lzma opt_lzma
;
45 if (lzma_lzma_preset(&opt_lzma
, 1))
48 lzma_filter filters
[] = {
50 .id
= LZMA_FILTER_LZMA2
,
54 .id
= LZMA_VLI_UNKNOWN
59 .check
= LZMA_CHECK_CRC32
,
60 .compressed_size
= BUFFER_SIZE
, // Worst case reserve
61 .uncompressed_size
= in_size
,
65 lzma_stream strm
= LZMA_STREAM_INIT
;
66 if (lzma_block_encoder(&strm
, &block
) != LZMA_OK
)
69 // Reserve space for Stream Header and Block Header. We need to
70 // calculate the size of the Block Header first.
71 if (lzma_block_header_size(&block
) != LZMA_OK
)
74 size_t out_size
= LZMA_STREAM_HEADER_SIZE
+ block
.header_size
;
77 strm
.avail_in
= in_size
;
78 strm
.next_out
= out
+ out_size
;
79 strm
.avail_out
= BUFFER_SIZE
- out_size
;
81 if (lzma_code(&strm
, LZMA_FINISH
) != LZMA_STREAM_END
)
84 out_size
+= strm
.total_out
;
86 if (lzma_block_header_encode(&block
, out
+ LZMA_STREAM_HEADER_SIZE
)
90 lzma_index
*idx
= lzma_index_init(NULL
);
94 if (lzma_index_append(idx
, NULL
, block
.header_size
+ strm
.total_out
,
95 strm
.total_in
) != LZMA_OK
)
98 if (lzma_index_encoder(&strm
, idx
) != LZMA_OK
)
101 if (lzma_code(&strm
, LZMA_RUN
) != LZMA_STREAM_END
)
104 out_size
+= strm
.total_out
;
108 lzma_index_end(idx
, NULL
);
110 // Encode the Stream Header and Stream Footer. backwards_size is
111 // needed only for the Stream Footer.
112 lzma_stream_flags sf
= {
113 .backward_size
= strm
.total_out
,
114 .check
= block
.check
,
117 if (lzma_stream_header_encode(&sf
, out
) != LZMA_OK
)
120 if (lzma_stream_footer_encode(&sf
, out
+ out_size
) != LZMA_OK
)
123 out_size
+= LZMA_STREAM_HEADER_SIZE
;
125 // Write out the file.
126 fwrite(out
, 1, out_size
, stdout
);