1/* Copyright (C) 1995-2018 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.
4
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.
9
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.
14
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/>. */
18
19#include <wchar.h>
20
21
22/* Append no more than N wide-character of SRC onto DEST. */
23wchar_t *
24__wcsncat_chk (wchar_t *dest, const wchar_t *src, size_t n, size_t destlen)
25{
26 wchar_t c;
27 wchar_t * const s = dest;
28
29 /* Find the end of DEST. */
30 do
31 {
32 if (__glibc_unlikely (destlen-- == 0))
33 __chk_fail ();
34 c = *dest++;
35 }
36 while (c != L'\0');
37
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;
42
43 if (n >= 4)
44 {
45 size_t n4 = n >> 2;
46 do
47 {
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;
74 }
75
76 while (n > 0)
77 {
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--;
85 }
86
87 if (c != L'\0')
88 {
89 if (__glibc_unlikely (destlen-- == 0))
90 __chk_fail ();
91 *++dest = L'\0';
92 }
93
94 return s;
95}
96