1 /* Copyright (C) 2013-2015 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
20 #include "string-endian.h"
22 /* Find the length of S, but scan at most MAXLEN characters. If no
23 '\0' terminator is found in that many characters, return MAXLEN. */
25 __strnlen (const char *s
, size_t maxlen
)
27 /* When maxlen is 0, can't read any bytes or it might cause a page fault. */
31 /* Get an aligned pointer. */
32 const uintptr_t s_int
= (uintptr_t) s
;
33 const uint64_t *p
= (const uint64_t *) (s_int
& -8);
34 size_t bytes_read
= sizeof (*p
) - (s_int
& (sizeof (*p
) - 1));
36 /* Read and MASK the first word. */
37 uint64_t v
= *p
| MASK (s_int
);
40 while ((bits
= __insn_v1cmpeqi (v
, 0)) == 0)
42 if (bytes_read
>= maxlen
)
44 /* Read maxlen bytes and didn't find the terminator. */
48 bytes_read
+= sizeof (v
);
51 /* Found '\0', check it is not larger than maxlen */
52 size_t len
= ((const char *) p
) + (CFZ (bits
) >> 3) - s
;
53 return (len
< maxlen
? len
: maxlen
);
55 weak_alias (__strnlen
, strnlen
)
56 libc_hidden_def (strnlen
)