iPXE
crc32.c
Go to the documentation of this file.
00001 /*
00002  * Little-endian CRC32 implementation.
00003  *
00004  * Copyright (c) 2009 Joshua Oreman <oremanj@rwcr.net>.
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License as
00008  * published by the Free Software Foundation; either version 2 of the
00009  * License, or any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful, but
00012  * WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00019  * 02110-1301, USA.
00020  */
00021 
00022 FILE_LICENCE ( GPL2_OR_LATER );
00023 
00024 #include <ipxe/crc32.h>
00025 
00026 #define CRCPOLY         0xedb88320
00027 
00028 /**
00029  * Calculate 32-bit little-endian CRC checksum
00030  *
00031  * @v seed      Initial value
00032  * @v data      Data to checksum
00033  * @v len       Length of data
00034  *
00035  * Usually @a seed is initially zero or all one bits, depending on the
00036  * protocol. To continue a CRC checksum over multiple calls, pass the
00037  * return value from one call as the @a seed parameter to the next.
00038  */
00039 u32 crc32_le ( u32 seed, const void *data, size_t len )
00040 {
00041         u32 crc = seed;
00042         const u8 *src = data;
00043         u32 mult;
00044         int i;
00045 
00046         while ( len-- ) {
00047                 crc ^= *src++;
00048                 for ( i = 0; i < 8; i++ ) {
00049                         mult = ( crc & 1 ) ? CRCPOLY : 0;
00050                         crc = ( crc >> 1 ) ^ mult;
00051                 }
00052         }
00053 
00054         return crc;
00055 }