All files / binary101/analyzers/lnk index.ts

89.8% Statements 185/206
46.34% Branches 19/41
100% Functions 2/2
89.8% Lines 185/206

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 2071x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x       4x 4x     4x 4x     4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x                     4x 4x 4x     4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 12x 4x 20x 20x 4x 4x 4x 4x 20x 20x 4x 4x 4x 4x 4x 4x 4x 4x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 20x 20x 20x 20x 20x 4x 4x 4x 4x 4x 4x 4x 1x 1x 79x 55x 6x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x   4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x   4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x  
"use strict";
 
import { parseExtraData } from "./extra-data.js";
import { parseLinkInfo } from "./link-info.js";
import type {
  LnkHeader,
  LnkIdList,
  LnkIdListItem,
  LnkLinkInfo,
  LnkParseResult,
  LnkStringData
} from "./types.js";
import {
  SHELL_LINK_CLSID,
  SHELL_LINK_HEADER_SIZE,
  describeHotKey,
  linkFlag,
  readCountedString,
  readFiletime,
  readGuid,
  showCommandName
} from "./utils.js";
import { parsePidlItems } from "./pidl.js";
 
type LnkStringField = Exclude<keyof LnkStringData, "size" | "endOffset">;
 
const parseLinkHeader = (dv: DataView, warnings: string[]): LnkHeader | null => {
  if (dv.byteLength < SHELL_LINK_HEADER_SIZE) {
    warnings.push("Shell link header is truncated.");
    return null;
  }
  const headerSize = dv.getUint32(0, true);
  if (headerSize !== SHELL_LINK_HEADER_SIZE) {
    warnings.push(`Unexpected header size ${headerSize} (expected ${SHELL_LINK_HEADER_SIZE}).`);
  }
  const clsid = readGuid(dv, 4);
  if (clsid && clsid !== SHELL_LINK_CLSID) {
    warnings.push("LinkCLSID does not match the Shell Link format.");
  }
  const linkFlags = dv.getUint32(0x14, true);
  const fileAttributes = dv.getUint32(0x18, true);
  const creationTime = readFiletime(dv, 0x1c);
  const accessTime = readFiletime(dv, 0x24);
  const writeTime = readFiletime(dv, 0x2c);
  const fileSize = dv.getUint32(0x34, true);
  const iconIndex = dv.getUint32(0x38, true);
  const showCommand = dv.getUint32(0x3c, true);
  const hotKey = dv.getUint16(0x40, true);
 
  return {
    size: headerSize,
    clsid,
    linkFlags,
    fileAttributes,
    creationTime,
    accessTime,
    writeTime,
    fileSize,
    iconIndex,
    showCommand,
    showCommandName: showCommandName(showCommand),
    hotKey,
    hotKeyLabel: describeHotKey(hotKey)
  };
};
 
const parseIdList = (dv: DataView, offset: number, warnings: string[]): LnkIdList => {
  if (offset + 2 > dv.byteLength) {
    warnings.push("LinkTargetIDList length is truncated.");
    return {
      size: 0,
      items: [],
      truncated: true,
      terminatorPresent: false,
      resolvedPath: null,
      totalSize: 0
    };
  }
  const idListSize = dv.getUint16(offset, true);
  const end = offset + 2 + idListSize;
  if (end > dv.byteLength) {
    warnings.push("LinkTargetIDList extends beyond the file size.");
  }
  const bodyStart = offset + 2;
  const bodyEnd = Math.min(end, dv.byteLength);
  const { items, terminatorPresent } = parsePidlItems(dv, bodyStart, bodyEnd, warnings);
  const resolvedPath = buildPidlPath(items);
  return {
    size: idListSize,
    items,
    terminatorPresent,
    truncated: end > dv.byteLength,
    resolvedPath,
    totalSize: Math.min(idListSize + 2, dv.byteLength - offset)
  };
};
 
const buildPidlPath = (items: LnkIdListItem[]): string | null => {
  if (!items.length) return null;
  const parts: string[] = [];
  const driveFromItem = (item: LnkIdListItem): string | null => {
    const text = item?.longName || item?.shortName;
    if (!text) return null;
    const match = text.match(/^([A-Za-z]):?/);
    return match && match[1] ? `${match[1].toUpperCase()}:` : null;
  };
  const cleanSegment = (text: string | null): string | null =>
    text ? text.replace(/[\\/]+/g, "").trim() : null;
  items.forEach(item => {
    if (!item || item.typeName === "Root") return;
    if (item.typeName === "Drive") {
      const drive = driveFromItem(item);
      if (drive) parts.push(drive);
      return;
    }
    const label = cleanSegment(item.longName || item.shortName);
    if (label) parts.push(label);
  });
  if (!parts.length) return null;
  const [first] = parts;
  if (first && first.endsWith(":")) {
    return [first, ...parts.slice(1)].join("\\");
  }
  return parts.join("\\");
};
 
const parseStringData = (
  dv: DataView,
  offset: number,
  linkFlags: number,
  warnings: string[],
  isUnicode: boolean
): LnkStringData => {
  const strings: Partial<Record<LnkStringField, string>> = {};
  let cursor = offset;
  const readIf = (mask: number, field: LnkStringField) => {
    if (!linkFlag(linkFlags, mask)) return;
    const { value, size } = readCountedString(dv, cursor, isUnicode, warnings, field);
    cursor += size;
    if (value != null) strings[field] = value;
  };
  readIf(0x00000004, "name");
  readIf(0x00000008, "relativePath");
  readIf(0x00000010, "workingDir");
  readIf(0x00000020, "arguments");
  readIf(0x00000040, "iconLocation");
  return { ...(strings as Partial<LnkStringData>), size: cursor - offset, endOffset: cursor };
};
 
export const hasShellLinkSignature = (dv: DataView): boolean =>
  dv.byteLength >= SHELL_LINK_HEADER_SIZE &&
  dv.getUint32(0, true) === SHELL_LINK_HEADER_SIZE &&
  readGuid(dv, 4) === SHELL_LINK_CLSID;
 
export async function parseLnk(file: File): Promise<LnkParseResult | null> {
  const buffer = await file.slice(0, file.size || 0).arrayBuffer();
  const dv = new DataView(buffer);
  const warnings: string[] = [];
 
  if (!hasShellLinkSignature(dv)) return null;
  const header = parseLinkHeader(dv, warnings);
  if (!header) return null;
 
  let cursor = SHELL_LINK_HEADER_SIZE;
  const idList = linkFlag(header.linkFlags, 0x1)
    ? parseIdList(dv, cursor, warnings)
    : null;
  if (idList) cursor += idList.totalSize;
 
  const linkInfo: LnkLinkInfo | null = parseLinkInfo(
    dv,
    cursor,
    warnings,
    linkFlag(header.linkFlags, 0x2) && !linkFlag(header.linkFlags, 0x100)
  );
  if (linkInfo?.size) cursor += linkInfo.size;
 
  const stringData = parseStringData(
    dv,
    cursor,
    header.linkFlags,
    warnings,
    linkFlag(header.linkFlags, 0x80)
  );
  cursor = stringData.endOffset || cursor;
 
  const extraData = parseExtraData(dv, cursor, warnings);
  const linkInfoBase = linkInfo?.localBasePathUnicode || linkInfo?.localBasePath || null;
  const linkInfoSuffix = linkInfo?.commonPathSuffixUnicode || linkInfo?.commonPathSuffix || null;
  const linkInfoParts: string[] = [];
  if (linkInfoBase) linkInfoParts.push(linkInfoBase);
  if (linkInfoSuffix) linkInfoParts.push(linkInfoSuffix);
  const linkInfoPath = linkInfoParts.length
    ? linkInfoParts.join(linkInfoBase ? "\\" : "")
    : null;
 
  return {
    header,
    idList,
    linkInfo,
    stringData,
    extraData,
    warnings,
    linkInfoPath
  };
}