From 9c99f574aa28cbb601f6894536cf7e1f9ddb7d7b Mon Sep 17 00:00:00 2001 From: Ajax Date: Fri, 8 Jan 2016 15:12:00 +0100 Subject: BinStream: merge getbits calls to getbytes --- miasm2/core/bin_stream.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'miasm2/core/bin_stream.py') diff --git a/miasm2/core/bin_stream.py b/miasm2/core/bin_stream.py index f7b160f9..638cfd0e 100644 --- a/miasm2/core/bin_stream.py +++ b/miasm2/core/bin_stream.py @@ -15,6 +15,7 @@ # with this program; if not, write to the Free Software Foundation, Inc., # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # +import math class bin_stream(object): @@ -36,18 +37,19 @@ class bin_stream(object): @start: the offset in bits @n: number of bits to read """ - if not n: + if n == 0: return 0 o = 0 if n > self.getlen() * 8: raise IOError('not enough bits %r %r' % (n, len(self.bin) * 8)) + temp = self.getbytes(start / 8, int(math.ceil(n / 8.))) + if not temp: + raise IOError('cannot get bytes') + start = start % 8 while n: # print 'xxx', n, start i = start / 8 - c = self.getbytes(i) - if not c: - raise IOError('cannot get bytes') - c = ord(c) + c = ord(temp[i]) # print 'o', hex(c) r = 8 - start % 8 c &= (1 << r) - 1 -- cgit 1.4.1