buffer.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #ifndef NETDATA_WEB_BUFFER_H
  3. #define NETDATA_WEB_BUFFER_H 1
  4. #include "../string/utf8.h"
  5. #include "../libnetdata.h"
  6. #define WEB_DATA_LENGTH_INCREASE_STEP 1024
  7. #define BUFFER_JSON_MAX_DEPTH 32 // max is 255
  8. extern const char hex_digits[16];
  9. extern const char base64_digits[64];
  10. extern unsigned char hex_value_from_ascii[256];
  11. extern unsigned char base64_value_from_ascii[256];
  12. typedef enum __attribute__ ((__packed__)) {
  13. BUFFER_JSON_EMPTY = 0,
  14. BUFFER_JSON_OBJECT,
  15. BUFFER_JSON_ARRAY,
  16. } BUFFER_JSON_NODE_TYPE;
  17. typedef struct web_buffer_json_node {
  18. BUFFER_JSON_NODE_TYPE type;
  19. uint32_t count:24;
  20. } BUFFER_JSON_NODE;
  21. #define BUFFER_QUOTE_MAX_SIZE 7
  22. typedef enum __attribute__ ((__packed__)) {
  23. WB_CONTENT_CACHEABLE = (1 << 0),
  24. WB_CONTENT_NO_CACHEABLE = (1 << 1),
  25. } BUFFER_OPTIONS;
  26. typedef enum __attribute__ ((__packed__)) {
  27. CT_NONE = 0,
  28. CT_APPLICATION_JSON,
  29. CT_TEXT_PLAIN,
  30. CT_TEXT_HTML,
  31. CT_APPLICATION_X_JAVASCRIPT,
  32. CT_TEXT_CSS,
  33. CT_TEXT_XML,
  34. CT_APPLICATION_XML,
  35. CT_TEXT_XSL,
  36. CT_APPLICATION_OCTET_STREAM,
  37. CT_APPLICATION_X_FONT_TRUETYPE,
  38. CT_APPLICATION_X_FONT_OPENTYPE,
  39. CT_APPLICATION_FONT_WOFF,
  40. CT_APPLICATION_FONT_WOFF2,
  41. CT_APPLICATION_VND_MS_FONTOBJ,
  42. CT_IMAGE_SVG_XML,
  43. CT_IMAGE_PNG,
  44. CT_IMAGE_JPG,
  45. CT_IMAGE_GIF,
  46. CT_IMAGE_XICON,
  47. CT_IMAGE_ICNS,
  48. CT_IMAGE_BMP,
  49. CT_PROMETHEUS,
  50. } HTTP_CONTENT_TYPE;
  51. typedef struct web_buffer {
  52. size_t size; // allocation size of buffer, in bytes
  53. size_t len; // current data length in buffer, in bytes
  54. char *buffer; // the buffer itself
  55. HTTP_CONTENT_TYPE content_type; // the content type of the data in the buffer
  56. BUFFER_OPTIONS options; // options related to the content
  57. time_t date; // the timestamp this content has been generated
  58. time_t expires; // the timestamp this content expires
  59. size_t *statistics;
  60. struct {
  61. char key_quote[BUFFER_QUOTE_MAX_SIZE + 1];
  62. char value_quote[BUFFER_QUOTE_MAX_SIZE + 1];
  63. int8_t depth;
  64. bool minify;
  65. BUFFER_JSON_NODE stack[BUFFER_JSON_MAX_DEPTH];
  66. } json;
  67. } BUFFER;
  68. #define buffer_cacheable(wb) do { (wb)->options |= WB_CONTENT_CACHEABLE; if((wb)->options & WB_CONTENT_NO_CACHEABLE) (wb)->options &= ~WB_CONTENT_NO_CACHEABLE; } while(0)
  69. #define buffer_no_cacheable(wb) do { (wb)->options |= WB_CONTENT_NO_CACHEABLE; if((wb)->options & WB_CONTENT_CACHEABLE) (wb)->options &= ~WB_CONTENT_CACHEABLE; (wb)->expires = 0; } while(0)
  70. #define buffer_strlen(wb) ((wb)->len)
  71. const char *buffer_tostring(BUFFER *wb);
  72. #define BUFFER_OVERFLOW_EOF "EOF"
  73. #ifdef NETDATA_INTERNAL_CHECKS
  74. #define buffer_overflow_check(b) _buffer_overflow_check(b)
  75. #else
  76. #define buffer_overflow_check(b)
  77. #endif
  78. static inline void _buffer_overflow_check(BUFFER *b) {
  79. assert(b->len <= b->size &&
  80. "BUFFER: length is above buffer size.");
  81. assert(!(b->buffer && (b->buffer[b->size] != '\0' || strcmp(&b->buffer[b->size + 1], BUFFER_OVERFLOW_EOF) != 0)) &&
  82. "BUFFER: detected overflow.");
  83. }
  84. static inline void buffer_flush(BUFFER *wb) {
  85. wb->len = 0;
  86. wb->json.depth = 0;
  87. wb->json.stack[0].type = BUFFER_JSON_EMPTY;
  88. wb->json.stack[0].count = 0;
  89. if(wb->buffer)
  90. wb->buffer[0] = '\0';
  91. }
  92. void buffer_reset(BUFFER *wb);
  93. void buffer_date(BUFFER *wb, int year, int month, int day, int hours, int minutes, int seconds);
  94. void buffer_jsdate(BUFFER *wb, int year, int month, int day, int hours, int minutes, int seconds);
  95. BUFFER *buffer_create(size_t size, size_t *statistics);
  96. void buffer_free(BUFFER *b);
  97. void buffer_increase(BUFFER *b, size_t free_size_required);
  98. void buffer_snprintf(BUFFER *wb, size_t len, const char *fmt, ...) PRINTFLIKE(3, 4);
  99. void buffer_vsprintf(BUFFER *wb, const char *fmt, va_list args);
  100. void buffer_sprintf(BUFFER *wb, const char *fmt, ...) PRINTFLIKE(2,3);
  101. void buffer_strcat_htmlescape(BUFFER *wb, const char *txt);
  102. void buffer_char_replace(BUFFER *wb, char from, char to);
  103. void buffer_print_sn_flags(BUFFER *wb, SN_FLAGS flags, bool send_anomaly_bit);
  104. static inline void buffer_need_bytes(BUFFER *buffer, size_t needed_free_size) {
  105. if(unlikely(buffer->len + needed_free_size >= buffer->size))
  106. buffer_increase(buffer, needed_free_size + 1);
  107. }
  108. void buffer_json_initialize(BUFFER *wb, const char *key_quote, const char *value_quote, int depth,
  109. bool add_anonymous_object, bool minify);
  110. void buffer_json_finalize(BUFFER *wb);
  111. static inline void _buffer_json_depth_push(BUFFER *wb, BUFFER_JSON_NODE_TYPE type) {
  112. #ifdef NETDATA_INTERNAL_CHECKS
  113. assert(wb->json.depth <= BUFFER_JSON_MAX_DEPTH && "BUFFER JSON: max nesting reached");
  114. #endif
  115. wb->json.depth++;
  116. wb->json.stack[wb->json.depth].count = 0;
  117. wb->json.stack[wb->json.depth].type = type;
  118. }
  119. static inline void _buffer_json_depth_pop(BUFFER *wb) {
  120. wb->json.depth--;
  121. }
  122. static inline void buffer_fast_charcat(BUFFER *wb, const char c) {
  123. buffer_need_bytes(wb, 2);
  124. *(&wb->buffer[wb->len]) = c;
  125. wb->len += 1;
  126. wb->buffer[wb->len] = '\0';
  127. buffer_overflow_check(wb);
  128. }
  129. static inline void buffer_fast_rawcat(BUFFER *wb, const char *txt, size_t len) {
  130. if(unlikely(!txt || !*txt || !len)) return;
  131. buffer_need_bytes(wb, len + 1);
  132. const char *t = txt;
  133. const char *e = &txt[len];
  134. char *d = &wb->buffer[wb->len];
  135. while(t != e)
  136. *d++ = *t++;
  137. wb->len += len;
  138. wb->buffer[wb->len] = '\0';
  139. buffer_overflow_check(wb);
  140. }
  141. static inline void buffer_fast_strcat(BUFFER *wb, const char *txt, size_t len) {
  142. if(unlikely(!txt || !*txt || !len)) return;
  143. buffer_need_bytes(wb, len + 1);
  144. const char *t = txt;
  145. const char *e = &txt[len];
  146. char *d = &wb->buffer[wb->len];
  147. while(t != e
  148. #ifdef NETDATA_INTERNAL_CHECKS
  149. && *t
  150. #endif
  151. )
  152. *d++ = *t++;
  153. #ifdef NETDATA_INTERNAL_CHECKS
  154. assert(!(t != e && !*t) && "BUFFER: source string is shorter than the length given.");
  155. #endif
  156. wb->len += len;
  157. wb->buffer[wb->len] = '\0';
  158. buffer_overflow_check(wb);
  159. }
  160. static inline void buffer_strcat(BUFFER *wb, const char *txt) {
  161. if(unlikely(!txt || !*txt)) return;
  162. const char *t = txt;
  163. while(*t) {
  164. buffer_need_bytes(wb, 100);
  165. char *s = &wb->buffer[wb->len];
  166. char *d = s;
  167. const char *e = &wb->buffer[wb->size];
  168. while(*t && d < e)
  169. *d++ = *t++;
  170. wb->len += d - s;
  171. }
  172. buffer_need_bytes(wb, 1);
  173. wb->buffer[wb->len] = '\0';
  174. buffer_overflow_check(wb);
  175. }
  176. static inline void buffer_strncat(BUFFER *wb, const char *txt, size_t len) {
  177. if(unlikely(!txt || !*txt)) return;
  178. const char *t = txt;
  179. while(*t) {
  180. buffer_need_bytes(wb, len);
  181. char *s = &wb->buffer[wb->len];
  182. char *d = s;
  183. const char *e = &wb->buffer[wb->len + len];
  184. while(*t && d < e)
  185. *d++ = *t++;
  186. wb->len += d - s;
  187. }
  188. buffer_need_bytes(wb, 1);
  189. wb->buffer[wb->len] = '\0';
  190. buffer_overflow_check(wb);
  191. }
  192. static inline void buffer_json_strcat(BUFFER *wb, const char *txt) {
  193. if(unlikely(!txt || !*txt)) return;
  194. const unsigned char *t = (const unsigned char *)txt;
  195. while(*t) {
  196. buffer_need_bytes(wb, 110);
  197. unsigned char *s = (unsigned char *)&wb->buffer[wb->len];
  198. unsigned char *d = s;
  199. const unsigned char *e = (unsigned char *)&wb->buffer[wb->size - 10]; // make room for the max escape sequence
  200. while(*t && d < e) {
  201. #ifdef BUFFER_JSON_ESCAPE_UTF
  202. if(unlikely(IS_UTF8_STARTBYTE(*t) && IS_UTF8_BYTE(t[1]))) {
  203. // UTF-8 multi-byte encoded character
  204. // find how big this character is (2-4 bytes)
  205. size_t utf_character_size = 2;
  206. while(utf_character_size < 4 && t[utf_character_size] && IS_UTF8_BYTE(t[utf_character_size]) && !IS_UTF8_STARTBYTE(t[utf_character_size]))
  207. utf_character_size++;
  208. uint32_t code_point = 0;
  209. for (size_t i = 0; i < utf_character_size; i++) {
  210. code_point <<= 6;
  211. code_point |= (t[i] & 0x3F);
  212. }
  213. t += utf_character_size;
  214. // encode as \u escape sequence
  215. *d++ = '\\';
  216. *d++ = 'u';
  217. *d++ = hex_digits[(code_point >> 12) & 0xf];
  218. *d++ = hex_digits[(code_point >> 8) & 0xf];
  219. *d++ = hex_digits[(code_point >> 4) & 0xf];
  220. *d++ = hex_digits[code_point & 0xf];
  221. }
  222. else
  223. #endif
  224. if(unlikely(*t < ' ')) {
  225. uint32_t v = *t++;
  226. *d++ = '\\';
  227. *d++ = 'u';
  228. *d++ = hex_digits[(v >> 12) & 0xf];
  229. *d++ = hex_digits[(v >> 8) & 0xf];
  230. *d++ = hex_digits[(v >> 4) & 0xf];
  231. *d++ = hex_digits[v & 0xf];
  232. }
  233. else {
  234. if (unlikely(*t == '\\' || *t == '\"'))
  235. *d++ = '\\';
  236. *d++ = *t++;
  237. }
  238. }
  239. wb->len += d - s;
  240. }
  241. buffer_need_bytes(wb, 1);
  242. wb->buffer[wb->len] = '\0';
  243. buffer_overflow_check(wb);
  244. }
  245. static inline void buffer_json_quoted_strcat(BUFFER *wb, const char *txt) {
  246. if(unlikely(!txt || !*txt)) return;
  247. if(*txt == '"')
  248. txt++;
  249. const char *t = txt;
  250. while(*t) {
  251. buffer_need_bytes(wb, 100);
  252. char *s = &wb->buffer[wb->len];
  253. char *d = s;
  254. const char *e = &wb->buffer[wb->size - 1]; // remove 1 to make room for the escape character
  255. while(*t && d < e) {
  256. if(unlikely(*t == '"' && !t[1])) {
  257. t++;
  258. continue;
  259. }
  260. if(unlikely(*t == '\\' || *t == '"'))
  261. *d++ = '\\';
  262. *d++ = *t++;
  263. }
  264. wb->len += d - s;
  265. }
  266. buffer_need_bytes(wb, 1);
  267. wb->buffer[wb->len] = '\0';
  268. buffer_overflow_check(wb);
  269. }
  270. // This trick seems to give an 80% speed increase in 32bit systems
  271. // print_number_llu_r() will just print the digits up to the
  272. // point the remaining value fits in 32 bits, and then calls
  273. // print_number_lu_r() to print the rest with 32 bit arithmetic.
  274. static inline char *print_uint32_reversed(char *dst, uint32_t value) {
  275. char *d = dst;
  276. do *d++ = (char)('0' + (value % 10)); while((value /= 10));
  277. return d;
  278. }
  279. static inline char *print_uint64_reversed(char *dst, uint64_t value) {
  280. #ifdef ENV32BIT
  281. if(value <= (uint64_t)0xffffffff)
  282. return print_uint32_reversed(dst, value);
  283. char *d = dst;
  284. do *d++ = (char)('0' + (value % 10)); while((value /= 10) && value > (uint64_t)0xffffffff);
  285. if(value) return print_uint32_reversed(d, value);
  286. return d;
  287. #else
  288. char *d = dst;
  289. do *d++ = (char)('0' + (value % 10)); while((value /= 10));
  290. return d;
  291. #endif
  292. }
  293. static inline char *print_uint32_hex_reversed(char *dst, uint32_t value) {
  294. static const char *digits = "0123456789ABCDEF";
  295. char *d = dst;
  296. do *d++ = digits[value & 0xf]; while((value >>= 4));
  297. return d;
  298. }
  299. static inline char *print_uint64_hex_reversed(char *dst, uint64_t value) {
  300. #ifdef ENV32BIT
  301. if(value <= (uint64_t)0xffffffff)
  302. return print_uint32_hex_reversed(dst, value);
  303. char *d = dst;
  304. do *d++ = hex_digits[value & 0xf]; while((value >>= 4) && value > (uint64_t)0xffffffff);
  305. if(value) return print_uint32_hex_reversed(d, value);
  306. return d;
  307. #else
  308. char *d = dst;
  309. do *d++ = hex_digits[value & 0xf]; while((value >>= 4));
  310. return d;
  311. #endif
  312. }
  313. static inline char *print_uint64_base64_reversed(char *dst, uint64_t value) {
  314. char *d = dst;
  315. do *d++ = base64_digits[value & 63]; while ((value >>= 6));
  316. return d;
  317. }
  318. static inline void char_array_reverse(char *from, char *to) {
  319. // from and to are inclusive
  320. char *begin = from, *end = to, aux;
  321. while (end > begin) aux = *end, *end-- = *begin, *begin++ = aux;
  322. }
  323. static inline int print_netdata_double(char *dst, NETDATA_DOUBLE value) {
  324. char *s = dst;
  325. if(unlikely(value < 0)) {
  326. *s++ = '-';
  327. value = fabsndd(value);
  328. }
  329. uint64_t fractional_precision = 10000000ULL; // fractional part 7 digits
  330. int fractional_wanted_digits = 7;
  331. int exponent = 0;
  332. if(unlikely(value >= (NETDATA_DOUBLE)(UINT64_MAX / 10))) {
  333. // the number is too big to print using 64bit numbers
  334. // so, let's convert it to exponential notation
  335. exponent = (int)(floorndd(log10ndd(value)));
  336. value /= powndd(10, exponent);
  337. // the max precision we can support is 18 digits
  338. // (UINT64_MAX is 20, but the first is 1)
  339. fractional_precision = 1000000000000000000ULL; // fractional part 18 digits
  340. fractional_wanted_digits = 18;
  341. }
  342. char *d = s;
  343. NETDATA_DOUBLE integral_d, fractional_d;
  344. fractional_d = modfndd(value, &integral_d);
  345. // get the integral and the fractional parts as 64-bit integers
  346. uint64_t integral = (uint64_t)integral_d;
  347. uint64_t fractional = (uint64_t)llrintndd(fractional_d * (NETDATA_DOUBLE)fractional_precision);
  348. if(unlikely(fractional >= fractional_precision)) {
  349. integral++;
  350. fractional -= fractional_precision;
  351. }
  352. // convert the integral part to string (reversed)
  353. d = print_uint64_reversed(d, integral);
  354. char_array_reverse(s, d - 1); // copy reversed the integral string
  355. if(likely(fractional != 0)) {
  356. *d++ = '.'; // add the dot
  357. // convert the fractional part to string (reversed)
  358. d = print_uint64_reversed(s = d, fractional);
  359. while(d - s < fractional_wanted_digits) *d++ = '0'; // prepend zeros to reach precision
  360. char_array_reverse(s, d - 1); // copy reversed the fractional string
  361. // remove trailing zeros from the fractional part
  362. while(*(d - 1) == '0') d--;
  363. }
  364. if(unlikely(exponent != 0)) {
  365. *d++ = 'e';
  366. *d++ = '+';
  367. d = print_uint32_reversed(s = d, exponent);
  368. char_array_reverse(s, d - 1);
  369. }
  370. *d = '\0';
  371. return (int)(d - dst);
  372. }
  373. static inline void buffer_print_uint64(BUFFER *wb, uint64_t value) {
  374. buffer_need_bytes(wb, 50);
  375. char *s = &wb->buffer[wb->len];
  376. char *d = print_uint64_reversed(s, value);
  377. char_array_reverse(s, d - 1);
  378. *d = '\0';
  379. wb->len += d - s;
  380. buffer_overflow_check(wb);
  381. }
  382. static inline void buffer_print_int64(BUFFER *wb, int64_t value) {
  383. buffer_need_bytes(wb, 50);
  384. if(value < 0) {
  385. buffer_fast_strcat(wb, "-", 1);
  386. value = -value;
  387. }
  388. buffer_print_uint64(wb, (uint64_t)value);
  389. buffer_overflow_check(wb);
  390. }
  391. static inline void buffer_print_uint64_hex(BUFFER *wb, uint64_t value) {
  392. buffer_need_bytes(wb, sizeof(uint64_t) * 2 + 2 + 1);
  393. buffer_fast_strcat(wb, HEX_PREFIX, sizeof(HEX_PREFIX) - 1);
  394. char *s = &wb->buffer[wb->len];
  395. char *d = print_uint64_hex_reversed(s, value);
  396. char_array_reverse(s, d - 1);
  397. *d = '\0';
  398. wb->len += d - s;
  399. buffer_overflow_check(wb);
  400. }
  401. static inline void buffer_print_uint64_base64(BUFFER *wb, uint64_t value) {
  402. buffer_need_bytes(wb, sizeof(uint64_t) * 2 + 2 + 1);
  403. buffer_fast_strcat(wb, IEEE754_UINT64_B64_PREFIX, sizeof(IEEE754_UINT64_B64_PREFIX) - 1);
  404. char *s = &wb->buffer[wb->len];
  405. char *d = print_uint64_base64_reversed(s, value);
  406. char_array_reverse(s, d - 1);
  407. *d = '\0';
  408. wb->len += d - s;
  409. buffer_overflow_check(wb);
  410. }
  411. static inline void buffer_print_int64_hex(BUFFER *wb, int64_t value) {
  412. buffer_need_bytes(wb, 2);
  413. if(value < 0) {
  414. buffer_fast_strcat(wb, "-", 1);
  415. value = -value;
  416. }
  417. buffer_print_uint64_hex(wb, (uint64_t)value);
  418. buffer_overflow_check(wb);
  419. }
  420. static inline void buffer_print_int64_base64(BUFFER *wb, int64_t value) {
  421. buffer_need_bytes(wb, 2);
  422. if(value < 0) {
  423. buffer_fast_strcat(wb, "-", 1);
  424. value = -value;
  425. }
  426. buffer_print_uint64_base64(wb, (uint64_t)value);
  427. buffer_overflow_check(wb);
  428. }
  429. static inline void buffer_print_netdata_double(BUFFER *wb, NETDATA_DOUBLE value) {
  430. buffer_need_bytes(wb, 512 + 2);
  431. if(isnan(value) || isinf(value)) {
  432. buffer_fast_strcat(wb, "null", 4);
  433. return;
  434. }
  435. else
  436. wb->len += print_netdata_double(&wb->buffer[wb->len], value);
  437. // terminate it
  438. buffer_need_bytes(wb, 1);
  439. wb->buffer[wb->len] = '\0';
  440. buffer_overflow_check(wb);
  441. }
  442. static inline void buffer_print_netdata_double_hex(BUFFER *wb, NETDATA_DOUBLE value) {
  443. buffer_need_bytes(wb, sizeof(uint64_t) * 2 + 2 + 1 + 1);
  444. uint64_t *ptr = (uint64_t *) (&value);
  445. buffer_fast_strcat(wb, IEEE754_DOUBLE_HEX_PREFIX, sizeof(IEEE754_DOUBLE_HEX_PREFIX) - 1);
  446. char *s = &wb->buffer[wb->len];
  447. char *d = print_uint64_hex_reversed(s, *ptr);
  448. char_array_reverse(s, d - 1);
  449. *d = '\0';
  450. wb->len += d - s;
  451. buffer_overflow_check(wb);
  452. }
  453. static inline void buffer_print_netdata_double_base64(BUFFER *wb, NETDATA_DOUBLE value) {
  454. buffer_need_bytes(wb, sizeof(uint64_t) * 2 + 2 + 1 + 1);
  455. uint64_t *ptr = (uint64_t *) (&value);
  456. buffer_fast_strcat(wb, IEEE754_DOUBLE_B64_PREFIX, sizeof(IEEE754_DOUBLE_B64_PREFIX) - 1);
  457. char *s = &wb->buffer[wb->len];
  458. char *d = print_uint64_base64_reversed(s, *ptr);
  459. char_array_reverse(s, d - 1);
  460. *d = '\0';
  461. wb->len += d - s;
  462. buffer_overflow_check(wb);
  463. }
  464. typedef enum {
  465. NUMBER_ENCODING_DECIMAL,
  466. NUMBER_ENCODING_HEX,
  467. NUMBER_ENCODING_BASE64,
  468. } NUMBER_ENCODING;
  469. static inline void buffer_print_int64_encoded(BUFFER *wb, NUMBER_ENCODING encoding, int64_t value) {
  470. if(encoding == NUMBER_ENCODING_BASE64)
  471. return buffer_print_int64_base64(wb, value);
  472. if(encoding == NUMBER_ENCODING_HEX)
  473. return buffer_print_int64_hex(wb, value);
  474. return buffer_print_int64(wb, value);
  475. }
  476. static inline void buffer_print_uint64_encoded(BUFFER *wb, NUMBER_ENCODING encoding, uint64_t value) {
  477. if(encoding == NUMBER_ENCODING_BASE64)
  478. return buffer_print_uint64_base64(wb, value);
  479. if(encoding == NUMBER_ENCODING_HEX)
  480. return buffer_print_uint64_hex(wb, value);
  481. return buffer_print_uint64(wb, value);
  482. }
  483. static inline void buffer_print_netdata_double_encoded(BUFFER *wb, NUMBER_ENCODING encoding, NETDATA_DOUBLE value) {
  484. if(encoding == NUMBER_ENCODING_BASE64)
  485. return buffer_print_netdata_double_base64(wb, value);
  486. if(encoding == NUMBER_ENCODING_HEX)
  487. return buffer_print_netdata_double_hex(wb, value);
  488. return buffer_print_netdata_double(wb, value);
  489. }
  490. static inline void buffer_print_spaces(BUFFER *wb, size_t spaces) {
  491. buffer_need_bytes(wb, spaces * 4 + 1);
  492. char *d = &wb->buffer[wb->len];
  493. for(size_t i = 0; i < spaces; i++) {
  494. *d++ = ' ';
  495. *d++ = ' ';
  496. *d++ = ' ';
  497. *d++ = ' ';
  498. }
  499. *d = '\0';
  500. wb->len += spaces * 4;
  501. buffer_overflow_check(wb);
  502. }
  503. static inline void buffer_print_json_comma_newline_spacing(BUFFER *wb) {
  504. if(wb->json.stack[wb->json.depth].count)
  505. buffer_fast_strcat(wb, ",", 1);
  506. if(wb->json.minify)
  507. return;
  508. buffer_fast_strcat(wb, "\n", 1);
  509. buffer_print_spaces(wb, wb->json.depth + 1);
  510. }
  511. static inline void buffer_print_json_key(BUFFER *wb, const char *key) {
  512. buffer_strcat(wb, wb->json.key_quote);
  513. buffer_json_strcat(wb, key);
  514. buffer_strcat(wb, wb->json.key_quote);
  515. }
  516. static inline void buffer_json_add_string_value(BUFFER *wb, const char *value) {
  517. if(value) {
  518. buffer_strcat(wb, wb->json.value_quote);
  519. buffer_json_strcat(wb, value);
  520. buffer_strcat(wb, wb->json.value_quote);
  521. }
  522. else
  523. buffer_fast_strcat(wb, "null", 4);
  524. }
  525. static inline void buffer_json_add_quoted_string_value(BUFFER *wb, const char *value) {
  526. if(value) {
  527. buffer_strcat(wb, wb->json.value_quote);
  528. buffer_json_quoted_strcat(wb, value);
  529. buffer_strcat(wb, wb->json.value_quote);
  530. }
  531. else
  532. buffer_fast_strcat(wb, "null", 4);
  533. }
  534. static inline void buffer_json_member_add_object(BUFFER *wb, const char *key) {
  535. buffer_print_json_comma_newline_spacing(wb);
  536. buffer_print_json_key(wb, key);
  537. buffer_fast_strcat(wb, ":{", 2);
  538. wb->json.stack[wb->json.depth].count++;
  539. _buffer_json_depth_push(wb, BUFFER_JSON_OBJECT);
  540. }
  541. static inline void buffer_json_object_close(BUFFER *wb) {
  542. #ifdef NETDATA_INTERNAL_CHECKS
  543. assert(wb->json.depth >= 0 && "BUFFER JSON: nothing is open to close it");
  544. assert(wb->json.stack[wb->json.depth].type == BUFFER_JSON_OBJECT && "BUFFER JSON: an object is not open to close it");
  545. #endif
  546. if(!wb->json.minify) {
  547. buffer_fast_strcat(wb, "\n", 1);
  548. buffer_print_spaces(wb, wb->json.depth);
  549. }
  550. buffer_fast_strcat(wb, "}", 1);
  551. _buffer_json_depth_pop(wb);
  552. }
  553. static inline void buffer_json_member_add_string(BUFFER *wb, const char *key, const char *value) {
  554. buffer_print_json_comma_newline_spacing(wb);
  555. buffer_print_json_key(wb, key);
  556. buffer_fast_strcat(wb, ":", 1);
  557. buffer_json_add_string_value(wb, value);
  558. wb->json.stack[wb->json.depth].count++;
  559. }
  560. static inline void buffer_json_member_add_string_or_omit(BUFFER *wb, const char *key, const char *value) {
  561. if(value && *value)
  562. buffer_json_member_add_string(wb, key, value);
  563. }
  564. static inline void buffer_json_member_add_string_or_empty(BUFFER *wb, const char *key, const char *value) {
  565. if(!value)
  566. value = "";
  567. buffer_json_member_add_string(wb, key, value);
  568. }
  569. static inline void buffer_json_member_add_quoted_string(BUFFER *wb, const char *key, const char *value) {
  570. buffer_print_json_comma_newline_spacing(wb);
  571. buffer_print_json_key(wb, key);
  572. buffer_fast_strcat(wb, ":", 1);
  573. if(!value || strcmp(value, "null") == 0)
  574. buffer_fast_strcat(wb, "null", 4);
  575. else
  576. buffer_json_add_quoted_string_value(wb, value);
  577. wb->json.stack[wb->json.depth].count++;
  578. }
  579. static inline void buffer_json_member_add_uuid(BUFFER *wb, const char *key, uuid_t *value) {
  580. buffer_print_json_comma_newline_spacing(wb);
  581. buffer_print_json_key(wb, key);
  582. buffer_fast_strcat(wb, ":", 1);
  583. if(value) {
  584. char uuid[GUID_LEN + 1];
  585. uuid_unparse_lower(*value, uuid);
  586. buffer_json_add_string_value(wb, uuid);
  587. }
  588. else
  589. buffer_json_add_string_value(wb, NULL);
  590. wb->json.stack[wb->json.depth].count++;
  591. }
  592. static inline void buffer_json_member_add_boolean(BUFFER *wb, const char *key, bool value) {
  593. buffer_print_json_comma_newline_spacing(wb);
  594. buffer_print_json_key(wb, key);
  595. buffer_fast_strcat(wb, ":", 1);
  596. buffer_strcat(wb, value?"true":"false");
  597. wb->json.stack[wb->json.depth].count++;
  598. }
  599. static inline void buffer_json_member_add_array(BUFFER *wb, const char *key) {
  600. buffer_print_json_comma_newline_spacing(wb);
  601. buffer_print_json_key(wb, key);
  602. buffer_fast_strcat(wb, ":[", 2);
  603. wb->json.stack[wb->json.depth].count++;
  604. _buffer_json_depth_push(wb, BUFFER_JSON_ARRAY);
  605. }
  606. static inline void buffer_json_add_array_item_array(BUFFER *wb) {
  607. buffer_print_json_comma_newline_spacing(wb);
  608. buffer_fast_strcat(wb, "[", 1);
  609. wb->json.stack[wb->json.depth].count++;
  610. _buffer_json_depth_push(wb, BUFFER_JSON_ARRAY);
  611. }
  612. static inline void buffer_json_add_array_item_string(BUFFER *wb, const char *value) {
  613. if(wb->json.stack[wb->json.depth].count)
  614. buffer_fast_strcat(wb, ",", 1);
  615. buffer_json_add_string_value(wb, value);
  616. wb->json.stack[wb->json.depth].count++;
  617. }
  618. static inline void buffer_json_add_array_item_double(BUFFER *wb, NETDATA_DOUBLE value) {
  619. if(wb->json.stack[wb->json.depth].count)
  620. buffer_fast_strcat(wb, ",", 1);
  621. buffer_print_netdata_double(wb, value);
  622. wb->json.stack[wb->json.depth].count++;
  623. }
  624. static inline void buffer_json_add_array_item_int64(BUFFER *wb, int64_t value) {
  625. if(wb->json.stack[wb->json.depth].count)
  626. buffer_fast_strcat(wb, ",", 1);
  627. buffer_print_int64(wb, value);
  628. wb->json.stack[wb->json.depth].count++;
  629. }
  630. static inline void buffer_json_add_array_item_uint64(BUFFER *wb, uint64_t value) {
  631. if(wb->json.stack[wb->json.depth].count)
  632. buffer_fast_strcat(wb, ",", 1);
  633. buffer_print_uint64(wb, value);
  634. wb->json.stack[wb->json.depth].count++;
  635. }
  636. static inline void buffer_json_add_array_item_time_t(BUFFER *wb, time_t value) {
  637. if(wb->json.stack[wb->json.depth].count)
  638. buffer_fast_strcat(wb, ",", 1);
  639. buffer_print_int64(wb, value);
  640. wb->json.stack[wb->json.depth].count++;
  641. }
  642. static inline void buffer_json_add_array_item_time_ms(BUFFER *wb, time_t value) {
  643. if(wb->json.stack[wb->json.depth].count)
  644. buffer_fast_strcat(wb, ",", 1);
  645. buffer_print_int64(wb, value);
  646. buffer_fast_strcat(wb, "000", 3);
  647. wb->json.stack[wb->json.depth].count++;
  648. }
  649. static inline void buffer_json_add_array_item_time_t2ms(BUFFER *wb, time_t value) {
  650. if(wb->json.stack[wb->json.depth].count)
  651. buffer_fast_strcat(wb, ",", 1);
  652. buffer_print_int64(wb, value);
  653. buffer_fast_strcat(wb, "000", 3);
  654. wb->json.stack[wb->json.depth].count++;
  655. }
  656. static inline void buffer_json_add_array_item_object(BUFFER *wb) {
  657. if(wb->json.stack[wb->json.depth].count)
  658. buffer_fast_strcat(wb, ",", 1);
  659. buffer_fast_strcat(wb, "{", 1);
  660. wb->json.stack[wb->json.depth].count++;
  661. _buffer_json_depth_push(wb, BUFFER_JSON_OBJECT);
  662. }
  663. static inline void buffer_json_member_add_time_t(BUFFER *wb, const char *key, time_t value) {
  664. buffer_print_json_comma_newline_spacing(wb);
  665. buffer_print_json_key(wb, key);
  666. buffer_fast_strcat(wb, ":", 1);
  667. buffer_print_int64(wb, value);
  668. wb->json.stack[wb->json.depth].count++;
  669. }
  670. static inline void buffer_json_member_add_time_t2ms(BUFFER *wb, const char *key, time_t value) {
  671. buffer_print_json_comma_newline_spacing(wb);
  672. buffer_print_json_key(wb, key);
  673. buffer_fast_strcat(wb, ":", 1);
  674. buffer_print_int64(wb, value);
  675. buffer_fast_strcat(wb, "000", 3);
  676. wb->json.stack[wb->json.depth].count++;
  677. }
  678. static inline void buffer_json_member_add_uint64(BUFFER *wb, const char *key, uint64_t value) {
  679. buffer_print_json_comma_newline_spacing(wb);
  680. buffer_print_json_key(wb, key);
  681. buffer_fast_strcat(wb, ":", 1);
  682. buffer_print_uint64(wb, value);
  683. wb->json.stack[wb->json.depth].count++;
  684. }
  685. static inline void buffer_json_member_add_int64(BUFFER *wb, const char *key, int64_t value) {
  686. buffer_print_json_comma_newline_spacing(wb);
  687. buffer_print_json_key(wb, key);
  688. buffer_fast_strcat(wb, ":", 1);
  689. buffer_print_int64(wb, value);
  690. wb->json.stack[wb->json.depth].count++;
  691. }
  692. static inline void buffer_json_member_add_double(BUFFER *wb, const char *key, NETDATA_DOUBLE value) {
  693. buffer_print_json_comma_newline_spacing(wb);
  694. buffer_print_json_key(wb, key);
  695. buffer_fast_strcat(wb, ":", 1);
  696. buffer_print_netdata_double(wb, value);
  697. wb->json.stack[wb->json.depth].count++;
  698. }
  699. static inline void buffer_json_array_close(BUFFER *wb) {
  700. #ifdef NETDATA_INTERNAL_CHECKS
  701. assert(wb->json.depth >= 0 && "BUFFER JSON: nothing is open to close it");
  702. assert(wb->json.stack[wb->json.depth].type == BUFFER_JSON_ARRAY && "BUFFER JSON: an array is not open to close it");
  703. #endif
  704. buffer_fast_strcat(wb, "]", 1);
  705. _buffer_json_depth_pop(wb);
  706. }
  707. #endif /* NETDATA_WEB_BUFFER_H */