6b2dd0
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
f4c76c
From: Peter Jones <pjones@redhat.com>
f4c76c
Date: Mon, 1 Oct 2012 13:24:37 -0400
752ceb
Subject: [PATCH] Pass "\x[[:hex:]][[:hex:]]" straight through unmolested.
f4c76c
f4c76c
---
f4c76c
 grub-core/commands/wildcard.c | 16 +++++++++++++++-
6f1e3d
 grub-core/lib/cmdline.c       | 34 ++++++++++++++++++++++++++++++++--
f4c76c
 grub-core/script/execute.c    | 43 +++++++++++++++++++++++++++++++++++++------
6f1e3d
 3 files changed, 84 insertions(+), 9 deletions(-)
f4c76c
f4c76c
diff --git a/grub-core/commands/wildcard.c b/grub-core/commands/wildcard.c
ec4acb
index 9b4e72766ff..02c46f9fdfa 100644
f4c76c
--- a/grub-core/commands/wildcard.c
f4c76c
+++ b/grub-core/commands/wildcard.c
f4c76c
@@ -462,6 +462,12 @@ check_file (const char *dir, const char *basename)
f4c76c
   return ctx.found;
f4c76c
 }
f4c76c
 
f4c76c
+static int
f4c76c
+is_hex(char c)
f4c76c
+{
f4c76c
+  return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
f4c76c
+}
f4c76c
+
f4c76c
 static void
f4c76c
 unescape (char *out, const char *in, const char *end)
f4c76c
 {
f4c76c
@@ -470,7 +476,15 @@ unescape (char *out, const char *in, const char *end)
f4c76c
 
f4c76c
   for (optr = out, iptr = in; iptr < end;)
f4c76c
     {
f4c76c
-      if (*iptr == '\\' && iptr + 1 < end)
f4c76c
+      if (*iptr == '\\' && iptr + 3 < end && iptr[1] == 'x' && is_hex(iptr[2]) && is_hex(iptr[3]))
f4c76c
+	{
f4c76c
+	  *optr++ = *iptr++;
f4c76c
+	  *optr++ = *iptr++;
f4c76c
+	  *optr++ = *iptr++;
f4c76c
+	  *optr++ = *iptr++;
f4c76c
+	  continue;
f4c76c
+	}
f4c76c
+      else if (*iptr == '\\' && iptr + 1 < end)
f4c76c
 	{
f4c76c
 	  *optr++ = iptr[1];
f4c76c
 	  iptr += 2;
f4c76c
diff --git a/grub-core/lib/cmdline.c b/grub-core/lib/cmdline.c
ec4acb
index d5e10ee8798..0a5b2afb94b 100644
f4c76c
--- a/grub-core/lib/cmdline.c
f4c76c
+++ b/grub-core/lib/cmdline.c
f4c76c
@@ -20,6 +20,12 @@
f4c76c
 #include <grub lib="" cmdline.h="">
f4c76c
 #include <grub misc.h="">
f4c76c
 
f4c76c
+static int
f4c76c
+is_hex(char c)
f4c76c
+{
f4c76c
+  return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
f4c76c
+}
f4c76c
+
f4c76c
 static unsigned int check_arg (char *c, int *has_space)
f4c76c
 {
f4c76c
   int space = 0;
f4c76c
@@ -27,7 +33,13 @@ static unsigned int check_arg (char *c, int *has_space)
f4c76c
 
f4c76c
   while (*c)
f4c76c
     {
f4c76c
-      if (*c == '\\' || *c == '\'' || *c == '"')
f4c76c
+      if (*c == '\\' && *(c+1) == 'x' && is_hex(*(c+2)) && is_hex(*(c+3)))
f4c76c
+	{
f4c76c
+	  size += 4;
f4c76c
+	  c += 4;
f4c76c
+	  continue;
f4c76c
+	}
f4c76c
+      else if (*c == '\\' || *c == '\'' || *c == '"')
f4c76c
 	size++;
f4c76c
       else if (*c == ' ')
f4c76c
 	space = 1;
6f1e3d
@@ -85,7 +97,25 @@ int grub_create_loader_cmdline (int argc, char *argv[], char *buf,
f4c76c
 
f4c76c
       while (*c)
f4c76c
 	{
f4c76c
-	  if (*c == '\\' || *c == '\'' || *c == '"')
6f1e3d
+	  if (*c == ' ')
6f1e3d
+	    {
6f1e3d
+	      *buf++ = '\\';
6f1e3d
+	      *buf++ = 'x';
6f1e3d
+	      *buf++ = '2';
6f1e3d
+	      *buf++ = '0';
6f1e3d
+	      c++;
6f1e3d
+	      continue;
6f1e3d
+	    }
6f1e3d
+	  else if (*c == '\\' && *(c+1) == 'x' &&
f4c76c
+		   is_hex(*(c+2)) && is_hex(*(c+3)))
f4c76c
+	    {
f4c76c
+	      *buf++ = *c++;
f4c76c
+	      *buf++ = *c++;
f4c76c
+	      *buf++ = *c++;
f4c76c
+	      *buf++ = *c++;
f4c76c
+	      continue;
f4c76c
+	    }
f4c76c
+	  else if (*c == '\\' || *c == '\'' || *c == '"')
f4c76c
 	    *buf++ = '\\';
f4c76c
 
f4c76c
 	  *buf++ = *c;
f4c76c
diff --git a/grub-core/script/execute.c b/grub-core/script/execute.c
ec4acb
index ab78ca87f90..cf6cd6601d6 100644
f4c76c
--- a/grub-core/script/execute.c
f4c76c
+++ b/grub-core/script/execute.c
d9747d
@@ -55,6 +55,12 @@ static struct grub_script_scope *scope = 0;
f4c76c
 /* Wildcard translator for GRUB script.  */
f4c76c
 struct grub_script_wildcard_translator *grub_wildcard_translator;
f4c76c
 
f4c76c
+static int
f4c76c
+is_hex(char c)
f4c76c
+{
f4c76c
+  return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
f4c76c
+}
f4c76c
+
f4c76c
 static char*
f4c76c
 wildcard_escape (const char *s)
f4c76c
 {
d9747d
@@ -71,7 +77,15 @@ wildcard_escape (const char *s)
f4c76c
   i = 0;
f4c76c
   while ((ch = *s++))
f4c76c
     {
f4c76c
-      if (ch == '*' || ch == '\\' || ch == '?')
f4c76c
+      if (ch == '\\' && s[0] == 'x' && is_hex(s[1]) && is_hex(s[2]))
f4c76c
+	{
f4c76c
+	  p[i++] = ch;
f4c76c
+	  p[i++] = *s++;
f4c76c
+	  p[i++] = *s++;
f4c76c
+	  p[i++] = *s++;
f4c76c
+	  continue;
f4c76c
+	}
f4c76c
+      else if (ch == '*' || ch == '\\' || ch == '?')
f4c76c
 	p[i++] = '\\';
f4c76c
       p[i++] = ch;
f4c76c
     }
d9747d
@@ -95,7 +109,14 @@ wildcard_unescape (const char *s)
f4c76c
   i = 0;
f4c76c
   while ((ch = *s++))
f4c76c
     {
f4c76c
-      if (ch == '\\')
f4c76c
+      if (ch == '\\' && s[0] == 'x' && is_hex(s[1]) && is_hex(s[2]))
f4c76c
+	{
f4c76c
+	  p[i++] = '\\';
f4c76c
+	  p[i++] = *s++;
f4c76c
+	  p[i++] = *s++;
f4c76c
+	  p[i++] = *s++;
f4c76c
+	}
f4c76c
+      else if (ch == '\\')
f4c76c
 	p[i++] = *s++;
f4c76c
       else
f4c76c
 	p[i++] = ch;
d9747d
@@ -397,10 +418,20 @@ parse_string (const char *str,
f4c76c
     switch (*ptr)
f4c76c
       {
f4c76c
       case '\\':
f4c76c
-	escaped = !escaped;
f4c76c
-	if (!escaped && put)
f4c76c
-	  *(put++) = '\\';
f4c76c
-	ptr++;
f4c76c
+	if (!escaped && put && *(ptr+1) == 'x' && is_hex(*(ptr+2)) && is_hex(*(ptr+3)))
f4c76c
+	  {
f4c76c
+	    *(put++) = *ptr++;
f4c76c
+	    *(put++) = *ptr++;
f4c76c
+	    *(put++) = *ptr++;
f4c76c
+	    *(put++) = *ptr++;
f4c76c
+	  }
f4c76c
+	else
f4c76c
+	  {
f4c76c
+	    escaped = !escaped;
f4c76c
+	    if (!escaped && put)
f4c76c
+	      *(put++) = '\\';
f4c76c
+	    ptr++;
f4c76c
+	  }
f4c76c
 	break;
f4c76c
       case '$':
f4c76c
 	if (escaped)