google-crc32c 0.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,2 @@
1
+ include src/google_crc32c_build.py
2
+ include LICENSE
@@ -0,0 +1,67 @@
1
+ Metadata-Version: 2.4
2
+ Name: google-crc32c
3
+ Version: 0.0.0
4
+ Summary: A python wrapper of the C library 'Google CRC32C'
5
+ Home-page: https://github.com/googleapis/python-crc32c
6
+ Author: Google LLC
7
+ Author-email: googleapis-packages@google.com
8
+ License: Apache 2.0
9
+ Platform: Posix
10
+ Platform: MacOS X
11
+ Platform: Windows
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Programming Language :: Python :: 3.14
22
+ Requires-Python: >=3.9
23
+ Description-Content-Type: text/markdown
24
+ License-File: LICENSE
25
+ Requires-Dist: importlib_resources>=1.3; python_version < "3.9" and os_name == "nt"
26
+ Provides-Extra: testing
27
+ Requires-Dist: pytest; extra == "testing"
28
+ Dynamic: license-file
29
+ Dynamic: requires-dist
30
+
31
+ # `google-crc32c`
32
+ ![GA](https://img.shields.io/badge/support-GA-gold.svg) [<img src="https://img.shields.io/pypi/v/google-crc32c.svg">](https://pypi.org/project/google-crc32c) ![Python Versions](https://img.shields.io/pypi/pyversions/google-crc32c)
33
+
34
+ This package wraps the [`google/crc32c`](https://github.com/google/crc32c)
35
+ hardware-based implementation of the CRC32C hashing algorithm. Multiple wheels
36
+ are distributed as well as source. If a wheel is not published for the python
37
+ version and platform you are using, you will need to compile crc32c using a
38
+ C toolchain.
39
+
40
+ # Currently Published Wheels
41
+
42
+ Wheels are currently published for CPython 3.9, 3.10, 3.11, 3.12, 3.13 and 3.14
43
+ for multiple architectures. PyPy 3.9 and 3.10 are also supported for Linux.
44
+ For information on building your own wheels please view [BUILDING.md](BUILDING.md).
45
+
46
+
47
+ ## Linux
48
+
49
+ Wheels are published for the following platforms / architectures:
50
+
51
+ - `manylinux2010` platform, `x86_64` and `1686` architectures
52
+ - `manylinux2014` platform, `aarch64` architecture
53
+
54
+ ### Unsupported Platforms
55
+
56
+ - `manylinux1` platform, `x86_64` architecture support has ended.
57
+
58
+ See https://github.com/pypa/manylinux/issues/994.
59
+
60
+ ## Mac OS
61
+
62
+ Wheels are published for `x86_64` and `arm64` architectures.
63
+
64
+
65
+ ## Windows
66
+
67
+ Wheels are published for the `win_amd64` architecture.
@@ -0,0 +1,37 @@
1
+ # `google-crc32c`
2
+ ![GA](https://img.shields.io/badge/support-GA-gold.svg) [<img src="https://img.shields.io/pypi/v/google-crc32c.svg">](https://pypi.org/project/google-crc32c) ![Python Versions](https://img.shields.io/pypi/pyversions/google-crc32c)
3
+
4
+ This package wraps the [`google/crc32c`](https://github.com/google/crc32c)
5
+ hardware-based implementation of the CRC32C hashing algorithm. Multiple wheels
6
+ are distributed as well as source. If a wheel is not published for the python
7
+ version and platform you are using, you will need to compile crc32c using a
8
+ C toolchain.
9
+
10
+ # Currently Published Wheels
11
+
12
+ Wheels are currently published for CPython 3.9, 3.10, 3.11, 3.12, 3.13 and 3.14
13
+ for multiple architectures. PyPy 3.9 and 3.10 are also supported for Linux.
14
+ For information on building your own wheels please view [BUILDING.md](BUILDING.md).
15
+
16
+
17
+ ## Linux
18
+
19
+ Wheels are published for the following platforms / architectures:
20
+
21
+ - `manylinux2010` platform, `x86_64` and `1686` architectures
22
+ - `manylinux2014` platform, `aarch64` architecture
23
+
24
+ ### Unsupported Platforms
25
+
26
+ - `manylinux1` platform, `x86_64` architecture support has ended.
27
+
28
+ See https://github.com/pypa/manylinux/issues/994.
29
+
30
+ ## Mac OS
31
+
32
+ Wheels are published for `x86_64` and `arm64` architectures.
33
+
34
+
35
+ ## Windows
36
+
37
+ Wheels are published for the `win_amd64` architecture.
@@ -0,0 +1,4 @@
1
+ [build-system]
2
+ # Minimum requirements for the build system to execute.
3
+ requires = ["setuptools", "wheel"] # PEP 508 specifications.
4
+ version = "1.8.0rc1"
@@ -0,0 +1,38 @@
1
+ [metadata]
2
+ name = google-crc32c
3
+ description = A python wrapper of the C library 'Google CRC32C'
4
+ url = https://github.com/googleapis/python-crc32c
5
+ long_description = file: README.md
6
+ long_description_content_type = text/markdown
7
+ author = Google LLC
8
+ author_email = googleapis-packages@google.com
9
+ license = Apache 2.0
10
+ license_files = LICENSE
11
+ platforms = Posix, MacOS X, Windows
12
+ classifiers =
13
+ Development Status :: 4 - Beta
14
+ Intended Audience :: Developers
15
+ Operating System :: OS Independent
16
+ Programming Language :: Python :: 3
17
+ Programming Language :: Python :: 3.9
18
+ Programming Language :: Python :: 3.10
19
+ Programming Language :: Python :: 3.11
20
+ Programming Language :: Python :: 3.12
21
+ Programming Language :: Python :: 3.13
22
+ Programming Language :: Python :: 3.14
23
+
24
+ [options]
25
+ zip_safe = True
26
+ python_requires = >=3.9
27
+
28
+ [options.extras_require]
29
+ testing = pytest
30
+
31
+ [options.package_data]
32
+ google_crc32c =
33
+ py.typed
34
+
35
+ [egg_info]
36
+ tag_build =
37
+ tag_date = 0
38
+
@@ -0,0 +1,134 @@
1
+ # Copyright 2018 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import logging
16
+ import os
17
+ import platform
18
+ import shutil
19
+ import setuptools
20
+ import setuptools.command.build_ext
21
+
22
+ _EXTRA_DLL = "extra-dll"
23
+ _DLL_FILENAME = "crc32c.dll"
24
+
25
+ # Explicit environment variable disables pure-Python fallback
26
+ CRC32C_PURE_PYTHON_EXPLICIT = "CRC32C_PURE_PYTHON" in os.environ
27
+ _FALSE_OPTIONS = ("0", "false", "no", "False", "No", None)
28
+ CRC32C_PURE_PYTHON = os.getenv("CRC32C_PURE_PYTHON") not in _FALSE_OPTIONS
29
+
30
+
31
+ def copy_dll(build_lib):
32
+ install_prefix = os.environ.get("CRC32C_INSTALL_PREFIX")
33
+
34
+ if os.name == "nt" and install_prefix is not None:
35
+ assert os.path.isdir(install_prefix)
36
+
37
+ installed_dll = os.path.join(install_prefix, "bin", _DLL_FILENAME)
38
+ assert os.path.isfile(installed_dll)
39
+
40
+ lib_dlls = os.path.join(build_lib, "google_crc32c", _EXTRA_DLL)
41
+ os.makedirs(lib_dlls, exist_ok=True)
42
+ relocated_dll = os.path.join(lib_dlls, _DLL_FILENAME)
43
+
44
+ shutil.copyfile(installed_dll, relocated_dll)
45
+ assert os.path.isfile(relocated_dll)
46
+
47
+
48
+ class BuildExtWithDLL(setuptools.command.build_ext.build_ext):
49
+ def run(self):
50
+ copy_dll(self.build_lib)
51
+ result = setuptools.command.build_ext.build_ext.run(self)
52
+ return result
53
+
54
+
55
+ def build_pure_python():
56
+ setuptools.setup(
57
+ packages=["google_crc32c"],
58
+ package_dir={"": "src"},
59
+ ext_modules=[],
60
+ )
61
+
62
+
63
+ def build_c_extension():
64
+ install_prefix = os.getenv("CRC32C_INSTALL_PREFIX")
65
+ if install_prefix is not None:
66
+ install_prefix = os.path.normcase(install_prefix)
67
+ print(f"#### using local install of 'crc32c': {install_prefix!r}")
68
+ #assert os.path.isdir(install_prefix)
69
+ install_prefix = os.path.realpath(install_prefix)
70
+ include_dirs = [os.path.join(install_prefix, "include")]
71
+ library_dirs = [os.path.join(install_prefix, "lib")]
72
+
73
+ if platform.system() == "Linux":
74
+ library_dirs.append(os.path.join(install_prefix, "lib64"))
75
+
76
+ if os.name == "nt":
77
+ library_dirs.append(os.path.join(install_prefix, "bin"))
78
+ kwargs = {
79
+ "include_dirs": include_dirs,
80
+ "library_dirs": library_dirs,
81
+ }
82
+ else:
83
+ runtime_library_dirs = library_dirs[:]
84
+ kwargs = {
85
+ "include_dirs": include_dirs,
86
+ "library_dirs": library_dirs,
87
+ "runtime_library_dirs": runtime_library_dirs,
88
+ }
89
+ else:
90
+ print("#### using global install of 'crc32c'")
91
+ kwargs = {}
92
+
93
+ module_path = os.path.join("src", "google_crc32c", "_crc32c.c")
94
+ sources=[os.path.normcase(module_path)]
95
+ print(f"##### sources: {sources}")
96
+ print(f"##### module kwargs: {kwargs}")
97
+ module = setuptools.Extension(
98
+ "google_crc32c._crc32c",
99
+ sources=sources,
100
+ libraries=["crc32c"],
101
+ **kwargs
102
+ )
103
+
104
+ setuptools.setup(
105
+ packages=["google_crc32c"],
106
+ package_dir={"": "src"},
107
+ ext_modules=[module],
108
+ cmdclass={"build_ext": BuildExtWithDLL},
109
+ install_requires=["importlib_resources>=1.3 ; python_version < '3.9' and os_name == 'nt'"],
110
+ )
111
+
112
+
113
+ if CRC32C_PURE_PYTHON:
114
+ build_pure_python()
115
+ else:
116
+ try:
117
+ build_c_extension()
118
+ except SystemExit:
119
+ if CRC32C_PURE_PYTHON_EXPLICIT:
120
+ # If build / install fails, it is likely a compilation error with
121
+ # the C extension: advise user how to enable the pure-Python
122
+ # build.
123
+ logging.error(
124
+ "Compiling the C Extension for the crc32c library failed. "
125
+ "To enable building / installing a pure-Python-only version, "
126
+ "set 'CRC32C_PURE_PYTHON=1' in the environment."
127
+ )
128
+ raise
129
+
130
+ logging.info(
131
+ "Compiling the C Extension for the crc32c library failed. "
132
+ "Falling back to pure Python build."
133
+ )
134
+ build_pure_python()
@@ -0,0 +1,38 @@
1
+ # Copyright 2018 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import os
16
+
17
+
18
+ def modify_path():
19
+ """Modify the module search path."""
20
+ # Only modify path on Windows.
21
+ if os.name != "nt":
22
+ return
23
+
24
+ path = os.environ.get("PATH")
25
+ if path is None:
26
+ return
27
+
28
+ try:
29
+ from importlib.resources import files as _resources_files
30
+
31
+ extra_dll_dir = str(_resources_files("google_crc32c") / "extra-dll")
32
+ if os.path.isdir(extra_dll_dir):
33
+ # Python 3.8+ uses add_dll_directory.
34
+ os.add_dll_directory(extra_dll_dir)
35
+ except ImportError:
36
+ pass
37
+
38
+ modify_path()
@@ -0,0 +1,37 @@
1
+ # Copyright 2018 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import warnings
16
+
17
+ _SLOW_CRC32C_WARNING = (
18
+ "As the c extension couldn't be imported, `google-crc32c` is using a "
19
+ "pure python implementation that is significantly slower. If possible, "
20
+ "please configure a c build environment and compile the extension"
21
+ )
22
+
23
+ # Default to C exstension Implementation, falling back to pure python.
24
+ try:
25
+ from google_crc32c import cext as impl
26
+ implementation = "c"
27
+ except ImportError as exc:
28
+ from google_crc32c import python as impl # type: ignore
29
+ warnings.warn(_SLOW_CRC32C_WARNING, RuntimeWarning)
30
+ implementation = "python"
31
+
32
+ extend = impl.extend
33
+ value = impl.value
34
+
35
+ Checksum = impl.Checksum
36
+
37
+ __all__ = ["extend", "value", "Checksum", "implementation"]
@@ -0,0 +1,87 @@
1
+ # Copyright 2020 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import struct
16
+
17
+
18
+ class CommonChecksum(object):
19
+ """Hashlib-alike helper for CRC32C operations.
20
+
21
+ This class should not be used directly and requires an update implementation.
22
+
23
+ Args:
24
+ initial_value (Optional[bytes]): the initial chunk of data from
25
+ which the CRC32C checksum is computed. Defaults to b''.
26
+ """
27
+ __slots__ = ()
28
+
29
+ def __init__(self, initial_value=b""):
30
+ self._crc = 0
31
+ if initial_value != b"":
32
+ self.update(initial_value)
33
+
34
+ def update(self, data):
35
+ """Update the checksum with a new chunk of data.
36
+
37
+ Args:
38
+ chunk (Optional[bytes]): a chunk of data used to extend
39
+ the CRC32C checksum.
40
+ """
41
+ raise NotImplementedError()
42
+
43
+ def digest(self):
44
+ """Big-endian order, per RFC 4960.
45
+
46
+ See: https://cloud.google.com/storage/docs/json_api/v1/objects#crc32c
47
+
48
+ Returns:
49
+ bytes: An eight-byte digest string.
50
+ """
51
+ return struct.pack(">L", self._crc)
52
+
53
+ def hexdigest(self):
54
+ """Like :meth:`digest` except returns as a bytestring of double length.
55
+
56
+ Returns
57
+ bytes: A sixteen byte digest string, contaiing only hex digits.
58
+ """
59
+ return "{:08x}".format(self._crc).encode("ascii")
60
+
61
+ def copy(self):
62
+ """Create another checksum with the same CRC32C value.
63
+
64
+ Returns:
65
+ Checksum: the new instance.
66
+ """
67
+ clone = self.__class__()
68
+ clone._crc = self._crc
69
+ return clone
70
+
71
+ def consume(self, stream, chunksize):
72
+ """Consume chunks from a stream, extending our CRC32 checksum.
73
+
74
+ Args:
75
+ stream (BinaryIO): the stream to consume.
76
+ chunksize (int): the size of the read to perform
77
+
78
+ Returns:
79
+ Generator[bytes, None, None]: Iterable of the chunks read from the
80
+ stream.
81
+ """
82
+ while True:
83
+ chunk = stream.read(chunksize)
84
+ if not chunk:
85
+ break
86
+ self.update(chunk)
87
+ yield chunk
@@ -0,0 +1,62 @@
1
+ #define PY_SSIZE_T_CLEAN
2
+ #include <Python.h>
3
+ #include <crc32c/crc32c.h>
4
+
5
+
6
+ static PyObject *
7
+ _crc32c_extend(PyObject *self, PyObject *args)
8
+ {
9
+ unsigned long crc_input;
10
+ uint32_t crc;
11
+ const char *chunk;
12
+ Py_ssize_t length;
13
+
14
+ if (!PyArg_ParseTuple(args, "ky#", &crc_input, &chunk, &length))
15
+ return NULL;
16
+
17
+ crc = crc32c_extend((uint32_t)crc_input, (const uint8_t*)chunk, length);
18
+
19
+ return PyLong_FromUnsignedLong(crc);
20
+ }
21
+
22
+
23
+ static PyObject *
24
+ _crc32c_value(PyObject *self, PyObject *args)
25
+ {
26
+ uint32_t crc;
27
+ const char *chunk;
28
+ Py_ssize_t length;
29
+
30
+ if (!PyArg_ParseTuple(args, "y#", &chunk, &length))
31
+ return NULL;
32
+
33
+ crc = crc32c_value((const uint8_t*)chunk, length);
34
+
35
+ return PyLong_FromUnsignedLong(crc);
36
+ }
37
+
38
+
39
+ static PyMethodDef Crc32cMethods[] = {
40
+ {"extend", _crc32c_extend, METH_VARARGS,
41
+ "Return an updated CRC32C checksum."},
42
+ {"value", _crc32c_value, METH_VARARGS,
43
+ "Return an initial CRC32C checksum."},
44
+ {NULL, NULL, 0, NULL} /* Sentinel */
45
+ };
46
+
47
+
48
+ static struct PyModuleDef crc32cmodule = {
49
+ PyModuleDef_HEAD_INIT,
50
+ "_crc32c", /* name of module */
51
+ NULL, /* module documentation, may be NULL */
52
+ -1, /* size of per-interpreter state of the module,
53
+ or -1 if the module keeps state in global variables. */
54
+ Crc32cMethods
55
+ };
56
+
57
+
58
+ PyMODINIT_FUNC
59
+ PyInit__crc32c(void)
60
+ {
61
+ return PyModule_Create(&crc32cmodule);
62
+ }
@@ -0,0 +1,45 @@
1
+ # Copyright 2020 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import struct
16
+
17
+ # NOTE: ``__config__`` **must** be the first import because it (may)
18
+ # modify the search path used to locate shared libraries.
19
+ import google_crc32c.__config__ # type: ignore
20
+ from google_crc32c._crc32c import extend # type: ignore
21
+ from google_crc32c._crc32c import value # type: ignore
22
+ from google_crc32c._checksum import CommonChecksum
23
+
24
+
25
+ class Checksum(CommonChecksum):
26
+ """Hashlib-alike helper for CRC32C operations.
27
+
28
+ Args:
29
+ initial_value (Optional[bytes]): the initial chunk of data from
30
+ which the CRC32C checksum is computed. Defaults to b''.
31
+ """
32
+
33
+ __slots__ = ("_crc",)
34
+
35
+ def __init__(self, initial_value=b""):
36
+ self._crc = value(initial_value)
37
+
38
+ def update(self, chunk):
39
+ """Update the checksum with a new chunk of data.
40
+
41
+ Args:
42
+ chunk (Optional[bytes]): a chunk of data used to extend
43
+ the CRC32C checksum.
44
+ """
45
+ self._crc = extend(self._crc, chunk)
@@ -0,0 +1,2 @@
1
+ # Marker file for PEP 561.
2
+ # The google_crc32c package uses inline types.
@@ -0,0 +1,124 @@
1
+ # Copyright 2020 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ import array
16
+ import struct
17
+
18
+ from google_crc32c._checksum import CommonChecksum
19
+
20
+
21
+ def extend(crc, chunk):
22
+ """Update an existing CRC checksum with new chunk of data.
23
+
24
+ Args
25
+ crc (int): An existing CRC check sum.
26
+ chunk (Union[bytes, List[int], Tuple[int]]): A new chunk of data.
27
+ Intended to be a byte string or similar.
28
+
29
+ Returns
30
+ int: New CRC checksum computed by extending existing CRC
31
+ with ``chunk``.
32
+ """
33
+ c = Checksum()
34
+ c._crc = crc
35
+ c.update(chunk)
36
+ return c._crc
37
+
38
+
39
+ def value(chunk):
40
+ """Compute a CRC checksum for a chunk of data.
41
+
42
+ Args
43
+ chunk (Union[bytes, List[int], Tuple[int]]): A new chunk of data.
44
+ Intended to be a byte string or similar.
45
+
46
+ Returns
47
+ int: New CRC checksum computed for ``chunk``.
48
+ """
49
+ c = Checksum()
50
+ c.update(chunk)
51
+ return c._crc
52
+
53
+
54
+ class Checksum(CommonChecksum):
55
+ """Hashlib-alike helper for CRC32C operations.
56
+
57
+ Args:
58
+ initial_value (Optional[bytes]): the initial chunk of data from
59
+ which the CRC32C checksum is computed. Defaults to b''.
60
+ """
61
+
62
+ def __init__(self, initial_value=b""):
63
+ self._crc = 0
64
+ if initial_value != b"":
65
+ self.update(initial_value)
66
+
67
+ def update(self, data):
68
+ """Update the checksum with a new chunk of data.
69
+
70
+ Args:
71
+ chunk (Optional[bytes]): a chunk of data used to extend
72
+ the CRC32C checksum.
73
+ """
74
+ if type(data) != array.array or data.itemsize != 1:
75
+ buffer = array.array("B", data)
76
+ else:
77
+ buffer = data
78
+ self._crc = self._crc ^ 0xFFFFFFFF
79
+ for b in buffer:
80
+ table_poly = _TABLE[(b ^ self._crc) & 0xFF]
81
+ self._crc = table_poly ^ ((self._crc >> 8) & 0xFFFFFFFF)
82
+ self._crc = self._crc ^ 0xFFFFFFFF
83
+
84
+
85
+ # fmt:off
86
+ _TABLE = [
87
+ 0x00000000, 0xf26b8303, 0xe13b70f7, 0x1350f3f4, 0xc79a971f, 0x35f1141c, 0x26a1e7e8,
88
+ 0xd4ca64eb, 0x8ad958cf, 0x78b2dbcc, 0x6be22838, 0x9989ab3b, 0x4d43cfd0, 0xbf284cd3,
89
+ 0xac78bf27, 0x5e133c24, 0x105ec76f, 0xe235446c, 0xf165b798, 0x30e349b, 0xd7c45070,
90
+ 0x25afd373, 0x36ff2087, 0xc494a384, 0x9a879fa0, 0x68ec1ca3, 0x7bbcef57, 0x89d76c54,
91
+ 0x5d1d08bf, 0xaf768bbc, 0xbc267848, 0x4e4dfb4b, 0x20bd8ede, 0xd2d60ddd, 0xc186fe29,
92
+ 0x33ed7d2a, 0xe72719c1, 0x154c9ac2, 0x61c6936, 0xf477ea35, 0xaa64d611, 0x580f5512,
93
+ 0x4b5fa6e6, 0xb93425e5, 0x6dfe410e, 0x9f95c20d, 0x8cc531f9, 0x7eaeb2fa, 0x30e349b1,
94
+ 0xc288cab2, 0xd1d83946, 0x23b3ba45, 0xf779deae, 0x5125dad, 0x1642ae59, 0xe4292d5a,
95
+ 0xba3a117e, 0x4851927d, 0x5b016189, 0xa96ae28a, 0x7da08661, 0x8fcb0562, 0x9c9bf696,
96
+ 0x6ef07595, 0x417b1dbc, 0xb3109ebf, 0xa0406d4b, 0x522bee48, 0x86e18aa3, 0x748a09a0,
97
+ 0x67dafa54, 0x95b17957, 0xcba24573, 0x39c9c670, 0x2a993584, 0xd8f2b687, 0xc38d26c,
98
+ 0xfe53516f, 0xed03a29b, 0x1f682198, 0x5125dad3, 0xa34e59d0, 0xb01eaa24, 0x42752927,
99
+ 0x96bf4dcc, 0x64d4cecf, 0x77843d3b, 0x85efbe38, 0xdbfc821c, 0x2997011f, 0x3ac7f2eb,
100
+ 0xc8ac71e8, 0x1c661503, 0xee0d9600, 0xfd5d65f4, 0xf36e6f7, 0x61c69362, 0x93ad1061,
101
+ 0x80fde395, 0x72966096, 0xa65c047d, 0x5437877e, 0x4767748a, 0xb50cf789, 0xeb1fcbad,
102
+ 0x197448ae, 0xa24bb5a, 0xf84f3859, 0x2c855cb2, 0xdeeedfb1, 0xcdbe2c45, 0x3fd5af46,
103
+ 0x7198540d, 0x83f3d70e, 0x90a324fa, 0x62c8a7f9, 0xb602c312, 0x44694011, 0x5739b3e5,
104
+ 0xa55230e6, 0xfb410cc2, 0x92a8fc1, 0x1a7a7c35, 0xe811ff36, 0x3cdb9bdd, 0xceb018de,
105
+ 0xdde0eb2a, 0x2f8b6829, 0x82f63b78, 0x709db87b, 0x63cd4b8f, 0x91a6c88c, 0x456cac67,
106
+ 0xb7072f64, 0xa457dc90, 0x563c5f93, 0x82f63b7, 0xfa44e0b4, 0xe9141340, 0x1b7f9043,
107
+ 0xcfb5f4a8, 0x3dde77ab, 0x2e8e845f, 0xdce5075c, 0x92a8fc17, 0x60c37f14, 0x73938ce0,
108
+ 0x81f80fe3, 0x55326b08, 0xa759e80b, 0xb4091bff, 0x466298fc, 0x1871a4d8, 0xea1a27db,
109
+ 0xf94ad42f, 0xb21572c, 0xdfeb33c7, 0x2d80b0c4, 0x3ed04330, 0xccbbc033, 0xa24bb5a6,
110
+ 0x502036a5, 0x4370c551, 0xb11b4652, 0x65d122b9, 0x97baa1ba, 0x84ea524e, 0x7681d14d,
111
+ 0x2892ed69, 0xdaf96e6a, 0xc9a99d9e, 0x3bc21e9d, 0xef087a76, 0x1d63f975, 0xe330a81,
112
+ 0xfc588982, 0xb21572c9, 0x407ef1ca, 0x532e023e, 0xa145813d, 0x758fe5d6, 0x87e466d5,
113
+ 0x94b49521, 0x66df1622, 0x38cc2a06, 0xcaa7a905, 0xd9f75af1, 0x2b9cd9f2, 0xff56bd19,
114
+ 0xd3d3e1a, 0x1e6dcdee, 0xec064eed, 0xc38d26c4, 0x31e6a5c7, 0x22b65633, 0xd0ddd530,
115
+ 0x417b1db, 0xf67c32d8, 0xe52cc12c, 0x1747422f, 0x49547e0b, 0xbb3ffd08, 0xa86f0efc,
116
+ 0x5a048dff, 0x8ecee914, 0x7ca56a17, 0x6ff599e3, 0x9d9e1ae0, 0xd3d3e1ab, 0x21b862a8,
117
+ 0x32e8915c, 0xc083125f, 0x144976b4, 0xe622f5b7, 0xf5720643, 0x7198540, 0x590ab964,
118
+ 0xab613a67, 0xb831c993, 0x4a5a4a90, 0x9e902e7b, 0x6cfbad78, 0x7fab5e8c, 0x8dc0dd8f,
119
+ 0xe330a81a, 0x115b2b19, 0x20bd8ed, 0xf0605bee, 0x24aa3f05, 0xd6c1bc06, 0xc5914ff2,
120
+ 0x37faccf1, 0x69e9f0d5, 0x9b8273d6, 0x88d28022, 0x7ab90321, 0xae7367ca, 0x5c18e4c9,
121
+ 0x4f48173d, 0xbd23943e, 0xf36e6f75, 0x105ec76, 0x12551f82, 0xe03e9c81, 0x34f4f86a,
122
+ 0xc69f7b69, 0xd5cf889d, 0x27a40b9e, 0x79b737ba, 0x8bdcb4b9, 0x988c474d, 0x6ae7c44e,
123
+ 0xbe2da0a5, 0x4c4623a6, 0x5f16d052, 0xad7d5351
124
+ ]
@@ -0,0 +1,67 @@
1
+ Metadata-Version: 2.4
2
+ Name: google-crc32c
3
+ Version: 0.0.0
4
+ Summary: A python wrapper of the C library 'Google CRC32C'
5
+ Home-page: https://github.com/googleapis/python-crc32c
6
+ Author: Google LLC
7
+ Author-email: googleapis-packages@google.com
8
+ License: Apache 2.0
9
+ Platform: Posix
10
+ Platform: MacOS X
11
+ Platform: Windows
12
+ Classifier: Development Status :: 4 - Beta
13
+ Classifier: Intended Audience :: Developers
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3
16
+ Classifier: Programming Language :: Python :: 3.9
17
+ Classifier: Programming Language :: Python :: 3.10
18
+ Classifier: Programming Language :: Python :: 3.11
19
+ Classifier: Programming Language :: Python :: 3.12
20
+ Classifier: Programming Language :: Python :: 3.13
21
+ Classifier: Programming Language :: Python :: 3.14
22
+ Requires-Python: >=3.9
23
+ Description-Content-Type: text/markdown
24
+ License-File: LICENSE
25
+ Requires-Dist: importlib_resources>=1.3; python_version < "3.9" and os_name == "nt"
26
+ Provides-Extra: testing
27
+ Requires-Dist: pytest; extra == "testing"
28
+ Dynamic: license-file
29
+ Dynamic: requires-dist
30
+
31
+ # `google-crc32c`
32
+ ![GA](https://img.shields.io/badge/support-GA-gold.svg) [<img src="https://img.shields.io/pypi/v/google-crc32c.svg">](https://pypi.org/project/google-crc32c) ![Python Versions](https://img.shields.io/pypi/pyversions/google-crc32c)
33
+
34
+ This package wraps the [`google/crc32c`](https://github.com/google/crc32c)
35
+ hardware-based implementation of the CRC32C hashing algorithm. Multiple wheels
36
+ are distributed as well as source. If a wheel is not published for the python
37
+ version and platform you are using, you will need to compile crc32c using a
38
+ C toolchain.
39
+
40
+ # Currently Published Wheels
41
+
42
+ Wheels are currently published for CPython 3.9, 3.10, 3.11, 3.12, 3.13 and 3.14
43
+ for multiple architectures. PyPy 3.9 and 3.10 are also supported for Linux.
44
+ For information on building your own wheels please view [BUILDING.md](BUILDING.md).
45
+
46
+
47
+ ## Linux
48
+
49
+ Wheels are published for the following platforms / architectures:
50
+
51
+ - `manylinux2010` platform, `x86_64` and `1686` architectures
52
+ - `manylinux2014` platform, `aarch64` architecture
53
+
54
+ ### Unsupported Platforms
55
+
56
+ - `manylinux1` platform, `x86_64` architecture support has ended.
57
+
58
+ See https://github.com/pypa/manylinux/issues/994.
59
+
60
+ ## Mac OS
61
+
62
+ Wheels are published for `x86_64` and `arm64` architectures.
63
+
64
+
65
+ ## Windows
66
+
67
+ Wheels are published for the `win_amd64` architecture.
@@ -0,0 +1,20 @@
1
+ LICENSE
2
+ MANIFEST.in
3
+ README.md
4
+ pyproject.toml
5
+ setup.cfg
6
+ setup.py
7
+ src/google_crc32c/__config__.py
8
+ src/google_crc32c/__init__.py
9
+ src/google_crc32c/_checksum.py
10
+ src/google_crc32c/_crc32c.c
11
+ src/google_crc32c/cext.py
12
+ src/google_crc32c/py.typed
13
+ src/google_crc32c/python.py
14
+ src/google_crc32c.egg-info/PKG-INFO
15
+ src/google_crc32c.egg-info/SOURCES.txt
16
+ src/google_crc32c.egg-info/dependency_links.txt
17
+ src/google_crc32c.egg-info/requires.txt
18
+ src/google_crc32c.egg-info/top_level.txt
19
+ src/google_crc32c.egg-info/zip-safe
20
+ tests/test___init__.py
@@ -0,0 +1,6 @@
1
+
2
+ [:python_version < "3.9" and os_name == "nt"]
3
+ importlib_resources>=1.3
4
+
5
+ [testing]
6
+ pytest
@@ -0,0 +1 @@
1
+ google_crc32c
@@ -0,0 +1,286 @@
1
+ # Copyright 2018 Google LLC
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # https://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ import functools
15
+ import itertools
16
+ from unittest import mock
17
+
18
+ import pytest # type: ignore
19
+
20
+ import google_crc32c
21
+
22
+ EMPTY = b""
23
+ EMPTY_CRC = 0x00000000
24
+
25
+ # From: https://tools.ietf.org/html/rfc3720#appendix-B.4
26
+ #
27
+ # 32 bytes of zeroes:
28
+ #
29
+ # Byte: 0 1 2 3
30
+ #
31
+ # 0: 00 00 00 00
32
+ # ...
33
+ # 28: 00 00 00 00
34
+ #
35
+ # CRC: aa 36 91 8a
36
+
37
+ ALL_ZEROS = b"\x00" * 32
38
+ ALL_ZEROS_CRC = 0x8A9136AA
39
+
40
+ # 32 bytes of ones:
41
+ #
42
+ # Byte: 0 1 2 3
43
+ #
44
+ # 0: ff ff ff ff
45
+ # ...
46
+ # 28: ff ff ff ff
47
+ #
48
+ # CRC: 43 ab a8 62
49
+
50
+ ALL_ONES = b"\xff" * 32
51
+ ALL_ONES_CRC = 0x62A8AB43
52
+
53
+ #
54
+ # 32 bytes of incrementing 00..1f:
55
+ #
56
+ # Byte: 0 1 2 3
57
+ #
58
+ # 0: 00 01 02 03
59
+ # ...
60
+ # 28: 1c 1d 1e 1f
61
+ #
62
+ # CRC: 4e 79 dd 46
63
+
64
+ INCREMENTING = bytes(range(32))
65
+ INCREMENTING_CRC = 0x46DD794E
66
+
67
+ #
68
+ # 32 bytes of decrementing 1f..00:
69
+ #
70
+ # Byte: 0 1 2 3
71
+ #
72
+ # 0: 1f 1e 1d 1c
73
+ # ...
74
+ # 28: 03 02 01 00
75
+ #
76
+ # CRC: 5c db 3f 11
77
+
78
+ DECREMENTING = bytes(reversed(range(32)))
79
+ DECREMENTING_CRC = 0x113FDB5C
80
+
81
+ #
82
+ # An iSCSI - SCSI Read (10) Command PDU
83
+ #
84
+ # Byte: 0 1 2 3
85
+ #
86
+ # 0: 01 c0 00 00
87
+ # 4: 00 00 00 00
88
+ # 8: 00 00 00 00
89
+ # 12: 00 00 00 00
90
+ # 16: 14 00 00 00
91
+ # 20: 00 00 04 00
92
+ # 24: 00 00 00 14
93
+ # 28: 00 00 00 18
94
+ # 32: 28 00 00 00
95
+ # 36: 00 00 00 00
96
+ # 40: 02 00 00 00
97
+ # 44: 00 00 00 00
98
+ #
99
+ # CRC: 56 3a 96 d9
100
+
101
+ ISCSI_SCSI_READ_10_COMMAND_PDU = [
102
+ 0x01,
103
+ 0xC0,
104
+ 0x00,
105
+ 0x00,
106
+ 0x00,
107
+ 0x00,
108
+ 0x00,
109
+ 0x00,
110
+ 0x00,
111
+ 0x00,
112
+ 0x00,
113
+ 0x00,
114
+ 0x00,
115
+ 0x00,
116
+ 0x00,
117
+ 0x00,
118
+ 0x14,
119
+ 0x00,
120
+ 0x00,
121
+ 0x00,
122
+ 0x00,
123
+ 0x00,
124
+ 0x04,
125
+ 0x00,
126
+ 0x00,
127
+ 0x00,
128
+ 0x00,
129
+ 0x14,
130
+ 0x00,
131
+ 0x00,
132
+ 0x00,
133
+ 0x18,
134
+ 0x28,
135
+ 0x00,
136
+ 0x00,
137
+ 0x00,
138
+ 0x00,
139
+ 0x00,
140
+ 0x00,
141
+ 0x00,
142
+ 0x02,
143
+ 0x00,
144
+ 0x00,
145
+ 0x00,
146
+ 0x00,
147
+ 0x00,
148
+ 0x00,
149
+ 0x00,
150
+ ]
151
+ ISCSI_LENGTH = len(ISCSI_SCSI_READ_10_COMMAND_PDU)
152
+ ISCSI_BYTES = bytes(ISCSI_SCSI_READ_10_COMMAND_PDU)
153
+ ISCSI_CRC = 0xD9963A56
154
+
155
+
156
+ def iscsi_chunks(chunksize):
157
+ for index in itertools.islice(range(ISCSI_LENGTH), 0, None, chunksize):
158
+ yield ISCSI_SCSI_READ_10_COMMAND_PDU[index : index + chunksize]
159
+
160
+
161
+ _EXPECTED = [
162
+ (EMPTY, EMPTY_CRC),
163
+ (ALL_ZEROS, ALL_ZEROS_CRC),
164
+ (ALL_ONES, ALL_ONES_CRC),
165
+ (INCREMENTING, INCREMENTING_CRC),
166
+ (DECREMENTING, DECREMENTING_CRC),
167
+ (ISCSI_SCSI_READ_10_COMMAND_PDU, ISCSI_CRC),
168
+ (ISCSI_BYTES, ISCSI_CRC),
169
+ ]
170
+
171
+
172
+ def test_extend_w_empty_chunk():
173
+ crc = 123
174
+ assert google_crc32c.extend(crc, b"") == crc
175
+
176
+
177
+ def test_extend_w_multiple_chunks():
178
+ crc = 0
179
+
180
+ for chunk in iscsi_chunks(7):
181
+ chunk_bytes = bytes(chunk)
182
+ crc = google_crc32c.extend(crc, chunk_bytes)
183
+
184
+ assert crc == ISCSI_CRC
185
+
186
+
187
+ def test_extend_w_reduce():
188
+ chunks = [bytes(chunk) for chunk in iscsi_chunks(3)]
189
+ assert functools.reduce(google_crc32c.extend, chunks, 0) == ISCSI_CRC
190
+
191
+
192
+ @pytest.mark.parametrize("chunk, expected", _EXPECTED)
193
+ def test_value(chunk, expected):
194
+ assert google_crc32c.value(bytes(chunk)) == expected
195
+
196
+
197
+ def pytest_generate_tests(metafunc):
198
+ if "_crc32c" in metafunc.fixturenames:
199
+ metafunc.parametrize("_crc32c", ["python", "cext"], indirect=True)
200
+
201
+ @pytest.fixture
202
+ def _crc32c(request):
203
+ if request.param == "python":
204
+ from google_crc32c import python
205
+ return python
206
+ elif request.param == "cext":
207
+ from google_crc32c import cext
208
+ return cext
209
+ else:
210
+ raise ValueError("invalid internal test config")
211
+
212
+
213
+ class TestChecksum(object):
214
+ @staticmethod
215
+ def test_ctor_defaults(_crc32c):
216
+ helper = google_crc32c.Checksum()
217
+ assert helper._crc == 0
218
+
219
+ @staticmethod
220
+ def test_ctor_explicit(_crc32c):
221
+ chunk = b"DEADBEEF"
222
+ helper = google_crc32c.Checksum(chunk)
223
+ assert helper._crc == google_crc32c.value(chunk)
224
+
225
+ @staticmethod
226
+ def test_update(_crc32c):
227
+ chunk = b"DEADBEEF"
228
+ helper = google_crc32c.Checksum()
229
+ helper.update(chunk)
230
+ assert helper._crc == google_crc32c.value(chunk)
231
+
232
+ @staticmethod
233
+ def test_update_w_multiple_chunks(_crc32c):
234
+ helper = google_crc32c.Checksum()
235
+
236
+ for index in itertools.islice(range(ISCSI_LENGTH), 0, None, 7):
237
+ chunk = ISCSI_SCSI_READ_10_COMMAND_PDU[index : index + 7]
238
+ helper.update(bytes(chunk))
239
+
240
+ assert helper._crc == ISCSI_CRC
241
+
242
+ @staticmethod
243
+ def test_digest_zero(_crc32c):
244
+ helper = google_crc32c.Checksum()
245
+ assert helper.digest() == b"\x00" * 4
246
+
247
+ @staticmethod
248
+ def test_digest_nonzero(_crc32c):
249
+ helper = google_crc32c.Checksum()
250
+ helper._crc = 0x01020304
251
+ assert helper.digest() == b"\x01\x02\x03\x04"
252
+
253
+ @staticmethod
254
+ def test_hexdigest_zero(_crc32c):
255
+ helper = google_crc32c.Checksum()
256
+ assert helper.hexdigest() == b"00" * 4
257
+
258
+ @staticmethod
259
+ def test_hexdigest_nonzero(_crc32c):
260
+ helper = google_crc32c.Checksum()
261
+ helper._crc = 0x091A3B2C
262
+ assert helper.hexdigest() == b"091a3b2c"
263
+
264
+ @staticmethod
265
+ def test_copy(_crc32c):
266
+ chunk = b"DEADBEEF"
267
+ helper = google_crc32c.Checksum(chunk)
268
+ clone = helper.copy()
269
+ before = helper._crc
270
+ helper.update(b"FACEDACE")
271
+ assert clone._crc == before
272
+
273
+ @staticmethod
274
+ @pytest.mark.parametrize("chunksize", [1, 3, 5, 7, 11, 13, ISCSI_LENGTH])
275
+ def test_consume_stream(_crc32c, chunksize):
276
+ helper = google_crc32c.Checksum()
277
+ expected = [bytes(chunk) for chunk in iscsi_chunks(chunksize)]
278
+ stream = mock.Mock(spec=["read"])
279
+ stream.read.side_effect = expected + [b""]
280
+
281
+ found = list(helper.consume(stream, chunksize))
282
+
283
+ assert helper._crc == ISCSI_CRC
284
+ assert found == expected
285
+ for call in stream.read.call_args_list:
286
+ assert call == mock.call(chunksize)