00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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;
00039 off_t header_bytes;
00040
00041
00042
00043
00044
00045
00046 int zip(in, out)
00047 int in, out;
00048 {
00049 uch flags = 0;
00050 ush attr = 0;
00051 ush deflate_flags = 0;
00052 ulg stamp;
00053
00054 ifd = in;
00055 ofd = out;
00056 outcnt = 0;
00057
00058
00059
00060 method = DEFLATED;
00061 put_byte(GZIP_MAGIC[0]);
00062 put_byte(GZIP_MAGIC[1]);
00063 put_byte(DEFLATED);
00064
00065 if (save_orig_name) {
00066 flags |= ORIG_NAME;
00067 }
00068 put_byte(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
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);
00082 put_byte(OS_CODE);
00083
00084 if (save_orig_name) {
00085 char *p = gzip_base_name (ifname);
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
00096
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
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
00116
00117
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 }