-
-
Save subutux/baf20ef9444dc9d6fba85083fd2066ff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import locale | |
import struct | |
def __readLink(path): | |
# http://stackoverflow.com/a/28952464/1119602 | |
with open(path, 'rb') as stream: | |
content = stream.read() | |
# skip first 20 bytes (HeaderSize and LinkCLSID) | |
# read the LinkFlags structure (4 bytes) | |
lflags = struct.unpack('I', content[0x14:0x18])[0] | |
position = 0x18 | |
# if the HasLinkTargetIDList bit is set then skip the stored IDList | |
# structure and header | |
if (lflags & 0x01) == 1: | |
position = struct.unpack('H', content[0x4C:0x4E])[0] + 0x4E | |
last_pos = position | |
position += 0x04 | |
# get how long the file information is (LinkInfoSize) | |
length = struct.unpack('I', content[last_pos:position])[0] | |
# skip 12 bytes (LinkInfoHeaderSize, LinkInfoFlags and VolumeIDOffset) | |
position += 0x0C | |
# go to the LocalBasePath position | |
lbpos = struct.unpack('I', content[position:position+0x04])[0] | |
position = last_pos + lbpos | |
# read the string at the given position of the determined length | |
size = (length + last_pos) - position - 0x02 | |
content = content[position:position+size].split(b'\x00', 1) | |
return content[-1].decode('utf-16' if len(content) > 1 | |
else locale.getdefaultlocale()[1]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment