1 /* Parse JSON files using the JSMN parser. */
4 * Copyright (c) 2014, Intel Corporation
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
41 #include <linux/kernel.h>
44 static char *mapfile(const char *fn, size_t *size)
46 unsigned ps = sysconf(_SC_PAGESIZE);
50 int fd = open(fn, O_RDONLY);
52 if (fd < 0 && verbose > 0 && fn) {
53 pr_err("Error opening events file '%s': %s\n", fn,
64 (st.st_size + ps - 1) & ~(ps - 1),
65 PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
66 if (map == MAP_FAILED)
73 static void unmapfile(char *map, size_t size)
75 unsigned ps = sysconf(_SC_PAGESIZE);
76 munmap(map, roundup(size, ps));
80 * Parse json file using jsmn. Return array of tokens,
81 * and mapped file. Caller needs to free array.
83 jsmntok_t *parse_json(const char *fn, char **map, size_t *size, int *len)
90 *map = mapfile(fn, size);
99 res = jsmn_parse(&parser, *map, *size, tokens,
100 sz / sizeof(jsmntok_t));
101 if (res != JSMN_SUCCESS) {
102 pr_err("%s: json error %s\n", fn, jsmn_strerror(res));
106 *len = parser.toknext;
111 unmapfile(*map, *size);
115 void free_json(char *map, size_t size, jsmntok_t *tokens)
118 unmapfile(map, size);
121 static int countchar(char *map, char c, int end)
125 for (i = 0; i < end; i++)
131 /* Return line number of a jsmn token */
132 int json_line(char *map, jsmntok_t *t)
134 return countchar(map, '\n', t->start) + 1;
137 static const char * const jsmn_types[] = {
138 [JSMN_PRIMITIVE] = "primitive",
139 [JSMN_ARRAY] = "array",
140 [JSMN_OBJECT] = "object",
141 [JSMN_STRING] = "string"
144 #define LOOKUP(a, i) ((i) < (sizeof(a)/sizeof(*(a))) ? ((a)[i]) : "?")
146 /* Return type name of a jsmn token */
147 const char *json_name(jsmntok_t *t)
149 return LOOKUP(jsmn_types, t->type);
152 int json_len(jsmntok_t *t)
154 return t->end - t->start;
157 /* Is string t equal to s? */
158 int json_streq(char *map, jsmntok_t *t, const char *s)
160 unsigned len = json_len(t);
161 return len == strlen(s) && !strncasecmp(map + t->start, s, len);