e976398bb814393b5b90c6b01cb2fab7cfe85ac9
[dragonfly.git] / contrib / libarchive-2.1 / 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         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);
96 }
97
98 /*
99  * Try to detect an "uncompressed" archive.
100  */
101 static int
102 archive_decompressor_none_bid(const void *buff, size_t len)
103 {
104         (void)buff;
105         (void)len;
106
107         return (1); /* Default: We'll take it if noone else does. */
108 }
109
110 static int
111 archive_decompressor_none_init(struct archive_read *a, const void *buff, size_t n)
112 {
113         struct archive_decompress_none  *state;
114
115         a->archive.compression_code = ARCHIVE_COMPRESSION_NONE;
116         a->archive.compression_name = "none";
117
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);
122         }
123         memset(state, 0, sizeof(*state));
124
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);
132         }
133
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;
139
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;
145
146         return (ARCHIVE_OK);
147 }
148
149 /*
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.
153  */
154 static ssize_t
155 archive_decompressor_none_read_ahead(struct archive_read *a, const void **buff,
156     size_t min)
157 {
158         struct archive_decompress_none *state;
159         ssize_t bytes_read;
160
161         state = (struct archive_decompress_none *)a->decompressor->data;
162         if (state->fatal)
163                 return (-1);
164
165         /*
166          * Don't make special efforts to handle requests larger than
167          * the copy buffer.
168          */
169         if (min > state->buffer_size)
170                 min = state->buffer_size;
171
172         /*
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.
178          */
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;
183                 state->avail = 0;
184                 state->next = state->buffer;
185                 *buff = state->client_next;
186                 return (state->client_avail);
187         }
188
189         /*
190          * If we can't use client buffer, we'll have to use copy buffer.
191          */
192
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;
199         }
200
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,
212                             tocopy);
213                         state->client_next += tocopy;
214                         state->client_avail -= tocopy;
215                         state->avail += tocopy;
216                 } else {
217                         /* There is no more client data: fetch more. */
218                         /*
219                          * It seems to me that const void ** and const
220                          * char ** should be compatible, but they
221                          * aren't, hence the cast.
222                          */
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;
228                                 state->fatal = 1;
229                                 return (-1);
230                         }
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;
235                                 break;
236                         }
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;
241                 }
242         }
243
244         *buff = state->next;
245         return (state->avail);
246 }
247
248 /*
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
251  * request.
252  */
253 static ssize_t
254 archive_decompressor_none_read_consume(struct archive_read *a, size_t request)
255 {
256         struct archive_decompress_none *state;
257
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;
263         } else {
264                 /* Read came from client buffer. */
265                 state->client_next += request;
266                 state->client_avail -= request;
267         }
268         a->archive.file_position += request;
269         return (request);
270 }
271
272 /*
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.
276  */
277 static off_t
278 archive_decompressor_none_skip(struct archive_read *a, off_t request)
279 {
280         struct archive_decompress_none *state;
281         off_t bytes_skipped, total_bytes_skipped = 0;
282         size_t min;
283
284         state = (struct archive_decompress_none *)a->decompressor->data;
285         if (state->fatal)
286                 return (-1);
287         /*
288          * If there is data in the buffers already, use that first.
289          */
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;
295         }
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;
301         }
302         if (request == 0)
303                 return (total_bytes_skipped);
304         /*
305          * If a client_skipper was provided, try that first.
306          */
307 #if ARCHIVE_API_VERSION < 2
308         if ((a->client_skipper != NULL) && (request < SSIZE_MAX)) {
309 #else
310         if (a->client_skipper != NULL) {
311 #endif
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;
317                         state->fatal = 1;
318                         return (bytes_skipped);
319                 }
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;
326         }
327         /*
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.
332          */
333         while (request > 0) {
334                 const void* dummy_buffer;
335                 ssize_t bytes_read;
336                 bytes_read = archive_decompressor_none_read_ahead(a,
337                     &dummy_buffer, request);
338                 if (bytes_read < 0)
339                         return (bytes_read);
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)",
344                             (intmax_t)request);
345                         return (ARCHIVE_FATAL);
346                 }
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;
351         }
352         return (total_bytes_skipped);
353 }
354
355 static int
356 archive_decompressor_none_finish(struct archive_read *a)
357 {
358         struct archive_decompress_none  *state;
359
360         state = (struct archive_decompress_none *)a->decompressor->data;
361         free(state->buffer);
362         free(state);
363         a->decompressor->data = NULL;
364         return (ARCHIVE_OK);
365 }