011fe81
From a4f335a93d417c7f51f715d55ae59621fc9772ad Mon Sep 17 00:00:00 2001
481bf3b
From: Peter Jones <pjones@redhat.com>
481bf3b
Date: Mon, 1 Oct 2012 13:24:37 -0400
58fe9aa
Subject: [PATCH 094/152] Pass "\x[[:hex:]][[:hex:]]" straight through
481bf3b
 unmolested.
481bf3b
481bf3b
---
481bf3b
 grub-core/commands/wildcard.c | 16 +++++++++++++++-
481bf3b
 grub-core/lib/cmdline.c       | 34 ++++++++++++++++++++++++++++++++--
481bf3b
 grub-core/script/execute.c    | 43 +++++++++++++++++++++++++++++++++++++------
481bf3b
 3 files changed, 84 insertions(+), 9 deletions(-)
481bf3b
481bf3b
diff --git a/grub-core/commands/wildcard.c b/grub-core/commands/wildcard.c
481bf3b
index 2807f80..0f40e04 100644
481bf3b
--- a/grub-core/commands/wildcard.c
481bf3b
+++ b/grub-core/commands/wildcard.c
481bf3b
@@ -458,6 +458,12 @@ check_file (const char *dir, const char *basename)
481bf3b
   return ctx.found;
481bf3b
 }
481bf3b
 
481bf3b
+static int
481bf3b
+is_hex(char c)
481bf3b
+{
481bf3b
+  return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
481bf3b
+}
481bf3b
+
481bf3b
 static void
481bf3b
 unescape (char *out, const char *in, const char *end)
481bf3b
 {
481bf3b
@@ -466,7 +472,15 @@ unescape (char *out, const char *in, const char *end)
481bf3b
 
481bf3b
   for (optr = out, iptr = in; iptr < end;)
481bf3b
     {
481bf3b
-      if (*iptr == '\\' && iptr + 1 < end)
481bf3b
+      if (*iptr == '\\' && iptr + 3 < end && iptr[1] == 'x' && is_hex(iptr[2]) && is_hex(iptr[3]))
481bf3b
+	{
481bf3b
+	  *optr++ = *iptr++;
481bf3b
+	  *optr++ = *iptr++;
481bf3b
+	  *optr++ = *iptr++;
481bf3b
+	  *optr++ = *iptr++;
481bf3b
+	  continue;
481bf3b
+	}
481bf3b
+      else if (*iptr == '\\' && iptr + 1 < end)
481bf3b
 	{
481bf3b
 	  *optr++ = iptr[1];
481bf3b
 	  iptr += 2;
481bf3b
diff --git a/grub-core/lib/cmdline.c b/grub-core/lib/cmdline.c
481bf3b
index d5e10ee..0a5b2af 100644
481bf3b
--- a/grub-core/lib/cmdline.c
481bf3b
+++ b/grub-core/lib/cmdline.c
481bf3b
@@ -20,6 +20,12 @@
481bf3b
 #include <grub/lib/cmdline.h>
481bf3b
 #include <grub/misc.h>
481bf3b
 
481bf3b
+static int
481bf3b
+is_hex(char c)
481bf3b
+{
481bf3b
+  return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
481bf3b
+}
481bf3b
+
481bf3b
 static unsigned int check_arg (char *c, int *has_space)
481bf3b
 {
481bf3b
   int space = 0;
481bf3b
@@ -27,7 +33,13 @@ static unsigned int check_arg (char *c, int *has_space)
481bf3b
 
481bf3b
   while (*c)
481bf3b
     {
481bf3b
-      if (*c == '\\' || *c == '\'' || *c == '"')
481bf3b
+      if (*c == '\\' && *(c+1) == 'x' && is_hex(*(c+2)) && is_hex(*(c+3)))
481bf3b
+	{
481bf3b
+	  size += 4;
481bf3b
+	  c += 4;
481bf3b
+	  continue;
481bf3b
+	}
481bf3b
+      else if (*c == '\\' || *c == '\'' || *c == '"')
481bf3b
 	size++;
481bf3b
       else if (*c == ' ')
481bf3b
 	space = 1;
481bf3b
@@ -85,7 +97,25 @@ int grub_create_loader_cmdline (int argc, char *argv[], char *buf,
481bf3b
 
481bf3b
       while (*c)
481bf3b
 	{
481bf3b
-	  if (*c == '\\' || *c == '\'' || *c == '"')
481bf3b
+	  if (*c == ' ')
481bf3b
+	    {
481bf3b
+	      *buf++ = '\\';
481bf3b
+	      *buf++ = 'x';
481bf3b
+	      *buf++ = '2';
481bf3b
+	      *buf++ = '0';
481bf3b
+	      c++;
481bf3b
+	      continue;
481bf3b
+	    }
481bf3b
+	  else if (*c == '\\' && *(c+1) == 'x' &&
481bf3b
+		   is_hex(*(c+2)) && is_hex(*(c+3)))
481bf3b
+	    {
481bf3b
+	      *buf++ = *c++;
481bf3b
+	      *buf++ = *c++;
481bf3b
+	      *buf++ = *c++;
481bf3b
+	      *buf++ = *c++;
481bf3b
+	      continue;
481bf3b
+	    }
481bf3b
+	  else if (*c == '\\' || *c == '\'' || *c == '"')
481bf3b
 	    *buf++ = '\\';
481bf3b
 
481bf3b
 	  *buf++ = *c;
481bf3b
diff --git a/grub-core/script/execute.c b/grub-core/script/execute.c
481bf3b
index afd5513..8f01c1b 100644
481bf3b
--- a/grub-core/script/execute.c
481bf3b
+++ b/grub-core/script/execute.c
481bf3b
@@ -52,6 +52,12 @@ static struct grub_script_scope *scope = 0;
481bf3b
 /* Wildcard translator for GRUB script.  */
481bf3b
 struct grub_script_wildcard_translator *grub_wildcard_translator;
481bf3b
 
481bf3b
+static int
481bf3b
+is_hex(char c)
481bf3b
+{
481bf3b
+  return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
481bf3b
+}
481bf3b
+
481bf3b
 static char*
481bf3b
 wildcard_escape (const char *s)
481bf3b
 {
481bf3b
@@ -68,7 +74,15 @@ wildcard_escape (const char *s)
481bf3b
   i = 0;
481bf3b
   while ((ch = *s++))
481bf3b
     {
481bf3b
-      if (ch == '*' || ch == '\\' || ch == '?')
481bf3b
+      if (ch == '\\' && s[0] == 'x' && is_hex(s[1]) && is_hex(s[2]))
481bf3b
+	{
481bf3b
+	  p[i++] = ch;
481bf3b
+	  p[i++] = *s++;
481bf3b
+	  p[i++] = *s++;
481bf3b
+	  p[i++] = *s++;
481bf3b
+	  continue;
481bf3b
+	}
481bf3b
+      else if (ch == '*' || ch == '\\' || ch == '?')
481bf3b
 	p[i++] = '\\';
481bf3b
       p[i++] = ch;
481bf3b
     }
481bf3b
@@ -92,7 +106,14 @@ wildcard_unescape (const char *s)
481bf3b
   i = 0;
481bf3b
   while ((ch = *s++))
481bf3b
     {
481bf3b
-      if (ch == '\\')
481bf3b
+      if (ch == '\\' && s[0] == 'x' && is_hex(s[1]) && is_hex(s[2]))
481bf3b
+	{
481bf3b
+	  p[i++] = '\\';
481bf3b
+	  p[i++] = *s++;
481bf3b
+	  p[i++] = *s++;
481bf3b
+	  p[i++] = *s++;
481bf3b
+	}
481bf3b
+      else if (ch == '\\')
481bf3b
 	p[i++] = *s++;
481bf3b
       else
481bf3b
 	p[i++] = ch;
481bf3b
@@ -394,10 +415,20 @@ parse_string (const char *str,
481bf3b
     switch (*ptr)
481bf3b
       {
481bf3b
       case '\\':
481bf3b
-	escaped = !escaped;
481bf3b
-	if (!escaped && put)
481bf3b
-	  *(put++) = '\\';
481bf3b
-	ptr++;
481bf3b
+	if (!escaped && put && *(ptr+1) == 'x' && is_hex(*(ptr+2)) && is_hex(*(ptr+3)))
481bf3b
+	  {
481bf3b
+	    *(put++) = *ptr++;
481bf3b
+	    *(put++) = *ptr++;
481bf3b
+	    *(put++) = *ptr++;
481bf3b
+	    *(put++) = *ptr++;
481bf3b
+	  }
481bf3b
+	else
481bf3b
+	  {
481bf3b
+	    escaped = !escaped;
481bf3b
+	    if (!escaped && put)
481bf3b
+	      *(put++) = '\\';
481bf3b
+	    ptr++;
481bf3b
+	  }
481bf3b
 	break;
481bf3b
       case '$':
481bf3b
 	if (escaped)
481bf3b
-- 
37b39b7
1.9.3
481bf3b