blob: b8a2f549ab0ef5db70ecae07de8ea3be4d67a06d [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
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080012/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * 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/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 * simple_strtoull - convert a string to an unsigned long 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 long simple_strtoull(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 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 }
74 if (endp)
75 *endp = (char *)cp;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080076
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 return result;
78}
Linus Torvalds1da177e2005-04-16 15:20:36 -070079EXPORT_SYMBOL(simple_strtoull);
80
81/**
André Goddard Rosa922ac252009-12-14 18:01:01 -080082 * simple_strtoul - convert a string to an unsigned 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 */
87unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
88{
89 return simple_strtoull(cp, endp, base);
90}
91EXPORT_SYMBOL(simple_strtoul);
92
93/**
94 * simple_strtol - convert a string to a signed long
95 * @cp: The start of the string
96 * @endp: A pointer to the end of the parsed string will be placed here
97 * @base: The number base to use
98 */
99long simple_strtol(const char *cp, char **endp, unsigned int base)
100{
101 if (*cp == '-')
102 return -simple_strtoul(cp + 1, endp, base);
103
104 return simple_strtoul(cp, endp, base);
105}
106EXPORT_SYMBOL(simple_strtol);
107
108/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 * simple_strtoll - convert a string to a signed long long
110 * @cp: The start of the string
111 * @endp: A pointer to the end of the parsed string will be placed here
112 * @base: The number base to use
113 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700114long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800116 if (*cp == '-')
Harvey Harrison22d27052008-10-16 13:40:35 -0700117 return -simple_strtoull(cp + 1, endp, base);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800118
Harvey Harrison22d27052008-10-16 13:40:35 -0700119 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
Hans Verkuil98d5ce02010-04-23 13:18:04 -0400121EXPORT_SYMBOL(simple_strtoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Yi Yang06b2a762008-02-08 04:21:57 -0800123/**
124 * strict_strtoul - convert a string to an unsigned long strictly
125 * @cp: The string to be converted
126 * @base: The number base to use
127 * @res: The converted result value
128 *
129 * strict_strtoul converts a string to an unsigned long only if the
130 * string is really an unsigned long string, any string containing
131 * any invalid char at the tail will be rejected and -EINVAL is returned,
132 * only a newline char at the tail is acceptible because people generally
133 * change a module parameter in the following way:
134 *
135 * echo 1024 > /sys/module/e1000/parameters/copybreak
136 *
137 * echo will append a newline to the tail.
138 *
139 * It returns 0 if conversion is successful and *res is set to the converted
140 * value, otherwise it returns -EINVAL and *res is set to 0.
141 *
142 * simple_strtoul just ignores the successive invalid characters and
143 * return the converted value of prefix part of the string.
144 */
Harvey Harrison9d85db22008-10-16 13:40:35 -0700145int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
146{
147 char *tail;
148 unsigned long val;
149 size_t len;
150
151 *res = 0;
152 len = strlen(cp);
153 if (len == 0)
154 return -EINVAL;
155
156 val = simple_strtoul(cp, &tail, base);
Pavel Macheke899aa82009-01-06 14:40:53 -0800157 if (tail == cp)
158 return -EINVAL;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800159
Harvey Harrison9d85db22008-10-16 13:40:35 -0700160 if ((*tail == '\0') ||
161 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
162 *res = val;
163 return 0;
164 }
165
166 return -EINVAL;
167}
168EXPORT_SYMBOL(strict_strtoul);
Yi Yang06b2a762008-02-08 04:21:57 -0800169
170/**
171 * strict_strtol - convert a string to a long strictly
172 * @cp: The string to be converted
173 * @base: The number base to use
174 * @res: The converted result value
175 *
176 * strict_strtol is similiar to strict_strtoul, but it allows the first
177 * character of a string is '-'.
178 *
179 * It returns 0 if conversion is successful and *res is set to the converted
180 * value, otherwise it returns -EINVAL and *res is set to 0.
181 */
Harvey Harrison9d85db22008-10-16 13:40:35 -0700182int strict_strtol(const char *cp, unsigned int base, long *res)
183{
184 int ret;
185 if (*cp == '-') {
186 ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
187 if (!ret)
188 *res = -(*res);
189 } else {
190 ret = strict_strtoul(cp, base, (unsigned long *)res);
191 }
192
193 return ret;
194}
195EXPORT_SYMBOL(strict_strtol);
Yi Yang06b2a762008-02-08 04:21:57 -0800196
197/**
198 * strict_strtoull - convert a string to an unsigned long long strictly
199 * @cp: The string to be converted
200 * @base: The number base to use
201 * @res: The converted result value
202 *
203 * strict_strtoull converts a string to an unsigned long long only if the
204 * string is really an unsigned long long string, any string containing
205 * any invalid char at the tail will be rejected and -EINVAL is returned,
206 * only a newline char at the tail is acceptible because people generally
207 * change a module parameter in the following way:
208 *
209 * echo 1024 > /sys/module/e1000/parameters/copybreak
210 *
211 * echo will append a newline to the tail of the string.
212 *
213 * It returns 0 if conversion is successful and *res is set to the converted
214 * value, otherwise it returns -EINVAL and *res is set to 0.
215 *
216 * simple_strtoull just ignores the successive invalid characters and
217 * return the converted value of prefix part of the string.
218 */
Harvey Harrison9d85db22008-10-16 13:40:35 -0700219int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
220{
221 char *tail;
222 unsigned long long val;
223 size_t len;
224
225 *res = 0;
226 len = strlen(cp);
227 if (len == 0)
228 return -EINVAL;
229
230 val = simple_strtoull(cp, &tail, base);
Pavel Macheke899aa82009-01-06 14:40:53 -0800231 if (tail == cp)
232 return -EINVAL;
Harvey Harrison9d85db22008-10-16 13:40:35 -0700233 if ((*tail == '\0') ||
234 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
235 *res = val;
236 return 0;
237 }
238
239 return -EINVAL;
240}
241EXPORT_SYMBOL(strict_strtoull);
Yi Yang06b2a762008-02-08 04:21:57 -0800242
243/**
244 * strict_strtoll - convert a string to a long long strictly
245 * @cp: The string to be converted
246 * @base: The number base to use
247 * @res: The converted result value
248 *
249 * strict_strtoll is similiar to strict_strtoull, but it allows the first
250 * character of a string is '-'.
251 *
252 * It returns 0 if conversion is successful and *res is set to the converted
253 * value, otherwise it returns -EINVAL and *res is set to 0.
254 */
Harvey Harrison9d85db22008-10-16 13:40:35 -0700255int strict_strtoll(const char *cp, unsigned int base, long long *res)
256{
257 int ret;
258 if (*cp == '-') {
259 ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
260 if (!ret)
261 *res = -(*res);
262 } else {
263 ret = strict_strtoull(cp, base, (unsigned long long *)res);
264 }
Yi Yang06b2a762008-02-08 04:21:57 -0800265
Harvey Harrison9d85db22008-10-16 13:40:35 -0700266 return ret;
267}
Yi Yang06b2a762008-02-08 04:21:57 -0800268EXPORT_SYMBOL(strict_strtoll);
Yi Yang06b2a762008-02-08 04:21:57 -0800269
Joe Perchescf3b4292010-05-24 14:33:16 -0700270static noinline_for_stack
271int skip_atoi(const char **s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800273 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 while (isdigit(**s))
276 i = i*10 + *((*s)++) - '0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return i;
279}
280
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700281/* Decimal conversion is by far the most typical, and is used
282 * for /proc and /sys data. This directly impacts e.g. top performance
283 * with many processes running. We optimize it for speed
284 * using code from
285 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
286 * (with permission from the author, Douglas W. Jones). */
287
288/* Formats correctly any integer in [0,99999].
289 * Outputs from one to five digits depending on input.
290 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
Joe Perchescf3b4292010-05-24 14:33:16 -0700291static noinline_for_stack
292char *put_dec_trunc(char *buf, unsigned q)
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700293{
294 unsigned d3, d2, d1, d0;
295 d1 = (q>>4) & 0xf;
296 d2 = (q>>8) & 0xf;
297 d3 = (q>>12);
298
299 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
300 q = (d0 * 0xcd) >> 11;
301 d0 = d0 - 10*q;
302 *buf++ = d0 + '0'; /* least significant digit */
303 d1 = q + 9*d3 + 5*d2 + d1;
304 if (d1 != 0) {
305 q = (d1 * 0xcd) >> 11;
306 d1 = d1 - 10*q;
307 *buf++ = d1 + '0'; /* next digit */
308
309 d2 = q + 2*d2;
310 if ((d2 != 0) || (d3 != 0)) {
311 q = (d2 * 0xd) >> 7;
312 d2 = d2 - 10*q;
313 *buf++ = d2 + '0'; /* next digit */
314
315 d3 = q + 4*d3;
316 if (d3 != 0) {
317 q = (d3 * 0xcd) >> 11;
318 d3 = d3 - 10*q;
319 *buf++ = d3 + '0'; /* next digit */
320 if (q != 0)
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800321 *buf++ = q + '0'; /* most sign. digit */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700322 }
323 }
324 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800325
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700326 return buf;
327}
328/* Same with if's removed. Always emits five digits */
Joe Perchescf3b4292010-05-24 14:33:16 -0700329static noinline_for_stack
330char *put_dec_full(char *buf, unsigned q)
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700331{
332 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
333 /* but anyway, gcc produces better code with full-sized ints */
334 unsigned d3, d2, d1, d0;
335 d1 = (q>>4) & 0xf;
336 d2 = (q>>8) & 0xf;
337 d3 = (q>>12);
338
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800339 /*
340 * Possible ways to approx. divide by 10
341 * gcc -O2 replaces multiply with shifts and adds
342 * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
343 * (x * 0x67) >> 10: 1100111
344 * (x * 0x34) >> 9: 110100 - same
345 * (x * 0x1a) >> 8: 11010 - same
346 * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
347 */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700348 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
349 q = (d0 * 0xcd) >> 11;
350 d0 = d0 - 10*q;
351 *buf++ = d0 + '0';
352 d1 = q + 9*d3 + 5*d2 + d1;
353 q = (d1 * 0xcd) >> 11;
354 d1 = d1 - 10*q;
355 *buf++ = d1 + '0';
356
357 d2 = q + 2*d2;
358 q = (d2 * 0xd) >> 7;
359 d2 = d2 - 10*q;
360 *buf++ = d2 + '0';
361
362 d3 = q + 4*d3;
363 q = (d3 * 0xcd) >> 11; /* - shorter code */
364 /* q = (d3 * 0x67) >> 10; - would also work */
365 d3 = d3 - 10*q;
366 *buf++ = d3 + '0';
367 *buf++ = q + '0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800368
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700369 return buf;
370}
371/* No inlining helps gcc to use registers better */
Joe Perchescf3b4292010-05-24 14:33:16 -0700372static noinline_for_stack
373char *put_dec(char *buf, unsigned long long num)
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700374{
375 while (1) {
376 unsigned rem;
377 if (num < 100000)
378 return put_dec_trunc(buf, num);
379 rem = do_div(num, 100000);
380 buf = put_dec_full(buf, rem);
381 }
382}
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384#define ZEROPAD 1 /* pad with zero */
385#define SIGN 2 /* unsigned/signed long */
386#define PLUS 4 /* show plus */
387#define SPACE 8 /* space if plus */
388#define LEFT 16 /* left justified */
Bjorn Helgaasb89dc5d2010-03-05 10:47:31 -0700389#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
390#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100392enum format_type {
393 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100394 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100395 FORMAT_TYPE_PRECISION,
396 FORMAT_TYPE_CHAR,
397 FORMAT_TYPE_STR,
398 FORMAT_TYPE_PTR,
399 FORMAT_TYPE_PERCENT_CHAR,
400 FORMAT_TYPE_INVALID,
401 FORMAT_TYPE_LONG_LONG,
402 FORMAT_TYPE_ULONG,
403 FORMAT_TYPE_LONG,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800404 FORMAT_TYPE_UBYTE,
405 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100406 FORMAT_TYPE_USHORT,
407 FORMAT_TYPE_SHORT,
408 FORMAT_TYPE_UINT,
409 FORMAT_TYPE_INT,
410 FORMAT_TYPE_NRCHARS,
411 FORMAT_TYPE_SIZE_T,
412 FORMAT_TYPE_PTRDIFF
413};
414
415struct printf_spec {
Joe Perches4e310fd2010-04-14 09:27:40 -0700416 u8 type; /* format_type enum */
Joe Perchesef0658f2010-03-06 17:10:14 -0800417 u8 flags; /* flags to number() */
Joe Perches4e310fd2010-04-14 09:27:40 -0700418 u8 base; /* number base, 8, 10 or 16 only */
419 u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
420 s16 field_width; /* width of output field */
421 s16 precision; /* # of digits/chars */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100422};
423
Joe Perchescf3b4292010-05-24 14:33:16 -0700424static noinline_for_stack
425char *number(char *buf, char *end, unsigned long long num,
426 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100428 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
429 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
430
431 char tmp[66];
432 char sign;
433 char locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100434 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 int i;
436
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100437 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
438 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100439 locase = (spec.flags & SMALL);
440 if (spec.flags & LEFT)
441 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100443 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800444 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800446 num = -(signed long long)num;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100447 spec.field_width--;
448 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 sign = '+';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100450 spec.field_width--;
451 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 sign = ' ';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100453 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700456 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100457 spec.field_width--;
458 if (spec.base == 16)
459 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700461
462 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 i = 0;
464 if (num == 0)
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700465 tmp[i++] = '0';
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700466 /* Generic code, for any base:
467 else do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100468 tmp[i++] = (digits[do_div(num,base)] | locase);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700469 } while (num != 0);
470 */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100471 else if (spec.base != 10) { /* 8 or 16 */
472 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700473 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800474
475 if (spec.base == 16)
476 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700477 do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100478 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700479 num >>= shift;
480 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700481 } else { /* base 10 */
482 i = put_dec(tmp, num) - tmp;
483 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700484
485 /* printing 100 using %2d gives "100", not "00" */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100486 if (i > spec.precision)
487 spec.precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700488 /* leading space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100489 spec.field_width -= spec.precision;
490 if (!(spec.flags & (ZEROPAD+LEFT))) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800491 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700492 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 *buf = ' ';
494 ++buf;
495 }
496 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700497 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700499 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 *buf = sign;
501 ++buf;
502 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700503 /* "0x" / "0" prefix */
504 if (need_pfx) {
505 if (buf < end)
506 *buf = '0';
507 ++buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100508 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700509 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100510 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 ++buf;
512 }
513 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700514 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100515 if (!(spec.flags & LEFT)) {
516 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
517 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700518 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 *buf = c;
520 ++buf;
521 }
522 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700523 /* hmm even more zero padding? */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100524 while (i <= --spec.precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700525 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 *buf = '0';
527 ++buf;
528 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700529 /* actual digits of result */
530 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700531 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 *buf = tmp[i];
533 ++buf;
534 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700535 /* trailing space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100536 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700537 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 *buf = ' ';
539 ++buf;
540 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return buf;
543}
544
Joe Perchescf3b4292010-05-24 14:33:16 -0700545static noinline_for_stack
546char *string(char *buf, char *end, const char *s, struct printf_spec spec)
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700547{
548 int len, i;
549
550 if ((unsigned long)s < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -0800551 s = "(null)";
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700552
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100553 len = strnlen(s, spec.precision);
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700554
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100555 if (!(spec.flags & LEFT)) {
556 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700557 if (buf < end)
558 *buf = ' ';
559 ++buf;
560 }
561 }
562 for (i = 0; i < len; ++i) {
563 if (buf < end)
564 *buf = *s;
565 ++buf; ++s;
566 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100567 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700568 if (buf < end)
569 *buf = ' ';
570 ++buf;
571 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800572
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700573 return buf;
574}
575
Joe Perchescf3b4292010-05-24 14:33:16 -0700576static noinline_for_stack
577char *symbol_string(char *buf, char *end, void *ptr,
578 struct printf_spec spec, char ext)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700579{
580 unsigned long value = (unsigned long) ptr;
581#ifdef CONFIG_KALLSYMS
582 char sym[KSYM_SYMBOL_LEN];
Steven Rostedt91adcd22009-09-16 20:03:06 -0400583 if (ext != 'f' && ext != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200584 sprint_symbol(sym, value);
585 else
586 kallsyms_lookup(value, NULL, NULL, NULL, sym);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800587
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100588 return string(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700589#else
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800590 spec.field_width = 2 * sizeof(void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100591 spec.flags |= SPECIAL | SMALL | ZEROPAD;
592 spec.base = 16;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800593
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100594 return number(buf, end, value, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700595#endif
596}
597
Joe Perchescf3b4292010-05-24 14:33:16 -0700598static noinline_for_stack
599char *resource_string(char *buf, char *end, struct resource *res,
600 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100601{
602#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600603#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100604#endif
605
606#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600607#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100608#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700609 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100610 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700611 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100612 .precision = -1,
613 .flags = SPECIAL | SMALL | ZEROPAD,
614 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700615 static const struct printf_spec mem_spec = {
616 .base = 16,
617 .field_width = MEM_RSRC_PRINTK_SIZE,
618 .precision = -1,
619 .flags = SPECIAL | SMALL | ZEROPAD,
620 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700621 static const struct printf_spec bus_spec = {
622 .base = 16,
623 .field_width = 2,
624 .precision = -1,
625 .flags = SMALL | ZEROPAD,
626 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700627 static const struct printf_spec dec_spec = {
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600628 .base = 10,
629 .precision = -1,
630 .flags = 0,
631 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700632 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600633 .field_width = -1,
634 .precision = 10,
635 .flags = LEFT,
636 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700637 static const struct printf_spec flag_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600638 .base = 16,
639 .precision = -1,
640 .flags = SPECIAL | SMALL,
641 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600642
643 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
644 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
645#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
646#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700647#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600648#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
649 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
650 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
651
Linus Torvalds332d2e72008-10-20 15:07:34 +1100652 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600653 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700654 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100655
656 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700657 if (res->flags & IORESOURCE_IO) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600658 p = string(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700659 specp = &io_spec;
660 } else if (res->flags & IORESOURCE_MEM) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600661 p = string(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700662 specp = &mem_spec;
663 } else if (res->flags & IORESOURCE_IRQ) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600664 p = string(p, pend, "irq ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700665 specp = &dec_spec;
666 } else if (res->flags & IORESOURCE_DMA) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600667 p = string(p, pend, "dma ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700668 specp = &dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700669 } else if (res->flags & IORESOURCE_BUS) {
670 p = string(p, pend, "bus ", str_spec);
671 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700672 } else {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600673 p = string(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700674 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600675 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600676 }
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700677 p = number(p, pend, res->start, *specp);
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600678 if (res->start != res->end) {
679 *p++ = '-';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700680 p = number(p, pend, res->end, *specp);
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600681 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600682 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600683 if (res->flags & IORESOURCE_MEM_64)
684 p = string(p, pend, " 64bit", str_spec);
685 if (res->flags & IORESOURCE_PREFETCH)
686 p = string(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700687 if (res->flags & IORESOURCE_WINDOW)
688 p = string(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600689 if (res->flags & IORESOURCE_DISABLED)
690 p = string(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600691 } else {
692 p = string(p, pend, " flags ", str_spec);
693 p = number(p, pend, res->flags, flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600694 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100695 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600696 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +1100697
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100698 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100699}
700
Joe Perchescf3b4292010-05-24 14:33:16 -0700701static noinline_for_stack
702char *mac_address_string(char *buf, char *end, u8 *addr,
703 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700704{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000705 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700706 char *p = mac_addr;
707 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000708 char separator;
709
710 if (fmt[1] == 'F') { /* FDDI canonical format */
Joe Perchesbc7259a2010-01-07 11:43:50 +0000711 separator = '-';
712 } else {
Joe Perchesbc7259a2010-01-07 11:43:50 +0000713 separator = ':';
714 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700715
716 for (i = 0; i < 6; i++) {
Joe Perchesc8e00062010-01-11 00:44:14 -0800717 p = pack_hex_byte(p, addr[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000718 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +0000719 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700720 }
721 *p = '\0';
722
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100723 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700724}
725
Joe Perchescf3b4292010-05-24 14:33:16 -0700726static noinline_for_stack
727char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -0700728{
Harvey Harrison689afa72008-10-28 16:04:44 -0700729 int i;
Joe Perches0159f242010-01-13 20:23:30 -0800730 bool leading_zeros = (fmt[0] == 'i');
731 int index;
732 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -0700733
Joe Perches0159f242010-01-13 20:23:30 -0800734 switch (fmt[2]) {
735 case 'h':
736#ifdef __BIG_ENDIAN
737 index = 0;
738 step = 1;
739#else
740 index = 3;
741 step = -1;
742#endif
743 break;
744 case 'l':
745 index = 3;
746 step = -1;
747 break;
748 case 'n':
749 case 'b':
750 default:
751 index = 0;
752 step = 1;
753 break;
754 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000755 for (i = 0; i < 4; i++) {
756 char temp[3]; /* hold each IP quad in reverse order */
Joe Perches0159f242010-01-13 20:23:30 -0800757 int digits = put_dec_trunc(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000758 if (leading_zeros) {
759 if (digits < 3)
760 *p++ = '0';
761 if (digits < 2)
762 *p++ = '0';
763 }
764 /* reverse the digits in the quad */
765 while (digits--)
766 *p++ = temp[digits];
767 if (i < 3)
768 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -0800769 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000770 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000771 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800772
Joe Perches8a27f7c2009-08-17 12:29:44 +0000773 return p;
774}
775
Joe Perchescf3b4292010-05-24 14:33:16 -0700776static noinline_for_stack
777char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000778{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800779 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000780 unsigned char zerolength[8];
781 int longest = 1;
782 int colonpos = -1;
783 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800784 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000785 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +0000786 bool useIPv4;
787 struct in6_addr in6;
788
789 memcpy(&in6, addr, sizeof(struct in6_addr));
790
791 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000792
793 memset(zerolength, 0, sizeof(zerolength));
794
795 if (useIPv4)
796 range = 6;
797 else
798 range = 8;
799
800 /* find position of longest 0 run */
801 for (i = 0; i < range; i++) {
802 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +0000803 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000804 break;
805 zerolength[i]++;
806 }
807 }
808 for (i = 0; i < range; i++) {
809 if (zerolength[i] > longest) {
810 longest = zerolength[i];
811 colonpos = i;
812 }
813 }
814
815 /* emit address */
816 for (i = 0; i < range; i++) {
817 if (i == colonpos) {
818 if (needcolon || i == 0)
819 *p++ = ':';
820 *p++ = ':';
821 needcolon = false;
822 i += longest - 1;
823 continue;
824 }
825 if (needcolon) {
826 *p++ = ':';
827 needcolon = false;
828 }
829 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +0000830 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000831 hi = word >> 8;
832 lo = word & 0xff;
833 if (hi) {
834 if (hi > 0x0f)
835 p = pack_hex_byte(p, hi);
836 else
837 *p++ = hex_asc_lo(hi);
André Goddard Rosab5ff9922009-12-14 18:00:59 -0800838 p = pack_hex_byte(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000839 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -0800840 else if (lo > 0x0f)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000841 p = pack_hex_byte(p, lo);
842 else
843 *p++ = hex_asc_lo(lo);
844 needcolon = true;
845 }
846
847 if (useIPv4) {
848 if (needcolon)
849 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -0800850 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +0000851 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000852 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800853
Joe Perches8a27f7c2009-08-17 12:29:44 +0000854 return p;
855}
856
Joe Perchescf3b4292010-05-24 14:33:16 -0700857static noinline_for_stack
858char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000859{
860 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800861
Harvey Harrison689afa72008-10-28 16:04:44 -0700862 for (i = 0; i < 8; i++) {
Joe Percheseb78cd22009-09-18 13:04:06 +0000863 p = pack_hex_byte(p, *addr++);
864 p = pack_hex_byte(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000865 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -0700866 *p++ = ':';
867 }
868 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800869
Joe Perches8a27f7c2009-08-17 12:29:44 +0000870 return p;
871}
872
Joe Perchescf3b4292010-05-24 14:33:16 -0700873static noinline_for_stack
874char *ip6_addr_string(char *buf, char *end, const u8 *addr,
875 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000876{
877 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
878
879 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +0000880 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000881 else
Joe Percheseb78cd22009-09-18 13:04:06 +0000882 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -0700883
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100884 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -0700885}
886
Joe Perchescf3b4292010-05-24 14:33:16 -0700887static noinline_for_stack
888char *ip4_addr_string(char *buf, char *end, const u8 *addr,
889 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -0700890{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000891 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -0700892
Joe Perches0159f242010-01-13 20:23:30 -0800893 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700894
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100895 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700896}
897
Joe Perchescf3b4292010-05-24 14:33:16 -0700898static noinline_for_stack
899char *uuid_string(char *buf, char *end, const u8 *addr,
900 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -0800901{
902 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
903 char *p = uuid;
904 int i;
905 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
906 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
907 const u8 *index = be;
908 bool uc = false;
909
910 switch (*(++fmt)) {
911 case 'L':
912 uc = true; /* fall-through */
913 case 'l':
914 index = le;
915 break;
916 case 'B':
917 uc = true;
918 break;
919 }
920
921 for (i = 0; i < 16; i++) {
922 p = pack_hex_byte(p, addr[index[i]]);
923 switch (i) {
924 case 3:
925 case 5:
926 case 7:
927 case 9:
928 *p++ = '-';
929 break;
930 }
931 }
932
933 *p = 0;
934
935 if (uc) {
936 p = uuid;
937 do {
938 *p = toupper(*p);
939 } while (*(++p));
940 }
941
942 return string(buf, end, uuid, spec);
943}
944
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700945/*
946 * Show a '%p' thing. A kernel extension is that the '%p' is followed
947 * by an extra set of alphanumeric characters that are extended format
948 * specifiers.
949 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100950 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700951 *
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200952 * - 'F' For symbolic function descriptor pointers with offset
953 * - 'f' For simple symbolic function names without offset
Steven Rostedt0efb4d22009-09-17 09:27:29 -0400954 * - 'S' For symbolic direct pointers with offset
955 * - 's' For symbolic direct pointers without offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600956 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
957 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700958 * - 'M' For a 6-byte MAC address, it prints the address in the
959 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +0000960 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +0000961 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -0800962 * with a dash-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +0000963 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
964 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
965 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
966 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
967 * IPv6 omits the colons (01020304...0f)
968 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Joe Perches0159f242010-01-13 20:23:30 -0800969 * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
Joe Perches8a27f7c2009-08-17 12:29:44 +0000970 * - 'I6c' for IPv6 addresses printed as specified by
Uwe Kleine-König3f472402010-01-08 14:43:02 -0800971 * http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00
Joe Perches9ac6e442009-12-14 18:01:09 -0800972 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
973 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
974 * Options for %pU are:
975 * b big endian lower case hex (default)
976 * B big endian UPPER case hex
977 * l little endian lower case hex
978 * L little endian UPPER case hex
979 * big endian output byte order is:
980 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
981 * little endian output byte order is:
982 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
983 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100984 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
985 * function pointers are really function descriptors, which contain a
986 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700987 */
Joe Perchescf3b4292010-05-24 14:33:16 -0700988static noinline_for_stack
989char *pointer(const char *fmt, char *buf, char *end, void *ptr,
990 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700991{
Linus Torvaldsd97106a2009-01-03 11:46:17 -0800992 if (!ptr)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100993 return string(buf, end, "(null)", spec);
Linus Torvaldsd97106a2009-01-03 11:46:17 -0800994
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700995 switch (*fmt) {
996 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200997 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700998 ptr = dereference_function_descriptor(ptr);
999 /* Fallthrough */
1000 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08001001 case 's':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001002 return symbol_string(buf, end, ptr, spec, *fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001003 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001004 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001005 return resource_string(buf, end, ptr, spec, fmt);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001006 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1007 case 'm': /* Contiguous: 000102030405 */
Joe Perchesbc7259a2010-01-07 11:43:50 +00001008 /* [mM]F (FDDI, bit reversed) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00001009 return mac_address_string(buf, end, ptr, spec, fmt);
1010 case 'I': /* Formatted IP supported
1011 * 4: 1.2.3.4
1012 * 6: 0001:0203:...:0708
1013 * 6c: 1::708 or 1::1.2.3.4
1014 */
1015 case 'i': /* Contiguous:
1016 * 4: 001.002.003.004
1017 * 6: 000102...0f
1018 */
1019 switch (fmt[1]) {
1020 case '6':
1021 return ip6_addr_string(buf, end, ptr, spec, fmt);
1022 case '4':
1023 return ip4_addr_string(buf, end, ptr, spec, fmt);
1024 }
Harvey Harrison4aa99602008-10-29 12:49:58 -07001025 break;
Joe Perches9ac6e442009-12-14 18:01:09 -08001026 case 'U':
1027 return uuid_string(buf, end, ptr, spec, fmt);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001028 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001029 spec.flags |= SMALL;
1030 if (spec.field_width == -1) {
1031 spec.field_width = 2*sizeof(void *);
1032 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001033 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001034 spec.base = 16;
1035
1036 return number(buf, end, (unsigned long) ptr, spec);
1037}
1038
1039/*
1040 * Helper function to decode printf style format.
1041 * Each call decode a token from the format and return the
1042 * number of characters read (or likely the delta where it wants
1043 * to go on the next call).
1044 * The decoded token is returned through the parameters
1045 *
1046 * 'h', 'l', or 'L' for integer fields
1047 * 'z' support added 23/7/1999 S.H.
1048 * 'z' changed to 'Z' --davidm 1/25/99
1049 * 't' added for ptrdiff_t
1050 *
1051 * @fmt: the format string
1052 * @type of the token returned
1053 * @flags: various flags such as +, -, # tokens..
1054 * @field_width: overwritten width
1055 * @base: base of the number (octal, hex, ...)
1056 * @precision: precision of a number
1057 * @qualifier: qualifier of a number (long, size_t, ...)
1058 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001059static noinline_for_stack
1060int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001061{
1062 const char *start = fmt;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001063
1064 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01001065 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001066 if (spec->field_width < 0) {
1067 spec->field_width = -spec->field_width;
1068 spec->flags |= LEFT;
1069 }
1070 spec->type = FORMAT_TYPE_NONE;
1071 goto precision;
1072 }
1073
1074 /* we finished early by reading the precision */
1075 if (spec->type == FORMAT_TYPE_PRECISION) {
1076 if (spec->precision < 0)
1077 spec->precision = 0;
1078
1079 spec->type = FORMAT_TYPE_NONE;
1080 goto qualifier;
1081 }
1082
1083 /* By default */
1084 spec->type = FORMAT_TYPE_NONE;
1085
1086 for (; *fmt ; ++fmt) {
1087 if (*fmt == '%')
1088 break;
1089 }
1090
1091 /* Return the current non-format string */
1092 if (fmt != start || !*fmt)
1093 return fmt - start;
1094
1095 /* Process flags */
1096 spec->flags = 0;
1097
1098 while (1) { /* this also skips first '%' */
1099 bool found = true;
1100
1101 ++fmt;
1102
1103 switch (*fmt) {
1104 case '-': spec->flags |= LEFT; break;
1105 case '+': spec->flags |= PLUS; break;
1106 case ' ': spec->flags |= SPACE; break;
1107 case '#': spec->flags |= SPECIAL; break;
1108 case '0': spec->flags |= ZEROPAD; break;
1109 default: found = false;
1110 }
1111
1112 if (!found)
1113 break;
1114 }
1115
1116 /* get field width */
1117 spec->field_width = -1;
1118
1119 if (isdigit(*fmt))
1120 spec->field_width = skip_atoi(&fmt);
1121 else if (*fmt == '*') {
1122 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01001123 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001124 return ++fmt - start;
1125 }
1126
1127precision:
1128 /* get the precision */
1129 spec->precision = -1;
1130 if (*fmt == '.') {
1131 ++fmt;
1132 if (isdigit(*fmt)) {
1133 spec->precision = skip_atoi(&fmt);
1134 if (spec->precision < 0)
1135 spec->precision = 0;
1136 } else if (*fmt == '*') {
1137 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01001138 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001139 return ++fmt - start;
1140 }
1141 }
1142
1143qualifier:
1144 /* get the conversion qualifier */
1145 spec->qualifier = -1;
André Goddard Rosa08562cb2009-12-14 18:00:58 -08001146 if (*fmt == 'h' || TOLOWER(*fmt) == 'l' ||
1147 TOLOWER(*fmt) == 'z' || *fmt == 't') {
Zhaoleia4e94ef2009-03-27 17:07:05 +08001148 spec->qualifier = *fmt++;
1149 if (unlikely(spec->qualifier == *fmt)) {
1150 if (spec->qualifier == 'l') {
1151 spec->qualifier = 'L';
1152 ++fmt;
1153 } else if (spec->qualifier == 'h') {
1154 spec->qualifier = 'H';
1155 ++fmt;
1156 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001157 }
1158 }
1159
1160 /* default base */
1161 spec->base = 10;
1162 switch (*fmt) {
1163 case 'c':
1164 spec->type = FORMAT_TYPE_CHAR;
1165 return ++fmt - start;
1166
1167 case 's':
1168 spec->type = FORMAT_TYPE_STR;
1169 return ++fmt - start;
1170
1171 case 'p':
1172 spec->type = FORMAT_TYPE_PTR;
1173 return fmt - start;
1174 /* skip alnum */
1175
1176 case 'n':
1177 spec->type = FORMAT_TYPE_NRCHARS;
1178 return ++fmt - start;
1179
1180 case '%':
1181 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1182 return ++fmt - start;
1183
1184 /* integer number formats - set up the flags and "break" */
1185 case 'o':
1186 spec->base = 8;
1187 break;
1188
1189 case 'x':
1190 spec->flags |= SMALL;
1191
1192 case 'X':
1193 spec->base = 16;
1194 break;
1195
1196 case 'd':
1197 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001198 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001199 case 'u':
1200 break;
1201
1202 default:
1203 spec->type = FORMAT_TYPE_INVALID;
1204 return fmt - start;
1205 }
1206
1207 if (spec->qualifier == 'L')
1208 spec->type = FORMAT_TYPE_LONG_LONG;
1209 else if (spec->qualifier == 'l') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001210 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001211 spec->type = FORMAT_TYPE_LONG;
1212 else
1213 spec->type = FORMAT_TYPE_ULONG;
André Goddard Rosa08562cb2009-12-14 18:00:58 -08001214 } else if (TOLOWER(spec->qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001215 spec->type = FORMAT_TYPE_SIZE_T;
1216 } else if (spec->qualifier == 't') {
1217 spec->type = FORMAT_TYPE_PTRDIFF;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001218 } else if (spec->qualifier == 'H') {
1219 if (spec->flags & SIGN)
1220 spec->type = FORMAT_TYPE_BYTE;
1221 else
1222 spec->type = FORMAT_TYPE_UBYTE;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001223 } else if (spec->qualifier == 'h') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001224 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001225 spec->type = FORMAT_TYPE_SHORT;
1226 else
1227 spec->type = FORMAT_TYPE_USHORT;
1228 } else {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001229 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001230 spec->type = FORMAT_TYPE_INT;
1231 else
1232 spec->type = FORMAT_TYPE_UINT;
1233 }
1234
1235 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001236}
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238/**
1239 * vsnprintf - Format a string and place it in a buffer
1240 * @buf: The buffer to place the result into
1241 * @size: The size of the buffer, including the trailing null space
1242 * @fmt: The format string to use
1243 * @args: Arguments for the format string
1244 *
Andi Kleen20036fd2008-10-15 22:02:02 -07001245 * This function follows C99 vsnprintf, but has some extensions:
Steven Rostedt91adcd22009-09-16 20:03:06 -04001246 * %pS output the name of a text symbol with offset
1247 * %ps output the name of a text symbol without offset
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001248 * %pF output the name of a function pointer with its offset
1249 * %pf output the name of a function pointer without its offset
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001250 * %pR output the address range in a struct resource with decoded flags
1251 * %pr output the address range in a struct resource with raw flags
1252 * %pM output a 6-byte MAC address with colons
1253 * %pm output a 6-byte MAC address without colons
1254 * %pI4 print an IPv4 address without leading zeros
1255 * %pi4 print an IPv4 address with leading zeros
1256 * %pI6 print an IPv6 address with colons
1257 * %pi6 print an IPv6 address without colons
1258 * %pI6c print an IPv6 address as specified by
Uwe Kleine-König3f472402010-01-08 14:43:02 -08001259 * http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001260 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1261 * case.
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001262 * %n is ignored
Andi Kleen20036fd2008-10-15 22:02:02 -07001263 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 * The return value is the number of characters which would
1265 * be generated for the given input, excluding the trailing
1266 * '\0', as per ISO C99. If you want to have the exact
1267 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001268 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 * return is greater than or equal to @size, the resulting
1270 * string is truncated.
1271 *
1272 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001273 * You probably want snprintf() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 */
1275int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1276{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001278 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001279 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001281 /* Reject out-of-range values early. Large positive sizes are
1282 used for unknown buffer sizes. */
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001283 if (WARN_ON_ONCE((int) size < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001287 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001289 /* Make sure end is always >= buf */
1290 if (end < buf) {
1291 end = ((void *)-1);
1292 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
1294
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001295 while (*fmt) {
1296 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001297 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001298
1299 fmt += read;
1300
1301 switch (spec.type) {
1302 case FORMAT_TYPE_NONE: {
1303 int copy = read;
1304 if (str < end) {
1305 if (copy > end - str)
1306 copy = end - str;
1307 memcpy(str, old_fmt, copy);
1308 }
1309 str += read;
1310 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
1312
Vegard Nossumed681a92009-03-14 12:08:50 +01001313 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001314 spec.field_width = va_arg(args, int);
1315 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001317 case FORMAT_TYPE_PRECISION:
1318 spec.precision = va_arg(args, int);
1319 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
André Goddard Rosad4be1512009-12-14 18:00:59 -08001321 case FORMAT_TYPE_CHAR: {
1322 char c;
1323
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001324 if (!(spec.flags & LEFT)) {
1325 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001326 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 *str = ' ';
1328 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001331 }
1332 c = (unsigned char) va_arg(args, int);
1333 if (str < end)
1334 *str = c;
1335 ++str;
1336 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001337 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001338 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001340 }
1341 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001342 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001344 case FORMAT_TYPE_STR:
1345 str = string(str, end, va_arg(args, char *), spec);
1346 break;
1347
1348 case FORMAT_TYPE_PTR:
1349 str = pointer(fmt+1, str, end, va_arg(args, void *),
1350 spec);
1351 while (isalnum(*fmt))
1352 fmt++;
1353 break;
1354
1355 case FORMAT_TYPE_PERCENT_CHAR:
1356 if (str < end)
1357 *str = '%';
1358 ++str;
1359 break;
1360
1361 case FORMAT_TYPE_INVALID:
1362 if (str < end)
1363 *str = '%';
1364 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001365 break;
1366
1367 case FORMAT_TYPE_NRCHARS: {
Joe Perchesef0658f2010-03-06 17:10:14 -08001368 u8 qualifier = spec.qualifier;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001369
1370 if (qualifier == 'l') {
1371 long *ip = va_arg(args, long *);
1372 *ip = (str - buf);
André Goddard Rosa08562cb2009-12-14 18:00:58 -08001373 } else if (TOLOWER(qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001374 size_t *ip = va_arg(args, size_t *);
1375 *ip = (str - buf);
1376 } else {
1377 int *ip = va_arg(args, int *);
1378 *ip = (str - buf);
1379 }
1380 break;
1381 }
1382
1383 default:
1384 switch (spec.type) {
1385 case FORMAT_TYPE_LONG_LONG:
1386 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001388 case FORMAT_TYPE_ULONG:
1389 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001391 case FORMAT_TYPE_LONG:
1392 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001394 case FORMAT_TYPE_SIZE_T:
1395 num = va_arg(args, size_t);
1396 break;
1397 case FORMAT_TYPE_PTRDIFF:
1398 num = va_arg(args, ptrdiff_t);
1399 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001400 case FORMAT_TYPE_UBYTE:
1401 num = (unsigned char) va_arg(args, int);
1402 break;
1403 case FORMAT_TYPE_BYTE:
1404 num = (signed char) va_arg(args, int);
1405 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001406 case FORMAT_TYPE_USHORT:
1407 num = (unsigned short) va_arg(args, int);
1408 break;
1409 case FORMAT_TYPE_SHORT:
1410 num = (short) va_arg(args, int);
1411 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001412 case FORMAT_TYPE_INT:
1413 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001414 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001416 num = va_arg(args, unsigned int);
1417 }
1418
1419 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001422
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001423 if (size > 0) {
1424 if (str < end)
1425 *str = '\0';
1426 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07001427 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001428 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001429
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001430 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001432
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434EXPORT_SYMBOL(vsnprintf);
1435
1436/**
1437 * vscnprintf - Format a string and place it in a buffer
1438 * @buf: The buffer to place the result into
1439 * @size: The size of the buffer, including the trailing null space
1440 * @fmt: The format string to use
1441 * @args: Arguments for the format string
1442 *
1443 * The return value is the number of characters which have been written into
1444 * the @buf not including the trailing '\0'. If @size is <= 0 the function
1445 * returns 0.
1446 *
1447 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001448 * You probably want scnprintf() instead.
Andi Kleen20036fd2008-10-15 22:02:02 -07001449 *
1450 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 */
1452int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1453{
1454 int i;
1455
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001456 i = vsnprintf(buf, size, fmt, args);
1457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 return (i >= size) ? (size - 1) : i;
1459}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460EXPORT_SYMBOL(vscnprintf);
1461
1462/**
1463 * snprintf - Format a string and place it in a buffer
1464 * @buf: The buffer to place the result into
1465 * @size: The size of the buffer, including the trailing null space
1466 * @fmt: The format string to use
1467 * @...: Arguments for the format string
1468 *
1469 * The return value is the number of characters which would be
1470 * generated for the given input, excluding the trailing null,
1471 * as per ISO C99. If the return is greater than or equal to
1472 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07001473 *
1474 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001476int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477{
1478 va_list args;
1479 int i;
1480
1481 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001482 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001484
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 return i;
1486}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487EXPORT_SYMBOL(snprintf);
1488
1489/**
1490 * scnprintf - Format a string and place it in a buffer
1491 * @buf: The buffer to place the result into
1492 * @size: The size of the buffer, including the trailing null space
1493 * @fmt: The format string to use
1494 * @...: Arguments for the format string
1495 *
1496 * The return value is the number of characters written into @buf not including
Martin Peschkeea6f3282007-02-12 00:51:56 -08001497 * the trailing '\0'. If @size is <= 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 */
1499
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001500int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
1502 va_list args;
1503 int i;
1504
1505 va_start(args, fmt);
1506 i = vsnprintf(buf, size, fmt, args);
1507 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 return (i >= size) ? (size - 1) : i;
1510}
1511EXPORT_SYMBOL(scnprintf);
1512
1513/**
1514 * vsprintf - Format a string and place it in a buffer
1515 * @buf: The buffer to place the result into
1516 * @fmt: The format string to use
1517 * @args: Arguments for the format string
1518 *
1519 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001520 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 * buffer overflows.
1522 *
1523 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001524 * You probably want sprintf() instead.
Andi Kleen20036fd2008-10-15 22:02:02 -07001525 *
1526 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 */
1528int vsprintf(char *buf, const char *fmt, va_list args)
1529{
1530 return vsnprintf(buf, INT_MAX, fmt, args);
1531}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532EXPORT_SYMBOL(vsprintf);
1533
1534/**
1535 * sprintf - Format a string and place it in a buffer
1536 * @buf: The buffer to place the result into
1537 * @fmt: The format string to use
1538 * @...: Arguments for the format string
1539 *
1540 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001541 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07001543 *
1544 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001546int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547{
1548 va_list args;
1549 int i;
1550
1551 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001552 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001554
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 return i;
1556}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557EXPORT_SYMBOL(sprintf);
1558
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001559#ifdef CONFIG_BINARY_PRINTF
1560/*
1561 * bprintf service:
1562 * vbin_printf() - VA arguments to binary data
1563 * bstr_printf() - Binary data to text string
1564 */
1565
1566/**
1567 * vbin_printf - Parse a format string and place args' binary value in a buffer
1568 * @bin_buf: The buffer to place args' binary value
1569 * @size: The size of the buffer(by words(32bits), not characters)
1570 * @fmt: The format string to use
1571 * @args: Arguments for the format string
1572 *
1573 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1574 * is skiped.
1575 *
1576 * The return value is the number of words(32bits) which would be generated for
1577 * the given input.
1578 *
1579 * NOTE:
1580 * If the return value is greater than @size, the resulting bin_buf is NOT
1581 * valid for bstr_printf().
1582 */
1583int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1584{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001585 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001586 char *str, *end;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001587
1588 str = (char *)bin_buf;
1589 end = (char *)(bin_buf + size);
1590
1591#define save_arg(type) \
1592do { \
1593 if (sizeof(type) == 8) { \
1594 unsigned long long value; \
1595 str = PTR_ALIGN(str, sizeof(u32)); \
1596 value = va_arg(args, unsigned long long); \
1597 if (str + sizeof(type) <= end) { \
1598 *(u32 *)str = *(u32 *)&value; \
1599 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1600 } \
1601 } else { \
1602 unsigned long value; \
1603 str = PTR_ALIGN(str, sizeof(type)); \
1604 value = va_arg(args, int); \
1605 if (str + sizeof(type) <= end) \
1606 *(typeof(type) *)str = (type)value; \
1607 } \
1608 str += sizeof(type); \
1609} while (0)
1610
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001611 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08001612 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001613
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001614 fmt += read;
1615
1616 switch (spec.type) {
1617 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08001618 case FORMAT_TYPE_INVALID:
1619 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001620 break;
1621
Vegard Nossumed681a92009-03-14 12:08:50 +01001622 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001623 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001624 save_arg(int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001625 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001626
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001627 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001628 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001629 break;
1630
1631 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001632 const char *save_str = va_arg(args, char *);
1633 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08001634
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001635 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1636 || (unsigned long)save_str < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -08001637 save_str = "(null)";
André Goddard Rosa6c356632009-12-14 18:00:56 -08001638 len = strlen(save_str) + 1;
1639 if (str + len < end)
1640 memcpy(str, save_str, len);
1641 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001642 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001643 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001644
1645 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001646 save_arg(void *);
1647 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001648 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001649 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001650 break;
1651
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001652 case FORMAT_TYPE_NRCHARS: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001653 /* skip %n 's argument */
Joe Perchesef0658f2010-03-06 17:10:14 -08001654 u8 qualifier = spec.qualifier;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001655 void *skip_arg;
1656 if (qualifier == 'l')
1657 skip_arg = va_arg(args, long *);
André Goddard Rosa08562cb2009-12-14 18:00:58 -08001658 else if (TOLOWER(qualifier) == 'z')
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001659 skip_arg = va_arg(args, size_t *);
1660 else
1661 skip_arg = va_arg(args, int *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001662 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001663 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001664
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001665 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001666 switch (spec.type) {
1667
1668 case FORMAT_TYPE_LONG_LONG:
1669 save_arg(long long);
1670 break;
1671 case FORMAT_TYPE_ULONG:
1672 case FORMAT_TYPE_LONG:
1673 save_arg(unsigned long);
1674 break;
1675 case FORMAT_TYPE_SIZE_T:
1676 save_arg(size_t);
1677 break;
1678 case FORMAT_TYPE_PTRDIFF:
1679 save_arg(ptrdiff_t);
1680 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001681 case FORMAT_TYPE_UBYTE:
1682 case FORMAT_TYPE_BYTE:
1683 save_arg(char);
1684 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001685 case FORMAT_TYPE_USHORT:
1686 case FORMAT_TYPE_SHORT:
1687 save_arg(short);
1688 break;
1689 default:
1690 save_arg(int);
1691 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001692 }
1693 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001694
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001695 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001696#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001697}
1698EXPORT_SYMBOL_GPL(vbin_printf);
1699
1700/**
1701 * bstr_printf - Format a string from binary arguments and place it in a buffer
1702 * @buf: The buffer to place the result into
1703 * @size: The size of the buffer, including the trailing null space
1704 * @fmt: The format string to use
1705 * @bin_buf: Binary arguments for the format string
1706 *
1707 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1708 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1709 * a binary buffer that generated by vbin_printf.
1710 *
1711 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001712 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001713 *
1714 * The return value is the number of characters which would
1715 * be generated for the given input, excluding the trailing
1716 * '\0', as per ISO C99. If you want to have the exact
1717 * number of characters written into @buf as return value
1718 * (not including the trailing '\0'), use vscnprintf(). If the
1719 * return is greater than or equal to @size, the resulting
1720 * string is truncated.
1721 */
1722int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1723{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001724 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08001725 char *str, *end;
1726 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001727
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001728 if (WARN_ON_ONCE((int) size < 0))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001729 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001730
1731 str = buf;
1732 end = buf + size;
1733
1734#define get_arg(type) \
1735({ \
1736 typeof(type) value; \
1737 if (sizeof(type) == 8) { \
1738 args = PTR_ALIGN(args, sizeof(u32)); \
1739 *(u32 *)&value = *(u32 *)args; \
1740 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1741 } else { \
1742 args = PTR_ALIGN(args, sizeof(type)); \
1743 value = *(typeof(type) *)args; \
1744 } \
1745 args += sizeof(type); \
1746 value; \
1747})
1748
1749 /* Make sure end is always >= buf */
1750 if (end < buf) {
1751 end = ((void *)-1);
1752 size = end - buf;
1753 }
1754
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001755 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001756 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001757 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001758
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001759 fmt += read;
1760
1761 switch (spec.type) {
1762 case FORMAT_TYPE_NONE: {
1763 int copy = read;
1764 if (str < end) {
1765 if (copy > end - str)
1766 copy = end - str;
1767 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001768 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001769 str += read;
1770 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001771 }
1772
Vegard Nossumed681a92009-03-14 12:08:50 +01001773 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001774 spec.field_width = get_arg(int);
1775 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001776
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001777 case FORMAT_TYPE_PRECISION:
1778 spec.precision = get_arg(int);
1779 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001780
André Goddard Rosad4be1512009-12-14 18:00:59 -08001781 case FORMAT_TYPE_CHAR: {
1782 char c;
1783
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001784 if (!(spec.flags & LEFT)) {
1785 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001786 if (str < end)
1787 *str = ' ';
1788 ++str;
1789 }
1790 }
1791 c = (unsigned char) get_arg(char);
1792 if (str < end)
1793 *str = c;
1794 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001795 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001796 if (str < end)
1797 *str = ' ';
1798 ++str;
1799 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001800 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001801 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001802
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001803 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001804 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001805 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001806 str = string(str, end, (char *)str_arg, spec);
1807 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001808 }
1809
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001810 case FORMAT_TYPE_PTR:
1811 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1812 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001813 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001814 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001815
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001816 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001817 case FORMAT_TYPE_INVALID:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001818 if (str < end)
1819 *str = '%';
1820 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001821 break;
1822
1823 case FORMAT_TYPE_NRCHARS:
1824 /* skip */
1825 break;
1826
André Goddard Rosad4be1512009-12-14 18:00:59 -08001827 default: {
1828 unsigned long long num;
1829
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001830 switch (spec.type) {
1831
1832 case FORMAT_TYPE_LONG_LONG:
1833 num = get_arg(long long);
1834 break;
1835 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001836 case FORMAT_TYPE_LONG:
1837 num = get_arg(unsigned long);
1838 break;
1839 case FORMAT_TYPE_SIZE_T:
1840 num = get_arg(size_t);
1841 break;
1842 case FORMAT_TYPE_PTRDIFF:
1843 num = get_arg(ptrdiff_t);
1844 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001845 case FORMAT_TYPE_UBYTE:
1846 num = get_arg(unsigned char);
1847 break;
1848 case FORMAT_TYPE_BYTE:
1849 num = get_arg(signed char);
1850 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001851 case FORMAT_TYPE_USHORT:
1852 num = get_arg(unsigned short);
1853 break;
1854 case FORMAT_TYPE_SHORT:
1855 num = get_arg(short);
1856 break;
1857 case FORMAT_TYPE_UINT:
1858 num = get_arg(unsigned int);
1859 break;
1860 default:
1861 num = get_arg(int);
1862 }
1863
1864 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08001865 } /* default: */
1866 } /* switch(spec.type) */
1867 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001868
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001869 if (size > 0) {
1870 if (str < end)
1871 *str = '\0';
1872 else
1873 end[-1] = '\0';
1874 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001875
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001876#undef get_arg
1877
1878 /* the trailing null byte doesn't count towards the total */
1879 return str - buf;
1880}
1881EXPORT_SYMBOL_GPL(bstr_printf);
1882
1883/**
1884 * bprintf - Parse a format string and place args' binary value in a buffer
1885 * @bin_buf: The buffer to place args' binary value
1886 * @size: The size of the buffer(by words(32bits), not characters)
1887 * @fmt: The format string to use
1888 * @...: Arguments for the format string
1889 *
1890 * The function returns the number of words(u32) written
1891 * into @bin_buf.
1892 */
1893int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1894{
1895 va_list args;
1896 int ret;
1897
1898 va_start(args, fmt);
1899 ret = vbin_printf(bin_buf, size, fmt, args);
1900 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001901
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001902 return ret;
1903}
1904EXPORT_SYMBOL_GPL(bprintf);
1905
1906#endif /* CONFIG_BINARY_PRINTF */
1907
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908/**
1909 * vsscanf - Unformat a buffer into a list of arguments
1910 * @buf: input buffer
1911 * @fmt: format of buffer
1912 * @args: arguments
1913 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001914int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
1916 const char *str = buf;
1917 char *next;
1918 char digit;
1919 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08001920 u8 qualifier;
1921 u8 base;
1922 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001923 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001925 while (*fmt && *str) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 /* skip any white space in format */
1927 /* white space in format matchs any amount of
1928 * white space, including none, in the input.
1929 */
1930 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08001931 fmt = skip_spaces(++fmt);
1932 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 }
1934
1935 /* anything that is not a conversion must match exactly */
1936 if (*fmt != '%' && *fmt) {
1937 if (*fmt++ != *str++)
1938 break;
1939 continue;
1940 }
1941
1942 if (!*fmt)
1943 break;
1944 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001945
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 /* skip this conversion.
1947 * advance both strings to next white space
1948 */
1949 if (*fmt == '*') {
Andy Spencer8fccae22009-10-01 15:44:27 -07001950 while (!isspace(*fmt) && *fmt != '%' && *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 fmt++;
1952 while (!isspace(*str) && *str)
1953 str++;
1954 continue;
1955 }
1956
1957 /* get field width */
1958 field_width = -1;
1959 if (isdigit(*fmt))
1960 field_width = skip_atoi(&fmt);
1961
1962 /* get conversion qualifier */
1963 qualifier = -1;
André Goddard Rosa08562cb2009-12-14 18:00:58 -08001964 if (*fmt == 'h' || TOLOWER(*fmt) == 'l' ||
1965 TOLOWER(*fmt) == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 qualifier = *fmt++;
1967 if (unlikely(qualifier == *fmt)) {
1968 if (qualifier == 'h') {
1969 qualifier = 'H';
1970 fmt++;
1971 } else if (qualifier == 'l') {
1972 qualifier = 'L';
1973 fmt++;
1974 }
1975 }
1976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977
1978 if (!*fmt || !*str)
1979 break;
1980
André Goddard Rosad4be1512009-12-14 18:00:59 -08001981 base = 10;
1982 is_sign = 0;
1983
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001984 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 case 'c':
1986 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001987 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 if (field_width == -1)
1989 field_width = 1;
1990 do {
1991 *s++ = *str++;
1992 } while (--field_width > 0 && *str);
1993 num++;
1994 }
1995 continue;
1996 case 's':
1997 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001998 char *s = (char *)va_arg(args, char *);
1999 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07002000 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002002 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
2004 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002005 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 *s = '\0';
2008 num++;
2009 }
2010 continue;
2011 case 'n':
2012 /* return number of characters read so far */
2013 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002014 int *i = (int *)va_arg(args, int*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 *i = str - buf;
2016 }
2017 continue;
2018 case 'o':
2019 base = 8;
2020 break;
2021 case 'x':
2022 case 'X':
2023 base = 16;
2024 break;
2025 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002026 base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 case 'd':
2028 is_sign = 1;
2029 case 'u':
2030 break;
2031 case '%':
2032 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002033 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 return num;
2035 continue;
2036 default:
2037 /* invalid format; stop here */
2038 return num;
2039 }
2040
2041 /* have some sort of integer conversion.
2042 * first, skip white space in buffer.
2043 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002044 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 digit = *str;
2047 if (is_sign && digit == '-')
2048 digit = *(str + 1);
2049
2050 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002051 || (base == 16 && !isxdigit(digit))
2052 || (base == 10 && !isdigit(digit))
2053 || (base == 8 && (!isdigit(digit) || digit > '7'))
2054 || (base == 0 && !isdigit(digit)))
2055 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002057 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 case 'H': /* that's 'hh' in format */
2059 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002060 signed char *s = (signed char *)va_arg(args, signed char *);
2061 *s = (signed char)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002063 unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
2064 *s = (unsigned char)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
2066 break;
2067 case 'h':
2068 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002069 short *s = (short *)va_arg(args, short *);
2070 *s = (short)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002072 unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
2073 *s = (unsigned short)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 }
2075 break;
2076 case 'l':
2077 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002078 long *l = (long *)va_arg(args, long *);
2079 *l = simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002081 unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
2082 *l = simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 }
2084 break;
2085 case 'L':
2086 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002087 long long *l = (long long *)va_arg(args, long long *);
2088 *l = simple_strtoll(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002090 unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
2091 *l = simple_strtoull(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
2093 break;
2094 case 'Z':
2095 case 'z':
2096 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002097 size_t *s = (size_t *)va_arg(args, size_t *);
2098 *s = (size_t)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 }
2100 break;
2101 default:
2102 if (is_sign) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002103 int *i = (int *)va_arg(args, int *);
2104 *i = (int)simple_strtol(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 } else {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002106 unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
2107 *i = (unsigned int)simple_strtoul(str, &next, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 }
2109 break;
2110 }
2111 num++;
2112
2113 if (!next)
2114 break;
2115 str = next;
2116 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07002117
2118 /*
2119 * Now we've come all the way through so either the input string or the
2120 * format ended. In the former case, there can be a %n at the current
2121 * position in the format that needs to be filled.
2122 */
2123 if (*fmt == '%' && *(fmt + 1) == 'n') {
2124 int *p = (int *)va_arg(args, int *);
2125 *p = str - buf;
2126 }
2127
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 return num;
2129}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130EXPORT_SYMBOL(vsscanf);
2131
2132/**
2133 * sscanf - Unformat a buffer into a list of arguments
2134 * @buf: input buffer
2135 * @fmt: formatting of buffer
2136 * @...: resulting arguments
2137 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002138int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
2140 va_list args;
2141 int i;
2142
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002143 va_start(args, fmt);
2144 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 return i;
2148}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149EXPORT_SYMBOL(sscanf);