Home | History | Annotate | Line # | Download | only in gzip
unbzip2.c revision 1.1.2.4.2.2
      1  1.1.2.4.2.2    jmc /*	$NetBSD: unbzip2.c,v 1.1.2.4.2.2 2005/12/14 04:06:59 jmc Exp $	*/
      2          1.1    mrg 
      3          1.1    mrg /* This file is #included by gzip.c */
      4          1.1    mrg 
      5          1.1    mrg static off_t
      6      1.1.2.3    jmc unbzip2(int in, int out, char *pre, size_t prelen, off_t *bytes_in)
      7          1.1    mrg {
      8      1.1.2.3    jmc 	int		ret, end_of_file;
      9  1.1.2.4.2.1   tron 	size_t		n = 0;
     10          1.1    mrg 	off_t		bytes_out = 0;
     11      1.1.2.2  grant 	bz_stream	bzs;
     12      1.1.2.2  grant 	static char	*inbuf, *outbuf;
     13          1.1    mrg 
     14  1.1.2.4.2.1   tron 	if (inbuf == NULL)
     15  1.1.2.4.2.1   tron 		inbuf = malloc(BUFLEN);
     16  1.1.2.4.2.1   tron 	if (outbuf == NULL)
     17  1.1.2.4.2.1   tron 		outbuf = malloc(BUFLEN);
     18  1.1.2.4.2.1   tron 	if (inbuf == NULL || outbuf == NULL)
     19      1.1.2.4   tron 	        maybe_err("malloc");
     20      1.1.2.2  grant 
     21      1.1.2.2  grant 	bzs.bzalloc = NULL;
     22      1.1.2.2  grant 	bzs.bzfree = NULL;
     23      1.1.2.2  grant 	bzs.opaque = NULL;
     24      1.1.2.2  grant 
     25      1.1.2.2  grant 	end_of_file = 0;
     26      1.1.2.2  grant 	ret = BZ2_bzDecompressInit(&bzs, 0, 0);
     27      1.1.2.2  grant 	if (ret != BZ_OK)
     28      1.1.2.4   tron 	        maybe_errx("bzip2 init");
     29      1.1.2.2  grant 
     30      1.1.2.3    jmc 	/* Prepend. */
     31      1.1.2.3    jmc 	bzs.avail_in = prelen;
     32      1.1.2.3    jmc 	bzs.next_in = pre;
     33      1.1.2.3    jmc 
     34      1.1.2.3    jmc 	if (bytes_in)
     35      1.1.2.3    jmc 		*bytes_in = prelen;
     36      1.1.2.2  grant 
     37  1.1.2.4.2.2    jmc 	while (ret >= BZ_OK && ret != BZ_STREAM_END) {
     38      1.1.2.2  grant 	        if (bzs.avail_in == 0 && !end_of_file) {
     39      1.1.2.3    jmc 	                n = read(in, inbuf, BUFLEN);
     40      1.1.2.2  grant 	                if (n < 0)
     41      1.1.2.4   tron 	                        maybe_err("read");
     42      1.1.2.2  grant 	                if (n == 0)
     43      1.1.2.2  grant 	                        end_of_file = 1;
     44      1.1.2.2  grant 	                bzs.next_in = inbuf;
     45      1.1.2.2  grant 	                bzs.avail_in = n;
     46      1.1.2.3    jmc 			if (bytes_in)
     47      1.1.2.3    jmc 				*bytes_in += n;
     48      1.1.2.3    jmc 	        }
     49      1.1.2.2  grant 
     50      1.1.2.2  grant 	        bzs.next_out = outbuf;
     51      1.1.2.3    jmc 	        bzs.avail_out = BUFLEN;
     52      1.1.2.2  grant 	        ret = BZ2_bzDecompress(&bzs);
     53      1.1.2.2  grant 
     54      1.1.2.2  grant 	        switch (ret) {
     55      1.1.2.2  grant 	        case BZ_STREAM_END:
     56      1.1.2.2  grant 	        case BZ_OK:
     57      1.1.2.2  grant 	                if (ret == BZ_OK && end_of_file)
     58      1.1.2.4   tron 	                        maybe_err("read");
     59      1.1.2.2  grant 	                if (!tflag) {
     60      1.1.2.3    jmc 	                        n = write(out, outbuf, BUFLEN - bzs.avail_out);
     61      1.1.2.2  grant 	                        if (n < 0)
     62      1.1.2.4   tron 	                                maybe_err("write");
     63      1.1.2.2  grant 	                }
     64      1.1.2.2  grant 	                bytes_out += n;
     65      1.1.2.2  grant 	                break;
     66      1.1.2.2  grant 
     67      1.1.2.2  grant 	        case BZ_DATA_ERROR:
     68  1.1.2.4.2.1   tron 	                maybe_warnx("bzip2 data integrity error");
     69      1.1.2.4   tron 			break;
     70      1.1.2.4   tron 
     71      1.1.2.2  grant 	        case BZ_DATA_ERROR_MAGIC:
     72  1.1.2.4.2.1   tron 	                maybe_warnx("bzip2 magic number error");
     73      1.1.2.4   tron 			break;
     74      1.1.2.4   tron 
     75      1.1.2.2  grant 	        case BZ_MEM_ERROR:
     76  1.1.2.4.2.1   tron 	                maybe_warnx("bzip2 out of memory");
     77      1.1.2.4   tron 			break;
     78      1.1.2.4   tron 
     79      1.1.2.2  grant 	        }
     80      1.1.2.2  grant 	}
     81          1.1    mrg 
     82      1.1.2.4   tron 	if (ret != BZ_STREAM_END || BZ2_bzDecompressEnd(&bzs) != BZ_OK)
     83      1.1.2.4   tron 	        return (-1);
     84          1.1    mrg 
     85          1.1    mrg 	return (bytes_out);
     86          1.1    mrg }
     87