about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorRen Kararou <[email protected]>2024-12-20 17:25:44 -0600
committerRen Kararou <[email protected]>2024-12-20 17:25:44 -0600
commitc6240b9162f104583218c1100c18be00a5584ca1 (patch)
tree1a7f0dc60d5ff1368370c59a86baead6e954a7fa
parent91033b583a9b4adede6866a6baf23304edf28871 (diff)
downloadnbtpd-c6240b9162f104583218c1100c18be00a5584ca1.tar.gz
nbtpd-c6240b9162f104583218c1100c18be00a5584ca1.tar.bz2
nbtpd-c6240b9162f104583218c1100c18be00a5584ca1.zip
implement netascii
-rw-r--r--inc/netascii.h9
-rw-r--r--inc/placeholder.h5
-rw-r--r--src/netascii.c52
3 files changed, 61 insertions, 5 deletions
diff --git a/inc/netascii.h b/inc/netascii.h
new file mode 100644
index 0000000..096b1c3
--- /dev/null
+++ b/inc/netascii.h
@@ -0,0 +1,9 @@
+#ifndef NBD_NETASCII_H
+#define NBD_NETASCII_H
+#include <stdint.h>
+
+uint8_t is_netascii_char(char c);
+uint8_t is_netascii_str(char* str);
+uint8_t is_netascii_buf(char* buf, uint64_t len);
+
+#endif
diff --git a/inc/placeholder.h b/inc/placeholder.h
deleted file mode 100644
index 106cd4e..0000000
--- a/inc/placeholder.h
+++ /dev/null
@@ -1,5 +0,0 @@
-// This file is a placeholder so git keeps the inc/ folder.
-#ifndef NBTPD_PLACEHOLDER_H
-#define NBTPD_PLACEHOLDER_H
-
-#endif
diff --git a/src/netascii.c b/src/netascii.c
new file mode 100644
index 0000000..606d257
--- /dev/null
+++ b/src/netascii.c
@@ -0,0 +1,52 @@
+#include <stdint.h>
+
+#include "inc/netascii.h"
+
+uint8_t is_netascii_char(char c) {
+	static const uint64_t LUT[4] = 
+		{0xffffffff00003f81,
+		 0x7fffffffffffffff,
+		 0x00,0x00};
+	int i = c / 64;
+	int e = i % 64;
+	return (LUT[i] >> e) & 1;
+}
+
+/// WARNING!  This function is NOT safe.  Use only for strings you have already
+/// verified are NULL terminated!
+uint8_t is_netascii_str(char* str) {
+	for (uint64_t i = 0; str[i] != 0; i++) {
+		if (!is_netascii_char(str[i])) {
+			return 0;
+		} else {
+			if (str[i] == '\r') {
+				if (str[++i] != '\n') {
+					return 0;
+				}
+			}
+		}
+	}
+	return 1;
+}
+
+uint8_t is_netascii_buf(char* buf, uint64_t len) {
+	for (uint64_t i = 0; i < len; i++) {
+		if (!is_netascii_char(buf[i])) {
+			return 0;
+		} else {
+			if (buf[i] == '\r') {
+				if ((i + 1) < len) {
+					switch (buf[++i]) {
+						case 0: case '\n':
+							break;
+						default:
+							return 0;
+					}
+				} else {
+					return 0;
+				}
+			}
+		}
+	}
+	return 1;
+}