[build-path-prefix-map-spec] 08/50: Convert examples to hexsplit

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 8c3fb57c2bfcc50d2d8cbb6e236ea950edf3e655
Author: Ximin Luo <infinity0 at debian.org>
Date:   Mon Jan 23 17:45:43 2017 +0100

    Convert examples to hexsplit
---
 .gitignore                                         |   3 +-
 consume/0.0-hexsplit.env                           |   1 +
 consume/0.0-split.env                              |   1 -
 consume/0.0-urlencode.env                          |   1 -
 consume/Makefile                                   |  11 ++-
 .../{afl-in-urlencode => afl-in-hexsplit}/0.0.in   | Bin
 consume/afl-in-split/0.0.in                        | Bin 65 -> 0 bytes
 consume/{urlencode.c => hexsplit.c}                |  12 ++--
 consume/{urlencode.js => hexsplit.js}              |   6 +-
 consume/hexsplit.py                                |  29 ++++++++
 consume/{source_prefix_map.h => prefix_map.h}      |  14 ++--
 consume/split.c                                    |  51 --------------
 consume/split.py                                   |  20 ------
 consume/urlencode.py                               |  21 ------
 produce/{split_char.mk => hexsplit.mk}             |   7 +-
 produce/split_2sep.mk                              |   9 ---
 produce/split_2sep.sh                              |   8 ---
 produce/split_char.sh                              |   8 ---
 produce/split_newline.mk                           |  26 -------
 produce/split_newline.sh                           |   9 ---
 produce/split_test-all.sh                          |  75 ---------------------
 produce/urlencode.mk                               |  20 ------
 22 files changed, 56 insertions(+), 276 deletions(-)

diff --git a/.gitignore b/.gitignore
index 2e25b66..557eb48 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,3 @@
-/consume/split
-/consume/urlencode
+/consume/hexsplit
 /consume/afl-out-*
 /produce/*.html
diff --git a/consume/0.0-hexsplit.env b/consume/0.0-hexsplit.env
new file mode 100644
index 0000000..263eceb
--- /dev/null
+++ b/consume/0.0-hexsplit.env
@@ -0,0 +1 @@
+/a/b%3dyyy=ERROR:/a=lol:/b=foo:/a/b%3dyyy=secreteh
\ No newline at end of file
diff --git a/consume/0.0-split.env b/consume/0.0-split.env
deleted file mode 100644
index fa5d629..0000000
--- a/consume/0.0-split.env
+++ /dev/null
@@ -1 +0,0 @@
-/a/b=yyy=ERROR	/a=lol	/b=foo	/a/b=yyy=secreteh
\ No newline at end of file
diff --git a/consume/0.0-urlencode.env b/consume/0.0-urlencode.env
deleted file mode 100644
index bb2a70d..0000000
--- a/consume/0.0-urlencode.env
+++ /dev/null
@@ -1 +0,0 @@
-/a/b%3Dyyy=ERROR&/a=lol&/b=foo&/a/b%3Dyyy=secreteh
\ No newline at end of file
diff --git a/consume/Makefile b/consume/Makefile
index cc19a47..a3ed39b 100644
--- a/consume/Makefile
+++ b/consume/Makefile
@@ -1,18 +1,17 @@
 TMPDIR = /run/shm/rb-prefix-map
 
-ALL = split urlencode
-ALLCHECK_split = split split.py
-ALLCHECK_urlencode = urlencode urlencode.py
+ALL = hexsplit
+ALLCHECK_hexsplit = hexsplit hexsplit.js hexsplit.py
 
 .PHONY: all
 all: $(ALL)
 
-%: %.c source_prefix_map.h
+%: %.c prefix_map.h
 	$(CC) -Wall -o "$@" "$<"
 
 check-apply-generic = \
 	set -ex; for i in $(1); do \
-	  SOURCE_PREFIX_MAP="$$(cat 0.$(2)-$(3).env)" \
+	  BUILD_PATH_PREFIX_MAP="$$(cat 0.$(2)-$(3).env)" \
 	  ./$$i $$(cat 0.in) | diff -ru - 0.$(2).out; \
 	done
 
@@ -22,7 +21,7 @@ check-apply-none = \
 	done
 
 .PHONY: check
-check: check-apply-split check-apply-urlencode
+check: check-apply-hexsplit
 
 .PHONY: check-apply-%
 check-apply-%:
diff --git a/consume/afl-in-urlencode/0.0.in b/consume/afl-in-hexsplit/0.0.in
similarity index 100%
rename from consume/afl-in-urlencode/0.0.in
rename to consume/afl-in-hexsplit/0.0.in
diff --git a/consume/afl-in-split/0.0.in b/consume/afl-in-split/0.0.in
deleted file mode 100644
index c61b632..0000000
Binary files a/consume/afl-in-split/0.0.in and /dev/null differ
diff --git a/consume/urlencode.c b/consume/hexsplit.c
similarity index 90%
rename from consume/urlencode.c
rename to consume/hexsplit.c
index bd51924..c6e6912 100644
--- a/consume/urlencode.c
+++ b/consume/hexsplit.c
@@ -1,8 +1,8 @@
-#include "source_prefix_map.h"
+#include "prefix_map.h"
 #include <errno.h>
 
 /* Parsing the variable. */
-/* For Applying the variable, see source_prefix_map.h. */
+/* For Applying the variable, see prefix_map.h. */
 
 int
 unquote (char *src)
@@ -14,11 +14,7 @@ unquote (char *src)
     {
       switch (*src)
 	{
-	case '+':
-	  *dest = ' ';
-	  break;
-	case '&':
-	case ';':
+	case ':':
 	case '=':
 	  return 0; // invalid, should have been escaped
 	case '%':
@@ -65,7 +61,7 @@ parse_prefix_maps (const char *arg, struct prefix_maps *maps)
   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 *sep = ":", *end;
   char *tok = strtok_r (copy, sep, &end);
   while (tok != NULL)
     {
diff --git a/consume/urlencode.js b/consume/hexsplit.js
similarity index 74%
rename from consume/urlencode.js
rename to consume/hexsplit.js
index 7b8d6e7..77a1e9d 100755
--- a/consume/urlencode.js
+++ b/consume/hexsplit.js
@@ -1,11 +1,11 @@
 #!/usr/bin/nodejs
 
 var unquote = function(x) {
-    return x.replace(/\+/g, " ").split(/=/g, 2).map(decodeURIComponent);
+    return x.split(/=/g, 2).map(unescape);
 };
 
 var parse_prefix_map = function(x) {
-    return x ? x.split(/[;&]/g).map(unquote) : [];
+    return x ? x.split(/:/g).map(unquote) : [];
 }
 
 var map_prefix = function(string, pm) {
@@ -19,7 +19,7 @@ var map_prefix = function(string, pm) {
     return string;
 }
 
-var pm = parse_prefix_map(process.env["SOURCE_PREFIX_MAP"]);
+var pm = parse_prefix_map(process.env["BUILD_PATH_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) {
diff --git a/consume/hexsplit.py b/consume/hexsplit.py
new file mode 100755
index 0000000..9c07aeb
--- /dev/null
+++ b/consume/hexsplit.py
@@ -0,0 +1,29 @@
+#!/usr/bin/python3
+
+import os
+import sys
+
+# Parsing the variable
+
+def _dequote(part):
+    subs = part.split("%")
+    # Will raise if there are <2 chars after % or if these aren't valid hex
+    return subs[0] + "".join(chr(int(sub[0:2], 16)) + sub[2:] for sub in subs[1:])
+
+def decode(prefix_str):
+    tuples = (part.split("=") for part in prefix_str.split(":")) if prefix_str else ()
+    # Will raise if any tuple can't be destructured into a pair
+    return [(_dequote(src), _dequote(dst)) for src, dst in tuples]
+
+pm = decode(os.getenv("BUILD_PATH_PREFIX_MAP", ""))
+
+# Applying the variable
+
+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(map_prefix(v, pm))
diff --git a/consume/source_prefix_map.h b/consume/prefix_map.h
similarity index 89%
rename from consume/source_prefix_map.h
rename to consume/prefix_map.h
index 702aaa9..179e285 100644
--- a/consume/source_prefix_map.h
+++ b/consume/prefix_map.h
@@ -114,15 +114,15 @@ remap_prefix (const char *filename, struct prefix_maps *maps)
 /*
  * Run as one of:
  *
- * $ SOURCE_PREFIX_MAP=${map} ./main ${path0} ${path1} ${path2}
+ * $ BUILD_PATH_PREFIX_MAP=${map} ./main ${path0} ${path1} ${path2}
  * $ printf "${map}\0${path0}\0${path1}\0${path2}\0" | ./main -
  */
 int
 generic_main (int (*parse_prefix_maps) (const char *, struct prefix_maps *), int argc, char *argv[])
 {
-  struct prefix_maps source_prefix_map = { NULL, 0 };
+  struct prefix_maps build_path_prefix_map = { NULL, 0 };
 
-  int using_stdin = 0; // 0 = SOURCE_PREFIX_MAP envvar, 1 = stdin (for afl)
+  int using_stdin = 0; // 0 = BUILD_PATH_PREFIX_MAP envvar, 1 = stdin (for afl)
   char *mapstr = NULL;
   if (argc > 1 && strncmp (argv[1], "-", 1) == 0)
     {
@@ -133,10 +133,10 @@ generic_main (int (*parse_prefix_maps) (const char *, struct prefix_maps *), int
       using_stdin = 1;
     }
   else
-    mapstr = getenv ("SOURCE_PREFIX_MAP");
+    mapstr = getenv ("BUILD_PATH_PREFIX_MAP");
 
   if (mapstr)
-    if (!parse_prefix_maps (mapstr, &source_prefix_map))
+    if (!parse_prefix_maps (mapstr, &build_path_prefix_map))
       {
 	fprintf (stderr, "parse_prefix_maps failed\n");
 	return 1;
@@ -150,7 +150,7 @@ generic_main (int (*parse_prefix_maps) (const char *, struct prefix_maps *), int
       size_t len = 0;
       while (getdelim (&arg, &len, 0, stdin) != -1)
 	{
-	  printf ("%s\n", remap_prefix (arg, &source_prefix_map));
+	  printf ("%s\n", remap_prefix (arg, &build_path_prefix_map));
 	}
 
       if (ferror (stdin))
@@ -161,7 +161,7 @@ generic_main (int (*parse_prefix_maps) (const char *, struct prefix_maps *), int
       for (int i = using_stdin ? 2 : 1; i < argc; i++)
 	{
 	  //fprintf (stderr, "%s", argv[i]);
-	  printf ("%s\n", remap_prefix (argv[i], &source_prefix_map));
+	  printf ("%s\n", remap_prefix (argv[i], &build_path_prefix_map));
 	}
     }
 
diff --git a/consume/split.c b/consume/split.c
deleted file mode 100644
index 2621da2..0000000
--- a/consume/split.c
+++ /dev/null
@@ -1,51 +0,0 @@
-#include "source_prefix_map.h"
-
-/* Parsing the variable. */
-/* For Applying the variable, see source_prefix_map.h. */
-
-int
-parse_prefix_map (const char *arg, struct prefix_map *map)
-{
-  const char *p;
-  p = strrchr (arg, '='); // right-split, to allow for more mapping sources
-  if (!p)
-    return 0;
-  map->old_prefix = xstrndup (arg, p - arg);
-  map->old_len = p - arg;
-  p++;
-  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 = "\t", *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", tok);
-	  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/split.py b/consume/split.py
deleted file mode 100755
index 7ab19fa..0000000
--- a/consume/split.py
+++ /dev/null
@@ -1,20 +0,0 @@
-#!/usr/bin/python3
-
-import os
-import sys
-
-# Parsing the variable
-
-val = os.getenv("SOURCE_PREFIX_MAP", "")
-pm = [r.rsplit("=", 1) for r in filter(None, val.split('\t'))]
-
-# Applying the variable
-
-def normprefix(string):
-    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))
diff --git a/consume/urlencode.py b/consume/urlencode.py
deleted file mode 100755
index 1ab33c2..0000000
--- a/consume/urlencode.py
+++ /dev/null
@@ -1,21 +0,0 @@
-#!/usr/bin/python3
-
-import os
-import re
-import sys
-from urllib.parse import parse_qsl
-
-# Parsing the variable
-
-pm = parse_qsl(os.getenv("SOURCE_PREFIX_MAP", ""))
-
-# Applying the variable
-
-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(map_prefix(v, pm))
diff --git a/produce/split_char.mk b/produce/hexsplit.mk
similarity index 60%
rename from produce/split_char.mk
rename to produce/hexsplit.mk
index 4f24e74..7a67e0d 100755
--- a/produce/split_char.mk
+++ b/produce/hexsplit.mk
@@ -2,7 +2,12 @@
 
 # Setting the variable
 
-export override SOURCE_PREFIX_MAP := $(if $(SOURCE_PREFIX_MAP),$(SOURCE_PREFIX_MAP)$(SEP),)a=b
+spm_encode = $(subst :,%3a,$(subst =,%3d,$(subst %,%25,$(1))))
+
+export override SOURCE_PREFIX_MAP := $(if $(SOURCE_PREFIX_MAP),$(SOURCE_PREFIX_MAP)&,)$(call\
+spm_encode,aa with 100% sauce)=$(call\
+spm_encode,bbb)
+
 print-%:; @echo "$($*)"
 default: print-SOURCE_PREFIX_MAP
 
diff --git a/produce/split_2sep.mk b/produce/split_2sep.mk
deleted file mode 100755
index d55c866..0000000
--- a/produce/split_2sep.mk
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/usr/bin/make -f
-
-# Setting the variable
-
-export override SOURCE_PREFIX_MAP := $(if $(SOURCE_PREFIX_MAP),$(SOURCE_PREFIX_MAP)
,)ab
-print-%:; @echo "$($*)"
-default: print-SOURCE_PREFIX_MAP
-
-# We don't expect Makefiles to have to read or apply the variable.
diff --git a/produce/split_2sep.sh b/produce/split_2sep.sh
deleted file mode 100755
index 7e791a4..0000000
--- a/produce/split_2sep.sh
+++ /dev/null
@@ -1,8 +0,0 @@
-#!/bin/sh
-
-# Setting the variable
-
-export SOURCE_PREFIX_MAP="${SOURCE_PREFIX_MAP:+$SOURCE_PREFIX_MAP
}ab"
-echo "$SOURCE_PREFIX_MAP"
-
-# We don't expect shell scripts to have to read or apply the variable.
diff --git a/produce/split_char.sh b/produce/split_char.sh
deleted file mode 100755
index a2d08dd..0000000
--- a/produce/split_char.sh
+++ /dev/null
@@ -1,8 +0,0 @@
-#!/bin/sh
-
-# Setting the variable
-
-export SOURCE_PREFIX_MAP="${SOURCE_PREFIX_MAP:+$SOURCE_PREFIX_MAP$SEP}a=b"
-echo "$SOURCE_PREFIX_MAP"
-
-# We don't expect shell scripts to have to read or apply the variable.
diff --git a/produce/split_newline.mk b/produce/split_newline.mk
deleted file mode 100755
index dca190c..0000000
--- a/produce/split_newline.mk
+++ /dev/null
@@ -1,26 +0,0 @@
-#!/usr/bin/make -f
-# The ugliest one, we can't use standard Makefile idioms because of the newline
-
-# Setting the variable
-
-ifdef SOURCE_PREFIX_MAP
-export override define SOURCE_PREFIX_MAP :=
-$(SOURCE_PREFIX_MAP)
-a=b
-endef
-else
-export override SOURCE_PREFIX_MAP := a=b
-endif
-
-# Alternative method that is slightly shorter but results in an extra empty first mapping
-#export override define SOURCE_PREFIX_MAP :=
-#$(SOURCE_PREFIX_MAP)
-#a=b
-#endef
-
-# Can't print of a Makefile variable containing newlines due to its special
-# treatment within recipes; we have to print it as a shell variable instead.
-print-SOURCE_PREFIX_MAP:
-	@echo "$$SOURCE_PREFIX_MAP"
-
-# We don't expect Makefiles to have to read or apply the variable.
diff --git a/produce/split_newline.sh b/produce/split_newline.sh
deleted file mode 100755
index d2d1732..0000000
--- a/produce/split_newline.sh
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/bin/sh
-
-# Setting the variable
-
-export SOURCE_PREFIX_MAP="${SOURCE_PREFIX_MAP:+$SOURCE_PREFIX_MAP
-}a=b"
-echo "$SOURCE_PREFIX_MAP"
-
-# We don't expect shell scripts to have to read or apply the variable.
diff --git a/produce/split_test-all.sh b/produce/split_test-all.sh
deleted file mode 100755
index b90b116..0000000
--- a/produce/split_test-all.sh
+++ /dev/null
@@ -1,75 +0,0 @@
-#!/bin/bash
-# See also
-# https://www.w3.org/International/questions/qa-controls
-# https://www.w3.org/TR/html5/syntax.html#character-references
-# https://validator.w3.org/
-
-set -e
-set -o pipefail # and this is why we need bash
-
-r1="a=b"
-r2="/lol/wtf=bbq/project std::ftw???\latex{evil}=y"
-check1() {
-	local sep="$1"; shift
-	set -x
-	test "$r1" = "$("$@")"
-	test "$r2$sep$r1" = "$(SOURCE_PREFIX_MAP=$r2 "$@")"
-	{ set +x; } 2>/dev/null
-}
-
-checkall() {
-	echo >&2 ">>>> testing $1"
-	for i in sh mk; do
-		check1 "$2" ./"$3.$i"
-	done
-    echo >&2 "<<<< end testing"
-}
-
-to_html() {
-	echo '<?xml version="1.0" encoding="utf-8"?><!DOCTYPE html><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
-<title>results</title><style>pre { border:1px solid black; }</style></head><body><p>Try c+p the below into a text editor and/ or into <a href="https://validator.w3.org/#validate_by_input">W3C Markup Validator</a>.</p><div>'
-	sed -e 's,^>>>> \(.*\),\1\n<pre>,g' \
-	    -e 's,^<<<< .*,</pre>,g'
-#	    -e 's/
/\&#x1E;/g' \
-#	    -e 's//\&#x1F;/g' \
-#	    -e 's/
/\&#x0B;/g' \
-#	    -e 's/
/\&#x0C;/g' \
-#	    -e 's/
/\&#x85;/g'
-	echo "</div></body></html>"
-}
-
-{
-# Very awkward to use in Makefile
-checkall "newline" "
-" newline
-
-# Miight conflict with other uses
-# Slight abuse-of-purpose
-SEP="	" checkall "tab" "	" char
-
-# not commonly-used, never see this in a file path
-# fits the "intended purpose" of the character
-# Not valid HTML5 or XML 1.0
-SEP="
" checkall "record-separator 0x1E" "
" char
-
-# Not valid HTML5 or XML 1.0
-# However this usage matches the "purpose" of the characters and is more flexible than "="
-r1="ab" \
-r2="/lol/wtf=bbq/project std::ftw???\latex{evil}y" \
-checkall "2-separators 0x1E and 0x1F" "
" 2sep
-
-# Valid HTML5, but invalid XML 1.0 (but valid XML 1.1)
-# Abuse-of-purpose
-SEP="
" checkall "form-feed" "
" char
-
-# Invalid in HTML and XML, but "seems like" newline
-SEP="
" checkall "vtab" "
" char
-
-# Downside are that this is encoding-dependent and may copy-paste incorrectly
-# It is acceptable in XML 1.0/1.1
-SEP="
" checkall "next-line 0x85" "
" char
-
-} 2>&1 | tee /dev/stderr | to_html > split_results.html
-
-echo >&2 "===="
-echo >&2 "tests succeeded, html log available in split_results.html"
diff --git a/produce/urlencode.mk b/produce/urlencode.mk
deleted file mode 100755
index af898f3..0000000
--- a/produce/urlencode.mk
+++ /dev/null
@@ -1,20 +0,0 @@
-#!/usr/bin/make -f
-
-# Setting the variable
-
-spm_encode = $(subst\
-$(empty) $(empty),+,$(subst\
-=,%3D,$(subst\
-;,%3B,$(subst\
-+,%2B,$(subst\
-&,%26,$(subst\
-%,%25,$(1)))))))
-
-export override SOURCE_PREFIX_MAP := $(if $(SOURCE_PREFIX_MAP),$(SOURCE_PREFIX_MAP)&,)$(call\
-spm_encode,a a=xxx)=$(call\
-spm_encode,b b)
-
-print-%:; @echo "$($*)"
-default: print-SOURCE_PREFIX_MAP
-
-# We don't expect Makefiles to have to read or apply the variable.

-- 
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