diff --git a/id3v2.go b/id3v2.go index 74314bd..d4c6838 100644 --- a/id3v2.go +++ b/id3v2.go @@ -5,6 +5,7 @@ package tag import ( + "errors" "fmt" "io" "regexp" @@ -269,11 +270,34 @@ func readID3v2Frames(r io.Reader, offset uint, h *id3v2Header) (map[string]inter if flags != nil { if flags.Compression { - _, err = read7BitChunkedUint(r, 4) // read 4 - if err != nil { + switch h.Version { + case ID3v2_3: + // No data length indicator defined. + if _, err := read7BitChunkedUint(r, 4); err != nil { // read 4 + return nil, err + } + size -= 4 + + case ID3v2_4: + // Must have a data length indicator (to give the size) if compression is enabled. + if !flags.DataLengthIndicator { + return nil, errors.New("compression without data length indicator") + } + + default: + return nil, fmt.Errorf("unsupported compression flag used in %v", h.Version) + } + } + + if flags.DataLengthIndicator { + if h.Version == ID3v2_3 { + return nil, fmt.Errorf("data length indicator set but not defined for %v", ID3v2_3) + } + + size, err = read7BitChunkedUint(r, 4) + if err != nil { // read 4 return nil, err } - size -= 4 } if flags.Encryption {