summaryrefslogtreecommitdiff
path: root/cesar/maximus/python/lib/fcVf/crc24
diff options
context:
space:
mode:
Diffstat (limited to 'cesar/maximus/python/lib/fcVf/crc24')
-rw-r--r--cesar/maximus/python/lib/fcVf/crc24/__init__.py1
-rw-r--r--cesar/maximus/python/lib/fcVf/crc24/crc24.py84
-rw-r--r--cesar/maximus/python/lib/fcVf/crc24/crc24_algorithms.py172
-rw-r--r--cesar/maximus/python/lib/fcVf/crc24/pycrc24.py44
4 files changed, 301 insertions, 0 deletions
diff --git a/cesar/maximus/python/lib/fcVf/crc24/__init__.py b/cesar/maximus/python/lib/fcVf/crc24/__init__.py
new file mode 100644
index 0000000000..96bcf63b8d
--- /dev/null
+++ b/cesar/maximus/python/lib/fcVf/crc24/__init__.py
@@ -0,0 +1 @@
+from pycrc24 import *
diff --git a/cesar/maximus/python/lib/fcVf/crc24/crc24.py b/cesar/maximus/python/lib/fcVf/crc24/crc24.py
new file mode 100644
index 0000000000..3f453d6271
--- /dev/null
+++ b/cesar/maximus/python/lib/fcVf/crc24/crc24.py
@@ -0,0 +1,84 @@
+# -*- coding: Latin-1 -*-
+
+
+# Class Options
+###############################################################################
+class Options(object):
+ """
+ The options parsing and validationg class
+ """
+
+ """
+ Bitmap of the algorithms
+ """
+ Algo_None = 0x00
+ Algo_Bit_by_Bit = 0x01
+ Algo_Bit_by_Bit_Fast = 0x02
+ Algo_Table_Driven = 0x04
+
+ # Class constructor
+ ###############################################################################
+ def __init__(self, version):
+ self.ProgramName = "pycrc"
+ self.Version = version
+ self.VersionStr = "%s v%s" % (self.ProgramName, self.Version)
+ self.WebAddress = "http://www.tty1.net/pycrc/"
+ self.Width = None
+ self.Poly = None
+ self.ReflectIn = None
+ self.XorIn = None
+ self.ReflectOut = None
+ self.XorOut = None
+ self.TableIdxWidth = 8
+ self.TableWidth = 1 << self.TableIdxWidth
+ self.Verbose = False
+ self.CheckString = "123456789"
+
+ self.Algorithm = self.Algo_None
+ self.SymbolPrefix = "crc_"
+ self.OutputFile = None
+ self.Action = "check_string"
+ self.CStd = None
+
+
+ #CRC24 :
+ self.Width=24
+ self.Poly=0x864cfbL
+ self.ReflectIn=False
+ self.XorIn=0xb704ceL
+ self.ReflectOut=False
+ self.XorOut=0x0L
+ self.parse()
+
+
+ # function parse
+ ###############################################################################
+ def parse(self):
+ """
+ Parses and validates the options given as arguments
+ """
+ if self.Width != None:
+ if self.Width <= 0:
+ sys.stderr.write("Error: Width must be strictly positive\n")
+ sys.exit(1)
+ self.MSB_Mask = 0x1 << (self.Width - 1)
+ self.Mask = ((self.MSB_Mask - 1) << 1) | 1
+ if self.Poly != None:
+ self.Poly = self.Poly & self.Mask
+ if self.XorIn != None:
+ self.XorIn = self.XorIn & self.Mask
+ if self.XorOut != None:
+ self.XorOut = self.XorOut & self.Mask
+ else:
+ self.MSB_Mask = None
+ self.Mask = None
+
+ if self.Width == None or \
+ self.Poly == None or \
+ self.ReflectIn == None or \
+ self.XorIn == None or \
+ self.ReflectOut == None or \
+ self.XorOut == None:
+ self.UndefinedCrcParameters = True
+ else:
+ self.UndefinedCrcParameters = False
diff --git a/cesar/maximus/python/lib/fcVf/crc24/crc24_algorithms.py b/cesar/maximus/python/lib/fcVf/crc24/crc24_algorithms.py
new file mode 100644
index 0000000000..9158758dd4
--- /dev/null
+++ b/cesar/maximus/python/lib/fcVf/crc24/crc24_algorithms.py
@@ -0,0 +1,172 @@
+# -*- coding: Latin-1 -*-
+
+# Class Crc
+###############################################################################
+class Crc(object):
+ """
+ A base class for CRC routines.
+ """
+
+ # constructor
+ ###############################################################################
+ def __init__(self, opt):
+ """The Crc constructor.
+
+ The opt parameter is an object containing the following members:
+ Width
+ Poly
+ ReflectIn
+ XorIn
+ ReflectOut
+ XorOut
+ """
+ self.Width = opt.Width
+ self.Poly = opt.Poly
+ self.ReflectIn = opt.ReflectIn
+ self.XorIn = opt.XorIn
+ self.ReflectOut = opt.ReflectOut
+ self.XorOut = opt.XorOut
+
+ self.MSB_Mask = 0x1 << (opt.Width - 1)
+ self.Mask = ((opt.MSB_Mask - 1) << 1) | 1
+ if opt.TableIdxWidth != None:
+ self.TableIdxWidth = opt.TableIdxWidth
+ self.TableWidth = 1 << opt.TableIdxWidth
+ else:
+ self.TableIdxWidth = 8
+ self.TableWidth = 1 << self.TableIdxWidth
+
+ # function reflect
+ ###############################################################################
+ def reflect(self, data, width):
+ """
+ reflects a data word, i.e. reverts the bit order
+ """
+ x = 0
+ for i in range(width):
+ x = x | (((data >> (width - i -1)) & 1) << i)
+ return x
+
+ # function handle_bit
+ ###############################################################################
+ def __handle_bit(self, register, new_bit):
+ """
+ This function is part of the bit_by_bit algorithm.
+ It function takes one bit from the augmented message as argument and returns the new crc value
+ """
+ register_msb = register & self.MSB_Mask
+ register = (register << 1) & self.Mask
+ if new_bit != 0:
+ register = register | 1
+ if register_msb != 0:
+ register = register ^ self.Poly
+ return register & self.Mask
+
+ # function bit_by_bit
+ ###############################################################################
+ def bit_by_bit(self, str):
+ """
+ Classic simple and slow CRC implementation.
+ This function iterates bit by bit over the augmented input message and returns the calculated CRC value at the end
+ """
+ register = self.XorIn
+ for j in range(self.Width):
+ bit = register & 1
+ if bit != 0:
+ register = ((register ^ self.Poly) >> 1) | self.MSB_Mask
+ else:
+ register = register >> 1
+ register &= self.Mask
+
+ for i in range(len(str)):
+ octet = ord(str[i])
+ if self.ReflectIn:
+ octet = self.reflect(octet, 8)
+ for j in range(8):
+ new_bit = octet & (0x80 >> j)
+ register = self.__handle_bit(register, new_bit)
+ for j in range(self.Width):
+ register = self.__handle_bit(register, 0)
+
+ if self.ReflectOut:
+ register = self.reflect(register, self.Width)
+ register = register ^ self.XorOut
+ return register
+
+ # function bit_by_bit_fast
+ ###############################################################################
+ def bit_by_bit_fast(self, str):
+ """
+ This is a slightly modified version of the bit_by_bit algorithm: it does not need to loop over the augmented bit,
+ i.e. the Width 0-bits wich are appended to the input message in the bit_by_bit algorithm.
+ """
+ register = self.XorIn
+
+ for i in range(len(str)):
+ octet = ord(str[i])
+ if self.ReflectIn:
+ octet = self.reflect(octet, 8)
+ for j in range(8):
+ bit = register & self.MSB_Mask
+ register <<= 1
+ if octet & (0x80 >> j):
+ bit ^= self.MSB_Mask
+ if bit:
+ register ^= self.Poly
+ register &= self.Mask
+ if self.ReflectOut:
+ register = self.reflect(register, self.Width)
+ register = register ^ self.XorOut
+ return register
+
+ # function gen_table
+ ###############################################################################
+ def gen_table(self):
+ """
+ This function generates the CRC table used for the table_driven CRC algorithm.
+ The Python version cannot handle tables of a different size rather than 8.
+ See the generated C code for tables with different sizes instead.
+ """
+ tbl = {}
+ for i in range(1 << self.TableIdxWidth):
+ register = i
+ if self.ReflectIn:
+ register = self.reflect(register, self.TableIdxWidth)
+ register = register << (self.Width - self.TableIdxWidth)
+ for j in range(self.TableIdxWidth):
+ if register & self.MSB_Mask != 0:
+ register = (register << 1) ^ self.Poly
+ else:
+ register = (register << 1)
+ if self.ReflectIn:
+ register = self.reflect(register, self.Width)
+ tbl[i] = register & self.Mask
+ return tbl
+
+ # function table_driven
+ ###############################################################################
+ def table_driven(self, str):
+ """
+ The Standard table_driven CRC algorithm.
+ """
+ tbl = self.gen_table()
+
+ if not self.ReflectIn:
+ register = self.XorIn
+ for i in range(len(str)):
+ octet = ord(str[i])
+ tblidx = ((register >> (self.Width - 8)) ^ octet) & 0xff
+ register = ((register << 8) ^ tbl[tblidx]) & self.Mask
+ else:
+ register = self.reflect(self.XorIn, self.Width)
+ for i in range(len(str)):
+ octet = ord(str[i])
+ tblidx = (register ^ octet) & 0xff
+ register = ((register >> 8) ^ tbl[tblidx]) & self.Mask
+ register = self.reflect(register, self.Width)
+
+ if self.ReflectOut:
+ register = self.reflect(register, self.Width)
+ register = register ^ self.XorOut
+ return register
+
diff --git a/cesar/maximus/python/lib/fcVf/crc24/pycrc24.py b/cesar/maximus/python/lib/fcVf/crc24/pycrc24.py
new file mode 100644
index 0000000000..2f8f4df7fc
--- /dev/null
+++ b/cesar/maximus/python/lib/fcVf/crc24/pycrc24.py
@@ -0,0 +1,44 @@
+# -*- coding: Latin-1 -*-
+
+from crc24 import Options
+from crc24_algorithms import Crc
+
+
+# function check_string
+###############################################################################
+def check_string(myString):
+
+
+ opt=Options("0.6.4")
+ opt.CheckString=myString
+ """
+ Returns the calculated CRC sum of a string
+ """
+ if opt.UndefinedCrcParameters:
+ sys.stderr.write("Error: undefined parameters\n")
+ sys.exit(1)
+ if opt.Algorithm == 0:
+ opt.Algorithm = opt.Algo_Bit_by_Bit | opt.Algo_Bit_by_Bit_Fast | opt.Algo_Table_Driven
+
+ alg = Crc(opt)
+ crc = this_crc = None
+ if opt.Algorithm & opt.Algo_Bit_by_Bit:
+ this_crc = alg.bit_by_bit(opt.CheckString)
+ if crc != None and this_crc != crc:
+ sys.stderr.write("Error: different checksums: 0x%x, 0x%x\n" % (this_crc, crc))
+ sys.exit(1)
+ crc = this_crc
+ if opt.Algorithm & opt.Algo_Bit_by_Bit_Fast:
+ this_crc = alg.bit_by_bit_fast(opt.CheckString)
+ if crc != None and this_crc != crc:
+ sys.stderr.write("Error: different checksums: 0x%x, 0x%x\n" % (this_crc, crc))
+ sys.exit(1)
+ crc = this_crc
+ if opt.Algorithm & opt.Algo_Table_Driven:
+ opt.TableIdxWidth = 8 # FIXME cowardly refusing to use less bits for the table
+ this_crc = alg.table_driven(opt.CheckString)
+ if crc != None and this_crc != crc:
+ sys.stderr.write("Error: different checksums: 0x%x, 0x%x\n" % (this_crc, crc))
+ sys.exit(1)
+ crc = this_crc
+ return crc