summaryrefslogtreecommitdiffhomepage
path: root/src/crc.c
diff options
context:
space:
mode:
authormimaki <[email protected]>2012-04-20 09:39:03 +0900
committermimaki <[email protected]>2012-04-20 09:39:03 +0900
commite0d6430f63c4cbe0c71ce82ee23284671389a818 (patch)
tree41abad7f12eced98d9ac14d141cea62464c3332f /src/crc.c
parent54ad561098ed353ada70205c39b2c42a2a2eb9e5 (diff)
downloadmruby-e0d6430f63c4cbe0c71ce82ee23284671389a818.tar.gz
mruby-e0d6430f63c4cbe0c71ce82ee23284671389a818.zip
add mruby sources
Diffstat (limited to 'src/crc.c')
-rw-r--r--src/crc.c28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/crc.c b/src/crc.c
new file mode 100644
index 000000000..513622a09
--- /dev/null
+++ b/src/crc.c
@@ -0,0 +1,28 @@
+#include <limits.h>
+#include <stdint.h>
+// Calculate CRC (CRC-16-CCITT)
+//
+// 0000_0000_0000_0000_0000_0000_0000_0000
+// ^|------- CRC -------|- work --|
+// carry
+#define CRC_16_CCITT 0x11021ul //x^16+x^12+x^5+1
+#define CRC_XOR_PATTERN (CRC_16_CCITT << 8)
+#define CRC_CARRY_BIT (1 << 24)
+
+uint16_t
+calc_crc_16_ccitt(unsigned char *src, int nbytes)
+{
+ uint32_t crcwk = 0ul;
+ int ibyte, ibit;
+
+ for (ibyte = 0; ibyte < nbytes; ibyte++) {
+ crcwk |= *src++;
+ for (ibit = 0; ibit < CHAR_BIT; ibit++) {
+ crcwk <<= 1;
+ if (crcwk & CRC_CARRY_BIT) {
+ crcwk ^= CRC_XOR_PATTERN;
+ }
+ }
+ }
+ return (uint16_t)(crcwk >> 8);
+}