Lines 1-5
Link Here
|
1 |
/*- |
1 |
/*- |
2 |
* Copyright (c) 2003-2007 Tim Kientzle |
2 |
* Copyright (c) 2003-2023 Tim Kientzle |
3 |
* Copyright (c) 2011-2012 Michihiro NAKAJIMA |
3 |
* Copyright (c) 2011-2012 Michihiro NAKAJIMA |
4 |
* Copyright (c) 2016 Martin Matuska |
4 |
* Copyright (c) 2016 Martin Matuska |
5 |
* All rights reserved. |
5 |
* All rights reserved. |
Lines 117-149
struct sparse_block {
Link Here
|
117 |
}; |
117 |
}; |
118 |
|
118 |
|
119 |
struct tar { |
119 |
struct tar { |
120 |
struct archive_string acl_text; |
|
|
121 |
struct archive_string entry_pathname; |
120 |
struct archive_string entry_pathname; |
122 |
/* For "GNU.sparse.name" and other similar path extensions. */ |
121 |
/* For "GNU.sparse.name" and other similar path extensions. */ |
123 |
struct archive_string entry_pathname_override; |
122 |
struct archive_string entry_pathname_override; |
124 |
struct archive_string entry_linkpath; |
|
|
125 |
struct archive_string entry_uname; |
123 |
struct archive_string entry_uname; |
126 |
struct archive_string entry_gname; |
124 |
struct archive_string entry_gname; |
127 |
struct archive_string longlink; |
125 |
struct archive_string entry_linkpath; |
128 |
struct archive_string longname; |
|
|
129 |
struct archive_string pax_header; |
130 |
struct archive_string pax_global; |
131 |
struct archive_string line; |
126 |
struct archive_string line; |
132 |
int pax_hdrcharset_binary; |
127 |
int pax_hdrcharset_utf8; |
133 |
int header_recursion_depth; |
|
|
134 |
int64_t entry_bytes_remaining; |
128 |
int64_t entry_bytes_remaining; |
135 |
int64_t entry_offset; |
129 |
int64_t entry_offset; |
136 |
int64_t entry_padding; |
130 |
int64_t entry_padding; |
137 |
int64_t entry_bytes_unconsumed; |
131 |
int64_t entry_bytes_unconsumed; |
138 |
int64_t realsize; |
132 |
int64_t realsize; |
139 |
int sparse_allowed; |
|
|
140 |
struct sparse_block *sparse_list; |
133 |
struct sparse_block *sparse_list; |
141 |
struct sparse_block *sparse_last; |
134 |
struct sparse_block *sparse_last; |
142 |
int64_t sparse_offset; |
135 |
int64_t sparse_offset; |
143 |
int64_t sparse_numbytes; |
136 |
int64_t sparse_numbytes; |
144 |
int sparse_gnu_major; |
137 |
int sparse_gnu_major; |
145 |
int sparse_gnu_minor; |
138 |
int sparse_gnu_minor; |
146 |
char sparse_gnu_pending; |
139 |
char sparse_gnu_attributes_seen; |
|
|
140 |
char filetype; |
147 |
|
141 |
|
148 |
struct archive_string localname; |
142 |
struct archive_string localname; |
149 |
struct archive_string_conv *opt_sconv; |
143 |
struct archive_string_conv *opt_sconv; |
Lines 168-192
static int gnu_sparse_old_read(struct archive_read *, struct tar *,
Link Here
|
168 |
static int gnu_sparse_old_parse(struct archive_read *, struct tar *, |
162 |
static int gnu_sparse_old_parse(struct archive_read *, struct tar *, |
169 |
const struct gnu_sparse *sparse, int length); |
163 |
const struct gnu_sparse *sparse, int length); |
170 |
static int gnu_sparse_01_parse(struct archive_read *, struct tar *, |
164 |
static int gnu_sparse_01_parse(struct archive_read *, struct tar *, |
171 |
const char *); |
165 |
const char *, size_t); |
172 |
static ssize_t gnu_sparse_10_read(struct archive_read *, struct tar *, |
166 |
static ssize_t gnu_sparse_10_read(struct archive_read *, struct tar *, |
173 |
size_t *); |
167 |
size_t *); |
174 |
static int header_Solaris_ACL(struct archive_read *, struct tar *, |
168 |
static int header_Solaris_ACL(struct archive_read *, struct tar *, |
175 |
struct archive_entry *, const void *, size_t *); |
169 |
struct archive_entry *, const void *, size_t *); |
176 |
static int header_common(struct archive_read *, struct tar *, |
170 |
static int header_common(struct archive_read *, struct tar *, |
177 |
struct archive_entry *, const void *); |
171 |
struct archive_entry *, const void *); |
178 |
static int header_old_tar(struct archive_read *, struct tar *, |
172 |
static int header_old_tar(struct archive_read *, struct tar *, |
179 |
struct archive_entry *, const void *); |
173 |
struct archive_entry *, const void *); |
180 |
static int header_pax_extensions(struct archive_read *, struct tar *, |
174 |
static int header_pax_extension(struct archive_read *, struct tar *, |
181 |
struct archive_entry *, const void *, size_t *); |
175 |
struct archive_entry *, const void *, size_t *); |
182 |
static int header_pax_global(struct archive_read *, struct tar *, |
176 |
static int header_pax_global(struct archive_read *, struct tar *, |
183 |
struct archive_entry *, const void *h, size_t *); |
177 |
struct archive_entry *, const void *h, size_t *); |
184 |
static int header_longlink(struct archive_read *, struct tar *, |
178 |
static int header_gnu_longlink(struct archive_read *, struct tar *, |
185 |
struct archive_entry *, const void *h, size_t *); |
|
|
186 |
static int header_longname(struct archive_read *, struct tar *, |
187 |
struct archive_entry *, const void *h, size_t *); |
179 |
struct archive_entry *, const void *h, size_t *); |
188 |
static int read_mac_metadata_blob(struct archive_read *, struct tar *, |
180 |
static int header_gnu_longname(struct archive_read *, struct tar *, |
189 |
struct archive_entry *, const void *h, size_t *); |
181 |
struct archive_entry *, const void *h, size_t *); |
|
|
182 |
static int is_mac_metadata_entry(struct archive_entry *entry); |
183 |
static int read_mac_metadata_blob(struct archive_read *, |
184 |
struct archive_entry *, size_t *); |
190 |
static int header_volume(struct archive_read *, struct tar *, |
185 |
static int header_volume(struct archive_read *, struct tar *, |
191 |
struct archive_entry *, const void *h, size_t *); |
186 |
struct archive_entry *, const void *h, size_t *); |
192 |
static int header_ustar(struct archive_read *, struct tar *, |
187 |
static int header_ustar(struct archive_read *, struct tar *, |
Lines 204-224
static int archive_read_format_tar_read_header(struct archive_read *,
Link Here
|
204 |
struct archive_entry *); |
199 |
struct archive_entry *); |
205 |
static int checksum(struct archive_read *, const void *); |
200 |
static int checksum(struct archive_read *, const void *); |
206 |
static int pax_attribute(struct archive_read *, struct tar *, |
201 |
static int pax_attribute(struct archive_read *, struct tar *, |
207 |
struct archive_entry *, const char *key, const char *value, |
202 |
struct archive_entry *, const char *key, size_t key_length, |
208 |
size_t value_length); |
203 |
size_t value_length, size_t *unconsumed); |
209 |
static int pax_attribute_acl(struct archive_read *, struct tar *, |
204 |
static int pax_attribute_LIBARCHIVE_xattr(struct archive_entry *, |
210 |
struct archive_entry *, const char *, int); |
205 |
const char *, size_t, const char *, size_t); |
211 |
static int pax_attribute_xattr(struct archive_entry *, const char *, |
206 |
static int pax_attribute_SCHILY_acl(struct archive_read *, struct tar *, |
212 |
const char *); |
207 |
struct archive_entry *, size_t, int); |
213 |
static int pax_header(struct archive_read *, struct tar *, |
208 |
static int pax_attribute_SUN_holesdata(struct archive_read *, struct tar *, |
214 |
struct archive_entry *, struct archive_string *); |
209 |
struct archive_entry *, const char *, size_t); |
215 |
static void pax_time(const char *, int64_t *sec, long *nanos); |
210 |
static void pax_time(const char *, size_t, int64_t *sec, long *nanos); |
216 |
static ssize_t readline(struct archive_read *, struct tar *, const char **, |
211 |
static ssize_t readline(struct archive_read *, struct tar *, const char **, |
217 |
ssize_t limit, size_t *); |
212 |
ssize_t limit, size_t *); |
218 |
static int read_body_to_string(struct archive_read *, struct tar *, |
213 |
static int read_body_to_string(struct archive_read *, struct tar *, |
219 |
struct archive_string *, const void *h, size_t *); |
214 |
struct archive_string *, const void *h, size_t *); |
220 |
static int solaris_sparse_parse(struct archive_read *, struct tar *, |
215 |
static int read_bytes_to_string(struct archive_read *, |
221 |
struct archive_entry *, const char *); |
216 |
struct archive_string *, size_t, size_t *); |
222 |
static int64_t tar_atol(const char *, size_t); |
217 |
static int64_t tar_atol(const char *, size_t); |
223 |
static int64_t tar_atol10(const char *, size_t); |
218 |
static int64_t tar_atol10(const char *, size_t); |
224 |
static int64_t tar_atol256(const char *, size_t); |
219 |
static int64_t tar_atol256(const char *, size_t); |
Lines 226-234
static int64_t tar_atol8(const char *, size_t);
Link Here
|
226 |
static int tar_read_header(struct archive_read *, struct tar *, |
221 |
static int tar_read_header(struct archive_read *, struct tar *, |
227 |
struct archive_entry *, size_t *); |
222 |
struct archive_entry *, size_t *); |
228 |
static int tohex(int c); |
223 |
static int tohex(int c); |
229 |
static char *url_decode(const char *); |
224 |
static char *url_decode(const char *, size_t); |
230 |
static void tar_flush_unconsumed(struct archive_read *, size_t *); |
225 |
static void tar_flush_unconsumed(struct archive_read *, size_t *); |
231 |
|
226 |
|
|
|
227 |
/* Sanity limits: These numbers should be low enough to |
228 |
* prevent a maliciously-crafted archive from forcing us to |
229 |
* allocate extreme amounts of memory. But of course, they |
230 |
* need to be high enough for any correct value. These |
231 |
* will likely need some adjustment as we get more experience. */ |
232 |
static const size_t guname_limit = 65536; /* Longest uname or gname: 64kiB */ |
233 |
static const size_t pathname_limit = 1048576; /* Longest path name: 1MiB */ |
234 |
static const size_t sparse_map_limit = 8 * 1048576; /* Longest sparse map: 8MiB */ |
235 |
static const size_t xattr_limit = 16 * 1048576; /* Longest xattr: 16MiB */ |
236 |
static const size_t fflags_limit = 512; /* Longest fflags */ |
237 |
static const size_t acl_limit = 131072; /* Longest textual ACL: 128kiB */ |
238 |
static const int64_t entry_limit = 0xfffffffffffffffLL; /* 2^60 bytes = 1 ExbiByte */ |
232 |
|
239 |
|
233 |
int |
240 |
int |
234 |
archive_read_support_format_gnutar(struct archive *a) |
241 |
archive_read_support_format_gnutar(struct archive *a) |
Lines 249-255
archive_read_support_format_tar(struct archive *_a)
Link Here
|
249 |
archive_check_magic(_a, ARCHIVE_READ_MAGIC, |
256 |
archive_check_magic(_a, ARCHIVE_READ_MAGIC, |
250 |
ARCHIVE_STATE_NEW, "archive_read_support_format_tar"); |
257 |
ARCHIVE_STATE_NEW, "archive_read_support_format_tar"); |
251 |
|
258 |
|
252 |
tar = (struct tar *)calloc(1, sizeof(*tar)); |
259 |
tar = calloc(1, sizeof(*tar)); |
253 |
if (tar == NULL) { |
260 |
if (tar == NULL) { |
254 |
archive_set_error(&a->archive, ENOMEM, |
261 |
archive_set_error(&a->archive, ENOMEM, |
255 |
"Can't allocate tar data"); |
262 |
"Can't allocate tar data"); |
Lines 283-299
archive_read_format_tar_cleanup(struct archive_read *a)
Link Here
|
283 |
|
290 |
|
284 |
tar = (struct tar *)(a->format->data); |
291 |
tar = (struct tar *)(a->format->data); |
285 |
gnu_clear_sparse_list(tar); |
292 |
gnu_clear_sparse_list(tar); |
286 |
archive_string_free(&tar->acl_text); |
|
|
287 |
archive_string_free(&tar->entry_pathname); |
293 |
archive_string_free(&tar->entry_pathname); |
288 |
archive_string_free(&tar->entry_pathname_override); |
294 |
archive_string_free(&tar->entry_pathname_override); |
289 |
archive_string_free(&tar->entry_linkpath); |
|
|
290 |
archive_string_free(&tar->entry_uname); |
295 |
archive_string_free(&tar->entry_uname); |
291 |
archive_string_free(&tar->entry_gname); |
296 |
archive_string_free(&tar->entry_gname); |
|
|
297 |
archive_string_free(&tar->entry_linkpath); |
292 |
archive_string_free(&tar->line); |
298 |
archive_string_free(&tar->line); |
293 |
archive_string_free(&tar->pax_global); |
|
|
294 |
archive_string_free(&tar->pax_header); |
295 |
archive_string_free(&tar->longname); |
296 |
archive_string_free(&tar->longlink); |
297 |
archive_string_free(&tar->localname); |
299 |
archive_string_free(&tar->localname); |
298 |
free(tar); |
300 |
free(tar); |
299 |
(a->format->data) = NULL; |
301 |
(a->format->data) = NULL; |
Lines 505-510
archive_read_format_tar_read_header(struct archive_read *a,
Link Here
|
505 |
* probably not worthwhile just to support the relatively |
507 |
* probably not worthwhile just to support the relatively |
506 |
* obscure tar->cpio conversion case. |
508 |
* obscure tar->cpio conversion case. |
507 |
*/ |
509 |
*/ |
|
|
510 |
/* TODO: Move this into `struct tar` to avoid conflicts |
511 |
* when reading multiple archives */ |
508 |
static int default_inode; |
512 |
static int default_inode; |
509 |
static int default_dev; |
513 |
static int default_dev; |
510 |
struct tar *tar; |
514 |
struct tar *tar; |
Lines 623-633
archive_read_format_tar_read_data(struct archive_read *a,
Link Here
|
623 |
} |
627 |
} |
624 |
|
628 |
|
625 |
*buff = __archive_read_ahead(a, 1, &bytes_read); |
629 |
*buff = __archive_read_ahead(a, 1, &bytes_read); |
626 |
if (bytes_read < 0) |
|
|
627 |
return (ARCHIVE_FATAL); |
628 |
if (*buff == NULL) { |
630 |
if (*buff == NULL) { |
629 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
631 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
630 |
"Truncated tar archive"); |
632 |
"Truncated tar archive" |
|
|
633 |
" detected while reading data"); |
631 |
return (ARCHIVE_FATAL); |
634 |
return (ARCHIVE_FATAL); |
632 |
} |
635 |
} |
633 |
if (bytes_read > tar->entry_bytes_remaining) |
636 |
if (bytes_read > tar->entry_bytes_remaining) |
Lines 688-694
archive_read_format_tar_skip(struct archive_read *a)
Link Here
|
688 |
} |
691 |
} |
689 |
|
692 |
|
690 |
/* |
693 |
/* |
691 |
* This function recursively interprets all of the headers associated |
694 |
* This function reads and interprets all of the headers associated |
692 |
* with a single entry. |
695 |
* with a single entry. |
693 |
*/ |
696 |
*/ |
694 |
static int |
697 |
static int |
Lines 696-885
tar_read_header(struct archive_read *a, struct tar *tar,
Link Here
|
696 |
struct archive_entry *entry, size_t *unconsumed) |
699 |
struct archive_entry *entry, size_t *unconsumed) |
697 |
{ |
700 |
{ |
698 |
ssize_t bytes; |
701 |
ssize_t bytes; |
699 |
int err, eof_vol_header; |
702 |
int err = ARCHIVE_OK, err2; |
|
|
703 |
int eof_fatal = 0; /* EOF is okay at some points... */ |
700 |
const char *h; |
704 |
const char *h; |
701 |
const struct archive_entry_header_ustar *header; |
705 |
const struct archive_entry_header_ustar *header; |
702 |
const struct archive_entry_header_gnutar *gnuheader; |
706 |
const struct archive_entry_header_gnutar *gnuheader; |
703 |
|
707 |
|
704 |
eof_vol_header = 0; |
708 |
/* Bitmask of what header types we've seen. */ |
705 |
|
709 |
int32_t seen_headers = 0; |
706 |
/* Loop until we find a workable header record. */ |
710 |
static const int32_t seen_A_header = 1; |
707 |
for (;;) { |
711 |
static const int32_t seen_g_header = 2; |
708 |
tar_flush_unconsumed(a, unconsumed); |
712 |
static const int32_t seen_K_header = 4; |
|
|
713 |
static const int32_t seen_L_header = 8; |
714 |
static const int32_t seen_V_header = 16; |
715 |
static const int32_t seen_x_header = 32; /* Also X */ |
716 |
static const int32_t seen_mac_metadata = 512; |
717 |
|
718 |
tar->pax_hdrcharset_utf8 = 1; |
719 |
tar->sparse_gnu_attributes_seen = 0; |
720 |
archive_string_empty(&(tar->entry_gname)); |
721 |
archive_string_empty(&(tar->entry_pathname)); |
722 |
archive_string_empty(&(tar->entry_pathname_override)); |
723 |
archive_string_empty(&(tar->entry_uname)); |
724 |
archive_string_empty(&tar->entry_linkpath); |
709 |
|
725 |
|
710 |
/* Read 512-byte header record */ |
726 |
/* Ensure format is set. */ |
711 |
h = __archive_read_ahead(a, 512, &bytes); |
727 |
if (a->archive.archive_format_name == NULL) { |
712 |
if (bytes < 0) |
728 |
a->archive.archive_format = ARCHIVE_FORMAT_TAR; |
713 |
return ((int)bytes); |
729 |
a->archive.archive_format_name = "tar"; |
714 |
if (bytes == 0) { /* EOF at a block boundary. */ |
730 |
} |
715 |
/* Some writers do omit the block of nulls. <sigh> */ |
|
|
716 |
return (ARCHIVE_EOF); |
717 |
} |
718 |
if (bytes < 512) { /* Short block at EOF; this is bad. */ |
719 |
archive_set_error(&a->archive, |
720 |
ARCHIVE_ERRNO_FILE_FORMAT, |
721 |
"Truncated tar archive"); |
722 |
return (ARCHIVE_FATAL); |
723 |
} |
724 |
*unconsumed = 512; |
725 |
|
731 |
|
726 |
/* Header is workable if it's not an end-of-archive mark. */ |
732 |
/* |
727 |
if (h[0] != 0 || !archive_block_is_null(h)) |
733 |
* TODO: Write global/default pax options into |
728 |
break; |
734 |
* 'entry' struct here before overwriting with |
|
|
735 |
* file-specific options. |
736 |
*/ |
729 |
|
737 |
|
730 |
/* Ensure format is set for archives with only null blocks. */ |
738 |
/* Loop over all the headers needed for the next entry */ |
731 |
if (a->archive.archive_format_name == NULL) { |
739 |
for (;;) { |
732 |
a->archive.archive_format = ARCHIVE_FORMAT_TAR; |
|
|
733 |
a->archive.archive_format_name = "tar"; |
734 |
} |
735 |
|
740 |
|
736 |
if (!tar->read_concatenated_archives) { |
741 |
/* Find the next valid header record. */ |
737 |
/* Try to consume a second all-null record, as well. */ |
742 |
while (1) { |
738 |
tar_flush_unconsumed(a, unconsumed); |
743 |
tar_flush_unconsumed(a, unconsumed); |
739 |
h = __archive_read_ahead(a, 512, NULL); |
|
|
740 |
if (h != NULL && h[0] == 0 && archive_block_is_null(h)) |
741 |
__archive_read_consume(a, 512); |
742 |
archive_clear_error(&a->archive); |
743 |
return (ARCHIVE_EOF); |
744 |
} |
745 |
|
744 |
|
746 |
/* |
745 |
/* Read 512-byte header record */ |
747 |
* We're reading concatenated archives, ignore this block and |
746 |
h = __archive_read_ahead(a, 512, &bytes); |
748 |
* loop to get the next. |
747 |
if (bytes == 0) { /* EOF at a block boundary. */ |
749 |
*/ |
748 |
if (eof_fatal) { |
750 |
} |
749 |
/* We've read a special header already; |
|
|
750 |
* if there's no regular header, then this is |
751 |
* a premature EOF. */ |
752 |
archive_set_error(&a->archive, EINVAL, |
753 |
"Damaged tar archive"); |
754 |
return (ARCHIVE_FATAL); |
755 |
} else { |
756 |
return (ARCHIVE_EOF); |
757 |
} |
758 |
} |
759 |
if (h == NULL) { /* Short block at EOF; this is bad. */ |
760 |
archive_set_error(&a->archive, |
761 |
ARCHIVE_ERRNO_FILE_FORMAT, |
762 |
"Truncated tar archive" |
763 |
" detected while reading next heaader"); |
764 |
return (ARCHIVE_FATAL); |
765 |
} |
766 |
*unconsumed += 512; |
751 |
|
767 |
|
752 |
/* |
768 |
if (h[0] == 0 && archive_block_is_null(h)) { |
753 |
* Note: If the checksum fails and we return ARCHIVE_RETRY, |
769 |
/* We found a NULL block which indicates end-of-archive */ |
754 |
* then the client is likely to just retry. This is a very |
|
|
755 |
* crude way to search for the next valid header! |
756 |
* |
757 |
* TODO: Improve this by implementing a real header scan. |
758 |
*/ |
759 |
if (!checksum(a, h)) { |
760 |
tar_flush_unconsumed(a, unconsumed); |
761 |
archive_set_error(&a->archive, EINVAL, "Damaged tar archive"); |
762 |
return (ARCHIVE_RETRY); /* Retryable: Invalid header */ |
763 |
} |
764 |
|
770 |
|
765 |
if (++tar->header_recursion_depth > 32) { |
771 |
if (tar->read_concatenated_archives) { |
766 |
tar_flush_unconsumed(a, unconsumed); |
772 |
/* We're ignoring NULL blocks, so keep going. */ |
767 |
archive_set_error(&a->archive, EINVAL, "Too many special headers"); |
773 |
continue; |
768 |
return (ARCHIVE_WARN); |
774 |
} |
769 |
} |
|
|
770 |
|
775 |
|
771 |
/* Determine the format variant. */ |
776 |
/* Try to consume a second all-null record, as well. */ |
772 |
header = (const struct archive_entry_header_ustar *)h; |
777 |
/* If we can't, that's okay. */ |
|
|
778 |
tar_flush_unconsumed(a, unconsumed); |
779 |
h = __archive_read_ahead(a, 512, NULL); |
780 |
if (h != NULL && h[0] == 0 && archive_block_is_null(h)) |
781 |
__archive_read_consume(a, 512); |
773 |
|
782 |
|
774 |
switch(header->typeflag[0]) { |
783 |
archive_clear_error(&a->archive); |
775 |
case 'A': /* Solaris tar ACL */ |
784 |
return (ARCHIVE_EOF); |
776 |
a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE; |
|
|
777 |
a->archive.archive_format_name = "Solaris tar"; |
778 |
err = header_Solaris_ACL(a, tar, entry, h, unconsumed); |
779 |
break; |
780 |
case 'g': /* POSIX-standard 'g' header. */ |
781 |
a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE; |
782 |
a->archive.archive_format_name = "POSIX pax interchange format"; |
783 |
err = header_pax_global(a, tar, entry, h, unconsumed); |
784 |
if (err == ARCHIVE_EOF) |
785 |
return (err); |
786 |
break; |
787 |
case 'K': /* Long link name (GNU tar, others) */ |
788 |
err = header_longlink(a, tar, entry, h, unconsumed); |
789 |
break; |
790 |
case 'L': /* Long filename (GNU tar, others) */ |
791 |
err = header_longname(a, tar, entry, h, unconsumed); |
792 |
break; |
793 |
case 'V': /* GNU volume header */ |
794 |
err = header_volume(a, tar, entry, h, unconsumed); |
795 |
if (err == ARCHIVE_EOF) |
796 |
eof_vol_header = 1; |
797 |
break; |
798 |
case 'X': /* Used by SUN tar; same as 'x'. */ |
799 |
a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE; |
800 |
a->archive.archive_format_name = |
801 |
"POSIX pax interchange format (Sun variant)"; |
802 |
err = header_pax_extensions(a, tar, entry, h, unconsumed); |
803 |
break; |
804 |
case 'x': /* POSIX-standard 'x' header. */ |
805 |
a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE; |
806 |
a->archive.archive_format_name = "POSIX pax interchange format"; |
807 |
err = header_pax_extensions(a, tar, entry, h, unconsumed); |
808 |
break; |
809 |
default: |
810 |
gnuheader = (const struct archive_entry_header_gnutar *)h; |
811 |
if (memcmp(gnuheader->magic, "ustar \0", 8) == 0) { |
812 |
a->archive.archive_format = ARCHIVE_FORMAT_TAR_GNUTAR; |
813 |
a->archive.archive_format_name = "GNU tar format"; |
814 |
err = header_gnutar(a, tar, entry, h, unconsumed); |
815 |
} else if (memcmp(header->magic, "ustar", 5) == 0) { |
816 |
if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE) { |
817 |
a->archive.archive_format = ARCHIVE_FORMAT_TAR_USTAR; |
818 |
a->archive.archive_format_name = "POSIX ustar format"; |
819 |
} |
785 |
} |
820 |
err = header_ustar(a, tar, entry, h); |
|
|
821 |
} else { |
822 |
a->archive.archive_format = ARCHIVE_FORMAT_TAR; |
823 |
a->archive.archive_format_name = "tar (non-POSIX)"; |
824 |
err = header_old_tar(a, tar, entry, h); |
825 |
} |
826 |
} |
827 |
if (err == ARCHIVE_FATAL) |
828 |
return (err); |
829 |
|
830 |
tar_flush_unconsumed(a, unconsumed); |
831 |
|
786 |
|
832 |
h = NULL; |
787 |
/* This is NOT a null block, so it must be a valid header. */ |
833 |
header = NULL; |
788 |
if (!checksum(a, h)) { |
|
|
789 |
tar_flush_unconsumed(a, unconsumed); |
790 |
archive_set_error(&a->archive, EINVAL, "Damaged tar archive"); |
791 |
/* If we've read some critical information (pax headers, etc) |
792 |
* and _then_ see a bad header, we can't really recover. */ |
793 |
if (eof_fatal) { |
794 |
return (ARCHIVE_FATAL); |
795 |
} else { |
796 |
return (ARCHIVE_RETRY); |
797 |
} |
798 |
} |
799 |
break; |
800 |
} |
834 |
|
801 |
|
835 |
--tar->header_recursion_depth; |
802 |
/* Determine the format variant. */ |
836 |
/* Yuck. Apple's design here ends up storing long pathname |
803 |
header = (const struct archive_entry_header_ustar *)h; |
837 |
* extensions for both the AppleDouble extension entry and the |
804 |
switch(header->typeflag[0]) { |
838 |
* regular entry. |
805 |
case 'A': /* Solaris tar ACL */ |
839 |
*/ |
806 |
if (seen_headers & seen_A_header) { |
840 |
if ((err == ARCHIVE_WARN || err == ARCHIVE_OK) && |
807 |
return (ARCHIVE_FATAL); |
841 |
tar->header_recursion_depth == 0 && |
808 |
} |
842 |
tar->process_mac_extensions) { |
809 |
seen_headers |= seen_A_header; |
843 |
int err2 = read_mac_metadata_blob(a, tar, entry, h, unconsumed); |
810 |
a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE; |
844 |
if (err2 < err) |
811 |
a->archive.archive_format_name = "Solaris tar"; |
845 |
err = err2; |
812 |
err2 = header_Solaris_ACL(a, tar, entry, h, unconsumed); |
846 |
} |
813 |
break; |
847 |
|
814 |
case 'g': /* POSIX-standard 'g' header. */ |
848 |
/* We return warnings or success as-is. Anything else is fatal. */ |
815 |
if (seen_headers & seen_g_header) { |
849 |
if (err == ARCHIVE_WARN || err == ARCHIVE_OK) { |
816 |
return (ARCHIVE_FATAL); |
850 |
if (tar->sparse_gnu_pending) { |
817 |
} |
851 |
if (tar->sparse_gnu_major == 1 && |
818 |
seen_headers |= seen_g_header; |
852 |
tar->sparse_gnu_minor == 0) { |
819 |
a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE; |
853 |
ssize_t bytes_read; |
820 |
a->archive.archive_format_name = "POSIX pax interchange format"; |
854 |
|
821 |
err2 = header_pax_global(a, tar, entry, h, unconsumed); |
855 |
tar->sparse_gnu_pending = 0; |
822 |
break; |
856 |
/* Read initial sparse map. */ |
823 |
case 'K': /* Long link name (GNU tar, others) */ |
857 |
bytes_read = gnu_sparse_10_read(a, tar, unconsumed); |
824 |
if (seen_headers & seen_K_header) { |
858 |
if (bytes_read < 0) |
825 |
return (ARCHIVE_FATAL); |
859 |
return ((int)bytes_read); |
826 |
} |
860 |
tar->entry_bytes_remaining -= bytes_read; |
827 |
seen_headers |= seen_K_header; |
|
|
828 |
err2 = header_gnu_longlink(a, tar, entry, h, unconsumed); |
829 |
break; |
830 |
case 'L': /* Long filename (GNU tar, others) */ |
831 |
if (seen_headers & seen_L_header) { |
832 |
return (ARCHIVE_FATAL); |
833 |
} |
834 |
seen_headers |= seen_L_header; |
835 |
err2 = header_gnu_longname(a, tar, entry, h, unconsumed); |
836 |
break; |
837 |
case 'V': /* GNU volume header */ |
838 |
if (seen_headers & seen_V_header) { |
839 |
return (ARCHIVE_FATAL); |
840 |
} |
841 |
seen_headers |= seen_V_header; |
842 |
err2 = header_volume(a, tar, entry, h, unconsumed); |
843 |
break; |
844 |
case 'X': /* Used by SUN tar; same as 'x'. */ |
845 |
if (seen_headers & seen_x_header) { |
846 |
return (ARCHIVE_FATAL); |
847 |
} |
848 |
seen_headers |= seen_x_header; |
849 |
a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE; |
850 |
a->archive.archive_format_name = |
851 |
"POSIX pax interchange format (Sun variant)"; |
852 |
err2 = header_pax_extension(a, tar, entry, h, unconsumed); |
853 |
break; |
854 |
case 'x': /* POSIX-standard 'x' header. */ |
855 |
if (seen_headers & seen_x_header) { |
856 |
return (ARCHIVE_FATAL); |
857 |
} |
858 |
seen_headers |= seen_x_header; |
859 |
a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE; |
860 |
a->archive.archive_format_name = "POSIX pax interchange format"; |
861 |
err2 = header_pax_extension(a, tar, entry, h, unconsumed); |
862 |
break; |
863 |
default: /* Regular header: Legacy tar, GNU tar, or ustar */ |
864 |
gnuheader = (const struct archive_entry_header_gnutar *)h; |
865 |
if (memcmp(gnuheader->magic, "ustar \0", 8) == 0) { |
866 |
a->archive.archive_format = ARCHIVE_FORMAT_TAR_GNUTAR; |
867 |
a->archive.archive_format_name = "GNU tar format"; |
868 |
err2 = header_gnutar(a, tar, entry, h, unconsumed); |
869 |
} else if (memcmp(header->magic, "ustar", 5) == 0) { |
870 |
if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE) { |
871 |
a->archive.archive_format = ARCHIVE_FORMAT_TAR_USTAR; |
872 |
a->archive.archive_format_name = "POSIX ustar format"; |
873 |
} |
874 |
err2 = header_ustar(a, tar, entry, h); |
861 |
} else { |
875 |
} else { |
862 |
archive_set_error(&a->archive, |
876 |
a->archive.archive_format = ARCHIVE_FORMAT_TAR; |
863 |
ARCHIVE_ERRNO_MISC, |
877 |
a->archive.archive_format_name = "tar (non-POSIX)"; |
864 |
"Unrecognized GNU sparse file format"); |
878 |
err2 = header_old_tar(a, tar, entry, h); |
865 |
return (ARCHIVE_WARN); |
879 |
} |
|
|
880 |
err = err_combine(err, err2); |
881 |
/* We return warnings or success as-is. Anything else is fatal. */ |
882 |
if (err < ARCHIVE_WARN) { |
883 |
return (ARCHIVE_FATAL); |
884 |
} |
885 |
/* Filename of the form `._filename` is an AppleDouble |
886 |
* extension entry. The body is the macOS metadata blob; |
887 |
* this is followed by another entry with the actual |
888 |
* regular file data. |
889 |
* This design has two drawbacks: |
890 |
* = it's brittle; you might just have a file with such a name |
891 |
* = it duplicates any long pathname extensions |
892 |
* |
893 |
* TODO: This probably shouldn't be here at all. Consider |
894 |
* just returning the contents as a regular entry here and |
895 |
* then dealing with it when we write data to disk. |
896 |
*/ |
897 |
if (tar->process_mac_extensions |
898 |
&& ((seen_headers & seen_mac_metadata) == 0) |
899 |
&& is_mac_metadata_entry(entry)) { |
900 |
err2 = read_mac_metadata_blob(a, entry, unconsumed); |
901 |
if (err2 < ARCHIVE_WARN) { |
902 |
return (ARCHIVE_FATAL); |
903 |
} |
904 |
err = err_combine(err, err2); |
905 |
/* Note: Other headers can appear again. */ |
906 |
seen_headers = seen_mac_metadata; |
907 |
break; |
908 |
} |
909 |
|
910 |
/* Reconcile GNU sparse attributes */ |
911 |
if (tar->sparse_gnu_attributes_seen) { |
912 |
/* Only 'S' (GNU sparse) and ustar '0' regular files can be sparse */ |
913 |
if (tar->filetype != 'S' && tar->filetype != '0') { |
914 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
915 |
"Non-regular file cannot be sparse"); |
916 |
return (ARCHIVE_WARN); |
917 |
} else if (tar->sparse_gnu_major == 0 && |
918 |
tar->sparse_gnu_minor == 0) { |
919 |
/* Sparse map already parsed from 'x' header */ |
920 |
} else if (tar->sparse_gnu_major == 0 && |
921 |
tar->sparse_gnu_minor == 1) { |
922 |
/* Sparse map already parsed from 'x' header */ |
923 |
} else if (tar->sparse_gnu_major == 1 && |
924 |
tar->sparse_gnu_minor == 0) { |
925 |
/* Sparse map is prepended to file contents */ |
926 |
ssize_t bytes_read; |
927 |
bytes_read = gnu_sparse_10_read(a, tar, unconsumed); |
928 |
if (bytes_read < 0) |
929 |
return ((int)bytes_read); |
930 |
tar->entry_bytes_remaining -= bytes_read; |
931 |
} else { |
932 |
archive_set_error(&a->archive, |
933 |
ARCHIVE_ERRNO_MISC, |
934 |
"Unrecognized GNU sparse file format"); |
935 |
return (ARCHIVE_WARN); |
936 |
} |
866 |
} |
937 |
} |
867 |
tar->sparse_gnu_pending = 0; |
938 |
return (err); |
868 |
} |
939 |
} |
869 |
return (err); |
940 |
|
870 |
} |
941 |
/* We're between headers ... */ |
871 |
if (err == ARCHIVE_EOF) { |
942 |
err = err_combine(err, err2); |
872 |
if (!eof_vol_header) { |
943 |
if (err == ARCHIVE_FATAL) |
873 |
/* EOF when recursively reading a header is bad. */ |
944 |
return (err); |
874 |
archive_set_error(&a->archive, EINVAL, |
945 |
|
875 |
"Damaged tar archive"); |
946 |
/* The GNU volume header and the pax `g` global header |
876 |
} else { |
947 |
* are both allowed to be the only header in an |
877 |
/* If we encounter just a GNU volume header treat |
948 |
* archive. If we've seen any other header, a |
878 |
* this situation as an empty archive */ |
949 |
* following EOF is fatal. */ |
879 |
return (ARCHIVE_EOF); |
950 |
if ((seen_headers & ~seen_V_header & ~seen_g_header) != 0) { |
|
|
951 |
eof_fatal = 1; |
880 |
} |
952 |
} |
881 |
} |
953 |
} |
882 |
return (ARCHIVE_FATAL); |
|
|
883 |
} |
954 |
} |
884 |
|
955 |
|
885 |
/* |
956 |
/* |
Lines 934-940
checksum(struct archive_read *a, const void *h)
Link Here
|
934 |
if (sum == check) |
1005 |
if (sum == check) |
935 |
return (1); |
1006 |
return (1); |
936 |
|
1007 |
|
|
|
1008 |
#if DONT_FAIL_ON_CRC_ERROR |
1009 |
/* Speed up fuzzing by pretending the checksum is always right. */ |
1010 |
return (1); |
1011 |
#else |
937 |
return (0); |
1012 |
return (0); |
|
|
1013 |
#endif |
938 |
} |
1014 |
} |
939 |
|
1015 |
|
940 |
/* |
1016 |
/* |
Lines 959-983
header_Solaris_ACL(struct archive_read *a, struct tar *tar,
Link Here
|
959 |
struct archive_entry *entry, const void *h, size_t *unconsumed) |
1035 |
struct archive_entry *entry, const void *h, size_t *unconsumed) |
960 |
{ |
1036 |
{ |
961 |
const struct archive_entry_header_ustar *header; |
1037 |
const struct archive_entry_header_ustar *header; |
|
|
1038 |
struct archive_string acl_text; |
962 |
size_t size; |
1039 |
size_t size; |
963 |
int err, acl_type; |
1040 |
int err, acl_type; |
964 |
int64_t type; |
1041 |
int64_t type; |
965 |
char *acl, *p; |
1042 |
char *acl, *p; |
966 |
|
1043 |
|
967 |
/* |
|
|
968 |
* read_body_to_string adds a NUL terminator, but we need a little |
969 |
* more to make sure that we don't overrun acl_text later. |
970 |
*/ |
971 |
header = (const struct archive_entry_header_ustar *)h; |
1044 |
header = (const struct archive_entry_header_ustar *)h; |
972 |
size = (size_t)tar_atol(header->size, sizeof(header->size)); |
1045 |
size = (size_t)tar_atol(header->size, sizeof(header->size)); |
973 |
err = read_body_to_string(a, tar, &(tar->acl_text), h, unconsumed); |
1046 |
archive_string_init(&acl_text); |
974 |
if (err != ARCHIVE_OK) |
1047 |
err = read_body_to_string(a, tar, &acl_text, h, unconsumed); |
975 |
return (err); |
1048 |
if (err != ARCHIVE_OK) { |
976 |
|
1049 |
archive_string_free(&acl_text); |
977 |
/* Recursively read next header */ |
|
|
978 |
err = tar_read_header(a, tar, entry, unconsumed); |
979 |
if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN)) |
980 |
return (err); |
1050 |
return (err); |
|
|
1051 |
} |
981 |
|
1052 |
|
982 |
/* TODO: Examine the first characters to see if this |
1053 |
/* TODO: Examine the first characters to see if this |
983 |
* is an AIX ACL descriptor. We'll likely never support |
1054 |
* is an AIX ACL descriptor. We'll likely never support |
Lines 985-996
header_Solaris_ACL(struct archive_read *a, struct tar *tar,
Link Here
|
985 |
* we do see them. */ |
1056 |
* we do see them. */ |
986 |
|
1057 |
|
987 |
/* Leading octal number indicates ACL type and number of entries. */ |
1058 |
/* Leading octal number indicates ACL type and number of entries. */ |
988 |
p = acl = tar->acl_text.s; |
1059 |
p = acl = acl_text.s; |
989 |
type = 0; |
1060 |
type = 0; |
990 |
while (*p != '\0' && p < acl + size) { |
1061 |
while (*p != '\0' && p < acl + size) { |
991 |
if (*p < '0' || *p > '7') { |
1062 |
if (*p < '0' || *p > '7') { |
992 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1063 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
993 |
"Malformed Solaris ACL attribute (invalid digit)"); |
1064 |
"Malformed Solaris ACL attribute (invalid digit)"); |
|
|
1065 |
archive_string_free(&acl_text); |
994 |
return(ARCHIVE_WARN); |
1066 |
return(ARCHIVE_WARN); |
995 |
} |
1067 |
} |
996 |
type <<= 3; |
1068 |
type <<= 3; |
Lines 998-1003
header_Solaris_ACL(struct archive_read *a, struct tar *tar,
Link Here
|
998 |
if (type > 077777777) { |
1070 |
if (type > 077777777) { |
999 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1071 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1000 |
"Malformed Solaris ACL attribute (count too large)"); |
1072 |
"Malformed Solaris ACL attribute (count too large)"); |
|
|
1073 |
archive_string_free(&acl_text); |
1001 |
return (ARCHIVE_WARN); |
1074 |
return (ARCHIVE_WARN); |
1002 |
} |
1075 |
} |
1003 |
p++; |
1076 |
p++; |
Lines 1015-1020
header_Solaris_ACL(struct archive_read *a, struct tar *tar,
Link Here
|
1015 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1088 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1016 |
"Malformed Solaris ACL attribute (unsupported type %o)", |
1089 |
"Malformed Solaris ACL attribute (unsupported type %o)", |
1017 |
(int)type); |
1090 |
(int)type); |
|
|
1091 |
archive_string_free(&acl_text); |
1018 |
return (ARCHIVE_WARN); |
1092 |
return (ARCHIVE_WARN); |
1019 |
} |
1093 |
} |
1020 |
p++; |
1094 |
p++; |
Lines 1022-1027
header_Solaris_ACL(struct archive_read *a, struct tar *tar,
Link Here
|
1022 |
if (p >= acl + size) { |
1096 |
if (p >= acl + size) { |
1023 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1097 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1024 |
"Malformed Solaris ACL attribute (body overflow)"); |
1098 |
"Malformed Solaris ACL attribute (body overflow)"); |
|
|
1099 |
archive_string_free(&acl_text); |
1025 |
return(ARCHIVE_WARN); |
1100 |
return(ARCHIVE_WARN); |
1026 |
} |
1101 |
} |
1027 |
|
1102 |
|
Lines 1035-1046
header_Solaris_ACL(struct archive_read *a, struct tar *tar,
Link Here
|
1035 |
if (tar->sconv_acl == NULL) { |
1110 |
if (tar->sconv_acl == NULL) { |
1036 |
tar->sconv_acl = archive_string_conversion_from_charset( |
1111 |
tar->sconv_acl = archive_string_conversion_from_charset( |
1037 |
&(a->archive), "UTF-8", 1); |
1112 |
&(a->archive), "UTF-8", 1); |
1038 |
if (tar->sconv_acl == NULL) |
1113 |
if (tar->sconv_acl == NULL) { |
|
|
1114 |
archive_string_free(&acl_text); |
1039 |
return (ARCHIVE_FATAL); |
1115 |
return (ARCHIVE_FATAL); |
|
|
1116 |
} |
1040 |
} |
1117 |
} |
1041 |
archive_strncpy(&(tar->localname), acl, p - acl); |
1118 |
archive_strncpy(&(tar->localname), acl, p - acl); |
1042 |
err = archive_acl_from_text_l(archive_entry_acl(entry), |
1119 |
err = archive_acl_from_text_l(archive_entry_acl(entry), |
1043 |
tar->localname.s, acl_type, tar->sconv_acl); |
1120 |
tar->localname.s, acl_type, tar->sconv_acl); |
|
|
1121 |
/* Workaround: Force perm_is_set() to be correct */ |
1122 |
/* If this bit were stored in the ACL, this wouldn't be needed */ |
1123 |
archive_entry_set_perm(entry, archive_entry_perm(entry)); |
1044 |
if (err != ARCHIVE_OK) { |
1124 |
if (err != ARCHIVE_OK) { |
1045 |
if (errno == ENOMEM) { |
1125 |
if (errno == ENOMEM) { |
1046 |
archive_set_error(&a->archive, ENOMEM, |
1126 |
archive_set_error(&a->archive, ENOMEM, |
Lines 1049-1054
header_Solaris_ACL(struct archive_read *a, struct tar *tar,
Link Here
|
1049 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1129 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1050 |
"Malformed Solaris ACL attribute (unparsable)"); |
1130 |
"Malformed Solaris ACL attribute (unparsable)"); |
1051 |
} |
1131 |
} |
|
|
1132 |
archive_string_free(&acl_text); |
1052 |
return (err); |
1133 |
return (err); |
1053 |
} |
1134 |
} |
1054 |
|
1135 |
|
Lines 1056-1075
header_Solaris_ACL(struct archive_read *a, struct tar *tar,
Link Here
|
1056 |
* Interpret 'K' long linkname header. |
1137 |
* Interpret 'K' long linkname header. |
1057 |
*/ |
1138 |
*/ |
1058 |
static int |
1139 |
static int |
1059 |
header_longlink(struct archive_read *a, struct tar *tar, |
1140 |
header_gnu_longlink(struct archive_read *a, struct tar *tar, |
1060 |
struct archive_entry *entry, const void *h, size_t *unconsumed) |
1141 |
struct archive_entry *entry, const void *h, size_t *unconsumed) |
1061 |
{ |
1142 |
{ |
1062 |
int err; |
1143 |
int err; |
1063 |
|
1144 |
|
1064 |
err = read_body_to_string(a, tar, &(tar->longlink), h, unconsumed); |
1145 |
struct archive_string linkpath; |
1065 |
if (err != ARCHIVE_OK) |
1146 |
archive_string_init(&linkpath); |
1066 |
return (err); |
1147 |
err = read_body_to_string(a, tar, &linkpath, h, unconsumed); |
1067 |
err = tar_read_header(a, tar, entry, unconsumed); |
1148 |
archive_entry_set_link(entry, linkpath.s); |
1068 |
if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN)) |
1149 |
archive_string_free(&linkpath); |
1069 |
return (err); |
1150 |
return (err); |
1070 |
/* Set symlink if symlink already set, else hardlink. */ |
|
|
1071 |
archive_entry_copy_link(entry, tar->longlink.s); |
1072 |
return (ARCHIVE_OK); |
1073 |
} |
1151 |
} |
1074 |
|
1152 |
|
1075 |
static int |
1153 |
static int |
Lines 1091-1115
set_conversion_failed_error(struct archive_read *a,
Link Here
|
1091 |
* Interpret 'L' long filename header. |
1169 |
* Interpret 'L' long filename header. |
1092 |
*/ |
1170 |
*/ |
1093 |
static int |
1171 |
static int |
1094 |
header_longname(struct archive_read *a, struct tar *tar, |
1172 |
header_gnu_longname(struct archive_read *a, struct tar *tar, |
1095 |
struct archive_entry *entry, const void *h, size_t *unconsumed) |
1173 |
struct archive_entry *entry, const void *h, size_t *unconsumed) |
1096 |
{ |
1174 |
{ |
1097 |
int err; |
1175 |
int err; |
|
|
1176 |
struct archive_string longname; |
1098 |
|
1177 |
|
1099 |
err = read_body_to_string(a, tar, &(tar->longname), h, unconsumed); |
1178 |
archive_string_init(&longname); |
1100 |
if (err != ARCHIVE_OK) |
1179 |
err = read_body_to_string(a, tar, &longname, h, unconsumed); |
1101 |
return (err); |
1180 |
if (err == ARCHIVE_OK) { |
1102 |
/* Read and parse "real" header, then override name. */ |
1181 |
if (archive_entry_copy_pathname_l(entry, longname.s, |
1103 |
err = tar_read_header(a, tar, entry, unconsumed); |
1182 |
archive_strlen(&longname), tar->sconv) != 0) |
1104 |
if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN)) |
1183 |
err = set_conversion_failed_error(a, tar->sconv, "Pathname"); |
1105 |
return (err); |
1184 |
} |
1106 |
if (archive_entry_copy_pathname_l(entry, tar->longname.s, |
1185 |
archive_string_free(&longname); |
1107 |
archive_strlen(&(tar->longname)), tar->sconv) != 0) |
|
|
1108 |
err = set_conversion_failed_error(a, tar->sconv, "Pathname"); |
1109 |
return (err); |
1186 |
return (err); |
1110 |
} |
1187 |
} |
1111 |
|
1188 |
|
1112 |
|
|
|
1113 |
/* |
1189 |
/* |
1114 |
* Interpret 'V' GNU tar volume header. |
1190 |
* Interpret 'V' GNU tar volume header. |
1115 |
*/ |
1191 |
*/ |
Lines 1117-1148
static int
Link Here
|
1117 |
header_volume(struct archive_read *a, struct tar *tar, |
1193 |
header_volume(struct archive_read *a, struct tar *tar, |
1118 |
struct archive_entry *entry, const void *h, size_t *unconsumed) |
1194 |
struct archive_entry *entry, const void *h, size_t *unconsumed) |
1119 |
{ |
1195 |
{ |
1120 |
(void)h; |
1196 |
const struct archive_entry_header_ustar *header; |
|
|
1197 |
int64_t size, to_consume; |
1198 |
|
1199 |
(void)a; /* UNUSED */ |
1200 |
(void)tar; /* UNUSED */ |
1201 |
(void)entry; /* UNUSED */ |
1121 |
|
1202 |
|
1122 |
/* Just skip this and read the next header. */ |
1203 |
header = (const struct archive_entry_header_ustar *)h; |
1123 |
return (tar_read_header(a, tar, entry, unconsumed)); |
1204 |
size = tar_atol(header->size, sizeof(header->size)); |
|
|
1205 |
if (size > (int64_t)pathname_limit) { |
1206 |
return (ARCHIVE_FATAL); |
1207 |
} |
1208 |
to_consume = ((size + 511) & ~511); |
1209 |
*unconsumed += to_consume; |
1210 |
return (ARCHIVE_OK); |
1124 |
} |
1211 |
} |
1125 |
|
1212 |
|
1126 |
/* |
1213 |
/* |
1127 |
* Read body of an archive entry into an archive_string object. |
1214 |
* Read the next `size` bytes into the provided string. |
|
|
1215 |
* Null-terminate the string. |
1128 |
*/ |
1216 |
*/ |
1129 |
static int |
1217 |
static int |
1130 |
read_body_to_string(struct archive_read *a, struct tar *tar, |
1218 |
read_bytes_to_string(struct archive_read *a, |
1131 |
struct archive_string *as, const void *h, size_t *unconsumed) |
1219 |
struct archive_string *as, size_t size, |
1132 |
{ |
1220 |
size_t *unconsumed) { |
1133 |
int64_t size; |
|
|
1134 |
const struct archive_entry_header_ustar *header; |
1135 |
const void *src; |
1221 |
const void *src; |
1136 |
|
1222 |
|
1137 |
(void)tar; /* UNUSED */ |
|
|
1138 |
header = (const struct archive_entry_header_ustar *)h; |
1139 |
size = tar_atol(header->size, sizeof(header->size)); |
1140 |
if ((size > 1048576) || (size < 0)) { |
1141 |
archive_set_error(&a->archive, EINVAL, |
1142 |
"Special header too large"); |
1143 |
return (ARCHIVE_FATAL); |
1144 |
} |
1145 |
|
1146 |
/* Fail if we can't make our buffer big enough. */ |
1223 |
/* Fail if we can't make our buffer big enough. */ |
1147 |
if (archive_string_ensure(as, (size_t)size+1) == NULL) { |
1224 |
if (archive_string_ensure(as, (size_t)size+1) == NULL) { |
1148 |
archive_set_error(&a->archive, ENOMEM, |
1225 |
archive_set_error(&a->archive, ENOMEM, |
Lines 1153-1170
read_body_to_string(struct archive_read *a, struct tar *tar,
Link Here
|
1153 |
tar_flush_unconsumed(a, unconsumed); |
1230 |
tar_flush_unconsumed(a, unconsumed); |
1154 |
|
1231 |
|
1155 |
/* Read the body into the string. */ |
1232 |
/* Read the body into the string. */ |
1156 |
*unconsumed = (size_t)((size + 511) & ~ 511); |
1233 |
src = __archive_read_ahead(a, size, NULL); |
1157 |
src = __archive_read_ahead(a, *unconsumed, NULL); |
|
|
1158 |
if (src == NULL) { |
1234 |
if (src == NULL) { |
|
|
1235 |
archive_set_error(&a->archive, EINVAL, |
1236 |
"Truncated archive" |
1237 |
" detected while reading metadata"); |
1159 |
*unconsumed = 0; |
1238 |
*unconsumed = 0; |
1160 |
return (ARCHIVE_FATAL); |
1239 |
return (ARCHIVE_FATAL); |
1161 |
} |
1240 |
} |
1162 |
memcpy(as->s, src, (size_t)size); |
1241 |
memcpy(as->s, src, (size_t)size); |
1163 |
as->s[size] = '\0'; |
1242 |
as->s[size] = '\0'; |
1164 |
as->length = (size_t)size; |
1243 |
as->length = (size_t)size; |
|
|
1244 |
*unconsumed += size; |
1165 |
return (ARCHIVE_OK); |
1245 |
return (ARCHIVE_OK); |
1166 |
} |
1246 |
} |
1167 |
|
1247 |
|
|
|
1248 |
/* |
1249 |
* Read body of an archive entry into an archive_string object. |
1250 |
*/ |
1251 |
static int |
1252 |
read_body_to_string(struct archive_read *a, struct tar *tar, |
1253 |
struct archive_string *as, const void *h, size_t *unconsumed) |
1254 |
{ |
1255 |
int64_t size; |
1256 |
const struct archive_entry_header_ustar *header; |
1257 |
int r; |
1258 |
|
1259 |
(void)tar; /* UNUSED */ |
1260 |
header = (const struct archive_entry_header_ustar *)h; |
1261 |
size = tar_atol(header->size, sizeof(header->size)); |
1262 |
if (size > entry_limit) { |
1263 |
return (ARCHIVE_FATAL); |
1264 |
} |
1265 |
if ((size > (int64_t)pathname_limit) || (size < 0)) { |
1266 |
archive_string_empty(as); |
1267 |
int64_t to_consume = ((size + 511) & ~511); |
1268 |
if (to_consume != __archive_read_consume(a, to_consume)) { |
1269 |
return (ARCHIVE_FATAL); |
1270 |
} |
1271 |
archive_set_error(&a->archive, EINVAL, |
1272 |
"Special header too large: %d > 1MiB", |
1273 |
(int)size); |
1274 |
return (ARCHIVE_WARN); |
1275 |
} |
1276 |
r = read_bytes_to_string(a, as, size, unconsumed); |
1277 |
*unconsumed += 0x1ff & (-size); |
1278 |
return(r); |
1279 |
} |
1280 |
|
1168 |
/* |
1281 |
/* |
1169 |
* Parse out common header elements. |
1282 |
* Parse out common header elements. |
1170 |
* |
1283 |
* |
Lines 1180-1200
header_common(struct archive_read *a, struct tar *tar,
Link Here
|
1180 |
struct archive_entry *entry, const void *h) |
1293 |
struct archive_entry *entry, const void *h) |
1181 |
{ |
1294 |
{ |
1182 |
const struct archive_entry_header_ustar *header; |
1295 |
const struct archive_entry_header_ustar *header; |
1183 |
char tartype; |
1296 |
const char *existing_linkpath; |
|
|
1297 |
const wchar_t *existing_wcs_linkpath; |
1184 |
int err = ARCHIVE_OK; |
1298 |
int err = ARCHIVE_OK; |
1185 |
|
1299 |
|
1186 |
header = (const struct archive_entry_header_ustar *)h; |
1300 |
header = (const struct archive_entry_header_ustar *)h; |
1187 |
if (header->linkname[0]) |
|
|
1188 |
archive_strncpy(&(tar->entry_linkpath), |
1189 |
header->linkname, sizeof(header->linkname)); |
1190 |
else |
1191 |
archive_string_empty(&(tar->entry_linkpath)); |
1192 |
|
1301 |
|
1193 |
/* Parse out the numeric fields (all are octal) */ |
1302 |
/* Parse out the numeric fields (all are octal) */ |
1194 |
archive_entry_set_mode(entry, |
1303 |
|
1195 |
(mode_t)tar_atol(header->mode, sizeof(header->mode))); |
1304 |
/* Split mode handling: Set filetype always, perm only if not already set */ |
1196 |
archive_entry_set_uid(entry, tar_atol(header->uid, sizeof(header->uid))); |
1305 |
archive_entry_set_filetype(entry, |
1197 |
archive_entry_set_gid(entry, tar_atol(header->gid, sizeof(header->gid))); |
1306 |
(mode_t)tar_atol(header->mode, sizeof(header->mode))); |
|
|
1307 |
if (!archive_entry_perm_is_set(entry)) { |
1308 |
archive_entry_set_perm(entry, |
1309 |
(mode_t)tar_atol(header->mode, sizeof(header->mode))); |
1310 |
} |
1311 |
if (!archive_entry_uid_is_set(entry)) { |
1312 |
archive_entry_set_uid(entry, tar_atol(header->uid, sizeof(header->uid))); |
1313 |
} |
1314 |
if (!archive_entry_gid_is_set(entry)) { |
1315 |
archive_entry_set_gid(entry, tar_atol(header->gid, sizeof(header->gid))); |
1316 |
} |
1317 |
|
1198 |
tar->entry_bytes_remaining = tar_atol(header->size, sizeof(header->size)); |
1318 |
tar->entry_bytes_remaining = tar_atol(header->size, sizeof(header->size)); |
1199 |
if (tar->entry_bytes_remaining < 0) { |
1319 |
if (tar->entry_bytes_remaining < 0) { |
1200 |
tar->entry_bytes_remaining = 0; |
1320 |
tar->entry_bytes_remaining = 0; |
Lines 1202-1229
header_common(struct archive_read *a, struct tar *tar,
Link Here
|
1202 |
"Tar entry has negative size"); |
1322 |
"Tar entry has negative size"); |
1203 |
return (ARCHIVE_FATAL); |
1323 |
return (ARCHIVE_FATAL); |
1204 |
} |
1324 |
} |
1205 |
if (tar->entry_bytes_remaining == INT64_MAX) { |
1325 |
if (tar->entry_bytes_remaining > entry_limit) { |
1206 |
/* Note: tar_atol returns INT64_MAX on overflow */ |
|
|
1207 |
tar->entry_bytes_remaining = 0; |
1326 |
tar->entry_bytes_remaining = 0; |
1208 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1327 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1209 |
"Tar entry size overflow"); |
1328 |
"Tar entry size overflow"); |
1210 |
return (ARCHIVE_FATAL); |
1329 |
return (ARCHIVE_FATAL); |
1211 |
} |
1330 |
} |
1212 |
tar->realsize = tar->entry_bytes_remaining; |
1331 |
if (!tar->realsize_override) { |
1213 |
archive_entry_set_size(entry, tar->entry_bytes_remaining); |
1332 |
tar->realsize = tar->entry_bytes_remaining; |
1214 |
archive_entry_set_mtime(entry, tar_atol(header->mtime, sizeof(header->mtime)), 0); |
1333 |
} |
|
|
1334 |
archive_entry_set_size(entry, tar->realsize); |
1335 |
|
1336 |
if (!archive_entry_mtime_is_set(entry)) { |
1337 |
archive_entry_set_mtime(entry, tar_atol(header->mtime, sizeof(header->mtime)), 0); |
1338 |
} |
1215 |
|
1339 |
|
1216 |
/* Handle the tar type flag appropriately. */ |
1340 |
/* Handle the tar type flag appropriately. */ |
1217 |
tartype = header->typeflag[0]; |
1341 |
tar->filetype = header->typeflag[0]; |
1218 |
|
1342 |
|
1219 |
switch (tartype) { |
1343 |
/* |
|
|
1344 |
* TODO: If the linkpath came from Pax extension header, then |
1345 |
* we should obey the hdrcharset_utf8 flag when converting these. |
1346 |
*/ |
1347 |
switch (tar->filetype) { |
1220 |
case '1': /* Hard link */ |
1348 |
case '1': /* Hard link */ |
1221 |
if (archive_entry_copy_hardlink_l(entry, tar->entry_linkpath.s, |
1349 |
archive_entry_set_link_to_hardlink(entry); |
1222 |
archive_strlen(&(tar->entry_linkpath)), tar->sconv) != 0) { |
1350 |
existing_wcs_linkpath = archive_entry_hardlink_w(entry); |
1223 |
err = set_conversion_failed_error(a, tar->sconv, |
1351 |
existing_linkpath = archive_entry_hardlink(entry); |
1224 |
"Linkname"); |
1352 |
if ((existing_linkpath == NULL || existing_linkpath[0] == '\0') |
1225 |
if (err == ARCHIVE_FATAL) |
1353 |
&& (existing_wcs_linkpath == NULL || existing_wcs_linkpath[0] == '\0')) { |
1226 |
return (err); |
1354 |
struct archive_string linkpath; |
|
|
1355 |
archive_string_init(&linkpath); |
1356 |
archive_strncpy(&linkpath, |
1357 |
header->linkname, sizeof(header->linkname)); |
1358 |
if (archive_entry_copy_hardlink_l(entry, linkpath.s, |
1359 |
archive_strlen(&linkpath), tar->sconv) != 0) { |
1360 |
err = set_conversion_failed_error(a, tar->sconv, |
1361 |
"Linkname"); |
1362 |
if (err == ARCHIVE_FATAL) { |
1363 |
archive_string_free(&linkpath); |
1364 |
return (err); |
1365 |
} |
1366 |
} |
1367 |
archive_string_free(&linkpath); |
1227 |
} |
1368 |
} |
1228 |
/* |
1369 |
/* |
1229 |
* The following may seem odd, but: Technically, tar |
1370 |
* The following may seem odd, but: Technically, tar |
Lines 1283-1298
header_common(struct archive_read *a, struct tar *tar,
Link Here
|
1283 |
*/ |
1424 |
*/ |
1284 |
break; |
1425 |
break; |
1285 |
case '2': /* Symlink */ |
1426 |
case '2': /* Symlink */ |
|
|
1427 |
archive_entry_set_link_to_symlink(entry); |
1428 |
existing_wcs_linkpath = archive_entry_symlink_w(entry); |
1429 |
existing_linkpath = archive_entry_symlink(entry); |
1430 |
if ((existing_linkpath == NULL || existing_linkpath[0] == '\0') |
1431 |
&& (existing_wcs_linkpath == NULL || existing_wcs_linkpath[0] == '\0')) { |
1432 |
struct archive_string linkpath; |
1433 |
archive_string_init(&linkpath); |
1434 |
archive_strncpy(&linkpath, |
1435 |
header->linkname, sizeof(header->linkname)); |
1436 |
if (archive_entry_copy_symlink_l(entry, linkpath.s, |
1437 |
archive_strlen(&linkpath), tar->sconv) != 0) { |
1438 |
err = set_conversion_failed_error(a, tar->sconv, |
1439 |
"Linkname"); |
1440 |
if (err == ARCHIVE_FATAL) { |
1441 |
archive_string_free(&linkpath); |
1442 |
return (err); |
1443 |
} |
1444 |
} |
1445 |
archive_string_free(&linkpath); |
1446 |
} |
1286 |
archive_entry_set_filetype(entry, AE_IFLNK); |
1447 |
archive_entry_set_filetype(entry, AE_IFLNK); |
1287 |
archive_entry_set_size(entry, 0); |
1448 |
archive_entry_set_size(entry, 0); |
1288 |
tar->entry_bytes_remaining = 0; |
1449 |
tar->entry_bytes_remaining = 0; |
1289 |
if (archive_entry_copy_symlink_l(entry, tar->entry_linkpath.s, |
|
|
1290 |
archive_strlen(&(tar->entry_linkpath)), tar->sconv) != 0) { |
1291 |
err = set_conversion_failed_error(a, tar->sconv, |
1292 |
"Linkname"); |
1293 |
if (err == ARCHIVE_FATAL) |
1294 |
return (err); |
1295 |
} |
1296 |
break; |
1450 |
break; |
1297 |
case '3': /* Character device */ |
1451 |
case '3': /* Character device */ |
1298 |
archive_entry_set_filetype(entry, AE_IFCHR); |
1452 |
archive_entry_set_filetype(entry, AE_IFCHR); |
Lines 1342-1356
header_common(struct archive_read *a, struct tar *tar,
Link Here
|
1342 |
* sparse information in the extended area. |
1496 |
* sparse information in the extended area. |
1343 |
*/ |
1497 |
*/ |
1344 |
/* FALLTHROUGH */ |
1498 |
/* FALLTHROUGH */ |
1345 |
case '0': |
1499 |
case '0': /* ustar "regular" file */ |
1346 |
/* |
|
|
1347 |
* Enable sparse file "read" support only for regular |
1348 |
* files and explicit GNU sparse files. However, we |
1349 |
* don't allow non-standard file types to be sparse. |
1350 |
*/ |
1351 |
tar->sparse_allowed = 1; |
1352 |
/* FALLTHROUGH */ |
1500 |
/* FALLTHROUGH */ |
1353 |
default: /* Regular file and non-standard types */ |
1501 |
default: /* Non-standard file types */ |
1354 |
/* |
1502 |
/* |
1355 |
* Per POSIX: non-recognized types should always be |
1503 |
* Per POSIX: non-recognized types should always be |
1356 |
* treated as regular files. |
1504 |
* treated as regular files. |
Lines 1371-1379
header_old_tar(struct archive_read *a, struct tar *tar,
Link Here
|
1371 |
const struct archive_entry_header_ustar *header; |
1519 |
const struct archive_entry_header_ustar *header; |
1372 |
int err = ARCHIVE_OK, err2; |
1520 |
int err = ARCHIVE_OK, err2; |
1373 |
|
1521 |
|
1374 |
/* Copy filename over (to ensure null termination). */ |
1522 |
/* |
|
|
1523 |
* Copy filename over (to ensure null termination). |
1524 |
* Skip if pathname was already set e.g. by header_gnu_longname() |
1525 |
*/ |
1375 |
header = (const struct archive_entry_header_ustar *)h; |
1526 |
header = (const struct archive_entry_header_ustar *)h; |
1376 |
if (archive_entry_copy_pathname_l(entry, |
1527 |
|
|
|
1528 |
const char *existing_pathname = archive_entry_pathname(entry); |
1529 |
const wchar_t *existing_wcs_pathname = archive_entry_pathname_w(entry); |
1530 |
if ((existing_pathname == NULL || existing_pathname[0] == '\0') |
1531 |
&& (existing_wcs_pathname == NULL || existing_wcs_pathname[0] == '\0') && |
1532 |
archive_entry_copy_pathname_l(entry, |
1377 |
header->name, sizeof(header->name), tar->sconv) != 0) { |
1533 |
header->name, sizeof(header->name), tar->sconv) != 0) { |
1378 |
err = set_conversion_failed_error(a, tar->sconv, "Pathname"); |
1534 |
err = set_conversion_failed_error(a, tar->sconv, "Pathname"); |
1379 |
if (err == ARCHIVE_FATAL) |
1535 |
if (err == ARCHIVE_FATAL) |
Lines 1390-1410
header_old_tar(struct archive_read *a, struct tar *tar,
Link Here
|
1390 |
} |
1546 |
} |
1391 |
|
1547 |
|
1392 |
/* |
1548 |
/* |
1393 |
* Read a Mac AppleDouble-encoded blob of file metadata, |
1549 |
* Is this likely an AppleDouble extension? |
1394 |
* if there is one. |
|
|
1395 |
*/ |
1550 |
*/ |
1396 |
static int |
1551 |
static int |
1397 |
read_mac_metadata_blob(struct archive_read *a, struct tar *tar, |
1552 |
is_mac_metadata_entry(struct archive_entry *entry) { |
1398 |
struct archive_entry *entry, const void *h, size_t *unconsumed) |
|
|
1399 |
{ |
1400 |
int64_t size; |
1401 |
size_t msize; |
1402 |
const void *data; |
1403 |
const char *p, *name; |
1553 |
const char *p, *name; |
1404 |
const wchar_t *wp, *wname; |
1554 |
const wchar_t *wp, *wname; |
1405 |
|
1555 |
|
1406 |
(void)h; /* UNUSED */ |
|
|
1407 |
|
1408 |
wname = wp = archive_entry_pathname_w(entry); |
1556 |
wname = wp = archive_entry_pathname_w(entry); |
1409 |
if (wp != NULL) { |
1557 |
if (wp != NULL) { |
1410 |
/* Find the last path element. */ |
1558 |
/* Find the last path element. */ |
Lines 1416-1423
read_mac_metadata_blob(struct archive_read *a, struct tar *tar,
Link Here
|
1416 |
* If last path element starts with "._", then |
1564 |
* If last path element starts with "._", then |
1417 |
* this is a Mac extension. |
1565 |
* this is a Mac extension. |
1418 |
*/ |
1566 |
*/ |
1419 |
if (wname[0] != L'.' || wname[1] != L'_' || wname[2] == L'\0') |
1567 |
if (wname[0] == L'.' && wname[1] == L'_' && wname[2] != L'\0') |
1420 |
return ARCHIVE_OK; |
1568 |
return 1; |
1421 |
} else { |
1569 |
} else { |
1422 |
/* Find the last path element. */ |
1570 |
/* Find the last path element. */ |
1423 |
name = p = archive_entry_pathname(entry); |
1571 |
name = p = archive_entry_pathname(entry); |
Lines 1431-1439
read_mac_metadata_blob(struct archive_read *a, struct tar *tar,
Link Here
|
1431 |
* If last path element starts with "._", then |
1579 |
* If last path element starts with "._", then |
1432 |
* this is a Mac extension. |
1580 |
* this is a Mac extension. |
1433 |
*/ |
1581 |
*/ |
1434 |
if (name[0] != '.' || name[1] != '_' || name[2] == '\0') |
1582 |
if (name[0] == '.' && name[1] == '_' && name[2] != '\0') |
1435 |
return ARCHIVE_OK; |
1583 |
return 1; |
1436 |
} |
1584 |
} |
|
|
1585 |
/* Not a mac extension */ |
1586 |
return 0; |
1587 |
} |
1588 |
|
1589 |
/* |
1590 |
* Read a Mac AppleDouble-encoded blob of file metadata, |
1591 |
* if there is one. |
1592 |
* |
1593 |
* TODO: In Libarchive 4, we should consider ripping this |
1594 |
* out -- instead, return a file starting with `._` as |
1595 |
* a regular file and let the client (or archive_write logic) |
1596 |
* handle it. |
1597 |
*/ |
1598 |
static int |
1599 |
read_mac_metadata_blob(struct archive_read *a, |
1600 |
struct archive_entry *entry, size_t *unconsumed) |
1601 |
{ |
1602 |
int64_t size; |
1603 |
size_t msize; |
1604 |
const void *data; |
1437 |
|
1605 |
|
1438 |
/* Read the body as a Mac OS metadata blob. */ |
1606 |
/* Read the body as a Mac OS metadata blob. */ |
1439 |
size = archive_entry_size(entry); |
1607 |
size = archive_entry_size(entry); |
Lines 1443-1448
read_mac_metadata_blob(struct archive_read *a, struct tar *tar,
Link Here
|
1443 |
return (ARCHIVE_FATAL); |
1611 |
return (ARCHIVE_FATAL); |
1444 |
} |
1612 |
} |
1445 |
|
1613 |
|
|
|
1614 |
/* TODO: Should this merely skip the overlarge entry and |
1615 |
* WARN? Or is xattr_limit sufficiently large that we can |
1616 |
* safely assume anything larger is malicious? */ |
1617 |
if (size > (int64_t)xattr_limit) { |
1618 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1619 |
"Oversized AppleDouble extension has size %llu > %llu", |
1620 |
(unsigned long long)size, |
1621 |
(unsigned long long)xattr_limit); |
1622 |
return (ARCHIVE_FATAL); |
1623 |
} |
1624 |
|
1446 |
/* |
1625 |
/* |
1447 |
* TODO: Look beyond the body here to peek at the next header. |
1626 |
* TODO: Look beyond the body here to peek at the next header. |
1448 |
* If it's a regular header (not an extension header) |
1627 |
* If it's a regular header (not an extension header) |
Lines 1455-1469
read_mac_metadata_blob(struct archive_read *a, struct tar *tar,
Link Here
|
1455 |
* Q: Is the above idea really possible? Even |
1634 |
* Q: Is the above idea really possible? Even |
1456 |
* when there are GNU or pax extension entries? |
1635 |
* when there are GNU or pax extension entries? |
1457 |
*/ |
1636 |
*/ |
|
|
1637 |
tar_flush_unconsumed(a, unconsumed); |
1458 |
data = __archive_read_ahead(a, msize, NULL); |
1638 |
data = __archive_read_ahead(a, msize, NULL); |
1459 |
if (data == NULL) { |
1639 |
if (data == NULL) { |
|
|
1640 |
archive_set_error(&a->archive, EINVAL, |
1641 |
"Truncated archive" |
1642 |
" detected while reading macOS metadata"); |
1460 |
*unconsumed = 0; |
1643 |
*unconsumed = 0; |
1461 |
return (ARCHIVE_FATAL); |
1644 |
return (ARCHIVE_FATAL); |
1462 |
} |
1645 |
} |
|
|
1646 |
archive_entry_clear(entry); |
1463 |
archive_entry_copy_mac_metadata(entry, data, msize); |
1647 |
archive_entry_copy_mac_metadata(entry, data, msize); |
1464 |
*unconsumed = (msize + 511) & ~ 511; |
1648 |
*unconsumed = (msize + 511) & ~ 511; |
1465 |
tar_flush_unconsumed(a, unconsumed); |
1649 |
return (ARCHIVE_OK); |
1466 |
return (tar_read_header(a, tar, entry, unconsumed)); |
|
|
1467 |
} |
1650 |
} |
1468 |
|
1651 |
|
1469 |
/* |
1652 |
/* |
Lines 1473-1548
static int
Link Here
|
1473 |
header_pax_global(struct archive_read *a, struct tar *tar, |
1656 |
header_pax_global(struct archive_read *a, struct tar *tar, |
1474 |
struct archive_entry *entry, const void *h, size_t *unconsumed) |
1657 |
struct archive_entry *entry, const void *h, size_t *unconsumed) |
1475 |
{ |
1658 |
{ |
1476 |
int err; |
1659 |
const struct archive_entry_header_ustar *header; |
|
|
1660 |
int64_t size, to_consume; |
1477 |
|
1661 |
|
1478 |
err = read_body_to_string(a, tar, &(tar->pax_global), h, unconsumed); |
1662 |
(void)a; /* UNUSED */ |
1479 |
if (err != ARCHIVE_OK) |
1663 |
(void)tar; /* UNUSED */ |
1480 |
return (err); |
1664 |
(void)entry; /* UNUSED */ |
1481 |
err = tar_read_header(a, tar, entry, unconsumed); |
1665 |
|
1482 |
return (err); |
1666 |
header = (const struct archive_entry_header_ustar *)h; |
|
|
1667 |
size = tar_atol(header->size, sizeof(header->size)); |
1668 |
if (size > entry_limit) { |
1669 |
return (ARCHIVE_FATAL); |
1670 |
} |
1671 |
to_consume = ((size + 511) & ~511); |
1672 |
*unconsumed += to_consume; |
1673 |
return (ARCHIVE_OK); |
1483 |
} |
1674 |
} |
1484 |
|
1675 |
|
|
|
1676 |
/* |
1677 |
* Parse a file header for a Posix "ustar" archive entry. This also |
1678 |
* handles "pax" or "extended ustar" entries. |
1679 |
* |
1680 |
* In order to correctly handle pax attributes (which precede this), |
1681 |
* we have to skip parsing any field for which the entry already has |
1682 |
* contents. |
1683 |
*/ |
1485 |
static int |
1684 |
static int |
1486 |
header_pax_extensions(struct archive_read *a, struct tar *tar, |
1685 |
header_ustar(struct archive_read *a, struct tar *tar, |
1487 |
struct archive_entry *entry, const void *h, size_t *unconsumed) |
1686 |
struct archive_entry *entry, const void *h) |
1488 |
{ |
1687 |
{ |
1489 |
int err, err2; |
1688 |
const struct archive_entry_header_ustar *header; |
1490 |
|
1689 |
struct archive_string as; |
1491 |
err = read_body_to_string(a, tar, &(tar->pax_header), h, unconsumed); |
1690 |
int err = ARCHIVE_OK, r; |
1492 |
if (err != ARCHIVE_OK) |
|
|
1493 |
return (err); |
1494 |
|
1495 |
/* Parse the next header. */ |
1496 |
err = tar_read_header(a, tar, entry, unconsumed); |
1497 |
if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN)) |
1498 |
return (err); |
1499 |
|
1500 |
/* |
1501 |
* TODO: Parse global/default options into 'entry' struct here |
1502 |
* before handling file-specific options. |
1503 |
* |
1504 |
* This design (parse standard header, then overwrite with pax |
1505 |
* extended attribute data) usually works well, but isn't ideal; |
1506 |
* it would be better to parse the pax extended attributes first |
1507 |
* and then skip any fields in the standard header that were |
1508 |
* defined in the pax header. |
1509 |
*/ |
1510 |
err2 = pax_header(a, tar, entry, &tar->pax_header); |
1511 |
err = err_combine(err, err2); |
1512 |
tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining); |
1513 |
return (err); |
1514 |
} |
1515 |
|
1516 |
|
1517 |
/* |
1518 |
* Parse a file header for a Posix "ustar" archive entry. This also |
1519 |
* handles "pax" or "extended ustar" entries. |
1520 |
*/ |
1521 |
static int |
1522 |
header_ustar(struct archive_read *a, struct tar *tar, |
1523 |
struct archive_entry *entry, const void *h) |
1524 |
{ |
1525 |
const struct archive_entry_header_ustar *header; |
1526 |
struct archive_string *as; |
1527 |
int err = ARCHIVE_OK, r; |
1528 |
|
1691 |
|
1529 |
header = (const struct archive_entry_header_ustar *)h; |
1692 |
header = (const struct archive_entry_header_ustar *)h; |
1530 |
|
1693 |
|
1531 |
/* Copy name into an internal buffer to ensure null-termination. */ |
1694 |
/* Copy name into an internal buffer to ensure null-termination. */ |
1532 |
as = &(tar->entry_pathname); |
1695 |
const char *existing_pathname = archive_entry_pathname(entry); |
1533 |
if (header->prefix[0]) { |
1696 |
const wchar_t *existing_wcs_pathname = archive_entry_pathname_w(entry); |
1534 |
archive_strncpy(as, header->prefix, sizeof(header->prefix)); |
1697 |
if ((existing_pathname == NULL || existing_pathname[0] == '\0') |
1535 |
if (as->s[archive_strlen(as) - 1] != '/') |
1698 |
&& (existing_wcs_pathname == NULL || existing_wcs_pathname[0] == '\0')) { |
1536 |
archive_strappend_char(as, '/'); |
1699 |
archive_string_init(&as); |
1537 |
archive_strncat(as, header->name, sizeof(header->name)); |
1700 |
if (header->prefix[0]) { |
1538 |
} else { |
1701 |
archive_strncpy(&as, header->prefix, sizeof(header->prefix)); |
1539 |
archive_strncpy(as, header->name, sizeof(header->name)); |
1702 |
if (as.s[archive_strlen(&as) - 1] != '/') |
1540 |
} |
1703 |
archive_strappend_char(&as, '/'); |
1541 |
if (archive_entry_copy_pathname_l(entry, as->s, archive_strlen(as), |
1704 |
archive_strncat(&as, header->name, sizeof(header->name)); |
1542 |
tar->sconv) != 0) { |
1705 |
} else { |
1543 |
err = set_conversion_failed_error(a, tar->sconv, "Pathname"); |
1706 |
archive_strncpy(&as, header->name, sizeof(header->name)); |
1544 |
if (err == ARCHIVE_FATAL) |
1707 |
} |
1545 |
return (err); |
1708 |
if (archive_entry_copy_pathname_l(entry, as.s, archive_strlen(&as), |
|
|
1709 |
tar->sconv) != 0) { |
1710 |
err = set_conversion_failed_error(a, tar->sconv, "Pathname"); |
1711 |
if (err == ARCHIVE_FATAL) |
1712 |
return (err); |
1713 |
} |
1714 |
archive_string_free(&as); |
1546 |
} |
1715 |
} |
1547 |
|
1716 |
|
1548 |
/* Handle rest of common fields. */ |
1717 |
/* Handle rest of common fields. */ |
Lines 1553-1578
header_ustar(struct archive_read *a, struct tar *tar,
Link Here
|
1553 |
err = r; |
1722 |
err = r; |
1554 |
|
1723 |
|
1555 |
/* Handle POSIX ustar fields. */ |
1724 |
/* Handle POSIX ustar fields. */ |
1556 |
if (archive_entry_copy_uname_l(entry, |
1725 |
const char *existing_uname = archive_entry_uname(entry); |
1557 |
header->uname, sizeof(header->uname), tar->sconv) != 0) { |
1726 |
if (existing_uname == NULL || existing_uname[0] == '\0') { |
1558 |
err = set_conversion_failed_error(a, tar->sconv, "Uname"); |
1727 |
if (archive_entry_copy_uname_l(entry, |
1559 |
if (err == ARCHIVE_FATAL) |
1728 |
header->uname, sizeof(header->uname), tar->sconv) != 0) { |
1560 |
return (err); |
1729 |
err = set_conversion_failed_error(a, tar->sconv, "Uname"); |
|
|
1730 |
if (err == ARCHIVE_FATAL) |
1731 |
return (err); |
1732 |
} |
1561 |
} |
1733 |
} |
1562 |
|
1734 |
|
1563 |
if (archive_entry_copy_gname_l(entry, |
1735 |
const char *existing_gname = archive_entry_gname(entry); |
1564 |
header->gname, sizeof(header->gname), tar->sconv) != 0) { |
1736 |
if (existing_gname == NULL || existing_gname[0] == '\0') { |
1565 |
err = set_conversion_failed_error(a, tar->sconv, "Gname"); |
1737 |
if (archive_entry_copy_gname_l(entry, |
1566 |
if (err == ARCHIVE_FATAL) |
1738 |
header->gname, sizeof(header->gname), tar->sconv) != 0) { |
1567 |
return (err); |
1739 |
err = set_conversion_failed_error(a, tar->sconv, "Gname"); |
|
|
1740 |
if (err == ARCHIVE_FATAL) |
1741 |
return (err); |
1742 |
} |
1568 |
} |
1743 |
} |
1569 |
|
1744 |
|
1570 |
/* Parse out device numbers only for char and block specials. */ |
1745 |
/* Parse out device numbers only for char and block specials. */ |
1571 |
if (header->typeflag[0] == '3' || header->typeflag[0] == '4') { |
1746 |
if (header->typeflag[0] == '3' || header->typeflag[0] == '4') { |
1572 |
archive_entry_set_rdevmajor(entry, (dev_t) |
1747 |
if (!archive_entry_rdev_is_set(entry)) { |
1573 |
tar_atol(header->rdevmajor, sizeof(header->rdevmajor))); |
1748 |
archive_entry_set_rdevmajor(entry, (dev_t) |
1574 |
archive_entry_set_rdevminor(entry, (dev_t) |
1749 |
tar_atol(header->rdevmajor, sizeof(header->rdevmajor))); |
1575 |
tar_atol(header->rdevminor, sizeof(header->rdevminor))); |
1750 |
archive_entry_set_rdevminor(entry, (dev_t) |
|
|
1751 |
tar_atol(header->rdevminor, sizeof(header->rdevminor))); |
1752 |
} |
1753 |
} else { |
1754 |
archive_entry_set_rdev(entry, 0); |
1576 |
} |
1755 |
} |
1577 |
|
1756 |
|
1578 |
tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining); |
1757 |
tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining); |
Lines 1580-1696
header_ustar(struct archive_read *a, struct tar *tar,
Link Here
|
1580 |
return (err); |
1759 |
return (err); |
1581 |
} |
1760 |
} |
1582 |
|
1761 |
|
1583 |
|
|
|
1584 |
/* |
1585 |
* Parse the pax extended attributes record. |
1586 |
* |
1587 |
* Returns non-zero if there's an error in the data. |
1588 |
*/ |
1589 |
static int |
1762 |
static int |
1590 |
pax_header(struct archive_read *a, struct tar *tar, |
1763 |
header_pax_extension(struct archive_read *a, struct tar *tar, |
1591 |
struct archive_entry *entry, struct archive_string *in_as) |
1764 |
struct archive_entry *entry, const void *h, size_t *unconsumed) |
1592 |
{ |
1765 |
{ |
1593 |
size_t attr_length, l, line_length, value_length; |
1766 |
/* Sanity checks: The largest `x` body I've ever heard of was |
1594 |
char *p; |
1767 |
* a little over 4MB. So I doubt there has ever been a |
1595 |
char *key, *value; |
1768 |
* well-formed archive with an `x` body over 1GiB. Similarly, |
1596 |
struct archive_string *as; |
1769 |
* it seems plausible that no single attribute has ever been |
|
|
1770 |
* larger than 100MB. So if we see a larger value here, it's |
1771 |
* almost certainly a sign of a corrupted/malicious archive. */ |
1772 |
|
1773 |
/* Maximum sane size for extension body: 1 GiB */ |
1774 |
/* This cannot be raised to larger than 8GiB without |
1775 |
* exceeding the maximum size for a standard ustar |
1776 |
* entry. */ |
1777 |
const int64_t ext_size_limit = 1024 * 1024 * (int64_t)1024; |
1778 |
/* Maximum size for a single line/attr: 100 million characters */ |
1779 |
/* This cannot be raised to more than 2GiB without exceeding |
1780 |
* a `size_t` on 32-bit platforms. */ |
1781 |
const size_t max_parsed_line_length = 99999999ULL; |
1782 |
/* Largest attribute prolog: size + name. */ |
1783 |
const size_t max_size_name = 512; |
1784 |
|
1785 |
/* Size and padding of the full extension body */ |
1786 |
int64_t ext_size, ext_padding; |
1787 |
size_t line_length, value_length, name_length; |
1788 |
ssize_t to_read, did_read; |
1789 |
const struct archive_entry_header_ustar *header; |
1790 |
const char *p, *attr_start, *name_start; |
1597 |
struct archive_string_conv *sconv; |
1791 |
struct archive_string_conv *sconv; |
1598 |
int err, err2; |
1792 |
struct archive_string *pas = NULL; |
1599 |
char *attr = in_as->s; |
1793 |
struct archive_string attr_name; |
|
|
1794 |
int err = ARCHIVE_OK, r; |
1600 |
|
1795 |
|
1601 |
attr_length = in_as->length; |
1796 |
header = (const struct archive_entry_header_ustar *)h; |
1602 |
tar->pax_hdrcharset_binary = 0; |
1797 |
ext_size = tar_atol(header->size, sizeof(header->size)); |
1603 |
archive_string_empty(&(tar->entry_gname)); |
1798 |
if (ext_size > entry_limit) { |
1604 |
archive_string_empty(&(tar->entry_linkpath)); |
1799 |
return (ARCHIVE_FATAL); |
1605 |
archive_string_empty(&(tar->entry_pathname)); |
1800 |
} |
1606 |
archive_string_empty(&(tar->entry_pathname_override)); |
1801 |
if (ext_size < 0) { |
1607 |
archive_string_empty(&(tar->entry_uname)); |
1802 |
archive_set_error(&a->archive, EINVAL, |
1608 |
err = ARCHIVE_OK; |
1803 |
"pax extension header has invalid size: %lld", |
1609 |
while (attr_length > 0) { |
1804 |
(long long)ext_size); |
1610 |
/* Parse decimal length field at start of line. */ |
1805 |
return (ARCHIVE_FATAL); |
|
|
1806 |
} |
1807 |
|
1808 |
ext_padding = 0x1ff & (-ext_size); |
1809 |
if (ext_size > ext_size_limit) { |
1810 |
/* Consume the pax extension body and return an error */ |
1811 |
if (ext_size + ext_padding != __archive_read_consume(a, ext_size + ext_padding)) { |
1812 |
return (ARCHIVE_FATAL); |
1813 |
} |
1814 |
archive_set_error(&a->archive, EINVAL, |
1815 |
"Ignoring oversized pax extensions: %d > %d", |
1816 |
(int)ext_size, (int)ext_size_limit); |
1817 |
return (ARCHIVE_WARN); |
1818 |
} |
1819 |
tar_flush_unconsumed(a, unconsumed); |
1820 |
|
1821 |
/* Parse the size/name of each pax attribute in the body */ |
1822 |
archive_string_init(&attr_name); |
1823 |
while (ext_size > 0) { |
1824 |
/* Read enough bytes to parse the size/name of the next attribute */ |
1825 |
to_read = max_size_name; |
1826 |
if (to_read > ext_size) { |
1827 |
to_read = ext_size; |
1828 |
} |
1829 |
p = __archive_read_ahead(a, to_read, &did_read); |
1830 |
if (p == NULL) { /* EOF */ |
1831 |
archive_set_error(&a->archive, EINVAL, |
1832 |
"Truncated tar archive" |
1833 |
" detected while reading pax attribute name"); |
1834 |
return (ARCHIVE_FATAL); |
1835 |
} |
1836 |
if (did_read > ext_size) { |
1837 |
did_read = ext_size; |
1838 |
} |
1839 |
|
1840 |
/* Parse size of attribute */ |
1611 |
line_length = 0; |
1841 |
line_length = 0; |
1612 |
l = attr_length; |
1842 |
attr_start = p; |
1613 |
p = attr; /* Record start of line. */ |
1843 |
while (1) { |
1614 |
while (l>0) { |
1844 |
if (p >= attr_start + did_read) { |
|
|
1845 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1846 |
"Ignoring malformed pax attributes: overlarge attribute size field"); |
1847 |
*unconsumed += ext_size + ext_padding; |
1848 |
return (ARCHIVE_WARN); |
1849 |
} |
1615 |
if (*p == ' ') { |
1850 |
if (*p == ' ') { |
1616 |
p++; |
1851 |
p++; |
1617 |
l--; |
|
|
1618 |
break; |
1852 |
break; |
1619 |
} |
1853 |
} |
1620 |
if (*p < '0' || *p > '9') { |
1854 |
if (*p < '0' || *p > '9') { |
1621 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1855 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1622 |
"Ignoring malformed pax extended attributes"); |
1856 |
"Ignoring malformed pax attributes: malformed attribute size field"); |
|
|
1857 |
*unconsumed += ext_size + ext_padding; |
1623 |
return (ARCHIVE_WARN); |
1858 |
return (ARCHIVE_WARN); |
1624 |
} |
1859 |
} |
1625 |
line_length *= 10; |
1860 |
line_length *= 10; |
1626 |
line_length += *p - '0'; |
1861 |
line_length += *p - '0'; |
1627 |
if (line_length > 999999) { |
1862 |
if (line_length > max_parsed_line_length) { |
1628 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1863 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1629 |
"Rejecting pax extended attribute > 1MB"); |
1864 |
"Ignoring malformed pax attribute: size > %lld", |
|
|
1865 |
(long long)max_parsed_line_length); |
1866 |
*unconsumed += ext_size + ext_padding; |
1630 |
return (ARCHIVE_WARN); |
1867 |
return (ARCHIVE_WARN); |
1631 |
} |
1868 |
} |
1632 |
p++; |
1869 |
p++; |
1633 |
l--; |
|
|
1634 |
} |
1870 |
} |
1635 |
|
1871 |
|
1636 |
/* |
1872 |
if ((int64_t)line_length > ext_size) { |
1637 |
* Parsed length must be no bigger than available data, |
1873 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1638 |
* at least 1, and the last character of the line must |
1874 |
"Ignoring malformed pax attribute: %lld > %lld", |
1639 |
* be '\n'. |
1875 |
(long long)line_length, (long long)ext_size); |
1640 |
*/ |
1876 |
*unconsumed += ext_size + ext_padding; |
1641 |
if (line_length > attr_length |
1877 |
return (ARCHIVE_WARN); |
1642 |
|| line_length < 1 |
|
|
1643 |
|| attr[line_length - 1] != '\n') |
1644 |
{ |
1645 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1646 |
"Ignoring malformed pax extended attribute"); |
1647 |
return (ARCHIVE_WARN); |
1648 |
} |
1878 |
} |
1649 |
|
1879 |
|
1650 |
/* Null-terminate the line. */ |
1880 |
/* Parse name of attribute */ |
1651 |
attr[line_length - 1] = '\0'; |
1881 |
if (p >= attr_start + did_read |
1652 |
|
1882 |
|| p >= attr_start + line_length |
1653 |
/* Find end of key and null terminate it. */ |
1883 |
|| *p == '=') { |
1654 |
key = p; |
|
|
1655 |
if (key[0] == '=') |
1656 |
return (-1); |
1657 |
while (*p && *p != '=') |
1658 |
++p; |
1659 |
if (*p == '\0') { |
1660 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1884 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1661 |
"Invalid pax extended attributes"); |
1885 |
"Ignoring malformed pax attributes: empty name found"); |
|
|
1886 |
*unconsumed += ext_size + ext_padding; |
1662 |
return (ARCHIVE_WARN); |
1887 |
return (ARCHIVE_WARN); |
1663 |
} |
1888 |
} |
1664 |
*p = '\0'; |
1889 |
name_start = p; |
|
|
1890 |
while (1) { |
1891 |
if (p >= attr_start + did_read || p >= attr_start + line_length) { |
1892 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1893 |
"Ignoring malformed pax attributes: overlarge attribute name"); |
1894 |
*unconsumed += ext_size + ext_padding; |
1895 |
return (ARCHIVE_WARN); |
1896 |
} |
1897 |
if (*p == '=') { |
1898 |
break; |
1899 |
} |
1900 |
p++; |
1901 |
} |
1902 |
name_length = p - name_start; |
1903 |
p++; // Skip '=' |
1665 |
|
1904 |
|
1666 |
value = p + 1; |
1905 |
// Save the name before we consume it |
|
|
1906 |
archive_strncpy(&attr_name, name_start, name_length); |
1667 |
|
1907 |
|
1668 |
/* Some values may be binary data */ |
1908 |
ext_size -= p - attr_start; |
1669 |
value_length = attr + line_length - 1 - value; |
1909 |
value_length = line_length - (p - attr_start); |
1670 |
|
1910 |
|
1671 |
/* Identify this attribute and set it in the entry. */ |
1911 |
/* Consume size, name, and `=` */ |
1672 |
err2 = pax_attribute(a, tar, entry, key, value, value_length); |
1912 |
*unconsumed += p - attr_start; |
1673 |
if (err2 == ARCHIVE_FATAL) |
1913 |
tar_flush_unconsumed(a, unconsumed); |
1674 |
return (err2); |
1914 |
|
1675 |
err = err_combine(err, err2); |
1915 |
/* pax_attribute will consume value_length - 1 */ |
|
|
1916 |
r = pax_attribute(a, tar, entry, attr_name.s, archive_strlen(&attr_name), value_length - 1, unconsumed); |
1917 |
ext_size -= value_length - 1; |
1918 |
|
1919 |
// Release the allocated attr_name (either here or before every return in this function) |
1920 |
archive_string_free(&attr_name); |
1676 |
|
1921 |
|
1677 |
/* Skip to next line */ |
1922 |
if (r < ARCHIVE_WARN) { |
1678 |
attr += line_length; |
1923 |
*unconsumed += ext_size + ext_padding; |
1679 |
attr_length -= line_length; |
1924 |
return (r); |
|
|
1925 |
} |
1926 |
err = err_combine(err, r); |
1927 |
|
1928 |
/* Consume the `\n` that follows the pax attribute value. */ |
1929 |
tar_flush_unconsumed(a, unconsumed); |
1930 |
p = __archive_read_ahead(a, 1, &did_read); |
1931 |
if (p == NULL) { |
1932 |
archive_set_error(&a->archive, EINVAL, |
1933 |
"Truncated tar archive" |
1934 |
" detected while completing pax attribute"); |
1935 |
return (ARCHIVE_FATAL); |
1936 |
} |
1937 |
if (p[0] != '\n') { |
1938 |
archive_set_error(&a->archive, EINVAL, |
1939 |
"Malformed pax attributes"); |
1940 |
*unconsumed += ext_size + ext_padding; |
1941 |
return (ARCHIVE_WARN); |
1942 |
} |
1943 |
ext_size -= 1; |
1944 |
*unconsumed += 1; |
1945 |
tar_flush_unconsumed(a, unconsumed); |
1680 |
} |
1946 |
} |
|
|
1947 |
*unconsumed += ext_size + ext_padding; |
1681 |
|
1948 |
|
1682 |
/* |
1949 |
/* |
1683 |
* PAX format uses UTF-8 as default charset for its metadata |
1950 |
* Some PAX values -- pathname, linkpath, uname, gname -- |
1684 |
* unless hdrcharset=BINARY is present in its header. |
1951 |
* can't be copied into the entry until we know the character |
1685 |
* We apply the charset specified by the hdrcharset option only |
1952 |
* set to use: |
1686 |
* when the hdrcharset attribute(in PAX header) is BINARY because |
|
|
1687 |
* we respect the charset described in PAX header and BINARY also |
1688 |
* means that metadata(filename,uname and gname) character-set |
1689 |
* is unknown. |
1690 |
*/ |
1953 |
*/ |
1691 |
if (tar->pax_hdrcharset_binary) |
1954 |
if (!tar->pax_hdrcharset_utf8) |
|
|
1955 |
/* PAX specified "BINARY", so use the default charset */ |
1692 |
sconv = tar->opt_sconv; |
1956 |
sconv = tar->opt_sconv; |
1693 |
else { |
1957 |
else { |
|
|
1958 |
/* PAX default UTF-8 */ |
1694 |
sconv = archive_string_conversion_from_charset( |
1959 |
sconv = archive_string_conversion_from_charset( |
1695 |
&(a->archive), "UTF-8", 1); |
1960 |
&(a->archive), "UTF-8", 1); |
1696 |
if (sconv == NULL) |
1961 |
if (sconv == NULL) |
Lines 1700-1782
pax_header(struct archive_read *a, struct tar *tar,
Link Here
|
1700 |
SCONV_SET_OPT_UTF8_LIBARCHIVE2X); |
1965 |
SCONV_SET_OPT_UTF8_LIBARCHIVE2X); |
1701 |
} |
1966 |
} |
1702 |
|
1967 |
|
|
|
1968 |
/* Pathname */ |
1969 |
pas = NULL; |
1970 |
if (archive_strlen(&(tar->entry_pathname_override)) > 0) { |
1971 |
/* Prefer GNU.sparse.name attribute if present */ |
1972 |
/* GNU sparse files store a fake name under the standard |
1973 |
* "pathname" key. */ |
1974 |
pas = &(tar->entry_pathname_override); |
1975 |
} else if (archive_strlen(&(tar->entry_pathname)) > 0) { |
1976 |
/* Use standard "pathname" PAX extension */ |
1977 |
pas = &(tar->entry_pathname); |
1978 |
} |
1979 |
if (pas != NULL) { |
1980 |
if (archive_entry_copy_pathname_l(entry, pas->s, |
1981 |
archive_strlen(pas), sconv) != 0) { |
1982 |
err = set_conversion_failed_error(a, sconv, "Pathname"); |
1983 |
if (err == ARCHIVE_FATAL) |
1984 |
return (err); |
1985 |
/* Use raw name without conversion */ |
1986 |
archive_entry_copy_pathname(entry, pas->s); |
1987 |
} |
1988 |
} |
1989 |
/* Uname */ |
1990 |
if (archive_strlen(&(tar->entry_uname)) > 0) { |
1991 |
if (archive_entry_copy_uname_l(entry, tar->entry_uname.s, |
1992 |
archive_strlen(&(tar->entry_uname)), sconv) != 0) { |
1993 |
err = set_conversion_failed_error(a, sconv, "Uname"); |
1994 |
if (err == ARCHIVE_FATAL) |
1995 |
return (err); |
1996 |
/* Use raw name without conversion */ |
1997 |
archive_entry_copy_uname(entry, tar->entry_uname.s); |
1998 |
} |
1999 |
} |
2000 |
/* Gname */ |
1703 |
if (archive_strlen(&(tar->entry_gname)) > 0) { |
2001 |
if (archive_strlen(&(tar->entry_gname)) > 0) { |
1704 |
if (archive_entry_copy_gname_l(entry, tar->entry_gname.s, |
2002 |
if (archive_entry_copy_gname_l(entry, tar->entry_gname.s, |
1705 |
archive_strlen(&(tar->entry_gname)), sconv) != 0) { |
2003 |
archive_strlen(&(tar->entry_gname)), sconv) != 0) { |
1706 |
err = set_conversion_failed_error(a, sconv, "Gname"); |
2004 |
err = set_conversion_failed_error(a, sconv, "Gname"); |
1707 |
if (err == ARCHIVE_FATAL) |
2005 |
if (err == ARCHIVE_FATAL) |
1708 |
return (err); |
2006 |
return (err); |
1709 |
/* Use a converted an original name. */ |
2007 |
/* Use raw name without conversion */ |
1710 |
archive_entry_copy_gname(entry, tar->entry_gname.s); |
2008 |
archive_entry_copy_gname(entry, tar->entry_gname.s); |
1711 |
} |
2009 |
} |
1712 |
} |
2010 |
} |
|
|
2011 |
/* Linkpath */ |
1713 |
if (archive_strlen(&(tar->entry_linkpath)) > 0) { |
2012 |
if (archive_strlen(&(tar->entry_linkpath)) > 0) { |
1714 |
if (archive_entry_copy_link_l(entry, tar->entry_linkpath.s, |
2013 |
if (archive_entry_copy_link_l(entry, tar->entry_linkpath.s, |
1715 |
archive_strlen(&(tar->entry_linkpath)), sconv) != 0) { |
2014 |
archive_strlen(&(tar->entry_linkpath)), sconv) != 0) { |
1716 |
err = set_conversion_failed_error(a, sconv, "Linkname"); |
2015 |
err = set_conversion_failed_error(a, sconv, "Linkpath"); |
1717 |
if (err == ARCHIVE_FATAL) |
2016 |
if (err == ARCHIVE_FATAL) |
1718 |
return (err); |
2017 |
return (err); |
1719 |
/* Use a converted an original name. */ |
2018 |
/* Use raw name without conversion */ |
1720 |
archive_entry_copy_link(entry, tar->entry_linkpath.s); |
2019 |
archive_entry_copy_link(entry, tar->entry_linkpath.s); |
1721 |
} |
2020 |
} |
1722 |
} |
2021 |
} |
1723 |
/* |
2022 |
|
1724 |
* Some extensions (such as the GNU sparse file extensions) |
2023 |
/* Extension may have given us a corrected `entry_bytes_remaining` for |
1725 |
* deliberately store a synthetic name under the regular 'path' |
2024 |
* the main entry; update the padding appropriately. */ |
1726 |
* attribute and the real file name under a different attribute. |
2025 |
tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining); |
1727 |
* Since we're supposed to not care about the order, we |
|
|
1728 |
* have no choice but to store all of the various filenames |
1729 |
* we find and figure it all out afterwards. This is the |
1730 |
* figuring out part. |
1731 |
*/ |
1732 |
as = NULL; |
1733 |
if (archive_strlen(&(tar->entry_pathname_override)) > 0) |
1734 |
as = &(tar->entry_pathname_override); |
1735 |
else if (archive_strlen(&(tar->entry_pathname)) > 0) |
1736 |
as = &(tar->entry_pathname); |
1737 |
if (as != NULL) { |
1738 |
if (archive_entry_copy_pathname_l(entry, as->s, |
1739 |
archive_strlen(as), sconv) != 0) { |
1740 |
err = set_conversion_failed_error(a, sconv, "Pathname"); |
1741 |
if (err == ARCHIVE_FATAL) |
1742 |
return (err); |
1743 |
/* Use a converted an original name. */ |
1744 |
archive_entry_copy_pathname(entry, as->s); |
1745 |
} |
1746 |
} |
1747 |
if (archive_strlen(&(tar->entry_uname)) > 0) { |
1748 |
if (archive_entry_copy_uname_l(entry, tar->entry_uname.s, |
1749 |
archive_strlen(&(tar->entry_uname)), sconv) != 0) { |
1750 |
err = set_conversion_failed_error(a, sconv, "Uname"); |
1751 |
if (err == ARCHIVE_FATAL) |
1752 |
return (err); |
1753 |
/* Use a converted an original name. */ |
1754 |
archive_entry_copy_uname(entry, tar->entry_uname.s); |
1755 |
} |
1756 |
} |
1757 |
return (err); |
2026 |
return (err); |
1758 |
} |
2027 |
} |
1759 |
|
2028 |
|
1760 |
static int |
2029 |
static int |
1761 |
pax_attribute_xattr(struct archive_entry *entry, |
2030 |
pax_attribute_LIBARCHIVE_xattr(struct archive_entry *entry, |
1762 |
const char *name, const char *value) |
2031 |
const char *name, size_t name_length, const char *value, size_t value_length) |
1763 |
{ |
2032 |
{ |
1764 |
char *name_decoded; |
2033 |
char *name_decoded; |
1765 |
void *value_decoded; |
2034 |
void *value_decoded; |
1766 |
size_t value_len; |
2035 |
size_t value_len; |
1767 |
|
2036 |
|
1768 |
if (strlen(name) < 18 || (memcmp(name, "LIBARCHIVE.xattr.", 17)) != 0) |
2037 |
if (name_length < 1) |
1769 |
return 3; |
2038 |
return 3; |
1770 |
|
2039 |
|
1771 |
name += 17; |
|
|
1772 |
|
1773 |
/* URL-decode name */ |
2040 |
/* URL-decode name */ |
1774 |
name_decoded = url_decode(name); |
2041 |
name_decoded = url_decode(name, name_length); |
1775 |
if (name_decoded == NULL) |
2042 |
if (name_decoded == NULL) |
1776 |
return 2; |
2043 |
return 2; |
1777 |
|
2044 |
|
1778 |
/* Base-64 decode value */ |
2045 |
/* Base-64 decode value */ |
1779 |
value_decoded = base64_decode(value, strlen(value), &value_len); |
2046 |
value_decoded = base64_decode(value, value_length, &value_len); |
1780 |
if (value_decoded == NULL) { |
2047 |
if (value_decoded == NULL) { |
1781 |
free(name_decoded); |
2048 |
free(name_decoded); |
1782 |
return 1; |
2049 |
return 1; |
Lines 1791-1811
pax_attribute_xattr(struct archive_entry *entry,
Link Here
|
1791 |
} |
2058 |
} |
1792 |
|
2059 |
|
1793 |
static int |
2060 |
static int |
1794 |
pax_attribute_schily_xattr(struct archive_entry *entry, |
2061 |
pax_attribute_SCHILY_xattr(struct archive_entry *entry, |
1795 |
const char *name, const char *value, size_t value_length) |
2062 |
const char *name, size_t name_length, const char *value, size_t value_length) |
1796 |
{ |
2063 |
{ |
1797 |
if (strlen(name) < 14 || (memcmp(name, "SCHILY.xattr.", 13)) != 0) |
2064 |
if (name_length < 1 || name_length > 128) { |
1798 |
return 1; |
2065 |
return 1; |
|
|
2066 |
} |
1799 |
|
2067 |
|
1800 |
name += 13; |
2068 |
char * null_terminated_name = malloc(name_length + 1); |
1801 |
|
2069 |
if (null_terminated_name != NULL) { |
1802 |
archive_entry_xattr_add_entry(entry, name, value, value_length); |
2070 |
memcpy(null_terminated_name, name, name_length); |
|
|
2071 |
null_terminated_name[name_length] = '\0'; |
2072 |
archive_entry_xattr_add_entry(entry, null_terminated_name, value, value_length); |
2073 |
free(null_terminated_name); |
2074 |
} |
1803 |
|
2075 |
|
1804 |
return 0; |
2076 |
return 0; |
1805 |
} |
2077 |
} |
1806 |
|
2078 |
|
1807 |
static int |
2079 |
static int |
1808 |
pax_attribute_rht_security_selinux(struct archive_entry *entry, |
2080 |
pax_attribute_RHT_security_selinux(struct archive_entry *entry, |
1809 |
const char *value, size_t value_length) |
2081 |
const char *value, size_t value_length) |
1810 |
{ |
2082 |
{ |
1811 |
archive_entry_xattr_add_entry(entry, "security.selinux", |
2083 |
archive_entry_xattr_add_entry(entry, "security.selinux", |
Lines 1815-1824
pax_attribute_rht_security_selinux(struct archive_entry *entry,
Link Here
|
1815 |
} |
2087 |
} |
1816 |
|
2088 |
|
1817 |
static int |
2089 |
static int |
1818 |
pax_attribute_acl(struct archive_read *a, struct tar *tar, |
2090 |
pax_attribute_SCHILY_acl(struct archive_read *a, struct tar *tar, |
1819 |
struct archive_entry *entry, const char *value, int type) |
2091 |
struct archive_entry *entry, size_t value_length, int type) |
1820 |
{ |
2092 |
{ |
1821 |
int r; |
2093 |
int r; |
|
|
2094 |
const char *p; |
1822 |
const char* errstr; |
2095 |
const char* errstr; |
1823 |
|
2096 |
|
1824 |
switch (type) { |
2097 |
switch (type) { |
Lines 1845-1852
pax_attribute_acl(struct archive_read *a, struct tar *tar,
Link Here
|
1845 |
return (ARCHIVE_FATAL); |
2118 |
return (ARCHIVE_FATAL); |
1846 |
} |
2119 |
} |
1847 |
|
2120 |
|
1848 |
r = archive_acl_from_text_l(archive_entry_acl(entry), value, type, |
2121 |
if (value_length > acl_limit) { |
1849 |
tar->sconv_acl); |
2122 |
__archive_read_consume(a, value_length); |
|
|
2123 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
2124 |
"Unreasonably large ACL: %d > %d", |
2125 |
(int)value_length, (int)acl_limit); |
2126 |
return (ARCHIVE_WARN); |
2127 |
} |
2128 |
|
2129 |
p = __archive_read_ahead(a, value_length, NULL); |
2130 |
if (p == NULL) { |
2131 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, |
2132 |
"Truncated tar archive " |
2133 |
"detected while reading ACL data"); |
2134 |
return (ARCHIVE_FATAL); |
2135 |
} |
2136 |
|
2137 |
r = archive_acl_from_text_nl(archive_entry_acl(entry), p, value_length, |
2138 |
type, tar->sconv_acl); |
2139 |
__archive_read_consume(a, value_length); |
2140 |
/* Workaround: Force perm_is_set() to be correct */ |
2141 |
/* If this bit were stored in the ACL, this wouldn't be needed */ |
2142 |
archive_entry_set_perm(entry, archive_entry_perm(entry)); |
1850 |
if (r != ARCHIVE_OK) { |
2143 |
if (r != ARCHIVE_OK) { |
1851 |
if (r == ARCHIVE_FATAL) { |
2144 |
if (r == ARCHIVE_FATAL) { |
1852 |
archive_set_error(&a->archive, ENOMEM, |
2145 |
archive_set_error(&a->archive, ENOMEM, |
Lines 1860-2101
pax_attribute_acl(struct archive_read *a, struct tar *tar,
Link Here
|
1860 |
return (r); |
2153 |
return (r); |
1861 |
} |
2154 |
} |
1862 |
|
2155 |
|
|
|
2156 |
static int |
2157 |
pax_attribute_read_time(struct archive_read *a, size_t value_length, int64_t *ps, long *pn, size_t *unconsumed) { |
2158 |
struct archive_string as; |
2159 |
int r; |
2160 |
|
2161 |
if (value_length > 128) { |
2162 |
__archive_read_consume(a, value_length); |
2163 |
*ps = 0; |
2164 |
*pn = 0; |
2165 |
return (ARCHIVE_FATAL); |
2166 |
} |
2167 |
|
2168 |
archive_string_init(&as); |
2169 |
r = read_bytes_to_string(a, &as, value_length, unconsumed); |
2170 |
if (r < ARCHIVE_OK) { |
2171 |
archive_string_free(&as); |
2172 |
return (r); |
2173 |
} |
2174 |
|
2175 |
pax_time(as.s, archive_strlen(&as), ps, pn); |
2176 |
archive_string_free(&as); |
2177 |
if (*ps < 0 || *ps == INT64_MAX) { |
2178 |
return (ARCHIVE_WARN); |
2179 |
} |
2180 |
return (ARCHIVE_OK); |
2181 |
} |
2182 |
|
2183 |
static int |
2184 |
pax_attribute_read_number(struct archive_read *a, size_t value_length, int64_t *result) { |
2185 |
struct archive_string as; |
2186 |
size_t unconsumed = 0; |
2187 |
int r; |
2188 |
|
2189 |
if (value_length > 64) { |
2190 |
__archive_read_consume(a, value_length); |
2191 |
*result = 0; |
2192 |
return (ARCHIVE_FATAL); |
2193 |
} |
2194 |
|
2195 |
archive_string_init(&as); |
2196 |
r = read_bytes_to_string(a, &as, value_length, &unconsumed); |
2197 |
tar_flush_unconsumed(a, &unconsumed); |
2198 |
if (r < ARCHIVE_OK) { |
2199 |
archive_string_free(&as); |
2200 |
return (r); |
2201 |
} |
2202 |
|
2203 |
*result = tar_atol10(as.s, archive_strlen(&as)); |
2204 |
archive_string_free(&as); |
2205 |
if (*result < 0 || *result == INT64_MAX) { |
2206 |
*result = INT64_MAX; |
2207 |
return (ARCHIVE_WARN); |
2208 |
} |
2209 |
return (ARCHIVE_OK); |
2210 |
} |
2211 |
|
1863 |
/* |
2212 |
/* |
1864 |
* Parse a single key=value attribute. key/value pointers are |
2213 |
* Parse a single key=value attribute. |
1865 |
* assumed to point into reasonably long-lived storage. |
|
|
1866 |
* |
2214 |
* |
1867 |
* Note that POSIX reserves all-lowercase keywords. Vendor-specific |
2215 |
* POSIX reserves all-lowercase keywords. Vendor-specific extensions |
1868 |
* extensions should always have keywords of the form "VENDOR.attribute" |
2216 |
* should always have keywords of the form "VENDOR.attribute" In |
1869 |
* In particular, it's quite feasible to support many different |
2217 |
* particular, it's quite feasible to support many different vendor |
1870 |
* vendor extensions here. I'm using "LIBARCHIVE" for extensions |
2218 |
* extensions here. I'm using "LIBARCHIVE" for extensions unique to |
1871 |
* unique to this library. |
2219 |
* this library. |
1872 |
* |
2220 |
* |
1873 |
* Investigate other vendor-specific extensions and see if |
2221 |
* TODO: Investigate other vendor-specific extensions and see if |
1874 |
* any of them look useful. |
2222 |
* any of them look useful. |
1875 |
*/ |
2223 |
*/ |
1876 |
static int |
2224 |
static int |
1877 |
pax_attribute(struct archive_read *a, struct tar *tar, |
2225 |
pax_attribute(struct archive_read *a, struct tar *tar, struct archive_entry *entry, |
1878 |
struct archive_entry *entry, const char *key, const char *value, size_t value_length) |
2226 |
const char *key, size_t key_length, size_t value_length, size_t *unconsumed) |
1879 |
{ |
2227 |
{ |
1880 |
int64_t s; |
2228 |
int64_t t; |
1881 |
long n; |
2229 |
long n; |
1882 |
int err = ARCHIVE_OK, r; |
2230 |
const char *p; |
|
|
2231 |
ssize_t bytes_read; |
2232 |
int err = ARCHIVE_OK; |
1883 |
|
2233 |
|
1884 |
#ifndef __FreeBSD__ |
|
|
1885 |
if (value == NULL) |
1886 |
value = ""; /* Disable compiler warning; do not pass |
1887 |
* NULL pointer to strlen(). */ |
1888 |
#endif |
1889 |
switch (key[0]) { |
2234 |
switch (key[0]) { |
1890 |
case 'G': |
2235 |
case 'G': |
1891 |
/* Reject GNU.sparse.* headers on non-regular files. */ |
2236 |
/* GNU.* extensions */ |
1892 |
if (strncmp(key, "GNU.sparse", 10) == 0 && |
2237 |
if (key_length > 4 && memcmp(key, "GNU.", 4) == 0) { |
1893 |
!tar->sparse_allowed) { |
2238 |
key += 4; |
1894 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
2239 |
key_length -= 4; |
1895 |
"Non-regular file cannot be sparse"); |
2240 |
|
1896 |
return (ARCHIVE_FATAL); |
2241 |
/* GNU.sparse marks the existence of GNU sparse information */ |
1897 |
} |
2242 |
if (key_length == 6 && memcmp(key, "sparse", 6) == 0) { |
1898 |
|
2243 |
tar->sparse_gnu_attributes_seen = 1; |
1899 |
/* GNU "0.0" sparse pax format. */ |
|
|
1900 |
if (strcmp(key, "GNU.sparse.numblocks") == 0) { |
1901 |
tar->sparse_offset = -1; |
1902 |
tar->sparse_numbytes = -1; |
1903 |
tar->sparse_gnu_major = 0; |
1904 |
tar->sparse_gnu_minor = 0; |
1905 |
} |
1906 |
if (strcmp(key, "GNU.sparse.offset") == 0) { |
1907 |
tar->sparse_offset = tar_atol10(value, strlen(value)); |
1908 |
if (tar->sparse_numbytes != -1) { |
1909 |
if (gnu_add_sparse_entry(a, tar, |
1910 |
tar->sparse_offset, tar->sparse_numbytes) |
1911 |
!= ARCHIVE_OK) |
1912 |
return (ARCHIVE_FATAL); |
1913 |
tar->sparse_offset = -1; |
1914 |
tar->sparse_numbytes = -1; |
1915 |
} |
2244 |
} |
1916 |
} |
|
|
1917 |
if (strcmp(key, "GNU.sparse.numbytes") == 0) { |
1918 |
tar->sparse_numbytes = tar_atol10(value, strlen(value)); |
1919 |
if (tar->sparse_offset != -1) { |
1920 |
if (gnu_add_sparse_entry(a, tar, |
1921 |
tar->sparse_offset, tar->sparse_numbytes) |
1922 |
!= ARCHIVE_OK) |
1923 |
return (ARCHIVE_FATAL); |
1924 |
tar->sparse_offset = -1; |
1925 |
tar->sparse_numbytes = -1; |
1926 |
} |
1927 |
} |
1928 |
if (strcmp(key, "GNU.sparse.size") == 0) { |
1929 |
tar->realsize = tar_atol10(value, strlen(value)); |
1930 |
archive_entry_set_size(entry, tar->realsize); |
1931 |
tar->realsize_override = 1; |
1932 |
} |
1933 |
|
2245 |
|
1934 |
/* GNU "0.1" sparse pax format. */ |
2246 |
/* GNU.sparse.* extensions */ |
1935 |
if (strcmp(key, "GNU.sparse.map") == 0) { |
2247 |
else if (key_length > 7 && memcmp(key, "sparse.", 7) == 0) { |
1936 |
tar->sparse_gnu_major = 0; |
2248 |
tar->sparse_gnu_attributes_seen = 1; |
1937 |
tar->sparse_gnu_minor = 1; |
2249 |
key += 7; |
1938 |
if (gnu_sparse_01_parse(a, tar, value) != ARCHIVE_OK) |
2250 |
key_length -= 7; |
1939 |
return (ARCHIVE_WARN); |
2251 |
|
1940 |
} |
2252 |
/* GNU "0.0" sparse pax format. */ |
1941 |
|
2253 |
if (key_length == 9 && memcmp(key, "numblocks", 9) == 0) { |
1942 |
/* GNU "1.0" sparse pax format */ |
2254 |
/* GNU.sparse.numblocks */ |
1943 |
if (strcmp(key, "GNU.sparse.major") == 0) { |
2255 |
tar->sparse_offset = -1; |
1944 |
tar->sparse_gnu_major = (int)tar_atol10(value, strlen(value)); |
2256 |
tar->sparse_numbytes = -1; |
1945 |
tar->sparse_gnu_pending = 1; |
2257 |
tar->sparse_gnu_major = 0; |
1946 |
} |
2258 |
tar->sparse_gnu_minor = 0; |
1947 |
if (strcmp(key, "GNU.sparse.minor") == 0) { |
2259 |
} |
1948 |
tar->sparse_gnu_minor = (int)tar_atol10(value, strlen(value)); |
2260 |
else if (key_length == 6 && memcmp(key, "offset", 6) == 0) { |
1949 |
tar->sparse_gnu_pending = 1; |
2261 |
/* GNU.sparse.offset */ |
1950 |
} |
2262 |
if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
1951 |
if (strcmp(key, "GNU.sparse.name") == 0) { |
2263 |
tar->sparse_offset = t; |
1952 |
/* |
2264 |
if (tar->sparse_numbytes != -1) { |
1953 |
* The real filename; when storing sparse |
2265 |
if (gnu_add_sparse_entry(a, tar, |
1954 |
* files, GNU tar puts a synthesized name into |
2266 |
tar->sparse_offset, tar->sparse_numbytes) |
1955 |
* the regular 'path' attribute in an attempt |
2267 |
!= ARCHIVE_OK) |
1956 |
* to limit confusion. ;-) |
2268 |
return (ARCHIVE_FATAL); |
1957 |
*/ |
2269 |
tar->sparse_offset = -1; |
1958 |
archive_strcpy(&(tar->entry_pathname_override), value); |
2270 |
tar->sparse_numbytes = -1; |
1959 |
} |
2271 |
} |
1960 |
if (strcmp(key, "GNU.sparse.realsize") == 0) { |
2272 |
} |
1961 |
tar->realsize = tar_atol10(value, strlen(value)); |
2273 |
return (err); |
1962 |
archive_entry_set_size(entry, tar->realsize); |
2274 |
} |
1963 |
tar->realsize_override = 1; |
2275 |
else if (key_length == 8 && memcmp(key, "numbytes", 8) == 0) { |
|
|
2276 |
/* GNU.sparse.numbytes */ |
2277 |
if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2278 |
tar->sparse_numbytes = t; |
2279 |
if (tar->sparse_offset != -1) { |
2280 |
if (gnu_add_sparse_entry(a, tar, |
2281 |
tar->sparse_offset, tar->sparse_numbytes) |
2282 |
!= ARCHIVE_OK) |
2283 |
return (ARCHIVE_FATAL); |
2284 |
tar->sparse_offset = -1; |
2285 |
tar->sparse_numbytes = -1; |
2286 |
} |
2287 |
} |
2288 |
return (err); |
2289 |
} |
2290 |
else if (key_length == 4 && memcmp(key, "size", 4) == 0) { |
2291 |
/* GNU.sparse.size */ |
2292 |
if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2293 |
tar->realsize = t; |
2294 |
archive_entry_set_size(entry, tar->realsize); |
2295 |
tar->realsize_override = 1; |
2296 |
} |
2297 |
return (err); |
2298 |
} |
2299 |
|
2300 |
/* GNU "0.1" sparse pax format. */ |
2301 |
else if (key_length == 3 && memcmp(key, "map", 3) == 0) { |
2302 |
/* GNU.sparse.map */ |
2303 |
tar->sparse_gnu_major = 0; |
2304 |
tar->sparse_gnu_minor = 1; |
2305 |
if (value_length > sparse_map_limit) { |
2306 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
2307 |
"Unreasonably large sparse map: %d > %d", |
2308 |
(int)value_length, (int)sparse_map_limit); |
2309 |
err = ARCHIVE_FAILED; |
2310 |
} else { |
2311 |
p = __archive_read_ahead(a, value_length, &bytes_read); |
2312 |
if (p == NULL) { |
2313 |
archive_set_error(&a->archive, EINVAL, |
2314 |
"Truncated archive" |
2315 |
" detected while reading GNU sparse data"); |
2316 |
return (ARCHIVE_FATAL); |
2317 |
} |
2318 |
if (gnu_sparse_01_parse(a, tar, p, value_length) != ARCHIVE_OK) { |
2319 |
err = ARCHIVE_WARN; |
2320 |
} |
2321 |
} |
2322 |
__archive_read_consume(a, value_length); |
2323 |
return (err); |
2324 |
} |
2325 |
|
2326 |
/* GNU "1.0" sparse pax format */ |
2327 |
else if (key_length == 5 && memcmp(key, "major", 5) == 0) { |
2328 |
/* GNU.sparse.major */ |
2329 |
if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK |
2330 |
&& t >= 0 |
2331 |
&& t <= 10) { |
2332 |
tar->sparse_gnu_major = (int)t; |
2333 |
} |
2334 |
return (err); |
2335 |
} |
2336 |
else if (key_length == 5 && memcmp(key, "minor", 5) == 0) { |
2337 |
/* GNU.sparse.minor */ |
2338 |
if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK |
2339 |
&& t >= 0 |
2340 |
&& t <= 10) { |
2341 |
tar->sparse_gnu_minor = (int)t; |
2342 |
} |
2343 |
return (err); |
2344 |
} |
2345 |
else if (key_length == 4 && memcmp(key, "name", 4) == 0) { |
2346 |
/* GNU.sparse.name */ |
2347 |
/* |
2348 |
* The real filename; when storing sparse |
2349 |
* files, GNU tar puts a synthesized name into |
2350 |
* the regular 'path' attribute in an attempt |
2351 |
* to limit confusion. ;-) |
2352 |
*/ |
2353 |
if (value_length > pathname_limit) { |
2354 |
*unconsumed += value_length; |
2355 |
err = ARCHIVE_WARN; |
2356 |
} else { |
2357 |
err = read_bytes_to_string(a, &(tar->entry_pathname_override), |
2358 |
value_length, unconsumed); |
2359 |
} |
2360 |
return (err); |
2361 |
} |
2362 |
else if (key_length == 8 && memcmp(key, "realsize", 8) == 0) { |
2363 |
/* GNU.sparse.realsize */ |
2364 |
if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2365 |
tar->realsize = t; |
2366 |
archive_entry_set_size(entry, tar->realsize); |
2367 |
tar->realsize_override = 1; |
2368 |
} |
2369 |
return (err); |
2370 |
} |
2371 |
} |
1964 |
} |
2372 |
} |
1965 |
break; |
2373 |
break; |
1966 |
case 'L': |
2374 |
case 'L': |
1967 |
/* Our extensions */ |
2375 |
/* LIBARCHIVE extensions */ |
1968 |
/* TODO: Handle arbitrary extended attributes... */ |
2376 |
if (key_length > 11 && memcmp(key, "LIBARCHIVE.", 11) == 0) { |
1969 |
/* |
2377 |
key_length -= 11; |
1970 |
if (strcmp(key, "LIBARCHIVE.xxxxxxx") == 0) |
2378 |
key += 11; |
1971 |
archive_entry_set_xxxxxx(entry, value); |
2379 |
|
1972 |
*/ |
2380 |
/* TODO: Handle arbitrary extended attributes... */ |
1973 |
if (strcmp(key, "LIBARCHIVE.creationtime") == 0) { |
2381 |
/* |
1974 |
pax_time(value, &s, &n); |
2382 |
if (strcmp(key, "LIBARCHIVE.xxxxxxx") == 0) |
1975 |
archive_entry_set_birthtime(entry, s, n); |
2383 |
archive_entry_set_xxxxxx(entry, value); |
1976 |
} |
2384 |
*/ |
1977 |
if (strcmp(key, "LIBARCHIVE.symlinktype") == 0) { |
2385 |
if (key_length == 12 && memcmp(key, "creationtime", 12) == 0) { |
1978 |
if (strcmp(value, "file") == 0) { |
2386 |
/* LIBARCHIVE.creationtime */ |
1979 |
archive_entry_set_symlink_type(entry, |
2387 |
if ((err = pax_attribute_read_time(a, value_length, &t, &n, unconsumed)) == ARCHIVE_OK) { |
1980 |
AE_SYMLINK_TYPE_FILE); |
2388 |
archive_entry_set_birthtime(entry, t, n); |
1981 |
} else if (strcmp(value, "dir") == 0) { |
2389 |
} |
1982 |
archive_entry_set_symlink_type(entry, |
2390 |
return (err); |
1983 |
AE_SYMLINK_TYPE_DIRECTORY); |
2391 |
} |
|
|
2392 |
else if (key_length == 11 && memcmp(key, "symlinktype", 11) == 0) { |
2393 |
/* LIBARCHIVE.symlinktype */ |
2394 |
if (value_length < 16) { |
2395 |
p = __archive_read_ahead(a, value_length, &bytes_read); |
2396 |
if (p == NULL) { |
2397 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, |
2398 |
"Truncated tar archive " |
2399 |
"detected while reading `symlinktype` attribute"); |
2400 |
return (ARCHIVE_FATAL); |
2401 |
} |
2402 |
if (value_length == 4 && memcmp(p, "file", 4) == 0) { |
2403 |
archive_entry_set_symlink_type(entry, |
2404 |
AE_SYMLINK_TYPE_FILE); |
2405 |
} else if (value_length == 3 && memcmp(p, "dir", 3) == 0) { |
2406 |
archive_entry_set_symlink_type(entry, |
2407 |
AE_SYMLINK_TYPE_DIRECTORY); |
2408 |
} else { |
2409 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
2410 |
"Unrecognized symlink type"); |
2411 |
err = ARCHIVE_WARN; |
2412 |
} |
2413 |
} else { |
2414 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
2415 |
"symlink type is very long" |
2416 |
"(longest recognized value is 4 bytes, this is %d)", |
2417 |
(int)value_length); |
2418 |
err = ARCHIVE_WARN; |
2419 |
} |
2420 |
__archive_read_consume(a, value_length); |
2421 |
return (err); |
2422 |
} |
2423 |
else if (key_length > 6 && memcmp(key, "xattr.", 6) == 0) { |
2424 |
key_length -= 6; |
2425 |
key += 6; |
2426 |
if (value_length > xattr_limit) { |
2427 |
err = ARCHIVE_WARN; |
2428 |
} else { |
2429 |
p = __archive_read_ahead(a, value_length, &bytes_read); |
2430 |
if (p == NULL) { |
2431 |
archive_set_error(&a->archive, EINVAL, |
2432 |
"Truncated archive" |
2433 |
" detected while reading xattr information"); |
2434 |
return (ARCHIVE_FATAL); |
2435 |
} |
2436 |
if (pax_attribute_LIBARCHIVE_xattr(entry, key, key_length, p, value_length)) { |
2437 |
/* TODO: Unable to parse xattr */ |
2438 |
err = ARCHIVE_WARN; |
2439 |
} |
2440 |
} |
2441 |
__archive_read_consume(a, value_length); |
2442 |
return (err); |
1984 |
} |
2443 |
} |
1985 |
} |
2444 |
} |
1986 |
if (memcmp(key, "LIBARCHIVE.xattr.", 17) == 0) |
|
|
1987 |
pax_attribute_xattr(entry, key, value); |
1988 |
break; |
2445 |
break; |
1989 |
case 'R': |
2446 |
case 'R': |
1990 |
/* GNU tar uses RHT.security header to store SELinux xattrs |
2447 |
/* GNU tar uses RHT.security header to store SELinux xattrs |
1991 |
* SCHILY.xattr.security.selinux == RHT.security.selinux */ |
2448 |
* SCHILY.xattr.security.selinux == RHT.security.selinux */ |
1992 |
if (strcmp(key, "RHT.security.selinux") == 0) { |
2449 |
if (key_length == 20 && memcmp(key, "RHT.security.selinux", 20) == 0) { |
1993 |
pax_attribute_rht_security_selinux(entry, value, |
2450 |
if (value_length > xattr_limit) { |
1994 |
value_length); |
2451 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
|
|
2452 |
"Ignoring unreasonably large security.selinux attribute:" |
2453 |
" %d > %d", |
2454 |
(int)value_length, (int)xattr_limit); |
2455 |
/* TODO: Should this be FAILED instead? */ |
2456 |
err = ARCHIVE_WARN; |
2457 |
} else { |
2458 |
p = __archive_read_ahead(a, value_length, &bytes_read); |
2459 |
if (p == NULL) { |
2460 |
archive_set_error(&a->archive, EINVAL, |
2461 |
"Truncated archive" |
2462 |
" detected while reading selinux data"); |
2463 |
return (ARCHIVE_FATAL); |
2464 |
} |
2465 |
if (pax_attribute_RHT_security_selinux(entry, p, value_length)) { |
2466 |
/* TODO: Unable to parse xattr */ |
2467 |
err = ARCHIVE_WARN; |
2468 |
} |
1995 |
} |
2469 |
} |
|
|
2470 |
__archive_read_consume(a, value_length); |
2471 |
return (err); |
2472 |
} |
1996 |
break; |
2473 |
break; |
1997 |
case 'S': |
2474 |
case 'S': |
1998 |
/* We support some keys used by the "star" archiver */ |
2475 |
/* SCHILY.* extensions used by "star" archiver */ |
1999 |
if (strcmp(key, "SCHILY.acl.access") == 0) { |
2476 |
if (key_length > 7 && memcmp(key, "SCHILY.", 7) == 0) { |
2000 |
r = pax_attribute_acl(a, tar, entry, value, |
2477 |
key_length -= 7; |
2001 |
ARCHIVE_ENTRY_ACL_TYPE_ACCESS); |
2478 |
key += 7; |
2002 |
if (r == ARCHIVE_FATAL) |
2479 |
|
2003 |
return (r); |
2480 |
if (key_length == 10 && memcmp(key, "acl.access", 10) == 0) { |
2004 |
} else if (strcmp(key, "SCHILY.acl.default") == 0) { |
2481 |
err = pax_attribute_SCHILY_acl(a, tar, entry, value_length, |
2005 |
r = pax_attribute_acl(a, tar, entry, value, |
2482 |
ARCHIVE_ENTRY_ACL_TYPE_ACCESS); |
2006 |
ARCHIVE_ENTRY_ACL_TYPE_DEFAULT); |
2483 |
// TODO: Mark mode as set |
2007 |
if (r == ARCHIVE_FATAL) |
2484 |
return (err); |
2008 |
return (r); |
2485 |
} |
2009 |
} else if (strcmp(key, "SCHILY.acl.ace") == 0) { |
2486 |
else if (key_length == 11 && memcmp(key, "acl.default", 11) == 0) { |
2010 |
r = pax_attribute_acl(a, tar, entry, value, |
2487 |
err = pax_attribute_SCHILY_acl(a, tar, entry, value_length, |
2011 |
ARCHIVE_ENTRY_ACL_TYPE_NFS4); |
2488 |
ARCHIVE_ENTRY_ACL_TYPE_DEFAULT); |
2012 |
if (r == ARCHIVE_FATAL) |
2489 |
return (err); |
2013 |
return (r); |
2490 |
} |
2014 |
} else if (strcmp(key, "SCHILY.devmajor") == 0) { |
2491 |
else if (key_length == 7 && memcmp(key, "acl.ace", 7) == 0) { |
2015 |
archive_entry_set_rdevmajor(entry, |
2492 |
err = pax_attribute_SCHILY_acl(a, tar, entry, value_length, |
2016 |
(dev_t)tar_atol10(value, strlen(value))); |
2493 |
ARCHIVE_ENTRY_ACL_TYPE_NFS4); |
2017 |
} else if (strcmp(key, "SCHILY.devminor") == 0) { |
2494 |
// TODO: Mark mode as set |
2018 |
archive_entry_set_rdevminor(entry, |
2495 |
return (err); |
2019 |
(dev_t)tar_atol10(value, strlen(value))); |
2496 |
} |
2020 |
} else if (strcmp(key, "SCHILY.fflags") == 0) { |
2497 |
else if (key_length == 8 && memcmp(key, "devmajor", 8) == 0) { |
2021 |
archive_entry_copy_fflags_text(entry, value); |
2498 |
if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2022 |
} else if (strcmp(key, "SCHILY.dev") == 0) { |
2499 |
archive_entry_set_rdevmajor(entry, (dev_t)t); |
2023 |
archive_entry_set_dev(entry, |
2500 |
} |
2024 |
(dev_t)tar_atol10(value, strlen(value))); |
2501 |
return (err); |
2025 |
} else if (strcmp(key, "SCHILY.ino") == 0) { |
2502 |
} |
2026 |
archive_entry_set_ino(entry, |
2503 |
else if (key_length == 8 && memcmp(key, "devminor", 8) == 0) { |
2027 |
tar_atol10(value, strlen(value))); |
2504 |
if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2028 |
} else if (strcmp(key, "SCHILY.nlink") == 0) { |
2505 |
archive_entry_set_rdevminor(entry, (dev_t)t); |
2029 |
archive_entry_set_nlink(entry, (unsigned) |
2506 |
} |
2030 |
tar_atol10(value, strlen(value))); |
2507 |
return (err); |
2031 |
} else if (strcmp(key, "SCHILY.realsize") == 0) { |
2508 |
} |
2032 |
tar->realsize = tar_atol10(value, strlen(value)); |
2509 |
else if (key_length == 6 && memcmp(key, "fflags", 6) == 0) { |
2033 |
tar->realsize_override = 1; |
2510 |
if (value_length < fflags_limit) { |
2034 |
archive_entry_set_size(entry, tar->realsize); |
2511 |
p = __archive_read_ahead(a, value_length, &bytes_read); |
2035 |
} else if (strncmp(key, "SCHILY.xattr.", 13) == 0) { |
2512 |
if (p == NULL) { |
2036 |
pax_attribute_schily_xattr(entry, key, value, |
2513 |
/* Truncated archive */ |
2037 |
value_length); |
2514 |
archive_set_error(&a->archive, EINVAL, |
2038 |
} else if (strcmp(key, "SUN.holesdata") == 0) { |
2515 |
"Truncated archive" |
2039 |
/* A Solaris extension for sparse. */ |
2516 |
" detected while reading SCHILY.fflags"); |
2040 |
r = solaris_sparse_parse(a, tar, entry, value); |
2517 |
return (ARCHIVE_FATAL); |
2041 |
if (r < err) { |
2518 |
} |
2042 |
if (r == ARCHIVE_FATAL) |
2519 |
archive_entry_copy_fflags_text_len(entry, p, value_length); |
2043 |
return (r); |
2520 |
err = ARCHIVE_OK; |
2044 |
err = r; |
2521 |
} else { |
2045 |
archive_set_error(&a->archive, |
2522 |
/* Overlong fflags field */ |
2046 |
ARCHIVE_ERRNO_MISC, |
2523 |
err = ARCHIVE_WARN; |
2047 |
"Parse error: SUN.holesdata"); |
2524 |
} |
|
|
2525 |
__archive_read_consume(a, value_length); |
2526 |
return (err); |
2527 |
} |
2528 |
else if (key_length == 3 && memcmp(key, "dev", 3) == 0) { |
2529 |
if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2530 |
archive_entry_set_dev(entry, (dev_t)t); |
2531 |
} |
2532 |
return (err); |
2533 |
} |
2534 |
else if (key_length == 3 && memcmp(key, "ino", 3) == 0) { |
2535 |
if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2536 |
archive_entry_set_ino(entry, t); |
2537 |
} |
2538 |
return (err); |
2539 |
} |
2540 |
else if (key_length == 5 && memcmp(key, "nlink", 5) == 0) { |
2541 |
if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2542 |
archive_entry_set_nlink(entry, (unsigned int)t); |
2543 |
} |
2544 |
return (err); |
2545 |
} |
2546 |
else if (key_length == 8 && memcmp(key, "realsize", 8) == 0) { |
2547 |
if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2548 |
tar->realsize = t; |
2549 |
tar->realsize_override = 1; |
2550 |
archive_entry_set_size(entry, tar->realsize); |
2551 |
} |
2552 |
return (err); |
2553 |
} |
2554 |
else if (key_length > 6 && memcmp(key, "xattr.", 6) == 0) { |
2555 |
key_length -= 6; |
2556 |
key += 6; |
2557 |
if (value_length < xattr_limit) { |
2558 |
p = __archive_read_ahead(a, value_length, &bytes_read); |
2559 |
if (p == NULL) { |
2560 |
archive_set_error(&a->archive, EINVAL, |
2561 |
"Truncated archive" |
2562 |
" detected while reading SCHILY.xattr"); |
2563 |
return (ARCHIVE_FATAL); |
2564 |
} |
2565 |
if (pax_attribute_SCHILY_xattr(entry, key, key_length, p, value_length)) { |
2566 |
/* TODO: Unable to parse xattr */ |
2567 |
err = ARCHIVE_WARN; |
2568 |
} |
2569 |
} else { |
2570 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
2571 |
"Unreasonably large xattr: %d > %d", |
2572 |
(int)value_length, (int)xattr_limit); |
2573 |
err = ARCHIVE_WARN; |
2574 |
} |
2575 |
__archive_read_consume(a, value_length); |
2576 |
return (err); |
2577 |
} |
2578 |
} |
2579 |
/* SUN.* extensions from Solaris tar */ |
2580 |
if (key_length > 4 && memcmp(key, "SUN.", 4) == 0) { |
2581 |
key_length -= 4; |
2582 |
key += 4; |
2583 |
|
2584 |
if (key_length == 9 && memcmp(key, "holesdata", 9) == 0) { |
2585 |
/* SUN.holesdata */ |
2586 |
if (value_length < sparse_map_limit) { |
2587 |
p = __archive_read_ahead(a, value_length, &bytes_read); |
2588 |
if (p == NULL) { |
2589 |
archive_set_error(&a->archive, EINVAL, |
2590 |
"Truncated archive" |
2591 |
" detected while reading SUN.holesdata"); |
2592 |
return (ARCHIVE_FATAL); |
2593 |
} |
2594 |
err = pax_attribute_SUN_holesdata(a, tar, entry, p, value_length); |
2595 |
if (err < ARCHIVE_OK) { |
2596 |
archive_set_error(&a->archive, |
2597 |
ARCHIVE_ERRNO_MISC, |
2598 |
"Parse error: SUN.holesdata"); |
2599 |
} |
2600 |
} else { |
2601 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
2602 |
"Unreasonably large sparse map: %d > %d", |
2603 |
(int)value_length, (int)sparse_map_limit); |
2604 |
err = ARCHIVE_FAILED; |
2605 |
} |
2606 |
__archive_read_consume(a, value_length); |
2607 |
return (err); |
2048 |
} |
2608 |
} |
2049 |
} |
2609 |
} |
2050 |
break; |
2610 |
break; |
2051 |
case 'a': |
2611 |
case 'a': |
2052 |
if (strcmp(key, "atime") == 0) { |
2612 |
if (key_length == 5 && memcmp(key, "atime", 5) == 0) { |
2053 |
pax_time(value, &s, &n); |
2613 |
if ((err = pax_attribute_read_time(a, value_length, &t, &n, unconsumed)) == ARCHIVE_OK) { |
2054 |
archive_entry_set_atime(entry, s, n); |
2614 |
archive_entry_set_atime(entry, t, n); |
|
|
2615 |
} |
2616 |
return (err); |
2055 |
} |
2617 |
} |
2056 |
break; |
2618 |
break; |
2057 |
case 'c': |
2619 |
case 'c': |
2058 |
if (strcmp(key, "ctime") == 0) { |
2620 |
if (key_length == 5 && memcmp(key, "ctime", 5) == 0) { |
2059 |
pax_time(value, &s, &n); |
2621 |
if ((err = pax_attribute_read_time(a, value_length, &t, &n, unconsumed)) == ARCHIVE_OK) { |
2060 |
archive_entry_set_ctime(entry, s, n); |
2622 |
archive_entry_set_ctime(entry, t, n); |
2061 |
} else if (strcmp(key, "charset") == 0) { |
2623 |
} |
|
|
2624 |
return (err); |
2625 |
} else if (key_length == 7 && memcmp(key, "charset", 7) == 0) { |
2062 |
/* TODO: Publish charset information in entry. */ |
2626 |
/* TODO: Publish charset information in entry. */ |
2063 |
} else if (strcmp(key, "comment") == 0) { |
2627 |
} else if (key_length == 7 && memcmp(key, "comment", 7) == 0) { |
2064 |
/* TODO: Publish comment in entry. */ |
2628 |
/* TODO: Publish comment in entry. */ |
2065 |
} |
2629 |
} |
2066 |
break; |
2630 |
break; |
2067 |
case 'g': |
2631 |
case 'g': |
2068 |
if (strcmp(key, "gid") == 0) { |
2632 |
if (key_length == 3 && memcmp(key, "gid", 3) == 0) { |
2069 |
archive_entry_set_gid(entry, |
2633 |
if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2070 |
tar_atol10(value, strlen(value))); |
2634 |
archive_entry_set_gid(entry, t); |
2071 |
} else if (strcmp(key, "gname") == 0) { |
2635 |
} |
2072 |
archive_strcpy(&(tar->entry_gname), value); |
2636 |
return (err); |
|
|
2637 |
} else if (key_length == 5 && memcmp(key, "gname", 5) == 0) { |
2638 |
if (value_length > guname_limit) { |
2639 |
*unconsumed += value_length; |
2640 |
err = ARCHIVE_WARN; |
2641 |
} else { |
2642 |
err = read_bytes_to_string(a, &(tar->entry_gname), value_length, unconsumed); |
2643 |
} |
2644 |
return (err); |
2073 |
} |
2645 |
} |
2074 |
break; |
2646 |
break; |
2075 |
case 'h': |
2647 |
case 'h': |
2076 |
if (strcmp(key, "hdrcharset") == 0) { |
2648 |
if (key_length == 10 && memcmp(key, "hdrcharset", 10) == 0) { |
2077 |
if (strcmp(value, "BINARY") == 0) |
2649 |
if (value_length < 64) { |
2078 |
/* Binary mode. */ |
2650 |
p = __archive_read_ahead(a, value_length, &bytes_read); |
2079 |
tar->pax_hdrcharset_binary = 1; |
2651 |
if (p == NULL) { |
2080 |
else if (strcmp(value, "ISO-IR 10646 2000 UTF-8") == 0) |
2652 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, |
2081 |
tar->pax_hdrcharset_binary = 0; |
2653 |
"Truncated tar archive " |
|
|
2654 |
"detected while reading hdrcharset attribute"); |
2655 |
return (ARCHIVE_FATAL); |
2656 |
} |
2657 |
if (value_length == 6 |
2658 |
&& memcmp(p, "BINARY", 6) == 0) { |
2659 |
/* Binary mode. */ |
2660 |
tar->pax_hdrcharset_utf8 = 0; |
2661 |
err = ARCHIVE_OK; |
2662 |
} else if (value_length == 23 |
2663 |
&& memcmp(p, "ISO-IR 10646 2000 UTF-8", 23) == 0) { |
2664 |
tar->pax_hdrcharset_utf8 = 1; |
2665 |
err = ARCHIVE_OK; |
2666 |
} else { |
2667 |
/* TODO: Unrecognized character set */ |
2668 |
err = ARCHIVE_WARN; |
2669 |
} |
2670 |
} else { |
2671 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, |
2672 |
"hdrcharset attribute is unreasonably large (%d bytes)", |
2673 |
(int)value_length); |
2674 |
err = ARCHIVE_WARN; |
2675 |
} |
2676 |
__archive_read_consume(a, value_length); |
2677 |
return (err); |
2082 |
} |
2678 |
} |
2083 |
break; |
2679 |
break; |
2084 |
case 'l': |
2680 |
case 'l': |
2085 |
/* pax interchange doesn't distinguish hardlink vs. symlink. */ |
2681 |
/* pax interchange doesn't distinguish hardlink vs. symlink. */ |
2086 |
if (strcmp(key, "linkpath") == 0) { |
2682 |
if (key_length == 8 && memcmp(key, "linkpath", 8) == 0) { |
2087 |
archive_strcpy(&(tar->entry_linkpath), value); |
2683 |
if (value_length > pathname_limit) { |
|
|
2684 |
*unconsumed += value_length; |
2685 |
err = ARCHIVE_WARN; |
2686 |
} else { |
2687 |
err = read_bytes_to_string(a, &tar->entry_linkpath, value_length, unconsumed); |
2688 |
} |
2689 |
return (err); |
2088 |
} |
2690 |
} |
2089 |
break; |
2691 |
break; |
2090 |
case 'm': |
2692 |
case 'm': |
2091 |
if (strcmp(key, "mtime") == 0) { |
2693 |
if (key_length == 5 && memcmp(key, "mtime", 5) == 0) { |
2092 |
pax_time(value, &s, &n); |
2694 |
if ((err = pax_attribute_read_time(a, value_length, &t, &n, unconsumed)) == ARCHIVE_OK) { |
2093 |
archive_entry_set_mtime(entry, s, n); |
2695 |
archive_entry_set_mtime(entry, t, n); |
|
|
2696 |
} |
2697 |
return (err); |
2094 |
} |
2698 |
} |
2095 |
break; |
2699 |
break; |
2096 |
case 'p': |
2700 |
case 'p': |
2097 |
if (strcmp(key, "path") == 0) { |
2701 |
if (key_length == 4 && memcmp(key, "path", 4) == 0) { |
2098 |
archive_strcpy(&(tar->entry_pathname), value); |
2702 |
if (value_length > pathname_limit) { |
|
|
2703 |
*unconsumed += value_length; |
2704 |
err = ARCHIVE_WARN; |
2705 |
} else { |
2706 |
err = read_bytes_to_string(a, &(tar->entry_pathname), value_length, unconsumed); |
2707 |
} |
2708 |
return (err); |
2099 |
} |
2709 |
} |
2100 |
break; |
2710 |
break; |
2101 |
case 'r': |
2711 |
case 'r': |
Lines 2104-2151
pax_attribute(struct archive_read *a, struct tar *tar,
Link Here
|
2104 |
case 's': |
2714 |
case 's': |
2105 |
/* POSIX has reserved 'security.*' */ |
2715 |
/* POSIX has reserved 'security.*' */ |
2106 |
/* Someday: if (strcmp(key, "security.acl") == 0) { ... } */ |
2716 |
/* Someday: if (strcmp(key, "security.acl") == 0) { ... } */ |
2107 |
if (strcmp(key, "size") == 0) { |
2717 |
if (key_length == 4 && memcmp(key, "size", 4) == 0) { |
2108 |
/* "size" is the size of the data in the entry. */ |
2718 |
/* "size" is the size of the data in the entry. */ |
2109 |
tar->entry_bytes_remaining |
2719 |
if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2110 |
= tar_atol10(value, strlen(value)); |
2720 |
tar->entry_bytes_remaining = t; |
2111 |
if (tar->entry_bytes_remaining < 0) { |
2721 |
/* |
2112 |
tar->entry_bytes_remaining = 0; |
2722 |
* The "size" pax header keyword always overrides the |
2113 |
archive_set_error(&a->archive, |
2723 |
* "size" field in the tar header. |
2114 |
ARCHIVE_ERRNO_MISC, |
2724 |
* GNU.sparse.realsize, GNU.sparse.size and |
2115 |
"Tar size attribute is negative"); |
2725 |
* SCHILY.realsize override this value. |
2116 |
return (ARCHIVE_FATAL); |
2726 |
*/ |
|
|
2727 |
if (!tar->realsize_override) { |
2728 |
archive_entry_set_size(entry, |
2729 |
tar->entry_bytes_remaining); |
2730 |
tar->realsize |
2731 |
= tar->entry_bytes_remaining; |
2732 |
} |
2117 |
} |
2733 |
} |
2118 |
if (tar->entry_bytes_remaining == INT64_MAX) { |
2734 |
else if (t == INT64_MAX) { |
2119 |
/* Note: tar_atol returns INT64_MAX on overflow */ |
2735 |
/* Note: pax_attr_read_number returns INT64_MAX on overflow or < 0 */ |
2120 |
tar->entry_bytes_remaining = 0; |
2736 |
tar->entry_bytes_remaining = 0; |
2121 |
archive_set_error(&a->archive, |
2737 |
archive_set_error(&a->archive, |
2122 |
ARCHIVE_ERRNO_MISC, |
2738 |
ARCHIVE_ERRNO_MISC, |
2123 |
"Tar size attribute overflow"); |
2739 |
"Tar size attribute overflow"); |
2124 |
return (ARCHIVE_FATAL); |
2740 |
return (ARCHIVE_FATAL); |
2125 |
} |
2741 |
} |
2126 |
/* |
2742 |
return (err); |
2127 |
* The "size" pax header keyword always overrides the |
|
|
2128 |
* "size" field in the tar header. |
2129 |
* GNU.sparse.realsize, GNU.sparse.size and |
2130 |
* SCHILY.realsize override this value. |
2131 |
*/ |
2132 |
if (!tar->realsize_override) { |
2133 |
archive_entry_set_size(entry, |
2134 |
tar->entry_bytes_remaining); |
2135 |
tar->realsize |
2136 |
= tar->entry_bytes_remaining; |
2137 |
} |
2138 |
} |
2743 |
} |
2139 |
break; |
2744 |
break; |
2140 |
case 'u': |
2745 |
case 'u': |
2141 |
if (strcmp(key, "uid") == 0) { |
2746 |
if (key_length == 3 && memcmp(key, "uid", 3) == 0) { |
2142 |
archive_entry_set_uid(entry, |
2747 |
if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2143 |
tar_atol10(value, strlen(value))); |
2748 |
archive_entry_set_uid(entry, t); |
2144 |
} else if (strcmp(key, "uname") == 0) { |
2749 |
} |
2145 |
archive_strcpy(&(tar->entry_uname), value); |
2750 |
return (err); |
|
|
2751 |
} else if (key_length == 5 && memcmp(key, "uname", 5) == 0) { |
2752 |
if (value_length > guname_limit) { |
2753 |
*unconsumed += value_length; |
2754 |
err = ARCHIVE_WARN; |
2755 |
} else { |
2756 |
err = read_bytes_to_string(a, &(tar->entry_uname), value_length, unconsumed); |
2757 |
} |
2758 |
return (err); |
2146 |
} |
2759 |
} |
2147 |
break; |
2760 |
break; |
2148 |
} |
2761 |
} |
|
|
2762 |
|
2763 |
/* Unrecognized key, just skip the entire value. */ |
2764 |
__archive_read_consume(a, value_length); |
2149 |
return (err); |
2765 |
return (err); |
2150 |
} |
2766 |
} |
2151 |
|
2767 |
|
Lines 2155-2161
pax_attribute(struct archive_read *a, struct tar *tar,
Link Here
|
2155 |
* parse a decimal time value, which may include a fractional portion |
2771 |
* parse a decimal time value, which may include a fractional portion |
2156 |
*/ |
2772 |
*/ |
2157 |
static void |
2773 |
static void |
2158 |
pax_time(const char *p, int64_t *ps, long *pn) |
2774 |
pax_time(const char *p, size_t length, int64_t *ps, long *pn) |
2159 |
{ |
2775 |
{ |
2160 |
char digit; |
2776 |
char digit; |
2161 |
int64_t s; |
2777 |
int64_t s; |
Lines 2166-2178
pax_time(const char *p, int64_t *ps, long *pn)
Link Here
|
2166 |
limit = INT64_MAX / 10; |
2782 |
limit = INT64_MAX / 10; |
2167 |
last_digit_limit = INT64_MAX % 10; |
2783 |
last_digit_limit = INT64_MAX % 10; |
2168 |
|
2784 |
|
|
|
2785 |
if (length <= 0) { |
2786 |
*ps = 0; |
2787 |
return; |
2788 |
} |
2169 |
s = 0; |
2789 |
s = 0; |
2170 |
sign = 1; |
2790 |
sign = 1; |
2171 |
if (*p == '-') { |
2791 |
if (*p == '-') { |
2172 |
sign = -1; |
2792 |
sign = -1; |
2173 |
p++; |
2793 |
p++; |
|
|
2794 |
length--; |
2174 |
} |
2795 |
} |
2175 |
while (*p >= '0' && *p <= '9') { |
2796 |
while (length > 0 && *p >= '0' && *p <= '9') { |
2176 |
digit = *p - '0'; |
2797 |
digit = *p - '0'; |
2177 |
if (s > limit || |
2798 |
if (s > limit || |
2178 |
(s == limit && digit > last_digit_limit)) { |
2799 |
(s == limit && digit > last_digit_limit)) { |
Lines 2181-2186
pax_time(const char *p, int64_t *ps, long *pn)
Link Here
|
2181 |
} |
2802 |
} |
2182 |
s = (s * 10) + digit; |
2803 |
s = (s * 10) + digit; |
2183 |
++p; |
2804 |
++p; |
|
|
2805 |
--length; |
2184 |
} |
2806 |
} |
2185 |
|
2807 |
|
2186 |
*ps = s * sign; |
2808 |
*ps = s * sign; |
Lines 2188-2200
pax_time(const char *p, int64_t *ps, long *pn)
Link Here
|
2188 |
/* Calculate nanoseconds. */ |
2810 |
/* Calculate nanoseconds. */ |
2189 |
*pn = 0; |
2811 |
*pn = 0; |
2190 |
|
2812 |
|
2191 |
if (*p != '.') |
2813 |
if (length <= 0 || *p != '.') |
2192 |
return; |
2814 |
return; |
2193 |
|
2815 |
|
2194 |
l = 100000000UL; |
2816 |
l = 100000000UL; |
2195 |
do { |
2817 |
do { |
2196 |
++p; |
2818 |
++p; |
2197 |
if (*p >= '0' && *p <= '9') |
2819 |
--length; |
|
|
2820 |
if (length > 0 && *p >= '0' && *p <= '9') |
2198 |
*pn += (*p - '0') * l; |
2821 |
*pn += (*p - '0') * l; |
2199 |
else |
2822 |
else |
2200 |
break; |
2823 |
break; |
Lines 2225-2273
header_gnutar(struct archive_read *a, struct tar *tar,
Link Here
|
2225 |
|
2848 |
|
2226 |
/* Copy filename over (to ensure null termination). */ |
2849 |
/* Copy filename over (to ensure null termination). */ |
2227 |
header = (const struct archive_entry_header_gnutar *)h; |
2850 |
header = (const struct archive_entry_header_gnutar *)h; |
2228 |
if (archive_entry_copy_pathname_l(entry, |
2851 |
const char *existing_pathname = archive_entry_pathname(entry); |
2229 |
header->name, sizeof(header->name), tar->sconv) != 0) { |
2852 |
if (existing_pathname == NULL || existing_pathname[0] == '\0') { |
2230 |
err = set_conversion_failed_error(a, tar->sconv, "Pathname"); |
2853 |
if (archive_entry_copy_pathname_l(entry, |
2231 |
if (err == ARCHIVE_FATAL) |
2854 |
header->name, sizeof(header->name), tar->sconv) != 0) { |
2232 |
return (err); |
2855 |
err = set_conversion_failed_error(a, tar->sconv, "Pathname"); |
|
|
2856 |
if (err == ARCHIVE_FATAL) |
2857 |
return (err); |
2858 |
} |
2233 |
} |
2859 |
} |
2234 |
|
2860 |
|
2235 |
/* Fields common to ustar and GNU */ |
2861 |
/* Fields common to ustar and GNU */ |
2236 |
/* XXX Can the following be factored out since it's common |
2862 |
/* XXX Can the following be factored out since it's common |
2237 |
* to ustar and gnu tar? Is it okay to move it down into |
2863 |
* to ustar and gnu tar? Is it okay to move it down into |
2238 |
* header_common, perhaps? */ |
2864 |
* header_common, perhaps? */ |
2239 |
if (archive_entry_copy_uname_l(entry, |
2865 |
const char *existing_uname = archive_entry_uname(entry); |
2240 |
header->uname, sizeof(header->uname), tar->sconv) != 0) { |
2866 |
if (existing_uname == NULL || existing_uname[0] == '\0') { |
2241 |
err = set_conversion_failed_error(a, tar->sconv, "Uname"); |
2867 |
if (archive_entry_copy_uname_l(entry, |
2242 |
if (err == ARCHIVE_FATAL) |
2868 |
header->uname, sizeof(header->uname), tar->sconv) != 0) { |
2243 |
return (err); |
2869 |
err = set_conversion_failed_error(a, tar->sconv, "Uname"); |
|
|
2870 |
if (err == ARCHIVE_FATAL) |
2871 |
return (err); |
2872 |
} |
2244 |
} |
2873 |
} |
2245 |
|
2874 |
|
2246 |
if (archive_entry_copy_gname_l(entry, |
2875 |
const char *existing_gname = archive_entry_gname(entry); |
2247 |
header->gname, sizeof(header->gname), tar->sconv) != 0) { |
2876 |
if (existing_gname == NULL || existing_gname[0] == '\0') { |
2248 |
err = set_conversion_failed_error(a, tar->sconv, "Gname"); |
2877 |
if (archive_entry_copy_gname_l(entry, |
2249 |
if (err == ARCHIVE_FATAL) |
2878 |
header->gname, sizeof(header->gname), tar->sconv) != 0) { |
2250 |
return (err); |
2879 |
err = set_conversion_failed_error(a, tar->sconv, "Gname"); |
|
|
2880 |
if (err == ARCHIVE_FATAL) |
2881 |
return (err); |
2882 |
} |
2251 |
} |
2883 |
} |
2252 |
|
2884 |
|
2253 |
/* Parse out device numbers only for char and block specials */ |
2885 |
/* Parse out device numbers only for char and block specials */ |
2254 |
if (header->typeflag[0] == '3' || header->typeflag[0] == '4') { |
2886 |
if (header->typeflag[0] == '3' || header->typeflag[0] == '4') { |
2255 |
archive_entry_set_rdevmajor(entry, (dev_t) |
2887 |
if (!archive_entry_rdev_is_set(entry)) { |
2256 |
tar_atol(header->rdevmajor, sizeof(header->rdevmajor))); |
2888 |
archive_entry_set_rdevmajor(entry, (dev_t) |
2257 |
archive_entry_set_rdevminor(entry, (dev_t) |
2889 |
tar_atol(header->rdevmajor, sizeof(header->rdevmajor))); |
2258 |
tar_atol(header->rdevminor, sizeof(header->rdevminor))); |
2890 |
archive_entry_set_rdevminor(entry, (dev_t) |
2259 |
} else |
2891 |
tar_atol(header->rdevminor, sizeof(header->rdevminor))); |
|
|
2892 |
} |
2893 |
} else { |
2260 |
archive_entry_set_rdev(entry, 0); |
2894 |
archive_entry_set_rdev(entry, 0); |
|
|
2895 |
} |
2261 |
|
2896 |
|
2262 |
tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining); |
2897 |
tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining); |
2263 |
|
2898 |
|
2264 |
/* Grab GNU-specific fields. */ |
2899 |
/* Grab GNU-specific fields. */ |
2265 |
t = tar_atol(header->atime, sizeof(header->atime)); |
2900 |
if (!archive_entry_atime_is_set(entry)) { |
2266 |
if (t > 0) |
2901 |
t = tar_atol(header->atime, sizeof(header->atime)); |
2267 |
archive_entry_set_atime(entry, t, 0); |
2902 |
if (t > 0) |
2268 |
t = tar_atol(header->ctime, sizeof(header->ctime)); |
2903 |
archive_entry_set_atime(entry, t, 0); |
2269 |
if (t > 0) |
2904 |
} |
2270 |
archive_entry_set_ctime(entry, t, 0); |
2905 |
if (!archive_entry_ctime_is_set(entry)) { |
|
|
2906 |
t = tar_atol(header->ctime, sizeof(header->ctime)); |
2907 |
if (t > 0) |
2908 |
archive_entry_set_ctime(entry, t, 0); |
2909 |
} |
2271 |
|
2910 |
|
2272 |
if (header->realsize[0] != 0) { |
2911 |
if (header->realsize[0] != 0) { |
2273 |
tar->realsize |
2912 |
tar->realsize |
Lines 2295-2301
gnu_add_sparse_entry(struct archive_read *a, struct tar *tar,
Link Here
|
2295 |
{ |
2934 |
{ |
2296 |
struct sparse_block *p; |
2935 |
struct sparse_block *p; |
2297 |
|
2936 |
|
2298 |
p = (struct sparse_block *)calloc(1, sizeof(*p)); |
2937 |
p = calloc(1, sizeof(*p)); |
2299 |
if (p == NULL) { |
2938 |
if (p == NULL) { |
2300 |
archive_set_error(&a->archive, ENOMEM, "Out of memory"); |
2939 |
archive_set_error(&a->archive, ENOMEM, "Out of memory"); |
2301 |
return (ARCHIVE_FATAL); |
2940 |
return (ARCHIVE_FATAL); |
Lines 2360-2368
gnu_sparse_old_read(struct archive_read *a, struct tar *tar,
Link Here
|
2360 |
do { |
2999 |
do { |
2361 |
tar_flush_unconsumed(a, unconsumed); |
3000 |
tar_flush_unconsumed(a, unconsumed); |
2362 |
data = __archive_read_ahead(a, 512, &bytes_read); |
3001 |
data = __archive_read_ahead(a, 512, &bytes_read); |
2363 |
if (bytes_read < 0) |
3002 |
if (data == NULL) { |
2364 |
return (ARCHIVE_FATAL); |
|
|
2365 |
if (bytes_read < 512) { |
2366 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, |
3003 |
archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, |
2367 |
"Truncated tar archive " |
3004 |
"Truncated tar archive " |
2368 |
"detected while reading sparse file data"); |
3005 |
"detected while reading sparse file data"); |
Lines 2419-2437
gnu_sparse_old_parse(struct archive_read *a, struct tar *tar,
Link Here
|
2419 |
* importantly, the sparse data was lost when extracted by archivers |
3056 |
* importantly, the sparse data was lost when extracted by archivers |
2420 |
* that didn't recognize this extension. |
3057 |
* that didn't recognize this extension. |
2421 |
*/ |
3058 |
*/ |
2422 |
|
|
|
2423 |
static int |
3059 |
static int |
2424 |
gnu_sparse_01_parse(struct archive_read *a, struct tar *tar, const char *p) |
3060 |
gnu_sparse_01_parse(struct archive_read *a, struct tar *tar, const char *p, size_t length) |
2425 |
{ |
3061 |
{ |
2426 |
const char *e; |
3062 |
const char *e; |
2427 |
int64_t offset = -1, size = -1; |
3063 |
int64_t offset = -1, size = -1; |
2428 |
|
3064 |
|
2429 |
for (;;) { |
3065 |
for (;;) { |
2430 |
e = p; |
3066 |
e = p; |
2431 |
while (*e != '\0' && *e != ',') { |
3067 |
while (length > 0 && *e != ',') { |
2432 |
if (*e < '0' || *e > '9') |
3068 |
if (*e < '0' || *e > '9') |
2433 |
return (ARCHIVE_WARN); |
3069 |
return (ARCHIVE_WARN); |
2434 |
e++; |
3070 |
e++; |
|
|
3071 |
length--; |
2435 |
} |
3072 |
} |
2436 |
if (offset < 0) { |
3073 |
if (offset < 0) { |
2437 |
offset = tar_atol10(p, e - p); |
3074 |
offset = tar_atol10(p, e - p); |
Lines 2446-2454
gnu_sparse_01_parse(struct archive_read *a, struct tar *tar, const char *p)
Link Here
|
2446 |
return (ARCHIVE_FATAL); |
3083 |
return (ARCHIVE_FATAL); |
2447 |
offset = -1; |
3084 |
offset = -1; |
2448 |
} |
3085 |
} |
2449 |
if (*e == '\0') |
3086 |
if (length == 0) |
2450 |
return (ARCHIVE_OK); |
3087 |
return (ARCHIVE_OK); |
2451 |
p = e + 1; |
3088 |
p = e + 1; |
|
|
3089 |
length--; |
2452 |
} |
3090 |
} |
2453 |
} |
3091 |
} |
2454 |
|
3092 |
|
Lines 2570-2577
gnu_sparse_10_read(struct archive_read *a, struct tar *tar, size_t *unconsumed)
Link Here
|
2570 |
* consist of both data and hole. |
3208 |
* consist of both data and hole. |
2571 |
*/ |
3209 |
*/ |
2572 |
static int |
3210 |
static int |
2573 |
solaris_sparse_parse(struct archive_read *a, struct tar *tar, |
3211 |
pax_attribute_SUN_holesdata(struct archive_read *a, struct tar *tar, |
2574 |
struct archive_entry *entry, const char *p) |
3212 |
struct archive_entry *entry, const char *p, size_t length) |
2575 |
{ |
3213 |
{ |
2576 |
const char *e; |
3214 |
const char *e; |
2577 |
int64_t start, end; |
3215 |
int64_t start, end; |
Lines 2580-2595
solaris_sparse_parse(struct archive_read *a, struct tar *tar,
Link Here
|
2580 |
(void)entry; /* UNUSED */ |
3218 |
(void)entry; /* UNUSED */ |
2581 |
|
3219 |
|
2582 |
end = 0; |
3220 |
end = 0; |
2583 |
if (*p == ' ') |
3221 |
if (length <= 0) |
|
|
3222 |
return (ARCHIVE_WARN); |
3223 |
if (*p == ' ') { |
2584 |
p++; |
3224 |
p++; |
2585 |
else |
3225 |
length--; |
|
|
3226 |
} else { |
2586 |
return (ARCHIVE_WARN); |
3227 |
return (ARCHIVE_WARN); |
|
|
3228 |
} |
2587 |
for (;;) { |
3229 |
for (;;) { |
2588 |
e = p; |
3230 |
e = p; |
2589 |
while (*e != '\0' && *e != ' ') { |
3231 |
while (length > 0 && *e != ' ') { |
2590 |
if (*e < '0' || *e > '9') |
3232 |
if (*e < '0' || *e > '9') |
2591 |
return (ARCHIVE_WARN); |
3233 |
return (ARCHIVE_WARN); |
2592 |
e++; |
3234 |
e++; |
|
|
3235 |
length--; |
2593 |
} |
3236 |
} |
2594 |
start = end; |
3237 |
start = end; |
2595 |
end = tar_atol10(p, e - p); |
3238 |
end = tar_atol10(p, e - p); |
Lines 2601-2609
solaris_sparse_parse(struct archive_read *a, struct tar *tar,
Link Here
|
2601 |
return (ARCHIVE_FATAL); |
3244 |
return (ARCHIVE_FATAL); |
2602 |
tar->sparse_last->hole = hole; |
3245 |
tar->sparse_last->hole = hole; |
2603 |
} |
3246 |
} |
2604 |
if (*e == '\0') |
3247 |
if (length == 0 || *e == '\n') { |
2605 |
return (ARCHIVE_OK); |
3248 |
if (length == 0 && *e == '\n') { |
|
|
3249 |
return (ARCHIVE_OK); |
3250 |
} else { |
3251 |
return (ARCHIVE_WARN); |
3252 |
} |
3253 |
} |
2606 |
p = e + 1; |
3254 |
p = e + 1; |
|
|
3255 |
length--; |
2607 |
hole = hole == 0; |
3256 |
hole = hole == 0; |
2608 |
} |
3257 |
} |
2609 |
} |
3258 |
} |
Lines 2770-2776
readline(struct archive_read *a, struct tar *tar, const char **start,
Link Here
|
2770 |
tar_flush_unconsumed(a, unconsumed); |
3419 |
tar_flush_unconsumed(a, unconsumed); |
2771 |
|
3420 |
|
2772 |
t = __archive_read_ahead(a, 1, &bytes_read); |
3421 |
t = __archive_read_ahead(a, 1, &bytes_read); |
2773 |
if (bytes_read <= 0) |
3422 |
if (bytes_read <= 0 || t == NULL) |
2774 |
return (ARCHIVE_FATAL); |
3423 |
return (ARCHIVE_FATAL); |
2775 |
s = t; /* Start of line? */ |
3424 |
s = t; /* Start of line? */ |
2776 |
p = memchr(t, '\n', bytes_read); |
3425 |
p = memchr(t, '\n', bytes_read); |
Lines 2811-2817
readline(struct archive_read *a, struct tar *tar, const char **start,
Link Here
|
2811 |
} |
3460 |
} |
2812 |
/* Read some more. */ |
3461 |
/* Read some more. */ |
2813 |
t = __archive_read_ahead(a, 1, &bytes_read); |
3462 |
t = __archive_read_ahead(a, 1, &bytes_read); |
2814 |
if (bytes_read <= 0) |
3463 |
if (bytes_read <= 0 || t == NULL) |
2815 |
return (ARCHIVE_FATAL); |
3464 |
return (ARCHIVE_FATAL); |
2816 |
s = t; /* Start of line? */ |
3465 |
s = t; /* Start of line? */ |
2817 |
p = memchr(t, '\n', bytes_read); |
3466 |
p = memchr(t, '\n', bytes_read); |
Lines 2855-2861
base64_decode(const char *s, size_t len, size_t *out_len)
Link Here
|
2855 |
|
3504 |
|
2856 |
/* Allocate enough space to hold the entire output. */ |
3505 |
/* Allocate enough space to hold the entire output. */ |
2857 |
/* Note that we may not use all of this... */ |
3506 |
/* Note that we may not use all of this... */ |
2858 |
out = (char *)malloc(len - len / 4 + 1); |
3507 |
out = malloc(len - len / 4 + 1); |
2859 |
if (out == NULL) { |
3508 |
if (out == NULL) { |
2860 |
*out_len = 0; |
3509 |
*out_len = 0; |
2861 |
return (NULL); |
3510 |
return (NULL); |
Lines 2905-2926
base64_decode(const char *s, size_t len, size_t *out_len)
Link Here
|
2905 |
} |
3554 |
} |
2906 |
|
3555 |
|
2907 |
static char * |
3556 |
static char * |
2908 |
url_decode(const char *in) |
3557 |
url_decode(const char *in, size_t length) |
2909 |
{ |
3558 |
{ |
2910 |
char *out, *d; |
3559 |
char *out, *d; |
2911 |
const char *s; |
3560 |
const char *s; |
2912 |
|
3561 |
|
2913 |
out = (char *)malloc(strlen(in) + 1); |
3562 |
out = malloc(length + 1); |
2914 |
if (out == NULL) |
3563 |
if (out == NULL) |
2915 |
return (NULL); |
3564 |
return (NULL); |
2916 |
for (s = in, d = out; *s != '\0'; ) { |
3565 |
for (s = in, d = out; length > 0 && *s != '\0'; ) { |
2917 |
if (s[0] == '%' && s[1] != '\0' && s[2] != '\0') { |
3566 |
if (s[0] == '%' && length > 2) { |
2918 |
/* Try to convert % escape */ |
3567 |
/* Try to convert % escape */ |
2919 |
int digit1 = tohex(s[1]); |
3568 |
int digit1 = tohex(s[1]); |
2920 |
int digit2 = tohex(s[2]); |
3569 |
int digit2 = tohex(s[2]); |
2921 |
if (digit1 >= 0 && digit2 >= 0) { |
3570 |
if (digit1 >= 0 && digit2 >= 0) { |
2922 |
/* Looks good, consume three chars */ |
3571 |
/* Looks good, consume three chars */ |
2923 |
s += 3; |
3572 |
s += 3; |
|
|
3573 |
length -= 3; |
2924 |
/* Convert output */ |
3574 |
/* Convert output */ |
2925 |
*d++ = ((digit1 << 4) | digit2); |
3575 |
*d++ = ((digit1 << 4) | digit2); |
2926 |
continue; |
3576 |
continue; |
Lines 2928-2933
url_decode(const char *in)
Link Here
|
2928 |
/* Else fall through and treat '%' as normal char */ |
3578 |
/* Else fall through and treat '%' as normal char */ |
2929 |
} |
3579 |
} |
2930 |
*d++ = *s++; |
3580 |
*d++ = *s++; |
|
|
3581 |
--length; |
2931 |
} |
3582 |
} |
2932 |
*d = '\0'; |
3583 |
*d = '\0'; |
2933 |
return (out); |
3584 |
return (out); |