| 650 | // Parses records of the form "<length> <key>=<value>\n", where the length counts |
| 651 | // the bytes of the whole record |
| 652 | const parsePax = (data: Uint8Array): Map<string, string> => { |
| 653 | const records = new Map<string, string>() |
| 654 | let offset = 0 |
| 655 | while (offset < data.length) { |
| 656 | let space = offset |
| 657 | while (space < data.length && space - offset < 16 && data[space] !== 0x20) space++ |
| 658 | const lengthField = String.fromCharCode.apply(null, data.subarray(offset, space) as unknown as Array<number>) |
| 659 | const length = /^\d+$/.test(lengthField) ? Number(lengthField) : Number.NaN |
| 660 | const end = offset + length |
| 661 | if (!(length >= 5) || end > data.length || data[space] !== 0x20 || space + 1 >= end || data[end - 1] !== 0x0a) { |
| 662 | throw invalidHeader("malformed PAX record") |
| 663 | } |
| 664 | const record = data.subarray(space + 1, end - 1) |
| 665 | const equals = record.indexOf(0x3d) |
| 666 | if (equals <= 0) { |
| 667 | throw invalidHeader("malformed PAX record") |
| 668 | } |
| 669 | const key = textDecoder.decode(record.subarray(0, equals)) |
| 670 | const value = textDecoder.decode(record.subarray(equals + 1)) |
| 671 | if (key.includes("\0") || (paxStringKeys.has(key) && value.includes("\0"))) { |
| 672 | throw invalidHeader(`invalid PAX record ${key}`) |
| 673 | } |
| 674 | records.set(key, value) |
| 675 | offset = end |
| 676 | } |
| 677 | return records |
| 678 | } |
| 679 | |
| 680 | const paxInteger = (key: string, value: string): number => { |
| 681 | const n = /^\d+$/.test(value) ? Number(value) : Number.NaN |