Subversion Repositories HelenOS

Rev

Rev 4743 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4743 Rev 4756
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/checksum.h"
39
#include "include/checksum.h"
40
 
40
 
-
 
41
/** Big-endian encoding CRC divider.
-
 
42
 */
41
#define CRC_DIVIDER_BE  0x04C11DB7
43
#define CRC_DIVIDER_BE  0x04C11DB7
42
 
44
 
-
 
45
/** Little-endian encoding CRC divider.
-
 
46
 */
43
#define CRC_DIVIDER_LE  0xEDB88320
47
#define CRC_DIVIDER_LE  0xEDB88320
44
 
48
 
45
/** IP checksum value for computed zero checksum.
49
/** IP checksum value for computed zero checksum.
46
 *  Zero is returned as 0xFFFF (not flipped)
50
 *  Zero is returned as 0xFFFF (not flipped)
47
 */
51
 */
48
#define IP_CHECKSUM_ZERO            0xFFFFu
52
#define IP_CHECKSUM_ZERO            0xFFFFu
49
 
53
 
50
uint32_t compute_crc32_le( uint32_t seed, uint8_t * data, size_t length ){
54
uint32_t compute_crc32_le( uint32_t seed, uint8_t * data, size_t length ){
51
    size_t  index;
55
    size_t  index;
52
 
56
 
53
    while( length >= 8 ){
57
    while( length >= 8 ){
54
        seed ^= ( * data );
58
        seed ^= ( * data );
55
        for( index = 0; index < 8; ++ index ){
59
        for( index = 0; index < 8; ++ index ){
56
            if( seed & 1 ){
60
            if( seed & 1 ){
57
                seed = ( seed >> 1 ) ^ (( uint32_t ) CRC_DIVIDER_LE );
61
                seed = ( seed >> 1 ) ^ (( uint32_t ) CRC_DIVIDER_LE );
58
            }else{
62
            }else{
59
                seed >>= 1;
63
                seed >>= 1;
60
            }
64
            }
61
        }
65
        }
62
        ++ data;
66
        ++ data;
63
        length -= 8;
67
        length -= 8;
64
    }
68
    }
65
    if( length > 0 ){
69
    if( length > 0 ){
66
        seed ^= ( * data ) >> ( 8 - length );
70
        seed ^= ( * data ) >> ( 8 - length );
67
        for( index = 0; index < length; ++ index ){
71
        for( index = 0; index < length; ++ index ){
68
            if( seed & 1 ){
72
            if( seed & 1 ){
69
                seed = ( seed >> 1 ) ^ (( uint32_t ) CRC_DIVIDER_LE );
73
                seed = ( seed >> 1 ) ^ (( uint32_t ) CRC_DIVIDER_LE );
70
            }else{
74
            }else{
71
                seed >>= 1;
75
                seed >>= 1;
72
            }
76
            }
73
        }
77
        }
74
        length -= 8;
78
        length -= 8;
75
    }
79
    }
76
    return seed;
80
    return seed;
77
}
81
}
78
 
82
 
79
uint32_t compute_crc32_be( uint32_t seed, uint8_t * data, size_t length ){
83
uint32_t compute_crc32_be( uint32_t seed, uint8_t * data, size_t length ){
80
    size_t  index;
84
    size_t  index;
81
 
85
 
82
    while( length >= 8 ){
86
    while( length >= 8 ){
83
        seed ^= ( * data ) << 24;
87
        seed ^= ( * data ) << 24;
84
        for( index = 0; index < 8; ++ index ){
88
        for( index = 0; index < 8; ++ index ){
85
            if( seed & 0x80000000 ){
89
            if( seed & 0x80000000 ){
86
                seed = ( seed << 1 ) ^ (( uint32_t ) CRC_DIVIDER_BE );
90
                seed = ( seed << 1 ) ^ (( uint32_t ) CRC_DIVIDER_BE );
87
            }else{
91
            }else{
88
                seed <<= 1;
92
                seed <<= 1;
89
            }
93
            }
90
        }
94
        }
91
        ++ data;
95
        ++ data;
92
        length -= 8;
96
        length -= 8;
93
    }
97
    }
94
    if( length > 0 ){
98
    if( length > 0 ){
95
        seed ^= (( * data ) & ( 0xFF << ( 8 - length ))) << 24;
99
        seed ^= (( * data ) & ( 0xFF << ( 8 - length ))) << 24;
96
        for( index = 0; index < length; ++ index ){
100
        for( index = 0; index < length; ++ index ){
97
            if( seed & 0x80000000 ){
101
            if( seed & 0x80000000 ){
98
                seed = ( seed << 1 ) ^ (( uint32_t ) CRC_DIVIDER_BE );
102
                seed = ( seed << 1 ) ^ (( uint32_t ) CRC_DIVIDER_BE );
99
            }else{
103
            }else{
100
                seed <<= 1;
104
                seed <<= 1;
101
            }
105
            }
102
        }
106
        }
103
        length -= 8;
107
        length -= 8;
104
    }
108
    }
105
    return seed;
109
    return seed;
106
}
110
}
107
 
111
 
108
uint32_t compute_checksum( uint32_t seed, uint8_t * data, size_t length ){
112
uint32_t compute_checksum( uint32_t seed, uint8_t * data, size_t length ){
109
    size_t  index;
113
    size_t  index;
110
 
114
 
111
    // sum all the 16 bit fields
115
    // sum all the 16 bit fields
112
    for( index = 0; index + 1 < length; index += 2 ){
116
    for( index = 0; index + 1 < length; index += 2 ){
113
        seed += ( data[ index ] << 8 ) + data[ index + 1 ];
117
        seed += ( data[ index ] << 8 ) + data[ index + 1 ];
114
    }
118
    }
115
 
119
 
116
    // last odd byte with zero padding
120
    // last odd byte with zero padding
117
    if( index + 1 == length ){
121
    if( index + 1 == length ){
118
        seed += data[ index ] << 8;
122
        seed += data[ index ] << 8;
119
    }
123
    }
120
 
124
 
121
    return seed;
125
    return seed;
122
}
126
}
123
 
127
 
124
uint16_t compact_checksum( uint32_t sum ){
128
uint16_t compact_checksum( uint32_t sum ){
125
    // shorten to the 16 bits
129
    // shorten to the 16 bits
126
    while( sum >> 16 ) sum = ( sum & 0xFFFF ) + ( sum >> 16 );
130
    while( sum >> 16 ) sum = ( sum & 0xFFFF ) + ( sum >> 16 );
127
 
131
 
128
    return ( uint16_t ) sum;
132
    return ( uint16_t ) sum;
129
}
133
}
130
 
134
 
131
uint16_t flip_checksum( uint16_t checksum ){
135
uint16_t flip_checksum( uint16_t checksum ){
132
    // flip, zero is returned as 0xFFFF (not flipped)
136
    // flip, zero is returned as 0xFFFF (not flipped)
133
    return ( ~ checksum ) ? ( uint16_t ) ( ~ checksum ) : IP_CHECKSUM_ZERO;
137
    return ( ~ checksum ) ? ( uint16_t ) ( ~ checksum ) : IP_CHECKSUM_ZERO;
134
}
138
}
135
 
139
 
136
uint16_t ip_checksum( uint8_t * data, size_t length ){
140
uint16_t ip_checksum( uint8_t * data, size_t length ){
137
    return flip_checksum( compact_checksum( compute_checksum( 0, data, length )));
141
    return flip_checksum( compact_checksum( compute_checksum( 0, data, length )));
138
}
142
}
139
 
143
 
140
/** @}
144
/** @}
141
 */
145
 */
142
 
146