Skip to content

Commit

Permalink
Prepared fix for issue #1118.
Browse files Browse the repository at this point in the history
  • Loading branch information
nilsschmidt1337 committed Nov 22, 2024
1 parent a4698ae commit 991a08d
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
53 changes: 43 additions & 10 deletions src/org/nschmidt/ldparteditor/text/Win32LnkParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
public enum Win32LnkParser {
INSTANCE;

private static final String BACKSLASH = "\\"; //$NON-NLS-1$
private static final String CURRENT_DIR_PREFIX = ".\\"; //$NON-NLS-1$
private static final String EMPTY_STRING = ""; //$NON-NLS-1$

/**
* Parses and resolves Windows *.lnk shortcut files
* @param lnkFile the shortcut file.
Expand All @@ -51,22 +55,23 @@ public static File resolveLnkShortcut(File lnkFile) {
sb.append(Integer.toHexString(header[2]));
sb.append(Integer.toHexString(header[1]));
sb.append(Integer.toHexString(header[0]));
sb.append("-"); //$NON-NLS-1$
sb.append('-');
sb.append(Integer.toHexString(header[5]));
sb.append(Integer.toHexString(header[4]));
sb.append("-"); //$NON-NLS-1$
sb.append('-');
sb.append(Integer.toHexString(header[7]));
sb.append(Integer.toHexString(header[6]));
sb.append("-"); //$NON-NLS-1$
sb.append('-');
sb.append(Integer.toHexString(header[8]));
sb.append(Integer.toHexString(header[9]));
sb.append("-"); //$NON-NLS-1$
sb.append('-');
sb.append(Integer.toHexString(header[10]));
sb.append(Integer.toHexString(header[11]));
sb.append(Integer.toHexString(header[12]));
sb.append(Integer.toHexString(header[13]));
sb.append(Integer.toHexString(header[14]));
sb.append(Integer.toHexString(header[15]));
// should be: "CLSID: 02141-00-00-c00-0000046"
NLogger.debug(Win32LnkParser.class, sb.toString());

// LinkFlags
Expand All @@ -75,6 +80,7 @@ public static File resolveLnkShortcut(File lnkFile) {
final boolean hasName = flag(header[16], 2);
final boolean hasRelativePath = flag(header[16], 3);
final boolean hasWorkingDir = flag(header[16], 4);
final boolean isUnicode = flag(header[16], 7);

NLogger.debug(Win32LnkParser.class, "HasLinkTargetIDList : {0}", hasLinkTargetIDList); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "HasLinkInfo : {0}", hasLinkInfo); //$NON-NLS-1$
Expand All @@ -83,7 +89,7 @@ public static File resolveLnkShortcut(File lnkFile) {
NLogger.debug(Win32LnkParser.class, "HasWorkingDir : {0}", hasWorkingDir); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "HasArguments : {0}", flag(header[16], 5)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "HasIconLocation : {0}", flag(header[16], 6)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "IsUnicode : {0}\n", flag(header[16], 7)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "IsUnicode : {0}\n", isUnicode); //$NON-NLS-1$

NLogger.debug(Win32LnkParser.class, "ForceNoLinkInfo : {0}", flag(header[17], 0)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "HasExpString : {0}", flag(header[17], 1)); //$NON-NLS-1$
Expand Down Expand Up @@ -125,11 +131,16 @@ public static File resolveLnkShortcut(File lnkFile) {
NLogger.debug(Win32LnkParser.class, "FILE_ATTRIBUTE_NOT_CONTENT_INDEXED : {0}", flag(header[21], 5)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "FILE_ATTRIBUTE_ENCRYPTED : {0}\n", flag(header[21], 6)); //$NON-NLS-1$

if (!isUnicode) {
NLogger.debug(Win32LnkParser.class, "File contains no unicode encoded paths."); //$NON-NLS-1$
return lnkFile;
}

if (hasLinkTargetIDList) {
final long targetListSize = readUnsignedShort(is);
NLogger.debug(Win32LnkParser.class, "IDListSize: {0}", targetListSize); //$NON-NLS-1$

// Read the target info
// Read and skip the target info
is.readNBytes((int) Math.max(targetListSize, 0));

}
Expand All @@ -138,27 +149,49 @@ public static File resolveLnkShortcut(File lnkFile) {
final long linkInfoSize = readUnsignedInt(is);
NLogger.debug(Win32LnkParser.class, "LinkInfoSize: {0}", linkInfoSize); //$NON-NLS-1$

// Read the link info (-4 because the size was already part of the link info section)
// Read and skip the link info (-4 because the size was already part of the link info section)
is.readNBytes((int) Math.max(linkInfoSize-4, 0));
}


// StringData
if (hasName) {
final String name = readString(is);
NLogger.error(Win32LnkParser.class, "name? :" + name); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "name? :" + name); //$NON-NLS-1$
}

// workingDir + relative path (thats the info we want!)
final String relativePathWithoutCurrentFolder;
if (hasRelativePath) {
final String relativePath = readString(is);
NLogger.error(Win32LnkParser.class, "relativePath? :" + relativePath); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "relativePath? :" + relativePath); //$NON-NLS-1$

if (relativePath.startsWith(CURRENT_DIR_PREFIX)) {
relativePathWithoutCurrentFolder = relativePath.substring(2);
} else {
relativePathWithoutCurrentFolder = relativePath;
}
} else {
relativePathWithoutCurrentFolder = EMPTY_STRING;
}

final String resultingPath;
if (hasWorkingDir) {
final String workingDir = readString(is);
NLogger.error(Win32LnkParser.class, "workingDir? :" + workingDir); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "workingDir? :" + workingDir); //$NON-NLS-1$

if (workingDir.endsWith(BACKSLASH)) {
resultingPath = workingDir + relativePathWithoutCurrentFolder;
} else {
resultingPath = workingDir + BACKSLASH + relativePathWithoutCurrentFolder;
}
} else {
resultingPath = EMPTY_STRING;
}

NLogger.debug(Win32LnkParser.class, "resultingPath?:" + resultingPath); //$NON-NLS-1$

return new File(resultingPath);
} catch (IOException ex) {
NLogger.debug(Win32LnkParser.class, ex);
}
Expand Down
2 changes: 2 additions & 0 deletions test/org/nschmidt/ldparteditor/Win32LnkParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public void testLinkToFileOnHarddisk() {
// HasLinkInfo is true

assertTrue(result.getName().contains("3782.jpg")); //$NON-NLS-1$
assertTrue(result.getName().contains("G")); //$NON-NLS-1$
});
}

Expand All @@ -36,6 +37,7 @@ public void testLinkToFileOnNetwork() {
File result = Win32LnkParser.resolveLnkShortcut(new File(resPath));

assertTrue(result.getName().contains("3782.jpg")); //$NON-NLS-1$
assertTrue(result.getName().contains("192.168.9.222")); //$NON-NLS-1$
});
}

Expand Down

0 comments on commit 991a08d

Please sign in to comment.