| 1 |
|
| /* |
| 2 |
|
| * tar.c - built-in tar plugin for File::Raw::Archive. |
| 3 |
|
| * |
| 4 |
|
| * Reads ustar (POSIX 1988), GNU `././@LongLink` extension for long |
| 5 |
|
| * filenames / symlink targets, and PAX extended headers (POSIX |
| 6 |
|
| * 1003.1-2001 typeflags 'x' and 'g'). Writes ustar + GNU @LongLink + |
| 7 |
|
| * PAX 'x' + optional PAX 'g' header at archive start, with a `format` |
| 8 |
|
| * option selecting auto / pax / gnu / ustar emission strategy. |
| 9 |
|
| * |
| 10 |
|
| * PAX keys handled on read AND write: path, linkpath, size, mtime |
| 11 |
|
| * (with nanoseconds), atime, uid, gid, uname, gname, plus |
| 12 |
|
| * SCHILY.xattr.* and LIBARCHIVE.xattr.* (optionally .b64 suffix for |
| 13 |
|
| * binary values). Sparse-file PAX keys flag the entry but don't |
| 14 |
|
| * reconstruct. |
| 15 |
|
| */ |
| 16 |
|
| |
| 17 |
|
| #define PERL_NO_GET_CONTEXT |
| 18 |
|
| |
| 19 |
|
| /* See the NO_XSLOCKS note in Archive.xs: under Strawberry's |
| 20 |
|
| * PERL_IMPLICIT_SYS, XSUB.h would rewrite this file's malloc/free into |
| 21 |
|
| * perl's allocator while its sibling translation units keep libc's, and |
| 22 |
|
| * a buffer allocated here would be freed to the wrong pool. */ |
| 23 |
|
| #define NO_XSLOCKS |
| 24 |
|
| |
| 25 |
|
| #include "EXTERN.h" |
| 26 |
|
| #include "perl.h" |
| 27 |
|
| #include "XSUB.h" |
| 28 |
|
| |
| 29 |
|
| #include "archive_plugin.h" |
| 30 |
|
| #include "arch_io.h" |
| 31 |
|
| #include "tar.h" |
| 32 |
|
| |
| 33 |
|
| #include <ctype.h> |
| 34 |
|
| #include <stdint.h> |
| 35 |
|
| #include <stdio.h> |
| 36 |
|
| #include <stdlib.h> |
| 37 |
|
| #include <string.h> |
| 38 |
|
| #include <time.h> |
| 39 |
|
| |
| 40 |
|
| /* ============================================================ |
| 41 |
|
| * Octal / numeric helpers |
| 42 |
|
| * ============================================================ */ |
| 43 |
|
| |
| 44 |
|
| /* Parse a NUL- or space-terminated octal field of `n` bytes. Honours |
| 45 |
|
| * the GNU base-256 extension when bit 7 of the first byte is set |
| 46 |
|
| * (used for size > 8 GiB, mtime, uid, etc.). */ |
| 47 |
|
| static uint64_t |
| 48 |
5069 |
| tar_parse_numeric(const char *field, size_t n) |
| 49 |
|
| { |
| 50 |
5069 |
▶ | if (n == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) return 0; |
0 |
5069 |
5069 |
|
| 51 |
5069 |
▶ | if ((unsigned char)field[0] & 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((unsigned char)field[0] & 0x80) { |
4 |
5065 |
5069 |
|
| 52 |
|
| /* Base-256: first byte's bit 7 is the marker (and bit 6 is |
| 53 |
|
| * the sign bit; we treat negative as 0). */ |
| 54 |
4 |
| uint64_t v = 0; |
| 55 |
|
| size_t i; |
| 56 |
4 |
▶ | if ((unsigned char)field[0] & 0x40) return 0; /* negative */ |
Branch: 1 branch
| Branch | True | False | Total |
| if ((unsigned char)field[0] & 0x40) return 0; /* negative */ |
0 |
4 |
4 |
|
| 57 |
4 |
| v = (unsigned char)field[0] & 0x3f; |
| 58 |
32 |
▶ | for (i = 1; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < n; i++) { |
28 |
4 |
32 |
|
| 59 |
28 |
| v = (v << 8) | (unsigned char)field[i]; |
| 60 |
|
| } |
| 61 |
4 |
| return v; |
| 62 |
|
| } |
| 63 |
|
| /* Octal ASCII, ignoring leading spaces and trailing spaces/NUL. */ |
| 64 |
5065 |
| uint64_t v = 0; |
| 65 |
5065 |
| size_t i = 0; |
| 66 |
37804 |
▶ | while (i < n && (field[i] == ' ' || field[i] == '0')) i++; |
Branch: 3 branches
| Branch | True | False | Total |
| while (i < n && (field[i] == ' ' || field[i] == '0')) i++; |
37804 |
0 |
37804 |
| while (i < n && (field[i] == ' ' || field[i] == '0')) i++; |
56 |
37748 |
37804 |
| while (i < n && (field[i] == ' ' || field[i] == '0')) i++; |
32683 |
5065 |
37748 |
|
| 67 |
|
| /* If we ate everything as zeros/spaces, value is 0 (handles all-zero fields). */ |
| 68 |
13683 |
▶ | while (i < n && field[i] >= '0' && field[i] <= '7') { |
Branch: 3 branches
| Branch | True | False | Total |
| while (i < n && field[i] >= '0' && field[i] <= '7') { |
13683 |
0 |
13683 |
| while (i < n && field[i] >= '0' && field[i] <= '7') { |
8619 |
5064 |
13683 |
| while (i < n && field[i] >= '0' && field[i] <= '7') { |
8618 |
1 |
8619 |
|
| 69 |
8618 |
| v = (v << 3) | (uint64_t)(field[i] - '0'); |
| 70 |
8618 |
| i++; |
| 71 |
|
| } |
| 72 |
5065 |
| return v; |
| 73 |
|
| } |
| 74 |
|
| |
| 75 |
|
| /* Emit an octal value into a fixed-width field, NUL-terminated. |
| 76 |
|
| * For values that don't fit, emit base-256 with the high bit set on |
| 77 |
|
| * the first byte. */ |
| 78 |
|
| static void |
| 79 |
3440 |
| tar_emit_numeric(char *field, size_t n, uint64_t v) |
| 80 |
|
| { |
| 81 |
|
| /* Try octal first. Field width n means n-1 digits (last byte NUL). */ |
| 82 |
3440 |
| int fits_octal = 1; |
| 83 |
3440 |
| uint64_t check = v; |
| 84 |
3440 |
| size_t digits = 0; |
| 85 |
3440 |
▶ | if (check == 0) digits = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (check == 0) digits = 1; |
2041 |
1399 |
3440 |
|
| 86 |
7052 |
▶ | while (check) { digits++; check >>= 3; } |
Branch: 1 branch
| Branch | True | False | Total |
| while (check) { digits++; check >>= 3; } |
3612 |
3440 |
7052 |
|
| 87 |
3440 |
▶ | if (digits > n - 1) fits_octal = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (digits > n - 1) fits_octal = 0; |
4 |
3436 |
3440 |
|
| 88 |
|
| |
| 89 |
3440 |
▶ | if (fits_octal) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (fits_octal) { |
3436 |
4 |
3440 |
|
| 90 |
|
| size_t i; |
| 91 |
3436 |
| field[n - 1] = '\0'; |
| 92 |
33040 |
▶ | for (i = n - 1; i > 0; i--) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = n - 1; i > 0; i--) { |
29604 |
3436 |
33040 |
|
| 93 |
29604 |
| field[i - 1] = (char)('0' + (v & 7)); |
| 94 |
29604 |
| v >>= 3; |
| 95 |
|
| } |
| 96 |
|
| } else { |
| 97 |
|
| /* Base-256, big-endian, with high bit set on first byte. */ |
| 98 |
|
| size_t i; |
| 99 |
36 |
▶ | for (i = n; i > 0; i--) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = n; i > 0; i--) { |
32 |
4 |
36 |
|
| 100 |
32 |
| field[i - 1] = (char)(v & 0xff); |
| 101 |
32 |
| v >>= 8; |
| 102 |
|
| } |
| 103 |
4 |
| field[0] |= 0x80; |
| 104 |
|
| } |
| 105 |
3440 |
| } |
| 106 |
|
| |
| 107 |
|
| /* Compute the standard tar header checksum: sum of all bytes treating |
| 108 |
|
| * the chksum field itself as eight spaces. */ |
| 109 |
|
| static uint32_t |
| 110 |
1551 |
| tar_compute_checksum(const tar_header_t *h) |
| 111 |
|
| { |
| 112 |
1551 |
| const unsigned char *p = (const unsigned char *)h; |
| 113 |
1551 |
| uint32_t sum = 0; |
| 114 |
|
| size_t i; |
| 115 |
795663 |
▶ | for (i = 0; i < TAR_BLOCK_SIZE; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < TAR_BLOCK_SIZE; i++) { |
794112 |
1551 |
795663 |
|
| 116 |
794112 |
▶ | if (i >= offsetof(tar_header_t, chksum) && |
Branch: 2 branches
| Branch | True | False | Total |
| if (i >= offsetof(tar_header_t, chksum) && |
564564 |
229548 |
794112 |
| if (i >= offsetof(tar_header_t, chksum) && |
12408 |
552156 |
564564 |
|
| 117 |
|
| i < offsetof(tar_header_t, chksum) + sizeof h->chksum) { |
| 118 |
12408 |
| sum += ' '; |
| 119 |
|
| } else { |
| 120 |
781704 |
| sum += p[i]; |
| 121 |
|
| } |
| 122 |
|
| } |
| 123 |
1551 |
| return sum; |
| 124 |
|
| } |
| 125 |
|
| |
| 126 |
|
| static int |
| 127 |
894 |
| tar_block_is_zero(const char *block) |
| 128 |
|
| { |
| 129 |
|
| size_t i; |
| 130 |
19838 |
▶ | for (i = 0; i < TAR_BLOCK_SIZE; i++) if (block[i]) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < TAR_BLOCK_SIZE; i++) if (block[i]) return 0; |
857 |
18944 |
19801 |
| for (i = 0; i < TAR_BLOCK_SIZE; i++) if (block[i]) return 0; |
19801 |
37 |
19838 |
|
| 131 |
37 |
| return 1; |
| 132 |
|
| } |
| 133 |
|
| |
| 134 |
|
| /* ============================================================ |
| 135 |
|
| * PAX record parsing / emission |
| 136 |
|
| * ============================================================ */ |
| 137 |
|
| |
| 138 |
|
| typedef struct pax_kv { |
| 139 |
|
| char *key; |
| 140 |
|
| char *value; |
| 141 |
|
| size_t value_len; |
| 142 |
|
| struct pax_kv *next; |
| 143 |
|
| } pax_kv_t; |
| 144 |
|
| |
| 145 |
|
| static void |
| 146 |
999 |
| pax_kv_free(pax_kv_t *list) |
| 147 |
|
| { |
| 148 |
1034 |
▶ | while (list) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (list) { |
35 |
999 |
1034 |
|
| 149 |
35 |
| pax_kv_t *n = list->next; |
| 150 |
35 |
| free(list->key); |
| 151 |
35 |
| free(list->value); |
| 152 |
35 |
| free(list); |
| 153 |
35 |
| list = n; |
| 154 |
|
| } |
| 155 |
999 |
| } |
| 156 |
|
| |
| 157 |
|
| static void |
| 158 |
35 |
| pax_kv_set(pax_kv_t **list, const char *key, const char *value, size_t value_len) |
| 159 |
|
| { |
| 160 |
|
| pax_kv_t *cur; |
| 161 |
83 |
▶ | for (cur = *list; cur; cur = cur->next) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (cur = *list; cur; cur = cur->next) { |
48 |
35 |
83 |
|
| 162 |
48 |
▶ | if (strcmp(cur->key, key) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(cur->key, key) == 0) { |
0 |
48 |
48 |
|
| 163 |
0 |
| free(cur->value); |
| 164 |
0 |
| cur->value = (char *)malloc(value_len + 1); |
| 165 |
0 |
| memcpy(cur->value, value, value_len); |
| 166 |
0 |
| cur->value[value_len] = '\0'; |
| 167 |
0 |
| cur->value_len = value_len; |
| 168 |
0 |
| return; |
| 169 |
|
| } |
| 170 |
|
| } |
| 171 |
35 |
| pax_kv_t *fresh = (pax_kv_t *)calloc(1, sizeof *fresh); |
| 172 |
35 |
| fresh->key = strdup(key); |
| 173 |
35 |
| fresh->value = (char *)malloc(value_len + 1); |
| 174 |
35 |
| memcpy(fresh->value, value, value_len); |
| 175 |
35 |
| fresh->value[value_len] = '\0'; |
| 176 |
35 |
| fresh->value_len = value_len; |
| 177 |
35 |
| fresh->next = *list; |
| 178 |
35 |
| *list = fresh; |
| 179 |
|
| } |
| 180 |
|
| |
| 181 |
|
| static const pax_kv_t * |
| 182 |
0 |
| pax_kv_find(const pax_kv_t *list, const char *key) |
| 183 |
|
| { |
| 184 |
0 |
▶ | while (list) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (list) { |
0 |
0 |
0 |
|
| 185 |
0 |
▶ | if (strcmp(list->key, key) == 0) return list; |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(list->key, key) == 0) return list; |
0 |
0 |
0 |
|
| 186 |
0 |
| list = list->next; |
| 187 |
|
| } |
| 188 |
0 |
| return NULL; |
| 189 |
|
| } |
| 190 |
|
| |
| 191 |
|
| /* Parse a buffer of length `len` as a sequence of PAX records: |
| 192 |
|
| * "<L> <key>=<value>\n" |
| 193 |
|
| * where <L> is the ASCII byte length of the whole record. */ |
| 194 |
|
| static int |
| 195 |
14 |
| pax_parse_records(const char *buf, size_t len, pax_kv_t **out) |
| 196 |
|
| { |
| 197 |
14 |
| size_t pos = 0; |
| 198 |
49 |
▶ | while (pos < len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (pos < len) { |
35 |
14 |
49 |
|
| 199 |
35 |
| size_t L = 0; |
| 200 |
35 |
| size_t lstart = pos; |
| 201 |
101 |
▶ | while (pos < len && buf[pos] >= '0' && buf[pos] <= '9') { |
Branch: 3 branches
| Branch | True | False | Total |
| while (pos < len && buf[pos] >= '0' && buf[pos] <= '9') { |
101 |
0 |
101 |
| while (pos < len && buf[pos] >= '0' && buf[pos] <= '9') { |
66 |
35 |
101 |
| while (pos < len && buf[pos] >= '0' && buf[pos] <= '9') { |
66 |
0 |
66 |
|
| 202 |
66 |
| L = L * 10 + (buf[pos] - '0'); |
| 203 |
66 |
| pos++; |
| 204 |
|
| } |
| 205 |
35 |
▶ | if (pos == lstart || pos >= len || buf[pos] != ' ') return -1; |
Branch: 3 branches
| Branch | True | False | Total |
| if (pos == lstart || pos >= len || buf[pos] != ' ') return -1; |
35 |
0 |
35 |
| if (pos == lstart || pos >= len || buf[pos] != ' ') return -1; |
35 |
0 |
35 |
| if (pos == lstart || pos >= len || buf[pos] != ' ') return -1; |
0 |
35 |
35 |
|
| 206 |
35 |
| pos++; /* skip space */ |
| 207 |
35 |
▶ | if (lstart + L > len) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (lstart + L > len) return -1; |
0 |
35 |
35 |
|
| 208 |
|
| /* The record minus the digits + space is "kw=value\n" */ |
| 209 |
35 |
| size_t rec_inner_end = lstart + L - 1; /* position of '\n' */ |
| 210 |
35 |
▶ | if (rec_inner_end >= len || buf[rec_inner_end] != '\n') return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (rec_inner_end >= len || buf[rec_inner_end] != '\n') return -1; |
35 |
0 |
35 |
| if (rec_inner_end >= len || buf[rec_inner_end] != '\n') return -1; |
0 |
35 |
35 |
|
| 211 |
35 |
| const char *eq = (const char *)memchr(buf + pos, '=', rec_inner_end - pos); |
| 212 |
35 |
▶ | if (!eq) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!eq) return -1; |
0 |
35 |
35 |
|
| 213 |
35 |
| size_t key_len = eq - (buf + pos); |
| 214 |
35 |
| size_t value_len = rec_inner_end - (eq - buf) - 1; |
| 215 |
35 |
| char *key = (char *)malloc(key_len + 1); |
| 216 |
35 |
| memcpy(key, buf + pos, key_len); |
| 217 |
35 |
| key[key_len] = '\0'; |
| 218 |
|
| /* pax_kv_set duplicates value; we'll free key after. */ |
| 219 |
35 |
| pax_kv_set(out, key, eq + 1, value_len); |
| 220 |
35 |
| free(key); |
| 221 |
35 |
| pos = lstart + L; |
| 222 |
|
| } |
| 223 |
14 |
| return 0; |
| 224 |
|
| } |
| 225 |
|
| |
| 226 |
|
| /* Build one PAX record into `out_buf` (caller-provided, must be big |
| 227 |
|
| * enough). Returns the byte length. The fixed-point challenge: the |
| 228 |
|
| * leading L includes its own digit count. */ |
| 229 |
|
| static size_t |
| 230 |
33 |
| pax_build_record(char *out_buf, const char *key, const char *value, size_t value_len) |
| 231 |
|
| { |
| 232 |
33 |
| size_t key_len = strlen(key); |
| 233 |
|
| /* Inner = key + '=' + value + '\n' */ |
| 234 |
33 |
| size_t inner = key_len + 1 + value_len + 1; |
| 235 |
|
| /* L is a positive integer; its digit count + 1 (for the space) + inner = total. */ |
| 236 |
33 |
| size_t L = inner + 2; /* start with 1-digit guess */ |
| 237 |
26 |
| for (;;) { |
| 238 |
|
| char tmp[32]; |
| 239 |
59 |
| int dlen = snprintf(tmp, sizeof tmp, "%zu", L); |
| 240 |
59 |
| size_t total = (size_t)dlen + 1 + inner; |
| 241 |
59 |
▶ | if (total == L) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (total == L) break; |
33 |
26 |
59 |
|
| 242 |
26 |
| L = total; |
| 243 |
|
| } |
| 244 |
33 |
| int dlen = snprintf(out_buf, 32, "%zu ", L); |
| 245 |
33 |
| memcpy(out_buf + dlen, key, key_len); |
| 246 |
33 |
| out_buf[dlen + key_len] = '='; |
| 247 |
33 |
| memcpy(out_buf + dlen + key_len + 1, value, value_len); |
| 248 |
33 |
| out_buf[L - 1] = '\n'; |
| 249 |
33 |
| return L; |
| 250 |
|
| } |
| 251 |
|
| |
| 252 |
|
| /* ============================================================ |
| 253 |
|
| * Base64 (for SCHILY.xattr.*.b64 binary values) |
| 254 |
|
| * ============================================================ */ |
| 255 |
|
| |
| 256 |
|
| static const char b64_alphabet[] = |
| 257 |
|
| "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 258 |
|
| |
| 259 |
|
| static int |
| 260 |
24 |
| b64_decode_byte(int c) |
| 261 |
|
| { |
| 262 |
24 |
▶ | if (c >= 'A' && c <= 'Z') return c - 'A'; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= 'A' && c <= 'Z') return c - 'A'; |
20 |
4 |
24 |
| if (c >= 'A' && c <= 'Z') return c - 'A'; |
14 |
6 |
20 |
|
| 263 |
10 |
▶ | if (c >= 'a' && c <= 'z') return c - 'a' + 26; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= 'a' && c <= 'z') return c - 'a' + 26; |
6 |
4 |
10 |
| if (c >= 'a' && c <= 'z') return c - 'a' + 26; |
6 |
0 |
6 |
|
| 264 |
4 |
▶ | if (c >= '0' && c <= '9') return c - '0' + 52; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= '0' && c <= '9') return c - '0' + 52; |
4 |
0 |
4 |
| if (c >= '0' && c <= '9') return c - '0' + 52; |
4 |
0 |
4 |
|
| 265 |
0 |
▶ | if (c == '+') return 62; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '+') return 62; |
0 |
0 |
0 |
|
| 266 |
0 |
▶ | if (c == '/') return 63; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '/') return 63; |
0 |
0 |
0 |
|
| 267 |
0 |
| return -1; |
| 268 |
|
| } |
| 269 |
|
| |
| 270 |
|
| /* malloc-allocates *out; caller frees. Returns 0 on success, -1 on |
| 271 |
|
| * malformed input. */ |
| 272 |
|
| static int |
| 273 |
2 |
| b64_decode(const char *in, size_t in_len, char **out, size_t *out_len) |
| 274 |
|
| { |
| 275 |
2 |
| size_t cap = (in_len / 4) * 3 + 4; |
| 276 |
2 |
| char *buf = (char *)malloc(cap); |
| 277 |
2 |
▶ | if (!buf) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!buf) return -1; |
0 |
2 |
2 |
|
| 278 |
2 |
| size_t bp = 0; |
| 279 |
2 |
| int quad[4] = { -1, -1, -1, -1 }; |
| 280 |
2 |
| int qi = 0; |
| 281 |
|
| size_t i; |
| 282 |
26 |
▶ | for (i = 0; i < in_len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < in_len; i++) { |
24 |
2 |
26 |
|
| 283 |
24 |
| int c = (unsigned char)in[i]; |
| 284 |
24 |
▶ | if (c == '=' || c == '\r' || c == '\n' || c == ' ' || c == '\t') { |
Branch: 5 branches
| Branch | True | False | Total |
| if (c == '=' || c == '\r' || c == '\n' || c == ' ' || c == '\t') { |
24 |
0 |
24 |
| if (c == '=' || c == '\r' || c == '\n' || c == ' ' || c == '\t') { |
24 |
0 |
24 |
| if (c == '=' || c == '\r' || c == '\n' || c == ' ' || c == '\t') { |
24 |
0 |
24 |
| if (c == '=' || c == '\r' || c == '\n' || c == ' ' || c == '\t') { |
24 |
0 |
24 |
| if (c == '=' || c == '\r' || c == '\n' || c == ' ' || c == '\t') { |
0 |
24 |
24 |
|
| 285 |
0 |
▶ | if (c == '=') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '=') { |
0 |
0 |
0 |
|
| 286 |
0 |
| quad[qi++] = -2; |
| 287 |
0 |
▶ | if (qi == 4) goto flush; |
Branch: 1 branch
| Branch | True | False | Total |
| if (qi == 4) goto flush; |
0 |
0 |
0 |
|
| 288 |
|
| } |
| 289 |
0 |
| continue; |
| 290 |
|
| } |
| 291 |
24 |
| int v = b64_decode_byte(c); |
| 292 |
24 |
▶ | if (v < 0) { free(buf); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (v < 0) { free(buf); return -1; } |
0 |
24 |
24 |
|
| 293 |
24 |
| quad[qi++] = v; |
| 294 |
24 |
▶ | if (qi == 4) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (qi == 4) { |
6 |
18 |
24 |
|
| 295 |
6 |
| flush: |
| 296 |
6 |
▶ | if (quad[0] < 0 || quad[1] < 0) { free(buf); return -1; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (quad[0] < 0 || quad[1] < 0) { free(buf); return -1; } |
6 |
0 |
6 |
| if (quad[0] < 0 || quad[1] < 0) { free(buf); return -1; } |
0 |
6 |
6 |
|
| 297 |
6 |
| buf[bp++] = (char)(((quad[0] & 0x3f) << 2) | ((quad[1] & 0x30) >> 4)); |
| 298 |
6 |
▶ | if (quad[2] >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (quad[2] >= 0) { |
6 |
0 |
6 |
|
| 299 |
6 |
| buf[bp++] = (char)(((quad[1] & 0x0f) << 4) | ((quad[2] & 0x3c) >> 2)); |
| 300 |
6 |
▶ | if (quad[3] >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (quad[3] >= 0) { |
6 |
0 |
6 |
|
| 301 |
6 |
| buf[bp++] = (char)(((quad[2] & 0x03) << 6) | (quad[3] & 0x3f)); |
| 302 |
|
| } |
| 303 |
|
| } |
| 304 |
6 |
| qi = 0; |
| 305 |
6 |
| quad[0] = quad[1] = quad[2] = quad[3] = -1; |
| 306 |
|
| } |
| 307 |
|
| } |
| 308 |
2 |
| *out = buf; |
| 309 |
2 |
| *out_len = bp; |
| 310 |
2 |
| return 0; |
| 311 |
|
| } |
| 312 |
|
| |
| 313 |
|
| static char * |
| 314 |
2 |
| b64_encode(const char *in, size_t in_len) |
| 315 |
|
| { |
| 316 |
2 |
| size_t out_len = 4 * ((in_len + 2) / 3); |
| 317 |
2 |
| char *out = (char *)malloc(out_len + 1); |
| 318 |
2 |
▶ | if (!out) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!out) return NULL; |
0 |
2 |
2 |
|
| 319 |
2 |
| size_t i, op = 0; |
| 320 |
8 |
▶ | for (i = 0; i + 2 < in_len; i += 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i + 2 < in_len; i += 3) { |
6 |
2 |
8 |
|
| 321 |
6 |
| unsigned a = (unsigned char)in[i]; |
| 322 |
6 |
| unsigned b = (unsigned char)in[i+1]; |
| 323 |
6 |
| unsigned c = (unsigned char)in[i+2]; |
| 324 |
6 |
| out[op++] = b64_alphabet[a >> 2]; |
| 325 |
6 |
| out[op++] = b64_alphabet[((a & 3) << 4) | (b >> 4)]; |
| 326 |
6 |
| out[op++] = b64_alphabet[((b & 0xf) << 2) | (c >> 6)]; |
| 327 |
6 |
| out[op++] = b64_alphabet[c & 0x3f]; |
| 328 |
|
| } |
| 329 |
2 |
▶ | if (i < in_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i < in_len) { |
0 |
2 |
2 |
|
| 330 |
0 |
| unsigned a = (unsigned char)in[i]; |
| 331 |
0 |
▶ | unsigned b = (i+1 < in_len) ? (unsigned char)in[i+1] : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| unsigned b = (i+1 < in_len) ? (unsigned char)in[i+1] : 0; |
0 |
0 |
0 |
|
| 332 |
0 |
| out[op++] = b64_alphabet[a >> 2]; |
| 333 |
0 |
| out[op++] = b64_alphabet[((a & 3) << 4) | (b >> 4)]; |
| 334 |
0 |
▶ | if (i + 1 < in_len) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (i + 1 < in_len) { |
0 |
0 |
0 |
|
| 335 |
0 |
| out[op++] = b64_alphabet[(b & 0xf) << 2]; |
| 336 |
|
| } else { |
| 337 |
0 |
| out[op++] = '='; |
| 338 |
|
| } |
| 339 |
0 |
| out[op++] = '='; |
| 340 |
|
| } |
| 341 |
2 |
| out[op] = '\0'; |
| 342 |
2 |
| return out; |
| 343 |
|
| } |
| 344 |
|
| |
| 345 |
|
| static int |
| 346 |
5 |
| needs_b64(const char *value, size_t value_len) |
| 347 |
|
| { |
| 348 |
|
| size_t i; |
| 349 |
20 |
▶ | for (i = 0; i < value_len; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < value_len; i++) { |
17 |
3 |
20 |
|
| 350 |
17 |
| unsigned c = (unsigned char)value[i]; |
| 351 |
17 |
▶ | if (c == 0 || c == '\n' || c < 0x20 || c > 0x7e) return 1; |
Branch: 4 branches
| Branch | True | False | Total |
| if (c == 0 || c == '\n' || c < 0x20 || c > 0x7e) return 1; |
15 |
2 |
17 |
| if (c == 0 || c == '\n' || c < 0x20 || c > 0x7e) return 1; |
15 |
0 |
15 |
| if (c == 0 || c == '\n' || c < 0x20 || c > 0x7e) return 1; |
15 |
0 |
15 |
| if (c == 0 || c == '\n' || c < 0x20 || c > 0x7e) return 1; |
0 |
15 |
15 |
|
| 352 |
|
| } |
| 353 |
3 |
| return 0; |
| 354 |
|
| } |
| 355 |
|
| |
| 356 |
|
| /* ============================================================ |
| 357 |
|
| * Cursor state |
| 358 |
|
| * ============================================================ */ |
| 359 |
|
| |
| 360 |
|
| typedef struct { |
| 361 |
|
| archive_pull_fn pull; |
| 362 |
|
| void *src; |
| 363 |
|
| archive_push_fn push; |
| 364 |
|
| void *sink; |
| 365 |
|
| |
| 366 |
|
| pax_kv_t *global_kv; |
| 367 |
|
| pax_kv_t *next_kv; /* per-entry 'x' header, applied to next regular entry */ |
| 368 |
|
| |
| 369 |
|
| /* read-side: pending bytes of the current entry payload */ |
| 370 |
|
| uint64_t entry_remaining; |
| 371 |
|
| uint64_t entry_pad; /* bytes of zero padding still to consume after entry */ |
| 372 |
|
| |
| 373 |
|
| /* read-side: storage for the current entry's strings/xattrs. |
| 374 |
|
| * Lifetime: until the next read_next call. */ |
| 375 |
|
| char *cur_name; |
| 376 |
|
| char *cur_link; |
| 377 |
|
| ArchiveXattr *cur_xattrs; |
| 378 |
|
| size_t cur_xattr_count; |
| 379 |
|
| size_t cur_xattr_cap; |
| 380 |
|
| int sparse_warning_emitted; |
| 381 |
|
| |
| 382 |
|
| /* write-side: chosen format and counters */ |
| 383 |
|
| int format; /* 0=auto 1=pax 2=gnu 3=ustar */ |
| 384 |
|
| int level; /* gzip level for gz sink (informational) */ |
| 385 |
|
| /* PAX header sequence number for the conventional name. */ |
| 386 |
|
| int pax_header_seq; |
| 387 |
|
| } tar_cursor_t; |
| 388 |
|
| |
| 389 |
|
| #define FMT_AUTO 0 |
| 390 |
|
| #define FMT_PAX 1 |
| 391 |
|
| #define FMT_GNU 2 |
| 392 |
|
| #define FMT_USTAR 3 |
| 393 |
|
| |
| 394 |
|
| static int |
| 395 |
2899 |
| read_full(archive_pull_fn pull, void *src, char *buf, size_t len) |
| 396 |
|
| { |
| 397 |
2899 |
| size_t got = 0; |
| 398 |
5797 |
▶ | while (got < len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (got < len) { |
2900 |
2897 |
5797 |
|
| 399 |
2900 |
| int n = pull(src, buf + got, len - got); |
| 400 |
2900 |
▶ | if (n < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 0) return -1; |
1 |
2899 |
2900 |
|
| 401 |
2899 |
▶ | if (n == 0) return (int)got; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) return (int)got; |
1 |
2898 |
2899 |
|
| 402 |
2898 |
| got += (size_t)n; |
| 403 |
|
| } |
| 404 |
2897 |
| return (int)got; |
| 405 |
|
| } |
| 406 |
|
| |
| 407 |
|
| static int |
| 408 |
2105 |
| write_full(archive_push_fn push, void *sink, const char *buf, size_t len) |
| 409 |
|
| { |
| 410 |
2105 |
| size_t put = 0; |
| 411 |
4210 |
▶ | while (put < len) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (put < len) { |
2105 |
2105 |
4210 |
|
| 412 |
2105 |
| int n = push(sink, buf + put, len - put); |
| 413 |
2105 |
▶ | if (n < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 0) return -1; |
0 |
2105 |
2105 |
|
| 414 |
2105 |
▶ | if (n == 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) return -1; |
0 |
2105 |
2105 |
|
| 415 |
2105 |
| put += (size_t)n; |
| 416 |
|
| } |
| 417 |
2105 |
| return 0; |
| 418 |
|
| } |
| 419 |
|
| |
| 420 |
|
| static void |
| 421 |
954 |
| free_cur_buffers(tar_cursor_t *c) |
| 422 |
|
| { |
| 423 |
954 |
| free(c->cur_name); c->cur_name = NULL; |
| 424 |
954 |
| free(c->cur_link); c->cur_link = NULL; |
| 425 |
954 |
▶ | if (c->cur_xattrs) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->cur_xattrs) { |
3 |
951 |
954 |
|
| 426 |
|
| size_t i; |
| 427 |
8 |
▶ | for (i = 0; i < c->cur_xattr_count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < c->cur_xattr_count; i++) { |
5 |
3 |
8 |
|
| 428 |
5 |
| free((void *)c->cur_xattrs[i].key); |
| 429 |
5 |
| free((void *)c->cur_xattrs[i].value); |
| 430 |
|
| } |
| 431 |
3 |
| free(c->cur_xattrs); |
| 432 |
|
| } |
| 433 |
954 |
| c->cur_xattrs = NULL; |
| 434 |
954 |
| c->cur_xattr_count = 0; |
| 435 |
954 |
| c->cur_xattr_cap = 0; |
| 436 |
954 |
| } |
| 437 |
|
| |
| 438 |
|
| /* ============================================================ |
| 439 |
|
| * Read side |
| 440 |
|
| * ============================================================ */ |
| 441 |
|
| |
| 442 |
|
| static int |
| 443 |
75 |
| tar_read_open(pTHX_ const ArchivePlugin *self, archive_pull_fn pull, |
| 444 |
|
| void *src, HV *opts, void **out_cursor) |
| 445 |
|
| { |
| 446 |
|
| PERL_UNUSED_ARG(self); |
| 447 |
|
| PERL_UNUSED_ARG(opts); |
| 448 |
75 |
| tar_cursor_t *c = (tar_cursor_t *)calloc(1, sizeof *c); |
| 449 |
75 |
▶ | if (!c) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!c) return -1; |
0 |
75 |
75 |
|
| 450 |
75 |
| c->pull = pull; |
| 451 |
75 |
| c->src = src; |
| 452 |
75 |
| *out_cursor = c; |
| 453 |
75 |
| return 0; |
| 454 |
|
| } |
| 455 |
|
| |
| 456 |
|
| /* Skip the trailing zero padding of the previous entry, if any. */ |
| 457 |
|
| static int |
| 458 |
879 |
| consume_pending_padding(tar_cursor_t *c) |
| 459 |
|
| { |
| 460 |
|
| char buf[TAR_BLOCK_SIZE]; |
| 461 |
879 |
▶ | while (c->entry_remaining > 0 || c->entry_pad > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| while (c->entry_remaining > 0 || c->entry_pad > 0) { |
0 |
879 |
879 |
| while (c->entry_remaining > 0 || c->entry_pad > 0) { |
0 |
879 |
879 |
|
| 462 |
0 |
▶ | if (c->entry_remaining > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->entry_remaining > 0) { |
0 |
0 |
0 |
|
| 463 |
0 |
| size_t take = c->entry_remaining > sizeof buf ? sizeof buf : (size_t)c->entry_remaining; |
| 464 |
0 |
| int n = read_full(c->pull, c->src, buf, take); |
| 465 |
0 |
▶ | if (n < (int)take) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < (int)take) return -1; |
0 |
0 |
0 |
|
| 466 |
0 |
| c->entry_remaining -= take; |
| 467 |
|
| } |
| 468 |
0 |
▶ | if (c->entry_remaining == 0 && c->entry_pad > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c->entry_remaining == 0 && c->entry_pad > 0) { |
0 |
0 |
0 |
| if (c->entry_remaining == 0 && c->entry_pad > 0) { |
0 |
0 |
0 |
|
| 469 |
0 |
| size_t take = c->entry_pad > sizeof buf ? sizeof buf : (size_t)c->entry_pad; |
| 470 |
0 |
| int n = read_full(c->pull, c->src, buf, take); |
| 471 |
0 |
▶ | if (n < (int)take) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < (int)take) return -1; |
0 |
0 |
0 |
|
| 472 |
0 |
| c->entry_pad -= take; |
| 473 |
|
| } |
| 474 |
|
| } |
| 475 |
879 |
| return 0; |
| 476 |
|
| } |
| 477 |
|
| |
| 478 |
|
| /* Read a tar header block, return: |
| 479 |
|
| * 1 = got a valid header (in *h), 0 = end of archive, -1 = error */ |
| 480 |
|
| static int |
| 481 |
896 |
| read_one_header(tar_cursor_t *c, tar_header_t *h) |
| 482 |
|
| { |
| 483 |
|
| char buf[TAR_BLOCK_SIZE]; |
| 484 |
896 |
| int n = read_full(c->pull, c->src, buf, TAR_BLOCK_SIZE); |
| 485 |
896 |
▶ | if (n < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 0) return -1; |
1 |
895 |
896 |
|
| 486 |
895 |
▶ | if (n == 0) return 0; /* clean EOF */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (n == 0) return 0; /* clean EOF */ |
0 |
895 |
895 |
|
| 487 |
895 |
▶ | if (n < TAR_BLOCK_SIZE) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < TAR_BLOCK_SIZE) return -1; |
1 |
894 |
895 |
|
| 488 |
894 |
▶ | if (tar_block_is_zero(buf)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tar_block_is_zero(buf)) { |
37 |
857 |
894 |
|
| 489 |
|
| /* First zero block: try one more. Two zero blocks => end-of-archive. */ |
| 490 |
|
| char buf2[TAR_BLOCK_SIZE]; |
| 491 |
37 |
| int n2 = read_full(c->pull, c->src, buf2, TAR_BLOCK_SIZE); |
| 492 |
|
| (void)n2; /* Either way: end. */ |
| 493 |
37 |
| return 0; |
| 494 |
|
| } |
| 495 |
857 |
| memcpy(h, buf, sizeof *h); |
| 496 |
|
| |
| 497 |
|
| /* Verify checksum. */ |
| 498 |
857 |
| uint32_t stored = (uint32_t)tar_parse_numeric(h->chksum, sizeof h->chksum); |
| 499 |
857 |
| uint32_t actual = tar_compute_checksum(h); |
| 500 |
857 |
▶ | if (stored != actual) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (stored != actual) return -1; |
1 |
856 |
857 |
|
| 501 |
|
| |
| 502 |
856 |
| return 1; |
| 503 |
|
| } |
| 504 |
|
| |
| 505 |
|
| /* Read an entry payload of `size` bytes followed by zero padding into |
| 506 |
|
| * a malloced buffer. Caller frees. */ |
| 507 |
|
| static int |
| 508 |
17 |
| read_payload(tar_cursor_t *c, uint64_t size, char **out) |
| 509 |
|
| { |
| 510 |
17 |
| *out = NULL; |
| 511 |
17 |
▶ | if (size == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (size == 0) { |
0 |
17 |
17 |
|
| 512 |
0 |
| *out = (char *)malloc(1); |
| 513 |
0 |
▶ | return *out ? 0 : -1; |
Branch: 1 branch
| Branch | True | False | Total |
| return *out ? 0 : -1; |
0 |
0 |
0 |
|
| 514 |
|
| } |
| 515 |
17 |
| char *buf = (char *)malloc((size_t)size + 1); |
| 516 |
17 |
▶ | if (!buf) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!buf) return -1; |
0 |
17 |
17 |
|
| 517 |
17 |
| int n = read_full(c->pull, c->src, buf, (size_t)size); |
| 518 |
17 |
▶ | if (n < (int)size) { free(buf); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < (int)size) { free(buf); return -1; } |
0 |
17 |
17 |
|
| 519 |
17 |
| buf[size] = '\0'; |
| 520 |
|
| |
| 521 |
|
| /* Skip padding. */ |
| 522 |
17 |
| size_t pad = (TAR_BLOCK_SIZE - ((size_t)size % TAR_BLOCK_SIZE)) % TAR_BLOCK_SIZE; |
| 523 |
17 |
▶ | if (pad > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pad > 0) { |
17 |
0 |
17 |
|
| 524 |
|
| char pbuf[TAR_BLOCK_SIZE]; |
| 525 |
17 |
| int pn = read_full(c->pull, c->src, pbuf, pad); |
| 526 |
17 |
▶ | if (pn < (int)pad) { free(buf); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (pn < (int)pad) { free(buf); return -1; } |
0 |
17 |
17 |
|
| 527 |
|
| } |
| 528 |
17 |
| *out = buf; |
| 529 |
17 |
| return 0; |
| 530 |
|
| } |
| 531 |
|
| |
| 532 |
|
| /* Apply a per-entry PAX kv list onto entry `out`, parsing typed |
| 533 |
|
| * values. Xattr keys (SCHILY.xattr.* and LIBARCHIVE.xattr.*) accumulate |
| 534 |
|
| * into c->cur_xattrs[]. */ |
| 535 |
|
| static void |
| 536 |
16 |
| apply_pax_to_entry(tar_cursor_t *c, const pax_kv_t *list, ArchiveEntry *out) |
| 537 |
|
| { |
| 538 |
55 |
▶ | while (list) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (list) { |
39 |
16 |
55 |
|
| 539 |
39 |
| const char *k = list->key; |
| 540 |
39 |
| const char *v = list->value; |
| 541 |
39 |
| size_t vlen = list->value_len; |
| 542 |
39 |
▶ | if (strcmp(k, "path") == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(k, "path") == 0) { |
5 |
34 |
39 |
|
| 543 |
5 |
| free(c->cur_name); |
| 544 |
5 |
| c->cur_name = (char *)malloc(vlen + 1); |
| 545 |
5 |
| memcpy(c->cur_name, v, vlen); |
| 546 |
5 |
| c->cur_name[vlen] = '\0'; |
| 547 |
5 |
| out->name = c->cur_name; |
| 548 |
5 |
| out->name_len = vlen; |
| 549 |
34 |
▶ | } else if (strcmp(k, "linkpath") == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (strcmp(k, "linkpath") == 0) { |
0 |
34 |
34 |
|
| 550 |
0 |
| free(c->cur_link); |
| 551 |
0 |
| c->cur_link = (char *)malloc(vlen + 1); |
| 552 |
0 |
| memcpy(c->cur_link, v, vlen); |
| 553 |
0 |
| c->cur_link[vlen] = '\0'; |
| 554 |
0 |
| out->link_target = c->cur_link; |
| 555 |
0 |
| out->link_target_len = vlen; |
| 556 |
34 |
▶ | } else if (strcmp(k, "size") == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (strcmp(k, "size") == 0) { |
5 |
29 |
34 |
|
| 557 |
5 |
| out->size = strtoull(v, NULL, 10); |
| 558 |
29 |
▶ | } else if (strcmp(k, "mtime") == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (strcmp(k, "mtime") == 0) { |
6 |
23 |
29 |
|
| 559 |
6 |
| const char *dot = (const char *)memchr(v, '.', vlen); |
| 560 |
6 |
| out->mtime = strtoull(v, NULL, 10); |
| 561 |
6 |
▶ | if (dot && (size_t)(dot - v) < vlen - 1) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (dot && (size_t)(dot - v) < vlen - 1) { |
6 |
0 |
6 |
| if (dot && (size_t)(dot - v) < vlen - 1) { |
6 |
0 |
6 |
|
| 562 |
|
| /* Parse fractional part into nanoseconds (truncate/pad to 9 digits). */ |
| 563 |
6 |
| uint32_t ns = 0; |
| 564 |
6 |
| int seen = 0; |
| 565 |
|
| size_t i; |
| 566 |
60 |
▶ | for (i = (size_t)(dot - v + 1); i < vlen && seen < 9; i++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = (size_t)(dot - v + 1); i < vlen && seen < 9; i++) { |
54 |
6 |
60 |
| for (i = (size_t)(dot - v + 1); i < vlen && seen < 9; i++) { |
54 |
0 |
54 |
|
| 567 |
54 |
▶ | if (v[i] < '0' || v[i] > '9') break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (v[i] < '0' || v[i] > '9') break; |
54 |
0 |
54 |
| if (v[i] < '0' || v[i] > '9') break; |
54 |
0 |
54 |
|
| 568 |
54 |
| ns = ns * 10 + (uint32_t)(v[i] - '0'); |
| 569 |
54 |
| seen++; |
| 570 |
|
| } |
| 571 |
6 |
▶ | while (seen < 9) { ns *= 10; seen++; } |
Branch: 1 branch
| Branch | True | False | Total |
| while (seen < 9) { ns *= 10; seen++; } |
0 |
6 |
6 |
|
| 572 |
6 |
| out->mtime_ns = ns; |
| 573 |
|
| } |
| 574 |
23 |
▶ | } else if (strcmp(k, "uid") == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (strcmp(k, "uid") == 0) { |
5 |
18 |
23 |
|
| 575 |
5 |
| out->uid = (uint32_t)strtoul(v, NULL, 10); |
| 576 |
18 |
▶ | } else if (strcmp(k, "gid") == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (strcmp(k, "gid") == 0) { |
5 |
13 |
18 |
|
| 577 |
5 |
| out->gid = (uint32_t)strtoul(v, NULL, 10); |
| 578 |
13 |
▶ | } else if (strncmp(k, "SCHILY.xattr.", 13) == 0 |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (strncmp(k, "SCHILY.xattr.", 13) == 0 |
8 |
5 |
13 |
|
| 579 |
8 |
▶ | || strncmp(k, "LIBARCHIVE.xattr.", 17) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| || strncmp(k, "LIBARCHIVE.xattr.", 17) == 0) { |
0 |
8 |
8 |
|
| 580 |
5 |
▶ | size_t prefix_len = (k[0] == 'S') ? 13 : 17; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t prefix_len = (k[0] == 'S') ? 13 : 17; |
5 |
0 |
5 |
|
| 581 |
5 |
| const char *xname = k + prefix_len; |
| 582 |
5 |
| size_t xname_len = strlen(xname); |
| 583 |
5 |
| int is_b64 = 0; |
| 584 |
5 |
▶ | if (xname_len > 4 && strcmp(xname + xname_len - 4, ".b64") == 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (xname_len > 4 && strcmp(xname + xname_len - 4, ".b64") == 0) { |
5 |
0 |
5 |
| if (xname_len > 4 && strcmp(xname + xname_len - 4, ".b64") == 0) { |
2 |
3 |
5 |
|
| 585 |
2 |
| is_b64 = 1; |
| 586 |
2 |
| xname_len -= 4; |
| 587 |
|
| } |
| 588 |
5 |
| char *decoded = NULL; |
| 589 |
5 |
| size_t dec_len = 0; |
| 590 |
5 |
▶ | if (is_b64) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_b64) { |
2 |
3 |
5 |
|
| 591 |
2 |
▶ | if (b64_decode(v, vlen, &decoded, &dec_len) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (b64_decode(v, vlen, &decoded, &dec_len) < 0) { |
0 |
2 |
2 |
|
| 592 |
0 |
| list = list->next; |
| 593 |
0 |
| continue; |
| 594 |
|
| } |
| 595 |
|
| } else { |
| 596 |
3 |
| decoded = (char *)malloc(vlen + 1); |
| 597 |
3 |
| memcpy(decoded, v, vlen); |
| 598 |
3 |
| decoded[vlen] = '\0'; |
| 599 |
3 |
| dec_len = vlen; |
| 600 |
|
| } |
| 601 |
5 |
▶ | if (c->cur_xattr_count >= c->cur_xattr_cap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->cur_xattr_count >= c->cur_xattr_cap) { |
3 |
2 |
5 |
|
| 602 |
3 |
▶ | size_t ncap = c->cur_xattr_cap ? c->cur_xattr_cap * 2 : 4; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t ncap = c->cur_xattr_cap ? c->cur_xattr_cap * 2 : 4; |
0 |
3 |
3 |
|
| 603 |
3 |
| ArchiveXattr *na = (ArchiveXattr *)realloc(c->cur_xattrs, ncap * sizeof(*na)); |
| 604 |
3 |
▶ | if (!na) { free(decoded); list = list->next; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!na) { free(decoded); list = list->next; continue; } |
0 |
3 |
3 |
|
| 605 |
3 |
| c->cur_xattrs = na; |
| 606 |
3 |
| c->cur_xattr_cap = ncap; |
| 607 |
|
| } |
| 608 |
5 |
| char *kdup = (char *)malloc(xname_len + 1); |
| 609 |
5 |
| memcpy(kdup, xname, xname_len); |
| 610 |
5 |
| kdup[xname_len] = '\0'; |
| 611 |
5 |
| c->cur_xattrs[c->cur_xattr_count].key = kdup; |
| 612 |
5 |
| c->cur_xattrs[c->cur_xattr_count].key_len = xname_len; |
| 613 |
5 |
| c->cur_xattrs[c->cur_xattr_count].value = decoded; |
| 614 |
5 |
| c->cur_xattrs[c->cur_xattr_count].value_len = dec_len; |
| 615 |
5 |
| c->cur_xattr_count++; |
| 616 |
8 |
▶ | } else if (strncmp(k, "GNU.sparse.", 11) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (strncmp(k, "GNU.sparse.", 11) == 0) { |
0 |
8 |
8 |
|
| 617 |
0 |
| out->is_sparse = 1; |
| 618 |
0 |
▶ | if (!c->sparse_warning_emitted) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!c->sparse_warning_emitted) { |
0 |
0 |
0 |
|
| 619 |
|
| /* Single warning per archive. */ |
| 620 |
0 |
| fprintf(stderr, |
| 621 |
|
| "File::Raw::Archive: sparse-encoded entries present; " |
| 622 |
|
| "dense bytes returned verbatim\n"); |
| 623 |
0 |
| c->sparse_warning_emitted = 1; |
| 624 |
|
| } |
| 625 |
|
| } |
| 626 |
|
| /* All other vendor keys silently ignored. */ |
| 627 |
39 |
| list = list->next; |
| 628 |
|
| } |
| 629 |
16 |
| } |
| 630 |
|
| |
| 631 |
|
| static int |
| 632 |
839 |
| typeflag_to_ae(char tf) |
| 633 |
|
| { |
| 634 |
839 |
| switch (tf) { |
| 635 |
826 |
| case TF_REGULAR: |
| 636 |
826 |
| case TF_AREGULAR: return AE_FILE; |
| 637 |
0 |
| case TF_HARDLINK: return AE_HARDLINK; |
| 638 |
6 |
| case TF_SYMLINK: return AE_SYMLINK; |
| 639 |
0 |
| case TF_CHAR: return AE_CHAR; |
| 640 |
0 |
| case TF_BLOCK: return AE_BLOCK; |
| 641 |
7 |
| case TF_DIRECTORY: return AE_DIR; |
| 642 |
0 |
| case TF_FIFO: return AE_FIFO; |
| 643 |
0 |
| default: return AE_OTHER; |
| 644 |
|
| } |
| 645 |
|
| } |
| 646 |
|
| |
| 647 |
|
| static int |
| 648 |
879 |
| tar_read_next(pTHX_ const ArchivePlugin *self, void *cursor, ArchiveEntry *out) |
| 649 |
|
| { |
| 650 |
|
| PERL_UNUSED_ARG(self); |
| 651 |
879 |
| tar_cursor_t *c = (tar_cursor_t *)cursor; |
| 652 |
|
| |
| 653 |
879 |
▶ | if (consume_pending_padding(c) < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (consume_pending_padding(c) < 0) return -1; |
0 |
879 |
879 |
|
| 654 |
879 |
| free_cur_buffers(c); |
| 655 |
879 |
| memset(out, 0, sizeof *out); |
| 656 |
|
| |
| 657 |
|
| /* Inner loop: peel off PAX / @LongLink prefix blocks until we see |
| 658 |
|
| * a regular entry. */ |
| 659 |
879 |
| char *long_name = NULL; |
| 660 |
879 |
| char *long_link = NULL; |
| 661 |
879 |
| pax_kv_t *per_entry = NULL; |
| 662 |
|
| |
| 663 |
17 |
| for (;;) { |
| 664 |
|
| tar_header_t h; |
| 665 |
896 |
| int rc = read_one_header(c, &h); |
| 666 |
1772 |
▶ | if (rc < 0) { pax_kv_free(per_entry); free(long_name); free(long_link); return -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc < 0) { pax_kv_free(per_entry); free(long_name); free(long_link); return -1; } |
3 |
893 |
896 |
|
| 667 |
893 |
▶ | if (rc == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rc == 0) { |
37 |
856 |
893 |
|
| 668 |
37 |
| pax_kv_free(per_entry); free(long_name); free(long_link); |
| 669 |
37 |
| return 0; /* end of archive */ |
| 670 |
|
| } |
| 671 |
|
| |
| 672 |
856 |
| char tf = h.typeflag; |
| 673 |
856 |
| uint64_t size = tar_parse_numeric(h.size, sizeof h.size); |
| 674 |
|
| |
| 675 |
856 |
▶ | if (tf == TF_GNU_LONGNAME || tf == TF_GNU_LONGLINK) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (tf == TF_GNU_LONGNAME || tf == TF_GNU_LONGLINK) { |
853 |
3 |
856 |
| if (tf == TF_GNU_LONGNAME || tf == TF_GNU_LONGLINK) { |
0 |
853 |
853 |
|
| 676 |
|
| char *payload; |
| 677 |
3 |
▶ | if (read_payload(c, size, &payload) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (read_payload(c, size, &payload) < 0) { |
0 |
3 |
3 |
|
| 678 |
0 |
| pax_kv_free(per_entry); free(long_name); free(long_link); return -1; |
| 679 |
|
| } |
| 680 |
3 |
▶ | if (tf == TF_GNU_LONGNAME) { free(long_name); long_name = payload; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (tf == TF_GNU_LONGNAME) { free(long_name); long_name = payload; } |
3 |
0 |
3 |
|
| 681 |
0 |
| else { free(long_link); long_link = payload; } |
| 682 |
3 |
| continue; |
| 683 |
|
| } |
| 684 |
853 |
▶ | if (tf == TF_PAX_FILE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tf == TF_PAX_FILE) { |
12 |
841 |
853 |
|
| 685 |
|
| char *payload; |
| 686 |
12 |
▶ | if (read_payload(c, size, &payload) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (read_payload(c, size, &payload) < 0) { |
0 |
12 |
12 |
|
| 687 |
0 |
| pax_kv_free(per_entry); free(long_name); free(long_link); return -1; |
| 688 |
|
| } |
| 689 |
12 |
| pax_parse_records(payload, (size_t)size, &per_entry); |
| 690 |
12 |
| free(payload); |
| 691 |
12 |
| continue; |
| 692 |
|
| } |
| 693 |
841 |
▶ | if (tf == TF_PAX_GLOBAL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (tf == TF_PAX_GLOBAL) { |
2 |
839 |
841 |
|
| 694 |
|
| char *payload; |
| 695 |
2 |
▶ | if (read_payload(c, size, &payload) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (read_payload(c, size, &payload) < 0) { |
0 |
2 |
2 |
|
| 696 |
0 |
| pax_kv_free(per_entry); free(long_name); free(long_link); return -1; |
| 697 |
|
| } |
| 698 |
2 |
| pax_parse_records(payload, (size_t)size, &c->global_kv); |
| 699 |
2 |
| free(payload); |
| 700 |
2 |
| continue; |
| 701 |
|
| } |
| 702 |
|
| |
| 703 |
|
| /* Regular entry. Populate from ustar header first. */ |
| 704 |
839 |
▶ | if (long_name) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (long_name) { |
3 |
836 |
839 |
|
| 705 |
3 |
| c->cur_name = long_name; |
| 706 |
3 |
| long_name = NULL; |
| 707 |
3 |
| out->name = c->cur_name; |
| 708 |
3 |
| out->name_len = strlen(c->cur_name); |
| 709 |
836 |
▶ | } else if (h.prefix[0]) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (h.prefix[0]) { |
0 |
836 |
836 |
|
| 710 |
0 |
| size_t pl = strnlen(h.prefix, sizeof h.prefix); |
| 711 |
0 |
| size_t nl = strnlen(h.name, sizeof h.name); |
| 712 |
0 |
| c->cur_name = (char *)malloc(pl + 1 + nl + 1); |
| 713 |
0 |
| memcpy(c->cur_name, h.prefix, pl); |
| 714 |
0 |
| c->cur_name[pl] = '/'; |
| 715 |
0 |
| memcpy(c->cur_name + pl + 1, h.name, nl); |
| 716 |
0 |
| c->cur_name[pl + 1 + nl] = '\0'; |
| 717 |
0 |
| out->name = c->cur_name; |
| 718 |
0 |
| out->name_len = pl + 1 + nl; |
| 719 |
|
| } else { |
| 720 |
836 |
| size_t nl = strnlen(h.name, sizeof h.name); |
| 721 |
836 |
| c->cur_name = (char *)malloc(nl + 1); |
| 722 |
836 |
| memcpy(c->cur_name, h.name, nl); |
| 723 |
836 |
| c->cur_name[nl] = '\0'; |
| 724 |
836 |
| out->name = c->cur_name; |
| 725 |
836 |
| out->name_len = nl; |
| 726 |
|
| } |
| 727 |
|
| |
| 728 |
839 |
▶ | if (long_link) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (long_link) { |
0 |
839 |
839 |
|
| 729 |
0 |
| c->cur_link = long_link; |
| 730 |
0 |
| long_link = NULL; |
| 731 |
0 |
| out->link_target = c->cur_link; |
| 732 |
0 |
| out->link_target_len = strlen(c->cur_link); |
| 733 |
839 |
▶ | } else if (h.linkname[0]) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (h.linkname[0]) { |
6 |
833 |
839 |
|
| 734 |
6 |
| size_t ll = strnlen(h.linkname, sizeof h.linkname); |
| 735 |
6 |
| c->cur_link = (char *)malloc(ll + 1); |
| 736 |
6 |
| memcpy(c->cur_link, h.linkname, ll); |
| 737 |
6 |
| c->cur_link[ll] = '\0'; |
| 738 |
6 |
| out->link_target = c->cur_link; |
| 739 |
6 |
| out->link_target_len = ll; |
| 740 |
|
| } |
| 741 |
|
| |
| 742 |
839 |
| out->size = size; |
| 743 |
839 |
| out->mode = (uint32_t)tar_parse_numeric(h.mode, sizeof h.mode); |
| 744 |
839 |
| out->uid = (uint32_t)tar_parse_numeric(h.uid, sizeof h.uid); |
| 745 |
839 |
| out->gid = (uint32_t)tar_parse_numeric(h.gid, sizeof h.gid); |
| 746 |
839 |
| out->mtime = tar_parse_numeric(h.mtime, sizeof h.mtime); |
| 747 |
839 |
| out->mtime_ns = 0; |
| 748 |
839 |
| out->type = typeflag_to_ae(tf); |
| 749 |
|
| |
| 750 |
|
| /* Apply globals first, then per-entry PAX overrides. */ |
| 751 |
839 |
▶ | if (c->global_kv) apply_pax_to_entry(c, c->global_kv, out); |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->global_kv) apply_pax_to_entry(c, c->global_kv, out); |
4 |
835 |
839 |
|
| 752 |
839 |
▶ | if (per_entry) apply_pax_to_entry(c, per_entry, out); |
Branch: 1 branch
| Branch | True | False | Total |
| if (per_entry) apply_pax_to_entry(c, per_entry, out); |
12 |
827 |
839 |
|
| 753 |
839 |
| pax_kv_free(per_entry); |
| 754 |
|
| |
| 755 |
839 |
▶ | if (c->cur_xattr_count) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->cur_xattr_count) { |
3 |
836 |
839 |
|
| 756 |
3 |
| out->xattrs = c->cur_xattrs; |
| 757 |
3 |
| out->xattr_count = c->cur_xattr_count; |
| 758 |
|
| } |
| 759 |
|
| |
| 760 |
|
| /* Set up payload bookkeeping. */ |
| 761 |
839 |
▶ | c->entry_remaining = (out->type == AE_FILE) ? out->size : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| c->entry_remaining = (out->type == AE_FILE) ? out->size : 0; |
826 |
13 |
839 |
|
| 762 |
839 |
▶ | if (out->type != AE_FILE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (out->type != AE_FILE) { |
13 |
826 |
839 |
|
| 763 |
|
| /* Some producers emit a non-zero size for non-files (rare); skip the bytes. */ |
| 764 |
13 |
| c->entry_remaining = out->size; |
| 765 |
|
| } |
| 766 |
1678 |
| c->entry_pad = c->entry_remaining |
| 767 |
826 |
| ? (TAR_BLOCK_SIZE - (c->entry_remaining % TAR_BLOCK_SIZE)) % TAR_BLOCK_SIZE |
| 768 |
839 |
▶ | : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| : 0; |
826 |
13 |
839 |
|
| 769 |
|
| |
| 770 |
839 |
| return 1; |
| 771 |
|
| } |
| 772 |
|
| } |
| 773 |
|
| |
| 774 |
|
| static int |
| 775 |
1947 |
| tar_read_data(pTHX_ const ArchivePlugin *self, void *cursor, char *buf, size_t len) |
| 776 |
|
| { |
| 777 |
|
| PERL_UNUSED_ARG(self); |
| 778 |
1947 |
| tar_cursor_t *c = (tar_cursor_t *)cursor; |
| 779 |
1947 |
▶ | if (c->entry_remaining == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->entry_remaining == 0) return 0; |
819 |
1128 |
1947 |
|
| 780 |
1128 |
| size_t take = (c->entry_remaining < len) ? (size_t)c->entry_remaining : len; |
| 781 |
1128 |
| int n = read_full(c->pull, c->src, buf, take); |
| 782 |
1128 |
▶ | if (n < (int)take) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < (int)take) return -1; |
0 |
1128 |
1128 |
|
| 783 |
1128 |
| c->entry_remaining -= take; |
| 784 |
|
| /* When the entry is exhausted, consume padding so next read_next |
| 785 |
|
| * starts on a header boundary. */ |
| 786 |
1128 |
▶ | if (c->entry_remaining == 0 && c->entry_pad > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (c->entry_remaining == 0 && c->entry_pad > 0) { |
810 |
318 |
1128 |
| if (c->entry_remaining == 0 && c->entry_pad > 0) { |
804 |
6 |
810 |
|
| 787 |
|
| char pad[TAR_BLOCK_SIZE]; |
| 788 |
804 |
| int pn = read_full(c->pull, c->src, pad, (size_t)c->entry_pad); |
| 789 |
804 |
▶ | if (pn < (int)c->entry_pad) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pn < (int)c->entry_pad) return -1; |
0 |
804 |
804 |
|
| 790 |
804 |
| c->entry_pad = 0; |
| 791 |
|
| } |
| 792 |
1128 |
| return (int)take; |
| 793 |
|
| } |
| 794 |
|
| |
| 795 |
|
| static void |
| 796 |
75 |
| tar_read_close(pTHX_ const ArchivePlugin *self, void *cursor) |
| 797 |
|
| { |
| 798 |
|
| PERL_UNUSED_ARG(self); |
| 799 |
75 |
▶ | if (!cursor) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cursor) return; |
0 |
75 |
75 |
|
| 800 |
75 |
| tar_cursor_t *c = (tar_cursor_t *)cursor; |
| 801 |
75 |
| pax_kv_free(c->global_kv); |
| 802 |
75 |
| free_cur_buffers(c); |
| 803 |
75 |
| free(c); |
| 804 |
|
| } |
| 805 |
|
| |
| 806 |
|
| /* ============================================================ |
| 807 |
|
| * Write side |
| 808 |
|
| * ============================================================ */ |
| 809 |
|
| |
| 810 |
|
| static int |
| 811 |
45 |
| parse_format_opt(pTHX_ HV *opts) |
| 812 |
|
| { |
| 813 |
45 |
▶ | if (!opts) return FMT_AUTO; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!opts) return FMT_AUTO; |
0 |
45 |
45 |
|
| 814 |
45 |
| SV **sv = hv_fetchs(opts, "format", 0); |
| 815 |
45 |
▶ | if (!sv || !*sv || !SvOK(*sv)) return FMT_AUTO; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!sv || !*sv || !SvOK(*sv)) return FMT_AUTO; |
10 |
35 |
45 |
| if (!sv || !*sv || !SvOK(*sv)) return FMT_AUTO; |
10 |
0 |
10 |
| if (!sv || !*sv || !SvOK(*sv)) return FMT_AUTO; |
0 |
10 |
10 |
|
| 816 |
|
| STRLEN n; |
| 817 |
10 |
| const char *p = SvPV(*sv, n); |
| 818 |
10 |
▶ | if (n == 4 && memcmp(p, "auto", 4) == 0) return FMT_AUTO; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n == 4 && memcmp(p, "auto", 4) == 0) return FMT_AUTO; |
1 |
9 |
10 |
| if (n == 4 && memcmp(p, "auto", 4) == 0) return FMT_AUTO; |
1 |
0 |
1 |
|
| 819 |
9 |
▶ | if (n == 3 && memcmp(p, "pax", 3) == 0) return FMT_PAX; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n == 3 && memcmp(p, "pax", 3) == 0) return FMT_PAX; |
5 |
4 |
9 |
| if (n == 3 && memcmp(p, "pax", 3) == 0) return FMT_PAX; |
4 |
1 |
5 |
|
| 820 |
5 |
▶ | if (n == 3 && memcmp(p, "gnu", 3) == 0) return FMT_GNU; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n == 3 && memcmp(p, "gnu", 3) == 0) return FMT_GNU; |
1 |
4 |
5 |
| if (n == 3 && memcmp(p, "gnu", 3) == 0) return FMT_GNU; |
1 |
0 |
1 |
|
| 821 |
4 |
▶ | if (n == 5 && memcmp(p, "ustar", 5) == 0) return FMT_USTAR; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n == 5 && memcmp(p, "ustar", 5) == 0) return FMT_USTAR; |
4 |
0 |
4 |
| if (n == 5 && memcmp(p, "ustar", 5) == 0) return FMT_USTAR; |
4 |
0 |
4 |
|
| 822 |
0 |
| return -1; |
| 823 |
|
| } |
| 824 |
|
| |
| 825 |
|
| /* Returns 1 if this entry's name fits the ustar 100/256-byte limit. */ |
| 826 |
|
| static int |
| 827 |
684 |
| ustar_name_fits(const ArchiveEntry *e) |
| 828 |
|
| { |
| 829 |
684 |
| return e->name_len <= 100; |
| 830 |
|
| /* (We could also try a prefix split; we leave that to PAX/@LongLink path.) */ |
| 831 |
|
| } |
| 832 |
|
| |
| 833 |
|
| static int |
| 834 |
4 |
| ustar_link_fits(const ArchiveEntry *e) |
| 835 |
|
| { |
| 836 |
4 |
| return e->link_target_len <= 100; |
| 837 |
|
| } |
| 838 |
|
| |
| 839 |
|
| static int |
| 840 |
2788 |
| ustar_numeric_fits(uint64_t v, size_t field_n) |
| 841 |
|
| { |
| 842 |
|
| /* field_n includes the trailing NUL: usable digits = field_n - 1. */ |
| 843 |
2788 |
| uint64_t cap = 1; |
| 844 |
|
| size_t i; |
| 845 |
27840 |
▶ | for (i = 0; i < field_n - 1; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < field_n - 1; i++) { |
25052 |
2788 |
27840 |
|
| 846 |
25052 |
▶ | if (cap > (UINT64_MAX >> 3)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (cap > (UINT64_MAX >> 3)) return 1; |
0 |
25052 |
25052 |
|
| 847 |
25052 |
| cap <<= 3; |
| 848 |
|
| } |
| 849 |
2788 |
| return v < cap; |
| 850 |
|
| } |
| 851 |
|
| |
| 852 |
|
| static int |
| 853 |
694 |
| emit_block(tar_cursor_t *c, const char *block) |
| 854 |
|
| { |
| 855 |
694 |
| return write_full(c->push, c->sink, block, TAR_BLOCK_SIZE); |
| 856 |
|
| } |
| 857 |
|
| |
| 858 |
|
| static int |
| 859 |
685 |
| emit_padding_for(tar_cursor_t *c, uint64_t size) |
| 860 |
|
| { |
| 861 |
685 |
▶ | if (size == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (size == 0) return 0; |
0 |
685 |
685 |
|
| 862 |
685 |
| size_t pad = (TAR_BLOCK_SIZE - (size_t)(size % TAR_BLOCK_SIZE)) % TAR_BLOCK_SIZE; |
| 863 |
685 |
▶ | if (!pad) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pad) return 0; |
4 |
681 |
685 |
|
| 864 |
681 |
| char zeros[TAR_BLOCK_SIZE] = {0}; |
| 865 |
681 |
| return write_full(c->push, c->sink, zeros, pad); |
| 866 |
|
| } |
| 867 |
|
| |
| 868 |
|
| /* Build a base ustar header into `h` (caller pre-zeroed). Truncates |
| 869 |
|
| * fields to ustar limits; PAX/@LongLink override paths handled by the |
| 870 |
|
| * caller. */ |
| 871 |
|
| static void |
| 872 |
679 |
| build_ustar_header(tar_header_t *h, const ArchiveEntry *e, char typeflag, const char *override_name) |
| 873 |
|
| { |
| 874 |
679 |
▶ | const char *name = override_name ? override_name : e->name; |
Branch: 1 branch
| Branch | True | False | Total |
| const char *name = override_name ? override_name : e->name; |
0 |
679 |
679 |
|
| 875 |
679 |
▶ | size_t name_len = override_name ? strlen(override_name) : e->name_len; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t name_len = override_name ? strlen(override_name) : e->name_len; |
679 |
0 |
679 |
|
| 876 |
679 |
| size_t cpy = name_len > sizeof h->name ? sizeof h->name : name_len; |
| 877 |
679 |
| memcpy(h->name, name, cpy); |
| 878 |
|
| |
| 879 |
679 |
▶ | if (e->link_target && e->link_target_len) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (e->link_target && e->link_target_len) { |
4 |
675 |
679 |
| if (e->link_target && e->link_target_len) { |
4 |
0 |
4 |
|
| 880 |
4 |
| size_t llen = e->link_target_len; |
| 881 |
4 |
▶ | if (llen > sizeof h->linkname) llen = sizeof h->linkname; |
Branch: 1 branch
| Branch | True | False | Total |
| if (llen > sizeof h->linkname) llen = sizeof h->linkname; |
0 |
4 |
4 |
|
| 882 |
4 |
| memcpy(h->linkname, e->link_target, llen); |
| 883 |
|
| } |
| 884 |
|
| |
| 885 |
679 |
▶ | tar_emit_numeric(h->mode, sizeof h->mode, (uint64_t)(e->mode ? e->mode : 0644)); |
Branch: 1 branch
| Branch | True | False | Total |
| tar_emit_numeric(h->mode, sizeof h->mode, (uint64_t)(e->mode ? e->mode : 0644)); |
679 |
0 |
679 |
|
| 886 |
679 |
| tar_emit_numeric(h->uid, sizeof h->uid, (uint64_t)e->uid); |
| 887 |
679 |
| tar_emit_numeric(h->gid, sizeof h->gid, (uint64_t)e->gid); |
| 888 |
679 |
| tar_emit_numeric(h->size, sizeof h->size, e->size); |
| 889 |
679 |
| tar_emit_numeric(h->mtime, sizeof h->mtime, e->mtime); |
| 890 |
|
| |
| 891 |
679 |
| h->typeflag = typeflag; |
| 892 |
679 |
| memcpy(h->magic, "ustar", 5); |
| 893 |
679 |
| h->version[0] = '0'; |
| 894 |
679 |
| h->version[1] = '0'; |
| 895 |
|
| |
| 896 |
|
| /* checksum: fill chksum field with spaces, compute, then write. */ |
| 897 |
679 |
| memset(h->chksum, ' ', sizeof h->chksum); |
| 898 |
679 |
| uint32_t sum = tar_compute_checksum(h); |
| 899 |
|
| /* chksum is 6 octal digits + NUL + space per convention. */ |
| 900 |
|
| char tmp[8]; |
| 901 |
679 |
| snprintf(tmp, sizeof tmp, "%06o", (unsigned)sum); |
| 902 |
679 |
| memcpy(h->chksum, tmp, 6); |
| 903 |
679 |
| h->chksum[6] = '\0'; |
| 904 |
679 |
| h->chksum[7] = ' '; |
| 905 |
679 |
| } |
| 906 |
|
| |
| 907 |
|
| /* Emit a GNU @LongLink block carrying `payload` of `payload_len` bytes |
| 908 |
|
| * with the given flag (TF_GNU_LONGNAME or TF_GNU_LONGLINK). */ |
| 909 |
|
| static int |
| 910 |
3 |
| emit_gnu_longlink(tar_cursor_t *c, char flag, const char *payload, size_t payload_len) |
| 911 |
|
| { |
| 912 |
|
| tar_header_t h; |
| 913 |
3 |
| memset(&h, 0, sizeof h); |
| 914 |
|
| /* @LongLink "filename" is the literal `././@LongLink`. */ |
| 915 |
3 |
| strcpy(h.name, "././@LongLink"); |
| 916 |
3 |
| tar_emit_numeric(h.mode, sizeof h.mode, 0644); |
| 917 |
3 |
| tar_emit_numeric(h.size, sizeof h.size, (uint64_t)payload_len); |
| 918 |
3 |
| tar_emit_numeric(h.mtime, sizeof h.mtime, 0); |
| 919 |
3 |
| h.typeflag = flag; |
| 920 |
|
| /* GNU magic is "ustar " (5 + space + space). */ |
| 921 |
3 |
| memcpy(h.magic, "ustar ", 6); |
| 922 |
3 |
| h.version[0] = ' '; |
| 923 |
3 |
| h.version[1] = '\0'; |
| 924 |
3 |
| memset(h.chksum, ' ', sizeof h.chksum); |
| 925 |
3 |
| uint32_t sum = tar_compute_checksum(&h); |
| 926 |
|
| char tmp[8]; |
| 927 |
3 |
| snprintf(tmp, sizeof tmp, "%06o", (unsigned)sum); |
| 928 |
3 |
| memcpy(h.chksum, tmp, 6); |
| 929 |
3 |
| h.chksum[6] = '\0'; |
| 930 |
3 |
| h.chksum[7] = ' '; |
| 931 |
3 |
▶ | if (emit_block(c, (char *)&h) < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (emit_block(c, (char *)&h) < 0) return -1; |
0 |
3 |
3 |
|
| 932 |
|
| /* Payload with trailing NUL, padded to block boundary. */ |
| 933 |
3 |
▶ | if (write_full(c->push, c->sink, payload, payload_len) < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (write_full(c->push, c->sink, payload, payload_len) < 0) return -1; |
0 |
3 |
3 |
|
| 934 |
3 |
▶ | if (emit_padding_for(c, payload_len) < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (emit_padding_for(c, payload_len) < 0) return -1; |
0 |
3 |
3 |
|
| 935 |
3 |
| return 0; |
| 936 |
|
| } |
| 937 |
|
| |
| 938 |
|
| /* Emit a PAX 'x' header carrying the given pre-built records buffer. */ |
| 939 |
|
| static int |
| 940 |
12 |
| emit_pax_header(tar_cursor_t *c, char flag, const ArchiveEntry *e, |
| 941 |
|
| const char *records, size_t records_len) |
| 942 |
|
| { |
| 943 |
|
| tar_header_t h; |
| 944 |
12 |
| memset(&h, 0, sizeof h); |
| 945 |
|
| /* Conventional PAX header name: <dirname>/PaxHeaders/<basename>. */ |
| 946 |
|
| char name[100]; |
| 947 |
12 |
▶ | const char *base = e ? e->name : ""; |
Branch: 1 branch
| Branch | True | False | Total |
| const char *base = e ? e->name : ""; |
10 |
2 |
12 |
|
| 948 |
12 |
▶ | size_t base_len = e ? e->name_len : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| size_t base_len = e ? e->name_len : 0; |
10 |
2 |
12 |
|
| 949 |
12 |
▶ | const char *slash = base ? (const char *)memchr(base, '/', base_len) : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| const char *slash = base ? (const char *)memchr(base, '/', base_len) : NULL; |
12 |
0 |
12 |
|
| 950 |
|
| /* Find LAST slash. */ |
| 951 |
12 |
▶ | if (base) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (base) { |
12 |
0 |
12 |
|
| 952 |
|
| const char *p; |
| 953 |
12 |
| slash = NULL; |
| 954 |
825 |
▶ | for (p = base; p < base + base_len; p++) if (*p == '/') slash = p; |
Branch: 2 branches
| Branch | True | False | Total |
| for (p = base; p < base + base_len; p++) if (*p == '/') slash = p; |
3 |
810 |
813 |
| for (p = base; p < base + base_len; p++) if (*p == '/') slash = p; |
813 |
12 |
825 |
|
| 955 |
|
| } |
| 956 |
12 |
▶ | if (slash) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (slash) { |
3 |
9 |
12 |
|
| 957 |
3 |
| size_t dirlen = slash - base; |
| 958 |
3 |
| size_t baselen = base_len - dirlen - 1; |
| 959 |
3 |
| snprintf(name, sizeof name, "%.*s/PaxHeaders/%.*s", |
| 960 |
|
| (int)dirlen, base, (int)baselen, slash + 1); |
| 961 |
9 |
▶ | } else if (e) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (e) { |
7 |
2 |
9 |
|
| 962 |
7 |
| snprintf(name, sizeof name, "PaxHeaders/%.*s", (int)base_len, base); |
| 963 |
|
| } else { |
| 964 |
2 |
| snprintf(name, sizeof name, "PaxHeaders/global.%d", c->pax_header_seq++); |
| 965 |
|
| } |
| 966 |
12 |
| name[sizeof name - 1] = '\0'; |
| 967 |
12 |
| strncpy(h.name, name, sizeof h.name); |
| 968 |
|
| |
| 969 |
12 |
| tar_emit_numeric(h.mode, sizeof h.mode, 0644); |
| 970 |
12 |
| tar_emit_numeric(h.size, sizeof h.size, (uint64_t)records_len); |
| 971 |
12 |
| tar_emit_numeric(h.mtime, sizeof h.mtime, 0); |
| 972 |
12 |
| h.typeflag = flag; |
| 973 |
12 |
| memcpy(h.magic, "ustar", 5); |
| 974 |
12 |
| h.version[0] = '0'; |
| 975 |
12 |
| h.version[1] = '0'; |
| 976 |
12 |
| memset(h.chksum, ' ', sizeof h.chksum); |
| 977 |
12 |
| uint32_t sum = tar_compute_checksum(&h); |
| 978 |
|
| char tmp[8]; |
| 979 |
12 |
| snprintf(tmp, sizeof tmp, "%06o", (unsigned)sum); |
| 980 |
12 |
| memcpy(h.chksum, tmp, 6); |
| 981 |
12 |
| h.chksum[6] = '\0'; |
| 982 |
12 |
| h.chksum[7] = ' '; |
| 983 |
12 |
▶ | if (emit_block(c, (char *)&h) < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (emit_block(c, (char *)&h) < 0) return -1; |
0 |
12 |
12 |
|
| 984 |
12 |
▶ | if (write_full(c->push, c->sink, records, records_len) < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (write_full(c->push, c->sink, records, records_len) < 0) return -1; |
0 |
12 |
12 |
|
| 985 |
12 |
▶ | if (emit_padding_for(c, records_len) < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (emit_padding_for(c, records_len) < 0) return -1; |
0 |
12 |
12 |
|
| 986 |
12 |
| return 0; |
| 987 |
|
| } |
| 988 |
|
| |
| 989 |
|
| /* Build PAX records from an entry. Returns malloced buffer; *out_len |
| 990 |
|
| * set. Caller frees. The `force_all` flag: if 1, emit records for |
| 991 |
|
| * every applicable field regardless of whether ustar would have fit |
| 992 |
|
| * (used for format=pax). */ |
| 993 |
|
| static int |
| 994 |
10 |
| build_pax_for_entry(const ArchiveEntry *e, char **out, size_t *out_len, int force_all) |
| 995 |
|
| { |
| 996 |
|
| /* Worst-case size: each record at most 32 + key + value + a few bytes. |
| 997 |
|
| * Aggregate xattr sizes and pad. */ |
| 998 |
10 |
| size_t cap = 256; |
| 999 |
|
| size_t i; |
| 1000 |
10 |
▶ | if (e->name_len > 100 || force_all) cap += 32 + 8 + e->name_len; |
Branch: 2 branches
| Branch | True | False | Total |
| if (e->name_len > 100 || force_all) cap += 32 + 8 + e->name_len; |
7 |
3 |
10 |
| if (e->name_len > 100 || force_all) cap += 32 + 8 + e->name_len; |
2 |
5 |
7 |
|
| 1001 |
10 |
▶ | if (e->link_target_len > 100 || force_all) cap += 32 + 12 + e->link_target_len; |
Branch: 2 branches
| Branch | True | False | Total |
| if (e->link_target_len > 100 || force_all) cap += 32 + 12 + e->link_target_len; |
10 |
0 |
10 |
| if (e->link_target_len > 100 || force_all) cap += 32 + 12 + e->link_target_len; |
5 |
5 |
10 |
|
| 1002 |
10 |
▶ | if (!ustar_numeric_fits(e->size, sizeof ((tar_header_t *)0)->size) || force_all) cap += 64; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!ustar_numeric_fits(e->size, sizeof ((tar_header_t *)0)->size) || force_all) cap += 64; |
10 |
0 |
10 |
| if (!ustar_numeric_fits(e->size, sizeof ((tar_header_t *)0)->size) || force_all) cap += 64; |
5 |
5 |
10 |
|
| 1003 |
10 |
▶ | if (e->mtime_ns) cap += 64; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->mtime_ns) cap += 64; |
4 |
6 |
10 |
|
| 1004 |
10 |
▶ | if (!ustar_numeric_fits(e->uid, sizeof ((tar_header_t *)0)->uid) || force_all) cap += 32; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!ustar_numeric_fits(e->uid, sizeof ((tar_header_t *)0)->uid) || force_all) cap += 32; |
8 |
2 |
10 |
| if (!ustar_numeric_fits(e->uid, sizeof ((tar_header_t *)0)->uid) || force_all) cap += 32; |
3 |
5 |
8 |
|
| 1005 |
10 |
▶ | if (!ustar_numeric_fits(e->gid, sizeof ((tar_header_t *)0)->gid) || force_all) cap += 32; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!ustar_numeric_fits(e->gid, sizeof ((tar_header_t *)0)->gid) || force_all) cap += 32; |
8 |
2 |
10 |
| if (!ustar_numeric_fits(e->gid, sizeof ((tar_header_t *)0)->gid) || force_all) cap += 32; |
3 |
5 |
8 |
|
| 1006 |
15 |
▶ | for (i = 0; i < e->xattr_count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < e->xattr_count; i++) { |
5 |
10 |
15 |
|
| 1007 |
5 |
| cap += 64 + 32 + e->xattrs[i].key_len + e->xattrs[i].value_len * 2 + 16; |
| 1008 |
|
| } |
| 1009 |
10 |
▶ | if (cap < 4096) cap = 4096; |
Branch: 1 branch
| Branch | True | False | Total |
| if (cap < 4096) cap = 4096; |
10 |
0 |
10 |
|
| 1010 |
10 |
| char *buf = (char *)malloc(cap); |
| 1011 |
10 |
▶ | if (!buf) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!buf) return -1; |
0 |
10 |
10 |
|
| 1012 |
10 |
| size_t pos = 0; |
| 1013 |
|
| char tmp[64]; |
| 1014 |
|
| |
| 1015 |
10 |
▶ | if (e->name_len > 100 || force_all) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (e->name_len > 100 || force_all) { |
7 |
3 |
10 |
| if (e->name_len > 100 || force_all) { |
2 |
5 |
7 |
|
| 1016 |
5 |
| char *rec = buf + pos; |
| 1017 |
5 |
| pos += pax_build_record(rec, "path", e->name, e->name_len); |
| 1018 |
|
| } |
| 1019 |
10 |
▶ | if ((e->link_target_len > 100 && e->link_target_len) || (force_all && e->link_target_len)) { |
Branch: 4 branches
| Branch | True | False | Total |
| if ((e->link_target_len > 100 && e->link_target_len) || (force_all && e->link_target_len)) { |
0 |
10 |
10 |
| if ((e->link_target_len > 100 && e->link_target_len) || (force_all && e->link_target_len)) { |
0 |
0 |
0 |
| if ((e->link_target_len > 100 && e->link_target_len) || (force_all && e->link_target_len)) { |
5 |
5 |
10 |
| if ((e->link_target_len > 100 && e->link_target_len) || (force_all && e->link_target_len)) { |
0 |
5 |
5 |
|
| 1020 |
0 |
| pos += pax_build_record(buf + pos, "linkpath", e->link_target, e->link_target_len); |
| 1021 |
|
| } |
| 1022 |
10 |
▶ | if (!ustar_numeric_fits(e->size, sizeof ((tar_header_t *)0)->size) || force_all) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!ustar_numeric_fits(e->size, sizeof ((tar_header_t *)0)->size) || force_all) { |
10 |
0 |
10 |
| if (!ustar_numeric_fits(e->size, sizeof ((tar_header_t *)0)->size) || force_all) { |
5 |
5 |
10 |
|
| 1023 |
5 |
| int n = snprintf(tmp, sizeof tmp, "%llu", (unsigned long long)e->size); |
| 1024 |
5 |
| pos += pax_build_record(buf + pos, "size", tmp, (size_t)n); |
| 1025 |
|
| } |
| 1026 |
10 |
▶ | if (e->mtime_ns) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->mtime_ns) { |
4 |
6 |
10 |
|
| 1027 |
4 |
| int n = snprintf(tmp, sizeof tmp, "%llu.%09u", |
| 1028 |
4 |
| (unsigned long long)e->mtime, (unsigned)e->mtime_ns); |
| 1029 |
4 |
| pos += pax_build_record(buf + pos, "mtime", tmp, (size_t)n); |
| 1030 |
|
| } |
| 1031 |
10 |
▶ | if (!ustar_numeric_fits(e->uid, sizeof ((tar_header_t *)0)->uid) || force_all) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!ustar_numeric_fits(e->uid, sizeof ((tar_header_t *)0)->uid) || force_all) { |
8 |
2 |
10 |
| if (!ustar_numeric_fits(e->uid, sizeof ((tar_header_t *)0)->uid) || force_all) { |
3 |
5 |
8 |
|
| 1032 |
5 |
| int n = snprintf(tmp, sizeof tmp, "%u", (unsigned)e->uid); |
| 1033 |
5 |
| pos += pax_build_record(buf + pos, "uid", tmp, (size_t)n); |
| 1034 |
|
| } |
| 1035 |
10 |
▶ | if (!ustar_numeric_fits(e->gid, sizeof ((tar_header_t *)0)->gid) || force_all) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!ustar_numeric_fits(e->gid, sizeof ((tar_header_t *)0)->gid) || force_all) { |
8 |
2 |
10 |
| if (!ustar_numeric_fits(e->gid, sizeof ((tar_header_t *)0)->gid) || force_all) { |
3 |
5 |
8 |
|
| 1036 |
5 |
| int n = snprintf(tmp, sizeof tmp, "%u", (unsigned)e->gid); |
| 1037 |
5 |
| pos += pax_build_record(buf + pos, "gid", tmp, (size_t)n); |
| 1038 |
|
| } |
| 1039 |
|
| /* xattrs */ |
| 1040 |
15 |
▶ | for (i = 0; i < e->xattr_count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < e->xattr_count; i++) { |
5 |
10 |
15 |
|
| 1041 |
5 |
| const ArchiveXattr *x = &e->xattrs[i]; |
| 1042 |
5 |
| int b64 = needs_b64(x->value, x->value_len); |
| 1043 |
|
| char keybuf[300]; |
| 1044 |
5 |
▶ | snprintf(keybuf, sizeof keybuf, "SCHILY.xattr.%.*s%s", |
Branch: 1 branch
| Branch | True | False | Total |
| snprintf(keybuf, sizeof keybuf, "SCHILY.xattr.%.*s%s", |
2 |
3 |
5 |
|
| 1045 |
5 |
| (int)x->key_len, x->key, b64 ? ".b64" : ""); |
| 1046 |
5 |
▶ | if (b64) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (b64) { |
2 |
3 |
5 |
|
| 1047 |
2 |
| char *enc = b64_encode(x->value, x->value_len); |
| 1048 |
2 |
▶ | if (enc) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (enc) { |
2 |
0 |
2 |
|
| 1049 |
2 |
| pos += pax_build_record(buf + pos, keybuf, enc, strlen(enc)); |
| 1050 |
2 |
| free(enc); |
| 1051 |
|
| } |
| 1052 |
|
| } else { |
| 1053 |
3 |
| pos += pax_build_record(buf + pos, keybuf, x->value, x->value_len); |
| 1054 |
|
| } |
| 1055 |
|
| } |
| 1056 |
|
| |
| 1057 |
10 |
| *out = buf; |
| 1058 |
10 |
| *out_len = pos; |
| 1059 |
10 |
| return 0; |
| 1060 |
|
| } |
| 1061 |
|
| |
| 1062 |
|
| static int |
| 1063 |
45 |
| tar_write_open(pTHX_ const ArchivePlugin *self, archive_push_fn push, |
| 1064 |
|
| void *sink, HV *opts, void **out_cursor) |
| 1065 |
|
| { |
| 1066 |
|
| PERL_UNUSED_ARG(self); |
| 1067 |
45 |
| int fmt = parse_format_opt(aTHX_ opts); |
| 1068 |
45 |
▶ | if (fmt < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fmt < 0) return -1; |
0 |
45 |
45 |
|
| 1069 |
45 |
| tar_cursor_t *c = (tar_cursor_t *)calloc(1, sizeof *c); |
| 1070 |
45 |
▶ | if (!c) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!c) return -1; |
0 |
45 |
45 |
|
| 1071 |
45 |
| c->push = push; |
| 1072 |
45 |
| c->sink = sink; |
| 1073 |
45 |
| c->format = fmt; |
| 1074 |
|
| |
| 1075 |
|
| /* PAX 'g' global header from opts.global_meta. */ |
| 1076 |
45 |
▶ | if (opts) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (opts) { |
45 |
0 |
45 |
|
| 1077 |
45 |
| SV **gm = hv_fetchs(opts, "global_meta", 0); |
| 1078 |
45 |
▶ | if (gm && *gm && SvROK(*gm) && SvTYPE(SvRV(*gm)) == SVt_PVHV) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (gm && *gm && SvROK(*gm) && SvTYPE(SvRV(*gm)) == SVt_PVHV) { |
2 |
43 |
45 |
| if (gm && *gm && SvROK(*gm) && SvTYPE(SvRV(*gm)) == SVt_PVHV) { |
2 |
0 |
2 |
| if (gm && *gm && SvROK(*gm) && SvTYPE(SvRV(*gm)) == SVt_PVHV) { |
2 |
0 |
2 |
| if (gm && *gm && SvROK(*gm) && SvTYPE(SvRV(*gm)) == SVt_PVHV) { |
2 |
0 |
2 |
|
| 1079 |
2 |
| HV *gh = (HV *)SvRV(*gm); |
| 1080 |
2 |
| char *records = (char *)malloc(4096); |
| 1081 |
2 |
| size_t rcap = 4096, rpos = 0; |
| 1082 |
2 |
| hv_iterinit(gh); |
| 1083 |
|
| HE *he; |
| 1084 |
6 |
▶ | while ((he = hv_iternext(gh))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(gh))) { |
4 |
2 |
6 |
|
| 1085 |
|
| I32 klen_i; |
| 1086 |
4 |
| const char *k = hv_iterkey(he, &klen_i); |
| 1087 |
4 |
| SV *v = hv_iterval(gh, he); |
| 1088 |
|
| STRLEN vlen; |
| 1089 |
4 |
| const char *vp = SvPV(v, vlen); |
| 1090 |
4 |
| size_t need = (size_t)klen_i + vlen + 64; |
| 1091 |
4 |
▶ | if (rpos + need > rcap) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rpos + need > rcap) { |
0 |
4 |
4 |
|
| 1092 |
0 |
▶ | while (rpos + need > rcap) rcap *= 2; |
Branch: 1 branch
| Branch | True | False | Total |
| while (rpos + need > rcap) rcap *= 2; |
0 |
0 |
0 |
|
| 1093 |
0 |
| records = (char *)realloc(records, rcap); |
| 1094 |
|
| } |
| 1095 |
|
| /* Build using a NUL-terminated key (pax_build_record needs strlen). */ |
| 1096 |
|
| char keybuf[256]; |
| 1097 |
4 |
| memcpy(keybuf, k, klen_i); |
| 1098 |
4 |
| keybuf[klen_i] = '\0'; |
| 1099 |
4 |
| rpos += pax_build_record(records + rpos, keybuf, vp, vlen); |
| 1100 |
|
| } |
| 1101 |
2 |
▶ | if (rpos > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rpos > 0) { |
2 |
0 |
2 |
|
| 1102 |
2 |
▶ | if (emit_pax_header(c, TF_PAX_GLOBAL, NULL, records, rpos) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (emit_pax_header(c, TF_PAX_GLOBAL, NULL, records, rpos) < 0) { |
0 |
2 |
2 |
|
| 1103 |
0 |
| free(records); |
| 1104 |
0 |
| free(c); |
| 1105 |
0 |
| return -1; |
| 1106 |
|
| } |
| 1107 |
|
| } |
| 1108 |
2 |
| free(records); |
| 1109 |
|
| } |
| 1110 |
|
| } |
| 1111 |
|
| |
| 1112 |
45 |
| *out_cursor = c; |
| 1113 |
45 |
| return 0; |
| 1114 |
|
| } |
| 1115 |
|
| |
| 1116 |
|
| static int |
| 1117 |
684 |
| tar_write_add(pTHX_ const ArchivePlugin *self, void *cursor, |
| 1118 |
|
| const ArchiveEntry *entry, const char *content, size_t len) |
| 1119 |
|
| { |
| 1120 |
|
| PERL_UNUSED_ARG(self); |
| 1121 |
684 |
| tar_cursor_t *c = (tar_cursor_t *)cursor; |
| 1122 |
|
| |
| 1123 |
684 |
| int needs_pax = 0; |
| 1124 |
684 |
| int needs_gnu = 0; |
| 1125 |
684 |
| int needs_long_name = !ustar_name_fits(entry); |
| 1126 |
684 |
▶ | int needs_long_link = entry->link_target && !ustar_link_fits(entry); |
Branch: 2 branches
| Branch | True | False | Total |
| int needs_long_link = entry->link_target && !ustar_link_fits(entry); |
4 |
680 |
684 |
| int needs_long_link = entry->link_target && !ustar_link_fits(entry); |
0 |
4 |
4 |
|
| 1127 |
|
| |
| 1128 |
684 |
▶ | if (!ustar_numeric_fits(entry->size, sizeof ((tar_header_t *)0)->size) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ustar_numeric_fits(entry->size, sizeof ((tar_header_t *)0)->size) |
684 |
0 |
684 |
|
| 1129 |
684 |
▶ | || !ustar_numeric_fits(entry->uid, sizeof ((tar_header_t *)0)->uid) |
Branch: 1 branch
| Branch | True | False | Total |
| || !ustar_numeric_fits(entry->uid, sizeof ((tar_header_t *)0)->uid) |
680 |
4 |
684 |
|
| 1130 |
680 |
▶ | || !ustar_numeric_fits(entry->gid, sizeof ((tar_header_t *)0)->gid) |
Branch: 1 branch
| Branch | True | False | Total |
| || !ustar_numeric_fits(entry->gid, sizeof ((tar_header_t *)0)->gid) |
680 |
0 |
680 |
|
| 1131 |
680 |
▶ | || !ustar_numeric_fits(entry->mtime, sizeof ((tar_header_t *)0)->mtime) |
Branch: 1 branch
| Branch | True | False | Total |
| || !ustar_numeric_fits(entry->mtime, sizeof ((tar_header_t *)0)->mtime) |
680 |
0 |
680 |
|
| 1132 |
680 |
▶ | || entry->mtime_ns |
Branch: 1 branch
| Branch | True | False | Total |
| || entry->mtime_ns |
677 |
3 |
680 |
|
| 1133 |
677 |
▶ | || entry->xattr_count) { |
Branch: 1 branch
| Branch | True | False | Total |
| || entry->xattr_count) { |
2 |
675 |
677 |
|
| 1134 |
9 |
| needs_pax = 1; |
| 1135 |
|
| } |
| 1136 |
|
| |
| 1137 |
684 |
▶ | if (c->format == FMT_USTAR) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c->format == FMT_USTAR) { |
4 |
680 |
684 |
|
| 1138 |
4 |
▶ | if (needs_pax || needs_long_name || needs_long_link) return -1; |
Branch: 3 branches
| Branch | True | False | Total |
| if (needs_pax || needs_long_name || needs_long_link) return -1; |
3 |
1 |
4 |
| if (needs_pax || needs_long_name || needs_long_link) return -1; |
0 |
3 |
3 |
| if (needs_pax || needs_long_name || needs_long_link) return -1; |
0 |
0 |
0 |
|
| 1139 |
680 |
▶ | } else if (c->format == FMT_GNU) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c->format == FMT_GNU) { |
2 |
678 |
680 |
|
| 1140 |
2 |
▶ | if (needs_pax) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (needs_pax) return -1; |
1 |
1 |
2 |
|
| 1141 |
1 |
▶ | if (needs_long_name || needs_long_link) needs_gnu = 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (needs_long_name || needs_long_link) needs_gnu = 1; |
0 |
1 |
1 |
| if (needs_long_name || needs_long_link) needs_gnu = 1; |
0 |
0 |
0 |
|
| 1142 |
678 |
▶ | } else if (c->format == FMT_PAX) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (c->format == FMT_PAX) { |
5 |
673 |
678 |
|
| 1143 |
5 |
| needs_pax = 1; /* force */ |
| 1144 |
|
| } else { /* FMT_AUTO */ |
| 1145 |
673 |
▶ | if (needs_pax) needs_pax = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (needs_pax) needs_pax = 1; |
5 |
668 |
673 |
|
| 1146 |
668 |
▶ | else if (needs_long_name || needs_long_link) needs_gnu = 1; |
Branch: 2 branches
| Branch | True | False | Total |
| else if (needs_long_name || needs_long_link) needs_gnu = 1; |
666 |
2 |
668 |
| else if (needs_long_name || needs_long_link) needs_gnu = 1; |
0 |
666 |
666 |
|
| 1147 |
|
| } |
| 1148 |
|
| |
| 1149 |
|
| /* Emit GNU @LongLink prefix blocks (for name and/or linkpath). */ |
| 1150 |
679 |
▶ | if (needs_gnu) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (needs_gnu) { |
3 |
676 |
679 |
|
| 1151 |
3 |
▶ | if (needs_long_name && entry->name_len) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (needs_long_name && entry->name_len) { |
3 |
0 |
3 |
| if (needs_long_name && entry->name_len) { |
3 |
0 |
3 |
|
| 1152 |
3 |
▶ | if (emit_gnu_longlink(c, TF_GNU_LONGNAME, entry->name, entry->name_len + 1) < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (emit_gnu_longlink(c, TF_GNU_LONGNAME, entry->name, entry->name_len + 1) < 0) return -1; |
0 |
3 |
3 |
|
| 1153 |
|
| } |
| 1154 |
3 |
▶ | if (needs_long_link && entry->link_target_len) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (needs_long_link && entry->link_target_len) { |
0 |
3 |
3 |
| if (needs_long_link && entry->link_target_len) { |
0 |
0 |
0 |
|
| 1155 |
0 |
▶ | if (emit_gnu_longlink(c, TF_GNU_LONGLINK, entry->link_target, entry->link_target_len + 1) < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (emit_gnu_longlink(c, TF_GNU_LONGLINK, entry->link_target, entry->link_target_len + 1) < 0) return -1; |
0 |
0 |
0 |
|
| 1156 |
|
| } |
| 1157 |
|
| } |
| 1158 |
|
| |
| 1159 |
|
| /* Emit PAX 'x' header. */ |
| 1160 |
679 |
▶ | if (needs_pax) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (needs_pax) { |
10 |
669 |
679 |
|
| 1161 |
|
| char *records; |
| 1162 |
|
| size_t rlen; |
| 1163 |
10 |
▶ | if (build_pax_for_entry(entry, &records, &rlen, c->format == FMT_PAX) < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (build_pax_for_entry(entry, &records, &rlen, c->format == FMT_PAX) < 0) return -1; |
0 |
10 |
10 |
|
| 1164 |
10 |
▶ | if (rlen > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (rlen > 0) { |
10 |
0 |
10 |
|
| 1165 |
10 |
▶ | if (emit_pax_header(c, TF_PAX_FILE, entry, records, rlen) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (emit_pax_header(c, TF_PAX_FILE, entry, records, rlen) < 0) { |
0 |
10 |
10 |
|
| 1166 |
0 |
| free(records); return -1; |
| 1167 |
|
| } |
| 1168 |
|
| } |
| 1169 |
10 |
| free(records); |
| 1170 |
|
| } |
| 1171 |
|
| |
| 1172 |
|
| /* Emit ustar header. */ |
| 1173 |
|
| tar_header_t h; |
| 1174 |
679 |
| memset(&h, 0, sizeof h); |
| 1175 |
|
| char typeflag; |
| 1176 |
679 |
| switch (entry->type) { |
| 1177 |
5 |
| case AE_DIR: typeflag = TF_DIRECTORY; break; |
| 1178 |
4 |
| case AE_SYMLINK: typeflag = TF_SYMLINK; break; |
| 1179 |
0 |
| case AE_HARDLINK: typeflag = TF_HARDLINK; break; |
| 1180 |
0 |
| case AE_FIFO: typeflag = TF_FIFO; break; |
| 1181 |
0 |
| case AE_CHAR: typeflag = TF_CHAR; break; |
| 1182 |
0 |
| case AE_BLOCK: typeflag = TF_BLOCK; break; |
| 1183 |
670 |
| default: typeflag = TF_REGULAR; break; |
| 1184 |
|
| } |
| 1185 |
|
| /* If name overflows ustar AND we're not using PAX/@LongLink, that's |
| 1186 |
|
| * an error. (Already handled above in format branches.) */ |
| 1187 |
679 |
| const char *name_for_header = entry->name; |
| 1188 |
|
| char tnbuf[101]; |
| 1189 |
679 |
▶ | if (entry->name_len > 100) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (entry->name_len > 100) { |
6 |
673 |
679 |
|
| 1190 |
|
| /* Use a truncated stand-in; the real name is in PAX/@LongLink. */ |
| 1191 |
6 |
| size_t cpy = 100; |
| 1192 |
6 |
| memcpy(tnbuf, entry->name, cpy); |
| 1193 |
6 |
| tnbuf[cpy] = '\0'; |
| 1194 |
6 |
| name_for_header = tnbuf; |
| 1195 |
|
| } |
| 1196 |
679 |
| build_ustar_header(&h, entry, typeflag, name_for_header); |
| 1197 |
679 |
▶ | if (emit_block(c, (char *)&h) < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (emit_block(c, (char *)&h) < 0) return -1; |
0 |
679 |
679 |
|
| 1198 |
|
| |
| 1199 |
|
| /* Emit payload. */ |
| 1200 |
679 |
▶ | if (entry->size > 0 && content) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (entry->size > 0 && content) { |
670 |
9 |
679 |
| if (entry->size > 0 && content) { |
670 |
0 |
670 |
|
| 1201 |
670 |
▶ | if (write_full(c->push, c->sink, content, (size_t)entry->size) < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (write_full(c->push, c->sink, content, (size_t)entry->size) < 0) return -1; |
0 |
670 |
670 |
|
| 1202 |
670 |
▶ | if (emit_padding_for(c, entry->size) < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (emit_padding_for(c, entry->size) < 0) return -1; |
0 |
670 |
670 |
|
| 1203 |
9 |
▶ | } else if (len > 0 && content) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (len > 0 && content) { |
0 |
9 |
9 |
| } else if (len > 0 && content) { |
0 |
0 |
0 |
|
| 1204 |
0 |
▶ | if (write_full(c->push, c->sink, content, len) < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (write_full(c->push, c->sink, content, len) < 0) return -1; |
0 |
0 |
0 |
|
| 1205 |
0 |
▶ | if (emit_padding_for(c, len) < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (emit_padding_for(c, len) < 0) return -1; |
0 |
0 |
0 |
|
| 1206 |
|
| } |
| 1207 |
|
| |
| 1208 |
679 |
| return 0; |
| 1209 |
|
| } |
| 1210 |
|
| |
| 1211 |
|
| static int |
| 1212 |
45 |
| tar_write_close(pTHX_ const ArchivePlugin *self, void *cursor) |
| 1213 |
|
| { |
| 1214 |
|
| PERL_UNUSED_ARG(self); |
| 1215 |
45 |
▶ | if (!cursor) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cursor) return 0; |
0 |
45 |
45 |
|
| 1216 |
45 |
| tar_cursor_t *c = (tar_cursor_t *)cursor; |
| 1217 |
45 |
| char zeros[TAR_BLOCK_SIZE * 2] = {0}; |
| 1218 |
45 |
| int rc = write_full(c->push, c->sink, zeros, sizeof zeros); |
| 1219 |
45 |
| pax_kv_free(c->global_kv); |
| 1220 |
45 |
| free(c); |
| 1221 |
45 |
| return rc; |
| 1222 |
|
| } |
| 1223 |
|
| |
| 1224 |
|
| /* ============================================================ |
| 1225 |
|
| * Probe |
| 1226 |
|
| * ============================================================ */ |
| 1227 |
|
| |
| 1228 |
|
| static int |
| 1229 |
0 |
| tar_probe(const char *bytes, size_t len) |
| 1230 |
|
| { |
| 1231 |
0 |
▶ | if (len < TAR_BLOCK_SIZE) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (len < TAR_BLOCK_SIZE) return 0; |
0 |
0 |
0 |
|
| 1232 |
0 |
| const tar_header_t *h = (const tar_header_t *)bytes; |
| 1233 |
0 |
▶ | if (memcmp(h->magic, "ustar", 5) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(h->magic, "ustar", 5) == 0) { |
0 |
0 |
0 |
|
| 1234 |
|
| /* Verify checksum to weed out false positives. */ |
| 1235 |
0 |
| uint32_t stored = (uint32_t)tar_parse_numeric(h->chksum, sizeof h->chksum); |
| 1236 |
0 |
| uint32_t actual = tar_compute_checksum(h); |
| 1237 |
0 |
▶ | if (stored == actual) return 90; |
Branch: 1 branch
| Branch | True | False | Total |
| if (stored == actual) return 90; |
0 |
0 |
0 |
|
| 1238 |
0 |
| return 60; |
| 1239 |
|
| } |
| 1240 |
|
| /* Old-style: maybe still tar if checksum verifies. */ |
| 1241 |
0 |
| uint32_t stored = (uint32_t)tar_parse_numeric(h->chksum, sizeof h->chksum); |
| 1242 |
0 |
| uint32_t actual = tar_compute_checksum(h); |
| 1243 |
0 |
▶ | if (stored == actual && stored != 0) return 50; |
Branch: 2 branches
| Branch | True | False | Total |
| if (stored == actual && stored != 0) return 50; |
0 |
0 |
0 |
| if (stored == actual && stored != 0) return 50; |
0 |
0 |
0 |
|
| 1244 |
0 |
| return 0; |
| 1245 |
|
| } |
| 1246 |
|
| |
| 1247 |
|
| /* ============================================================ |
| 1248 |
|
| * Plugin descriptor |
| 1249 |
|
| * ============================================================ */ |
| 1250 |
|
| |
| 1251 |
|
| ArchivePlugin tar_plugin = { |
| 1252 |
|
| .name = "tar", |
| 1253 |
|
| .probe = tar_probe, |
| 1254 |
|
| .read_open = tar_read_open, |
| 1255 |
|
| .read_next = tar_read_next, |
| 1256 |
|
| .read_data = tar_read_data, |
| 1257 |
|
| .read_close = tar_read_close, |
| 1258 |
|
| .read_seek_to = NULL, |
| 1259 |
|
| .write_open = tar_write_open, |
| 1260 |
|
| .write_add = tar_write_add, |
| 1261 |
|
| .write_close = tar_write_close, |
| 1262 |
|
| .state = NULL, |
| 1263 |
|
| }; |