@preventive/archive 1.4.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 +29 -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 +40 -94
- package/src/zip/zip.js +24 -32
- package/tar.d.ts +166 -137
- package/tar.js +2 -18
- package/zip.d.ts +103 -98
- package/zip.js +1 -19
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 {
|
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
|
}
|