6a3a245d52a0459f3be5b0877b0a8bc60ad8b5d1
[dragonfly.git] / contrib / libarchive-2.0 / libarchive / archive_read_support_compression_none.c
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
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.
13  *
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.
24  */
25
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 $");
28
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
44
45 #include "archive.h"
46 #include "archive_private.h"
47 #include "archive_read_private.h"
48
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;
60 };
61
62 /*
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.
72  */
73 #define BUFFER_SIZE     65536
74
75 #define minimum(a, b) (a < b ? a : b)
76
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);
86
87 int
88 archive_read_support_compression_none(struct archive *_a)
89 {
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));
94 }
95
96 /*
97  * Try to detect an "uncompressed" archive.
98  */
99 static int
100 archive_decompressor_none_bid(const void *buff, size_t len)
101 {
102         (void)buff;
103         (void)len;
104
105         return (1); /* Default: We'll take it if noone else does. */
106 }
107
108 static int
109 archive_decompressor_none_init(struct archive_read *a, const void *buff, size_t n)
110 {
111         struct archive_decompress_none  *state;
112
113         a->archive.compression_code = ARCHIVE_COMPRESSION_NONE;
114         a->archive.compression_name = "none";
115
116         state = (struct archive_decompress_none *)malloc(sizeof(*state));
117         if (!state) {
118                 archive_set_error(&a->archive, ENOMEM, "Can't allocate input data");
119                 return (ARCHIVE_FATAL);
120         }
121         memset(state, 0, sizeof(*state));
122
123         state->buffer_size = BUFFER_SIZE;
124         state->buffer = (char *)malloc(state->buffer_size);
125         state->next = state->buffer;
126         if (state->buffer == NULL) {
127                 free(state);
128                 archive_set_error(&a->archive, ENOMEM, "Can't allocate input buffer");
129                 return (ARCHIVE_FATAL);
130         }
131
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;
137
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;
143
144         return (ARCHIVE_OK);
145 }
146
147 /*
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.
151  */
152 static ssize_t
153 archive_decompressor_none_read_ahead(struct archive_read *a, const void **buff,
154     size_t min)
155 {
156         struct archive_decompress_none *state;
157         ssize_t bytes_read;
158
159         state = (struct archive_decompress_none *)a->compression_data;
160         if (state->fatal)
161                 return (-1);
162
163         /*
164          * Don't make special efforts to handle requests larger than
165          * the copy buffer.
166          */
167         if (min > state->buffer_size)
168                 min = state->buffer_size;
169
170         /*
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.
176          */
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;
181                 state->avail = 0;
182                 state->next = state->buffer;
183                 *buff = state->client_next;
184                 return (state->client_avail);
185         }
186
187         /*
188          * If we can't use client buffer, we'll have to use copy buffer.
189          */
190
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;
197         }
198
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,
210                             tocopy);
211                         state->client_next += tocopy;
212                         state->client_avail -= tocopy;
213                         state->avail += tocopy;
214                 } else {
215                         /* There is no more client data: fetch more. */
216                         /*
217                          * It seems to me that const void ** and const
218                          * char ** should be compatible, but they
219                          * aren't, hence the cast.
220                          */
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;
226                                 state->fatal = 1;
227                                 return (-1);
228                         }
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;
233                                 break;
234                         }
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;
239                 }
240         }
241
242         *buff = state->next;
243         return (state->avail);
244 }
245
246 /*
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
249  * request.
250  */
251 static ssize_t
252 archive_decompressor_none_read_consume(struct archive_read *a, size_t request)
253 {
254         struct archive_decompress_none *state;
255
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;
261         } else {
262                 /* Read came from client buffer. */
263                 state->client_next += request;
264                 state->client_avail -= request;
265         }
266         a->archive.file_position += request;
267         return (request);
268 }
269
270 /*
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.
274  */
275 static off_t
276 archive_decompressor_none_skip(struct archive_read *a, off_t request)
277 {
278         struct archive_decompress_none *state;
279         off_t bytes_skipped, total_bytes_skipped = 0;
280         size_t min;
281
282         state = (struct archive_decompress_none *)a->compression_data;
283         if (state->fatal)
284                 return (-1);
285         /*
286          * If there is data in the buffers already, use that first.
287          */
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;
293         }
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;
299         }
300         if (request == 0)
301                 return (total_bytes_skipped);
302         /*
303          * If a client_skipper was provided, try that first.
304          */
305 #if ARCHIVE_API_VERSION < 2
306         if ((a->client_skipper != NULL) && (request < SSIZE_MAX)) {
307 #else
308         if (a->client_skipper != NULL) {
309 #endif
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;
315                         state->fatal = 1;
316                         return (bytes_skipped);
317                 }
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;
324         }
325         /*
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.
330          */
331         while (request > 0) {
332                 const void* dummy_buffer;
333                 ssize_t bytes_read;
334                 bytes_read = archive_decompressor_none_read_ahead(a,
335                     &dummy_buffer, request);
336                 if (bytes_read < 0)
337                         return (bytes_read);
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)",
342                             (intmax_t)request);
343                         return (ARCHIVE_FATAL);
344                 }
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;
349         }
350         return (total_bytes_skipped);
351 }
352
353 static int
354 archive_decompressor_none_finish(struct archive_read *a)
355 {
356         struct archive_decompress_none  *state;
357
358         state = (struct archive_decompress_none *)a->compression_data;
359         free(state->buffer);
360         free(state);
361         a->compression_data = NULL;
362         if (a->client_closer != NULL)
363                 return ((a->client_closer)(&a->archive, a->client_data));
364         return (ARCHIVE_OK);
365 }