]> icculus.org git repositories - icculus/xz.git/blob - debug/known_sizes.c
Oh well, big messy commit again. Some highlights:
[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_options_lzma opt_lzma;
51         if (lzma_lzma_preset(&opt_lzma, 1))
52                 return 1;
53         
54         lzma_filter filters[] = {
55                 {
56                         .id = LZMA_FILTER_LZMA2,
57                         .options = &opt_lzma
58                 },
59                 {
60                         .id = LZMA_VLI_UNKNOWN
61                 }
62         };
63
64         lzma_block block = {
65                 .check = LZMA_CHECK_CRC32,
66                 .compressed_size = BUFFER_SIZE, // Worst case reserve
67                 .uncompressed_size = in_size,
68                 .filters = filters,
69         };
70
71         lzma_stream strm = LZMA_STREAM_INIT;
72         if (lzma_block_encoder(&strm, &block) != LZMA_OK)
73                 return 1;
74
75         // Reserve space for Stream Header and Block Header. We need to
76         // calculate the size of the Block Header first.
77         if (lzma_block_header_size(&block) != LZMA_OK)
78                 return 1;
79
80         size_t out_size = LZMA_STREAM_HEADER_SIZE + block.header_size;
81
82         strm.next_in = in;
83         strm.avail_in = in_size;
84         strm.next_out = out + out_size;
85         strm.avail_out = BUFFER_SIZE - out_size;
86
87         if (lzma_code(&strm, LZMA_FINISH) != LZMA_STREAM_END)
88                 return 1;
89
90         out_size += strm.total_out;
91
92         if (lzma_block_header_encode(&block, out + LZMA_STREAM_HEADER_SIZE)
93                         != LZMA_OK)
94                 return 1;
95
96         lzma_index *idx = lzma_index_init(NULL, NULL);
97         if (idx == NULL)
98                 return 1;
99
100         if (lzma_index_append(idx, NULL, block.header_size + strm.total_out,
101                         strm.total_in) != LZMA_OK)
102                 return 1;
103
104         if (lzma_index_encoder(&strm, idx) != LZMA_OK)
105                 return 1;
106
107         if (lzma_code(&strm, LZMA_RUN) != LZMA_STREAM_END)
108                 return 1;
109
110         out_size += strm.total_out;
111
112         lzma_end(&strm);
113
114         lzma_index_end(idx, NULL);
115
116         // Encode the Stream Header and Stream Footer. backwards_size is
117         // needed only for the Stream Footer.
118         lzma_stream_flags sf = {
119                 .backward_size = strm.total_out,
120                 .check = block.check,
121         };
122
123         if (lzma_stream_header_encode(&sf, out) != LZMA_OK)
124                 return 1;
125
126         if (lzma_stream_footer_encode(&sf, out + out_size) != LZMA_OK)
127                 return 1;
128
129         out_size += LZMA_STREAM_HEADER_SIZE;
130
131         // Write out the file.
132         fwrite(out, 1, out_size, stdout);
133
134         return 0;
135 }