Project

General

Profile

Bug #13929 » main.c

Nizam Moidu, 02/17/2023 12:15 PM

 
1
#include <stdio.h>
2
#include <stdint.h>
3
char* payload  = "\x46\xc0\x00\x20\xe3\xf1\x00\x00\x01\x02\x67\x7a\xc0\xa8\x38\x02" \
4
"\xe0\x00\x00\x01\x94\x04\x00\x00\x11\x64\x00\x00\x00\x00\x00\x00";
5

    
6
uint16_t inetChksum(uint16_t *addr, int len) {
7
    register int nleft = len;
8
    register uint16_t *w = addr;
9
    uint16_t answer = 0;
10
    register int32_t sum = 0;
11

    
12
    /*
13
     *  Our algorithm is simple, using a 32 bit accumulator (sum),
14
     *  we add sequential 16 bit words to it, and at the end, fold
15
     *  back all the carry bits from the top 16 bits into the lower
16
     *  16 bits.
17
     */
18
    while (nleft > 1) {
19
        sum += *w++;
20
        nleft -= 2;
21
    }
22

    
23
    /* mop up an odd byte, if necessary */
24
    if (nleft == 1) {
25
        *(uint8_t *) (&answer) = *(uint8_t *)w ;
26
        sum += answer;
27
    }
28

    
29
    /*
30
     * add back carry outs from top 16 bits to low 16 bits
31
     */
32
    sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
33
    sum += (sum >> 16);                 /* add carry */
34
    answer = ~sum;                      /* truncate to 16 bits */
35
    return(answer);
36
}
37

    
38
int main() {
39
    uint16_t ck = inetChksum(payload, 32);
40
    printf("check sum is %#04x\n", ck);
41
    return 0;
42
}
(3-3/7)