/[pkg-mixmaster]/trunk/Mix/Src/compress.c
ViewVC logotype

Contents of /trunk/Mix/Src/compress.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 262 - (show annotations) (download)
Wed Sep 18 23:26:17 2002 UTC (10 years, 8 months ago) by rabbi
File MIME type: text/plain
File size: 4616 byte(s)
Added closing comments for all #ifdef statements. All #endif's, as well as
nested braces, should be commented to reference their start.

We need to provide comments before every function as well.
1 /* Mixmaster version 3 -- (C) 1999 Anonymizer Inc.
2
3 Mixmaster may be redistributed and modified under certain conditions.
4 This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF
5 ANY KIND, either express or implied. See the file COPYRIGHT for
6 details.
7
8 Buffer compression (interface to zlib)
9 $Id: compress.c,v 1.2 2002/09/18 23:26:16 rabbi Exp $ */
10
11
12 #include "mix3.h"
13 #include <stdio.h>
14 #include <assert.h>
15
16 static byte gz_magic[2] =
17 {0x1f, 0x8b}; /* gzip magic header */
18
19 /* gzip flag byte */
20 #define ASCII_FLAG 0x01
21 #define HEAD_CRC 0x02
22 #define EXTRA_FIELD 0x04
23 #define ORIG_NAME 0x08
24 #define COMMENT 0x10
25 #define RESERVED 0xE0
26 #define Z_DEFLATED 8
27
28 #ifdef USE_ZLIB
29 #include "zlib.h"
30
31 int buf_unzip(BUFFER *in, int type)
32 {
33 BUFFER *out;
34 z_stream s;
35 long outstart;
36 int err;
37 int ret = 0;
38
39 out = buf_new();
40
41 s.zalloc = (alloc_func) 0;
42 s.zfree = (free_func) 0;
43 s.opaque = (voidpf) 0;
44
45 s.next_in = in->data + in->ptr;
46 s.avail_in = in->length + 1 - in->ptr; /* terminating 0 byte as "dummy" */
47 s.next_out = NULL;
48
49 if (type == 1)
50 err = inflateInit(&s); /* zlib */
51 else
52 err = inflateInit2(&s, -MAX_WBITS);
53 if (err != Z_OK) {
54 ret = -1;
55 goto end;
56 }
57 outstart = 0;
58 buf_append(out, NULL, in->length * 15 / 10);
59
60 for (;;) {
61 s.next_out = out->data + s.total_out + outstart;
62 s.avail_out = out->length - outstart - s.total_out;
63 err = inflate(&s, Z_PARTIAL_FLUSH);
64 out->length -= s.avail_out;
65 if (err != Z_OK)
66 break;
67 buf_append(out, NULL, BUFSIZE);
68 }
69 if (err != Z_STREAM_END)
70 errlog(WARNING, "Decompression error %d\n", err);
71
72 err = inflateEnd(&s);
73 if (err != Z_OK)
74 ret = -1;
75 end:
76 if (ret != 0)
77 switch (err) {
78 case Z_STREAM_ERROR:
79 errlog(ERRORMSG, "Decompression error Z_STREAM_ERROR.\n", err);
80 break;
81 case Z_MEM_ERROR:
82 errlog(ERRORMSG, "Decompression error Z_MEM_ERROR.\n", err);
83 break;
84 case Z_BUF_ERROR:
85 errlog(ERRORMSG, "Decompression error Z_BUF_ERROR.\n", err);
86 break;
87 case Z_VERSION_ERROR:
88 errlog(ERRORMSG, "Decompression error Z_VERSION_ERROR.\n", err);
89 break;
90 default:
91 errlog(ERRORMSG, "Decompression error %d.\n", err);
92 }
93 buf_move(in, out);
94 buf_free(out);
95 return (ret);
96 }
97
98 int buf_zip(BUFFER *out, BUFFER *in, int bits)
99 {
100 z_stream s;
101 long outstart;
102 int err = -1;
103
104 assert(in != out);
105
106 s.zalloc = (alloc_func) 0;
107 s.zfree = (free_func) 0;
108 s.opaque = (voidpf) 0;
109 s.next_in = NULL;
110
111 if (bits == 0)
112 bits = MAX_WBITS;
113
114 if (deflateInit2(&s, Z_DEFAULT_COMPRESSION, Z_DEFLATED, -bits, 8, 0) != Z_OK)
115 goto end;
116
117 outstart = out->length;
118 buf_append(out, NULL, in->length); /* try to fit all into one chunk */
119
120 s.next_in = in->data;
121 s.avail_in = in->length;
122
123 for (;;) {
124 s.next_out = out->data + s.total_out + outstart;
125 s.avail_out = out->length - outstart - s.total_out;
126 err = deflate(&s, Z_FINISH);
127 out->length -= s.avail_out;
128 if (err != Z_OK)
129 break;
130 buf_append(out, NULL, BUFSIZE);
131 }
132 if (deflateEnd(&s) != Z_OK || err != Z_STREAM_END)
133 err = -1;
134 else
135 err = 0;
136 end:
137 if (err != 0)
138 errlog(ERRORMSG, "Compression error.\n");
139 return (err);
140 }
141
142 #else /* end of USE_ZLIB */
143 int buf_zip(BUFFER *out, BUFFER *in, int bits)
144 {
145 return (-1);
146 }
147
148 int buf_unzip(BUFFER *b, int type)
149 {
150 errlog(ERRORMSG, "Can't uncompress: no zlib\n");
151 return (-1);
152 }
153 #endif /* else not USE_ZLIB */
154
155 int compressed(BUFFER *b)
156 {
157 return (b->length >= 10 && b->data[0] == gz_magic[0] &&
158 b->data[1] == gz_magic[1]);
159 }
160
161 int buf_uncompress(BUFFER *in)
162 {
163 int type;
164 int err = -1;
165 unsigned int len;
166
167 if (!compressed(in))
168 return (0);
169 type = in->data[3];
170 if (in->data[2] != Z_DEFLATED || (type & RESERVED) == 0) {
171 in->ptr = 10;
172 if ((type & EXTRA_FIELD) != 0) {
173 len = buf_geti(in);
174 in->ptr += len;
175 }
176 if ((type & ORIG_NAME) != 0)
177 while (buf_getc(in) > 0) ;
178 if ((type & COMMENT) != 0)
179 while (buf_getc(in) > 0) ;
180 if ((type & HEAD_CRC) != 0)
181 buf_geti(in);
182 err = buf_unzip(in, 0);
183 }
184 return (err);
185 }
186
187 int buf_compress(BUFFER *in)
188 {
189 BUFFER *out;
190 int err;
191
192 if (compressed(in))
193 return (0);
194
195 out = buf_new();
196 buf_appendc(out, gz_magic[0]);
197 buf_appendc(out, gz_magic[1]);
198 buf_appendc(out, Z_DEFLATED);
199 buf_appendc(out, 0); /* flags */
200 buf_appendl(out, 0); /* time */
201 buf_appendc(out, 0); /* xflags */
202 buf_appendc(out, 3); /* Unix */
203 err = buf_zip(out, in, 0);
204 if (err == 0)
205 buf_move(in, out);
206 buf_free(out);
207 return (err);
208 }

  ViewVC Help
Powered by ViewVC 1.1.5