CRC32 C 或 C++ 实现
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/302914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CRC32 C or C++ implementation
提问by twk
I'm looking for an implementation of CRC32 in C or C++ that is explicitly licensed as being no cost or public domain. The implementation hereseems nice, but the only thing it says about the license is "source code", which isn't good enough. I'd prefer non LGPL so I don't have to fool around with a DLL (my app is closed source). I saw the adler32 implementation in zlib, but I'm checking small chunks of data, which adler is not good for.
我正在寻找 C 或 C++ 中 CRC32 的实现,该实现被明确许可为免费或公共领域。这里的实现看起来不错,但它关于许可证的唯一说明是“源代码”,这还不够好。我更喜欢非 LGPL,所以我不必在 DLL 上胡闹(我的应用程序是封闭源代码)。我在 zlib 中看到了 adler32 实现,但我正在检查小块数据,adler 不适合这些数据。
采纳答案by Zan Lynx
Use the Boost C++ libraries. There is a CRCincluded there and the licenseis good.
使用Boost C++ 库。那里包含一个CRC,许可证很好。
回答by cjm
The SNIPPETS C Source Code Archivehas a CRC32 implementationthat is freely usable:
该SNIPPETS C源代码归档具有CRC32实现是可自由使用:
/* Copyright (C) 1986 Gary S. Brown. You may use this program, or
code or tables extracted from it, as desired without restriction.*/
(Unfortunately, c.snippets.org seems to have died. Fortunately, the Wayback Machinehas it archived.)
(不幸的是,c.snippets.org 似乎已经死了。幸运的是,Wayback Machine已将其存档。)
In order to be able to compile the code, you'll need to add typedefs for BYTE
as an unsigned 8-bit integer and DWORD
as an unsigned 32-bit integer, along with the header files crc.h& sniptype.h.
为了能够编译代码,您需要添加 typedefBYTE
作为无符号 8 位整数和DWORD
无符号 32 位整数,以及头文件crc.h和sniptype.h。
The only critical item in the header is this macro (which could just as easily go in CRC_32.c itself:
标题中唯一的关键项是这个宏(它可以很容易地进入 CRC_32.c 本身:
#define UPDC32(octet, crc) (crc_32_tab[((crc) ^ (octet)) & 0xff] ^ ((crc) >> 8))
回答by NTDLS
I am the author of the source code at the specified link. While the intention of the source code license is not clear (it will be later today), the code is in fact open and free for use in your free or commercial applications with no strings attached.
我是指定链接中源代码的作者。虽然源代码许可证的意图尚不清楚(今天晚些时候),但该代码实际上是开放的,可免费用于您的免费或商业应用程序,不附带任何条件。
回答by Mark Adler
The crc code in zlib (http://zlib.net/) is among the fastest there is, and has a very liberal open source license.
zlib (http://zlib.net/) 中的 crc 代码是最快的,并且拥有非常自由的开源许可证。
And you should not use adler-32 except for special applications where speed is more important than error detection performance.
除了速度比错误检测性能更重要的特殊应用程序外,您不应使用 adler-32。
回答by giuspen
using zlib.h (http://refspecs.linuxbase.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/zlib-crc32-1.html):
使用 zlib.h ( http://refspecs.linuxbase.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/zlib-crc32-1.html):
#include <zlib.h>
unsigned long crc = crc32(0L, Z_NULL, 0);
crc = crc32(crc, (const unsigned char*)data_address, data_len);
回答by John Carter
回答by Martin M.
The most simple and straightforward C/C++ implementation that I found is in a link at the bottom of this page:
我发现的最简单直接的 C/C++ 实现位于本页底部的链接中:
Web Page: http://www.barrgroup.com/Embedded-Systems/How-To/CRC-Calculation-C-Code
网页:http: //www.barrgroup.com/Embedded-Systems/How-To/CRC-Calculation-C-Code
Code Download Link: https://barrgroup.com/code/crc.zip
代码下载链接:https: //barrgroup.com/code/crc.zip
It is a simple standalone implementation with one .h and one .c file. There is support for CRC32, CRC16 and CRC_CCITT thru the use of a define. Also, the code lets the user change parameter settings like the CRC polynomial, initial/final XOR value, and reflection options if you so desire.
它是一个简单的独立实现,带有一个 .h 和一个 .c 文件。通过使用定义支持 CRC32、CRC16 和 CRC_CCITT。此外,如果您愿意,代码允许用户更改参数设置,如 CRC 多项式、初始/最终 XOR 值和反射选项。
The license is not explicitly defined ala LGPL or similar. However the site does say that they are placing the code in the public domain for any use. The actual code files also say this.
许可证没有明确定义 ala LGPL 或类似的。然而,该网站确实表示他们将代码置于公共领域以供任何使用。实际的代码文件也说明了这一点。
Hope it helps!
希望能帮助到你!
回答by Nazar
The mhash libraryworks pretty good for me. It's fast enough, supports multiple types of hashing (crc32, MD5, SHA-1, HAVAL, RIPEMD128, RIPEMD160, TIGER, GOST, etc.). To get CRC32 of a string you would do something like this:
该mhash库工作得对我好。它足够快,支持多种类型的散列(crc32、MD5、SHA-1、HAVAL、RIPEMD128、RIPEMD160、TIGER、GOST 等)。要获取字符串的 CRC32,您可以执行以下操作:
MHASH td = mhash_init(MHASH_CRC32);
if (td == MHASH_FAILED) return -1; // handle failure
mhash(td, s, strlen(s));
unsigned int digest = 0; // crc32 will be stored here
mhash_deinit(td, &digest);
// do endian swap here if desired
回答by Janus Troelsen
rurban's fork of SMHasher(the original SMHasher seems abandoned) has hardware CRC32 support. The changes were added before the initial commit, but try comparing the new CMakeLists.txtand the old one(which doesn't mention SSE at all).
rurban 的 SMHasher 分支(最初的 SMHasher 似乎已被放弃)具有硬件 CRC32 支持。更改是在初始提交之前添加的,但请尝试比较新的 CMakeLists.txt和旧的(根本没有提到 SSE)。
The best option is probably Intel's zlib fork with PCLMULQDQ supportdescribed in this paper. This library also has the SSE 4.2 optimizations.
最好的办法可能是英特尔的zlib的叉PCLMULQDQ支持中所述文章。该库还具有 SSE 4.2 优化。
If you don't need portability and you're on Linux, you can use the kernel's implementation (which is hardware accelerated if available): https://stackoverflow.com/a/11156040/309483
如果你不需要可移植性并且你在 Linux 上,你可以使用内核的实现(如果可用,它是硬件加速的):https: //stackoverflow.com/a/11156040/309483