Contents of /trunk/mkinitrd-magellan/klibc/usr/gzip/zip.c
Parent Directory | Revision Log
Revision 815 -
(show annotations)
(download)
Fri Apr 24 18:32:46 2009 UTC (15 years ago) by niro
File MIME type: text/plain
File size: 3031 byte(s)
Fri Apr 24 18:32:46 2009 UTC (15 years ago) by niro
File MIME type: text/plain
File size: 3031 byte(s)
-updated to klibc-1.5.15
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 1.1 2002/08/18 00:59:21 hpa Exp $"; |
9 | #endif |
10 | |
11 | #include <ctype.h> |
12 | #include <sys/types.h> |
13 | #include <unistd.h> |
14 | #include <fcntl.h> |
15 | |
16 | #include "tailor.h" |
17 | #include "gzip.h" |
18 | |
19 | local ulg crc; /* crc on uncompressed file data */ |
20 | |
21 | /* =========================================================================== |
22 | * Deflate in to out. |
23 | * IN assertions: the input and output buffers are cleared. |
24 | * The variables time_stamp and save_orig_name are initialized. |
25 | */ |
26 | int zip(in, out) |
27 | int in, out; /* input and output file descriptors */ |
28 | { |
29 | uch flags = 0; /* general purpose bit flags */ |
30 | ush attr = 0; /* ascii/binary flag */ |
31 | ush deflate_flags = 0; /* pkzip -es, -en or -ex equivalent */ |
32 | |
33 | ifd = in; |
34 | ofd = out; |
35 | outcnt = 0; |
36 | |
37 | /* Write the header to the gzip file. See algorithm.doc for the format */ |
38 | |
39 | method = DEFLATED; |
40 | put_byte(GZIP_MAGIC[0]); /* magic header */ |
41 | put_byte(GZIP_MAGIC[1]); |
42 | put_byte(DEFLATED); /* compression method */ |
43 | |
44 | if (save_orig_name) { |
45 | flags |= ORIG_NAME; |
46 | } |
47 | put_byte(flags); /* general flags */ |
48 | put_long(time_stamp); |
49 | |
50 | /* Write deflated file to zip file */ |
51 | crc = updcrc(0, 0); |
52 | |
53 | bi_init(out); |
54 | ct_init(&attr, &method); |
55 | lm_init(level, &deflate_flags); |
56 | |
57 | put_byte((uch)deflate_flags); /* extra flags */ |
58 | put_byte(OS_CODE); /* OS identifier */ |
59 | |
60 | if (save_orig_name) { |
61 | char *p = basename(ifname); /* Don't save the directory part. */ |
62 | do { |
63 | put_char(*p); |
64 | } while (*p++); |
65 | } |
66 | header_bytes = (long)outcnt; |
67 | |
68 | (void)deflate(); |
69 | |
70 | #if !defined(NO_SIZE_CHECK) && !defined(RECORD_IO) |
71 | /* Check input size (but not in VMS -- variable record lengths mess it up) |
72 | * and not on MSDOS -- diet in TSR mode reports an incorrect file size) |
73 | */ |
74 | if (ifile_size != -1L && isize != (ulg)ifile_size) { |
75 | Trace((stderr, " actual=%ld, read=%ld ", ifile_size, isize)); |
76 | fprintf(stderr, "%s: %s: file size changed while zipping\n", |
77 | progname, ifname); |
78 | } |
79 | #endif |
80 | |
81 | /* Write the crc and uncompressed size */ |
82 | put_long(crc); |
83 | put_long(isize); |
84 | header_bytes += 2*sizeof(long); |
85 | |
86 | flush_outbuf(); |
87 | return OK; |
88 | } |
89 | |
90 | |
91 | /* =========================================================================== |
92 | * Read a new buffer from the current input file, perform end-of-line |
93 | * translation, and update the crc and input file size. |
94 | * IN assertion: size >= 2 (for end-of-line translation) |
95 | */ |
96 | int file_read(buf, size) |
97 | char *buf; |
98 | unsigned size; |
99 | { |
100 | unsigned len; |
101 | |
102 | Assert(insize == 0, "inbuf not empty"); |
103 | |
104 | len = read(ifd, buf, size); |
105 | if (len == (unsigned)(-1) || len == 0) return (int)len; |
106 | |
107 | crc = updcrc((uch*)buf, len); |
108 | isize += (ulg)len; |
109 | return (int)len; |
110 | } |