Skip to content

Commit 7398602

Browse files
uklotzdeSerial-ATA
authored andcommitted
Strip trailing null characters from decoded text
1 parent 486cba1 commit 7398602

File tree

14 files changed

+82
-48
lines changed

14 files changed

+82
-48
lines changed

src/ape/tag/read.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::ape::header::{self, ApeHeader};
55
use crate::error::Result;
66
use crate::macros::{decode_err, err, try_vec};
77
use crate::tag::item::ItemValue;
8+
use crate::util::text::utf8_decode;
89

910
use std::io::{Read, Seek, SeekFrom};
1011

@@ -38,7 +39,7 @@ where
3839
key_char = data.read_u8()?;
3940
}
4041

41-
let key = String::from_utf8(key)
42+
let key = utf8_decode(key)
4243
.map_err(|_| decode_err!(Ape, "APE tag item contains a non UTF-8 key"))?;
4344

4445
if INVALID_KEYS.contains(&&*key.to_uppercase()) {
@@ -57,11 +58,11 @@ where
5758
data.read_exact(&mut value)?;
5859

5960
let parsed_value = match item_type {
60-
0 => ItemValue::Text(String::from_utf8(value).map_err(|_| {
61+
0 => ItemValue::Text(utf8_decode(value).map_err(|_| {
6162
decode_err!(Ape, "Failed to convert text item into a UTF-8 string")
6263
})?),
6364
1 => ItemValue::Binary(value),
64-
2 => ItemValue::Locator(String::from_utf8(value).map_err(|_| {
65+
2 => ItemValue::Locator(utf8_decode(value).map_err(|_| {
6566
decode_err!(Ape, "Failed to convert locator item into a UTF-8 string")
6667
})?),
6768
_ => decode_err!(@BAIL Ape, "APE tag item contains an invalid item type"),

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ impl Display for FileEncodingError {
388388

389389
/// Errors that could occur within Lofty
390390
pub struct LoftyError {
391-
kind: ErrorKind,
391+
pub(crate) kind: ErrorKind,
392392
}
393393

394394
impl LoftyError {

src/id3/v2/items/extended_text_frame.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::error::{Id3v2Error, Id3v2ErrorKind, LoftyError, Result};
22
use crate::id3::v2::frame::content::verify_encoding;
33
use crate::id3::v2::header::Id3v2Version;
44
use crate::util::text::{
5-
decode_text, encode_text, read_to_terminator, trim_end_nulls, utf16_decode, TextEncoding,
5+
decode_text, encode_text, read_to_terminator, utf16_decode_bytes, TextEncoding,
66
};
77

88
use std::hash::{Hash, Hasher};
@@ -61,10 +61,9 @@ impl ExtendedTextFrame {
6161
let encoding = verify_encoding(encoding_byte, version)?;
6262
let description = decode_text(reader, encoding, true)?;
6363

64-
let mut frame_content;
64+
let frame_content;
6565
if encoding != TextEncoding::UTF16 {
6666
frame_content = decode_text(reader, encoding, false)?.content;
67-
trim_end_nulls(&mut frame_content);
6867

6968
return Ok(Some(ExtendedTextFrame {
7069
encoding,
@@ -95,7 +94,7 @@ impl ExtendedTextFrame {
9594
_ => unreachable!(),
9695
};
9796

98-
frame_content = utf16_decode(&raw_text, endianness).map_err(|_| {
97+
frame_content = utf16_decode_bytes(&raw_text, endianness).map_err(|_| {
9998
Into::<LoftyError>::into(Id3v2Error::new(Id3v2ErrorKind::BadSyncText))
10099
})?;
101100
}

src/id3/v2/items/extended_url_frame.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::error::Result;
22
use crate::id3::v2::frame::content::verify_encoding;
33
use crate::id3::v2::header::Id3v2Version;
4-
use crate::util::text::{decode_text, encode_text, trim_end_nulls, TextEncoding};
4+
use crate::util::text::{decode_text, encode_text, TextEncoding};
55

66
use std::hash::{Hash, Hasher};
77
use std::io::Read;
@@ -58,8 +58,7 @@ impl ExtendedUrlFrame {
5858

5959
let encoding = verify_encoding(encoding_byte, version)?;
6060
let description = decode_text(reader, encoding, true)?.content;
61-
let mut content = decode_text(reader, TextEncoding::Latin1, false)?.content;
62-
trim_end_nulls(&mut content);
61+
let content = decode_text(reader, TextEncoding::Latin1, false)?.content;
6362

6463
Ok(Some(ExtendedUrlFrame {
6564
encoding,

src/id3/v2/items/language_frame.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::error::{Id3v2Error, Id3v2ErrorKind, Result};
22
use crate::id3::v2::frame::content::verify_encoding;
33
use crate::id3::v2::header::Id3v2Version;
4-
use crate::util::text::{decode_text, encode_text, trim_end_nulls, TextEncoding};
4+
use crate::util::text::{decode_text, encode_text, TextEncoding};
55

66
use std::hash::{Hash, Hasher};
77
use std::io::Read;
@@ -33,8 +33,7 @@ impl LanguageFrame {
3333
reader.read_exact(&mut language)?;
3434

3535
let description = decode_text(reader, encoding, true)?.content;
36-
let mut content = decode_text(reader, encoding, false)?.content;
37-
trim_end_nulls(&mut content);
36+
let content = decode_text(reader, encoding, false)?.content;
3837

3938
Ok(Some(Self {
4039
encoding,

src/id3/v2/items/ownership_frame.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::error::{ErrorKind, Id3v2Error, Id3v2ErrorKind, LoftyError, Result};
2-
use crate::util::text::{decode_text, encode_text, TextEncoding};
2+
use crate::util::text::{decode_text, encode_text, utf8_decode, TextEncoding};
33

44
use std::hash::Hash;
55
use std::io::Read;
@@ -50,7 +50,7 @@ impl OwnershipFrame {
5050
let mut date_bytes = vec![0u8; 8];
5151
reader.read_exact(&mut date_bytes)?;
5252

53-
let date_of_purchase = String::from_utf8(date_bytes)?;
53+
let date_of_purchase = utf8_decode(date_bytes)?;
5454

5555
let seller = decode_text(reader, encoding, false)?.content;
5656

src/id3/v2/items/sync_text.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use crate::error::{ErrorKind, Id3v2Error, Id3v2ErrorKind, LoftyError, Result};
22
use crate::macros::err;
3-
use crate::util::text::{decode_text, encode_text, read_to_terminator, utf16_decode, TextEncoding};
3+
use crate::util::text::{
4+
decode_text, encode_text, read_to_terminator, utf16_decode_bytes, TextEncoding,
5+
};
46

57
use std::io::{Cursor, Read, Seek, SeekFrom, Write};
68

@@ -144,7 +146,7 @@ impl SynchronizedText {
144146
// text + null terminator
145147
pos += (raw_text.len() + 2) as u64;
146148

147-
return utf16_decode(&raw_text, endianness)
149+
return utf16_decode_bytes(&raw_text, endianness)
148150
.map_err(|_| Id3v2Error::new(Id3v2ErrorKind::BadSyncText).into());
149151
}
150152

src/id3/v2/items/text_information_frame.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::error::Result;
22
use crate::id3::v2::frame::content::verify_encoding;
33
use crate::id3::v2::header::Id3v2Version;
4-
use crate::util::text::{decode_text, encode_text, trim_end_nulls, TextEncoding};
4+
use crate::util::text::{decode_text, encode_text, TextEncoding};
55

66
use byteorder::ReadBytesExt;
77

@@ -37,8 +37,7 @@ impl TextInformationFrame {
3737
};
3838

3939
let encoding = verify_encoding(encoding_byte, version)?;
40-
let mut value = decode_text(reader, encoding, false)?.content;
41-
trim_end_nulls(&mut value);
40+
let value = decode_text(reader, encoding, false)?.content;
4241

4342
Ok(Some(TextInformationFrame { encoding, value }))
4443
}

src/iff/aiff/properties.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use super::read::CompressionPresent;
22
use crate::error::Result;
33
use crate::macros::{decode_err, try_vec};
44
use crate::properties::FileProperties;
5+
use crate::util::text::utf8_decode;
56

67
use std::borrow::Cow;
78
use std::io::Read;
@@ -234,7 +235,7 @@ pub(super) fn read_properties(
234235
let mut compression_name_bytes = try_vec![0u8; compression_name_size as usize];
235236
comm.read_exact(&mut compression_name_bytes)?;
236237

237-
compression_name = String::from_utf8(compression_name_bytes)?;
238+
compression_name = utf8_decode(compression_name_bytes)?;
238239
}
239240

240241
AiffCompressionType::Other {

src/iff/chunk.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::error::Result;
22
use crate::id3::v2::tag::Id3v2Tag;
33
use crate::macros::{err, try_vec};
44
use crate::probe::ParsingMode;
5+
use crate::util::text::utf8_decode;
56

67
use std::io::{Read, Seek, SeekFrom};
78
use std::marker::PhantomData;
@@ -67,7 +68,7 @@ impl<B: ByteOrder> Chunks<B> {
6768
data.seek(SeekFrom::Current(1))?;
6869
}
6970

70-
Ok(String::from_utf8(cont)?)
71+
utf8_decode(cont)
7172
}
7273

7374
pub fn content<R>(&mut self, data: &mut R) -> Result<Vec<u8>>

0 commit comments

Comments
 (0)