CRC32 C or C++ implementation

C++CCrc32

C++ Problem Overview


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 here seems 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++ Solutions


Solution 1 - C++

The SNIPPETS C Source Code Archive has a CRC32 implementation that is freely usable:

/* 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 Machine has it archived.)

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.

The only critical item in the header is this macro (which could just as easily go in CRC_32.c itself:

#define UPDC32(octet, crc) (crc_32_tab[((crc) ^ (octet)) & 0xff] ^ ((crc) >> 8))

Solution 2 - C++

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.

Solution 3 - C++

Use the Boost C++ libraries. There is a CRC included there and the license is good.

Solution 4 - C++

The crc code in zlib (http://zlib.net/) is among the fastest there is, and has a very liberal open source license.

And you should not use adler-32 except for special applications where speed is more important than error detection performance.

Solution 5 - C++

using 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);

Solution 6 - C++

http://www.tty1.net/pycrc/">**pycrc**</a> is a Python script that generates C CRC code, with options to select the CRC size, algorithm and model.

It's released under the MIT licence. Is that acceptable for your purposes?

Solution 7 - C++

The most simple and straightforward C/C++ implementation that I found is in a link at the bottom of this page:

Web Page: http://www.barrgroup.com/Embedded-Systems/How-To/CRC-Calculation-C-Code

Code Download Link: 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.

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.

Hope it helps!

Solution 8 - C++

The mhash library works 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 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

Solution 9 - C++

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.txt and the old one (which doesn't mention SSE at all).

The best option is probably Intel's zlib fork with PCLMULQDQ support described in this paper. This library also has the SSE 4.2 optimizations.

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiontwkView Question on Stackoverflow
Solution 1 - C++cjmView Answer on Stackoverflow
Solution 2 - C++NTDLSView Answer on Stackoverflow
Solution 3 - C++Zan LynxView Answer on Stackoverflow
Solution 4 - C++Mark AdlerView Answer on Stackoverflow
Solution 5 - C++giuspenView Answer on Stackoverflow
Solution 6 - C++John CarterView Answer on Stackoverflow
Solution 7 - C++Martin M.View Answer on Stackoverflow
Solution 8 - C++NazarView Answer on Stackoverflow
Solution 9 - C++Janus TroelsenView Answer on Stackoverflow