UNPKG

tar

Version:
381 lines • 13.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Parser = void 0; const bytes_js_1 = require("./bytes.js"); const header_js_1 = require("./header.js"); const pax_js_1 = require("./pax.js"); const options_js_1 = require("./options.js"); const read_entry_js_1 = require("./read-entry.js"); const warn_js_1 = require("./warn.js"); const BEGIN = 0; const HEADER = 1; const BODY = 2; const IGNORE = 3; const META = 4; const ENTRY_END = { type: 'entryEnd' }; const END = { type: 'end' }; const metaTypes = new Set([ 'ExtendedHeader', 'OldExtendedHeader', 'GlobalExtendedHeader', 'NextFileHasLongPath', 'OldGnuLongPath', 'NextFileHasLongLinkpath', ]); // The text up to the first NUL. const cstring = (buf) => { const nul = buf.indexOf(0); return (0, bytes_js_1.decodeUtf8)(buf, 0, nul === -1 ? buf.length : nul); }; /** * A tar parser that the caller pulls. Give it input with `push()`, * and call `next()` for each event until it returns `undefined`. * Then give it more input, or call `end()` and call `next()` again. * * It does not decompress. It calls no code of the caller but * `filter`, `onwarn`, and the function that makes the entry objects. * * With `strict`, `next()` throws at each problem. The parser stays * usable: the next call continues after the block with the problem. */ class Parser { maxMetaEntrySize; /** * The number of input bytes up to the end of the last complete * entry that is not a meta entry. */ position = 0; /** * The number of input bytes that are parsed: the header blocks and * the body blocks. At an `entry` event, it is the place of the body * of the entry in the input. */ offset = 0; #opt; #filter; #create; #queue = []; #chunk; #pos = 0; // a header block that is in more than one chunk #block; #blockLen = 0; #state = BEGIN; // the entry of the body that is read or skipped now #entry; #header; #meta; #metaLen = 0; // body bytes to come, without and with the padding #remain = 0; #blockRemain = 0; #ex; // the names of the GNU long name headers for the next entry #longPath; #longLinkpath; #gex; #ended = false; #sawValid; #entryEnd = false; // 0: reading, 1: the end is found, 2: the end event is returned #done = 0; /** * @param createEntry makes the object for each entry event. It is * required if `E` is not `EntryData`. */ constructor(opt = {}, createEntry) { opt = this.#opt = (0, options_js_1.ownOptions)(opt); this.maxMetaEntrySize = opt.maxMetaEntrySize || 1024 * 1024; this.#filter = opt.filter; const win32 = opt.platform === 'win32'; this.#create = createEntry ?? ((header, ex, gex) => new read_entry_js_1.EntryData(header, ex, gex, win32)); } /** * Report a problem to `onwarn`. Throws if `strict` is set, or if * `data.recoverable` is false. */ warn(code, message, data) { (0, warn_js_1.warn)(this.#opt, code, message, data); } /** * Add input. The parser keeps the chunk until `next()` returns * `undefined`, and each body event is a view into it. Input after * `end()`, or after the end of the archive, is dropped. */ push(chunk) { if (!this.#ended) this.#queue.push(chunk); } /** Tell the parser that the input is complete. */ end() { this.#ended = true; } /** * The number of bytes of the body of the current entry, with its * padding, that are after the input given so far. The caller can * skip them in its input, and call `skipBody()`. It is 0 if the * parser has input that it did not parse, and in a PAX or GNU * header, because the parser reads that body. */ get bodyRemain() { const chunk = this.#chunk; const state = this.#state; return ((state !== BODY && state !== IGNORE) || this.#queue.length || (chunk && this.#pos < chunk.length)) ? 0 : this.#blockRemain; } /** * Count the `bodyRemain` bytes as parsed. The next input is the * header after the body. */ skipBody() { this.#entryEnd = this.#state === BODY; this.offset += this.#blockRemain; this.#remain = this.#blockRemain = 0; this.#endBody(); } /** * The next event. `undefined` means that the parser needs more * input, or after the `end` event, that it is done. */ next() { if (this.#entryEnd) { this.#entryEnd = false; return ENTRY_END; } for (;;) { let chunk = this.#chunk; let pos = this.#pos; if (!chunk || pos === chunk.length) { chunk = this.#chunk = this.#queue.shift(); pos = this.#pos = 0; if (!chunk) { if (!this.#ended || this.#done === 2) return undefined; const ev = this.#finish(); if (ev) return ev; } continue; } const avail = chunk.length - pos; const state = this.#state; if (state <= HEADER) { let ev; if (this.#blockLen === 0 && avail >= 512) { this.#pos = pos + 512; ev = this.#consumeHeader(chunk, pos); } else { const block = (this.#block ??= new Uint8Array(512)); const n = Math.min(512 - this.#blockLen, avail); block.set(chunk.subarray(pos, pos + n), this.#blockLen); this.#pos = pos + n; this.#blockLen += n; if (this.#blockLen < 512) continue; this.#blockLen = 0; ev = this.#consumeHeader(block, 0); } if (ev) return ev; continue; } const blockRemain = this.#blockRemain; const n = avail < blockRemain ? avail : blockRemain; const remain = this.#remain; const len = n < remain ? n : remain; this.#pos = pos + n; this.offset += n; this.#blockRemain = blockRemain - n; this.#remain = remain - len; const last = n === blockRemain; if (state === META) { const meta = this.#meta; meta.set(chunk.subarray(pos, pos + len), this.#metaLen); this.#metaLen += len; if (last) { this.#endBody(); this.#applyMeta(meta); } continue; } if (last) this.#endBody(); if (state === IGNORE) continue; if (len === 0) { if (last) return ENTRY_END; continue; } this.#entryEnd = last; return { type: 'body', data: len === chunk.length ? chunk : chunk.subarray(pos, pos + len), }; } } #endBody() { const entry = this.#entry; if (entry) { this.position = this.offset; this.#entry = undefined; } if (!entry?.invalid) this.#sawValid = true; this.#state = HEADER; } // The input is complete, and all of it is read. #finish() { const state = this.#state; if (state > HEADER) { // The body of the last entry is not complete. const entry = this.#entry; // The bytes of the last block of the input, which is not // complete, and the bytes from the start of that block to the // end of the body. The chunks of the input do not change them. const held = (512 - (this.#blockRemain % 512)) % 512; const need = this.#blockRemain + held; this.#entry = undefined; this.#meta = undefined; this.#state = HEADER; if (!entry?.invalid) this.#sawValid = true; this.#entryEnd = state === BODY; this.warn('TAR_BAD_ARCHIVE', `Truncated input (needed ${need} more bytes, only ${held} available)`, { entry, header: this.#header }); return undefined; } if (this.#entryEnd) { this.#entryEnd = false; return ENTRY_END; } if (this.#done === 0) { this.#done = 1; if (state === BEGIN || this.#sawValid === false) { // Less than one block of data, or no valid entry. this.warn('TAR_BAD_ARCHIVE', 'Unrecognized archive format'); } } this.#done = 2; return END; } #consumeHeader(buf, off) { this.offset += 512; if (this.#sawValid === undefined) this.#sawValid = false; // A PAX name wins over a GNU long name. The header does not use // these values if it is a PAX or GNU header itself. let ex = this.#ex; const longPath = this.#longPath; const longLinkpath = this.#longLinkpath; if (longPath !== undefined || longLinkpath !== undefined) { ex = Object.assign(Object.create(null), ex); ex.path ??= longPath; ex.linkpath ??= longLinkpath; } let header; try { header = new header_js_1.Header(buf, off, ex, this.#gex); } catch (er) { this.warn('TAR_ENTRY_INVALID', er); return; } if (header.nullBlock) { // A null block is the end of the archive, as it is for bsdtar. // A second null block normally comes after it. All the data // after the first one is dropped, so an entry there is not // read. this.#ended = true; this.#chunk = undefined; this.#queue.length = 0; this.#blockLen = 0; // The input is not shorter than one block. An archive with no // valid entry still gets the warning of `#finish`. this.#state = HEADER; return; } if (!header.cksumValid) { this.warn('TAR_ENTRY_INVALID', 'checksum failure', { header }); return; } if (!header.path) { this.warn('TAR_ENTRY_INVALID', 'path is required', { header }); return; } const type = header.type; const link = type === 'Link' || type === 'SymbolicLink'; if (link && !header.linkpath) { this.warn('TAR_ENTRY_INVALID', 'linkpath required', { header }); return; } if (!link && header.linkpath && type !== 'ExtendedHeader' && type !== 'GlobalExtendedHeader') { this.warn('TAR_ENTRY_INVALID', 'linkpath forbidden', { header }); return; } const size = header.size ?? 0; this.#header = header; this.#remain = size; this.#blockRemain = 512 * Math.ceil(size / 512); // A meta entry or an ignored entry is valid tar also. if (!size) this.#sawValid = true; if (metaTypes.has(type)) { if (size > this.maxMetaEntrySize) { this.#state = IGNORE; } else if (size > 0) { this.#meta = new Uint8Array(size); this.#metaLen = 0; this.#state = META; } return; } this.#ex = this.#longPath = this.#longLinkpath = undefined; const entry = this.#create(header, ex, this.#gex); if (!entry.ignore && this.#filter && !this.#filter(entry.path, entry)) { entry.ignore = true; } if (size) { this.#entry = entry; this.#state = entry.ignore ? IGNORE : BODY; } else { this.#state = HEADER; this.position = this.offset; this.#entryEnd = !entry.ignore; } return { type: 'entry', entry }; } #applyMeta(meta) { switch (this.#header.type) { case 'ExtendedHeader': case 'OldExtendedHeader': this.#ex = pax_js_1.Pax.parseBytes(meta, this.#ex, false); break; case 'GlobalExtendedHeader': this.#gex = pax_js_1.Pax.parseBytes(meta, this.#gex, true); break; // The last GNU long name of each kind is the one to use. case 'NextFileHasLongPath': case 'OldGnuLongPath': this.#longPath = cstring(meta); break; // NextFileHasLongLinkpath default: this.#longLinkpath = cstring(meta); } } } exports.Parser = Parser; //# sourceMappingURL=parser.js.map