From: Michael Andres Date: Thu, 5 Sep 2013 14:53:57 +0000 (+0200) Subject: Fix string hexdecoding X-Git-Tag: upstream/14.27.0~231 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=5fd3f3ba383aa9a720253c376d958728980856f7;p=platform%2Fupstream%2Flibzypp.git Fix string hexdecoding --- diff --git a/tests/zypp/base/String_test.cc b/tests/zypp/base/String_test.cc index e01bb00..0d42046 100644 --- a/tests/zypp/base/String_test.cc +++ b/tests/zypp/base/String_test.cc @@ -276,8 +276,30 @@ BOOST_AUTO_TEST_CASE(hexencode_hexdecode) } std::string d( str::hexdecode( e ) ); + // decoded equals original BOOST_CHECK( o == d ); -// for ( unsigned i = 0; i < 255; ++i ) -// if ( o[i] != d[i] ) -// WAR << i << " " << unsigned(o[i]) << " != " << unsigned(d[i]) << endl; + + // Test %XX is decoded for hexdigits only + const char *const dig = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + for ( const char * d1 = dig; *d1; ++d1 ) + for ( const char * d2 = dig; *d2; ++d2 ) + { + std::string eu( "%" ); + eu += *d1; eu += *d2; + std::string el( str::toLower(eu) ); + + std::string u( str::hexdecode( eu ) ); + std::string l( str::hexdecode( el ) ); + + if ( *d1 <= 'F' && *d2 <= 'F' ) + { + BOOST_CHECK_EQUAL( u, l ); // no matter if upper or lower case hexdigit + BOOST_CHECK_EQUAL( u.size(), 1 ); // size 1 == decoded + } + else + { + BOOST_CHECK_EQUAL( u, eu ); // no hexdigits remain unchanged + BOOST_CHECK_EQUAL( l, el ); + } + } } diff --git a/zypp/base/String.cc b/zypp/base/String.cc index f332d3f..8ec8d6e 100644 --- a/zypp/base/String.cc +++ b/zypp/base/String.cc @@ -102,10 +102,10 @@ namespace zypp { if ( '0' <= ch && ch <= '9' ) return( ch - '0' ); - if ( 'A' <= ch && ch <= 'Z' ) - return( ch - 'A' + 10 ); - if ( 'a' <= ch && ch <= 'z' ) + if ( 'A' <= ch && ch <= 'F' ) return( ch - 'A' + 10 ); + if ( 'a' <= ch && ch <= 'f' ) + return( ch - 'a' + 10 ); return -1; } }