gsasl  1.8.0
getsubopt.c
Go to the documentation of this file.
00001 /* getsubopt.c --- Parse comma separate list into words, DIGEST-MD5 style.
00002  * Copyright (C) 2002-2012 Simon Josefsson
00003  * Copyright (C) 1996, 1997, 1999 Free Software Foundation, Inc.
00004  * From the GNU C Library, under GNU LGPL version 2.1.
00005  * Contributed by Ulrich Drepper <drepper@cygnus.com>, 1996.
00006  * Modified for Libgsasl by Simon Josefsson <simon@josefsson.org>
00007  *
00008  * This file is part of GNU SASL Library.
00009  *
00010  * GNU SASL Library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public License
00012  * as published by the Free Software Foundation; either version 2.1 of
00013  * the License, or (at your option) any later version.
00014  *
00015  * GNU SASL Library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with GNU SASL Library; if not, write to the Free
00022  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00023  * Boston, MA 02110-1301, USA.
00024  *
00025  */
00026 
00027 #ifdef HAVE_CONFIG_H
00028 #include "config.h"
00029 #endif
00030 
00031 /* Get prototypes. */
00032 #include "parser.h"
00033 
00034 /* Get memchr and memcmp. */
00035 #include <string.h>
00036 
00037 /* Parse comma separated suboption from *OPTIONP and match against
00038    strings in TOKENS.  If found return index and set *VALUEP to
00039    optional value introduced by an equal sign.  If the suboption is
00040    not part of TOKENS return in *VALUEP beginning of unknown
00041    suboption.  On exit *OPTIONP is set to the beginning of the next
00042    token or at the terminating NUL character.
00043 
00044    This function is NOT identical to standard getsubopt! */
00045 int
00046 digest_md5_getsubopt (char **optionp,
00047                       const char *const *tokens, char **valuep)
00048 {
00049   char *endp, *vstart;
00050   int cnt;
00051   int inside_quote = 0;
00052 
00053   if (**optionp == '\0')
00054     return -1;
00055 
00056   /* Find end of next token.  */
00057   endp = *optionp;
00058   while (*endp != '\0' && (inside_quote || (!inside_quote && *endp != ',')))
00059     {
00060       if (*endp == '"')
00061         inside_quote = !inside_quote;
00062       endp++;
00063     }
00064 
00065   /* Find start of value.  */
00066   vstart = memchr (*optionp, '=', endp - *optionp);
00067   if (vstart == NULL)
00068     vstart = endp;
00069 
00070   /* Try to match the characters between *OPTIONP and VSTART against
00071      one of the TOKENS.  */
00072   for (cnt = 0; tokens[cnt] != NULL; ++cnt)
00073     if (memcmp (*optionp, tokens[cnt], vstart - *optionp) == 0
00074         && tokens[cnt][vstart - *optionp] == '\0')
00075       {
00076         /* We found the current option in TOKENS.  */
00077         *valuep = vstart != endp ? vstart + 1 : NULL;
00078 
00079         while (*valuep && (**valuep == ' ' ||
00080                            **valuep == '\t' ||
00081                            **valuep == '\r' ||
00082                            **valuep == '\n' || **valuep == '"'))
00083           (*valuep)++;
00084 
00085         if (*endp != '\0')
00086           {
00087             *endp = '\0';
00088             *optionp = endp + 1;
00089           }
00090         else
00091           *optionp = endp;
00092         endp--;
00093         while (*endp == ' ' ||
00094                *endp == '\t' ||
00095                *endp == '\r' || *endp == '\n' || *endp == '"')
00096           *endp-- = '\0';
00097         while (**optionp == ' ' ||
00098                **optionp == '\t' || **optionp == '\r' || **optionp == '\n')
00099           (*optionp)++;
00100 
00101         return cnt;
00102       }
00103 
00104   /* The current suboption does not match any option.  */
00105   *valuep = *optionp;
00106 
00107   if (*endp != '\0')
00108     *endp++ = '\0';
00109   *optionp = endp;
00110   while (**optionp == ' ' ||
00111          **optionp == '\t' || **optionp == '\r' || **optionp == '\n')
00112     (*optionp)++;
00113 
00114   return -1;
00115 }