1 // SPDX-License-Identifier: GPL-2.0-only
4 #include "ucall_common.h"
6 #define APPEND_BUFFER_SAFE(str, end, v) \
8 GUEST_ASSERT(str < end); \
12 static int isdigit(int ch)
14 return (ch >= '0') && (ch <= '9');
17 static int skip_atoi(const char **s)
22 i = i * 10 + *((*s)++) - '0';
26 #define ZEROPAD 1 /* pad with zero */
27 #define SIGN 2 /* unsigned/signed long */
28 #define PLUS 4 /* show plus */
29 #define SPACE 8 /* space if plus */
30 #define LEFT 16 /* left justified */
31 #define SMALL 32 /* Must be 32 == 0x20 */
32 #define SPECIAL 64 /* 0x */
34 #define __do_div(n, base) \
38 __res = ((uint64_t) n) % (uint32_t) base; \
39 n = ((uint64_t) n) / (uint32_t) base; \
43 static char *number(char *str, const char *end, long num, int base, int size,
44 int precision, int type)
46 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
47 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
54 * locase = 0 or 0x20. ORing digits or letters with 'locase'
55 * produces same digits or (maybe lowercased) letters
57 locase = (type & SMALL);
60 if (base < 2 || base > 16)
62 c = (type & ZEROPAD) ? '0' : ' ';
69 } else if (type & PLUS) {
72 } else if (type & SPACE) {
88 tmp[i++] = (digits[__do_div(num, base)] | locase);
92 if (!(type & (ZEROPAD + LEFT)))
94 APPEND_BUFFER_SAFE(str, end, ' ');
96 APPEND_BUFFER_SAFE(str, end, sign);
99 APPEND_BUFFER_SAFE(str, end, '0');
100 else if (base == 16) {
101 APPEND_BUFFER_SAFE(str, end, '0');
102 APPEND_BUFFER_SAFE(str, end, 'x');
107 APPEND_BUFFER_SAFE(str, end, c);
108 while (i < precision--)
109 APPEND_BUFFER_SAFE(str, end, '0');
111 APPEND_BUFFER_SAFE(str, end, tmp[i]);
113 APPEND_BUFFER_SAFE(str, end, ' ');
118 int guest_vsnprintf(char *buf, int n, const char *fmt, va_list args)
126 int flags; /* flags to number() */
128 int field_width; /* width of output field */
130 * min. # of digits for integers; max
131 * number of chars for from string
133 int qualifier; /* 'h', 'l', or 'L' for integer fields */
136 GUEST_ASSERT(buf < end);
139 for (str = buf; *fmt; ++fmt) {
141 APPEND_BUFFER_SAFE(str, end, *fmt);
148 ++fmt; /* this also skips first '%' */
167 /* get field width */
170 field_width = skip_atoi(&fmt);
171 else if (*fmt == '*') {
173 /* it's the next argument */
174 field_width = va_arg(args, int);
175 if (field_width < 0) {
176 field_width = -field_width;
181 /* get the precision */
186 precision = skip_atoi(&fmt);
187 else if (*fmt == '*') {
189 /* it's the next argument */
190 precision = va_arg(args, int);
196 /* get the conversion qualifier */
198 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
209 while (--field_width > 0)
210 APPEND_BUFFER_SAFE(str, end, ' ');
211 APPEND_BUFFER_SAFE(str, end,
212 (uint8_t)va_arg(args, int));
213 while (--field_width > 0)
214 APPEND_BUFFER_SAFE(str, end, ' ');
218 s = va_arg(args, char *);
219 len = strnlen(s, precision);
222 while (len < field_width--)
223 APPEND_BUFFER_SAFE(str, end, ' ');
224 for (i = 0; i < len; ++i)
225 APPEND_BUFFER_SAFE(str, end, *s++);
226 while (len < field_width--)
227 APPEND_BUFFER_SAFE(str, end, ' ');
231 if (field_width == -1) {
232 field_width = 2 * sizeof(void *);
233 flags |= SPECIAL | SMALL | ZEROPAD;
235 str = number(str, end,
236 (uint64_t)va_arg(args, void *), 16,
237 field_width, precision, flags);
241 if (qualifier == 'l') {
242 long *ip = va_arg(args, long *);
245 int *ip = va_arg(args, int *);
251 APPEND_BUFFER_SAFE(str, end, '%');
254 /* integer number formats - set up the flags and "break" */
272 APPEND_BUFFER_SAFE(str, end, '%');
274 APPEND_BUFFER_SAFE(str, end, *fmt);
279 if (qualifier == 'l')
280 num = va_arg(args, uint64_t);
281 else if (qualifier == 'h') {
282 num = (uint16_t)va_arg(args, int);
285 } else if (flags & SIGN)
286 num = va_arg(args, int);
288 num = va_arg(args, uint32_t);
289 str = number(str, end, num, base, field_width, precision, flags);
292 GUEST_ASSERT(str < end);
297 int guest_snprintf(char *buf, int n, const char *fmt, ...)
303 len = guest_vsnprintf(buf, n, fmt, va);