]> icculus.org git repositories - icculus/xz.git/blob - debug/known_sizes.c
Renamed constants:
[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 //  Copyright (C) 2008 Lasse Collin
11 //
12 //  This library is free software; you can redistribute it and/or
13 //  modify it under the terms of the GNU Lesser General Public
14 //  License as published by the Free Software Foundation; either
15 //  version 2.1 of the License, or (at your option) any later version.
16 //
17 //  This library is distributed in the hope that it will be useful,
18 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
19 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 //  Lesser General Public License for more details.
21 //
22 ///////////////////////////////////////////////////////////////////////////////
23
24 #include "sysdefs.h"
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <sys/unistd.h>
28 #include <stdio.h>
29
30
31 // Support file sizes up to 1 MiB. We use this for output space too, so files
32 // close to 1 MiB had better compress at least a little or we have a buffer
33 // overflow.
34 #define BUFFER_SIZE (1U << 20)
35
36
37 int
38 main(void)
39 {
40         // Allocate the buffers.
41         uint8_t *in = malloc(BUFFER_SIZE);
42         uint8_t *out = malloc(BUFFER_SIZE);
43         if (in == NULL || out == NULL)
44                 return 1;
45
46         // Fill the input buffer.
47         const size_t in_size = fread(in, 1, BUFFER_SIZE, stdin);
48
49         // Filter setup
50         lzma_filter filters[] = {
51                 {
52                         .id = LZMA_FILTER_LZMA2,
53                         .options = (void *)(&lzma_preset_lzma[0])
54                 },
55                 {
56                         .id = LZMA_VLI_UNKNOWN
57                 }
58         };
59
60         lzma_block block = {
61                 .check = LZMA_CHECK_CRC32,
62                 .compressed_size = BUFFER_SIZE, // Worst case reserve
63                 .uncompressed_size = in_size,
64                 .filters = filters,
65         };
66
67         lzma_stream strm = LZMA_STREAM_INIT;
68         if (lzma_block_encoder(&strm, &block) != LZMA_OK)
69                 return 1;
70
71         // Reserve space for Stream Header and Block Header. We need to
72         // calculate the size of the Block Header first.
73         if (lzma_block_header_size(&block) != LZMA_OK)
74                 return 1;
75
76         size_t out_size = LZMA_STREAM_HEADER_SIZE + block.header_size;
77
78         strm.next_in = in;
79         strm.avail_in = in_size;
80         strm.next_out = out + out_size;
81         strm.avail_out = BUFFER_SIZE - out_size;
82
83         if (lzma_code(&strm, LZMA_FINISH) != LZMA_STREAM_END)
84                 return 1;
85
86         out_size += strm.total_out;
87
88         if (lzma_block_header_encode(&block, out + LZMA_STREAM_HEADER_SIZE)
89                         != LZMA_OK)
90                 return 1;
91
92         lzma_index *idx = lzma_index_init(NULL, NULL);
93         if (idx == NULL)
94                 return 1;
95
96         if (lzma_index_append(idx, NULL, block.header_size + strm.total_out,
97                         strm.total_in) != LZMA_OK)
98                 return 1;
99
100         if (lzma_index_encoder(&strm, idx) != LZMA_OK)
101                 return 1;
102
103         if (lzma_code(&strm, LZMA_RUN) != LZMA_STREAM_END)
104                 return 1;
105
106         out_size += strm.total_out;
107
108         lzma_end(&strm);
109
110         lzma_index_end(idx, NULL);
111
112         // Encode the Stream Header and Stream Footer. backwards_size is
113         // needed only for the Stream Footer.
114         lzma_stream_flags sf = {
115                 .backward_size = strm.total_out,
116                 .check = block.check,
117         };
118
119         if (lzma_stream_header_encode(&sf, out) != LZMA_OK)
120                 return 1;
121
122         if (lzma_stream_footer_encode(&sf, out + out_size) != LZMA_OK)
123                 return 1;
124
125         out_size += LZMA_STREAM_HEADER_SIZE;
126
127         // Write out the file.
128         fwrite(out, 1, out_size, stdout);
129
130         return 0;
131 }