summaryrefslogtreecommitdiff
path: root/na/include
diff options
context:
space:
mode:
authorRichard Qin <richard.qin.x@gmail.com>2026-04-21 23:16:50 +0800
committerRichard Qin <richard.qin.x@gmail.com>2026-04-21 23:16:50 +0800
commit5278f6def4248391a481382bd7b0042b04c829f2 (patch)
tree6b41347c2675dfda2da34c3aec6cea11ad033fe0 /na/include
parente7b6e9453800ecf15bee5f8f1763fd8c4862f70c (diff)
downloadlume-5278f6def4248391a481382bd7b0042b04c829f2.tar.gz
lume-5278f6def4248391a481382bd7b0042b04c829f2.tar.xz
feat: add string manipulation functions derived from musl libc
This commit introduces a variety of string manipulation functions, including: - `memmem`: Searches for a substring in a memory area. - `memmove`: Safely copies memory areas, handling overlaps. - `mempcpy`: Copies memory and returns a pointer to the end of the destination. - `memrchr`: Finds the last occurrence of a character in a memory area. - `memset`: Fills a memory area with a specified value. - `rindex`: Returns the last occurrence of a character in a string. - `stpcpy` and `stpncpy`: Copies strings and returns a pointer to the end. - `strcat`, `strchr`, `strcspn`, `strlcat`, `strlcpy`, `strlen`, `strncat`, `strncmp`, `strncpy`, `strnlen`, `strpbrk`, `strrchr`, `strstr`, `strtok`, and `strtok_r`: Various string manipulation functions for concatenation, searching, and tokenization. Assembly implementations for `memcpy`, `memmove`, and `memset` are also included for optimized performance on x86_64 architectures.
Diffstat (limited to 'na/include')
-rw-r--r--na/include/libc-shim/bits/alltypes.h15
-rw-r--r--na/include/libc-shim/endian.h9
-rw-r--r--na/include/libc-shim/features.h12
-rw-r--r--na/include/libc-shim/limits.h17
-rw-r--r--na/include/libc-shim/stdint.h21
-rw-r--r--na/include/libc-shim/string.h110
-rw-r--r--na/include/libc-shim/strings.h40
7 files changed, 224 insertions, 0 deletions
diff --git a/na/include/libc-shim/bits/alltypes.h b/na/include/libc-shim/bits/alltypes.h
new file mode 100644
index 0000000..cf21a5e
--- /dev/null
+++ b/na/include/libc-shim/bits/alltypes.h
@@ -0,0 +1,15 @@
+/* Shim: provide types musl string functions need */
+#ifndef _BITS_ALLTYPES_H
+#define _BITS_ALLTYPES_H
+
+#ifndef __cplusplus
+typedef unsigned long size_t;
+#endif
+
+typedef long ssize_t;
+typedef unsigned long uintptr_t;
+
+/* locale_t stub — unused in kernel, but declared by some headers */
+typedef void *locale_t;
+
+#endif
diff --git a/na/include/libc-shim/endian.h b/na/include/libc-shim/endian.h
new file mode 100644
index 0000000..8346386
--- /dev/null
+++ b/na/include/libc-shim/endian.h
@@ -0,0 +1,9 @@
+/* Shim: endianness for musl string functions */
+#ifndef _ENDIAN_H
+#define _ENDIAN_H
+
+#define __LITTLE_ENDIAN 1234
+#define __BIG_ENDIAN 4321
+#define __BYTE_ORDER __LITTLE_ENDIAN /* RISC-V is LE */
+
+#endif
diff --git a/na/include/libc-shim/features.h b/na/include/libc-shim/features.h
new file mode 100644
index 0000000..a3466c8
--- /dev/null
+++ b/na/include/libc-shim/features.h
@@ -0,0 +1,12 @@
+/* Shim: musl expects this header */
+#ifndef _FEATURES_H
+#define _FEATURES_H
+
+/* weak_alias: musl uses this for symbol aliasing (e.g. __memrchr -> memrchr) */
+#define weak_alias(old, new) \
+ extern __typeof(old) new __attribute__((__weak__, __alias__(#old)))
+
+/* hidden attribute used by some musl sources */
+#define hidden __attribute__((__visibility__("hidden")))
+
+#endif
diff --git a/na/include/libc-shim/limits.h b/na/include/libc-shim/limits.h
new file mode 100644
index 0000000..a1d546e
--- /dev/null
+++ b/na/include/libc-shim/limits.h
@@ -0,0 +1,17 @@
+/* Shim: limits for musl string functions (memmem uses SIZE_MAX) */
+#ifndef _LIMITS_H
+#define _LIMITS_H
+
+#define CHAR_BIT 8
+#define UCHAR_MAX 255
+#define SCHAR_MAX 127
+#define SCHAR_MIN (-128)
+#define INT_MAX 2147483647
+#define INT_MIN (-INT_MAX - 1)
+#define UINT_MAX 4294967295U
+#define LONG_MAX 9223372036854775807L
+#define LONG_MIN (-LONG_MAX - 1L)
+#define ULONG_MAX 18446744073709551615UL
+#define SIZE_MAX ULONG_MAX
+
+#endif
diff --git a/na/include/libc-shim/stdint.h b/na/include/libc-shim/stdint.h
new file mode 100644
index 0000000..650bdcf
--- /dev/null
+++ b/na/include/libc-shim/stdint.h
@@ -0,0 +1,21 @@
+/* Shim: stdint types for musl string functions */
+#ifndef _STDINT_H
+#define _STDINT_H
+
+typedef unsigned char uint8_t;
+typedef unsigned short uint16_t;
+typedef unsigned int uint32_t;
+typedef unsigned long uint64_t;
+typedef signed char int8_t;
+typedef signed short int16_t;
+typedef signed int int32_t;
+typedef signed long int64_t;
+typedef unsigned long uintptr_t;
+typedef long intptr_t;
+
+#define UINT8_MAX 0xFF
+#define UINT16_MAX 0xFFFF
+#define UINT32_MAX 0xFFFFFFFFU
+#define UINT64_MAX 0xFFFFFFFFFFFFFFFFUL
+
+#endif
diff --git a/na/include/libc-shim/string.h b/na/include/libc-shim/string.h
new file mode 100644
index 0000000..8a86304
--- /dev/null
+++ b/na/include/libc-shim/string.h
@@ -0,0 +1,110 @@
+#ifndef _STRING_H
+#define _STRING_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <features.h>
+
+#if __cplusplus >= 201103L
+#define NULL nullptr
+#elif defined(__cplusplus)
+#define NULL 0L
+#else
+#define NULL ((void*)0)
+#endif
+
+#define __NEED_size_t
+#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
+ || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
+ || defined(_BSD_SOURCE)
+#define __NEED_locale_t
+#endif
+
+#include <bits/alltypes.h>
+
+void *memcpy (void *__restrict, const void *__restrict, size_t);
+void *memmove (void *, const void *, size_t);
+void *memset (void *, int, size_t);
+int memcmp (const void *, const void *, size_t);
+void *memchr (const void *, int, size_t);
+
+char *strcpy (char *__restrict, const char *__restrict);
+char *strncpy (char *__restrict, const char *__restrict, size_t);
+
+char *strcat (char *__restrict, const char *__restrict);
+char *strncat (char *__restrict, const char *__restrict, size_t);
+
+int strcmp (const char *, const char *);
+int strncmp (const char *, const char *, size_t);
+
+int strcoll (const char *, const char *);
+size_t strxfrm (char *__restrict, const char *__restrict, size_t);
+
+char *strchr (const char *, int);
+char *strrchr (const char *, int);
+
+size_t strcspn (const char *, const char *);
+size_t strspn (const char *, const char *);
+char *strpbrk (const char *, const char *);
+char *strstr (const char *, const char *);
+char *strtok (char *__restrict, const char *__restrict);
+
+size_t strlen (const char *);
+
+char *strerror (int);
+
+#if defined(_BSD_SOURCE) || defined(_GNU_SOURCE)
+#include <strings.h>
+#endif
+
+#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) \
+ || defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
+ || defined(_BSD_SOURCE)
+char *strtok_r (char *__restrict, const char *__restrict, char **__restrict);
+int strerror_r (int, char *, size_t);
+char *stpcpy(char *__restrict, const char *__restrict);
+char *stpncpy(char *__restrict, const char *__restrict, size_t);
+size_t strnlen (const char *, size_t);
+char *strdup (const char *);
+char *strndup (const char *, size_t);
+char *strsignal(int);
+char *strerror_l (int, locale_t);
+int strcoll_l (const char *, const char *, locale_t);
+size_t strxfrm_l (char *__restrict, const char *__restrict, size_t, locale_t);
+void *memmem(const void *, size_t, const void *, size_t);
+#endif
+
+#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) \
+ || defined(_BSD_SOURCE)
+void *memccpy (void *__restrict, const void *__restrict, int, size_t);
+#endif
+
+#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
+char *strsep(char **, const char *);
+size_t strlcat (char *, const char *, size_t);
+size_t strlcpy (char *, const char *, size_t);
+void explicit_bzero (void *, size_t);
+#endif
+
+#ifdef _GNU_SOURCE
+#define strdupa(x) strcpy(alloca(strlen(x)+1),x)
+int strverscmp (const char *, const char *);
+char *strchrnul(const char *, int);
+char *strcasestr(const char *, const char *);
+void *memrchr(const void *, int, size_t);
+void *mempcpy(void *, const void *, size_t);
+#endif
+
+/* musl internal symbols used across translation units */
+char *__strchrnul(const char *, int);
+void *__memrchr(const void *, int, size_t);
+char *__stpcpy(char *__restrict, const char *__restrict);
+char *__stpncpy(char *__restrict, const char *__restrict, size_t);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/na/include/libc-shim/strings.h b/na/include/libc-shim/strings.h
new file mode 100644
index 0000000..b7a5ea0
--- /dev/null
+++ b/na/include/libc-shim/strings.h
@@ -0,0 +1,40 @@
+#ifndef _STRINGS_H
+#define _STRINGS_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <features.h>
+
+#define __NEED_size_t
+#define __NEED_locale_t
+#include <bits/alltypes.h>
+
+#if defined(_GNU_SOURCE) || defined(_BSD_SOURCE) || defined(_POSIX_SOURCE) \
+ || (defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE+0 < 200809L) \
+ || (defined(_XOPEN_SOURCE) && _XOPEN_SOURCE+0 < 700)
+int bcmp (const void *, const void *, size_t);
+void bcopy (const void *, void *, size_t);
+void bzero (void *, size_t);
+char *index (const char *, int);
+char *rindex (const char *, int);
+#endif
+
+#if defined(_XOPEN_SOURCE) || defined(_GNU_SOURCE) || defined(_BSD_SOURCE)
+int ffs (int);
+int ffsl (long);
+int ffsll (long long);
+#endif
+
+int strcasecmp (const char *, const char *);
+int strncasecmp (const char *, const char *, size_t);
+
+int strcasecmp_l (const char *, const char *, locale_t);
+int strncasecmp_l (const char *, const char *, size_t, locale_t);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif