Added pycrc library (MIT License) for CRC calculations to the repository.

This commit is contained in:
Christian Sltrop 2012-07-30 15:08:37 +02:00
parent c35e26f025
commit 3233dc96b1
24 changed files with 6136 additions and 0 deletions

283
pycrc/test/main.c Normal file
View file

@ -0,0 +1,283 @@
// Copyright (c) 2006-2010 Thomas Pircher <tehpeh@gmx.net>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#include "crc.h"
#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
#include <string.h>
static bool atob(const char *str);
static crc_t xtoi(const char *str);
static int get_config(int argc, char *argv[], crc_cfg_t *cfg);
#if CRC_ALGO_BIT_BY_BIT
static crc_t crc_verify(const crc_cfg_t *cfg, crc_t crc_pre_final, crc_t crc);
static crc_t crc_reflect(crc_t data, size_t data_len);
#endif
static bool verbose = false;
static unsigned char str[256] = "123456789";
bool atob(const char *str)
{
if (!str) {
return 0;
}
if (isdigit(str[0])) {
return (bool)atoi(str);
}
if (tolower(str[0]) == 't') {
return true;
}
return false;
}
crc_t xtoi(const char *str)
{
crc_t ret = 0;
if (!str) {
return 0;
}
if (str[0] == '0' && tolower(str[1]) == 'x') {
str += 2;
while (*str) {
if (isdigit(*str))
ret = 16 * ret + *str - '0';
else if (isxdigit(*str))
ret = 16 * ret + tolower(*str) - 'a' + 10;
else
return ret;
str++;
}
} else if (isdigit(*str)) {
while (*str) {
if (isdigit(*str))
ret = 10 * ret + *str - '0';
else
return ret;
str++;
}
}
return ret;
}
int get_config(int argc, char *argv[], crc_cfg_t *cfg)
{
int c;
int option_index;
static struct option long_options[] = {
{"width", 1, 0, 'w'},
{"poly", 1, 0, 'p'},
{"reflect-in", 1, 0, 'n'},
{"xor-in", 1, 0, 'i'},
{"reflect-out", 1, 0, 'u'},
{"xor-out", 1, 0, 'o'},
{"verbose", 0, 0, 'v'},
{"check-string", 1, 0, 's'},
{"table-idx-with", 1, 0, 't'},
{0, 0, 0, 0}
};
while (1) {
option_index = 0;
c = getopt_long (argc, argv, "w:p:ni:uo:v", long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 0:
printf ("option %s", long_options[option_index].name);
if (optarg)
printf (" with arg %s", optarg);
printf ("\n");
case 'w':
cfg->width = atoi(optarg);
break;
case 'p':
cfg->poly = xtoi(optarg);
break;
case 'n':
cfg->reflect_in = atob(optarg);
break;
case 'i':
cfg->xor_in = xtoi(optarg);
break;
case 'u':
cfg->reflect_out = atob(optarg);
break;
case 'o':
cfg->xor_out = xtoi(optarg);
break;
case 's':
memcpy(str, optarg, strlen(optarg) < sizeof(str) ? strlen(optarg) + 1 : sizeof(str));
str[sizeof(str) - 1] = '\0';
break;
case 'v':
verbose = true;
break;
case 't':
// ignore --table_idx_width option
break;
case '?':
return -1;
case ':':
fprintf(stderr, "missing argument to option %c\n", c);
return -1;
default:
fprintf(stderr, "unhandled option %c\n", c);
return -1;
}
}
cfg->msb_mask = 1UL << (cfg->width - 1);
cfg->crc_mask = (cfg->msb_mask - 1) | cfg->msb_mask;
cfg->crc_shift = cfg->width < 8 ? 8 - cfg->width : 0;
cfg->poly &= cfg->crc_mask;
cfg->xor_in &= cfg->crc_mask;
cfg->xor_out &= cfg->crc_mask;
return 0;
}
#if CRC_ALGO_BIT_BY_BIT
crc_t crc_verify(const crc_cfg_t *cfg, crc_t crc_pre_final, crc_t crc)
{
crc_t result;
unsigned char data;
unsigned int i;
// don't verify if the width is not a multiple of 8
if (cfg->width % 8) {
return 0;
}
if (cfg->xor_out) {
crc ^= cfg->xor_out;
}
if (cfg->reflect_out) {
crc = crc_reflect(crc, cfg->width);
}
result = crc_pre_final;
for (i = 0; i < cfg->width / 8; i++) {
data = (crc >> (cfg->width - 8 * i - 8)) & 0xff;
if (cfg->reflect_in) {
data = crc_reflect(data, 8);
}
result = crc_update(cfg, result, &data, 1);
}
// no crc_finalize, because if the CRC calculation is correct, result == 0.
// A crc_finalize would XOR-in again some ones into the solution.
// In theory the finalize function of the bit-by-bit algorithm
// would also loop over cfg->width zero bits, but since
// a) result == 0, and
// b) input data == 0
// the output would always be zero
return result;
}
crc_t crc_reflect(crc_t data, size_t data_len)
{
unsigned int i;
crc_t ret;
ret = 0;
for (i = 0; i < data_len; i++)
{
if (data & 0x01) {
ret = (ret << 1) | 1;
} else {
ret = ret << 1;
}
data >>= 1;
}
return ret;
}
#endif // CRC_ALGO_BIT_BY_BIT
int main(int argc, char *argv[])
{
crc_cfg_t cfg = {
0, // width
0, // poly
0, // xor_in
0, // reflect_in
0, // xor_out
0, // reflect_out
0, // crc_mask
0, // msb_mask
0, // crc_shift
};
crc_t crc;
crc_t crc_test, crc_pre_final;
char format[20];
int ret, i;
ret = get_config(argc, argv, &cfg);
if (ret == 0) {
# if CRC_ALGO_TABLE_DRIVEN
crc_table_gen(&cfg);
# endif // CRC_ALGO_TABLE_DRIVEN
crc = crc_init(&cfg);
crc = crc_pre_final = crc_update(&cfg, crc, str, strlen((char *)str));
crc = crc_finalize(&cfg, crc);
# if CRC_ALGO_BIT_BY_BIT
if (crc_verify(&cfg, crc_pre_final, crc) != 0) {
fprintf(stderr, "error: crc verification failed\n");
return 1;
}
# endif
// calculate the checksum again, but this time loop over the input
// bytes one-by-one.
crc_test = crc_init(&cfg);
for (i = 0; str[i]; i++)
{
crc_test = crc_update(&cfg, crc_test, str + i, 1);
}
crc_test = crc_finalize(&cfg, crc_test);
if (crc_test != crc) {
fprintf(stderr, "error: crc loop verification failed\n");
return 1;
}
if (verbose) {
snprintf(format, sizeof(format), "%%-16s = 0x%%0%dx\n", (unsigned int)(cfg.width + 3) / 4);
printf("%-16s = %d\n", "width", (unsigned int)cfg.width);
printf(format, "poly", (unsigned int)cfg.poly);
printf("%-16s = %s\n", "reflect_in", cfg.reflect_in ? "true": "false");
printf(format, "xor_in", cfg.xor_in);
printf("%-16s = %s\n", "reflect_out", cfg.reflect_out ? "true": "false");
printf(format, "xor_out", (unsigned int)cfg.xor_out);
printf(format, "crc_mask", (unsigned int)cfg.crc_mask);
printf(format, "msb_mask", (unsigned int)cfg.msb_mask);
}
printf("0x%lx\n", (unsigned long)crc);
}
return !ret;
}

152
pycrc/test/performance.sh Normal file
View file

@ -0,0 +1,152 @@
#!/bin/bash
set -e
PYCRC=`dirname $0`/../pycrc.py
function cleanup {
rm -f a.out performance.c crc_bbb.[ch] crc_bbf.[ch] crc_tbl.[ch] crc_tb4.[ch]
}
trap cleanup 0 1 2 3 15
prefix=bbb
$PYCRC --model crc-32 --symbol-prefix crc_${prefix}_ --generate c -o crc_$prefix.c --algo bit-by-bit
$PYCRC --model crc-32 --symbol-prefix crc_${prefix}_ --generate h -o crc_$prefix.h --algo bit-by-bit
prefix=bbf
$PYCRC --model crc-32 --symbol-prefix crc_${prefix}_ --generate h -o crc_$prefix.h --algo bit-by-bit-fast
$PYCRC --model crc-32 --symbol-prefix crc_${prefix}_ --generate c -o crc_$prefix.c --algo bit-by-bit-fast
prefix=tbl
$PYCRC --model crc-32 --symbol-prefix crc_${prefix}_ --generate h -o crc_$prefix.h --algo table-driven
$PYCRC --model crc-32 --symbol-prefix crc_${prefix}_ --generate c -o crc_$prefix.c --algo table-driven
prefix=tb4
$PYCRC --model crc-32 --symbol-prefix crc_${prefix}_ --generate h -o crc_$prefix.h --algo table-driven --table-idx-width 4
$PYCRC --model crc-32 --symbol-prefix crc_${prefix}_ --generate c -o crc_$prefix.c --algo table-driven --table-idx-width 4
function print_main {
cat <<EOF
#include "crc_bbb.h"
#include "crc_bbf.h"
#include "crc_tbl.h"
#include "crc_tb4.h"
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <sys/times.h>
#include <unistd.h>
#define NUM_RUNS (256*256)
unsigned char buf[1024];
void test_bbb(unsigned char *buf, size_t buf_len, size_t num_runs, clock_t clock_per_sec);
void test_bbf(unsigned char *buf, size_t buf_len, size_t num_runs, clock_t clock_per_sec);
void test_tbl(unsigned char *buf, size_t buf_len, size_t num_runs, clock_t clock_per_sec);
void test_tb4(unsigned char *buf, size_t buf_len, size_t num_runs, clock_t clock_per_sec);
/**
* Print results.
*
* \param dsc Description of the test
* \param crc Resulting CRC
* \param buflen Length of one buffer
* \param num_runs Number of runs over that buffer
* \param t_user user time
* \param t_sys system time
* \return void
*****************************************************************************/
void show_times(const char *dsc, unsigned int crc, size_t buflen, size_t num_runs, double t_user, double t_sys)
{
printf("%s of %ld bytes (%ld * %ld): 0x%08x\n", dsc, (long)buflen*num_runs, (long)buflen, (long)num_runs, crc);
printf("%13s: %7.3f s\n", "user time", t_user);
printf("%13s: %7.3f s\n", "system time", t_sys);
printf("\n");
}
/**
* C main function.
*
* \retval 0 on success
* \retval 1 on failure
*****************************************************************************/
int main(void)
{
unsigned int i;
long int clock_per_sec;
for (i = 0; i < sizeof(buf); i++) {
buf[i] = (unsigned char)rand();
}
clock_per_sec = sysconf(_SC_CLK_TCK);
// bit-by-bit
test_bbb(buf, sizeof(buf), NUM_RUNS, clock_per_sec);
// bit-by-bit-fast
test_bbf(buf, sizeof(buf), NUM_RUNS, clock_per_sec);
// table-driven
test_tbl(buf, sizeof(buf), NUM_RUNS, clock_per_sec);
// table-driven idx4
test_tb4(buf, sizeof(buf), NUM_RUNS, clock_per_sec);
return 0;
}
EOF
}
function print_routine {
algo=$1
prefix=$2
cat <<EOF
/**
* Test $algo Algorithm.
*
* \return void
*****************************************************************************/
void test_${prefix}(unsigned char *buf, size_t buf_len, size_t num_runs, clock_t clock_per_sec)
{
crc_${prefix}_t crc;
unsigned int i, j;
struct tms tm1, tm2;
times(&tm1);
crc = crc_${prefix}_init();
for (i = 0; i < num_runs; i++) {
crc = crc_${prefix}_update(crc, buf, buf_len);
}
crc = crc_${prefix}_finalize(crc);
times(&tm2);
show_times("CRC32, $algo, block-wise", crc, buf_len, num_runs,
((double)(tm2.tms_utime - tm1.tms_utime) / (double)clock_per_sec),
((double)(tm2.tms_stime - tm1.tms_stime) / (double)clock_per_sec));
times(&tm1);
crc = crc_${prefix}_init();
for (i = 0; i < num_runs; i++) {
for (j = 0; j < buf_len; j++) {
crc = crc_${prefix}_update(crc, &buf[j], 1);
}
}
crc = crc_${prefix}_finalize(crc);
times(&tm2);
show_times("CRC32, $algo, byte-wise", crc, buf_len, num_runs,
((double)(tm2.tms_utime - tm1.tms_utime) / (double)clock_per_sec),
((double)(tm2.tms_stime - tm1.tms_stime) / (double)clock_per_sec));
}
EOF
}
print_main > performance.c
print_routine "bit-by-bit" bbb >> performance.c
print_routine "bit-by-bit-fast" bbf >> performance.c
print_routine "table-driven" tbl >> performance.c
print_routine "table-driven idx4" tb4 >> performance.c
gcc -W -Wall -O3 crc_bbb.c crc_bbf.c crc_tbl.c crc_tb4.c performance.c
./a.out

473
pycrc/test/test.sh Normal file
View file

@ -0,0 +1,473 @@
#!/bin/bash
set -e
PYCRC=`dirname $0`/../pycrc.py
usage() {
echo >&2 "usage: $0 [OPTIONS]"
echo >&2 ""
echo >&2 "with OPTIONS in"
echo >&2 " -c test compiled version"
echo >&2 " -n test compiled version with fixed parameters"
echo >&2 " -f test file"
echo >&2 " -r test random parameters"
echo >&2 " -p test compiled C program with random arguments"
echo >&2 " -w test variable width from 1 to 64"
echo >&2 " -a do all tests"
}
crc_std_test=on
crc_compiled_test=off
crc_compile_noparam_test=off
crc_file_test=off
crc_random_loop_test=off
crc_args_compile_test=off
crc_variable_width_test=off
crc_all_tests=off
while getopts cnfrpwah opt; do
case "$opt" in
c) crc_compiled_test=on;;
n) crc_compile_noparam_test=on;;
f) crc_file_test=on;;
r) crc_random_loop_test=on;;
p) crc_args_compile_test=on;;
w) crc_variable_width_test=on;;
a) crc_all_tests=on;;
h) usage
exit 0
;;
\?) usage # unknown flag
exit 1
;;
esac
done
shift `expr $OPTIND - 1`
cleanup() {
rm -f crc.c crc.h a.out crc-bb crc-bf crc-td2 crc-td4 crc-td8 file.txt
}
trap cleanup 0 1 2 3 15
testres() {
testres_lcmd="$1"
testres_lres="$2"
testres_lres=`echo $testres_lres | sed -e 's/.*0x0*\([0-9a-fA-F][0-9a-fA-F]*\).*/\1/'`
testres_lclc=`$testres_lcmd | sed -e '$!d; s/.*0x0*\([0-9a-fA-F][0-9a-fA-F]*\).*/\1/'`
if [ "$testres_lclc" != "$testres_lres" ]; then
echo >&2 test failed: $testres_lcmd
echo >&2 got $testres_lclc instead of expected $testres_lres
return 1
fi
}
teststr() {
teststr_lopt="$1 --check-string 123456789"
teststr_lres="$2"
testres "$teststr_lopt" "$teststr_lres"
}
compile() {
compile_lalg="$1"
compile_lopt="$2"
compile_lout="$3"
$PYCRC --algorithm "$compile_lalg" $compile_lopt --generate h -o crc.h
ldef=`echo "$compile_lopt" | egrep -c 'width|poly|reflect|xor'` || true
if [ "$ldef" -eq 0 ]; then
$PYCRC --algorithm "$compile_lalg" $compile_lopt --generate c -o crc.c
gcc -W -Wall -pedantic -std=c99 main.c crc.c -o "$compile_lout"
else
$PYCRC --algorithm "$compile_lalg" $compile_lopt --generate c-main -o crc.c
gcc -W -Wall -pedantic -std=c99 crc.c -o "$compile_lout"
fi
}
testcmp() {
testcmp_lalg="$1"
testcmp_lopt="$2"
testcmp_larg="$3"
testcmp_lres="$4"
compile "$testcmp_lalg" "$testcmp_lopt --std C89" "a.out"
testres "./a.out $testcmp_larg" "$testcmp_lres"
compile "$testcmp_lalg" "$testcmp_lopt --std C99" "a.out"
testres "./a.out $testcmp_larg" "$testcmp_lres"
}
testbin() {
testbin_lopt="$1"
testbin_lres="$2"
if [ "$crc_compiled_test" == on -o "$crc_all_tests" == on ]; then
testres "./crc-bb $testbin_lopt" "$testbin_lres"
testres "./crc-bf $testbin_lopt" "$testbin_lres"
testres "./crc-td2 $testbin_lopt" "$testbin_lres"
testres "./crc-td4 $testbin_lopt" "$testbin_lres"
testres "./crc-td8 $testbin_lopt" "$testbin_lres"
if [ "$crc_compile_noparam_test" == on -o "$crc_all_tests" == on ]; then
testcmp bit-by-bit "$testbin_lopt" "" "$testbin_lres"
testcmp bit-by-bit-fast "$testbin_lopt" "" "$testbin_lres"
testcmp table-driven "$testbin_lopt --table-idx-width 2" "" "$testbin_lres"
testcmp table-driven "$testbin_lopt --table-idx-width 4" "" "$testbin_lres"
testcmp table-driven "$testbin_lopt --table-idx-width 8" "" "$testbin_lres"
fi
fi
}
testfil() {
testfil_lopt="$1 --check-file file.txt"
testfil_lres="$2"
if [ "$crc_file_test" == on -o "$crc_all_tests" == on ]; then
testres "$testfil_lopt" "$testfil_lres"
fi
}
if [ "$crc_compiled_test" == on -o "$crc_all_tests" == on ]; then
compile "bit-by-bit" "" "crc-bb"
compile "bit-by-bit-fast" "" "crc-bf"
compile "table-driven" "--table-idx-width 2" "crc-td2"
compile "table-driven" "--table-idx-width 4" "crc-td4"
compile "table-driven" "--table-idx-width 8" "crc-td8"
fi
if [ ! -f file.txt ]; then
echo -n "123456789" > file.txt
fi
if [ "$crc_std_test" == on -o "$crc_all_tests" == on ]; then
#CRC-5
res="0x19"
cmd="$PYCRC --model crc-5"
opt="--width 5 --poly 0x05 --reflect-in 1 --xor-in 0x1f --reflect-out 1 --xor-out 0x1f"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#CRC-8
res="0xf4"
cmd="$PYCRC --model crc-8"
opt="--width 8 --poly 0x07 --reflect-in 0 --xor-in 0x0 --reflect-out 0 --xor-out 0x0"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#DALLAS-1-WIRE
res="0xa1"
cmd="$PYCRC --model dallas-1-wire"
opt="--width 8 --poly 0x31 --reflect-in 1 --xor-in 0 --reflect-out 1 --xor-out 0"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#3gpp
res="0xdaf"
cmd="$PYCRC --model crc-12-3gpp"
opt="--width 12 --poly 0x80f --reflect-in 0 --xor-in 0x0 --reflect-out 1 --xor-out 0x0"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#CRC-15
res="0x59e"
cmd="$PYCRC --model crc-15"
opt="--width 15 --poly 0x4599 --reflect-in 0 --xor-in 0x0 --reflect-out 0 --xor-out 0x0"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#CRC-16/ARC
res="0xbb3d"
cmd="$PYCRC --model crc-16"
opt="--width 16 --poly 0x8005 --reflect-in 1 --xor-in 0x0 --reflect-out 1 --xor-out 0x0"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#CRC-16-USB
res="0xb4c8"
cmd="$PYCRC --model crc-16-usb"
opt="--width 16 --poly 0x8005 --reflect-in 1 --xor-in 0xffff --reflect-out 1 --xor-out 0xffff"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#CRC-16-MODBUS
res="0x4b37"
cmd="$PYCRC --model crc-16-modbus"
opt="--width 16 --poly 0x8005 --reflect-in 1 --xor-in 0xffff --reflect-out 1 --xor-out 0x0"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#CRC-16-GENIBUS
res="0xd64e"
cmd="$PYCRC --model crc-16-genibus"
opt="--width 16 --poly 0x1021 --reflect-in 0 --xor-in 0xffff --reflect-out 0 --xor-out 0xffff"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#CRC-16/CITT
res="0x29b1"
cmd="$PYCRC --model ccitt"
opt="--width 16 --poly 0x1021 --reflect-in 0 --xor-in 0xffff --reflect-out 0 --xor-out 0x0"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#R-CRC-16/DECT packets A-field according to ETSI EN 300 175-3 v2.1.1
res="0x007e"
cmd="$PYCRC --model r-crc-16"
opt="--width 16 --poly 0x0589 --reflect-in 0 --xor-in 0x0 --reflect-out 0 --xor-out 0x0001"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#kermit
res="0x2189"
cmd="$PYCRC --model kermit"
opt="--width 16 --poly 0x1021 --reflect-in 1 --xor-in 0x0 --reflect-out 1 --xor-out 0x0"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#X-25
res="0x906e"
cmd="$PYCRC --model x-25"
opt="--width 16 --poly 0x1021 --reflect-in 1 --xor-in 0xffff --reflect-out 1 --xor-out 0xffff"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#xmodem
res="0x31c3"
cmd="$PYCRC --model xmodem"
opt="--width 16 --poly 0x1021 --reflect-in 0 --xor-in 0x0 --reflect-out 0 --xor-out 0x0"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#zmodem
res="0x31c3"
cmd="$PYCRC --model zmodem"
opt="--width 16 --poly 0x1021 --reflect-in 0 --xor-in 0x0 --reflect-out 0 --xor-out 0x0"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#crc-24
res="0x21cf02"
cmd="$PYCRC --model crc-24"
opt="--width 24 --poly 0x1864cfb --reflect-in 0 --xor-in 0xb704ce --reflect-out 0 --xor-out 0x0"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#CRC-32
res="0xcbf43926"
cmd="$PYCRC --model crc-32"
opt="--width 32 --poly 0x4c11db7 --reflect-in 1 --xor-in 0xffffffff --reflect-out 1 --xor-out 0xffffffff"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#CRC-32C
res="0xe3069283"
cmd="$PYCRC --model crc-32c"
opt="--width 32 --poly 0x1edc6f41 --reflect-in 1 --xor-in 0xffffffff --reflect-out 1 --xor-out 0xffffffff"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#CRC-32/POSIX
res="0x765e7680"
cmd="$PYCRC --model posix"
opt="--width 32 --poly 0x4c11db7 --reflect-in 0 --xor-in 0x0 --reflect-out 0 --xor-out 0xffffffff"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#JAMCRC
res="0x340bc6d9"
cmd="$PYCRC --model jam"
opt="--width 32 --poly 0x4c11db7 --reflect-in 1 --xor-in 0xffffffff --reflect-out 1 --xor-out 0x0"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#CRC-32MPEG
res="0x376e6e7"
cmd="$PYCRC --model crc-32-mpeg"
opt="--width 32 --poly 0x4c11db7 --reflect-in 0 --xor-in 0xffffffff --reflect-out 0 --xor-out 0x0"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#CRC-32-BZIP2
res="0xfc891918"
cmd="$PYCRC --model crc-32-bzip2"
opt="--width 32 --poly 0x04c11db7 --reflect-in 0 --xor-in 0xffffffff --reflect-out 0 --xor-out 0xffffffff"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#XFER
res="0xbd0be338"
cmd="$PYCRC --model xfer"
opt="--width 32 --poly 0x000000af --reflect-in 0 --xor-in 0x0 --reflect-out 0 --xor-out 0x0"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#CRC-64
res="0x46a5a9388a5beffe"
cmd="$PYCRC --model crc-64"
opt="--width 64 --poly 0x000000000000001b --reflect-in 1 --xor-in 0x0 --reflect-out 1 --xor-out 0x0"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#CRC-64-jones
res="0xcaa717168609f281"
cmd="$PYCRC --model crc-64-jones"
opt="--width 64 --poly 0xad93d23594c935a9 --reflect-in 1 --xor-in 0xffffffffffffffff --reflect-out 1 --xor-out 0x0"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
#CRC-64-zx
res="0x995dc9bbdf1939fa"
cmd="$PYCRC --model crc-64-xz"
opt="--width 64 --poly 0x42f0e1eba9ea3693 --reflect-in 1 --xor-in 0xffffffffffffffff --reflect-out 1 --xor-out 0xffffffffffffffff"
teststr "$cmd" "$res"
teststr "$PYCRC $opt" "$res"
testfil "$cmd" "$res"
testbin "$opt" "$res"
fi
if [ "$crc_random_loop_test" == on -o "$crc_all_tests" == on ]; then
for width in 8 16 32; do
for poly in 0x8005 0x4c11db7 0xa5a5a5a5; do
for refxx in "--reflect-in 0 --reflect-out 0" "--reflect-in 0 --reflect-out 1" "--reflect-in 1 --reflect-out 0" "--reflect-in 1 --reflect-out 1"; do
for init in 0x0 0x1 0xa5a5a5a5; do
opt="--width $width --poly $poly $refxx --xor-in $init --xor-out 0x0"
cmd="$PYCRC $opt"
res=`$cmd` || $cmd
testbin "$opt" "$res"
done
done
done
done
fi
if [ "$crc_args_compile_test" == on -o "$crc_all_tests" == on ]; then
#zmodem
res="0x31c3"
opt_width="--width 16"
opt_poly="--poly 0x1021"
opt_refin="--reflect-in 0"
opt_xorin="--xor-in 0x0"
opt_refout="--reflect-out 0"
opt_xorout="--xor-out 0x0"
for b_width in 0 1; do
if [ "$b_width" -eq 1 ]; then cmp_width="$opt_width"; arg_width=""; else cmp_width=""; arg_width="$opt_width"; fi
for b_poly in 0 1; do
if [ "$b_poly" -eq 1 ]; then cmp_poly="$opt_poly"; arg_poly=""; else cmp_poly=""; arg_poly="$opt_poly"; fi
for b_ref_in in 0 1; do
if [ "$b_ref_in" -eq 1 ]; then cmp_refin="$opt_refin"; arg_refin=""; else cmp_refin=""; arg_refin="$opt_refin"; fi
for b_ref_out in 0 1; do
if [ "$b_ref_out" -eq 1 ]; then cmp_refout="$opt_refout"; arg_refout=""; else cmp_refout=""; arg_refout="$opt_refout"; fi
for b_xor_in in 0 1; do
if [ "$b_xor_in" -eq 1 ]; then cmp_xorin="$opt_xorin"; arg_xorin=""; else cmp_xorin=""; arg_xorin="$opt_xorin"; fi
for b_xor_out in 0 1; do
if [ "$b_xor_out" -eq 1 ]; then cmp_xorout="$opt_xorout"; arg_xorout=""; else cmp_xorout=""; arg_xorout="$opt_xorout"; fi
cmp_opt="$cmp_width $cmp_poly $cmp_refin $cmp_refout $cmp_xorin $cmp_xorout"
arg_opt="$arg_width $arg_poly $arg_refin $arg_refout $arg_xorin $arg_xorout"
testcmp bit-by-bit "$cmp_opt" "$arg_opt" "$res"
testcmp bit-by-bit-fast "$cmp_opt" "$arg_opt" "$res"
testcmp table-driven "$cmp_opt" "$arg_opt" "$res"
done
done
done
done
done
done
fi
if [ "$crc_variable_width_test" == on -o "$crc_all_tests" == on ]; then
opt_poly="--poly 0xad93d23594c935a9"
opt_refin="--reflect-in 1"
opt_xorin="--xor-in 0xffffffffffffffff"
opt_refout="--reflect-out 1"
opt_xorout="--xor-out 0x0000000000000000"
for width in 1 2 3 4 5 6 7 8 9 11 12 15 16 17 24 31 32 33 63 64; do
for b_width in 0 1; do
if [ "$b_width" -eq 1 ]; then cmp_width="--width $width"; arg_width=""; else cmp_width=""; arg_width="--width $width"; fi
for b_poly in 0 1; do
if [ "$b_poly" -eq 1 ]; then cmp_poly="$opt_poly"; arg_poly=""; else cmp_poly=""; arg_poly="$opt_poly"; fi
for b_ref_in in 0 1; do
if [ "$b_ref_in" -eq 1 ]; then cmp_refin="$opt_refin"; arg_refin=""; else cmp_refin=""; arg_refin="$opt_refin"; fi
for b_ref_out in 0 1; do
if [ "$b_ref_out" -eq 1 ]; then cmp_refout="$opt_refout"; arg_refout=""; else cmp_refout=""; arg_refout="$opt_refout"; fi
for b_xor_in in 0 1; do
if [ "$b_xor_in" -eq 1 ]; then cmp_xorin="$opt_xorin"; arg_xorin=""; else cmp_xorin=""; arg_xorin="$opt_xorin"; fi
for b_xor_out in 0 1; do
if [ "$b_xor_out" -eq 1 ]; then cmp_xorout="$opt_xorout"; arg_xorout=""; else cmp_xorout=""; arg_xorout="$opt_xorout"; fi
cmp_opt="$cmp_width $cmp_poly $cmp_refin $cmp_refout $cmp_xorin $cmp_xorout"
arg_opt="$arg_width $arg_poly $arg_refin $arg_refout $arg_xorin $arg_xorout"
res=`$PYCRC --algorithm bit-by-bit $cmp_opt $arg_opt`
testcmp table-driven "$cmp_opt" "$arg_opt" "$res"
done
done
done
done
done
done
done
fi
echo Test OK