]>
Commit | Line | Data |
---|---|---|
ecacdfa4 MM |
1 | /* |
2 | * BIRD Library -- Formatted Output | |
3 | * | |
4 | * (c) 1991, 1992 Lars Wirzenius & Linus Torvalds | |
5 | * | |
6 | * Hacked up for BIRD by Martin Mares <mj@ucw.cz> | |
53a416d3 | 7 | * Buffer size limitation implemented by Martin Mares. |
ecacdfa4 MM |
8 | */ |
9 | ||
10 | #include "nest/bird.h" | |
11 | #include "string.h" | |
12 | ||
d997534f | 13 | #include <errno.h> |
d997534f | 14 | |
53ffbff3 OZ |
15 | #include "nest/iface.h" |
16 | ||
ecacdfa4 MM |
17 | /* we use this so that we can do without the ctype library */ |
18 | #define is_digit(c) ((c) >= '0' && (c) <= '9') | |
19 | ||
20 | static int skip_atoi(const char **s) | |
21 | { | |
22 | int i=0; | |
23 | ||
24 | while (is_digit(**s)) | |
25 | i = i*10 + *((*s)++) - '0'; | |
26 | return i; | |
27 | } | |
28 | ||
29 | #define ZEROPAD 1 /* pad with zero */ | |
30 | #define SIGN 2 /* unsigned/signed long */ | |
31 | #define PLUS 4 /* show plus */ | |
32 | #define SPACE 8 /* space if plus */ | |
33 | #define LEFT 16 /* left justified */ | |
34 | #define SPECIAL 32 /* 0x */ | |
35 | #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */ | |
36 | ||
37 | #define do_div(n,base) ({ \ | |
38 | int __res; \ | |
39 | __res = ((unsigned long) n) % (unsigned) base; \ | |
40 | n = ((unsigned long) n) / (unsigned) base; \ | |
41 | __res; }) | |
42 | ||
53a416d3 MM |
43 | static char * number(char * str, long num, int base, int size, int precision, |
44 | int type, int remains) | |
ecacdfa4 MM |
45 | { |
46 | char c,sign,tmp[66]; | |
47 | const char *digits="0123456789abcdefghijklmnopqrstuvwxyz"; | |
48 | int i; | |
49 | ||
53a416d3 MM |
50 | if (size >= 0 && (remains -= size) < 0) |
51 | return NULL; | |
ecacdfa4 MM |
52 | if (type & LARGE) |
53 | digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; | |
54 | if (type & LEFT) | |
55 | type &= ~ZEROPAD; | |
56 | if (base < 2 || base > 36) | |
57 | return 0; | |
58 | c = (type & ZEROPAD) ? '0' : ' '; | |
59 | sign = 0; | |
60 | if (type & SIGN) { | |
61 | if (num < 0) { | |
62 | sign = '-'; | |
63 | num = -num; | |
64 | size--; | |
65 | } else if (type & PLUS) { | |
66 | sign = '+'; | |
67 | size--; | |
68 | } else if (type & SPACE) { | |
69 | sign = ' '; | |
70 | size--; | |
71 | } | |
72 | } | |
73 | if (type & SPECIAL) { | |
74 | if (base == 16) | |
75 | size -= 2; | |
76 | else if (base == 8) | |
77 | size--; | |
78 | } | |
79 | i = 0; | |
80 | if (num == 0) | |
81 | tmp[i++]='0'; | |
82 | else while (num != 0) | |
83 | tmp[i++] = digits[do_div(num,base)]; | |
84 | if (i > precision) | |
85 | precision = i; | |
86 | size -= precision; | |
53a416d3 MM |
87 | if (size < 0 && -size > remains) |
88 | return NULL; | |
ecacdfa4 MM |
89 | if (!(type&(ZEROPAD+LEFT))) |
90 | while(size-->0) | |
91 | *str++ = ' '; | |
92 | if (sign) | |
93 | *str++ = sign; | |
94 | if (type & SPECIAL) { | |
95 | if (base==8) | |
96 | *str++ = '0'; | |
97 | else if (base==16) { | |
98 | *str++ = '0'; | |
99 | *str++ = digits[33]; | |
100 | } | |
101 | } | |
102 | if (!(type & LEFT)) | |
103 | while (size-- > 0) | |
104 | *str++ = c; | |
105 | while (i < precision--) | |
106 | *str++ = '0'; | |
107 | while (i-- > 0) | |
108 | *str++ = tmp[i]; | |
109 | while (size-- > 0) | |
110 | *str++ = ' '; | |
111 | return str; | |
112 | } | |
113 | ||
7722938d MM |
114 | /** |
115 | * bvsnprintf - BIRD's vsnprintf() | |
116 | * @buf: destination buffer | |
117 | * @size: size of the buffer | |
118 | * @fmt: format string | |
119 | * @args: a list of arguments to be formatted | |
120 | * | |
e691d16a OZ |
121 | * This functions acts like ordinary sprintf() except that it checks available |
122 | * space to avoid buffer overflows and it allows some more format specifiers: | |
123 | * |%I| for formatting of IP addresses (width of 1 is automatically replaced by | |
124 | * standard IP address width which depends on whether we use IPv4 or IPv6; |%I4| | |
125 | * or |%I6| can be used for explicit ip4_addr / ip6_addr arguments, |%N| for | |
126 | * generic network addresses (net_addr *), |%R| for Router / Network ID (u32 | |
af678af0 | 127 | * value printed as IPv4 address), |%lR| for 64bit Router / Network ID (u64 |
49fc0213 OZ |
128 | * value printed as eight :-separated octets), |%t| for time values (btime) with |
129 | * specified subsecond precision, and |%m| resp. |%M| for error messages (uses | |
130 | * strerror() to translate @errno code to message text). On the other hand, it | |
131 | * doesn't support floating point numbers. | |
7722938d MM |
132 | * |
133 | * Result: number of characters of the output string or -1 if | |
134 | * the buffer space was insufficient. | |
135 | */ | |
53a416d3 | 136 | int bvsnprintf(char *buf, int size, const char *fmt, va_list args) |
ecacdfa4 MM |
137 | { |
138 | int len; | |
139 | unsigned long num; | |
140 | int i, base; | |
2f6483cd | 141 | u32 x; |
937e75d8 | 142 | u64 X; |
49fc0213 OZ |
143 | btime t; |
144 | s64 t1, t2; | |
53a416d3 | 145 | char *str, *start; |
ecacdfa4 | 146 | const char *s; |
7fd4143e | 147 | char ipbuf[NET_MAX_TEXT_LENGTH+1]; |
53ffbff3 | 148 | struct iface *iface; |
ecacdfa4 MM |
149 | |
150 | int flags; /* flags to number() */ | |
151 | ||
152 | int field_width; /* width of output field */ | |
153 | int precision; /* min. # of digits for integers; max | |
154 | number of chars for from string */ | |
155 | int qualifier; /* 'h', 'l', or 'L' for integer fields */ | |
156 | ||
53a416d3 | 157 | for (start=str=buf ; *fmt ; ++fmt, size-=(str-start), start=str) { |
ecacdfa4 | 158 | if (*fmt != '%') { |
53a416d3 MM |
159 | if (!size) |
160 | return -1; | |
ecacdfa4 MM |
161 | *str++ = *fmt; |
162 | continue; | |
163 | } | |
7fd4143e | 164 | |
ecacdfa4 MM |
165 | /* process flags */ |
166 | flags = 0; | |
167 | repeat: | |
168 | ++fmt; /* this also skips first '%' */ | |
169 | switch (*fmt) { | |
170 | case '-': flags |= LEFT; goto repeat; | |
171 | case '+': flags |= PLUS; goto repeat; | |
172 | case ' ': flags |= SPACE; goto repeat; | |
173 | case '#': flags |= SPECIAL; goto repeat; | |
174 | case '0': flags |= ZEROPAD; goto repeat; | |
d107ef78 | 175 | } |
e691d16a | 176 | |
ecacdfa4 MM |
177 | /* get field width */ |
178 | field_width = -1; | |
179 | if (is_digit(*fmt)) | |
180 | field_width = skip_atoi(&fmt); | |
181 | else if (*fmt == '*') { | |
182 | ++fmt; | |
183 | /* it's the next argument */ | |
184 | field_width = va_arg(args, int); | |
185 | if (field_width < 0) { | |
186 | field_width = -field_width; | |
187 | flags |= LEFT; | |
188 | } | |
189 | } | |
190 | ||
191 | /* get the precision */ | |
192 | precision = -1; | |
193 | if (*fmt == '.') { | |
e691d16a | 194 | ++fmt; |
ecacdfa4 MM |
195 | if (is_digit(*fmt)) |
196 | precision = skip_atoi(&fmt); | |
197 | else if (*fmt == '*') { | |
198 | ++fmt; | |
199 | /* it's the next argument */ | |
200 | precision = va_arg(args, int); | |
201 | } | |
202 | if (precision < 0) | |
203 | precision = 0; | |
204 | } | |
205 | ||
206 | /* get the conversion qualifier */ | |
207 | qualifier = -1; | |
208 | if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') { | |
209 | qualifier = *fmt; | |
210 | ++fmt; | |
211 | } | |
212 | ||
213 | /* default base */ | |
214 | base = 10; | |
215 | ||
53a416d3 MM |
216 | if (field_width > size) |
217 | return -1; | |
ecacdfa4 MM |
218 | switch (*fmt) { |
219 | case 'c': | |
220 | if (!(flags & LEFT)) | |
221 | while (--field_width > 0) | |
222 | *str++ = ' '; | |
e348ef01 | 223 | *str++ = (byte) va_arg(args, int); |
ecacdfa4 MM |
224 | while (--field_width > 0) |
225 | *str++ = ' '; | |
226 | continue; | |
227 | ||
d997534f | 228 | case 'm': |
05476c4d OZ |
229 | if (flags & SPECIAL) { |
230 | if (!errno) | |
231 | continue; | |
232 | if (size < 2) | |
233 | return -1; | |
234 | *str++ = ':'; | |
235 | *str++ = ' '; | |
236 | start += 2; | |
237 | size -= 2; | |
238 | } | |
d997534f MM |
239 | s = strerror(errno); |
240 | goto str; | |
53943a00 MM |
241 | case 'M': |
242 | s = strerror(va_arg(args, int)); | |
243 | goto str; | |
7fd4143e JMM |
244 | case 'N': { |
245 | net_addr *n = va_arg(args, net_addr *); | |
fe9f1a6d | 246 | if (field_width == 1) |
7fd4143e JMM |
247 | field_width = net_max_text_length[n->type]; |
248 | net_format(n, ipbuf, sizeof(ipbuf)); | |
fe9f1a6d OZ |
249 | s = ipbuf; |
250 | goto str; | |
7fd4143e | 251 | } |
ecacdfa4 MM |
252 | case 's': |
253 | s = va_arg(args, char *); | |
254 | if (!s) | |
255 | s = "<NULL>"; | |
256 | ||
d997534f | 257 | str: |
ecacdfa4 MM |
258 | len = strlen(s); |
259 | if (precision >= 0 && len > precision) | |
260 | len = precision; | |
53a416d3 MM |
261 | if (len > size) |
262 | return -1; | |
ecacdfa4 MM |
263 | |
264 | if (!(flags & LEFT)) | |
265 | while (len < field_width--) | |
266 | *str++ = ' '; | |
267 | for (i = 0; i < len; ++i) | |
268 | *str++ = *s++; | |
269 | while (len < field_width--) | |
270 | *str++ = ' '; | |
271 | continue; | |
272 | ||
64c5ad58 JMM |
273 | case 'V': { |
274 | const char *vfmt = va_arg(args, const char *); | |
275 | va_list *vargs = va_arg(args, va_list *); | |
276 | int res = bvsnprintf(str, size, vfmt, *vargs); | |
277 | if (res < 0) | |
278 | return -1; | |
279 | str += res; | |
280 | size -= res; | |
281 | continue; | |
282 | } | |
283 | ||
ecacdfa4 MM |
284 | case 'p': |
285 | if (field_width == -1) { | |
286 | field_width = 2*sizeof(void *); | |
287 | flags |= ZEROPAD; | |
288 | } | |
289 | str = number(str, | |
290 | (unsigned long) va_arg(args, void *), 16, | |
53a416d3 MM |
291 | field_width, precision, flags, size); |
292 | if (!str) | |
293 | return -1; | |
ecacdfa4 MM |
294 | continue; |
295 | ||
ecacdfa4 MM |
296 | case 'n': |
297 | if (qualifier == 'l') { | |
298 | long * ip = va_arg(args, long *); | |
299 | *ip = (str - buf); | |
300 | } else { | |
301 | int * ip = va_arg(args, int *); | |
302 | *ip = (str - buf); | |
303 | } | |
304 | continue; | |
305 | ||
306 | /* IP address */ | |
e691d16a OZ |
307 | case 'I': |
308 | if (fmt[1] == '4') { | |
309 | /* Explicit IPv4 address */ | |
310 | ip4_addr a = va_arg(args, ip4_addr); | |
311 | ip4_ntop(a, ipbuf); | |
312 | i = IP4_MAX_TEXT_LENGTH; | |
9a883adf | 313 | fmt++; |
e691d16a OZ |
314 | } else if (fmt[1] == '6') { |
315 | /* Explicit IPv6 address */ | |
316 | ip6_addr a = va_arg(args, ip6_addr); | |
317 | ip6_ntop(a, ipbuf); | |
318 | i = IP6_MAX_TEXT_LENGTH; | |
9a883adf | 319 | fmt++; |
e691d16a OZ |
320 | } else { |
321 | /* Just IP address */ | |
322 | ip_addr a = va_arg(args, ip_addr); | |
323 | ||
324 | if (ipa_is_ip4(a)) { | |
23c212e7 | 325 | ip4_ntop(ipa_to_ip4(a), ipbuf); |
e691d16a OZ |
326 | i = IP4_MAX_TEXT_LENGTH; |
327 | } else { | |
23c212e7 | 328 | ip6_ntop(ipa_to_ip6(a), ipbuf); |
e691d16a OZ |
329 | i = IP6_MAX_TEXT_LENGTH; |
330 | } | |
ecacdfa4 | 331 | } |
e691d16a | 332 | |
30b77304 | 333 | s = ipbuf; |
e691d16a OZ |
334 | if (field_width == 1) |
335 | field_width = i; | |
336 | ||
30b77304 | 337 | goto str; |
e691d16a | 338 | |
53ffbff3 OZ |
339 | /* Interface scope after link-local IP address */ |
340 | case 'J': | |
341 | iface = va_arg(args, struct iface *); | |
342 | if (!iface) | |
343 | continue; | |
344 | if (!size) | |
345 | return -1; | |
346 | ||
347 | *str++ = '%'; | |
348 | start++; | |
349 | size--; | |
350 | ||
351 | s = iface->name; | |
352 | goto str; | |
353 | ||
2f6483cd OZ |
354 | /* Router/Network ID - essentially IPv4 address in u32 value */ |
355 | case 'R': | |
af678af0 | 356 | if (qualifier == 'l') { |
937e75d8 OZ |
357 | X = va_arg(args, u64); |
358 | bsprintf(ipbuf, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x", | |
359 | ((X >> 56) & 0xff), | |
360 | ((X >> 48) & 0xff), | |
361 | ((X >> 40) & 0xff), | |
362 | ((X >> 32) & 0xff), | |
363 | ((X >> 24) & 0xff), | |
364 | ((X >> 16) & 0xff), | |
365 | ((X >> 8) & 0xff), | |
366 | (X & 0xff)); | |
367 | } | |
368 | else | |
369 | { | |
370 | x = va_arg(args, u32); | |
af678af0 | 371 | ip4_ntop(ip4_from_u32(x), ipbuf); |
937e75d8 | 372 | } |
2f6483cd OZ |
373 | s = ipbuf; |
374 | goto str; | |
375 | ||
49fc0213 OZ |
376 | case 't': |
377 | t = va_arg(args, btime); | |
378 | t1 = t TO_S; | |
379 | t2 = t - t1 S; | |
380 | ||
381 | if (precision < 0) | |
382 | precision = 3; | |
383 | ||
384 | if (precision > 6) | |
385 | precision = 6; | |
386 | ||
387 | /* Compute field_width for second part */ | |
388 | if ((precision > 0) && (field_width > 0)) | |
389 | field_width -= (1 + precision); | |
390 | ||
391 | if (field_width < 0) | |
392 | field_width = 0; | |
393 | ||
394 | /* Print seconds */ | |
395 | flags |= SIGN; | |
396 | str = number(str, t1, 10, field_width, 0, flags, size); | |
397 | if (!str) | |
398 | return -1; | |
399 | ||
400 | if (precision > 0) | |
401 | { | |
402 | size -= (str-start); | |
403 | start = str; | |
404 | ||
405 | if ((1 + precision) > size) | |
406 | return -1; | |
407 | ||
408 | /* Convert microseconds to requested precision */ | |
409 | for (i = precision; i < 6; i++) | |
410 | t2 /= 10; | |
411 | ||
412 | /* Print sub-seconds */ | |
413 | *str++ = '.'; | |
414 | str = number(str, t2, 10, precision, 0, ZEROPAD, size - 1); | |
415 | if (!str) | |
416 | return -1; | |
417 | } | |
418 | goto done; | |
419 | ||
ecacdfa4 MM |
420 | /* integer number formats - set up the flags and "break" */ |
421 | case 'o': | |
422 | base = 8; | |
423 | break; | |
424 | ||
425 | case 'X': | |
426 | flags |= LARGE; | |
d4cebc6b | 427 | /* fallthrough */ |
ecacdfa4 MM |
428 | case 'x': |
429 | base = 16; | |
430 | break; | |
431 | ||
432 | case 'd': | |
433 | case 'i': | |
434 | flags |= SIGN; | |
435 | case 'u': | |
436 | break; | |
437 | ||
438 | default: | |
53a416d3 MM |
439 | if (size < 2) |
440 | return -1; | |
ecacdfa4 MM |
441 | if (*fmt != '%') |
442 | *str++ = '%'; | |
443 | if (*fmt) | |
444 | *str++ = *fmt; | |
445 | else | |
446 | --fmt; | |
447 | continue; | |
448 | } | |
449 | if (qualifier == 'l') | |
450 | num = va_arg(args, unsigned long); | |
451 | else if (qualifier == 'h') { | |
4254dc45 | 452 | num = (unsigned short) va_arg(args, int); |
ecacdfa4 | 453 | if (flags & SIGN) |
4254dc45 | 454 | num = (short) num; |
ecacdfa4 MM |
455 | } else if (flags & SIGN) |
456 | num = va_arg(args, int); | |
457 | else | |
ae80a2de | 458 | num = va_arg(args, uint); |
53a416d3 MM |
459 | str = number(str, num, base, field_width, precision, flags, size); |
460 | if (!str) | |
461 | return -1; | |
49fc0213 | 462 | done: ; |
ecacdfa4 | 463 | } |
53a416d3 MM |
464 | if (!size) |
465 | return -1; | |
ecacdfa4 MM |
466 | *str = '\0'; |
467 | return str-buf; | |
468 | } | |
469 | ||
7722938d MM |
470 | /** |
471 | * bvsprintf - BIRD's vsprintf() | |
472 | * @buf: buffer | |
473 | * @fmt: format string | |
474 | * @args: a list of arguments to be formatted | |
475 | * | |
476 | * This function is equivalent to bvsnprintf() with an infinite | |
477 | * buffer size. Please use carefully only when you are absolutely | |
478 | * sure the buffer won't overflow. | |
479 | */ | |
53a416d3 MM |
480 | int bvsprintf(char *buf, const char *fmt, va_list args) |
481 | { | |
482 | return bvsnprintf(buf, 1000000000, fmt, args); | |
483 | } | |
484 | ||
7722938d MM |
485 | /** |
486 | * bsprintf - BIRD's sprintf() | |
487 | * @buf: buffer | |
488 | * @fmt: format string | |
489 | * | |
490 | * This function is equivalent to bvsnprintf() with an infinite | |
491 | * buffer size and variable arguments instead of a &va_list. | |
492 | * Please use carefully only when you are absolutely | |
493 | * sure the buffer won't overflow. | |
494 | */ | |
ecacdfa4 MM |
495 | int bsprintf(char * buf, const char *fmt, ...) |
496 | { | |
53a416d3 MM |
497 | va_list args; |
498 | int i; | |
ecacdfa4 | 499 | |
53a416d3 MM |
500 | va_start(args, fmt); |
501 | i=bvsnprintf(buf, 1000000000, fmt, args); | |
502 | va_end(args); | |
503 | return i; | |
504 | } | |
505 | ||
7722938d MM |
506 | /** |
507 | * bsnprintf - BIRD's snprintf() | |
508 | * @buf: buffer | |
509 | * @size: buffer size | |
510 | * @fmt: format string | |
511 | * | |
512 | * This function is equivalent to bsnprintf() with variable arguments instead of a &va_list. | |
513 | */ | |
53a416d3 MM |
514 | int bsnprintf(char * buf, int size, const char *fmt, ...) |
515 | { | |
516 | va_list args; | |
517 | int i; | |
518 | ||
519 | va_start(args, fmt); | |
520 | i=bvsnprintf(buf, size, fmt, args); | |
521 | va_end(args); | |
522 | return i; | |
ecacdfa4 | 523 | } |
0e175f9f OZ |
524 | |
525 | int | |
526 | buffer_vprint(buffer *buf, const char *fmt, va_list args) | |
527 | { | |
528 | int i = bvsnprintf((char *) buf->pos, buf->end - buf->pos, fmt, args); | |
77234bbb OZ |
529 | |
530 | if ((i < 0) && (buf->pos < buf->end)) | |
531 | *buf->pos = 0; | |
532 | ||
0e175f9f OZ |
533 | buf->pos = (i >= 0) ? (buf->pos + i) : buf->end; |
534 | return i; | |
535 | } | |
536 | ||
537 | int | |
538 | buffer_print(buffer *buf, const char *fmt, ...) | |
539 | { | |
540 | va_list args; | |
541 | int i; | |
542 | ||
543 | va_start(args, fmt); | |
77234bbb | 544 | i = bvsnprintf((char *) buf->pos, buf->end - buf->pos, fmt, args); |
0e175f9f OZ |
545 | va_end(args); |
546 | ||
77234bbb OZ |
547 | if ((i < 0) && (buf->pos < buf->end)) |
548 | *buf->pos = 0; | |
549 | ||
0e175f9f OZ |
550 | buf->pos = (i >= 0) ? (buf->pos + i) : buf->end; |
551 | return i; | |
552 | } | |
553 | ||
554 | void | |
555 | buffer_puts(buffer *buf, const char *str) | |
556 | { | |
557 | byte *bp = buf->pos; | |
77234bbb | 558 | byte *be = buf->end - 1; |
0e175f9f OZ |
559 | |
560 | while (bp < be && *str) | |
561 | *bp++ = *str++; | |
562 | ||
77234bbb | 563 | if (bp <= be) |
0e175f9f OZ |
564 | *bp = 0; |
565 | ||
77234bbb | 566 | buf->pos = (bp < be) ? bp : buf->end; |
0e175f9f | 567 | } |