Debian Codegraph @

include/v4l-getsubopt.h

raw
1 #ifndef __V4L_GETSUBOPT__
2 #define __V4L_GETSUBOPT__
3
4 /*
5 * Apparently non-glibc variants of getsubopt behave slightly
6 * differently, and on some OSes (Android) it is missing altogether.
7 * So add support for it here, if needed.
8 */
9
10 #if defined(__GLIBC__) && !defined(GETSUBOPT_IGNORE_COMMA_IN_BRACKET)
11
12 #define v4l_getsubopt getsubopt
13
14 #else
15
16 #include <string.h>
17 #include <stdlib.h>
18
19 #if defined (GETSUBOPT_IGNORE_COMMA_IN_BRACKET)
20 /*
21 * Find the first occurrence of C in S or the final NUL byte.
22 * But ignore the C in brackets
23 */
24 static inline char *v4l_strchrnul (const char *s, int c_in)
25 {
26 const unsigned char *char_ptr;
27 unsigned char c;
28 int in_brackets = 0;
29
30 c = (unsigned char) c_in;
31
32 char_ptr = (const unsigned char *) s;
33 while (*char_ptr) {
34 if (*char_ptr == '(')
35 in_brackets++;
36 else if (*char_ptr == ')')
37 in_brackets--;
38 else if (*char_ptr == c && in_brackets == 0)
39 break;
40 ++char_ptr;
41 }
42 return (char *) char_ptr;
43 }
44 #else
45 /*
46 * Import strchrnul(...) from uClibc version 0.9.33.2 since this feature is
47 * missing in the Android C library.
48 */
49
50 /* Copyright (C) 1991,93,94,95,96,97,99,2000 Free Software Foundation, Inc.
51 This file is part of the GNU C Library.
52 Based on strlen implementation by Torbjorn Granlund ([email protected]),
53 with help from Dan Sahlin ([email protected]) and
54 bug fix and commentary by Jim Blandy ([email protected]);
55 adaptation to strchr suggested by Dick Karpinski ([email protected]),
56 and implemented by Roland McGrath ([email protected]).
57
58 The GNU C Library is free software; you can redistribute it and/or
59 modify it under the terms of the GNU Lesser General Public
60 License as published by the Free Software Foundation; either
61 version 2.1 of the License, or (at your option) any later version.
62
63 The GNU C Library is distributed in the hope that it will be useful,
64 but WITHOUT ANY WARRANTY; without even the implied warranty of
65 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
66 Lesser General Public License for more details.
67
68 You should have received a copy of the GNU Lesser General Public
69 License along with the GNU C Library; if not, write to the Free
70 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
71 02111-1307 USA. */
72
73 /* Find the first occurrence of C in S or the final NUL byte. */
74 static inline char *v4l_strchrnul (const char *s, int c_in)
75 {
76 const unsigned char *char_ptr;
77 const unsigned long int *longword_ptr;
78 unsigned long int longword, magic_bits, charmask;
79 unsigned char c;
80
81 c = (unsigned char) c_in;
82
83 /* Handle the first few characters by reading one character at a time.
84 Do this until CHAR_PTR is aligned on a longword boundary. */
85 for (char_ptr = (const unsigned char *) s;
86 ((unsigned long int) char_ptr & (sizeof (longword) - 1)) != 0;
87 ++char_ptr)
88 if (*char_ptr == c || *char_ptr == '\0')
89 return (char *) char_ptr;
90
91 /* All these elucidatory comments refer to 4-byte longwords,
92 but the theory applies equally well to 8-byte longwords. */
93
94 longword_ptr = (unsigned long int *) char_ptr;
95
96 /* Bits 31, 24, 16, and 8 of this number are zero. Call these bits
97 the "holes." Note that there is a hole just to the left of
98 each byte, with an extra at the end:
99
100 bits: 01111110 11111110 11111110 11111111
101 bytes: AAAAAAAA BBBBBBBB CCCCCCCC DDDDDDDD
102
103 The 1-bits make sure that carries propagate to the next 0-bit.
104 The 0-bits provide holes for carries to fall into. */
105 switch (sizeof (longword))
106 {
107 case 4: magic_bits = 0x7efefeffL; break;
108 case 8: magic_bits = ((0x7efefefeL << 16) << 16) | 0xfefefeffL; break;
109 default:
110 abort ();
111 }
112
113 /* Set up a longword, each of whose bytes is C. */
114 charmask = c | (c << 8);
115 charmask |= charmask << 16;
116 if (sizeof (longword) > 4)
117 /* Do the shift in two steps to avoid a warning if long has 32 bits. */
118 charmask |= (charmask << 16) << 16;
119 if (sizeof (longword) > 8)
120 abort ();
121
122 /* Instead of the traditional loop which tests each character,
123 we will test a longword at a time. The tricky part is testing
124 if *any of the four* bytes in the longword in question are zero. */
125 for (;;)
126 {
127 /* We tentatively exit the loop if adding MAGIC_BITS to
128 LONGWORD fails to change any of the hole bits of LONGWORD.
129
130 1) Is this safe? Will it catch all the zero bytes?
131 Suppose there is a byte with all zeros. Any carry bits
132 propagating from its left will fall into the hole at its
133 least significant bit and stop. Since there will be no
134 carry from its most significant bit, the LSB of the
135 byte to the left will be unchanged, and the zero will be
136 detected.
137
138 2) Is this worthwhile? Will it ignore everything except
139 zero bytes? Suppose every byte of LONGWORD has a bit set
140 somewhere. There will be a carry into bit 8. If bit 8
141 is set, this will carry into bit 16. If bit 8 is clear,
142 one of bits 9-15 must be set, so there will be a carry
143 into bit 16. Similarly, there will be a carry into bit
144 24. If one of bits 24-30 is set, there will be a carry
145 into bit 31, so all of the hole bits will be changed.
146
147 The one misfire occurs when bits 24-30 are clear and bit
148 31 is set; in this case, the hole at bit 31 is not
149 changed. If we had access to the processor carry flag,
150 we could close this loophole by putting the fourth hole
151 at bit 32!
152
153 So it ignores everything except 128's, when they're aligned
154 properly.
155
156 3) But wait! Aren't we looking for C as well as zero?
157 Good point. So what we do is XOR LONGWORD with a longword,
158 each of whose bytes is C. This turns each byte that is C
159 into a zero. */
160
161 longword = *longword_ptr++;
162
163 /* Add MAGIC_BITS to LONGWORD. */
164 if ((((longword + magic_bits)
165
166 /* Set those bits that were unchanged by the addition. */
167 ^ ~longword)
168
169 /* Look at only the hole bits. If any of the hole bits
170 are unchanged, most likely one of the bytes was a
171 zero. */
172 & ~magic_bits) != 0 ||
173
174 /* That caught zeroes. Now test for C. */
175 ((((longword ^ charmask) + magic_bits) ^ ~(longword ^ charmask))
176 & ~magic_bits) != 0)
177 {
178 /* Which of the bytes was C or zero?
179 If none of them were, it was a misfire; continue the search. */
180
181 const unsigned char *cp = (const unsigned char *) (longword_ptr - 1);
182
183 if (*cp == c || *cp == '\0')
184 return (char *) cp;
185 if (*++cp == c || *cp == '\0')
186 return (char *) cp;
187 if (*++cp == c || *cp == '\0')
188 return (char *) cp;
189 if (*++cp == c || *cp == '\0')
190 return (char *) cp;
191 if (sizeof (longword) > 4)
192 {
193 if (*++cp == c || *cp == '\0')
194 return (char *) cp;
195 if (*++cp == c || *cp == '\0')
196 return (char *) cp;
197 if (*++cp == c || *cp == '\0')
198 return (char *) cp;
199 if (*++cp == c || *cp == '\0')
200 return (char *) cp;
201 }
202 }
203 }
204
205 /* This should never happen. */
206 return NULL;
207 }
208 #endif
209
210 /*
211 * Import getsubopt(...) from uClibc version 0.9.33.2 since this feature is
212 * missing in the Android C library.
213 */
214
215 /* Parse comma separate list into words.
216 Copyright (C) 1996, 1997, 1999, 2004 Free Software Foundation, Inc.
217 This file is part of the GNU C Library.
218 Contributed by Ulrich Drepper <[email protected]>, 1996.
219
220 The GNU C Library is free software; you can redistribute it and/or
221 modify it under the terms of the GNU Lesser General Public
222 License as published by the Free Software Foundation; either
223 version 2.1 of the License, or (at your option) any later version.
224
225 The GNU C Library is distributed in the hope that it will be useful,
226 but WITHOUT ANY WARRANTY; without even the implied warranty of
227 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
228 Lesser General Public License for more details.
229
230 You should have received a copy of the GNU Lesser General Public
231 License along with the GNU C Library; if not, write to the Free
232 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
233 02111-1307 USA. */
234
235 /* Parse comma separated suboption from *OPTIONP and match against
236 strings in TOKENS. If found return index and set *VALUEP to
237 optional value introduced by an equal sign. If the suboption is
238 not part of TOKENS return in *VALUEP beginning of unknown
239 suboption. On exit *OPTIONP is set to the beginning of the next
240 token or at the terminating NUL character. */
241 static inline int
242 v4l_getsubopt (char **optionp, char *const *tokens, char **valuep)
243 {
244 char *endp, *vstart;
245 int cnt;
246
247 if (**optionp == '\0')
248 return -1;
249
250 /* Find end of next token. */
251 endp = v4l_strchrnul (*optionp, ',');
252
253 /* Find start of value. */
254 vstart = (char *) memchr (*optionp, '=', endp - *optionp);
255 if (vstart == NULL)
256 vstart = endp;
257
258 /* Try to match the characters between *OPTIONP and VSTART against
259 one of the TOKENS. */
260 for (cnt = 0; tokens[cnt] != NULL; ++cnt)
261 if (strncmp (*optionp, tokens[cnt], vstart - *optionp) == 0
262 && tokens[cnt][vstart - *optionp] == '\0')
263 {
264 /* We found the current option in TOKENS. */
265 *valuep = vstart != endp ? vstart + 1 : NULL;
266
267 if (*endp != '\0')
268 *endp++ = '\0';
269 *optionp = endp;
270
271 return cnt;
272 }
273
274 /* The current suboption does not match any option. */
275 *valuep = *optionp;
276
277 if (*endp != '\0')
278 *endp++ = '\0';
279 *optionp = endp;
280
281 return -1;
282 }
283
284 #endif
285
286 #endif