From a513ace2d2e3b71d607257990893199c6105b072 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Tue, 26 Jul 2011 10:49:28 +0200 Subject: * apt-pkg/contrib/strutl.{h,cc}, test/libapt/strutil_test.cc: - add new DeEscapeString() similar to DeQuoteQuotedWord but unescape charackter escapes like \0XXX and \xXX (plus add test) --- apt-pkg/contrib/strutl.cc | 63 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) (limited to 'apt-pkg/contrib/strutl.cc') diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index 072dda3ac..a97dd30e5 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -1240,7 +1240,70 @@ bool CheckDomainList(const string &Host,const string &List) return false; } /*}}}*/ +// ProcessEscapeSequences /*{{{*/ +// --------------------------------------------------------------------- +/* */ +string DeEscapeString(string &input) +{ + char tmp[5]; + string::const_iterator it, escape_start; + string output, octal, hex; + for (it = input.begin(); it != input.end(); it++) + { + // just copy non-escape chars + if (*it != '\\') + { + output += *it; + continue; + } + + // deal with double escape + if (*it == '\\' && + (it + 1 < input.end()) && it[1] == '\\') + { + // copy + output += *it; + // advance iterator one step further + it += 1; + continue; + } + + // ensure we have a char to read + if (it + 1 == input.end()) + continue; + // read it + it++; + switch (*it) + { + case '0': + if (it + 3 <= input.end()) { + tmp[0] = it[1]; + tmp[1] = it[2]; + tmp[2] = it[3]; + tmp[3] = 0; + output += (char)strtol(tmp, 0, 8); + it += 2; + } + break; + case 'x': + if (it + 2 <= input.end()) { + tmp[0] = it[1]; + tmp[1] = it[2]; + tmp[2] = 0; + output += (char)strtol(tmp, 0, 16); + it += 2; + } + break; + default: + // FIXME: raise exception here? + std::cerr << "lala" << *it << endl; + break; + } + } + return output; +} + /*}}}*/ // URI::CopyFrom - Copy from an object /*{{{*/ // --------------------------------------------------------------------- /* This parses the URI into all of its components */ -- cgit v1.2.3 From e0146bb1813743e9042c2aad4d1131ca382861c1 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Tue, 26 Jul 2011 10:50:43 +0200 Subject: apt-pkg/contrib/strutl.cc: kill unneeded debug output --- apt-pkg/contrib/strutl.cc | 1 - 1 file changed, 1 deletion(-) (limited to 'apt-pkg/contrib/strutl.cc') diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index a97dd30e5..f9d8d7e90 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -1297,7 +1297,6 @@ string DeEscapeString(string &input) break; default: // FIXME: raise exception here? - std::cerr << "lala" << *it << endl; break; } } -- cgit v1.2.3 From b9dc47069ab90a79ca3b9eae3cc85d38062d57ee Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Tue, 26 Jul 2011 11:10:47 +0200 Subject: add another escape test case, fixup octal one (its \0XX instead of \0XXX) --- apt-pkg/contrib/strutl.cc | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'apt-pkg/contrib/strutl.cc') diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index f9d8d7e90..e998d74dc 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -1242,10 +1242,10 @@ bool CheckDomainList(const string &Host,const string &List) /*}}}*/ // ProcessEscapeSequences /*{{{*/ // --------------------------------------------------------------------- -/* */ +/* unescape (\0XX and \xXX) from a string */ string DeEscapeString(string &input) { - char tmp[5]; + char tmp[3]; string::const_iterator it, escape_start; string output, octal, hex; for (it = input.begin(); it != input.end(); it++) @@ -1277,11 +1277,10 @@ string DeEscapeString(string &input) switch (*it) { case '0': - if (it + 3 <= input.end()) { + if (it + 2 <= input.end()) { tmp[0] = it[1]; tmp[1] = it[2]; - tmp[2] = it[3]; - tmp[3] = 0; + tmp[2] = 0; output += (char)strtol(tmp, 0, 8); it += 2; } -- cgit v1.2.3 From cca2efe60edfae106380a55ee4fb561aa570f2c9 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Tue, 26 Jul 2011 11:52:42 +0200 Subject: fix typos in changelog, make DeEscapeString const, improve description --- apt-pkg/contrib/strutl.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'apt-pkg/contrib/strutl.cc') diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index e998d74dc..ab2da2d9a 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -1240,10 +1240,10 @@ bool CheckDomainList(const string &Host,const string &List) return false; } /*}}}*/ -// ProcessEscapeSequences /*{{{*/ +// DeEscapeString - unescape (\0XX and \xXX) from a string /*{{{*/ // --------------------------------------------------------------------- -/* unescape (\0XX and \xXX) from a string */ -string DeEscapeString(string &input) +/* */ +string DeEscapeString(const string &input) { char tmp[3]; string::const_iterator it, escape_start; -- cgit v1.2.3