blob: ed760be9d5db92f1b707c4d1b9a8f72d8eac4b3c [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/* Parse JSON files using the JSMN parser. */
2
3/*
4 * Copyright (c) 2014, Intel Corporation
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer.
12 *
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.
16 *
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.
29*/
30
31#include <stdlib.h>
32#include <string.h>
33#include <sys/mman.h>
34#include <sys/stat.h>
35#include <fcntl.h>
36#include <stdio.h>
37#include <errno.h>
38#include <unistd.h>
39#include "jsmn.h"
40#include "json.h"
41
42
43static char *mapfile(const char *fn, size_t *size)
44{
45 unsigned ps = sysconf(_SC_PAGESIZE);
46 struct stat st;
47 char *map = NULL;
48 int err;
49 int fd = open(fn, O_RDONLY);
50
51 if (fd < 0 && verbose > 0 && fn) {
52 pr_err("Error opening events file '%s': %s\n", fn,
53 strerror(errno));
54 }
55
56 if (fd < 0)
57 return NULL;
58 err = fstat(fd, &st);
59 if (err < 0)
60 goto out;
61 *size = st.st_size;
62 map = mmap(NULL,
63 (st.st_size + ps - 1) & ~(ps - 1),
64 PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
65 if (map == MAP_FAILED)
66 map = NULL;
67out:
68 close(fd);
69 return map;
70}
71
72static void unmapfile(char *map, size_t size)
73{
74 unsigned ps = sysconf(_SC_PAGESIZE);
75 munmap(map, roundup(size, ps));
76}
77
78/*
79 * Parse json file using jsmn. Return array of tokens,
80 * and mapped file. Caller needs to free array.
81 */
82jsmntok_t *parse_json(const char *fn, char **map, size_t *size, int *len)
83{
84 jsmn_parser parser;
85 jsmntok_t *tokens;
86 jsmnerr_t res;
87 unsigned sz;
88
89 *map = mapfile(fn, size);
90 if (!*map)
91 return NULL;
92 /* Heuristic */
93 sz = *size * 16;
94 tokens = malloc(sz);
95 if (!tokens)
96 goto error;
97 jsmn_init(&parser);
98 res = jsmn_parse(&parser, *map, *size, tokens,
99 sz / sizeof(jsmntok_t));
100 if (res != JSMN_SUCCESS) {
101 pr_err("%s: json error %s\n", fn, jsmn_strerror(res));
102 goto error_free;
103 }
104 if (len)
105 *len = parser.toknext;
106 return tokens;
107error_free:
108 free(tokens);
109error:
110 unmapfile(*map, *size);
111 return NULL;
112}
113
114void free_json(char *map, size_t size, jsmntok_t *tokens)
115{
116 free(tokens);
117 unmapfile(map, size);
118}
119
120static int countchar(char *map, char c, int end)
121{
122 int i;
123 int count = 0;
124 for (i = 0; i < end; i++)
125 if (map[i] == c)
126 count++;
127 return count;
128}
129
130/* Return line number of a jsmn token */
131int json_line(char *map, jsmntok_t *t)
132{
133 return countchar(map, '\n', t->start) + 1;
134}
135
136static const char * const jsmn_types[] = {
137 [JSMN_PRIMITIVE] = "primitive",
138 [JSMN_ARRAY] = "array",
139 [JSMN_OBJECT] = "object",
140 [JSMN_STRING] = "string"
141};
142
143#define LOOKUP(a, i) ((i) < (sizeof(a)/sizeof(*(a))) ? ((a)[i]) : "?")
144
145/* Return type name of a jsmn token */
146const char *json_name(jsmntok_t *t)
147{
148 return LOOKUP(jsmn_types, t->type);
149}
150
151int json_len(jsmntok_t *t)
152{
153 return t->end - t->start;
154}
155
156/* Is string t equal to s? */
157int json_streq(char *map, jsmntok_t *t, const char *s)
158{
159 unsigned len = json_len(t);
160 return len == strlen(s) && !strncasecmp(map + t->start, s, len);
161}