From f6a1e5325cff7a59c42e755cbe4a85a33c1df77c Mon Sep 17 00:00:00 2001 From: Xavier Henner Date: Fri, 22 May 2015 00:37:12 +0200 Subject: [PATCH] Fix bug in the split method Sometimes, we can have this configuration : description 00 00 00 data (with delim = [00 00]) with splitN, we make the separation at the first 00 00 : [description, 00 data] But in fact, we must make the separation one byte later [description 00, data] --- id3v2frames.go | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/id3v2frames.go b/id3v2frames.go index 9a5a67d..6d55967 100644 --- a/id3v2frames.go +++ b/id3v2frames.go @@ -67,6 +67,26 @@ func encodingDelim(enc byte) ([]byte, error) { } } +func dataSplit(b []byte, enc byte) ([][]byte, error) { + delim, err := encodingDelim(enc) + if err != nil { + return nil, err + } + result := bytes.SplitN(b, delim, 2) + + if len(result) <= 1 { + return result, nil + } + + if result[1][0] == 0 { + // there was a double (or triple) 0 and we cut too early + result[0] = append(result[0], make([]byte, 0)...) + result[1] = result[1][1:] + } + + return result, nil +} + func decodeISO8859(b []byte) string { r := make([]rune, len(b)) for i, x := range b { @@ -123,12 +143,11 @@ func (t Comm) String() string { // Lyrics/text func readTextWithDescrFrame(b []byte) (*Comm, error) { enc := b[0] - delim, err := encodingDelim(enc) + + descTextSplit, err := dataSplit(b[4:], enc) if err != nil { return nil, err } - - descTextSplit := bytes.SplitN(b[4:], delim, 2) desc, err := decodeText(enc, descTextSplit[0]) if err != nil { return nil, fmt.Errorf("error decoding tag description text: %v", err) @@ -200,12 +219,10 @@ func readPICFrame(b []byte) (*Picture, error) { ext := string(b[1:4]) picType := b[4] - delim, err := encodingDelim(enc) + descDataSplit, err := dataSplit(b[5:], enc) if err != nil { return nil, err } - - descDataSplit := bytes.SplitN(b[5:], delim, 2) desc, err := decodeText(enc, descDataSplit[0]) if err != nil { return nil, fmt.Errorf("error decoding PIC description text: %v", err) @@ -245,12 +262,10 @@ func readAPICFrame(b []byte) (*Picture, error) { b = mimeDataSplit[1] picType := b[0] - delim, err := encodingDelim(enc) + descDataSplit, err := dataSplit(b[1:], enc) if err != nil { return nil, err } - - descDataSplit := bytes.SplitN(b[1:], delim, 2) desc, err := decodeText(enc, descDataSplit[0]) if err != nil { return nil, fmt.Errorf("error decoding APIC description text: %v", err)