1 ///////////////////////////////////////////////////////////////////////////////
3 /// \file 01_compress_easy.c
4 /// \brief Compress from stdin to stdout in multi-call mode
6 /// Usage: ./01_compress_easy PRESET < INFILE > OUTFILE
8 /// Example: ./01_compress_easy 6 < foo > foo.xz
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 ///////////////////////////////////////////////////////////////////////////////
26 show_usage_and_exit(const char *argv0
)
28 fprintf(stderr
, "Usage: %s PRESET < INFILE > OUTFILE\n"
29 "PRESET is a number 0-9 and can optionally be "
30 "followed by `e' to indicate extreme preset\n",
37 get_preset(int argc
, char **argv
)
39 // One argument whose first char must be 0-9.
40 if (argc
!= 2 || argv
[1][0] < '0' || argv
[1][0] > '9')
41 show_usage_and_exit(argv
[0]);
43 // Calculate the preste level 0-9.
44 uint32_t preset
= argv
[1][0] - '0';
46 // If there is a second char, it must be 'e'. It will set
47 // the LZMA_PRESET_EXTREME flag.
48 if (argv
[1][1] != '\0') {
49 if (argv
[1][1] != 'e' || argv
[1][2] != '\0')
50 show_usage_and_exit(argv
[0]);
52 preset
|= LZMA_PRESET_EXTREME
;
60 init_encoder(lzma_stream
*strm
, uint32_t preset
)
62 // Initialize the encoder using a preset. Set the integrity to check
63 // to CRC64, which is the default in the xz command line tool. If
64 // the .xz file needs to be decompressed with XZ Embedded, use
65 // LZMA_CHECK_CRC32 instead.
66 lzma_ret ret
= lzma_easy_encoder(strm
, preset
, LZMA_CHECK_CRC64
);
68 // Return successfully if the initialization went fine.
72 // Something went wrong. The possible errors are documented in
73 // lzma/container.h (src/liblzma/api/lzma/container.h in the source
74 // package or e.g. /usr/include/lzma/container.h depending on the
79 msg
= "Memory allocation failed";
82 case LZMA_OPTIONS_ERROR
:
83 msg
= "Specified preset is not supported";
86 case LZMA_UNSUPPORTED_CHECK
:
87 msg
= "Specified integrity check is not supported";
91 // This is most likely LZMA_PROG_ERROR indicating a bug in
92 // this program or in liblzma. It is inconvenient to have a
93 // separate error message for errors that should be impossible
94 // to occur, but knowing the error code is important for
95 // debugging. That's why it is good to print the error code
96 // at least when there is no good error message to show.
97 msg
= "Unknown error, possibly a bug";
101 fprintf(stderr
, "Error initializing the encoder: %s (error code %u)\n",
108 compress(lzma_stream
*strm
, FILE *infile
, FILE *outfile
)
110 // This will be LZMA_RUN until the end of the input file is reached.
111 // This tells lzma_code() when there will be no more input.
112 lzma_action action
= LZMA_RUN
;
114 // Buffers to temporarily hold uncompressed input
115 // and compressed output.
116 uint8_t inbuf
[BUFSIZ
];
117 uint8_t outbuf
[BUFSIZ
];
119 // Initialize the input and output pointers. Initializing next_in
120 // and avail_in isn't really necessary when we are going to encode
121 // just one file since LZMA_STREAM_INIT takes care of initializing
122 // those already. But it doesn't hurt much and it will be needed
123 // if encoding more than one file like we will in 02_decompress.c.
125 // While we don't care about strm->total_in or strm->total_out in this
126 // example, it is worth noting that initializing the encoder will
127 // always reset total_in and total_out to zero. But the encoder
128 // initialization doesn't touch next_in, avail_in, next_out, or
130 strm
->next_in
= NULL
;
132 strm
->next_out
= outbuf
;
133 strm
->avail_out
= sizeof(outbuf
);
135 // Loop until the file has been successfully compressed or until
138 // Fill the input buffer if it is empty.
139 if (strm
->avail_in
== 0 && !feof(infile
)) {
140 strm
->next_in
= inbuf
;
141 strm
->avail_in
= fread(inbuf
, 1, sizeof(inbuf
),
144 if (ferror(infile
)) {
145 fprintf(stderr
, "Read error: %s\n",
150 // Once the end of the input file has been reached,
151 // we need to tell lzma_code() that no more input
152 // will be coming and that it should finish the
155 action
= LZMA_FINISH
;
158 // Tell liblzma do the actual encoding.
160 // This reads up to strm->avail_in bytes of input starting
161 // from strm->next_in. avail_in will be decremented and
162 // next_in incremented by an equal amount to match the
163 // number of input bytes consumed.
165 // Up to strm->avail_out bytes of compressed output will be
166 // written starting from strm->next_out. avail_out and next_out
167 // will be incremented by an equal amount to match the number
168 // of output bytes written.
170 // The encoder has to do internal buffering, which means that
171 // it may take quite a bit of input before the same data is
172 // available in compressed form in the output buffer.
173 lzma_ret ret
= lzma_code(strm
, action
);
175 // If the output buffer is full or if the compression finished
176 // successfully, write the data from the output buffer to
178 if (strm
->avail_out
== 0 || ret
== LZMA_STREAM_END
) {
179 // When lzma_code() has returned LZMA_STREAM_END,
180 // the output buffer is likely to be only partially
181 // full. Calculate how much new data there is to
182 // be written to the output file.
183 size_t write_size
= sizeof(outbuf
) - strm
->avail_out
;
185 if (fwrite(outbuf
, 1, write_size
, outfile
)
187 fprintf(stderr
, "Write error: %s\n",
192 // Reset next_out and avail_out.
193 strm
->next_out
= outbuf
;
194 strm
->avail_out
= sizeof(outbuf
);
197 // Normally the return value of lzma_code() will be LZMA_OK
198 // until everything has been encoded.
199 if (ret
!= LZMA_OK
) {
200 // Once everything has been encoded successfully, the
201 // return value of lzma_code() will be LZMA_STREAM_END.
203 // It is important to check for LZMA_STREAM_END. Do not
204 // assume that getting ret != LZMA_OK would mean that
205 // everything has gone well.
206 if (ret
== LZMA_STREAM_END
)
209 // It's not LZMA_OK nor LZMA_STREAM_END,
210 // so it must be an error code. See lzma/base.h
211 // (src/liblzma/api/lzma/base.h in the source package
212 // or e.g. /usr/include/lzma/base.h depending on the
213 // install prefix) for the list and documentation of
214 // possible values. Most values listen in lzma_ret
215 // enumeration aren't possible in this example.
219 msg
= "Memory allocation failed";
222 case LZMA_DATA_ERROR
:
223 // This error is returned if the compressed
224 // or uncompressed size get near 8 EiB
225 // (2^63 bytes) because that's where the .xz
226 // file format size limits currently are.
227 // That is, the possibility of this error
228 // is mostly theoretical unless you are doing
229 // something very unusual.
231 // Note that strm->total_in and strm->total_out
232 // have nothing to do with this error. Changing
233 // those variables won't increase or decrease
234 // the chance of getting this error.
235 msg
= "File size limits exceeded";
239 // This is most likely LZMA_PROG_ERROR, but
240 // if this program is buggy (or liblzma has
241 // a bug), it may be e.g. LZMA_BUF_ERROR or
242 // LZMA_OPTIONS_ERROR too.
244 // It is inconvenient to have a separate
245 // error message for errors that should be
246 // impossible to occur, but knowing the error
247 // code is important for debugging. That's why
248 // it is good to print the error code at least
249 // when there is no good error message to show.
250 msg
= "Unknown error, possibly a bug";
254 fprintf(stderr
, "Encoder error: %s (error code %u)\n",
263 main(int argc
, char **argv
)
265 // Get the preset number from the command line.
266 uint32_t preset
= get_preset(argc
, argv
);
268 // Initialize a lzma_stream structure. When it is allocated on stack,
269 // it is simplest to use LZMA_STREAM_INIT macro like below. When it
270 // is allocated on heap, using memset(strmptr, 0, sizeof(*strmptr))
271 // works (as long as NULL pointers are represented with zero bits
272 // as they are on practically all computers today).
273 lzma_stream strm
= LZMA_STREAM_INIT
;
275 // Initialize the encoder. If it succeeds, compress from
277 bool success
= init_encoder(&strm
, preset
);
279 success
= compress(&strm
, stdin
, stdout
);
281 // Free the memory allocated for the encoder. If we were encoding
282 // multiple files, this would only need to be done after the last
283 // file. See 02_decompress.c for handling of multiple files.
285 // It is OK to call lzma_end() multiple times or when it hasn't been
286 // actually used except initialized with LZMA_STREAM_INIT.
289 // Close stdout to catch possible write errors that can occur
290 // when pending data is flushed from the stdio buffers.
291 if (fclose(stdout
)) {
292 fprintf(stderr
, "Write error: %s\n", strerror(errno
));
296 return success
? EXIT_SUCCESS
: EXIT_FAILURE
;