]> cvs.zerfleddert.de Git - rsbs2/blob - rsb-crc.c
make crc implementation a bit more readable
[rsbs2] / rsb-crc.c
1 #define POLY 0x04c11db7
2
3 /* Theory of operation:
4 * (arm-elf-objdump -b binary -m arm -M reg-names-raw -D RSB_S2_SINGLE.bin)
5 *
6 * 440: push {r4, r5, r6, r7, r8, r9, r10, r11, r14}
7 * 444: mov r11, r0
8 * 448: mov r10, r1
9 * 44c: mov r14, r2
10 * 450: mov r6, #0 ; 0x0
11 * 454: b 0x4a0
12 * 458: add r3, r6, r10
13 * 45c: ldrb r3, [r3]
14 * 460: lsl r3, r3, #24
15 * 464: eor r11, r11, r3
16 * 468: mov r5, #8 ; 0x8
17 * 46c: and r3, r11, #-2147483648 ; 0x80000000
18 * 470: cmp r3, #0 ; 0x0
19 * 474: beq 0x48c
20 * 478: lsl r3, r11, #1
21 * 47c: ldr r12, [pc, #64] ; 0x4c4
22 * 480: eor r0, r3, r12
23 * 484: mov r11, r0
24 * 488: b 0x490
25 * 48c: lsl r11, r11, #1
26 * 490: sub r5, r5, #1 ; 0x1
27 * 494: cmp r5, #0 ; 0x0
28 * 498: bne 0x46c
29 * 49c: add r6, r6, #1 ; 0x1
30 * 4a0: cmp r6, r14
31 * 4a4: blt 0x458
32 * 4a8: mov r0, r11
33 * 4ac: pop {r4, r5, r6, r7, r8, r9, r10, r11, r15}
34 * 4c4: DATA: 0x04c11db7
35 */
36
37 unsigned int rsb_crc(unsigned int r11_crc, unsigned char *r10_buf, unsigned int r14_len) {
38 unsigned int r6_pos = 0;
39 unsigned int r3_data;
40 int r5_bit;
41
42 while (r6_pos < r14_len) {
43 r3_data = (*(r6_pos+r10_buf)) << 24;
44 r11_crc = r11_crc ^ r3_data;
45
46 r5_bit = 8;
47
48 do {
49 r3_data = r11_crc & 0x80000000;
50
51 if (r3_data != 0) {
52 r3_data = r11_crc << 1;
53 r11_crc = r3_data ^ POLY;
54 } else {
55 r11_crc = r11_crc << 1;
56 }
57 r5_bit--;
58 } while (r5_bit);
59
60 r6_pos++;
61 }
62
63 return r11_crc;
64 }
Impressum, Datenschutz