codekingpro/portable-devtools
115k
1// parse a 512-byte header block to a data object, or vice-versa
2// encode returns `true` if a pax extended header is needed, because
3// the data could not be faithfully encoded in a simple header.
4// (Also, check header.needPax to see if it needs a pax header.)
5import { posix as pathModule } from 'node:path';
6import * as large from './large-numbers.js';
7import * as types from './types.js';
8export class Header {
9 cksumValid = false;
10 needPax = false;
11 nullBlock = false;
12 block;
13 path;
14 mode;
15 uid;
16 gid;
17 size;
18 cksum;
19 #type = 'Unsupported';
20 linkpath;
21 uname;
22 gname;
23 devmaj = 0;
24 devmin = 0;
25 atime;
26 ctime;
27 mtime;
28 charset;
29 comment;
30 constructor(data, off = 0, ex, gex) {
31 if (Buffer.isBuffer(data)) {
32 this.decode(data, off || 0, ex, gex);
33 }
34 else if (data) {
35 this.#slurp(data);
36 }
37 }
38 decode(buf, off, ex, gex) {
39 if (!off) {
40 off = 0;
41 }
42 if (!buf || !(buf.length >= off + 512)) {
43 throw new Error('need 512 bytes for header');
44 }
45 this.path = ex?.path ?? decString(buf, off, 100);
46 this.mode = ex?.mode ?? gex?.mode ?? decNumber(buf, off + 100, 8);
47 this.uid = ex?.uid ?? gex?.uid ?? decNumber(buf, off + 108, 8);
48 this.gid = ex?.gid ?? gex?.gid ?? decNumber(buf, off + 116, 8);
49 this.size = ex?.size ?? gex?.size ?? decNumber(buf, off + 124, 12);
50 this.mtime = ex?.mtime ?? gex?.mtime ?? decDate(buf, off + 136, 12);
51 this.cksum = decNumber(buf, off + 148, 12);
52 // if we have extended or global extended headers, apply them now
53 // See https://github.com/npm/node-tar/pull/187
54 // Apply global before local, so it overrides
55 if (gex)
56 this.#slurp(gex, true);
57 if (ex)
58 this.#slurp(ex);
59 // old tar versions marked dirs as a file with a trailing /
60 const t = decString(buf, off + 156, 1);
61 if (types.isCode(t)) {
62 this.#type = t || '0';
63 }
64 if (this.#type === '0' && this.path.slice(-1) === '/') {
65 this.#type = '5';
66 }
67 // tar implementations sometimes incorrectly put the stat(dir).size
68 // as the size in the tarball, even though Directory entries are
69 // not able to have any body at all. In the very rare chance that
70 // it actually DOES have a body, we weren't going to do anything with
71 // it anyway, and it'll just be a warning about an invalid header.
72 if (this.#type === '5') {
73 this.size = 0;
74 }
75 this.linkpath = decString(buf, off + 157, 100);
76 if (buf.subarray(off + 257, off + 265).toString() === 'ustar\u000000') {
77 /* c8 ignore start */
78 this.uname = ex?.uname ?? gex?.uname ?? decString(buf, off + 265, 32);
79 this.gname = ex?.gname ?? gex?.gname ?? decString(buf, off + 297, 32);
80 this.devmaj =
81 ex?.devmaj ?? gex?.devmaj ?? decNumber(buf, off + 329, 8) ?? 0;
82 this.devmin =
83 ex?.devmin ?? gex?.devmin ?? decNumber(buf, off + 337, 8) ?? 0;
84 /* c8 ignore stop */
85 if (buf[off + 475] !== 0) {
86 // definitely a prefix, definitely >130 chars.
87 const prefix = decString(buf, off + 345, 155);
88 this.path = prefix + '/' + this.path;
89 }
90 else {
91 const prefix = decString(buf, off + 345, 130);
92 if (prefix) {
93 this.path = prefix + '/' + this.path;
94 }
95 /* c8 ignore start */
96 this.atime = ex?.atime ?? gex?.atime ?? decDate(buf, off + 476, 12);
97 this.ctime = ex?.ctime ?? gex?.ctime ?? decDate(buf, off + 488, 12);
98 /* c8 ignore stop */
99 }
100 }
101 let sum = 8 * 0x20;
102 for (let i = off; i < off + 148; i++) {
103 sum += buf[i];
104 }
105 for (let i = off + 156; i < off + 512; i++) {
106 sum += buf[i];
107 }
108 this.cksumValid = sum === this.cksum;
109 if (this.cksum === undefined && sum === 8 * 0x20) {
110 this.nullBlock = true;
111 }
112 }
113 #slurp(ex, gex = false) {
114 Object.assign(this, Object.fromEntries(Object.entries(ex).filter(([k, v]) => {
115 // we slurp in everything except for the path attribute in
116 // a global extended header, because that's weird. Also, any
117 // null/undefined values are ignored.
118 return !(v === null ||
119 v === undefined ||
120 (k === 'path' && gex) ||
121 (k === 'linkpath' && gex) ||
122 k === 'global');
123 })));
124 }
125 encode(buf, off = 0) {
126 if (!buf) {
127 buf = this.block = Buffer.alloc(512);
128 }
129 if (this.#type === 'Unsupported') {
130 this.#type = '0';
131 }
132 if (!(buf.length >= off + 512)) {
133 throw new Error('need 512 bytes for header');
134 }
135 const prefixSize = this.ctime || this.atime ? 130 : 155;
136 const split = splitPrefix(this.path || '', prefixSize);
137 const path = split[0];
138 const prefix = split[1];
139 this.needPax = !!split[2];
140 this.needPax = encString(buf, off, 100, path) || this.needPax;
141 this.needPax = encNumber(buf, off + 100, 8, this.mode) || this.needPax;
142 this.needPax = encNumber(buf, off + 108, 8, this.uid) || this.needPax;
143 this.needPax = encNumber(buf, off + 116, 8, this.gid) || this.needPax;
144 this.needPax = encNumber(buf, off + 124, 12, this.size) || this.needPax;
145 this.needPax = encDate(buf, off + 136, 12, this.mtime) || this.needPax;
146 buf[off + 156] = Number(this.#type.codePointAt(0));
147 this.needPax =
148 encString(buf, off + 157, 100, this.linkpath) || this.needPax;
149 buf.write('ustar\u000000', off + 257, 8);
150 this.needPax =
151 encString(buf, off + 265, 32, this.uname) || this.needPax;
152 this.needPax =
153 encString(buf, off + 297, 32, this.gname) || this.needPax;
154 this.needPax =
155 encNumber(buf, off + 329, 8, this.devmaj) || this.needPax;
156 this.needPax =
157 encNumber(buf, off + 337, 8, this.devmin) || this.needPax;
158 this.needPax =
159 encString(buf, off + 345, prefixSize, prefix) || this.needPax;
160 if (buf[off + 475] !== 0) {
161 this.needPax = encString(buf, off + 345, 155, prefix) || this.needPax;
162 }
163 else {
164 this.needPax = encString(buf, off + 345, 130, prefix) || this.needPax;
165 this.needPax =
166 encDate(buf, off + 476, 12, this.atime) || this.needPax;
167 this.needPax =
168 encDate(buf, off + 488, 12, this.ctime) || this.needPax;
169 }
170 let sum = 8 * 0x20;
171 for (let i = off; i < off + 148; i++) {
172 sum += buf[i];
173 }
174 for (let i = off + 156; i < off + 512; i++) {
175 sum += buf[i];
176 }
177 this.cksum = sum;
178 encNumber(buf, off + 148, 8, this.cksum);
179 this.cksumValid = true;
180 return this.needPax;
181 }
182 get type() {
183 return (this.#type === 'Unsupported' ?
184 this.#type
185 : types.name.get(this.#type));
186 }
187 get typeKey() {
188 return this.#type;
189 }
190 set type(type) {
191 const c = String(types.code.get(type));
192 if (types.isCode(c) || c === 'Unsupported') {
193 this.#type = c;
194 }
195 else if (types.isCode(type)) {
196 this.#type = type;
197 }
198 else {
199 throw new TypeError('invalid entry type: ' + type);
200 }
201 }
202}
203const splitPrefix = (p, prefixSize) => {
204 const pathSize = 100;
205 let pp = p;
206 let prefix = '';
207 let ret = undefined;
208 const root = pathModule.parse(p).root || '.';
209 if (Buffer.byteLength(pp) < pathSize) {
210 ret = [pp, prefix, false];
211 }
212 else {
213 // first set prefix to the dir, and path to the base
214 prefix = pathModule.dirname(pp);
215 pp = pathModule.basename(pp);
216 do {
217 if (Buffer.byteLength(pp) <= pathSize &&
218 Buffer.byteLength(prefix) <= prefixSize) {
219 // both fit!
220 ret = [pp, prefix, false];
221 }
222 else if (Buffer.byteLength(pp) > pathSize &&
223 Buffer.byteLength(prefix) <= prefixSize) {
224 // prefix fits in prefix, but path doesn't fit in path
225 ret = [pp.slice(0, pathSize - 1), prefix, true];
226 }
227 else {
228 // make path take a bit from prefix
229 pp = pathModule.join(pathModule.basename(prefix), pp);
230 prefix = pathModule.dirname(prefix);
231 }
232 } while (prefix !== root && ret === undefined);
233 // at this point, found no resolution, just truncate
234 if (!ret) {
235 ret = [p.slice(0, pathSize - 1), '', true];
236 }
237 }
238 return ret;
239};
240const decString = (buf, off, size) => buf
241 .subarray(off, off + size)
242 .toString('utf8')
243 .replace(/\0.*/, '');
244const decDate = (buf, off, size) => numToDate(decNumber(buf, off, size));
245const numToDate = (num) => num === undefined ? undefined : new Date(num * 1000);
246const decNumber = (buf, off, size) => Number(buf[off]) & 0x80 ?
247 large.parse(buf.subarray(off, off + size))
248 : decSmallNumber(buf, off, size);
249const nanUndef = (value) => (isNaN(value) ? undefined : value);
250const decSmallNumber = (buf, off, size) => nanUndef(parseInt(buf
251 .subarray(off, off + size)
252 .toString('utf8')
253 .replace(/\0.*$/, '')
254 .trim(), 8));
255// the maximum encodable as a null-terminated octal, by field size
256const MAXNUM = {
257 12: 0o77777777777,
258 8: 0o7777777,
259};
260const encNumber = (buf, off, size, num) => num === undefined ? false
261 : num > MAXNUM[size] || num < 0 ?
262 (large.encode(num, buf.subarray(off, off + size)), true)
263 : (encSmallNumber(buf, off, size, num), false);
264const encSmallNumber = (buf, off, size, num) => buf.write(octalString(num, size), off, size, 'ascii');
265const octalString = (num, size) => padOctal(Math.floor(num).toString(8), size);
266const padOctal = (str, size) => (str.length === size - 1 ?
267 str
268 : new Array(size - str.length - 1).join('0') + str + ' ') + '\0';
269const encDate = (buf, off, size, date) => date === undefined ? false : (encNumber(buf, off, size, date.getTime() / 1000));
270// enough to fill the longest string we've got
271const NULLS = new Array(156).join('\0');
272// pad with nulls, return true if it's longer or non-ascii
273const encString = (buf, off, size, str) => str === undefined ? false : ((buf.write(str + NULLS, off, size, 'utf8'),
274 str.length !== Buffer.byteLength(str) || str.length > size));
275//# sourceMappingURL=header.js.map