xref: /freebsd/contrib/libarchive/libarchive/archive_read_support_format_mtree.c (revision 185becb1e1bd2657c156f78aeb52edac05ba5fb5)
1 /*-
2  * Copyright (c) 2003-2007 Tim Kientzle
3  * Copyright (c) 2008 Joerg Sonnenberger
4  * Copyright (c) 2011-2012 Michihiro NAKAJIMA
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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.
26  */
27 
28 #include "archive_platform.h"
29 
30 #ifdef HAVE_SYS_STAT_H
31 #include <sys/stat.h>
32 #endif
33 #ifdef HAVE_ERRNO_H
34 #include <errno.h>
35 #endif
36 #ifdef HAVE_FCNTL_H
37 #include <fcntl.h>
38 #endif
39 #include <stddef.h>
40 /* #include <stdint.h> */ /* See archive_platform.h */
41 #ifdef HAVE_STDLIB_H
42 #include <stdlib.h>
43 #endif
44 #ifdef HAVE_STRING_H
45 #include <string.h>
46 #endif
47 #ifdef HAVE_CTYPE_H
48 #include <ctype.h>
49 #endif
50 
51 #include "archive.h"
52 #include "archive_entry.h"
53 #include "archive_entry_private.h"
54 #include "archive_integer.h"
55 #include "archive_platform_stat.h"
56 #include "archive_private.h"
57 #include "archive_rb.h"
58 #include "archive_read_private.h"
59 #include "archive_string.h"
60 #include "archive_pack_dev.h"
61 
62 #ifndef O_BINARY
63 #define	O_BINARY 0
64 #endif
65 #ifndef O_CLOEXEC
66 #define O_CLOEXEC	0
67 #endif
68 
69 #define	MTREE_HAS_DEVICE	0x0001
70 #define	MTREE_HAS_FFLAGS	0x0002
71 #define	MTREE_HAS_GID		0x0004
72 #define	MTREE_HAS_GNAME		0x0008
73 #define	MTREE_HAS_MTIME		0x0010
74 #define	MTREE_HAS_NLINK		0x0020
75 #define	MTREE_HAS_PERM		0x0040
76 #define	MTREE_HAS_SIZE		0x0080
77 #define	MTREE_HAS_TYPE		0x0100
78 #define	MTREE_HAS_UID		0x0200
79 #define	MTREE_HAS_UNAME		0x0400
80 
81 #define	MTREE_HAS_OPTIONAL	0x0800
82 #define	MTREE_HAS_NOCHANGE	0x1000 /* FreeBSD specific */
83 
84 #define	MAX_LINE_LEN		(1024 * 1024)
85 
86 struct mtree_option {
87 	struct mtree_option *next;
88 	char *value;
89 };
90 
91 struct mtree_entry {
92 	struct archive_rb_node rbnode;
93 	struct mtree_entry *next_dup;
94 	struct mtree_entry *next;
95 	struct mtree_option *options;
96 	char *name;
97 	char full;
98 	char used;
99 };
100 
101 struct mtree {
102 	struct archive_string	 line;
103 	size_t			 buffsize;
104 	char			*buff;
105 	int64_t			 offset;
106 	int			 fd;
107 	int			 archive_format;
108 	const char		*archive_format_name;
109 	struct mtree_entry	*entries;
110 	struct mtree_entry	*this_entry;
111 	struct archive_rb_tree	 entry_rbtree;
112 	struct archive_string	 current_dir;
113 	struct archive_string	 contents_name;
114 
115 	struct archive_entry_linkresolver *resolver;
116 	struct archive_rb_tree rbtree;
117 
118 	int64_t			 cur_size;
119 	char checkfs;
120 };
121 
122 static int	bid_keycmp(const char *, const char *, ssize_t);
123 static int	cleanup(struct archive_read *);
124 static int	detect_form(struct archive_read *, int *);
125 static int	mtree_bid(struct archive_read *, int);
126 static int	parse_file(struct archive_read *, struct archive_entry *,
127 		    struct mtree *, struct mtree_entry *, int *);
128 static void	parse_escapes(char *, struct mtree_entry *);
129 static int	parse_line(struct archive_read *, struct archive_entry *,
130 		    struct mtree *, struct mtree_entry *, int *);
131 static int	parse_keyword(struct archive_read *, struct mtree *,
132 		    struct archive_entry *, struct mtree_option *, int *);
133 static int	read_data(struct archive_read *a,
134 		    const void **buff, size_t *size, int64_t *offset);
135 static ssize_t	readline(struct archive_read *, struct mtree *, char **, ssize_t);
136 static int	skip(struct archive_read *a);
137 static int	read_header(struct archive_read *,
138 		    struct archive_entry *);
139 static int64_t	mtree_atol(char **, int base);
140 #ifndef HAVE_STRNLEN
141 static size_t	mtree_strnlen(const char *, size_t);
142 #endif
143 
144 #ifdef HAVE_STRNLEN
145 #define mtree_strnlen(a,b) strnlen(a,b)
146 #else
147 static size_t
mtree_strnlen(const char * p,size_t maxlen)148 mtree_strnlen(const char *p, size_t maxlen)
149 {
150 	size_t i;
151 
152 	for (i = 0; i < maxlen; i++) {
153 		if (p[i] == 0)
154 			break;
155 	}
156 	return (i);
157 }
158 #endif
159 
160 static int
archive_read_format_mtree_options(struct archive_read * a,const char * key,const char * val)161 archive_read_format_mtree_options(struct archive_read *a,
162     const char *key, const char *val)
163 {
164 	struct mtree *mtree = a->format->data;
165 
166 	if (strcmp(key, "checkfs")  == 0) {
167 		/* Allows to read information missing from the mtree from the file system */
168 		if (val == NULL || val[0] == 0) {
169 			mtree->checkfs = 0;
170 		} else {
171 			mtree->checkfs = 1;
172 		}
173 		return (ARCHIVE_OK);
174 	}
175 
176 	/* Note: The "warn" return is just to inform the options
177 	 * supervisor that we didn't handle it.  It will generate
178 	 * a suitable error if no one used this option. */
179 	return (ARCHIVE_WARN);
180 }
181 
182 static void
free_options(struct mtree_option * head)183 free_options(struct mtree_option *head)
184 {
185 	struct mtree_option *next;
186 
187 	for (; head != NULL; head = next) {
188 		next = head->next;
189 		free(head->value);
190 		free(head);
191 	}
192 }
193 
194 static int
mtree_cmp_node(const struct archive_rb_node * n1,const struct archive_rb_node * n2)195 mtree_cmp_node(const struct archive_rb_node *n1,
196     const struct archive_rb_node *n2)
197 {
198 	const struct mtree_entry *e1 = (const struct mtree_entry *)n1;
199 	const struct mtree_entry *e2 = (const struct mtree_entry *)n2;
200 
201 	return (strcmp(e1->name, e2->name));
202 }
203 
204 static int
mtree_cmp_key(const struct archive_rb_node * n,const void * key)205 mtree_cmp_key(const struct archive_rb_node *n, const void *key)
206 {
207 	const struct mtree_entry *e = (const struct mtree_entry *)n;
208 
209 	return (strcmp(e->name, key));
210 }
211 
212 int
archive_read_support_format_mtree(struct archive * _a)213 archive_read_support_format_mtree(struct archive *_a)
214 {
215 	static const struct archive_rb_tree_ops rb_ops = {
216 		mtree_cmp_node, mtree_cmp_key,
217 	};
218 	struct archive_read *a = (struct archive_read *)_a;
219 	struct mtree *mtree;
220 	int r;
221 
222 	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
223 	    ARCHIVE_STATE_NEW, "archive_read_support_format_mtree");
224 
225 	mtree = calloc(1, sizeof(*mtree));
226 	if (mtree == NULL) {
227 		archive_set_error(&a->archive, ENOMEM,
228 		    "Can't allocate mtree data");
229 		return (ARCHIVE_FATAL);
230 	}
231 	mtree->checkfs = 0;
232 	mtree->fd = -1;
233 
234 	__archive_rb_tree_init(&mtree->rbtree, &rb_ops);
235 
236 	r = __archive_read_register_format(a, mtree, "mtree",
237            mtree_bid, archive_read_format_mtree_options, read_header, read_data, skip, NULL, cleanup, NULL, NULL);
238 
239 	if (r != ARCHIVE_OK)
240 		free(mtree);
241 	return (ARCHIVE_OK);
242 }
243 
244 static int
cleanup(struct archive_read * a)245 cleanup(struct archive_read *a)
246 {
247 	struct mtree *mtree = a->format->data;
248 	struct mtree_entry *p, *q;
249 
250 	/* Close any dangling file descriptor before freeing */
251     if (mtree->fd >= 0) {
252         close(mtree->fd);
253         mtree->fd = -1;
254     }
255 	p = mtree->entries;
256 	while (p != NULL) {
257 		q = p->next;
258 		free(p->name);
259 		free_options(p->options);
260 		free(p);
261 		p = q;
262 	}
263 	archive_string_free(&mtree->line);
264 	archive_string_free(&mtree->current_dir);
265 	archive_string_free(&mtree->contents_name);
266 	archive_entry_linkresolver_free(mtree->resolver);
267 
268 	free(mtree->buff);
269 	free(mtree);
270 	a->format->data = NULL;
271 	return (ARCHIVE_OK);
272 }
273 
274 static ssize_t
get_line_size(const char * b,ssize_t avail,ssize_t * nlsize)275 get_line_size(const char *b, ssize_t avail, ssize_t *nlsize)
276 {
277 	ssize_t len;
278 
279 	len = 0;
280 	while (len < avail) {
281 		switch (*b) {
282 		case '\0':/* Non-ascii character or control character. */
283 			if (nlsize != NULL)
284 				*nlsize = 0;
285 			return (-1);
286 		case '\r':
287 			if (avail-len > 1 && b[1] == '\n') {
288 				if (nlsize != NULL)
289 					*nlsize = 2;
290 				return (len+2);
291 			}
292 			/* FALL THROUGH */
293 		case '\n':
294 			if (nlsize != NULL)
295 				*nlsize = 1;
296 			return (len+1);
297 		default:
298 			b++;
299 			len++;
300 			break;
301 		}
302 	}
303 	if (nlsize != NULL)
304 		*nlsize = 0;
305 	return (avail);
306 }
307 
308 /*
309  *  <---------------- ravail --------------------->
310  *  <-- diff ------> <---  avail ----------------->
311  *                   <---- len ----------->
312  * | Previous lines | line being parsed  nl extra |
313  *                  ^
314  *                  b
315  *
316  */
317 static ssize_t
next_line(struct archive_read * a,const char ** b,ssize_t * avail,ssize_t * ravail,ssize_t * nl)318 next_line(struct archive_read *a,
319     const char **b, ssize_t *avail, ssize_t *ravail, ssize_t *nl)
320 {
321 	ssize_t len;
322 	int quit;
323 
324 	quit = 0;
325 	if (*avail == 0) {
326 		*nl = 0;
327 		len = 0;
328 	} else
329 		len = get_line_size(*b, *avail, nl);
330 	/*
331 	 * Read bytes more while it does not reach the end of line.
332 	 */
333 	while (*nl == 0 && len == *avail && !quit) {
334 		ssize_t diff = *ravail - *avail;
335 		size_t nbytes_req = ((size_t)*ravail + 1023) & ~1023U;
336 		ssize_t tested;
337 
338 		/*
339 		 * Place an arbitrary limit on the line length.
340 		 * mtree is almost free-form input and without line length limits,
341 		 * it can consume a lot of memory.
342 		 */
343 		if (len >= MAX_LINE_LEN)
344 			return (-1);
345 
346 		/* Increase reading bytes if it is not enough for at least
347 		 * two new lines. */
348 		if (nbytes_req < (size_t)*ravail + 160)
349 			nbytes_req <<= 1;
350 
351 		*b = __archive_read_ahead(a, nbytes_req, avail);
352 		if (*b == NULL) {
353 			if (*ravail >= *avail)
354 				return (0);
355 			/* Reading bytes reaches the end of file. */
356 			*b = __archive_read_ahead(a, *avail, avail);
357 			quit = 1;
358 		}
359 		*ravail = *avail;
360 		*b += diff;
361 		*avail -= diff;
362 		tested = len;/* Skip some bytes we already determined. */
363 		len = get_line_size(*b + len, *avail - len, nl);
364 		if (len >= 0)
365 			len += tested;
366 	}
367 	return (len);
368 }
369 
370 /*
371  * Compare characters with an mtree keyword.
372  * Returns the length of an mtree keyword if matched.
373  * Returns 0 if not matched.
374  */
375 static int
bid_keycmp(const char * p,const char * key,ssize_t len)376 bid_keycmp(const char *p, const char *key, ssize_t len)
377 {
378 	int match_len = 0;
379 
380 	while (len > 0 && *p && *key) {
381 		if (*p == *key) {
382 			--len;
383 			++p;
384 			++key;
385 			++match_len;
386 			continue;
387 		}
388 		return (0);/* Not match */
389 	}
390 	if (*key != '\0')
391 		return (0);/* Not match */
392 
393 	/* A following character should be specified characters */
394 	if (p[0] == '=' || p[0] == ' ' || p[0] == '\t' ||
395 	    p[0] == '\n' || p[0] == '\r' ||
396 	   (p[0] == '\\' && (p[1] == '\n' || p[1] == '\r')))
397 		return (match_len);
398 	return (0);/* Not match */
399 }
400 
401 /*
402  * Test whether the characters 'p' has is mtree keyword.
403  * Returns the length of a detected keyword.
404  * Returns 0 if any keywords were not found.
405  */
406 static int
bid_keyword(const char * p,ssize_t len)407 bid_keyword(const char *p,  ssize_t len)
408 {
409 	static const char * const keys_c[] = {
410 		"content", "contents", "cksum", NULL
411 	};
412 	static const char * const keys_df[] = {
413 		"device", "flags", NULL
414 	};
415 	static const char * const keys_g[] = {
416 		"gid", "gname", NULL
417 	};
418 	static const char * const keys_il[] = {
419 		"ignore", "inode", "link", NULL
420 	};
421 	static const char * const keys_m[] = {
422 		"md5", "md5digest", "mode", NULL
423 	};
424 	static const char * const keys_no[] = {
425 		"nlink", "nochange", "optional", NULL
426 	};
427 	static const char * const keys_r[] = {
428 		"resdevice", "rmd160", "rmd160digest", NULL
429 	};
430 	static const char * const keys_s[] = {
431 		"sha1", "sha1digest",
432 		"sha256", "sha256digest",
433 		"sha384", "sha384digest",
434 		"sha512", "sha512digest",
435 		"size", NULL
436 	};
437 	static const char * const keys_t[] = {
438 		"tags", "time", "type", NULL
439 	};
440 	static const char * const keys_u[] = {
441 		"uid", "uname",	NULL
442 	};
443 	const char * const *keys;
444 	int i;
445 
446 	switch (*p) {
447 	case 'c': keys = keys_c; break;
448 	case 'd': case 'f': keys = keys_df; break;
449 	case 'g': keys = keys_g; break;
450 	case 'i': case 'l': keys = keys_il; break;
451 	case 'm': keys = keys_m; break;
452 	case 'n': case 'o': keys = keys_no; break;
453 	case 'r': keys = keys_r; break;
454 	case 's': keys = keys_s; break;
455 	case 't': keys = keys_t; break;
456 	case 'u': keys = keys_u; break;
457 	default: return (0);/* Unknown key */
458 	}
459 
460 	for (i = 0; keys[i] != NULL; i++) {
461 		int l = bid_keycmp(p, keys[i], len);
462 		if (l > 0)
463 			return (l);
464 	}
465 	return (0);/* Unknown key */
466 }
467 
468 /*
469  * Test whether there is a set of mtree keywords.
470  * Returns the number of keywords.
471  * Returns -1 if we got incorrect sequence.
472  * This function expects a set of "<space characters>keyword=value".
473  * When "unset" is specified, expects a set of "<space characters>keyword".
474  */
475 static int
bid_keyword_list(const char * p,ssize_t len,int unset,int last_is_path)476 bid_keyword_list(const char *p,  ssize_t len, int unset, int last_is_path)
477 {
478 	int l;
479 	int keycnt = 0;
480 
481 	while (len > 0 && *p) {
482 		int blank = 0;
483 
484 		/* Test whether there are blank characters in the line. */
485 		while (len >0 && (*p == ' ' || *p == '\t')) {
486 			++p;
487 			--len;
488 			blank = 1;
489 		}
490 		if (*p == '\n' || *p == '\r')
491 			break;
492 		if (p[0] == '\\' && (p[1] == '\n' || p[1] == '\r'))
493 			break;
494 		if (!blank && !last_is_path) /* No blank character. */
495 			return (-1);
496 		if (last_is_path && len == 0)
497 				return (keycnt);
498 
499 		if (unset) {
500 			l = bid_keycmp(p, "all", len);
501 			if (l > 0)
502 				return (1);
503 		}
504 		/* Test whether there is a correct key in the line. */
505 		l = bid_keyword(p, len);
506 		if (l == 0)
507 			return (-1);/* Unknown keyword was found. */
508 		p += l;
509 		len -= l;
510 		keycnt++;
511 
512 		/* Skip value */
513 		if (*p == '=') {
514 			int value = 0;
515 			++p;
516 			--len;
517 			while (len > 0 && *p != ' ' && *p != '\t') {
518 				++p;
519 				--len;
520 				value = 1;
521 			}
522 			/* A keyword should have a value unless this is
523 			 * an "/unset" operation. */
524 			if (!unset && value == 0)
525 				return (-1);
526 		}
527 	}
528 	return (keycnt);
529 }
530 
531 static int
bid_entry(const char * p,ssize_t len,ssize_t nl,int * last_is_path)532 bid_entry(const char *p, ssize_t len, ssize_t nl, int *last_is_path)
533 {
534 	int f = 0;
535 	static const unsigned char safe_char[256] = {
536 		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 00 - 0F */
537 		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 - 1F */
538 		/* !"$%&'()*+,-./  EXCLUSION:( )(#) */
539 		0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 20 - 2F */
540 		/* 0123456789:;<>?  EXCLUSION:(=) */
541 		1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, /* 30 - 3F */
542 		/* @ABCDEFGHIJKLMNO */
543 		1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 40 - 4F */
544 		/* PQRSTUVWXYZ[\]^_  */
545 		1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 50 - 5F */
546 		/* `abcdefghijklmno */
547 		1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 60 - 6F */
548 		/* pqrstuvwxyz{|}~ */
549 		1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, /* 70 - 7F */
550 		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80 - 8F */
551 		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90 - 9F */
552 		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* A0 - AF */
553 		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* B0 - BF */
554 		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* C0 - CF */
555 		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* D0 - DF */
556 		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* E0 - EF */
557 		0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* F0 - FF */
558 	};
559 	ssize_t ll;
560 	const char *pp = p;
561 	const char * const pp_end = pp + len;
562 
563 	*last_is_path = 0;
564 	/*
565 	 * Skip the path-name which is quoted.
566 	 */
567 	for (;pp < pp_end; ++pp) {
568 		if (!safe_char[*(const unsigned char *)pp]) {
569 			if (*pp != ' ' && *pp != '\t' && *pp != '\r'
570 			    && *pp != '\n')
571 				f = 0;
572 			break;
573 		}
574 		f = 1;
575 	}
576 	ll = pp_end - pp;
577 
578 	/* If a path-name was not found at the first, try to check
579 	 * a mtree format(a.k.a form D) ``NetBSD's mtree -D'' creates,
580 	 * which places the path-name at the last. */
581 	if (f == 0) {
582 		const char *pb = p + len - nl;
583 		int name_len = 0;
584 		int slash;
585 
586 		/* The form D accepts only a single line for an entry. */
587 		if (pb-2 >= p &&
588 		    pb[-1] == '\\' && (pb[-2] == ' ' || pb[-2] == '\t'))
589 			return (-1);
590 		if (pb-1 >= p && pb[-1] == '\\')
591 			return (-1);
592 
593 		slash = 0;
594 		while (p <= --pb && *pb != ' ' && *pb != '\t') {
595 			if (!safe_char[*(const unsigned char *)pb])
596 				return (-1);
597 			name_len++;
598 			/* The pathname should have a slash in this
599 			 * format. */
600 			if (*pb == '/')
601 				slash = 1;
602 		}
603 		if (name_len == 0 || slash == 0)
604 			return (-1);
605 		/* If '/' is placed at the first in this field, this is not
606 		 * a valid filename. */
607 		if (pb[1] == '/')
608 			return (-1);
609 		ll = len - nl - name_len;
610 		pp = p;
611 		*last_is_path = 1;
612 	}
613 
614 	return (bid_keyword_list(pp, ll, 0, *last_is_path));
615 }
616 
617 #define MAX_BID_ENTRY	3
618 
619 static int
mtree_bid(struct archive_read * a,int best_bid)620 mtree_bid(struct archive_read *a, int best_bid)
621 {
622 	const char *signature = "#mtree";
623 	const char *p;
624 
625 	(void)best_bid; /* UNUSED */
626 
627 	/* Now let's look at the actual header and see if it matches. */
628 	p = __archive_read_ahead(a, strlen(signature), NULL);
629 	if (p == NULL)
630 		return (-1);
631 
632 	if (memcmp(p, signature, strlen(signature)) == 0)
633 		return (8 * (int)strlen(signature));
634 
635 	/*
636 	 * There is not a mtree signature. Let's try to detect mtree format.
637 	 */
638 	return (detect_form(a, NULL));
639 }
640 
641 static int
detect_form(struct archive_read * a,int * is_form_d)642 detect_form(struct archive_read *a, int *is_form_d)
643 {
644 	const char *p;
645 	ssize_t avail, ravail;
646 	ssize_t len, nl;
647 	int entry_cnt = 0, multiline = 0;
648 	int form_D = 0;/* The archive is generated by `NetBSD mtree -D'
649 			* (In this source we call it `form D') . */
650 
651 	if (is_form_d != NULL)
652 		*is_form_d = 0;
653 	p = __archive_read_ahead(a, 1, &avail);
654 	if (p == NULL)
655 		return (-1);
656 	ravail = avail;
657 	for (;;) {
658 		len = next_line(a, &p, &avail, &ravail, &nl);
659 		/* The terminal character of the line should be
660 		 * a new line character, '\r\n' or '\n'. */
661 		if (len <= 0 || nl == 0)
662 			break;
663 		if (!multiline) {
664 			/* Leading whitespace is never significant,
665 			 * ignore it. */
666 			while (len > 0 && (*p == ' ' || *p == '\t')) {
667 				++p;
668 				--avail;
669 				--len;
670 			}
671 			/* Skip comment or empty line. */
672 			if (p[0] == '#' || p[0] == '\n' || p[0] == '\r') {
673 				p += len;
674 				avail -= len;
675 				continue;
676 			}
677 		} else {
678 			/* A continuance line; the terminal
679 			 * character of previous line was '\' character. */
680 			if (bid_keyword_list(p, len, 0, 0) <= 0)
681 				break;
682 			if (p[len-nl-1] != '\\') {
683 				if (multiline == 1 &&
684 				    ++entry_cnt >= MAX_BID_ENTRY)
685 					break;
686 				multiline = 0;
687 			}
688 			p += len;
689 			avail -= len;
690 			continue;
691 		}
692 		if (p[0] != '/') {
693 			int last_is_path, keywords;
694 
695 			keywords = bid_entry(p, len, nl, &last_is_path);
696 			if (keywords >= 0) {
697 				if (form_D == 0) {
698 					if (last_is_path)
699 						form_D = 1;
700 					else if (keywords > 0)
701 						/* This line is not `form D'. */
702 						form_D = -1;
703 				} else if (form_D == 1) {
704 					if (!last_is_path && keywords > 0)
705 						/* This this is not `form D'
706 						 * and we cannot accept mixed
707 						 * format. */
708 						break;
709 				}
710 				if (!last_is_path && p[len-nl-1] == '\\')
711 					/* This line continues. */
712 					multiline = 1;
713 				else {
714 					/* We've got plenty of correct lines
715 					 * to assume that this file is an mtree
716 					 * format. */
717 					if (++entry_cnt >= MAX_BID_ENTRY)
718 						break;
719 				}
720 			} else
721 				break;
722 		} else if (len > 4 && strncmp(p, "/set", 4) == 0) {
723 			if (bid_keyword_list(p+4, len-4, 0, 0) <= 0)
724 				break;
725 			/* This line continues. */
726 			if (p[len-nl-1] == '\\')
727 				multiline = 2;
728 		} else if (len > 6 && strncmp(p, "/unset", 6) == 0) {
729 			if (bid_keyword_list(p+6, len-6, 1, 0) <= 0)
730 				break;
731 			/* This line continues. */
732 			if (p[len-nl-1] == '\\')
733 				multiline = 2;
734 		} else
735 			break;
736 
737 		/* Test next line. */
738 		p += len;
739 		avail -= len;
740 	}
741 	if (entry_cnt >= MAX_BID_ENTRY || (entry_cnt > 0 && len == 0)) {
742 		if (is_form_d != NULL) {
743 			if (form_D == 1)
744 				*is_form_d = 1;
745 		}
746 		return (32);
747 	}
748 
749 	return (0);
750 }
751 
752 /*
753  * The extended mtree format permits multiple lines specifying
754  * attributes for each file.  For those entries, only the last line
755  * is actually used.  Practically speaking, that means we have
756  * to read the entire mtree file into memory up front.
757  *
758  * The parsing is done in two steps.  First, it is decided if a line
759  * changes the global defaults and if it does, it is processed accordingly.
760  * Otherwise, the options of the line are merged with the current
761  * global options.
762  */
763 static int
add_option(struct archive_read * a,struct mtree_option ** global,const char * value,size_t len)764 add_option(struct archive_read *a, struct mtree_option **global,
765     const char *value, size_t len)
766 {
767 	struct mtree_option *opt;
768 
769 	if ((opt = malloc(sizeof(*opt))) == NULL) {
770 		archive_set_error(&a->archive, errno, "Can't allocate memory");
771 		return (ARCHIVE_FATAL);
772 	}
773 	if ((opt->value = malloc(len + 1)) == NULL) {
774 		free(opt);
775 		archive_set_error(&a->archive, errno, "Can't allocate memory");
776 		return (ARCHIVE_FATAL);
777 	}
778 	memcpy(opt->value, value, len);
779 	opt->value[len] = '\0';
780 	opt->next = *global;
781 	*global = opt;
782 	return (ARCHIVE_OK);
783 }
784 
785 static void
remove_option(struct mtree_option ** global,const char * value,size_t len)786 remove_option(struct mtree_option **global, const char *value, size_t len)
787 {
788 	struct mtree_option *iter, *last;
789 
790 	last = NULL;
791 	for (iter = *global; iter != NULL; last = iter, iter = iter->next) {
792 		if (strncmp(iter->value, value, len) == 0 &&
793 		    (iter->value[len] == '\0' ||
794 		     iter->value[len] == '='))
795 			break;
796 	}
797 	if (iter == NULL)
798 		return;
799 	if (last == NULL)
800 		*global = iter->next;
801 	else
802 		last->next = iter->next;
803 
804 	free(iter->value);
805 	free(iter);
806 }
807 
808 static int
process_global_set(struct archive_read * a,struct mtree_option ** global,const char * line)809 process_global_set(struct archive_read *a,
810     struct mtree_option **global, const char *line)
811 {
812 	const char *next, *eq;
813 	size_t len;
814 	int r;
815 
816 	line += 4;
817 	for (;;) {
818 		next = line + strspn(line, " \t\r\n");
819 		if (*next == '\0')
820 			return (ARCHIVE_OK);
821 		line = next;
822 		next = line + strcspn(line, " \t\r\n");
823 		eq = strchr(line, '=');
824 		if (eq == NULL || eq > next)
825 			len = next - line;
826 		else
827 			len = eq - line;
828 
829 		remove_option(global, line, len);
830 		r = add_option(a, global, line, next - line);
831 		if (r != ARCHIVE_OK)
832 			return (r);
833 		line = next;
834 	}
835 }
836 
837 static int
process_global_unset(struct archive_read * a,struct mtree_option ** global,const char * line)838 process_global_unset(struct archive_read *a,
839     struct mtree_option **global, const char *line)
840 {
841 	const char *next;
842 	size_t len;
843 
844 	line += 6;
845 	if (strchr(line, '=') != NULL) {
846 		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
847 		    "/unset shall not contain `='");
848 		return ARCHIVE_FATAL;
849 	}
850 
851 	for (;;) {
852 		next = line + strspn(line, " \t\r\n");
853 		if (*next == '\0')
854 			return (ARCHIVE_OK);
855 		line = next;
856 		len = strcspn(line, " \t\r\n");
857 
858 		if (len == 3 && strncmp(line, "all", 3) == 0) {
859 			free_options(*global);
860 			*global = NULL;
861 		} else {
862 			remove_option(global, line, len);
863 		}
864 
865 		line += len;
866 	}
867 }
868 
869 static int
process_add_entry(struct archive_read * a,struct mtree * mtree,struct mtree_option ** global,const char * line,ssize_t line_len,struct mtree_entry ** last_entry,int is_form_d)870 process_add_entry(struct archive_read *a, struct mtree *mtree,
871     struct mtree_option **global, const char *line, ssize_t line_len,
872     struct mtree_entry **last_entry, int is_form_d)
873 {
874 	struct mtree_entry *entry;
875 	struct mtree_option *iter;
876 	const char *next, *eq, *name, *end;
877 	size_t name_len, len;
878 	int r, i;
879 
880 	if ((entry = malloc(sizeof(*entry))) == NULL) {
881 		archive_set_error(&a->archive, errno, "Can't allocate memory");
882 		return (ARCHIVE_FATAL);
883 	}
884 	entry->next = NULL;
885 	entry->options = NULL;
886 	entry->name = NULL;
887 	entry->used = 0;
888 	entry->full = 0;
889 
890 	/* Add this entry to list. */
891 	if (*last_entry == NULL)
892 		mtree->entries = entry;
893 	else
894 		(*last_entry)->next = entry;
895 	*last_entry = entry;
896 
897 	if (is_form_d) {
898 		/* Filename is last item on line. */
899 		/* Adjust line_len to trim trailing whitespace */
900 		while (line_len > 0) {
901 			char last_character = line[line_len - 1];
902 			if (last_character == '\r'
903 			    || last_character == '\n'
904 			    || last_character == '\t'
905 			    || last_character == ' ') {
906 				line_len--;
907 			} else {
908 				break;
909 			}
910 		}
911 		/* Name starts after the last whitespace separator */
912 		name = line;
913 		for (i = 0; i < line_len; i++) {
914 			if (line[i] == '\r'
915 			    || line[i] == '\n'
916 			    || line[i] == '\t'
917 			    || line[i] == ' ') {
918 				name = line + i + 1;
919 			}
920 		}
921 		name_len = line + line_len - name;
922 		end = name;
923 	} else {
924 		/* Filename is first item on line */
925 		name_len = strcspn(line, " \t\r\n");
926 		name = line;
927 		line += name_len;
928 		end = line + line_len;
929 	}
930 	/* name/name_len is the name within the line. */
931 	/* line..end brackets the entire line except the name */
932 
933 	if ((entry->name = malloc(name_len + 1)) == NULL) {
934 		archive_set_error(&a->archive, errno, "Can't allocate memory");
935 		return (ARCHIVE_FATAL);
936 	}
937 
938 	memcpy(entry->name, name, name_len);
939 	entry->name[name_len] = '\0';
940 	parse_escapes(entry->name, entry);
941 
942 	entry->next_dup = NULL;
943 	if (entry->full) {
944 		if (!__archive_rb_tree_insert_node(&mtree->rbtree, &entry->rbnode)) {
945 			struct mtree_entry *alt;
946 			alt = (struct mtree_entry *)__archive_rb_tree_find_node(
947 			    &mtree->rbtree, entry->name);
948 			if (alt != NULL) {
949 				while (alt->next_dup)
950 					alt = alt->next_dup;
951 				alt->next_dup = entry;
952 			}
953 		}
954 	}
955 
956 	for (iter = *global; iter != NULL; iter = iter->next) {
957 		r = add_option(a, &entry->options, iter->value,
958 		    strlen(iter->value));
959 		if (r != ARCHIVE_OK)
960 			return (r);
961 	}
962 
963 	for (;;) {
964 		next = line + strspn(line, " \t\r\n");
965 		if (*next == '\0')
966 			return (ARCHIVE_OK);
967 		if (next >= end)
968 			return (ARCHIVE_OK);
969 		line = next;
970 		next = line + strcspn(line, " \t\r\n");
971 		eq = strchr(line, '=');
972 		if (eq == NULL || eq > next)
973 			len = next - line;
974 		else
975 			len = eq - line;
976 
977 		remove_option(&entry->options, line, len);
978 		r = add_option(a, &entry->options, line, next - line);
979 		if (r != ARCHIVE_OK)
980 			return (r);
981 		line = next;
982 	}
983 }
984 
985 static int
read_mtree(struct archive_read * a,struct mtree * mtree)986 read_mtree(struct archive_read *a, struct mtree *mtree)
987 {
988 	ssize_t len;
989 	uintmax_t counter;
990 	char *p, *s;
991 	struct mtree_option *global;
992 	struct mtree_entry *last_entry;
993 	int r, is_form_d;
994 
995 	mtree->archive_format = ARCHIVE_FORMAT_MTREE;
996 	mtree->archive_format_name = "mtree";
997 
998 	global = NULL;
999 	last_entry = NULL;
1000 
1001 	(void)detect_form(a, &is_form_d);
1002 
1003 	for (counter = 1; ; ++counter) {
1004 		r = ARCHIVE_OK;
1005 		len = readline(a, mtree, &p, 65536);
1006 		if (len == 0) {
1007 			mtree->this_entry = mtree->entries;
1008 			free_options(global);
1009 			return (ARCHIVE_OK);
1010 		}
1011 		if (len < 0) {
1012 			free_options(global);
1013 			return ((int)len);
1014 		}
1015 		/* Leading whitespace is never significant, ignore it. */
1016 		while (*p == ' ' || *p == '\t') {
1017 			++p;
1018 			--len;
1019 		}
1020 		/* Skip content lines and blank lines. */
1021 		if (*p == '#')
1022 			continue;
1023 		if (*p == '\r' || *p == '\n' || *p == '\0')
1024 			continue;
1025 		/* Non-printable characters are not allowed */
1026 		for (s = p;s < p + len - 1; s++) {
1027 			if (!isprint((unsigned char)*s) && *s != '\t') {
1028 				archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1029 					"Non-printable character 0x%02X", (unsigned char)(*s));
1030 				r = ARCHIVE_FATAL;
1031 				break;
1032 			}
1033 		}
1034 		if (r != ARCHIVE_OK)
1035 			break;
1036 		if (*p != '/') {
1037 			r = process_add_entry(a, mtree, &global, p, len,
1038 			    &last_entry, is_form_d);
1039 		} else if (len > 4 && strncmp(p, "/set", 4) == 0) {
1040 			if (p[4] != ' ' && p[4] != '\t')
1041 				break;
1042 			r = process_global_set(a, &global, p);
1043 		} else if (len > 6 && strncmp(p, "/unset", 6) == 0) {
1044 			if (p[6] != ' ' && p[6] != '\t')
1045 				break;
1046 			r = process_global_unset(a, &global, p);
1047 		} else
1048 			break;
1049 
1050 		if (r != ARCHIVE_OK) {
1051 			free_options(global);
1052 			return r;
1053 		}
1054 	}
1055 
1056 	archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1057 	    "Can't parse line %ju", counter);
1058 	free_options(global);
1059 	return (ARCHIVE_FATAL);
1060 }
1061 
1062 /*
1063  * Read in the entire mtree file into memory on the first request.
1064  * Then use the next unused file to satisfy each header request.
1065  */
1066 static int
read_header(struct archive_read * a,struct archive_entry * entry)1067 read_header(struct archive_read *a, struct archive_entry *entry)
1068 {
1069 	struct mtree *mtree = a->format->data;
1070 	char *p;
1071 	int r, use_next;
1072 
1073 	if (mtree->fd >= 0) {
1074 		close(mtree->fd);
1075 		mtree->fd = -1;
1076 	}
1077 
1078 	if (mtree->entries == NULL) {
1079 		mtree->resolver = archive_entry_linkresolver_new();
1080 		if (mtree->resolver == NULL)
1081 			return ARCHIVE_FATAL;
1082 		archive_entry_linkresolver_set_strategy(mtree->resolver,
1083 		    ARCHIVE_FORMAT_MTREE);
1084 		r = read_mtree(a, mtree);
1085 		if (r != ARCHIVE_OK)
1086 			return (r);
1087 	}
1088 
1089 	a->archive.archive_format = mtree->archive_format;
1090 	a->archive.archive_format_name = mtree->archive_format_name;
1091 
1092 	for (;;) {
1093 		if (mtree->this_entry == NULL)
1094 			return (ARCHIVE_EOF);
1095 		if (strcmp(mtree->this_entry->name, "..") == 0) {
1096 			mtree->this_entry->used = 1;
1097 			if (archive_strlen(&mtree->current_dir) > 0) {
1098 				/* Roll back current path. */
1099 				p = mtree->current_dir.s
1100 				    + mtree->current_dir.length - 1;
1101 				while (p >= mtree->current_dir.s && *p != '/')
1102 					--p;
1103 				if (p >= mtree->current_dir.s)
1104 					--p;
1105 				mtree->current_dir.length
1106 				    = p - mtree->current_dir.s + 1;
1107 			}
1108 		}
1109 		if (!mtree->this_entry->used) {
1110 			use_next = 0;
1111 			r = parse_file(a, entry, mtree, mtree->this_entry,
1112 				&use_next);
1113 			if (use_next == 0)
1114 				return (r);
1115 		}
1116 		mtree->this_entry = mtree->this_entry->next;
1117 	}
1118 }
1119 
1120 /*
1121  * A single file can have multiple lines contribute specifications.
1122  * Parse as many lines as necessary, then pull additional information
1123  * from a backing file on disk as necessary.
1124  */
1125 static int
parse_file(struct archive_read * a,struct archive_entry * entry,struct mtree * mtree,struct mtree_entry * mentry,int * use_next)1126 parse_file(struct archive_read *a, struct archive_entry *entry,
1127     struct mtree *mtree, struct mtree_entry *mentry, int *use_next)
1128 {
1129 	const char *path;
1130 	la_seek_stat_t st_storage, *st;
1131 	struct mtree_entry *mp;
1132 	struct archive_entry *sparse_entry;
1133 	int r = ARCHIVE_OK, r1, parsed_kws;
1134 
1135 	mentry->used = 1;
1136 
1137 	/* Initialize reasonable defaults. */
1138 	archive_entry_set_filetype(entry, AE_IFREG);
1139 	archive_entry_set_size(entry, 0);
1140 	archive_string_empty(&mtree->contents_name);
1141 
1142 	/* Parse options from this line. */
1143 	parsed_kws = 0;
1144 	r = parse_line(a, entry, mtree, mentry, &parsed_kws);
1145 
1146 	if (mentry->full) {
1147 		archive_entry_copy_pathname(entry, mentry->name);
1148 		/*
1149 		 * "Full" entries are allowed to have multiple lines
1150 		 * and those lines aren't required to be adjacent.  We
1151 		 * don't support multiple lines for "relative" entries
1152 		 * nor do we make any attempt to merge data from
1153 		 * separate "relative" and "full" entries.  (Merging
1154 		 * "relative" and "full" entries would require dealing
1155 		 * with pathname canonicalization, which is a very
1156 		 * tricky subject.)
1157 		 */
1158 		mp = (struct mtree_entry *)__archive_rb_tree_find_node(
1159 		    &mtree->rbtree, mentry->name);
1160 		for (; mp; mp = mp->next_dup) {
1161 			if (mp->full && !mp->used) {
1162 				/* Later lines override earlier ones. */
1163 				mp->used = 1;
1164 				r1 = parse_line(a, entry, mtree, mp, &parsed_kws);
1165 				if (r1 < r)
1166 					r = r1;
1167 			}
1168 		}
1169 	} else {
1170 		/*
1171 		 * Relative entries require us to construct
1172 		 * the full path and possibly update the
1173 		 * current directory.
1174 		 */
1175 		size_t n = archive_strlen(&mtree->current_dir);
1176 		if (n > 0)
1177 			archive_strcat(&mtree->current_dir, "/");
1178 		archive_strcat(&mtree->current_dir, mentry->name);
1179 		archive_entry_copy_pathname(entry, mtree->current_dir.s);
1180 		if (archive_entry_filetype(entry) != AE_IFDIR)
1181 			mtree->current_dir.length = n;
1182 	}
1183 
1184 	if (mtree->checkfs) {
1185 		/*
1186 		 * Try to open and stat the file to get the real size
1187 		 * and other file info.  It would be nice to avoid
1188 		 * this here so that getting a listing of an mtree
1189 		 * wouldn't require opening every referenced contents
1190 		 * file.  But then we wouldn't know the actual
1191 		 * contents size, so I don't see a really viable way
1192 		 * around this.  (Also, we may want to someday pull
1193 		 * other unspecified info from the contents file on
1194 		 * disk.)
1195 		 */
1196 		mtree->fd = -1;
1197 		if (archive_strlen(&mtree->contents_name) > 0)
1198 			path = mtree->contents_name.s;
1199 		else
1200 			path = archive_entry_pathname(entry);
1201 
1202 		if (archive_entry_filetype(entry) == AE_IFREG ||
1203 				archive_entry_filetype(entry) == AE_IFDIR) {
1204 			mtree->fd = open(path, O_RDONLY | O_BINARY | O_CLOEXEC);
1205 			__archive_ensure_cloexec_flag(mtree->fd);
1206 			if (mtree->fd == -1 && (
1207 #if defined(_WIN32) && !defined(__CYGWIN__)
1208         /*
1209          * On Windows, attempting to open a file with an
1210          * invalid name result in EINVAL (Error 22)
1211          */
1212 				(errno != ENOENT && errno != EINVAL)
1213 #else
1214 				errno != ENOENT
1215 #endif
1216         || archive_strlen(&mtree->contents_name) > 0)) {
1217 				archive_set_error(&a->archive, errno,
1218 						"Can't open %s", path);
1219 				r = ARCHIVE_WARN;
1220 			}
1221 		}
1222 
1223 		st = &st_storage;
1224 		if (mtree->fd >= 0) {
1225 			if (la_seek_fstat(mtree->fd, st) == -1) {
1226 				archive_set_error(&a->archive, errno,
1227 						"Could not fstat %s", path);
1228 				r = ARCHIVE_WARN;
1229 				/* If we can't stat it, don't keep it open. */
1230 				close(mtree->fd);
1231 				mtree->fd = -1;
1232 				st = NULL;
1233 			}
1234 		}
1235 #ifdef HAVE_LSTAT
1236 		else if (lstat(path, st) == -1)
1237 #else
1238 		else if (la_seek_stat(path, st) == -1)
1239 #endif
1240 		{
1241 			st = NULL;
1242 		}
1243 
1244 		/*
1245 		 * Check for a mismatch between the type in the specification
1246 		 * and the type of the contents object on disk.
1247 		 */
1248 		if (st != NULL) {
1249 			if (((st->st_mode & S_IFMT) == S_IFREG &&
1250 			      archive_entry_filetype(entry) == AE_IFREG)
1251 #ifdef S_IFLNK
1252 			  ||((st->st_mode & S_IFMT) == S_IFLNK &&
1253 			      archive_entry_filetype(entry) == AE_IFLNK)
1254 #endif
1255 #ifdef S_IFSOCK
1256 			  ||((st->st_mode & S_IFSOCK) == S_IFSOCK &&
1257 			      archive_entry_filetype(entry) == AE_IFSOCK)
1258 #endif
1259 #ifdef S_IFCHR
1260 			  ||((st->st_mode & S_IFMT) == S_IFCHR &&
1261 			      archive_entry_filetype(entry) == AE_IFCHR)
1262 #endif
1263 #ifdef S_IFBLK
1264 			  ||((st->st_mode & S_IFMT) == S_IFBLK &&
1265 			      archive_entry_filetype(entry) == AE_IFBLK)
1266 #endif
1267 			  ||((st->st_mode & S_IFMT) == S_IFDIR &&
1268 			      archive_entry_filetype(entry) == AE_IFDIR)
1269 #ifdef S_IFIFO
1270 			  ||((st->st_mode & S_IFMT) == S_IFIFO &&
1271 			      archive_entry_filetype(entry) == AE_IFIFO)
1272 #endif
1273 			) {
1274 				/* Types match. */
1275 			} else {
1276 				/* Types don't match; bail out gracefully. */
1277 				if (mtree->fd >= 0)
1278 					close(mtree->fd);
1279 				mtree->fd = -1;
1280 				if (parsed_kws & MTREE_HAS_OPTIONAL) {
1281 					/* It's not an error for an optional
1282 					 * entry to not match disk. */
1283 					*use_next = 1;
1284 				} else if (r == ARCHIVE_OK) {
1285 					archive_set_error(&a->archive,
1286 					    ARCHIVE_ERRNO_MISC,
1287 					    "mtree specification has different"
1288 					    " type for %s",
1289 					    archive_entry_pathname(entry));
1290 					r = ARCHIVE_WARN;
1291 				}
1292 				return (r);
1293 			}
1294 		}
1295 
1296 		/*
1297 		 * If there is a contents file on disk, pick some of the
1298 		 * metadata from that file.  For most of these, we only
1299 		 * set it from the contents if it wasn't already parsed
1300 		 * from the specification.
1301 		 */
1302 		if (st != NULL) {
1303 			if (((parsed_kws & MTREE_HAS_DEVICE) == 0 ||
1304 				(parsed_kws & MTREE_HAS_NOCHANGE) != 0) &&
1305 				(archive_entry_filetype(entry) == AE_IFCHR ||
1306 				 archive_entry_filetype(entry) == AE_IFBLK))
1307 				archive_entry_set_rdev(entry, st->st_rdev);
1308 			if ((parsed_kws & (MTREE_HAS_GID | MTREE_HAS_GNAME))
1309 				== 0 ||
1310 			    (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1311 				archive_entry_set_gid(entry, st->st_gid);
1312 			if ((parsed_kws & (MTREE_HAS_UID | MTREE_HAS_UNAME))
1313 				== 0 ||
1314 			    (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1315 				archive_entry_set_uid(entry, st->st_uid);
1316 			if ((parsed_kws & MTREE_HAS_MTIME) == 0 ||
1317 			    (parsed_kws & MTREE_HAS_NOCHANGE) != 0) {
1318 #if HAVE_STRUCT_STAT_ST_MTIMESPEC_TV_NSEC
1319 				archive_entry_set_mtime(entry, st->st_mtime,
1320 						st->st_mtimespec.tv_nsec);
1321 #elif HAVE_STRUCT_STAT_ST_MTIM_TV_NSEC
1322 				archive_entry_set_mtime(entry, st->st_mtime,
1323 						st->st_mtim.tv_nsec);
1324 #elif HAVE_STRUCT_STAT_ST_MTIME_N
1325 				archive_entry_set_mtime(entry, st->st_mtime,
1326 						st->st_mtime_n);
1327 #elif HAVE_STRUCT_STAT_ST_UMTIME
1328 				archive_entry_set_mtime(entry, st->st_mtime,
1329 						st->st_umtime*1000);
1330 #elif HAVE_STRUCT_STAT_ST_MTIME_USEC
1331 				archive_entry_set_mtime(entry, st->st_mtime,
1332 						st->st_mtime_usec*1000);
1333 #else
1334 				archive_entry_set_mtime(entry, st->st_mtime, 0);
1335 #endif
1336 			}
1337 			if ((parsed_kws & MTREE_HAS_NLINK) == 0 ||
1338 			    (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1339 				archive_entry_set_nlink(entry, st->st_nlink);
1340 			if ((parsed_kws & MTREE_HAS_PERM) == 0 ||
1341 			    (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1342 				archive_entry_set_perm(entry, st->st_mode);
1343 			if ((parsed_kws & MTREE_HAS_SIZE) == 0 ||
1344 			    (parsed_kws & MTREE_HAS_NOCHANGE) != 0)
1345 				archive_entry_set_size(entry, st->st_size);
1346 			archive_entry_set_ino(entry, st->st_ino);
1347 			archive_entry_set_dev(entry, st->st_dev);
1348 
1349 			archive_entry_linkify(mtree->resolver, &entry,
1350 				&sparse_entry);
1351 		} else if (parsed_kws & MTREE_HAS_OPTIONAL) {
1352 			/*
1353 			 * Couldn't open the entry, stat it or the on-disk type
1354 			 * didn't match.  If this entry is optional, just
1355 			 * ignore it and read the next header entry.
1356 			 */
1357 			*use_next = 1;
1358 			return ARCHIVE_OK;
1359 		}
1360 	}
1361 
1362 	mtree->cur_size = archive_entry_size(entry);
1363 	mtree->offset = 0;
1364 
1365 	return r;
1366 }
1367 
1368 /*
1369  * Each line contains a sequence of keywords.
1370  */
1371 static int
parse_line(struct archive_read * a,struct archive_entry * entry,struct mtree * mtree,struct mtree_entry * mp,int * parsed_kws)1372 parse_line(struct archive_read *a, struct archive_entry *entry,
1373     struct mtree *mtree, struct mtree_entry *mp, int *parsed_kws)
1374 {
1375 	struct mtree_option *iter;
1376 	int r = ARCHIVE_OK, r1;
1377 
1378 	for (iter = mp->options; iter != NULL; iter = iter->next) {
1379 		r1 = parse_keyword(a, mtree, entry, iter, parsed_kws);
1380 		if (r1 < r)
1381 			r = r1;
1382 	}
1383 	if (r == ARCHIVE_OK && (*parsed_kws & MTREE_HAS_TYPE) == 0) {
1384 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1385 		    "Missing type keyword in mtree specification");
1386 		return (ARCHIVE_WARN);
1387 	}
1388 	return (r);
1389 }
1390 
1391 /*
1392  * Device entries have one of the following forms:
1393  *  - raw dev_t
1394  *  - format,major,minor[,subdevice]
1395  * When parsing succeeded, `pdev' will contain the appropriate dev_t value.
1396  */
1397 
1398 /* strsep() is not in C90, but strcspn() is. */
1399 /* Taken from http://unixpapa.com/incnote/string.html */
1400 static char *
la_strsep(char ** sp,const char * sep)1401 la_strsep(char **sp, const char *sep)
1402 {
1403 	char *p, *s;
1404 	if (sp == NULL || *sp == NULL || **sp == '\0')
1405 		return(NULL);
1406 	s = *sp;
1407 	p = s + strcspn(s, sep);
1408 	if (*p != '\0')
1409 		*p++ = '\0';
1410 	*sp = p;
1411 	return(s);
1412 }
1413 
1414 static int
parse_device(dev_t * pdev,struct archive * a,char * val)1415 parse_device(dev_t *pdev, struct archive *a, char *val)
1416 {
1417 #define MAX_PACK_ARGS 3
1418 	unsigned long numbers[MAX_PACK_ARGS];
1419 	char *p, *dev;
1420 	int argc;
1421 	pack_t *pack;
1422 	dev_t result;
1423 	const char *error = NULL;
1424 
1425 	memset(pdev, 0, sizeof(*pdev));
1426 	if ((dev = strchr(val, ',')) != NULL) {
1427 		/*
1428 		 * Device's major/minor are given in a specified format.
1429 		 * Decode and pack it accordingly.
1430 		 */
1431 		*dev++ = '\0';
1432 		if ((pack = pack_find(val)) == NULL) {
1433 			archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1434 			    "Unknown format `%s'", val);
1435 			return ARCHIVE_WARN;
1436 		}
1437 		argc = 0;
1438 		while ((p = la_strsep(&dev, ",")) != NULL) {
1439 			if (*p == '\0') {
1440 				archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1441 				    "Missing number");
1442 				return ARCHIVE_WARN;
1443 			}
1444 			if (argc >= MAX_PACK_ARGS) {
1445 				archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1446 				    "Too many arguments");
1447 				return ARCHIVE_WARN;
1448 			}
1449 			numbers[argc++] = (unsigned long)mtree_atol(&p, 0);
1450 		}
1451 		if (argc < 2) {
1452 			archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1453 			    "Not enough arguments");
1454 			return ARCHIVE_WARN;
1455 		}
1456 		result = (*pack)(argc, numbers, &error);
1457 		if (error != NULL) {
1458 			archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
1459 			    "%s", error);
1460 			return ARCHIVE_WARN;
1461 		}
1462 	} else {
1463 		/* file system raw value. */
1464 		result = (dev_t)mtree_atol(&val, 0);
1465 	}
1466 	*pdev = result;
1467 	return ARCHIVE_OK;
1468 #undef MAX_PACK_ARGS
1469 }
1470 
1471 static int
parse_hex_nibble(char c)1472 parse_hex_nibble(char c)
1473 {
1474 	if (c >= '0' && c <= '9')
1475 		return c - '0';
1476 	if (c >= 'a' && c <= 'f')
1477 		return 10 + c - 'a';
1478 #if 0
1479 	/* XXX: Is uppercase something we should support? */
1480 	if (c >= 'A' && c <= 'F')
1481 		return 10 + c - 'A';
1482 #endif
1483 
1484 	return -1;
1485 }
1486 
1487 static int
parse_digest(struct archive_read * a,struct archive_entry * entry,const char * digest,int type)1488 parse_digest(struct archive_read *a, struct archive_entry *entry,
1489     const char *digest, int type)
1490 {
1491 	unsigned char digest_buf[64];
1492 	int high, low;
1493 	size_t i, j, len;
1494 
1495 	switch (type) {
1496 	case ARCHIVE_ENTRY_DIGEST_MD5:
1497 		len = sizeof(entry->digest.md5);
1498 		break;
1499 	case ARCHIVE_ENTRY_DIGEST_RMD160:
1500 		len = sizeof(entry->digest.rmd160);
1501 		break;
1502 	case ARCHIVE_ENTRY_DIGEST_SHA1:
1503 		len = sizeof(entry->digest.sha1);
1504 		break;
1505 	case ARCHIVE_ENTRY_DIGEST_SHA256:
1506 		len = sizeof(entry->digest.sha256);
1507 		break;
1508 	case ARCHIVE_ENTRY_DIGEST_SHA384:
1509 		len = sizeof(entry->digest.sha384);
1510 		break;
1511 	case ARCHIVE_ENTRY_DIGEST_SHA512:
1512 		len = sizeof(entry->digest.sha512);
1513 		break;
1514 	default:
1515 		archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1516 			"Internal error: Unknown digest type");
1517 		return ARCHIVE_FATAL;
1518 	}
1519 
1520 	if (len > sizeof(digest_buf)) {
1521 		archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
1522 			"Internal error: Digest storage too large");
1523 		return ARCHIVE_FATAL;
1524 	}
1525 
1526 	len *= 2;
1527 
1528 	if (mtree_strnlen(digest, len+1) != len) {
1529 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1530 				  "incorrect digest length, ignoring");
1531 		return ARCHIVE_WARN;
1532 	}
1533 
1534 	for (i = 0, j = 0; i < len; i += 2, j++) {
1535 		high = parse_hex_nibble(digest[i]);
1536 		low = parse_hex_nibble(digest[i+1]);
1537 		if (high == -1 || low == -1) {
1538 			archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1539 					  "invalid digest data, ignoring");
1540 			return ARCHIVE_WARN;
1541 		}
1542 
1543 		digest_buf[j] = high << 4 | low;
1544 	}
1545 
1546 	return archive_entry_set_digest(entry, type, digest_buf);
1547 }
1548 
1549 /*
1550  * Parse a single keyword and its value.
1551  */
1552 static int
parse_keyword(struct archive_read * a,struct mtree * mtree,struct archive_entry * entry,struct mtree_option * opt,int * parsed_kws)1553 parse_keyword(struct archive_read *a, struct mtree *mtree,
1554     struct archive_entry *entry, struct mtree_option *opt, int *parsed_kws)
1555 {
1556 	char *val, *key;
1557 
1558 	key = opt->value;
1559 
1560 	if (*key == '\0')
1561 		return (ARCHIVE_OK);
1562 
1563 	if (strcmp(key, "nochange") == 0) {
1564 		*parsed_kws |= MTREE_HAS_NOCHANGE;
1565 		return (ARCHIVE_OK);
1566 	}
1567 	if (strcmp(key, "optional") == 0) {
1568 		*parsed_kws |= MTREE_HAS_OPTIONAL;
1569 		return (ARCHIVE_OK);
1570 	}
1571 	if (strcmp(key, "ignore") == 0) {
1572 		/*
1573 		 * The mtree processing is not recursive, so
1574 		 * recursion will only happen for explicitly listed
1575 		 * entries.
1576 		 */
1577 		return (ARCHIVE_OK);
1578 	}
1579 
1580 	val = strchr(key, '=');
1581 	if (val == NULL) {
1582 		archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1583 		    "Malformed attribute \"%s\" (%d)", key, key[0]);
1584 		return (ARCHIVE_WARN);
1585 	}
1586 
1587 	*val = '\0';
1588 	++val;
1589 
1590 	switch (key[0]) {
1591 	case 'c':
1592 		if (strcmp(key, "content") == 0
1593 		    || strcmp(key, "contents") == 0) {
1594 			parse_escapes(val, NULL);
1595 			archive_strcpy(&mtree->contents_name, val);
1596 			return (ARCHIVE_OK);
1597 		}
1598 		if (strcmp(key, "cksum") == 0)
1599 			return (ARCHIVE_OK);
1600 		break;
1601 	case 'd':
1602 		if (strcmp(key, "device") == 0) {
1603 			/* stat(2) st_rdev field, e.g. the major/minor IDs
1604 			 * of a char/block special file */
1605 			int r;
1606 			dev_t dev;
1607 
1608 			*parsed_kws |= MTREE_HAS_DEVICE;
1609 			r = parse_device(&dev, &a->archive, val);
1610 			if (r == ARCHIVE_OK)
1611 				archive_entry_set_rdev(entry, dev);
1612 			return r;
1613 		}
1614 		break;
1615 	case 'f':
1616 		if (strcmp(key, "flags") == 0) {
1617 			*parsed_kws |= MTREE_HAS_FFLAGS;
1618 			archive_entry_copy_fflags_text(entry, val);
1619 			return (ARCHIVE_OK);
1620 		}
1621 		break;
1622 	case 'g':
1623 		if (strcmp(key, "gid") == 0) {
1624 			*parsed_kws |= MTREE_HAS_GID;
1625 			archive_entry_set_gid(entry, mtree_atol(&val, 10));
1626 			return (ARCHIVE_OK);
1627 		}
1628 		if (strcmp(key, "gname") == 0) {
1629 			*parsed_kws |= MTREE_HAS_GNAME;
1630 			archive_entry_copy_gname(entry, val);
1631 			return (ARCHIVE_OK);
1632 		}
1633 		break;
1634 	case 'i':
1635 		if (strcmp(key, "inode") == 0) {
1636 			archive_entry_set_ino(entry, mtree_atol(&val, 10));
1637 			return (ARCHIVE_OK);
1638 		}
1639 		break;
1640 	case 'l':
1641 		if (strcmp(key, "link") == 0) {
1642 			parse_escapes(val, NULL);
1643 			archive_entry_copy_symlink(entry, val);
1644 			return (ARCHIVE_OK);
1645 		}
1646 		break;
1647 	case 'm':
1648 		if (strcmp(key, "md5") == 0 || strcmp(key, "md5digest") == 0) {
1649 			return parse_digest(a, entry, val,
1650 			    ARCHIVE_ENTRY_DIGEST_MD5);
1651 		}
1652 		if (strcmp(key, "mode") == 0) {
1653 			if (val[0] < '0' || val[0] > '7') {
1654 				archive_set_error(&a->archive,
1655 				    ARCHIVE_ERRNO_FILE_FORMAT,
1656 				    "Symbolic or non-octal mode \"%s\" unsupported", val);
1657 				return (ARCHIVE_WARN);
1658 			}
1659 			*parsed_kws |= MTREE_HAS_PERM;
1660 			archive_entry_set_perm(entry, (mode_t)mtree_atol(&val, 8));
1661 			return (ARCHIVE_OK);
1662 		}
1663 		break;
1664 	case 'n':
1665 		if (strcmp(key, "nlink") == 0) {
1666 			*parsed_kws |= MTREE_HAS_NLINK;
1667 			archive_entry_set_nlink(entry,
1668 				(unsigned int)mtree_atol(&val, 10));
1669 			return (ARCHIVE_OK);
1670 		}
1671 		break;
1672 	case 'r':
1673 		if (strcmp(key, "resdevice") == 0) {
1674 			/* stat(2) st_dev field, e.g. the device ID where the
1675 			 * inode resides */
1676 			int r;
1677 			dev_t dev;
1678 
1679 			r = parse_device(&dev, &a->archive, val);
1680 			if (r == ARCHIVE_OK)
1681 				archive_entry_set_dev(entry, dev);
1682 			return r;
1683 		}
1684 		if (strcmp(key, "rmd160") == 0 ||
1685 		    strcmp(key, "rmd160digest") == 0) {
1686 			return parse_digest(a, entry, val,
1687 			    ARCHIVE_ENTRY_DIGEST_RMD160);
1688 		}
1689 		break;
1690 	case 's':
1691 		if (strcmp(key, "sha1") == 0 ||
1692 		    strcmp(key, "sha1digest") == 0) {
1693 			return parse_digest(a, entry, val,
1694 			    ARCHIVE_ENTRY_DIGEST_SHA1);
1695 		}
1696 		if (strcmp(key, "sha256") == 0 ||
1697 		    strcmp(key, "sha256digest") == 0) {
1698 			return parse_digest(a, entry, val,
1699 			    ARCHIVE_ENTRY_DIGEST_SHA256);
1700 		}
1701 		if (strcmp(key, "sha384") == 0 ||
1702 		    strcmp(key, "sha384digest") == 0) {
1703 			return parse_digest(a, entry, val,
1704 			    ARCHIVE_ENTRY_DIGEST_SHA384);
1705 		}
1706 		if (strcmp(key, "sha512") == 0 ||
1707 		    strcmp(key, "sha512digest") == 0) {
1708 			return parse_digest(a, entry, val,
1709 			    ARCHIVE_ENTRY_DIGEST_SHA512);
1710 		}
1711 		if (strcmp(key, "size") == 0) {
1712 			archive_entry_set_size(entry, mtree_atol(&val, 10));
1713 			return (ARCHIVE_OK);
1714 		}
1715 		break;
1716 	case 't':
1717 		if (strcmp(key, "tags") == 0) {
1718 			/*
1719 			 * Comma delimited list of tags.
1720 			 * Ignore the tags for now, but the interface
1721 			 * should be extended to allow inclusion/exclusion.
1722 			 */
1723 			return (ARCHIVE_OK);
1724 		}
1725 		if (strcmp(key, "time") == 0) {
1726 			int64_t m;
1727 			long ns = 0;
1728 
1729 			*parsed_kws |= MTREE_HAS_MTIME;
1730 			m = mtree_atol(&val, 10);
1731 			/* Replicate an old mtree bug:
1732 			 * 123456789.1 represents 123456789
1733 			 * seconds and 1 nanosecond. */
1734 			if (*val == '.') {
1735 				int64_t v;
1736 
1737 				++val;
1738 				v = mtree_atol(&val, 10);
1739 				if (v < 0)
1740 					ns = 0;
1741 				else if (v > 999999999)
1742 					ns = 999999999;
1743 				else
1744 					ns = (long)v;
1745 			}
1746 			if (m > TIME_MAX)
1747 				m = TIME_MAX;
1748 			else if (m < TIME_MIN)
1749 				m = TIME_MIN;
1750 			archive_entry_set_mtime(entry, (time_t)m, ns);
1751 			return (ARCHIVE_OK);
1752 		}
1753 		if (strcmp(key, "type") == 0) {
1754 			switch (val[0]) {
1755 			case 'b':
1756 				if (strcmp(val, "block") == 0) {
1757 					*parsed_kws |= MTREE_HAS_TYPE;
1758 					archive_entry_set_filetype(entry,
1759 						AE_IFBLK);
1760 					return (ARCHIVE_OK);
1761 				}
1762 				break;
1763 			case 'c':
1764 				if (strcmp(val, "char") == 0) {
1765 					*parsed_kws |= MTREE_HAS_TYPE;
1766 					archive_entry_set_filetype(entry,
1767 						AE_IFCHR);
1768 					return (ARCHIVE_OK);
1769 				}
1770 				break;
1771 			case 'd':
1772 				if (strcmp(val, "dir") == 0) {
1773 					*parsed_kws |= MTREE_HAS_TYPE;
1774 					archive_entry_set_filetype(entry,
1775 						AE_IFDIR);
1776 					return (ARCHIVE_OK);
1777 				}
1778 				break;
1779 			case 'f':
1780 				if (strcmp(val, "fifo") == 0) {
1781 					*parsed_kws |= MTREE_HAS_TYPE;
1782 					archive_entry_set_filetype(entry,
1783 						AE_IFIFO);
1784 					return (ARCHIVE_OK);
1785 				}
1786 				if (strcmp(val, "file") == 0) {
1787 					*parsed_kws |= MTREE_HAS_TYPE;
1788 					archive_entry_set_filetype(entry,
1789 						AE_IFREG);
1790 					return (ARCHIVE_OK);
1791 				}
1792 				break;
1793 			case 'l':
1794 				if (strcmp(val, "link") == 0) {
1795 					*parsed_kws |= MTREE_HAS_TYPE;
1796 					archive_entry_set_filetype(entry,
1797 						AE_IFLNK);
1798 					return (ARCHIVE_OK);
1799 				}
1800 				break;
1801 			default:
1802 				break;
1803 			}
1804 			archive_set_error(&a->archive,
1805 			    ARCHIVE_ERRNO_FILE_FORMAT,
1806 			    "Unrecognized file type \"%s\"; "
1807 			    "assuming \"file\"", val);
1808 			archive_entry_set_filetype(entry, AE_IFREG);
1809 			return (ARCHIVE_WARN);
1810 		}
1811 		break;
1812 	case 'u':
1813 		if (strcmp(key, "uid") == 0) {
1814 			*parsed_kws |= MTREE_HAS_UID;
1815 			archive_entry_set_uid(entry, mtree_atol(&val, 10));
1816 			return (ARCHIVE_OK);
1817 		}
1818 		if (strcmp(key, "uname") == 0) {
1819 			*parsed_kws |= MTREE_HAS_UNAME;
1820 			archive_entry_copy_uname(entry, val);
1821 			return (ARCHIVE_OK);
1822 		}
1823 		break;
1824 	default:
1825 		break;
1826 	}
1827 	archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1828 	    "Unrecognized key %s=%s", key, val);
1829 	return (ARCHIVE_WARN);
1830 }
1831 
1832 static int
read_data(struct archive_read * a,const void ** buff,size_t * size,int64_t * offset)1833 read_data(struct archive_read *a, const void **buff, size_t *size,
1834     int64_t *offset)
1835 {
1836 	struct mtree *mtree = a->format->data;
1837 	size_t bytes_to_read;
1838 	ssize_t bytes_read;
1839 
1840 	if (mtree->fd < 0) {
1841 		*buff = NULL;
1842 		*offset = 0;
1843 		*size = 0;
1844 		return (ARCHIVE_EOF);
1845 	}
1846 	if (mtree->buff == NULL) {
1847 		mtree->buffsize = 64 * 1024;
1848 		mtree->buff = malloc(mtree->buffsize);
1849 		if (mtree->buff == NULL) {
1850 			archive_set_error(&a->archive, ENOMEM,
1851 			    "Can't allocate memory");
1852 			return (ARCHIVE_FATAL);
1853 		}
1854 	}
1855 
1856 	*buff = mtree->buff;
1857 	*offset = mtree->offset;
1858 	if ((int64_t)mtree->buffsize > mtree->cur_size - mtree->offset)
1859 		bytes_to_read = (size_t)(mtree->cur_size - mtree->offset);
1860 	else
1861 		bytes_to_read = mtree->buffsize;
1862 	bytes_read = read(mtree->fd, mtree->buff, bytes_to_read);
1863 	if (bytes_read < 0) {
1864 		archive_set_error(&a->archive, errno, "Can't read");
1865 		return (ARCHIVE_WARN);
1866 	}
1867 	if (bytes_read == 0) {
1868 		*size = 0;
1869 		return (ARCHIVE_EOF);
1870 	}
1871 	mtree->offset += bytes_read;
1872 	*size = bytes_read;
1873 	return (ARCHIVE_OK);
1874 }
1875 
1876 /* Skip does nothing except possibly close the contents file. */
1877 static int
skip(struct archive_read * a)1878 skip(struct archive_read *a)
1879 {
1880 	struct mtree *mtree = a->format->data;
1881 
1882 	if (mtree->fd >= 0) {
1883 		close(mtree->fd);
1884 		mtree->fd = -1;
1885 	}
1886 	return (ARCHIVE_OK);
1887 }
1888 
1889 /*
1890  * Since parsing backslash sequences always makes strings shorter,
1891  * we can always do this conversion in-place.
1892  */
1893 static void
parse_escapes(char * src,struct mtree_entry * mentry)1894 parse_escapes(char *src, struct mtree_entry *mentry)
1895 {
1896 	char *dest = src;
1897 	char c;
1898 
1899 	if (mentry != NULL && strcmp(src, ".") == 0)
1900 		mentry->full = 1;
1901 
1902 	while (*src != '\0') {
1903 		c = *src++;
1904 		if (c == '/' && mentry != NULL)
1905 			mentry->full = 1;
1906 		if (c == '\\') {
1907 			switch (src[0]) {
1908 			case '0':
1909 				if (src[1] < '0' || src[1] > '7') {
1910 					c = 0;
1911 					++src;
1912 					break;
1913 				}
1914 				/* FALLTHROUGH */
1915 			case '1':
1916 			case '2':
1917 			case '3':
1918 				if (src[1] >= '0' && src[1] <= '7' &&
1919 				    src[2] >= '0' && src[2] <= '7') {
1920 					c = (src[0] - '0') << 6;
1921 					c |= (src[1] - '0') << 3;
1922 					c |= (src[2] - '0');
1923 					src += 3;
1924 				}
1925 				break;
1926 			case 'a':
1927 				c = '\a';
1928 				++src;
1929 				break;
1930 			case 'b':
1931 				c = '\b';
1932 				++src;
1933 				break;
1934 			case 'f':
1935 				c = '\f';
1936 				++src;
1937 				break;
1938 			case 'n':
1939 				c = '\n';
1940 				++src;
1941 				break;
1942 			case 'r':
1943 				c = '\r';
1944 				++src;
1945 				break;
1946 			case 's':
1947 				c = ' ';
1948 				++src;
1949 				break;
1950 			case 't':
1951 				c = '\t';
1952 				++src;
1953 				break;
1954 			case 'v':
1955 				c = '\v';
1956 				++src;
1957 				break;
1958 			case '\\':
1959 				c = '\\';
1960 				++src;
1961 				break;
1962 			}
1963 		}
1964 		*dest++ = c;
1965 	}
1966 	*dest = '\0';
1967 }
1968 
1969 /* Parse a hex digit. */
1970 static int
parsedigit(char c)1971 parsedigit(char c)
1972 {
1973 	if (c >= '0' && c <= '9')
1974 		return c - '0';
1975 	else if (c >= 'a' && c <= 'f')
1976 		return 10 + c - 'a';
1977 	else if (c >= 'A' && c <= 'F')
1978 		return 10 + c - 'A';
1979 	else
1980 		return -1;
1981 }
1982 
1983 /*
1984  * Note that this implementation does not (and should not!) obey
1985  * locale settings; you cannot simply substitute strtol here, since
1986  * it does obey locale.
1987  */
1988 static int64_t
mtree_atol(char ** p,int base)1989 mtree_atol(char **p, int base)
1990 {
1991 	int64_t l;
1992 	int digit;
1993 
1994 	if (base == 0) {
1995 		if (**p != '0')
1996 			base = 10;
1997 		else if ((*p)[1] == 'x' || (*p)[1] == 'X') {
1998 			*p += 2;
1999 			base = 16;
2000 		} else {
2001 			base = 8;
2002 		}
2003 	}
2004 
2005 	if (**p == '-') {
2006 		++(*p);
2007 
2008 		l = 0;
2009 		digit = parsedigit(**p);
2010 		while (digit >= 0 && digit < base) {
2011 			if (archive_ckd_mul_i64(&l, l, base) ||
2012 			    archive_ckd_sub_i64(&l, l, digit))
2013 				return INT64_MIN;
2014 			digit = parsedigit(*++(*p));
2015 		}
2016 		return l;
2017 	} else {
2018 		l = 0;
2019 		digit = parsedigit(**p);
2020 		while (digit >= 0 && digit < base) {
2021 			if (archive_ckd_mul_i64(&l, l, base) ||
2022 			    archive_ckd_add_i64(&l, l, digit))
2023 				return INT64_MAX;
2024 			digit = parsedigit(*++(*p));
2025 		}
2026 		return l;
2027 	}
2028 }
2029 
2030 /*
2031  * Returns length of line (including trailing newline)
2032  * or negative on error.  'start' argument is updated to
2033  * point to first character of line.
2034  */
2035 static ssize_t
readline(struct archive_read * a,struct mtree * mtree,char ** start,ssize_t limit)2036 readline(struct archive_read *a, struct mtree *mtree, char **start,
2037     ssize_t limit)
2038 {
2039 	ssize_t bytes_read;
2040 	ssize_t total_size = 0;
2041 	ssize_t find_off = 0;
2042 	const void *nl, *t;
2043 	char *u;
2044 
2045 	/* Accumulate line in a line buffer. */
2046 	for (;;) {
2047 		/* Read some more. */
2048 		t = __archive_read_ahead(a, 1, &bytes_read);
2049 		if (t == NULL)
2050 			return (0);
2051 		if (bytes_read < 0)
2052 			return (ARCHIVE_FATAL);
2053 		nl = memchr(t, '\n', bytes_read);
2054 		/* If we found '\n', trim the read to end exactly there. */
2055 		if (nl != NULL) {
2056 			bytes_read = ((const char *)nl) - ((const char *)t) + 1;
2057 		}
2058 		if (total_size + bytes_read + 1 > limit) {
2059 			archive_set_error(&a->archive,
2060 			    ARCHIVE_ERRNO_FILE_FORMAT,
2061 			    "Line too long");
2062 			return (ARCHIVE_FATAL);
2063 		}
2064 		if (archive_string_ensure(&mtree->line,
2065 			total_size + bytes_read + 1) == NULL) {
2066 			archive_set_error(&a->archive, ENOMEM,
2067 			    "Can't allocate working buffer");
2068 			return (ARCHIVE_FATAL);
2069 		}
2070 		/* Append new bytes to string. */
2071 		memcpy(mtree->line.s + total_size, t, bytes_read);
2072 		__archive_read_consume(a, bytes_read);
2073 		total_size += bytes_read;
2074 		mtree->line.s[total_size] = '\0';
2075 
2076 		for (u = mtree->line.s + find_off; *u; ++u) {
2077 			if (u[0] == '\n') {
2078 				/* Ends with unescaped newline. */
2079 				/* Check if preceded by '\r' for CRLF handling */
2080 				if (u > mtree->line.s && u[-1] == '\r') {
2081 					/* CRLF ending - remove the '\r' */
2082 					u[-1] = '\n';
2083 					u[0] = '\0';
2084 					total_size--;
2085 				}
2086 				*start = mtree->line.s;
2087 				return total_size;
2088 			} else if (u[0] == '#') {
2089 				/* Ends with comment sequence #...\n */
2090 				if (nl == NULL) {
2091 					/* But we've not found the \n yet */
2092 					break;
2093 				}
2094 			} else if (u[0] == '\\') {
2095 				if (u[1] == '\n') {
2096 					/* Trim escaped newline. */
2097 					total_size -= 2;
2098 					mtree->line.s[total_size] = '\0';
2099 					break;
2100 				} else if (u[1] == '\r' && u[2] == '\n') {
2101 					/* Trim escaped CRLF. */
2102 					total_size -= 3;
2103 					mtree->line.s[total_size] = '\0';
2104 					break;
2105 				} else if (u[1] != '\0') {
2106 					/* Skip the two-char escape sequence */
2107 					++u;
2108 				}
2109 			}
2110 		}
2111 		find_off = u - mtree->line.s;
2112 	}
2113 }
2114