Files
minstrel/internal/library/id3v2genre.go
T
bvandeusenandClaude Opus 5.5 c9e058b3c9
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
fix(library): a UTF-16 tag frame with a stray trailing zero no longer hides the whole file (#8060)
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>
2026-11-26 14:05:55 -05:00

313 lines
9.3 KiB
Go

package library
import (
"encoding/binary"
"errors"
"io"
"strings"
"unicode/utf16"
"git.fabledsword.com/bvandeusen/minstrel/internal/tagread"
)
// Why this file exists at all: github.com/dhowden/tag reads every other field
// we need correctly, but its text-frame reader destroys multi-value frames.
// readTFrame does
//
// strings.Join(strings.Split(txt, string(singleZero)), "")
//
// — it splits on the ID3v2 null separator and rejoins with the EMPTY string, so
// a file tagged "Alternative Rock" + "Rock" comes back as the single token
// "Alternative RockRock". Stored verbatim, that would corrupt the genre
// browse axis and pollute the taste profile's tag vocabulary.
//
// ffprobe is not an escape hatch either: ffmpeg's read_ttag calls decode_str
// exactly once with no loop, so it keeps only the FIRST value and silently
// discards the rest. Truncating multi-genre tags would blunt genre similarity,
// which is the main thing genre feeds.
//
// So the TCON frame is parsed here directly. Only the genre frame — everything
// else still comes from dhowden/tag, which handles it fine.
// errNoGenreFrame means the file carries no readable genre frame. It is an
// expected outcome (plenty of files are untagged), not a failure.
var errNoGenreFrame = errors.New("library: no ID3v2 genre frame")
// readID3v2GenreValues returns the raw, still-unnormalised values of the ID3v2
// genre frame — one entry per value the tag actually declares. Numeric ID3v1
// references are left alone here; normaliseGenreValue resolves them.
//
// rs is seeked to the start, so it is safe to call after dhowden/tag has
// already consumed the reader.
func readID3v2GenreValues(rs io.ReadSeeker) ([]string, error) {
if _, err := rs.Seek(0, io.SeekStart); err != nil {
return nil, err
}
var hdr [10]byte
if _, err := io.ReadFull(rs, hdr[:]); err != nil {
return nil, errNoGenreFrame
}
if string(hdr[0:3]) != "ID3" {
return nil, errNoGenreFrame
}
major := hdr[3]
// 2.2, 2.3 and 2.4 are the versions in the wild. A future 2.5 would very
// likely move the frame layout, so refuse rather than misparse it.
if major < 2 || major > 4 {
return nil, errNoGenreFrame
}
tagFlags := hdr[5]
size := tagread.SyncsafeInt(hdr[6:10])
if size <= 0 || size > tagread.MaxTagSize {
return nil, errNoGenreFrame
}
body := make([]byte, size)
if _, err := io.ReadFull(rs, body); err != nil {
// A truncated tag is still worth parsing as far as it goes — frame
// walking stops cleanly at the end of what we managed to read.
return nil, errNoGenreFrame
}
// 2.2 used flag 0x40 for whole-tag compression with a scheme that was
// never actually specified. Nothing can read those.
if major == 2 && tagFlags&0x40 != 0 {
return nil, errNoGenreFrame
}
if tagFlags&0x80 != 0 {
// Whole-tag unsynchronisation (2.2/2.3). 2.4 moved this per-frame, but
// some writers still set it at tag level, and undoing it twice is
// harmless: after the first pass no 0xFF 0x00 pairs remain.
body = tagread.UndoUnsynchronisation(body)
}
if major >= 3 && tagFlags&0x40 != 0 {
var ok bool
if body, ok = tagread.SkipExtendedHeader(body, major); !ok {
return nil, errNoGenreFrame
}
}
return findGenreFrame(body, major)
}
// findGenreFrame walks the frame list and decodes the genre frame's values.
func findGenreFrame(body []byte, major byte) ([]string, error) {
// 2.2 frames: 3-byte id + 3-byte size, no flags. 2.3/2.4: 4-byte id +
// 4-byte size + 2-byte flags. The size field is the other difference that
// matters — see tagread.FrameSize.
idLen, sizeLen, flagLen := 4, 4, 2
wantID := "TCON"
if major == 2 {
idLen, sizeLen, flagLen = 3, 3, 0
wantID = "TCO"
}
hdrLen := idLen + sizeLen + flagLen
for off := 0; off+hdrLen <= len(body); {
id := string(body[off : off+idLen])
// A zero byte where a frame id belongs means we've reached the padding
// that fills out the tag. Everything after it is zeros.
if body[off] == 0 {
break
}
size := tagread.FrameSize(body[off+idLen:off+idLen+sizeLen], major)
if size <= 0 || off+hdrLen+size > len(body) {
// Bogus length — we can't trust any offset past this point.
break
}
if id == wantID {
var flags uint16
if flagLen == 2 {
flags = binary.BigEndian.Uint16(body[off+idLen+sizeLen : off+hdrLen])
}
data, ok := frameData(body[off+hdrLen:off+hdrLen+size], major, flags)
if !ok {
return nil, errNoGenreFrame
}
return decodeTextValues(data), nil
}
off += hdrLen + size
}
return nil, errNoGenreFrame
}
// frameData strips per-frame wrappers and reports whether the payload is
// readable at all. Compressed and encrypted frames are not (we have no
// zlib-in-frame or key handling, and neither is meaningful for a genre tag).
func frameData(data []byte, major byte, flags uint16) ([]byte, bool) {
if major == 3 {
// 2.3 flags: %abc00000 %ijk00000 — i compression, j encryption,
// k grouping.
if flags&0x0080 != 0 || flags&0x0040 != 0 {
return nil, false
}
if flags&0x0020 != 0 {
if len(data) < 1 {
return nil, false
}
data = data[1:] // group identifier
}
return data, true
}
if major == 4 {
// 2.4 flags: %0abc0000 %0h00kmnp — h grouping, k compression,
// m encryption, n unsynchronisation, p data-length indicator.
if flags&0x0008 != 0 || flags&0x0004 != 0 {
return nil, false
}
if flags&0x0040 != 0 {
if len(data) < 1 {
return nil, false
}
data = data[1:]
}
if flags&0x0001 != 0 {
if len(data) < 4 {
return nil, false
}
data = data[4:] // syncsafe expanded size; we don't need it
}
if flags&0x0002 != 0 {
data = tagread.UndoUnsynchronisation(data)
}
return data, true
}
return data, true // 2.2 has no frame flags
}
// decodeTextValues splits a text frame's payload into its individual values and
// decodes each according to the frame's encoding byte.
//
// This is the whole point of the file: ID3v2 separates multiple values in one
// text frame with a null, and that separator is two bytes wide for the UTF-16
// encodings. Splitting a UTF-16 payload on single nulls would cut every ASCII
// character in half.
func decodeTextValues(data []byte) []string {
if len(data) == 0 {
return nil
}
encoding := data[0]
payload := data[1:]
switch encoding {
case 0: // ISO-8859-1
return mapChunks(splitOnNul(payload, 1), decodeLatin1)
case 3: // UTF-8
return mapChunks(splitOnNul(payload, 1), func(b []byte) string { return string(b) })
case 1, 2: // UTF-16 with BOM / UTF-16BE without
chunks := splitOnNul(payload, 2)
// Encoding 2 is big-endian by definition. Encoding 1 carries a byte
// order mark, which the spec says must appear on EVERY value in a
// multi-value frame — but writers that emit one only on the first value
// are common. Take the first BOM found as the default for values that
// lack their own, otherwise everything after the first value decodes
// byte-swapped into CJK gibberish.
defaultBE := true
if encoding == 1 {
for _, c := range chunks {
if be, ok := bomOrder(c); ok {
defaultBE = be
break
}
}
}
out := make([]string, 0, len(chunks))
for _, c := range chunks {
be := defaultBE
if encoding == 1 {
if o, ok := bomOrder(c); ok {
be, c = o, c[2:]
}
}
if s := strings.TrimSpace(decodeUTF16(c, be)); s != "" {
out = append(out, s)
}
}
return out
default:
// Unknown encoding byte. Treating it as Latin-1 recovers ASCII text,
// which is better than dropping the frame.
return mapChunks(splitOnNul(payload, 1), decodeLatin1)
}
}
// splitOnNul splits on a null of the given width, honouring alignment so a
// 2-byte-wide separator can't match across a character boundary.
func splitOnNul(b []byte, width int) [][]byte {
var out [][]byte
start := 0
for i := 0; i+width <= len(b); i += width {
if !isNul(b[i : i+width]) {
continue
}
out = append(out, b[start:i])
start = i + width
}
if start < len(b) {
out = append(out, b[start:])
}
return out
}
func isNul(b []byte) bool {
for _, c := range b {
if c != 0 {
return false
}
}
return true
}
func mapChunks(chunks [][]byte, decode func([]byte) string) []string {
out := make([]string, 0, len(chunks))
for _, c := range chunks {
if s := strings.TrimSpace(decode(c)); s != "" {
out = append(out, s)
}
}
return out
}
// decodeLatin1 widens ISO-8859-1 bytes to runes. A plain string() conversion
// would treat the bytes as UTF-8 and mangle every accented character.
func decodeLatin1(b []byte) string {
runes := make([]rune, len(b))
for i, c := range b {
runes[i] = rune(c)
}
return string(runes)
}
// bomOrder reports the byte order a UTF-16 byte-order mark declares, and
// whether one is present at all.
func bomOrder(b []byte) (bigEndian, ok bool) {
if len(b) < 2 {
return false, false
}
switch {
case b[0] == 0xFE && b[1] == 0xFF:
return true, true
case b[0] == 0xFF && b[1] == 0xFE:
return false, true
}
return false, false
}
// decodeUTF16 decodes UTF-16 code units in the given byte order. Any BOM has
// already been consumed by the caller.
func decodeUTF16(b []byte, bigEndian bool) string {
if len(b) < 2 {
return ""
}
units := make([]uint16, 0, len(b)/2)
for i := 0; i+1 < len(b); i += 2 {
if bigEndian {
units = append(units, uint16(b[i])<<8|uint16(b[i+1]))
} else {
units = append(units, uint16(b[i+1])<<8|uint16(b[i]))
}
}
return string(utf16.Decode(units))
}