2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2008 Joerg Sonnenberger
4 * Copyright (c) 2011-2012 Michihiro NAKAJIMA
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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$");
31 #ifdef HAVE_SYS_STAT_H
41 /* #include <stdint.h> */ /* See archive_platform.h */
50 #include "archive_entry.h"
51 #include "archive_private.h"
52 #include "archive_read_private.h"
53 #include "archive_string.h"
54 #include "archive_pack_dev.h"
63 #define MTREE_HAS_DEVICE 0x0001
64 #define MTREE_HAS_FFLAGS 0x0002
65 #define MTREE_HAS_GID 0x0004
66 #define MTREE_HAS_GNAME 0x0008
67 #define MTREE_HAS_MTIME 0x0010
68 #define MTREE_HAS_NLINK 0x0020
69 #define MTREE_HAS_PERM 0x0040
70 #define MTREE_HAS_SIZE 0x0080
71 #define MTREE_HAS_TYPE 0x0100
72 #define MTREE_HAS_UID 0x0200
73 #define MTREE_HAS_UNAME 0x0400
75 #define MTREE_HAS_OPTIONAL 0x0800
76 #define MTREE_HAS_NOCHANGE 0x1000 /* FreeBSD specific */
78 #define MTREE_HASHTABLE_SIZE 1024
81 struct mtree_option *next;
86 struct mtree_entry *next;
87 struct mtree_option *options;
91 unsigned int name_hash;
92 struct mtree_entry *hashtable_next;
96 struct archive_string line;
102 const char *archive_format_name;
103 struct mtree_entry *entries;
104 struct mtree_entry *this_entry;
105 struct mtree_entry *entry_hashtable[MTREE_HASHTABLE_SIZE];
106 struct archive_string current_dir;
107 struct archive_string contents_name;
109 struct archive_entry_linkresolver *resolver;
115 static int bid_keycmp(const char *, const char *, ssize_t);
116 static int cleanup(struct archive_read *);
117 static int detect_form(struct archive_read *, int *);
118 static unsigned int hash(const char *);
119 static int mtree_bid(struct archive_read *, int);
120 static int parse_file(struct archive_read *, struct archive_entry *,
121 struct mtree *, struct mtree_entry *, int *);
122 static void parse_escapes(char *, struct mtree_entry *);
123 static int parse_line(struct archive_read *, struct archive_entry *,
124 struct mtree *, struct mtree_entry *, int *);
125 static int parse_keyword(struct archive_read *, struct mtree *,
126 struct archive_entry *, struct mtree_option *, int *);
127 static int read_data(struct archive_read *a,
128 const void **buff, size_t *size, int64_t *offset);
129 static ssize_t readline(struct archive_read *, struct mtree *, char **, ssize_t);
130 static int skip(struct archive_read *a);
131 static int read_header(struct archive_read *,
132 struct archive_entry *);
133 static int64_t mtree_atol10(char **);
134 static int64_t mtree_atol8(char **);
135 static int64_t mtree_atol(char **);
138 * There's no standard for TIME_T_MAX/TIME_T_MIN. So we compute them
139 * here. TODO: Move this to configure time, but be careful
140 * about cross-compile environments.
145 #if defined(TIME_T_MAX)
148 /* ISO C allows time_t to be a floating-point type,
149 but POSIX requires an integer type. The following
150 should work on any system that follows the POSIX
152 if (((time_t)0) < ((time_t)-1)) {
153 /* Time_t is unsigned */
156 /* Time_t is signed. */
157 /* Assume it's the same as int64_t or int32_t */
158 if (sizeof(time_t) == sizeof(int64_t)) {
159 return (time_t)INT64_MAX;
161 return (time_t)INT32_MAX;
170 #if defined(TIME_T_MIN)
173 if (((time_t)0) < ((time_t)-1)) {
174 /* Time_t is unsigned */
177 /* Time_t is signed. */
178 if (sizeof(time_t) == sizeof(int64_t)) {
179 return (time_t)INT64_MIN;
181 return (time_t)INT32_MIN;
188 archive_read_format_mtree_options(struct archive_read *a,
189 const char *key, const char *val)
193 mtree = (struct mtree *)(a->format->data);
194 if (strcmp(key, "checkfs") == 0) {
195 /* Allows to read information missing from the mtree from the file system */
196 if (val == NULL || val[0] == 0) {
204 /* Note: The "warn" return is just to inform the options
205 * supervisor that we didn't handle it. It will generate
206 * a suitable error if no one used this option. */
207 return (ARCHIVE_WARN);
211 free_options(struct mtree_option *head)
213 struct mtree_option *next;
215 for (; head != NULL; head = next) {
223 archive_read_support_format_mtree(struct archive *_a)
225 struct archive_read *a = (struct archive_read *)_a;
229 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
230 ARCHIVE_STATE_NEW, "archive_read_support_format_mtree");
232 mtree = (struct mtree *)calloc(1, sizeof(*mtree));
234 archive_set_error(&a->archive, ENOMEM,
235 "Can't allocate mtree data");
236 return (ARCHIVE_FATAL);
240 r = __archive_read_register_format(a, mtree, "mtree",
241 mtree_bid, archive_read_format_mtree_options, read_header, read_data, skip, NULL, cleanup, NULL, NULL);
249 cleanup(struct archive_read *a)
252 struct mtree_entry *p, *q;
254 mtree = (struct mtree *)(a->format->data);
260 free_options(p->options);
264 archive_string_free(&mtree->line);
265 archive_string_free(&mtree->current_dir);
266 archive_string_free(&mtree->contents_name);
267 archive_entry_linkresolver_free(mtree->resolver);
271 (a->format->data) = NULL;
276 get_line_size(const char *b, ssize_t avail, ssize_t *nlsize)
281 while (len < avail) {
283 case '\0':/* Non-ascii character or control character. */
288 if (avail-len > 1 && b[1] == '\n') {
310 * <---------------- ravail --------------------->
311 * <-- diff ------> <--- avail ----------------->
312 * <---- len ----------->
313 * | Previous lines | line being parsed nl extra |
319 next_line(struct archive_read *a,
320 const char **b, ssize_t *avail, ssize_t *ravail, ssize_t *nl)
330 len = get_line_size(*b, *avail, nl);
332 * Read bytes more while it does not reach the end of line.
334 while (*nl == 0 && len == *avail && !quit) {
335 ssize_t diff = *ravail - *avail;
336 size_t nbytes_req = (*ravail+1023) & ~1023U;
339 /* Increase reading bytes if it is not enough to at least
341 if (nbytes_req < (size_t)*ravail + 160)
344 *b = __archive_read_ahead(a, nbytes_req, avail);
346 if (*ravail >= *avail)
348 /* Reading bytes reaches the end of file. */
349 *b = __archive_read_ahead(a, *avail, avail);
355 tested = len;/* Skip some bytes we already determinated. */
356 len = get_line_size(*b + len, *avail - len, nl);
364 * Compare characters with a mtree keyword.
365 * Returns the length of a mtree keyword if matched.
366 * Returns 0 if not matched.
369 bid_keycmp(const char *p, const char *key, ssize_t len)
373 while (len > 0 && *p && *key) {
381 return (0);/* Not match */
384 return (0);/* Not match */
386 /* A following character should be specified characters */
387 if (p[0] == '=' || p[0] == ' ' || p[0] == '\t' ||
388 p[0] == '\n' || p[0] == '\r' ||
389 (p[0] == '\\' && (p[1] == '\n' || p[1] == '\r')))
391 return (0);/* Not match */
395 * Test whether the characters 'p' has is mtree keyword.
396 * Returns the length of a detected keyword.
397 * Returns 0 if any keywords were not found.
400 bid_keyword(const char *p, ssize_t len)
402 static const char *keys_c[] = {
403 "content", "contents", "cksum", NULL
405 static const char *keys_df[] = {
406 "device", "flags", NULL
408 static const char *keys_g[] = {
411 static const char *keys_il[] = {
412 "ignore", "inode", "link", NULL
414 static const char *keys_m[] = {
415 "md5", "md5digest", "mode", NULL
417 static const char *keys_no[] = {
418 "nlink", "nochange", "optional", NULL
420 static const char *keys_r[] = {
421 "resdevice", "rmd160", "rmd160digest", NULL
423 static const char *keys_s[] = {
424 "sha1", "sha1digest",
425 "sha256", "sha256digest",
426 "sha384", "sha384digest",
427 "sha512", "sha512digest",
430 static const char *keys_t[] = {
431 "tags", "time", "type", NULL
433 static const char *keys_u[] = {
440 case 'c': keys = keys_c; break;
441 case 'd': case 'f': keys = keys_df; break;
442 case 'g': keys = keys_g; break;
443 case 'i': case 'l': keys = keys_il; break;
444 case 'm': keys = keys_m; break;
445 case 'n': case 'o': keys = keys_no; break;
446 case 'r': keys = keys_r; break;
447 case 's': keys = keys_s; break;
448 case 't': keys = keys_t; break;
449 case 'u': keys = keys_u; break;
450 default: return (0);/* Unknown key */
453 for (i = 0; keys[i] != NULL; i++) {
454 int l = bid_keycmp(p, keys[i], len);
458 return (0);/* Unknown key */
462 * Test whether there is a set of mtree keywords.
463 * Returns the number of keyword.
464 * Returns -1 if we got incorrect sequence.
465 * This function expects a set of "<space characters>keyword=value".
466 * When "unset" is specified, expects a set of "<space characters>keyword".
469 bid_keyword_list(const char *p, ssize_t len, int unset, int last_is_path)
474 while (len > 0 && *p) {
477 /* Test whether there are blank characters in the line. */
478 while (len >0 && (*p == ' ' || *p == '\t')) {
483 if (*p == '\n' || *p == '\r')
485 if (p[0] == '\\' && (p[1] == '\n' || p[1] == '\r'))
487 if (!blank && !last_is_path) /* No blank character. */
489 if (last_is_path && len == 0)
493 l = bid_keycmp(p, "all", len);
497 /* Test whether there is a correct key in the line. */
498 l = bid_keyword(p, len);
500 return (-1);/* Unknown keyword was found. */
510 while (len > 0 && *p != ' ' && *p != '\t') {
515 /* A keyword should have a its value unless
516 * "/unset" operation. */
517 if (!unset && value == 0)
525 bid_entry(const char *p, ssize_t len, ssize_t nl, int *last_is_path)
528 static const unsigned char safe_char[256] = {
529 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 00 - 0F */
530 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 1F */
531 /* !"$%&'()*+,-./ EXCLUSION:( )(#) */
532 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 - 2F */
533 /* 0123456789:;<>? EXCLUSION:(=) */
534 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, /* 30 - 3F */
535 /* @ABCDEFGHIJKLMNO */
536 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 40 - 4F */
537 /* PQRSTUVWXYZ[\]^_ */
538 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 50 - 5F */
539 /* `abcdefghijklmno */
540 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 60 - 6F */
541 /* pqrstuvwxyz{|}~ */
542 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, /* 70 - 7F */
543 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
544 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
545 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
546 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
547 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
548 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
549 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
550 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
554 const char * const pp_end = pp + len;
558 * Skip the path-name which is quoted.
560 for (;pp < pp_end; ++pp) {
561 if (!safe_char[*(const unsigned char *)pp]) {
562 if (*pp != ' ' && *pp != '\t' && *pp != '\r'
571 /* If a path-name was not found at the first, try to check
572 * a mtree format(a.k.a form D) ``NetBSD's mtree -D'' creates,
573 * which places the path-name at the last. */
575 const char *pb = p + len - nl;
579 /* The form D accepts only a single line for an entry. */
581 pb[-1] == '\\' && (pb[-2] == ' ' || pb[-2] == '\t'))
583 if (pb-1 >= p && pb[-1] == '\\')
587 while (p <= --pb && *pb != ' ' && *pb != '\t') {
588 if (!safe_char[*(const unsigned char *)pb])
591 /* The pathname should have a slash in this
596 if (name_len == 0 || slash == 0)
598 /* If '/' is placed at the first in this field, this is not
599 * a valid filename. */
602 ll = len - nl - name_len;
607 return (bid_keyword_list(pp, ll, 0, *last_is_path));
610 #define MAX_BID_ENTRY 3
613 mtree_bid(struct archive_read *a, int best_bid)
615 const char *signature = "#mtree";
618 (void)best_bid; /* UNUSED */
620 /* Now let's look at the actual header and see if it matches. */
621 p = __archive_read_ahead(a, strlen(signature), NULL);
625 if (memcmp(p, signature, strlen(signature)) == 0)
626 return (8 * (int)strlen(signature));
629 * There is not a mtree signature. Let's try to detect mtree format.
631 return (detect_form(a, NULL));
635 detect_form(struct archive_read *a, int *is_form_d)
638 ssize_t avail, ravail;
639 ssize_t detected_bytes = 0, len, nl;
640 int entry_cnt = 0, multiline = 0;
641 int form_D = 0;/* The archive is generated by `NetBSD mtree -D'
642 * (In this source we call it `form D') . */
644 if (is_form_d != NULL)
646 p = __archive_read_ahead(a, 1, &avail);
651 len = next_line(a, &p, &avail, &ravail, &nl);
652 /* The terminal character of the line should be
653 * a new line character, '\r\n' or '\n'. */
654 if (len <= 0 || nl == 0)
657 /* Leading whitespace is never significant,
659 while (len > 0 && (*p == ' ' || *p == '\t')) {
664 /* Skip comment or empty line. */
665 if (p[0] == '#' || p[0] == '\n' || p[0] == '\r') {
671 /* A continuance line; the terminal
672 * character of previous line was '\' character. */
673 if (bid_keyword_list(p, len, 0, 0) <= 0)
676 detected_bytes += len;
677 if (p[len-nl-1] != '\\') {
678 if (multiline == 1 &&
679 ++entry_cnt >= MAX_BID_ENTRY)
688 int last_is_path, keywords;
690 keywords = bid_entry(p, len, nl, &last_is_path);
692 detected_bytes += len;
696 else if (keywords > 0)
697 /* This line is not `form D'. */
699 } else if (form_D == 1) {
700 if (!last_is_path && keywords > 0)
701 /* This this is not `form D'
702 * and We cannot accept mixed
706 if (!last_is_path && p[len-nl-1] == '\\')
707 /* This line continues. */
710 /* We've got plenty of correct lines
711 * to assume that this file is a mtree
713 if (++entry_cnt >= MAX_BID_ENTRY)
718 } else if (strncmp(p, "/set", 4) == 0) {
719 if (bid_keyword_list(p+4, len-4, 0, 0) <= 0)
721 /* This line continues. */
722 if (p[len-nl-1] == '\\')
724 } else if (strncmp(p, "/unset", 6) == 0) {
725 if (bid_keyword_list(p+6, len-6, 1, 0) <= 0)
727 /* This line continues. */
728 if (p[len-nl-1] == '\\')
733 /* Test next line. */
737 if (entry_cnt >= MAX_BID_ENTRY || (entry_cnt > 0 && len == 0)) {
738 if (is_form_d != NULL) {
749 * The extended mtree format permits multiple lines specifying
750 * attributes for each file. For those entries, only the last line
751 * is actually used. Practically speaking, that means we have
752 * to read the entire mtree file into memory up front.
754 * The parsing is done in two steps. First, it is decided if a line
755 * changes the global defaults and if it is, processed accordingly.
756 * Otherwise, the options of the line are merged with the current
760 add_option(struct archive_read *a, struct mtree_option **global,
761 const char *value, size_t len)
763 struct mtree_option *opt;
765 if ((opt = malloc(sizeof(*opt))) == NULL) {
766 archive_set_error(&a->archive, errno, "Can't allocate memory");
767 return (ARCHIVE_FATAL);
769 if ((opt->value = malloc(len + 1)) == NULL) {
771 archive_set_error(&a->archive, errno, "Can't allocate memory");
772 return (ARCHIVE_FATAL);
774 memcpy(opt->value, value, len);
775 opt->value[len] = '\0';
782 remove_option(struct mtree_option **global, const char *value, size_t len)
784 struct mtree_option *iter, *last;
787 for (iter = *global; iter != NULL; last = iter, iter = iter->next) {
788 if (strncmp(iter->value, value, len) == 0 &&
789 (iter->value[len] == '\0' ||
790 iter->value[len] == '='))
796 *global = iter->next;
798 last->next = iter->next;
805 process_global_set(struct archive_read *a,
806 struct mtree_option **global, const char *line)
808 const char *next, *eq;
814 next = line + strspn(line, " \t\r\n");
818 next = line + strcspn(line, " \t\r\n");
819 eq = strchr(line, '=');
825 remove_option(global, line, len);
826 r = add_option(a, global, line, next - line);
834 process_global_unset(struct archive_read *a,
835 struct mtree_option **global, const char *line)
841 if (strchr(line, '=') != NULL) {
842 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
843 "/unset shall not contain `='");
844 return ARCHIVE_FATAL;
848 next = line + strspn(line, " \t\r\n");
852 len = strcspn(line, " \t\r\n");
854 if (len == 3 && strncmp(line, "all", 3) == 0) {
855 free_options(*global);
858 remove_option(global, line, len);
866 process_add_entry(struct archive_read *a, struct mtree *mtree,
867 struct mtree_option **global, const char *line, ssize_t line_len,
868 struct mtree_entry **last_entry, int is_form_d)
870 struct mtree_entry *entry, *ht_iter;
871 struct mtree_option *iter;
872 const char *next, *eq, *name, *end;
873 size_t name_len, len;
877 if ((entry = malloc(sizeof(*entry))) == NULL) {
878 archive_set_error(&a->archive, errno, "Can't allocate memory");
879 return (ARCHIVE_FATAL);
882 entry->options = NULL;
886 entry->name_hash = 0;
887 entry->hashtable_next = NULL;
889 /* Add this entry to list. */
890 if (*last_entry == NULL)
891 mtree->entries = entry;
893 (*last_entry)->next = entry;
897 /* Filename is last item on line. */
898 /* Adjust line_len to trim trailing whitespace */
899 while (line_len > 0) {
900 char last_character = line[line_len - 1];
901 if (last_character == '\r'
902 || last_character == '\n'
903 || last_character == '\t'
904 || last_character == ' ') {
910 /* Name starts after the last whitespace separator */
912 for (i = 0; i < line_len; i++) {
920 name_len = line + line_len - name;
923 /* Filename is first item on line */
924 name_len = strcspn(line, " \t\r\n");
927 end = line + line_len;
929 /* name/name_len is the name within the line. */
930 /* line..end brackets the entire line except the name */
932 if ((entry->name = malloc(name_len + 1)) == NULL) {
933 archive_set_error(&a->archive, errno, "Can't allocate memory");
934 return (ARCHIVE_FATAL);
937 memcpy(entry->name, name, name_len);
938 entry->name[name_len] = '\0';
939 parse_escapes(entry->name, entry);
940 entry->name_hash = hash(entry->name);
942 ht_idx = entry->name_hash % MTREE_HASHTABLE_SIZE;
943 if ((ht_iter = mtree->entry_hashtable[ht_idx]) != NULL) {
944 while (ht_iter->hashtable_next)
945 ht_iter = ht_iter->hashtable_next;
946 ht_iter->hashtable_next = entry;
948 mtree->entry_hashtable[ht_idx] = entry;
951 for (iter = *global; iter != NULL; iter = iter->next) {
952 r = add_option(a, &entry->options, iter->value,
953 strlen(iter->value));
959 next = line + strspn(line, " \t\r\n");
965 next = line + strcspn(line, " \t\r\n");
966 eq = strchr(line, '=');
967 if (eq == NULL || eq > next)
972 remove_option(&entry->options, line, len);
973 r = add_option(a, &entry->options, line, next - line);
981 read_mtree(struct archive_read *a, struct mtree *mtree)
986 struct mtree_option *global;
987 struct mtree_entry *last_entry;
990 mtree->archive_format = ARCHIVE_FORMAT_MTREE;
991 mtree->archive_format_name = "mtree";
996 (void)detect_form(a, &is_form_d);
998 for (counter = 1; ; ++counter) {
999 len = readline(a, mtree, &p, 65536);
1001 mtree->this_entry = mtree->entries;
1002 free_options(global);
1003 return (ARCHIVE_OK);
1006 free_options(global);
1009 /* Leading whitespace is never significant, ignore it. */
1010 while (*p == ' ' || *p == '\t') {
1014 /* Skip content lines and blank lines. */
1017 if (*p == '\r' || *p == '\n' || *p == '\0')
1020 r = process_add_entry(a, mtree, &global, p, len,
1021 &last_entry, is_form_d);
1022 } else if (strncmp(p, "/set", 4) == 0) {
1023 if (p[4] != ' ' && p[4] != '\t')
1025 r = process_global_set(a, &global, p);
1026 } else if (strncmp(p, "/unset", 6) == 0) {
1027 if (p[6] != ' ' && p[6] != '\t')
1029 r = process_global_unset(a, &global, p);
1033 if (r != ARCHIVE_OK) {
1034 free_options(global);
1039 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1040 "Can't parse line %ju", counter);
1041 free_options(global);
1042 return (ARCHIVE_FATAL);
1046 * Read in the entire mtree file into memory on the first request.
1047 * Then use the next unused file to satisfy each header request.
1050 read_header(struct archive_read *a, struct archive_entry *entry)
1052 struct mtree *mtree;
1056 mtree = (struct mtree *)(a->format->data);
1058 if (mtree->fd >= 0) {
1063 if (mtree->entries == NULL) {
1064 mtree->resolver = archive_entry_linkresolver_new();
1065 if (mtree->resolver == NULL)
1066 return ARCHIVE_FATAL;
1067 archive_entry_linkresolver_set_strategy(mtree->resolver,
1068 ARCHIVE_FORMAT_MTREE);
1069 r = read_mtree(a, mtree);
1070 if (r != ARCHIVE_OK)
1074 a->archive.archive_format = mtree->archive_format;
1075 a->archive.archive_format_name = mtree->archive_format_name;
1078 if (mtree->this_entry == NULL)
1079 return (ARCHIVE_EOF);
1080 if (strcmp(mtree->this_entry->name, "..") == 0) {
1081 mtree->this_entry->used = 1;
1082 if (archive_strlen(&mtree->current_dir) > 0) {
1083 /* Roll back current path. */
1084 p = mtree->current_dir.s
1085 + mtree->current_dir.length - 1;
1086 while (p >= mtree->current_dir.s && *p != '/')
1088 if (p >= mtree->current_dir.s)
1090 mtree->current_dir.length
1091 = p - mtree->current_dir.s + 1;
1094 if (!mtree->this_entry->used) {
1096 r = parse_file(a, entry, mtree, mtree->this_entry,
1101 mtree->this_entry = mtree->this_entry->next;
1106 * A single file can have multiple lines contribute specifications.
1107 * Parse as many lines as necessary, then pull additional information
1108 * from a backing file on disk as necessary.
1111 parse_file(struct archive_read *a, struct archive_entry *entry,
1112 struct mtree *mtree, struct mtree_entry *mentry, int *use_next)
1115 struct stat st_storage, *st;
1116 struct mtree_entry *mp;
1117 struct archive_entry *sparse_entry;
1118 int r = ARCHIVE_OK, r1, parsed_kws;
1122 /* Initialize reasonable defaults. */
1123 archive_entry_set_filetype(entry, AE_IFREG);
1124 archive_entry_set_size(entry, 0);
1125 archive_string_empty(&mtree->contents_name);
1127 /* Parse options from this line. */
1129 r = parse_line(a, entry, mtree, mentry, &parsed_kws);
1132 archive_entry_copy_pathname(entry, mentry->name);
1134 * "Full" entries are allowed to have multiple lines
1135 * and those lines aren't required to be adjacent. We
1136 * don't support multiple lines for "relative" entries
1137 * nor do we make any attempt to merge data from
1138 * separate "relative" and "full" entries. (Merging
1139 * "relative" and "full" entries would require dealing
1140 * with pathname canonicalization, which is a very
1143 for (mp = mentry->hashtable_next; mp != NULL; mp = mp->hashtable_next) {
1144 if (mp->full && !mp->used
1145 && mentry->name_hash == mp->name_hash
1146 && strcmp(mentry->name, mp->name) == 0) {
1147 /* Later lines override earlier ones. */
1149 r1 = parse_line(a, entry, mtree, mp,
1157 * Relative entries require us to construct
1158 * the full path and possibly update the
1159 * current directory.
1161 size_t n = archive_strlen(&mtree->current_dir);
1163 archive_strcat(&mtree->current_dir, "/");
1164 archive_strcat(&mtree->current_dir, mentry->name);
1165 archive_entry_copy_pathname(entry, mtree->current_dir.s);
1166 if (archive_entry_filetype(entry) != AE_IFDIR)
1167 mtree->current_dir.length = n;
1170 if (mtree->checkfs) {
1172 * Try to open and stat the file to get the real size
1173 * and other file info. It would be nice to avoid
1174 * this here so that getting a listing of an mtree
1175 * wouldn't require opening every referenced contents
1176 * file. But then we wouldn't know the actual
1177 * contents size, so I don't see a really viable way
1178 * around this. (Also, we may want to someday pull
1179 * other unspecified info from the contents file on
1183 if (archive_strlen(&mtree->contents_name) > 0)
1184 path = mtree->contents_name.s;
1186 path = archive_entry_pathname(entry);
1188 if (archive_entry_filetype(entry) == AE_IFREG ||
1189 archive_entry_filetype(entry) == AE_IFDIR) {
1190 mtree->fd = open(path, O_RDONLY | O_BINARY | O_CLOEXEC);
1191 __archive_ensure_cloexec_flag(mtree->fd);
1192 if (mtree->fd == -1 &&
1194 archive_strlen(&mtree->contents_name) > 0)) {
1195 archive_set_error(&a->archive, errno,
1196 "Can't open %s", path);
1202 if (mtree->fd >= 0) {
1203 if (fstat(mtree->fd, st) == -1) {
1204 archive_set_error(&a->archive, errno,
1205 "Could not fstat %s", path);
1207 /* If we can't stat it, don't keep it open. */
1212 } else if (lstat(path, st) == -1) {
1217 * Check for a mismatch between the type in the specification
1218 * and the type of the contents object on disk.
1221 if (((st->st_mode & S_IFMT) == S_IFREG &&
1222 archive_entry_filetype(entry) == AE_IFREG)
1224 ||((st->st_mode & S_IFMT) == S_IFLNK &&
1225 archive_entry_filetype(entry) == AE_IFLNK)
1228 ||((st->st_mode & S_IFSOCK) == S_IFSOCK &&
1229 archive_entry_filetype(entry) == AE_IFSOCK)
1232 ||((st->st_mode & S_IFMT) == S_IFCHR &&
1233 archive_entry_filetype(entry) == AE_IFCHR)
1236 ||((st->st_mode & S_IFMT) == S_IFBLK &&
1237 archive_entry_filetype(entry) == AE_IFBLK)
1239 ||((st->st_mode & S_IFMT) == S_IFDIR &&
1240 archive_entry_filetype(entry) == AE_IFDIR)
1242 ||((st->st_mode & S_IFMT) == S_IFIFO &&
1243 archive_entry_filetype(entry) == AE_IFIFO)
1248 /* Types don't match; bail out gracefully. */
1252 if (parsed_kws & MTREE_HAS_OPTIONAL) {
1253 /* It's not an error for an optional
1254 * entry to not match disk. */
1256 } else if (r == ARCHIVE_OK) {
1257 archive_set_error(&a->archive,
1259 "mtree specification has different"
1261 archive_entry_pathname(entry));
1269 * If there is a contents file on disk, pick some of the
1270 * metadata from that file. For most of these, we only
1271 * set it from the contents if it wasn't already parsed
1272 * from the specification.
1275 if (((parsed_kws & MTREE_HAS_DEVICE) == 0 ||
1276 (parsed_kws & MTREE_HAS_NOCHANGE) != 0) &&
1277 (archive_entry_filetype(entry) == AE_IFCHR ||
1278 archive_entry_filetype(entry) == AE_IFBLK))
1279 archive_entry_set_rdev(entry, st->st_rdev);
1280 if ((parsed_kws & (MTREE_HAS_GID | MTREE_HAS_GNAME))
1282 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1283 archive_entry_set_gid(entry, st->st_gid);
1284 if ((parsed_kws & (MTREE_HAS_UID | MTREE_HAS_UNAME))
1286 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1287 archive_entry_set_uid(entry, st->st_uid);
1288 if ((parsed_kws & MTREE_HAS_MTIME) == 0 ||
1289 (parsed_kws & MTREE_HAS_NOCHANGE) != 0) {
1290 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
1291 archive_entry_set_mtime(entry, st->st_mtime,
1292 st->st_mtimespec.tv_nsec);
1293 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
1294 archive_entry_set_mtime(entry, st->st_mtime,
1295 st->st_mtim.tv_nsec);
1296 #elif HAVE_STRUCT_STAT_ST_MTIME_N
1297 archive_entry_set_mtime(entry, st->st_mtime,
1299 #elif HAVE_STRUCT_STAT_ST_UMTIME
1300 archive_entry_set_mtime(entry, st->st_mtime,
1301 st->st_umtime*1000);
1302 #elif HAVE_STRUCT_STAT_ST_MTIME_USEC
1303 archive_entry_set_mtime(entry, st->st_mtime,
1304 st->st_mtime_usec*1000);
1306 archive_entry_set_mtime(entry, st->st_mtime, 0);
1309 if ((parsed_kws & MTREE_HAS_NLINK) == 0 ||
1310 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1311 archive_entry_set_nlink(entry, st->st_nlink);
1312 if ((parsed_kws & MTREE_HAS_PERM) == 0 ||
1313 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1314 archive_entry_set_perm(entry, st->st_mode);
1315 if ((parsed_kws & MTREE_HAS_SIZE) == 0 ||
1316 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1317 archive_entry_set_size(entry, st->st_size);
1318 archive_entry_set_ino(entry, st->st_ino);
1319 archive_entry_set_dev(entry, st->st_dev);
1321 archive_entry_linkify(mtree->resolver, &entry,
1323 } else if (parsed_kws & MTREE_HAS_OPTIONAL) {
1325 * Couldn't open the entry, stat it or the on-disk type
1326 * didn't match. If this entry is optional, just
1327 * ignore it and read the next header entry.
1334 mtree->cur_size = archive_entry_size(entry);
1341 * Each line contains a sequence of keywords.
1344 parse_line(struct archive_read *a, struct archive_entry *entry,
1345 struct mtree *mtree, struct mtree_entry *mp, int *parsed_kws)
1347 struct mtree_option *iter;
1348 int r = ARCHIVE_OK, r1;
1350 for (iter = mp->options; iter != NULL; iter = iter->next) {
1351 r1 = parse_keyword(a, mtree, entry, iter, parsed_kws);
1355 if (r == ARCHIVE_OK && (*parsed_kws & MTREE_HAS_TYPE) == 0) {
1356 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1357 "Missing type keyword in mtree specification");
1358 return (ARCHIVE_WARN);
1364 * Device entries have one of the following forms:
1366 * - format,major,minor[,subdevice]
1367 * When parsing succeeded, `pdev' will contain the appropriate dev_t value.
1370 /* strsep() is not in C90, but strcspn() is. */
1371 /* Taken from http://unixpapa.com/incnote/string.html */
1373 la_strsep(char **sp, const char *sep)
1376 if (sp == NULL || *sp == NULL || **sp == '\0')
1379 p = s + strcspn(s, sep);
1387 parse_device(dev_t *pdev, struct archive *a, char *val)
1389 #define MAX_PACK_ARGS 3
1390 unsigned long numbers[MAX_PACK_ARGS];
1395 const char *error = NULL;
1397 memset(pdev, 0, sizeof(*pdev));
1398 if ((dev = strchr(val, ',')) != NULL) {
1400 * Device's major/minor are given in a specified format.
1401 * Decode and pack it accordingly.
1404 if ((pack = pack_find(val)) == NULL) {
1405 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1406 "Unknown format `%s'", val);
1407 return ARCHIVE_WARN;
1410 while ((p = la_strsep(&dev, ",")) != NULL) {
1412 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1414 return ARCHIVE_WARN;
1416 if (argc >= MAX_PACK_ARGS) {
1417 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1418 "Too many arguments");
1419 return ARCHIVE_WARN;
1421 numbers[argc++] = (unsigned long)mtree_atol(&p);
1424 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1425 "Not enough arguments");
1426 return ARCHIVE_WARN;
1428 result = (*pack)(argc, numbers, &error);
1429 if (error != NULL) {
1430 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1432 return ARCHIVE_WARN;
1435 /* file system raw value. */
1436 result = (dev_t)mtree_atol(&val);
1440 #undef MAX_PACK_ARGS
1444 * Parse a single keyword and its value.
1447 parse_keyword(struct archive_read *a, struct mtree *mtree,
1448 struct archive_entry *entry, struct mtree_option *opt, int *parsed_kws)
1455 return (ARCHIVE_OK);
1457 if (strcmp(key, "nochange") == 0) {
1458 *parsed_kws |= MTREE_HAS_NOCHANGE;
1459 return (ARCHIVE_OK);
1461 if (strcmp(key, "optional") == 0) {
1462 *parsed_kws |= MTREE_HAS_OPTIONAL;
1463 return (ARCHIVE_OK);
1465 if (strcmp(key, "ignore") == 0) {
1467 * The mtree processing is not recursive, so
1468 * recursion will only happen for explicitly listed
1471 return (ARCHIVE_OK);
1474 val = strchr(key, '=');
1476 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1477 "Malformed attribute \"%s\" (%d)", key, key[0]);
1478 return (ARCHIVE_WARN);
1486 if (strcmp(key, "content") == 0
1487 || strcmp(key, "contents") == 0) {
1488 parse_escapes(val, NULL);
1489 archive_strcpy(&mtree->contents_name, val);
1492 if (strcmp(key, "cksum") == 0)
1495 if (strcmp(key, "device") == 0) {
1496 /* stat(2) st_rdev field, e.g. the major/minor IDs
1497 * of a char/block special file */
1501 *parsed_kws |= MTREE_HAS_DEVICE;
1502 r = parse_device(&dev, &a->archive, val);
1503 if (r == ARCHIVE_OK)
1504 archive_entry_set_rdev(entry, dev);
1508 if (strcmp(key, "flags") == 0) {
1509 *parsed_kws |= MTREE_HAS_FFLAGS;
1510 archive_entry_copy_fflags_text(entry, val);
1514 if (strcmp(key, "gid") == 0) {
1515 *parsed_kws |= MTREE_HAS_GID;
1516 archive_entry_set_gid(entry, mtree_atol10(&val));
1519 if (strcmp(key, "gname") == 0) {
1520 *parsed_kws |= MTREE_HAS_GNAME;
1521 archive_entry_copy_gname(entry, val);
1525 if (strcmp(key, "inode") == 0) {
1526 archive_entry_set_ino(entry, mtree_atol10(&val));
1530 if (strcmp(key, "link") == 0) {
1531 archive_entry_copy_symlink(entry, val);
1535 if (strcmp(key, "md5") == 0 || strcmp(key, "md5digest") == 0)
1537 if (strcmp(key, "mode") == 0) {
1538 if (val[0] >= '0' && val[0] <= '9') {
1539 *parsed_kws |= MTREE_HAS_PERM;
1540 archive_entry_set_perm(entry,
1541 (mode_t)mtree_atol8(&val));
1543 archive_set_error(&a->archive,
1544 ARCHIVE_ERRNO_FILE_FORMAT,
1545 "Symbolic mode \"%s\" unsupported", val);
1546 return ARCHIVE_WARN;
1551 if (strcmp(key, "nlink") == 0) {
1552 *parsed_kws |= MTREE_HAS_NLINK;
1553 archive_entry_set_nlink(entry,
1554 (unsigned int)mtree_atol10(&val));
1558 if (strcmp(key, "resdevice") == 0) {
1559 /* stat(2) st_dev field, e.g. the device ID where the
1564 r = parse_device(&dev, &a->archive, val);
1565 if (r == ARCHIVE_OK)
1566 archive_entry_set_dev(entry, dev);
1569 if (strcmp(key, "rmd160") == 0 ||
1570 strcmp(key, "rmd160digest") == 0)
1573 if (strcmp(key, "sha1") == 0 || strcmp(key, "sha1digest") == 0)
1575 if (strcmp(key, "sha256") == 0 ||
1576 strcmp(key, "sha256digest") == 0)
1578 if (strcmp(key, "sha384") == 0 ||
1579 strcmp(key, "sha384digest") == 0)
1581 if (strcmp(key, "sha512") == 0 ||
1582 strcmp(key, "sha512digest") == 0)
1584 if (strcmp(key, "size") == 0) {
1585 archive_entry_set_size(entry, mtree_atol10(&val));
1589 if (strcmp(key, "tags") == 0) {
1591 * Comma delimited list of tags.
1592 * Ignore the tags for now, but the interface
1593 * should be extended to allow inclusion/exclusion.
1597 if (strcmp(key, "time") == 0) {
1599 int64_t my_time_t_max = get_time_t_max();
1600 int64_t my_time_t_min = get_time_t_min();
1603 *parsed_kws |= MTREE_HAS_MTIME;
1604 m = mtree_atol10(&val);
1605 /* Replicate an old mtree bug:
1606 * 123456789.1 represents 123456789
1607 * seconds and 1 nanosecond. */
1610 ns = (long)mtree_atol10(&val);
1613 if (m > my_time_t_max)
1615 else if (m < my_time_t_min)
1617 archive_entry_set_mtime(entry, (time_t)m, ns);
1620 if (strcmp(key, "type") == 0) {
1623 if (strcmp(val, "block") == 0) {
1624 archive_entry_set_filetype(entry, AE_IFBLK);
1628 if (strcmp(val, "char") == 0) {
1629 archive_entry_set_filetype(entry,
1634 if (strcmp(val, "dir") == 0) {
1635 archive_entry_set_filetype(entry,
1640 if (strcmp(val, "fifo") == 0) {
1641 archive_entry_set_filetype(entry,
1645 if (strcmp(val, "file") == 0) {
1646 archive_entry_set_filetype(entry,
1651 if (strcmp(val, "link") == 0) {
1652 archive_entry_set_filetype(entry,
1657 archive_set_error(&a->archive,
1658 ARCHIVE_ERRNO_FILE_FORMAT,
1659 "Unrecognized file type \"%s\"; "
1660 "assuming \"file\"", val);
1661 archive_entry_set_filetype(entry, AE_IFREG);
1662 return (ARCHIVE_WARN);
1664 *parsed_kws |= MTREE_HAS_TYPE;
1668 if (strcmp(key, "uid") == 0) {
1669 *parsed_kws |= MTREE_HAS_UID;
1670 archive_entry_set_uid(entry, mtree_atol10(&val));
1673 if (strcmp(key, "uname") == 0) {
1674 *parsed_kws |= MTREE_HAS_UNAME;
1675 archive_entry_copy_uname(entry, val);
1679 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1680 "Unrecognized key %s=%s", key, val);
1681 return (ARCHIVE_WARN);
1683 return (ARCHIVE_OK);
1687 read_data(struct archive_read *a, const void **buff, size_t *size,
1690 size_t bytes_to_read;
1692 struct mtree *mtree;
1694 mtree = (struct mtree *)(a->format->data);
1695 if (mtree->fd < 0) {
1699 return (ARCHIVE_EOF);
1701 if (mtree->buff == NULL) {
1702 mtree->buffsize = 64 * 1024;
1703 mtree->buff = malloc(mtree->buffsize);
1704 if (mtree->buff == NULL) {
1705 archive_set_error(&a->archive, ENOMEM,
1706 "Can't allocate memory");
1707 return (ARCHIVE_FATAL);
1711 *buff = mtree->buff;
1712 *offset = mtree->offset;
1713 if ((int64_t)mtree->buffsize > mtree->cur_size - mtree->offset)
1714 bytes_to_read = (size_t)(mtree->cur_size - mtree->offset);
1716 bytes_to_read = mtree->buffsize;
1717 bytes_read = read(mtree->fd, mtree->buff, bytes_to_read);
1718 if (bytes_read < 0) {
1719 archive_set_error(&a->archive, errno, "Can't read");
1720 return (ARCHIVE_WARN);
1722 if (bytes_read == 0) {
1724 return (ARCHIVE_EOF);
1726 mtree->offset += bytes_read;
1728 return (ARCHIVE_OK);
1731 /* Skip does nothing except possibly close the contents file. */
1733 skip(struct archive_read *a)
1735 struct mtree *mtree;
1737 mtree = (struct mtree *)(a->format->data);
1738 if (mtree->fd >= 0) {
1742 return (ARCHIVE_OK);
1746 * Since parsing backslash sequences always makes strings shorter,
1747 * we can always do this conversion in-place.
1750 parse_escapes(char *src, struct mtree_entry *mentry)
1755 if (mentry != NULL && strcmp(src, ".") == 0)
1758 while (*src != '\0') {
1760 if (c == '/' && mentry != NULL)
1765 if (src[1] < '0' || src[1] > '7') {
1774 if (src[1] >= '0' && src[1] <= '7' &&
1775 src[2] >= '0' && src[2] <= '7') {
1776 c = (src[0] - '0') << 6;
1777 c |= (src[1] - '0') << 3;
1778 c |= (src[2] - '0');
1826 * Note that this implementation does not (and should not!) obey
1827 * locale settings; you cannot simply substitute strtol here, since
1828 * it does obey locale.
1831 mtree_atol8(char **p)
1833 int64_t l, limit, last_digit_limit;
1837 limit = INT64_MAX / base;
1838 last_digit_limit = INT64_MAX % base;
1842 while (digit >= 0 && digit < base) {
1843 if (l>limit || (l == limit && digit > last_digit_limit)) {
1844 l = INT64_MAX; /* Truncate on overflow. */
1847 l = (l * base) + digit;
1848 digit = *++(*p) - '0';
1854 * Note that this implementation does not (and should not!) obey
1855 * locale settings; you cannot simply substitute strtol here, since
1856 * it does obey locale.
1859 mtree_atol10(char **p)
1861 int64_t l, limit, last_digit_limit;
1862 int base, digit, sign;
1868 limit = ((uint64_t)(INT64_MAX) + 1) / base;
1869 last_digit_limit = ((uint64_t)(INT64_MAX) + 1) % base;
1873 limit = INT64_MAX / base;
1874 last_digit_limit = INT64_MAX % base;
1879 while (digit >= 0 && digit < base) {
1880 if (l > limit || (l == limit && digit > last_digit_limit))
1881 return (sign < 0) ? INT64_MIN : INT64_MAX;
1882 l = (l * base) + digit;
1883 digit = *++(*p) - '0';
1885 return (sign < 0) ? -l : l;
1888 /* Parse a hex digit. */
1892 if (c >= '0' && c <= '9')
1894 else if (c >= 'a' && c <= 'f')
1896 else if (c >= 'A' && c <= 'F')
1903 * Note that this implementation does not (and should not!) obey
1904 * locale settings; you cannot simply substitute strtol here, since
1905 * it does obey locale.
1908 mtree_atol16(char **p)
1910 int64_t l, limit, last_digit_limit;
1911 int base, digit, sign;
1917 limit = ((uint64_t)(INT64_MAX) + 1) / base;
1918 last_digit_limit = ((uint64_t)(INT64_MAX) + 1) % base;
1922 limit = INT64_MAX / base;
1923 last_digit_limit = INT64_MAX % base;
1927 digit = parsehex(**p);
1928 while (digit >= 0 && digit < base) {
1929 if (l > limit || (l == limit && digit > last_digit_limit))
1930 return (sign < 0) ? INT64_MIN : INT64_MAX;
1931 l = (l * base) + digit;
1932 digit = parsehex(*++(*p));
1934 return (sign < 0) ? -l : l;
1938 mtree_atol(char **p)
1941 return mtree_atol10(p);
1942 if ((*p)[1] == 'x' || (*p)[1] == 'X') {
1944 return mtree_atol16(p);
1946 return mtree_atol8(p);
1950 * Returns length of line (including trailing newline)
1951 * or negative on error. 'start' argument is updated to
1952 * point to first character of line.
1955 readline(struct archive_read *a, struct mtree *mtree, char **start,
1959 ssize_t total_size = 0;
1960 ssize_t find_off = 0;
1965 /* Accumulate line in a line buffer. */
1967 /* Read some more. */
1968 t = __archive_read_ahead(a, 1, &bytes_read);
1972 return (ARCHIVE_FATAL);
1973 nl = memchr(t, '\n', bytes_read);
1974 /* If we found '\n', trim the read to end exactly there. */
1976 bytes_read = ((const char *)nl) - ((const char *)t) + 1;
1978 if (total_size + bytes_read + 1 > limit) {
1979 archive_set_error(&a->archive,
1980 ARCHIVE_ERRNO_FILE_FORMAT,
1982 return (ARCHIVE_FATAL);
1984 if (archive_string_ensure(&mtree->line,
1985 total_size + bytes_read + 1) == NULL) {
1986 archive_set_error(&a->archive, ENOMEM,
1987 "Can't allocate working buffer");
1988 return (ARCHIVE_FATAL);
1990 /* Append new bytes to string. */
1991 memcpy(mtree->line.s + total_size, t, bytes_read);
1992 __archive_read_consume(a, bytes_read);
1993 total_size += bytes_read;
1994 mtree->line.s[total_size] = '\0';
1996 for (u = mtree->line.s + find_off; *u; ++u) {
1998 /* Ends with unescaped newline. */
1999 *start = mtree->line.s;
2001 } else if (u[0] == '#') {
2002 /* Ends with comment sequence #...\n */
2004 /* But we've not found the \n yet */
2007 } else if (u[0] == '\\') {
2009 /* Trim escaped newline. */
2011 mtree->line.s[total_size] = '\0';
2013 } else if (u[1] != '\0') {
2014 /* Skip the two-char escape sequence */
2019 find_off = u - mtree->line.s;
2026 /* A 32-bit version of Peter Weinberger's (PJW) hash algorithm,
2027 as used by ELF for hashing function names. */
2029 while (*p != '\0') {
2030 h = (h << 4) + *p++;
2031 if ((g = h & 0xF0000000) != 0) {