From: Matthias Kruk Date: Tue, 5 May 2020 17:20:33 +0000 (+0900) Subject: libc: Add memcpy() implementation, fix bug in memset() X-Git-Url: https://git.corax.cc/?a=commitdiff_plain;h=a89681b0b8c84c9c3de1daaf979d3e485a87b115;p=corax libc: Add memcpy() implementation, fix bug in memset() --- diff --git a/libc/string.c b/libc/string.c index af6ea7f..5822776 100644 --- a/libc/string.c +++ b/libc/string.c @@ -12,12 +12,40 @@ size_t strlen(const char *s) void *memset(void *s, int c, size_t n) { char *ptr; + size_t i; ptr = (char*)s; - while(--n >= 0) { - *ptr++ = (char)c; + for(i = 0; i < n; i++) { + ptr[i] = (char)c; } return(s); } + +void* memcpy(void *dst, const void *src, size_t n) +{ + u32_t *d; + const u32_t *s; + + d = (u32_t*)dst; + s = (const u32_t*)src; + + while(n > 4) { + *d++ = *s++; + n -= 4; + } + + if(n >= 2) { + *((u16_t*)d) = *((u16_t*)s); + d = (u32_t*)(((void*)d) + 2); + s = (u32_t*)(((void*)s) + 2); + n -= 2; + } + + if(n >= 1) { + *((u8_t*)d) = *((u8_t*)s); + } + + return(dst); +}