release / go (push) Successful in 2m4s
release / web (push) Successful in 1m21s
release / govulncheck (push) Successful in 20s
release / integration (push) Successful in 4m9s
release / android (push) Successful in 4m28s
release / Build signed APK (push) Successful in 4m56s
release / Attach APK to the Release (tag releases only) (push) Skipped
release / Build + push container image (push) Successful in 14s
release / Verify release artifacts (tag releases only) (push) Skipped
Some taggers end a UTF-16 text frame with a two-byte terminator and then one more zero byte, so the frame holds an odd number of bytes. dhowden/tag refuses the whole tag when it meets one, and the scanner then skipped the file: 61 tracks on dev (whole albums from one tagger, with a genre or year frame written this way) never reached the library. internal/tagread.ReadFrom reads with dhowden/tag first. When that fails on an ID3v2.3/2.4 tag, it rebuilds the tag in memory with the stray byte trimmed from each such frame and reads it again. Anything else still fails with dhowden/tag's own error. The scanner, both MBID backfills, embedded cover art and Subsonic lyrics all read through it. The ID3v2 primitives (syncsafe and frame sizes, the extended header, unsynchronisation) move from library/id3v2genre.go into tagread, so the genre reader and the repair share one copy. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
148 lines
4.6 KiB
Go
148 lines
4.6 KiB
Go
// 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 end a UTF-16 frame
|
|
// with one zero byte too many (a two-byte terminator followed by a stray
|
|
// single one). 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 byte 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 a stray trailing zero in a UTF-16 text frame, it answers from a
|
|
// copy of the tag with that byte 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 each stray trailing zero trimmed (trimStrayNul). 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 trimmed, ok := trimStrayNul(id, data); ok {
|
|
data, changed = trimmed, 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
|
|
}
|
|
|
|
// trimStrayNul drops the last byte of a UTF-16 text frame whose text runs
|
|
// to an odd number of bytes and ends in zero: the stray terminator. A UTF-16
|
|
// frame of odd length that ends any other way is left alone.
|
|
func trimStrayNul(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
|
|
}
|
|
if (len(data)-textStart)%2 == 0 || data[len(data)-1] != 0 {
|
|
return nil, false
|
|
}
|
|
return data[:len(data)-1], true
|
|
}
|
|
|
|
// 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))
|
|
}
|