Upgrade to libarchive 2.0.25 which gives us a nice speed boost along with
[dragonfly/port-amd64.git] / contrib / libarchive-1.3.1 / libarchive / archive_read_support_compression_none.c
blob9af7853d8f4889677930f0745965629cb239bed7
1 /*-
2 * Copyright (c) 2003-2004 Tim Kientzle
3 * All rights reserved.
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
9 * notice, this list of conditions and the following disclaimer
10 * in this position and unchanged.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_support_compression_none.c,v 1.8 2006/08/29 04:59:25 kientzle Exp $");
30 #include <assert.h>
31 #include <errno.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
36 #include "archive.h"
37 #include "archive_private.h"
39 struct archive_decompress_none {
40 char *buffer;
41 size_t buffer_size;
42 char *next; /* Current read location. */
43 size_t avail; /* Bytes in my buffer. */
44 const char *client_buff; /* Client buffer information. */
45 size_t client_total;
46 const char *client_next;
47 size_t client_avail;
48 char end_of_file;
49 char fatal;
53 * Size of internal buffer used for combining short reads. This is
54 * also an upper limit on the size of a read request. Recall,
55 * however, that we can (and will!) return blocks of data larger than
56 * this. The read semantics are: you ask for a minimum, I give you a
57 * pointer to my best-effort match and tell you how much data is
58 * there. It could be less than you asked for, it could be much more.
59 * For example, a client might use mmap() to "read" the entire file as
60 * a single block. In that case, I will return that entire block to
61 * my clients.
63 #define BUFFER_SIZE 65536
65 #define minimum(a, b) (a < b ? a : b)
67 static int archive_decompressor_none_bid(const void *, size_t);
68 static int archive_decompressor_none_finish(struct archive *);
69 static int archive_decompressor_none_init(struct archive *,
70 const void *, size_t);
71 static ssize_t archive_decompressor_none_read_ahead(struct archive *,
72 const void **, size_t);
73 static ssize_t archive_decompressor_none_read_consume(struct archive *,
74 size_t);
75 static ssize_t archive_decompressor_none_skip(struct archive *, size_t);
77 int
78 archive_read_support_compression_none(struct archive *a)
80 return (__archive_read_register_compression(a,
81 archive_decompressor_none_bid,
82 archive_decompressor_none_init));
86 * Try to detect an "uncompressed" archive.
88 static int
89 archive_decompressor_none_bid(const void *buff, size_t len)
91 (void)buff;
92 (void)len;
94 return (1); /* Default: We'll take it if noone else does. */
97 static int
98 archive_decompressor_none_init(struct archive *a, const void *buff, size_t n)
100 struct archive_decompress_none *state;
102 a->compression_code = ARCHIVE_COMPRESSION_NONE;
103 a->compression_name = "none";
105 state = (struct archive_decompress_none *)malloc(sizeof(*state));
106 if (!state) {
107 archive_set_error(a, ENOMEM, "Can't allocate input data");
108 return (ARCHIVE_FATAL);
110 memset(state, 0, sizeof(*state));
112 state->buffer_size = BUFFER_SIZE;
113 state->buffer = malloc(state->buffer_size);
114 state->next = state->buffer;
115 if (state->buffer == NULL) {
116 free(state);
117 archive_set_error(a, ENOMEM, "Can't allocate input buffer");
118 return (ARCHIVE_FATAL);
121 /* Save reference to first block of data. */
122 state->client_buff = buff;
123 state->client_total = n;
124 state->client_next = state->client_buff;
125 state->client_avail = state->client_total;
127 a->compression_data = state;
128 a->compression_read_ahead = archive_decompressor_none_read_ahead;
129 a->compression_read_consume = archive_decompressor_none_read_consume;
130 a->compression_skip = archive_decompressor_none_skip;
131 a->compression_finish = archive_decompressor_none_finish;
133 return (ARCHIVE_OK);
137 * We just pass through pointers to the client buffer if we can.
138 * If the client buffer is short, then we copy stuff to our internal
139 * buffer to combine reads.
141 static ssize_t
142 archive_decompressor_none_read_ahead(struct archive *a, const void **buff,
143 size_t min)
145 struct archive_decompress_none *state;
146 ssize_t bytes_read;
148 state = a->compression_data;
149 if (state->fatal)
150 return (-1);
153 * Don't make special efforts to handle requests larger than
154 * the copy buffer.
156 if (min > state->buffer_size)
157 min = state->buffer_size;
160 * Try to satisfy the request directly from the client
161 * buffer. We can do this if all of the data in the copy
162 * buffer was copied from the current client buffer. This
163 * also covers the case where the copy buffer is empty and
164 * the client buffer has all the data we need.
166 if (state->client_total >= state->client_avail + state->avail
167 && state->client_avail + state->avail >= min) {
168 state->client_avail += state->avail;
169 state->client_next -= state->avail;
170 state->avail = 0;
171 state->next = state->buffer;
172 *buff = state->client_next;
173 return (state->client_avail);
177 * If we can't use client buffer, we'll have to use copy buffer.
180 /* Move data forward in copy buffer if necessary. */
181 if (state->next > state->buffer &&
182 state->next + min > state->buffer + state->buffer_size) {
183 if (state->avail > 0)
184 memmove(state->buffer, state->next, state->avail);
185 state->next = state->buffer;
188 /* Collect data in copy buffer to fulfill request. */
189 while (state->avail < min) {
190 /* Copy data from client buffer to our copy buffer. */
191 if (state->client_avail > 0) {
192 /* First estimate: copy to fill rest of buffer. */
193 size_t tocopy = (state->buffer + state->buffer_size)
194 - (state->next + state->avail);
195 /* Don't copy more than is available. */
196 if (tocopy > state->client_avail)
197 tocopy = state->client_avail;
198 memcpy(state->next + state->avail, state->client_next,
199 tocopy);
200 state->client_next += tocopy;
201 state->client_avail -= tocopy;
202 state->avail += tocopy;
203 } else {
204 /* There is no more client data: fetch more. */
206 * It seems to me that const void ** and const
207 * char ** should be compatible, but they
208 * aren't, hence the cast.
210 bytes_read = (a->client_reader)(a, a->client_data,
211 (const void **)&state->client_buff);
212 if (bytes_read < 0) { /* Read error. */
213 state->client_total = state->client_avail = 0;
214 state->client_next = state->client_buff = NULL;
215 state->fatal = 1;
216 return (-1);
218 if (bytes_read == 0) { /* End-of-file. */
219 state->client_total = state->client_avail = 0;
220 state->client_next = state->client_buff = NULL;
221 state->end_of_file = 1;
222 break;
224 a->raw_position += bytes_read;
225 state->client_total = bytes_read;
226 state->client_avail = state->client_total;
227 state->client_next = state->client_buff;
231 *buff = state->next;
232 return (state->avail);
236 * Mark the appropriate data as used. Note that the request here will
237 * often be much smaller than the size of the previous read_ahead
238 * request.
240 static ssize_t
241 archive_decompressor_none_read_consume(struct archive *a, size_t request)
243 struct archive_decompress_none *state;
245 state = a->compression_data;
246 if (state->avail > 0) {
247 /* Read came from copy buffer. */
248 state->next += request;
249 state->avail -= request;
250 } else {
251 /* Read came from client buffer. */
252 state->client_next += request;
253 state->client_avail -= request;
255 a->file_position += request;
256 return (request);
260 * Skip at most request bytes. Skipped data is marked as consumed.
262 static ssize_t
263 archive_decompressor_none_skip(struct archive *a, size_t request)
265 struct archive_decompress_none *state;
266 ssize_t bytes_skipped, total_bytes_skipped = 0;
267 size_t min;
269 state = a->compression_data;
270 if (state->fatal)
271 return (-1);
273 * If there is data in the buffers already, use that first.
275 if (state->avail > 0) {
276 min = minimum(request, state->avail);
277 bytes_skipped = archive_decompressor_none_read_consume(a, min);
278 request -= bytes_skipped;
279 total_bytes_skipped += bytes_skipped;
281 if (state->client_avail > 0) {
282 min = minimum(request, state->client_avail);
283 bytes_skipped = archive_decompressor_none_read_consume(a, min);
284 request -= bytes_skipped;
285 total_bytes_skipped += bytes_skipped;
287 if (request == 0)
288 return (total_bytes_skipped);
290 * If no client_skipper is provided, just read the old way. It is very
291 * likely that after skipping, the request has not yet been fully
292 * satisfied (and is still > 0). In that case, read as well.
294 if (a->client_skipper != NULL) {
295 bytes_skipped = (a->client_skipper)(a, a->client_data,
296 request);
297 if (bytes_skipped < 0) { /* error */
298 state->client_total = state->client_avail = 0;
299 state->client_next = state->client_buff = NULL;
300 state->fatal = 1;
301 return (bytes_skipped);
303 total_bytes_skipped += bytes_skipped;
304 a->file_position += bytes_skipped;
305 request -= bytes_skipped;
306 state->client_next = state->client_buff;
307 a->raw_position += bytes_skipped;
308 state->client_avail = state->client_total = 0;
310 while (request > 0) {
311 const void* dummy_buffer;
312 ssize_t bytes_read;
313 bytes_read = archive_decompressor_none_read_ahead(a,
314 &dummy_buffer, request);
315 if (bytes_read < 0)
316 return (bytes_read);
317 assert(bytes_read >= 0); /* precondition for cast below */
318 min = minimum((size_t)bytes_read, request);
319 bytes_read = archive_decompressor_none_read_consume(a, min);
320 total_bytes_skipped += bytes_read;
321 request -= bytes_read;
323 assert(request == 0);
324 return (total_bytes_skipped);
327 static int
328 archive_decompressor_none_finish(struct archive *a)
330 struct archive_decompress_none *state;
332 state = a->compression_data;
333 free(state->buffer);
334 free(state);
335 a->compression_data = NULL;
336 if (a->client_closer != NULL)
337 return ((a->client_closer)(a, a->client_data));
338 return (ARCHIVE_OK);