1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <string.h>
#ifdef STORAGE_WITH_MATH
#include <math.h>
#endif
#include "common.h"
#include "web_buffer.h"
#include "log.h"
#define BUFFER_OVERFLOW_EOF "EOF"
static inline void buffer_overflow_init(BUFFER *b)
{
b->buffer[b->size] = '\0';
strcpy(&b->buffer[b->size + 1], BUFFER_OVERFLOW_EOF);
}
#ifdef NETDATA_INTERNAL_CHECKS
#define buffer_overflow_check(b) _buffer_overflow_check(b, __FILE__, __FUNCTION__, __LINE__)
#else
#define buffer_overflow_check(b)
#endif
static inline void _buffer_overflow_check(BUFFER *b, const char *file, const char *function, const unsigned long line)
{
if(b->len > b->size) {
error("BUFFER: length %ld is above size %ld, at line %lu, at function %s() of file '%s'.", b->len, b->size, line, function, file);
b->len = b->size;
}
if(b->buffer[b->size] != '\0' || strcmp(&b->buffer[b->size + 1], BUFFER_OVERFLOW_EOF)) {
error("BUFFER: detected overflow at line %lu, at function %s() of file '%s'.", line, function, file);
buffer_overflow_init(b);
}
}
void buffer_reset(BUFFER *wb)
{
buffer_flush(wb);
wb->contenttype = CT_TEXT_PLAIN;
wb->options = 0;
wb->date = 0;
buffer_overflow_check(wb);
}
const char *buffer_tostring(BUFFER *wb)
{
buffer_need_bytes(wb, (size_t)1);
wb->buffer[wb->len] = '\0';
buffer_overflow_check(wb);
return(wb->buffer);
}
void buffer_char_replace(BUFFER *wb, char from, char to)
{
char *s = wb->buffer, *end = &wb->buffer[wb->len];
while(s != end) {
if(*s == from) *s = to;
s++;
}
buffer_overflow_check(wb);
}
void buffer_strcat(BUFFER *wb, const char *txt)
{
if(unlikely(!txt || !*txt)) return;
buffer_need_bytes(wb, (size_t)(1));
char *s = &wb->buffer[wb->len], *end = &wb->buffer[wb->size];
long len = wb->len;
while(*txt && s != end) {
*s++ = *txt++;
len++;
}
wb->len = len;
buffer_overflow_check(wb);
if(*txt) {
debug(D_WEB_BUFFER, "strcat(): increasing web_buffer at position %ld, size = %ld\n", wb->len, wb->size);
len = strlen(txt);
buffer_increase(wb, len);
buffer_strcat(wb, txt);
}
else {
// terminate the string
// without increasing the length
buffer_need_bytes(wb, (size_t)1);
wb->buffer[wb->len] = '\0';
}
}
void buffer_snprintf(BUFFER *wb, size_t len, const char *fmt, ...)
{
if(unlikely(!fmt || !*fmt)) return;
buffer_need_bytes(wb, len+1);
va_list args;
va_start(args, fmt);
wb->len += vsnprintf(&wb->buffer[wb->len], len+1, fmt, args);
va_end(args);
buffer_overflow_check(wb);
// the buffer is \0 terminated by vsnprintf
}
void buffer_vsprintf(BUFFER *wb, const char *fmt, va_list args)
{
if(unlikely(!fmt || !*fmt)) return;
buffer_need_bytes(wb, 1);
size_t len = wb->size - wb->len;
wb->len += vsnprintf(&wb->buffer[wb->len], len, fmt, args);
buffer_overflow_check(wb);
// the buffer is \0 terminated by vsnprintf
}
void buffer_sprintf(BUFFER *wb, const char *fmt, ...)
{
if(unlikely(!fmt || !*fmt)) return;
buffer_need_bytes(wb, 1);
size_t len = wb->size - wb->len, wrote;
va_list args;
va_start(args, fmt);
wrote = (size_t) vsnprintf(&wb->buffer[wb->len], len, fmt, args);
va_end(args);
if(unlikely(wrote >= len)) {
// there is bug in vsnprintf() and it returns
// a number higher to len, but it does not
// overflow the buffer.
// our buffer overflow detector will log it
// if it does.
buffer_overflow_check(wb);
debug(D_WEB_BUFFER, "web_buffer_sprintf(): increasing web_buffer at position %ld, size = %ld\n", wb->len, wb->size);
buffer_need_bytes(wb, len + WEB_DATA_LENGTH_INCREASE_STEP);
va_start(args, fmt);
buffer_vsprintf(wb, fmt, args);
va_end(args);
}
else
wb->len += wrote;
// the buffer is \0 terminated by vsnprintf
}
void buffer_rrd_value(BUFFER *wb, calculated_number value)
{
buffer_need_bytes(wb, 50);
wb->len += print_calculated_number(&wb->buffer[wb->len], value);
// terminate it
buffer_need_bytes(wb, 1);
wb->buffer[wb->len] = '\0';
buffer_overflow_check(wb);
}
// generate a javascript date, the fastest possible way...
void buffer_jsdate(BUFFER *wb, int year, int month, int day, int hours, int minutes, int seconds)
{
// 10 20 30 = 35
// 01234567890123456789012345678901234
// Date(2014,04,01,03,28,20)
buffer_need_bytes(wb, 30);
char *b = &wb->buffer[wb->len];
int i = 0;
b[i++]='D';
b[i++]='a';
b[i++]='t';
b[i++]='e';
b[i++]='(';
b[i++]= (char) (48 + year / 1000); year -= (year / 1000) * 1000;
b[i++]= (char) (48 + year / 100); year -= (year / 100) * 100;
b[i++]= (char) (48 + year / 10);
b[i++]= (char) (48 + year % 10);
b[i++]=',';
b[i]= (char) (48 + month / 10); if(b[i] != '0') i++;
b[i++]= (char) (48 + month % 10);
b[i++]=',';
b[i]= (char) (48 + day / 10); if(b[i] != '0') i++;
b[i++]= (char) (48 + day % 10);
b[i++]=',';
b[i]= (char) (48 + hours / 10); if(b[i] != '0') i++;
b[i++]= (char) (48 + hours % 10);
b[i++]=',';
b[i]= (char) (48 + minutes / 10); if(b[i] != '0') i++;
b[i++]= (char) (48 + minutes % 10);
b[i++]=',';
b[i]= (char) (48 + seconds / 10); if(b[i] != '0') i++;
b[i++]= (char) (48 + seconds % 10);
b[i++]=')';
b[i]='\0';
wb->len += i;
// terminate it
wb->buffer[wb->len] = '\0';
buffer_overflow_check(wb);
}
// generate a date, the fastest possible way...
void buffer_date(BUFFER *wb, int year, int month, int day, int hours, int minutes, int seconds)
{
// 10 20 30 = 35
// 01234567890123456789012345678901234
// 2014-04-01 03:28:20
buffer_need_bytes(wb, 36);
char *b = &wb->buffer[wb->len];
int i = 0;
b[i++]= (char) (48 + year / 1000); year -= (year / 1000) * 1000;
b[i++]= (char) (48 + year / 100); year -= (year / 100) * 100;
b[i++]= (char) (48 + year / 10);
b[i++]= (char) (48 + year % 10);
b[i++]='-';
b[i++]= (char) (48 + month / 10);
b[i++]= (char) (48 + month % 10);
b[i++]='-';
b[i++]= (char) (48 + day / 10);
b[i++]= (char) (48 + day % 10);
b[i++]=' ';
b[i++]= (char) (48 + hours / 10);
b[i++]= (char) (48 + hours % 10);
b[i++]=':';
b[i++]= (char) (48 + minutes / 10);
b[i++]= (char) (48 + minutes % 10);
b[i++]=':';
b[i++]= (char) (48 + seconds / 10);
b[i++]= (char) (48 + seconds % 10);
b[i]='\0';
wb->len += i;
// terminate it
wb->buffer[wb->len] = '\0';
buffer_overflow_check(wb);
}
BUFFER *buffer_create(long size)
{
BUFFER *b;
debug(D_WEB_BUFFER, "Creating new web buffer of size %d.", size);
b = calloc(1, sizeof(BUFFER));
if(!b) {
error("Cannot allocate a web_buffer.");
return NULL;
}
b->buffer = malloc(size + sizeof(BUFFER_OVERFLOW_EOF) + 2);
if(!b->buffer) {
error("Cannot allocate a buffer of size %u.", size + sizeof(BUFFER_OVERFLOW_EOF) + 2);
free(b);
return NULL;
}
b->buffer[0] = '\0';
b->size = size;
b->contenttype = CT_TEXT_PLAIN;
buffer_overflow_init(b);
buffer_overflow_check(b);
return(b);
}
void buffer_free(BUFFER *b)
{
buffer_overflow_check(b);
debug(D_WEB_BUFFER, "Freeing web buffer of size %d.", b->size);
if(b->buffer) free(b->buffer);
free(b);
}
void buffer_increase(BUFFER *b, size_t free_size_required)
{
buffer_overflow_check(b);
size_t left = b->size - b->len;
if(left >= free_size_required) return;
size_t increase = free_size_required - left;
if(increase < WEB_DATA_LENGTH_INCREASE_STEP) increase = WEB_DATA_LENGTH_INCREASE_STEP;
debug(D_WEB_BUFFER, "Increasing data buffer from size %d to %d.", b->size, b->size + increase);
b->buffer = realloc(b->buffer, b->size + increase + sizeof(BUFFER_OVERFLOW_EOF) + 2);
if(!b->buffer) fatal("Failed to increase data buffer from size %d to %d.", b->size + sizeof(BUFFER_OVERFLOW_EOF) + 2, b->size + increase + sizeof(BUFFER_OVERFLOW_EOF) + 2);
b->size += increase;
buffer_overflow_init(b);
buffer_overflow_check(b);
}
|