From f748b4760d0f8247001c0b46e9eaf02b379bc3c4 Mon Sep 17 00:00:00 2001 From: Michael Vogt Date: Mon, 25 Jul 2011 16:53:18 +0200 Subject: * apt-pkg/contrib/cdromutl.cc: - fix escape problem when looking for the mounted devices --- apt-pkg/contrib/cdromutl.cc | 8 +++++++- debian/changelog | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/apt-pkg/contrib/cdromutl.cc b/apt-pkg/contrib/cdromutl.cc index 821e6d688..551efa7d9 100644 --- a/apt-pkg/contrib/cdromutl.cc +++ b/apt-pkg/contrib/cdromutl.cc @@ -258,7 +258,13 @@ string FindMountPointForDevice(const char *devnode) if(TokSplitString(' ', buf, out, 10)) { fclose(f); - return string(out[1]); + // unescape \040 and return the path + size_t pos; + string mount_point = out[1]; + static const char *needle = "\\040"; + while ((pos = mount_point.find(needle)) != string::npos) + mount_point.replace(pos, strlen(needle), " "); + return mount_point; } } } diff --git a/debian/changelog b/debian/changelog index 418f37f4d..29a571fb9 100644 --- a/debian/changelog +++ b/debian/changelog @@ -4,6 +4,8 @@ apt (0.8.15.2) unstable; urgency=high code (LP: #784473) * cmdline/apt-get.cc: - fix missing download progress in apt-get download + * apt-pkg/contrib/cdromutl.cc: + - fix escape problem when looking for the mounted devices -- Michael Vogt Tue, 12 Jul 2011 11:54:47 +0200 -- cgit v1.2.3 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/cdromutl.cc | 8 ++---- apt-pkg/contrib/strutl.cc | 63 +++++++++++++++++++++++++++++++++++++++++++++ apt-pkg/contrib/strutl.h | 4 +++ debian/changelog | 3 +++ test/libapt/makefile | 6 +++++ test/libapt/strutil_test.cc | 40 ++++++++++++++++++++++++++++ 6 files changed, 118 insertions(+), 6 deletions(-) create mode 100644 test/libapt/strutil_test.cc diff --git a/apt-pkg/contrib/cdromutl.cc b/apt-pkg/contrib/cdromutl.cc index 551efa7d9..e25caf1a5 100644 --- a/apt-pkg/contrib/cdromutl.cc +++ b/apt-pkg/contrib/cdromutl.cc @@ -258,13 +258,9 @@ string FindMountPointForDevice(const char *devnode) if(TokSplitString(' ', buf, out, 10)) { fclose(f); - // unescape \040 and return the path - size_t pos; + // unescape the \0XXX chars in the path string mount_point = out[1]; - static const char *needle = "\\040"; - while ((pos = mount_point.find(needle)) != string::npos) - mount_point.replace(pos, strlen(needle), " "); - return mount_point; + return DeEscapeString(mount_point); } } } 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 */ diff --git a/apt-pkg/contrib/strutl.h b/apt-pkg/contrib/strutl.h index 89cbf0370..43bbfe366 100644 --- a/apt-pkg/contrib/strutl.h +++ b/apt-pkg/contrib/strutl.h @@ -39,6 +39,10 @@ bool ParseCWord(const char *&String,string &Res); string QuoteString(const string &Str,const char *Bad); string DeQuoteString(const string &Str); string DeQuoteString(string::const_iterator const &begin, string::const_iterator const &end); + +// unescape (\0XXX and \xXX) from a string +string DeEscapeString(string &input); + string SizeToStr(double Bytes); string TimeToStr(unsigned long Sec); string Base64Encode(const string &Str); diff --git a/debian/changelog b/debian/changelog index 29a571fb9..f620bd871 100644 --- a/debian/changelog +++ b/debian/changelog @@ -6,6 +6,9 @@ apt (0.8.15.2) unstable; urgency=high - fix missing download progress in apt-get download * apt-pkg/contrib/cdromutl.cc: - fix escape problem when looking for the mounted devices + * 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) -- Michael Vogt Tue, 12 Jul 2011 11:54:47 +0200 diff --git a/test/libapt/makefile b/test/libapt/makefile index 50058262e..fec928ad2 100644 --- a/test/libapt/makefile +++ b/test/libapt/makefile @@ -46,3 +46,9 @@ PROGRAM = GlobalError${BASENAME} SLIBS = -lapt-pkg SOURCE = globalerror_test.cc include $(PROGRAM_H) + +# test the strutils stuff +PROGRAM = StrUtil${BASENAME} +SLIBS = -lapt-pkg +SOURCE = strutil_test.cc +include $(PROGRAM_H) diff --git a/test/libapt/strutil_test.cc b/test/libapt/strutil_test.cc new file mode 100644 index 000000000..8d81a0c6c --- /dev/null +++ b/test/libapt/strutil_test.cc @@ -0,0 +1,40 @@ +#include + +#include "assert.h" + +int main(int argc,char *argv[]) +{ + string input, output, expected; + + // no input + input = "foobar"; + expected = "foobar"; + output = DeEscapeString(input); + equals(output, expected); + + // hex and octal + input = "foo\\040bar\\x0abaz"; + expected = "foo bar\nbaz"; + output = DeEscapeString(input); + equals(output, expected); + + // at the end + input = "foo\\040"; + expected = "foo "; + output = DeEscapeString(input); + equals(output, expected); + + // double escape + input = "foo\\\\ x"; + expected = "foo\\ x"; + output = DeEscapeString(input); + equals(output, expected); + + // double escape at the end + input = "\\\\foo\\\\"; + expected = "\\foo\\"; + output = DeEscapeString(input); + equals(output, expected); + + return 0; +} -- 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(-) 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 ++++----- apt-pkg/contrib/strutl.h | 2 +- test/libapt/strutil_test.cc | 6 ++++++ 3 files changed, 11 insertions(+), 6 deletions(-) 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; } diff --git a/apt-pkg/contrib/strutl.h b/apt-pkg/contrib/strutl.h index 43bbfe366..2a6bc1990 100644 --- a/apt-pkg/contrib/strutl.h +++ b/apt-pkg/contrib/strutl.h @@ -40,7 +40,7 @@ string QuoteString(const string &Str,const char *Bad); string DeQuoteString(const string &Str); string DeQuoteString(string::const_iterator const &begin, string::const_iterator const &end); -// unescape (\0XXX and \xXX) from a string +// unescape (\0XX and \xXX) from a string string DeEscapeString(string &input); string SizeToStr(double Bytes); diff --git a/test/libapt/strutil_test.cc b/test/libapt/strutil_test.cc index 8d81a0c6c..af6eb2cc6 100644 --- a/test/libapt/strutil_test.cc +++ b/test/libapt/strutil_test.cc @@ -36,5 +36,11 @@ int main(int argc,char *argv[]) output = DeEscapeString(input); equals(output, expected); + // the string that we actually need it for + input = "/media/Ubuntu\\04011.04\\040amd64"; + expected = "/media/Ubuntu 11.04 amd64"; + output = DeEscapeString(input); + equals(output, expected); + return 0; } -- 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 +++--- apt-pkg/contrib/strutl.h | 2 +- debian/changelog | 5 +++-- 3 files changed, 7 insertions(+), 6 deletions(-) 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; diff --git a/apt-pkg/contrib/strutl.h b/apt-pkg/contrib/strutl.h index 2a6bc1990..fba85cf94 100644 --- a/apt-pkg/contrib/strutl.h +++ b/apt-pkg/contrib/strutl.h @@ -41,7 +41,7 @@ string DeQuoteString(const string &Str); string DeQuoteString(string::const_iterator const &begin, string::const_iterator const &end); // unescape (\0XX and \xXX) from a string -string DeEscapeString(string &input); +string DeEscapeString(const string &input); string SizeToStr(double Bytes); string TimeToStr(unsigned long Sec); diff --git a/debian/changelog b/debian/changelog index f620bd871..0f8035a78 100644 --- a/debian/changelog +++ b/debian/changelog @@ -7,8 +7,9 @@ apt (0.8.15.2) unstable; urgency=high * apt-pkg/contrib/cdromutl.cc: - fix escape problem when looking for the mounted devices * 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) + - add new DeEscapeString() similar to DeQuoteString but + unescape character escapes like \0XX and \xXX (plus added + test) -- Michael Vogt Tue, 12 Jul 2011 11:54:47 +0200 -- cgit v1.2.3