json.c 3.9 KB

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