Import libarchive-2.4.17. See NEWS for details.
[dragonfly.git] / contrib / libarchive-2 / libarchive / archive_read_support_format_ar.c
bloba74791c6dae0bd26cc34165d043d4d1c4b0aa7b5
1 /*-
2 * Copyright (c) 2007 Kai Wang
3 * Copyright (c) 2007 Tim Kientzle
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer
11 * in this position and unchanged.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "archive_platform.h"
29 __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_support_format_ar.c,v 1.8 2008/02/19 05:54:24 kientzle Exp $");
31 #ifdef HAVE_SYS_STAT_H
32 #include <sys/stat.h>
33 #endif
34 #ifdef HAVE_ERRNO_H
35 #include <errno.h>
36 #endif
37 #ifdef HAVE_STDLIB_H
38 #include <stdlib.h>
39 #endif
40 #ifdef HAVE_STRING_H
41 #include <string.h>
42 #endif
44 #include "archive.h"
45 #include "archive_entry.h"
46 #include "archive_private.h"
47 #include "archive_read_private.h"
49 struct ar {
50 off_t entry_bytes_remaining;
51 off_t entry_offset;
52 off_t entry_padding;
53 char *strtab;
54 size_t strtab_size;
58 * Define structure of the "ar" header.
60 #define AR_name_offset 0
61 #define AR_name_size 16
62 #define AR_date_offset 16
63 #define AR_date_size 12
64 #define AR_uid_offset 28
65 #define AR_uid_size 6
66 #define AR_gid_offset 34
67 #define AR_gid_size 6
68 #define AR_mode_offset 40
69 #define AR_mode_size 8
70 #define AR_size_offset 48
71 #define AR_size_size 10
72 #define AR_fmag_offset 58
73 #define AR_fmag_size 2
75 #define isdigit(x) (x) >= '0' && (x) <= '9'
77 static int archive_read_format_ar_bid(struct archive_read *a);
78 static int archive_read_format_ar_cleanup(struct archive_read *a);
79 static int archive_read_format_ar_read_data(struct archive_read *a,
80 const void **buff, size_t *size, off_t *offset);
81 static int archive_read_format_ar_skip(struct archive_read *a);
82 static int archive_read_format_ar_read_header(struct archive_read *a,
83 struct archive_entry *e);
84 static uint64_t ar_atol8(const char *p, unsigned char_cnt);
85 static uint64_t ar_atol10(const char *p, unsigned char_cnt);
86 static int ar_parse_gnu_filename_table(struct archive_read *, struct ar *,
87 const void *, size_t);
88 static int ar_parse_common_header(struct ar *ar, struct archive_entry *,
89 const char *h);
91 int
92 archive_read_support_format_ar(struct archive *_a)
94 struct archive_read *a = (struct archive_read *)_a;
95 struct ar *ar;
96 int r;
98 ar = (struct ar *)malloc(sizeof(*ar));
99 if (ar == NULL) {
100 archive_set_error(&a->archive, ENOMEM,
101 "Can't allocate ar data");
102 return (ARCHIVE_FATAL);
104 memset(ar, 0, sizeof(*ar));
105 ar->strtab = NULL;
107 r = __archive_read_register_format(a,
109 archive_read_format_ar_bid,
110 archive_read_format_ar_read_header,
111 archive_read_format_ar_read_data,
112 archive_read_format_ar_skip,
113 archive_read_format_ar_cleanup);
115 if (r != ARCHIVE_OK) {
116 free(ar);
117 return (r);
119 return (ARCHIVE_OK);
122 static int
123 archive_read_format_ar_cleanup(struct archive_read *a)
125 struct ar *ar;
127 ar = (struct ar *)(a->format->data);
128 if (ar->strtab)
129 free(ar->strtab);
130 free(ar);
131 (a->format->data) = NULL;
132 return (ARCHIVE_OK);
135 static int
136 archive_read_format_ar_bid(struct archive_read *a)
138 struct ar *ar;
139 ssize_t bytes_read;
140 const void *h;
142 if (a->archive.archive_format != 0 &&
143 (a->archive.archive_format & ARCHIVE_FORMAT_BASE_MASK) !=
144 ARCHIVE_FORMAT_AR)
145 return(0);
147 ar = (struct ar *)(a->format->data);
150 * Verify the 8-byte file signature.
151 * TODO: Do we need to check more than this?
153 bytes_read = (a->decompressor->read_ahead)(a, &h, 8);
154 if (bytes_read < 8)
155 return (-1);
156 if (strncmp((const char*)h, "!<arch>\n", 8) == 0) {
157 return (64);
159 return (-1);
162 static int
163 archive_read_format_ar_read_header(struct archive_read *a,
164 struct archive_entry *entry)
166 char filename[AR_name_size + 1];
167 struct ar *ar;
168 uint64_t number; /* Used to hold parsed numbers before validation. */
169 ssize_t bytes_read;
170 size_t bsd_name_length, entry_size;
171 char *p;
172 const void *b;
173 const char *h;
174 int r;
176 ar = (struct ar*)(a->format->data);
178 if (a->archive.file_position == 0) {
180 * We are now at the beginning of the archive,
181 * so we need first consume the ar global header.
183 (a->decompressor->consume)(a, 8);
184 /* Set a default format code for now. */
185 a->archive.archive_format = ARCHIVE_FORMAT_AR;
188 /* Read the header for the next file entry. */
189 bytes_read = (a->decompressor->read_ahead)(a, &b, 60);
190 if (bytes_read < 60) {
191 /* Broken header. */
192 return (ARCHIVE_EOF);
194 (a->decompressor->consume)(a, 60);
195 h = (const char *)b;
197 /* Verify the magic signature on the file header. */
198 if (strncmp(h + AR_fmag_offset, "`\n", 2) != 0) {
199 archive_set_error(&a->archive, EINVAL,
200 "Consistency check failed");
201 return (ARCHIVE_WARN);
204 /* Copy filename into work buffer. */
205 strncpy(filename, h + AR_name_offset, AR_name_size);
206 filename[AR_name_size] = '\0';
209 * Guess the format variant based on the filename.
211 if (a->archive.archive_format == ARCHIVE_FORMAT_AR) {
212 /* We don't already know the variant, so let's guess. */
214 * Biggest clue is presence of '/': GNU starts special
215 * filenames with '/', appends '/' as terminator to
216 * non-special names, so anything with '/' should be
217 * GNU except for BSD long filenames.
219 if (strncmp(filename, "#1/", 3) == 0)
220 a->archive.archive_format = ARCHIVE_FORMAT_AR_BSD;
221 else if (strchr(filename, '/') != NULL)
222 a->archive.archive_format = ARCHIVE_FORMAT_AR_GNU;
223 else if (strncmp(filename, "__.SYMDEF", 9) == 0)
224 a->archive.archive_format = ARCHIVE_FORMAT_AR_BSD;
226 * XXX Do GNU/SVR4 'ar' programs ever omit trailing '/'
227 * if name exactly fills 16-byte field? If so, we
228 * can't assume entries without '/' are BSD. XXX
232 /* Update format name from the code. */
233 if (a->archive.archive_format == ARCHIVE_FORMAT_AR_GNU)
234 a->archive.archive_format_name = "ar (GNU/SVR4)";
235 else if (a->archive.archive_format == ARCHIVE_FORMAT_AR_BSD)
236 a->archive.archive_format_name = "ar (BSD)";
237 else
238 a->archive.archive_format_name = "ar";
241 * Remove trailing spaces from the filename. GNU and BSD
242 * variants both pad filename area out with spaces.
243 * This will only be wrong if GNU/SVR4 'ar' implementations
244 * omit trailing '/' for 16-char filenames and we have
245 * a 16-char filename that ends in ' '.
247 p = filename + AR_name_size - 1;
248 while (p >= filename && *p == ' ') {
249 *p = '\0';
250 p--;
254 * Remove trailing slash unless first character is '/'.
255 * (BSD entries never end in '/', so this will only trim
256 * GNU-format entries. GNU special entries start with '/'
257 * and are not terminated in '/', so we don't trim anything
258 * that starts with '/'.)
260 if (filename[0] != '/' && *p == '/')
261 *p = '\0';
264 * '//' is the GNU filename table.
265 * Later entries can refer to names in this table.
267 if (strcmp(filename, "//") == 0) {
268 /* This must come before any call to _read_ahead. */
269 ar_parse_common_header(ar, entry, h);
270 archive_entry_copy_pathname(entry, filename);
271 archive_entry_set_filetype(entry, AE_IFREG);
272 /* Get the size of the filename table. */
273 number = ar_atol10(h + AR_size_offset, AR_size_size);
274 if (number > SIZE_MAX) {
275 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
276 "Filename table too large");
277 return (ARCHIVE_FATAL);
279 entry_size = (size_t)number;
280 /* Read the filename table into memory. */
281 bytes_read = (a->decompressor->read_ahead)(a, &b, entry_size);
282 if (bytes_read <= 0)
283 return (ARCHIVE_FATAL);
284 if ((size_t)bytes_read < entry_size) {
285 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
286 "Truncated input file");
287 return (ARCHIVE_FATAL);
290 * Don't consume the contents, so the client will
291 * also get a shot at reading it.
294 /* Parse the filename table. */
295 return (ar_parse_gnu_filename_table(a, ar, b, entry_size));
299 * GNU variant handles long filenames by storing /<number>
300 * to indicate a name stored in the filename table.
302 if (filename[0] == '/' && isdigit(filename[1])) {
303 number = ar_atol10(h + AR_name_offset + 1, AR_name_size - 1);
305 * If we can't look up the real name, warn and return
306 * the entry with the wrong name.
308 if (ar->strtab == NULL || number > ar->strtab_size) {
309 archive_set_error(&a->archive, EINVAL,
310 "Can't find long filename for entry");
311 archive_entry_copy_pathname(entry, filename);
312 /* Parse the time, owner, mode, size fields. */
313 ar_parse_common_header(ar, entry, h);
314 return (ARCHIVE_WARN);
317 archive_entry_copy_pathname(entry, &ar->strtab[(size_t)number]);
318 /* Parse the time, owner, mode, size fields. */
319 return (ar_parse_common_header(ar, entry, h));
323 * BSD handles long filenames by storing "#1/" followed by the
324 * length of filename as a decimal number, then prepends the
325 * the filename to the file contents.
327 if (strncmp(filename, "#1/", 3) == 0) {
328 /* Parse the time, owner, mode, size fields. */
329 /* This must occur before _read_ahead is called again. */
330 ar_parse_common_header(ar, entry, h);
332 /* Parse the size of the name, adjust the file size. */
333 number = ar_atol10(h + AR_name_offset + 3, AR_name_size - 3);
334 if ((off_t)number > ar->entry_bytes_remaining) {
335 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
336 "Bad input file size");
337 return (ARCHIVE_FATAL);
339 bsd_name_length = (size_t)number;
340 ar->entry_bytes_remaining -= bsd_name_length;
341 /* Adjust file size reported to client. */
342 archive_entry_set_size(entry, ar->entry_bytes_remaining);
344 /* Read the long name into memory. */
345 bytes_read = (a->decompressor->read_ahead)(a, &b, bsd_name_length);
346 if (bytes_read <= 0)
347 return (ARCHIVE_FATAL);
348 if ((size_t)bytes_read < bsd_name_length) {
349 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
350 "Truncated input file");
351 return (ARCHIVE_FATAL);
353 (a->decompressor->consume)(a, bsd_name_length);
355 /* Store it in the entry. */
356 p = (char *)malloc(bsd_name_length + 1);
357 if (p == NULL) {
358 archive_set_error(&a->archive, ENOMEM,
359 "Can't allocate fname buffer");
360 return (ARCHIVE_FATAL);
362 strncpy(p, b, bsd_name_length);
363 p[bsd_name_length] = '\0';
364 archive_entry_copy_pathname(entry, p);
365 free(p);
366 return (ARCHIVE_OK);
370 * "/" is the SVR4/GNU archive symbol table.
372 if (strcmp(filename, "/") == 0) {
373 archive_entry_copy_pathname(entry, "/");
374 /* Parse the time, owner, mode, size fields. */
375 r = ar_parse_common_header(ar, entry, h);
376 /* Force the file type to a regular file. */
377 archive_entry_set_filetype(entry, AE_IFREG);
378 return (r);
382 * "__.SYMDEF" is a BSD archive symbol table.
384 if (strcmp(filename, "__.SYMDEF") == 0) {
385 archive_entry_copy_pathname(entry, filename);
386 /* Parse the time, owner, mode, size fields. */
387 return (ar_parse_common_header(ar, entry, h));
391 * Otherwise, this is a standard entry. The filename
392 * has already been trimmed as much as possible, based
393 * on our current knowledge of the format.
395 archive_entry_copy_pathname(entry, filename);
396 return (ar_parse_common_header(ar, entry, h));
399 static int
400 ar_parse_common_header(struct ar *ar, struct archive_entry *entry,
401 const char *h)
403 uint64_t n;
405 /* Copy remaining header */
406 archive_entry_set_mtime(entry,
407 (time_t)ar_atol10(h + AR_date_offset, AR_date_size), 0L);
408 archive_entry_set_uid(entry,
409 (uid_t)ar_atol10(h + AR_uid_offset, AR_uid_size));
410 archive_entry_set_gid(entry,
411 (gid_t)ar_atol10(h + AR_gid_offset, AR_gid_size));
412 archive_entry_set_mode(entry,
413 (mode_t)ar_atol8(h + AR_mode_offset, AR_mode_size));
414 n = ar_atol10(h + AR_size_offset, AR_size_size);
416 ar->entry_offset = 0;
417 ar->entry_padding = n % 2;
418 archive_entry_set_size(entry, n);
419 ar->entry_bytes_remaining = n;
420 return (ARCHIVE_OK);
423 static int
424 archive_read_format_ar_read_data(struct archive_read *a,
425 const void **buff, size_t *size, off_t *offset)
427 ssize_t bytes_read;
428 struct ar *ar;
430 ar = (struct ar *)(a->format->data);
432 if (ar->entry_bytes_remaining > 0) {
433 bytes_read = (a->decompressor->read_ahead)(a, buff, 1);
434 if (bytes_read == 0) {
435 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
436 "Truncated ar archive");
437 return (ARCHIVE_FATAL);
439 if (bytes_read < 0)
440 return (ARCHIVE_FATAL);
441 if (bytes_read > ar->entry_bytes_remaining)
442 bytes_read = (ssize_t)ar->entry_bytes_remaining;
443 *size = bytes_read;
444 *offset = ar->entry_offset;
445 ar->entry_offset += bytes_read;
446 ar->entry_bytes_remaining -= bytes_read;
447 (a->decompressor->consume)(a, (size_t)bytes_read);
448 return (ARCHIVE_OK);
449 } else {
450 while (ar->entry_padding > 0) {
451 bytes_read = (a->decompressor->read_ahead)(a, buff, 1);
452 if (bytes_read <= 0)
453 return (ARCHIVE_FATAL);
454 if (bytes_read > ar->entry_padding)
455 bytes_read = (ssize_t)ar->entry_padding;
456 (a->decompressor->consume)(a, (size_t)bytes_read);
457 ar->entry_padding -= bytes_read;
459 *buff = NULL;
460 *size = 0;
461 *offset = ar->entry_offset;
462 return (ARCHIVE_EOF);
466 static int
467 archive_read_format_ar_skip(struct archive_read *a)
469 off_t bytes_skipped;
470 struct ar* ar;
471 int r = ARCHIVE_OK;
472 const void *b; /* Dummy variables */
473 size_t s;
474 off_t o;
476 ar = (struct ar *)(a->format->data);
477 if (a->decompressor->skip == NULL) {
478 while (r == ARCHIVE_OK)
479 r = archive_read_format_ar_read_data(a, &b, &s, &o);
480 return (r);
483 bytes_skipped = (a->decompressor->skip)(a, ar->entry_bytes_remaining +
484 ar->entry_padding);
485 if (bytes_skipped < 0)
486 return (ARCHIVE_FATAL);
488 ar->entry_bytes_remaining = 0;
489 ar->entry_padding = 0;
491 return (ARCHIVE_OK);
494 static int
495 ar_parse_gnu_filename_table(struct archive_read *a, struct ar *ar,
496 const void *h, size_t size)
498 char *p;
500 if (ar->strtab != NULL) {
501 archive_set_error(&a->archive, EINVAL,
502 "More than one string tables exist");
503 return (ARCHIVE_WARN);
506 if (size == 0) {
507 archive_set_error(&a->archive, EINVAL, "Invalid string table");
508 return (ARCHIVE_WARN);
511 ar->strtab_size = size;
512 ar->strtab = malloc(size);
513 if (ar->strtab == NULL) {
514 archive_set_error(&a->archive, ENOMEM,
515 "Can't allocate string table buffer");
516 return (ARCHIVE_FATAL);
519 (void)memcpy(ar->strtab, h, size);
520 for (p = ar->strtab; p < ar->strtab + size - 1; ++p) {
521 if (*p == '/') {
522 *p++ = '\0';
523 if (*p != '\n')
524 goto bad_string_table;
525 *p = '\0';
529 * Sanity check, last two chars must be `/\n' or '\n\n',
530 * depending on whether the string table is padded by a '\n'
531 * (string table produced by GNU ar always has a even size).
533 if (p != ar->strtab + size && *p != '\n')
534 goto bad_string_table;
536 /* Enforce zero termination. */
537 ar->strtab[size - 1] = '\0';
539 return (ARCHIVE_OK);
541 bad_string_table:
542 archive_set_error(&a->archive, EINVAL,
543 "Invalid string table");
544 free(ar->strtab);
545 ar->strtab = NULL;
546 return (ARCHIVE_WARN);
549 static uint64_t
550 ar_atol8(const char *p, unsigned char_cnt)
552 uint64_t l, limit, last_digit_limit;
553 unsigned int digit, base;
555 base = 8;
556 limit = UINT64_MAX / base;
557 last_digit_limit = UINT64_MAX % base;
559 while ((*p == ' ' || *p == '\t') && char_cnt-- > 0)
560 p++;
562 l = 0;
563 digit = *p - '0';
564 while (*p >= '0' && digit < base && char_cnt-- > 0) {
565 if (l>limit || (l == limit && digit > last_digit_limit)) {
566 l = UINT64_MAX; /* Truncate on overflow. */
567 break;
569 l = (l * base) + digit;
570 digit = *++p - '0';
572 return (l);
575 static uint64_t
576 ar_atol10(const char *p, unsigned char_cnt)
578 uint64_t l, limit, last_digit_limit;
579 unsigned int base, digit;
581 base = 10;
582 limit = UINT64_MAX / base;
583 last_digit_limit = UINT64_MAX % base;
585 while ((*p == ' ' || *p == '\t') && char_cnt-- > 0)
586 p++;
587 l = 0;
588 digit = *p - '0';
589 while (*p >= '0' && digit < base && char_cnt-- > 0) {
590 if (l > limit || (l == limit && digit > last_digit_limit)) {
591 l = UINT64_MAX; /* Truncate on overflow. */
592 break;
594 l = (l * base) + digit;
595 digit = *++p - '0';
597 return (l);