• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 gRPC authors.
4  *
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
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  */
18 
19 #include <grpc/support/port_platform.h>
20 
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include <grpc/support/alloc.h>
25 #include <grpc/support/log.h>
26 
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"
30 
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.
34  *
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.
38  *
39  * Note that this works because the act of parsing json always reduces its
40  * input size, and never expands it.
41  */
42 typedef struct {
43   grpc_json* top;
44   grpc_json* current_container;
45   grpc_json* current_value;
46   uint8_t* input;
47   uint8_t* key;
48   uint8_t* string;
49   uint8_t* string_ptr;
50   size_t remaining_input;
51 } json_reader_userdata;
52 
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.
55  */
56 typedef struct {
57   char* output;
58   size_t free_space;
59   size_t string_len;
60   size_t allocated;
61 } json_writer_userdata;
62 
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).
66  */
json_writer_output_check(void * userdata,size_t needed)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;
73   state->output =
74       static_cast<char*>(gpr_realloc(state->output, state->allocated + needed));
75   state->free_space += needed;
76   state->allocated += needed;
77 }
78 
79 /* These are needed by the writer's implementation. */
json_writer_output_char(void * userdata,char c)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;
84   state->free_space--;
85 }
86 
json_writer_output_string_with_len(void * userdata,const char * str,size_t len)87 static void json_writer_output_string_with_len(void* userdata, const char* str,
88                                                size_t len) {
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;
94 }
95 
json_writer_output_string(void * userdata,const char * str)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);
99 }
100 
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.
103  */
json_reader_string_clear(void * userdata)104 static void json_reader_string_clear(void* userdata) {
105   json_reader_userdata* state = static_cast<json_reader_userdata*>(userdata);
106   if (state->string) {
107     GPR_ASSERT(state->string_ptr < state->input);
108     *state->string_ptr++ = 0;
109   }
110   state->string = state->string_ptr;
111 }
112 
json_reader_string_add_char(void * userdata,uint32_t c)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);
118 }
119 
120 /* We are converting a UTF-32 character into UTF-8 here,
121  * as described by RFC3629.
122  */
json_reader_string_add_utf32(void * userdata,uint32_t c)123 static void json_reader_string_add_utf32(void* userdata, uint32_t c) {
124   if (c <= 0x7f) {
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);
147   }
148 }
149 
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.
152  */
json_reader_read_char(void * userdata)153 static uint32_t json_reader_read_char(void* userdata) {
154   uint32_t r;
155   json_reader_userdata* state = static_cast<json_reader_userdata*>(userdata);
156 
157   if (state->remaining_input == 0) return GRPC_JSON_READ_CHAR_EOF;
158 
159   r = *state->input++;
160   state->remaining_input--;
161 
162   if (r == 0) {
163     state->remaining_input = 0;
164     return GRPC_JSON_READ_CHAR_EOF;
165   }
166 
167   return r;
168 }
169 
170 /* Helper function to create a new grpc_json object and link it into
171  * our tree-in-progress inside our opaque structure.
172  */
json_create_and_link(void * userdata,grpc_json_type type)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);
176 
177   json->parent = state->current_container;
178   json->prev = state->current_value;
179   state->current_value = json;
180 
181   if (json->prev) {
182     json->prev->next = json;
183   }
184   if (json->parent) {
185     if (!json->parent->child) {
186       json->parent->child = json;
187     }
188     if (json->parent->type == GRPC_JSON_OBJECT) {
189       json->key = reinterpret_cast<char*>(state->key);
190     }
191   }
192   if (!state->top) {
193     state->top = json;
194   }
195 
196   return json;
197 }
198 
json_reader_container_begins(void * userdata,grpc_json_type type)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;
202 
203   GPR_ASSERT(type == GRPC_JSON_ARRAY || type == GRPC_JSON_OBJECT);
204 
205   container = json_create_and_link(userdata, type);
206   state->current_container = container;
207   state->current_value = nullptr;
208 }
209 
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.
215  *
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.
218  */
json_reader_container_ends(void * userdata)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);
222 
223   GPR_ASSERT(state->current_container);
224 
225   state->current_value = state->current_container;
226   state->current_container = state->current_container->parent;
227 
228   if (state->current_container) {
229     container_type = state->current_container->type;
230   }
231 
232   return container_type;
233 }
234 
235 /* The next 3 functions basically are the reader asking us to use our string
236  * scratchpad for one of these 3 purposes.
237  *
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.
240  */
json_reader_set_key(void * userdata)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;
244 }
245 
json_reader_set_string(void * userdata)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);
250 }
251 
json_reader_set_number(void * userdata)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);
256   return 1;
257 }
258 
259 /* The object types true, false and null are self-sufficient, and don't need
260  * any more information beside their type.
261  */
json_reader_set_true(void * userdata)262 static void json_reader_set_true(void* userdata) {
263   json_create_and_link(userdata, GRPC_JSON_TRUE);
264 }
265 
json_reader_set_false(void * userdata)266 static void json_reader_set_false(void* userdata) {
267   json_create_and_link(userdata, GRPC_JSON_FALSE);
268 }
269 
json_reader_set_null(void * userdata)270 static void json_reader_set_null(void* userdata) {
271   json_create_and_link(userdata, GRPC_JSON_NULL);
272 }
273 
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};
281 
282 /* And finally, let's define our public API. */
grpc_json_parse_string_with_len(char * input,size_t size)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;
288 
289   if (!input) return nullptr;
290 
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);
296 
297   status = grpc_json_reader_run(&reader);
298   json = state.top;
299 
300   if ((status != GRPC_JSON_DONE) && json) {
301     grpc_json_destroy(json);
302     json = nullptr;
303   }
304 
305   return json;
306 }
307 
308 #define UNBOUND_JSON_STRING_LENGTH 0x7fffffff
309 
grpc_json_parse_string(char * input)310 grpc_json* grpc_json_parse_string(char* input) {
311   return grpc_json_parse_string_with_len(input, UNBOUND_JSON_STRING_LENGTH);
312 }
313 
json_dump_recursive(grpc_json_writer * writer,grpc_json * json,int in_object)314 static void json_dump_recursive(grpc_json_writer* writer, grpc_json* json,
315                                 int in_object) {
316   while (json) {
317     if (in_object) grpc_json_writer_object_key(writer, json->key);
318 
319     switch (json->type) {
320       case GRPC_JSON_OBJECT:
321       case GRPC_JSON_ARRAY:
322         grpc_json_writer_container_begins(writer, json->type);
323         if (json->child)
324           json_dump_recursive(writer, json->child,
325                               json->type == GRPC_JSON_OBJECT);
326         grpc_json_writer_container_ends(writer, json->type);
327         break;
328       case GRPC_JSON_STRING:
329         grpc_json_writer_value_string(writer, json->value);
330         break;
331       case GRPC_JSON_NUMBER:
332         grpc_json_writer_value_raw(writer, json->value);
333         break;
334       case GRPC_JSON_TRUE:
335         grpc_json_writer_value_raw_with_len(writer, "true", 4);
336         break;
337       case GRPC_JSON_FALSE:
338         grpc_json_writer_value_raw_with_len(writer, "false", 5);
339         break;
340       case GRPC_JSON_NULL:
341         grpc_json_writer_value_raw_with_len(writer, "null", 4);
342         break;
343       default:
344         GPR_UNREACHABLE_CODE(abort());
345     }
346     json = json->next;
347   }
348 }
349 
350 static grpc_json_writer_vtable writer_vtable = {
351     json_writer_output_char, json_writer_output_string,
352     json_writer_output_string_with_len};
353 
grpc_json_dump_to_string(grpc_json * json,int indent)354 char* grpc_json_dump_to_string(grpc_json* json, int indent) {
355   grpc_json_writer writer;
356   json_writer_userdata state;
357 
358   state.output = nullptr;
359   state.free_space = state.string_len = state.allocated = 0;
360   grpc_json_writer_init(&writer, indent, &writer_vtable, &state);
361 
362   json_dump_recursive(&writer, json, 0);
363 
364   json_writer_output_char(&state, 0);
365 
366   return state.output;
367 }
368