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 * SPDX-License-Identifier: GPL-2.0-or-later
21 #include <epan/media_params.h>
24 ws_get_next_media_type_parameter(const char *pos
, size_t *retnamelen
,
25 const char **retvalue
, size_t *retvaluelen
,
28 const char *p
, *namep
, *valuep
;
32 while ((c
= *p
) != '\0' && g_ascii_isspace(c
))
33 p
++; /* Skip white space */
36 /* No more parameters left */
42 /* Look for a '\0' (end of string), '=' (end of parameter name,
43 beginning of parameter value), or ';' (end of parameter). */
44 while ((c
= *p
) != '\0' && c
!= '=' && c
!= ';')
46 *retnamelen
= (size_t) (p
- namep
);
48 /* End of string, so end of parameter, no parameter value */
51 if (retvaluelen
!= NULL
)
57 /* End of parameter, no parameter value */
60 if (retvaluelen
!= NULL
)
65 /* The parameter has a value. Skip the '=' */
70 /* Is the value a quoted string? */
72 /* Yes. Skip the opening quote, and scan forward looking for
73 a non-escaped closing quote. */
78 /* End-of-string. We're done.
79 (XXX - this is an error.) */
80 if (retvaluelen
!= NULL
) {
81 *retvaluelen
= (size_t) (p
- valuep
);
87 /* Closing quote. Skip it; we're done with
93 /* Backslash; this escapes the next character
94 (quoted-pair). Skip the backslash, and make
95 sure there *is* a next character. */
98 /* Nothing left; we're done.
99 (XXX - this is an error.) */
103 /* Skip the character we just processed. */
106 /* Now scan forward looking for a '\0' (end of string)
107 or ';' (end of parameter), in case there's any
108 extra cruft after the quoted-string. */
109 while ((c
= *p
) != '\0' && c
!= ';')
112 /* No. Just scan forward looking for a '\0' (end
113 of string) or ';' (end of parameter). */
114 while ((c
= *p
) != '\0' && c
!= ';')
118 /* End of string, so end of parameter */
119 if (retvaluelen
!= NULL
) {
120 *retvaluelen
= (size_t) (p
- valuep
);
125 /* End of parameter; point past the terminating ';' */
126 if (retvaluelen
!= NULL
) {
127 *retvaluelen
= (size_t) (p
- valuep
);
134 ws_find_media_type_parameter(wmem_allocator_t
*scope
, const char *parameters
, const char *key
)
136 const char *p
, *name
, *value
;
138 size_t keylen
, namelen
, valuelen
;
141 if (parameters
== NULL
|| key
== NULL
)
142 /* we won't be able to find anything */
145 keylen
= (size_t) strlen(key
);
147 /* There's no parameter name to search for */
152 /* There are no parameters in which to search */
157 /* Get the next parameter. */
158 name
= ws_get_next_media_type_parameter(p
, &namelen
, &value
,
161 /* No more parameters - not found. */
165 /* Is it the parameter we're looking for? */
166 if (namelen
== keylen
&& g_ascii_strncasecmp(name
, key
, keylen
) == 0) {
173 /* The parameter doesn't have a value. */
177 /* We found the parameter with that name; now extract the value. */
178 valuestr
= (char *)wmem_alloc(scope
, valuelen
+ 1);
181 /* Is the value a quoted string? */
183 /* Yes. Skip the opening quote, and scan forward looking for
184 a non-escaped closing quote, copying characters. */
189 /* End-of-string. We're done.
190 (XXX - this is an error.) */
195 /* Closing quote. Skip it; we're done with
196 the quoted-string. */
201 /* Backslash; this escapes the next character
202 (quoted-pair). Skip the backslash, and make
203 sure there *is* a next character. */
206 /* Nothing left; we're done.
207 (XXX - this is an error.) */
211 /* Copy the character. */
215 /* No. Just scan forward until we see a '\0' (end of
216 string or a non-token character, copying characters. */
217 while ((c
= *p
) != '\0' && g_ascii_isgraph(c
) && c
!= '(' &&
218 c
!= ')' && c
!= '<' && c
!= '>' && c
!= '@' &&
219 c
!= ',' && c
!= ';' && c
!= ':' && c
!= '\\' &&
220 c
!= '"' && c
!= '/' && c
!= '[' && c
!= ']' &&
221 c
!= '?' && c
!= '=' && c
!= '{' && c
!= '}') {