All files / binary101/analyzers/lnk property-values.ts

51.01% Statements 126/247
35.48% Branches 11/31
100% Functions 1/1
51.01% Lines 126/247

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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 2481x 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 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                 1x 1x 8x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 132x 132x 132x 132x 132x 128x 4x 4x 4x 4x 1x 1x                                                                                                                                       1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x                   8x 8x 8x 8x 8x     8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 4x 8x                                             8x 4x 4x 4x 4x 4x   8x                   8x 8x 8x 8x 8x 8x 8x 8x       8x 8x 1x 1x  
"use strict";
 
import { readFiletime, readGuid } from "./utils.js";
import type { LnkPropertyScalar, LnkPropertyValue } from "./types.js";
 
const PROPERTY_KEY_LABELS: Record<string, string> = {
  "f29f85e0-4ff9-1068-ab91-08002b27b3d9:2": "System.Link.TargetParsingPath",
  "f29f85e0-4ff9-1068-ab91-08002b27b3d9:3": "System.Link.WorkingDirectory",
  "f29f85e0-4ff9-1068-ab91-08002b27b3d9:5": "System.Link.Arguments",
  "446d16b1-8dad-4870-a748-402ea43d788c:104": "System.VolumeId"
};
 
const PROPERTY_TYPE_NAMES: Record<number, string> = {
  0x0000: "Empty",
  0x0001: "Null",
  0x0002: "Signed 16-bit",
  0x0003: "Signed 32-bit",
  0x0004: "Float",
  0x0005: "Double",
  0x0006: "Currency",
  0x0007: "Date",
  0x0008: "BSTR",
  0x000b: "Boolean",
  0x0010: "Signed 8-bit",
  0x0011: "Unsigned 8-bit",
  0x0012: "Unsigned 16-bit",
  0x0013: "Unsigned 32-bit",
  0x0014: "Signed 64-bit",
  0x0015: "Unsigned 64-bit",
  0x001e: "ANSI string",
  0x001f: "Unicode string",
  0x0040: "Filetime",
  0x0041: "Blob",
  0x0048: "CLSID"
};
 
const ansiFromBytes = (bytes: Uint8Array): string => {
  let out = "";
  for (let i = 0; i < bytes.length; i += 1) {
    const code = bytes[i] ?? 0;
    if (code === 0) break;
    out += String.fromCharCode(code);
  }
  return out;
};
 
const propertyKeyLabel = (formatId: string | null, propertyId: number): string | null =>
  PROPERTY_KEY_LABELS[`${formatId}:${propertyId}`] || null;
 
const propertyTypeName = (type: number): string | null => PROPERTY_TYPE_NAMES[type] || null;
 
const parsePropertyString = (
  dv: DataView,
  offset: number,
  available: number,
  isUnicode: boolean
): string | null => {
  if (available < 4) return null;
  const count = dv.getUint32(offset, true);
  if (count <= 0) return "";
  const maxBytes = available - 4;
  const byteLength = isUnicode ? Math.min(maxBytes, count * 2) : Math.min(maxBytes, count);
  const bytes = new Uint8Array(dv.buffer, dv.byteOffset + offset + 4, Math.max(0, byteLength));
  if (!bytes.length) return "";
  if (isUnicode) {
    let result = "";
    for (let i = 0; i + 1 < bytes.length; i += 2) {
      const low = bytes[i] ?? 0;
      const high = bytes[i + 1] ?? 0;
      const code = low | (high << 8);
      if (code === 0) break;
      result += String.fromCharCode(code);
    }
    return result.replace(/\0+$/, "");
  }
  return ansiFromBytes(bytes).replace(/\0+$/, "");
};
 
const decodeVector = (
  dv: DataView,
  offset: number,
  available: number,
  baseType: number
): { value: LnkPropertyScalar[]; truncated: boolean } => {
  if (available < 4) return { value: [], truncated: true };
  const count = dv.getUint32(offset, true);
  const values: LnkPropertyScalar[] = [];
  let cursor = offset + 4;
  for (let i = 0; i < count && cursor < offset + available; i += 1) {
    const remaining = offset + available - cursor;
    let element: LnkPropertyScalar;
    switch (baseType) {
      case 0x0011:
        element = dv.getUint8(cursor);
        cursor += 1;
        break;
      case 0x0010:
        element = dv.getInt8(cursor);
        cursor += 1;
        break;
      case 0x0002:
        if (remaining < 2) return { value: values, truncated: true };
        element = dv.getInt16(cursor, true);
        cursor += 2;
        break;
      case 0x0012:
        if (remaining < 2) return { value: values, truncated: true };
        element = dv.getUint16(cursor, true);
        cursor += 2;
        break;
      case 0x0003:
        if (remaining < 4) return { value: values, truncated: true };
        element = dv.getInt32(cursor, true);
        cursor += 4;
        break;
      case 0x0013:
        if (remaining < 4) return { value: values, truncated: true };
        element = dv.getUint32(cursor, true);
        cursor += 4;
        break;
      case 0x0014:
        if (remaining < 8 || typeof dv.getBigInt64 !== "function") {
          return { value: values, truncated: true };
        }
        element = dv.getBigInt64(cursor, true);
        cursor += 8;
        break;
      case 0x0015:
        if (remaining < 8 || typeof dv.getBigUint64 !== "function") {
          return { value: values, truncated: true };
        }
        element = dv.getBigUint64(cursor, true);
        cursor += 8;
        break;
      case 0x0048:
        if (remaining < 16) return { value: values, truncated: true };
        element = readGuid(dv, cursor);
        cursor += 16;
        break;
      default:
        return { value: values, truncated: true };
    }
    values.push(element);
  }
  return { value: values, truncated: cursor > offset + available };
};
 
const parsePropertyValue = (
  dv: DataView,
  offset: number,
  declaredSize: number,
  warnings: string[],
  label: string
): {
    type: number | null;
    typeName: string | null;
    value: LnkPropertyValue;
    truncated: boolean;
    valueSize: number;
    isVector: boolean;
  } => {
  if (declaredSize < 4 || offset + 4 > dv.byteLength) {
    return {
      type: null,
      typeName: null,
      value: null,
      truncated: true,
      valueSize: declaredSize,
      isVector: false
    };
  }
  let type = dv.getUint16(offset, true);
  const rawType = type;
  const lowByte = rawType & 0xff;
  const highByte = (rawType >> 8) & 0xff;
  if (lowByte === 0 && highByte !== 0 && propertyTypeName(highByte)) {
    type = highByte;
  }
  dv.getUint16(offset + 2, true);
  const isVector = (type & 0x1000) !== 0;
  const baseType = isVector ? type & ~0x1000 : type;
  const dataStart = offset + 4;
  const declaredEnd = offset + declaredSize;
  const clampedEnd = Math.min(declaredEnd, dv.byteLength);
  const available = Math.max(0, clampedEnd - dataStart);
  const typeName = propertyTypeName(baseType);
  let truncated = declaredEnd > dv.byteLength;
 
  const parseScalar = (): LnkPropertyValue => {
    switch (baseType) {
      case 0x001f:
        return parsePropertyString(dv, dataStart, available, true);
      case 0x001e:
        return parsePropertyString(dv, dataStart, available, false);
      case 0x0013:
        return available >= 4 ? dv.getUint32(dataStart, true) : null;
      case 0x0003:
        return available >= 4 ? dv.getInt32(dataStart, true) : null;
      case 0x000b:
        return available >= 2 ? dv.getInt16(dataStart, true) !== 0 : null;
      case 0x0014:
        return available >= 8 && typeof dv.getBigInt64 === "function"
          ? dv.getBigInt64(dataStart, true)
          : null;
      case 0x0015:
        return available >= 8 && typeof dv.getBigUint64 === "function"
          ? dv.getBigUint64(dataStart, true)
          : null;
      case 0x0040: {
        if (available >= 8) {
          const ftView = new DataView(dv.buffer, dv.byteOffset + dataStart, Math.min(8, available));
          return readFiletime(ftView, 0).iso;
        }
        return null;
      }
      case 0x0048: {
        if (available >= 16) {
          const guidView = new DataView(dv.buffer, dv.byteOffset + dataStart, Math.min(16, available));
          return readGuid(guidView, 0);
        }
        return null;
      }
      case 0x0041: {
        const bytes = new Uint8Array(
          dv.buffer,
          dv.byteOffset + dataStart,
          Math.max(0, Math.min(available, Math.max(0, declaredSize - 4)))
        );
        return { length: bytes.length };
      }
      default:
        return null;
    }
  };
 
  const parsedVector = isVector ? decodeVector(dv, dataStart, available, baseType) : null;
  const value = parsedVector ? parsedVector.value : parseScalar();
  truncated = truncated || parsedVector?.truncated === true;
 
  if (declaredEnd > dv.byteLength) {
    const labelText = label || "property store entry";
    warnings.push(`${labelText} value is truncated.`);
  }
  return { type, typeName, value, truncated, valueSize: declaredSize, isVector };
};
 
export { parsePropertyValue, propertyKeyLabel, propertyTypeName };