update all wireEncode & add packet test: CPacket

This commit is contained in:
free will committed 2021-04-07 16:37:05 +08:00
1 parent 424d182d60
commit 2bf431af9c
33 files changed
+335 -317

No files matched your search

+3 -3
View File
@@ -47,21 +47,21 @@ public class CanBePrefix implements TlvComponentBase,InitialAble, IEncodingAble
try {
// 这是一个存在性组件,所以如果值为false,则无需编码
if (!this.canBePrefix) {
return -1;
return 0;
}
int totalLength = 0;
// 编码 TLV-LENGTH
int tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvCanBePrefix));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
+4 -6
View File
@@ -50,21 +50,21 @@ public class CongestionMark implements TlvComponentBase, InitialAble, IEncodingA
// 编码 TLV-VALUE
int tmplen = encoder.prependNonNegativeInteger(this.congestionMark);
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-LENGTH
tmplen += encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen += encoder.prependVarNumber(new VlInt(TLV.TlvCongestionMark));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
@@ -90,9 +90,7 @@ public class CongestionMark implements TlvComponentBase, InitialAble, IEncodingA
}
// 读取 CongestionMark
long value = 0;
value = TLV.readNonNegativeInteger(block.getValue(), 0, block.getLength().getVlIntValue2Int());
long value = TLV.readNonNegativeInteger(block.getValue(), 0, block.getLength().getVlIntValue2Int());
if (value < 0) {
return false;
}
+3 -3
View File
@@ -50,21 +50,21 @@ public class FreshnessPeriod implements TlvComponentBase, InitialAble, IEncoding
// 编码 TLV-VALUE
int tmplen = encoder.prependNonNegativeInteger(this.freshnessPeriod);
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-LENGTH
tmplen += encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen += encoder.prependVarNumber(new VlInt(TLV.TlvFreshnessPeriod));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
+3 -3
View File
@@ -49,21 +49,21 @@ public class HopLimit implements TlvComponentBase,InitialAble, IEncodingAble {
// 编码 TLV-VALUE
int tmplen = encoder.prependNonNegativeInteger(this.hopLimit);
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-LENGTH
tmplen += encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen += encoder.prependVarNumber(new VlInt(TLV.TlvHopLimit));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
+3 -3
View File
@@ -214,7 +214,7 @@ public class Identifier implements TlvComponentBase, IEncodingAble {
// 编码 TLV-VALUE
for (int i = components.length()-1; i >=0 ; i--) {
int tmpLen = components.getElement(i).wireEncode(encoder);
if (tmpLen <= 0) {
if (tmpLen < 0) {
return -1;
}
totalLength += tmpLen;
@@ -222,7 +222,7 @@ public class Identifier implements TlvComponentBase, IEncodingAble {
// 编码 TLV-LENGTH
int tmpLen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmpLen <= 0) {
if (tmpLen < 0) {
return -1;
}
totalLength += tmpLen;
@@ -231,7 +231,7 @@ public class Identifier implements TlvComponentBase, IEncodingAble {
tmpLen = encoder.prependVarNumber(new VlInt(TLV.TlvIdentifier));
if (tmpLen <= 0) {
if (tmpLen < 0) {
return -1;
}
totalLength += tmpLen;
@@ -305,8 +305,7 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
this.identifierBlock = new Block(block);
//todo:越界问题
int length = 0;
length = this.identifierBlock.getLength().getVlIntValue2Int();
int length = this.identifierBlock.getLength().getVlIntValue2Int();
byte[] value = this.identifierBlock.getValue();
// 要求TLV-LENGTH至少为1,因为 IdentifierComponent 使用 TLV-VALUE 中的第一个字节作为Marker
@@ -321,9 +320,7 @@ public class IdentifierComponent implements TlvComponentBase, IEncodingAble {
this.marker = value[0];
// 根据不同类型的Marker,给不同的成员赋值
byte[] v = new byte[0];
v = ByteHelper.getTailBytes(value, 1);
byte[] v = ByteHelper.getTailBytes(value, 1);
if (isString()) {
this.stringValue = new String(v);
} else if (isNonNegativeInteger()||isVersionNumber()||isFragmentNumber()) {
+4 -3
View File
@@ -151,6 +151,7 @@ public class IdentifierField implements TlvComponentBase, IEncodingAble {
public int wireEncode(Encoder encoder) throws ComponentException {
try {
// 如果标识区不存在标识,则是一个无效的MIN包,直接抛出错误
// 注意,这里只能返回-1,表示已经出错,返回0则表示可以进行编码
if (this.identifiers.length() <= 0) {
return -1;
}
@@ -159,7 +160,7 @@ public class IdentifierField implements TlvComponentBase, IEncodingAble {
// 反向遍历,可以保证解码的时候正向输出
for (int i = this.identifiers.length() - 1; i >= 0; i--) {
int tmplen = this.identifiers.getElement(i).wireEncode(encoder);
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
@@ -167,7 +168,7 @@ public class IdentifierField implements TlvComponentBase, IEncodingAble {
// 编码 TLV-LENGTH
int tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
@@ -178,7 +179,7 @@ public class IdentifierField implements TlvComponentBase, IEncodingAble {
} catch (EncoderException e) {
throw new ComponentException("IdentifierField.wireEncode: " + e.getMessage());
}
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
@@ -303,14 +303,14 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
// 编码 TLV-VALUE
int tmplen = this.identifier.wireEncode(encoder);
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-LENGTH
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
@@ -321,7 +321,7 @@ public class IdentifierWrapper implements TlvComponentBase, IEncodingAble {
} catch (EncoderException e) {
throw new ComponentException("IdentifierWrapper.wireEncode: " + e.getMessage());
}
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
+37 -47
View File
@@ -44,43 +44,34 @@ public class InterestLifeTime implements TlvComponentBase,InitialAble, IEncoding
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen= 0;
try {
tmplen = encoder.prependNonNegativeInteger(this.interestLifeTime);
int totalLength = 0;
// 编码 TLV-VALUE
int tmplen = encoder.prependNonNegativeInteger(this.interestLifeTime);
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-LENGTH
tmplen += encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen += encoder.prependVarNumber(new VlInt(TLV.TlvInterestLifeTime));
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
return totalLength;
} catch (EncoderException e) {
throw new ComponentException("InterestLifeTime.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
try {
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("InterestLifeTime.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
try {
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvInterestLifeTime));
} catch (EncoderException e) {
throw new ComponentException("InterestLifeTime.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
return totalLength;
}
/**
@@ -90,23 +81,22 @@ public class InterestLifeTime implements TlvComponentBase,InitialAble, IEncoding
*/
@Override
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvInterestLifeTime))){
return false;
}
// 读取 value
long value= 0;
try {
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
// 检查 Type 是否正确
if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvInterestLifeTime))) {
return false;
}
// 读取 value
long value = TLV.readNonNegativeInteger(block.getValue(), 0, block.getLength().getVlIntValue2Int());
if (value < 0) {
return false;
}
this.setInterestLifeTime(value);
return true;
} catch (TLVException | VlIntException e) {
throw new ComponentException("InterestLifeTime.wireDecode: "+e.getMessage());
}
if(value<0){
return false;
}
this.setInterestLifeTime(value);
return true;
}
}
+3 -3
View File
@@ -117,21 +117,21 @@ public class KeyLocator implements TlvComponentBase, IEncodingAble {
int totalLength = 0;
// 编码 TLV-VALUE
int tmplen = this.identifier.wireEncode(encoder);
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-LENGTH
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvKeyLocator));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
@@ -49,21 +49,21 @@ public class LpPacketFragmentId implements TlvComponentBase,InitialAble,IEncodin
// 编码 TLV-VALUE
int tmpLen=encoder.prependNonNegativeInteger(this.id);
if(tmpLen<=0){
if(tmpLen<0){
return -1;
}
totalLength+=tmpLen;
// 编码 TLV-TYPE
tmpLen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmpLen<=0){
if(tmpLen<0){
return -1;
}
totalLength+=tmpLen;
// 编码 TLV-TYPE
tmpLen=encoder.prependVarNumber(new VlInt(TLV.TlvLpPacketFragmentId));
if(tmpLen<=0){
if(tmpLen<0){
return -1;
}
totalLength+=tmpLen;
@@ -49,21 +49,21 @@ public class LpPacketFragmentNum implements TlvComponentBase,InitialAble, IEncod
// 编码 TLV-VALUE
int tmpLen=encoder.prependNonNegativeInteger(this.fragmentNum);
if(tmpLen<=0){
if(tmpLen<0){
return -1;
}
totalLength+=tmpLen;
// 编码 TLV-TYPE
tmpLen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmpLen<=0){
if(tmpLen<0){
return -1;
}
totalLength+=tmpLen;
// 编码 TLV-TYPE
tmpLen=encoder.prependVarNumber(new VlInt(TLV.TlvLpPacketFragmentNum));
if(tmpLen<=0){
if(tmpLen<0){
return -1;
}
totalLength+=tmpLen;
@@ -49,21 +49,21 @@ public class LpPacketFragmentSeq implements TlvComponentBase,InitialAble, IEncod
// 编码 TLV-VALUE
int tmpLen=encoder.prependNonNegativeInteger(this.fragmentSeq);
if(tmpLen<=0){
if(tmpLen<0){
return -1;
}
totalLength+=tmpLen;
// 编码 TLV-TYPE
tmpLen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmpLen<=0){
if(tmpLen<0){
return -1;
}
totalLength+=tmpLen;
// 编码 TLV-TYPE
tmpLen=encoder.prependVarNumber(new VlInt(TLV.TlvLpPacketFragmentSeq));
if(tmpLen<=0){
if(tmpLen<0){
return -1;
}
totalLength+=tmpLen;
+5 -5
View File
@@ -63,35 +63,35 @@ public class LpPacketHeader implements TlvComponentBase, IEncodingAble {
// 编码 TLV-VALUE
// seq
int tmpLen=this.lpPacketFragmentSeq.wireEncode(encoder);
if(tmpLen<=0){
if(tmpLen<0){
return -1;
}
totalLength+=tmpLen;
// num
tmpLen=this.lpPacketFragmentNum.wireEncode(encoder);
if(tmpLen<=0){
if(tmpLen<0){
return -1;
}
totalLength+=tmpLen;
// id
tmpLen=this.lpPacketFragmentId.wireEncode(encoder);
if(tmpLen<=0){
if(tmpLen<0){
return -1;
}
totalLength+=tmpLen;
// 编码 TLV-LENGTH
tmpLen=encoder.prependVarNumber(new VlInt(totalLength));
if(tmpLen<=0){
if(tmpLen<0){
return -1;
}
totalLength+=tmpLen;
// 编码 TLV-TYPE
tmpLen=encoder.prependVarNumber(new VlInt(TLV.TlvLpPacketHeader));
if(tmpLen<=0){
if(tmpLen<0){
return -1;
}
totalLength+=tmpLen;
+3 -3
View File
@@ -48,21 +48,21 @@ public class MustBeRefresh implements TlvComponentBase, InitialAble, IEncodingAb
try {
// 这是一个存在性组件,所以如果值为false,则无需编码
if (!this.mustBeRefresh) {
return -1;
return 0;
}
int totalLength = 0;
// 编码 TLV-LENGTH
int tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvMustBeRefresh));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
@@ -78,36 +78,30 @@ public class MutableDangerousField implements TlvComponentBase, IEncodingAble {
try {
// 如果可变非受保护区没有任何子 TLV,则可以去掉可变非受保护区 TLV
if (this.blocks.length() == 0) {
return -1;
return 0;
}
int totalLength = 0;
// 编码TLV-VALUE
// 反向遍历,可以保证解码的时候正向输出
for (int i = this.blocks.length() - 1; i >= 0; i--) {
int tmplen = 0;
tmplen = encoder.prependBlock(this.blocks.getBlock(i));
if (tmplen <= 0) {
int tmplen = encoder.prependBlock(this.blocks.getBlock(i));
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
}
// 编码 TLV-LENGTH
int tmplen = 0;
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
int tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
try {
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvMutableDangerousField));
} catch (EncoderException e) {
throw new ComponentException("MutableDangerousField.wireEncode: " + e.getMessage());
}
if (tmplen <= 0) {
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvMutableDangerousField));
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
+30 -39
View File
@@ -26,53 +26,44 @@ public class MutableField implements TlvComponentBase, IEncodingAble {
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength = 0;
// 编码 TLV—VALUE
int tmplen = 0;
try {
tmplen = this.mutableDangerousField.wireEncode(encoder);
} catch (ComponentException e) {
throw new ComponentException("MutableField.wireEncode: " + e.getMessage());
}
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
tmplen = this.mutableProtectField.wireEncode(encoder);
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
int totalLength = 0;
// 如果可变保护区和可变非受保护区均为空,则可以去掉可变区
if (totalLength <= 0) {
return -1;
}
// 编码 TLV—VALUE
int tmplen = this.mutableDangerousField.wireEncode(encoder);
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
tmplen = this.mutableProtectField.wireEncode(encoder);
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-LENGTH
try {
// 如果可变保护区和可变非受保护区均为空,则可以去掉可变区
if (totalLength < 0) {
return -1;
}
// 编码 TLV-LENGTH
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("MutableField.wireEncode: " + e.getMessage());
}
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
try {
// 编码 TLV-TYPE
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvMutableField));
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
return totalLength;
} catch (EncoderException e) {
throw new ComponentException("MutableField.wireEncode: " + e.getMessage());
}
if (tmplen <= 0) {
return -1;
}
totalLength += tmplen;
return totalLength;
}
/**
@@ -109,7 +100,7 @@ public class MutableField implements TlvComponentBase, IEncodingAble {
}
}
return false;
return true;
} catch (BlockException e) {
throw new ComponentException("MutableField.wireDecode: " + e.getMessage());
}
@@ -76,44 +76,32 @@ public class MutableProtectField implements TlvComponentBase, IEncodingAble {
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
try {
// 如果没有任何子 TLV,则可以去掉它
if (this.blocks.length() == 0) {
return -1;
return 0;
}
int totalLength = 0;
// 编码TLV-VALUE
// 反向遍历,可以保证解码的时候正向输出
for (int i = this.blocks.length() - 1; i >= 0; i--) {
int tmplen = 0;
tmplen = encoder.prependBlock(this.blocks.getBlock(i));
if (tmplen <= 0) {
int tmplen = encoder.prependBlock(this.blocks.getBlock(i));
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
}
// 编码 TLV-LENGTH
int tmplen = 0;
try {
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("MutableProtectField.wireEncode: " + e.getMessage());
}
if (tmplen <= 0) {
int tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
try {
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvMutableProtectField));
} catch (EncoderException e) {
throw new ComponentException("MutableProtectField.wireEncode: " + e.getMessage());
}
if (tmplen <= 0) {
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvMutableProtectField));
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
+37 -47
View File
@@ -48,43 +48,34 @@ public class NackHeader implements TlvComponentBase,InitialAble, IEncodingAble {
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen= 0;
try {
tmplen = encoder.prependNonNegativeInteger(this.nackReason);
int totalLength = 0;
// 编码 TLV-VALUE
int tmplen = encoder.prependNonNegativeInteger(this.nackReason);
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-LENGTH
tmplen += encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen += encoder.prependVarNumber(new VlInt(TLV.TlvNackHeader));
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
return totalLength;
} catch (EncoderException e) {
throw new ComponentException("NackHeader.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
try {
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("NackHeader.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
try {
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvNackHeader));
} catch (EncoderException e) {
throw new ComponentException("NackHeader.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
return totalLength;
}
/**
@@ -94,23 +85,22 @@ public class NackHeader implements TlvComponentBase,InitialAble, IEncodingAble {
*/
@Override
public boolean wireDecode(Block block) throws ComponentException {
// 检查 Type 是否正确
if(!TLV.expectType(block.getType(),new VlInt(TLV.TlvNackHeader))){
return false;
}
// 读取 value
long value= 0;
try {
value = TLV.readNonNegativeInteger(block.getValue(),0,block.getLength().getVlIntValue2Int());
// 检查 Type 是否正确
if (!TLV.expectType(block.getType(), new VlInt(TLV.TlvNackHeader))) {
return false;
}
// 读取 value
long value = TLV.readNonNegativeInteger(block.getValue(), 0, block.getLength().getVlIntValue2Int());
if (value < 0) {
return false;
}
this.setNackReason(value);
return true;
} catch (TLVException | VlIntException e) {
throw new ComponentException("NackHeader.wireDecode: "+e.getMessage());
}
if(value<0){
return false;
}
this.setNackReason(value);
return true;
}
}
+3 -3
View File
@@ -60,21 +60,21 @@ public class Nonce implements TlvComponentBase, InitialAble, IEncodingAble {
// 编码 TLV-VALUE
int tmplen = encoder.prependNonNegativeInteger(this.nonce);
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-LENGTH
tmplen += encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen += encoder.prependVarNumber(new VlInt(TLV.TlvNonce));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
+24 -33
View File
@@ -74,43 +74,34 @@ public class Payload implements TlvComponentBase,InitialAble, IEncodingAble {
*/
@Override
public int wireEncode(Encoder encoder) throws ComponentException {
int totalLength=0;
// 编码 TLV-VALUE
int tmplen= 0;
try {
tmplen = encoder.prependByteArray(this.value,new SizeT(this.value.length));
int totalLength = 0;
// 编码 TLV-VALUE
int tmplen = encoder.prependByteArray(this.value, new SizeT(this.value.length));
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-LENGTH
tmplen += encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen += encoder.prependVarNumber(new VlInt(TLV.TlvPayload));
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
return totalLength;
} catch (EncoderException e) {
throw new ComponentException("Payload.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-LENGTH
try {
tmplen+=encoder.prependVarNumber(new VlInt(totalLength));
} catch (EncoderException e) {
throw new ComponentException("Payload.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
// 编码 TLV-TYPE
try {
tmplen+=encoder.prependVarNumber(new VlInt(TLV.TlvPayload));
} catch (EncoderException e) {
throw new ComponentException("Payload.wireEncode: "+e.getMessage());
}
if(tmplen<=0){
return -1;
}
totalLength+=tmplen;
return totalLength;
}
@Override
+6 -10
View File
@@ -78,33 +78,30 @@ public class ReadOnlyField implements TlvComponentBase, IEncodingAble {
try {
// 如果只读区没有任何子 TLV,则可以去掉只读区 TLV
if (this.blocks.length() == 0) {
return -1;
return 0;
}
int totalLength = 0;
// 编码TLV-VALUE
// 反向遍历,可以保证解码的时候正向输出
for (int i = this.blocks.length() - 1; i >= 0; i--) {
int tmplen = 0;
tmplen = encoder.prependBlock(this.blocks.getBlock(i));
if (tmplen <= 0) {
int tmplen = encoder.prependBlock(this.blocks.getBlock(i));
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
}
// 编码 TLV-LENGTH
int tmplen = 0;
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
int tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvReadOnlyField));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
@@ -130,7 +127,6 @@ public class ReadOnlyField implements TlvComponentBase, IEncodingAble {
}
// 解析子 TLV
if (!block.parseSubElements()) {
return false;
}
+5 -5
View File
@@ -121,7 +121,7 @@ public class SignatureField implements TlvComponentBase, IEncodingAble {
try{
// 如果签名区没有签名,则可以直接去掉签名区 TLV,因为它不是必须的
if (signatures.length() == 0) {
return -1;
return 0;
}
int totalLength = 0;
// 编码 TLV-VALUE
@@ -129,12 +129,12 @@ public class SignatureField implements TlvComponentBase, IEncodingAble {
for (int i = signatures.length() - 1; i >= 0; i--) {
Signature newSignature = signatures.getElement(i);
int tmplen = newSignature.getSigValue().wireEncode(encoder);
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
tmplen = newSignature.getSigInfo().wireEncode(encoder);
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
@@ -142,14 +142,14 @@ public class SignatureField implements TlvComponentBase, IEncodingAble {
// 编码签名区 TLV-LENGTH
int tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码签名区 TLV-TYPE
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvSignatureField));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
+4 -4
View File
@@ -115,27 +115,27 @@ public class SignatureInfo implements TlvComponentBase, IEncodingAble {
if (this.keyLocator.isValid()) {
// KeyLocator 是可选的,如果当前 SignatureInfo 对象有一个有效的 KeyLocator,则对其进行编码
int tmplen = this.keyLocator.wireEncode(encoder);
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
}
int tmplen = this.signatureType.wireEncode(encoder);
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-LENGTH
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvSignatureInfo));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
+3 -4
View File
@@ -113,22 +113,21 @@ public class SignatureType implements TlvComponentBase, IEncodingAble {
// 编码 TLV-VALUE
int tmplen = encoder.prependNonNegativeInteger(this.value.getSignatureAlgorithm());
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-LENGTH
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvSignatureType));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
+3 -3
View File
@@ -77,21 +77,21 @@ public class SignatureValue implements TlvComponentBase, IEncodingAble {
int totalLength = 0;
// 编码 TLV-VALUE
int tmplen = encoder.prependByteArray(this.value, new SizeT(this.value.length));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-LENGTH
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码 TLV-TYPE
tmplen = encoder.prependVarNumber(new VlInt(TLV.TlvSignatureValue));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
+7 -3
View File
@@ -22,6 +22,10 @@ public class TTL implements IEncodingAble {
public TTL() {
}
public TTL(long ttl){
this.setTtl(ttl);
}
public TTL(Block block) throws ComponentException {
buildTTLByBlock(block);
}
@@ -66,21 +70,21 @@ public class TTL implements IEncodingAble {
int totalLength = 0;
// 编码 TLV-VALUE
int tmpLen = encoder.prependNonNegativeInteger(this.ttl);
if (tmpLen <= 0) {
if (tmpLen < 0) {
return -1;
}
totalLength += tmpLen;
// 编码 TLV-LENGTH
tmpLen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmpLen <= 0) {
if (tmpLen < 0) {
return -1;
}
totalLength += tmpLen;
// 编码 TLV-TYPE
tmpLen = encoder.prependVarNumber(new VlInt(TLV.TlvTTL));
if (tmpLen <= 0) {
if (tmpLen < 0) {
return -1;
}
totalLength += tmpLen;
+13 -22
View File
@@ -8,13 +8,12 @@ package encoding;
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class Encoder {
//TODO 便于测试暂时先将private属性改成public了
public byte[] buffer;
public int left; // 左指针 => 指向 Prepend* 操作下一个可以插入的位置
public int right; // 右指针 => 指向 Append* 操作下一个可以插入的位置
private int left; // 左指针 => 指向 Prepend* 操作下一个可以插入的位置
private int right; // 右指针 => 指向 Append* 操作下一个可以插入的位置
public boolean isInitial; // 是否初始化
public boolean isEstimator;// 是否仅测量编码后的长度,而不实际进行编码
public SizeT totalReserve; // 编码缓存数组的大小
private boolean isEstimator;// 是否仅测量编码后的长度,而不实际进行编码
private SizeT totalReserve; // 编码缓存数组的大小
public static final int MaxPacketSize = 8800;
@@ -184,30 +183,22 @@ public class Encoder {
* @return 返回插入成功的字节数
*/
public int prependByteArray(byte[] array, SizeT size) throws EncoderException {
if (check(size) && (size.compareTo(array.length) <= 0)) {
if (this.isEstimator) {
this.left -= array.length;
try {
try {
if (check(size) && (size.compareTo(array.length) <= 0)) {
if (this.isEstimator) {
this.left -= array.length;
return size.getVlIntValue2Int();
} catch (VlIntException e) {
throw new EncoderException("prependByteArray: " + e.getMessage());
}
} else {
try {
} else {
System.arraycopy(array, 0, this.buffer,
this.left - size.getVlIntValue2Int() + 1, size.getVlIntValue2Int());
} catch (VlIntException e) {
throw new EncoderException("prependByteArray: " + e.getMessage());
}
this.left -= array.length;
try {
this.left -= array.length;
return size.getVlIntValue2Int();
} catch (VlIntException e) {
throw new EncoderException("prependByteArray: " + e.getMessage());
}
}
return -1;
} catch (VlIntException e) {
throw new EncoderException("prependByteArray: " + e.getMessage());
}
return -1;
}
/**
+16 -3
View File
@@ -16,7 +16,10 @@ import encoding.*;
// @Description:
// 1. 包格式定义如下:
// CPacket = 5 TLV-LENGTH
// { CommonIdentifier } => 标识区
// { => 标识区
// SrcIdentifier => 源标识
// DstIdentifier => 目的标识
// }
// { Signature } => 签名区
// { => 只读区
// <Payload>
@@ -101,6 +104,14 @@ public class CPacket implements InteractWithField, IEncodingAble {
}
}
/**
* 获取payload中的value
* @return
*/
public byte[] getValue(){
return this.payload.getValue();
}
/**
* 将 CPacket 的各项属性填充到 MINPacket 中定义的对应分区当中
*
@@ -117,6 +128,7 @@ public class CPacket implements InteractWithField, IEncodingAble {
this.minPacket.mutableField.mutableDangerousField.clearBlocks();
Block block = new SelfEncodingBase().selfWireEncode(this.ttl);
if (block == null) {
System.out.println("err 1");
return false;
}
this.minPacket.mutableField.mutableDangerousField.addBlock(block);
@@ -126,6 +138,7 @@ public class CPacket implements InteractWithField, IEncodingAble {
this.minPacket.readOnlyField.clearBlocks();
block = new SelfEncodingBase().selfWireEncode(this.payload);
if (block == null) {
System.out.println("err 2");
return false;
}
this.minPacket.readOnlyField.addBlock(block);
@@ -162,7 +175,7 @@ public class CPacket implements InteractWithField, IEncodingAble {
if (payloadBlock == null) {
return false;
}
if (this.payload.wireDecode(payloadBlock)) {
if (!this.payload.wireDecode(payloadBlock)) {
return false;
}
@@ -171,7 +184,7 @@ public class CPacket implements InteractWithField, IEncodingAble {
if (ttlBlock == null) {
return false;
}
if (this.ttl.wireDecode(ttlBlock)) {
if (!this.ttl.wireDecode(ttlBlock)) {
return false;
}
+4 -4
View File
@@ -30,27 +30,27 @@ public class LpPacket implements IEncodingAble {
// 编码 TLV-VALUE
// LpHeader
int tmpLen = this.lpPacketHeader.wireEncode(encoder);
if(tmpLen<=0){
if(tmpLen<0){
return -1;
}
totalLength+=tmpLen;
// Fragment
tmpLen = this.payload.wireEncode(encoder);
if(tmpLen<=0){
if(tmpLen<0){
return -1;
}
totalLength+=tmpLen;
// 编码 TLV-LENGTH
tmpLen = encoder.prependVarNumber(new VlInt(totalLength));
if(tmpLen<=0){
if(tmpLen<0){
return -1;
}
totalLength+=tmpLen;
// 编码 TLV-TYPE
tmpLen = encoder.prependVarNumber(new VlInt(TLV.TlvLpPacket));
if(tmpLen<=0){
if(tmpLen<0){
return -1;
}
totalLength+=tmpLen;
+6 -7
View File
@@ -45,42 +45,42 @@ public class MINPacket implements IEncodingAble,IMINPacket {
// 可变区
int tmplen = this.mutableField.wireEncode(encoder);
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 只读区
tmplen = this.readOnlyField.wireEncode(encoder);
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 签名区
tmplen = this.signatureField.wireEncode(encoder);
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 标识区
tmplen = this.identifierField.wireEncode(encoder);
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码包 TLV-LENGTH
tmplen = encoder.prependVarNumber(new VlInt(totalLength));
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
// 编码包 TLV-TYPE
tmplen = encoder.prependVarNumber(this.packetType);
if (tmplen <= 0) {
if (tmplen < 0) {
return -1;
}
totalLength += tmplen;
@@ -113,7 +113,6 @@ public class MINPacket implements IEncodingAble,IMINPacket {
return false;
}
// 标记,用来记录是否满足必要条件
boolean existIdentifier = false;