[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.11

1.11    ! david       1: /*     $OpenBSD: gzopen.c,v 1.10 2003/07/08 00:30:12 mickey 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.11    ! david      62:     "$OpenBSD: gzopen.c,v 1.10 2003/07/08 00:30:12 mickey 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 *);
                    100: static int get_header(gz_stream *);
                    101: static int get_byte(gz_stream *);
1.1       mickey    102:
                    103: int
1.7       deraadt   104: gz_check_header(int fd, struct stat *sb, const char *ofn)
1.1       mickey    105: {
                    106:        int f;
                    107:        u_char buf[sizeof(gz_magic)];
                    108:        off_t off = lseek(fd, 0, SEEK_CUR);
                    109:
                    110:        f = (read(fd, buf, sizeof(buf)) == sizeof(buf) &&
1.7       deraadt   111:            !memcmp(buf, gz_magic, sizeof(buf)));
1.1       mickey    112:
                    113:        lseek (fd, off, SEEK_SET);
                    114:
                    115:        return f;
                    116: }
                    117:
                    118: void *
1.7       deraadt   119: gz_open(int fd, const char *mode, int bits)
1.1       mickey    120: {
                    121:        gz_stream *s;
                    122:
                    123:        if (fd < 0 || !mode)
                    124:                return NULL;
                    125:
                    126:        if ((mode[0] != 'r' && mode[0] != 'w') || mode[1] != '\0' ||
                    127:            bits < 0 || bits > Z_BEST_COMPRESSION) {
                    128:                errno = EINVAL;
                    129:                return NULL;
                    130:        }
1.4       aaron     131:        if ((s = (gz_stream *)calloc(1, sizeof(gz_stream))) == NULL)
1.1       mickey    132:                return NULL;
                    133:
                    134:        s->z_stream.zalloc = (alloc_func)0;
                    135:        s->z_stream.zfree = (free_func)0;
                    136:        s->z_stream.opaque = (voidpf)0;
                    137:        s->z_stream.next_in = Z_NULL;
                    138:        s->z_stream.next_out = Z_NULL;
                    139:        s->z_stream.avail_in = s->z_stream.avail_out = 0;
                    140:        s->z_fd = 0;
                    141:        s->z_eof = 0;
                    142:        s->z_crc = crc32(0L, Z_NULL, 0);
                    143:        s->z_mode = mode[0];
                    144:
                    145:        if (s->z_mode == 'w') {
                    146:                /* windowBits is passed < 0 to suppress zlib header */
                    147:                if (deflateInit2(&(s->z_stream), bits, Z_DEFLATED,
                    148:                                 -MAX_WBITS, DEF_MEM_LEVEL, 0) != Z_OK) {
                    149:                        free (s);
                    150:                        return NULL;
                    151:                }
                    152:                s->z_stream.next_out = s->z_buf;
                    153:        } else {
                    154:                if (inflateInit2(&(s->z_stream), -MAX_WBITS) != Z_OK) {
                    155:                        free (s);
                    156:                        return NULL;
                    157:                }
                    158:                s->z_stream.next_in = s->z_buf;
                    159:        }
                    160:        s->z_stream.avail_out = Z_BUFSIZE;
                    161:
                    162:        errno = 0;
                    163:        s->z_fd = fd;
                    164:
                    165:        if (s->z_mode == 'w') {
                    166:                u_char buf[10];
                    167:                /* Write a very simple .gz header: */
                    168:                buf[0] = gz_magic[0];
                    169:                buf[1] = gz_magic[1];
                    170:                buf[2] = Z_DEFLATED;
                    171:                buf[3] = 0 /*flags*/;
                    172:                buf[4] = buf[5] = buf[6] = buf[7] = 0 /*time*/;
                    173:                buf[8] = 0 /*xflags*/;
                    174:                buf[9] = OS_CODE;
                    175:                if (write(fd, buf, sizeof(buf)) != sizeof(buf)) {
                    176:                        gz_close(s);
                    177:                        s = NULL;
                    178:                }
                    179:        } else {
                    180:                if (get_header(s) != 0) { /* skip the .gz header */
                    181:                        gz_close (s);
                    182:                        s = NULL;
                    183:                }
                    184:        }
                    185:
                    186:        return s;
                    187: }
                    188:
                    189: int
1.7       deraadt   190: gz_close(void *cookie)
1.1       mickey    191: {
1.2       mpech     192:        gz_stream *s = (gz_stream*)cookie;
1.1       mickey    193:        int err = 0;
                    194:
                    195:        if (s == NULL)
                    196:                return -1;
                    197:
                    198:        if (s->z_mode == 'w' && (err = gz_flush (s, Z_FINISH)) == Z_OK) {
                    199:                if ((err = put_int32 (s, s->z_crc)) == Z_OK)
                    200:                        err = put_int32 (s, s->z_stream.total_in);
                    201:        }
                    202:
                    203:        if (!err && s->z_stream.state != NULL) {
                    204:                if (s->z_mode == 'w')
                    205:                        err = deflateEnd(&s->z_stream);
                    206:                else if (s->z_mode == 'r')
                    207:                        err = inflateEnd(&s->z_stream);
                    208:        }
1.10      mickey    209:
                    210:        if (!err)
                    211:                err = close(s->z_fd);
                    212:        else
                    213:                (void)close(s->z_fd);
1.1       mickey    214:
                    215:        free(s);
                    216:
                    217:        return err;
                    218: }
                    219:
                    220: int
1.7       deraadt   221: gz_flush(void *cookie, int flush)
1.1       mickey    222: {
1.2       mpech     223:        gz_stream *s = (gz_stream*)cookie;
1.1       mickey    224:        size_t len;
                    225:        int done = 0;
                    226:        int err;
                    227:
                    228:        if (s == NULL || s->z_mode != 'w') {
                    229:                errno = EBADF;
                    230:                return Z_ERRNO;
                    231:        }
                    232:
                    233:        s->z_stream.avail_in = 0; /* should be zero already anyway */
                    234:
                    235:        for (;;) {
                    236:                len = Z_BUFSIZE - s->z_stream.avail_out;
                    237:
                    238:                if (len != 0) {
                    239:                        if (write(s->z_fd, s->z_buf, len) != len)
                    240:                                return Z_ERRNO;
                    241:                        s->z_stream.next_out = s->z_buf;
                    242:                        s->z_stream.avail_out = Z_BUFSIZE;
                    243:                }
                    244:                if (done)
                    245:                        break;
                    246:                if ((err = deflate(&(s->z_stream), flush)) != Z_OK &&
                    247:                    err != Z_STREAM_END)
                    248:                        return err;
                    249:
                    250:                /* deflate has finished flushing only when it hasn't
                    251:                 * used up all the available space in the output buffer
                    252:                 */
                    253:                done = (s->z_stream.avail_out != 0 || err == Z_STREAM_END);
                    254:        }
                    255:        return 0;
                    256: }
                    257:
                    258: static int
1.7       deraadt   259: put_int32(gz_stream *s, u_int32_t x)
1.1       mickey    260: {
1.8       millert   261:        u_int32_t y = htole32(x);
                    262:
                    263:        if (write(s->z_fd, &y, sizeof(y)) != sizeof(y))
1.1       mickey    264:                return Z_ERRNO;
                    265:        return 0;
                    266: }
                    267:
                    268: static int
1.7       deraadt   269: get_byte(gz_stream *s)
1.1       mickey    270: {
                    271:        if (s->z_eof)
                    272:                return EOF;
1.5       mickey    273:
1.1       mickey    274:        if (s->z_stream.avail_in == 0) {
                    275:                errno = 0;
                    276:                s->z_stream.avail_in = read(s->z_fd, s->z_buf, Z_BUFSIZE);
                    277:                if (s->z_stream.avail_in <= 0) {
                    278:                        s->z_eof = 1;
                    279:                        return EOF;
                    280:                }
                    281:                s->z_stream.next_in = s->z_buf;
                    282:        }
                    283:        s->z_stream.avail_in--;
                    284:        return *s->z_stream.next_in++;
                    285: }
                    286:
1.5       mickey    287: static u_int32_t
1.7       deraadt   288: get_int32(gz_stream *s)
1.1       mickey    289: {
1.2       mpech     290:        u_int32_t x;
1.1       mickey    291:
                    292:        x  = ((u_int32_t)(get_byte(s) & 0xff));
                    293:        x |= ((u_int32_t)(get_byte(s) & 0xff))<<8;
                    294:        x |= ((u_int32_t)(get_byte(s) & 0xff))<<16;
                    295:        x |= ((u_int32_t)(get_byte(s) & 0xff))<<24;
                    296:        return x;
                    297: }
                    298:
                    299: static int
1.7       deraadt   300: get_header(gz_stream *s)
1.1       mickey    301: {
                    302:        int method; /* method byte */
                    303:        int flags;  /* flags byte */
                    304:        uInt len;
                    305:        int c;
                    306:
                    307:        /* Check the gzip magic header */
                    308:        for (len = 0; len < 2; len++) {
                    309:                c = get_byte(s);
                    310:                if (c != gz_magic[len]) {
                    311:                        errno = EFTYPE;
                    312:                        return -1;
                    313:                }
                    314:        }
                    315:
                    316:        method = get_byte(s);
                    317:        flags = get_byte(s);
                    318:        if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
                    319:                errno = EFTYPE;
                    320:                return -1;
                    321:        }
                    322:
                    323:        /* Discard time, xflags and OS code: */
                    324:        for (len = 0; len < 6; len++)
                    325:                (void)get_byte(s);
                    326:
                    327:        if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
                    328:                len  =  (uInt)get_byte(s);
                    329:                len += ((uInt)get_byte(s))<<8;
                    330:                /* len is garbage if EOF but the loop below will quit anyway */
                    331:                while (len-- != 0 && get_byte(s) != EOF)
                    332:                        ;
                    333:        }
                    334:
                    335:        if ((flags & ORIG_NAME) != 0) { /* skip the original file name */
                    336:                while ((c = get_byte(s)) != 0 && c != EOF) ;
                    337:        }
                    338:
                    339:        if ((flags & COMMENT) != 0) {   /* skip the .gz file comment */
                    340:                while ((c = get_byte(s)) != 0 && c != EOF) ;
                    341:        }
                    342:
                    343:        if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
                    344:                for (len = 0; len < 2; len++) (void)get_byte(s);
                    345:        }
                    346:
                    347:        if (s->z_eof) {
                    348:                errno = EFTYPE;
                    349:                return -1;
                    350:        }
                    351:
                    352:        return 0;
                    353: }
                    354:
                    355: int
1.7       deraadt   356: gz_read(void *cookie, char *buf, int len)
1.1       mickey    357: {
1.2       mpech     358:        gz_stream *s = (gz_stream*)cookie;
1.1       mickey    359:        u_char *start = buf; /* starting point for crc computation */
                    360:
                    361:        s->z_stream.next_out = buf;
                    362:        s->z_stream.avail_out = len;
                    363:
                    364:        while (s->z_stream.avail_out != 0 && !s->z_eof) {
                    365:
                    366:                if (s->z_stream.avail_in == 0) {
                    367:
                    368:                        errno = 0;
                    369:                        if ((s->z_stream.avail_in =
1.7       deraadt   370:                            read(s->z_fd, s->z_buf, Z_BUFSIZE)) == 0)
1.1       mickey    371:                                s->z_eof = 1;
                    372:                        s->z_stream.next_in = s->z_buf;
                    373:                }
                    374:
                    375:                if (inflate(&(s->z_stream), Z_NO_FLUSH) == Z_STREAM_END) {
                    376:                        /* Check CRC and original size */
                    377:                        s->z_crc = crc32(s->z_crc, start,
1.7       deraadt   378:                            (uInt)(s->z_stream.next_out - start));
1.1       mickey    379:                        start = s->z_stream.next_out;
                    380:
1.9       millert   381:                        if (get_int32(s) != s->z_crc) {
                    382:                                errno = EINVAL;
                    383:                                return -1;
                    384:                        }
                    385:                        if (get_int32(s) != s->z_stream.total_out) {
1.7       deraadt   386:                                errno = EIO;
1.1       mickey    387:                                return -1;
                    388:                        }
                    389:                        s->z_eof = 1;
                    390:                        break;
                    391:                }
                    392:        }
                    393:        s->z_crc = crc32(s->z_crc, start,
1.7       deraadt   394:            (uInt)(s->z_stream.next_out - start));
1.1       mickey    395:
                    396:        return (int)(len - s->z_stream.avail_out);
                    397: }
                    398:
                    399: int
1.7       deraadt   400: gz_write(void *cookie, const char *buf, int len)
1.1       mickey    401: {
1.2       mpech     402:        gz_stream *s = (gz_stream*)cookie;
1.1       mickey    403:
                    404:        s->z_stream.next_in = (char *)buf;
                    405:        s->z_stream.avail_in = len;
                    406:
                    407:        while (s->z_stream.avail_in != 0) {
                    408:                if (s->z_stream.avail_out == 0) {
                    409:                        if (write(s->z_fd, s->z_buf, Z_BUFSIZE) != Z_BUFSIZE)
                    410:                                break;
                    411:                        s->z_stream.next_out = s->z_buf;
                    412:                        s->z_stream.avail_out = Z_BUFSIZE;
                    413:                }
                    414:                if (deflate(&(s->z_stream), Z_NO_FLUSH) != Z_OK)
                    415:                        break;
                    416:        }
                    417:        s->z_crc = crc32(s->z_crc, buf, len);
                    418:
                    419:        return (int)(len - s->z_stream.avail_in);
                    420: }
                    421: