Fix strchr test
[platform/upstream/glibc.git] / string / strnlen.c
1 /* Find the length of STRING, but scan at most MAXLEN characters.
2    Copyright (C) 1991, 1993, 1997, 2000, 2001, 2005, 2011 Free Software Foundation, Inc.
3    Contributed by Jakub Jelinek <jakub@redhat.com>.
4
5    Based on strlen written by Torbjorn Granlund (tege@sics.se),
6    with help from Dan Sahlin (dan@sics.se);
7    commentary by Jim Blandy (jimb@ai.mit.edu).
8
9    The GNU C Library is free software; you can redistribute it and/or
10    modify it under the terms of the GNU Lesser General Public License as
11    published by the Free Software Foundation; either version 2.1 of the
12    License, or (at your option) any later version.
13
14    The GNU C Library is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17    Lesser General Public License for more details.
18
19    You should have received a copy of the GNU Lesser General Public
20    License along with the GNU C Library; see the file COPYING.LIB.  If not,
21    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22    Boston, MA 02111-1307, USA.  */
23
24 #include <string.h>
25 #include <stdlib.h>
26
27 /* Find the length of S, but scan at most MAXLEN characters.  If no
28    '\0' terminator is found in that many characters, return MAXLEN.  */
29
30 #ifdef STRNLEN
31 # define __strnlen STRNLEN
32 #endif
33
34 size_t
35 __strnlen (const char *str, size_t maxlen)
36 {
37   const char *char_ptr, *end_ptr = str + maxlen;
38   const unsigned long int *longword_ptr;
39   unsigned long int longword, himagic, lomagic;
40
41   if (maxlen == 0)
42     return 0;
43
44   if (__builtin_expect (end_ptr < str, 0))
45     end_ptr = (const char *) ~0UL;
46
47   /* Handle the first few characters by reading one character at a time.
48      Do this until CHAR_PTR is aligned on a longword boundary.  */
49   for (char_ptr = str; ((unsigned long int) char_ptr
50                         & (sizeof (longword) - 1)) != 0;
51        ++char_ptr)
52     if (*char_ptr == '\0')
53       {
54         if (char_ptr > end_ptr)
55           char_ptr = end_ptr;
56         return char_ptr - str;
57       }
58
59   /* All these elucidatory comments refer to 4-byte longwords,
60      but the theory applies equally well to 8-byte longwords.  */
61
62   longword_ptr = (unsigned long int *) char_ptr;
63
64   /* Bits 31, 24, 16, and 8 of this number are zero.  Call these bits
65      the "holes."  Note that there is a hole just to the left of
66      each byte, with an extra at the end:
67
68      bits:  01111110 11111110 11111110 11111111
69      bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
70
71      The 1-bits make sure that carries propagate to the next 0-bit.
72      The 0-bits provide holes for carries to fall into.  */
73   himagic = 0x80808080L;
74   lomagic = 0x01010101L;
75   if (sizeof (longword) > 4)
76     {
77       /* 64-bit version of the magic.  */
78       /* Do the shift in two steps to avoid a warning if long has 32 bits.  */
79       himagic = ((himagic << 16) << 16) | himagic;
80       lomagic = ((lomagic << 16) << 16) | lomagic;
81     }
82   if (sizeof (longword) > 8)
83     abort ();
84
85   /* Instead of the traditional loop which tests each character,
86      we will test a longword at a time.  The tricky part is testing
87      if *any of the four* bytes in the longword in question are zero.  */
88   while (longword_ptr < (unsigned long int *) end_ptr)
89     {
90       /* We tentatively exit the loop if adding MAGIC_BITS to
91          LONGWORD fails to change any of the hole bits of LONGWORD.
92
93          1) Is this safe?  Will it catch all the zero bytes?
94          Suppose there is a byte with all zeros.  Any carry bits
95          propagating from its left will fall into the hole at its
96          least significant bit and stop.  Since there will be no
97          carry from its most significant bit, the LSB of the
98          byte to the left will be unchanged, and the zero will be
99          detected.
100
101          2) Is this worthwhile?  Will it ignore everything except
102          zero bytes?  Suppose every byte of LONGWORD has a bit set
103          somewhere.  There will be a carry into bit 8.  If bit 8
104          is set, this will carry into bit 16.  If bit 8 is clear,
105          one of bits 9-15 must be set, so there will be a carry
106          into bit 16.  Similarly, there will be a carry into bit
107          24.  If one of bits 24-30 is set, there will be a carry
108          into bit 31, so all of the hole bits will be changed.
109
110          The one misfire occurs when bits 24-30 are clear and bit
111          31 is set; in this case, the hole at bit 31 is not
112          changed.  If we had access to the processor carry flag,
113          we could close this loophole by putting the fourth hole
114          at bit 32!
115
116          So it ignores everything except 128's, when they're aligned
117          properly.  */
118
119       longword = *longword_ptr++;
120
121       if ((longword - lomagic) & himagic)
122         {
123           /* Which of the bytes was the zero?  If none of them were, it was
124              a misfire; continue the search.  */
125
126           const char *cp = (const char *) (longword_ptr - 1);
127
128           char_ptr = cp;
129           if (cp[0] == 0)
130             break;
131           char_ptr = cp + 1;
132           if (cp[1] == 0)
133             break;
134           char_ptr = cp + 2;
135           if (cp[2] == 0)
136             break;
137           char_ptr = cp + 3;
138           if (cp[3] == 0)
139             break;
140           if (sizeof (longword) > 4)
141             {
142               char_ptr = cp + 4;
143               if (cp[4] == 0)
144                 break;
145               char_ptr = cp + 5;
146               if (cp[5] == 0)
147                 break;
148               char_ptr = cp + 6;
149               if (cp[6] == 0)
150                 break;
151               char_ptr = cp + 7;
152               if (cp[7] == 0)
153                 break;
154             }
155         }
156       char_ptr = end_ptr;
157     }
158
159   if (char_ptr > end_ptr)
160     char_ptr = end_ptr;
161   return char_ptr - str;
162 }
163 #ifndef STRNLEN
164 weak_alias (__strnlen, strnlen)
165 #endif
166 libc_hidden_def (strnlen)