Branch data Line data Source code
1 : : /* getsubopt.c --- Parse comma separate list into words, DIGEST-MD5 style.
2 : : * Copyright (C) 2002-2012 Simon Josefsson
3 : : * Copyright (C) 1996, 1997, 1999 Free Software Foundation, Inc.
4 : : * From the GNU C Library, under GNU LGPL version 2.1.
5 : : * Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
6 : : * Modified for Libgsasl by Simon Josefsson <simon@josefsson.org>
7 : : *
8 : : * This file is part of GNU SASL Library.
9 : : *
10 : : * GNU SASL Library is free software; you can redistribute it and/or
11 : : * modify it under the terms of the GNU Lesser General Public License
12 : : * as published by the Free Software Foundation; either version 2.1 of
13 : : * the License, or (at your option) any later version.
14 : : *
15 : : * GNU SASL Library is distributed in the hope that it will be useful,
16 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 : : * Lesser General Public License for more details.
19 : : *
20 : : * You should have received a copy of the GNU Lesser General Public
21 : : * License along with GNU SASL Library; if not, write to the Free
22 : : * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 : : * Boston, MA 02110-1301, USA.
24 : : *
25 : : */
26 : :
27 : : #ifdef HAVE_CONFIG_H
28 : : #include "config.h"
29 : : #endif
30 : :
31 : : /* Get prototypes. */
32 : : #include "parser.h"
33 : :
34 : : /* Get memchr and memcmp. */
35 : : #include <string.h>
36 : :
37 : : /* Parse comma separated suboption from *OPTIONP and match against
38 : : strings in TOKENS. If found return index and set *VALUEP to
39 : : optional value introduced by an equal sign. If the suboption is
40 : : not part of TOKENS return in *VALUEP beginning of unknown
41 : : suboption. On exit *OPTIONP is set to the beginning of the next
42 : : token or at the terminating NUL character.
43 : :
44 : : This function is NOT identical to standard getsubopt! */
45 : : int
46 : 281 : digest_md5_getsubopt (char **optionp,
47 : : const char *const *tokens, char **valuep)
48 : : {
49 : : char *endp, *vstart;
50 : : int cnt;
51 : 281 : int inside_quote = 0;
52 : :
53 [ - + ]: 281 : if (**optionp == '\0')
54 : 0 : return -1;
55 : :
56 : : /* Find end of next token. */
57 : 281 : endp = *optionp;
58 [ + + ][ + + ]: 5546 : while (*endp != '\0' && (inside_quote || (!inside_quote && *endp != ',')))
[ + - ][ + + ]
59 : : {
60 [ + + ]: 5265 : if (*endp == '"')
61 : 226 : inside_quote = !inside_quote;
62 : 5265 : endp++;
63 : : }
64 : :
65 : : /* Find start of value. */
66 : 281 : vstart = memchr (*optionp, '=', endp - *optionp);
67 [ + + ]: 281 : if (vstart == NULL)
68 : 38 : vstart = endp;
69 : :
70 : : /* Try to match the characters between *OPTIONP and VSTART against
71 : : one of the TOKENS. */
72 [ + + ]: 1260 : for (cnt = 0; tokens[cnt] != NULL; ++cnt)
73 [ + + ]: 1249 : if (memcmp (*optionp, tokens[cnt], vstart - *optionp) == 0
74 [ + - ]: 270 : && tokens[cnt][vstart - *optionp] == '\0')
75 : : {
76 : : /* We found the current option in TOKENS. */
77 [ + + ]: 270 : *valuep = vstart != endp ? vstart + 1 : NULL;
78 : :
79 [ + + ][ - + ]: 383 : while (*valuep && (**valuep == ' ' ||
[ - + ]
80 [ - + ]: 349 : **valuep == '\t' ||
81 [ - + ]: 349 : **valuep == '\r' ||
82 [ + + ]: 349 : **valuep == '\n' || **valuep == '"'))
83 : 113 : (*valuep)++;
84 : :
85 [ + + ]: 270 : if (*endp != '\0')
86 : : {
87 : 188 : *endp = '\0';
88 : 188 : *optionp = endp + 1;
89 : : }
90 : : else
91 : 82 : *optionp = endp;
92 : 270 : endp--;
93 [ - + ][ - + ]: 383 : while (*endp == ' ' ||
94 [ - + ]: 383 : *endp == '\t' ||
95 [ - + ][ + + ]: 383 : *endp == '\r' || *endp == '\n' || *endp == '"')
96 : 113 : *endp-- = '\0';
97 [ + + ][ - + ]: 458 : while (**optionp == ' ' ||
98 [ - + ][ - + ]: 270 : **optionp == '\t' || **optionp == '\r' || **optionp == '\n')
99 : 188 : (*optionp)++;
100 : :
101 : 270 : return cnt;
102 : : }
103 : :
104 : : /* The current suboption does not match any option. */
105 : 11 : *valuep = *optionp;
106 : :
107 [ + + ]: 11 : if (*endp != '\0')
108 : 4 : *endp++ = '\0';
109 : 11 : *optionp = endp;
110 [ + + ][ - + ]: 15 : while (**optionp == ' ' ||
111 [ - + ][ - + ]: 11 : **optionp == '\t' || **optionp == '\r' || **optionp == '\n')
112 : 4 : (*optionp)++;
113 : :
114 : 281 : return -1;
115 : : }
|