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,7 +55,7 @@ static_assert(sizeof(struct mem_contents) < FLASH_SECTOR_SIZE,
55 55
               "Config needs to fit inside a flash sector");
56 56
 
57 57
 static uint32_t calc_checksum(const struct mem_contents *data) {
58
-    uint32_t c = 0x4223DEAD;
58
+    uint32_t c = 0xFFFFFFFF;
59 59
     const uint8_t *d = (const uint8_t *)data;
60 60
 
61 61
     const size_t offset_checksum = offsetof(struct mem_contents, checksum);
@@ -66,10 +66,15 @@ static uint32_t calc_checksum(const struct mem_contents *data) {
66 66
             continue;
67 67
         }
68 68
 
69
+        // adapted from "Hacker's Delight"
69 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 80
 void mem_init(void) {

Loading…
Cancel
Save