Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) /* Parse JSON files using the JSMN parser. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  * Copyright (c) 2014, Intel Corporation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * All rights reserved.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Redistribution and use in source and binary forms, with or without
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * modification, are permitted provided that the following conditions are met:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * 1. Redistributions of source code must retain the above copyright notice,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * this list of conditions and the following disclaimer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * 2. Redistributions in binary form must reproduce the above copyright
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * notice, this list of conditions and the following disclaimer in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  * documentation and/or other materials provided with the distribution.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17)  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18)  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19)  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20)  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * OF THE POSSIBILITY OF SUCH DAMAGE.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #include <sys/mman.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) #include "jsmn.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) #include "json.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) static char *mapfile(const char *fn, size_t *size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	unsigned ps = sysconf(_SC_PAGESIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	char *map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	int fd = open(fn, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	if (fd < 0 && verbose > 0 && fn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 		pr_err("Error opening events file '%s': %s\n", fn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 				strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	err = fstat(fd, &st);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	*size = st.st_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	map = mmap(NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		   (st.st_size + ps - 1) & ~(ps - 1),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		   PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	if (map == MAP_FAILED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		map = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	return map;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) static void unmapfile(char *map, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	unsigned ps = sysconf(_SC_PAGESIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 	munmap(map, roundup(size, ps));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80)  * Parse json file using jsmn. Return array of tokens,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81)  * and mapped file. Caller needs to free array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) jsmntok_t *parse_json(const char *fn, char **map, size_t *size, int *len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	jsmn_parser parser;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	jsmntok_t *tokens;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 	jsmnerr_t res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	unsigned sz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	*map = mapfile(fn, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	if (!*map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	/* Heuristic */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 	sz = *size * 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	tokens = malloc(sz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (!tokens)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	jsmn_init(&parser);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	res = jsmn_parse(&parser, *map, *size, tokens,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 			 sz / sizeof(jsmntok_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (res != JSMN_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		pr_err("%s: json error %s\n", fn, jsmn_strerror(res));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		goto error_free;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 	if (len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 		*len = parser.toknext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	return tokens;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) error_free:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	free(tokens);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	unmapfile(*map, *size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) void free_json(char *map, size_t size, jsmntok_t *tokens)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	free(tokens);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	unmapfile(map, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static int countchar(char *map, char c, int end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 	int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 	for (i = 0; i < end; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		if (map[i] == c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 			count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /* Return line number of a jsmn token */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) int json_line(char *map, jsmntok_t *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	return countchar(map, '\n', t->start) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) static const char * const jsmn_types[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 	[JSMN_PRIMITIVE] = "primitive",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 	[JSMN_ARRAY] = "array",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 	[JSMN_OBJECT] = "object",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 	[JSMN_STRING] = "string"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) #define LOOKUP(a, i) ((i) < (sizeof(a)/sizeof(*(a))) ? ((a)[i]) : "?")
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) /* Return type name of a jsmn token */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) const char *json_name(jsmntok_t *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	return LOOKUP(jsmn_types, t->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) int json_len(jsmntok_t *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	return t->end - t->start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) /* Is string t equal to s? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) int json_streq(char *map, jsmntok_t *t, const char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 	unsigned len = json_len(t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	return len == strlen(s) && !strncasecmp(map + t->start, s, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }