Permalink
Browse files

2009-02-15

	* crc_algorithms.py: code tidy-up.
	* crc_algorithms.py, crc_opt.py: added --direct option.
	* crc_symtable.py: added --direct option for the generated code.
2009-02-03
	* crc_opt.py: added --check-hexstring option. Closes issue 2545183.
	Thanks to Arnim Littek.
2009-01-31
	* crc_opt.py: added a check for extra arguments on the command line.
	Closes issue 2545185. Thanks to Arnim Littek.
2008-12-24
	* doc/pycrc.xml: Added one more example.
  • Loading branch information...
1 parent 96249d7 commit 5796fa15e59a75b5a62bce8bc8a955d543ed40c8 @tpircher committed Feb 27, 2009
Showing with 304 additions and 143 deletions.
  1. +1 −1 COPYING
  2. +24 −0 ChangeLog
  3. +6 −6 README
  4. +75 −58 crc_algorithms.py
  5. +1 −1 crc_lexer.py
  6. +21 −1 crc_models.py
  7. +22 −4 crc_opt.py
  8. +1 −1 crc_parser.py
  9. +36 −28 crc_symtable.py
  10. +28 −4 doc/pycrc.xml
  11. +51 −20 pycrc.py
  12. +38 −19 test/test.sh
View
@@ -1,4 +1,4 @@
-Copyright (c) 2006-2007, Thomas Pircher <tehpeh@gmx.net>
+Copyright (c) 2006-2009, Thomas Pircher <tehpeh@gmx.net>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
View
@@ -1,3 +1,27 @@
+#
+# Version 0.7, 2009-02-27
+#
+
+2009-02-15 Thomas Pircher <tehpeh@gmx.net>
+
+ * crc_algorithms.py: code tidy-up.
+ * crc_algorithms.py, crc_opt.py: added --direct option.
+ * crc_symtable.py: added --direct option for the generated code.
+
+2009-02-03 Thomas Pircher <tehpeh@gmx.net>
+
+ * crc_opt.py: added --check-hexstring option. Closes issue 2545183.
+ Thanks to Arnim Littek.
+
+2009-01-31 Thomas Pircher <tehpeh@gmx.net>
+
+ * crc_opt.py: added a check for extra arguments on the command line.
+ Closes issue 2545185. Thanks to Arnim Littek.
+
+2008-12-24 Thomas Pircher <tehpeh@gmx.net>
+
+ * doc/pycrc.xml: Added one more example.
+
#
# Version 0.6.7, 2008-12-11
#
View
12 README
@@ -7,11 +7,11 @@ See the files doc/faq.html or doc/faq.html for frequently asked questions.
Installation
============
-pycrc requires Python 2.2.3 or later.
+pycrc requires Python 2.4 or later. Python 3.x is supported.
This program doesn't need any particular installation. You may execute the
-script from any directory. On UN*X platforms, you might need to make the script
-executable: chmod +x pycrc.py
+script from any directory. On UNIX-oid platforms, you might need to make the
+script executable: chmod +x pycrc.py
Getting help
@@ -32,6 +32,6 @@ Copyright of the generated source code
Prior to v0.6, pycrc was released under the GPL and an additional addition to
the licence was required to permit to use the generated source code in products
-with a OSI unapproved licence. Since version 0.6, pycrc is released under the
-terms of the MIT licence and thus such an additional clause to the licence has
-become obsolete.
+with a OSI unapproved licence. As of version 0.6, pycrc is released under the
+terms of the MIT licence and such an additional clause to the licence is no
+more required.
View
@@ -2,7 +2,7 @@
# pycrc -- parametrisable CRC calculation utility and C source code generator
#
-# Copyright (c) 2006-2008 Thomas Pircher <tehpeh@gmx.net>
+# Copyright (c) 2006-2009 Thomas Pircher <tehpeh@gmx.net>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
@@ -55,7 +55,7 @@ class Crc(object):
# constructor
###############################################################################
- def __init__(self, width, poly, reflect_in, xor_in, reflect_out, xor_out, table_idx_width = None):
+ def __init__(self, width, poly, reflect_in, xor_in, reflect_out, xor_out, direct = True, table_idx_width = None):
"""The Crc constructor.
The parameters are as follows:
@@ -72,6 +72,7 @@ def __init__(self, width, poly, reflect_in, xor_in, reflect_out, xor_out, table_
self.XorIn = xor_in
self.ReflectOut = reflect_out
self.XorOut = xor_out
+ self.Direct = direct
self.TableIdxWidth = table_idx_width
self.MSB_Mask = 0x1 << (self.Width - 1)
@@ -83,31 +84,55 @@ def __init__(self, width, poly, reflect_in, xor_in, reflect_out, xor_out, table_
self.TableIdxWidth = 8
self.TableWidth = 1 << self.TableIdxWidth
- # function reflect
+ if self.Direct:
+ self.DirectInit = self.XorIn
+ self.NonDirectInit = self.__get_nondirect_init(self.XorIn)
+ else:
+ self.DirectInit = self.__get_direct_init(self.XorIn)
+ self.NonDirectInit = self.XorIn
+
+
+ # function __get_direct_init
###############################################################################
- def reflect(self, data, width):
+ def __get_direct_init(self, init):
"""
- reflects a data word, i.e. reverts the bit order
+ return the direct init if the non-direct algorithm has been selected.
"""
- x = 0
- for i in range(width):
- x = x | (((data >> (width - i -1)) & 1) << i)
- return x
+ crc = init
+ for i in range(self.Width):
+ bit = crc & self.MSB_Mask
+ crc <<= 1
+ if bit:
+ crc ^= self.Poly
+ return crc & self.Mask
+
+ # function __get_nondirect_init
+ ###############################################################################
+ def __get_nondirect_init(self, init):
+ """
+ return the non-direct init if the direct algorithm has been selected.
+ """
+ crc = init
+ for i in range(self.Width):
+ bit = crc & 0x01
+ if bit:
+ crc^= self.Poly
+ crc >>= 1
+ if bit:
+ crc |= self.MSB_Mask;
+ return crc & self.Mask
- # function handle_bit
+ # function reflect
###############################################################################
- def __handle_bit(self, register, new_bit):
+ def reflect(self, data, width):
"""
- 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
+ reflect a data word, i.e. reverts the bit order.
"""
- 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
+ x = data & 0x01
+ for i in range(width - 1):
+ data >>= 1
+ x = (x << 1) | (data & 0x01)
+ return x
# function bit_by_bit
###############################################################################
@@ -116,55 +141,51 @@ 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])
+ register = self.NonDirectInit
+ for c in str:
+ octet = ord(c)
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)
+ for i in range(8):
+ topbit = register & self.MSB_Mask
+ register = ((register << 1) & self.Mask) | ((octet >> 7) & 0x01)
+ octet <<= 1
+ if topbit:
+ register ^= self.Poly
+
+ for i in range(self.Width):
+ topbit = register & self.MSB_Mask
+ register = ((register << 1) & self.Mask)
+ if topbit:
+ register ^= self.Poly
if self.ReflectOut:
register = self.reflect(register, self.Width)
- register = register ^ self.XorOut
- return register
+ return register ^ self.XorOut
# 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,
+ This is a slightly modified version of the bit-by-bit algorithm: it does not need to loop over the augmented bits,
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])
+ register = self.DirectInit
+ for c in str:
+ octet = ord(c)
if self.ReflectIn:
octet = self.reflect(octet, 8)
- for j in range(8):
+ for i in range(8):
bit = register & self.MSB_Mask
register <<= 1
- if octet & (0x80 >> j):
+ if octet & (0x80 >> i):
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
+ return register ^ self.XorOut
# function gen_table
###############################################################################
@@ -198,22 +219,18 @@ def table_driven(self, str):
"""
tbl = self.gen_table()
+ register = self.DirectInit
if not self.ReflectIn:
- register = self.XorIn
- for i in range(len(str)):
- octet = ord(str[i])
- tblidx = ((register >> (self.Width - 8)) ^ octet) & 0xff
+ for c in str:
+ tblidx = ((register >> (self.Width - 8)) ^ ord(c)) & 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 = self.reflect(register, self.Width)
+ for c in str:
+ tblidx = (register ^ ord(c)) & 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
-
+ return register ^ self.XorOut
View
@@ -2,7 +2,7 @@
# pycrc -- parametrisable CRC calculation utility and C source code generator
#
-# Copyright (c) 2006-2008 Thomas Pircher <tehpeh@gmx.net>
+# Copyright (c) 2006-2009 Thomas Pircher <tehpeh@gmx.net>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
Oops, something went wrong.

0 comments on commit 5796fa1

Please sign in to comment.