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

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