blob: 73a14b8c6d1f860796ebe7982ff0a7ca1d6c2081 [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,
598 struct printf_spec spec)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100599{
600#ifndef IO_RSRC_PRINTK_SIZE
601#define IO_RSRC_PRINTK_SIZE 4
602#endif
603
604#ifndef MEM_RSRC_PRINTK_SIZE
605#define MEM_RSRC_PRINTK_SIZE 8
606#endif
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100607 struct printf_spec num_spec = {
608 .base = 16,
609 .precision = -1,
610 .flags = SPECIAL | SMALL | ZEROPAD,
611 };
Linus Torvalds332d2e72008-10-20 15:07:34 +1100612 /* room for the actual numbers, the two "0x", -, [, ] and the final zero */
613 char sym[4*sizeof(resource_size_t) + 8];
614 char *p = sym, *pend = sym + sizeof(sym);
615 int size = -1;
616
617 if (res->flags & IORESOURCE_IO)
618 size = IO_RSRC_PRINTK_SIZE;
619 else if (res->flags & IORESOURCE_MEM)
620 size = MEM_RSRC_PRINTK_SIZE;
621
622 *p++ = '[';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100623 num_spec.field_width = size;
624 p = number(p, pend, res->start, num_spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100625 *p++ = '-';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100626 p = number(p, pend, res->end, num_spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100627 *p++ = ']';
628 *p = 0;
629
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100630 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100631}
632
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100633static char *mac_address_string(char *buf, char *end, u8 *addr,
Joe Perches8a27f7c2009-08-17 12:29:44 +0000634 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700635{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000636 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700637 char *p = mac_addr;
638 int i;
639
640 for (i = 0; i < 6; i++) {
641 p = pack_hex_byte(p, addr[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000642 if (fmt[0] == 'M' && i != 5)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700643 *p++ = ':';
644 }
645 *p = '\0';
646
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100647 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700648}
649
Joe Perches8a27f7c2009-08-17 12:29:44 +0000650static char *ip4_string(char *p, const u8 *addr, bool leading_zeros)
Harvey Harrison689afa72008-10-28 16:04:44 -0700651{
Harvey Harrison689afa72008-10-28 16:04:44 -0700652 int i;
653
Joe Perches8a27f7c2009-08-17 12:29:44 +0000654 for (i = 0; i < 4; i++) {
655 char temp[3]; /* hold each IP quad in reverse order */
656 int digits = put_dec_trunc(temp, addr[i]) - temp;
657 if (leading_zeros) {
658 if (digits < 3)
659 *p++ = '0';
660 if (digits < 2)
661 *p++ = '0';
662 }
663 /* reverse the digits in the quad */
664 while (digits--)
665 *p++ = temp[digits];
666 if (i < 3)
667 *p++ = '.';
668 }
669
670 *p = '\0';
671 return p;
672}
673
674static char *ip6_compressed_string(char *p, const struct in6_addr *addr)
675{
676 int i;
677 int j;
678 int range;
679 unsigned char zerolength[8];
680 int longest = 1;
681 int colonpos = -1;
682 u16 word;
683 u8 hi;
684 u8 lo;
685 bool needcolon = false;
686 bool useIPv4 = ipv6_addr_v4mapped(addr) || ipv6_addr_is_isatap(addr);
687
688 memset(zerolength, 0, sizeof(zerolength));
689
690 if (useIPv4)
691 range = 6;
692 else
693 range = 8;
694
695 /* find position of longest 0 run */
696 for (i = 0; i < range; i++) {
697 for (j = i; j < range; j++) {
698 if (addr->s6_addr16[j] != 0)
699 break;
700 zerolength[i]++;
701 }
702 }
703 for (i = 0; i < range; i++) {
704 if (zerolength[i] > longest) {
705 longest = zerolength[i];
706 colonpos = i;
707 }
708 }
709
710 /* emit address */
711 for (i = 0; i < range; i++) {
712 if (i == colonpos) {
713 if (needcolon || i == 0)
714 *p++ = ':';
715 *p++ = ':';
716 needcolon = false;
717 i += longest - 1;
718 continue;
719 }
720 if (needcolon) {
721 *p++ = ':';
722 needcolon = false;
723 }
724 /* hex u16 without leading 0s */
725 word = ntohs(addr->s6_addr16[i]);
726 hi = word >> 8;
727 lo = word & 0xff;
728 if (hi) {
729 if (hi > 0x0f)
730 p = pack_hex_byte(p, hi);
731 else
732 *p++ = hex_asc_lo(hi);
733 }
734 if (hi || lo > 0x0f)
735 p = pack_hex_byte(p, lo);
736 else
737 *p++ = hex_asc_lo(lo);
738 needcolon = true;
739 }
740
741 if (useIPv4) {
742 if (needcolon)
743 *p++ = ':';
744 p = ip4_string(p, &addr->s6_addr[12], false);
745 }
746
747 *p = '\0';
748 return p;
749}
750
751static char *ip6_string(char *p, const struct in6_addr *addr, const char *fmt)
752{
753 int i;
Harvey Harrison689afa72008-10-28 16:04:44 -0700754 for (i = 0; i < 8; i++) {
Joe Perches8a27f7c2009-08-17 12:29:44 +0000755 p = pack_hex_byte(p, addr->s6_addr[2 * i]);
756 p = pack_hex_byte(p, addr->s6_addr[2 * i + 1]);
757 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -0700758 *p++ = ':';
759 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000760
Harvey Harrison689afa72008-10-28 16:04:44 -0700761 *p = '\0';
Joe Perches8a27f7c2009-08-17 12:29:44 +0000762 return p;
763}
764
765static char *ip6_addr_string(char *buf, char *end, const u8 *addr,
766 struct printf_spec spec, const char *fmt)
767{
768 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
769
770 if (fmt[0] == 'I' && fmt[2] == 'c')
771 ip6_compressed_string(ip6_addr, (const struct in6_addr *)addr);
772 else
773 ip6_string(ip6_addr, (const struct in6_addr *)addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -0700774
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100775 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -0700776}
777
Joe Perches8a27f7c2009-08-17 12:29:44 +0000778static char *ip4_addr_string(char *buf, char *end, const u8 *addr,
779 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -0700780{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000781 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -0700782
Joe Perches8a27f7c2009-08-17 12:29:44 +0000783 ip4_string(ip4_addr, addr, fmt[0] == 'i');
Harvey Harrison4aa99602008-10-29 12:49:58 -0700784
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100785 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700786}
787
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700788/*
789 * Show a '%p' thing. A kernel extension is that the '%p' is followed
790 * by an extra set of alphanumeric characters that are extended format
791 * specifiers.
792 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100793 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700794 *
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200795 * - 'F' For symbolic function descriptor pointers with offset
796 * - 'f' For simple symbolic function names without offset
Steven Rostedt0efb4d22009-09-17 09:27:29 -0400797 * - 'S' For symbolic direct pointers with offset
798 * - 's' For symbolic direct pointers without offset
Linus Torvalds332d2e72008-10-20 15:07:34 +1100799 * - 'R' For a struct resource pointer, it prints the range of
800 * addresses (not the name nor the flags)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700801 * - 'M' For a 6-byte MAC address, it prints the address in the
802 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +0000803 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
804 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
805 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
806 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
807 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
808 * IPv6 omits the colons (01020304...0f)
809 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
810 * - 'I6c' for IPv6 addresses printed as specified by
811 * http://www.ietf.org/id/draft-kawamura-ipv6-text-representation-03.txt
Linus Torvalds332d2e72008-10-20 15:07:34 +1100812 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
813 * function pointers are really function descriptors, which contain a
814 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700815 */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100816static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
817 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700818{
Linus Torvaldsd97106a2009-01-03 11:46:17 -0800819 if (!ptr)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100820 return string(buf, end, "(null)", spec);
Linus Torvaldsd97106a2009-01-03 11:46:17 -0800821
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700822 switch (*fmt) {
823 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200824 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700825 ptr = dereference_function_descriptor(ptr);
Steven Rostedt91adcd22009-09-16 20:03:06 -0400826 case 's':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700827 /* Fallthrough */
828 case 'S':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200829 return symbol_string(buf, end, ptr, spec, *fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100830 case 'R':
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100831 return resource_string(buf, end, ptr, spec);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000832 case 'M': /* Colon separated: 00:01:02:03:04:05 */
833 case 'm': /* Contiguous: 000102030405 */
834 return mac_address_string(buf, end, ptr, spec, fmt);
835 case 'I': /* Formatted IP supported
836 * 4: 1.2.3.4
837 * 6: 0001:0203:...:0708
838 * 6c: 1::708 or 1::1.2.3.4
839 */
840 case 'i': /* Contiguous:
841 * 4: 001.002.003.004
842 * 6: 000102...0f
843 */
844 switch (fmt[1]) {
845 case '6':
846 return ip6_addr_string(buf, end, ptr, spec, fmt);
847 case '4':
848 return ip4_addr_string(buf, end, ptr, spec, fmt);
849 }
Harvey Harrison4aa99602008-10-29 12:49:58 -0700850 break;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700851 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100852 spec.flags |= SMALL;
853 if (spec.field_width == -1) {
854 spec.field_width = 2*sizeof(void *);
855 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700856 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100857 spec.base = 16;
858
859 return number(buf, end, (unsigned long) ptr, spec);
860}
861
862/*
863 * Helper function to decode printf style format.
864 * Each call decode a token from the format and return the
865 * number of characters read (or likely the delta where it wants
866 * to go on the next call).
867 * The decoded token is returned through the parameters
868 *
869 * 'h', 'l', or 'L' for integer fields
870 * 'z' support added 23/7/1999 S.H.
871 * 'z' changed to 'Z' --davidm 1/25/99
872 * 't' added for ptrdiff_t
873 *
874 * @fmt: the format string
875 * @type of the token returned
876 * @flags: various flags such as +, -, # tokens..
877 * @field_width: overwritten width
878 * @base: base of the number (octal, hex, ...)
879 * @precision: precision of a number
880 * @qualifier: qualifier of a number (long, size_t, ...)
881 */
882static int format_decode(const char *fmt, struct printf_spec *spec)
883{
884 const char *start = fmt;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100885
886 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +0100887 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100888 if (spec->field_width < 0) {
889 spec->field_width = -spec->field_width;
890 spec->flags |= LEFT;
891 }
892 spec->type = FORMAT_TYPE_NONE;
893 goto precision;
894 }
895
896 /* we finished early by reading the precision */
897 if (spec->type == FORMAT_TYPE_PRECISION) {
898 if (spec->precision < 0)
899 spec->precision = 0;
900
901 spec->type = FORMAT_TYPE_NONE;
902 goto qualifier;
903 }
904
905 /* By default */
906 spec->type = FORMAT_TYPE_NONE;
907
908 for (; *fmt ; ++fmt) {
909 if (*fmt == '%')
910 break;
911 }
912
913 /* Return the current non-format string */
914 if (fmt != start || !*fmt)
915 return fmt - start;
916
917 /* Process flags */
918 spec->flags = 0;
919
920 while (1) { /* this also skips first '%' */
921 bool found = true;
922
923 ++fmt;
924
925 switch (*fmt) {
926 case '-': spec->flags |= LEFT; break;
927 case '+': spec->flags |= PLUS; break;
928 case ' ': spec->flags |= SPACE; break;
929 case '#': spec->flags |= SPECIAL; break;
930 case '0': spec->flags |= ZEROPAD; break;
931 default: found = false;
932 }
933
934 if (!found)
935 break;
936 }
937
938 /* get field width */
939 spec->field_width = -1;
940
941 if (isdigit(*fmt))
942 spec->field_width = skip_atoi(&fmt);
943 else if (*fmt == '*') {
944 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +0100945 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100946 return ++fmt - start;
947 }
948
949precision:
950 /* get the precision */
951 spec->precision = -1;
952 if (*fmt == '.') {
953 ++fmt;
954 if (isdigit(*fmt)) {
955 spec->precision = skip_atoi(&fmt);
956 if (spec->precision < 0)
957 spec->precision = 0;
958 } else if (*fmt == '*') {
959 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +0100960 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100961 return ++fmt - start;
962 }
963 }
964
965qualifier:
966 /* get the conversion qualifier */
967 spec->qualifier = -1;
968 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
969 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
Zhaoleia4e94ef2009-03-27 17:07:05 +0800970 spec->qualifier = *fmt++;
971 if (unlikely(spec->qualifier == *fmt)) {
972 if (spec->qualifier == 'l') {
973 spec->qualifier = 'L';
974 ++fmt;
975 } else if (spec->qualifier == 'h') {
976 spec->qualifier = 'H';
977 ++fmt;
978 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100979 }
980 }
981
982 /* default base */
983 spec->base = 10;
984 switch (*fmt) {
985 case 'c':
986 spec->type = FORMAT_TYPE_CHAR;
987 return ++fmt - start;
988
989 case 's':
990 spec->type = FORMAT_TYPE_STR;
991 return ++fmt - start;
992
993 case 'p':
994 spec->type = FORMAT_TYPE_PTR;
995 return fmt - start;
996 /* skip alnum */
997
998 case 'n':
999 spec->type = FORMAT_TYPE_NRCHARS;
1000 return ++fmt - start;
1001
1002 case '%':
1003 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1004 return ++fmt - start;
1005
1006 /* integer number formats - set up the flags and "break" */
1007 case 'o':
1008 spec->base = 8;
1009 break;
1010
1011 case 'x':
1012 spec->flags |= SMALL;
1013
1014 case 'X':
1015 spec->base = 16;
1016 break;
1017
1018 case 'd':
1019 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001020 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001021 case 'u':
1022 break;
1023
1024 default:
1025 spec->type = FORMAT_TYPE_INVALID;
1026 return fmt - start;
1027 }
1028
1029 if (spec->qualifier == 'L')
1030 spec->type = FORMAT_TYPE_LONG_LONG;
1031 else if (spec->qualifier == 'l') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001032 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001033 spec->type = FORMAT_TYPE_LONG;
1034 else
1035 spec->type = FORMAT_TYPE_ULONG;
1036 } else if (spec->qualifier == 'Z' || spec->qualifier == 'z') {
1037 spec->type = FORMAT_TYPE_SIZE_T;
1038 } else if (spec->qualifier == 't') {
1039 spec->type = FORMAT_TYPE_PTRDIFF;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001040 } else if (spec->qualifier == 'H') {
1041 if (spec->flags & SIGN)
1042 spec->type = FORMAT_TYPE_BYTE;
1043 else
1044 spec->type = FORMAT_TYPE_UBYTE;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001045 } else if (spec->qualifier == 'h') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001046 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001047 spec->type = FORMAT_TYPE_SHORT;
1048 else
1049 spec->type = FORMAT_TYPE_USHORT;
1050 } else {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001051 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001052 spec->type = FORMAT_TYPE_INT;
1053 else
1054 spec->type = FORMAT_TYPE_UINT;
1055 }
1056
1057 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001058}
1059
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060/**
1061 * vsnprintf - Format a string and place it in a buffer
1062 * @buf: The buffer to place the result into
1063 * @size: The size of the buffer, including the trailing null space
1064 * @fmt: The format string to use
1065 * @args: Arguments for the format string
1066 *
Andi Kleen20036fd2008-10-15 22:02:02 -07001067 * This function follows C99 vsnprintf, but has some extensions:
Steven Rostedt91adcd22009-09-16 20:03:06 -04001068 * %pS output the name of a text symbol with offset
1069 * %ps output the name of a text symbol without offset
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001070 * %pF output the name of a function pointer with its offset
1071 * %pf output the name of a function pointer without its offset
Linus Torvalds332d2e72008-10-20 15:07:34 +11001072 * %pR output the address range in a struct resource
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001073 * %n is ignored
Andi Kleen20036fd2008-10-15 22:02:02 -07001074 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 * The return value is the number of characters which would
1076 * be generated for the given input, excluding the trailing
1077 * '\0', as per ISO C99. If you want to have the exact
1078 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001079 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 * return is greater than or equal to @size, the resulting
1081 * string is truncated.
1082 *
1083 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001084 * You probably want snprintf() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 */
1086int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1087{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 unsigned long long num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 char *str, *end, c;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001090 int read;
1091 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001093 /* Reject out-of-range values early. Large positive sizes are
1094 used for unknown buffer sizes. */
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001095 if (WARN_ON_ONCE((int) size < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001099 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001101 /* Make sure end is always >= buf */
1102 if (end < buf) {
1103 end = ((void *)-1);
1104 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 }
1106
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001107 while (*fmt) {
1108 const char *old_fmt = fmt;
1109
1110 read = format_decode(fmt, &spec);
1111
1112 fmt += read;
1113
1114 switch (spec.type) {
1115 case FORMAT_TYPE_NONE: {
1116 int copy = read;
1117 if (str < end) {
1118 if (copy > end - str)
1119 copy = end - str;
1120 memcpy(str, old_fmt, copy);
1121 }
1122 str += read;
1123 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 }
1125
Vegard Nossumed681a92009-03-14 12:08:50 +01001126 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001127 spec.field_width = va_arg(args, int);
1128 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001130 case FORMAT_TYPE_PRECISION:
1131 spec.precision = va_arg(args, int);
1132 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001134 case FORMAT_TYPE_CHAR:
1135 if (!(spec.flags & LEFT)) {
1136 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001137 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 *str = ' ';
1139 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001142 }
1143 c = (unsigned char) va_arg(args, int);
1144 if (str < end)
1145 *str = c;
1146 ++str;
1147 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001148 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001149 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001151 }
1152 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001154 case FORMAT_TYPE_STR:
1155 str = string(str, end, va_arg(args, char *), spec);
1156 break;
1157
1158 case FORMAT_TYPE_PTR:
1159 str = pointer(fmt+1, str, end, va_arg(args, void *),
1160 spec);
1161 while (isalnum(*fmt))
1162 fmt++;
1163 break;
1164
1165 case FORMAT_TYPE_PERCENT_CHAR:
1166 if (str < end)
1167 *str = '%';
1168 ++str;
1169 break;
1170
1171 case FORMAT_TYPE_INVALID:
1172 if (str < end)
1173 *str = '%';
1174 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001175 break;
1176
1177 case FORMAT_TYPE_NRCHARS: {
1178 int qualifier = spec.qualifier;
1179
1180 if (qualifier == 'l') {
1181 long *ip = va_arg(args, long *);
1182 *ip = (str - buf);
1183 } else if (qualifier == 'Z' ||
1184 qualifier == 'z') {
1185 size_t *ip = va_arg(args, size_t *);
1186 *ip = (str - buf);
1187 } else {
1188 int *ip = va_arg(args, int *);
1189 *ip = (str - buf);
1190 }
1191 break;
1192 }
1193
1194 default:
1195 switch (spec.type) {
1196 case FORMAT_TYPE_LONG_LONG:
1197 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001199 case FORMAT_TYPE_ULONG:
1200 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001202 case FORMAT_TYPE_LONG:
1203 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001205 case FORMAT_TYPE_SIZE_T:
1206 num = va_arg(args, size_t);
1207 break;
1208 case FORMAT_TYPE_PTRDIFF:
1209 num = va_arg(args, ptrdiff_t);
1210 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001211 case FORMAT_TYPE_UBYTE:
1212 num = (unsigned char) va_arg(args, int);
1213 break;
1214 case FORMAT_TYPE_BYTE:
1215 num = (signed char) va_arg(args, int);
1216 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001217 case FORMAT_TYPE_USHORT:
1218 num = (unsigned short) va_arg(args, int);
1219 break;
1220 case FORMAT_TYPE_SHORT:
1221 num = (short) va_arg(args, int);
1222 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001223 case FORMAT_TYPE_INT:
1224 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001225 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001227 num = va_arg(args, unsigned int);
1228 }
1229
1230 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001233
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001234 if (size > 0) {
1235 if (str < end)
1236 *str = '\0';
1237 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07001238 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001239 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001240
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001241 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001243
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245EXPORT_SYMBOL(vsnprintf);
1246
1247/**
1248 * vscnprintf - Format a string and place it in a buffer
1249 * @buf: The buffer to place the result into
1250 * @size: The size of the buffer, including the trailing null space
1251 * @fmt: The format string to use
1252 * @args: Arguments for the format string
1253 *
1254 * The return value is the number of characters which have been written into
1255 * the @buf not including the trailing '\0'. If @size is <= 0 the function
1256 * returns 0.
1257 *
1258 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001259 * You probably want scnprintf() instead.
Andi Kleen20036fd2008-10-15 22:02:02 -07001260 *
1261 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 */
1263int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1264{
1265 int i;
1266
1267 i=vsnprintf(buf,size,fmt,args);
1268 return (i >= size) ? (size - 1) : i;
1269}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270EXPORT_SYMBOL(vscnprintf);
1271
1272/**
1273 * snprintf - Format a string and place it in a buffer
1274 * @buf: The buffer to place the result into
1275 * @size: The size of the buffer, including the trailing null space
1276 * @fmt: The format string to use
1277 * @...: Arguments for the format string
1278 *
1279 * The return value is the number of characters which would be
1280 * generated for the given input, excluding the trailing null,
1281 * as per ISO C99. If the return is greater than or equal to
1282 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07001283 *
1284 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 */
1286int snprintf(char * buf, size_t size, const char *fmt, ...)
1287{
1288 va_list args;
1289 int i;
1290
1291 va_start(args, fmt);
1292 i=vsnprintf(buf,size,fmt,args);
1293 va_end(args);
1294 return i;
1295}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296EXPORT_SYMBOL(snprintf);
1297
1298/**
1299 * scnprintf - Format a string and place it in a buffer
1300 * @buf: The buffer to place the result into
1301 * @size: The size of the buffer, including the trailing null space
1302 * @fmt: The format string to use
1303 * @...: Arguments for the format string
1304 *
1305 * The return value is the number of characters written into @buf not including
Martin Peschkeea6f3282007-02-12 00:51:56 -08001306 * the trailing '\0'. If @size is <= 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 */
1308
1309int scnprintf(char * buf, size_t size, const char *fmt, ...)
1310{
1311 va_list args;
1312 int i;
1313
1314 va_start(args, fmt);
1315 i = vsnprintf(buf, size, fmt, args);
1316 va_end(args);
1317 return (i >= size) ? (size - 1) : i;
1318}
1319EXPORT_SYMBOL(scnprintf);
1320
1321/**
1322 * vsprintf - Format a string and place it in a buffer
1323 * @buf: The buffer to place the result into
1324 * @fmt: The format string to use
1325 * @args: Arguments for the format string
1326 *
1327 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001328 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 * buffer overflows.
1330 *
1331 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001332 * You probably want sprintf() instead.
Andi Kleen20036fd2008-10-15 22:02:02 -07001333 *
1334 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 */
1336int vsprintf(char *buf, const char *fmt, va_list args)
1337{
1338 return vsnprintf(buf, INT_MAX, fmt, args);
1339}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340EXPORT_SYMBOL(vsprintf);
1341
1342/**
1343 * sprintf - Format a string and place it in a buffer
1344 * @buf: The buffer to place the result into
1345 * @fmt: The format string to use
1346 * @...: Arguments for the format string
1347 *
1348 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001349 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07001351 *
1352 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 */
1354int sprintf(char * buf, const char *fmt, ...)
1355{
1356 va_list args;
1357 int i;
1358
1359 va_start(args, fmt);
1360 i=vsnprintf(buf, INT_MAX, fmt, args);
1361 va_end(args);
1362 return i;
1363}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364EXPORT_SYMBOL(sprintf);
1365
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001366#ifdef CONFIG_BINARY_PRINTF
1367/*
1368 * bprintf service:
1369 * vbin_printf() - VA arguments to binary data
1370 * bstr_printf() - Binary data to text string
1371 */
1372
1373/**
1374 * vbin_printf - Parse a format string and place args' binary value in a buffer
1375 * @bin_buf: The buffer to place args' binary value
1376 * @size: The size of the buffer(by words(32bits), not characters)
1377 * @fmt: The format string to use
1378 * @args: Arguments for the format string
1379 *
1380 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1381 * is skiped.
1382 *
1383 * The return value is the number of words(32bits) which would be generated for
1384 * the given input.
1385 *
1386 * NOTE:
1387 * If the return value is greater than @size, the resulting bin_buf is NOT
1388 * valid for bstr_printf().
1389 */
1390int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1391{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001392 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001393 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001394 int read;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001395
1396 str = (char *)bin_buf;
1397 end = (char *)(bin_buf + size);
1398
1399#define save_arg(type) \
1400do { \
1401 if (sizeof(type) == 8) { \
1402 unsigned long long value; \
1403 str = PTR_ALIGN(str, sizeof(u32)); \
1404 value = va_arg(args, unsigned long long); \
1405 if (str + sizeof(type) <= end) { \
1406 *(u32 *)str = *(u32 *)&value; \
1407 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1408 } \
1409 } else { \
1410 unsigned long value; \
1411 str = PTR_ALIGN(str, sizeof(type)); \
1412 value = va_arg(args, int); \
1413 if (str + sizeof(type) <= end) \
1414 *(typeof(type) *)str = (type)value; \
1415 } \
1416 str += sizeof(type); \
1417} while (0)
1418
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001419
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001420 while (*fmt) {
1421 read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001422
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001423 fmt += read;
1424
1425 switch (spec.type) {
1426 case FORMAT_TYPE_NONE:
1427 break;
1428
Vegard Nossumed681a92009-03-14 12:08:50 +01001429 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001430 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001431 save_arg(int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001432 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001433
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001434 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001435 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001436 break;
1437
1438 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001439 const char *save_str = va_arg(args, char *);
1440 size_t len;
1441 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1442 || (unsigned long)save_str < PAGE_SIZE)
1443 save_str = "<NULL>";
1444 len = strlen(save_str);
1445 if (str + len + 1 < end)
1446 memcpy(str, save_str, len + 1);
1447 str += len + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001448 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001449 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001450
1451 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001452 save_arg(void *);
1453 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001454 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001455 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001456 break;
1457
1458 case FORMAT_TYPE_PERCENT_CHAR:
1459 break;
1460
1461 case FORMAT_TYPE_INVALID:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001462 break;
1463
1464 case FORMAT_TYPE_NRCHARS: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001465 /* skip %n 's argument */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001466 int qualifier = spec.qualifier;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001467 void *skip_arg;
1468 if (qualifier == 'l')
1469 skip_arg = va_arg(args, long *);
1470 else if (qualifier == 'Z' || qualifier == 'z')
1471 skip_arg = va_arg(args, size_t *);
1472 else
1473 skip_arg = va_arg(args, int *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001474 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001475 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001476
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001477 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001478 switch (spec.type) {
1479
1480 case FORMAT_TYPE_LONG_LONG:
1481 save_arg(long long);
1482 break;
1483 case FORMAT_TYPE_ULONG:
1484 case FORMAT_TYPE_LONG:
1485 save_arg(unsigned long);
1486 break;
1487 case FORMAT_TYPE_SIZE_T:
1488 save_arg(size_t);
1489 break;
1490 case FORMAT_TYPE_PTRDIFF:
1491 save_arg(ptrdiff_t);
1492 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001493 case FORMAT_TYPE_UBYTE:
1494 case FORMAT_TYPE_BYTE:
1495 save_arg(char);
1496 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001497 case FORMAT_TYPE_USHORT:
1498 case FORMAT_TYPE_SHORT:
1499 save_arg(short);
1500 break;
1501 default:
1502 save_arg(int);
1503 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001504 }
1505 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001506 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001507
1508#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001509}
1510EXPORT_SYMBOL_GPL(vbin_printf);
1511
1512/**
1513 * bstr_printf - Format a string from binary arguments and place it in a buffer
1514 * @buf: The buffer to place the result into
1515 * @size: The size of the buffer, including the trailing null space
1516 * @fmt: The format string to use
1517 * @bin_buf: Binary arguments for the format string
1518 *
1519 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1520 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1521 * a binary buffer that generated by vbin_printf.
1522 *
1523 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001524 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001525 *
1526 * The return value is the number of characters which would
1527 * be generated for the given input, excluding the trailing
1528 * '\0', as per ISO C99. If you want to have the exact
1529 * number of characters written into @buf as return value
1530 * (not including the trailing '\0'), use vscnprintf(). If the
1531 * return is greater than or equal to @size, the resulting
1532 * string is truncated.
1533 */
1534int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1535{
1536 unsigned long long num;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001537 char *str, *end, c;
1538 const char *args = (const char *)bin_buf;
1539
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001540 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001541
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001542 if (WARN_ON_ONCE((int) size < 0))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001543 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001544
1545 str = buf;
1546 end = buf + size;
1547
1548#define get_arg(type) \
1549({ \
1550 typeof(type) value; \
1551 if (sizeof(type) == 8) { \
1552 args = PTR_ALIGN(args, sizeof(u32)); \
1553 *(u32 *)&value = *(u32 *)args; \
1554 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1555 } else { \
1556 args = PTR_ALIGN(args, sizeof(type)); \
1557 value = *(typeof(type) *)args; \
1558 } \
1559 args += sizeof(type); \
1560 value; \
1561})
1562
1563 /* Make sure end is always >= buf */
1564 if (end < buf) {
1565 end = ((void *)-1);
1566 size = end - buf;
1567 }
1568
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001569 while (*fmt) {
1570 int read;
1571 const char *old_fmt = fmt;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001572
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001573 read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001574
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001575 fmt += read;
1576
1577 switch (spec.type) {
1578 case FORMAT_TYPE_NONE: {
1579 int copy = read;
1580 if (str < end) {
1581 if (copy > end - str)
1582 copy = end - str;
1583 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001584 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001585 str += read;
1586 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001587 }
1588
Vegard Nossumed681a92009-03-14 12:08:50 +01001589 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001590 spec.field_width = get_arg(int);
1591 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001592
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001593 case FORMAT_TYPE_PRECISION:
1594 spec.precision = get_arg(int);
1595 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001596
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001597 case FORMAT_TYPE_CHAR:
1598 if (!(spec.flags & LEFT)) {
1599 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001600 if (str < end)
1601 *str = ' ';
1602 ++str;
1603 }
1604 }
1605 c = (unsigned char) get_arg(char);
1606 if (str < end)
1607 *str = c;
1608 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001609 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001610 if (str < end)
1611 *str = ' ';
1612 ++str;
1613 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001614 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001615
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001616 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001617 const char *str_arg = args;
1618 size_t len = strlen(str_arg);
1619 args += len + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001620 str = string(str, end, (char *)str_arg, spec);
1621 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001622 }
1623
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001624 case FORMAT_TYPE_PTR:
1625 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1626 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001627 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001628 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001629
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001630 case FORMAT_TYPE_PERCENT_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001631 if (str < end)
1632 *str = '%';
1633 ++str;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001634 break;
1635
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001636 case FORMAT_TYPE_INVALID:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001637 if (str < end)
1638 *str = '%';
1639 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001640 break;
1641
1642 case FORMAT_TYPE_NRCHARS:
1643 /* skip */
1644 break;
1645
1646 default:
1647 switch (spec.type) {
1648
1649 case FORMAT_TYPE_LONG_LONG:
1650 num = get_arg(long long);
1651 break;
1652 case FORMAT_TYPE_ULONG:
1653 num = get_arg(unsigned long);
1654 break;
1655 case FORMAT_TYPE_LONG:
1656 num = get_arg(unsigned long);
1657 break;
1658 case FORMAT_TYPE_SIZE_T:
1659 num = get_arg(size_t);
1660 break;
1661 case FORMAT_TYPE_PTRDIFF:
1662 num = get_arg(ptrdiff_t);
1663 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001664 case FORMAT_TYPE_UBYTE:
1665 num = get_arg(unsigned char);
1666 break;
1667 case FORMAT_TYPE_BYTE:
1668 num = get_arg(signed char);
1669 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001670 case FORMAT_TYPE_USHORT:
1671 num = get_arg(unsigned short);
1672 break;
1673 case FORMAT_TYPE_SHORT:
1674 num = get_arg(short);
1675 break;
1676 case FORMAT_TYPE_UINT:
1677 num = get_arg(unsigned int);
1678 break;
1679 default:
1680 num = get_arg(int);
1681 }
1682
1683 str = number(str, end, num, spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001684 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001685 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001686
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001687 if (size > 0) {
1688 if (str < end)
1689 *str = '\0';
1690 else
1691 end[-1] = '\0';
1692 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001693
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001694#undef get_arg
1695
1696 /* the trailing null byte doesn't count towards the total */
1697 return str - buf;
1698}
1699EXPORT_SYMBOL_GPL(bstr_printf);
1700
1701/**
1702 * bprintf - Parse a format string and place args' binary value in a buffer
1703 * @bin_buf: The buffer to place args' binary value
1704 * @size: The size of the buffer(by words(32bits), not characters)
1705 * @fmt: The format string to use
1706 * @...: Arguments for the format string
1707 *
1708 * The function returns the number of words(u32) written
1709 * into @bin_buf.
1710 */
1711int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1712{
1713 va_list args;
1714 int ret;
1715
1716 va_start(args, fmt);
1717 ret = vbin_printf(bin_buf, size, fmt, args);
1718 va_end(args);
1719 return ret;
1720}
1721EXPORT_SYMBOL_GPL(bprintf);
1722
1723#endif /* CONFIG_BINARY_PRINTF */
1724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725/**
1726 * vsscanf - Unformat a buffer into a list of arguments
1727 * @buf: input buffer
1728 * @fmt: format of buffer
1729 * @args: arguments
1730 */
1731int vsscanf(const char * buf, const char * fmt, va_list args)
1732{
1733 const char *str = buf;
1734 char *next;
1735 char digit;
1736 int num = 0;
1737 int qualifier;
1738 int base;
1739 int field_width;
1740 int is_sign = 0;
1741
1742 while(*fmt && *str) {
1743 /* skip any white space in format */
1744 /* white space in format matchs any amount of
1745 * white space, including none, in the input.
1746 */
1747 if (isspace(*fmt)) {
1748 while (isspace(*fmt))
1749 ++fmt;
1750 while (isspace(*str))
1751 ++str;
1752 }
1753
1754 /* anything that is not a conversion must match exactly */
1755 if (*fmt != '%' && *fmt) {
1756 if (*fmt++ != *str++)
1757 break;
1758 continue;
1759 }
1760
1761 if (!*fmt)
1762 break;
1763 ++fmt;
1764
1765 /* skip this conversion.
1766 * advance both strings to next white space
1767 */
1768 if (*fmt == '*') {
1769 while (!isspace(*fmt) && *fmt)
1770 fmt++;
1771 while (!isspace(*str) && *str)
1772 str++;
1773 continue;
1774 }
1775
1776 /* get field width */
1777 field_width = -1;
1778 if (isdigit(*fmt))
1779 field_width = skip_atoi(&fmt);
1780
1781 /* get conversion qualifier */
1782 qualifier = -1;
1783 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1784 *fmt == 'Z' || *fmt == 'z') {
1785 qualifier = *fmt++;
1786 if (unlikely(qualifier == *fmt)) {
1787 if (qualifier == 'h') {
1788 qualifier = 'H';
1789 fmt++;
1790 } else if (qualifier == 'l') {
1791 qualifier = 'L';
1792 fmt++;
1793 }
1794 }
1795 }
1796 base = 10;
1797 is_sign = 0;
1798
1799 if (!*fmt || !*str)
1800 break;
1801
1802 switch(*fmt++) {
1803 case 'c':
1804 {
1805 char *s = (char *) va_arg(args,char*);
1806 if (field_width == -1)
1807 field_width = 1;
1808 do {
1809 *s++ = *str++;
1810 } while (--field_width > 0 && *str);
1811 num++;
1812 }
1813 continue;
1814 case 's':
1815 {
1816 char *s = (char *) va_arg(args, char *);
1817 if(field_width == -1)
1818 field_width = INT_MAX;
1819 /* first, skip leading white space in buffer */
1820 while (isspace(*str))
1821 str++;
1822
1823 /* now copy until next white space */
1824 while (*str && !isspace(*str) && field_width--) {
1825 *s++ = *str++;
1826 }
1827 *s = '\0';
1828 num++;
1829 }
1830 continue;
1831 case 'n':
1832 /* return number of characters read so far */
1833 {
1834 int *i = (int *)va_arg(args,int*);
1835 *i = str - buf;
1836 }
1837 continue;
1838 case 'o':
1839 base = 8;
1840 break;
1841 case 'x':
1842 case 'X':
1843 base = 16;
1844 break;
1845 case 'i':
1846 base = 0;
1847 case 'd':
1848 is_sign = 1;
1849 case 'u':
1850 break;
1851 case '%':
1852 /* looking for '%' in str */
1853 if (*str++ != '%')
1854 return num;
1855 continue;
1856 default:
1857 /* invalid format; stop here */
1858 return num;
1859 }
1860
1861 /* have some sort of integer conversion.
1862 * first, skip white space in buffer.
1863 */
1864 while (isspace(*str))
1865 str++;
1866
1867 digit = *str;
1868 if (is_sign && digit == '-')
1869 digit = *(str + 1);
1870
1871 if (!digit
1872 || (base == 16 && !isxdigit(digit))
1873 || (base == 10 && !isdigit(digit))
1874 || (base == 8 && (!isdigit(digit) || digit > '7'))
1875 || (base == 0 && !isdigit(digit)))
1876 break;
1877
1878 switch(qualifier) {
1879 case 'H': /* that's 'hh' in format */
1880 if (is_sign) {
1881 signed char *s = (signed char *) va_arg(args,signed char *);
1882 *s = (signed char) simple_strtol(str,&next,base);
1883 } else {
1884 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1885 *s = (unsigned char) simple_strtoul(str, &next, base);
1886 }
1887 break;
1888 case 'h':
1889 if (is_sign) {
1890 short *s = (short *) va_arg(args,short *);
1891 *s = (short) simple_strtol(str,&next,base);
1892 } else {
1893 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1894 *s = (unsigned short) simple_strtoul(str, &next, base);
1895 }
1896 break;
1897 case 'l':
1898 if (is_sign) {
1899 long *l = (long *) va_arg(args,long *);
1900 *l = simple_strtol(str,&next,base);
1901 } else {
1902 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1903 *l = simple_strtoul(str,&next,base);
1904 }
1905 break;
1906 case 'L':
1907 if (is_sign) {
1908 long long *l = (long long*) va_arg(args,long long *);
1909 *l = simple_strtoll(str,&next,base);
1910 } else {
1911 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1912 *l = simple_strtoull(str,&next,base);
1913 }
1914 break;
1915 case 'Z':
1916 case 'z':
1917 {
1918 size_t *s = (size_t*) va_arg(args,size_t*);
1919 *s = (size_t) simple_strtoul(str,&next,base);
1920 }
1921 break;
1922 default:
1923 if (is_sign) {
1924 int *i = (int *) va_arg(args, int*);
1925 *i = (int) simple_strtol(str,&next,base);
1926 } else {
1927 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1928 *i = (unsigned int) simple_strtoul(str,&next,base);
1929 }
1930 break;
1931 }
1932 num++;
1933
1934 if (!next)
1935 break;
1936 str = next;
1937 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07001938
1939 /*
1940 * Now we've come all the way through so either the input string or the
1941 * format ended. In the former case, there can be a %n at the current
1942 * position in the format that needs to be filled.
1943 */
1944 if (*fmt == '%' && *(fmt + 1) == 'n') {
1945 int *p = (int *)va_arg(args, int *);
1946 *p = str - buf;
1947 }
1948
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 return num;
1950}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951EXPORT_SYMBOL(vsscanf);
1952
1953/**
1954 * sscanf - Unformat a buffer into a list of arguments
1955 * @buf: input buffer
1956 * @fmt: formatting of buffer
1957 * @...: resulting arguments
1958 */
1959int sscanf(const char * buf, const char * fmt, ...)
1960{
1961 va_list args;
1962 int i;
1963
1964 va_start(args,fmt);
1965 i = vsscanf(buf,fmt,args);
1966 va_end(args);
1967 return i;
1968}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969EXPORT_SYMBOL(sscanf);