initrd: escape_zero: allow some more characters

Since it's not supposed to be shell safe, just display safe
inside double quotes, we can allow some more characters.

Also fix the escape character not being escaped.
This commit is contained in:
3hhh 2023-01-14 13:14:09 +01:00
parent 2ae7f53e04
commit d07df1e60b
No known key found for this signature in database
GPG Key ID: EB03A691DB2F0833

View File

@ -339,10 +339,11 @@ print_tree() {
find ./ ! -path './kexec*' -print0 | sort -z find ./ ! -path './kexec*' -print0 | sort -z
} }
# Escape zero-delimited standard input for use in shell. # Escape zero-delimited standard input to safely display it to the user in e.g.
# These characters are passed verbatim: a-zA-Z0-9,._+:@%/- # `whiptail`, `less`, `echo`, `cat`. Doesn't produce shell-escaped output.
# These escapes are used to replace their corresponding characters: #n#r#t# #v#b # Most printable characters are passed verbatim (exception: \).
# Other characters are rendered as hexadecimal escapes # These escapes are used to replace their corresponding characters: #n#r#t#v#b
# Other characters are rendered as hexadecimal escapes.
# escape_zero [prefix] [escape character] # escape_zero [prefix] [escape character]
# prefix: \0 in the input will result in \n[prefix] # prefix: \0 in the input will result in \n[prefix]
# escape character: character to use for escapes (default: #); \ may be interpreted by `whiptail` # escape character: character to use for escapes (default: #); \ may be interpreted by `whiptail`
@ -350,9 +351,11 @@ escape_zero() {
local prefix="$1" local prefix="$1"
local echar="${2:-#}" local echar="${2:-#}"
local todo="" local todo=""
local echar_hex="$(echo -n "$echar" | xxd -p -c1)"
[ ${#echar_hex} -eq 2 ] || die "Invalid escape character $echar passed to escape_zero(). Programming error?!"
echo -e -n "$prefix" echo -e -n "$prefix"
xxd -p | tr -d '\n' | xxd -p -c1 | tr -d '\n' |
{ {
while IFS= read -r -n2 -d '' ; do while IFS= read -r -n2 -d '' ; do
if [ -n "$todo" ] ; then if [ -n "$todo" ] ; then
@ -380,16 +383,15 @@ escape_zero() {
0d) 0d)
echo -n "${echar}r" echo -n "${echar}r"
;; ;;
"$echar_hex")
20) echo -n "$echar$echar"
echo -n "${echar} "
;; ;;
#%+,-./ 0-9: @A-0 P-Z_ a-o p-z #interpreted characters:
2[5b-f]|3[0-9a]|4[0-9a-f]|5[0-9af]|6[1-f]|7[0-9a]) 2[0-9a-f]|3[0-9a-f]|4[0-9a-f]|5[0-9abd-f]|6[0-9a-f]|7[0-9a-e])
echo -e -n '\x'"$REPLY" echo -e -n '\x'"$REPLY"
;; ;;
# All others are escaped # All others are escaped
[0-9a-f][0-9a-f]) *)
echo -n "${echar}x$REPLY" echo -n "${echar}x$REPLY"
;; ;;
esac esac