summaryrefslogtreecommitdiff
path: root/src/user/app/netstack/util.c
diff options
context:
space:
mode:
authordzwdz2022-08-21 22:33:09 +0200
committerdzwdz2022-08-21 22:33:09 +0200
commite35d6a4fde9a0671bc7d2527ff6b55b0ce1b4b1e (patch)
treebeb45a1ea334c18a29297684c009e9fb479bebeb /src/user/app/netstack/util.c
parentdc5098f83ac55722744a97d2950f50ef2a221f1a (diff)
user: rename ethdump to netstack
Diffstat (limited to 'src/user/app/netstack/util.c')
-rw-r--r--src/user/app/netstack/util.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/user/app/netstack/util.c b/src/user/app/netstack/util.c
new file mode 100644
index 0000000..7c484f3
--- /dev/null
+++ b/src/user/app/netstack/util.c
@@ -0,0 +1,31 @@
+#include "util.h"
+
+/* https://www.w3.org/TR/PNG/#D-CRCAppendix */
+static uint32_t crc_table[256];
+uint32_t crc32(const uint8_t *buf, size_t len) {
+ if (!crc_table[1]) {
+ for (int i = 0; i < 256; i++) {
+ uint32_t c = i;
+ for (int j = 0; j < 8; j++)
+ c = ((c&1) ? 0xedb88320 : 0) ^ (c >> 1);
+ crc_table[i] = c;
+ }
+ }
+
+ uint32_t c = 0xFFFFFFFF;
+ for (size_t i = 0; i < len; i++)
+ c = crc_table[(c ^ buf[i]) & 0xff] ^ (c >> 8);
+ return ~c;
+}
+
+uint16_t ip_checksum(const uint8_t *buf, size_t len) {
+ uint32_t c = 0;
+ while (len >= 2) {
+ c += nget16(buf);
+ buf += 2; len -= 2;
+ }
+ if (len) c += (*buf) << 8;
+ while (c >= 0xFFFF)
+ c = (c & 0xFFFF) + (c >> 16);
+ return ~c;
+}