Import libarchive and bsdtar version 2.0.25
[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.15 2007/03/03 07:37:36 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 char      *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 = (const char *)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,
223                             (const void **)&state->client_buff);
224                         if (bytes_read < 0) {           /* Read error. */
225                                 state->client_total = state->client_avail = 0;
226                                 state->client_next = state->client_buff = NULL;
227                                 state->fatal = 1;
228                                 return (-1);
229                         }
230                         if (bytes_read == 0) {          /* End-of-file. */
231                                 state->client_total = state->client_avail = 0;
232                                 state->client_next = state->client_buff = NULL;
233                                 state->end_of_file = 1;
234                                 break;
235                         }
236                         a->archive.raw_position += bytes_read;
237                         state->client_total = bytes_read;
238                         state->client_avail = state->client_total;
239                         state->client_next = state->client_buff;
240                 }
241         }
242
243         *buff = state->next;
244         return (state->avail);
245 }
246
247 /*
248  * Mark the appropriate data as used.  Note that the request here will
249  * often be much smaller than the size of the previous read_ahead
250  * request.
251  */
252 static ssize_t
253 archive_decompressor_none_read_consume(struct archive_read *a, size_t request)
254 {
255         struct archive_decompress_none *state;
256
257         state = (struct archive_decompress_none *)a->compression_data;
258         if (state->avail > 0) {
259                 /* Read came from copy buffer. */
260                 state->next += request;
261                 state->avail -= request;
262         } else {
263                 /* Read came from client buffer. */
264                 state->client_next += request;
265                 state->client_avail -= request;
266         }
267         a->archive.file_position += request;
268         return (request);
269 }
270
271 /*
272  * Skip forward by exactly the requested bytes or else return
273  * ARCHIVE_FATAL.  Note that this differs from the contract for
274  * read_ahead, which does not guarantee a minimum count.
275  */
276 static off_t
277 archive_decompressor_none_skip(struct archive_read *a, off_t request)
278 {
279         struct archive_decompress_none *state;
280         off_t bytes_skipped, total_bytes_skipped = 0;
281         size_t min;
282
283         state = (struct archive_decompress_none *)a->compression_data;
284         if (state->fatal)
285                 return (-1);
286         /*
287          * If there is data in the buffers already, use that first.
288          */
289         if (state->avail > 0) {
290                 min = minimum(request, (off_t)state->avail);
291                 bytes_skipped = archive_decompressor_none_read_consume(a, min);
292                 request -= bytes_skipped;
293                 total_bytes_skipped += bytes_skipped;
294         }
295         if (state->client_avail > 0) {
296                 min = minimum(request, (off_t)state->client_avail);
297                 bytes_skipped = archive_decompressor_none_read_consume(a, min);
298                 request -= bytes_skipped;
299                 total_bytes_skipped += bytes_skipped;
300         }
301         if (request == 0)
302                 return (total_bytes_skipped);
303         /*
304          * If a client_skipper was provided, try that first.
305          */
306 #if ARCHIVE_API_VERSION < 2
307         if ((a->client_skipper != NULL) && (request < SSIZE_MAX)) {
308 #else
309         if (a->client_skipper != NULL) {
310 #endif
311                 bytes_skipped = (a->client_skipper)(&a->archive,
312                     a->client_data, request);
313                 if (bytes_skipped < 0) {        /* error */
314                         state->client_total = state->client_avail = 0;
315                         state->client_next = state->client_buff = NULL;
316                         state->fatal = 1;
317                         return (bytes_skipped);
318                 }
319                 total_bytes_skipped += bytes_skipped;
320                 a->archive.file_position += bytes_skipped;
321                 request -= bytes_skipped;
322                 state->client_next = state->client_buff;
323                 a->archive.raw_position += bytes_skipped;
324                 state->client_avail = state->client_total = 0;
325         }
326         /*
327          * Note that client_skipper will usually not satisfy the
328          * full request (due to low-level blocking concerns),
329          * so even if client_skipper is provided, we may still
330          * have to use ordinary reads to finish out the request.
331          */
332         while (request > 0) {
333                 const void* dummy_buffer;
334                 ssize_t bytes_read;
335                 bytes_read = archive_decompressor_none_read_ahead(a,
336                     &dummy_buffer, request);
337                 if (bytes_read < 0)
338                         return (bytes_read);
339                 if (bytes_read == 0) {
340                         /* We hit EOF before we satisfied the skip request. */
341                         archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
342                             "Truncated input file (need to skip %jd bytes)",
343                             (intmax_t)request);
344                         return (ARCHIVE_FATAL);
345                 }
346                 min = (size_t)(minimum(bytes_read, request));
347                 bytes_read = archive_decompressor_none_read_consume(a, min);
348                 total_bytes_skipped += bytes_read;
349                 request -= bytes_read;
350         }
351         return (total_bytes_skipped);
352 }
353
354 static int
355 archive_decompressor_none_finish(struct archive_read *a)
356 {
357         struct archive_decompress_none  *state;
358
359         state = (struct archive_decompress_none *)a->compression_data;
360         free(state->buffer);
361         free(state);
362         a->compression_data = NULL;
363         if (a->client_closer != NULL)
364                 return ((a->client_closer)(&a->archive, a->client_data));
365         return (ARCHIVE_OK);
366 }