[build-path-prefix-map-spec] 07/50: Add C and JS examples for urlencode, as well as fuzzing for the C code
Ximin Luo
infinity0 at debian.org
Fri Mar 10 15:17:18 UTC 2017
This is an automated email from the git hooks/post-receive script.
infinity0 pushed a commit to branch master
in repository build-path-prefix-map-spec.
commit 98dbc88b4bd818a5e02c31c97a3cc60dacaa3e0a
Author: Ximin Luo <infinity0 at debian.org>
Date: Mon Jan 23 16:48:28 2017 +0100
Add C and JS examples for urlencode, as well as fuzzing for the C code
---
.gitignore | 1 +
consume/Makefile | 6 +--
consume/afl-in-split/{1.in => 0.0.in} | Bin
consume/afl-in-urlencode/0.0.in | Bin 0 -> 68 bytes
consume/split.c | 2 +-
consume/urlencode.c | 91 ++++++++++++++++++++++++++++++++++
consume/urlencode.js | 27 ++++++++++
consume/urlencode.py | 7 ++-
8 files changed, 126 insertions(+), 8 deletions(-)
diff --git a/.gitignore b/.gitignore
index 90cdd76..2e25b66 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
/consume/split
+/consume/urlencode
/consume/afl-out-*
/produce/*.html
diff --git a/consume/Makefile b/consume/Makefile
index b0a538c..cc19a47 100644
--- a/consume/Makefile
+++ b/consume/Makefile
@@ -1,14 +1,14 @@
TMPDIR = /run/shm/rb-prefix-map
-ALL = split
+ALL = split urlencode
ALLCHECK_split = split split.py
-ALLCHECK_urlencode = urlencode.py
+ALLCHECK_urlencode = urlencode urlencode.py
.PHONY: all
all: $(ALL)
%: %.c source_prefix_map.h
- $(CC) -o "$@" "$<"
+ $(CC) -Wall -o "$@" "$<"
check-apply-generic = \
set -ex; for i in $(1); do \
diff --git a/consume/afl-in-split/1.in b/consume/afl-in-split/0.0.in
similarity index 100%
rename from consume/afl-in-split/1.in
rename to consume/afl-in-split/0.0.in
diff --git a/consume/afl-in-urlencode/0.0.in b/consume/afl-in-urlencode/0.0.in
new file mode 100644
index 0000000..8baf759
Binary files /dev/null and b/consume/afl-in-urlencode/0.0.in differ
diff --git a/consume/split.c b/consume/split.c
index 2a7a52d..2621da2 100644
--- a/consume/split.c
+++ b/consume/split.c
@@ -7,7 +7,7 @@ int
parse_prefix_map (const char *arg, struct prefix_map *map)
{
const char *p;
- p = strrchr (arg, '=');
+ p = strrchr (arg, '='); // right-split, to allow for more mapping sources
if (!p)
return 0;
map->old_prefix = xstrndup (arg, p - arg);
diff --git a/consume/urlencode.c b/consume/urlencode.c
new file mode 100644
index 0000000..bd51924
--- /dev/null
+++ b/consume/urlencode.c
@@ -0,0 +1,91 @@
+#include "source_prefix_map.h"
+#include <errno.h>
+
+/* Parsing the variable. */
+/* For Applying the variable, see source_prefix_map.h. */
+
+int
+unquote (char *src)
+{
+ char *dest = src;
+ char x[] = {0, 0, 0};
+ char c;
+ while (*src != 0)
+ {
+ switch (*src)
+ {
+ case '+':
+ *dest = ' ';
+ break;
+ case '&':
+ case ';':
+ case '=':
+ return 0; // invalid, should have been escaped
+ case '%':
+ if (!(x[0] = *++src) || !(x[1] = *++src))
+ return 0; // invalid, past end of string
+ sscanf(x, "%2hhx", &c);
+ if (errno != 0)
+ return 0; // invalid, not valid hex
+ *dest = c;
+ break;
+ default:
+ *dest = *src;
+ }
+ ++dest, ++src;
+ }
+ *dest = '\0';
+ return 1;
+}
+
+int
+parse_prefix_map (char *arg, struct prefix_map *map)
+{
+ char *p;
+ p = strchr (arg, '='); // left-split, to match the urlencode algorithm
+ if (!p)
+ return 0;
+ *p = '\0';
+ if (!unquote (arg))
+ return 0;
+ map->old_prefix = xstrdup (arg);
+ map->old_len = strlen (arg);
+ p++;
+ if (!unquote (p))
+ return 0;
+ map->new_prefix = xstrdup (p);
+ map->new_len = strlen (p);
+ return 1;
+}
+
+int
+parse_prefix_maps (const char *arg, struct prefix_maps *maps)
+{
+ size_t len = strlen (arg);
+ char *copy = (char *) alloca (len + 1);
+ memcpy (copy, arg, len + 1); // strtok modifies the string so we have to copy it
+
+ char *sep = "&;", *end;
+ char *tok = strtok_r (copy, sep, &end);
+ while (tok != NULL)
+ {
+ struct prefix_map *map = XNEW (struct prefix_map);
+ if (!parse_prefix_map (tok, map))
+ {
+ fprintf (stderr, "invalid value for prefix-map: '%s'\n", arg);
+ free (map);
+ return 0;
+ }
+
+ add_prefix_map (map, maps);
+ tok = strtok_r (NULL, sep, &end);
+ }
+
+ return 1;
+}
+
+int
+main (int argc, char *argv[])
+{
+ return generic_main (parse_prefix_maps, argc, argv);
+}
diff --git a/consume/urlencode.js b/consume/urlencode.js
new file mode 100755
index 0000000..7b8d6e7
--- /dev/null
+++ b/consume/urlencode.js
@@ -0,0 +1,27 @@
+#!/usr/bin/nodejs
+
+var unquote = function(x) {
+ return x.replace(/\+/g, " ").split(/=/g, 2).map(decodeURIComponent);
+};
+
+var parse_prefix_map = function(x) {
+ return x ? x.split(/[;&]/g).map(unquote) : [];
+}
+
+var map_prefix = function(string, pm) {
+ for (var i = pm.length - 1; i >= 0; --i) {;
+ var src = pm[i][0];
+ var dst = pm[i][1];
+ if (string.indexOf(src) === 0) {
+ return dst + string.substr(src.length);
+ }
+ }
+ return string;
+}
+
+var pm = parse_prefix_map(process.env["SOURCE_PREFIX_MAP"]);
+
+// var i = 2 is just how nodejs yolos its way through common conventions
+for (var i = 2, l = process.argv.length; i < l; ++i) {
+ console.log(map_prefix(process.argv[i], pm));
+}
diff --git a/consume/urlencode.py b/consume/urlencode.py
index 1b4dcd7..1ab33c2 100755
--- a/consume/urlencode.py
+++ b/consume/urlencode.py
@@ -7,16 +7,15 @@ from urllib.parse import parse_qsl
# Parsing the variable
-val = os.getenv("SOURCE_PREFIX_MAP", "")
-pm = parse_qsl(val)
+pm = parse_qsl(os.getenv("SOURCE_PREFIX_MAP", ""))
# Applying the variable
-def normprefix(string):
+def map_prefix(string, pm):
for src, dst in reversed(pm):
if string.startswith(src):
return dst + string[len(src):]
return string
for v in sys.argv[1:]:
- print(normprefix(v))
+ print(map_prefix(v, pm))
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/build-path-prefix-map-spec.git
More information about the Reproducible-commits
mailing list