]> git.cworth.org Git - gzip/blob - zip.c
Imported Debian patch 1.3.5-13
[gzip] / zip.c
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 #ifdef RCSID
8 static char rcsid[] = "$Id: zip.c,v 0.17 1993/06/10 13:29:25 jloup Exp $";
9 #endif
10
11 #include <config.h>
12 #include <ctype.h>
13
14 #include "tailor.h"
15 #include "gzip.h"
16 #include "crypt.h"
17
18 #ifdef HAVE_UNISTD_H
19 #  include <unistd.h>
20 #endif
21 #ifdef HAVE_FCNTL_H
22 #  include <fcntl.h>
23 #endif
24
25 local ulg crc;       /* crc on uncompressed file data */
26 off_t header_bytes;   /* number of bytes in gzip header */
27
28 /* ===========================================================================
29  * Deflate in to out.
30  * IN assertions: the input and output buffers are cleared.
31  *   The variables time_stamp and save_orig_name are initialized.
32  */
33 int zip(in, out)
34     int in, out;            /* input and output file descriptors */
35 {
36     uch  flags = 0;         /* general purpose bit flags */
37     ush  attr = 0;          /* ascii/binary flag */
38     ush  deflate_flags = 0; /* pkzip -es, -en or -ex equivalent */
39
40     ifd = in;
41     ofd = out;
42     outcnt = 0;
43
44     /* Write the header to the gzip file. See algorithm.doc for the format */
45
46     method = DEFLATED;
47     put_byte(GZIP_MAGIC[0]); /* magic header */
48     put_byte(GZIP_MAGIC[1]);
49     put_byte(DEFLATED);      /* compression method */
50
51     if (save_orig_name) {
52         flags |= ORIG_NAME;
53     }
54     put_byte(flags);         /* general flags */
55     put_long(time_stamp == (time_stamp & 0xffffffff)
56              ? (ulg)time_stamp : (ulg)0);
57
58     /* Write deflated file to zip file */
59     crc = updcrc(0, 0);
60
61     bi_init(out);
62     ct_init(&attr, &method);
63     lm_init(level, &deflate_flags);
64
65     put_byte((uch)deflate_flags); /* extra flags */
66     put_byte(OS_CODE);            /* OS identifier */
67
68     if (save_orig_name) {
69         char *p = base_name(ifname); /* Don't save the directory part. */
70         do {
71             put_char(*p);
72         } while (*p++);
73     }
74     header_bytes = (off_t)outcnt;
75
76     (void)deflate();
77
78 #if !defined(NO_SIZE_CHECK) && !defined(RECORD_IO)
79   /* Check input size (but not in VMS -- variable record lengths mess it up)
80    * and not on MSDOS -- diet in TSR mode reports an incorrect file size)
81    */
82     if (ifile_size != -1L && bytes_in != ifile_size) {
83         fprintf(stderr, "%s: %s: file size changed while zipping\n",
84                 progname, ifname);
85     }
86 #endif
87
88     /* Write the crc and uncompressed size */
89     put_long(crc);
90     put_long((ulg)bytes_in);
91     header_bytes += 2*sizeof(long);
92
93     flush_outbuf();
94     return OK;
95 }
96
97
98 /* ===========================================================================
99  * Read a new buffer from the current input file, perform end-of-line
100  * translation, and update the crc and input file size.
101  * IN assertion: size >= 2 (for end-of-line translation)
102  */
103 int file_read(buf, size)
104     char *buf;
105     unsigned size;
106 {
107     unsigned len;
108
109     Assert(insize == 0, "inbuf not empty");
110
111     len = read(ifd, buf, size);
112     if (len == 0) return (int)len;
113     if (len == (unsigned)-1) {
114         read_error();
115         return EOF;
116     }
117
118     crc = updcrc((uch*)buf, len);
119     bytes_in += (off_t)len;
120     return (int)len;
121 }