Skip to content
Snippets Groups Projects
Commit 8d35eb97 authored by Dmitry Shamrikov's avatar Dmitry Shamrikov
Browse files

header reading/writing implemented

parents
No related branches found
No related tags found
No related merge requests found
Showing with 895 additions and 0 deletions
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
<classpathentry kind="output" path="bin"/>
</classpath>
bin/
*.pdf
.project 0 → 100644
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>mslinks</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
org.eclipse.jdt.core.compiler.debug.localVariable=generate
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
package mslinks;
import java.io.IOException;
public class BitSet32 {
private int d;
public BitSet32(int n) {
d = n;
}
public BitSet32(ByteReader data) throws IOException {
d = (int)data.read4bytes();
}
protected boolean get(int i) {
return (d & (1 << i)) != 0;
}
protected void set(int i) {
d = (d & ~(1 << i)) | (1 << i);
}
protected void clear(int i) {
d = d & ~(1 << i);
}
public void serialize(ByteWriter bw) throws IOException {
bw.write4bytes(d);
}
}
package mslinks;
import java.io.IOException;
import java.io.InputStream;
public class ByteReader extends InputStream {
private InputStream stream;
private Endianness end = Endianness.LITTLE_ENDIAN;
public ByteReader(InputStream in) {
stream = in;
}
public ByteReader setBigEndian() {
end = Endianness.BIG_ENDIAN;
return this;
}
public ByteReader setLittleEndian() {
end = Endianness.LITTLE_ENDIAN;
return this;
}
public ByteReader changeEndiannes() {
if (isLitteEndian())
setBigEndian();
else
setLittleEndian();
return this;
}
public boolean isBigEndian() {
return end == Endianness.BIG_ENDIAN;
}
public boolean isLitteEndian() {
return end == Endianness.LITTLE_ENDIAN;
}
@Override
public int read() throws IOException {
return stream.read();
}
public long read2bytes() throws IOException {
long b0 = read();
long b1 = read();
if (isLitteEndian())
return b0 | (b1 << 8);
else
return b1 | (b0 << 8);
}
public long read3bytes() throws IOException {
long b0 = read();
long b1 = read();
long b2 = read();
if (isLitteEndian())
return b0 | (b1 << 8) | (b2 << 16);
else
return b2 | (b1 << 8) | (b0 << 16);
}
public long read4bytes() throws IOException {
long b0 = read();
long b1 = read();
long b2 = read();
long b3 = read();
if (isLitteEndian())
return b0 | (b1 << 8) | (b2 << 16) | (b3 << 24);
else
return b3 | (b2 << 8) | (b1 << 16) | (b0 << 24);
}
public long read5bytes() throws IOException {
long b0 = read();
long b1 = read();
long b2 = read();
long b3 = read();
long b4 = read();
if (isLitteEndian())
return b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) | (b4 << 32);
else
return b4 | (b3 << 8) | (b2 << 16) | (b1 << 24) | (b0 << 32);
}
public long read6bytes() throws IOException {
long b0 = read();
long b1 = read();
long b2 = read();
long b3 = read();
long b4 = read();
long b5 = read();
if (isLitteEndian())
return b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) | (b4 << 32) | (b5 << 40);
else
return b5 | (b4 << 8) | (b3 << 16) | (b2 << 24) | (b1 << 32) | (b0 << 40);
}
public long read7bytes() throws IOException {
long b0 = read();
long b1 = read();
long b2 = read();
long b3 = read();
long b4 = read();
long b5 = read();
long b6 = read();
if (isLitteEndian())
return b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) | (b4 << 32) | (b5 << 40) | (b6 << 48);
else
return b6 | (b5 << 8) | (b4 << 16) | (b3 << 24) | (b2 << 32) | (b1 << 40) | (b0 << 48);
}
public long read8bytes() throws IOException {
long b0 = read();
long b1 = read();
long b2 = read();
long b3 = read();
long b4 = read();
long b5 = read();
long b6 = read();
long b7 = read();
if (isLitteEndian())
return b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) | (b4 << 32) | (b5 << 40) | (b6 << 48) | (b7 << 56);
else
return b7 | (b6 << 8) | (b5 << 16) | (b4 << 24) | (b3 << 32) | (b2 << 40) | (b1 << 48) | (b0 << 56);
}
public static short reverse(short n) {
return (short)(((n & 0xff) << 8) | ((n & 0xff00) >> 8));
}
public static int reverse(int n) {
return ((n & 0xff) << 24) | ((n & 0xff00) << 8) | ((n & 0xff0000) >> 8) | ((n & 0xff000000) >>> 24);
}
public static long reverse(long n) {
return ((n & 0xff) << 56) | ((n & 0xff00) << 40) | ((n & 0xff0000) << 24) | ((n & 0xff000000) << 8) |
((n & 0xff00000000L) >> 8) | ((n & 0xff0000000000L) >> 24) | ((n & 0xff000000000000L) >> 40) | ((n & 0xff00000000000000L) >>> 56);
}
public static short makeShortB(byte b0, byte b1) {
return (short)((i(b0) << 8) | i(b1));
}
public static int makeIntB(byte b0, byte b1, byte b2, byte b3) {
return (i(b0) << 24) | (i(b1) << 16) | (i(b2) << 8) | i(b3);
}
public static long makeLongB(byte b0, byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7) {
return (l(b0) << 56) | (l(b1) << 48) | (l(b2) << 40) | (l(b3) << 32) | (l(b4) << 24) | (l(b5) << 16) | (l(b6) << 8) | l(b7);
}
public static short makeShortL(byte b0, byte b1) {
return (short)((i(b1) << 8) | i(b0));
}
public static int makeIntL(byte b0, byte b1, byte b2, byte b3) {
return (i(b3) << 24) | (i(b2) << 16) | (i(b1) << 8) | i(b0);
}
public static long makeLongL(byte b0, byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7) {
return (l(b7) << 56) | (l(b6) << 48) | (l(b5) << 40) | (l(b4) << 32) | (l(b3) << 24) | (l(b2) << 16) | (l(b1) << 8) | l(b0);
}
private static int i(byte b) {
return b & 0xff;
}
private static long l(byte b) {
return b & 0xffL;
}
}
enum Endianness {
BIG_ENDIAN, LITTLE_ENDIAN
}
\ No newline at end of file
package mslinks;
import java.io.IOException;
import java.io.OutputStream;
public class ByteWriter extends OutputStream {
private OutputStream stream;
private Endianness end = Endianness.LITTLE_ENDIAN;
public ByteWriter(OutputStream out) {
stream = out;
}
public ByteWriter setBigEndian() {
end = Endianness.BIG_ENDIAN;
return this;
}
public ByteWriter setLittleEndian() {
end = Endianness.LITTLE_ENDIAN;
return this;
}
public ByteWriter changeEndiannes() {
if (isLitteEndian())
setBigEndian();
else
setLittleEndian();
return this;
}
public boolean isBigEndian() {
return end == Endianness.BIG_ENDIAN;
}
public boolean isLitteEndian() {
return end == Endianness.LITTLE_ENDIAN;
}
@Override
public void write(int b) throws IOException {
stream.write(b);
}
public void write(long b) throws IOException {
write((int)b);
}
public void write2bytes(long n) throws IOException {
long b0 = n & 0xff;
long b1 = (n & 0xff00) >> 8;
if (isLitteEndian()) {
write(b0); write(b1);
} else {
write(b1); write(b0);
}
}
public void write3bytes(long n) throws IOException {
long b0 = n & 0xff;
long b1 = (n & 0xff00) >> 8;
long b2 = (n & 0xff0000) >> 16;
if (isLitteEndian()) {
write(b0); write(b1); write(b2);
} else {
write(b2); write(b1); write(b0);
}
}
public void write4bytes(long n) throws IOException {
long b0 = n & 0xff;
long b1 = (n & 0xff00) >> 8;
long b2 = (n & 0xff0000) >> 16;
long b3 = (n & 0xff000000) >>> 24;
if (isLitteEndian()) {
write(b0); write(b1); write(b2); write(b3);
} else {
write(b3); write(b2); write(b1); write(b0);
}
}
public void write5bytes(long n) throws IOException {
long b0 = n & 0xff;
long b1 = (n & 0xff00) >> 8;
long b2 = (n & 0xff0000) >> 16;
long b3 = (n & 0xff000000) >>> 24;
long b4 = (n & 0xff00000000L) >> 32;
if (isLitteEndian()) {
write(b0); write(b1); write(b2); write(b3); write(b4);
} else {
write(b4); write(b3); write(b2); write(b1); write(b0);
}
}
public void write6bytes(long n) throws IOException {
long b0 = n & 0xff;
long b1 = (n & 0xff00) >> 8;
long b2 = (n & 0xff0000) >> 16;
long b3 = (n & 0xff000000) >>> 24;
long b4 = (n & 0xff00000000L) >> 32;
long b5 = (n & 0xff0000000000L) >> 40;
if (isLitteEndian()) {
write(b0); write(b1); write(b2); write(b3); write(b4); write(b5);
} else {
write(b5); write(b4); write(b3); write(b2); write(b1); write(b0);
}
}
public void write7bytes(long n) throws IOException {
long b0 = n & 0xff;
long b1 = (n & 0xff00) >> 8;
long b2 = (n & 0xff0000) >> 16;
long b3 = (n & 0xff000000) >>> 24;
long b4 = (n & 0xff00000000L) >> 32;
long b5 = (n & 0xff0000000000L) >> 40;
long b6 = (n & 0xff000000000000L) >> 48;
if (isLitteEndian()) {
write(b0); write(b1); write(b2); write(b3); write(b4); write(b5); write(b6);
} else {
write(b6); write(b5); write(b4); write(b3); write(b2); write(b1); write(b0);
}
}
public void write8bytes(long n) throws IOException {
long b0 = n & 0xff;
long b1 = (n & 0xff00) >> 8;
long b2 = (n & 0xff0000) >> 16;
long b3 = (n & 0xff000000) >>> 24;
long b4 = (n & 0xff00000000L) >> 32;
long b5 = (n & 0xff0000000000L) >> 40;
long b6 = (n & 0xff000000000000L) >> 48;
long b7 = (n & 0xff00000000000000L) >>> 56;
if (isLitteEndian()) {
write(b0); write(b1); write(b2); write(b3); write(b4); write(b5); write(b6); write(b7);
} else {
write(b7); write(b6); write(b5); write(b4); write(b3); write(b2); write(b1); write(b0);
}
}
}
package mslinks;
import java.io.IOException;
public class FileAttributesFlags extends BitSet32 {
public FileAttributesFlags(int n) {
super(n);
}
public FileAttributesFlags(ByteReader data) throws IOException {
super(data);
}
public boolean isReadonly() { return get(0); }
public boolean isHidden() { return get(1); }
public boolean isSystem() { return get(2); }
public boolean isDirecory() { return get(4); }
public boolean isArchive() { return get(5); }
public boolean isNormal() { return get(7); }
public boolean isTemporary() { return get(8); }
public boolean isSparseFile() { return get(9); }
public boolean isReparsePoint() { return get(10); }
public boolean isCompressed() { return get(11); }
public boolean isOffline() { return get(12); }
public boolean isNotContentIndexed() { return get(13); }
public boolean isEncypted() { return get(14); }
public void setReadonly() { set(0); }
public void setHidden() { set(1); }
public void setSystem() { set(2); }
public void setDirecory() { set(4); }
public void setArchive() { set(5); }
public void setNormal() { set(7); }
public void setTemporary() { set(8); }
public void setSparseFile() { set(9); }
public void setReparsePoint() { set(10); }
public void setCompressed() { set(11); }
public void setOffline() { set(12); }
public void setNotContentIndexed() { set(13); }
public void setEncypted() { set(14); }
public void clearReadonly() { clear(0); }
public void clearHidden() { clear(1); }
public void clearSystem() { clear(2); }
public void clearDirecory() { clear(4); }
public void clearArchive() { clear(5); }
public void clearNormal() { clear(7); }
public void clearTemporary() { clear(8); }
public void clearSparseFile() { clear(9); }
public void clearReparsePoint() { clear(10); }
public void clearCompressed() { clear(11); }
public void clearOffline() { clear(12); }
public void clearNotContentIndexed() { clear(13); }
public void clearEncypted() { clear(14); }
}
package mslinks;
import java.io.IOException;
import java.util.GregorianCalendar;
public class Filetime extends GregorianCalendar {
private long residue;
public Filetime(ByteReader data) throws IOException {
this(data.read8bytes());
}
public Filetime(long time) {
long t = time / 10000;
residue = time - t;
setTimeInMillis(t);
add(GregorianCalendar.YEAR, -369);
}
public long toLong() {
GregorianCalendar tmp = (GregorianCalendar)clone();
tmp.add(GregorianCalendar.YEAR, 369);
return tmp.getTimeInMillis() + residue;
}
public void serialize(ByteWriter bw) throws IOException {
bw.write8bytes(toLong());
}
}
package mslinks;
import java.io.IOException;
public class GUID {
private int d1;
private short d2, d3, d4;
private long d5;
public GUID(byte[] d) {
d1 = ByteReader.makeIntL(d[0], d[1], d[2], d[3]);
d2 = ByteReader.makeShortL(d[4], d[5]);
d3 = ByteReader.makeShortL(d[6], d[7]);
d4 = ByteReader.makeShortB(d[8], d[9]);
d5 = ByteReader.makeLongB((byte)0, (byte)0, d[10], d[11], d[12], d[13], d[14], d[15]);
}
public GUID(ByteReader data) throws IOException {
d1 = (int)data.read4bytes();
d2 = (short)data.read2bytes();
d3 = (short)data.read2bytes();
data.changeEndiannes();
d4 = (short)data.read2bytes();
d5 = data.read6bytes();
data.changeEndiannes();
}
public String toString() {
return String.format("%08x-%04x-%04x-%04x-%012x", d1, d2, d3, d4, d5);
}
public boolean equals(Object o) {
GUID g = (GUID)o;
return d1 == g.d1 && d2 == g.d2 && d3 == g.d3 && d4 == g.d4 && d5 == g.d5;
}
public void serialize(ByteWriter bw) throws IOException {
bw.write4bytes(d1);
bw.write2bytes(d2);
bw.write2bytes(d3);
bw.changeEndiannes();
bw.write2bytes(d4);
bw.write6bytes(d5);
bw.changeEndiannes();
}
}
package mslinks;
import java.io.IOException;
import java.util.HashMap;
public class HotKeyFlags {
private static HashMap<Byte, String> keys = new HashMap<Byte, String>() {{
put((byte)0x30, "0");
put((byte)0x31, "1");
put((byte)0x32, "2");
put((byte)0x33, "3");
put((byte)0x34, "4");
put((byte)0x35, "5");
put((byte)0x36, "6");
put((byte)0x37, "7");
put((byte)0x38, "8");
put((byte)0x39, "9");
put((byte)0x41, "A");
put((byte)0x42, "B");
put((byte)0x43, "C");
put((byte)0x44, "D");
put((byte)0x45, "E");
put((byte)0x46, "F");
put((byte)0x47, "G");
put((byte)0x48, "H");
put((byte)0x49, "I");
put((byte)0x4A, "J");
put((byte)0x4B, "K");
put((byte)0x4C, "L");
put((byte)0x4D, "M");
put((byte)0x4E, "N");
put((byte)0x4F, "O");
put((byte)0x50, "P");
put((byte)0x51, "Q");
put((byte)0x52, "R");
put((byte)0x53, "S");
put((byte)0x54, "T");
put((byte)0x55, "U");
put((byte)0x56, "V");
put((byte)0x57, "W");
put((byte)0x58, "X");
put((byte)0x59, "Y");
put((byte)0x5A, "Z");
put((byte)0x70, "F1");
put((byte)0x71, "F2");
put((byte)0x72, "F3");
put((byte)0x73, "F4");
put((byte)0x74, "F5");
put((byte)0x75, "F6");
put((byte)0x76, "F7");
put((byte)0x77, "F8");
put((byte)0x78, "F9");
put((byte)0x79, "F10");
put((byte)0x7A, "F11");
put((byte)0x7B, "F12");
put((byte)0x7C, "F13");
put((byte)0x7D, "F14");
put((byte)0x7E, "F15");
put((byte)0x7F, "F16");
put((byte)0x80, "F17");
put((byte)0x81, "F18");
put((byte)0x82, "F19");
put((byte)0x83, "F20");
put((byte)0x84, "F21");
put((byte)0x85, "F22");
put((byte)0x86, "F23");
put((byte)0x87, "F24");
put((byte)0x90, "NUM LOCK");
put((byte)0x91, "SCROLL LOCK");
put((byte)0x01, "SHIFT");
put((byte)0x02, "CTRL");
put((byte)0x04, "ALT");
}};
private static HashMap<String, Byte> keysr = new HashMap<String, Byte>();
static {
for (Byte i : keys.keySet())
keysr.put(keys.get(i), i);
}
private byte low;
private byte high;
public HotKeyFlags(ByteReader data) throws IOException {
low = (byte)data.read();
high = (byte)data.read();
}
public String getKey() {
return keys.get(low);
}
public void setKey(String k) {
if (k == null || k.equals(""))
return;
low = keysr.get(k);
}
public boolean isShift() { return (high & 1) != 0; }
public boolean isCtrl() { return (high & 2) != 0; }
public boolean isAlt() { return (high & 4) != 0; }
public void setShift() { high = (byte)(1 | (high & 6)); }
public void setCtrl() { high = (byte)(2 | (high & 5)); }
public void setAlt() { high = (byte)(4 | (high & 3)); }
public void clearShift() { high = (byte)(high & 6); }
public void clearCtrl() { high = (byte)(high & 5); }
public void clearAlt() { high = (byte)(high & 3); }
public void serialize(ByteWriter bw) throws IOException {
bw.write(low);
bw.write(high);
}
}
package mslinks;
import java.io.IOException;
public class LinkFlags extends BitSet32 {
public LinkFlags(int n) {
super(n);
}
public LinkFlags(ByteReader data) throws IOException {
super(data);
}
public boolean hasLinkTargetIDList() { return get(0); }
public boolean hasLinkInfo() { return get(1); }
public boolean hasName() { return get(2); }
public boolean hasRelativePath() { return get(3); }
public boolean hasWorkingDir() { return get(4); }
public boolean hasArguments() { return get(5); }
public boolean hasIconLocation() { return get(6); }
public boolean isUnicode() { return get(7); }
public boolean forceNoLinkInfo() { return get(8); }
public boolean hasExpString() { return get(9); }
public boolean runInSeparateProcess() { return get(10); }
public boolean hasDarwinID() { return get(12); }
public boolean runAsUser() { return get(13); }
public boolean hasExpIcon() { return get(14); }
public boolean noPidlAlias() { return get(15); }
public boolean runWithShimLayer() { return get(17); }
public boolean forceNoLinkTrack() { return get(18); }
public boolean enableTargetMetadata() { return get(19); }
public boolean disableLinkPathTracking() { return get(20); }
public boolean disableKnownFolderTracking() { return get(21); }
public boolean disableKnownFolderAlias() { return get(22); }
public boolean allowLinkToLink() { return get(23); }
public boolean unaliasOnSave() { return get(24); }
public boolean preferEnvironmentPath() { return get(25); }
public boolean keepLocalIDListForUNCTarget() { return get(26); }
public void setHasLinkTargetIDList() { set(0); }
public void setHasLinkInfo() { set(1); }
public void setHasName() { set(2); }
public void setHasRelativePath() { set(3); }
public void setHasWorkingDir() { set(4); }
public void setHasArguments() { set(5); }
public void setHasIconLocation() { set(6); }
public void setIsUnicode() { set(7); }
public void setForceNoLinkInfo() { set(8); }
public void setHasExpString() { set(9); }
public void setRunInSeparateProcess() { set(10); }
public void setHasDarwinID() { set(12); }
public void setRunAsUser() { set(13); }
public void setHasExpIcon() { set(14); }
public void setNoPidlAlias() { set(15); }
public void setRunWithShimLayer() { set(17); }
public void setForceNoLinkTrack() { set(18); }
public void setEnableTargetMetadata() { set(19); }
public void setDisableLinkPathTracking() { set(20); }
public void setDisableKnownFolderTracking() { set(21); }
public void setDisableKnownFolderAlias() { set(22); }
public void setAllowLinkToLink() { set(23); }
public void setUnaliasOnSave() { set(24); }
public void setPreferEnvironmentPath() { set(25); }
public void setKeepLocalIDListForUNCTarget() { set(26); }
public void clearHasLinkTargetIDList() { clear(0); }
public void clearHasLinkInfo() { clear(1); }
public void clearHasName() { clear(2); }
public void clearHasRelativePath() { clear(3); }
public void clearHasWorkingDir() { clear(4); }
public void clearHasArguments() { clear(5); }
public void clearHasIconLocation() { clear(6); }
public void clearIsUnicode() { clear(7); }
public void clearForceNoLinkInfo() { clear(8); }
public void clearHasExpString() { clear(9); }
public void clearRunInSeparateProcess() { clear(10); }
public void clearHasDarwinID() { clear(12); }
public void clearRunAsUser() { clear(13); }
public void clearHasExpIcon() { clear(14); }
public void clearNoPidlAlias() { clear(15); }
public void clearRunWithShimLayer() { clear(17); }
public void clearForceNoLinkTrack() { clear(18); }
public void clearEnableTargetMetadata() { clear(19); }
public void clearDisableLinkPathTracking() { clear(20); }
public void clearDisableKnownFolderTracking() { clear(21); }
public void clearDisableKnownFolderAlias() { clear(22); }
public void clearAllowLinkToLink() { clear(23); }
public void clearUnaliasOnSave() { clear(24); }
public void clearPreferEnvironmentPath() { clear(25); }
public void clearKeepLocalIDListForUNCTarget() { clear(26); }
}
package mslinks;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.GregorianCalendar;
public class Main {
public static void main(String[] args) throws IOException, ShellLinkException {
ShellLink link = new ShellLink("testlink.lnk");
Filetime ft = link.getWriteTime();
System.out.println(String.format("%d:%d:%d %d.%d.%d", ft.get(GregorianCalendar.HOUR_OF_DAY), ft.get(GregorianCalendar.MINUTE), ft.get(GregorianCalendar.SECOND),
ft.get(GregorianCalendar.DAY_OF_MONTH), ft.get(GregorianCalendar.MONTH) + 1, ft.get(GregorianCalendar.YEAR)));
link.serialize(Files.newOutputStream(Paths.get("test.lnk")));
}
}
package mslinks;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class ShellLink {
private ShellLinkHeader header;
public ShellLink(String file) throws IOException, ShellLinkException {
this(Paths.get(file));
}
public ShellLink(File file) throws IOException, ShellLinkException {
this(file.toPath());
}
public ShellLink(Path file) throws IOException, ShellLinkException {
this(Files.newInputStream(file));
}
public ShellLink(InputStream in) throws IOException, ShellLinkException {
this(new ByteReader(in));
}
private ShellLink(ByteReader data) throws ShellLinkException, IOException {
header = new ShellLinkHeader(data);
}
public void serialize(OutputStream out) throws IOException {
serialize(out, true);
}
public void serialize(OutputStream out, boolean littleendian) throws IOException {
ByteWriter bw = new ByteWriter(out);
if (littleendian) bw.setLittleEndian();
else bw.setBigEndian();
header.serialize(bw);
}
/* to header */
public LinkFlags getLinkFlags() { return header.getLinkFlags(); }
public FileAttributesFlags getFileAttributesFlags() { return header.getFileAttributesFlags(); }
public Filetime getCreationTime() { return header.getCreationTime(); }
public Filetime getAccessTime() { return header.getAccessTime(); }
public Filetime getWriteTime() { return header.getWriteTime(); }
public HotKeyFlags getHotKeyFlags() { return header.getHotKeyFlags(); }
public int getFileSize() { return header.getFileSize(); }
public void setFileSize(long n) { header.setFileSize(n); }
public int getIconIndex() { return header.getIconIndex(); }
public void setIconIndex(int n) { header.setIconIndex(n); }
public int getShowCommand() { return header.getShowCommand(); }
public void setShowCommand(int n) throws ShellLinkException { header.setShowCommand(n); }
}
package mslinks;
public class ShellLinkException extends Exception {
}
package mslinks;
import java.io.IOException;
public class ShellLinkHeader {
private static byte b(int i) { return (byte)i; }
private static int headerSize = 0x0000004C;
private static GUID clsid = new GUID(new byte[] {
b(0x01), b(0x14), b(0x02), b(0x00),
b(0x00), b(0x00),
b(0x00), b(0x00),
b(0xc0), b(0x00),
b(0x00), b(0x00), b(0x00), b(0x00), b(0x00), b(0x46) });
public static int SW_SHOWNORMAL = 1;
public static int SW_SHOWMAXIMIZED = 3;
public static int SW_SHOWMINNOACTIVE = 7;
private LinkFlags lf;
private FileAttributesFlags faf;
private Filetime creationTime, accessTime, writeTime;
private int fileSize, iconIndex, showCommand;
private HotKeyFlags hkf;
public ShellLinkHeader(ByteReader data) throws ShellLinkException, IOException {
int size = (int)data.read4bytes();
if (size != headerSize) {
size = ByteReader.reverse(size);
if (size != headerSize)
throw new ShellLinkException();
data.changeEndiannes();
}
GUID g = new GUID(data);
if (!g.equals(clsid))
throw new ShellLinkException();
lf = new LinkFlags(data);
faf = new FileAttributesFlags(data);
creationTime = new Filetime(data);
accessTime = new Filetime(data);
writeTime = new Filetime(data);
fileSize = (int)data.read4bytes();
iconIndex = (int)data.read4bytes();
showCommand = (int)data.read4bytes();
hkf = new HotKeyFlags(data);
data.read2bytes();
data.read8bytes();
}
public LinkFlags getLinkFlags() { return lf; }
public FileAttributesFlags getFileAttributesFlags() { return faf; }
public Filetime getCreationTime() { return creationTime; }
public Filetime getAccessTime() { return accessTime; }
public Filetime getWriteTime() { return writeTime; }
public HotKeyFlags getHotKeyFlags() { return hkf; }
public int getFileSize() { return fileSize; }
public void setFileSize(long n) { fileSize = (int)n; }
public int getIconIndex() { return iconIndex; }
public void setIconIndex(int n) { iconIndex = n; }
public int getShowCommand() { return showCommand; }
public void setShowCommand(int n) throws ShellLinkException {
if (n == SW_SHOWNORMAL || n == SW_SHOWMAXIMIZED || n == SW_SHOWMINNOACTIVE)
showCommand = n;
else
throw new ShellLinkException();
}
public void serialize(ByteWriter bw) throws IOException {
bw.write4bytes(headerSize);
clsid.serialize(bw);
lf.serialize(bw);
faf.serialize(bw);
creationTime.serialize(bw);
accessTime.serialize(bw);
writeTime.serialize(bw);
bw.write4bytes(fileSize);
bw.write4bytes(iconIndex);
bw.write4bytes(showCommand);
hkf.serialize(bw);
bw.write2bytes(0);
bw.write8bytes(0);
}
}
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment