writing tlv & block ...

This commit is contained in:
free will
2021-03-02 18:24:10 +08:00
parent 12a99b3655
commit b07bf8a9f9
5 changed files with 570 additions and 17 deletions
+188 -1
View File
@@ -1,5 +1,192 @@
package encoding;
public class Block {
import util.ByteHelper;
import java.util.LinkedList;
import java.util.List;
/*
* @Author: Wang Feng
* @Description:
* @Version: 1.0.0
* @Date: 14:19 2021/3/2
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
// TLV 组件容器
class ElementContainer{
private List<Block> ElementContainer;
public ElementContainer(){
ElementContainer= new LinkedList<>();
}
public int length(){
return ElementContainer.size();
}
}
public class Block {
private VlInt tlvType; // TLV-TYPE
private VlInt length; // TLV-LENGTH
private byte[] value; // TLV-VALUE
private ElementContainer elements; // TLV-sub-elements
private byte[] raw; // TLV编码后的字节数组
/**
* 从一个包含TLV的字节数组中构造一个 Block 对象
* @param buffer
* @param verifyLength 是否要验证TLV的长度是否匹配,如果true,
* 则会对比解析到的 TLV-LENGTH 与 TLV-VALUE的实际长度是否匹配
* @return
*/
public Block createBlockByBuffer(byte[] buffer,boolean verifyLength){
if(buffer.length==0){
return null;
}
// 读取type
VlInt tlvType=TLV.readType(buffer,new VlInt(0));
if(tlvType==null){
return null;
}
// 读取length
VlInt tlvLength=TLV.readVarNumber(buffer,new VlInt(tlvType.SizeOfVarNumber()));
if(tlvLength==null){
return null;
}
return createBlockByTypeLengthBuffer(tlvType,tlvLength,
ByteHelper.getLenBytes(buffer,new VlInt(tlvType.SizeOfVarNumber()+tlvLength.SizeOfVarNumber()),
buffer.length-(tlvType.SizeOfVarNumber()+tlvLength.SizeOfVarNumber())),verifyLength);
}
/**
* 传入 T、L、V 构造一个 TLV Block
* @param tlvType
* @param tlvLength
* @param buffer
* @param verifyLength
* @return
*/
public Block createBlockByTypeLengthBuffer(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength){
return new Block(tlvType,tlvLength,buffer,verifyLength);
}
/**
* 构造函数
* @param tlvType
* @param tlvLength
* @param buffer
* @param verifyLength
*/
public Block(VlInt tlvType,VlInt tlvLength,byte[] buffer,boolean verifyLength){
this.tlvType=tlvType;
this.length=tlvLength;
// 检查长度是否正确
if(verifyLength && (!this.length.isEqual(buffer.length))){
//todo: 抛出异常
}
// todo;设置 Raw
// todo 设置Value
}
public VlInt getTlvType() {
return tlvType;
}
public void setTlvType(VlInt tlvType) {
this.tlvType = tlvType;
}
public VlInt getLength() {
return length;
}
public void setLength(VlInt length) {
this.length = length;
}
public byte[] getValue() {
return value;
}
public void setValue(byte[] value) {
this.value = value;
}
public byte[] getRaw() {
return raw;
}
/**
* 整个TLV编码后的大小,包含T和L
* @return
*/
public SizeT Size(){
if(!this.isValid()){
return null;
}
return new SizeT(this.tlvType.SizeOfVarNumber()+this.length.SizeOfVarNumber()+this.value.length);
}
/**
* 判断当前的 Block 对象是否含有 value
* @return
*/
public boolean hasValue(){
return this.value.length!=0;
}
/**
* 判断当前的 Block 对象是否含有 raw数组
* @return
*/
public boolean hasRaw(){
return this.raw.length!=0;
}
// ************** Sub-Elements **************
/**
* 判断当前 TLV Block 是否包含子 TLV
* 1. 每次通过 CreateBlockBy* 系列工厂方法构造出Block之后,需要手动调用 ParseSubElements 方法,
* 才会去解析一级子TLV
* @return
*/
public boolean hasSubElement(){
return this.elements.length()!=0;
}
/**
* 获取所有的一级子TLV Block
* * 1. 每次通过 CreateBlockBy* 系列工厂方法构造出Block之后,需要手动调用 ParseSubElements 方法,
* * 才会去解析一级子TLV
* @return
*/
public ElementContainer getSubElements(){
return this.elements;
}
/**
* 从 value 字节数组中解析出所有的一级子 TLV Block
* 1. 这要求这个TLV确实存在嵌套的子TLV,或者为空;
* 2. 如果TLV的value不为空,且不存在嵌套的子TLV,则会抛出错误
* @return
*/
// public boolean parseSubElements(){
//
// }
/**
* 判断是否有效
* @return
*/
public boolean isValid(){
return true;
}
}
+181
View File
@@ -0,0 +1,181 @@
package encoding;
import java.math.BigInteger;
import static java.lang.Math.pow;
/*
* @Author: Wang Feng
* @Description:变长无符号整型,用于表示TLV中的T和L
* @Version: 1.0.0
* @Date: 18:13 2021/3/1
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class SizeT {
// 实际传输的数据
private byte[] VlIntBytes;
// 变长整数表示的真实数值
private BigInteger VlIntValue;
/**
* 测量一个变长整型实际用几个字节表示
* @return
*/
public int SizeOfVarNumber(){
return SizeOfVarNumber(this.VlIntValue);
}
private static int SizeOfVarNumber(BigInteger bigInteger){
if(bigInteger.compareTo(BigInteger.valueOf(Long.parseLong("241"))) < 0){
return 1;
}else if(bigInteger.compareTo(BigInteger.valueOf(
new Double(pow(2, 16)).longValue())) < 0){
return 3;
}else if(bigInteger.compareTo(BigInteger.valueOf(
new Double(pow(2, 32)).longValue())) < 0){
return 5;
}else{ //MIN1.0中,VlInt的size最大为9
return 9;
}
}
//todo:判断bytes是否是一个合法的变长非负整型
//todo:判断biginteger是否合法
public SizeT(byte[] bytes){
this.VlIntBytes=bytes;
this.VlIntValue=vlintBytes2bigInteger(bytes);
}
public SizeT(BigInteger bigInteger){
this.VlIntValue=bigInteger;
this.VlIntBytes=bigInteger2vlintBytes(bigInteger);
}
// 传入一个变长无符号字节数组byte[],将它解析成一个VlInt
public SizeT createVlInt(byte[] bytes){
return new SizeT(bytes);
}
// 传入一个BigInteger,将它解析成一个VlInt
public SizeT createVlIntByBigInteger(BigInteger bigInteger){
return new SizeT(bigInteger);
}
// todo:传入一个int,将它解析成一个VlInt
public SizeT(int i){
this.VlIntValue=new BigInteger(String.valueOf(i));
this.VlIntBytes=bigInteger2vlintBytes(this.VlIntValue);
}
// todo:传入一个long,将它解析成一个VlInt
// 将vlint转为一个int输出:需要判断size是不是小于4(1 or 3)
public Integer getVlIntValue2Int(){
if(this.SizeOfVarNumber()<4){
return VlIntValue.intValue();
}
return null;
}
// 将vlint转为一个long输出:需要判断size是不是小于8(5)
public Long getVlIntValue2Long(){
if(this.SizeOfVarNumber()<8){
return VlIntValue.longValue();
}
return null;
}
// 将vlint转为一个biginteger输出:需要判断size是不是等于9(9)
public BigInteger getVlIntValue(){
if(this.SizeOfVarNumber()>=9){
return VlIntValue;
}
return null;
}
// 判断vlint是否与某个int值相等
public boolean isEqual(Integer vlint){
return isEqual(vlint.intValue());
}
public boolean isEqual(int i){
if(this.VlIntValue.compareTo(new BigInteger(String.valueOf(i)))==0){
return true;
}
return false;
}
public boolean isEqual(SizeT vlInt){
if(this.VlIntValue.compareTo(vlInt.VlIntValue)==0){
return true;
}
return false;
}
// todo: 判断vlint是否与某个long值相等
/**
* 判断与另一个VlInt的数值孰大孰小
* @param vlInt
* @return
*/
public int compareTo(SizeT vlInt){
return this.VlIntValue.compareTo(vlInt.VlIntValue);
}
public int compareTo(int i){
BigInteger bigInteger=new BigInteger(String.valueOf(i));
return this.VlIntValue.compareTo(bigInteger);
}
/**
* 减去另一个VlInt
* todo:暂没考虑两者差值越界int最大值
* @param vlInt
* @return
*/
public int subtract(SizeT vlInt){
return this.VlIntValue.subtract(vlInt.VlIntValue).intValue();
}
// 将BigInteger转为byte[按照变长无符号整数方式转]
private byte[] bigInteger2vlintBytes(BigInteger bigInteger){
byte[] vlintbytes;
if(SizeOfVarNumber(bigInteger)==1){
vlintbytes=new byte[1];
//todo
}else if(SizeOfVarNumber(bigInteger)==3){
vlintbytes=new byte[3];
//todo
}else if(SizeOfVarNumber(bigInteger)==5){
vlintbytes=new byte[5];
//todo
}else{
vlintbytes=new byte[9];
//todo
}
return vlintbytes;
}
// 将变长无符号整数数组转为BigInteger
private BigInteger vlintBytes2bigInteger(byte[] vlIntBytes){
BigInteger vlintValue=null;
if(vlIntBytes.length==1){
//todo
}else if(vlIntBytes.length==3){
//todo
}else if(vlIntBytes.length==5){
//todo
}else if(vlIntBytes.length==9){
//todo
}else{
return null;
}
return vlintValue;
}
// 测试
public static void main(String[] args){
BigInteger bigInteger=new BigInteger("3");
SizeT vlInt=new SizeT(bigInteger);
System.out.println(vlInt.isEqual(2));
}
}
+91 -1
View File
@@ -1,5 +1,9 @@
package encoding;
import static util.ByteHelper.getFirstByte;
import static util.ByteHelper.getLenBytes;
import static util.ByteHelper.unsignedbyte2int;
/*
* @Author: Wang Feng
* @Description:
@@ -63,5 +67,91 @@ public class TLV {
// TODO: 目前TLV值的分配,第一轮分配都在500以内,如果以后分配更高值时,需要修改此处的限制
public static final int MaxTlvNum = 500;
// public void ReadVarNumber
/**
* 从字节数组中读出一个变长整型
* @param buffer
* @param start
* @return
*/
public static VlInt readVarNumber(byte[] buffer,VlInt start){
VlInt length=new VlInt(buffer.length);
if(start.compareTo(length)>=0){
return null;
}
int firstbyte=unsignedbyte2int(getFirstByte(buffer,start));
if(firstbyte<241){
return new VlInt(firstbyte);
}else if(firstbyte==241){
if(length.subtract(start)<3){
return null;
}
return new VlInt(getLenBytes(buffer,start,3));
}else if(firstbyte==242){
if(length.subtract(start)<5){
return null;
}
return new VlInt(getLenBytes(buffer,start,5));
}else if(firstbyte==243){
if(length.subtract(start)<9){
return null;
}
return new VlInt(getLenBytes(buffer,start,9));
}else{ //MIN1.0中算越界
return null;
}
}
/**
* 从字节数组的指定位置中读取一个变长的整型作为 TLV Type
* @param buffer
* @param start
* @return
*/
public static VlInt readType(byte[] buffer,VlInt start){
VlInt tlvType=readVarNumber(buffer,start);
if(tlvType==null||tlvType.isEqual(0)||tlvType.compareTo(MaxTlvNum)>0){
return null;
}
return tlvType;
}
// todo:从字节数组的指定位置中读取一个非负整型
/**
* 检查某个type值是否是指定的 TLV-TYPE
* @param current
* @param target
* @return
*/
public static boolean expectType(VlInt current,VlInt target){
if(current.isEqual(target)){
return true;
}
return false;
}
/**
* 根据 TLV 分配规则,判断某个tlvType是否是合规的标识类型
* TODO: 目前TLV的分配是500为一轮,如果标识范围扩展了,本方法需要对应修改
* @param tlvType
* @return
*/
public static boolean isValidIdentifierType(VlInt tlvType){
return tlvType.compareTo(102)>=0 && tlvType.compareTo(199)<=0;
}
/**
* 根据 TLV 分配规则,判断某个tlvType是否是合规的包类型
* TODO: 目前TLV的分配是500为一轮,如果标识范围扩展了,本方法需要对应修改
* @param tlvType
* @return
*/
public static boolean isValidPacketType(VlInt tlvType){
return tlvType.compareTo(0)>0 && tlvType.compareTo(50)<0;
}
//todo: 错误处理
}
+52 -15
View File
@@ -38,6 +38,10 @@ public class VlInt {
}
}
//todo:判断bytes是否是一个合法的变长非负整型
//todo:判断biginteger是否合法
public VlInt(byte[] bytes){
this.VlIntBytes=bytes;
this.VlIntValue=vlintBytes2bigInteger(bytes);
@@ -58,9 +62,13 @@ public class VlInt {
return new VlInt(bigInteger);
}
// 传入一个int,将它解析成一个VlInt
// todo:传入一个int,将它解析成一个VlInt
public VlInt(int i){
this.VlIntValue=new BigInteger(String.valueOf(i));
this.VlIntBytes=bigInteger2vlintBytes(this.VlIntValue);
}
// 传入一个long,将它解析成一个VlInt
// todo:传入一个long,将它解析成一个VlInt
// 将vlint转为一个int输出:需要判断size是不是小于4(1 or 3)
public Integer getVlIntValue2Int(){
@@ -90,30 +98,59 @@ public class VlInt {
public boolean isEqual(Integer vlint){
return isEqual(vlint.intValue());
}
public boolean isEqual(int vlint){
if(VlIntValue.compareTo(new BigInteger(String.valueOf(vlint)))==0){
public boolean isEqual(int i){
if(this.VlIntValue.compareTo(new BigInteger(String.valueOf(i)))==0){
return true;
}
return false;
}
public boolean isEqual(VlInt vlInt){
if(this.VlIntValue.compareTo(vlInt.VlIntValue)==0){
return true;
}
return false;
}
// 判断vlint是否与某个long值相等
// todo: 判断vlint是否与某个long值相等
/**
* 判断与另一个VlInt的数值孰大孰小
* @param vlInt
* @return
*/
public int compareTo(VlInt vlInt){
return this.VlIntValue.compareTo(vlInt.VlIntValue);
}
public int compareTo(int i){
BigInteger bigInteger=new BigInteger(String.valueOf(i));
return this.VlIntValue.compareTo(bigInteger);
}
/**
* 减去另一个VlInt
* todo:暂没考虑两者差值越界int最大值
* @param vlInt
* @return
*/
public int subtract(VlInt vlInt){
return this.VlIntValue.subtract(vlInt.VlIntValue).intValue();
}
// 将BigInteger转为byte[按照变长无符号整数方式转]
private byte[] bigInteger2vlintBytes(BigInteger bigInteger){
byte[] vlintbytes;
if(SizeOfVarNumber(bigInteger)==1){
vlintbytes=new byte[1];
//...
//todo
}else if(SizeOfVarNumber(bigInteger)==3){
vlintbytes=new byte[3];
//...
//todo
}else if(SizeOfVarNumber(bigInteger)==5){
vlintbytes=new byte[5];
//...
//todo
}else{
vlintbytes=new byte[9];
//...
//todo
}
return vlintbytes;
}
@@ -122,13 +159,13 @@ public class VlInt {
private BigInteger vlintBytes2bigInteger(byte[] vlIntBytes){
BigInteger vlintValue=null;
if(vlIntBytes.length==1){
//todo
}else if(vlIntBytes.length==3){
//todo
}else if(vlIntBytes.length==5){
//todo
}else if(vlIntBytes.length==9){
//todo
}else{
return null;
}
@@ -138,7 +175,7 @@ public class VlInt {
// 测试
public static void main(String[] args){
BigInteger bigInteger=new BigInteger("3");
System.out.println(bigInteger);
VlInt vlInt=new VlInt(bigInteger);
System.out.println(vlInt.isEqual(2));
}
}
+58
View File
@@ -0,0 +1,58 @@
package util;
import encoding.VlInt;
/*
* @Author: Wang Feng
* @Description:常用的数据类型转换。转无符号byte是重点
* @Version: 1.0.0
* @Date: 15:40 2021/3/2
* @Copyright: MIN-Group;国家重大科技基础设施——未来网络北大实验室;深圳市信息论与未来网络重点实验室
*/
public class ByteHelper {
/**
* 从byte[]数组指定开始位置获取第一个字节
* todo: start位置可能是大于int类型的数值
* @param buffer
* @param start
* @return
*/
public static byte getFirstByte(byte[] buffer, VlInt start){
int startInt=start.getVlIntValue2Int();
byte[] firstbyte=new byte[1];
System.arraycopy(buffer,startInt,firstbyte,0,1);
return firstbyte[0];
}
/**
* 从byte[]数组指定开始位置获取长度为len的字节数组
* todo: start位置可能是大于int类型的数值
* @param buffer
* @param start
* @return
*/
public static byte[] getLenBytes(byte[] buffer, VlInt start,VlInt len){
int startInt=start.getVlIntValue2Int();
int lenInt=len.getVlIntValue2Int();
byte[] lenbytes=new byte[1];
System.arraycopy(buffer,startInt,lenbytes,0,lenInt);
return lenbytes;
}
public static byte[] getLenBytes(byte[] buffer, VlInt start,int len){
int startInt=start.getVlIntValue2Int();
byte[] lenbytes=new byte[1];
System.arraycopy(buffer,startInt,lenbytes,0,len);
return lenbytes;
}
/**
* 无符号字节转int
* @param b
* @return
*/
public static int unsignedbyte2int(byte b){
return (int) b;
}
}