]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - lib/libarchive/archive_read_support_compression_xz.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / lib / libarchive / archive_read_support_compression_xz.c
1 /*-
2  * Copyright (c) 2009 Michihiro NAKAJIMA
3  * Copyright (c) 2003-2008 Tim Kientzle and Miklos Vajna
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include "archive_platform.h"
28
29 __FBSDID("$FreeBSD$");
30
31 #ifdef HAVE_ERRNO_H
32 #include <errno.h>
33 #endif
34 #include <stdio.h>
35 #ifdef HAVE_STDLIB_H
36 #include <stdlib.h>
37 #endif
38 #ifdef HAVE_STRING_H
39 #include <string.h>
40 #endif
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44 #if HAVE_LZMA_H
45 #include <lzma.h>
46 #elif HAVE_LZMADEC_H
47 #include <lzmadec.h>
48 #endif
49
50 #include "archive.h"
51 #include "archive_private.h"
52 #include "archive_read_private.h"
53
54 #if HAVE_LZMA_H && HAVE_LIBLZMA
55
56 struct private_data {
57         lzma_stream      stream;
58         unsigned char   *out_block;
59         size_t           out_block_size;
60         int64_t          total_out;
61         char             eof; /* True = found end of compressed data. */
62 };
63
64 /* Combined lzma/xz filter */
65 static ssize_t  xz_filter_read(struct archive_read_filter *, const void **);
66 static int      xz_filter_close(struct archive_read_filter *);
67 static int      xz_lzma_bidder_init(struct archive_read_filter *);
68
69 #elif HAVE_LZMADEC_H && HAVE_LIBLZMADEC
70
71 struct private_data {
72         lzmadec_stream   stream;
73         unsigned char   *out_block;
74         size_t           out_block_size;
75         int64_t          total_out;
76         char             eof; /* True = found end of compressed data. */
77 };
78
79 /* Lzma-only filter */
80 static ssize_t  lzma_filter_read(struct archive_read_filter *, const void **);
81 static int      lzma_filter_close(struct archive_read_filter *);
82 #endif
83
84 /*
85  * Note that we can detect xz and lzma compressed files even if we
86  * can't decompress them.  (In fact, we like detecting them because we
87  * can give better error messages.)  So the bid framework here gets
88  * compiled even if no lzma library is available.
89  */
90 static int      xz_bidder_bid(struct archive_read_filter_bidder *,
91                     struct archive_read_filter *);
92 static int      xz_bidder_init(struct archive_read_filter *);
93 static int      lzma_bidder_bid(struct archive_read_filter_bidder *,
94                     struct archive_read_filter *);
95 static int      lzma_bidder_init(struct archive_read_filter *);
96
97 int
98 archive_read_support_compression_xz(struct archive *_a)
99 {
100         struct archive_read *a = (struct archive_read *)_a;
101         struct archive_read_filter_bidder *bidder = __archive_read_get_bidder(a);
102
103         archive_clear_error(_a);
104         if (bidder == NULL)
105                 return (ARCHIVE_FATAL);
106
107         bidder->data = NULL;
108         bidder->bid = xz_bidder_bid;
109         bidder->init = xz_bidder_init;
110         bidder->options = NULL;
111         bidder->free = NULL;
112 #if HAVE_LZMA_H && HAVE_LIBLZMA
113         return (ARCHIVE_OK);
114 #else
115         archive_set_error(_a, ARCHIVE_ERRNO_MISC,
116             "Using external unxz program for xz decompression");
117         return (ARCHIVE_WARN);
118 #endif
119 }
120
121 int
122 archive_read_support_compression_lzma(struct archive *_a)
123 {
124         struct archive_read *a = (struct archive_read *)_a;
125         struct archive_read_filter_bidder *bidder = __archive_read_get_bidder(a);
126
127         archive_clear_error(_a);
128         if (bidder == NULL)
129                 return (ARCHIVE_FATAL);
130
131         bidder->data = NULL;
132         bidder->bid = lzma_bidder_bid;
133         bidder->init = lzma_bidder_init;
134         bidder->options = NULL;
135         bidder->free = NULL;
136 #if HAVE_LZMA_H && HAVE_LIBLZMA
137         return (ARCHIVE_OK);
138 #elif HAVE_LZMADEC_H && HAVE_LIBLZMADEC
139         return (ARCHIVE_OK);
140 #else
141         archive_set_error(_a, ARCHIVE_ERRNO_MISC,
142             "Using external unlzma program for lzma decompression");
143         return (ARCHIVE_WARN);
144 #endif
145 }
146
147 /*
148  * Test whether we can handle this data.
149  */
150 static int
151 xz_bidder_bid(struct archive_read_filter_bidder *self,
152     struct archive_read_filter *filter)
153 {
154         const unsigned char *buffer;
155         ssize_t avail;
156         int bits_checked;
157
158         (void)self; /* UNUSED */
159
160         buffer = __archive_read_filter_ahead(filter, 6, &avail);
161         if (buffer == NULL)
162                 return (0);
163
164         /*
165          * Verify Header Magic Bytes : FD 37 7A 58 5A 00
166          */
167         bits_checked = 0;
168         if (buffer[0] != 0xFD)
169                 return (0);
170         bits_checked += 8;
171         if (buffer[1] != 0x37)
172                 return (0);
173         bits_checked += 8;
174         if (buffer[2] != 0x7A)
175                 return (0);
176         bits_checked += 8;
177         if (buffer[3] != 0x58)
178                 return (0);
179         bits_checked += 8;
180         if (buffer[4] != 0x5A)
181                 return (0);
182         bits_checked += 8;
183         if (buffer[5] != 0x00)
184                 return (0);
185         bits_checked += 8;
186
187         return (bits_checked);
188 }
189
190 /*
191  * Test whether we can handle this data.
192  *
193  * <sigh> LZMA has a rather poor file signature.  Zeros do not
194  * make good signature bytes as a rule, and the only non-zero byte
195  * here is an ASCII character.  For example, an uncompressed tar
196  * archive whose first file is ']' would satisfy this check.  It may
197  * be necessary to exclude LZMA from compression_all() because of
198  * this.  Clients of libarchive would then have to explicitly enable
199  * LZMA checking instead of (or in addition to) compression_all() when
200  * they have other evidence (file name, command-line option) to go on.
201  */
202 static int
203 lzma_bidder_bid(struct archive_read_filter_bidder *self,
204     struct archive_read_filter *filter)
205 {
206         const unsigned char *buffer;
207         ssize_t avail;
208         int bits_checked;
209
210         (void)self; /* UNUSED */
211
212         buffer = __archive_read_filter_ahead(filter, 6, &avail);
213         if (buffer == NULL)
214                 return (0);
215
216         /* First byte of raw LZMA stream is always 0x5d. */
217         bits_checked = 0;
218         if (buffer[0] != 0x5d)
219                 return (0);
220         bits_checked += 8;
221
222         /* Second through fifth bytes are dictionary code, stored in
223          * little-endian order.  The two least-significant bytes are
224          * always zero. */
225         if (buffer[1] != 0 || buffer[2] != 0)
226                 return (0);
227         bits_checked += 16;
228
229         /* ??? TODO:  Fix this. ??? */
230         /* NSIS format check uses this, but I've seen tar.lzma
231          * archives where this byte is 0xff, not 0.  Can it
232          * ever be anything other than 0 or 0xff?
233          */
234 #if 0
235         if (buffer[5] != 0)
236                 return (0);
237         bits_checked += 8;
238 #endif
239
240         /* TODO: The above test is still very weak.  It would be
241          * good to do better. */
242
243         return (bits_checked);
244 }
245
246 #if HAVE_LZMA_H && HAVE_LIBLZMA
247
248 /*
249  * liblzma 4.999.7 and later support both lzma and xz streams.
250  */
251 static int
252 xz_bidder_init(struct archive_read_filter *self)
253 {
254         self->code = ARCHIVE_COMPRESSION_XZ;
255         self->name = "xz";
256         return (xz_lzma_bidder_init(self));
257 }
258
259 static int
260 lzma_bidder_init(struct archive_read_filter *self)
261 {
262         self->code = ARCHIVE_COMPRESSION_LZMA;
263         self->name = "lzma";
264         return (xz_lzma_bidder_init(self));
265 }
266
267 /*
268  * Setup the callbacks.
269  */
270 static int
271 xz_lzma_bidder_init(struct archive_read_filter *self)
272 {
273         static const size_t out_block_size = 64 * 1024;
274         void *out_block;
275         struct private_data *state;
276         int ret;
277
278         state = (struct private_data *)calloc(sizeof(*state), 1);
279         out_block = (unsigned char *)malloc(out_block_size);
280         if (state == NULL || out_block == NULL) {
281                 archive_set_error(&self->archive->archive, ENOMEM,
282                     "Can't allocate data for xz decompression");
283                 free(out_block);
284                 free(state);
285                 return (ARCHIVE_FATAL);
286         }
287
288         self->data = state;
289         state->out_block_size = out_block_size;
290         state->out_block = out_block;
291         self->read = xz_filter_read;
292         self->skip = NULL; /* not supported */
293         self->close = xz_filter_close;
294
295         state->stream.avail_in = 0;
296
297         state->stream.next_out = state->out_block;
298         state->stream.avail_out = state->out_block_size;
299
300         /* Initialize compression library.
301          * TODO: I don't know what value is best for memlimit.
302          *       maybe, it needs to check memory size which
303          *       running system has.
304          */
305         if (self->code == ARCHIVE_COMPRESSION_XZ)
306                 ret = lzma_stream_decoder(&(state->stream),
307                     (1U << 23) + (1U << 21),/* memlimit */
308                     LZMA_CONCATENATED);
309         else
310                 ret = lzma_alone_decoder(&(state->stream),
311                     (1U << 23) + (1U << 21));/* memlimit */
312
313         if (ret == LZMA_OK)
314                 return (ARCHIVE_OK);
315
316         /* Library setup failed: Choose an error message and clean up. */
317         switch (ret) {
318         case LZMA_MEM_ERROR:
319                 archive_set_error(&self->archive->archive, ENOMEM,
320                     "Internal error initializing compression library: "
321                     "Cannot allocate memory");
322                 break;
323         case LZMA_OPTIONS_ERROR:
324                 archive_set_error(&self->archive->archive,
325                     ARCHIVE_ERRNO_MISC,
326                     "Internal error initializing compression library: "
327                     "Invalid or unsupported options");
328                 break;
329         default:
330                 archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
331                     "Internal error initializing lzma library");
332                 break;
333         }
334
335         free(state->out_block);
336         free(state);
337         self->data = NULL;
338         return (ARCHIVE_FATAL);
339 }
340
341 /*
342  * Return the next block of decompressed data.
343  */
344 static ssize_t
345 xz_filter_read(struct archive_read_filter *self, const void **p)
346 {
347         struct private_data *state;
348         size_t decompressed;
349         ssize_t avail_in;
350         int ret;
351
352         state = (struct private_data *)self->data;
353
354         /* Empty our output buffer. */
355         state->stream.next_out = state->out_block;
356         state->stream.avail_out = state->out_block_size;
357
358         /* Try to fill the output buffer. */
359         while (state->stream.avail_out > 0 && !state->eof) {
360                 state->stream.next_in =
361                     __archive_read_filter_ahead(self->upstream, 1, &avail_in);
362                 if (state->stream.next_in == NULL && avail_in < 0)
363                         return (ARCHIVE_FATAL);
364                 state->stream.avail_in = avail_in;
365
366                 /* Decompress as much as we can in one pass. */
367                 ret = lzma_code(&(state->stream),
368                     (state->stream.avail_in == 0)? LZMA_FINISH: LZMA_RUN);
369                 switch (ret) {
370                 case LZMA_STREAM_END: /* Found end of stream. */
371                         state->eof = 1;
372                         /* FALL THROUGH */
373                 case LZMA_OK: /* Decompressor made some progress. */
374                         __archive_read_filter_consume(self->upstream,
375                             avail_in - state->stream.avail_in);
376                         break;
377                 case LZMA_MEM_ERROR:
378                         archive_set_error(&self->archive->archive, ENOMEM,
379                             "Lzma library error: Cannot allocate memory");
380                         return (ARCHIVE_FATAL);
381                 case LZMA_MEMLIMIT_ERROR:
382                         archive_set_error(&self->archive->archive, ENOMEM,
383                             "Lzma library error: Out of memory");
384                         return (ARCHIVE_FATAL);
385                 case LZMA_FORMAT_ERROR:
386                         archive_set_error(&self->archive->archive,
387                             ARCHIVE_ERRNO_MISC,
388                             "Lzma library error: format not recognized");
389                         return (ARCHIVE_FATAL);
390                 case LZMA_OPTIONS_ERROR:
391                         archive_set_error(&self->archive->archive,
392                             ARCHIVE_ERRNO_MISC,
393                             "Lzma library error: Invalid options");
394                         return (ARCHIVE_FATAL);
395                 case LZMA_DATA_ERROR:
396                         archive_set_error(&self->archive->archive,
397                             ARCHIVE_ERRNO_MISC,
398                             "Lzma library error: Corrupted input data");
399                         return (ARCHIVE_FATAL);
400                 case LZMA_BUF_ERROR:
401                         archive_set_error(&self->archive->archive,
402                             ARCHIVE_ERRNO_MISC,
403                             "Lzma library error:  No progress is possible");
404                         return (ARCHIVE_FATAL);
405                 default:
406                         /* Return an error. */
407                         archive_set_error(&self->archive->archive,
408                             ARCHIVE_ERRNO_MISC,
409                             "Lzma decompression failed:  Unknown error");
410                         return (ARCHIVE_FATAL);
411                 }
412         }
413
414         decompressed = state->stream.next_out - state->out_block;
415         state->total_out += decompressed;
416         if (decompressed == 0)
417                 *p = NULL;
418         else
419                 *p = state->out_block;
420         return (decompressed);
421 }
422
423 /*
424  * Clean up the decompressor.
425  */
426 static int
427 xz_filter_close(struct archive_read_filter *self)
428 {
429         struct private_data *state;
430
431         state = (struct private_data *)self->data;
432         lzma_end(&(state->stream));
433         free(state->out_block);
434         free(state);
435         return (ARCHIVE_OK);
436 }
437
438 #else
439
440 #if HAVE_LZMADEC_H && HAVE_LIBLZMADEC
441
442 /*
443  * If we have the older liblzmadec library, then we can handle
444  * LZMA streams but not XZ streams.
445  */
446
447 /*
448  * Setup the callbacks.
449  */
450 static int
451 lzma_bidder_init(struct archive_read_filter *self)
452 {
453         static const size_t out_block_size = 64 * 1024;
454         void *out_block;
455         struct private_data *state;
456         ssize_t ret, avail_in;
457
458         self->code = ARCHIVE_COMPRESSION_LZMA;
459         self->name = "lzma";
460
461         state = (struct private_data *)calloc(sizeof(*state), 1);
462         out_block = (unsigned char *)malloc(out_block_size);
463         if (state == NULL || out_block == NULL) {
464                 archive_set_error(&self->archive->archive, ENOMEM,
465                     "Can't allocate data for lzma decompression");
466                 free(out_block);
467                 free(state);
468                 return (ARCHIVE_FATAL);
469         }
470
471         self->data = state;
472         state->out_block_size = out_block_size;
473         state->out_block = out_block;
474         self->read = lzma_filter_read;
475         self->skip = NULL; /* not supported */
476         self->close = lzma_filter_close;
477
478         /* Prime the lzma library with 18 bytes of input. */
479         state->stream.next_in = (unsigned char *)(uintptr_t)
480             __archive_read_filter_ahead(self->upstream, 18, &avail_in);
481         if (state->stream.next_in == NULL)
482                 return (ARCHIVE_FATAL);
483         state->stream.avail_in = avail_in;
484         state->stream.next_out = state->out_block;
485         state->stream.avail_out = state->out_block_size;
486
487         /* Initialize compression library. */
488         ret = lzmadec_init(&(state->stream));
489         __archive_read_filter_consume(self->upstream,
490             avail_in - state->stream.avail_in);
491         if (ret == LZMADEC_OK)
492                 return (ARCHIVE_OK);
493
494         /* Library setup failed: Clean up. */
495         archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC,
496             "Internal error initializing lzma library");
497
498         /* Override the error message if we know what really went wrong. */
499         switch (ret) {
500         case LZMADEC_HEADER_ERROR:
501                 archive_set_error(&self->archive->archive,
502                     ARCHIVE_ERRNO_MISC,
503                     "Internal error initializing compression library: "
504                     "invalid header");
505                 break;
506         case LZMADEC_MEM_ERROR:
507                 archive_set_error(&self->archive->archive, ENOMEM,
508                     "Internal error initializing compression library: "
509                     "out of memory");
510                 break;
511         }
512
513         free(state->out_block);
514         free(state);
515         self->data = NULL;
516         return (ARCHIVE_FATAL);
517 }
518
519 /*
520  * Return the next block of decompressed data.
521  */
522 static ssize_t
523 lzma_filter_read(struct archive_read_filter *self, const void **p)
524 {
525         struct private_data *state;
526         size_t decompressed;
527         ssize_t avail_in, ret;
528
529         state = (struct private_data *)self->data;
530
531         /* Empty our output buffer. */
532         state->stream.next_out = state->out_block;
533         state->stream.avail_out = state->out_block_size;
534
535         /* Try to fill the output buffer. */
536         while (state->stream.avail_out > 0 && !state->eof) {
537                 state->stream.next_in = (unsigned char *)(uintptr_t)
538                     __archive_read_filter_ahead(self->upstream, 1, &avail_in);
539                 if (state->stream.next_in == NULL && avail_in < 0)
540                         return (ARCHIVE_FATAL);
541                 state->stream.avail_in = avail_in;
542
543                 /* Decompress as much as we can in one pass. */
544                 ret = lzmadec_decode(&(state->stream), avail_in == 0);
545                 switch (ret) {
546                 case LZMADEC_STREAM_END: /* Found end of stream. */
547                         state->eof = 1;
548                         /* FALL THROUGH */
549                 case LZMADEC_OK: /* Decompressor made some progress. */
550                         __archive_read_filter_consume(self->upstream,
551                             avail_in - state->stream.avail_in);
552                         break;
553                 case LZMADEC_BUF_ERROR: /* Insufficient input data? */
554                         archive_set_error(&self->archive->archive,
555                             ARCHIVE_ERRNO_MISC,
556                             "Insufficient compressed data");
557                         return (ARCHIVE_FATAL);
558                 default:
559                         /* Return an error. */
560                         archive_set_error(&self->archive->archive,
561                             ARCHIVE_ERRNO_MISC,
562                             "Lzma decompression failed");
563                         return (ARCHIVE_FATAL);
564                 }
565         }
566
567         decompressed = state->stream.next_out - state->out_block;
568         state->total_out += decompressed;
569         if (decompressed == 0)
570                 *p = NULL;
571         else
572                 *p = state->out_block;
573         return (decompressed);
574 }
575
576 /*
577  * Clean up the decompressor.
578  */
579 static int
580 lzma_filter_close(struct archive_read_filter *self)
581 {
582         struct private_data *state;
583         int ret;
584
585         state = (struct private_data *)self->data;
586         ret = ARCHIVE_OK;
587         switch (lzmadec_end(&(state->stream))) {
588         case LZMADEC_OK:
589                 break;
590         default:
591                 archive_set_error(&(self->archive->archive),
592                     ARCHIVE_ERRNO_MISC,
593                     "Failed to clean up %s compressor",
594                     self->archive->archive.compression_name);
595                 ret = ARCHIVE_FATAL;
596         }
597
598         free(state->out_block);
599         free(state);
600         return (ret);
601 }
602
603 #else
604
605 /*
606  *
607  * If we have no suitable library on this system, we can't actually do
608  * the decompression.  We can, however, still detect compressed
609  * archives and emit a useful message.
610  *
611  */
612 static int
613 lzma_bidder_init(struct archive_read_filter *self)
614 {
615         int r;
616
617         r = __archive_read_program(self, "unlzma");
618         /* Note: We set the format here even if __archive_read_program()
619          * above fails.  We do, after all, know what the format is
620          * even if we weren't able to read it. */
621         self->code = ARCHIVE_COMPRESSION_LZMA;
622         self->name = "lzma";
623         return (r);
624 }
625
626 #endif /* HAVE_LZMADEC_H */
627
628
629 static int
630 xz_bidder_init(struct archive_read_filter *self)
631 {
632         int r;
633
634         r = __archive_read_program(self, "unxz");
635         /* Note: We set the format here even if __archive_read_program()
636          * above fails.  We do, after all, know what the format is
637          * even if we weren't able to read it. */
638         self->code = ARCHIVE_COMPRESSION_XZ;
639         self->name = "xz";
640         return (r);
641 }
642
643
644 #endif /* HAVE_LZMA_H */