From 280bf2c08da810bc4b31bff91ad77a366e749e2a Mon Sep 17 00:00:00 2001 From: paul Date: Thu, 9 Nov 2017 03:35:08 +0000 Subject: [PATCH] transform hex to oct for posix compatibility in printf svn path=/trunk/; revision=58 --- cgilite.sh | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/cgilite.sh b/cgilite.sh index e263424..cbfa724 100755 --- a/cgilite.sh +++ b/cgilite.sh @@ -44,7 +44,14 @@ cgilite_value(){ REF) printf %s "&${HTTP_REFERER#*\?}";; esac \ | grep -Eo '&'"$2"'=[^&]*' \ - | sed -rn "${3:-1}"'{s;^[^=]+=;;; s;\+; ;g; s;\\;\\\\;g; s;%;\\x;g; p}' + | sed -rn "${3:-1}"'{s;^[^=]+=;;; s;\+; ;g; s;\\;\\\\;g; + # Hexadecimal { %00 - %FF } will be transformed to octal { \000 - \377 } for posix printf + s;%[0123].;&\\0;g; s;%[4567].;&\\1;g; s;%[89AB].;&\\2;g; s;%[CDEF].;&\\3;g; + s;%[048C][0-7]\\.;&0;g; s;%[048C][89A-F]\\.;&1;g; s;%[159D][0-7]\\.;&2;g; s;%[159D][89A-F]\\.;&3;g; + s;%[26AE][0-7]\\.;&4;g; s;%[26AE][89A-F]\\.;&5;g; s;%[37BF][0-7]\\.;&6;g; s;%[37BF][89A-F]\\.;&7;g; + s;%.[08](\\..);\10;g; s;%.[19](\\..);\11;g; s;%.[2A](\\..);\12;g; s;%.[3B](\\..);\13;g; + s;%.[4C](\\..);\14;g; s;%.[5D](\\..);\15;g; s;%.[6E](\\..);\16;g; s;%.[7F](\\..);\17;g; + p}' )" } @@ -61,7 +68,14 @@ COOKIE(){ printf "$( printf %s " ${HTTP_COOKIE}" \ | grep -Eo '[; ]'"$1"'=[^;]*' \ - | sed -rn "${2:-1}"'{s;^[^=]+=;;; s;\+; ;g; s;\\;\\\\;g; s;%;\\x;g; p}' + | sed -rn "${2:-1}"'{s;^[^=]+=;;; s;\+; ;g; s;\\;\\\\;g; + # Hexadecimal { %00 - %FF } will be transformed to octal { \000 - \377 } for posix printf + s;%[0123].;&\\0;g; s;%[4567].;&\\1;g; s;%[89AB].;&\\2;g; s;%[CDEF].;&\\3;g; + s;%[048C][0-7]\\.;&0;g; s;%[048C][89A-F]\\.;&1;g; s;%[159D][0-7]\\.;&2;g; s;%[159D][89A-F]\\.;&3;g; + s;%[26AE][0-7]\\.;&4;g; s;%[26AE][89A-F]\\.;&5;g; s;%[37BF][0-7]\\.;&6;g; s;%[37BF][89A-F]\\.;&7;g; + s;%.[08](\\..);\10;g; s;%.[19](\\..);\11;g; s;%.[2A](\\..);\12;g; s;%.[3B](\\..);\13;g; + s;%.[4C](\\..);\14;g; s;%.[5D](\\..);\15;g; s;%.[6E](\\..);\16;g; s;%.[7F](\\..);\17;g; + p}' )" } @@ -73,12 +87,13 @@ HTML(){ | hexdump -ve '/1 "%03o\n"' \ | while read n; do case $n in - [01]??) printf '0000%s' $n;; - 2??) printf '%s' ${n#2};; - 3[0123]?) printf '000%s' ${n#3};; - 34?) printf '00%s' ${n#34};; - 35?) printf '01%s' ${n#35};; - 36?) printf '%s' ${n#36};; + # bitbanging octal UTF-8 chains into singular 7 digit octal numbers + [01]??) printf '0000%s' $n;; # 7 bit ASCII character, nothing to do + 2??) printf '%s' ${n#2};; # tail fragment, append 6 bit + 3[0123]?) printf '000%s' ${n#3};; # 2 octet (11 bit) chain start + 34?) printf '00%s' ${n#34};; # 3 octet (16 bit) chain start + 35?) printf '01%s' ${n#35};; # 3 octet chain start, high + 36?) printf '%s' ${n#36};; # 4 octet (21 bit) chain start esac done \ | sed -r 's;.{7};&\n;g;' \ -- 2.39.2