]> git.zerfleddert.de Git - rsbs2/blob - src/rsb-crc.c
make sure i2c rxbuf does not overrun
[rsbs2] / src / rsb-crc.c
1 #include <stdio.h>
2
3 #define POLY 0x04c11db7
4
5 unsigned int rsb_crc(unsigned int r11_crc, unsigned char *r10_buf, unsigned int r14_len) {
6 unsigned int r6_pos = 0;
7 unsigned int r3_data;
8 int r5_bit;
9
10 while (r6_pos < r14_len) {
11 r3_data = (*(r6_pos+r10_buf)) << 24;
12 r11_crc = r11_crc ^ r3_data;
13
14 r5_bit = 8;
15
16 do {
17 r3_data = r11_crc & 0x80000000;
18
19 if (r3_data != 0) {
20 r3_data = r11_crc << 1;
21 r11_crc = r3_data ^ POLY;
22 } else {
23 r11_crc = r11_crc << 1;
24 }
25 r5_bit--;
26 } while (r5_bit);
27
28 r6_pos++;
29 }
30
31 return r11_crc;
32 }
33
34 unsigned int rsb_crc2(unsigned char *r0_buf, unsigned int r1_buflen, unsigned int r2_magic, unsigned int *crc_out) {
35 unsigned int r4_len;
36 unsigned int file_crc;
37
38 r4_len = *(unsigned int*)(r0_buf + 0x20);
39
40 if (*((unsigned int*)(r0_buf + 0x24)) != r2_magic)
41 return 2; /* MAGIC does not match */
42
43 if (r1_buflen < r4_len)
44 return 3; /* image to small */
45
46 *crc_out = ~rsb_crc(~0x0, r0_buf, r4_len);
47
48 file_crc = *((unsigned int*)(r0_buf + r4_len));
49
50 if (file_crc != *crc_out)
51 return 4;
52
53 return 0;
54 }
Impressum, Datenschutz