From: Paul Hänsch Date: Tue, 2 Jun 2020 18:00:04 +0000 (+0200) Subject: quicker path sanitizing X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=87f88f16edb809677be04464c15a6c89c3641724;p=cgilite quicker path sanitizing --- diff --git a/cgilite.sh b/cgilite.sh index 384ec19..ddad1d4 100755 --- a/cgilite.sh +++ b/cgilite.sh @@ -27,18 +27,21 @@ BR=' ' cgilite_timeout=2 -PATH(){ - { [ $# -eq 0 ] && cat || printf %s "$*"; } \ - | sed -E 's;^.*$;/&/;; s;/+;/;g; - :X; - s;^/\.\./;/;; s;/\./;/;g; - tX; - s;/[^/]+/\.\./;/;; - tX; - s;^(/.*)/$;\1;' +PATH(){ + local str seg out + [ $# -eq 0 ] && str="$(cat)" || str="$*" + while [ "$str" ]; do + seg=${str%%/*}; str="${str#*/}" + case $seg in + ..) out="${out%/}"; out="${out%/*}/";; + .|'') out="${out%/}/";; + *) out="${out%/}/${seg}";; + esac; + [ "$seg" = "$str" ] && break + done + [ "${str}" -a "${out}" ] && printf %s "$out" || printf %s/ "${out%/}" } - HEX_DECODE=' s;\\;\\\\;g; :HEXDECODE_X; s;%([^0-9A-F]);\\045\1;g; tHEXDECODE_X; # Hexadecimal { %00 - %FF } will be transformed to octal { \000 - \377 } for posix printf