// Package tagread reads a music file's tags. It is github.com/dhowden/tag // with one repair: dhowden/tag refuses a whole file when a single UTF-16 text // frame holds an odd number of bytes, and some taggers write one zero byte // too many after each UTF-16 string in a frame. Those files then read as // having no tags at all, and the scanner skipped them. When dhowden/tag // fails, ReadFrom rebuilds the ID3v2 tag in memory without the stray bytes // and reads it again. package tagread import ( "bytes" "encoding/binary" "io" "github.com/dhowden/tag" ) // ReadFrom reads rs's tags, as tag.ReadFrom does. When that fails on an ID3v2 // tag with stray zero bytes in its UTF-16 text frames, it answers from a // copy of the tag with those bytes removed. Any other failure returns // tag.ReadFrom's own error. func ReadFrom(rs io.ReadSeeker) (tag.Metadata, error) { start, err := rs.Seek(0, io.SeekCurrent) if err != nil { return nil, err } meta, err := tag.ReadFrom(rs) if err == nil { return meta, nil } if _, serr := rs.Seek(start, io.SeekStart); serr != nil { return nil, err } fixed, ok := repairID3v2(rs) if !ok { return nil, err } repaired, rerr := tag.ReadFrom(bytes.NewReader(fixed)) if rerr != nil { return nil, err } return repaired, nil } // repairID3v2 reads the ID3v2 tag at rs's position and returns it rebuilt // with the stray zeros removed from each UTF-16 text frame // (dropStrayZeros). It reports false // when there is no ID3v2.3 or 2.4 tag, the tag can't be walked safely, or // nothing needed trimming, so the caller keeps the original error. // // Only the tag is returned: dhowden/tag reads an ID3v2 file's tags without // touching the audio after them. func repairID3v2(rs io.Reader) ([]byte, bool) { var hdr [10]byte if _, err := io.ReadFull(rs, hdr[:]); err != nil || string(hdr[0:3]) != "ID3" { return nil, false } major, flags := hdr[3], hdr[5] // A whole-tag unsynchronised tag would have to be re-synchronised after // the edit; 2.2 has a different frame layout. Neither is worth the risk // for a repair. if (major != 3 && major != 4) || flags&0x80 != 0 { return nil, false } size := SyncsafeInt(hdr[6:10]) if size <= 0 || size > MaxTagSize { return nil, false } body := make([]byte, size) if _, err := io.ReadFull(rs, body); err != nil { return nil, false } frames := body if flags&0x40 != 0 { var ok bool if frames, ok = SkipExtendedHeader(body, major); !ok { return nil, false } } out := make([]byte, 0, len(body)) out = append(out, body[:len(body)-len(frames)]...) // the extended header, unchanged changed := false off := 0 for off+10 <= len(frames) && frames[off] != 0 { n := FrameSize(frames[off+4:off+8], major) if n < 0 || off+10+n > len(frames) { return nil, false } id := string(frames[off : off+4]) frameFlags := frames[off+8 : off+10] data := frames[off+10 : off+10+n] if frameFlags[0] == 0 && frameFlags[1] == 0 { if repaired, ok := dropStrayZeros(id, data); ok { data, changed = repaired, true } } out = append(out, frames[off:off+4]...) out = appendFrameSize(out, len(data), major) out = append(out, frameFlags...) out = append(out, data...) off += 10 + n } if !changed { return nil, false } out = append(out, frames[off:]...) // padding tagBytes := make([]byte, 0, 10+len(out)) tagBytes = append(tagBytes, hdr[:6]...) tagBytes = appendSyncsafe(tagBytes, len(out)) return append(tagBytes, out...), true } // dropStrayZeros removes the zero bytes a faulty tagger writes after each // string of a UTF-16 text frame, and reports whether there were any. That // tagger writes every string as its byte order mark, its text, its // terminator when it has one, and then one extra zero. The extra zero puts // everything after it out of step with the two-byte characters, and leaves // the frame an odd length. // // Only two places are touched, both of which a well-formed frame never has: // zeros between a string's terminator and the next string's byte order mark // (or the end of the frame), and a lone zero left over at the very end. A // frame of odd length that ends in any other byte is left alone. func dropStrayZeros(id string, data []byte) ([]byte, bool) { textStart := 1 // the encoding byte switch { case id == "COMM" || id == "USLT": textStart = 4 // encoding, then a three-letter language case id[0] != 'T': return nil, false } if len(data) <= textStart || (data[0] != 1 && data[0] != 2) { return nil, false } text := data[textStart:] out := append(make([]byte, 0, len(data)), data[:textStart]...) changed := false i := 0 for i+1 < len(text) { out = append(out, text[i], text[i+1]) if text[i] == 0 && text[i+1] == 0 { next := i + 2 for next < len(text) && text[next] == 0 { next++ } if next > i+2 && (next == len(text) || isBOM(text[next:])) { i, changed = next, true continue } } i += 2 } if i < len(text) { if text[i] != 0 { return nil, false } changed = true } return out, changed } // isBOM reports whether b starts with a UTF-16 byte order mark. func isBOM(b []byte) bool { return len(b) >= 2 && (b[0] == 0xFE && b[1] == 0xFF || b[0] == 0xFF && b[1] == 0xFE) } // appendFrameSize writes a frame's length field: syncsafe in 2.4, plain // big-endian in 2.3 (FrameSize reads both). func appendFrameSize(b []byte, n int, major byte) []byte { if major == 4 { return appendSyncsafe(b, n) } return binary.BigEndian.AppendUint32(b, uint32(n)) } // appendSyncsafe writes n as a 4-byte syncsafe integer (SyncsafeInt reads // it). n is at most MaxTagSize, well inside the 28 bits it can hold. func appendSyncsafe(b []byte, n int) []byte { return append(b, byte(n>>21&0x7f), byte(n>>14&0x7f), byte(n>>7&0x7f), byte(n&0x7f)) }