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

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