Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed pdata-length check in constructor #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 31 additions & 38 deletions src/flashsystem/S1Packet.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public S1Packet(byte[] pdata) throws X10FlashException {
validate();
return;
}
if (pdata.length==0) {
// check minimum length
if (pdata.length<13) {
validate();
return;
}
Expand All @@ -39,7 +40,7 @@ public S1Packet(byte[] pdata) throws X10FlashException {
System.arraycopy(pdata, 8, datalen, 0, 4);
hdr = pdata[12];
if (getDataLength()>65553)
throw new X10FlashException("Incorect read packet. Bad Data length");
throw new X10FlashException("Incorrect read packet. Bad Data length");
data = new byte[getDataLength()];
int totransfer=pdata.length-13;
if (totransfer>getDataLength()) totransfer=getDataLength();
Expand All @@ -48,10 +49,7 @@ public S1Packet(byte[] pdata) throws X10FlashException {
if (pdata.length>13+totransfer) {
System.arraycopy(pdata, 13+totransfer, crc32, 0, 4);
finalized=true;
if (BytesUtil.getLong(calculatedCRC32())!=BytesUtil.getLong(crc32))
throw new X10FlashException("S1 Data CRC32 Error");
if (calculateHeaderCkSum()!=hdr)
throw new X10FlashException("S1 Header checksum Error");
validate();
}
}
catch (Exception e) {
Expand Down Expand Up @@ -112,13 +110,12 @@ public void setFlags(boolean flag1, boolean flag2, boolean ongoing) {
flags = BytesUtil.getBytesWord(getFlag(flag1,flag2,ongoing), 4);
}

private int getFlag(boolean flag1, boolean flag2, boolean ongoing)
{
boolean flag = !flag1;
byte byte0 = (byte)(flag2 ? 2 : 0);
byte byte1 = (byte)(ongoing ? 4 : 0);
return (((byte)(flag ? 1 : 0))) | byte0 | byte1;
}
private int getFlag(boolean flag1, boolean flag2, boolean ongoing) {
boolean flag = !flag1;
byte byte0 = (byte)(flag2 ? 2 : 0);
byte byte1 = (byte)(ongoing ? 4 : 0);
return (((byte)(flag ? 1 : 0))) | byte0 | byte1;
}

public int getFlags() {
return BytesUtil.getInt(flags);
Expand Down Expand Up @@ -180,7 +177,7 @@ public void addData(byte[] datachunk) throws X10FlashException {
}

public String toString() {
return "CommandID : "+getCommand()+" / Flags : "+this.getFlagsAsString()+" / Data length : "+this.getDataLength()+" / Data CRC32 : "+HexDump.toHex(crc32);
return "CommandID : "+getCommand()+" / Flags : "+this.getFlagsAsString()+" / Data length : "+this.getDataLength()+" / Data CRC32 : "+HexDump.toHex(crc32);
}

public byte[] calculatedCRC32() {
Expand All @@ -192,30 +189,27 @@ public boolean hasMoreToRead() {
return !finalized;
}

public byte calculateHeaderCkSum()
{
byte header[] = new byte[12];
System.arraycopy(cmd, 0, header, 0, 4);
System.arraycopy(flags, 0, header, 4, 4);
System.arraycopy(datalen, 0, header, 8, 4);
byte result = calcSum(header);
header = null;
return result;
}

private byte calcSum(byte paramArray[])
{
byte byte0 = 0;
if(paramArray.length < 12)
return 0;
for(int i = 0; i < 12; i++)
byte0 ^= paramArray[i];

byte0 += 7;
return byte0;
}
public byte calculateHeaderCkSum() {
byte header[] = new byte[12];
System.arraycopy(cmd, 0, header, 0, 4);
System.arraycopy(flags, 0, header, 4, 4);
System.arraycopy(datalen, 0, header, 8, 4);
byte result = calcSum(header);
header = null;
return result;
}

private byte calcSum(byte paramArray[]) {
byte byte0 = 0;
if(paramArray.length < 12)
return 0;
for(int i = 0; i < 12; i++)
byte0 ^= paramArray[i];
byte0 += 7;
return byte0;
}

public void saveDataAs(String file) {
public void saveDataAs(String file) {
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
Expand All @@ -236,5 +230,4 @@ public byte[] getHeader() {
array[12] = hdr;
return array;
}

}