A stack which works with or without an Mbed os library. Provides IPv4 or IPv6 with a full 1500 byte buffer.
Dependents: oldheating gps motorhome heating
checksum.c@200:5acbc41bf469, 2021-05-20 (annotated)
- Committer:
- andrewboyson
- Date:
- Thu May 20 14:32:52 2021 +0000
- Revision:
- 200:5acbc41bf469
- Parent:
- 136:8a65abb0dc63
Increased number of arp entries from 20 to 30 to accommodate the number of WIZ devices plus a few incoming port 80 calls from the internet.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 136:8a65abb0dc63 | 1 | #include <stdint.h> |
andrewboyson | 136:8a65abb0dc63 | 2 | |
andrewboyson | 136:8a65abb0dc63 | 3 | /* |
andrewboyson | 136:8a65abb0dc63 | 4 | Checksum is calculated on 2 byte chunks |
andrewboyson | 136:8a65abb0dc63 | 5 | All the routines below rely on the start being at a 2 byte boundary |
andrewboyson | 136:8a65abb0dc63 | 6 | It makes no sence for any, other than the last, call to accumulate the checksum to have an odd number of bytes |
andrewboyson | 136:8a65abb0dc63 | 7 | |
andrewboyson | 136:8a65abb0dc63 | 8 | Start the first call with a sum of zero. |
andrewboyson | 136:8a65abb0dc63 | 9 | Thereafter propagate it to each subsequent call. |
andrewboyson | 136:8a65abb0dc63 | 10 | Always call the Fin routine for the last call to roll back in any carries, to convert to 16 bit and to take the ones complement. |
andrewboyson | 136:8a65abb0dc63 | 11 | The Fin call can have an odd number of byes but cannot invert. |
andrewboyson | 136:8a65abb0dc63 | 12 | |
andrewboyson | 136:8a65abb0dc63 | 13 | The simple CheckSum is for when there is just one lump of data to calculate the checksum over. |
andrewboyson | 136:8a65abb0dc63 | 14 | |
andrewboyson | 136:8a65abb0dc63 | 15 | If the lump of data starts at an odd address the 2 byte cast will result in an unaligned access. |
andrewboyson | 136:8a65abb0dc63 | 16 | This is ok for the LPC1768 M3, it just takes it a bit longer, but could cause other processors to fault. |
andrewboyson | 136:8a65abb0dc63 | 17 | It should never happen as no protocol is not at least aligned on even bytes and the buffers all start aligned. |
andrewboyson | 136:8a65abb0dc63 | 18 | */ |
andrewboyson | 136:8a65abb0dc63 | 19 | |
andrewboyson | 136:8a65abb0dc63 | 20 | uint32_t CheckSumAddDirect(uint32_t sum, int count, void* pData) |
andrewboyson | 136:8a65abb0dc63 | 21 | { |
andrewboyson | 136:8a65abb0dc63 | 22 | uint16_t* p = (uint16_t*)pData; //Set up a 16 bit pointer for the data |
andrewboyson | 136:8a65abb0dc63 | 23 | |
andrewboyson | 136:8a65abb0dc63 | 24 | while(count > 1) |
andrewboyson | 136:8a65abb0dc63 | 25 | { |
andrewboyson | 136:8a65abb0dc63 | 26 | sum += *p++; // Add each pair of bytes into 32 bit accumulator |
andrewboyson | 136:8a65abb0dc63 | 27 | count -= 2; |
andrewboyson | 136:8a65abb0dc63 | 28 | } |
andrewboyson | 136:8a65abb0dc63 | 29 | if (count) sum += *(uint8_t*) p; // Add left-over byte, if any |
andrewboyson | 136:8a65abb0dc63 | 30 | return sum; |
andrewboyson | 136:8a65abb0dc63 | 31 | } |
andrewboyson | 136:8a65abb0dc63 | 32 | uint32_t CheckSumAddInvert(uint32_t sum, int count, void* pData) |
andrewboyson | 136:8a65abb0dc63 | 33 | { |
andrewboyson | 136:8a65abb0dc63 | 34 | uint16_t* p = (uint16_t*)pData; //Set up a 16 bit pointer for the data |
andrewboyson | 136:8a65abb0dc63 | 35 | |
andrewboyson | 136:8a65abb0dc63 | 36 | while(count > 1) |
andrewboyson | 136:8a65abb0dc63 | 37 | { |
andrewboyson | 136:8a65abb0dc63 | 38 | uint16_t value = (*p & 0xFF00) >> 8; // Invert the value |
andrewboyson | 136:8a65abb0dc63 | 39 | value |= (*p & 0x00FF) << 8; |
andrewboyson | 136:8a65abb0dc63 | 40 | p++; |
andrewboyson | 136:8a65abb0dc63 | 41 | sum += value; // Add each pair of bytes into 32 bit accumulator |
andrewboyson | 136:8a65abb0dc63 | 42 | count -= 2; |
andrewboyson | 136:8a65abb0dc63 | 43 | } |
andrewboyson | 136:8a65abb0dc63 | 44 | if (count) sum += *(uint8_t*) p; // Add left-over byte, if any |
andrewboyson | 136:8a65abb0dc63 | 45 | return sum; |
andrewboyson | 136:8a65abb0dc63 | 46 | } |
andrewboyson | 136:8a65abb0dc63 | 47 | uint16_t CheckSumFinDirect(uint32_t sum, int count, void* pData) |
andrewboyson | 136:8a65abb0dc63 | 48 | { |
andrewboyson | 136:8a65abb0dc63 | 49 | sum = CheckSumAddDirect(sum, count, pData); |
andrewboyson | 136:8a65abb0dc63 | 50 | while (sum>>16) sum = (sum & 0xffff) + (sum >> 16); // Add any carries from the sum back into the sum to make it ones complement |
andrewboyson | 136:8a65abb0dc63 | 51 | return ~sum; |
andrewboyson | 136:8a65abb0dc63 | 52 | } |
andrewboyson | 136:8a65abb0dc63 | 53 | uint16_t CheckSum(int count, void* pData) |
andrewboyson | 136:8a65abb0dc63 | 54 | { |
andrewboyson | 136:8a65abb0dc63 | 55 | return CheckSumFinDirect(0, count, pData); |
andrewboyson | 136:8a65abb0dc63 | 56 | } |