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

1.19    ! henning     1: /*     $OpenBSD: gzopen.c,v 1.18 2003/12/16 22:38:40 henning 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.19    ! henning    61: #ifndef SMALL
1.5       mickey     62: const char gz_rcsid[] =
1.19    ! henning    63:     "$OpenBSD: gzopen.c,v 1.18 2003/12/16 22:38:40 henning Exp $";
        !            64: #endif
1.5       mickey     65:
1.13      millert    66: #include <sys/param.h>
1.1       mickey     67: #include <sys/stat.h>
1.13      millert    68: #include <sys/uio.h>
1.1       mickey     69: #include <stdio.h>
                     70: #include <stdlib.h>
1.11      david      71: #include <string.h>
1.1       mickey     72: #include <errno.h>
                     73: #include <unistd.h>
                     74: #include <zlib.h>
                     75: #include "compress.h"
                     76:
                     77: /* gzip flag byte */
                     78: #define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
                     79: #define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
                     80: #define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
                     81: #define ORIG_NAME    0x08 /* bit 3 set: original file name present */
                     82: #define COMMENT      0x10 /* bit 4 set: file comment present */
                     83: #define RESERVED     0xE0 /* bits 5..7: reserved */
                     84:
                     85: #define DEF_MEM_LEVEL 8
                     86: #define OS_CODE 0x03 /* unix */
                     87:
                     88: typedef
                     89: struct gz_stream {
                     90:        int     z_fd;           /* .gz file */
                     91:        z_stream z_stream;      /* libz stream */
                     92:        int     z_eof;          /* set if end of input file */
                     93:        u_char  z_buf[Z_BUFSIZE]; /* i/o buffer */
1.13      millert    94:        u_int32_t z_time;       /* timestamp (mtime) */
                     95:        u_int32_t z_hlen;       /* length of the gz header */
1.1       mickey     96:        u_int32_t z_crc;        /* crc32 of uncompressed data */
                     97:        char    z_mode;         /* 'w' or 'r' */
                     98:
                     99: } gz_stream;
                    100:
1.5       mickey    101: static const u_char gz_magic[2] = {0x1f, 0x8b}; /* gzip magic header */
1.1       mickey    102:
1.3       millert   103: static int put_int32(gz_stream *, u_int32_t);
                    104: static u_int32_t get_int32(gz_stream *);
1.13      millert   105: static int get_header(gz_stream *, char *, int);
                    106: static int put_header(gz_stream *, char *, u_int32_t);
1.3       millert   107: static int get_byte(gz_stream *);
1.1       mickey    108:
                    109: void *
1.13      millert   110: gz_open(int fd, const char *mode, char *name, int bits,
                    111:     u_int32_t mtime, int gotmagic)
1.1       mickey    112: {
                    113:        gz_stream *s;
                    114:
                    115:        if (fd < 0 || !mode)
                    116:                return NULL;
                    117:
                    118:        if ((mode[0] != 'r' && mode[0] != 'w') || mode[1] != '\0' ||
                    119:            bits < 0 || bits > Z_BEST_COMPRESSION) {
                    120:                errno = EINVAL;
                    121:                return NULL;
                    122:        }
1.4       aaron     123:        if ((s = (gz_stream *)calloc(1, sizeof(gz_stream))) == NULL)
1.1       mickey    124:                return NULL;
                    125:
                    126:        s->z_stream.zalloc = (alloc_func)0;
                    127:        s->z_stream.zfree = (free_func)0;
                    128:        s->z_stream.opaque = (voidpf)0;
                    129:        s->z_stream.next_in = Z_NULL;
                    130:        s->z_stream.next_out = Z_NULL;
                    131:        s->z_stream.avail_in = s->z_stream.avail_out = 0;
                    132:        s->z_fd = 0;
                    133:        s->z_eof = 0;
1.13      millert   134:        s->z_time = 0;
                    135:        s->z_hlen = 0;
1.1       mickey    136:        s->z_crc = crc32(0L, Z_NULL, 0);
                    137:        s->z_mode = mode[0];
                    138:
                    139:        if (s->z_mode == 'w') {
                    140:                /* windowBits is passed < 0 to suppress zlib header */
                    141:                if (deflateInit2(&(s->z_stream), bits, Z_DEFLATED,
                    142:                                 -MAX_WBITS, DEF_MEM_LEVEL, 0) != Z_OK) {
                    143:                        free (s);
                    144:                        return NULL;
                    145:                }
                    146:                s->z_stream.next_out = s->z_buf;
                    147:        } else {
                    148:                if (inflateInit2(&(s->z_stream), -MAX_WBITS) != Z_OK) {
                    149:                        free (s);
                    150:                        return NULL;
                    151:                }
                    152:                s->z_stream.next_in = s->z_buf;
                    153:        }
                    154:        s->z_stream.avail_out = Z_BUFSIZE;
                    155:
                    156:        errno = 0;
                    157:        s->z_fd = fd;
                    158:
                    159:        if (s->z_mode == 'w') {
1.13      millert   160:                /* write the .gz header */
                    161:                if (put_header(s, name, mtime) != 0) {
                    162:                        gz_close(s, NULL);
1.1       mickey    163:                        s = NULL;
                    164:                }
                    165:        } else {
1.13      millert   166:                /* read the .gz header */
                    167:                if (get_header(s, name, gotmagic) != 0) {
                    168:                        gz_close(s, NULL);
1.1       mickey    169:                        s = NULL;
                    170:                }
                    171:        }
                    172:
                    173:        return s;
                    174: }
                    175:
                    176: int
1.13      millert   177: gz_close(void *cookie, struct z_info *info)
1.1       mickey    178: {
1.2       mpech     179:        gz_stream *s = (gz_stream*)cookie;
1.1       mickey    180:        int err = 0;
                    181:
                    182:        if (s == NULL)
                    183:                return -1;
                    184:
                    185:        if (s->z_mode == 'w' && (err = gz_flush (s, Z_FINISH)) == Z_OK) {
1.13      millert   186:                if ((err = put_int32 (s, s->z_crc)) == Z_OK) {
                    187:                        s->z_hlen += sizeof(int32_t);
                    188:                        if ((err = put_int32 (s, s->z_stream.total_in)) == Z_OK)
                    189:                                s->z_hlen += sizeof(int32_t);
                    190:                }
1.1       mickey    191:        }
                    192:
                    193:        if (!err && s->z_stream.state != NULL) {
                    194:                if (s->z_mode == 'w')
                    195:                        err = deflateEnd(&s->z_stream);
                    196:                else if (s->z_mode == 'r')
                    197:                        err = inflateEnd(&s->z_stream);
                    198:        }
1.10      mickey    199:
1.13      millert   200:        if (info != NULL) {
                    201:                info->mtime = s->z_time;
                    202:                info->crc = s->z_crc;
                    203:                info->hlen = s->z_hlen;
                    204:                info->total_in = (off_t)s->z_stream.total_in;
                    205:                info->total_out = (off_t)s->z_stream.total_out;
                    206:        }
                    207:
1.10      mickey    208:        if (!err)
                    209:                err = close(s->z_fd);
                    210:        else
                    211:                (void)close(s->z_fd);
1.1       mickey    212:
                    213:        free(s);
                    214:
                    215:        return err;
                    216: }
                    217:
                    218: int
1.7       deraadt   219: gz_flush(void *cookie, int flush)
1.1       mickey    220: {
1.2       mpech     221:        gz_stream *s = (gz_stream*)cookie;
1.1       mickey    222:        size_t len;
                    223:        int done = 0;
                    224:        int err;
                    225:
                    226:        if (s == NULL || s->z_mode != 'w') {
                    227:                errno = EBADF;
                    228:                return Z_ERRNO;
                    229:        }
                    230:
                    231:        s->z_stream.avail_in = 0; /* should be zero already anyway */
                    232:
                    233:        for (;;) {
                    234:                len = Z_BUFSIZE - s->z_stream.avail_out;
                    235:
                    236:                if (len != 0) {
                    237:                        if (write(s->z_fd, s->z_buf, len) != len)
                    238:                                return Z_ERRNO;
                    239:                        s->z_stream.next_out = s->z_buf;
                    240:                        s->z_stream.avail_out = Z_BUFSIZE;
                    241:                }
                    242:                if (done)
                    243:                        break;
                    244:                if ((err = deflate(&(s->z_stream), flush)) != Z_OK &&
                    245:                    err != Z_STREAM_END)
                    246:                        return err;
                    247:
                    248:                /* deflate has finished flushing only when it hasn't
                    249:                 * used up all the available space in the output buffer
                    250:                 */
                    251:                done = (s->z_stream.avail_out != 0 || err == Z_STREAM_END);
                    252:        }
                    253:        return 0;
                    254: }
                    255:
                    256: static int
1.7       deraadt   257: put_int32(gz_stream *s, u_int32_t x)
1.1       mickey    258: {
1.8       millert   259:        u_int32_t y = htole32(x);
                    260:
                    261:        if (write(s->z_fd, &y, sizeof(y)) != sizeof(y))
1.1       mickey    262:                return Z_ERRNO;
                    263:        return 0;
                    264: }
                    265:
                    266: static int
1.7       deraadt   267: get_byte(gz_stream *s)
1.1       mickey    268: {
                    269:        if (s->z_eof)
                    270:                return EOF;
1.5       mickey    271:
1.1       mickey    272:        if (s->z_stream.avail_in == 0) {
                    273:                errno = 0;
                    274:                s->z_stream.avail_in = read(s->z_fd, s->z_buf, Z_BUFSIZE);
                    275:                if (s->z_stream.avail_in <= 0) {
                    276:                        s->z_eof = 1;
                    277:                        return EOF;
                    278:                }
                    279:                s->z_stream.next_in = s->z_buf;
                    280:        }
                    281:        s->z_stream.avail_in--;
                    282:        return *s->z_stream.next_in++;
                    283: }
                    284:
1.5       mickey    285: static u_int32_t
1.7       deraadt   286: get_int32(gz_stream *s)
1.1       mickey    287: {
1.2       mpech     288:        u_int32_t x;
1.1       mickey    289:
                    290:        x  = ((u_int32_t)(get_byte(s) & 0xff));
                    291:        x |= ((u_int32_t)(get_byte(s) & 0xff))<<8;
                    292:        x |= ((u_int32_t)(get_byte(s) & 0xff))<<16;
                    293:        x |= ((u_int32_t)(get_byte(s) & 0xff))<<24;
                    294:        return x;
                    295: }
                    296:
                    297: static int
1.13      millert   298: get_header(gz_stream *s, char *name, int gotmagic)
1.1       mickey    299: {
                    300:        int method; /* method byte */
                    301:        int flags;  /* flags byte */
1.13      millert   302:        char *ep;
1.1       mickey    303:        uInt len;
                    304:        int c;
                    305:
                    306:        /* Check the gzip magic header */
1.12      millert   307:        if (!gotmagic) {
                    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:                        }
1.1       mickey    314:                }
                    315:        }
                    316:
                    317:        method = get_byte(s);
                    318:        flags = get_byte(s);
                    319:        if (method != Z_DEFLATED || (flags & RESERVED) != 0) {
                    320:                errno = EFTYPE;
                    321:                return -1;
                    322:        }
                    323:
1.13      millert   324:        /* Stash timestamp (mtime) */
                    325:        s->z_time = get_int32(s);
                    326:
                    327:        /* Discard xflags and OS code */
                    328:        (void)get_byte(s);
                    329:        (void)get_byte(s);
1.1       mickey    330:
1.13      millert   331:        s->z_hlen = 10; /* magic, method, flags, time, xflags, OS code */
1.1       mickey    332:        if ((flags & EXTRA_FIELD) != 0) { /* skip the extra field */
                    333:                len  =  (uInt)get_byte(s);
                    334:                len += ((uInt)get_byte(s))<<8;
1.13      millert   335:                s->z_hlen += 2;
1.1       mickey    336:                /* len is garbage if EOF but the loop below will quit anyway */
                    337:                while (len-- != 0 && get_byte(s) != EOF)
1.13      millert   338:                        s->z_hlen++;
1.1       mickey    339:        }
                    340:
1.13      millert   341:        if ((flags & ORIG_NAME) != 0) { /* read/save the original file name */
                    342:                if ((ep = name) != NULL)
                    343:                        ep += MAXPATHLEN - 1;
                    344:                while ((c = get_byte(s)) != EOF) {
                    345:                        s->z_hlen++;
                    346:                        if (c == '\0')
                    347:                                break;
                    348:                        if (name < ep)
                    349:                                *name++ = c;
                    350:                }
                    351:                if (name != NULL)
                    352:                        *name = '\0';
1.1       mickey    353:        }
                    354:
                    355:        if ((flags & COMMENT) != 0) {   /* skip the .gz file comment */
1.13      millert   356:                while ((c = get_byte(s)) != EOF) {
                    357:                        s->z_hlen++;
                    358:                        if (c == '\0')
                    359:                                break;
                    360:                }
1.1       mickey    361:        }
                    362:
                    363:        if ((flags & HEAD_CRC) != 0) {  /* skip the header crc */
1.14      mickey    364:                (void)get_byte(s);
                    365:                (void)get_byte(s);
1.13      millert   366:                s->z_hlen += 2;
1.1       mickey    367:        }
                    368:
                    369:        if (s->z_eof) {
                    370:                errno = EFTYPE;
                    371:                return -1;
                    372:        }
                    373:
                    374:        return 0;
                    375: }
                    376:
1.13      millert   377: static int
                    378: put_header(gz_stream *s, char *name, u_int32_t mtime)
                    379: {
                    380:        struct iovec iov[2];
                    381:        u_char buf[10];
                    382:
                    383:        buf[0] = gz_magic[0];
                    384:        buf[1] = gz_magic[1];
                    385:        buf[2] = Z_DEFLATED;
                    386:        buf[3] = name ? ORIG_NAME : 0;
                    387:        buf[4] = mtime & 0xff;
                    388:        buf[5] = (mtime >> 8) & 0xff;
                    389:        buf[6] = (mtime >> 16) & 0xff;
                    390:        buf[7] = (mtime >> 24) & 0xff;
                    391:        buf[8] = 0 /* xflags */;
                    392:        buf[9] = OS_CODE;
                    393:        iov[0].iov_base = buf;
                    394:        iov[0].iov_len = sizeof(buf);
                    395:        s->z_hlen = sizeof(buf);
                    396:
                    397:        if (name != NULL) {
                    398:                iov[1].iov_base = name;
                    399:                iov[1].iov_len = strlen(name) + 1;
                    400:                s->z_hlen += iov[1].iov_len;
                    401:        }
                    402:        if (writev(s->z_fd, iov, name ? 2 : 1) == -1)
                    403:                return (-1);
                    404:        return (0);
                    405: }
                    406:
1.1       mickey    407: int
1.7       deraadt   408: gz_read(void *cookie, char *buf, int len)
1.1       mickey    409: {
1.2       mpech     410:        gz_stream *s = (gz_stream*)cookie;
1.1       mickey    411:        u_char *start = buf; /* starting point for crc computation */
1.17      millert   412:        int error = Z_OK;
1.1       mickey    413:
                    414:        s->z_stream.next_out = buf;
                    415:        s->z_stream.avail_out = len;
                    416:
1.17      millert   417:        while (error == Z_OK && !s->z_eof && s->z_stream.avail_out != 0) {
1.1       mickey    418:
                    419:                if (s->z_stream.avail_in == 0) {
                    420:
                    421:                        errno = 0;
                    422:                        if ((s->z_stream.avail_in =
1.7       deraadt   423:                            read(s->z_fd, s->z_buf, Z_BUFSIZE)) == 0)
1.1       mickey    424:                                s->z_eof = 1;
                    425:                        s->z_stream.next_in = s->z_buf;
                    426:                }
                    427:
1.17      millert   428:                error = inflate(&(s->z_stream), Z_NO_FLUSH);
                    429:                if (error == Z_STREAM_END) {
1.1       mickey    430:                        /* Check CRC and original size */
                    431:                        s->z_crc = crc32(s->z_crc, start,
1.7       deraadt   432:                            (uInt)(s->z_stream.next_out - start));
1.1       mickey    433:                        start = s->z_stream.next_out;
                    434:
1.9       millert   435:                        if (get_int32(s) != s->z_crc) {
                    436:                                errno = EINVAL;
                    437:                                return -1;
                    438:                        }
1.18      henning   439:                        if (get_int32(s) != (u_int32_t)s->z_stream.total_out) {
1.7       deraadt   440:                                errno = EIO;
1.1       mickey    441:                                return -1;
                    442:                        }
1.13      millert   443:                        s->z_hlen += 2 * sizeof(int32_t);
1.16      millert   444:                        /* Check for the existence of an appended file. */
                    445:                        if (get_header(s, NULL, 0) != 0) {
                    446:                                s->z_eof = 1;
                    447:                                break;
                    448:                        }
                    449:                        inflateReset(&(s->z_stream));
                    450:                        s->z_crc = crc32(0L, Z_NULL, 0);
1.17      millert   451:                        error = Z_OK;
1.1       mickey    452:                }
                    453:        }
                    454:        s->z_crc = crc32(s->z_crc, start,
1.7       deraadt   455:            (uInt)(s->z_stream.next_out - start));
1.15      millert   456:        len -= s->z_stream.avail_out;
                    457:
                    458:        return (len);
1.1       mickey    459: }
                    460:
                    461: int
1.7       deraadt   462: gz_write(void *cookie, const char *buf, int len)
1.1       mickey    463: {
1.2       mpech     464:        gz_stream *s = (gz_stream*)cookie;
1.1       mickey    465:
                    466:        s->z_stream.next_in = (char *)buf;
                    467:        s->z_stream.avail_in = len;
                    468:
                    469:        while (s->z_stream.avail_in != 0) {
                    470:                if (s->z_stream.avail_out == 0) {
                    471:                        if (write(s->z_fd, s->z_buf, Z_BUFSIZE) != Z_BUFSIZE)
                    472:                                break;
                    473:                        s->z_stream.next_out = s->z_buf;
                    474:                        s->z_stream.avail_out = Z_BUFSIZE;
                    475:                }
                    476:                if (deflate(&(s->z_stream), Z_NO_FLUSH) != Z_OK)
                    477:                        break;
                    478:        }
                    479:        s->z_crc = crc32(s->z_crc, buf, len);
                    480:
                    481:        return (int)(len - s->z_stream.avail_in);
                    482: }