[an error occurred while processing this directive]
|
Вы я вижу всё-таки занялись этим неблагодарным делом.
Вот пример подсчёта CRC для PPP:
// Add next character to the CRC checksum for PPP packets
unsigned int calc(uint c) {
char i; // Just a loop index
c &= 0xFF; // Only calculate CRC on low byte
for (i=0;i<8;i++) { // Loop eight times, once for each bit
if (c&1) { // Is bit high?
c /= 2; // Position for next bit
c ^= 0x8408; // Toggle the feedback bits
} else c /= 2; // Just position for next bit
} // This routine would be best optimized in assembly
return c; // Return the 16 bit checksum
}
// Add character to the new packet
void add(byte c) {
checksum = calc((c^checksum)&0xff) ^ (checksum/256); // Add CRC from this char to running total
// Create packet
void MakePacket(uint packet, byte cod, byte num, byte *str, byte length) {
checksum = 0xFFFF; // Initialize checksum
*(byte*)(&tmp) = 0x7E;
if (packet == LCP) {
add(0xFF); // Insert PPP header OxFF
add(3); // Insert PPP header 0x03
}
.
.
.
.
while (length--) // copy the whole string into packet
add(*str++); // add current character to packet
tmp = ~checksum; // invert the checksum
add(tmp&255); // Insert checksum msb
add(tmp/256); // Insert checksum lsb
.
.
.