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
80 #define MAX_LINE_LEN (1024 * 1024)
83 struct mtree_option *next;
88 struct mtree_entry *next;
89 struct mtree_option *options;
93 unsigned int name_hash;
94 struct mtree_entry *hashtable_next;
98 struct archive_string line;
104 const char *archive_format_name;
105 struct mtree_entry *entries;
106 struct mtree_entry *this_entry;
107 struct mtree_entry *entry_hashtable[MTREE_HASHTABLE_SIZE];
108 struct archive_string current_dir;
109 struct archive_string contents_name;
111 struct archive_entry_linkresolver *resolver;
117 static int bid_keycmp(const char *, const char *, ssize_t);
118 static int cleanup(struct archive_read *);
119 static int detect_form(struct archive_read *, int *);
120 static unsigned int hash(const char *);
121 static int mtree_bid(struct archive_read *, int);
122 static int parse_file(struct archive_read *, struct archive_entry *,
123 struct mtree *, struct mtree_entry *, int *);
124 static void parse_escapes(char *, struct mtree_entry *);
125 static int parse_line(struct archive_read *, struct archive_entry *,
126 struct mtree *, struct mtree_entry *, int *);
127 static int parse_keyword(struct archive_read *, struct mtree *,
128 struct archive_entry *, struct mtree_option *, int *);
129 static int read_data(struct archive_read *a,
130 const void **buff, size_t *size, int64_t *offset);
131 static ssize_t readline(struct archive_read *, struct mtree *, char **, ssize_t);
132 static int skip(struct archive_read *a);
133 static int read_header(struct archive_read *,
134 struct archive_entry *);
135 static int64_t mtree_atol(char **, int base);
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;
340 * Place an arbitrary limit on the line length.
341 * mtree is almost free-form input and without line length limits,
342 * it can consume a lot of memory.
344 if (len >= MAX_LINE_LEN)
347 /* Increase reading bytes if it is not enough to at least
349 if (nbytes_req < (size_t)*ravail + 160)
352 *b = __archive_read_ahead(a, nbytes_req, avail);
354 if (*ravail >= *avail)
356 /* Reading bytes reaches the end of file. */
357 *b = __archive_read_ahead(a, *avail, avail);
363 tested = len;/* Skip some bytes we already determinated. */
364 len = get_line_size(*b + len, *avail - len, nl);
372 * Compare characters with a mtree keyword.
373 * Returns the length of a mtree keyword if matched.
374 * Returns 0 if not matched.
377 bid_keycmp(const char *p, const char *key, ssize_t len)
381 while (len > 0 && *p && *key) {
389 return (0);/* Not match */
392 return (0);/* Not match */
394 /* A following character should be specified characters */
395 if (p[0] == '=' || p[0] == ' ' || p[0] == '\t' ||
396 p[0] == '\n' || p[0] == '\r' ||
397 (p[0] == '\\' && (p[1] == '\n' || p[1] == '\r')))
399 return (0);/* Not match */
403 * Test whether the characters 'p' has is mtree keyword.
404 * Returns the length of a detected keyword.
405 * Returns 0 if any keywords were not found.
408 bid_keyword(const char *p, ssize_t len)
410 static const char * const keys_c[] = {
411 "content", "contents", "cksum", NULL
413 static const char * const keys_df[] = {
414 "device", "flags", NULL
416 static const char * const keys_g[] = {
419 static const char * const keys_il[] = {
420 "ignore", "inode", "link", NULL
422 static const char * const keys_m[] = {
423 "md5", "md5digest", "mode", NULL
425 static const char * const keys_no[] = {
426 "nlink", "nochange", "optional", NULL
428 static const char * const keys_r[] = {
429 "resdevice", "rmd160", "rmd160digest", NULL
431 static const char * const keys_s[] = {
432 "sha1", "sha1digest",
433 "sha256", "sha256digest",
434 "sha384", "sha384digest",
435 "sha512", "sha512digest",
438 static const char * const keys_t[] = {
439 "tags", "time", "type", NULL
441 static const char * const keys_u[] = {
444 const char * const *keys;
448 case 'c': keys = keys_c; break;
449 case 'd': case 'f': keys = keys_df; break;
450 case 'g': keys = keys_g; break;
451 case 'i': case 'l': keys = keys_il; break;
452 case 'm': keys = keys_m; break;
453 case 'n': case 'o': keys = keys_no; break;
454 case 'r': keys = keys_r; break;
455 case 's': keys = keys_s; break;
456 case 't': keys = keys_t; break;
457 case 'u': keys = keys_u; break;
458 default: return (0);/* Unknown key */
461 for (i = 0; keys[i] != NULL; i++) {
462 int l = bid_keycmp(p, keys[i], len);
466 return (0);/* Unknown key */
470 * Test whether there is a set of mtree keywords.
471 * Returns the number of keyword.
472 * Returns -1 if we got incorrect sequence.
473 * This function expects a set of "<space characters>keyword=value".
474 * When "unset" is specified, expects a set of "<space characters>keyword".
477 bid_keyword_list(const char *p, ssize_t len, int unset, int last_is_path)
482 while (len > 0 && *p) {
485 /* Test whether there are blank characters in the line. */
486 while (len >0 && (*p == ' ' || *p == '\t')) {
491 if (*p == '\n' || *p == '\r')
493 if (p[0] == '\\' && (p[1] == '\n' || p[1] == '\r'))
495 if (!blank && !last_is_path) /* No blank character. */
497 if (last_is_path && len == 0)
501 l = bid_keycmp(p, "all", len);
505 /* Test whether there is a correct key in the line. */
506 l = bid_keyword(p, len);
508 return (-1);/* Unknown keyword was found. */
518 while (len > 0 && *p != ' ' && *p != '\t') {
523 /* A keyword should have a its value unless
524 * "/unset" operation. */
525 if (!unset && value == 0)
533 bid_entry(const char *p, ssize_t len, ssize_t nl, int *last_is_path)
536 static const unsigned char safe_char[256] = {
537 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 00 - 0F */
538 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 1F */
539 /* !"$%&'()*+,-./ EXCLUSION:( )(#) */
540 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 - 2F */
541 /* 0123456789:;<>? EXCLUSION:(=) */
542 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, /* 30 - 3F */
543 /* @ABCDEFGHIJKLMNO */
544 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 40 - 4F */
545 /* PQRSTUVWXYZ[\]^_ */
546 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 50 - 5F */
547 /* `abcdefghijklmno */
548 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 60 - 6F */
549 /* pqrstuvwxyz{|}~ */
550 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, /* 70 - 7F */
551 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
552 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
553 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
554 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
555 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
556 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
557 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
558 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
562 const char * const pp_end = pp + len;
566 * Skip the path-name which is quoted.
568 for (;pp < pp_end; ++pp) {
569 if (!safe_char[*(const unsigned char *)pp]) {
570 if (*pp != ' ' && *pp != '\t' && *pp != '\r'
579 /* If a path-name was not found at the first, try to check
580 * a mtree format(a.k.a form D) ``NetBSD's mtree -D'' creates,
581 * which places the path-name at the last. */
583 const char *pb = p + len - nl;
587 /* The form D accepts only a single line for an entry. */
589 pb[-1] == '\\' && (pb[-2] == ' ' || pb[-2] == '\t'))
591 if (pb-1 >= p && pb[-1] == '\\')
595 while (p <= --pb && *pb != ' ' && *pb != '\t') {
596 if (!safe_char[*(const unsigned char *)pb])
599 /* The pathname should have a slash in this
604 if (name_len == 0 || slash == 0)
606 /* If '/' is placed at the first in this field, this is not
607 * a valid filename. */
610 ll = len - nl - name_len;
615 return (bid_keyword_list(pp, ll, 0, *last_is_path));
618 #define MAX_BID_ENTRY 3
621 mtree_bid(struct archive_read *a, int best_bid)
623 const char *signature = "#mtree";
626 (void)best_bid; /* UNUSED */
628 /* Now let's look at the actual header and see if it matches. */
629 p = __archive_read_ahead(a, strlen(signature), NULL);
633 if (memcmp(p, signature, strlen(signature)) == 0)
634 return (8 * (int)strlen(signature));
637 * There is not a mtree signature. Let's try to detect mtree format.
639 return (detect_form(a, NULL));
643 detect_form(struct archive_read *a, int *is_form_d)
646 ssize_t avail, ravail;
647 ssize_t detected_bytes = 0, len, nl;
648 int entry_cnt = 0, multiline = 0;
649 int form_D = 0;/* The archive is generated by `NetBSD mtree -D'
650 * (In this source we call it `form D') . */
652 if (is_form_d != NULL)
654 p = __archive_read_ahead(a, 1, &avail);
659 len = next_line(a, &p, &avail, &ravail, &nl);
660 /* The terminal character of the line should be
661 * a new line character, '\r\n' or '\n'. */
662 if (len <= 0 || nl == 0)
665 /* Leading whitespace is never significant,
667 while (len > 0 && (*p == ' ' || *p == '\t')) {
672 /* Skip comment or empty line. */
673 if (p[0] == '#' || p[0] == '\n' || p[0] == '\r') {
679 /* A continuance line; the terminal
680 * character of previous line was '\' character. */
681 if (bid_keyword_list(p, len, 0, 0) <= 0)
684 detected_bytes += len;
685 if (p[len-nl-1] != '\\') {
686 if (multiline == 1 &&
687 ++entry_cnt >= MAX_BID_ENTRY)
696 int last_is_path, keywords;
698 keywords = bid_entry(p, len, nl, &last_is_path);
700 detected_bytes += len;
704 else if (keywords > 0)
705 /* This line is not `form D'. */
707 } else if (form_D == 1) {
708 if (!last_is_path && keywords > 0)
709 /* This this is not `form D'
710 * and We cannot accept mixed
714 if (!last_is_path && p[len-nl-1] == '\\')
715 /* This line continues. */
718 /* We've got plenty of correct lines
719 * to assume that this file is a mtree
721 if (++entry_cnt >= MAX_BID_ENTRY)
726 } else if (len > 4 && strncmp(p, "/set", 4) == 0) {
727 if (bid_keyword_list(p+4, len-4, 0, 0) <= 0)
729 /* This line continues. */
730 if (p[len-nl-1] == '\\')
732 } else if (len > 6 && strncmp(p, "/unset", 6) == 0) {
733 if (bid_keyword_list(p+6, len-6, 1, 0) <= 0)
735 /* This line continues. */
736 if (p[len-nl-1] == '\\')
741 /* Test next line. */
745 if (entry_cnt >= MAX_BID_ENTRY || (entry_cnt > 0 && len == 0)) {
746 if (is_form_d != NULL) {
757 * The extended mtree format permits multiple lines specifying
758 * attributes for each file. For those entries, only the last line
759 * is actually used. Practically speaking, that means we have
760 * to read the entire mtree file into memory up front.
762 * The parsing is done in two steps. First, it is decided if a line
763 * changes the global defaults and if it is, processed accordingly.
764 * Otherwise, the options of the line are merged with the current
768 add_option(struct archive_read *a, struct mtree_option **global,
769 const char *value, size_t len)
771 struct mtree_option *opt;
773 if ((opt = malloc(sizeof(*opt))) == NULL) {
774 archive_set_error(&a->archive, errno, "Can't allocate memory");
775 return (ARCHIVE_FATAL);
777 if ((opt->value = malloc(len + 1)) == NULL) {
779 archive_set_error(&a->archive, errno, "Can't allocate memory");
780 return (ARCHIVE_FATAL);
782 memcpy(opt->value, value, len);
783 opt->value[len] = '\0';
790 remove_option(struct mtree_option **global, const char *value, size_t len)
792 struct mtree_option *iter, *last;
795 for (iter = *global; iter != NULL; last = iter, iter = iter->next) {
796 if (strncmp(iter->value, value, len) == 0 &&
797 (iter->value[len] == '\0' ||
798 iter->value[len] == '='))
804 *global = iter->next;
806 last->next = iter->next;
813 process_global_set(struct archive_read *a,
814 struct mtree_option **global, const char *line)
816 const char *next, *eq;
822 next = line + strspn(line, " \t\r\n");
826 next = line + strcspn(line, " \t\r\n");
827 eq = strchr(line, '=');
833 remove_option(global, line, len);
834 r = add_option(a, global, line, next - line);
842 process_global_unset(struct archive_read *a,
843 struct mtree_option **global, const char *line)
849 if (strchr(line, '=') != NULL) {
850 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
851 "/unset shall not contain `='");
852 return ARCHIVE_FATAL;
856 next = line + strspn(line, " \t\r\n");
860 len = strcspn(line, " \t\r\n");
862 if (len == 3 && strncmp(line, "all", 3) == 0) {
863 free_options(*global);
866 remove_option(global, line, len);
874 process_add_entry(struct archive_read *a, struct mtree *mtree,
875 struct mtree_option **global, const char *line, ssize_t line_len,
876 struct mtree_entry **last_entry, int is_form_d)
878 struct mtree_entry *entry, *ht_iter;
879 struct mtree_option *iter;
880 const char *next, *eq, *name, *end;
881 size_t name_len, len;
885 if ((entry = malloc(sizeof(*entry))) == NULL) {
886 archive_set_error(&a->archive, errno, "Can't allocate memory");
887 return (ARCHIVE_FATAL);
890 entry->options = NULL;
894 entry->name_hash = 0;
895 entry->hashtable_next = NULL;
897 /* Add this entry to list. */
898 if (*last_entry == NULL)
899 mtree->entries = entry;
901 (*last_entry)->next = entry;
905 /* Filename is last item on line. */
906 /* Adjust line_len to trim trailing whitespace */
907 while (line_len > 0) {
908 char last_character = line[line_len - 1];
909 if (last_character == '\r'
910 || last_character == '\n'
911 || last_character == '\t'
912 || last_character == ' ') {
918 /* Name starts after the last whitespace separator */
920 for (i = 0; i < line_len; i++) {
928 name_len = line + line_len - name;
931 /* Filename is first item on line */
932 name_len = strcspn(line, " \t\r\n");
935 end = line + line_len;
937 /* name/name_len is the name within the line. */
938 /* line..end brackets the entire line except the name */
940 if ((entry->name = malloc(name_len + 1)) == NULL) {
941 archive_set_error(&a->archive, errno, "Can't allocate memory");
942 return (ARCHIVE_FATAL);
945 memcpy(entry->name, name, name_len);
946 entry->name[name_len] = '\0';
947 parse_escapes(entry->name, entry);
948 entry->name_hash = hash(entry->name);
950 ht_idx = entry->name_hash % MTREE_HASHTABLE_SIZE;
951 if ((ht_iter = mtree->entry_hashtable[ht_idx]) != NULL) {
952 while (ht_iter->hashtable_next)
953 ht_iter = ht_iter->hashtable_next;
954 ht_iter->hashtable_next = entry;
956 mtree->entry_hashtable[ht_idx] = entry;
959 for (iter = *global; iter != NULL; iter = iter->next) {
960 r = add_option(a, &entry->options, iter->value,
961 strlen(iter->value));
967 next = line + strspn(line, " \t\r\n");
973 next = line + strcspn(line, " \t\r\n");
974 eq = strchr(line, '=');
975 if (eq == NULL || eq > next)
980 remove_option(&entry->options, line, len);
981 r = add_option(a, &entry->options, line, next - line);
989 read_mtree(struct archive_read *a, struct mtree *mtree)
994 struct mtree_option *global;
995 struct mtree_entry *last_entry;
998 mtree->archive_format = ARCHIVE_FORMAT_MTREE;
999 mtree->archive_format_name = "mtree";
1004 (void)detect_form(a, &is_form_d);
1006 for (counter = 1; ; ++counter) {
1007 len = readline(a, mtree, &p, 65536);
1009 mtree->this_entry = mtree->entries;
1010 free_options(global);
1011 return (ARCHIVE_OK);
1014 free_options(global);
1017 /* Leading whitespace is never significant, ignore it. */
1018 while (*p == ' ' || *p == '\t') {
1022 /* Skip content lines and blank lines. */
1025 if (*p == '\r' || *p == '\n' || *p == '\0')
1028 r = process_add_entry(a, mtree, &global, p, len,
1029 &last_entry, is_form_d);
1030 } else if (len > 4 && strncmp(p, "/set", 4) == 0) {
1031 if (p[4] != ' ' && p[4] != '\t')
1033 r = process_global_set(a, &global, p);
1034 } else if (len > 6 && strncmp(p, "/unset", 6) == 0) {
1035 if (p[6] != ' ' && p[6] != '\t')
1037 r = process_global_unset(a, &global, p);
1041 if (r != ARCHIVE_OK) {
1042 free_options(global);
1047 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1048 "Can't parse line %ju", counter);
1049 free_options(global);
1050 return (ARCHIVE_FATAL);
1054 * Read in the entire mtree file into memory on the first request.
1055 * Then use the next unused file to satisfy each header request.
1058 read_header(struct archive_read *a, struct archive_entry *entry)
1060 struct mtree *mtree;
1064 mtree = (struct mtree *)(a->format->data);
1066 if (mtree->fd >= 0) {
1071 if (mtree->entries == NULL) {
1072 mtree->resolver = archive_entry_linkresolver_new();
1073 if (mtree->resolver == NULL)
1074 return ARCHIVE_FATAL;
1075 archive_entry_linkresolver_set_strategy(mtree->resolver,
1076 ARCHIVE_FORMAT_MTREE);
1077 r = read_mtree(a, mtree);
1078 if (r != ARCHIVE_OK)
1082 a->archive.archive_format = mtree->archive_format;
1083 a->archive.archive_format_name = mtree->archive_format_name;
1086 if (mtree->this_entry == NULL)
1087 return (ARCHIVE_EOF);
1088 if (strcmp(mtree->this_entry->name, "..") == 0) {
1089 mtree->this_entry->used = 1;
1090 if (archive_strlen(&mtree->current_dir) > 0) {
1091 /* Roll back current path. */
1092 p = mtree->current_dir.s
1093 + mtree->current_dir.length - 1;
1094 while (p >= mtree->current_dir.s && *p != '/')
1096 if (p >= mtree->current_dir.s)
1098 mtree->current_dir.length
1099 = p - mtree->current_dir.s + 1;
1102 if (!mtree->this_entry->used) {
1104 r = parse_file(a, entry, mtree, mtree->this_entry,
1109 mtree->this_entry = mtree->this_entry->next;
1114 * A single file can have multiple lines contribute specifications.
1115 * Parse as many lines as necessary, then pull additional information
1116 * from a backing file on disk as necessary.
1119 parse_file(struct archive_read *a, struct archive_entry *entry,
1120 struct mtree *mtree, struct mtree_entry *mentry, int *use_next)
1123 struct stat st_storage, *st;
1124 struct mtree_entry *mp;
1125 struct archive_entry *sparse_entry;
1126 int r = ARCHIVE_OK, r1, parsed_kws;
1130 /* Initialize reasonable defaults. */
1131 archive_entry_set_filetype(entry, AE_IFREG);
1132 archive_entry_set_size(entry, 0);
1133 archive_string_empty(&mtree->contents_name);
1135 /* Parse options from this line. */
1137 r = parse_line(a, entry, mtree, mentry, &parsed_kws);
1140 archive_entry_copy_pathname(entry, mentry->name);
1142 * "Full" entries are allowed to have multiple lines
1143 * and those lines aren't required to be adjacent. We
1144 * don't support multiple lines for "relative" entries
1145 * nor do we make any attempt to merge data from
1146 * separate "relative" and "full" entries. (Merging
1147 * "relative" and "full" entries would require dealing
1148 * with pathname canonicalization, which is a very
1151 for (mp = mentry->hashtable_next; mp != NULL; mp = mp->hashtable_next) {
1152 if (mp->full && !mp->used
1153 && mentry->name_hash == mp->name_hash
1154 && strcmp(mentry->name, mp->name) == 0) {
1155 /* Later lines override earlier ones. */
1157 r1 = parse_line(a, entry, mtree, mp,
1165 * Relative entries require us to construct
1166 * the full path and possibly update the
1167 * current directory.
1169 size_t n = archive_strlen(&mtree->current_dir);
1171 archive_strcat(&mtree->current_dir, "/");
1172 archive_strcat(&mtree->current_dir, mentry->name);
1173 archive_entry_copy_pathname(entry, mtree->current_dir.s);
1174 if (archive_entry_filetype(entry) != AE_IFDIR)
1175 mtree->current_dir.length = n;
1178 if (mtree->checkfs) {
1180 * Try to open and stat the file to get the real size
1181 * and other file info. It would be nice to avoid
1182 * this here so that getting a listing of an mtree
1183 * wouldn't require opening every referenced contents
1184 * file. But then we wouldn't know the actual
1185 * contents size, so I don't see a really viable way
1186 * around this. (Also, we may want to someday pull
1187 * other unspecified info from the contents file on
1191 if (archive_strlen(&mtree->contents_name) > 0)
1192 path = mtree->contents_name.s;
1194 path = archive_entry_pathname(entry);
1196 if (archive_entry_filetype(entry) == AE_IFREG ||
1197 archive_entry_filetype(entry) == AE_IFDIR) {
1198 mtree->fd = open(path, O_RDONLY | O_BINARY | O_CLOEXEC);
1199 __archive_ensure_cloexec_flag(mtree->fd);
1200 if (mtree->fd == -1 &&
1202 archive_strlen(&mtree->contents_name) > 0)) {
1203 archive_set_error(&a->archive, errno,
1204 "Can't open %s", path);
1210 if (mtree->fd >= 0) {
1211 if (fstat(mtree->fd, st) == -1) {
1212 archive_set_error(&a->archive, errno,
1213 "Could not fstat %s", path);
1215 /* If we can't stat it, don't keep it open. */
1220 } else if (lstat(path, st) == -1) {
1225 * Check for a mismatch between the type in the specification
1226 * and the type of the contents object on disk.
1229 if (((st->st_mode & S_IFMT) == S_IFREG &&
1230 archive_entry_filetype(entry) == AE_IFREG)
1232 ||((st->st_mode & S_IFMT) == S_IFLNK &&
1233 archive_entry_filetype(entry) == AE_IFLNK)
1236 ||((st->st_mode & S_IFSOCK) == S_IFSOCK &&
1237 archive_entry_filetype(entry) == AE_IFSOCK)
1240 ||((st->st_mode & S_IFMT) == S_IFCHR &&
1241 archive_entry_filetype(entry) == AE_IFCHR)
1244 ||((st->st_mode & S_IFMT) == S_IFBLK &&
1245 archive_entry_filetype(entry) == AE_IFBLK)
1247 ||((st->st_mode & S_IFMT) == S_IFDIR &&
1248 archive_entry_filetype(entry) == AE_IFDIR)
1250 ||((st->st_mode & S_IFMT) == S_IFIFO &&
1251 archive_entry_filetype(entry) == AE_IFIFO)
1256 /* Types don't match; bail out gracefully. */
1260 if (parsed_kws & MTREE_HAS_OPTIONAL) {
1261 /* It's not an error for an optional
1262 * entry to not match disk. */
1264 } else if (r == ARCHIVE_OK) {
1265 archive_set_error(&a->archive,
1267 "mtree specification has different"
1269 archive_entry_pathname(entry));
1277 * If there is a contents file on disk, pick some of the
1278 * metadata from that file. For most of these, we only
1279 * set it from the contents if it wasn't already parsed
1280 * from the specification.
1283 if (((parsed_kws & MTREE_HAS_DEVICE) == 0 ||
1284 (parsed_kws & MTREE_HAS_NOCHANGE) != 0) &&
1285 (archive_entry_filetype(entry) == AE_IFCHR ||
1286 archive_entry_filetype(entry) == AE_IFBLK))
1287 archive_entry_set_rdev(entry, st->st_rdev);
1288 if ((parsed_kws & (MTREE_HAS_GID | MTREE_HAS_GNAME))
1290 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1291 archive_entry_set_gid(entry, st->st_gid);
1292 if ((parsed_kws & (MTREE_HAS_UID | MTREE_HAS_UNAME))
1294 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1295 archive_entry_set_uid(entry, st->st_uid);
1296 if ((parsed_kws & MTREE_HAS_MTIME) == 0 ||
1297 (parsed_kws & MTREE_HAS_NOCHANGE) != 0) {
1298 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
1299 archive_entry_set_mtime(entry, st->st_mtime,
1300 st->st_mtimespec.tv_nsec);
1301 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
1302 archive_entry_set_mtime(entry, st->st_mtime,
1303 st->st_mtim.tv_nsec);
1304 #elif HAVE_STRUCT_STAT_ST_MTIME_N
1305 archive_entry_set_mtime(entry, st->st_mtime,
1307 #elif HAVE_STRUCT_STAT_ST_UMTIME
1308 archive_entry_set_mtime(entry, st->st_mtime,
1309 st->st_umtime*1000);
1310 #elif HAVE_STRUCT_STAT_ST_MTIME_USEC
1311 archive_entry_set_mtime(entry, st->st_mtime,
1312 st->st_mtime_usec*1000);
1314 archive_entry_set_mtime(entry, st->st_mtime, 0);
1317 if ((parsed_kws & MTREE_HAS_NLINK) == 0 ||
1318 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1319 archive_entry_set_nlink(entry, st->st_nlink);
1320 if ((parsed_kws & MTREE_HAS_PERM) == 0 ||
1321 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1322 archive_entry_set_perm(entry, st->st_mode);
1323 if ((parsed_kws & MTREE_HAS_SIZE) == 0 ||
1324 (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1325 archive_entry_set_size(entry, st->st_size);
1326 archive_entry_set_ino(entry, st->st_ino);
1327 archive_entry_set_dev(entry, st->st_dev);
1329 archive_entry_linkify(mtree->resolver, &entry,
1331 } else if (parsed_kws & MTREE_HAS_OPTIONAL) {
1333 * Couldn't open the entry, stat it or the on-disk type
1334 * didn't match. If this entry is optional, just
1335 * ignore it and read the next header entry.
1342 mtree->cur_size = archive_entry_size(entry);
1349 * Each line contains a sequence of keywords.
1352 parse_line(struct archive_read *a, struct archive_entry *entry,
1353 struct mtree *mtree, struct mtree_entry *mp, int *parsed_kws)
1355 struct mtree_option *iter;
1356 int r = ARCHIVE_OK, r1;
1358 for (iter = mp->options; iter != NULL; iter = iter->next) {
1359 r1 = parse_keyword(a, mtree, entry, iter, parsed_kws);
1363 if (r == ARCHIVE_OK && (*parsed_kws & MTREE_HAS_TYPE) == 0) {
1364 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1365 "Missing type keyword in mtree specification");
1366 return (ARCHIVE_WARN);
1372 * Device entries have one of the following forms:
1374 * - format,major,minor[,subdevice]
1375 * When parsing succeeded, `pdev' will contain the appropriate dev_t value.
1378 /* strsep() is not in C90, but strcspn() is. */
1379 /* Taken from http://unixpapa.com/incnote/string.html */
1381 la_strsep(char **sp, const char *sep)
1384 if (sp == NULL || *sp == NULL || **sp == '\0')
1387 p = s + strcspn(s, sep);
1395 parse_device(dev_t *pdev, struct archive *a, char *val)
1397 #define MAX_PACK_ARGS 3
1398 unsigned long numbers[MAX_PACK_ARGS];
1403 const char *error = NULL;
1405 memset(pdev, 0, sizeof(*pdev));
1406 if ((dev = strchr(val, ',')) != NULL) {
1408 * Device's major/minor are given in a specified format.
1409 * Decode and pack it accordingly.
1412 if ((pack = pack_find(val)) == NULL) {
1413 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1414 "Unknown format `%s'", val);
1415 return ARCHIVE_WARN;
1418 while ((p = la_strsep(&dev, ",")) != NULL) {
1420 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1422 return ARCHIVE_WARN;
1424 if (argc >= MAX_PACK_ARGS) {
1425 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1426 "Too many arguments");
1427 return ARCHIVE_WARN;
1429 numbers[argc++] = (unsigned long)mtree_atol(&p, 0);
1432 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1433 "Not enough arguments");
1434 return ARCHIVE_WARN;
1436 result = (*pack)(argc, numbers, &error);
1437 if (error != NULL) {
1438 archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1440 return ARCHIVE_WARN;
1443 /* file system raw value. */
1444 result = (dev_t)mtree_atol(&val, 0);
1448 #undef MAX_PACK_ARGS
1452 * Parse a single keyword and its value.
1455 parse_keyword(struct archive_read *a, struct mtree *mtree,
1456 struct archive_entry *entry, struct mtree_option *opt, int *parsed_kws)
1463 return (ARCHIVE_OK);
1465 if (strcmp(key, "nochange") == 0) {
1466 *parsed_kws |= MTREE_HAS_NOCHANGE;
1467 return (ARCHIVE_OK);
1469 if (strcmp(key, "optional") == 0) {
1470 *parsed_kws |= MTREE_HAS_OPTIONAL;
1471 return (ARCHIVE_OK);
1473 if (strcmp(key, "ignore") == 0) {
1475 * The mtree processing is not recursive, so
1476 * recursion will only happen for explicitly listed
1479 return (ARCHIVE_OK);
1482 val = strchr(key, '=');
1484 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1485 "Malformed attribute \"%s\" (%d)", key, key[0]);
1486 return (ARCHIVE_WARN);
1494 if (strcmp(key, "content") == 0
1495 || strcmp(key, "contents") == 0) {
1496 parse_escapes(val, NULL);
1497 archive_strcpy(&mtree->contents_name, val);
1500 if (strcmp(key, "cksum") == 0)
1503 if (strcmp(key, "device") == 0) {
1504 /* stat(2) st_rdev field, e.g. the major/minor IDs
1505 * of a char/block special file */
1509 *parsed_kws |= MTREE_HAS_DEVICE;
1510 r = parse_device(&dev, &a->archive, val);
1511 if (r == ARCHIVE_OK)
1512 archive_entry_set_rdev(entry, dev);
1516 if (strcmp(key, "flags") == 0) {
1517 *parsed_kws |= MTREE_HAS_FFLAGS;
1518 archive_entry_copy_fflags_text(entry, val);
1522 if (strcmp(key, "gid") == 0) {
1523 *parsed_kws |= MTREE_HAS_GID;
1524 archive_entry_set_gid(entry, mtree_atol(&val, 10));
1527 if (strcmp(key, "gname") == 0) {
1528 *parsed_kws |= MTREE_HAS_GNAME;
1529 archive_entry_copy_gname(entry, val);
1533 if (strcmp(key, "inode") == 0) {
1534 archive_entry_set_ino(entry, mtree_atol(&val, 10));
1538 if (strcmp(key, "link") == 0) {
1539 archive_entry_copy_symlink(entry, val);
1543 if (strcmp(key, "md5") == 0 || strcmp(key, "md5digest") == 0)
1545 if (strcmp(key, "mode") == 0) {
1546 if (val[0] >= '0' && val[0] <= '7') {
1547 *parsed_kws |= MTREE_HAS_PERM;
1548 archive_entry_set_perm(entry,
1549 (mode_t)mtree_atol(&val, 8));
1551 archive_set_error(&a->archive,
1552 ARCHIVE_ERRNO_FILE_FORMAT,
1553 "Symbolic or non-octal mode \"%s\" unsupported", val);
1554 return ARCHIVE_WARN;
1559 if (strcmp(key, "nlink") == 0) {
1560 *parsed_kws |= MTREE_HAS_NLINK;
1561 archive_entry_set_nlink(entry,
1562 (unsigned int)mtree_atol(&val, 10));
1566 if (strcmp(key, "resdevice") == 0) {
1567 /* stat(2) st_dev field, e.g. the device ID where the
1572 r = parse_device(&dev, &a->archive, val);
1573 if (r == ARCHIVE_OK)
1574 archive_entry_set_dev(entry, dev);
1577 if (strcmp(key, "rmd160") == 0 ||
1578 strcmp(key, "rmd160digest") == 0)
1581 if (strcmp(key, "sha1") == 0 || strcmp(key, "sha1digest") == 0)
1583 if (strcmp(key, "sha256") == 0 ||
1584 strcmp(key, "sha256digest") == 0)
1586 if (strcmp(key, "sha384") == 0 ||
1587 strcmp(key, "sha384digest") == 0)
1589 if (strcmp(key, "sha512") == 0 ||
1590 strcmp(key, "sha512digest") == 0)
1592 if (strcmp(key, "size") == 0) {
1593 archive_entry_set_size(entry, mtree_atol(&val, 10));
1597 if (strcmp(key, "tags") == 0) {
1599 * Comma delimited list of tags.
1600 * Ignore the tags for now, but the interface
1601 * should be extended to allow inclusion/exclusion.
1605 if (strcmp(key, "time") == 0) {
1607 int64_t my_time_t_max = get_time_t_max();
1608 int64_t my_time_t_min = get_time_t_min();
1611 *parsed_kws |= MTREE_HAS_MTIME;
1612 m = mtree_atol(&val, 10);
1613 /* Replicate an old mtree bug:
1614 * 123456789.1 represents 123456789
1615 * seconds and 1 nanosecond. */
1618 ns = (long)mtree_atol(&val, 10);
1621 else if (ns > 999999999)
1624 if (m > my_time_t_max)
1626 else if (m < my_time_t_min)
1628 archive_entry_set_mtime(entry, (time_t)m, ns);
1631 if (strcmp(key, "type") == 0) {
1634 if (strcmp(val, "block") == 0) {
1635 archive_entry_set_filetype(entry, AE_IFBLK);
1639 if (strcmp(val, "char") == 0) {
1640 archive_entry_set_filetype(entry,
1645 if (strcmp(val, "dir") == 0) {
1646 archive_entry_set_filetype(entry,
1651 if (strcmp(val, "fifo") == 0) {
1652 archive_entry_set_filetype(entry,
1656 if (strcmp(val, "file") == 0) {
1657 archive_entry_set_filetype(entry,
1662 if (strcmp(val, "link") == 0) {
1663 archive_entry_set_filetype(entry,
1668 archive_set_error(&a->archive,
1669 ARCHIVE_ERRNO_FILE_FORMAT,
1670 "Unrecognized file type \"%s\"; "
1671 "assuming \"file\"", val);
1672 archive_entry_set_filetype(entry, AE_IFREG);
1673 return (ARCHIVE_WARN);
1675 *parsed_kws |= MTREE_HAS_TYPE;
1679 if (strcmp(key, "uid") == 0) {
1680 *parsed_kws |= MTREE_HAS_UID;
1681 archive_entry_set_uid(entry, mtree_atol(&val, 10));
1684 if (strcmp(key, "uname") == 0) {
1685 *parsed_kws |= MTREE_HAS_UNAME;
1686 archive_entry_copy_uname(entry, val);
1690 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1691 "Unrecognized key %s=%s", key, val);
1692 return (ARCHIVE_WARN);
1694 return (ARCHIVE_OK);
1698 read_data(struct archive_read *a, const void **buff, size_t *size,
1701 size_t bytes_to_read;
1703 struct mtree *mtree;
1705 mtree = (struct mtree *)(a->format->data);
1706 if (mtree->fd < 0) {
1710 return (ARCHIVE_EOF);
1712 if (mtree->buff == NULL) {
1713 mtree->buffsize = 64 * 1024;
1714 mtree->buff = malloc(mtree->buffsize);
1715 if (mtree->buff == NULL) {
1716 archive_set_error(&a->archive, ENOMEM,
1717 "Can't allocate memory");
1718 return (ARCHIVE_FATAL);
1722 *buff = mtree->buff;
1723 *offset = mtree->offset;
1724 if ((int64_t)mtree->buffsize > mtree->cur_size - mtree->offset)
1725 bytes_to_read = (size_t)(mtree->cur_size - mtree->offset);
1727 bytes_to_read = mtree->buffsize;
1728 bytes_read = read(mtree->fd, mtree->buff, bytes_to_read);
1729 if (bytes_read < 0) {
1730 archive_set_error(&a->archive, errno, "Can't read");
1731 return (ARCHIVE_WARN);
1733 if (bytes_read == 0) {
1735 return (ARCHIVE_EOF);
1737 mtree->offset += bytes_read;
1739 return (ARCHIVE_OK);
1742 /* Skip does nothing except possibly close the contents file. */
1744 skip(struct archive_read *a)
1746 struct mtree *mtree;
1748 mtree = (struct mtree *)(a->format->data);
1749 if (mtree->fd >= 0) {
1753 return (ARCHIVE_OK);
1757 * Since parsing backslash sequences always makes strings shorter,
1758 * we can always do this conversion in-place.
1761 parse_escapes(char *src, struct mtree_entry *mentry)
1766 if (mentry != NULL && strcmp(src, ".") == 0)
1769 while (*src != '\0') {
1771 if (c == '/' && mentry != NULL)
1776 if (src[1] < '0' || src[1] > '7') {
1785 if (src[1] >= '0' && src[1] <= '7' &&
1786 src[2] >= '0' && src[2] <= '7') {
1787 c = (src[0] - '0') << 6;
1788 c |= (src[1] - '0') << 3;
1789 c |= (src[2] - '0');
1836 /* Parse a hex digit. */
1840 if (c >= '0' && c <= '9')
1842 else if (c >= 'a' && c <= 'f')
1844 else if (c >= 'A' && c <= 'F')
1851 * Note that this implementation does not (and should not!) obey
1852 * locale settings; you cannot simply substitute strtol here, since
1853 * it does obey locale.
1856 mtree_atol(char **p, int base)
1859 int digit, last_digit_limit;
1864 else if ((*p)[1] == 'x' || (*p)[1] == 'X') {
1873 limit = INT64_MIN / base;
1874 last_digit_limit = INT64_MIN % base;
1878 digit = parsedigit(**p);
1879 while (digit >= 0 && digit < base) {
1880 if (l < limit || (l == limit && digit > last_digit_limit))
1882 l = (l * base) - digit;
1883 digit = parsedigit(*++(*p));
1887 limit = INT64_MAX / base;
1888 last_digit_limit = INT64_MAX % base;
1891 digit = parsedigit(**p);
1892 while (digit >= 0 && digit < base) {
1893 if (l > limit || (l == limit && digit > last_digit_limit))
1895 l = (l * base) + digit;
1896 digit = parsedigit(*++(*p));
1903 * Returns length of line (including trailing newline)
1904 * or negative on error. 'start' argument is updated to
1905 * point to first character of line.
1908 readline(struct archive_read *a, struct mtree *mtree, char **start,
1912 ssize_t total_size = 0;
1913 ssize_t find_off = 0;
1918 /* Accumulate line in a line buffer. */
1920 /* Read some more. */
1921 t = __archive_read_ahead(a, 1, &bytes_read);
1925 return (ARCHIVE_FATAL);
1926 nl = memchr(t, '\n', bytes_read);
1927 /* If we found '\n', trim the read to end exactly there. */
1929 bytes_read = ((const char *)nl) - ((const char *)t) + 1;
1931 if (total_size + bytes_read + 1 > limit) {
1932 archive_set_error(&a->archive,
1933 ARCHIVE_ERRNO_FILE_FORMAT,
1935 return (ARCHIVE_FATAL);
1937 if (archive_string_ensure(&mtree->line,
1938 total_size + bytes_read + 1) == NULL) {
1939 archive_set_error(&a->archive, ENOMEM,
1940 "Can't allocate working buffer");
1941 return (ARCHIVE_FATAL);
1943 /* Append new bytes to string. */
1944 memcpy(mtree->line.s + total_size, t, bytes_read);
1945 __archive_read_consume(a, bytes_read);
1946 total_size += bytes_read;
1947 mtree->line.s[total_size] = '\0';
1949 for (u = mtree->line.s + find_off; *u; ++u) {
1951 /* Ends with unescaped newline. */
1952 *start = mtree->line.s;
1954 } else if (u[0] == '#') {
1955 /* Ends with comment sequence #...\n */
1957 /* But we've not found the \n yet */
1960 } else if (u[0] == '\\') {
1962 /* Trim escaped newline. */
1964 mtree->line.s[total_size] = '\0';
1966 } else if (u[1] != '\0') {
1967 /* Skip the two-char escape sequence */
1972 find_off = u - mtree->line.s;
1979 /* A 32-bit version of Peter Weinberger's (PJW) hash algorithm,
1980 as used by ELF for hashing function names. */
1982 while (*p != '\0') {
1983 h = (h << 4) + *p++;
1984 if ((g = h & 0xF0000000) != 0) {