]> icculus.org git repositories - icculus/xz.git/blob - debug/known_sizes.c
Put the interesting parts of XZ Utils into the public domain.
[icculus/xz.git] / debug / known_sizes.c
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       known_sizes.c
4 /// \brief      Encodes .lzma Stream with sizes known in Block Header
5 ///
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.
9 //
10 //  Author:     Lasse Collin
11 //
12 //  This file has been put into the public domain.
13 //  You can do whatever you want with this file.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16
17 #include "sysdefs.h"
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <sys/unistd.h>
21 #include <stdio.h>
22
23
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)
28
29
30 int
31 main(void)
32 {
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;
38
39         // Fill the input buffer.
40         const size_t in_size = fread(in, 1, BUFFER_SIZE, stdin);
41
42         // Filter setup
43         lzma_options_lzma opt_lzma;
44         if (lzma_lzma_preset(&opt_lzma, 1))
45                 return 1;
46
47         lzma_filter filters[] = {
48                 {
49                         .id = LZMA_FILTER_LZMA2,
50                         .options = &opt_lzma
51                 },
52                 {
53                         .id = LZMA_VLI_UNKNOWN
54                 }
55         };
56
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,
62         };
63
64         lzma_stream strm = LZMA_STREAM_INIT;
65         if (lzma_block_encoder(&strm, &block) != LZMA_OK)
66                 return 1;
67
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;
72
73         size_t out_size = LZMA_STREAM_HEADER_SIZE + block.header_size;
74
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;
79
80         if (lzma_code(&strm, LZMA_FINISH) != LZMA_STREAM_END)
81                 return 1;
82
83         out_size += strm.total_out;
84
85         if (lzma_block_header_encode(&block, out + LZMA_STREAM_HEADER_SIZE)
86                         != LZMA_OK)
87                 return 1;
88
89         lzma_index *idx = lzma_index_init(NULL, NULL);
90         if (idx == NULL)
91                 return 1;
92
93         if (lzma_index_append(idx, NULL, block.header_size + strm.total_out,
94                         strm.total_in) != LZMA_OK)
95                 return 1;
96
97         if (lzma_index_encoder(&strm, idx) != LZMA_OK)
98                 return 1;
99
100         if (lzma_code(&strm, LZMA_RUN) != LZMA_STREAM_END)
101                 return 1;
102
103         out_size += strm.total_out;
104
105         lzma_end(&strm);
106
107         lzma_index_end(idx, NULL);
108
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,
114         };
115
116         if (lzma_stream_header_encode(&sf, out) != LZMA_OK)
117                 return 1;
118
119         if (lzma_stream_footer_encode(&sf, out + out_size) != LZMA_OK)
120                 return 1;
121
122         out_size += LZMA_STREAM_HEADER_SIZE;
123
124         // Write out the file.
125         fwrite(out, 1, out_size, stdout);
126
127         return 0;
128 }