From 2acee30e54a36708ddc85936f14080bc5f05cd9f Mon Sep 17 00:00:00 2001 From: rlaphoenix <17136956+rlaphoenix@users.noreply.github.com> Date: Wed, 15 May 2024 17:54:21 +0100 Subject: [PATCH] fix(utilities): Prevent finding the same box index over and over 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. --- devine/core/utilities.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/devine/core/utilities.py b/devine/core/utilities.py index 81c0a4d..d0ddf78 100644 --- a/devine/core/utilities.py +++ b/devine/core/utilities.py @@ -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 @@ -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