1228753Smm/*- 2228753Smm * Copyright (c) 2009 Joerg Sonnenberger 3228753Smm * All rights reserved. 4228753Smm * 5228753Smm * Redistribution and use in source and binary forms, with or without 6228753Smm * modification, are permitted provided that the following conditions 7228753Smm * are met: 8228753Smm * 1. Redistributions of source code must retain the above copyright 9228753Smm * notice, this list of conditions and the following disclaimer. 10228753Smm * 2. Redistributions in binary form must reproduce the above copyright 11228753Smm * notice, this list of conditions and the following disclaimer in the 12228753Smm * documentation and/or other materials provided with the distribution. 13228753Smm * 14228753Smm * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 15228753Smm * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16228753Smm * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17228753Smm * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 18228753Smm * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19228753Smm * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20228753Smm * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21228753Smm * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22228753Smm * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23228753Smm * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24228753Smm * 25229592Smm * $FreeBSD$ 26228753Smm */ 27228753Smm 28228753Smm#ifndef __LIBARCHIVE_BUILD 29228753Smm#error This header is only to be used internally to libarchive. 30228753Smm#endif 31228753Smm 32228753Smm/* 33228753Smm * When zlib is unavailable, we should still be able to validate 34228753Smm * uncompressed zip archives. That requires us to be able to compute 35228753Smm * the CRC32 check value. This is a drop-in compatible replacement 36228753Smm * for crc32() from zlib. It's slower than the zlib implementation, 37228753Smm * but still pretty fast: This runs about 300MB/s on my 3GHz P4 38228753Smm * compared to about 800MB/s for the zlib implementation. 39228753Smm */ 40228753Smmstatic unsigned long 41228753Smmcrc32(unsigned long crc, const void *_p, size_t len) 42228753Smm{ 43228753Smm unsigned long crc2, b, i; 44228753Smm const unsigned char *p = _p; 45228753Smm static volatile int crc_tbl_inited = 0; 46228753Smm static unsigned long crc_tbl[256]; 47228753Smm 48228753Smm if (!crc_tbl_inited) { 49228753Smm for (b = 0; b < 256; ++b) { 50228753Smm crc2 = b; 51228753Smm for (i = 8; i > 0; --i) { 52228753Smm if (crc2 & 1) 53228753Smm crc2 = (crc2 >> 1) ^ 0xedb88320UL; 54228753Smm else 55228753Smm crc2 = (crc2 >> 1); 56228753Smm } 57228753Smm crc_tbl[b] = crc2; 58228753Smm } 59228753Smm crc_tbl_inited = 1; 60228753Smm } 61228753Smm 62228753Smm crc = crc ^ 0xffffffffUL; 63228753Smm while (len--) 64228753Smm crc = crc_tbl[(crc ^ *p++) & 0xff] ^ (crc >> 8); 65228753Smm return (crc ^ 0xffffffffUL); 66228753Smm} 67