From 94fa39a7b322aee8ff95c8e515779195a43650bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Mon, 12 Jun 2023 23:05:08 +0200 Subject: [PATCH] Add ElementOffset field --- mkvparse.go | 20 ++++---- mkvparse_test.go | 119 +++++++++++++++++++++++++++-------------------- 2 files changed, 79 insertions(+), 60 deletions(-) diff --git a/mkvparse.go b/mkvparse.go index 9b19140..ec70afc 100644 --- a/mkvparse.go +++ b/mkvparse.go @@ -41,9 +41,10 @@ const ( // ElementInfo contains information about an element encountered in // the stream, and is passed to the handler by the parser on parse events. type ElementInfo struct { - Offset int64 - Size int64 - Level int + ElementOffset int64 + Offset int64 // Data offset + Size int64 + Level int } // Handler declares an interface for handling parse events @@ -147,14 +148,14 @@ func parseElement(reader io.Reader, currentOffset int64, level int, unknownSizeP if unknownSizeParent != -1 && isFinishUnknownSizeBlock(id, unknownSizeParent) { return 0, id, idCount, nil } - count, err = parseElementAfterID(reader, id, currentOffset+idCount, level, unknownSizeParent, handler) + count, err = parseElementAfterID(reader, id, currentOffset, currentOffset+idCount, level, unknownSizeParent, handler) if err != nil { return -1, -1, -1, err } return count + idCount, -1, -1, nil } -func parseElementAfterID(reader io.Reader, id ElementID, currentOffset int64, level int, unknownSizeParent ElementID, handler Handler) (count int64, err error) { +func parseElementAfterID(reader io.Reader, id ElementID, elementOffset int64, currentOffset int64, level int, unknownSizeParent ElementID, handler Handler) (count int64, err error) { size, sizeCount, all1, err := readVarInt(reader) if err != nil { return -1, err @@ -164,9 +165,10 @@ func parseElementAfterID(reader io.Reader, id ElementID, currentOffset int64, le dataOffset := currentOffset + sizeCount count = sizeCount + size info := ElementInfo{ - Offset: dataOffset, - Size: size, - Level: level, + ElementOffset: elementOffset, + Offset: dataOffset, + Size: size, + Level: level, } if typ == masterType { if all1 { @@ -196,7 +198,7 @@ func parseElementAfterID(reader io.Reader, id ElementID, currentOffset int64, le if nextID == -1 { return count, nil } - nextcount, err := parseElementAfterID(reader, nextID, dataOffset+count+nextIDCount, level, unknownSizeParent, handler) + nextcount, err := parseElementAfterID(reader, nextID, dataOffset+count, dataOffset+count+nextIDCount, level, unknownSizeParent, handler) if err != nil { return -1, err } diff --git a/mkvparse_test.go b/mkvparse_test.go index aab2e42..a173e7b 100644 --- a/mkvparse_test.go +++ b/mkvparse_test.go @@ -113,27 +113,30 @@ func TestParseElement(t *testing.T) { { ClusterElement, ElementInfo{ - Offset: 5, - Size: 3, - Level: 0, + ElementOffset: 0, + Offset: 5, + Size: 3, + Level: 0, }, MasterBeginEvent{}, }, { TimecodeElement, ElementInfo{ - Offset: 7, - Size: 1, - Level: 1, + ElementOffset: 5, + Offset: 7, + Size: 1, + Level: 1, }, int64(0x3), }, { ClusterElement, ElementInfo{ - Offset: 5, - Size: 3, - Level: 0, + ElementOffset: 0, + Offset: 5, + Size: 3, + Level: 0, }, MasterEndEvent{}, }, @@ -168,72 +171,80 @@ func TestParseElement(t *testing.T) { { ClusterElement, ElementInfo{ - Offset: 5, - Size: -1, - Level: 0, + ElementOffset: 0, + Offset: 5, + Size: -1, + Level: 0, }, MasterBeginEvent{}, }, { TimecodeElement, ElementInfo{ - Offset: 7, - Size: 1, - Level: 1, + ElementOffset: 5, + Offset: 7, + Size: 1, + Level: 1, }, int64(0x3), }, { TimecodeElement, ElementInfo{ - Offset: 10, - Size: 1, - Level: 1, + ElementOffset: 8, + Offset: 10, + Size: 1, + Level: 1, }, int64(0x3), }, { TimecodeElement, ElementInfo{ - Offset: 13, - Size: 1, - Level: 1, + ElementOffset: 11, + Offset: 13, + Size: 1, + Level: 1, }, int64(0x3), }, { ClusterElement, ElementInfo{ - Offset: 5, - Size: -1, - Level: 0, + ElementOffset: 0, + Offset: 5, + Size: -1, + Level: 0, }, MasterEndEvent{}, }, { ClusterElement, ElementInfo{ - Offset: 20, - Size: 3, - Level: 0, + ElementOffset: 15, + Offset: 20, + Size: 3, + Level: 0, }, MasterBeginEvent{}, }, { TimecodeElement, ElementInfo{ - Offset: 22, - Size: 1, - Level: 1, + ElementOffset: 20, + Offset: 22, + Size: 1, + Level: 1, }, int64(0x3), }, { ClusterElement, ElementInfo{ - Offset: 20, - Size: 3, - Level: 0, + ElementOffset: 15, + Offset: 20, + Size: 3, + Level: 0, }, MasterEndEvent{}, }, @@ -293,18 +304,20 @@ func TestParseMaster_Skips(t *testing.T) { { ClusterElement, ElementInfo{ - Offset: 5, - Size: 3, - Level: 0, + ElementOffset: 0, + Offset: 5, + Size: 3, + Level: 0, }, MasterBeginEvent{}, }, { ClusterElement, ElementInfo{ - Offset: 5, - Size: 3, - Level: 0, + ElementOffset: 0, + Offset: 5, + Size: 3, + Level: 0, }, MasterEndEvent{}, }, @@ -325,36 +338,40 @@ func TestParseMaster_Skips(t *testing.T) { { ClusterElement, ElementInfo{ - Offset: 5, - Size: -1, - Level: 0, + ElementOffset: 0, + Offset: 5, + Size: -1, + Level: 0, }, MasterBeginEvent{}, }, { ClusterElement, ElementInfo{ - Offset: 5, - Size: -1, - Level: 0, + ElementOffset: 0, + Offset: 5, + Size: -1, + Level: 0, }, MasterEndEvent{}, }, { ClusterElement, ElementInfo{ - Offset: 20, - Size: 3, - Level: 0, + ElementOffset: 15, + Offset: 20, + Size: 3, + Level: 0, }, MasterBeginEvent{}, }, { ClusterElement, ElementInfo{ - Offset: 20, - Size: 3, - Level: 0, + ElementOffset: 15, + Offset: 20, + Size: 3, + Level: 0, }, MasterEndEvent{}, },