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 8, 2024
1 parent ecb410c commit dad0ab1
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 23 deletions.
147 changes: 130 additions & 17 deletions src/org/nschmidt/ldparteditor/text/Win32LnkParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,28 +36,141 @@ public static File resolveLnkShortcut(File lnkFile) {

// Read header size
final int headerSize = Integer.reverseBytes(is.readInt());
System.out.println(Integer.toHexString(headerSize));

// Read and skip the header
is.readNBytes(headerSize);

// Read and skip 4 bytes of LinkFlags
is.readInt();

// Read and skip 4 bytes of FileAttributesFlags
is.readInt();

// Read and skip 4 bytes of HotKeyFlags
is.readInt();

// Read and skip the LinkTargetIDList
final int idListSize = is.readUnsignedShort();
is.readNBytes(idListSize);
NLogger.debug(Win32LnkParser.class, "HeaderSize: {0}", Integer.toHexString(headerSize)); //$NON-NLS-1$

// Read the header
final byte[] header = is.readNBytes(headerSize - 4);

// CLSID
final StringBuilder sb = new StringBuilder();
sb.append("CLSID: "); //$NON-NLS-1$
sb.append(Integer.toHexString(byteToInt(header[3])));
sb.append(Integer.toHexString(byteToInt(header[2])));
sb.append(Integer.toHexString(byteToInt(header[1])));
sb.append(Integer.toHexString(byteToInt(header[0])));
sb.append("-"); //$NON-NLS-1$
sb.append(Integer.toHexString(byteToInt(header[5])));
sb.append(Integer.toHexString(byteToInt(header[4])));
sb.append("-"); //$NON-NLS-1$
sb.append(Integer.toHexString(byteToInt(header[7])));
sb.append(Integer.toHexString(byteToInt(header[6])));
sb.append("-"); //$NON-NLS-1$
sb.append(Integer.toHexString(byteToInt(header[8])));
sb.append(Integer.toHexString(byteToInt(header[9])));
sb.append("-"); //$NON-NLS-1$
sb.append(Integer.toHexString(byteToInt(header[10])));
sb.append(Integer.toHexString(byteToInt(header[11])));
sb.append(Integer.toHexString(byteToInt(header[12])));
sb.append(Integer.toHexString(byteToInt(header[13])));
sb.append(Integer.toHexString(byteToInt(header[14])));
sb.append(Integer.toHexString(byteToInt(header[15])));
NLogger.debug(Win32LnkParser.class, sb.toString());

// LinkFlags
final boolean hasLinkTargetIDList = flag(header[16], 0);
final boolean hasLinkInfo = flag(header[16], 1);
final boolean hasName = flag(header[16], 2);

NLogger.debug(Win32LnkParser.class, "HasLinkTargetIDList : {0}", hasLinkTargetIDList); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "HasLinkInfo : {0}", hasLinkInfo); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "HasName : {0}", hasName); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "HasRelativePath : {0}", flag(header[16], 3)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "HasWorkingDir : {0}", flag(header[16], 4)); //$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}", flag(header[16], 7)); //$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$
NLogger.debug(Win32LnkParser.class, "RunInSeparateProcess : {0}", flag(header[17], 2)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "Unused1 : {0}", flag(header[17], 3)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "HasDarwinID : {0}", flag(header[17], 4)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "RunAsUser : {0}", flag(header[17], 5)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "HasExpIcon : {0}", flag(header[17], 6)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "NoPidlAlias : {0}", flag(header[17], 7)); //$NON-NLS-1$

NLogger.debug(Win32LnkParser.class, "Unused2 : {0}", flag(header[18], 0)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "RunWithShimLayer : {0}", flag(header[18], 1)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "ForceNoLinkTrack : {0}", flag(header[18], 2)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "EnableTargetMetadata : {0}", flag(header[18], 3)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "DisableLinkPathTracking : {0}", flag(header[18], 4)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "DisableKnownFolderTracking : {0}", flag(header[18], 5)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "DisableKnownFolderAlias : {0}", flag(header[18], 6)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "AllowLinkToLink : {0}", flag(header[18], 7)); //$NON-NLS-1$

NLogger.debug(Win32LnkParser.class, "UnaliasOnSave : {0}", flag(header[19], 0)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "PreferEnvironmentPath : {0}", flag(header[19], 1)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "KeepLocalIDListForUNCTarget : {0}", flag(header[19], 2)); //$NON-NLS-1$

// FileAttributesFlags
NLogger.debug(Win32LnkParser.class, "FILE_ATTRIBUTE_READONLY : {0}", flag(header[20], 0)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "FILE_ATTRIBUTE_HIDDEN : {0}", flag(header[20], 1)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "FILE_ATTRIBUTE_SYSTEM : {0}", flag(header[20], 2)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "Reserved1 : {0}", flag(header[20], 3)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "FILE_ATTRIBUTE_DIRECTORY : {0}", flag(header[20], 4)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "FILE_ATTRIBUTE_ARCHIVE : {0}", flag(header[20], 5)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "Reserved2 : {0}", flag(header[20], 6)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "FILE_ATTRIBUTE_NORMAL : {0}", flag(header[20], 7)); //$NON-NLS-1$

NLogger.debug(Win32LnkParser.class, "FILE_ATTRIBUTE_TEMPORARY : {0}", flag(header[21], 0)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "FILE_ATTRIBUTE_SPARSE_FILE : {0}", flag(header[21], 1)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "FILE_ATTRIBUTE_REPARSE_POINT : {0}", flag(header[21], 2)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "FILE_ATTRIBUTE_COMPRESSED : {0}", flag(header[21], 3)); //$NON-NLS-1$
NLogger.debug(Win32LnkParser.class, "FILE_ATTRIBUTE_OFFLINE : {0}", flag(header[21], 4)); //$NON-NLS-1$
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}", flag(header[21], 6)); //$NON-NLS-1$

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

// Read the target info
final byte[] targetListInfo = is.readNBytes(Math.max(targetListSize - 4, 0));

}

if (hasLinkInfo) {
final int linkInfoSize = is.readShort();
NLogger.debug(Win32LnkParser.class, "LinkInfoSize: {0}", linkInfoSize); //$NON-NLS-1$

// Read the link info
final byte[] linkInfo = is.readNBytes(linkInfoSize - 4);
}

final StringBuilder sb2 = new StringBuilder();

// StringData
// name + relative path (thats the info we want!)
if (hasName) {
final int charCount = byteToInt(is.readByte()) << 8 + byteToInt(is.readByte());
System.out.println(charCount);
for (int i = 0; i < charCount; i++) {
//sb2.append((char) is.readByte());
}
}

NLogger.error(Win32LnkParser.class, sb2.toString());

final StringBuilder sb3 = new StringBuilder();

while (is.available() > 0) {
sb3.append((char) is.readByte());
}

NLogger.error(Win32LnkParser.class, sb3.toString());

} catch (IOException ex) {
NLogger.debug(Win32LnkParser.class, ex);
}

return lnkFile;
}

private static int byteToInt(final byte b) {
return b < 0 ? 128 - b : b;
}

private static boolean flag(final byte b, int bitIndex) {
return (byteToInt(b) & (1 << bitIndex)) > 0;
}
}
26 changes: 20 additions & 6 deletions test/org/nschmidt/ldparteditor/Win32LnkParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.File;

import org.junit.Test;
import org.nschmidt.ldparteditor.logger.NLogger;
import org.nschmidt.ldparteditor.text.Win32LnkParser;

@SuppressWarnings("java:S5960")
Expand All @@ -16,19 +17,32 @@ public class Win32LnkParserTest {

@Test
public void testLinkToFileOnHarddisk() {
String resPath = resourcePath("3782.jpg-Shortcut.lnk"); //$NON-NLS-1$
withDebugging(() -> {
String resPath = resourcePath("3782.jpg-Shortcut.lnk"); //$NON-NLS-1$

File result = Win32LnkParser.resolveLnkShortcut(new File(resPath));
File result = Win32LnkParser.resolveLnkShortcut(new File(resPath));

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

@Test
public void testLinkToFileOnNetwork() {
String resPath = resourcePath("3782.jpg-NetworkShortcut.lnk"); //$NON-NLS-1$
withDebugging(() -> {
String resPath = resourcePath("3782.jpg-NetworkShortcut.lnk"); //$NON-NLS-1$

File result = Win32LnkParser.resolveLnkShortcut(new File(resPath));
File result = Win32LnkParser.resolveLnkShortcut(new File(resPath));

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

private static void withDebugging(Runnable op) {
NLogger.debugging = true;
try {
op.run();
} finally {
NLogger.debugging = false;
}
}
}

0 comments on commit dad0ab1

Please sign in to comment.