feat: Add new gcloud commands, API clients, and third-party libraries across various services.

This commit is contained in:
2026-01-01 20:26:35 +01:00
parent 5e23cbece0
commit a19e592eb7
25221 changed files with 8324611 additions and 0 deletions

View File

@@ -0,0 +1,14 @@
[patterns]
MANIFEST.in = native
README = native
LICENSE = native
changelog = native
**.c = native
**.h = native
**.py = native
**.txt = native
**.rst = native
**.bat = CRLF
Makefile = LF

View File

@@ -0,0 +1,5 @@
^dist/.*
^build/.*
^docs/_build/.*
^MANIFEST$
\.pyc$

View File

@@ -0,0 +1,4 @@
4fa23153c09ac27f67f1830a35f838abc968a7d8 version_1.7
f7d2dd6fe898fedd97d924baec8676ef324db2c1 version_1.6.1
93c51ec901a9acddda127d18e8256a11fb23d02a version_1.6
23dfa5ee23de9c5a0bd6f815851b6aa2b1572f09 version_1.5

View File

@@ -0,0 +1,22 @@
----------------------------------------------------------------------------
Copyright (c) 2010 Raymond L. Buvel
Copyright (c) 2010 Craig McQueen
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
----------------------------------------------------------------------------

View File

@@ -0,0 +1,9 @@
include README
include LICENSE
include MANIFEST.in
include changelog
include setup.py
recursive-include python2 *
recursive-include python3 *
recursive-include test *
recursive-include docs *

View File

@@ -0,0 +1,124 @@
===========================
crcmod for Calculating CRCs
===========================
The software in this package is a Python module for generating objects that
compute the Cyclic Redundancy Check (CRC). There is no attempt in this package
to explain how the CRC works. There are a number of resources on the web that
give a good explanation of the algorithms. Just do a Google search for "crc
calculation" and browse till you find what you need. Another resource can be
found in chapter 20 of the book "Numerical Recipes in C" by Press et. al.
This package allows the use of any 8, 16, 24, 32, or 64 bit CRC. You can
generate a Python function for the selected polynomial or an instance of the
Crc class which provides the same interface as the ``md5`` and ``sha`` modules
from the Python standard library. A ``Crc`` class instance can also generate
C/C++ source code that can be used in another application.
----------
Guidelines
----------
Documentation is available from the doc strings. It is up to you to decide
what polynomials to use in your application. If someone has not specified the
polynomials to use, you will need to do some research to find one suitable for
your application. Examples are available in the unit test script ``test.py``.
You may also use the ``predefined`` module to select one of the standard
polynomials.
If you need to generate code for another language, I suggest you subclass the
``Crc`` class and replace the method ``generateCode``. Use ``generateCode`` as
a model for the new version.
------------
Dependencies
------------
Python Version
^^^^^^^^^^^^^^
The package has separate code to support the 2.x and 3.x Python series.
For the 2.x versions of Python, these versions have been tested:
* 2.4
* 2.5
* 2.6
* 2.7
It may still work on earlier versions of Python 2.x, but these have not been
recently tested.
For the 3.x versions of Python, these versions have been tested:
* 3.1
Building C extension
^^^^^^^^^^^^^^^^^^^^
To build the C extension, the appropriate compiler tools for your platform must
be installed. Refer to the Python documentation for building C extensions for
details.
------------
Installation
------------
The crcmod package is installed using ``distutils``.
Run the following command::
python setup.py install
If the extension module builds, it will be installed. Otherwise, the
installation will include the pure Python version. This will run significantly
slower than the extension module but will allow the package to be used.
For Windows users who want to use the mingw32 compiler, run this command::
python setup.py build --compiler=mingw32 install
For Python 3.x, the install process is the same but you need to use the 3.x
interpreter.
------------
Unit Testing
------------
The ``crcmod`` package has a module ``crcmod.test``, which contains unit
tests for both ``crcmod`` and ``crcmod.predefined``.
When you first install ``crcmod``, you should run the unit tests to make sure
everything is installed properly. The test script performs a number of tests
including a comparison to the direct method which uses a class implementing
polynomials over the integers mod 2.
To run the unit tests on Python >=2.5::
python -m crcmod.test
Alternatively, in the ``test`` directory run::
python test_crcmod.py
---------------
Code Generation
---------------
The crcmod package is capable of generating C functions that can be compiled
with a C or C++ compiler. In the test directory, there is an examples.py
script that demonstrates how to use the code generator. The result of this is
written out to the file ``examples.c``. The generated code was checked to make
sure it compiles with the GCC compiler.
-------
License
-------
The ``crcmod`` package is released under the MIT license. See the ``LICENSE``
file for details.
------------
Contributors
------------
Craig McQueen

View File

@@ -0,0 +1,38 @@
1.7 Enhancement Release - Jun 27, 2010
* Improve the installation process.
* For Python3 don't allow unicode and require an object that supports the
buffer interface.
* Added Sphinx documentation.
* Added LICENSE file.
1.6.1 Enhancement Release - Mar 7, 2010
Enhancements made by Craig McQueen
* Code cleanup
* Updated database of pre-defined CRC algorithms
1.6 Enhancement Release - Jan 24, 2010
Enhancements made by Craig McQueen
* Added XOR out feature to allow creation of standard CRC algorithms
* Added a database of pre-defined CRC algorithms
1.5 Enhancement Release - Mar 7, 2009
* Added Python 3.x version.
* Upgraded some unit tests.
1.4 Enhancement Release - Jul 28, 2007
* Add 24-bit CRCs as one of the options.
1.3 Enhancement Release - Apr 22, 2006
* Make compatible with Python 2.5 on 64-bit platforms.
* Improve the install procedure.
1.2 Initial Public Release - Jul 10, 2004

View File

@@ -0,0 +1,118 @@
# Makefile for Sphinx documentation
#
# You can set these variables from the command line.
SPHINXOPTS =
SPHINXBUILD = sphinx-build
PAPER =
BUILDDIR = _build
# Internal variables.
PAPEROPT_a4 = -D latex_paper_size=a4
PAPEROPT_letter = -D latex_paper_size=letter
ALLSPHINXOPTS = -d $(BUILDDIR)/doctrees $(PAPEROPT_$(PAPER)) $(SPHINXOPTS) .
.PHONY: help clean html dirhtml singlehtml pickle json htmlhelp qthelp epub latex changes linkcheck doctest
help:
@echo "Please use \`make <target>' where <target> is one of"
@echo " html to make standalone HTML files"
@echo " dirhtml to make HTML files named index.html in directories"
@echo " singlehtml to make a single large HTML file"
@echo " pickle to make pickle files"
@echo " json to make JSON files"
@echo " htmlhelp to make HTML files and a HTML help project"
@echo " qthelp to make HTML files and a qthelp project"
@echo " devhelp to make HTML files and a Devhelp project"
@echo " epub to make an epub"
@echo " latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter"
@echo " latexpdf to make LaTeX files and run them through pdflatex"
@echo " changes to make an overview of all changed/added/deprecated items"
@echo " linkcheck to check all external links for integrity"
@echo " doctest to run all doctests embedded in the documentation (if enabled)"
clean:
-rm -rf $(BUILDDIR)/*
html:
$(SPHINXBUILD) -b html $(ALLSPHINXOPTS) $(BUILDDIR)/html
@echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/html."
dirhtml:
$(SPHINXBUILD) -b dirhtml $(ALLSPHINXOPTS) $(BUILDDIR)/dirhtml
@echo
@echo "Build finished. The HTML pages are in $(BUILDDIR)/dirhtml."
singlehtml:
$(SPHINXBUILD) -b singlehtml $(ALLSPHINXOPTS) $(BUILDDIR)/singlehtml
@echo
@echo "Build finished. The HTML page is in $(BUILDDIR)/singlehtml."
pickle:
$(SPHINXBUILD) -b pickle $(ALLSPHINXOPTS) $(BUILDDIR)/pickle
@echo
@echo "Build finished; now you can process the pickle files."
json:
$(SPHINXBUILD) -b json $(ALLSPHINXOPTS) $(BUILDDIR)/json
@echo
@echo "Build finished; now you can process the JSON files."
htmlhelp:
$(SPHINXBUILD) -b htmlhelp $(ALLSPHINXOPTS) $(BUILDDIR)/htmlhelp
@echo
@echo "Build finished; now you can run HTML Help Workshop with the" \
".hhp project file in $(BUILDDIR)/htmlhelp."
qthelp:
$(SPHINXBUILD) -b qthelp $(ALLSPHINXOPTS) $(BUILDDIR)/qthelp
@echo
@echo "Build finished; now you can run "qcollectiongenerator" with the" \
".qhcp project file in $(BUILDDIR)/qthelp, like this:"
@echo "# qcollectiongenerator $(BUILDDIR)/qthelp/crcmod.qhcp"
@echo "To view the help file:"
@echo "# assistant -collectionFile $(BUILDDIR)/qthelp/crcmod.qhc"
devhelp:
$(SPHINXBUILD) -b devhelp $(ALLSPHINXOPTS) _build/devhelp
@echo
@echo "Build finished."
@echo "To view the help file:"
@echo "# mkdir -p $$HOME/.local/share/devhelp/crcmod"
@echo "# ln -s _build/devhelp $$HOME/.local/share/devhelp/crcmod"
@echo "# devhelp"
epub:
$(SPHINXBUILD) -b epub $(ALLSPHINXOPTS) $(BUILDDIR)/epub
@echo
@echo "Build finished. The epub file is in $(BUILDDIR)/epub."
latex:
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) $(BUILDDIR)/latex
@echo
@echo "Build finished; the LaTeX files are in $(BUILDDIR)/latex."
@echo "Run \`make all-pdf' or \`make all-ps' in that directory to" \
"run these through (pdf)latex."
latexpdf: latex
$(SPHINXBUILD) -b latex $(ALLSPHINXOPTS) _build/latex
@echo "Running LaTeX files through pdflatex..."
make -C _build/latex all-pdf
@echo "pdflatex finished; the PDF files are in _build/latex."
changes:
$(SPHINXBUILD) -b changes $(ALLSPHINXOPTS) $(BUILDDIR)/changes
@echo
@echo "The overview file is in $(BUILDDIR)/changes."
linkcheck:
$(SPHINXBUILD) -b linkcheck $(ALLSPHINXOPTS) $(BUILDDIR)/linkcheck
@echo
@echo "Link check complete; look for any errors in the above output " \
"or in $(BUILDDIR)/linkcheck/output.txt."
doctest:
$(SPHINXBUILD) -b doctest $(ALLSPHINXOPTS) $(BUILDDIR)/doctest
@echo "Testing of doctests in the sources finished, look at the " \
"results in $(BUILDDIR)/doctest/output.txt."

View File

@@ -0,0 +1,237 @@
# -*- coding: utf-8 -*-
#
# crcmod documentation build configuration file, created by
# sphinx-quickstart on Thu Jan 21 14:04:12 2010.
#
# This file is execfile()d with the current directory set to its containing dir.
#
# Note that not all possible configuration values are present in this
# autogenerated file.
#
# All configuration values have a default; values that are commented out
# serve to show the default.
import sys, os
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
#sys.path.append(os.path.abspath('.'))
# -- General configuration -----------------------------------------------------
# If your documentation needs a minimal Sphinx version, state it here.
#needs_sphinx = '1.0'
# Add any Sphinx extension module names here, as strings. They can be extensions
# coming with Sphinx (named 'sphinx.ext.*') or your custom ones.
extensions = [ 'sphinx.ext.intersphinx', ]
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# The suffix of source filenames.
source_suffix = '.rst'
# The encoding of source files.
#source_encoding = 'utf-8-sig'
# The master toctree document.
master_doc = 'index'
# General information about the project.
project = u'crcmod'
copyright = u'2010, Raymond L Buvel'
# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the
# built documents.
#
# The short X.Y version.
version = '1.7'
# The full version, including alpha/beta/rc tags.
release = '1.7'
# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
#language = None
# There are two options for replacing |today|: either, you set today to some
# non-false value, then it is used:
#today = ''
# Else, today_fmt is used as the format for a strftime call.
#today_fmt = '%B %d, %Y'
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
exclude_patterns = ['_build']
# The reST default role (used for this markup: `text`) to use for all documents.
#default_role = None
# If true, '()' will be appended to :func: etc. cross-reference text.
#add_function_parentheses = True
# If true, the current module name will be prepended to all description
# unit titles (such as .. function::).
#add_module_names = True
# If true, sectionauthor and moduleauthor directives will be shown in the
# output. They are ignored by default.
#show_authors = False
# The name of the Pygments (syntax highlighting) style to use.
pygments_style = 'sphinx'
# A list of ignored prefixes for module index sorting.
#modindex_common_prefix = []
# -- Options for HTML output ---------------------------------------------------
# The theme to use for HTML and HTML Help pages. Major themes that come with
# Sphinx are currently 'default' and 'sphinxdoc'.
html_theme = 'default'
# Theme options are theme-specific and customize the look and feel of a theme
# further. For a list of options available for each theme, see the
# documentation.
#html_theme_options = {}
# Add any paths that contain custom themes here, relative to this directory.
#html_theme_path = []
# The name for this set of Sphinx documents. If None, it defaults to
# "<project> v<release> documentation".
#html_title = None
# A shorter title for the navigation bar. Default is the same as html_title.
#html_short_title = None
# The name of an image file (relative to this directory) to place at the top
# of the sidebar.
#html_logo = None
# The name of an image file (within the static path) to use as favicon of the
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
#html_favicon = None
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
# If not '', a 'Last updated on:' timestamp is inserted at every page bottom,
# using the given strftime format.
#html_last_updated_fmt = '%b %d, %Y'
# If true, SmartyPants will be used to convert quotes and dashes to
# typographically correct entities.
#html_use_smartypants = True
# Custom sidebar templates, maps document names to template names.
#html_sidebars = {}
# Additional templates that should be rendered to pages, maps page names to
# template names.
#html_additional_pages = {}
# If false, no module index is generated.
#html_use_modindex = True
# If false, no index is generated.
#html_use_index = True
# If true, the index is split into individual pages for each letter.
#html_split_index = False
# If true, links to the reST sources are added to the pages.
#html_show_sourcelink = True
# If true, "Created using Sphinx" is shown in the HTML footer. Default is True.
#html_show_sphinx = True
# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True.
#html_show_copyright = True
# If true, an OpenSearch description file will be output, and all pages will
# contain a <link> tag referring to it. The value of this option must be the
# base URL from which the finished HTML is served.
#html_use_opensearch = ''
# If nonempty, this is the file name suffix for HTML files (e.g. ".xhtml").
#html_file_suffix = ''
# Output file base name for HTML help builder.
htmlhelp_basename = 'crcmoddoc'
# -- Options for LaTeX output --------------------------------------------------
# The paper size ('letter' or 'a4').
#latex_paper_size = 'letter'
# The font size ('10pt', '11pt' or '12pt').
#latex_font_size = '10pt'
# Grouping the document tree into LaTeX files. List of tuples
# (source start file, target name, title, author, documentclass [howto/manual]).
latex_documents = [
('index', 'crcmod.tex', u'crcmod Documentation',
u'Raymond L Buvel', 'manual'),
]
# The name of an image file (relative to this directory) to place at the top of
# the title page.
#latex_logo = None
# For "manual" documents, if this is true, then toplevel headings are parts,
# not chapters.
#latex_use_parts = False
# Additional stuff for the LaTeX preamble.
#latex_preamble = ''
# Documents to append as an appendix to all manuals.
#latex_appendices = []
# If false, no module index is generated.
#latex_use_modindex = True
# -- Options for Epub output ---------------------------------------------------
# Bibliographic Dublin Core info.
#epub_title = ''
#epub_author = ''
#epub_publisher = ''
#epub_copyright = ''
# The language of the text. It defaults to the language option
# or en if the language is not set.
#epub_language = ''
# The scheme of the identifier. Typical schemes are ISBN or URL.
#epub_scheme = ''
# The unique identifier of the text. This can be a ISBN number
# or the project homepage.
#epub_identifier = ''
# A unique identification for the text.
#epub_uid = ''
# HTML files that should be inserted before the pages created by sphinx.
# The format is a list of tuples containing the path and title.
#epub_pre_files = []
# HTML files shat should be inserted after the pages created by sphinx.
# The format is a list of tuples containing the path and title.
#epub_post_files = []
# A list of files that should not be packed into the epub file.
#epub_exclude_files = []
# -- Options for intersphinx --------------------------------------------------
intersphinx_mapping = {'http://docs.python.org/': None}

View File

@@ -0,0 +1,164 @@
:mod:`crcmod.predefined` -- CRC calculation using predefined algorithms
=======================================================================
.. module:: crcmod.predefined
:synopsis: CRC calculation using predefined algorithms
.. moduleauthor:: Craig McQueen
.. sectionauthor:: Craig McQueen
This module provides a function factory :func:`mkPredefinedCrcFun` and a class :class:`PredefinedCrc`
for calculating CRCs of byte strings using common predefined CRC algorithms.
The function factory and the class are very similar to those defined in :mod:`crcmod`,
except that the CRC algorithm is specified by a predefined name, rather than the
individual polynomial, reflection, and initial and final-XOR parameters.
Predefined CRC algorithms
-------------------------
The :mod:`crcmod.predefined` module offers the following predefined algorithms:
================================ ====================== ========== ==================== ==================== ====================
Name Polynomial Reversed? Init-value XOR-out Check
================================ ====================== ========== ==================== ==================== ====================
``crc-8`` 0x107 False 0x00 0x00 0xF4
``crc-8-darc`` 0x139 True 0x00 0x00 0x15
``crc-8-i-code`` 0x11D False 0xFD 0x00 0x7E
``crc-8-itu`` 0x107 False 0x55 0x55 0xA1
``crc-8-maxim`` 0x131 True 0x00 0x00 0xA1
``crc-8-rohc`` 0x107 True 0xFF 0x00 0xD0
``crc-8-wcdma`` 0x19B True 0x00 0x00 0x25
``crc-16`` 0x18005 True 0x0000 0x0000 0xBB3D
``crc-16-buypass`` 0x18005 False 0x0000 0x0000 0xFEE8
``crc-16-dds-110`` 0x18005 False 0x800D 0x0000 0x9ECF
``crc-16-dect`` 0x10589 False 0x0001 0x0001 0x007E
``crc-16-dnp`` 0x13D65 True 0xFFFF 0xFFFF 0xEA82
``crc-16-en-13757`` 0x13D65 False 0xFFFF 0xFFFF 0xC2B7
``crc-16-genibus`` 0x11021 False 0x0000 0xFFFF 0xD64E
``crc-16-maxim`` 0x18005 True 0xFFFF 0xFFFF 0x44C2
``crc-16-mcrf4xx`` 0x11021 True 0xFFFF 0x0000 0x6F91
``crc-16-riello`` 0x11021 True 0x554D 0x0000 0x63D0
``crc-16-t10-dif`` 0x18BB7 False 0x0000 0x0000 0xD0DB
``crc-16-teledisk`` 0x1A097 False 0x0000 0x0000 0x0FB3
``crc-16-usb`` 0x18005 True 0x0000 0xFFFF 0xB4C8
``x-25`` 0x11021 True 0x0000 0xFFFF 0x906E
``xmodem`` 0x11021 False 0x0000 0x0000 0x31C3
``modbus`` 0x18005 True 0xFFFF 0x0000 0x4B37
``kermit`` [#ccitt]_ 0x11021 True 0x0000 0x0000 0x2189
``crc-ccitt-false`` [#ccitt]_ 0x11021 False 0xFFFF 0x0000 0x29B1
``crc-aug-ccitt`` [#ccitt]_ 0x11021 False 0x1D0F 0x0000 0xE5CC
``crc-24`` 0x1864CFB False 0xB704CE 0x000000 0x21CF02
``crc-24-flexray-a`` 0x15D6DCB False 0xFEDCBA 0x000000 0x7979BD
``crc-24-flexray-b`` 0x15D6DCB False 0xABCDEF 0x000000 0x1F23B8
``crc-32`` 0x104C11DB7 True 0x00000000 0xFFFFFFFF 0xCBF43926
``crc-32-bzip2`` 0x104C11DB7 False 0x00000000 0xFFFFFFFF 0xFC891918
``crc-32c`` 0x11EDC6F41 True 0x00000000 0xFFFFFFFF 0xE3069283
``crc-32d`` 0x1A833982B True 0x00000000 0xFFFFFFFF 0x87315576
``crc-32-mpeg`` 0x104C11DB7 False 0xFFFFFFFF 0x00000000 0x0376E6E7
``posix`` 0x104C11DB7 False 0xFFFFFFFF 0xFFFFFFFF 0x765E7680
``crc-32q`` 0x1814141AB False 0x00000000 0x00000000 0x3010BF7F
``jamcrc`` 0x104C11DB7 True 0xFFFFFFFF 0x00000000 0x340BC6D9
``xfer`` 0x1000000AF False 0x00000000 0x00000000 0xBD0BE338
``crc-64`` 0x1000000000000001B True 0x0000000000000000 0x0000000000000000 0x46A5A9388A5BEFFE
``crc-64-we`` 0x142F0E1EBA9EA3693 False 0x0000000000000000 0xFFFFFFFFFFFFFFFF 0x62EC59E3F1A4F00A
``crc-64-jones`` 0x1AD93D23594C935A9 True 0xFFFFFFFFFFFFFFFF 0x0000000000000000 0xCAA717168609F281
================================ ====================== ========== ==================== ==================== ====================
.. rubric:: Notes
.. [#ccitt] Definitions of CCITT are disputable. See:
* http://homepages.tesco.net/~rainstorm/crc-catalogue.htm
* http://web.archive.org/web/20071229021252/http://www.joegeluso.com/software/articles/ccitt.htm
:func:`mkPredefinedCrcFun` -- CRC function factory
--------------------------------------------------
The function factory provides a simple interface for CRC calculation. It is similar
to :func:`crcmod.mkCrcFun`, except that it specifies a CRC algorithm by name rather
than its parameters.
.. function:: mkPredefinedCrcFun(crc_name)
Function factory that returns a new function for calculating CRCs
using a specified CRC algorithm.
:param crc_name: The name of the predefined CRC algorithm to use.
:type crc_name: string
:return: CRC calculation function
:rtype: function
The function that is returned is the same as that returned by :func:`crcmod.mkCrcFun`:
.. function:: .crc_function(data[, crc=initCrc])
:param data: Data for which to calculate the CRC.
:type data: byte string
:param crc: Initial CRC value.
:return: Calculated CRC value.
:rtype: integer
.. function:: mkCrcFun(crc_name)
This is an alias for :func:`crcmod.predefined.mkPredefinedCrcFun`. However, it is not defined when
:mod:`crcmod.predefined` is imported using the form::
>>> from crcmod.predefined import *
Examples
^^^^^^^^
**CRC-32** example::
>>> import crcmod.predefined
>>> crc32_func = crcmod.predefined.mkCrcFun('crc-32')
>>> hex(crc32_func('123456789'))
'0xcbf43926L'
**XMODEM** example::
>>> xmodem_crc_func = crcmod.predefined.mkCrcFun('xmodem')
>>> hex(xmodem_crc_func('123456789'))
'0x31c3'
Class :class:`PredefinedCrc`
----------------------------
This class is inherited from the :class:`crcmod.Crc` class, and is the same except for the
initialization. It specifies a CRC algorithm by name rather than its parameters.
.. class:: PredefinedCrc(crc_name)
Returns a new :class:`Crc` object for calculating CRCs using a specified CRC algorithm.
The parameter is the same as that for the factory function :func:`crcmod.predefined.mkPredefinedCrcFun`.
:param crc_name: The name of the predefined CRC algorithm to use.
:type crc_name: string
.. class:: Crc(poly[, initCrc, rev, xorOut])
This is an alias for :class:`crcmod.predefined.PredefinedCrc`. However, it is not defined when
:mod:`crcmod.predefined` is imported using the form::
>>> from crcmod.predefined import *
Examples
^^^^^^^^
**CRC-32** Example::
>>> import crcmod.predefined
>>> crc32 = crcmod.predefined.Crc('crc-32')
>>> crc32.update('123456789')
>>> hex(crc32.crcValue)
'0xcbf43926L'
>>> crc32.hexdigest()
'CBF43926'

View File

@@ -0,0 +1,219 @@
:mod:`crcmod` -- CRC calculation
================================
.. module:: crcmod
:synopsis: CRC calculation
.. moduleauthor:: Raymond L Buvel
.. sectionauthor:: Craig McQueen
This module provides a function factory :func:`mkCrcFun` and a class :class:`Crc`
for calculating CRCs of byte strings using common CRC algorithms.
.. note:: This documentation normally shows Python 2.x usage. Python 3.x usage is very similar,
with the main difference that input strings must be explicitly defined as
:keyword:`bytes` type, or an object that supports the buffer protocol. E.g.::
>>> crc_value = crc_function(b'123456789')
>>> crc_value = crc_function(bytearray((49, 50, 51, 52, 53, 54, 55, 56, 57)))
:func:`mkCrcFun` -- CRC function factory
----------------------------------------
The function factory provides a simple interface for CRC calculation.
.. function:: mkCrcFun(poly[, initCrc, rev, xorOut])
Function factory that returns a new function for calculating CRCs
using a specified CRC algorithm.
:param poly: The generator polynomial to use in calculating the CRC. The value
is specified as a Python integer or long integer. The bits in this integer
are the coefficients of the polynomial. The only polynomials allowed are
those that generate 8, 16, 24, 32, or 64 bit CRCs.
:param initCrc: Initial value used to start the CRC calculation. This initial
value should be the initial shift register value, reversed if it uses a
reversed algorithm, and then XORed with the final XOR value. That is
equivalent to the CRC result the algorithm should return for a
zero-length string. Defaults to all bits set because that starting value
will take leading zero bytes into account. Starting with zero will ignore
all leading zero bytes.
:param rev: A flag that selects a bit reversed algorithm when :keyword:`True`. Defaults to
:keyword:`True` because the bit reversed algorithms are more efficient.
:param xorOut: Final value to XOR with the calculated CRC value. Used by some
CRC algorithms. Defaults to zero.
:return: CRC calculation function
:rtype: function
The function that is returned is as follows:
.. function:: .crc_function(data[, crc=initCrc])
:param data: Data for which to calculate the CRC.
:type data: byte string
:param crc: Initial CRC value.
:return: Calculated CRC value.
:rtype: integer
Examples
^^^^^^^^
**CRC-32** Example::
>>> import crcmod
>>> crc32_func = crcmod.mkCrcFun(0x104c11db7, initCrc=0, xorOut=0xFFFFFFFF)
>>> hex(crc32_func('123456789'))
'0xcbf43926L'
The CRC-32 uses a "reversed" algorithm, used for many common CRC algorithms.
Less common is the non-reversed algorithm, as used by the 16-bit **XMODEM** CRC::
>>> xmodem_crc_func = crcmod.mkCrcFun(0x11021, rev=False, initCrc=0x0000, xorOut=0x0000)
>>> hex(xmodem_crc_func('123456789'))
'0x31c3'
The CRC function can be called multiple times. On subsequent calls, pass the
CRC value previously calculated as a second parameter::
>>> crc_value = crc32_func('1234')
>>> crc_value = crc32_func('56789', crc_value)
>>> hex(crc_value)
'0xcbf43926L'
Python 3.x example: Unicode strings are not accepted as input. Byte strings are acceptable.
You may calculate a CRC for an object that implements the buffer protocol::
>>> import crcmod
>>> crc32_func = crcmod.mkCrcFun(0x104c11db7, initCrc=0, xorOut=0xFFFFFFFF)
>>> hex(crc32_func('123456789'))
...
TypeError: Unicode-objects must be encoded before calculating a CRC
>>> hex(crc32_func(b'123456789'))
'0xcbf43926'
>>> hex(crc32_func(bytearray((49, 50, 51, 52, 53, 54, 55, 56, 57))))
'0xcbf43926'
Class :class:`Crc`
------------------
The class provides an interface similar to the Python :mod:`hashlib`, :mod:`md5` and :mod:`sha` modules.
.. class:: Crc(poly[, initCrc, rev, xorOut])
Returns a new :class:`Crc` object for calculating CRCs using a specified CRC algorithm.
The parameters are the same as those for the factory function :func:`mkCrcFun`.
:param poly: The generator polynomial to use in calculating the CRC. The value
is specified as a Python integer or long integer. The bits in this integer
are the coefficients of the polynomial. The only polynomials allowed are
those that generate 8, 16, 24, 32, or 64 bit CRCs.
:param initCrc: Initial value used to start the CRC calculation. This initial
value should be the initial shift register value, reversed if it uses a
reversed algorithm, and then XORed with the final XOR value. That is
equivalent to the CRC result the algorithm should return for a
zero-length string. Defaults to all bits set because that starting value
will take leading zero bytes into account. Starting with zero will ignore
all leading zero bytes.
:param rev: A flag that selects a bit reversed algorithm when :keyword:`True`. Defaults to
:keyword:`True` because the bit reversed algorithms are more efficient.
:param xorOut: Final value to XOR with the calculated CRC value. Used by some
CRC algorithms. Defaults to zero.
:class:`Crc` objects contain the following constant values:
.. attribute:: digest_size
The size of the resulting digest in bytes. This depends on the width of the CRC polynomial.
E.g. for a 32-bit CRC, :data:`digest_size` will be ``4``.
.. attribute:: crcValue
The calculated CRC value, as an integer, for the data that has been input
using :meth:`update`. This value is updated after each call to :meth:`update`.
:class:`Crc` objects support the following methods:
.. method:: new([arg])
Create a new instance of the :class:`Crc` class initialized to the same
values as the original instance. The CRC value is set to the initial
value. If a string is provided in the optional ``arg`` parameter, it is
passed to the :meth:`update` method.
.. method:: copy()
Create a new instance of the :class:`Crc` class initialized to the same
values as the original instance. The CRC value is copied from the current
value. This allows multiple CRC calculations using a common initial
string.
.. method:: update(data)
:param data: Data for which to calculate the CRC
:type data: byte string
Update the calculated CRC value for the specified input data.
.. method:: digest()
Return the current CRC value as a string of bytes. The length of
this string is specified in the :attr:`digest_size` attribute.
.. method:: hexdigest()
Return the current CRC value as a string of hex digits. The length
of this string is twice the :attr:`digest_size` attribute.
.. method:: generateCode(functionName, out, [dataType, crcType])
Generate a C/C++ function.
:param functionName: String specifying the name of the function.
:param out: An open file-like object with a write method.
This specifies where the generated code is written.
:param dataType: An optional parameter specifying the data type of the input
data to the function. Defaults to ``UINT8``.
:param crcType: An optional parameter specifying the data type of the CRC value.
Defaults to one of ``UINT8``, ``UINT16``, ``UINT32``, or ``UINT64`` depending
on the size of the CRC value.
Examples
^^^^^^^^
**CRC-32** Example::
>>> import crcmod
>>> crc32 = crcmod.Crc(0x104c11db7, initCrc=0, xorOut=0xFFFFFFFF)
>>> crc32.update('123456789')
>>> hex(crc32.crcValue)
'0xcbf43926L'
>>> crc32.hexdigest()
'CBF43926'
The :meth:`Crc.update` method can be called multiple times, and the CRC value is updated with each call::
>>> crc32new = crc32.new()
>>> crc32new.update('1234')
>>> crc32new.hexdigest()
'9BE3E0A3'
>>> crc32new.update('56789')
>>> crc32new.hexdigest()
'CBF43926'

View File

@@ -0,0 +1,20 @@
.. crcmod documentation master file, created by
sphinx-quickstart on Thu Jan 21 14:04:12 2010.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
crcmod documentation
====================
This is the documentation for the :mod:`crcmod` Python library.
.. toctree::
:maxdepth: 2
intro.rst
crcmod.rst
crcmod.predefined.rst
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

View File

@@ -0,0 +1,149 @@
============
Introduction
============
The software in this package is a Python module for generating objects that
compute the Cyclic Redundancy Check (CRC). It includes a (optional) C
extension for fast calculation, as well as a pure Python implementation.
There is no attempt in this package to explain how the CRC works. There are a
number of resources on the web that give a good explanation of the algorithms.
Just do a Google search for "crc calculation" and browse till you find what you
need. Another resource can be found in chapter 20 of the book "Numerical
Recipes in C" by Press et. al.
This package allows the use of any 8, 16, 24, 32, or 64 bit CRC. You can
generate a Python function for the selected polynomial or an instance of the
:class:`crcmod.Crc` class which provides the same interface as the
:mod:`hashlib`, :mod:`md5` and :mod:`sha` modules from the Python standard
library. A :class:`crcmod.Crc` class instance can also generate C/C++ source
code that can be used in another application.
----------
Guidelines
----------
Documentation is available here as well as from the doc strings.
It is up to you to decide what polynomials to use in your application. Some
common CRC algorithms are predefined in :mod:`crcmod.predefined`. If someone
has not specified the polynomials to use, you will need to do some research to
find one suitable for your application. Examples are available in the unit
test script :file:`test.py`.
If you need to generate code for another language, I suggest you subclass the
:class:`crcmod.Crc` class and replace the method
:meth:`crcmod.Crc.generateCode`. Use :meth:`crcmod.Crc.generateCode` as a
model for the new version.
------------
Dependencies
------------
Python Version
^^^^^^^^^^^^^^
The package has separate code to support the 2.x and 3.x Python series.
For the 2.x versions of Python, these versions have been tested:
* 2.4
* 2.5
* 2.6
* 2.7
It may still work on earlier versions of Python 2.x, but these have not been
recently tested.
For the 3.x versions of Python, these versions have been tested:
* 3.1
Building C extension
^^^^^^^^^^^^^^^^^^^^
To build the C extension, the appropriate compiler tools for your platform must
be installed. Refer to the Python documentation for building C extensions for
details.
------------
Installation
------------
The :mod:`crcmod` package is installed using :mod:`distutils`.
Run the following command::
python setup.py install
If the extension module builds, it will be installed. Otherwise, the
installation will include the pure Python version. This will run significantly
slower than the extension module but will allow the package to be used.
For Windows users who want to use the mingw32 compiler, run this command::
python setup.py build --compiler=mingw32 install
For Python 3.x, the install process is the same but you need to use the 3.x
interpreter.
------------
Unit Testing
------------
The :mod:`crcmod` package has a module :mod:`crcmod.test`, which contains
unit tests for both :mod:`crcmod` and :mod:`crcmod.predefined`.
When you first install :mod:`crcmod`, you should run the unit tests to make
sure everything is installed properly. The test script performs a number of
tests including a comparison to the direct method which uses a class
implementing polynomials over the integers mod 2.
To run the unit tests on Python >=2.5::
python -m crcmod.test
Alternatively, in the :file:`test` directory run::
python test_crcmod.py
---------------
Code Generation
---------------
The :mod:`crcmod` package is capable of generating C functions that can be
compiled with a C or C++ compiler. In the :file:`test` directory, there is an
:file:`examples.py` script that demonstrates how to use the code generator.
The result of this is written out to the file :file:`examples.c`. The
generated code was checked to make sure it compiles with the GCC compiler.
-------
License
-------
The :mod:`crcmod` package is released under the MIT license. See the
:file:`LICENSE` file for details.
----------
References
----------
.. seealso::
:func:`binascii.crc32` function from the :mod:`binascii` module
CRC-32 implementation
:func:`zlib.crc32` function from the :mod:`zlib` module
CRC-32 implementation
Module :mod:`hashlib`
Secure hash and message digest algorithms.
Module :mod:`md5`
RSA's MD5 message digest algorithm.
Module :mod:`sha`
NIST's secure hash algorithm, SHA.
Module :mod:`hmac`
Keyed-hashing for message authentication.

View File

@@ -0,0 +1,137 @@
@ECHO OFF
REM Command file for Sphinx documentation
set SPHINXBUILD=sphinx-build
set BUILDDIR=_build
set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% .
if NOT "%PAPER%" == "" (
set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS%
)
if "%1" == "" goto help
if "%1" == "help" (
:help
echo.Please use `make ^<target^>` where ^<target^> is one of
echo. html to make standalone HTML files
echo. dirhtml to make HTML files named index.html in directories
echo. singlehtml to make a single large HTML file
echo. pickle to make pickle files
echo. json to make JSON files
echo. htmlhelp to make HTML files and a HTML help project
echo. qthelp to make HTML files and a qthelp project
echo. devhelp to make HTML files and a Devhelp project
echo. epub to make an epub
echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter
echo. changes to make an overview over all changed/added/deprecated items
echo. linkcheck to check all external links for integrity
echo. doctest to run all doctests embedded in the documentation if enabled
goto end
)
if "%1" == "clean" (
for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i
del /q /s %BUILDDIR%\*
goto end
)
if "%1" == "html" (
%SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html
echo.
echo.Build finished. The HTML pages are in %BUILDDIR%/html.
goto end
)
if "%1" == "dirhtml" (
%SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml
echo.
echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml.
goto end
)
if "%1" == "singlehtml" (
%SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml
echo.
echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml.
goto end
)
if "%1" == "pickle" (
%SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle
echo.
echo.Build finished; now you can process the pickle files.
goto end
)
if "%1" == "json" (
%SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json
echo.
echo.Build finished; now you can process the JSON files.
goto end
)
if "%1" == "htmlhelp" (
%SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp
echo.
echo.Build finished; now you can run HTML Help Workshop with the ^
.hhp project file in %BUILDDIR%/htmlhelp.
goto end
)
if "%1" == "qthelp" (
%SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp
echo.
echo.Build finished; now you can run "qcollectiongenerator" with the ^
.qhcp project file in %BUILDDIR%/qthelp, like this:
echo.^> qcollectiongenerator %BUILDDIR%\qthelp\crcmod.qhcp
echo.To view the help file:
echo.^> assistant -collectionFile %BUILDDIR%\qthelp\crcmod.ghc
goto end
)
if "%1" == "devhelp" (
%SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% _build/devhelp
echo.
echo.Build finished.
goto end
)
if "%1" == "epub" (
%SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub
echo.
echo.Build finished. The epub file is in %BUILDDIR%/epub.
goto end
)
if "%1" == "latex" (
%SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex
echo.
echo.Build finished; the LaTeX files are in %BUILDDIR%/latex.
goto end
)
if "%1" == "changes" (
%SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes
echo.
echo.The overview file is in %BUILDDIR%/changes.
goto end
)
if "%1" == "linkcheck" (
%SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck
echo.
echo.Link check complete; look for any errors in the above output ^
or in %BUILDDIR%/linkcheck/output.txt.
goto end
)
if "%1" == "doctest" (
%SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest
echo.
echo.Testing of doctests in the sources finished, look at the ^
results in %BUILDDIR%/doctest/output.txt.
goto end
)
:end

View File

@@ -0,0 +1,45 @@
from __future__ import print_function
import numbers
import crcmod.predefined
table_data = [
[ "Name", 'name', 32, ],
[ "Polynomial", 'poly', 22, ],
[ "Reversed?", 'reverse', 10, ],
[ "Init-value", 'init', 20, ],
[ "XOR-out", 'xor_out', 20, ],
[ "Check", 'check', 20, ],
]
ccitt_defns = [
'kermit',
'crc-ccitt-false',
'crc-aug-ccitt',
]
column_dashes = ' '.join(('=' * table_data_item[2] for table_data_item in table_data))
print(column_dashes)
print(' '.join(("%-*s" % (table_data_item[2], table_data_item[0]) for table_data_item in table_data)).strip())
print(column_dashes)
for defn in crcmod.predefined._crc_definitions:
poly_width = crcmod.crcmod._verifyPoly(defn['poly'])
hex_width = (poly_width + 3) // 4
defn_data_list = []
for (header_text, key, width) in table_data:
if isinstance(defn[key], bool):
item = "%s" % (defn[key],)
elif isinstance(defn[key], numbers.Integral):
item = "0x%0*X" % (hex_width, defn[key])
else:
item = "``%s``" % (defn[key])
if defn['name'] in ccitt_defns:
item = ' '.join([item, '[#ccitt]_'])
item = "%-*s" % (width, item)
defn_data_list.append(item)
print(' '.join(defn_data_list).strip())
print(column_dashes)

View File

@@ -0,0 +1,8 @@
try:
from crcmod.crcmod import *
import crcmod.predefined
except ImportError:
# Make this backward compatible
from crcmod import *
import predefined
__doc__ = crcmod.__doc__

View File

@@ -0,0 +1,87 @@
#-----------------------------------------------------------------------------
# Low level CRC functions for use by crcmod. This version is implemented in
# Python for a couple of reasons. 1) Provide a reference implememtation.
# 2) Provide a version that can be used on systems where a C compiler is not
# available for building extension modules.
#
# Copyright (c) 2004 Raymond L. Buvel
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#-----------------------------------------------------------------------------
def _crc8(data, crc, table):
crc = crc & 0xFF
for x in data:
crc = table[ord(x) ^ crc]
return crc
def _crc8r(data, crc, table):
crc = crc & 0xFF
for x in data:
crc = table[ord(x) ^ crc]
return crc
def _crc16(data, crc, table):
crc = crc & 0xFFFF
for x in data:
crc = table[ord(x) ^ ((crc>>8) & 0xFF)] ^ ((crc << 8) & 0xFF00)
return crc
def _crc16r(data, crc, table):
crc = crc & 0xFFFF
for x in data:
crc = table[ord(x) ^ (crc & 0xFF)] ^ (crc >> 8)
return crc
def _crc24(data, crc, table):
crc = crc & 0xFFFFFF
for x in data:
crc = table[ord(x) ^ (int(crc>>16) & 0xFF)] ^ ((crc << 8) & 0xFFFF00)
return crc
def _crc24r(data, crc, table):
crc = crc & 0xFFFFFF
for x in data:
crc = table[ord(x) ^ int(crc & 0xFF)] ^ (crc >> 8)
return crc
def _crc32(data, crc, table):
crc = crc & 0xFFFFFFFFL
for x in data:
crc = table[ord(x) ^ (int(crc>>24) & 0xFF)] ^ ((crc << 8) & 0xFFFFFF00L)
return crc
def _crc32r(data, crc, table):
crc = crc & 0xFFFFFFFFL
for x in data:
crc = table[ord(x) ^ int(crc & 0xFFL)] ^ (crc >> 8)
return crc
def _crc64(data, crc, table):
crc = crc & 0xFFFFFFFFFFFFFFFFL
for x in data:
crc = table[ord(x) ^ (int(crc>>56) & 0xFF)] ^ ((crc << 8) & 0xFFFFFFFFFFFFFF00L)
return crc
def _crc64r(data, crc, table):
crc = crc & 0xFFFFFFFFFFFFFFFFL
for x in data:
crc = table[ord(x) ^ int(crc & 0xFFL)] ^ (crc >> 8)
return crc

View File

@@ -0,0 +1,472 @@
#-----------------------------------------------------------------------------
# Copyright (c) 2010 Raymond L. Buvel
# Copyright (c) 2010 Craig McQueen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#-----------------------------------------------------------------------------
'''crcmod is a Python module for gererating objects that compute the Cyclic
Redundancy Check. Any 8, 16, 24, 32, or 64 bit polynomial can be used.
The following are the public components of this module.
Crc -- a class that creates instances providing the same interface as the
md5 and sha modules in the Python standard library. These instances also
provide a method for generating a C/C++ function to compute the CRC.
mkCrcFun -- create a Python function to compute the CRC using the specified
polynomial and initial value. This provides a much simpler interface if
all you need is a function for CRC calculation.
'''
__all__ = '''mkCrcFun Crc
'''.split()
# Select the appropriate set of low-level CRC functions for this installation.
# If the extension module was not built, drop back to the Python implementation
# even though it is significantly slower.
try:
import _crcfunext as _crcfun
_usingExtension = True
except ImportError:
import _crcfunpy as _crcfun
_usingExtension = False
import sys, struct
#-----------------------------------------------------------------------------
class Crc:
'''Compute a Cyclic Redundancy Check (CRC) using the specified polynomial.
Instances of this class have the same interface as the md5 and sha modules
in the Python standard library. See the documentation for these modules
for examples of how to use a Crc instance.
The string representation of a Crc instance identifies the polynomial,
initial value, XOR out value, and the current CRC value. The print
statement can be used to output this information.
If you need to generate a C/C++ function for use in another application,
use the generateCode method. If you need to generate code for another
language, subclass Crc and override the generateCode method.
The following are the parameters supplied to the constructor.
poly -- The generator polynomial to use in calculating the CRC. The value
is specified as a Python integer or long integer. The bits in this integer
are the coefficients of the polynomial. The only polynomials allowed are
those that generate 8, 16, 24, 32, or 64 bit CRCs.
initCrc -- Initial value used to start the CRC calculation. This initial
value should be the initial shift register value XORed with the final XOR
value. That is equivalent to the CRC result the algorithm should return for
a zero-length string. Defaults to all bits set because that starting value
will take leading zero bytes into account. Starting with zero will ignore
all leading zero bytes.
rev -- A flag that selects a bit reversed algorithm when True. Defaults to
True because the bit reversed algorithms are more efficient.
xorOut -- Final value to XOR with the calculated CRC value. Used by some
CRC algorithms. Defaults to zero.
'''
def __init__(self, poly, initCrc=~0L, rev=True, xorOut=0, initialize=True):
if not initialize:
# Don't want to perform the initialization when using new or copy
# to create a new instance.
return
(sizeBits, initCrc, xorOut) = _verifyParams(poly, initCrc, xorOut)
self.digest_size = sizeBits//8
self.initCrc = initCrc
self.xorOut = xorOut
self.poly = poly
self.reverse = rev
(crcfun, table) = _mkCrcFun(poly, sizeBits, initCrc, rev, xorOut)
self._crc = crcfun
self.table = table
self.crcValue = self.initCrc
def __str__(self):
lst = []
lst.append('poly = 0x%X' % self.poly)
lst.append('reverse = %s' % self.reverse)
fmt = '0x%%0%dX' % (self.digest_size*2)
lst.append('initCrc = %s' % (fmt % self.initCrc))
lst.append('xorOut = %s' % (fmt % self.xorOut))
lst.append('crcValue = %s' % (fmt % self.crcValue))
return '\n'.join(lst)
def new(self, arg=None):
'''Create a new instance of the Crc class initialized to the same
values as the original instance. The current CRC is set to the initial
value. If a string is provided in the optional arg parameter, it is
passed to the update method.
'''
n = Crc(poly=None, initialize=False)
n._crc = self._crc
n.digest_size = self.digest_size
n.initCrc = self.initCrc
n.xorOut = self.xorOut
n.table = self.table
n.crcValue = self.initCrc
n.reverse = self.reverse
n.poly = self.poly
if arg is not None:
n.update(arg)
return n
def copy(self):
'''Create a new instance of the Crc class initialized to the same
values as the original instance. The current CRC is set to the current
value. This allows multiple CRC calculations using a common initial
string.
'''
c = self.new()
c.crcValue = self.crcValue
return c
def update(self, data):
'''Update the current CRC value using the string specified as the data
parameter.
'''
self.crcValue = self._crc(data, self.crcValue)
def digest(self):
'''Return the current CRC value as a string of bytes. The length of
this string is specified in the digest_size attribute.
'''
n = self.digest_size
crc = self.crcValue
lst = []
while n > 0:
lst.append(chr(crc & 0xFF))
crc = crc >> 8
n -= 1
lst.reverse()
return ''.join(lst)
def hexdigest(self):
'''Return the current CRC value as a string of hex digits. The length
of this string is twice the digest_size attribute.
'''
n = self.digest_size
crc = self.crcValue
lst = []
while n > 0:
lst.append('%02X' % (crc & 0xFF))
crc = crc >> 8
n -= 1
lst.reverse()
return ''.join(lst)
def generateCode(self, functionName, out, dataType=None, crcType=None):
'''Generate a C/C++ function.
functionName -- String specifying the name of the function.
out -- An open file-like object with a write method. This specifies
where the generated code is written.
dataType -- An optional parameter specifying the data type of the input
data to the function. Defaults to UINT8.
crcType -- An optional parameter specifying the data type of the CRC
value. Defaults to one of UINT8, UINT16, UINT32, or UINT64 depending
on the size of the CRC value.
'''
if dataType is None:
dataType = 'UINT8'
if crcType is None:
size = 8*self.digest_size
if size == 24:
size = 32
crcType = 'UINT%d' % size
if self.digest_size == 1:
# Both 8-bit CRC algorithms are the same
crcAlgor = 'table[*data ^ (%s)crc]'
elif self.reverse:
# The bit reverse algorithms are all the same except for the data
# type of the crc variable which is specified elsewhere.
crcAlgor = 'table[*data ^ (%s)crc] ^ (crc >> 8)'
else:
# The forward CRC algorithms larger than 8 bits have an extra shift
# operation to get the high byte.
shift = 8*(self.digest_size - 1)
crcAlgor = 'table[*data ^ (%%s)(crc >> %d)] ^ (crc << 8)' % shift
fmt = '0x%%0%dX' % (2*self.digest_size)
if self.digest_size <= 4:
fmt = fmt + 'U,'
else:
# Need the long long type identifier to keep gcc from complaining.
fmt = fmt + 'ULL,'
# Select the number of entries per row in the output code.
n = {1:8, 2:8, 3:4, 4:4, 8:2}[self.digest_size]
lst = []
for i, val in enumerate(self.table):
if (i % n) == 0:
lst.append('\n ')
lst.append(fmt % val)
poly = 'polynomial: 0x%X' % self.poly
if self.reverse:
poly = poly + ', bit reverse algorithm'
if self.xorOut:
# Need to remove the comma from the format.
preCondition = '\n crc = crc ^ %s;' % (fmt[:-1] % self.xorOut)
postCondition = preCondition
else:
preCondition = ''
postCondition = ''
if self.digest_size == 3:
# The 24-bit CRC needs to be conditioned so that only 24-bits are
# used from the 32-bit variable.
if self.reverse:
preCondition += '\n crc = crc & 0xFFFFFFU;'
else:
postCondition += '\n crc = crc & 0xFFFFFFU;'
parms = {
'dataType' : dataType,
'crcType' : crcType,
'name' : functionName,
'crcAlgor' : crcAlgor % dataType,
'crcTable' : ''.join(lst),
'poly' : poly,
'preCondition' : preCondition,
'postCondition' : postCondition,
}
out.write(_codeTemplate % parms)
#-----------------------------------------------------------------------------
def mkCrcFun(poly, initCrc=~0L, rev=True, xorOut=0):
'''Return a function that computes the CRC using the specified polynomial.
poly -- integer representation of the generator polynomial
initCrc -- default initial CRC value
rev -- when true, indicates that the data is processed bit reversed.
xorOut -- the final XOR value
The returned function has the following user interface
def crcfun(data, crc=initCrc):
'''
# First we must verify the params
(sizeBits, initCrc, xorOut) = _verifyParams(poly, initCrc, xorOut)
# Make the function (and table), return the function
return _mkCrcFun(poly, sizeBits, initCrc, rev, xorOut)[0]
#-----------------------------------------------------------------------------
# Naming convention:
# All function names ending with r are bit reverse variants of the ones
# without the r.
#-----------------------------------------------------------------------------
# Check the polynomial to make sure that it is acceptable and return the number
# of bits in the CRC.
def _verifyPoly(poly):
msg = 'The degree of the polynomial must be 8, 16, 24, 32 or 64'
poly = long(poly) # Use a common representation for all operations
for n in (8,16,24,32,64):
low = 1L<<n
high = low*2
if low <= poly < high:
return n
raise ValueError(msg)
#-----------------------------------------------------------------------------
# Bit reverse the input value.
def _bitrev(x, n):
x = long(x)
y = 0L
for i in xrange(n):
y = (y << 1) | (x & 1L)
x = x >> 1
if ((1L<<n)-1) <= sys.maxint:
return int(y)
return y
#-----------------------------------------------------------------------------
# The following functions compute the CRC for a single byte. These are used
# to build up the tables needed in the CRC algorithm. Assumes the high order
# bit of the polynomial has been stripped off.
def _bytecrc(crc, poly, n):
crc = long(crc)
poly = long(poly)
mask = 1L<<(n-1)
for i in xrange(8):
if crc & mask:
crc = (crc << 1) ^ poly
else:
crc = crc << 1
mask = (1L<<n) - 1
crc = crc & mask
if mask <= sys.maxint:
return int(crc)
return crc
def _bytecrc_r(crc, poly, n):
crc = long(crc)
poly = long(poly)
for i in xrange(8):
if crc & 1L:
crc = (crc >> 1) ^ poly
else:
crc = crc >> 1
mask = (1L<<n) - 1
crc = crc & mask
if mask <= sys.maxint:
return int(crc)
return crc
#-----------------------------------------------------------------------------
# The following functions compute the table needed to compute the CRC. The
# table is returned as a list. Note that the array module does not support
# 64-bit integers on a 32-bit architecture as of Python 2.3.
#
# These routines assume that the polynomial and the number of bits in the CRC
# have been checked for validity by the caller.
def _mkTable(poly, n):
mask = (1L<<n) - 1
poly = long(poly) & mask
table = [_bytecrc(long(i)<<(n-8),poly,n) for i in xrange(256)]
return table
def _mkTable_r(poly, n):
mask = (1L<<n) - 1
poly = _bitrev(long(poly) & mask, n)
table = [_bytecrc_r(long(i),poly,n) for i in xrange(256)]
return table
#-----------------------------------------------------------------------------
# Map the CRC size onto the functions that handle these sizes.
_sizeMap = {
8 : [_crcfun._crc8, _crcfun._crc8r],
16 : [_crcfun._crc16, _crcfun._crc16r],
24 : [_crcfun._crc24, _crcfun._crc24r],
32 : [_crcfun._crc32, _crcfun._crc32r],
64 : [_crcfun._crc64, _crcfun._crc64r],
}
#-----------------------------------------------------------------------------
# Build a mapping of size to struct module type code. This table is
# constructed dynamically so that it has the best chance of picking the best
# code to use for the platform we are running on. This should properly adapt
# to 32 and 64 bit machines.
_sizeToTypeCode = {}
for typeCode in 'B H I L Q'.split():
size = {1:8, 2:16, 4:32, 8:64}.get(struct.calcsize(typeCode),None)
if size is not None and size not in _sizeToTypeCode:
_sizeToTypeCode[size] = '256%s' % typeCode
_sizeToTypeCode[24] = _sizeToTypeCode[32]
del typeCode, size
#-----------------------------------------------------------------------------
# The following function validates the parameters of the CRC, namely,
# poly, and initial/final XOR values.
# It returns the size of the CRC (in bits), and "sanitized" initial/final XOR values.
def _verifyParams(poly, initCrc, xorOut):
sizeBits = _verifyPoly(poly)
mask = (1L<<sizeBits) - 1
# Adjust the initial CRC to the correct data type (unsigned value).
initCrc = long(initCrc) & mask
if mask <= sys.maxint:
initCrc = int(initCrc)
# Similar for XOR-out value.
xorOut = long(xorOut) & mask
if mask <= sys.maxint:
xorOut = int(xorOut)
return (sizeBits, initCrc, xorOut)
#-----------------------------------------------------------------------------
# The following function returns a Python function to compute the CRC.
#
# It must be passed parameters that are already verified & sanitized by
# _verifyParams().
#
# The returned function calls a low level function that is written in C if the
# extension module could be loaded. Otherwise, a Python implementation is
# used.
#
# In addition to this function, a list containing the CRC table is returned.
def _mkCrcFun(poly, sizeBits, initCrc, rev, xorOut):
if rev:
tableList = _mkTable_r(poly, sizeBits)
_fun = _sizeMap[sizeBits][1]
else:
tableList = _mkTable(poly, sizeBits)
_fun = _sizeMap[sizeBits][0]
_table = tableList
if _usingExtension:
_table = struct.pack(_sizeToTypeCode[sizeBits], *tableList)
if xorOut == 0:
def crcfun(data, crc=initCrc, table=_table, fun=_fun):
return fun(data, crc, table)
else:
def crcfun(data, crc=initCrc, table=_table, fun=_fun):
return xorOut ^ fun(data, xorOut ^ crc, table)
return crcfun, tableList
#-----------------------------------------------------------------------------
_codeTemplate = '''// Automatically generated CRC function
// %(poly)s
%(crcType)s
%(name)s(%(dataType)s *data, int len, %(crcType)s crc)
{
static const %(crcType)s table[256] = {%(crcTable)s
};
%(preCondition)s
while (len > 0)
{
crc = %(crcAlgor)s;
data++;
len--;
}%(postCondition)s
return crc;
}
'''

View File

@@ -0,0 +1,162 @@
#-----------------------------------------------------------------------------
# Copyright (c) 2010 Craig McQueen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#-----------------------------------------------------------------------------
'''
crcmod.predefined defines some well-known CRC algorithms.
To use it, e.g.:
import crcmod.predefined
crc32func = crcmod.predefined.mkPredefinedCrcFun("crc-32")
crc32class = crcmod.predefined.PredefinedCrc("crc-32")
crcmod.predefined.Crc is an alias for crcmod.predefined.PredefinedCrc
But if doing 'from crc.predefined import *', only PredefinedCrc is imported.
'''
# local imports
import crcmod
__all__ = [
'PredefinedCrc',
'mkPredefinedCrcFun',
]
REVERSE = True
NON_REVERSE = False
# The following table defines the parameters of well-known CRC algorithms.
# The "Check" value is the CRC for the ASCII byte sequence "123456789". It
# can be used for unit tests.
_crc_definitions_table = [
# Name Identifier-name, Poly Reverse Init-value XOR-out Check
[ 'crc-8', 'Crc8', 0x107, NON_REVERSE, 0x00, 0x00, 0xF4, ],
[ 'crc-8-darc', 'Crc8Darc', 0x139, REVERSE, 0x00, 0x00, 0x15, ],
[ 'crc-8-i-code', 'Crc8ICode', 0x11D, NON_REVERSE, 0xFD, 0x00, 0x7E, ],
[ 'crc-8-itu', 'Crc8Itu', 0x107, NON_REVERSE, 0x55, 0x55, 0xA1, ],
[ 'crc-8-maxim', 'Crc8Maxim', 0x131, REVERSE, 0x00, 0x00, 0xA1, ],
[ 'crc-8-rohc', 'Crc8Rohc', 0x107, REVERSE, 0xFF, 0x00, 0xD0, ],
[ 'crc-8-wcdma', 'Crc8Wcdma', 0x19B, REVERSE, 0x00, 0x00, 0x25, ],
[ 'crc-16', 'Crc16', 0x18005, REVERSE, 0x0000, 0x0000, 0xBB3D, ],
[ 'crc-16-buypass', 'Crc16Buypass', 0x18005, NON_REVERSE, 0x0000, 0x0000, 0xFEE8, ],
[ 'crc-16-dds-110', 'Crc16Dds110', 0x18005, NON_REVERSE, 0x800D, 0x0000, 0x9ECF, ],
[ 'crc-16-dect', 'Crc16Dect', 0x10589, NON_REVERSE, 0x0001, 0x0001, 0x007E, ],
[ 'crc-16-dnp', 'Crc16Dnp', 0x13D65, REVERSE, 0xFFFF, 0xFFFF, 0xEA82, ],
[ 'crc-16-en-13757', 'Crc16En13757', 0x13D65, NON_REVERSE, 0xFFFF, 0xFFFF, 0xC2B7, ],
[ 'crc-16-genibus', 'Crc16Genibus', 0x11021, NON_REVERSE, 0x0000, 0xFFFF, 0xD64E, ],
[ 'crc-16-maxim', 'Crc16Maxim', 0x18005, REVERSE, 0xFFFF, 0xFFFF, 0x44C2, ],
[ 'crc-16-mcrf4xx', 'Crc16Mcrf4xx', 0x11021, REVERSE, 0xFFFF, 0x0000, 0x6F91, ],
[ 'crc-16-riello', 'Crc16Riello', 0x11021, REVERSE, 0x554D, 0x0000, 0x63D0, ],
[ 'crc-16-t10-dif', 'Crc16T10Dif', 0x18BB7, NON_REVERSE, 0x0000, 0x0000, 0xD0DB, ],
[ 'crc-16-teledisk', 'Crc16Teledisk', 0x1A097, NON_REVERSE, 0x0000, 0x0000, 0x0FB3, ],
[ 'crc-16-usb', 'Crc16Usb', 0x18005, REVERSE, 0x0000, 0xFFFF, 0xB4C8, ],
[ 'x-25', 'CrcX25', 0x11021, REVERSE, 0x0000, 0xFFFF, 0x906E, ],
[ 'xmodem', 'CrcXmodem', 0x11021, NON_REVERSE, 0x0000, 0x0000, 0x31C3, ],
[ 'modbus', 'CrcModbus', 0x18005, REVERSE, 0xFFFF, 0x0000, 0x4B37, ],
# Note definitions of CCITT are disputable. See:
# http://homepages.tesco.net/~rainstorm/crc-catalogue.htm
# http://web.archive.org/web/20071229021252/http://www.joegeluso.com/software/articles/ccitt.htm
[ 'kermit', 'CrcKermit', 0x11021, REVERSE, 0x0000, 0x0000, 0x2189, ],
[ 'crc-ccitt-false', 'CrcCcittFalse', 0x11021, NON_REVERSE, 0xFFFF, 0x0000, 0x29B1, ],
[ 'crc-aug-ccitt', 'CrcAugCcitt', 0x11021, NON_REVERSE, 0x1D0F, 0x0000, 0xE5CC, ],
[ 'crc-24', 'Crc24', 0x1864CFB, NON_REVERSE, 0xB704CE, 0x000000, 0x21CF02, ],
[ 'crc-24-flexray-a', 'Crc24FlexrayA', 0x15D6DCB, NON_REVERSE, 0xFEDCBA, 0x000000, 0x7979BD, ],
[ 'crc-24-flexray-b', 'Crc24FlexrayB', 0x15D6DCB, NON_REVERSE, 0xABCDEF, 0x000000, 0x1F23B8, ],
[ 'crc-32', 'Crc32', 0x104C11DB7, REVERSE, 0x00000000, 0xFFFFFFFF, 0xCBF43926, ],
[ 'crc-32-bzip2', 'Crc32Bzip2', 0x104C11DB7, NON_REVERSE, 0x00000000, 0xFFFFFFFF, 0xFC891918, ],
[ 'crc-32c', 'Crc32C', 0x11EDC6F41, REVERSE, 0x00000000, 0xFFFFFFFF, 0xE3069283, ],
[ 'crc-32d', 'Crc32D', 0x1A833982B, REVERSE, 0x00000000, 0xFFFFFFFF, 0x87315576, ],
[ 'crc-32-mpeg', 'Crc32Mpeg', 0x104C11DB7, NON_REVERSE, 0xFFFFFFFF, 0x00000000, 0x0376E6E7, ],
[ 'posix', 'CrcPosix', 0x104C11DB7, NON_REVERSE, 0xFFFFFFFF, 0xFFFFFFFF, 0x765E7680, ],
[ 'crc-32q', 'Crc32Q', 0x1814141AB, NON_REVERSE, 0x00000000, 0x00000000, 0x3010BF7F, ],
[ 'jamcrc', 'CrcJamCrc', 0x104C11DB7, REVERSE, 0xFFFFFFFF, 0x00000000, 0x340BC6D9, ],
[ 'xfer', 'CrcXfer', 0x1000000AF, NON_REVERSE, 0x00000000, 0x00000000, 0xBD0BE338, ],
# 64-bit
# Name Identifier-name, Poly Reverse Init-value XOR-out Check
[ 'crc-64', 'Crc64', 0x1000000000000001B, REVERSE, 0x0000000000000000, 0x0000000000000000, 0x46A5A9388A5BEFFE, ],
[ 'crc-64-we', 'Crc64We', 0x142F0E1EBA9EA3693, NON_REVERSE, 0x0000000000000000, 0xFFFFFFFFFFFFFFFF, 0x62EC59E3F1A4F00A, ],
[ 'crc-64-jones', 'Crc64Jones', 0x1AD93D23594C935A9, REVERSE, 0xFFFFFFFFFFFFFFFF, 0x0000000000000000, 0xCAA717168609F281, ],
]
def _simplify_name(name):
"""
Reduce CRC definition name to a simplified form:
* lowercase
* dashes removed
* spaces removed
* any initial "CRC" string removed
"""
name = name.lower()
name = name.replace('-', '')
name = name.replace(' ', '')
if name.startswith('crc'):
name = name[len('crc'):]
return name
_crc_definitions_by_name = {}
_crc_definitions_by_identifier = {}
_crc_definitions = []
_crc_table_headings = [ 'name', 'identifier', 'poly', 'reverse', 'init', 'xor_out', 'check' ]
for table_entry in _crc_definitions_table:
crc_definition = dict(zip(_crc_table_headings, table_entry))
_crc_definitions.append(crc_definition)
name = _simplify_name(table_entry[0])
if name in _crc_definitions_by_name:
raise Exception("Duplicate entry for '%s' in CRC table" % name)
_crc_definitions_by_name[name] = crc_definition
_crc_definitions_by_identifier[table_entry[1]] = crc_definition
def _get_definition_by_name(crc_name):
definition = _crc_definitions_by_name.get(_simplify_name(crc_name), None)
if not definition:
definition = _crc_definitions_by_identifier.get(crc_name, None)
if not definition:
raise KeyError("Unkown CRC name '%s'" % crc_name)
return definition
class PredefinedCrc(crcmod.Crc):
def __init__(self, crc_name):
definition = _get_definition_by_name(crc_name)
crcmod.Crc.__init__(self, poly=definition['poly'], initCrc=definition['init'], rev=definition['reverse'], xorOut=definition['xor_out'])
# crcmod.predefined.Crc is an alias for crcmod.predefined.PredefinedCrc
Crc = PredefinedCrc
def mkPredefinedCrcFun(crc_name):
definition = _get_definition_by_name(crc_name)
return crcmod.mkCrcFun(poly=definition['poly'], initCrc=definition['init'], rev=definition['reverse'], xorOut=definition['xor_out'])
# crcmod.predefined.mkCrcFun is an alias for crcmod.predefined.mkPredefinedCrcFun
mkCrcFun = mkPredefinedCrcFun

View File

@@ -0,0 +1,500 @@
#-----------------------------------------------------------------------------
# Copyright (c) 2010 Raymond L. Buvel
# Copyright (c) 2010 Craig McQueen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#-----------------------------------------------------------------------------
'''Unit tests for crcmod functionality'''
import unittest
import binascii
from crcmod import mkCrcFun, Crc
try:
from crcmod.crcmod import _usingExtension
from crcmod.predefined import PredefinedCrc
from crcmod.predefined import mkPredefinedCrcFun
from crcmod.predefined import _crc_definitions as _predefined_crc_definitions
except ImportError:
from crcmod import _usingExtension
from predefined import PredefinedCrc
from predefined import mkPredefinedCrcFun
from predefined import _crc_definitions as _predefined_crc_definitions
#-----------------------------------------------------------------------------
# This polynomial was chosen because it is the product of two irreducible
# polynomials.
# g8 = (x^7+x+1)*(x+1)
g8 = 0x185
#-----------------------------------------------------------------------------
# The following reproduces all of the entries in the Numerical Recipes table.
# This is the standard CCITT polynomial.
g16 = 0x11021
#-----------------------------------------------------------------------------
g24 = 0x15D6DCB
#-----------------------------------------------------------------------------
# This is the standard AUTODIN-II polynomial which appears to be used in a
# wide variety of standards and applications.
g32 = 0x104C11DB7
#-----------------------------------------------------------------------------
# I was able to locate a couple of 64-bit polynomials on the web. To make it
# easier to input the representation, define a function that builds a
# polynomial from a list of the bits that need to be turned on.
def polyFromBits(bits):
p = 0L
for n in bits:
p = p | (1L << n)
return p
# The following is from the paper "An Improved 64-bit Cyclic Redundancy Check
# for Protein Sequences" by David T. Jones
g64a = polyFromBits([64, 63, 61, 59, 58, 56, 55, 52, 49, 48, 47, 46, 44, 41,
37, 36, 34, 32, 31, 28, 26, 23, 22, 19, 16, 13, 12, 10, 9, 6, 4,
3, 0])
# The following is from Standard ECMA-182 "Data Interchange on 12,7 mm 48-Track
# Magnetic Tape Cartridges -DLT1 Format-", December 1992.
g64b = polyFromBits([64, 62, 57, 55, 54, 53, 52, 47, 46, 45, 40, 39, 38, 37,
35, 33, 32, 31, 29, 27, 24, 23, 22, 21, 19, 17, 13, 12, 10, 9, 7,
4, 1, 0])
#-----------------------------------------------------------------------------
# This class is used to check the CRC calculations against a direct
# implementation using polynomial division.
class poly:
'''Class implementing polynomials over the field of integers mod 2'''
def __init__(self,p):
p = long(p)
if p < 0: raise ValueError('invalid polynomial')
self.p = p
def __long__(self):
return self.p
def __eq__(self,other):
return self.p == other.p
def __ne__(self,other):
return self.p != other.p
# To allow sorting of polynomials, use their long integer form for
# comparison
def __cmp__(self,other):
return cmp(self.p, other.p)
def __nonzero__(self):
return self.p != 0L
def __neg__(self):
return self # These polynomials are their own inverse under addition
def __invert__(self):
n = max(self.deg() + 1, 1)
x = (1L << n) - 1
return poly(self.p ^ x)
def __add__(self,other):
return poly(self.p ^ other.p)
def __sub__(self,other):
return poly(self.p ^ other.p)
def __mul__(self,other):
a = self.p
b = other.p
if a == 0 or b == 0: return poly(0)
x = 0L
while b:
if b&1:
x = x ^ a
a = a<<1
b = b>>1
return poly(x)
def __divmod__(self,other):
u = self.p
m = self.deg()
v = other.p
n = other.deg()
if v == 0: raise ZeroDivisionError('polynomial division by zero')
if n == 0: return (self,poly(0))
if m < n: return (poly(0),self)
k = m-n
a = 1L << m
v = v << k
q = 0L
while k > 0:
if a & u:
u = u ^ v
q = q | 1L
q = q << 1
a = a >> 1
v = v >> 1
k -= 1
if a & u:
u = u ^ v
q = q | 1L
return (poly(q),poly(u))
def __div__(self,other):
return self.__divmod__(other)[0]
def __mod__(self,other):
return self.__divmod__(other)[1]
def __repr__(self):
return 'poly(0x%XL)' % self.p
def __str__(self):
p = self.p
if p == 0: return '0'
lst = { 0:[], 1:['1'], 2:['x'], 3:['1','x'] }[p&3]
p = p>>2
n = 2
while p:
if p&1: lst.append('x^%d' % n)
p = p>>1
n += 1
lst.reverse()
return '+'.join(lst)
def deg(self):
'''return the degree of the polynomial'''
a = self.p
if a == 0: return -1
n = 0
while a >= 0x10000L:
n += 16
a = a >> 16
a = int(a)
while a > 1:
n += 1
a = a >> 1
return n
#-----------------------------------------------------------------------------
# The following functions compute the CRC using direct polynomial division.
# These functions are checked against the result of the table driven
# algorithms.
g8p = poly(g8)
x8p = poly(1L<<8)
def crc8p(d):
d = map(ord, d)
p = 0L
for i in d:
p = p*256L + i
p = poly(p)
return long(p*x8p%g8p)
g16p = poly(g16)
x16p = poly(1L<<16)
def crc16p(d):
d = map(ord, d)
p = 0L
for i in d:
p = p*256L + i
p = poly(p)
return long(p*x16p%g16p)
g24p = poly(g24)
x24p = poly(1L<<24)
def crc24p(d):
d = map(ord, d)
p = 0L
for i in d:
p = p*256L + i
p = poly(p)
return long(p*x24p%g24p)
g32p = poly(g32)
x32p = poly(1L<<32)
def crc32p(d):
d = map(ord, d)
p = 0L
for i in d:
p = p*256L + i
p = poly(p)
return long(p*x32p%g32p)
g64ap = poly(g64a)
x64p = poly(1L<<64)
def crc64ap(d):
d = map(ord, d)
p = 0L
for i in d:
p = p*256L + i
p = poly(p)
return long(p*x64p%g64ap)
g64bp = poly(g64b)
def crc64bp(d):
d = map(ord, d)
p = 0L
for i in d:
p = p*256L + i
p = poly(p)
return long(p*x64p%g64bp)
class KnownAnswerTests(unittest.TestCase):
test_messages = [
'T',
'CatMouse987654321',
]
known_answers = [
[ (g8,0,0), (0xFE, 0x9D) ],
[ (g8,-1,1), (0x4F, 0x9B) ],
[ (g8,0,1), (0xFE, 0x62) ],
[ (g16,0,0), (0x1A71, 0xE556) ],
[ (g16,-1,1), (0x1B26, 0xF56E) ],
[ (g16,0,1), (0x14A1, 0xC28D) ],
[ (g24,0,0), (0xBCC49D, 0xC4B507) ],
[ (g24,-1,1), (0x59BD0E, 0x0AAA37) ],
[ (g24,0,1), (0xD52B0F, 0x1523AB) ],
[ (g32,0,0), (0x6B93DDDB, 0x12DCA0F4) ],
[ (g32,0xFFFFFFFFL,1), (0x41FB859FL, 0xF7B400A7L) ],
[ (g32,0,1), (0x6C0695EDL, 0xC1A40EE5L) ],
[ (g32,0,1,0xFFFFFFFF), (0xBE047A60L, 0x084BFF58L) ],
]
def test_known_answers(self):
for crcfun_params, v in self.known_answers:
crcfun = mkCrcFun(*crcfun_params)
self.assertEqual(crcfun('',0), 0, "Wrong answer for CRC parameters %s, input ''" % (crcfun_params,))
for i, msg in enumerate(self.test_messages):
self.assertEqual(crcfun(msg), v[i], "Wrong answer for CRC parameters %s, input '%s'" % (crcfun_params,msg))
self.assertEqual(crcfun(msg[4:], crcfun(msg[:4])), v[i], "Wrong answer for CRC parameters %s, input '%s'" % (crcfun_params,msg))
self.assertEqual(crcfun(msg[-1:], crcfun(msg[:-1])), v[i], "Wrong answer for CRC parameters %s, input '%s'" % (crcfun_params,msg))
class CompareReferenceCrcTest(unittest.TestCase):
test_messages = [
'',
'T',
'123456789',
'CatMouse987654321',
]
test_poly_crcs = [
[ (g8,0,0), crc8p ],
[ (g16,0,0), crc16p ],
[ (g24,0,0), crc24p ],
[ (g32,0,0), crc32p ],
[ (g64a,0,0), crc64ap ],
[ (g64b,0,0), crc64bp ],
]
@staticmethod
def reference_crc32(d, crc=0):
"""This function modifies the return value of binascii.crc32
to be an unsigned 32-bit value. I.e. in the range 0 to 2**32-1."""
# Work around the future warning on constants.
if crc > 0x7FFFFFFFL:
x = int(crc & 0x7FFFFFFFL)
crc = x | -2147483648
x = binascii.crc32(d,crc)
return long(x) & 0xFFFFFFFFL
def test_compare_crc32(self):
"""The binascii module has a 32-bit CRC function that is used in a wide range
of applications including the checksum used in the ZIP file format.
This test compares the CRC-32 implementation of this crcmod module to
that of binascii.crc32."""
# The following function should produce the same result as
# self.reference_crc32 which is derived from binascii.crc32.
crc32 = mkCrcFun(g32,0,1,0xFFFFFFFF)
for msg in self.test_messages:
self.assertEqual(crc32(msg), self.reference_crc32(msg))
def test_compare_poly(self):
"""Compare various CRCs of this crcmod module to a pure
polynomial-based implementation."""
for crcfun_params, crc_poly_fun in self.test_poly_crcs:
# The following function should produce the same result as
# the associated polynomial CRC function.
crcfun = mkCrcFun(*crcfun_params)
for msg in self.test_messages:
self.assertEqual(crcfun(msg), crc_poly_fun(msg))
class CrcClassTest(unittest.TestCase):
"""Verify the Crc class"""
msg = 'CatMouse987654321'
def test_simple_crc32_class(self):
"""Verify the CRC class when not using xorOut"""
crc = Crc(g32)
str_rep = \
'''poly = 0x104C11DB7
reverse = True
initCrc = 0xFFFFFFFF
xorOut = 0x00000000
crcValue = 0xFFFFFFFF'''
self.assertEqual(str(crc), str_rep)
self.assertEqual(crc.digest(), '\xff\xff\xff\xff')
self.assertEqual(crc.hexdigest(), 'FFFFFFFF')
crc.update(self.msg)
self.assertEqual(crc.crcValue, 0xF7B400A7L)
self.assertEqual(crc.digest(), '\xf7\xb4\x00\xa7')
self.assertEqual(crc.hexdigest(), 'F7B400A7')
# Verify the .copy() method
x = crc.copy()
self.assertTrue(x is not crc)
str_rep = \
'''poly = 0x104C11DB7
reverse = True
initCrc = 0xFFFFFFFF
xorOut = 0x00000000
crcValue = 0xF7B400A7'''
self.assertEqual(str(crc), str_rep)
self.assertEqual(str(x), str_rep)
def test_full_crc32_class(self):
"""Verify the CRC class when using xorOut"""
crc = Crc(g32, initCrc=0, xorOut= ~0L)
str_rep = \
'''poly = 0x104C11DB7
reverse = True
initCrc = 0x00000000
xorOut = 0xFFFFFFFF
crcValue = 0x00000000'''
self.assertEqual(str(crc), str_rep)
self.assertEqual(crc.digest(), '\x00\x00\x00\x00')
self.assertEqual(crc.hexdigest(), '00000000')
crc.update(self.msg)
self.assertEqual(crc.crcValue, 0x84BFF58L)
self.assertEqual(crc.digest(), '\x08\x4b\xff\x58')
self.assertEqual(crc.hexdigest(), '084BFF58')
# Verify the .copy() method
x = crc.copy()
self.assertTrue(x is not crc)
str_rep = \
'''poly = 0x104C11DB7
reverse = True
initCrc = 0x00000000
xorOut = 0xFFFFFFFF
crcValue = 0x084BFF58'''
self.assertEqual(str(crc), str_rep)
self.assertEqual(str(x), str_rep)
# Verify the .new() method
y = crc.new()
self.assertTrue(y is not crc)
self.assertTrue(y is not x)
str_rep = \
'''poly = 0x104C11DB7
reverse = True
initCrc = 0x00000000
xorOut = 0xFFFFFFFF
crcValue = 0x00000000'''
self.assertEqual(str(y), str_rep)
class PredefinedCrcTest(unittest.TestCase):
"""Verify the predefined CRCs"""
test_messages_for_known_answers = [
'', # Test cases below depend on this first entry being the empty string.
'T',
'CatMouse987654321',
]
known_answers = [
[ 'crc-aug-ccitt', (0x1D0F, 0xD6ED, 0x5637) ],
[ 'x-25', (0x0000, 0xE4D9, 0x0A91) ],
[ 'crc-32', (0x00000000, 0xBE047A60, 0x084BFF58) ],
]
def test_known_answers(self):
for crcfun_name, v in self.known_answers:
crcfun = mkPredefinedCrcFun(crcfun_name)
self.assertEqual(crcfun('',0), 0, "Wrong answer for CRC '%s', input ''" % crcfun_name)
for i, msg in enumerate(self.test_messages_for_known_answers):
self.assertEqual(crcfun(msg), v[i], "Wrong answer for CRC %s, input '%s'" % (crcfun_name,msg))
self.assertEqual(crcfun(msg[4:], crcfun(msg[:4])), v[i], "Wrong answer for CRC %s, input '%s'" % (crcfun_name,msg))
self.assertEqual(crcfun(msg[-1:], crcfun(msg[:-1])), v[i], "Wrong answer for CRC %s, input '%s'" % (crcfun_name,msg))
def test_class_with_known_answers(self):
for crcfun_name, v in self.known_answers:
for i, msg in enumerate(self.test_messages_for_known_answers):
crc1 = PredefinedCrc(crcfun_name)
crc1.update(msg)
self.assertEqual(crc1.crcValue, v[i], "Wrong answer for crc1 %s, input '%s'" % (crcfun_name,msg))
crc2 = crc1.new()
# Check that crc1 maintains its same value, after .new() call.
self.assertEqual(crc1.crcValue, v[i], "Wrong state for crc1 %s, input '%s'" % (crcfun_name,msg))
# Check that the new class instance created by .new() contains the initialisation value.
# This depends on the first string in self.test_messages_for_known_answers being
# the empty string.
self.assertEqual(crc2.crcValue, v[0], "Wrong state for crc2 %s, input '%s'" % (crcfun_name,msg))
crc2.update(msg)
# Check that crc1 maintains its same value, after crc2 has called .update()
self.assertEqual(crc1.crcValue, v[i], "Wrong state for crc1 %s, input '%s'" % (crcfun_name,msg))
# Check that crc2 contains the right value after calling .update()
self.assertEqual(crc2.crcValue, v[i], "Wrong state for crc2 %s, input '%s'" % (crcfun_name,msg))
def test_function_predefined_table(self):
for table_entry in _predefined_crc_definitions:
# Check predefined function
crc_func = mkPredefinedCrcFun(table_entry['name'])
calc_value = crc_func("123456789")
self.assertEqual(calc_value, table_entry['check'], "Wrong answer for CRC '%s'" % table_entry['name'])
def test_class_predefined_table(self):
for table_entry in _predefined_crc_definitions:
# Check predefined class
crc1 = PredefinedCrc(table_entry['name'])
crc1.update("123456789")
self.assertEqual(crc1.crcValue, table_entry['check'], "Wrong answer for CRC '%s'" % table_entry['name'])
def runtests():
print "Using extension:", _usingExtension
print
unittest.main()
if __name__ == '__main__':
runtests()

View File

@@ -0,0 +1,501 @@
//-----------------------------------------------------------------------------
// Low level CRC functions for use by crcmod. This version is the C
// implementation that corresponds to the Python module _crcfunpy. This module
// will be used by crcmod if it is built for the target platform. Otherwise,
// the Python module is used.
//
// Copyright (c) 2004 Raymond L. Buvel
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
// Force Py_ssize_t to be used for s# conversions.
#define PY_SSIZE_T_CLEAN
#include <Python.h>
// Make compatible with previous Python versions
#if PY_VERSION_HEX < 0x02050000
typedef int Py_ssize_t;
#define PY_SSIZE_T_MAX INT_MAX
#define PY_SSIZE_T_MIN INT_MIN
#endif
// Note: the type declarations are set up to work on 32-bit platforms using the
// GNU C compiler. They will need to be adjusted for other platforms. In
// particular, the Microsoft Windows compiler uses _int64 instead of long long.
// Define a few types to make it easier to port to other platforms.
typedef unsigned char UINT8;
typedef unsigned short UINT16;
typedef unsigned int UINT32;
typedef unsigned long long UINT64;
// Define some macros for the data format strings. The INPUT strings are for
// decoding the input parameters to the function which are (data, crc, table).
// Note: these format strings use codes that are new in Python 2.3 so it would
// be necessary to rewrite the code for versions earlier than 2.3.
#define INPUT8 "s#Bs#"
#define INPUT16 "s#Hs#"
#define INPUT32 "s#Is#"
#define INPUT64 "s#Ks#"
// Define some macros that extract the specified byte from an integral value in
// what should be a platform independent manner.
#define BYTE0(x) ((UINT8)(x))
#define BYTE1(x) ((UINT8)((x) >> 8))
#define BYTE2(x) ((UINT8)((x) >> 16))
#define BYTE3(x) ((UINT8)((x) >> 24))
#define BYTE7(x) ((UINT8)((x) >> 56))
//-----------------------------------------------------------------------------
// Compute a 8-bit crc over the input data.
// Inputs:
// data - string containing the data
// crc - unsigned integer containing the initial crc
// table - string containing the 8-bit table corresponding to the generator
// polynomial.
// Returns:
// crc - unsigned integer containing the resulting crc
static PyObject*
_crc8(PyObject* self, PyObject* args)
{
UINT8 crc;
UINT8* data;
Py_ssize_t dataLen;
UINT8* table;
Py_ssize_t tableLen;
if (!PyArg_ParseTuple(args, INPUT8, &data, &dataLen, &crc,
&table, &tableLen))
{
return NULL;
}
if (tableLen != 256)
{
PyErr_SetString(PyExc_ValueError, "invalid CRC table");
return NULL;
}
while (dataLen--)
{
crc = table[*data ^ crc];
data++;
}
return PyInt_FromLong((long)crc);
}
//-----------------------------------------------------------------------------
// Compute a 8-bit crc over the input data. The data stream is bit reversed
// during the computation.
// Inputs:
// data - string containing the data
// crc - unsigned integer containing the initial crc
// table - string containing the 8-bit table corresponding to the generator
// polynomial.
// Returns:
// crc - unsigned integer containing the resulting crc
static PyObject*
_crc8r(PyObject* self, PyObject* args)
{
UINT8 crc;
UINT8* data;
Py_ssize_t dataLen;
UINT8* table;
Py_ssize_t tableLen;
if (!PyArg_ParseTuple(args, INPUT8, &data, &dataLen, &crc,
&table, &tableLen))
{
return NULL;
}
if (tableLen != 256)
{
PyErr_SetString(PyExc_ValueError, "invalid CRC table");
return NULL;
}
while (dataLen--)
{
crc = table[*data ^ crc];
data++;
}
return PyInt_FromLong((long)crc);
}
//-----------------------------------------------------------------------------
// Compute a 16-bit crc over the input data.
// Inputs:
// data - string containing the data
// crc - unsigned integer containing the initial crc
// table - string containing the 16-bit table corresponding to the generator
// polynomial.
// Returns:
// crc - unsigned integer containing the resulting crc
static PyObject*
_crc16(PyObject* self, PyObject* args)
{
UINT16 crc;
UINT8* data;
Py_ssize_t dataLen;
UINT16* table;
Py_ssize_t tableLen;
if (!PyArg_ParseTuple(args, INPUT16, &data, &dataLen, &crc,
&table, &tableLen))
{
return NULL;
}
if (tableLen != 256*2)
{
PyErr_SetString(PyExc_ValueError, "invalid CRC table");
return NULL;
}
while (dataLen--)
{
crc = table[*data ^ BYTE1(crc)] ^ (crc << 8);
data++;
}
return PyInt_FromLong((long)crc);
}
//-----------------------------------------------------------------------------
// Compute a 16-bit crc over the input data. The data stream is bit reversed
// during the computation.
// Inputs:
// data - string containing the data
// crc - unsigned integer containing the initial crc
// table - string containing the 16-bit table corresponding to the generator
// polynomial.
// Returns:
// crc - unsigned integer containing the resulting crc
static PyObject*
_crc16r(PyObject* self, PyObject* args)
{
UINT16 crc;
UINT8* data;
Py_ssize_t dataLen;
UINT16* table;
Py_ssize_t tableLen;
if (!PyArg_ParseTuple(args, INPUT16, &data, &dataLen, &crc,
&table, &tableLen))
{
return NULL;
}
if (tableLen != 256*2)
{
PyErr_SetString(PyExc_ValueError, "invalid CRC table");
return NULL;
}
while (dataLen--)
{
crc = table[*data ^ BYTE0(crc)] ^ (crc >> 8);
data++;
}
return PyInt_FromLong((long)crc);
}
//-----------------------------------------------------------------------------
// Compute a 24-bit crc over the input data.
// Inputs:
// data - string containing the data
// crc - unsigned integer containing the initial crc
// table - string containing the 24-bit table corresponding to the generator
// polynomial.
// Returns:
// crc - unsigned integer containing the resulting crc
static PyObject*
_crc24(PyObject* self, PyObject* args)
{
UINT32 crc;
UINT8* data;
Py_ssize_t dataLen;
UINT32* table;
Py_ssize_t tableLen;
if (!PyArg_ParseTuple(args, INPUT32, &data, &dataLen, &crc,
&table, &tableLen))
{
return NULL;
}
if (tableLen != 256*4)
{
PyErr_SetString(PyExc_ValueError, "invalid CRC table");
return NULL;
}
while (dataLen--)
{
crc = table[*data ^ BYTE2(crc)] ^ (crc << 8);
data++;
}
return PyInt_FromLong((long)(crc & 0xFFFFFFU));
}
//-----------------------------------------------------------------------------
// Compute a 24-bit crc over the input data. The data stream is bit reversed
// during the computation.
// Inputs:
// data - string containing the data
// crc - unsigned integer containing the initial crc
// table - string containing the 24-bit table corresponding to the generator
// polynomial.
// Returns:
// crc - unsigned integer containing the resulting crc
static PyObject*
_crc24r(PyObject* self, PyObject* args)
{
UINT32 crc;
UINT8* data;
Py_ssize_t dataLen;
UINT32* table;
Py_ssize_t tableLen;
if (!PyArg_ParseTuple(args, INPUT32, &data, &dataLen, &crc,
&table, &tableLen))
{
return NULL;
}
if (tableLen != 256*4)
{
PyErr_SetString(PyExc_ValueError, "invalid CRC table");
return NULL;
}
crc = crc & 0xFFFFFFU;
while (dataLen--)
{
crc = table[*data ^ BYTE0(crc)] ^ (crc >> 8);
data++;
}
return PyInt_FromLong((long)crc);
}
//-----------------------------------------------------------------------------
// Compute a 32-bit crc over the input data.
// Inputs:
// data - string containing the data
// crc - unsigned integer containing the initial crc
// table - string containing the 32-bit table corresponding to the generator
// polynomial.
// Returns:
// crc - unsigned integer containing the resulting crc
static PyObject*
_crc32(PyObject* self, PyObject* args)
{
UINT32 crc;
UINT8* data;
Py_ssize_t dataLen;
UINT32* table;
Py_ssize_t tableLen;
if (!PyArg_ParseTuple(args, INPUT32, &data, &dataLen, &crc,
&table, &tableLen))
{
return NULL;
}
if (tableLen != 256*4)
{
PyErr_SetString(PyExc_ValueError, "invalid CRC table");
return NULL;
}
while (dataLen--)
{
crc = table[*data ^ BYTE3(crc)] ^ (crc << 8);
data++;
}
return PyLong_FromUnsignedLong(crc);
}
//-----------------------------------------------------------------------------
// Compute a 32-bit crc over the input data. The data stream is bit reversed
// during the computation.
// Inputs:
// data - string containing the data
// crc - unsigned integer containing the initial crc
// table - string containing the 32-bit table corresponding to the generator
// polynomial.
// Returns:
// crc - unsigned integer containing the resulting crc
static PyObject*
_crc32r(PyObject* self, PyObject* args)
{
UINT32 crc;
UINT8* data;
Py_ssize_t dataLen;
UINT32* table;
Py_ssize_t tableLen;
if (!PyArg_ParseTuple(args, INPUT32, &data, &dataLen, &crc,
&table, &tableLen))
{
return NULL;
}
if (tableLen != 256*4)
{
PyErr_SetString(PyExc_ValueError, "invalid CRC table");
return NULL;
}
while (dataLen--)
{
crc = table[*data ^ BYTE0(crc)] ^ (crc >> 8);
data++;
}
return PyLong_FromUnsignedLong(crc);
}
//-----------------------------------------------------------------------------
// Compute a 64-bit crc over the input data.
// Inputs:
// data - string containing the data
// crc - unsigned integer containing the initial crc
// table - string containing the 64-bit table corresponding to the generator
// polynomial.
// Returns:
// crc - unsigned integer containing the resulting crc
static PyObject*
_crc64(PyObject* self, PyObject* args)
{
UINT64 crc;
UINT8* data;
Py_ssize_t dataLen;
UINT64* table;
Py_ssize_t tableLen;
if (!PyArg_ParseTuple(args, INPUT64, &data, &dataLen, &crc,
&table, &tableLen))
{
return NULL;
}
if (tableLen != 256*8)
{
PyErr_SetString(PyExc_ValueError, "invalid CRC table");
return NULL;
}
while (dataLen--)
{
crc = table[*data ^ BYTE7(crc)] ^ (crc << 8);
data++;
}
return PyLong_FromUnsignedLongLong(crc);
}
//-----------------------------------------------------------------------------
// Compute a 64-bit crc over the input data. The data stream is bit reversed
// during the computation.
// Inputs:
// data - string containing the data
// crc - unsigned integer containing the initial crc
// table - string containing the 64-bit table corresponding to the generator
// polynomial.
// Returns:
// crc - unsigned integer containing the resulting crc
static PyObject*
_crc64r(PyObject* self, PyObject* args)
{
UINT64 crc;
UINT8* data;
Py_ssize_t dataLen;
UINT64* table;
Py_ssize_t tableLen;
if (!PyArg_ParseTuple(args, INPUT64, &data, &dataLen, &crc,
&table, &tableLen))
{
return NULL;
}
if (tableLen != 256*8)
{
PyErr_SetString(PyExc_ValueError, "invalid CRC table");
return NULL;
}
while (dataLen--)
{
crc = table[*data ^ BYTE0(crc)] ^ (crc >> 8);
data++;
}
return PyLong_FromUnsignedLongLong(crc);
}
//-----------------------------------------------------------------------------
static PyMethodDef methodTable[] = {
{"_crc8", _crc8, METH_VARARGS},
{"_crc8r", _crc8r, METH_VARARGS},
{"_crc16", _crc16, METH_VARARGS},
{"_crc16r", _crc16r, METH_VARARGS},
{"_crc24", _crc24, METH_VARARGS},
{"_crc24r", _crc24r, METH_VARARGS},
{"_crc32", _crc32, METH_VARARGS},
{"_crc32r", _crc32r, METH_VARARGS},
{"_crc64", _crc64, METH_VARARGS},
{"_crc64r", _crc64r, METH_VARARGS},
{NULL, NULL}
};
//-----------------------------------------------------------------------------
void init_crcfunext(void)
{
PyObject *m;
if ((sizeof(UINT8) != 1) || (sizeof(UINT16) != 2) ||
(sizeof(UINT32) != 4) || (sizeof(UINT64) != 8))
{
Py_FatalError("crcfunext: One of the data types is invalid");
}
m = Py_InitModule("_crcfunext", methodTable);
}

View File

@@ -0,0 +1,8 @@
try:
from crcmod.crcmod import *
import crcmod.predefined
except ImportError:
# Make this backward compatible
from crcmod import *
import predefined
__doc__ = crcmod.__doc__

View File

@@ -0,0 +1,107 @@
#-----------------------------------------------------------------------------
# Low level CRC functions for use by crcmod. This version is implemented in
# Python for a couple of reasons. 1) Provide a reference implememtation.
# 2) Provide a version that can be used on systems where a C compiler is not
# available for building extension modules.
#
# Copyright (c) 2009 Raymond L. Buvel
# Copyright (c) 2010 Craig McQueen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#-----------------------------------------------------------------------------
def _get_buffer_view(in_obj):
if isinstance(in_obj, str):
raise TypeError('Unicode-objects must be encoded before calculating a CRC')
mv = memoryview(in_obj)
if mv.ndim > 1:
raise BufferError('Buffer must be single dimension')
return mv
def _crc8(data, crc, table):
mv = _get_buffer_view(data)
crc = crc & 0xFF
for x in mv.tobytes():
crc = table[x ^ crc]
return crc
def _crc8r(data, crc, table):
mv = _get_buffer_view(data)
crc = crc & 0xFF
for x in mv.tobytes():
crc = table[x ^ crc]
return crc
def _crc16(data, crc, table):
mv = _get_buffer_view(data)
crc = crc & 0xFFFF
for x in mv.tobytes():
crc = table[x ^ ((crc>>8) & 0xFF)] ^ ((crc << 8) & 0xFF00)
return crc
def _crc16r(data, crc, table):
mv = _get_buffer_view(data)
crc = crc & 0xFFFF
for x in mv.tobytes():
crc = table[x ^ (crc & 0xFF)] ^ (crc >> 8)
return crc
def _crc24(data, crc, table):
mv = _get_buffer_view(data)
crc = crc & 0xFFFFFF
for x in mv.tobytes():
crc = table[x ^ (crc>>16 & 0xFF)] ^ ((crc << 8) & 0xFFFF00)
return crc
def _crc24r(data, crc, table):
mv = _get_buffer_view(data)
crc = crc & 0xFFFFFF
for x in mv.tobytes():
crc = table[x ^ (crc & 0xFF)] ^ (crc >> 8)
return crc
def _crc32(data, crc, table):
mv = _get_buffer_view(data)
crc = crc & 0xFFFFFFFF
for x in mv.tobytes():
crc = table[x ^ ((crc>>24) & 0xFF)] ^ ((crc << 8) & 0xFFFFFF00)
return crc
def _crc32r(data, crc, table):
mv = _get_buffer_view(data)
crc = crc & 0xFFFFFFFF
for x in mv.tobytes():
crc = table[x ^ (crc & 0xFF)] ^ (crc >> 8)
return crc
def _crc64(data, crc, table):
mv = _get_buffer_view(data)
crc = crc & 0xFFFFFFFFFFFFFFFF
for x in mv.tobytes():
crc = table[x ^ ((crc>>56) & 0xFF)] ^ ((crc << 8) & 0xFFFFFFFFFFFFFF00)
return crc
def _crc64r(data, crc, table):
mv = _get_buffer_view(data)
crc = crc & 0xFFFFFFFFFFFFFFFF
for x in mv.tobytes():
crc = table[x ^ (crc & 0xFF)] ^ (crc >> 8)
return crc

View File

@@ -0,0 +1,457 @@
#-----------------------------------------------------------------------------
# Copyright (c) 2010 Raymond L. Buvel
# Copyright (c) 2010 Craig McQueen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#-----------------------------------------------------------------------------
'''crcmod is a Python module for gererating objects that compute the Cyclic
Redundancy Check. Any 8, 16, 24, 32, or 64 bit polynomial can be used.
The following are the public components of this module.
Crc -- a class that creates instances providing the same interface as the
algorithms in the hashlib module in the Python standard library. These
instances also provide a method for generating a C/C++ function to compute
the CRC.
mkCrcFun -- create a Python function to compute the CRC using the specified
polynomial and initial value. This provides a much simpler interface if
all you need is a function for CRC calculation.
'''
__all__ = '''mkCrcFun Crc
'''.split()
# Select the appropriate set of low-level CRC functions for this installation.
# If the extension module was not built, drop back to the Python implementation
# even though it is significantly slower.
try:
import crcmod._crcfunext as _crcfun
_usingExtension = True
except ImportError:
import crcmod._crcfunpy as _crcfun
_usingExtension = False
import sys, struct
#-----------------------------------------------------------------------------
class Crc:
'''Compute a Cyclic Redundancy Check (CRC) using the specified polynomial.
Instances of this class have the same interface as the algorithms in the
hashlib module in the Python standard library. See the documentation of
this module for examples of how to use a Crc instance.
The string representation of a Crc instance identifies the polynomial,
initial value, XOR out value, and the current CRC value. The print
statement can be used to output this information.
If you need to generate a C/C++ function for use in another application,
use the generateCode method. If you need to generate code for another
language, subclass Crc and override the generateCode method.
The following are the parameters supplied to the constructor.
poly -- The generator polynomial to use in calculating the CRC. The value
is specified as a Python integer. The bits in this integer are the
coefficients of the polynomial. The only polynomials allowed are those
that generate 8, 16, 24, 32, or 64 bit CRCs.
initCrc -- Initial value used to start the CRC calculation. This initial
value should be the initial shift register value XORed with the final XOR
value. That is equivalent to the CRC result the algorithm should return for
a zero-length string. Defaults to all bits set because that starting value
will take leading zero bytes into account. Starting with zero will ignore
all leading zero bytes.
rev -- A flag that selects a bit reversed algorithm when True. Defaults to
True because the bit reversed algorithms are more efficient.
xorOut -- Final value to XOR with the calculated CRC value. Used by some
CRC algorithms. Defaults to zero.
'''
def __init__(self, poly, initCrc=~0, rev=True, xorOut=0, initialize=True):
if not initialize:
# Don't want to perform the initialization when using new or copy
# to create a new instance.
return
(sizeBits, initCrc, xorOut) = _verifyParams(poly, initCrc, xorOut)
self.digest_size = sizeBits//8
self.initCrc = initCrc
self.xorOut = xorOut
self.poly = poly
self.reverse = rev
(crcfun, table) = _mkCrcFun(poly, sizeBits, initCrc, rev, xorOut)
self._crc = crcfun
self.table = table
self.crcValue = self.initCrc
def __str__(self):
lst = []
lst.append('poly = 0x%X' % self.poly)
lst.append('reverse = %s' % self.reverse)
fmt = '0x%%0%dX' % (self.digest_size*2)
lst.append('initCrc = %s' % (fmt % self.initCrc))
lst.append('xorOut = %s' % (fmt % self.xorOut))
lst.append('crcValue = %s' % (fmt % self.crcValue))
return '\n'.join(lst)
def new(self, arg=None):
'''Create a new instance of the Crc class initialized to the same
values as the original instance. The current CRC is set to the initial
value. If a string is provided in the optional arg parameter, it is
passed to the update method.
'''
n = Crc(poly=None, initialize=False)
n._crc = self._crc
n.digest_size = self.digest_size
n.initCrc = self.initCrc
n.xorOut = self.xorOut
n.table = self.table
n.crcValue = self.initCrc
n.reverse = self.reverse
n.poly = self.poly
if arg is not None:
n.update(arg)
return n
def copy(self):
'''Create a new instance of the Crc class initialized to the same
values as the original instance. The current CRC is set to the current
value. This allows multiple CRC calculations using a common initial
string.
'''
c = self.new()
c.crcValue = self.crcValue
return c
def update(self, data):
'''Update the current CRC value using the string specified as the data
parameter.
'''
self.crcValue = self._crc(data, self.crcValue)
def digest(self):
'''Return the current CRC value as a string of bytes. The length of
this string is specified in the digest_size attribute.
'''
n = self.digest_size
crc = self.crcValue
lst = []
while n > 0:
lst.append(crc & 0xFF)
crc = crc >> 8
n -= 1
lst.reverse()
return bytes(lst)
def hexdigest(self):
'''Return the current CRC value as a string of hex digits. The length
of this string is twice the digest_size attribute.
'''
n = self.digest_size
crc = self.crcValue
lst = []
while n > 0:
lst.append('%02X' % (crc & 0xFF))
crc = crc >> 8
n -= 1
lst.reverse()
return ''.join(lst)
def generateCode(self, functionName, out, dataType=None, crcType=None):
'''Generate a C/C++ function.
functionName -- String specifying the name of the function.
out -- An open file-like object with a write method. This specifies
where the generated code is written.
dataType -- An optional parameter specifying the data type of the input
data to the function. Defaults to UINT8.
crcType -- An optional parameter specifying the data type of the CRC
value. Defaults to one of UINT8, UINT16, UINT32, or UINT64 depending
on the size of the CRC value.
'''
if dataType is None:
dataType = 'UINT8'
if crcType is None:
size = 8*self.digest_size
if size == 24:
size = 32
crcType = 'UINT%d' % size
if self.digest_size == 1:
# Both 8-bit CRC algorithms are the same
crcAlgor = 'table[*data ^ (%s)crc]'
elif self.reverse:
# The bit reverse algorithms are all the same except for the data
# type of the crc variable which is specified elsewhere.
crcAlgor = 'table[*data ^ (%s)crc] ^ (crc >> 8)'
else:
# The forward CRC algorithms larger than 8 bits have an extra shift
# operation to get the high byte.
shift = 8*(self.digest_size - 1)
crcAlgor = 'table[*data ^ (%%s)(crc >> %d)] ^ (crc << 8)' % shift
fmt = '0x%%0%dX' % (2*self.digest_size)
if self.digest_size <= 4:
fmt = fmt + 'U,'
else:
# Need the long long type identifier to keep gcc from complaining.
fmt = fmt + 'ULL,'
# Select the number of entries per row in the output code.
n = {1:8, 2:8, 3:4, 4:4, 8:2}[self.digest_size]
lst = []
for i, val in enumerate(self.table):
if (i % n) == 0:
lst.append('\n ')
lst.append(fmt % val)
poly = 'polynomial: 0x%X' % self.poly
if self.reverse:
poly = poly + ', bit reverse algorithm'
if self.xorOut:
# Need to remove the comma from the format.
preCondition = '\n crc = crc ^ %s;' % (fmt[:-1] % self.xorOut)
postCondition = preCondition
else:
preCondition = ''
postCondition = ''
if self.digest_size == 3:
# The 24-bit CRC needs to be conditioned so that only 24-bits are
# used from the 32-bit variable.
if self.reverse:
preCondition += '\n crc = crc & 0xFFFFFFU;'
else:
postCondition += '\n crc = crc & 0xFFFFFFU;'
parms = {
'dataType' : dataType,
'crcType' : crcType,
'name' : functionName,
'crcAlgor' : crcAlgor % dataType,
'crcTable' : ''.join(lst),
'poly' : poly,
'preCondition' : preCondition,
'postCondition' : postCondition,
}
out.write(_codeTemplate % parms)
#-----------------------------------------------------------------------------
def mkCrcFun(poly, initCrc=~0, rev=True, xorOut=0):
'''Return a function that computes the CRC using the specified polynomial.
poly -- integer representation of the generator polynomial
initCrc -- default initial CRC value
rev -- when true, indicates that the data is processed bit reversed.
xorOut -- the final XOR value
The returned function has the following user interface
def crcfun(data, crc=initCrc):
'''
# First we must verify the params
(sizeBits, initCrc, xorOut) = _verifyParams(poly, initCrc, xorOut)
# Make the function (and table), return the function
return _mkCrcFun(poly, sizeBits, initCrc, rev, xorOut)[0]
#-----------------------------------------------------------------------------
# Naming convention:
# All function names ending with r are bit reverse variants of the ones
# without the r.
#-----------------------------------------------------------------------------
# Check the polynomial to make sure that it is acceptable and return the number
# of bits in the CRC.
def _verifyPoly(poly):
msg = 'The degree of the polynomial must be 8, 16, 24, 32 or 64'
for n in (8,16,24,32,64):
low = 1<<n
high = low*2
if low <= poly < high:
return n
raise ValueError(msg)
#-----------------------------------------------------------------------------
# Bit reverse the input value.
def _bitrev(x, n):
y = 0
for i in range(n):
y = (y << 1) | (x & 1)
x = x >> 1
return y
#-----------------------------------------------------------------------------
# The following functions compute the CRC for a single byte. These are used
# to build up the tables needed in the CRC algorithm. Assumes the high order
# bit of the polynomial has been stripped off.
def _bytecrc(crc, poly, n):
mask = 1<<(n-1)
for i in range(8):
if crc & mask:
crc = (crc << 1) ^ poly
else:
crc = crc << 1
mask = (1<<n) - 1
crc = crc & mask
return crc
def _bytecrc_r(crc, poly, n):
for i in range(8):
if crc & 1:
crc = (crc >> 1) ^ poly
else:
crc = crc >> 1
mask = (1<<n) - 1
crc = crc & mask
return crc
#-----------------------------------------------------------------------------
# The following functions compute the table needed to compute the CRC. The
# table is returned as a list. Note that the array module does not support
# 64-bit integers on a 32-bit architecture as of Python 2.3.
#
# These routines assume that the polynomial and the number of bits in the CRC
# have been checked for validity by the caller.
def _mkTable(poly, n):
mask = (1<<n) - 1
poly = poly & mask
table = [_bytecrc(i<<(n-8),poly,n) for i in range(256)]
return table
def _mkTable_r(poly, n):
mask = (1<<n) - 1
poly = _bitrev(poly & mask, n)
table = [_bytecrc_r(i,poly,n) for i in range(256)]
return table
#-----------------------------------------------------------------------------
# Map the CRC size onto the functions that handle these sizes.
_sizeMap = {
8 : [_crcfun._crc8, _crcfun._crc8r],
16 : [_crcfun._crc16, _crcfun._crc16r],
24 : [_crcfun._crc24, _crcfun._crc24r],
32 : [_crcfun._crc32, _crcfun._crc32r],
64 : [_crcfun._crc64, _crcfun._crc64r],
}
#-----------------------------------------------------------------------------
# Build a mapping of size to struct module type code. This table is
# constructed dynamically so that it has the best chance of picking the best
# code to use for the platform we are running on. This should properly adapt
# to 32 and 64 bit machines.
_sizeToTypeCode = {}
for typeCode in 'B H I L Q'.split():
size = {1:8, 2:16, 4:32, 8:64}.get(struct.calcsize(typeCode),None)
if size is not None and size not in _sizeToTypeCode:
_sizeToTypeCode[size] = '256%s' % typeCode
_sizeToTypeCode[24] = _sizeToTypeCode[32]
del typeCode, size
#-----------------------------------------------------------------------------
# The following function validates the parameters of the CRC, namely,
# poly, and initial/final XOR values.
# It returns the size of the CRC (in bits), and "sanitized" initial/final XOR values.
def _verifyParams(poly, initCrc, xorOut):
sizeBits = _verifyPoly(poly)
mask = (1<<sizeBits) - 1
# Adjust the initial CRC to the correct data type (unsigned value).
initCrc = initCrc & mask
# Similar for XOR-out value.
xorOut = xorOut & mask
return (sizeBits, initCrc, xorOut)
#-----------------------------------------------------------------------------
# The following function returns a Python function to compute the CRC.
#
# It must be passed parameters that are already verified & sanitized by
# _verifyParams().
#
# The returned function calls a low level function that is written in C if the
# extension module could be loaded. Otherwise, a Python implementation is
# used.
#
# In addition to this function, a list containing the CRC table is returned.
def _mkCrcFun(poly, sizeBits, initCrc, rev, xorOut):
if rev:
tableList = _mkTable_r(poly, sizeBits)
_fun = _sizeMap[sizeBits][1]
else:
tableList = _mkTable(poly, sizeBits)
_fun = _sizeMap[sizeBits][0]
_table = tableList
if _usingExtension:
_table = struct.pack(_sizeToTypeCode[sizeBits], *tableList)
if xorOut == 0:
def crcfun(data, crc=initCrc, table=_table, fun=_fun):
return fun(data, crc, table)
else:
def crcfun(data, crc=initCrc, table=_table, fun=_fun):
return xorOut ^ fun(data, xorOut ^ crc, table)
return crcfun, tableList
#-----------------------------------------------------------------------------
_codeTemplate = '''// Automatically generated CRC function
// %(poly)s
%(crcType)s
%(name)s(%(dataType)s *data, int len, %(crcType)s crc)
{
static const %(crcType)s table[256] = {%(crcTable)s
};
%(preCondition)s
while (len > 0)
{
crc = %(crcAlgor)s;
data++;
len--;
}%(postCondition)s
return crc;
}
'''

View File

@@ -0,0 +1,162 @@
#-----------------------------------------------------------------------------
# Copyright (c) 2010 Craig McQueen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#-----------------------------------------------------------------------------
'''
crcmod.predefined defines some well-known CRC algorithms.
To use it, e.g.:
import crcmod.predefined
crc32func = crcmod.predefined.mkPredefinedCrcFun("crc-32")
crc32class = crcmod.predefined.PredefinedCrc("crc-32")
crcmod.predefined.Crc is an alias for crcmod.predefined.PredefinedCrc
But if doing 'from crc.predefined import *', only PredefinedCrc is imported.
'''
# local imports
import crcmod
__all__ = [
'PredefinedCrc',
'mkPredefinedCrcFun',
]
REVERSE = True
NON_REVERSE = False
# The following table defines the parameters of well-known CRC algorithms.
# The "Check" value is the CRC for the ASCII byte sequence b"123456789". It
# can be used for unit tests.
_crc_definitions_table = [
# Name Identifier-name, Poly Reverse Init-value XOR-out Check
[ 'crc-8', 'Crc8', 0x107, NON_REVERSE, 0x00, 0x00, 0xF4, ],
[ 'crc-8-darc', 'Crc8Darc', 0x139, REVERSE, 0x00, 0x00, 0x15, ],
[ 'crc-8-i-code', 'Crc8ICode', 0x11D, NON_REVERSE, 0xFD, 0x00, 0x7E, ],
[ 'crc-8-itu', 'Crc8Itu', 0x107, NON_REVERSE, 0x55, 0x55, 0xA1, ],
[ 'crc-8-maxim', 'Crc8Maxim', 0x131, REVERSE, 0x00, 0x00, 0xA1, ],
[ 'crc-8-rohc', 'Crc8Rohc', 0x107, REVERSE, 0xFF, 0x00, 0xD0, ],
[ 'crc-8-wcdma', 'Crc8Wcdma', 0x19B, REVERSE, 0x00, 0x00, 0x25, ],
[ 'crc-16', 'Crc16', 0x18005, REVERSE, 0x0000, 0x0000, 0xBB3D, ],
[ 'crc-16-buypass', 'Crc16Buypass', 0x18005, NON_REVERSE, 0x0000, 0x0000, 0xFEE8, ],
[ 'crc-16-dds-110', 'Crc16Dds110', 0x18005, NON_REVERSE, 0x800D, 0x0000, 0x9ECF, ],
[ 'crc-16-dect', 'Crc16Dect', 0x10589, NON_REVERSE, 0x0001, 0x0001, 0x007E, ],
[ 'crc-16-dnp', 'Crc16Dnp', 0x13D65, REVERSE, 0xFFFF, 0xFFFF, 0xEA82, ],
[ 'crc-16-en-13757', 'Crc16En13757', 0x13D65, NON_REVERSE, 0xFFFF, 0xFFFF, 0xC2B7, ],
[ 'crc-16-genibus', 'Crc16Genibus', 0x11021, NON_REVERSE, 0x0000, 0xFFFF, 0xD64E, ],
[ 'crc-16-maxim', 'Crc16Maxim', 0x18005, REVERSE, 0xFFFF, 0xFFFF, 0x44C2, ],
[ 'crc-16-mcrf4xx', 'Crc16Mcrf4xx', 0x11021, REVERSE, 0xFFFF, 0x0000, 0x6F91, ],
[ 'crc-16-riello', 'Crc16Riello', 0x11021, REVERSE, 0x554D, 0x0000, 0x63D0, ],
[ 'crc-16-t10-dif', 'Crc16T10Dif', 0x18BB7, NON_REVERSE, 0x0000, 0x0000, 0xD0DB, ],
[ 'crc-16-teledisk', 'Crc16Teledisk', 0x1A097, NON_REVERSE, 0x0000, 0x0000, 0x0FB3, ],
[ 'crc-16-usb', 'Crc16Usb', 0x18005, REVERSE, 0x0000, 0xFFFF, 0xB4C8, ],
[ 'x-25', 'CrcX25', 0x11021, REVERSE, 0x0000, 0xFFFF, 0x906E, ],
[ 'xmodem', 'CrcXmodem', 0x11021, NON_REVERSE, 0x0000, 0x0000, 0x31C3, ],
[ 'modbus', 'CrcModbus', 0x18005, REVERSE, 0xFFFF, 0x0000, 0x4B37, ],
# Note definitions of CCITT are disputable. See:
# http://homepages.tesco.net/~rainstorm/crc-catalogue.htm
# http://web.archive.org/web/20071229021252/http://www.joegeluso.com/software/articles/ccitt.htm
[ 'kermit', 'CrcKermit', 0x11021, REVERSE, 0x0000, 0x0000, 0x2189, ],
[ 'crc-ccitt-false', 'CrcCcittFalse', 0x11021, NON_REVERSE, 0xFFFF, 0x0000, 0x29B1, ],
[ 'crc-aug-ccitt', 'CrcAugCcitt', 0x11021, NON_REVERSE, 0x1D0F, 0x0000, 0xE5CC, ],
[ 'crc-24', 'Crc24', 0x1864CFB, NON_REVERSE, 0xB704CE, 0x000000, 0x21CF02, ],
[ 'crc-24-flexray-a', 'Crc24FlexrayA', 0x15D6DCB, NON_REVERSE, 0xFEDCBA, 0x000000, 0x7979BD, ],
[ 'crc-24-flexray-b', 'Crc24FlexrayB', 0x15D6DCB, NON_REVERSE, 0xABCDEF, 0x000000, 0x1F23B8, ],
[ 'crc-32', 'Crc32', 0x104C11DB7, REVERSE, 0x00000000, 0xFFFFFFFF, 0xCBF43926, ],
[ 'crc-32-bzip2', 'Crc32Bzip2', 0x104C11DB7, NON_REVERSE, 0x00000000, 0xFFFFFFFF, 0xFC891918, ],
[ 'crc-32c', 'Crc32C', 0x11EDC6F41, REVERSE, 0x00000000, 0xFFFFFFFF, 0xE3069283, ],
[ 'crc-32d', 'Crc32D', 0x1A833982B, REVERSE, 0x00000000, 0xFFFFFFFF, 0x87315576, ],
[ 'crc-32-mpeg', 'Crc32Mpeg', 0x104C11DB7, NON_REVERSE, 0xFFFFFFFF, 0x00000000, 0x0376E6E7, ],
[ 'posix', 'CrcPosix', 0x104C11DB7, NON_REVERSE, 0xFFFFFFFF, 0xFFFFFFFF, 0x765E7680, ],
[ 'crc-32q', 'Crc32Q', 0x1814141AB, NON_REVERSE, 0x00000000, 0x00000000, 0x3010BF7F, ],
[ 'jamcrc', 'CrcJamCrc', 0x104C11DB7, REVERSE, 0xFFFFFFFF, 0x00000000, 0x340BC6D9, ],
[ 'xfer', 'CrcXfer', 0x1000000AF, NON_REVERSE, 0x00000000, 0x00000000, 0xBD0BE338, ],
# 64-bit
# Name Identifier-name, Poly Reverse Init-value XOR-out Check
[ 'crc-64', 'Crc64', 0x1000000000000001B, REVERSE, 0x0000000000000000, 0x0000000000000000, 0x46A5A9388A5BEFFE, ],
[ 'crc-64-we', 'Crc64We', 0x142F0E1EBA9EA3693, NON_REVERSE, 0x0000000000000000, 0xFFFFFFFFFFFFFFFF, 0x62EC59E3F1A4F00A, ],
[ 'crc-64-jones', 'Crc64Jones', 0x1AD93D23594C935A9, REVERSE, 0xFFFFFFFFFFFFFFFF, 0x0000000000000000, 0xCAA717168609F281, ],
]
def _simplify_name(name):
"""
Reduce CRC definition name to a simplified form:
* lowercase
* dashes removed
* spaces removed
* any initial "CRC" string removed
"""
name = name.lower()
name = name.replace('-', '')
name = name.replace(' ', '')
if name.startswith('crc'):
name = name[len('crc'):]
return name
_crc_definitions_by_name = {}
_crc_definitions_by_identifier = {}
_crc_definitions = []
_crc_table_headings = [ 'name', 'identifier', 'poly', 'reverse', 'init', 'xor_out', 'check' ]
for table_entry in _crc_definitions_table:
crc_definition = dict(zip(_crc_table_headings, table_entry))
_crc_definitions.append(crc_definition)
name = _simplify_name(table_entry[0])
if name in _crc_definitions_by_name:
raise Exception("Duplicate entry for '{0}' in CRC table".format(name))
_crc_definitions_by_name[name] = crc_definition
_crc_definitions_by_identifier[table_entry[1]] = crc_definition
def _get_definition_by_name(crc_name):
definition = _crc_definitions_by_name.get(_simplify_name(crc_name), None)
if not definition:
definition = _crc_definitions_by_identifier.get(crc_name, None)
if not definition:
raise KeyError("Unkown CRC name '{0}'".format(crc_name))
return definition
class PredefinedCrc(crcmod.Crc):
def __init__(self, crc_name):
definition = _get_definition_by_name(crc_name)
super().__init__(poly=definition['poly'], initCrc=definition['init'], rev=definition['reverse'], xorOut=definition['xor_out'])
# crcmod.predefined.Crc is an alias for crcmod.predefined.PredefinedCrc
Crc = PredefinedCrc
def mkPredefinedCrcFun(crc_name):
definition = _get_definition_by_name(crc_name)
return crcmod.mkCrcFun(poly=definition['poly'], initCrc=definition['init'], rev=definition['reverse'], xorOut=definition['xor_out'])
# crcmod.predefined.mkCrcFun is an alias for crcmod.predefined.mkPredefinedCrcFun
mkCrcFun = mkPredefinedCrcFun

View File

@@ -0,0 +1,540 @@
#-----------------------------------------------------------------------------
# Copyright (c) 2010 Raymond L. Buvel
# Copyright (c) 2010 Craig McQueen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
#-----------------------------------------------------------------------------
'''Unit tests for crcmod functionality'''
import unittest
from array import array
import binascii
from .crcmod import mkCrcFun, Crc
from .crcmod import _usingExtension
from .predefined import PredefinedCrc
from .predefined import mkPredefinedCrcFun
from .predefined import _crc_definitions as _predefined_crc_definitions
#-----------------------------------------------------------------------------
# This polynomial was chosen because it is the product of two irreducible
# polynomials.
# g8 = (x^7+x+1)*(x+1)
g8 = 0x185
#-----------------------------------------------------------------------------
# The following reproduces all of the entries in the Numerical Recipes table.
# This is the standard CCITT polynomial.
g16 = 0x11021
#-----------------------------------------------------------------------------
g24 = 0x15D6DCB
#-----------------------------------------------------------------------------
# This is the standard AUTODIN-II polynomial which appears to be used in a
# wide variety of standards and applications.
g32 = 0x104C11DB7
#-----------------------------------------------------------------------------
# I was able to locate a couple of 64-bit polynomials on the web. To make it
# easier to input the representation, define a function that builds a
# polynomial from a list of the bits that need to be turned on.
def polyFromBits(bits):
p = 0
for n in bits:
p = p | (1 << n)
return p
# The following is from the paper "An Improved 64-bit Cyclic Redundancy Check
# for Protein Sequences" by David T. Jones
g64a = polyFromBits([64, 63, 61, 59, 58, 56, 55, 52, 49, 48, 47, 46, 44, 41,
37, 36, 34, 32, 31, 28, 26, 23, 22, 19, 16, 13, 12, 10, 9, 6, 4,
3, 0])
# The following is from Standard ECMA-182 "Data Interchange on 12,7 mm 48-Track
# Magnetic Tape Cartridges -DLT1 Format-", December 1992.
g64b = polyFromBits([64, 62, 57, 55, 54, 53, 52, 47, 46, 45, 40, 39, 38, 37,
35, 33, 32, 31, 29, 27, 24, 23, 22, 21, 19, 17, 13, 12, 10, 9, 7,
4, 1, 0])
#-----------------------------------------------------------------------------
# This class is used to check the CRC calculations against a direct
# implementation using polynomial division.
class poly:
'''Class implementing polynomials over the field of integers mod 2'''
def __init__(self,p):
p = int(p)
if p < 0: raise ValueError('invalid polynomial')
self.p = p
def __int__(self):
return self.p
def __eq__(self,other):
return self.p == other.p
def __ne__(self,other):
return self.p != other.p
# To allow sorting of polynomials, use their long integer form for
# comparison
def __cmp__(self,other):
return cmp(self.p, other.p)
def __bool__(self):
return self.p != 0
def __neg__(self):
return self # These polynomials are their own inverse under addition
def __invert__(self):
n = max(self.deg() + 1, 1)
x = (1 << n) - 1
return poly(self.p ^ x)
def __add__(self,other):
return poly(self.p ^ other.p)
def __sub__(self,other):
return poly(self.p ^ other.p)
def __mul__(self,other):
a = self.p
b = other.p
if a == 0 or b == 0: return poly(0)
x = 0
while b:
if b&1:
x = x ^ a
a = a<<1
b = b>>1
return poly(x)
def __divmod__(self,other):
u = self.p
m = self.deg()
v = other.p
n = other.deg()
if v == 0: raise ZeroDivisionError('polynomial division by zero')
if n == 0: return (self,poly(0))
if m < n: return (poly(0),self)
k = m-n
a = 1 << m
v = v << k
q = 0
while k > 0:
if a & u:
u = u ^ v
q = q | 1
q = q << 1
a = a >> 1
v = v >> 1
k -= 1
if a & u:
u = u ^ v
q = q | 1
return (poly(q),poly(u))
def __div__(self,other):
return self.__divmod__(other)[0]
def __mod__(self,other):
return self.__divmod__(other)[1]
def __repr__(self):
return 'poly(0x%XL)' % self.p
def __str__(self):
p = self.p
if p == 0: return '0'
lst = { 0:[], 1:['1'], 2:['x'], 3:['1','x'] }[p&3]
p = p>>2
n = 2
while p:
if p&1: lst.append('x^%d' % n)
p = p>>1
n += 1
lst.reverse()
return '+'.join(lst)
def deg(self):
'''return the degree of the polynomial'''
a = self.p
if a == 0: return -1
n = 0
while a >= 0x10000:
n += 16
a = a >> 16
a = int(a)
while a > 1:
n += 1
a = a >> 1
return n
#-----------------------------------------------------------------------------
# The following functions compute the CRC using direct polynomial division.
# These functions are checked against the result of the table driven
# algorithms.
g8p = poly(g8)
x8p = poly(1<<8)
def crc8p(d):
p = 0
for i in d:
p = p*256 + i
p = poly(p)
return int(p*x8p%g8p)
g16p = poly(g16)
x16p = poly(1<<16)
def crc16p(d):
p = 0
for i in d:
p = p*256 + i
p = poly(p)
return int(p*x16p%g16p)
g24p = poly(g24)
x24p = poly(1<<24)
def crc24p(d):
p = 0
for i in d:
p = p*256 + i
p = poly(p)
return int(p*x24p%g24p)
g32p = poly(g32)
x32p = poly(1<<32)
def crc32p(d):
p = 0
for i in d:
p = p*256 + i
p = poly(p)
return int(p*x32p%g32p)
g64ap = poly(g64a)
x64p = poly(1<<64)
def crc64ap(d):
p = 0
for i in d:
p = p*256 + i
p = poly(p)
return int(p*x64p%g64ap)
g64bp = poly(g64b)
def crc64bp(d):
p = 0
for i in d:
p = p*256 + i
p = poly(p)
return int(p*x64p%g64bp)
class KnownAnswerTests(unittest.TestCase):
test_messages = [
b'T',
b'CatMouse987654321',
]
known_answers = [
[ (g8,0,0), (0xFE, 0x9D) ],
[ (g8,-1,1), (0x4F, 0x9B) ],
[ (g8,0,1), (0xFE, 0x62) ],
[ (g16,0,0), (0x1A71, 0xE556) ],
[ (g16,-1,1), (0x1B26, 0xF56E) ],
[ (g16,0,1), (0x14A1, 0xC28D) ],
[ (g24,0,0), (0xBCC49D, 0xC4B507) ],
[ (g24,-1,1), (0x59BD0E, 0x0AAA37) ],
[ (g24,0,1), (0xD52B0F, 0x1523AB) ],
[ (g32,0,0), (0x6B93DDDB, 0x12DCA0F4) ],
[ (g32,0xFFFFFFFF,1), (0x41FB859F, 0xF7B400A7) ],
[ (g32,0,1), (0x6C0695ED, 0xC1A40EE5) ],
[ (g32,0,1,0xFFFFFFFF), (0xBE047A60, 0x084BFF58) ],
]
def test_known_answers(self):
for crcfun_params, v in self.known_answers:
crcfun = mkCrcFun(*crcfun_params)
self.assertEqual(crcfun(b'',0), 0, "Wrong answer for CRC parameters %s, input ''" % (crcfun_params,))
for i, msg in enumerate(self.test_messages):
self.assertEqual(crcfun(msg), v[i], "Wrong answer for CRC parameters %s, input '%s'" % (crcfun_params,msg))
self.assertEqual(crcfun(msg[4:], crcfun(msg[:4])), v[i], "Wrong answer for CRC parameters %s, input '%s'" % (crcfun_params,msg))
self.assertEqual(crcfun(msg[-1:], crcfun(msg[:-1])), v[i], "Wrong answer for CRC parameters %s, input '%s'" % (crcfun_params,msg))
class CompareReferenceCrcTest(unittest.TestCase):
test_messages = [
b'',
b'T',
b'123456789',
b'CatMouse987654321',
]
test_poly_crcs = [
[ (g8,0,0), crc8p ],
[ (g16,0,0), crc16p ],
[ (g24,0,0), crc24p ],
[ (g32,0,0), crc32p ],
[ (g64a,0,0), crc64ap ],
[ (g64b,0,0), crc64bp ],
]
@staticmethod
def reference_crc32(d, crc=0):
"""This function modifies the return value of binascii.crc32
to be an unsigned 32-bit value. I.e. in the range 0 to 2**32-1."""
# Work around the future warning on constants.
if crc > 0x7FFFFFFF:
x = int(crc & 0x7FFFFFFF)
crc = x | -2147483648
x = binascii.crc32(d,crc)
return int(x) & 0xFFFFFFFF
def test_compare_crc32(self):
"""The binascii module has a 32-bit CRC function that is used in a wide range
of applications including the checksum used in the ZIP file format.
This test compares the CRC-32 implementation of this crcmod module to
that of binascii.crc32."""
# The following function should produce the same result as
# self.reference_crc32 which is derived from binascii.crc32.
crc32 = mkCrcFun(g32,0,1,0xFFFFFFFF)
for msg in self.test_messages:
self.assertEqual(crc32(msg), self.reference_crc32(msg))
def test_compare_poly(self):
"""Compare various CRCs of this crcmod module to a pure
polynomial-based implementation."""
for crcfun_params, crc_poly_fun in self.test_poly_crcs:
# The following function should produce the same result as
# the associated polynomial CRC function.
crcfun = mkCrcFun(*crcfun_params)
for msg in self.test_messages:
self.assertEqual(crcfun(msg), crc_poly_fun(msg))
class CrcClassTest(unittest.TestCase):
"""Verify the Crc class"""
msg = b'CatMouse987654321'
def test_simple_crc32_class(self):
"""Verify the CRC class when not using xorOut"""
crc = Crc(g32)
str_rep = \
'''poly = 0x104C11DB7
reverse = True
initCrc = 0xFFFFFFFF
xorOut = 0x00000000
crcValue = 0xFFFFFFFF'''
self.assertEqual(str(crc), str_rep)
self.assertEqual(crc.digest(), b'\xff\xff\xff\xff')
self.assertEqual(crc.hexdigest(), 'FFFFFFFF')
crc.update(self.msg)
self.assertEqual(crc.crcValue, 0xF7B400A7)
self.assertEqual(crc.digest(), b'\xf7\xb4\x00\xa7')
self.assertEqual(crc.hexdigest(), 'F7B400A7')
# Verify the .copy() method
x = crc.copy()
self.assertTrue(x is not crc)
str_rep = \
'''poly = 0x104C11DB7
reverse = True
initCrc = 0xFFFFFFFF
xorOut = 0x00000000
crcValue = 0xF7B400A7'''
self.assertEqual(str(crc), str_rep)
self.assertEqual(str(x), str_rep)
def test_full_crc32_class(self):
"""Verify the CRC class when using xorOut"""
crc = Crc(g32, initCrc=0, xorOut= ~0)
str_rep = \
'''poly = 0x104C11DB7
reverse = True
initCrc = 0x00000000
xorOut = 0xFFFFFFFF
crcValue = 0x00000000'''
self.assertEqual(str(crc), str_rep)
self.assertEqual(crc.digest(), b'\x00\x00\x00\x00')
self.assertEqual(crc.hexdigest(), '00000000')
crc.update(self.msg)
self.assertEqual(crc.crcValue, 0x84BFF58)
self.assertEqual(crc.digest(), b'\x08\x4b\xff\x58')
self.assertEqual(crc.hexdigest(), '084BFF58')
# Verify the .copy() method
x = crc.copy()
self.assertTrue(x is not crc)
str_rep = \
'''poly = 0x104C11DB7
reverse = True
initCrc = 0x00000000
xorOut = 0xFFFFFFFF
crcValue = 0x084BFF58'''
self.assertEqual(str(crc), str_rep)
self.assertEqual(str(x), str_rep)
# Verify the .new() method
y = crc.new()
self.assertTrue(y is not crc)
self.assertTrue(y is not x)
str_rep = \
'''poly = 0x104C11DB7
reverse = True
initCrc = 0x00000000
xorOut = 0xFFFFFFFF
crcValue = 0x00000000'''
self.assertEqual(str(y), str_rep)
class PredefinedCrcTest(unittest.TestCase):
"""Verify the predefined CRCs"""
test_messages_for_known_answers = [
b'', # Test cases below depend on this first entry being the empty string.
b'T',
b'CatMouse987654321',
]
known_answers = [
[ 'crc-aug-ccitt', (0x1D0F, 0xD6ED, 0x5637) ],
[ 'x-25', (0x0000, 0xE4D9, 0x0A91) ],
[ 'crc-32', (0x00000000, 0xBE047A60, 0x084BFF58) ],
]
def test_known_answers(self):
for crcfun_name, v in self.known_answers:
crcfun = mkPredefinedCrcFun(crcfun_name)
self.assertEqual(crcfun(b'',0), 0, "Wrong answer for CRC '%s', input ''" % crcfun_name)
for i, msg in enumerate(self.test_messages_for_known_answers):
self.assertEqual(crcfun(msg), v[i], "Wrong answer for CRC %s, input '%s'" % (crcfun_name,msg))
self.assertEqual(crcfun(msg[4:], crcfun(msg[:4])), v[i], "Wrong answer for CRC %s, input '%s'" % (crcfun_name,msg))
self.assertEqual(crcfun(msg[-1:], crcfun(msg[:-1])), v[i], "Wrong answer for CRC %s, input '%s'" % (crcfun_name,msg))
def test_class_with_known_answers(self):
for crcfun_name, v in self.known_answers:
for i, msg in enumerate(self.test_messages_for_known_answers):
crc1 = PredefinedCrc(crcfun_name)
crc1.update(msg)
self.assertEqual(crc1.crcValue, v[i], "Wrong answer for crc1 %s, input '%s'" % (crcfun_name,msg))
crc2 = crc1.new()
# Check that crc1 maintains its same value, after .new() call.
self.assertEqual(crc1.crcValue, v[i], "Wrong state for crc1 %s, input '%s'" % (crcfun_name,msg))
# Check that the new class instance created by .new() contains the initialisation value.
# This depends on the first string in self.test_messages_for_known_answers being
# the empty string.
self.assertEqual(crc2.crcValue, v[0], "Wrong state for crc2 %s, input '%s'" % (crcfun_name,msg))
crc2.update(msg)
# Check that crc1 maintains its same value, after crc2 has called .update()
self.assertEqual(crc1.crcValue, v[i], "Wrong state for crc1 %s, input '%s'" % (crcfun_name,msg))
# Check that crc2 contains the right value after calling .update()
self.assertEqual(crc2.crcValue, v[i], "Wrong state for crc2 %s, input '%s'" % (crcfun_name,msg))
def test_function_predefined_table(self):
for table_entry in _predefined_crc_definitions:
# Check predefined function
crc_func = mkPredefinedCrcFun(table_entry['name'])
calc_value = crc_func(b"123456789")
self.assertEqual(calc_value, table_entry['check'], "Wrong answer for CRC '%s'" % table_entry['name'])
def test_class_predefined_table(self):
for table_entry in _predefined_crc_definitions:
# Check predefined class
crc1 = PredefinedCrc(table_entry['name'])
crc1.update(b"123456789")
self.assertEqual(crc1.crcValue, table_entry['check'], "Wrong answer for CRC '%s'" % table_entry['name'])
class InputTypesTest(unittest.TestCase):
"""Check the various input types that CRC functions can accept."""
msg = b'CatMouse987654321'
check_crc_names = [
'crc-aug-ccitt',
'x-25',
'crc-32',
]
array_check_types = [
'B',
'H',
'I',
'L',
]
def test_bytearray_input(self):
"""Test that bytearray inputs are accepted, as an example
of a type that implements the buffer protocol."""
for crc_name in self.check_crc_names:
crcfun = mkPredefinedCrcFun(crc_name)
for i in range(len(self.msg) + 1):
test_msg = self.msg[:i]
bytes_answer = crcfun(test_msg)
bytearray_answer = crcfun(bytearray(test_msg))
self.assertEqual(bytes_answer, bytearray_answer)
def test_array_input(self):
"""Test that array inputs are accepted, as an example
of a type that implements the buffer protocol."""
for crc_name in self.check_crc_names:
crcfun = mkPredefinedCrcFun(crc_name)
for i in range(len(self.msg) + 1):
test_msg = self.msg[:i]
bytes_answer = crcfun(test_msg)
for array_type in self.array_check_types:
if i % array(array_type).itemsize == 0:
test_array = array(array_type, test_msg)
array_answer = crcfun(test_array)
self.assertEqual(bytes_answer, array_answer)
def test_unicode_input(self):
"""Test that Unicode input raises TypeError"""
for crc_name in self.check_crc_names:
crcfun = mkPredefinedCrcFun(crc_name)
with self.assertRaises(TypeError):
crcfun("123456789")
def runtests():
print("Using extension:", _usingExtension)
print()
unittest.main()
if __name__ == '__main__':
runtests()

View File

@@ -0,0 +1,609 @@
//-----------------------------------------------------------------------------
// Low level CRC functions for use by crcmod. This version is the C
// implementation that corresponds to the Python module _crcfunpy. This module
// will be used by crcmod if it is built for the target platform. Otherwise,
// the Python module is used.
//
// Copyright (c) 2010 Raymond L. Buvel
// Copyright (c) 2010 Craig McQueen
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#define PY_SSIZE_T_CLEAN
#include <Python.h>
// Note: the type declarations are set up to work on 32-bit and 64-bit
// platforms using the GNU C compiler. They may need to be adjusted for other
// platforms.
// Define a few types to make it easier to port to other platforms.
typedef unsigned char UINT8;
typedef unsigned short UINT16;
typedef unsigned int UINT32;
typedef unsigned long long UINT64;
// Define some macros for the data format strings. The INPUT strings are for
// decoding the input parameters to the function which are (data, crc, table).
#define INPUT8 "OBs#"
#define INPUT16 "OHs#"
#define INPUT32 "OIs#"
#define INPUT64 "OKs#"
// The following macro is taken from hashlib.h in the Python 3.1 code,
// providing "Common code for use by all hashlib related modules".
// Given a PyObject* obj, fill in the Py_buffer* viewp with the result
// of PyObject_GetBuffer. Sets an exception and issues a return NULL
// on any errors.
#define GET_BUFFER_VIEW_OR_ERROUT(obj, viewp) do { \
if (PyUnicode_Check((obj))) { \
PyErr_SetString(PyExc_TypeError, \
"Unicode-objects must be encoded before calculating a CRC");\
return NULL; \
} \
if (!PyObject_CheckBuffer((obj))) { \
PyErr_SetString(PyExc_TypeError, \
"object supporting the buffer API required"); \
return NULL; \
} \
if (PyObject_GetBuffer((obj), (viewp), PyBUF_SIMPLE) == -1) { \
return NULL; \
} \
if ((viewp)->ndim > 1) { \
PyErr_SetString(PyExc_BufferError, \
"Buffer must be single dimension"); \
PyBuffer_Release((viewp)); \
return NULL; \
} \
} while(0);
// Define some macros that extract the specified byte from an integral value in
// what should be a platform independent manner.
#define BYTE0(x) ((UINT8)(x))
#define BYTE1(x) ((UINT8)((x) >> 8))
#define BYTE2(x) ((UINT8)((x) >> 16))
#define BYTE3(x) ((UINT8)((x) >> 24))
#define BYTE7(x) ((UINT8)((x) >> 56))
//-----------------------------------------------------------------------------
// Compute a 8-bit crc over the input data.
// Inputs:
// data - string containing the data
// crc - unsigned integer containing the initial crc
// table - string containing the 8-bit table corresponding to the generator
// polynomial.
// Returns:
// crc - unsigned integer containing the resulting crc
static PyObject*
_crc8(PyObject* self, PyObject* args)
{
PyObject *obj;
Py_buffer buf;
UINT8 crc;
UINT8* data;
Py_ssize_t dataLen;
UINT8* table;
Py_ssize_t tableLen;
if (!PyArg_ParseTuple(args, INPUT8, &obj, &crc,
&table, &tableLen))
{
return NULL;
}
if (tableLen != 256)
{
PyErr_SetString(PyExc_ValueError, "invalid CRC table");
return NULL;
}
GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
data = buf.buf;
dataLen = buf.len;
while (dataLen--)
{
crc = table[*data ^ crc];
data++;
}
PyBuffer_Release(&buf);
return PyLong_FromLong((long)crc);
}
//-----------------------------------------------------------------------------
// Compute a 8-bit crc over the input data. The data stream is bit reversed
// during the computation.
// Inputs:
// data - string containing the data
// crc - unsigned integer containing the initial crc
// table - string containing the 8-bit table corresponding to the generator
// polynomial.
// Returns:
// crc - unsigned integer containing the resulting crc
static PyObject*
_crc8r(PyObject* self, PyObject* args)
{
PyObject *obj;
Py_buffer buf;
UINT8 crc;
UINT8* data;
Py_ssize_t dataLen;
UINT8* table;
Py_ssize_t tableLen;
if (!PyArg_ParseTuple(args, INPUT8, &obj, &crc,
&table, &tableLen))
{
return NULL;
}
if (tableLen != 256)
{
PyErr_SetString(PyExc_ValueError, "invalid CRC table");
return NULL;
}
GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
data = buf.buf;
dataLen = buf.len;
while (dataLen--)
{
crc = table[*data ^ crc];
data++;
}
PyBuffer_Release(&buf);
return PyLong_FromLong((long)crc);
}
//-----------------------------------------------------------------------------
// Compute a 16-bit crc over the input data.
// Inputs:
// data - string containing the data
// crc - unsigned integer containing the initial crc
// table - string containing the 16-bit table corresponding to the generator
// polynomial.
// Returns:
// crc - unsigned integer containing the resulting crc
static PyObject*
_crc16(PyObject* self, PyObject* args)
{
PyObject *obj;
Py_buffer buf;
UINT16 crc;
UINT8* data;
Py_ssize_t dataLen;
UINT16* table;
Py_ssize_t tableLen;
if (!PyArg_ParseTuple(args, INPUT16, &obj, &crc,
&table, &tableLen))
{
return NULL;
}
if (tableLen != 256*2)
{
PyErr_SetString(PyExc_ValueError, "invalid CRC table");
return NULL;
}
GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
data = buf.buf;
dataLen = buf.len;
while (dataLen--)
{
crc = table[*data ^ BYTE1(crc)] ^ (crc << 8);
data++;
}
PyBuffer_Release(&buf);
return PyLong_FromLong((long)crc);
}
//-----------------------------------------------------------------------------
// Compute a 16-bit crc over the input data. The data stream is bit reversed
// during the computation.
// Inputs:
// data - string containing the data
// crc - unsigned integer containing the initial crc
// table - string containing the 16-bit table corresponding to the generator
// polynomial.
// Returns:
// crc - unsigned integer containing the resulting crc
static PyObject*
_crc16r(PyObject* self, PyObject* args)
{
PyObject *obj;
Py_buffer buf;
UINT16 crc;
UINT8* data;
Py_ssize_t dataLen;
UINT16* table;
Py_ssize_t tableLen;
if (!PyArg_ParseTuple(args, INPUT16, &obj, &crc,
&table, &tableLen))
{
return NULL;
}
if (tableLen != 256*2)
{
PyErr_SetString(PyExc_ValueError, "invalid CRC table");
return NULL;
}
GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
data = buf.buf;
dataLen = buf.len;
while (dataLen--)
{
crc = table[*data ^ BYTE0(crc)] ^ (crc >> 8);
data++;
}
PyBuffer_Release(&buf);
return PyLong_FromLong((long)crc);
}
//-----------------------------------------------------------------------------
// Compute a 24-bit crc over the input data.
// Inputs:
// data - string containing the data
// crc - unsigned integer containing the initial crc
// table - string containing the 24-bit table corresponding to the generator
// polynomial.
// Returns:
// crc - unsigned integer containing the resulting crc
static PyObject*
_crc24(PyObject* self, PyObject* args)
{
PyObject *obj;
Py_buffer buf;
UINT32 crc;
UINT8* data;
Py_ssize_t dataLen;
UINT32* table;
Py_ssize_t tableLen;
if (!PyArg_ParseTuple(args, INPUT32, &obj, &crc,
&table, &tableLen))
{
return NULL;
}
if (tableLen != 256*4)
{
PyErr_SetString(PyExc_ValueError, "invalid CRC table");
return NULL;
}
GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
data = buf.buf;
dataLen = buf.len;
while (dataLen--)
{
crc = table[*data ^ BYTE2(crc)] ^ (crc << 8);
data++;
}
PyBuffer_Release(&buf);
return PyLong_FromLong((long)(crc & 0xFFFFFFU));
}
//-----------------------------------------------------------------------------
// Compute a 24-bit crc over the input data. The data stream is bit reversed
// during the computation.
// Inputs:
// data - string containing the data
// crc - unsigned integer containing the initial crc
// table - string containing the 24-bit table corresponding to the generator
// polynomial.
// Returns:
// crc - unsigned integer containing the resulting crc
static PyObject*
_crc24r(PyObject* self, PyObject* args)
{
PyObject *obj;
Py_buffer buf;
UINT32 crc;
UINT8* data;
Py_ssize_t dataLen;
UINT32* table;
Py_ssize_t tableLen;
if (!PyArg_ParseTuple(args, INPUT32, &obj, &crc,
&table, &tableLen))
{
return NULL;
}
if (tableLen != 256*4)
{
PyErr_SetString(PyExc_ValueError, "invalid CRC table");
return NULL;
}
GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
data = buf.buf;
dataLen = buf.len;
crc = crc & 0xFFFFFFU;
while (dataLen--)
{
crc = table[*data ^ BYTE0(crc)] ^ (crc >> 8);
data++;
}
PyBuffer_Release(&buf);
return PyLong_FromLong((long)crc);
}
//-----------------------------------------------------------------------------
// Compute a 32-bit crc over the input data.
// Inputs:
// data - string containing the data
// crc - unsigned integer containing the initial crc
// table - string containing the 32-bit table corresponding to the generator
// polynomial.
// Returns:
// crc - unsigned integer containing the resulting crc
static PyObject*
_crc32(PyObject* self, PyObject* args)
{
PyObject *obj;
Py_buffer buf;
UINT32 crc;
UINT8* data;
Py_ssize_t dataLen;
UINT32* table;
Py_ssize_t tableLen;
if (!PyArg_ParseTuple(args, INPUT32, &obj, &crc,
&table, &tableLen))
{
return NULL;
}
if (tableLen != 256*4)
{
PyErr_SetString(PyExc_ValueError, "invalid CRC table");
return NULL;
}
GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
data = buf.buf;
dataLen = buf.len;
while (dataLen--)
{
crc = table[*data ^ BYTE3(crc)] ^ (crc << 8);
data++;
}
PyBuffer_Release(&buf);
return PyLong_FromUnsignedLong(crc);
}
//-----------------------------------------------------------------------------
// Compute a 32-bit crc over the input data. The data stream is bit reversed
// during the computation.
// Inputs:
// data - string containing the data
// crc - unsigned integer containing the initial crc
// table - string containing the 32-bit table corresponding to the generator
// polynomial.
// Returns:
// crc - unsigned integer containing the resulting crc
static PyObject*
_crc32r(PyObject* self, PyObject* args)
{
PyObject *obj;
Py_buffer buf;
UINT32 crc;
UINT8* data;
Py_ssize_t dataLen;
UINT32* table;
Py_ssize_t tableLen;
if (!PyArg_ParseTuple(args, INPUT32, &obj, &crc,
&table, &tableLen))
{
return NULL;
}
if (tableLen != 256*4)
{
PyErr_SetString(PyExc_ValueError, "invalid CRC table");
return NULL;
}
GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
data = buf.buf;
dataLen = buf.len;
while (dataLen--)
{
crc = table[*data ^ BYTE0(crc)] ^ (crc >> 8);
data++;
}
PyBuffer_Release(&buf);
return PyLong_FromUnsignedLong(crc);
}
//-----------------------------------------------------------------------------
// Compute a 64-bit crc over the input data.
// Inputs:
// data - string containing the data
// crc - unsigned integer containing the initial crc
// table - string containing the 64-bit table corresponding to the generator
// polynomial.
// Returns:
// crc - unsigned integer containing the resulting crc
static PyObject*
_crc64(PyObject* self, PyObject* args)
{
PyObject *obj;
Py_buffer buf;
UINT64 crc;
UINT8* data;
Py_ssize_t dataLen;
UINT64* table;
Py_ssize_t tableLen;
if (!PyArg_ParseTuple(args, INPUT64, &obj, &crc,
&table, &tableLen))
{
return NULL;
}
if (tableLen != 256*8)
{
PyErr_SetString(PyExc_ValueError, "invalid CRC table");
return NULL;
}
GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
data = buf.buf;
dataLen = buf.len;
while (dataLen--)
{
crc = table[*data ^ BYTE7(crc)] ^ (crc << 8);
data++;
}
PyBuffer_Release(&buf);
return PyLong_FromUnsignedLongLong(crc);
}
//-----------------------------------------------------------------------------
// Compute a 64-bit crc over the input data. The data stream is bit reversed
// during the computation.
// Inputs:
// data - string containing the data
// crc - unsigned integer containing the initial crc
// table - string containing the 64-bit table corresponding to the generator
// polynomial.
// Returns:
// crc - unsigned integer containing the resulting crc
static PyObject*
_crc64r(PyObject* self, PyObject* args)
{
PyObject *obj;
Py_buffer buf;
UINT64 crc;
UINT8* data;
Py_ssize_t dataLen;
UINT64* table;
Py_ssize_t tableLen;
if (!PyArg_ParseTuple(args, INPUT64, &obj, &crc,
&table, &tableLen))
{
return NULL;
}
if (tableLen != 256*8)
{
PyErr_SetString(PyExc_ValueError, "invalid CRC table");
return NULL;
}
GET_BUFFER_VIEW_OR_ERROUT(obj, &buf);
data = buf.buf;
dataLen = buf.len;
while (dataLen--)
{
crc = table[*data ^ BYTE0(crc)] ^ (crc >> 8);
data++;
}
PyBuffer_Release(&buf);
return PyLong_FromUnsignedLongLong(crc);
}
//-----------------------------------------------------------------------------
static PyMethodDef methodTable[] = {
{"_crc8", _crc8, METH_VARARGS},
{"_crc8r", _crc8r, METH_VARARGS},
{"_crc16", _crc16, METH_VARARGS},
{"_crc16r", _crc16r, METH_VARARGS},
{"_crc24", _crc24, METH_VARARGS},
{"_crc24r", _crc24r, METH_VARARGS},
{"_crc32", _crc32, METH_VARARGS},
{"_crc32r", _crc32r, METH_VARARGS},
{"_crc64", _crc64, METH_VARARGS},
{"_crc64r", _crc64r, METH_VARARGS},
{NULL, NULL}
};
//-----------------------------------------------------------------------------
static struct PyModuleDef moduleDef = {
PyModuleDef_HEAD_INIT,
"_crcfunext", // name of module
NULL, // module documentation, may be NULL
-1, // size of per-interpreter state of the module,
// or -1 if the module keeps state in global variables.
methodTable
};
//-----------------------------------------------------------------------------
PyMODINIT_FUNC
PyInit__crcfunext(void)
{
if ((sizeof(UINT8) != 1) || (sizeof(UINT16) != 2) ||
(sizeof(UINT32) != 4) || (sizeof(UINT64) != 8))
{
Py_FatalError("crcfunext: One of the data types is invalid");
}
return PyModule_Create(&moduleDef);
}

View File

@@ -0,0 +1,66 @@
from distutils.core import setup
from distutils.extension import Extension
import sys,os
if sys.version_info[0] == 2:
base_dir = 'python2'
elif sys.version_info[0] == 3:
base_dir = 'python3'
setup_dict = dict(
name='crcmod',
version='1.7',
description='CRC Generator',
author='Ray Buvel',
author_email='rlbuvel@gmail.com',
url='http://crcmod.sourceforge.net/',
download_url='http://sourceforge.net/projects/crcmod',
packages=['crcmod'],
package_dir={
'crcmod' : os.path.join(base_dir,'crcmod'),
},
ext_modules=[
Extension('crcmod._crcfunext', [os.path.join(base_dir,'src/_crcfunext.c'), ],
),
],
long_description=open('README').read(),
license="MIT",
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Intended Audience :: Education',
'Intended Audience :: End Users/Desktop',
'Intended Audience :: Information Technology',
'Intended Audience :: Science/Research',
'License :: OSI Approved :: MIT License',
'Operating System :: OS Independent',
'Programming Language :: C',
'Programming Language :: C++',
'Programming Language :: Python',
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.4',
'Programming Language :: Python :: 2.5',
'Programming Language :: Python :: 2.6',
'Programming Language :: Python :: 2.7',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.1',
'Topic :: Communications',
'Topic :: Scientific/Engineering :: Interface Engine/Protocol Translator',
'Topic :: Scientific/Engineering :: Mathematics',
'Topic :: Utilities',
],
)
try:
setup(**setup_dict)
except KeyboardInterrupt:
raise
except:
# If there are any compilation errors or there are no build tools available
# for the extension module, delete the extension module and try to install
# the pure Python version.
del setup_dict['ext_modules']
setup(**setup_dict)

View File

@@ -0,0 +1,49 @@
#-----------------------------------------------------------------------------
# Demonstrate the use of the code generator
from crcmod import Crc
g8 = 0x185
g16 = 0x11021
g24 = 0x15D6DCB
g32 = 0x104C11DB7
def polyFromBits(bits):
p = 0
for n in bits:
p = p | (1 << n)
return p
# The following is from Standard ECMA-182 "Data Interchange on 12,7 mm 48-Track
# Magnetic Tape Cartridges -DLT1 Format-", December 1992.
g64 = polyFromBits([64, 62, 57, 55, 54, 53, 52, 47, 46, 45, 40, 39, 38, 37,
35, 33, 32, 31, 29, 27, 24, 23, 22, 21, 19, 17, 13, 12, 10, 9, 7,
4, 1, 0])
print('Generating examples.c')
out = open('examples.c', 'w')
out.write('''// Define the required data types
typedef unsigned char UINT8;
typedef unsigned short UINT16;
typedef unsigned int UINT32;
typedef unsigned long long UINT64;
''')
Crc(g8, rev=False).generateCode('crc8',out)
Crc(g8, rev=True).generateCode('crc8r',out)
Crc(g16, rev=False).generateCode('crc16',out)
Crc(g16, rev=True).generateCode('crc16r',out)
Crc(g24, rev=False).generateCode('crc24',out)
Crc(g24, rev=True).generateCode('crc24r',out)
Crc(g32, rev=False).generateCode('crc32',out)
Crc(g32, rev=True).generateCode('crc32r',out)
Crc(g64, rev=False).generateCode('crc64',out)
Crc(g64, rev=True).generateCode('crc64r',out)
# Check out the XOR-out feature.
Crc(g16, initCrc=0, rev=True, xorOut=~0).generateCode('crc16x',out)
Crc(g24, initCrc=0, rev=True, xorOut=~0).generateCode('crc24x',out)
Crc(g32, initCrc=0, rev=True, xorOut=~0).generateCode('crc32x',out)
Crc(g64, initCrc=0, rev=True, xorOut=~0).generateCode('crc64x',out)
out.close()
print('Done')

View File

@@ -0,0 +1,6 @@
import unittest
import crcmod.test
unittest.main(module=crcmod.test)