11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/lib/vsprintf.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */ 81da177e4SLinus Torvalds /* 91da177e4SLinus Torvalds * Wirzenius wrote this portably, Torvalds fucked it up :-) 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds /* 131da177e4SLinus Torvalds * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com> 141da177e4SLinus Torvalds * - changed to provide snprintf and vsnprintf functions 151da177e4SLinus Torvalds * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de> 161da177e4SLinus Torvalds * - scnprintf and vscnprintf 171da177e4SLinus Torvalds */ 181da177e4SLinus Torvalds 191da177e4SLinus Torvalds #include <stdarg.h> 208bc3bcc9SPaul Gortmaker #include <linux/module.h> /* for KSYM_SYMBOL_LEN */ 211da177e4SLinus Torvalds #include <linux/types.h> 221da177e4SLinus Torvalds #include <linux/string.h> 231da177e4SLinus Torvalds #include <linux/ctype.h> 241da177e4SLinus Torvalds #include <linux/kernel.h> 250fe1ef24SLinus Torvalds #include <linux/kallsyms.h> 260fe1ef24SLinus Torvalds #include <linux/uaccess.h> 27332d2e78SLinus Torvalds #include <linux/ioport.h> 288a27f7c9SJoe Perches #include <net/addrconf.h> 291da177e4SLinus Torvalds 304e57b681STim Schmielau #include <asm/page.h> /* for PAGE_SIZE */ 311da177e4SLinus Torvalds #include <asm/div64.h> 32deac93dfSJames Bottomley #include <asm/sections.h> /* for dereference_function_descriptor() */ 331da177e4SLinus Torvalds 341dff46d6SAlexey Dobriyan #include "kstrtox.h" 35aa46a63eSHarvey Harrison 361da177e4SLinus Torvalds /** 371da177e4SLinus Torvalds * simple_strtoull - convert a string to an unsigned long long 381da177e4SLinus Torvalds * @cp: The start of the string 391da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 401da177e4SLinus Torvalds * @base: The number base to use 411da177e4SLinus Torvalds */ 421da177e4SLinus Torvalds unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) 431da177e4SLinus Torvalds { 441dff46d6SAlexey Dobriyan unsigned long long result; 451dff46d6SAlexey Dobriyan unsigned int rv; 461da177e4SLinus Torvalds 471dff46d6SAlexey Dobriyan cp = _parse_integer_fixup_radix(cp, &base); 481dff46d6SAlexey Dobriyan rv = _parse_integer(cp, base, &result); 491dff46d6SAlexey Dobriyan /* FIXME */ 501dff46d6SAlexey Dobriyan cp += (rv & ~KSTRTOX_OVERFLOW); 51aa46a63eSHarvey Harrison 521da177e4SLinus Torvalds if (endp) 531da177e4SLinus Torvalds *endp = (char *)cp; 547b9186f5SAndré Goddard Rosa 551da177e4SLinus Torvalds return result; 561da177e4SLinus Torvalds } 571da177e4SLinus Torvalds EXPORT_SYMBOL(simple_strtoull); 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds /** 60922ac25cSAndré Goddard Rosa * simple_strtoul - convert a string to an unsigned long 61922ac25cSAndré Goddard Rosa * @cp: The start of the string 62922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 63922ac25cSAndré Goddard Rosa * @base: The number base to use 64922ac25cSAndré Goddard Rosa */ 65922ac25cSAndré Goddard Rosa unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) 66922ac25cSAndré Goddard Rosa { 67922ac25cSAndré Goddard Rosa return simple_strtoull(cp, endp, base); 68922ac25cSAndré Goddard Rosa } 69922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtoul); 70922ac25cSAndré Goddard Rosa 71922ac25cSAndré Goddard Rosa /** 72922ac25cSAndré Goddard Rosa * simple_strtol - convert a string to a signed long 73922ac25cSAndré Goddard Rosa * @cp: The start of the string 74922ac25cSAndré Goddard Rosa * @endp: A pointer to the end of the parsed string will be placed here 75922ac25cSAndré Goddard Rosa * @base: The number base to use 76922ac25cSAndré Goddard Rosa */ 77922ac25cSAndré Goddard Rosa long simple_strtol(const char *cp, char **endp, unsigned int base) 78922ac25cSAndré Goddard Rosa { 79922ac25cSAndré Goddard Rosa if (*cp == '-') 80922ac25cSAndré Goddard Rosa return -simple_strtoul(cp + 1, endp, base); 81922ac25cSAndré Goddard Rosa 82922ac25cSAndré Goddard Rosa return simple_strtoul(cp, endp, base); 83922ac25cSAndré Goddard Rosa } 84922ac25cSAndré Goddard Rosa EXPORT_SYMBOL(simple_strtol); 85922ac25cSAndré Goddard Rosa 86922ac25cSAndré Goddard Rosa /** 871da177e4SLinus Torvalds * simple_strtoll - convert a string to a signed long long 881da177e4SLinus Torvalds * @cp: The start of the string 891da177e4SLinus Torvalds * @endp: A pointer to the end of the parsed string will be placed here 901da177e4SLinus Torvalds * @base: The number base to use 911da177e4SLinus Torvalds */ 921da177e4SLinus Torvalds long long simple_strtoll(const char *cp, char **endp, unsigned int base) 931da177e4SLinus Torvalds { 941da177e4SLinus Torvalds if (*cp == '-') 951da177e4SLinus Torvalds return -simple_strtoull(cp + 1, endp, base); 967b9186f5SAndré Goddard Rosa 971da177e4SLinus Torvalds return simple_strtoull(cp, endp, base); 981da177e4SLinus Torvalds } 9998d5ce0dSHans Verkuil EXPORT_SYMBOL(simple_strtoll); 1001da177e4SLinus Torvalds 101cf3b429bSJoe Perches static noinline_for_stack 102cf3b429bSJoe Perches int skip_atoi(const char **s) 1031da177e4SLinus Torvalds { 1041da177e4SLinus Torvalds int i = 0; 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds while (isdigit(**s)) 1071da177e4SLinus Torvalds i = i*10 + *((*s)++) - '0'; 1087b9186f5SAndré Goddard Rosa 1091da177e4SLinus Torvalds return i; 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds 1124277eeddSDenis Vlasenko /* Decimal conversion is by far the most typical, and is used 1134277eeddSDenis Vlasenko * for /proc and /sys data. This directly impacts e.g. top performance 1144277eeddSDenis Vlasenko * with many processes running. We optimize it for speed 115*133fd9f5SDenys Vlasenko * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html> 116*133fd9f5SDenys Vlasenko * (with permission from the author, Douglas W. Jones). 117*133fd9f5SDenys Vlasenko */ 1184277eeddSDenis Vlasenko 119*133fd9f5SDenys Vlasenko #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64 120*133fd9f5SDenys Vlasenko /* Formats correctly any integer in [0, 999999999] */ 121cf3b429bSJoe Perches static noinline_for_stack 122*133fd9f5SDenys Vlasenko char *put_dec_full9(char *buf, unsigned q) 1234277eeddSDenis Vlasenko { 124*133fd9f5SDenys Vlasenko unsigned r; 1254277eeddSDenis Vlasenko 1267b9186f5SAndré Goddard Rosa /* 1277b9186f5SAndré Goddard Rosa * Possible ways to approx. divide by 10 128*133fd9f5SDenys Vlasenko * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit) 129*133fd9f5SDenys Vlasenko * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul) 130*133fd9f5SDenys Vlasenko * (x * 0x6667) >> 18 x < 43699 131*133fd9f5SDenys Vlasenko * (x * 0x3334) >> 17 x < 16389 132*133fd9f5SDenys Vlasenko * (x * 0x199a) >> 16 x < 16389 133*133fd9f5SDenys Vlasenko * (x * 0x0ccd) >> 15 x < 16389 134*133fd9f5SDenys Vlasenko * (x * 0x0667) >> 14 x < 2739 135*133fd9f5SDenys Vlasenko * (x * 0x0334) >> 13 x < 1029 136*133fd9f5SDenys Vlasenko * (x * 0x019a) >> 12 x < 1029 137*133fd9f5SDenys Vlasenko * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386) 138*133fd9f5SDenys Vlasenko * (x * 0x0067) >> 10 x < 179 139*133fd9f5SDenys Vlasenko * (x * 0x0034) >> 9 x < 69 same 140*133fd9f5SDenys Vlasenko * (x * 0x001a) >> 8 x < 69 same 141*133fd9f5SDenys Vlasenko * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386) 142*133fd9f5SDenys Vlasenko * (x * 0x0007) >> 6 x < 19 143*133fd9f5SDenys Vlasenko * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html> 1447b9186f5SAndré Goddard Rosa */ 145*133fd9f5SDenys Vlasenko r = (q * (uint64_t)0x1999999a) >> 32; 146*133fd9f5SDenys Vlasenko *buf++ = (q - 10 * r) + '0'; /* 1 */ 147*133fd9f5SDenys Vlasenko q = (r * (uint64_t)0x1999999a) >> 32; 148*133fd9f5SDenys Vlasenko *buf++ = (r - 10 * q) + '0'; /* 2 */ 149*133fd9f5SDenys Vlasenko r = (q * (uint64_t)0x1999999a) >> 32; 150*133fd9f5SDenys Vlasenko *buf++ = (q - 10 * r) + '0'; /* 3 */ 151*133fd9f5SDenys Vlasenko q = (r * (uint64_t)0x1999999a) >> 32; 152*133fd9f5SDenys Vlasenko *buf++ = (r - 10 * q) + '0'; /* 4 */ 153*133fd9f5SDenys Vlasenko r = (q * (uint64_t)0x1999999a) >> 32; 154*133fd9f5SDenys Vlasenko *buf++ = (q - 10 * r) + '0'; /* 5 */ 155*133fd9f5SDenys Vlasenko /* Now value is under 10000, can avoid 64-bit multiply */ 156*133fd9f5SDenys Vlasenko q = (r * 0x199a) >> 16; 157*133fd9f5SDenys Vlasenko *buf++ = (r - 10 * q) + '0'; /* 6 */ 158*133fd9f5SDenys Vlasenko r = (q * 0xcd) >> 11; 159*133fd9f5SDenys Vlasenko *buf++ = (q - 10 * r) + '0'; /* 7 */ 160*133fd9f5SDenys Vlasenko q = (r * 0xcd) >> 11; 161*133fd9f5SDenys Vlasenko *buf++ = (r - 10 * q) + '0'; /* 8 */ 162*133fd9f5SDenys Vlasenko *buf++ = q + '0'; /* 9 */ 163*133fd9f5SDenys Vlasenko return buf; 164*133fd9f5SDenys Vlasenko } 165*133fd9f5SDenys Vlasenko #endif 1664277eeddSDenis Vlasenko 167*133fd9f5SDenys Vlasenko /* Similar to above but do not pad with zeros. 168*133fd9f5SDenys Vlasenko * Code can be easily arranged to print 9 digits too, but our callers 169*133fd9f5SDenys Vlasenko * always call put_dec_full9() instead when the number has 9 decimal digits. 170*133fd9f5SDenys Vlasenko */ 171*133fd9f5SDenys Vlasenko static noinline_for_stack 172*133fd9f5SDenys Vlasenko char *put_dec_trunc8(char *buf, unsigned r) 173*133fd9f5SDenys Vlasenko { 174*133fd9f5SDenys Vlasenko unsigned q; 1754277eeddSDenis Vlasenko 176*133fd9f5SDenys Vlasenko /* Copy of previous function's body with added early returns */ 177*133fd9f5SDenys Vlasenko q = (r * (uint64_t)0x1999999a) >> 32; 178*133fd9f5SDenys Vlasenko *buf++ = (r - 10 * q) + '0'; /* 2 */ 179*133fd9f5SDenys Vlasenko if (q == 0) 180*133fd9f5SDenys Vlasenko return buf; 181*133fd9f5SDenys Vlasenko r = (q * (uint64_t)0x1999999a) >> 32; 182*133fd9f5SDenys Vlasenko *buf++ = (q - 10 * r) + '0'; /* 3 */ 183*133fd9f5SDenys Vlasenko if (r == 0) 184*133fd9f5SDenys Vlasenko return buf; 185*133fd9f5SDenys Vlasenko q = (r * (uint64_t)0x1999999a) >> 32; 186*133fd9f5SDenys Vlasenko *buf++ = (r - 10 * q) + '0'; /* 4 */ 187*133fd9f5SDenys Vlasenko if (q == 0) 188*133fd9f5SDenys Vlasenko return buf; 189*133fd9f5SDenys Vlasenko r = (q * (uint64_t)0x1999999a) >> 32; 190*133fd9f5SDenys Vlasenko *buf++ = (q - 10 * r) + '0'; /* 5 */ 191*133fd9f5SDenys Vlasenko if (r == 0) 192*133fd9f5SDenys Vlasenko return buf; 193*133fd9f5SDenys Vlasenko q = (r * 0x199a) >> 16; 194*133fd9f5SDenys Vlasenko *buf++ = (r - 10 * q) + '0'; /* 6 */ 195*133fd9f5SDenys Vlasenko if (q == 0) 196*133fd9f5SDenys Vlasenko return buf; 197*133fd9f5SDenys Vlasenko r = (q * 0xcd) >> 11; 198*133fd9f5SDenys Vlasenko *buf++ = (q - 10 * r) + '0'; /* 7 */ 199*133fd9f5SDenys Vlasenko if (r == 0) 200*133fd9f5SDenys Vlasenko return buf; 201*133fd9f5SDenys Vlasenko q = (r * 0xcd) >> 11; 202*133fd9f5SDenys Vlasenko *buf++ = (r - 10 * q) + '0'; /* 8 */ 203*133fd9f5SDenys Vlasenko if (q == 0) 204*133fd9f5SDenys Vlasenko return buf; 205*133fd9f5SDenys Vlasenko *buf++ = q + '0'; /* 9 */ 206*133fd9f5SDenys Vlasenko return buf; 207*133fd9f5SDenys Vlasenko } 208*133fd9f5SDenys Vlasenko 209*133fd9f5SDenys Vlasenko /* There are two algorithms to print larger numbers. 210*133fd9f5SDenys Vlasenko * One is generic: divide by 1000000000 and repeatedly print 211*133fd9f5SDenys Vlasenko * groups of (up to) 9 digits. It's conceptually simple, 212*133fd9f5SDenys Vlasenko * but requires a (unsigned long long) / 1000000000 division. 213*133fd9f5SDenys Vlasenko * 214*133fd9f5SDenys Vlasenko * Second algorithm splits 64-bit unsigned long long into 16-bit chunks, 215*133fd9f5SDenys Vlasenko * manipulates them cleverly and generates groups of 4 decimal digits. 216*133fd9f5SDenys Vlasenko * It so happens that it does NOT require long long division. 217*133fd9f5SDenys Vlasenko * 218*133fd9f5SDenys Vlasenko * If long is > 32 bits, division of 64-bit values is relatively easy, 219*133fd9f5SDenys Vlasenko * and we will use the first algorithm. 220*133fd9f5SDenys Vlasenko * If long long is > 64 bits (strange architecture with VERY large long long), 221*133fd9f5SDenys Vlasenko * second algorithm can't be used, and we again use the first one. 222*133fd9f5SDenys Vlasenko * 223*133fd9f5SDenys Vlasenko * Else (if long is 32 bits and long long is 64 bits) we use second one. 224*133fd9f5SDenys Vlasenko */ 225*133fd9f5SDenys Vlasenko 226*133fd9f5SDenys Vlasenko #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64 227*133fd9f5SDenys Vlasenko 228*133fd9f5SDenys Vlasenko /* First algorithm: generic */ 229*133fd9f5SDenys Vlasenko 230*133fd9f5SDenys Vlasenko static 231*133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 232*133fd9f5SDenys Vlasenko { 233*133fd9f5SDenys Vlasenko if (n >= 100*1000*1000) { 234*133fd9f5SDenys Vlasenko while (n >= 1000*1000*1000) 235*133fd9f5SDenys Vlasenko buf = put_dec_full9(buf, do_div(n, 1000*1000*1000)); 236*133fd9f5SDenys Vlasenko if (n >= 100*1000*1000) 237*133fd9f5SDenys Vlasenko return put_dec_full9(buf, n); 238*133fd9f5SDenys Vlasenko } 239*133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 240*133fd9f5SDenys Vlasenko } 241*133fd9f5SDenys Vlasenko 242*133fd9f5SDenys Vlasenko #else 243*133fd9f5SDenys Vlasenko 244*133fd9f5SDenys Vlasenko /* Second algorithm: valid only for 64-bit long longs */ 245*133fd9f5SDenys Vlasenko 246*133fd9f5SDenys Vlasenko static noinline_for_stack 247*133fd9f5SDenys Vlasenko char *put_dec_full4(char *buf, unsigned q) 248*133fd9f5SDenys Vlasenko { 249*133fd9f5SDenys Vlasenko unsigned r; 250*133fd9f5SDenys Vlasenko r = (q * 0xcccd) >> 19; 251*133fd9f5SDenys Vlasenko *buf++ = (q - 10 * r) + '0'; 252*133fd9f5SDenys Vlasenko q = (r * 0x199a) >> 16; 253*133fd9f5SDenys Vlasenko *buf++ = (r - 10 * q) + '0'; 254*133fd9f5SDenys Vlasenko r = (q * 0xcd) >> 11; 255*133fd9f5SDenys Vlasenko *buf++ = (q - 10 * r) + '0'; 256*133fd9f5SDenys Vlasenko *buf++ = r + '0'; 257*133fd9f5SDenys Vlasenko return buf; 258*133fd9f5SDenys Vlasenko } 259*133fd9f5SDenys Vlasenko 260*133fd9f5SDenys Vlasenko /* Based on code by Douglas W. Jones found at 261*133fd9f5SDenys Vlasenko * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour> 262*133fd9f5SDenys Vlasenko * (with permission from the author). 263*133fd9f5SDenys Vlasenko * Performs no 64-bit division and hence should be fast on 32-bit machines. 264*133fd9f5SDenys Vlasenko */ 265*133fd9f5SDenys Vlasenko static 266*133fd9f5SDenys Vlasenko char *put_dec(char *buf, unsigned long long n) 267*133fd9f5SDenys Vlasenko { 268*133fd9f5SDenys Vlasenko uint32_t d3, d2, d1, q, h; 269*133fd9f5SDenys Vlasenko 270*133fd9f5SDenys Vlasenko if (n < 100*1000*1000) 271*133fd9f5SDenys Vlasenko return put_dec_trunc8(buf, n); 272*133fd9f5SDenys Vlasenko 273*133fd9f5SDenys Vlasenko d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */ 274*133fd9f5SDenys Vlasenko h = (n >> 32); 275*133fd9f5SDenys Vlasenko d2 = (h ) & 0xffff; 276*133fd9f5SDenys Vlasenko d3 = (h >> 16); /* implicit "& 0xffff" */ 277*133fd9f5SDenys Vlasenko 278*133fd9f5SDenys Vlasenko q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff); 279*133fd9f5SDenys Vlasenko 280*133fd9f5SDenys Vlasenko buf = put_dec_full4(buf, q % 10000); 281*133fd9f5SDenys Vlasenko q = q / 10000; 282*133fd9f5SDenys Vlasenko 283*133fd9f5SDenys Vlasenko d1 = q + 7671 * d3 + 9496 * d2 + 6 * d1; 284*133fd9f5SDenys Vlasenko buf = put_dec_full4(buf, d1 % 10000); 285*133fd9f5SDenys Vlasenko q = d1 / 10000; 286*133fd9f5SDenys Vlasenko 287*133fd9f5SDenys Vlasenko d2 = q + 4749 * d3 + 42 * d2; 288*133fd9f5SDenys Vlasenko buf = put_dec_full4(buf, d2 % 10000); 289*133fd9f5SDenys Vlasenko q = d2 / 10000; 290*133fd9f5SDenys Vlasenko 291*133fd9f5SDenys Vlasenko d3 = q + 281 * d3; 292*133fd9f5SDenys Vlasenko if (!d3) 293*133fd9f5SDenys Vlasenko goto done; 294*133fd9f5SDenys Vlasenko buf = put_dec_full4(buf, d3 % 10000); 295*133fd9f5SDenys Vlasenko q = d3 / 10000; 296*133fd9f5SDenys Vlasenko if (!q) 297*133fd9f5SDenys Vlasenko goto done; 298*133fd9f5SDenys Vlasenko buf = put_dec_full4(buf, q); 299*133fd9f5SDenys Vlasenko done: 300*133fd9f5SDenys Vlasenko while (buf[-1] == '0') 301*133fd9f5SDenys Vlasenko --buf; 3027b9186f5SAndré Goddard Rosa 3034277eeddSDenis Vlasenko return buf; 3044277eeddSDenis Vlasenko } 305*133fd9f5SDenys Vlasenko 306*133fd9f5SDenys Vlasenko #endif 3074277eeddSDenis Vlasenko 3081ac101a5SKAMEZAWA Hiroyuki /* 3091ac101a5SKAMEZAWA Hiroyuki * Convert passed number to decimal string. 3101ac101a5SKAMEZAWA Hiroyuki * Returns the length of string. On buffer overflow, returns 0. 3111ac101a5SKAMEZAWA Hiroyuki * 3121ac101a5SKAMEZAWA Hiroyuki * If speed is not important, use snprintf(). It's easy to read the code. 3131ac101a5SKAMEZAWA Hiroyuki */ 3141ac101a5SKAMEZAWA Hiroyuki int num_to_str(char *buf, int size, unsigned long long num) 3151ac101a5SKAMEZAWA Hiroyuki { 316*133fd9f5SDenys Vlasenko char tmp[sizeof(num) * 3]; 3171ac101a5SKAMEZAWA Hiroyuki int idx, len; 3181ac101a5SKAMEZAWA Hiroyuki 319*133fd9f5SDenys Vlasenko /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */ 320*133fd9f5SDenys Vlasenko if (num <= 9) { 321*133fd9f5SDenys Vlasenko tmp[0] = '0' + num; 322*133fd9f5SDenys Vlasenko len = 1; 323*133fd9f5SDenys Vlasenko } else { 3241ac101a5SKAMEZAWA Hiroyuki len = put_dec(tmp, num) - tmp; 325*133fd9f5SDenys Vlasenko } 3261ac101a5SKAMEZAWA Hiroyuki 3271ac101a5SKAMEZAWA Hiroyuki if (len > size) 3281ac101a5SKAMEZAWA Hiroyuki return 0; 3291ac101a5SKAMEZAWA Hiroyuki for (idx = 0; idx < len; ++idx) 3301ac101a5SKAMEZAWA Hiroyuki buf[idx] = tmp[len - idx - 1]; 3311ac101a5SKAMEZAWA Hiroyuki return len; 3321ac101a5SKAMEZAWA Hiroyuki } 3331ac101a5SKAMEZAWA Hiroyuki 3341da177e4SLinus Torvalds #define ZEROPAD 1 /* pad with zero */ 3351da177e4SLinus Torvalds #define SIGN 2 /* unsigned/signed long */ 3361da177e4SLinus Torvalds #define PLUS 4 /* show plus */ 3371da177e4SLinus Torvalds #define SPACE 8 /* space if plus */ 3381da177e4SLinus Torvalds #define LEFT 16 /* left justified */ 339b89dc5d6SBjorn Helgaas #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ 340b89dc5d6SBjorn Helgaas #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ 3411da177e4SLinus Torvalds 342fef20d9cSFrederic Weisbecker enum format_type { 343fef20d9cSFrederic Weisbecker FORMAT_TYPE_NONE, /* Just a string part */ 344ed681a91SVegard Nossum FORMAT_TYPE_WIDTH, 345fef20d9cSFrederic Weisbecker FORMAT_TYPE_PRECISION, 346fef20d9cSFrederic Weisbecker FORMAT_TYPE_CHAR, 347fef20d9cSFrederic Weisbecker FORMAT_TYPE_STR, 348fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTR, 349fef20d9cSFrederic Weisbecker FORMAT_TYPE_PERCENT_CHAR, 350fef20d9cSFrederic Weisbecker FORMAT_TYPE_INVALID, 351fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG_LONG, 352fef20d9cSFrederic Weisbecker FORMAT_TYPE_ULONG, 353fef20d9cSFrederic Weisbecker FORMAT_TYPE_LONG, 354a4e94ef0SZhaolei FORMAT_TYPE_UBYTE, 355a4e94ef0SZhaolei FORMAT_TYPE_BYTE, 356fef20d9cSFrederic Weisbecker FORMAT_TYPE_USHORT, 357fef20d9cSFrederic Weisbecker FORMAT_TYPE_SHORT, 358fef20d9cSFrederic Weisbecker FORMAT_TYPE_UINT, 359fef20d9cSFrederic Weisbecker FORMAT_TYPE_INT, 360fef20d9cSFrederic Weisbecker FORMAT_TYPE_NRCHARS, 361fef20d9cSFrederic Weisbecker FORMAT_TYPE_SIZE_T, 362fef20d9cSFrederic Weisbecker FORMAT_TYPE_PTRDIFF 363fef20d9cSFrederic Weisbecker }; 364fef20d9cSFrederic Weisbecker 365fef20d9cSFrederic Weisbecker struct printf_spec { 3664e310fdaSJoe Perches u8 type; /* format_type enum */ 367ef0658f3SJoe Perches u8 flags; /* flags to number() */ 3684e310fdaSJoe Perches u8 base; /* number base, 8, 10 or 16 only */ 3694e310fdaSJoe Perches u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */ 3704e310fdaSJoe Perches s16 field_width; /* width of output field */ 3714e310fdaSJoe Perches s16 precision; /* # of digits/chars */ 372fef20d9cSFrederic Weisbecker }; 373fef20d9cSFrederic Weisbecker 374cf3b429bSJoe Perches static noinline_for_stack 375cf3b429bSJoe Perches char *number(char *buf, char *end, unsigned long long num, 376fef20d9cSFrederic Weisbecker struct printf_spec spec) 3771da177e4SLinus Torvalds { 3789b706aeeSDenys Vlasenko /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ 3799b706aeeSDenys Vlasenko static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ 3809b706aeeSDenys Vlasenko 3819b706aeeSDenys Vlasenko char tmp[66]; 3829b706aeeSDenys Vlasenko char sign; 3839b706aeeSDenys Vlasenko char locase; 384fef20d9cSFrederic Weisbecker int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); 3851da177e4SLinus Torvalds int i; 3867c203422SPierre Carrier bool is_zero = num == 0LL; 3871da177e4SLinus Torvalds 3889b706aeeSDenys Vlasenko /* locase = 0 or 0x20. ORing digits or letters with 'locase' 3899b706aeeSDenys Vlasenko * produces same digits or (maybe lowercased) letters */ 390fef20d9cSFrederic Weisbecker locase = (spec.flags & SMALL); 391fef20d9cSFrederic Weisbecker if (spec.flags & LEFT) 392fef20d9cSFrederic Weisbecker spec.flags &= ~ZEROPAD; 3931da177e4SLinus Torvalds sign = 0; 394fef20d9cSFrederic Weisbecker if (spec.flags & SIGN) { 3951da177e4SLinus Torvalds if ((signed long long)num < 0) { 3961da177e4SLinus Torvalds sign = '-'; 3971da177e4SLinus Torvalds num = -(signed long long)num; 398fef20d9cSFrederic Weisbecker spec.field_width--; 399fef20d9cSFrederic Weisbecker } else if (spec.flags & PLUS) { 4001da177e4SLinus Torvalds sign = '+'; 401fef20d9cSFrederic Weisbecker spec.field_width--; 402fef20d9cSFrederic Weisbecker } else if (spec.flags & SPACE) { 4031da177e4SLinus Torvalds sign = ' '; 404fef20d9cSFrederic Weisbecker spec.field_width--; 4051da177e4SLinus Torvalds } 4061da177e4SLinus Torvalds } 407b39a7340SDenis Vlasenko if (need_pfx) { 408fef20d9cSFrederic Weisbecker if (spec.base == 16) 4097c203422SPierre Carrier spec.field_width -= 2; 4107c203422SPierre Carrier else if (!is_zero) 411fef20d9cSFrederic Weisbecker spec.field_width--; 4121da177e4SLinus Torvalds } 413b39a7340SDenis Vlasenko 414b39a7340SDenis Vlasenko /* generate full string in tmp[], in reverse order */ 4151da177e4SLinus Torvalds i = 0; 416*133fd9f5SDenys Vlasenko if (num < spec.base) 417*133fd9f5SDenys Vlasenko tmp[i++] = digits[num] | locase; 4184277eeddSDenis Vlasenko /* Generic code, for any base: 4194277eeddSDenis Vlasenko else do { 4209b706aeeSDenys Vlasenko tmp[i++] = (digits[do_div(num,base)] | locase); 4214277eeddSDenis Vlasenko } while (num != 0); 4224277eeddSDenis Vlasenko */ 423fef20d9cSFrederic Weisbecker else if (spec.base != 10) { /* 8 or 16 */ 424fef20d9cSFrederic Weisbecker int mask = spec.base - 1; 425b39a7340SDenis Vlasenko int shift = 3; 4267b9186f5SAndré Goddard Rosa 4277b9186f5SAndré Goddard Rosa if (spec.base == 16) 4287b9186f5SAndré Goddard Rosa shift = 4; 429b39a7340SDenis Vlasenko do { 4309b706aeeSDenys Vlasenko tmp[i++] = (digits[((unsigned char)num) & mask] | locase); 431b39a7340SDenis Vlasenko num >>= shift; 432b39a7340SDenis Vlasenko } while (num); 4334277eeddSDenis Vlasenko } else { /* base 10 */ 4344277eeddSDenis Vlasenko i = put_dec(tmp, num) - tmp; 4354277eeddSDenis Vlasenko } 436b39a7340SDenis Vlasenko 437b39a7340SDenis Vlasenko /* printing 100 using %2d gives "100", not "00" */ 438fef20d9cSFrederic Weisbecker if (i > spec.precision) 439fef20d9cSFrederic Weisbecker spec.precision = i; 440b39a7340SDenis Vlasenko /* leading space padding */ 441fef20d9cSFrederic Weisbecker spec.field_width -= spec.precision; 442fef20d9cSFrederic Weisbecker if (!(spec.flags & (ZEROPAD+LEFT))) { 443fef20d9cSFrederic Weisbecker while (--spec.field_width >= 0) { 444f796937aSJeremy Fitzhardinge if (buf < end) 4451da177e4SLinus Torvalds *buf = ' '; 4461da177e4SLinus Torvalds ++buf; 4471da177e4SLinus Torvalds } 4481da177e4SLinus Torvalds } 449b39a7340SDenis Vlasenko /* sign */ 4501da177e4SLinus Torvalds if (sign) { 451f796937aSJeremy Fitzhardinge if (buf < end) 4521da177e4SLinus Torvalds *buf = sign; 4531da177e4SLinus Torvalds ++buf; 4541da177e4SLinus Torvalds } 455b39a7340SDenis Vlasenko /* "0x" / "0" prefix */ 456b39a7340SDenis Vlasenko if (need_pfx) { 4577c203422SPierre Carrier if (spec.base == 16 || !is_zero) { 458f796937aSJeremy Fitzhardinge if (buf < end) 4591da177e4SLinus Torvalds *buf = '0'; 4601da177e4SLinus Torvalds ++buf; 4617c203422SPierre Carrier } 462fef20d9cSFrederic Weisbecker if (spec.base == 16) { 463f796937aSJeremy Fitzhardinge if (buf < end) 4649b706aeeSDenys Vlasenko *buf = ('X' | locase); 4651da177e4SLinus Torvalds ++buf; 4661da177e4SLinus Torvalds } 4671da177e4SLinus Torvalds } 468b39a7340SDenis Vlasenko /* zero or space padding */ 469fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 470fef20d9cSFrederic Weisbecker char c = (spec.flags & ZEROPAD) ? '0' : ' '; 471fef20d9cSFrederic Weisbecker while (--spec.field_width >= 0) { 472f796937aSJeremy Fitzhardinge if (buf < end) 4731da177e4SLinus Torvalds *buf = c; 4741da177e4SLinus Torvalds ++buf; 4751da177e4SLinus Torvalds } 4761da177e4SLinus Torvalds } 477b39a7340SDenis Vlasenko /* hmm even more zero padding? */ 478fef20d9cSFrederic Weisbecker while (i <= --spec.precision) { 479f796937aSJeremy Fitzhardinge if (buf < end) 4801da177e4SLinus Torvalds *buf = '0'; 4811da177e4SLinus Torvalds ++buf; 4821da177e4SLinus Torvalds } 483b39a7340SDenis Vlasenko /* actual digits of result */ 484b39a7340SDenis Vlasenko while (--i >= 0) { 485f796937aSJeremy Fitzhardinge if (buf < end) 4861da177e4SLinus Torvalds *buf = tmp[i]; 4871da177e4SLinus Torvalds ++buf; 4881da177e4SLinus Torvalds } 489b39a7340SDenis Vlasenko /* trailing space padding */ 490fef20d9cSFrederic Weisbecker while (--spec.field_width >= 0) { 491f796937aSJeremy Fitzhardinge if (buf < end) 4921da177e4SLinus Torvalds *buf = ' '; 4931da177e4SLinus Torvalds ++buf; 4941da177e4SLinus Torvalds } 4957b9186f5SAndré Goddard Rosa 4961da177e4SLinus Torvalds return buf; 4971da177e4SLinus Torvalds } 4981da177e4SLinus Torvalds 499cf3b429bSJoe Perches static noinline_for_stack 500cf3b429bSJoe Perches char *string(char *buf, char *end, const char *s, struct printf_spec spec) 5010f9bfa56SLinus Torvalds { 5020f9bfa56SLinus Torvalds int len, i; 5030f9bfa56SLinus Torvalds 5040f9bfa56SLinus Torvalds if ((unsigned long)s < PAGE_SIZE) 5050f4f81dcSAndré Goddard Rosa s = "(null)"; 5060f9bfa56SLinus Torvalds 507fef20d9cSFrederic Weisbecker len = strnlen(s, spec.precision); 5080f9bfa56SLinus Torvalds 509fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 510fef20d9cSFrederic Weisbecker while (len < spec.field_width--) { 5110f9bfa56SLinus Torvalds if (buf < end) 5120f9bfa56SLinus Torvalds *buf = ' '; 5130f9bfa56SLinus Torvalds ++buf; 5140f9bfa56SLinus Torvalds } 5150f9bfa56SLinus Torvalds } 5160f9bfa56SLinus Torvalds for (i = 0; i < len; ++i) { 5170f9bfa56SLinus Torvalds if (buf < end) 5180f9bfa56SLinus Torvalds *buf = *s; 5190f9bfa56SLinus Torvalds ++buf; ++s; 5200f9bfa56SLinus Torvalds } 521fef20d9cSFrederic Weisbecker while (len < spec.field_width--) { 5220f9bfa56SLinus Torvalds if (buf < end) 5230f9bfa56SLinus Torvalds *buf = ' '; 5240f9bfa56SLinus Torvalds ++buf; 5250f9bfa56SLinus Torvalds } 5267b9186f5SAndré Goddard Rosa 5270f9bfa56SLinus Torvalds return buf; 5280f9bfa56SLinus Torvalds } 5290f9bfa56SLinus Torvalds 530cf3b429bSJoe Perches static noinline_for_stack 531cf3b429bSJoe Perches char *symbol_string(char *buf, char *end, void *ptr, 5320c8b946eSFrederic Weisbecker struct printf_spec spec, char ext) 5330fe1ef24SLinus Torvalds { 5340fe1ef24SLinus Torvalds unsigned long value = (unsigned long) ptr; 5350fe1ef24SLinus Torvalds #ifdef CONFIG_KALLSYMS 5360fe1ef24SLinus Torvalds char sym[KSYM_SYMBOL_LEN]; 5370f77a8d3SNamhyung Kim if (ext == 'B') 5380f77a8d3SNamhyung Kim sprint_backtrace(sym, value); 5390f77a8d3SNamhyung Kim else if (ext != 'f' && ext != 's') 5400fe1ef24SLinus Torvalds sprint_symbol(sym, value); 5410c8b946eSFrederic Weisbecker else 5424796dd20SStephen Boyd sprint_symbol_no_offset(sym, value); 5437b9186f5SAndré Goddard Rosa 544fef20d9cSFrederic Weisbecker return string(buf, end, sym, spec); 5450fe1ef24SLinus Torvalds #else 546fef20d9cSFrederic Weisbecker spec.field_width = 2 * sizeof(void *); 547fef20d9cSFrederic Weisbecker spec.flags |= SPECIAL | SMALL | ZEROPAD; 548fef20d9cSFrederic Weisbecker spec.base = 16; 5497b9186f5SAndré Goddard Rosa 550fef20d9cSFrederic Weisbecker return number(buf, end, value, spec); 5510fe1ef24SLinus Torvalds #endif 5520fe1ef24SLinus Torvalds } 5530fe1ef24SLinus Torvalds 554cf3b429bSJoe Perches static noinline_for_stack 555cf3b429bSJoe Perches char *resource_string(char *buf, char *end, struct resource *res, 556fd95541eSBjorn Helgaas struct printf_spec spec, const char *fmt) 557332d2e78SLinus Torvalds { 558332d2e78SLinus Torvalds #ifndef IO_RSRC_PRINTK_SIZE 55928405372SBjorn Helgaas #define IO_RSRC_PRINTK_SIZE 6 560332d2e78SLinus Torvalds #endif 561332d2e78SLinus Torvalds 562332d2e78SLinus Torvalds #ifndef MEM_RSRC_PRINTK_SIZE 56328405372SBjorn Helgaas #define MEM_RSRC_PRINTK_SIZE 10 564332d2e78SLinus Torvalds #endif 5654da0b66cSBjorn Helgaas static const struct printf_spec io_spec = { 566fef20d9cSFrederic Weisbecker .base = 16, 5674da0b66cSBjorn Helgaas .field_width = IO_RSRC_PRINTK_SIZE, 568fef20d9cSFrederic Weisbecker .precision = -1, 569fef20d9cSFrederic Weisbecker .flags = SPECIAL | SMALL | ZEROPAD, 570fef20d9cSFrederic Weisbecker }; 5714da0b66cSBjorn Helgaas static const struct printf_spec mem_spec = { 5724da0b66cSBjorn Helgaas .base = 16, 5734da0b66cSBjorn Helgaas .field_width = MEM_RSRC_PRINTK_SIZE, 5744da0b66cSBjorn Helgaas .precision = -1, 5754da0b66cSBjorn Helgaas .flags = SPECIAL | SMALL | ZEROPAD, 5764da0b66cSBjorn Helgaas }; 5770f4050c7SBjorn Helgaas static const struct printf_spec bus_spec = { 5780f4050c7SBjorn Helgaas .base = 16, 5790f4050c7SBjorn Helgaas .field_width = 2, 5800f4050c7SBjorn Helgaas .precision = -1, 5810f4050c7SBjorn Helgaas .flags = SMALL | ZEROPAD, 5820f4050c7SBjorn Helgaas }; 5834da0b66cSBjorn Helgaas static const struct printf_spec dec_spec = { 584c91d3376SBjorn Helgaas .base = 10, 585c91d3376SBjorn Helgaas .precision = -1, 586c91d3376SBjorn Helgaas .flags = 0, 587c91d3376SBjorn Helgaas }; 5884da0b66cSBjorn Helgaas static const struct printf_spec str_spec = { 589fd95541eSBjorn Helgaas .field_width = -1, 590fd95541eSBjorn Helgaas .precision = 10, 591fd95541eSBjorn Helgaas .flags = LEFT, 592fd95541eSBjorn Helgaas }; 5934da0b66cSBjorn Helgaas static const struct printf_spec flag_spec = { 594fd95541eSBjorn Helgaas .base = 16, 595fd95541eSBjorn Helgaas .precision = -1, 596fd95541eSBjorn Helgaas .flags = SPECIAL | SMALL, 597fd95541eSBjorn Helgaas }; 598c7dabef8SBjorn Helgaas 599c7dabef8SBjorn Helgaas /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) 600c7dabef8SBjorn Helgaas * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ 601c7dabef8SBjorn Helgaas #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) 602c7dabef8SBjorn Helgaas #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) 6039d7cca04SBjorn Helgaas #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") 604c7dabef8SBjorn Helgaas #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") 605c7dabef8SBjorn Helgaas char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, 606c7dabef8SBjorn Helgaas 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; 607c7dabef8SBjorn Helgaas 608332d2e78SLinus Torvalds char *p = sym, *pend = sym + sizeof(sym); 609c7dabef8SBjorn Helgaas int decode = (fmt[0] == 'R') ? 1 : 0; 6104da0b66cSBjorn Helgaas const struct printf_spec *specp; 611332d2e78SLinus Torvalds 612332d2e78SLinus Torvalds *p++ = '['; 6134da0b66cSBjorn Helgaas if (res->flags & IORESOURCE_IO) { 614fd95541eSBjorn Helgaas p = string(p, pend, "io ", str_spec); 6154da0b66cSBjorn Helgaas specp = &io_spec; 6164da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_MEM) { 617fd95541eSBjorn Helgaas p = string(p, pend, "mem ", str_spec); 6184da0b66cSBjorn Helgaas specp = &mem_spec; 6194da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_IRQ) { 620fd95541eSBjorn Helgaas p = string(p, pend, "irq ", str_spec); 6214da0b66cSBjorn Helgaas specp = &dec_spec; 6224da0b66cSBjorn Helgaas } else if (res->flags & IORESOURCE_DMA) { 623fd95541eSBjorn Helgaas p = string(p, pend, "dma ", str_spec); 6244da0b66cSBjorn Helgaas specp = &dec_spec; 6250f4050c7SBjorn Helgaas } else if (res->flags & IORESOURCE_BUS) { 6260f4050c7SBjorn Helgaas p = string(p, pend, "bus ", str_spec); 6270f4050c7SBjorn Helgaas specp = &bus_spec; 6284da0b66cSBjorn Helgaas } else { 629c7dabef8SBjorn Helgaas p = string(p, pend, "??? ", str_spec); 6304da0b66cSBjorn Helgaas specp = &mem_spec; 631c7dabef8SBjorn Helgaas decode = 0; 632fd95541eSBjorn Helgaas } 6334da0b66cSBjorn Helgaas p = number(p, pend, res->start, *specp); 634c91d3376SBjorn Helgaas if (res->start != res->end) { 635332d2e78SLinus Torvalds *p++ = '-'; 6364da0b66cSBjorn Helgaas p = number(p, pend, res->end, *specp); 637c91d3376SBjorn Helgaas } 638c7dabef8SBjorn Helgaas if (decode) { 639fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_MEM_64) 640fd95541eSBjorn Helgaas p = string(p, pend, " 64bit", str_spec); 641fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_PREFETCH) 642fd95541eSBjorn Helgaas p = string(p, pend, " pref", str_spec); 6439d7cca04SBjorn Helgaas if (res->flags & IORESOURCE_WINDOW) 6449d7cca04SBjorn Helgaas p = string(p, pend, " window", str_spec); 645fd95541eSBjorn Helgaas if (res->flags & IORESOURCE_DISABLED) 646fd95541eSBjorn Helgaas p = string(p, pend, " disabled", str_spec); 647c7dabef8SBjorn Helgaas } else { 648fd95541eSBjorn Helgaas p = string(p, pend, " flags ", str_spec); 649c7dabef8SBjorn Helgaas p = number(p, pend, res->flags, flag_spec); 650fd95541eSBjorn Helgaas } 651332d2e78SLinus Torvalds *p++ = ']'; 652c7dabef8SBjorn Helgaas *p = '\0'; 653332d2e78SLinus Torvalds 654fef20d9cSFrederic Weisbecker return string(buf, end, sym, spec); 655332d2e78SLinus Torvalds } 656332d2e78SLinus Torvalds 657cf3b429bSJoe Perches static noinline_for_stack 658cf3b429bSJoe Perches char *mac_address_string(char *buf, char *end, u8 *addr, 6598a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 660dd45c9cfSHarvey Harrison { 6618a27f7c9SJoe Perches char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; 662dd45c9cfSHarvey Harrison char *p = mac_addr; 663dd45c9cfSHarvey Harrison int i; 664bc7259a2SJoe Perches char separator; 665bc7259a2SJoe Perches 666bc7259a2SJoe Perches if (fmt[1] == 'F') { /* FDDI canonical format */ 667bc7259a2SJoe Perches separator = '-'; 668bc7259a2SJoe Perches } else { 669bc7259a2SJoe Perches separator = ':'; 670bc7259a2SJoe Perches } 671dd45c9cfSHarvey Harrison 672dd45c9cfSHarvey Harrison for (i = 0; i < 6; i++) { 67355036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[i]); 6748a27f7c9SJoe Perches if (fmt[0] == 'M' && i != 5) 675bc7259a2SJoe Perches *p++ = separator; 676dd45c9cfSHarvey Harrison } 677dd45c9cfSHarvey Harrison *p = '\0'; 678dd45c9cfSHarvey Harrison 679fef20d9cSFrederic Weisbecker return string(buf, end, mac_addr, spec); 680dd45c9cfSHarvey Harrison } 681dd45c9cfSHarvey Harrison 682cf3b429bSJoe Perches static noinline_for_stack 683cf3b429bSJoe Perches char *ip4_string(char *p, const u8 *addr, const char *fmt) 684689afa7dSHarvey Harrison { 685689afa7dSHarvey Harrison int i; 6860159f24eSJoe Perches bool leading_zeros = (fmt[0] == 'i'); 6870159f24eSJoe Perches int index; 6880159f24eSJoe Perches int step; 689689afa7dSHarvey Harrison 6900159f24eSJoe Perches switch (fmt[2]) { 6910159f24eSJoe Perches case 'h': 6920159f24eSJoe Perches #ifdef __BIG_ENDIAN 6930159f24eSJoe Perches index = 0; 6940159f24eSJoe Perches step = 1; 6950159f24eSJoe Perches #else 6960159f24eSJoe Perches index = 3; 6970159f24eSJoe Perches step = -1; 6980159f24eSJoe Perches #endif 6990159f24eSJoe Perches break; 7000159f24eSJoe Perches case 'l': 7010159f24eSJoe Perches index = 3; 7020159f24eSJoe Perches step = -1; 7030159f24eSJoe Perches break; 7040159f24eSJoe Perches case 'n': 7050159f24eSJoe Perches case 'b': 7060159f24eSJoe Perches default: 7070159f24eSJoe Perches index = 0; 7080159f24eSJoe Perches step = 1; 7090159f24eSJoe Perches break; 7100159f24eSJoe Perches } 7118a27f7c9SJoe Perches for (i = 0; i < 4; i++) { 7128a27f7c9SJoe Perches char temp[3]; /* hold each IP quad in reverse order */ 713*133fd9f5SDenys Vlasenko int digits = put_dec_trunc8(temp, addr[index]) - temp; 7148a27f7c9SJoe Perches if (leading_zeros) { 7158a27f7c9SJoe Perches if (digits < 3) 7168a27f7c9SJoe Perches *p++ = '0'; 7178a27f7c9SJoe Perches if (digits < 2) 7188a27f7c9SJoe Perches *p++ = '0'; 7198a27f7c9SJoe Perches } 7208a27f7c9SJoe Perches /* reverse the digits in the quad */ 7218a27f7c9SJoe Perches while (digits--) 7228a27f7c9SJoe Perches *p++ = temp[digits]; 7238a27f7c9SJoe Perches if (i < 3) 7248a27f7c9SJoe Perches *p++ = '.'; 7250159f24eSJoe Perches index += step; 7268a27f7c9SJoe Perches } 7278a27f7c9SJoe Perches *p = '\0'; 7287b9186f5SAndré Goddard Rosa 7298a27f7c9SJoe Perches return p; 7308a27f7c9SJoe Perches } 7318a27f7c9SJoe Perches 732cf3b429bSJoe Perches static noinline_for_stack 733cf3b429bSJoe Perches char *ip6_compressed_string(char *p, const char *addr) 7348a27f7c9SJoe Perches { 7357b9186f5SAndré Goddard Rosa int i, j, range; 7368a27f7c9SJoe Perches unsigned char zerolength[8]; 7378a27f7c9SJoe Perches int longest = 1; 7388a27f7c9SJoe Perches int colonpos = -1; 7398a27f7c9SJoe Perches u16 word; 7407b9186f5SAndré Goddard Rosa u8 hi, lo; 7418a27f7c9SJoe Perches bool needcolon = false; 742eb78cd26SJoe Perches bool useIPv4; 743eb78cd26SJoe Perches struct in6_addr in6; 744eb78cd26SJoe Perches 745eb78cd26SJoe Perches memcpy(&in6, addr, sizeof(struct in6_addr)); 746eb78cd26SJoe Perches 747eb78cd26SJoe Perches useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); 7488a27f7c9SJoe Perches 7498a27f7c9SJoe Perches memset(zerolength, 0, sizeof(zerolength)); 7508a27f7c9SJoe Perches 7518a27f7c9SJoe Perches if (useIPv4) 7528a27f7c9SJoe Perches range = 6; 7538a27f7c9SJoe Perches else 7548a27f7c9SJoe Perches range = 8; 7558a27f7c9SJoe Perches 7568a27f7c9SJoe Perches /* find position of longest 0 run */ 7578a27f7c9SJoe Perches for (i = 0; i < range; i++) { 7588a27f7c9SJoe Perches for (j = i; j < range; j++) { 759eb78cd26SJoe Perches if (in6.s6_addr16[j] != 0) 7608a27f7c9SJoe Perches break; 7618a27f7c9SJoe Perches zerolength[i]++; 7628a27f7c9SJoe Perches } 7638a27f7c9SJoe Perches } 7648a27f7c9SJoe Perches for (i = 0; i < range; i++) { 7658a27f7c9SJoe Perches if (zerolength[i] > longest) { 7668a27f7c9SJoe Perches longest = zerolength[i]; 7678a27f7c9SJoe Perches colonpos = i; 7688a27f7c9SJoe Perches } 7698a27f7c9SJoe Perches } 77029cf519eSJoe Perches if (longest == 1) /* don't compress a single 0 */ 77129cf519eSJoe Perches colonpos = -1; 7728a27f7c9SJoe Perches 7738a27f7c9SJoe Perches /* emit address */ 7748a27f7c9SJoe Perches for (i = 0; i < range; i++) { 7758a27f7c9SJoe Perches if (i == colonpos) { 7768a27f7c9SJoe Perches if (needcolon || i == 0) 7778a27f7c9SJoe Perches *p++ = ':'; 7788a27f7c9SJoe Perches *p++ = ':'; 7798a27f7c9SJoe Perches needcolon = false; 7808a27f7c9SJoe Perches i += longest - 1; 7818a27f7c9SJoe Perches continue; 7828a27f7c9SJoe Perches } 7838a27f7c9SJoe Perches if (needcolon) { 7848a27f7c9SJoe Perches *p++ = ':'; 7858a27f7c9SJoe Perches needcolon = false; 7868a27f7c9SJoe Perches } 7878a27f7c9SJoe Perches /* hex u16 without leading 0s */ 788eb78cd26SJoe Perches word = ntohs(in6.s6_addr16[i]); 7898a27f7c9SJoe Perches hi = word >> 8; 7908a27f7c9SJoe Perches lo = word & 0xff; 7918a27f7c9SJoe Perches if (hi) { 7928a27f7c9SJoe Perches if (hi > 0x0f) 79355036ba7SAndy Shevchenko p = hex_byte_pack(p, hi); 7948a27f7c9SJoe Perches else 7958a27f7c9SJoe Perches *p++ = hex_asc_lo(hi); 79655036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 7978a27f7c9SJoe Perches } 798b5ff992bSAndré Goddard Rosa else if (lo > 0x0f) 79955036ba7SAndy Shevchenko p = hex_byte_pack(p, lo); 8008a27f7c9SJoe Perches else 8018a27f7c9SJoe Perches *p++ = hex_asc_lo(lo); 8028a27f7c9SJoe Perches needcolon = true; 8038a27f7c9SJoe Perches } 8048a27f7c9SJoe Perches 8058a27f7c9SJoe Perches if (useIPv4) { 8068a27f7c9SJoe Perches if (needcolon) 8078a27f7c9SJoe Perches *p++ = ':'; 8080159f24eSJoe Perches p = ip4_string(p, &in6.s6_addr[12], "I4"); 8098a27f7c9SJoe Perches } 8108a27f7c9SJoe Perches *p = '\0'; 8117b9186f5SAndré Goddard Rosa 8128a27f7c9SJoe Perches return p; 8138a27f7c9SJoe Perches } 8148a27f7c9SJoe Perches 815cf3b429bSJoe Perches static noinline_for_stack 816cf3b429bSJoe Perches char *ip6_string(char *p, const char *addr, const char *fmt) 8178a27f7c9SJoe Perches { 8188a27f7c9SJoe Perches int i; 8197b9186f5SAndré Goddard Rosa 820689afa7dSHarvey Harrison for (i = 0; i < 8; i++) { 82155036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 82255036ba7SAndy Shevchenko p = hex_byte_pack(p, *addr++); 8238a27f7c9SJoe Perches if (fmt[0] == 'I' && i != 7) 824689afa7dSHarvey Harrison *p++ = ':'; 825689afa7dSHarvey Harrison } 826689afa7dSHarvey Harrison *p = '\0'; 8277b9186f5SAndré Goddard Rosa 8288a27f7c9SJoe Perches return p; 8298a27f7c9SJoe Perches } 8308a27f7c9SJoe Perches 831cf3b429bSJoe Perches static noinline_for_stack 832cf3b429bSJoe Perches char *ip6_addr_string(char *buf, char *end, const u8 *addr, 8338a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 8348a27f7c9SJoe Perches { 8358a27f7c9SJoe Perches char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; 8368a27f7c9SJoe Perches 8378a27f7c9SJoe Perches if (fmt[0] == 'I' && fmt[2] == 'c') 838eb78cd26SJoe Perches ip6_compressed_string(ip6_addr, addr); 8398a27f7c9SJoe Perches else 840eb78cd26SJoe Perches ip6_string(ip6_addr, addr, fmt); 841689afa7dSHarvey Harrison 842fef20d9cSFrederic Weisbecker return string(buf, end, ip6_addr, spec); 843689afa7dSHarvey Harrison } 844689afa7dSHarvey Harrison 845cf3b429bSJoe Perches static noinline_for_stack 846cf3b429bSJoe Perches char *ip4_addr_string(char *buf, char *end, const u8 *addr, 8478a27f7c9SJoe Perches struct printf_spec spec, const char *fmt) 8484aa99606SHarvey Harrison { 8498a27f7c9SJoe Perches char ip4_addr[sizeof("255.255.255.255")]; 8504aa99606SHarvey Harrison 8510159f24eSJoe Perches ip4_string(ip4_addr, addr, fmt); 8524aa99606SHarvey Harrison 853fef20d9cSFrederic Weisbecker return string(buf, end, ip4_addr, spec); 8544aa99606SHarvey Harrison } 8554aa99606SHarvey Harrison 856cf3b429bSJoe Perches static noinline_for_stack 857cf3b429bSJoe Perches char *uuid_string(char *buf, char *end, const u8 *addr, 8589ac6e44eSJoe Perches struct printf_spec spec, const char *fmt) 8599ac6e44eSJoe Perches { 8609ac6e44eSJoe Perches char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]; 8619ac6e44eSJoe Perches char *p = uuid; 8629ac6e44eSJoe Perches int i; 8639ac6e44eSJoe Perches static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; 8649ac6e44eSJoe Perches static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15}; 8659ac6e44eSJoe Perches const u8 *index = be; 8669ac6e44eSJoe Perches bool uc = false; 8679ac6e44eSJoe Perches 8689ac6e44eSJoe Perches switch (*(++fmt)) { 8699ac6e44eSJoe Perches case 'L': 8709ac6e44eSJoe Perches uc = true; /* fall-through */ 8719ac6e44eSJoe Perches case 'l': 8729ac6e44eSJoe Perches index = le; 8739ac6e44eSJoe Perches break; 8749ac6e44eSJoe Perches case 'B': 8759ac6e44eSJoe Perches uc = true; 8769ac6e44eSJoe Perches break; 8779ac6e44eSJoe Perches } 8789ac6e44eSJoe Perches 8799ac6e44eSJoe Perches for (i = 0; i < 16; i++) { 88055036ba7SAndy Shevchenko p = hex_byte_pack(p, addr[index[i]]); 8819ac6e44eSJoe Perches switch (i) { 8829ac6e44eSJoe Perches case 3: 8839ac6e44eSJoe Perches case 5: 8849ac6e44eSJoe Perches case 7: 8859ac6e44eSJoe Perches case 9: 8869ac6e44eSJoe Perches *p++ = '-'; 8879ac6e44eSJoe Perches break; 8889ac6e44eSJoe Perches } 8899ac6e44eSJoe Perches } 8909ac6e44eSJoe Perches 8919ac6e44eSJoe Perches *p = 0; 8929ac6e44eSJoe Perches 8939ac6e44eSJoe Perches if (uc) { 8949ac6e44eSJoe Perches p = uuid; 8959ac6e44eSJoe Perches do { 8969ac6e44eSJoe Perches *p = toupper(*p); 8979ac6e44eSJoe Perches } while (*(++p)); 8989ac6e44eSJoe Perches } 8999ac6e44eSJoe Perches 9009ac6e44eSJoe Perches return string(buf, end, uuid, spec); 9019ac6e44eSJoe Perches } 9029ac6e44eSJoe Perches 903c8f44affSMichał Mirosław static 904c8f44affSMichał Mirosław char *netdev_feature_string(char *buf, char *end, const u8 *addr, 905c8f44affSMichał Mirosław struct printf_spec spec) 906c8f44affSMichał Mirosław { 907c8f44affSMichał Mirosław spec.flags |= SPECIAL | SMALL | ZEROPAD; 908c8f44affSMichał Mirosław if (spec.field_width == -1) 909c8f44affSMichał Mirosław spec.field_width = 2 + 2 * sizeof(netdev_features_t); 910c8f44affSMichał Mirosław spec.base = 16; 911c8f44affSMichał Mirosław 912c8f44affSMichał Mirosław return number(buf, end, *(const netdev_features_t *)addr, spec); 913c8f44affSMichał Mirosław } 914c8f44affSMichał Mirosław 915411f05f1SIngo Molnar int kptr_restrict __read_mostly; 916455cd5abSDan Rosenberg 9174d8a743cSLinus Torvalds /* 9184d8a743cSLinus Torvalds * Show a '%p' thing. A kernel extension is that the '%p' is followed 9194d8a743cSLinus Torvalds * by an extra set of alphanumeric characters that are extended format 9204d8a743cSLinus Torvalds * specifiers. 9214d8a743cSLinus Torvalds * 922332d2e78SLinus Torvalds * Right now we handle: 9230fe1ef24SLinus Torvalds * 9240c8b946eSFrederic Weisbecker * - 'F' For symbolic function descriptor pointers with offset 9250c8b946eSFrederic Weisbecker * - 'f' For simple symbolic function names without offset 9260efb4d20SSteven Rostedt * - 'S' For symbolic direct pointers with offset 9270efb4d20SSteven Rostedt * - 's' For symbolic direct pointers without offset 9280f77a8d3SNamhyung Kim * - 'B' For backtraced symbolic direct pointers with offset 929c7dabef8SBjorn Helgaas * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] 930c7dabef8SBjorn Helgaas * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] 931dd45c9cfSHarvey Harrison * - 'M' For a 6-byte MAC address, it prints the address in the 932dd45c9cfSHarvey Harrison * usual colon-separated hex notation 9338a27f7c9SJoe Perches * - 'm' For a 6-byte MAC address, it prints the hex address without colons 934bc7259a2SJoe Perches * - 'MF' For a 6-byte MAC FDDI address, it prints the address 935c8e00060SJoe Perches * with a dash-separated hex notation 9368a27f7c9SJoe Perches * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way 9378a27f7c9SJoe Perches * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) 9388a27f7c9SJoe Perches * IPv6 uses colon separated network-order 16 bit hex with leading 0's 9398a27f7c9SJoe Perches * - 'i' [46] for 'raw' IPv4/IPv6 addresses 9408a27f7c9SJoe Perches * IPv6 omits the colons (01020304...0f) 9418a27f7c9SJoe Perches * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) 9420159f24eSJoe Perches * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order 9438a27f7c9SJoe Perches * - 'I6c' for IPv6 addresses printed as specified by 94429cf519eSJoe Perches * http://tools.ietf.org/html/rfc5952 9459ac6e44eSJoe Perches * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form 9469ac6e44eSJoe Perches * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" 9479ac6e44eSJoe Perches * Options for %pU are: 9489ac6e44eSJoe Perches * b big endian lower case hex (default) 9499ac6e44eSJoe Perches * B big endian UPPER case hex 9509ac6e44eSJoe Perches * l little endian lower case hex 9519ac6e44eSJoe Perches * L little endian UPPER case hex 9529ac6e44eSJoe Perches * big endian output byte order is: 9539ac6e44eSJoe Perches * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] 9549ac6e44eSJoe Perches * little endian output byte order is: 9559ac6e44eSJoe Perches * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] 9567db6f5fbSJoe Perches * - 'V' For a struct va_format which contains a format string * and va_list *, 9577db6f5fbSJoe Perches * call vsnprintf(->format, *->va_list). 9587db6f5fbSJoe Perches * Implements a "recursive vsnprintf". 9597db6f5fbSJoe Perches * Do not use this feature without some mechanism to verify the 9607db6f5fbSJoe Perches * correctness of the format string and va_list arguments. 961455cd5abSDan Rosenberg * - 'K' For a kernel pointer that should be hidden from unprivileged users 962c8f44affSMichał Mirosław * - 'NF' For a netdev_features_t 9639ac6e44eSJoe Perches * 964332d2e78SLinus Torvalds * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 965332d2e78SLinus Torvalds * function pointers are really function descriptors, which contain a 966332d2e78SLinus Torvalds * pointer to the real address. 9674d8a743cSLinus Torvalds */ 968cf3b429bSJoe Perches static noinline_for_stack 969cf3b429bSJoe Perches char *pointer(const char *fmt, char *buf, char *end, void *ptr, 970fef20d9cSFrederic Weisbecker struct printf_spec spec) 97178a8bf69SLinus Torvalds { 972725fe002SGrant Likely int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0); 973725fe002SGrant Likely 9749f36e2c4SKees Cook if (!ptr && *fmt != 'K') { 9755e057981SJoe Perches /* 9765e057981SJoe Perches * Print (null) with the same width as a pointer so it makes 9775e057981SJoe Perches * tabular output look nice. 9785e057981SJoe Perches */ 9795e057981SJoe Perches if (spec.field_width == -1) 980725fe002SGrant Likely spec.field_width = default_width; 981fef20d9cSFrederic Weisbecker return string(buf, end, "(null)", spec); 9825e057981SJoe Perches } 983d97106abSLinus Torvalds 9840fe1ef24SLinus Torvalds switch (*fmt) { 9850fe1ef24SLinus Torvalds case 'F': 9860c8b946eSFrederic Weisbecker case 'f': 9870fe1ef24SLinus Torvalds ptr = dereference_function_descriptor(ptr); 9880fe1ef24SLinus Torvalds /* Fallthrough */ 9890fe1ef24SLinus Torvalds case 'S': 9909ac6e44eSJoe Perches case 's': 9910f77a8d3SNamhyung Kim case 'B': 9920c8b946eSFrederic Weisbecker return symbol_string(buf, end, ptr, spec, *fmt); 993332d2e78SLinus Torvalds case 'R': 994c7dabef8SBjorn Helgaas case 'r': 995fd95541eSBjorn Helgaas return resource_string(buf, end, ptr, spec, fmt); 9968a27f7c9SJoe Perches case 'M': /* Colon separated: 00:01:02:03:04:05 */ 9978a27f7c9SJoe Perches case 'm': /* Contiguous: 000102030405 */ 998bc7259a2SJoe Perches /* [mM]F (FDDI, bit reversed) */ 9998a27f7c9SJoe Perches return mac_address_string(buf, end, ptr, spec, fmt); 10008a27f7c9SJoe Perches case 'I': /* Formatted IP supported 10018a27f7c9SJoe Perches * 4: 1.2.3.4 10028a27f7c9SJoe Perches * 6: 0001:0203:...:0708 10038a27f7c9SJoe Perches * 6c: 1::708 or 1::1.2.3.4 10048a27f7c9SJoe Perches */ 10058a27f7c9SJoe Perches case 'i': /* Contiguous: 10068a27f7c9SJoe Perches * 4: 001.002.003.004 10078a27f7c9SJoe Perches * 6: 000102...0f 10088a27f7c9SJoe Perches */ 10098a27f7c9SJoe Perches switch (fmt[1]) { 10108a27f7c9SJoe Perches case '6': 10118a27f7c9SJoe Perches return ip6_addr_string(buf, end, ptr, spec, fmt); 10128a27f7c9SJoe Perches case '4': 10138a27f7c9SJoe Perches return ip4_addr_string(buf, end, ptr, spec, fmt); 10148a27f7c9SJoe Perches } 10154aa99606SHarvey Harrison break; 10169ac6e44eSJoe Perches case 'U': 10179ac6e44eSJoe Perches return uuid_string(buf, end, ptr, spec, fmt); 10187db6f5fbSJoe Perches case 'V': 10195756b76eSJan Beulich { 10205756b76eSJan Beulich va_list va; 10215756b76eSJan Beulich 10225756b76eSJan Beulich va_copy(va, *((struct va_format *)ptr)->va); 10235756b76eSJan Beulich buf += vsnprintf(buf, end > buf ? end - buf : 0, 10245756b76eSJan Beulich ((struct va_format *)ptr)->fmt, va); 10255756b76eSJan Beulich va_end(va); 10265756b76eSJan Beulich return buf; 10275756b76eSJan Beulich } 1028455cd5abSDan Rosenberg case 'K': 1029455cd5abSDan Rosenberg /* 1030455cd5abSDan Rosenberg * %pK cannot be used in IRQ context because its test 1031455cd5abSDan Rosenberg * for CAP_SYSLOG would be meaningless. 1032455cd5abSDan Rosenberg */ 1033455cd5abSDan Rosenberg if (in_irq() || in_serving_softirq() || in_nmi()) { 1034455cd5abSDan Rosenberg if (spec.field_width == -1) 1035725fe002SGrant Likely spec.field_width = default_width; 1036455cd5abSDan Rosenberg return string(buf, end, "pK-error", spec); 1037455cd5abSDan Rosenberg } 103826297607SJoe Perches if (!((kptr_restrict == 0) || 103926297607SJoe Perches (kptr_restrict == 1 && 104026297607SJoe Perches has_capability_noaudit(current, CAP_SYSLOG)))) 104126297607SJoe Perches ptr = NULL; 104226297607SJoe Perches break; 1043c8f44affSMichał Mirosław case 'N': 1044c8f44affSMichał Mirosław switch (fmt[1]) { 1045c8f44affSMichał Mirosław case 'F': 1046c8f44affSMichał Mirosław return netdev_feature_string(buf, end, ptr, spec); 1047c8f44affSMichał Mirosław } 1048c8f44affSMichał Mirosław break; 10490fe1ef24SLinus Torvalds } 1050fef20d9cSFrederic Weisbecker spec.flags |= SMALL; 1051fef20d9cSFrederic Weisbecker if (spec.field_width == -1) { 1052725fe002SGrant Likely spec.field_width = default_width; 1053fef20d9cSFrederic Weisbecker spec.flags |= ZEROPAD; 105478a8bf69SLinus Torvalds } 1055fef20d9cSFrederic Weisbecker spec.base = 16; 1056fef20d9cSFrederic Weisbecker 1057fef20d9cSFrederic Weisbecker return number(buf, end, (unsigned long) ptr, spec); 1058fef20d9cSFrederic Weisbecker } 1059fef20d9cSFrederic Weisbecker 1060fef20d9cSFrederic Weisbecker /* 1061fef20d9cSFrederic Weisbecker * Helper function to decode printf style format. 1062fef20d9cSFrederic Weisbecker * Each call decode a token from the format and return the 1063fef20d9cSFrederic Weisbecker * number of characters read (or likely the delta where it wants 1064fef20d9cSFrederic Weisbecker * to go on the next call). 1065fef20d9cSFrederic Weisbecker * The decoded token is returned through the parameters 1066fef20d9cSFrederic Weisbecker * 1067fef20d9cSFrederic Weisbecker * 'h', 'l', or 'L' for integer fields 1068fef20d9cSFrederic Weisbecker * 'z' support added 23/7/1999 S.H. 1069fef20d9cSFrederic Weisbecker * 'z' changed to 'Z' --davidm 1/25/99 1070fef20d9cSFrederic Weisbecker * 't' added for ptrdiff_t 1071fef20d9cSFrederic Weisbecker * 1072fef20d9cSFrederic Weisbecker * @fmt: the format string 1073fef20d9cSFrederic Weisbecker * @type of the token returned 1074fef20d9cSFrederic Weisbecker * @flags: various flags such as +, -, # tokens.. 1075fef20d9cSFrederic Weisbecker * @field_width: overwritten width 1076fef20d9cSFrederic Weisbecker * @base: base of the number (octal, hex, ...) 1077fef20d9cSFrederic Weisbecker * @precision: precision of a number 1078fef20d9cSFrederic Weisbecker * @qualifier: qualifier of a number (long, size_t, ...) 1079fef20d9cSFrederic Weisbecker */ 1080cf3b429bSJoe Perches static noinline_for_stack 1081cf3b429bSJoe Perches int format_decode(const char *fmt, struct printf_spec *spec) 1082fef20d9cSFrederic Weisbecker { 1083fef20d9cSFrederic Weisbecker const char *start = fmt; 1084fef20d9cSFrederic Weisbecker 1085fef20d9cSFrederic Weisbecker /* we finished early by reading the field width */ 1086ed681a91SVegard Nossum if (spec->type == FORMAT_TYPE_WIDTH) { 1087fef20d9cSFrederic Weisbecker if (spec->field_width < 0) { 1088fef20d9cSFrederic Weisbecker spec->field_width = -spec->field_width; 1089fef20d9cSFrederic Weisbecker spec->flags |= LEFT; 1090fef20d9cSFrederic Weisbecker } 1091fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 1092fef20d9cSFrederic Weisbecker goto precision; 1093fef20d9cSFrederic Weisbecker } 1094fef20d9cSFrederic Weisbecker 1095fef20d9cSFrederic Weisbecker /* we finished early by reading the precision */ 1096fef20d9cSFrederic Weisbecker if (spec->type == FORMAT_TYPE_PRECISION) { 1097fef20d9cSFrederic Weisbecker if (spec->precision < 0) 1098fef20d9cSFrederic Weisbecker spec->precision = 0; 1099fef20d9cSFrederic Weisbecker 1100fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 1101fef20d9cSFrederic Weisbecker goto qualifier; 1102fef20d9cSFrederic Weisbecker } 1103fef20d9cSFrederic Weisbecker 1104fef20d9cSFrederic Weisbecker /* By default */ 1105fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NONE; 1106fef20d9cSFrederic Weisbecker 1107fef20d9cSFrederic Weisbecker for (; *fmt ; ++fmt) { 1108fef20d9cSFrederic Weisbecker if (*fmt == '%') 1109fef20d9cSFrederic Weisbecker break; 1110fef20d9cSFrederic Weisbecker } 1111fef20d9cSFrederic Weisbecker 1112fef20d9cSFrederic Weisbecker /* Return the current non-format string */ 1113fef20d9cSFrederic Weisbecker if (fmt != start || !*fmt) 1114fef20d9cSFrederic Weisbecker return fmt - start; 1115fef20d9cSFrederic Weisbecker 1116fef20d9cSFrederic Weisbecker /* Process flags */ 1117fef20d9cSFrederic Weisbecker spec->flags = 0; 1118fef20d9cSFrederic Weisbecker 1119fef20d9cSFrederic Weisbecker while (1) { /* this also skips first '%' */ 1120fef20d9cSFrederic Weisbecker bool found = true; 1121fef20d9cSFrederic Weisbecker 1122fef20d9cSFrederic Weisbecker ++fmt; 1123fef20d9cSFrederic Weisbecker 1124fef20d9cSFrederic Weisbecker switch (*fmt) { 1125fef20d9cSFrederic Weisbecker case '-': spec->flags |= LEFT; break; 1126fef20d9cSFrederic Weisbecker case '+': spec->flags |= PLUS; break; 1127fef20d9cSFrederic Weisbecker case ' ': spec->flags |= SPACE; break; 1128fef20d9cSFrederic Weisbecker case '#': spec->flags |= SPECIAL; break; 1129fef20d9cSFrederic Weisbecker case '0': spec->flags |= ZEROPAD; break; 1130fef20d9cSFrederic Weisbecker default: found = false; 1131fef20d9cSFrederic Weisbecker } 1132fef20d9cSFrederic Weisbecker 1133fef20d9cSFrederic Weisbecker if (!found) 1134fef20d9cSFrederic Weisbecker break; 1135fef20d9cSFrederic Weisbecker } 1136fef20d9cSFrederic Weisbecker 1137fef20d9cSFrederic Weisbecker /* get field width */ 1138fef20d9cSFrederic Weisbecker spec->field_width = -1; 1139fef20d9cSFrederic Weisbecker 1140fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) 1141fef20d9cSFrederic Weisbecker spec->field_width = skip_atoi(&fmt); 1142fef20d9cSFrederic Weisbecker else if (*fmt == '*') { 1143fef20d9cSFrederic Weisbecker /* it's the next argument */ 1144ed681a91SVegard Nossum spec->type = FORMAT_TYPE_WIDTH; 1145fef20d9cSFrederic Weisbecker return ++fmt - start; 1146fef20d9cSFrederic Weisbecker } 1147fef20d9cSFrederic Weisbecker 1148fef20d9cSFrederic Weisbecker precision: 1149fef20d9cSFrederic Weisbecker /* get the precision */ 1150fef20d9cSFrederic Weisbecker spec->precision = -1; 1151fef20d9cSFrederic Weisbecker if (*fmt == '.') { 1152fef20d9cSFrederic Weisbecker ++fmt; 1153fef20d9cSFrederic Weisbecker if (isdigit(*fmt)) { 1154fef20d9cSFrederic Weisbecker spec->precision = skip_atoi(&fmt); 1155fef20d9cSFrederic Weisbecker if (spec->precision < 0) 1156fef20d9cSFrederic Weisbecker spec->precision = 0; 1157fef20d9cSFrederic Weisbecker } else if (*fmt == '*') { 1158fef20d9cSFrederic Weisbecker /* it's the next argument */ 1159adf26f84SVegard Nossum spec->type = FORMAT_TYPE_PRECISION; 1160fef20d9cSFrederic Weisbecker return ++fmt - start; 1161fef20d9cSFrederic Weisbecker } 1162fef20d9cSFrederic Weisbecker } 1163fef20d9cSFrederic Weisbecker 1164fef20d9cSFrederic Weisbecker qualifier: 1165fef20d9cSFrederic Weisbecker /* get the conversion qualifier */ 1166fef20d9cSFrederic Weisbecker spec->qualifier = -1; 116775fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 116875fb8f26SAndy Shevchenko _tolower(*fmt) == 'z' || *fmt == 't') { 1169a4e94ef0SZhaolei spec->qualifier = *fmt++; 1170a4e94ef0SZhaolei if (unlikely(spec->qualifier == *fmt)) { 1171a4e94ef0SZhaolei if (spec->qualifier == 'l') { 1172fef20d9cSFrederic Weisbecker spec->qualifier = 'L'; 1173fef20d9cSFrederic Weisbecker ++fmt; 1174a4e94ef0SZhaolei } else if (spec->qualifier == 'h') { 1175a4e94ef0SZhaolei spec->qualifier = 'H'; 1176a4e94ef0SZhaolei ++fmt; 1177a4e94ef0SZhaolei } 1178fef20d9cSFrederic Weisbecker } 1179fef20d9cSFrederic Weisbecker } 1180fef20d9cSFrederic Weisbecker 1181fef20d9cSFrederic Weisbecker /* default base */ 1182fef20d9cSFrederic Weisbecker spec->base = 10; 1183fef20d9cSFrederic Weisbecker switch (*fmt) { 1184fef20d9cSFrederic Weisbecker case 'c': 1185fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_CHAR; 1186fef20d9cSFrederic Weisbecker return ++fmt - start; 1187fef20d9cSFrederic Weisbecker 1188fef20d9cSFrederic Weisbecker case 's': 1189fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_STR; 1190fef20d9cSFrederic Weisbecker return ++fmt - start; 1191fef20d9cSFrederic Weisbecker 1192fef20d9cSFrederic Weisbecker case 'p': 1193fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTR; 1194fef20d9cSFrederic Weisbecker return fmt - start; 1195fef20d9cSFrederic Weisbecker /* skip alnum */ 1196fef20d9cSFrederic Weisbecker 1197fef20d9cSFrederic Weisbecker case 'n': 1198fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_NRCHARS; 1199fef20d9cSFrederic Weisbecker return ++fmt - start; 1200fef20d9cSFrederic Weisbecker 1201fef20d9cSFrederic Weisbecker case '%': 1202fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PERCENT_CHAR; 1203fef20d9cSFrederic Weisbecker return ++fmt - start; 1204fef20d9cSFrederic Weisbecker 1205fef20d9cSFrederic Weisbecker /* integer number formats - set up the flags and "break" */ 1206fef20d9cSFrederic Weisbecker case 'o': 1207fef20d9cSFrederic Weisbecker spec->base = 8; 1208fef20d9cSFrederic Weisbecker break; 1209fef20d9cSFrederic Weisbecker 1210fef20d9cSFrederic Weisbecker case 'x': 1211fef20d9cSFrederic Weisbecker spec->flags |= SMALL; 1212fef20d9cSFrederic Weisbecker 1213fef20d9cSFrederic Weisbecker case 'X': 1214fef20d9cSFrederic Weisbecker spec->base = 16; 1215fef20d9cSFrederic Weisbecker break; 1216fef20d9cSFrederic Weisbecker 1217fef20d9cSFrederic Weisbecker case 'd': 1218fef20d9cSFrederic Weisbecker case 'i': 121939e874f8SFrederic Weisbecker spec->flags |= SIGN; 1220fef20d9cSFrederic Weisbecker case 'u': 1221fef20d9cSFrederic Weisbecker break; 1222fef20d9cSFrederic Weisbecker 1223fef20d9cSFrederic Weisbecker default: 1224fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_INVALID; 1225fef20d9cSFrederic Weisbecker return fmt - start; 1226fef20d9cSFrederic Weisbecker } 1227fef20d9cSFrederic Weisbecker 1228fef20d9cSFrederic Weisbecker if (spec->qualifier == 'L') 1229fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_LONG_LONG; 1230fef20d9cSFrederic Weisbecker else if (spec->qualifier == 'l') { 123139e874f8SFrederic Weisbecker if (spec->flags & SIGN) 1232fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_LONG; 1233fef20d9cSFrederic Weisbecker else 1234fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_ULONG; 123575fb8f26SAndy Shevchenko } else if (_tolower(spec->qualifier) == 'z') { 1236fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_SIZE_T; 1237fef20d9cSFrederic Weisbecker } else if (spec->qualifier == 't') { 1238fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_PTRDIFF; 1239a4e94ef0SZhaolei } else if (spec->qualifier == 'H') { 1240a4e94ef0SZhaolei if (spec->flags & SIGN) 1241a4e94ef0SZhaolei spec->type = FORMAT_TYPE_BYTE; 1242a4e94ef0SZhaolei else 1243a4e94ef0SZhaolei spec->type = FORMAT_TYPE_UBYTE; 1244fef20d9cSFrederic Weisbecker } else if (spec->qualifier == 'h') { 124539e874f8SFrederic Weisbecker if (spec->flags & SIGN) 1246fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_SHORT; 1247fef20d9cSFrederic Weisbecker else 1248fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_USHORT; 1249fef20d9cSFrederic Weisbecker } else { 125039e874f8SFrederic Weisbecker if (spec->flags & SIGN) 1251fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_INT; 1252fef20d9cSFrederic Weisbecker else 1253fef20d9cSFrederic Weisbecker spec->type = FORMAT_TYPE_UINT; 1254fef20d9cSFrederic Weisbecker } 1255fef20d9cSFrederic Weisbecker 1256fef20d9cSFrederic Weisbecker return ++fmt - start; 125778a8bf69SLinus Torvalds } 125878a8bf69SLinus Torvalds 12591da177e4SLinus Torvalds /** 12601da177e4SLinus Torvalds * vsnprintf - Format a string and place it in a buffer 12611da177e4SLinus Torvalds * @buf: The buffer to place the result into 12621da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 12631da177e4SLinus Torvalds * @fmt: The format string to use 12641da177e4SLinus Torvalds * @args: Arguments for the format string 12651da177e4SLinus Torvalds * 126620036fdcSAndi Kleen * This function follows C99 vsnprintf, but has some extensions: 126791adcd2cSSteven Rostedt * %pS output the name of a text symbol with offset 126891adcd2cSSteven Rostedt * %ps output the name of a text symbol without offset 12690c8b946eSFrederic Weisbecker * %pF output the name of a function pointer with its offset 12700c8b946eSFrederic Weisbecker * %pf output the name of a function pointer without its offset 12710f77a8d3SNamhyung Kim * %pB output the name of a backtrace symbol with its offset 12728a79503aSUwe Kleine-König * %pR output the address range in a struct resource with decoded flags 12738a79503aSUwe Kleine-König * %pr output the address range in a struct resource with raw flags 12748a79503aSUwe Kleine-König * %pM output a 6-byte MAC address with colons 12758a79503aSUwe Kleine-König * %pm output a 6-byte MAC address without colons 12768a79503aSUwe Kleine-König * %pI4 print an IPv4 address without leading zeros 12778a79503aSUwe Kleine-König * %pi4 print an IPv4 address with leading zeros 12788a79503aSUwe Kleine-König * %pI6 print an IPv6 address with colons 12798a79503aSUwe Kleine-König * %pi6 print an IPv6 address without colons 1280f996f208SJan Engelhardt * %pI6c print an IPv6 address as specified by RFC 5952 12818a79503aSUwe Kleine-König * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper 12828a79503aSUwe Kleine-König * case. 12830efb4d20SSteven Rostedt * %n is ignored 128420036fdcSAndi Kleen * 12851da177e4SLinus Torvalds * The return value is the number of characters which would 12861da177e4SLinus Torvalds * be generated for the given input, excluding the trailing 12871da177e4SLinus Torvalds * '\0', as per ISO C99. If you want to have the exact 12881da177e4SLinus Torvalds * number of characters written into @buf as return value 128972fd4a35SRobert P. J. Day * (not including the trailing '\0'), use vscnprintf(). If the 12901da177e4SLinus Torvalds * return is greater than or equal to @size, the resulting 12911da177e4SLinus Torvalds * string is truncated. 12921da177e4SLinus Torvalds * 1293ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using snprintf(). 12941da177e4SLinus Torvalds */ 12951da177e4SLinus Torvalds int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) 12961da177e4SLinus Torvalds { 12971da177e4SLinus Torvalds unsigned long long num; 1298d4be151bSAndré Goddard Rosa char *str, *end; 1299fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 13001da177e4SLinus Torvalds 1301f796937aSJeremy Fitzhardinge /* Reject out-of-range values early. Large positive sizes are 1302f796937aSJeremy Fitzhardinge used for unknown buffer sizes. */ 13032f30b1f9SMarcin Slusarz if (WARN_ON_ONCE((int) size < 0)) 13041da177e4SLinus Torvalds return 0; 13051da177e4SLinus Torvalds 13061da177e4SLinus Torvalds str = buf; 1307f796937aSJeremy Fitzhardinge end = buf + size; 13081da177e4SLinus Torvalds 1309f796937aSJeremy Fitzhardinge /* Make sure end is always >= buf */ 1310f796937aSJeremy Fitzhardinge if (end < buf) { 13111da177e4SLinus Torvalds end = ((void *)-1); 1312f796937aSJeremy Fitzhardinge size = end - buf; 13131da177e4SLinus Torvalds } 13141da177e4SLinus Torvalds 1315fef20d9cSFrederic Weisbecker while (*fmt) { 1316fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 1317d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 1318fef20d9cSFrederic Weisbecker 1319fef20d9cSFrederic Weisbecker fmt += read; 1320fef20d9cSFrederic Weisbecker 1321fef20d9cSFrederic Weisbecker switch (spec.type) { 1322fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 1323fef20d9cSFrederic Weisbecker int copy = read; 1324fef20d9cSFrederic Weisbecker if (str < end) { 1325fef20d9cSFrederic Weisbecker if (copy > end - str) 1326fef20d9cSFrederic Weisbecker copy = end - str; 1327fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 1328fef20d9cSFrederic Weisbecker } 1329fef20d9cSFrederic Weisbecker str += read; 1330fef20d9cSFrederic Weisbecker break; 13311da177e4SLinus Torvalds } 13321da177e4SLinus Torvalds 1333ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 1334fef20d9cSFrederic Weisbecker spec.field_width = va_arg(args, int); 1335fef20d9cSFrederic Weisbecker break; 13361da177e4SLinus Torvalds 1337fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 1338fef20d9cSFrederic Weisbecker spec.precision = va_arg(args, int); 1339fef20d9cSFrederic Weisbecker break; 13401da177e4SLinus Torvalds 1341d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 1342d4be151bSAndré Goddard Rosa char c; 1343d4be151bSAndré Goddard Rosa 1344fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 1345fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 1346f796937aSJeremy Fitzhardinge if (str < end) 13471da177e4SLinus Torvalds *str = ' '; 13481da177e4SLinus Torvalds ++str; 1349fef20d9cSFrederic Weisbecker 13501da177e4SLinus Torvalds } 13511da177e4SLinus Torvalds } 13521da177e4SLinus Torvalds c = (unsigned char) va_arg(args, int); 1353f796937aSJeremy Fitzhardinge if (str < end) 13541da177e4SLinus Torvalds *str = c; 13551da177e4SLinus Torvalds ++str; 1356fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 1357f796937aSJeremy Fitzhardinge if (str < end) 13581da177e4SLinus Torvalds *str = ' '; 13591da177e4SLinus Torvalds ++str; 13601da177e4SLinus Torvalds } 1361fef20d9cSFrederic Weisbecker break; 1362d4be151bSAndré Goddard Rosa } 13631da177e4SLinus Torvalds 1364fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: 1365fef20d9cSFrederic Weisbecker str = string(str, end, va_arg(args, char *), spec); 1366fef20d9cSFrederic Weisbecker break; 13671da177e4SLinus Torvalds 1368fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 1369fef20d9cSFrederic Weisbecker str = pointer(fmt+1, str, end, va_arg(args, void *), 1370fef20d9cSFrederic Weisbecker spec); 1371fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 13724d8a743cSLinus Torvalds fmt++; 1373fef20d9cSFrederic Weisbecker break; 13741da177e4SLinus Torvalds 1375fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 1376f796937aSJeremy Fitzhardinge if (str < end) 13771da177e4SLinus Torvalds *str = '%'; 13781da177e4SLinus Torvalds ++str; 13791da177e4SLinus Torvalds break; 13801da177e4SLinus Torvalds 1381fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 1382f796937aSJeremy Fitzhardinge if (str < end) 13831da177e4SLinus Torvalds *str = '%'; 13841da177e4SLinus Torvalds ++str; 1385fef20d9cSFrederic Weisbecker break; 1386fef20d9cSFrederic Weisbecker 1387fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NRCHARS: { 1388ef0658f3SJoe Perches u8 qualifier = spec.qualifier; 1389fef20d9cSFrederic Weisbecker 1390fef20d9cSFrederic Weisbecker if (qualifier == 'l') { 1391fef20d9cSFrederic Weisbecker long *ip = va_arg(args, long *); 1392fef20d9cSFrederic Weisbecker *ip = (str - buf); 139375fb8f26SAndy Shevchenko } else if (_tolower(qualifier) == 'z') { 1394fef20d9cSFrederic Weisbecker size_t *ip = va_arg(args, size_t *); 1395fef20d9cSFrederic Weisbecker *ip = (str - buf); 13961da177e4SLinus Torvalds } else { 1397fef20d9cSFrederic Weisbecker int *ip = va_arg(args, int *); 1398fef20d9cSFrederic Weisbecker *ip = (str - buf); 1399fef20d9cSFrederic Weisbecker } 1400fef20d9cSFrederic Weisbecker break; 1401fef20d9cSFrederic Weisbecker } 1402fef20d9cSFrederic Weisbecker 1403fef20d9cSFrederic Weisbecker default: 1404fef20d9cSFrederic Weisbecker switch (spec.type) { 1405fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 1406fef20d9cSFrederic Weisbecker num = va_arg(args, long long); 1407fef20d9cSFrederic Weisbecker break; 1408fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 1409fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned long); 1410fef20d9cSFrederic Weisbecker break; 1411fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 1412fef20d9cSFrederic Weisbecker num = va_arg(args, long); 1413fef20d9cSFrederic Weisbecker break; 1414fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 1415fef20d9cSFrederic Weisbecker num = va_arg(args, size_t); 1416fef20d9cSFrederic Weisbecker break; 1417fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 1418fef20d9cSFrederic Weisbecker num = va_arg(args, ptrdiff_t); 1419fef20d9cSFrederic Weisbecker break; 1420a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 1421a4e94ef0SZhaolei num = (unsigned char) va_arg(args, int); 1422a4e94ef0SZhaolei break; 1423a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 1424a4e94ef0SZhaolei num = (signed char) va_arg(args, int); 1425a4e94ef0SZhaolei break; 1426fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 1427fef20d9cSFrederic Weisbecker num = (unsigned short) va_arg(args, int); 1428fef20d9cSFrederic Weisbecker break; 1429fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 1430fef20d9cSFrederic Weisbecker num = (short) va_arg(args, int); 1431fef20d9cSFrederic Weisbecker break; 143239e874f8SFrederic Weisbecker case FORMAT_TYPE_INT: 143339e874f8SFrederic Weisbecker num = (int) va_arg(args, int); 1434fef20d9cSFrederic Weisbecker break; 1435fef20d9cSFrederic Weisbecker default: 1436fef20d9cSFrederic Weisbecker num = va_arg(args, unsigned int); 14371da177e4SLinus Torvalds } 1438fef20d9cSFrederic Weisbecker 1439fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 14401da177e4SLinus Torvalds } 1441fef20d9cSFrederic Weisbecker } 1442fef20d9cSFrederic Weisbecker 1443f796937aSJeremy Fitzhardinge if (size > 0) { 1444f796937aSJeremy Fitzhardinge if (str < end) 14451da177e4SLinus Torvalds *str = '\0'; 1446f796937aSJeremy Fitzhardinge else 14470a6047eeSLinus Torvalds end[-1] = '\0'; 1448f796937aSJeremy Fitzhardinge } 1449fef20d9cSFrederic Weisbecker 1450f796937aSJeremy Fitzhardinge /* the trailing null byte doesn't count towards the total */ 14511da177e4SLinus Torvalds return str-buf; 1452fef20d9cSFrederic Weisbecker 14531da177e4SLinus Torvalds } 14541da177e4SLinus Torvalds EXPORT_SYMBOL(vsnprintf); 14551da177e4SLinus Torvalds 14561da177e4SLinus Torvalds /** 14571da177e4SLinus Torvalds * vscnprintf - Format a string and place it in a buffer 14581da177e4SLinus Torvalds * @buf: The buffer to place the result into 14591da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 14601da177e4SLinus Torvalds * @fmt: The format string to use 14611da177e4SLinus Torvalds * @args: Arguments for the format string 14621da177e4SLinus Torvalds * 14631da177e4SLinus Torvalds * The return value is the number of characters which have been written into 1464b921c69fSAnton Arapov * the @buf not including the trailing '\0'. If @size is == 0 the function 14651da177e4SLinus Torvalds * returns 0. 14661da177e4SLinus Torvalds * 1467ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using scnprintf(). 146820036fdcSAndi Kleen * 146920036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 14701da177e4SLinus Torvalds */ 14711da177e4SLinus Torvalds int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) 14721da177e4SLinus Torvalds { 14731da177e4SLinus Torvalds int i; 14741da177e4SLinus Torvalds 14751da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 14767b9186f5SAndré Goddard Rosa 1477b921c69fSAnton Arapov if (likely(i < size)) 1478b921c69fSAnton Arapov return i; 1479b921c69fSAnton Arapov if (size != 0) 1480b921c69fSAnton Arapov return size - 1; 1481b921c69fSAnton Arapov return 0; 14821da177e4SLinus Torvalds } 14831da177e4SLinus Torvalds EXPORT_SYMBOL(vscnprintf); 14841da177e4SLinus Torvalds 14851da177e4SLinus Torvalds /** 14861da177e4SLinus Torvalds * snprintf - Format a string and place it in a buffer 14871da177e4SLinus Torvalds * @buf: The buffer to place the result into 14881da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 14891da177e4SLinus Torvalds * @fmt: The format string to use 14901da177e4SLinus Torvalds * @...: Arguments for the format string 14911da177e4SLinus Torvalds * 14921da177e4SLinus Torvalds * The return value is the number of characters which would be 14931da177e4SLinus Torvalds * generated for the given input, excluding the trailing null, 14941da177e4SLinus Torvalds * as per ISO C99. If the return is greater than or equal to 14951da177e4SLinus Torvalds * @size, the resulting string is truncated. 149620036fdcSAndi Kleen * 149720036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 14981da177e4SLinus Torvalds */ 14991da177e4SLinus Torvalds int snprintf(char *buf, size_t size, const char *fmt, ...) 15001da177e4SLinus Torvalds { 15011da177e4SLinus Torvalds va_list args; 15021da177e4SLinus Torvalds int i; 15031da177e4SLinus Torvalds 15041da177e4SLinus Torvalds va_start(args, fmt); 15051da177e4SLinus Torvalds i = vsnprintf(buf, size, fmt, args); 15061da177e4SLinus Torvalds va_end(args); 15077b9186f5SAndré Goddard Rosa 15081da177e4SLinus Torvalds return i; 15091da177e4SLinus Torvalds } 15101da177e4SLinus Torvalds EXPORT_SYMBOL(snprintf); 15111da177e4SLinus Torvalds 15121da177e4SLinus Torvalds /** 15131da177e4SLinus Torvalds * scnprintf - Format a string and place it in a buffer 15141da177e4SLinus Torvalds * @buf: The buffer to place the result into 15151da177e4SLinus Torvalds * @size: The size of the buffer, including the trailing null space 15161da177e4SLinus Torvalds * @fmt: The format string to use 15171da177e4SLinus Torvalds * @...: Arguments for the format string 15181da177e4SLinus Torvalds * 15191da177e4SLinus Torvalds * The return value is the number of characters written into @buf not including 1520b903c0b8SChangli Gao * the trailing '\0'. If @size is == 0 the function returns 0. 15211da177e4SLinus Torvalds */ 15221da177e4SLinus Torvalds 15231da177e4SLinus Torvalds int scnprintf(char *buf, size_t size, const char *fmt, ...) 15241da177e4SLinus Torvalds { 15251da177e4SLinus Torvalds va_list args; 15261da177e4SLinus Torvalds int i; 15271da177e4SLinus Torvalds 15281da177e4SLinus Torvalds va_start(args, fmt); 1529b921c69fSAnton Arapov i = vscnprintf(buf, size, fmt, args); 15301da177e4SLinus Torvalds va_end(args); 15317b9186f5SAndré Goddard Rosa 1532b903c0b8SChangli Gao return i; 15331da177e4SLinus Torvalds } 15341da177e4SLinus Torvalds EXPORT_SYMBOL(scnprintf); 15351da177e4SLinus Torvalds 15361da177e4SLinus Torvalds /** 15371da177e4SLinus Torvalds * vsprintf - Format a string and place it in a buffer 15381da177e4SLinus Torvalds * @buf: The buffer to place the result into 15391da177e4SLinus Torvalds * @fmt: The format string to use 15401da177e4SLinus Torvalds * @args: Arguments for the format string 15411da177e4SLinus Torvalds * 15421da177e4SLinus Torvalds * The function returns the number of characters written 154372fd4a35SRobert P. J. Day * into @buf. Use vsnprintf() or vscnprintf() in order to avoid 15441da177e4SLinus Torvalds * buffer overflows. 15451da177e4SLinus Torvalds * 1546ba1835ebSUwe Kleine-König * If you're not already dealing with a va_list consider using sprintf(). 154720036fdcSAndi Kleen * 154820036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 15491da177e4SLinus Torvalds */ 15501da177e4SLinus Torvalds int vsprintf(char *buf, const char *fmt, va_list args) 15511da177e4SLinus Torvalds { 15521da177e4SLinus Torvalds return vsnprintf(buf, INT_MAX, fmt, args); 15531da177e4SLinus Torvalds } 15541da177e4SLinus Torvalds EXPORT_SYMBOL(vsprintf); 15551da177e4SLinus Torvalds 15561da177e4SLinus Torvalds /** 15571da177e4SLinus Torvalds * sprintf - Format a string and place it in a buffer 15581da177e4SLinus Torvalds * @buf: The buffer to place the result into 15591da177e4SLinus Torvalds * @fmt: The format string to use 15601da177e4SLinus Torvalds * @...: Arguments for the format string 15611da177e4SLinus Torvalds * 15621da177e4SLinus Torvalds * The function returns the number of characters written 156372fd4a35SRobert P. J. Day * into @buf. Use snprintf() or scnprintf() in order to avoid 15641da177e4SLinus Torvalds * buffer overflows. 156520036fdcSAndi Kleen * 156620036fdcSAndi Kleen * See the vsnprintf() documentation for format string extensions over C99. 15671da177e4SLinus Torvalds */ 15681da177e4SLinus Torvalds int sprintf(char *buf, const char *fmt, ...) 15691da177e4SLinus Torvalds { 15701da177e4SLinus Torvalds va_list args; 15711da177e4SLinus Torvalds int i; 15721da177e4SLinus Torvalds 15731da177e4SLinus Torvalds va_start(args, fmt); 15741da177e4SLinus Torvalds i = vsnprintf(buf, INT_MAX, fmt, args); 15751da177e4SLinus Torvalds va_end(args); 15767b9186f5SAndré Goddard Rosa 15771da177e4SLinus Torvalds return i; 15781da177e4SLinus Torvalds } 15791da177e4SLinus Torvalds EXPORT_SYMBOL(sprintf); 15801da177e4SLinus Torvalds 15814370aa4aSLai Jiangshan #ifdef CONFIG_BINARY_PRINTF 15824370aa4aSLai Jiangshan /* 15834370aa4aSLai Jiangshan * bprintf service: 15844370aa4aSLai Jiangshan * vbin_printf() - VA arguments to binary data 15854370aa4aSLai Jiangshan * bstr_printf() - Binary data to text string 15864370aa4aSLai Jiangshan */ 15874370aa4aSLai Jiangshan 15884370aa4aSLai Jiangshan /** 15894370aa4aSLai Jiangshan * vbin_printf - Parse a format string and place args' binary value in a buffer 15904370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 15914370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 15924370aa4aSLai Jiangshan * @fmt: The format string to use 15934370aa4aSLai Jiangshan * @args: Arguments for the format string 15944370aa4aSLai Jiangshan * 15954370aa4aSLai Jiangshan * The format follows C99 vsnprintf, except %n is ignored, and its argument 15964370aa4aSLai Jiangshan * is skiped. 15974370aa4aSLai Jiangshan * 15984370aa4aSLai Jiangshan * The return value is the number of words(32bits) which would be generated for 15994370aa4aSLai Jiangshan * the given input. 16004370aa4aSLai Jiangshan * 16014370aa4aSLai Jiangshan * NOTE: 16024370aa4aSLai Jiangshan * If the return value is greater than @size, the resulting bin_buf is NOT 16034370aa4aSLai Jiangshan * valid for bstr_printf(). 16044370aa4aSLai Jiangshan */ 16054370aa4aSLai Jiangshan int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) 16064370aa4aSLai Jiangshan { 1607fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 16084370aa4aSLai Jiangshan char *str, *end; 16094370aa4aSLai Jiangshan 16104370aa4aSLai Jiangshan str = (char *)bin_buf; 16114370aa4aSLai Jiangshan end = (char *)(bin_buf + size); 16124370aa4aSLai Jiangshan 16134370aa4aSLai Jiangshan #define save_arg(type) \ 16144370aa4aSLai Jiangshan do { \ 16154370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 16164370aa4aSLai Jiangshan unsigned long long value; \ 16174370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(u32)); \ 16184370aa4aSLai Jiangshan value = va_arg(args, unsigned long long); \ 16194370aa4aSLai Jiangshan if (str + sizeof(type) <= end) { \ 16204370aa4aSLai Jiangshan *(u32 *)str = *(u32 *)&value; \ 16214370aa4aSLai Jiangshan *(u32 *)(str + 4) = *((u32 *)&value + 1); \ 16224370aa4aSLai Jiangshan } \ 16234370aa4aSLai Jiangshan } else { \ 16244370aa4aSLai Jiangshan unsigned long value; \ 16254370aa4aSLai Jiangshan str = PTR_ALIGN(str, sizeof(type)); \ 16264370aa4aSLai Jiangshan value = va_arg(args, int); \ 16274370aa4aSLai Jiangshan if (str + sizeof(type) <= end) \ 16284370aa4aSLai Jiangshan *(typeof(type) *)str = (type)value; \ 16294370aa4aSLai Jiangshan } \ 16304370aa4aSLai Jiangshan str += sizeof(type); \ 16314370aa4aSLai Jiangshan } while (0) 16324370aa4aSLai Jiangshan 1633fef20d9cSFrederic Weisbecker while (*fmt) { 1634d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 16354370aa4aSLai Jiangshan 1636fef20d9cSFrederic Weisbecker fmt += read; 1637fef20d9cSFrederic Weisbecker 1638fef20d9cSFrederic Weisbecker switch (spec.type) { 1639fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: 1640d4be151bSAndré Goddard Rosa case FORMAT_TYPE_INVALID: 1641d4be151bSAndré Goddard Rosa case FORMAT_TYPE_PERCENT_CHAR: 1642fef20d9cSFrederic Weisbecker break; 1643fef20d9cSFrederic Weisbecker 1644ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 1645fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 16464370aa4aSLai Jiangshan save_arg(int); 1647fef20d9cSFrederic Weisbecker break; 16484370aa4aSLai Jiangshan 1649fef20d9cSFrederic Weisbecker case FORMAT_TYPE_CHAR: 16504370aa4aSLai Jiangshan save_arg(char); 1651fef20d9cSFrederic Weisbecker break; 1652fef20d9cSFrederic Weisbecker 1653fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 16544370aa4aSLai Jiangshan const char *save_str = va_arg(args, char *); 16554370aa4aSLai Jiangshan size_t len; 16566c356634SAndré Goddard Rosa 16574370aa4aSLai Jiangshan if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE 16584370aa4aSLai Jiangshan || (unsigned long)save_str < PAGE_SIZE) 16590f4f81dcSAndré Goddard Rosa save_str = "(null)"; 16606c356634SAndré Goddard Rosa len = strlen(save_str) + 1; 16616c356634SAndré Goddard Rosa if (str + len < end) 16626c356634SAndré Goddard Rosa memcpy(str, save_str, len); 16636c356634SAndré Goddard Rosa str += len; 1664fef20d9cSFrederic Weisbecker break; 16654370aa4aSLai Jiangshan } 1666fef20d9cSFrederic Weisbecker 1667fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 16684370aa4aSLai Jiangshan save_arg(void *); 16694370aa4aSLai Jiangshan /* skip all alphanumeric pointer suffixes */ 1670fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 16714370aa4aSLai Jiangshan fmt++; 1672fef20d9cSFrederic Weisbecker break; 1673fef20d9cSFrederic Weisbecker 1674fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NRCHARS: { 16754370aa4aSLai Jiangshan /* skip %n 's argument */ 1676ef0658f3SJoe Perches u8 qualifier = spec.qualifier; 16774370aa4aSLai Jiangshan void *skip_arg; 16784370aa4aSLai Jiangshan if (qualifier == 'l') 16794370aa4aSLai Jiangshan skip_arg = va_arg(args, long *); 168075fb8f26SAndy Shevchenko else if (_tolower(qualifier) == 'z') 16814370aa4aSLai Jiangshan skip_arg = va_arg(args, size_t *); 16824370aa4aSLai Jiangshan else 16834370aa4aSLai Jiangshan skip_arg = va_arg(args, int *); 1684fef20d9cSFrederic Weisbecker break; 16854370aa4aSLai Jiangshan } 16864370aa4aSLai Jiangshan 1687fef20d9cSFrederic Weisbecker default: 1688fef20d9cSFrederic Weisbecker switch (spec.type) { 1689fef20d9cSFrederic Weisbecker 1690fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 1691fef20d9cSFrederic Weisbecker save_arg(long long); 1692fef20d9cSFrederic Weisbecker break; 1693fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 1694fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 1695fef20d9cSFrederic Weisbecker save_arg(unsigned long); 1696fef20d9cSFrederic Weisbecker break; 1697fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 1698fef20d9cSFrederic Weisbecker save_arg(size_t); 1699fef20d9cSFrederic Weisbecker break; 1700fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 1701fef20d9cSFrederic Weisbecker save_arg(ptrdiff_t); 1702fef20d9cSFrederic Weisbecker break; 1703a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 1704a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 1705a4e94ef0SZhaolei save_arg(char); 1706a4e94ef0SZhaolei break; 1707fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 1708fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 1709fef20d9cSFrederic Weisbecker save_arg(short); 1710fef20d9cSFrederic Weisbecker break; 1711fef20d9cSFrederic Weisbecker default: 1712fef20d9cSFrederic Weisbecker save_arg(int); 1713fef20d9cSFrederic Weisbecker } 1714fef20d9cSFrederic Weisbecker } 1715fef20d9cSFrederic Weisbecker } 1716fef20d9cSFrederic Weisbecker 17177b9186f5SAndré Goddard Rosa return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; 1718fef20d9cSFrederic Weisbecker #undef save_arg 17194370aa4aSLai Jiangshan } 17204370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(vbin_printf); 17214370aa4aSLai Jiangshan 17224370aa4aSLai Jiangshan /** 17234370aa4aSLai Jiangshan * bstr_printf - Format a string from binary arguments and place it in a buffer 17244370aa4aSLai Jiangshan * @buf: The buffer to place the result into 17254370aa4aSLai Jiangshan * @size: The size of the buffer, including the trailing null space 17264370aa4aSLai Jiangshan * @fmt: The format string to use 17274370aa4aSLai Jiangshan * @bin_buf: Binary arguments for the format string 17284370aa4aSLai Jiangshan * 17294370aa4aSLai Jiangshan * This function like C99 vsnprintf, but the difference is that vsnprintf gets 17304370aa4aSLai Jiangshan * arguments from stack, and bstr_printf gets arguments from @bin_buf which is 17314370aa4aSLai Jiangshan * a binary buffer that generated by vbin_printf. 17324370aa4aSLai Jiangshan * 17334370aa4aSLai Jiangshan * The format follows C99 vsnprintf, but has some extensions: 17340efb4d20SSteven Rostedt * see vsnprintf comment for details. 17354370aa4aSLai Jiangshan * 17364370aa4aSLai Jiangshan * The return value is the number of characters which would 17374370aa4aSLai Jiangshan * be generated for the given input, excluding the trailing 17384370aa4aSLai Jiangshan * '\0', as per ISO C99. If you want to have the exact 17394370aa4aSLai Jiangshan * number of characters written into @buf as return value 17404370aa4aSLai Jiangshan * (not including the trailing '\0'), use vscnprintf(). If the 17414370aa4aSLai Jiangshan * return is greater than or equal to @size, the resulting 17424370aa4aSLai Jiangshan * string is truncated. 17434370aa4aSLai Jiangshan */ 17444370aa4aSLai Jiangshan int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) 17454370aa4aSLai Jiangshan { 1746fef20d9cSFrederic Weisbecker struct printf_spec spec = {0}; 1747d4be151bSAndré Goddard Rosa char *str, *end; 1748d4be151bSAndré Goddard Rosa const char *args = (const char *)bin_buf; 17494370aa4aSLai Jiangshan 17502f30b1f9SMarcin Slusarz if (WARN_ON_ONCE((int) size < 0)) 17514370aa4aSLai Jiangshan return 0; 17524370aa4aSLai Jiangshan 17534370aa4aSLai Jiangshan str = buf; 17544370aa4aSLai Jiangshan end = buf + size; 17554370aa4aSLai Jiangshan 17564370aa4aSLai Jiangshan #define get_arg(type) \ 17574370aa4aSLai Jiangshan ({ \ 17584370aa4aSLai Jiangshan typeof(type) value; \ 17594370aa4aSLai Jiangshan if (sizeof(type) == 8) { \ 17604370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(u32)); \ 17614370aa4aSLai Jiangshan *(u32 *)&value = *(u32 *)args; \ 17624370aa4aSLai Jiangshan *((u32 *)&value + 1) = *(u32 *)(args + 4); \ 17634370aa4aSLai Jiangshan } else { \ 17644370aa4aSLai Jiangshan args = PTR_ALIGN(args, sizeof(type)); \ 17654370aa4aSLai Jiangshan value = *(typeof(type) *)args; \ 17664370aa4aSLai Jiangshan } \ 17674370aa4aSLai Jiangshan args += sizeof(type); \ 17684370aa4aSLai Jiangshan value; \ 17694370aa4aSLai Jiangshan }) 17704370aa4aSLai Jiangshan 17714370aa4aSLai Jiangshan /* Make sure end is always >= buf */ 17724370aa4aSLai Jiangshan if (end < buf) { 17734370aa4aSLai Jiangshan end = ((void *)-1); 17744370aa4aSLai Jiangshan size = end - buf; 17754370aa4aSLai Jiangshan } 17764370aa4aSLai Jiangshan 1777fef20d9cSFrederic Weisbecker while (*fmt) { 1778fef20d9cSFrederic Weisbecker const char *old_fmt = fmt; 1779d4be151bSAndré Goddard Rosa int read = format_decode(fmt, &spec); 1780fef20d9cSFrederic Weisbecker 1781fef20d9cSFrederic Weisbecker fmt += read; 1782fef20d9cSFrederic Weisbecker 1783fef20d9cSFrederic Weisbecker switch (spec.type) { 1784fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NONE: { 1785fef20d9cSFrederic Weisbecker int copy = read; 1786fef20d9cSFrederic Weisbecker if (str < end) { 1787fef20d9cSFrederic Weisbecker if (copy > end - str) 1788fef20d9cSFrederic Weisbecker copy = end - str; 1789fef20d9cSFrederic Weisbecker memcpy(str, old_fmt, copy); 1790fef20d9cSFrederic Weisbecker } 1791fef20d9cSFrederic Weisbecker str += read; 1792fef20d9cSFrederic Weisbecker break; 17934370aa4aSLai Jiangshan } 17944370aa4aSLai Jiangshan 1795ed681a91SVegard Nossum case FORMAT_TYPE_WIDTH: 1796fef20d9cSFrederic Weisbecker spec.field_width = get_arg(int); 1797fef20d9cSFrederic Weisbecker break; 17984370aa4aSLai Jiangshan 1799fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PRECISION: 1800fef20d9cSFrederic Weisbecker spec.precision = get_arg(int); 1801fef20d9cSFrederic Weisbecker break; 18024370aa4aSLai Jiangshan 1803d4be151bSAndré Goddard Rosa case FORMAT_TYPE_CHAR: { 1804d4be151bSAndré Goddard Rosa char c; 1805d4be151bSAndré Goddard Rosa 1806fef20d9cSFrederic Weisbecker if (!(spec.flags & LEFT)) { 1807fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 18084370aa4aSLai Jiangshan if (str < end) 18094370aa4aSLai Jiangshan *str = ' '; 18104370aa4aSLai Jiangshan ++str; 18114370aa4aSLai Jiangshan } 18124370aa4aSLai Jiangshan } 18134370aa4aSLai Jiangshan c = (unsigned char) get_arg(char); 18144370aa4aSLai Jiangshan if (str < end) 18154370aa4aSLai Jiangshan *str = c; 18164370aa4aSLai Jiangshan ++str; 1817fef20d9cSFrederic Weisbecker while (--spec.field_width > 0) { 18184370aa4aSLai Jiangshan if (str < end) 18194370aa4aSLai Jiangshan *str = ' '; 18204370aa4aSLai Jiangshan ++str; 18214370aa4aSLai Jiangshan } 1822fef20d9cSFrederic Weisbecker break; 1823d4be151bSAndré Goddard Rosa } 18244370aa4aSLai Jiangshan 1825fef20d9cSFrederic Weisbecker case FORMAT_TYPE_STR: { 18264370aa4aSLai Jiangshan const char *str_arg = args; 1827d4be151bSAndré Goddard Rosa args += strlen(str_arg) + 1; 1828fef20d9cSFrederic Weisbecker str = string(str, end, (char *)str_arg, spec); 1829fef20d9cSFrederic Weisbecker break; 18304370aa4aSLai Jiangshan } 18314370aa4aSLai Jiangshan 1832fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTR: 1833fef20d9cSFrederic Weisbecker str = pointer(fmt+1, str, end, get_arg(void *), spec); 1834fef20d9cSFrederic Weisbecker while (isalnum(*fmt)) 18354370aa4aSLai Jiangshan fmt++; 1836fef20d9cSFrederic Weisbecker break; 18374370aa4aSLai Jiangshan 1838fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PERCENT_CHAR: 1839fef20d9cSFrederic Weisbecker case FORMAT_TYPE_INVALID: 18404370aa4aSLai Jiangshan if (str < end) 18414370aa4aSLai Jiangshan *str = '%'; 18424370aa4aSLai Jiangshan ++str; 1843fef20d9cSFrederic Weisbecker break; 1844fef20d9cSFrederic Weisbecker 1845fef20d9cSFrederic Weisbecker case FORMAT_TYPE_NRCHARS: 1846fef20d9cSFrederic Weisbecker /* skip */ 1847fef20d9cSFrederic Weisbecker break; 1848fef20d9cSFrederic Weisbecker 1849d4be151bSAndré Goddard Rosa default: { 1850d4be151bSAndré Goddard Rosa unsigned long long num; 1851d4be151bSAndré Goddard Rosa 1852fef20d9cSFrederic Weisbecker switch (spec.type) { 1853fef20d9cSFrederic Weisbecker 1854fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG_LONG: 18554370aa4aSLai Jiangshan num = get_arg(long long); 1856fef20d9cSFrederic Weisbecker break; 1857fef20d9cSFrederic Weisbecker case FORMAT_TYPE_ULONG: 1858fef20d9cSFrederic Weisbecker case FORMAT_TYPE_LONG: 1859fef20d9cSFrederic Weisbecker num = get_arg(unsigned long); 1860fef20d9cSFrederic Weisbecker break; 1861fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SIZE_T: 18624370aa4aSLai Jiangshan num = get_arg(size_t); 1863fef20d9cSFrederic Weisbecker break; 1864fef20d9cSFrederic Weisbecker case FORMAT_TYPE_PTRDIFF: 18654370aa4aSLai Jiangshan num = get_arg(ptrdiff_t); 1866fef20d9cSFrederic Weisbecker break; 1867a4e94ef0SZhaolei case FORMAT_TYPE_UBYTE: 1868a4e94ef0SZhaolei num = get_arg(unsigned char); 1869a4e94ef0SZhaolei break; 1870a4e94ef0SZhaolei case FORMAT_TYPE_BYTE: 1871a4e94ef0SZhaolei num = get_arg(signed char); 1872a4e94ef0SZhaolei break; 1873fef20d9cSFrederic Weisbecker case FORMAT_TYPE_USHORT: 1874fef20d9cSFrederic Weisbecker num = get_arg(unsigned short); 1875fef20d9cSFrederic Weisbecker break; 1876fef20d9cSFrederic Weisbecker case FORMAT_TYPE_SHORT: 1877fef20d9cSFrederic Weisbecker num = get_arg(short); 1878fef20d9cSFrederic Weisbecker break; 1879fef20d9cSFrederic Weisbecker case FORMAT_TYPE_UINT: 18804370aa4aSLai Jiangshan num = get_arg(unsigned int); 1881fef20d9cSFrederic Weisbecker break; 1882fef20d9cSFrederic Weisbecker default: 1883fef20d9cSFrederic Weisbecker num = get_arg(int); 18844370aa4aSLai Jiangshan } 1885fef20d9cSFrederic Weisbecker 1886fef20d9cSFrederic Weisbecker str = number(str, end, num, spec); 1887d4be151bSAndré Goddard Rosa } /* default: */ 1888d4be151bSAndré Goddard Rosa } /* switch(spec.type) */ 1889d4be151bSAndré Goddard Rosa } /* while(*fmt) */ 1890fef20d9cSFrederic Weisbecker 18914370aa4aSLai Jiangshan if (size > 0) { 18924370aa4aSLai Jiangshan if (str < end) 18934370aa4aSLai Jiangshan *str = '\0'; 18944370aa4aSLai Jiangshan else 18954370aa4aSLai Jiangshan end[-1] = '\0'; 18964370aa4aSLai Jiangshan } 1897fef20d9cSFrederic Weisbecker 18984370aa4aSLai Jiangshan #undef get_arg 18994370aa4aSLai Jiangshan 19004370aa4aSLai Jiangshan /* the trailing null byte doesn't count towards the total */ 19014370aa4aSLai Jiangshan return str - buf; 19024370aa4aSLai Jiangshan } 19034370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bstr_printf); 19044370aa4aSLai Jiangshan 19054370aa4aSLai Jiangshan /** 19064370aa4aSLai Jiangshan * bprintf - Parse a format string and place args' binary value in a buffer 19074370aa4aSLai Jiangshan * @bin_buf: The buffer to place args' binary value 19084370aa4aSLai Jiangshan * @size: The size of the buffer(by words(32bits), not characters) 19094370aa4aSLai Jiangshan * @fmt: The format string to use 19104370aa4aSLai Jiangshan * @...: Arguments for the format string 19114370aa4aSLai Jiangshan * 19124370aa4aSLai Jiangshan * The function returns the number of words(u32) written 19134370aa4aSLai Jiangshan * into @bin_buf. 19144370aa4aSLai Jiangshan */ 19154370aa4aSLai Jiangshan int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) 19164370aa4aSLai Jiangshan { 19174370aa4aSLai Jiangshan va_list args; 19184370aa4aSLai Jiangshan int ret; 19194370aa4aSLai Jiangshan 19204370aa4aSLai Jiangshan va_start(args, fmt); 19214370aa4aSLai Jiangshan ret = vbin_printf(bin_buf, size, fmt, args); 19224370aa4aSLai Jiangshan va_end(args); 19237b9186f5SAndré Goddard Rosa 19244370aa4aSLai Jiangshan return ret; 19254370aa4aSLai Jiangshan } 19264370aa4aSLai Jiangshan EXPORT_SYMBOL_GPL(bprintf); 19274370aa4aSLai Jiangshan 19284370aa4aSLai Jiangshan #endif /* CONFIG_BINARY_PRINTF */ 19294370aa4aSLai Jiangshan 19301da177e4SLinus Torvalds /** 19311da177e4SLinus Torvalds * vsscanf - Unformat a buffer into a list of arguments 19321da177e4SLinus Torvalds * @buf: input buffer 19331da177e4SLinus Torvalds * @fmt: format of buffer 19341da177e4SLinus Torvalds * @args: arguments 19351da177e4SLinus Torvalds */ 19361da177e4SLinus Torvalds int vsscanf(const char *buf, const char *fmt, va_list args) 19371da177e4SLinus Torvalds { 19381da177e4SLinus Torvalds const char *str = buf; 19391da177e4SLinus Torvalds char *next; 19401da177e4SLinus Torvalds char digit; 19411da177e4SLinus Torvalds int num = 0; 1942ef0658f3SJoe Perches u8 qualifier; 1943ef0658f3SJoe Perches u8 base; 1944ef0658f3SJoe Perches s16 field_width; 1945d4be151bSAndré Goddard Rosa bool is_sign; 19461da177e4SLinus Torvalds 19471da177e4SLinus Torvalds while (*fmt && *str) { 19481da177e4SLinus Torvalds /* skip any white space in format */ 19491da177e4SLinus Torvalds /* white space in format matchs any amount of 19501da177e4SLinus Torvalds * white space, including none, in the input. 19511da177e4SLinus Torvalds */ 19521da177e4SLinus Torvalds if (isspace(*fmt)) { 1953e7d2860bSAndré Goddard Rosa fmt = skip_spaces(++fmt); 1954e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 19551da177e4SLinus Torvalds } 19561da177e4SLinus Torvalds 19571da177e4SLinus Torvalds /* anything that is not a conversion must match exactly */ 19581da177e4SLinus Torvalds if (*fmt != '%' && *fmt) { 19591da177e4SLinus Torvalds if (*fmt++ != *str++) 19601da177e4SLinus Torvalds break; 19611da177e4SLinus Torvalds continue; 19621da177e4SLinus Torvalds } 19631da177e4SLinus Torvalds 19641da177e4SLinus Torvalds if (!*fmt) 19651da177e4SLinus Torvalds break; 19661da177e4SLinus Torvalds ++fmt; 19671da177e4SLinus Torvalds 19681da177e4SLinus Torvalds /* skip this conversion. 19691da177e4SLinus Torvalds * advance both strings to next white space 19701da177e4SLinus Torvalds */ 19711da177e4SLinus Torvalds if (*fmt == '*') { 19728fccae2cSAndy Spencer while (!isspace(*fmt) && *fmt != '%' && *fmt) 19731da177e4SLinus Torvalds fmt++; 19741da177e4SLinus Torvalds while (!isspace(*str) && *str) 19751da177e4SLinus Torvalds str++; 19761da177e4SLinus Torvalds continue; 19771da177e4SLinus Torvalds } 19781da177e4SLinus Torvalds 19791da177e4SLinus Torvalds /* get field width */ 19801da177e4SLinus Torvalds field_width = -1; 19811da177e4SLinus Torvalds if (isdigit(*fmt)) 19821da177e4SLinus Torvalds field_width = skip_atoi(&fmt); 19831da177e4SLinus Torvalds 19841da177e4SLinus Torvalds /* get conversion qualifier */ 19851da177e4SLinus Torvalds qualifier = -1; 198675fb8f26SAndy Shevchenko if (*fmt == 'h' || _tolower(*fmt) == 'l' || 198775fb8f26SAndy Shevchenko _tolower(*fmt) == 'z') { 19881da177e4SLinus Torvalds qualifier = *fmt++; 19891da177e4SLinus Torvalds if (unlikely(qualifier == *fmt)) { 19901da177e4SLinus Torvalds if (qualifier == 'h') { 19911da177e4SLinus Torvalds qualifier = 'H'; 19921da177e4SLinus Torvalds fmt++; 19931da177e4SLinus Torvalds } else if (qualifier == 'l') { 19941da177e4SLinus Torvalds qualifier = 'L'; 19951da177e4SLinus Torvalds fmt++; 19961da177e4SLinus Torvalds } 19971da177e4SLinus Torvalds } 19981da177e4SLinus Torvalds } 19991da177e4SLinus Torvalds 20001da177e4SLinus Torvalds if (!*fmt || !*str) 20011da177e4SLinus Torvalds break; 20021da177e4SLinus Torvalds 2003d4be151bSAndré Goddard Rosa base = 10; 2004d4be151bSAndré Goddard Rosa is_sign = 0; 2005d4be151bSAndré Goddard Rosa 20061da177e4SLinus Torvalds switch (*fmt++) { 20071da177e4SLinus Torvalds case 'c': 20081da177e4SLinus Torvalds { 20091da177e4SLinus Torvalds char *s = (char *)va_arg(args, char*); 20101da177e4SLinus Torvalds if (field_width == -1) 20111da177e4SLinus Torvalds field_width = 1; 20121da177e4SLinus Torvalds do { 20131da177e4SLinus Torvalds *s++ = *str++; 20141da177e4SLinus Torvalds } while (--field_width > 0 && *str); 20151da177e4SLinus Torvalds num++; 20161da177e4SLinus Torvalds } 20171da177e4SLinus Torvalds continue; 20181da177e4SLinus Torvalds case 's': 20191da177e4SLinus Torvalds { 20201da177e4SLinus Torvalds char *s = (char *)va_arg(args, char *); 20211da177e4SLinus Torvalds if (field_width == -1) 20224be929beSAlexey Dobriyan field_width = SHRT_MAX; 20231da177e4SLinus Torvalds /* first, skip leading white space in buffer */ 2024e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 20251da177e4SLinus Torvalds 20261da177e4SLinus Torvalds /* now copy until next white space */ 20277b9186f5SAndré Goddard Rosa while (*str && !isspace(*str) && field_width--) 20281da177e4SLinus Torvalds *s++ = *str++; 20291da177e4SLinus Torvalds *s = '\0'; 20301da177e4SLinus Torvalds num++; 20311da177e4SLinus Torvalds } 20321da177e4SLinus Torvalds continue; 20331da177e4SLinus Torvalds case 'n': 20341da177e4SLinus Torvalds /* return number of characters read so far */ 20351da177e4SLinus Torvalds { 20361da177e4SLinus Torvalds int *i = (int *)va_arg(args, int*); 20371da177e4SLinus Torvalds *i = str - buf; 20381da177e4SLinus Torvalds } 20391da177e4SLinus Torvalds continue; 20401da177e4SLinus Torvalds case 'o': 20411da177e4SLinus Torvalds base = 8; 20421da177e4SLinus Torvalds break; 20431da177e4SLinus Torvalds case 'x': 20441da177e4SLinus Torvalds case 'X': 20451da177e4SLinus Torvalds base = 16; 20461da177e4SLinus Torvalds break; 20471da177e4SLinus Torvalds case 'i': 20481da177e4SLinus Torvalds base = 0; 20491da177e4SLinus Torvalds case 'd': 20501da177e4SLinus Torvalds is_sign = 1; 20511da177e4SLinus Torvalds case 'u': 20521da177e4SLinus Torvalds break; 20531da177e4SLinus Torvalds case '%': 20541da177e4SLinus Torvalds /* looking for '%' in str */ 20551da177e4SLinus Torvalds if (*str++ != '%') 20561da177e4SLinus Torvalds return num; 20571da177e4SLinus Torvalds continue; 20581da177e4SLinus Torvalds default: 20591da177e4SLinus Torvalds /* invalid format; stop here */ 20601da177e4SLinus Torvalds return num; 20611da177e4SLinus Torvalds } 20621da177e4SLinus Torvalds 20631da177e4SLinus Torvalds /* have some sort of integer conversion. 20641da177e4SLinus Torvalds * first, skip white space in buffer. 20651da177e4SLinus Torvalds */ 2066e7d2860bSAndré Goddard Rosa str = skip_spaces(str); 20671da177e4SLinus Torvalds 20681da177e4SLinus Torvalds digit = *str; 20691da177e4SLinus Torvalds if (is_sign && digit == '-') 20701da177e4SLinus Torvalds digit = *(str + 1); 20711da177e4SLinus Torvalds 20721da177e4SLinus Torvalds if (!digit 20731da177e4SLinus Torvalds || (base == 16 && !isxdigit(digit)) 20741da177e4SLinus Torvalds || (base == 10 && !isdigit(digit)) 20751da177e4SLinus Torvalds || (base == 8 && (!isdigit(digit) || digit > '7')) 20761da177e4SLinus Torvalds || (base == 0 && !isdigit(digit))) 20771da177e4SLinus Torvalds break; 20781da177e4SLinus Torvalds 20791da177e4SLinus Torvalds switch (qualifier) { 20801da177e4SLinus Torvalds case 'H': /* that's 'hh' in format */ 20811da177e4SLinus Torvalds if (is_sign) { 20821da177e4SLinus Torvalds signed char *s = (signed char *)va_arg(args, signed char *); 20831da177e4SLinus Torvalds *s = (signed char)simple_strtol(str, &next, base); 20841da177e4SLinus Torvalds } else { 20851da177e4SLinus Torvalds unsigned char *s = (unsigned char *)va_arg(args, unsigned char *); 20861da177e4SLinus Torvalds *s = (unsigned char)simple_strtoul(str, &next, base); 20871da177e4SLinus Torvalds } 20881da177e4SLinus Torvalds break; 20891da177e4SLinus Torvalds case 'h': 20901da177e4SLinus Torvalds if (is_sign) { 20911da177e4SLinus Torvalds short *s = (short *)va_arg(args, short *); 20921da177e4SLinus Torvalds *s = (short)simple_strtol(str, &next, base); 20931da177e4SLinus Torvalds } else { 20941da177e4SLinus Torvalds unsigned short *s = (unsigned short *)va_arg(args, unsigned short *); 20951da177e4SLinus Torvalds *s = (unsigned short)simple_strtoul(str, &next, base); 20961da177e4SLinus Torvalds } 20971da177e4SLinus Torvalds break; 20981da177e4SLinus Torvalds case 'l': 20991da177e4SLinus Torvalds if (is_sign) { 21001da177e4SLinus Torvalds long *l = (long *)va_arg(args, long *); 21011da177e4SLinus Torvalds *l = simple_strtol(str, &next, base); 21021da177e4SLinus Torvalds } else { 21031da177e4SLinus Torvalds unsigned long *l = (unsigned long *)va_arg(args, unsigned long *); 21041da177e4SLinus Torvalds *l = simple_strtoul(str, &next, base); 21051da177e4SLinus Torvalds } 21061da177e4SLinus Torvalds break; 21071da177e4SLinus Torvalds case 'L': 21081da177e4SLinus Torvalds if (is_sign) { 21091da177e4SLinus Torvalds long long *l = (long long *)va_arg(args, long long *); 21101da177e4SLinus Torvalds *l = simple_strtoll(str, &next, base); 21111da177e4SLinus Torvalds } else { 21121da177e4SLinus Torvalds unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *); 21131da177e4SLinus Torvalds *l = simple_strtoull(str, &next, base); 21141da177e4SLinus Torvalds } 21151da177e4SLinus Torvalds break; 21161da177e4SLinus Torvalds case 'Z': 21171da177e4SLinus Torvalds case 'z': 21181da177e4SLinus Torvalds { 21191da177e4SLinus Torvalds size_t *s = (size_t *)va_arg(args, size_t *); 21201da177e4SLinus Torvalds *s = (size_t)simple_strtoul(str, &next, base); 21211da177e4SLinus Torvalds } 21221da177e4SLinus Torvalds break; 21231da177e4SLinus Torvalds default: 21241da177e4SLinus Torvalds if (is_sign) { 21251da177e4SLinus Torvalds int *i = (int *)va_arg(args, int *); 21261da177e4SLinus Torvalds *i = (int)simple_strtol(str, &next, base); 21271da177e4SLinus Torvalds } else { 21281da177e4SLinus Torvalds unsigned int *i = (unsigned int *)va_arg(args, unsigned int*); 21291da177e4SLinus Torvalds *i = (unsigned int)simple_strtoul(str, &next, base); 21301da177e4SLinus Torvalds } 21311da177e4SLinus Torvalds break; 21321da177e4SLinus Torvalds } 21331da177e4SLinus Torvalds num++; 21341da177e4SLinus Torvalds 21351da177e4SLinus Torvalds if (!next) 21361da177e4SLinus Torvalds break; 21371da177e4SLinus Torvalds str = next; 21381da177e4SLinus Torvalds } 2139c6b40d16SJohannes Berg 2140c6b40d16SJohannes Berg /* 2141c6b40d16SJohannes Berg * Now we've come all the way through so either the input string or the 2142c6b40d16SJohannes Berg * format ended. In the former case, there can be a %n at the current 2143c6b40d16SJohannes Berg * position in the format that needs to be filled. 2144c6b40d16SJohannes Berg */ 2145c6b40d16SJohannes Berg if (*fmt == '%' && *(fmt + 1) == 'n') { 2146c6b40d16SJohannes Berg int *p = (int *)va_arg(args, int *); 2147c6b40d16SJohannes Berg *p = str - buf; 2148c6b40d16SJohannes Berg } 2149c6b40d16SJohannes Berg 21501da177e4SLinus Torvalds return num; 21511da177e4SLinus Torvalds } 21521da177e4SLinus Torvalds EXPORT_SYMBOL(vsscanf); 21531da177e4SLinus Torvalds 21541da177e4SLinus Torvalds /** 21551da177e4SLinus Torvalds * sscanf - Unformat a buffer into a list of arguments 21561da177e4SLinus Torvalds * @buf: input buffer 21571da177e4SLinus Torvalds * @fmt: formatting of buffer 21581da177e4SLinus Torvalds * @...: resulting arguments 21591da177e4SLinus Torvalds */ 21601da177e4SLinus Torvalds int sscanf(const char *buf, const char *fmt, ...) 21611da177e4SLinus Torvalds { 21621da177e4SLinus Torvalds va_list args; 21631da177e4SLinus Torvalds int i; 21641da177e4SLinus Torvalds 21651da177e4SLinus Torvalds va_start(args, fmt); 21661da177e4SLinus Torvalds i = vsscanf(buf, fmt, args); 21671da177e4SLinus Torvalds va_end(args); 21687b9186f5SAndré Goddard Rosa 21691da177e4SLinus Torvalds return i; 21701da177e4SLinus Torvalds } 21711da177e4SLinus Torvalds EXPORT_SYMBOL(sscanf); 2172