Browse Source

proper crc32

Thomas Buck 1 year ago
parent
commit
535d325144
1 changed files with 7 additions and 2 deletions
  1. 7
    2
      src/mem.c

+ 7
- 2
src/mem.c View File

55
               "Config needs to fit inside a flash sector");
55
               "Config needs to fit inside a flash sector");
56
 
56
 
57
 static uint32_t calc_checksum(const struct mem_contents *data) {
57
 static uint32_t calc_checksum(const struct mem_contents *data) {
58
-    uint32_t c = 0x4223DEAD;
58
+    uint32_t c = 0xFFFFFFFF;
59
     const uint8_t *d = (const uint8_t *)data;
59
     const uint8_t *d = (const uint8_t *)data;
60
 
60
 
61
     const size_t offset_checksum = offsetof(struct mem_contents, checksum);
61
     const size_t offset_checksum = offsetof(struct mem_contents, checksum);
66
             continue;
66
             continue;
67
         }
67
         }
68
 
68
 
69
+        // adapted from "Hacker's Delight"
69
         c ^= d[i];
70
         c ^= d[i];
71
+        for (size_t j = 0; j < 8; j++) {
72
+            uint32_t mask = -(c & 1);
73
+            c = (c >> 1) ^ (0xEDB88320 & mask);
74
+        }
70
     }
75
     }
71
 
76
 
72
-    return c;
77
+    return ~c;
73
 }
78
 }
74
 
79
 
75
 void mem_init(void) {
80
 void mem_init(void) {

Loading…
Cancel
Save