blob: fab33a9c5318b297e5d4077b8bb3f3261312ee1e [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>
Paul Gortmaker8bc3bcc2011-11-16 21:29:17 -050020#include <linux/module.h> /* for KSYM_SYMBOL_LEN */
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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>
Jan Beulich53809752012-12-17 16:01:31 -080026#include <linux/math64.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070027#include <linux/uaccess.h>
Linus Torvalds332d2e72008-10-20 15:07:34 +110028#include <linux/ioport.h>
Joe Perches8a27f7c2009-08-17 12:29:44 +000029#include <net/addrconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Tim Schmielau4e57b682005-10-30 15:03:48 -080031#include <asm/page.h> /* for PAGE_SIZE */
James Bottomleydeac93d2008-09-03 20:43:36 -050032#include <asm/sections.h> /* for dereference_function_descriptor() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070034#include "kstrtox.h"
Harvey Harrisonaa46a632008-10-16 13:40:34 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * simple_strtoull - convert a string to an unsigned long long
38 * @cp: The start of the string
39 * @endp: A pointer to the end of the parsed string will be placed here
40 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080041 *
42 * This function is obsolete. Please use kstrtoull instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 */
Harvey Harrison22d27052008-10-16 13:40:35 -070044unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045{
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070046 unsigned long long result;
47 unsigned int rv;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Alexey Dobriyan1dff46d2011-10-31 17:12:28 -070049 cp = _parse_integer_fixup_radix(cp, &base);
50 rv = _parse_integer(cp, base, &result);
51 /* FIXME */
52 cp += (rv & ~KSTRTOX_OVERFLOW);
Harvey Harrisonaa46a632008-10-16 13:40:34 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if (endp)
55 *endp = (char *)cp;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -080056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 return result;
58}
Linus Torvalds1da177e2005-04-16 15:20:36 -070059EXPORT_SYMBOL(simple_strtoull);
60
61/**
André Goddard Rosa922ac252009-12-14 18:01:01 -080062 * simple_strtoul - convert a string to an unsigned long
63 * @cp: The start of the string
64 * @endp: A pointer to the end of the parsed string will be placed here
65 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080066 *
67 * This function is obsolete. Please use kstrtoul instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080068 */
69unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
70{
71 return simple_strtoull(cp, endp, base);
72}
73EXPORT_SYMBOL(simple_strtoul);
74
75/**
76 * simple_strtol - convert a string to a signed long
77 * @cp: The start of the string
78 * @endp: A pointer to the end of the parsed string will be placed here
79 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080080 *
81 * This function is obsolete. Please use kstrtol instead.
André Goddard Rosa922ac252009-12-14 18:01:01 -080082 */
83long simple_strtol(const char *cp, char **endp, unsigned int base)
84{
85 if (*cp == '-')
86 return -simple_strtoul(cp + 1, endp, base);
87
88 return simple_strtoul(cp, endp, base);
89}
90EXPORT_SYMBOL(simple_strtol);
91
92/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * simple_strtoll - convert a string to a signed long long
94 * @cp: The start of the string
95 * @endp: A pointer to the end of the parsed string will be placed here
96 * @base: The number base to use
Eldad Zack462e4712012-12-17 16:03:05 -080097 *
98 * This function is obsolete. Please use kstrtoll instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700100long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800102 if (*cp == '-')
Harvey Harrison22d27052008-10-16 13:40:35 -0700103 return -simple_strtoull(cp + 1, endp, base);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800104
Harvey Harrison22d27052008-10-16 13:40:35 -0700105 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
Hans Verkuil98d5ce02010-04-23 13:18:04 -0400107EXPORT_SYMBOL(simple_strtoll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Joe Perchescf3b4292010-05-24 14:33:16 -0700109static noinline_for_stack
110int skip_atoi(const char **s)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800112 int i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 while (isdigit(**s))
115 i = i*10 + *((*s)++) - '0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return i;
118}
119
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700120/* Decimal conversion is by far the most typical, and is used
121 * for /proc and /sys data. This directly impacts e.g. top performance
122 * with many processes running. We optimize it for speed
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700123 * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
124 * (with permission from the author, Douglas W. Jones).
125 */
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700126
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700127#if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
128/* Formats correctly any integer in [0, 999999999] */
Joe Perchescf3b4292010-05-24 14:33:16 -0700129static noinline_for_stack
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700130char *put_dec_full9(char *buf, unsigned q)
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700131{
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700132 unsigned r;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700133
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800134 /*
135 * Possible ways to approx. divide by 10
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700136 * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit)
137 * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul)
138 * (x * 0x6667) >> 18 x < 43699
139 * (x * 0x3334) >> 17 x < 16389
140 * (x * 0x199a) >> 16 x < 16389
141 * (x * 0x0ccd) >> 15 x < 16389
142 * (x * 0x0667) >> 14 x < 2739
143 * (x * 0x0334) >> 13 x < 1029
144 * (x * 0x019a) >> 12 x < 1029
145 * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386)
146 * (x * 0x0067) >> 10 x < 179
147 * (x * 0x0034) >> 9 x < 69 same
148 * (x * 0x001a) >> 8 x < 69 same
149 * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386)
150 * (x * 0x0007) >> 6 x < 19
151 * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800152 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700153 r = (q * (uint64_t)0x1999999a) >> 32;
154 *buf++ = (q - 10 * r) + '0'; /* 1 */
155 q = (r * (uint64_t)0x1999999a) >> 32;
156 *buf++ = (r - 10 * q) + '0'; /* 2 */
157 r = (q * (uint64_t)0x1999999a) >> 32;
158 *buf++ = (q - 10 * r) + '0'; /* 3 */
159 q = (r * (uint64_t)0x1999999a) >> 32;
160 *buf++ = (r - 10 * q) + '0'; /* 4 */
161 r = (q * (uint64_t)0x1999999a) >> 32;
162 *buf++ = (q - 10 * r) + '0'; /* 5 */
163 /* Now value is under 10000, can avoid 64-bit multiply */
164 q = (r * 0x199a) >> 16;
165 *buf++ = (r - 10 * q) + '0'; /* 6 */
166 r = (q * 0xcd) >> 11;
167 *buf++ = (q - 10 * r) + '0'; /* 7 */
168 q = (r * 0xcd) >> 11;
169 *buf++ = (r - 10 * q) + '0'; /* 8 */
170 *buf++ = q + '0'; /* 9 */
171 return buf;
172}
173#endif
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700174
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700175/* Similar to above but do not pad with zeros.
176 * Code can be easily arranged to print 9 digits too, but our callers
177 * always call put_dec_full9() instead when the number has 9 decimal digits.
178 */
179static noinline_for_stack
180char *put_dec_trunc8(char *buf, unsigned r)
181{
182 unsigned q;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700183
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700184 /* Copy of previous function's body with added early returns */
George Spelvincb239d02012-10-04 17:12:30 -0700185 while (r >= 10000) {
186 q = r + '0';
187 r = (r * (uint64_t)0x1999999a) >> 32;
188 *buf++ = q - 10*r;
189 }
190
George Spelvinf4000512012-10-04 17:12:32 -0700191 q = (r * 0x199a) >> 16; /* r <= 9999 */
192 *buf++ = (r - 10 * q) + '0';
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700193 if (q == 0)
194 return buf;
George Spelvinf4000512012-10-04 17:12:32 -0700195 r = (q * 0xcd) >> 11; /* q <= 999 */
196 *buf++ = (q - 10 * r) + '0';
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700197 if (r == 0)
198 return buf;
George Spelvinf4000512012-10-04 17:12:32 -0700199 q = (r * 0xcd) >> 11; /* r <= 99 */
200 *buf++ = (r - 10 * q) + '0';
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700201 if (q == 0)
202 return buf;
George Spelvinf4000512012-10-04 17:12:32 -0700203 *buf++ = q + '0'; /* q <= 9 */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700204 return buf;
205}
206
207/* There are two algorithms to print larger numbers.
208 * One is generic: divide by 1000000000 and repeatedly print
209 * groups of (up to) 9 digits. It's conceptually simple,
210 * but requires a (unsigned long long) / 1000000000 division.
211 *
212 * Second algorithm splits 64-bit unsigned long long into 16-bit chunks,
213 * manipulates them cleverly and generates groups of 4 decimal digits.
214 * It so happens that it does NOT require long long division.
215 *
216 * If long is > 32 bits, division of 64-bit values is relatively easy,
217 * and we will use the first algorithm.
218 * If long long is > 64 bits (strange architecture with VERY large long long),
219 * second algorithm can't be used, and we again use the first one.
220 *
221 * Else (if long is 32 bits and long long is 64 bits) we use second one.
222 */
223
224#if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
225
226/* First algorithm: generic */
227
228static
229char *put_dec(char *buf, unsigned long long n)
230{
231 if (n >= 100*1000*1000) {
232 while (n >= 1000*1000*1000)
233 buf = put_dec_full9(buf, do_div(n, 1000*1000*1000));
234 if (n >= 100*1000*1000)
235 return put_dec_full9(buf, n);
236 }
237 return put_dec_trunc8(buf, n);
238}
239
240#else
241
242/* Second algorithm: valid only for 64-bit long longs */
243
George Spelvine49317d2012-10-04 17:12:27 -0700244/* See comment in put_dec_full9 for choice of constants */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700245static noinline_for_stack
George Spelvin23591722012-10-04 17:12:29 -0700246void put_dec_full4(char *buf, unsigned q)
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700247{
248 unsigned r;
George Spelvine49317d2012-10-04 17:12:27 -0700249 r = (q * 0xccd) >> 15;
George Spelvin23591722012-10-04 17:12:29 -0700250 buf[0] = (q - 10 * r) + '0';
George Spelvine49317d2012-10-04 17:12:27 -0700251 q = (r * 0xcd) >> 11;
George Spelvin23591722012-10-04 17:12:29 -0700252 buf[1] = (r - 10 * q) + '0';
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700253 r = (q * 0xcd) >> 11;
George Spelvin23591722012-10-04 17:12:29 -0700254 buf[2] = (q - 10 * r) + '0';
255 buf[3] = r + '0';
256}
257
258/*
259 * Call put_dec_full4 on x % 10000, return x / 10000.
260 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
261 * holds for all x < 1,128,869,999. The largest value this
262 * helper will ever be asked to convert is 1,125,520,955.
263 * (d1 in the put_dec code, assuming n is all-ones).
264 */
265static
266unsigned put_dec_helper4(char *buf, unsigned x)
267{
268 uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
269
270 put_dec_full4(buf, x - q * 10000);
271 return q;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700272}
273
274/* Based on code by Douglas W. Jones found at
275 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
276 * (with permission from the author).
277 * Performs no 64-bit division and hence should be fast on 32-bit machines.
278 */
279static
280char *put_dec(char *buf, unsigned long long n)
281{
282 uint32_t d3, d2, d1, q, h;
283
284 if (n < 100*1000*1000)
285 return put_dec_trunc8(buf, n);
286
287 d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
288 h = (n >> 32);
289 d2 = (h ) & 0xffff;
290 d3 = (h >> 16); /* implicit "& 0xffff" */
291
292 q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
George Spelvin23591722012-10-04 17:12:29 -0700293 q = put_dec_helper4(buf, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700294
George Spelvin23591722012-10-04 17:12:29 -0700295 q += 7671 * d3 + 9496 * d2 + 6 * d1;
296 q = put_dec_helper4(buf+4, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700297
George Spelvin23591722012-10-04 17:12:29 -0700298 q += 4749 * d3 + 42 * d2;
299 q = put_dec_helper4(buf+8, q);
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700300
George Spelvin23591722012-10-04 17:12:29 -0700301 q += 281 * d3;
302 buf += 12;
303 if (q)
304 buf = put_dec_trunc8(buf, q);
305 else while (buf[-1] == '0')
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700306 --buf;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800307
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700308 return buf;
309}
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700310
311#endif
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700312
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700313/*
314 * Convert passed number to decimal string.
315 * Returns the length of string. On buffer overflow, returns 0.
316 *
317 * If speed is not important, use snprintf(). It's easy to read the code.
318 */
319int num_to_str(char *buf, int size, unsigned long long num)
320{
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700321 char tmp[sizeof(num) * 3];
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700322 int idx, len;
323
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700324 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
325 if (num <= 9) {
326 tmp[0] = '0' + num;
327 len = 1;
328 } else {
329 len = put_dec(tmp, num) - tmp;
330 }
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700331
332 if (len > size)
333 return 0;
334 for (idx = 0; idx < len; ++idx)
335 buf[idx] = tmp[len - idx - 1];
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700336 return len;
KAMEZAWA Hiroyuki1ac101a2012-03-23 15:02:54 -0700337}
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339#define ZEROPAD 1 /* pad with zero */
340#define SIGN 2 /* unsigned/signed long */
341#define PLUS 4 /* show plus */
342#define SPACE 8 /* space if plus */
343#define LEFT 16 /* left justified */
Bjorn Helgaasb89dc5d2010-03-05 10:47:31 -0700344#define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
345#define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100347enum format_type {
348 FORMAT_TYPE_NONE, /* Just a string part */
Vegard Nossumed681a92009-03-14 12:08:50 +0100349 FORMAT_TYPE_WIDTH,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100350 FORMAT_TYPE_PRECISION,
351 FORMAT_TYPE_CHAR,
352 FORMAT_TYPE_STR,
353 FORMAT_TYPE_PTR,
354 FORMAT_TYPE_PERCENT_CHAR,
355 FORMAT_TYPE_INVALID,
356 FORMAT_TYPE_LONG_LONG,
357 FORMAT_TYPE_ULONG,
358 FORMAT_TYPE_LONG,
Zhaoleia4e94ef2009-03-27 17:07:05 +0800359 FORMAT_TYPE_UBYTE,
360 FORMAT_TYPE_BYTE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100361 FORMAT_TYPE_USHORT,
362 FORMAT_TYPE_SHORT,
363 FORMAT_TYPE_UINT,
364 FORMAT_TYPE_INT,
365 FORMAT_TYPE_NRCHARS,
366 FORMAT_TYPE_SIZE_T,
367 FORMAT_TYPE_PTRDIFF
368};
369
370struct printf_spec {
Joe Perches4e310fd2010-04-14 09:27:40 -0700371 u8 type; /* format_type enum */
Joe Perchesef0658f2010-03-06 17:10:14 -0800372 u8 flags; /* flags to number() */
Joe Perches4e310fd2010-04-14 09:27:40 -0700373 u8 base; /* number base, 8, 10 or 16 only */
374 u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
375 s16 field_width; /* width of output field */
376 s16 precision; /* # of digits/chars */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100377};
378
Joe Perchescf3b4292010-05-24 14:33:16 -0700379static noinline_for_stack
380char *number(char *buf, char *end, unsigned long long num,
381 struct printf_spec spec)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100383 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
384 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
385
386 char tmp[66];
387 char sign;
388 char locase;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100389 int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 int i;
Pierre Carrier7c203422012-05-29 15:07:35 -0700391 bool is_zero = num == 0LL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100393 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
394 * produces same digits or (maybe lowercased) letters */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100395 locase = (spec.flags & SMALL);
396 if (spec.flags & LEFT)
397 spec.flags &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 sign = 0;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100399 if (spec.flags & SIGN) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800400 if ((signed long long)num < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 sign = '-';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800402 num = -(signed long long)num;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100403 spec.field_width--;
404 } else if (spec.flags & PLUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 sign = '+';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100406 spec.field_width--;
407 } else if (spec.flags & SPACE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 sign = ' ';
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100409 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700412 if (need_pfx) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100413 if (spec.base == 16)
Pierre Carrier7c203422012-05-29 15:07:35 -0700414 spec.field_width -= 2;
415 else if (!is_zero)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100416 spec.field_width--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700418
419 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 i = 0;
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700421 if (num < spec.base)
422 tmp[i++] = digits[num] | locase;
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700423 /* Generic code, for any base:
424 else do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100425 tmp[i++] = (digits[do_div(num,base)] | locase);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700426 } while (num != 0);
427 */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100428 else if (spec.base != 10) { /* 8 or 16 */
429 int mask = spec.base - 1;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700430 int shift = 3;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800431
432 if (spec.base == 16)
433 shift = 4;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700434 do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100435 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700436 num >>= shift;
437 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700438 } else { /* base 10 */
439 i = put_dec(tmp, num) - tmp;
440 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700441
442 /* printing 100 using %2d gives "100", not "00" */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100443 if (i > spec.precision)
444 spec.precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700445 /* leading space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100446 spec.field_width -= spec.precision;
447 if (!(spec.flags & (ZEROPAD+LEFT))) {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800448 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700449 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 *buf = ' ';
451 ++buf;
452 }
453 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700454 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700456 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 *buf = sign;
458 ++buf;
459 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700460 /* "0x" / "0" prefix */
461 if (need_pfx) {
Pierre Carrier7c203422012-05-29 15:07:35 -0700462 if (spec.base == 16 || !is_zero) {
463 if (buf < end)
464 *buf = '0';
465 ++buf;
466 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100467 if (spec.base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700468 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100469 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 ++buf;
471 }
472 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700473 /* zero or space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100474 if (!(spec.flags & LEFT)) {
475 char c = (spec.flags & ZEROPAD) ? '0' : ' ';
476 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700477 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 *buf = c;
479 ++buf;
480 }
481 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700482 /* hmm even more zero padding? */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100483 while (i <= --spec.precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700484 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 *buf = '0';
486 ++buf;
487 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700488 /* actual digits of result */
489 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700490 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 *buf = tmp[i];
492 ++buf;
493 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700494 /* trailing space padding */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100495 while (--spec.field_width >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700496 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 *buf = ' ';
498 ++buf;
499 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return buf;
502}
503
Joe Perchescf3b4292010-05-24 14:33:16 -0700504static noinline_for_stack
505char *string(char *buf, char *end, const char *s, struct printf_spec spec)
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700506{
507 int len, i;
508
509 if ((unsigned long)s < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -0800510 s = "(null)";
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700511
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100512 len = strnlen(s, spec.precision);
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700513
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100514 if (!(spec.flags & LEFT)) {
515 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700516 if (buf < end)
517 *buf = ' ';
518 ++buf;
519 }
520 }
521 for (i = 0; i < len; ++i) {
522 if (buf < end)
523 *buf = *s;
524 ++buf; ++s;
525 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100526 while (len < spec.field_width--) {
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700527 if (buf < end)
528 *buf = ' ';
529 ++buf;
530 }
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800531
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700532 return buf;
533}
534
Joe Perchescf3b4292010-05-24 14:33:16 -0700535static noinline_for_stack
536char *symbol_string(char *buf, char *end, void *ptr,
537 struct printf_spec spec, char ext)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700538{
539 unsigned long value = (unsigned long) ptr;
540#ifdef CONFIG_KALLSYMS
541 char sym[KSYM_SYMBOL_LEN];
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900542 if (ext == 'B')
543 sprint_backtrace(sym, value);
544 else if (ext != 'f' && ext != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200545 sprint_symbol(sym, value);
546 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700547 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800548
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100549 return string(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700550#else
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800551 spec.field_width = 2 * sizeof(void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100552 spec.flags |= SPECIAL | SMALL | ZEROPAD;
553 spec.base = 16;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800554
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100555 return number(buf, end, value, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700556#endif
557}
558
Joe Perchescf3b4292010-05-24 14:33:16 -0700559static noinline_for_stack
560char *resource_string(char *buf, char *end, struct resource *res,
561 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100562{
563#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600564#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100565#endif
566
567#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600568#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100569#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700570 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100571 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700572 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100573 .precision = -1,
574 .flags = SPECIAL | SMALL | ZEROPAD,
575 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700576 static const struct printf_spec mem_spec = {
577 .base = 16,
578 .field_width = MEM_RSRC_PRINTK_SIZE,
579 .precision = -1,
580 .flags = SPECIAL | SMALL | ZEROPAD,
581 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700582 static const struct printf_spec bus_spec = {
583 .base = 16,
584 .field_width = 2,
585 .precision = -1,
586 .flags = SMALL | ZEROPAD,
587 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700588 static const struct printf_spec dec_spec = {
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600589 .base = 10,
590 .precision = -1,
591 .flags = 0,
592 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700593 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600594 .field_width = -1,
595 .precision = 10,
596 .flags = LEFT,
597 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700598 static const struct printf_spec flag_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600599 .base = 16,
600 .precision = -1,
601 .flags = SPECIAL | SMALL,
602 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600603
604 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
605 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
606#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
607#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700608#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600609#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
610 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
611 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
612
Linus Torvalds332d2e72008-10-20 15:07:34 +1100613 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600614 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700615 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100616
617 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700618 if (res->flags & IORESOURCE_IO) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600619 p = string(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700620 specp = &io_spec;
621 } else if (res->flags & IORESOURCE_MEM) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600622 p = string(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700623 specp = &mem_spec;
624 } else if (res->flags & IORESOURCE_IRQ) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600625 p = string(p, pend, "irq ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700626 specp = &dec_spec;
627 } else if (res->flags & IORESOURCE_DMA) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600628 p = string(p, pend, "dma ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700629 specp = &dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700630 } else if (res->flags & IORESOURCE_BUS) {
631 p = string(p, pend, "bus ", str_spec);
632 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700633 } else {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600634 p = string(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700635 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600636 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600637 }
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700638 p = number(p, pend, res->start, *specp);
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600639 if (res->start != res->end) {
640 *p++ = '-';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700641 p = number(p, pend, res->end, *specp);
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600642 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600643 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600644 if (res->flags & IORESOURCE_MEM_64)
645 p = string(p, pend, " 64bit", str_spec);
646 if (res->flags & IORESOURCE_PREFETCH)
647 p = string(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700648 if (res->flags & IORESOURCE_WINDOW)
649 p = string(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600650 if (res->flags & IORESOURCE_DISABLED)
651 p = string(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600652 } else {
653 p = string(p, pend, " flags ", str_spec);
654 p = number(p, pend, res->flags, flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600655 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100656 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600657 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +1100658
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100659 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100660}
661
Joe Perchescf3b4292010-05-24 14:33:16 -0700662static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -0700663char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
664 const char *fmt)
665{
666 int i, len = 1; /* if we pass '%ph[CDN]', field witdh remains
667 negative value, fallback to the default */
668 char separator;
669
670 if (spec.field_width == 0)
671 /* nothing to print */
672 return buf;
673
674 if (ZERO_OR_NULL_PTR(addr))
675 /* NULL pointer */
676 return string(buf, end, NULL, spec);
677
678 switch (fmt[1]) {
679 case 'C':
680 separator = ':';
681 break;
682 case 'D':
683 separator = '-';
684 break;
685 case 'N':
686 separator = 0;
687 break;
688 default:
689 separator = ' ';
690 break;
691 }
692
693 if (spec.field_width > 0)
694 len = min_t(int, spec.field_width, 64);
695
696 for (i = 0; i < len && buf < end - 1; i++) {
697 buf = hex_byte_pack(buf, addr[i]);
698
699 if (buf < end && separator && i != len - 1)
700 *buf++ = separator;
701 }
702
703 return buf;
704}
705
706static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -0700707char *mac_address_string(char *buf, char *end, u8 *addr,
708 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700709{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000710 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700711 char *p = mac_addr;
712 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000713 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700714 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000715
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700716 switch (fmt[1]) {
717 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +0000718 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700719 break;
720
721 case 'R':
722 reversed = true;
723 /* fall through */
724
725 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +0000726 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700727 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000728 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700729
730 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700731 if (reversed)
732 p = hex_byte_pack(p, addr[5 - i]);
733 else
734 p = hex_byte_pack(p, addr[i]);
735
Joe Perches8a27f7c2009-08-17 12:29:44 +0000736 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +0000737 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700738 }
739 *p = '\0';
740
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100741 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700742}
743
Joe Perchescf3b4292010-05-24 14:33:16 -0700744static noinline_for_stack
745char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -0700746{
Harvey Harrison689afa72008-10-28 16:04:44 -0700747 int i;
Joe Perches0159f242010-01-13 20:23:30 -0800748 bool leading_zeros = (fmt[0] == 'i');
749 int index;
750 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -0700751
Joe Perches0159f242010-01-13 20:23:30 -0800752 switch (fmt[2]) {
753 case 'h':
754#ifdef __BIG_ENDIAN
755 index = 0;
756 step = 1;
757#else
758 index = 3;
759 step = -1;
760#endif
761 break;
762 case 'l':
763 index = 3;
764 step = -1;
765 break;
766 case 'n':
767 case 'b':
768 default:
769 index = 0;
770 step = 1;
771 break;
772 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000773 for (i = 0; i < 4; i++) {
774 char temp[3]; /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700775 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000776 if (leading_zeros) {
777 if (digits < 3)
778 *p++ = '0';
779 if (digits < 2)
780 *p++ = '0';
781 }
782 /* reverse the digits in the quad */
783 while (digits--)
784 *p++ = temp[digits];
785 if (i < 3)
786 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -0800787 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000788 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000789 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800790
Joe Perches8a27f7c2009-08-17 12:29:44 +0000791 return p;
792}
793
Joe Perchescf3b4292010-05-24 14:33:16 -0700794static noinline_for_stack
795char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000796{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800797 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000798 unsigned char zerolength[8];
799 int longest = 1;
800 int colonpos = -1;
801 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800802 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000803 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +0000804 bool useIPv4;
805 struct in6_addr in6;
806
807 memcpy(&in6, addr, sizeof(struct in6_addr));
808
809 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000810
811 memset(zerolength, 0, sizeof(zerolength));
812
813 if (useIPv4)
814 range = 6;
815 else
816 range = 8;
817
818 /* find position of longest 0 run */
819 for (i = 0; i < range; i++) {
820 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +0000821 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000822 break;
823 zerolength[i]++;
824 }
825 }
826 for (i = 0; i < range; i++) {
827 if (zerolength[i] > longest) {
828 longest = zerolength[i];
829 colonpos = i;
830 }
831 }
Joe Perches29cf5192011-06-09 11:23:37 -0700832 if (longest == 1) /* don't compress a single 0 */
833 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000834
835 /* emit address */
836 for (i = 0; i < range; i++) {
837 if (i == colonpos) {
838 if (needcolon || i == 0)
839 *p++ = ':';
840 *p++ = ':';
841 needcolon = false;
842 i += longest - 1;
843 continue;
844 }
845 if (needcolon) {
846 *p++ = ':';
847 needcolon = false;
848 }
849 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +0000850 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000851 hi = word >> 8;
852 lo = word & 0xff;
853 if (hi) {
854 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700855 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000856 else
857 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700858 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000859 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -0800860 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700861 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000862 else
863 *p++ = hex_asc_lo(lo);
864 needcolon = true;
865 }
866
867 if (useIPv4) {
868 if (needcolon)
869 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -0800870 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +0000871 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000872 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800873
Joe Perches8a27f7c2009-08-17 12:29:44 +0000874 return p;
875}
876
Joe Perchescf3b4292010-05-24 14:33:16 -0700877static noinline_for_stack
878char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000879{
880 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800881
Harvey Harrison689afa72008-10-28 16:04:44 -0700882 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700883 p = hex_byte_pack(p, *addr++);
884 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000885 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -0700886 *p++ = ':';
887 }
888 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800889
Joe Perches8a27f7c2009-08-17 12:29:44 +0000890 return p;
891}
892
Joe Perchescf3b4292010-05-24 14:33:16 -0700893static noinline_for_stack
894char *ip6_addr_string(char *buf, char *end, const u8 *addr,
895 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000896{
897 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
898
899 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +0000900 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000901 else
Joe Percheseb78cd22009-09-18 13:04:06 +0000902 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -0700903
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100904 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -0700905}
906
Joe Perchescf3b4292010-05-24 14:33:16 -0700907static noinline_for_stack
908char *ip4_addr_string(char *buf, char *end, const u8 *addr,
909 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -0700910{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000911 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -0700912
Joe Perches0159f242010-01-13 20:23:30 -0800913 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700914
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100915 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700916}
917
Joe Perchescf3b4292010-05-24 14:33:16 -0700918static noinline_for_stack
919char *uuid_string(char *buf, char *end, const u8 *addr,
920 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -0800921{
922 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
923 char *p = uuid;
924 int i;
925 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
926 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
927 const u8 *index = be;
928 bool uc = false;
929
930 switch (*(++fmt)) {
931 case 'L':
932 uc = true; /* fall-through */
933 case 'l':
934 index = le;
935 break;
936 case 'B':
937 uc = true;
938 break;
939 }
940
941 for (i = 0; i < 16; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700942 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -0800943 switch (i) {
944 case 3:
945 case 5:
946 case 7:
947 case 9:
948 *p++ = '-';
949 break;
950 }
951 }
952
953 *p = 0;
954
955 if (uc) {
956 p = uuid;
957 do {
958 *p = toupper(*p);
959 } while (*(++p));
960 }
961
962 return string(buf, end, uuid, spec);
963}
964
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000965static
966char *netdev_feature_string(char *buf, char *end, const u8 *addr,
967 struct printf_spec spec)
968{
969 spec.flags |= SPECIAL | SMALL | ZEROPAD;
970 if (spec.field_width == -1)
971 spec.field_width = 2 + 2 * sizeof(netdev_features_t);
972 spec.base = 16;
973
974 return number(buf, end, *(const netdev_features_t *)addr, spec);
975}
976
Ingo Molnar411f05f2011-05-12 23:00:28 +0200977int kptr_restrict __read_mostly;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -0800978
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700979/*
980 * Show a '%p' thing. A kernel extension is that the '%p' is followed
981 * by an extra set of alphanumeric characters that are extended format
982 * specifiers.
983 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100984 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700985 *
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200986 * - 'F' For symbolic function descriptor pointers with offset
987 * - 'f' For simple symbolic function names without offset
Steven Rostedt0efb4d22009-09-17 09:27:29 -0400988 * - 'S' For symbolic direct pointers with offset
989 * - 's' For symbolic direct pointers without offset
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900990 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600991 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
992 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700993 * - 'M' For a 6-byte MAC address, it prints the address in the
994 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +0000995 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +0000996 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -0800997 * with a dash-separated hex notation
Andy Shevchenko7c591542012-10-04 17:12:33 -0700998 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000999 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1000 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1001 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
1002 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1003 * IPv6 omits the colons (01020304...0f)
1004 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Joe Perches0159f242010-01-13 20:23:30 -08001005 * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
Joe Perches8a27f7c2009-08-17 12:29:44 +00001006 * - 'I6c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -07001007 * http://tools.ietf.org/html/rfc5952
Joe Perches9ac6e442009-12-14 18:01:09 -08001008 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1009 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1010 * Options for %pU are:
1011 * b big endian lower case hex (default)
1012 * B big endian UPPER case hex
1013 * l little endian lower case hex
1014 * L little endian UPPER case hex
1015 * big endian output byte order is:
1016 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1017 * little endian output byte order is:
1018 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00001019 * - 'V' For a struct va_format which contains a format string * and va_list *,
1020 * call vsnprintf(->format, *->va_list).
1021 * Implements a "recursive vsnprintf".
1022 * Do not use this feature without some mechanism to verify the
1023 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001024 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001025 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07001026 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1027 * a certain separator (' ' by default):
1028 * C colon
1029 * D dash
1030 * N no separator
1031 * The maximum supported length is 64 bytes of the input. Consider
1032 * to use print_hex_dump() for the larger input.
Joe Perches9ac6e442009-12-14 18:01:09 -08001033 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001034 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1035 * function pointers are really function descriptors, which contain a
1036 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001037 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001038static noinline_for_stack
1039char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1040 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001041{
Grant Likely725fe002012-05-31 16:26:08 -07001042 int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
1043
Kees Cook9f36e2c2011-03-22 16:34:22 -07001044 if (!ptr && *fmt != 'K') {
Joe Perches5e057982010-10-26 14:22:50 -07001045 /*
1046 * Print (null) with the same width as a pointer so it makes
1047 * tabular output look nice.
1048 */
1049 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001050 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001051 return string(buf, end, "(null)", spec);
Joe Perches5e057982010-10-26 14:22:50 -07001052 }
Linus Torvaldsd97106a2009-01-03 11:46:17 -08001053
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001054 switch (*fmt) {
1055 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001056 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001057 ptr = dereference_function_descriptor(ptr);
1058 /* Fallthrough */
1059 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08001060 case 's':
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001061 case 'B':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001062 return symbol_string(buf, end, ptr, spec, *fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001063 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001064 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001065 return resource_string(buf, end, ptr, spec, fmt);
Andy Shevchenko31550a12012-07-30 14:40:27 -07001066 case 'h':
1067 return hex_string(buf, end, ptr, spec, fmt);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001068 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1069 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001070 /* [mM]F (FDDI) */
1071 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00001072 return mac_address_string(buf, end, ptr, spec, fmt);
1073 case 'I': /* Formatted IP supported
1074 * 4: 1.2.3.4
1075 * 6: 0001:0203:...:0708
1076 * 6c: 1::708 or 1::1.2.3.4
1077 */
1078 case 'i': /* Contiguous:
1079 * 4: 001.002.003.004
1080 * 6: 000102...0f
1081 */
1082 switch (fmt[1]) {
1083 case '6':
1084 return ip6_addr_string(buf, end, ptr, spec, fmt);
1085 case '4':
1086 return ip4_addr_string(buf, end, ptr, spec, fmt);
1087 }
Harvey Harrison4aa99602008-10-29 12:49:58 -07001088 break;
Joe Perches9ac6e442009-12-14 18:01:09 -08001089 case 'U':
1090 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00001091 case 'V':
Jan Beulich5756b762012-03-05 16:49:24 +00001092 {
1093 va_list va;
1094
1095 va_copy(va, *((struct va_format *)ptr)->va);
1096 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1097 ((struct va_format *)ptr)->fmt, va);
1098 va_end(va);
1099 return buf;
1100 }
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001101 case 'K':
1102 /*
1103 * %pK cannot be used in IRQ context because its test
1104 * for CAP_SYSLOG would be meaningless.
1105 */
Dan Rosenberg3715c5302012-07-30 14:40:26 -07001106 if (kptr_restrict && (in_irq() || in_serving_softirq() ||
1107 in_nmi())) {
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001108 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001109 spec.field_width = default_width;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001110 return string(buf, end, "pK-error", spec);
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001111 }
Joe Perches26297602011-03-22 16:34:19 -07001112 if (!((kptr_restrict == 0) ||
1113 (kptr_restrict == 1 &&
1114 has_capability_noaudit(current, CAP_SYSLOG))))
1115 ptr = NULL;
1116 break;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001117 case 'N':
1118 switch (fmt[1]) {
1119 case 'F':
1120 return netdev_feature_string(buf, end, ptr, spec);
1121 }
1122 break;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001123 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001124 spec.flags |= SMALL;
1125 if (spec.field_width == -1) {
Grant Likely725fe002012-05-31 16:26:08 -07001126 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001127 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001128 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001129 spec.base = 16;
1130
1131 return number(buf, end, (unsigned long) ptr, spec);
1132}
1133
1134/*
1135 * Helper function to decode printf style format.
1136 * Each call decode a token from the format and return the
1137 * number of characters read (or likely the delta where it wants
1138 * to go on the next call).
1139 * The decoded token is returned through the parameters
1140 *
1141 * 'h', 'l', or 'L' for integer fields
1142 * 'z' support added 23/7/1999 S.H.
1143 * 'z' changed to 'Z' --davidm 1/25/99
1144 * 't' added for ptrdiff_t
1145 *
1146 * @fmt: the format string
1147 * @type of the token returned
1148 * @flags: various flags such as +, -, # tokens..
1149 * @field_width: overwritten width
1150 * @base: base of the number (octal, hex, ...)
1151 * @precision: precision of a number
1152 * @qualifier: qualifier of a number (long, size_t, ...)
1153 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001154static noinline_for_stack
1155int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001156{
1157 const char *start = fmt;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001158
1159 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01001160 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001161 if (spec->field_width < 0) {
1162 spec->field_width = -spec->field_width;
1163 spec->flags |= LEFT;
1164 }
1165 spec->type = FORMAT_TYPE_NONE;
1166 goto precision;
1167 }
1168
1169 /* we finished early by reading the precision */
1170 if (spec->type == FORMAT_TYPE_PRECISION) {
1171 if (spec->precision < 0)
1172 spec->precision = 0;
1173
1174 spec->type = FORMAT_TYPE_NONE;
1175 goto qualifier;
1176 }
1177
1178 /* By default */
1179 spec->type = FORMAT_TYPE_NONE;
1180
1181 for (; *fmt ; ++fmt) {
1182 if (*fmt == '%')
1183 break;
1184 }
1185
1186 /* Return the current non-format string */
1187 if (fmt != start || !*fmt)
1188 return fmt - start;
1189
1190 /* Process flags */
1191 spec->flags = 0;
1192
1193 while (1) { /* this also skips first '%' */
1194 bool found = true;
1195
1196 ++fmt;
1197
1198 switch (*fmt) {
1199 case '-': spec->flags |= LEFT; break;
1200 case '+': spec->flags |= PLUS; break;
1201 case ' ': spec->flags |= SPACE; break;
1202 case '#': spec->flags |= SPECIAL; break;
1203 case '0': spec->flags |= ZEROPAD; break;
1204 default: found = false;
1205 }
1206
1207 if (!found)
1208 break;
1209 }
1210
1211 /* get field width */
1212 spec->field_width = -1;
1213
1214 if (isdigit(*fmt))
1215 spec->field_width = skip_atoi(&fmt);
1216 else if (*fmt == '*') {
1217 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01001218 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001219 return ++fmt - start;
1220 }
1221
1222precision:
1223 /* get the precision */
1224 spec->precision = -1;
1225 if (*fmt == '.') {
1226 ++fmt;
1227 if (isdigit(*fmt)) {
1228 spec->precision = skip_atoi(&fmt);
1229 if (spec->precision < 0)
1230 spec->precision = 0;
1231 } else if (*fmt == '*') {
1232 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01001233 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001234 return ++fmt - start;
1235 }
1236 }
1237
1238qualifier:
1239 /* get the conversion qualifier */
1240 spec->qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001241 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1242 _tolower(*fmt) == 'z' || *fmt == 't') {
Zhaoleia4e94ef2009-03-27 17:07:05 +08001243 spec->qualifier = *fmt++;
1244 if (unlikely(spec->qualifier == *fmt)) {
1245 if (spec->qualifier == 'l') {
1246 spec->qualifier = 'L';
1247 ++fmt;
1248 } else if (spec->qualifier == 'h') {
1249 spec->qualifier = 'H';
1250 ++fmt;
1251 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001252 }
1253 }
1254
1255 /* default base */
1256 spec->base = 10;
1257 switch (*fmt) {
1258 case 'c':
1259 spec->type = FORMAT_TYPE_CHAR;
1260 return ++fmt - start;
1261
1262 case 's':
1263 spec->type = FORMAT_TYPE_STR;
1264 return ++fmt - start;
1265
1266 case 'p':
1267 spec->type = FORMAT_TYPE_PTR;
1268 return fmt - start;
1269 /* skip alnum */
1270
1271 case 'n':
1272 spec->type = FORMAT_TYPE_NRCHARS;
1273 return ++fmt - start;
1274
1275 case '%':
1276 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1277 return ++fmt - start;
1278
1279 /* integer number formats - set up the flags and "break" */
1280 case 'o':
1281 spec->base = 8;
1282 break;
1283
1284 case 'x':
1285 spec->flags |= SMALL;
1286
1287 case 'X':
1288 spec->base = 16;
1289 break;
1290
1291 case 'd':
1292 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001293 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001294 case 'u':
1295 break;
1296
1297 default:
1298 spec->type = FORMAT_TYPE_INVALID;
1299 return fmt - start;
1300 }
1301
1302 if (spec->qualifier == 'L')
1303 spec->type = FORMAT_TYPE_LONG_LONG;
1304 else if (spec->qualifier == 'l') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001305 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001306 spec->type = FORMAT_TYPE_LONG;
1307 else
1308 spec->type = FORMAT_TYPE_ULONG;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001309 } else if (_tolower(spec->qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001310 spec->type = FORMAT_TYPE_SIZE_T;
1311 } else if (spec->qualifier == 't') {
1312 spec->type = FORMAT_TYPE_PTRDIFF;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001313 } else if (spec->qualifier == 'H') {
1314 if (spec->flags & SIGN)
1315 spec->type = FORMAT_TYPE_BYTE;
1316 else
1317 spec->type = FORMAT_TYPE_UBYTE;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001318 } else if (spec->qualifier == 'h') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001319 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001320 spec->type = FORMAT_TYPE_SHORT;
1321 else
1322 spec->type = FORMAT_TYPE_USHORT;
1323 } else {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001324 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001325 spec->type = FORMAT_TYPE_INT;
1326 else
1327 spec->type = FORMAT_TYPE_UINT;
1328 }
1329
1330 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001331}
1332
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333/**
1334 * vsnprintf - Format a string and place it in a buffer
1335 * @buf: The buffer to place the result into
1336 * @size: The size of the buffer, including the trailing null space
1337 * @fmt: The format string to use
1338 * @args: Arguments for the format string
1339 *
Andi Kleen20036fd2008-10-15 22:02:02 -07001340 * This function follows C99 vsnprintf, but has some extensions:
Steven Rostedt91adcd22009-09-16 20:03:06 -04001341 * %pS output the name of a text symbol with offset
1342 * %ps output the name of a text symbol without offset
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001343 * %pF output the name of a function pointer with its offset
1344 * %pf output the name of a function pointer without its offset
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001345 * %pB output the name of a backtrace symbol with its offset
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001346 * %pR output the address range in a struct resource with decoded flags
1347 * %pr output the address range in a struct resource with raw flags
1348 * %pM output a 6-byte MAC address with colons
Andy Shevchenko7c591542012-10-04 17:12:33 -07001349 * %pMR output a 6-byte MAC address with colons in reversed order
1350 * %pMF output a 6-byte MAC address with dashes
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001351 * %pm output a 6-byte MAC address without colons
Andy Shevchenko7c591542012-10-04 17:12:33 -07001352 * %pmR output a 6-byte MAC address without colons in reversed order
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001353 * %pI4 print an IPv4 address without leading zeros
1354 * %pi4 print an IPv4 address with leading zeros
1355 * %pI6 print an IPv6 address with colons
1356 * %pi6 print an IPv6 address without colons
Jan Engelhardtf996f202011-07-14 18:48:56 +02001357 * %pI6c print an IPv6 address as specified by RFC 5952
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001358 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1359 * case.
Andy Shevchenko31550a12012-07-30 14:40:27 -07001360 * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
1361 * bytes of the input)
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001362 * %n is ignored
Andi Kleen20036fd2008-10-15 22:02:02 -07001363 *
Andrew Morton80f548e2012-07-30 14:40:25 -07001364 * ** Please update Documentation/printk-formats.txt when making changes **
1365 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 * The return value is the number of characters which would
1367 * be generated for the given input, excluding the trailing
1368 * '\0', as per ISO C99. If you want to have the exact
1369 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001370 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 * return is greater than or equal to @size, the resulting
1372 * string is truncated.
1373 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001374 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 */
1376int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1377{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001379 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001380 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001382 /* Reject out-of-range values early. Large positive sizes are
1383 used for unknown buffer sizes. */
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001384 if (WARN_ON_ONCE((int) size < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
1387 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001388 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001390 /* Make sure end is always >= buf */
1391 if (end < buf) {
1392 end = ((void *)-1);
1393 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 }
1395
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001396 while (*fmt) {
1397 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001398 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001399
1400 fmt += read;
1401
1402 switch (spec.type) {
1403 case FORMAT_TYPE_NONE: {
1404 int copy = read;
1405 if (str < end) {
1406 if (copy > end - str)
1407 copy = end - str;
1408 memcpy(str, old_fmt, copy);
1409 }
1410 str += read;
1411 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 }
1413
Vegard Nossumed681a92009-03-14 12:08:50 +01001414 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001415 spec.field_width = va_arg(args, int);
1416 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001418 case FORMAT_TYPE_PRECISION:
1419 spec.precision = va_arg(args, int);
1420 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421
André Goddard Rosad4be1512009-12-14 18:00:59 -08001422 case FORMAT_TYPE_CHAR: {
1423 char c;
1424
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001425 if (!(spec.flags & LEFT)) {
1426 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001427 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 *str = ' ';
1429 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001430
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001432 }
1433 c = (unsigned char) va_arg(args, int);
1434 if (str < end)
1435 *str = c;
1436 ++str;
1437 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001438 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001439 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001441 }
1442 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001443 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001445 case FORMAT_TYPE_STR:
1446 str = string(str, end, va_arg(args, char *), spec);
1447 break;
1448
1449 case FORMAT_TYPE_PTR:
1450 str = pointer(fmt+1, str, end, va_arg(args, void *),
1451 spec);
1452 while (isalnum(*fmt))
1453 fmt++;
1454 break;
1455
1456 case FORMAT_TYPE_PERCENT_CHAR:
1457 if (str < end)
1458 *str = '%';
1459 ++str;
1460 break;
1461
1462 case FORMAT_TYPE_INVALID:
1463 if (str < end)
1464 *str = '%';
1465 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001466 break;
1467
1468 case FORMAT_TYPE_NRCHARS: {
Joe Perchesef0658f2010-03-06 17:10:14 -08001469 u8 qualifier = spec.qualifier;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001470
1471 if (qualifier == 'l') {
1472 long *ip = va_arg(args, long *);
1473 *ip = (str - buf);
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001474 } else if (_tolower(qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001475 size_t *ip = va_arg(args, size_t *);
1476 *ip = (str - buf);
1477 } else {
1478 int *ip = va_arg(args, int *);
1479 *ip = (str - buf);
1480 }
1481 break;
1482 }
1483
1484 default:
1485 switch (spec.type) {
1486 case FORMAT_TYPE_LONG_LONG:
1487 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001489 case FORMAT_TYPE_ULONG:
1490 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001492 case FORMAT_TYPE_LONG:
1493 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001495 case FORMAT_TYPE_SIZE_T:
Jason Gunthorpeef124962012-12-17 15:59:58 -08001496 if (spec.flags & SIGN)
1497 num = va_arg(args, ssize_t);
1498 else
1499 num = va_arg(args, size_t);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001500 break;
1501 case FORMAT_TYPE_PTRDIFF:
1502 num = va_arg(args, ptrdiff_t);
1503 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001504 case FORMAT_TYPE_UBYTE:
1505 num = (unsigned char) va_arg(args, int);
1506 break;
1507 case FORMAT_TYPE_BYTE:
1508 num = (signed char) va_arg(args, int);
1509 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001510 case FORMAT_TYPE_USHORT:
1511 num = (unsigned short) va_arg(args, int);
1512 break;
1513 case FORMAT_TYPE_SHORT:
1514 num = (short) va_arg(args, int);
1515 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001516 case FORMAT_TYPE_INT:
1517 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001518 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001520 num = va_arg(args, unsigned int);
1521 }
1522
1523 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001526
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001527 if (size > 0) {
1528 if (str < end)
1529 *str = '\0';
1530 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07001531 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001532 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001533
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001534 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001536
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538EXPORT_SYMBOL(vsnprintf);
1539
1540/**
1541 * vscnprintf - Format a string and place it in a buffer
1542 * @buf: The buffer to place the result into
1543 * @size: The size of the buffer, including the trailing null space
1544 * @fmt: The format string to use
1545 * @args: Arguments for the format string
1546 *
1547 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08001548 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 * returns 0.
1550 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001551 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07001552 *
1553 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 */
1555int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1556{
1557 int i;
1558
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001559 i = vsnprintf(buf, size, fmt, args);
1560
Anton Arapovb921c692011-01-12 16:59:49 -08001561 if (likely(i < size))
1562 return i;
1563 if (size != 0)
1564 return size - 1;
1565 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567EXPORT_SYMBOL(vscnprintf);
1568
1569/**
1570 * snprintf - Format a string and place it in a buffer
1571 * @buf: The buffer to place the result into
1572 * @size: The size of the buffer, including the trailing null space
1573 * @fmt: The format string to use
1574 * @...: Arguments for the format string
1575 *
1576 * The return value is the number of characters which would be
1577 * generated for the given input, excluding the trailing null,
1578 * as per ISO C99. If the return is greater than or equal to
1579 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07001580 *
1581 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001583int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
1585 va_list args;
1586 int i;
1587
1588 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001589 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 return i;
1593}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594EXPORT_SYMBOL(snprintf);
1595
1596/**
1597 * scnprintf - Format a string and place it in a buffer
1598 * @buf: The buffer to place the result into
1599 * @size: The size of the buffer, including the trailing null space
1600 * @fmt: The format string to use
1601 * @...: Arguments for the format string
1602 *
1603 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07001604 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 */
1606
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001607int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608{
1609 va_list args;
1610 int i;
1611
1612 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08001613 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001615
Anton Arapovb921c692011-01-12 16:59:49 -08001616 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618EXPORT_SYMBOL(scnprintf);
1619
1620/**
1621 * vsprintf - Format a string and place it in a buffer
1622 * @buf: The buffer to place the result into
1623 * @fmt: The format string to use
1624 * @args: Arguments for the format string
1625 *
1626 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001627 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 * buffer overflows.
1629 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001630 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07001631 *
1632 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 */
1634int vsprintf(char *buf, const char *fmt, va_list args)
1635{
1636 return vsnprintf(buf, INT_MAX, fmt, args);
1637}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638EXPORT_SYMBOL(vsprintf);
1639
1640/**
1641 * sprintf - Format a string and place it in a buffer
1642 * @buf: The buffer to place the result into
1643 * @fmt: The format string to use
1644 * @...: Arguments for the format string
1645 *
1646 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001647 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07001649 *
1650 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001652int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
1654 va_list args;
1655 int i;
1656
1657 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001658 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 return i;
1662}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663EXPORT_SYMBOL(sprintf);
1664
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001665#ifdef CONFIG_BINARY_PRINTF
1666/*
1667 * bprintf service:
1668 * vbin_printf() - VA arguments to binary data
1669 * bstr_printf() - Binary data to text string
1670 */
1671
1672/**
1673 * vbin_printf - Parse a format string and place args' binary value in a buffer
1674 * @bin_buf: The buffer to place args' binary value
1675 * @size: The size of the buffer(by words(32bits), not characters)
1676 * @fmt: The format string to use
1677 * @args: Arguments for the format string
1678 *
1679 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1680 * is skiped.
1681 *
1682 * The return value is the number of words(32bits) which would be generated for
1683 * the given input.
1684 *
1685 * NOTE:
1686 * If the return value is greater than @size, the resulting bin_buf is NOT
1687 * valid for bstr_printf().
1688 */
1689int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1690{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001691 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001692 char *str, *end;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001693
1694 str = (char *)bin_buf;
1695 end = (char *)(bin_buf + size);
1696
1697#define save_arg(type) \
1698do { \
1699 if (sizeof(type) == 8) { \
1700 unsigned long long value; \
1701 str = PTR_ALIGN(str, sizeof(u32)); \
1702 value = va_arg(args, unsigned long long); \
1703 if (str + sizeof(type) <= end) { \
1704 *(u32 *)str = *(u32 *)&value; \
1705 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1706 } \
1707 } else { \
1708 unsigned long value; \
1709 str = PTR_ALIGN(str, sizeof(type)); \
1710 value = va_arg(args, int); \
1711 if (str + sizeof(type) <= end) \
1712 *(typeof(type) *)str = (type)value; \
1713 } \
1714 str += sizeof(type); \
1715} while (0)
1716
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001717 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08001718 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001719
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001720 fmt += read;
1721
1722 switch (spec.type) {
1723 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08001724 case FORMAT_TYPE_INVALID:
1725 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001726 break;
1727
Vegard Nossumed681a92009-03-14 12:08:50 +01001728 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001729 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001730 save_arg(int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001731 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001732
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001733 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001734 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001735 break;
1736
1737 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001738 const char *save_str = va_arg(args, char *);
1739 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08001740
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001741 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1742 || (unsigned long)save_str < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -08001743 save_str = "(null)";
André Goddard Rosa6c356632009-12-14 18:00:56 -08001744 len = strlen(save_str) + 1;
1745 if (str + len < end)
1746 memcpy(str, save_str, len);
1747 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001748 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001749 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001750
1751 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001752 save_arg(void *);
1753 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001754 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001755 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001756 break;
1757
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001758 case FORMAT_TYPE_NRCHARS: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001759 /* skip %n 's argument */
Joe Perchesef0658f2010-03-06 17:10:14 -08001760 u8 qualifier = spec.qualifier;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001761 void *skip_arg;
1762 if (qualifier == 'l')
1763 skip_arg = va_arg(args, long *);
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001764 else if (_tolower(qualifier) == 'z')
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001765 skip_arg = va_arg(args, size_t *);
1766 else
1767 skip_arg = va_arg(args, int *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001768 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001769 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001770
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001771 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001772 switch (spec.type) {
1773
1774 case FORMAT_TYPE_LONG_LONG:
1775 save_arg(long long);
1776 break;
1777 case FORMAT_TYPE_ULONG:
1778 case FORMAT_TYPE_LONG:
1779 save_arg(unsigned long);
1780 break;
1781 case FORMAT_TYPE_SIZE_T:
1782 save_arg(size_t);
1783 break;
1784 case FORMAT_TYPE_PTRDIFF:
1785 save_arg(ptrdiff_t);
1786 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001787 case FORMAT_TYPE_UBYTE:
1788 case FORMAT_TYPE_BYTE:
1789 save_arg(char);
1790 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001791 case FORMAT_TYPE_USHORT:
1792 case FORMAT_TYPE_SHORT:
1793 save_arg(short);
1794 break;
1795 default:
1796 save_arg(int);
1797 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001798 }
1799 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001800
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001801 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001802#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001803}
1804EXPORT_SYMBOL_GPL(vbin_printf);
1805
1806/**
1807 * bstr_printf - Format a string from binary arguments and place it in a buffer
1808 * @buf: The buffer to place the result into
1809 * @size: The size of the buffer, including the trailing null space
1810 * @fmt: The format string to use
1811 * @bin_buf: Binary arguments for the format string
1812 *
1813 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1814 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1815 * a binary buffer that generated by vbin_printf.
1816 *
1817 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001818 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001819 *
1820 * The return value is the number of characters which would
1821 * be generated for the given input, excluding the trailing
1822 * '\0', as per ISO C99. If you want to have the exact
1823 * number of characters written into @buf as return value
1824 * (not including the trailing '\0'), use vscnprintf(). If the
1825 * return is greater than or equal to @size, the resulting
1826 * string is truncated.
1827 */
1828int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1829{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001830 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08001831 char *str, *end;
1832 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001833
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001834 if (WARN_ON_ONCE((int) size < 0))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001835 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001836
1837 str = buf;
1838 end = buf + size;
1839
1840#define get_arg(type) \
1841({ \
1842 typeof(type) value; \
1843 if (sizeof(type) == 8) { \
1844 args = PTR_ALIGN(args, sizeof(u32)); \
1845 *(u32 *)&value = *(u32 *)args; \
1846 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1847 } else { \
1848 args = PTR_ALIGN(args, sizeof(type)); \
1849 value = *(typeof(type) *)args; \
1850 } \
1851 args += sizeof(type); \
1852 value; \
1853})
1854
1855 /* Make sure end is always >= buf */
1856 if (end < buf) {
1857 end = ((void *)-1);
1858 size = end - buf;
1859 }
1860
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001861 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001862 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001863 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001864
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001865 fmt += read;
1866
1867 switch (spec.type) {
1868 case FORMAT_TYPE_NONE: {
1869 int copy = read;
1870 if (str < end) {
1871 if (copy > end - str)
1872 copy = end - str;
1873 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001874 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001875 str += read;
1876 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001877 }
1878
Vegard Nossumed681a92009-03-14 12:08:50 +01001879 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001880 spec.field_width = get_arg(int);
1881 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001882
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001883 case FORMAT_TYPE_PRECISION:
1884 spec.precision = get_arg(int);
1885 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001886
André Goddard Rosad4be1512009-12-14 18:00:59 -08001887 case FORMAT_TYPE_CHAR: {
1888 char c;
1889
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001890 if (!(spec.flags & LEFT)) {
1891 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001892 if (str < end)
1893 *str = ' ';
1894 ++str;
1895 }
1896 }
1897 c = (unsigned char) get_arg(char);
1898 if (str < end)
1899 *str = c;
1900 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001901 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001902 if (str < end)
1903 *str = ' ';
1904 ++str;
1905 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001906 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001907 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001908
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001909 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001910 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001911 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001912 str = string(str, end, (char *)str_arg, spec);
1913 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001914 }
1915
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001916 case FORMAT_TYPE_PTR:
1917 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1918 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001919 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001920 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001921
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001922 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001923 case FORMAT_TYPE_INVALID:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001924 if (str < end)
1925 *str = '%';
1926 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001927 break;
1928
1929 case FORMAT_TYPE_NRCHARS:
1930 /* skip */
1931 break;
1932
André Goddard Rosad4be1512009-12-14 18:00:59 -08001933 default: {
1934 unsigned long long num;
1935
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001936 switch (spec.type) {
1937
1938 case FORMAT_TYPE_LONG_LONG:
1939 num = get_arg(long long);
1940 break;
1941 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001942 case FORMAT_TYPE_LONG:
1943 num = get_arg(unsigned long);
1944 break;
1945 case FORMAT_TYPE_SIZE_T:
1946 num = get_arg(size_t);
1947 break;
1948 case FORMAT_TYPE_PTRDIFF:
1949 num = get_arg(ptrdiff_t);
1950 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001951 case FORMAT_TYPE_UBYTE:
1952 num = get_arg(unsigned char);
1953 break;
1954 case FORMAT_TYPE_BYTE:
1955 num = get_arg(signed char);
1956 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001957 case FORMAT_TYPE_USHORT:
1958 num = get_arg(unsigned short);
1959 break;
1960 case FORMAT_TYPE_SHORT:
1961 num = get_arg(short);
1962 break;
1963 case FORMAT_TYPE_UINT:
1964 num = get_arg(unsigned int);
1965 break;
1966 default:
1967 num = get_arg(int);
1968 }
1969
1970 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08001971 } /* default: */
1972 } /* switch(spec.type) */
1973 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001974
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001975 if (size > 0) {
1976 if (str < end)
1977 *str = '\0';
1978 else
1979 end[-1] = '\0';
1980 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001981
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001982#undef get_arg
1983
1984 /* the trailing null byte doesn't count towards the total */
1985 return str - buf;
1986}
1987EXPORT_SYMBOL_GPL(bstr_printf);
1988
1989/**
1990 * bprintf - Parse a format string and place args' binary value in a buffer
1991 * @bin_buf: The buffer to place args' binary value
1992 * @size: The size of the buffer(by words(32bits), not characters)
1993 * @fmt: The format string to use
1994 * @...: Arguments for the format string
1995 *
1996 * The function returns the number of words(u32) written
1997 * into @bin_buf.
1998 */
1999int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
2000{
2001 va_list args;
2002 int ret;
2003
2004 va_start(args, fmt);
2005 ret = vbin_printf(bin_buf, size, fmt, args);
2006 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002007
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002008 return ret;
2009}
2010EXPORT_SYMBOL_GPL(bprintf);
2011
2012#endif /* CONFIG_BINARY_PRINTF */
2013
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014/**
2015 * vsscanf - Unformat a buffer into a list of arguments
2016 * @buf: input buffer
2017 * @fmt: format of buffer
2018 * @args: arguments
2019 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002020int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021{
2022 const char *str = buf;
2023 char *next;
2024 char digit;
2025 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08002026 u8 qualifier;
Jan Beulich53809752012-12-17 16:01:31 -08002027 unsigned int base;
2028 union {
2029 long long s;
2030 unsigned long long u;
2031 } val;
Joe Perchesef0658f2010-03-06 17:10:14 -08002032 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002033 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Jan Beulichda990752012-10-04 17:13:24 -07002035 while (*fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 /* skip any white space in format */
2037 /* white space in format matchs any amount of
2038 * white space, including none, in the input.
2039 */
2040 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08002041 fmt = skip_spaces(++fmt);
2042 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 }
2044
2045 /* anything that is not a conversion must match exactly */
2046 if (*fmt != '%' && *fmt) {
2047 if (*fmt++ != *str++)
2048 break;
2049 continue;
2050 }
2051
2052 if (!*fmt)
2053 break;
2054 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002055
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 /* skip this conversion.
2057 * advance both strings to next white space
2058 */
2059 if (*fmt == '*') {
Jan Beulichda990752012-10-04 17:13:24 -07002060 if (!*str)
2061 break;
Andy Spencer8fccae22009-10-01 15:44:27 -07002062 while (!isspace(*fmt) && *fmt != '%' && *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 fmt++;
2064 while (!isspace(*str) && *str)
2065 str++;
2066 continue;
2067 }
2068
2069 /* get field width */
2070 field_width = -1;
Jan Beulich53809752012-12-17 16:01:31 -08002071 if (isdigit(*fmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 field_width = skip_atoi(&fmt);
Jan Beulich53809752012-12-17 16:01:31 -08002073 if (field_width <= 0)
2074 break;
2075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076
2077 /* get conversion qualifier */
2078 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002079 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2080 _tolower(*fmt) == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 qualifier = *fmt++;
2082 if (unlikely(qualifier == *fmt)) {
2083 if (qualifier == 'h') {
2084 qualifier = 'H';
2085 fmt++;
2086 } else if (qualifier == 'l') {
2087 qualifier = 'L';
2088 fmt++;
2089 }
2090 }
2091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Jan Beulichda990752012-10-04 17:13:24 -07002093 if (!*fmt)
2094 break;
2095
2096 if (*fmt == 'n') {
2097 /* return number of characters read so far */
2098 *va_arg(args, int *) = str - buf;
2099 ++fmt;
2100 continue;
2101 }
2102
2103 if (!*str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 break;
2105
André Goddard Rosad4be1512009-12-14 18:00:59 -08002106 base = 10;
2107 is_sign = 0;
2108
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002109 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 case 'c':
2111 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002112 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 if (field_width == -1)
2114 field_width = 1;
2115 do {
2116 *s++ = *str++;
2117 } while (--field_width > 0 && *str);
2118 num++;
2119 }
2120 continue;
2121 case 's':
2122 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002123 char *s = (char *)va_arg(args, char *);
2124 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07002125 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002127 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002130 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 *s = '\0';
2133 num++;
2134 }
2135 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 case 'o':
2137 base = 8;
2138 break;
2139 case 'x':
2140 case 'X':
2141 base = 16;
2142 break;
2143 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002144 base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 case 'd':
2146 is_sign = 1;
2147 case 'u':
2148 break;
2149 case '%':
2150 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002151 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 return num;
2153 continue;
2154 default:
2155 /* invalid format; stop here */
2156 return num;
2157 }
2158
2159 /* have some sort of integer conversion.
2160 * first, skip white space in buffer.
2161 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002162 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
2164 digit = *str;
2165 if (is_sign && digit == '-')
2166 digit = *(str + 1);
2167
2168 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002169 || (base == 16 && !isxdigit(digit))
2170 || (base == 10 && !isdigit(digit))
2171 || (base == 8 && (!isdigit(digit) || digit > '7'))
2172 || (base == 0 && !isdigit(digit)))
2173 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174
Jan Beulich53809752012-12-17 16:01:31 -08002175 if (is_sign)
2176 val.s = qualifier != 'L' ?
2177 simple_strtol(str, &next, base) :
2178 simple_strtoll(str, &next, base);
2179 else
2180 val.u = qualifier != 'L' ?
2181 simple_strtoul(str, &next, base) :
2182 simple_strtoull(str, &next, base);
2183
2184 if (field_width > 0 && next - str > field_width) {
2185 if (base == 0)
2186 _parse_integer_fixup_radix(str, &base);
2187 while (next - str > field_width) {
2188 if (is_sign)
2189 val.s = div_s64(val.s, base);
2190 else
2191 val.u = div_u64(val.u, base);
2192 --next;
2193 }
2194 }
2195
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002196 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 case 'H': /* that's 'hh' in format */
Jan Beulich53809752012-12-17 16:01:31 -08002198 if (is_sign)
2199 *va_arg(args, signed char *) = val.s;
2200 else
2201 *va_arg(args, unsigned char *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 break;
2203 case 'h':
Jan Beulich53809752012-12-17 16:01:31 -08002204 if (is_sign)
2205 *va_arg(args, short *) = val.s;
2206 else
2207 *va_arg(args, unsigned short *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 break;
2209 case 'l':
Jan Beulich53809752012-12-17 16:01:31 -08002210 if (is_sign)
2211 *va_arg(args, long *) = val.s;
2212 else
2213 *va_arg(args, unsigned long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 break;
2215 case 'L':
Jan Beulich53809752012-12-17 16:01:31 -08002216 if (is_sign)
2217 *va_arg(args, long long *) = val.s;
2218 else
2219 *va_arg(args, unsigned long long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 break;
2221 case 'Z':
2222 case 'z':
Jan Beulich53809752012-12-17 16:01:31 -08002223 *va_arg(args, size_t *) = val.u;
2224 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 default:
Jan Beulich53809752012-12-17 16:01:31 -08002226 if (is_sign)
2227 *va_arg(args, int *) = val.s;
2228 else
2229 *va_arg(args, unsigned int *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 break;
2231 }
2232 num++;
2233
2234 if (!next)
2235 break;
2236 str = next;
2237 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07002238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 return num;
2240}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241EXPORT_SYMBOL(vsscanf);
2242
2243/**
2244 * sscanf - Unformat a buffer into a list of arguments
2245 * @buf: input buffer
2246 * @fmt: formatting of buffer
2247 * @...: resulting arguments
2248 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002249int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250{
2251 va_list args;
2252 int i;
2253
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002254 va_start(args, fmt);
2255 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002257
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 return i;
2259}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260EXPORT_SYMBOL(sscanf);