2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.16 2007/04/05 05:18:16 kientzle Exp $");
46 #include "archive_private.h"
47 #include "archive_read_private.h"
49 struct archive_decompress_none {
52 char *next; /* Current read location. */
53 size_t avail; /* Bytes in my buffer. */
54 const void *client_buff; /* Client buffer information. */
56 const char *client_next;
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
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 *,
85 static off_t archive_decompressor_none_skip(struct archive_read *, off_t);
88 archive_read_support_compression_none(struct archive *_a)
90 struct archive_read *a = (struct archive_read *)_a;
91 return (__archive_read_register_compression(a,
92 archive_decompressor_none_bid,
93 archive_decompressor_none_init));
97 * Try to detect an "uncompressed" archive.
100 archive_decompressor_none_bid(const void *buff, size_t len)
105 return (1); /* Default: We'll take it if noone else does. */
109 archive_decompressor_none_init(struct archive_read *a, const void *buff, size_t n)
111 struct archive_decompress_none *state;
113 a->archive.compression_code = ARCHIVE_COMPRESSION_NONE;
114 a->archive.compression_name = "none";
116 state = (struct archive_decompress_none *)malloc(sizeof(*state));
118 archive_set_error(&a->archive, ENOMEM, "Can't allocate input data");
119 return (ARCHIVE_FATAL);
121 memset(state, 0, sizeof(*state));
123 state->buffer_size = BUFFER_SIZE;
124 state->buffer = (char *)malloc(state->buffer_size);
125 state->next = state->buffer;
126 if (state->buffer == NULL) {
128 archive_set_error(&a->archive, ENOMEM, "Can't allocate input buffer");
129 return (ARCHIVE_FATAL);
132 /* Save reference to first block of data. */
133 state->client_buff = buff;
134 state->client_total = n;
135 state->client_next = state->client_buff;
136 state->client_avail = state->client_total;
138 a->compression_data = state;
139 a->compression_read_ahead = archive_decompressor_none_read_ahead;
140 a->compression_read_consume = archive_decompressor_none_read_consume;
141 a->compression_skip = archive_decompressor_none_skip;
142 a->compression_finish = archive_decompressor_none_finish;
148 * We just pass through pointers to the client buffer if we can.
149 * If the client buffer is short, then we copy stuff to our internal
150 * buffer to combine reads.
153 archive_decompressor_none_read_ahead(struct archive_read *a, const void **buff,
156 struct archive_decompress_none *state;
159 state = (struct archive_decompress_none *)a->compression_data;
164 * Don't make special efforts to handle requests larger than
167 if (min > state->buffer_size)
168 min = state->buffer_size;
171 * Try to satisfy the request directly from the client
172 * buffer. We can do this if all of the data in the copy
173 * buffer was copied from the current client buffer. This
174 * also covers the case where the copy buffer is empty and
175 * the client buffer has all the data we need.
177 if (state->client_total >= state->client_avail + state->avail
178 && state->client_avail + state->avail >= min) {
179 state->client_avail += state->avail;
180 state->client_next -= state->avail;
182 state->next = state->buffer;
183 *buff = state->client_next;
184 return (state->client_avail);
188 * If we can't use client buffer, we'll have to use copy buffer.
191 /* Move data forward in copy buffer if necessary. */
192 if (state->next > state->buffer &&
193 state->next + min > state->buffer + state->buffer_size) {
194 if (state->avail > 0)
195 memmove(state->buffer, state->next, state->avail);
196 state->next = state->buffer;
199 /* Collect data in copy buffer to fulfill request. */
200 while (state->avail < min) {
201 /* Copy data from client buffer to our copy buffer. */
202 if (state->client_avail > 0) {
203 /* First estimate: copy to fill rest of buffer. */
204 size_t tocopy = (state->buffer + state->buffer_size)
205 - (state->next + state->avail);
206 /* Don't copy more than is available. */
207 if (tocopy > state->client_avail)
208 tocopy = state->client_avail;
209 memcpy(state->next + state->avail, state->client_next,
211 state->client_next += tocopy;
212 state->client_avail -= tocopy;
213 state->avail += tocopy;
215 /* There is no more client data: fetch more. */
217 * It seems to me that const void ** and const
218 * char ** should be compatible, but they
219 * aren't, hence the cast.
221 bytes_read = (a->client_reader)(&a->archive,
222 a->client_data, &state->client_buff);
223 if (bytes_read < 0) { /* Read error. */
224 state->client_total = state->client_avail = 0;
225 state->client_next = state->client_buff = NULL;
229 if (bytes_read == 0) { /* End-of-file. */
230 state->client_total = state->client_avail = 0;
231 state->client_next = state->client_buff = NULL;
232 state->end_of_file = 1;
235 a->archive.raw_position += bytes_read;
236 state->client_total = bytes_read;
237 state->client_avail = state->client_total;
238 state->client_next = state->client_buff;
243 return (state->avail);
247 * Mark the appropriate data as used. Note that the request here will
248 * often be much smaller than the size of the previous read_ahead
252 archive_decompressor_none_read_consume(struct archive_read *a, size_t request)
254 struct archive_decompress_none *state;
256 state = (struct archive_decompress_none *)a->compression_data;
257 if (state->avail > 0) {
258 /* Read came from copy buffer. */
259 state->next += request;
260 state->avail -= request;
262 /* Read came from client buffer. */
263 state->client_next += request;
264 state->client_avail -= request;
266 a->archive.file_position += request;
271 * Skip forward by exactly the requested bytes or else return
272 * ARCHIVE_FATAL. Note that this differs from the contract for
273 * read_ahead, which does not guarantee a minimum count.
276 archive_decompressor_none_skip(struct archive_read *a, off_t request)
278 struct archive_decompress_none *state;
279 off_t bytes_skipped, total_bytes_skipped = 0;
282 state = (struct archive_decompress_none *)a->compression_data;
286 * If there is data in the buffers already, use that first.
288 if (state->avail > 0) {
289 min = minimum(request, (off_t)state->avail);
290 bytes_skipped = archive_decompressor_none_read_consume(a, min);
291 request -= bytes_skipped;
292 total_bytes_skipped += bytes_skipped;
294 if (state->client_avail > 0) {
295 min = minimum(request, (off_t)state->client_avail);
296 bytes_skipped = archive_decompressor_none_read_consume(a, min);
297 request -= bytes_skipped;
298 total_bytes_skipped += bytes_skipped;
301 return (total_bytes_skipped);
303 * If a client_skipper was provided, try that first.
305 #if ARCHIVE_API_VERSION < 2
306 if ((a->client_skipper != NULL) && (request < SSIZE_MAX)) {
308 if (a->client_skipper != NULL) {
310 bytes_skipped = (a->client_skipper)(&a->archive,
311 a->client_data, request);
312 if (bytes_skipped < 0) { /* error */
313 state->client_total = state->client_avail = 0;
314 state->client_next = state->client_buff = NULL;
316 return (bytes_skipped);
318 total_bytes_skipped += bytes_skipped;
319 a->archive.file_position += bytes_skipped;
320 request -= bytes_skipped;
321 state->client_next = state->client_buff;
322 a->archive.raw_position += bytes_skipped;
323 state->client_avail = state->client_total = 0;
326 * Note that client_skipper will usually not satisfy the
327 * full request (due to low-level blocking concerns),
328 * so even if client_skipper is provided, we may still
329 * have to use ordinary reads to finish out the request.
331 while (request > 0) {
332 const void* dummy_buffer;
334 bytes_read = archive_decompressor_none_read_ahead(a,
335 &dummy_buffer, request);
338 if (bytes_read == 0) {
339 /* We hit EOF before we satisfied the skip request. */
340 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
341 "Truncated input file (need to skip %jd bytes)",
343 return (ARCHIVE_FATAL);
345 min = (size_t)(minimum(bytes_read, request));
346 bytes_read = archive_decompressor_none_read_consume(a, min);
347 total_bytes_skipped += bytes_read;
348 request -= bytes_read;
350 return (total_bytes_skipped);
354 archive_decompressor_none_finish(struct archive_read *a)
356 struct archive_decompress_none *state;
358 state = (struct archive_decompress_none *)a->compression_data;
361 a->compression_data = NULL;
362 if (a->client_closer != NULL)
363 return ((a->client_closer)(&a->archive, a->client_data));