--- /dev/null
+#include <stdio.h>
+
+#define POLY 0x04c11db7
+
+unsigned int rsb_crc(unsigned int r11_crc, unsigned char *r10_buf, unsigned int r14_len) {
+ unsigned int r6_pos = 0;
+ unsigned int r3_data;
+ int r5_bit;
+
+ while (r6_pos < r14_len) {
+ r3_data = (*(r6_pos+r10_buf)) << 24;
+ r11_crc = r11_crc ^ r3_data;
+
+ r5_bit = 8;
+
+ do {
+ r3_data = r11_crc & 0x80000000;
+
+ if (r3_data != 0) {
+ r3_data = r11_crc << 1;
+ r11_crc = r3_data ^ POLY;
+ } else {
+ r11_crc = r11_crc << 1;
+ }
+ r5_bit--;
+ } while (r5_bit);
+
+ r6_pos++;
+ }
+
+ return r11_crc;
+}
+
+unsigned int rsb_crc2(unsigned char *r0_buf, unsigned int r1_buflen, unsigned int r2_magic, unsigned int *crc_out) {
+ unsigned int r4_len;
+ unsigned int file_crc;
+
+ r4_len = *(unsigned int*)(r0_buf + 0x20);
+
+ if (*((unsigned int*)(r0_buf + 0x24)) != r2_magic)
+ return 2; /* MAGIC does not match */
+
+ if (r1_buflen < r4_len)
+ return 3; /* image to small */
+
+ *crc_out = ~rsb_crc(~0x0, r0_buf, r4_len);
+
+ file_crc = *((unsigned int*)(r0_buf + r4_len));
+
+ if (file_crc != *crc_out)
+ return 4;
+
+ return 0;
+}