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 | digest_md5_getsubopt (char **optionp, |
47 | const char *const *tokens, char **valuep) |
48 | { |
49 | char *endp, *vstart; |
50 | int cnt; |
51 | int inside_quote = 0; |
52 | |
53 | if (**optionp == '\0') |
54 | return -1; |
55 | |
56 | /* Find end of next token. */ |
57 | endp = *optionp; |
58 | while (*endp != '\0' && (inside_quote || (!inside_quote && *endp != ','))) |
59 | { |
60 | if (*endp == '"') |
61 | inside_quote = !inside_quote; |
62 | endp++; |
63 | } |
64 | |
65 | /* Find start of value. */ |
66 | vstart = memchr (*optionp, '=', endp - *optionp); |
67 | if (vstart == NULL) |
68 | vstart = endp; |
69 | |
70 | /* Try to match the characters between *OPTIONP and VSTART against |
71 | one of the TOKENS. */ |
72 | for (cnt = 0; tokens[cnt] != NULL; ++cnt) |
73 | if (memcmp (*optionp, tokens[cnt], vstart - *optionp) == 0 |
74 | && tokens[cnt][vstart - *optionp] == '\0') |
75 | { |
76 | /* We found the current option in TOKENS. */ |
77 | *valuep = vstart != endp ? vstart + 1 : NULL; |
78 | |
79 | while (*valuep && (**valuep == ' ' || |
80 | **valuep == '\t' || |
81 | **valuep == '\r' || |
82 | **valuep == '\n' || **valuep == '"')) |
83 | (*valuep)++; |
84 | |
85 | if (*endp != '\0') |
86 | { |
87 | *endp = '\0'; |
88 | *optionp = endp + 1; |
89 | } |
90 | else |
91 | *optionp = endp; |
92 | endp--; |
93 | while (*endp == ' ' || |
94 | *endp == '\t' || |
95 | *endp == '\r' || *endp == '\n' || *endp == '"') |
96 | *endp-- = '\0'; |
97 | while (**optionp == ' ' || |
98 | **optionp == '\t' || **optionp == '\r' || **optionp == '\n') |
99 | (*optionp)++; |
100 | |
101 | return cnt; |
102 | } |
103 | |
104 | /* The current suboption does not match any option. */ |
105 | *valuep = *optionp; |
106 | |
107 | if (*endp != '\0') |
108 | *endp++ = '\0'; |
109 | *optionp = endp; |
110 | while (**optionp == ' ' || |
111 | **optionp == '\t' || **optionp == '\r' || **optionp == '\n') |
112 | (*optionp)++; |
113 | |
114 | return -1; |
115 | } |
116 | |