3 * Copyright 2015 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #include <grpc/support/port_platform.h>
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
27 #include "src/core/lib/json/json.h"
28 #include "src/core/lib/json/json_reader.h"
29 #include "src/core/lib/json/json_writer.h"
31 /* The json reader will construct a bunch of grpc_json objects and
32 * link them all up together in a tree-like structure that will represent
33 * the json data in memory.
35 * It also uses its own input as a scratchpad to store all of the decoded,
36 * unescaped strings. So we need to keep track of all these pointers in
37 * that opaque structure the reader will carry for us.
39 * Note that this works because the act of parsing json always reduces its
40 * input size, and never expands it.
44 grpc_json* current_container;
45 grpc_json* current_value;
50 size_t remaining_input;
51 } json_reader_userdata;
53 /* This json writer will put everything in a big string.
54 * The point is that we allocate that string in chunks of 256 bytes.
61 } json_writer_userdata;
63 /* This function checks if there's enough space left in the output buffer,
64 * and will enlarge it if necessary. We're only allocating chunks of 256
65 * bytes at a time (or multiples thereof).
67 static void json_writer_output_check(void* userdata, size_t needed) {
68 json_writer_userdata* state = static_cast<json_writer_userdata*>(userdata);
69 if (state->free_space >= needed) return;
70 needed -= state->free_space;
71 /* Round up by 256 bytes. */
72 needed = (needed + 0xff) & ~0xffU;
74 static_cast<char*>(gpr_realloc(state->output, state->allocated + needed));
75 state->free_space += needed;
76 state->allocated += needed;
79 /* These are needed by the writer's implementation. */
80 static void json_writer_output_char(void* userdata, char c) {
81 json_writer_userdata* state = static_cast<json_writer_userdata*>(userdata);
82 json_writer_output_check(userdata, 1);
83 state->output[state->string_len++] = c;
87 static void json_writer_output_string_with_len(void* userdata, const char* str,
89 json_writer_userdata* state = static_cast<json_writer_userdata*>(userdata);
90 json_writer_output_check(userdata, len);
91 memcpy(state->output + state->string_len, str, len);
92 state->string_len += len;
93 state->free_space -= len;
96 static void json_writer_output_string(void* userdata, const char* str) {
97 size_t len = strlen(str);
98 json_writer_output_string_with_len(userdata, str, len);
101 /* The reader asks us to clear our scratchpad. In our case, we'll simply mark
102 * the end of the current string, and advance our output pointer.
104 static void json_reader_string_clear(void* userdata) {
105 json_reader_userdata* state = static_cast<json_reader_userdata*>(userdata);
107 GPR_ASSERT(state->string_ptr < state->input);
108 *state->string_ptr++ = 0;
110 state->string = state->string_ptr;
113 static void json_reader_string_add_char(void* userdata, uint32_t c) {
114 json_reader_userdata* state = static_cast<json_reader_userdata*>(userdata);
115 GPR_ASSERT(state->string_ptr < state->input);
116 GPR_ASSERT(c <= 0xff);
117 *state->string_ptr++ = static_cast<uint8_t>(c);
120 /* We are converting a UTF-32 character into UTF-8 here,
121 * as described by RFC3629.
123 static void json_reader_string_add_utf32(void* userdata, uint32_t c) {
125 json_reader_string_add_char(userdata, c);
126 } else if (c <= 0x7ff) {
127 uint32_t b1 = 0xc0 | ((c >> 6) & 0x1f);
128 uint32_t b2 = 0x80 | (c & 0x3f);
129 json_reader_string_add_char(userdata, b1);
130 json_reader_string_add_char(userdata, b2);
131 } else if (c <= 0xffff) {
132 uint32_t b1 = 0xe0 | ((c >> 12) & 0x0f);
133 uint32_t b2 = 0x80 | ((c >> 6) & 0x3f);
134 uint32_t b3 = 0x80 | (c & 0x3f);
135 json_reader_string_add_char(userdata, b1);
136 json_reader_string_add_char(userdata, b2);
137 json_reader_string_add_char(userdata, b3);
138 } else if (c <= 0x1fffff) {
139 uint32_t b1 = 0xf0 | ((c >> 18) & 0x07);
140 uint32_t b2 = 0x80 | ((c >> 12) & 0x3f);
141 uint32_t b3 = 0x80 | ((c >> 6) & 0x3f);
142 uint32_t b4 = 0x80 | (c & 0x3f);
143 json_reader_string_add_char(userdata, b1);
144 json_reader_string_add_char(userdata, b2);
145 json_reader_string_add_char(userdata, b3);
146 json_reader_string_add_char(userdata, b4);
150 /* We consider that the input may be a zero-terminated string. So we
151 * can end up hitting eof before the end of the alleged string length.
153 static uint32_t json_reader_read_char(void* userdata) {
155 json_reader_userdata* state = static_cast<json_reader_userdata*>(userdata);
157 if (state->remaining_input == 0) return GRPC_JSON_READ_CHAR_EOF;
160 state->remaining_input--;
163 state->remaining_input = 0;
164 return GRPC_JSON_READ_CHAR_EOF;
170 /* Helper function to create a new grpc_json object and link it into
171 * our tree-in-progress inside our opaque structure.
173 static grpc_json* json_create_and_link(void* userdata, grpc_json_type type) {
174 json_reader_userdata* state = static_cast<json_reader_userdata*>(userdata);
175 grpc_json* json = grpc_json_create(type);
177 json->parent = state->current_container;
178 json->prev = state->current_value;
179 state->current_value = json;
182 json->prev->next = json;
185 if (!json->parent->child) {
186 json->parent->child = json;
188 if (json->parent->type == GRPC_JSON_OBJECT) {
189 json->key = reinterpret_cast<char*>(state->key);
199 static void json_reader_container_begins(void* userdata, grpc_json_type type) {
200 json_reader_userdata* state = static_cast<json_reader_userdata*>(userdata);
201 grpc_json* container;
203 GPR_ASSERT(type == GRPC_JSON_ARRAY || type == GRPC_JSON_OBJECT);
205 container = json_create_and_link(userdata, type);
206 state->current_container = container;
207 state->current_value = nullptr;
210 /* It's important to remember that the reader is mostly stateless, so it
211 * isn't trying to remember what the container was prior the one that just
212 * ends. Since we're keeping track of these for our own purpose, we are
213 * able to return that information back, which is useful for it to validate
214 * the input json stream.
216 * Also note that if we're at the top of the tree, and the last container
217 * ends, we have to return GRPC_JSON_TOP_LEVEL.
219 static grpc_json_type json_reader_container_ends(void* userdata) {
220 grpc_json_type container_type = GRPC_JSON_TOP_LEVEL;
221 json_reader_userdata* state = static_cast<json_reader_userdata*>(userdata);
223 GPR_ASSERT(state->current_container);
225 state->current_value = state->current_container;
226 state->current_container = state->current_container->parent;
228 if (state->current_container) {
229 container_type = state->current_container->type;
232 return container_type;
235 /* The next 3 functions basically are the reader asking us to use our string
236 * scratchpad for one of these 3 purposes.
238 * Note that in the set_number case, we're not going to try interpreting it.
239 * We'll keep it as a string, and leave it to the caller to evaluate it.
241 static void json_reader_set_key(void* userdata) {
242 json_reader_userdata* state = static_cast<json_reader_userdata*>(userdata);
243 state->key = state->string;
246 static void json_reader_set_string(void* userdata) {
247 json_reader_userdata* state = static_cast<json_reader_userdata*>(userdata);
248 grpc_json* json = json_create_and_link(userdata, GRPC_JSON_STRING);
249 json->value = reinterpret_cast<char*>(state->string);
252 static int json_reader_set_number(void* userdata) {
253 json_reader_userdata* state = static_cast<json_reader_userdata*>(userdata);
254 grpc_json* json = json_create_and_link(userdata, GRPC_JSON_NUMBER);
255 json->value = reinterpret_cast<char*>(state->string);
259 /* The object types true, false and null are self-sufficient, and don't need
260 * any more information beside their type.
262 static void json_reader_set_true(void* userdata) {
263 json_create_and_link(userdata, GRPC_JSON_TRUE);
266 static void json_reader_set_false(void* userdata) {
267 json_create_and_link(userdata, GRPC_JSON_FALSE);
270 static void json_reader_set_null(void* userdata) {
271 json_create_and_link(userdata, GRPC_JSON_NULL);
274 static grpc_json_reader_vtable reader_vtable = {
275 json_reader_string_clear, json_reader_string_add_char,
276 json_reader_string_add_utf32, json_reader_read_char,
277 json_reader_container_begins, json_reader_container_ends,
278 json_reader_set_key, json_reader_set_string,
279 json_reader_set_number, json_reader_set_true,
280 json_reader_set_false, json_reader_set_null};
282 /* And finally, let's define our public API. */
283 grpc_json* grpc_json_parse_string_with_len(char* input, size_t size) {
284 grpc_json_reader reader;
285 json_reader_userdata state;
286 grpc_json* json = nullptr;
287 grpc_json_reader_status status;
289 if (!input) return nullptr;
291 state.top = state.current_container = state.current_value = nullptr;
292 state.string = state.key = nullptr;
293 state.string_ptr = state.input = reinterpret_cast<uint8_t*>(input);
294 state.remaining_input = size;
295 grpc_json_reader_init(&reader, &reader_vtable, &state);
297 status = grpc_json_reader_run(&reader);
300 if ((status != GRPC_JSON_DONE) && json) {
301 grpc_json_destroy(json);
308 #define UNBOUND_JSON_STRING_LENGTH 0x7fffffff
310 grpc_json* grpc_json_parse_string(char* input) {
311 return grpc_json_parse_string_with_len(input, UNBOUND_JSON_STRING_LENGTH);
314 static void json_dump_recursive(grpc_json_writer* writer, grpc_json* json,
317 if (in_object) grpc_json_writer_object_key(writer, json->key);
319 switch (json->type) {
320 case GRPC_JSON_OBJECT:
321 case GRPC_JSON_ARRAY:
322 grpc_json_writer_container_begins(writer, json->type);
324 json_dump_recursive(writer, json->child,
325 json->type == GRPC_JSON_OBJECT);
326 grpc_json_writer_container_ends(writer, json->type);
328 case GRPC_JSON_STRING:
329 grpc_json_writer_value_string(writer, json->value);
331 case GRPC_JSON_NUMBER:
332 grpc_json_writer_value_raw(writer, json->value);
335 grpc_json_writer_value_raw_with_len(writer, "true", 4);
337 case GRPC_JSON_FALSE:
338 grpc_json_writer_value_raw_with_len(writer, "false", 5);
341 grpc_json_writer_value_raw_with_len(writer, "null", 4);
344 GPR_UNREACHABLE_CODE(abort());
350 static grpc_json_writer_vtable writer_vtable = {
351 json_writer_output_char, json_writer_output_string,
352 json_writer_output_string_with_len};
354 char* grpc_json_dump_to_string(grpc_json* json, int indent) {
355 grpc_json_writer writer;
356 json_writer_userdata state;
358 state.output = nullptr;
359 state.free_space = state.string_len = state.allocated = 0;
360 grpc_json_writer_init(&writer, indent, &writer_vtable, &state);
362 json_dump_recursive(&writer, json, 0);
364 json_writer_output_char(&state, 0);