Subversion Repositories tpanel

Rev

Rev 432 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 432 Rev 434
Line 24... Line 24...
24
 
24
 
25
TCrc32::TCrc32(const vector<uint8_t>& bytes)
25
TCrc32::TCrc32(const vector<uint8_t>& bytes)
26
{
26
{
27
    DECL_TRACER("TCrc32::TCrc32(const vector<uint8_t>& bytes)");
27
    DECL_TRACER("TCrc32::TCrc32(const vector<uint8_t>& bytes)");
28
 
28
 
-
 
29
    uint32_t salt = 0;
-
 
30
    size_t pos = 0;
-
 
31
 
-
 
32
    for (size_t i = 0; i < bytes.size(); ++i)
-
 
33
    {
-
 
34
        uint32_t b = static_cast<uint32_t>(bytes[i]);
-
 
35
 
-
 
36
        if (b == 0 && bytes.size() > 4)
-
 
37
            continue;
-
 
38
 
-
 
39
        salt |= (b << (pos * 8));
-
 
40
 
-
 
41
        if (pos >= 4)
-
 
42
            break;
-
 
43
 
-
 
44
        pos++;
-
 
45
    }
-
 
46
 
29
    mCrc32 = calculate_crc32c(0, bytes.data(), bytes.size());
47
    mCrc32 = calculate_crc32c(salt, bytes.data(), bytes.size());
30
}
48
}
31
 
49
 
32
uint32_t TCrc32::crc32c_sb8_64_bit(uint32_t crc, const unsigned char *p_buf, uint32_t length, uint32_t init_bytes)
50
uint32_t TCrc32::crc32c_sb8_64_bit(uint32_t crc, const unsigned char *p_buf, uint32_t length, uint32_t init_bytes)
33
{
51
{
-
 
52
    DECL_TRACER("TCrc32::crc32c_sb8_64_bit(uint32_t crc, const unsigned char *p_buf, uint32_t length, uint32_t init_bytes)");
-
 
53
 
34
    uint32_t li;
54
    uint32_t li;
35
    uint32_t term1, term2;
55
    uint32_t term1, term2;
36
    uint32_t running_length;
56
    uint32_t running_length;
37
    uint32_t end_bytes;
57
    uint32_t end_bytes;
38
 
58
 
Line 55... Line 75...
55
        {
75
        {
56
            crc ^= *(const uint32_t *) p_buf;
76
            crc ^= *(const uint32_t *) p_buf;
57
            p_buf += 4;
77
            p_buf += 4;
58
        }
78
        }
59
 
79
 
60
        term1 = sctp_crc_tableil8_o88[crc & 0x000000FF] ^
80
        term1 = sctp_crc_tableil8_o88[crc & 0x000000FF] ^ sctp_crc_tableil8_o80[(crc >> 8) & 0x000000FF];
61
        sctp_crc_tableil8_o80[(crc >> 8) & 0x000000FF];
-
 
62
        term2 = crc >> 16;
81
        term2 = crc >> 16;
63
        crc = term1 ^
-
 
64
        sctp_crc_tableil8_o72[term2 & 0x000000FF] ^
-
 
65
        sctp_crc_tableil8_o64[(term2 >> 8) & 0x000000FF];
82
        crc = term1 ^ sctp_crc_tableil8_o72[term2 & 0x000000FF] ^ sctp_crc_tableil8_o64[(term2 >> 8) & 0x000000FF];
66
 
83
 
67
        if (isBigEndian())
84
        if (isBigEndian())
68
        {
85
        {
69
            crc ^= sctp_crc_tableil8_o56[*p_buf++];
86
            crc ^= sctp_crc_tableil8_o56[*p_buf++];
70
            crc ^= sctp_crc_tableil8_o48[*p_buf++];
87
            crc ^= sctp_crc_tableil8_o48[*p_buf++];
71
            crc ^= sctp_crc_tableil8_o40[*p_buf++];
88
            crc ^= sctp_crc_tableil8_o40[*p_buf++];
72
            crc ^= sctp_crc_tableil8_o32[*p_buf++];
89
            crc ^= sctp_crc_tableil8_o32[*p_buf++];
73
        }
90
        }
74
        else
91
        else
75
        {
92
        {
76
            term1 = sctp_crc_tableil8_o56[(*(const uint32_t *) p_buf) & 0x000000FF] ^
93
            term1 = sctp_crc_tableil8_o56[(*(const uint32_t *) p_buf) & 0x000000FF] ^ sctp_crc_tableil8_o48[((*(const uint32_t *) p_buf) >> 8) & 0x000000FF];
77
            sctp_crc_tableil8_o48[((*(const uint32_t *) p_buf) >> 8) & 0x000000FF];
-
 
78
 
94
 
79
            term2 = (*(const uint32_t *) p_buf) >> 16;
95
            term2 = (*(const uint32_t *) p_buf) >> 16;
80
            crc = crc ^
-
 
81
            term1 ^
-
 
82
            sctp_crc_tableil8_o40[term2 & 0x000000FF] ^
-
 
83
            sctp_crc_tableil8_o32[(term2 >> 8) & 0x000000FF];
96
            crc = crc ^ term1 ^ sctp_crc_tableil8_o40[term2 & 0x000000FF] ^ sctp_crc_tableil8_o32[(term2 >> 8) & 0x000000FF];
84
            p_buf += 4;
97
            p_buf += 4;
85
        }
98
        }
86
    }
99
    }
87
 
100
 
88
    for (li = 0; li < end_bytes; li++)
101
    for (li = 0; li < end_bytes; li++)
Line 91... Line 104...
91
    return crc;
104
    return crc;
92
}
105
}
93
 
106
 
94
uint32_t TCrc32::multitable_crc32c(uint32_t crc32c, const unsigned char *buffer, unsigned int length)
107
uint32_t TCrc32::multitable_crc32c(uint32_t crc32c, const unsigned char *buffer, unsigned int length)
95
{
108
{
-
 
109
    DECL_TRACER("TCrc32::multitable_crc32c(uint32_t crc32c, const unsigned char *buffer, unsigned int length)");
-
 
110
 
96
    uint32_t to_even_word;
111
    uint32_t to_even_word;
97
 
112
 
98
    if (length == 0)
113
    if (length == 0)
99
        return (crc32c);
114
        return (crc32c);
100
 
115
 
101
    to_even_word = (4 - (((uintptr_t) buffer) & 0x3));
116
    to_even_word = (4 - (((uintptr_t) buffer) & 0x3));
102
    return (crc32c_sb8_64_bit(crc32c, buffer, length, to_even_word));
117
    return (crc32c_sb8_64_bit(crc32c, buffer, length, to_even_word));
103
}
118
}
104
 
119
 
105
uint32_t TCrc32::calculate_crc32c(uint32_t crc32c, const unsigned char *buffer, unsigned int length)
-
 
106
{
-
 
107
    if (length < 4)
-
 
108
        return (singletable_crc32c(crc32c, buffer, length));
-
 
109
    else
-
 
110
        return (multitable_crc32c(crc32c, buffer, length));
-
 
111
}
-
 
112
 
-
 
113
uint32_t TCrc32::singletable_crc32c(uint32_t crc, const void *buf, size_t size)
120
uint32_t TCrc32::singletable_crc32c(uint32_t crc, const void *buf, size_t size)
114
{
121
{
115
    const uint8_t *p = (uint8_t *)buf;
122
    DECL_TRACER("TCrc32::singletable_crc32c(uint32_t crc, const void *buf, size_t size)");
116
 
123
 
-
 
124
    const uint8_t *p = (uint8_t *)buf;
117
 
125
 
118
    while (size--)
126
    while (size--)
119
        crc = crc32Table[(crc ^ *p++) & 0xff] ^ (crc >> 8);
127
        crc = crc32Table[(crc ^ *p++) & 0xff] ^ (crc >> 8);
120
 
128
 
121
    return crc;
129
    return crc;
122
}
130
}
-
 
131
 
-
 
132
uint32_t TCrc32::calculate_crc32c(uint32_t crc32c, const unsigned char *buffer, unsigned int length)
-
 
133
{
-
 
134
    DECL_TRACER("TCrc32::calculate_crc32c(uint32_t crc32c, const unsigned char *buffer, unsigned int length)");
-
 
135
 
-
 
136
    if (!buffer || !length)
-
 
137
        return 0;
-
 
138
 
-
 
139
    std::stringstream s;
-
 
140
    s << std::setw(8) << std::setfill('0') << std::hex << crc32c;
-
 
141
    MSG_DEBUG("Calculating CRC32 with the salt " << s.str() << " and a length of " << length << " bytes.");
-
 
142
 
-
 
143
    if (length < 4)
-
 
144
        return (singletable_crc32c(crc32c, buffer, length));
-
 
145
    else
-
 
146
        return (multitable_crc32c(crc32c, buffer, length));
-
 
147
}