@preventive/archive 1.3.0 → 1.4.1
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 +30 -120
- package/src/tar/header.js +17 -43
- package/src/tar/pack.js +33 -48
- package/src/tar/pax.js +6 -11
- package/src/tar/unpack.js +75 -132
- package/src/text.js +6 -32
- package/src/zip/format.js +10 -29
- package/src/zip/unzip.js +47 -93
- package/src/zip/zip.js +24 -32
- package/tar.d.ts +166 -137
- package/tar.js +2 -18
- package/zip.d.ts +106 -82
- package/zip.js +1 -19
package/src/tar/unpack.js
CHANGED
|
@@ -1,33 +1,25 @@
|
|
|
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
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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) {
|
|
22
|
+
if (keys.some((key) => key.startsWith('GNU.sparse.') || SPARSE_KEYS.has(key))) throw new ArchiveError('sparse entries are not supported', at)
|
|
31
23
|
}
|
|
32
24
|
|
|
33
25
|
function paxNumber(value, what, at) {
|
|
@@ -35,11 +27,7 @@ function paxNumber(value, what, at) {
|
|
|
35
27
|
return Number(value)
|
|
36
28
|
}
|
|
37
29
|
|
|
38
|
-
// Whole seconds,
|
|
39
|
-
// Read from the digits, not through a double: near today a double steps by
|
|
40
|
-
// a quarter of a microsecond, so .9999999 of a second would round up to the
|
|
41
|
-
// next one before any floor saw it. Flooring moves a negative time with any
|
|
42
|
-
// 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.
|
|
43
31
|
function paxTime(value, what, at) {
|
|
44
32
|
const match = /^(-?)([0-9]+)(?:\.([0-9]+))?$/u.exec(value)
|
|
45
33
|
if (!match) throw new ArchiveError(`pax ${what}=${quote(value)} is not a time`, at)
|
|
@@ -49,66 +37,51 @@ function paxTime(value, what, at) {
|
|
|
49
37
|
return seconds
|
|
50
38
|
}
|
|
51
39
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
// The records of every entry with no pax header of its own, or under no
|
|
57
|
-
// global one.
|
|
58
|
-
const NO_RECORDS = new Records()
|
|
59
|
-
|
|
60
|
-
// The extended headers of every entry with none ahead of it.
|
|
61
|
-
const NO_EXTENDED = Object.freeze([])
|
|
62
|
-
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
|
+
}
|
|
63
44
|
|
|
45
|
+
// Chunks in, entries out as each completes; the in-memory call and the stream share it
|
|
64
46
|
class Unpacker {
|
|
65
|
-
#chunks = []
|
|
47
|
+
#chunks = [] // unread input, from #offset in the first
|
|
66
48
|
#offset = 0
|
|
67
49
|
#buffered = 0
|
|
68
|
-
#position = 0
|
|
50
|
+
#position = 0 // in the archive, of the first byte unread
|
|
69
51
|
#names
|
|
70
|
-
#pending =
|
|
71
|
-
//
|
|
72
|
-
// kind and its magic.
|
|
73
|
-
#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
|
|
74
54
|
#global = null
|
|
75
|
-
//
|
|
76
|
-
#awaiting = null
|
|
55
|
+
#awaiting = null // the header whose body is next: { at, size, entry } or { at, size, extended }
|
|
77
56
|
#zeros = 0
|
|
78
57
|
#done = false
|
|
79
58
|
#count = 0
|
|
80
59
|
#maxEntries
|
|
81
60
|
#trailingGlobal
|
|
82
61
|
|
|
83
|
-
// `keep` holds entries for a repeat to be compared with, which only the
|
|
84
|
-
// in-memory call can afford; the options are unpack's.
|
|
85
62
|
constructor(keep, { symlinksAbove, maxEntries = Infinity, trailingGlobal = false } = {}) {
|
|
86
63
|
if (maxEntries !== Infinity && !(Number.isSafeInteger(maxEntries) && maxEntries >= 0)) throw new ArchiveError(`maxEntries ${String(maxEntries)} is not a whole number of entries`)
|
|
87
64
|
if (typeof trailingGlobal !== 'boolean') throw new ArchiveError(`trailingGlobal ${String(trailingGlobal)} is not a boolean`)
|
|
88
|
-
this.#names = new Names(keep,
|
|
65
|
+
this.#names = new Names(keep, symlinksAbove)
|
|
89
66
|
this.#maxEntries = maxEntries
|
|
90
67
|
this.#trailingGlobal = trailingGlobal
|
|
91
68
|
}
|
|
92
69
|
|
|
93
|
-
//
|
|
94
|
-
// 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
|
|
95
71
|
*push(chunk) {
|
|
96
72
|
if (!(chunk instanceof Uint8Array)) throw new ArchiveError('a chunk is not a Uint8Array')
|
|
97
|
-
if (this.#done) {
|
|
98
|
-
this.#trailing(chunk, this.#position)
|
|
99
|
-
this.#position += chunk.length
|
|
100
|
-
return
|
|
101
|
-
}
|
|
102
73
|
if (chunk.length) {
|
|
103
74
|
this.#chunks.push(chunk)
|
|
104
75
|
this.#buffered += chunk.length
|
|
105
76
|
}
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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
|
|
111
82
|
}
|
|
83
|
+
const at = this.#position
|
|
84
|
+
if (!isZeroBlock(this.#take(this.#buffered))) throw new ArchiveError('data after the end of the archive', at)
|
|
112
85
|
}
|
|
113
86
|
|
|
114
87
|
end() {
|
|
@@ -118,28 +91,22 @@ class Unpacker {
|
|
|
118
91
|
throw new ArchiveError('the archive is truncated', this.#position)
|
|
119
92
|
}
|
|
120
93
|
|
|
121
|
-
// The next `size` bytes
|
|
122
|
-
// when one chunk holds them, else a copy of just that span out of the
|
|
123
|
-
// 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
|
|
124
95
|
#take(size) {
|
|
125
96
|
if (this.#buffered < size) return null
|
|
126
97
|
if (size === 0) return EMPTY
|
|
127
98
|
let out
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
this.#offset
|
|
99
|
+
let end = this.#offset + size
|
|
100
|
+
if (end <= this.#chunks[0].length) {
|
|
101
|
+
out = this.#chunks[0].subarray(this.#offset, end)
|
|
131
102
|
} else {
|
|
132
|
-
out = new Uint8Array(size)
|
|
133
103
|
let i = 0
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
at += piece.length
|
|
138
|
-
this.#offset = from + piece.length
|
|
139
|
-
}
|
|
140
|
-
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)
|
|
141
107
|
}
|
|
142
|
-
|
|
108
|
+
this.#offset = end
|
|
109
|
+
if (end === this.#chunks[0].length) {
|
|
143
110
|
this.#chunks.shift()
|
|
144
111
|
this.#offset = 0
|
|
145
112
|
}
|
|
@@ -148,82 +115,62 @@ class Unpacker {
|
|
|
148
115
|
return out
|
|
149
116
|
}
|
|
150
117
|
|
|
151
|
-
#
|
|
152
|
-
if (!isZeroBlock(bytes)) throw new ArchiveError('data after the end of the archive', at)
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
// False when more bytes are needed.
|
|
156
|
-
#header() {
|
|
157
|
-
const block = this.#take(BLOCK)
|
|
158
|
-
if (block === null) return false
|
|
118
|
+
#header(block) {
|
|
159
119
|
const at = this.#position - BLOCK
|
|
160
120
|
if (isZeroBlock(block)) {
|
|
161
|
-
// A global header
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
if (++this.#zeros === 2) {
|
|
166
|
-
this.#done = true
|
|
167
|
-
const after = this.#position
|
|
168
|
-
this.#trailing(this.#take(this.#buffered), after)
|
|
169
|
-
}
|
|
170
|
-
return true
|
|
121
|
+
// A global header may trail if the caller takes it so, as GNU tar does
|
|
122
|
+
if (this.#extended.some(({ kind }) => kind !== 'global' || !this.#trailingGlobal)) throw new ArchiveError('an extended header is not followed by an entry', at)
|
|
123
|
+
this.#done = ++this.#zeros === 2
|
|
124
|
+
return
|
|
171
125
|
}
|
|
172
126
|
if (this.#zeros) throw new ArchiveError('a lone zero block where a header should be', at)
|
|
173
127
|
const header = decodeHeader(block, at)
|
|
174
|
-
const
|
|
175
|
-
if (
|
|
176
|
-
// Counted
|
|
128
|
+
const kind = EXTENDED.get(header.typeflag)
|
|
129
|
+
if (kind === undefined) {
|
|
130
|
+
// Counted before anything is made of it
|
|
177
131
|
if (++this.#count > this.#maxEntries) throw new ArchiveError(`the archive holds more than ${this.#maxEntries} entries`, at, { limited: true })
|
|
178
|
-
this.#awaiting = {
|
|
132
|
+
this.#awaiting = { at, ...this.#resolve(header, at) }
|
|
179
133
|
} else {
|
|
180
|
-
if (header.size > MAX_EXTENDED) throw new ArchiveError(`a ${
|
|
181
|
-
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 }) }
|
|
182
136
|
}
|
|
183
|
-
return true
|
|
184
137
|
}
|
|
185
138
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
const
|
|
189
|
-
const body = this.#take(whole)
|
|
190
|
-
if (body === null) return false
|
|
191
|
-
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
|
|
192
142
|
this.#awaiting = null
|
|
193
|
-
|
|
194
|
-
|
|
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) {
|
|
195
146
|
entry.data = raw
|
|
196
147
|
located(() => this.#names.add(entry), at)
|
|
197
|
-
|
|
198
|
-
}
|
|
148
|
+
return entry
|
|
149
|
+
}
|
|
150
|
+
const { kind } = extended
|
|
151
|
+
if (kind === 'global') {
|
|
199
152
|
this.#global = decodePax(raw, at)
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
// Once, here: a global header stands until another replaces it, and
|
|
204
|
-
// re-reading its keys under every entry is work an archive can ask for.
|
|
205
|
-
sparse(this.#global.keys(), at)
|
|
206
|
-
this.#extended.push(Object.freeze({ kind: extended, magic, octal }))
|
|
153
|
+
const set = ['path', 'linkpath', 'size'].find((key) => this.#global.has(key))
|
|
154
|
+
if (set !== undefined) throw new ArchiveError(`a global header sets ${set}`, at)
|
|
155
|
+
refuseSparse(this.#global.keys(), at) // once here, not under every entry it stands for
|
|
207
156
|
} else {
|
|
208
|
-
if (this.#pending[
|
|
209
|
-
this.#pending[
|
|
210
|
-
this.#extended.push(Object.freeze({ kind: extended, magic, octal }))
|
|
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)
|
|
211
159
|
}
|
|
212
|
-
|
|
160
|
+
this.#extended.push(extended)
|
|
213
161
|
}
|
|
214
162
|
|
|
215
|
-
// The entry a header stands for,
|
|
216
|
-
// 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
|
|
217
164
|
#resolve(header, at) {
|
|
218
165
|
const type = TYPES.get(header.typeflag)
|
|
219
166
|
if (type === undefined) throw new ArchiveError(`entry type ${quote(String.fromCodePoint(header.typeflag))} is not one this package reads`, at)
|
|
220
167
|
const { pax, longname, longlink } = this.#pending
|
|
221
|
-
this.#pending =
|
|
168
|
+
this.#pending = { pax: null, longname: null, longlink: null }
|
|
222
169
|
const record = (key) => pax?.get(key) ?? this.#global?.get(key)
|
|
223
|
-
if (pax !== null)
|
|
170
|
+
if (pax !== null) refuseSparse(pax.keys(), at)
|
|
224
171
|
if (longname !== null && record('path') !== undefined) throw new ArchiveError('both a long name header and a pax path name one entry', at)
|
|
225
172
|
if (longlink !== null && record('linkpath') !== undefined) throw new ArchiveError('both a long link header and a pax linkpath name one entry', at)
|
|
226
|
-
const rawName = longname ?? record('path') ??
|
|
173
|
+
const rawName = longname ?? record('path') ?? headerName(header, at)
|
|
227
174
|
const rawTarget = longlink ?? record('linkpath') ?? decodeUtf8(header.linkname, 'link target', at)
|
|
228
175
|
const { name, linkname } = located(() => cleanNames(rawName, type, rawTarget, this.#names.above), at)
|
|
229
176
|
const number = (key, parse = paxNumber) => {
|
|
@@ -238,6 +185,7 @@ class Unpacker {
|
|
|
238
185
|
if (hasUnsafe(value, false)) throw new ArchiveError(`${key} ${quote(value)} holds a control or formatting character`, at)
|
|
239
186
|
return value
|
|
240
187
|
}
|
|
188
|
+
const device = isDevice(type)
|
|
241
189
|
const entry = {
|
|
242
190
|
name,
|
|
243
191
|
type,
|
|
@@ -248,8 +196,8 @@ class Unpacker {
|
|
|
248
196
|
uname: owner('uname'),
|
|
249
197
|
gname: owner('gname'),
|
|
250
198
|
linkname,
|
|
251
|
-
devmajor:
|
|
252
|
-
devminor:
|
|
199
|
+
devmajor: device ? number('devmajor') : 0,
|
|
200
|
+
devminor: device ? number('devminor') : 0,
|
|
253
201
|
data: EMPTY,
|
|
254
202
|
storedName: rawName,
|
|
255
203
|
storedLinkname: rawTarget,
|
|
@@ -264,11 +212,6 @@ class Unpacker {
|
|
|
264
212
|
this.#extended = []
|
|
265
213
|
return { entry, size }
|
|
266
214
|
}
|
|
267
|
-
|
|
268
|
-
#headerName(header, at) {
|
|
269
|
-
const name = decodeUtf8(header.name, 'entry name', at)
|
|
270
|
-
return header.prefix.length ? `${decodeUtf8(header.prefix, 'name prefix', at)}/${name}` : name
|
|
271
|
-
}
|
|
272
215
|
}
|
|
273
216
|
|
|
274
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 {
|
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
|
}
|