#include <tstring.h>
#include <tmap.h>
#include <tpropertymap.h>
+#include <tdebug.h>
#include "apetag.h"
#include "apefooter.h"
using namespace TagLib;
using namespace APE;
-namespace
-{
- inline bool isValidItemKey(const String &key)
- {
- for(String::ConstIterator it = key.begin(); it != key.end(); ++it) {
- const int c = static_cast<unsigned short>(*it);
- if(c < 0x20 || 0x7E < c)
- return false;
- }
-
- return true;
- }
-}
-
class APE::Tag::TagPrivate
{
public:
bool APE::Tag::checkKey(const String &key)
{
if(key.size() < 2 || key.size() > 16)
+ return false;
+
+ for(String::ConstIterator it = key.begin(); it != key.end(); it++) {
+ // only allow printable ASCII including space (32..126)
+ const int c = static_cast<unsigned short>(*it);
+ if(c < 32 || c > 126)
return false;
- for(String::ConstIterator it = key.begin(); it != key.end(); it++)
- // only allow printable ASCII including space (32..127)
- if (*it < 32 || *it >= 128)
- return false;
- String upperKey = key.upper();
- if (upperKey=="ID3" || upperKey=="TAG" || upperKey=="OGGS" || upperKey=="MP+")
- return false;
- return true;
+ }
+
+ const String upperKey = key.upper();
+ if(upperKey == "ID3" || upperKey == "TAG" || upperKey == "OGGS" || upperKey == "MP+")
+ return false;
+
+ return true;
}
APE::Footer *APE::Tag::footer() const
void APE::Tag::addValue(const String &key, const String &value, bool replace)
{
+ if(!checkKey(key)) {
+ debug("APE::Tag::addValue() - Couldn't add a value due to an invalid key.");
+ return;
+ }
+
if(replace)
removeItem(key);
- if(!key.isEmpty() && !value.isEmpty()) {
+
+ if(!value.isEmpty()) {
if(!replace && d->itemListMap.contains(key)) {
// Text items may contain more than one value
if(APE::Item::Text == d->itemListMap.begin()->second.type())
APE::Item item;
item.parse(data.mid(pos));
- if(isValidItemKey(item.key()))
+ if(checkKey(item.key())) {
d->itemListMap.insert(item.key().upper(), item);
+ }
+ else {
+ debug("APE::Tag::parse() - Skipped an item due to an invalid key.");
+ }
pos += item.size();
}
CPPUNIT_ASSERT_EQUAL((unsigned int)2, unsuccessful.size());
CPPUNIT_ASSERT(unsuccessful.contains("A"));
CPPUNIT_ASSERT(unsuccessful.contains("MP+"));
+
+ CPPUNIT_ASSERT_EQUAL((unsigned int)2, tag.itemListMap().size());
+ tag.addValue("VALID KEY", "Test Value 1");
+ tag.addValue("INVALID KEY \x7f", "Test Value 2");
+ CPPUNIT_ASSERT_EQUAL((unsigned int)3, tag.itemListMap().size());
}
void testTextBinary()