blob: 3543bbe8b1bc06023e380faa2ff3c87067d65034 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/lib/vsprintf.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 */
6
7/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
8/*
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
10 */
11
12/*
13 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
14 * - changed to provide snprintf and vsnprintf functions
15 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
16 * - scnprintf and vscnprintf
17 */
18
19#include <stdarg.h>
20#include <linux/module.h>
21#include <linux/types.h>
22#include <linux/string.h>
23#include <linux/ctype.h>
24#include <linux/kernel.h>
Linus Torvalds0fe1ef22008-07-06 16:43:12 -070025#include <linux/kallsyms.h>
26#include <linux/uaccess.h>
Linus Torvalds332d2e72008-10-20 15:07:34 +110027#include <linux/ioport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Tim Schmielau4e57b682005-10-30 15:03:48 -080029#include <asm/page.h> /* for PAGE_SIZE */
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/div64.h>
James Bottomleydeac93d2008-09-03 20:43:36 -050031#include <asm/sections.h> /* for dereference_function_descriptor() */
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Denys Vlasenko9b706ae2008-02-09 23:24:09 +010033/* Works only for digits and letters, but small and fast */
34#define TOLOWER(x) ((x) | 0x20)
35
Harvey Harrisonaa46a632008-10-16 13:40:34 -070036static unsigned int simple_guess_base(const char *cp)
37{
38 if (cp[0] == '0') {
39 if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
40 return 16;
41 else
42 return 8;
43 } else {
44 return 10;
45 }
46}
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048/**
49 * simple_strtoul - convert a string to an unsigned long
50 * @cp: The start of the string
51 * @endp: A pointer to the end of the parsed string will be placed here
52 * @base: The number base to use
53 */
Harvey Harrison22d27052008-10-16 13:40:35 -070054unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Harvey Harrisonaa46a632008-10-16 13:40:34 -070056 unsigned long result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Harvey Harrisonaa46a632008-10-16 13:40:34 -070058 if (!base)
59 base = simple_guess_base(cp);
60
61 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
62 cp += 2;
63
64 while (isxdigit(*cp)) {
65 unsigned int value;
66
67 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
68 if (value >= base)
69 break;
70 result = result * base + value;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 cp++;
72 }
Harvey Harrisonaa46a632008-10-16 13:40:34 -070073
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (endp)
75 *endp = (char *)cp;
76 return result;
77}
Linus Torvalds1da177e2005-04-16 15:20:36 -070078EXPORT_SYMBOL(simple_strtoul);
79
80/**
81 * simple_strtol - convert a string to a signed long
82 * @cp: The start of the string
83 * @endp: A pointer to the end of the parsed string will be placed here
84 * @base: The number base to use
85 */
Harvey Harrison22d27052008-10-16 13:40:35 -070086long simple_strtol(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Harvey Harrison22d27052008-10-16 13:40:35 -070088 if(*cp == '-')
89 return -simple_strtoul(cp + 1, endp, base);
90 return simple_strtoul(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091}
Linus Torvalds1da177e2005-04-16 15:20:36 -070092EXPORT_SYMBOL(simple_strtol);
93
94/**
95 * simple_strtoull - convert a string to an unsigned long long
96 * @cp: The start of the string
97 * @endp: A pointer to the end of the parsed string will be placed here
98 * @base: The number base to use
99 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700100unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
Harvey Harrisonaa46a632008-10-16 13:40:34 -0700102 unsigned long long result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Harvey Harrisonaa46a632008-10-16 13:40:34 -0700104 if (!base)
105 base = simple_guess_base(cp);
106
107 if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
108 cp += 2;
109
110 while (isxdigit(*cp)) {
111 unsigned int value;
112
113 value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
114 if (value >= base)
115 break;
116 result = result * base + value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 cp++;
118 }
Harvey Harrisonaa46a632008-10-16 13:40:34 -0700119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (endp)
121 *endp = (char *)cp;
122 return result;
123}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124EXPORT_SYMBOL(simple_strtoull);
125
126/**
127 * simple_strtoll - convert a string to a signed long long
128 * @cp: The start of the string
129 * @endp: A pointer to the end of the parsed string will be placed here
130 * @base: The number base to use
131 */
Harvey Harrison22d27052008-10-16 13:40:35 -0700132long long simple_strtoll(const char *cp, char **endp, unsigned int base)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
134 if(*cp=='-')
Harvey Harrison22d27052008-10-16 13:40:35 -0700135 return -simple_strtoull(cp + 1, endp, base);
136 return simple_strtoull(cp, endp, base);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Yi Yang06b2a762008-02-08 04:21:57 -0800139/**
140 * strict_strtoul - convert a string to an unsigned long strictly
141 * @cp: The string to be converted
142 * @base: The number base to use
143 * @res: The converted result value
144 *
145 * strict_strtoul converts a string to an unsigned long only if the
146 * string is really an unsigned long string, any string containing
147 * any invalid char at the tail will be rejected and -EINVAL is returned,
148 * only a newline char at the tail is acceptible because people generally
149 * change a module parameter in the following way:
150 *
151 * echo 1024 > /sys/module/e1000/parameters/copybreak
152 *
153 * echo will append a newline to the tail.
154 *
155 * It returns 0 if conversion is successful and *res is set to the converted
156 * value, otherwise it returns -EINVAL and *res is set to 0.
157 *
158 * simple_strtoul just ignores the successive invalid characters and
159 * return the converted value of prefix part of the string.
160 */
Harvey Harrison9d85db22008-10-16 13:40:35 -0700161int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
162{
163 char *tail;
164 unsigned long val;
165 size_t len;
166
167 *res = 0;
168 len = strlen(cp);
169 if (len == 0)
170 return -EINVAL;
171
172 val = simple_strtoul(cp, &tail, base);
Pavel Macheke899aa82009-01-06 14:40:53 -0800173 if (tail == cp)
174 return -EINVAL;
Harvey Harrison9d85db22008-10-16 13:40:35 -0700175 if ((*tail == '\0') ||
176 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
177 *res = val;
178 return 0;
179 }
180
181 return -EINVAL;
182}
183EXPORT_SYMBOL(strict_strtoul);
Yi Yang06b2a762008-02-08 04:21:57 -0800184
185/**
186 * strict_strtol - convert a string to a long strictly
187 * @cp: The string to be converted
188 * @base: The number base to use
189 * @res: The converted result value
190 *
191 * strict_strtol is similiar to strict_strtoul, but it allows the first
192 * character of a string is '-'.
193 *
194 * It returns 0 if conversion is successful and *res is set to the converted
195 * value, otherwise it returns -EINVAL and *res is set to 0.
196 */
Harvey Harrison9d85db22008-10-16 13:40:35 -0700197int strict_strtol(const char *cp, unsigned int base, long *res)
198{
199 int ret;
200 if (*cp == '-') {
201 ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
202 if (!ret)
203 *res = -(*res);
204 } else {
205 ret = strict_strtoul(cp, base, (unsigned long *)res);
206 }
207
208 return ret;
209}
210EXPORT_SYMBOL(strict_strtol);
Yi Yang06b2a762008-02-08 04:21:57 -0800211
212/**
213 * strict_strtoull - convert a string to an unsigned long long strictly
214 * @cp: The string to be converted
215 * @base: The number base to use
216 * @res: The converted result value
217 *
218 * strict_strtoull converts a string to an unsigned long long only if the
219 * string is really an unsigned long long string, any string containing
220 * any invalid char at the tail will be rejected and -EINVAL is returned,
221 * only a newline char at the tail is acceptible because people generally
222 * change a module parameter in the following way:
223 *
224 * echo 1024 > /sys/module/e1000/parameters/copybreak
225 *
226 * echo will append a newline to the tail of the string.
227 *
228 * It returns 0 if conversion is successful and *res is set to the converted
229 * value, otherwise it returns -EINVAL and *res is set to 0.
230 *
231 * simple_strtoull just ignores the successive invalid characters and
232 * return the converted value of prefix part of the string.
233 */
Harvey Harrison9d85db22008-10-16 13:40:35 -0700234int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
235{
236 char *tail;
237 unsigned long long val;
238 size_t len;
239
240 *res = 0;
241 len = strlen(cp);
242 if (len == 0)
243 return -EINVAL;
244
245 val = simple_strtoull(cp, &tail, base);
Pavel Macheke899aa82009-01-06 14:40:53 -0800246 if (tail == cp)
247 return -EINVAL;
Harvey Harrison9d85db22008-10-16 13:40:35 -0700248 if ((*tail == '\0') ||
249 ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
250 *res = val;
251 return 0;
252 }
253
254 return -EINVAL;
255}
256EXPORT_SYMBOL(strict_strtoull);
Yi Yang06b2a762008-02-08 04:21:57 -0800257
258/**
259 * strict_strtoll - convert a string to a long long strictly
260 * @cp: The string to be converted
261 * @base: The number base to use
262 * @res: The converted result value
263 *
264 * strict_strtoll is similiar to strict_strtoull, but it allows the first
265 * character of a string is '-'.
266 *
267 * It returns 0 if conversion is successful and *res is set to the converted
268 * value, otherwise it returns -EINVAL and *res is set to 0.
269 */
Harvey Harrison9d85db22008-10-16 13:40:35 -0700270int strict_strtoll(const char *cp, unsigned int base, long long *res)
271{
272 int ret;
273 if (*cp == '-') {
274 ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
275 if (!ret)
276 *res = -(*res);
277 } else {
278 ret = strict_strtoull(cp, base, (unsigned long long *)res);
279 }
Yi Yang06b2a762008-02-08 04:21:57 -0800280
Harvey Harrison9d85db22008-10-16 13:40:35 -0700281 return ret;
282}
Yi Yang06b2a762008-02-08 04:21:57 -0800283EXPORT_SYMBOL(strict_strtoll);
Yi Yang06b2a762008-02-08 04:21:57 -0800284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285static int skip_atoi(const char **s)
286{
287 int i=0;
288
289 while (isdigit(**s))
290 i = i*10 + *((*s)++) - '0';
291 return i;
292}
293
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700294/* Decimal conversion is by far the most typical, and is used
295 * for /proc and /sys data. This directly impacts e.g. top performance
296 * with many processes running. We optimize it for speed
297 * using code from
298 * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
299 * (with permission from the author, Douglas W. Jones). */
300
301/* Formats correctly any integer in [0,99999].
302 * Outputs from one to five digits depending on input.
303 * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
304static char* put_dec_trunc(char *buf, unsigned q)
305{
306 unsigned d3, d2, d1, d0;
307 d1 = (q>>4) & 0xf;
308 d2 = (q>>8) & 0xf;
309 d3 = (q>>12);
310
311 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
312 q = (d0 * 0xcd) >> 11;
313 d0 = d0 - 10*q;
314 *buf++ = d0 + '0'; /* least significant digit */
315 d1 = q + 9*d3 + 5*d2 + d1;
316 if (d1 != 0) {
317 q = (d1 * 0xcd) >> 11;
318 d1 = d1 - 10*q;
319 *buf++ = d1 + '0'; /* next digit */
320
321 d2 = q + 2*d2;
322 if ((d2 != 0) || (d3 != 0)) {
323 q = (d2 * 0xd) >> 7;
324 d2 = d2 - 10*q;
325 *buf++ = d2 + '0'; /* next digit */
326
327 d3 = q + 4*d3;
328 if (d3 != 0) {
329 q = (d3 * 0xcd) >> 11;
330 d3 = d3 - 10*q;
331 *buf++ = d3 + '0'; /* next digit */
332 if (q != 0)
333 *buf++ = q + '0'; /* most sign. digit */
334 }
335 }
336 }
337 return buf;
338}
339/* Same with if's removed. Always emits five digits */
340static char* put_dec_full(char *buf, unsigned q)
341{
342 /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
343 /* but anyway, gcc produces better code with full-sized ints */
344 unsigned d3, d2, d1, d0;
345 d1 = (q>>4) & 0xf;
346 d2 = (q>>8) & 0xf;
347 d3 = (q>>12);
348
349 /* Possible ways to approx. divide by 10 */
350 /* gcc -O2 replaces multiply with shifts and adds */
351 // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
352 // (x * 0x67) >> 10: 1100111
353 // (x * 0x34) >> 9: 110100 - same
354 // (x * 0x1a) >> 8: 11010 - same
355 // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
356
357 d0 = 6*(d3 + d2 + d1) + (q & 0xf);
358 q = (d0 * 0xcd) >> 11;
359 d0 = d0 - 10*q;
360 *buf++ = d0 + '0';
361 d1 = q + 9*d3 + 5*d2 + d1;
362 q = (d1 * 0xcd) >> 11;
363 d1 = d1 - 10*q;
364 *buf++ = d1 + '0';
365
366 d2 = q + 2*d2;
367 q = (d2 * 0xd) >> 7;
368 d2 = d2 - 10*q;
369 *buf++ = d2 + '0';
370
371 d3 = q + 4*d3;
372 q = (d3 * 0xcd) >> 11; /* - shorter code */
373 /* q = (d3 * 0x67) >> 10; - would also work */
374 d3 = d3 - 10*q;
375 *buf++ = d3 + '0';
376 *buf++ = q + '0';
377 return buf;
378}
379/* No inlining helps gcc to use registers better */
380static noinline char* put_dec(char *buf, unsigned long long num)
381{
382 while (1) {
383 unsigned rem;
384 if (num < 100000)
385 return put_dec_trunc(buf, num);
386 rem = do_div(num, 100000);
387 buf = put_dec_full(buf, rem);
388 }
389}
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391#define ZEROPAD 1 /* pad with zero */
392#define SIGN 2 /* unsigned/signed long */
393#define PLUS 4 /* show plus */
394#define SPACE 8 /* space if plus */
395#define LEFT 16 /* left justified */
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100396#define SMALL 32 /* Must be 32 == 0x20 */
397#define SPECIAL 64 /* 0x */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700399static char *number(char *buf, char *end, unsigned long long num, int base, int size, int precision, int type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100401 /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
402 static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
403
404 char tmp[66];
405 char sign;
406 char locase;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700407 int need_pfx = ((type & SPECIAL) && base != 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 int i;
409
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100410 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
411 * produces same digits or (maybe lowercased) letters */
412 locase = (type & SMALL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (type & LEFT)
414 type &= ~ZEROPAD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 sign = 0;
416 if (type & SIGN) {
417 if ((signed long long) num < 0) {
418 sign = '-';
419 num = - (signed long long) num;
420 size--;
421 } else if (type & PLUS) {
422 sign = '+';
423 size--;
424 } else if (type & SPACE) {
425 sign = ' ';
426 size--;
427 }
428 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700429 if (need_pfx) {
430 size--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (base == 16)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 size--;
433 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700434
435 /* generate full string in tmp[], in reverse order */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 i = 0;
437 if (num == 0)
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700438 tmp[i++] = '0';
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700439 /* Generic code, for any base:
440 else do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100441 tmp[i++] = (digits[do_div(num,base)] | locase);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700442 } while (num != 0);
443 */
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700444 else if (base != 10) { /* 8 or 16 */
445 int mask = base - 1;
446 int shift = 3;
447 if (base == 16) shift = 4;
448 do {
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100449 tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700450 num >>= shift;
451 } while (num);
Denis Vlasenko4277eed2007-07-15 23:41:56 -0700452 } else { /* base 10 */
453 i = put_dec(tmp, num) - tmp;
454 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700455
456 /* printing 100 using %2d gives "100", not "00" */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 if (i > precision)
458 precision = i;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700459 /* leading space padding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 size -= precision;
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700461 if (!(type & (ZEROPAD+LEFT))) {
462 while(--size >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700463 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 *buf = ' ';
465 ++buf;
466 }
467 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700468 /* sign */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (sign) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700470 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 *buf = sign;
472 ++buf;
473 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700474 /* "0x" / "0" prefix */
475 if (need_pfx) {
476 if (buf < end)
477 *buf = '0';
478 ++buf;
479 if (base == 16) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700480 if (buf < end)
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100481 *buf = ('X' | locase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 ++buf;
483 }
484 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700485 /* zero or space padding */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (!(type & LEFT)) {
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700487 char c = (type & ZEROPAD) ? '0' : ' ';
488 while (--size >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700489 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 *buf = c;
491 ++buf;
492 }
493 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700494 /* hmm even more zero padding? */
495 while (i <= --precision) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700496 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 *buf = '0';
498 ++buf;
499 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700500 /* actual digits of result */
501 while (--i >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700502 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 *buf = tmp[i];
504 ++buf;
505 }
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700506 /* trailing space padding */
507 while (--size >= 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700508 if (buf < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 *buf = ' ';
510 ++buf;
511 }
512 return buf;
513}
514
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700515static char *string(char *buf, char *end, char *s, int field_width, int precision, int flags)
516{
517 int len, i;
518
519 if ((unsigned long)s < PAGE_SIZE)
520 s = "<NULL>";
521
522 len = strnlen(s, precision);
523
524 if (!(flags & LEFT)) {
525 while (len < field_width--) {
526 if (buf < end)
527 *buf = ' ';
528 ++buf;
529 }
530 }
531 for (i = 0; i < len; ++i) {
532 if (buf < end)
533 *buf = *s;
534 ++buf; ++s;
535 }
536 while (len < field_width--) {
537 if (buf < end)
538 *buf = ' ';
539 ++buf;
540 }
541 return buf;
542}
543
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700544static char *symbol_string(char *buf, char *end, void *ptr, int field_width, int precision, int flags)
545{
546 unsigned long value = (unsigned long) ptr;
547#ifdef CONFIG_KALLSYMS
548 char sym[KSYM_SYMBOL_LEN];
549 sprint_symbol(sym, value);
550 return string(buf, end, sym, field_width, precision, flags);
551#else
552 field_width = 2*sizeof(void *);
553 flags |= SPECIAL | SMALL | ZEROPAD;
554 return number(buf, end, value, 16, field_width, precision, flags);
555#endif
556}
557
Linus Torvalds332d2e72008-10-20 15:07:34 +1100558static char *resource_string(char *buf, char *end, struct resource *res, int field_width, int precision, int flags)
559{
560#ifndef IO_RSRC_PRINTK_SIZE
561#define IO_RSRC_PRINTK_SIZE 4
562#endif
563
564#ifndef MEM_RSRC_PRINTK_SIZE
565#define MEM_RSRC_PRINTK_SIZE 8
566#endif
567
568 /* room for the actual numbers, the two "0x", -, [, ] and the final zero */
569 char sym[4*sizeof(resource_size_t) + 8];
570 char *p = sym, *pend = sym + sizeof(sym);
571 int size = -1;
572
573 if (res->flags & IORESOURCE_IO)
574 size = IO_RSRC_PRINTK_SIZE;
575 else if (res->flags & IORESOURCE_MEM)
576 size = MEM_RSRC_PRINTK_SIZE;
577
578 *p++ = '[';
579 p = number(p, pend, res->start, 16, size, -1, SPECIAL | SMALL | ZEROPAD);
580 *p++ = '-';
581 p = number(p, pend, res->end, 16, size, -1, SPECIAL | SMALL | ZEROPAD);
582 *p++ = ']';
583 *p = 0;
584
585 return string(buf, end, sym, field_width, precision, flags);
586}
587
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700588static char *mac_address_string(char *buf, char *end, u8 *addr, int field_width,
589 int precision, int flags)
590{
591 char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
592 char *p = mac_addr;
593 int i;
594
595 for (i = 0; i < 6; i++) {
596 p = pack_hex_byte(p, addr[i]);
597 if (!(flags & SPECIAL) && i != 5)
598 *p++ = ':';
599 }
600 *p = '\0';
601
602 return string(buf, end, mac_addr, field_width, precision, flags & ~SPECIAL);
603}
604
Harvey Harrison689afa72008-10-28 16:04:44 -0700605static char *ip6_addr_string(char *buf, char *end, u8 *addr, int field_width,
606 int precision, int flags)
607{
608 char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
609 char *p = ip6_addr;
610 int i;
611
612 for (i = 0; i < 8; i++) {
613 p = pack_hex_byte(p, addr[2 * i]);
614 p = pack_hex_byte(p, addr[2 * i + 1]);
615 if (!(flags & SPECIAL) && i != 7)
616 *p++ = ':';
617 }
618 *p = '\0';
619
620 return string(buf, end, ip6_addr, field_width, precision, flags & ~SPECIAL);
621}
622
Harvey Harrison4aa99602008-10-29 12:49:58 -0700623static char *ip4_addr_string(char *buf, char *end, u8 *addr, int field_width,
624 int precision, int flags)
625{
626 char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
Harvey Harrisonb9ac9982008-11-03 17:09:55 -0800627 char temp[3]; /* hold each IP quad in reverse order */
Harvey Harrison4aa99602008-10-29 12:49:58 -0700628 char *p = ip4_addr;
Harvey Harrisonb9ac9982008-11-03 17:09:55 -0800629 int i, digits;
Harvey Harrison4aa99602008-10-29 12:49:58 -0700630
631 for (i = 0; i < 4; i++) {
Harvey Harrisonb9ac9982008-11-03 17:09:55 -0800632 digits = put_dec_trunc(temp, addr[i]) - temp;
633 /* reverse the digits in the quad */
634 while (digits--)
635 *p++ = temp[digits];
Harvey Harrison4aa99602008-10-29 12:49:58 -0700636 if (i != 3)
637 *p++ = '.';
638 }
639 *p = '\0';
640
641 return string(buf, end, ip4_addr, field_width, precision, flags & ~SPECIAL);
642}
643
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700644/*
645 * Show a '%p' thing. A kernel extension is that the '%p' is followed
646 * by an extra set of alphanumeric characters that are extended format
647 * specifiers.
648 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100649 * Right now we handle:
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700650 *
Linus Torvalds332d2e72008-10-20 15:07:34 +1100651 * - 'F' For symbolic function descriptor pointers
652 * - 'S' For symbolic direct pointers
653 * - 'R' For a struct resource pointer, it prints the range of
654 * addresses (not the name nor the flags)
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700655 * - 'M' For a 6-byte MAC address, it prints the address in the
656 * usual colon-separated hex notation
Harvey Harrison4aa99602008-10-29 12:49:58 -0700657 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
658 * decimal for v4 and colon separated network-order 16 bit hex for v6)
659 * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
Harvey Harrison6b9a1062008-10-29 12:53:10 -0700660 * currently the same
Linus Torvalds332d2e72008-10-20 15:07:34 +1100661 *
662 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
663 * function pointers are really function descriptors, which contain a
664 * pointer to the real address.
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700665 */
666static char *pointer(const char *fmt, char *buf, char *end, void *ptr, int field_width, int precision, int flags)
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700667{
Linus Torvaldsd97106a2009-01-03 11:46:17 -0800668 if (!ptr)
669 return string(buf, end, "(null)", field_width, precision, flags);
670
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700671 switch (*fmt) {
672 case 'F':
673 ptr = dereference_function_descriptor(ptr);
674 /* Fallthrough */
675 case 'S':
676 return symbol_string(buf, end, ptr, field_width, precision, flags);
Linus Torvalds332d2e72008-10-20 15:07:34 +1100677 case 'R':
678 return resource_string(buf, end, ptr, field_width, precision, flags);
Harvey Harrison411c41e2008-11-25 00:40:37 -0800679 case 'm':
680 flags |= SPECIAL;
681 /* Fallthrough */
Harvey Harrisondd45c9c2008-10-27 15:47:12 -0700682 case 'M':
683 return mac_address_string(buf, end, ptr, field_width, precision, flags);
Harvey Harrison4aa99602008-10-29 12:49:58 -0700684 case 'i':
685 flags |= SPECIAL;
686 /* Fallthrough */
687 case 'I':
688 if (fmt[1] == '6')
689 return ip6_addr_string(buf, end, ptr, field_width, precision, flags);
690 if (fmt[1] == '4')
691 return ip4_addr_string(buf, end, ptr, field_width, precision, flags);
692 flags &= ~SPECIAL;
693 break;
Linus Torvalds0fe1ef22008-07-06 16:43:12 -0700694 }
Linus Torvalds78a8bf62008-07-06 16:16:15 -0700695 flags |= SMALL;
696 if (field_width == -1) {
697 field_width = 2*sizeof(void *);
698 flags |= ZEROPAD;
699 }
700 return number(buf, end, (unsigned long) ptr, 16, field_width, precision, flags);
701}
702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703/**
704 * vsnprintf - Format a string and place it in a buffer
705 * @buf: The buffer to place the result into
706 * @size: The size of the buffer, including the trailing null space
707 * @fmt: The format string to use
708 * @args: Arguments for the format string
709 *
Andi Kleen20036fd2008-10-15 22:02:02 -0700710 * This function follows C99 vsnprintf, but has some extensions:
711 * %pS output the name of a text symbol
712 * %pF output the name of a function pointer
Linus Torvalds332d2e72008-10-20 15:07:34 +1100713 * %pR output the address range in a struct resource
Andi Kleen20036fd2008-10-15 22:02:02 -0700714 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 * The return value is the number of characters which would
716 * be generated for the given input, excluding the trailing
717 * '\0', as per ISO C99. If you want to have the exact
718 * number of characters written into @buf as return value
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800719 * (not including the trailing '\0'), use vscnprintf(). If the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 * return is greater than or equal to @size, the resulting
721 * string is truncated.
722 *
723 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800724 * You probably want snprintf() instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 */
726int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
727{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 unsigned long long num;
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700729 int base;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 char *str, *end, c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 int flags; /* flags to number() */
733
734 int field_width; /* width of output field */
735 int precision; /* min. # of digits for integers; max
736 number of chars for from string */
737 int qualifier; /* 'h', 'l', or 'L' for integer fields */
738 /* 'z' support added 23/7/1999 S.H. */
739 /* 'z' changed to 'Z' --davidm 1/25/99 */
Al Viro80322302005-08-23 22:48:17 +0100740 /* 't' added for ptrdiff_t */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700742 /* Reject out-of-range values early. Large positive sizes are
743 used for unknown buffer sizes. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 if (unlikely((int) size < 0)) {
745 /* There can be only one.. */
Denis Vlasenkob39a7342007-07-15 23:41:54 -0700746 static char warn = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 WARN_ON(warn);
748 warn = 0;
749 return 0;
750 }
751
752 str = buf;
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700753 end = buf + size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700755 /* Make sure end is always >= buf */
756 if (end < buf) {
757 end = ((void *)-1);
758 size = end - buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 }
760
761 for (; *fmt ; ++fmt) {
762 if (*fmt != '%') {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700763 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 *str = *fmt;
765 ++str;
766 continue;
767 }
768
769 /* process flags */
770 flags = 0;
771 repeat:
772 ++fmt; /* this also skips first '%' */
773 switch (*fmt) {
774 case '-': flags |= LEFT; goto repeat;
775 case '+': flags |= PLUS; goto repeat;
776 case ' ': flags |= SPACE; goto repeat;
777 case '#': flags |= SPECIAL; goto repeat;
778 case '0': flags |= ZEROPAD; goto repeat;
779 }
780
781 /* get field width */
782 field_width = -1;
783 if (isdigit(*fmt))
784 field_width = skip_atoi(&fmt);
785 else if (*fmt == '*') {
786 ++fmt;
787 /* it's the next argument */
788 field_width = va_arg(args, int);
789 if (field_width < 0) {
790 field_width = -field_width;
791 flags |= LEFT;
792 }
793 }
794
795 /* get the precision */
796 precision = -1;
797 if (*fmt == '.') {
798 ++fmt;
799 if (isdigit(*fmt))
800 precision = skip_atoi(&fmt);
801 else if (*fmt == '*') {
802 ++fmt;
803 /* it's the next argument */
804 precision = va_arg(args, int);
805 }
806 if (precision < 0)
807 precision = 0;
808 }
809
810 /* get the conversion qualifier */
811 qualifier = -1;
812 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
Al Viro80322302005-08-23 22:48:17 +0100813 *fmt =='Z' || *fmt == 'z' || *fmt == 't') {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 qualifier = *fmt;
815 ++fmt;
816 if (qualifier == 'l' && *fmt == 'l') {
817 qualifier = 'L';
818 ++fmt;
819 }
820 }
821
822 /* default base */
823 base = 10;
824
825 switch (*fmt) {
826 case 'c':
827 if (!(flags & LEFT)) {
828 while (--field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700829 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 *str = ' ';
831 ++str;
832 }
833 }
834 c = (unsigned char) va_arg(args, int);
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700835 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 *str = c;
837 ++str;
838 while (--field_width > 0) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700839 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 *str = ' ';
841 ++str;
842 }
843 continue;
844
845 case 's':
Linus Torvalds0f9bfa52008-07-06 16:06:25 -0700846 str = string(str, end, va_arg(args, char *), field_width, precision, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 continue;
848
849 case 'p':
Linus Torvalds4d8a7432008-07-06 16:24:57 -0700850 str = pointer(fmt+1, str, end,
851 va_arg(args, void *),
852 field_width, precision, flags);
853 /* Skip all alphanumeric pointer suffixes */
854 while (isalnum(fmt[1]))
855 fmt++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 continue;
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 case 'n':
859 /* FIXME:
860 * What does C99 say about the overflow case here? */
861 if (qualifier == 'l') {
862 long * ip = va_arg(args, long *);
863 *ip = (str - buf);
864 } else if (qualifier == 'Z' || qualifier == 'z') {
865 size_t * ip = va_arg(args, size_t *);
866 *ip = (str - buf);
867 } else {
868 int * ip = va_arg(args, int *);
869 *ip = (str - buf);
870 }
871 continue;
872
873 case '%':
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700874 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 *str = '%';
876 ++str;
877 continue;
878
879 /* integer number formats - set up the flags and "break" */
880 case 'o':
881 base = 8;
882 break;
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 case 'x':
Denys Vlasenko9b706ae2008-02-09 23:24:09 +0100885 flags |= SMALL;
886 case 'X':
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 base = 16;
888 break;
889
890 case 'd':
891 case 'i':
892 flags |= SIGN;
893 case 'u':
894 break;
895
896 default:
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700897 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 *str = '%';
899 ++str;
900 if (*fmt) {
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700901 if (str < end)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 *str = *fmt;
903 ++str;
904 } else {
905 --fmt;
906 }
907 continue;
908 }
909 if (qualifier == 'L')
910 num = va_arg(args, long long);
911 else if (qualifier == 'l') {
912 num = va_arg(args, unsigned long);
913 if (flags & SIGN)
914 num = (signed long) num;
915 } else if (qualifier == 'Z' || qualifier == 'z') {
916 num = va_arg(args, size_t);
Al Viro80322302005-08-23 22:48:17 +0100917 } else if (qualifier == 't') {
918 num = va_arg(args, ptrdiff_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 } else if (qualifier == 'h') {
920 num = (unsigned short) va_arg(args, int);
921 if (flags & SIGN)
922 num = (signed short) num;
923 } else {
924 num = va_arg(args, unsigned int);
925 if (flags & SIGN)
926 num = (signed int) num;
927 }
928 str = number(str, end, num, base,
929 field_width, precision, flags);
930 }
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700931 if (size > 0) {
932 if (str < end)
933 *str = '\0';
934 else
Linus Torvalds0a6047e2006-06-28 17:09:34 -0700935 end[-1] = '\0';
Jeremy Fitzhardingef7969372006-06-25 05:49:17 -0700936 }
937 /* the trailing null byte doesn't count towards the total */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return str-buf;
939}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940EXPORT_SYMBOL(vsnprintf);
941
942/**
943 * vscnprintf - Format a string and place it in a buffer
944 * @buf: The buffer to place the result into
945 * @size: The size of the buffer, including the trailing null space
946 * @fmt: The format string to use
947 * @args: Arguments for the format string
948 *
949 * The return value is the number of characters which have been written into
950 * the @buf not including the trailing '\0'. If @size is <= 0 the function
951 * returns 0.
952 *
953 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -0800954 * You probably want scnprintf() instead.
Andi Kleen20036fd2008-10-15 22:02:02 -0700955 *
956 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 */
958int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
959{
960 int i;
961
962 i=vsnprintf(buf,size,fmt,args);
963 return (i >= size) ? (size - 1) : i;
964}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965EXPORT_SYMBOL(vscnprintf);
966
967/**
968 * snprintf - Format a string and place it in a buffer
969 * @buf: The buffer to place the result into
970 * @size: The size of the buffer, including the trailing null space
971 * @fmt: The format string to use
972 * @...: Arguments for the format string
973 *
974 * The return value is the number of characters which would be
975 * generated for the given input, excluding the trailing null,
976 * as per ISO C99. If the return is greater than or equal to
977 * @size, the resulting string is truncated.
Andi Kleen20036fd2008-10-15 22:02:02 -0700978 *
979 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 */
981int snprintf(char * buf, size_t size, const char *fmt, ...)
982{
983 va_list args;
984 int i;
985
986 va_start(args, fmt);
987 i=vsnprintf(buf,size,fmt,args);
988 va_end(args);
989 return i;
990}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991EXPORT_SYMBOL(snprintf);
992
993/**
994 * scnprintf - Format a string and place it in a buffer
995 * @buf: The buffer to place the result into
996 * @size: The size of the buffer, including the trailing null space
997 * @fmt: The format string to use
998 * @...: Arguments for the format string
999 *
1000 * The return value is the number of characters written into @buf not including
Martin Peschkeea6f3282007-02-12 00:51:56 -08001001 * the trailing '\0'. If @size is <= 0 the function returns 0.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 */
1003
1004int scnprintf(char * buf, size_t size, const char *fmt, ...)
1005{
1006 va_list args;
1007 int i;
1008
1009 va_start(args, fmt);
1010 i = vsnprintf(buf, size, fmt, args);
1011 va_end(args);
1012 return (i >= size) ? (size - 1) : i;
1013}
1014EXPORT_SYMBOL(scnprintf);
1015
1016/**
1017 * vsprintf - Format a string and place it in a buffer
1018 * @buf: The buffer to place the result into
1019 * @fmt: The format string to use
1020 * @args: Arguments for the format string
1021 *
1022 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001023 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 * buffer overflows.
1025 *
1026 * Call this function if you are already dealing with a va_list.
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001027 * You probably want sprintf() instead.
Andi Kleen20036fd2008-10-15 22:02:02 -07001028 *
1029 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 */
1031int vsprintf(char *buf, const char *fmt, va_list args)
1032{
1033 return vsnprintf(buf, INT_MAX, fmt, args);
1034}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035EXPORT_SYMBOL(vsprintf);
1036
1037/**
1038 * sprintf - Format a string and place it in a buffer
1039 * @buf: The buffer to place the result into
1040 * @fmt: The format string to use
1041 * @...: Arguments for the format string
1042 *
1043 * The function returns the number of characters written
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08001044 * into @buf. Use snprintf() or scnprintf() in order to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 * buffer overflows.
Andi Kleen20036fd2008-10-15 22:02:02 -07001046 *
1047 * See the vsnprintf() documentation for format string extensions over C99.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 */
1049int sprintf(char * buf, const char *fmt, ...)
1050{
1051 va_list args;
1052 int i;
1053
1054 va_start(args, fmt);
1055 i=vsnprintf(buf, INT_MAX, fmt, args);
1056 va_end(args);
1057 return i;
1058}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059EXPORT_SYMBOL(sprintf);
1060
Lai Jiangshan4370aa42009-03-06 17:21:46 +01001061#ifdef CONFIG_BINARY_PRINTF
1062/*
1063 * bprintf service:
1064 * vbin_printf() - VA arguments to binary data
1065 * bstr_printf() - Binary data to text string
1066 */
1067
1068/**
1069 * vbin_printf - Parse a format string and place args' binary value in a buffer
1070 * @bin_buf: The buffer to place args' binary value
1071 * @size: The size of the buffer(by words(32bits), not characters)
1072 * @fmt: The format string to use
1073 * @args: Arguments for the format string
1074 *
1075 * The format follows C99 vsnprintf, except %n is ignored, and its argument
1076 * is skiped.
1077 *
1078 * The return value is the number of words(32bits) which would be generated for
1079 * the given input.
1080 *
1081 * NOTE:
1082 * If the return value is greater than @size, the resulting bin_buf is NOT
1083 * valid for bstr_printf().
1084 */
1085int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
1086{
1087 char *str, *end;
1088 int qualifier;
1089
1090 str = (char *)bin_buf;
1091 end = (char *)(bin_buf + size);
1092
1093#define save_arg(type) \
1094do { \
1095 if (sizeof(type) == 8) { \
1096 unsigned long long value; \
1097 str = PTR_ALIGN(str, sizeof(u32)); \
1098 value = va_arg(args, unsigned long long); \
1099 if (str + sizeof(type) <= end) { \
1100 *(u32 *)str = *(u32 *)&value; \
1101 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
1102 } \
1103 } else { \
1104 unsigned long value; \
1105 str = PTR_ALIGN(str, sizeof(type)); \
1106 value = va_arg(args, int); \
1107 if (str + sizeof(type) <= end) \
1108 *(typeof(type) *)str = (type)value; \
1109 } \
1110 str += sizeof(type); \
1111} while (0)
1112
1113 for (; *fmt ; ++fmt) {
1114 if (*fmt != '%')
1115 continue;
1116
1117repeat:
1118 /* parse flags */
1119 ++fmt; /* this also skips first '%' */
1120 if (*fmt == '-' || *fmt == '+' || *fmt == ' '
1121 || *fmt == '#' || *fmt == '0')
1122 goto repeat;
1123
1124 /* parse field width */
1125 if (isdigit(*fmt))
1126 skip_atoi(&fmt);
1127 else if (*fmt == '*') {
1128 ++fmt;
1129 /* it's the next argument */
1130 save_arg(int);
1131 }
1132
1133 /* parse the precision */
1134 if (*fmt == '.') {
1135 ++fmt;
1136 if (isdigit(*fmt))
1137 skip_atoi(&fmt);
1138 else if (*fmt == '*') {
1139 ++fmt;
1140 /* it's the next argument */
1141 save_arg(int);
1142 }
1143 }
1144
1145 /* parse the conversion qualifier */
1146 qualifier = -1;
1147 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1148 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
1149 qualifier = *fmt;
1150 ++fmt;
1151 if (qualifier == 'l' && *fmt == 'l') {
1152 qualifier = 'L';
1153 ++fmt;
1154 }
1155 }
1156
1157 /* parse format type */
1158 switch (*fmt) {
1159 case 'c':
1160 save_arg(char);
1161 continue;
1162 case 's': {
1163 /* save the string argument */
1164 const char *save_str = va_arg(args, char *);
1165 size_t len;
1166 if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
1167 || (unsigned long)save_str < PAGE_SIZE)
1168 save_str = "<NULL>";
1169 len = strlen(save_str);
1170 if (str + len + 1 < end)
1171 memcpy(str, save_str, len + 1);
1172 str += len + 1;
1173 continue;
1174 }
1175 case 'p':
1176 save_arg(void *);
1177 /* skip all alphanumeric pointer suffixes */
1178 while (isalnum(fmt[1]))
1179 fmt++;
1180 continue;
1181 case 'n': {
1182 /* skip %n 's argument */
1183 void *skip_arg;
1184 if (qualifier == 'l')
1185 skip_arg = va_arg(args, long *);
1186 else if (qualifier == 'Z' || qualifier == 'z')
1187 skip_arg = va_arg(args, size_t *);
1188 else
1189 skip_arg = va_arg(args, int *);
1190 continue;
1191 }
1192 case 'o':
1193 case 'x':
1194 case 'X':
1195 case 'd':
1196 case 'i':
1197 case 'u':
1198 /* save arg for case: 'o', 'x', 'X', 'd', 'i', 'u' */
1199 if (qualifier == 'L')
1200 save_arg(long long);
1201 else if (qualifier == 'l')
1202 save_arg(unsigned long);
1203 else if (qualifier == 'Z' || qualifier == 'z')
1204 save_arg(size_t);
1205 else if (qualifier == 't')
1206 save_arg(ptrdiff_t);
1207 else if (qualifier == 'h')
1208 save_arg(short);
1209 else
1210 save_arg(int);
1211 continue;
1212 default:
1213 if (!*fmt)
1214 --fmt;
1215 continue;
1216 }
1217 }
1218#undef save_arg
1219
1220 return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
1221}
1222EXPORT_SYMBOL_GPL(vbin_printf);
1223
1224/**
1225 * bstr_printf - Format a string from binary arguments and place it in a buffer
1226 * @buf: The buffer to place the result into
1227 * @size: The size of the buffer, including the trailing null space
1228 * @fmt: The format string to use
1229 * @bin_buf: Binary arguments for the format string
1230 *
1231 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
1232 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
1233 * a binary buffer that generated by vbin_printf.
1234 *
1235 * The format follows C99 vsnprintf, but has some extensions:
1236 * %pS output the name of a text symbol
1237 * %pF output the name of a function pointer
1238 * %pR output the address range in a struct resource
1239 * %n is ignored
1240 *
1241 * The return value is the number of characters which would
1242 * be generated for the given input, excluding the trailing
1243 * '\0', as per ISO C99. If you want to have the exact
1244 * number of characters written into @buf as return value
1245 * (not including the trailing '\0'), use vscnprintf(). If the
1246 * return is greater than or equal to @size, the resulting
1247 * string is truncated.
1248 */
1249int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
1250{
1251 unsigned long long num;
1252 int base;
1253 char *str, *end, c;
1254 const char *args = (const char *)bin_buf;
1255
1256 int flags;
1257 int field_width;
1258 int precision;
1259 int qualifier;
1260
1261 if (unlikely((int) size < 0)) {
1262 /* There can be only one.. */
1263 static char warn = 1;
1264 WARN_ON(warn);
1265 warn = 0;
1266 return 0;
1267 }
1268
1269 str = buf;
1270 end = buf + size;
1271
1272#define get_arg(type) \
1273({ \
1274 typeof(type) value; \
1275 if (sizeof(type) == 8) { \
1276 args = PTR_ALIGN(args, sizeof(u32)); \
1277 *(u32 *)&value = *(u32 *)args; \
1278 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
1279 } else { \
1280 args = PTR_ALIGN(args, sizeof(type)); \
1281 value = *(typeof(type) *)args; \
1282 } \
1283 args += sizeof(type); \
1284 value; \
1285})
1286
1287 /* Make sure end is always >= buf */
1288 if (end < buf) {
1289 end = ((void *)-1);
1290 size = end - buf;
1291 }
1292
1293 for (; *fmt ; ++fmt) {
1294 if (*fmt != '%') {
1295 if (str < end)
1296 *str = *fmt;
1297 ++str;
1298 continue;
1299 }
1300
1301 /* process flags */
1302 flags = 0;
1303repeat:
1304 ++fmt; /* this also skips first '%' */
1305 switch (*fmt) {
1306 case '-':
1307 flags |= LEFT;
1308 goto repeat;
1309 case '+':
1310 flags |= PLUS;
1311 goto repeat;
1312 case ' ':
1313 flags |= SPACE;
1314 goto repeat;
1315 case '#':
1316 flags |= SPECIAL;
1317 goto repeat;
1318 case '0':
1319 flags |= ZEROPAD;
1320 goto repeat;
1321 }
1322
1323 /* get field width */
1324 field_width = -1;
1325 if (isdigit(*fmt))
1326 field_width = skip_atoi(&fmt);
1327 else if (*fmt == '*') {
1328 ++fmt;
1329 /* it's the next argument */
1330 field_width = get_arg(int);
1331 if (field_width < 0) {
1332 field_width = -field_width;
1333 flags |= LEFT;
1334 }
1335 }
1336
1337 /* get the precision */
1338 precision = -1;
1339 if (*fmt == '.') {
1340 ++fmt;
1341 if (isdigit(*fmt))
1342 precision = skip_atoi(&fmt);
1343 else if (*fmt == '*') {
1344 ++fmt;
1345 /* it's the next argument */
1346 precision = get_arg(int);
1347 }
1348 if (precision < 0)
1349 precision = 0;
1350 }
1351
1352 /* get the conversion qualifier */
1353 qualifier = -1;
1354 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1355 *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
1356 qualifier = *fmt;
1357 ++fmt;
1358 if (qualifier == 'l' && *fmt == 'l') {
1359 qualifier = 'L';
1360 ++fmt;
1361 }
1362 }
1363
1364 /* default base */
1365 base = 10;
1366
1367 switch (*fmt) {
1368 case 'c':
1369 if (!(flags & LEFT)) {
1370 while (--field_width > 0) {
1371 if (str < end)
1372 *str = ' ';
1373 ++str;
1374 }
1375 }
1376 c = (unsigned char) get_arg(char);
1377 if (str < end)
1378 *str = c;
1379 ++str;
1380 while (--field_width > 0) {
1381 if (str < end)
1382 *str = ' ';
1383 ++str;
1384 }
1385 continue;
1386
1387 case 's':{
1388 const char *str_arg = args;
1389 size_t len = strlen(str_arg);
1390 args += len + 1;
1391 str = string(str, end, (char *)str_arg, field_width,
1392 precision, flags);
1393 continue;
1394 }
1395
1396 case 'p':
1397 str = pointer(fmt+1, str, end, get_arg(void *),
1398 field_width, precision, flags);
1399 /* Skip all alphanumeric pointer suffixes */
1400 while (isalnum(fmt[1]))
1401 fmt++;
1402 continue;
1403
1404 case 'n':
1405 /* skip %n */
1406 continue;
1407
1408 case '%':
1409 if (str < end)
1410 *str = '%';
1411 ++str;
1412 continue;
1413
1414 /* integer number formats - set up the flags and "break" */
1415 case 'o':
1416 base = 8;
1417 break;
1418
1419 case 'x':
1420 flags |= SMALL;
1421 case 'X':
1422 base = 16;
1423 break;
1424
1425 case 'd':
1426 case 'i':
1427 flags |= SIGN;
1428 case 'u':
1429 break;
1430
1431 default:
1432 if (str < end)
1433 *str = '%';
1434 ++str;
1435 if (*fmt) {
1436 if (str < end)
1437 *str = *fmt;
1438 ++str;
1439 } else {
1440 --fmt;
1441 }
1442 continue;
1443 }
1444 if (qualifier == 'L')
1445 num = get_arg(long long);
1446 else if (qualifier == 'l') {
1447 num = get_arg(unsigned long);
1448 if (flags & SIGN)
1449 num = (signed long) num;
1450 } else if (qualifier == 'Z' || qualifier == 'z') {
1451 num = get_arg(size_t);
1452 } else if (qualifier == 't') {
1453 num = get_arg(ptrdiff_t);
1454 } else if (qualifier == 'h') {
1455 num = (unsigned short) get_arg(short);
1456 if (flags & SIGN)
1457 num = (signed short) num;
1458 } else {
1459 num = get_arg(unsigned int);
1460 if (flags & SIGN)
1461 num = (signed int) num;
1462 }
1463 str = number(str, end, num, base,
1464 field_width, precision, flags);
1465 }
1466 if (size > 0) {
1467 if (str < end)
1468 *str = '\0';
1469 else
1470 end[-1] = '\0';
1471 }
1472#undef get_arg
1473
1474 /* the trailing null byte doesn't count towards the total */
1475 return str - buf;
1476}
1477EXPORT_SYMBOL_GPL(bstr_printf);
1478
1479/**
1480 * bprintf - Parse a format string and place args' binary value in a buffer
1481 * @bin_buf: The buffer to place args' binary value
1482 * @size: The size of the buffer(by words(32bits), not characters)
1483 * @fmt: The format string to use
1484 * @...: Arguments for the format string
1485 *
1486 * The function returns the number of words(u32) written
1487 * into @bin_buf.
1488 */
1489int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
1490{
1491 va_list args;
1492 int ret;
1493
1494 va_start(args, fmt);
1495 ret = vbin_printf(bin_buf, size, fmt, args);
1496 va_end(args);
1497 return ret;
1498}
1499EXPORT_SYMBOL_GPL(bprintf);
1500
1501#endif /* CONFIG_BINARY_PRINTF */
1502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503/**
1504 * vsscanf - Unformat a buffer into a list of arguments
1505 * @buf: input buffer
1506 * @fmt: format of buffer
1507 * @args: arguments
1508 */
1509int vsscanf(const char * buf, const char * fmt, va_list args)
1510{
1511 const char *str = buf;
1512 char *next;
1513 char digit;
1514 int num = 0;
1515 int qualifier;
1516 int base;
1517 int field_width;
1518 int is_sign = 0;
1519
1520 while(*fmt && *str) {
1521 /* skip any white space in format */
1522 /* white space in format matchs any amount of
1523 * white space, including none, in the input.
1524 */
1525 if (isspace(*fmt)) {
1526 while (isspace(*fmt))
1527 ++fmt;
1528 while (isspace(*str))
1529 ++str;
1530 }
1531
1532 /* anything that is not a conversion must match exactly */
1533 if (*fmt != '%' && *fmt) {
1534 if (*fmt++ != *str++)
1535 break;
1536 continue;
1537 }
1538
1539 if (!*fmt)
1540 break;
1541 ++fmt;
1542
1543 /* skip this conversion.
1544 * advance both strings to next white space
1545 */
1546 if (*fmt == '*') {
1547 while (!isspace(*fmt) && *fmt)
1548 fmt++;
1549 while (!isspace(*str) && *str)
1550 str++;
1551 continue;
1552 }
1553
1554 /* get field width */
1555 field_width = -1;
1556 if (isdigit(*fmt))
1557 field_width = skip_atoi(&fmt);
1558
1559 /* get conversion qualifier */
1560 qualifier = -1;
1561 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
1562 *fmt == 'Z' || *fmt == 'z') {
1563 qualifier = *fmt++;
1564 if (unlikely(qualifier == *fmt)) {
1565 if (qualifier == 'h') {
1566 qualifier = 'H';
1567 fmt++;
1568 } else if (qualifier == 'l') {
1569 qualifier = 'L';
1570 fmt++;
1571 }
1572 }
1573 }
1574 base = 10;
1575 is_sign = 0;
1576
1577 if (!*fmt || !*str)
1578 break;
1579
1580 switch(*fmt++) {
1581 case 'c':
1582 {
1583 char *s = (char *) va_arg(args,char*);
1584 if (field_width == -1)
1585 field_width = 1;
1586 do {
1587 *s++ = *str++;
1588 } while (--field_width > 0 && *str);
1589 num++;
1590 }
1591 continue;
1592 case 's':
1593 {
1594 char *s = (char *) va_arg(args, char *);
1595 if(field_width == -1)
1596 field_width = INT_MAX;
1597 /* first, skip leading white space in buffer */
1598 while (isspace(*str))
1599 str++;
1600
1601 /* now copy until next white space */
1602 while (*str && !isspace(*str) && field_width--) {
1603 *s++ = *str++;
1604 }
1605 *s = '\0';
1606 num++;
1607 }
1608 continue;
1609 case 'n':
1610 /* return number of characters read so far */
1611 {
1612 int *i = (int *)va_arg(args,int*);
1613 *i = str - buf;
1614 }
1615 continue;
1616 case 'o':
1617 base = 8;
1618 break;
1619 case 'x':
1620 case 'X':
1621 base = 16;
1622 break;
1623 case 'i':
1624 base = 0;
1625 case 'd':
1626 is_sign = 1;
1627 case 'u':
1628 break;
1629 case '%':
1630 /* looking for '%' in str */
1631 if (*str++ != '%')
1632 return num;
1633 continue;
1634 default:
1635 /* invalid format; stop here */
1636 return num;
1637 }
1638
1639 /* have some sort of integer conversion.
1640 * first, skip white space in buffer.
1641 */
1642 while (isspace(*str))
1643 str++;
1644
1645 digit = *str;
1646 if (is_sign && digit == '-')
1647 digit = *(str + 1);
1648
1649 if (!digit
1650 || (base == 16 && !isxdigit(digit))
1651 || (base == 10 && !isdigit(digit))
1652 || (base == 8 && (!isdigit(digit) || digit > '7'))
1653 || (base == 0 && !isdigit(digit)))
1654 break;
1655
1656 switch(qualifier) {
1657 case 'H': /* that's 'hh' in format */
1658 if (is_sign) {
1659 signed char *s = (signed char *) va_arg(args,signed char *);
1660 *s = (signed char) simple_strtol(str,&next,base);
1661 } else {
1662 unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
1663 *s = (unsigned char) simple_strtoul(str, &next, base);
1664 }
1665 break;
1666 case 'h':
1667 if (is_sign) {
1668 short *s = (short *) va_arg(args,short *);
1669 *s = (short) simple_strtol(str,&next,base);
1670 } else {
1671 unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
1672 *s = (unsigned short) simple_strtoul(str, &next, base);
1673 }
1674 break;
1675 case 'l':
1676 if (is_sign) {
1677 long *l = (long *) va_arg(args,long *);
1678 *l = simple_strtol(str,&next,base);
1679 } else {
1680 unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
1681 *l = simple_strtoul(str,&next,base);
1682 }
1683 break;
1684 case 'L':
1685 if (is_sign) {
1686 long long *l = (long long*) va_arg(args,long long *);
1687 *l = simple_strtoll(str,&next,base);
1688 } else {
1689 unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
1690 *l = simple_strtoull(str,&next,base);
1691 }
1692 break;
1693 case 'Z':
1694 case 'z':
1695 {
1696 size_t *s = (size_t*) va_arg(args,size_t*);
1697 *s = (size_t) simple_strtoul(str,&next,base);
1698 }
1699 break;
1700 default:
1701 if (is_sign) {
1702 int *i = (int *) va_arg(args, int*);
1703 *i = (int) simple_strtol(str,&next,base);
1704 } else {
1705 unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
1706 *i = (unsigned int) simple_strtoul(str,&next,base);
1707 }
1708 break;
1709 }
1710 num++;
1711
1712 if (!next)
1713 break;
1714 str = next;
1715 }
Johannes Bergc6b40d12007-05-08 00:27:20 -07001716
1717 /*
1718 * Now we've come all the way through so either the input string or the
1719 * format ended. In the former case, there can be a %n at the current
1720 * position in the format that needs to be filled.
1721 */
1722 if (*fmt == '%' && *(fmt + 1) == 'n') {
1723 int *p = (int *)va_arg(args, int *);
1724 *p = str - buf;
1725 }
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 return num;
1728}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729EXPORT_SYMBOL(vsscanf);
1730
1731/**
1732 * sscanf - Unformat a buffer into a list of arguments
1733 * @buf: input buffer
1734 * @fmt: formatting of buffer
1735 * @...: resulting arguments
1736 */
1737int sscanf(const char * buf, const char * fmt, ...)
1738{
1739 va_list args;
1740 int i;
1741
1742 va_start(args,fmt);
1743 i = vsscanf(buf,fmt,args);
1744 va_end(args);
1745 return i;
1746}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747EXPORT_SYMBOL(sscanf);