`
lobin
  • 浏览: 427291 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

RLP

 
阅读更多
RLP
Recursive Length Prefix

RLP元素:
item,list

长度编码
数据编码
数据编码和长度编码采用的是相同的编码规则。

编码规则:
                           <input>   -> <length of data prefix> | <length of data prefix><data> | <input> | <length of data prefix>(<input>)+
                            <input>  -> -128, 1-127
                               <data> -> (octet①)*
   <length of data prefix> -> <length of length prefix> | <length of length prefix><length of data>
<length of length prefix> -> octet②



RLP
长list偏移
<offset long list>     -> 0xf7 -> 247, -9(1111 0111)
短list偏移
<offset short list>   -> 0xc0 -> 192, -64(1100 0000)
长item偏移
<offset long item>  -> 0xb7 -> 183, -73(1011 0111)
短item偏移
<offset short item> -> 0x80 -> 128, -128(1000 0000)





<length of length prefix> > <offset long list> -> <long list>

<offset short list> <= <length of length prefix> <= <offset long list> -> <short list>

<offset long item> < <length of length prefix> < <offset short list> -> <long item>

<offset short item> < <length of length prefix> <= <offset long item> -> <short item>

<length of length prefix> = <offset short item> -> <null item>

<length of length prefix> < <offset short item> -> <single byte item>








编码规则:
                           <input> -> <length of data prefix> | <length of data prefix><data> | <input> | <length of data prefix>(<input>)+
                            <input>  -> -128, 1-127
                               <data> -> (octet①)*
   <length of data prefix> -> <length of length prefix> | <length of length prefix><length of data>
<length of length prefix> -> octet②





如果<offset short item> < <length of length prefix> <= <offset long item>
                           <input> -> <length of data prefix><data>
<length of data prefix> -> octet
                               octet -> L + <offset short item>





如果<offset long item> < <length of length prefix> < <offset short list>

                             <input> -> <length of data prefix><data>
   <length of data prefix> -> <length of length prefix><length of data>
<length of length prefix> -> LL + <offset long item>
               <length of data> -> (octet①)LL
                                          L -> (octet[0] & 0xFF) << (8 * LL - 1) + (octet[1] & 0xFF) << (8 * LL - 2) + (octet[2] & 0xFF) << (8 * LL - 3) + ... + (octet[LL - 1] & 0xFF) << (8 * LL - LL)
                                <data> -> (octet②)L






如果<offset short list> <= <length of length prefix> <= <offset long list>

                              <input> -> <length of data prefix> | <length of data prefix><data> | <input> | <length of data prefix>(<input>)+
   <length of data prefix> -> <length of length prefix> | <length of length prefix><length of data>
<length of length prefix> -> L + <offset short list>
                               <data> -> (octet)L









如果<length of length prefix> > <offset long list>

                          <input> -> <length of data prefix> | <length of data prefix><data> | <input> | <length of data prefix>(<input>)+
<length of data prefix> -> <length of length prefix> | <length of length prefix><length of data>
<length of data prefix> -> octet
                               octet -> LL + <offset long list>
           <length of data> -> (octet①)LL
                                      L -> (octet[0] & 0xFF) << (8 * LL - 1) + (octet[1] & 0xFF) << (8 * LL - 2) + (octet[2] & 0xFF) << (8 * LL - 3) + ... + (octet[LL - 1] & 0xFF) << (8 * LL - LL)
                           <data> -> (octet②)L







RLP
输入:
只支持String, Integer, Long以及BigInteger。
支持byte[]
数组,且不可以是原始数据类型的数组。
支持Object[],数组中的元素必须是1中指定的类型或者嵌套的数组(包括byte[]、Object[])。
数组支持嵌套

Integer和Long在内部都是按照BigInteger处理。








RLP
数据编码
                           <input>   -> <length of data prefix> | <length of data prefix><data> | <input> | <length of data prefix>(<input>)+
                            <input>  -> -128, 1-127
                               <data> -> (octet①)*
   <length of data prefix> -> <length of length prefix> | <length of length prefix><length of data>
<length of length prefix> -> octet②










RLP
长度编码

如果输入数据字节流长度L < 56(0011 1000):
       <length of data prefix> -> <length of length prefix>
   
       <length of data prefix> -> <length of length prefix>
    <length of length prefix> -> octet
                                       octet -> L + (<offset short item> | <offset short list>)








RLP
如果255(0xff, 1111 1111) < 输入数据字节流长度L < 最大item长度(2^64):
       <length of data prefix> -> <length of length prefix><length of data>
                  <length of data> -> (octet①)+
                                             + -> <size of octet①>
                 <size of octet①> -> octet②
   <length of length prefix> -> octet③
                                 octet③ -> octet② + <offset short item> | <offset short list> + 56(0011 1000) - 1

如果56(0011 1000) <= 输入数据字节流长度L <= 255(0xff, 1111 1111):
       <length of data prefix> -> <length of length prefix><length of data>
                  <length of data> -> octet①
   <length of length prefix> -> octet②
                                 octet② -> 1 + <offset short item> | <offset short list> + 56(0011 1000) - 1







input: (byte[0]) -> output: 0x80

input: (Object[0]) | (String[0]) | (Integer[0]) | (Long[0]) | (BigInteger[0]) -> output: 0xC0

input: (byte) 0 -> output: Unsupported type: Only accepting String, Integer and BigInteger for now

input: (byte[1]) -128, 0,1,2,...,127 -> output: -128, 0,1,2,...,127





分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics