Make sure we don't write UTF8 or UTF16BE to ID3v2.3 tags

This commit is contained in:
Lukáš Lalinský
2011-03-15 21:57:49 +01:00
parent 1802237c75
commit 061b381ea8
7 changed files with 64 additions and 7 deletions

View File

@@ -152,7 +152,7 @@ ByteVector AttachedPictureFrame::renderFields() const
{
ByteVector data;
String::Type encoding = checkEncoding(d->description, d->textEncoding);
String::Type encoding = checkTextEncoding(d->description, d->textEncoding);
data.append(char(encoding));
data.append(d->mimeType.data(String::Latin1));

View File

@@ -155,8 +155,8 @@ ByteVector CommentsFrame::renderFields() const
String::Type encoding = d->textEncoding;
encoding = checkEncoding(d->description, encoding);
encoding = checkEncoding(d->text, encoding);
encoding = checkTextEncoding(d->description, encoding);
encoding = checkTextEncoding(d->text, encoding);
v.append(char(encoding));
v.append(d->language.size() == 3 ? d->language : "XXX");

View File

@@ -139,7 +139,7 @@ void TextIdentificationFrame::parseFields(const ByteVector &data)
ByteVector TextIdentificationFrame::renderFields() const
{
String::Type encoding = checkEncoding(d->fieldList, d->textEncoding);
String::Type encoding = checkTextEncoding(d->fieldList, d->textEncoding);
ByteVector v;

View File

@@ -175,7 +175,7 @@ ByteVector UserUrlLinkFrame::renderFields() const
{
ByteVector v;
String::Type encoding = checkEncoding(d->description, d->textEncoding);
String::Type encoding = checkTextEncoding(d->description, d->textEncoding);
v.append(char(encoding));
v.append(d->description.data(encoding));

View File

@@ -230,19 +230,38 @@ String Frame::readStringField(const ByteVector &data, String::Type encoding, int
String::Type Frame::checkEncoding(const StringList &fields, String::Type encoding) // static
{
return checkEncoding(fields, encoding, 4);
}
String::Type Frame::checkEncoding(const StringList &fields, String::Type encoding, uint version) // static
{
if((encoding == String::UTF8 || encoding == String::UTF16BE) && version != 4)
return String::UTF16;
if(encoding != String::Latin1)
return encoding;
for(StringList::ConstIterator it = fields.begin(); it != fields.end(); ++it) {
if(!(*it).isLatin1()) {
debug("Frame::checkEncoding() -- Rendering using UTF8.");
return String::UTF8;
if(version == 4) {
debug("Frame::checkEncoding() -- Rendering using UTF8.");
return String::UTF8;
}
else {
debug("Frame::checkEncoding() -- Rendering using UTF16.");
return String::UTF16;
}
}
}
return String::Latin1;
}
String::Type Frame::checkTextEncoding(const StringList &fields, String::Type encoding) const
{
return checkEncoding(fields, encoding, header()->version());
}
////////////////////////////////////////////////////////////////////////////////
// Frame::Header class
////////////////////////////////////////////////////////////////////////////////

View File

@@ -199,9 +199,29 @@ namespace TagLib {
* Checks a the list of string values to see if they can be used with the
* specified encoding and returns the recommended encoding.
*/
// BIC: remove and make non-static
static String::Type checkEncoding(const StringList &fields,
String::Type encoding);
/*!
* Checks a the list of string values to see if they can be used with the
* specified encoding and returns the recommended encoding. This method
* also checks the ID3v2 version and makes sure the encoding can be used
* in the specified version.
*/
// BIC: remove and make non-static
static String::Type checkEncoding(const StringList &fields,
String::Type encoding, uint version);
/*!
* Checks a the list of string values to see if they can be used with the
* specified encoding and returns the recommended encoding. This method
* also checks the ID3v2 version and makes sure the encoding can be used
* in the version specified by the frame's header.
*/
String::Type checkTextEncoding(const StringList &fields,
String::Type encoding) const;
private:
Frame(const Frame &);
Frame &operator=(const Frame &);