Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
git-mirror
/
glibc
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
0
Security
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security
Insights
Files
ec21534
argp
assert
benchtests
bits
catgets
conf
conform
crypt
csu
ctype
debug
dirent
dlfcn
elf
gmon
gnulib
grp
gshadow
hesiod
hurd
iconv
iconvdata
include
inet
intl
io
libidn
libio
locale
localedata
login
mach
malloc
manual
math
mathvec
misc
nis
nptl
nptl_db
nscd
nss
po
posix
pwd
resolv
resource
rt
scripts
setjmp
shadow
signal
socket
soft-fp
stdio-common
stdlib
streams
string
bits
Depend
Makefile
Versions
_strerror.c
argz-addsep.c
argz-append.c
argz-count.c
argz-create.c
argz-ctsep.c
argz-delete.c
argz-extract.c
argz-insert.c
argz-next.c
argz-replace.c
argz-stringify.c
argz.h
basename.c
bcopy.c
bug-envz1.c
bug-strcoll1.c
bug-strcoll2.c
bug-strncat1.c
bug-strpbrk1.c
bug-strspn1.c
bug-strtok1.c
byteswap.h
bzero.c
endian.h
envz.c
envz.h
ffs.c
ffsll.c
inl-tester.c
memccpy.c
memchr.c
memcmp.c
memcpy.c
memfrob.c
memmem.c
memmove.c
memory.h
mempcpy.c
memrchr.c
memset.c
noinl-tester.c
rawmemchr.c
stpcpy.c
stpncpy.c
str-two-way.h
stratcliff.c
strcasecmp.c
strcasecmp_l.c
strcasestr.c
strcat.c
strchr.c
strchrnul.c
strcmp.c
strcoll.c
strcoll_l.c
strcpy.c
strcspn.c
strdup.c
strerror.c
strerror_l.c
strfry.c
string-inlines.c
string.h
strings.h
strlen.c
strncase.c
strncase_l.c
strncat.c
strncmp.c
strncpy.c
strndup.c
strnlen.c
strpbrk.c
strrchr.c
strsep.c
strsignal.c
strspn.c
strstr.c
strtok.c
strtok_r.c
strverscmp.c
strxfrm.c
strxfrm_l.c
swab.c
test-bcopy.c
test-bzero.c
test-ffs.c
test-memccpy.c
test-memchr.c
test-memcmp.c
test-memcpy.c
test-memmem.c
test-memmove.c
test-mempcpy.c
test-memrchr.c
test-memset.c
test-rawmemchr.c
test-stpcpy.c
test-stpncpy.c
test-strcasecmp.c
test-strcasestr.c
test-strcat.c
test-strchr.c
test-strchrnul.c
test-strcmp.c
test-strcpy.c
test-strcspn.c
test-string.h
test-strlen.c
test-strncasecmp.c
test-strncat.c
test-strncmp.c
test-strncpy.c
test-strnlen.c
test-strpbrk.c
test-strrchr.c
test-strspn.c
test-strstr.c
testcopy.c
tester.c
tst-bswap.c
tst-endian.c
tst-inlcall.c
tst-strcoll-overflow.c
tst-strfry.c
tst-strlen.c
tst-strtok.c
tst-strtok_r.c
tst-strxfrm.c
tst-strxfrm2.c
tst-svc.c
tst-svc.expect
tst-svc.input
tst-svc2.c
wordcopy.c
xpg-strerror.c
sunrpc
sysdeps
sysvipc
termios
time
timezone
wcsmbs
wctype
.gitattributes
.gitignore
BUGS
CONFORMANCE
COPYING
COPYING.LIB
ChangeLog
ChangeLog.1
ChangeLog.10
ChangeLog.11
ChangeLog.12
ChangeLog.13
ChangeLog.14
ChangeLog.15
ChangeLog.16
ChangeLog.17
ChangeLog.2
ChangeLog.3
ChangeLog.4
ChangeLog.5
ChangeLog.6
ChangeLog.7
ChangeLog.8
ChangeLog.9
ChangeLog.old-ports
ChangeLog.old-ports-aarch64
ChangeLog.old-ports-aix
ChangeLog.old-ports-alpha
ChangeLog.old-ports-am33
ChangeLog.old-ports-arm
ChangeLog.old-ports-cris
ChangeLog.old-ports-hppa
ChangeLog.old-ports-ia64
ChangeLog.old-ports-linux-generic
ChangeLog.old-ports-m68k
ChangeLog.old-ports-microblaze
ChangeLog.old-ports-mips
ChangeLog.old-ports-powerpc
ChangeLog.old-ports-tile
INSTALL
LICENSES
Makeconfig
Makefile
Makefile.in
Makerules
NAMESPACE
NEWS
PROJECTS
README
Rules
WUR-REPORT
abi-tags
aclocal.m4
config.h.in
config.make.in
configure
configure.ac
cppflags-iterator.mk
extra-lib.mk
extra-modules.mk
gen-locales.mk
libc-abis
o-iterator.mk
shlib-versions
test-skeleton.c
version.h
Breadcrumbs
glibc
/
string
/
strchrnul.c
Blame
Blame
Latest commit
History
History
166 lines (136 loc) · 5.7 KB
Breadcrumbs
glibc
/
string
/
strchrnul.c
Top
File metadata and controls
Code
Blame
166 lines (136 loc) · 5.7 KB
Raw
/* Copyright (C) 1991-2016 Free Software Foundation, Inc. This file is part of the GNU C Library. Based on strlen implementation by Torbjorn Granlund (tege@sics.se), with help from Dan Sahlin (dan@sics.se) and bug fix and commentary by Jim Blandy (jimb@ai.mit.edu); adaptation to strchr suggested by Dick Karpinski (dick@cca.ucsf.edu), and implemented by Roland McGrath (roland@ai.mit.edu). The GNU C Library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. The GNU C Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with the GNU C Library; if not, see <http://www.gnu.org/licenses/>. */ #include <string.h> #include <memcopy.h> #include <stdlib.h> #undef __strchrnul #undef strchrnul #ifndef STRCHRNUL # define STRCHRNUL __strchrnul #endif /* Find the first occurrence of C in S or the final NUL byte. */ char * STRCHRNUL (const char *s, int c_in) { const unsigned char *char_ptr; const unsigned long int *longword_ptr; unsigned long int longword, magic_bits, charmask; unsigned char c; c = (unsigned char) c_in; /* Handle the first few characters by reading one character at a time. Do this until CHAR_PTR is aligned on a longword boundary. */ for (char_ptr = (const unsigned char *) s; ((unsigned long int) char_ptr & (sizeof (longword) - 1)) != 0; ++char_ptr) if (*char_ptr == c || *char_ptr == '\0') return (void *) char_ptr; /* All these elucidatory comments refer to 4-byte longwords, but the theory applies equally well to 8-byte longwords. */ longword_ptr = (unsigned long int *) char_ptr; /* Bits 31, 24, 16, and 8 of this number are zero. Call these bits the "holes." Note that there is a hole just to the left of each byte, with an extra at the end: bits: 01111110 11111110 11111110 11111111 bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD The 1-bits make sure that carries propagate to the next 0-bit. The 0-bits provide holes for carries to fall into. */ magic_bits = -1; magic_bits = magic_bits / 0xff * 0xfe << 1 >> 1 | 1; /* Set up a longword, each of whose bytes is C. */ charmask = c | (c << 8); charmask |= charmask << 16; if (sizeof (longword) > 4) /* Do the shift in two steps to avoid a warning if long has 32 bits. */ charmask |= (charmask << 16) << 16; if (sizeof (longword) > 8) abort (); /* Instead of the traditional loop which tests each character, we will test a longword at a time. The tricky part is testing if *any of the four* bytes in the longword in question are zero. */ for (;;) { /* We tentatively exit the loop if adding MAGIC_BITS to LONGWORD fails to change any of the hole bits of LONGWORD. 1) Is this safe? Will it catch all the zero bytes? Suppose there is a byte with all zeros. Any carry bits propagating from its left will fall into the hole at its least significant bit and stop. Since there will be no carry from its most significant bit, the LSB of the byte to the left will be unchanged, and the zero will be detected. 2) Is this worthwhile? Will it ignore everything except zero bytes? Suppose every byte of LONGWORD has a bit set somewhere. There will be a carry into bit 8. If bit 8 is set, this will carry into bit 16. If bit 8 is clear, one of bits 9-15 must be set, so there will be a carry into bit 16. Similarly, there will be a carry into bit 24. If one of bits 24-30 is set, there will be a carry into bit 31, so all of the hole bits will be changed. The one misfire occurs when bits 24-30 are clear and bit 31 is set; in this case, the hole at bit 31 is not changed. If we had access to the processor carry flag, we could close this loophole by putting the fourth hole at bit 32! So it ignores everything except 128's, when they're aligned properly. 3) But wait! Aren't we looking for C as well as zero? Good point. So what we do is XOR LONGWORD with a longword, each of whose bytes is C. This turns each byte that is C into a zero. */ longword = *longword_ptr++; /* Add MAGIC_BITS to LONGWORD. */ if ((((longword + magic_bits) /* Set those bits that were unchanged by the addition. */ ^ ~longword) /* Look at only the hole bits. If any of the hole bits are unchanged, most likely one of the bytes was a zero. */ & ~magic_bits) != 0 || /* That caught zeroes. Now test for C. */ ((((longword ^ charmask) + magic_bits) ^ ~(longword ^ charmask)) & ~magic_bits) != 0) { /* Which of the bytes was C or zero? If none of them were, it was a misfire; continue the search. */ const unsigned char *cp = (const unsigned char *) (longword_ptr - 1); if (*cp == c || *cp == '\0') return (char *) cp; if (*++cp == c || *cp == '\0') return (char *) cp; if (*++cp == c || *cp == '\0') return (char *) cp; if (*++cp == c || *cp == '\0') return (char *) cp; if (sizeof (longword) > 4) { if (*++cp == c || *cp == '\0') return (char *) cp; if (*++cp == c || *cp == '\0') return (char *) cp; if (*++cp == c || *cp == '\0') return (char *) cp; if (*++cp == c || *cp == '\0') return (char *) cp; } } } /* This should never happen. */ return NULL; } weak_alias (__strchrnul, strchrnul)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
You can’t perform that action at this time.