summaryrefslogtreecommitdiff
path: root/zap/source/any
diff options
context:
space:
mode:
Diffstat (limited to 'zap/source/any')
-rw-r--r--zap/source/any/bs/trap.c16
-rw-r--r--zap/source/any/math/abs.c20
-rw-r--r--zap/source/any/math/div0.c22
-rw-r--r--zap/source/any/math/divmod.c24
-rw-r--r--zap/source/any/mem/cp.c16
-rw-r--r--zap/source/any/mem/eq.c19
-rw-r--r--zap/source/any/mem/fill.c15
-rw-r--r--zap/source/any/mem/srch.c18
-rw-r--r--zap/source/any/mem/utf8dec.c51
-rw-r--r--zap/source/any/mem/utf8declen.c32
-rw-r--r--zap/source/any/mem/utf8enc.c48
-rw-r--r--zap/source/any/mem/utf8enclen.c32
-rw-r--r--zap/source/any/mem/win1252dec.c111
-rw-r--r--zap/source/any/mem/win1252enc.c113
14 files changed, 537 insertions, 0 deletions
diff --git a/zap/source/any/bs/trap.c b/zap/source/any/bs/trap.c
new file mode 100644
index 0000000..9428d44
--- /dev/null
+++ b/zap/source/any/bs/trap.c
@@ -0,0 +1,16 @@
+/*
+ Copyright 2022-2023 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>.
+*/
+
+#include <zap/bs.h>
+
+void zap_priv_trap(void) {
+#if defined(__has_builtin)
+#if __has_builtin(__builtin_trap)
+ __builtin_trap();
+#endif
+#endif
+ for (;;) {}
+}
diff --git a/zap/source/any/math/abs.c b/zap/source/any/math/abs.c
new file mode 100644
index 0000000..d12b6a6
--- /dev/null
+++ b/zap/source/any/math/abs.c
@@ -0,0 +1,20 @@
+/*
+ Copyright 2022-2023 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>.
+*/
+
+#include <zap/math.h>
+
+#define zap_priv_abs(_wdth) \
+zap_i##_wdth zap_abs##_wdth(zap_i##_wdth##s const _val) { \
+ if (_val > 0x0) { \
+ return (zap_i##_wdth)_val; \
+ } \
+ return (zap_i##_wdth)(0x0 - _val); \
+}
+
+zap_priv_abs(8)
+zap_priv_abs(01)
+zap_priv_abs(02)
+zap_priv_abs(04)
diff --git a/zap/source/any/math/div0.c b/zap/source/any/math/div0.c
new file mode 100644
index 0000000..87fcfa0
--- /dev/null
+++ b/zap/source/any/math/div0.c
@@ -0,0 +1,22 @@
+/*
+ Copyright 2022-2023 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>.
+*/
+
+#include <zap/math.h>
+
+#include <csys.h>
+
+#if csys_os_linux
+#include <asm/unistd.h>
+#endif
+
+zap_i04 zap_priv_div0(void) {
+#if defined(zap_priv_signaldiv0)
+#if csys_os_linux
+ csys_syscall(__NR_kill,csys_syscall(__NR_getpid),0x8u);
+#endif
+#endif
+ return (zap_i04)-0x1;
+}
diff --git a/zap/source/any/math/divmod.c b/zap/source/any/math/divmod.c
new file mode 100644
index 0000000..4214651
--- /dev/null
+++ b/zap/source/any/math/divmod.c
@@ -0,0 +1,24 @@
+/*
+ Copyright 2022-2023 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>.
+*/
+
+#include <zap/math.h>
+
+#define zap_priv_divmod(_wdth) \
+zap_quotrem##_wdth zap_divmod##_wdth(zap_i##_wdth const _num,zap_i##_wdth const _den) { \
+zap_quotrem##_wdth quotrem; \
+ if (__builtin_expect(_den == 0x0,0x0)) { \
+ quotrem.quot = zap_priv_div0(); \
+ quotrem.rem = quotrem.quot; \
+ return quotrem; \
+ } \
+ for (quotrem = (zap_quotrem##_wdth){.quot = 0x0u,.rem = _num};quotrem.rem >= _den;++quotrem.quot,quotrem.rem -= _den) {} \
+ return quotrem; \
+}
+
+zap_priv_divmod(8)
+zap_priv_divmod(01)
+zap_priv_divmod(02)
+zap_priv_divmod(04)
diff --git a/zap/source/any/mem/cp.c b/zap/source/any/mem/cp.c
new file mode 100644
index 0000000..9bfd895
--- /dev/null
+++ b/zap/source/any/mem/cp.c
@@ -0,0 +1,16 @@
+/*
+ Copyright 2022-2023 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>.
+*/
+
+#include <zap/mem.h>
+
+void zap_cp(void * const zap_priv_restr _dest,void const * const zap_priv_restr _src,zap_sz const _num) {
+ zap_byte * dest;
+ zap_byte const * src;
+ zap_byte * const stop = (zap_byte *)_dest + _num;
+ for (dest = _dest,src = _src;dest != stop;++dest,++src) {
+ *dest = *src;
+ }
+}
diff --git a/zap/source/any/mem/eq.c b/zap/source/any/mem/eq.c
new file mode 100644
index 0000000..0229fb6
--- /dev/null
+++ b/zap/source/any/mem/eq.c
@@ -0,0 +1,19 @@
+/*
+ Copyright 2022-2023 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>.
+*/
+
+#include <zap/mem.h>
+
+zap_bool zap_eq(void const * const _lbuf,void const * const _rbuf,zap_sz const _num) {
+ zap_byte const * lbuf;
+ zap_byte const * rbuf;
+ zap_byte * const stop = (zap_byte *)_lbuf + _num;
+ for (lbuf = _lbuf,rbuf = _rbuf;lbuf != stop;++lbuf,++rbuf) {
+ if (*lbuf != *rbuf) {
+ return zap_false;
+ }
+ }
+ return zap_true;
+}
diff --git a/zap/source/any/mem/fill.c b/zap/source/any/mem/fill.c
new file mode 100644
index 0000000..11f5b59
--- /dev/null
+++ b/zap/source/any/mem/fill.c
@@ -0,0 +1,15 @@
+/*
+ Copyright 2022-2023 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>.
+*/
+
+#include <zap/mem.h>
+
+void zap_fill(void * const _dest,zap_byte const _val,zap_sz const _num) {
+ zap_byte * dest;
+ zap_byte * const stop = (zap_byte *)_dest + _num;
+ for (dest = _dest;dest != stop;++dest) {
+ *dest = _val;
+ }
+}
diff --git a/zap/source/any/mem/srch.c b/zap/source/any/mem/srch.c
new file mode 100644
index 0000000..494e2d8
--- /dev/null
+++ b/zap/source/any/mem/srch.c
@@ -0,0 +1,18 @@
+/*
+ Copyright 2022-2023 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>.
+*/
+
+#include <zap/mem.h>
+
+void * zap_srch(void const * const _buf,zap_byte const _val,zap_sz const _num) {
+ zap_byte const * buf;
+ zap_byte * const stop = (zap_byte *)_buf + _num;
+ for (buf = _buf;buf != stop;++buf) {
+ if (*buf == _val) {
+ return (void *)buf;
+ }
+ }
+ return zap_nullptr;
+}
diff --git a/zap/source/any/mem/utf8dec.c b/zap/source/any/mem/utf8dec.c
new file mode 100644
index 0000000..f9a0eac
--- /dev/null
+++ b/zap/source/any/mem/utf8dec.c
@@ -0,0 +1,51 @@
+/*
+ Copyright 2022-2023 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>.
+*/
+
+#include <zap/mem.h>
+
+void zap_utf8dec(zap_chr02 * const _dest,zap_chr8 const * const _src) {
+ zap_chr02 * dest;
+ zap_chr8 const * src;
+ for (dest = _dest,src = _src;;++dest) {
+ zap_chr8 const oct = *src;
+ if (oct == 0x0u) {
+ break;
+ }
+ if (oct >= 0xF0u) { /* Four octets. */
+ zap_chr02 chr = ((zap_chr02)oct ^ 0xF0u) << 0x12u;
+ ++src;
+ chr += ((zap_chr02)*src ^ 0x80u) << 0xCu;
+ ++src;
+ chr += ((zap_chr02)*src ^ 0x80u) << 0x6u;
+ ++src;
+ chr += (zap_chr02)*src ^ 0x80u;
+ ++src;
+ *dest = chr;
+ continue;
+ }
+ if (oct >= 0xE0u) { /* Three octets. */
+ zap_chr02 chr = ((zap_chr02)oct ^ 0xE0u) << 0xCu;
+ ++src;
+ chr += ((zap_chr02)*src ^ 0x80u) << 0x6u;
+ ++src;
+ chr += (zap_chr02)*src ^ 0x80u;
+ ++src;
+ *dest = chr;
+ continue;
+ }
+ if (oct >= 0xC0u) { /* Two octets. */
+ zap_chr02 chr = ((zap_chr02)oct ^ 0xC0u) << 0x6u;
+ ++src;
+ chr += (zap_chr02)*src ^ 0x80u;
+ ++src;
+ *dest = chr;
+ continue;
+ }
+ /* One octet. */
+ *dest = oct;
+ ++src;
+ }
+}
diff --git a/zap/source/any/mem/utf8declen.c b/zap/source/any/mem/utf8declen.c
new file mode 100644
index 0000000..81fd274
--- /dev/null
+++ b/zap/source/any/mem/utf8declen.c
@@ -0,0 +1,32 @@
+/*
+ Copyright 2022-2023 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>.
+*/
+
+#include <zap/mem.h>
+
+zap_sz zap_utf8declen(zap_chr8 const * const _buf) {
+ zap_sz len = 0x0u;
+ zap_chr8 const * pos;
+ for (pos = _buf;;++len) {
+ zap_chr8 const oct = *pos;
+ if (oct == 0x0u) {
+ break;
+ }
+ if (oct >= 0xF0u) {
+ pos += 0x4u;
+ continue;
+ }
+ if (oct >= 0xE0u) {
+ pos += 0x3u;
+ continue;
+ }
+ if (oct >= 0xC0u) {
+ pos += 0x2u;
+ continue;
+ }
+ ++pos;
+ }
+ return len;
+}
diff --git a/zap/source/any/mem/utf8enc.c b/zap/source/any/mem/utf8enc.c
new file mode 100644
index 0000000..04febb2
--- /dev/null
+++ b/zap/source/any/mem/utf8enc.c
@@ -0,0 +1,48 @@
+/*
+ Copyright 2022-2023 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>.
+*/
+
+#include <zap/mem.h>
+
+void zap_utf8enc(zap_chr8 * const _dest,zap_chr02 const * const _src) {
+ zap_chr8 * dest;
+ zap_chr02 const * src;
+ for (dest = _dest,src = _src;;++src) {
+ zap_chr02 const chr = *src;
+ if (chr > 0xFFFFu) { /* Four octets. */
+ *dest = 0xF0u + (chr >> 0x12u);
+ ++dest;
+ *dest = 0x80u + (chr >> 0xCu & 0x3Fu);
+ ++dest;
+ *dest = 0x80u + (chr >> 0x6u & 0x3Fu);
+ ++dest;
+ *dest = 0x80u + (chr & 0x3Fu);
+ ++dest;
+ continue;
+ }
+ if (chr >= 0x7FFu) { /* Three octets. */
+ *dest = 0xE0u + (zap_chr8)(chr >> 0xCu);
+ ++dest;
+ *dest = 0x80u + (zap_chr8)(chr >> 0x6u & 0x3Fu);
+ ++dest;
+ *dest = 0x80u + (zap_chr8)(chr & 0x3Fu);
+ ++dest;
+ continue;
+ }
+ if (chr >= 0x7Fu) { /* Two octets. */
+ *dest = 0xC0u + (zap_chr8)(chr >> 0x6u);
+ ++dest;
+ *dest = 0x80u + (zap_chr8)(chr & 0x3Fu);
+ ++dest;
+ continue;
+ }
+ /* One octet. */
+ *dest = chr;
+ ++dest;
+ if (chr == 0x0u) {
+ break;
+ }
+ }
+}
diff --git a/zap/source/any/mem/utf8enclen.c b/zap/source/any/mem/utf8enclen.c
new file mode 100644
index 0000000..67d938d
--- /dev/null
+++ b/zap/source/any/mem/utf8enclen.c
@@ -0,0 +1,32 @@
+/*
+ Copyright 2022-2023 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>.
+*/
+
+#include <zap/mem.h>
+
+zap_sz zap_utf8enclen(zap_chr02 const * const _buf) {
+ zap_sz len = 0x0u;
+ zap_chr02 const * pos;
+ for (pos = _buf;;++pos) {
+ zap_chr02 const chr = *pos;
+ if (chr == 0x0u) {
+ break;
+ }
+ if (chr >= 0x10000u) {
+ len += 0x4u;
+ continue;
+ }
+ if (chr >= 0x800u) {
+ len += 0x3u;
+ continue;
+ }
+ if (chr >= 0x80u) {
+ len += 0x2u;
+ continue;
+ }
+ ++len;
+ }
+ return len;
+}
diff --git a/zap/source/any/mem/win1252dec.c b/zap/source/any/mem/win1252dec.c
new file mode 100644
index 0000000..b808c9b
--- /dev/null
+++ b/zap/source/any/mem/win1252dec.c
@@ -0,0 +1,111 @@
+/*
+ Copyright 2022-2023 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>.
+*/
+
+#include <zap/mem.h>
+
+void zap_win1252dec(zap_chr02 * const _dest,zap_chr8 const * const _src) {
+ zap_chr02 * dest;
+ zap_chr8 const * src;
+ for (dest = _dest,src = _src;;++src,++dest) {
+ zap_chr8 const chr = *src;
+ if (chr == 0x0u) {
+ break;
+ }
+ switch (chr) {
+ default:
+ *dest = *src;
+ break;
+ case 0x81: /* Bad characters. */
+ case 0x8D:
+ case 0x8F:
+ case 0x90:
+ case 0x9D:
+ *dest = 0xFFFDu; /* REPLACEMENT CHARACTER */
+ break;
+ case 0x80:
+ *dest = 0x20ACu;
+ break;
+ case 0x82:
+ *dest = 0x201Au;
+ break;
+ case 0x83:
+ *dest = 0x192u;
+ break;
+ case 0x84:
+ *dest = 0x201Eu;
+ break;
+ case 0x85:
+ *dest = 0x2026u;
+ break;
+ case 0x86:
+ *dest = 0x2020u;
+ break;
+ case 0x87:
+ *dest = 0x2021u;
+ break;
+ case 0x88:
+ *dest = 0x2C6u;
+ break;
+ case 0x89:
+ *dest = 0x2030u;
+ break;
+ case 0x8A:
+ *dest = 0x160u;
+ break;
+ case 0x8B:
+ *dest = 0x2039u;
+ break;
+ case 0x8C:
+ *dest = 0x152u;
+ break;
+ case 0x8E:
+ *dest = 0x17Du;
+ break;
+ case 0x91:
+ *dest = 0x2018u;
+ break;
+ case 0x92:
+ *dest = 0x2019u;
+ break;
+ case 0x93:
+ *dest = 0x201Cu;
+ break;
+ case 0x94:
+ *dest = 0x201Du;
+ break;
+ case 0x95:
+ *dest = 0x2022u;
+ break;
+ case 0x96:
+ *dest = 0x2013u;
+ break;
+ case 0x97:
+ *dest = 0x2014u;
+ break;
+ case 0x98:
+ *dest = 0x2DCu;
+ break;
+ case 0x99:
+ *dest = 0x2122u;
+ break;
+ case 0x9A:
+ *dest = 0x161u;
+ break;
+ case 0x9B:
+ *dest = 0x203Au;
+ break;
+ case 0x9C:
+ *dest = 0x153u;
+ break;
+ case 0x9E:
+ *dest = 0x17Eu;
+ break;
+ case 0x9F:
+ *dest = 0x178u;
+ break;
+ }
+ }
+}
diff --git a/zap/source/any/mem/win1252enc.c b/zap/source/any/mem/win1252enc.c
new file mode 100644
index 0000000..f773540
--- /dev/null
+++ b/zap/source/any/mem/win1252enc.c
@@ -0,0 +1,113 @@
+/*
+ Copyright 2022-2023 Gabriel Jensen.
+ This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
+ If a copy of the MPL was not distributed with this file, You can obtain one at <https://mozilla.org/MPL/2.0>.
+*/
+
+#include <zap/mem.h>
+
+void zap_win1252enc(zap_chr8 * const _dest,zap_chr02 const * const _src) {
+ zap_chr8 * dest;
+ zap_chr02 const * src;
+ for (dest = _dest,src = _src;;++src,++dest) {
+ zap_chr02 const chr = *src;
+ if (chr == 0x0u) {
+ break;
+ }
+ zap_chr8 const bad = 0x3Fu;
+ switch (chr) {
+ default:
+ if (chr > 0xFFu) {
+ *dest = bad;
+ break;
+ }
+ if (chr >= 0x80u && chr <= 0x9Fu) {
+ *dest = bad;
+ break;
+ }
+ *dest = *src;
+ break;
+ case 0x20ACu:
+ *dest = 0x80u;
+ break;
+ case 0x201Au:
+ *dest = 0x82u;
+ break;
+ case 0x192u:
+ *dest = 0x83u;
+ break;
+ case 0x201Eu:
+ *dest = 0x84u;
+ break;
+ case 0x2026u:
+ *dest = 0x85u;
+ break;
+ case 0x2020u:
+ *dest = 0x86u;
+ break;
+ case 0x2021u:
+ *dest = 0x87u;
+ break;
+ case 0x2C6u:
+ *dest = 0x88u;
+ break;
+ case 0x2030u:
+ *dest = 0x89u;
+ break;
+ case 0x160u:
+ *dest = 0x8Au;
+ break;
+ case 0x2039u:
+ *dest = 0x8Bu;
+ break;
+ case 0x152u:
+ *dest = 0x8Cu;
+ break;
+ case 0x17Du:
+ *dest = 0x8Eu;
+ break;
+ case 0x2018u:
+ *dest = 0x91u;
+ break;
+ case 0x2019u:
+ *dest = 0x92u;
+ break;
+ case 0x201Cu:
+ *dest = 0x93u;
+ break;
+ case 0x201Du:
+ *dest = 0x94u;
+ break;
+ case 0x2022u:
+ *dest = 0x95u;
+ break;
+ case 0x2013u:
+ *dest = 0x96u;
+ break;
+ case 0x2014u:
+ *dest = 0x97u;
+ break;
+ case 0x2DCu:
+ *dest = 0x98u;
+ break;
+ case 0x2122u:
+ *dest = 0x99u;
+ break;
+ case 0x161u:
+ *dest = 0x9Au;
+ break;
+ case 0x203Au:
+ *dest = 0x9Bu;
+ break;
+ case 0x153u:
+ *dest = 0x9Cu;
+ break;
+ case 0x17Eu:
+ *dest = 0x9Eu;
+ break;
+ case 0x178u:
+ *dest = 0x9Fu;
+ break;
+ }
+ }
+}