vfs: check userland buffers before reading them.
[haiku.git] / src / system / libroot / posix / string / arch / generic / memcpy.c
blobb1979601544b04078910c1feb1eb8460768336d4
1 /*
2 ** Copyright 2001, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
4 */
6 #include <sys/types.h>
7 #include <string.h>
9 /* do not build for for arches that have overridden this with an assembly version */
10 #if !defined(ARCH_x86)
12 typedef int word;
14 #define lsize sizeof(word)
15 #define lmask (lsize - 1)
17 void *memcpy(void *dest, const void *src, size_t count)
19 char *d = (char *)dest;
20 const char *s = (const char *)src;
21 int len;
23 if(count == 0 || dest == src)
24 return dest;
26 if(((long)d | (long)s) & lmask) {
27 // src and/or dest do not align on word boundary
28 if((((long)d ^ (long)s) & lmask) || (count < lsize))
29 len = count; // copy the rest of the buffer with the byte mover
30 else
31 len = lsize - ((long)d & lmask); // move the ptrs up to a word boundary
33 count -= len;
34 for(; len > 0; len--)
35 *d++ = *s++;
37 for(len = count / lsize; len > 0; len--) {
38 *(word *)d = *(word *)s;
39 d += lsize;
40 s += lsize;
42 for(len = count & lmask; len > 0; len--)
43 *d++ = *s++;
45 return dest;
48 #endif