fix(apple): traverse keyed archive hierarchy collections (#976)
This commit is contained in:
@@ -202,53 +202,66 @@ export const buildInterfaceBuilderAnalysis = (input: {
|
||||
};
|
||||
let hierarchyCount = 0;
|
||||
let hierarchyTruncated = false;
|
||||
const visitHierarchy = (value: unknown, parentId: string): void => {
|
||||
if (hierarchyCount >= input.limits.max_objects) {
|
||||
truncated = true;
|
||||
hierarchyTruncated = true;
|
||||
return;
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
for (const child of value) visitHierarchy(child, parentId);
|
||||
return;
|
||||
}
|
||||
if (typeof value !== "object" || value === null) return;
|
||||
const item = record(value);
|
||||
const objectId = firstString(
|
||||
item.objectID,
|
||||
item["object-id"],
|
||||
item.id,
|
||||
typeof item.archiveUID === "number" ? String(item.archiveUID) : null,
|
||||
);
|
||||
let nextParent = parentId;
|
||||
if (objectId !== null) {
|
||||
const known = objectIds.has(objectId);
|
||||
const childId = known
|
||||
? objectNodeId(objectId)
|
||||
: `${prefix}unknown:${objectId}`;
|
||||
if (
|
||||
!known &&
|
||||
!addNode({
|
||||
id: childId,
|
||||
kind: "unknown",
|
||||
name: firstString(item.label, item.name) ?? objectId,
|
||||
location: null,
|
||||
attributes: { interface_builder_object_id: objectId },
|
||||
evidence: evidenceFor(`hierarchy item ${objectId}`),
|
||||
})
|
||||
)
|
||||
const visitHierarchy = (
|
||||
initial: unknown,
|
||||
initialParentId: string,
|
||||
): void => {
|
||||
const pending: { readonly value: unknown; readonly parentId: string }[] =
|
||||
[{ value: initial, parentId: initialParentId }];
|
||||
while (pending.length > 0) {
|
||||
if (hierarchyCount >= input.limits.max_objects) {
|
||||
truncated = true;
|
||||
hierarchyTruncated = true;
|
||||
connect(
|
||||
`${prefix}hierarchy:${parentId}:${childId}`,
|
||||
parentId,
|
||||
childId,
|
||||
"contains",
|
||||
`hierarchy ${objectId}`,
|
||||
return;
|
||||
}
|
||||
const entry = pending.pop();
|
||||
if (entry === undefined) break;
|
||||
const { value, parentId } = entry;
|
||||
if (Array.isArray(value)) {
|
||||
for (let index = value.length - 1; index >= 0; index -= 1) {
|
||||
const child = value[index];
|
||||
if (child !== undefined) pending.push({ value: child, parentId });
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (typeof value !== "object" || value === null) continue;
|
||||
const item = record(value);
|
||||
const objectId = firstString(
|
||||
item.objectID,
|
||||
item["object-id"],
|
||||
item.id,
|
||||
typeof item.archiveUID === "number" ? String(item.archiveUID) : null,
|
||||
);
|
||||
nextParent = childId;
|
||||
hierarchyCount += 1;
|
||||
let nextParent = parentId;
|
||||
if (objectId !== null) {
|
||||
const known = objectIds.has(objectId);
|
||||
const childId = known
|
||||
? objectNodeId(objectId)
|
||||
: `${prefix}unknown:${objectId}`;
|
||||
if (
|
||||
!known &&
|
||||
!addNode({
|
||||
id: childId,
|
||||
kind: "unknown",
|
||||
name: firstString(item.label, item.name) ?? objectId,
|
||||
location: null,
|
||||
attributes: { interface_builder_object_id: objectId },
|
||||
evidence: evidenceFor(`hierarchy item ${objectId}`),
|
||||
})
|
||||
)
|
||||
hierarchyTruncated = true;
|
||||
connect(
|
||||
`${prefix}hierarchy:${parentId}:${childId}`,
|
||||
parentId,
|
||||
childId,
|
||||
"contains",
|
||||
`hierarchy ${objectId}`,
|
||||
);
|
||||
nextParent = childId;
|
||||
hierarchyCount += 1;
|
||||
}
|
||||
pending.push({ value: item.children, parentId: nextParent });
|
||||
}
|
||||
visitHierarchy(item.children, nextParent);
|
||||
};
|
||||
for (const hierarchy of parsed.hierarchy) visitHierarchy(hierarchy, rootId);
|
||||
for (const [index, item] of parsed.connections
|
||||
|
||||
@@ -196,39 +196,85 @@ const keyedArchiveTable = (objectTable: readonly unknown[]) => {
|
||||
};
|
||||
const hierarchy = (start: unknown): unknown[] => {
|
||||
const visited = new Set<number>();
|
||||
const node = (
|
||||
const root: unknown[] = [];
|
||||
const pending: { readonly value: unknown; readonly output: unknown[] }[] = [
|
||||
{ value: start, output: root },
|
||||
];
|
||||
const appendNode = (
|
||||
item: Record<string, unknown>,
|
||||
uid: number | undefined,
|
||||
depth: number,
|
||||
): unknown[] => {
|
||||
output: unknown[],
|
||||
): void => {
|
||||
const objectId = firstString(
|
||||
authoredId(item),
|
||||
text(item.id),
|
||||
uid === undefined ? null : String(uid),
|
||||
);
|
||||
const children = Object.entries(item)
|
||||
.filter(([key]) => HIERARCHY_KEY.test(key))
|
||||
.flatMap(([, child]) => visit(child, depth + 1));
|
||||
return objectId === null ? children : [{ objectID: objectId, children }];
|
||||
if (objectId === null) {
|
||||
for (const [key, child] of Object.entries(item).reverse())
|
||||
if (HIERARCHY_KEY.test(key)) pending.push({ value: child, output });
|
||||
return;
|
||||
}
|
||||
const children: unknown[] = [];
|
||||
output.push({ objectID: objectId, children });
|
||||
for (const [key, child] of Object.entries(item).reverse())
|
||||
if (HIERARCHY_KEY.test(key))
|
||||
pending.push({ value: child, output: children });
|
||||
};
|
||||
const visit = (value: unknown, depth: number): unknown[] => {
|
||||
if (depth > 32) return [];
|
||||
while (pending.length > 0) {
|
||||
const entry = pending.pop();
|
||||
if (entry === undefined) break;
|
||||
const { value, output } = entry;
|
||||
const uid = archiveUid(value);
|
||||
if (uid !== undefined) {
|
||||
if (visited.has(uid)) return [];
|
||||
if (visited.has(uid)) continue;
|
||||
visited.add(uid);
|
||||
const target = objectTable[uid];
|
||||
if (Array.isArray(target)) return visit(target, depth + 1);
|
||||
return typeof target === "object" && target !== null
|
||||
? node(record(target), uid, depth)
|
||||
: [];
|
||||
if (Array.isArray(target)) {
|
||||
for (let index = target.length - 1; index >= 0; index -= 1) {
|
||||
const child = target[index];
|
||||
if (child !== undefined) pending.push({ value: child, output });
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (typeof target !== "object" || target === null) continue;
|
||||
const item = record(target);
|
||||
if (archivedArrayClass(item.$class)) {
|
||||
const members = item["NS.objects"];
|
||||
if (Array.isArray(members))
|
||||
for (let index = members.length - 1; index >= 0; index -= 1) {
|
||||
const child = members[index];
|
||||
if (child !== undefined) pending.push({ value: child, output });
|
||||
}
|
||||
continue;
|
||||
}
|
||||
appendNode(item, uid, output);
|
||||
continue;
|
||||
}
|
||||
if (Array.isArray(value))
|
||||
return value.flatMap((item) => visit(item, depth + 1));
|
||||
if (typeof value !== "object" || value === null) return [];
|
||||
return node(record(value), undefined, depth);
|
||||
};
|
||||
return visit(start, 0);
|
||||
if (Array.isArray(value)) {
|
||||
for (let index = value.length - 1; index >= 0; index -= 1) {
|
||||
const child = value[index];
|
||||
if (child !== undefined) pending.push({ value: child, output });
|
||||
}
|
||||
} else if (typeof value === "object" && value !== null) {
|
||||
appendNode(record(value), undefined, output);
|
||||
}
|
||||
}
|
||||
return root;
|
||||
};
|
||||
const archivedArrayClass = (classReference: unknown): boolean => {
|
||||
const uid = archiveUid(classReference);
|
||||
const descriptor = record(
|
||||
uid === undefined ? classReference : objectTable[uid],
|
||||
);
|
||||
const classNames = Array.isArray(descriptor.$classes)
|
||||
? descriptor.$classes.filter(
|
||||
(name): name is string => typeof name === "string",
|
||||
)
|
||||
: [];
|
||||
return (
|
||||
classNames.includes("NSArray") || classNames.includes("NSMutableArray")
|
||||
);
|
||||
};
|
||||
return { resolve, hierarchy };
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user