]> bbs.cooldavid.org Git - net-next-2.6.git/blame - lib/vsprintf.c
softirq: add BLOCK_IOPOLL to softirq_to_name
[net-next-2.6.git] / lib / vsprintf.c
CommitLineData
1da177e4
LT
1/*
2 * linux/lib/vsprintf.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8/*
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
10 */
11
12/*
13 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
14 * - changed to provide snprintf and vsnprintf functions
15 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
16 * - scnprintf and vscnprintf
17 */
18
19#include <stdarg.h>
20#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/string.h>
23#include <linux/ctype.h>
24#include <linux/kernel.h>
0fe1ef24
LT
25#include <linux/kallsyms.h>
26#include <linux/uaccess.h>
332d2e78 27#include <linux/ioport.h>
8a27f7c9 28#include <net/addrconf.h>
1da177e4 29
4e57b681 30#include <asm/page.h> /* for PAGE_SIZE */
1da177e4 31#include <asm/div64.h>
deac93df 32#include <asm/sections.h> /* for dereference_function_descriptor() */
1da177e4 33
9b706aee
DV
34/* Works only for digits and letters, but small and fast */
35#define TOLOWER(x) ((x) | 0x20)
36
aa46a63e
HH
37static unsigned int simple_guess_base(const char *cp)
38{
39 if (cp[0] == '0') {
40 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
41 return 16;
42 else
43 return 8;
44 } else {
45 return 10;
46 }
47}
48
1da177e4
LT
49/**
50 * simple_strtoul - convert a string to an unsigned long
51 * @cp: The start of the string
52 * @endp: A pointer to the end of the parsed string will be placed here
53 * @base: The number base to use
54 */
22d27051 55unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
1da177e4 56{
aa46a63e
HH
57 unsigned long result = 0;
58
59 if (!base)
60 base = simple_guess_base(cp);
61
62 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
63 cp += 2;
64
65 while (isxdigit(*cp)) {
66 unsigned int value;
67
68 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
69 if (value >= base)
70 break;
71 result = result * base + value;
1da177e4
LT
72 cp++;
73 }
aa46a63e 74
1da177e4
LT
75 if (endp)
76 *endp = (char *)cp;
77 return result;
78}
1da177e4
LT
79EXPORT_SYMBOL(simple_strtoul);
80
81/**
82 * simple_strtol - convert a string to a signed long
83 * @cp: The start of the string
84 * @endp: A pointer to the end of the parsed string will be placed here
85 * @base: The number base to use
86 */
22d27051 87long simple_strtol(const char *cp, char **endp, unsigned int base)
1da177e4 88{
22d27051
HH
89 if(*cp == '-')
90 return -simple_strtoul(cp + 1, endp, base);
91 return simple_strtoul(cp, endp, base);
1da177e4 92}
1da177e4
LT
93EXPORT_SYMBOL(simple_strtol);
94
95/**
96 * simple_strtoull - convert a string to an unsigned long long
97 * @cp: The start of the string
98 * @endp: A pointer to the end of the parsed string will be placed here
99 * @base: The number base to use
100 */
22d27051 101unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
1da177e4 102{
aa46a63e
HH
103 unsigned long long result = 0;
104
105 if (!base)
106 base = simple_guess_base(cp);
107
108 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
109 cp += 2;
110
111 while (isxdigit(*cp)) {
112 unsigned int value;
113
114 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
115 if (value >= base)
116 break;
117 result = result * base + value;
1da177e4
LT
118 cp++;
119 }
aa46a63e 120
1da177e4
LT
121 if (endp)
122 *endp = (char *)cp;
123 return result;
124}
1da177e4
LT
125EXPORT_SYMBOL(simple_strtoull);
126
127/**
128 * simple_strtoll - convert a string to a signed long long
129 * @cp: The start of the string
130 * @endp: A pointer to the end of the parsed string will be placed here
131 * @base: The number base to use
132 */
22d27051 133long long simple_strtoll(const char *cp, char **endp, unsigned int base)
1da177e4
LT
134{
135 if(*cp=='-')
22d27051
HH
136 return -simple_strtoull(cp + 1, endp, base);
137 return simple_strtoull(cp, endp, base);
1da177e4
LT
138}
139
06b2a76d
YY
140/**
141 * strict_strtoul - convert a string to an unsigned long strictly
142 * @cp: The string to be converted
143 * @base: The number base to use
144 * @res: The converted result value
145 *
146 * strict_strtoul converts a string to an unsigned long only if the
147 * string is really an unsigned long string, any string containing
148 * any invalid char at the tail will be rejected and -EINVAL is returned,
149 * only a newline char at the tail is acceptible because people generally
150 * change a module parameter in the following way:
151 *
152 * echo 1024 > /sys/module/e1000/parameters/copybreak
153 *
154 * echo will append a newline to the tail.
155 *
156 * It returns 0 if conversion is successful and *res is set to the converted
157 * value, otherwise it returns -EINVAL and *res is set to 0.
158 *
159 * simple_strtoul just ignores the successive invalid characters and
160 * return the converted value of prefix part of the string.
161 */
9d85db22
HH
162int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
163{
164 char *tail;
165 unsigned long val;
166 size_t len;
167
168 *res = 0;
169 len = strlen(cp);
170 if (len == 0)
171 return -EINVAL;
172
173 val = simple_strtoul(cp, &tail, base);
e899aa82
PM
174 if (tail == cp)
175 return -EINVAL;
9d85db22
HH
176 if ((*tail == '\0') ||
177 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
178 *res = val;
179 return 0;
180 }
181
182 return -EINVAL;
183}
184EXPORT_SYMBOL(strict_strtoul);
06b2a76d
YY
185
186/**
187 * strict_strtol - convert a string to a long strictly
188 * @cp: The string to be converted
189 * @base: The number base to use
190 * @res: The converted result value
191 *
192 * strict_strtol is similiar to strict_strtoul, but it allows the first
193 * character of a string is '-'.
194 *
195 * It returns 0 if conversion is successful and *res is set to the converted
196 * value, otherwise it returns -EINVAL and *res is set to 0.
197 */
9d85db22
HH
198int strict_strtol(const char *cp, unsigned int base, long *res)
199{
200 int ret;
201 if (*cp == '-') {
202 ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
203 if (!ret)
204 *res = -(*res);
205 } else {
206 ret = strict_strtoul(cp, base, (unsigned long *)res);
207 }
208
209 return ret;
210}
211EXPORT_SYMBOL(strict_strtol);
06b2a76d
YY
212
213/**
214 * strict_strtoull - convert a string to an unsigned long long strictly
215 * @cp: The string to be converted
216 * @base: The number base to use
217 * @res: The converted result value
218 *
219 * strict_strtoull converts a string to an unsigned long long only if the
220 * string is really an unsigned long long string, any string containing
221 * any invalid char at the tail will be rejected and -EINVAL is returned,
222 * only a newline char at the tail is acceptible because people generally
223 * change a module parameter in the following way:
224 *
225 * echo 1024 > /sys/module/e1000/parameters/copybreak
226 *
227 * echo will append a newline to the tail of the string.
228 *
229 * It returns 0 if conversion is successful and *res is set to the converted
230 * value, otherwise it returns -EINVAL and *res is set to 0.
231 *
232 * simple_strtoull just ignores the successive invalid characters and
233 * return the converted value of prefix part of the string.
234 */
9d85db22
HH
235int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
236{
237 char *tail;
238 unsigned long long val;
239 size_t len;
240
241 *res = 0;
242 len = strlen(cp);
243 if (len == 0)
244 return -EINVAL;
245
246 val = simple_strtoull(cp, &tail, base);
e899aa82
PM
247 if (tail == cp)
248 return -EINVAL;
9d85db22
HH
249 if ((*tail == '\0') ||
250 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
251 *res = val;
252 return 0;
253 }
254
255 return -EINVAL;
256}
257EXPORT_SYMBOL(strict_strtoull);
06b2a76d
YY
258
259/**
260 * strict_strtoll - convert a string to a long long strictly
261 * @cp: The string to be converted
262 * @base: The number base to use
263 * @res: The converted result value
264 *
265 * strict_strtoll is similiar to strict_strtoull, but it allows the first
266 * character of a string is '-'.
267 *
268 * It returns 0 if conversion is successful and *res is set to the converted
269 * value, otherwise it returns -EINVAL and *res is set to 0.
270 */
9d85db22
HH
271int strict_strtoll(const char *cp, unsigned int base, long long *res)
272{
273 int ret;
274 if (*cp == '-') {
275 ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
276 if (!ret)
277 *res = -(*res);
278 } else {
279 ret = strict_strtoull(cp, base, (unsigned long long *)res);
280 }
06b2a76d 281
9d85db22
HH
282 return ret;
283}
06b2a76d 284EXPORT_SYMBOL(strict_strtoll);
06b2a76d 285
1da177e4
LT
286static int skip_atoi(const char **s)
287{
288 int i=0;
289
290 while (isdigit(**s))
291 i = i*10 + *((*s)++) - '0';
292 return i;
293}
294
4277eedd
DV
295/* Decimal conversion is by far the most typical, and is used
296 * for /proc and /sys data. This directly impacts e.g. top performance
297 * with many processes running. We optimize it for speed
298 * using code from
299 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
300 * (with permission from the author, Douglas W. Jones). */
301
302/* Formats correctly any integer in [0,99999].
303 * Outputs from one to five digits depending on input.
304 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
305static char* put_dec_trunc(char *buf, unsigned q)
306{
307 unsigned d3, d2, d1, d0;
308 d1 = (q>>4) & 0xf;
309 d2 = (q>>8) & 0xf;
310 d3 = (q>>12);
311
312 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
313 q = (d0 * 0xcd) >> 11;
314 d0 = d0 - 10*q;
315 *buf++ = d0 + '0'; /* least significant digit */
316 d1 = q + 9*d3 + 5*d2 + d1;
317 if (d1 != 0) {
318 q = (d1 * 0xcd) >> 11;
319 d1 = d1 - 10*q;
320 *buf++ = d1 + '0'; /* next digit */
321
322 d2 = q + 2*d2;
323 if ((d2 != 0) || (d3 != 0)) {
324 q = (d2 * 0xd) >> 7;
325 d2 = d2 - 10*q;
326 *buf++ = d2 + '0'; /* next digit */
327
328 d3 = q + 4*d3;
329 if (d3 != 0) {
330 q = (d3 * 0xcd) >> 11;
331 d3 = d3 - 10*q;
332 *buf++ = d3 + '0'; /* next digit */
333 if (q != 0)
334 *buf++ = q + '0'; /* most sign. digit */
335 }
336 }
337 }
338 return buf;
339}
340/* Same with if's removed. Always emits five digits */
341static char* put_dec_full(char *buf, unsigned q)
342{
343 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
344 /* but anyway, gcc produces better code with full-sized ints */
345 unsigned d3, d2, d1, d0;
346 d1 = (q>>4) & 0xf;
347 d2 = (q>>8) & 0xf;
348 d3 = (q>>12);
349
350 /* Possible ways to approx. divide by 10 */
351 /* gcc -O2 replaces multiply with shifts and adds */
352 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
353 // (x * 0x67) >> 10: 1100111
354 // (x * 0x34) >> 9: 110100 - same
355 // (x * 0x1a) >> 8: 11010 - same
356 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
357
358 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
359 q = (d0 * 0xcd) >> 11;
360 d0 = d0 - 10*q;
361 *buf++ = d0 + '0';
362 d1 = q + 9*d3 + 5*d2 + d1;
363 q = (d1 * 0xcd) >> 11;
364 d1 = d1 - 10*q;
365 *buf++ = d1 + '0';
366
367 d2 = q + 2*d2;
368 q = (d2 * 0xd) >> 7;
369 d2 = d2 - 10*q;
370 *buf++ = d2 + '0';
371
372 d3 = q + 4*d3;
373 q = (d3 * 0xcd) >> 11; /* - shorter code */
374 /* q = (d3 * 0x67) >> 10; - would also work */
375 d3 = d3 - 10*q;
376 *buf++ = d3 + '0';
377 *buf++ = q + '0';
378 return buf;
379}
380/* No inlining helps gcc to use registers better */
381static noinline char* put_dec(char *buf, unsigned long long num)
382{
383 while (1) {
384 unsigned rem;
385 if (num < 100000)
386 return put_dec_trunc(buf, num);
387 rem = do_div(num, 100000);
388 buf = put_dec_full(buf, rem);
389 }
390}
391
1da177e4
LT
392#define ZEROPAD 1 /* pad with zero */
393#define SIGN 2 /* unsigned/signed long */
394#define PLUS 4 /* show plus */
395#define SPACE 8 /* space if plus */
396#define LEFT 16 /* left justified */
9b706aee
DV
397#define SMALL 32 /* Must be 32 == 0x20 */
398#define SPECIAL 64 /* 0x */
1da177e4 399
fef20d9c
FW
400enum format_type {
401 FORMAT_TYPE_NONE, /* Just a string part */
ed681a91 402 FORMAT_TYPE_WIDTH,
fef20d9c
FW
403 FORMAT_TYPE_PRECISION,
404 FORMAT_TYPE_CHAR,
405 FORMAT_TYPE_STR,
406 FORMAT_TYPE_PTR,
407 FORMAT_TYPE_PERCENT_CHAR,
408 FORMAT_TYPE_INVALID,
409 FORMAT_TYPE_LONG_LONG,
410 FORMAT_TYPE_ULONG,
411 FORMAT_TYPE_LONG,
a4e94ef0
Z
412 FORMAT_TYPE_UBYTE,
413 FORMAT_TYPE_BYTE,
fef20d9c
FW
414 FORMAT_TYPE_USHORT,
415 FORMAT_TYPE_SHORT,
416 FORMAT_TYPE_UINT,
417 FORMAT_TYPE_INT,
418 FORMAT_TYPE_NRCHARS,
419 FORMAT_TYPE_SIZE_T,
420 FORMAT_TYPE_PTRDIFF
421};
422
423struct printf_spec {
424 enum format_type type;
425 int flags; /* flags to number() */
426 int field_width; /* width of output field */
427 int base;
428 int precision; /* # of digits/chars */
429 int qualifier;
430};
431
432static char *number(char *buf, char *end, unsigned long long num,
433 struct printf_spec spec)
1da177e4 434{
9b706aee
DV
435 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
436 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
437
438 char tmp[66];
439 char sign;
440 char locase;
fef20d9c 441 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
1da177e4
LT
442 int i;
443
9b706aee
DV
444 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
445 * produces same digits or (maybe lowercased) letters */
fef20d9c
FW
446 locase = (spec.flags & SMALL);
447 if (spec.flags & LEFT)
448 spec.flags &= ~ZEROPAD;
1da177e4 449 sign = 0;
fef20d9c 450 if (spec.flags & SIGN) {
1da177e4
LT
451 if ((signed long long) num < 0) {
452 sign = '-';
453 num = - (signed long long) num;
fef20d9c
FW
454 spec.field_width--;
455 } else if (spec.flags & PLUS) {
1da177e4 456 sign = '+';
fef20d9c
FW
457 spec.field_width--;
458 } else if (spec.flags & SPACE) {
1da177e4 459 sign = ' ';
fef20d9c 460 spec.field_width--;
1da177e4
LT
461 }
462 }
b39a7340 463 if (need_pfx) {
fef20d9c
FW
464 spec.field_width--;
465 if (spec.base == 16)
466 spec.field_width--;
1da177e4 467 }
b39a7340
DV
468
469 /* generate full string in tmp[], in reverse order */
1da177e4
LT
470 i = 0;
471 if (num == 0)
b39a7340 472 tmp[i++] = '0';
4277eedd
DV
473 /* Generic code, for any base:
474 else do {
9b706aee 475 tmp[i++] = (digits[do_div(num,base)] | locase);
4277eedd
DV
476 } while (num != 0);
477 */
fef20d9c
FW
478 else if (spec.base != 10) { /* 8 or 16 */
479 int mask = spec.base - 1;
b39a7340 480 int shift = 3;
fef20d9c 481 if (spec.base == 16) shift = 4;
b39a7340 482 do {
9b706aee 483 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
b39a7340
DV
484 num >>= shift;
485 } while (num);
4277eedd
DV
486 } else { /* base 10 */
487 i = put_dec(tmp, num) - tmp;
488 }
b39a7340
DV
489
490 /* printing 100 using %2d gives "100", not "00" */
fef20d9c
FW
491 if (i > spec.precision)
492 spec.precision = i;
b39a7340 493 /* leading space padding */
fef20d9c
FW
494 spec.field_width -= spec.precision;
495 if (!(spec.flags & (ZEROPAD+LEFT))) {
496 while(--spec.field_width >= 0) {
f796937a 497 if (buf < end)
1da177e4
LT
498 *buf = ' ';
499 ++buf;
500 }
501 }
b39a7340 502 /* sign */
1da177e4 503 if (sign) {
f796937a 504 if (buf < end)
1da177e4
LT
505 *buf = sign;
506 ++buf;
507 }
b39a7340
DV
508 /* "0x" / "0" prefix */
509 if (need_pfx) {
510 if (buf < end)
511 *buf = '0';
512 ++buf;
fef20d9c 513 if (spec.base == 16) {
f796937a 514 if (buf < end)
9b706aee 515 *buf = ('X' | locase);
1da177e4
LT
516 ++buf;
517 }
518 }
b39a7340 519 /* zero or space padding */
fef20d9c
FW
520 if (!(spec.flags & LEFT)) {
521 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
522 while (--spec.field_width >= 0) {
f796937a 523 if (buf < end)
1da177e4
LT
524 *buf = c;
525 ++buf;
526 }
527 }
b39a7340 528 /* hmm even more zero padding? */
fef20d9c 529 while (i <= --spec.precision) {
f796937a 530 if (buf < end)
1da177e4
LT
531 *buf = '0';
532 ++buf;
533 }
b39a7340
DV
534 /* actual digits of result */
535 while (--i >= 0) {
f796937a 536 if (buf < end)
1da177e4
LT
537 *buf = tmp[i];
538 ++buf;
539 }
b39a7340 540 /* trailing space padding */
fef20d9c 541 while (--spec.field_width >= 0) {
f796937a 542 if (buf < end)
1da177e4
LT
543 *buf = ' ';
544 ++buf;
545 }
546 return buf;
547}
548
fef20d9c 549static char *string(char *buf, char *end, char *s, struct printf_spec spec)
0f9bfa56
LT
550{
551 int len, i;
552
553 if ((unsigned long)s < PAGE_SIZE)
554 s = "<NULL>";
555
fef20d9c 556 len = strnlen(s, spec.precision);
0f9bfa56 557
fef20d9c
FW
558 if (!(spec.flags & LEFT)) {
559 while (len < spec.field_width--) {
0f9bfa56
LT
560 if (buf < end)
561 *buf = ' ';
562 ++buf;
563 }
564 }
565 for (i = 0; i < len; ++i) {
566 if (buf < end)
567 *buf = *s;
568 ++buf; ++s;
569 }
fef20d9c 570 while (len < spec.field_width--) {
0f9bfa56
LT
571 if (buf < end)
572 *buf = ' ';
573 ++buf;
574 }
575 return buf;
576}
577
fef20d9c 578static char *symbol_string(char *buf, char *end, void *ptr,
0c8b946e 579 struct printf_spec spec, char ext)
0fe1ef24
LT
580{
581 unsigned long value = (unsigned long) ptr;
582#ifdef CONFIG_KALLSYMS
583 char sym[KSYM_SYMBOL_LEN];
91adcd2c 584 if (ext != 'f' && ext != 's')
0c8b946e
FW
585 sprint_symbol(sym, value);
586 else
587 kallsyms_lookup(value, NULL, NULL, NULL, sym);
fef20d9c 588 return string(buf, end, sym, spec);
0fe1ef24 589#else
fef20d9c
FW
590 spec.field_width = 2*sizeof(void *);
591 spec.flags |= SPECIAL | SMALL | ZEROPAD;
592 spec.base = 16;
593 return number(buf, end, value, spec);
0fe1ef24
LT
594#endif
595}
596
fef20d9c
FW
597static char *resource_string(char *buf, char *end, struct resource *res,
598 struct printf_spec spec)
332d2e78
LT
599{
600#ifndef IO_RSRC_PRINTK_SIZE
601#define IO_RSRC_PRINTK_SIZE 4
602#endif
603
604#ifndef MEM_RSRC_PRINTK_SIZE
605#define MEM_RSRC_PRINTK_SIZE 8
606#endif
fef20d9c
FW
607 struct printf_spec num_spec = {
608 .base = 16,
609 .precision = -1,
610 .flags = SPECIAL | SMALL | ZEROPAD,
611 };
332d2e78
LT
612 /* room for the actual numbers, the two "0x", -, [, ] and the final zero */
613 char sym[4*sizeof(resource_size_t) + 8];
614 char *p = sym, *pend = sym + sizeof(sym);
615 int size = -1;
616
617 if (res->flags & IORESOURCE_IO)
618 size = IO_RSRC_PRINTK_SIZE;
619 else if (res->flags & IORESOURCE_MEM)
620 size = MEM_RSRC_PRINTK_SIZE;
621
622 *p++ = '[';
fef20d9c
FW
623 num_spec.field_width = size;
624 p = number(p, pend, res->start, num_spec);
332d2e78 625 *p++ = '-';
fef20d9c 626 p = number(p, pend, res->end, num_spec);
332d2e78
LT
627 *p++ = ']';
628 *p = 0;
629
fef20d9c 630 return string(buf, end, sym, spec);
332d2e78
LT
631}
632
fef20d9c 633static char *mac_address_string(char *buf, char *end, u8 *addr,
8a27f7c9 634 struct printf_spec spec, const char *fmt)
dd45c9cf 635{
8a27f7c9 636 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
dd45c9cf
HH
637 char *p = mac_addr;
638 int i;
639
640 for (i = 0; i < 6; i++) {
641 p = pack_hex_byte(p, addr[i]);
8a27f7c9 642 if (fmt[0] == 'M' && i != 5)
dd45c9cf
HH
643 *p++ = ':';
644 }
645 *p = '\0';
646
fef20d9c 647 return string(buf, end, mac_addr, spec);
dd45c9cf
HH
648}
649
8a27f7c9
JP
650static char *ip4_string(char *p, const u8 *addr, bool leading_zeros)
651{
652 int i;
653
654 for (i = 0; i < 4; i++) {
655 char temp[3]; /* hold each IP quad in reverse order */
656 int digits = put_dec_trunc(temp, addr[i]) - temp;
657 if (leading_zeros) {
658 if (digits < 3)
659 *p++ = '0';
660 if (digits < 2)
661 *p++ = '0';
662 }
663 /* reverse the digits in the quad */
664 while (digits--)
665 *p++ = temp[digits];
666 if (i < 3)
667 *p++ = '.';
668 }
669
670 *p = '\0';
671 return p;
672}
673
674static char *ip6_compressed_string(char *p, const struct in6_addr *addr)
689afa7d 675{
689afa7d 676 int i;
8a27f7c9
JP
677 int j;
678 int range;
679 unsigned char zerolength[8];
680 int longest = 1;
681 int colonpos = -1;
682 u16 word;
683 u8 hi;
684 u8 lo;
685 bool needcolon = false;
686 bool useIPv4 = ipv6_addr_v4mapped(addr) || ipv6_addr_is_isatap(addr);
687
688 memset(zerolength, 0, sizeof(zerolength));
689
690 if (useIPv4)
691 range = 6;
692 else
693 range = 8;
694
695 /* find position of longest 0 run */
696 for (i = 0; i < range; i++) {
697 for (j = i; j < range; j++) {
698 if (addr->s6_addr16[j] != 0)
699 break;
700 zerolength[i]++;
701 }
702 }
703 for (i = 0; i < range; i++) {
704 if (zerolength[i] > longest) {
705 longest = zerolength[i];
706 colonpos = i;
707 }
708 }
689afa7d 709
8a27f7c9
JP
710 /* emit address */
711 for (i = 0; i < range; i++) {
712 if (i == colonpos) {
713 if (needcolon || i == 0)
714 *p++ = ':';
715 *p++ = ':';
716 needcolon = false;
717 i += longest - 1;
718 continue;
719 }
720 if (needcolon) {
721 *p++ = ':';
722 needcolon = false;
723 }
724 /* hex u16 without leading 0s */
725 word = ntohs(addr->s6_addr16[i]);
726 hi = word >> 8;
727 lo = word & 0xff;
728 if (hi) {
729 if (hi > 0x0f)
730 p = pack_hex_byte(p, hi);
731 else
732 *p++ = hex_asc_lo(hi);
733 }
734 if (hi || lo > 0x0f)
735 p = pack_hex_byte(p, lo);
736 else
737 *p++ = hex_asc_lo(lo);
738 needcolon = true;
739 }
740
741 if (useIPv4) {
742 if (needcolon)
743 *p++ = ':';
744 p = ip4_string(p, &addr->s6_addr[12], false);
745 }
746
747 *p = '\0';
748 return p;
749}
750
751static char *ip6_string(char *p, const struct in6_addr *addr, const char *fmt)
752{
753 int i;
689afa7d 754 for (i = 0; i < 8; i++) {
8a27f7c9
JP
755 p = pack_hex_byte(p, addr->s6_addr[2 * i]);
756 p = pack_hex_byte(p, addr->s6_addr[2 * i + 1]);
757 if (fmt[0] == 'I' && i != 7)
689afa7d
HH
758 *p++ = ':';
759 }
8a27f7c9 760
689afa7d 761 *p = '\0';
8a27f7c9
JP
762 return p;
763}
764
765static char *ip6_addr_string(char *buf, char *end, const u8 *addr,
766 struct printf_spec spec, const char *fmt)
767{
768 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
769
770 if (fmt[0] == 'I' && fmt[2] == 'c')
771 ip6_compressed_string(ip6_addr, (const struct in6_addr *)addr);
772 else
773 ip6_string(ip6_addr, (const struct in6_addr *)addr, fmt);
689afa7d 774
fef20d9c 775 return string(buf, end, ip6_addr, spec);
689afa7d
HH
776}
777
8a27f7c9
JP
778static char *ip4_addr_string(char *buf, char *end, const u8 *addr,
779 struct printf_spec spec, const char *fmt)
4aa99606 780{
8a27f7c9 781 char ip4_addr[sizeof("255.255.255.255")];
4aa99606 782
8a27f7c9 783 ip4_string(ip4_addr, addr, fmt[0] == 'i');
4aa99606 784
fef20d9c 785 return string(buf, end, ip4_addr, spec);
4aa99606
HH
786}
787
4d8a743c
LT
788/*
789 * Show a '%p' thing. A kernel extension is that the '%p' is followed
790 * by an extra set of alphanumeric characters that are extended format
791 * specifiers.
792 *
332d2e78
LT
793 * Right now we handle:
794 *
0c8b946e
FW
795 * - 'F' For symbolic function descriptor pointers with offset
796 * - 'f' For simple symbolic function names without offset
332d2e78
LT
797 * - 'S' For symbolic direct pointers
798 * - 'R' For a struct resource pointer, it prints the range of
799 * addresses (not the name nor the flags)
dd45c9cf
HH
800 * - 'M' For a 6-byte MAC address, it prints the address in the
801 * usual colon-separated hex notation
8a27f7c9
JP
802 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
803 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
804 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
805 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
806 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
807 * IPv6 omits the colons (01020304...0f)
808 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
809 * - 'I6c' for IPv6 addresses printed as specified by
810 * http://www.ietf.org/id/draft-kawamura-ipv6-text-representation-03.txt
332d2e78
LT
811 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
812 * function pointers are really function descriptors, which contain a
813 * pointer to the real address.
4d8a743c 814 */
fef20d9c
FW
815static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
816 struct printf_spec spec)
78a8bf69 817{
d97106ab 818 if (!ptr)
fef20d9c 819 return string(buf, end, "(null)", spec);
d97106ab 820
0fe1ef24
LT
821 switch (*fmt) {
822 case 'F':
0c8b946e 823 case 'f':
0fe1ef24 824 ptr = dereference_function_descriptor(ptr);
91adcd2c 825 case 's':
0fe1ef24
LT
826 /* Fallthrough */
827 case 'S':
0c8b946e 828 return symbol_string(buf, end, ptr, spec, *fmt);
332d2e78 829 case 'R':
fef20d9c 830 return resource_string(buf, end, ptr, spec);
8a27f7c9
JP
831 case 'M': /* Colon separated: 00:01:02:03:04:05 */
832 case 'm': /* Contiguous: 000102030405 */
833 return mac_address_string(buf, end, ptr, spec, fmt);
834 case 'I': /* Formatted IP supported
835 * 4: 1.2.3.4
836 * 6: 0001:0203:...:0708
837 * 6c: 1::708 or 1::1.2.3.4
838 */
839 case 'i': /* Contiguous:
840 * 4: 001.002.003.004
841 * 6: 000102...0f
842 */
843 switch (fmt[1]) {
844 case '6':
845 return ip6_addr_string(buf, end, ptr, spec, fmt);
846 case '4':
847 return ip4_addr_string(buf, end, ptr, spec, fmt);
848 }
fef20d9c
FW
849 break;
850 }
851 spec.flags |= SMALL;
852 if (spec.field_width == -1) {
853 spec.field_width = 2*sizeof(void *);
854 spec.flags |= ZEROPAD;
855 }
856 spec.base = 16;
857
858 return number(buf, end, (unsigned long) ptr, spec);
859}
860
861/*
862 * Helper function to decode printf style format.
863 * Each call decode a token from the format and return the
864 * number of characters read (or likely the delta where it wants
865 * to go on the next call).
866 * The decoded token is returned through the parameters
867 *
868 * 'h', 'l', or 'L' for integer fields
869 * 'z' support added 23/7/1999 S.H.
870 * 'z' changed to 'Z' --davidm 1/25/99
871 * 't' added for ptrdiff_t
872 *
873 * @fmt: the format string
874 * @type of the token returned
875 * @flags: various flags such as +, -, # tokens..
876 * @field_width: overwritten width
877 * @base: base of the number (octal, hex, ...)
878 * @precision: precision of a number
879 * @qualifier: qualifier of a number (long, size_t, ...)
880 */
881static int format_decode(const char *fmt, struct printf_spec *spec)
882{
883 const char *start = fmt;
fef20d9c
FW
884
885 /* we finished early by reading the field width */
ed681a91 886 if (spec->type == FORMAT_TYPE_WIDTH) {
fef20d9c
FW
887 if (spec->field_width < 0) {
888 spec->field_width = -spec->field_width;
889 spec->flags |= LEFT;
890 }
891 spec->type = FORMAT_TYPE_NONE;
892 goto precision;
893 }
894
895 /* we finished early by reading the precision */
896 if (spec->type == FORMAT_TYPE_PRECISION) {
897 if (spec->precision < 0)
898 spec->precision = 0;
899
900 spec->type = FORMAT_TYPE_NONE;
901 goto qualifier;
902 }
903
904 /* By default */
905 spec->type = FORMAT_TYPE_NONE;
906
907 for (; *fmt ; ++fmt) {
908 if (*fmt == '%')
909 break;
910 }
911
912 /* Return the current non-format string */
913 if (fmt != start || !*fmt)
914 return fmt - start;
915
916 /* Process flags */
917 spec->flags = 0;
918
919 while (1) { /* this also skips first '%' */
920 bool found = true;
921
922 ++fmt;
923
924 switch (*fmt) {
925 case '-': spec->flags |= LEFT; break;
926 case '+': spec->flags |= PLUS; break;
927 case ' ': spec->flags |= SPACE; break;
928 case '#': spec->flags |= SPECIAL; break;
929 case '0': spec->flags |= ZEROPAD; break;
930 default: found = false;
931 }
932
933 if (!found)
934 break;
935 }
936
937 /* get field width */
938 spec->field_width = -1;
939
940 if (isdigit(*fmt))
941 spec->field_width = skip_atoi(&fmt);
942 else if (*fmt == '*') {
943 /* it's the next argument */
ed681a91 944 spec->type = FORMAT_TYPE_WIDTH;
fef20d9c
FW
945 return ++fmt - start;
946 }
947
948precision:
949 /* get the precision */
950 spec->precision = -1;
951 if (*fmt == '.') {
952 ++fmt;
953 if (isdigit(*fmt)) {
954 spec->precision = skip_atoi(&fmt);
955 if (spec->precision < 0)
956 spec->precision = 0;
957 } else if (*fmt == '*') {
958 /* it's the next argument */
adf26f84 959 spec->type = FORMAT_TYPE_PRECISION;
fef20d9c
FW
960 return ++fmt - start;
961 }
962 }
963
964qualifier:
965 /* get the conversion qualifier */
966 spec->qualifier = -1;
967 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
968 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
a4e94ef0
Z
969 spec->qualifier = *fmt++;
970 if (unlikely(spec->qualifier == *fmt)) {
971 if (spec->qualifier == 'l') {
972 spec->qualifier = 'L';
973 ++fmt;
974 } else if (spec->qualifier == 'h') {
975 spec->qualifier = 'H';
976 ++fmt;
977 }
fef20d9c
FW
978 }
979 }
980
981 /* default base */
982 spec->base = 10;
983 switch (*fmt) {
984 case 'c':
985 spec->type = FORMAT_TYPE_CHAR;
986 return ++fmt - start;
987
988 case 's':
989 spec->type = FORMAT_TYPE_STR;
990 return ++fmt - start;
991
992 case 'p':
993 spec->type = FORMAT_TYPE_PTR;
994 return fmt - start;
995 /* skip alnum */
996
997 case 'n':
998 spec->type = FORMAT_TYPE_NRCHARS;
999 return ++fmt - start;
1000
1001 case '%':
1002 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1003 return ++fmt - start;
1004
1005 /* integer number formats - set up the flags and "break" */
1006 case 'o':
1007 spec->base = 8;
1008 break;
1009
1010 case 'x':
1011 spec->flags |= SMALL;
1012
1013 case 'X':
1014 spec->base = 16;
1015 break;
1016
1017 case 'd':
1018 case 'i':
39e874f8 1019 spec->flags |= SIGN;
fef20d9c 1020 case 'u':
4aa99606 1021 break;
fef20d9c
FW
1022
1023 default:
1024 spec->type = FORMAT_TYPE_INVALID;
1025 return fmt - start;
0fe1ef24 1026 }
fef20d9c
FW
1027
1028 if (spec->qualifier == 'L')
1029 spec->type = FORMAT_TYPE_LONG_LONG;
1030 else if (spec->qualifier == 'l') {
39e874f8 1031 if (spec->flags & SIGN)
fef20d9c
FW
1032 spec->type = FORMAT_TYPE_LONG;
1033 else
1034 spec->type = FORMAT_TYPE_ULONG;
1035 } else if (spec->qualifier == 'Z' || spec->qualifier == 'z') {
1036 spec->type = FORMAT_TYPE_SIZE_T;
1037 } else if (spec->qualifier == 't') {
1038 spec->type = FORMAT_TYPE_PTRDIFF;
a4e94ef0
Z
1039 } else if (spec->qualifier == 'H') {
1040 if (spec->flags & SIGN)
1041 spec->type = FORMAT_TYPE_BYTE;
1042 else
1043 spec->type = FORMAT_TYPE_UBYTE;
fef20d9c 1044 } else if (spec->qualifier == 'h') {
39e874f8 1045 if (spec->flags & SIGN)
fef20d9c
FW
1046 spec->type = FORMAT_TYPE_SHORT;
1047 else
1048 spec->type = FORMAT_TYPE_USHORT;
1049 } else {
39e874f8 1050 if (spec->flags & SIGN)
fef20d9c
FW
1051 spec->type = FORMAT_TYPE_INT;
1052 else
1053 spec->type = FORMAT_TYPE_UINT;
78a8bf69 1054 }
fef20d9c
FW
1055
1056 return ++fmt - start;
78a8bf69
LT
1057}
1058
1da177e4
LT
1059/**
1060 * vsnprintf - Format a string and place it in a buffer
1061 * @buf: The buffer to place the result into
1062 * @size: The size of the buffer, including the trailing null space
1063 * @fmt: The format string to use
1064 * @args: Arguments for the format string
1065 *
20036fdc 1066 * This function follows C99 vsnprintf, but has some extensions:
91adcd2c
SR
1067 * %pS output the name of a text symbol with offset
1068 * %ps output the name of a text symbol without offset
0c8b946e
FW
1069 * %pF output the name of a function pointer with its offset
1070 * %pf output the name of a function pointer without its offset
332d2e78 1071 * %pR output the address range in a struct resource
20036fdc 1072 *
1da177e4
LT
1073 * The return value is the number of characters which would
1074 * be generated for the given input, excluding the trailing
1075 * '\0', as per ISO C99. If you want to have the exact
1076 * number of characters written into @buf as return value
72fd4a35 1077 * (not including the trailing '\0'), use vscnprintf(). If the
1da177e4
LT
1078 * return is greater than or equal to @size, the resulting
1079 * string is truncated.
1080 *
1081 * Call this function if you are already dealing with a va_list.
72fd4a35 1082 * You probably want snprintf() instead.
1da177e4
LT
1083 */
1084int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1085{
1da177e4 1086 unsigned long long num;
1da177e4 1087 char *str, *end, c;
fef20d9c
FW
1088 int read;
1089 struct printf_spec spec = {0};
1da177e4 1090
f796937a
JF
1091 /* Reject out-of-range values early. Large positive sizes are
1092 used for unknown buffer sizes. */
1da177e4
LT
1093 if (unlikely((int) size < 0)) {
1094 /* There can be only one.. */
b39a7340 1095 static char warn = 1;
1da177e4
LT
1096 WARN_ON(warn);
1097 warn = 0;
1098 return 0;
1099 }
1100
1101 str = buf;
f796937a 1102 end = buf + size;
1da177e4 1103
f796937a
JF
1104 /* Make sure end is always >= buf */
1105 if (end < buf) {
1106 end = ((void *)-1);
1107 size = end - buf;
1da177e4
LT
1108 }
1109
fef20d9c
FW
1110 while (*fmt) {
1111 const char *old_fmt = fmt;
1da177e4 1112
fef20d9c 1113 read = format_decode(fmt, &spec);
1da177e4 1114
fef20d9c 1115 fmt += read;
1da177e4 1116
fef20d9c
FW
1117 switch (spec.type) {
1118 case FORMAT_TYPE_NONE: {
1119 int copy = read;
1120 if (str < end) {
1121 if (copy > end - str)
1122 copy = end - str;
1123 memcpy(str, old_fmt, copy);
1da177e4 1124 }
fef20d9c
FW
1125 str += read;
1126 break;
1da177e4
LT
1127 }
1128
ed681a91 1129 case FORMAT_TYPE_WIDTH:
fef20d9c
FW
1130 spec.field_width = va_arg(args, int);
1131 break;
1da177e4 1132
fef20d9c
FW
1133 case FORMAT_TYPE_PRECISION:
1134 spec.precision = va_arg(args, int);
1135 break;
1da177e4 1136
fef20d9c
FW
1137 case FORMAT_TYPE_CHAR:
1138 if (!(spec.flags & LEFT)) {
1139 while (--spec.field_width > 0) {
f796937a 1140 if (str < end)
1da177e4
LT
1141 *str = ' ';
1142 ++str;
1da177e4 1143
fef20d9c
FW
1144 }
1145 }
1146 c = (unsigned char) va_arg(args, int);
1147 if (str < end)
1148 *str = c;
1149 ++str;
1150 while (--spec.field_width > 0) {
f796937a 1151 if (str < end)
fef20d9c 1152 *str = ' ';
1da177e4 1153 ++str;
fef20d9c
FW
1154 }
1155 break;
1da177e4 1156
fef20d9c
FW
1157 case FORMAT_TYPE_STR:
1158 str = string(str, end, va_arg(args, char *), spec);
1159 break;
1da177e4 1160
fef20d9c
FW
1161 case FORMAT_TYPE_PTR:
1162 str = pointer(fmt+1, str, end, va_arg(args, void *),
1163 spec);
1164 while (isalnum(*fmt))
1165 fmt++;
1166 break;
1da177e4 1167
fef20d9c
FW
1168 case FORMAT_TYPE_PERCENT_CHAR:
1169 if (str < end)
1170 *str = '%';
1171 ++str;
1172 break;
1da177e4 1173
fef20d9c
FW
1174 case FORMAT_TYPE_INVALID:
1175 if (str < end)
1176 *str = '%';
1177 ++str;
fef20d9c
FW
1178 break;
1179
1180 case FORMAT_TYPE_NRCHARS: {
1181 int qualifier = spec.qualifier;
1182
1183 if (qualifier == 'l') {
1184 long *ip = va_arg(args, long *);
1185 *ip = (str - buf);
1186 } else if (qualifier == 'Z' ||
1187 qualifier == 'z') {
1188 size_t *ip = va_arg(args, size_t *);
1189 *ip = (str - buf);
1190 } else {
1191 int *ip = va_arg(args, int *);
1192 *ip = (str - buf);
1193 }
1194 break;
1da177e4 1195 }
fef20d9c
FW
1196
1197 default:
1198 switch (spec.type) {
1199 case FORMAT_TYPE_LONG_LONG:
1200 num = va_arg(args, long long);
1201 break;
1202 case FORMAT_TYPE_ULONG:
1203 num = va_arg(args, unsigned long);
1204 break;
1205 case FORMAT_TYPE_LONG:
1206 num = va_arg(args, long);
1207 break;
1208 case FORMAT_TYPE_SIZE_T:
1209 num = va_arg(args, size_t);
1210 break;
1211 case FORMAT_TYPE_PTRDIFF:
1212 num = va_arg(args, ptrdiff_t);
1213 break;
a4e94ef0
Z
1214 case FORMAT_TYPE_UBYTE:
1215 num = (unsigned char) va_arg(args, int);
1216 break;
1217 case FORMAT_TYPE_BYTE:
1218 num = (signed char) va_arg(args, int);
1219 break;
fef20d9c
FW
1220 case FORMAT_TYPE_USHORT:
1221 num = (unsigned short) va_arg(args, int);
1222 break;
1223 case FORMAT_TYPE_SHORT:
1224 num = (short) va_arg(args, int);
1225 break;
39e874f8
FW
1226 case FORMAT_TYPE_INT:
1227 num = (int) va_arg(args, int);
fef20d9c
FW
1228 break;
1229 default:
1230 num = va_arg(args, unsigned int);
1231 }
1232
1233 str = number(str, end, num, spec);
1da177e4 1234 }
1da177e4 1235 }
fef20d9c 1236
f796937a
JF
1237 if (size > 0) {
1238 if (str < end)
1239 *str = '\0';
1240 else
0a6047ee 1241 end[-1] = '\0';
f796937a 1242 }
fef20d9c 1243
f796937a 1244 /* the trailing null byte doesn't count towards the total */
1da177e4 1245 return str-buf;
fef20d9c 1246
1da177e4 1247}
1da177e4
LT
1248EXPORT_SYMBOL(vsnprintf);
1249
1250/**
1251 * vscnprintf - Format a string and place it in a buffer
1252 * @buf: The buffer to place the result into
1253 * @size: The size of the buffer, including the trailing null space
1254 * @fmt: The format string to use
1255 * @args: Arguments for the format string
1256 *
1257 * The return value is the number of characters which have been written into
1258 * the @buf not including the trailing '\0'. If @size is <= 0 the function
1259 * returns 0.
1260 *
1261 * Call this function if you are already dealing with a va_list.
72fd4a35 1262 * You probably want scnprintf() instead.
20036fdc
AK
1263 *
1264 * See the vsnprintf() documentation for format string extensions over C99.
1da177e4
LT
1265 */
1266int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1267{
1268 int i;
1269
1270 i=vsnprintf(buf,size,fmt,args);
1271 return (i >= size) ? (size - 1) : i;
1272}
1da177e4
LT
1273EXPORT_SYMBOL(vscnprintf);
1274
1275/**
1276 * snprintf - Format a string and place it in a buffer
1277 * @buf: The buffer to place the result into
1278 * @size: The size of the buffer, including the trailing null space
1279 * @fmt: The format string to use
1280 * @...: Arguments for the format string
1281 *
1282 * The return value is the number of characters which would be
1283 * generated for the given input, excluding the trailing null,
1284 * as per ISO C99. If the return is greater than or equal to
1285 * @size, the resulting string is truncated.
20036fdc
AK
1286 *
1287 * See the vsnprintf() documentation for format string extensions over C99.
1da177e4
LT
1288 */
1289int snprintf(char * buf, size_t size, const char *fmt, ...)
1290{
1291 va_list args;
1292 int i;
1293
1294 va_start(args, fmt);
1295 i=vsnprintf(buf,size,fmt,args);
1296 va_end(args);
1297 return i;
1298}
1da177e4
LT
1299EXPORT_SYMBOL(snprintf);
1300
1301/**
1302 * scnprintf - Format a string and place it in a buffer
1303 * @buf: The buffer to place the result into
1304 * @size: The size of the buffer, including the trailing null space
1305 * @fmt: The format string to use
1306 * @...: Arguments for the format string
1307 *
1308 * The return value is the number of characters written into @buf not including
ea6f3281 1309 * the trailing '\0'. If @size is <= 0 the function returns 0.
1da177e4
LT
1310 */
1311
1312int scnprintf(char * buf, size_t size, const char *fmt, ...)
1313{
1314 va_list args;
1315 int i;
1316
1317 va_start(args, fmt);
1318 i = vsnprintf(buf, size, fmt, args);
1319 va_end(args);
1320 return (i >= size) ? (size - 1) : i;
1321}
1322EXPORT_SYMBOL(scnprintf);
1323
1324/**
1325 * vsprintf - Format a string and place it in a buffer
1326 * @buf: The buffer to place the result into
1327 * @fmt: The format string to use
1328 * @args: Arguments for the format string
1329 *
1330 * The function returns the number of characters written
72fd4a35 1331 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
1da177e4
LT
1332 * buffer overflows.
1333 *
1334 * Call this function if you are already dealing with a va_list.
72fd4a35 1335 * You probably want sprintf() instead.
20036fdc
AK
1336 *
1337 * See the vsnprintf() documentation for format string extensions over C99.
1da177e4
LT
1338 */
1339int vsprintf(char *buf, const char *fmt, va_list args)
1340{
1341 return vsnprintf(buf, INT_MAX, fmt, args);
1342}
1da177e4
LT
1343EXPORT_SYMBOL(vsprintf);
1344
1345/**
1346 * sprintf - Format a string and place it in a buffer
1347 * @buf: The buffer to place the result into
1348 * @fmt: The format string to use
1349 * @...: Arguments for the format string
1350 *
1351 * The function returns the number of characters written
72fd4a35 1352 * into @buf. Use snprintf() or scnprintf() in order to avoid
1da177e4 1353 * buffer overflows.
20036fdc
AK
1354 *
1355 * See the vsnprintf() documentation for format string extensions over C99.
1da177e4
LT
1356 */
1357int sprintf(char * buf, const char *fmt, ...)
1358{
1359 va_list args;
1360 int i;
1361
1362 va_start(args, fmt);
1363 i=vsnprintf(buf, INT_MAX, fmt, args);
1364 va_end(args);
1365 return i;
1366}
1da177e4
LT
1367EXPORT_SYMBOL(sprintf);
1368
4370aa4a
LJ
1369#ifdef CONFIG_BINARY_PRINTF
1370/*
1371 * bprintf service:
1372 * vbin_printf() - VA arguments to binary data
1373 * bstr_printf() - Binary data to text string
1374 */
1375
1376/**
1377 * vbin_printf - Parse a format string and place args' binary value in a buffer
1378 * @bin_buf: The buffer to place args' binary value
1379 * @size: The size of the buffer(by words(32bits), not characters)
1380 * @fmt: The format string to use
1381 * @args: Arguments for the format string
1382 *
1383 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1384 * is skiped.
1385 *
1386 * The return value is the number of words(32bits) which would be generated for
1387 * the given input.
1388 *
1389 * NOTE:
1390 * If the return value is greater than @size, the resulting bin_buf is NOT
1391 * valid for bstr_printf().
1392 */
1393int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1394{
fef20d9c 1395 struct printf_spec spec = {0};
4370aa4a 1396 char *str, *end;
fef20d9c 1397 int read;
4370aa4a
LJ
1398
1399 str = (char *)bin_buf;
1400 end = (char *)(bin_buf + size);
1401
1402#define save_arg(type) \
1403do { \
1404 if (sizeof(type) == 8) { \
1405 unsigned long long value; \
1406 str = PTR_ALIGN(str, sizeof(u32)); \
1407 value = va_arg(args, unsigned long long); \
1408 if (str + sizeof(type) <= end) { \
1409 *(u32 *)str = *(u32 *)&value; \
1410 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1411 } \
1412 } else { \
1413 unsigned long value; \
1414 str = PTR_ALIGN(str, sizeof(type)); \
1415 value = va_arg(args, int); \
1416 if (str + sizeof(type) <= end) \
1417 *(typeof(type) *)str = (type)value; \
1418 } \
1419 str += sizeof(type); \
1420} while (0)
1421
4370aa4a 1422
fef20d9c
FW
1423 while (*fmt) {
1424 read = format_decode(fmt, &spec);
4370aa4a 1425
fef20d9c 1426 fmt += read;
4370aa4a 1427
fef20d9c
FW
1428 switch (spec.type) {
1429 case FORMAT_TYPE_NONE:
1430 break;
1431
ed681a91 1432 case FORMAT_TYPE_WIDTH:
fef20d9c
FW
1433 case FORMAT_TYPE_PRECISION:
1434 save_arg(int);
1435 break;
1436
1437 case FORMAT_TYPE_CHAR:
4370aa4a 1438 save_arg(char);
fef20d9c
FW
1439 break;
1440
1441 case FORMAT_TYPE_STR: {
4370aa4a
LJ
1442 const char *save_str = va_arg(args, char *);
1443 size_t len;
1444 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1445 || (unsigned long)save_str < PAGE_SIZE)
1446 save_str = "<NULL>";
1447 len = strlen(save_str);
1448 if (str + len + 1 < end)
1449 memcpy(str, save_str, len + 1);
1450 str += len + 1;
fef20d9c 1451 break;
4370aa4a 1452 }
fef20d9c
FW
1453
1454 case FORMAT_TYPE_PTR:
4370aa4a
LJ
1455 save_arg(void *);
1456 /* skip all alphanumeric pointer suffixes */
fef20d9c 1457 while (isalnum(*fmt))
4370aa4a 1458 fmt++;
fef20d9c
FW
1459 break;
1460
1461 case FORMAT_TYPE_PERCENT_CHAR:
1462 break;
1463
1464 case FORMAT_TYPE_INVALID:
fef20d9c
FW
1465 break;
1466
1467 case FORMAT_TYPE_NRCHARS: {
4370aa4a 1468 /* skip %n 's argument */
fef20d9c 1469 int qualifier = spec.qualifier;
4370aa4a
LJ
1470 void *skip_arg;
1471 if (qualifier == 'l')
1472 skip_arg = va_arg(args, long *);
1473 else if (qualifier == 'Z' || qualifier == 'z')
1474 skip_arg = va_arg(args, size_t *);
1475 else
1476 skip_arg = va_arg(args, int *);
fef20d9c 1477 break;
4370aa4a 1478 }
fef20d9c
FW
1479
1480 default:
1481 switch (spec.type) {
1482
1483 case FORMAT_TYPE_LONG_LONG:
4370aa4a 1484 save_arg(long long);
fef20d9c
FW
1485 break;
1486 case FORMAT_TYPE_ULONG:
1487 case FORMAT_TYPE_LONG:
4370aa4a 1488 save_arg(unsigned long);
fef20d9c
FW
1489 break;
1490 case FORMAT_TYPE_SIZE_T:
4370aa4a 1491 save_arg(size_t);
fef20d9c
FW
1492 break;
1493 case FORMAT_TYPE_PTRDIFF:
4370aa4a 1494 save_arg(ptrdiff_t);
fef20d9c 1495 break;
a4e94ef0
Z
1496 case FORMAT_TYPE_UBYTE:
1497 case FORMAT_TYPE_BYTE:
1498 save_arg(char);
1499 break;
fef20d9c
FW
1500 case FORMAT_TYPE_USHORT:
1501 case FORMAT_TYPE_SHORT:
4370aa4a 1502 save_arg(short);
fef20d9c
FW
1503 break;
1504 default:
4370aa4a 1505 save_arg(int);
fef20d9c 1506 }
4370aa4a
LJ
1507 }
1508 }
4370aa4a 1509 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
fef20d9c
FW
1510
1511#undef save_arg
4370aa4a
LJ
1512}
1513EXPORT_SYMBOL_GPL(vbin_printf);
1514
1515/**
1516 * bstr_printf - Format a string from binary arguments and place it in a buffer
1517 * @buf: The buffer to place the result into
1518 * @size: The size of the buffer, including the trailing null space
1519 * @fmt: The format string to use
1520 * @bin_buf: Binary arguments for the format string
1521 *
1522 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1523 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1524 * a binary buffer that generated by vbin_printf.
1525 *
1526 * The format follows C99 vsnprintf, but has some extensions:
1527 * %pS output the name of a text symbol
0c8b946e
FW
1528 * %pF output the name of a function pointer with its offset
1529 * %pf output the name of a function pointer without its offset
4370aa4a
LJ
1530 * %pR output the address range in a struct resource
1531 * %n is ignored
1532 *
1533 * The return value is the number of characters which would
1534 * be generated for the given input, excluding the trailing
1535 * '\0', as per ISO C99. If you want to have the exact
1536 * number of characters written into @buf as return value
1537 * (not including the trailing '\0'), use vscnprintf(). If the
1538 * return is greater than or equal to @size, the resulting
1539 * string is truncated.
1540 */
1541int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1542{
1543 unsigned long long num;
4370aa4a
LJ
1544 char *str, *end, c;
1545 const char *args = (const char *)bin_buf;
1546
fef20d9c 1547 struct printf_spec spec = {0};
4370aa4a
LJ
1548
1549 if (unlikely((int) size < 0)) {
1550 /* There can be only one.. */
1551 static char warn = 1;
1552 WARN_ON(warn);
1553 warn = 0;
1554 return 0;
1555 }
1556
1557 str = buf;
1558 end = buf + size;
1559
1560#define get_arg(type) \
1561({ \
1562 typeof(type) value; \
1563 if (sizeof(type) == 8) { \
1564 args = PTR_ALIGN(args, sizeof(u32)); \
1565 *(u32 *)&value = *(u32 *)args; \
1566 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1567 } else { \
1568 args = PTR_ALIGN(args, sizeof(type)); \
1569 value = *(typeof(type) *)args; \
1570 } \
1571 args += sizeof(type); \
1572 value; \
1573})
1574
1575 /* Make sure end is always >= buf */
1576 if (end < buf) {
1577 end = ((void *)-1);
1578 size = end - buf;
1579 }
1580
fef20d9c
FW
1581 while (*fmt) {
1582 int read;
1583 const char *old_fmt = fmt;
4370aa4a 1584
fef20d9c 1585 read = format_decode(fmt, &spec);
4370aa4a 1586
fef20d9c 1587 fmt += read;
4370aa4a 1588
fef20d9c
FW
1589 switch (spec.type) {
1590 case FORMAT_TYPE_NONE: {
1591 int copy = read;
1592 if (str < end) {
1593 if (copy > end - str)
1594 copy = end - str;
1595 memcpy(str, old_fmt, copy);
4370aa4a 1596 }
fef20d9c
FW
1597 str += read;
1598 break;
4370aa4a
LJ
1599 }
1600
ed681a91 1601 case FORMAT_TYPE_WIDTH:
fef20d9c
FW
1602 spec.field_width = get_arg(int);
1603 break;
4370aa4a 1604
fef20d9c
FW
1605 case FORMAT_TYPE_PRECISION:
1606 spec.precision = get_arg(int);
1607 break;
4370aa4a 1608
fef20d9c
FW
1609 case FORMAT_TYPE_CHAR:
1610 if (!(spec.flags & LEFT)) {
1611 while (--spec.field_width > 0) {
4370aa4a
LJ
1612 if (str < end)
1613 *str = ' ';
1614 ++str;
1615 }
1616 }
1617 c = (unsigned char) get_arg(char);
1618 if (str < end)
1619 *str = c;
1620 ++str;
fef20d9c 1621 while (--spec.field_width > 0) {
4370aa4a
LJ
1622 if (str < end)
1623 *str = ' ';
1624 ++str;
1625 }
fef20d9c 1626 break;
4370aa4a 1627
fef20d9c 1628 case FORMAT_TYPE_STR: {
4370aa4a
LJ
1629 const char *str_arg = args;
1630 size_t len = strlen(str_arg);
1631 args += len + 1;
fef20d9c
FW
1632 str = string(str, end, (char *)str_arg, spec);
1633 break;
4370aa4a
LJ
1634 }
1635
fef20d9c
FW
1636 case FORMAT_TYPE_PTR:
1637 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1638 while (isalnum(*fmt))
4370aa4a 1639 fmt++;
fef20d9c 1640 break;
4370aa4a 1641
fef20d9c 1642 case FORMAT_TYPE_PERCENT_CHAR:
4370aa4a
LJ
1643 if (str < end)
1644 *str = '%';
1645 ++str;
4370aa4a
LJ
1646 break;
1647
fef20d9c 1648 case FORMAT_TYPE_INVALID:
4370aa4a
LJ
1649 if (str < end)
1650 *str = '%';
1651 ++str;
fef20d9c
FW
1652 break;
1653
1654 case FORMAT_TYPE_NRCHARS:
1655 /* skip */
1656 break;
1657
1658 default:
1659 switch (spec.type) {
1660
1661 case FORMAT_TYPE_LONG_LONG:
1662 num = get_arg(long long);
1663 break;
1664 case FORMAT_TYPE_ULONG:
1665 num = get_arg(unsigned long);
1666 break;
1667 case FORMAT_TYPE_LONG:
1668 num = get_arg(unsigned long);
1669 break;
1670 case FORMAT_TYPE_SIZE_T:
1671 num = get_arg(size_t);
1672 break;
1673 case FORMAT_TYPE_PTRDIFF:
1674 num = get_arg(ptrdiff_t);
1675 break;
a4e94ef0
Z
1676 case FORMAT_TYPE_UBYTE:
1677 num = get_arg(unsigned char);
1678 break;
1679 case FORMAT_TYPE_BYTE:
1680 num = get_arg(signed char);
1681 break;
fef20d9c
FW
1682 case FORMAT_TYPE_USHORT:
1683 num = get_arg(unsigned short);
1684 break;
1685 case FORMAT_TYPE_SHORT:
1686 num = get_arg(short);
1687 break;
1688 case FORMAT_TYPE_UINT:
1689 num = get_arg(unsigned int);
1690 break;
1691 default:
1692 num = get_arg(int);
1693 }
1694
1695 str = number(str, end, num, spec);
4370aa4a 1696 }
4370aa4a 1697 }
fef20d9c 1698
4370aa4a
LJ
1699 if (size > 0) {
1700 if (str < end)
1701 *str = '\0';
1702 else
1703 end[-1] = '\0';
1704 }
fef20d9c 1705
4370aa4a
LJ
1706#undef get_arg
1707
1708 /* the trailing null byte doesn't count towards the total */
1709 return str - buf;
1710}
1711EXPORT_SYMBOL_GPL(bstr_printf);
1712
1713/**
1714 * bprintf - Parse a format string and place args' binary value in a buffer
1715 * @bin_buf: The buffer to place args' binary value
1716 * @size: The size of the buffer(by words(32bits), not characters)
1717 * @fmt: The format string to use
1718 * @...: Arguments for the format string
1719 *
1720 * The function returns the number of words(u32) written
1721 * into @bin_buf.
1722 */
1723int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1724{
1725 va_list args;
1726 int ret;
1727
1728 va_start(args, fmt);
1729 ret = vbin_printf(bin_buf, size, fmt, args);
1730 va_end(args);
1731 return ret;
1732}
1733EXPORT_SYMBOL_GPL(bprintf);
1734
1735#endif /* CONFIG_BINARY_PRINTF */
1736
1da177e4
LT
1737/**
1738 * vsscanf - Unformat a buffer into a list of arguments
1739 * @buf: input buffer
1740 * @fmt: format of buffer
1741 * @args: arguments
1742 */
1743int vsscanf(const char * buf, const char * fmt, va_list args)
1744{
1745 const char *str = buf;
1746 char *next;
1747 char digit;
1748 int num = 0;
1749 int qualifier;
1750 int base;
1751 int field_width;
1752 int is_sign = 0;
1753
1754 while(*fmt && *str) {
1755 /* skip any white space in format */
1756 /* white space in format matchs any amount of
1757 * white space, including none, in the input.
1758 */
1759 if (isspace(*fmt)) {
1760 while (isspace(*fmt))
1761 ++fmt;
1762 while (isspace(*str))
1763 ++str;
1764 }
1765
1766 /* anything that is not a conversion must match exactly */
1767 if (*fmt != '%' && *fmt) {
1768 if (*fmt++ != *str++)
1769 break;
1770 continue;
1771 }
1772
1773 if (!*fmt)
1774 break;
1775 ++fmt;
1776
1777 /* skip this conversion.
1778 * advance both strings to next white space
1779 */
1780 if (*fmt == '*') {
1781 while (!isspace(*fmt) && *fmt)
1782 fmt++;
1783 while (!isspace(*str) && *str)
1784 str++;
1785 continue;
1786 }
1787
1788 /* get field width */
1789 field_width = -1;
1790 if (isdigit(*fmt))
1791 field_width = skip_atoi(&fmt);
1792
1793 /* get conversion qualifier */
1794 qualifier = -1;
1795 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1796 *fmt == 'Z' || *fmt == 'z') {
1797 qualifier = *fmt++;
1798 if (unlikely(qualifier == *fmt)) {
1799 if (qualifier == 'h') {
1800 qualifier = 'H';
1801 fmt++;
1802 } else if (qualifier == 'l') {
1803 qualifier = 'L';
1804 fmt++;
1805 }
1806 }
1807 }
1808 base = 10;
1809 is_sign = 0;
1810
1811 if (!*fmt || !*str)
1812 break;
1813
1814 switch(*fmt++) {
1815 case 'c':
1816 {
1817 char *s = (char *) va_arg(args,char*);
1818 if (field_width == -1)
1819 field_width = 1;
1820 do {
1821 *s++ = *str++;
1822 } while (--field_width > 0 && *str);
1823 num++;
1824 }
1825 continue;
1826 case 's':
1827 {
1828 char *s = (char *) va_arg(args, char *);
1829 if(field_width == -1)
1830 field_width = INT_MAX;
1831 /* first, skip leading white space in buffer */
1832 while (isspace(*str))
1833 str++;
1834
1835 /* now copy until next white space */
1836 while (*str && !isspace(*str) && field_width--) {
1837 *s++ = *str++;
1838 }
1839 *s = '\0';
1840 num++;
1841 }
1842 continue;
1843 case 'n':
1844 /* return number of characters read so far */
1845 {
1846 int *i = (int *)va_arg(args,int*);
1847 *i = str - buf;
1848 }
1849 continue;
1850 case 'o':
1851 base = 8;
1852 break;
1853 case 'x':
1854 case 'X':
1855 base = 16;
1856 break;
1857 case 'i':
1858 base = 0;
1859 case 'd':
1860 is_sign = 1;
1861 case 'u':
1862 break;
1863 case '%':
1864 /* looking for '%' in str */
1865 if (*str++ != '%')
1866 return num;
1867 continue;
1868 default:
1869 /* invalid format; stop here */
1870 return num;
1871 }
1872
1873 /* have some sort of integer conversion.
1874 * first, skip white space in buffer.
1875 */
1876 while (isspace(*str))
1877 str++;
1878
1879 digit = *str;
1880 if (is_sign && digit == '-')
1881 digit = *(str + 1);
1882
1883 if (!digit
1884 || (base == 16 && !isxdigit(digit))
1885 || (base == 10 && !isdigit(digit))
1886 || (base == 8 && (!isdigit(digit) || digit > '7'))
1887 || (base == 0 && !isdigit(digit)))
1888 break;
1889
1890 switch(qualifier) {
1891 case 'H': /* that's 'hh' in format */
1892 if (is_sign) {
1893 signed char *s = (signed char *) va_arg(args,signed char *);
1894 *s = (signed char) simple_strtol(str,&next,base);
1895 } else {
1896 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1897 *s = (unsigned char) simple_strtoul(str, &next, base);
1898 }
1899 break;
1900 case 'h':
1901 if (is_sign) {
1902 short *s = (short *) va_arg(args,short *);
1903 *s = (short) simple_strtol(str,&next,base);
1904 } else {
1905 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1906 *s = (unsigned short) simple_strtoul(str, &next, base);
1907 }
1908 break;
1909 case 'l':
1910 if (is_sign) {
1911 long *l = (long *) va_arg(args,long *);
1912 *l = simple_strtol(str,&next,base);
1913 } else {
1914 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1915 *l = simple_strtoul(str,&next,base);
1916 }
1917 break;
1918 case 'L':
1919 if (is_sign) {
1920 long long *l = (long long*) va_arg(args,long long *);
1921 *l = simple_strtoll(str,&next,base);
1922 } else {
1923 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1924 *l = simple_strtoull(str,&next,base);
1925 }
1926 break;
1927 case 'Z':
1928 case 'z':
1929 {
1930 size_t *s = (size_t*) va_arg(args,size_t*);
1931 *s = (size_t) simple_strtoul(str,&next,base);
1932 }
1933 break;
1934 default:
1935 if (is_sign) {
1936 int *i = (int *) va_arg(args, int*);
1937 *i = (int) simple_strtol(str,&next,base);
1938 } else {
1939 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1940 *i = (unsigned int) simple_strtoul(str,&next,base);
1941 }
1942 break;
1943 }
1944 num++;
1945
1946 if (!next)
1947 break;
1948 str = next;
1949 }
c6b40d16
JB
1950
1951 /*
1952 * Now we've come all the way through so either the input string or the
1953 * format ended. In the former case, there can be a %n at the current
1954 * position in the format that needs to be filled.
1955 */
1956 if (*fmt == '%' && *(fmt + 1) == 'n') {
1957 int *p = (int *)va_arg(args, int *);
1958 *p = str - buf;
1959 }
1960
1da177e4
LT
1961 return num;
1962}
1da177e4
LT
1963EXPORT_SYMBOL(vsscanf);
1964
1965/**
1966 * sscanf - Unformat a buffer into a list of arguments
1967 * @buf: input buffer
1968 * @fmt: formatting of buffer
1969 * @...: resulting arguments
1970 */
1971int sscanf(const char * buf, const char * fmt, ...)
1972{
1973 va_list args;
1974 int i;
1975
1976 va_start(args,fmt);
1977 i = vsscanf(buf,fmt,args);
1978 va_end(args);
1979 return i;
1980}
1da177e4 1981EXPORT_SYMBOL(sscanf);