@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/zip/format.js
CHANGED
|
@@ -1,6 +1,3 @@
|
|
|
1
|
-
// What the zip half shares: the record signatures and mode bits, little-
|
|
2
|
-
// endian fields, DOS time, and raw deflate under zip's own words.
|
|
3
|
-
|
|
4
1
|
import { compress, decompressInto } from '../compression.js'
|
|
5
2
|
import { ArchiveError } from '../error.js'
|
|
6
3
|
|
|
@@ -8,14 +5,13 @@ export const LOCAL = 0x04034b50
|
|
|
8
5
|
export const CENTRAL = 0x02014b50
|
|
9
6
|
export const END = 0x06054b50
|
|
10
7
|
|
|
11
|
-
//
|
|
8
|
+
// Unix file type bits, as the external attributes carry them
|
|
12
9
|
export const TYPE_MASK = 0o170000
|
|
13
10
|
export const TYPE_BITS = { file: 0o100000, directory: 0o40000, symlink: 0o120000 }
|
|
14
11
|
|
|
15
12
|
export const view = (bytes) => new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength)
|
|
16
13
|
|
|
17
|
-
//
|
|
18
|
-
// that does not fit its width is a bug upstream, not a field to truncate.
|
|
14
|
+
// Little-endian [width, value] pairs; a value that does not fit is a bug, not a field to truncate
|
|
19
15
|
export function record(fields) {
|
|
20
16
|
const out = new Uint8Array(fields.reduce((total, [width]) => total + width, 0))
|
|
21
17
|
const data = view(out)
|
|
@@ -32,30 +28,21 @@ export function record(fields) {
|
|
|
32
28
|
|
|
33
29
|
export const deflate = (bytes) => compress(bytes, 'deflate-raw')
|
|
34
30
|
|
|
35
|
-
//
|
|
36
|
-
// bytes, so no data inflates to more than 1032 times its own size: zlib's
|
|
37
|
-
// best, on zeros, is 1028.8.
|
|
31
|
+
// A deflate match spends at least 2 bits on at most 258 bytes (zlib's best, on zeros, is 1028.8)
|
|
38
32
|
const MAX_RATIO = 1032
|
|
39
33
|
|
|
40
|
-
// Exactly the declared size, made
|
|
41
|
-
// where it is. Room for all of it is made first, so a size the data could
|
|
42
|
-
// not inflate to is refused before that: past it, a few bytes of archive
|
|
43
|
-
// would have room made for gigabytes no valid one of their size holds.
|
|
34
|
+
// Exactly the declared size, room for which is only made where the data could inflate to it
|
|
44
35
|
export async function inflate(bytes, size, at) {
|
|
45
36
|
if (size > bytes.length * MAX_RATIO) throw new ArchiveError('an entry declares more than its data can inflate to', at)
|
|
46
|
-
|
|
47
|
-
try {
|
|
48
|
-
out = await decompressInto(bytes, 'deflate-raw', new Uint8Array(size))
|
|
49
|
-
} catch (error) {
|
|
37
|
+
const out = await decompressInto(bytes, 'deflate-raw', new Uint8Array(size)).catch((error) => {
|
|
50
38
|
throw new ArchiveError(error.limited ? 'an entry inflates to more than its declared size' : 'an entry does not inflate', at)
|
|
51
|
-
}
|
|
39
|
+
})
|
|
52
40
|
if (out.length !== size) throw new ArchiveError('an entry inflates to less than its declared size', at)
|
|
53
41
|
return out
|
|
54
42
|
}
|
|
55
43
|
|
|
56
|
-
//
|
|
57
|
-
|
|
58
|
-
function dos(seconds) {
|
|
44
|
+
// Two-second resolution from 1980 to 2107, in the maker's local time, read and written here as UTC
|
|
45
|
+
export function toDos(seconds) {
|
|
59
46
|
const date = new Date(seconds * 1000)
|
|
60
47
|
const year = date.getUTCFullYear()
|
|
61
48
|
if (year < 1980 || year > 2107) return null
|
|
@@ -65,16 +52,10 @@ function dos(seconds) {
|
|
|
65
52
|
}
|
|
66
53
|
}
|
|
67
54
|
|
|
68
|
-
|
|
69
|
-
const fields = dos(mtime)
|
|
70
|
-
if (fields === null) throw new ArchiveError(`mtime ${mtime} is outside DOS time, 1980 to 2107`)
|
|
71
|
-
return fields
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
// Fields that do not come back as themselves named no date at all.
|
|
55
|
+
// Fields that do not come back as themselves name no date
|
|
75
56
|
export function fromDos(date, time, at) {
|
|
76
57
|
const seconds = Date.UTC(1980 + (date >> 9), ((date >> 5) & 15) - 1, date & 31, time >> 11, (time >> 5) & 63, (time & 31) * 2) / 1000
|
|
77
|
-
const fields =
|
|
58
|
+
const fields = toDos(seconds)
|
|
78
59
|
if (fields === null || fields.time !== time || fields.date !== date) throw new ArchiveError('an entry has an invalid DOS time', at)
|
|
79
60
|
return seconds
|
|
80
61
|
}
|
package/src/zip/unzip.js
CHANGED
|
@@ -1,46 +1,26 @@
|
|
|
1
|
-
// A zip archive in, its entries out, in the order of the central directory,
|
|
2
|
-
// which is what a zip is read from. Strict throughout: the end record has to
|
|
3
|
-
// sit at the very end, the central directory right before it, and the
|
|
4
|
-
// entries have to tile everything before that from byte 0 -- no bytes belong
|
|
5
|
-
// to nothing, no two entries share any. Each local header has to agree with
|
|
6
|
-
// the central directory, the data has to inflate to exactly the declared
|
|
7
|
-
// size and match its CRC-32, and what this package does not model -- zip64,
|
|
8
|
-
// encryption, any method but stored and deflate, several disks -- is refused.
|
|
9
|
-
// Given a limit, the sizes the entries declare may not add up to more than
|
|
10
|
-
// it; since each has to come out at exactly its own, that bounds everything
|
|
11
|
-
// unzip() puts out, and it is known from the central directory alone,
|
|
12
|
-
// before a single byte is inflated.
|
|
13
|
-
//
|
|
14
|
-
// One reader serves both calls. It asks for the archive a range at a time:
|
|
15
|
-
// views of it when it is in memory, and reads of just those bytes when it
|
|
16
|
-
// is a Blob -- a File, or a file opened with fs.openAsBlob -- so a stream
|
|
17
|
-
// over one holds no more of it than the entries' names and the entry it is
|
|
18
|
-
// on. Everything about the layout is checked before the first entry is
|
|
19
|
-
// handed over; what an entry's own data holds is checked as it is reached.
|
|
20
|
-
|
|
21
1
|
import { crc32 } from '@exodus/bytes/crc.js'
|
|
22
2
|
import { EMPTY, sameBytes } from '../bytes.js'
|
|
23
3
|
import { DEFAULT_MODE } from '../entry.js'
|
|
24
4
|
import { ArchiveError, located } from '../error.js'
|
|
25
|
-
import {
|
|
26
|
-
import { Names, cleanNames, levelsAbove } from '../names.js'
|
|
5
|
+
import { Names, cleanNames } from '../names.js'
|
|
27
6
|
import { decodeUtf8, quote } from '../text.js'
|
|
7
|
+
import { CENTRAL, END, LOCAL, TYPE_BITS, TYPE_MASK, fromDos, inflate, view } from './format.js'
|
|
8
|
+
|
|
9
|
+
// Strict: the end record ends the archive, the central directory ends at it, and the entries tile
|
|
10
|
+
// everything before it from byte 0. Each local header agrees with the central directory, each entry
|
|
11
|
+
// inflates to exactly its declared size and CRC-32; zip64, encryption and other methods are refused.
|
|
28
12
|
|
|
29
13
|
const DESCRIPTOR = 0x08074b50
|
|
30
14
|
const ZIP64_LOCATOR = 0x07064b50
|
|
31
15
|
const ZIP64_EXTRA = 0x0001
|
|
32
16
|
const TIMESTAMP_EXTRA = 0x5455
|
|
33
|
-
|
|
34
|
-
// the one in the header when its CRC-32 matches that header name.
|
|
35
|
-
const UNICODE_PATH_EXTRA = 0x7075
|
|
17
|
+
const UNICODE_PATH_EXTRA = 0x7075 // a second name, which Info-ZIP's unzip takes over the header's
|
|
36
18
|
|
|
37
19
|
const ENCRYPTED = 1
|
|
38
20
|
const DESCRIBED = 8 // sizes and CRC follow the data, and may be 0 in the local header
|
|
39
21
|
const DOS_DIRECTORY = 0x10
|
|
40
22
|
|
|
41
|
-
// Bounds-checked
|
|
42
|
-
// the archive: offsets are the archive's own, and one past what was read,
|
|
43
|
-
// which stops short only at the archive's end, is past its end.
|
|
23
|
+
// Bounds-checked reads of `bytes`, which sit at `base` in the archive, by the archive's own offsets
|
|
44
24
|
function reader(bytes, base = 0) {
|
|
45
25
|
const data = view(bytes)
|
|
46
26
|
const check = (at, width) => {
|
|
@@ -49,14 +29,11 @@ function reader(bytes, base = 0) {
|
|
|
49
29
|
return {
|
|
50
30
|
u16: (at) => (check(at, 2), data.getUint16(at - base, true)),
|
|
51
31
|
u32: (at) => (check(at, 4), data.getUint32(at - base, true)),
|
|
52
|
-
|
|
32
|
+
range: (at, width) => (check(at, width), bytes.subarray(at - base, at - base + width)),
|
|
53
33
|
}
|
|
54
34
|
}
|
|
55
35
|
|
|
56
|
-
//
|
|
57
|
-
// bytes themselves, `window` for a reader over them, cut short where the
|
|
58
|
-
// archive ends. In memory, a range is a view and every window is the one
|
|
59
|
-
// reader over the whole; a Blob reads each range as it is asked for.
|
|
36
|
+
// The archive a range at a time: views of it in memory, reads of just that range from a Blob
|
|
60
37
|
function sourceOf(archive) {
|
|
61
38
|
if (archive instanceof Uint8Array) {
|
|
62
39
|
const whole = reader(archive)
|
|
@@ -69,11 +46,8 @@ function sourceOf(archive) {
|
|
|
69
46
|
throw new ArchiveError('the archive is not a Uint8Array or a Blob')
|
|
70
47
|
}
|
|
71
48
|
|
|
72
|
-
//
|
|
73
|
-
//
|
|
74
|
-
// and over what else the read it came from holds -- up to CHUNK bytes past
|
|
75
|
-
// `at`, and short of `stop`, so that the records after it come in the same
|
|
76
|
-
// read and nothing past them is read to no purpose.
|
|
49
|
+
// Reads for a walk forward through small records: each window reads ahead up to CHUNK bytes,
|
|
50
|
+
// short of `stop`, so the records after come in the same read and data past them is not read
|
|
77
51
|
const CHUNK = 1 << 16
|
|
78
52
|
|
|
79
53
|
function walk(source) {
|
|
@@ -91,9 +65,7 @@ function walk(source) {
|
|
|
91
65
|
}
|
|
92
66
|
}
|
|
93
67
|
|
|
94
|
-
// The
|
|
95
|
-
// Read with the 20 bytes ahead of the furthest back it can start, where a
|
|
96
|
-
// zip64 locator would sit.
|
|
68
|
+
// The last 22 bytes and the comment they count, read with the 20 before, where a zip64 locator would be
|
|
97
69
|
async function findEnd(source) {
|
|
98
70
|
const { size } = source
|
|
99
71
|
const r = await source.window(Math.max(0, size - 22 - 0xffff - 20), size)
|
|
@@ -106,8 +78,7 @@ async function findEnd(source) {
|
|
|
106
78
|
throw new ArchiveError('no end of central directory record')
|
|
107
79
|
}
|
|
108
80
|
|
|
109
|
-
//
|
|
110
|
-
// in the order stored; copies, so that none holds on to the chunk read.
|
|
81
|
+
// Each a copy, holding on to no chunk
|
|
111
82
|
function extras(raw, at) {
|
|
112
83
|
const fields = []
|
|
113
84
|
const r = reader(raw)
|
|
@@ -116,8 +87,6 @@ function extras(raw, at) {
|
|
|
116
87
|
const size = r.u16(pos + 2)
|
|
117
88
|
if (pos + 4 + size > raw.length) throw new ArchiveError('an extra field runs past its room', at)
|
|
118
89
|
if (id === ZIP64_EXTRA) throw new ArchiveError('zip64 is not supported', at)
|
|
119
|
-
// Every name here is UTF-8 already, so this field can only disagree with
|
|
120
|
-
// the name checked above -- and unzip would extract the name it carries.
|
|
121
90
|
if (id === UNICODE_PATH_EXTRA) throw new ArchiveError('an entry carries a second name in a Unicode path extra field', at)
|
|
122
91
|
fields.push({ id, data: raw.slice(pos + 4, pos + 4 + size) })
|
|
123
92
|
pos += 4 + size
|
|
@@ -145,18 +114,15 @@ async function readCentral(read, at) {
|
|
|
145
114
|
internal: r.u16(at + 36),
|
|
146
115
|
attributes: r.u32(at + 38),
|
|
147
116
|
offset: r.u32(at + 42),
|
|
148
|
-
|
|
149
|
-
name: r.slice(at + 46, nameLength).slice(),
|
|
117
|
+
name: r.range(at + 46, nameLength).slice(), // a copy, holding on to no chunk
|
|
150
118
|
next: at + 46 + nameLength + extraLength + r.u16(at + 32),
|
|
151
119
|
}
|
|
152
120
|
if (r.u16(at + 34) !== 0) throw new ArchiveError('the archive spans several disks', at)
|
|
153
121
|
if (entry.flags & ENCRYPTED) throw new ArchiveError('an entry is encrypted', at)
|
|
154
122
|
if (entry.method !== 0 && entry.method !== 8) throw new ArchiveError(`compression method ${entry.method} is not stored or deflate`, at)
|
|
155
123
|
if (entry.csize === 0xffffffff || entry.usize === 0xffffffff || entry.offset === 0xffffffff) throw new ArchiveError('zip64 is not supported', at)
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
entry.mtime = fromDos(entry.date, entry.time, at)
|
|
159
|
-
entry.extra = extras(r.slice(at + 46 + nameLength, extraLength), at)
|
|
124
|
+
entry.mtime = fromDos(entry.date, entry.time, at) // a valid DOS time even where the extended one is kept
|
|
125
|
+
entry.extra = extras(r.range(at + 46 + nameLength, extraLength), at)
|
|
160
126
|
const stamp = entry.extra.find((field) => field.id === TIMESTAMP_EXTRA)?.data
|
|
161
127
|
if (stamp !== undefined && stamp[0] & 1) {
|
|
162
128
|
if (stamp.length < 5) throw new ArchiveError('an extended timestamp is cut short', at)
|
|
@@ -165,35 +131,28 @@ async function readCentral(read, at) {
|
|
|
165
131
|
return entry
|
|
166
132
|
}
|
|
167
133
|
|
|
168
|
-
//
|
|
169
|
-
// entry
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
// one and not the other is read two ways by two readers -- and the local
|
|
173
|
-
// sizes and CRC may be 0 only where a descriptor carries them. `stop` is
|
|
174
|
-
// as far as the header is read ahead of itself.
|
|
175
|
-
async function readLocal(read, entry, boundary, stop) {
|
|
176
|
-
const at = entry.offset
|
|
134
|
+
// Checks the local header against the central one, flags included, as readers differ on a descriptor bit
|
|
135
|
+
// set in one alone. Returns where the entry ends, which must be `boundary`; `stop` bounds the read-ahead.
|
|
136
|
+
async function readLocal(read, entry) {
|
|
137
|
+
const { offset: at, boundary, stop } = entry
|
|
177
138
|
let r = await read(at, 30, stop)
|
|
178
139
|
if (r.u32(at) !== LOCAL) throw new ArchiveError('no local header where the central directory points', at)
|
|
179
140
|
const nameLength = r.u16(at + 26)
|
|
180
141
|
const extraLength = r.u16(at + 28)
|
|
181
142
|
r = await read(at, 30 + nameLength + extraLength, stop)
|
|
182
|
-
if (!sameBytes(r.
|
|
143
|
+
if (!sameBytes(r.range(at + 30, nameLength), entry.name)) throw new ArchiveError('the local header names a different entry', at)
|
|
183
144
|
const shared = [[4, 'version', 'a different version'], [6, 'flags', 'different flags'], [8, 'method', 'a different compression method'], [10, 'time', 'a different time'], [12, 'date', 'a different date']]
|
|
184
145
|
for (const [field, key, what] of shared) {
|
|
185
146
|
if (r.u16(at + field) !== entry[key]) throw new ArchiveError(`the local header has ${what}`, at)
|
|
186
147
|
}
|
|
187
|
-
extras(r.
|
|
148
|
+
extras(r.range(at + 30 + nameLength, extraLength), at)
|
|
188
149
|
const described = entry.flags & DESCRIBED
|
|
189
150
|
const agrees = (field, value) => r.u32(at + field) === value || (described && r.u32(at + field) === 0)
|
|
190
151
|
if (!agrees(14, entry.crc) || !agrees(18, entry.csize) || !agrees(22, entry.usize)) throw new ArchiveError('the local header disagrees with the central directory', at)
|
|
191
152
|
entry.dataAt = at + 30 + nameLength + extraLength
|
|
192
153
|
let end = entry.dataAt + entry.csize
|
|
193
154
|
if (described) {
|
|
194
|
-
//
|
|
195
|
-
// that very value, so both layouts are tried against the record; where
|
|
196
|
-
// both fit (every word the signature), the one reaching the boundary is it.
|
|
155
|
+
// With or without its signature, which a CRC can equal: where both fit, the one reaching `boundary`
|
|
197
156
|
const d = await read(end, 16, boundary)
|
|
198
157
|
const matches = (from) => d.u32(from) === entry.crc && d.u32(from + 4) === entry.csize && d.u32(from + 8) === entry.usize
|
|
199
158
|
const signed = d.u32(end) === DESCRIPTOR && matches(end + 4)
|
|
@@ -204,9 +163,7 @@ async function readLocal(read, entry, boundary, stop) {
|
|
|
204
163
|
return end
|
|
205
164
|
}
|
|
206
165
|
|
|
207
|
-
// A directory marker
|
|
208
|
-
// the slash; the slash needs no marker, since Java writes no attributes at
|
|
209
|
-
// all and every JAR has directories.
|
|
166
|
+
// A directory marker needs the slash; the slash needs no marker, as Java writes no attributes
|
|
210
167
|
function typeOf(entry, rawName, mode) {
|
|
211
168
|
const format = mode & TYPE_MASK
|
|
212
169
|
if (rawName.endsWith('/')) {
|
|
@@ -223,33 +180,32 @@ function typeOf(entry, rawName, mode) {
|
|
|
223
180
|
async function entryOf(source, entry, names) {
|
|
224
181
|
const { at, method, csize, usize } = entry
|
|
225
182
|
const rawName = decodeUtf8(entry.name, 'entry name', at)
|
|
226
|
-
//
|
|
227
|
-
const mode = entry.madeBy >> 8 === 3 ? entry.attributes >>> 16 : 0
|
|
183
|
+
const mode = entry.madeBy >> 8 === 3 ? entry.attributes >>> 16 : 0 // only a Unix maker's carry one
|
|
228
184
|
const type = typeOf(entry, rawName, mode)
|
|
229
185
|
if (type === 'directory' && usize !== 0) throw new ArchiveError(`directory ${quote(rawName)} has data`, at)
|
|
230
186
|
if (method === 0 && csize !== usize) throw new ArchiveError('a stored entry has two sizes', at)
|
|
231
|
-
// A Blob
|
|
232
|
-
// layout was checked; the bytes checked are the bytes read, or nothing.
|
|
187
|
+
// A Blob changed since the layout was checked can come back short
|
|
233
188
|
const raw = await source.read(entry.dataAt, entry.dataAt + csize)
|
|
234
189
|
if (raw.length !== csize) throw new ArchiveError('the archive is truncated', entry.dataAt)
|
|
235
190
|
const body = method === 8 ? await inflate(raw, usize, at) : raw
|
|
236
191
|
if (crc32(body) !== entry.crc) throw new ArchiveError('the data does not match its CRC-32', at)
|
|
237
192
|
const target = type === 'symlink' ? decodeUtf8(body, 'symlink target', at) : ''
|
|
238
|
-
const named = located(() => cleanNames(rawName, type, target, names.above), at)
|
|
239
193
|
const out = {
|
|
240
|
-
...
|
|
194
|
+
...located(() => cleanNames(rawName, type, target, names.above), at),
|
|
195
|
+
type, mode: mode === 0 ? DEFAULT_MODE[type] : mode & 0o7777, mtime: entry.mtime, data: type === 'file' ? body : EMPTY, storedName: rawName,
|
|
241
196
|
system: entry.madeBy >> 8, version: entry.version, flags: entry.flags, internalAttributes: entry.internal, attributes: entry.attributes, extra: entry.extra,
|
|
242
197
|
}
|
|
243
198
|
located(() => names.add(out), at)
|
|
244
199
|
return out
|
|
245
200
|
}
|
|
246
201
|
|
|
247
|
-
// The
|
|
248
|
-
|
|
249
|
-
// in-memory call can afford.
|
|
250
|
-
async function* entries(source, { limit = Infinity, symlinksAbove } = {}, keep = false) {
|
|
202
|
+
// The layout is checked whole before the first entry is read
|
|
203
|
+
async function* entries(source, { limit = Infinity, symlinksAbove, symlinkChains = false } = {}, keep = false) {
|
|
251
204
|
if (limit !== Infinity && !(Number.isSafeInteger(limit) && limit >= 0)) throw new ArchiveError(`limit ${String(limit)} is not a whole number of bytes`)
|
|
252
|
-
|
|
205
|
+
if (typeof symlinkChains !== 'boolean') throw new ArchiveError(`symlinkChains ${String(symlinkChains)} is not a boolean`)
|
|
206
|
+
// Settled once every entry is in, before unzip() answers any; a stream has answered each by then
|
|
207
|
+
if (symlinkChains && !keep) throw new ArchiveError('symlinkChains is taken by unzip() alone, which answers no entry before every one is read')
|
|
208
|
+
const names = new Names(keep, symlinksAbove, symlinkChains)
|
|
253
209
|
const { end, r } = await findEnd(source)
|
|
254
210
|
const count = r.u16(end + 8)
|
|
255
211
|
if (r.u16(end + 4) !== 0 || r.u16(end + 6) !== 0 || count !== r.u16(end + 10)) throw new ArchiveError('the archive spans several disks', end)
|
|
@@ -257,9 +213,7 @@ async function* entries(source, { limit = Infinity, symlinksAbove } = {}, keep =
|
|
|
257
213
|
const start = r.u32(end + 16)
|
|
258
214
|
if (count === 0xffff || size === 0xffffffff || start === 0xffffffff) throw new ArchiveError('zip64 is not supported', end)
|
|
259
215
|
if (start + size !== end) throw new ArchiveError('the central directory does not end at the end record', end)
|
|
260
|
-
// A
|
|
261
|
-
// and cut only at the archive's end, so a record counted past the
|
|
262
|
-
// directory meets the end record, as it would in the archive.
|
|
216
|
+
// A window at a time, cut only at the archive's end, so a record counted past the directory meets the end record
|
|
263
217
|
const directory = walk(source)
|
|
264
218
|
const list = []
|
|
265
219
|
let pos = start
|
|
@@ -272,27 +226,23 @@ async function* entries(source, { limit = Infinity, symlinksAbove } = {}, keep =
|
|
|
272
226
|
pos = entry.next
|
|
273
227
|
}
|
|
274
228
|
if (pos !== end) throw new ArchiveError('the central directory does not hold what the end record counts', pos)
|
|
275
|
-
|
|
229
|
+
// Each entry ends where the next starts, the last at the directory. Headers are read ahead through,
|
|
230
|
+
// but not the data between: each `stop` is the next data's start, or past it while it is empty.
|
|
276
231
|
const sorted = list.toSorted((a, b) => a.offset - b.offset)
|
|
277
|
-
// Each entry ends where the next starts, the last at the directory. The
|
|
278
|
-
// headers are read ahead through, but never the data between them, which
|
|
279
|
-
// is read again when its entry is reached: from each header to its own
|
|
280
|
-
// data, give or take a descriptor, by the directory's sizes, or on through
|
|
281
|
-
// the next header where it has none.
|
|
282
|
-
const boundaries = []
|
|
283
|
-
const stops = []
|
|
284
232
|
for (let i = sorted.length - 1; i >= 0; i--) {
|
|
285
|
-
|
|
286
|
-
|
|
233
|
+
const [entry, next] = [sorted[i], sorted[i + 1]]
|
|
234
|
+
entry.boundary = next?.offset ?? start
|
|
235
|
+
entry.stop = entry.csize === 0 && next ? next.stop : entry.boundary - entry.csize
|
|
287
236
|
}
|
|
288
237
|
const locals = walk(source)
|
|
289
|
-
|
|
238
|
+
let expected = 0
|
|
239
|
+
for (const entry of sorted) {
|
|
290
240
|
if (entry.offset !== expected) throw new ArchiveError(entry.offset > expected ? 'bytes belong to no entry' : 'two entries overlap', expected)
|
|
291
|
-
expected = await readLocal(locals, entry
|
|
241
|
+
expected = await readLocal(locals, entry)
|
|
292
242
|
}
|
|
293
243
|
if (expected !== start) throw new ArchiveError('bytes belong to no entry', expected)
|
|
294
|
-
const names = new Names(keep, above)
|
|
295
244
|
for (const entry of list) yield await entryOf(source, entry, names)
|
|
245
|
+
names.settle()
|
|
296
246
|
}
|
|
297
247
|
|
|
298
248
|
export async function unzip(bytes, options) {
|
package/src/zip/zip.js
CHANGED
|
@@ -1,24 +1,22 @@
|
|
|
1
|
-
// Entries in, a zip archive out, laid out the way Info-ZIP lays one out: a
|
|
2
|
-
// Unix maker, the mode in the external attributes, a symlink as a stored
|
|
3
|
-
// entry whose data is its target, an extended-timestamp extra field for an
|
|
4
|
-
// exact UTC mtime, and each file deflated only where that made it smaller.
|
|
5
|
-
|
|
6
1
|
import { crc32 } from '@exodus/bytes/crc.js'
|
|
2
|
+
import { utf8fromString } from '@exodus/bytes/utf8.js'
|
|
7
3
|
import { concat, isAscii } from '../bytes.js'
|
|
8
4
|
import { DEFAULT_MODE, checkEntry, wireName } from '../entry.js'
|
|
9
5
|
import { ArchiveError } from '../error.js'
|
|
6
|
+
import { Names, cleanNames } from '../names.js'
|
|
7
|
+
import { quote } from '../text.js'
|
|
10
8
|
import { CENTRAL, END, LOCAL, TYPE_BITS, deflate, record, toDos } from './format.js'
|
|
11
|
-
|
|
12
|
-
|
|
9
|
+
|
|
10
|
+
// Laid out as Info-ZIP does: a Unix maker with the mode in the external attributes, a symlink stored
|
|
11
|
+
// with its target as data, an extended timestamp, and each file deflated only where that is smaller
|
|
13
12
|
|
|
14
13
|
const METHODS = new Set(['deflate', 'store'])
|
|
15
14
|
|
|
16
15
|
const MADE_BY = (3 << 8) | 20 // Unix, spec 2.0
|
|
17
16
|
const UTF8_NAME = 0x800
|
|
18
|
-
const DOS_EPOCH = 315532800 // 1980-01-01T00:00:00Z
|
|
19
|
-
const LAST_STAMP = 0x7fffffff // the extended timestamp is
|
|
20
|
-
//
|
|
21
|
-
// zip64, so the last value short of each is the most this writes.
|
|
17
|
+
const DOS_EPOCH = 315532800 // 1980-01-01T00:00:00Z
|
|
18
|
+
const LAST_STAMP = 0x7fffffff // the extended timestamp is signed 32-bit
|
|
19
|
+
// 0xffff entries or 0xffffffff bytes send a reader to zip64
|
|
22
20
|
const MAX_ENTRIES = 0xfffe
|
|
23
21
|
const MAX_SIZE = 0xfffffffe
|
|
24
22
|
|
|
@@ -27,45 +25,39 @@ function integer(value, what, min, max) {
|
|
|
27
25
|
return value
|
|
28
26
|
}
|
|
29
27
|
|
|
30
|
-
function normalize(entry) {
|
|
28
|
+
function normalize(entry, above) {
|
|
31
29
|
const checked = checkEntry(entry, TYPE_BITS)
|
|
30
|
+
const { name, type, linkname } = checked
|
|
32
31
|
return {
|
|
33
32
|
...checked,
|
|
34
|
-
mode: integer(entry.mode ?? DEFAULT_MODE[
|
|
33
|
+
mode: integer(entry.mode ?? DEFAULT_MODE[type], 'mode', 0, 0o7777),
|
|
35
34
|
mtime: integer(entry.mtime ?? DOS_EPOCH, 'mtime', DOS_EPOCH, LAST_STAMP),
|
|
35
|
+
...cleanNames(name, type, linkname, above),
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
// 0x5455
|
|
39
|
+
// 0x5455 with flag bit 0 and the mtime, as Info-ZIP writes it in the central directory
|
|
40
40
|
const timestamp = (mtime) => record([[2, 0x5455], [2, 5], [1, 1], [4, mtime]])
|
|
41
41
|
|
|
42
42
|
export async function zip(entries, { method = 'deflate', symlinksAbove } = {}) {
|
|
43
43
|
if (!METHODS.has(method)) throw new ArchiveError(`method ${quote(String(method))} is not deflate or store`)
|
|
44
|
-
const names = new Names(true,
|
|
44
|
+
const names = new Names(true, symlinksAbove)
|
|
45
45
|
const locals = []
|
|
46
46
|
const centrals = []
|
|
47
47
|
let offset = 0
|
|
48
48
|
let count = 0
|
|
49
|
-
for (const
|
|
50
|
-
const
|
|
51
|
-
const e = { ...entry, ...cleanNames(entry.name, entry.type, entry.linkname, names.above) }
|
|
49
|
+
for (const entry of entries) {
|
|
50
|
+
const e = normalize(entry, names.above)
|
|
52
51
|
names.add(e)
|
|
53
52
|
if (++count > MAX_ENTRIES) throw new ArchiveError(`more than ${MAX_ENTRIES} entries would need zip64`)
|
|
54
|
-
const name =
|
|
55
|
-
const body = e.type === 'symlink' ?
|
|
56
|
-
// Before deflating, which
|
|
57
|
-
// ever kept where it made the entry smaller.
|
|
53
|
+
const name = utf8fromString(wireName(e))
|
|
54
|
+
const body = e.type === 'symlink' ? utf8fromString(e.linkname) : e.data
|
|
55
|
+
// Before deflating, which may not make it smaller
|
|
58
56
|
if (body.length > MAX_SIZE || offset > MAX_SIZE) throw new ArchiveError(`${quote(e.name)} would need zip64`)
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
if (packed.length < body.length) {
|
|
64
|
-
stored = packed
|
|
65
|
-
compression = 8
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
const { time, date } = toDos(e.mtime)
|
|
57
|
+
const packed = method === 'deflate' && e.type === 'file' && body.length ? await deflate(body) : body
|
|
58
|
+
const stored = packed.length < body.length ? packed : body
|
|
59
|
+
const compression = stored === body ? 0 : 8
|
|
60
|
+
const { time, date } = toDos(e.mtime) // within DOS time, as normalize holds mtime to 1980-2038
|
|
69
61
|
const extra = timestamp(e.mtime)
|
|
70
62
|
const common = [
|
|
71
63
|
[2, compression === 8 ? 20 : 10], [2, isAscii(name) ? 0 : UTF8_NAME], [2, compression], [2, time], [2, date],
|