Fixed base64::decode() return "" if input ends with garbase and no padding

pull/24/head
Tatsuhiro Tsujikawa 2012-07-04 22:51:41 +09:00
parent aa944f4ef6
commit 995c07c184
2 changed files with 8 additions and 1 deletions

View File

@ -133,7 +133,11 @@ std::string decode(InputIterator first, InputIterator last)
for(int i = 1; i <= 4; ++i) {
k[i] = getNext(first, last, INDEX_TABLE);
if(k[i] == last) {
// If i == 1, input may look like this: "TWFu\n" (i.e.,
// garbage at the end)
if(i != 1) {
res.clear();
}
return res;
} else if(*k[i] == '=' && eq == 0) {
eq = i;

View File

@ -76,6 +76,9 @@ void Base64Test::testDecode()
s = "TWFu";
CPPUNIT_ASSERT_EQUAL(std::string("Man"), base64::decode(s.begin(), s.end()));
s = "TWFu\n";
CPPUNIT_ASSERT_EQUAL(std::string("Man"), base64::decode(s.begin(), s.end()));
s = "TQ==";
CPPUNIT_ASSERT_EQUAL(std::string("M"), base64::decode(s.begin(), s.end()));