Deleted Added
full compact
archive_read_support_filter_gzip.c (348607) archive_read_support_filter_gzip.c (353376)
1/*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 117 unchanged lines hidden (view full) ---

126 * Read and verify the header.
127 *
128 * Returns zero if the header couldn't be validated, else returns
129 * number of bytes in header. If pbits is non-NULL, it receives a
130 * count of bits verified, suitable for use by bidder.
131 */
132static ssize_t
133peek_at_header(struct archive_read_filter *filter, int *pbits,
1/*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 117 unchanged lines hidden (view full) ---

126 * Read and verify the header.
127 *
128 * Returns zero if the header couldn't be validated, else returns
129 * number of bytes in header. If pbits is non-NULL, it receives a
130 * count of bits verified, suitable for use by bidder.
131 */
132static ssize_t
133peek_at_header(struct archive_read_filter *filter, int *pbits,
134 struct private_data *state)
134#ifdef HAVE_ZLIB_H
135 struct private_data *state
136#else
137 void *state
138#endif
139 )
135{
136 const unsigned char *p;
137 ssize_t avail, len;
138 int bits = 0;
139 int header_flags;
140{
141 const unsigned char *p;
142 ssize_t avail, len;
143 int bits = 0;
144 int header_flags;
145#ifndef HAVE_ZLIB_H
146 (void)state; /* UNUSED */
147#endif
140
141 /* Start by looking at the first ten bytes of the header, which
142 * is all fixed layout. */
143 len = 10;
144 p = __archive_read_filter_ahead(filter, len, &avail);
145 if (p == NULL || avail == 0)
146 return (0);
147 /* We only support deflation- third byte must be 0x08. */
148 if (memcmp(p, "\x1F\x8B\x08", 3) != 0)
149 return (0);
150 bits += 24;
151 if ((p[3] & 0xE0)!= 0) /* No reserved flags set. */
152 return (0);
153 bits += 3;
154 header_flags = p[3];
155 /* Bytes 4-7 are mod time in little endian. */
148
149 /* Start by looking at the first ten bytes of the header, which
150 * is all fixed layout. */
151 len = 10;
152 p = __archive_read_filter_ahead(filter, len, &avail);
153 if (p == NULL || avail == 0)
154 return (0);
155 /* We only support deflation- third byte must be 0x08. */
156 if (memcmp(p, "\x1F\x8B\x08", 3) != 0)
157 return (0);
158 bits += 24;
159 if ((p[3] & 0xE0)!= 0) /* No reserved flags set. */
160 return (0);
161 bits += 3;
162 header_flags = p[3];
163 /* Bytes 4-7 are mod time in little endian. */
164#ifdef HAVE_ZLIB_H
156 if (state)
157 state->mtime = archive_le32dec(p + 4);
165 if (state)
166 state->mtime = archive_le32dec(p + 4);
167#endif
158 /* Byte 8 is deflate flags. */
159 /* XXXX TODO: return deflate flags back to consume_header for use
160 in initializing the decompressor. */
161 /* Byte 9 is OS. */
162
163 /* Optional extra data: 2 byte length plus variable body. */
164 if (header_flags & 4) {
165 p = __archive_read_filter_ahead(filter, len + 2, &avail);
166 if (p == NULL)
167 return (0);
168 len += ((int)p[len + 1] << 8) | (int)p[len];
169 len += 2;
170 }
171
172 /* Null-terminated optional filename. */
173 if (header_flags & 8) {
168 /* Byte 8 is deflate flags. */
169 /* XXXX TODO: return deflate flags back to consume_header for use
170 in initializing the decompressor. */
171 /* Byte 9 is OS. */
172
173 /* Optional extra data: 2 byte length plus variable body. */
174 if (header_flags & 4) {
175 p = __archive_read_filter_ahead(filter, len + 2, &avail);
176 if (p == NULL)
177 return (0);
178 len += ((int)p[len + 1] << 8) | (int)p[len];
179 len += 2;
180 }
181
182 /* Null-terminated optional filename. */
183 if (header_flags & 8) {
184#ifdef HAVE_ZLIB_H
174 ssize_t file_start = len;
185 ssize_t file_start = len;
186#endif
175 do {
176 ++len;
177 if (avail < len)
178 p = __archive_read_filter_ahead(filter,
179 len, &avail);
180 if (p == NULL)
181 return (0);
182 } while (p[len - 1] != 0);
183
187 do {
188 ++len;
189 if (avail < len)
190 p = __archive_read_filter_ahead(filter,
191 len, &avail);
192 if (p == NULL)
193 return (0);
194 } while (p[len - 1] != 0);
195
196#ifdef HAVE_ZLIB_H
184 if (state) {
185 /* Reset the name in case of repeat header reads. */
186 free(state->name);
187 state->name = strdup((const char *)&p[file_start]);
188 }
197 if (state) {
198 /* Reset the name in case of repeat header reads. */
199 free(state->name);
200 state->name = strdup((const char *)&p[file_start]);
201 }
202#endif
189 }
190
191 /* Null-terminated optional comment. */
192 if (header_flags & 16) {
193 do {
194 ++len;
195 if (avail < len)
196 p = __archive_read_filter_ahead(filter,

--- 34 unchanged lines hidden (view full) ---

231
232 (void)self; /* UNUSED */
233
234 if (peek_at_header(filter, &bits_checked, NULL))
235 return (bits_checked);
236 return (0);
237}
238
203 }
204
205 /* Null-terminated optional comment. */
206 if (header_flags & 16) {
207 do {
208 ++len;
209 if (avail < len)
210 p = __archive_read_filter_ahead(filter,

--- 34 unchanged lines hidden (view full) ---

245
246 (void)self; /* UNUSED */
247
248 if (peek_at_header(filter, &bits_checked, NULL))
249 return (bits_checked);
250 return (0);
251}
252
239static int
240gzip_read_header(struct archive_read_filter *self, struct archive_entry *entry)
241{
242 struct private_data *state;
243
244 state = (struct private_data *)self->data;
245
246 /* A mtime of 0 is considered invalid/missing. */
247 if (state->mtime != 0)
248 archive_entry_set_mtime(entry, state->mtime, 0);
249
250 /* If the name is available, extract it. */
251 if (state->name)
252 archive_entry_set_pathname(entry, state->name);
253
254 return (ARCHIVE_OK);
255}
256
257#ifndef HAVE_ZLIB_H
258
259/*
260 * If we don't have the library on this system, we can't do the
261 * decompression directly. We can, however, try to run "gzip -d"
262 * in case that's available.
263 */
264static int

--- 7 unchanged lines hidden (view full) ---

272 * even if we weren't able to read it. */
273 self->code = ARCHIVE_FILTER_GZIP;
274 self->name = "gzip";
275 return (r);
276}
277
278#else
279
253#ifndef HAVE_ZLIB_H
254
255/*
256 * If we don't have the library on this system, we can't do the
257 * decompression directly. We can, however, try to run "gzip -d"
258 * in case that's available.
259 */
260static int

--- 7 unchanged lines hidden (view full) ---

268 * even if we weren't able to read it. */
269 self->code = ARCHIVE_FILTER_GZIP;
270 self->name = "gzip";
271 return (r);
272}
273
274#else
275
276static int
277gzip_read_header(struct archive_read_filter *self, struct archive_entry *entry)
278{
279 struct private_data *state;
280
281 state = (struct private_data *)self->data;
282
283 /* A mtime of 0 is considered invalid/missing. */
284 if (state->mtime != 0)
285 archive_entry_set_mtime(entry, state->mtime, 0);
286
287 /* If the name is available, extract it. */
288 if (state->name)
289 archive_entry_set_pathname(entry, state->name);
290
291 return (ARCHIVE_OK);
292}
293
280/*
281 * Initialize the filter object.
282 */
283static int
284gzip_bidder_init(struct archive_read_filter *self)
285{
286 struct private_data *state;
287 static const size_t out_block_size = 64 * 1024;

--- 13 unchanged lines hidden (view full) ---

301 }
302
303 self->data = state;
304 state->out_block_size = out_block_size;
305 state->out_block = out_block;
306 self->read = gzip_filter_read;
307 self->skip = NULL; /* not supported */
308 self->close = gzip_filter_close;
294/*
295 * Initialize the filter object.
296 */
297static int
298gzip_bidder_init(struct archive_read_filter *self)
299{
300 struct private_data *state;
301 static const size_t out_block_size = 64 * 1024;

--- 13 unchanged lines hidden (view full) ---

315 }
316
317 self->data = state;
318 state->out_block_size = out_block_size;
319 state->out_block = out_block;
320 self->read = gzip_filter_read;
321 self->skip = NULL; /* not supported */
322 self->close = gzip_filter_close;
323#ifdef HAVE_ZLIB_H
309 self->read_header = gzip_read_header;
324 self->read_header = gzip_read_header;
325#endif
310
311 state->in_stream = 0; /* We're not actually within a stream yet. */
312
313 return (ARCHIVE_OK);
314}
315
316static int
317consume_header(struct archive_read_filter *self)

--- 202 unchanged lines hidden ---
326
327 state->in_stream = 0; /* We're not actually within a stream yet. */
328
329 return (ARCHIVE_OK);
330}
331
332static int
333consume_header(struct archive_read_filter *self)

--- 202 unchanged lines hidden ---