Line | Count | Source (jump to first uncovered line) |
1 | | #ifndef ZSERIO_JSON_DECODER_H_INC |
2 | | #define ZSERIO_JSON_DECODER_H_INC |
3 | | |
4 | | #include <cerrno> |
5 | | #include <cmath> |
6 | | #include <cstdlib> |
7 | | #include <cstring> |
8 | | #include <string> |
9 | | #include <utility> |
10 | | |
11 | | #include "zserio/AllocatorHolder.h" |
12 | | #include "zserio/AnyHolder.h" |
13 | | #include "zserio/CppRuntimeException.h" |
14 | | #include "zserio/String.h" |
15 | | #include "zserio/StringView.h" |
16 | | #include "zserio/Types.h" |
17 | | |
18 | | namespace zserio |
19 | | { |
20 | | |
21 | | /** |
22 | | * JSON value decoder. |
23 | | */ |
24 | | template <typename ALLOC = std::allocator<uint8_t>> |
25 | | class BasicJsonDecoder : public AllocatorHolder<ALLOC> |
26 | | { |
27 | | public: |
28 | | using AllocatorHolder<ALLOC>::get_allocator; |
29 | | |
30 | | /** |
31 | | * Decoder result value. |
32 | | */ |
33 | | struct DecoderResult |
34 | | { |
35 | | /** |
36 | | * Constructor used for decoder failure. |
37 | | * |
38 | | * \param numRead Number of processed characters. |
39 | | * \param allocator Allocator to use. |
40 | | */ |
41 | | DecoderResult(size_t numRead, const ALLOC& allocator) : |
42 | | numReadChars(numRead), |
43 | | value(allocator), |
44 | | integerOverflow(false) |
45 | 136 | {} |
46 | | |
47 | | /** |
48 | | * Constructor for decoder success. |
49 | | * |
50 | | * \param numRead Number of processed characters. |
51 | | * \param decodedValue Value decoded from JSON stream. |
52 | | * \param allocator Allocator to use. |
53 | | */ |
54 | | template <typename T> |
55 | | DecoderResult(size_t numRead, T&& decodedValue, const ALLOC& allocator) : |
56 | | numReadChars(numRead), |
57 | | value(std::forward<T>(decodedValue), allocator), |
58 | | integerOverflow(false) |
59 | 20.3k | {} |
60 | | |
61 | | /** |
62 | | * Constructor used for integer decoder. |
63 | | * |
64 | | * \param numRead Number of processed characters. |
65 | | * \param decodedValue Value decoded from JSON stream. |
66 | | * \param overflow True in case of integer overflow. |
67 | | * \param allocator Allocator to use. |
68 | | */ |
69 | | template <typename T> |
70 | | DecoderResult(size_t numRead, T&& decodedValue, bool overflow, const ALLOC& allocator) : |
71 | | numReadChars(numRead), |
72 | | value(createValue(std::forward<T>(decodedValue), overflow, allocator)), |
73 | | integerOverflow(overflow) |
74 | 4.11k | {} |
75 | | |
76 | | size_t numReadChars; /**< Number of processed characters. */ |
77 | | AnyHolder<ALLOC> value; /**< Decoded value. Empty on failure. */ |
78 | | bool integerOverflow; /**< True if decoded value was bigger than UINT64_MAX or was not in interval |
79 | | <INT64_MIN, INT64_MAX>. */ |
80 | | |
81 | | private: |
82 | | template <typename T> |
83 | | AnyHolder<ALLOC> createValue(T&& decodedValue, bool overflow, const ALLOC& allocator) |
84 | 4.11k | { |
85 | 4.11k | return overflow ? AnyHolder<ALLOC>(allocator)2 |
86 | 4.11k | : AnyHolder<ALLOC>(std::forward<T>(decodedValue), allocator)4.11k ; |
87 | 4.11k | } |
88 | | }; |
89 | | |
90 | | /** |
91 | | * Empty constructor. |
92 | | */ |
93 | | BasicJsonDecoder() : |
94 | | AllocatorHolder<ALLOC>(ALLOC()) |
95 | 12 | {} |
96 | | |
97 | | /** |
98 | | * Constructor from given allocator. |
99 | | * |
100 | | * \param allocator Allocator to use. |
101 | | */ |
102 | | explicit BasicJsonDecoder(const ALLOC& allocator) : |
103 | | AllocatorHolder<ALLOC>(allocator) |
104 | 93 | {} |
105 | | |
106 | | /** |
107 | | * Decodes the JSON value from the input. |
108 | | * |
109 | | * \param input Input to decode from. |
110 | | * |
111 | | * \return Decoder result. |
112 | | */ |
113 | | DecoderResult decodeValue(StringView input) |
114 | 24.5k | { |
115 | 24.5k | if (input.empty()) |
116 | 1 | { |
117 | 1 | return DecoderResult(0, get_allocator()); |
118 | 1 | } |
119 | | |
120 | 24.5k | switch (input[0]) |
121 | 24.5k | { |
122 | 9 | case 'n': |
123 | 9 | return decodeLiteral(input, "null"_sv, nullptr); |
124 | 7 | case 't': |
125 | 7 | return decodeLiteral(input, "true"_sv, true); |
126 | 4 | case 'f': |
127 | 4 | return decodeLiteral(input, "false"_sv, false); |
128 | 4 | case 'N': |
129 | 4 | return decodeLiteral(input, "NaN"_sv, static_cast<double>(NAN)); |
130 | 4 | case 'I': |
131 | 4 | return decodeLiteral(input, "Infinity"_sv, static_cast<double>(INFINITY)); |
132 | 16.3k | case '"': |
133 | 16.3k | return decodeString(input); |
134 | 24 | case '-': |
135 | 24 | if (input.size() > 1 && input[1] == 'I'23 ) |
136 | 5 | { |
137 | 5 | return decodeLiteral(input, "-Infinity"_sv, -static_cast<double>(INFINITY)); |
138 | 5 | } |
139 | 19 | return decodeNumber(input); |
140 | 8.13k | default: |
141 | 8.13k | return decodeNumber(input); |
142 | 24.5k | } |
143 | 24.5k | } |
144 | | |
145 | | private: |
146 | | template <typename T> |
147 | | DecoderResult decodeLiteral(StringView input, StringView literal, T&& value); |
148 | | DecoderResult decodeString(StringView input); |
149 | | static bool decodeUnicodeEscape( |
150 | | StringView input, StringView::const_iterator& inputIt, string<ALLOC>& value); |
151 | | static int32_t decodeHex(char character); |
152 | | size_t checkNumber(StringView input, bool& isDouble, bool& isSigned); |
153 | | DecoderResult decodeNumber(StringView input); |
154 | | DecoderResult decodeSigned(StringView input); |
155 | | DecoderResult decodeUnsigned(StringView input); |
156 | | DecoderResult decodeDouble(StringView input, size_t numChars); |
157 | | }; |
158 | | |
159 | | template <typename ALLOC> |
160 | | template <typename T> |
161 | | typename BasicJsonDecoder<ALLOC>::DecoderResult BasicJsonDecoder<ALLOC>::decodeLiteral( |
162 | | StringView input, StringView literal, T&& value) |
163 | 33 | { |
164 | 33 | StringView::const_iterator literalIt = literal.begin(); |
165 | 33 | StringView::const_iterator inputIt = input.begin(); |
166 | 174 | while (inputIt != input.end() && literalIt != literal.end()162 ) |
167 | 148 | { |
168 | 148 | if (*inputIt++ != *literalIt++) |
169 | 7 | { |
170 | | // failure, not decoded |
171 | 7 | return DecoderResult(static_cast<size_t>(inputIt - input.begin()), get_allocator()); |
172 | 7 | } |
173 | 148 | } |
174 | | |
175 | 26 | if (literalIt != literal.end()) |
176 | 6 | { |
177 | | // short input, not decoded |
178 | 6 | return DecoderResult(input.size(), get_allocator()); |
179 | 6 | } |
180 | | |
181 | | // success |
182 | 20 | return DecoderResult(literal.size(), std::forward<T>(value), get_allocator()); |
183 | 26 | } |
184 | | |
185 | | template <typename ALLOC> |
186 | | typename BasicJsonDecoder<ALLOC>::DecoderResult BasicJsonDecoder<ALLOC>::decodeString(StringView input) |
187 | 16.3k | { |
188 | 16.3k | StringView::const_iterator inputIt = input.begin() + 1; // we know that at the beginning is '"' |
189 | 16.3k | string<ALLOC> value(get_allocator()); |
190 | | |
191 | 83.1k | while (inputIt != input.end()) |
192 | 83.1k | { |
193 | 83.1k | if (*inputIt == '\\') |
194 | 24 | { |
195 | 24 | ++inputIt; |
196 | 24 | if (inputIt == input.end()) |
197 | 1 | { |
198 | | // wrong escape, not decoded |
199 | 1 | return DecoderResult(static_cast<size_t>(inputIt - input.begin()), get_allocator()); |
200 | 1 | } |
201 | | |
202 | 23 | char nextChar = *inputIt; |
203 | 23 | switch (nextChar) |
204 | 23 | { |
205 | 2 | case '\\': |
206 | 4 | case '"': |
207 | 4 | value.push_back(nextChar); |
208 | 4 | ++inputIt; |
209 | 4 | break; |
210 | 1 | case 'b': |
211 | 1 | value.push_back('\b'); |
212 | 1 | ++inputIt; |
213 | 1 | break; |
214 | 1 | case 'f': |
215 | 1 | value.push_back('\f'); |
216 | 1 | ++inputIt; |
217 | 1 | break; |
218 | 2 | case 'n': |
219 | 2 | value.push_back('\n'); |
220 | 2 | ++inputIt; |
221 | 2 | break; |
222 | 1 | case 'r': |
223 | 1 | value.push_back('\r'); |
224 | 1 | ++inputIt; |
225 | 1 | break; |
226 | 2 | case 't': |
227 | 2 | value.push_back('\t'); |
228 | 2 | ++inputIt; |
229 | 2 | break; |
230 | 11 | case 'u': // unicode escape |
231 | 11 | { |
232 | 11 | ++inputIt; |
233 | 11 | if (!decodeUnicodeEscape(input, inputIt, value)) |
234 | 10 | { |
235 | | // unsupported unicode escape, not decoded |
236 | 10 | return DecoderResult(static_cast<size_t>(inputIt - input.begin()), get_allocator()); |
237 | 10 | } |
238 | 1 | break; |
239 | 11 | } |
240 | 1 | default: |
241 | 1 | ++inputIt; |
242 | | // unknown escape, not decoded |
243 | 1 | return DecoderResult(static_cast<size_t>(inputIt - input.begin()), get_allocator()); |
244 | 23 | } |
245 | 23 | } |
246 | 83.0k | else if (*inputIt == '"') |
247 | 16.3k | { |
248 | 16.3k | ++inputIt; |
249 | | // successfully decoded |
250 | 16.3k | return DecoderResult( |
251 | 16.3k | static_cast<size_t>(inputIt - input.begin()), std::move(value), get_allocator()); |
252 | 16.3k | } |
253 | 66.7k | else |
254 | 66.7k | { |
255 | 66.7k | value.push_back(*inputIt++); |
256 | 66.7k | } |
257 | 83.1k | } |
258 | | |
259 | | // unterminated string, not decoded |
260 | 2 | return DecoderResult(input.size(), get_allocator()); |
261 | 16.3k | } |
262 | | |
263 | | template <typename ALLOC> |
264 | | bool BasicJsonDecoder<ALLOC>::decodeUnicodeEscape( |
265 | | StringView input, StringView::const_iterator& inputIt, string<ALLOC>& value) |
266 | 11 | { |
267 | | // TODO[Mi-L@]: Simplified just to decode what zserio encodes, for complex solution we could use |
268 | | // std::wstring_convert but it's deprecated in C++17. |
269 | 11 | if (inputIt == input.end() || *inputIt++ != '0'10 ) |
270 | 2 | { |
271 | 2 | return false; |
272 | 2 | } |
273 | 9 | if (inputIt == input.end() || *inputIt++ != '0'8 ) |
274 | 2 | { |
275 | 2 | return false; |
276 | 2 | } |
277 | | |
278 | 7 | if (inputIt == input.end()) |
279 | 1 | { |
280 | 1 | return false; |
281 | 1 | } |
282 | | |
283 | 6 | const int32_t hex1 = decodeHex(*inputIt++); |
284 | 6 | if (hex1 < 0) |
285 | 2 | { |
286 | 2 | return false; |
287 | 2 | } |
288 | | |
289 | 4 | if (inputIt == input.end()) |
290 | 1 | { |
291 | 1 | return false; |
292 | 1 | } |
293 | | |
294 | 3 | const int32_t hex2 = decodeHex(*inputIt++); |
295 | 3 | if (hex2 < 0) |
296 | 2 | { |
297 | 2 | return false; |
298 | 2 | } |
299 | | |
300 | 1 | const uint32_t characterInt = (static_cast<uint32_t>(hex1) << 4U) | static_cast<uint32_t>(hex2); |
301 | 1 | using char_traits = std::char_traits<char>; |
302 | 1 | const char character = char_traits::to_char_type(static_cast<char_traits::int_type>(characterInt)); |
303 | 1 | value.push_back(character); |
304 | | |
305 | 1 | return true; |
306 | 3 | } |
307 | | |
308 | | template <typename ALLOC> |
309 | | int32_t BasicJsonDecoder<ALLOC>::decodeHex(char character) |
310 | 9 | { |
311 | 9 | if (character >= '0' && character <= '9'8 ) |
312 | 3 | { |
313 | 3 | return static_cast<int32_t>(character - '0'); |
314 | 3 | } |
315 | 6 | else if (character >= 'a' && character <= 'f'2 ) |
316 | 1 | { |
317 | 1 | return static_cast<int32_t>(character - 'a' + 10); |
318 | 1 | } |
319 | 5 | else if (character >= 'A' && character <= 'F'4 ) |
320 | 1 | { |
321 | 1 | return static_cast<int32_t>(character - 'A' + 10); |
322 | 1 | } |
323 | | |
324 | 4 | return -1; |
325 | 9 | } |
326 | | |
327 | | template <typename ALLOC> |
328 | | size_t BasicJsonDecoder<ALLOC>::checkNumber(StringView input, bool& isDouble, bool& isSigned) |
329 | 8.15k | { |
330 | 8.15k | StringView::const_iterator inputIt = input.begin(); |
331 | 8.15k | bool acceptExpSign = false; |
332 | 8.15k | bool isScientificDouble = false; |
333 | 8.15k | isDouble = false; |
334 | | |
335 | 8.15k | if (*inputIt == '-') // we know that at the beginning is at least one character |
336 | 19 | { |
337 | 19 | ++inputIt; |
338 | 19 | isSigned = true; |
339 | 19 | } |
340 | 8.13k | else |
341 | 8.13k | { |
342 | 8.13k | isSigned = false; |
343 | 8.13k | } |
344 | | |
345 | 56.6k | while (inputIt != input.end()) |
346 | 56.6k | { |
347 | 56.6k | if (acceptExpSign) |
348 | 4.01k | { |
349 | 4.01k | acceptExpSign = false; |
350 | 4.01k | if (*inputIt == '+' || *inputIt == '-'4.01k ) |
351 | 16 | { |
352 | 16 | ++inputIt; |
353 | 16 | continue; |
354 | 16 | } |
355 | 4.01k | } |
356 | | |
357 | 56.5k | if (*inputIt >= '0' && *inputIt <= '9'48.4k ) |
358 | 44.4k | { |
359 | 44.4k | ++inputIt; |
360 | 44.4k | continue; |
361 | 44.4k | } |
362 | | |
363 | 12.1k | if ((*inputIt == 'e' || *inputIt == 'E'8.15k ) && !isScientificDouble4.02k ) |
364 | 4.02k | { |
365 | 4.02k | isDouble = true; |
366 | 4.02k | isScientificDouble = true; |
367 | 4.02k | acceptExpSign = true; |
368 | 4.02k | ++inputIt; |
369 | 4.02k | continue; |
370 | 4.02k | } |
371 | | |
372 | 8.13k | if (*inputIt == '.' && !isDouble14 ) |
373 | 13 | { |
374 | 13 | isDouble = true; |
375 | 13 | ++inputIt; |
376 | 13 | continue; |
377 | 13 | } |
378 | | |
379 | 8.12k | break; // end of a number |
380 | 8.13k | } |
381 | | |
382 | 8.15k | const size_t numberLen = static_cast<size_t>(inputIt - input.begin()); |
383 | 8.15k | if (isSigned && numberLen == 119 ) |
384 | 3 | { |
385 | 3 | return 0; // single minus is not a number |
386 | 3 | } |
387 | | |
388 | 8.15k | return numberLen; |
389 | 8.15k | } |
390 | | |
391 | | template <typename ALLOC> |
392 | | typename BasicJsonDecoder<ALLOC>::DecoderResult BasicJsonDecoder<ALLOC>::decodeNumber(StringView input) |
393 | 8.15k | { |
394 | 8.15k | bool isDouble = false; |
395 | 8.15k | bool isSigned = false; |
396 | 8.15k | const size_t numChars = checkNumber(input, isDouble, isSigned); |
397 | 8.15k | if (numChars == 0) |
398 | 7 | { |
399 | 7 | return DecoderResult(1, get_allocator()); |
400 | 7 | } |
401 | | |
402 | | // for decodeSigned and decodeUnsigned, we know that all numChars will be processed because checkNumber |
403 | | // already checked this |
404 | 8.14k | if (isDouble) |
405 | 4.03k | { |
406 | 4.03k | return decodeDouble(input, numChars); |
407 | 4.03k | } |
408 | 4.11k | else if (isSigned) |
409 | 9 | { |
410 | 9 | return decodeSigned(input); |
411 | 9 | } |
412 | 4.11k | else |
413 | 4.11k | { |
414 | 4.11k | return decodeUnsigned(input); |
415 | 4.11k | } |
416 | 8.14k | } |
417 | | |
418 | | template <typename ALLOC> |
419 | | typename BasicJsonDecoder<ALLOC>::DecoderResult BasicJsonDecoder<ALLOC>::decodeSigned(StringView input) |
420 | 9 | { |
421 | 9 | char* pEnd = nullptr; |
422 | 9 | errno = 0; // no library function sets its value back to zero once changed |
423 | 9 | const int64_t value = std::strtoll(input.begin(), &pEnd, 10); |
424 | | |
425 | 9 | const bool overflow = (errno == ERANGE); |
426 | | |
427 | 9 | return DecoderResult(static_cast<size_t>(pEnd - input.begin()), value, overflow, get_allocator()); |
428 | 9 | } |
429 | | |
430 | | template <typename ALLOC> |
431 | | typename BasicJsonDecoder<ALLOC>::DecoderResult BasicJsonDecoder<ALLOC>::decodeUnsigned(StringView input) |
432 | 4.11k | { |
433 | 4.11k | char* pEnd = nullptr; |
434 | 4.11k | errno = 0; // no library function sets its value back to zero once changed |
435 | 4.11k | const uint64_t value = std::strtoull(input.begin(), &pEnd, 10); |
436 | | |
437 | 4.11k | const bool overflow = (errno == ERANGE); |
438 | | |
439 | 4.11k | return DecoderResult(static_cast<size_t>(pEnd - input.begin()), value, overflow, get_allocator()); |
440 | 4.11k | } |
441 | | |
442 | | template <typename ALLOC> |
443 | | typename BasicJsonDecoder<ALLOC>::DecoderResult BasicJsonDecoder<ALLOC>::decodeDouble( |
444 | | StringView input, size_t numChars) |
445 | 4.03k | { |
446 | 4.03k | char* pEnd = nullptr; |
447 | 4.03k | const double value = std::strtod(input.begin(), &pEnd); |
448 | 4.03k | if (static_cast<size_t>(pEnd - input.begin()) != numChars) |
449 | 8 | { |
450 | 8 | return DecoderResult(numChars, get_allocator()); |
451 | 8 | } |
452 | | |
453 | 4.02k | return DecoderResult(numChars, value, get_allocator()); |
454 | 4.03k | } |
455 | | |
456 | | } // namespace zserio |
457 | | |
458 | | #endif // ZSERIO_JSON_DECODER_H_INC |