/src/libarchive/libarchive/archive_read_support_format_tar.c
Line | Count | Source |
1 | | /*- |
2 | | * Copyright (c) 2003-2023 Tim Kientzle |
3 | | * Copyright (c) 2011-2012 Michihiro NAKAJIMA |
4 | | * Copyright (c) 2016 Martin Matuska |
5 | | * All rights reserved. |
6 | | * |
7 | | * Redistribution and use in source and binary forms, with or without |
8 | | * modification, are permitted provided that the following conditions |
9 | | * are met: |
10 | | * 1. Redistributions of source code must retain the above copyright |
11 | | * notice, this list of conditions and the following disclaimer. |
12 | | * 2. Redistributions in binary form must reproduce the above copyright |
13 | | * notice, this list of conditions and the following disclaimer in the |
14 | | * documentation and/or other materials provided with the distribution. |
15 | | * |
16 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR |
17 | | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | | * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | | */ |
27 | | |
28 | | #include "archive_platform.h" |
29 | | |
30 | | #ifdef HAVE_ERRNO_H |
31 | | #include <errno.h> |
32 | | #endif |
33 | | #include <stddef.h> |
34 | | #ifdef HAVE_STDLIB_H |
35 | | #include <stdlib.h> |
36 | | #endif |
37 | | #ifdef HAVE_STRING_H |
38 | | #include <string.h> |
39 | | #endif |
40 | | |
41 | | #include "archive.h" |
42 | | #include "archive_acl_private.h" /* For ACL parsing routines. */ |
43 | | #include "archive_entry.h" |
44 | | #include "archive_entry_locale.h" |
45 | | #include "archive_private.h" |
46 | | #include "archive_read_private.h" |
47 | | |
48 | 3 | #define tar_min(a,b) ((a) < (b) ? (a) : (b)) |
49 | | |
50 | | /* |
51 | | * Layout of POSIX 'ustar' tar header. |
52 | | */ |
53 | | struct archive_entry_header_ustar { |
54 | | char name[100]; |
55 | | char mode[8]; |
56 | | char uid[8]; |
57 | | char gid[8]; |
58 | | char size[12]; |
59 | | char mtime[12]; |
60 | | char checksum[8]; |
61 | | char typeflag[1]; |
62 | | char linkname[100]; /* "old format" header ends here */ |
63 | | char magic[6]; /* For POSIX: "ustar\0" */ |
64 | | char version[2]; /* For POSIX: "00" */ |
65 | | char uname[32]; |
66 | | char gname[32]; |
67 | | char rdevmajor[8]; |
68 | | char rdevminor[8]; |
69 | | char prefix[155]; |
70 | | }; |
71 | | |
72 | | /* |
73 | | * Structure of GNU tar header |
74 | | */ |
75 | | struct gnu_sparse { |
76 | | char offset[12]; |
77 | | char numbytes[12]; |
78 | | }; |
79 | | |
80 | | struct archive_entry_header_gnutar { |
81 | | char name[100]; |
82 | | char mode[8]; |
83 | | char uid[8]; |
84 | | char gid[8]; |
85 | | char size[12]; |
86 | | char mtime[12]; |
87 | | char checksum[8]; |
88 | | char typeflag[1]; |
89 | | char linkname[100]; |
90 | | char magic[8]; /* "ustar \0" (note blank/blank/null at end) */ |
91 | | char uname[32]; |
92 | | char gname[32]; |
93 | | char rdevmajor[8]; |
94 | | char rdevminor[8]; |
95 | | char atime[12]; |
96 | | char ctime[12]; |
97 | | char offset[12]; |
98 | | char longnames[4]; |
99 | | char unused[1]; |
100 | | struct gnu_sparse sparse[4]; |
101 | | char isextended[1]; |
102 | | char realsize[12]; |
103 | | /* |
104 | | * Old GNU format doesn't use POSIX 'prefix' field; they use |
105 | | * the 'L' (longname) entry instead. |
106 | | */ |
107 | | }; |
108 | | |
109 | | /* |
110 | | * Data specific to this format. |
111 | | */ |
112 | | struct sparse_block { |
113 | | struct sparse_block *next; |
114 | | int64_t offset; |
115 | | int64_t remaining; |
116 | | int hole; |
117 | | }; |
118 | | |
119 | | struct tar { |
120 | | struct archive_string entry_pathname; |
121 | | /* For "GNU.sparse.name" and other similar path extensions. */ |
122 | | struct archive_string entry_pathname_override; |
123 | | struct archive_string entry_uname; |
124 | | struct archive_string entry_gname; |
125 | | struct archive_string entry_linkpath; |
126 | | struct archive_string line; |
127 | | int pax_hdrcharset_utf8; |
128 | | int64_t entry_bytes_remaining; |
129 | | int64_t entry_offset; |
130 | | int64_t entry_padding; |
131 | | int64_t entry_bytes_unconsumed; |
132 | | int64_t disk_size; |
133 | | int64_t GNU_sparse_realsize; |
134 | | int64_t GNU_sparse_size; |
135 | | int64_t SCHILY_sparse_realsize; |
136 | | int64_t pax_size; |
137 | | struct sparse_block *sparse_list; |
138 | | struct sparse_block *sparse_last; |
139 | | int64_t sparse_offset; |
140 | | int64_t sparse_numbytes; |
141 | | int sparse_gnu_major; |
142 | | int sparse_gnu_minor; |
143 | | char sparse_gnu_attributes_seen; |
144 | | char filetype; |
145 | | char size_fields; /* Bits defined below */ |
146 | | |
147 | | struct archive_string localname; |
148 | | struct archive_string_conv *opt_sconv; |
149 | | struct archive_string_conv *sconv; |
150 | | struct archive_string_conv *sconv_acl; |
151 | | struct archive_string_conv *sconv_default; |
152 | | int init_default_conversion; |
153 | | int compat_2x; |
154 | | int process_mac_extensions; |
155 | | int read_concatenated_archives; |
156 | | int default_inode; |
157 | | int default_dev; |
158 | | }; |
159 | | |
160 | | /* Track which size fields were present in the headers */ |
161 | 41.7k | #define TAR_SIZE_PAX_SIZE 1 |
162 | 21.9k | #define TAR_SIZE_GNU_SPARSE_REALSIZE 2 |
163 | 41.7k | #define TAR_SIZE_GNU_SPARSE_SIZE 4 |
164 | 20.5k | #define TAR_SIZE_SCHILY_SPARSE_REALSIZE 8 |
165 | | |
166 | | |
167 | | static int archive_block_is_null(const char *p); |
168 | | static char *base64_decode(const char *, size_t, size_t *); |
169 | | static int gnu_add_sparse_entry(struct archive_read *, struct tar *, |
170 | | int64_t offset, int64_t remaining); |
171 | | |
172 | | static void gnu_clear_sparse_list(struct tar *); |
173 | | static int gnu_sparse_old_read(struct archive_read *, struct tar *, |
174 | | const struct archive_entry_header_gnutar *header, int64_t *); |
175 | | static int gnu_sparse_old_parse(struct archive_read *, struct tar *, |
176 | | const struct gnu_sparse *sparse, int length); |
177 | | static int gnu_sparse_01_parse(struct archive_read *, struct tar *, |
178 | | const char *, size_t); |
179 | | static int64_t gnu_sparse_10_read(struct archive_read *, struct tar *, |
180 | | int64_t *); |
181 | | static int header_Solaris_ACL(struct archive_read *, struct tar *, |
182 | | struct archive_entry *, const void *, int64_t *); |
183 | | static int header_common(struct archive_read *, struct tar *, |
184 | | struct archive_entry *, const void *); |
185 | | static int header_old_tar(struct archive_read *, struct tar *, |
186 | | struct archive_entry *, const void *); |
187 | | static int header_pax_extension(struct archive_read *, struct tar *, |
188 | | struct archive_entry *, const void *, int64_t *); |
189 | | static int header_pax_global(struct archive_read *, struct tar *, |
190 | | struct archive_entry *, const void *h, int64_t *); |
191 | | static int header_gnu_longlink(struct archive_read *, struct tar *, |
192 | | struct archive_entry *, const void *h, int64_t *); |
193 | | static int header_gnu_longname(struct archive_read *, struct tar *, |
194 | | struct archive_entry *, const void *h, int64_t *); |
195 | | static int is_mac_metadata_entry(struct archive_entry *entry); |
196 | | static int read_mac_metadata_blob(struct archive_read *, |
197 | | struct archive_entry *, int64_t *); |
198 | | static int header_volume(struct archive_read *, struct tar *, |
199 | | struct archive_entry *, const void *h, int64_t *); |
200 | | static int header_ustar(struct archive_read *, struct tar *, |
201 | | struct archive_entry *, const void *h); |
202 | | static int header_gnutar(struct archive_read *, struct tar *, |
203 | | struct archive_entry *, const void *h, int64_t *); |
204 | | static int archive_read_format_tar_bid(struct archive_read *, int); |
205 | | static int archive_read_format_tar_options(struct archive_read *, |
206 | | const char *, const char *); |
207 | | static int archive_read_format_tar_cleanup(struct archive_read *); |
208 | | static int archive_read_format_tar_read_data(struct archive_read *a, |
209 | | const void **buff, size_t *size, int64_t *offset); |
210 | | static int archive_read_format_tar_skip(struct archive_read *a); |
211 | | static int archive_read_format_tar_read_header(struct archive_read *, |
212 | | struct archive_entry *); |
213 | | static int checksum(struct archive_read *, const void *); |
214 | | static int pax_attribute(struct archive_read *, struct tar *, |
215 | | struct archive_entry *, const char *key, size_t key_length, |
216 | | size_t value_length, int64_t *unconsumed); |
217 | | static int pax_attribute_LIBARCHIVE_xattr(struct archive_entry *, |
218 | | const char *, size_t, const char *, size_t); |
219 | | static int pax_attribute_SCHILY_acl(struct archive_read *, struct tar *, |
220 | | struct archive_entry *, size_t, int); |
221 | | static int pax_attribute_SUN_holesdata(struct archive_read *, struct tar *, |
222 | | struct archive_entry *, const char *, size_t); |
223 | | static void pax_time(const char *, size_t, int64_t *sec, long *nanos); |
224 | | static ssize_t readline(struct archive_read *, struct tar *, const char **, |
225 | | ssize_t limit, int64_t *); |
226 | | static int read_body_to_string(struct archive_read *, struct tar *, |
227 | | struct archive_string *, const void *h, int64_t *); |
228 | | static int read_bytes_to_string(struct archive_read *, |
229 | | struct archive_string *, size_t, int64_t *); |
230 | | static int64_t tar_atol(const char *, size_t); |
231 | | static int64_t tar_atol10(const char *, size_t); |
232 | | static int64_t tar_atol256(const char *, size_t); |
233 | | static int64_t tar_atol8(const char *, size_t); |
234 | | static int tar_read_header(struct archive_read *, struct tar *, |
235 | | struct archive_entry *, int64_t *); |
236 | | static int tohex(int c); |
237 | | static char *url_decode(const char *, size_t); |
238 | | static int tar_flush_unconsumed(struct archive_read *, int64_t *); |
239 | | |
240 | | /* Sanity limits: These numbers should be low enough to |
241 | | * prevent a maliciously-crafted archive from forcing us to |
242 | | * allocate extreme amounts of memory. But of course, they |
243 | | * need to be high enough for any correct value. These |
244 | | * will likely need some adjustment as we get more experience. */ |
245 | | static const size_t guname_limit = 65536; /* Longest uname or gname: 64kiB */ |
246 | | static const size_t pathname_limit = 1048576; /* Longest path name: 1MiB */ |
247 | | static const size_t sparse_map_limit = 8 * 1048576; /* Longest sparse map: 8MiB */ |
248 | | static const size_t xattr_limit = 16 * 1048576; /* Longest xattr: 16MiB */ |
249 | | static const size_t fflags_limit = 512; /* Longest fflags */ |
250 | | static const size_t acl_limit = 131072; /* Longest textual ACL: 128kiB */ |
251 | | static const int64_t entry_limit = 0xfffffffffffffffLL; /* 2^60 bytes = 1 ExbiByte */ |
252 | | |
253 | | /* |
254 | | * There's no standard for TIME_T_MAX. So we compute it |
255 | | * here. TODO: Move this to configure time, but be careful |
256 | | * about cross-compile environments. |
257 | | */ |
258 | | static time_t |
259 | | get_time_t_max(void) |
260 | 0 | { |
261 | | #if defined(TIME_T_MAX) |
262 | | return TIME_T_MAX; |
263 | | #else |
264 | | /* ISO C allows time_t to be a floating-point type, |
265 | | but POSIX requires an integer type. The following |
266 | | should work on any system that follows the POSIX |
267 | | conventions. */ |
268 | 0 | if (((time_t)0) < ((time_t)-1)) { |
269 | | /* Time_t is unsigned */ |
270 | 0 | return (~(time_t)0); |
271 | 0 | } else { |
272 | | /* Time_t is signed. */ |
273 | | /* Assume it's the same as int64_t or int32_t */ |
274 | 0 | if (sizeof(time_t) == sizeof(int64_t)) { |
275 | 0 | return (time_t)INT64_MAX; |
276 | 0 | } else { |
277 | 0 | return (time_t)INT32_MAX; |
278 | 0 | } |
279 | 0 | } |
280 | 0 | #endif |
281 | 0 | } |
282 | | |
283 | | int |
284 | | archive_read_support_format_gnutar(struct archive *a) |
285 | 2.49k | { |
286 | 2.49k | archive_check_magic(a, ARCHIVE_READ_MAGIC, |
287 | 2.49k | ARCHIVE_STATE_NEW, "archive_read_support_format_gnutar"); |
288 | 2.49k | return (archive_read_support_format_tar(a)); |
289 | 2.49k | } |
290 | | |
291 | | |
292 | | int |
293 | | archive_read_support_format_tar(struct archive *_a) |
294 | 4.99k | { |
295 | 4.99k | struct archive_read *a = (struct archive_read *)_a; |
296 | 4.99k | struct tar *tar; |
297 | 4.99k | int r; |
298 | | |
299 | 4.99k | archive_check_magic(_a, ARCHIVE_READ_MAGIC, |
300 | 4.99k | ARCHIVE_STATE_NEW, "archive_read_support_format_tar"); |
301 | | |
302 | 4.99k | tar = calloc(1, sizeof(*tar)); |
303 | 4.99k | if (tar == NULL) { |
304 | 0 | archive_set_error(&a->archive, ENOMEM, |
305 | 0 | "Can't allocate tar data"); |
306 | 0 | return (ARCHIVE_FATAL); |
307 | 0 | } |
308 | | #ifdef HAVE_COPYFILE_H |
309 | | /* Set this by default on Mac OS. */ |
310 | | tar->process_mac_extensions = 1; |
311 | | #endif |
312 | | |
313 | 4.99k | r = __archive_read_register_format(a, tar, "tar", |
314 | 4.99k | archive_read_format_tar_bid, |
315 | 4.99k | archive_read_format_tar_options, |
316 | 4.99k | archive_read_format_tar_read_header, |
317 | 4.99k | archive_read_format_tar_read_data, |
318 | 4.99k | archive_read_format_tar_skip, |
319 | 4.99k | NULL, |
320 | 4.99k | archive_read_format_tar_cleanup, |
321 | 4.99k | NULL, |
322 | 4.99k | NULL); |
323 | | |
324 | 4.99k | if (r != ARCHIVE_OK) |
325 | 2.49k | free(tar); |
326 | 4.99k | return (ARCHIVE_OK); |
327 | 4.99k | } |
328 | | |
329 | | static int |
330 | | archive_read_format_tar_cleanup(struct archive_read *a) |
331 | 2.49k | { |
332 | 2.49k | struct tar *tar; |
333 | | |
334 | 2.49k | tar = (struct tar *)(a->format->data); |
335 | 2.49k | gnu_clear_sparse_list(tar); |
336 | 2.49k | archive_string_free(&tar->entry_pathname); |
337 | 2.49k | archive_string_free(&tar->entry_pathname_override); |
338 | 2.49k | archive_string_free(&tar->entry_uname); |
339 | 2.49k | archive_string_free(&tar->entry_gname); |
340 | 2.49k | archive_string_free(&tar->entry_linkpath); |
341 | 2.49k | archive_string_free(&tar->line); |
342 | 2.49k | archive_string_free(&tar->localname); |
343 | 2.49k | free(tar); |
344 | 2.49k | (a->format->data) = NULL; |
345 | 2.49k | return (ARCHIVE_OK); |
346 | 2.49k | } |
347 | | |
348 | | /* |
349 | | * Validate number field |
350 | | * |
351 | | * This has to be pretty lenient in order to accommodate the enormous |
352 | | * variety of tar writers in the world: |
353 | | * = POSIX (IEEE Std 1003.1-1988) ustar requires octal values with leading |
354 | | * zeros and allows fields to be terminated with space or null characters |
355 | | * = Many writers use different termination (in particular, libarchive |
356 | | * omits terminator bytes to squeeze one or two more digits) |
357 | | * = Many writers pad with space and omit leading zeros |
358 | | * = GNU tar and star write base-256 values if numbers are too |
359 | | * big to be represented in octal |
360 | | * |
361 | | * Examples of specific tar headers that we should support: |
362 | | * = Perl Archive::Tar terminates uid, gid, devminor and devmajor with two |
363 | | * null bytes, pads size with spaces and other numeric fields with zeroes |
364 | | * = plexus-archiver prior to 2.6.3 (before switching to commons-compress) |
365 | | * may have uid and gid fields filled with spaces without any octal digits |
366 | | * at all and pads all numeric fields with spaces |
367 | | * |
368 | | * This should tolerate all variants in use. It will reject a field |
369 | | * where the writer just left garbage after a trailing NUL. |
370 | | */ |
371 | | static int |
372 | | validate_number_field(const char* p_field, size_t i_size) |
373 | 33.4k | { |
374 | 33.4k | unsigned char marker = (unsigned char)p_field[0]; |
375 | 33.4k | if (marker == 128 || marker == 255 || marker == 0) { |
376 | | /* Base-256 marker, there's nothing we can check. */ |
377 | 4.55k | return 1; |
378 | 28.9k | } else { |
379 | | /* Must be octal */ |
380 | 28.9k | size_t i = 0; |
381 | | /* Skip any leading spaces */ |
382 | 160k | while (i < i_size && p_field[i] == ' ') { |
383 | 131k | ++i; |
384 | 131k | } |
385 | | /* Skip octal digits. */ |
386 | 105k | while (i < i_size && p_field[i] >= '0' && p_field[i] <= '7') { |
387 | 76.3k | ++i; |
388 | 76.3k | } |
389 | | /* Any remaining characters must be space or NUL padding. */ |
390 | 67.9k | while (i < i_size) { |
391 | 42.6k | if (p_field[i] != ' ' && p_field[i] != 0) { |
392 | 3.64k | return 0; |
393 | 3.64k | } |
394 | 39.0k | ++i; |
395 | 39.0k | } |
396 | 25.2k | return 1; |
397 | 28.9k | } |
398 | 33.4k | } |
399 | | |
400 | | static int |
401 | | archive_read_format_tar_bid(struct archive_read *a, int best_bid) |
402 | 7.62k | { |
403 | 7.62k | int bid; |
404 | 7.62k | const char *h; |
405 | 7.62k | const struct archive_entry_header_ustar *header; |
406 | | |
407 | 7.62k | (void)best_bid; /* UNUSED */ |
408 | | |
409 | 7.62k | bid = 0; |
410 | | |
411 | | /* Now let's look at the actual header and see if it matches. */ |
412 | 7.62k | h = __archive_read_ahead(a, 512, NULL); |
413 | 7.62k | if (h == NULL) |
414 | 25 | return (-1); |
415 | | |
416 | | /* If it's an end-of-archive mark, we can handle it. */ |
417 | 7.60k | if (h[0] == 0 && archive_block_is_null(h)) { |
418 | | /* |
419 | | * Usually, I bid the number of bits verified, but |
420 | | * in this case, 4096 seems excessive so I picked 10 as |
421 | | * an arbitrary but reasonable-seeming value. |
422 | | */ |
423 | 129 | return (10); |
424 | 129 | } |
425 | | |
426 | | /* If it's not an end-of-archive mark, it must have a valid checksum.*/ |
427 | 7.47k | if (!checksum(a, h)) |
428 | 1.68k | return (0); |
429 | 5.78k | bid += 48; /* Checksum is usually 6 octal digits. */ |
430 | | |
431 | 5.78k | header = (const struct archive_entry_header_ustar *)h; |
432 | | |
433 | | /* Recognize POSIX formats. */ |
434 | 5.78k | if ((memcmp(header->magic, "ustar\0", 6) == 0) |
435 | 677 | && (memcmp(header->version, "00", 2) == 0)) |
436 | 433 | bid += 56; |
437 | | |
438 | | /* Recognize GNU tar format. */ |
439 | 5.78k | if ((memcmp(header->magic, "ustar ", 6) == 0) |
440 | 4.46k | && (memcmp(header->version, " \0", 2) == 0)) |
441 | 143 | bid += 56; |
442 | | |
443 | | /* Type flag must be null, digit or A-Z, a-z. */ |
444 | 5.78k | if (header->typeflag[0] != 0 && |
445 | 5.46k | !( header->typeflag[0] >= '0' && header->typeflag[0] <= '9') && |
446 | 165 | !( header->typeflag[0] >= 'A' && header->typeflag[0] <= 'Z') && |
447 | 60 | !( header->typeflag[0] >= 'a' && header->typeflag[0] <= 'z') ) |
448 | 11 | return (0); |
449 | 5.77k | bid += 2; /* 6 bits of variation in an 8-bit field leaves 2 bits. */ |
450 | | |
451 | | /* |
452 | | * Check format of mode/uid/gid/mtime/size/rdevmajor/rdevminor fields. |
453 | | */ |
454 | 5.77k | if (validate_number_field(header->mode, sizeof(header->mode)) == 0 |
455 | 5.48k | || validate_number_field(header->uid, sizeof(header->uid)) == 0 |
456 | 5.30k | || validate_number_field(header->gid, sizeof(header->gid)) == 0 |
457 | 4.82k | || validate_number_field(header->mtime, sizeof(header->mtime)) == 0 |
458 | 4.53k | || validate_number_field(header->size, sizeof(header->size)) == 0 |
459 | 4.35k | || validate_number_field(header->rdevmajor, sizeof(header->rdevmajor)) == 0 |
460 | 3.64k | || validate_number_field(header->rdevminor, sizeof(header->rdevminor)) == 0) { |
461 | 3.64k | bid = 0; |
462 | 3.64k | } |
463 | | |
464 | 5.77k | return (bid); |
465 | 5.78k | } |
466 | | |
467 | | static int |
468 | | archive_read_format_tar_options(struct archive_read *a, |
469 | | const char *key, const char *val) |
470 | 4.99k | { |
471 | 4.99k | struct tar *tar; |
472 | 4.99k | int ret = ARCHIVE_FAILED; |
473 | | |
474 | 4.99k | tar = (struct tar *)(a->format->data); |
475 | 4.99k | if (strcmp(key, "compat-2x") == 0) { |
476 | | /* Handle UTF-8 filenames as libarchive 2.x */ |
477 | 0 | tar->compat_2x = (val != NULL && val[0] != 0); |
478 | 0 | tar->init_default_conversion = tar->compat_2x; |
479 | 0 | return (ARCHIVE_OK); |
480 | 4.99k | } else if (strcmp(key, "hdrcharset") == 0) { |
481 | 0 | if (val == NULL || val[0] == 0) |
482 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
483 | 0 | "tar: hdrcharset option needs a character-set name"); |
484 | 0 | else { |
485 | 0 | tar->opt_sconv = |
486 | 0 | archive_string_conversion_from_charset( |
487 | 0 | &a->archive, val, 0); |
488 | 0 | if (tar->opt_sconv != NULL) |
489 | 0 | ret = ARCHIVE_OK; |
490 | 0 | else |
491 | 0 | ret = ARCHIVE_FATAL; |
492 | 0 | } |
493 | 0 | return (ret); |
494 | 4.99k | } else if (strcmp(key, "mac-ext") == 0) { |
495 | 2.49k | tar->process_mac_extensions = (val != NULL && val[0] != 0); |
496 | 2.49k | return (ARCHIVE_OK); |
497 | 2.49k | } else if (strcmp(key, "read_concatenated_archives") == 0) { |
498 | 2.49k | tar->read_concatenated_archives = (val != NULL && val[0] != 0); |
499 | 2.49k | return (ARCHIVE_OK); |
500 | 2.49k | } |
501 | | |
502 | | /* Note: The "warn" return is just to inform the options |
503 | | * supervisor that we didn't handle it. It will generate |
504 | | * a suitable error if no one used this option. */ |
505 | 0 | return (ARCHIVE_WARN); |
506 | 4.99k | } |
507 | | |
508 | | /* utility function- this exists to centralize the logic of tracking |
509 | | * how much unconsumed data we have floating around, and to consume |
510 | | * anything outstanding since we're going to do read_aheads |
511 | | */ |
512 | | static int |
513 | | tar_flush_unconsumed(struct archive_read *a, int64_t *unconsumed) |
514 | 721k | { |
515 | 721k | if (*unconsumed) { |
516 | | /* |
517 | | void *data = (void *)__archive_read_ahead(a, *unconsumed, NULL); |
518 | | * this block of code is to poison claimed unconsumed space, ensuring |
519 | | * things break if it is in use still. |
520 | | * currently it WILL break things, so enable it only for debugging this issue |
521 | | if (data) { |
522 | | memset(data, 0xff, *unconsumed); |
523 | | } |
524 | | */ |
525 | 273k | int64_t consumed = __archive_read_consume(a, *unconsumed); |
526 | 273k | if (consumed != *unconsumed) { |
527 | 159 | return (ARCHIVE_FATAL); |
528 | 159 | } |
529 | 273k | *unconsumed = 0; |
530 | 273k | } |
531 | 721k | return (ARCHIVE_OK); |
532 | 721k | } |
533 | | |
534 | | /* |
535 | | * The function invoked by archive_read_next_header(). This |
536 | | * just sets up a few things and then calls the internal |
537 | | * tar_read_header() function below. |
538 | | */ |
539 | | static int |
540 | | archive_read_format_tar_read_header(struct archive_read *a, |
541 | | struct archive_entry *entry) |
542 | 233k | { |
543 | | /* |
544 | | * When converting tar archives to cpio archives, it is |
545 | | * essential that each distinct file have a distinct inode |
546 | | * number. To simplify this, we keep a static count here to |
547 | | * assign fake dev/inode numbers to each tar entry. Note that |
548 | | * pax format archives may overwrite this with something more |
549 | | * useful. |
550 | | * |
551 | | * Ideally, we would track every file read from the archive so |
552 | | * that we could assign the same dev/ino pair to hardlinks, |
553 | | * but the memory required to store a complete lookup table is |
554 | | * probably not worthwhile just to support the relatively |
555 | | * obscure tar->cpio conversion case. |
556 | | */ |
557 | 233k | struct tar *tar; |
558 | 233k | const char *p; |
559 | 233k | const wchar_t *wp; |
560 | 233k | int r; |
561 | 233k | size_t l; |
562 | 233k | int64_t unconsumed = 0; |
563 | | |
564 | 233k | tar = (struct tar *)(a->format->data); |
565 | | |
566 | | /* Assign default device/inode values. */ |
567 | 233k | archive_entry_set_dev(entry, 1 + tar->default_dev); /* Don't use zero. */ |
568 | 233k | archive_entry_set_ino(entry, ++tar->default_inode); /* Don't use zero. */ |
569 | | /* Limit generated st_ino number to 16 bits. */ |
570 | 233k | if (tar->default_inode >= 0xffff) { |
571 | 2 | ++tar->default_dev; |
572 | 2 | tar->default_inode = 0; |
573 | 2 | } |
574 | | |
575 | 233k | tar->entry_offset = 0; |
576 | 233k | gnu_clear_sparse_list(tar); |
577 | 233k | tar->size_fields = 0; /* We don't have any size info yet */ |
578 | | |
579 | | /* Setup default string conversion. */ |
580 | 233k | tar->sconv = tar->opt_sconv; |
581 | 233k | if (tar->sconv == NULL) { |
582 | 233k | if (!tar->init_default_conversion) { |
583 | 342 | tar->sconv_default = |
584 | 342 | archive_string_default_conversion_for_read(&(a->archive)); |
585 | 342 | tar->init_default_conversion = 1; |
586 | 342 | } |
587 | 233k | tar->sconv = tar->sconv_default; |
588 | 233k | } |
589 | | |
590 | 233k | r = tar_read_header(a, tar, entry, &unconsumed); |
591 | | |
592 | 233k | tar_flush_unconsumed(a, &unconsumed); |
593 | | |
594 | | /* |
595 | | * "non-sparse" files are really just sparse files with |
596 | | * a single block. |
597 | | */ |
598 | 233k | if (tar->sparse_list == NULL) { |
599 | 232k | if (gnu_add_sparse_entry(a, tar, 0, tar->entry_bytes_remaining) |
600 | 232k | != ARCHIVE_OK) |
601 | 0 | return (ARCHIVE_FATAL); |
602 | 232k | } else { |
603 | 1.56k | struct sparse_block *sb; |
604 | | |
605 | 40.7k | for (sb = tar->sparse_list; sb != NULL; sb = sb->next) { |
606 | 39.1k | if (!sb->hole) |
607 | 39.1k | archive_entry_sparse_add_entry(entry, |
608 | 39.1k | sb->offset, sb->remaining); |
609 | 39.1k | } |
610 | 1.56k | } |
611 | | |
612 | 233k | if (r == ARCHIVE_OK && archive_entry_filetype(entry) == AE_IFREG) { |
613 | | /* |
614 | | * "Regular" entry with trailing '/' is really |
615 | | * directory: This is needed for certain old tar |
616 | | * variants and even for some broken newer ones. |
617 | | */ |
618 | 18.8k | if ((wp = archive_entry_pathname_w(entry)) != NULL) { |
619 | 18.1k | l = wcslen(wp); |
620 | 18.1k | if (l > 0 && wp[l - 1] == L'/') { |
621 | 4.98k | archive_entry_set_filetype(entry, AE_IFDIR); |
622 | 4.98k | tar->entry_bytes_remaining = 0; |
623 | 4.98k | tar->entry_padding = 0; |
624 | 4.98k | } |
625 | 18.1k | } else if ((p = archive_entry_pathname(entry)) != NULL) { |
626 | 646 | l = strlen(p); |
627 | 646 | if (l > 0 && p[l - 1] == '/') { |
628 | 232 | archive_entry_set_filetype(entry, AE_IFDIR); |
629 | 232 | tar->entry_bytes_remaining = 0; |
630 | 232 | tar->entry_padding = 0; |
631 | 232 | } |
632 | 646 | } |
633 | 18.8k | } |
634 | 233k | return (r); |
635 | 233k | } |
636 | | |
637 | | static int |
638 | | archive_read_format_tar_read_data(struct archive_read *a, |
639 | | const void **buff, size_t *size, int64_t *offset) |
640 | 37.4k | { |
641 | 37.4k | ssize_t bytes_read; |
642 | 37.4k | struct tar *tar; |
643 | 37.4k | struct sparse_block *p; |
644 | | |
645 | 37.4k | tar = (struct tar *)(a->format->data); |
646 | | |
647 | 37.4k | for (;;) { |
648 | | /* Remove exhausted entries from sparse list. */ |
649 | 55.9k | while (tar->sparse_list != NULL && |
650 | 37.1k | tar->sparse_list->remaining == 0) { |
651 | 18.4k | p = tar->sparse_list; |
652 | 18.4k | tar->sparse_list = p->next; |
653 | 18.4k | free(p); |
654 | 18.4k | } |
655 | | |
656 | 37.4k | if (tar->entry_bytes_unconsumed) { |
657 | 1.10k | __archive_read_consume(a, tar->entry_bytes_unconsumed); |
658 | 1.10k | tar->entry_bytes_unconsumed = 0; |
659 | 1.10k | } |
660 | | |
661 | | /* If we're at end of file, return EOF. */ |
662 | 37.4k | if (tar->sparse_list == NULL || |
663 | 36.2k | tar->entry_bytes_remaining == 0) { |
664 | 36.2k | int64_t request = tar->entry_bytes_remaining + |
665 | 36.2k | tar->entry_padding; |
666 | | |
667 | 36.2k | if (__archive_read_consume(a, request) != request) |
668 | 4 | return (ARCHIVE_FATAL); |
669 | 36.2k | tar->entry_padding = 0; |
670 | 36.2k | *buff = NULL; |
671 | 36.2k | *size = 0; |
672 | 36.2k | *offset = tar->disk_size; |
673 | 36.2k | return (ARCHIVE_EOF); |
674 | 36.2k | } |
675 | | |
676 | 1.24k | *buff = __archive_read_ahead(a, 1, &bytes_read); |
677 | 1.24k | if (*buff == NULL) { |
678 | 11 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
679 | 11 | "Truncated tar archive" |
680 | 11 | " detected while reading data"); |
681 | 11 | return (ARCHIVE_FATAL); |
682 | 11 | } |
683 | 1.23k | if (bytes_read > tar->entry_bytes_remaining) |
684 | 1.20k | bytes_read = (ssize_t)tar->entry_bytes_remaining; |
685 | | /* Don't read more than is available in the |
686 | | * current sparse block. */ |
687 | 1.23k | if (tar->sparse_list->remaining < bytes_read) |
688 | 260 | bytes_read = (ssize_t)tar->sparse_list->remaining; |
689 | 1.23k | *size = bytes_read; |
690 | 1.23k | *offset = tar->sparse_list->offset; |
691 | 1.23k | tar->sparse_list->remaining -= bytes_read; |
692 | 1.23k | tar->sparse_list->offset += bytes_read; |
693 | 1.23k | tar->entry_bytes_remaining -= bytes_read; |
694 | 1.23k | tar->entry_bytes_unconsumed = bytes_read; |
695 | | |
696 | 1.23k | if (!tar->sparse_list->hole) |
697 | 1.23k | return (ARCHIVE_OK); |
698 | | /* Current is hole data and skip this. */ |
699 | 1.23k | } |
700 | 37.4k | } |
701 | | |
702 | | static int |
703 | | archive_read_format_tar_skip(struct archive_read *a) |
704 | 21.2k | { |
705 | 21.2k | int64_t request; |
706 | 21.2k | struct tar* tar; |
707 | | |
708 | 21.2k | tar = (struct tar *)(a->format->data); |
709 | | |
710 | 21.2k | request = tar->entry_bytes_remaining + tar->entry_padding + |
711 | 21.2k | tar->entry_bytes_unconsumed; |
712 | | |
713 | 21.2k | if (__archive_read_consume(a, request) != request) |
714 | 0 | return (ARCHIVE_FATAL); |
715 | | |
716 | 21.2k | tar->entry_bytes_remaining = 0; |
717 | 21.2k | tar->entry_bytes_unconsumed = 0; |
718 | 21.2k | tar->entry_padding = 0; |
719 | | |
720 | | /* Free the sparse list. */ |
721 | 21.2k | gnu_clear_sparse_list(tar); |
722 | | |
723 | 21.2k | return (ARCHIVE_OK); |
724 | 21.2k | } |
725 | | |
726 | | /* |
727 | | * This function resets the accumulated state while reading |
728 | | * a header. |
729 | | */ |
730 | | static void |
731 | | tar_reset_header_state(struct tar *tar) |
732 | 233k | { |
733 | 233k | tar->pax_hdrcharset_utf8 = 1; |
734 | 233k | tar->sparse_gnu_attributes_seen = 0; |
735 | 233k | archive_string_empty(&(tar->entry_gname)); |
736 | 233k | archive_string_empty(&(tar->entry_pathname)); |
737 | 233k | archive_string_empty(&(tar->entry_pathname_override)); |
738 | 233k | archive_string_empty(&(tar->entry_uname)); |
739 | 233k | archive_string_empty(&tar->entry_linkpath); |
740 | 233k | } |
741 | | |
742 | | /* |
743 | | * This function reads and interprets all of the headers associated |
744 | | * with a single entry. |
745 | | */ |
746 | | static int |
747 | | tar_read_header(struct archive_read *a, struct tar *tar, |
748 | | struct archive_entry *entry, int64_t *unconsumed) |
749 | 233k | { |
750 | 233k | ssize_t bytes; |
751 | 233k | int err = ARCHIVE_OK, err2; |
752 | 233k | int eof_fatal = 0; /* EOF is okay at some points... */ |
753 | 233k | const char *h; |
754 | 233k | const struct archive_entry_header_ustar *header; |
755 | 233k | const struct archive_entry_header_gnutar *gnuheader; |
756 | | |
757 | | /* Bitmask of what header types we've seen. */ |
758 | 233k | int32_t seen_headers = 0; |
759 | 233k | static const int32_t seen_A_header = 1; |
760 | 233k | static const int32_t seen_g_header = 2; |
761 | 233k | static const int32_t seen_K_header = 4; |
762 | 233k | static const int32_t seen_L_header = 8; |
763 | 233k | static const int32_t seen_V_header = 16; |
764 | 233k | static const int32_t seen_x_header = 32; /* Also X */ |
765 | 233k | static const int32_t seen_mac_metadata = 512; |
766 | | |
767 | 233k | tar_reset_header_state(tar); |
768 | | |
769 | | /* Ensure format is set. */ |
770 | 233k | if (a->archive.archive_format_name == NULL) { |
771 | 342 | a->archive.archive_format = ARCHIVE_FORMAT_TAR; |
772 | 342 | a->archive.archive_format_name = "tar"; |
773 | 342 | } |
774 | | |
775 | | /* |
776 | | * TODO: Write global/default pax options into |
777 | | * 'entry' struct here before overwriting with |
778 | | * file-specific options. |
779 | | */ |
780 | | |
781 | | /* Loop over all the headers needed for the next entry */ |
782 | 235k | for (;;) { |
783 | | |
784 | | /* Find the next valid header record. */ |
785 | 268k | while (1) { |
786 | 268k | if (tar_flush_unconsumed(a, unconsumed) != ARCHIVE_OK) { |
787 | 78 | return (ARCHIVE_FATAL); |
788 | 78 | } |
789 | | |
790 | | /* Read 512-byte header record */ |
791 | 268k | h = __archive_read_ahead(a, 512, &bytes); |
792 | 268k | if (bytes == 0) { /* EOF at a block boundary. */ |
793 | 16 | if (eof_fatal) { |
794 | | /* We've read a special header already; |
795 | | * if there's no regular header, then this is |
796 | | * a premature EOF. */ |
797 | 8 | archive_set_error(&a->archive, EINVAL, |
798 | 8 | "Damaged tar archive (end-of-archive within a sequence of headers)"); |
799 | 8 | return (ARCHIVE_FATAL); |
800 | 8 | } else { |
801 | 8 | return (ARCHIVE_EOF); |
802 | 8 | } |
803 | 16 | } |
804 | 268k | if (h == NULL) { /* Short block at EOF; this is bad. */ |
805 | 190 | archive_set_error(&a->archive, |
806 | 190 | ARCHIVE_ERRNO_FILE_FORMAT, |
807 | 190 | "Truncated tar archive" |
808 | 190 | " detected while reading next header"); |
809 | 190 | return (ARCHIVE_FATAL); |
810 | 190 | } |
811 | 268k | *unconsumed += 512; |
812 | | |
813 | 268k | if (h[0] == 0 && archive_block_is_null(h)) { |
814 | | /* We found a NULL block which indicates end-of-archive */ |
815 | | |
816 | 32.9k | if (tar->read_concatenated_archives) { |
817 | | /* We're ignoring NULL blocks, so keep going. */ |
818 | 32.9k | continue; |
819 | 32.9k | } |
820 | | |
821 | | /* Try to consume a second all-null record, as well. */ |
822 | | /* If we can't, that's okay. */ |
823 | 0 | tar_flush_unconsumed(a, unconsumed); |
824 | 0 | h = __archive_read_ahead(a, 512, NULL); |
825 | 0 | if (h != NULL && h[0] == 0 && archive_block_is_null(h)) |
826 | 0 | __archive_read_consume(a, 512); |
827 | |
|
828 | 0 | archive_clear_error(&a->archive); |
829 | 0 | return (ARCHIVE_EOF); |
830 | 32.9k | } |
831 | | |
832 | | /* This is NOT a null block, so it must be a valid header. */ |
833 | 235k | if (!checksum(a, h)) { |
834 | 212k | if (tar_flush_unconsumed(a, unconsumed) != ARCHIVE_OK) { |
835 | 0 | return (ARCHIVE_FATAL); |
836 | 0 | } |
837 | 212k | archive_set_error(&a->archive, EINVAL, |
838 | 212k | "Damaged tar archive (bad header checksum)"); |
839 | | /* If we've read some critical information (pax headers, etc) |
840 | | * and _then_ see a bad header, we can't really recover. */ |
841 | 212k | if (eof_fatal) { |
842 | 12 | return (ARCHIVE_FATAL); |
843 | 212k | } else { |
844 | 212k | return (ARCHIVE_RETRY); |
845 | 212k | } |
846 | 212k | } |
847 | 23.2k | break; |
848 | 235k | } |
849 | | |
850 | | /* Determine the format variant. */ |
851 | 23.2k | header = (const struct archive_entry_header_ustar *)h; |
852 | 23.2k | switch(header->typeflag[0]) { |
853 | 122 | case 'A': /* Solaris tar ACL */ |
854 | 122 | if (seen_headers & seen_A_header) { |
855 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
856 | 0 | "Redundant 'A' header"); |
857 | 0 | return (ARCHIVE_FATAL); |
858 | 0 | } |
859 | 122 | seen_headers |= seen_A_header; |
860 | 122 | a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE; |
861 | 122 | a->archive.archive_format_name = "Solaris tar"; |
862 | 122 | err2 = header_Solaris_ACL(a, tar, entry, h, unconsumed); |
863 | 122 | break; |
864 | 1.24k | case 'g': /* POSIX-standard 'g' header. */ |
865 | 1.24k | if (seen_headers & seen_g_header) { |
866 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
867 | 0 | "Redundant 'g' header"); |
868 | 0 | return (ARCHIVE_FATAL); |
869 | 0 | } |
870 | 1.24k | seen_headers |= seen_g_header; |
871 | 1.24k | a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE; |
872 | 1.24k | a->archive.archive_format_name = "POSIX pax interchange format"; |
873 | 1.24k | err2 = header_pax_global(a, tar, entry, h, unconsumed); |
874 | 1.24k | break; |
875 | 379 | case 'K': /* Long link name (GNU tar, others) */ |
876 | 379 | if (seen_headers & seen_K_header) { |
877 | 369 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
878 | 369 | "Damaged archive: Redundant 'K' headers may cause linknames to be incorrect"); |
879 | 369 | err = err_combine(err, ARCHIVE_WARN); |
880 | 369 | } |
881 | 379 | seen_headers |= seen_K_header; |
882 | 379 | a->archive.archive_format = ARCHIVE_FORMAT_TAR_GNUTAR; |
883 | 379 | a->archive.archive_format_name = "GNU tar format"; |
884 | 379 | err2 = header_gnu_longlink(a, tar, entry, h, unconsumed); |
885 | 379 | break; |
886 | 146 | case 'L': /* Long filename (GNU tar, others) */ |
887 | 146 | if (seen_headers & seen_L_header) { |
888 | 143 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
889 | 143 | "Damaged archive: Redundant 'L' headers may cause filenames to be incorrect"); |
890 | 143 | err = err_combine(err, ARCHIVE_WARN); |
891 | 143 | } |
892 | 146 | seen_headers |= seen_L_header; |
893 | 146 | a->archive.archive_format = ARCHIVE_FORMAT_TAR_GNUTAR; |
894 | 146 | a->archive.archive_format_name = "GNU tar format"; |
895 | 146 | err2 = header_gnu_longname(a, tar, entry, h, unconsumed); |
896 | 146 | break; |
897 | 0 | case 'V': /* GNU volume header */ |
898 | 0 | if (seen_headers & seen_V_header) { |
899 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
900 | 0 | "Redundant 'V' header"); |
901 | 0 | err = err_combine(err, ARCHIVE_WARN); |
902 | 0 | } |
903 | 0 | seen_headers |= seen_V_header; |
904 | 0 | a->archive.archive_format = ARCHIVE_FORMAT_TAR_GNUTAR; |
905 | 0 | a->archive.archive_format_name = "GNU tar format"; |
906 | 0 | err2 = header_volume(a, tar, entry, h, unconsumed); |
907 | 0 | break; |
908 | 0 | case 'X': /* Used by SUN tar; same as 'x'. */ |
909 | 0 | if (seen_headers & seen_x_header) { |
910 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
911 | 0 | "Redundant 'X'/'x' header"); |
912 | 0 | return (ARCHIVE_FATAL); |
913 | 0 | } |
914 | 0 | seen_headers |= seen_x_header; |
915 | 0 | a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE; |
916 | 0 | a->archive.archive_format_name = |
917 | 0 | "POSIX pax interchange format (Sun variant)"; |
918 | 0 | err2 = header_pax_extension(a, tar, entry, h, unconsumed); |
919 | 0 | break; |
920 | 146 | case 'x': /* POSIX-standard 'x' header. */ |
921 | 146 | if (seen_headers & seen_x_header) { |
922 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
923 | 0 | "Redundant 'x' header"); |
924 | 0 | return (ARCHIVE_FATAL); |
925 | 0 | } |
926 | 146 | seen_headers |= seen_x_header; |
927 | 146 | a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE; |
928 | 146 | a->archive.archive_format_name = "POSIX pax interchange format"; |
929 | 146 | err2 = header_pax_extension(a, tar, entry, h, unconsumed); |
930 | 146 | break; |
931 | 21.2k | default: /* Regular header: Legacy tar, GNU tar, or ustar */ |
932 | 21.2k | gnuheader = (const struct archive_entry_header_gnutar *)h; |
933 | 21.2k | if (memcmp(gnuheader->magic, "ustar \0", 8) == 0) { |
934 | 1.84k | a->archive.archive_format = ARCHIVE_FORMAT_TAR_GNUTAR; |
935 | 1.84k | a->archive.archive_format_name = "GNU tar format"; |
936 | 1.84k | err2 = header_gnutar(a, tar, entry, h, unconsumed); |
937 | 19.3k | } else if (memcmp(header->magic, "ustar", 5) == 0) { |
938 | 6.16k | if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE) { |
939 | 5.92k | a->archive.archive_format = ARCHIVE_FORMAT_TAR_USTAR; |
940 | 5.92k | a->archive.archive_format_name = "POSIX ustar format"; |
941 | 5.92k | } |
942 | 6.16k | err2 = header_ustar(a, tar, entry, h); |
943 | 13.2k | } else { |
944 | 13.2k | a->archive.archive_format = ARCHIVE_FORMAT_TAR; |
945 | 13.2k | a->archive.archive_format_name = "tar (non-POSIX)"; |
946 | 13.2k | err2 = header_old_tar(a, tar, entry, h); |
947 | 13.2k | } |
948 | 21.2k | err = err_combine(err, err2); |
949 | | /* We return warnings or success as-is. Anything else is fatal. */ |
950 | 21.2k | if (err < ARCHIVE_WARN) { |
951 | 19 | return (ARCHIVE_FATAL); |
952 | 19 | } |
953 | | /* Filename of the form `._filename` is an AppleDouble |
954 | | * extension entry. The body is the macOS metadata blob; |
955 | | * this is followed by another entry with the actual |
956 | | * regular file data. |
957 | | * This design has two drawbacks: |
958 | | * = it's brittle; you might just have a file with such a name |
959 | | * = it duplicates any long pathname extensions |
960 | | * |
961 | | * TODO: This probably shouldn't be here at all. Consider |
962 | | * just returning the contents as a regular entry here and |
963 | | * then dealing with it when we write data to disk. |
964 | | */ |
965 | 21.2k | if (tar->process_mac_extensions |
966 | 21.2k | && ((seen_headers & seen_mac_metadata) == 0) |
967 | 21.2k | && is_mac_metadata_entry(entry)) { |
968 | 0 | err2 = read_mac_metadata_blob(a, entry, unconsumed); |
969 | 0 | if (err2 < ARCHIVE_WARN) { |
970 | 0 | return (ARCHIVE_FATAL); |
971 | 0 | } |
972 | 0 | err = err_combine(err, err2); |
973 | | /* Note: Other headers can appear again. */ |
974 | 0 | seen_headers = seen_mac_metadata; |
975 | 0 | tar_reset_header_state(tar); |
976 | 0 | break; |
977 | 0 | } |
978 | | |
979 | | /* Reconcile GNU sparse attributes */ |
980 | 21.2k | if (tar->sparse_gnu_attributes_seen) { |
981 | | /* Only 'S' (GNU sparse) and ustar '0' regular files can be sparse */ |
982 | 25 | if (tar->filetype != 'S' && tar->filetype != '0') { |
983 | 14 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
984 | 14 | "Non-regular file cannot be sparse"); |
985 | 14 | return (ARCHIVE_WARN); |
986 | 14 | } else if (tar->sparse_gnu_major == 0 && |
987 | 8 | tar->sparse_gnu_minor == 0) { |
988 | | /* Sparse map already parsed from 'x' header */ |
989 | 6 | } else if (tar->sparse_gnu_major == 0 && |
990 | 3 | tar->sparse_gnu_minor == 1) { |
991 | | /* Sparse map already parsed from 'x' header */ |
992 | 6 | } else if (tar->sparse_gnu_major == 1 && |
993 | 3 | tar->sparse_gnu_minor == 0) { |
994 | | /* Sparse map is prepended to file contents */ |
995 | 3 | ssize_t bytes_read; |
996 | 3 | bytes_read = gnu_sparse_10_read(a, tar, unconsumed); |
997 | 3 | if (bytes_read < 0) |
998 | 3 | return ((int)bytes_read); |
999 | 0 | tar->entry_bytes_remaining -= bytes_read; |
1000 | 3 | } else { |
1001 | 3 | archive_set_error(&a->archive, |
1002 | 3 | ARCHIVE_ERRNO_MISC, |
1003 | 3 | "Unrecognized GNU sparse file format"); |
1004 | 3 | return (ARCHIVE_WARN); |
1005 | 3 | } |
1006 | 25 | } |
1007 | 21.2k | return (err); |
1008 | 23.2k | } |
1009 | | |
1010 | | /* We're between headers ... */ |
1011 | 2.04k | err = err_combine(err, err2); |
1012 | 2.04k | if (err == ARCHIVE_FATAL) |
1013 | 9 | return (err); |
1014 | | |
1015 | | /* The GNU volume header and the pax `g` global header |
1016 | | * are both allowed to be the only header in an |
1017 | | * archive. If we've seen any other header, a |
1018 | | * following EOF is fatal. */ |
1019 | 2.03k | if ((seen_headers & ~seen_V_header & ~seen_g_header) != 0) { |
1020 | 784 | eof_fatal = 1; |
1021 | 784 | } |
1022 | 2.03k | } |
1023 | 233k | } |
1024 | | |
1025 | | /* |
1026 | | * Return true if block checksum is correct. |
1027 | | */ |
1028 | | static int |
1029 | | checksum(struct archive_read *a, const void *h) |
1030 | 243k | { |
1031 | 243k | const unsigned char *bytes; |
1032 | 243k | const struct archive_entry_header_ustar *header; |
1033 | 243k | int check, sum; |
1034 | 243k | size_t i; |
1035 | | |
1036 | 243k | (void)a; /* UNUSED */ |
1037 | 243k | bytes = (const unsigned char *)h; |
1038 | 243k | header = (const struct archive_entry_header_ustar *)h; |
1039 | | |
1040 | | /* Checksum field must hold an octal number */ |
1041 | 533k | for (i = 0; i < sizeof(header->checksum); ++i) { |
1042 | 504k | char c = header->checksum[i]; |
1043 | 504k | if (c != ' ' && c != '\0' && (c < '0' || c > '7')) |
1044 | 213k | return 0; |
1045 | 504k | } |
1046 | | |
1047 | | /* |
1048 | | * Test the checksum. Note that POSIX specifies _unsigned_ |
1049 | | * bytes for this calculation. |
1050 | | */ |
1051 | 29.0k | sum = (int)tar_atol(header->checksum, sizeof(header->checksum)); |
1052 | 29.0k | check = 0; |
1053 | 4.33M | for (i = 0; i < 148; i++) |
1054 | 4.30M | check += (unsigned char)bytes[i]; |
1055 | 261k | for (; i < 156; i++) |
1056 | 232k | check += 32; |
1057 | 10.3M | for (; i < 512; i++) |
1058 | 10.3M | check += (unsigned char)bytes[i]; |
1059 | 29.0k | if (sum == check) |
1060 | 0 | return (1); |
1061 | | |
1062 | | /* |
1063 | | * Repeat test with _signed_ bytes, just in case this archive |
1064 | | * was created by an old BSD, Solaris, or HP-UX tar with a |
1065 | | * broken checksum calculation. |
1066 | | */ |
1067 | 29.0k | check = 0; |
1068 | 4.33M | for (i = 0; i < 148; i++) |
1069 | 4.30M | check += (signed char)bytes[i]; |
1070 | 261k | for (; i < 156; i++) |
1071 | 232k | check += 32; |
1072 | 10.3M | for (; i < 512; i++) |
1073 | 10.3M | check += (signed char)bytes[i]; |
1074 | 29.0k | if (sum == check) |
1075 | 0 | return (1); |
1076 | | |
1077 | 29.0k | #if DONT_FAIL_ON_CRC_ERROR |
1078 | | /* Speed up fuzzing by pretending the checksum is always right. */ |
1079 | 29.0k | return (1); |
1080 | | #else |
1081 | | return (0); |
1082 | | #endif |
1083 | 29.0k | } |
1084 | | |
1085 | | /* |
1086 | | * Return true if this block contains only nulls. |
1087 | | */ |
1088 | | static int |
1089 | | archive_block_is_null(const char *p) |
1090 | 55.6k | { |
1091 | 55.6k | unsigned i; |
1092 | | |
1093 | 17.5M | for (i = 0; i < 512; i++) |
1094 | 17.4M | if (*p++) |
1095 | 22.5k | return (0); |
1096 | 33.1k | return (1); |
1097 | 55.6k | } |
1098 | | |
1099 | | /* |
1100 | | * Interpret 'A' Solaris ACL header |
1101 | | */ |
1102 | | static int |
1103 | | header_Solaris_ACL(struct archive_read *a, struct tar *tar, |
1104 | | struct archive_entry *entry, const void *h, int64_t *unconsumed) |
1105 | 122 | { |
1106 | 122 | const struct archive_entry_header_ustar *header; |
1107 | 122 | struct archive_string acl_text; |
1108 | 122 | size_t size; |
1109 | 122 | int err, acl_type; |
1110 | 122 | uint64_t type; |
1111 | 122 | char *acl, *p; |
1112 | | |
1113 | 122 | header = (const struct archive_entry_header_ustar *)h; |
1114 | 122 | size = (size_t)tar_atol(header->size, sizeof(header->size)); |
1115 | 122 | archive_string_init(&acl_text); |
1116 | 122 | err = read_body_to_string(a, tar, &acl_text, h, unconsumed); |
1117 | 122 | if (err != ARCHIVE_OK) { |
1118 | 6 | archive_string_free(&acl_text); |
1119 | 6 | return (err); |
1120 | 6 | } |
1121 | | |
1122 | | /* TODO: Examine the first characters to see if this |
1123 | | * is an AIX ACL descriptor. We'll likely never support |
1124 | | * them, but it would be polite to recognize and warn when |
1125 | | * we do see them. */ |
1126 | | |
1127 | | /* Leading octal number indicates ACL type and number of entries. */ |
1128 | 116 | p = acl = acl_text.s; |
1129 | 116 | type = 0; |
1130 | 928 | while (*p != '\0' && p < acl + size) { |
1131 | 812 | if (*p < '0' || *p > '7') { |
1132 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1133 | 0 | "Malformed Solaris ACL attribute (invalid digit)"); |
1134 | 0 | archive_string_free(&acl_text); |
1135 | 0 | return(ARCHIVE_WARN); |
1136 | 0 | } |
1137 | 812 | type <<= 3; |
1138 | 812 | type += *p - '0'; |
1139 | 812 | if (type > 077777777) { |
1140 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1141 | 0 | "Malformed Solaris ACL attribute (count too large)"); |
1142 | 0 | archive_string_free(&acl_text); |
1143 | 0 | return (ARCHIVE_WARN); |
1144 | 0 | } |
1145 | 812 | p++; |
1146 | 812 | } |
1147 | 116 | switch (type & ~0777777) { |
1148 | 89 | case 01000000: |
1149 | | /* POSIX.1e ACL */ |
1150 | 89 | acl_type = ARCHIVE_ENTRY_ACL_TYPE_ACCESS; |
1151 | 89 | break; |
1152 | 27 | case 03000000: |
1153 | | /* NFSv4 ACL */ |
1154 | 27 | acl_type = ARCHIVE_ENTRY_ACL_TYPE_NFS4; |
1155 | 27 | break; |
1156 | 0 | default: |
1157 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1158 | 0 | "Malformed Solaris ACL attribute (unsupported type %llu)", |
1159 | 0 | (unsigned long long)type); |
1160 | 0 | archive_string_free(&acl_text); |
1161 | 0 | return (ARCHIVE_WARN); |
1162 | 116 | } |
1163 | 116 | p++; |
1164 | | |
1165 | 116 | if (p >= acl + size) { |
1166 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1167 | 0 | "Malformed Solaris ACL attribute (body overflow)"); |
1168 | 0 | archive_string_free(&acl_text); |
1169 | 0 | return(ARCHIVE_WARN); |
1170 | 0 | } |
1171 | | |
1172 | | /* ACL text is null-terminated; find the end. */ |
1173 | 116 | size -= (p - acl); |
1174 | 116 | acl = p; |
1175 | | |
1176 | 251k | while (*p != '\0' && p < acl + size) |
1177 | 251k | p++; |
1178 | | |
1179 | 116 | if (tar->sconv_acl == NULL) { |
1180 | 83 | tar->sconv_acl = archive_string_conversion_from_charset( |
1181 | 83 | &(a->archive), "UTF-8", 1); |
1182 | 83 | if (tar->sconv_acl == NULL) { |
1183 | 0 | archive_string_free(&acl_text); |
1184 | 0 | return (ARCHIVE_FATAL); |
1185 | 0 | } |
1186 | 83 | } |
1187 | 116 | archive_strncpy(&(tar->localname), acl, p - acl); |
1188 | 116 | err = archive_acl_from_text_l(archive_entry_acl(entry), |
1189 | 116 | tar->localname.s, acl_type, tar->sconv_acl); |
1190 | | /* Workaround: Force perm_is_set() to be correct */ |
1191 | | /* If this bit were stored in the ACL, this wouldn't be needed */ |
1192 | 116 | archive_entry_set_perm(entry, archive_entry_perm(entry)); |
1193 | 116 | if (err != ARCHIVE_OK) { |
1194 | 116 | if (errno == ENOMEM) { |
1195 | 0 | archive_set_error(&a->archive, ENOMEM, |
1196 | 0 | "Can't allocate memory for ACL"); |
1197 | 0 | } else |
1198 | 116 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1199 | 116 | "Malformed Solaris ACL attribute (unparsable)"); |
1200 | 116 | } |
1201 | 116 | archive_string_free(&acl_text); |
1202 | 116 | return (err); |
1203 | 116 | } |
1204 | | |
1205 | | /* |
1206 | | * Interpret 'K' long linkname header. |
1207 | | */ |
1208 | | static int |
1209 | | header_gnu_longlink(struct archive_read *a, struct tar *tar, |
1210 | | struct archive_entry *entry, const void *h, int64_t *unconsumed) |
1211 | 379 | { |
1212 | 379 | int err; |
1213 | | |
1214 | 379 | struct archive_string linkpath; |
1215 | 379 | archive_string_init(&linkpath); |
1216 | 379 | err = read_body_to_string(a, tar, &linkpath, h, unconsumed); |
1217 | 379 | if (err == ARCHIVE_OK) { |
1218 | 379 | archive_entry_set_link(entry, linkpath.s); |
1219 | 379 | } |
1220 | 379 | archive_string_free(&linkpath); |
1221 | 379 | return (err); |
1222 | 379 | } |
1223 | | |
1224 | | static int |
1225 | | set_conversion_failed_error(struct archive_read *a, |
1226 | | struct archive_string_conv *sconv, const char *name) |
1227 | 0 | { |
1228 | 0 | if (errno == ENOMEM) { |
1229 | 0 | archive_set_error(&a->archive, ENOMEM, |
1230 | 0 | "Can't allocate memory for %s", name); |
1231 | 0 | return (ARCHIVE_FATAL); |
1232 | 0 | } |
1233 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, |
1234 | 0 | "%s can't be converted from %s to current locale", |
1235 | 0 | name, archive_string_conversion_charset_name(sconv)); |
1236 | 0 | return (ARCHIVE_WARN); |
1237 | 0 | } |
1238 | | |
1239 | | /* |
1240 | | * Interpret 'L' long filename header. |
1241 | | */ |
1242 | | static int |
1243 | | header_gnu_longname(struct archive_read *a, struct tar *tar, |
1244 | | struct archive_entry *entry, const void *h, int64_t *unconsumed) |
1245 | 146 | { |
1246 | 146 | int err; |
1247 | 146 | struct archive_string longname; |
1248 | | |
1249 | 146 | archive_string_init(&longname); |
1250 | 146 | err = read_body_to_string(a, tar, &longname, h, unconsumed); |
1251 | 146 | if (err == ARCHIVE_OK) { |
1252 | 146 | if (archive_entry_copy_pathname_l(entry, longname.s, |
1253 | 146 | archive_strlen(&longname), tar->sconv) != 0) |
1254 | 0 | err = set_conversion_failed_error(a, tar->sconv, "Pathname"); |
1255 | 146 | } |
1256 | 146 | archive_string_free(&longname); |
1257 | 146 | return (err); |
1258 | 146 | } |
1259 | | |
1260 | | /* |
1261 | | * Interpret 'V' GNU tar volume header. |
1262 | | */ |
1263 | | static int |
1264 | | header_volume(struct archive_read *a, struct tar *tar, |
1265 | | struct archive_entry *entry, const void *h, int64_t *unconsumed) |
1266 | 0 | { |
1267 | 0 | const struct archive_entry_header_ustar *header; |
1268 | 0 | int64_t size, to_consume; |
1269 | |
|
1270 | 0 | (void)a; /* UNUSED */ |
1271 | 0 | (void)tar; /* UNUSED */ |
1272 | 0 | (void)entry; /* UNUSED */ |
1273 | |
|
1274 | 0 | header = (const struct archive_entry_header_ustar *)h; |
1275 | 0 | size = tar_atol(header->size, sizeof(header->size)); |
1276 | 0 | if (size < 0 || size > (int64_t)pathname_limit) { |
1277 | 0 | return (ARCHIVE_FATAL); |
1278 | 0 | } |
1279 | 0 | to_consume = ((size + 511) & ~511); |
1280 | 0 | *unconsumed += to_consume; |
1281 | 0 | return (ARCHIVE_OK); |
1282 | 0 | } |
1283 | | |
1284 | | /* |
1285 | | * Read the next `size` bytes into the provided string. |
1286 | | * Null-terminate the string. |
1287 | | */ |
1288 | | static int |
1289 | | read_bytes_to_string(struct archive_read *a, |
1290 | | struct archive_string *as, size_t size, |
1291 | 808 | int64_t *unconsumed) { |
1292 | 808 | const void *src; |
1293 | | |
1294 | | /* Fail if we can't make our buffer big enough. */ |
1295 | 808 | if (archive_string_ensure(as, size + 1) == NULL) { |
1296 | 0 | archive_set_error(&a->archive, ENOMEM, |
1297 | 0 | "No memory"); |
1298 | 0 | return (ARCHIVE_FATAL); |
1299 | 0 | } |
1300 | | |
1301 | 808 | if (tar_flush_unconsumed(a, unconsumed) != ARCHIVE_OK) { |
1302 | 0 | return (ARCHIVE_FATAL); |
1303 | 0 | } |
1304 | | |
1305 | | /* Read the body into the string. */ |
1306 | 808 | src = __archive_read_ahead(a, size, NULL); |
1307 | 808 | if (src == NULL) { |
1308 | 1 | archive_set_error(&a->archive, EINVAL, |
1309 | 1 | "Truncated archive" |
1310 | 1 | " detected while reading metadata"); |
1311 | 1 | *unconsumed = 0; |
1312 | 1 | return (ARCHIVE_FATAL); |
1313 | 1 | } |
1314 | 807 | memcpy(as->s, src, size); |
1315 | 807 | as->s[size] = '\0'; |
1316 | 807 | as->length = size; |
1317 | 807 | *unconsumed += size; |
1318 | 807 | return (ARCHIVE_OK); |
1319 | 808 | } |
1320 | | |
1321 | | /* |
1322 | | * Read body of an archive entry into an archive_string object. |
1323 | | */ |
1324 | | static int |
1325 | | read_body_to_string(struct archive_read *a, struct tar *tar, |
1326 | | struct archive_string *as, const void *h, int64_t *unconsumed) |
1327 | 647 | { |
1328 | 647 | int64_t size; |
1329 | 647 | const struct archive_entry_header_ustar *header; |
1330 | 647 | int r; |
1331 | | |
1332 | 647 | (void)tar; /* UNUSED */ |
1333 | 647 | header = (const struct archive_entry_header_ustar *)h; |
1334 | 647 | size = tar_atol(header->size, sizeof(header->size)); |
1335 | 647 | if (size < 0 || size > entry_limit) { |
1336 | 1 | archive_set_error(&a->archive, EINVAL, |
1337 | 1 | "Special header has invalid size: %lld", |
1338 | 1 | (long long)size); |
1339 | 1 | return (ARCHIVE_FATAL); |
1340 | 1 | } |
1341 | 646 | if (size > (int64_t)pathname_limit) { |
1342 | 4 | archive_string_empty(as); |
1343 | 4 | int64_t to_consume = ((size + 511) & ~511); |
1344 | 4 | if (to_consume != __archive_read_consume(a, to_consume)) { |
1345 | 1 | return (ARCHIVE_FATAL); |
1346 | 1 | } |
1347 | 3 | archive_set_error(&a->archive, EINVAL, |
1348 | 3 | "Special header too large: %lld > 1MiB", |
1349 | 3 | (long long)size); |
1350 | 3 | return (ARCHIVE_WARN); |
1351 | 4 | } |
1352 | 642 | r = read_bytes_to_string(a, as, size, unconsumed); |
1353 | 642 | *unconsumed += 0x1ff & (-size); |
1354 | 642 | return(r); |
1355 | 646 | } |
1356 | | |
1357 | | /* |
1358 | | * Parse out common header elements. |
1359 | | * |
1360 | | * This would be the same as header_old_tar, except that the |
1361 | | * filename is handled slightly differently for old and POSIX |
1362 | | * entries (POSIX entries support a 'prefix'). This factoring |
1363 | | * allows header_old_tar and header_ustar |
1364 | | * to handle filenames differently, while still putting most of the |
1365 | | * common parsing into one place. |
1366 | | * |
1367 | | * This is called _after_ ustar, GNU tar, Schily, etc, special |
1368 | | * fields have already been parsed into the `tar` structure. |
1369 | | * So we can make final decisions here about how to reconcile |
1370 | | * size, mode, etc, information. |
1371 | | */ |
1372 | | static int |
1373 | | header_common(struct archive_read *a, struct tar *tar, |
1374 | | struct archive_entry *entry, const void *h) |
1375 | 21.2k | { |
1376 | 21.2k | const struct archive_entry_header_ustar *header; |
1377 | 21.2k | const char *existing_linkpath; |
1378 | 21.2k | const wchar_t *existing_wcs_linkpath; |
1379 | 21.2k | int err = ARCHIVE_OK; |
1380 | | |
1381 | 21.2k | header = (const struct archive_entry_header_ustar *)h; |
1382 | | |
1383 | | /* Parse out the numeric fields (all are octal) */ |
1384 | | |
1385 | | /* Split mode handling: Set filetype always, perm only if not already set */ |
1386 | 21.2k | archive_entry_set_filetype(entry, |
1387 | 21.2k | (mode_t)tar_atol(header->mode, sizeof(header->mode))); |
1388 | 21.2k | if (!archive_entry_perm_is_set(entry)) { |
1389 | 21.1k | archive_entry_set_perm(entry, |
1390 | 21.1k | (mode_t)tar_atol(header->mode, sizeof(header->mode))); |
1391 | 21.1k | } |
1392 | | |
1393 | | /* Set uid, gid, mtime if not already set */ |
1394 | 21.2k | if (!archive_entry_uid_is_set(entry)) { |
1395 | 21.2k | archive_entry_set_uid(entry, tar_atol(header->uid, sizeof(header->uid))); |
1396 | 21.2k | } |
1397 | 21.2k | if (!archive_entry_gid_is_set(entry)) { |
1398 | 21.2k | archive_entry_set_gid(entry, tar_atol(header->gid, sizeof(header->gid))); |
1399 | 21.2k | } |
1400 | 21.2k | if (!archive_entry_mtime_is_set(entry)) { |
1401 | 21.2k | int64_t t64 = tar_atol(header->mtime, sizeof(header->mtime)); |
1402 | 21.2k | time_t t = (time_t)t64; |
1403 | 21.2k | if ((int64_t)t != t64) { /* time_t overflowed */ |
1404 | 0 | t = get_time_t_max(); |
1405 | 0 | } |
1406 | 21.2k | archive_entry_set_mtime(entry, t, 0); |
1407 | 21.2k | } |
1408 | | |
1409 | | /* Reconcile the size info. */ |
1410 | | /* First, how big is the file on disk? */ |
1411 | 21.2k | if ((tar->size_fields & TAR_SIZE_GNU_SPARSE_REALSIZE) != 0) { |
1412 | | /* GNU sparse format 1.0 uses `GNU.sparse.realsize` |
1413 | | * to hold the size of the file on disk. */ |
1414 | 682 | tar->disk_size = tar->GNU_sparse_realsize; |
1415 | 20.5k | } else if ((tar->size_fields & TAR_SIZE_GNU_SPARSE_SIZE) != 0 |
1416 | 0 | && (tar->sparse_gnu_major == 0)) { |
1417 | | /* GNU sparse format 0.0 and 0.1 use `GNU.sparse.size` |
1418 | | * to hold the size of the file on disk. */ |
1419 | 0 | tar->disk_size = tar->GNU_sparse_size; |
1420 | 20.5k | } else if ((tar->size_fields & TAR_SIZE_SCHILY_SPARSE_REALSIZE) != 0) { |
1421 | 4 | tar->disk_size = tar->SCHILY_sparse_realsize; |
1422 | 20.5k | } else if ((tar->size_fields & TAR_SIZE_PAX_SIZE) != 0) { |
1423 | 0 | tar->disk_size = tar->pax_size; |
1424 | 20.5k | } else { |
1425 | | /* There wasn't a suitable pax header, so use the ustar info */ |
1426 | 20.5k | tar->disk_size = tar_atol(header->size, sizeof(header->size)); |
1427 | 20.5k | } |
1428 | | |
1429 | 21.2k | if (tar->disk_size < 0) { |
1430 | 11 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1431 | 11 | "Tar entry has negative file size"); |
1432 | 11 | return (ARCHIVE_FATAL); |
1433 | 21.2k | } else if (tar->disk_size > entry_limit) { |
1434 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1435 | 0 | "Tar entry size overflow"); |
1436 | 0 | return (ARCHIVE_FATAL); |
1437 | 21.2k | } else { |
1438 | 21.2k | archive_entry_set_size(entry, tar->disk_size); |
1439 | 21.2k | } |
1440 | | |
1441 | | /* Second, how big is the data in the archive? */ |
1442 | 21.2k | if ((tar->size_fields & TAR_SIZE_GNU_SPARSE_SIZE) != 0 |
1443 | 0 | && (tar->sparse_gnu_major == 1)) { |
1444 | | /* GNU sparse format 1.0 uses `GNU.sparse.size` |
1445 | | * to hold the size of the data in the archive. */ |
1446 | 0 | tar->entry_bytes_remaining = tar->GNU_sparse_size; |
1447 | 21.2k | } else if ((tar->size_fields & TAR_SIZE_PAX_SIZE) != 0) { |
1448 | 0 | tar->entry_bytes_remaining = tar->pax_size; |
1449 | 21.2k | } else { |
1450 | 21.2k | tar->entry_bytes_remaining |
1451 | 21.2k | = tar_atol(header->size, sizeof(header->size)); |
1452 | 21.2k | } |
1453 | 21.2k | if (tar->entry_bytes_remaining < 0) { |
1454 | 0 | tar->entry_bytes_remaining = 0; |
1455 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1456 | 0 | "Tar entry has negative size"); |
1457 | 0 | return (ARCHIVE_FATAL); |
1458 | 21.2k | } else if (tar->entry_bytes_remaining > entry_limit) { |
1459 | 0 | tar->entry_bytes_remaining = 0; |
1460 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1461 | 0 | "Tar entry size overflow"); |
1462 | 0 | return (ARCHIVE_FATAL); |
1463 | 0 | } |
1464 | | |
1465 | | /* Handle the tar type flag appropriately. */ |
1466 | 21.2k | tar->filetype = header->typeflag[0]; |
1467 | | |
1468 | | /* |
1469 | | * TODO: If the linkpath came from Pax extension header, then |
1470 | | * we should obey the hdrcharset_utf8 flag when converting these. |
1471 | | */ |
1472 | 21.2k | switch (tar->filetype) { |
1473 | 6.19k | case '1': /* Hard link */ |
1474 | 6.19k | archive_entry_set_link_to_hardlink(entry); |
1475 | 6.19k | existing_wcs_linkpath = archive_entry_hardlink_w(entry); |
1476 | 6.19k | existing_linkpath = archive_entry_hardlink(entry); |
1477 | 6.19k | if ((existing_linkpath == NULL || existing_linkpath[0] == '\0') |
1478 | 6.19k | && (existing_wcs_linkpath == NULL || existing_wcs_linkpath[0] == '\0')) { |
1479 | 6.19k | struct archive_string linkpath; |
1480 | 6.19k | archive_string_init(&linkpath); |
1481 | 6.19k | archive_strncpy(&linkpath, |
1482 | 6.19k | header->linkname, sizeof(header->linkname)); |
1483 | 6.19k | if (archive_entry_copy_hardlink_l(entry, linkpath.s, |
1484 | 6.19k | archive_strlen(&linkpath), tar->sconv) != 0) { |
1485 | 0 | err = set_conversion_failed_error(a, tar->sconv, |
1486 | 0 | "Linkname"); |
1487 | 0 | if (err == ARCHIVE_FATAL) { |
1488 | 0 | archive_string_free(&linkpath); |
1489 | 0 | return (err); |
1490 | 0 | } |
1491 | 0 | } |
1492 | 6.19k | archive_string_free(&linkpath); |
1493 | 6.19k | } |
1494 | | /* |
1495 | | * The following may seem odd, but: Technically, tar |
1496 | | * does not store the file type for a "hard link" |
1497 | | * entry, only the fact that it is a hard link. So, I |
1498 | | * leave the type zero normally. But, pax interchange |
1499 | | * format allows hard links to have data, which |
1500 | | * implies that the underlying entry is a regular |
1501 | | * file. |
1502 | | */ |
1503 | 6.19k | if (archive_entry_size(entry) > 0) |
1504 | 5.84k | archive_entry_set_filetype(entry, AE_IFREG); |
1505 | | |
1506 | | /* |
1507 | | * A tricky point: Traditionally, tar readers have |
1508 | | * ignored the size field when reading hardlink |
1509 | | * entries, and some writers put non-zero sizes even |
1510 | | * though the body is empty. POSIX blessed this |
1511 | | * convention in the 1988 standard, but broke with |
1512 | | * this tradition in 2001 by permitting hardlink |
1513 | | * entries to store valid bodies in pax interchange |
1514 | | * format, but not in ustar format. Since there is no |
1515 | | * hard and fast way to distinguish pax interchange |
1516 | | * from earlier archives (the 'x' and 'g' entries are |
1517 | | * optional, after all), we need a heuristic. |
1518 | | */ |
1519 | 6.19k | if (archive_entry_size(entry) == 0) { |
1520 | | /* If the size is already zero, we're done. */ |
1521 | 5.84k | } else if (a->archive.archive_format |
1522 | 5.84k | == ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE) { |
1523 | | /* Definitely pax extended; must obey hardlink size. */ |
1524 | 5.84k | } else if (a->archive.archive_format == ARCHIVE_FORMAT_TAR |
1525 | 5.52k | || a->archive.archive_format == ARCHIVE_FORMAT_TAR_GNUTAR) |
1526 | 675 | { |
1527 | | /* Old-style or GNU tar: we must ignore the size. */ |
1528 | 675 | archive_entry_set_size(entry, 0); |
1529 | 675 | tar->entry_bytes_remaining = 0; |
1530 | 5.16k | } else if (archive_read_format_tar_bid(a, 50) > 50) { |
1531 | | /* |
1532 | | * We don't know if it's pax: If the bid |
1533 | | * function sees a valid ustar header |
1534 | | * immediately following, then let's ignore |
1535 | | * the hardlink size. |
1536 | | */ |
1537 | 247 | archive_entry_set_size(entry, 0); |
1538 | 247 | tar->entry_bytes_remaining = 0; |
1539 | 247 | } |
1540 | | /* |
1541 | | * TODO: There are still two cases I'd like to handle: |
1542 | | * = a ustar non-pax archive with a hardlink entry at |
1543 | | * end-of-archive. (Look for block of nulls following?) |
1544 | | * = a pax archive that has not seen any pax headers |
1545 | | * and has an entry which is a hardlink entry storing |
1546 | | * a body containing an uncompressed tar archive. |
1547 | | * The first is worth addressing; I don't see any reliable |
1548 | | * way to deal with the second possibility. |
1549 | | */ |
1550 | 6.19k | break; |
1551 | 178 | case '2': /* Symlink */ |
1552 | 178 | archive_entry_set_link_to_symlink(entry); |
1553 | 178 | existing_wcs_linkpath = archive_entry_symlink_w(entry); |
1554 | 178 | existing_linkpath = archive_entry_symlink(entry); |
1555 | 178 | if ((existing_linkpath == NULL || existing_linkpath[0] == '\0') |
1556 | 178 | && (existing_wcs_linkpath == NULL || existing_wcs_linkpath[0] == '\0')) { |
1557 | 178 | struct archive_string linkpath; |
1558 | 178 | archive_string_init(&linkpath); |
1559 | 178 | archive_strncpy(&linkpath, |
1560 | 178 | header->linkname, sizeof(header->linkname)); |
1561 | 178 | if (archive_entry_copy_symlink_l(entry, linkpath.s, |
1562 | 178 | archive_strlen(&linkpath), tar->sconv) != 0) { |
1563 | 0 | err = set_conversion_failed_error(a, tar->sconv, |
1564 | 0 | "Linkname"); |
1565 | 0 | if (err == ARCHIVE_FATAL) { |
1566 | 0 | archive_string_free(&linkpath); |
1567 | 0 | return (err); |
1568 | 0 | } |
1569 | 0 | } |
1570 | 178 | archive_string_free(&linkpath); |
1571 | 178 | } |
1572 | 178 | archive_entry_set_filetype(entry, AE_IFLNK); |
1573 | 178 | archive_entry_set_size(entry, 0); |
1574 | 178 | tar->entry_bytes_remaining = 0; |
1575 | 178 | break; |
1576 | 270 | case '3': /* Character device */ |
1577 | 270 | archive_entry_set_filetype(entry, AE_IFCHR); |
1578 | 270 | archive_entry_set_size(entry, 0); |
1579 | 270 | tar->entry_bytes_remaining = 0; |
1580 | 270 | break; |
1581 | 494 | case '4': /* Block device */ |
1582 | 494 | archive_entry_set_filetype(entry, AE_IFBLK); |
1583 | 494 | archive_entry_set_size(entry, 0); |
1584 | 494 | tar->entry_bytes_remaining = 0; |
1585 | 494 | break; |
1586 | 239 | case '5': /* Dir */ |
1587 | 239 | archive_entry_set_filetype(entry, AE_IFDIR); |
1588 | 239 | archive_entry_set_size(entry, 0); |
1589 | 239 | tar->entry_bytes_remaining = 0; |
1590 | 239 | break; |
1591 | 289 | case '6': /* FIFO device */ |
1592 | 289 | archive_entry_set_filetype(entry, AE_IFIFO); |
1593 | 289 | archive_entry_set_size(entry, 0); |
1594 | 289 | tar->entry_bytes_remaining = 0; |
1595 | 289 | break; |
1596 | 6 | case 'D': /* GNU incremental directory type */ |
1597 | | /* |
1598 | | * No special handling is actually required here. |
1599 | | * It might be nice someday to preprocess the file list and |
1600 | | * provide it to the client, though. |
1601 | | */ |
1602 | 6 | archive_entry_set_filetype(entry, AE_IFDIR); |
1603 | 6 | break; |
1604 | 445 | case 'M': /* GNU "Multi-volume" (remainder of file from last archive)*/ |
1605 | | /* |
1606 | | * As far as I can tell, this is just like a regular file |
1607 | | * entry, except that the contents should be _appended_ to |
1608 | | * the indicated file at the indicated offset. This may |
1609 | | * require some API work to fully support. |
1610 | | */ |
1611 | 445 | break; |
1612 | 0 | case 'N': /* Old GNU "long filename" entry. */ |
1613 | | /* The body of this entry is a script for renaming |
1614 | | * previously-extracted entries. Ugh. It will never |
1615 | | * be supported by libarchive. */ |
1616 | 0 | archive_entry_set_filetype(entry, AE_IFREG); |
1617 | 0 | break; |
1618 | 0 | case 'S': /* GNU sparse files */ |
1619 | | /* |
1620 | | * Sparse files are really just regular files with |
1621 | | * sparse information in the extended area. |
1622 | | */ |
1623 | | /* FALLTHROUGH */ |
1624 | 1.10k | case '0': /* ustar "regular" file */ |
1625 | | /* FALLTHROUGH */ |
1626 | 13.1k | default: /* Non-standard file types */ |
1627 | | /* |
1628 | | * Per POSIX: non-recognized types should always be |
1629 | | * treated as regular files. |
1630 | | */ |
1631 | 13.1k | archive_entry_set_filetype(entry, AE_IFREG); |
1632 | 13.1k | break; |
1633 | 21.2k | } |
1634 | 21.2k | return (err); |
1635 | 21.2k | } |
1636 | | |
1637 | | /* |
1638 | | * Parse out header elements for "old-style" tar archives. |
1639 | | */ |
1640 | | static int |
1641 | | header_old_tar(struct archive_read *a, struct tar *tar, |
1642 | | struct archive_entry *entry, const void *h) |
1643 | 13.2k | { |
1644 | 13.2k | const struct archive_entry_header_ustar *header; |
1645 | 13.2k | int err = ARCHIVE_OK, err2; |
1646 | | |
1647 | | /* |
1648 | | * Copy filename over (to ensure null termination). |
1649 | | * Skip if pathname was already set e.g. by header_gnu_longname() |
1650 | | */ |
1651 | 13.2k | header = (const struct archive_entry_header_ustar *)h; |
1652 | | |
1653 | 13.2k | const char *existing_pathname = archive_entry_pathname(entry); |
1654 | 13.2k | const wchar_t *existing_wcs_pathname = archive_entry_pathname_w(entry); |
1655 | 13.2k | if ((existing_pathname == NULL || existing_pathname[0] == '\0') |
1656 | 13.2k | && (existing_wcs_pathname == NULL || existing_wcs_pathname[0] == '\0') && |
1657 | 13.2k | archive_entry_copy_pathname_l(entry, |
1658 | 13.2k | header->name, sizeof(header->name), tar->sconv) != 0) { |
1659 | 0 | err = set_conversion_failed_error(a, tar->sconv, "Pathname"); |
1660 | 0 | if (err == ARCHIVE_FATAL) |
1661 | 0 | return (err); |
1662 | 0 | } |
1663 | | |
1664 | | /* Grab rest of common fields */ |
1665 | 13.2k | err2 = header_common(a, tar, entry, h); |
1666 | 13.2k | if (err > err2) |
1667 | 11 | err = err2; |
1668 | | |
1669 | 13.2k | tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining); |
1670 | 13.2k | return (err); |
1671 | 13.2k | } |
1672 | | |
1673 | | /* |
1674 | | * Is this likely an AppleDouble extension? |
1675 | | */ |
1676 | | static int |
1677 | 21.2k | is_mac_metadata_entry(struct archive_entry *entry) { |
1678 | 21.2k | const char *p, *name; |
1679 | 21.2k | const wchar_t *wp, *wname; |
1680 | | |
1681 | 21.2k | wname = wp = archive_entry_pathname_w(entry); |
1682 | 21.2k | if (wp != NULL) { |
1683 | | /* Find the last path element. */ |
1684 | 918k | for (; *wp != L'\0'; ++wp) { |
1685 | 897k | if (wp[0] == '/' && wp[1] != L'\0') |
1686 | 365 | wname = wp + 1; |
1687 | 897k | } |
1688 | | /* |
1689 | | * If last path element starts with "._", then |
1690 | | * this is a Mac extension. |
1691 | | */ |
1692 | 20.5k | if (wname[0] == L'.' && wname[1] == L'_' && wname[2] != L'\0') |
1693 | 0 | return 1; |
1694 | 20.5k | } else { |
1695 | | /* Find the last path element. */ |
1696 | 660 | name = p = archive_entry_pathname(entry); |
1697 | 660 | if (p == NULL) |
1698 | 0 | return (ARCHIVE_FAILED); |
1699 | 52.8k | for (; *p != '\0'; ++p) { |
1700 | 52.1k | if (p[0] == '/' && p[1] != '\0') |
1701 | 45 | name = p + 1; |
1702 | 52.1k | } |
1703 | | /* |
1704 | | * If last path element starts with "._", then |
1705 | | * this is a Mac extension. |
1706 | | */ |
1707 | 660 | if (name[0] == '.' && name[1] == '_' && name[2] != '\0') |
1708 | 0 | return 1; |
1709 | 660 | } |
1710 | | /* Not a mac extension */ |
1711 | 21.2k | return 0; |
1712 | 21.2k | } |
1713 | | |
1714 | | /* |
1715 | | * Read a Mac AppleDouble-encoded blob of file metadata, |
1716 | | * if there is one. |
1717 | | * |
1718 | | * TODO: In Libarchive 4, we should consider ripping this |
1719 | | * out -- instead, return a file starting with `._` as |
1720 | | * a regular file and let the client (or archive_write logic) |
1721 | | * handle it. |
1722 | | */ |
1723 | | static int |
1724 | | read_mac_metadata_blob(struct archive_read *a, |
1725 | | struct archive_entry *entry, int64_t *unconsumed) |
1726 | 0 | { |
1727 | 0 | int64_t size; |
1728 | 0 | size_t msize; |
1729 | 0 | const void *data; |
1730 | | |
1731 | | /* Read the body as a Mac OS metadata blob. */ |
1732 | 0 | size = archive_entry_size(entry); |
1733 | 0 | msize = (size_t)size; |
1734 | 0 | if (size < 0 || (uintmax_t)msize != (uintmax_t)size) { |
1735 | 0 | *unconsumed = 0; |
1736 | 0 | return (ARCHIVE_FATAL); |
1737 | 0 | } |
1738 | | |
1739 | | /* TODO: Should this merely skip the overlarge entry and |
1740 | | * WARN? Or is xattr_limit sufficiently large that we can |
1741 | | * safely assume anything larger is malicious? */ |
1742 | 0 | if (size > (int64_t)xattr_limit) { |
1743 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1744 | 0 | "Oversized AppleDouble extension has size %llu > %llu", |
1745 | 0 | (unsigned long long)size, |
1746 | 0 | (unsigned long long)xattr_limit); |
1747 | 0 | return (ARCHIVE_FATAL); |
1748 | 0 | } |
1749 | | |
1750 | | /* |
1751 | | * TODO: Look beyond the body here to peek at the next header. |
1752 | | * If it's a regular header (not an extension header) |
1753 | | * that has the wrong name, just return the current |
1754 | | * entry as-is, without consuming the body here. |
1755 | | * That would reduce the risk of us mis-identifying |
1756 | | * an ordinary file that just happened to have |
1757 | | * a name starting with "._". |
1758 | | * |
1759 | | * Q: Is the above idea really possible? Even |
1760 | | * when there are GNU or pax extension entries? |
1761 | | */ |
1762 | 0 | if (tar_flush_unconsumed(a, unconsumed) != ARCHIVE_OK) { |
1763 | 0 | return (ARCHIVE_FATAL); |
1764 | 0 | } |
1765 | 0 | data = __archive_read_ahead(a, msize, NULL); |
1766 | 0 | if (data == NULL) { |
1767 | 0 | archive_set_error(&a->archive, EINVAL, |
1768 | 0 | "Truncated archive" |
1769 | 0 | " detected while reading macOS metadata"); |
1770 | 0 | *unconsumed = 0; |
1771 | 0 | return (ARCHIVE_FATAL); |
1772 | 0 | } |
1773 | 0 | archive_entry_clear(entry); |
1774 | 0 | archive_entry_copy_mac_metadata(entry, data, msize); |
1775 | 0 | *unconsumed = (msize + 511) & ~ 511; |
1776 | 0 | return (ARCHIVE_OK); |
1777 | 0 | } |
1778 | | |
1779 | | /* |
1780 | | * Parse a file header for a pax extended archive entry. |
1781 | | */ |
1782 | | static int |
1783 | | header_pax_global(struct archive_read *a, struct tar *tar, |
1784 | | struct archive_entry *entry, const void *h, int64_t *unconsumed) |
1785 | 1.24k | { |
1786 | 1.24k | const struct archive_entry_header_ustar *header; |
1787 | 1.24k | int64_t size, to_consume; |
1788 | | |
1789 | 1.24k | (void)a; /* UNUSED */ |
1790 | 1.24k | (void)tar; /* UNUSED */ |
1791 | 1.24k | (void)entry; /* UNUSED */ |
1792 | | |
1793 | 1.24k | header = (const struct archive_entry_header_ustar *)h; |
1794 | 1.24k | size = tar_atol(header->size, sizeof(header->size)); |
1795 | 1.24k | if (size < 0 || size > entry_limit) { |
1796 | 0 | archive_set_error(&a->archive, EINVAL, |
1797 | 0 | "Special header has invalid size: %lld", |
1798 | 0 | (long long)size); |
1799 | 0 | return (ARCHIVE_FATAL); |
1800 | 0 | } |
1801 | 1.24k | to_consume = ((size + 511) & ~511); |
1802 | 1.24k | *unconsumed += to_consume; |
1803 | 1.24k | return (ARCHIVE_OK); |
1804 | 1.24k | } |
1805 | | |
1806 | | /* |
1807 | | * Parse a file header for a Posix "ustar" archive entry. This also |
1808 | | * handles "pax" or "extended ustar" entries. |
1809 | | * |
1810 | | * In order to correctly handle pax attributes (which precede this), |
1811 | | * we have to skip parsing any field for which the entry already has |
1812 | | * contents. |
1813 | | */ |
1814 | | static int |
1815 | | header_ustar(struct archive_read *a, struct tar *tar, |
1816 | | struct archive_entry *entry, const void *h) |
1817 | 6.16k | { |
1818 | 6.16k | const struct archive_entry_header_ustar *header; |
1819 | 6.16k | int err = ARCHIVE_OK, r; |
1820 | | |
1821 | 6.16k | header = (const struct archive_entry_header_ustar *)h; |
1822 | | |
1823 | | /* |
1824 | | * The name field is fixed-width and may not be NUL-terminated. |
1825 | | * Use a temporary string only when prefix/name joining is required. |
1826 | | */ |
1827 | 6.16k | const char *existing_pathname = archive_entry_pathname(entry); |
1828 | 6.16k | const wchar_t *existing_wcs_pathname = archive_entry_pathname_w(entry); |
1829 | 6.16k | if ((existing_pathname == NULL || existing_pathname[0] == '\0') |
1830 | 6.16k | && (existing_wcs_pathname == NULL || existing_wcs_pathname[0] == '\0')) { |
1831 | 6.16k | struct archive_string as; |
1832 | 6.16k | const char *pathname; |
1833 | 6.16k | size_t pathname_length; |
1834 | | |
1835 | 6.16k | archive_string_init(&as); |
1836 | 6.16k | if (header->prefix[0]) { |
1837 | 5.71k | archive_strncpy(&as, header->prefix, sizeof(header->prefix)); |
1838 | 5.71k | if (as.s[archive_strlen(&as) - 1] != '/') |
1839 | 5.71k | archive_strappend_char(&as, '/'); |
1840 | 5.71k | archive_strncat(&as, header->name, sizeof(header->name)); |
1841 | 5.71k | pathname = as.s; |
1842 | 5.71k | pathname_length = archive_strlen(&as); |
1843 | 5.71k | } else { |
1844 | 451 | pathname = header->name; |
1845 | 451 | pathname_length = sizeof(header->name); |
1846 | 451 | } |
1847 | 6.16k | r = archive_entry_copy_pathname_l(entry, pathname, |
1848 | 6.16k | pathname_length, tar->sconv); |
1849 | 6.16k | archive_string_free(&as); |
1850 | 6.16k | if (r != 0) { |
1851 | 0 | err = set_conversion_failed_error(a, tar->sconv, "Pathname"); |
1852 | 0 | if (err == ARCHIVE_FATAL) |
1853 | 0 | return (err); |
1854 | 0 | } |
1855 | 6.16k | } |
1856 | | |
1857 | | /* Handle rest of common fields. */ |
1858 | 6.16k | r = header_common(a, tar, entry, h); |
1859 | 6.16k | if (r == ARCHIVE_FATAL) |
1860 | 0 | return (r); |
1861 | 6.16k | if (r < err) |
1862 | 0 | err = r; |
1863 | | |
1864 | | /* Handle POSIX ustar fields. */ |
1865 | 6.16k | const char *existing_uname = archive_entry_uname(entry); |
1866 | 6.16k | if (existing_uname == NULL || existing_uname[0] == '\0') { |
1867 | 6.16k | if (archive_entry_copy_uname_l(entry, |
1868 | 6.16k | header->uname, sizeof(header->uname), tar->sconv) != 0) { |
1869 | 0 | err = set_conversion_failed_error(a, tar->sconv, "Uname"); |
1870 | 0 | if (err == ARCHIVE_FATAL) |
1871 | 0 | return (err); |
1872 | 0 | } |
1873 | 6.16k | } |
1874 | | |
1875 | 6.16k | const char *existing_gname = archive_entry_gname(entry); |
1876 | 6.16k | if (existing_gname == NULL || existing_gname[0] == '\0') { |
1877 | 6.16k | if (archive_entry_copy_gname_l(entry, |
1878 | 6.16k | header->gname, sizeof(header->gname), tar->sconv) != 0) { |
1879 | 0 | err = set_conversion_failed_error(a, tar->sconv, "Gname"); |
1880 | 0 | if (err == ARCHIVE_FATAL) |
1881 | 0 | return (err); |
1882 | 0 | } |
1883 | 6.16k | } |
1884 | | |
1885 | | /* Parse out device numbers only for char and block specials. */ |
1886 | 6.16k | if (header->typeflag[0] == '3' || header->typeflag[0] == '4') { |
1887 | 298 | if (!archive_entry_rdev_is_set(entry)) { |
1888 | 298 | archive_entry_set_rdevmajor(entry, (dev_t) |
1889 | 298 | tar_atol(header->rdevmajor, sizeof(header->rdevmajor))); |
1890 | 298 | archive_entry_set_rdevminor(entry, (dev_t) |
1891 | 298 | tar_atol(header->rdevminor, sizeof(header->rdevminor))); |
1892 | 298 | } |
1893 | 5.86k | } else { |
1894 | 5.86k | archive_entry_set_rdev(entry, 0); |
1895 | 5.86k | } |
1896 | | |
1897 | 6.16k | tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining); |
1898 | | |
1899 | 6.16k | return (err); |
1900 | 6.16k | } |
1901 | | |
1902 | | static int |
1903 | | header_pax_extension(struct archive_read *a, struct tar *tar, |
1904 | | struct archive_entry *entry, const void *h, int64_t *unconsumed) |
1905 | 146 | { |
1906 | | /* Sanity checks: The largest `x` body I've ever heard of was |
1907 | | * a little over 4MB. So I doubt there has ever been a |
1908 | | * well-formed archive with an `x` body over 1GiB. Similarly, |
1909 | | * it seems plausible that no single attribute has ever been |
1910 | | * larger than 100MB. So if we see a larger value here, it's |
1911 | | * almost certainly a sign of a corrupted/malicious archive. */ |
1912 | | |
1913 | | /* Maximum sane size for extension body: 1 GiB */ |
1914 | | /* This cannot be raised to larger than 8GiB without |
1915 | | * exceeding the maximum size for a standard ustar |
1916 | | * entry. */ |
1917 | 146 | const int64_t ext_size_limit = 1024 * 1024 * (int64_t)1024; |
1918 | | /* Maximum size for a single line/attr: 100 million characters */ |
1919 | | /* This cannot be raised to more than 2GiB without exceeding |
1920 | | * a `size_t` on 32-bit platforms. */ |
1921 | 146 | const size_t max_parsed_line_length = 99999999ULL; |
1922 | | /* Largest attribute prolog: size + name. */ |
1923 | 146 | const size_t max_size_name = 512; |
1924 | | |
1925 | | /* Size and padding of the full extension body */ |
1926 | 146 | int64_t ext_size, ext_padding; |
1927 | 146 | size_t line_length, value_length, name_length; |
1928 | 146 | ssize_t to_read, did_read; |
1929 | 146 | const struct archive_entry_header_ustar *header; |
1930 | 146 | const char *p, *attr_start, *name_start; |
1931 | 146 | struct archive_string_conv *sconv; |
1932 | 146 | struct archive_string *pas = NULL; |
1933 | 146 | struct archive_string attr_name; |
1934 | 146 | int err = ARCHIVE_OK, r; |
1935 | | |
1936 | 146 | header = (const struct archive_entry_header_ustar *)h; |
1937 | 146 | ext_size = tar_atol(header->size, sizeof(header->size)); |
1938 | 146 | if (ext_size > entry_limit) { |
1939 | 0 | return (ARCHIVE_FATAL); |
1940 | 0 | } |
1941 | 146 | if (ext_size < 0) { |
1942 | 0 | archive_set_error(&a->archive, EINVAL, |
1943 | 0 | "pax extension header has invalid size: %lld", |
1944 | 0 | (long long)ext_size); |
1945 | 0 | return (ARCHIVE_FATAL); |
1946 | 0 | } |
1947 | | |
1948 | 146 | ext_padding = 0x1ff & (-ext_size); |
1949 | 146 | if (ext_size > ext_size_limit) { |
1950 | | /* Consume the pax extension body and return an error */ |
1951 | 0 | if (ext_size + ext_padding != __archive_read_consume(a, ext_size + ext_padding)) { |
1952 | 0 | return (ARCHIVE_FATAL); |
1953 | 0 | } |
1954 | 0 | archive_set_error(&a->archive, EINVAL, |
1955 | 0 | "Ignoring oversized pax extensions: %lld > %lld", |
1956 | 0 | (long long)ext_size, (long long)ext_size_limit); |
1957 | 0 | return (ARCHIVE_WARN); |
1958 | 0 | } |
1959 | 146 | if (tar_flush_unconsumed(a, unconsumed) != ARCHIVE_OK) { |
1960 | 0 | return (ARCHIVE_FATAL); |
1961 | 0 | } |
1962 | | |
1963 | | /* Parse the size/name of each pax attribute in the body */ |
1964 | 146 | archive_string_init(&attr_name); |
1965 | 1.12k | while (ext_size > 0) { |
1966 | | /* Read enough bytes to parse the size/name of the next attribute */ |
1967 | 1.12k | to_read = max_size_name; |
1968 | 1.12k | if (to_read > ext_size) { |
1969 | 198 | to_read = ext_size; |
1970 | 198 | } |
1971 | 1.12k | p = __archive_read_ahead(a, to_read, &did_read); |
1972 | 1.12k | if (p == NULL) { /* EOF */ |
1973 | 1 | archive_set_error(&a->archive, EINVAL, |
1974 | 1 | "Truncated tar archive" |
1975 | 1 | " detected while reading pax attribute name"); |
1976 | 1 | return (ARCHIVE_FATAL); |
1977 | 1 | } |
1978 | 1.12k | if (did_read > ext_size) { |
1979 | 255 | did_read = ext_size; |
1980 | 255 | } |
1981 | | |
1982 | | /* Parse size of attribute */ |
1983 | 1.12k | line_length = 0; |
1984 | 1.12k | attr_start = p; |
1985 | 3.78k | while (1) { |
1986 | 3.78k | if (p >= attr_start + did_read) { |
1987 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1988 | 0 | "Ignoring malformed pax attributes: overlarge attribute size field"); |
1989 | 0 | *unconsumed += ext_size + ext_padding; |
1990 | 0 | return (ARCHIVE_WARN); |
1991 | 0 | } |
1992 | 3.78k | if (*p == ' ') { |
1993 | 1.03k | p++; |
1994 | 1.03k | break; |
1995 | 1.03k | } |
1996 | 2.75k | if (*p < '0' || *p > '9') { |
1997 | 95 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1998 | 95 | "Ignoring malformed pax attributes: malformed attribute size field"); |
1999 | 95 | *unconsumed += ext_size + ext_padding; |
2000 | 95 | return (ARCHIVE_WARN); |
2001 | 95 | } |
2002 | 2.65k | line_length *= 10; |
2003 | 2.65k | line_length += *p - '0'; |
2004 | 2.65k | if (line_length > max_parsed_line_length) { |
2005 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
2006 | 0 | "Ignoring malformed pax attribute: size > %lld", |
2007 | 0 | (long long)max_parsed_line_length); |
2008 | 0 | *unconsumed += ext_size + ext_padding; |
2009 | 0 | return (ARCHIVE_WARN); |
2010 | 0 | } |
2011 | 2.65k | p++; |
2012 | 2.65k | } |
2013 | | |
2014 | 1.03k | if ((int64_t)line_length > ext_size) { |
2015 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
2016 | 0 | "Ignoring malformed pax attribute: %lld > %lld", |
2017 | 0 | (long long)line_length, (long long)ext_size); |
2018 | 0 | *unconsumed += ext_size + ext_padding; |
2019 | 0 | return (ARCHIVE_WARN); |
2020 | 0 | } |
2021 | | |
2022 | | /* Parse name of attribute */ |
2023 | 1.03k | if (p >= attr_start + did_read |
2024 | 1.03k | || p >= attr_start + line_length |
2025 | 1.03k | || *p == '=') { |
2026 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
2027 | 0 | "Ignoring malformed pax attributes: empty name found"); |
2028 | 0 | *unconsumed += ext_size + ext_padding; |
2029 | 0 | return (ARCHIVE_WARN); |
2030 | 0 | } |
2031 | 1.03k | name_start = p; |
2032 | 36.5k | while (1) { |
2033 | 36.5k | if (p >= attr_start + did_read || p >= attr_start + line_length) { |
2034 | 4 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
2035 | 4 | "Ignoring malformed pax attributes: overlarge attribute name"); |
2036 | 4 | *unconsumed += ext_size + ext_padding; |
2037 | 4 | return (ARCHIVE_WARN); |
2038 | 4 | } |
2039 | 36.5k | if (*p == '=') { |
2040 | 1.02k | break; |
2041 | 1.02k | } |
2042 | 35.5k | p++; |
2043 | 35.5k | } |
2044 | 1.02k | name_length = p - name_start; |
2045 | 1.02k | p++; // Skip '=' |
2046 | | |
2047 | | // Save the name before we consume it |
2048 | 1.02k | archive_strncpy(&attr_name, name_start, name_length); |
2049 | | |
2050 | 1.02k | ext_size -= p - attr_start; |
2051 | 1.02k | value_length = line_length - (p - attr_start); |
2052 | | |
2053 | | /* Consume size, name, and `=` */ |
2054 | 1.02k | *unconsumed += p - attr_start; |
2055 | 1.02k | if (tar_flush_unconsumed(a, unconsumed) != ARCHIVE_OK) { |
2056 | 0 | archive_string_free(&attr_name); |
2057 | 0 | return (ARCHIVE_FATAL); |
2058 | 0 | } |
2059 | | |
2060 | 1.02k | if (value_length == 0) { |
2061 | 1 | archive_set_error(&a->archive, EINVAL, |
2062 | 1 | "Malformed pax attributes"); |
2063 | 1 | *unconsumed += ext_size + ext_padding; |
2064 | 1 | archive_string_free(&attr_name); |
2065 | 1 | return (ARCHIVE_WARN); |
2066 | 1 | } |
2067 | | |
2068 | | /* pax_attribute will consume value_length - 1 */ |
2069 | 1.02k | r = pax_attribute(a, tar, entry, attr_name.s, archive_strlen(&attr_name), value_length - 1, unconsumed); |
2070 | 1.02k | ext_size -= value_length - 1; |
2071 | | |
2072 | | // Release the allocated attr_name (either here or before every return in this function) |
2073 | 1.02k | archive_string_free(&attr_name); |
2074 | | |
2075 | 1.02k | if (r < ARCHIVE_WARN) { |
2076 | 2 | *unconsumed += ext_size + ext_padding; |
2077 | 2 | return (r); |
2078 | 2 | } |
2079 | 1.02k | err = err_combine(err, r); |
2080 | | |
2081 | | /* Consume the `\n` that follows the pax attribute value. */ |
2082 | 1.02k | if (tar_flush_unconsumed(a, unconsumed) != ARCHIVE_OK) { |
2083 | 0 | return (ARCHIVE_FATAL); |
2084 | 0 | } |
2085 | 1.02k | p = __archive_read_ahead(a, 1, &did_read); |
2086 | 1.02k | if (p == NULL) { |
2087 | 3 | archive_set_error(&a->archive, EINVAL, |
2088 | 3 | "Truncated tar archive" |
2089 | 3 | " detected while completing pax attribute"); |
2090 | 3 | return (ARCHIVE_FATAL); |
2091 | 3 | } |
2092 | 1.02k | if (p[0] != '\n') { |
2093 | 40 | archive_set_error(&a->archive, EINVAL, |
2094 | 40 | "Malformed pax attributes"); |
2095 | 40 | *unconsumed += ext_size + ext_padding; |
2096 | 40 | return (ARCHIVE_WARN); |
2097 | 40 | } |
2098 | 981 | ext_size -= 1; |
2099 | 981 | *unconsumed += 1; |
2100 | 981 | if (tar_flush_unconsumed(a, unconsumed) != ARCHIVE_OK) { |
2101 | 0 | return (ARCHIVE_FATAL); |
2102 | 0 | } |
2103 | 981 | } |
2104 | 0 | *unconsumed += ext_size + ext_padding; |
2105 | | |
2106 | | /* |
2107 | | * Some PAX values -- pathname, linkpath, uname, gname -- |
2108 | | * can't be copied into the entry until we know the character |
2109 | | * set to use: |
2110 | | */ |
2111 | 0 | if (!tar->pax_hdrcharset_utf8) |
2112 | | /* PAX specified "BINARY", so use the default charset */ |
2113 | 0 | sconv = tar->opt_sconv; |
2114 | 0 | else { |
2115 | | /* PAX default UTF-8 */ |
2116 | 0 | sconv = archive_string_conversion_from_charset( |
2117 | 0 | &(a->archive), "UTF-8", 1); |
2118 | 0 | if (sconv == NULL) |
2119 | 0 | return (ARCHIVE_FATAL); |
2120 | 0 | if (tar->compat_2x) |
2121 | 0 | archive_string_conversion_set_opt(sconv, |
2122 | 0 | SCONV_SET_OPT_UTF8_LIBARCHIVE2X); |
2123 | 0 | } |
2124 | | |
2125 | | /* Pathname */ |
2126 | 0 | pas = NULL; |
2127 | 0 | if (archive_strlen(&(tar->entry_pathname_override)) > 0) { |
2128 | | /* Prefer GNU.sparse.name attribute if present */ |
2129 | | /* GNU sparse files store a fake name under the standard |
2130 | | * "pathname" key. */ |
2131 | 0 | pas = &(tar->entry_pathname_override); |
2132 | 0 | } else if (archive_strlen(&(tar->entry_pathname)) > 0) { |
2133 | | /* Use standard "pathname" PAX extension */ |
2134 | 0 | pas = &(tar->entry_pathname); |
2135 | 0 | } |
2136 | 0 | if (pas != NULL) { |
2137 | 0 | if (archive_entry_copy_pathname_l(entry, pas->s, |
2138 | 0 | archive_strlen(pas), sconv) != 0) { |
2139 | 0 | err = set_conversion_failed_error(a, sconv, "Pathname"); |
2140 | 0 | if (err == ARCHIVE_FATAL) |
2141 | 0 | return (err); |
2142 | | /* Use raw name without conversion */ |
2143 | 0 | archive_entry_copy_pathname(entry, pas->s); |
2144 | 0 | } |
2145 | 0 | } |
2146 | | /* Uname */ |
2147 | 0 | if (archive_strlen(&(tar->entry_uname)) > 0) { |
2148 | 0 | if (archive_entry_copy_uname_l(entry, tar->entry_uname.s, |
2149 | 0 | archive_strlen(&(tar->entry_uname)), sconv) != 0) { |
2150 | 0 | err = set_conversion_failed_error(a, sconv, "Uname"); |
2151 | 0 | if (err == ARCHIVE_FATAL) |
2152 | 0 | return (err); |
2153 | | /* Use raw name without conversion */ |
2154 | 0 | archive_entry_copy_uname(entry, tar->entry_uname.s); |
2155 | 0 | } |
2156 | 0 | } |
2157 | | /* Gname */ |
2158 | 0 | if (archive_strlen(&(tar->entry_gname)) > 0) { |
2159 | 0 | if (archive_entry_copy_gname_l(entry, tar->entry_gname.s, |
2160 | 0 | archive_strlen(&(tar->entry_gname)), sconv) != 0) { |
2161 | 0 | err = set_conversion_failed_error(a, sconv, "Gname"); |
2162 | 0 | if (err == ARCHIVE_FATAL) |
2163 | 0 | return (err); |
2164 | | /* Use raw name without conversion */ |
2165 | 0 | archive_entry_copy_gname(entry, tar->entry_gname.s); |
2166 | 0 | } |
2167 | 0 | } |
2168 | | /* Linkpath */ |
2169 | 0 | if (archive_strlen(&(tar->entry_linkpath)) > 0) { |
2170 | 0 | if (archive_entry_copy_link_l(entry, tar->entry_linkpath.s, |
2171 | 0 | archive_strlen(&(tar->entry_linkpath)), sconv) != 0) { |
2172 | 0 | err = set_conversion_failed_error(a, sconv, "Linkpath"); |
2173 | 0 | if (err == ARCHIVE_FATAL) |
2174 | 0 | return (err); |
2175 | | /* Use raw name without conversion */ |
2176 | 0 | archive_entry_copy_link(entry, tar->entry_linkpath.s); |
2177 | 0 | } |
2178 | 0 | } |
2179 | | |
2180 | | /* Extension may have given us a corrected `entry_bytes_remaining` for |
2181 | | * the main entry; update the padding appropriately. */ |
2182 | 0 | tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining); |
2183 | 0 | return (err); |
2184 | 0 | } |
2185 | | |
2186 | | static int |
2187 | | pax_attribute_LIBARCHIVE_xattr(struct archive_entry *entry, |
2188 | | const char *name, size_t name_length, const char *value, size_t value_length) |
2189 | 36 | { |
2190 | 36 | char *name_decoded; |
2191 | 36 | void *value_decoded; |
2192 | 36 | size_t value_len; |
2193 | | |
2194 | 36 | if (name_length < 1) |
2195 | 0 | return 3; |
2196 | | |
2197 | | /* URL-decode name */ |
2198 | 36 | name_decoded = url_decode(name, name_length); |
2199 | 36 | if (name_decoded == NULL) |
2200 | 0 | return 2; |
2201 | | |
2202 | | /* Base-64 decode value */ |
2203 | 36 | value_decoded = base64_decode(value, value_length, &value_len); |
2204 | 36 | if (value_decoded == NULL) { |
2205 | 0 | free(name_decoded); |
2206 | 0 | return 1; |
2207 | 0 | } |
2208 | | |
2209 | 36 | archive_entry_xattr_add_entry(entry, name_decoded, |
2210 | 36 | value_decoded, value_len); |
2211 | | |
2212 | 36 | free(name_decoded); |
2213 | 36 | free(value_decoded); |
2214 | 36 | return 0; |
2215 | 36 | } |
2216 | | |
2217 | | static int |
2218 | | pax_attribute_SCHILY_xattr(struct archive_entry *entry, |
2219 | | const char *name, size_t name_length, const char *value, size_t value_length) |
2220 | 0 | { |
2221 | 0 | if (name_length < 1 || name_length > 128) { |
2222 | 0 | return 1; |
2223 | 0 | } |
2224 | | |
2225 | 0 | char * null_terminated_name = malloc(name_length + 1); |
2226 | 0 | if (null_terminated_name != NULL) { |
2227 | 0 | memcpy(null_terminated_name, name, name_length); |
2228 | 0 | null_terminated_name[name_length] = '\0'; |
2229 | 0 | archive_entry_xattr_add_entry(entry, null_terminated_name, value, value_length); |
2230 | 0 | free(null_terminated_name); |
2231 | 0 | } |
2232 | |
|
2233 | 0 | return 0; |
2234 | 0 | } |
2235 | | |
2236 | | static int |
2237 | | pax_attribute_RHT_security_selinux(struct archive_entry *entry, |
2238 | | const char *value, size_t value_length) |
2239 | 0 | { |
2240 | 0 | archive_entry_xattr_add_entry(entry, "security.selinux", |
2241 | 0 | value, value_length); |
2242 | |
|
2243 | 0 | return 0; |
2244 | 0 | } |
2245 | | |
2246 | | static int |
2247 | | pax_attribute_SCHILY_acl(struct archive_read *a, struct tar *tar, |
2248 | | struct archive_entry *entry, size_t value_length, int type) |
2249 | 0 | { |
2250 | 0 | int r; |
2251 | 0 | const char *p; |
2252 | 0 | const char* errstr; |
2253 | |
|
2254 | 0 | switch (type) { |
2255 | 0 | case ARCHIVE_ENTRY_ACL_TYPE_ACCESS: |
2256 | 0 | errstr = "SCHILY.acl.access"; |
2257 | 0 | break; |
2258 | 0 | case ARCHIVE_ENTRY_ACL_TYPE_DEFAULT: |
2259 | 0 | errstr = "SCHILY.acl.default"; |
2260 | 0 | break; |
2261 | 0 | case ARCHIVE_ENTRY_ACL_TYPE_NFS4: |
2262 | 0 | errstr = "SCHILY.acl.ace"; |
2263 | 0 | break; |
2264 | 0 | default: |
2265 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
2266 | 0 | "Unknown ACL type: %d", type); |
2267 | 0 | return(ARCHIVE_FATAL); |
2268 | 0 | } |
2269 | | |
2270 | 0 | if (tar->sconv_acl == NULL) { |
2271 | 0 | tar->sconv_acl = |
2272 | 0 | archive_string_conversion_from_charset( |
2273 | 0 | &(a->archive), "UTF-8", 1); |
2274 | 0 | if (tar->sconv_acl == NULL) |
2275 | 0 | return (ARCHIVE_FATAL); |
2276 | 0 | } |
2277 | | |
2278 | 0 | if (value_length > acl_limit) { |
2279 | 0 | __archive_read_consume(a, value_length); |
2280 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
2281 | 0 | "Unreasonably large ACL: %llu > %llu", |
2282 | 0 | (unsigned long long)value_length, |
2283 | 0 | (unsigned long long)acl_limit); |
2284 | 0 | return (ARCHIVE_WARN); |
2285 | 0 | } |
2286 | | |
2287 | 0 | p = __archive_read_ahead(a, value_length, NULL); |
2288 | 0 | if (p == NULL) { |
2289 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, |
2290 | 0 | "Truncated tar archive " |
2291 | 0 | "detected while reading ACL data"); |
2292 | 0 | return (ARCHIVE_FATAL); |
2293 | 0 | } |
2294 | | |
2295 | 0 | r = archive_acl_from_text_nl(archive_entry_acl(entry), p, value_length, |
2296 | 0 | type, tar->sconv_acl); |
2297 | 0 | __archive_read_consume(a, value_length); |
2298 | | /* Workaround: Force perm_is_set() to be correct */ |
2299 | | /* If this bit were stored in the ACL, this wouldn't be needed */ |
2300 | 0 | archive_entry_set_perm(entry, archive_entry_perm(entry)); |
2301 | 0 | if (r != ARCHIVE_OK) { |
2302 | 0 | if (r == ARCHIVE_FATAL) { |
2303 | 0 | archive_set_error(&a->archive, ENOMEM, |
2304 | 0 | "%s %s", "Can't allocate memory for", |
2305 | 0 | errstr); |
2306 | 0 | return (r); |
2307 | 0 | } |
2308 | 0 | archive_set_error(&a->archive, |
2309 | 0 | ARCHIVE_ERRNO_MISC, "%s %s", "Parse error:", errstr); |
2310 | 0 | } |
2311 | 0 | return (r); |
2312 | 0 | } |
2313 | | |
2314 | | static int |
2315 | 84 | pax_attribute_read_time(struct archive_read *a, size_t value_length, __LA_TIME_T *ps, long *pn, int64_t *unconsumed) { |
2316 | 84 | struct archive_string as; |
2317 | 84 | int r; |
2318 | | |
2319 | 84 | if (value_length > 128) { |
2320 | 1 | __archive_read_consume(a, value_length); |
2321 | 1 | *ps = 0; |
2322 | 1 | *pn = 0; |
2323 | 1 | return (ARCHIVE_FATAL); |
2324 | 1 | } |
2325 | | |
2326 | 83 | archive_string_init(&as); |
2327 | 83 | r = read_bytes_to_string(a, &as, value_length, unconsumed); |
2328 | 83 | if (r < ARCHIVE_OK) { |
2329 | 0 | archive_string_free(&as); |
2330 | 0 | *ps = 0; |
2331 | 0 | *pn = 0; |
2332 | 0 | return (r); |
2333 | 0 | } |
2334 | | |
2335 | 83 | int64_t sec = 0; |
2336 | 83 | pax_time(as.s, archive_strlen(&as), &sec, pn); |
2337 | 83 | archive_string_free(&as); |
2338 | | |
2339 | 83 | if (sec == INT64_MIN) { |
2340 | 45 | *ps = 0; |
2341 | 45 | *pn = 0; |
2342 | 45 | return (ARCHIVE_WARN); |
2343 | 45 | } else { |
2344 | 38 | *ps = (__LA_TIME_T)sec; |
2345 | 38 | } |
2346 | 38 | return (ARCHIVE_OK); |
2347 | 83 | } |
2348 | | |
2349 | | static int |
2350 | 83 | pax_attribute_read_number(struct archive_read *a, size_t value_length, int64_t *result) { |
2351 | 83 | struct archive_string as; |
2352 | 83 | int64_t unconsumed = 0; |
2353 | 83 | int r; |
2354 | | |
2355 | 83 | if (value_length > 64) { |
2356 | 0 | __archive_read_consume(a, value_length); |
2357 | 0 | *result = 0; |
2358 | 0 | return (ARCHIVE_FATAL); |
2359 | 0 | } |
2360 | | |
2361 | 83 | archive_string_init(&as); |
2362 | 83 | r = read_bytes_to_string(a, &as, value_length, &unconsumed); |
2363 | 83 | if (tar_flush_unconsumed(a, &unconsumed) != ARCHIVE_OK) { |
2364 | 0 | *result = 0; |
2365 | 0 | return (ARCHIVE_FATAL); |
2366 | 0 | } |
2367 | 83 | if (r < ARCHIVE_OK) { |
2368 | 0 | archive_string_free(&as); |
2369 | 0 | *result = 0; |
2370 | 0 | return (r); |
2371 | 0 | } |
2372 | | |
2373 | 83 | *result = tar_atol10(as.s, archive_strlen(&as)); |
2374 | 83 | archive_string_free(&as); |
2375 | 83 | if (*result < 0 || *result == INT64_MAX) { |
2376 | 0 | *result = INT64_MAX; |
2377 | 0 | return (ARCHIVE_WARN); |
2378 | 0 | } |
2379 | 83 | return (ARCHIVE_OK); |
2380 | 83 | } |
2381 | | |
2382 | | /* |
2383 | | * Parse a single key=value attribute. |
2384 | | * |
2385 | | * POSIX reserves all-lowercase keywords. Vendor-specific extensions |
2386 | | * should always have keywords of the form "VENDOR.attribute" In |
2387 | | * particular, it's quite feasible to support many different vendor |
2388 | | * extensions here. I'm using "LIBARCHIVE" for extensions unique to |
2389 | | * this library. |
2390 | | * |
2391 | | * TODO: Investigate other vendor-specific extensions and see if |
2392 | | * any of them look useful. |
2393 | | */ |
2394 | | static int |
2395 | | pax_attribute(struct archive_read *a, struct tar *tar, struct archive_entry *entry, |
2396 | | const char *key, size_t key_length, size_t value_length, int64_t *unconsumed) |
2397 | 1.02k | { |
2398 | 1.02k | int64_t t; |
2399 | 1.02k | long n; |
2400 | 1.02k | const char *p; |
2401 | 1.02k | ssize_t bytes_read; |
2402 | 1.02k | int err = ARCHIVE_OK; |
2403 | | |
2404 | 1.02k | switch (key[0]) { |
2405 | 28 | case 'G': |
2406 | | /* GNU.* extensions */ |
2407 | 28 | if (key_length > 4 && memcmp(key, "GNU.", 4) == 0) { |
2408 | 26 | key += 4; |
2409 | 26 | key_length -= 4; |
2410 | | |
2411 | | /* GNU.sparse marks the existence of GNU sparse information */ |
2412 | 26 | if (key_length == 6 && memcmp(key, "sparse", 6) == 0) { |
2413 | 0 | tar->sparse_gnu_attributes_seen = 1; |
2414 | 0 | } |
2415 | | |
2416 | | /* GNU.sparse.* extensions */ |
2417 | 26 | else if (key_length > 7 && memcmp(key, "sparse.", 7) == 0) { |
2418 | 26 | tar->sparse_gnu_attributes_seen = 1; |
2419 | 26 | key += 7; |
2420 | 26 | key_length -= 7; |
2421 | | |
2422 | | /* GNU "0.0" sparse pax format. */ |
2423 | 26 | if (key_length == 9 && memcmp(key, "numblocks", 9) == 0) { |
2424 | | /* GNU.sparse.numblocks */ |
2425 | 0 | tar->sparse_offset = -1; |
2426 | 0 | tar->sparse_numbytes = -1; |
2427 | 0 | tar->sparse_gnu_major = 0; |
2428 | 0 | tar->sparse_gnu_minor = 0; |
2429 | 0 | } |
2430 | 26 | else if (key_length == 6 && memcmp(key, "offset", 6) == 0) { |
2431 | | /* GNU.sparse.offset */ |
2432 | 0 | if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2433 | 0 | tar->sparse_offset = t; |
2434 | 0 | if (tar->sparse_numbytes != -1) { |
2435 | 0 | if (gnu_add_sparse_entry(a, tar, |
2436 | 0 | tar->sparse_offset, tar->sparse_numbytes) |
2437 | 0 | != ARCHIVE_OK) |
2438 | 0 | return (ARCHIVE_FATAL); |
2439 | 0 | tar->sparse_offset = -1; |
2440 | 0 | tar->sparse_numbytes = -1; |
2441 | 0 | } |
2442 | 0 | } |
2443 | 0 | return (err); |
2444 | 0 | } |
2445 | 26 | else if (key_length == 8 && memcmp(key, "numbytes", 8) == 0) { |
2446 | | /* GNU.sparse.numbytes */ |
2447 | 0 | if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2448 | 0 | tar->sparse_numbytes = t; |
2449 | 0 | if (tar->sparse_offset != -1) { |
2450 | 0 | if (gnu_add_sparse_entry(a, tar, |
2451 | 0 | tar->sparse_offset, tar->sparse_numbytes) |
2452 | 0 | != ARCHIVE_OK) |
2453 | 0 | return (ARCHIVE_FATAL); |
2454 | 0 | tar->sparse_offset = -1; |
2455 | 0 | tar->sparse_numbytes = -1; |
2456 | 0 | } |
2457 | 0 | } |
2458 | 0 | return (err); |
2459 | 0 | } |
2460 | 26 | else if (key_length == 4 && memcmp(key, "size", 4) == 0) { |
2461 | | /* GNU.sparse.size */ |
2462 | | /* This is either the size of stored entry OR the size of data on disk, |
2463 | | * depending on which GNU sparse format version is in use. |
2464 | | * Since pax attributes can be in any order, we may not actually |
2465 | | * know at this point how to interpret this. */ |
2466 | 0 | if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2467 | 0 | tar->GNU_sparse_size = t; |
2468 | 0 | tar->size_fields |= TAR_SIZE_GNU_SPARSE_SIZE; |
2469 | 0 | } |
2470 | 0 | return (err); |
2471 | 0 | } |
2472 | | |
2473 | | /* GNU "0.1" sparse pax format. */ |
2474 | 26 | else if (key_length == 3 && memcmp(key, "map", 3) == 0) { |
2475 | | /* GNU.sparse.map */ |
2476 | 0 | tar->sparse_gnu_major = 0; |
2477 | 0 | tar->sparse_gnu_minor = 1; |
2478 | 0 | if (value_length > sparse_map_limit) { |
2479 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
2480 | 0 | "Unreasonably large sparse map: %llu > %llu", |
2481 | 0 | (unsigned long long)value_length, |
2482 | 0 | (unsigned long long)sparse_map_limit); |
2483 | 0 | err = ARCHIVE_FAILED; |
2484 | 0 | } else { |
2485 | 0 | p = __archive_read_ahead(a, value_length, &bytes_read); |
2486 | 0 | if (p == NULL) { |
2487 | 0 | archive_set_error(&a->archive, EINVAL, |
2488 | 0 | "Truncated archive" |
2489 | 0 | " detected while reading GNU sparse data"); |
2490 | 0 | return (ARCHIVE_FATAL); |
2491 | 0 | } |
2492 | 0 | if (gnu_sparse_01_parse(a, tar, p, value_length) != ARCHIVE_OK) { |
2493 | 0 | err = ARCHIVE_WARN; |
2494 | 0 | } |
2495 | 0 | } |
2496 | 0 | __archive_read_consume(a, value_length); |
2497 | 0 | return (err); |
2498 | 0 | } |
2499 | | |
2500 | | /* GNU "1.0" sparse pax format */ |
2501 | 26 | else if (key_length == 5 && memcmp(key, "major", 5) == 0) { |
2502 | | /* GNU.sparse.major */ |
2503 | 11 | if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK |
2504 | 11 | && t >= 0 |
2505 | 11 | && t <= 10) { |
2506 | 11 | tar->sparse_gnu_major = (int)t; |
2507 | 11 | } |
2508 | 11 | return (err); |
2509 | 11 | } |
2510 | 15 | else if (key_length == 5 && memcmp(key, "minor", 5) == 0) { |
2511 | | /* GNU.sparse.minor */ |
2512 | 10 | if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK |
2513 | 10 | && t >= 0 |
2514 | 10 | && t <= 10) { |
2515 | 10 | tar->sparse_gnu_minor = (int)t; |
2516 | 10 | } |
2517 | 10 | return (err); |
2518 | 10 | } |
2519 | 5 | else if (key_length == 4 && memcmp(key, "name", 4) == 0) { |
2520 | | /* GNU.sparse.name */ |
2521 | | /* |
2522 | | * The real filename; when storing sparse |
2523 | | * files, GNU tar puts a synthesized name into |
2524 | | * the regular 'path' attribute in an attempt |
2525 | | * to limit confusion. ;-) |
2526 | | */ |
2527 | 0 | if (value_length > pathname_limit) { |
2528 | 0 | *unconsumed += value_length; |
2529 | 0 | err = ARCHIVE_WARN; |
2530 | 0 | } else { |
2531 | 0 | err = read_bytes_to_string(a, &(tar->entry_pathname_override), |
2532 | 0 | value_length, unconsumed); |
2533 | 0 | } |
2534 | 0 | return (err); |
2535 | 0 | } |
2536 | 5 | else if (key_length == 8 && memcmp(key, "realsize", 8) == 0) { |
2537 | | /* GNU.sparse.realsize = size of file on disk */ |
2538 | 0 | if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2539 | 0 | tar->GNU_sparse_realsize = t; |
2540 | 0 | tar->size_fields |= TAR_SIZE_GNU_SPARSE_REALSIZE; |
2541 | 0 | } |
2542 | 0 | return (err); |
2543 | 0 | } |
2544 | 26 | } |
2545 | 26 | } |
2546 | 7 | break; |
2547 | 50 | case 'L': |
2548 | | /* LIBARCHIVE extensions */ |
2549 | 50 | if (key_length > 11 && memcmp(key, "LIBARCHIVE.", 11) == 0) { |
2550 | 48 | key_length -= 11; |
2551 | 48 | key += 11; |
2552 | | |
2553 | | /* TODO: Handle arbitrary extended attributes... */ |
2554 | | /* |
2555 | | if (strcmp(key, "LIBARCHIVE.xxxxxxx") == 0) |
2556 | | archive_entry_set_xxxxxx(entry, value); |
2557 | | */ |
2558 | 48 | if (key_length == 12 && memcmp(key, "creationtime", 12) == 0) { |
2559 | | /* LIBARCHIVE.creationtime */ |
2560 | 1 | __LA_TIME_T sec = 0; |
2561 | 1 | if ((err = pax_attribute_read_time(a, value_length, &sec, &n, unconsumed)) == ARCHIVE_OK) { |
2562 | 0 | archive_entry_set_birthtime(entry, sec, n); |
2563 | 1 | } else { |
2564 | 1 | archive_set_error(&a->archive, |
2565 | 1 | ARCHIVE_ERRNO_MISC, |
2566 | 1 | "Ignoring malformed pax creationtime"); |
2567 | 1 | } |
2568 | 1 | return (err); |
2569 | 1 | } |
2570 | 47 | else if (key_length == 11 && memcmp(key, "symlinktype", 11) == 0) { |
2571 | | /* LIBARCHIVE.symlinktype */ |
2572 | 0 | if (value_length < 16) { |
2573 | 0 | p = __archive_read_ahead(a, value_length, &bytes_read); |
2574 | 0 | if (p == NULL) { |
2575 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, |
2576 | 0 | "Truncated tar archive " |
2577 | 0 | "detected while reading `symlinktype` attribute"); |
2578 | 0 | return (ARCHIVE_FATAL); |
2579 | 0 | } |
2580 | 0 | if (value_length == 4 && memcmp(p, "file", 4) == 0) { |
2581 | 0 | archive_entry_set_symlink_type(entry, |
2582 | 0 | AE_SYMLINK_TYPE_FILE); |
2583 | 0 | } else if (value_length == 3 && memcmp(p, "dir", 3) == 0) { |
2584 | 0 | archive_entry_set_symlink_type(entry, |
2585 | 0 | AE_SYMLINK_TYPE_DIRECTORY); |
2586 | 0 | } else { |
2587 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
2588 | 0 | "Unrecognized symlink type"); |
2589 | 0 | err = ARCHIVE_WARN; |
2590 | 0 | } |
2591 | 0 | } else { |
2592 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
2593 | 0 | "symlink type is very long" |
2594 | 0 | "(longest recognized value is 4 bytes, this is %llu)", |
2595 | 0 | (unsigned long long)value_length); |
2596 | 0 | err = ARCHIVE_WARN; |
2597 | 0 | } |
2598 | 0 | __archive_read_consume(a, value_length); |
2599 | 0 | return (err); |
2600 | 0 | } |
2601 | 47 | else if (key_length > 6 && memcmp(key, "xattr.", 6) == 0) { |
2602 | 37 | key_length -= 6; |
2603 | 37 | key += 6; |
2604 | 37 | if (value_length > xattr_limit) { |
2605 | 0 | err = ARCHIVE_WARN; |
2606 | 37 | } else { |
2607 | 37 | p = __archive_read_ahead(a, value_length, &bytes_read); |
2608 | 37 | if (p == NULL) { |
2609 | 1 | archive_set_error(&a->archive, EINVAL, |
2610 | 1 | "Truncated archive" |
2611 | 1 | " detected while reading xattr information"); |
2612 | 1 | return (ARCHIVE_FATAL); |
2613 | 1 | } |
2614 | 36 | if (pax_attribute_LIBARCHIVE_xattr(entry, key, key_length, p, value_length)) { |
2615 | | /* TODO: Unable to parse xattr */ |
2616 | 0 | err = ARCHIVE_WARN; |
2617 | 0 | } |
2618 | 36 | } |
2619 | 36 | __archive_read_consume(a, value_length); |
2620 | 36 | return (err); |
2621 | 37 | } |
2622 | 48 | } |
2623 | 12 | break; |
2624 | 12 | case 'R': |
2625 | | /* GNU tar uses RHT.security header to store SELinux xattrs |
2626 | | * SCHILY.xattr.security.selinux == RHT.security.selinux */ |
2627 | 0 | if (key_length == 20 && memcmp(key, "RHT.security.selinux", 20) == 0) { |
2628 | 0 | if (value_length > xattr_limit) { |
2629 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
2630 | 0 | "Ignoring unreasonably large security.selinux attribute:" |
2631 | 0 | " %llu > %llu", |
2632 | 0 | (unsigned long long)value_length, |
2633 | 0 | (unsigned long long)xattr_limit); |
2634 | | /* TODO: Should this be FAILED instead? */ |
2635 | 0 | err = ARCHIVE_WARN; |
2636 | 0 | } else { |
2637 | 0 | p = __archive_read_ahead(a, value_length, &bytes_read); |
2638 | 0 | if (p == NULL) { |
2639 | 0 | archive_set_error(&a->archive, EINVAL, |
2640 | 0 | "Truncated archive" |
2641 | 0 | " detected while reading selinux data"); |
2642 | 0 | return (ARCHIVE_FATAL); |
2643 | 0 | } |
2644 | 0 | if (pax_attribute_RHT_security_selinux(entry, p, value_length)) { |
2645 | | /* TODO: Unable to parse xattr */ |
2646 | 0 | err = ARCHIVE_WARN; |
2647 | 0 | } |
2648 | 0 | } |
2649 | 0 | __archive_read_consume(a, value_length); |
2650 | 0 | return (err); |
2651 | 0 | } |
2652 | 0 | break; |
2653 | 109 | case 'S': |
2654 | | /* SCHILY.* extensions used by "star" archiver */ |
2655 | 109 | if (key_length > 7 && memcmp(key, "SCHILY.", 7) == 0) { |
2656 | 76 | key_length -= 7; |
2657 | 76 | key += 7; |
2658 | | |
2659 | 76 | if (key_length == 10 && memcmp(key, "acl.access", 10) == 0) { |
2660 | 0 | err = pax_attribute_SCHILY_acl(a, tar, entry, value_length, |
2661 | 0 | ARCHIVE_ENTRY_ACL_TYPE_ACCESS); |
2662 | | // TODO: Mark mode as set |
2663 | 0 | return (err); |
2664 | 0 | } |
2665 | 76 | else if (key_length == 11 && memcmp(key, "acl.default", 11) == 0) { |
2666 | 0 | err = pax_attribute_SCHILY_acl(a, tar, entry, value_length, |
2667 | 0 | ARCHIVE_ENTRY_ACL_TYPE_DEFAULT); |
2668 | 0 | return (err); |
2669 | 0 | } |
2670 | 76 | else if (key_length == 7 && memcmp(key, "acl.ace", 7) == 0) { |
2671 | 0 | err = pax_attribute_SCHILY_acl(a, tar, entry, value_length, |
2672 | 0 | ARCHIVE_ENTRY_ACL_TYPE_NFS4); |
2673 | | // TODO: Mark mode as set |
2674 | 0 | return (err); |
2675 | 0 | } |
2676 | 76 | else if (key_length == 8 && memcmp(key, "devmajor", 8) == 0) { |
2677 | 0 | if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2678 | 0 | archive_entry_set_rdevmajor(entry, (dev_t)t); |
2679 | 0 | } |
2680 | 0 | return (err); |
2681 | 0 | } |
2682 | 76 | else if (key_length == 8 && memcmp(key, "devminor", 8) == 0) { |
2683 | 0 | if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2684 | 0 | archive_entry_set_rdevminor(entry, (dev_t)t); |
2685 | 0 | } |
2686 | 0 | return (err); |
2687 | 0 | } |
2688 | 76 | else if (key_length == 6 && memcmp(key, "fflags", 6) == 0) { |
2689 | 0 | if (value_length < fflags_limit) { |
2690 | 0 | p = __archive_read_ahead(a, value_length, &bytes_read); |
2691 | 0 | if (p == NULL) { |
2692 | | /* Truncated archive */ |
2693 | 0 | archive_set_error(&a->archive, EINVAL, |
2694 | 0 | "Truncated archive" |
2695 | 0 | " detected while reading SCHILY.fflags"); |
2696 | 0 | return (ARCHIVE_FATAL); |
2697 | 0 | } |
2698 | 0 | archive_entry_copy_fflags_text_len(entry, p, value_length); |
2699 | 0 | err = ARCHIVE_OK; |
2700 | 0 | } else { |
2701 | | /* Overlong fflags field */ |
2702 | 0 | err = ARCHIVE_WARN; |
2703 | 0 | } |
2704 | 0 | __archive_read_consume(a, value_length); |
2705 | 0 | return (err); |
2706 | 0 | } |
2707 | 76 | else if (key_length == 3 && memcmp(key, "dev", 3) == 0) { |
2708 | 48 | if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2709 | 48 | archive_entry_set_dev(entry, (dev_t)t); |
2710 | 48 | } |
2711 | 48 | return (err); |
2712 | 48 | } |
2713 | 28 | else if (key_length == 3 && memcmp(key, "ino", 3) == 0) { |
2714 | 0 | if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2715 | 0 | archive_entry_set_ino(entry, t); |
2716 | 0 | } |
2717 | 0 | return (err); |
2718 | 0 | } |
2719 | 28 | else if (key_length == 5 && memcmp(key, "nlink", 5) == 0) { |
2720 | 0 | if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2721 | 0 | archive_entry_set_nlink(entry, (unsigned int)t); |
2722 | 0 | } |
2723 | 0 | return (err); |
2724 | 0 | } |
2725 | 28 | else if (key_length == 8 && memcmp(key, "realsize", 8) == 0) { |
2726 | 14 | if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2727 | 14 | tar->SCHILY_sparse_realsize = t; |
2728 | 14 | tar->size_fields |= TAR_SIZE_SCHILY_SPARSE_REALSIZE; |
2729 | 14 | } |
2730 | 14 | return (err); |
2731 | 14 | } |
2732 | | /* TODO: Is there a SCHILY.sparse.size similar to GNU.sparse.size ? */ |
2733 | 14 | else if (key_length > 6 && memcmp(key, "xattr.", 6) == 0) { |
2734 | 0 | key_length -= 6; |
2735 | 0 | key += 6; |
2736 | 0 | if (value_length < xattr_limit) { |
2737 | 0 | p = __archive_read_ahead(a, value_length, &bytes_read); |
2738 | 0 | if (p == NULL) { |
2739 | 0 | archive_set_error(&a->archive, EINVAL, |
2740 | 0 | "Truncated archive" |
2741 | 0 | " detected while reading SCHILY.xattr"); |
2742 | 0 | return (ARCHIVE_FATAL); |
2743 | 0 | } |
2744 | 0 | if (pax_attribute_SCHILY_xattr(entry, key, key_length, p, value_length)) { |
2745 | | /* TODO: Unable to parse xattr */ |
2746 | 0 | err = ARCHIVE_WARN; |
2747 | 0 | } |
2748 | 0 | } else { |
2749 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
2750 | 0 | "Unreasonably large xattr: %llu > %llu", |
2751 | 0 | (unsigned long long)value_length, |
2752 | 0 | (unsigned long long)xattr_limit); |
2753 | 0 | err = ARCHIVE_WARN; |
2754 | 0 | } |
2755 | 0 | __archive_read_consume(a, value_length); |
2756 | 0 | return (err); |
2757 | 0 | } |
2758 | 76 | } |
2759 | | /* SUN.* extensions from Solaris tar */ |
2760 | 47 | if (key_length > 4 && memcmp(key, "SUN.", 4) == 0) { |
2761 | 0 | key_length -= 4; |
2762 | 0 | key += 4; |
2763 | |
|
2764 | 0 | if (key_length == 9 && memcmp(key, "holesdata", 9) == 0) { |
2765 | | /* SUN.holesdata */ |
2766 | 0 | if (value_length < sparse_map_limit) { |
2767 | 0 | p = __archive_read_ahead(a, value_length, &bytes_read); |
2768 | 0 | if (p == NULL) { |
2769 | 0 | archive_set_error(&a->archive, EINVAL, |
2770 | 0 | "Truncated archive" |
2771 | 0 | " detected while reading SUN.holesdata"); |
2772 | 0 | return (ARCHIVE_FATAL); |
2773 | 0 | } |
2774 | 0 | err = pax_attribute_SUN_holesdata(a, tar, entry, p, value_length); |
2775 | 0 | if (err < ARCHIVE_OK) { |
2776 | 0 | archive_set_error(&a->archive, |
2777 | 0 | ARCHIVE_ERRNO_MISC, |
2778 | 0 | "Parse error: SUN.holesdata"); |
2779 | 0 | } |
2780 | 0 | } else { |
2781 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
2782 | 0 | "Unreasonably large sparse map: %llu > %llu", |
2783 | 0 | (unsigned long long)value_length, |
2784 | 0 | (unsigned long long)sparse_map_limit); |
2785 | 0 | err = ARCHIVE_FAILED; |
2786 | 0 | } |
2787 | 0 | __archive_read_consume(a, value_length); |
2788 | 0 | return (err); |
2789 | 0 | } |
2790 | 0 | } |
2791 | 47 | break; |
2792 | 516 | case 'a': |
2793 | 516 | if (key_length == 5 && memcmp(key, "atime", 5) == 0) { |
2794 | 0 | __LA_TIME_T sec = 0; |
2795 | 0 | if ((err = pax_attribute_read_time(a, value_length, &sec, &n, unconsumed)) == ARCHIVE_OK) { |
2796 | 0 | archive_entry_set_atime(entry, sec, n); |
2797 | 0 | } else { |
2798 | 0 | archive_set_error(&a->archive, |
2799 | 0 | ARCHIVE_ERRNO_MISC, |
2800 | 0 | "Ignoring malformed pax atime"); |
2801 | 0 | } |
2802 | 0 | return (err); |
2803 | 0 | } |
2804 | 516 | break; |
2805 | 516 | case 'c': |
2806 | 34 | if (key_length == 5 && memcmp(key, "ctime", 5) == 0) { |
2807 | 0 | __LA_TIME_T sec = 0; |
2808 | 0 | if ((err = pax_attribute_read_time(a, value_length, &sec, &n, unconsumed)) == ARCHIVE_OK) { |
2809 | 0 | archive_entry_set_ctime(entry, sec, n); |
2810 | 0 | } else { |
2811 | 0 | archive_set_error(&a->archive, |
2812 | 0 | ARCHIVE_ERRNO_MISC, |
2813 | 0 | "Ignoring malformed pax ctime"); |
2814 | 0 | } |
2815 | 0 | return (err); |
2816 | 34 | } else if (key_length == 7 && memcmp(key, "charset", 7) == 0) { |
2817 | | /* TODO: Publish charset information in entry. */ |
2818 | 34 | } else if (key_length == 7 && memcmp(key, "comment", 7) == 0) { |
2819 | | /* TODO: Publish comment in entry. */ |
2820 | 0 | } |
2821 | 34 | break; |
2822 | 34 | case 'g': |
2823 | 0 | if (key_length == 3 && memcmp(key, "gid", 3) == 0) { |
2824 | 0 | if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2825 | 0 | archive_entry_set_gid(entry, t); |
2826 | 0 | } |
2827 | 0 | return (err); |
2828 | 0 | } else if (key_length == 5 && memcmp(key, "gname", 5) == 0) { |
2829 | 0 | if (value_length > guname_limit) { |
2830 | 0 | *unconsumed += value_length; |
2831 | 0 | err = ARCHIVE_WARN; |
2832 | 0 | } else { |
2833 | 0 | err = read_bytes_to_string(a, &(tar->entry_gname), value_length, unconsumed); |
2834 | 0 | } |
2835 | 0 | return (err); |
2836 | 0 | } |
2837 | 0 | break; |
2838 | 0 | case 'h': |
2839 | 0 | if (key_length == 10 && memcmp(key, "hdrcharset", 10) == 0) { |
2840 | 0 | if (value_length < 64) { |
2841 | 0 | p = __archive_read_ahead(a, value_length, &bytes_read); |
2842 | 0 | if (p == NULL) { |
2843 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, |
2844 | 0 | "Truncated tar archive " |
2845 | 0 | "detected while reading hdrcharset attribute"); |
2846 | 0 | return (ARCHIVE_FATAL); |
2847 | 0 | } |
2848 | 0 | if (value_length == 6 |
2849 | 0 | && memcmp(p, "BINARY", 6) == 0) { |
2850 | | /* Binary mode. */ |
2851 | 0 | tar->pax_hdrcharset_utf8 = 0; |
2852 | 0 | err = ARCHIVE_OK; |
2853 | 0 | } else if (value_length == 23 |
2854 | 0 | && memcmp(p, "ISO-IR 10646 2000 UTF-8", 23) == 0) { |
2855 | 0 | tar->pax_hdrcharset_utf8 = 1; |
2856 | 0 | err = ARCHIVE_OK; |
2857 | 0 | } else { |
2858 | | /* TODO: Unrecognized character set */ |
2859 | 0 | err = ARCHIVE_WARN; |
2860 | 0 | } |
2861 | 0 | } else { |
2862 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, |
2863 | 0 | "hdrcharset attribute is unreasonably large (%llu bytes)", |
2864 | 0 | (unsigned long long)value_length); |
2865 | 0 | err = ARCHIVE_WARN; |
2866 | 0 | } |
2867 | 0 | __archive_read_consume(a, value_length); |
2868 | 0 | return (err); |
2869 | 0 | } |
2870 | 0 | break; |
2871 | 9 | case 'l': |
2872 | | /* pax interchange doesn't distinguish hardlink vs. symlink. */ |
2873 | 9 | if (key_length == 8 && memcmp(key, "linkpath", 8) == 0) { |
2874 | 0 | if (value_length > pathname_limit) { |
2875 | 0 | *unconsumed += value_length; |
2876 | 0 | err = ARCHIVE_WARN; |
2877 | 0 | } else { |
2878 | 0 | err = read_bytes_to_string(a, &tar->entry_linkpath, value_length, unconsumed); |
2879 | 0 | } |
2880 | 0 | return (err); |
2881 | 0 | } |
2882 | 9 | break; |
2883 | 204 | case 'm': |
2884 | 204 | if (key_length == 5 && memcmp(key, "mtime", 5) == 0) { |
2885 | 83 | __LA_TIME_T sec; |
2886 | 83 | if ((err = pax_attribute_read_time(a, value_length, &sec, &n, unconsumed)) == ARCHIVE_OK) { |
2887 | 38 | archive_entry_set_mtime(entry, sec, n); |
2888 | 45 | } else { |
2889 | 45 | archive_set_error(&a->archive, |
2890 | 45 | ARCHIVE_ERRNO_MISC, |
2891 | 45 | "Ignoring malformed pax mtime"); |
2892 | 45 | } |
2893 | 83 | return (err); |
2894 | 83 | } |
2895 | 121 | break; |
2896 | 121 | case 'p': |
2897 | 2 | if (key_length == 4 && memcmp(key, "path", 4) == 0) { |
2898 | 0 | if (value_length > pathname_limit) { |
2899 | 0 | *unconsumed += value_length; |
2900 | 0 | err = ARCHIVE_WARN; |
2901 | 0 | } else { |
2902 | 0 | err = read_bytes_to_string(a, &(tar->entry_pathname), value_length, unconsumed); |
2903 | 0 | } |
2904 | 0 | return (err); |
2905 | 0 | } |
2906 | 2 | break; |
2907 | 2 | case 'r': |
2908 | | /* POSIX has reserved 'realtime.*' */ |
2909 | 0 | break; |
2910 | 11 | case 's': |
2911 | | /* POSIX has reserved 'security.*' */ |
2912 | | /* Someday: if (strcmp(key, "security.acl") == 0) { ... } */ |
2913 | 11 | if (key_length == 4 && memcmp(key, "size", 4) == 0) { |
2914 | | /* "size" is the size of the data in the entry. */ |
2915 | 0 | if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2916 | 0 | tar->pax_size = t; |
2917 | 0 | tar->size_fields |= TAR_SIZE_PAX_SIZE; |
2918 | 0 | } |
2919 | 0 | else if (t == INT64_MAX) { |
2920 | | /* Note: pax_attr_read_number returns INT64_MAX on overflow or < 0 */ |
2921 | 0 | tar->entry_bytes_remaining = 0; |
2922 | 0 | archive_set_error(&a->archive, |
2923 | 0 | ARCHIVE_ERRNO_MISC, |
2924 | 0 | "Tar size attribute overflow"); |
2925 | 0 | return (ARCHIVE_FATAL); |
2926 | 0 | } |
2927 | 0 | return (err); |
2928 | 0 | } |
2929 | 11 | break; |
2930 | 38 | case 'u': |
2931 | 38 | if (key_length == 3 && memcmp(key, "uid", 3) == 0) { |
2932 | 0 | if ((err = pax_attribute_read_number(a, value_length, &t)) == ARCHIVE_OK) { |
2933 | 0 | archive_entry_set_uid(entry, t); |
2934 | 0 | } |
2935 | 0 | return (err); |
2936 | 38 | } else if (key_length == 5 && memcmp(key, "uname", 5) == 0) { |
2937 | 0 | if (value_length > guname_limit) { |
2938 | 0 | *unconsumed += value_length; |
2939 | 0 | err = ARCHIVE_WARN; |
2940 | 0 | } else { |
2941 | 0 | err = read_bytes_to_string(a, &(tar->entry_uname), value_length, unconsumed); |
2942 | 0 | } |
2943 | 0 | return (err); |
2944 | 0 | } |
2945 | 38 | break; |
2946 | 1.02k | } |
2947 | | |
2948 | | /* Unrecognized key, just skip the entire value. */ |
2949 | 822 | __archive_read_consume(a, value_length); |
2950 | 822 | return (err); |
2951 | 1.02k | } |
2952 | | |
2953 | | |
2954 | | |
2955 | | /* |
2956 | | * Parse a decimal time value, which may include a fractional portion |
2957 | | * |
2958 | | * Sets ps to INT64_MIN on error, including syntax issues such as non-digits, |
2959 | | * or a time value that's outside the range of time_t. |
2960 | | */ |
2961 | | static void |
2962 | | pax_time(const char *p, size_t length, int64_t *ps, long *pn) |
2963 | 83 | { |
2964 | 83 | char digit; |
2965 | 83 | int64_t s; |
2966 | 83 | unsigned long l; |
2967 | 83 | int sign; |
2968 | 83 | int64_t limit, last_digit_limit; |
2969 | | |
2970 | 83 | limit = INT64_MAX / 10; |
2971 | 83 | last_digit_limit = INT64_MAX % 10; |
2972 | | |
2973 | 83 | if (length <= 0) { |
2974 | 0 | *ps = 0; |
2975 | 0 | *pn = 0; |
2976 | 0 | return; |
2977 | 0 | } |
2978 | 83 | s = 0; |
2979 | 83 | sign = 1; |
2980 | 83 | if (*p == '-') { |
2981 | 83 | sign = -1; |
2982 | 83 | p++; |
2983 | 83 | length--; |
2984 | 83 | } |
2985 | 845 | while (length > 0 && *p >= '0' && *p <= '9') { |
2986 | 762 | digit = *p - '0'; |
2987 | 762 | if (s > limit || |
2988 | 762 | (s == limit && digit > last_digit_limit)) { |
2989 | 0 | *ps = INT64_MIN; |
2990 | 0 | *pn = 0; |
2991 | 0 | return; |
2992 | 0 | } |
2993 | 762 | s = (s * 10) + digit; |
2994 | 762 | ++p; |
2995 | 762 | --length; |
2996 | 762 | } |
2997 | | |
2998 | 83 | *ps = s * sign; |
2999 | | |
3000 | 83 | #if ARCHIVE_VERSION_NUMBER < 4000000 |
3001 | | /* Libarchive 4.0 will have __LA_TIME_T == int64_t, so |
3002 | | this will be unnecessary. */ |
3003 | | /* Test whether it overflows __LA_TIME_T */ |
3004 | 83 | __LA_TIME_T sec = (__LA_TIME_T)*ps; |
3005 | 83 | if ((int64_t)sec != *ps) { |
3006 | 0 | *ps = INT64_MIN; |
3007 | 0 | *pn = 0; |
3008 | 0 | return; |
3009 | 0 | } |
3010 | 83 | #endif |
3011 | | |
3012 | | /* Calculate nanoseconds. */ |
3013 | 83 | *pn = 0; |
3014 | | |
3015 | 83 | if (length <= 0) { |
3016 | 0 | return; |
3017 | 0 | } |
3018 | | |
3019 | | /* Skip `.` */ |
3020 | 83 | if (*p != '.') { |
3021 | 28 | *ps = INT64_MIN; |
3022 | 28 | *pn = 0; |
3023 | 28 | return; |
3024 | 28 | } |
3025 | 55 | ++p; |
3026 | 55 | --length; |
3027 | | |
3028 | 55 | l = 100000000UL; |
3029 | 277 | do { |
3030 | 277 | if (length <= 0) { |
3031 | 38 | return; |
3032 | 38 | } |
3033 | 239 | if (*p >= '0' && *p <= '9') { |
3034 | 222 | *pn += (*p - '0') * l; |
3035 | 222 | } else { |
3036 | 17 | *ps = INT64_MIN; |
3037 | 17 | *pn = 0; |
3038 | 17 | return; |
3039 | 17 | } |
3040 | 222 | ++p; |
3041 | 222 | --length; |
3042 | 222 | } while (l /= 10); |
3043 | | |
3044 | | /* Ignore resolution beyond nanoseconds, |
3045 | | but verify it's all decimal digits. */ |
3046 | 0 | while (length > 0) { |
3047 | 0 | if (*p < '0' || *p > '9') { |
3048 | 0 | *ps = INT64_MIN; |
3049 | 0 | *pn = 0; |
3050 | 0 | return; |
3051 | 0 | } |
3052 | 0 | ++p; |
3053 | 0 | --length; |
3054 | 0 | } |
3055 | 0 | } |
3056 | | |
3057 | | /* |
3058 | | * Parse GNU tar header |
3059 | | */ |
3060 | | static int |
3061 | | header_gnutar(struct archive_read *a, struct tar *tar, |
3062 | | struct archive_entry *entry, const void *h, int64_t *unconsumed) |
3063 | 1.84k | { |
3064 | 1.84k | const struct archive_entry_header_gnutar *header; |
3065 | 1.84k | int64_t t; |
3066 | 1.84k | int err = ARCHIVE_OK; |
3067 | | |
3068 | | /* |
3069 | | * GNU header is like POSIX ustar, except 'prefix' is |
3070 | | * replaced with some other fields. This also means the |
3071 | | * filename is stored as in old-style archives. |
3072 | | */ |
3073 | | |
3074 | | /* Copy filename over (to ensure null termination). */ |
3075 | 1.84k | header = (const struct archive_entry_header_gnutar *)h; |
3076 | 1.84k | const char *existing_pathname = archive_entry_pathname(entry); |
3077 | 1.84k | const wchar_t *existing_wcs_pathname = archive_entry_pathname_w(entry); |
3078 | 1.84k | if ((existing_pathname == NULL || existing_pathname[0] == '\0') |
3079 | 1.84k | && (existing_wcs_pathname == NULL || existing_wcs_pathname[0] == L'\0')) { |
3080 | 1.84k | if (archive_entry_copy_pathname_l(entry, |
3081 | 1.84k | header->name, sizeof(header->name), tar->sconv) != 0) { |
3082 | 0 | err = set_conversion_failed_error(a, tar->sconv, "Pathname"); |
3083 | 0 | if (err == ARCHIVE_FATAL) |
3084 | 0 | return (err); |
3085 | 0 | } |
3086 | 1.84k | } |
3087 | | |
3088 | | /* Fields common to ustar and GNU */ |
3089 | | /* XXX Can the following be factored out since it's common |
3090 | | * to ustar and gnu tar? Is it okay to move it down into |
3091 | | * header_common, perhaps? */ |
3092 | 1.84k | const char *existing_uname = archive_entry_uname(entry); |
3093 | 1.84k | if (existing_uname == NULL || existing_uname[0] == '\0') { |
3094 | 1.84k | if (archive_entry_copy_uname_l(entry, |
3095 | 1.84k | header->uname, sizeof(header->uname), tar->sconv) != 0) { |
3096 | 0 | err = set_conversion_failed_error(a, tar->sconv, "Uname"); |
3097 | 0 | if (err == ARCHIVE_FATAL) |
3098 | 0 | return (err); |
3099 | 0 | } |
3100 | 1.84k | } |
3101 | | |
3102 | 1.84k | const char *existing_gname = archive_entry_gname(entry); |
3103 | 1.84k | if (existing_gname == NULL || existing_gname[0] == '\0') { |
3104 | 1.84k | if (archive_entry_copy_gname_l(entry, |
3105 | 1.84k | header->gname, sizeof(header->gname), tar->sconv) != 0) { |
3106 | 0 | err = set_conversion_failed_error(a, tar->sconv, "Gname"); |
3107 | 0 | if (err == ARCHIVE_FATAL) |
3108 | 0 | return (err); |
3109 | 0 | } |
3110 | 1.84k | } |
3111 | | |
3112 | | /* Parse out device numbers only for char and block specials */ |
3113 | 1.84k | if (header->typeflag[0] == '3' || header->typeflag[0] == '4') { |
3114 | 436 | if (!archive_entry_rdev_is_set(entry)) { |
3115 | 436 | archive_entry_set_rdevmajor(entry, (dev_t) |
3116 | 436 | tar_atol(header->rdevmajor, sizeof(header->rdevmajor))); |
3117 | 436 | archive_entry_set_rdevminor(entry, (dev_t) |
3118 | 436 | tar_atol(header->rdevminor, sizeof(header->rdevminor))); |
3119 | 436 | } |
3120 | 1.40k | } else { |
3121 | 1.40k | archive_entry_set_rdev(entry, 0); |
3122 | 1.40k | } |
3123 | | |
3124 | | /* Grab GNU-specific fields. */ |
3125 | 1.84k | if (!archive_entry_atime_is_set(entry)) { |
3126 | 1.84k | t = tar_atol(header->atime, sizeof(header->atime)); |
3127 | 1.84k | if (t > 0) |
3128 | 707 | archive_entry_set_atime(entry, t, 0); |
3129 | 1.84k | } |
3130 | 1.84k | if (!archive_entry_ctime_is_set(entry)) { |
3131 | 1.84k | t = tar_atol(header->ctime, sizeof(header->ctime)); |
3132 | 1.84k | if (t > 0) |
3133 | 1.16k | archive_entry_set_ctime(entry, t, 0); |
3134 | 1.84k | } |
3135 | | |
3136 | 1.84k | if (header->realsize[0] != 0) { |
3137 | | /* Treat as a synonym for the pax GNU.sparse.realsize attr */ |
3138 | 690 | tar->GNU_sparse_realsize |
3139 | 690 | = tar_atol(header->realsize, sizeof(header->realsize)); |
3140 | 690 | tar->size_fields |= TAR_SIZE_GNU_SPARSE_REALSIZE; |
3141 | 690 | } |
3142 | | |
3143 | 1.84k | if (header->sparse[0].offset[0] != 0) { |
3144 | 1.56k | if (gnu_sparse_old_read(a, tar, header, unconsumed) |
3145 | 1.56k | != ARCHIVE_OK) |
3146 | 8 | return (ARCHIVE_FATAL); |
3147 | 1.56k | } else { |
3148 | 276 | if (header->isextended[0] != 0) { |
3149 | | /* XXX WTF? XXX */ |
3150 | 37 | } |
3151 | 276 | } |
3152 | | |
3153 | | /* Grab fields common to all tar variants. */ |
3154 | 1.83k | err = header_common(a, tar, entry, h); |
3155 | 1.83k | if (err == ARCHIVE_FATAL) |
3156 | 0 | return (err); |
3157 | | |
3158 | 1.83k | tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining); |
3159 | | |
3160 | 1.83k | return (err); |
3161 | 1.83k | } |
3162 | | |
3163 | | static int |
3164 | | gnu_add_sparse_entry(struct archive_read *a, struct tar *tar, |
3165 | | int64_t offset, int64_t remaining) |
3166 | 272k | { |
3167 | 272k | struct sparse_block *p; |
3168 | | |
3169 | 272k | p = calloc(1, sizeof(*p)); |
3170 | 272k | if (p == NULL) { |
3171 | 0 | archive_set_error(&a->archive, ENOMEM, "Out of memory"); |
3172 | 0 | return (ARCHIVE_FATAL); |
3173 | 0 | } |
3174 | 272k | if (tar->sparse_last != NULL) |
3175 | 38.5k | tar->sparse_last->next = p; |
3176 | 233k | else |
3177 | 233k | tar->sparse_list = p; |
3178 | 272k | tar->sparse_last = p; |
3179 | 272k | if (remaining < 0 || offset < 0 || offset > INT64_MAX - remaining) { |
3180 | 1 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, "Malformed sparse map data"); |
3181 | 1 | return (ARCHIVE_FATAL); |
3182 | 1 | } |
3183 | 272k | p->offset = offset; |
3184 | 272k | p->remaining = remaining; |
3185 | 272k | return (ARCHIVE_OK); |
3186 | 272k | } |
3187 | | |
3188 | | static void |
3189 | | gnu_clear_sparse_list(struct tar *tar) |
3190 | 257k | { |
3191 | 257k | struct sparse_block *p; |
3192 | | |
3193 | 511k | while (tar->sparse_list != NULL) { |
3194 | 253k | p = tar->sparse_list; |
3195 | 253k | tar->sparse_list = p->next; |
3196 | 253k | free(p); |
3197 | 253k | } |
3198 | 257k | tar->sparse_last = NULL; |
3199 | 257k | } |
3200 | | |
3201 | | /* |
3202 | | * GNU tar old-format sparse data. |
3203 | | * |
3204 | | * GNU old-format sparse data is stored in a fixed-field |
3205 | | * format. Offset/size values are 11-byte octal fields (same |
3206 | | * format as 'size' field in ustart header). These are |
3207 | | * stored in the header, allocating subsequent header blocks |
3208 | | * as needed. Extending the header in this way is a pretty |
3209 | | * severe POSIX violation; this design has earned GNU tar a |
3210 | | * lot of criticism. |
3211 | | */ |
3212 | | |
3213 | | static int |
3214 | | gnu_sparse_old_read(struct archive_read *a, struct tar *tar, |
3215 | | const struct archive_entry_header_gnutar *header, int64_t *unconsumed) |
3216 | 1.56k | { |
3217 | 1.56k | ssize_t bytes_read; |
3218 | 1.56k | const void *data; |
3219 | 1.56k | struct extended { |
3220 | 1.56k | struct gnu_sparse sparse[21]; |
3221 | 1.56k | char isextended[1]; |
3222 | 1.56k | char padding[7]; |
3223 | 1.56k | }; |
3224 | 1.56k | const struct extended *ext; |
3225 | | |
3226 | 1.56k | if (gnu_sparse_old_parse(a, tar, header->sparse, 4) != ARCHIVE_OK) |
3227 | 0 | return (ARCHIVE_FATAL); |
3228 | 1.56k | if (header->isextended[0] == 0) |
3229 | 1.05k | return (ARCHIVE_OK); |
3230 | | |
3231 | 2.23k | do { |
3232 | 2.23k | if (tar_flush_unconsumed(a, unconsumed) != ARCHIVE_OK) { |
3233 | 0 | return (ARCHIVE_FATAL); |
3234 | 0 | } |
3235 | 2.23k | data = __archive_read_ahead(a, 512, &bytes_read); |
3236 | 2.23k | if (data == NULL) { |
3237 | 7 | archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, |
3238 | 7 | "Truncated tar archive " |
3239 | 7 | "detected while reading sparse file data"); |
3240 | 7 | return (ARCHIVE_FATAL); |
3241 | 7 | } |
3242 | 2.23k | *unconsumed = 512; |
3243 | 2.23k | ext = (const struct extended *)data; |
3244 | 2.23k | if (gnu_sparse_old_parse(a, tar, ext->sparse, 21) != ARCHIVE_OK) |
3245 | 1 | return (ARCHIVE_FATAL); |
3246 | 2.23k | } while (ext->isextended[0] != 0); |
3247 | 510 | if (tar->sparse_list != NULL) |
3248 | 510 | tar->entry_offset = tar->sparse_list->offset; |
3249 | 510 | return (ARCHIVE_OK); |
3250 | 518 | } |
3251 | | |
3252 | | static int |
3253 | | gnu_sparse_old_parse(struct archive_read *a, struct tar *tar, |
3254 | | const struct gnu_sparse *sparse, int length) |
3255 | 3.79k | { |
3256 | 43.9k | while (length > 0 && sparse->offset[0] != 0) { |
3257 | 40.1k | if (gnu_add_sparse_entry(a, tar, |
3258 | 40.1k | tar_atol(sparse->offset, sizeof(sparse->offset)), |
3259 | 40.1k | tar_atol(sparse->numbytes, sizeof(sparse->numbytes))) |
3260 | 40.1k | != ARCHIVE_OK) |
3261 | 1 | return (ARCHIVE_FATAL); |
3262 | 40.1k | sparse++; |
3263 | 40.1k | length--; |
3264 | 40.1k | } |
3265 | 3.79k | return (ARCHIVE_OK); |
3266 | 3.79k | } |
3267 | | |
3268 | | /* |
3269 | | * GNU tar sparse format 0.0 |
3270 | | * |
3271 | | * Beginning with GNU tar 1.15, sparse files are stored using |
3272 | | * information in the pax extended header. The GNU tar maintainers |
3273 | | * have gone through a number of variations in the process of working |
3274 | | * out this scheme; fortunately, they're all numbered. |
3275 | | * |
3276 | | * Sparse format 0.0 uses attribute GNU.sparse.numblocks to store the |
3277 | | * number of blocks, and GNU.sparse.offset/GNU.sparse.numbytes to |
3278 | | * store offset/size for each block. The repeated instances of these |
3279 | | * latter fields violate the pax specification (which frowns on |
3280 | | * duplicate keys), so this format was quickly replaced. |
3281 | | */ |
3282 | | |
3283 | | /* |
3284 | | * GNU tar sparse format 0.1 |
3285 | | * |
3286 | | * This version replaced the offset/numbytes attributes with |
3287 | | * a single "map" attribute that stored a list of integers. This |
3288 | | * format had two problems: First, the "map" attribute could be very |
3289 | | * long, which caused problems for some implementations. More |
3290 | | * importantly, the sparse data was lost when extracted by archivers |
3291 | | * that didn't recognize this extension. |
3292 | | */ |
3293 | | static int |
3294 | | gnu_sparse_01_parse(struct archive_read *a, struct tar *tar, const char *p, size_t length) |
3295 | 0 | { |
3296 | 0 | const char *e; |
3297 | 0 | int64_t offset = -1, size = -1; |
3298 | |
|
3299 | 0 | for (;;) { |
3300 | 0 | e = p; |
3301 | 0 | while (length > 0 && *e != ',') { |
3302 | 0 | if (*e < '0' || *e > '9') |
3303 | 0 | return (ARCHIVE_WARN); |
3304 | 0 | e++; |
3305 | 0 | length--; |
3306 | 0 | } |
3307 | 0 | if (offset < 0) { |
3308 | 0 | offset = tar_atol10(p, e - p); |
3309 | 0 | if (offset < 0) |
3310 | 0 | return (ARCHIVE_WARN); |
3311 | 0 | } else { |
3312 | 0 | size = tar_atol10(p, e - p); |
3313 | 0 | if (size < 0) |
3314 | 0 | return (ARCHIVE_WARN); |
3315 | 0 | if (gnu_add_sparse_entry(a, tar, offset, size) |
3316 | 0 | != ARCHIVE_OK) |
3317 | 0 | return (ARCHIVE_FATAL); |
3318 | 0 | offset = -1; |
3319 | 0 | } |
3320 | 0 | if (length == 0) |
3321 | 0 | return (ARCHIVE_OK); |
3322 | 0 | p = e + 1; |
3323 | 0 | length--; |
3324 | 0 | } |
3325 | 0 | } |
3326 | | |
3327 | | /* |
3328 | | * GNU tar sparse format 1.0 |
3329 | | * |
3330 | | * The idea: The offset/size data is stored as a series of base-10 |
3331 | | * ASCII numbers prepended to the file data, so that dearchivers that |
3332 | | * don't support this format will extract the block map along with the |
3333 | | * data and a separate post-process can restore the sparseness. |
3334 | | * |
3335 | | * Unfortunately, GNU tar 1.16 had a bug that added unnecessary |
3336 | | * padding to the body of the file when using this format. GNU tar |
3337 | | * 1.17 corrected this bug without bumping the version number, so |
3338 | | * it's not possible to support both variants. This code supports |
3339 | | * the later variant at the expense of not supporting the former. |
3340 | | * |
3341 | | * This variant also introduced the GNU.sparse.major/GNU.sparse.minor attributes. |
3342 | | */ |
3343 | | |
3344 | | /* |
3345 | | * Read the next line from the input, and parse it as a decimal |
3346 | | * integer followed by '\n'. Returns positive integer value or |
3347 | | * negative on error. |
3348 | | */ |
3349 | | static int64_t |
3350 | | gnu_sparse_10_atol(struct archive_read *a, struct tar *tar, |
3351 | | int64_t *remaining, int64_t *unconsumed) |
3352 | 3 | { |
3353 | 3 | int64_t l, limit, last_digit_limit; |
3354 | 3 | const char *p; |
3355 | 3 | ssize_t bytes_read; |
3356 | 3 | int base, digit; |
3357 | | |
3358 | 3 | base = 10; |
3359 | 3 | limit = INT64_MAX / base; |
3360 | 3 | last_digit_limit = INT64_MAX % base; |
3361 | | |
3362 | | /* |
3363 | | * Skip any lines starting with '#'; GNU tar specs |
3364 | | * don't require this, but they should. |
3365 | | */ |
3366 | 3 | do { |
3367 | 3 | bytes_read = readline(a, tar, &p, |
3368 | 3 | (ssize_t)tar_min(*remaining, 100), unconsumed); |
3369 | 3 | if (bytes_read <= 0) |
3370 | 2 | return (ARCHIVE_FATAL); |
3371 | 1 | *remaining -= bytes_read; |
3372 | 1 | } while (p[0] == '#'); |
3373 | | |
3374 | 1 | l = 0; |
3375 | 1 | while (bytes_read > 0) { |
3376 | 1 | if (*p == '\n') |
3377 | 0 | return (l); |
3378 | 1 | if (*p < '0' || *p >= '0' + base) |
3379 | 1 | return (ARCHIVE_WARN); |
3380 | 0 | digit = *p - '0'; |
3381 | 0 | if (l > limit || (l == limit && digit > last_digit_limit)) |
3382 | 0 | l = INT64_MAX; /* Truncate on overflow. */ |
3383 | 0 | else |
3384 | 0 | l = (l * base) + digit; |
3385 | 0 | p++; |
3386 | 0 | bytes_read--; |
3387 | 0 | } |
3388 | | /* TODO: Error message. */ |
3389 | 0 | return (ARCHIVE_WARN); |
3390 | 1 | } |
3391 | | |
3392 | | /* |
3393 | | * Returns length (in bytes) of the sparse data description |
3394 | | * that was read. |
3395 | | */ |
3396 | | static int64_t |
3397 | | gnu_sparse_10_read(struct archive_read *a, struct tar *tar, int64_t *unconsumed) |
3398 | 3 | { |
3399 | 3 | int64_t bytes_read, entries, offset, size, to_skip, remaining; |
3400 | | |
3401 | | /* Clear out the existing sparse list. */ |
3402 | 3 | gnu_clear_sparse_list(tar); |
3403 | | |
3404 | 3 | remaining = tar->entry_bytes_remaining; |
3405 | | |
3406 | | /* Parse entries. */ |
3407 | 3 | entries = gnu_sparse_10_atol(a, tar, &remaining, unconsumed); |
3408 | 3 | if (entries < 0) |
3409 | 3 | return (ARCHIVE_FATAL); |
3410 | | /* Parse the individual entries. */ |
3411 | 0 | while (entries-- > 0) { |
3412 | | /* Parse offset/size */ |
3413 | 0 | offset = gnu_sparse_10_atol(a, tar, &remaining, unconsumed); |
3414 | 0 | if (offset < 0) |
3415 | 0 | return (ARCHIVE_FATAL); |
3416 | 0 | size = gnu_sparse_10_atol(a, tar, &remaining, unconsumed); |
3417 | 0 | if (size < 0) |
3418 | 0 | return (ARCHIVE_FATAL); |
3419 | | /* Add a new sparse entry. */ |
3420 | 0 | if (gnu_add_sparse_entry(a, tar, offset, size) != ARCHIVE_OK) |
3421 | 0 | return (ARCHIVE_FATAL); |
3422 | 0 | } |
3423 | | /* Skip rest of block... */ |
3424 | 0 | if (tar_flush_unconsumed(a, unconsumed) != ARCHIVE_OK) { |
3425 | 0 | return (ARCHIVE_FATAL); |
3426 | 0 | } |
3427 | 0 | bytes_read = tar->entry_bytes_remaining - remaining; |
3428 | 0 | to_skip = 0x1ff & -bytes_read; |
3429 | | /* Fail if tar->entry_bytes_remaing would get negative */ |
3430 | 0 | if (to_skip > remaining) |
3431 | 0 | return (ARCHIVE_FATAL); |
3432 | 0 | if (to_skip != __archive_read_consume(a, to_skip)) |
3433 | 0 | return (ARCHIVE_FATAL); |
3434 | 0 | return (bytes_read + to_skip); |
3435 | 0 | } |
3436 | | |
3437 | | /* |
3438 | | * Solaris pax extension for a sparse file. This is recorded with the |
3439 | | * data and hole pairs. The way recording sparse information by Solaris' |
3440 | | * pax simply indicates where data and sparse are, so the stored contents |
3441 | | * consist of both data and hole. |
3442 | | */ |
3443 | | static int |
3444 | | pax_attribute_SUN_holesdata(struct archive_read *a, struct tar *tar, |
3445 | | struct archive_entry *entry, const char *p, size_t length) |
3446 | 0 | { |
3447 | 0 | const char *e; |
3448 | 0 | int64_t start, end; |
3449 | 0 | int hole = 1; |
3450 | |
|
3451 | 0 | (void)entry; /* UNUSED */ |
3452 | |
|
3453 | 0 | end = 0; |
3454 | 0 | if (length <= 0) |
3455 | 0 | return (ARCHIVE_WARN); |
3456 | 0 | if (*p == ' ') { |
3457 | 0 | p++; |
3458 | 0 | length--; |
3459 | 0 | } else { |
3460 | 0 | return (ARCHIVE_WARN); |
3461 | 0 | } |
3462 | 0 | for (;;) { |
3463 | 0 | e = p; |
3464 | 0 | while (length > 0 && *e != ' ') { |
3465 | 0 | if (*e < '0' || *e > '9') |
3466 | 0 | return (ARCHIVE_WARN); |
3467 | 0 | e++; |
3468 | 0 | length--; |
3469 | 0 | } |
3470 | 0 | start = end; |
3471 | 0 | end = tar_atol10(p, e - p); |
3472 | 0 | if (end < 0) |
3473 | 0 | return (ARCHIVE_WARN); |
3474 | 0 | if (start < end) { |
3475 | 0 | if (gnu_add_sparse_entry(a, tar, start, |
3476 | 0 | end - start) != ARCHIVE_OK) |
3477 | 0 | return (ARCHIVE_FATAL); |
3478 | 0 | tar->sparse_last->hole = hole; |
3479 | 0 | } |
3480 | 0 | if (length == 0 || *e == '\n') { |
3481 | 0 | if (length == 0 && *e == '\n') { |
3482 | 0 | return (ARCHIVE_OK); |
3483 | 0 | } else { |
3484 | 0 | return (ARCHIVE_WARN); |
3485 | 0 | } |
3486 | 0 | } |
3487 | 0 | p = e + 1; |
3488 | 0 | length--; |
3489 | 0 | hole = hole == 0; |
3490 | 0 | } |
3491 | 0 | } |
3492 | | |
3493 | | /*- |
3494 | | * Convert text->integer. |
3495 | | * |
3496 | | * Traditional tar formats (including POSIX) specify base-8 for |
3497 | | * all of the standard numeric fields. This is a significant limitation |
3498 | | * in practice: |
3499 | | * = file size is limited to 8GB |
3500 | | * = rdevmajor and rdevminor are limited to 21 bits |
3501 | | * = uid/gid are limited to 21 bits |
3502 | | * |
3503 | | * There are two workarounds for this: |
3504 | | * = pax extended headers, which use variable-length string fields |
3505 | | * = GNU tar and STAR both allow either base-8 or base-256 in |
3506 | | * most fields. The high bit is set to indicate base-256. |
3507 | | * |
3508 | | * On read, this implementation supports both extensions. |
3509 | | */ |
3510 | | static int64_t |
3511 | | tar_atol(const char *p, size_t char_cnt) |
3512 | 265k | { |
3513 | | /* |
3514 | | * Technically, GNU tar considers a field to be in base-256 |
3515 | | * only if the first byte is 0xff or 0x80. |
3516 | | */ |
3517 | 265k | if (*p & 0x80) |
3518 | 2.00k | return (tar_atol256(p, char_cnt)); |
3519 | 263k | return (tar_atol8(p, char_cnt)); |
3520 | 265k | } |
3521 | | |
3522 | | /* |
3523 | | * Note that this implementation does not (and should not!) obey |
3524 | | * locale settings; you cannot simply substitute strtol here, since |
3525 | | * it does obey locale. |
3526 | | */ |
3527 | | static int64_t |
3528 | | tar_atol_base_n(const char *p, size_t char_cnt, int base) |
3529 | 263k | { |
3530 | 263k | int64_t l, maxval, limit, last_digit_limit; |
3531 | 263k | int digit, sign; |
3532 | | |
3533 | 263k | maxval = INT64_MAX; |
3534 | 263k | limit = INT64_MAX / base; |
3535 | 263k | last_digit_limit = INT64_MAX % base; |
3536 | | |
3537 | | /* the pointer will not be dereferenced if char_cnt is zero |
3538 | | * due to the way the && operator is evaluated. |
3539 | | */ |
3540 | 1.02M | while (char_cnt != 0 && (*p == ' ' || *p == '\t')) { |
3541 | 757k | p++; |
3542 | 757k | char_cnt--; |
3543 | 757k | } |
3544 | | |
3545 | 263k | sign = 1; |
3546 | 263k | if (char_cnt != 0 && *p == '-') { |
3547 | 3 | sign = -1; |
3548 | 3 | p++; |
3549 | 3 | char_cnt--; |
3550 | | |
3551 | 3 | maxval = INT64_MIN; |
3552 | 3 | limit = -(INT64_MIN / base); |
3553 | 3 | last_digit_limit = -(INT64_MIN % base); |
3554 | 3 | } |
3555 | | |
3556 | 263k | l = 0; |
3557 | 263k | if (char_cnt != 0) { |
3558 | 205k | digit = *p - '0'; |
3559 | 585k | while (digit >= 0 && digit < base && char_cnt != 0) { |
3560 | 379k | if (l>limit || (l == limit && digit >= last_digit_limit)) { |
3561 | 0 | return maxval; /* Truncate on overflow. */ |
3562 | 0 | } |
3563 | 379k | l = (l * base) + digit; |
3564 | 379k | digit = *++p - '0'; |
3565 | 379k | char_cnt--; |
3566 | 379k | } |
3567 | 205k | } |
3568 | 263k | return (sign < 0) ? -l : l; |
3569 | 263k | } |
3570 | | |
3571 | | static int64_t |
3572 | | tar_atol8(const char *p, size_t char_cnt) |
3573 | 263k | { |
3574 | 263k | return tar_atol_base_n(p, char_cnt, 8); |
3575 | 263k | } |
3576 | | |
3577 | | static int64_t |
3578 | | tar_atol10(const char *p, size_t char_cnt) |
3579 | 83 | { |
3580 | 83 | return tar_atol_base_n(p, char_cnt, 10); |
3581 | 83 | } |
3582 | | |
3583 | | /* |
3584 | | * Parse a base-256 integer. This is just a variable-length |
3585 | | * twos-complement signed binary value in big-endian order, except |
3586 | | * that the high-order bit is ignored. The values here can be up to |
3587 | | * 12 bytes, so we need to be careful about overflowing 64-bit |
3588 | | * (8-byte) integers. |
3589 | | * |
3590 | | * This code unashamedly assumes that the local machine uses 8-bit |
3591 | | * bytes and twos-complement arithmetic. |
3592 | | */ |
3593 | | static int64_t |
3594 | | tar_atol256(const char *_p, size_t char_cnt) |
3595 | 2.00k | { |
3596 | 2.00k | uint64_t l; |
3597 | 2.00k | const unsigned char *p = (const unsigned char *)_p; |
3598 | 2.00k | unsigned char c, neg; |
3599 | | |
3600 | | /* Extend 7-bit 2s-comp to 8-bit 2s-comp, decide sign. */ |
3601 | 2.00k | c = *p; |
3602 | 2.00k | if (c & 0x40) { |
3603 | 1.71k | neg = 0xff; |
3604 | 1.71k | c |= 0x80; |
3605 | 1.71k | l = ~ARCHIVE_LITERAL_ULL(0); |
3606 | 1.71k | } else { |
3607 | 290 | neg = 0; |
3608 | 290 | c &= 0x7f; |
3609 | 290 | l = 0; |
3610 | 290 | } |
3611 | | |
3612 | | /* If more than 8 bytes, check that we can ignore |
3613 | | * high-order bits without overflow. */ |
3614 | 3.16k | while (char_cnt > sizeof(int64_t)) { |
3615 | 1.25k | --char_cnt; |
3616 | 1.25k | if (c != neg) |
3617 | 91 | return neg ? INT64_MIN : INT64_MAX; |
3618 | 1.16k | c = *++p; |
3619 | 1.16k | } |
3620 | | |
3621 | | /* c is first byte that fits; if sign mismatch, return overflow */ |
3622 | 1.91k | if ((c ^ neg) & 0x80) { |
3623 | 148 | return neg ? INT64_MIN : INT64_MAX; |
3624 | 148 | } |
3625 | | |
3626 | | /* Accumulate remaining bytes. */ |
3627 | 14.0k | while (--char_cnt > 0) { |
3628 | 12.3k | l = (l << 8) | c; |
3629 | 12.3k | c = *++p; |
3630 | 12.3k | } |
3631 | 1.76k | l = (l << 8) | c; |
3632 | | /* Return signed twos-complement value. */ |
3633 | 1.76k | return (int64_t)(l); |
3634 | 1.91k | } |
3635 | | |
3636 | | /* |
3637 | | * Returns length of line (including trailing newline) |
3638 | | * or negative on error. 'start' argument is updated to |
3639 | | * point to first character of line. This avoids copying |
3640 | | * when possible. |
3641 | | */ |
3642 | | static ssize_t |
3643 | | readline(struct archive_read *a, struct tar *tar, const char **start, |
3644 | | ssize_t limit, int64_t *unconsumed) |
3645 | 3 | { |
3646 | 3 | ssize_t bytes_read; |
3647 | 3 | ssize_t total_size = 0; |
3648 | 3 | const void *p, *t; |
3649 | 3 | const char *s; |
3650 | | |
3651 | 3 | if (tar_flush_unconsumed(a, unconsumed) != ARCHIVE_OK) { |
3652 | 0 | return (ARCHIVE_FATAL); |
3653 | 0 | } |
3654 | | |
3655 | 3 | t = __archive_read_ahead(a, 1, &bytes_read); |
3656 | 3 | if (bytes_read <= 0 || t == NULL) |
3657 | 0 | return (ARCHIVE_FATAL); |
3658 | 3 | s = t; /* Start of line? */ |
3659 | 3 | p = memchr(t, '\n', bytes_read); |
3660 | | /* If we found '\n' in the read buffer, return pointer to that. */ |
3661 | 3 | if (p != NULL) { |
3662 | 3 | bytes_read = 1 + ((const char *)p) - s; |
3663 | 3 | if (bytes_read > limit) { |
3664 | 2 | archive_set_error(&a->archive, |
3665 | 2 | ARCHIVE_ERRNO_FILE_FORMAT, |
3666 | 2 | "Line too long"); |
3667 | 2 | return (ARCHIVE_FATAL); |
3668 | 2 | } |
3669 | 1 | *unconsumed = bytes_read; |
3670 | 1 | *start = s; |
3671 | 1 | return (bytes_read); |
3672 | 3 | } |
3673 | 0 | *unconsumed = bytes_read; |
3674 | | /* Otherwise, we need to accumulate in a line buffer. */ |
3675 | 0 | for (;;) { |
3676 | 0 | if (total_size + bytes_read > limit) { |
3677 | 0 | archive_set_error(&a->archive, |
3678 | 0 | ARCHIVE_ERRNO_FILE_FORMAT, |
3679 | 0 | "Line too long"); |
3680 | 0 | return (ARCHIVE_FATAL); |
3681 | 0 | } |
3682 | 0 | if (archive_string_ensure(&tar->line, total_size + bytes_read) == NULL) { |
3683 | 0 | archive_set_error(&a->archive, ENOMEM, |
3684 | 0 | "Can't allocate working buffer"); |
3685 | 0 | return (ARCHIVE_FATAL); |
3686 | 0 | } |
3687 | 0 | memcpy(tar->line.s + total_size, t, bytes_read); |
3688 | 0 | tar_flush_unconsumed(a, unconsumed); |
3689 | 0 | total_size += bytes_read; |
3690 | | /* If we found '\n', clean up and return. */ |
3691 | 0 | if (p != NULL) { |
3692 | 0 | *start = tar->line.s; |
3693 | 0 | return (total_size); |
3694 | 0 | } |
3695 | | /* Read some more. */ |
3696 | 0 | t = __archive_read_ahead(a, 1, &bytes_read); |
3697 | 0 | if (bytes_read <= 0 || t == NULL) |
3698 | 0 | return (ARCHIVE_FATAL); |
3699 | 0 | s = t; /* Start of line? */ |
3700 | 0 | p = memchr(t, '\n', bytes_read); |
3701 | | /* If we found '\n', trim the read. */ |
3702 | 0 | if (p != NULL) { |
3703 | 0 | bytes_read = 1 + ((const char *)p) - s; |
3704 | 0 | } |
3705 | 0 | *unconsumed = bytes_read; |
3706 | 0 | } |
3707 | 0 | } |
3708 | | |
3709 | | /* |
3710 | | * base64_decode - Base64 decode |
3711 | | * |
3712 | | * This accepts most variations of base-64 encoding, including: |
3713 | | * * with or without line breaks |
3714 | | * * with or without the final group padded with '=' or '_' characters |
3715 | | * (The most economical Base-64 variant does not pad the last group and |
3716 | | * omits line breaks; RFC1341 used for MIME requires both.) |
3717 | | */ |
3718 | | static char * |
3719 | | base64_decode(const char *s, size_t len, size_t *out_len) |
3720 | 36 | { |
3721 | 36 | static const unsigned char decode_table[128] = { |
3722 | 36 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
3723 | 36 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
3724 | 36 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, |
3725 | 36 | 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63, |
3726 | 36 | 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 255, |
3727 | 36 | 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, |
3728 | 36 | 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, |
3729 | 36 | 255, 255, 255, 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, |
3730 | 36 | 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, |
3731 | 36 | 51, 255, 255, 255, 255, 255 }; |
3732 | 36 | char *out, *d; |
3733 | 36 | const unsigned char *src = (const unsigned char *)s; |
3734 | | |
3735 | | /* Allocate enough space to hold the entire output. */ |
3736 | | /* Note that we may not use all of this... */ |
3737 | 36 | out = malloc(len - len / 4 + 1); |
3738 | 36 | if (out == NULL) { |
3739 | 0 | *out_len = 0; |
3740 | 0 | return (NULL); |
3741 | 0 | } |
3742 | 36 | d = out; |
3743 | | |
3744 | 975 | while (len > 0) { |
3745 | | /* Collect the next group of (up to) four characters. */ |
3746 | 939 | int v = 0; |
3747 | 939 | int group_size = 0; |
3748 | 13.1k | while (group_size < 4 && len > 0) { |
3749 | | /* '=' or '_' padding indicates final group. */ |
3750 | 12.1k | if (*src == '=' || *src == '_') { |
3751 | 30 | len = 0; |
3752 | 30 | break; |
3753 | 30 | } |
3754 | | /* Skip illegal characters (including line breaks) */ |
3755 | 12.1k | if (*src > 127 || *src < 32 |
3756 | 8.49k | || decode_table[*src] == 0xff) { |
3757 | 8.49k | len--; |
3758 | 8.49k | src++; |
3759 | 8.49k | continue; |
3760 | 8.49k | } |
3761 | 3.66k | v <<= 6; |
3762 | 3.66k | v |= decode_table[*src++]; |
3763 | 3.66k | len --; |
3764 | 3.66k | group_size++; |
3765 | 3.66k | } |
3766 | | /* Align a short group properly. */ |
3767 | 939 | v <<= 6 * (4 - group_size); |
3768 | | /* Unpack the group we just collected. */ |
3769 | 939 | switch (group_size) { |
3770 | 903 | case 4: d[2] = v & 0xff; |
3771 | | /* FALLTHROUGH */ |
3772 | 913 | case 3: d[1] = (v >> 8) & 0xff; |
3773 | | /* FALLTHROUGH */ |
3774 | 921 | case 2: d[0] = (v >> 16) & 0xff; |
3775 | 921 | break; |
3776 | 9 | case 1: /* this is invalid! */ |
3777 | 9 | break; |
3778 | 939 | } |
3779 | 939 | d += group_size * 3 / 4; |
3780 | 939 | } |
3781 | | |
3782 | 36 | *out_len = d - out; |
3783 | 36 | return (out); |
3784 | 36 | } |
3785 | | |
3786 | | static char * |
3787 | | url_decode(const char *in, size_t length) |
3788 | 36 | { |
3789 | 36 | char *out, *d; |
3790 | 36 | const char *s; |
3791 | | |
3792 | 36 | out = malloc(length + 1); |
3793 | 36 | if (out == NULL) |
3794 | 0 | return (NULL); |
3795 | 155 | for (s = in, d = out; length > 0 && *s != '\0'; ) { |
3796 | 119 | if (s[0] == '%' && length > 2) { |
3797 | | /* Try to convert % escape */ |
3798 | 9 | int digit1 = tohex(s[1]); |
3799 | 9 | int digit2 = tohex(s[2]); |
3800 | 9 | if (digit1 >= 0 && digit2 >= 0) { |
3801 | | /* Looks good, consume three chars */ |
3802 | 0 | s += 3; |
3803 | 0 | length -= 3; |
3804 | | /* Convert output */ |
3805 | 0 | *d++ = ((digit1 << 4) | digit2); |
3806 | 0 | continue; |
3807 | 0 | } |
3808 | | /* Else fall through and treat '%' as normal char */ |
3809 | 9 | } |
3810 | 119 | *d++ = *s++; |
3811 | 119 | --length; |
3812 | 119 | } |
3813 | 36 | *d = '\0'; |
3814 | 36 | return (out); |
3815 | 36 | } |
3816 | | |
3817 | | static int |
3818 | | tohex(int c) |
3819 | 18 | { |
3820 | 18 | if (c >= '0' && c <= '9') |
3821 | 0 | return (c - '0'); |
3822 | 18 | else if (c >= 'A' && c <= 'F') |
3823 | 0 | return (c - 'A' + 10); |
3824 | 18 | else if (c >= 'a' && c <= 'f') |
3825 | 8 | return (c - 'a' + 10); |
3826 | 10 | else |
3827 | 10 | return (-1); |
3828 | 18 | } |