net_checksum.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009 Lukas Mejdrech
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00037 #include <sys/types.h>
00038 
00039 #include <net_checksum.h>
00040 
00042 #define CRC_DIVIDER_BE  0x04c11db7
00043 
00045 #define CRC_DIVIDER_LE  0xedb88320
00046 
00052 uint16_t compact_checksum(uint32_t sum)
00053 {
00054         /* Shorten to the 16 bits */
00055         while (sum >> 16)
00056                 sum = (sum & 0xffff) + (sum >> 16);
00057 
00058         return (uint16_t) sum;
00059 }
00060 
00070 uint32_t compute_checksum(uint32_t seed, uint8_t *data, size_t length)
00071 {
00072         size_t index;
00073 
00074         /* Sum all the 16 bit fields */
00075         for (index = 0; index + 1 < length; index += 2)
00076                 seed += (data[index] << 8) + data[index + 1];
00077 
00078         /* Last odd byte with zero padding */
00079         if (index + 1 == length)
00080                 seed += data[index] << 8;
00081 
00082         return seed;
00083 }
00084 
00092 uint32_t compute_crc32_be(uint32_t seed, uint8_t * data, size_t length)
00093 {
00094         size_t index;
00095 
00096         /* Process full bytes */
00097         while (length >= 8) {
00098                 /* Add the data */
00099                 seed ^= (*data) << 24;
00100                 
00101                 /* For each added bit */
00102                 for (index = 0; index < 8; ++index) {
00103                         /* If the first bit is set */
00104                         if (seed & 0x80000000) {
00105                                 /* Shift and divide the checksum */
00106                                 seed = (seed << 1) ^ ((uint32_t) CRC_DIVIDER_BE);
00107                         } else {
00108                                 /* Shift otherwise */
00109                                 seed <<= 1;
00110                         }
00111                 }
00112                 
00113                 /* Move to the next byte */
00114                 ++data;
00115                 length -= 8;
00116         }
00117 
00118         /* Process the odd bits */
00119         if (length > 0) {
00120                 /* Add the data with zero padding */
00121                 seed ^= ((*data) & (0xff << (8 - length))) << 24;
00122                 
00123                 /* For each added bit */
00124                 for (index = 0; index < length; ++index) {
00125                         /* If the first bit is set */
00126                         if (seed & 0x80000000) {
00127                                 /* Shift and divide the checksum */
00128                                 seed = (seed << 1) ^ ((uint32_t) CRC_DIVIDER_BE);
00129                         } else {
00130                                 /* Shift otherwise */
00131                                 seed <<= 1;
00132                         }
00133                 }
00134         }
00135 
00136         return seed;
00137 }
00138 
00146 uint32_t compute_crc32_le(uint32_t seed, uint8_t * data, size_t length)
00147 {
00148         size_t index;
00149 
00150         /* Process full bytes */
00151         while (length >= 8) {
00152                 /* Add the data */
00153                 seed ^= (*data);
00154                 
00155                 /* For each added bit */
00156                 for (index = 0; index < 8; ++index) {
00157                         /* If the last bit is set */
00158                         if (seed & 1) {
00159                                 /* Shift and divide the checksum */
00160                                 seed = (seed >> 1) ^ ((uint32_t) CRC_DIVIDER_LE);
00161                         } else {
00162                                 /* Shift otherwise */
00163                                 seed >>= 1;
00164                         }
00165                 }
00166                 
00167                 /* Move to the next byte */
00168                 ++data;
00169                 length -= 8;
00170         }
00171 
00172         /* Process the odd bits */
00173         if (length > 0) {
00174                 /* Add the data with zero padding */
00175                 seed ^= (*data) >> (8 - length);
00176                 
00177                 for (index = 0; index < length; ++index) {
00178                         /* If the last bit is set */
00179                         if (seed & 1) {
00180                                 /* Shift and divide the checksum */
00181                                 seed = (seed >> 1) ^ ((uint32_t) CRC_DIVIDER_LE);
00182                         } else {
00183                                 /* Shift otherwise */
00184                                 seed >>= 1;
00185                         }
00186                 }
00187         }
00188 
00189         return seed;
00190 }
00191 
00198 uint16_t flip_checksum(uint16_t checksum)
00199 {
00200         /* Flip, zero is returned as 0xFFFF (not flipped) */
00201         checksum = ~checksum;
00202         return checksum ? checksum : IP_CHECKSUM_ZERO;
00203 }
00204 
00216 uint16_t ip_checksum(uint8_t *data, size_t length)
00217 {
00218         /* Compute, compact and flip the data checksum */
00219         return flip_checksum(compact_checksum(compute_checksum(0, data,
00220             length)));
00221 }
00222 

Generated on Thu Jun 2 07:45:47 2011 for HelenOS/USB by  doxygen 1.4.7