]>
Commit | Line | Data |
---|---|---|
d4697bc9 | 1 | /* Copyright (C) 1991-2014 Free Software Foundation, Inc. |
b5cc329c UD |
2 | This file is part of the GNU C Library. |
3 | ||
4 | The GNU C Library is free software; you can redistribute it and/or | |
5 | modify it under the terms of the GNU Lesser General Public | |
6 | License as published by the Free Software Foundation; either | |
7 | version 2.1 of the License, or (at your option) any later version. | |
8 | ||
9 | The GNU C Library is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
12 | Lesser General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU Lesser General Public | |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
b5cc329c UD |
17 | |
18 | #include <string.h> | |
19 | ||
20 | #include <memcopy.h> | |
21 | ||
22 | ||
23 | char * | |
24 | __strncat_chk (s1, s2, n, s1len) | |
25 | char *s1; | |
26 | const char *s2; | |
27 | size_t n; | |
28 | size_t s1len; | |
29 | { | |
50f81fd7 | 30 | char c; |
b5cc329c UD |
31 | char *s = s1; |
32 | ||
33 | /* Find the end of S1. */ | |
34 | do | |
35 | { | |
a1ffb40e | 36 | if (__glibc_unlikely (s1len-- == 0)) |
b5cc329c UD |
37 | __chk_fail (); |
38 | c = *s1++; | |
39 | } | |
40 | while (c != '\0'); | |
41 | ||
42 | /* Make S1 point before next character, so we can increment | |
43 | it while memory is read (wins on pipelined cpus). */ | |
44 | ++s1len; | |
45 | s1 -= 2; | |
46 | ||
47 | if (n >= 4) | |
48 | { | |
49 | size_t n4 = n >> 2; | |
50 | do | |
51 | { | |
a1ffb40e | 52 | if (__glibc_unlikely (s1len-- == 0)) |
b5cc329c UD |
53 | __chk_fail (); |
54 | c = *s2++; | |
55 | *++s1 = c; | |
56 | if (c == '\0') | |
57 | return s; | |
a1ffb40e | 58 | if (__glibc_unlikely (s1len-- == 0)) |
b5cc329c UD |
59 | __chk_fail (); |
60 | c = *s2++; | |
61 | *++s1 = c; | |
62 | if (c == '\0') | |
63 | return s; | |
a1ffb40e | 64 | if (__glibc_unlikely (s1len-- == 0)) |
b5cc329c UD |
65 | __chk_fail (); |
66 | c = *s2++; | |
67 | *++s1 = c; | |
68 | if (c == '\0') | |
69 | return s; | |
a1ffb40e | 70 | if (__glibc_unlikely (s1len-- == 0)) |
b5cc329c UD |
71 | __chk_fail (); |
72 | c = *s2++; | |
73 | *++s1 = c; | |
74 | if (c == '\0') | |
75 | return s; | |
76 | } while (--n4 > 0); | |
77 | n &= 3; | |
78 | } | |
79 | ||
80 | while (n > 0) | |
81 | { | |
a1ffb40e | 82 | if (__glibc_unlikely (s1len-- == 0)) |
b5cc329c UD |
83 | __chk_fail (); |
84 | c = *s2++; | |
85 | *++s1 = c; | |
86 | if (c == '\0') | |
87 | return s; | |
88 | n--; | |
89 | } | |
90 | ||
91 | if (c != '\0') | |
92 | { | |
a1ffb40e | 93 | if (__glibc_unlikely (s1len-- == 0)) |
b5cc329c UD |
94 | __chk_fail (); |
95 | *++s1 = '\0'; | |
96 | } | |
97 | ||
98 | return s; | |
99 | } |