import { artifactExpansionBudgetBytes } from "../domain/artifactExpansionBudget.js"; import type { MqlProgramMetadata } from "../domain/formatMetadata/mqlProgramMetadata.js"; import { MQL_PROGRAM_HEADER_BYTES, MQL_PROGRAM_RESOURCES_LIMIT, mqlProgramResourceTableRange, parseMqlProgramHeader, parseMqlProgramLayout, type MqlProgramLayout, } from "../domain/mqlProgram.js"; import { ParseFailureError } from "../domain/parseFailure.js"; import type { StableRegularFileDescriptor } from "../filesystem/RegularFile.js"; import { artifactFixedLimitFailure, artifactParseFailure, artifactReaderLimitFailure, malformedArtifact, type ArtifactReaderFailure, } from "../resources/index.js"; import { createExactArtifactRead } from "./ArtifactFileRange.js"; import { PositionalArtifactReader, type PositionalArtifactEntries, type PositionalArtifactEntry, } from "./PositionalArtifactReader.js"; /** Stored bytes of the body or a resource, whose encoding REA does not decode. */ const OPAQUE_ENTRY = "Stored bytes as found in the file. REA does not decode, decompress or decrypt them, and their encoding is unknown."; /** * MetaTrader 4 (`.ex4`) or MetaTrader 5 (`.ex5`) compiled program: header * facts, `#property` slots, and the opaque body and resource payloads as * stored byte ranges. Nothing is decrypted, decompressed or executed. */ export class MqlProgramArtifactReader extends PositionalArtifactReader { override readonly format = "mql-program" as const; readonly #expandedBytes: number | undefined; /** `expandedBytes` is `artifact_limits.expanded_bytes`. */ constructor( path: string, admitted?: StableRegularFileDescriptor, expandedBytes?: number, ) { super(path, "MQL program", admitted); this.#expandedBytes = expandedBytes; } formatMetadata(): MqlProgramMetadata | undefined { const metadata = this.preparedFormatMetadata; return metadata === undefined ? undefined : structuredClone(metadata); } protected override entryEndedEarly( entry: PositionalArtifactEntry, ): ArtifactReaderFailure { return malformedArtifact( "mql-program", `MQL program region ended early: ${entry.path}`, ); } protected override async prepareEntries( signal?: AbortSignal, ): Promise> { const { handle, initial } = await this.verifyCarrier(signal); const fileSize = initial.size; const read = createExactArtifactRead(this.format, handle, fileSize, signal); let layout: MqlProgramLayout; try { const header = parseMqlProgramHeader( await read(0, Math.min(MQL_PROGRAM_HEADER_BYTES, fileSize)), fileSize, ); const range = mqlProgramResourceTableRange(header, fileSize); const table = range === undefined ? undefined : await read(range.offset, range.length); this.assertOpen(signal); layout = parseMqlProgramLayout(header, table, fileSize); } catch (cause: unknown) { if ( cause instanceof ParseFailureError && cause.failure.kind === "limit" && cause.failure.limit.name === MQL_PROGRAM_RESOURCES_LIMIT ) throw artifactFixedLimitFailure( { name: "The MQL program resource record allowance", kind: "parser", limit: cause.failure.limit.maximum, unit: "records", }, cause.message, "capacity", { cause }, ); throw artifactParseFailure(cause, signal); } // Resource records may share one payload range, but every entry is hashed // and can be extracted separately, so charge each occurrence. const expansionBudget = this.#expandedBytes ?? artifactExpansionBudgetBytes(fileSize); const declaredBytes = layout.regions.reduce( (total, region) => total + region.length, 0, ); if (declaredBytes > expansionBudget) throw artifactReaderLimitFailure( "artifact_limits.expanded_bytes", expansionBudget, "bytes", declaredBytes, "memory", ); return { entries: layout.regions.map((region): PositionalArtifactEntry => ({ path: region.path, kind: "file", declaredSize: region.length, compressedSize: null, executable: false, encrypted: false, byteOffset: region.offset, declaredIntegrity: null, unpacked: false, limitations: [OPAQUE_ENTRY], adapterKey: `mql-program:${region.path}`, formatMetadata: region.metadata, })), coverage: { status: layout.limitations.length === 0 ? "complete" : "unknown", limitations: [...layout.limitations], }, formatMetadata: layout.metadata, }; } }