ID3v2: Trim trailing nulls in some frames' content

This commit is contained in:
sublipri 2023-10-22 08:19:55 +08:00 committed by Alex
parent 0cd16bda35
commit 486cba1f04
5 changed files with 15 additions and 8 deletions

View file

@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- **VorbisComments**: When converting from `Tag` to `VorbisComments`, `ItemKey::Unknown`s will be checked for spec compliance. ([PR](https://github.com/Serial-ATA/lofty-rs/pull/272))
- **ID3v2**: Any trailing null terminators will be trimmed when reading Comment, Text, UserText, UserUrl, and UnsynchronizedText frames. ([PR](https://github.com/Serial-ATA/lofty-rs/pull/275))
- **Alloc**: The default allocation limit for any single tag item is now **16MB**. ([PR](https://github.com/Serial-ATA/lofty-rs/pull/276))
- **Probe**: `Probe::set_file_type()` will return the `Probe` to allow for builder-style usage. ([PR](https://github.com/Serial-ATA/lofty-rs/pull/276))

View file

@ -1,7 +1,9 @@
use crate::error::{Id3v2Error, Id3v2ErrorKind, LoftyError, Result};
use crate::id3::v2::frame::content::verify_encoding;
use crate::id3::v2::header::Id3v2Version;
use crate::util::text::{decode_text, encode_text, read_to_terminator, utf16_decode, TextEncoding};
use crate::util::text::{
decode_text, encode_text, read_to_terminator, trim_end_nulls, utf16_decode, TextEncoding,
};
use std::hash::{Hash, Hasher};
use std::io::Read;
@ -59,9 +61,10 @@ impl ExtendedTextFrame {
let encoding = verify_encoding(encoding_byte, version)?;
let description = decode_text(reader, encoding, true)?;
let frame_content;
let mut frame_content;
if encoding != TextEncoding::UTF16 {
frame_content = decode_text(reader, encoding, false)?.content;
trim_end_nulls(&mut frame_content);
return Ok(Some(ExtendedTextFrame {
encoding,

View file

@ -1,7 +1,7 @@
use crate::error::Result;
use crate::id3::v2::frame::content::verify_encoding;
use crate::id3::v2::header::Id3v2Version;
use crate::util::text::{decode_text, encode_text, TextEncoding};
use crate::util::text::{decode_text, encode_text, trim_end_nulls, TextEncoding};
use std::hash::{Hash, Hasher};
use std::io::Read;
@ -58,7 +58,8 @@ impl ExtendedUrlFrame {
let encoding = verify_encoding(encoding_byte, version)?;
let description = decode_text(reader, encoding, true)?.content;
let content = decode_text(reader, TextEncoding::Latin1, false)?.content;
let mut content = decode_text(reader, TextEncoding::Latin1, false)?.content;
trim_end_nulls(&mut content);
Ok(Some(ExtendedUrlFrame {
encoding,

View file

@ -1,7 +1,7 @@
use crate::error::{Id3v2Error, Id3v2ErrorKind, Result};
use crate::id3::v2::frame::content::verify_encoding;
use crate::id3::v2::header::Id3v2Version;
use crate::util::text::{decode_text, encode_text, TextEncoding};
use crate::util::text::{decode_text, encode_text, trim_end_nulls, TextEncoding};
use std::hash::{Hash, Hasher};
use std::io::Read;
@ -33,7 +33,8 @@ impl LanguageFrame {
reader.read_exact(&mut language)?;
let description = decode_text(reader, encoding, true)?.content;
let content = decode_text(reader, encoding, false)?.content;
let mut content = decode_text(reader, encoding, false)?.content;
trim_end_nulls(&mut content);
Ok(Some(Self {
encoding,

View file

@ -1,7 +1,7 @@
use crate::error::Result;
use crate::id3::v2::frame::content::verify_encoding;
use crate::id3::v2::header::Id3v2Version;
use crate::util::text::{decode_text, encode_text, TextEncoding};
use crate::util::text::{decode_text, encode_text, trim_end_nulls, TextEncoding};
use byteorder::ReadBytesExt;
@ -37,7 +37,8 @@ impl TextInformationFrame {
};
let encoding = verify_encoding(encoding_byte, version)?;
let value = decode_text(reader, encoding, false)?.content;
let mut value = decode_text(reader, encoding, false)?.content;
trim_end_nulls(&mut value);
Ok(Some(TextInformationFrame { encoding, value }))
}