Blame SOURCES/0020-Pass-x-hex-hex-straight-through-unmolested.patch

5593c8
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
5593c8
From: Peter Jones <pjones@redhat.com>
5593c8
Date: Mon, 1 Oct 2012 13:24:37 -0400
5593c8
Subject: [PATCH] Pass "\x[[:hex:]][[:hex:]]" straight through unmolested.
5593c8
5593c8
Don't munge raw spaces when we're doing our cmdline escaping (#923374)
5593c8
5593c8
Signed-off-by: Peter Jones <pjones@redhat.com>
5593c8
---
5593c8
 grub-core/commands/wildcard.c | 16 +++++++++++++++-
5593c8
 grub-core/lib/cmdline.c       | 25 +++++++++++++++++++++++--
5593c8
 grub-core/script/execute.c    | 43 +++++++++++++++++++++++++++++++++++++------
5593c8
 3 files changed, 75 insertions(+), 9 deletions(-)
5593c8
5593c8
diff --git a/grub-core/commands/wildcard.c b/grub-core/commands/wildcard.c
5593c8
index cc3290311f0..8f67a4be7f0 100644
5593c8
--- a/grub-core/commands/wildcard.c
5593c8
+++ b/grub-core/commands/wildcard.c
5593c8
@@ -488,6 +488,12 @@ check_file (const char *dir, const char *basename)
5593c8
   return ctx.found;
5593c8
 }
5593c8
 
5593c8
+static int
5593c8
+is_hex(char c)
5593c8
+{
5593c8
+  return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
5593c8
+}
5593c8
+
5593c8
 static void
5593c8
 unescape (char *out, const char *in, const char *end)
5593c8
 {
5593c8
@@ -496,7 +502,15 @@ unescape (char *out, const char *in, const char *end)
5593c8
 
5593c8
   for (optr = out, iptr = in; iptr < end;)
5593c8
     {
5593c8
-      if (*iptr == '\\' && iptr + 1 < end)
5593c8
+      if (*iptr == '\\' && iptr + 3 < end && iptr[1] == 'x' && is_hex(iptr[2]) && is_hex(iptr[3]))
5593c8
+	{
5593c8
+	  *optr++ = *iptr++;
5593c8
+	  *optr++ = *iptr++;
5593c8
+	  *optr++ = *iptr++;
5593c8
+	  *optr++ = *iptr++;
5593c8
+	  continue;
5593c8
+	}
5593c8
+      else if (*iptr == '\\' && iptr + 1 < end)
5593c8
 	{
5593c8
 	  *optr++ = iptr[1];
5593c8
 	  iptr += 2;
5593c8
diff --git a/grub-core/lib/cmdline.c b/grub-core/lib/cmdline.c
5593c8
index ed0b149dca5..8e2294d8ff6 100644
5593c8
--- a/grub-core/lib/cmdline.c
5593c8
+++ b/grub-core/lib/cmdline.c
5593c8
@@ -20,6 +20,12 @@
5593c8
 #include <grub/lib/cmdline.h>
5593c8
 #include <grub/misc.h>
5593c8
 
5593c8
+static int
5593c8
+is_hex(char c)
5593c8
+{
5593c8
+  return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
5593c8
+}
5593c8
+
5593c8
 static unsigned int check_arg (char *c, int *has_space)
5593c8
 {
5593c8
   int space = 0;
5593c8
@@ -27,7 +33,13 @@ static unsigned int check_arg (char *c, int *has_space)
5593c8
 
5593c8
   while (*c)
5593c8
     {
5593c8
-      if (*c == '\\' || *c == '\'' || *c == '"')
5593c8
+      if (*c == '\\' && *(c+1) == 'x' && is_hex(*(c+2)) && is_hex(*(c+3)))
5593c8
+	{
5593c8
+	  size += 4;
5593c8
+	  c += 4;
5593c8
+	  continue;
5593c8
+	}
5593c8
+      else if (*c == '\\' || *c == '\'' || *c == '"')
5593c8
 	size++;
5593c8
       else if (*c == ' ')
5593c8
 	space = 1;
5593c8
@@ -86,7 +98,16 @@ grub_create_loader_cmdline (int argc, char *argv[], char *buf,
5593c8
 
5593c8
       while (*c)
5593c8
 	{
5593c8
-	  if (*c == '\\' || *c == '\'' || *c == '"')
5593c8
+	  if (*c == '\\' && *(c+1) == 'x' &&
5593c8
+		   is_hex(*(c+2)) && is_hex(*(c+3)))
5593c8
+	    {
5593c8
+	      *buf++ = *c++;
5593c8
+	      *buf++ = *c++;
5593c8
+	      *buf++ = *c++;
5593c8
+	      *buf++ = *c++;
5593c8
+	      continue;
5593c8
+	    }
5593c8
+	  else if (*c == '\\' || *c == '\'' || *c == '"')
5593c8
 	    *buf++ = '\\';
5593c8
 
5593c8
 	  *buf++ = *c;
5593c8
diff --git a/grub-core/script/execute.c b/grub-core/script/execute.c
5593c8
index ad80399246a..0c6dd9c5201 100644
5593c8
--- a/grub-core/script/execute.c
5593c8
+++ b/grub-core/script/execute.c
5593c8
@@ -56,6 +56,12 @@ static struct grub_script_scope *scope = 0;
5593c8
 /* Wildcard translator for GRUB script.  */
5593c8
 struct grub_script_wildcard_translator *grub_wildcard_translator;
5593c8
 
5593c8
+static int
5593c8
+is_hex(char c)
5593c8
+{
5593c8
+  return ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
5593c8
+}
5593c8
+
5593c8
 static char*
5593c8
 wildcard_escape (const char *s)
5593c8
 {
5593c8
@@ -72,7 +78,15 @@ wildcard_escape (const char *s)
5593c8
   i = 0;
5593c8
   while ((ch = *s++))
5593c8
     {
5593c8
-      if (ch == '*' || ch == '\\' || ch == '?')
5593c8
+      if (ch == '\\' && s[0] == 'x' && is_hex(s[1]) && is_hex(s[2]))
5593c8
+	{
5593c8
+	  p[i++] = ch;
5593c8
+	  p[i++] = *s++;
5593c8
+	  p[i++] = *s++;
5593c8
+	  p[i++] = *s++;
5593c8
+	  continue;
5593c8
+	}
5593c8
+      else if (ch == '*' || ch == '\\' || ch == '?')
5593c8
 	p[i++] = '\\';
5593c8
       p[i++] = ch;
5593c8
     }
5593c8
@@ -96,7 +110,14 @@ wildcard_unescape (const char *s)
5593c8
   i = 0;
5593c8
   while ((ch = *s++))
5593c8
     {
5593c8
-      if (ch == '\\')
5593c8
+      if (ch == '\\' && s[0] == 'x' && is_hex(s[1]) && is_hex(s[2]))
5593c8
+	{
5593c8
+	  p[i++] = '\\';
5593c8
+	  p[i++] = *s++;
5593c8
+	  p[i++] = *s++;
5593c8
+	  p[i++] = *s++;
5593c8
+	}
5593c8
+      else if (ch == '\\')
5593c8
 	p[i++] = *s++;
5593c8
       else
5593c8
 	p[i++] = ch;
5593c8
@@ -398,10 +419,20 @@ parse_string (const char *str,
5593c8
     switch (*ptr)
5593c8
       {
5593c8
       case '\\':
5593c8
-	escaped = !escaped;
5593c8
-	if (!escaped && put)
5593c8
-	  *(put++) = '\\';
5593c8
-	ptr++;
5593c8
+	if (!escaped && put && *(ptr+1) == 'x' && is_hex(*(ptr+2)) && is_hex(*(ptr+3)))
5593c8
+	  {
5593c8
+	    *(put++) = *ptr++;
5593c8
+	    *(put++) = *ptr++;
5593c8
+	    *(put++) = *ptr++;
5593c8
+	    *(put++) = *ptr++;
5593c8
+	  }
5593c8
+	else
5593c8
+	  {
5593c8
+	    escaped = !escaped;
5593c8
+	    if (!escaped && put)
5593c8
+	      *(put++) = '\\';
5593c8
+	    ptr++;
5593c8
+	  }
5593c8
 	break;
5593c8
       case '$':
5593c8
 	if (escaped)