master
c 564 lines 17.4 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "../libnetdata.h"
4
5 static ALWAYS_INLINE void buffer_overflow_init(BUFFER *b)
6 {
7 b->buffer[b->size] = '\0';
8 strcpy(&b->buffer[b->size + 1], BUFFER_OVERFLOW_EOF);
9 }
10
11 ALWAYS_INLINE void buffer_reset(BUFFER *wb) {
12 buffer_flush(wb);
13
14 wb->content_type = CT_TEXT_PLAIN;
15 wb->options = 0;
16 wb->date = 0;
17 wb->expires = 0;
18 buffer_no_cacheable(wb);
19
20 buffer_overflow_check(wb);
21 }
22
23 void buffer_char_replace(BUFFER *wb, char from, char to) {
24 char *s = wb->buffer, *end = &wb->buffer[wb->len];
25
26 while(s != end) {
27 if(*s == from) *s = to;
28 s++;
29 }
30
31 buffer_overflow_check(wb);
32 }
33
34 ALWAYS_INLINE void buffer_print_sn_flags(BUFFER *wb, SN_FLAGS flags, bool send_anomaly_bit) {
35 if(unlikely(flags == SN_EMPTY_SLOT)) {
36 buffer_fast_strcat(wb, "E", 1);
37 return;
38 }
39
40 size_t printed = 0;
41 if(likely(send_anomaly_bit && (flags & SN_FLAG_NOT_ANOMALOUS))) {
42 buffer_fast_strcat(wb, "A", 1);
43 printed++;
44 }
45
46 if(unlikely(flags & SN_FLAG_RESET)) {
47 buffer_fast_strcat(wb, "R", 1);
48 printed++;
49 }
50
51 if(!printed)
52 buffer_fast_strcat(wb, "''", 2);
53 }
54
55 void buffer_strcat_htmlescape(BUFFER *wb, const char *txt)
56 {
57 while(*txt) {
58 switch(*txt) {
59 case '&': buffer_strcat(wb, "&"); break;
60 case '<': buffer_strcat(wb, "&lt;"); break;
61 case '>': buffer_strcat(wb, "&gt;"); break;
62 case '"': buffer_strcat(wb, "&quot;"); break;
63 case '/': buffer_strcat(wb, "&#x2F;"); break;
64 case '\'': buffer_strcat(wb, "&#x27;"); break;
65 default: {
66 buffer_need_bytes(wb, 1);
67 wb->buffer[wb->len++] = *txt;
68 }
69 }
70 txt++;
71 }
72
73 buffer_overflow_check(wb);
74 }
75
76 void buffer_snprintf(BUFFER *wb, size_t len, const char *fmt, ...)
77 {
78 if(unlikely(!fmt || !*fmt)) return;
79
80 buffer_need_bytes(wb, len + 1);
81
82 va_list args;
83 va_start(args, fmt);
84 // vsnprintfz() returns the number of bytes actually written - after possible truncation
85 wb->len += vsnprintfz(&wb->buffer[wb->len], len, fmt, args);
86 va_end(args);
87
88 buffer_overflow_check(wb);
89
90 // the buffer is \0 terminated by vsnprintfz
91 }
92
93 void buffer_vsprintf(BUFFER *wb, const char *fmt, va_list args) {
94 if(unlikely(!fmt || !*fmt)) return;
95
96 size_t full_size_bytes = 0, need = 2, space_remaining = 0;
97
98 do {
99 need += full_size_bytes + 2;
100
101 buffer_need_bytes(wb, need);
102
103 space_remaining = wb->size - wb->len - 1;
104
105 // Use the copy of va_list for vsnprintf
106 va_list args_copy;
107 va_copy(args_copy, args);
108 // vsnprintf() returns the number of bytes required, even if bigger than the buffer provided
109 full_size_bytes = (size_t) vsnprintf(&wb->buffer[wb->len], space_remaining, fmt, args_copy);
110 va_end(args_copy);
111
112 } while(full_size_bytes >= space_remaining);
113
114 wb->len += full_size_bytes;
115
116 wb->buffer[wb->len] = '\0';
117 buffer_overflow_check(wb);
118 }
119
120 void buffer_sprintf(BUFFER *wb, const char *fmt, ...)
121 {
122 va_list args;
123 va_start(args, fmt);
124 buffer_vsprintf(wb, fmt, args);
125 va_end(args);
126 }
127
128 void buffer_json_member_add_sprintf(BUFFER *wb, const char *key, const char *fmt, ...)
129 {
130 va_list args;
131
132 // Create a temporary buffer for the formatted string
133 BUFFER *tmp = buffer_create(0, NULL);
134
135 va_start(args, fmt);
136 buffer_vsprintf(tmp, fmt, args);
137 va_end(args);
138
139 // Add as JSON member (which will handle escaping)
140 buffer_json_member_add_string(wb, key, buffer_tostring(tmp));
141
142 // Free the temporary buffer
143 buffer_free(tmp);
144 }
145
146 void buffer_json_add_array_item_sprintf(BUFFER *wb, const char *fmt, ...)
147 {
148 va_list args;
149
150 // Create a temporary buffer for the formatted string
151 BUFFER *tmp = buffer_create(0, NULL);
152
153 va_start(args, fmt);
154 buffer_vsprintf(tmp, fmt, args);
155 va_end(args);
156
157 // Add as array item (which will handle escaping)
158 buffer_json_add_array_item_string(wb, buffer_tostring(tmp));
159
160 // Free the temporary buffer
161 buffer_free(tmp);
162 }
163
164 // generate a javascript date, the fastest possible way...
165 void buffer_jsdate(BUFFER *wb, int year, int month, int day, int hours, int minutes, int seconds)
166 {
167 // 10 20 30 = 35
168 // 01234567890123456789012345678901234
169 // Date(2014,04,01,03,28,20)
170
171 buffer_need_bytes(wb, 30);
172
173 char *b = &wb->buffer[wb->len], *p;
174 unsigned int *q = (unsigned int *)b;
175
176 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
177 *q++ = 0x65746144; // "Date" backwards.
178 #else
179 *q++ = 0x44617465; // "Date"
180 #endif
181 p = (char *)q;
182
183 *p++ = '(';
184 *p++ = '0' + year / 1000; year %= 1000;
185 *p++ = '0' + year / 100; year %= 100;
186 *p++ = '0' + year / 10;
187 *p++ = '0' + year % 10;
188 *p++ = ',';
189 *p = '0' + month / 10; if (*p != '0') p++;
190 *p++ = '0' + month % 10;
191 *p++ = ',';
192 *p = '0' + day / 10; if (*p != '0') p++;
193 *p++ = '0' + day % 10;
194 *p++ = ',';
195 *p = '0' + hours / 10; if (*p != '0') p++;
196 *p++ = '0' + hours % 10;
197 *p++ = ',';
198 *p = '0' + minutes / 10; if (*p != '0') p++;
199 *p++ = '0' + minutes % 10;
200 *p++ = ',';
201 *p = '0' + seconds / 10; if (*p != '0') p++;
202 *p++ = '0' + seconds % 10;
203
204 unsigned short *r = (unsigned short *)p;
205
206 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
207 *r++ = 0x0029; // ")\0" backwards.
208 #else
209 *r++ = 0x2900; // ")\0"
210 #endif
211
212 wb->len += (size_t)((char *)r - b - 1);
213
214 // terminate it
215 wb->buffer[wb->len] = '\0';
216 buffer_overflow_check(wb);
217 }
218
219 // generate a date, the fastest possible way...
220 void buffer_date(BUFFER *wb, int year, int month, int day, int hours, int minutes, int seconds)
221 {
222 // 10 20 30 = 35
223 // 01234567890123456789012345678901234
224 // 2014-04-01 03:28:20
225
226 buffer_need_bytes(wb, 36);
227
228 char *b = &wb->buffer[wb->len];
229 char *p = b;
230
231 *p++ = '0' + year / 1000; year %= 1000;
232 *p++ = '0' + year / 100; year %= 100;
233 *p++ = '0' + year / 10;
234 *p++ = '0' + year % 10;
235 *p++ = '-';
236 *p++ = '0' + month / 10;
237 *p++ = '0' + month % 10;
238 *p++ = '-';
239 *p++ = '0' + day / 10;
240 *p++ = '0' + day % 10;
241 *p++ = ' ';
242 *p++ = '0' + hours / 10;
243 *p++ = '0' + hours % 10;
244 *p++ = ':';
245 *p++ = '0' + minutes / 10;
246 *p++ = '0' + minutes % 10;
247 *p++ = ':';
248 *p++ = '0' + seconds / 10;
249 *p++ = '0' + seconds % 10;
250 *p = '\0';
251
252 wb->len += (size_t)(p - b);
253
254 // terminate it
255 wb->buffer[wb->len] = '\0';
256 buffer_overflow_check(wb);
257 }
258
259 BUFFER *buffer_create(size_t size, size_t *statistics)
260 {
261 BUFFER *b;
262
263 if(!size)
264 size = 1024 - sizeof(BUFFER_OVERFLOW_EOF) - 2;
265 else
266 size++; // make room for the terminator
267
268 netdata_log_debug(D_WEB_BUFFER, "Creating new web buffer of size %zu.", size);
269
270 b = callocz(1, sizeof(BUFFER));
271 b->buffer = mallocz(size + sizeof(BUFFER_OVERFLOW_EOF) + 2);
272 b->buffer[0] = '\0';
273 b->size = size;
274 b->content_type = CT_TEXT_PLAIN;
275 b->statistics = statistics;
276 buffer_no_cacheable(b);
277 buffer_overflow_init(b);
278 buffer_overflow_check(b);
279
280 if(b->statistics)
281 __atomic_add_fetch(b->statistics, b->size + sizeof(BUFFER) + sizeof(BUFFER_OVERFLOW_EOF) + 2, __ATOMIC_RELAXED);
282
283 return(b);
284 }
285
286 void buffer_free(BUFFER *b) {
287 if(unlikely(!b)) return;
288
289 buffer_overflow_check(b);
290
291 netdata_log_debug(D_WEB_BUFFER, "Freeing web buffer of size %zu.", (size_t)b->size);
292
293 if(b->statistics)
294 __atomic_sub_fetch(b->statistics, b->size + sizeof(BUFFER) + sizeof(BUFFER_OVERFLOW_EOF) + 2, __ATOMIC_RELAXED);
295
296 freez(b->buffer);
297 freez(b);
298 }
299
300 void buffer_increase(BUFFER *b, size_t free_size_required) {
301 buffer_overflow_check(b);
302
303 size_t remaining = b->size - b->len;
304 if(remaining >= free_size_required) return;
305
306 size_t increase = free_size_required - remaining;
307 size_t minimum = 1024;
308 if(minimum > increase) increase = minimum;
309
310 size_t optimal = (b->size > 5 * 1024 * 1024) ? b->size / 2 : b->size;
311 if(optimal > increase) increase = optimal;
312
313 netdata_log_debug(D_WEB_BUFFER, "Increasing data buffer from size %zu to %zu.", (size_t)b->size, (size_t)(b->size + increase));
314
315 b->buffer = reallocz(b->buffer, b->size + increase + sizeof(BUFFER_OVERFLOW_EOF) + 2);
316 b->size += increase;
317
318 if(b->statistics)
319 __atomic_add_fetch(b->statistics, increase, __ATOMIC_RELAXED);
320
321 buffer_overflow_init(b);
322 buffer_overflow_check(b);
323 }
324
325 // ----------------------------------------------------------------------------
326
327 void buffer_json_initialize(BUFFER *wb, const char *key_quote, const char *value_quote, int depth,
328 bool add_anonymous_object, BUFFER_JSON_OPTIONS options) {
329 strncpyz(wb->json.key_quote, key_quote, BUFFER_QUOTE_MAX_SIZE);
330 strncpyz(wb->json.value_quote, value_quote, BUFFER_QUOTE_MAX_SIZE);
331
332 wb->json.depth = (int8_t)(depth - 1);
333 _buffer_json_depth_push(wb, BUFFER_JSON_OBJECT);
334
335 if(add_anonymous_object)
336 buffer_fast_strcat(wb, "{", 1);
337 else
338 options |= BUFFER_JSON_OPTIONS_NON_ANONYMOUS;
339
340 wb->json.options = options;
341
342 wb->content_type = CT_APPLICATION_JSON;
343 buffer_no_cacheable(wb);
344 }
345
346 void buffer_json_finalize(BUFFER *wb) {
347 while(wb->json.depth >= 0) {
348 switch(wb->json.stack[wb->json.depth].type) {
349 case BUFFER_JSON_OBJECT:
350 if (wb->json.depth == 0)
351 if (!(wb->json.options & BUFFER_JSON_OPTIONS_NON_ANONYMOUS))
352 buffer_json_object_close(wb);
353 else
354 _buffer_json_depth_pop(wb);
355 else
356 buffer_json_object_close(wb);
357 break;
358 case BUFFER_JSON_ARRAY:
359 buffer_json_array_close(wb);
360 break;
361
362 default:
363 internal_fatal(true, "BUFFER: unknown json member type in stack");
364 break;
365 }
366 }
367
368 if(!(wb->json.options & BUFFER_JSON_OPTIONS_MINIFY))
369 buffer_fast_strcat(wb, "\n", 1);
370 }
371
372 // ----------------------------------------------------------------------------
373
374 __attribute__((nonstring))
375 const char hex_digits[16] = "0123456789ABCDEF";
376
377 __attribute__((nonstring))
378 const char hex_digits_lower[16] = "0123456789abcdef";
379
380 __attribute__((nonstring))
381 const char base64_digits[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
382
383 unsigned char hex_value_from_ascii[256];
384 unsigned char base64_value_from_ascii[256];
385
386 __attribute__((constructor)) void initialize_ascii_maps(void) {
387 for(size_t i = 0 ; i < 256 ; i++) {
388 hex_value_from_ascii[i] = 255;
389 base64_value_from_ascii[i] = 255;
390 }
391
392 for(size_t i = 0; i < 16 ; i++) {
393 hex_value_from_ascii[(int)toupper(hex_digits[i])] = i;
394 hex_value_from_ascii[(int)tolower(hex_digits[i])] = i;
395 }
396
397 for(size_t i = 0; i < 64 ; i++)
398 base64_value_from_ascii[(int)base64_digits[i]] = i;
399 }
400
401 // ----------------------------------------------------------------------------
402
403 void buffer_json_member_add_datetime_rfc3339(BUFFER *wb, const char *key, uint64_t datetime_ut, bool utc) {
404 char buf[RFC3339_MAX_LENGTH];
405 rfc3339_datetime_ut(buf, sizeof(buf), datetime_ut, 2, utc);
406 buffer_json_member_add_string(wb, key, buf);
407 }
408
409 void buffer_json_member_add_duration_ut(BUFFER *wb, const char *key, int64_t duration_ut) {
410 char buf[64];
411 duration_snprintf(buf, sizeof(buf), duration_ut, "us", true);
412 buffer_json_member_add_string(wb, key, buf);
413 }
414
415 void buffer_json_add_array_item_datetime_rfc3339(BUFFER *wb, uint64_t datetime_ut, bool utc) {
416 char buf[RFC3339_MAX_LENGTH];
417 rfc3339_datetime_ut(buf, sizeof(buf), datetime_ut, 2, utc);
418 buffer_json_add_array_item_string(wb, buf);
419 }
420
421 // ----------------------------------------------------------------------------
422 // unit test
423
424 static int buffer_expect(BUFFER *wb, const char *expected) {
425 const char *generated = buffer_tostring(wb);
426
427 if(strcmp(generated, expected) != 0) {
428 netdata_log_error("BUFFER: mismatch.\nGenerated:\n%s\nExpected:\n%s\n",
429 generated, expected);
430 return 1;
431 }
432
433 return 0;
434 }
435
436 static int buffer_uint64_roundtrip(BUFFER *wb, NUMBER_ENCODING encoding, uint64_t value, const char *expected) {
437 int errors = 0;
438 buffer_flush(wb);
439 buffer_print_uint64_encoded(wb, encoding, value);
440
441 if(expected)
442 errors += buffer_expect(wb, expected);
443
444 uint64_t v = str2ull_encoded(buffer_tostring(wb));
445 if(v != value) {
446 netdata_log_error("BUFFER: string '%s' does resolves to %llu, expected %llu",
447 buffer_tostring(wb), (unsigned long long)v, (unsigned long long)value);
448 errors++;
449 }
450 buffer_flush(wb);
451 return errors;
452 }
453
454 static int buffer_int64_roundtrip(BUFFER *wb, NUMBER_ENCODING encoding, int64_t value, const char *expected) {
455 int errors = 0;
456 buffer_flush(wb);
457 buffer_print_int64_encoded(wb, encoding, value);
458
459 if(expected)
460 errors += buffer_expect(wb, expected);
461
462 int64_t v = str2ll_encoded(buffer_tostring(wb));
463 if(v != value) {
464 netdata_log_error("BUFFER: string '%s' does resolves to %lld, expected %lld",
465 buffer_tostring(wb), (long long)v, (long long)value);
466 errors++;
467 }
468 buffer_flush(wb);
469 return errors;
470 }
471
472 static int buffer_double_roundtrip(BUFFER *wb, NUMBER_ENCODING encoding, NETDATA_DOUBLE value, const char *expected) {
473 int errors = 0;
474 buffer_flush(wb);
475 buffer_print_netdata_double_encoded(wb, encoding, value);
476
477 if(expected)
478 errors += buffer_expect(wb, expected);
479
480 NETDATA_DOUBLE v = str2ndd_encoded(buffer_tostring(wb), NULL);
481 if(v != value) {
482 netdata_log_error("BUFFER: string '%s' does resolves to %.12f, expected %.12f",
483 buffer_tostring(wb), v, value);
484 errors++;
485 }
486 buffer_flush(wb);
487 return errors;
488 }
489
490 int buffer_unittest(void) {
491 int errors = 0;
492 BUFFER *wb = buffer_create(0, NULL);
493
494 buffer_uint64_roundtrip(wb, NUMBER_ENCODING_DECIMAL, 0, "0");
495 buffer_uint64_roundtrip(wb, NUMBER_ENCODING_HEX, 0, "0x0");
496 buffer_uint64_roundtrip(wb, NUMBER_ENCODING_BASE64, 0, "#A");
497
498 buffer_uint64_roundtrip(wb, NUMBER_ENCODING_DECIMAL, 1676071986, "1676071986");
499 buffer_uint64_roundtrip(wb, NUMBER_ENCODING_HEX, 1676071986, "0x63E6D432");
500 buffer_uint64_roundtrip(wb, NUMBER_ENCODING_BASE64, 1676071986, "#Bj5tQy");
501
502 buffer_uint64_roundtrip(wb, NUMBER_ENCODING_DECIMAL, 18446744073709551615ULL, "18446744073709551615");
503 buffer_uint64_roundtrip(wb, NUMBER_ENCODING_HEX, 18446744073709551615ULL, "0xFFFFFFFFFFFFFFFF");
504 buffer_uint64_roundtrip(wb, NUMBER_ENCODING_BASE64, 18446744073709551615ULL, "#P//////////");
505
506 buffer_int64_roundtrip(wb, NUMBER_ENCODING_DECIMAL, 0, "0");
507 buffer_int64_roundtrip(wb, NUMBER_ENCODING_HEX, 0, "0x0");
508 buffer_int64_roundtrip(wb, NUMBER_ENCODING_BASE64, 0, "#A");
509
510 buffer_int64_roundtrip(wb, NUMBER_ENCODING_DECIMAL, -1676071986, "-1676071986");
511 buffer_int64_roundtrip(wb, NUMBER_ENCODING_HEX, -1676071986, "-0x63E6D432");
512 buffer_int64_roundtrip(wb, NUMBER_ENCODING_BASE64, -1676071986, "-#Bj5tQy");
513
514 buffer_int64_roundtrip(wb, NUMBER_ENCODING_DECIMAL, (int64_t)-9223372036854775807ULL, "-9223372036854775807");
515 buffer_int64_roundtrip(wb, NUMBER_ENCODING_HEX, (int64_t)-9223372036854775807ULL, "-0x7FFFFFFFFFFFFFFF");
516 buffer_int64_roundtrip(wb, NUMBER_ENCODING_BASE64, (int64_t)-9223372036854775807ULL, "-#H//////////");
517
518 buffer_double_roundtrip(wb, NUMBER_ENCODING_DECIMAL, 0, "0");
519 buffer_double_roundtrip(wb, NUMBER_ENCODING_HEX, 0, "%0");
520 buffer_double_roundtrip(wb, NUMBER_ENCODING_BASE64, 0, "@A");
521
522 buffer_double_roundtrip(wb, NUMBER_ENCODING_DECIMAL, 1.5, "1.5");
523 buffer_double_roundtrip(wb, NUMBER_ENCODING_HEX, 1.5, "%3FF8000000000000");
524 buffer_double_roundtrip(wb, NUMBER_ENCODING_BASE64, 1.5, "@D/4AAAAAAAA");
525
526 buffer_double_roundtrip(wb, NUMBER_ENCODING_DECIMAL, 1.23e+14, "123000000000000");
527 buffer_double_roundtrip(wb, NUMBER_ENCODING_HEX, 1.23e+14, "%42DBF78AD3AC0000");
528 buffer_double_roundtrip(wb, NUMBER_ENCODING_BASE64, 1.23e+14, "@ELb94rTrAAA");
529
530 buffer_double_roundtrip(wb, NUMBER_ENCODING_DECIMAL, 9.12345678901234567890123456789e+45, "9.123456789012346128e+45");
531 buffer_double_roundtrip(wb, NUMBER_ENCODING_HEX, 9.12345678901234567890123456789e+45, "%497991C25C9E4309");
532 buffer_double_roundtrip(wb, NUMBER_ENCODING_BASE64, 9.12345678901234567890123456789e+45, "@El5kcJcnkMJ");
533
534 buffer_flush(wb);
535
536 {
537 char buf[1024 + 1];
538 for(size_t i = 0; i < 1024 ;i++)
539 buf[i] = (char)(i % 26) + 'A';
540 buf[1024] = '\0';
541
542 buffer_strcat(wb, buf);
543 errors += buffer_expect(wb, buf);
544 }
545
546 buffer_flush(wb);
547
548 buffer_json_initialize(wb, "\"", "\"", 0, true, BUFFER_JSON_OPTIONS_DEFAULT);
549 buffer_json_finalize(wb);
550 errors += buffer_expect(wb, "{\n}\n");
551
552 buffer_flush(wb);
553
554 buffer_json_initialize(wb, "\"", "\"", 0, true, BUFFER_JSON_OPTIONS_DEFAULT);
555 buffer_json_member_add_string(wb, "hello", "world");
556 buffer_json_member_add_string(wb, "alpha", "this: \" is a double quote");
557 buffer_json_member_add_object(wb, "object1");
558 buffer_json_member_add_string(wb, "hello", "world");
559 buffer_json_finalize(wb);
560 errors += buffer_expect(wb, "{\n \"hello\":\"world\",\n \"alpha\":\"this: \\\" is a double quote\",\n \"object1\":{\n \"hello\":\"world\"\n }\n}\n");
561
562 buffer_free(wb);
563 return errors;
564 }