source: mainline/uspace/lib/net/generic/net_checksum.c@ d3ce33fa

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d3ce33fa was ccca251, checked in by Martin Decky <martin@…>, 14 years ago

improve comments, use C++ style comments for TODOs and FIXMEs

  • Property mode set to 100644
File size: 6.1 KB
RevLine 
[21580dd]1/*
2 * Copyright (c) 2009 Lukas Mejdrech
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
[8d601db]29/** @addtogroup libnet
30 * @{
[21580dd]31 */
32
33/** @file
[8d601db]34 * General CRC and checksum computation implementation.
[21580dd]35 */
36
37#include <sys/types.h>
38
[849ed54]39#include <net_checksum.h>
[21580dd]40
[8d601db]41/** Big-endian encoding CRC divider. */
42#define CRC_DIVIDER_BE 0x04c11db7
[21580dd]43
[8d601db]44/** Little-endian encoding CRC divider. */
45#define CRC_DIVIDER_LE 0xedb88320
[21580dd]46
[8d601db]47/** Compacts the computed checksum to the 16 bit number adding the carries.
48 *
49 * @param[in] sum Computed checksum.
[1bfd3d3]50 * @return Compacted computed checksum to the 16 bits.
[8d601db]51 */
52uint16_t compact_checksum(uint32_t sum)
53{
[28a3e74]54 /* Shorten to the 16 bits */
[8d601db]55 while (sum >> 16)
56 sum = (sum & 0xffff) + (sum >> 16);
[a64c64d]57
58 return (uint16_t) sum;
59}
60
[8d601db]61/** Computes sum of the 2 byte fields.
62 *
63 * Padds one zero (0) byte if odd.
64 *
65 * @param[in] seed Initial value. Often used as 0 or ~0.
66 * @param[in] data Pointer to the beginning of data to process.
67 * @param[in] length Length of the data in bytes.
[1bfd3d3]68 * @return The computed checksum of the length bytes of the data.
[8d601db]69 */
70uint32_t compute_checksum(uint32_t seed, uint8_t *data, size_t length)
71{
[aadf01e]72 size_t index;
73
[28a3e74]74 /* Sum all the 16 bit fields */
[8d601db]75 for (index = 0; index + 1 < length; index += 2)
[a64c64d]76 seed += (data[index] << 8) + data[index + 1];
77
[28a3e74]78 /* Last odd byte with zero padding */
[8d601db]79 if (index + 1 == length)
[a64c64d]80 seed += data[index] << 8;
81
[21580dd]82 return seed;
83}
84
[8d601db]85/** Computes CRC32 value in the big-endian environment.
86 *
87 * @param[in] seed Initial value. Often used as 0 or ~0.
88 * @param[in] data Pointer to the beginning of data to process.
89 * @param[in] length Length of the data in bits.
[1bfd3d3]90 * @return The computed CRC32 of the length bits of the data.
[8d601db]91 */
92uint32_t compute_crc32_be(uint32_t seed, uint8_t * data, size_t length)
93{
[aadf01e]94 size_t index;
[21580dd]95
[28a3e74]96 /* Process full bytes */
[8d601db]97 while (length >= 8) {
[28a3e74]98 /* Add the data */
[aadf01e]99 seed ^= (*data) << 24;
[8d601db]100
[28a3e74]101 /* For each added bit */
[8d601db]102 for (index = 0; index < 8; ++index) {
[28a3e74]103 /* If the first bit is set */
[8d601db]104 if (seed & 0x80000000) {
[28a3e74]105 /* Shift and divide the checksum */
[aadf01e]106 seed = (seed << 1) ^ ((uint32_t) CRC_DIVIDER_BE);
[8d601db]107 } else {
[ccca251]108 /* Shift otherwise */
[21580dd]109 seed <<= 1;
110 }
111 }
[8d601db]112
[28a3e74]113 /* Move to the next byte */
[8d601db]114 ++data;
[21580dd]115 length -= 8;
116 }
[a64c64d]117
[28a3e74]118 /* Process the odd bits */
[8d601db]119 if (length > 0) {
[28a3e74]120 /* Add the data with zero padding */
[8d601db]121 seed ^= ((*data) & (0xff << (8 - length))) << 24;
122
[28a3e74]123 /* For each added bit */
[8d601db]124 for (index = 0; index < length; ++index) {
[28a3e74]125 /* If the first bit is set */
[8d601db]126 if (seed & 0x80000000) {
[28a3e74]127 /* Shift and divide the checksum */
[aadf01e]128 seed = (seed << 1) ^ ((uint32_t) CRC_DIVIDER_BE);
[8d601db]129 } else {
[28a3e74]130 /* Shift otherwise */
[21580dd]131 seed <<= 1;
132 }
133 }
134 }
[a64c64d]135
[21580dd]136 return seed;
137}
138
[8d601db]139/** Computes CRC32 value in the little-endian environment.
140 *
141 * @param[in] seed Initial value. Often used as 0 or ~0.
142 * @param[in] data Pointer to the beginning of data to process.
143 * @param[in] length Length of the data in bits.
[1bfd3d3]144 * @return The computed CRC32 of the length bits of the data.
[8d601db]145 */
146uint32_t compute_crc32_le(uint32_t seed, uint8_t * data, size_t length)
147{
[aadf01e]148 size_t index;
[21580dd]149
[28a3e74]150 /* Process full bytes */
[8d601db]151 while (length >= 8) {
[28a3e74]152 /* Add the data */
[a64c64d]153 seed ^= (*data);
[8d601db]154
[28a3e74]155 /* For each added bit */
[8d601db]156 for (index = 0; index < 8; ++index) {
[28a3e74]157 /* If the last bit is set */
[8d601db]158 if (seed & 1) {
[28a3e74]159 /* Shift and divide the checksum */
[a64c64d]160 seed = (seed >> 1) ^ ((uint32_t) CRC_DIVIDER_LE);
[8d601db]161 } else {
[28a3e74]162 /* Shift otherwise */
[a64c64d]163 seed >>= 1;
164 }
165 }
[8d601db]166
[28a3e74]167 /* Move to the next byte */
[8d601db]168 ++data;
[a64c64d]169 length -= 8;
[21580dd]170 }
171
[28a3e74]172 /* Process the odd bits */
[8d601db]173 if (length > 0) {
[28a3e74]174 /* Add the data with zero padding */
[a64c64d]175 seed ^= (*data) >> (8 - length);
[8d601db]176
177 for (index = 0; index < length; ++index) {
[28a3e74]178 /* If the last bit is set */
[8d601db]179 if (seed & 1) {
[28a3e74]180 /* Shift and divide the checksum */
[a64c64d]181 seed = (seed >> 1) ^ ((uint32_t) CRC_DIVIDER_LE);
[8d601db]182 } else {
[28a3e74]183 /* Shift otherwise */
[a64c64d]184 seed >>= 1;
185 }
186 }
[21580dd]187 }
188
189 return seed;
190}
191
[8d601db]192/** Returns or flips the checksum if zero.
193 *
194 * @param[in] checksum The computed checksum.
[1bfd3d3]195 * @return The internet protocol header checksum.
196 * @return 0xFFFF if the computed checksum is zero.
[8d601db]197 */
198uint16_t flip_checksum(uint16_t checksum)
199{
[28a3e74]200 /* Flip, zero is returned as 0xFFFF (not flipped) */
[8d601db]201 checksum = ~checksum;
[918e9910]202 return checksum ? checksum : IP_CHECKSUM_ZERO;
[21580dd]203}
204
[8d601db]205/** Computes the ip header checksum.
206 *
207 * To compute the checksum of a new packet, the checksum header field must be
208 * zero. To check the checksum of a received packet, the checksum may be left
209 * set. Zero will be returned in this case if valid.
210 *
211 * @param[in] data The header data.
212 * @param[in] length The header length in bytes.
[1bfd3d3]213 * @return The internet protocol header checksum.
214 * @return 0xFFFF if the computed checksum is zero.
[8d601db]215 */
216uint16_t ip_checksum(uint8_t *data, size_t length)
217{
[28a3e74]218 /* Compute, compact and flip the data checksum */
[8d601db]219 return flip_checksum(compact_checksum(compute_checksum(0, data,
220 length)));
[21580dd]221}
222
223/** @}
224 */
Note: See TracBrowser for help on using the repository browser.