瀏覽代碼

proper crc32

Thomas Buck 1 年之前
父節點
當前提交
535d325144
共有 1 個檔案被更改,包括 7 行新增2 行删除
  1. 7
    2
      src/mem.c

+ 7
- 2
src/mem.c 查看文件

@@ -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…
取消
儲存