1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
/* media_params.c
* Routines for parsing media type parameters as per RFC 822 and RFC 2045
* Copyright 2004, Anders Broman.
* Copyright 2004, Olivier Biot.
*
* Refer to the AUTHORS file or the AUTHORS section in the man page
* for contacting the author(s) of this file.
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <gerald@wireshark.org>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include <string.h>
#include <glib.h>
#include <epan/media_params.h>
static const char *
ws_get_next_media_type_parameter(const char *pos, size_t *retnamelen,
const char **retvalue, size_t *retvaluelen,
const char **nextp)
{
const char *p, *namep, *valuep;
char c;
p = pos;
while ((c = *p) != '\0' && g_ascii_isspace(c))
p++; /* Skip white space */
if (c == '\0') {
/* No more parameters left */
return NULL;
}
namep = p;
/* Look for a '\0' (end of string), '=' (end of parameter name,
beginning of parameter value), or ';' (end of parameter). */
while ((c = *p) != '\0' && c != '=' && c != ';')
p++;
*retnamelen = (size_t) (p - namep);
if (c == '\0') {
/* End of string, so end of parameter, no parameter value */
if (retvalue != NULL)
*retvalue = NULL;
if (retvaluelen != NULL)
*retvaluelen = 0;
*nextp = p;
return namep;
}
if (c == ';') {
/* End of parameter, no parameter value */
if (retvalue != NULL)
*retvalue = NULL;
if (retvaluelen != NULL)
*retvaluelen = 0;
*nextp = p + 1;
return namep;
}
/* The parameter has a value. Skip the '=' */
p++;
valuep = p;
if (retvalue != NULL)
*retvalue = valuep;
/* Is the value a quoted string? */
if (*p == '"') {
/* Yes. Skip the opening quote, and scan forward looking for
a non-escaped closing quote. */
p++;
for (;;) {
c = *p;
if (c == '\0') {
/* End-of-string. We're done.
(XXX - this is an error.) */
if (retvaluelen != NULL) {
*retvaluelen = (size_t) (p - valuep);
}
*nextp = p;
return namep;
}
if (c == '"') {
/* Closing quote. Skip it; we're done with
the quoted-string. */
p++;
break;
}
if (c == '\\') {
/* Backslash; this escapes the next character
(quoted-pair). Skip the backslash, and make
sure there *is* a next character. */
p++;
if (*p == '\0') {
/* Nothing left; we're done.
(XXX - this is an error.) */
break;
}
}
/* Skip the character we just processed. */
p++;
}
/* Now scan forward looking for a '\0' (end of string)
or ';' (end of parameter), in case there's any
extra cruft after the quoted-string. */
while ((c = *p) != '\0' && c != ';')
p++;
} else {
/* No. Just scan forward looking for a '\0' (end
of string) or ';' (end of parameter). */
while ((c = *p) != '\0' && c != ';')
p++;
}
if (c == '\0') {
/* End of string, so end of parameter */
if (retvaluelen != NULL) {
*retvaluelen = (size_t) (p - valuep);
}
*nextp = p;
return namep;
}
/* End of parameter; point past the terminating ';' */
if (retvaluelen != NULL) {
*retvaluelen = (size_t) (p - valuep);
}
*nextp = p + 1;
return namep;
}
char *
ws_find_media_type_parameter(wmem_allocator_t *scope, const char *parameters, const char *key)
{
const char *p, *name, *value;
char c;
size_t keylen, namelen, valuelen;
char *valuestr, *vp;
if (parameters == NULL || key == NULL)
/* we won't be able to find anything */
return NULL;
keylen = (size_t) strlen(key);
if (keylen == 0) {
/* There's no parameter name to search for */
return NULL;
}
p = parameters;
if (*p == '\0') {
/* There are no parameters in which to search */
return NULL;
}
do {
/* Get the next parameter. */
name = ws_get_next_media_type_parameter(p, &namelen, &value,
&valuelen, &p);
if (name == NULL) {
/* No more parameters - not found. */
return NULL;
}
/* Is it the parameter we're looking for? */
if (namelen == keylen && g_ascii_strncasecmp(name, key, keylen) == 0) {
/* Yes. */
break;
}
} while (*p);
if (value == NULL) {
/* The parameter doesn't have a value. */
return NULL;
}
/* We found the parameter with that name; now extract the value. */
valuestr = (char *)wmem_alloc(scope, valuelen + 1);
vp = valuestr;
p = value;
/* Is the value a quoted string? */
if (*p == '"') {
/* Yes. Skip the opening quote, and scan forward looking for
a non-escaped closing quote, copying characters. */
p++;
for (;;) {
c = *p;
if (c == '\0') {
/* End-of-string. We're done.
(XXX - this is an error.) */
*vp = '\0';
return valuestr;
}
if (c == '"') {
/* Closing quote. Skip it; we're done with
the quoted-string. */
p++;
break;
}
if (c == '\\') {
/* Backslash; this escapes the next character
(quoted-pair). Skip the backslash, and make
sure there *is* a next character. */
p++;
if (*p == '\0') {
/* Nothing left; we're done.
(XXX - this is an error.) */
break;
}
}
/* Copy the character. */
*vp++ = *p++;
}
} else {
/* No. Just scan forward until we see a '\0' (end of
string or a non-token character, copying characters. */
while ((c = *p) != '\0' && g_ascii_isgraph(c) && c != '(' &&
c != ')' && c != '<' && c != '>' && c != '@' &&
c != ',' && c != ';' && c != ':' && c != '\\' &&
c != '"' && c != '/' && c != '[' && c != ']' &&
c != '?' && c != '=' && c != '{' && c != '}') {
*vp++ = c;
p++;
}
}
*vp = '\0';
return valuestr;
}
|