[rs-commit] r46 - in /redwax-tool/trunk: redwax-tool.c redwax_p11kit.c redwax_util.c redwax_util.h

rs-commit at redwax.eu rs-commit at redwax.eu
Sat Nov 20 11:55:49 CET 2021


Author: minfrin at redwax.eu
Date: Sat Nov 20 11:55:45 2021
New Revision: 46

Log:
Don't use apr_escape.h, it is too new.

Modified:
    redwax-tool/trunk/redwax-tool.c
    redwax-tool/trunk/redwax_p11kit.c
    redwax-tool/trunk/redwax_util.c
    redwax-tool/trunk/redwax_util.h

Modified: redwax-tool/trunk/redwax-tool.c
==============================================================================
--- redwax-tool/trunk/redwax-tool.c	(original)
+++ redwax-tool/trunk/redwax-tool.c	Sat Nov 20 11:55:45 2021
@@ -1142,7 +1142,7 @@
         if (!strncmp(arg, (const char *)val, arglen)) {
 
             apr_file_printf(r->out, "%s \n",
-                    redwax_pescape_echo(r->pool,
+                    redwax_pescape_echo_quoted(r->pool,
                             (const char *)val, quoted, 1));
         }
     }
@@ -1215,7 +1215,7 @@
         case APR_DIR: {
 
             if (!strncmp(base, dirent.name, base_len)) {
-                apr_file_printf(r->out, "%s/\n", redwax_pescape_echo(r->pool,
+                apr_file_printf(r->out, "%s/\n", redwax_pescape_echo_quoted(r->pool,
                         apr_pstrcat(r->pool, prefix, dirent.name, NULL),
                         quoted, 0));
             }
@@ -1300,7 +1300,7 @@
         case APR_REG: {
 
             if (!strncmp(base, dirent.name, base_len)) {
-                                apr_file_printf(r->out, "%s \n", redwax_pescape_echo(r->pool,
+                                apr_file_printf(r->out, "%s \n", redwax_pescape_echo_quoted(r->pool,
                         apr_pstrcat(r->pool, prefix, dirent.name, NULL),
                         quoted, 1));
             }
@@ -1311,7 +1311,7 @@
         case APR_DIR: {
 
             if (!strncmp(base, dirent.name, base_len)) {
-                apr_file_printf(r->out, "%s/\n", redwax_pescape_echo(r->pool,
+                apr_file_printf(r->out, "%s/\n", redwax_pescape_echo_quoted(r->pool,
                         apr_pstrcat(r->pool, prefix, dirent.name, NULL),
                         quoted, 0));
             }
@@ -1346,7 +1346,7 @@
             /* FIXME: wildcard matching, let's make it a thing */
 
             apr_file_printf(r->out, "%s \n",
-                    redwax_pescape_echo(r->pool,
+                    redwax_pescape_echo_quoted(r->pool,
                             apr_pstrndup(r->pool, (const char*) key,
                                     (int) klen), quoted, 1));
 
@@ -1369,7 +1369,7 @@
 
         if (arglen <= klen && !strncmp(arg, (const char *)key, arglen)) {
             apr_file_printf(r->out, "%s \n",
-                    redwax_pescape_echo(r->pool,
+                    redwax_pescape_echo_quoted(r->pool,
                             apr_pstrndup(r->pool, (const char*) key,
                                     (int) klen), quoted, 1));
         }
@@ -1391,7 +1391,7 @@
 
         if (arglen <= klen && !strncmp(arg, (const char *)key, arglen)) {
             apr_file_printf(r->out, "%s \n",
-                    redwax_pescape_echo(r->pool,
+                    redwax_pescape_echo_quoted(r->pool,
                             apr_pstrndup(r->pool, (const char*) key,
                                     (int) klen), quoted, 1));
         }
@@ -1417,7 +1417,7 @@
         if (!strncmp(arg, (const char *)val, arglen)) {
 
             apr_file_printf(r->out, "%s \n",
-                    redwax_pescape_echo(r->pool,
+                    redwax_pescape_echo_quoted(r->pool,
                             (const char *)val, quoted, 1));
         }
     }
@@ -1442,7 +1442,7 @@
         if (!strncmp(arg, (const char *)val, arglen)) {
 
             apr_file_printf(r->out, "%s \n",
-                    redwax_pescape_echo(r->pool,
+                    redwax_pescape_echo_quoted(r->pool,
                             (const char *)val, quoted, 1));
         }
     }
@@ -1467,7 +1467,7 @@
         apr_hash_this(hi, NULL, NULL, &val);
 
         apr_file_printf(r->out, "%s\n",
-                redwax_pescape_echo(r->pool,
+                redwax_pescape_echo_quoted(r->pool,
                         redwax_stroff((const char *)val, skiplen), quoted, 0));
     }
 
@@ -1491,7 +1491,7 @@
         apr_hash_this(hi, NULL, NULL, &val);
 
         apr_file_printf(r->out, "%s\n",
-                redwax_pescape_echo(r->pool,
+                redwax_pescape_echo_quoted(r->pool,
                         redwax_stroff((const char *)val, skiplen), quoted, 0));
     }
 

Modified: redwax-tool/trunk/redwax_p11kit.c
==============================================================================
--- redwax-tool/trunk/redwax_p11kit.c	(original)
+++ redwax-tool/trunk/redwax_p11kit.c	Sat Nov 20 11:55:45 2021
@@ -29,7 +29,6 @@
  */
 
 #include <apr_crypto.h>
-#include <apr_escape.h>
 #include <apr_lib.h>
 #include <apr_strings.h>
 
@@ -1978,7 +1977,7 @@
         const char *match, const char *prefix, const char *name,
         const char *val, apr_hash_t *urls) {
 
-    const char *v = apr_pescape_path_segment(pool, val);
+    const char *v = redwax_pescape_path(pool, val);
     const char *entry = apr_pstrcat(r->pool, prefix, name, v, NULL);
 
     if (!strncmp(match, v, strlen(match))) {
@@ -1996,9 +1995,9 @@
     char *v;
     const char *entry;
 
-    apr_escape_path_segment(NULL, val, len, &size);
+    redwax_urlescape_all(NULL, val, len, &size);
     v = apr_palloc(pool, size);
-    apr_escape_path_segment(v, val, len, NULL);
+    redwax_urlescape_all(v, val, len, NULL);
 
     entry = apr_pstrcat(r->pool, prefix, name, v, NULL);
 
@@ -2530,7 +2529,7 @@
                     if (module) {
 
                         apr_file_printf(r->out, "%s \n",
-                                redwax_pescape_echo(r->pool,
+                                redwax_pescape_echo_quoted(r->pool,
                                         apr_pstrcat(r->pool, prefix,
                                                 dirent.name, NULL), quoted, 1));
 
@@ -2547,7 +2546,7 @@
         case APR_DIR: {
 
             if (abs && !strncmp(base, dirent.name, base_len)) {
-                apr_file_printf(r->out, "%s/\n", redwax_pescape_echo(r->pool,
+                apr_file_printf(r->out, "%s/\n", redwax_pescape_echo_quoted(r->pool,
                         apr_pstrcat(r->pool, prefix, dirent.name, NULL),
                         quoted, 0));
             }

Modified: redwax-tool/trunk/redwax_util.c
==============================================================================
--- redwax-tool/trunk/redwax_util.c	(original)
+++ redwax-tool/trunk/redwax_util.c	Sat Nov 20 11:55:45 2021
@@ -21,8 +21,8 @@
 
 #include "redwax_util.h"
 
+#include <apr_lib.h>
 #include <apr_strings.h>
-#include <apr_escape.h>
 
 int redwax_certcmp(redwax_certificate_t *cert1, redwax_certificate_t *cert2)
 {
@@ -85,11 +85,205 @@
     return apr_pstrndup(p, start, end - start);
 }
 
-const char *redwax_pescape_echo(apr_pool_t *p, const char *str,
+/* c2x takes an unsigned, and expects the caller has guaranteed that
+ * 0 <= what < 256... which usually means that you have to cast to
+ * unsigned char first, because (unsigned)(char)(x) first goes through
+ * signed extension to an int before the unsigned cast.
+ *
+ * The reason for this assumption is to assist gcc code generation --
+ * the unsigned char -> unsigned extension is already done earlier in
+ * both uses of this code, so there's no need to waste time doing it
+ * again.
+ */
+static const char c2x_table[] = "0123456789abcdef";
+
+static APR_INLINE unsigned char *c2x(unsigned what, unsigned char prefix,
+        unsigned char *where)
+{
+#if APR_CHARSET_EBCDIC
+    what = convert_e2a[(unsigned char)what];
+#endif /*APR_CHARSET_EBCDIC*/
+    *where++ = prefix;
+    *where++ = c2x_table[what >> 4];
+    *where++ = c2x_table[what & 0xf];
+    return where;
+}
+
+static int test_echo(char c)
+{
+    if (c && (!apr_isprint(c) || c == '"' || c == '\\' || apr_iscntrl(c))) {
+        return 1;
+    }
+    return 0;
+}
+
+static int test_path(char c)
+{
+    if (!apr_isalnum(c) && !strchr("$-_.+!*'(),:@&=~", c)) {
+        return 1;
+    }
+    return 0;
+}
+
+static int test_all(char c)
+{
+    return 1;
+}
+
+apr_status_t redwax_escape_echo(char *escaped, const char *str,
+        apr_ssize_t slen, int quote, apr_size_t *len)
+{
+    apr_size_t size = 1;
+    int found = 0;
+    const unsigned char *s = (const unsigned char *) str;
+    unsigned char *d = (unsigned char *) escaped;
+    unsigned c;
+
+    if (s) {
+        if (d) {
+            while ((c = *s) && slen) {
+                if (test_echo(c)) {
+                    *d++ = '\\';
+                    size++;
+                    switch (c) {
+                    case '\a':
+                        *d++ = 'a';
+                        size++;
+                        found = 1;
+                        break;
+                    case '\b':
+                        *d++ = 'b';
+                        size++;
+                        found = 1;
+                        break;
+                    case '\f':
+                        *d++ = 'f';
+                        size++;
+                        found = 1;
+                        break;
+                    case '\n':
+                        *d++ = 'n';
+                        size++;
+                        found = 1;
+                        break;
+                    case '\r':
+                        *d++ = 'r';
+                        size++;
+                        found = 1;
+                        break;
+                    case '\t':
+                        *d++ = 't';
+                        size++;
+                        found = 1;
+                        break;
+                    case '\v':
+                        *d++ = 'v';
+                        size++;
+                        found = 1;
+                        break;
+                    case '\\':
+                        *d++ = '\\';
+                        size++;
+                        found = 1;
+                        break;
+                    case '"':
+                        if (quote) {
+                            *d++ = c;
+                            size++;
+                            found = 1;
+                        }
+                        else {
+                            d[-1] = c;
+                        }
+                        break;
+                    default:
+                        c2x(c, 'x', d);
+                        d += 3;
+                        size += 3;
+                        found = 1;
+                        break;
+                    }
+                }
+                else {
+                    *d++ = c;
+                    size++;
+                }
+                ++s;
+                slen--;
+            }
+            *d = '\0';
+        }
+        else {
+            while ((c = *s) && slen) {
+                if (test_echo(c)) {
+                    size++;
+                    switch (c) {
+                    case '\a':
+                    case '\b':
+                    case '\f':
+                    case '\n':
+                    case '\r':
+                    case '\t':
+                    case '\v':
+                    case '\\':
+                        size++;
+                        found = 1;
+                        break;
+                    case '"':
+                        if (quote) {
+                            size++;
+                            found = 1;
+                        }
+                        break;
+                    default:
+                        size += 3;
+                        found = 1;
+                        break;
+                    }
+                }
+                else {
+                    size++;
+                }
+                ++s;
+                slen--;
+            }
+        }
+    }
+
+    if (len) {
+        *len = size;
+    }
+    if (!found) {
+        return APR_NOTFOUND;
+    }
+
+    return APR_SUCCESS;
+}
+
+static const char *redwax_pescape_echo(apr_pool_t *p, const char *str,
+        int quote)
+{
+    apr_size_t len;
+
+    switch (redwax_escape_echo(NULL, str, REDWAX_ESCAPE_STRING, quote, &len)) {
+    case APR_SUCCESS: {
+        char *cmd = apr_palloc(p, len);
+        redwax_escape_echo(cmd, str, REDWAX_ESCAPE_STRING, quote, NULL);
+        return cmd;
+    }
+    case APR_NOTFOUND: {
+        break;
+    }
+    }
+
+    return str;
+}
+
+const char *redwax_pescape_echo_quoted(apr_pool_t *p, const char *str,
         redwax_token_quoted_e quoted, int close)
 {
 
-     str = apr_pescape_echo(p, str, (quoted != REDWAX_TOKEN_NOQUOTE));
+     str = redwax_pescape_echo(p, str, (quoted != REDWAX_TOKEN_NOQUOTE));
 
      if (str) {
 
@@ -178,6 +372,103 @@
      return str;
 }
 
+static apr_status_t redwax_urlescape(char *escaped,
+        const char *str, apr_ssize_t slen, apr_size_t *len, int(*test)(char))
+{
+    apr_size_t size = 1;
+    int found = 0;
+    const unsigned char *s = (const unsigned char *) str;
+    unsigned char *d = (unsigned char *) escaped;
+    unsigned c;
+
+    if (s) {
+        if (d) {
+            while ((c = *s) && slen) {
+                if (test(c)) {
+                    d = c2x(c, '%', d);
+                    size += 2;
+                    found = 1;
+                }
+                else {
+                    *d++ = c;
+                }
+                ++s;
+                size++;
+                slen--;
+            }
+            *d = '\0';
+        }
+        else {
+            while ((c = *s) && slen) {
+                if (test(c)) {
+                    size += 2;
+                    found = 1;
+                }
+                ++s;
+                size++;
+                slen--;
+            }
+        }
+    }
+
+    if (len) {
+        *len = size;
+    }
+    if (!found) {
+        return APR_NOTFOUND;
+    }
+
+    return APR_SUCCESS;
+}
+
+apr_status_t redwax_urlescape_path(char *escaped,
+        const char *str, apr_ssize_t slen, apr_size_t *len)
+{
+    return redwax_urlescape(escaped, str, slen, len, test_path);
+}
+
+const char* redwax_pescape_path(apr_pool_t *p, const char *str)
+{
+    apr_size_t len;
+
+    switch (redwax_urlescape_path(NULL, str, REDWAX_ESCAPE_STRING, &len)) {
+    case APR_SUCCESS: {
+        char *cmd = apr_palloc(p, len);
+        redwax_urlescape_path(cmd, str, REDWAX_ESCAPE_STRING, NULL);
+        return cmd;
+    }
+    case APR_NOTFOUND: {
+        break;
+    }
+    }
+
+    return str;
+}
+
+apr_status_t redwax_urlescape_all(char *escaped,
+        const char *str, apr_ssize_t slen, apr_size_t *len)
+{
+    return redwax_urlescape(escaped, str, slen, len, test_all);
+}
+
+const char* redwax_pescape_all(apr_pool_t *p, const char *str)
+{
+    apr_size_t len;
+
+    switch (redwax_urlescape_all(NULL, str, REDWAX_ESCAPE_STRING, &len)) {
+    case APR_SUCCESS: {
+        char *cmd = apr_palloc(p, len);
+        redwax_urlescape_all(cmd, str, REDWAX_ESCAPE_STRING, NULL);
+        return cmd;
+    }
+    case APR_NOTFOUND: {
+        break;
+    }
+    }
+
+    return str;
+}
+
 static const char base16[] = "0123456789ABCDEF";
 static const char base16lower[] = "0123456789abcdef";
 

Modified: redwax-tool/trunk/redwax_util.h
==============================================================================
--- redwax-tool/trunk/redwax_util.h	(original)
+++ redwax-tool/trunk/redwax_util.h	Sat Nov 20 11:55:45 2021
@@ -34,14 +34,26 @@
 
 const char *redwax_pstrntrim(apr_pool_t *p, const char *s, apr_size_t off);
 
-const char *redwax_pescape_echo(apr_pool_t *p, const char *str,
+const char *redwax_pescape_echo_quoted(apr_pool_t *p, const char *str,
         redwax_token_quoted_e quoted, int close);
+
+apr_status_t redwax_urlescape_path(char *escaped,
+        const char *str, apr_ssize_t slen, apr_size_t *len);
+
+const char* redwax_pescape_path(apr_pool_t *p, const char *str);
+
+apr_status_t redwax_urlescape_all(char *escaped,
+        const char *str, apr_ssize_t slen, apr_size_t *len);
+
+const char* redwax_pescape_all(apr_pool_t *p, const char *str);
 
 apr_status_t redwax_encode_base16_binary(char *dest, const unsigned char *src,
         apr_ssize_t slen, int flags, apr_size_t *len);
 
 const char* redwax_pencode_base16_binary(apr_pool_t *p,
         const unsigned char *src, apr_ssize_t slen, int flags, apr_size_t *len);
+
+#define REDWAX_ESCAPE_STRING -1
 
 /**
  * Generate RFC4648 base16/base32/base64.



More information about the rs-commit mailing list