Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions tests/test_opus_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,41 @@ def test_rejects_what_it_cannot_rewrite(self):
with self.assertRaisesRegex(ValueError, "two Ogg pages"):
T.retag(data[:first], T.tags_packet([]))

def test_retag_rejects_serial_mismatch_sequence_gap_and_continuation_flags(self):
with tempfile.TemporaryDirectory() as d:
path = os.path.join(d, "b", "0ABCDEF1.ogg")
make_ogg(path, packets=3, stream_id=0x11111111)
original = Path(path).read_bytes()
raw_pages = [original[s:e] for s, e in T.pages(original)]
p0, p1, p2 = raw_pages[0], raw_pages[1], raw_pages[2]
head_pkt = p0[27 + p0[26]:]
new_tags = T.tags_packet([("TITLE", "new")])

cases = [
("page 0 sequence != 0",
awc_opus.ogg_page(0x11111111, 2, 0, [head_pkt], 0x02) + p1 + p2,
"sequence number 0"),
("page 0 serial mismatch",
awc_opus.ogg_page(0x22222222, 0, 0, [head_pkt], 0x02) + p1 + p2,
"serial mismatch"),
("audio page serial mismatch",
p0 + p1 + awc_opus.ogg_page(0x33333333, 2, 1000, [PACKET], 0x04),
"serial mismatch"),
("audio page sequence gap",
p0 + p1 + awc_opus.ogg_page(0x11111111, 5, 1000, [PACKET], 0x04),
"non-contiguous"),
("OpusTags continuation flag",
p0 + awc_opus.ogg_page(0x11111111, 1, 0, [T.tags_packet([])], 0x01) + p2,
"continuation page"),
("OpusTags EOS flag",
p0 + awc_opus.ogg_page(0x11111111, 1, 0, [T.tags_packet([])], 0x04) + p2,
"BOS/EOS"),
]
for label, bad_data, pattern in cases:
with self.subTest(case=label):
with self.assertRaisesRegex(ValueError, pattern):
T.retag(bad_data, new_tags)


class TestFolder(unittest.TestCase):
def setUp(self):
Expand Down
28 changes: 22 additions & 6 deletions tools/opus_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,22 @@ def retag(data: bytes, packet: bytes) -> bytes:
raise ValueError("tags do not fit in one page")
it = checked_pages(data)
try:
_, (tags_at, tags_end) = next(it), next(it)
(head_at, _head_end), (tags_at, tags_end) = next(it), next(it)
except StopIteration:
raise ValueError("fewer than two Ogg pages") from None
head_serial, head_seq = struct.unpack_from("<II", data, head_at + 14)
if head_seq != 0:
raise ValueError("page 0 does not have sequence number 0")
serial, seq = struct.unpack_from("<II", data, tags_at + 14)
if serial != head_serial:
raise ValueError("Ogg bitstream serial mismatch")
if seq != 1:
raise ValueError("the OpusTags page is not page 1")
tags_flags = data[tags_at + 5]
if tags_flags & 0x01:
raise ValueError("the OpusTags page is a continuation page")
if tags_flags & 0x06:
raise ValueError("the OpusTags page has unexpected BOS/EOS flags")
body = tags_at + 27 + data[tags_at + 26]
if data[body:body + 8] != b"OpusTags":
raise ValueError("page 1 does not start an OpusTags packet")
Expand All @@ -101,11 +114,14 @@ def retag(data: bytes, packet: bytes) -> bytes:
raise ValueError("the OpusTags packet continues past page 1")
if any(segment != 255 for segment in segments[:-1]):
raise ValueError("the OpusTags page contains another packet")
for _ in it: # also check the audio tail before publishing any replacement
pass
serial, seq = struct.unpack_from("<II", data, tags_at + 14)
if seq != 1:
raise ValueError("the OpusTags page is not page 1")
expected_seq = 2
for page_at, _page_end in it: # also check the audio tail before publishing any replacement
page_serial, page_seq = struct.unpack_from("<II", data, page_at + 14)
if page_serial != head_serial:
raise ValueError("Ogg bitstream serial mismatch")
if page_seq != expected_seq:
raise ValueError("non-contiguous Ogg page sequence")
expected_seq = (expected_seq + 1) & 0xFFFFFFFF
return data[:tags_at] + ogg_page(serial, 1, 0, [packet], 0x00) + data[tags_end:]


Expand Down
Loading