uleb128: fix sign extend in read_i{32,64}_leb128
This commit is contained in:
parent
fd09ff9872
commit
cae54ff8d9
2 changed files with 16 additions and 25 deletions
|
|
@ -211,19 +211,19 @@ RZ_API size_t read_u32_leb128(const ut8 *p, const ut8 *max, ut32 *out_value) {
|
|||
RZ_API size_t read_i32_leb128(const ut8 *p, const ut8 *max, st32 *out_value) {
|
||||
if (p < max && !(p[0] & 0x80)) {
|
||||
ut32 result = LEB128_1(ut32);
|
||||
*out_value = SIGN_EXTEND(ut32, result, 6);
|
||||
*out_value = SIGN_EXTEND(st32, result, 6);
|
||||
return 1;
|
||||
} else if (p + 1 < max && !(p[1] & 0x80)) {
|
||||
ut32 result = LEB128_2(ut32);
|
||||
*out_value = SIGN_EXTEND(ut32, result, 13);
|
||||
*out_value = SIGN_EXTEND(st32, result, 13);
|
||||
return 2;
|
||||
} else if (p + 2 < max && !(p[2] & 0x80)) {
|
||||
ut32 result = LEB128_3(ut32);
|
||||
*out_value = SIGN_EXTEND(ut32, result, 20);
|
||||
*out_value = SIGN_EXTEND(st32, result, 20);
|
||||
return 3;
|
||||
} else if (p + 3 < max && !(p[3] & 0x80)) {
|
||||
ut32 result = LEB128_4(ut32);
|
||||
*out_value = SIGN_EXTEND(ut32, result, 27);
|
||||
*out_value = SIGN_EXTEND(st32, result, 27);
|
||||
return 4;
|
||||
} else if (p + 4 < max && !(p[4] & 0x80)) {
|
||||
/* the top bits should be a sign-extension of the sign bit */
|
||||
|
|
@ -282,39 +282,39 @@ RZ_API size_t read_u64_leb128(const ut8 *p, const ut8 *max, ut64 *out_value) {
|
|||
RZ_API size_t read_i64_leb128(const ut8 *p, const ut8 *max, st64 *out_value) {
|
||||
if (p < max && !(p[0] & 0x80)) {
|
||||
ut64 result = LEB128_1(ut64);
|
||||
*out_value = SIGN_EXTEND(ut64, result, 6);
|
||||
*out_value = SIGN_EXTEND(st64, result, 6);
|
||||
return 1;
|
||||
} else if (p + 1 < max && !(p[1] & 0x80)) {
|
||||
ut64 result = LEB128_2(ut64);
|
||||
*out_value = SIGN_EXTEND(ut64, result, 13);
|
||||
*out_value = SIGN_EXTEND(st64, result, 13);
|
||||
return 2;
|
||||
} else if (p + 2 < max && !(p[2] & 0x80)) {
|
||||
ut64 result = LEB128_3(ut64);
|
||||
*out_value = SIGN_EXTEND(ut64, result, 20);
|
||||
*out_value = SIGN_EXTEND(st64, result, 20);
|
||||
return 3;
|
||||
} else if (p + 3 < max && !(p[3] & 0x80)) {
|
||||
ut64 result = LEB128_4(ut64);
|
||||
*out_value = SIGN_EXTEND(ut64, result, 27);
|
||||
*out_value = SIGN_EXTEND(st64, result, 27);
|
||||
return 4;
|
||||
} else if (p + 4 < max && !(p[4] & 0x80)) {
|
||||
ut64 result = LEB128_5(ut64);
|
||||
*out_value = SIGN_EXTEND(ut64, result, 34);
|
||||
*out_value = SIGN_EXTEND(st64, result, 34);
|
||||
return 5;
|
||||
} else if (p + 5 < max && !(p[5] & 0x80)) {
|
||||
ut64 result = LEB128_6(ut64);
|
||||
*out_value = SIGN_EXTEND(ut64, result, 41);
|
||||
*out_value = SIGN_EXTEND(st64, result, 41);
|
||||
return 6;
|
||||
} else if (p + 6 < max && !(p[6] & 0x80)) {
|
||||
ut64 result = LEB128_7(ut64);
|
||||
*out_value = SIGN_EXTEND(ut64, result, 48);
|
||||
*out_value = SIGN_EXTEND(st64, result, 48);
|
||||
return 7;
|
||||
} else if (p + 7 < max && !(p[7] & 0x80)) {
|
||||
ut64 result = LEB128_8(ut64);
|
||||
*out_value = SIGN_EXTEND(ut64, result, 55);
|
||||
*out_value = SIGN_EXTEND(st64, result, 55);
|
||||
return 8;
|
||||
} else if (p + 8 < max && !(p[8] & 0x80)) {
|
||||
ut64 result = LEB128_9(ut64);
|
||||
*out_value = SIGN_EXTEND(ut64, result, 62);
|
||||
*out_value = SIGN_EXTEND(st64, result, 62);
|
||||
return 9;
|
||||
} else if (p + 9 < max && !(p[9] & 0x80)) {
|
||||
/* the top bits should be a sign-extension of the sign bit */
|
||||
|
|
@ -345,12 +345,3 @@ RZ_API size_t read_i64_leb128(const ut8 *p, const ut8 *max, st64 *out_value) {
|
|||
#undef LEB128_10
|
||||
#undef SHIFT_AMOUNT
|
||||
#undef SIGN_EXTEND
|
||||
|
||||
#if 0
|
||||
main() {
|
||||
ut32 n;
|
||||
ut8 *buf = "\x10\x02\x90\x88";
|
||||
rz_uleb128 (buf, &n);
|
||||
printf ("n = %d\n", n);
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -1,8 +1,8 @@
|
|||
d "trap" 00
|
||||
d "nop" 01
|
||||
d "block" 0240
|
||||
d "loop" 0340
|
||||
d "if" 0440
|
||||
d "block (result ?)" 0240
|
||||
d "loop (result ?)" 0340
|
||||
d "if (result ?)" 0440
|
||||
d "else" 05
|
||||
d "end" 0b
|
||||
d "br 0" 0c00
|
||||
|
|
|
|||
Loading…
Reference in a new issue