Skip to content

Commit

Permalink
added support for older os versions
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriiShamrikov committed Sep 25, 2016
1 parent f6f03dc commit 6d7b8d0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/mslinks/LinkTargetIDList.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public LinkTargetIDList(ByteReader data) throws IOException, ShellLinkException
b[i] = (byte)data.read();
add(new ItemID(b));
} else try {
add(new ItemID(data));
add(new ItemID(data, s));
} catch (UnsupportedCLSIDException e) {
System.err.println("unsupported CLSID");
binary = true;
Expand Down
2 changes: 1 addition & 1 deletion src/mslinks/ShellLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ public String resolveTarget() {
if (header.getLinkFlags().hasLinkTargetIDList() && idlist != null && idlist.isCorrect()) {
String path = "";
for (ItemID i : idlist) {
if (i.getType() == ItemID.TYPE_DRIVE)
if (i.getType() == ItemID.TYPE_DRIVE || i.getType() == ItemID.TYPE_DRIVE_OLD)
path = i.getName();
else if (i.getType() == ItemID.TYPE_DIRECTORY)
path += i.getName() + File.separator;
Expand Down
30 changes: 21 additions & 9 deletions src/mslinks/data/ItemID.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,33 @@ public class ItemID implements Serializable {
public static final int TYPE_UNKNOWN = 0;
public static final int TYPE_FILE = 0x32;
public static final int TYPE_DIRECTORY = 0x31;
public static final int TYPE_DRIVE_OLD = 0x23;
public static final int TYPE_DRIVE = 0x2f;
public static final int TYPE_CLSID = 0x1f;

private int type;
private int size;
private String shortname, longname;
private GUID clsid;
private boolean hasExtension;
private byte[] data;

public ItemID() {
shortname = "";
longname = "";
hasExtension = true;
}

public ItemID(byte[] d) {
data = d;
}

public ItemID(ByteReader br) throws IOException, ShellLinkException {
public ItemID(ByteReader br, int maxSize) throws IOException, ShellLinkException {
int pos = br.getPosition();
type = br.read();
if (type == TYPE_DRIVE) {
setName(br.readString(22));
br.seek(pos + 23 - br.getPosition());
if (type == TYPE_DRIVE || type == TYPE_DRIVE_OLD) {
setName(br.readString(maxSize - 1));
br.seek(pos + maxSize - br.getPosition());
} else if (type == TYPE_FILE || type == TYPE_DIRECTORY) {
br.read(); // unknown
size = (int)br.read4bytes();
Expand All @@ -70,8 +73,14 @@ public ItemID(ByteReader br) throws IOException, ShellLinkException {
shortname = br.readString(13);
if (((br.getPosition() - pos) & 1) != 0)
br.read();
if (pos + maxSize - br.getPosition() < 2) {
br.seek(pos + maxSize - br.getPosition());
hasExtension = false;
return;
}

pos = br.getPosition();
int sz = (int)br.read2bytes();
int extSize = (int)br.read2bytes();
int extensionVersion = (int)br.read2bytes();
br.read4bytes(); // unknown
br.read4bytes(); // date created
Expand All @@ -84,8 +93,8 @@ public ItemID(ByteReader br) throws IOException, ShellLinkException {
case EXT_VERSION_WIN8: br.seek(30); break;
default: throw new ShellLinkException("Unknown extension version");
}
longname = br.readUnicodeString(pos + sz - br.getPosition());
br.seek(pos + sz - br.getPosition()); // unknown
longname = br.readUnicodeString(pos + extSize - br.getPosition());
br.seek(pos + extSize - br.getPosition()); // unknown
} else if (type == TYPE_CLSID) {
br.read(); // unknown
clsid = new GUID(br);
Expand All @@ -111,6 +120,7 @@ public void serialize(ByteWriter bw) throws IOException {
clsid.serialize(bw);
return;
case TYPE_DRIVE:
case TYPE_DRIVE_OLD:
byte[] b = getName().getBytes();
bw.write(b);
for (int i=0; i<22-b.length; i++)
Expand All @@ -133,6 +143,8 @@ public void serialize(ByteWriter bw) throws IOException {
bw.write(0);
if (((bw.getPosition() - pos) & 1) != 0)
bw.write(0);
if (!hasExtension)
return;

bw.write2bytes(2 + 2 + ub1.length + 4 + 4 + ub2.length + 4 + (longname.length() + 1) * 2 + 2);
bw.write2bytes(EXT_VERSION_WINXP);
Expand Down Expand Up @@ -173,7 +185,7 @@ public ItemID setName(String s) throws ShellLinkException {

shortname = name + ext;
}
if (type == TYPE_DRIVE) {
if (type == TYPE_DRIVE || type == TYPE_DRIVE_OLD) {
if (Pattern.matches("\\w+:\\\\", s))
shortname = longname = s;
else if (Pattern.matches("\\w+:", s))
Expand All @@ -200,7 +212,7 @@ public ItemID setType(int t) throws ShellLinkException {
clsid = mycomputer;
return this;
}
if (t == TYPE_FILE || t == TYPE_DIRECTORY || t == TYPE_DRIVE) {
if (t == TYPE_FILE || t == TYPE_DIRECTORY || t == TYPE_DRIVE || t == TYPE_DRIVE_OLD) {
type = t;
return this;
}
Expand Down

0 comments on commit 6d7b8d0

Please sign in to comment.