| 1 | // Lector de .zip (un .vsix es un zip) sin dependencias: el directorio central |
| 2 | // se lee a mano y cada entrada se descomprime con DecompressionStream |
| 3 | // ('deflate-raw'), que ya traen los navegadores y Node. Solo se descomprime lo |
| 4 | // que se pide, con topes por entrada y en total: un zip puede declarar un |
| 5 | // tamaño y luego inflarse a gigas (zip bomb), así que se cuenta lo que sale de |
| 6 | // verdad, no lo que dice la cabecera. |
| 7 | |
| 8 | const u16 = (b, o) => b[o] | (b[o + 1] << 8); |
| 9 | // Errores con código (el panel los traduce) y, si la hay, la entrada del zip. |
| 10 | const fallo = (msg, code, entry) => Object.assign(new Error(msg), { code, ...(entry && { entry }) }); |
| 11 | const u32 = (b, o) => (b[o] | (b[o + 1] << 8) | (b[o + 2] << 16) | (b[o + 3] << 24)) >>> 0; |
| 12 | |
| 13 | let CRC_TABLA = null; |
| 14 | export function crc32(bytes) { |
| 15 | if (!CRC_TABLA) { |
| 16 | CRC_TABLA = new Uint32Array(256); |
| 17 | for (let n = 0; n < 256; n++) { |
| 18 | let c = n; |
| 19 | for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1; |
| 20 | CRC_TABLA[n] = c >>> 0; |
| 21 | } |
| 22 | } |
| 23 | let c = 0xffffffff; |
| 24 | for (let i = 0; i < bytes.length; i++) c = CRC_TABLA[(c ^ bytes[i]) & 0xff] ^ (c >>> 8); |
| 25 | return (c ^ 0xffffffff) >>> 0; |
| 26 | } |
| 27 | |
| 28 | // Índice de entradas a partir del directorio central. |
| 29 | export function listEntries(input) { |
| 30 | const b = input instanceof Uint8Array ? input : new Uint8Array(input); |
| 31 | // el registro final (EOCD) está en los últimos 22 bytes + comentario (≤ 64 KB) |
| 32 | let eocd = -1; |
| 33 | for (let o = b.length - 22; o >= Math.max(0, b.length - 22 - 0xffff); o--) { |
| 34 | if (u32(b, o) === 0x06054b50) { eocd = o; break; } |
| 35 | } |
| 36 | if (eocd < 0) throw Object.assign(new Error('no es un zip (falta el directorio central)'), { code: 'notvsix' }); |
| 37 | const total = u16(b, eocd + 10); |
| 38 | const cdOffset = u32(b, eocd + 16); |
| 39 | if (total === 0xffff || cdOffset === 0xffffffff) throw fallo('zip64 no soportado', 'corrupt'); |
| 40 | const entries = []; |
| 41 | const dec = new TextDecoder(); |
| 42 | let p = cdOffset; |
| 43 | for (let k = 0; k < total; k++) { |
| 44 | if (p + 46 > b.length || u32(b, p) !== 0x02014b50) throw fallo('directorio central corrupto', 'corrupt'); |
| 45 | const flags = u16(b, p + 8); |
| 46 | const method = u16(b, p + 10); |
| 47 | const crc = u32(b, p + 16); |
| 48 | const csize = u32(b, p + 20); |
| 49 | const usize = u32(b, p + 24); |
| 50 | const nlen = u16(b, p + 28), xlen = u16(b, p + 30), clen = u16(b, p + 32); |
| 51 | const local = u32(b, p + 42); |
| 52 | const name = dec.decode(b.subarray(p + 46, p + 46 + nlen)).replace(/\\/g, '/'); |
| 53 | entries.push({ name, flags, method, crc, csize, usize, local }); |
| 54 | p += 46 + nlen + xlen + clen; |
| 55 | } |
| 56 | return entries; |
| 57 | } |
| 58 | |
| 59 | async function inflateRaw(datos, tope) { |
| 60 | const flujo = new Blob([datos]).stream().pipeThrough(new DecompressionStream('deflate-raw')); |
| 61 | const lector = flujo.getReader(); |
| 62 | const trozos = []; |
| 63 | let n = 0; |
| 64 | for (;;) { |
| 65 | const { done, value } = await lector.read(); |
| 66 | if (done) break; |
| 67 | n += value.length; |
| 68 | if (n > tope) { lector.cancel().catch(() => {}); throw fallo('entrada demasiado grande al descomprimir', 'big'); } |
| 69 | trozos.push(value); |
| 70 | } |
| 71 | const out = new Uint8Array(n); |
| 72 | let o = 0; |
| 73 | for (const t of trozos) { out.set(t, o); o += t.length; } |
| 74 | return out; |
| 75 | } |
| 76 | |
| 77 | // Abre el zip y devuelve un lector perezoso: names, has(name), read(name). |
| 78 | // `maxEntry` limita cada entrada y `maxTotal` lo que se descomprime en total. |
| 79 | export function openZip(input, { maxEntry = 8 << 20, maxTotal = 48 << 20 } = {}) { |
| 80 | const b = input instanceof Uint8Array ? input : new Uint8Array(input); |
| 81 | const entries = listEntries(b); |
| 82 | const porNombre = new Map(entries.filter(e => !e.name.endsWith('/')).map(e => [e.name, e])); |
| 83 | let gastado = 0; |
| 84 | async function read(name) { |
| 85 | const e = porNombre.get(name); |
| 86 | if (!e) throw fallo('no está en el paquete: ' + name, 'missing', name); |
| 87 | if (e.flags & 1) throw fallo('entrada cifrada: ' + name, 'corrupt', name); |
| 88 | if (e.usize > maxEntry) throw fallo(`${name} pesa demasiado (${e.usize} bytes)`, 'big', name); |
| 89 | if (gastado + e.usize > maxTotal) throw fallo('el paquete descomprime demasiado', 'big', name); |
| 90 | if (e.local + 30 > b.length || u32(b, e.local) !== 0x04034b50) throw fallo('cabecera local corrupta: ' + name, 'corrupt', name); |
| 91 | const inicio = e.local + 30 + u16(b, e.local + 26) + u16(b, e.local + 28); |
| 92 | const datos = b.subarray(inicio, inicio + e.csize); |
| 93 | if (datos.length !== e.csize) throw fallo('entrada truncada: ' + name, 'corrupt', name); |
| 94 | let out; |
| 95 | if (e.method === 0) out = datos.slice(); |
| 96 | else if (e.method === 8) { |
| 97 | try { out = await inflateRaw(datos, Math.min(maxEntry, e.usize)); } |
| 98 | catch (err) { throw err?.code === 'big' ? err : fallo('no se pudo descomprimir: ' + name, 'corrupt', name); } // el resto: datos que no son deflate |
| 99 | } |
| 100 | else throw fallo(`compresión ${e.method} no soportada: ${name}`, 'corrupt', name); |
| 101 | if (out.length !== e.usize || crc32(out) !== e.crc) throw fallo('CRC incorrecto: ' + name, 'corrupt', name); |
| 102 | gastado += out.length; |
| 103 | return out; |
| 104 | } |
| 105 | return { names: [...porNombre.keys()], has: n => porNombre.has(n), read, entries }; |
| 106 | } |