chx4

幸福是什么?自我欣赏和东游西荡!

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

LEB128即"Little-Endian Base 128",基于128的小印第安序编码格式,是对任意有符号或者无符号整型数的可变长度的编码。

也即,用LEB128编码的正数,会根据数字的大小改变所占字节数。在android的.dex文件中,他只用来编码32bits的整型数。

格式:


上图只是指示性的用两个字节表示。编码的每个字节有效部分只有低7bits,每个字节的最高bit用来指示是否是最后一个字节。
非最高字节的bit7为0
最高字节的bit7为1
将leb128编码的数字转换为可读数字的规则是:除去每个字节的bit7,将每个字节剩余的7个bits拼接在一起,即为数字。
比如:
LEB128编码的0x02b0 ---> 转换后的数字0x0130
转换过程:
0x02b0 ---> 0000 0010 1011 0000 -->去除最高位--> 000 0010 011 0000 -->按4bits重排 --> 00 0001 0011 0000 --> 0x130
转换函数为:
代码位于:android/dalvik/libdex/leb128.h

 1 /*
 2  * Reads an unsigned LEB128 value, updating the given pointer to point
 3  * just past the end of the read value. This function tolerates
 4  * non-zero high-order bits in the fifth encoded byte.
 5  */
 6 DEX_INLINE int readUnsignedLeb128(const u1** pStream) {
 7     const u1* ptr = *pStream;
 8     int result = *(ptr++);
 9 
10     if (result > 0x7f) {
11         int cur = *(ptr++);
12         result = (result & 0x7f) | ((cur & 0x7f) << 7);
13         if (cur > 0x7f) {
14             cur = *(ptr++);
15             result |= (cur & 0x7f) << 14;
16             if (cur > 0x7f) {
17                 cur = *(ptr++);
18                 result |= (cur & 0x7f) << 21;
19                 if (cur > 0x7f) {
20                     /*
21                      * Note: We don't check to see if cur is out of
22                      * range here, meaning we tolerate garbage in the
23                      * high four-order bits.
24                      */
25                     cur = *(ptr++);
26                     result |= cur << 28;
27                 }
28             }
29         }
30     }
31 
32     *pStream = ptr;
33     return result;
34 }
35 
36 /*
37  * Reads a signed LEB128 value, updating the given pointer to point
38  * just past the end of the read value. This function tolerates
39  * non-zero high-order bits in the fifth encoded byte.
40  */
41 DEX_INLINE int readSignedLeb128(const u1** pStream) {
42     const u1* ptr = *pStream;
43     int result = *(ptr++);
44 
45     if (result <= 0x7f) {
46         result = (result << 25) >> 25;
47     } else {
48         int cur = *(ptr++);
49         result = (result & 0x7f) | ((cur & 0x7f) << 7);
50         if (cur <= 0x7f) {
51             result = (result << 18) >> 18;
52         } else {
53             cur = *(ptr++);
54             result |= (cur & 0x7f) << 14;
55             if (cur <= 0x7f) {
56                 result = (result << 11) >> 11;
57             } else {
58                 cur = *(ptr++);
59                 result |= (cur & 0x7f) << 21;
60                 if (cur <= 0x7f) {
61                     result = (result << 4) >> 4;
62                 } else {
63                     /*
64                      * Note: We don't check to see if cur is out of
65                      * range here, meaning we tolerate garbage in the
66                      * high four-order bits.
67                      */
68                     cur = *(ptr++);
69                     result |= cur << 28;
70                 }
71             }
72         }
73     }
74 
75     *pStream = ptr;
76     return result;
77 }

 

posted on 2013-07-30 00:42  chx4  阅读(708)  评论(0编辑  收藏  举报