#! /opt/bin/dash
#
# os/prog/checksum/post.sh
export PATH="/opt/bin:/bin:/usr/bin"
echo "Content-type: text/html\n\n
checksum"
user=`echo "${REMOTE_USER}" | tr -dc '[:alnum:]' | tr '[:upper:]' '[:lower:]'`
if [ -z "$user" ]; then echo "Bad user: ${REMOTE_USER} -> $user"; exit 1; fi
if [ -n "$HTTP_HOST" ]; then cd /os/prog/checksum || exit 1; fi
# tomato = #FF6347
# lightgreen = #90EE90
#
in=$(cat | egrep -v '#FF6347|#90EE90')
h=$(echo "$in" | awk '{ if( $2 == "P") { print $3; exit; }}')
useed=$(echo "$in" | awk '{ if( $2 == "useed") { gsub(/[^0-9]/,"",$3); print $3; exit; }}')
#echo "$in\n
"
case "$h" in
"2") p="2"; s="123456789";;
*) p="1"; s="123456789";;
esac
if [ -n "$useed" ]; then
seed="$useed"
msg="- seed = $seed"
else
x=$(perl -e "print unpack(\"C*\",$user);"); seed="${s}${x}"
#echo "s = $s, x = $x, seed = $seed
"
msg=""
fi
echo "$user - checksum #$p results $msg
green = correct |
red = wrong |
"
out=$(
(echo "$in"; python3 ./checksum.py -s "$seed" -c) | awk '
function hex(h) { h=substr(h,3); # remove leading 0x
while(length(h)>2 && index(h,"0") == 1) h=substr(h,2); # remove excess leading zeros
while(length(h)<2) h = "0"h; return "0x"h; } # add leading zeros to make length 2
function bin(h) { h=substr(h,3); # remove leading 0b
while(length(h)>8 && index(h,"0") == 1) h=substr(h,2); # remove excess leading zeros
while(length(h)<8) h = "0"h; return "0b"h; } # add leading zeros to make length 8
function bits(d,r) { r=""; # convert to 8-bit binary string
for(i=0;i<8;++i) { r=(and(d,1)?"1":"0")""r; d=rshift(d,1); } return "0b"r; }
BEGIN { print "\n
"; }
{ if( $1 == "") next;
if( $1 == "U") { if( index($3,"0x") == 1) u[$2]=hex($3);
else if( index($3,"0b") == 1) u[$2]=bin($3); else u[$2]=$3;
# print ""$2, u[$2]"
";
} else {
if( $1 == "Add:") { d=$2; x=sprintf("0x%02x",d); b=bits(d);
v=u["A"]; if( v == d || v == x || v == b) color="#90EE90"; else color="#FF6347";
if( v == "") v = " "
print "Add: | "v" |
";
} else if( $1 == "Xor:") { d=$2; x=sprintf("0x%02x",d); b=bits(d);
v=u["X"]; if( v == d || v == x || v == b) color="#90EE90"; else color="#FF6347";
if( v == "") v = " "
print "Xor: | "v" |
";
} else if( $1 == "Fletcher(a,b):") { gsub(/[\(\),]/," ",$0); d1=$5; d2=$6;
x1=sprintf("0x%02x",d1); b1=bits(d1); x2=sprintf("0x%02x",d2); b2=bits(d2);
v1=u["F1"]; if( v1 == d1 || v1 == x1 || v1 == b1) color="#90EE90"; else color="#FF6347";
if( v1 == "") v1 = " "
print "Fletcher1: | "v1" |
";
v2=u["F2"]; if( v2 == d2 || v2 == x2 || v2 == b2) color="#90EE90"; else color="#FF6347";
if( v2 == "") v2 = " "
print "Fletcher2: | "v2" |
";
print "
";
}
}
}'
)
# Add: 196 (0b11000100)
# Xor: 134 (0b10000110)
# Fletcher(a,b): 198,223 (0b11000110,0b11011111)
# after gsub:
# Fletcher a b : 198 223 0b11000110 0b11011111
if [ -z "$useed" ]; then
bad=$(echo "$out" | grep "#FF6347" | wc -l)
good=$(echo "$out" | grep "#90EE90" | wc -l)
log=$(echo "$in" | tr -dc '\-+,:[:alnum:]')
echo "$(date) $user ${p}: good $good bad $bad in: $log" >> log
fi
echo "\n$out
"