* apt-pkg/contrib/strutl.{h,cc}, test/libapt/strutil_test.cc:
authorMichael Vogt <michael.vogt@ubuntu.com>
Tue, 26 Jul 2011 08:49:28 +0000 (10:49 +0200)
committerMichael Vogt <michael.vogt@ubuntu.com>
Tue, 26 Jul 2011 08:49:28 +0000 (10:49 +0200)
  - add new DeEscapeString() similar to DeQuoteQuotedWord but
    unescape charackter escapes like \0XXX and \xXX (plus add test)

apt-pkg/contrib/cdromutl.cc
apt-pkg/contrib/strutl.cc
apt-pkg/contrib/strutl.h
debian/changelog
test/libapt/makefile
test/libapt/strutil_test.cc [new file with mode: 0644]

index 551efa7..e25caf1 100644 (file)
@@ -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);
                }
             }
          }
index 072dda3..a97dd30 100644 (file)
@@ -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 */
index 89cbf03..43bbfe3 100644 (file)
@@ -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);
index 29a571f..f620bd8 100644 (file)
@@ -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 <mvo@debian.org>  Tue, 12 Jul 2011 11:54:47 +0200
 
index 5005826..fec928a 100644 (file)
@@ -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 (file)
index 0000000..8d81a0c
--- /dev/null
@@ -0,0 +1,40 @@
+#include <apt-pkg/strutl.h>
+
+#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;
+}