blob: e149c6416384a2a335783f0db538e030e90e6e69 [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,
Joe Perchesb0d33c22012-12-12 10:18:50 -0800537 struct printf_spec spec, const char *fmt)
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700538{
Joe Perchesb0d33c22012-12-12 10:18:50 -0800539 unsigned long value;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700540#ifdef CONFIG_KALLSYMS
541 char sym[KSYM_SYMBOL_LEN];
Joe Perchesb0d33c22012-12-12 10:18:50 -0800542#endif
543
544 if (fmt[1] == 'R')
545 ptr = __builtin_extract_return_addr(ptr);
546 value = (unsigned long)ptr;
547
548#ifdef CONFIG_KALLSYMS
549 if (*fmt == 'B')
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900550 sprint_backtrace(sym, value);
Joe Perchesb0d33c22012-12-12 10:18:50 -0800551 else if (*fmt != 'f' && *fmt != 's')
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200552 sprint_symbol(sym, value);
553 else
Stephen Boyd4796dd22012-05-29 15:07:33 -0700554 sprint_symbol_no_offset(sym, value);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800555
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100556 return string(buf, end, sym, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700557#else
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800558 spec.field_width = 2 * sizeof(void *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100559 spec.flags |= SPECIAL | SMALL | ZEROPAD;
560 spec.base = 16;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800561
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100562 return number(buf, end, value, spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700563#endif
564}
565
Joe Perchescf3b4292010-05-24 14:33:16 -0700566static noinline_for_stack
567char *resource_string(char *buf, char *end, struct resource *res,
568 struct printf_spec spec, const char *fmt)
Linus Torvalds332d2e72008-10-20 15:07:34 +1100569{
570#ifndef IO_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600571#define IO_RSRC_PRINTK_SIZE 6
Linus Torvalds332d2e72008-10-20 15:07:34 +1100572#endif
573
574#ifndef MEM_RSRC_PRINTK_SIZE
Bjorn Helgaas28405372009-10-06 15:33:29 -0600575#define MEM_RSRC_PRINTK_SIZE 10
Linus Torvalds332d2e72008-10-20 15:07:34 +1100576#endif
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700577 static const struct printf_spec io_spec = {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100578 .base = 16,
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700579 .field_width = IO_RSRC_PRINTK_SIZE,
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100580 .precision = -1,
581 .flags = SPECIAL | SMALL | ZEROPAD,
582 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700583 static const struct printf_spec mem_spec = {
584 .base = 16,
585 .field_width = MEM_RSRC_PRINTK_SIZE,
586 .precision = -1,
587 .flags = SPECIAL | SMALL | ZEROPAD,
588 };
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700589 static const struct printf_spec bus_spec = {
590 .base = 16,
591 .field_width = 2,
592 .precision = -1,
593 .flags = SMALL | ZEROPAD,
594 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700595 static const struct printf_spec dec_spec = {
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600596 .base = 10,
597 .precision = -1,
598 .flags = 0,
599 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700600 static const struct printf_spec str_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600601 .field_width = -1,
602 .precision = 10,
603 .flags = LEFT,
604 };
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700605 static const struct printf_spec flag_spec = {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600606 .base = 16,
607 .precision = -1,
608 .flags = SPECIAL | SMALL,
609 };
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600610
611 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
612 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
613#define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
614#define FLAG_BUF_SIZE (2 * sizeof(res->flags))
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700615#define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600616#define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
617 char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
618 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
619
Linus Torvalds332d2e72008-10-20 15:07:34 +1100620 char *p = sym, *pend = sym + sizeof(sym);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600621 int decode = (fmt[0] == 'R') ? 1 : 0;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700622 const struct printf_spec *specp;
Linus Torvalds332d2e72008-10-20 15:07:34 +1100623
624 *p++ = '[';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700625 if (res->flags & IORESOURCE_IO) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600626 p = string(p, pend, "io ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700627 specp = &io_spec;
628 } else if (res->flags & IORESOURCE_MEM) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600629 p = string(p, pend, "mem ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700630 specp = &mem_spec;
631 } else if (res->flags & IORESOURCE_IRQ) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600632 p = string(p, pend, "irq ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700633 specp = &dec_spec;
634 } else if (res->flags & IORESOURCE_DMA) {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600635 p = string(p, pend, "dma ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700636 specp = &dec_spec;
Bjorn Helgaas0f4050c2010-03-05 10:47:42 -0700637 } else if (res->flags & IORESOURCE_BUS) {
638 p = string(p, pend, "bus ", str_spec);
639 specp = &bus_spec;
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700640 } else {
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600641 p = string(p, pend, "??? ", str_spec);
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700642 specp = &mem_spec;
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600643 decode = 0;
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600644 }
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700645 p = number(p, pend, res->start, *specp);
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600646 if (res->start != res->end) {
647 *p++ = '-';
Bjorn Helgaas4da0b662010-03-05 10:47:37 -0700648 p = number(p, pend, res->end, *specp);
Bjorn Helgaasc91d3372009-10-06 15:33:34 -0600649 }
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600650 if (decode) {
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600651 if (res->flags & IORESOURCE_MEM_64)
652 p = string(p, pend, " 64bit", str_spec);
653 if (res->flags & IORESOURCE_PREFETCH)
654 p = string(p, pend, " pref", str_spec);
Bjorn Helgaas9d7cca02010-03-05 10:47:47 -0700655 if (res->flags & IORESOURCE_WINDOW)
656 p = string(p, pend, " window", str_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600657 if (res->flags & IORESOURCE_DISABLED)
658 p = string(p, pend, " disabled", str_spec);
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600659 } else {
660 p = string(p, pend, " flags ", str_spec);
661 p = number(p, pend, res->flags, flag_spec);
Bjorn Helgaasfd955412009-10-06 15:33:39 -0600662 }
Linus Torvalds332d2e72008-10-20 15:07:34 +1100663 *p++ = ']';
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600664 *p = '\0';
Linus Torvalds332d2e72008-10-20 15:07:34 +1100665
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100666 return string(buf, end, sym, spec);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100667}
668
Joe Perchescf3b4292010-05-24 14:33:16 -0700669static noinline_for_stack
Andy Shevchenko31550a12012-07-30 14:40:27 -0700670char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
671 const char *fmt)
672{
673 int i, len = 1; /* if we pass '%ph[CDN]', field witdh remains
674 negative value, fallback to the default */
675 char separator;
676
677 if (spec.field_width == 0)
678 /* nothing to print */
679 return buf;
680
681 if (ZERO_OR_NULL_PTR(addr))
682 /* NULL pointer */
683 return string(buf, end, NULL, spec);
684
685 switch (fmt[1]) {
686 case 'C':
687 separator = ':';
688 break;
689 case 'D':
690 separator = '-';
691 break;
692 case 'N':
693 separator = 0;
694 break;
695 default:
696 separator = ' ';
697 break;
698 }
699
700 if (spec.field_width > 0)
701 len = min_t(int, spec.field_width, 64);
702
703 for (i = 0; i < len && buf < end - 1; i++) {
704 buf = hex_byte_pack(buf, addr[i]);
705
706 if (buf < end && separator && i != len - 1)
707 *buf++ = separator;
708 }
709
710 return buf;
711}
712
713static noinline_for_stack
Joe Perchescf3b4292010-05-24 14:33:16 -0700714char *mac_address_string(char *buf, char *end, u8 *addr,
715 struct printf_spec spec, const char *fmt)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700716{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000717 char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700718 char *p = mac_addr;
719 int i;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000720 char separator;
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700721 bool reversed = false;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000722
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700723 switch (fmt[1]) {
724 case 'F':
Joe Perchesbc7259a2010-01-07 11:43:50 +0000725 separator = '-';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700726 break;
727
728 case 'R':
729 reversed = true;
730 /* fall through */
731
732 default:
Joe Perchesbc7259a2010-01-07 11:43:50 +0000733 separator = ':';
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700734 break;
Joe Perchesbc7259a2010-01-07 11:43:50 +0000735 }
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700736
737 for (i = 0; i < 6; i++) {
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -0700738 if (reversed)
739 p = hex_byte_pack(p, addr[5 - i]);
740 else
741 p = hex_byte_pack(p, addr[i]);
742
Joe Perches8a27f7c2009-08-17 12:29:44 +0000743 if (fmt[0] == 'M' && i != 5)
Joe Perchesbc7259a2010-01-07 11:43:50 +0000744 *p++ = separator;
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700745 }
746 *p = '\0';
747
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100748 return string(buf, end, mac_addr, spec);
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700749}
750
Joe Perchescf3b4292010-05-24 14:33:16 -0700751static noinline_for_stack
752char *ip4_string(char *p, const u8 *addr, const char *fmt)
Harvey Harrison689afa72008-10-28 16:04:44 -0700753{
Harvey Harrison689afa72008-10-28 16:04:44 -0700754 int i;
Joe Perches0159f242010-01-13 20:23:30 -0800755 bool leading_zeros = (fmt[0] == 'i');
756 int index;
757 int step;
Harvey Harrison689afa72008-10-28 16:04:44 -0700758
Joe Perches0159f242010-01-13 20:23:30 -0800759 switch (fmt[2]) {
760 case 'h':
761#ifdef __BIG_ENDIAN
762 index = 0;
763 step = 1;
764#else
765 index = 3;
766 step = -1;
767#endif
768 break;
769 case 'l':
770 index = 3;
771 step = -1;
772 break;
773 case 'n':
774 case 'b':
775 default:
776 index = 0;
777 step = 1;
778 break;
779 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000780 for (i = 0; i < 4; i++) {
781 char temp[3]; /* hold each IP quad in reverse order */
Denys Vlasenko133fd9f2012-05-31 16:26:08 -0700782 int digits = put_dec_trunc8(temp, addr[index]) - temp;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000783 if (leading_zeros) {
784 if (digits < 3)
785 *p++ = '0';
786 if (digits < 2)
787 *p++ = '0';
788 }
789 /* reverse the digits in the quad */
790 while (digits--)
791 *p++ = temp[digits];
792 if (i < 3)
793 *p++ = '.';
Joe Perches0159f242010-01-13 20:23:30 -0800794 index += step;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000795 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000796 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800797
Joe Perches8a27f7c2009-08-17 12:29:44 +0000798 return p;
799}
800
Joe Perchescf3b4292010-05-24 14:33:16 -0700801static noinline_for_stack
802char *ip6_compressed_string(char *p, const char *addr)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000803{
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800804 int i, j, range;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000805 unsigned char zerolength[8];
806 int longest = 1;
807 int colonpos = -1;
808 u16 word;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800809 u8 hi, lo;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000810 bool needcolon = false;
Joe Percheseb78cd22009-09-18 13:04:06 +0000811 bool useIPv4;
812 struct in6_addr in6;
813
814 memcpy(&in6, addr, sizeof(struct in6_addr));
815
816 useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000817
818 memset(zerolength, 0, sizeof(zerolength));
819
820 if (useIPv4)
821 range = 6;
822 else
823 range = 8;
824
825 /* find position of longest 0 run */
826 for (i = 0; i < range; i++) {
827 for (j = i; j < range; j++) {
Joe Percheseb78cd22009-09-18 13:04:06 +0000828 if (in6.s6_addr16[j] != 0)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000829 break;
830 zerolength[i]++;
831 }
832 }
833 for (i = 0; i < range; i++) {
834 if (zerolength[i] > longest) {
835 longest = zerolength[i];
836 colonpos = i;
837 }
838 }
Joe Perches29cf5192011-06-09 11:23:37 -0700839 if (longest == 1) /* don't compress a single 0 */
840 colonpos = -1;
Joe Perches8a27f7c2009-08-17 12:29:44 +0000841
842 /* emit address */
843 for (i = 0; i < range; i++) {
844 if (i == colonpos) {
845 if (needcolon || i == 0)
846 *p++ = ':';
847 *p++ = ':';
848 needcolon = false;
849 i += longest - 1;
850 continue;
851 }
852 if (needcolon) {
853 *p++ = ':';
854 needcolon = false;
855 }
856 /* hex u16 without leading 0s */
Joe Percheseb78cd22009-09-18 13:04:06 +0000857 word = ntohs(in6.s6_addr16[i]);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000858 hi = word >> 8;
859 lo = word & 0xff;
860 if (hi) {
861 if (hi > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700862 p = hex_byte_pack(p, hi);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000863 else
864 *p++ = hex_asc_lo(hi);
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700865 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000866 }
André Goddard Rosab5ff9922009-12-14 18:00:59 -0800867 else if (lo > 0x0f)
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700868 p = hex_byte_pack(p, lo);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000869 else
870 *p++ = hex_asc_lo(lo);
871 needcolon = true;
872 }
873
874 if (useIPv4) {
875 if (needcolon)
876 *p++ = ':';
Joe Perches0159f242010-01-13 20:23:30 -0800877 p = ip4_string(p, &in6.s6_addr[12], "I4");
Joe Perches8a27f7c2009-08-17 12:29:44 +0000878 }
Joe Perches8a27f7c2009-08-17 12:29:44 +0000879 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800880
Joe Perches8a27f7c2009-08-17 12:29:44 +0000881 return p;
882}
883
Joe Perchescf3b4292010-05-24 14:33:16 -0700884static noinline_for_stack
885char *ip6_string(char *p, const char *addr, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000886{
887 int i;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800888
Harvey Harrison689afa72008-10-28 16:04:44 -0700889 for (i = 0; i < 8; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700890 p = hex_byte_pack(p, *addr++);
891 p = hex_byte_pack(p, *addr++);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000892 if (fmt[0] == 'I' && i != 7)
Harvey Harrison689afa72008-10-28 16:04:44 -0700893 *p++ = ':';
894 }
895 *p = '\0';
André Goddard Rosa7b9186f2009-12-14 18:00:57 -0800896
Joe Perches8a27f7c2009-08-17 12:29:44 +0000897 return p;
898}
899
Joe Perchescf3b4292010-05-24 14:33:16 -0700900static noinline_for_stack
901char *ip6_addr_string(char *buf, char *end, const u8 *addr,
902 struct printf_spec spec, const char *fmt)
Joe Perches8a27f7c2009-08-17 12:29:44 +0000903{
904 char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
905
906 if (fmt[0] == 'I' && fmt[2] == 'c')
Joe Percheseb78cd22009-09-18 13:04:06 +0000907 ip6_compressed_string(ip6_addr, addr);
Joe Perches8a27f7c2009-08-17 12:29:44 +0000908 else
Joe Percheseb78cd22009-09-18 13:04:06 +0000909 ip6_string(ip6_addr, addr, fmt);
Harvey Harrison689afa72008-10-28 16:04:44 -0700910
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100911 return string(buf, end, ip6_addr, spec);
Harvey Harrison689afa72008-10-28 16:04:44 -0700912}
913
Joe Perchescf3b4292010-05-24 14:33:16 -0700914static noinline_for_stack
915char *ip4_addr_string(char *buf, char *end, const u8 *addr,
916 struct printf_spec spec, const char *fmt)
Harvey Harrison4aa99602008-10-29 12:49:58 -0700917{
Joe Perches8a27f7c2009-08-17 12:29:44 +0000918 char ip4_addr[sizeof("255.255.255.255")];
Harvey Harrison4aa99602008-10-29 12:49:58 -0700919
Joe Perches0159f242010-01-13 20:23:30 -0800920 ip4_string(ip4_addr, addr, fmt);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700921
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +0100922 return string(buf, end, ip4_addr, spec);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700923}
924
Joe Perchescf3b4292010-05-24 14:33:16 -0700925static noinline_for_stack
926char *uuid_string(char *buf, char *end, const u8 *addr,
927 struct printf_spec spec, const char *fmt)
Joe Perches9ac6e442009-12-14 18:01:09 -0800928{
929 char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
930 char *p = uuid;
931 int i;
932 static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
933 static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
934 const u8 *index = be;
935 bool uc = false;
936
937 switch (*(++fmt)) {
938 case 'L':
939 uc = true; /* fall-through */
940 case 'l':
941 index = le;
942 break;
943 case 'B':
944 uc = true;
945 break;
946 }
947
948 for (i = 0; i < 16; i++) {
Andy Shevchenko55036ba2011-10-31 17:12:41 -0700949 p = hex_byte_pack(p, addr[index[i]]);
Joe Perches9ac6e442009-12-14 18:01:09 -0800950 switch (i) {
951 case 3:
952 case 5:
953 case 7:
954 case 9:
955 *p++ = '-';
956 break;
957 }
958 }
959
960 *p = 0;
961
962 if (uc) {
963 p = uuid;
964 do {
965 *p = toupper(*p);
966 } while (*(++p));
967 }
968
969 return string(buf, end, uuid, spec);
970}
971
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000972static
973char *netdev_feature_string(char *buf, char *end, const u8 *addr,
974 struct printf_spec spec)
975{
976 spec.flags |= SPECIAL | SMALL | ZEROPAD;
977 if (spec.field_width == -1)
978 spec.field_width = 2 + 2 * sizeof(netdev_features_t);
979 spec.base = 16;
980
981 return number(buf, end, *(const netdev_features_t *)addr, spec);
982}
983
Ingo Molnar411f05f2011-05-12 23:00:28 +0200984int kptr_restrict __read_mostly;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -0800985
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700986/*
987 * Show a '%p' thing. A kernel extension is that the '%p' is followed
988 * by an extra set of alphanumeric characters that are extended format
989 * specifiers.
990 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100991 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700992 *
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +0200993 * - 'F' For symbolic function descriptor pointers with offset
994 * - 'f' For simple symbolic function names without offset
Steven Rostedt0efb4d22009-09-17 09:27:29 -0400995 * - 'S' For symbolic direct pointers with offset
996 * - 's' For symbolic direct pointers without offset
Joe Perchesb0d33c22012-12-12 10:18:50 -0800997 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
Namhyung Kim0f77a8d2011-03-24 11:42:29 +0900998 * - 'B' For backtraced symbolic direct pointers with offset
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -0600999 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1000 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
Harvey Harrisondd45c9c2008-10-27 15:47:12 -07001001 * - 'M' For a 6-byte MAC address, it prints the address in the
1002 * usual colon-separated hex notation
Joe Perches8a27f7c2009-08-17 12:29:44 +00001003 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
Joe Perchesbc7259a2010-01-07 11:43:50 +00001004 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
Joe Perchesc8e00062010-01-11 00:44:14 -08001005 * with a dash-separated hex notation
Andy Shevchenko7c591542012-10-04 17:12:33 -07001006 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
Joe Perches8a27f7c2009-08-17 12:29:44 +00001007 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1008 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1009 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
1010 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1011 * IPv6 omits the colons (01020304...0f)
1012 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
Joe Perches0159f242010-01-13 20:23:30 -08001013 * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order
Joe Perches8a27f7c2009-08-17 12:29:44 +00001014 * - 'I6c' for IPv6 addresses printed as specified by
Joe Perches29cf5192011-06-09 11:23:37 -07001015 * http://tools.ietf.org/html/rfc5952
Joe Perches9ac6e442009-12-14 18:01:09 -08001016 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1017 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1018 * Options for %pU are:
1019 * b big endian lower case hex (default)
1020 * B big endian UPPER case hex
1021 * l little endian lower case hex
1022 * L little endian UPPER case hex
1023 * big endian output byte order is:
1024 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1025 * little endian output byte order is:
1026 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
Joe Perches7db6f5f2010-06-27 01:02:33 +00001027 * - 'V' For a struct va_format which contains a format string * and va_list *,
1028 * call vsnprintf(->format, *->va_list).
1029 * Implements a "recursive vsnprintf".
1030 * Do not use this feature without some mechanism to verify the
1031 * correctness of the format string and va_list arguments.
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001032 * - 'K' For a kernel pointer that should be hidden from unprivileged users
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001033 * - 'NF' For a netdev_features_t
Andy Shevchenko31550a12012-07-30 14:40:27 -07001034 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1035 * a certain separator (' ' by default):
1036 * C colon
1037 * D dash
1038 * N no separator
1039 * The maximum supported length is 64 bytes of the input. Consider
1040 * to use print_hex_dump() for the larger input.
Stepan Moskovchenko7d799212013-02-21 16:43:09 -08001041 * - 'a' For a phys_addr_t type and its derivative types (passed by reference)
Joe Perches9ac6e442009-12-14 18:01:09 -08001042 *
Linus Torvalds332d2e72008-10-20 15:07:34 +11001043 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1044 * function pointers are really function descriptors, which contain a
1045 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -07001046 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001047static noinline_for_stack
1048char *pointer(const char *fmt, char *buf, char *end, void *ptr,
1049 struct printf_spec spec)
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001050{
Grant Likely725fe002012-05-31 16:26:08 -07001051 int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
1052
Kees Cook9f36e2c2011-03-22 16:34:22 -07001053 if (!ptr && *fmt != 'K') {
Joe Perches5e057982010-10-26 14:22:50 -07001054 /*
1055 * Print (null) with the same width as a pointer so it makes
1056 * tabular output look nice.
1057 */
1058 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001059 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001060 return string(buf, end, "(null)", spec);
Joe Perches5e057982010-10-26 14:22:50 -07001061 }
Linus Torvaldsd97106a2009-01-03 11:46:17 -08001062
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001063 switch (*fmt) {
1064 case 'F':
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001065 case 'f':
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001066 ptr = dereference_function_descriptor(ptr);
1067 /* Fallthrough */
1068 case 'S':
Joe Perches9ac6e442009-12-14 18:01:09 -08001069 case 's':
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001070 case 'B':
Joe Perchesb0d33c22012-12-12 10:18:50 -08001071 return symbol_string(buf, end, ptr, spec, fmt);
Linus Torvalds332d2e72008-10-20 15:07:34 +11001072 case 'R':
Bjorn Helgaasc7dabef2009-10-27 13:26:47 -06001073 case 'r':
Bjorn Helgaasfd955412009-10-06 15:33:39 -06001074 return resource_string(buf, end, ptr, spec, fmt);
Andy Shevchenko31550a12012-07-30 14:40:27 -07001075 case 'h':
1076 return hex_string(buf, end, ptr, spec, fmt);
Joe Perches8a27f7c2009-08-17 12:29:44 +00001077 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1078 case 'm': /* Contiguous: 000102030405 */
Andrei Emeltchenko76597ff92012-07-30 14:40:23 -07001079 /* [mM]F (FDDI) */
1080 /* [mM]R (Reverse order; Bluetooth) */
Joe Perches8a27f7c2009-08-17 12:29:44 +00001081 return mac_address_string(buf, end, ptr, spec, fmt);
1082 case 'I': /* Formatted IP supported
1083 * 4: 1.2.3.4
1084 * 6: 0001:0203:...:0708
1085 * 6c: 1::708 or 1::1.2.3.4
1086 */
1087 case 'i': /* Contiguous:
1088 * 4: 001.002.003.004
1089 * 6: 000102...0f
1090 */
1091 switch (fmt[1]) {
1092 case '6':
1093 return ip6_addr_string(buf, end, ptr, spec, fmt);
1094 case '4':
1095 return ip4_addr_string(buf, end, ptr, spec, fmt);
1096 }
Harvey Harrison4aa99602008-10-29 12:49:58 -07001097 break;
Joe Perches9ac6e442009-12-14 18:01:09 -08001098 case 'U':
1099 return uuid_string(buf, end, ptr, spec, fmt);
Joe Perches7db6f5f2010-06-27 01:02:33 +00001100 case 'V':
Jan Beulich5756b762012-03-05 16:49:24 +00001101 {
1102 va_list va;
1103
1104 va_copy(va, *((struct va_format *)ptr)->va);
1105 buf += vsnprintf(buf, end > buf ? end - buf : 0,
1106 ((struct va_format *)ptr)->fmt, va);
1107 va_end(va);
1108 return buf;
1109 }
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001110 case 'K':
1111 /*
1112 * %pK cannot be used in IRQ context because its test
1113 * for CAP_SYSLOG would be meaningless.
1114 */
Dan Rosenberg3715c5302012-07-30 14:40:26 -07001115 if (kptr_restrict && (in_irq() || in_serving_softirq() ||
1116 in_nmi())) {
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001117 if (spec.field_width == -1)
Grant Likely725fe002012-05-31 16:26:08 -07001118 spec.field_width = default_width;
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001119 return string(buf, end, "pK-error", spec);
Dan Rosenberg455cd5a2011-01-12 16:59:41 -08001120 }
Joe Perches26297602011-03-22 16:34:19 -07001121 if (!((kptr_restrict == 0) ||
1122 (kptr_restrict == 1 &&
1123 has_capability_noaudit(current, CAP_SYSLOG))))
1124 ptr = NULL;
1125 break;
Michał Mirosławc8f44af2011-11-15 15:29:55 +00001126 case 'N':
1127 switch (fmt[1]) {
1128 case 'F':
1129 return netdev_feature_string(buf, end, ptr, spec);
1130 }
1131 break;
Stepan Moskovchenko7d799212013-02-21 16:43:09 -08001132 case 'a':
1133 spec.flags |= SPECIAL | SMALL | ZEROPAD;
1134 spec.field_width = sizeof(phys_addr_t) * 2 + 2;
1135 spec.base = 16;
1136 return number(buf, end,
1137 (unsigned long long) *((phys_addr_t *)ptr), spec);
Linus Torvalds0fe1ef22008-07-06 16:43:12 -07001138 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001139 spec.flags |= SMALL;
1140 if (spec.field_width == -1) {
Grant Likely725fe002012-05-31 16:26:08 -07001141 spec.field_width = default_width;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001142 spec.flags |= ZEROPAD;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001143 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001144 spec.base = 16;
1145
1146 return number(buf, end, (unsigned long) ptr, spec);
1147}
1148
1149/*
1150 * Helper function to decode printf style format.
1151 * Each call decode a token from the format and return the
1152 * number of characters read (or likely the delta where it wants
1153 * to go on the next call).
1154 * The decoded token is returned through the parameters
1155 *
1156 * 'h', 'l', or 'L' for integer fields
1157 * 'z' support added 23/7/1999 S.H.
1158 * 'z' changed to 'Z' --davidm 1/25/99
1159 * 't' added for ptrdiff_t
1160 *
1161 * @fmt: the format string
1162 * @type of the token returned
1163 * @flags: various flags such as +, -, # tokens..
1164 * @field_width: overwritten width
1165 * @base: base of the number (octal, hex, ...)
1166 * @precision: precision of a number
1167 * @qualifier: qualifier of a number (long, size_t, ...)
1168 */
Joe Perchescf3b4292010-05-24 14:33:16 -07001169static noinline_for_stack
1170int format_decode(const char *fmt, struct printf_spec *spec)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001171{
1172 const char *start = fmt;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001173
1174 /* we finished early by reading the field width */
Vegard Nossumed681a92009-03-14 12:08:50 +01001175 if (spec->type == FORMAT_TYPE_WIDTH) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001176 if (spec->field_width < 0) {
1177 spec->field_width = -spec->field_width;
1178 spec->flags |= LEFT;
1179 }
1180 spec->type = FORMAT_TYPE_NONE;
1181 goto precision;
1182 }
1183
1184 /* we finished early by reading the precision */
1185 if (spec->type == FORMAT_TYPE_PRECISION) {
1186 if (spec->precision < 0)
1187 spec->precision = 0;
1188
1189 spec->type = FORMAT_TYPE_NONE;
1190 goto qualifier;
1191 }
1192
1193 /* By default */
1194 spec->type = FORMAT_TYPE_NONE;
1195
1196 for (; *fmt ; ++fmt) {
1197 if (*fmt == '%')
1198 break;
1199 }
1200
1201 /* Return the current non-format string */
1202 if (fmt != start || !*fmt)
1203 return fmt - start;
1204
1205 /* Process flags */
1206 spec->flags = 0;
1207
1208 while (1) { /* this also skips first '%' */
1209 bool found = true;
1210
1211 ++fmt;
1212
1213 switch (*fmt) {
1214 case '-': spec->flags |= LEFT; break;
1215 case '+': spec->flags |= PLUS; break;
1216 case ' ': spec->flags |= SPACE; break;
1217 case '#': spec->flags |= SPECIAL; break;
1218 case '0': spec->flags |= ZEROPAD; break;
1219 default: found = false;
1220 }
1221
1222 if (!found)
1223 break;
1224 }
1225
1226 /* get field width */
1227 spec->field_width = -1;
1228
1229 if (isdigit(*fmt))
1230 spec->field_width = skip_atoi(&fmt);
1231 else if (*fmt == '*') {
1232 /* it's the next argument */
Vegard Nossumed681a92009-03-14 12:08:50 +01001233 spec->type = FORMAT_TYPE_WIDTH;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001234 return ++fmt - start;
1235 }
1236
1237precision:
1238 /* get the precision */
1239 spec->precision = -1;
1240 if (*fmt == '.') {
1241 ++fmt;
1242 if (isdigit(*fmt)) {
1243 spec->precision = skip_atoi(&fmt);
1244 if (spec->precision < 0)
1245 spec->precision = 0;
1246 } else if (*fmt == '*') {
1247 /* it's the next argument */
Vegard Nossumadf26f82009-03-14 12:08:50 +01001248 spec->type = FORMAT_TYPE_PRECISION;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001249 return ++fmt - start;
1250 }
1251 }
1252
1253qualifier:
1254 /* get the conversion qualifier */
1255 spec->qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001256 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
1257 _tolower(*fmt) == 'z' || *fmt == 't') {
Zhaoleia4e94ef2009-03-27 17:07:05 +08001258 spec->qualifier = *fmt++;
1259 if (unlikely(spec->qualifier == *fmt)) {
1260 if (spec->qualifier == 'l') {
1261 spec->qualifier = 'L';
1262 ++fmt;
1263 } else if (spec->qualifier == 'h') {
1264 spec->qualifier = 'H';
1265 ++fmt;
1266 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001267 }
1268 }
1269
1270 /* default base */
1271 spec->base = 10;
1272 switch (*fmt) {
1273 case 'c':
1274 spec->type = FORMAT_TYPE_CHAR;
1275 return ++fmt - start;
1276
1277 case 's':
1278 spec->type = FORMAT_TYPE_STR;
1279 return ++fmt - start;
1280
1281 case 'p':
1282 spec->type = FORMAT_TYPE_PTR;
1283 return fmt - start;
1284 /* skip alnum */
1285
1286 case 'n':
1287 spec->type = FORMAT_TYPE_NRCHARS;
1288 return ++fmt - start;
1289
1290 case '%':
1291 spec->type = FORMAT_TYPE_PERCENT_CHAR;
1292 return ++fmt - start;
1293
1294 /* integer number formats - set up the flags and "break" */
1295 case 'o':
1296 spec->base = 8;
1297 break;
1298
1299 case 'x':
1300 spec->flags |= SMALL;
1301
1302 case 'X':
1303 spec->base = 16;
1304 break;
1305
1306 case 'd':
1307 case 'i':
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001308 spec->flags |= SIGN;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001309 case 'u':
1310 break;
1311
1312 default:
1313 spec->type = FORMAT_TYPE_INVALID;
1314 return fmt - start;
1315 }
1316
1317 if (spec->qualifier == 'L')
1318 spec->type = FORMAT_TYPE_LONG_LONG;
1319 else if (spec->qualifier == 'l') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001320 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001321 spec->type = FORMAT_TYPE_LONG;
1322 else
1323 spec->type = FORMAT_TYPE_ULONG;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001324 } else if (_tolower(spec->qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001325 spec->type = FORMAT_TYPE_SIZE_T;
1326 } else if (spec->qualifier == 't') {
1327 spec->type = FORMAT_TYPE_PTRDIFF;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001328 } else if (spec->qualifier == 'H') {
1329 if (spec->flags & SIGN)
1330 spec->type = FORMAT_TYPE_BYTE;
1331 else
1332 spec->type = FORMAT_TYPE_UBYTE;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001333 } else if (spec->qualifier == 'h') {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001334 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001335 spec->type = FORMAT_TYPE_SHORT;
1336 else
1337 spec->type = FORMAT_TYPE_USHORT;
1338 } else {
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001339 if (spec->flags & SIGN)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001340 spec->type = FORMAT_TYPE_INT;
1341 else
1342 spec->type = FORMAT_TYPE_UINT;
1343 }
1344
1345 return ++fmt - start;
Linus Torvalds78a8bf62008-07-06 16:16:15 -07001346}
1347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348/**
1349 * vsnprintf - Format a string and place it in a buffer
1350 * @buf: The buffer to place the result into
1351 * @size: The size of the buffer, including the trailing null space
1352 * @fmt: The format string to use
1353 * @args: Arguments for the format string
1354 *
Andi Kleen20036fd2008-10-15 22:02:02 -07001355 * This function follows C99 vsnprintf, but has some extensions:
Steven Rostedt91adcd22009-09-16 20:03:06 -04001356 * %pS output the name of a text symbol with offset
1357 * %ps output the name of a text symbol without offset
Frederic Weisbecker0c8b9462009-04-15 17:48:18 +02001358 * %pF output the name of a function pointer with its offset
1359 * %pf output the name of a function pointer without its offset
Namhyung Kim0f77a8d2011-03-24 11:42:29 +09001360 * %pB output the name of a backtrace symbol with its offset
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001361 * %pR output the address range in a struct resource with decoded flags
1362 * %pr output the address range in a struct resource with raw flags
1363 * %pM output a 6-byte MAC address with colons
Andy Shevchenko7c591542012-10-04 17:12:33 -07001364 * %pMR output a 6-byte MAC address with colons in reversed order
1365 * %pMF output a 6-byte MAC address with dashes
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001366 * %pm output a 6-byte MAC address without colons
Andy Shevchenko7c591542012-10-04 17:12:33 -07001367 * %pmR output a 6-byte MAC address without colons in reversed order
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001368 * %pI4 print an IPv4 address without leading zeros
1369 * %pi4 print an IPv4 address with leading zeros
1370 * %pI6 print an IPv6 address with colons
1371 * %pi6 print an IPv6 address without colons
Jan Engelhardtf996f202011-07-14 18:48:56 +02001372 * %pI6c print an IPv6 address as specified by RFC 5952
Uwe Kleine-König8a795032009-12-17 15:27:12 -08001373 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1374 * case.
Andy Shevchenko31550a12012-07-30 14:40:27 -07001375 * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
1376 * bytes of the input)
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001377 * %n is ignored
Andi Kleen20036fd2008-10-15 22:02:02 -07001378 *
Andrew Morton80f548e2012-07-30 14:40:25 -07001379 * ** Please update Documentation/printk-formats.txt when making changes **
1380 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 * The return value is the number of characters which would
1382 * be generated for the given input, excluding the trailing
1383 * '\0', as per ISO C99. If you want to have the exact
1384 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001385 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 * return is greater than or equal to @size, the resulting
1387 * string is truncated.
1388 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001389 * If you're not already dealing with a va_list consider using snprintf().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 */
1391int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
1392{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 unsigned long long num;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001394 char *str, *end;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001395 struct printf_spec spec = {0};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001397 /* Reject out-of-range values early. Large positive sizes are
1398 used for unknown buffer sizes. */
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001399 if (WARN_ON_ONCE((int) size < 0))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001403 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001405 /* Make sure end is always >= buf */
1406 if (end < buf) {
1407 end = ((void *)-1);
1408 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 }
1410
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001411 while (*fmt) {
1412 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001413 int read = format_decode(fmt, &spec);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001414
1415 fmt += read;
1416
1417 switch (spec.type) {
1418 case FORMAT_TYPE_NONE: {
1419 int copy = read;
1420 if (str < end) {
1421 if (copy > end - str)
1422 copy = end - str;
1423 memcpy(str, old_fmt, copy);
1424 }
1425 str += read;
1426 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428
Vegard Nossumed681a92009-03-14 12:08:50 +01001429 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001430 spec.field_width = va_arg(args, int);
1431 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001433 case FORMAT_TYPE_PRECISION:
1434 spec.precision = va_arg(args, int);
1435 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436
André Goddard Rosad4be1512009-12-14 18:00:59 -08001437 case FORMAT_TYPE_CHAR: {
1438 char c;
1439
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001440 if (!(spec.flags & LEFT)) {
1441 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001442 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 *str = ' ';
1444 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001445
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001447 }
1448 c = (unsigned char) va_arg(args, int);
1449 if (str < end)
1450 *str = c;
1451 ++str;
1452 while (--spec.field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001453 if (str < end)
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001454 *str = ' ';
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001456 }
1457 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001460 case FORMAT_TYPE_STR:
1461 str = string(str, end, va_arg(args, char *), spec);
1462 break;
1463
1464 case FORMAT_TYPE_PTR:
1465 str = pointer(fmt+1, str, end, va_arg(args, void *),
1466 spec);
1467 while (isalnum(*fmt))
1468 fmt++;
1469 break;
1470
1471 case FORMAT_TYPE_PERCENT_CHAR:
1472 if (str < end)
1473 *str = '%';
1474 ++str;
1475 break;
1476
1477 case FORMAT_TYPE_INVALID:
1478 if (str < end)
1479 *str = '%';
1480 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001481 break;
1482
1483 case FORMAT_TYPE_NRCHARS: {
Joe Perchesef0658f2010-03-06 17:10:14 -08001484 u8 qualifier = spec.qualifier;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001485
1486 if (qualifier == 'l') {
1487 long *ip = va_arg(args, long *);
1488 *ip = (str - buf);
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001489 } else if (_tolower(qualifier) == 'z') {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001490 size_t *ip = va_arg(args, size_t *);
1491 *ip = (str - buf);
1492 } else {
1493 int *ip = va_arg(args, int *);
1494 *ip = (str - buf);
1495 }
1496 break;
1497 }
1498
1499 default:
1500 switch (spec.type) {
1501 case FORMAT_TYPE_LONG_LONG:
1502 num = va_arg(args, long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001504 case FORMAT_TYPE_ULONG:
1505 num = va_arg(args, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001507 case FORMAT_TYPE_LONG:
1508 num = va_arg(args, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001510 case FORMAT_TYPE_SIZE_T:
Jason Gunthorpeef124962012-12-17 15:59:58 -08001511 if (spec.flags & SIGN)
1512 num = va_arg(args, ssize_t);
1513 else
1514 num = va_arg(args, size_t);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001515 break;
1516 case FORMAT_TYPE_PTRDIFF:
1517 num = va_arg(args, ptrdiff_t);
1518 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001519 case FORMAT_TYPE_UBYTE:
1520 num = (unsigned char) va_arg(args, int);
1521 break;
1522 case FORMAT_TYPE_BYTE:
1523 num = (signed char) va_arg(args, int);
1524 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001525 case FORMAT_TYPE_USHORT:
1526 num = (unsigned short) va_arg(args, int);
1527 break;
1528 case FORMAT_TYPE_SHORT:
1529 num = (short) va_arg(args, int);
1530 break;
Frederic Weisbecker39e874f2009-03-09 21:15:04 +01001531 case FORMAT_TYPE_INT:
1532 num = (int) va_arg(args, int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001533 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001535 num = va_arg(args, unsigned int);
1536 }
1537
1538 str = number(str, end, num, spec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001541
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001542 if (size > 0) {
1543 if (str < end)
1544 *str = '\0';
1545 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -07001546 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001547 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001548
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -07001549 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 return str-buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001551
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553EXPORT_SYMBOL(vsnprintf);
1554
1555/**
1556 * vscnprintf - Format a string and place it in a buffer
1557 * @buf: The buffer to place the result into
1558 * @size: The size of the buffer, including the trailing null space
1559 * @fmt: The format string to use
1560 * @args: Arguments for the format string
1561 *
1562 * The return value is the number of characters which have been written into
Anton Arapovb921c692011-01-12 16:59:49 -08001563 * the @buf not including the trailing '\0'. If @size is == 0 the function
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 * returns 0.
1565 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001566 * If you're not already dealing with a va_list consider using scnprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07001567 *
1568 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 */
1570int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
1571{
1572 int i;
1573
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001574 i = vsnprintf(buf, size, fmt, args);
1575
Anton Arapovb921c692011-01-12 16:59:49 -08001576 if (likely(i < size))
1577 return i;
1578 if (size != 0)
1579 return size - 1;
1580 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582EXPORT_SYMBOL(vscnprintf);
1583
1584/**
1585 * snprintf - Format a string and place it in a buffer
1586 * @buf: The buffer to place the result into
1587 * @size: The size of the buffer, including the trailing null space
1588 * @fmt: The format string to use
1589 * @...: Arguments for the format string
1590 *
1591 * The return value is the number of characters which would be
1592 * generated for the given input, excluding the trailing null,
1593 * as per ISO C99. If the return is greater than or equal to
1594 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -07001595 *
1596 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001598int snprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599{
1600 va_list args;
1601 int i;
1602
1603 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001604 i = vsnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 return i;
1608}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609EXPORT_SYMBOL(snprintf);
1610
1611/**
1612 * scnprintf - Format a string and place it in a buffer
1613 * @buf: The buffer to place the result into
1614 * @size: The size of the buffer, including the trailing null space
1615 * @fmt: The format string to use
1616 * @...: Arguments for the format string
1617 *
1618 * The return value is the number of characters written into @buf not including
Changli Gaob903c0b2010-10-26 14:22:50 -07001619 * the trailing '\0'. If @size is == 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 */
1621
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001622int scnprintf(char *buf, size_t size, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
1624 va_list args;
1625 int i;
1626
1627 va_start(args, fmt);
Anton Arapovb921c692011-01-12 16:59:49 -08001628 i = vscnprintf(buf, size, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001630
Anton Arapovb921c692011-01-12 16:59:49 -08001631 return i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632}
1633EXPORT_SYMBOL(scnprintf);
1634
1635/**
1636 * vsprintf - Format a string and place it in a buffer
1637 * @buf: The buffer to place the result into
1638 * @fmt: The format string to use
1639 * @args: Arguments for the format string
1640 *
1641 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001642 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 * buffer overflows.
1644 *
Uwe Kleine-Königba1835e2011-04-06 07:49:04 -07001645 * If you're not already dealing with a va_list consider using sprintf().
Andi Kleen20036fd2008-10-15 22:02:02 -07001646 *
1647 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 */
1649int vsprintf(char *buf, const char *fmt, va_list args)
1650{
1651 return vsnprintf(buf, INT_MAX, fmt, args);
1652}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653EXPORT_SYMBOL(vsprintf);
1654
1655/**
1656 * sprintf - Format a string and place it in a buffer
1657 * @buf: The buffer to place the result into
1658 * @fmt: The format string to use
1659 * @...: Arguments for the format string
1660 *
1661 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001662 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07001664 *
1665 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001667int sprintf(char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
1669 va_list args;
1670 int i;
1671
1672 va_start(args, fmt);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001673 i = vsnprintf(buf, INT_MAX, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001675
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return i;
1677}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678EXPORT_SYMBOL(sprintf);
1679
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001680#ifdef CONFIG_BINARY_PRINTF
1681/*
1682 * bprintf service:
1683 * vbin_printf() - VA arguments to binary data
1684 * bstr_printf() - Binary data to text string
1685 */
1686
1687/**
1688 * vbin_printf - Parse a format string and place args' binary value in a buffer
1689 * @bin_buf: The buffer to place args' binary value
1690 * @size: The size of the buffer(by words(32bits), not characters)
1691 * @fmt: The format string to use
1692 * @args: Arguments for the format string
1693 *
1694 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1695 * is skiped.
1696 *
1697 * The return value is the number of words(32bits) which would be generated for
1698 * the given input.
1699 *
1700 * NOTE:
1701 * If the return value is greater than @size, the resulting bin_buf is NOT
1702 * valid for bstr_printf().
1703 */
1704int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1705{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001706 struct printf_spec spec = {0};
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001707 char *str, *end;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001708
1709 str = (char *)bin_buf;
1710 end = (char *)(bin_buf + size);
1711
1712#define save_arg(type) \
1713do { \
1714 if (sizeof(type) == 8) { \
1715 unsigned long long value; \
1716 str = PTR_ALIGN(str, sizeof(u32)); \
1717 value = va_arg(args, unsigned long long); \
1718 if (str + sizeof(type) <= end) { \
1719 *(u32 *)str = *(u32 *)&value; \
1720 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1721 } \
1722 } else { \
1723 unsigned long value; \
1724 str = PTR_ALIGN(str, sizeof(type)); \
1725 value = va_arg(args, int); \
1726 if (str + sizeof(type) <= end) \
1727 *(typeof(type) *)str = (type)value; \
1728 } \
1729 str += sizeof(type); \
1730} while (0)
1731
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001732 while (*fmt) {
André Goddard Rosad4be1512009-12-14 18:00:59 -08001733 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001734
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001735 fmt += read;
1736
1737 switch (spec.type) {
1738 case FORMAT_TYPE_NONE:
André Goddard Rosad4be1512009-12-14 18:00:59 -08001739 case FORMAT_TYPE_INVALID:
1740 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001741 break;
1742
Vegard Nossumed681a92009-03-14 12:08:50 +01001743 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001744 case FORMAT_TYPE_PRECISION:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001745 save_arg(int);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001746 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001747
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001748 case FORMAT_TYPE_CHAR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001749 save_arg(char);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001750 break;
1751
1752 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001753 const char *save_str = va_arg(args, char *);
1754 size_t len;
André Goddard Rosa6c356632009-12-14 18:00:56 -08001755
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001756 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1757 || (unsigned long)save_str < PAGE_SIZE)
André Goddard Rosa0f4f81d2009-12-14 18:00:55 -08001758 save_str = "(null)";
André Goddard Rosa6c356632009-12-14 18:00:56 -08001759 len = strlen(save_str) + 1;
1760 if (str + len < end)
1761 memcpy(str, save_str, len);
1762 str += len;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001763 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001764 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001765
1766 case FORMAT_TYPE_PTR:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001767 save_arg(void *);
1768 /* skip all alphanumeric pointer suffixes */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001769 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001770 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001771 break;
1772
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001773 case FORMAT_TYPE_NRCHARS: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001774 /* skip %n 's argument */
Joe Perchesef0658f2010-03-06 17:10:14 -08001775 u8 qualifier = spec.qualifier;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001776 void *skip_arg;
1777 if (qualifier == 'l')
1778 skip_arg = va_arg(args, long *);
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07001779 else if (_tolower(qualifier) == 'z')
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001780 skip_arg = va_arg(args, size_t *);
1781 else
1782 skip_arg = va_arg(args, int *);
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001783 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001784 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001785
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001786 default:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001787 switch (spec.type) {
1788
1789 case FORMAT_TYPE_LONG_LONG:
1790 save_arg(long long);
1791 break;
1792 case FORMAT_TYPE_ULONG:
1793 case FORMAT_TYPE_LONG:
1794 save_arg(unsigned long);
1795 break;
1796 case FORMAT_TYPE_SIZE_T:
1797 save_arg(size_t);
1798 break;
1799 case FORMAT_TYPE_PTRDIFF:
1800 save_arg(ptrdiff_t);
1801 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001802 case FORMAT_TYPE_UBYTE:
1803 case FORMAT_TYPE_BYTE:
1804 save_arg(char);
1805 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001806 case FORMAT_TYPE_USHORT:
1807 case FORMAT_TYPE_SHORT:
1808 save_arg(short);
1809 break;
1810 default:
1811 save_arg(int);
1812 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001813 }
1814 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001815
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08001816 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001817#undef save_arg
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001818}
1819EXPORT_SYMBOL_GPL(vbin_printf);
1820
1821/**
1822 * bstr_printf - Format a string from binary arguments and place it in a buffer
1823 * @buf: The buffer to place the result into
1824 * @size: The size of the buffer, including the trailing null space
1825 * @fmt: The format string to use
1826 * @bin_buf: Binary arguments for the format string
1827 *
1828 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1829 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1830 * a binary buffer that generated by vbin_printf.
1831 *
1832 * The format follows C99 vsnprintf, but has some extensions:
Steven Rostedt0efb4d22009-09-17 09:27:29 -04001833 * see vsnprintf comment for details.
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001834 *
1835 * The return value is the number of characters which would
1836 * be generated for the given input, excluding the trailing
1837 * '\0', as per ISO C99. If you want to have the exact
1838 * number of characters written into @buf as return value
1839 * (not including the trailing '\0'), use vscnprintf(). If the
1840 * return is greater than or equal to @size, the resulting
1841 * string is truncated.
1842 */
1843int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1844{
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001845 struct printf_spec spec = {0};
André Goddard Rosad4be1512009-12-14 18:00:59 -08001846 char *str, *end;
1847 const char *args = (const char *)bin_buf;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001848
Marcin Slusarz2f30b1f92009-09-21 17:04:29 -07001849 if (WARN_ON_ONCE((int) size < 0))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001850 return 0;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001851
1852 str = buf;
1853 end = buf + size;
1854
1855#define get_arg(type) \
1856({ \
1857 typeof(type) value; \
1858 if (sizeof(type) == 8) { \
1859 args = PTR_ALIGN(args, sizeof(u32)); \
1860 *(u32 *)&value = *(u32 *)args; \
1861 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1862 } else { \
1863 args = PTR_ALIGN(args, sizeof(type)); \
1864 value = *(typeof(type) *)args; \
1865 } \
1866 args += sizeof(type); \
1867 value; \
1868})
1869
1870 /* Make sure end is always >= buf */
1871 if (end < buf) {
1872 end = ((void *)-1);
1873 size = end - buf;
1874 }
1875
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001876 while (*fmt) {
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001877 const char *old_fmt = fmt;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001878 int read = format_decode(fmt, &spec);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001879
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001880 fmt += read;
1881
1882 switch (spec.type) {
1883 case FORMAT_TYPE_NONE: {
1884 int copy = read;
1885 if (str < end) {
1886 if (copy > end - str)
1887 copy = end - str;
1888 memcpy(str, old_fmt, copy);
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001889 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001890 str += read;
1891 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001892 }
1893
Vegard Nossumed681a92009-03-14 12:08:50 +01001894 case FORMAT_TYPE_WIDTH:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001895 spec.field_width = get_arg(int);
1896 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001897
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001898 case FORMAT_TYPE_PRECISION:
1899 spec.precision = get_arg(int);
1900 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001901
André Goddard Rosad4be1512009-12-14 18:00:59 -08001902 case FORMAT_TYPE_CHAR: {
1903 char c;
1904
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001905 if (!(spec.flags & LEFT)) {
1906 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001907 if (str < end)
1908 *str = ' ';
1909 ++str;
1910 }
1911 }
1912 c = (unsigned char) get_arg(char);
1913 if (str < end)
1914 *str = c;
1915 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001916 while (--spec.field_width > 0) {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001917 if (str < end)
1918 *str = ' ';
1919 ++str;
1920 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001921 break;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001922 }
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001923
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001924 case FORMAT_TYPE_STR: {
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001925 const char *str_arg = args;
André Goddard Rosad4be1512009-12-14 18:00:59 -08001926 args += strlen(str_arg) + 1;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001927 str = string(str, end, (char *)str_arg, spec);
1928 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001929 }
1930
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001931 case FORMAT_TYPE_PTR:
1932 str = pointer(fmt+1, str, end, get_arg(void *), spec);
1933 while (isalnum(*fmt))
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001934 fmt++;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001935 break;
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001936
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001937 case FORMAT_TYPE_PERCENT_CHAR:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001938 case FORMAT_TYPE_INVALID:
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001939 if (str < end)
1940 *str = '%';
1941 ++str;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001942 break;
1943
1944 case FORMAT_TYPE_NRCHARS:
1945 /* skip */
1946 break;
1947
André Goddard Rosad4be1512009-12-14 18:00:59 -08001948 default: {
1949 unsigned long long num;
1950
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001951 switch (spec.type) {
1952
1953 case FORMAT_TYPE_LONG_LONG:
1954 num = get_arg(long long);
1955 break;
1956 case FORMAT_TYPE_ULONG:
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001957 case FORMAT_TYPE_LONG:
1958 num = get_arg(unsigned long);
1959 break;
1960 case FORMAT_TYPE_SIZE_T:
1961 num = get_arg(size_t);
1962 break;
1963 case FORMAT_TYPE_PTRDIFF:
1964 num = get_arg(ptrdiff_t);
1965 break;
Zhaoleia4e94ef2009-03-27 17:07:05 +08001966 case FORMAT_TYPE_UBYTE:
1967 num = get_arg(unsigned char);
1968 break;
1969 case FORMAT_TYPE_BYTE:
1970 num = get_arg(signed char);
1971 break;
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001972 case FORMAT_TYPE_USHORT:
1973 num = get_arg(unsigned short);
1974 break;
1975 case FORMAT_TYPE_SHORT:
1976 num = get_arg(short);
1977 break;
1978 case FORMAT_TYPE_UINT:
1979 num = get_arg(unsigned int);
1980 break;
1981 default:
1982 num = get_arg(int);
1983 }
1984
1985 str = number(str, end, num, spec);
André Goddard Rosad4be1512009-12-14 18:00:59 -08001986 } /* default: */
1987 } /* switch(spec.type) */
1988 } /* while(*fmt) */
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001989
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001990 if (size > 0) {
1991 if (str < end)
1992 *str = '\0';
1993 else
1994 end[-1] = '\0';
1995 }
Frederic Weisbeckerfef20d92009-03-06 17:21:50 +01001996
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001997#undef get_arg
1998
1999 /* the trailing null byte doesn't count towards the total */
2000 return str - buf;
2001}
2002EXPORT_SYMBOL_GPL(bstr_printf);
2003
2004/**
2005 * bprintf - Parse a format string and place args' binary value in a buffer
2006 * @bin_buf: The buffer to place args' binary value
2007 * @size: The size of the buffer(by words(32bits), not characters)
2008 * @fmt: The format string to use
2009 * @...: Arguments for the format string
2010 *
2011 * The function returns the number of words(u32) written
2012 * into @bin_buf.
2013 */
2014int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
2015{
2016 va_list args;
2017 int ret;
2018
2019 va_start(args, fmt);
2020 ret = vbin_printf(bin_buf, size, fmt, args);
2021 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002022
Lai Jiangshan4370aa42009-03-06 17:21:46 +01002023 return ret;
2024}
2025EXPORT_SYMBOL_GPL(bprintf);
2026
2027#endif /* CONFIG_BINARY_PRINTF */
2028
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029/**
2030 * vsscanf - Unformat a buffer into a list of arguments
2031 * @buf: input buffer
2032 * @fmt: format of buffer
2033 * @args: arguments
2034 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002035int vsscanf(const char *buf, const char *fmt, va_list args)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036{
2037 const char *str = buf;
2038 char *next;
2039 char digit;
2040 int num = 0;
Joe Perchesef0658f2010-03-06 17:10:14 -08002041 u8 qualifier;
Jan Beulich53809752012-12-17 16:01:31 -08002042 unsigned int base;
2043 union {
2044 long long s;
2045 unsigned long long u;
2046 } val;
Joe Perchesef0658f2010-03-06 17:10:14 -08002047 s16 field_width;
André Goddard Rosad4be1512009-12-14 18:00:59 -08002048 bool is_sign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Jan Beulichda990752012-10-04 17:13:24 -07002050 while (*fmt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 /* skip any white space in format */
2052 /* white space in format matchs any amount of
2053 * white space, including none, in the input.
2054 */
2055 if (isspace(*fmt)) {
André Goddard Rosae7d28602009-12-14 18:01:06 -08002056 fmt = skip_spaces(++fmt);
2057 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
2059
2060 /* anything that is not a conversion must match exactly */
2061 if (*fmt != '%' && *fmt) {
2062 if (*fmt++ != *str++)
2063 break;
2064 continue;
2065 }
2066
2067 if (!*fmt)
2068 break;
2069 ++fmt;
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 /* skip this conversion.
2072 * advance both strings to next white space
2073 */
2074 if (*fmt == '*') {
Jan Beulichda990752012-10-04 17:13:24 -07002075 if (!*str)
2076 break;
Andy Spencer8fccae22009-10-01 15:44:27 -07002077 while (!isspace(*fmt) && *fmt != '%' && *fmt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 fmt++;
2079 while (!isspace(*str) && *str)
2080 str++;
2081 continue;
2082 }
2083
2084 /* get field width */
2085 field_width = -1;
Jan Beulich53809752012-12-17 16:01:31 -08002086 if (isdigit(*fmt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 field_width = skip_atoi(&fmt);
Jan Beulich53809752012-12-17 16:01:31 -08002088 if (field_width <= 0)
2089 break;
2090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091
2092 /* get conversion qualifier */
2093 qualifier = -1;
Andy Shevchenko75fb8f22011-07-25 17:13:20 -07002094 if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
2095 _tolower(*fmt) == 'z') {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 qualifier = *fmt++;
2097 if (unlikely(qualifier == *fmt)) {
2098 if (qualifier == 'h') {
2099 qualifier = 'H';
2100 fmt++;
2101 } else if (qualifier == 'l') {
2102 qualifier = 'L';
2103 fmt++;
2104 }
2105 }
2106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
Jan Beulichda990752012-10-04 17:13:24 -07002108 if (!*fmt)
2109 break;
2110
2111 if (*fmt == 'n') {
2112 /* return number of characters read so far */
2113 *va_arg(args, int *) = str - buf;
2114 ++fmt;
2115 continue;
2116 }
2117
2118 if (!*str)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 break;
2120
André Goddard Rosad4be1512009-12-14 18:00:59 -08002121 base = 10;
2122 is_sign = 0;
2123
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002124 switch (*fmt++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 case 'c':
2126 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002127 char *s = (char *)va_arg(args, char*);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 if (field_width == -1)
2129 field_width = 1;
2130 do {
2131 *s++ = *str++;
2132 } while (--field_width > 0 && *str);
2133 num++;
2134 }
2135 continue;
2136 case 's':
2137 {
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002138 char *s = (char *)va_arg(args, char *);
2139 if (field_width == -1)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -07002140 field_width = SHRT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 /* first, skip leading white space in buffer */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002142 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
2144 /* now copy until next white space */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002145 while (*str && !isspace(*str) && field_width--)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 *s++ = *str++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 *s = '\0';
2148 num++;
2149 }
2150 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 case 'o':
2152 base = 8;
2153 break;
2154 case 'x':
2155 case 'X':
2156 base = 16;
2157 break;
2158 case 'i':
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002159 base = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 case 'd':
2161 is_sign = 1;
2162 case 'u':
2163 break;
2164 case '%':
2165 /* looking for '%' in str */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002166 if (*str++ != '%')
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 return num;
2168 continue;
2169 default:
2170 /* invalid format; stop here */
2171 return num;
2172 }
2173
2174 /* have some sort of integer conversion.
2175 * first, skip white space in buffer.
2176 */
André Goddard Rosae7d28602009-12-14 18:01:06 -08002177 str = skip_spaces(str);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
2179 digit = *str;
2180 if (is_sign && digit == '-')
2181 digit = *(str + 1);
2182
2183 if (!digit
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002184 || (base == 16 && !isxdigit(digit))
2185 || (base == 10 && !isdigit(digit))
2186 || (base == 8 && (!isdigit(digit) || digit > '7'))
2187 || (base == 0 && !isdigit(digit)))
2188 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Jan Beulich53809752012-12-17 16:01:31 -08002190 if (is_sign)
2191 val.s = qualifier != 'L' ?
2192 simple_strtol(str, &next, base) :
2193 simple_strtoll(str, &next, base);
2194 else
2195 val.u = qualifier != 'L' ?
2196 simple_strtoul(str, &next, base) :
2197 simple_strtoull(str, &next, base);
2198
2199 if (field_width > 0 && next - str > field_width) {
2200 if (base == 0)
2201 _parse_integer_fixup_radix(str, &base);
2202 while (next - str > field_width) {
2203 if (is_sign)
2204 val.s = div_s64(val.s, base);
2205 else
2206 val.u = div_u64(val.u, base);
2207 --next;
2208 }
2209 }
2210
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002211 switch (qualifier) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 case 'H': /* that's 'hh' in format */
Jan Beulich53809752012-12-17 16:01:31 -08002213 if (is_sign)
2214 *va_arg(args, signed char *) = val.s;
2215 else
2216 *va_arg(args, unsigned char *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 break;
2218 case 'h':
Jan Beulich53809752012-12-17 16:01:31 -08002219 if (is_sign)
2220 *va_arg(args, short *) = val.s;
2221 else
2222 *va_arg(args, unsigned short *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 break;
2224 case 'l':
Jan Beulich53809752012-12-17 16:01:31 -08002225 if (is_sign)
2226 *va_arg(args, long *) = val.s;
2227 else
2228 *va_arg(args, unsigned long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 break;
2230 case 'L':
Jan Beulich53809752012-12-17 16:01:31 -08002231 if (is_sign)
2232 *va_arg(args, long long *) = val.s;
2233 else
2234 *va_arg(args, unsigned long long *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 break;
2236 case 'Z':
2237 case 'z':
Jan Beulich53809752012-12-17 16:01:31 -08002238 *va_arg(args, size_t *) = val.u;
2239 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 default:
Jan Beulich53809752012-12-17 16:01:31 -08002241 if (is_sign)
2242 *va_arg(args, int *) = val.s;
2243 else
2244 *va_arg(args, unsigned int *) = val.u;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 break;
2246 }
2247 num++;
2248
2249 if (!next)
2250 break;
2251 str = next;
2252 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07002253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 return num;
2255}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256EXPORT_SYMBOL(vsscanf);
2257
2258/**
2259 * sscanf - Unformat a buffer into a list of arguments
2260 * @buf: input buffer
2261 * @fmt: formatting of buffer
2262 * @...: resulting arguments
2263 */
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002264int sscanf(const char *buf, const char *fmt, ...)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265{
2266 va_list args;
2267 int i;
2268
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002269 va_start(args, fmt);
2270 i = vsscanf(buf, fmt, args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 va_end(args);
André Goddard Rosa7b9186f2009-12-14 18:00:57 -08002272
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 return i;
2274}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275EXPORT_SYMBOL(sscanf);