UNPKG

@hazae41/kdbx

Version:

Rust-like KeePass (KDBX 4) file format for TypeScript

61 lines (60 loc) 2.01 kB
// deno-lint-ignore-file no-namespace import { TLV } from "../../../libs/tlv/mod.js"; import { Readable, Unknown, Writable } from "@hazae41/binary"; import { Cursor } from "@hazae41/cursor"; export class Vector { bytes; value; constructor(bytes, value) { this.bytes = bytes; this.value = value; } sizeOrThrow() { return this.bytes.sizeOrThrow(); } writeOrThrow(cursor) { this.bytes.writeOrThrow(cursor); } cloneOrThrow() { return Readable.readFromBytesOrThrow(Vector, Writable.writeToBytesOrThrow(this)); } } (function (Vector) { function initOrThrow(indexed) { const entries = new Array(); for (const key of Object.keys(indexed)) { const index = Number(key); const array = indexed[index]; if (array == null) continue; for (const value of array) entries.push(new TLV(index, value)); continue; } const sized = entries.reduce((x, r) => x + r.sizeOrThrow(), 0) + TLV.Empty.sizeOrThrow(); const bytes = new Unknown(new Uint8Array(sized)); const cursor = new Cursor(bytes.bytes); for (const entry of entries) entry.writeOrThrow(cursor); TLV.Empty.writeOrThrow(cursor); return new Vector(bytes, indexed); } Vector.initOrThrow = initOrThrow; function readOrThrow(cursor) { const start = cursor.offset; const entries = new Array(); const indexed = {}; while (true) { const tlv = TLV.readOrThrow(cursor); if (tlv.type === 0) break; entries.push(tlv); indexed[tlv.type] ??= []; indexed[tlv.type].push(tlv.value); continue; } const bytes = new Unknown(cursor.bytes.subarray(start, cursor.offset)); return new Vector(bytes, indexed); } Vector.readOrThrow = readOrThrow; })(Vector || (Vector = {}));