@preventive/archive 1.4.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/compression.d.ts +50 -30
- package/compression.js +0 -5
- package/package.json +2 -1
- package/src/bytes.js +0 -2
- package/src/compression.js +36 -93
- package/src/entry.js +3 -9
- package/src/error.d.ts +10 -0
- package/src/error.js +4 -8
- package/src/names.js +115 -117
- package/src/tar/header.js +14 -39
- package/src/tar/pack.js +33 -48
- package/src/tar/pax.js +6 -11
- package/src/tar/unpack.js +67 -122
- package/src/text.js +6 -32
- package/src/zip/format.js +10 -29
- package/src/zip/unzip.js +45 -95
- package/src/zip/zip.js +24 -32
- package/tar.d.ts +166 -137
- package/tar.js +2 -18
- package/zip.d.ts +110 -98
- package/zip.js +1 -19
package/src/names.js
CHANGED
|
@@ -1,93 +1,51 @@
|
|
|
1
|
-
// Name safety, applied before writing an entry and before handing one out
|
|
2
|
-
// of an archive. A name is a relative path: `.` segments and a directory's
|
|
3
|
-
// trailing slash are dropped, and what is left has no empty or `..`
|
|
4
|
-
// segment, no control character and no backslash -- a separator on
|
|
5
|
-
// Windows, where `..\` would get past the check on `..` -- and does not
|
|
6
|
-
// start with a drive letter and colon, which Windows resolves from that
|
|
7
|
-
// drive rather than from the archive. A symlink target may use `..`, but
|
|
8
|
-
// is followed from where the link sits and refused if it climbs above the
|
|
9
|
-
// archive or passes through anything but a directory, which is how a
|
|
10
|
-
// chain of links would climb. A caller may let a relative target climb up
|
|
11
|
-
// to a number of levels above the archive's root, as a directory archived
|
|
12
|
-
// on its own links to the files of the tree it was cut from: past the root
|
|
13
|
-
// the archive cannot tell where a target leads -- not even back into
|
|
14
|
-
// itself, by the name it is extracted under -- so what it passes through
|
|
15
|
-
// there is the caller's to know. An absolute target is refused regardless.
|
|
16
|
-
// A hard link to a symlink, directly or down a chain of hard links, is a
|
|
17
|
-
// second name for it, and is held to the same walk from where that name
|
|
18
|
-
// sits, since a target safe under `a/b/` need not be safe at the root.
|
|
19
|
-
// Lengths are bounded by what a filesystem takes at all: PATH_MAX for the
|
|
20
|
-
// whole as the archive stores it, a directory's slash counted, and NAME_MAX
|
|
21
|
-
// for a segment, in bytes.
|
|
22
|
-
//
|
|
23
|
-
// A name may repeat only as the same entry again, field for field and byte
|
|
24
|
-
// for byte (some npm packagers write `d/f` and `d/./f` both): two different
|
|
25
|
-
// entries under one name would leave the winner to extraction order.
|
|
26
|
-
// Collisions a filesystem might add -- case, Unicode normalisation -- are
|
|
27
|
-
// the filesystem's, not the archive's, and are not looked for.
|
|
28
|
-
|
|
29
1
|
import { sameBytes } from './bytes.js'
|
|
30
2
|
import { ArchiveError } from './error.js'
|
|
31
3
|
import { checkString, hasUnsafe, quote, tooLong } from './text.js'
|
|
32
4
|
|
|
33
|
-
const PATH_MAX = 4096
|
|
34
|
-
const NAME_MAX = 255
|
|
5
|
+
const PATH_MAX = 4096 // bytes of the name as stored, a directory's slash counted
|
|
6
|
+
const NAME_MAX = 255 // bytes of a segment
|
|
35
7
|
|
|
36
|
-
// The rules
|
|
37
|
-
// segments, split the once.
|
|
8
|
+
// The rules for any path, a symlink target's too
|
|
38
9
|
function checkText(path, what) {
|
|
39
10
|
checkString(path, what)
|
|
40
11
|
if (path === '') throw new ArchiveError(`${what} is empty`)
|
|
12
|
+
// A backslash is a separator on Windows, where `..\` would pass the `..` check
|
|
41
13
|
if (hasUnsafe(path, true)) throw new ArchiveError(`${what} ${quote(path)} holds a control or formatting character, or a backslash`)
|
|
42
14
|
if (path.startsWith('/')) throw new ArchiveError(`${what} ${quote(path)} is absolute`)
|
|
43
15
|
const segments = path.split('/')
|
|
44
|
-
//
|
|
45
|
-
// that is not one is what a drive letter is looked for on.
|
|
16
|
+
// Windows resolves `C:x` from that drive, and `./C:x` is `C:x` once `.` is dropped
|
|
46
17
|
if (/^[a-zA-Z]:/u.test(segments.find((segment) => segment !== '.') ?? '')) throw new ArchiveError(`${what} ${quote(path)} starts with a drive letter`)
|
|
47
18
|
if (tooLong(path, PATH_MAX)) throw new ArchiveError(`${what} ${quote(path)} is longer than ${PATH_MAX} bytes`)
|
|
48
19
|
if (segments.some((segment) => tooLong(segment, NAME_MAX))) throw new ArchiveError(`${what} ${quote(path)} has a segment longer than ${NAME_MAX} bytes`)
|
|
49
20
|
return segments
|
|
50
21
|
}
|
|
51
22
|
|
|
52
|
-
//
|
|
53
|
-
// may name; it comes back as `.`. A path with nothing to drop comes back as
|
|
54
|
-
// the very string it came in as, so the name an archive stores and the one
|
|
55
|
-
// cleaned out of it are one string where they are one path.
|
|
23
|
+
// `.` segments and a directory's trailing slash dropped; only a directory may name the root, as `.`
|
|
56
24
|
export function cleanPath(path, what, directory = false) {
|
|
57
25
|
const segments = checkText(path, what)
|
|
58
|
-
|
|
59
|
-
if (slash) {
|
|
26
|
+
if (segments.at(-1) === '') {
|
|
60
27
|
if (!directory) throw new ArchiveError(`${what} ${quote(path)} ends in a slash but is not a directory`)
|
|
61
28
|
segments.pop()
|
|
62
29
|
}
|
|
63
30
|
const kept = segments.filter((segment) => segment !== '.')
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
if (segment === '..') throw new ArchiveError(`${what} ${quote(path)} has a .. segment`)
|
|
67
|
-
}
|
|
31
|
+
const bad = kept.find((segment) => segment === '' || segment === '..')
|
|
32
|
+
if (bad !== undefined) throw new ArchiveError(`${what} ${quote(path)} has ${bad ? 'a .. segment' : 'an empty segment'}`)
|
|
68
33
|
if (kept.length === 0) {
|
|
69
34
|
if (!directory) throw new ArchiveError(`${what} ${quote(path)} names the archive root but is not a directory`)
|
|
70
35
|
return '.'
|
|
71
36
|
}
|
|
72
|
-
const name =
|
|
73
|
-
// As the archive stores it, a directory's name carries its slash: what
|
|
74
|
-
// pack takes, unpack then reads.
|
|
37
|
+
const name = kept.join('/')
|
|
75
38
|
if (tooLong(name, PATH_MAX - (directory ? 1 : 0))) throw new ArchiveError(`${what} ${quote(path)} is longer than ${PATH_MAX} bytes`)
|
|
76
39
|
return name
|
|
77
40
|
}
|
|
78
41
|
|
|
79
|
-
// The levels above the archive's root a caller lets a symlink target climb
|
|
80
|
-
// (`symlinksAbove`): none unless given.
|
|
81
42
|
export function levelsAbove(above = 0) {
|
|
82
43
|
if (!(Number.isSafeInteger(above) && above >= 0)) throw new ArchiveError(`symlinksAbove ${String(above)} is not a whole number of levels`)
|
|
83
44
|
return above
|
|
84
45
|
}
|
|
85
46
|
|
|
86
|
-
//
|
|
87
|
-
// the
|
|
88
|
-
// steps: each segment it goes into, or `..` to come back out of one. The
|
|
89
|
-
// walk is counted rather than spelled out, since spelling out the path at
|
|
90
|
-
// every step of a deep one costs its depth squared.
|
|
47
|
+
// The target's steps from the link's directory, each a segment or `..`, climbing at most `above` levels
|
|
48
|
+
// above the root. Counted rather than spelled out, which would cost a deep walk its depth squared.
|
|
91
49
|
export function checkSymlinkTarget(name, target, above = 0) {
|
|
92
50
|
const steps = checkText(target, `symlink target of ${quote(name)}`).filter((segment) => segment !== '' && segment !== '.')
|
|
93
51
|
let depth = name.split('/').length - 1
|
|
@@ -101,86 +59,65 @@ export function checkSymlinkTarget(name, target, above = 0) {
|
|
|
101
59
|
return steps
|
|
102
60
|
}
|
|
103
61
|
|
|
104
|
-
//
|
|
62
|
+
// A symlink's target is checked, never rewritten; a hard link's is a name, cleaned as one
|
|
105
63
|
export function cleanNames(path, type, target, above = 0) {
|
|
106
64
|
const name = cleanPath(path, 'entry name', type === 'directory')
|
|
107
|
-
let linkname = target
|
|
108
65
|
if (type === 'symlink') checkSymlinkTarget(name, target, above)
|
|
109
|
-
|
|
110
|
-
return { name, linkname }
|
|
66
|
+
return { name, linkname: type === 'hardlink' ? cleanPath(target, `hard link target of ${quote(name)}`) : target }
|
|
111
67
|
}
|
|
112
68
|
|
|
113
69
|
const node = () => ({ kind: undefined, entry: undefined, symlink: undefined, first: undefined, firstNode: undefined, rest: undefined })
|
|
114
70
|
|
|
115
|
-
// A node's first child sits on the node
|
|
116
|
-
//
|
|
117
|
-
// one thing each, and a map at every link of it would cost more than the
|
|
118
|
-
// link. With `make` false, a missing child stays missing.
|
|
71
|
+
// A node's first child sits on the node and only a second brings a Map: a deep path is a chain of
|
|
72
|
+
// directories holding one thing each, where a Map at every link would cost more than the link.
|
|
119
73
|
function child(parent, segment, make = true) {
|
|
120
74
|
if (parent.first === segment) return parent.firstNode
|
|
121
75
|
let next = parent.rest?.get(segment)
|
|
122
76
|
if (next !== undefined || !make) return next
|
|
123
77
|
next = node()
|
|
124
|
-
if (parent.first === undefined) {
|
|
125
|
-
|
|
126
|
-
parent.firstNode = next
|
|
127
|
-
} else {
|
|
128
|
-
parent.rest ??= new Map()
|
|
129
|
-
parent.rest.set(segment, next)
|
|
130
|
-
}
|
|
78
|
+
if (parent.first === undefined) Object.assign(parent, { first: segment, firstNode: next })
|
|
79
|
+
else (parent.rest ??= new Map()).set(segment, next)
|
|
131
80
|
return next
|
|
132
81
|
}
|
|
133
82
|
|
|
134
83
|
const FIELDS = ['type', 'mode', 'uid', 'gid', 'mtime', 'uname', 'gname', 'linkname', 'devmajor', 'devminor']
|
|
135
84
|
|
|
136
|
-
//
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
//
|
|
142
|
-
//
|
|
143
|
-
//
|
|
144
|
-
//
|
|
145
|
-
//
|
|
146
|
-
//
|
|
147
|
-
// The names are a tree walked a segment at a time, so a lookup hashes one
|
|
148
|
-
// segment rather than a whole path, and a walk down a deep one costs its
|
|
149
|
-
// length rather than its depth squared; a path is spelled out only for a
|
|
150
|
-
// refusal. A node's `kind` is unset until its path is seen, which makes the
|
|
151
|
-
// tree hold exactly what a map from each path seen to its kind would.
|
|
85
|
+
// Linux's ELOOP bound
|
|
86
|
+
const MAX_HOPS = 40
|
|
87
|
+
const RESOLVING = Symbol('resolving')
|
|
88
|
+
const stepsOf = (target) => target.split('/').filter((segment) => segment !== '' && segment !== '.')
|
|
89
|
+
|
|
90
|
+
// Each path seen is an 'entry', a 'directory', or a directory 'implied' by a path or symlink target through it.
|
|
91
|
+
// Nothing may sit or be walked through inside a non-directory, so no chain of links leads out of the archive.
|
|
92
|
+
// A name repeats only as the same entry; `keep` keeps data to compare a repeat with, else it is refused.
|
|
93
|
+
// With `chains`, a symlink target may pass through symlinks, as a macOS framework's do, and implies nothing:
|
|
94
|
+
// settle() follows each once every entry is in. A name still never may.
|
|
152
95
|
export class Names {
|
|
153
96
|
#root = node()
|
|
154
97
|
#keep
|
|
155
98
|
#above
|
|
99
|
+
#links
|
|
100
|
+
#resolved = new Map()
|
|
101
|
+
#resolving = 0
|
|
156
102
|
|
|
157
|
-
|
|
158
|
-
constructor(keep = false, above = 0) {
|
|
103
|
+
constructor(keep = false, symlinksAbove = 0, chains = false) {
|
|
159
104
|
this.#keep = keep
|
|
160
|
-
this.#above =
|
|
105
|
+
this.#above = levelsAbove(symlinksAbove)
|
|
106
|
+
this.#links = chains ? [] : null
|
|
161
107
|
}
|
|
162
108
|
|
|
163
|
-
// The levels a symlink target may climb above the root, for cleanNames.
|
|
164
109
|
get above() {
|
|
165
110
|
return this.#above
|
|
166
111
|
}
|
|
167
112
|
|
|
168
|
-
// `entry` is cleaned
|
|
113
|
+
// `entry` is cleaned
|
|
169
114
|
add(entry) {
|
|
170
115
|
const { name, type } = entry
|
|
171
|
-
// The target of the symlink this entry is, if it is one: a symlink's
|
|
172
|
-
// own, or, for a hard link, that of the entry it links to.
|
|
173
116
|
let symlink = type === 'symlink' ? entry.linkname : undefined
|
|
174
117
|
if (type === 'hardlink') {
|
|
175
118
|
const target = this.#find(entry.linkname)
|
|
176
119
|
if (target?.kind !== 'entry') throw new ArchiveError(`hard link ${quote(name)} targets ${quote(entry.linkname)}, which is not an earlier non-directory entry`)
|
|
177
|
-
// A hard link to a symlink is
|
|
178
|
-
// relative target is followed from wherever it is reached: `../x` under
|
|
179
|
-
// `a/b/` stays inside, the same link at the root does not. So the
|
|
180
|
-
// target is walked again from here, as if the symlink sat at this name.
|
|
181
|
-
// A hard link to such a hard link is the same symlink again, so the
|
|
182
|
-
// target travels down a chain of them, rather than the chain being
|
|
183
|
-
// followed back, which would cost each new link the chain's length.
|
|
120
|
+
// A hard link to a symlink is that symlink again at this name, where its target is walked again
|
|
184
121
|
symlink = target.symlink
|
|
185
122
|
if (symlink !== undefined) this.#symlink(name, symlink, `hard link ${quote(name)} to symlink ${quote(entry.linkname)}`)
|
|
186
123
|
}
|
|
@@ -196,8 +133,6 @@ export class Names {
|
|
|
196
133
|
}
|
|
197
134
|
const kind = type === 'directory' ? 'directory' : 'entry'
|
|
198
135
|
if (seen.kind !== undefined && kind !== 'directory') throw new ArchiveError(`${quote(name)} is already a directory, so it cannot be a ${type}`)
|
|
199
|
-
// Every parent, the outermost first; nodes[0] is the root, which no
|
|
200
|
-
// name has as a parent.
|
|
201
136
|
for (let i = 1; i < segments.length; i++) {
|
|
202
137
|
this.#directory(nodes[i], () => `${quote(name)} is inside ${quote(segments.slice(0, i).join('/'))}, which is not a directory`)
|
|
203
138
|
}
|
|
@@ -207,13 +142,14 @@ export class Names {
|
|
|
207
142
|
if (type === 'symlink') this.#symlink(name, entry.linkname, `symlink ${quote(name)}`)
|
|
208
143
|
}
|
|
209
144
|
|
|
210
|
-
//
|
|
211
|
-
//
|
|
212
|
-
// inside it has to be a directory -- each one it stands on short of the
|
|
213
|
-
// last step, the archive root aside. Out of it, nothing it passes is the
|
|
214
|
-
// archive's. `what` names it in a refusal.
|
|
145
|
+
// Every path the target stands on inside the archive short of its last step has to be a directory.
|
|
146
|
+
// Past the root nothing it passes is the archive's.
|
|
215
147
|
#symlink(at, target, what) {
|
|
216
148
|
const steps = checkSymlinkTarget(at, target, this.#above)
|
|
149
|
+
if (this.#links !== null) {
|
|
150
|
+
this.#links.push({ at, what })
|
|
151
|
+
return
|
|
152
|
+
}
|
|
217
153
|
const path = at.split('/').slice(0, -1)
|
|
218
154
|
const nodes = this.#walk(path)
|
|
219
155
|
for (const [i, step] of steps.entries()) {
|
|
@@ -229,28 +165,90 @@ export class Names {
|
|
|
229
165
|
}
|
|
230
166
|
}
|
|
231
167
|
|
|
232
|
-
|
|
233
|
-
|
|
168
|
+
settle() {
|
|
169
|
+
for (const { at, what } of this.#links ?? []) {
|
|
170
|
+
const names = at.split('/')
|
|
171
|
+
const nodes = this.#walk(names)
|
|
172
|
+
this.#resolve(nodes.pop(), { nodes, names: names.slice(0, -1) }, what)
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Memoized by link: no name passes through one, so each sits at one place
|
|
177
|
+
#resolve(link, dir, what) {
|
|
178
|
+
const known = this.#resolved.get(link)
|
|
179
|
+
if (known === RESOLVING) throw new ArchiveError(`${what} leads through a loop of symlinks`)
|
|
180
|
+
if (known !== undefined) return known
|
|
181
|
+
// Each nested resolve is a link the outermost passes: refused before the stack is spent
|
|
182
|
+
if (this.#resolving > MAX_HOPS) throw new ArchiveError(`${what} leads through more than ${MAX_HOPS} symlinks`)
|
|
183
|
+
this.#resolved.set(link, RESOLVING)
|
|
184
|
+
this.#resolving++
|
|
185
|
+
try {
|
|
186
|
+
const end = this.#follow(dir, stepsOf(link.symlink), what)
|
|
187
|
+
this.#resolved.set(link, end)
|
|
188
|
+
return end
|
|
189
|
+
} finally {
|
|
190
|
+
this.#resolving--
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Levels counted as checkSymlinkTarget counts them; past the root nothing is the archive's
|
|
195
|
+
#follow(from, steps, what) {
|
|
196
|
+
let nodes = [...from.nodes]
|
|
197
|
+
let names = [...from.names]
|
|
198
|
+
let depth = names.length
|
|
199
|
+
let outside = false
|
|
200
|
+
let hops = 0
|
|
201
|
+
for (const [i, step] of steps.entries()) {
|
|
202
|
+
if (step === '..') {
|
|
203
|
+
depth -= 1
|
|
204
|
+
if (depth < -this.#above) {
|
|
205
|
+
const how = this.#above === 0 ? 'outside the archive' : `more than ${this.#above} level${this.#above === 1 ? '' : 's'} above the archive`
|
|
206
|
+
throw new ArchiveError(`${what} leads ${how} through the symlinks it passes`)
|
|
207
|
+
}
|
|
208
|
+
if (outside || depth < 0) outside = true
|
|
209
|
+
else {
|
|
210
|
+
nodes.pop()
|
|
211
|
+
names.pop()
|
|
212
|
+
}
|
|
213
|
+
continue
|
|
214
|
+
}
|
|
215
|
+
depth += 1
|
|
216
|
+
if (outside) continue
|
|
217
|
+
const next = child(nodes.at(-1), step, false)
|
|
218
|
+
if (next?.symlink !== undefined) {
|
|
219
|
+
const end = this.#resolve(next, { nodes, names }, what)
|
|
220
|
+
hops += end.hops + 1
|
|
221
|
+
if (hops > MAX_HOPS) throw new ArchiveError(`${what} leads through more than ${MAX_HOPS} symlinks`)
|
|
222
|
+
// A link may end at a file, but no step, `..` included, goes on from one
|
|
223
|
+
if (!end.outside && end.nodes.at(-1).kind === 'entry' && i < steps.length - 1) {
|
|
224
|
+
throw new ArchiveError(`the target of ${what} passes through ${quote([...names, step].join('/'))}, which leads to ${quote(end.names.join('/'))}, not a directory`)
|
|
225
|
+
}
|
|
226
|
+
depth = end.depth
|
|
227
|
+
outside = end.outside
|
|
228
|
+
nodes = [...end.nodes]
|
|
229
|
+
names = [...end.names]
|
|
230
|
+
continue
|
|
231
|
+
}
|
|
232
|
+
if (next?.kind === 'entry' && i < steps.length - 1) throw new ArchiveError(`the target of ${what} passes through ${quote([...names, step].join('/'))}, which is not a directory`)
|
|
233
|
+
nodes.push(next ?? node())
|
|
234
|
+
names.push(step)
|
|
235
|
+
}
|
|
236
|
+
return { nodes, names, depth, outside, hops }
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// `refusal` spells the path out only when it has to
|
|
234
240
|
#directory(at, refusal) {
|
|
235
241
|
if (at.kind === 'entry') throw new ArchiveError(refusal())
|
|
236
242
|
at.kind ??= 'implied'
|
|
237
243
|
}
|
|
238
244
|
|
|
239
|
-
// The root and the node of each segment in turn, made where missing: a
|
|
240
|
-
// node made here is unseen until something sets its kind.
|
|
241
245
|
#walk(segments) {
|
|
242
246
|
const nodes = [this.#root]
|
|
243
247
|
for (const segment of segments) nodes.push(child(nodes.at(-1), segment))
|
|
244
248
|
return nodes
|
|
245
249
|
}
|
|
246
250
|
|
|
247
|
-
// A path's node where it has one, without making any.
|
|
248
251
|
#find(path) {
|
|
249
|
-
|
|
250
|
-
for (const segment of path.split('/')) {
|
|
251
|
-
at = child(at, segment, false)
|
|
252
|
-
if (at === undefined) return undefined
|
|
253
|
-
}
|
|
254
|
-
return at
|
|
252
|
+
return path.split('/').reduce((at, segment) => at && child(at, segment, false), this.#root)
|
|
255
253
|
}
|
|
256
254
|
}
|
package/src/tar/header.js
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
// The 512-byte ustar header block, as GNU tar 1.35 writes and reads it.
|
|
2
|
-
// String fields come back as bytes cut at the first NUL, not text: GNU cuts
|
|
3
|
-
// a long name at 100 bytes wherever the real name went, and that is not
|
|
4
|
-
// always a whole character, so only the field that counts gets decoded.
|
|
5
|
-
|
|
6
1
|
import { EMPTY } from '../bytes.js'
|
|
7
2
|
import { ArchiveError } from '../error.js'
|
|
8
3
|
|
|
@@ -10,8 +5,6 @@ export const BLOCK = 512
|
|
|
10
5
|
|
|
11
6
|
export const isDevice = (type) => type === 'character-device' || type === 'block-device'
|
|
12
7
|
|
|
13
|
-
// Each entry type's type flag, and each extended header's: those GNU
|
|
14
|
-
// writes, under the names unpack reads them by.
|
|
15
8
|
export const TYPEFLAGS = {
|
|
16
9
|
file: 0x30,
|
|
17
10
|
hardlink: 0x31,
|
|
@@ -26,11 +19,9 @@ export const EXTENDED_TYPEFLAGS = { pax: 0x78, global: 0x67, longname: 0x4c, lon
|
|
|
26
19
|
|
|
27
20
|
export const NAME_SIZE = 100
|
|
28
21
|
export const PREFIX_SIZE = 155
|
|
29
|
-
|
|
30
|
-
export const OWNER_SIZE = 32
|
|
22
|
+
export const OWNER_SIZE = 32 // GNU always ends one with a NUL, so 31 bytes at most
|
|
31
23
|
|
|
32
24
|
const NAME = 0
|
|
33
|
-
// The numbers from mode to mtime, [key, offset, size], in the block's order.
|
|
34
25
|
const NUMBERS = [['mode', 100, 8], ['uid', 108, 8], ['gid', 116, 8], ['size', 124, 12], ['mtime', 136, 12]]
|
|
35
26
|
const CHKSUM = 148
|
|
36
27
|
const TYPEFLAG = 156
|
|
@@ -41,29 +32,22 @@ const GNAME = 297
|
|
|
41
32
|
const DEVMAJOR = 329
|
|
42
33
|
const DEVMINOR = 337
|
|
43
34
|
const PREFIX = 345
|
|
44
|
-
// Under the gnu magic
|
|
45
|
-
// file's real size. libarchive reads them whatever the type flag says --
|
|
46
|
-
// taking the real size as the file's, and the map and any extension blocks
|
|
47
|
-
// after the header as its layout -- where GNU tar and the rest read them for
|
|
48
|
-
// a sparse entry alone.
|
|
35
|
+
// Under the gnu magic, the old sparse map and real size: libarchive reads them whatever the type flag
|
|
49
36
|
const OLD_SPARSE = 386
|
|
50
37
|
const OLD_SPARSE_END = 495
|
|
51
38
|
|
|
52
|
-
const USTAR_MAGIC = 'ustar\u000000' //
|
|
39
|
+
const USTAR_MAGIC = 'ustar\u000000' // ustar and pax
|
|
53
40
|
const GNU_MAGIC = 'ustar \0'
|
|
54
41
|
|
|
55
42
|
const latin1 = (text) => Uint8Array.from(text, (c) => c.codePointAt(0))
|
|
56
43
|
const ascii = (raw) => String.fromCodePoint(...raw)
|
|
57
|
-
|
|
58
|
-
const put = (block, offset, size, bytes) => block.subarray(offset, offset + size).set(bytes)
|
|
44
|
+
const put = (block, offset, size, bytes) => block.subarray(offset, offset + size).set(bytes) // throws rather than overflow
|
|
59
45
|
const field = (block, offset, size) => untilNul(block.subarray(offset, offset + size))
|
|
60
46
|
|
|
61
47
|
export const octalMax = (size) => 8 ** (size - 1) - 1
|
|
62
48
|
export const fitsOctal = (value, size) => value >= 0 && value <= octalMax(size)
|
|
63
49
|
|
|
64
|
-
//
|
|
65
|
-
// 0x80 (0xff when negative) and the value in base 256, big-endian, two's
|
|
66
|
-
// complement, over the rest of the field.
|
|
50
|
+
// Octal digits and a NUL, or for gnu, 0x80 (0xff if negative) and two's complement base 256 after it
|
|
67
51
|
export function writeNumber(block, offset, size, value, gnu) {
|
|
68
52
|
if (fitsOctal(value, size)) {
|
|
69
53
|
put(block, offset, size, latin1(value.toString(8).padStart(size - 1, '0')))
|
|
@@ -78,12 +62,8 @@ export function writeNumber(block, offset, size, value, gnu) {
|
|
|
78
62
|
}
|
|
79
63
|
}
|
|
80
64
|
|
|
81
|
-
//
|
|
82
|
-
//
|
|
83
|
-
// libarchive and the rest read it. A NUL ends the number wherever it sits, so
|
|
84
|
-
// a field that opens with one is 0 however it goes on -- GNU stops there, and
|
|
85
|
-
// reading the digits past it instead would give a size, and so an archive,
|
|
86
|
-
// that only this package sees. Only a time may be negative.
|
|
65
|
+
// Spaces around the digits and a blank field (0) are read as GNU tar and libarchive do. A NUL ends
|
|
66
|
+
// the number wherever it sits, as in GNU tar, which reading past it would disagree with.
|
|
87
67
|
export function readNumber(block, offset, size, what, at, signed = false) {
|
|
88
68
|
const first = block[offset]
|
|
89
69
|
if (first === 0x80 || first === 0xff) {
|
|
@@ -91,7 +71,7 @@ export function readNumber(block, offset, size, what, at, signed = false) {
|
|
|
91
71
|
for (let i = offset + 1; i < offset + size; i++) v = (v << 8n) | BigInt(block[i])
|
|
92
72
|
if (first === 0xff) v = BigInt.asIntN((size - 1) * 8, v)
|
|
93
73
|
if (v < 0n && !signed) throw new ArchiveError(`the ${what} field is negative`, at)
|
|
94
|
-
if (
|
|
74
|
+
if (!Number.isSafeInteger(Number(v))) throw new ArchiveError(`the ${what} field is too large`, at)
|
|
95
75
|
return Number(v)
|
|
96
76
|
}
|
|
97
77
|
const match = /^ *([0-7]*) *$/u.exec(ascii(field(block, offset, size)))
|
|
@@ -104,7 +84,7 @@ export function untilNul(raw) {
|
|
|
104
84
|
return end === -1 ? raw : raw.subarray(0, end)
|
|
105
85
|
}
|
|
106
86
|
|
|
107
|
-
//
|
|
87
|
+
// The checksum field itself counts as spaces
|
|
108
88
|
function checksum(block) {
|
|
109
89
|
let sum = 8 * 0x20
|
|
110
90
|
for (let i = 0; i < CHKSUM; i++) sum += block[i]
|
|
@@ -114,9 +94,7 @@ function checksum(block) {
|
|
|
114
94
|
|
|
115
95
|
export const isZeroBlock = (block) => block.every((byte) => byte === 0)
|
|
116
96
|
|
|
117
|
-
//
|
|
118
|
-
// format. `devmajor` and `devminor` are null except for a device, since
|
|
119
|
-
// GNU leaves those fields NUL for everything else.
|
|
97
|
+
// Strings are bytes already cut to size, numbers already fit; device numbers are null but for a device
|
|
120
98
|
export function encodeHeader(f) {
|
|
121
99
|
const block = new Uint8Array(BLOCK)
|
|
122
100
|
put(block, NAME, NAME_SIZE, f.name)
|
|
@@ -129,15 +107,13 @@ export function encodeHeader(f) {
|
|
|
129
107
|
if (f.devmajor !== null) writeNumber(block, DEVMAJOR, 8, f.devmajor, f.gnu)
|
|
130
108
|
if (f.devminor !== null) writeNumber(block, DEVMINOR, 8, f.devminor, f.gnu)
|
|
131
109
|
put(block, PREFIX, PREFIX_SIZE, f.prefix)
|
|
132
|
-
// The one field written as digits, NUL, space.
|
|
133
110
|
put(block, CHKSUM, 8, latin1(`${checksum(block).toString(8).padStart(6, '0')}\0 `))
|
|
134
111
|
return block
|
|
135
112
|
}
|
|
136
113
|
|
|
137
|
-
// Whether a numeric field holds octal digits, as POSIX writes it, rather
|
|
138
|
-
// than nothing or GNU's base 256, both of which readNumber reads.
|
|
139
114
|
const isOctal = (block, offset, size) => /^ *[0-7]+ *$/u.test(ascii(field(block, offset, size)))
|
|
140
115
|
|
|
116
|
+
// Strings come back as bytes: GNU cuts a long name at 100 bytes, not always on a whole character
|
|
141
117
|
export function decodeHeader(block, at) {
|
|
142
118
|
if (checksum(block) !== readNumber(block, CHKSUM, 8, 'checksum', at)) throw new ArchiveError('header checksum does not match', at)
|
|
143
119
|
const magic = ascii(block.subarray(MAGIC, MAGIC + 8))
|
|
@@ -146,19 +122,18 @@ export function decodeHeader(block, at) {
|
|
|
146
122
|
if (gnu && !isZeroBlock(block.subarray(OLD_SPARSE, OLD_SPARSE_END))) throw new ArchiveError('header carries an old GNU sparse map or real size', at)
|
|
147
123
|
const typeflag = block[TYPEFLAG]
|
|
148
124
|
const device = typeflag === TYPEFLAGS['character-device'] || typeflag === TYPEFLAGS['block-device']
|
|
125
|
+
const numbers = [['checksum', CHKSUM, 8], ...NUMBERS, ...(device ? [['devmajor', DEVMAJOR, 8], ['devminor', DEVMINOR, 8]] : [])]
|
|
149
126
|
return {
|
|
150
127
|
gnu,
|
|
151
128
|
typeflag,
|
|
152
129
|
name: field(block, NAME, NAME_SIZE),
|
|
153
|
-
|
|
154
|
-
prefix: gnu ? EMPTY : field(block, PREFIX, PREFIX_SIZE),
|
|
130
|
+
prefix: gnu ? EMPTY : field(block, PREFIX, PREFIX_SIZE), // gnu has its own fields there
|
|
155
131
|
linkname: field(block, LINKNAME, NAME_SIZE),
|
|
156
132
|
uname: field(block, UNAME, OWNER_SIZE),
|
|
157
133
|
gname: field(block, GNAME, OWNER_SIZE),
|
|
158
134
|
...Object.fromEntries(NUMBERS.map(([key, offset, size]) => [key, readNumber(block, offset, size, key, at, key === 'mtime')])),
|
|
159
135
|
devmajor: device ? readNumber(block, DEVMAJOR, 8, 'devmajor', at) : 0,
|
|
160
136
|
devminor: device ? readNumber(block, DEVMINOR, 8, 'devminor', at) : 0,
|
|
161
|
-
octal: [
|
|
162
|
-
.every(([, offset, size]) => isOctal(block, offset, size)),
|
|
137
|
+
octal: numbers.every(([, offset, size]) => isOctal(block, offset, size)),
|
|
163
138
|
}
|
|
164
139
|
}
|