@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/tar/pack.js
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
// --group=0 --numeric-owner` and, for pax, `--pax-option=delete=atime,
|
|
3
|
-
// delete=ctime`. Where GNU would cut or substitute -- a name too long for
|
|
4
|
-
// ustar, a number out of range -- this refuses instead.
|
|
5
|
-
|
|
1
|
+
import { utf8fromString } from '@exodus/bytes/utf8.js'
|
|
6
2
|
import { EMPTY, concat, isAscii } from '../bytes.js'
|
|
7
3
|
import { DEFAULT_MODE, checkEntry, wireName } from '../entry.js'
|
|
8
4
|
import { ArchiveError } from '../error.js'
|
|
5
|
+
import { Names, cleanNames } from '../names.js'
|
|
6
|
+
import { checkString, hasUnsafe, quote } from '../text.js'
|
|
9
7
|
import { BLOCK, EXTENDED_TYPEFLAGS, NAME_SIZE, OWNER_SIZE, PREFIX_SIZE, TYPEFLAGS, encodeHeader, fitsOctal, isDevice, octalMax } from './header.js'
|
|
10
|
-
import { Names, cleanNames, levelsAbove } from '../names.js'
|
|
11
8
|
import { encodePax } from './pax.js'
|
|
12
|
-
|
|
9
|
+
|
|
10
|
+
// What GNU tar 1.35 writes with `--owner=0 --group=0 --numeric-owner` (and for pax,
|
|
11
|
+
// `--pax-option=delete=atime,delete=ctime`), but refusing what GNU would cut or substitute
|
|
13
12
|
|
|
14
13
|
const FORMATS = new Set(['gnu', 'ustar', 'pax'])
|
|
15
14
|
const SLASH = 0x2f
|
|
@@ -25,9 +24,9 @@ function ownerName(value, what) {
|
|
|
25
24
|
return value
|
|
26
25
|
}
|
|
27
26
|
|
|
28
|
-
function normalize(entry) {
|
|
27
|
+
function normalize(entry, above) {
|
|
29
28
|
const checked = checkEntry(entry, TYPEFLAGS)
|
|
30
|
-
const { name, type } = checked
|
|
29
|
+
const { name, type, linkname } = checked
|
|
31
30
|
const devmajor = integer(entry.devmajor ?? 0, 'devmajor')
|
|
32
31
|
const devminor = integer(entry.devminor ?? 0, 'devminor')
|
|
33
32
|
if ((devmajor !== 0 || devminor !== 0) && !isDevice(type)) throw new ArchiveError(`a ${type} cannot have device numbers (${quote(name)})`)
|
|
@@ -43,27 +42,26 @@ function normalize(entry) {
|
|
|
43
42
|
gname: ownerName(entry.gname ?? '', 'gname'),
|
|
44
43
|
devmajor,
|
|
45
44
|
devminor,
|
|
45
|
+
...cleanNames(name, type, linkname, above),
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
-
// A copy in whole blocks; `length` may reach past `raw` to count a NUL
|
|
49
|
+
// A copy in whole blocks; `length` may reach past `raw` to count a NUL
|
|
50
50
|
function padded(raw, length = raw.length) {
|
|
51
51
|
const out = new Uint8Array(Math.ceil(length / BLOCK) * BLOCK)
|
|
52
52
|
out.set(raw)
|
|
53
53
|
return out
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
// GNU's start_private_header
|
|
56
|
+
// GNU's start_private_header
|
|
57
57
|
const privateHeader = (name, size, mtime, typeflag, gnu) => encodeHeader({
|
|
58
|
-
gnu, name:
|
|
58
|
+
gnu, name: utf8fromString(name).subarray(0, NAME_SIZE), prefix: EMPTY, linkname: EMPTY, uname: EMPTY, gname: EMPTY,
|
|
59
59
|
typeflag, mode: 0o644, uid: 0, gid: 0, size, mtime, devmajor: null, devminor: null,
|
|
60
60
|
})
|
|
61
61
|
|
|
62
|
-
// The size counts the NUL after the name.
|
|
63
62
|
const longLink = (raw, typeflag) => [privateHeader('././@LongLink', raw.length + 1, 0, typeflag, true), padded(raw, raw.length + 1)]
|
|
64
63
|
|
|
65
|
-
// Named `%d/PaxHeaders/%f` after the entry,
|
|
66
|
-
// clamped into the field.
|
|
64
|
+
// Named `%d/PaxHeaders/%f` after the entry, its mtime clamped into the field
|
|
67
65
|
function paxHeader(e, records) {
|
|
68
66
|
const body = encodePax(records)
|
|
69
67
|
const slash = e.name.lastIndexOf('/')
|
|
@@ -71,32 +69,27 @@ function paxHeader(e, records) {
|
|
|
71
69
|
return [privateHeader(label, body.length, Math.max(0, Math.min(e.mtime, octalMax(12))), EXTENDED_TYPEFLAGS.pax, false), padded(body)]
|
|
72
70
|
}
|
|
73
71
|
|
|
74
|
-
// GNU's split_long_name: the last slash within 155 bytes of
|
|
75
|
-
// trailing slash does not count), leaving 1 to 100 bytes of name.
|
|
72
|
+
// GNU's split_long_name: at the last slash within 155 bytes, a trailing one aside, leaving 1 to 100 bytes of name
|
|
76
73
|
function splitName(raw, display) {
|
|
77
74
|
const { length } = raw
|
|
78
75
|
if (length > PREFIX_SIZE + NAME_SIZE + 1) throw new ArchiveError(`name ${quote(display)} is longer than 256 bytes, which ustar cannot hold`)
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
else if (raw[limit - 1] === SLASH) limit--
|
|
82
|
-
let i = limit - 1
|
|
83
|
-
while (i > 0 && raw[i] !== SLASH) i--
|
|
76
|
+
const limit = length > PREFIX_SIZE + 1 ? PREFIX_SIZE + 1 : raw[length - 1] === SLASH ? length - 1 : length
|
|
77
|
+
const i = raw.lastIndexOf(SLASH, limit - 1)
|
|
84
78
|
const rest = length - i - 1
|
|
85
|
-
if (i
|
|
79
|
+
if (i <= 0 || rest > NAME_SIZE || rest === 0) throw new ArchiveError(`name ${quote(display)} cannot be split into ustar's prefix and name fields`)
|
|
86
80
|
return { prefix: raw.subarray(0, i), name: raw.subarray(i + 1) }
|
|
87
81
|
}
|
|
88
82
|
|
|
89
|
-
//
|
|
90
|
-
//
|
|
91
|
-
// linkpath, path, uid, gid, size, mtime, devmajor, devminor, uname, gname.
|
|
83
|
+
// Long-name blocks or a pax header, the header, the data and its padding.
|
|
84
|
+
// Pax records go in GNU's order: linkpath, path, uid, gid, size, mtime, devmajor, devminor, uname, gname.
|
|
92
85
|
function encodeEntry(e, format) {
|
|
93
86
|
const gnu = format === 'gnu'
|
|
94
87
|
const pax = []
|
|
95
88
|
const chunks = []
|
|
96
89
|
const wire = wireName(e)
|
|
97
|
-
let name =
|
|
90
|
+
let name = utf8fromString(wire)
|
|
98
91
|
let prefix = EMPTY
|
|
99
|
-
let link =
|
|
92
|
+
let link = utf8fromString(e.linkname)
|
|
100
93
|
if (link.length > NAME_SIZE) {
|
|
101
94
|
if (gnu) chunks.push(...longLink(link, EXTENDED_TYPEFLAGS.longlink))
|
|
102
95
|
else if (format === 'pax') pax.push(['linkpath', e.linkname])
|
|
@@ -111,16 +104,13 @@ function encodeEntry(e, format) {
|
|
|
111
104
|
}
|
|
112
105
|
const number = (what, value, size) => {
|
|
113
106
|
if (gnu || fitsOctal(value, size)) return value
|
|
114
|
-
if (format
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
throw new ArchiveError(`${what} ${value} of ${quote(e.name)} does not fit the ustar format`)
|
|
107
|
+
if (format !== 'pax') throw new ArchiveError(`${what} ${value} of ${quote(e.name)} does not fit the ustar format`)
|
|
108
|
+
pax.push([what, String(value)])
|
|
109
|
+
return 0
|
|
119
110
|
}
|
|
120
|
-
// GNU
|
|
121
|
-
// 31 without one; that loss is the one place this does not follow it.
|
|
111
|
+
// GNU cuts a 32-byte owner name to 31 with no pax record; this refuses instead
|
|
122
112
|
const owner = (what, text) => {
|
|
123
|
-
const raw =
|
|
113
|
+
const raw = utf8fromString(text)
|
|
124
114
|
if (raw.length < OWNER_SIZE && (format !== 'pax' || isAscii(raw))) return raw
|
|
125
115
|
if (format !== 'pax') throw new ArchiveError(`${what} of ${quote(e.name)} is longer than 31 bytes, which the ${format} format cannot hold`)
|
|
126
116
|
pax.push([what, text])
|
|
@@ -140,33 +130,28 @@ function encodeEntry(e, format) {
|
|
|
140
130
|
}
|
|
141
131
|
if (pax.length) chunks.push(...paxHeader(e, pax))
|
|
142
132
|
chunks.push(encodeHeader(fields))
|
|
143
|
-
const rest = e.data.length % BLOCK
|
|
144
133
|
if (e.data.length) chunks.push(e.data)
|
|
145
|
-
if (
|
|
134
|
+
if (e.data.length % BLOCK) chunks.push(new Uint8Array(BLOCK - (e.data.length % BLOCK)))
|
|
146
135
|
return chunks
|
|
147
136
|
}
|
|
148
137
|
|
|
149
|
-
// `keep` holds entries for a repeat to be compared with, which only the
|
|
150
|
-
// in-memory call can afford.
|
|
151
138
|
function packer({ format = 'gnu', blocking = 20, symlinksAbove } = {}, keep = false) {
|
|
152
139
|
if (!FORMATS.has(format)) throw new ArchiveError(`format ${quote(String(format))} is not gnu, ustar or pax`)
|
|
153
140
|
if (!Number.isSafeInteger(blocking) || blocking < 1) throw new ArchiveError('blocking is not a positive integer')
|
|
154
|
-
const names = new Names(keep,
|
|
141
|
+
const names = new Names(keep, symlinksAbove)
|
|
155
142
|
let total = 0
|
|
156
143
|
return {
|
|
157
144
|
add(entry) {
|
|
158
|
-
const e = normalize(entry)
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
const chunks = encodeEntry(cleaned, format)
|
|
145
|
+
const e = normalize(entry, names.above)
|
|
146
|
+
names.add(e)
|
|
147
|
+
const chunks = encodeEntry(e, format)
|
|
162
148
|
for (const chunk of chunks) total += chunk.length
|
|
163
149
|
return chunks
|
|
164
150
|
},
|
|
165
|
-
// Two zero blocks, then zeros to a
|
|
151
|
+
// Two zero blocks, then zeros to a whole record
|
|
166
152
|
end() {
|
|
167
153
|
const record = blocking * BLOCK
|
|
168
|
-
|
|
169
|
-
return [new Uint8Array(2 * BLOCK + (rest ? record - rest : 0))]
|
|
154
|
+
return [new Uint8Array(Math.ceil((total + 2 * BLOCK) / record) * record - total)]
|
|
170
155
|
},
|
|
171
156
|
}
|
|
172
157
|
}
|
package/src/tar/pax.js
CHANGED
|
@@ -1,23 +1,19 @@
|
|
|
1
|
-
// pax extended header records: `<length> <keyword>=<value>\n`, the length
|
|
2
|
-
// in decimal counting the whole line, its own digits included.
|
|
3
|
-
|
|
4
1
|
import { utf8fromString } from '@exodus/bytes/utf8.js'
|
|
5
2
|
import { concat } from '../bytes.js'
|
|
6
3
|
import { ArchiveError } from '../error.js'
|
|
7
4
|
import { decodeUtf8, hasUnsafe, quote } from '../text.js'
|
|
8
5
|
|
|
6
|
+
// `<length> <keyword>=<value>\n`, the length counting the whole record, its own digits included
|
|
9
7
|
function record(keyword, value) {
|
|
10
8
|
const body = utf8fromString(` ${keyword}=${value}\n`)
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return concat([utf8fromString(String(total)), body])
|
|
9
|
+
let length = body.length + 1
|
|
10
|
+
while (body.length + String(length).length !== length) length++
|
|
11
|
+
return concat([utf8fromString(String(length)), body])
|
|
15
12
|
}
|
|
16
13
|
|
|
17
14
|
export const encodePax = (records) => concat(records.map(([keyword, value]) => record(keyword, value)))
|
|
18
15
|
|
|
19
|
-
//
|
|
20
|
-
// every entry under the same global header shares that header's.
|
|
16
|
+
// Read-only, as entries under one global header share its records
|
|
21
17
|
const READ_ONLY = 'pax records cannot be changed'
|
|
22
18
|
export class Records extends Map {
|
|
23
19
|
set() {
|
|
@@ -32,7 +28,6 @@ export class Records extends Map {
|
|
|
32
28
|
throw new TypeError(READ_ONLY)
|
|
33
29
|
}
|
|
34
30
|
}
|
|
35
|
-
const put = (records, keyword, value) => Map.prototype.set.call(records, keyword, value)
|
|
36
31
|
|
|
37
32
|
export function decodePax(bytes, at) {
|
|
38
33
|
const records = new Records()
|
|
@@ -48,7 +43,7 @@ export function decodePax(bytes, at) {
|
|
|
48
43
|
const keyword = decodeUtf8(bytes.subarray(i + 1, equals), 'a pax record', at)
|
|
49
44
|
if (keyword === '' || hasUnsafe(keyword, false) || keyword.includes(' ')) throw new ArchiveError(`pax keyword ${quote(keyword)} is malformed`, at)
|
|
50
45
|
if (records.has(keyword)) throw new ArchiveError(`pax keyword ${keyword} repeats`, at)
|
|
51
|
-
|
|
46
|
+
Map.prototype.set.call(records, keyword, decodeUtf8(bytes.subarray(equals + 1, end - 1), 'a pax record', at))
|
|
52
47
|
pos = end
|
|
53
48
|
}
|
|
54
49
|
return records
|
package/src/tar/unpack.js
CHANGED
|
@@ -1,30 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
// in-memory call and the streams share one reader. Extended headers are
|
|
3
|
-
// the kinds GNU writes: pax `x` and `g`, and the `L`/`K` long-name blocks.
|
|
4
|
-
// An entry's data views the chunk it arrived in where one chunk held it
|
|
5
|
-
// whole, and is a copy otherwise.
|
|
6
|
-
|
|
7
|
-
import { EMPTY } from '../bytes.js'
|
|
1
|
+
import { EMPTY, concat } from '../bytes.js'
|
|
8
2
|
import { isFile } from '../entry.js'
|
|
9
3
|
import { ArchiveError, located } from '../error.js'
|
|
4
|
+
import { Names, cleanNames } from '../names.js'
|
|
5
|
+
import { decodeUtf8, hasUnsafe, quote } from '../text.js'
|
|
10
6
|
import { BLOCK, EXTENDED_TYPEFLAGS, TYPEFLAGS, decodeHeader, isDevice, isZeroBlock, untilNul } from './header.js'
|
|
11
|
-
import { Names, cleanNames, levelsAbove } from '../names.js'
|
|
12
7
|
import { Records, decodePax } from './pax.js'
|
|
13
|
-
import { decodeUtf8, hasUnsafe, quote } from '../text.js'
|
|
14
8
|
|
|
15
|
-
// Type flag -> name. NUL is the pre-POSIX regular file.
|
|
16
9
|
const byFlag = (flags) => Object.entries(flags).map(([name, flag]) => [flag, name])
|
|
17
|
-
const TYPES = new Map([...byFlag(TYPEFLAGS), [0, 'file']])
|
|
10
|
+
const TYPES = new Map([...byFlag(TYPEFLAGS), [0, 'file']]) // NUL is the pre-POSIX file
|
|
18
11
|
const EXTENDED = new Map(byFlag(EXTENDED_TYPEFLAGS))
|
|
19
12
|
const MAX_EXTENDED = 1 << 20
|
|
20
13
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const SPARSE_KEYS = new Set(['SCHILY.realsize', 'SUN.holesdata'])
|
|
14
|
+
const NO_RECORDS = new Records()
|
|
15
|
+
const NO_EXTENDED = Object.freeze([])
|
|
16
|
+
const magicOf = (header) => (header.gnu ? 'gnu' : 'ustar')
|
|
17
|
+
const blocks = (size) => Math.ceil(size / BLOCK) * BLOCK
|
|
26
18
|
|
|
27
|
-
|
|
19
|
+
// A sparse entry's body is not the file; libarchive also takes star's and Solaris' keys for one
|
|
20
|
+
const SPARSE_KEYS = new Set(['SCHILY.realsize', 'SUN.holesdata'])
|
|
21
|
+
function refuseSparse(keys, at) {
|
|
28
22
|
if (keys.some((key) => key.startsWith('GNU.sparse.') || SPARSE_KEYS.has(key))) throw new ArchiveError('sparse entries are not supported', at)
|
|
29
23
|
}
|
|
30
24
|
|
|
@@ -33,11 +27,7 @@ function paxNumber(value, what, at) {
|
|
|
33
27
|
return Number(value)
|
|
34
28
|
}
|
|
35
29
|
|
|
36
|
-
// Whole seconds,
|
|
37
|
-
// Read from the digits, not through a double: near today a double steps by
|
|
38
|
-
// a quarter of a microsecond, so .9999999 of a second would round up to the
|
|
39
|
-
// next one before any floor saw it. Flooring moves a negative time with any
|
|
40
|
-
// fraction a second earlier, and nothing else.
|
|
30
|
+
// Whole seconds, floored as GNU does. From the digits, as a double would round .9999999 up to the next second.
|
|
41
31
|
function paxTime(value, what, at) {
|
|
42
32
|
const match = /^(-?)([0-9]+)(?:\.([0-9]+))?$/u.exec(value)
|
|
43
33
|
if (!match) throw new ArchiveError(`pax ${what}=${quote(value)} is not a time`, at)
|
|
@@ -47,66 +37,51 @@ function paxTime(value, what, at) {
|
|
|
47
37
|
return seconds
|
|
48
38
|
}
|
|
49
39
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
// The records of every entry with no pax header of its own, or under no
|
|
55
|
-
// global one.
|
|
56
|
-
const NO_RECORDS = new Records()
|
|
57
|
-
|
|
58
|
-
// The extended headers of every entry with none ahead of it.
|
|
59
|
-
const NO_EXTENDED = Object.freeze([])
|
|
60
|
-
const magicOf = (header) => (header.gnu ? 'gnu' : 'ustar')
|
|
40
|
+
function headerName(header, at) {
|
|
41
|
+
const name = decodeUtf8(header.name, 'entry name', at)
|
|
42
|
+
return header.prefix.length ? `${decodeUtf8(header.prefix, 'name prefix', at)}/${name}` : name
|
|
43
|
+
}
|
|
61
44
|
|
|
45
|
+
// Chunks in, entries out as each completes; the in-memory call and the stream share it
|
|
62
46
|
class Unpacker {
|
|
63
|
-
#chunks = []
|
|
47
|
+
#chunks = [] // unread input, from #offset in the first
|
|
64
48
|
#offset = 0
|
|
65
49
|
#buffered = 0
|
|
66
|
-
#position = 0
|
|
50
|
+
#position = 0 // in the archive, of the first byte unread
|
|
67
51
|
#names
|
|
68
|
-
#pending =
|
|
69
|
-
//
|
|
70
|
-
// kind and its magic.
|
|
71
|
-
#extended = []
|
|
52
|
+
#pending = { pax: null, longname: null, longlink: null } // extended headers awaiting their entry
|
|
53
|
+
#extended = [] // each extended header since the last entry, as entries list them
|
|
72
54
|
#global = null
|
|
73
|
-
//
|
|
74
|
-
#awaiting = null
|
|
55
|
+
#awaiting = null // the header whose body is next: { at, size, entry } or { at, size, extended }
|
|
75
56
|
#zeros = 0
|
|
76
57
|
#done = false
|
|
77
58
|
#count = 0
|
|
78
59
|
#maxEntries
|
|
79
60
|
#trailingGlobal
|
|
80
61
|
|
|
81
|
-
// `keep` holds entries for a repeat to be compared with, which only the
|
|
82
|
-
// in-memory call can afford; the options are unpack's.
|
|
83
62
|
constructor(keep, { symlinksAbove, maxEntries = Infinity, trailingGlobal = false } = {}) {
|
|
84
63
|
if (maxEntries !== Infinity && !(Number.isSafeInteger(maxEntries) && maxEntries >= 0)) throw new ArchiveError(`maxEntries ${String(maxEntries)} is not a whole number of entries`)
|
|
85
64
|
if (typeof trailingGlobal !== 'boolean') throw new ArchiveError(`trailingGlobal ${String(trailingGlobal)} is not a boolean`)
|
|
86
|
-
this.#names = new Names(keep,
|
|
65
|
+
this.#names = new Names(keep, symlinksAbove)
|
|
87
66
|
this.#maxEntries = maxEntries
|
|
88
67
|
this.#trailingGlobal = trailingGlobal
|
|
89
68
|
}
|
|
90
69
|
|
|
91
|
-
//
|
|
92
|
-
// holding many has no more of them held than the one being read.
|
|
70
|
+
// Each entry as it is read, so a chunk holding many holds no more of them than the one being read
|
|
93
71
|
*push(chunk) {
|
|
94
72
|
if (!(chunk instanceof Uint8Array)) throw new ArchiveError('a chunk is not a Uint8Array')
|
|
95
|
-
if (this.#done) {
|
|
96
|
-
this.#trailing(chunk, this.#position)
|
|
97
|
-
this.#position += chunk.length
|
|
98
|
-
return
|
|
99
|
-
}
|
|
100
73
|
if (chunk.length) {
|
|
101
74
|
this.#chunks.push(chunk)
|
|
102
75
|
this.#buffered += chunk.length
|
|
103
76
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
77
|
+
while (!this.#done) {
|
|
78
|
+
const bytes = this.#take(this.#awaiting ? blocks(this.#awaiting.size) : BLOCK)
|
|
79
|
+
if (bytes === null) return
|
|
80
|
+
const entry = this.#awaiting ? this.#body(bytes) : this.#header(bytes)
|
|
81
|
+
if (entry) yield entry
|
|
109
82
|
}
|
|
83
|
+
const at = this.#position
|
|
84
|
+
if (!isZeroBlock(this.#take(this.#buffered))) throw new ArchiveError('data after the end of the archive', at)
|
|
110
85
|
}
|
|
111
86
|
|
|
112
87
|
end() {
|
|
@@ -116,28 +91,22 @@ class Unpacker {
|
|
|
116
91
|
throw new ArchiveError('the archive is truncated', this.#position)
|
|
117
92
|
}
|
|
118
93
|
|
|
119
|
-
// The next `size` bytes
|
|
120
|
-
// when one chunk holds them, else a copy of just that span out of the
|
|
121
|
-
// chunks it crosses, however much more those chunks hold.
|
|
94
|
+
// The next `size` bytes once all have arrived: a view where one chunk holds them, else a copy
|
|
122
95
|
#take(size) {
|
|
123
96
|
if (this.#buffered < size) return null
|
|
124
97
|
if (size === 0) return EMPTY
|
|
125
98
|
let out
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
this.#offset
|
|
99
|
+
let end = this.#offset + size
|
|
100
|
+
if (end <= this.#chunks[0].length) {
|
|
101
|
+
out = this.#chunks[0].subarray(this.#offset, end)
|
|
129
102
|
} else {
|
|
130
|
-
out = new Uint8Array(size)
|
|
131
103
|
let i = 0
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
at += piece.length
|
|
136
|
-
this.#offset = from + piece.length
|
|
137
|
-
}
|
|
138
|
-
this.#chunks = this.#chunks.slice(i - 1)
|
|
104
|
+
while (end > this.#chunks[i].length) end -= this.#chunks[i++].length
|
|
105
|
+
out = concat([this.#chunks[0].subarray(this.#offset), ...this.#chunks.slice(1, i), this.#chunks[i].subarray(0, end)])
|
|
106
|
+
this.#chunks.splice(0, i)
|
|
139
107
|
}
|
|
140
|
-
|
|
108
|
+
this.#offset = end
|
|
109
|
+
if (end === this.#chunks[0].length) {
|
|
141
110
|
this.#chunks.shift()
|
|
142
111
|
this.#offset = 0
|
|
143
112
|
}
|
|
@@ -146,81 +115,62 @@ class Unpacker {
|
|
|
146
115
|
return out
|
|
147
116
|
}
|
|
148
117
|
|
|
149
|
-
#
|
|
150
|
-
if (!isZeroBlock(bytes)) throw new ArchiveError('data after the end of the archive', at)
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
// False when more bytes are needed.
|
|
154
|
-
#header() {
|
|
155
|
-
const block = this.#take(BLOCK)
|
|
156
|
-
if (block === null) return false
|
|
118
|
+
#header(block) {
|
|
157
119
|
const at = this.#position - BLOCK
|
|
158
120
|
if (isZeroBlock(block)) {
|
|
159
|
-
// A global header
|
|
160
|
-
// unless the caller takes it as GNU tar does (trailingGlobal).
|
|
121
|
+
// A global header may trail if the caller takes it so, as GNU tar does
|
|
161
122
|
if (this.#extended.some(({ kind }) => kind !== 'global' || !this.#trailingGlobal)) throw new ArchiveError('an extended header is not followed by an entry', at)
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
const after = this.#position
|
|
165
|
-
this.#trailing(this.#take(this.#buffered), after)
|
|
166
|
-
}
|
|
167
|
-
return true
|
|
123
|
+
this.#done = ++this.#zeros === 2
|
|
124
|
+
return
|
|
168
125
|
}
|
|
169
126
|
if (this.#zeros) throw new ArchiveError('a lone zero block where a header should be', at)
|
|
170
127
|
const header = decodeHeader(block, at)
|
|
171
|
-
const
|
|
172
|
-
if (
|
|
173
|
-
// Counted
|
|
128
|
+
const kind = EXTENDED.get(header.typeflag)
|
|
129
|
+
if (kind === undefined) {
|
|
130
|
+
// Counted before anything is made of it
|
|
174
131
|
if (++this.#count > this.#maxEntries) throw new ArchiveError(`the archive holds more than ${this.#maxEntries} entries`, at, { limited: true })
|
|
175
|
-
this.#awaiting = {
|
|
132
|
+
this.#awaiting = { at, ...this.#resolve(header, at) }
|
|
176
133
|
} else {
|
|
177
|
-
if (header.size > MAX_EXTENDED) throw new ArchiveError(`a ${
|
|
178
|
-
this.#awaiting = {
|
|
134
|
+
if (header.size > MAX_EXTENDED) throw new ArchiveError(`a ${kind} header of ${header.size} bytes is longer than any tar writes`, at)
|
|
135
|
+
this.#awaiting = { at, size: header.size, extended: Object.freeze({ kind, magic: magicOf(header), octal: header.octal }) }
|
|
179
136
|
}
|
|
180
|
-
return true
|
|
181
137
|
}
|
|
182
138
|
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
const
|
|
186
|
-
const body = this.#take(whole)
|
|
187
|
-
if (body === null) return false
|
|
188
|
-
if (!isZeroBlock(body.subarray(size))) throw new ArchiveError('the padding after an entry is not zero', at)
|
|
139
|
+
// An entry's data, returning the entry, or an extended header's records
|
|
140
|
+
#body(bytes) {
|
|
141
|
+
const { at, size, entry, extended } = this.#awaiting
|
|
189
142
|
this.#awaiting = null
|
|
190
|
-
|
|
191
|
-
|
|
143
|
+
if (!isZeroBlock(bytes.subarray(size))) throw new ArchiveError('the padding after an entry is not zero', at)
|
|
144
|
+
const raw = bytes.subarray(0, size)
|
|
145
|
+
if (entry) {
|
|
192
146
|
entry.data = raw
|
|
193
147
|
located(() => this.#names.add(entry), at)
|
|
194
|
-
|
|
195
|
-
return true
|
|
148
|
+
return entry
|
|
196
149
|
}
|
|
197
|
-
|
|
150
|
+
const { kind } = extended
|
|
151
|
+
if (kind === 'global') {
|
|
198
152
|
this.#global = decodePax(raw, at)
|
|
199
153
|
const set = ['path', 'linkpath', 'size'].find((key) => this.#global.has(key))
|
|
200
154
|
if (set !== undefined) throw new ArchiveError(`a global header sets ${set}`, at)
|
|
201
|
-
|
|
202
|
-
// re-reading its keys under every entry is work an archive can ask for.
|
|
203
|
-
sparse(this.#global.keys(), at)
|
|
155
|
+
refuseSparse(this.#global.keys(), at) // once here, not under every entry it stands for
|
|
204
156
|
} else {
|
|
205
|
-
if (this.#pending[
|
|
206
|
-
this.#pending[
|
|
157
|
+
if (this.#pending[kind] !== null) throw new ArchiveError(`two ${kind} headers ahead of one entry`, at)
|
|
158
|
+
this.#pending[kind] = kind === 'pax' ? decodePax(raw, at) : decodeUtf8(untilNul(raw), `a ${kind} header`, at)
|
|
207
159
|
}
|
|
208
|
-
this.#extended.push(
|
|
209
|
-
return true
|
|
160
|
+
this.#extended.push(extended)
|
|
210
161
|
}
|
|
211
162
|
|
|
212
|
-
// The entry a header stands for,
|
|
213
|
-
// applied: a pax record wins over the field it stands in for.
|
|
163
|
+
// The entry a header stands for, a pax record winning over the field it stands in for
|
|
214
164
|
#resolve(header, at) {
|
|
215
165
|
const type = TYPES.get(header.typeflag)
|
|
216
166
|
if (type === undefined) throw new ArchiveError(`entry type ${quote(String.fromCodePoint(header.typeflag))} is not one this package reads`, at)
|
|
217
167
|
const { pax, longname, longlink } = this.#pending
|
|
218
|
-
this.#pending =
|
|
168
|
+
this.#pending = { pax: null, longname: null, longlink: null }
|
|
219
169
|
const record = (key) => pax?.get(key) ?? this.#global?.get(key)
|
|
220
|
-
if (pax !== null)
|
|
170
|
+
if (pax !== null) refuseSparse(pax.keys(), at)
|
|
221
171
|
if (longname !== null && record('path') !== undefined) throw new ArchiveError('both a long name header and a pax path name one entry', at)
|
|
222
172
|
if (longlink !== null && record('linkpath') !== undefined) throw new ArchiveError('both a long link header and a pax linkpath name one entry', at)
|
|
223
|
-
const rawName = longname ?? record('path') ??
|
|
173
|
+
const rawName = longname ?? record('path') ?? headerName(header, at)
|
|
224
174
|
const rawTarget = longlink ?? record('linkpath') ?? decodeUtf8(header.linkname, 'link target', at)
|
|
225
175
|
const { name, linkname } = located(() => cleanNames(rawName, type, rawTarget, this.#names.above), at)
|
|
226
176
|
const number = (key, parse = paxNumber) => {
|
|
@@ -262,11 +212,6 @@ class Unpacker {
|
|
|
262
212
|
this.#extended = []
|
|
263
213
|
return { entry, size }
|
|
264
214
|
}
|
|
265
|
-
|
|
266
|
-
#headerName(header, at) {
|
|
267
|
-
const name = decodeUtf8(header.name, 'entry name', at)
|
|
268
|
-
return header.prefix.length ? `${decodeUtf8(header.prefix, 'name prefix', at)}/${name}` : name
|
|
269
|
-
}
|
|
270
215
|
}
|
|
271
216
|
|
|
272
217
|
export function unpack(bytes, options) {
|
package/src/text.js
CHANGED
|
@@ -2,51 +2,25 @@ import { utf8fromString, utf8toString } from '@exodus/bytes/utf8.js'
|
|
|
2
2
|
import { ArchiveError } from './error.js'
|
|
3
3
|
|
|
4
4
|
// Escaped, so a name in a message cannot carry a terminal sequence, and cut
|
|
5
|
-
// short
|
|
5
|
+
// short, not inside a surrogate pair, so a long one cannot flood it
|
|
6
6
|
export function quote(text) {
|
|
7
7
|
if (text.length <= 200) return JSON.stringify(text)
|
|
8
8
|
return JSON.stringify(`${text.slice(0, text.codePointAt(199) > 0xffff ? 199 : 200)}\u2026`)
|
|
9
9
|
}
|
|
10
10
|
|
|
11
|
-
//
|
|
12
|
-
// paragraph separator (Zl, Zp: one each); and every bidirectional control
|
|
13
|
-
// Unicode names -- embeddings, overrides and isolates, and the three marks
|
|
14
|
-
// that shift neutral characters about them unseen -- since each breaks or
|
|
15
|
-
// reorders a name as shown. A backslash too, if asked.
|
|
11
|
+
// Controls, line and paragraph separators, and bidi controls: each breaks or reorders a name as shown
|
|
16
12
|
const UNSAFE = /[\p{Cc}\p{Zl}\p{Zp}\p{Bidi_Control}]/u
|
|
17
|
-
const UNSAFE_OR_BACKSLASH = /[\p{Cc}\p{Zl}\p{Zp}\p{Bidi_Control}\\]/u
|
|
18
13
|
|
|
19
|
-
export const hasUnsafe = (text, backslash) => (
|
|
14
|
+
export const hasUnsafe = (text, backslash) => UNSAFE.test(text) || (backslash && text.includes('\\'))
|
|
20
15
|
|
|
21
|
-
// Every string
|
|
22
|
-
// surrogate has no UTF-8, and so no length or bytes for any later check
|
|
23
|
-
// to look at, and JSON.stringify would only hide it.
|
|
16
|
+
// Every string from a caller passes here first: a lone surrogate has no UTF-8 to measure or write
|
|
24
17
|
export function checkString(value, what) {
|
|
25
18
|
if (typeof value !== 'string') throw new ArchiveError(`${what} is not a string`)
|
|
26
19
|
if (!value.isWellFormed()) throw new ArchiveError(`${what} is not well-formed Unicode`)
|
|
27
20
|
}
|
|
28
21
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
for (const char of text) {
|
|
32
|
-
const code = char.codePointAt(0)
|
|
33
|
-
length += code < 0x80 ? 1 : code < 0x800 ? 2 : code < 0x10000 ? 3 : 4
|
|
34
|
-
}
|
|
35
|
-
return length
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
// Whether text is more than `max` bytes of UTF-8. A code unit is at least
|
|
39
|
-
// a byte and at most three, so text of more units than `max` is over, and
|
|
40
|
-
// of a third as many is not, without being counted.
|
|
41
|
-
export const tooLong = (text, max) => text.length > max || (text.length * 3 > max && utf8Length(text) > max)
|
|
42
|
-
|
|
43
|
-
export function encodeUtf8(text, what) {
|
|
44
|
-
try {
|
|
45
|
-
return utf8fromString(text)
|
|
46
|
-
} catch {
|
|
47
|
-
throw new ArchiveError(`${what} is not well-formed Unicode`)
|
|
48
|
-
}
|
|
49
|
-
}
|
|
22
|
+
// A code unit is 1 to 3 bytes of UTF-8, so only text of max/3 to max units is encoded to be measured
|
|
23
|
+
export const tooLong = (text, max) => text.length > max || (text.length * 3 > max && utf8fromString(text).length > max)
|
|
50
24
|
|
|
51
25
|
export function decodeUtf8(raw, what, at) {
|
|
52
26
|
try {
|