Main Page | Class List | Directories | File List | Class Members | File Members

zip.c

Go to the documentation of this file.
00001 /* zip.c -- compress files to the gzip or pkzip format
00002 
00003    Copyright (C) 1997, 1998, 1999, 2006, 2007 Free Software Foundation, Inc.
00004    Copyright (C) 1992-1993 Jean-loup Gailly
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2, or (at your option)
00009    any later version.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software Foundation,
00018    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
00019 
00020 #ifdef RCSID
00021 static char rcsid[] = "$Id: zip.c,v 1.7 2007/03/20 05:09:51 eggert Exp $";
00022 #endif
00023 
00024 #include <config.h>
00025 #include <ctype.h>
00026 
00027 #include "tailor.h"
00028 #include "gzip.h"
00029 #include "crypt.h"
00030 
00031 #ifdef HAVE_UNISTD_H
00032 #  include <unistd.h>
00033 #endif
00034 #ifdef HAVE_FCNTL_H
00035 #  include <fcntl.h>
00036 #endif
00037 
00038 local ulg crc;       /* crc on uncompressed file data */
00039 off_t header_bytes;   /* number of bytes in gzip header */
00040 
00041 /* ===========================================================================
00042  * Deflate in to out.
00043  * IN assertions: the input and output buffers are cleared.
00044  *   The variables time_stamp and save_orig_name are initialized.
00045  */
00046 int zip(in, out)
00047     int in, out;            /* input and output file descriptors */
00048 {
00049     uch  flags = 0;         /* general purpose bit flags */
00050     ush  attr = 0;          /* ascii/binary flag */
00051     ush  deflate_flags = 0; /* pkzip -es, -en or -ex equivalent */
00052     ulg  stamp;
00053 
00054     ifd = in;
00055     ofd = out;
00056     outcnt = 0;
00057 
00058     /* Write the header to the gzip file. See algorithm.doc for the format */
00059 
00060     method = DEFLATED;
00061     put_byte(GZIP_MAGIC[0]); /* magic header */
00062     put_byte(GZIP_MAGIC[1]);
00063     put_byte(DEFLATED);      /* compression method */
00064 
00065     if (save_orig_name) {
00066         flags |= ORIG_NAME;
00067     }
00068     put_byte(flags);         /* general flags */
00069     stamp = (0 <= time_stamp.tv_sec && time_stamp.tv_sec <= 0xffffffff
00070              ? (ulg) time_stamp.tv_sec
00071              : (ulg) 0);
00072     put_long (stamp);
00073 
00074     /* Write deflated file to zip file */
00075     crc = updcrc(0, 0);
00076 
00077     bi_init(out);
00078     ct_init(&attr, &method);
00079     lm_init(level, &deflate_flags);
00080 
00081     put_byte((uch)deflate_flags); /* extra flags */
00082     put_byte(OS_CODE);            /* OS identifier */
00083 
00084     if (save_orig_name) {
00085         char *p = gzip_base_name (ifname); /* Don't save the directory part. */
00086         do {
00087             put_char(*p);
00088         } while (*p++);
00089     }
00090     header_bytes = (off_t)outcnt;
00091 
00092     (void)deflate();
00093 
00094 #if !defined(NO_SIZE_CHECK) && !defined(RECORD_IO)
00095   /* Check input size (but not in VMS -- variable record lengths mess it up)
00096    * and not on MSDOS -- diet in TSR mode reports an incorrect file size)
00097    */
00098     if (ifile_size != -1L && bytes_in != ifile_size) {
00099         fprintf(stderr, "%s: %s: file size changed while zipping\n",
00100                 program_name, ifname);
00101     }
00102 #endif
00103 
00104     /* Write the crc and uncompressed size */
00105     put_long(crc);
00106     put_long((ulg)bytes_in);
00107     header_bytes += 2*sizeof(long);
00108 
00109     flush_outbuf();
00110     return OK;
00111 }
00112 
00113 
00114 /* ===========================================================================
00115  * Read a new buffer from the current input file, perform end-of-line
00116  * translation, and update the crc and input file size.
00117  * IN assertion: size >= 2 (for end-of-line translation)
00118  */
00119 int file_read(buf, size)
00120     char *buf;
00121     unsigned size;
00122 {
00123     unsigned len;
00124 
00125     Assert(insize == 0, "inbuf not empty");
00126 
00127     len = read_buffer (ifd, buf, size);
00128     if (len == 0) return (int)len;
00129     if (len == (unsigned)-1) {
00130         read_error();
00131         return EOF;
00132     }
00133 
00134     crc = updcrc((uch*)buf, len);
00135     bytes_in += (off_t)len;
00136     return (int)len;
00137 }

© sourcejam.com 2005-2008