diff server/utils.py @ 26:d3e5934fe55c

- Move crc16 to utils and fix it - Fix main.c crc calculation
author Matt Johnston <matt@ucc.asn.au>
date Thu, 07 Jun 2012 20:26:50 +0800
parents 2943f62c8e62
children 340a14fcbaeb
line wrap: on
line diff
--- a/server/utils.py	Wed Jun 06 23:05:35 2012 +0800
+++ b/server/utils.py	Thu Jun 07 20:26:50 2012 +0800
@@ -81,3 +81,19 @@
         if c == '\n':
             return buf
 
+# from http://blog.stalkr.net/2011/04/pctf-2011-32-thats-no-bluetooth.html
+def crc16(buff, crc = 0, poly = 0x8408):
+    l = len(buff)
+    i = 0
+    while i < l:
+        ch = ord(buff[i])
+        uc = 0
+        while uc < 8:
+            if (crc & 1) ^ (ch & 1):
+                crc = (crc >> 1) ^ poly
+            else:
+                crc >>= 1
+            ch >>= 1
+            uc += 1
+        i += 1
+    return crc