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 if (__archive_read_register_compression(a
,
92 archive_decompressor_none_bid
,
93 archive_decompressor_none_init
) != NULL
)
95 return (ARCHIVE_FATAL
);
99 * Try to detect an "uncompressed" archive.
102 archive_decompressor_none_bid(const void *buff
, size_t len
)
107 return (1); /* Default: We'll take it if noone else does. */
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
));
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
) {
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
;
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.
155 archive_decompressor_none_read_ahead(struct archive_read
*a
, const void **buff
,
158 struct archive_decompress_none
*state
;
161 state
= (struct archive_decompress_none
*)a
->decompressor
->data
;
166 * Don't make special efforts to handle requests larger than
169 if (min
> state
->buffer_size
)
170 min
= state
->buffer_size
;
173 * Try to satisfy the request directly from the client
174 * buffer. We can do this if all of the data in the copy
175 * buffer was copied from the current client buffer. This
176 * also covers the case where the copy buffer is empty and
177 * the client buffer has all the data we need.
179 if (state
->client_total
>= state
->client_avail
+ state
->avail
180 && state
->client_avail
+ state
->avail
>= min
) {
181 state
->client_avail
+= state
->avail
;
182 state
->client_next
-= state
->avail
;
184 state
->next
= state
->buffer
;
185 *buff
= state
->client_next
;
186 return (state
->client_avail
);
190 * If we can't use client buffer, we'll have to use copy buffer.
193 /* Move data forward in copy buffer if necessary. */
194 if (state
->next
> state
->buffer
&&
195 state
->next
+ min
> state
->buffer
+ state
->buffer_size
) {
196 if (state
->avail
> 0)
197 memmove(state
->buffer
, state
->next
, state
->avail
);
198 state
->next
= state
->buffer
;
201 /* Collect data in copy buffer to fulfill request. */
202 while (state
->avail
< min
) {
203 /* Copy data from client buffer to our copy buffer. */
204 if (state
->client_avail
> 0) {
205 /* First estimate: copy to fill rest of buffer. */
206 size_t tocopy
= (state
->buffer
+ state
->buffer_size
)
207 - (state
->next
+ state
->avail
);
208 /* Don't copy more than is available. */
209 if (tocopy
> state
->client_avail
)
210 tocopy
= state
->client_avail
;
211 memcpy(state
->next
+ state
->avail
, state
->client_next
,
213 state
->client_next
+= tocopy
;
214 state
->client_avail
-= tocopy
;
215 state
->avail
+= tocopy
;
217 /* There is no more client data: fetch more. */
219 * It seems to me that const void ** and const
220 * char ** should be compatible, but they
221 * aren't, hence the cast.
223 bytes_read
= (a
->client_reader
)(&a
->archive
,
224 a
->client_data
, &state
->client_buff
);
225 if (bytes_read
< 0) { /* Read error. */
226 state
->client_total
= state
->client_avail
= 0;
227 state
->client_next
= state
->client_buff
= NULL
;
231 if (bytes_read
== 0) { /* End-of-file. */
232 state
->client_total
= state
->client_avail
= 0;
233 state
->client_next
= state
->client_buff
= NULL
;
234 state
->end_of_file
= 1;
237 a
->archive
.raw_position
+= bytes_read
;
238 state
->client_total
= bytes_read
;
239 state
->client_avail
= state
->client_total
;
240 state
->client_next
= state
->client_buff
;
245 return (state
->avail
);
249 * Mark the appropriate data as used. Note that the request here will
250 * often be much smaller than the size of the previous read_ahead
254 archive_decompressor_none_read_consume(struct archive_read
*a
, size_t request
)
256 struct archive_decompress_none
*state
;
258 state
= (struct archive_decompress_none
*)a
->decompressor
->data
;
259 if (state
->avail
> 0) {
260 /* Read came from copy buffer. */
261 state
->next
+= request
;
262 state
->avail
-= request
;
264 /* Read came from client buffer. */
265 state
->client_next
+= request
;
266 state
->client_avail
-= request
;
268 a
->archive
.file_position
+= request
;
273 * Skip forward by exactly the requested bytes or else return
274 * ARCHIVE_FATAL. Note that this differs from the contract for
275 * read_ahead, which does not guarantee a minimum count.
278 archive_decompressor_none_skip(struct archive_read
*a
, off_t request
)
280 struct archive_decompress_none
*state
;
281 off_t bytes_skipped
, total_bytes_skipped
= 0;
284 state
= (struct archive_decompress_none
*)a
->decompressor
->data
;
288 * If there is data in the buffers already, use that first.
290 if (state
->avail
> 0) {
291 min
= minimum(request
, (off_t
)state
->avail
);
292 bytes_skipped
= archive_decompressor_none_read_consume(a
, min
);
293 request
-= bytes_skipped
;
294 total_bytes_skipped
+= bytes_skipped
;
296 if (state
->client_avail
> 0) {
297 min
= minimum(request
, (off_t
)state
->client_avail
);
298 bytes_skipped
= archive_decompressor_none_read_consume(a
, min
);
299 request
-= bytes_skipped
;
300 total_bytes_skipped
+= bytes_skipped
;
303 return (total_bytes_skipped
);
305 * If a client_skipper was provided, try that first.
307 #if ARCHIVE_API_VERSION < 2
308 if ((a
->client_skipper
!= NULL
) && (request
< SSIZE_MAX
)) {
310 if (a
->client_skipper
!= NULL
) {
312 bytes_skipped
= (a
->client_skipper
)(&a
->archive
,
313 a
->client_data
, request
);
314 if (bytes_skipped
< 0) { /* error */
315 state
->client_total
= state
->client_avail
= 0;
316 state
->client_next
= state
->client_buff
= NULL
;
318 return (bytes_skipped
);
320 total_bytes_skipped
+= bytes_skipped
;
321 a
->archive
.file_position
+= bytes_skipped
;
322 request
-= bytes_skipped
;
323 state
->client_next
= state
->client_buff
;
324 a
->archive
.raw_position
+= bytes_skipped
;
325 state
->client_avail
= state
->client_total
= 0;
328 * Note that client_skipper will usually not satisfy the
329 * full request (due to low-level blocking concerns),
330 * so even if client_skipper is provided, we may still
331 * have to use ordinary reads to finish out the request.
333 while (request
> 0) {
334 const void* dummy_buffer
;
336 bytes_read
= archive_decompressor_none_read_ahead(a
,
337 &dummy_buffer
, request
);
340 if (bytes_read
== 0) {
341 /* We hit EOF before we satisfied the skip request. */
342 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
343 "Truncated input file (need to skip %jd bytes)",
345 return (ARCHIVE_FATAL
);
347 min
= (size_t)(minimum(bytes_read
, request
));
348 bytes_read
= archive_decompressor_none_read_consume(a
, min
);
349 total_bytes_skipped
+= bytes_read
;
350 request
-= bytes_read
;
352 return (total_bytes_skipped
);
356 archive_decompressor_none_finish(struct archive_read
*a
)
358 struct archive_decompress_none
*state
;
360 state
= (struct archive_decompress_none
*)a
->decompressor
->data
;
363 a
->decompressor
->data
= NULL
;