File: testcrc.c

package info (click to toggle)
modemp3d 0.1-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,688 kB
  • ctags: 1,472
  • sloc: ansic: 12,007; sh: 2,838; makefile: 341; yacc: 285; sed: 93; lex: 33; perl: 31; xml: 10
file content (91 lines) | stat: -rw-r--r-- 2,556 bytes parent folder | download | duplicates (14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*****************************************************************************/

/*
 *      testcrc.c  --  Test the AMSAT CRC table.
 *
 *      Copyright (C) 2000  Thomas Sailer (sailer@ife.ee.ethz.ch)
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Please note that the GPL allows you to use the driver, NOT the radio.
 *  In order to use the radio, you need a license from the communications
 *  authority of your country.
 *
 */

/*****************************************************************************/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "p3dtbl.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* --------------------------------------------------------------------- */

#define BLOCKSZ  512

static int testone(void)
{
	unsigned char data[BLOCKSZ];
	unsigned int i, j;
	u_int16_t crc1 = 0xffff, crc2 = 0xffff;
	
	/* fill block with random bytes */
	for (i = 0; i < BLOCKSZ; i++)
		data[i] = random();
	/* compute conventional CRC */
	for (i = 0; i < BLOCKSZ; i++)
		for (j = 0; j < 8; j++) {
			crc1 = (crc1 << 1) | (((crc1 >> 15) ^ (data[i] >> (7-j))) & 1);
			if (crc1 & 1)
				crc1 ^= (1 << 5) | (1 << 12);
		}
	/* compute table CRC */
	for (i = 0; i < BLOCKSZ; i++)
		crc2 = (crc2 << 8) ^ amsat_crc[((crc2 >> 8) ^ data[i]) & 0xff];
	if (crc1 == crc2)
		return 0;
	printf("CRC error!  conventional CRC 0x%04x, table CRC 0x%04x\n", crc1, crc2);
	for (i = 0; i < BLOCKSZ; i++) {
		if (!(i & 15))
			printf("\n%04x:", i);
		printf(" %02x", data[i]);
	}
	printf("\n");
	return -1;
}

/* --------------------------------------------------------------------- */

int main(int argc, char *argv[])
{
	unsigned int i;

	srandom(time(NULL));
	for (i = 0; i < 131072; i++) {
		if (!(i & 1023)) {
			printf("%u\r", i);
			fflush(stdout);
		}
		if (testone())
			exit(1);
	}
	printf("OK\n");
	exit(0);
}