Rev 4708 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4708 | Rev 4711 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2009 Lukas Mejdrech |
2 | * Copyright (c) 2009 Lukas Mejdrech |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup net |
29 | /** @addtogroup net |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | 32 | ||
33 | /** @file |
33 | /** @file |
34 | * General CRC and checksum computation implementation. |
34 | * General CRC and checksum computation implementation. |
35 | */ |
35 | */ |
36 | 36 | ||
37 | #include <sys/types.h> |
37 | #include <sys/types.h> |
38 | 38 | ||
39 | #include "include/crc.h" |
39 | #include "include/crc.h" |
40 | 40 | ||
41 | #define CRC_DIVIDER_BE 0x04C11DB7 |
41 | #define CRC_DIVIDER_BE 0x04C11DB7 |
42 | 42 | ||
43 | #define CRC_DIVIDER_LE 0xEDB88320 |
43 | #define CRC_DIVIDER_LE 0xEDB88320 |
44 | 44 | ||
- | 45 | /** IP checksum value for computed zero checksum. |
|
- | 46 | * Zero is returned as 0xFFFF (not flipped) |
|
- | 47 | */ |
|
- | 48 | #define IP_CHECKSUM_ZERO 0xFFFFu |
|
- | 49 | ||
45 | uint32_t compute_crc32_le( uint32_t seed, uint8_t * data, size_t length ){ |
50 | uint32_t compute_crc32_le( uint32_t seed, uint8_t * data, size_t length ){ |
46 | size_t index; |
51 | size_t index; |
47 | 52 | ||
48 | while( length >= 8 ){ |
53 | while( length >= 8 ){ |
49 | seed ^= ( * data ); |
54 | seed ^= ( * data ); |
50 | for( index = 0; index < 8; ++ index ){ |
55 | for( index = 0; index < 8; ++ index ){ |
51 | if( seed & 1 ){ |
56 | if( seed & 1 ){ |
52 | seed = ( seed >> 1 ) ^ (( uint32_t ) CRC_DIVIDER_LE ); |
57 | seed = ( seed >> 1 ) ^ (( uint32_t ) CRC_DIVIDER_LE ); |
53 | }else{ |
58 | }else{ |
54 | seed >>= 1; |
59 | seed >>= 1; |
55 | } |
60 | } |
56 | } |
61 | } |
57 | ++ data; |
62 | ++ data; |
58 | length -= 8; |
63 | length -= 8; |
59 | } |
64 | } |
60 | if( length > 0 ){ |
65 | if( length > 0 ){ |
61 | seed ^= ( * data ) >> ( 8 - length ); |
66 | seed ^= ( * data ) >> ( 8 - length ); |
62 | for( index = 0; index < length; ++ index ){ |
67 | for( index = 0; index < length; ++ index ){ |
63 | if( seed & 1 ){ |
68 | if( seed & 1 ){ |
64 | seed = ( seed >> 1 ) ^ (( uint32_t ) CRC_DIVIDER_LE ); |
69 | seed = ( seed >> 1 ) ^ (( uint32_t ) CRC_DIVIDER_LE ); |
65 | }else{ |
70 | }else{ |
66 | seed >>= 1; |
71 | seed >>= 1; |
67 | } |
72 | } |
68 | } |
73 | } |
69 | length -= 8; |
74 | length -= 8; |
70 | } |
75 | } |
71 | return seed; |
76 | return seed; |
72 | } |
77 | } |
73 | 78 | ||
74 | uint32_t compute_crc32_be( uint32_t seed, uint8_t * data, size_t length ){ |
79 | uint32_t compute_crc32_be( uint32_t seed, uint8_t * data, size_t length ){ |
75 | size_t index; |
80 | size_t index; |
76 | 81 | ||
77 | while( length >= 8 ){ |
82 | while( length >= 8 ){ |
78 | seed ^= ( * data ) << 24; |
83 | seed ^= ( * data ) << 24; |
79 | for( index = 0; index < 8; ++ index ){ |
84 | for( index = 0; index < 8; ++ index ){ |
80 | if( seed & 0x80000000 ){ |
85 | if( seed & 0x80000000 ){ |
81 | seed = ( seed << 1 ) ^ (( uint32_t ) CRC_DIVIDER_BE ); |
86 | seed = ( seed << 1 ) ^ (( uint32_t ) CRC_DIVIDER_BE ); |
82 | }else{ |
87 | }else{ |
83 | seed <<= 1; |
88 | seed <<= 1; |
84 | } |
89 | } |
85 | } |
90 | } |
86 | ++ data; |
91 | ++ data; |
87 | length -= 8; |
92 | length -= 8; |
88 | } |
93 | } |
89 | if( length > 0 ){ |
94 | if( length > 0 ){ |
90 | seed ^= (( * data ) & ( 0xFF << ( 8 - length ))) << 24; |
95 | seed ^= (( * data ) & ( 0xFF << ( 8 - length ))) << 24; |
91 | for( index = 0; index < length; ++ index ){ |
96 | for( index = 0; index < length; ++ index ){ |
92 | if( seed & 0x80000000 ){ |
97 | if( seed & 0x80000000 ){ |
93 | seed = ( seed << 1 ) ^ (( uint32_t ) CRC_DIVIDER_BE ); |
98 | seed = ( seed << 1 ) ^ (( uint32_t ) CRC_DIVIDER_BE ); |
94 | }else{ |
99 | }else{ |
95 | seed <<= 1; |
100 | seed <<= 1; |
96 | } |
101 | } |
97 | } |
102 | } |
98 | length -= 8; |
103 | length -= 8; |
99 | } |
104 | } |
100 | return seed; |
105 | return seed; |
101 | } |
106 | } |
102 | 107 | ||
103 | uint32_t compute_checksum( uint32_t seed, uint8_t * data, size_t length ){ |
108 | uint32_t compute_checksum( uint32_t seed, uint8_t * data, size_t length ){ |
104 | size_t index; |
109 | size_t index; |
105 | 110 | ||
106 | // sum all the 16 bit fields |
111 | // sum all the 16 bit fields |
107 | for( index = 0; index + 1 < length; index += 2 ){ |
112 | for( index = 0; index + 1 < length; index += 2 ){ |
108 | seed += ( data[ index ] << 8 ) + data[ index + 1 ]; |
113 | seed += ( data[ index ] << 8 ) + data[ index + 1 ]; |
109 | } |
114 | } |
110 | 115 | ||
111 | // last odd byte with zero padding |
116 | // last odd byte with zero padding |
112 | if( index + 1 == length ){ |
117 | if( index + 1 == length ){ |
113 | seed += data[ index ] << 8; |
118 | seed += data[ index ] << 8; |
114 | } |
119 | } |
115 | 120 | ||
116 | return seed; |
121 | return seed; |
117 | } |
122 | } |
118 | 123 | ||
119 | uint16_t compact_checksum( uint32_t sum ){ |
124 | uint16_t compact_checksum( uint32_t sum ){ |
120 | // shorten to the 16 bits |
125 | // shorten to the 16 bits |
121 | while( sum >> 16 ) sum = ( sum & 0xFFFF ) + ( sum >> 16 ); |
126 | while( sum >> 16 ) sum = ( sum & 0xFFFF ) + ( sum >> 16 ); |
122 | 127 | ||
123 | return ( uint16_t ) sum; |
128 | return ( uint16_t ) sum; |
124 | } |
129 | } |
- | 130 | ||
- | 131 | uint16_t ip_checksum( uint8_t * data, size_t length ){ |
|
- | 132 | uint16_t checksum; |
|
- | 133 | ||
- | 134 | checksum = compact_checksum(compute_checksum( 0, data, length )); |
|
- | 135 | ||
- | 136 | // flip, zero is returned as 0xFFFF (not flipped) |
|
- | 137 | return ( ~ checksum ) ? ( uint16_t ) ( ~ checksum ) : IP_CHECKSUM_ZERO; |
|
- | 138 | } |
|
125 | 139 | ||
126 | /** @} |
140 | /** @} |
127 | */ |
141 | */ |
128 | 142 |