2 * String parsing visitor
4 * Copyright Red Hat, Inc. 2012-2016
6 * Author: Paolo Bonzini <pbonzini@redhat.com>
7 * David Hildenbrand <david@redhat.com>
9 * This work is licensed under the terms of the GNU LGPL, version 2.1 or later.
10 * See the COPYING.LIB file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qapi/string-input-visitor.h"
16 #include "qapi/visitor-impl.h"
17 #include "qapi/qmp/qerror.h"
18 #include "qapi/qmp/qnull.h"
19 #include "qemu/option.h"
20 #include "qemu/cutils.h"
22 typedef enum ListMode
{
23 /* no list parsing active / no list expected */
25 /* we have an unparsed string remaining */
27 /* we have an unfinished int64 range */
29 /* we have an unfinished uint64 range */
31 /* we have parsed the string completely and no range is remaining */
35 /* protect against DOS attacks, limit the amount of elements per range */
36 #define RANGE_MAX_ELEMENTS 65536
38 typedef union RangeElement
{
43 struct StringInputVisitor
47 /* List parsing state */
49 RangeElement rangeNext
;
50 RangeElement rangeEnd
;
51 const char *unparsed_string
;
54 /* The original string to parse */
58 static StringInputVisitor
*to_siv(Visitor
*v
)
60 return container_of(v
, StringInputVisitor
, visitor
);
63 static void start_list(Visitor
*v
, const char *name
, GenericList
**list
,
64 size_t size
, Error
**errp
)
66 StringInputVisitor
*siv
= to_siv(v
);
68 assert(siv
->lm
== LM_NONE
);
70 siv
->unparsed_string
= siv
->string
;
72 if (!siv
->string
[0]) {
79 *list
= g_malloc0(size
);
81 siv
->lm
= LM_UNPARSED
;
85 static GenericList
*next_list(Visitor
*v
, GenericList
*tail
, size_t size
)
87 StringInputVisitor
*siv
= to_siv(v
);
95 /* we have an unparsed string or something left in a range */
101 tail
->next
= g_malloc0(size
);
105 static void check_list(Visitor
*v
, Error
**errp
)
107 const StringInputVisitor
*siv
= to_siv(v
);
111 case LM_UINT64_RANGE
:
113 error_setg(errp
, "Fewer list elements expected");
122 static void end_list(Visitor
*v
, void **obj
)
124 StringInputVisitor
*siv
= to_siv(v
);
126 assert(siv
->lm
!= LM_NONE
);
127 assert(siv
->list
== obj
);
129 siv
->unparsed_string
= NULL
;
133 static int try_parse_int64_list_entry(StringInputVisitor
*siv
, int64_t *obj
)
138 /* parse a simple int64 or range */
139 if (qemu_strtoi64(siv
->unparsed_string
, &endptr
, 0, &start
)) {
146 siv
->unparsed_string
= endptr
;
149 siv
->unparsed_string
= endptr
+ 1;
152 /* parse the end of the range */
153 if (qemu_strtoi64(endptr
+ 1, &endptr
, 0, &end
)) {
156 if (start
> end
|| end
- start
>= RANGE_MAX_ELEMENTS
) {
161 siv
->unparsed_string
= endptr
;
164 siv
->unparsed_string
= endptr
+ 1;
174 /* we have a proper range (with maybe only one element) */
175 siv
->lm
= LM_INT64_RANGE
;
176 siv
->rangeNext
.i64
= start
;
177 siv
->rangeEnd
.i64
= end
;
181 static void parse_type_int64(Visitor
*v
, const char *name
, int64_t *obj
,
184 StringInputVisitor
*siv
= to_siv(v
);
189 /* just parse a simple int64, bail out if not completely consumed */
190 if (qemu_strtoi64(siv
->string
, NULL
, 0, &val
)) {
191 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
,
192 name
? name
: "null", "int64");
198 if (try_parse_int64_list_entry(siv
, obj
)) {
199 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
? name
: "null",
200 "list of int64 values or ranges");
203 assert(siv
->lm
== LM_INT64_RANGE
);
206 /* return the next element in the range */
207 assert(siv
->rangeNext
.i64
<= siv
->rangeEnd
.i64
);
208 *obj
= siv
->rangeNext
.i64
++;
210 if (siv
->rangeNext
.i64
> siv
->rangeEnd
.i64
|| *obj
== INT64_MAX
) {
211 /* end of range, check if there is more to parse */
212 siv
->lm
= siv
->unparsed_string
[0] ? LM_UNPARSED
: LM_END
;
216 error_setg(errp
, "Fewer list elements expected");
223 static int try_parse_uint64_list_entry(StringInputVisitor
*siv
, uint64_t *obj
)
228 /* parse a simple uint64 or range */
229 if (qemu_strtou64(siv
->unparsed_string
, &endptr
, 0, &start
)) {
236 siv
->unparsed_string
= endptr
;
239 siv
->unparsed_string
= endptr
+ 1;
242 /* parse the end of the range */
243 if (qemu_strtou64(endptr
+ 1, &endptr
, 0, &end
)) {
246 if (start
> end
|| end
- start
>= RANGE_MAX_ELEMENTS
) {
251 siv
->unparsed_string
= endptr
;
254 siv
->unparsed_string
= endptr
+ 1;
264 /* we have a proper range (with maybe only one element) */
265 siv
->lm
= LM_UINT64_RANGE
;
266 siv
->rangeNext
.u64
= start
;
267 siv
->rangeEnd
.u64
= end
;
271 static void parse_type_uint64(Visitor
*v
, const char *name
, uint64_t *obj
,
274 StringInputVisitor
*siv
= to_siv(v
);
279 /* just parse a simple uint64, bail out if not completely consumed */
280 if (qemu_strtou64(siv
->string
, NULL
, 0, &val
)) {
281 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
? name
: "null",
288 if (try_parse_uint64_list_entry(siv
, obj
)) {
289 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, name
? name
: "null",
290 "list of uint64 values or ranges");
293 assert(siv
->lm
== LM_UINT64_RANGE
);
295 case LM_UINT64_RANGE
:
296 /* return the next element in the range */
297 assert(siv
->rangeNext
.u64
<= siv
->rangeEnd
.u64
);
298 *obj
= siv
->rangeNext
.u64
++;
300 if (siv
->rangeNext
.u64
> siv
->rangeEnd
.u64
|| *obj
== UINT64_MAX
) {
301 /* end of range, check if there is more to parse */
302 siv
->lm
= siv
->unparsed_string
[0] ? LM_UNPARSED
: LM_END
;
306 error_setg(errp
, "Fewer list elements expected");
313 static void parse_type_size(Visitor
*v
, const char *name
, uint64_t *obj
,
316 StringInputVisitor
*siv
= to_siv(v
);
320 assert(siv
->lm
== LM_NONE
);
321 parse_option_size(name
, siv
->string
, &val
, &err
);
323 error_propagate(errp
, err
);
330 static void parse_type_bool(Visitor
*v
, const char *name
, bool *obj
,
333 StringInputVisitor
*siv
= to_siv(v
);
335 assert(siv
->lm
== LM_NONE
);
336 if (!strcasecmp(siv
->string
, "on") ||
337 !strcasecmp(siv
->string
, "yes") ||
338 !strcasecmp(siv
->string
, "true")) {
342 if (!strcasecmp(siv
->string
, "off") ||
343 !strcasecmp(siv
->string
, "no") ||
344 !strcasecmp(siv
->string
, "false")) {
349 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
353 static void parse_type_str(Visitor
*v
, const char *name
, char **obj
,
356 StringInputVisitor
*siv
= to_siv(v
);
358 assert(siv
->lm
== LM_NONE
);
359 *obj
= g_strdup(siv
->string
);
362 static void parse_type_number(Visitor
*v
, const char *name
, double *obj
,
365 StringInputVisitor
*siv
= to_siv(v
);
368 assert(siv
->lm
== LM_NONE
);
369 if (qemu_strtod_finite(siv
->string
, NULL
, &val
)) {
370 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
378 static void parse_type_null(Visitor
*v
, const char *name
, QNull
**obj
,
381 StringInputVisitor
*siv
= to_siv(v
);
383 assert(siv
->lm
== LM_NONE
);
386 if (siv
->string
[0]) {
387 error_setg(errp
, QERR_INVALID_PARAMETER_TYPE
, name
? name
: "null",
395 static void string_input_free(Visitor
*v
)
397 StringInputVisitor
*siv
= to_siv(v
);
402 Visitor
*string_input_visitor_new(const char *str
)
404 StringInputVisitor
*v
;
407 v
= g_malloc0(sizeof(*v
));
409 v
->visitor
.type
= VISITOR_INPUT
;
410 v
->visitor
.type_int64
= parse_type_int64
;
411 v
->visitor
.type_uint64
= parse_type_uint64
;
412 v
->visitor
.type_size
= parse_type_size
;
413 v
->visitor
.type_bool
= parse_type_bool
;
414 v
->visitor
.type_str
= parse_type_str
;
415 v
->visitor
.type_number
= parse_type_number
;
416 v
->visitor
.type_null
= parse_type_null
;
417 v
->visitor
.start_list
= start_list
;
418 v
->visitor
.next_list
= next_list
;
419 v
->visitor
.check_list
= check_list
;
420 v
->visitor
.end_list
= end_list
;
421 v
->visitor
.free
= string_input_free
;