crcmod.py 17 KB

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