Skip to content

Commit

Permalink
Merge pull request #342 from Eyevinn/fix-mfro
Browse files Browse the repository at this point in the history
fix: more careful mfro decoding
  • Loading branch information
tobbee authored Apr 5, 2024
2 parents bb8cefa + 9851df8 commit 37aee0a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

- Nothing yet

### Added

- New TryDecodeMfro function

### Fixed

- More robust check for mfro at the end of file

## [0.43.0] - 2024-04-04

Expand Down
10 changes: 2 additions & 8 deletions mp4/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,24 +346,18 @@ func (f *File) findAndReadMfra(r io.Reader) error {
if err != nil {
return fmt.Errorf("could not seek %d bytes from end: %w", mfroSize, err)
}
b, err := DecodeBox(uint64(pos), rs) // mfro
mfro, err := TryDecodeMfro(uint64(pos), rs) // mfro
if err != nil {
// Not an mfro box here, just reset and return
_, err = rs.Seek(0, io.SeekStart)
return err
}
mfro, ok := b.(*MfroBox)
if !ok {
// Not an mfro box here, just reset and return
_, err = rs.Seek(0, io.SeekStart)
return err
}
mfraSize := int64(mfro.ParentSize)
pos, err = rs.Seek(-mfraSize, io.SeekEnd)
if err != nil {
return fmt.Errorf("could not seek %d bytes from end: %w", mfraSize, err)
}
b, err = DecodeBox(uint64(pos), rs) // mfra
b, err := DecodeBox(uint64(pos), rs) // mfra
if err != nil {
return fmt.Errorf("could not decode mfra box: %w", err)
}
Expand Down
18 changes: 18 additions & 0 deletions mp4/mfro.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mp4

import (
"fmt"
"io"

"github.com/Eyevinn/mp4ff/bits"
Expand All @@ -14,6 +15,23 @@ type MfroBox struct {
ParentSize uint32
}

// TryDecodeMfro only decode an MfroBox and return it.
// If it is not an MfroBox, it returns nil and an error.
func TryDecodeMfro(startPos uint64, r io.Reader) (*MfroBox, error) {
hdr, err := DecodeHeader(r)
if err != nil {
return nil, err
}
if hdr.Name != "mfro" {
return nil, fmt.Errorf("not an mfro box")
}
box, err := DecodeMfro(hdr, startPos, r)
if err != nil {
return nil, err
}
return box.(*MfroBox), nil
}

// DecodeMfro - box-specific decode
func DecodeMfro(hdr BoxHeader, startPos uint64, r io.Reader) (Box, error) {
data, err := readBoxBody(r, hdr)
Expand Down

0 comments on commit 37aee0a

Please sign in to comment.