|
| 1 | +import { |
| 2 | + createAsarHeaderSignatureProbe, |
| 3 | + type FinalAsarHeaderProbeObservation, |
| 4 | +} from "./AsarHeader.js"; |
| 5 | +import { |
| 6 | + createXmlPlistSignatureProbe, |
| 7 | + MAX_XML_PLIST_PROBE_BYTES, |
| 8 | +} from "../domain/apple/appleArchiveFormat.js"; |
| 9 | + |
| 10 | +/** Initial bytes required by the other bounded format classifiers. */ |
| 11 | +export const ARTIFACT_CLASSIFICATION_PREFIX_BYTES = 8_192; |
| 12 | + |
| 13 | +/** Retained bytes and independently parsed ASAR signature facts. */ |
| 14 | +export interface ArtifactClassificationEvidence { |
| 15 | + readonly prefix: Buffer; |
| 16 | + readonly asarHeader: FinalAsarHeaderProbeObservation; |
| 17 | +} |
| 18 | + |
| 19 | +/** Share acquisition bounds and signature observations across files and streams. */ |
| 20 | +export const createArtifactClassificationProbe = () => { |
| 21 | + const xml = createXmlPlistSignatureProbe(); |
| 22 | + const asar = createAsarHeaderSignatureProbe(); |
| 23 | + const blocks: Buffer[] = []; |
| 24 | + let retainedBytes = 0; |
| 25 | + let observedBytes = 0; |
| 26 | + const retain = (bytes: Uint8Array): void => { |
| 27 | + let offset = 0; |
| 28 | + while (offset < bytes.length) { |
| 29 | + const blockOffset = retainedBytes % ARTIFACT_CLASSIFICATION_PREFIX_BYTES; |
| 30 | + if (blockOffset === 0) |
| 31 | + blocks.push(Buffer.allocUnsafe(ARTIFACT_CLASSIFICATION_PREFIX_BYTES)); |
| 32 | + const count = Math.min( |
| 33 | + ARTIFACT_CLASSIFICATION_PREFIX_BYTES - blockOffset, |
| 34 | + bytes.length - offset, |
| 35 | + ); |
| 36 | + blocks[blocks.length - 1]!.set( |
| 37 | + bytes.subarray(offset, offset + count), |
| 38 | + blockOffset, |
| 39 | + ); |
| 40 | + retainedBytes += count; |
| 41 | + offset += count; |
| 42 | + } |
| 43 | + }; |
| 44 | + return { |
| 45 | + get length(): number { |
| 46 | + return observedBytes; |
| 47 | + }, |
| 48 | + get limit(): number { |
| 49 | + if (observedBytes < ARTIFACT_CLASSIFICATION_PREFIX_BYTES) |
| 50 | + return ARTIFACT_CLASSIFICATION_PREFIX_BYTES; |
| 51 | + return Math.max( |
| 52 | + ARTIFACT_CLASSIFICATION_PREFIX_BYTES, |
| 53 | + xml.state === "pending" ? MAX_XML_PLIST_PROBE_BYTES : 0, |
| 54 | + asar.observation.state === "pending" ? asar.limit : 0, |
| 55 | + ); |
| 56 | + }, |
| 57 | + write(bytes: Uint8Array): void { |
| 58 | + asar.write(bytes); |
| 59 | + let offset = 0; |
| 60 | + while (offset < bytes.length) { |
| 61 | + const limit = |
| 62 | + retainedBytes < ARTIFACT_CLASSIFICATION_PREFIX_BYTES |
| 63 | + ? ARTIFACT_CLASSIFICATION_PREFIX_BYTES |
| 64 | + : xml.state === "pending" |
| 65 | + ? MAX_XML_PLIST_PROBE_BYTES |
| 66 | + : retainedBytes; |
| 67 | + if (retainedBytes >= limit) break; |
| 68 | + const selected = bytes.subarray( |
| 69 | + offset, |
| 70 | + offset + Math.min(limit - retainedBytes, bytes.length - offset), |
| 71 | + ); |
| 72 | + retain(selected); |
| 73 | + xml.write(selected); |
| 74 | + offset += selected.length; |
| 75 | + } |
| 76 | + observedBytes += bytes.length; |
| 77 | + }, |
| 78 | + finish(archiveBytes: number): ArtifactClassificationEvidence { |
| 79 | + const prefix = Buffer.allocUnsafe(retainedBytes); |
| 80 | + let copied = 0; |
| 81 | + for (const block of blocks) { |
| 82 | + const count = Math.min(block.length, retainedBytes - copied); |
| 83 | + prefix.set(block.subarray(0, count), copied); |
| 84 | + copied += count; |
| 85 | + } |
| 86 | + return { prefix, asarHeader: asar.finish(archiveBytes) }; |
| 87 | + }, |
| 88 | + }; |
| 89 | +}; |
0 commit comments