1*0a6a1f1dSLionel Sambuc ///////////////////////////////////////////////////////////////////////////////
2*0a6a1f1dSLionel Sambuc //
3*0a6a1f1dSLionel Sambuc /// \file 01_compress_easy.c
4*0a6a1f1dSLionel Sambuc /// \brief Compress from stdin to stdout in multi-call mode
5*0a6a1f1dSLionel Sambuc ///
6*0a6a1f1dSLionel Sambuc /// Usage: ./01_compress_easy PRESET < INFILE > OUTFILE
7*0a6a1f1dSLionel Sambuc ///
8*0a6a1f1dSLionel Sambuc /// Example: ./01_compress_easy 6 < foo > foo.xz
9*0a6a1f1dSLionel Sambuc //
10*0a6a1f1dSLionel Sambuc // Author: Lasse Collin
11*0a6a1f1dSLionel Sambuc //
12*0a6a1f1dSLionel Sambuc // This file has been put into the public domain.
13*0a6a1f1dSLionel Sambuc // You can do whatever you want with this file.
14*0a6a1f1dSLionel Sambuc //
15*0a6a1f1dSLionel Sambuc ///////////////////////////////////////////////////////////////////////////////
16*0a6a1f1dSLionel Sambuc
17*0a6a1f1dSLionel Sambuc #include <stdbool.h>
18*0a6a1f1dSLionel Sambuc #include <stdlib.h>
19*0a6a1f1dSLionel Sambuc #include <stdio.h>
20*0a6a1f1dSLionel Sambuc #include <string.h>
21*0a6a1f1dSLionel Sambuc #include <errno.h>
22*0a6a1f1dSLionel Sambuc #include <lzma.h>
23*0a6a1f1dSLionel Sambuc
24*0a6a1f1dSLionel Sambuc
25*0a6a1f1dSLionel Sambuc static void
show_usage_and_exit(const char * argv0)26*0a6a1f1dSLionel Sambuc show_usage_and_exit(const char *argv0)
27*0a6a1f1dSLionel Sambuc {
28*0a6a1f1dSLionel Sambuc fprintf(stderr, "Usage: %s PRESET < INFILE > OUTFILE\n"
29*0a6a1f1dSLionel Sambuc "PRESET is a number 0-9 and can optionally be "
30*0a6a1f1dSLionel Sambuc "followed by `e' to indicate extreme preset\n",
31*0a6a1f1dSLionel Sambuc argv0);
32*0a6a1f1dSLionel Sambuc exit(EXIT_FAILURE);
33*0a6a1f1dSLionel Sambuc }
34*0a6a1f1dSLionel Sambuc
35*0a6a1f1dSLionel Sambuc
36*0a6a1f1dSLionel Sambuc static uint32_t
get_preset(int argc,char ** argv)37*0a6a1f1dSLionel Sambuc get_preset(int argc, char **argv)
38*0a6a1f1dSLionel Sambuc {
39*0a6a1f1dSLionel Sambuc // One argument whose first char must be 0-9.
40*0a6a1f1dSLionel Sambuc if (argc != 2 || argv[1][0] < '0' || argv[1][0] > '9')
41*0a6a1f1dSLionel Sambuc show_usage_and_exit(argv[0]);
42*0a6a1f1dSLionel Sambuc
43*0a6a1f1dSLionel Sambuc // Calculate the preste level 0-9.
44*0a6a1f1dSLionel Sambuc uint32_t preset = argv[1][0] - '0';
45*0a6a1f1dSLionel Sambuc
46*0a6a1f1dSLionel Sambuc // If there is a second char, it must be 'e'. It will set
47*0a6a1f1dSLionel Sambuc // the LZMA_PRESET_EXTREME flag.
48*0a6a1f1dSLionel Sambuc if (argv[1][1] != '\0') {
49*0a6a1f1dSLionel Sambuc if (argv[1][1] != 'e' || argv[1][2] != '\0')
50*0a6a1f1dSLionel Sambuc show_usage_and_exit(argv[0]);
51*0a6a1f1dSLionel Sambuc
52*0a6a1f1dSLionel Sambuc preset |= LZMA_PRESET_EXTREME;
53*0a6a1f1dSLionel Sambuc }
54*0a6a1f1dSLionel Sambuc
55*0a6a1f1dSLionel Sambuc return preset;
56*0a6a1f1dSLionel Sambuc }
57*0a6a1f1dSLionel Sambuc
58*0a6a1f1dSLionel Sambuc
59*0a6a1f1dSLionel Sambuc static bool
init_encoder(lzma_stream * strm,uint32_t preset)60*0a6a1f1dSLionel Sambuc init_encoder(lzma_stream *strm, uint32_t preset)
61*0a6a1f1dSLionel Sambuc {
62*0a6a1f1dSLionel Sambuc // Initialize the encoder using a preset. Set the integrity to check
63*0a6a1f1dSLionel Sambuc // to CRC64, which is the default in the xz command line tool. If
64*0a6a1f1dSLionel Sambuc // the .xz file needs to be decompressed with XZ Embedded, use
65*0a6a1f1dSLionel Sambuc // LZMA_CHECK_CRC32 instead.
66*0a6a1f1dSLionel Sambuc lzma_ret ret = lzma_easy_encoder(strm, preset, LZMA_CHECK_CRC64);
67*0a6a1f1dSLionel Sambuc
68*0a6a1f1dSLionel Sambuc // Return successfully if the initialization went fine.
69*0a6a1f1dSLionel Sambuc if (ret == LZMA_OK)
70*0a6a1f1dSLionel Sambuc return true;
71*0a6a1f1dSLionel Sambuc
72*0a6a1f1dSLionel Sambuc // Something went wrong. The possible errors are documented in
73*0a6a1f1dSLionel Sambuc // lzma/container.h (src/liblzma/api/lzma/container.h in the source
74*0a6a1f1dSLionel Sambuc // package or e.g. /usr/include/lzma/container.h depending on the
75*0a6a1f1dSLionel Sambuc // install prefix).
76*0a6a1f1dSLionel Sambuc const char *msg;
77*0a6a1f1dSLionel Sambuc switch (ret) {
78*0a6a1f1dSLionel Sambuc case LZMA_MEM_ERROR:
79*0a6a1f1dSLionel Sambuc msg = "Memory allocation failed";
80*0a6a1f1dSLionel Sambuc break;
81*0a6a1f1dSLionel Sambuc
82*0a6a1f1dSLionel Sambuc case LZMA_OPTIONS_ERROR:
83*0a6a1f1dSLionel Sambuc msg = "Specified preset is not supported";
84*0a6a1f1dSLionel Sambuc break;
85*0a6a1f1dSLionel Sambuc
86*0a6a1f1dSLionel Sambuc case LZMA_UNSUPPORTED_CHECK:
87*0a6a1f1dSLionel Sambuc msg = "Specified integrity check is not supported";
88*0a6a1f1dSLionel Sambuc break;
89*0a6a1f1dSLionel Sambuc
90*0a6a1f1dSLionel Sambuc default:
91*0a6a1f1dSLionel Sambuc // This is most likely LZMA_PROG_ERROR indicating a bug in
92*0a6a1f1dSLionel Sambuc // this program or in liblzma. It is inconvenient to have a
93*0a6a1f1dSLionel Sambuc // separate error message for errors that should be impossible
94*0a6a1f1dSLionel Sambuc // to occur, but knowing the error code is important for
95*0a6a1f1dSLionel Sambuc // debugging. That's why it is good to print the error code
96*0a6a1f1dSLionel Sambuc // at least when there is no good error message to show.
97*0a6a1f1dSLionel Sambuc msg = "Unknown error, possibly a bug";
98*0a6a1f1dSLionel Sambuc break;
99*0a6a1f1dSLionel Sambuc }
100*0a6a1f1dSLionel Sambuc
101*0a6a1f1dSLionel Sambuc fprintf(stderr, "Error initializing the encoder: %s (error code %u)\n",
102*0a6a1f1dSLionel Sambuc msg, ret);
103*0a6a1f1dSLionel Sambuc return false;
104*0a6a1f1dSLionel Sambuc }
105*0a6a1f1dSLionel Sambuc
106*0a6a1f1dSLionel Sambuc
107*0a6a1f1dSLionel Sambuc static bool
compress(lzma_stream * strm,FILE * infile,FILE * outfile)108*0a6a1f1dSLionel Sambuc compress(lzma_stream *strm, FILE *infile, FILE *outfile)
109*0a6a1f1dSLionel Sambuc {
110*0a6a1f1dSLionel Sambuc // This will be LZMA_RUN until the end of the input file is reached.
111*0a6a1f1dSLionel Sambuc // This tells lzma_code() when there will be no more input.
112*0a6a1f1dSLionel Sambuc lzma_action action = LZMA_RUN;
113*0a6a1f1dSLionel Sambuc
114*0a6a1f1dSLionel Sambuc // Buffers to temporarily hold uncompressed input
115*0a6a1f1dSLionel Sambuc // and compressed output.
116*0a6a1f1dSLionel Sambuc uint8_t inbuf[BUFSIZ];
117*0a6a1f1dSLionel Sambuc uint8_t outbuf[BUFSIZ];
118*0a6a1f1dSLionel Sambuc
119*0a6a1f1dSLionel Sambuc // Initialize the input and output pointers. Initializing next_in
120*0a6a1f1dSLionel Sambuc // and avail_in isn't really necessary when we are going to encode
121*0a6a1f1dSLionel Sambuc // just one file since LZMA_STREAM_INIT takes care of initializing
122*0a6a1f1dSLionel Sambuc // those already. But it doesn't hurt much and it will be needed
123*0a6a1f1dSLionel Sambuc // if encoding more than one file like we will in 02_decompress.c.
124*0a6a1f1dSLionel Sambuc //
125*0a6a1f1dSLionel Sambuc // While we don't care about strm->total_in or strm->total_out in this
126*0a6a1f1dSLionel Sambuc // example, it is worth noting that initializing the encoder will
127*0a6a1f1dSLionel Sambuc // always reset total_in and total_out to zero. But the encoder
128*0a6a1f1dSLionel Sambuc // initialization doesn't touch next_in, avail_in, next_out, or
129*0a6a1f1dSLionel Sambuc // avail_out.
130*0a6a1f1dSLionel Sambuc strm->next_in = NULL;
131*0a6a1f1dSLionel Sambuc strm->avail_in = 0;
132*0a6a1f1dSLionel Sambuc strm->next_out = outbuf;
133*0a6a1f1dSLionel Sambuc strm->avail_out = sizeof(outbuf);
134*0a6a1f1dSLionel Sambuc
135*0a6a1f1dSLionel Sambuc // Loop until the file has been successfully compressed or until
136*0a6a1f1dSLionel Sambuc // an error occurs.
137*0a6a1f1dSLionel Sambuc while (true) {
138*0a6a1f1dSLionel Sambuc // Fill the input buffer if it is empty.
139*0a6a1f1dSLionel Sambuc if (strm->avail_in == 0 && !feof(infile)) {
140*0a6a1f1dSLionel Sambuc strm->next_in = inbuf;
141*0a6a1f1dSLionel Sambuc strm->avail_in = fread(inbuf, 1, sizeof(inbuf),
142*0a6a1f1dSLionel Sambuc infile);
143*0a6a1f1dSLionel Sambuc
144*0a6a1f1dSLionel Sambuc if (ferror(infile)) {
145*0a6a1f1dSLionel Sambuc fprintf(stderr, "Read error: %s\n",
146*0a6a1f1dSLionel Sambuc strerror(errno));
147*0a6a1f1dSLionel Sambuc return false;
148*0a6a1f1dSLionel Sambuc }
149*0a6a1f1dSLionel Sambuc
150*0a6a1f1dSLionel Sambuc // Once the end of the input file has been reached,
151*0a6a1f1dSLionel Sambuc // we need to tell lzma_code() that no more input
152*0a6a1f1dSLionel Sambuc // will be coming and that it should finish the
153*0a6a1f1dSLionel Sambuc // encoding.
154*0a6a1f1dSLionel Sambuc if (feof(infile))
155*0a6a1f1dSLionel Sambuc action = LZMA_FINISH;
156*0a6a1f1dSLionel Sambuc }
157*0a6a1f1dSLionel Sambuc
158*0a6a1f1dSLionel Sambuc // Tell liblzma do the actual encoding.
159*0a6a1f1dSLionel Sambuc //
160*0a6a1f1dSLionel Sambuc // This reads up to strm->avail_in bytes of input starting
161*0a6a1f1dSLionel Sambuc // from strm->next_in. avail_in will be decremented and
162*0a6a1f1dSLionel Sambuc // next_in incremented by an equal amount to match the
163*0a6a1f1dSLionel Sambuc // number of input bytes consumed.
164*0a6a1f1dSLionel Sambuc //
165*0a6a1f1dSLionel Sambuc // Up to strm->avail_out bytes of compressed output will be
166*0a6a1f1dSLionel Sambuc // written starting from strm->next_out. avail_out and next_out
167*0a6a1f1dSLionel Sambuc // will be incremented by an equal amount to match the number
168*0a6a1f1dSLionel Sambuc // of output bytes written.
169*0a6a1f1dSLionel Sambuc //
170*0a6a1f1dSLionel Sambuc // The encoder has to do internal buffering, which means that
171*0a6a1f1dSLionel Sambuc // it may take quite a bit of input before the same data is
172*0a6a1f1dSLionel Sambuc // available in compressed form in the output buffer.
173*0a6a1f1dSLionel Sambuc lzma_ret ret = lzma_code(strm, action);
174*0a6a1f1dSLionel Sambuc
175*0a6a1f1dSLionel Sambuc // If the output buffer is full or if the compression finished
176*0a6a1f1dSLionel Sambuc // successfully, write the data from the output bufffer to
177*0a6a1f1dSLionel Sambuc // the output file.
178*0a6a1f1dSLionel Sambuc if (strm->avail_out == 0 || ret == LZMA_STREAM_END) {
179*0a6a1f1dSLionel Sambuc // When lzma_code() has returned LZMA_STREAM_END,
180*0a6a1f1dSLionel Sambuc // the output buffer is likely to be only partially
181*0a6a1f1dSLionel Sambuc // full. Calculate how much new data there is to
182*0a6a1f1dSLionel Sambuc // be written to the output file.
183*0a6a1f1dSLionel Sambuc size_t write_size = sizeof(outbuf) - strm->avail_out;
184*0a6a1f1dSLionel Sambuc
185*0a6a1f1dSLionel Sambuc if (fwrite(outbuf, 1, write_size, outfile)
186*0a6a1f1dSLionel Sambuc != write_size) {
187*0a6a1f1dSLionel Sambuc fprintf(stderr, "Write error: %s\n",
188*0a6a1f1dSLionel Sambuc strerror(errno));
189*0a6a1f1dSLionel Sambuc return false;
190*0a6a1f1dSLionel Sambuc }
191*0a6a1f1dSLionel Sambuc
192*0a6a1f1dSLionel Sambuc // Reset next_out and avail_out.
193*0a6a1f1dSLionel Sambuc strm->next_out = outbuf;
194*0a6a1f1dSLionel Sambuc strm->avail_out = sizeof(outbuf);
195*0a6a1f1dSLionel Sambuc }
196*0a6a1f1dSLionel Sambuc
197*0a6a1f1dSLionel Sambuc // Normally the return value of lzma_code() will be LZMA_OK
198*0a6a1f1dSLionel Sambuc // until everything has been encoded.
199*0a6a1f1dSLionel Sambuc if (ret != LZMA_OK) {
200*0a6a1f1dSLionel Sambuc // Once everything has been encoded successfully, the
201*0a6a1f1dSLionel Sambuc // return value of lzma_code() will be LZMA_STREAM_END.
202*0a6a1f1dSLionel Sambuc //
203*0a6a1f1dSLionel Sambuc // It is important to check for LZMA_STREAM_END. Do not
204*0a6a1f1dSLionel Sambuc // assume that getting ret != LZMA_OK would mean that
205*0a6a1f1dSLionel Sambuc // everything has gone well.
206*0a6a1f1dSLionel Sambuc if (ret == LZMA_STREAM_END)
207*0a6a1f1dSLionel Sambuc return true;
208*0a6a1f1dSLionel Sambuc
209*0a6a1f1dSLionel Sambuc // It's not LZMA_OK nor LZMA_STREAM_END,
210*0a6a1f1dSLionel Sambuc // so it must be an error code. See lzma/base.h
211*0a6a1f1dSLionel Sambuc // (src/liblzma/api/lzma/base.h in the source package
212*0a6a1f1dSLionel Sambuc // or e.g. /usr/include/lzma/base.h depending on the
213*0a6a1f1dSLionel Sambuc // install prefix) for the list and documentation of
214*0a6a1f1dSLionel Sambuc // possible values. Most values listen in lzma_ret
215*0a6a1f1dSLionel Sambuc // enumeration aren't possible in this example.
216*0a6a1f1dSLionel Sambuc const char *msg;
217*0a6a1f1dSLionel Sambuc switch (ret) {
218*0a6a1f1dSLionel Sambuc case LZMA_MEM_ERROR:
219*0a6a1f1dSLionel Sambuc msg = "Memory allocation failed";
220*0a6a1f1dSLionel Sambuc break;
221*0a6a1f1dSLionel Sambuc
222*0a6a1f1dSLionel Sambuc case LZMA_DATA_ERROR:
223*0a6a1f1dSLionel Sambuc // This error is returned if the compressed
224*0a6a1f1dSLionel Sambuc // or uncompressed size get near 8 EiB
225*0a6a1f1dSLionel Sambuc // (2^63 bytes) because that's where the .xz
226*0a6a1f1dSLionel Sambuc // file format size limits currently are.
227*0a6a1f1dSLionel Sambuc // That is, the possibility of this error
228*0a6a1f1dSLionel Sambuc // is mostly theoretical unless you are doing
229*0a6a1f1dSLionel Sambuc // something very unusual.
230*0a6a1f1dSLionel Sambuc //
231*0a6a1f1dSLionel Sambuc // Note that strm->total_in and strm->total_out
232*0a6a1f1dSLionel Sambuc // have nothing to do with this error. Changing
233*0a6a1f1dSLionel Sambuc // those variables won't increase or decrease
234*0a6a1f1dSLionel Sambuc // the chance of getting this error.
235*0a6a1f1dSLionel Sambuc msg = "File size limits exceeded";
236*0a6a1f1dSLionel Sambuc break;
237*0a6a1f1dSLionel Sambuc
238*0a6a1f1dSLionel Sambuc default:
239*0a6a1f1dSLionel Sambuc // This is most likely LZMA_PROG_ERROR, but
240*0a6a1f1dSLionel Sambuc // if this program is buggy (or liblzma has
241*0a6a1f1dSLionel Sambuc // a bug), it may be e.g. LZMA_BUF_ERROR or
242*0a6a1f1dSLionel Sambuc // LZMA_OPTIONS_ERROR too.
243*0a6a1f1dSLionel Sambuc //
244*0a6a1f1dSLionel Sambuc // It is inconvenient to have a separate
245*0a6a1f1dSLionel Sambuc // error message for errors that should be
246*0a6a1f1dSLionel Sambuc // impossible to occur, but knowing the error
247*0a6a1f1dSLionel Sambuc // code is important for debugging. That's why
248*0a6a1f1dSLionel Sambuc // it is good to print the error code at least
249*0a6a1f1dSLionel Sambuc // when there is no good error message to show.
250*0a6a1f1dSLionel Sambuc msg = "Unknown error, possibly a bug";
251*0a6a1f1dSLionel Sambuc break;
252*0a6a1f1dSLionel Sambuc }
253*0a6a1f1dSLionel Sambuc
254*0a6a1f1dSLionel Sambuc fprintf(stderr, "Encoder error: %s (error code %u)\n",
255*0a6a1f1dSLionel Sambuc msg, ret);
256*0a6a1f1dSLionel Sambuc return false;
257*0a6a1f1dSLionel Sambuc }
258*0a6a1f1dSLionel Sambuc }
259*0a6a1f1dSLionel Sambuc }
260*0a6a1f1dSLionel Sambuc
261*0a6a1f1dSLionel Sambuc
262*0a6a1f1dSLionel Sambuc extern int
main(int argc,char ** argv)263*0a6a1f1dSLionel Sambuc main(int argc, char **argv)
264*0a6a1f1dSLionel Sambuc {
265*0a6a1f1dSLionel Sambuc // Get the preset number from the command line.
266*0a6a1f1dSLionel Sambuc uint32_t preset = get_preset(argc, argv);
267*0a6a1f1dSLionel Sambuc
268*0a6a1f1dSLionel Sambuc // Initialize a lzma_stream structure. When it is allocated on stack,
269*0a6a1f1dSLionel Sambuc // it is simplest to use LZMA_STREAM_INIT macro like below. When it
270*0a6a1f1dSLionel Sambuc // is allocated on heap, using memset(strmptr, 0, sizeof(*strmptr))
271*0a6a1f1dSLionel Sambuc // works (as long as NULL pointers are represented with zero bits
272*0a6a1f1dSLionel Sambuc // as they are on practically all computers today).
273*0a6a1f1dSLionel Sambuc lzma_stream strm = LZMA_STREAM_INIT;
274*0a6a1f1dSLionel Sambuc
275*0a6a1f1dSLionel Sambuc // Initialize the encoder. If it succeeds, compress from
276*0a6a1f1dSLionel Sambuc // stdin to stdout.
277*0a6a1f1dSLionel Sambuc bool success = init_encoder(&strm, preset);
278*0a6a1f1dSLionel Sambuc if (success)
279*0a6a1f1dSLionel Sambuc success = compress(&strm, stdin, stdout);
280*0a6a1f1dSLionel Sambuc
281*0a6a1f1dSLionel Sambuc // Free the memory allocated for the encoder. If we were encoding
282*0a6a1f1dSLionel Sambuc // multiple files, this would only need to be done after the last
283*0a6a1f1dSLionel Sambuc // file. See 02_decompress.c for handling of multiple files.
284*0a6a1f1dSLionel Sambuc //
285*0a6a1f1dSLionel Sambuc // It is OK to call lzma_end() multiple times or when it hasn't been
286*0a6a1f1dSLionel Sambuc // actually used except initialized with LZMA_STREAM_INIT.
287*0a6a1f1dSLionel Sambuc lzma_end(&strm);
288*0a6a1f1dSLionel Sambuc
289*0a6a1f1dSLionel Sambuc // Close stdout to catch possible write errors that can occur
290*0a6a1f1dSLionel Sambuc // when pending data is flushed from the stdio buffers.
291*0a6a1f1dSLionel Sambuc if (fclose(stdout)) {
292*0a6a1f1dSLionel Sambuc fprintf(stderr, "Write error: %s\n", strerror(errno));
293*0a6a1f1dSLionel Sambuc success = false;
294*0a6a1f1dSLionel Sambuc }
295*0a6a1f1dSLionel Sambuc
296*0a6a1f1dSLionel Sambuc return success ? EXIT_SUCCESS : EXIT_FAILURE;
297*0a6a1f1dSLionel Sambuc }
298