blob: a6e195163eb39f536bacf25b41dc22e739d63e4a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070025#include <linux/kallsyms.h>
26#include <linux/uaccess.h>
Linus Torvalds332d2e72008-10-20 15:07:34 +110027#include <linux/ioport.h>
Joe Perches8a27f7c2009-08-17 12:29:44 +000028#include <net/addrconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Tim Schmielau4e57b682005-10-30 15:03:48 -080030#include <asm/page.h> /* for PAGE_SIZE */
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <asm/div64.h>
James Bottomleydeac93d2008-09-03 20:43:36 -050032#include <asm/sections.h> /* for dereference_function_descriptor() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Denys Vlasenko9b706ae2008-02-09 23:24:09 +010034/* Works only for digits and letters, but small and fast */
35#define TOLOWER(x) ((x) | 0x20)
36
Harvey Harrisonaa46a632008-10-16 13:40:34 -070037static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070049/**
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 */
Harvey Harrison22d27052008-10-16 13:40:35 -070055unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Harvey Harrisonaa46a632008-10-16 13:40:34 -070057 unsigned long result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Harvey Harrisonaa46a632008-10-16 13:40:34 -070059 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 cp++;
73 }
Harvey Harrisonaa46a632008-10-16 13:40:34 -070074
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (endp)
76 *endp = (char *)cp;
77 return result;
78}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079EXPORT_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 */
Harvey Harrison22d27052008-10-16 13:40:35 -070087long simple_strtol(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
Harvey Harrison22d27052008-10-16 13:40:35 -070089 if(*cp == '-')
90 return -simple_strtoul(cp + 1, endp, base);
91 return simple_strtoul(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
Linus Torvalds1da177e2005-04-16 15:20:36 -070093EXPORT_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 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700101unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
Harvey Harrisonaa46a632008-10-16 13:40:34 -0700103 unsigned long long result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Harvey Harrisonaa46a632008-10-16 13:40:34 -0700105 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 cp++;
119 }
Harvey Harrisonaa46a632008-10-16 13:40:34 -0700120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (endp)
122 *endp = (char *)cp;
123 return result;
124}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125EXPORT_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 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700133long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
135 if(*cp=='-')
Harvey Harrison22d27052008-10-16 13:40:35 -0700136 return -simple_strtoull(cp + 1, endp, base);
137 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Yi Yang06b2a762008-02-08 04:21:57 -0800140/**
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 */
Harvey Harrison9d85db22008-10-16 13:40:35 -0700162int 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);
Pavel Macheke899aa82009-01-06 14:40:53 -0800174 if (tail == cp)
175 return -EINVAL;
Harvey Harrison9d85db22008-10-16 13:40:35 -0700176 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);
Yi Yang06b2a762008-02-08 04:21:57 -0800185
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 */
Harvey Harrison9d85db22008-10-16 13:40:35 -0700198int 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);
Yi Yang06b2a762008-02-08 04:21:57 -0800212
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 */
Harvey Harrison9d85db22008-10-16 13:40:35 -0700235int 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);
Pavel Macheke899aa82009-01-06 14:40:53 -0800247 if (tail == cp)
248 return -EINVAL;
Harvey Harrison9d85db22008-10-16 13:40:35 -0700249 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);
Yi Yang06b2a762008-02-08 04:21:57 -0800258
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 */
Harvey Harrison9d85db22008-10-16 13:40:35 -0700271int 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 }
Yi Yang06b2a762008-02-08 04:21:57 -0800281
Harvey Harrison9d85db22008-10-16 13:40:35 -0700282 return ret;
283}
Yi Yang06b2a762008-02-08 04:21:57 -0800284EXPORT_SYMBOL(strict_strtoll);
Yi Yang06b2a762008-02-08 04:21:57 -0800285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286static 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
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700295/* 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392#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 */
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100397#define SMALL 32 /* Must be 32 == 0x20 */
398#define SPECIAL 64 /* 0x */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100400enum format_type {
401 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100402 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100403 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,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800412 FORMAT_TYPE_UBYTE,
413 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100414 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100435 /* 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;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100441 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 int i;
443
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100444 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
445 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100446 locase = (spec.flags & SMALL);
447 if (spec.flags & LEFT)
448 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100450 if (spec.flags & SIGN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if ((signed long long) num < 0) {
452 sign = '-';
453 num = - (signed long long) num;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100454 spec.field_width--;
455 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 sign = '+';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100457 spec.field_width--;
458 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 sign = ' ';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100460 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
462 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700463 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100464 spec.field_width--;
465 if (spec.base == 16)
466 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700468
469 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 i = 0;
471 if (num == 0)
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700472 tmp[i++] = '0';
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700473 /* Generic code, for any base:
474 else do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100475 tmp[i++] = (digits[do_div(num,base)] | locase);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700476 } while (num != 0);
477 */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100478 else if (spec.base != 10) { /* 8 or 16 */
479 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700480 int shift = 3;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100481 if (spec.base == 16) shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700482 do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100483 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700484 num >>= shift;
485 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700486 } else { /* base 10 */
487 i = put_dec(tmp, num) - tmp;
488 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700489
490 /* printing 100 using %2d gives "100", not "00" */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100491 if (i > spec.precision)
492 spec.precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700493 /* leading space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100494 spec.field_width -= spec.precision;
495 if (!(spec.flags & (ZEROPAD+LEFT))) {
496 while(--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700497 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 *buf = ' ';
499 ++buf;
500 }
501 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700502 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700504 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 *buf = sign;
506 ++buf;
507 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700508 /* "0x" / "0" prefix */
509 if (need_pfx) {
510 if (buf < end)
511 *buf = '0';
512 ++buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100513 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700514 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100515 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 ++buf;
517 }
518 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700519 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100520 if (!(spec.flags & LEFT)) {
521 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
522 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700523 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 *buf = c;
525 ++buf;
526 }
527 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700528 /* hmm even more zero padding? */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100529 while (i <= --spec.precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700530 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 *buf = '0';
532 ++buf;
533 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700534 /* actual digits of result */
535 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700536 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 *buf = tmp[i];
538 ++buf;
539 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700540 /* trailing space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100541 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700542 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 *buf = ' ';
544 ++buf;
545 }
546 return buf;
547}
548
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100549static char *string(char *buf, char *end, char *s, struct printf_spec spec)
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700550{
551 int len, i;
552
553 if ((unsigned long)s < PAGE_SIZE)
554 s = "<NULL>";
555
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100556 len = strnlen(s, spec.precision);
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700557
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100558 if (!(spec.flags & LEFT)) {
559 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700560 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 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100570 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700571 if (buf < end)
572 *buf = ' ';
573 ++buf;
574 }
575 return buf;
576}
577
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100578static char *symbol_string(char *buf, char *end, void *ptr,
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200579 struct printf_spec spec, char ext)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700580{
581 unsigned long value = (unsigned long) ptr;
582#ifdef CONFIG_KALLSYMS
583 char sym[KSYM_SYMBOL_LEN];
Steven Rostedt91adcd22009-09-16 20:03:06 -0400584 if (ext != 'f' && ext != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200585 sprint_symbol(sym, value);
586 else
587 kallsyms_lookup(value, NULL, NULL, NULL, sym);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100588 return string(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700589#else
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100590 spec.field_width = 2*sizeof(void *);
591 spec.flags |= SPECIAL | SMALL | ZEROPAD;
592 spec.base = 16;
593 return number(buf, end, value, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700594#endif
595}
596
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100597static char *resource_string(char *buf, char *end, struct resource *res,
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600598 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100599{
600#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600601#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100602#endif
603
604#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600605#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100606#endif
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600607 struct printf_spec hex_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100608 .base = 16,
609 .precision = -1,
610 .flags = SPECIAL | SMALL | ZEROPAD,
611 };
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600612 struct printf_spec dec_spec = {
613 .base = 10,
614 .precision = -1,
615 .flags = 0,
616 };
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600617 struct printf_spec str_spec = {
618 .field_width = -1,
619 .precision = 10,
620 .flags = LEFT,
621 };
622 struct printf_spec flag_spec = {
623 .base = 16,
624 .precision = -1,
625 .flags = SPECIAL | SMALL,
626 };
627 /*
628 * room for three actual numbers (decimal or hex), plus
629 * "[mem 0x-0x 64bit pref disabled flags 0x]\0"
630 */
631 char sym[3*3*sizeof(resource_size_t) + 41];
Linus Torvalds332d2e72008-10-20 15:07:34 +1100632 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600633 int size = -1, addr = 0;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100634
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600635 if (res->flags & IORESOURCE_IO) {
Linus Torvalds332d2e72008-10-20 15:07:34 +1100636 size = IO_RSRC_PRINTK_SIZE;
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600637 addr = 1;
638 } else if (res->flags & IORESOURCE_MEM) {
Linus Torvalds332d2e72008-10-20 15:07:34 +1100639 size = MEM_RSRC_PRINTK_SIZE;
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600640 addr = 1;
641 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100642
643 *p++ = '[';
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600644 if (fmt[1] == 't' || fmt[1] == 'f') {
645 if (res->flags & IORESOURCE_IO)
646 p = string(p, pend, "io ", str_spec);
647 else if (res->flags & IORESOURCE_MEM)
648 p = string(p, pend, "mem ", str_spec);
649 else if (res->flags & IORESOURCE_IRQ)
650 p = string(p, pend, "irq ", str_spec);
651 else if (res->flags & IORESOURCE_DMA)
652 p = string(p, pend, "dma ", str_spec);
653 }
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600654 hex_spec.field_width = size;
655 p = number(p, pend, res->start, addr ? hex_spec : dec_spec);
656 if (res->start != res->end) {
657 *p++ = '-';
658 p = number(p, pend, res->end, addr ? hex_spec : dec_spec);
659 }
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600660 if (fmt[1] == 't' || fmt[1] == 'f') {
661 if (res->flags & IORESOURCE_MEM_64)
662 p = string(p, pend, " 64bit", str_spec);
663 if (res->flags & IORESOURCE_PREFETCH)
664 p = string(p, pend, " pref", str_spec);
665 if (res->flags & IORESOURCE_DISABLED)
666 p = string(p, pend, " disabled", str_spec);
667 if (fmt[1] == 'f') {
668 p = string(p, pend, " flags ", str_spec);
669 p = number(p, pend, res->flags & ~IORESOURCE_TYPE_BITS,
670 flag_spec);
671 }
672 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100673 *p++ = ']';
674 *p = 0;
675
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100676 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100677}
678
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100679static char *mac_address_string(char *buf, char *end, u8 *addr,
Joe Perches8a27f7c2009-08-17 12:29:44 +0000680 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700681{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000682 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700683 char *p = mac_addr;
684 int i;
685
686 for (i = 0; i < 6; i++) {
687 p = pack_hex_byte(p, addr[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000688 if (fmt[0] == 'M' && i != 5)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700689 *p++ = ':';
690 }
691 *p = '\0';
692
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100693 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700694}
695
Joe Perches8a27f7c2009-08-17 12:29:44 +0000696static char *ip4_string(char *p, const u8 *addr, bool leading_zeros)
Harvey Harrison689afa72008-10-28 16:04:44 -0700697{
Harvey Harrison689afa72008-10-28 16:04:44 -0700698 int i;
699
Joe Perches8a27f7c2009-08-17 12:29:44 +0000700 for (i = 0; i < 4; i++) {
701 char temp[3]; /* hold each IP quad in reverse order */
702 int digits = put_dec_trunc(temp, addr[i]) - temp;
703 if (leading_zeros) {
704 if (digits < 3)
705 *p++ = '0';
706 if (digits < 2)
707 *p++ = '0';
708 }
709 /* reverse the digits in the quad */
710 while (digits--)
711 *p++ = temp[digits];
712 if (i < 3)
713 *p++ = '.';
714 }
715
716 *p = '\0';
717 return p;
718}
719
Joe Percheseb78cd22009-09-18 13:04:06 +0000720static char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000721{
722 int i;
723 int j;
724 int range;
725 unsigned char zerolength[8];
726 int longest = 1;
727 int colonpos = -1;
728 u16 word;
729 u8 hi;
730 u8 lo;
731 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +0000732 bool useIPv4;
733 struct in6_addr in6;
734
735 memcpy(&in6, addr, sizeof(struct in6_addr));
736
737 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000738
739 memset(zerolength, 0, sizeof(zerolength));
740
741 if (useIPv4)
742 range = 6;
743 else
744 range = 8;
745
746 /* find position of longest 0 run */
747 for (i = 0; i < range; i++) {
748 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +0000749 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000750 break;
751 zerolength[i]++;
752 }
753 }
754 for (i = 0; i < range; i++) {
755 if (zerolength[i] > longest) {
756 longest = zerolength[i];
757 colonpos = i;
758 }
759 }
760
761 /* emit address */
762 for (i = 0; i < range; i++) {
763 if (i == colonpos) {
764 if (needcolon || i == 0)
765 *p++ = ':';
766 *p++ = ':';
767 needcolon = false;
768 i += longest - 1;
769 continue;
770 }
771 if (needcolon) {
772 *p++ = ':';
773 needcolon = false;
774 }
775 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +0000776 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000777 hi = word >> 8;
778 lo = word & 0xff;
779 if (hi) {
780 if (hi > 0x0f)
781 p = pack_hex_byte(p, hi);
782 else
783 *p++ = hex_asc_lo(hi);
784 }
785 if (hi || lo > 0x0f)
786 p = pack_hex_byte(p, lo);
787 else
788 *p++ = hex_asc_lo(lo);
789 needcolon = true;
790 }
791
792 if (useIPv4) {
793 if (needcolon)
794 *p++ = ':';
Joe Percheseb78cd22009-09-18 13:04:06 +0000795 p = ip4_string(p, &in6.s6_addr[12], false);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000796 }
797
798 *p = '\0';
799 return p;
800}
801
Joe Percheseb78cd22009-09-18 13:04:06 +0000802static char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000803{
804 int i;
Harvey Harrison689afa72008-10-28 16:04:44 -0700805 for (i = 0; i < 8; i++) {
Joe Percheseb78cd22009-09-18 13:04:06 +0000806 p = pack_hex_byte(p, *addr++);
807 p = pack_hex_byte(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000808 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -0700809 *p++ = ':';
810 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000811
Harvey Harrison689afa72008-10-28 16:04:44 -0700812 *p = '\0';
Joe Perches8a27f7c2009-08-17 12:29:44 +0000813 return p;
814}
815
816static char *ip6_addr_string(char *buf, char *end, const u8 *addr,
817 struct printf_spec spec, const char *fmt)
818{
819 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
820
821 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +0000822 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000823 else
Joe Percheseb78cd22009-09-18 13:04:06 +0000824 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -0700825
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100826 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -0700827}
828
Joe Perches8a27f7c2009-08-17 12:29:44 +0000829static char *ip4_addr_string(char *buf, char *end, const u8 *addr,
830 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -0700831{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000832 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -0700833
Joe Perches8a27f7c2009-08-17 12:29:44 +0000834 ip4_string(ip4_addr, addr, fmt[0] == 'i');
Harvey Harrison4aa99602008-10-29 12:49:58 -0700835
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100836 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700837}
838
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700839/*
840 * Show a '%p' thing. A kernel extension is that the '%p' is followed
841 * by an extra set of alphanumeric characters that are extended format
842 * specifiers.
843 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100844 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700845 *
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200846 * - 'F' For symbolic function descriptor pointers with offset
847 * - 'f' For simple symbolic function names without offset
Steven Rostedt0efb4d22009-09-17 09:27:29 -0400848 * - 'S' For symbolic direct pointers with offset
849 * - 's' For symbolic direct pointers without offset
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600850 * - 'R' For a struct resource pointer, print:
851 * R address range only ([0x0-0x1f])
852 * Rt type and range ([mem 0x0-0x1f 64bit pref])
853 * Rf type, range, and flags ([mem 0x0-0x1f 64bit pref flags 0x1])
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700854 * - 'M' For a 6-byte MAC address, it prints the address in the
855 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +0000856 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
857 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
858 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
859 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
860 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
861 * IPv6 omits the colons (01020304...0f)
862 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
863 * - 'I6c' for IPv6 addresses printed as specified by
864 * http://www.ietf.org/id/draft-kawamura-ipv6-text-representation-03.txt
Linus Torvalds332d2e72008-10-20 15:07:34 +1100865 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
866 * function pointers are really function descriptors, which contain a
867 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700868 */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100869static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
870 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700871{
Linus Torvaldsd97106a2009-01-03 11:46:17 -0800872 if (!ptr)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100873 return string(buf, end, "(null)", spec);
Linus Torvaldsd97106a2009-01-03 11:46:17 -0800874
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700875 switch (*fmt) {
876 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200877 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700878 ptr = dereference_function_descriptor(ptr);
Steven Rostedt91adcd22009-09-16 20:03:06 -0400879 case 's':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700880 /* Fallthrough */
881 case 'S':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200882 return symbol_string(buf, end, ptr, spec, *fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100883 case 'R':
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600884 return resource_string(buf, end, ptr, spec, fmt);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000885 case 'M': /* Colon separated: 00:01:02:03:04:05 */
886 case 'm': /* Contiguous: 000102030405 */
887 return mac_address_string(buf, end, ptr, spec, fmt);
888 case 'I': /* Formatted IP supported
889 * 4: 1.2.3.4
890 * 6: 0001:0203:...:0708
891 * 6c: 1::708 or 1::1.2.3.4
892 */
893 case 'i': /* Contiguous:
894 * 4: 001.002.003.004
895 * 6: 000102...0f
896 */
897 switch (fmt[1]) {
898 case '6':
899 return ip6_addr_string(buf, end, ptr, spec, fmt);
900 case '4':
901 return ip4_addr_string(buf, end, ptr, spec, fmt);
902 }
Harvey Harrison4aa99602008-10-29 12:49:58 -0700903 break;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700904 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100905 spec.flags |= SMALL;
906 if (spec.field_width == -1) {
907 spec.field_width = 2*sizeof(void *);
908 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700909 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100910 spec.base = 16;
911
912 return number(buf, end, (unsigned long) ptr, spec);
913}
914
915/*
916 * Helper function to decode printf style format.
917 * Each call decode a token from the format and return the
918 * number of characters read (or likely the delta where it wants
919 * to go on the next call).
920 * The decoded token is returned through the parameters
921 *
922 * 'h', 'l', or 'L' for integer fields
923 * 'z' support added 23/7/1999 S.H.
924 * 'z' changed to 'Z' --davidm 1/25/99
925 * 't' added for ptrdiff_t
926 *
927 * @fmt: the format string
928 * @type of the token returned
929 * @flags: various flags such as +, -, # tokens..
930 * @field_width: overwritten width
931 * @base: base of the number (octal, hex, ...)
932 * @precision: precision of a number
933 * @qualifier: qualifier of a number (long, size_t, ...)
934 */
935static int format_decode(const char *fmt, struct printf_spec *spec)
936{
937 const char *start = fmt;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100938
939 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +0100940 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100941 if (spec->field_width < 0) {
942 spec->field_width = -spec->field_width;
943 spec->flags |= LEFT;
944 }
945 spec->type = FORMAT_TYPE_NONE;
946 goto precision;
947 }
948
949 /* we finished early by reading the precision */
950 if (spec->type == FORMAT_TYPE_PRECISION) {
951 if (spec->precision < 0)
952 spec->precision = 0;
953
954 spec->type = FORMAT_TYPE_NONE;
955 goto qualifier;
956 }
957
958 /* By default */
959 spec->type = FORMAT_TYPE_NONE;
960
961 for (; *fmt ; ++fmt) {
962 if (*fmt == '%')
963 break;
964 }
965
966 /* Return the current non-format string */
967 if (fmt != start || !*fmt)
968 return fmt - start;
969
970 /* Process flags */
971 spec->flags = 0;
972
973 while (1) { /* this also skips first '%' */
974 bool found = true;
975
976 ++fmt;
977
978 switch (*fmt) {
979 case '-': spec->flags |= LEFT; break;
980 case '+': spec->flags |= PLUS; break;
981 case ' ': spec->flags |= SPACE; break;
982 case '#': spec->flags |= SPECIAL; break;
983 case '0': spec->flags |= ZEROPAD; break;
984 default: found = false;
985 }
986
987 if (!found)
988 break;
989 }
990
991 /* get field width */
992 spec->field_width = -1;
993
994 if (isdigit(*fmt))
995 spec->field_width = skip_atoi(&fmt);
996 else if (*fmt == '*') {
997 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +0100998 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100999 return ++fmt - start;
1000 }
1001
1002precision:
1003 /* get the precision */
1004 spec->precision = -1;
1005 if (*fmt == '.') {
1006 ++fmt;
1007 if (isdigit(*fmt)) {
1008 spec->precision = skip_atoi(&fmt);
1009 if (spec->precision < 0)
1010 spec->precision = 0;
1011 } else if (*fmt == '*') {
1012 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01001013 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001014 return ++fmt - start;
1015 }
1016 }
1017
1018qualifier:
1019 /* get the conversion qualifier */
1020 spec->qualifier = -1;
1021 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1022 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
Zhaoleia4e94ef2009-03-27 17:07:05 +08001023 spec->qualifier = *fmt++;
1024 if (unlikely(spec->qualifier == *fmt)) {
1025 if (spec->qualifier == 'l') {
1026 spec->qualifier = 'L';
1027 ++fmt;
1028 } else if (spec->qualifier == 'h') {
1029 spec->qualifier = 'H';
1030 ++fmt;
1031 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001032 }
1033 }
1034
1035 /* default base */
1036 spec->base = 10;
1037 switch (*fmt) {
1038 case 'c':
1039 spec->type = FORMAT_TYPE_CHAR;
1040 return ++fmt - start;
1041
1042 case 's':
1043 spec->type = FORMAT_TYPE_STR;
1044 return ++fmt - start;
1045
1046 case 'p':
1047 spec->type = FORMAT_TYPE_PTR;
1048 return fmt - start;
1049 /* skip alnum */
1050
1051 case 'n':
1052 spec->type = FORMAT_TYPE_NRCHARS;
1053 return ++fmt - start;
1054
1055 case '%':
1056 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1057 return ++fmt - start;
1058
1059 /* integer number formats - set up the flags and "break" */
1060 case 'o':
1061 spec->base = 8;
1062 break;
1063
1064 case 'x':
1065 spec->flags |= SMALL;
1066
1067 case 'X':
1068 spec->base = 16;
1069 break;
1070
1071 case 'd':
1072 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001073 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001074 case 'u':
1075 break;
1076
1077 default:
1078 spec->type = FORMAT_TYPE_INVALID;
1079 return fmt - start;
1080 }
1081
1082 if (spec->qualifier == 'L')
1083 spec->type = FORMAT_TYPE_LONG_LONG;
1084 else if (spec->qualifier == 'l') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001085 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001086 spec->type = FORMAT_TYPE_LONG;
1087 else
1088 spec->type = FORMAT_TYPE_ULONG;
1089 } else if (spec->qualifier == 'Z' || spec->qualifier == 'z') {
1090 spec->type = FORMAT_TYPE_SIZE_T;
1091 } else if (spec->qualifier == 't') {
1092 spec->type = FORMAT_TYPE_PTRDIFF;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001093 } else if (spec->qualifier == 'H') {
1094 if (spec->flags & SIGN)
1095 spec->type = FORMAT_TYPE_BYTE;
1096 else
1097 spec->type = FORMAT_TYPE_UBYTE;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001098 } else if (spec->qualifier == 'h') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001099 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001100 spec->type = FORMAT_TYPE_SHORT;
1101 else
1102 spec->type = FORMAT_TYPE_USHORT;
1103 } else {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001104 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001105 spec->type = FORMAT_TYPE_INT;
1106 else
1107 spec->type = FORMAT_TYPE_UINT;
1108 }
1109
1110 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001111}
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113/**
1114 * vsnprintf - Format a string and place it in a buffer
1115 * @buf: The buffer to place the result into
1116 * @size: The size of the buffer, including the trailing null space
1117 * @fmt: The format string to use
1118 * @args: Arguments for the format string
1119 *
Andi Kleen20036fd2008-10-15 22:02:02 -07001120 * This function follows C99 vsnprintf, but has some extensions:
Steven Rostedt91adcd22009-09-16 20:03:06 -04001121 * %pS output the name of a text symbol with offset
1122 * %ps output the name of a text symbol without offset
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001123 * %pF output the name of a function pointer with its offset
1124 * %pf output the name of a function pointer without its offset
Linus Torvalds332d2e72008-10-20 15:07:34 +11001125 * %pR output the address range in a struct resource
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001126 * %n is ignored
Andi Kleen20036fd2008-10-15 22:02:02 -07001127 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 * The return value is the number of characters which would
1129 * be generated for the given input, excluding the trailing
1130 * '\0', as per ISO C99. If you want to have the exact
1131 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001132 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 * return is greater than or equal to @size, the resulting
1134 * string is truncated.
1135 *
1136 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001137 * You probably want snprintf() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 */
1139int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1140{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 unsigned long long num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 char *str, *end, c;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001143 int read;
1144 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001146 /* Reject out-of-range values early. Large positive sizes are
1147 used for unknown buffer sizes. */
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001148 if (WARN_ON_ONCE((int) size < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
1151 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001152 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001154 /* Make sure end is always >= buf */
1155 if (end < buf) {
1156 end = ((void *)-1);
1157 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 }
1159
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001160 while (*fmt) {
1161 const char *old_fmt = fmt;
1162
1163 read = format_decode(fmt, &spec);
1164
1165 fmt += read;
1166
1167 switch (spec.type) {
1168 case FORMAT_TYPE_NONE: {
1169 int copy = read;
1170 if (str < end) {
1171 if (copy > end - str)
1172 copy = end - str;
1173 memcpy(str, old_fmt, copy);
1174 }
1175 str += read;
1176 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 }
1178
Vegard Nossumed681a92009-03-14 12:08:50 +01001179 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001180 spec.field_width = va_arg(args, int);
1181 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001183 case FORMAT_TYPE_PRECISION:
1184 spec.precision = va_arg(args, int);
1185 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001187 case FORMAT_TYPE_CHAR:
1188 if (!(spec.flags & LEFT)) {
1189 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001190 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 *str = ' ';
1192 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001195 }
1196 c = (unsigned char) va_arg(args, int);
1197 if (str < end)
1198 *str = c;
1199 ++str;
1200 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001201 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001202 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001204 }
1205 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001207 case FORMAT_TYPE_STR:
1208 str = string(str, end, va_arg(args, char *), spec);
1209 break;
1210
1211 case FORMAT_TYPE_PTR:
1212 str = pointer(fmt+1, str, end, va_arg(args, void *),
1213 spec);
1214 while (isalnum(*fmt))
1215 fmt++;
1216 break;
1217
1218 case FORMAT_TYPE_PERCENT_CHAR:
1219 if (str < end)
1220 *str = '%';
1221 ++str;
1222 break;
1223
1224 case FORMAT_TYPE_INVALID:
1225 if (str < end)
1226 *str = '%';
1227 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001228 break;
1229
1230 case FORMAT_TYPE_NRCHARS: {
1231 int qualifier = spec.qualifier;
1232
1233 if (qualifier == 'l') {
1234 long *ip = va_arg(args, long *);
1235 *ip = (str - buf);
1236 } else if (qualifier == 'Z' ||
1237 qualifier == 'z') {
1238 size_t *ip = va_arg(args, size_t *);
1239 *ip = (str - buf);
1240 } else {
1241 int *ip = va_arg(args, int *);
1242 *ip = (str - buf);
1243 }
1244 break;
1245 }
1246
1247 default:
1248 switch (spec.type) {
1249 case FORMAT_TYPE_LONG_LONG:
1250 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001252 case FORMAT_TYPE_ULONG:
1253 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001255 case FORMAT_TYPE_LONG:
1256 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001258 case FORMAT_TYPE_SIZE_T:
1259 num = va_arg(args, size_t);
1260 break;
1261 case FORMAT_TYPE_PTRDIFF:
1262 num = va_arg(args, ptrdiff_t);
1263 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001264 case FORMAT_TYPE_UBYTE:
1265 num = (unsigned char) va_arg(args, int);
1266 break;
1267 case FORMAT_TYPE_BYTE:
1268 num = (signed char) va_arg(args, int);
1269 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001270 case FORMAT_TYPE_USHORT:
1271 num = (unsigned short) va_arg(args, int);
1272 break;
1273 case FORMAT_TYPE_SHORT:
1274 num = (short) va_arg(args, int);
1275 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001276 case FORMAT_TYPE_INT:
1277 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001278 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001280 num = va_arg(args, unsigned int);
1281 }
1282
1283 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001286
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001287 if (size > 0) {
1288 if (str < end)
1289 *str = '\0';
1290 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07001291 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001292 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001293
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001294 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298EXPORT_SYMBOL(vsnprintf);
1299
1300/**
1301 * vscnprintf - Format a string and place it in a buffer
1302 * @buf: The buffer to place the result into
1303 * @size: The size of the buffer, including the trailing null space
1304 * @fmt: The format string to use
1305 * @args: Arguments for the format string
1306 *
1307 * The return value is the number of characters which have been written into
1308 * the @buf not including the trailing '\0'. If @size is <= 0 the function
1309 * returns 0.
1310 *
1311 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001312 * You probably want scnprintf() instead.
Andi Kleen20036fd2008-10-15 22:02:02 -07001313 *
1314 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 */
1316int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1317{
1318 int i;
1319
1320 i=vsnprintf(buf,size,fmt,args);
1321 return (i >= size) ? (size - 1) : i;
1322}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323EXPORT_SYMBOL(vscnprintf);
1324
1325/**
1326 * snprintf - Format a string and place it in a buffer
1327 * @buf: The buffer to place the result into
1328 * @size: The size of the buffer, including the trailing null space
1329 * @fmt: The format string to use
1330 * @...: Arguments for the format string
1331 *
1332 * The return value is the number of characters which would be
1333 * generated for the given input, excluding the trailing null,
1334 * as per ISO C99. If the return is greater than or equal to
1335 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07001336 *
1337 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 */
1339int snprintf(char * buf, size_t size, const char *fmt, ...)
1340{
1341 va_list args;
1342 int i;
1343
1344 va_start(args, fmt);
1345 i=vsnprintf(buf,size,fmt,args);
1346 va_end(args);
1347 return i;
1348}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349EXPORT_SYMBOL(snprintf);
1350
1351/**
1352 * scnprintf - Format a string and place it in a buffer
1353 * @buf: The buffer to place the result into
1354 * @size: The size of the buffer, including the trailing null space
1355 * @fmt: The format string to use
1356 * @...: Arguments for the format string
1357 *
1358 * The return value is the number of characters written into @buf not including
Martin Peschkeea6f3282007-02-12 00:51:56 -08001359 * the trailing '\0'. If @size is <= 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 */
1361
1362int scnprintf(char * buf, size_t size, const char *fmt, ...)
1363{
1364 va_list args;
1365 int i;
1366
1367 va_start(args, fmt);
1368 i = vsnprintf(buf, size, fmt, args);
1369 va_end(args);
1370 return (i >= size) ? (size - 1) : i;
1371}
1372EXPORT_SYMBOL(scnprintf);
1373
1374/**
1375 * vsprintf - Format a string and place it in a buffer
1376 * @buf: The buffer to place the result into
1377 * @fmt: The format string to use
1378 * @args: Arguments for the format string
1379 *
1380 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001381 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 * buffer overflows.
1383 *
1384 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001385 * You probably want sprintf() instead.
Andi Kleen20036fd2008-10-15 22:02:02 -07001386 *
1387 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 */
1389int vsprintf(char *buf, const char *fmt, va_list args)
1390{
1391 return vsnprintf(buf, INT_MAX, fmt, args);
1392}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393EXPORT_SYMBOL(vsprintf);
1394
1395/**
1396 * sprintf - Format a string and place it in a buffer
1397 * @buf: The buffer to place the result into
1398 * @fmt: The format string to use
1399 * @...: Arguments for the format string
1400 *
1401 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001402 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07001404 *
1405 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 */
1407int sprintf(char * buf, const char *fmt, ...)
1408{
1409 va_list args;
1410 int i;
1411
1412 va_start(args, fmt);
1413 i=vsnprintf(buf, INT_MAX, fmt, args);
1414 va_end(args);
1415 return i;
1416}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417EXPORT_SYMBOL(sprintf);
1418
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001419#ifdef CONFIG_BINARY_PRINTF
1420/*
1421 * bprintf service:
1422 * vbin_printf() - VA arguments to binary data
1423 * bstr_printf() - Binary data to text string
1424 */
1425
1426/**
1427 * vbin_printf - Parse a format string and place args' binary value in a buffer
1428 * @bin_buf: The buffer to place args' binary value
1429 * @size: The size of the buffer(by words(32bits), not characters)
1430 * @fmt: The format string to use
1431 * @args: Arguments for the format string
1432 *
1433 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1434 * is skiped.
1435 *
1436 * The return value is the number of words(32bits) which would be generated for
1437 * the given input.
1438 *
1439 * NOTE:
1440 * If the return value is greater than @size, the resulting bin_buf is NOT
1441 * valid for bstr_printf().
1442 */
1443int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1444{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001445 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001446 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001447 int read;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001448
1449 str = (char *)bin_buf;
1450 end = (char *)(bin_buf + size);
1451
1452#define save_arg(type) \
1453do { \
1454 if (sizeof(type) == 8) { \
1455 unsigned long long value; \
1456 str = PTR_ALIGN(str, sizeof(u32)); \
1457 value = va_arg(args, unsigned long long); \
1458 if (str + sizeof(type) <= end) { \
1459 *(u32 *)str = *(u32 *)&value; \
1460 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1461 } \
1462 } else { \
1463 unsigned long value; \
1464 str = PTR_ALIGN(str, sizeof(type)); \
1465 value = va_arg(args, int); \
1466 if (str + sizeof(type) <= end) \
1467 *(typeof(type) *)str = (type)value; \
1468 } \
1469 str += sizeof(type); \
1470} while (0)
1471
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001472
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001473 while (*fmt) {
1474 read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001475
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001476 fmt += read;
1477
1478 switch (spec.type) {
1479 case FORMAT_TYPE_NONE:
1480 break;
1481
Vegard Nossumed681a92009-03-14 12:08:50 +01001482 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001483 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001484 save_arg(int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001485 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001486
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001487 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001488 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001489 break;
1490
1491 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001492 const char *save_str = va_arg(args, char *);
1493 size_t len;
1494 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1495 || (unsigned long)save_str < PAGE_SIZE)
1496 save_str = "<NULL>";
1497 len = strlen(save_str);
1498 if (str + len + 1 < end)
1499 memcpy(str, save_str, len + 1);
1500 str += len + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001501 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001502 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001503
1504 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001505 save_arg(void *);
1506 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001507 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001508 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001509 break;
1510
1511 case FORMAT_TYPE_PERCENT_CHAR:
1512 break;
1513
1514 case FORMAT_TYPE_INVALID:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001515 break;
1516
1517 case FORMAT_TYPE_NRCHARS: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001518 /* skip %n 's argument */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001519 int qualifier = spec.qualifier;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001520 void *skip_arg;
1521 if (qualifier == 'l')
1522 skip_arg = va_arg(args, long *);
1523 else if (qualifier == 'Z' || qualifier == 'z')
1524 skip_arg = va_arg(args, size_t *);
1525 else
1526 skip_arg = va_arg(args, int *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001527 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001528 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001529
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001530 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001531 switch (spec.type) {
1532
1533 case FORMAT_TYPE_LONG_LONG:
1534 save_arg(long long);
1535 break;
1536 case FORMAT_TYPE_ULONG:
1537 case FORMAT_TYPE_LONG:
1538 save_arg(unsigned long);
1539 break;
1540 case FORMAT_TYPE_SIZE_T:
1541 save_arg(size_t);
1542 break;
1543 case FORMAT_TYPE_PTRDIFF:
1544 save_arg(ptrdiff_t);
1545 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001546 case FORMAT_TYPE_UBYTE:
1547 case FORMAT_TYPE_BYTE:
1548 save_arg(char);
1549 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001550 case FORMAT_TYPE_USHORT:
1551 case FORMAT_TYPE_SHORT:
1552 save_arg(short);
1553 break;
1554 default:
1555 save_arg(int);
1556 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001557 }
1558 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001559 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001560
1561#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001562}
1563EXPORT_SYMBOL_GPL(vbin_printf);
1564
1565/**
1566 * bstr_printf - Format a string from binary arguments and place it in a buffer
1567 * @buf: The buffer to place the result into
1568 * @size: The size of the buffer, including the trailing null space
1569 * @fmt: The format string to use
1570 * @bin_buf: Binary arguments for the format string
1571 *
1572 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1573 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1574 * a binary buffer that generated by vbin_printf.
1575 *
1576 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001577 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001578 *
1579 * The return value is the number of characters which would
1580 * be generated for the given input, excluding the trailing
1581 * '\0', as per ISO C99. If you want to have the exact
1582 * number of characters written into @buf as return value
1583 * (not including the trailing '\0'), use vscnprintf(). If the
1584 * return is greater than or equal to @size, the resulting
1585 * string is truncated.
1586 */
1587int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1588{
1589 unsigned long long num;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001590 char *str, *end, c;
1591 const char *args = (const char *)bin_buf;
1592
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001593 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001594
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001595 if (WARN_ON_ONCE((int) size < 0))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001596 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001597
1598 str = buf;
1599 end = buf + size;
1600
1601#define get_arg(type) \
1602({ \
1603 typeof(type) value; \
1604 if (sizeof(type) == 8) { \
1605 args = PTR_ALIGN(args, sizeof(u32)); \
1606 *(u32 *)&value = *(u32 *)args; \
1607 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1608 } else { \
1609 args = PTR_ALIGN(args, sizeof(type)); \
1610 value = *(typeof(type) *)args; \
1611 } \
1612 args += sizeof(type); \
1613 value; \
1614})
1615
1616 /* Make sure end is always >= buf */
1617 if (end < buf) {
1618 end = ((void *)-1);
1619 size = end - buf;
1620 }
1621
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001622 while (*fmt) {
1623 int read;
1624 const char *old_fmt = fmt;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001625
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001626 read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001627
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001628 fmt += read;
1629
1630 switch (spec.type) {
1631 case FORMAT_TYPE_NONE: {
1632 int copy = read;
1633 if (str < end) {
1634 if (copy > end - str)
1635 copy = end - str;
1636 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001637 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001638 str += read;
1639 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001640 }
1641
Vegard Nossumed681a92009-03-14 12:08:50 +01001642 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001643 spec.field_width = get_arg(int);
1644 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001645
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001646 case FORMAT_TYPE_PRECISION:
1647 spec.precision = get_arg(int);
1648 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001649
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001650 case FORMAT_TYPE_CHAR:
1651 if (!(spec.flags & LEFT)) {
1652 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001653 if (str < end)
1654 *str = ' ';
1655 ++str;
1656 }
1657 }
1658 c = (unsigned char) get_arg(char);
1659 if (str < end)
1660 *str = c;
1661 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001662 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001663 if (str < end)
1664 *str = ' ';
1665 ++str;
1666 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001667 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001668
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001669 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001670 const char *str_arg = args;
1671 size_t len = strlen(str_arg);
1672 args += len + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001673 str = string(str, end, (char *)str_arg, spec);
1674 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001675 }
1676
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001677 case FORMAT_TYPE_PTR:
1678 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1679 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001680 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001681 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001682
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001683 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001684 if (str < end)
1685 *str = '%';
1686 ++str;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001687 break;
1688
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001689 case FORMAT_TYPE_INVALID:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001690 if (str < end)
1691 *str = '%';
1692 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001693 break;
1694
1695 case FORMAT_TYPE_NRCHARS:
1696 /* skip */
1697 break;
1698
1699 default:
1700 switch (spec.type) {
1701
1702 case FORMAT_TYPE_LONG_LONG:
1703 num = get_arg(long long);
1704 break;
1705 case FORMAT_TYPE_ULONG:
1706 num = get_arg(unsigned long);
1707 break;
1708 case FORMAT_TYPE_LONG:
1709 num = get_arg(unsigned long);
1710 break;
1711 case FORMAT_TYPE_SIZE_T:
1712 num = get_arg(size_t);
1713 break;
1714 case FORMAT_TYPE_PTRDIFF:
1715 num = get_arg(ptrdiff_t);
1716 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001717 case FORMAT_TYPE_UBYTE:
1718 num = get_arg(unsigned char);
1719 break;
1720 case FORMAT_TYPE_BYTE:
1721 num = get_arg(signed char);
1722 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001723 case FORMAT_TYPE_USHORT:
1724 num = get_arg(unsigned short);
1725 break;
1726 case FORMAT_TYPE_SHORT:
1727 num = get_arg(short);
1728 break;
1729 case FORMAT_TYPE_UINT:
1730 num = get_arg(unsigned int);
1731 break;
1732 default:
1733 num = get_arg(int);
1734 }
1735
1736 str = number(str, end, num, spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001737 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001738 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001739
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001740 if (size > 0) {
1741 if (str < end)
1742 *str = '\0';
1743 else
1744 end[-1] = '\0';
1745 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001746
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001747#undef get_arg
1748
1749 /* the trailing null byte doesn't count towards the total */
1750 return str - buf;
1751}
1752EXPORT_SYMBOL_GPL(bstr_printf);
1753
1754/**
1755 * bprintf - Parse a format string and place args' binary value in a buffer
1756 * @bin_buf: The buffer to place args' binary value
1757 * @size: The size of the buffer(by words(32bits), not characters)
1758 * @fmt: The format string to use
1759 * @...: Arguments for the format string
1760 *
1761 * The function returns the number of words(u32) written
1762 * into @bin_buf.
1763 */
1764int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1765{
1766 va_list args;
1767 int ret;
1768
1769 va_start(args, fmt);
1770 ret = vbin_printf(bin_buf, size, fmt, args);
1771 va_end(args);
1772 return ret;
1773}
1774EXPORT_SYMBOL_GPL(bprintf);
1775
1776#endif /* CONFIG_BINARY_PRINTF */
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778/**
1779 * vsscanf - Unformat a buffer into a list of arguments
1780 * @buf: input buffer
1781 * @fmt: format of buffer
1782 * @args: arguments
1783 */
1784int vsscanf(const char * buf, const char * fmt, va_list args)
1785{
1786 const char *str = buf;
1787 char *next;
1788 char digit;
1789 int num = 0;
1790 int qualifier;
1791 int base;
1792 int field_width;
1793 int is_sign = 0;
1794
1795 while(*fmt && *str) {
1796 /* skip any white space in format */
1797 /* white space in format matchs any amount of
1798 * white space, including none, in the input.
1799 */
1800 if (isspace(*fmt)) {
1801 while (isspace(*fmt))
1802 ++fmt;
1803 while (isspace(*str))
1804 ++str;
1805 }
1806
1807 /* anything that is not a conversion must match exactly */
1808 if (*fmt != '%' && *fmt) {
1809 if (*fmt++ != *str++)
1810 break;
1811 continue;
1812 }
1813
1814 if (!*fmt)
1815 break;
1816 ++fmt;
1817
1818 /* skip this conversion.
1819 * advance both strings to next white space
1820 */
1821 if (*fmt == '*') {
Andy Spencer8fccae22009-10-01 15:44:27 -07001822 while (!isspace(*fmt) && *fmt != '%' && *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 fmt++;
1824 while (!isspace(*str) && *str)
1825 str++;
1826 continue;
1827 }
1828
1829 /* get field width */
1830 field_width = -1;
1831 if (isdigit(*fmt))
1832 field_width = skip_atoi(&fmt);
1833
1834 /* get conversion qualifier */
1835 qualifier = -1;
1836 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1837 *fmt == 'Z' || *fmt == 'z') {
1838 qualifier = *fmt++;
1839 if (unlikely(qualifier == *fmt)) {
1840 if (qualifier == 'h') {
1841 qualifier = 'H';
1842 fmt++;
1843 } else if (qualifier == 'l') {
1844 qualifier = 'L';
1845 fmt++;
1846 }
1847 }
1848 }
1849 base = 10;
1850 is_sign = 0;
1851
1852 if (!*fmt || !*str)
1853 break;
1854
1855 switch(*fmt++) {
1856 case 'c':
1857 {
1858 char *s = (char *) va_arg(args,char*);
1859 if (field_width == -1)
1860 field_width = 1;
1861 do {
1862 *s++ = *str++;
1863 } while (--field_width > 0 && *str);
1864 num++;
1865 }
1866 continue;
1867 case 's':
1868 {
1869 char *s = (char *) va_arg(args, char *);
1870 if(field_width == -1)
1871 field_width = INT_MAX;
1872 /* first, skip leading white space in buffer */
1873 while (isspace(*str))
1874 str++;
1875
1876 /* now copy until next white space */
1877 while (*str && !isspace(*str) && field_width--) {
1878 *s++ = *str++;
1879 }
1880 *s = '\0';
1881 num++;
1882 }
1883 continue;
1884 case 'n':
1885 /* return number of characters read so far */
1886 {
1887 int *i = (int *)va_arg(args,int*);
1888 *i = str - buf;
1889 }
1890 continue;
1891 case 'o':
1892 base = 8;
1893 break;
1894 case 'x':
1895 case 'X':
1896 base = 16;
1897 break;
1898 case 'i':
1899 base = 0;
1900 case 'd':
1901 is_sign = 1;
1902 case 'u':
1903 break;
1904 case '%':
1905 /* looking for '%' in str */
1906 if (*str++ != '%')
1907 return num;
1908 continue;
1909 default:
1910 /* invalid format; stop here */
1911 return num;
1912 }
1913
1914 /* have some sort of integer conversion.
1915 * first, skip white space in buffer.
1916 */
1917 while (isspace(*str))
1918 str++;
1919
1920 digit = *str;
1921 if (is_sign && digit == '-')
1922 digit = *(str + 1);
1923
1924 if (!digit
1925 || (base == 16 && !isxdigit(digit))
1926 || (base == 10 && !isdigit(digit))
1927 || (base == 8 && (!isdigit(digit) || digit > '7'))
1928 || (base == 0 && !isdigit(digit)))
1929 break;
1930
1931 switch(qualifier) {
1932 case 'H': /* that's 'hh' in format */
1933 if (is_sign) {
1934 signed char *s = (signed char *) va_arg(args,signed char *);
1935 *s = (signed char) simple_strtol(str,&next,base);
1936 } else {
1937 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1938 *s = (unsigned char) simple_strtoul(str, &next, base);
1939 }
1940 break;
1941 case 'h':
1942 if (is_sign) {
1943 short *s = (short *) va_arg(args,short *);
1944 *s = (short) simple_strtol(str,&next,base);
1945 } else {
1946 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1947 *s = (unsigned short) simple_strtoul(str, &next, base);
1948 }
1949 break;
1950 case 'l':
1951 if (is_sign) {
1952 long *l = (long *) va_arg(args,long *);
1953 *l = simple_strtol(str,&next,base);
1954 } else {
1955 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1956 *l = simple_strtoul(str,&next,base);
1957 }
1958 break;
1959 case 'L':
1960 if (is_sign) {
1961 long long *l = (long long*) va_arg(args,long long *);
1962 *l = simple_strtoll(str,&next,base);
1963 } else {
1964 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1965 *l = simple_strtoull(str,&next,base);
1966 }
1967 break;
1968 case 'Z':
1969 case 'z':
1970 {
1971 size_t *s = (size_t*) va_arg(args,size_t*);
1972 *s = (size_t) simple_strtoul(str,&next,base);
1973 }
1974 break;
1975 default:
1976 if (is_sign) {
1977 int *i = (int *) va_arg(args, int*);
1978 *i = (int) simple_strtol(str,&next,base);
1979 } else {
1980 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1981 *i = (unsigned int) simple_strtoul(str,&next,base);
1982 }
1983 break;
1984 }
1985 num++;
1986
1987 if (!next)
1988 break;
1989 str = next;
1990 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07001991
1992 /*
1993 * Now we've come all the way through so either the input string or the
1994 * format ended. In the former case, there can be a %n at the current
1995 * position in the format that needs to be filled.
1996 */
1997 if (*fmt == '%' && *(fmt + 1) == 'n') {
1998 int *p = (int *)va_arg(args, int *);
1999 *p = str - buf;
2000 }
2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return num;
2003}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004EXPORT_SYMBOL(vsscanf);
2005
2006/**
2007 * sscanf - Unformat a buffer into a list of arguments
2008 * @buf: input buffer
2009 * @fmt: formatting of buffer
2010 * @...: resulting arguments
2011 */
2012int sscanf(const char * buf, const char * fmt, ...)
2013{
2014 va_list args;
2015 int i;
2016
2017 va_start(args,fmt);
2018 i = vsscanf(buf,fmt,args);
2019 va_end(args);
2020 return i;
2021}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022EXPORT_SYMBOL(sscanf);