106 lines
3.0 KiB
TypeScript
106 lines
3.0 KiB
TypeScript
import { deflateSync } from 'node:zlib'
|
|
|
|
/**
|
|
* Byte builders of the R4 reader oracles, copied from scan.test.ts and
|
|
* inflate.test.ts: a test file cannot be imported without registering its
|
|
* cases, and those files stay untouched. Same output for the same arguments.
|
|
*/
|
|
|
|
export const KiB = 1024
|
|
export const MiB = 1024 * KiB
|
|
|
|
export const latin1 = (bytes: Uint8Array) =>
|
|
Buffer.from(bytes).toString('latin1')
|
|
/** One byte per character: every string given here stays at or below U+00FF. */
|
|
export const ascii = (text: string) =>
|
|
new Uint8Array(Buffer.from(text, 'latin1'))
|
|
|
|
export function concat(parts: Uint8Array[]): Uint8Array {
|
|
const out = new Uint8Array(parts.reduce((n, p) => n + p.byteLength, 0))
|
|
let at = 0
|
|
for (const part of parts) {
|
|
out.set(part, at)
|
|
at += part.byteLength
|
|
}
|
|
return out
|
|
}
|
|
|
|
export interface StreamSpec {
|
|
num: number
|
|
dict: string
|
|
data: Uint8Array
|
|
eol?: string
|
|
}
|
|
|
|
export function streamObject({
|
|
num,
|
|
dict,
|
|
data,
|
|
eol = '\n',
|
|
}: StreamSpec): Uint8Array {
|
|
return concat([
|
|
ascii(`${num} 0 obj\n<<${dict}>>\nstream${eol}`),
|
|
data,
|
|
ascii('\nendstream\nendobj\n'),
|
|
])
|
|
}
|
|
|
|
/** The scanner never reads the xref, so none is written. */
|
|
export function craftPdf(objects: Uint8Array[]): Uint8Array {
|
|
return concat([
|
|
ascii('%PDF-1.7\n'),
|
|
...objects,
|
|
ascii('trailer\n<< /Root 1 0 R >>\n%%EOF\n'),
|
|
])
|
|
}
|
|
|
|
/** `craftPdf` over Latin-1 texts and byte parts. */
|
|
export function pdfOf(...objects: Array<string | Uint8Array>): Uint8Array {
|
|
return craftPdf(objects.map((o) => (typeof o === 'string' ? ascii(o) : o)))
|
|
}
|
|
|
|
export interface Span {
|
|
start: number
|
|
end: number
|
|
}
|
|
|
|
/** Bounds of the `n 0 obj … endobj` object whose dictionary holds `marker` (scan.test.ts `objectHolding`). */
|
|
export function objectSpan(pdf: Uint8Array, marker: string): Span {
|
|
const text = latin1(pdf)
|
|
const at = text.indexOf(marker)
|
|
if (at <= 0) throw new Error(`marker not found: ${marker}`)
|
|
const start = text.lastIndexOf(' 0 obj', at)
|
|
const headerStart = text.lastIndexOf('\n', start) + 1
|
|
const end = text.indexOf('endobj', at) + 'endobj'.length
|
|
return { start: headerStart, end }
|
|
}
|
|
|
|
export function objectHolding(pdf: Uint8Array, marker: string): Uint8Array {
|
|
const { start, end } = objectSpan(pdf, marker)
|
|
return pdf.slice(start, end)
|
|
}
|
|
|
|
export const filled = (byte: string, length: number) =>
|
|
ascii(byte.repeat(length))
|
|
|
|
export const deflate = (data: Uint8Array) => new Uint8Array(deflateSync(data))
|
|
|
|
/** Incompressible bytes: the compressed stream is about as long as the input. */
|
|
export function noise(length: number): Uint8Array {
|
|
let seed = 0x9e3779b9
|
|
return Uint8Array.from({ length }, () => {
|
|
seed = (seed * 1664525 + 1013904223) >>> 0
|
|
return seed >>> 24
|
|
})
|
|
}
|
|
|
|
/** CompressionStream('deflate') output, as security.test.ts builds its inputs. */
|
|
export async function compressionStreamDeflate(
|
|
data: Uint8Array,
|
|
): Promise<Uint8Array> {
|
|
const stream = new Blob([data])
|
|
.stream()
|
|
.pipeThrough(new CompressionStream('deflate'))
|
|
return new Uint8Array(await new Response(stream).arrayBuffer())
|
|
}
|