aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar murgatroid99 <michael.lumish@gmail.com>2015-01-29 09:38:42 -0800
committerGravatar murgatroid99 <michael.lumish@gmail.com>2015-01-29 09:38:42 -0800
commit5582b2d1d7a9c0537b3196f70fe7bd3c85310e64 (patch)
tree17f1b775da58aac6836f65889d4a9e03db735cb5 /src
parent684f6f4359a4c09b3430ae8da80f9db5ac00a603 (diff)
parent9b3809f99f50c579aa2a21246514ae15b332187a (diff)
Merge branch 'master' of github.com:google/grpc into node_server_metadata
Diffstat (limited to 'src')
-rw-r--r--src/core/iomgr/fd_posix.c84
-rw-r--r--src/core/iomgr/fd_posix.h4
-rw-r--r--src/core/iomgr/iomgr.c2
-rw-r--r--src/core/iomgr/iomgr_posix.c11
-rw-r--r--src/core/json/json.c64
-rw-r--r--src/core/json/json.h88
-rw-r--r--src/core/json/json_common.h49
-rw-r--r--src/core/json/json_reader.c653
-rw-r--r--src/core/json/json_reader.h160
-rw-r--r--src/core/json/json_string.c391
-rw-r--r--src/core/json/json_writer.c252
-rw-r--r--src/core/json/json_writer.h93
-rw-r--r--src/core/security/credentials.c41
-rw-r--r--src/core/security/json_token.c93
14 files changed, 1914 insertions, 71 deletions
diff --git a/src/core/iomgr/fd_posix.c b/src/core/iomgr/fd_posix.c
index 9f70a26c64..b67c6cde70 100644
--- a/src/core/iomgr/fd_posix.c
+++ b/src/core/iomgr/fd_posix.c
@@ -47,12 +47,63 @@
enum descriptor_state { NOT_READY, READY, WAITING };
+/* We need to keep a freelist not because of any concerns of malloc performance
+ * but instead so that implementations with multiple threads in (for example)
+ * epoll_wait deal with the race between pollset removal and incoming poll
+ * notifications.
+ *
+ * The problem is that the poller ultimately holds a reference to this
+ * object, so it is very difficult to know when is safe to free it, at least
+ * without some expensive synchronization.
+ *
+ * If we keep the object freelisted, in the worst case losing this race just
+ * becomes a spurious read notification on a reused fd.
+ */
+/* TODO(klempner): We could use some form of polling generation count to know
+ * when these are safe to free. */
+/* TODO(klempner): Consider disabling freelisting if we don't have multiple
+ * threads in poll on the same fd */
+/* TODO(klempner): Batch these allocations to reduce fragmentation */
+static grpc_fd *fd_freelist = NULL;
+static gpr_mu fd_freelist_mu;
+
+static void freelist_fd(grpc_fd *fd) {
+ gpr_free(fd->watchers);
+ gpr_mu_lock(&fd_freelist_mu);
+ fd->freelist_next = fd_freelist;
+ fd_freelist = fd;
+ gpr_mu_unlock(&fd_freelist_mu);
+}
+
+static grpc_fd *alloc_fd(int fd) {
+ grpc_fd *r = NULL;
+ gpr_mu_lock(&fd_freelist_mu);
+ if (fd_freelist != NULL) {
+ r = fd_freelist;
+ fd_freelist = fd_freelist->freelist_next;
+ }
+ gpr_mu_unlock(&fd_freelist_mu);
+ if (r == NULL) {
+ r = gpr_malloc(sizeof(grpc_fd));
+ gpr_mu_init(&r->set_state_mu);
+ gpr_mu_init(&r->watcher_mu);
+ }
+ gpr_atm_rel_store(&r->refst, 1);
+ gpr_atm_rel_store(&r->readst.state, NOT_READY);
+ gpr_atm_rel_store(&r->writest.state, NOT_READY);
+ gpr_atm_rel_store(&r->shutdown, 0);
+ r->fd = fd;
+ r->watchers = NULL;
+ r->watcher_count = 0;
+ r->watcher_capacity = 0;
+ r->freelist_next = NULL;
+ return r;
+}
+
static void destroy(grpc_fd *fd) {
- grpc_iomgr_add_callback(fd->on_done, fd->on_done_user_data);
gpr_mu_destroy(&fd->set_state_mu);
- gpr_free(fd->watchers);
+ gpr_mu_destroy(&fd->watcher_mu);
gpr_free(fd);
- grpc_iomgr_unref();
}
static void ref_by(grpc_fd *fd, int n) {
@@ -61,25 +112,30 @@ static void ref_by(grpc_fd *fd, int n) {
static void unref_by(grpc_fd *fd, int n) {
if (gpr_atm_full_fetch_add(&fd->refst, -n) == n) {
+ grpc_iomgr_add_callback(fd->on_done, fd->on_done_user_data);
+ freelist_fd(fd);
+ grpc_iomgr_unref();
+ }
+}
+
+void grpc_fd_global_init(void) {
+ gpr_mu_init(&fd_freelist_mu);
+}
+
+void grpc_fd_global_shutdown(void) {
+ while (fd_freelist != NULL) {
+ grpc_fd *fd = fd_freelist;
+ fd_freelist = fd_freelist->freelist_next;
destroy(fd);
}
+ gpr_mu_destroy(&fd_freelist_mu);
}
static void do_nothing(void *ignored, int success) {}
grpc_fd *grpc_fd_create(int fd) {
- grpc_fd *r = gpr_malloc(sizeof(grpc_fd));
+ grpc_fd *r = alloc_fd(fd);
grpc_iomgr_ref();
- gpr_atm_rel_store(&r->refst, 1);
- gpr_atm_rel_store(&r->readst.state, NOT_READY);
- gpr_atm_rel_store(&r->writest.state, NOT_READY);
- gpr_mu_init(&r->set_state_mu);
- gpr_mu_init(&r->watcher_mu);
- gpr_atm_rel_store(&r->shutdown, 0);
- r->fd = fd;
- r->watchers = NULL;
- r->watcher_count = 0;
- r->watcher_capacity = 0;
grpc_pollset_add_fd(grpc_backup_pollset(), r);
return r;
}
diff --git a/src/core/iomgr/fd_posix.h b/src/core/iomgr/fd_posix.h
index 232de0c3e0..f42ae19579 100644
--- a/src/core/iomgr/fd_posix.h
+++ b/src/core/iomgr/fd_posix.h
@@ -69,6 +69,7 @@ typedef struct grpc_fd {
grpc_iomgr_cb_func on_done;
void *on_done_user_data;
+ struct grpc_fd *freelist_next;
} grpc_fd;
/* Create a wrapped file descriptor.
@@ -135,4 +136,7 @@ void grpc_fd_become_writable(grpc_fd *fd, int allow_synchronous_callback);
void grpc_fd_ref(grpc_fd *fd);
void grpc_fd_unref(grpc_fd *fd);
+void grpc_fd_global_init(void);
+void grpc_fd_global_shutdown(void);
+
#endif /* __GRPC_INTERNAL_IOMGR_FD_POSIX_H_ */
diff --git a/src/core/iomgr/iomgr.c b/src/core/iomgr/iomgr.c
index 7f266ab235..8989b491d5 100644
--- a/src/core/iomgr/iomgr.c
+++ b/src/core/iomgr/iomgr.c
@@ -98,7 +98,6 @@ void grpc_iomgr_shutdown(void) {
gpr_timespec shutdown_deadline =
gpr_time_add(gpr_now(), gpr_time_from_seconds(10));
- grpc_iomgr_platform_shutdown();
gpr_mu_lock(&g_mu);
g_shutdown = 1;
@@ -129,6 +128,7 @@ void grpc_iomgr_shutdown(void) {
gpr_event_wait(&g_background_callback_executor_done, gpr_inf_future);
+ grpc_iomgr_platform_shutdown();
grpc_alarm_list_shutdown();
gpr_mu_destroy(&g_mu);
gpr_cv_destroy(&g_cv);
diff --git a/src/core/iomgr/iomgr_posix.c b/src/core/iomgr/iomgr_posix.c
index 61fec6bc53..9297f08e99 100644
--- a/src/core/iomgr/iomgr_posix.c
+++ b/src/core/iomgr/iomgr_posix.c
@@ -32,7 +32,14 @@
*/
#include "src/core/iomgr/iomgr_posix.h"
+#include "src/core/iomgr/fd_posix.h"
-void grpc_iomgr_platform_init(void) { grpc_pollset_global_init(); }
+void grpc_iomgr_platform_init(void) {
+ grpc_fd_global_init();
+ grpc_pollset_global_init();
+}
-void grpc_iomgr_platform_shutdown(void) { grpc_pollset_global_shutdown(); }
+void grpc_iomgr_platform_shutdown(void) {
+ grpc_pollset_global_shutdown();
+ grpc_fd_global_shutdown();
+}
diff --git a/src/core/json/json.c b/src/core/json/json.c
new file mode 100644
index 0000000000..1cff4fa195
--- /dev/null
+++ b/src/core/json/json.c
@@ -0,0 +1,64 @@
+/*
+ *
+ * Copyright 2014, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <string.h>
+
+#include <grpc/support/alloc.h>
+
+#include "src/core/json/json.h"
+
+grpc_json *grpc_json_create(grpc_json_type type) {
+ grpc_json *json = gpr_malloc(sizeof(grpc_json));
+ memset(json, 0, sizeof(grpc_json));
+ json->type = type;
+
+ return json;
+}
+
+void grpc_json_destroy(grpc_json *json) {
+ while (json->child) {
+ grpc_json_destroy(json->child);
+ }
+
+ if (json->next) {
+ json->next->prev = json->prev;
+ }
+
+ if (json->prev) {
+ json->prev->next = json->next;
+ } else if (json->parent) {
+ json->parent->child = json->next;
+ }
+
+ gpr_free(json);
+}
diff --git a/src/core/json/json.h b/src/core/json/json.h
new file mode 100644
index 0000000000..6676744ff7
--- /dev/null
+++ b/src/core/json/json.h
@@ -0,0 +1,88 @@
+/*
+ *
+ * Copyright 2014, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef __GRPC_SRC_CORE_JSON_JSON_H__
+#define __GRPC_SRC_CORE_JSON_JSON_H__
+
+#include <stdlib.h>
+
+#include "src/core/json/json_common.h"
+
+/* A tree-like structure to hold json values. The key and value pointers
+ * are not owned by it.
+ */
+typedef struct grpc_json {
+ struct grpc_json* next;
+ struct grpc_json* prev;
+ struct grpc_json* child;
+ struct grpc_json* parent;
+
+ grpc_json_type type;
+ const char* key;
+ const char* value;
+} grpc_json;
+
+/* The next two functions are going to parse the input string, and
+ * destroy it in the process, in order to use its space to store
+ * all of the keys and values for the returned object tree.
+ *
+ * They assume UTF-8 input stream, and will output UTF-8 encoded
+ * strings in the tree. The input stream's UTF-8 isn't validated,
+ * as in, what you input is what you get as an output.
+ *
+ * All the keys and values in the grpc_json_t objects will be strings
+ * pointing at your input buffer.
+ *
+ * Delete the allocated tree afterward using grpc_json_destroy().
+ */
+grpc_json* grpc_json_parse_string_with_len(char* input, size_t size);
+grpc_json* grpc_json_parse_string(char* input);
+
+/* This function will create a new string using gpr_realloc, and will
+ * deserialize the grpc_json tree into it. It'll be zero-terminated,
+ * but will be allocated in chunks of 256 bytes.
+ *
+ * The indent parameter controls the way the output is formatted.
+ * If indent is 0, then newlines will be suppressed as well, and the
+ * output will be condensed at its maximum.
+ */
+char* grpc_json_dump_to_string(grpc_json* json, int indent);
+
+/* Use these to create or delete a grpc_json object.
+ * Deletion is recursive. We will not attempt to free any of the strings
+ * in any of the objects of that tree.
+ */
+grpc_json* grpc_json_create(grpc_json_type type);
+void grpc_json_destroy(grpc_json* json);
+
+#endif /* __GRPC_SRC_CORE_JSON_JSON_H__ */
diff --git a/src/core/json/json_common.h b/src/core/json/json_common.h
new file mode 100644
index 0000000000..88a8155a42
--- /dev/null
+++ b/src/core/json/json_common.h
@@ -0,0 +1,49 @@
+/*
+ *
+ * Copyright 2014, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef __GRPC_SRC_CORE_JSON_JSON_COMMON_H__
+#define __GRPC_SRC_CORE_JSON_JSON_COMMON_H__
+
+/* The various json types. */
+typedef enum {
+ GRPC_JSON_OBJECT,
+ GRPC_JSON_ARRAY,
+ GRPC_JSON_STRING,
+ GRPC_JSON_NUMBER,
+ GRPC_JSON_TRUE,
+ GRPC_JSON_FALSE,
+ GRPC_JSON_NULL,
+ GRPC_JSON_TOP_LEVEL
+} grpc_json_type;
+
+#endif /* __GRPC_SRC_CORE_JSON_JSON_COMMON_H__ */
diff --git a/src/core/json/json_reader.c b/src/core/json/json_reader.c
new file mode 100644
index 0000000000..75aa87eb03
--- /dev/null
+++ b/src/core/json/json_reader.c
@@ -0,0 +1,653 @@
+/*
+ *
+ * Copyright 2014, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <string.h>
+
+#include <grpc/support/port_platform.h>
+
+#include "src/core/json/json_reader.h"
+
+static void json_reader_string_clear(grpc_json_reader* reader) {
+ reader->vtable->string_clear(reader->userdata);
+}
+
+static void json_reader_string_add_char(grpc_json_reader* reader,
+ gpr_uint32 c) {
+ reader->vtable->string_add_char(reader->userdata, c);
+}
+
+static void json_reader_string_add_utf32(grpc_json_reader* reader,
+ gpr_uint32 utf32) {
+ reader->vtable->string_add_utf32(reader->userdata, utf32);
+}
+
+static gpr_uint32
+ grpc_json_reader_read_char(grpc_json_reader* reader) {
+ return reader->vtable->read_char(reader->userdata);
+}
+
+static void json_reader_container_begins(grpc_json_reader* reader,
+ grpc_json_type type) {
+ reader->vtable->container_begins(reader->userdata, type);
+}
+
+static grpc_json_type
+ grpc_json_reader_container_ends(grpc_json_reader* reader) {
+ return reader->vtable->container_ends(reader->userdata);
+}
+
+static void json_reader_set_key(grpc_json_reader* reader) {
+ reader->vtable->set_key(reader->userdata);
+}
+
+static void json_reader_set_string(grpc_json_reader* reader) {
+ reader->vtable->set_string(reader->userdata);
+}
+
+static int json_reader_set_number(grpc_json_reader* reader) {
+ return reader->vtable->set_number(reader->userdata);
+}
+
+static void json_reader_set_true(grpc_json_reader* reader) {
+ reader->vtable->set_true(reader->userdata);
+}
+
+static void json_reader_set_false(grpc_json_reader* reader) {
+ reader->vtable->set_false(reader->userdata);
+}
+
+static void json_reader_set_null(grpc_json_reader* reader) {
+ reader->vtable->set_null(reader->userdata);
+}
+
+/* Call this function to initialize the reader structure. */
+void grpc_json_reader_init(grpc_json_reader* reader,
+ grpc_json_reader_vtable* vtable, void* userdata) {
+ memset(reader, 0, sizeof(grpc_json_reader));
+ reader->vtable = vtable;
+ reader->userdata = userdata;
+ json_reader_string_clear(reader);
+ reader->state = GRPC_JSON_STATE_VALUE_BEGIN;
+}
+
+int grpc_json_reader_is_complete(grpc_json_reader* reader) {
+ return ((reader->depth == 0) && ((reader->state == GRPC_JSON_STATE_END) ||
+ (reader->state == GRPC_JSON_STATE_VALUE_END)));
+}
+
+grpc_json_reader_status grpc_json_reader_run(grpc_json_reader* reader) {
+ gpr_uint32 c, success;
+
+ /* This state-machine is a strict implementation of ECMA-404 */
+ for (;;) {
+ c = grpc_json_reader_read_char(reader);
+ switch (c) {
+ /* Let's process the error cases first. */
+ case GRPC_JSON_READ_CHAR_ERROR:
+ return GRPC_JSON_READ_ERROR;
+
+ case GRPC_JSON_READ_CHAR_EAGAIN:
+ return GRPC_JSON_EAGAIN;
+
+ case GRPC_JSON_READ_CHAR_EOF:
+ if (grpc_json_reader_is_complete(reader)) {
+ return GRPC_JSON_DONE;
+ } else {
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ break;
+
+ /* Processing whitespaces. */
+ case ' ':
+ case '\t':
+ case '\n':
+ case '\r':
+ switch (reader->state) {
+ case GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
+ case GRPC_JSON_STATE_OBJECT_KEY_END:
+ case GRPC_JSON_STATE_VALUE_BEGIN:
+ case GRPC_JSON_STATE_VALUE_END:
+ case GRPC_JSON_STATE_END:
+ break;
+
+ case GRPC_JSON_STATE_OBJECT_KEY_STRING:
+ case GRPC_JSON_STATE_VALUE_STRING:
+ if (c != ' ') return GRPC_JSON_PARSE_ERROR;
+ if (reader->unicode_high_surrogate != 0) return GRPC_JSON_PARSE_ERROR;
+ json_reader_string_add_char(reader, c);
+ break;
+
+ case GRPC_JSON_STATE_VALUE_NUMBER:
+ case GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
+ case GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
+ case GRPC_JSON_STATE_VALUE_NUMBER_EPM:
+ success = json_reader_set_number(reader);
+ if (!success) return GRPC_JSON_PARSE_ERROR;
+ json_reader_string_clear(reader);
+ reader->state = GRPC_JSON_STATE_VALUE_END;
+ break;
+
+ default:
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ break;
+
+ /* Value, object or array terminations. */
+ case ',':
+ case '}':
+ case ']':
+ switch (reader->state) {
+ case GRPC_JSON_STATE_OBJECT_KEY_STRING:
+ case GRPC_JSON_STATE_VALUE_STRING:
+ if (reader->unicode_high_surrogate != 0) return GRPC_JSON_PARSE_ERROR;
+ json_reader_string_add_char(reader, c);
+ break;
+
+ case GRPC_JSON_STATE_VALUE_NUMBER:
+ case GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
+ case GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
+ case GRPC_JSON_STATE_VALUE_NUMBER_EPM:
+ success = json_reader_set_number(reader);
+ if (!success) return GRPC_JSON_PARSE_ERROR;
+ json_reader_string_clear(reader);
+ reader->state = GRPC_JSON_STATE_VALUE_END;
+ /* The missing break here is intentional. */
+
+ case GRPC_JSON_STATE_VALUE_END:
+ case GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
+ case GRPC_JSON_STATE_VALUE_BEGIN:
+ if (c == ',') {
+ if (reader->state != GRPC_JSON_STATE_VALUE_END) {
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ if (reader->in_object) {
+ reader->state = GRPC_JSON_STATE_OBJECT_KEY_BEGIN;
+ } else {
+ reader->state = GRPC_JSON_STATE_VALUE_BEGIN;
+ }
+ } else {
+ if (reader->depth-- == 0) return GRPC_JSON_PARSE_ERROR;
+ if ((c == '}') && !reader->in_object) {
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ if ((c == '}') &&
+ (reader->state == GRPC_JSON_STATE_OBJECT_KEY_BEGIN) &&
+ !reader->container_just_begun) {
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ if ((c == ']') && !reader->in_array) return GRPC_JSON_PARSE_ERROR;
+ if ((c == ']') &&
+ (reader->state == GRPC_JSON_STATE_VALUE_BEGIN) &&
+ !reader->container_just_begun) {
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ reader->state = GRPC_JSON_STATE_VALUE_END;
+ switch (grpc_json_reader_container_ends(reader)) {
+ case GRPC_JSON_OBJECT:
+ reader->in_object = 1;
+ reader->in_array = 0;
+ break;
+ case GRPC_JSON_ARRAY:
+ reader->in_object = 0;
+ reader->in_array = 1;
+ break;
+ case GRPC_JSON_TOP_LEVEL:
+ if (reader->depth != 0) return GRPC_JSON_INTERNAL_ERROR;
+ reader->in_object = 0;
+ reader->in_array = 0;
+ reader->state = GRPC_JSON_STATE_END;
+ break;
+ default:
+ return GRPC_JSON_INTERNAL_ERROR;
+ }
+ }
+ break;
+
+ default:
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ break;
+
+ /* In-string escaping. */
+ case '\\':
+ switch (reader->state) {
+ case GRPC_JSON_STATE_OBJECT_KEY_STRING:
+ reader->escaped_string_was_key = 1;
+ reader->state = GRPC_JSON_STATE_STRING_ESCAPE;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_STRING:
+ reader->escaped_string_was_key = 0;
+ reader->state = GRPC_JSON_STATE_STRING_ESCAPE;
+ break;
+
+ /* This is the \\ case. */
+ case GRPC_JSON_STATE_STRING_ESCAPE:
+ if (reader->unicode_high_surrogate != 0) return GRPC_JSON_PARSE_ERROR;
+ json_reader_string_add_char(reader, '\\');
+ if (reader->escaped_string_was_key) {
+ reader->state = GRPC_JSON_STATE_OBJECT_KEY_STRING;
+ } else {
+ reader->state = GRPC_JSON_STATE_VALUE_STRING;
+ }
+ break;
+
+ default:
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ break;
+
+ default:
+ reader->container_just_begun = 0;
+ switch (reader->state) {
+ case GRPC_JSON_STATE_OBJECT_KEY_BEGIN:
+ if (c != '"') return GRPC_JSON_PARSE_ERROR;
+ reader->state = GRPC_JSON_STATE_OBJECT_KEY_STRING;
+ break;
+
+ case GRPC_JSON_STATE_OBJECT_KEY_STRING:
+ if (reader->unicode_high_surrogate != 0) return GRPC_JSON_PARSE_ERROR;
+ if (c == '"') {
+ reader->state = GRPC_JSON_STATE_OBJECT_KEY_END;
+ json_reader_set_key(reader);
+ json_reader_string_clear(reader);
+ } else {
+ if (c <= 0x001f) return GRPC_JSON_PARSE_ERROR;
+ json_reader_string_add_char(reader, c);
+ }
+ break;
+
+ case GRPC_JSON_STATE_VALUE_STRING:
+ if (reader->unicode_high_surrogate != 0) return GRPC_JSON_PARSE_ERROR;
+ if (c == '"') {
+ reader->state = GRPC_JSON_STATE_VALUE_END;
+ json_reader_set_string(reader);
+ json_reader_string_clear(reader);
+ } else {
+ if (c < 32) return GRPC_JSON_PARSE_ERROR;
+ json_reader_string_add_char(reader, c);
+ }
+ break;
+
+ case GRPC_JSON_STATE_OBJECT_KEY_END:
+ if (c != ':') return GRPC_JSON_PARSE_ERROR;
+ reader->state = GRPC_JSON_STATE_VALUE_BEGIN;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_BEGIN:
+ switch (c) {
+ case 't':
+ reader->state = GRPC_JSON_STATE_VALUE_TRUE_R;
+ break;
+
+ case 'f':
+ reader->state = GRPC_JSON_STATE_VALUE_FALSE_A;
+ break;
+
+ case 'n':
+ reader->state = GRPC_JSON_STATE_VALUE_NULL_U;
+ break;
+
+ case '"':
+ reader->state = GRPC_JSON_STATE_VALUE_STRING;
+ break;
+
+ case '0':
+ json_reader_string_add_char(reader, c);
+ reader->state = GRPC_JSON_STATE_VALUE_NUMBER_ZERO;
+ break;
+
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ case '-':
+ json_reader_string_add_char(reader, c);
+ reader->state = GRPC_JSON_STATE_VALUE_NUMBER;
+ break;
+
+ case '{':
+ reader->container_just_begun = 1;
+ json_reader_container_begins(reader, GRPC_JSON_OBJECT);
+ reader->depth++;
+ reader->state = GRPC_JSON_STATE_OBJECT_KEY_BEGIN;
+ reader->in_object = 1;
+ reader->in_array = 0;
+ break;
+
+ case '[':
+ reader->container_just_begun = 1;
+ json_reader_container_begins(reader, GRPC_JSON_ARRAY);
+ reader->depth++;
+ reader->in_object = 0;
+ reader->in_array = 1;
+ break;
+ }
+ break;
+
+ case GRPC_JSON_STATE_STRING_ESCAPE:
+ if (reader->escaped_string_was_key) {
+ reader->state = GRPC_JSON_STATE_OBJECT_KEY_STRING;
+ } else {
+ reader->state = GRPC_JSON_STATE_VALUE_STRING;
+ }
+ if (reader->unicode_high_surrogate && c != 'u')
+ return GRPC_JSON_PARSE_ERROR;
+ switch (c) {
+ case '"':
+ case '/':
+ json_reader_string_add_char(reader, c);
+ break;
+ case 'b':
+ json_reader_string_add_char(reader, '\b');
+ break;
+ case 'f':
+ json_reader_string_add_char(reader, '\f');
+ break;
+ case 'n':
+ json_reader_string_add_char(reader, '\n');
+ break;
+ case 'r':
+ json_reader_string_add_char(reader, '\r');
+ break;
+ case 't':
+ json_reader_string_add_char(reader, '\t');
+ break;
+ case 'u':
+ reader->state = GRPC_JSON_STATE_STRING_ESCAPE_U1;
+ reader->unicode_char = 0;
+ break;
+ default:
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ break;
+
+ case GRPC_JSON_STATE_STRING_ESCAPE_U1:
+ case GRPC_JSON_STATE_STRING_ESCAPE_U2:
+ case GRPC_JSON_STATE_STRING_ESCAPE_U3:
+ case GRPC_JSON_STATE_STRING_ESCAPE_U4:
+ if ((c >= '0') && (c <= '9')) {
+ c -= '0';
+ } else if ((c >= 'A') && (c <= 'F')) {
+ c -= 'A' - 10;
+ } else if ((c >= 'a') && (c <= 'f')) {
+ c -= 'a' - 10;
+ } else {
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ reader->unicode_char <<= 4;
+ reader->unicode_char |= c;
+
+ switch (reader->state) {
+ case GRPC_JSON_STATE_STRING_ESCAPE_U1:
+ reader->state = GRPC_JSON_STATE_STRING_ESCAPE_U2;
+ break;
+ case GRPC_JSON_STATE_STRING_ESCAPE_U2:
+ reader->state = GRPC_JSON_STATE_STRING_ESCAPE_U3;
+ break;
+ case GRPC_JSON_STATE_STRING_ESCAPE_U3:
+ reader->state = GRPC_JSON_STATE_STRING_ESCAPE_U4;
+ break;
+ case GRPC_JSON_STATE_STRING_ESCAPE_U4:
+ /* See grpc_json_writer_escape_string to have a description
+ * of what's going on here.
+ */
+ if ((reader->unicode_char & 0xfc00) == 0xd800) {
+ /* high surrogate utf-16 */
+ if (reader->unicode_high_surrogate != 0)
+ return GRPC_JSON_PARSE_ERROR;
+ reader->unicode_high_surrogate = reader->unicode_char;
+ } else if ((reader->unicode_char & 0xfc00) == 0xdc00) {
+ /* low surrogate utf-16 */
+ gpr_uint32 utf32;
+ if (reader->unicode_high_surrogate == 0)
+ return GRPC_JSON_PARSE_ERROR;
+ utf32 = 0x10000;
+ utf32 += (reader->unicode_high_surrogate - 0xd800) * 0x400;
+ utf32 += reader->unicode_char - 0xdc00;
+ json_reader_string_add_utf32(reader, utf32);
+ reader->unicode_high_surrogate = 0;
+ } else {
+ /* anything else */
+ if (reader->unicode_high_surrogate != 0)
+ return GRPC_JSON_PARSE_ERROR;
+ json_reader_string_add_utf32(reader, reader->unicode_char);
+ }
+ if (reader->escaped_string_was_key) {
+ reader->state = GRPC_JSON_STATE_OBJECT_KEY_STRING;
+ } else {
+ reader->state = GRPC_JSON_STATE_VALUE_STRING;
+ }
+ break;
+ default:
+ return GRPC_JSON_INTERNAL_ERROR;
+ }
+ break;
+
+ case GRPC_JSON_STATE_VALUE_NUMBER:
+ json_reader_string_add_char(reader, c);
+ switch (c) {
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ break;
+ case 'e':
+ case 'E':
+ reader->state = GRPC_JSON_STATE_VALUE_NUMBER_E;
+ break;
+ case '.':
+ reader->state = GRPC_JSON_STATE_VALUE_NUMBER_DOT;
+ break;
+ default:
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ break;
+
+ case GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL:
+ json_reader_string_add_char(reader, c);
+ switch (c) {
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ break;
+ case 'e':
+ case 'E':
+ reader->state = GRPC_JSON_STATE_VALUE_NUMBER_E;
+ break;
+ default:
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ break;
+
+ case GRPC_JSON_STATE_VALUE_NUMBER_ZERO:
+ if (c != '.') return GRPC_JSON_PARSE_ERROR;
+ json_reader_string_add_char(reader, c);
+ reader->state = GRPC_JSON_STATE_VALUE_NUMBER_DOT;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_NUMBER_DOT:
+ json_reader_string_add_char(reader, c);
+ switch (c) {
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ reader->state = GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL;
+ break;
+ default:
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ break;
+
+ case GRPC_JSON_STATE_VALUE_NUMBER_E:
+ json_reader_string_add_char(reader, c);
+ switch (c) {
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ case '+':
+ case '-':
+ reader->state = GRPC_JSON_STATE_VALUE_NUMBER_EPM;
+ break;
+ default:
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ break;
+
+ case GRPC_JSON_STATE_VALUE_NUMBER_EPM:
+ json_reader_string_add_char(reader, c);
+ switch (c) {
+ case '0':
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ break;
+ default:
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ break;
+
+ case GRPC_JSON_STATE_VALUE_TRUE_R:
+ if (c != 'r') return GRPC_JSON_PARSE_ERROR;
+ reader->state = GRPC_JSON_STATE_VALUE_TRUE_U;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_TRUE_U:
+ if (c != 'u') return GRPC_JSON_PARSE_ERROR;
+ reader->state = GRPC_JSON_STATE_VALUE_TRUE_E;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_TRUE_E:
+ if (c != 'e') return GRPC_JSON_PARSE_ERROR;
+ json_reader_set_true(reader);
+ reader->state = GRPC_JSON_STATE_VALUE_END;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_FALSE_A:
+ if (c != 'a') return GRPC_JSON_PARSE_ERROR;
+ reader->state = GRPC_JSON_STATE_VALUE_FALSE_L;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_FALSE_L:
+ if (c != 'l') return GRPC_JSON_PARSE_ERROR;
+ reader->state = GRPC_JSON_STATE_VALUE_FALSE_S;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_FALSE_S:
+ if (c != 's') return GRPC_JSON_PARSE_ERROR;
+ reader->state = GRPC_JSON_STATE_VALUE_FALSE_E;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_FALSE_E:
+ if (c != 'e') return GRPC_JSON_PARSE_ERROR;
+ json_reader_set_false(reader);
+ reader->state = GRPC_JSON_STATE_VALUE_END;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_NULL_U:
+ if (c != 'u') return GRPC_JSON_PARSE_ERROR;
+ reader->state = GRPC_JSON_STATE_VALUE_NULL_L1;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_NULL_L1:
+ if (c != 'l') return GRPC_JSON_PARSE_ERROR;
+ reader->state = GRPC_JSON_STATE_VALUE_NULL_L2;
+ break;
+
+ case GRPC_JSON_STATE_VALUE_NULL_L2:
+ if (c != 'l') return GRPC_JSON_PARSE_ERROR;
+ json_reader_set_null(reader);
+ reader->state = GRPC_JSON_STATE_VALUE_END;
+ break;
+
+ /* All of the VALUE_END cases are handled in the specialized case
+ * above. */
+ case GRPC_JSON_STATE_VALUE_END:
+ switch (c) {
+ case ',':
+ case '}':
+ case ']':
+ return GRPC_JSON_INTERNAL_ERROR;
+ break;
+
+ default:
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ break;
+
+ case GRPC_JSON_STATE_END:
+ return GRPC_JSON_PARSE_ERROR;
+ }
+ }
+ }
+
+ return GRPC_JSON_INTERNAL_ERROR;
+}
diff --git a/src/core/json/json_reader.h b/src/core/json/json_reader.h
new file mode 100644
index 0000000000..388ee3633f
--- /dev/null
+++ b/src/core/json/json_reader.h
@@ -0,0 +1,160 @@
+/*
+ *
+ * Copyright 2014, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#ifndef __GRPC_SRC_CORE_JSON_JSON_READER_H__
+#define __GRPC_SRC_CORE_JSON_JSON_READER_H__
+
+#include <grpc/support/port_platform.h>
+#include "src/core/json/json_common.h"
+
+typedef enum {
+ GRPC_JSON_STATE_OBJECT_KEY_BEGIN,
+ GRPC_JSON_STATE_OBJECT_KEY_STRING,
+ GRPC_JSON_STATE_OBJECT_KEY_END,
+ GRPC_JSON_STATE_VALUE_BEGIN,
+ GRPC_JSON_STATE_VALUE_STRING,
+ GRPC_JSON_STATE_STRING_ESCAPE,
+ GRPC_JSON_STATE_STRING_ESCAPE_U1,
+ GRPC_JSON_STATE_STRING_ESCAPE_U2,
+ GRPC_JSON_STATE_STRING_ESCAPE_U3,
+ GRPC_JSON_STATE_STRING_ESCAPE_U4,
+ GRPC_JSON_STATE_VALUE_NUMBER,
+ GRPC_JSON_STATE_VALUE_NUMBER_WITH_DECIMAL,
+ GRPC_JSON_STATE_VALUE_NUMBER_ZERO,
+ GRPC_JSON_STATE_VALUE_NUMBER_DOT,
+ GRPC_JSON_STATE_VALUE_NUMBER_E,
+ GRPC_JSON_STATE_VALUE_NUMBER_EPM,
+ GRPC_JSON_STATE_VALUE_TRUE_R,
+ GRPC_JSON_STATE_VALUE_TRUE_U,
+ GRPC_JSON_STATE_VALUE_TRUE_E,
+ GRPC_JSON_STATE_VALUE_FALSE_A,
+ GRPC_JSON_STATE_VALUE_FALSE_L,
+ GRPC_JSON_STATE_VALUE_FALSE_S,
+ GRPC_JSON_STATE_VALUE_FALSE_E,
+ GRPC_JSON_STATE_VALUE_NULL_U,
+ GRPC_JSON_STATE_VALUE_NULL_L1,
+ GRPC_JSON_STATE_VALUE_NULL_L2,
+ GRPC_JSON_STATE_VALUE_END,
+ GRPC_JSON_STATE_END
+} grpc_json_reader_state;
+
+enum {
+ /* The first non-unicode value is 0x110000. But let's pick
+ * a value high enough to start our error codes from. These
+ * values are safe to return from the read_char function.
+ */
+ GRPC_JSON_READ_CHAR_EOF = 0x7ffffff0,
+ GRPC_JSON_READ_CHAR_EAGAIN,
+ GRPC_JSON_READ_CHAR_ERROR
+};
+
+struct grpc_json_reader;
+
+typedef struct grpc_json_reader_vtable {
+ /* Clears your internal string scratchpad. */
+ void (*string_clear)(void* userdata);
+ /* Adds a char to the string scratchpad. */
+ void (*string_add_char)(void* userdata, gpr_uint32 c);
+ /* Adds a utf32 char to the string scratchpad. */
+ void (*string_add_utf32)(void* userdata, gpr_uint32 c);
+ /* Reads a character from your input. May be utf-8, 16 or 32. */
+ gpr_uint32 (*read_char)(void* userdata);
+ /* Starts a container of type GRPC_JSON_ARRAY or GRPC_JSON_OBJECT. */
+ void (*container_begins)(void* userdata, grpc_json_type type);
+ /* Ends the current container. Must return the type of its parent. */
+ grpc_json_type (*container_ends)(void* userdata);
+ /* Your internal string scratchpad is an object's key. */
+ void (*set_key)(void* userdata);
+ /* Your internal string scratchpad is a string value. */
+ void (*set_string)(void* userdata);
+ /* Your internal string scratchpad is a numerical value. Return 1 if valid. */
+ int (*set_number)(void* userdata);
+ /* Sets the values true, false or null. */
+ void (*set_true)(void* userdata);
+ void (*set_false)(void* userdata);
+ void (*set_null)(void* userdata);
+} grpc_json_reader_vtable;
+
+typedef struct grpc_json_reader {
+ /* That structure is fully private, and initialized by grpc_json_reader_init.
+ * The definition is public so you can put it on your stack.
+ */
+
+ void* userdata;
+ grpc_json_reader_vtable* vtable;
+ int depth;
+ int in_object;
+ int in_array;
+ int escaped_string_was_key;
+ int container_just_begun;
+ gpr_uint16 unicode_char, unicode_high_surrogate;
+ grpc_json_reader_state state;
+} grpc_json_reader;
+
+/* The return type of the parser. */
+typedef enum {
+ GRPC_JSON_DONE, /* The parser finished successfully. */
+ GRPC_JSON_EAGAIN, /* The parser yields to get more data. */
+ GRPC_JSON_READ_ERROR, /* The parser passes through a read error. */
+ GRPC_JSON_PARSE_ERROR, /* The parser found an error in the json stream. */
+ GRPC_JSON_INTERNAL_ERROR /* The parser got an internal error. */
+} grpc_json_reader_status;
+
+/* Call this function to start parsing the input. It will return the following:
+ * . GRPC_JSON_DONE if the input got eof, and the parsing finished
+ * successfully.
+ * . GRPC_JSON_EAGAIN if the read_char function returned again. Call the
+ * parser again as needed. It is okay to call the parser in polling mode,
+ * although a bit dull.
+ * . GRPC_JSON_READ_ERROR if the read_char function returned an error. The
+ * state isn't broken however, and the function can be called again if the
+ * error has been corrected. But please use the EAGAIN feature instead for
+ * consistency.
+ * . GRPC_JSON_PARSE_ERROR if the input was somehow invalid.
+ * . GRPC_JSON_INTERNAL_ERROR if the parser somehow ended into an invalid
+ * internal state.
+ */
+grpc_json_reader_status grpc_json_reader_run(grpc_json_reader* reader);
+
+/* Call this function to initialize the reader structure. */
+void grpc_json_reader_init(grpc_json_reader* reader,
+ grpc_json_reader_vtable* vtable, void* userdata);
+
+/* You may call this from the read_char callback if you don't know where is the
+ * end of your input stream, and you'd like the json reader to hint you that it
+ * has completed reading its input, so you can return an EOF to it. Note that
+ * there might still be trailing whitespaces after that point.
+ */
+int grpc_json_reader_is_complete(grpc_json_reader* reader);
+
+#endif /* __GRPC_SRC_CORE_JSON_JSON_READER_H__ */
diff --git a/src/core/json/json_string.c b/src/core/json/json_string.c
new file mode 100644
index 0000000000..d29e9e30e8
--- /dev/null
+++ b/src/core/json/json_string.c
@@ -0,0 +1,391 @@
+/*
+ *
+ * Copyright 2014, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <string.h>
+#include <stdlib.h>
+
+#include <grpc/support/alloc.h>
+#include <grpc/support/log.h>
+
+#include "src/core/json/json.h"
+#include "src/core/json/json_reader.h"
+#include "src/core/json/json_writer.h"
+
+/* The json reader will construct a bunch of grpc_json objects and
+ * link them all up together in a tree-like structure that will represent
+ * the json data in memory.
+ *
+ * It also uses its own input as a scratchpad to store all of the decoded,
+ * unescaped strings. So we need to keep track of all these pointers in
+ * that opaque structure the reader will carry for us.
+ *
+ * Note that this works because the act of parsing json always reduces its
+ * input size, and never expands it.
+ */
+typedef struct {
+ grpc_json* top;
+ grpc_json* current_container;
+ grpc_json* current_value;
+ gpr_uint8* input;
+ gpr_uint8* key;
+ gpr_uint8* string;
+ gpr_uint8* string_ptr;
+ size_t remaining_input;
+} json_reader_userdata;
+
+/* This json writer will put everything in a big string.
+ * The point is that we allocate that string in chunks of 256 bytes.
+ */
+typedef struct {
+ char* output;
+ size_t free_space;
+ size_t string_len;
+ size_t allocated;
+} json_writer_userdata;
+
+
+/* This function checks if there's enough space left in the output buffer,
+ * and will enlarge it if necessary. We're only allocating chunks of 256
+ * bytes at a time (or multiples thereof).
+ */
+static void json_writer_output_check(void* userdata, size_t needed) {
+ json_writer_userdata* state = userdata;
+ if (state->free_space >= needed) return;
+ needed -= state->free_space;
+ /* Round up by 256 bytes. */
+ needed = (needed + 0xff) & ~0xff;
+ state->output = gpr_realloc(state->output, state->allocated + needed);
+ state->free_space += needed;
+ state->allocated += needed;
+}
+
+/* These are needed by the writer's implementation. */
+static void json_writer_output_char(void* userdata, char c) {
+ json_writer_userdata* state = userdata;
+ json_writer_output_check(userdata, 1);
+ state->output[state->string_len++] = c;
+ state->free_space--;
+}
+
+static void json_writer_output_string_with_len(void* userdata,
+ const char* str, size_t len) {
+ json_writer_userdata* state = userdata;
+ json_writer_output_check(userdata, len);
+ memcpy(state->output + state->string_len, str, len);
+ state->string_len += len;
+ state->free_space -= len;
+}
+
+static void json_writer_output_string(void* userdata,
+ const char* str) {
+ size_t len = strlen(str);
+ json_writer_output_string_with_len(userdata, str, len);
+}
+
+/* The reader asks us to clear our scratchpad. In our case, we'll simply mark
+ * the end of the current string, and advance our output pointer.
+ */
+static void json_reader_string_clear(void* userdata) {
+ json_reader_userdata* state = userdata;
+ if (state->string) {
+ GPR_ASSERT(state->string_ptr < state->input);
+ *state->string_ptr++ = 0;
+ }
+ state->string = state->string_ptr;
+}
+
+static void json_reader_string_add_char(void* userdata, gpr_uint32 c) {
+ json_reader_userdata* state = userdata;
+ GPR_ASSERT(state->string_ptr < state->input);
+ GPR_ASSERT(c <= 0xff);
+ *state->string_ptr++ = (char)c;
+}
+
+/* We are converting a UTF-32 character into UTF-8 here,
+ * as described by RFC3629.
+ */
+static void json_reader_string_add_utf32(void* userdata, gpr_uint32 c) {
+ if (c <= 0x7f) {
+ json_reader_string_add_char(userdata, c);
+ } else if (c <= 0x7ff) {
+ int b1 = 0xc0 | ((c >> 6) & 0x1f);
+ int b2 = 0x80 | (c & 0x3f);
+ json_reader_string_add_char(userdata, b1);
+ json_reader_string_add_char(userdata, b2);
+ } else if (c <= 0xffff) {
+ int b1 = 0xe0 | ((c >> 12) & 0x0f);
+ int b2 = 0x80 | ((c >> 6) & 0x3f);
+ int b3 = 0x80 | (c & 0x3f);
+ json_reader_string_add_char(userdata, b1);
+ json_reader_string_add_char(userdata, b2);
+ json_reader_string_add_char(userdata, b3);
+ } else if (c <= 0x1fffff) {
+ int b1 = 0xf0 | ((c >> 18) & 0x07);
+ int b2 = 0x80 | ((c >> 12) & 0x3f);
+ int b3 = 0x80 | ((c >> 6) & 0x3f);
+ int b4 = 0x80 | (c & 0x3f);
+ json_reader_string_add_char(userdata, b1);
+ json_reader_string_add_char(userdata, b2);
+ json_reader_string_add_char(userdata, b3);
+ json_reader_string_add_char(userdata, b4);
+ }
+}
+
+/* We consider that the input may be a zero-terminated string. So we
+ * can end up hitting eof before the end of the alleged string length.
+ */
+static gpr_uint32 json_reader_read_char(void* userdata) {
+ gpr_uint32 r;
+ json_reader_userdata* state = userdata;
+
+ if (state->remaining_input == 0) return GRPC_JSON_READ_CHAR_EOF;
+
+ r = *state->input++;
+ state->remaining_input--;
+
+ if (r == 0) {
+ state->remaining_input = 0;
+ return GRPC_JSON_READ_CHAR_EOF;
+ }
+
+ return r;
+}
+
+/* Helper function to create a new grpc_json object and link it into
+ * our tree-in-progress inside our opaque structure.
+ */
+static grpc_json* json_create_and_link(void* userdata,
+ grpc_json_type type) {
+ json_reader_userdata* state = userdata;
+ grpc_json* json = grpc_json_create(type);
+
+ json->parent = state->current_container;
+ json->prev = state->current_value;
+ state->current_value = json;
+
+ if (json->prev) {
+ json->prev->next = json;
+ }
+ if (json->parent) {
+ if (!json->parent->child) {
+ json->parent->child = json;
+ }
+ if (json->parent->type == GRPC_JSON_OBJECT) {
+ json->key = (char*) state->key;
+ }
+ }
+ if (!state->top) {
+ state->top = json;
+ }
+
+ return json;
+}
+
+static void json_reader_container_begins(void* userdata, grpc_json_type type) {
+ json_reader_userdata* state = userdata;
+ grpc_json* container;
+
+ GPR_ASSERT(type == GRPC_JSON_ARRAY || type == GRPC_JSON_OBJECT);
+
+ container = json_create_and_link(userdata, type);
+ state->current_container = container;
+ state->current_value = NULL;
+}
+
+/* It's important to remember that the reader is mostly stateless, so it
+ * isn't trying to remember what the container was prior the one that just
+ * ends. Since we're keeping track of these for our own purpose, we are
+ * able to return that information back, which is useful for it to validate
+ * the input json stream.
+ *
+ * Also note that if we're at the top of the tree, and the last container
+ * ends, we have to return GRPC_JSON_TOP_LEVEL.
+ */
+static grpc_json_type json_reader_container_ends(void* userdata) {
+ grpc_json_type container_type = GRPC_JSON_TOP_LEVEL;
+ json_reader_userdata* state = userdata;
+
+ GPR_ASSERT(state->current_container);
+
+ state->current_value = state->current_container;
+ state->current_container = state->current_container->parent;
+
+ if (state->current_container) {
+ container_type = state->current_container->type;
+ }
+
+ return container_type;
+}
+
+/* The next 3 functions basically are the reader asking us to use our string
+ * scratchpad for one of these 3 purposes.
+ *
+ * Note that in the set_number case, we're not going to try interpreting it.
+ * We'll keep it as a string, and leave it to the caller to evaluate it.
+ */
+static void json_reader_set_key(void* userdata) {
+ json_reader_userdata* state = userdata;
+ state->key = state->string;
+}
+
+static void json_reader_set_string(void* userdata) {
+ json_reader_userdata* state = userdata;
+ grpc_json* json = json_create_and_link(userdata, GRPC_JSON_STRING);
+ json->value = (char*) state->string;
+}
+
+static int json_reader_set_number(void* userdata) {
+ json_reader_userdata* state = userdata;
+ grpc_json* json = json_create_and_link(userdata, GRPC_JSON_NUMBER);
+ json->value = (char*) state->string;
+ return 1;
+}
+
+/* The object types true, false and null are self-sufficient, and don't need
+ * any more information beside their type.
+ */
+static void json_reader_set_true(void* userdata) {
+ json_create_and_link(userdata, GRPC_JSON_TRUE);
+}
+
+static void json_reader_set_false(void* userdata) {
+ json_create_and_link(userdata, GRPC_JSON_FALSE);
+}
+
+static void json_reader_set_null(void* userdata) {
+ json_create_and_link(userdata, GRPC_JSON_NULL);
+}
+
+static grpc_json_reader_vtable reader_vtable = {
+ json_reader_string_clear,
+ json_reader_string_add_char,
+ json_reader_string_add_utf32,
+ json_reader_read_char,
+ json_reader_container_begins,
+ json_reader_container_ends,
+ json_reader_set_key,
+ json_reader_set_string,
+ json_reader_set_number,
+ json_reader_set_true,
+ json_reader_set_false,
+ json_reader_set_null
+};
+
+/* And finally, let's define our public API. */
+grpc_json* grpc_json_parse_string_with_len(char* input, size_t size) {
+ grpc_json_reader reader;
+ json_reader_userdata state;
+ grpc_json *json = NULL;
+ grpc_json_reader_status status;
+
+ if (!input) return NULL;
+
+ state.top = state.current_container = state.current_value = NULL;
+ state.string = state.key = NULL;
+ state.string_ptr = state.input = (gpr_uint8*) input;
+ state.remaining_input = size;
+ grpc_json_reader_init(&reader, &reader_vtable, &state);
+
+ status = grpc_json_reader_run(&reader);
+ json = state.top;
+
+ if ((status != GRPC_JSON_DONE) && json) {
+ grpc_json_destroy(json);
+ json = NULL;
+ }
+
+ return json;
+}
+
+#define UNBOUND_JSON_STRING_LENGTH 0x7fffffff
+
+grpc_json* grpc_json_parse_string(char* input) {
+ return grpc_json_parse_string_with_len(input, UNBOUND_JSON_STRING_LENGTH);
+}
+
+static void json_dump_recursive(grpc_json_writer* writer,
+ grpc_json* json, int in_object) {
+ while (json) {
+ if (in_object) grpc_json_writer_object_key(writer, json->key);
+
+ switch (json->type) {
+ case GRPC_JSON_OBJECT:
+ case GRPC_JSON_ARRAY:
+ grpc_json_writer_container_begins(writer, json->type);
+ if (json->child)
+ json_dump_recursive(writer, json->child,
+ json->type == GRPC_JSON_OBJECT);
+ grpc_json_writer_container_ends(writer, json->type);
+ break;
+ case GRPC_JSON_STRING:
+ grpc_json_writer_value_string(writer, json->value);
+ break;
+ case GRPC_JSON_NUMBER:
+ grpc_json_writer_value_raw(writer, json->value);
+ break;
+ case GRPC_JSON_TRUE:
+ grpc_json_writer_value_raw_with_len(writer, "true", 4);
+ break;
+ case GRPC_JSON_FALSE:
+ grpc_json_writer_value_raw_with_len(writer, "false", 5);
+ break;
+ case GRPC_JSON_NULL:
+ grpc_json_writer_value_raw_with_len(writer, "null", 4);
+ break;
+ default:
+ abort();
+ }
+ json = json->next;
+ }
+}
+
+static grpc_json_writer_vtable writer_vtable = {
+ json_writer_output_char,
+ json_writer_output_string,
+ json_writer_output_string_with_len
+};
+
+char* grpc_json_dump_to_string(grpc_json* json, int indent) {
+ grpc_json_writer writer;
+ json_writer_userdata state;
+
+ state.output = NULL;
+ state.free_space = state.string_len = state.allocated = 0;
+ grpc_json_writer_init(&writer, indent, &writer_vtable, &state);
+
+ json_dump_recursive(&writer, json, 0);
+
+ json_writer_output_char(&state, 0);
+
+ return state.output;
+}
diff --git a/src/core/json/json_writer.c b/src/core/json/json_writer.c
new file mode 100644
index 0000000000..63c86ac008
--- /dev/null
+++ b/src/core/json/json_writer.c
@@ -0,0 +1,252 @@
+/*
+ *
+ * Copyright 2014, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+#include <string.h>
+
+#include <grpc/support/port_platform.h>
+
+#include "src/core/json/json_writer.h"
+
+static void json_writer_output_char(grpc_json_writer* writer, char c) {
+ writer->vtable->output_char(writer->userdata, c);
+}
+
+static void json_writer_output_string(grpc_json_writer* writer, const char* str) {
+ writer->vtable->output_string(writer->userdata, str);
+}
+
+static void json_writer_output_string_with_len(grpc_json_writer* writer, const char* str, size_t len) {
+ writer->vtable->output_string_with_len(writer->userdata, str, len);
+}
+
+void grpc_json_writer_init(grpc_json_writer* writer, int indent,
+ grpc_json_writer_vtable* vtable, void* userdata) {
+ memset(writer, 0, sizeof(grpc_json_writer));
+ writer->container_empty = 1;
+ writer->indent = indent;
+ writer->vtable = vtable;
+ writer->userdata = userdata;
+}
+
+static void json_writer_output_indent(
+ grpc_json_writer* writer) {
+ static const char spacesstr[] =
+ " "
+ " "
+ " "
+ " ";
+
+ int spaces = writer->depth * writer->indent;
+
+ if (writer->indent == 0) return;
+
+ if (writer->got_key) {
+ json_writer_output_char(writer, ' ');
+ return;
+ }
+
+ while (spaces >= (sizeof(spacesstr) - 1)) {
+ json_writer_output_string_with_len(writer, spacesstr,
+ sizeof(spacesstr) - 1);
+ spaces -= (sizeof(spacesstr) - 1);
+ }
+
+ if (spaces == 0) return;
+
+ json_writer_output_string_with_len(
+ writer, spacesstr + sizeof(spacesstr) - 1 - spaces, spaces);
+}
+
+static void json_writer_value_end(grpc_json_writer* writer) {
+ if (writer->container_empty) {
+ writer->container_empty = 0;
+ if ((writer->indent == 0) || (writer->depth == 0)) return;
+ json_writer_output_char(writer, '\n');
+ } else {
+ json_writer_output_char(writer, ',');
+ if (writer->indent == 0) return;
+ json_writer_output_char(writer, '\n');
+ }
+}
+
+static void json_writer_escape_utf16(grpc_json_writer* writer, gpr_uint16 utf16) {
+ static const char hex[] = "0123456789abcdef";
+
+ json_writer_output_string_with_len(writer, "\\u", 2);
+ json_writer_output_char(writer, hex[(utf16 >> 12) & 0x0f]);
+ json_writer_output_char(writer, hex[(utf16 >> 8) & 0x0f]);
+ json_writer_output_char(writer, hex[(utf16 >> 4) & 0x0f]);
+ json_writer_output_char(writer, hex[(utf16) & 0x0f]);
+}
+
+static void json_writer_escape_string(grpc_json_writer* writer,
+ const char* string) {
+ json_writer_output_char(writer, '"');
+
+ for (;;) {
+ gpr_uint8 c = (gpr_uint8)*string++;
+ if (c == 0) {
+ break;
+ } else if ((c >= 32) && (c <= 127)) {
+ if ((c == '\\') || (c == '"')) json_writer_output_char(writer, '\\');
+ json_writer_output_char(writer, c);
+ } else if (c < 32) {
+ switch (c) {
+ case '\b':
+ json_writer_output_string_with_len(writer, "\\b", 2);
+ break;
+ case '\f':
+ json_writer_output_string_with_len(writer, "\\f", 2);
+ break;
+ case '\n':
+ json_writer_output_string_with_len(writer, "\\n", 2);
+ break;
+ case '\r':
+ json_writer_output_string_with_len(writer, "\\r", 2);
+ break;
+ case '\t':
+ json_writer_output_string_with_len(writer, "\\t", 2);
+ break;
+ default:
+ json_writer_escape_utf16(writer, c);
+ break;
+ }
+ } else {
+ gpr_uint32 utf32 = 0;
+ int extra = 0;
+ int i;
+ int valid = 1;
+ if ((c & 0xe0) == 0xc0) {
+ utf32 = c & 0x1f;
+ extra = 1;
+ } else if ((c & 0xf0) == 0xe0) {
+ utf32 = c & 0x0f;
+ extra = 2;
+ } else if ((c & 0xf8) == 0xf0) {
+ utf32 = c & 0x07;
+ extra = 3;
+ } else {
+ break;
+ }
+ for (i = 0; i < extra; i++) {
+ utf32 <<= 6;
+ c = *string++;
+ if ((c & 0xc0) != 0x80) {
+ valid = 0;
+ break;
+ }
+ utf32 |= c & 0x3f;
+ }
+ if (!valid) break;
+ /* The range 0xd800 - 0xdfff is reserved by the surrogates ad vitam.
+ * Any other range is technically reserved for future usage, so if we
+ * don't want the software to break in the future, we have to allow
+ * anything else. The first non-unicode character is 0x110000. */
+ if (((utf32 >= 0xd800) && (utf32 <= 0xdfff)) ||
+ (utf32 >= 0x110000)) break;
+ if (utf32 >= 0x10000) {
+ /* If utf32 contains a character that is above 0xffff, it needs to be
+ * broken down into a utf-16 surrogate pair. A surrogate pair is first
+ * a high surrogate, followed by a low surrogate. Each surrogate holds
+ * 10 bits of usable data, thus allowing a total of 20 bits of data.
+ * The high surrogate marker is 0xd800, while the low surrogate marker
+ * is 0xdc00. The low 10 bits of each will be the usable data.
+ *
+ * After re-combining the 20 bits of data, one has to add 0x10000 to
+ * the resulting value, in order to obtain the original character.
+ * This is obviously because the range 0x0000 - 0xffff can be written
+ * without any special trick.
+ *
+ * Since 0x10ffff is the highest allowed character, we're working in
+ * the range 0x00000 - 0xfffff after we decrement it by 0x10000.
+ * That range is exactly 20 bits.
+ */
+ utf32 -= 0x10000;
+ json_writer_escape_utf16(writer, 0xd800 | (utf32 >> 10));
+ json_writer_escape_utf16(writer, 0xdc00 | (utf32 & 0x3ff));
+ } else {
+ json_writer_escape_utf16(writer, utf32);
+ }
+ }
+ }
+
+ json_writer_output_char(writer, '"');
+}
+
+void grpc_json_writer_container_begins(grpc_json_writer* writer, grpc_json_type type) {
+ if (!writer->got_key) json_writer_value_end(writer);
+ json_writer_output_indent(writer);
+ json_writer_output_char(writer, type == GRPC_JSON_OBJECT ? '{' : '[');
+ writer->container_empty = 1;
+ writer->got_key = 0;
+ writer->depth++;
+}
+
+void grpc_json_writer_container_ends(grpc_json_writer* writer, grpc_json_type type) {
+ if (writer->indent && !writer->container_empty)
+ json_writer_output_char(writer, '\n');
+ writer->depth--;
+ if (!writer->container_empty) json_writer_output_indent(writer);
+ json_writer_output_char(writer, type == GRPC_JSON_OBJECT ? '}' : ']');
+ writer->container_empty = 0;
+ writer->got_key = 0;
+}
+
+void grpc_json_writer_object_key(grpc_json_writer* writer, const char* string) {
+ json_writer_value_end(writer);
+ json_writer_output_indent(writer);
+ json_writer_escape_string(writer, string);
+ json_writer_output_char(writer, ':');
+ writer->got_key = 1;
+}
+
+void grpc_json_writer_value_raw(grpc_json_writer* writer, const char* string) {
+ if (!writer->got_key) json_writer_value_end(writer);
+ json_writer_output_indent(writer);
+ json_writer_output_string(writer, string);
+ writer->got_key = 0;
+}
+
+void grpc_json_writer_value_raw_with_len(grpc_json_writer* writer, const char* string, size_t len) {
+ if (!writer->got_key) json_writer_value_end(writer);
+ json_writer_output_indent(writer);
+ json_writer_output_string_with_len(writer, string, len);
+ writer->got_key = 0;
+}
+
+void grpc_json_writer_value_string(grpc_json_writer* writer, const char* string) {
+ if (!writer->got_key) json_writer_value_end(writer);
+ json_writer_output_indent(writer);
+ json_writer_escape_string(writer, string);
+ writer->got_key = 0;
+}
diff --git a/src/core/json/json_writer.h b/src/core/json/json_writer.h
new file mode 100644
index 0000000000..0568401590
--- /dev/null
+++ b/src/core/json/json_writer.h
@@ -0,0 +1,93 @@
+/*
+ *
+ * Copyright 2014, Google Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following disclaimer
+ * in the documentation and/or other materials provided with the
+ * distribution.
+ * * Neither the name of Google Inc. nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ */
+
+/* The idea of the writer is basically symmetrical of the reader. While the
+ * reader emits various calls to your code, the writer takes basically the
+ * same calls and emit json out of it. It doesn't try to make any check on
+ * the order of the calls you do on it. Meaning you can theorically force
+ * it to generate invalid json.
+ *
+ * Also, unlike the reader, the writer expects UTF-8 encoded input strings.
+ * These strings will be UTF-8 validated, and any invalid character will
+ * cut the conversion short, before any invalid UTF-8 sequence, thus forming
+ * a valid UTF-8 string overall.
+ */
+
+#ifndef __GRPC_SRC_CORE_JSON_JSON_WRITER_H__
+#define __GRPC_SRC_CORE_JSON_JSON_WRITER_H__
+
+#include <stdlib.h>
+
+#include "src/core/json/json_common.h"
+
+typedef struct grpc_json_writer_vtable {
+ /* Adds a character to the output stream. */
+ void (*output_char)(void* userdata, char);
+ /* Adds a zero-terminated string to the output stream. */
+ void (*output_string)(void* userdata, const char* str);
+ /* Adds a fixed-length string to the output stream. */
+ void (*output_string_with_len)(void* userdata, const char* str, size_t len);
+
+} grpc_json_writer_vtable;
+
+typedef struct grpc_json_writer {
+ void* userdata;
+ grpc_json_writer_vtable* vtable;
+ int indent;
+ int depth;
+ int container_empty;
+ int got_key;
+} grpc_json_writer;
+
+/* Call this to initialize your writer structure. The indent parameter is
+ * specifying the number of spaces to use for indenting the output. If you
+ * use indent=0, then the output will not have any newlines either, thus
+ * emitting a condensed json output.
+ */
+void grpc_json_writer_init(grpc_json_writer* writer, int indent,
+ grpc_json_writer_vtable* vtable, void* userdata);
+
+/* Signals the beginning of a container. */
+void grpc_json_writer_container_begins(grpc_json_writer* writer, grpc_json_type type);
+/* Signals the end of a container. */
+void grpc_json_writer_container_ends(grpc_json_writer* writer, grpc_json_type type);
+/* Writes down an object key for the next value. */
+void grpc_json_writer_object_key(grpc_json_writer* writer, const char* string);
+/* Sets a raw value. Useful for numbers. */
+void grpc_json_writer_value_raw(grpc_json_writer* writer, const char* string);
+/* Sets a raw value with its length. Useful for values like true or false. */
+void grpc_json_writer_value_raw_with_len(grpc_json_writer* writer, const char* string, size_t len);
+/* Sets a string value. It'll be escaped, and utf-8 validated. */
+void grpc_json_writer_value_string(grpc_json_writer* writer, const char* string);
+
+#endif /* __GRPC_SRC_CORE_JSON_JSON_WRITER_H__ */
diff --git a/src/core/security/credentials.c b/src/core/security/credentials.c
index 2f75556e7b..5d06f14215 100644
--- a/src/core/security/credentials.c
+++ b/src/core/security/credentials.c
@@ -42,7 +42,7 @@
#include <grpc/support/sync.h>
#include <grpc/support/time.h>
-#include "third_party/cJSON/cJSON.h"
+#include "src/core/json/json.h"
#include <string.h>
#include <stdio.h>
@@ -336,7 +336,7 @@ grpc_oauth2_token_fetcher_credentials_parse_server_response(
char *null_terminated_body = NULL;
char *new_access_token = NULL;
grpc_credentials_status status = GRPC_CREDENTIALS_OK;
- cJSON *json = NULL;
+ grpc_json *json = NULL;
if (response->body_length > 0) {
null_terminated_body = gpr_malloc(response->body_length + 1);
@@ -351,41 +351,48 @@ grpc_oauth2_token_fetcher_credentials_parse_server_response(
status = GRPC_CREDENTIALS_ERROR;
goto end;
} else {
- cJSON *access_token = NULL;
- cJSON *token_type = NULL;
- cJSON *expires_in = NULL;
- json = cJSON_Parse(null_terminated_body);
+ grpc_json *access_token = NULL;
+ grpc_json *token_type = NULL;
+ grpc_json *expires_in = NULL;
+ grpc_json *ptr;
+ json = grpc_json_parse_string(null_terminated_body);
if (json == NULL) {
gpr_log(GPR_ERROR, "Could not parse JSON from %s", null_terminated_body);
status = GRPC_CREDENTIALS_ERROR;
goto end;
}
- if (json->type != cJSON_Object) {
+ if (json->type != GRPC_JSON_OBJECT) {
gpr_log(GPR_ERROR, "Response should be a JSON object");
status = GRPC_CREDENTIALS_ERROR;
goto end;
}
- access_token = cJSON_GetObjectItem(json, "access_token");
- if (access_token == NULL || access_token->type != cJSON_String) {
+ for (ptr = json->child; ptr; ptr = ptr->next) {
+ if (strcmp(ptr->key, "access_token") == 0) {
+ access_token = ptr;
+ } else if (strcmp(ptr->key, "token_type") == 0) {
+ token_type = ptr;
+ } else if (strcmp(ptr->key, "expires_in") == 0) {
+ expires_in = ptr;
+ }
+ }
+ if (access_token == NULL || access_token->type != GRPC_JSON_STRING) {
gpr_log(GPR_ERROR, "Missing or invalid access_token in JSON.");
status = GRPC_CREDENTIALS_ERROR;
goto end;
}
- token_type = cJSON_GetObjectItem(json, "token_type");
- if (token_type == NULL || token_type->type != cJSON_String) {
+ if (token_type == NULL || token_type->type != GRPC_JSON_STRING) {
gpr_log(GPR_ERROR, "Missing or invalid token_type in JSON.");
status = GRPC_CREDENTIALS_ERROR;
goto end;
}
- expires_in = cJSON_GetObjectItem(json, "expires_in");
- if (expires_in == NULL || expires_in->type != cJSON_Number) {
+ if (expires_in == NULL || expires_in->type != GRPC_JSON_NUMBER) {
gpr_log(GPR_ERROR, "Missing or invalid expires_in in JSON.");
status = GRPC_CREDENTIALS_ERROR;
goto end;
}
- gpr_asprintf(&new_access_token, "%s %s", token_type->valuestring,
- access_token->valuestring);
- token_lifetime->tv_sec = expires_in->valueint;
+ gpr_asprintf(&new_access_token, "%s %s", token_type->value,
+ access_token->value);
+ token_lifetime->tv_sec = strtol(expires_in->value, NULL, 10);
token_lifetime->tv_nsec = 0;
if (*token_elem != NULL) grpc_mdelem_unref(*token_elem);
*token_elem = grpc_mdelem_from_strings(ctx, GRPC_AUTHORIZATION_METADATA_KEY,
@@ -400,7 +407,7 @@ end:
}
if (null_terminated_body != NULL) gpr_free(null_terminated_body);
if (new_access_token != NULL) gpr_free(new_access_token);
- if (json != NULL) cJSON_Delete(json);
+ if (json != NULL) grpc_json_destroy(json);
return status;
}
diff --git a/src/core/security/json_token.c b/src/core/security/json_token.c
index 82bd9b505a..b1548f02a7 100644
--- a/src/core/security/json_token.c
+++ b/src/core/security/json_token.c
@@ -44,7 +44,8 @@
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/pem.h>
-#include "third_party/cJSON/cJSON.h"
+
+#include "src/core/json/json.h"
/* --- Constants. --- */
@@ -64,18 +65,20 @@ static grpc_jwt_encode_and_sign_override g_jwt_encode_and_sign_override = NULL;
/* --- grpc_auth_json_key. --- */
-static const char *json_get_string_property(cJSON *json,
+static const char *json_get_string_property(grpc_json *json,
const char *prop_name) {
- cJSON *child = NULL;
- child = cJSON_GetObjectItem(json, prop_name);
- if (child == NULL || child->type != cJSON_String) {
+ grpc_json *child;
+ for (child = json->child; child != NULL; child = child->next) {
+ if (strcmp(child->key, prop_name) == 0) break;
+ }
+ if (child == NULL || child->type != GRPC_JSON_STRING) {
gpr_log(GPR_ERROR, "Invalid or missing %s property.", prop_name);
return NULL;
}
- return child->valuestring;
+ return child->value;
}
-static int set_json_key_string_property(cJSON *json, const char *prop_name,
+static int set_json_key_string_property(grpc_json *json, const char *prop_name,
char **json_key_field) {
const char *prop_value = json_get_string_property(json, prop_name);
if (prop_value == NULL) return 0;
@@ -91,7 +94,8 @@ int grpc_auth_json_key_is_valid(const grpc_auth_json_key *json_key) {
grpc_auth_json_key grpc_auth_json_key_create_from_string(
const char *json_string) {
grpc_auth_json_key result;
- cJSON *json = cJSON_Parse(json_string);
+ char *scratchpad = gpr_strdup(json_string);
+ grpc_json *json = grpc_json_parse_string(scratchpad);
BIO *bio = NULL;
const char *prop_value;
int success = 0;
@@ -100,7 +104,7 @@ grpc_auth_json_key grpc_auth_json_key_create_from_string(
result.type = GRPC_AUTH_JSON_KEY_TYPE_INVALID;
if (json == NULL) {
gpr_log(GPR_ERROR, "Invalid json string %s", json_string);
- return result;
+ goto end;
}
prop_value = json_get_string_property(json, "type");
@@ -136,8 +140,9 @@ grpc_auth_json_key grpc_auth_json_key_create_from_string(
end:
if (bio != NULL) BIO_free(bio);
- if (json != NULL) cJSON_Delete(json);
+ if (json != NULL) grpc_json_destroy(json);
if (!success) grpc_auth_json_key_destruct(&result);
+ gpr_free(scratchpad);
return result;
}
@@ -164,49 +169,63 @@ void grpc_auth_json_key_destruct(grpc_auth_json_key *json_key) {
/* --- jwt encoding and signature. --- */
+static grpc_json *create_child(grpc_json *brother, grpc_json *parent,
+ const char *key, const char *value,
+ grpc_json_type type) {
+ grpc_json *child = grpc_json_create(type);
+ if (brother) brother->next = child;
+ if (!parent->child) parent->child = child;
+ child->parent = parent;
+ child->value = value;
+ child->key = key;
+ return child;
+}
+
static char *encoded_jwt_header(const char *algorithm) {
- cJSON *json = cJSON_CreateObject();
- cJSON *child = cJSON_CreateString(algorithm);
+ grpc_json *json = grpc_json_create(GRPC_JSON_OBJECT);
+ grpc_json *child = NULL;
char *json_str = NULL;
char *result = NULL;
- cJSON_AddItemToObject(json, "alg", child);
- child = cJSON_CreateString(GRPC_JWT_TYPE);
- cJSON_AddItemToObject(json, "typ", child);
- json_str = cJSON_PrintUnformatted(json);
+
+ child = create_child(NULL, json, "alg", algorithm, GRPC_JSON_STRING);
+ create_child(child, json, "typ", GRPC_JWT_TYPE, GRPC_JSON_STRING);
+
+ json_str = grpc_json_dump_to_string(json, 0);
result = grpc_base64_encode(json_str, strlen(json_str), 1, 0);
- free(json_str);
- cJSON_Delete(json);
+ gpr_free(json_str);
+ grpc_json_destroy(json);
return result;
}
static char *encoded_jwt_claim(const grpc_auth_json_key *json_key,
const char *scope, gpr_timespec token_lifetime) {
- cJSON *json = cJSON_CreateObject();
- cJSON *child = NULL;
+ grpc_json *json = grpc_json_create(GRPC_JSON_OBJECT);
+ grpc_json *child = NULL;
char *json_str = NULL;
char *result = NULL;
gpr_timespec now = gpr_now();
gpr_timespec expiration = gpr_time_add(now, token_lifetime);
+ /* log10(2^64) ~= 20 */
+ char now_str[24];
+ char expiration_str[24];
if (gpr_time_cmp(token_lifetime, grpc_max_auth_token_lifetime) > 0) {
gpr_log(GPR_INFO, "Cropping token lifetime to maximum allowed value.");
expiration = gpr_time_add(now, grpc_max_auth_token_lifetime);
}
- child = cJSON_CreateString(json_key->client_email);
- cJSON_AddItemToObject(json, "iss", child);
- child = cJSON_CreateString(scope);
- cJSON_AddItemToObject(json, "scope", child);
- child = cJSON_CreateString(GRPC_JWT_AUDIENCE);
- cJSON_AddItemToObject(json, "aud", child);
- child = cJSON_CreateNumber(now.tv_sec);
- cJSON_SetIntValue(child, now.tv_sec);
- cJSON_AddItemToObject(json, "iat", child);
- child = cJSON_CreateNumber(expiration.tv_sec);
- cJSON_SetIntValue(child, expiration.tv_sec);
- cJSON_AddItemToObject(json, "exp", child);
- json_str = cJSON_PrintUnformatted(json);
+ sprintf(now_str, "%ld", now.tv_sec);
+ sprintf(expiration_str, "%ld", expiration.tv_sec);
+
+ child = create_child(NULL, json, "iss", json_key->client_email,
+ GRPC_JSON_STRING);
+ child = create_child(child, json, "scope", scope, GRPC_JSON_STRING);
+ child = create_child(child, json, "aud", GRPC_JWT_AUDIENCE, GRPC_JSON_STRING);
+ child = create_child(child, json, "iat", now_str, GRPC_JSON_NUMBER);
+ create_child(child, json, "exp", expiration_str, GRPC_JSON_NUMBER);
+
+ json_str = grpc_json_dump_to_string(json, 0);
result = grpc_base64_encode(json_str, strlen(json_str), 1, 0);
- free(json_str);
- cJSON_Delete(json);
+ gpr_free(json_str);
+ grpc_json_destroy(json);
return result;
}
@@ -216,10 +235,10 @@ static char *dot_concat_and_free_strings(char *str1, char *str2) {
size_t result_len = str1_len + 1 /* dot */ + str2_len;
char *result = gpr_malloc(result_len + 1 /* NULL terminated */);
char *current = result;
- strncpy(current, str1, str1_len);
+ memcpy(current, str1, str1_len);
current += str1_len;
*(current++) = '.';
- strncpy(current, str2, str2_len);
+ memcpy(current, str2, str2_len);
current += str2_len;
GPR_ASSERT((current - result) == result_len);
*current = '\0';