From ab1b5847c0167c06091c30c2a6ea4d1d94b7c40d Mon Sep 17 00:00:00 2001 From: Michael Beck Date: Wed, 1 Aug 2007 15:25:26 +0000 Subject: [PATCH] fixed broken get_tarval_sub_bits() implementation: now correctly clip number of bits [r15417] --- ir/tv/strcalc.c | 8 ++++++-- ir/tv/strcalc.h | 8 ++++++++ ir/tv/tv.c | 2 +- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/ir/tv/strcalc.c b/ir/tv/strcalc.c index fefa802a7..10bf88572 100644 --- a/ir/tv/strcalc.c +++ b/ir/tv/strcalc.c @@ -1268,13 +1268,17 @@ unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs) { unsigned char res; /* the current scheme uses one byte to store a nibble */ - if (nibble_ofs >= len) + if (4 * nibble_ofs >= len) return 0; res = _val(val[nibble_ofs]); - if (len > nibble_ofs + 1) + if (len > 4 * (nibble_ofs + 1)) res |= _val(val[nibble_ofs + 1]) << 4; + /* kick bits outsize */ + if (len < 8*byte_ofs) { + res &= 0xFF >> (8*byte_ofs - len); + } return res; } diff --git a/ir/tv/strcalc.h b/ir/tv/strcalc.h index 3a854c582..c23307c8e 100644 --- a/ir/tv/strcalc.h +++ b/ir/tv/strcalc.h @@ -189,6 +189,14 @@ int sc_get_lowest_set_bit(const void *value); int sc_is_zero(const void *value); int sc_is_negative(const void *value); int sc_had_carry(void); + +/** + * Return the bits of a tarval at a given byte-offset. + * + * @param value the value + * @param len number of valid bits in the value + * @param byte_ofs the byte offset + */ unsigned char sc_sub_bits(const void *value, int len, unsigned byte_ofs); /** diff --git a/ir/tv/tv.c b/ir/tv/tv.c index ca2165b2c..aecdf2441 100644 --- a/ir/tv/tv.c +++ b/ir/tv/tv.c @@ -1479,7 +1479,7 @@ char *get_tarval_bitpattern(tarval *tv) { unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs) { switch (get_mode_arithmetic(tv->mode)) { case irma_twos_complement: - return sc_sub_bits(tv->value, tv->length, byte_ofs); + return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs); case irma_ieee754: return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs); default: -- 2.20.1