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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since a9c6b966 was 1bfd3d3, checked in by Jiri Svoboda <jiri@…>, 15 years ago

Replace @returns with @return.

  • Property mode set to 100644
File size: 6.0 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{
[a64c64d]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
[a64c64d]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
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
[a64c64d]96 // process full bytes
[8d601db]97 while (length >= 8) {
[a64c64d]98 // add the data
[aadf01e]99 seed ^= (*data) << 24;
[8d601db]100
[a64c64d]101 // for each added bit
[8d601db]102 for (index = 0; index < 8; ++index) {
[a64c64d]103 // if the first bit is set
[8d601db]104 if (seed & 0x80000000) {
[a64c64d]105 // shift and divide the checksum
[aadf01e]106 seed = (seed << 1) ^ ((uint32_t) CRC_DIVIDER_BE);
[8d601db]107 } else {
[a64c64d]108 // shift otherwise
[21580dd]109 seed <<= 1;
110 }
111 }
[8d601db]112
[a64c64d]113 // move to the next byte
[8d601db]114 ++data;
[21580dd]115 length -= 8;
116 }
[a64c64d]117
118 // process the odd bits
[8d601db]119 if (length > 0) {
[a64c64d]120 // add the data with zero padding
[8d601db]121 seed ^= ((*data) & (0xff << (8 - length))) << 24;
122
[a64c64d]123 // for each added bit
[8d601db]124 for (index = 0; index < length; ++index) {
[a64c64d]125 // if the first bit is set
[8d601db]126 if (seed & 0x80000000) {
[a64c64d]127 // shift and divide the checksum
[aadf01e]128 seed = (seed << 1) ^ ((uint32_t) CRC_DIVIDER_BE);
[8d601db]129 } else {
[a64c64d]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
[a64c64d]150 // process full bytes
[8d601db]151 while (length >= 8) {
[a64c64d]152 // add the data
153 seed ^= (*data);
[8d601db]154
[a64c64d]155 // for each added bit
[8d601db]156 for (index = 0; index < 8; ++index) {
[a64c64d]157 // if the last bit is set
[8d601db]158 if (seed & 1) {
[a64c64d]159 // shift and divide the checksum
160 seed = (seed >> 1) ^ ((uint32_t) CRC_DIVIDER_LE);
[8d601db]161 } else {
[a64c64d]162 // shift otherwise
163 seed >>= 1;
164 }
165 }
[8d601db]166
[a64c64d]167 // move to the next byte
[8d601db]168 ++data;
[a64c64d]169 length -= 8;
[21580dd]170 }
171
[a64c64d]172 // process the odd bits
[8d601db]173 if (length > 0) {
[a64c64d]174 // add the data with zero padding
175 seed ^= (*data) >> (8 - length);
[8d601db]176
177 for (index = 0; index < length; ++index) {
[a64c64d]178 // if the last bit is set
[8d601db]179 if (seed & 1) {
[a64c64d]180 // shift and divide the checksum
181 seed = (seed >> 1) ^ ((uint32_t) CRC_DIVIDER_LE);
[8d601db]182 } else {
[a64c64d]183 // shift otherwise
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{
[21580dd]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{
[a64c64d]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.