aboutsummaryrefslogtreecommitdiff
path: root/crc16.c
diff options
context:
space:
mode:
Diffstat (limited to 'crc16.c')
-rw-r--r--crc16.c35
1 files changed, 35 insertions, 0 deletions
diff --git a/crc16.c b/crc16.c
new file mode 100644
index 0000000..d0b5f0f
--- /dev/null
+++ b/crc16.c
@@ -0,0 +1,35 @@
+/* $Id$ */
+/*
+ * this source code is based on Rex and Binstock which, in turn,
+ * acknowledges William James Hunt.
+ * source: http://www.darkridge.com/~jpr5/archive/alg/node191.html
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+
+unsigned int crc_16_table[16] = {
+ 0x0000, 0xcc01, 0xd801, 0x1400, 0xf001, 0x3c00, 0x2800, 0xe401,
+ 0xa001, 0x6c00, 0x7800, 0xb401, 0x5000, 0x9c01, 0x8801, 0x4400
+};
+
+unsigned short int
+get_crc_16(int start, char *p, int n)
+{
+ unsigned short int crc;
+ int r;
+
+ for (crc = start; n-- > 0; ++p) {
+ /* compute checksum of lower four bits of *p */
+ r = crc_16_table[crc & 0x0f];
+ crc = (crc >> 4) & 0x0fff;
+ crc ^= r ^ crc_16_table[*p & 0x0f];
+
+ /* compute checksum of upper four bits of *p */
+ r = crc_16_table[crc & 0x0f];
+ crc = (crc >> 4) & 0x0fff;
+ crc ^= r ^ crc_16_table[(*p >> 4) & 0x0f];
+ }
+
+ return crc;
+}