Annotation of coherent/g/usr/bin/gzip/zip.c, revision 1.1

1.1     ! root        1: /* zip.c -- compress files to the gzip or pkzip format
        !             2:  * Copyright (C) 1992-1993 Jean-loup Gailly
        !             3:  * This is free software; you can redistribute it and/or modify it under the
        !             4:  * terms of the GNU General Public License, see the file COPYING.
        !             5:  */
        !             6: 
        !             7: #ifndef lint
        !             8: static char rcsid[] = "$Id: zip.c,v 0.12 1993/02/24 18:23:13 jloup Exp $";
        !             9: #endif
        !            10: 
        !            11: #include "tailor.h"
        !            12: #include "gzip.h"
        !            13: #include "crypt.h"
        !            14: 
        !            15: #include <ctype.h>
        !            16: #include <stdio.h>
        !            17: 
        !            18: #ifdef HAVE_UNISTD_H
        !            19: #  include <sys/types.h>
        !            20: #  include <unistd.h>
        !            21: #endif
        !            22: 
        !            23: local ulg crc;       /* crc on uncompressed file data */
        !            24: long overhead;       /* number of bytes in gzip header */
        !            25: 
        !            26: /* ===========================================================================
        !            27:  * Deflate in to out.
        !            28:  * IN assertions: the input and output buffers are cleared.
        !            29:  *   The variables time_stamp, save_orig_name and text_mode are initialized.
        !            30:  */
        !            31: void zip(in, out)
        !            32:     int in, out;            /* input and output file descriptors */
        !            33: {
        !            34:     uch  flags = 0;         /* general purpose bit flags */
        !            35:     ush  attr = 0;          /* ascii/binary flag */
        !            36:     ush  deflate_flags = 0; /* pkzip -es, -en or -ex equivalent */
        !            37: 
        !            38:     ifd = in;
        !            39:     ofd = out;
        !            40:     outcnt = 0;
        !            41: 
        !            42:     /* Write the header to the gzip file. See algorithm.doc for the format */
        !            43: 
        !            44:     method = DEFLATED;
        !            45:     put_byte(GZIP_MAGIC[0]); /* magic header */
        !            46:     put_byte(GZIP_MAGIC[1]);
        !            47:     put_byte(DEFLATED);      /* compression method */
        !            48: 
        !            49:     if (save_orig_name) {
        !            50:        flags |= ORIG_NAME;
        !            51:     }
        !            52:     if (text_mode) {
        !            53:        flags |= ASCII_FLAG;
        !            54:        /* This currently works only for VMS files in variable record format.
        !            55:          * For other systems, we should look ahead a little in the input to
        !            56:          * determine the ascii/binary flag.
        !            57:          */
        !            58:     }
        !            59:     put_byte(flags);         /* general flags */
        !            60:     put_long(time_stamp);
        !            61: 
        !            62:     /* Write deflated file to zip file */
        !            63:     crc = updcrc(0, 0);
        !            64: 
        !            65:     bi_init(out);
        !            66:     ct_init(&attr, &method);
        !            67:     lm_init(level, &deflate_flags);
        !            68: 
        !            69:     put_byte((uch)deflate_flags); /* extra flags */
        !            70:     put_byte(OS_CODE);            /* OS identifier */
        !            71: 
        !            72:     if (save_orig_name) {
        !            73:        char *p = basename(ifname); /* Don't save the directory part. */
        !            74:        do {
        !            75:            put_byte(*p);
        !            76:        } while (*p++);
        !            77:     }
        !            78:     overhead = (long)outcnt;
        !            79: 
        !            80:     (void)deflate();
        !            81: 
        !            82: #if !defined(NO_SIZE_CHECK) && !defined(RECORD_IO)
        !            83:   /* Check input size (but not in VMS -- variable record lengths mess it up)
        !            84:    * and not on MSDOS -- diet in TSR mode reports an incorrect file size)
        !            85:    */
        !            86:     if (ifile_size != -1L && isize != (ulg)ifile_size) {
        !            87:        Trace((stderr, " actual=%ld, read=%ld ", ifile_size, isize));
        !            88:        fprintf(stderr, " file size changed while zipping %s\n", ifname);
        !            89:     }
        !            90: #endif
        !            91: 
        !            92:     /* Write the crc and uncompressed size */
        !            93:     put_long(crc);
        !            94:     put_long(isize);
        !            95: 
        !            96:     flush_outbuf();
        !            97: }
        !            98: 
        !            99: 
        !           100: /* ===========================================================================
        !           101:  * Read a new buffer from the current input file, perform end-of-line
        !           102:  * translation, and update the crc and input file size.
        !           103:  * IN assertion: size >= 2 (for end-of-line translation)
        !           104:  */
        !           105: int file_read(buf, size)
        !           106:     char *buf;
        !           107:     unsigned size;
        !           108: {
        !           109:     unsigned len;
        !           110: 
        !           111:     Assert(insize == 0, "inbuf not empty");
        !           112: 
        !           113:     len = read(ifd, buf, size);
        !           114:     if (len == (unsigned)(-1) || len == 0) return (int)len;
        !           115: 
        !           116:     crc = updcrc((uch*)buf, len);
        !           117:     isize += (ulg)len;
        !           118:     return (int)len;
        !           119: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.