Jump to content

CRC-32: Difference between revisions

m
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 Bashbash>#!/usr/bin/env sh
# POSIX Shell CRC32 of string
# @Name: crc32.sh
# @Version: 1.0.01
# @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 polynomalpolynomial
r=0 # The polynomalpolynomial reminder
c='' # The current character
byte=0 # TgeThe byte value of the current character
# Iterates each character of the input stream
while c="$(dd bs=1 count=1 2>/dev/null)" && [ -n "$c" ]; do
byte=$(printf '%d' "'${c}") # Converts the character into its byte value
r=$(((crc & 0xff) ^ byte)) # XOR LSB of CRC with current byte
# 8-bit lsb shift with XOR polynomial reminder when odd
b=0 # bit index
#for 8-bit_ lsbin shift_ with_ XOR_ polynomal_ reminder_ when_ odd_ _; do
while [ $((b <= 7)) -ne 0 ] && bt=$((br +>> 1)); do
r=$(((r & 0x11) ? (r >> 1)t ^ p : r >> 1t))
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 postfixingsuffixing the string
# with a newline, since echo -n is not available in POSIX Shell
printf '%s' "$1" | crc32_stream
5

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.