Skip to content
Snippets Groups Projects
Commit 1fcda47a authored by BlackOverlord's avatar BlackOverlord
Browse files

endianness refactoring

parent b0c4f90b
No related branches found
No related tags found
No related merge requests found
......@@ -2,11 +2,12 @@ package io;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteOrder;
public class ByteReader extends InputStream {
private static boolean le = ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN);
private InputStream stream;
private Endianness end = Endianness.LITTLE_ENDIAN;
private InputStream stream;
private int pos = 0;
......@@ -14,36 +15,15 @@ public class ByteReader extends InputStream {
stream = in;
}
public ByteReader setBigEndian() {
end = Endianness.BIG_ENDIAN;
return this;
}
public ByteReader setLittleEndian() {
end = Endianness.LITTLE_ENDIAN;
return this;
public int getPosition() {
return pos;
}
public ByteReader changeEndiannes() {
if (isLitteEndian())
setBigEndian();
else
setLittleEndian();
le = !le;
return this;
}
public boolean isBigEndian() {
return end == Endianness.BIG_ENDIAN;
}
public boolean isLitteEndian() {
return end == Endianness.LITTLE_ENDIAN;
}
public int getPosition() {
return pos;
}
public boolean seek(int n) throws IOException {
if (n <= 0) return false;
for (int i=0; i<n; i++)
......@@ -60,7 +40,7 @@ public class ByteReader extends InputStream {
public long read2bytes() throws IOException {
long b0 = read();
long b1 = read();
if (isLitteEndian())
if (le)
return b0 | (b1 << 8);
else
return b1 | (b0 << 8);
......@@ -70,7 +50,7 @@ public class ByteReader extends InputStream {
long b0 = read();
long b1 = read();
long b2 = read();
if (isLitteEndian())
if (le)
return b0 | (b1 << 8) | (b2 << 16);
else
return b2 | (b1 << 8) | (b0 << 16);
......@@ -81,7 +61,7 @@ public class ByteReader extends InputStream {
long b1 = read();
long b2 = read();
long b3 = read();
if (isLitteEndian())
if (le)
return b0 | (b1 << 8) | (b2 << 16) | (b3 << 24);
else
return b3 | (b2 << 8) | (b1 << 16) | (b0 << 24);
......@@ -93,7 +73,7 @@ public class ByteReader extends InputStream {
long b2 = read();
long b3 = read();
long b4 = read();
if (isLitteEndian())
if (le)
return b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) | (b4 << 32);
else
return b4 | (b3 << 8) | (b2 << 16) | (b1 << 24) | (b0 << 32);
......@@ -106,7 +86,7 @@ public class ByteReader extends InputStream {
long b3 = read();
long b4 = read();
long b5 = read();
if (isLitteEndian())
if (le)
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);
......@@ -120,7 +100,7 @@ public class ByteReader extends InputStream {
long b4 = read();
long b5 = read();
long b6 = read();
if (isLitteEndian())
if (le)
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);
......@@ -135,7 +115,7 @@ public class ByteReader extends InputStream {
long b5 = read();
long b6 = read();
long b7 = read();
if (isLitteEndian())
if (le)
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);
......@@ -185,8 +165,4 @@ public class ByteReader extends InputStream {
buf[i] = (char)read2bytes();
return new String(buf);
}
}
enum Endianness {
BIG_ENDIAN, LITTLE_ENDIAN
}
\ No newline at end of file
......@@ -2,11 +2,12 @@ package io;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteOrder;
public class ByteWriter extends OutputStream {
private static boolean le = ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN);
private OutputStream stream;
private Endianness end = Endianness.LITTLE_ENDIAN;
private int pos = 0;
......@@ -14,36 +15,15 @@ public class ByteWriter extends OutputStream {
stream = out;
}
public ByteWriter setBigEndian() {
end = Endianness.BIG_ENDIAN;
return this;
}
public ByteWriter setLittleEndian() {
end = Endianness.LITTLE_ENDIAN;
return this;
public int getPosition() {
return pos;
}
public ByteWriter changeEndiannes() {
if (isLitteEndian())
setBigEndian();
else
setLittleEndian();
le = !le;
return this;
}
public boolean isBigEndian() {
return end == Endianness.BIG_ENDIAN;
}
public boolean isLitteEndian() {
return end == Endianness.LITTLE_ENDIAN;
}
public int getPosition() {
return pos;
}
@Override
public void write(int b) throws IOException {
pos++;
......@@ -57,7 +37,7 @@ public class ByteWriter extends OutputStream {
public void write2bytes(long n) throws IOException {
long b0 = n & 0xff;
long b1 = (n & 0xff00) >> 8;
if (isLitteEndian()) {
if (le) {
write(b0); write(b1);
} else {
write(b1); write(b0);
......@@ -68,7 +48,7 @@ public class ByteWriter extends OutputStream {
long b0 = n & 0xff;
long b1 = (n & 0xff00) >> 8;
long b2 = (n & 0xff0000) >> 16;
if (isLitteEndian()) {
if (le) {
write(b0); write(b1); write(b2);
} else {
write(b2); write(b1); write(b0);
......@@ -80,7 +60,7 @@ public class ByteWriter extends OutputStream {
long b1 = (n & 0xff00) >> 8;
long b2 = (n & 0xff0000) >> 16;
long b3 = (n & 0xff000000) >>> 24;
if (isLitteEndian()) {
if (le) {
write(b0); write(b1); write(b2); write(b3);
} else {
write(b3); write(b2); write(b1); write(b0);
......@@ -93,7 +73,7 @@ public class ByteWriter extends OutputStream {
long b2 = (n & 0xff0000) >> 16;
long b3 = (n & 0xff000000) >>> 24;
long b4 = (n & 0xff00000000L) >> 32;
if (isLitteEndian()) {
if (le) {
write(b0); write(b1); write(b2); write(b3); write(b4);
} else {
write(b4); write(b3); write(b2); write(b1); write(b0);
......@@ -107,7 +87,7 @@ public class ByteWriter extends OutputStream {
long b3 = (n & 0xff000000) >>> 24;
long b4 = (n & 0xff00000000L) >> 32;
long b5 = (n & 0xff0000000000L) >> 40;
if (isLitteEndian()) {
if (le) {
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);
......@@ -122,7 +102,7 @@ public class ByteWriter extends OutputStream {
long b4 = (n & 0xff00000000L) >> 32;
long b5 = (n & 0xff0000000000L) >> 40;
long b6 = (n & 0xff000000000000L) >> 48;
if (isLitteEndian()) {
if (le) {
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);
......@@ -138,7 +118,7 @@ public class ByteWriter extends OutputStream {
long b5 = (n & 0xff0000000000L) >> 40;
long b6 = (n & 0xff000000000000L) >> 48;
long b7 = (n & 0xff00000000000000L) >>> 56;
if (isLitteEndian()) {
if (le) {
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);
......
......@@ -69,12 +69,12 @@ public class LinkInfo implements Serializable {
byte[] vid_b = null, localBasePath_b = null, cnrlink_b = null, commonPathSuffix_b = null;
if (lif.hasVolumeIDAndLocalBasePath()) {
vid_b = toByteArray(vid, bw.isLitteEndian());
vid_b = toByteArray(vid);
localBasePath_b = localBasePath.getBytes();
commonPathSuffix_b = new byte[0];
}
if (lif.hasCommonNetworkRelativeLinkAndPathSuffix()) {
cnrlink_b = toByteArray(cnrlink, bw.isLitteEndian());
cnrlink_b = toByteArray(cnrlink);
commonPathSuffix_b = commonPathSuffix.getBytes();
}
......@@ -157,11 +157,9 @@ public class LinkInfo implements Serializable {
bw.write(0);
}
private byte[] toByteArray(Serializable o, boolean le) throws IOException {
private byte[] toByteArray(Serializable o) throws IOException {
ByteArrayOutputStream arr = new ByteArrayOutputStream();
ByteWriter bt = new ByteWriter(arr);
if (le) bt.setLittleEndian();
else bt.setBigEndian();
o.serialize(bt);
return arr.toByteArray();
}
......
......@@ -48,8 +48,6 @@ public class LinkTargetIDList extends LinkedList<ItemID> implements Serializable
for (ItemID j : this) {
ByteArrayOutputStream ba = new ByteArrayOutputStream();
ByteWriter w = new ByteWriter(ba);
if (bw.isLitteEndian()) w.setLittleEndian();
else w.setBigEndian();
j.serialize(w);
b[i++] = ba.toByteArray();
......
......@@ -3,7 +3,7 @@ package mslinks;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
public static void main(String[] args) throws IOException {
ShellLink sl = ShellLink.createLink("pause.bat")
.setWorkingDir("..");
sl.getConsoleData()
......
......@@ -38,7 +38,6 @@ public class ShellLink {
}};
private boolean le;
private ShellLinkHeader header;
private LinkTargetIDList idlist;
private LinkInfo info;
......@@ -48,7 +47,6 @@ public class ShellLink {
private Path linkFileSource;
private ShellLink() {
le = true;
header = new ShellLinkHeader();
header.getLinkFlags().setIsUnicode();
}
......@@ -103,15 +101,11 @@ public class ShellLink {
e.printStackTrace();
}
}
le = data.isLitteEndian();
}
private void serialize(OutputStream out) throws IOException {
LinkFlags lf = header.getLinkFlags();
ByteWriter bw = new ByteWriter(out);
if (le) bw.setLittleEndian();
else bw.setBigEndian();
header.serialize(bw);
if (lf.hasLinkTargetIDList())
idlist.serialize(bw);
......@@ -226,13 +220,8 @@ public class ShellLink {
cd.setLanguage(s);
return this;
}
public ShellLink saveTo(String path) throws IOException {
return saveTo(path, le);
}
public ShellLink saveTo(String path, boolean littleendiand) throws IOException {
le = littleendiand;
linkFileSource = Paths.get(path).toAbsolutePath().normalize();
if (Files.isDirectory(linkFileSource))
throw new IOException("path is directory!");
......
......@@ -2,7 +2,6 @@ package mslinks;
import io.ByteReader;
import io.ByteWriter;
import io.Bytes;
import java.io.IOException;
......@@ -38,13 +37,8 @@ public class ShellLinkHeader implements Serializable {
public ShellLinkHeader(ByteReader data) throws ShellLinkException, IOException {
int size = (int)data.read4bytes();
if (size != headerSize) {
size = Bytes.reverse(size);
if (size != headerSize)
throw new ShellLinkException();
data.changeEndiannes();
}
if (size != headerSize)
throw new ShellLinkException();
GUID g = new GUID(data);
if (!g.equals(clsid))
throw new ShellLinkException();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment