Subversion Repositories HelenOS

Rev

Rev 4743 | Details | Compare with Previous | Last modification | View Log | RSS feed

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