gsasl  2.2.1
getsubopt.c
Go to the documentation of this file.
1 /* getsubopt.c --- Parse comma separate list into words, DIGEST-MD5 style.
2  * Copyright (C) 2002-2024 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 #include <config.h>
28 
29 /* Get prototypes. */
30 #include "parser.h"
31 
32 /* Get memchr and memcmp. */
33 #include <string.h>
34 
35 /* Parse comma separated suboption from *OPTIONP and match against
36  strings in TOKENS. If found return index and set *VALUEP to
37  optional value introduced by an equal sign. If the suboption is
38  not part of TOKENS return in *VALUEP beginning of unknown
39  suboption. On exit *OPTIONP is set to the beginning of the next
40  token or at the terminating NUL character.
41 
42  This function is NOT identical to standard getsubopt! */
43 int
44 digest_md5_getsubopt (char **optionp,
45  const char *const *tokens, char **valuep)
46 {
47  char *endp, *vstart;
48  int cnt;
49  int inside_quote = 0;
50 
51  if (**optionp == '\0')
52  return -1;
53 
54  /* Find end of next token. */
55  endp = *optionp;
56  while (*endp != '\0' && (inside_quote || (!inside_quote && *endp != ',')))
57  {
58  if (*endp == '"')
59  inside_quote = !inside_quote;
60  endp++;
61  }
62 
63  /* Find start of value. */
64  vstart = memchr (*optionp, '=', endp - *optionp);
65  if (vstart == NULL)
66  vstart = endp;
67 
68  /* Try to match the characters between *OPTIONP and VSTART against
69  one of the TOKENS. */
70  for (cnt = 0; tokens[cnt] != NULL; ++cnt)
71  if (strncmp (*optionp, tokens[cnt], vstart - *optionp) == 0
72  && tokens[cnt][vstart - *optionp] == '\0')
73  {
74  /* We found the current option in TOKENS. */
75  *valuep = vstart != endp ? vstart + 1 : NULL;
76 
77  while (*valuep && (**valuep == ' ' ||
78  **valuep == '\t' ||
79  **valuep == '\r' ||
80  **valuep == '\n' || **valuep == '"'))
81  (*valuep)++;
82 
83  if (*endp != '\0')
84  {
85  *endp = '\0';
86  *optionp = endp + 1;
87  }
88  else
89  *optionp = endp;
90  endp--;
91  while (*endp == ' ' ||
92  *endp == '\t' ||
93  *endp == '\r' || *endp == '\n' || *endp == '"')
94  *endp-- = '\0';
95  while (**optionp == ' ' ||
96  **optionp == '\t' || **optionp == '\r' || **optionp == '\n')
97  (*optionp)++;
98 
99  return cnt;
100  }
101 
102  /* The current suboption does not match any option. */
103  *valuep = *optionp;
104 
105  if (*endp != '\0')
106  *endp++ = '\0';
107  *optionp = endp;
108  while (**optionp == ' ' ||
109  **optionp == '\t' || **optionp == '\r' || **optionp == '\n')
110  (*optionp)++;
111 
112  return -1;
113 }
int digest_md5_getsubopt(char **optionp, const char *const *tokens, char **valuep)
Definition: getsubopt.c:44