2 * Routines for parsing media type parameters as per RFC 822 and RFC 2045
3 * Copyright 2004, Anders Broman.
4 * Copyright 2004, Olivier Biot.
6 * Refer to the AUTHORS file or the AUTHORS section in the man page
7 * for contacting the author(s) of this file.
9 * Wireshark - Network traffic analyzer
10 * By Gerald Combs <gerald@wireshark.org>
11 * Copyright 1998 Gerald Combs
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34 #include <epan/media_params.h>
37 ws_get_next_media_type_parameter(const char *pos, gsize *retnamelen,
38 const char **retvalue, gsize *retvaluelen,
41 const char *p, *namep, *valuep;
45 while ((c = *p) != '\0' && g_ascii_isspace(c))
46 p++; /* Skip white space */
49 /* No more parameters left */
55 /* Look for a '\0' (end of string), '=' (end of parameter name,
56 beginning of parameter value), or ';' (end of parameter). */
57 while ((c = *p) != '\0' && c != '=' && c != ';')
59 *retnamelen = (gsize) (p - namep);
61 /* End of string, so end of parameter, no parameter value */
64 if (retvaluelen != NULL)
70 /* End of parameter, no parameter value */
73 if (retvaluelen != NULL)
78 /* The parameter has a value. Skip the '=' */
83 /* Is the value a quoted string? */
85 /* Yes. Skip the opening quote, and scan forward looking for
86 a non-escaped closing quote. */
91 /* End-of-string. We're done.
92 (XXX - this is an error.) */
93 if (retvaluelen != NULL) {
94 *retvaluelen = (gsize) (p - valuep);
100 /* Closing quote. Skip it; we're done with
101 the quoted-string. */
106 /* Backslash; this escapes the next character
107 (quoted-pair). Skip the backslash, and make
108 sure there *is* a next character. */
111 /* Nothing left; we're done.
112 (XXX - this is an error.) */
116 /* Skip the character we just processed. */
119 /* Now scan forward looking for a '\0' (end of string)
120 or ';' (end of parameter), in case there's any
121 extra cruft after the quoted-string. */
122 while ((c = *p) != '\0' && c != ';')
125 /* No. Just scan forward looking for a '\0' (end
126 of string) or ';' (end of parameter). */
127 while ((c = *p) != '\0' && c != ';')
131 /* End of string, so end of parameter */
132 if (retvaluelen != NULL) {
133 *retvaluelen = (gsize) (p - valuep);
138 /* End of parameter; point past the terminating ';' */
139 if (retvaluelen != NULL) {
140 *retvaluelen = (gsize) (p - valuep);
147 ws_find_media_type_parameter(const char *parameters, const char *key)
149 const char *p, *name, *value;
151 gsize keylen, namelen, valuelen;
154 if (parameters == NULL || key == NULL)
155 /* we won't be able to find anything */
158 keylen = (gsize) strlen(key);
160 /* There's no parameter name to searh for */
165 /* There are no parameters in which to search */
170 /* Get the next parameter. */
171 name = ws_get_next_media_type_parameter(p, &namelen, &value,
174 /* No more parameters - not found. */
178 /* Is it the parameter we're looking for? */
179 if (namelen == keylen && g_ascii_strncasecmp(name, key, keylen) == 0) {
186 /* The parameter doesn't have a value. */
190 /* We found the parameter with that name; now extract the value. */
191 valuestr = (char *)g_malloc(valuelen + 1);
194 /* Is the value a quoted string? */
196 /* Yes. Skip the opening quote, and scan forward looking for
197 a non-escaped closing quote, copying characters. */
202 /* End-of-string. We're done.
203 (XXX - this is an error.) */
208 /* Closing quote. Skip it; we're done with
209 the quoted-string. */
214 /* Backslash; this escapes the next character
215 (quoted-pair). Skip the backslash, and make
216 sure there *is* a next character. */
219 /* Nothing left; we're done.
220 (XXX - this is an error.) */
224 /* Copy the character. */
228 /* No. Just scan forward until we see a '\0' (end of
229 string or a non-token character, copying characters. */
230 while ((c = *p) != '\0' && g_ascii_isgraph(c) && c != '(' &&
231 c != ')' && c != '<' && c != '>' && c != '@' &&
232 c != ',' && c != ';' && c != ':' && c != '\\' &&
233 c != '"' && c != '/' && c != '[' && c != ']' &&
234 c != '?' && c != '=' && c != '{' && c != '}') {