summaryrefslogtreecommitdiff
path: root/lib/libc/string/memcpy.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libc/string/memcpy.c')
-rw-r--r--lib/libc/string/memcpy.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/lib/libc/string/memcpy.c b/lib/libc/string/memcpy.c
index ecdbd602..05586b55 100644
--- a/lib/libc/string/memcpy.c
+++ b/lib/libc/string/memcpy.c
@@ -1,3 +1,4 @@
+#include <errno.h>
#include <string.h>
#include <features.h>
@@ -14,3 +15,41 @@ void *memcpy(void *restrict s1, const void *restrict s2, size_t n)
return s1;
}
+
+errno_t memcpy_s(void *restrict dest, rsize_t destsz, const void *restrict src,
+ rsize_t count)
+{
+ if (dest == NULL || src == NULL) {
+ if (dest != NULL && destsz > 0) {
+ unsigned char *d = dest;
+ for (rsize_t i = 0; i < destsz; i++) {
+ d[i] = 0;
+ }
+ }
+
+ return EINVAL;
+ }
+
+ const unsigned char *s = src;
+ unsigned char *d = dest;
+
+ if ((d > s && d < s + count) || (s > d && s < d + count)) {
+ for (rsize_t i = 0; i < destsz; i++) {
+ d[i] = 0;
+ }
+ return EINVAL;
+ }
+
+ if (count > destsz) {
+ for (rsize_t i = 0; i < destsz; i++) {
+ d[i] = 0;
+ }
+ return ERANGE;
+ }
+
+ for (rsize_t i = 0; i < count; i++) {
+ d[i] = s[i];
+ }
+
+ return 0;
+}