@push.rocks/smartlinuxrepo 1.1.0 → 1.1.2
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/dist_ts/00_commitinfo_data.js +1 -1
- package/dist_ts/arch/reader.js +2 -2
- package/dist_ts/debian/control.d.ts +7 -1
- package/dist_ts/debian/control.js +26 -4
- package/dist_ts/debian/indexbuilder.js +8 -10
- package/dist_ts/debian/reader.js +6 -2
- package/dist_ts/debian/rules.d.ts +7 -0
- package/dist_ts/debian/rules.js +20 -1
- package/dist_ts/readers/decompress.d.ts +13 -3
- package/dist_ts/readers/decompress.js +401 -26
- package/dist_ts/shared/architecture.d.ts +5 -1
- package/dist_ts/shared/architecture.js +6 -2
- package/dist_ts/shared/limits.d.ts +1 -1
- package/package.json +1 -1
- package/readme.md +4 -2
- package/ts/00_commitinfo_data.ts +1 -1
- package/ts/arch/reader.ts +1 -1
- package/ts/debian/control.ts +25 -3
- package/ts/debian/indexbuilder.ts +11 -9
- package/ts/debian/reader.ts +5 -1
- package/ts/debian/rules.ts +21 -0
- package/ts/readers/decompress.ts +441 -29
- package/ts/shared/architecture.ts +5 -1
- package/ts/shared/limits.ts +1 -1
package/ts/readers/decompress.ts
CHANGED
|
@@ -10,6 +10,11 @@ export interface IDecompressOptions {
|
|
|
10
10
|
/** Bound on the decompressed bytes delivered. */
|
|
11
11
|
maxOutputBytes: number;
|
|
12
12
|
outputLimit: keyof ILinuxPackageLimits;
|
|
13
|
+
/**
|
|
14
|
+
* Bound on the Zstandard frames of a zstd stream (skippable frames do not
|
|
15
|
+
* count): one more is `limit-exceeded` on `maxEntries`.
|
|
16
|
+
*/
|
|
17
|
+
maxZstdFrames: number;
|
|
13
18
|
}
|
|
14
19
|
|
|
15
20
|
/**
|
|
@@ -18,15 +23,20 @@ export interface IDecompressOptions {
|
|
|
18
23
|
* `limit-exceeded` and stops the decoder. `close()` releases the decoder and
|
|
19
24
|
* must be called (see `withDecompressingReader`).
|
|
20
25
|
*
|
|
21
|
-
* - gzip (multi-member)
|
|
22
|
-
*
|
|
26
|
+
* - gzip (multi-member) comes from `node:zlib` `createGunzip`; zstd from one
|
|
27
|
+
* `node:zlib` `createZstdDecompress` fed by the codec's RFC 8878 frame walk,
|
|
28
|
+
* at most `maxZstdFrames` Zstandard frames; xz from `@napi-rs/lzma`
|
|
29
|
+
* `xz.decompressStream` (64 KiB chunks, cancellable).
|
|
23
30
|
* - An error from the inner reader (the caller's source, or a codec limit on
|
|
24
31
|
* it) propagates unchanged.
|
|
25
32
|
* - A compressed stream that ends early is `truncated`; corrupt data is
|
|
26
33
|
* `malformed-archive` with the decoder error as `cause`.
|
|
27
34
|
* - The decoder reads the inner reader ahead and never hands bytes back, so
|
|
28
35
|
* the compressed stream must run to the end of the inner reader (a whole
|
|
29
|
-
* package, or a `LimitedReader` over one archive member).
|
|
36
|
+
* package, or a `LimitedReader` over one archive member). Bytes after the
|
|
37
|
+
* last gzip member or zstd frame are `malformed-archive` (zstd skippable
|
|
38
|
+
* frames are skipped). A reader closed before the end skips the end check;
|
|
39
|
+
* the zstd walk still refuses such bytes when it has already read them.
|
|
30
40
|
* - Opening a zstd decoder on a Node.js without nodejs/node#64593 throws a
|
|
31
41
|
* plain `Error` (see `isZstdDecodingSafe`): a server fault, not a verdict
|
|
32
42
|
* on the package.
|
|
@@ -52,7 +62,7 @@ export function openDecompressingReader(
|
|
|
52
62
|
source = new PassThroughSource(inputArg);
|
|
53
63
|
break;
|
|
54
64
|
case 'gzip':
|
|
55
|
-
source = new
|
|
65
|
+
source = new GzipSource(inputArg);
|
|
56
66
|
break;
|
|
57
67
|
case 'zstd':
|
|
58
68
|
if (!isZstdDecodingSafe(process.versions.node)) {
|
|
@@ -61,7 +71,7 @@ export function openDecompressingReader(
|
|
|
61
71
|
`this process runs Node.js ${process.versions.node}`,
|
|
62
72
|
);
|
|
63
73
|
}
|
|
64
|
-
source = new
|
|
74
|
+
source = new ZstdSource(inputArg, optionsArg.maxZstdFrames);
|
|
65
75
|
break;
|
|
66
76
|
case 'xz':
|
|
67
77
|
source = new XzSource(inputArg);
|
|
@@ -175,28 +185,36 @@ class PassThroughSource implements IChunkSource {
|
|
|
175
185
|
public async close(): Promise<void> {}
|
|
176
186
|
}
|
|
177
187
|
|
|
178
|
-
/**
|
|
179
|
-
|
|
180
|
-
|
|
188
|
+
/**
|
|
189
|
+
* gzip through `node:zlib` `createGunzip`, with stream backpressure. Gunzip
|
|
190
|
+
* decodes concatenated members, as Go's default reader does, but it stops at a
|
|
191
|
+
* NUL byte after a member and ignores the rest of the input, a valid member
|
|
192
|
+
* included; its `bytesWritten` counts only the bytes it consumed. So when the
|
|
193
|
+
* decoder ends by itself it must have consumed every byte read from the inner
|
|
194
|
+
* reader, and the inner reader must be at its end; anything else is
|
|
195
|
+
* `malformed-archive`. A source closed before that skips the check.
|
|
196
|
+
*/
|
|
197
|
+
class GzipSource implements IChunkSource {
|
|
198
|
+
private readonly input: IByteReader;
|
|
181
199
|
private readonly feeder: plugins.stream.Readable;
|
|
182
|
-
private readonly decoder: plugins.zlib.Gunzip
|
|
200
|
+
private readonly decoder: plugins.zlib.Gunzip;
|
|
183
201
|
private readonly iterator: AsyncIterator<Buffer>;
|
|
202
|
+
/** Bytes read from the inner reader. */
|
|
203
|
+
private inputBytes = 0;
|
|
204
|
+
private inputEnded = false;
|
|
184
205
|
private inputFailed = false;
|
|
185
206
|
private inputError: unknown = undefined;
|
|
207
|
+
/** The read in flight on the inner reader; there is never more than one. */
|
|
208
|
+
private pendingRead: Promise<Uint8Array | null> | null = null;
|
|
209
|
+
/** Set when the decoder has ended: the feeder starts no further read. */
|
|
210
|
+
private stopped = false;
|
|
186
211
|
private finished = false;
|
|
187
212
|
|
|
188
|
-
constructor(inputArg: IByteReader
|
|
189
|
-
this.
|
|
190
|
-
const pump = async function* (this:
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
try {
|
|
194
|
-
chunk = await inputArg.read(READ_CHUNK_BYTES);
|
|
195
|
-
} catch (error) {
|
|
196
|
-
this.inputFailed = true;
|
|
197
|
-
this.inputError = error;
|
|
198
|
-
throw error;
|
|
199
|
-
}
|
|
213
|
+
constructor(inputArg: IByteReader) {
|
|
214
|
+
this.input = inputArg;
|
|
215
|
+
const pump = async function* (this: GzipSource): AsyncGenerator<Uint8Array> {
|
|
216
|
+
while (!this.stopped) {
|
|
217
|
+
const chunk = await this.readInput(READ_CHUNK_BYTES);
|
|
200
218
|
if (chunk === null) {
|
|
201
219
|
return;
|
|
202
220
|
}
|
|
@@ -204,7 +222,7 @@ class ZlibSource implements IChunkSource {
|
|
|
204
222
|
}
|
|
205
223
|
};
|
|
206
224
|
this.feeder = plugins.stream.Readable.from(pump.call(this), { objectMode: false });
|
|
207
|
-
this.decoder =
|
|
225
|
+
this.decoder = plugins.zlib.createGunzip();
|
|
208
226
|
// errors surface through the iterator below; the callback only completes the pipeline
|
|
209
227
|
plugins.stream.pipeline(this.feeder, this.decoder, () => {});
|
|
210
228
|
this.iterator = this.decoder[Symbol.asyncIterator]();
|
|
@@ -214,27 +232,421 @@ class ZlibSource implements IChunkSource {
|
|
|
214
232
|
if (this.finished) {
|
|
215
233
|
return null;
|
|
216
234
|
}
|
|
235
|
+
let result: IteratorResult<Buffer>;
|
|
217
236
|
try {
|
|
218
|
-
|
|
219
|
-
if (result.done) {
|
|
220
|
-
this.finished = true;
|
|
221
|
-
return null;
|
|
222
|
-
}
|
|
223
|
-
return result.value;
|
|
237
|
+
result = await this.iterator.next();
|
|
224
238
|
} catch (error) {
|
|
225
239
|
this.finished = true;
|
|
226
240
|
if (this.inputFailed) {
|
|
227
241
|
throw this.inputError;
|
|
228
242
|
}
|
|
229
|
-
throw mapZlibError(error,
|
|
243
|
+
throw mapZlibError(error, 'gzip');
|
|
244
|
+
}
|
|
245
|
+
if (!result.done) {
|
|
246
|
+
return result.value;
|
|
247
|
+
}
|
|
248
|
+
this.finished = true;
|
|
249
|
+
await this.checkEnd();
|
|
250
|
+
return null;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
public async close(): Promise<void> {
|
|
254
|
+
this.finished = true;
|
|
255
|
+
this.stopped = true;
|
|
256
|
+
this.feeder.destroy();
|
|
257
|
+
this.decoder.destroy();
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** Reads the inner reader and counts what it returns. */
|
|
261
|
+
private readInput(maxArg: number): Promise<Uint8Array | null> {
|
|
262
|
+
const pending = this.input.read(maxArg).then(
|
|
263
|
+
(chunk) => {
|
|
264
|
+
this.pendingRead = null;
|
|
265
|
+
if (chunk === null) {
|
|
266
|
+
this.inputEnded = true;
|
|
267
|
+
} else {
|
|
268
|
+
this.inputBytes += chunk.length;
|
|
269
|
+
}
|
|
270
|
+
return chunk;
|
|
271
|
+
},
|
|
272
|
+
(error: unknown) => {
|
|
273
|
+
this.pendingRead = null;
|
|
274
|
+
this.inputFailed = true;
|
|
275
|
+
this.inputError = error;
|
|
276
|
+
throw error;
|
|
277
|
+
},
|
|
278
|
+
);
|
|
279
|
+
this.pendingRead = pending;
|
|
280
|
+
return pending;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* The decoder ended by itself. It must have consumed every byte read from
|
|
285
|
+
* the inner reader, and one more read must report the end of input.
|
|
286
|
+
*/
|
|
287
|
+
private async checkEnd(): Promise<void> {
|
|
288
|
+
this.stopped = true;
|
|
289
|
+
if (this.pendingRead !== null && this.decoder.bytesWritten === this.inputBytes) {
|
|
290
|
+
// a read the feeder started settles first: its bytes count, and no two reads overlap
|
|
291
|
+
await this.pendingRead;
|
|
292
|
+
}
|
|
293
|
+
if (this.inputFailed) {
|
|
294
|
+
throw this.inputError;
|
|
295
|
+
}
|
|
296
|
+
const consumedAll =
|
|
297
|
+
this.decoder.bytesWritten === this.inputBytes && (this.inputEnded || (await this.readInput(1)) === null);
|
|
298
|
+
if (!consumedAll) {
|
|
299
|
+
throw new LinuxRepoError('malformed-archive', 'bytes follow the last gzip member', { field: 'gzip stream' });
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** RFC 8878 §3.1.1: the Zstandard frame magic number 0xFD2FB528, little-endian. */
|
|
305
|
+
const ZSTD_FRAME_MAGIC = Uint8Array.of(0x28, 0xb5, 0x2f, 0xfd);
|
|
306
|
+
/** RFC 8878 §3.1.2: a skippable frame magic number 0x184D2A5?, little-endian, after its first byte 0x5?. */
|
|
307
|
+
const ZSTD_SKIPPABLE_MAGIC = Uint8Array.of(0x50, 0x2a, 0x4d, 0x18);
|
|
308
|
+
/** Dictionary_ID field bytes by Dictionary_ID_Flag (RFC 8878 §3.1.1.1.1). */
|
|
309
|
+
const ZSTD_DICTIONARY_ID_BYTES = [0, 1, 2, 4];
|
|
310
|
+
/** Frame_Content_Size field bytes by Frame_Content_Size_Flag; flag 0 means 1 byte with Single_Segment_Flag, else 0. */
|
|
311
|
+
const ZSTD_CONTENT_SIZE_BYTES = [0, 2, 4, 8];
|
|
312
|
+
const ZSTD_BLOCK_TYPE_RLE = 1;
|
|
313
|
+
const ZSTD_BLOCK_TYPE_RESERVED = 3;
|
|
314
|
+
/** Place values of the bytes of a little-endian integer. */
|
|
315
|
+
const LITTLE_ENDIAN_PLACE = [1, 0x100, 0x10000, 0x1000000];
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Where the frame walk stands: the magic number at a frame start, a skippable
|
|
319
|
+
* frame's size and content, or a Zstandard frame's header descriptor, header
|
|
320
|
+
* fields, block header, block content and checksum.
|
|
321
|
+
*/
|
|
322
|
+
type TZstdWalkState =
|
|
323
|
+
| 'magic'
|
|
324
|
+
| 'skippable-size'
|
|
325
|
+
| 'skippable-body'
|
|
326
|
+
| 'frame-header'
|
|
327
|
+
| 'header-fields'
|
|
328
|
+
| 'block-header'
|
|
329
|
+
| 'block-body'
|
|
330
|
+
| 'checksum';
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* zstd through one `node:zlib` `createZstdDecompress` for the whole stream,
|
|
334
|
+
* fed by the codec's own RFC 8878 frame walk. node:zlib's zstd decoder before
|
|
335
|
+
* Node.js 26 drops the rest of a write after a frame (a second frame in the
|
|
336
|
+
* same write is lost, one in the next write is decoded), and every release
|
|
337
|
+
* ignores non-frame bytes after a frame. So the walk decides the frame
|
|
338
|
+
* boundaries, and no write to the decoder holds the end of one frame and any
|
|
339
|
+
* byte after it: every frame therefore costs at least one write (about 35 µs),
|
|
340
|
+
* which is why `maxZstdFrames` caps the Zstandard frames of a stream.
|
|
341
|
+
* - The walk reads the inner reader in chunks of `READ_CHUNK_BYTES` and parses
|
|
342
|
+
* each chunk synchronously; it writes the longest runs of Zstandard-frame
|
|
343
|
+
* bytes as views into the chunk, each ending at the end of the chunk or at a
|
|
344
|
+
* frame end, so a frame costs one write per chunk it spans, never one per
|
|
345
|
+
* header or block. Magic-number bytes from an earlier read are written as a
|
|
346
|
+
* copy before the run. It keeps no bytes across reads, only the state below.
|
|
347
|
+
* - Skippable frames are skipped, never written; they do not count against
|
|
348
|
+
* the cap. Anything else at a frame start, a set reserved header bit and
|
|
349
|
+
* block type 3 are `malformed-archive`, raised as soon as the byte is read.
|
|
350
|
+
* - Input that ends inside a frame, a magic-number prefix included, or is
|
|
351
|
+
* empty, is `truncated`; at least one Zstandard frame is required.
|
|
352
|
+
* - When the decoder ends by itself it must have consumed every byte written:
|
|
353
|
+
* libzstd and the walk read the same header fields, so one check at the end
|
|
354
|
+
* fails closed. A walk error is rethrown unchanged; a source closed before
|
|
355
|
+
* the end skips the check.
|
|
356
|
+
*/
|
|
357
|
+
class ZstdSource implements IChunkSource {
|
|
358
|
+
private readonly input: IByteReader;
|
|
359
|
+
private readonly maxFrames: number;
|
|
360
|
+
private readonly feeder: plugins.stream.Readable;
|
|
361
|
+
private readonly decoder: plugins.zlib.ZstdDecompress;
|
|
362
|
+
private readonly iterator: AsyncIterator<Buffer>;
|
|
363
|
+
/** Frame bytes the walk has written to the decoder. */
|
|
364
|
+
private written = 0;
|
|
365
|
+
private walkFailed = false;
|
|
366
|
+
private walkError: unknown = undefined;
|
|
367
|
+
/** Set when the source closes: the walk starts no further read. */
|
|
368
|
+
private stopped = false;
|
|
369
|
+
private finished = false;
|
|
370
|
+
|
|
371
|
+
private state: TZstdWalkState = 'magic';
|
|
372
|
+
/** Bytes of the current magic number, skippable frame size or block header read so far. */
|
|
373
|
+
private fieldBytes = 0;
|
|
374
|
+
/** Whether the magic number being read is a skippable frame's. */
|
|
375
|
+
private skippable = false;
|
|
376
|
+
/** A skippable frame size or block header as read so far, little-endian. */
|
|
377
|
+
private fieldValue = 0;
|
|
378
|
+
/** Bytes left in the current header fields, block content, checksum or skippable content. */
|
|
379
|
+
private remaining = 0;
|
|
380
|
+
private descriptor = 0;
|
|
381
|
+
private lastBlock = false;
|
|
382
|
+
private zstandardFrames = 0;
|
|
383
|
+
private sawInput = false;
|
|
384
|
+
|
|
385
|
+
constructor(inputArg: IByteReader, maxFramesArg: number) {
|
|
386
|
+
this.input = inputArg;
|
|
387
|
+
this.maxFrames = maxFramesArg;
|
|
388
|
+
this.feeder = plugins.stream.Readable.from(this.walk(), { objectMode: false });
|
|
389
|
+
this.decoder = plugins.zlib.createZstdDecompress();
|
|
390
|
+
// errors surface through the iterator below; the callback only completes the pipeline
|
|
391
|
+
plugins.stream.pipeline(this.feeder, this.decoder, () => {});
|
|
392
|
+
this.iterator = this.decoder[Symbol.asyncIterator]();
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
public async next(): Promise<Uint8Array | null> {
|
|
396
|
+
if (this.finished) {
|
|
397
|
+
return null;
|
|
398
|
+
}
|
|
399
|
+
let result: IteratorResult<Buffer>;
|
|
400
|
+
try {
|
|
401
|
+
result = await this.iterator.next();
|
|
402
|
+
} catch (error) {
|
|
403
|
+
await this.close();
|
|
404
|
+
if (this.walkFailed) {
|
|
405
|
+
throw this.walkError;
|
|
406
|
+
}
|
|
407
|
+
throw mapZlibError(error, 'zstd');
|
|
408
|
+
}
|
|
409
|
+
if (!result.done) {
|
|
410
|
+
return result.value;
|
|
411
|
+
}
|
|
412
|
+
const consumed = this.decoder.bytesWritten;
|
|
413
|
+
await this.close();
|
|
414
|
+
if (this.walkFailed) {
|
|
415
|
+
throw this.walkError;
|
|
416
|
+
}
|
|
417
|
+
if (consumed !== this.written) {
|
|
418
|
+
throw zstdMalformed(`the zstd decoder ended after ${consumed} of the ${this.written} frame bytes`);
|
|
230
419
|
}
|
|
420
|
+
return null;
|
|
231
421
|
}
|
|
232
422
|
|
|
233
423
|
public async close(): Promise<void> {
|
|
234
424
|
this.finished = true;
|
|
425
|
+
this.stopped = true;
|
|
235
426
|
this.feeder.destroy();
|
|
236
427
|
this.decoder.destroy();
|
|
237
428
|
}
|
|
429
|
+
|
|
430
|
+
/** Reads and parses the inner reader chunk by chunk and yields the runs of Zstandard-frame bytes to write. */
|
|
431
|
+
private async *walk(): AsyncGenerator<Uint8Array> {
|
|
432
|
+
const runs: Uint8Array[] = [];
|
|
433
|
+
for (;;) {
|
|
434
|
+
// only errors the walk raises or reads count as walk errors, not ones a destroyed feeder injects at a yield
|
|
435
|
+
try {
|
|
436
|
+
const chunk = await this.input.read(READ_CHUNK_BYTES);
|
|
437
|
+
if (this.stopped) {
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
if (chunk === null) {
|
|
441
|
+
this.endOfInput();
|
|
442
|
+
return;
|
|
443
|
+
}
|
|
444
|
+
this.parse(chunk, runs);
|
|
445
|
+
} catch (error) {
|
|
446
|
+
this.walkFailed = true;
|
|
447
|
+
this.walkError = error;
|
|
448
|
+
throw error;
|
|
449
|
+
}
|
|
450
|
+
for (const run of runs) {
|
|
451
|
+
this.written += run.length;
|
|
452
|
+
yield run;
|
|
453
|
+
}
|
|
454
|
+
runs.length = 0;
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
/** Advances the state over `chunkArg` and appends the runs of Zstandard-frame bytes it holds to `runsArg`. */
|
|
459
|
+
private parse(chunkArg: Uint8Array, runsArg: Uint8Array[]): void {
|
|
460
|
+
const length = chunkArg.length;
|
|
461
|
+
if (length > 0) {
|
|
462
|
+
this.sawInput = true;
|
|
463
|
+
}
|
|
464
|
+
const outsideFrame = this.state === 'magic' || this.state === 'skippable-size' || this.state === 'skippable-body';
|
|
465
|
+
/** Start of the current run in this chunk, or -1 outside a Zstandard frame. */
|
|
466
|
+
let runStart = outsideFrame ? -1 : 0;
|
|
467
|
+
/** Magic-number bytes read before this chunk. */
|
|
468
|
+
let earlier = this.state === 'magic' ? this.fieldBytes : 0;
|
|
469
|
+
let index = 0;
|
|
470
|
+
while (index < length) {
|
|
471
|
+
switch (this.state) {
|
|
472
|
+
case 'magic': {
|
|
473
|
+
const byte = chunkArg[index];
|
|
474
|
+
if (this.fieldBytes === 0) {
|
|
475
|
+
this.skippable = (byte & 0xf0) === ZSTD_SKIPPABLE_MAGIC[0];
|
|
476
|
+
if (!this.skippable && byte !== ZSTD_FRAME_MAGIC[0]) {
|
|
477
|
+
throw zstdMalformed('bytes at a zstd frame start are not a zstd frame');
|
|
478
|
+
}
|
|
479
|
+
} else if (byte !== (this.skippable ? ZSTD_SKIPPABLE_MAGIC : ZSTD_FRAME_MAGIC)[this.fieldBytes]) {
|
|
480
|
+
throw zstdMalformed('bytes at a zstd frame start are not a zstd frame');
|
|
481
|
+
}
|
|
482
|
+
index += 1;
|
|
483
|
+
this.fieldBytes += 1;
|
|
484
|
+
if (this.fieldBytes < 4) {
|
|
485
|
+
break;
|
|
486
|
+
}
|
|
487
|
+
this.fieldBytes = 0;
|
|
488
|
+
if (this.skippable) {
|
|
489
|
+
this.fieldValue = 0;
|
|
490
|
+
this.state = 'skippable-size';
|
|
491
|
+
} else {
|
|
492
|
+
this.zstandardFrames += 1;
|
|
493
|
+
if (this.zstandardFrames > this.maxFrames) {
|
|
494
|
+
throw limitExceeded('maxEntries', this.maxFrames, 'zstd frames');
|
|
495
|
+
}
|
|
496
|
+
if (earlier > 0) {
|
|
497
|
+
runsArg.push(ZSTD_FRAME_MAGIC.slice(0, earlier));
|
|
498
|
+
runStart = 0;
|
|
499
|
+
} else {
|
|
500
|
+
runStart = index - 4;
|
|
501
|
+
}
|
|
502
|
+
this.state = 'frame-header';
|
|
503
|
+
}
|
|
504
|
+
earlier = 0;
|
|
505
|
+
break;
|
|
506
|
+
}
|
|
507
|
+
case 'skippable-size':
|
|
508
|
+
this.fieldValue += chunkArg[index] * LITTLE_ENDIAN_PLACE[this.fieldBytes];
|
|
509
|
+
index += 1;
|
|
510
|
+
this.fieldBytes += 1;
|
|
511
|
+
if (this.fieldBytes === 4) {
|
|
512
|
+
this.fieldBytes = 0;
|
|
513
|
+
this.remaining = this.fieldValue;
|
|
514
|
+
this.state = this.remaining === 0 ? 'magic' : 'skippable-body';
|
|
515
|
+
}
|
|
516
|
+
break;
|
|
517
|
+
case 'skippable-body': {
|
|
518
|
+
const take = Math.min(this.remaining, length - index);
|
|
519
|
+
index += take;
|
|
520
|
+
this.remaining -= take;
|
|
521
|
+
if (this.remaining === 0) {
|
|
522
|
+
this.state = 'magic';
|
|
523
|
+
}
|
|
524
|
+
break;
|
|
525
|
+
}
|
|
526
|
+
case 'frame-header': {
|
|
527
|
+
const flags = chunkArg[index];
|
|
528
|
+
index += 1;
|
|
529
|
+
if ((flags & 0x08) !== 0) {
|
|
530
|
+
throw zstdMalformed('a zstd frame header sets the reserved bit');
|
|
531
|
+
}
|
|
532
|
+
const contentSizeFlag = flags >> 6;
|
|
533
|
+
const singleSegment = (flags >> 5) & 1;
|
|
534
|
+
this.descriptor = flags;
|
|
535
|
+
this.remaining =
|
|
536
|
+
(singleSegment === 1 ? 0 : 1) +
|
|
537
|
+
ZSTD_DICTIONARY_ID_BYTES[flags & 3] +
|
|
538
|
+
(contentSizeFlag === 0 ? singleSegment : ZSTD_CONTENT_SIZE_BYTES[contentSizeFlag]);
|
|
539
|
+
this.fieldValue = 0;
|
|
540
|
+
this.state = this.remaining === 0 ? 'block-header' : 'header-fields';
|
|
541
|
+
break;
|
|
542
|
+
}
|
|
543
|
+
case 'header-fields': {
|
|
544
|
+
const take = Math.min(this.remaining, length - index);
|
|
545
|
+
index += take;
|
|
546
|
+
this.remaining -= take;
|
|
547
|
+
if (this.remaining === 0) {
|
|
548
|
+
this.state = 'block-header';
|
|
549
|
+
}
|
|
550
|
+
break;
|
|
551
|
+
}
|
|
552
|
+
case 'block-header': {
|
|
553
|
+
this.fieldValue += chunkArg[index] * LITTLE_ENDIAN_PLACE[this.fieldBytes];
|
|
554
|
+
index += 1;
|
|
555
|
+
this.fieldBytes += 1;
|
|
556
|
+
if (this.fieldBytes < 3) {
|
|
557
|
+
break;
|
|
558
|
+
}
|
|
559
|
+
const header = this.fieldValue;
|
|
560
|
+
this.fieldBytes = 0;
|
|
561
|
+
this.fieldValue = 0;
|
|
562
|
+
const blockType = (header >> 1) & 3;
|
|
563
|
+
if (blockType === ZSTD_BLOCK_TYPE_RESERVED) {
|
|
564
|
+
throw zstdMalformed('a zstd block has the reserved block type');
|
|
565
|
+
}
|
|
566
|
+
this.lastBlock = (header & 1) === 1;
|
|
567
|
+
this.remaining = blockType === ZSTD_BLOCK_TYPE_RLE ? 1 : header >>> 3;
|
|
568
|
+
if (this.remaining > 0) {
|
|
569
|
+
this.state = 'block-body';
|
|
570
|
+
} else if (this.endBlock()) {
|
|
571
|
+
runsArg.push(chunkArg.subarray(runStart, index));
|
|
572
|
+
runStart = -1;
|
|
573
|
+
}
|
|
574
|
+
break;
|
|
575
|
+
}
|
|
576
|
+
case 'block-body':
|
|
577
|
+
case 'checksum': {
|
|
578
|
+
const take = Math.min(this.remaining, length - index);
|
|
579
|
+
index += take;
|
|
580
|
+
this.remaining -= take;
|
|
581
|
+
if (this.remaining > 0) {
|
|
582
|
+
break;
|
|
583
|
+
}
|
|
584
|
+
if (this.state === 'checksum' || this.endBlock()) {
|
|
585
|
+
this.state = 'magic';
|
|
586
|
+
runsArg.push(chunkArg.subarray(runStart, index));
|
|
587
|
+
runStart = -1;
|
|
588
|
+
}
|
|
589
|
+
break;
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
if (runStart >= 0 && runStart < length) {
|
|
594
|
+
runsArg.push(chunkArg.subarray(runStart, length));
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
/** A block's content has been read: returns true when that ends the frame, else moves to the next field. */
|
|
599
|
+
private endBlock(): boolean {
|
|
600
|
+
if (!this.lastBlock) {
|
|
601
|
+
this.state = 'block-header';
|
|
602
|
+
return false;
|
|
603
|
+
}
|
|
604
|
+
if ((this.descriptor & 0x04) !== 0) {
|
|
605
|
+
this.remaining = 4;
|
|
606
|
+
this.state = 'checksum';
|
|
607
|
+
return false;
|
|
608
|
+
}
|
|
609
|
+
this.state = 'magic';
|
|
610
|
+
return true;
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/** The inner reader has ended: only a frame start after at least one Zstandard frame is a clean end. */
|
|
614
|
+
private endOfInput(): void {
|
|
615
|
+
switch (this.state) {
|
|
616
|
+
case 'magic':
|
|
617
|
+
if (this.fieldBytes > 0) {
|
|
618
|
+
throw zstdTruncated('a zstd frame magic number');
|
|
619
|
+
}
|
|
620
|
+
if (this.zstandardFrames > 0) {
|
|
621
|
+
return;
|
|
622
|
+
}
|
|
623
|
+
if (!this.sawInput) {
|
|
624
|
+
throw zstdTruncated('the zstd stream');
|
|
625
|
+
}
|
|
626
|
+
throw zstdMalformed('the zstd stream holds no Zstandard frame');
|
|
627
|
+
case 'skippable-size':
|
|
628
|
+
throw zstdTruncated('a zstd skippable frame header');
|
|
629
|
+
case 'skippable-body':
|
|
630
|
+
throw zstdTruncated('a zstd skippable frame');
|
|
631
|
+
case 'frame-header':
|
|
632
|
+
case 'header-fields':
|
|
633
|
+
throw zstdTruncated('a zstd frame header');
|
|
634
|
+
case 'block-header':
|
|
635
|
+
throw zstdTruncated('a zstd block header');
|
|
636
|
+
case 'block-body':
|
|
637
|
+
throw zstdTruncated('a zstd block');
|
|
638
|
+
case 'checksum':
|
|
639
|
+
throw zstdTruncated('a zstd frame checksum');
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
function zstdTruncated(whereArg: string): LinuxRepoError {
|
|
645
|
+
return new LinuxRepoError('truncated', `input ended inside ${whereArg}`, { field: 'zstd stream' });
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
function zstdMalformed(messageArg: string): LinuxRepoError {
|
|
649
|
+
return new LinuxRepoError('malformed-archive', messageArg, { field: 'zstd stream' });
|
|
238
650
|
}
|
|
239
651
|
|
|
240
652
|
function mapZlibError(errorArg: unknown, kindArg: 'gzip' | 'zstd'): LinuxRepoError {
|
|
@@ -2,7 +2,11 @@
|
|
|
2
2
|
* Whether an architecture can name an index path segment. Readers record any
|
|
3
3
|
* architecture a package states (Gitea accepts them); builders refuse an entry
|
|
4
4
|
* whose architecture fails this rule with `invalid-input`, and never skip it
|
|
5
|
-
* silently.
|
|
5
|
+
* silently. The exception is `readDebianPackage`: the record's file name
|
|
6
|
+
* (`<name>_<version>_<architecture>.deb`) embeds the architecture, so it
|
|
7
|
+
* refuses one that fails this rule or the pool file name rule with
|
|
8
|
+
* `invalid-field` (`Architecture`), and never returns a record
|
|
9
|
+
* `buildDebianIndex` refuses. Refused: the empty string, `.` and `..`, and any value containing
|
|
6
10
|
* `/`, whitespace or a control character.
|
|
7
11
|
*/
|
|
8
12
|
export function isIndexableArchitecture(architectureArg: string): boolean {
|
package/ts/shared/limits.ts
CHANGED
|
@@ -19,7 +19,7 @@ export interface ILinuxPackageLimits {
|
|
|
19
19
|
maxDecompressedMetadataBytes: number;
|
|
20
20
|
/** The decompressed Arch payload walked for the file list, in bytes. */
|
|
21
21
|
maxPayloadBytes: number;
|
|
22
|
-
/** Tar or ar entries per archive, and values per multi-valued metadata field. */
|
|
22
|
+
/** Tar or ar entries per archive, Zstandard frames per zstd stream, and values per multi-valued metadata field. */
|
|
23
23
|
maxEntries: number;
|
|
24
24
|
/** Total bytes of Arch file names. */
|
|
25
25
|
maxFileListBytes: number;
|