-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchecksum.c
More file actions
35 lines (34 loc) · 906 Bytes
/
Copy pathchecksum.c
File metadata and controls
35 lines (34 loc) · 906 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (int argc, char* argv[]){
int len = strlen(argv[1]);
char *post;
if (len % 4){
char temp[4-(len%4)];
len += 4-(len%4);
post = (char*)malloc((len)*sizeof(char));
memset(temp, '0', sizeof(temp));
post = strcat(argv[1], temp);
} else {
post = (char*)malloc((len)*sizeof(char));
post = argv[1];
}
int checksum = 0;
for (int i = 0; i < len/4; i ++){
int num = 0;
for (int j = 0; j < 4; j ++){
char now = post[i*4+j];
if (now <= '9'){
num = num * 16 + now - '0';
} else {
num = num * 16 + now - 'a' + 10;
}
}
checksum += num;
}
checksum += (checksum >> 16);
checksum &= 0xffff;
printf("%04x\n", (~checksum)&0xffff);
return 0;
}