// Calculated columns (Access 2010 and later), patched into mdb-reader by build.mjs. // // Access 2010 stores the value of a calculated column with a header in front // of it: 16 bytes, then the length of the value (4 bytes, little-endian), then // the value as a column of the result type would store it, then 3 more bytes // (23 extra bytes in all). Long text keeps the wrapped value in its long-value // pages; a yes/no value is a byte (not the row's null bitmap); a decimal is // stored as a total length (2 bytes), the scale, a sign byte and up to 16 // bytes of the number, in eight-byte groups. Layout as documented by Jackcess (Apache-2.0, // CalculatedColumnUtil); this code is written here. import { ColumnTypes } from "./types.js"; import { readFieldValue } from "./data/index.js"; import { uncompressText } from "./unicodeCompression.js"; const DATA_LEN_OFFSET = 16; const DATA_OFFSET = DATA_LEN_OFFSET + 4; /** The value inside a calculated column's wrapper; data too short to have one is returned as it is. */ export function unwrapCalculated(buffer) { if (buffer.length < DATA_OFFSET) return buffer; const len = buffer.readUInt32LE(DATA_LEN_OFFSET); return buffer.slice(DATA_OFFSET, DATA_OFFSET + Math.min(len, buffer.length - DATA_OFFSET)); } /** A calculated column's value, from the bytes the row holds for it. */ export function readCalculatedValue(buffer, column, database) { switch (column.type) { case ColumnTypes.Boolean: { const data = unwrapCalculated(buffer); return data.length > 0 && data[0] !== 0; } case ColumnTypes.Memo: return readCalculatedMemo(buffer, database); case ColumnTypes.Numeric: return readCalculatedNumeric(unwrapCalculated(buffer)); default: { const data = unwrapCalculated(buffer); if (data.length === 0) return null; return readFieldValue(data, column, database); } } } function readCalculatedNumeric(data) { if (data.length < 4) return null; const total = data.readInt16LE(0); let len = (total > 0 ? total : data.length - 2) - 2; len = Math.min(Math.floor(len / 4) * 4, 16, Math.floor((data.length - 4) / 4) * 4); const scale = data[2]; const negative = data[3] !== 0; // Not as an ordinary decimal column: eight-byte groups, the most significant first, each // little-endian, after a four-byte little-endian word first when the length is not a multiple // of 8 (Jackcess's fixNumericByteOrder for calculated values). let n = 0n; let i = 0; if (len % 8) { n = BigInt(data.readUInt32LE(4)); i = 4; } for (; i + 8 <= len; i += 8) n = (n << 64n) | data.readBigUInt64LE(4 + i); let digits = n.toString().padStart(scale + 1, "0"); if (scale > 0) digits = `${digits.slice(0, -scale)}.${digits.slice(-scale)}`; return (negative && n !== 0n ? "-" : "") + digits; } // Long text: the long-value pages are read as for any long text, and their bytes unwrapped before decoding. function readCalculatedMemo(buffer, database) { const length = buffer.readUIntLE(0, 3); const type = buffer.readUInt8(3); let bytes; if (type === 0x80) { bytes = buffer.slice(12, 12 + length); } else if (type === 0x40) { bytes = database.findPageRow(buffer.readUInt32LE(4)).slice(0, length); } else if (type === 0x00) { let pageRow = buffer.readInt32LE(4); const parts = []; let got = 0; do { const row = database.findPageRow(pageRow); if (got + row.length - 4 > length || row.length === 0) break; parts.push(row.slice(4)); got += row.length - 4; pageRow = row.readInt32LE(0); } while (pageRow !== 0); bytes = Buffer.concat(parts).slice(0, length); } else { throw new Error(`Unknown memo type ${type}`); } // -jm7: pages that end before the length is reached are damage, not a shorter text. if (bytes.length < length) { throw new Error("Long text cut short"); } return uncompressText(unwrapCalculated(bytes), database.format); }