diff(1): Commit our patches to contrib/ and get rid of -I-.
[dragonfly.git] / contrib / libarchive-2 / libarchive / archive_read_support_compression_none.c
blob3f17756abaed1394eebfc00abb7c452d04fca311
1 /*-
2 * Copyright (c) 2003-2007 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 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_support_compression_none.c,v 1.19 2007/12/30 04:58:21 kientzle Exp $");
29 #ifdef HAVE_ERRNO_H
30 #include <errno.h>
31 #endif
32 #ifdef HAVE_LIMITS_H
33 #include <limits.h>
34 #endif
35 #ifdef HAVE_STDLIB_H
36 #include <stdlib.h>
37 #endif
38 #ifdef HAVE_STRING_H
39 #include <string.h>
40 #endif
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
45 #include "archive.h"
46 #include "archive_private.h"
47 #include "archive_read_private.h"
49 struct archive_decompress_none {
50 char *buffer;
51 size_t buffer_size;
52 char *next; /* Current read location. */
53 size_t avail; /* Bytes in my buffer. */
54 const void *client_buff; /* Client buffer information. */
55 size_t client_total;
56 const char *client_next;
57 size_t client_avail;
58 char end_of_file;
59 char fatal;
63 * Size of internal buffer used for combining short reads. This is
64 * also an upper limit on the size of a read request. Recall,
65 * however, that we can (and will!) return blocks of data larger than
66 * this. The read semantics are: you ask for a minimum, I give you a
67 * pointer to my best-effort match and tell you how much data is
68 * there. It could be less than you asked for, it could be much more.
69 * For example, a client might use mmap() to "read" the entire file as
70 * a single block. In that case, I will return that entire block to
71 * my clients.
73 #define BUFFER_SIZE 65536
75 #define minimum(a, b) (a < b ? a : b)
77 static int archive_decompressor_none_bid(const void *, size_t);
78 static int archive_decompressor_none_finish(struct archive_read *);
79 static int archive_decompressor_none_init(struct archive_read *,
80 const void *, size_t);
81 static ssize_t archive_decompressor_none_read_ahead(struct archive_read *,
82 const void **, size_t);
83 static ssize_t archive_decompressor_none_read_consume(struct archive_read *,
84 size_t);
85 static off_t archive_decompressor_none_skip(struct archive_read *, off_t);
87 int
88 archive_read_support_compression_none(struct archive *_a)
90 struct archive_read *a = (struct archive_read *)_a;
91 if (__archive_read_register_compression(a,
92 archive_decompressor_none_bid,
93 archive_decompressor_none_init) != NULL)
94 return (ARCHIVE_OK);
95 return (ARCHIVE_FATAL);
99 * Try to detect an "uncompressed" archive.
101 static int
102 archive_decompressor_none_bid(const void *buff, size_t len)
104 (void)buff;
105 (void)len;
107 return (1); /* Default: We'll take it if noone else does. */
110 static int
111 archive_decompressor_none_init(struct archive_read *a, const void *buff, size_t n)
113 struct archive_decompress_none *state;
115 a->archive.compression_code = ARCHIVE_COMPRESSION_NONE;
116 a->archive.compression_name = "none";
118 state = (struct archive_decompress_none *)malloc(sizeof(*state));
119 if (!state) {
120 archive_set_error(&a->archive, ENOMEM, "Can't allocate input data");
121 return (ARCHIVE_FATAL);
123 memset(state, 0, sizeof(*state));
125 state->buffer_size = BUFFER_SIZE;
126 state->buffer = (char *)malloc(state->buffer_size);
127 state->next = state->buffer;
128 if (state->buffer == NULL) {
129 free(state);
130 archive_set_error(&a->archive, ENOMEM, "Can't allocate input buffer");
131 return (ARCHIVE_FATAL);
134 /* Save reference to first block of data. */
135 state->client_buff = buff;
136 state->client_total = n;
137 state->client_next = state->client_buff;
138 state->client_avail = state->client_total;
140 a->decompressor->data = state;
141 a->decompressor->read_ahead = archive_decompressor_none_read_ahead;
142 a->decompressor->consume = archive_decompressor_none_read_consume;
143 a->decompressor->skip = archive_decompressor_none_skip;
144 a->decompressor->finish = archive_decompressor_none_finish;
146 return (ARCHIVE_OK);
150 * We just pass through pointers to the client buffer if we can.
151 * If the client buffer is short, then we copy stuff to our internal
152 * buffer to combine reads.
154 static ssize_t
155 archive_decompressor_none_read_ahead(struct archive_read *a, const void **buff,
156 size_t min)
158 struct archive_decompress_none *state;
159 ssize_t bytes_read;
161 state = (struct archive_decompress_none *)a->decompressor->data;
162 if (state->fatal)
163 return (-1);
166 * Don't make special efforts to handle requests larger than
167 * the copy buffer.
169 if (min > state->buffer_size)
170 min = state->buffer_size;
173 * Keep pulling more data until we can satisfy the request.
175 for (;;) {
178 * If we can satisfy from the copy buffer, we're done.
180 if (state->avail >= min) {
181 *buff = state->next;
182 return (state->avail);
186 * We can satisfy directly from client buffer if everything
187 * currently in the copy buffer is still in the client buffer.
189 if (state->client_total >= state->client_avail + state->avail
190 && state->client_avail + state->avail >= min) {
191 /* "Roll back" to client buffer. */
192 state->client_avail += state->avail;
193 state->client_next -= state->avail;
194 /* Copy buffer is now empty. */
195 state->avail = 0;
196 state->next = state->buffer;
197 /* Return data from client buffer. */
198 *buff = state->client_next;
199 return (state->client_avail);
202 /* Move data forward in copy buffer if necessary. */
203 if (state->next > state->buffer &&
204 state->next + min > state->buffer + state->buffer_size) {
205 if (state->avail > 0)
206 memmove(state->buffer, state->next, state->avail);
207 state->next = state->buffer;
210 /* If we've used up the client data, get more. */
211 if (state->client_avail <= 0) {
212 bytes_read = (a->client_reader)(&a->archive,
213 a->client_data, &state->client_buff);
214 if (bytes_read < 0) { /* Read error. */
215 state->client_total = state->client_avail = 0;
216 state->client_next = state->client_buff = NULL;
217 state->fatal = 1;
218 return (-1);
220 if (bytes_read == 0) { /* End-of-file. */
221 state->client_total = state->client_avail = 0;
222 state->client_next = state->client_buff = NULL;
223 state->end_of_file = 1;
224 /* Return whatever we do have. */
225 *buff = state->next;
226 return (state->avail);
228 a->archive.raw_position += bytes_read;
229 state->client_total = bytes_read;
230 state->client_avail = state->client_total;
231 state->client_next = state->client_buff;
233 else
235 /* We can add client data to copy buffer. */
236 /* First estimate: copy to fill rest of buffer. */
237 size_t tocopy = (state->buffer + state->buffer_size)
238 - (state->next + state->avail);
239 /* Don't copy more than is available. */
240 if (tocopy > state->client_avail)
241 tocopy = state->client_avail;
242 memcpy(state->next + state->avail, state->client_next,
243 tocopy);
244 /* Remove this data from client buffer. */
245 state->client_next += tocopy;
246 state->client_avail -= tocopy;
247 /* add it to copy buffer. */
248 state->avail += tocopy;
254 * Mark the appropriate data as used. Note that the request here will
255 * often be much smaller than the size of the previous read_ahead
256 * request.
258 static ssize_t
259 archive_decompressor_none_read_consume(struct archive_read *a, size_t request)
261 struct archive_decompress_none *state;
263 state = (struct archive_decompress_none *)a->decompressor->data;
264 if (state->avail > 0) {
265 /* Read came from copy buffer. */
266 state->next += request;
267 state->avail -= request;
268 } else {
269 /* Read came from client buffer. */
270 state->client_next += request;
271 state->client_avail -= request;
273 a->archive.file_position += request;
274 return (request);
278 * Skip forward by exactly the requested bytes or else return
279 * ARCHIVE_FATAL. Note that this differs from the contract for
280 * read_ahead, which does not guarantee a minimum count.
282 static off_t
283 archive_decompressor_none_skip(struct archive_read *a, off_t request)
285 struct archive_decompress_none *state;
286 off_t bytes_skipped, total_bytes_skipped = 0;
287 size_t min;
289 state = (struct archive_decompress_none *)a->decompressor->data;
290 if (state->fatal)
291 return (-1);
293 * If there is data in the buffers already, use that first.
295 if (state->avail > 0) {
296 min = minimum(request, (off_t)state->avail);
297 bytes_skipped = archive_decompressor_none_read_consume(a, min);
298 request -= bytes_skipped;
299 total_bytes_skipped += bytes_skipped;
301 if (state->client_avail > 0) {
302 min = minimum(request, (off_t)state->client_avail);
303 bytes_skipped = archive_decompressor_none_read_consume(a, min);
304 request -= bytes_skipped;
305 total_bytes_skipped += bytes_skipped;
307 if (request == 0)
308 return (total_bytes_skipped);
310 * If a client_skipper was provided, try that first.
312 #if ARCHIVE_API_VERSION < 2
313 if ((a->client_skipper != NULL) && (request < SSIZE_MAX)) {
314 #else
315 if (a->client_skipper != NULL) {
316 #endif
317 bytes_skipped = (a->client_skipper)(&a->archive,
318 a->client_data, request);
319 if (bytes_skipped < 0) { /* error */
320 state->client_total = state->client_avail = 0;
321 state->client_next = state->client_buff = NULL;
322 state->fatal = 1;
323 return (bytes_skipped);
325 total_bytes_skipped += bytes_skipped;
326 a->archive.file_position += bytes_skipped;
327 request -= bytes_skipped;
328 state->client_next = state->client_buff;
329 a->archive.raw_position += bytes_skipped;
330 state->client_avail = state->client_total = 0;
333 * Note that client_skipper will usually not satisfy the
334 * full request (due to low-level blocking concerns),
335 * so even if client_skipper is provided, we may still
336 * have to use ordinary reads to finish out the request.
338 while (request > 0) {
339 const void* dummy_buffer;
340 ssize_t bytes_read;
341 bytes_read = archive_decompressor_none_read_ahead(a,
342 &dummy_buffer, 1);
343 if (bytes_read < 0)
344 return (bytes_read);
345 if (bytes_read == 0) {
346 /* We hit EOF before we satisfied the skip request. */
347 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
348 "Truncated input file (need to skip %jd bytes)",
349 (intmax_t)request);
350 return (ARCHIVE_FATAL);
352 min = (size_t)(minimum(bytes_read, request));
353 bytes_read = archive_decompressor_none_read_consume(a, min);
354 total_bytes_skipped += bytes_read;
355 request -= bytes_read;
357 return (total_bytes_skipped);
360 static int
361 archive_decompressor_none_finish(struct archive_read *a)
363 struct archive_decompress_none *state;
365 state = (struct archive_decompress_none *)a->decompressor->data;
366 free(state->buffer);
367 free(state);
368 a->decompressor->data = NULL;
369 return (ARCHIVE_OK);