I'm trying to find specific bytes within a file loaded as ConstBitStream:
s = ConstBitStream(filename=myFile)
found = s.find('0x4140', bytealigned=False)
This is working fine for the first occurrence. After finding the first sequence, I want to find the next one by using the find method again but starting from an offset now:
s.bytepos = position_after_the_first_occurrence + my_offset
found = s.find('0x4140', start=s.bytepos, bytealigned=False)
This doesn't seem to work. I'm always getting the position from the first occurrence of my binary sequence.
What's wrong?
UPDATE:
(values of first found and s.bytepos):
found = {tuple} (54784, )
s.bytepos = {int} 6848
(values of second found and s.bytepos):
s.bytepos = {int} 32969
found = {tuple} (54784, )
It seems that setting start=s.bytepos does not have any effect.
The start parameter is the bit position to start the search, not the byte position. To get the next occurrence you need to use start=s.bitpos + 1.
Another thing to note is that if you use bytealigned=False (which is the default) then you can't in general even use s.bytepos afterwards as the current position might not be byte aligned (it will raise a ByteAlignError). Chances are you want bytealigned=True, which is also a fair bit faster.
Also note you can just use
g = s.findall('0x4140')
which returns a generator that gives all the positions without having to do multiple find calls (just use g.next() repeatedly or list(g) to get them all at once).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With