blob: 4969d27278bd6d3798bbbb9f48498fa20817a98f [file] [log] [blame]
Ed Tanous57fce802019-05-21 13:00:34 -07001#include <nlohmann/json.hpp>
2
3#include <algorithm>
4
5namespace json_html_util
6{
7
8static constexpr uint8_t utf8Accept = 0;
9static constexpr uint8_t utf8Reject = 1;
10
11inline uint8_t decode(uint8_t& state, uint32_t& codePoint,
12 const uint8_t byte) noexcept
13{
14 // clang-format off
15 static const std::array<std::uint8_t, 400> utf8d =
16 {
17 {
18 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00..1F
19 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20..3F
20 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40..5F
21 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 60..7F
22 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 80..9F
23 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // A0..BF
24 8, 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // C0..DF
25 0xA, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // E0..EF
26 0xB, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // F0..FF
27 0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
28 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, // s1..s2
29 1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, // s3..s4
30 1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, // s5..s6
31 1, 3, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // s7..s8
32 }
33 };
34 // clang-format on
35
36 if (state > 0x8)
37 {
38 return state;
39 }
40
41 const uint8_t type = utf8d[byte];
42
43 codePoint = (state != utf8Accept)
44 ? (byte & 0x3fu) | (codePoint << 6)
45 : static_cast<uint32_t>(0xff >> type) & (byte);
46
47 state = utf8d[256u + state * 16u + type];
48 return state;
49}
50
51inline void dumpEscaped(std::string& out, const std::string& str)
52{
53 std::array<char, 512> stringBuffer{{}};
54 uint32_t codePoint = 0;
55 uint8_t state = utf8Accept;
56 std::size_t bytes = 0; // number of bytes written to string_buffer
57
58 // number of bytes written at the point of the last valid byte
Ed Tanous2c70f802020-09-28 14:29:23 -070059 std::size_t bytesAfterLastAccept = 0;
Ed Tanous57fce802019-05-21 13:00:34 -070060 std::size_t undumpedChars = 0;
61
62 for (std::size_t i = 0; i < str.size(); ++i)
63 {
64 const uint8_t byte = static_cast<uint8_t>(str[i]);
65
66 switch (decode(state, codePoint, byte))
67 {
68 case utf8Accept: // decode found a new code point
69 {
70 switch (codePoint)
71 {
72 case 0x08: // backspace
73 {
74 stringBuffer[bytes++] = '\\';
75 stringBuffer[bytes++] = 'b';
76 break;
77 }
78
79 case 0x09: // horizontal tab
80 {
81 stringBuffer[bytes++] = '\\';
82 stringBuffer[bytes++] = 't';
83 break;
84 }
85
86 case 0x0A: // newline
87 {
88 stringBuffer[bytes++] = '\\';
89 stringBuffer[bytes++] = 'n';
90 break;
91 }
92
93 case 0x0C: // formfeed
94 {
95 stringBuffer[bytes++] = '\\';
96 stringBuffer[bytes++] = 'f';
97 break;
98 }
99
100 case 0x0D: // carriage return
101 {
102 stringBuffer[bytes++] = '\\';
103 stringBuffer[bytes++] = 'r';
104 break;
105 }
106
107 case 0x22: // quotation mark
108 {
109 stringBuffer[bytes++] = '&';
110 stringBuffer[bytes++] = 'q';
111 stringBuffer[bytes++] = 'u';
112 stringBuffer[bytes++] = 'o';
113 stringBuffer[bytes++] = 't';
114 stringBuffer[bytes++] = ';';
115 break;
116 }
117
118 case 0x27: // apostrophe
119 {
120 stringBuffer[bytes++] = '&';
121 stringBuffer[bytes++] = 'a';
122 stringBuffer[bytes++] = 'p';
123 stringBuffer[bytes++] = 'o';
124 stringBuffer[bytes++] = 's';
125 stringBuffer[bytes++] = ';';
126 break;
127 }
128
129 case 0x26: // ampersand
130 {
131 stringBuffer[bytes++] = '&';
132 stringBuffer[bytes++] = 'a';
133 stringBuffer[bytes++] = 'm';
134 stringBuffer[bytes++] = 'p';
135 stringBuffer[bytes++] = ';';
136 break;
137 }
138
139 case 0x3C: // less than
140 {
141 stringBuffer[bytes++] = '\\';
142 stringBuffer[bytes++] = 'l';
143 stringBuffer[bytes++] = 't';
144 stringBuffer[bytes++] = ';';
145 break;
146 }
147
148 case 0x3E: // greater than
149 {
150 stringBuffer[bytes++] = '\\';
151 stringBuffer[bytes++] = 'g';
152 stringBuffer[bytes++] = 't';
153 stringBuffer[bytes++] = ';';
154 break;
155 }
156
157 default:
158 {
159 // escape control characters (0x00..0x1F)
160 if ((codePoint <= 0x1F) or (codePoint >= 0x7F))
161 {
162 if (codePoint <= 0xFFFF)
163 {
164 (std::snprintf)(
165 stringBuffer.data() + bytes, 7, "\\u%04x",
166 static_cast<uint16_t>(codePoint));
167 bytes += 6;
168 }
169 else
170 {
171 (std::snprintf)(
172 stringBuffer.data() + bytes, 13,
173 "\\u%04x\\u%04x",
174 static_cast<uint16_t>(0xD7C0 +
175 (codePoint >> 10)),
176 static_cast<uint16_t>(0xDC00 +
177 (codePoint & 0x3FF)));
178 bytes += 12;
179 }
180 }
181 else
182 {
183 // copy byte to buffer (all previous bytes
184 // been copied have in default case above)
185 stringBuffer[bytes++] = str[i];
186 }
187 break;
188 }
189 }
190
191 // write buffer and reset index; there must be 13 bytes
192 // left, as this is the maximal number of bytes to be
193 // written ("\uxxxx\uxxxx\0") for one code point
194 if (stringBuffer.size() - bytes < 13)
195 {
196 out.append(stringBuffer.data(), bytes);
197 bytes = 0;
198 }
199
200 // remember the byte position of this accept
Ed Tanous2c70f802020-09-28 14:29:23 -0700201 bytesAfterLastAccept = bytes;
Ed Tanous57fce802019-05-21 13:00:34 -0700202 undumpedChars = 0;
203 break;
204 }
205
206 case utf8Reject: // decode found invalid UTF-8 byte
207 {
208 // in case we saw this character the first time, we
209 // would like to read it again, because the byte
210 // may be OK for itself, but just not OK for the
211 // previous sequence
212 if (undumpedChars > 0)
213 {
214 --i;
215 }
216
217 // reset length buffer to the last accepted index;
218 // thus removing/ignoring the invalid characters
Ed Tanous2c70f802020-09-28 14:29:23 -0700219 bytes = bytesAfterLastAccept;
Ed Tanous57fce802019-05-21 13:00:34 -0700220
221 stringBuffer[bytes++] = '\\';
222 stringBuffer[bytes++] = 'u';
223 stringBuffer[bytes++] = 'f';
224 stringBuffer[bytes++] = 'f';
225 stringBuffer[bytes++] = 'f';
226 stringBuffer[bytes++] = 'd';
227
Ed Tanous2c70f802020-09-28 14:29:23 -0700228 bytesAfterLastAccept = bytes;
Ed Tanous57fce802019-05-21 13:00:34 -0700229
230 undumpedChars = 0;
231
232 // continue processing the string
233 state = utf8Accept;
234 break;
235
236 break;
237 }
238
239 default: // decode found yet incomplete multi-byte code point
240 {
241 ++undumpedChars;
242 break;
243 }
244 }
245 }
246
247 // we finished processing the string
248 if (state == utf8Accept)
249 {
250 // write buffer
251 if (bytes > 0)
252 {
253 out.append(stringBuffer.data(), bytes);
254 }
255 }
256 else
257 {
258 // write all accepted bytes
Ed Tanous2c70f802020-09-28 14:29:23 -0700259 out.append(stringBuffer.data(), bytesAfterLastAccept);
Ed Tanous57fce802019-05-21 13:00:34 -0700260 out += "\\ufffd";
261 }
262}
263
264inline unsigned int countDigits(uint64_t number) noexcept
265{
Ed Tanous2c70f802020-09-28 14:29:23 -0700266 unsigned int nDigits = 1;
Ed Tanous57fce802019-05-21 13:00:34 -0700267 for (;;)
268 {
269 if (number < 10)
270 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700271 return nDigits;
Ed Tanous57fce802019-05-21 13:00:34 -0700272 }
273 if (number < 100)
274 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700275 return nDigits + 1;
Ed Tanous57fce802019-05-21 13:00:34 -0700276 }
277 if (number < 1000)
278 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700279 return nDigits + 2;
Ed Tanous57fce802019-05-21 13:00:34 -0700280 }
281 if (number < 10000)
282 {
Ed Tanous2c70f802020-09-28 14:29:23 -0700283 return nDigits + 3;
Ed Tanous57fce802019-05-21 13:00:34 -0700284 }
285 number = number / 10000u;
Ed Tanous2c70f802020-09-28 14:29:23 -0700286 nDigits += 4;
Ed Tanous57fce802019-05-21 13:00:34 -0700287 }
288}
289
290template <typename NumberType,
291 std::enable_if_t<std::is_same<NumberType, uint64_t>::value or
292 std::is_same<NumberType, int64_t>::value,
293 int> = 0>
294void dumpInteger(std::string& out, NumberType number)
295{
296 std::array<char, 64> numberbuffer{{}};
297
Ed Tanous2c70f802020-09-28 14:29:23 -0700298 static constexpr std::array<std::array<char, 2>, 100> digitsTo99{{
Ed Tanous57fce802019-05-21 13:00:34 -0700299 {'0', '0'}, {'0', '1'}, {'0', '2'}, {'0', '3'}, {'0', '4'}, {'0', '5'},
300 {'0', '6'}, {'0', '7'}, {'0', '8'}, {'0', '9'}, {'1', '0'}, {'1', '1'},
301 {'1', '2'}, {'1', '3'}, {'1', '4'}, {'1', '5'}, {'1', '6'}, {'1', '7'},
302 {'1', '8'}, {'1', '9'}, {'2', '0'}, {'2', '1'}, {'2', '2'}, {'2', '3'},
303 {'2', '4'}, {'2', '5'}, {'2', '6'}, {'2', '7'}, {'2', '8'}, {'2', '9'},
304 {'3', '0'}, {'3', '1'}, {'3', '2'}, {'3', '3'}, {'3', '4'}, {'3', '5'},
305 {'3', '6'}, {'3', '7'}, {'3', '8'}, {'3', '9'}, {'4', '0'}, {'4', '1'},
306 {'4', '2'}, {'4', '3'}, {'4', '4'}, {'4', '5'}, {'4', '6'}, {'4', '7'},
307 {'4', '8'}, {'4', '9'}, {'5', '0'}, {'5', '1'}, {'5', '2'}, {'5', '3'},
308 {'5', '4'}, {'5', '5'}, {'5', '6'}, {'5', '7'}, {'5', '8'}, {'5', '9'},
309 {'6', '0'}, {'6', '1'}, {'6', '2'}, {'6', '3'}, {'6', '4'}, {'6', '5'},
310 {'6', '6'}, {'6', '7'}, {'6', '8'}, {'6', '9'}, {'7', '0'}, {'7', '1'},
311 {'7', '2'}, {'7', '3'}, {'7', '4'}, {'7', '5'}, {'7', '6'}, {'7', '7'},
312 {'7', '8'}, {'7', '9'}, {'8', '0'}, {'8', '1'}, {'8', '2'}, {'8', '3'},
313 {'8', '4'}, {'8', '5'}, {'8', '6'}, {'8', '7'}, {'8', '8'}, {'8', '9'},
314 {'9', '0'}, {'9', '1'}, {'9', '2'}, {'9', '3'}, {'9', '4'}, {'9', '5'},
315 {'9', '6'}, {'9', '7'}, {'9', '8'}, {'9', '9'},
316 }};
317
318 // special case for "0"
319 if (number == 0)
320 {
321 out += '0';
322 return;
323 }
324
325 // use a pointer to fill the buffer
326 auto bufferPtr = begin(numberbuffer);
327
328 const bool isNegative = std::is_same<NumberType, int64_t>::value &&
329 !(number >= 0); // see issue #755
Ed Tanous543f4402022-01-06 13:12:53 -0800330 uint64_t absValue = 0;
Ed Tanous57fce802019-05-21 13:00:34 -0700331
Ed Tanous543f4402022-01-06 13:12:53 -0800332 unsigned int nChars = 0;
Ed Tanous57fce802019-05-21 13:00:34 -0700333
334 if (isNegative)
335 {
336 *bufferPtr = '-';
337 absValue = static_cast<uint64_t>(0 - number);
338
339 // account one more byte for the minus sign
340 nChars = 1 + countDigits(absValue);
341 }
342 else
343 {
344 absValue = static_cast<uint64_t>(number);
345 nChars = countDigits(absValue);
346 }
347
348 // spare 1 byte for '\0'
349 if (nChars >= numberbuffer.size() - 1)
350 {
351 return;
352 }
353
354 // jump to the end to generate the string from backward
355 // so we later avoid reversing the result
Ed Tanousca45aa32022-01-07 09:28:45 -0800356 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
Ed Tanous57fce802019-05-21 13:00:34 -0700357 bufferPtr += nChars;
358
359 // Fast int2ascii implementation inspired by "Fastware" talk by Andrei
360 // Alexandrescu See: https://www.youtube.com/watch?v=o4-CwDo2zpg
361 while (absValue >= 100)
362 {
363 const auto digitsIndex = static_cast<unsigned>((absValue % 100));
364 absValue /= 100;
Ed Tanous2c70f802020-09-28 14:29:23 -0700365 *(--bufferPtr) = digitsTo99[digitsIndex][1];
366 *(--bufferPtr) = digitsTo99[digitsIndex][0];
Ed Tanous57fce802019-05-21 13:00:34 -0700367 }
368
369 if (absValue >= 10)
370 {
371 const auto digitsIndex = static_cast<unsigned>(absValue);
Ed Tanous2c70f802020-09-28 14:29:23 -0700372 *(--bufferPtr) = digitsTo99[digitsIndex][1];
373 *(--bufferPtr) = digitsTo99[digitsIndex][0];
Ed Tanous57fce802019-05-21 13:00:34 -0700374 }
375 else
376 {
377 *(--bufferPtr) = static_cast<char>('0' + absValue);
378 }
379
380 out.append(numberbuffer.data(), nChars);
381}
382
383inline void dumpfloat(std::string& out, double number,
384 std::true_type /*isIeeeSingleOrDouble*/)
385{
386 std::array<char, 64> numberbuffer{{}};
387 char* begin = numberbuffer.data();
Ed Tanousca45aa32022-01-07 09:28:45 -0800388
389 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
Ed Tanous57fce802019-05-21 13:00:34 -0700390 ::nlohmann::detail::to_chars(begin, begin + numberbuffer.size(), number);
391
392 out += begin;
393}
394
395inline void dumpfloat(std::string& out, double number,
396 std::false_type /*isIeeeSingleOrDouble*/)
397{
398 std::array<char, 64> numberbuffer{{}};
399 // get number of digits for a float -> text -> float round-trip
400 static constexpr auto d = std::numeric_limits<double>::max_digits10;
401
402 // the actual conversion
Ed Tanous3c2a8d02021-09-22 10:07:25 -0700403 std::ptrdiff_t len = std::snprintf(numberbuffer.data(), numberbuffer.size(),
404 "%.*g", d, number);
Ed Tanous57fce802019-05-21 13:00:34 -0700405
406 // negative value indicates an error
407 if (len <= 0)
408 {
409 return;
410 }
411
412 // check if buffer was large enough
413 if (numberbuffer.size() < static_cast<std::size_t>(len))
414 {
415 return;
416 }
417
418 const auto end =
419 std::remove(numberbuffer.begin(), numberbuffer.begin() + len, ',');
420 std::fill(end, numberbuffer.end(), '\0');
421
422 if ((end - numberbuffer.begin()) > len)
423 {
424 return;
425 }
426 len = (end - numberbuffer.begin());
427
428 out.append(numberbuffer.data(), static_cast<std::size_t>(len));
429
430 // determine if need to append ".0"
431 const bool valueIsIntLike =
432 std::none_of(numberbuffer.begin(), numberbuffer.begin() + len + 1,
433 [](char c) { return (c == '.' or c == 'e'); });
434
435 if (valueIsIntLike)
436 {
437 out += ".0";
438 }
439}
440
441inline void dumpfloat(std::string& out, double number)
442{
443 // NaN / inf
444 if (!std::isfinite(number))
445 {
446 out += "null";
447 return;
448 }
449
450 // If float is an IEEE-754 single or double precision number,
451 // use the Grisu2 algorithm to produce short numbers which are
452 // guaranteed to round-trip, using strtof and strtod, resp.
453 //
454 // NB: The test below works if <long double> == <double>.
455 static constexpr bool isIeeeSingleOrDouble =
456 (std::numeric_limits<double>::is_iec559 and
457 std::numeric_limits<double>::digits == 24 and
458 std::numeric_limits<double>::max_exponent == 128) or
459 (std::numeric_limits<double>::is_iec559 and
460 std::numeric_limits<double>::digits == 53 and
461 std::numeric_limits<double>::max_exponent == 1024);
462
463 dumpfloat(out, number,
464 std::integral_constant<bool, isIeeeSingleOrDouble>());
465}
466
467inline void dump(std::string& out, const nlohmann::json& val)
468{
469 switch (val.type())
470 {
471 case nlohmann::json::value_t::object:
472 {
473 if (val.empty())
474 {
475 out += "{}";
476 return;
477 }
478
479 out += "{";
480
481 out += "<div class=tab>";
482 for (auto i = val.begin(); i != val.end();)
483 {
484 out += "&quot";
485 dumpEscaped(out, i.key());
486 out += "&quot: ";
487
488 bool inATag = false;
489 if (i.key() == "@odata.id" || i.key() == "@odata.context" ||
490 i.key() == "Members@odata.nextLink" || i.key() == "Uri")
491 {
492 inATag = true;
493 out += "<a href=\"";
494 dumpEscaped(out, i.value());
495 out += "\">";
496 }
497 dump(out, i.value());
498 if (inATag)
499 {
500 out += "</a>";
501 }
502 i++;
503 if (i != val.end())
504 {
505 out += ",";
506 }
507 out += "<br>";
508 }
509 out += "</div>";
510 out += '}';
511
512 return;
513 }
514
515 case nlohmann::json::value_t::array:
516 {
517 if (val.empty())
518 {
519 out += "[]";
520 return;
521 }
522
523 out += "[";
524
525 out += "<div class=tab>";
526
527 // first n-1 elements
528 for (auto i = val.cbegin(); i != val.cend() - 1; ++i)
529 {
530 dump(out, *i);
531 out += ",<br>";
532 }
533
534 // last element
535 dump(out, val.back());
536
537 out += "</div>";
538 out += ']';
539
540 return;
541 }
542
543 case nlohmann::json::value_t::string:
544 {
545 out += '\"';
546 const std::string* ptr = val.get_ptr<const std::string*>();
547 dumpEscaped(out, *ptr);
548 out += '\"';
549 return;
550 }
551
552 case nlohmann::json::value_t::boolean:
553 {
554 if (*(val.get_ptr<const bool*>()))
555 {
556 out += "true";
557 }
558 else
559 {
560 out += "false";
561 }
562 return;
563 }
564
565 case nlohmann::json::value_t::number_integer:
566 {
567 dumpInteger(out, *(val.get_ptr<const int64_t*>()));
568 return;
569 }
570
571 case nlohmann::json::value_t::number_unsigned:
572 {
573 dumpInteger(out, *(val.get_ptr<const uint64_t*>()));
574 return;
575 }
576
577 case nlohmann::json::value_t::number_float:
578 {
579 dumpfloat(out, *(val.get_ptr<const double*>()));
580 return;
581 }
582
583 case nlohmann::json::value_t::discarded:
584 {
585 out += "<discarded>";
586 return;
587 }
588
589 case nlohmann::json::value_t::null:
590 {
591 out += "null";
592 return;
593 }
Ed Tanous3e4c7792020-09-25 08:12:41 -0700594 case nlohmann::json::value_t::binary:
595 {
596 // Do nothing; Should never happen.
597 return;
598 }
Ed Tanous57fce802019-05-21 13:00:34 -0700599 }
600}
601
602inline void dumpHtml(std::string& out, const nlohmann::json& json)
603{
604 out += "<html>\n"
605 "<head>\n"
606 "<title>Redfish API</title>\n"
607 "<link href=\"/redfish.css\" rel=\"stylesheet\">\n"
608 "</head>\n"
609 "<body>\n"
610 "<div class=\"container\">\n"
611 "<img src=\"/DMTF_Redfish_logo_2017.svg\" alt=\"redfish\" "
612 "height=\"406px\" "
613 "width=\"576px\">\n"
614 "<div class=\"content\">\n";
615 dump(out, json);
616 out += "</div>\n"
617 "</div>\n"
618 "</body>\n"
619 "</html>\n";
620}
621
622} // namespace json_html_util