Skip to content
Permalink
Browse files

scriptlib: further POSIX compatibility

- add support for new ringbuffer format from AVM
  • Loading branch information...
PeterPawn committed Oct 31, 2018
1 parent e61e3fb commit b448012fa0cc9de8c16730719a675eb2455ec059
@@ -35,12 +35,12 @@ yf_base32()
{
for x in $*; do
b=$(( b + 8 ))
v=$(( ( v << 8 ) + $x ))
v=$(( ( v * 256 ) + $x ))
while [ $b -ge 5 ]; do
b=$(( b - 5 ))
d=$(( v >> b ))
printf "%c" $(expr "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456" : ".\{$d\}\(.\).*")
v=$(( v % ( 2 ** b ) ))
v=$(( v % ( 1 << b ) ))
done
done
}
@@ -46,7 +46,7 @@ yf_base32_decode()
v=0
j=0
for c in $part; do
v=$(( ( v << 5 ) + $(yf_index "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456" $c) - 1 ))
v=$(( ( v * 32 ) + $(yf_index "ABCDEFGHIJKLMNOPQRSTUVWXYZ123456" $c) - 1 ))
j=$(( j + 1 ))
[ $(( j % 4 )) -ne 0 ] && continue
# process 16 and 24 bits in two separate steps, this avoids
@@ -36,7 +36,7 @@ yf_base64()
{
if ! [ $1 = eof ]; then
l=$(( l + 1 ))
v=$(( ( v << 8 ) + $1 ))
v=$(( ( v * 256 ) + $1 ))
else
[ $l -eq 0 ] && return
v=$(( v << ( 8 * ( 3 - l ) ) ))
@@ -48,7 +48,7 @@ yf_base64_decode()
for c in $part; do
[ $c = = ] \
&& f=$(( f + 1 )) \
|| v=$(( ( v << 6 ) + $(yf_index "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_/" $c) - 1 ))
|| v=$(( ( v * 64 ) + $(yf_index "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_/" $c) - 1 ))
done
[ $f -gt 0 ] && v=$(( v >> ( f * 2 ) ))
s=16
@@ -42,21 +42,21 @@ yf_bin2dec()
ff=0
while read pos left right; do
if [ $ff -eq 1 ]; then
[ $e -eq 0 ] && v=$(( v << 8 )) || s=$(( s + 8 ))
[ $e -eq 0 ] && v=$(( v * 256 )) || s=$(( s + 8 ))
[ $e -eq 0 ] && ff=255 || ff=$(( 255 << s ))
v=$(( v + ff ))
i=$(( i + 1 ))
ff=0
fi
while [ $i -lt $pos ]; do # insert zeros for missing bytes
[ $e -eq 0 ] && v=$(( v << 8 )) || s=$(( s + 8 ))
[ $e -eq 0 ] && v=$(( v * 256 )) || s=$(( s + 8 ))
i=$(( i + 1 ))
done
if [ $right = 377 ] && [ $ff -eq 0 ]; then
ff=1
continue
fi
[ $e -eq 0 ] && v=$(( v << 8 )) || s=$(( s + 8 ))
[ $e -eq 0 ] && v=$(( v * 256 )) || s=$(( s + 8 ))
[ $e -eq 0 ] && right=$(( 0$right )) || right=$(( 0$right << s ))
v=$(( v + right ))
i=$(( pos + 1 ))
@@ -41,7 +41,7 @@ yf_dec2hex()
else
yf_is_decimal "$len" || return 1
fi
len=$(( len << 1 ))
len=$(( len * 2 ))
mask="%0${len}x"
printf "$mask" $val
return 0
@@ -32,7 +32,7 @@ yf_get_default_gateway_address()
(
addr="$(sed -n -e "s|[^\t]*\t*0\{8\}\t*\([^\t]*\)\t*.*0\{8\}\t*.*\$|\1|p" /proc/net/route)"
[ ${#addr} -eq 0 ] && return 1
if [ $(yf_endianess) == L ]; then
if [ "$(yf_endianess)" = "L" ]; then
addr="$(yf_reverse_hex "$addr")"
fi
yf_print_ip "$addr"
@@ -70,7 +70,7 @@ yf_hex2bin()
fi
h=1
else
v=$(( c << 4 ))
v=$(( c * 16 ))
h=0
fi
done
@@ -32,7 +32,7 @@ yf_hex2dec()
while [ ${#val} -gt 0 ]; do
byte=$(yf_substring "$val" 0 2)
val="$(yf_substring "$val" 2)"
out=$(( ( out << 8 ) + 0x$byte ))
out=$(( ( out * 256 ) + 0x$byte ))
done
printf "%u" "$out"
return 0
@@ -16,17 +16,22 @@
# used (the same size is used by 'showshringbuf', if the file does not exist), #
# the size has to be specified in KBytes (1KByte = 1024 Byte) and it has to be #
# a value between 8 and 64 #
# $3 - 'old' to use the format, which was supported up to FRITZ!OS 07.0x #
# 'new' to use the format, which was introduced in FRITZ!OS 07.0x #
# If this parameter is missing, '/usr/bin/showshringbuf -?' is used to deter- #
# mine the format on the system, where this script was called. #
# #
# return code: #
# 0 - ring buffer file created #
# 1 - parameter error, e.g. the specified facility is invalid (too long or contains #
# invalid character) or the specified size is invalid #
# 2 - ring buffer file does already exist, the file was not changed #
# 3 - no binary found for auto-detection of used format #
# #
#######################################################################################
# #
# U: printf dd #
# W: - #
# W: showshringbuf grep #
# F: yf_is_decimal yf_pack #
# I: - #
# K: fritzbox #
@@ -38,19 +43,37 @@ yf_initialize_ringbuffer()
size="${2:-8}"
[ ${#facility} -eq 0 ] && return 1
[ ${#facility} -gt 8 ] && return 1
check=$(printf "$facility" | sed -e "s|[a-z0-9]||g")
check=$(printf "%s\n" "$facility" | sed -e "s|[a-z0-9]||g")
[ ${#check} -gt 0 ] && return 1
yf_is_decimal $size || return 1
[ $size -ge 8 -a $size -le 64 ] || return 1
( [ $size -ge 8 ] && [ $size -le 64 ] ) || return 1
filename=/var/.srb_$facility
[ -f $filename ] && return 2
maxoffset=$(( ( size * 1024 ) - 1 ))
# file starts with signature 0xF913668E, followed by first, next and last entry and
# finally by the maximum usable offset
yf_pack 8 249 8 19 8 102 8 142 32 20 32 20 32 20 32 $maxoffset >$filename
# first 1K block
dd if=/dev/zero bs=$(( 1024 - 20 )) count=1 >>$filename
# remaining blocks
dd if=/dev/zero bs=1024 count=$(( size - 1 )) >>$filename
f=0
[ "$3" = "old" ] && f=1
[ "$3" = "new" ] && f=2
[ $f -eq 0 ] && showshringbuf -? 2>&1 | grep -q -- '-m[ \t]*- print' && f=2
[ $f -eq 0 ] && return 3
dd if=/dev/zero of=$filename bs=1024 count=$size 2>/dev/null
printf "\371\023\146\216" | $bb dd of=$filename bs=4 count=1 conv=notrunc seek=0 2>/dev/null
if [ $f -eq 1 ]; then
value=$(( size * 1024 - 1 ))
# first entry
printf "\024" | $bb dd of=$file bs=1 count=1 conv=notrunc seek=7 2>/dev/null
# last entry
printf "\024" | $bb dd of=$file bs=1 count=1 conv=notrunc seek=11 2>/dev/null
# next entry
printf "\024" | $bb dd of=$file bs=1 count=1 conv=notrunc seek=15 2>/dev/null
offset=19
else
value=$(( size * 1024 ))
offset=15
fi
while [ $value -gt 0 ]; do
mask=$(printf "\\%03o" $(( value % 256 )))
printf "$mask" | $bb dd of=$file bs=1 count=1 conv=notrunc seek=$offset 2>/dev/null
offset=$(( offset - 1 ))
value=$(( value >> 8 ))
done
return 0
)
@@ -51,16 +51,18 @@ yf_ipv4_address()
error=1
break
fi
addr=$(( ( addr << 8 ) + tupel ))
addr=$(( ( addr * 256 ) + tupel ))
shifts=$(( shifts - 1 ))
else
addr=$(( ( addr << ( shifts * 8 ) ) + tupel ))
addr=$(( ( addr * ( shifts * 256 ) ) + tupel ))
fi
done
if [ $addr -lt $(( 0x01000000 )) ] || [ $addr -gt $(( 0xFFFFFFFF )) ]; then
addr=0
error=1
if [ $error -eq 0 ]; then
if [ $addr -lt $(( 0x01000000 )) ] || [ $addr -gt $(( 0xFFFFFFFF )) ]; then
addr=0
error=1
fi
yf_dec2hex $addr
fi
yf_dec2hex $addr
return $error
)
@@ -86,11 +86,11 @@ yf_pack()
;;
esac
if [ ! -z $endian ]; then
if [ $endian == L ]; then
if [ "$endian" = "L" ]; then
hex="$(yf_reverse_hex "$hex")"
fi
else
if [ $(yf_endianess) == L ]; then
if [ "$(yf_endianess)" = "L" ]; then
hex="$(yf_reverse_hex "$hex")"
fi
fi
@@ -47,7 +47,7 @@ yf_random_string()
else
v=0
while [ ${#val} -gt 0 ]; do
v=$(( ( v << 3 ) + $(expr \( "$val" : "\([0-9]\).*" \) ) ))
v=$(( ( v * 8 ) + $(expr \( "$val" : "\([0-9]\).*" \) ) ))
val="$(expr \( "$val" : "[0-9]\([0-9]*\)" \) )"
done
yf_random_string_read_octals_print "$v" "$len"
@@ -43,71 +43,56 @@
#######################################################################################
yf_readable_size()
(
[ $(( 2 ** 32 )) -eq 0 ] && return 2
v1K=1024
v1M=$(( v1K * v1K ))
v1G=$(( v1M * v1K ))
v1T=$(( v1G * v1K ))
[ $v1T -lt $v1M ] && return 2
[ -z "$1" ] && return 1
u="$(printf "$1" | sed -n -e "s|^[0-9,\.]* \?\([kKmMgGtT]\)\?\$|\1|p" | sed -e "y/kmgt/KMGT/")"
v="$(printf "$1" | sed -n -e "s|^\([0-9,\.]*\) \?[kKmMgGtT]\?\$|\1|p" | sed -e "s|,||")"
if ! [ -z $u ]; then
case $u in
(K)
iu=$(( 2 ** 10 ))
;;
(M)
iu=$(( 2 ** 20 ))
;;
(G)
iu=$(( 2 ** 30 ))
;;
(T)
iu=$(( 2 ** 40 ))
;;
(*)
return 1
;;
esac
else
iu=1
fi
u="$(printf "%s\n" "$1" | sed -n -e "s|^[0-9,\.]* \?\([kKmMgGtT]\)\?\$|\1|p" | sed -e "y/kmgt/KMGT/")"
v="$(printf "%s\n" "$1" | sed -n -e "s|^\([0-9,\.]*\) \?[kKmMgGtT]\?\$|\1|p" | sed -e "s|,||")"
iu=1
[ -z $u ] || iu=$(eval printf "%u" "\$v1$u")
f=0
[ $(yf_index $v .) -gt 0 ] && [ $iu -eq 1 ] && return 1
if [ $(yf_index $v .) -gt 0 ]; then
l=$(printf "$v" | sed -e "s|[^\.]||g")
l=$(printf "$v\n" | sed -e "s|[^\.]||g")
[ ${#l} -gt 1 ] && return 1
f=$(printf "${v##*.}000" | sed -n -e "s|^\([0-9]\{3\}\).*\$|\1|p" | sed -e "s|^0*||")
f=$(printf "%s000\n" "${v##*.}" | sed -n -e "s|^\([0-9]\{3\}\).*\$|\1|p" | sed -e "s|^0*||")
v=${v%%.*}
f=$(( f * iu ))
f=$(( ( f * 2 ** 10 ) / 10 ** 3 ))
f=$(( ( f * v1K ) / 1000 ))
fi
[ $(( v * iu )) -ge $(( 2 ** 53 )) ] && return 1
v=$(( v * iu * 2 ** 10 ))
[ $(( v * iu )) -ge $(( 16384 * v1T )) ] && return 1
v=$(( v * iu * v1K ))
v=$(( v + f ))
if ! [ -z "$2" ]; then
case "$2" in
([Kk])
c=$(( v ))
c=$(( v / v1K ))
ou=$2
;;
([Mm])
c=$(( v / 2 ** 10 ))
c=$(( v / v1M ))
ou=$2
;;
([Gg])
c=$(( v / 2 ** 20 ))
c=$(( v / v1G ))
ou=$2
;;
([Tt])
c=$(( v / 2 ** 30 ))
c=$(( v / v1T ))
ou=$2
;;
(*)
return 1
;;
esac
d=$(( c % 2 ** 20 ))
c=$(( c / 2 ** 20 ))
d=$(( c % v1K ))
c=$(( c / v1K ))
else
ou=""
while [ $v -gt $(( 2 ** 20 )) ]; do
while [ $v -gt $v1M ]; do
case $ou in
(T)
break
@@ -125,28 +110,28 @@ yf_readable_size()
ou=K
;;
esac
c=$(( v / 2 ** 10 ))
d=$(( v % 2 ** 10 ))
v=$(( v / 2 ** 10 ))
c=$(( v / v1K ))
d=$(( v % v1K ))
v=$(( v / v1K ))
done
d=$(( c % 2 ** 10 ))
c=$(( c / 2 ** 10 ))
d=$(( c % v1K ))
c=$(( c / v1K ))
fi
if [ $d -ne 0 ]; then
while [ $d -gt $(( 2 ** 10 * 10 )) ]; do
d=$(( d / 2 ** 10 ))
while [ $d -gt $(( v1K * 10 )) ]; do
d=$(( d / v1K ))
done
d=$(( d * 10 ** 3 / 2 ** 10 ))
while [ $d -gt $(( 10 ** 3 )) ]; do
d=$(( d * 1000 / v1K ))
while [ $d -gt 1000 ]; do
d=$(( ( d + 5 ) / 10 ))
done
d=$(( ( d + 5 ) / 10 ))
d=$(printf "%02u" $d | sed -n -e "s|^\([0-9]\{2\}\).*\$|\1|p")
d=$(printf "%02u\n" $d | sed -n -e "s|^\([0-9]\{2\}\).*\$|\1|p")
while [ ${d%0} != $d ]; do
d=${d%0}
done
printf "%u.%s %c" $c $d $ou
printf "%u.%s %c\n" $c $d $ou
else
printf "%u %c" $c $ou
printf "%u %c\n" $c $ou
fi
)

0 comments on commit b448012

Please sign in to comment.
You can’t perform that action at this time.