Repository navigation
Description
Reproducer
I tried this code (lofty 0.25.0 from crates.io; also reproduces on current main @ 5affdd9):
use std::borrow::Cow;
use std::io::Cursor;
use lofty::config::{ParseOptions, WriteOptions};
use lofty::file::AudioFile;
use lofty::id3::v2::{Frame, FrameId, Id3v2Tag, KeyValueFrame};
use lofty::mpeg::MpegFile;
use lofty::tag::TagExt;
use lofty::TextEncoding;
fn main() {
let mut tag = Id3v2Tag::new();
tag.insert(Frame::KeyValue(KeyValueFrame::new(
FrameId::Valid(Cow::Borrowed("TMCL")),
TextEncoding::UTF8,
vec![(Cow::Borrowed("cello"), Cow::Borrowed("Yo-Yo Ma"))],
)));
let mut dump = Vec::new();
tag.dump_to(&mut dump, WriteOptions::default().use_id3v23(true))
.unwrap();
let mut cursor = Cursor::new(dump);
let file = MpegFile::read_from(&mut cursor, ParseOptions::new().read_properties(false)).unwrap();
let read_back = file.id3v2().unwrap();
for frame in read_back {
if let Frame::KeyValue(kv) = frame {
for (k, v) in kv.key_value_pairs.iter() {
println!("({k:?}, {v:?})");
}
}
}
}Output:
("cello", "o-Yo Ma")
The first character of the value is gone ("Yo-Yo Ma" → "o-Yo Ma"). With more pairs, every NUL-separated segment after the first loses its first character.
Hexdump of the IPLS frame as written by dump_to:
49 50 4c 53 00 00 00 1f 00 00 01 ff fe 63 00 65 IPLS.........c.e
00 6c 00 6c 00 6f 00 00 00 59 00 6f 00 2d 00 59 .l.l.o...Y.o.-.Y
00 6f 00 20 00 4d 00 61 00 .o. .M.a.
i.e. encoding byte 01, one ff fe BOM at the very start, cello in UTF-16LE, the 00 00 separator, then Yo-Yo Ma in UTF-16LE with no BOM — its first code unit is 59 00 ('Y'), which the reader then consumes as if it were a BOM.
(The frame is written as UTF-16 even though the KeyValueFrame was created with TextEncoding::UTF8, because to_id3v23() substitutes UTF-16 for the v2.3 write.)
Summary
The ID3v2.3 write path and the KeyValueFrame read path disagree about where UTF-16 BOMs live in an IPLS frame:
- Write: the
"TIPL" | "TMCL"arm inlofty/src/id3/v2/write/frame.rs#L193merges all key/value pairs into a singleTextInformationFrameby joining them into one'\0'-separated string, which is then encoded once — so only the first segment gets a BOM. - Read:
KeyValueFrame::parsedecodes the segments one at a time. It does anticipate this layout — the comment says the first pair "may be the only string with a BOM", and it carries the first BOM forward viatext_decode_options.bom(first_key.bom)— but the carry-forward only affects which byte order is used. The UTF-16 branch ofdecode_textstill unconditionally strips the first two bytes of every segment as a presumed BOM (lofty/src/util/text.rs#L368-L377— note both match arms decode&raw_bytes[2..]even whenbomcame fromoptions.bomrather than from the segment itself). So the first UTF-16 code unit of every BOM-less continuation segment is eaten.
Either side could be fixed to restore the symmetry:
- Write per-segment BOMs when merging TIPL/TMCL into IPLS — i.e. encode each key/value separately, as
KeyValueFrame::as_bytesalready does for the native v2.4 path (this is also what the v2.3 spec §3.3 asks for: "Unicode strings must begin with the Unicode BOM"); or - Tolerate BOM-less continuation segments on read — in
decode_text, only skip the two BOM bytes when the segment actually starts withFF FE/FE FF, and otherwise decode from byte 0 using the carried-over byte order.
Expected behavior
The round-trip preserves the pair: ("cello", "Yo-Yo Ma").
Assets
n/a — the reproducer is self-contained (it round-trips through an in-memory buffer).
Activity