Prevent buffer overflows in vsn(w)printf
[reactos.git] / reactos / ntoskrnl / rtl / sprintf.c
1 /* $Id: sprintf.c,v 1.10 2002/09/12 17:50:05 guido Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS kernel
5 * FILE: ntoskrnl/rtl/sprintf.c
6 * PURPOSE: Single byte sprintf functions
7 * PROGRAMMERS: David Welch
8 * Eric Kohl
9 *
10 */
11
12 /*
13 * linux/lib/vsprintf.c
14 *
15 * Copyright (C) 1991, 1992 Linus Torvalds
16 */
17
18 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
19 /*
20 * Wirzenius wrote this portably, Torvalds fucked it up :-)
21 */
22
23 #include <ddk/ntddk.h>
24 #include <stdarg.h>
25 #include <stdlib.h>
26 #include <ctype.h>
27 #include <string.h>
28 #include <limits.h>
29
30 #include <internal/debug.h>
31
32
33 #define ZEROPAD 1 /* pad with zero */
34 #define SIGN 2 /* unsigned/signed long */
35 #define PLUS 4 /* show plus */
36 #define SPACE 8 /* space if plus */
37 #define LEFT 16 /* left justified */
38 #define SPECIAL 32 /* 0x */
39 #define LARGE 64 /* use 'ABCDEF' instead of 'abcdef' */
40
41
42 #define do_div(n,base) ({ \
43 int __res; \
44 __res = ((unsigned long long) n) % (unsigned) base; \
45 n = ((unsigned long long) n) / (unsigned) base; \
46 __res; })
47
48
49 static int skip_atoi(const char **s)
50 {
51 int i=0;
52
53 while (isdigit(**s))
54 i = i*10 + *((*s)++) - '0';
55 return i;
56 }
57
58
59 static char *
60 number(char *buf, char *end, long long num, int base, int size, int precision, int type)
61 {
62 char c,sign,tmp[66];
63 const char *digits;
64 const char small_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
65 const char large_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
66 int i;
67
68 digits = (type & LARGE) ? large_digits : small_digits;
69 if (type & LEFT)
70 type &= ~ZEROPAD;
71 if (base < 2 || base > 36)
72 return 0;
73 c = (type & ZEROPAD) ? '0' : ' ';
74 sign = 0;
75 if (type & SIGN) {
76 if (num < 0) {
77 sign = '-';
78 num = -num;
79 size--;
80 } else if (type & PLUS) {
81 sign = '+';
82 size--;
83 } else if (type & SPACE) {
84 sign = ' ';
85 size--;
86 }
87 }
88 if (type & SPECIAL) {
89 if (base == 16)
90 size -= 2;
91 else if (base == 8)
92 size--;
93 }
94 i = 0;
95 if (num == 0)
96 tmp[i++]='0';
97 else while (num != 0)
98 tmp[i++] = digits[do_div(num,base)];
99 if (i > precision)
100 precision = i;
101 size -= precision;
102 if (!(type&(ZEROPAD+LEFT))) {
103 while(size-->0) {
104 if (buf <= end)
105 *buf = ' ';
106 ++buf;
107 }
108 }
109 if (sign) {
110 if (buf <= end)
111 *buf = sign;
112 ++buf;
113 }
114 if (type & SPECIAL) {
115 if (base==8) {
116 if (buf <= end)
117 *buf = '0';
118 ++buf;
119 } else if (base==16) {
120 if (buf <= end)
121 *buf = '0';
122 ++buf;
123 if (buf <= end)
124 *buf = digits[33];
125 ++buf;
126 }
127 }
128 if (!(type & LEFT)) {
129 while (size-- > 0) {
130 if (buf <= end)
131 *buf = c;
132 ++buf;
133 }
134 }
135 while (i < precision--) {
136 if (buf <= end)
137 *buf = '0';
138 ++buf;
139 }
140 while (i-- > 0) {
141 if (buf <= end)
142 *buf = tmp[i];
143 ++buf;
144 }
145 while (size-- > 0) {
146 if (buf <= end)
147 *buf = ' ';
148 ++buf;
149 }
150 return buf;
151 }
152
153
154 int _vsnprintf(char *buf, size_t cnt, const char *fmt, va_list args)
155 {
156 int len;
157 unsigned long long num;
158 int i, base;
159 char *str, *end;
160 const char *s;
161 const wchar_t *sw;
162
163 int flags; /* flags to number() */
164
165 int field_width; /* width of output field */
166 int precision; /* min. # of digits for integers; max
167 number of chars for from string */
168 int qualifier; /* 'h', 'l', 'L', 'I' or 'w' for integer fields */
169
170 str = buf;
171 end = buf + cnt - 1;
172 if (end < buf - 1) {
173 end = ((void *) -1);
174 cnt = end - buf + 1;
175 }
176
177 for ( ; *fmt ; ++fmt) {
178 if (*fmt != '%') {
179 if (str <= end)
180 *str = *fmt;
181 ++str;
182 continue;
183 }
184
185 /* process flags */
186 flags = 0;
187 repeat:
188 ++fmt; /* this also skips first '%' */
189 switch (*fmt) {
190 case '-': flags |= LEFT; goto repeat;
191 case '+': flags |= PLUS; goto repeat;
192 case ' ': flags |= SPACE; goto repeat;
193 case '#': flags |= SPECIAL; goto repeat;
194 case '0': flags |= ZEROPAD; goto repeat;
195 }
196
197 /* get field width */
198 field_width = -1;
199 if (isdigit(*fmt))
200 field_width = skip_atoi(&fmt);
201 else if (*fmt == '*') {
202 ++fmt;
203 /* it's the next argument */
204 field_width = va_arg(args, int);
205 if (field_width < 0) {
206 field_width = -field_width;
207 flags |= LEFT;
208 }
209 }
210
211 /* get the precision */
212 precision = -1;
213 if (*fmt == '.') {
214 ++fmt;
215 if (isdigit(*fmt))
216 precision = skip_atoi(&fmt);
217 else if (*fmt == '*') {
218 ++fmt;
219 /* it's the next argument */
220 precision = va_arg(args, int);
221 }
222 if (precision < 0)
223 precision = 0;
224 }
225
226 /* get the conversion qualifier */
227 qualifier = -1;
228 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt == 'w') {
229 qualifier = *fmt;
230 ++fmt;
231 } else if (*fmt == 'I' && *(fmt+1) == '6' && *(fmt+2) == '4') {
232 qualifier = *fmt;
233 fmt += 3;
234 }
235
236 /* default base */
237 base = 10;
238
239 switch (*fmt) {
240 case 'c': /* finished */
241 if (!(flags & LEFT))
242 while (--field_width > 0)
243 {
244 if (str <= end)
245 *str = ' ';
246 ++str;
247 }
248 if (qualifier == 'l' || qualifier == 'w')
249 {
250 if (str <= end)
251 *str = (unsigned char)(wchar_t) va_arg(args, int);
252 ++str;
253 }
254 else
255 {
256 if (str <= end)
257 *str = (unsigned char) va_arg(args, int);
258 ++str;
259 }
260 while (--field_width > 0)
261 {
262 if (str <= end)
263 *str = ' ';
264 ++str;
265 }
266 continue;
267
268 case 'C': /* finished */
269 if (!(flags & LEFT))
270 while (--field_width > 0)
271 {
272 if (str <= end)
273 *str = ' ';
274 ++str;
275 }
276 if (qualifier == 'h')
277 {
278 if (str <= end)
279 *str = (unsigned char) va_arg(args, int);
280 ++str;
281 }
282 else
283 {
284 if (str <= end)
285 *str = (unsigned char)(wchar_t) va_arg(args, int);
286 ++str;
287 }
288 while (--field_width > 0)
289 {
290 if (str <= end)
291 *str = ' ';
292 ++str;
293 }
294 continue;
295
296 case 's': /* finished */
297 if (qualifier == 'l' || qualifier == 'w') {
298 /* print unicode string */
299 sw = va_arg(args, wchar_t *);
300 if (sw == NULL)
301 sw = L"<NULL>";
302
303 for (len = 0; (unsigned int)len < (unsigned int)precision && sw[len]; len++);
304
305 if (!(flags & LEFT))
306 while (len < field_width--)
307 {
308 if (str <= end)
309 *str = ' ';
310 ++str;
311 }
312 for (i = 0; i < len; ++i)
313 {
314 if (str <= end)
315 *str = (unsigned char)(*sw);
316 ++str;
317 ++sw;
318 }
319 while (len < field_width--)
320 {
321 if (str <= end)
322 *str = ' ';
323 ++str;
324 }
325 } else {
326 /* print ascii string */
327 s = va_arg(args, char *);
328 if (s == NULL)
329 s = "<NULL>";
330
331 for (len = 0; (unsigned int)len < (unsigned int)precision && s[len]; len++);
332
333 if (!(flags & LEFT))
334 while (len < field_width--)
335 {
336 if (str <= end)
337 *str = ' ';
338 ++str;
339 }
340 for (i = 0; i < len; ++i)
341 {
342 if (str <= end)
343 *str = *s;
344 ++str;
345 ++s;
346 }
347 while (len < field_width--)
348 {
349 if (str <= end)
350 *str = ' ';
351 ++str;
352 }
353 }
354 continue;
355
356 case 'S':
357 if (qualifier == 'h') {
358 /* print ascii string */
359 s = va_arg(args, char *);
360 if (s == NULL)
361 s = "<NULL>";
362
363 for (len = 0; (unsigned int)len < (unsigned int)precision && s[len]; len++);
364
365 if (!(flags & LEFT))
366 while (len < field_width--)
367 {
368 if (str <= end)
369 *str = ' ';
370 ++str;
371 }
372 for (i = 0; i < len; ++i)
373 {
374 if (str <= end)
375 *str = *s;
376 ++str;
377 ++s;
378 }
379 while (len < field_width--)
380 {
381 if (str <= end)
382 *str = ' ';
383 ++str;
384 }
385 } else {
386 /* print unicode string */
387 sw = va_arg(args, wchar_t *);
388 if (sw == NULL)
389 sw = L"<NULL>";
390
391 for (len = 0; (unsigned int)len < (unsigned int)precision && sw[len]; len++);
392
393 if (!(flags & LEFT))
394 while (len < field_width--)
395 {
396 if (str <= end)
397 *str = ' ';
398 ++str;
399 }
400 for (i = 0; i < len; ++i)
401 {
402 if (str <= end)
403 *str = (unsigned char)(*sw);
404 ++str;
405 ++sw;
406 }
407 while (len < field_width--)
408 {
409 if (str <= end)
410 *str = ' ';
411 ++str;
412 }
413 }
414 continue;
415
416 case 'Z':
417 if (qualifier == 'w') {
418 /* print counted unicode string */
419 PUNICODE_STRING pus = va_arg(args, PUNICODE_STRING);
420 if ((pus == NULL) || (pus->Buffer == NULL)) {
421 s = "<NULL>";
422 while ((*s) != 0)
423 {
424 if (str <= end)
425 *str = *s;
426 ++str;
427 ++s;
428 }
429 } else {
430 for (i = 0; pus->Buffer[i] && i < pus->Length / sizeof(WCHAR); i++)
431 {
432 if (str <= end)
433 *str = (unsigned char)(pus->Buffer[i]);
434 ++str;
435 }
436 }
437 } else {
438 /* print counted ascii string */
439 PANSI_STRING pus = va_arg(args, PANSI_STRING);
440 if ((pus == NULL) || (pus->Buffer == NULL)) {
441 s = "<NULL>";
442 while ((*s) != 0)
443 {
444 if (str <= end)
445 *str = *s;
446 ++str;
447 ++s;
448 }
449 } else {
450 for (i = 0; pus->Buffer[i] && i < pus->Length; i++)
451 {
452 if (str <= end)
453 *str = pus->Buffer[i];
454 ++str;
455 }
456 }
457 }
458 continue;
459
460 case 'p':
461 if (field_width == -1) {
462 field_width = 2 * sizeof(void *);
463 flags |= ZEROPAD;
464 }
465 str = number(str, end,
466 (unsigned long) va_arg(args, void *),
467 16, field_width, precision, flags);
468 continue;
469
470 case 'n':
471 /* FIXME: What does C99 say about the overflow case here? */
472 if (qualifier == 'l') {
473 long * ip = va_arg(args, long *);
474 *ip = (str - buf);
475 } else {
476 int * ip = va_arg(args, int *);
477 *ip = (str - buf);
478 }
479 continue;
480
481 /* integer number formats - set up the flags and "break" */
482 case 'o':
483 base = 8;
484 break;
485
486 case 'b':
487 base = 2;
488 break;
489
490 case 'X':
491 flags |= LARGE;
492 case 'x':
493 base = 16;
494 break;
495
496 case 'd':
497 case 'i':
498 flags |= SIGN;
499 case 'u':
500 break;
501
502 default:
503 if (*fmt != '%')
504 {
505 if (str <= end)
506 *str = '%';
507 ++str;
508 }
509 if (*fmt)
510 {
511 if (str <= end)
512 *str = *fmt;
513 ++str;
514 }
515 else
516 --fmt;
517 continue;
518 }
519
520 if (qualifier == 'I')
521 num = va_arg(args, unsigned long long);
522 else if (qualifier == 'l')
523 num = va_arg(args, unsigned long);
524 else if (qualifier == 'h') {
525 if (flags & SIGN)
526 num = va_arg(args, int);
527 else
528 num = va_arg(args, unsigned int);
529 }
530 else {
531 if (flags & SIGN)
532 num = va_arg(args, int);
533 else
534 num = va_arg(args, unsigned int);
535 }
536 str = number(str, end, num, base, field_width, precision, flags);
537 }
538
539 if (str <= end)
540 *str = '\0';
541 else if (cnt > 0)
542 /* don't write out a null byte if the buf size is zero */
543 *end = '\0';
544
545 return str-buf;
546 }
547
548
549 int sprintf(char * buf, const char *fmt, ...)
550 {
551 va_list args;
552 int i;
553
554 va_start(args, fmt);
555 i=_vsnprintf(buf,INT_MAX,fmt,args);
556 va_end(args);
557 return i;
558 }
559
560
561 int _snprintf(char * buf, size_t cnt, const char *fmt, ...)
562 {
563 va_list args;
564 int i;
565
566 va_start(args, fmt);
567 i=_vsnprintf(buf,cnt,fmt,args);
568 va_end(args);
569 return i;
570 }
571
572
573 int vsprintf(char *buf, const char *fmt, va_list args)
574 {
575 return _vsnprintf(buf,INT_MAX,fmt,args);
576 }
577
578 /* EOF */