Path: blob/master/Utilities/cmlibarchive/libarchive/archive_crc32.h
3153 views
/*-1* Copyright (c) 2009 Joerg Sonnenberger2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR14* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES15* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.16* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,17* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT18* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,19* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY20* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT21* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF22* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.23*/2425#ifndef ARCHIVE_CRC32_H26#define ARCHIVE_CRC32_H2728#ifndef __LIBARCHIVE_BUILD29#error This header is only to be used internally to libarchive.30#endif3132#include <stddef.h>3334/*35* When zlib is unavailable, we should still be able to validate36* uncompressed zip archives. That requires us to be able to compute37* the CRC32 check value. This is a drop-in compatible replacement38* for crc32() from zlib. It's slower than the zlib implementation,39* but still pretty fast: This runs about 300MB/s on my 3GHz P440* compared to about 800MB/s for the zlib implementation.41*/42static unsigned long43crc32(unsigned long crc, const void *_p, size_t len)44{45unsigned long crc2, b, i;46const unsigned char *p = _p;47static volatile int crc_tbl_inited = 0;48static unsigned long crc_tbl[256];4950if (_p == NULL)51return (0);5253if (!crc_tbl_inited) {54for (b = 0; b < 256; ++b) {55crc2 = b;56for (i = 8; i > 0; --i) {57if (crc2 & 1)58crc2 = (crc2 >> 1) ^ 0xedb88320UL;59else60crc2 = (crc2 >> 1);61}62crc_tbl[b] = crc2;63}64crc_tbl_inited = 1;65}6667crc = crc ^ 0xffffffffUL;68/* A use of this loop is about 20% - 30% faster than69* no use version in any optimization option of gcc. */70for (;len >= 8; len -= 8) {71crc = crc_tbl[(crc ^ *p++) & 0xff] ^ (crc >> 8);72crc = crc_tbl[(crc ^ *p++) & 0xff] ^ (crc >> 8);73crc = crc_tbl[(crc ^ *p++) & 0xff] ^ (crc >> 8);74crc = crc_tbl[(crc ^ *p++) & 0xff] ^ (crc >> 8);75crc = crc_tbl[(crc ^ *p++) & 0xff] ^ (crc >> 8);76crc = crc_tbl[(crc ^ *p++) & 0xff] ^ (crc >> 8);77crc = crc_tbl[(crc ^ *p++) & 0xff] ^ (crc >> 8);78crc = crc_tbl[(crc ^ *p++) & 0xff] ^ (crc >> 8);79}80while (len--)81crc = crc_tbl[(crc ^ *p++) & 0xff] ^ (crc >> 8);82return (crc ^ 0xffffffffUL);83}8485#endif868788