On 27.02.20 19:46, Eric Blake wrote: > On 2/27/20 11:02 AM, Max Reitz wrote: >> Similarly to peek_file_[bl]e, we may want to write binary integers into >> a file. Currently, this often means messing around with poke_file and >> raw binary strings. I hope these functions make it a bit more >> comfortable. >> >> Signed-off-by: Max Reitz <mre...@redhat.com> >> --- >> tests/qemu-iotests/common.rc | 37 ++++++++++++++++++++++++++++++++++++ >> 1 file changed, 37 insertions(+) >> >> diff --git a/tests/qemu-iotests/common.rc b/tests/qemu-iotests/common.rc >> index 4c246c0450..604f837668 100644 >> --- a/tests/qemu-iotests/common.rc >> +++ b/tests/qemu-iotests/common.rc >> @@ -53,6 +53,43 @@ poke_file() >> printf "$3" | dd "of=$1" bs=1 "seek=$2" conv=notrunc &>/dev/null >> } >> +# poke_file_le 'test.img' 512 2 65534 >> +poke_file_le() >> +{ > > I like the interface. However, the implementation is a bit bloated (but > then again, that's why you cc'd me for review ;) > >> + local img=$1 ofs=$2 len=$3 val=$4 str='' >> + >> + for i in $(seq 0 $((len - 1))); do > > No need to fork seq, when we can let bash do the iteration for us: > > while ((len--)); do > >> + byte=$((val & 0xff)) >> + if [ $byte != 0 ]; then >> + chr="$(printf "\x$(printf %x $byte)")" > > Why are we doing two printf command substitutions instead of 1?
Because I had no idea that $() would evaluate '\x*' escape sequences. Interesting. >> + else >> + chr="\0" > > Why do we have to special-case 0? printf '\x00' does the right thing. The non-special-cased version didn’t seem to work for NUL. >> + fi >> + str+="$chr" > > I'd go with the faster str+=$(printf '\\x%02x' $((val & 0xff))), > completely skipping the byte and chr variables. Sure! That’s much better. >> + val=$((val >> 8)) >> + done >> + >> + poke_file "$img" "$ofs" "$str" >> +} > > So my version: > > poke_file_le() > { > local img=$1 ofs=$2 len=$3 val=$4 str= > while ((len--)); do > str+=$(printf '\\x%02x' $((val & 0xff))) > val=$((val >> 8)) > done > poke_file "$img" "$ofs" "$str" > } Much better indeed. >> + >> +# poke_file_be 'test.img' 512 2 65279 >> +poke_file_be() >> +{ >> + local img=$1 ofs=$2 len=$3 val=$4 str='' > > And this one's even easier: we get big-endian for free from printf > output, with a sed post-processing to add \x: > > poke_file_be() > { > local str="$(printf "%0$(($3 * 2))x\n" $4 | sed 's/\(..\)/\\x\1/g')" > poke_file "$1" "$2" "$str" > } Thanks. I knew I could count on you. :) Max
signature.asc
Description: OpenPGP digital signature