[BACK]Return to gzopen.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / compress

Annotation of src/usr.bin/compress/gzopen.c, Revision 1.12

1.12    ! millert     1: /*     $OpenBSD: gzopen.c,v 1.11 2003/07/10 00:06:50 david Exp $       */
1.1       mickey      2:
                      3: /*
                      4:  * Copyright (c) 1997 Michael Shalayeff
                      5:  * All rights reserved.
                      6:  *
                      7:  * Redistribution and use in source and binary forms, with or without
                      8:  * modification, are permitted provided that the following conditions
                      9:  * are met:
                     10:  * 1. Redistributions of source code must retain the above copyright
                     11:  *    notice, this list of conditions and the following disclaimer.
                     12:  * 2. Redistributions in binary form must reproduce the above copyright
                     13:  *    notice, this list of conditions and the following disclaimer in the
                     14:  *    documentation and/or other materials provided with the distribution.
                     15:  *
1.5       mickey     16:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
                     17:  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
1.1       mickey     18:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     19:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     20:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     21:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     22:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     23:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     24:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     25:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     26:  * SUCH DAMAGE.
                     27:  *
                     28:  */
                     29: /* this is partially derived from the zlib's gzio.c file, so the notice: */
                     30: /*
                     31:   zlib.h -- interface of the 'zlib' general purpose compression library
                     32:   version 1.0.4, Jul 24th, 1996.
                     33:
                     34:   Copyright (C) 1995-1996 Jean-loup Gailly and Mark Adler
                     35:
                     36:   This software is provided 'as-is', without any express or implied
                     37:   warranty.  In no event will the authors be held liable for any damages
                     38:   arising from the use of this software.
                     39:
                     40:   Permission is granted to anyone to use this software for any purpose,
                     41:   including commercial applications, and to alter it and redistribute it
                     42:   freely, subject to the following restrictions:
                     43:
                     44:   1. The origin of this software must not be misrepresented; you must not
                     45:      claim that you wrote the original software. If you use this software
                     46:      in a product, an acknowledgment in the product documentation would be
                     47:      appreciated but is not required.
                     48:   2. Altered source versions must be plainly marked as such, and must not be
                     49:      misrepresented as being the original software.
                     50:   3. This notice may not be removed or altered from any source distribution.
                     51:
                     52:   Jean-loup Gailly        Mark Adler
                     53:   gzip@prep.ai.mit.edu    madler@alumni.caltech.edu
                     54:
                     55:
                     56:   The data format used by the zlib library is described by RFCs (Request for
                     57:   Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
                     58:   (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
                     59: */
                     60:
1.5       mickey     61: const char gz_rcsid[] =
1.12    ! millert    62:     "$OpenBSD: gzopen.c,v 1.11 2003/07/10 00:06:50 david Exp $";
1.5       mickey     63:
1.1       mickey     64: #include <sys/types.h>
                     65: #include <sys/stat.h>
                     66: #include <stdio.h>
                     67: #include <stdlib.h>
1.11      david      68: #include <string.h>
1.1       mickey     69: #include <errno.h>
                     70: #include <unistd.h>
                     71: #include <zlib.h>
                     72: #include "compress.h"
                     73:
                     74: /* gzip flag byte */
                     75: #define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
                     76: #define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
                     77: #define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
                     78: #define ORIG_NAME    0x08 /* bit 3 set: original file name present */
                     79: #define COMMENT      0x10 /* bit 4 set: file comment present */
                     80: #define RESERVED     0xE0 /* bits 5..7: reserved */
                     81:
                     82: #define DEF_MEM_LEVEL 8
                     83: #define OS_CODE 0x03 /* unix */
                     84:
                     85: typedef
                     86: struct gz_stream {
                     87:        int     z_fd;           /* .gz file */
                     88:        z_stream z_stream;      /* libz stream */
                     89:        int     z_eof;          /* set if end of input file */
                     90:        u_char  z_buf[Z_BUFSIZE]; /* i/o buffer */
                     91:        u_int32_t z_crc;        /* crc32 of uncompressed data */
                     92:        char    z_mode;         /* 'w' or 'r' */
                     93:
                     94: } gz_stream;
                     95:
1.5       mickey     96: static const u_char gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
1.1       mickey     97:
1.3       millert    98: static int put_int32(gz_stream *, u_int32_t);
                     99: static u_int32_t get_int32(gz_stream *);
1.12    ! millert   100: static int get_header(gz_stream *, int);
1.3       millert   101: static int get_byte(gz_stream *);
1.1       mickey    102:
                    103: void *
1.12    ! millert   104: gz_open(int fd, const char *mode, int bits, int gotmagic)
1.1       mickey    105: {
                    106:        gz_stream *s;
                    107:
                    108:        if (fd < 0 || !mode)
                    109:                return NULL;
                    110:
                    111:        if ((mode[0] != 'r' && mode[0] != 'w') || mode[1] != '\0' ||
                    112:            bits < 0 || bits > Z_BEST_COMPRESSION) {
                    113:                errno = EINVAL;
                    114:                return NULL;
                    115:        }
1.4       aaron     116:        if ((s = (gz_stream *)calloc(1, sizeof(gz_stream))) == NULL)
1.1       mickey    117:                return NULL;
                    118:
                    119:        s->z_stream.zalloc = (alloc_func)0;
                    120:        s->z_stream.zfree = (free_func)0;
                    121:        s->z_stream.opaque = (voidpf)0;
                    122:        s->z_stream.next_in = Z_NULL;
                    123:        s->z_stream.next_out = Z_NULL;
                    124:        s->z_stream.avail_in = s->z_stream.avail_out = 0;
                    125:        s->z_fd = 0;
                    126:        s->z_eof = 0;
                    127:        s->z_crc = crc32(0L, Z_NULL, 0);
                    128:        s->z_mode = mode[0];
                    129:
                    130:        if (s->z_mode == 'w') {
                    131:                /* windowBits is passed < 0 to suppress zlib header */
                    132:                if (deflateInit2(&(s->z_stream), bits, Z_DEFLATED,
                    133:                                 -MAX_WBITS, DEF_MEM_LEVEL, 0) != Z_OK) {
                    134:                        free (s);
                    135:                        return NULL;
                    136:                }
                    137:                s->z_stream.next_out = s->z_buf;
                    138:        } else {
                    139:                if (inflateInit2(&(s->z_stream), -MAX_WBITS) != Z_OK) {
                    140:                        free (s);
                    141:                        return NULL;
                    142:                }
                    143:                s->z_stream.next_in = s->z_buf;
                    144:        }
                    145:        s->z_stream.avail_out = Z_BUFSIZE;
                    146:
                    147:        errno = 0;
                    148:        s->z_fd = fd;
                    149:
                    150:        if (s->z_mode == 'w') {
                    151:                u_char buf[10];
                    152:                /* Write a very simple .gz header: */
                    153:                buf[0] = gz_magic[0];
                    154:                buf[1] = gz_magic[1];
                    155:                buf[2] = Z_DEFLATED;
                    156:                buf[3] = 0 /*flags*/;
                    157:                buf[4] = buf[5] = buf[6] = buf[7] = 0 /*time*/;
                    158:                buf[8] = 0 /*xflags*/;
                    159:                buf[9] = OS_CODE;
                    160:                if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
                    161:                        gz_close(s);
                    162:                        s = NULL;
                    163:                }
                    164:        } else {
1.12    ! millert   165:                if (get_header(s, gotmagic) != 0) { /* skip the .gz header */
1.1       mickey    166:                        gz_close (s);
                    167:                        s = NULL;
                    168:                }
                    169:        }
                    170:
                    171:        return s;
                    172: }
                    173:
                    174: int
1.7       deraadt   175: gz_close(void *cookie)
1.1       mickey    176: {
1.2       mpech     177:        gz_stream *s = (gz_stream*)cookie;
1.1       mickey    178:        int err = 0;
                    179:
                    180:        if (s == NULL)
                    181:                return -1;
                    182:
                    183:        if (s->z_mode == 'w' && (err = gz_flush (s, Z_FINISH)) == Z_OK) {
                    184:                if ((err = put_int32 (s, s->z_crc)) == Z_OK)
                    185:                        err = put_int32 (s, s->z_stream.total_in);
                    186:        }
                    187:
                    188:        if (!err && s->z_stream.state != NULL) {
                    189:                if (s->z_mode == 'w')
                    190:                        err = deflateEnd(&s->z_stream);
                    191:                else if (s->z_mode == 'r')
                    192:                        err = inflateEnd(&s->z_stream);
                    193:        }
1.10      mickey    194:
                    195:        if (!err)
                    196:                err = close(s->z_fd);
                    197:        else
                    198:                (void)close(s->z_fd);
1.1       mickey    199:
                    200:        free(s);
                    201:
                    202:        return err;
                    203: }
                    204:
                    205: int
1.7       deraadt   206: gz_flush(void *cookie, int flush)
1.1       mickey    207: {
1.2       mpech     208:        gz_stream *s = (gz_stream*)cookie;
1.1       mickey    209:        size_t len;
                    210:        int done = 0;
                    211:        int err;
                    212:
                    213:        if (s == NULL || s->z_mode != 'w') {
                    214:                errno = EBADF;
                    215:                return Z_ERRNO;
                    216:        }
                    217:
                    218:        s->z_stream.avail_in = 0; /* should be zero already anyway */
                    219:
                    220:        for (;;) {
                    221:                len = Z_BUFSIZE - s->z_stream.avail_out;
                    222:
                    223:                if (len != 0) {
                    224:                        if (write(s->z_fd, s->z_buf, len) != len)
                    225:                                return Z_ERRNO;
                    226:                        s->z_stream.next_out = s->z_buf;
                    227:                        s->z_stream.avail_out = Z_BUFSIZE;
                    228:                }
                    229:                if (done)
                    230:                        break;
                    231:                if ((err = deflate(&(s->z_stream), flush)) != Z_OK &&
                    232:                    err != Z_STREAM_END)
                    233:                        return err;
                    234:
                    235:                /* deflate has finished flushing only when it hasn't
                    236:                 * used up all the available space in the output buffer
                    237:                 */
                    238:                done = (s->z_stream.avail_out != 0 || err == Z_STREAM_END);
                    239:        }
                    240:        return 0;
                    241: }
                    242:
                    243: static int
1.7       deraadt   244: put_int32(gz_stream *s, u_int32_t x)
1.1       mickey    245: {
1.8       millert   246:        u_int32_t y = htole32(x);
                    247:
                    248:        if (write(s->z_fd, &y, sizeof(y)) != sizeof(y))
1.1       mickey    249:                return Z_ERRNO;
                    250:        return 0;
                    251: }
                    252:
                    253: static int
1.7       deraadt   254: get_byte(gz_stream *s)
1.1       mickey    255: {
                    256:        if (s->z_eof)
                    257:                return EOF;
1.5       mickey    258:
1.1       mickey    259:        if (s->z_stream.avail_in == 0) {
                    260:                errno = 0;
                    261:                s->z_stream.avail_in = read(s->z_fd, s->z_buf, Z_BUFSIZE);
                    262:                if (s->z_stream.avail_in <= 0) {
                    263:                        s->z_eof = 1;
                    264:                        return EOF;
                    265:                }
                    266:                s->z_stream.next_in = s->z_buf;
                    267:        }
                    268:        s->z_stream.avail_in--;
                    269:        return *s->z_stream.next_in++;
                    270: }
                    271:
1.5       mickey    272: static u_int32_t
1.7       deraadt   273: get_int32(gz_stream *s)
1.1       mickey    274: {
1.2       mpech     275:        u_int32_t x;
1.1       mickey    276:
                    277:        x  = ((u_int32_t)(get_byte(s) & 0xff));
                    278:        x |= ((u_int32_t)(get_byte(s) & 0xff))<<8;
                    279:        x |= ((u_int32_t)(get_byte(s) & 0xff))<<16;
                    280:        x |= ((u_int32_t)(get_byte(s) & 0xff))<<24;
                    281:        return x;
                    282: }
                    283:
                    284: static int
1.12    ! millert   285: get_header(gz_stream *s, int gotmagic)
1.1       mickey    286: {
                    287:        int method; /* method byte */
                    288:        int flags;  /* flags byte */
                    289:        uInt len;
                    290:        int c;
                    291:
                    292:        /* Check the gzip magic header */
1.12    ! millert   293:        if (!gotmagic) {
        !           294:                for (len = 0; len < 2; len++) {
        !           295:                        c = get_byte(s);
        !           296:                        if (c != gz_magic[len]) {
        !           297:                                errno = EFTYPE;
        !           298:                                return -1;
        !           299:                        }
1.1       mickey    300:                }
                    301:        }
                    302:
                    303:        method = get_byte(s);
                    304:        flags = get_byte(s);
                    305:        if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
                    306:                errno = EFTYPE;
                    307:                return -1;
                    308:        }
                    309:
                    310:        /* Discard time, xflags and OS code: */
                    311:        for (len = 0; len < 6; len++)
                    312:                (void)get_byte(s);
                    313:
                    314:        if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
                    315:                len  =  (uInt)get_byte(s);
                    316:                len += ((uInt)get_byte(s))<<8;
                    317:                /* len is garbage if EOF but the loop below will quit anyway */
                    318:                while (len-- != 0 && get_byte(s) != EOF)
                    319:                        ;
                    320:        }
                    321:
                    322:        if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
                    323:                while ((c = get_byte(s)) != 0 && c != EOF) ;
                    324:        }
                    325:
                    326:        if ((flags & COMMENT) != 0) {   /* skip the .gz file comment */
                    327:                while ((c = get_byte(s)) != 0 && c != EOF) ;
                    328:        }
                    329:
                    330:        if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
                    331:                for (len = 0; len < 2; len++) (void)get_byte(s);
                    332:        }
                    333:
                    334:        if (s->z_eof) {
                    335:                errno = EFTYPE;
                    336:                return -1;
                    337:        }
                    338:
                    339:        return 0;
                    340: }
                    341:
                    342: int
1.7       deraadt   343: gz_read(void *cookie, char *buf, int len)
1.1       mickey    344: {
1.2       mpech     345:        gz_stream *s = (gz_stream*)cookie;
1.1       mickey    346:        u_char *start = buf; /* starting point for crc computation */
                    347:
                    348:        s->z_stream.next_out = buf;
                    349:        s->z_stream.avail_out = len;
                    350:
                    351:        while (s->z_stream.avail_out != 0 && !s->z_eof) {
                    352:
                    353:                if (s->z_stream.avail_in == 0) {
                    354:
                    355:                        errno = 0;
                    356:                        if ((s->z_stream.avail_in =
1.7       deraadt   357:                            read(s->z_fd, s->z_buf, Z_BUFSIZE)) == 0)
1.1       mickey    358:                                s->z_eof = 1;
                    359:                        s->z_stream.next_in = s->z_buf;
                    360:                }
                    361:
                    362:                if (inflate(&(s->z_stream), Z_NO_FLUSH) == Z_STREAM_END) {
                    363:                        /* Check CRC and original size */
                    364:                        s->z_crc = crc32(s->z_crc, start,
1.7       deraadt   365:                            (uInt)(s->z_stream.next_out - start));
1.1       mickey    366:                        start = s->z_stream.next_out;
                    367:
1.9       millert   368:                        if (get_int32(s) != s->z_crc) {
                    369:                                errno = EINVAL;
                    370:                                return -1;
                    371:                        }
                    372:                        if (get_int32(s) != s->z_stream.total_out) {
1.7       deraadt   373:                                errno = EIO;
1.1       mickey    374:                                return -1;
                    375:                        }
                    376:                        s->z_eof = 1;
                    377:                        break;
                    378:                }
                    379:        }
                    380:        s->z_crc = crc32(s->z_crc, start,
1.7       deraadt   381:            (uInt)(s->z_stream.next_out - start));
1.1       mickey    382:
                    383:        return (int)(len - s->z_stream.avail_out);
                    384: }
                    385:
                    386: int
1.7       deraadt   387: gz_write(void *cookie, const char *buf, int len)
1.1       mickey    388: {
1.2       mpech     389:        gz_stream *s = (gz_stream*)cookie;
1.1       mickey    390:
                    391:        s->z_stream.next_in = (char *)buf;
                    392:        s->z_stream.avail_in = len;
                    393:
                    394:        while (s->z_stream.avail_in != 0) {
                    395:                if (s->z_stream.avail_out == 0) {
                    396:                        if (write(s->z_fd, s->z_buf, Z_BUFSIZE) != Z_BUFSIZE)
                    397:                                break;
                    398:                        s->z_stream.next_out = s->z_buf;
                    399:                        s->z_stream.avail_out = Z_BUFSIZE;
                    400:                }
                    401:                if (deflate(&(s->z_stream), Z_NO_FLUSH) != Z_OK)
                    402:                        break;
                    403:        }
                    404:        s->z_crc = crc32(s->z_crc, buf, len);
                    405:
                    406:        return (int)(len - s->z_stream.avail_in);
                    407: }
                    408: