日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

[转载]LEB128格式简介(CN)

發布時間:2023/12/19 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转载]LEB128格式简介(CN) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
[轉載]LEB128格式簡介(CN)

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 閱讀(...) 評論(...) ?編輯 收藏

轉載于:https://www.cnblogs.com/chx4/articles/3224212.html

總結

以上是生活随笔為你收集整理的[转载]LEB128格式简介(CN)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。