blob: 6438cd5599eefe583451b9b5bd858b9ab5a0ca38 [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 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600627
628 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
629 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
630#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
631#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
632#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref disabled]")
633#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
634 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
635 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
636
Linus Torvalds332d2e72008-10-20 15:07:34 +1100637 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600638 int size = -1, addr = 0;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600639 int decode = (fmt[0] == 'R') ? 1 : 0;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100640
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600641 if (res->flags & IORESOURCE_IO) {
Linus Torvalds332d2e72008-10-20 15:07:34 +1100642 size = IO_RSRC_PRINTK_SIZE;
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600643 addr = 1;
644 } else if (res->flags & IORESOURCE_MEM) {
Linus Torvalds332d2e72008-10-20 15:07:34 +1100645 size = MEM_RSRC_PRINTK_SIZE;
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600646 addr = 1;
647 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100648
649 *p++ = '[';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600650 if (res->flags & IORESOURCE_IO)
651 p = string(p, pend, "io ", str_spec);
652 else if (res->flags & IORESOURCE_MEM)
653 p = string(p, pend, "mem ", str_spec);
654 else if (res->flags & IORESOURCE_IRQ)
655 p = string(p, pend, "irq ", str_spec);
656 else if (res->flags & IORESOURCE_DMA)
657 p = string(p, pend, "dma ", str_spec);
658 else {
659 p = string(p, pend, "??? ", str_spec);
660 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600661 }
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600662 hex_spec.field_width = size;
663 p = number(p, pend, res->start, addr ? hex_spec : dec_spec);
664 if (res->start != res->end) {
665 *p++ = '-';
666 p = number(p, pend, res->end, addr ? hex_spec : dec_spec);
667 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600668 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600669 if (res->flags & IORESOURCE_MEM_64)
670 p = string(p, pend, " 64bit", str_spec);
671 if (res->flags & IORESOURCE_PREFETCH)
672 p = string(p, pend, " pref", str_spec);
673 if (res->flags & IORESOURCE_DISABLED)
674 p = string(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600675 } else {
676 p = string(p, pend, " flags ", str_spec);
677 p = number(p, pend, res->flags, flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600678 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100679 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600680 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +1100681
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100682 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100683}
684
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100685static char *mac_address_string(char *buf, char *end, u8 *addr,
Joe Perches8a27f7c2009-08-17 12:29:44 +0000686 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700687{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000688 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700689 char *p = mac_addr;
690 int i;
691
692 for (i = 0; i < 6; i++) {
693 p = pack_hex_byte(p, addr[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000694 if (fmt[0] == 'M' && i != 5)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700695 *p++ = ':';
696 }
697 *p = '\0';
698
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100699 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700700}
701
Joe Perches8a27f7c2009-08-17 12:29:44 +0000702static char *ip4_string(char *p, const u8 *addr, bool leading_zeros)
Harvey Harrison689afa72008-10-28 16:04:44 -0700703{
Harvey Harrison689afa72008-10-28 16:04:44 -0700704 int i;
705
Joe Perches8a27f7c2009-08-17 12:29:44 +0000706 for (i = 0; i < 4; i++) {
707 char temp[3]; /* hold each IP quad in reverse order */
708 int digits = put_dec_trunc(temp, addr[i]) - temp;
709 if (leading_zeros) {
710 if (digits < 3)
711 *p++ = '0';
712 if (digits < 2)
713 *p++ = '0';
714 }
715 /* reverse the digits in the quad */
716 while (digits--)
717 *p++ = temp[digits];
718 if (i < 3)
719 *p++ = '.';
720 }
721
722 *p = '\0';
723 return p;
724}
725
Joe Percheseb78cd22009-09-18 13:04:06 +0000726static char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000727{
728 int i;
729 int j;
730 int range;
731 unsigned char zerolength[8];
732 int longest = 1;
733 int colonpos = -1;
734 u16 word;
735 u8 hi;
736 u8 lo;
737 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +0000738 bool useIPv4;
739 struct in6_addr in6;
740
741 memcpy(&in6, addr, sizeof(struct in6_addr));
742
743 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000744
745 memset(zerolength, 0, sizeof(zerolength));
746
747 if (useIPv4)
748 range = 6;
749 else
750 range = 8;
751
752 /* find position of longest 0 run */
753 for (i = 0; i < range; i++) {
754 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +0000755 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000756 break;
757 zerolength[i]++;
758 }
759 }
760 for (i = 0; i < range; i++) {
761 if (zerolength[i] > longest) {
762 longest = zerolength[i];
763 colonpos = i;
764 }
765 }
766
767 /* emit address */
768 for (i = 0; i < range; i++) {
769 if (i == colonpos) {
770 if (needcolon || i == 0)
771 *p++ = ':';
772 *p++ = ':';
773 needcolon = false;
774 i += longest - 1;
775 continue;
776 }
777 if (needcolon) {
778 *p++ = ':';
779 needcolon = false;
780 }
781 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +0000782 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000783 hi = word >> 8;
784 lo = word & 0xff;
785 if (hi) {
786 if (hi > 0x0f)
787 p = pack_hex_byte(p, hi);
788 else
789 *p++ = hex_asc_lo(hi);
790 }
791 if (hi || lo > 0x0f)
792 p = pack_hex_byte(p, lo);
793 else
794 *p++ = hex_asc_lo(lo);
795 needcolon = true;
796 }
797
798 if (useIPv4) {
799 if (needcolon)
800 *p++ = ':';
Joe Percheseb78cd22009-09-18 13:04:06 +0000801 p = ip4_string(p, &in6.s6_addr[12], false);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000802 }
803
804 *p = '\0';
805 return p;
806}
807
Joe Percheseb78cd22009-09-18 13:04:06 +0000808static char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000809{
810 int i;
Harvey Harrison689afa72008-10-28 16:04:44 -0700811 for (i = 0; i < 8; i++) {
Joe Percheseb78cd22009-09-18 13:04:06 +0000812 p = pack_hex_byte(p, *addr++);
813 p = pack_hex_byte(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000814 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -0700815 *p++ = ':';
816 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000817
Harvey Harrison689afa72008-10-28 16:04:44 -0700818 *p = '\0';
Joe Perches8a27f7c2009-08-17 12:29:44 +0000819 return p;
820}
821
822static char *ip6_addr_string(char *buf, char *end, const u8 *addr,
823 struct printf_spec spec, const char *fmt)
824{
825 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
826
827 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +0000828 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000829 else
Joe Percheseb78cd22009-09-18 13:04:06 +0000830 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -0700831
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100832 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -0700833}
834
Joe Perches8a27f7c2009-08-17 12:29:44 +0000835static char *ip4_addr_string(char *buf, char *end, const u8 *addr,
836 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -0700837{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000838 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -0700839
Joe Perches8a27f7c2009-08-17 12:29:44 +0000840 ip4_string(ip4_addr, addr, fmt[0] == 'i');
Harvey Harrison4aa99602008-10-29 12:49:58 -0700841
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100842 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700843}
844
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700845/*
846 * Show a '%p' thing. A kernel extension is that the '%p' is followed
847 * by an extra set of alphanumeric characters that are extended format
848 * specifiers.
849 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100850 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700851 *
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200852 * - 'F' For symbolic function descriptor pointers with offset
853 * - 'f' For simple symbolic function names without offset
Steven Rostedt0efb4d22009-09-17 09:27:29 -0400854 * - 'S' For symbolic direct pointers with offset
855 * - 's' For symbolic direct pointers without offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600856 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
857 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700858 * - 'M' For a 6-byte MAC address, it prints the address in the
859 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +0000860 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
861 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
862 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
863 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
864 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
865 * IPv6 omits the colons (01020304...0f)
866 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
867 * - 'I6c' for IPv6 addresses printed as specified by
868 * http://www.ietf.org/id/draft-kawamura-ipv6-text-representation-03.txt
Linus Torvalds332d2e72008-10-20 15:07:34 +1100869 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
870 * function pointers are really function descriptors, which contain a
871 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700872 */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100873static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
874 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700875{
Linus Torvaldsd97106a2009-01-03 11:46:17 -0800876 if (!ptr)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100877 return string(buf, end, "(null)", spec);
Linus Torvaldsd97106a2009-01-03 11:46:17 -0800878
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700879 switch (*fmt) {
880 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200881 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700882 ptr = dereference_function_descriptor(ptr);
Steven Rostedt91adcd22009-09-16 20:03:06 -0400883 case 's':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700884 /* Fallthrough */
885 case 'S':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200886 return symbol_string(buf, end, ptr, spec, *fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100887 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600888 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600889 return resource_string(buf, end, ptr, spec, fmt);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000890 case 'M': /* Colon separated: 00:01:02:03:04:05 */
891 case 'm': /* Contiguous: 000102030405 */
892 return mac_address_string(buf, end, ptr, spec, fmt);
893 case 'I': /* Formatted IP supported
894 * 4: 1.2.3.4
895 * 6: 0001:0203:...:0708
896 * 6c: 1::708 or 1::1.2.3.4
897 */
898 case 'i': /* Contiguous:
899 * 4: 001.002.003.004
900 * 6: 000102...0f
901 */
902 switch (fmt[1]) {
903 case '6':
904 return ip6_addr_string(buf, end, ptr, spec, fmt);
905 case '4':
906 return ip4_addr_string(buf, end, ptr, spec, fmt);
907 }
Harvey Harrison4aa99602008-10-29 12:49:58 -0700908 break;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700909 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100910 spec.flags |= SMALL;
911 if (spec.field_width == -1) {
912 spec.field_width = 2*sizeof(void *);
913 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700914 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100915 spec.base = 16;
916
917 return number(buf, end, (unsigned long) ptr, spec);
918}
919
920/*
921 * Helper function to decode printf style format.
922 * Each call decode a token from the format and return the
923 * number of characters read (or likely the delta where it wants
924 * to go on the next call).
925 * The decoded token is returned through the parameters
926 *
927 * 'h', 'l', or 'L' for integer fields
928 * 'z' support added 23/7/1999 S.H.
929 * 'z' changed to 'Z' --davidm 1/25/99
930 * 't' added for ptrdiff_t
931 *
932 * @fmt: the format string
933 * @type of the token returned
934 * @flags: various flags such as +, -, # tokens..
935 * @field_width: overwritten width
936 * @base: base of the number (octal, hex, ...)
937 * @precision: precision of a number
938 * @qualifier: qualifier of a number (long, size_t, ...)
939 */
940static int format_decode(const char *fmt, struct printf_spec *spec)
941{
942 const char *start = fmt;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100943
944 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +0100945 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100946 if (spec->field_width < 0) {
947 spec->field_width = -spec->field_width;
948 spec->flags |= LEFT;
949 }
950 spec->type = FORMAT_TYPE_NONE;
951 goto precision;
952 }
953
954 /* we finished early by reading the precision */
955 if (spec->type == FORMAT_TYPE_PRECISION) {
956 if (spec->precision < 0)
957 spec->precision = 0;
958
959 spec->type = FORMAT_TYPE_NONE;
960 goto qualifier;
961 }
962
963 /* By default */
964 spec->type = FORMAT_TYPE_NONE;
965
966 for (; *fmt ; ++fmt) {
967 if (*fmt == '%')
968 break;
969 }
970
971 /* Return the current non-format string */
972 if (fmt != start || !*fmt)
973 return fmt - start;
974
975 /* Process flags */
976 spec->flags = 0;
977
978 while (1) { /* this also skips first '%' */
979 bool found = true;
980
981 ++fmt;
982
983 switch (*fmt) {
984 case '-': spec->flags |= LEFT; break;
985 case '+': spec->flags |= PLUS; break;
986 case ' ': spec->flags |= SPACE; break;
987 case '#': spec->flags |= SPECIAL; break;
988 case '0': spec->flags |= ZEROPAD; break;
989 default: found = false;
990 }
991
992 if (!found)
993 break;
994 }
995
996 /* get field width */
997 spec->field_width = -1;
998
999 if (isdigit(*fmt))
1000 spec->field_width = skip_atoi(&fmt);
1001 else if (*fmt == '*') {
1002 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01001003 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001004 return ++fmt - start;
1005 }
1006
1007precision:
1008 /* get the precision */
1009 spec->precision = -1;
1010 if (*fmt == '.') {
1011 ++fmt;
1012 if (isdigit(*fmt)) {
1013 spec->precision = skip_atoi(&fmt);
1014 if (spec->precision < 0)
1015 spec->precision = 0;
1016 } else if (*fmt == '*') {
1017 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01001018 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001019 return ++fmt - start;
1020 }
1021 }
1022
1023qualifier:
1024 /* get the conversion qualifier */
1025 spec->qualifier = -1;
1026 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1027 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
Zhaoleia4e94ef2009-03-27 17:07:05 +08001028 spec->qualifier = *fmt++;
1029 if (unlikely(spec->qualifier == *fmt)) {
1030 if (spec->qualifier == 'l') {
1031 spec->qualifier = 'L';
1032 ++fmt;
1033 } else if (spec->qualifier == 'h') {
1034 spec->qualifier = 'H';
1035 ++fmt;
1036 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001037 }
1038 }
1039
1040 /* default base */
1041 spec->base = 10;
1042 switch (*fmt) {
1043 case 'c':
1044 spec->type = FORMAT_TYPE_CHAR;
1045 return ++fmt - start;
1046
1047 case 's':
1048 spec->type = FORMAT_TYPE_STR;
1049 return ++fmt - start;
1050
1051 case 'p':
1052 spec->type = FORMAT_TYPE_PTR;
1053 return fmt - start;
1054 /* skip alnum */
1055
1056 case 'n':
1057 spec->type = FORMAT_TYPE_NRCHARS;
1058 return ++fmt - start;
1059
1060 case '%':
1061 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1062 return ++fmt - start;
1063
1064 /* integer number formats - set up the flags and "break" */
1065 case 'o':
1066 spec->base = 8;
1067 break;
1068
1069 case 'x':
1070 spec->flags |= SMALL;
1071
1072 case 'X':
1073 spec->base = 16;
1074 break;
1075
1076 case 'd':
1077 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001078 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001079 case 'u':
1080 break;
1081
1082 default:
1083 spec->type = FORMAT_TYPE_INVALID;
1084 return fmt - start;
1085 }
1086
1087 if (spec->qualifier == 'L')
1088 spec->type = FORMAT_TYPE_LONG_LONG;
1089 else if (spec->qualifier == 'l') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001090 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001091 spec->type = FORMAT_TYPE_LONG;
1092 else
1093 spec->type = FORMAT_TYPE_ULONG;
1094 } else if (spec->qualifier == 'Z' || spec->qualifier == 'z') {
1095 spec->type = FORMAT_TYPE_SIZE_T;
1096 } else if (spec->qualifier == 't') {
1097 spec->type = FORMAT_TYPE_PTRDIFF;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001098 } else if (spec->qualifier == 'H') {
1099 if (spec->flags & SIGN)
1100 spec->type = FORMAT_TYPE_BYTE;
1101 else
1102 spec->type = FORMAT_TYPE_UBYTE;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001103 } else if (spec->qualifier == 'h') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001104 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001105 spec->type = FORMAT_TYPE_SHORT;
1106 else
1107 spec->type = FORMAT_TYPE_USHORT;
1108 } else {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001109 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001110 spec->type = FORMAT_TYPE_INT;
1111 else
1112 spec->type = FORMAT_TYPE_UINT;
1113 }
1114
1115 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001116}
1117
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118/**
1119 * vsnprintf - Format a string and place it in a buffer
1120 * @buf: The buffer to place the result into
1121 * @size: The size of the buffer, including the trailing null space
1122 * @fmt: The format string to use
1123 * @args: Arguments for the format string
1124 *
Andi Kleen20036fd2008-10-15 22:02:02 -07001125 * This function follows C99 vsnprintf, but has some extensions:
Steven Rostedt91adcd22009-09-16 20:03:06 -04001126 * %pS output the name of a text symbol with offset
1127 * %ps output the name of a text symbol without offset
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001128 * %pF output the name of a function pointer with its offset
1129 * %pf output the name of a function pointer without its offset
Linus Torvalds332d2e72008-10-20 15:07:34 +11001130 * %pR output the address range in a struct resource
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001131 * %n is ignored
Andi Kleen20036fd2008-10-15 22:02:02 -07001132 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 * The return value is the number of characters which would
1134 * be generated for the given input, excluding the trailing
1135 * '\0', as per ISO C99. If you want to have the exact
1136 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001137 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 * return is greater than or equal to @size, the resulting
1139 * string is truncated.
1140 *
1141 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001142 * You probably want snprintf() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 */
1144int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1145{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 unsigned long long num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 char *str, *end, c;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001148 int read;
1149 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001151 /* Reject out-of-range values early. Large positive sizes are
1152 used for unknown buffer sizes. */
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001153 if (WARN_ON_ONCE((int) size < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
1156 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001157 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001159 /* Make sure end is always >= buf */
1160 if (end < buf) {
1161 end = ((void *)-1);
1162 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
1164
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001165 while (*fmt) {
1166 const char *old_fmt = fmt;
1167
1168 read = format_decode(fmt, &spec);
1169
1170 fmt += read;
1171
1172 switch (spec.type) {
1173 case FORMAT_TYPE_NONE: {
1174 int copy = read;
1175 if (str < end) {
1176 if (copy > end - str)
1177 copy = end - str;
1178 memcpy(str, old_fmt, copy);
1179 }
1180 str += read;
1181 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 }
1183
Vegard Nossumed681a92009-03-14 12:08:50 +01001184 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001185 spec.field_width = va_arg(args, int);
1186 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001188 case FORMAT_TYPE_PRECISION:
1189 spec.precision = va_arg(args, int);
1190 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001192 case FORMAT_TYPE_CHAR:
1193 if (!(spec.flags & LEFT)) {
1194 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001195 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 *str = ' ';
1197 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001200 }
1201 c = (unsigned char) va_arg(args, int);
1202 if (str < end)
1203 *str = c;
1204 ++str;
1205 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001206 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001207 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001209 }
1210 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001212 case FORMAT_TYPE_STR:
1213 str = string(str, end, va_arg(args, char *), spec);
1214 break;
1215
1216 case FORMAT_TYPE_PTR:
1217 str = pointer(fmt+1, str, end, va_arg(args, void *),
1218 spec);
1219 while (isalnum(*fmt))
1220 fmt++;
1221 break;
1222
1223 case FORMAT_TYPE_PERCENT_CHAR:
1224 if (str < end)
1225 *str = '%';
1226 ++str;
1227 break;
1228
1229 case FORMAT_TYPE_INVALID:
1230 if (str < end)
1231 *str = '%';
1232 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001233 break;
1234
1235 case FORMAT_TYPE_NRCHARS: {
1236 int qualifier = spec.qualifier;
1237
1238 if (qualifier == 'l') {
1239 long *ip = va_arg(args, long *);
1240 *ip = (str - buf);
1241 } else if (qualifier == 'Z' ||
1242 qualifier == 'z') {
1243 size_t *ip = va_arg(args, size_t *);
1244 *ip = (str - buf);
1245 } else {
1246 int *ip = va_arg(args, int *);
1247 *ip = (str - buf);
1248 }
1249 break;
1250 }
1251
1252 default:
1253 switch (spec.type) {
1254 case FORMAT_TYPE_LONG_LONG:
1255 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001257 case FORMAT_TYPE_ULONG:
1258 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001260 case FORMAT_TYPE_LONG:
1261 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001263 case FORMAT_TYPE_SIZE_T:
1264 num = va_arg(args, size_t);
1265 break;
1266 case FORMAT_TYPE_PTRDIFF:
1267 num = va_arg(args, ptrdiff_t);
1268 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001269 case FORMAT_TYPE_UBYTE:
1270 num = (unsigned char) va_arg(args, int);
1271 break;
1272 case FORMAT_TYPE_BYTE:
1273 num = (signed char) va_arg(args, int);
1274 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001275 case FORMAT_TYPE_USHORT:
1276 num = (unsigned short) va_arg(args, int);
1277 break;
1278 case FORMAT_TYPE_SHORT:
1279 num = (short) va_arg(args, int);
1280 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001281 case FORMAT_TYPE_INT:
1282 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001283 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001285 num = va_arg(args, unsigned int);
1286 }
1287
1288 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001291
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001292 if (size > 0) {
1293 if (str < end)
1294 *str = '\0';
1295 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07001296 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001297 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001298
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001299 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001301
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303EXPORT_SYMBOL(vsnprintf);
1304
1305/**
1306 * vscnprintf - Format a string and place it in a buffer
1307 * @buf: The buffer to place the result into
1308 * @size: The size of the buffer, including the trailing null space
1309 * @fmt: The format string to use
1310 * @args: Arguments for the format string
1311 *
1312 * The return value is the number of characters which have been written into
1313 * the @buf not including the trailing '\0'. If @size is <= 0 the function
1314 * returns 0.
1315 *
1316 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001317 * You probably want scnprintf() instead.
Andi Kleen20036fd2008-10-15 22:02:02 -07001318 *
1319 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 */
1321int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1322{
1323 int i;
1324
1325 i=vsnprintf(buf,size,fmt,args);
1326 return (i >= size) ? (size - 1) : i;
1327}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328EXPORT_SYMBOL(vscnprintf);
1329
1330/**
1331 * snprintf - Format a string and place it in a buffer
1332 * @buf: The buffer to place the result into
1333 * @size: The size of the buffer, including the trailing null space
1334 * @fmt: The format string to use
1335 * @...: Arguments for the format string
1336 *
1337 * The return value is the number of characters which would be
1338 * generated for the given input, excluding the trailing null,
1339 * as per ISO C99. If the return is greater than or equal to
1340 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07001341 *
1342 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 */
1344int snprintf(char * buf, size_t size, const char *fmt, ...)
1345{
1346 va_list args;
1347 int i;
1348
1349 va_start(args, fmt);
1350 i=vsnprintf(buf,size,fmt,args);
1351 va_end(args);
1352 return i;
1353}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354EXPORT_SYMBOL(snprintf);
1355
1356/**
1357 * scnprintf - Format a string and place it in a buffer
1358 * @buf: The buffer to place the result into
1359 * @size: The size of the buffer, including the trailing null space
1360 * @fmt: The format string to use
1361 * @...: Arguments for the format string
1362 *
1363 * The return value is the number of characters written into @buf not including
Martin Peschkeea6f3282007-02-12 00:51:56 -08001364 * the trailing '\0'. If @size is <= 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 */
1366
1367int scnprintf(char * buf, size_t size, const char *fmt, ...)
1368{
1369 va_list args;
1370 int i;
1371
1372 va_start(args, fmt);
1373 i = vsnprintf(buf, size, fmt, args);
1374 va_end(args);
1375 return (i >= size) ? (size - 1) : i;
1376}
1377EXPORT_SYMBOL(scnprintf);
1378
1379/**
1380 * vsprintf - Format a string and place it in a buffer
1381 * @buf: The buffer to place the result into
1382 * @fmt: The format string to use
1383 * @args: Arguments for the format string
1384 *
1385 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001386 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 * buffer overflows.
1388 *
1389 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001390 * You probably want sprintf() instead.
Andi Kleen20036fd2008-10-15 22:02:02 -07001391 *
1392 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 */
1394int vsprintf(char *buf, const char *fmt, va_list args)
1395{
1396 return vsnprintf(buf, INT_MAX, fmt, args);
1397}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398EXPORT_SYMBOL(vsprintf);
1399
1400/**
1401 * sprintf - Format a string and place it in a buffer
1402 * @buf: The buffer to place the result into
1403 * @fmt: The format string to use
1404 * @...: Arguments for the format string
1405 *
1406 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001407 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07001409 *
1410 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 */
1412int sprintf(char * buf, const char *fmt, ...)
1413{
1414 va_list args;
1415 int i;
1416
1417 va_start(args, fmt);
1418 i=vsnprintf(buf, INT_MAX, fmt, args);
1419 va_end(args);
1420 return i;
1421}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422EXPORT_SYMBOL(sprintf);
1423
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001424#ifdef CONFIG_BINARY_PRINTF
1425/*
1426 * bprintf service:
1427 * vbin_printf() - VA arguments to binary data
1428 * bstr_printf() - Binary data to text string
1429 */
1430
1431/**
1432 * vbin_printf - Parse a format string and place args' binary value in a buffer
1433 * @bin_buf: The buffer to place args' binary value
1434 * @size: The size of the buffer(by words(32bits), not characters)
1435 * @fmt: The format string to use
1436 * @args: Arguments for the format string
1437 *
1438 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1439 * is skiped.
1440 *
1441 * The return value is the number of words(32bits) which would be generated for
1442 * the given input.
1443 *
1444 * NOTE:
1445 * If the return value is greater than @size, the resulting bin_buf is NOT
1446 * valid for bstr_printf().
1447 */
1448int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1449{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001450 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001451 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001452 int read;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001453
1454 str = (char *)bin_buf;
1455 end = (char *)(bin_buf + size);
1456
1457#define save_arg(type) \
1458do { \
1459 if (sizeof(type) == 8) { \
1460 unsigned long long value; \
1461 str = PTR_ALIGN(str, sizeof(u32)); \
1462 value = va_arg(args, unsigned long long); \
1463 if (str + sizeof(type) <= end) { \
1464 *(u32 *)str = *(u32 *)&value; \
1465 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1466 } \
1467 } else { \
1468 unsigned long value; \
1469 str = PTR_ALIGN(str, sizeof(type)); \
1470 value = va_arg(args, int); \
1471 if (str + sizeof(type) <= end) \
1472 *(typeof(type) *)str = (type)value; \
1473 } \
1474 str += sizeof(type); \
1475} while (0)
1476
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001477
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001478 while (*fmt) {
1479 read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001480
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001481 fmt += read;
1482
1483 switch (spec.type) {
1484 case FORMAT_TYPE_NONE:
1485 break;
1486
Vegard Nossumed681a92009-03-14 12:08:50 +01001487 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001488 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001489 save_arg(int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001490 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001491
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001492 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001493 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001494 break;
1495
1496 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001497 const char *save_str = va_arg(args, char *);
1498 size_t len;
1499 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1500 || (unsigned long)save_str < PAGE_SIZE)
1501 save_str = "<NULL>";
1502 len = strlen(save_str);
1503 if (str + len + 1 < end)
1504 memcpy(str, save_str, len + 1);
1505 str += len + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001506 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001507 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001508
1509 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001510 save_arg(void *);
1511 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001512 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001513 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001514 break;
1515
1516 case FORMAT_TYPE_PERCENT_CHAR:
1517 break;
1518
1519 case FORMAT_TYPE_INVALID:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001520 break;
1521
1522 case FORMAT_TYPE_NRCHARS: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001523 /* skip %n 's argument */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001524 int qualifier = spec.qualifier;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001525 void *skip_arg;
1526 if (qualifier == 'l')
1527 skip_arg = va_arg(args, long *);
1528 else if (qualifier == 'Z' || qualifier == 'z')
1529 skip_arg = va_arg(args, size_t *);
1530 else
1531 skip_arg = va_arg(args, int *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001532 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001533 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001534
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001535 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001536 switch (spec.type) {
1537
1538 case FORMAT_TYPE_LONG_LONG:
1539 save_arg(long long);
1540 break;
1541 case FORMAT_TYPE_ULONG:
1542 case FORMAT_TYPE_LONG:
1543 save_arg(unsigned long);
1544 break;
1545 case FORMAT_TYPE_SIZE_T:
1546 save_arg(size_t);
1547 break;
1548 case FORMAT_TYPE_PTRDIFF:
1549 save_arg(ptrdiff_t);
1550 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001551 case FORMAT_TYPE_UBYTE:
1552 case FORMAT_TYPE_BYTE:
1553 save_arg(char);
1554 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001555 case FORMAT_TYPE_USHORT:
1556 case FORMAT_TYPE_SHORT:
1557 save_arg(short);
1558 break;
1559 default:
1560 save_arg(int);
1561 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001562 }
1563 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001564 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001565
1566#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001567}
1568EXPORT_SYMBOL_GPL(vbin_printf);
1569
1570/**
1571 * bstr_printf - Format a string from binary arguments and place it in a buffer
1572 * @buf: The buffer to place the result into
1573 * @size: The size of the buffer, including the trailing null space
1574 * @fmt: The format string to use
1575 * @bin_buf: Binary arguments for the format string
1576 *
1577 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1578 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1579 * a binary buffer that generated by vbin_printf.
1580 *
1581 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001582 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001583 *
1584 * The return value is the number of characters which would
1585 * be generated for the given input, excluding the trailing
1586 * '\0', as per ISO C99. If you want to have the exact
1587 * number of characters written into @buf as return value
1588 * (not including the trailing '\0'), use vscnprintf(). If the
1589 * return is greater than or equal to @size, the resulting
1590 * string is truncated.
1591 */
1592int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1593{
1594 unsigned long long num;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001595 char *str, *end, c;
1596 const char *args = (const char *)bin_buf;
1597
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001598 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001599
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001600 if (WARN_ON_ONCE((int) size < 0))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001601 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001602
1603 str = buf;
1604 end = buf + size;
1605
1606#define get_arg(type) \
1607({ \
1608 typeof(type) value; \
1609 if (sizeof(type) == 8) { \
1610 args = PTR_ALIGN(args, sizeof(u32)); \
1611 *(u32 *)&value = *(u32 *)args; \
1612 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1613 } else { \
1614 args = PTR_ALIGN(args, sizeof(type)); \
1615 value = *(typeof(type) *)args; \
1616 } \
1617 args += sizeof(type); \
1618 value; \
1619})
1620
1621 /* Make sure end is always >= buf */
1622 if (end < buf) {
1623 end = ((void *)-1);
1624 size = end - buf;
1625 }
1626
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001627 while (*fmt) {
1628 int read;
1629 const char *old_fmt = fmt;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001630
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001631 read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001632
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001633 fmt += read;
1634
1635 switch (spec.type) {
1636 case FORMAT_TYPE_NONE: {
1637 int copy = read;
1638 if (str < end) {
1639 if (copy > end - str)
1640 copy = end - str;
1641 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001642 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001643 str += read;
1644 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001645 }
1646
Vegard Nossumed681a92009-03-14 12:08:50 +01001647 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001648 spec.field_width = get_arg(int);
1649 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001650
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001651 case FORMAT_TYPE_PRECISION:
1652 spec.precision = get_arg(int);
1653 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001654
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001655 case FORMAT_TYPE_CHAR:
1656 if (!(spec.flags & LEFT)) {
1657 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001658 if (str < end)
1659 *str = ' ';
1660 ++str;
1661 }
1662 }
1663 c = (unsigned char) get_arg(char);
1664 if (str < end)
1665 *str = c;
1666 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001667 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001668 if (str < end)
1669 *str = ' ';
1670 ++str;
1671 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001672 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001673
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001674 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001675 const char *str_arg = args;
1676 size_t len = strlen(str_arg);
1677 args += len + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001678 str = string(str, end, (char *)str_arg, spec);
1679 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001680 }
1681
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001682 case FORMAT_TYPE_PTR:
1683 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1684 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001685 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001686 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001687
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001688 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001689 if (str < end)
1690 *str = '%';
1691 ++str;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001692 break;
1693
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001694 case FORMAT_TYPE_INVALID:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001695 if (str < end)
1696 *str = '%';
1697 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001698 break;
1699
1700 case FORMAT_TYPE_NRCHARS:
1701 /* skip */
1702 break;
1703
1704 default:
1705 switch (spec.type) {
1706
1707 case FORMAT_TYPE_LONG_LONG:
1708 num = get_arg(long long);
1709 break;
1710 case FORMAT_TYPE_ULONG:
1711 num = get_arg(unsigned long);
1712 break;
1713 case FORMAT_TYPE_LONG:
1714 num = get_arg(unsigned long);
1715 break;
1716 case FORMAT_TYPE_SIZE_T:
1717 num = get_arg(size_t);
1718 break;
1719 case FORMAT_TYPE_PTRDIFF:
1720 num = get_arg(ptrdiff_t);
1721 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001722 case FORMAT_TYPE_UBYTE:
1723 num = get_arg(unsigned char);
1724 break;
1725 case FORMAT_TYPE_BYTE:
1726 num = get_arg(signed char);
1727 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001728 case FORMAT_TYPE_USHORT:
1729 num = get_arg(unsigned short);
1730 break;
1731 case FORMAT_TYPE_SHORT:
1732 num = get_arg(short);
1733 break;
1734 case FORMAT_TYPE_UINT:
1735 num = get_arg(unsigned int);
1736 break;
1737 default:
1738 num = get_arg(int);
1739 }
1740
1741 str = number(str, end, num, spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001742 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001743 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001744
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001745 if (size > 0) {
1746 if (str < end)
1747 *str = '\0';
1748 else
1749 end[-1] = '\0';
1750 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001751
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001752#undef get_arg
1753
1754 /* the trailing null byte doesn't count towards the total */
1755 return str - buf;
1756}
1757EXPORT_SYMBOL_GPL(bstr_printf);
1758
1759/**
1760 * bprintf - Parse a format string and place args' binary value in a buffer
1761 * @bin_buf: The buffer to place args' binary value
1762 * @size: The size of the buffer(by words(32bits), not characters)
1763 * @fmt: The format string to use
1764 * @...: Arguments for the format string
1765 *
1766 * The function returns the number of words(u32) written
1767 * into @bin_buf.
1768 */
1769int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1770{
1771 va_list args;
1772 int ret;
1773
1774 va_start(args, fmt);
1775 ret = vbin_printf(bin_buf, size, fmt, args);
1776 va_end(args);
1777 return ret;
1778}
1779EXPORT_SYMBOL_GPL(bprintf);
1780
1781#endif /* CONFIG_BINARY_PRINTF */
1782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783/**
1784 * vsscanf - Unformat a buffer into a list of arguments
1785 * @buf: input buffer
1786 * @fmt: format of buffer
1787 * @args: arguments
1788 */
1789int vsscanf(const char * buf, const char * fmt, va_list args)
1790{
1791 const char *str = buf;
1792 char *next;
1793 char digit;
1794 int num = 0;
1795 int qualifier;
1796 int base;
1797 int field_width;
1798 int is_sign = 0;
1799
1800 while(*fmt && *str) {
1801 /* skip any white space in format */
1802 /* white space in format matchs any amount of
1803 * white space, including none, in the input.
1804 */
1805 if (isspace(*fmt)) {
1806 while (isspace(*fmt))
1807 ++fmt;
1808 while (isspace(*str))
1809 ++str;
1810 }
1811
1812 /* anything that is not a conversion must match exactly */
1813 if (*fmt != '%' && *fmt) {
1814 if (*fmt++ != *str++)
1815 break;
1816 continue;
1817 }
1818
1819 if (!*fmt)
1820 break;
1821 ++fmt;
1822
1823 /* skip this conversion.
1824 * advance both strings to next white space
1825 */
1826 if (*fmt == '*') {
Andy Spencer8fccae22009-10-01 15:44:27 -07001827 while (!isspace(*fmt) && *fmt != '%' && *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 fmt++;
1829 while (!isspace(*str) && *str)
1830 str++;
1831 continue;
1832 }
1833
1834 /* get field width */
1835 field_width = -1;
1836 if (isdigit(*fmt))
1837 field_width = skip_atoi(&fmt);
1838
1839 /* get conversion qualifier */
1840 qualifier = -1;
1841 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1842 *fmt == 'Z' || *fmt == 'z') {
1843 qualifier = *fmt++;
1844 if (unlikely(qualifier == *fmt)) {
1845 if (qualifier == 'h') {
1846 qualifier = 'H';
1847 fmt++;
1848 } else if (qualifier == 'l') {
1849 qualifier = 'L';
1850 fmt++;
1851 }
1852 }
1853 }
1854 base = 10;
1855 is_sign = 0;
1856
1857 if (!*fmt || !*str)
1858 break;
1859
1860 switch(*fmt++) {
1861 case 'c':
1862 {
1863 char *s = (char *) va_arg(args,char*);
1864 if (field_width == -1)
1865 field_width = 1;
1866 do {
1867 *s++ = *str++;
1868 } while (--field_width > 0 && *str);
1869 num++;
1870 }
1871 continue;
1872 case 's':
1873 {
1874 char *s = (char *) va_arg(args, char *);
1875 if(field_width == -1)
1876 field_width = INT_MAX;
1877 /* first, skip leading white space in buffer */
1878 while (isspace(*str))
1879 str++;
1880
1881 /* now copy until next white space */
1882 while (*str && !isspace(*str) && field_width--) {
1883 *s++ = *str++;
1884 }
1885 *s = '\0';
1886 num++;
1887 }
1888 continue;
1889 case 'n':
1890 /* return number of characters read so far */
1891 {
1892 int *i = (int *)va_arg(args,int*);
1893 *i = str - buf;
1894 }
1895 continue;
1896 case 'o':
1897 base = 8;
1898 break;
1899 case 'x':
1900 case 'X':
1901 base = 16;
1902 break;
1903 case 'i':
1904 base = 0;
1905 case 'd':
1906 is_sign = 1;
1907 case 'u':
1908 break;
1909 case '%':
1910 /* looking for '%' in str */
1911 if (*str++ != '%')
1912 return num;
1913 continue;
1914 default:
1915 /* invalid format; stop here */
1916 return num;
1917 }
1918
1919 /* have some sort of integer conversion.
1920 * first, skip white space in buffer.
1921 */
1922 while (isspace(*str))
1923 str++;
1924
1925 digit = *str;
1926 if (is_sign && digit == '-')
1927 digit = *(str + 1);
1928
1929 if (!digit
1930 || (base == 16 && !isxdigit(digit))
1931 || (base == 10 && !isdigit(digit))
1932 || (base == 8 && (!isdigit(digit) || digit > '7'))
1933 || (base == 0 && !isdigit(digit)))
1934 break;
1935
1936 switch(qualifier) {
1937 case 'H': /* that's 'hh' in format */
1938 if (is_sign) {
1939 signed char *s = (signed char *) va_arg(args,signed char *);
1940 *s = (signed char) simple_strtol(str,&next,base);
1941 } else {
1942 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1943 *s = (unsigned char) simple_strtoul(str, &next, base);
1944 }
1945 break;
1946 case 'h':
1947 if (is_sign) {
1948 short *s = (short *) va_arg(args,short *);
1949 *s = (short) simple_strtol(str,&next,base);
1950 } else {
1951 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1952 *s = (unsigned short) simple_strtoul(str, &next, base);
1953 }
1954 break;
1955 case 'l':
1956 if (is_sign) {
1957 long *l = (long *) va_arg(args,long *);
1958 *l = simple_strtol(str,&next,base);
1959 } else {
1960 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1961 *l = simple_strtoul(str,&next,base);
1962 }
1963 break;
1964 case 'L':
1965 if (is_sign) {
1966 long long *l = (long long*) va_arg(args,long long *);
1967 *l = simple_strtoll(str,&next,base);
1968 } else {
1969 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1970 *l = simple_strtoull(str,&next,base);
1971 }
1972 break;
1973 case 'Z':
1974 case 'z':
1975 {
1976 size_t *s = (size_t*) va_arg(args,size_t*);
1977 *s = (size_t) simple_strtoul(str,&next,base);
1978 }
1979 break;
1980 default:
1981 if (is_sign) {
1982 int *i = (int *) va_arg(args, int*);
1983 *i = (int) simple_strtol(str,&next,base);
1984 } else {
1985 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1986 *i = (unsigned int) simple_strtoul(str,&next,base);
1987 }
1988 break;
1989 }
1990 num++;
1991
1992 if (!next)
1993 break;
1994 str = next;
1995 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07001996
1997 /*
1998 * Now we've come all the way through so either the input string or the
1999 * format ended. In the former case, there can be a %n at the current
2000 * position in the format that needs to be filled.
2001 */
2002 if (*fmt == '%' && *(fmt + 1) == 'n') {
2003 int *p = (int *)va_arg(args, int *);
2004 *p = str - buf;
2005 }
2006
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 return num;
2008}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009EXPORT_SYMBOL(vsscanf);
2010
2011/**
2012 * sscanf - Unformat a buffer into a list of arguments
2013 * @buf: input buffer
2014 * @fmt: formatting of buffer
2015 * @...: resulting arguments
2016 */
2017int sscanf(const char * buf, const char * fmt, ...)
2018{
2019 va_list args;
2020 int i;
2021
2022 va_start(args,fmt);
2023 i = vsscanf(buf,fmt,args);
2024 va_end(args);
2025 return i;
2026}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027EXPORT_SYMBOL(sscanf);