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