// Rescuing rows from a damaged database (-jm7), for this site's mdb-reader build. // // Table.salvageData() (patched in by build.mjs) reads what it can of a table // whose normal read fails: each data page and each row is tried on its own, // and whatever cannot be read is reported rather than guessed. These helpers // find the pages when a table's page map (usage map) or the catalog // (MSysObjects) is itself damaged, and check that a row's layout makes sense // before its values are read, so that zeroed or overwritten bytes are left out // rather than read as a row of empty or made-up values. import { PageType } from "./PageType.js"; import { findMapPages } from "./usage-map.js"; /** Per database: the pages the file's free-page map lists, or null when it cannot be read. */ const freeMaps = new WeakMap(); /** * The pages the file marks as free: the usage map in row 0 of page 1, which lists the pages not in use * (those past the end of the file among them). A dropped table's definition and data pages keep their bytes * until Compact, and are listed here, so the scans below skip them rather than bring a deleted table, or its * old rows, back. Null when the map cannot be read, or claims MSysObjects's own definition (page 2) is free. */ export function freePages(database) { if (!freeMaps.has(database)) { let set = null; try { set = new Set(findMapPages(database.findPageRow(1 << 8), database)); if (set.has(2)) { set = null; } } catch { set = null; } freeMaps.set(database, set); } return freeMaps.get(database); } /** Per database: every data page, by the table definition page that owns it (byte 4 of a data page). */ const owners = new WeakMap(); function pageCount(database, length) { return Math.floor(length / database.format.pageSize); } /** * The data pages that say they belong to a table, found by reading every page of the file: used when the * table's own page map cannot be read. Pages cut off at the end of the file are not data pages here, nor * pages the free-page map lists. */ export function findOwnedDataPages(database, length, tableDefinitionPage) { let map = owners.get(database); if (!map) { map = new Map(); const count = pageCount(database, length); const free = freePages(database); for (let page = 1; page < count; ++page) { if (free?.has(page)) { continue; } let buffer; try { buffer = database.getPage(page); } catch { continue; } if (buffer.length < database.format.pageSize || buffer[0] !== PageType.DataPage) { continue; } const owner = buffer.readUInt32LE(4); let list = map.get(owner); if (!list) { list = []; map.set(owner, list); } list.push(page); } owners.set(database, map); } return map.get(tableDefinitionPage) ?? []; } /** * The first page of every table definition in the file, found by reading every page: used when the catalog * cannot be read. A definition longer than one page goes on in pages it points to (byte 4), which are not * tables of their own. Pages the free-page map lists (a dropped table's) are left out. */ export function findTableDefinitionPages(database, length) { const count = pageCount(database, length); const all = []; const continued = new Set(); const free = freePages(database); for (let page = 1; page < count; ++page) { if (free?.has(page)) { continue; } let buffer; try { buffer = database.getPage(page); } catch { continue; } if (buffer.length < database.format.pageSize || buffer[0] !== PageType.TableDefinition) { continue; } all.push(page); const next = buffer.readUInt32LE(4); if (next) { continued.add(next); } } return all.filter((p) => !continued.has(p)); } /** * Whether a row's layout holds together: a column count from 1 to 255, a null mask and a * variable-length column table that fit in the row, and offsets that run forwards inside it. A row that * fails this is damaged (zeroed or overwritten) and is left out: null. Otherwise its column counts. */ export function rowLooksSound(pageBuffer, recordStart, recordEnd, format, tableVariableColumnCount) { const { columnCountSize, variableColumnCountSize } = format.dataPage.record; const recordLength = recordEnd - recordStart + 1; if (recordStart < 0 || recordEnd >= format.pageSize || recordLength < columnCountSize + 1) { return null; } const rowColumnCount = pageBuffer.readUIntLE(recordStart, columnCountSize); // A row counts the columns by number, and numbers of deleted columns stay used, so it may count more than // the table now has; Access allows 255. if (rowColumnCount < 1 || rowColumnCount > 255) { return null; } const bitmaskSize = Math.floor((rowColumnCount + 7) / 8); if (recordLength < columnCountSize + bitmaskSize) { return null; } if (tableVariableColumnCount === 0) { return { rowColumnCount, rowVariableColumnCount: 0 }; } const offsets = []; let rowVariableColumnCount; if (variableColumnCountSize === 1) { if (recordLength < columnCountSize + bitmaskSize + 1) { return null; } rowVariableColumnCount = pageBuffer.readUInt8(recordEnd - bitmaskSize); let jumpCount = Math.floor((recordLength - 1) / 256); const columnPointer = recordEnd - bitmaskSize - jumpCount - 1; if ((columnPointer - recordStart - rowVariableColumnCount) / 256 < jumpCount) { --jumpCount; } if (columnPointer - rowVariableColumnCount < recordStart) { return null; } let jumpsUsed = 0; for (let i = 0; i < rowVariableColumnCount + 1; ++i) { while (jumpsUsed < jumpCount && i === pageBuffer.readUInt8(recordEnd - bitmaskSize - jumpsUsed - 1)) { ++jumpsUsed; } offsets.push(pageBuffer.readUInt8(columnPointer - i) + jumpsUsed * 256); } } else { if (recordLength < columnCountSize + bitmaskSize + 2) { return null; } rowVariableColumnCount = pageBuffer.readUInt16LE(recordEnd - bitmaskSize - 1); if (recordEnd - bitmaskSize - 3 - rowVariableColumnCount * 2 < recordStart) { return null; } for (let i = 0; i < rowVariableColumnCount + 1; ++i) { offsets.push(pageBuffer.readUInt16LE(recordEnd - bitmaskSize - 3 - i * 2)); } } if (rowVariableColumnCount > rowColumnCount) { return null; } for (let i = 0; i < offsets.length; ++i) { if (offsets[i] < columnCountSize || offsets[i] > recordLength || (i > 0 && offsets[i] < offsets[i - 1])) { return null; } } return { rowColumnCount, rowVariableColumnCount }; } /** * Column sets of Access's own hidden tables, so a table found without the catalog is not shown as the person's. * Only sets no one's own table is likely to have: a table wrongly listed is better than one wrongly hidden. */ const SYSTEM_COLUMNS = [ ["Connect", "Database", "DateCreate", "DateUpdate", "Flags", "ForeignName", "Id", "Lv", "LvExtra", "LvModule", "LvProp", "Name", "Owner", "ParentId", "RmtInfoLong", "RmtInfoShort", "Type"], ["ACM", "FInheritable", "ObjectId", "SID"], ["Attribute", "Expression", "Flag", "LvExtra", "Name1", "Name2", "ObjectId", "Order"], ["ccolumn", "grbit", "icolumn", "szColumn", "szObject", "szReferencedColumn", "szReferencedObject", "szRelationship"], ["DateCreate", "DateUpdate", "Id", "Lv", "Name", "ParentId", "Type"], ["Attribute", "Expression", "Flag", "Name1", "Name2", "ObjectId", "Order"], ["ColumnName", "ComplexID", "ComplexTypeObjectID", "ConceptualTableID", "FlatTableID"], ["Id", "LValue", "ObjectGuid", "ObjectName", "Property", "Value"], ["Data", "Extension", "Id", "Name", "Type"], ["Filter", "Flags", "Id", "Name", "Position", "SelectedObjectID", "Type"], ["Flags", "GroupID", "Icon", "Id", "Name", "ObjectID", "Position"], ["Flags", "GroupCategoryID", "Id", "Name", "Object Type Group", "ObjectID", "Position"], ["Flags", "Form", "Module", "Name", "ReplicationVersion", "Type", "TypeInfo", "Version"], // An attachment field's hidden template table, and the one MSysResources keeps its files in (.accdb). ["FileData", "FileFlags", "FileName", "FileTimeStamp", "FileType", "FileURL"], ["_Data", "FileData", "FileFlags", "FileName", "FileTimeStamp", "FileType", "FileURL", "MSysResources_Data"], ["__A__"], ]; /** Whether a table's columns are those of one of Access's own hidden tables (MSysObjects, MSysACEs, ...). */ export function looksLikeSystemTable(columnNames) { const have = [...columnNames].sort().join("\u0000"); return SYSTEM_COLUMNS.some((set) => { const want = [...set].sort().join("\u0000"); return have === want || (set.length > 6 && set.every((c) => columnNames.includes(c))); }); }