aboutsummaryrefslogtreecommitdiffstats
path: root/com32/lib/vsnprintf.c
blob: 1d1d2a2fdfefc69d174f132045b6056843fc7150 (plain)
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
 * vsnprintf.c
 *
 * vsnprintf(), from which the rest of the printf()
 * family is built
 */

#include <stdarg.h>
#include <stddef.h>
#include <inttypes.h>
#include <string.h>
#include <limits.h>
#include <stdio.h>

enum flags {
    FL_ZERO = 0x01,		/* Zero modifier */
    FL_MINUS = 0x02,		/* Minus modifier */
    FL_PLUS = 0x04,		/* Plus modifier */
    FL_TICK = 0x08,		/* ' modifier */
    FL_SPACE = 0x10,		/* Space modifier */
    FL_HASH = 0x20,		/* # modifier */
    FL_SIGNED = 0x40,		/* Number is signed */
    FL_UPPER = 0x80		/* Upper case digits */
};

/* These may have to be adjusted on certain implementations */
enum ranks {
    rank_char = -2,
    rank_short = -1,
    rank_int = 0,
    rank_long = 1,
    rank_longlong = 2
};

#define MIN_RANK	rank_char
#define MAX_RANK	rank_longlong

#define INTMAX_RANK	rank_longlong
#define SIZE_T_RANK	rank_long
#define PTRDIFF_T_RANK	rank_long

#define EMIT(x) ({ if (o<n){*q++ = (x);} o++; })

static size_t
format_int(char *q, size_t n, uintmax_t val, enum flags flags,
	   int base, int width, int prec)
{
    char *qq;
    size_t o = 0, oo;
    static const char lcdigits[] = "0123456789abcdef";
    static const char ucdigits[] = "0123456789ABCDEF";
    const char *digits;
    uintmax_t tmpval;
    int minus = 0;
    int ndigits = 0, nchars;
    int tickskip, b4tick;

    /* Select type of digits */
    digits = (flags & FL_UPPER) ? ucdigits : lcdigits;

    /* If signed, separate out the minus */
    if (flags & FL_SIGNED && (intmax_t) val < 0) {
	minus = 1;
	val = (uintmax_t) (-(intmax_t) val);
    }

    /* Count the number of digits needed.  This returns zero for 0. */
    tmpval = val;
    while (tmpval) {
	tmpval /= base;
	ndigits++;
    }

    /* Adjust ndigits for size of output */

    if (flags & FL_HASH && base == 8) {
	if (prec < ndigits + 1)
	    prec = ndigits + 1;
    }

    if (ndigits < prec) {
	ndigits = prec;		/* Mandatory number padding */
    } else if (val == 0) {
	ndigits = 1;		/* Zero still requires space */
    }

    /* For ', figure out what the skip should be */
    if (flags & FL_TICK) {
	tickskip = (base == 16) ? 4 : 3;
    } else {
	tickskip = ndigits;	/* No tick marks */
    }

    /* Tick marks aren't digits, but generated by the number converter */
    ndigits += (ndigits - 1) / tickskip;

    /* Now compute the number of nondigits */
    nchars = ndigits;

    if (minus || (flags & (FL_PLUS | FL_SPACE)))
	nchars++;		/* Need space for sign */
    if ((flags & FL_HASH) && base == 16) {
	nchars += 2;		/* Add 0x for hex */
    }

    /* Emit early space padding */
    if (!(flags & (FL_MINUS | FL_ZERO)) && width > nchars) {
	while (width > nchars) {
	    EMIT(' ');
	    width--;
	}
    }

    /* Emit nondigits */
    if (minus)
	EMIT('-');
    else if (flags & FL_PLUS)
	EMIT('+');
    else if (flags & FL_SPACE)
	EMIT(' ');

    if ((flags & FL_HASH) && base == 16) {
	EMIT('0');
	EMIT((flags & FL_UPPER) ? 'X' : 'x');
    }

    /* Emit zero padding */
    if ((flags & (FL_MINUS | FL_ZERO)) == FL_ZERO && width > ndigits) {
	while (width > nchars) {
	    EMIT('0');
	    width--;
	}
    }

    /* Generate the number.  This is done from right to left. */
    q += ndigits;		/* Advance the pointer to end of number */
    o += ndigits;
    qq = q;
    oo = o;			/* Temporary values */

    b4tick = tickskip;
    while (ndigits > 0) {
	if (!b4tick--) {
	    qq--;
	    oo--;
	    ndigits--;
	    if (oo < n)
		*qq = '_';
	    b4tick = tickskip - 1;
	}
	qq--;
	oo--;
	ndigits--;
	if (oo < n)
	    *qq = digits[val % base];
	val /= base;
    }

    /* Emit late space padding */
    while ((flags & FL_MINUS) && width > nchars) {
	EMIT(' ');
	width--;
    }

    return o;
}

int vsnprintf(char *buffer, size_t n, const char *format, va_list ap)
{
    const char *p = format;
    char ch;
    char *q = buffer;
    size_t o = 0;		/* Number of characters output */
    uintmax_t val = 0;
    int rank = rank_int;	/* Default rank */
    int width = 0;
    int prec = -1;
    int base;
    size_t sz;
    enum flags flags = 0;
    enum {
	st_normal,		/* Ground state */
	st_flags,		/* Special flags */
	st_width,		/* Field width */
	st_prec,		/* Field precision */
	st_modifiers		/* Length or conversion modifiers */
    } state = st_normal;
    const char *sarg;		/* %s string argument */
    char carg;			/* %c char argument */
    int slen;			/* String length */

    while ((ch = *p++)) {
	switch (state) {
	case st_normal:
	    if (ch == '%') {
		state = st_flags;
		flags = 0;
		rank = rank_int;
		width = 0;
		prec = -1;
	    } else {
		EMIT(ch);
	    }
	    break;

	case st_flags:
	    switch (ch) {
	    case '-':
		flags |= FL_MINUS;
		break;
	    case '+':
		flags |= FL_PLUS;
		break;
	    case '\'':
		flags |= FL_TICK;
		break;
	    case ' ':
		flags |= FL_SPACE;
		break;
	    case '#':
		flags |= FL_HASH;
		break;
	    case '0':
		flags |= FL_ZERO;
		break;
	    default:
		state = st_width;
		p--;		/* Process this character again */
		break;
	    }
	    break;

	case st_width:
	    if (ch >= '0' && ch <= '9') {
		width = width * 10 + (ch - '0');
	    } else if (ch == '*') {
		width = va_arg(ap, int);
		if (width < 0) {
		    width = -width;
		    flags |= FL_MINUS;
		}
	    } else if (ch == '.') {
		prec = 0;	/* Precision given */
		state = st_prec;
	    } else {
		state = st_modifiers;
		p--;		/* Process this character again */
	    }
	    break;

	case st_prec:
	    if (ch >= '0' && ch <= '9') {
		prec = prec * 10 + (ch - '0');
	    } else if (ch == '*') {
		prec = va_arg(ap, int);
		if (prec < 0)
		    prec = -1;
	    } else {
		state = st_modifiers;
		p--;		/* Process this character again */
	    }
	    break;

	case st_modifiers:
	    switch (ch) {
		/* Length modifiers - nonterminal sequences */
	    case 'h':
		rank--;		/* Shorter rank */
		break;
	    case 'l':
		rank++;		/* Longer rank */
		break;
	    case 'j':
		rank = INTMAX_RANK;
		break;
	    case 'z':
		rank = SIZE_T_RANK;
		break;
	    case 't':
		rank = PTRDIFF_T_RANK;
		break;
	    case 'L':
	    case 'q':
		rank += 2;
		break;
	    default:
		/* Output modifiers - terminal sequences */
		state = st_normal;	/* Next state will be normal */
		if (rank < MIN_RANK)	/* Canonicalize rank */
		    rank = MIN_RANK;
		else if (rank > MAX_RANK)
		    rank = MAX_RANK;

		switch (ch) {
		case 'P':	/* Upper case pointer */
		    flags |= FL_UPPER;
		    /* fall through */
		case 'p':	/* Pointer */
		    base = 16;
		    prec = (CHAR_BIT * sizeof(void *) + 3) / 4;
		    flags |= FL_HASH;
		    val = (uintmax_t) (uintptr_t) va_arg(ap, void *);
		    goto is_integer;

		case 'd':	/* Signed decimal output */
		case 'i':
		    base = 10;
		    flags |= FL_SIGNED;
		    switch (rank) {
		    case rank_char:
			/* Yes, all these casts are needed... */
			val =
			    (uintmax_t) (intmax_t) (signed char)va_arg(ap,
								       signed
								       int);
			break;
		    case rank_short:
			val =
			    (uintmax_t) (intmax_t) (signed short)va_arg(ap,
									signed
									int);
			break;
		    case rank_int:
			val = (uintmax_t) (intmax_t) va_arg(ap, signed int);
			break;
		    case rank_long:
			val = (uintmax_t) (intmax_t) va_arg(ap, signed long);
			break;
		    case rank_longlong:
			val =
			    (uintmax_t) (intmax_t) va_arg(ap, signed long long);
			break;
		    }
		    goto is_integer;
		case 'o':	/* Octal */
		    base = 8;
		    goto is_unsigned;
		case 'u':	/* Unsigned decimal */
		    base = 10;
		    goto is_unsigned;
		case 'X':	/* Upper case hexadecimal */
		    flags |= FL_UPPER;
		    /* fall through */
		case 'x':	/* Hexadecimal */
		    base = 16;
		    goto is_unsigned;

is_unsigned:
		    switch (rank) {
		    case rank_char:
			val =
			    (uintmax_t) (unsigned char)va_arg(ap, unsigned int);
			break;
		    case rank_short:
			val =
			    (uintmax_t) (unsigned short)va_arg(ap,
							       unsigned int);
			break;
		    case rank_int:
			val = (uintmax_t) va_arg(ap, unsigned int);
			break;
		    case rank_long:
			val = (uintmax_t) va_arg(ap, unsigned long);
			break;
		    case rank_longlong:
			val = (uintmax_t) va_arg(ap, unsigned long long);
			break;
		    }
		    /* fall through */

is_integer:
		    sz = format_int(q, (o < n) ? n - o : 0, val, flags, base,
				    width, prec);
		    q += sz;
		    o += sz;
		    break;

		case 'c':	/* Character */
		    carg = (char)va_arg(ap, int);
		    sarg = &carg;
		    slen = 1;
		    goto is_string;
		case 's':	/* String */
		    sarg = va_arg(ap, const char *);
		    sarg = sarg ? sarg : "(null)";
		    slen = strlen(sarg);
		    goto is_string;

is_string:
		    {
			char sch;
			int i;

			if (prec != -1 && slen > prec)
			    slen = prec;

			if (width > slen && !(flags & FL_MINUS)) {
			    char pad = (flags & FL_ZERO) ? '0' : ' ';
			    while (width > slen) {
				EMIT(pad);
				width--;
			    }
			}
			for (i = slen; i; i--) {
			    sch = *sarg++;
			    EMIT(sch);
			}
			if (width > slen && (flags & FL_MINUS)) {
			    while (width > slen) {
				EMIT(' ');
				width--;
			    }
			}
		    }
		    break;

		case 'n':	/* Output the number of characters written */
		    {
			switch (rank) {
			case rank_char:
			    *va_arg(ap, signed char *) = o;
			    break;
			case rank_short:
			    *va_arg(ap, signed short *) = o;
			    break;
			case rank_int:
			    *va_arg(ap, signed int *) = o;
			    break;
			case rank_long:
			    *va_arg(ap, signed long *) = o;
			    break;
			case rank_longlong:
			    *va_arg(ap, signed long long *) = o;
			    break;
			}
		    }
		    break;

		default:	/* Anything else, including % */
		    EMIT(ch);
		    break;
		}
	    }
	}
    }

    /* Null-terminate the string */
    if (o < n)
	*q = '\0';		/* No overflow */
    else if (n > 0)
	buffer[n - 1] = '\0';	/* Overflow - terminate at end of buffer */

    return o;
}