crcmod.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. #-----------------------------------------------------------------------------
  2. # Copyright (c) 2010 Raymond L. Buvel
  3. # Copyright (c) 2010 Craig McQueen
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. #-----------------------------------------------------------------------------
  23. '''crcmod is a Python module for gererating objects that compute the Cyclic
  24. Redundancy Check. Any 8, 16, 24, 32, or 64 bit polynomial can be used.
  25. The following are the public components of this module.
  26. Crc -- a class that creates instances providing the same interface as the
  27. algorithms in the hashlib module in the Python standard library. These
  28. instances also provide a method for generating a C/C++ function to compute
  29. the CRC.
  30. mkCrcFun -- create a Python function to compute the CRC using the specified
  31. polynomial and initial value. This provides a much simpler interface if
  32. all you need is a function for CRC calculation.
  33. '''
  34. __all__ = '''mkCrcFun Crc
  35. '''.split()
  36. # Select the appropriate set of low-level CRC functions for this installation.
  37. # If the extension module was not built, drop back to the Python implementation
  38. # even though it is significantly slower.
  39. try:
  40. import crcmod._crcfunext as _crcfun
  41. _usingExtension = True
  42. except ImportError:
  43. import crcmod._crcfunpy as _crcfun
  44. _usingExtension = False
  45. import sys, struct
  46. #-----------------------------------------------------------------------------
  47. class Crc:
  48. '''Compute a Cyclic Redundancy Check (CRC) using the specified polynomial.
  49. Instances of this class have the same interface as the algorithms in the
  50. hashlib module in the Python standard library. See the documentation of
  51. this module for examples of how to use a Crc instance.
  52. The string representation of a Crc instance identifies the polynomial,
  53. initial value, XOR out value, and the current CRC value. The print
  54. statement can be used to output this information.
  55. If you need to generate a C/C++ function for use in another application,
  56. use the generateCode method. If you need to generate code for another
  57. language, subclass Crc and override the generateCode method.
  58. The following are the parameters supplied to the constructor.
  59. poly -- The generator polynomial to use in calculating the CRC. The value
  60. is specified as a Python integer. The bits in this integer are the
  61. coefficients of the polynomial. The only polynomials allowed are those
  62. that generate 8, 16, 24, 32, or 64 bit CRCs.
  63. initCrc -- Initial value used to start the CRC calculation. This initial
  64. value should be the initial shift register value XORed with the final XOR
  65. value. That is equivalent to the CRC result the algorithm should return for
  66. a zero-length string. Defaults to all bits set because that starting value
  67. will take leading zero bytes into account. Starting with zero will ignore
  68. all leading zero bytes.
  69. rev -- A flag that selects a bit reversed algorithm when True. Defaults to
  70. True because the bit reversed algorithms are more efficient.
  71. xorOut -- Final value to XOR with the calculated CRC value. Used by some
  72. CRC algorithms. Defaults to zero.
  73. '''
  74. def __init__(self, poly, initCrc=~0, rev=True, xorOut=0, initialize=True):
  75. if not initialize:
  76. # Don't want to perform the initialization when using new or copy
  77. # to create a new instance.
  78. return
  79. (sizeBits, initCrc, xorOut) = _verifyParams(poly, initCrc, xorOut)
  80. self.digest_size = sizeBits//8
  81. self.initCrc = initCrc
  82. self.xorOut = xorOut
  83. self.poly = poly
  84. self.reverse = rev
  85. (crcfun, table) = _mkCrcFun(poly, sizeBits, initCrc, rev, xorOut)
  86. self._crc = crcfun
  87. self.table = table
  88. self.crcValue = self.initCrc
  89. def __str__(self):
  90. lst = []
  91. lst.append('poly = 0x%X' % self.poly)
  92. lst.append('reverse = %s' % self.reverse)
  93. fmt = '0x%%0%dX' % (self.digest_size*2)
  94. lst.append('initCrc = %s' % (fmt % self.initCrc))
  95. lst.append('xorOut = %s' % (fmt % self.xorOut))
  96. lst.append('crcValue = %s' % (fmt % self.crcValue))
  97. return '\n'.join(lst)
  98. def new(self, arg=None):
  99. '''Create a new instance of the Crc class initialized to the same
  100. values as the original instance. The current CRC is set to the initial
  101. value. If a string is provided in the optional arg parameter, it is
  102. passed to the update method.
  103. '''
  104. n = Crc(poly=None, initialize=False)
  105. n._crc = self._crc
  106. n.digest_size = self.digest_size
  107. n.initCrc = self.initCrc
  108. n.xorOut = self.xorOut
  109. n.table = self.table
  110. n.crcValue = self.initCrc
  111. n.reverse = self.reverse
  112. n.poly = self.poly
  113. if arg is not None:
  114. n.update(arg)
  115. return n
  116. def copy(self):
  117. '''Create a new instance of the Crc class initialized to the same
  118. values as the original instance. The current CRC is set to the current
  119. value. This allows multiple CRC calculations using a common initial
  120. string.
  121. '''
  122. c = self.new()
  123. c.crcValue = self.crcValue
  124. return c
  125. def update(self, data):
  126. '''Update the current CRC value using the string specified as the data
  127. parameter.
  128. '''
  129. self.crcValue = self._crc(data, self.crcValue)
  130. def digest(self):
  131. '''Return the current CRC value as a string of bytes. The length of
  132. this string is specified in the digest_size attribute.
  133. '''
  134. n = self.digest_size
  135. crc = self.crcValue
  136. lst = []
  137. while n > 0:
  138. lst.append(crc & 0xFF)
  139. crc = crc >> 8
  140. n -= 1
  141. lst.reverse()
  142. return bytes(lst)
  143. def hexdigest(self):
  144. '''Return the current CRC value as a string of hex digits. The length
  145. of this string is twice the digest_size attribute.
  146. '''
  147. n = self.digest_size
  148. crc = self.crcValue
  149. lst = []
  150. while n > 0:
  151. lst.append('%02X' % (crc & 0xFF))
  152. crc = crc >> 8
  153. n -= 1
  154. lst.reverse()
  155. return ''.join(lst)
  156. def generateCode(self, functionName, out, dataType=None, crcType=None):
  157. '''Generate a C/C++ function.
  158. functionName -- String specifying the name of the function.
  159. out -- An open file-like object with a write method. This specifies
  160. where the generated code is written.
  161. dataType -- An optional parameter specifying the data type of the input
  162. data to the function. Defaults to UINT8.
  163. crcType -- An optional parameter specifying the data type of the CRC
  164. value. Defaults to one of UINT8, UINT16, UINT32, or UINT64 depending
  165. on the size of the CRC value.
  166. '''
  167. if dataType is None:
  168. dataType = 'UINT8'
  169. if crcType is None:
  170. size = 8*self.digest_size
  171. if size == 24:
  172. size = 32
  173. crcType = 'UINT%d' % size
  174. if self.digest_size == 1:
  175. # Both 8-bit CRC algorithms are the same
  176. crcAlgor = 'table[*data ^ (%s)crc]'
  177. elif self.reverse:
  178. # The bit reverse algorithms are all the same except for the data
  179. # type of the crc variable which is specified elsewhere.
  180. crcAlgor = 'table[*data ^ (%s)crc] ^ (crc >> 8)'
  181. else:
  182. # The forward CRC algorithms larger than 8 bits have an extra shift
  183. # operation to get the high byte.
  184. shift = 8*(self.digest_size - 1)
  185. crcAlgor = 'table[*data ^ (%%s)(crc >> %d)] ^ (crc << 8)' % shift
  186. fmt = '0x%%0%dX' % (2*self.digest_size)
  187. if self.digest_size <= 4:
  188. fmt = fmt + 'U,'
  189. else:
  190. # Need the long long type identifier to keep gcc from complaining.
  191. fmt = fmt + 'ULL,'
  192. # Select the number of entries per row in the output code.
  193. n = {1:8, 2:8, 3:4, 4:4, 8:2}[self.digest_size]
  194. lst = []
  195. for i, val in enumerate(self.table):
  196. if (i % n) == 0:
  197. lst.append('\n ')
  198. lst.append(fmt % val)
  199. poly = 'polynomial: 0x%X' % self.poly
  200. if self.reverse:
  201. poly = poly + ', bit reverse algorithm'
  202. if self.xorOut:
  203. # Need to remove the comma from the format.
  204. preCondition = '\n crc = crc ^ %s;' % (fmt[:-1] % self.xorOut)
  205. postCondition = preCondition
  206. else:
  207. preCondition = ''
  208. postCondition = ''
  209. if self.digest_size == 3:
  210. # The 24-bit CRC needs to be conditioned so that only 24-bits are
  211. # used from the 32-bit variable.
  212. if self.reverse:
  213. preCondition += '\n crc = crc & 0xFFFFFFU;'
  214. else:
  215. postCondition += '\n crc = crc & 0xFFFFFFU;'
  216. parms = {
  217. 'dataType' : dataType,
  218. 'crcType' : crcType,
  219. 'name' : functionName,
  220. 'crcAlgor' : crcAlgor % dataType,
  221. 'crcTable' : ''.join(lst),
  222. 'poly' : poly,
  223. 'preCondition' : preCondition,
  224. 'postCondition' : postCondition,
  225. }
  226. out.write(_codeTemplate % parms)
  227. #-----------------------------------------------------------------------------
  228. def mkCrcFun(poly, initCrc=~0, rev=True, xorOut=0):
  229. '''Return a function that computes the CRC using the specified polynomial.
  230. poly -- integer representation of the generator polynomial
  231. initCrc -- default initial CRC value
  232. rev -- when true, indicates that the data is processed bit reversed.
  233. xorOut -- the final XOR value
  234. The returned function has the following user interface
  235. def crcfun(data, crc=initCrc):
  236. '''
  237. # First we must verify the params
  238. (sizeBits, initCrc, xorOut) = _verifyParams(poly, initCrc, xorOut)
  239. # Make the function (and table), return the function
  240. return _mkCrcFun(poly, sizeBits, initCrc, rev, xorOut)[0]
  241. #-----------------------------------------------------------------------------
  242. # Naming convention:
  243. # All function names ending with r are bit reverse variants of the ones
  244. # without the r.
  245. #-----------------------------------------------------------------------------
  246. # Check the polynomial to make sure that it is acceptable and return the number
  247. # of bits in the CRC.
  248. def _verifyPoly(poly):
  249. msg = 'The degree of the polynomial must be 8, 16, 24, 32 or 64'
  250. for n in (8,16,24,32,64):
  251. low = 1<<n
  252. high = low*2
  253. if low <= poly < high:
  254. return n
  255. raise ValueError(msg)
  256. #-----------------------------------------------------------------------------
  257. # Bit reverse the input value.
  258. def _bitrev(x, n):
  259. y = 0
  260. for i in range(n):
  261. y = (y << 1) | (x & 1)
  262. x = x >> 1
  263. return y
  264. #-----------------------------------------------------------------------------
  265. # The following functions compute the CRC for a single byte. These are used
  266. # to build up the tables needed in the CRC algorithm. Assumes the high order
  267. # bit of the polynomial has been stripped off.
  268. def _bytecrc(crc, poly, n):
  269. mask = 1<<(n-1)
  270. for i in range(8):
  271. if crc & mask:
  272. crc = (crc << 1) ^ poly
  273. else:
  274. crc = crc << 1
  275. mask = (1<<n) - 1
  276. crc = crc & mask
  277. return crc
  278. def _bytecrc_r(crc, poly, n):
  279. for i in range(8):
  280. if crc & 1:
  281. crc = (crc >> 1) ^ poly
  282. else:
  283. crc = crc >> 1
  284. mask = (1<<n) - 1
  285. crc = crc & mask
  286. return crc
  287. #-----------------------------------------------------------------------------
  288. # The following functions compute the table needed to compute the CRC. The
  289. # table is returned as a list. Note that the array module does not support
  290. # 64-bit integers on a 32-bit architecture as of Python 2.3.
  291. #
  292. # These routines assume that the polynomial and the number of bits in the CRC
  293. # have been checked for validity by the caller.
  294. def _mkTable(poly, n):
  295. mask = (1<<n) - 1
  296. poly = poly & mask
  297. table = [_bytecrc(i<<(n-8),poly,n) for i in range(256)]
  298. return table
  299. def _mkTable_r(poly, n):
  300. mask = (1<<n) - 1
  301. poly = _bitrev(poly & mask, n)
  302. table = [_bytecrc_r(i,poly,n) for i in range(256)]
  303. return table
  304. #-----------------------------------------------------------------------------
  305. # Map the CRC size onto the functions that handle these sizes.
  306. _sizeMap = {
  307. 8 : [_crcfun._crc8, _crcfun._crc8r],
  308. 16 : [_crcfun._crc16, _crcfun._crc16r],
  309. 24 : [_crcfun._crc24, _crcfun._crc24r],
  310. 32 : [_crcfun._crc32, _crcfun._crc32r],
  311. 64 : [_crcfun._crc64, _crcfun._crc64r],
  312. }
  313. #-----------------------------------------------------------------------------
  314. # Build a mapping of size to struct module type code. This table is
  315. # constructed dynamically so that it has the best chance of picking the best
  316. # code to use for the platform we are running on. This should properly adapt
  317. # to 32 and 64 bit machines.
  318. _sizeToTypeCode = {}
  319. for typeCode in 'B H I L Q'.split():
  320. size = {1:8, 2:16, 4:32, 8:64}.get(struct.calcsize(typeCode),None)
  321. if size is not None and size not in _sizeToTypeCode:
  322. _sizeToTypeCode[size] = '256%s' % typeCode
  323. _sizeToTypeCode[24] = _sizeToTypeCode[32]
  324. del typeCode, size
  325. #-----------------------------------------------------------------------------
  326. # The following function validates the parameters of the CRC, namely,
  327. # poly, and initial/final XOR values.
  328. # It returns the size of the CRC (in bits), and "sanitized" initial/final XOR values.
  329. def _verifyParams(poly, initCrc, xorOut):
  330. sizeBits = _verifyPoly(poly)
  331. mask = (1<<sizeBits) - 1
  332. # Adjust the initial CRC to the correct data type (unsigned value).
  333. initCrc = initCrc & mask
  334. # Similar for XOR-out value.
  335. xorOut = xorOut & mask
  336. return (sizeBits, initCrc, xorOut)
  337. #-----------------------------------------------------------------------------
  338. # The following function returns a Python function to compute the CRC.
  339. #
  340. # It must be passed parameters that are already verified & sanitized by
  341. # _verifyParams().
  342. #
  343. # The returned function calls a low level function that is written in C if the
  344. # extension module could be loaded. Otherwise, a Python implementation is
  345. # used.
  346. #
  347. # In addition to this function, a list containing the CRC table is returned.
  348. def _mkCrcFun(poly, sizeBits, initCrc, rev, xorOut):
  349. if rev:
  350. tableList = _mkTable_r(poly, sizeBits)
  351. _fun = _sizeMap[sizeBits][1]
  352. else:
  353. tableList = _mkTable(poly, sizeBits)
  354. _fun = _sizeMap[sizeBits][0]
  355. _table = tableList
  356. if _usingExtension:
  357. _table = struct.pack(_sizeToTypeCode[sizeBits], *tableList)
  358. if xorOut == 0:
  359. def crcfun(data, crc=initCrc, table=_table, fun=_fun):
  360. return fun(data, crc, table)
  361. else:
  362. def crcfun(data, crc=initCrc, table=_table, fun=_fun):
  363. return xorOut ^ fun(data, xorOut ^ crc, table)
  364. return crcfun, tableList
  365. #-----------------------------------------------------------------------------
  366. _codeTemplate = '''// Automatically generated CRC function
  367. // %(poly)s
  368. %(crcType)s
  369. %(name)s(%(dataType)s *data, int len, %(crcType)s crc)
  370. {
  371. static const %(crcType)s table[256] = {%(crcTable)s
  372. };
  373. %(preCondition)s
  374. while (len > 0)
  375. {
  376. crc = %(crcAlgor)s;
  377. data++;
  378. len--;
  379. }%(postCondition)s
  380. return crc;
  381. }
  382. '''