Skip to content

Commit

Permalink
fix(utilities): Prevent finding the same box index over and over
Browse files Browse the repository at this point in the history
Since it removed the data before the found box's index(-4), all loops would only find the same box at the same index again, but this time the box index would be 4 since all previous data was removed in the prior loop. Since the index-=4 code is only run if the index > 4, this never run on the second loop, and since this data now does not have the box length, Box.parse failed with an IOError.

This corrects looping through boxes and correctly obtains and parses each box.
  • Loading branch information
rlaphoenix committed May 15, 2024
1 parent 2e697d9 commit 2acee30
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions devine/core/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,18 @@ def get_boxes(data: bytes, box_type: bytes, as_bytes: bool = False) -> Box:
# since it doesn't care what child box the wanted box is from, this works fine.
if not isinstance(data, (bytes, bytearray)):
raise ValueError("data must be bytes")

offset = 0
while True:
try:
index = data.index(box_type)
index = data[offset:].index(box_type)
except ValueError:
break
if index < 0:
break
if index > 4:
index -= 4 # size is before box type and is 4 bytes long
data = data[index:]
index -= 4 # size is before box type and is 4 bytes long
try:
box = Box.parse(data)
box = Box.parse(data[offset:][index:])
except IOError:
# since get_init_segment might cut off unexpectedly, pymp4 may be unable to read
# the expected amounts of data and complain, so let's just end the function here
Expand All @@ -147,6 +147,7 @@ def get_boxes(data: bytes, box_type: bytes, as_bytes: bool = False) -> Box:
raise e
if as_bytes:
box = Box.build(box)
offset += index + len(Box.build(box))
yield box


Expand Down

0 comments on commit 2acee30

Please sign in to comment.