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 "absl/strings/string_view.h"
25
26 #include <grpc/support/alloc.h>
27 #include <grpc/support/log.h>
28
29 #include "src/core/lib/json/json.h"
30
31 namespace grpc_core {
32
33 namespace {
34
35 /* The idea of the writer is basically symmetrical of the reader. While the
36 * reader emits various calls to your code, the writer takes basically the
37 * same calls and emit json out of it. It doesn't try to make any check on
38 * the order of the calls you do on it. Meaning you can theorically force
39 * it to generate invalid json.
40 *
41 * Also, unlike the reader, the writer expects UTF-8 encoded input strings.
42 * These strings will be UTF-8 validated, and any invalid character will
43 * cut the conversion short, before any invalid UTF-8 sequence, thus forming
44 * a valid UTF-8 string overall.
45 */
46 class JsonWriter {
47 public:
48 static std::string Dump(const Json& value, int indent);
49
50 private:
JsonWriter(int indent)51 explicit JsonWriter(int indent) : indent_(indent) {}
52
53 void OutputCheck(size_t needed);
54 void OutputChar(char c);
55 void OutputString(const absl::string_view str);
56 void OutputIndent();
57 void ValueEnd();
58 void EscapeUtf16(uint16_t utf16);
59 void EscapeString(const std::string& string);
60 void ContainerBegins(Json::Type type);
61 void ContainerEnds(Json::Type type);
62 void ObjectKey(const std::string& string);
63 void ValueRaw(const std::string& string);
64 void ValueString(const std::string& string);
65
66 void DumpObject(const Json::Object& object);
67 void DumpArray(const Json::Array& array);
68 void DumpValue(const Json& value);
69
70 int indent_;
71 int depth_ = 0;
72 bool container_empty_ = true;
73 bool got_key_ = false;
74 std::string output_;
75 };
76
77 /* This function checks if there's enough space left in the output buffer,
78 * and will enlarge it if necessary. We're only allocating chunks of 256
79 * bytes at a time (or multiples thereof).
80 */
OutputCheck(size_t needed)81 void JsonWriter::OutputCheck(size_t needed) {
82 size_t free_space = output_.capacity() - output_.size();
83 if (free_space >= needed) return;
84 needed -= free_space;
85 /* Round up by 256 bytes. */
86 needed = (needed + 0xff) & ~0xffU;
87 output_.reserve(output_.capacity() + needed);
88 }
89
OutputChar(char c)90 void JsonWriter::OutputChar(char c) {
91 OutputCheck(1);
92 output_.push_back(c);
93 }
94
OutputString(const absl::string_view str)95 void JsonWriter::OutputString(const absl::string_view str) {
96 OutputCheck(str.size());
97 output_.append(str.data(), str.size());
98 }
99
OutputIndent()100 void JsonWriter::OutputIndent() {
101 static const char spacesstr[] =
102 " "
103 " "
104 " "
105 " ";
106 unsigned spaces = static_cast<unsigned>(depth_ * indent_);
107 if (indent_ == 0) return;
108 if (got_key_) {
109 OutputChar(' ');
110 return;
111 }
112 while (spaces >= (sizeof(spacesstr) - 1)) {
113 OutputString(absl::string_view(spacesstr, sizeof(spacesstr) - 1));
114 spaces -= static_cast<unsigned>(sizeof(spacesstr) - 1);
115 }
116 if (spaces == 0) return;
117 OutputString(
118 absl::string_view(spacesstr + sizeof(spacesstr) - 1 - spaces, spaces));
119 }
120
ValueEnd()121 void JsonWriter::ValueEnd() {
122 if (container_empty_) {
123 container_empty_ = false;
124 if (indent_ == 0 || depth_ == 0) return;
125 OutputChar('\n');
126 } else {
127 OutputChar(',');
128 if (indent_ == 0) return;
129 OutputChar('\n');
130 }
131 }
132
EscapeUtf16(uint16_t utf16)133 void JsonWriter::EscapeUtf16(uint16_t utf16) {
134 static const char hex[] = "0123456789abcdef";
135 OutputString(absl::string_view("\\u", 2));
136 OutputChar(hex[(utf16 >> 12) & 0x0f]);
137 OutputChar(hex[(utf16 >> 8) & 0x0f]);
138 OutputChar(hex[(utf16 >> 4) & 0x0f]);
139 OutputChar(hex[(utf16)&0x0f]);
140 }
141
EscapeString(const std::string & string)142 void JsonWriter::EscapeString(const std::string& string) {
143 OutputChar('"');
144 for (size_t idx = 0; idx < string.size(); ++idx) {
145 uint8_t c = static_cast<uint8_t>(string[idx]);
146 if (c == 0) {
147 break;
148 } else if (c >= 32 && c <= 126) {
149 if (c == '\\' || c == '"') OutputChar('\\');
150 OutputChar(static_cast<char>(c));
151 } else if (c < 32 || c == 127) {
152 switch (c) {
153 case '\b':
154 OutputString(absl::string_view("\\b", 2));
155 break;
156 case '\f':
157 OutputString(absl::string_view("\\f", 2));
158 break;
159 case '\n':
160 OutputString(absl::string_view("\\n", 2));
161 break;
162 case '\r':
163 OutputString(absl::string_view("\\r", 2));
164 break;
165 case '\t':
166 OutputString(absl::string_view("\\t", 2));
167 break;
168 default:
169 EscapeUtf16(c);
170 break;
171 }
172 } else {
173 uint32_t utf32 = 0;
174 int extra = 0;
175 int i;
176 int valid = 1;
177 if ((c & 0xe0) == 0xc0) {
178 utf32 = c & 0x1f;
179 extra = 1;
180 } else if ((c & 0xf0) == 0xe0) {
181 utf32 = c & 0x0f;
182 extra = 2;
183 } else if ((c & 0xf8) == 0xf0) {
184 utf32 = c & 0x07;
185 extra = 3;
186 } else {
187 break;
188 }
189 for (i = 0; i < extra; i++) {
190 utf32 <<= 6;
191 ++idx;
192 /* Breaks out and bail if we hit the end of the string. */
193 if (idx == string.size()) {
194 valid = 0;
195 break;
196 }
197 c = static_cast<uint8_t>(string[idx]);
198 /* Breaks out and bail on any invalid UTF-8 sequence, including \0. */
199 if ((c & 0xc0) != 0x80) {
200 valid = 0;
201 break;
202 }
203 utf32 |= c & 0x3f;
204 }
205 if (!valid) break;
206 /* The range 0xd800 - 0xdfff is reserved by the surrogates ad vitam.
207 * Any other range is technically reserved for future usage, so if we
208 * don't want the software to break in the future, we have to allow
209 * anything else. The first non-unicode character is 0x110000. */
210 if (((utf32 >= 0xd800) && (utf32 <= 0xdfff)) || (utf32 >= 0x110000))
211 break;
212 if (utf32 >= 0x10000) {
213 /* If utf32 contains a character that is above 0xffff, it needs to be
214 * broken down into a utf-16 surrogate pair. A surrogate pair is first
215 * a high surrogate, followed by a low surrogate. Each surrogate holds
216 * 10 bits of usable data, thus allowing a total of 20 bits of data.
217 * The high surrogate marker is 0xd800, while the low surrogate marker
218 * is 0xdc00. The low 10 bits of each will be the usable data.
219 *
220 * After re-combining the 20 bits of data, one has to add 0x10000 to
221 * the resulting value, in order to obtain the original character.
222 * This is obviously because the range 0x0000 - 0xffff can be written
223 * without any special trick.
224 *
225 * Since 0x10ffff is the highest allowed character, we're working in
226 * the range 0x00000 - 0xfffff after we decrement it by 0x10000.
227 * That range is exactly 20 bits.
228 */
229 utf32 -= 0x10000;
230 EscapeUtf16(static_cast<uint16_t>(0xd800 | (utf32 >> 10)));
231 EscapeUtf16(static_cast<uint16_t>(0xdc00 | (utf32 & 0x3ff)));
232 } else {
233 EscapeUtf16(static_cast<uint16_t>(utf32));
234 }
235 }
236 }
237 OutputChar('"');
238 }
239
ContainerBegins(Json::Type type)240 void JsonWriter::ContainerBegins(Json::Type type) {
241 if (!got_key_) ValueEnd();
242 OutputIndent();
243 OutputChar(type == Json::Type::OBJECT ? '{' : '[');
244 container_empty_ = true;
245 got_key_ = false;
246 depth_++;
247 }
248
ContainerEnds(Json::Type type)249 void JsonWriter::ContainerEnds(Json::Type type) {
250 if (indent_ && !container_empty_) OutputChar('\n');
251 depth_--;
252 if (!container_empty_) OutputIndent();
253 OutputChar(type == Json::Type::OBJECT ? '}' : ']');
254 container_empty_ = false;
255 got_key_ = false;
256 }
257
ObjectKey(const std::string & string)258 void JsonWriter::ObjectKey(const std::string& string) {
259 ValueEnd();
260 OutputIndent();
261 EscapeString(string);
262 OutputChar(':');
263 got_key_ = true;
264 }
265
ValueRaw(const std::string & string)266 void JsonWriter::ValueRaw(const std::string& string) {
267 if (!got_key_) ValueEnd();
268 OutputIndent();
269 OutputString(string);
270 got_key_ = false;
271 }
272
ValueString(const std::string & string)273 void JsonWriter::ValueString(const std::string& string) {
274 if (!got_key_) ValueEnd();
275 OutputIndent();
276 EscapeString(string);
277 got_key_ = false;
278 }
279
DumpObject(const Json::Object & object)280 void JsonWriter::DumpObject(const Json::Object& object) {
281 ContainerBegins(Json::Type::OBJECT);
282 for (const auto& p : object) {
283 ObjectKey(p.first.data());
284 DumpValue(p.second);
285 }
286 ContainerEnds(Json::Type::OBJECT);
287 }
288
DumpArray(const Json::Array & array)289 void JsonWriter::DumpArray(const Json::Array& array) {
290 ContainerBegins(Json::Type::ARRAY);
291 for (const auto& v : array) {
292 DumpValue(v);
293 }
294 ContainerEnds(Json::Type::ARRAY);
295 }
296
DumpValue(const Json & value)297 void JsonWriter::DumpValue(const Json& value) {
298 switch (value.type()) {
299 case Json::Type::OBJECT:
300 DumpObject(value.object_value());
301 break;
302 case Json::Type::ARRAY:
303 DumpArray(value.array_value());
304 break;
305 case Json::Type::STRING:
306 ValueString(value.string_value());
307 break;
308 case Json::Type::NUMBER:
309 ValueRaw(value.string_value());
310 break;
311 case Json::Type::JSON_TRUE:
312 ValueRaw(std::string("true", 4));
313 break;
314 case Json::Type::JSON_FALSE:
315 ValueRaw(std::string("false", 5));
316 break;
317 case Json::Type::JSON_NULL:
318 ValueRaw(std::string("null", 4));
319 break;
320 default:
321 GPR_UNREACHABLE_CODE(abort());
322 }
323 }
324
Dump(const Json & value,int indent)325 std::string JsonWriter::Dump(const Json& value, int indent) {
326 JsonWriter writer(indent);
327 writer.DumpValue(value);
328 return std::move(writer.output_);
329 }
330
331 } // namespace
332
Dump(int indent) const333 std::string Json::Dump(int indent) const {
334 return JsonWriter::Dump(*this, indent);
335 }
336
337 } // namespace grpc_core
338