OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015, Google Inc. |
| 4 * All rights reserved. |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are |
| 8 * met: |
| 9 * |
| 10 * * Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. |
| 12 * * Redistributions in binary form must reproduce the above |
| 13 * copyright notice, this list of conditions and the following disclaimer |
| 14 * in the documentation and/or other materials provided with the |
| 15 * distribution. |
| 16 * * Neither the name of Google Inc. nor the names of its |
| 17 * contributors may be used to endorse or promote products derived from |
| 18 * this software without specific prior written permission. |
| 19 * |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 * |
| 32 */ |
| 33 |
| 34 #include <stdio.h> |
| 35 #include <stdlib.h> |
| 36 |
| 37 #include <grpc/support/alloc.h> |
| 38 #include <grpc/support/cmdline.h> |
| 39 #include <grpc/support/log.h> |
| 40 |
| 41 #include "src/core/json/json_reader.h" |
| 42 #include "src/core/json/json_writer.h" |
| 43 |
| 44 typedef struct json_writer_userdata { FILE *out; } json_writer_userdata; |
| 45 |
| 46 typedef struct stacked_container { |
| 47 grpc_json_type type; |
| 48 struct stacked_container *next; |
| 49 } stacked_container; |
| 50 |
| 51 typedef struct json_reader_userdata { |
| 52 FILE *in; |
| 53 grpc_json_writer *writer; |
| 54 char *scratchpad; |
| 55 char *ptr; |
| 56 size_t free_space; |
| 57 size_t allocated; |
| 58 size_t string_len; |
| 59 stacked_container *top; |
| 60 } json_reader_userdata; |
| 61 |
| 62 static void json_writer_output_char(void *userdata, char c) { |
| 63 json_writer_userdata *state = userdata; |
| 64 fputc(c, state->out); |
| 65 } |
| 66 |
| 67 static void json_writer_output_string(void *userdata, const char *str) { |
| 68 json_writer_userdata *state = userdata; |
| 69 fputs(str, state->out); |
| 70 } |
| 71 |
| 72 static void json_writer_output_string_with_len(void *userdata, const char *str, |
| 73 size_t len) { |
| 74 json_writer_userdata *state = userdata; |
| 75 fwrite(str, len, 1, state->out); |
| 76 } |
| 77 |
| 78 grpc_json_writer_vtable writer_vtable = {json_writer_output_char, |
| 79 json_writer_output_string, |
| 80 json_writer_output_string_with_len}; |
| 81 |
| 82 static void check_string(json_reader_userdata *state, size_t needed) { |
| 83 if (state->free_space >= needed) return; |
| 84 needed -= state->free_space; |
| 85 needed = (needed + 0xffu) & ~0xffu; |
| 86 state->scratchpad = gpr_realloc(state->scratchpad, state->allocated + needed); |
| 87 state->free_space += needed; |
| 88 state->allocated += needed; |
| 89 } |
| 90 |
| 91 static void json_reader_string_clear(void *userdata) { |
| 92 json_reader_userdata *state = userdata; |
| 93 state->free_space = state->allocated; |
| 94 state->string_len = 0; |
| 95 } |
| 96 |
| 97 static void json_reader_string_add_char(void *userdata, uint32_t c) { |
| 98 json_reader_userdata *state = userdata; |
| 99 check_string(state, 1); |
| 100 GPR_ASSERT(c < 256); |
| 101 state->scratchpad[state->string_len++] = (char)c; |
| 102 } |
| 103 |
| 104 static void json_reader_string_add_utf32(void *userdata, uint32_t c) { |
| 105 if (c <= 0x7f) { |
| 106 json_reader_string_add_char(userdata, c); |
| 107 } else if (c <= 0x7ff) { |
| 108 uint32_t b1 = 0xc0u | ((c >> 6u) & 0x1fu); |
| 109 uint32_t b2 = 0x80u | (c & 0x3fu); |
| 110 json_reader_string_add_char(userdata, b1); |
| 111 json_reader_string_add_char(userdata, b2); |
| 112 } else if (c <= 0xffffu) { |
| 113 uint32_t b1 = 0xe0u | ((c >> 12u) & 0x0fu); |
| 114 uint32_t b2 = 0x80u | ((c >> 6u) & 0x3fu); |
| 115 uint32_t b3 = 0x80u | (c & 0x3fu); |
| 116 json_reader_string_add_char(userdata, b1); |
| 117 json_reader_string_add_char(userdata, b2); |
| 118 json_reader_string_add_char(userdata, b3); |
| 119 } else if (c <= 0x1fffffu) { |
| 120 uint32_t b1 = 0xf0u | ((c >> 18u) & 0x07u); |
| 121 uint32_t b2 = 0x80u | ((c >> 12u) & 0x3fu); |
| 122 uint32_t b3 = 0x80u | ((c >> 6u) & 0x3fu); |
| 123 uint32_t b4 = 0x80u | (c & 0x3fu); |
| 124 json_reader_string_add_char(userdata, b1); |
| 125 json_reader_string_add_char(userdata, b2); |
| 126 json_reader_string_add_char(userdata, b3); |
| 127 json_reader_string_add_char(userdata, b4); |
| 128 } |
| 129 } |
| 130 |
| 131 static uint32_t json_reader_read_char(void *userdata) { |
| 132 int r; |
| 133 json_reader_userdata *state = userdata; |
| 134 |
| 135 r = fgetc(state->in); |
| 136 if (r == EOF) r = GRPC_JSON_READ_CHAR_EOF; |
| 137 return (uint32_t)r; |
| 138 } |
| 139 |
| 140 static void json_reader_container_begins(void *userdata, grpc_json_type type) { |
| 141 json_reader_userdata *state = userdata; |
| 142 stacked_container *container = gpr_malloc(sizeof(stacked_container)); |
| 143 |
| 144 container->type = type; |
| 145 container->next = state->top; |
| 146 state->top = container; |
| 147 |
| 148 grpc_json_writer_container_begins(state->writer, type); |
| 149 } |
| 150 |
| 151 static grpc_json_type json_reader_container_ends(void *userdata) { |
| 152 json_reader_userdata *state = userdata; |
| 153 stacked_container *container = state->top; |
| 154 |
| 155 grpc_json_writer_container_ends(state->writer, container->type); |
| 156 state->top = container->next; |
| 157 gpr_free(container); |
| 158 return state->top ? state->top->type : GRPC_JSON_TOP_LEVEL; |
| 159 } |
| 160 |
| 161 static void json_reader_set_key(void *userdata) { |
| 162 json_reader_userdata *state = userdata; |
| 163 json_reader_string_add_char(userdata, 0); |
| 164 |
| 165 grpc_json_writer_object_key(state->writer, state->scratchpad); |
| 166 } |
| 167 |
| 168 static void json_reader_set_string(void *userdata) { |
| 169 json_reader_userdata *state = userdata; |
| 170 json_reader_string_add_char(userdata, 0); |
| 171 |
| 172 grpc_json_writer_value_string(state->writer, state->scratchpad); |
| 173 } |
| 174 |
| 175 static int json_reader_set_number(void *userdata) { |
| 176 json_reader_userdata *state = userdata; |
| 177 |
| 178 grpc_json_writer_value_raw_with_len(state->writer, state->scratchpad, |
| 179 state->string_len); |
| 180 |
| 181 return 1; |
| 182 } |
| 183 |
| 184 static void json_reader_set_true(void *userdata) { |
| 185 json_reader_userdata *state = userdata; |
| 186 |
| 187 grpc_json_writer_value_raw_with_len(state->writer, "true", 4); |
| 188 } |
| 189 |
| 190 static void json_reader_set_false(void *userdata) { |
| 191 json_reader_userdata *state = userdata; |
| 192 |
| 193 grpc_json_writer_value_raw_with_len(state->writer, "false", 5); |
| 194 } |
| 195 |
| 196 static void json_reader_set_null(void *userdata) { |
| 197 json_reader_userdata *state = userdata; |
| 198 |
| 199 grpc_json_writer_value_raw_with_len(state->writer, "null", 4); |
| 200 } |
| 201 |
| 202 static grpc_json_reader_vtable reader_vtable = { |
| 203 json_reader_string_clear, json_reader_string_add_char, |
| 204 json_reader_string_add_utf32, json_reader_read_char, |
| 205 json_reader_container_begins, json_reader_container_ends, |
| 206 json_reader_set_key, json_reader_set_string, |
| 207 json_reader_set_number, json_reader_set_true, |
| 208 json_reader_set_false, json_reader_set_null}; |
| 209 |
| 210 int rewrite(FILE *in, FILE *out, int indent) { |
| 211 grpc_json_writer writer; |
| 212 grpc_json_reader reader; |
| 213 grpc_json_reader_status status; |
| 214 json_writer_userdata writer_user; |
| 215 json_reader_userdata reader_user; |
| 216 |
| 217 reader_user.writer = &writer; |
| 218 reader_user.in = in; |
| 219 reader_user.top = NULL; |
| 220 reader_user.scratchpad = NULL; |
| 221 reader_user.string_len = 0; |
| 222 reader_user.free_space = 0; |
| 223 reader_user.allocated = 0; |
| 224 |
| 225 writer_user.out = out; |
| 226 |
| 227 grpc_json_writer_init(&writer, indent, &writer_vtable, &writer_user); |
| 228 grpc_json_reader_init(&reader, &reader_vtable, &reader_user); |
| 229 |
| 230 status = grpc_json_reader_run(&reader); |
| 231 |
| 232 free(reader_user.scratchpad); |
| 233 while (reader_user.top) { |
| 234 stacked_container *container = reader_user.top; |
| 235 reader_user.top = container->next; |
| 236 free(container); |
| 237 } |
| 238 |
| 239 return status == GRPC_JSON_DONE; |
| 240 } |
| 241 |
| 242 int main(int argc, char **argv) { |
| 243 int indent = 2; |
| 244 gpr_cmdline *cl; |
| 245 |
| 246 cl = gpr_cmdline_create(NULL); |
| 247 gpr_cmdline_add_int(cl, "indent", NULL, &indent); |
| 248 gpr_cmdline_parse(cl, argc, argv); |
| 249 gpr_cmdline_destroy(cl); |
| 250 |
| 251 return rewrite(stdin, stdout, indent) ? 0 : 1; |
| 252 } |
OLD | NEW |