2014-12-11 Steve Ellcey <sellcey@imgtec.com>
[glibc.git] / debug / wcsncat_chk.c
blobec990248fb452eca87196270fd2ab9e707a9a266
1 /* Copyright (C) 1995-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.ai.mit.edu>, 1995.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include <wchar.h>
22 /* Append no more than N wide-character of SRC onto DEST. */
23 wchar_t *
24 __wcsncat_chk (wchar_t *dest, const wchar_t *src, size_t n, size_t destlen)
26 wchar_t c;
27 wchar_t * const s = dest;
29 /* Find the end of DEST. */
32 if (__glibc_unlikely (destlen-- == 0))
33 __chk_fail ();
34 c = *dest++;
36 while (c != L'\0');
38 /* Make DEST point before next character, so we can increment
39 it while memory is read (wins on pipelined cpus). */
40 ++destlen;
41 dest -= 2;
43 if (n >= 4)
45 size_t n4 = n >> 2;
48 if (__glibc_unlikely (destlen-- == 0))
49 __chk_fail ();
50 c = *src++;
51 *++dest = c;
52 if (c == L'\0')
53 return s;
54 if (__glibc_unlikely (destlen-- == 0))
55 __chk_fail ();
56 c = *src++;
57 *++dest = c;
58 if (c == L'\0')
59 return s;
60 if (__glibc_unlikely (destlen-- == 0))
61 __chk_fail ();
62 c = *src++;
63 *++dest = c;
64 if (c == L'\0')
65 return s;
66 if (__glibc_unlikely (destlen-- == 0))
67 __chk_fail ();
68 c = *src++;
69 *++dest = c;
70 if (c == L'\0')
71 return s;
72 } while (--n4 > 0);
73 n &= 3;
76 while (n > 0)
78 if (__glibc_unlikely (destlen-- == 0))
79 __chk_fail ();
80 c = *src++;
81 *++dest = c;
82 if (c == L'\0')
83 return s;
84 n--;
87 if (c != L'\0')
89 if (__glibc_unlikely (destlen-- == 0))
90 __chk_fail ();
91 *++dest = L'\0';
94 return s;