package sos.monitor;


public class SOSPacket {
    private boolean valid;
    private int dstMod;
    private int srcMod;
    private int dstAdr;
    private int srcAdr;
    private int type;
    private int length;
    private byte data[];
    static final private int SOS_HEADER_SIZE = 8;
    
    int unsign(byte sign) {
	int pos = sign;
	if(pos < 0) {
            pos += 256;
	}
	return pos;
    }
    
    public SOSPacket(byte buf[], int size) {
	valid = true;
	if (size < SOS_HEADER_SIZE) valid = false;
	dstMod = unsign(buf[0]);
	srcMod = unsign(buf[1]);
	dstAdr = unsign(buf[2]) + unsign(buf[3]) * 256;
	srcAdr = unsign(buf[4]) + unsign(buf[5]) * 256;
	type = unsign(buf[6]);
	length = unsign(buf[7]);
	if (size < (length + 7)) valid = false;
	data = new byte[length];
	for(int i=0; i < length; i++) {
            data[i] = buf[8+i];
	}
    }
      

    public boolean isValid()
    {
	return valid;   
    }

    public int get_dstMod()
    {
	return dstMod;
    }
      
    public int get_srcMod()
    {
	return srcMod;
    }

    public int get_type()
    {
	return type;   
    }
      
    public int get_length()
    {
	return length;
    }

    public byte[] get_data()
    {
	return data;   
    }
      
      
      
      

    public StringBuffer HeaderToStringBuffer(){
	StringBuffer buf = new StringBuffer();
	if (!valid) 
	    {
		buf.append("Invalid packet");
		return buf;
	    }
	buf.append("DID: ");
	buf.append(dstMod);
	buf.append(" SID: ");
	buf.append(srcMod);
	buf.append(" DST ADDR: ");
	buf.append(dstAdr);
	buf.append(" SRC ADDR: ");
	buf.append(srcAdr);
	buf.append(" TYPE: ");
	buf.append(type);
	buf.append(" LEN: ");
	buf.append(length);
	return buf;
    }
      

    public StringBuffer ToStringBuffer(){
	StringBuffer buf = new StringBuffer();
	if (!valid)
	    {
		buf.append("Invalid packet");
		return buf;
            
	    }
	StringBuffer temp = HeaderToStringBuffer();
	buf.append(temp.toString());
	int i;
	buf.append(" DATA: ");
	for (i = 0; i < length; i++)
	    {
		int j = unsign(data[i]);
		buf.append(Integer.toHexString(j));
		buf.append(" ");
	    }
	return buf;
    }      


}


@@ -446,16 +446,17 @@
 size) %7B%0A
+%09
 %09valid =
@@ -462,16 +462,17 @@
 = true;%0A
+%09
 %09if (siz
@@ -507,16 +507,17 @@
  false;%0A
+%09
 %09dstMod 
@@ -534,16 +534,17 @@
 uf%5B0%5D);%0A
+%09
 %09srcMod 
@@ -561,16 +561,17 @@
 uf%5B1%5D);%0A
+%09
 %09dstAdr 
@@ -611,16 +611,17 @@
  * 256;%0A
+%09
 %09srcAdr 
@@ -661,16 +661,17 @@
  * 256;%0A
+%09
 %09type = 
@@ -686,16 +686,17 @@
 uf%5B6%5D);%0A
+%09
 %09length 
@@ -713,16 +713,17 @@
 uf%5B7%5D);%0A
+%09
 %09if (siz
@@ -736,17 +736,17 @@
 ength + 
-7
+8
 )) valid
@@ -755,16 +755,17 @@
  false;%0A
+%09
 %09data = 
@@ -782,16 +782,42 @@
 ength%5D;%0A
+%09%09if( valid == true ) %7B%0A%09%09
 %09for(int
@@ -841,28 +841,20 @@
  i++) %7B%0A
-            
+%09%09%09%09
 data%5Bi%5D 
@@ -870,28 +870,22 @@
 %5D;%0A%09
-%7D%0A    %7D%0A      %0A%0A    
+%09%09%7D%0A%09%09%7D%0A%09%7D%0A%0A%0A%09
 publ
@@ -901,30 +901,28 @@
 n isValid()%0A
-    
+%09
 %7B%0A
+%09
 %09return vali
@@ -927,27 +927,21 @@
 lid;   %0A
-    %7D%0A%0A    
+%09%7D%0A%0A%09
 public i
@@ -952,30 +952,28 @@
 et_dstMod()%0A
-    
+%09
 %7B%0A
+%09
 %09return dstM
@@ -976,33 +976,21 @@
 dstMod;%0A
-    %7D%0A      %0A    
+%09%7D%0A%0A%09
 public i
@@ -1005,22 +1005,20 @@
 rcMod()%0A
-    
+%09
 %7B%0A
+%09
 %09return 

fix buffer overflow bug