diff --git a/elementtype.go b/elementtype.go index 0303248..c821ab0 100644 --- a/elementtype.go +++ b/elementtype.go @@ -47,7 +47,7 @@ const ( ElementSeekPosition ElementInfo - ElementTimecodeScale + ElementTimestampScale ElementDuration ElementDateUTC ElementTitle @@ -55,7 +55,7 @@ const ( ElementWritingApp ElementCluster - ElementTimecode + ElementTimestamp ElementPrevSize ElementSimpleBlock ElementBlockGroup @@ -125,6 +125,12 @@ const ( elementMax ) +// WebM aliases +const ( + ElementTimecodeScale = ElementTimestampScale + ElementTimecode = ElementTimestamp +) + func (i ElementType) String() string { switch i { case ElementEBML: @@ -157,8 +163,8 @@ func (i ElementType) String() string { return "SeekPosition" case ElementInfo: return "Info" - case ElementTimecodeScale: - return "TimecodeScale" + case ElementTimestampScale: + return "TimestampScale" case ElementDuration: return "Duration" case ElementDateUTC: @@ -171,8 +177,8 @@ func (i ElementType) String() string { return "WritingApp" case ElementCluster: return "Cluster" - case ElementTimecode: - return "Timecode" + case ElementTimestamp: + return "Timestamp" case ElementPrevSize: return "PrevSize" case ElementSimpleBlock: @@ -342,8 +348,8 @@ func ElementTypeFromString(s string) (ElementType, error) { return ElementSeekPosition, nil case "Info": return ElementInfo, nil - case "TimecodeScale": - return ElementTimecodeScale, nil + case "TimestampScale": + return ElementTimestampScale, nil case "Duration": return ElementDuration, nil case "DateUTC": @@ -356,8 +362,8 @@ func ElementTypeFromString(s string) (ElementType, error) { return ElementWritingApp, nil case "Cluster": return ElementCluster, nil - case "Timecode": - return ElementTimecode, nil + case "Timestamp": + return ElementTimestamp, nil case "PrevSize": return ElementPrevSize, nil case "SimpleBlock": @@ -484,6 +490,13 @@ func ElementTypeFromString(s string) (ElementType, error) { return ElementCueClusterPosition, nil case "CueBlockNumber": return ElementCueBlockNumber, nil + + // WebM aliases + case "TimecodeScale": + return ElementTimecodeScale, nil + case "Timecode": + return ElementTimecode, nil + default: return 0, errUnknownElementType } diff --git a/unmarshal.go b/unmarshal.go index c65d4d2..626b2aa 100644 --- a/unmarshal.go +++ b/unmarshal.go @@ -60,11 +60,7 @@ func readElement(r0 io.Reader, n int64, vo reflect.Value, depth int, pos uint64, r = r0 } - type field struct { - v reflect.Value - t reflect.Type - } - fieldMap := make(map[string]field) + fieldMap := make(map[ElementType]reflect.Value) if vo.IsValid() { for i := 0; i < vo.NumField(); i++ { var nn []string @@ -77,7 +73,11 @@ func readElement(r0 io.Reader, n int64, vo reflect.Value, depth int, pos uint64, } else { name = vo.Type().Field(i).Name } - fieldMap[name] = field{vo.Field(i), vo.Type().Field(i).Type} + t, err := ElementTypeFromString(name) + if err != nil { + return nil, err + } + fieldMap[t] = vo.Field(i) } } @@ -102,8 +102,8 @@ func readElement(r0 io.Reader, n int64, vo reflect.Value, depth int, pos uint64, return nil, err } var vnext reflect.Value - if fm, ok := fieldMap[v.e.String()]; ok { - vnext = fm.v + if vn, ok := fieldMap[v.e]; ok { + vnext = vn } var elem *Element diff --git a/unmarshal_test.go b/unmarshal_test.go index f32c56c..35a3d87 100644 --- a/unmarshal_test.go +++ b/unmarshal_test.go @@ -258,6 +258,15 @@ func TestUnmarshal_Error(t *testing.T) { t.Errorf("Unexpected error, %v, got %v\n", errIndefiniteType, err) } }) + t.Run("UnknownElement", func(t *testing.T) { + input := &struct { + Header struct { + } `ebml:"Unknown"` + }{} + if err := Unmarshal(bytes.NewBuffer([]byte{}), input); err != errUnknownElementType { + t.Errorf("Unexpected error, %v, got %v\n", errUnknownElementType, err) + } + }) t.Run("Short", func(t *testing.T) { TestBinaries := map[string][]byte{ "ElementID": {0x1a, 0x45, 0xdf},