(bytes: Uint8Array)
| 1451 | } |
| 1452 | |
| 1453 | private parseLocal(bytes: Uint8Array): Current { |
| 1454 | const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.length) |
| 1455 | const flags = view.getUint16(6, true) |
| 1456 | const method = view.getUint16(8, true) |
| 1457 | const crc = view.getUint32(14, true) |
| 1458 | let compressedSize = view.getUint32(18, true) |
| 1459 | let size = view.getUint32(22, true) |
| 1460 | const time = view.getUint16(10, true) |
| 1461 | const date = view.getUint16(12, true) |
| 1462 | const nameLength = view.getUint16(26, true) |
| 1463 | const rawName = bytes.subarray(30, 30 + nameLength) |
| 1464 | const utf8 = (flags & FLAG_UTF8) !== 0 |
| 1465 | let path = decodeName(rawName, utf8) |
| 1466 | // As in Info-ZIP and 7-Zip, only the first Unicode path field is read, and |
| 1467 | // only without the UTF-8 flag |
| 1468 | let readUnicodePath = !utf8 |
| 1469 | let seconds: number | undefined |
| 1470 | let zip64 = false |
| 1471 | let at = 30 + nameLength |
| 1472 | while (at + 4 <= bytes.length) { |
| 1473 | const id = view.getUint16(at, true) |
| 1474 | const start = at + 4 |
| 1475 | const end = start + view.getUint16(at + 2, true) |
| 1476 | if (end > bytes.length) throw invalidHeader("extra field overflows the header") |
| 1477 | if (id === EXTRA_ZIP64) { |
| 1478 | zip64 = true |
| 1479 | let field = start |
| 1480 | if (size === MAX32 && field + 8 <= end) { |
| 1481 | size = getUint64(view, field) |
| 1482 | field += 8 |
| 1483 | } |
| 1484 | if (compressedSize === MAX32 && field + 8 <= end) { |
| 1485 | compressedSize = getUint64(view, field) |
| 1486 | } |
| 1487 | } else if (id === EXTRA_TIMESTAMP && end - start >= 5 && (bytes[start] & 1) !== 0) { |
| 1488 | // Info-ZIP writes times before 1970 as negative numbers, and others |
| 1489 | // write times after 2038 as unsigned ones; the DOS date tells them apart |
| 1490 | const value = view.getUint32(start + 1, true) |
| 1491 | seconds = value >= 0x80000000 && (date >> 9) + 1980 < 2038 ? value - 0x100000000 : value |
| 1492 | } else if (id === EXTRA_UNICODE_PATH && readUnicodePath) { |
| 1493 | readUnicodePath = false |
| 1494 | if (end - start >= 5 && bytes[start] === 1 && view.getUint32(start + 1, true) === crc32(0, rawName)) { |
| 1495 | path = textDecoder.decode(bytes.subarray(start + 5, end)) |
| 1496 | } |
| 1497 | } |
| 1498 | at = end |
| 1499 | } |
| 1500 | const quoted = JSON.stringify(path) |
| 1501 | // Other tools end the name at a NUL character, or reject it |
| 1502 | if (path.includes("\0")) throw invalidHeader(`path ${quoted} contains a NUL character`) |
| 1503 | if ((flags & (FLAG_ENCRYPTED | FLAG_STRONG_ENCRYPTION)) !== 0) throw unsupported(`entry ${quoted} is encrypted`) |
| 1504 | if (method !== 0 && method !== 8) throw unsupported(`entry ${quoted} uses compression method ${method}`) |
| 1505 | const descriptor = (flags & FLAG_DESCRIPTOR) !== 0 |
| 1506 | // Writers that put sizes after the data usually leave zeros here |
| 1507 | const known = !descriptor || compressedSize !== 0 || size !== 0 |
| 1508 | // Searching for the descriptor of stored data would let its content forge |
| 1509 | // the entries after it |
| 1510 | if (method === 0 && !known) throw unsupported(`stored entry ${quoted} has no size before its data`) |
no test coverage detected