Add our READMEs.
[dragonfly/vkernel-mp.git] / contrib / libarchive-2.1 / libarchive / archive_read_support_format_ar.c
blob4ffc1cd80ab319f32aa039dafd9eff68422a6872
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.5 2007/04/15 00:53:38 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 int bid;
51 off_t entry_bytes_remaining;
52 off_t entry_offset;
53 off_t entry_padding;
54 char *strtab;
55 size_t strtab_size;
59 * Define structure of the "ar" header.
61 #define AR_name_offset 0
62 #define AR_name_size 16
63 #define AR_date_offset 16
64 #define AR_date_size 12
65 #define AR_uid_offset 28
66 #define AR_uid_size 6
67 #define AR_gid_offset 34
68 #define AR_gid_size 6
69 #define AR_mode_offset 40
70 #define AR_mode_size 8
71 #define AR_size_offset 48
72 #define AR_size_size 10
73 #define AR_fmag_offset 58
74 #define AR_fmag_size 2
76 #define isdigit(x) (x) >= '0' && (x) <= '9'
78 static int archive_read_format_ar_bid(struct archive_read *a);
79 static int archive_read_format_ar_cleanup(struct archive_read *a);
80 static int archive_read_format_ar_read_data(struct archive_read *a,
81 const void **buff, size_t *size, off_t *offset);
82 static int archive_read_format_ar_skip(struct archive_read *a);
83 static int archive_read_format_ar_read_header(struct archive_read *a,
84 struct archive_entry *e);
85 static uint64_t ar_atol8(const char *p, unsigned char_cnt);
86 static uint64_t ar_atol10(const char *p, unsigned char_cnt);
87 static int ar_parse_gnu_filename_table(struct archive_read *, struct ar *,
88 const void *, size_t);
89 static int ar_parse_common_header(struct ar *ar, struct archive_entry *,
90 const char *h);
92 int
93 archive_read_support_format_ar(struct archive *_a)
95 struct archive_read *a = (struct archive_read *)_a;
96 struct ar *ar;
97 int r;
99 ar = (struct ar *)malloc(sizeof(*ar));
100 if (ar == NULL) {
101 archive_set_error(&a->archive, ENOMEM,
102 "Can't allocate ar data");
103 return (ARCHIVE_FATAL);
105 memset(ar, 0, sizeof(*ar));
106 ar->bid = -1;
107 ar->strtab = NULL;
109 r = __archive_read_register_format(a,
111 archive_read_format_ar_bid,
112 archive_read_format_ar_read_header,
113 archive_read_format_ar_read_data,
114 archive_read_format_ar_skip,
115 archive_read_format_ar_cleanup);
117 if (r != ARCHIVE_OK) {
118 free(ar);
119 return (r);
121 return (ARCHIVE_OK);
124 static int
125 archive_read_format_ar_cleanup(struct archive_read *a)
127 struct ar *ar;
129 ar = (struct ar *)(a->format->data);
130 if (ar->strtab)
131 free(ar->strtab);
132 free(ar);
133 (a->format->data) = NULL;
134 return (ARCHIVE_OK);
137 static int
138 archive_read_format_ar_bid(struct archive_read *a)
140 struct ar *ar;
141 ssize_t bytes_read;
142 const void *h;
144 if (a->archive.archive_format != 0 &&
145 (a->archive.archive_format & ARCHIVE_FORMAT_BASE_MASK) !=
146 ARCHIVE_FORMAT_AR)
147 return(0);
149 ar = (struct ar *)(a->format->data);
151 if (ar->bid > 0)
152 return (ar->bid);
155 * Verify the 8-byte file signature.
156 * TODO: Do we need to check more than this?
158 bytes_read = (a->decompressor->read_ahead)(a, &h, 8);
159 if (bytes_read < 8)
160 return (-1);
161 if (strncmp((const char*)h, "!<arch>\n", 8) == 0) {
162 ar->bid = 64;
163 return (ar->bid);
165 return (-1);
168 static int
169 archive_read_format_ar_read_header(struct archive_read *a,
170 struct archive_entry *entry)
172 char filename[AR_name_size + 1];
173 struct ar *ar;
174 uint64_t number; /* Used to hold parsed numbers before validation. */
175 ssize_t bytes_read;
176 size_t bsd_name_length, entry_size;
177 char *p;
178 const void *b;
179 const char *h;
180 int r;
182 ar = (struct ar*)(a->format->data);
184 if (a->archive.file_position == 0) {
186 * We are now at the beginning of the archive,
187 * so we need first consume the ar global header.
189 (a->decompressor->consume)(a, 8);
190 /* Set a default format code for now. */
191 a->archive.archive_format = ARCHIVE_FORMAT_AR;
194 /* Read the header for the next file entry. */
195 bytes_read = (a->decompressor->read_ahead)(a, &b, 60);
196 if (bytes_read < 60) {
197 /* Broken header. */
198 return (ARCHIVE_EOF);
200 (a->decompressor->consume)(a, 60);
201 h = (const char *)b;
203 /* Verify the magic signature on the file header. */
204 if (strncmp(h + AR_fmag_offset, "`\n", 2) != 0) {
205 archive_set_error(&a->archive, EINVAL,
206 "Consistency check failed");
207 return (ARCHIVE_WARN);
210 /* Copy filename into work buffer. */
211 strncpy(filename, h + AR_name_offset, AR_name_size);
212 filename[AR_name_size] = '\0';
215 * Guess the format variant based on the filename.
217 if (a->archive.archive_format == ARCHIVE_FORMAT_AR) {
218 /* We don't already know the variant, so let's guess. */
220 * Biggest clue is presence of '/': GNU starts special
221 * filenames with '/', appends '/' as terminator to
222 * non-special names, so anything with '/' should be
223 * GNU except for BSD long filenames.
225 if (strncmp(filename, "#1/", 3) == 0)
226 a->archive.archive_format = ARCHIVE_FORMAT_AR_BSD;
227 else if (strchr(filename, '/') != NULL)
228 a->archive.archive_format = ARCHIVE_FORMAT_AR_GNU;
229 else if (strncmp(filename, "__.SYMDEF", 9) == 0)
230 a->archive.archive_format = ARCHIVE_FORMAT_AR_BSD;
232 * XXX Do GNU/SVR4 'ar' programs ever omit trailing '/'
233 * if name exactly fills 16-byte field? If so, we
234 * can't assume entries without '/' are BSD. XXX
238 /* Update format name from the code. */
239 if (a->archive.archive_format == ARCHIVE_FORMAT_AR_GNU)
240 a->archive.archive_format_name = "ar (GNU/SVR4)";
241 else if (a->archive.archive_format == ARCHIVE_FORMAT_AR_BSD)
242 a->archive.archive_format_name = "ar (BSD)";
243 else
244 a->archive.archive_format_name = "ar";
247 * Remove trailing spaces from the filename. GNU and BSD
248 * variants both pad filename area out with spaces.
249 * This will only be wrong if GNU/SVR4 'ar' implementations
250 * omit trailing '/' for 16-char filenames and we have
251 * a 16-char filename that ends in ' '.
253 p = filename + AR_name_size - 1;
254 while (p >= filename && *p == ' ') {
255 *p = '\0';
256 p--;
260 * Remove trailing slash unless first character is '/'.
261 * (BSD entries never end in '/', so this will only trim
262 * GNU-format entries. GNU special entries start with '/'
263 * and are not terminated in '/', so we don't trim anything
264 * that starts with '/'.)
266 if (filename[0] != '/' && *p == '/')
267 *p = '\0';
270 * '//' is the GNU filename table.
271 * Later entries can refer to names in this table.
273 if (strcmp(filename, "//") == 0) {
274 /* This must come before any call to _read_ahead. */
275 ar_parse_common_header(ar, entry, h);
276 archive_entry_copy_pathname(entry, filename);
277 archive_entry_set_mode(entry,
278 S_IFREG | (archive_entry_mode(entry) & 0777));
279 /* Get the size of the filename table. */
280 number = ar_atol10(h + AR_size_offset, AR_size_size);
281 if (number > SIZE_MAX) {
282 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
283 "Filename table too large");
284 return (ARCHIVE_FATAL);
286 entry_size = (size_t)number;
287 /* Read the filename table into memory. */
288 bytes_read = (a->decompressor->read_ahead)(a, &b, entry_size);
289 if (bytes_read <= 0)
290 return (ARCHIVE_FATAL);
291 if ((size_t)bytes_read < entry_size) {
292 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
293 "Truncated input file");
294 return (ARCHIVE_FATAL);
297 * Don't consume the contents, so the client will
298 * also get a shot at reading it.
301 /* Parse the filename table. */
302 return (ar_parse_gnu_filename_table(a, ar, b, entry_size));
306 * GNU variant handles long filenames by storing /<number>
307 * to indicate a name stored in the filename table.
309 if (filename[0] == '/' && isdigit(filename[1])) {
310 number = ar_atol10(h + AR_name_offset + 1, AR_name_size - 1);
312 * If we can't look up the real name, warn and return
313 * the entry with the wrong name.
315 if (ar->strtab == NULL || number > ar->strtab_size) {
316 archive_set_error(&a->archive, EINVAL,
317 "Can't find long filename for entry");
318 archive_entry_copy_pathname(entry, filename);
319 /* Parse the time, owner, mode, size fields. */
320 ar_parse_common_header(ar, entry, h);
321 return (ARCHIVE_WARN);
324 archive_entry_copy_pathname(entry, &ar->strtab[(size_t)number]);
325 /* Parse the time, owner, mode, size fields. */
326 return (ar_parse_common_header(ar, entry, h));
330 * BSD handles long filenames by storing "#1/" followed by the
331 * length of filename as a decimal number, then prepends the
332 * the filename to the file contents.
334 if (strncmp(filename, "#1/", 3) == 0) {
335 /* Parse the time, owner, mode, size fields. */
336 /* This must occur before _read_ahead is called again. */
337 ar_parse_common_header(ar, entry, h);
339 /* Parse the size of the name, adjust the file size. */
340 number = ar_atol10(h + AR_name_offset + 3, AR_name_size - 3);
341 if ((off_t)number > ar->entry_bytes_remaining) {
342 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
343 "Bad input file size");
344 return (ARCHIVE_FATAL);
346 bsd_name_length = (size_t)number;
347 ar->entry_bytes_remaining -= bsd_name_length;
348 /* Adjust file size reported to client. */
349 archive_entry_set_size(entry, ar->entry_bytes_remaining);
351 /* Read the long name into memory. */
352 bytes_read = (a->decompressor->read_ahead)(a, &b, bsd_name_length);
353 if (bytes_read <= 0)
354 return (ARCHIVE_FATAL);
355 if ((size_t)bytes_read < bsd_name_length) {
356 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
357 "Truncated input file");
358 return (ARCHIVE_FATAL);
360 (a->decompressor->consume)(a, bsd_name_length);
362 /* Store it in the entry. */
363 p = (char *)malloc(bsd_name_length + 1);
364 if (p == NULL) {
365 archive_set_error(&a->archive, ENOMEM,
366 "Can't allocate fname buffer");
367 return (ARCHIVE_FATAL);
369 strncpy(p, b, bsd_name_length);
370 p[bsd_name_length] = '\0';
371 archive_entry_copy_pathname(entry, p);
372 free(p);
373 return (ARCHIVE_OK);
377 * "/" is the SVR4/GNU archive symbol table.
379 if (strcmp(filename, "/") == 0) {
380 archive_entry_copy_pathname(entry, "/");
381 /* Parse the time, owner, mode, size fields. */
382 r = ar_parse_common_header(ar, entry, h);
383 /* Force the file type to a regular file. */
384 archive_entry_set_mode(entry,
385 S_IFREG | (archive_entry_mode(entry) & 0777));
386 return (r);
390 * "__.SYMDEF" is a BSD archive symbol table.
392 if (strcmp(filename, "__.SYMDEF") == 0) {
393 archive_entry_copy_pathname(entry, filename);
394 /* Parse the time, owner, mode, size fields. */
395 return (ar_parse_common_header(ar, entry, h));
399 * Otherwise, this is a standard entry. The filename
400 * has already been trimmed as much as possible, based
401 * on our current knowledge of the format.
403 archive_entry_copy_pathname(entry, filename);
404 return (ar_parse_common_header(ar, entry, h));
407 static int
408 ar_parse_common_header(struct ar *ar, struct archive_entry *entry,
409 const char *h)
411 uint64_t n;
413 /* Copy remaining header */
414 archive_entry_set_mtime(entry,
415 (time_t)ar_atol10(h + AR_date_offset, AR_date_size), 0L);
416 archive_entry_set_uid(entry,
417 (uid_t)ar_atol10(h + AR_uid_offset, AR_uid_size));
418 archive_entry_set_gid(entry,
419 (gid_t)ar_atol10(h + AR_gid_offset, AR_gid_size));
420 archive_entry_set_mode(entry,
421 (mode_t)ar_atol8(h + AR_mode_offset, AR_mode_size));
422 n = ar_atol10(h + AR_size_offset, AR_size_size);
424 ar->entry_offset = 0;
425 ar->entry_padding = n % 2;
426 archive_entry_set_size(entry, n);
427 ar->entry_bytes_remaining = n;
428 return (ARCHIVE_OK);
431 static int
432 archive_read_format_ar_read_data(struct archive_read *a,
433 const void **buff, size_t *size, off_t *offset)
435 ssize_t bytes_read;
436 struct ar *ar;
438 ar = (struct ar *)(a->format->data);
440 if (ar->entry_bytes_remaining > 0) {
441 bytes_read = (a->decompressor->read_ahead)(a, buff, 1);
442 if (bytes_read == 0) {
443 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
444 "Truncated ar archive");
445 return (ARCHIVE_FATAL);
447 if (bytes_read < 0)
448 return (ARCHIVE_FATAL);
449 if (bytes_read > ar->entry_bytes_remaining)
450 bytes_read = (ssize_t)ar->entry_bytes_remaining;
451 *size = bytes_read;
452 *offset = ar->entry_offset;
453 ar->entry_offset += bytes_read;
454 ar->entry_bytes_remaining -= bytes_read;
455 (a->decompressor->consume)(a, (size_t)bytes_read);
456 return (ARCHIVE_OK);
457 } else {
458 while (ar->entry_padding > 0) {
459 bytes_read = (a->decompressor->read_ahead)(a, buff, 1);
460 if (bytes_read <= 0)
461 return (ARCHIVE_FATAL);
462 if (bytes_read > ar->entry_padding)
463 bytes_read = (ssize_t)ar->entry_padding;
464 (a->decompressor->consume)(a, (size_t)bytes_read);
465 ar->entry_padding -= bytes_read;
467 *buff = NULL;
468 *size = 0;
469 *offset = ar->entry_offset;
470 return (ARCHIVE_EOF);
474 static int
475 archive_read_format_ar_skip(struct archive_read *a)
477 off_t bytes_skipped;
478 struct ar* ar;
479 int r = ARCHIVE_OK;
480 const void *b; /* Dummy variables */
481 size_t s;
482 off_t o;
484 ar = (struct ar *)(a->format->data);
485 if (a->decompressor->skip == NULL) {
486 while (r == ARCHIVE_OK)
487 r = archive_read_format_ar_read_data(a, &b, &s, &o);
488 return (r);
491 bytes_skipped = (a->decompressor->skip)(a, ar->entry_bytes_remaining +
492 ar->entry_padding);
493 if (bytes_skipped < 0)
494 return (ARCHIVE_FATAL);
496 ar->entry_bytes_remaining = 0;
497 ar->entry_padding = 0;
499 return (ARCHIVE_OK);
502 static int
503 ar_parse_gnu_filename_table(struct archive_read *a, struct ar *ar,
504 const void *h, size_t size)
506 char *p;
508 if (ar->strtab != NULL) {
509 archive_set_error(&a->archive, EINVAL,
510 "More than one string tables exist");
511 return (ARCHIVE_WARN);
514 if (size == 0) {
515 archive_set_error(&a->archive, EINVAL, "Invalid string table");
516 return (ARCHIVE_WARN);
519 ar->strtab_size = size;
520 ar->strtab = malloc(size);
521 if (ar->strtab == NULL) {
522 archive_set_error(&a->archive, ENOMEM,
523 "Can't allocate string table buffer");
524 return (ARCHIVE_FATAL);
527 (void)memcpy(ar->strtab, h, size);
528 for (p = ar->strtab; p < ar->strtab + size - 1; ++p) {
529 if (*p == '/') {
530 *p++ = '\0';
531 if (*p != '\n')
532 goto bad_string_table;
533 *p = '\0';
537 * Sanity check, last two chars must be `/\n' or '\n\n',
538 * depending on whether the string table is padded by a '\n'
539 * (string table produced by GNU ar always has a even size).
541 if (p != ar->strtab + size && *p != '\n')
542 goto bad_string_table;
544 /* Enforce zero termination. */
545 ar->strtab[size - 1] = '\0';
547 return (ARCHIVE_OK);
549 bad_string_table:
550 archive_set_error(&a->archive, EINVAL,
551 "Invalid string table");
552 free(ar->strtab);
553 ar->strtab = NULL;
554 return (ARCHIVE_WARN);
557 static uint64_t
558 ar_atol8(const char *p, unsigned char_cnt)
560 uint64_t l, limit, last_digit_limit;
561 unsigned int digit, base;
563 base = 8;
564 limit = UINT64_MAX / base;
565 last_digit_limit = UINT64_MAX % base;
567 while ((*p == ' ' || *p == '\t') && char_cnt-- > 0)
568 p++;
570 l = 0;
571 digit = *p - '0';
572 while (*p >= '0' && digit < base && char_cnt-- > 0) {
573 if (l>limit || (l == limit && digit > last_digit_limit)) {
574 l = UINT64_MAX; /* Truncate on overflow. */
575 break;
577 l = (l * base) + digit;
578 digit = *++p - '0';
580 return (l);
583 static uint64_t
584 ar_atol10(const char *p, unsigned char_cnt)
586 uint64_t l, limit, last_digit_limit;
587 unsigned int base, digit;
589 base = 10;
590 limit = UINT64_MAX / base;
591 last_digit_limit = UINT64_MAX % base;
593 while ((*p == ' ' || *p == '\t') && char_cnt-- > 0)
594 p++;
595 l = 0;
596 digit = *p - '0';
597 while (*p >= '0' && digit < base && char_cnt-- > 0) {
598 if (l > limit || (l == limit && digit > last_digit_limit)) {
599 l = UINT64_MAX; /* Truncate on overflow. */
600 break;
602 l = (l * base) + digit;
603 digit = *++p - '0';
605 return (l);