diff options
author | deraadt <> | 2014-11-30 19:43:57 +0000 |
---|---|---|
committer | deraadt <> | 2014-11-30 19:43:57 +0000 |
commit | 3539e3fa3e1f8ac6b4177476a424681e58c76af5 (patch) | |
tree | 82fa98088bf55df432f5bac1dd59e9f100a1cac9 /src | |
parent | c65fff479ff8dc713956bcf5d92883f19a479d84 (diff) | |
download | openbsd-3539e3fa3e1f8ac6b4177476a424681e58c76af5.tar.gz openbsd-3539e3fa3e1f8ac6b4177476a424681e58c76af5.tar.bz2 openbsd-3539e3fa3e1f8ac6b4177476a424681e58c76af5.zip |
restructure libc/string + libc/arch/*/string coperation regarding
(potentially) MD versions (function dependent, not filename dependent)
split out memcpy/memmove/bcopy and strchr/index/strrchr/rindex
Bring back amd64 .S versions
And the final touch: switch all architectures temporarily to MI
memcpy.c, which contains syslog + abort for overlapping copies. A nice
harsh undefined behaviour. We will clean the entire userland of the
remaining issues in this catagory, then switch to the optimised memcpy
which skips the memmove check.
I tried to cut this change into pieces, but testing each sub-step on
every architecture is too time consuming and mindnumbing.
ok miod
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/libc/string/Makefile.inc | 135 | ||||
-rw-r--r-- | src/lib/libc/string/bcopy.c | 18 | ||||
-rw-r--r-- | src/lib/libc/string/index.c | 6 | ||||
-rw-r--r-- | src/lib/libc/string/memcpy.c | 99 | ||||
-rw-r--r-- | src/lib/libc/string/memmove.c | 112 | ||||
-rw-r--r-- | src/lib/libc/string/rindex.c | 6 | ||||
-rw-r--r-- | src/lib/libc/string/strchr.c | 43 | ||||
-rw-r--r-- | src/lib/libc/string/strrchr.c | 45 |
8 files changed, 309 insertions, 155 deletions
diff --git a/src/lib/libc/string/Makefile.inc b/src/lib/libc/string/Makefile.inc index a293219058..f684800db3 100644 --- a/src/lib/libc/string/Makefile.inc +++ b/src/lib/libc/string/Makefile.inc | |||
@@ -1,11 +1,11 @@ | |||
1 | # $OpenBSD: Makefile.inc,v 1.35 2014/06/13 02:12:17 matthew Exp $ | 1 | # $OpenBSD: Makefile.inc,v 1.36 2014/11/30 19:43:56 deraadt Exp $ |
2 | 2 | ||
3 | # string sources | 3 | # string sources |
4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/string ${LIBCSRCDIR}/string | 4 | .PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/string ${LIBCSRCDIR}/string |
5 | 5 | ||
6 | SRCS+= explicit_bzero.c memccpy.c memmem.c memrchr.c stpcpy.c stpncpy.c \ | 6 | SRCS+= explicit_bzero.c memccpy.c memmem.c memrchr.c stpcpy.c stpncpy.c \ |
7 | strcasecmp.c strcasestr.c strcoll.c strdup.c \ | 7 | strcasecmp.c strcasestr.c strcoll.c strdup.c \ |
8 | strerror.c strerror_r.c strlcat.c strmode.c strndup.c strnlen.c \ | 8 | strerror.c strerror_r.c strmode.c strndup.c strnlen.c \ |
9 | strsignal.c strtok.c strxfrm.c \ | 9 | strsignal.c strtok.c strxfrm.c \ |
10 | timingsafe_bcmp.c timingsafe_memcmp.c \ | 10 | timingsafe_bcmp.c timingsafe_memcmp.c \ |
11 | wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \ | 11 | wcscat.c wcschr.c wcscmp.c wcscpy.c wcscspn.c wcslcat.c wcslcpy.c \ |
@@ -14,136 +14,15 @@ SRCS+= explicit_bzero.c memccpy.c memmem.c memrchr.c stpcpy.c stpncpy.c \ | |||
14 | wmemmove.c wmemset.c wcsdup.c wcscasecmp.c | 14 | wmemmove.c wmemset.c wcsdup.c wcscasecmp.c |
15 | 15 | ||
16 | # machine-dependent net sources | 16 | # machine-dependent net sources |
17 | # m-d Makefile.inc must include sources for: | 17 | # ../arch/ARCH/Makefile.inc must include sources for: |
18 | # bcmp() bcopy() bzero() ffs() index() memchr() memcmp() memset() | 18 | # bcmp() bcopy() bzero() ffs() index() memchr() memcmp() memset() |
19 | # rindex() strcat() strcmp() strcpy() strcspn() strlen() strlcpy() | 19 | # memcpy() memmove() memset() rindex() strcat() strchr() |
20 | # strncat() strncmp() strncpy() strpbrk() strsep() | 20 | # strcmp() strcpy() strcspn() strlen() strlcat() strlcpy() |
21 | # strspn() strstr() swav() | 21 | # strncat() strncmp() strncpy() strpbrk() strrchr() strsep() |
22 | # m-d Makefile.inc may include sources for: | 22 | # strspn() strstr() swab() |
23 | # memcpy() memmove() strchr() strrchr() | ||
24 | 23 | ||
25 | .include "${LIBCSRCDIR}/arch/${MACHINE_CPU}/string/Makefile.inc" | 24 | .include "${LIBCSRCDIR}/arch/${MACHINE_CPU}/string/Makefile.inc" |
26 | 25 | ||
27 | # if no machine specific memmove(3), build one out of bcopy(3). | ||
28 | .if empty(SRCS:Mmemmove.S) | ||
29 | OBJS+= memmove.o | ||
30 | memmove.o: bcopy.c | ||
31 | ${CC} -DMEMMOVE ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
32 | @${LD} -o ${.TARGET}.tmp -x -r ${.TARGET} | ||
33 | @mv ${.TARGET}.tmp ${.TARGET} | ||
34 | |||
35 | memmove.go: bcopy.c | ||
36 | ${CC} -g -DMEMMOVE ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
37 | @${LD} -o ${.TARGET}.tmp -x -r ${.TARGET} | ||
38 | @mv ${.TARGET}.tmp ${.TARGET} | ||
39 | |||
40 | memmove.po: bcopy.c | ||
41 | ${CC} -DMEMMOVE ${CFLAGS} ${CPPFLAGS} -c -p ${.ALLSRC} -o ${.TARGET} | ||
42 | @${LD} -o ${.TARGET}.tmp -X -r ${.TARGET} | ||
43 | @mv ${.TARGET}.tmp ${.TARGET} | ||
44 | |||
45 | memmove.so: bcopy.c | ||
46 | ${CC} ${PICFLAG} -DPIC -DMEMMOVE ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} \ | ||
47 | -o ${.TARGET} | ||
48 | |||
49 | memmove.do: bcopy.c | ||
50 | ${CC} -DMEMMOVE ${CFLAGS} ${CPPFLAGS} ${DIST_CFLAGS} -c ${.ALLSRC} \ | ||
51 | -o ${.TARGET} | ||
52 | @${LD} -o ${.TARGET}.tmp -x -r ${.TARGET} | ||
53 | @mv ${.TARGET}.tmp ${.TARGET} | ||
54 | .endif | ||
55 | |||
56 | # if no machine specific memcpy(3), build one out of bcopy(3). | ||
57 | # if there is a machine specific memmove(3), we'll assume it aliases | ||
58 | # memcpy(3). | ||
59 | .if empty(SRCS:Mmemcpy.S) | ||
60 | .if empty(SRCS:Mmemmove.S) | ||
61 | OBJS+= memcpy.o | ||
62 | memcpy.o: bcopy.c | ||
63 | ${CC} -DMEMCOPY ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
64 | @${LD} -o ${.TARGET}.tmp -x -r ${.TARGET} | ||
65 | @mv ${.TARGET}.tmp ${.TARGET} | ||
66 | |||
67 | memcpy.go: bcopy.c | ||
68 | ${CC} -g -DMEMCOPY ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
69 | @${LD} -o ${.TARGET}.tmp -x -r ${.TARGET} | ||
70 | @mv ${.TARGET}.tmp ${.TARGET} | ||
71 | |||
72 | memcpy.po: bcopy.c | ||
73 | ${CC} -DMEMCOPY ${CFLAGS} ${CPPFLAGS} -c -p ${.ALLSRC} -o ${.TARGET} | ||
74 | @${LD} -o ${.TARGET}.tmp -X -r ${.TARGET} | ||
75 | @mv ${.TARGET}.tmp ${.TARGET} | ||
76 | |||
77 | memcpy.so: bcopy.c | ||
78 | ${CC} ${PICFLAG} -DPIC -DMEMCOPY ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} \ | ||
79 | -o ${.TARGET} | ||
80 | |||
81 | memcpy.do: bcopy.c | ||
82 | ${CC} -DMEMCOPY ${CFLAGS} ${CPPFLAGS} ${DIST_CFLAGS} -c ${.ALLSRC} \ | ||
83 | -o ${.TARGET} | ||
84 | @${LD} -o ${.TARGET}.tmp -x -r ${.TARGET} | ||
85 | @mv ${.TARGET}.tmp ${.TARGET} | ||
86 | .endif | ||
87 | .endif | ||
88 | |||
89 | # if no machine specific strchr(3), build one out of index(3). | ||
90 | .if empty(SRCS:Mstrchr.S) | ||
91 | OBJS+= strchr.o | ||
92 | strchr.o: index.c | ||
93 | ${CC} -DSTRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
94 | @${LD} -o ${.TARGET}.tmp -x -r ${.TARGET} | ||
95 | @mv ${.TARGET}.tmp ${.TARGET} | ||
96 | |||
97 | strchr.go: index.c | ||
98 | ${CC} -g -DSTRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
99 | @${LD} -o ${.TARGET}.tmp -x -r ${.TARGET} | ||
100 | @mv ${.TARGET}.tmp ${.TARGET} | ||
101 | |||
102 | strchr.po: index.c | ||
103 | ${CC} -DSTRCHR ${CFLAGS} ${CPPFLAGS} -c -p ${.ALLSRC} -o ${.TARGET} | ||
104 | @${LD} -o ${.TARGET}.tmp -X -r ${.TARGET} | ||
105 | @mv ${.TARGET}.tmp ${.TARGET} | ||
106 | |||
107 | strchr.so: index.c | ||
108 | ${CC} ${PICFLAG} -DPIC -DSTRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} \ | ||
109 | -o ${.TARGET} | ||
110 | |||
111 | strchr.do: index.c | ||
112 | ${CC} -DSTRCHR ${CFLAGS} ${CPPFLAGS} ${DIST_CFLAGS} -c ${.ALLSRC} \ | ||
113 | -o ${.TARGET} | ||
114 | @${LD} -o ${.TARGET}.tmp -x -r ${.TARGET} | ||
115 | @mv ${.TARGET}.tmp ${.TARGET} | ||
116 | .endif | ||
117 | |||
118 | # if no machine specific strrchr(3), build one out of rindex(3). | ||
119 | .if empty(SRCS:Mstrrchr.S) | ||
120 | OBJS+= strrchr.o | ||
121 | strrchr.o: rindex.c | ||
122 | ${CC} -DSTRRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
123 | @${LD} -o ${.TARGET}.tmp -x -r ${.TARGET} | ||
124 | @mv ${.TARGET}.tmp ${.TARGET} | ||
125 | |||
126 | strrchr.go: rindex.c | ||
127 | ${CC} -g -DSTRRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} -o ${.TARGET} | ||
128 | @${LD} -o ${.TARGET}.tmp -x -r ${.TARGET} | ||
129 | @mv ${.TARGET}.tmp ${.TARGET} | ||
130 | |||
131 | strrchr.po: rindex.c | ||
132 | ${CC} -DSTRRCHR ${CFLAGS} ${CPPFLAGS} -c -p ${.ALLSRC} -o ${.TARGET} | ||
133 | @${LD} -o ${.TARGET}.tmp -X -r ${.TARGET} | ||
134 | @mv ${.TARGET}.tmp ${.TARGET} | ||
135 | |||
136 | strrchr.so: rindex.c | ||
137 | ${CC} ${PICFLAG} -DPIC -DSTRRCHR ${CFLAGS} ${CPPFLAGS} -c ${.ALLSRC} \ | ||
138 | -o ${.TARGET} | ||
139 | |||
140 | strrchr.do: rindex.c | ||
141 | ${CC} -DSTRRCHR ${CFLAGS} ${CPPFLAGS} ${DIST_CFLAGS} -c ${.ALLSRC} \ | ||
142 | -o ${.TARGET} | ||
143 | @${LD} -o ${.TARGET}.tmp -x -r ${.TARGET} | ||
144 | @mv ${.TARGET}.tmp ${.TARGET} | ||
145 | .endif | ||
146 | |||
147 | MAN+= bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 memccpy.3 memchr.3 \ | 26 | MAN+= bcmp.3 bcopy.3 bstring.3 bzero.3 ffs.3 memccpy.3 memchr.3 \ |
148 | memcmp.3 memcpy.3 memmem.3 memmove.3 memset.3 stpcpy.3 strcasecmp.3 \ | 27 | memcmp.3 memcpy.3 memmem.3 memmove.3 memset.3 stpcpy.3 strcasecmp.3 \ |
149 | strcat.3 strchr.3 strcmp.3 strcoll.3 strcpy.3 strcspn.3 strdup.3 \ | 28 | strcat.3 strchr.3 strcmp.3 strcoll.3 strcpy.3 strcspn.3 strdup.3 \ |
diff --git a/src/lib/libc/string/bcopy.c b/src/lib/libc/string/bcopy.c index 4308c6484a..fcaa843c71 100644 --- a/src/lib/libc/string/bcopy.c +++ b/src/lib/libc/string/bcopy.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: bcopy.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ | 1 | /* $OpenBSD: bcopy.c,v 1.6 2014/11/30 19:43:56 deraadt Exp $ */ |
2 | /*- | 2 | /*- |
3 | * Copyright (c) 1990 The Regents of the University of California. | 3 | * Copyright (c) 1990 The Regents of the University of California. |
4 | * All rights reserved. | 4 | * All rights reserved. |
@@ -44,21 +44,9 @@ typedef long word; /* "word" used for optimal copy speed */ | |||
44 | 44 | ||
45 | /* | 45 | /* |
46 | * Copy a block of memory, handling overlap. | 46 | * Copy a block of memory, handling overlap. |
47 | * This is the routine that actually implements | ||
48 | * (the portable versions of) bcopy, memcpy, and memmove. | ||
49 | */ | 47 | */ |
50 | #ifdef MEMCOPY | ||
51 | void * | ||
52 | memcpy(void *dst0, const void *src0, size_t length) | ||
53 | #else | ||
54 | #ifdef MEMMOVE | ||
55 | void * | ||
56 | memmove(void *dst0, const void *src0, size_t length) | ||
57 | #else | ||
58 | void | 48 | void |
59 | bcopy(const void *src0, void *dst0, size_t length) | 49 | bcopy(const void *src0, void *dst0, size_t length) |
60 | #endif | ||
61 | #endif | ||
62 | { | 50 | { |
63 | char *dst = dst0; | 51 | char *dst = dst0; |
64 | const char *src = src0; | 52 | const char *src = src0; |
@@ -120,9 +108,5 @@ bcopy(const void *src0, void *dst0, size_t length) | |||
120 | TLOOP(*--dst = *--src); | 108 | TLOOP(*--dst = *--src); |
121 | } | 109 | } |
122 | done: | 110 | done: |
123 | #if defined(MEMCOPY) || defined(MEMMOVE) | ||
124 | return (dst0); | ||
125 | #else | ||
126 | return; | 111 | return; |
127 | #endif | ||
128 | } | 112 | } |
diff --git a/src/lib/libc/string/index.c b/src/lib/libc/string/index.c index 50e9ca35ab..ebae15afe7 100644 --- a/src/lib/libc/string/index.c +++ b/src/lib/libc/string/index.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: index.c,v 1.5 2005/08/08 08:05:37 espie Exp $ */ | 1 | /* $OpenBSD: index.c,v 1.6 2014/11/30 19:43:56 deraadt Exp $ */ |
2 | /*- | 2 | /*- |
3 | * Copyright (c) 1990 The Regents of the University of California. | 3 | * Copyright (c) 1990 The Regents of the University of California. |
4 | * All rights reserved. | 4 | * All rights reserved. |
@@ -31,11 +31,7 @@ | |||
31 | #include <string.h> | 31 | #include <string.h> |
32 | 32 | ||
33 | char * | 33 | char * |
34 | #ifdef STRCHR | ||
35 | strchr(const char *p, int ch) | ||
36 | #else | ||
37 | index(const char *p, int ch) | 34 | index(const char *p, int ch) |
38 | #endif | ||
39 | { | 35 | { |
40 | for (;; ++p) { | 36 | for (;; ++p) { |
41 | if (*p == ch) | 37 | if (*p == ch) |
diff --git a/src/lib/libc/string/memcpy.c b/src/lib/libc/string/memcpy.c new file mode 100644 index 0000000000..1b9715e471 --- /dev/null +++ b/src/lib/libc/string/memcpy.c | |||
@@ -0,0 +1,99 @@ | |||
1 | /* $OpenBSD: memcpy.c,v 1.1 2014/11/30 19:43:56 deraadt Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1990 The Regents of the University of California. | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * This code is derived from software contributed to Berkeley by | ||
7 | * Chris Torek. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer in the | ||
16 | * documentation and/or other materials provided with the distribution. | ||
17 | * 3. Neither the name of the University nor the names of its contributors | ||
18 | * may be used to endorse or promote products derived from this software | ||
19 | * without specific prior written permission. | ||
20 | * | ||
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
31 | * SUCH DAMAGE. | ||
32 | */ | ||
33 | |||
34 | #include <string.h> | ||
35 | #include <stdlib.h> | ||
36 | #include <syslog.h> | ||
37 | |||
38 | /* | ||
39 | * sizeof(word) MUST BE A POWER OF TWO | ||
40 | * SO THAT wmask BELOW IS ALL ONES | ||
41 | */ | ||
42 | typedef long word; /* "word" used for optimal copy speed */ | ||
43 | |||
44 | #define wsize sizeof(word) | ||
45 | #define wmask (wsize - 1) | ||
46 | |||
47 | /* | ||
48 | * Copy a block of memory, not handling overlap. | ||
49 | */ | ||
50 | void * | ||
51 | memcpy(void *dst0, const void *src0, size_t length) | ||
52 | { | ||
53 | char *dst = dst0; | ||
54 | const char *src = src0; | ||
55 | size_t t; | ||
56 | |||
57 | if (length == 0 || dst == src) /* nothing to do */ | ||
58 | goto done; | ||
59 | |||
60 | if ((dst < src && dst + length > src) || | ||
61 | (src < dst && src + length > dst)) { | ||
62 | struct syslog_data sdata = SYSLOG_DATA_INIT; | ||
63 | |||
64 | syslog_r(LOG_CRIT, &sdata, "backwards memcpy"); | ||
65 | abort(); | ||
66 | } | ||
67 | |||
68 | /* | ||
69 | * Macros: loop-t-times; and loop-t-times, t>0 | ||
70 | */ | ||
71 | #define TLOOP(s) if (t) TLOOP1(s) | ||
72 | #define TLOOP1(s) do { s; } while (--t) | ||
73 | |||
74 | /* | ||
75 | * Copy forward. | ||
76 | */ | ||
77 | t = (long)src; /* only need low bits */ | ||
78 | if ((t | (long)dst) & wmask) { | ||
79 | /* | ||
80 | * Try to align operands. This cannot be done | ||
81 | * unless the low bits match. | ||
82 | */ | ||
83 | if ((t ^ (long)dst) & wmask || length < wsize) | ||
84 | t = length; | ||
85 | else | ||
86 | t = wsize - (t & wmask); | ||
87 | length -= t; | ||
88 | TLOOP1(*dst++ = *src++); | ||
89 | } | ||
90 | /* | ||
91 | * Copy whole words, then mop up any trailing bytes. | ||
92 | */ | ||
93 | t = length / wsize; | ||
94 | TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize); | ||
95 | t = length & wmask; | ||
96 | TLOOP(*dst++ = *src++); | ||
97 | done: | ||
98 | return (dst0); | ||
99 | } | ||
diff --git a/src/lib/libc/string/memmove.c b/src/lib/libc/string/memmove.c new file mode 100644 index 0000000000..1baad53544 --- /dev/null +++ b/src/lib/libc/string/memmove.c | |||
@@ -0,0 +1,112 @@ | |||
1 | /* $OpenBSD: memmove.c,v 1.1 2014/11/30 19:43:56 deraadt Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1990 The Regents of the University of California. | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * This code is derived from software contributed to Berkeley by | ||
7 | * Chris Torek. | ||
8 | * | ||
9 | * Redistribution and use in source and binary forms, with or without | ||
10 | * modification, are permitted provided that the following conditions | ||
11 | * are met: | ||
12 | * 1. Redistributions of source code must retain the above copyright | ||
13 | * notice, this list of conditions and the following disclaimer. | ||
14 | * 2. Redistributions in binary form must reproduce the above copyright | ||
15 | * notice, this list of conditions and the following disclaimer in the | ||
16 | * documentation and/or other materials provided with the distribution. | ||
17 | * 3. Neither the name of the University nor the names of its contributors | ||
18 | * may be used to endorse or promote products derived from this software | ||
19 | * without specific prior written permission. | ||
20 | * | ||
21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
27 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
28 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
29 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
30 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
31 | * SUCH DAMAGE. | ||
32 | */ | ||
33 | |||
34 | #include <string.h> | ||
35 | |||
36 | /* | ||
37 | * sizeof(word) MUST BE A POWER OF TWO | ||
38 | * SO THAT wmask BELOW IS ALL ONES | ||
39 | */ | ||
40 | typedef long word; /* "word" used for optimal copy speed */ | ||
41 | |||
42 | #define wsize sizeof(word) | ||
43 | #define wmask (wsize - 1) | ||
44 | |||
45 | /* | ||
46 | * Copy a block of memory, handling overlap. | ||
47 | */ | ||
48 | void * | ||
49 | memmove(void *dst0, const void *src0, size_t length) | ||
50 | { | ||
51 | char *dst = dst0; | ||
52 | const char *src = src0; | ||
53 | size_t t; | ||
54 | |||
55 | if (length == 0 || dst == src) /* nothing to do */ | ||
56 | goto done; | ||
57 | |||
58 | /* | ||
59 | * Macros: loop-t-times; and loop-t-times, t>0 | ||
60 | */ | ||
61 | #define TLOOP(s) if (t) TLOOP1(s) | ||
62 | #define TLOOP1(s) do { s; } while (--t) | ||
63 | |||
64 | if ((unsigned long)dst < (unsigned long)src) { | ||
65 | /* | ||
66 | * Copy forward. | ||
67 | */ | ||
68 | t = (long)src; /* only need low bits */ | ||
69 | if ((t | (long)dst) & wmask) { | ||
70 | /* | ||
71 | * Try to align operands. This cannot be done | ||
72 | * unless the low bits match. | ||
73 | */ | ||
74 | if ((t ^ (long)dst) & wmask || length < wsize) | ||
75 | t = length; | ||
76 | else | ||
77 | t = wsize - (t & wmask); | ||
78 | length -= t; | ||
79 | TLOOP1(*dst++ = *src++); | ||
80 | } | ||
81 | /* | ||
82 | * Copy whole words, then mop up any trailing bytes. | ||
83 | */ | ||
84 | t = length / wsize; | ||
85 | TLOOP(*(word *)dst = *(word *)src; src += wsize; dst += wsize); | ||
86 | t = length & wmask; | ||
87 | TLOOP(*dst++ = *src++); | ||
88 | } else { | ||
89 | /* | ||
90 | * Copy backwards. Otherwise essentially the same. | ||
91 | * Alignment works as before, except that it takes | ||
92 | * (t&wmask) bytes to align, not wsize-(t&wmask). | ||
93 | */ | ||
94 | src += length; | ||
95 | dst += length; | ||
96 | t = (long)src; | ||
97 | if ((t | (long)dst) & wmask) { | ||
98 | if ((t ^ (long)dst) & wmask || length <= wsize) | ||
99 | t = length; | ||
100 | else | ||
101 | t &= wmask; | ||
102 | length -= t; | ||
103 | TLOOP1(*--dst = *--src); | ||
104 | } | ||
105 | t = length / wsize; | ||
106 | TLOOP(src -= wsize; dst -= wsize; *(word *)dst = *(word *)src); | ||
107 | t = length & wmask; | ||
108 | TLOOP(*--dst = *--src); | ||
109 | } | ||
110 | done: | ||
111 | return (dst0); | ||
112 | } | ||
diff --git a/src/lib/libc/string/rindex.c b/src/lib/libc/string/rindex.c index bf9d6f7cf1..23716f6af3 100644 --- a/src/lib/libc/string/rindex.c +++ b/src/lib/libc/string/rindex.c | |||
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: rindex.c,v 1.6 2005/08/08 08:05:37 espie Exp $ */ | 1 | /* $OpenBSD: rindex.c,v 1.7 2014/11/30 19:43:56 deraadt Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 1988 Regents of the University of California. | 3 | * Copyright (c) 1988 Regents of the University of California. |
4 | * All rights reserved. | 4 | * All rights reserved. |
@@ -31,11 +31,7 @@ | |||
31 | #include <string.h> | 31 | #include <string.h> |
32 | 32 | ||
33 | char * | 33 | char * |
34 | #ifdef STRRCHR | ||
35 | strrchr(const char *p, int ch) | ||
36 | #else | ||
37 | rindex(const char *p, int ch) | 34 | rindex(const char *p, int ch) |
38 | #endif | ||
39 | { | 35 | { |
40 | char *save; | 36 | char *save; |
41 | 37 | ||
diff --git a/src/lib/libc/string/strchr.c b/src/lib/libc/string/strchr.c new file mode 100644 index 0000000000..20f2faeb11 --- /dev/null +++ b/src/lib/libc/string/strchr.c | |||
@@ -0,0 +1,43 @@ | |||
1 | /* $OpenBSD: strchr.c,v 1.1 2014/11/30 19:43:57 deraadt Exp $ */ | ||
2 | /*- | ||
3 | * Copyright (c) 1990 The Regents of the University of California. | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #include <string.h> | ||
32 | |||
33 | char * | ||
34 | strchr(const char *p, int ch) | ||
35 | { | ||
36 | for (;; ++p) { | ||
37 | if (*p == ch) | ||
38 | return((char *)p); | ||
39 | if (!*p) | ||
40 | return((char *)NULL); | ||
41 | } | ||
42 | /* NOTREACHED */ | ||
43 | } | ||
diff --git a/src/lib/libc/string/strrchr.c b/src/lib/libc/string/strrchr.c new file mode 100644 index 0000000000..b2f17fba28 --- /dev/null +++ b/src/lib/libc/string/strrchr.c | |||
@@ -0,0 +1,45 @@ | |||
1 | /* $OpenBSD: strrchr.c,v 1.1 2014/11/30 19:43:57 deraadt Exp $ */ | ||
2 | /* | ||
3 | * Copyright (c) 1988 Regents of the University of California. | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * Redistribution and use in source and binary forms, with or without | ||
7 | * modification, are permitted provided that the following conditions | ||
8 | * are met: | ||
9 | * 1. Redistributions of source code must retain the above copyright | ||
10 | * notice, this list of conditions and the following disclaimer. | ||
11 | * 2. Redistributions in binary form must reproduce the above copyright | ||
12 | * notice, this list of conditions and the following disclaimer in the | ||
13 | * documentation and/or other materials provided with the distribution. | ||
14 | * 3. Neither the name of the University nor the names of its contributors | ||
15 | * may be used to endorse or promote products derived from this software | ||
16 | * without specific prior written permission. | ||
17 | * | ||
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | ||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | ||
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | ||
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | ||
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | ||
28 | * SUCH DAMAGE. | ||
29 | */ | ||
30 | |||
31 | #include <string.h> | ||
32 | |||
33 | char * | ||
34 | strrchr(const char *p, int ch) | ||
35 | { | ||
36 | char *save; | ||
37 | |||
38 | for (save = NULL;; ++p) { | ||
39 | if (*p == ch) | ||
40 | save = (char *)p; | ||
41 | if (!*p) | ||
42 | return(save); | ||
43 | } | ||
44 | /* NOTREACHED */ | ||
45 | } | ||