aboutsummaryrefslogtreecommitdiff
path: root/octedit/crc32.c
diff options
context:
space:
mode:
authorJacques Comeaux <jacquesrcomeaux@protonmail.com>2024-07-01 12:16:02 -0500
committerJacques Comeaux <jacquesrcomeaux@protonmail.com>2024-07-01 12:16:02 -0500
commit83aef192e447c800cba6f4dbde8a450a4f8581a5 (patch)
treec5b21bb6238d9117057f3433d4122d66f4add9f8 /octedit/crc32.c
parent4e018fc5bdd4ff9ee6065a5167fa291404e20576 (diff)
Use in-house CRC32 computation for boot sector
Diffstat (limited to 'octedit/crc32.c')
-rw-r--r--octedit/crc32.c22
1 files changed, 22 insertions, 0 deletions
diff --git a/octedit/crc32.c b/octedit/crc32.c
new file mode 100644
index 0000000..175ab50
--- /dev/null
+++ b/octedit/crc32.c
@@ -0,0 +1,22 @@
+#include <stdio.h>
+#include <unistd.h>
+#include <stdint.h>
+
+#define DATA_LEN 252
+#define DIVISOR 0x04C11DB7
+
+int main() {
+ uint8_t data[DATA_LEN];
+ read(STDIN_FILENO, data, DATA_LEN);
+ uint32_t crc = 0xFFFFFFFF;
+ for (size_t i = 0; i < DATA_LEN; i++) {
+ crc ^= data[i] << 24;
+ for (uint8_t j = 0; j < 8; j++) {
+ uint8_t nskip = crc >> 31;
+ crc <<= 1;
+ if (nskip) crc ^= DIVISOR;
+ }
+ }
+ write(STDOUT_FILENO, (uint8_t *) &crc, 4);
+ return 0;
+}