Allow windows "C:" style filenames when looking for user-manual
[privoxy.git] / pcrs.c
diff --git a/pcrs.c b/pcrs.c
index 657a286..a6c521a 100644 (file)
--- a/pcrs.c
+++ b/pcrs.c
@@ -1,5 +1,4 @@
-const char pcrs_rcs[] = "$Id: pcrs.c,v 1.24 2007/01/05 15:46:12 fabiankeil Exp $";
-
+const char pcrs_rcs[] = "$Id: pcrs.c,v 1.28 2007/08/18 14:37:27 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/pcrs.c,v $
@@ -38,6 +37,19 @@ const char pcrs_rcs[] = "$Id: pcrs.c,v 1.24 2007/01/05 15:46:12 fabiankeil Exp $
  *
  * Revisions   :
  *    $Log: pcrs.c,v $
+ *    Revision 1.28  2007/08/18 14:37:27  fabiankeil
+ *    Ditch hex_to_byte() in favour of xtoi().
+ *
+ *    Revision 1.27  2007/08/05 13:47:04  fabiankeil
+ *    #1763173 from Stefan Huehner: s@const static@static const@.
+ *
+ *    Revision 1.26  2007/07/01 13:29:54  fabiankeil
+ *    Add limited hex notation support for the PCRS
+ *    substitution text ('\x7e' = '~'). Closes #1627140.
+ *
+ *    Revision 1.25  2007/04/30 15:02:18  fabiankeil
+ *    Introduce dynamic pcrs jobs that can resolve variables.
+ *
  *    Revision 1.24  2007/01/05 15:46:12  fabiankeil
  *    Don't use strlen() to calculate the length of
  *    the pcrs substitutes. They don't have to be valid C
@@ -171,6 +183,10 @@ const char pcrs_rcs[] = "$Id: pcrs.c,v 1.24 2007/01/05 15:46:12 fabiankeil Exp $
  *********************************************************************/
 \f
 
+#include <string.h>
+#include <ctype.h>
+#include <assert.h>
+
 /*
  * Include project.h just so that the right pcre.h gets
  * included from there
@@ -179,10 +195,8 @@ const char pcrs_rcs[] = "$Id: pcrs.c,v 1.24 2007/01/05 15:46:12 fabiankeil Exp $
 
 /* For snprintf only */
 #include "miscutil.h"
-
-#include <string.h>
-#include <ctype.h>
-#include <assert.h>
+/* For xtoi */
+#include "encode.h"
 
 #include "pcrs.h"
 
@@ -195,6 +209,7 @@ const char pcrs_h_rcs[] = PCRS_H_VERSION;
 static int              pcrs_parse_perl_options(const char *optstring, int *flags);
 static pcrs_substitute *pcrs_compile_replacement(const char *replacement, int trivialflag,
                         int capturecount, int *errptr);
+static int              is_hex_sequence(const char *sequence);
 
 /*********************************************************************
  *
@@ -422,6 +437,20 @@ static pcrs_substitute *pcrs_compile_replacement(const char *replacement, int tr
                   }
                   i++;
                }
+               else if (is_hex_sequence(&replacement[i]))
+               {
+                  /*
+                   * Replace a hex sequence with a single
+                   * character with the sequence's ascii value.
+                   * e.g.: '\x7e' => '~'
+                   */
+                  const int ascii_value = xtoi(&replacement[i+2]);
+
+                  assert(ascii_value > 0);
+                  assert(ascii_value < 256);
+                  text[k++] = (char)ascii_value;
+                  i += 4;
+               }               
                else
                {
                   quoted = 1;
@@ -1014,6 +1043,33 @@ int pcrs_execute(pcrs_job *job, const char *subject, size_t subject_length, char
 
 }
 
+
+#define is_hex_digit(x) ((x) && strchr("0123456789ABCDEF", toupper(x)))
+
+/*********************************************************************
+ *
+ * Function    :  is_hex_sequence
+ *
+ * Description :  Checks the first four characters of a string
+ *                and decides if they are a valid hex sequence
+ *                (like '\x40').
+ *
+ * Parameters  :
+ *          1  :  sequence = The string to check
+ *
+ * Returns     :  Non-zero if it's valid sequence, or
+ *                Zero if it isn't.
+ *
+ *********************************************************************/
+static int is_hex_sequence(const char *sequence)
+{
+   return (sequence[0] == '\\' &&
+           sequence[1] == 'x'  &&
+           is_hex_digit(sequence[2]) &&
+           is_hex_digit(sequence[3]));
+}
+
+
 /*
  * Functions below this line are only part of the pcrs version
  * included in Privoxy. If you use any of them you should not
@@ -1099,7 +1155,6 @@ char pcrs_get_delimiter(const char *string)
 }
 
 
-
 /*********************************************************************
  *
  * Function    :  pcrs_execute_single_command
@@ -1145,7 +1200,7 @@ char *pcrs_execute_single_command(const char *subject, const char *pcrs_command,
 }
 
 
-const static char warning[] = "... [too long, truncated]";
+static const char warning[] = "... [too long, truncated]";
 /*********************************************************************
  *
  * Function    :  pcrs_compile_dynamic_command