CRC-32: Difference between revisions
m
→POSIX
m (→POSIX) |
|||
Line 2,110:
It costs less to recompute polynomal shift for each character than indexing
with external tools like <code>awk</code> or <code>tr</code>.
<lang
# POSIX Shell CRC32 of string
# @Name: crc32.sh
# @Version: 1.0.
# @Author: Léa Gris <lea.gris@noiraude.net>
# @Date: Wed, 27 Mar 2019
Line 2,128:
crc32_stream() {
crc=0xFFFFFFFF # The Initial CRC32 value
p=0xedb88320 # The CRC32
r=0 # The
c='' # The current character
byte=0 #
# Iterates each character of the input stream
while c="$(dd bs=1 count=1 2>/dev/null)" && [ -n "$c" ]; do
byte=$(printf '%d' "'$
r=$(((crc & 0xff) ^ byte))
# 8-bit lsb shift with XOR polynomial reminder when odd
r=$(((r &
done
crc=$(((crc >> 8) ^ r)) # XOR MSB of CRC with Reminder
Line 2,148:
}
# Computes the CRC32 of the argument string
# 1: The argument string
# >&1: The CRC32 integer of the argument string
crc32_string() {
[ $# -eq 1 ] || return # argument required
# Streams with printf to prevent
# with a newline, since echo -n is not available in POSIX Shell
printf '%s' "$1" | crc32_stream
|