diff --git a/go.mod b/go.mod index 50db6b0..0ef89cd 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/go-avkit/worker go 1.27.1 require ( + github.com/at-wat/ebml-go v0.19.4 github.com/go-avkit/avkit v0.16.0 github.com/go-avkit/decode v0.5.4 ) @@ -11,7 +12,6 @@ require ( github.com/Eyevinn/mp4ff v0.62.1 // indirect github.com/asticode/go-astikit v0.30.0 // indirect github.com/asticode/go-astits v1.17.0 // indirect - github.com/at-wat/ebml-go v0.19.4 // indirect github.com/gen2brain/h265 v0.2.4 // indirect github.com/go-avkit/bitstream v0.3.1 // indirect github.com/go-avkit/h264 v0.9.0 // indirect diff --git a/serve/masters_test.go b/serve/masters_test.go new file mode 100644 index 0000000..4fb64d6 --- /dev/null +++ b/serve/masters_test.go @@ -0,0 +1,78 @@ +// Copyright (c) 2026, go-avkit +// SPDX-License-Identifier: BSD-3-Clause + +package serve + +import ( + "math" + "testing" + + "github.com/at-wat/ebml-go" + "github.com/go-avkit/worker/proto" +) + +func idOf(t ebml.ElementType) uint32 { + var v uint32 + for _, c := range t.Bytes() { + v = v<<8 | uint32(c) + } + return v +} + +// The scan's master set and ebml-go's element table agree, element by element: +// an element is in the set if and only if the table says it is a Master. A +// named element that is missing from the set is a place a block can hide in. +func TestMastersAgreeWithEbmlGoTable(t *testing.T) { + n := 0 + for et := ebml.ElementInvalid + 1; et < 4096; et++ { + if len(et.Bytes()) == 0 { + continue + } + n++ + if got, want := ebmlMasters[idOf(et)], et.DataType() == ebml.DataTypeMaster; got != want { + t.Errorf("%s (%#x): in the set %v, Master in the table %v", et, idOf(et), got, want) + } + } + if n < 100 { + t.Fatalf("only %d elements enumerated: the table is not being read", n) + } + // the ids the review named, and the hand-written list this set replaced + for _, id := range []uint32{ + 0x41E4, 0x8E, 0xA6, 0xE8, // BlockAdditionMapping, Slices, BlockMore, TimeSlice + 0x18538067, 0x114D9B74, 0x1549A966, 0x1654AE6B, 0xAE, 0xE0, 0x1F43B675, 0xA0, 0x1C53BB6B, 0xBB, 0xB7, + 0x1254C367, 0x7373, 0x1043A770, 0x45B9, 0xB6, 0x75A1, 0x1941A469, 0x61A7, 0x4DBB, + } { + if !ebmlMasters[id] { + t.Errorf("%#x is not a master", id) + } + } + // the block elements are leaves for the scan, and counted + if ebmlMasters[idSimpleBlock] || ebmlMasters[idBlock] { + t.Error("a block is a master") + } + if ebml.ElementSimpleBlock.DataType() != ebml.DataTypeBlock || idOf(ebml.ElementSimpleBlock) != idSimpleBlock || + ebml.ElementBlock.DataType() != ebml.DataTypeBlock || idOf(ebml.ElementBlock) != idBlock { + t.Error("the block ids of the scan are not ebml-go's") + } +} + +// laced blocks hidden in a master that a hand-written list lacked are counted: +// refused as video.limit, with next to nothing allocated. +func TestLacedBlocksInOtherMastersAreCounted(t *testing.T) { + huge := proto.Limits{MaxBytes: math.MaxInt, MaxBoxes: math.MaxInt, MaxTrackSamples: math.MaxInt} + for name, master := range map[string][]byte{ + "BlockAdditionMapping": {0x41, 0xE4}, + "BlockMore": {0xA6}, + "Slices": {0x8E}, + "TimeSlice": {0xE8}, + } { + d := append(append([]byte{}, ebmlHead...), 0x18, 0x53, 0x80, 0x67) + d = append(append(d, unknown...), 0x1F, 0x43, 0xB6, 0x75) + d = append(append(d, unknown...), master...) + d = append(d, unknown...) + for range 10000 { + d = append(d, block(idSimpleBlock, 1, 0x04, 256)...) + } + bomb(t, "10000 laced blocks in "+name, d, huge, proto.CodeLimit, 1<<20) + } +} diff --git a/serve/scan.go b/serve/scan.go index d4d5e32..da16d78 100644 --- a/serve/scan.go +++ b/serve/scan.go @@ -8,6 +8,7 @@ import ( "encoding/binary" "fmt" + "github.com/at-wat/ebml-go" "github.com/go-avkit/worker/proto" ) @@ -87,14 +88,27 @@ func scanBoxes(data []byte, lim proto.Limits) error { return walk(data, 1) } -// ebmlMasters are the Matroska elements whose children are elements: Segment, -// SeekHead, Info, Tracks, TrackEntry, Video, Cluster, BlockGroup, Cues, -// CuePoint, CueTrackPositions, Tags, Tag, Chapters, EditionEntry, ChapterAtom, -// BlockAdditions, Attachments. -var ebmlMasters = map[uint32]bool{ - 0x18538067: true, 0x114D9B74: true, 0x1549A966: true, 0x1654AE6B: true, 0xAE: true, 0xE0: true, - 0x1F43B675: true, 0xA0: true, 0x1C53BB6B: true, 0xBB: true, 0xB7: true, 0x1254C367: true, 0x7373: true, - 0x1043A770: true, 0x45B9: true, 0xB6: true, 0x75A1: true, 0x1941A469: true, 0x61A7: true, 0x4DBB: true, +// ebmlMasters are the elements whose children are elements, derived from +// ebml-go's element table (every ElementType whose DataType is Master): the +// same table the container reader decodes with, so a block can hide in no +// master that the reader descends into and this scan does not (a hand-written +// list missed BlockAdditionMapping, 0x41E4, and others). +var ebmlMasters = mastersOf() + +func mastersOf() map[uint32]bool { + m := map[uint32]bool{} + for t := ebml.ElementInvalid + 1; t < 4096; t++ { + id := t.Bytes() + if len(id) == 0 || len(id) > 4 || t.DataType() != ebml.DataTypeMaster { + continue + } + var v uint32 + for _, c := range id { + v = v<<8 | uint32(c) + } + m[v] = true + } + return m } func ebmlVint(b []byte, keepMarker bool) (v uint64, n int) {