| 1 | /* |
|---|
| 2 | * linux/lib/vsprintf.c |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 1991, 1992 Linus Torvalds |
|---|
| 5 | */ |
|---|
| 6 | |
|---|
| 7 | /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */ |
|---|
| 8 | /* |
|---|
| 9 | * Wirzenius wrote this portably, Torvalds fucked it up :-) |
|---|
| 10 | */ |
|---|
| 11 | |
|---|
| 12 | /* |
|---|
| 13 | * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com> |
|---|
| 14 | * - changed to provide snprintf and vsnprintf functions |
|---|
| 15 | * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de> |
|---|
| 16 | * - scnprintf and vscnprintf |
|---|
| 17 | */ |
|---|
| 18 | |
|---|
| 19 | #include <stdarg.h> |
|---|
| 20 | #include <linux/module.h> |
|---|
| 21 | #include <linux/types.h> |
|---|
| 22 | #include <linux/string.h> |
|---|
| 23 | #include <linux/ctype.h> |
|---|
| 24 | #include <linux/kernel.h> |
|---|
| 25 | #include <linux/kallsyms.h> |
|---|
| 26 | #include <linux/uaccess.h> |
|---|
| 27 | #include <linux/ioport.h> |
|---|
| 28 | #include <net/addrconf.h> |
|---|
| 29 | |
|---|
| 30 | #include <asm/page.h> /* for PAGE_SIZE */ |
|---|
| 31 | #include <asm/div64.h> |
|---|
| 32 | #include <asm/sections.h> /* for dereference_function_descriptor() */ |
|---|
| 33 | #include "compat.h" |
|---|
| 34 | |
|---|
| 35 | /* Works only for digits and letters, but small and fast */ |
|---|
| 36 | #define TOLOWER(x) ((x) | 0x20) |
|---|
| 37 | |
|---|
| 38 | static int skip_atoi(const char **s) |
|---|
| 39 | { |
|---|
| 40 | int i = 0; |
|---|
| 41 | |
|---|
| 42 | while (isdigit(**s)) |
|---|
| 43 | i = i*10 + *((*s)++) - '0'; |
|---|
| 44 | return i; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | /* Decimal conversion is by far the most typical, and is used |
|---|
| 48 | * for /proc and /sys data. This directly impacts e.g. top performance |
|---|
| 49 | * with many processes running. We optimize it for speed |
|---|
| 50 | * using code from |
|---|
| 51 | * http://www.cs.uiowa.edu/~jones/bcd/decimal.html |
|---|
| 52 | * (with permission from the author, Douglas W. Jones). */ |
|---|
| 53 | |
|---|
| 54 | /* Formats correctly any integer in [0,99999]. |
|---|
| 55 | * Outputs from one to five digits depending on input. |
|---|
| 56 | * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */ |
|---|
| 57 | static char *put_dec_trunc(char *buf, unsigned q) |
|---|
| 58 | { |
|---|
| 59 | unsigned d3, d2, d1, d0; |
|---|
| 60 | d1 = (q>>4) & 0xf; |
|---|
| 61 | d2 = (q>>8) & 0xf; |
|---|
| 62 | d3 = (q>>12); |
|---|
| 63 | |
|---|
| 64 | d0 = 6*(d3 + d2 + d1) + (q & 0xf); |
|---|
| 65 | q = (d0 * 0xcd) >> 11; |
|---|
| 66 | d0 = d0 - 10*q; |
|---|
| 67 | *buf++ = d0 + '0'; /* least significant digit */ |
|---|
| 68 | d1 = q + 9*d3 + 5*d2 + d1; |
|---|
| 69 | if (d1 != 0) { |
|---|
| 70 | q = (d1 * 0xcd) >> 11; |
|---|
| 71 | d1 = d1 - 10*q; |
|---|
| 72 | *buf++ = d1 + '0'; /* next digit */ |
|---|
| 73 | |
|---|
| 74 | d2 = q + 2*d2; |
|---|
| 75 | if ((d2 != 0) || (d3 != 0)) { |
|---|
| 76 | q = (d2 * 0xd) >> 7; |
|---|
| 77 | d2 = d2 - 10*q; |
|---|
| 78 | *buf++ = d2 + '0'; /* next digit */ |
|---|
| 79 | |
|---|
| 80 | d3 = q + 4*d3; |
|---|
| 81 | if (d3 != 0) { |
|---|
| 82 | q = (d3 * 0xcd) >> 11; |
|---|
| 83 | d3 = d3 - 10*q; |
|---|
| 84 | *buf++ = d3 + '0'; /* next digit */ |
|---|
| 85 | if (q != 0) |
|---|
| 86 | /* most sign. digit */ |
|---|
| 87 | *buf++ = q + '0'; |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | return buf; |
|---|
| 92 | } |
|---|
| 93 | /* Same with if's removed. Always emits five digits */ |
|---|
| 94 | static char *put_dec_full(char *buf, unsigned q) |
|---|
| 95 | { |
|---|
| 96 | /* BTW, if q is in [0,9999], 8-bit ints will be enough, */ |
|---|
| 97 | /* but anyway, gcc produces better code with full-sized ints */ |
|---|
| 98 | unsigned d3, d2, d1, d0; |
|---|
| 99 | d1 = (q>>4) & 0xf; |
|---|
| 100 | d2 = (q>>8) & 0xf; |
|---|
| 101 | d3 = (q>>12); |
|---|
| 102 | |
|---|
| 103 | /* Possible ways to approx. divide by 10 */ |
|---|
| 104 | /* gcc -O2 replaces multiply with shifts and adds */ |
|---|
| 105 | /* (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386) */ |
|---|
| 106 | /* (x * 0x67) >> 10: 1100111 */ |
|---|
| 107 | /* (x * 0x34) >> 9: 110100 - same */ |
|---|
| 108 | /* (x * 0x1a) >> 8: 11010 - same */ |
|---|
| 109 | /* (x * 0x0d) >> 7: 1101 - same, shortest code (on i386) */ |
|---|
| 110 | |
|---|
| 111 | d0 = 6*(d3 + d2 + d1) + (q & 0xf); |
|---|
| 112 | q = (d0 * 0xcd) >> 11; |
|---|
| 113 | d0 = d0 - 10*q; |
|---|
| 114 | *buf++ = d0 + '0'; |
|---|
| 115 | d1 = q + 9*d3 + 5*d2 + d1; |
|---|
| 116 | q = (d1 * 0xcd) >> 11; |
|---|
| 117 | d1 = d1 - 10*q; |
|---|
| 118 | *buf++ = d1 + '0'; |
|---|
| 119 | |
|---|
| 120 | d2 = q + 2*d2; |
|---|
| 121 | q = (d2 * 0xd) >> 7; |
|---|
| 122 | d2 = d2 - 10*q; |
|---|
| 123 | *buf++ = d2 + '0'; |
|---|
| 124 | |
|---|
| 125 | d3 = q + 4*d3; |
|---|
| 126 | q = (d3 * 0xcd) >> 11; /* - shorter code */ |
|---|
| 127 | /* q = (d3 * 0x67) >> 10; - would also work */ |
|---|
| 128 | d3 = d3 - 10*q; |
|---|
| 129 | *buf++ = d3 + '0'; |
|---|
| 130 | *buf++ = q + '0'; |
|---|
| 131 | return buf; |
|---|
| 132 | } |
|---|
| 133 | /* No inlining helps gcc to use registers better */ |
|---|
| 134 | static noinline char *put_dec(char *buf, unsigned long long num) |
|---|
| 135 | { |
|---|
| 136 | while (1) { |
|---|
| 137 | unsigned rem; |
|---|
| 138 | if (num < 100000) |
|---|
| 139 | return put_dec_trunc(buf, num); |
|---|
| 140 | rem = do_div(num, 100000); |
|---|
| 141 | buf = put_dec_full(buf, rem); |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | #define ZEROPAD 1 /* pad with zero */ |
|---|
| 146 | #define SIGN 2 /* unsigned/signed long */ |
|---|
| 147 | #define PLUS 4 /* show plus */ |
|---|
| 148 | #define SPACE 8 /* space if plus */ |
|---|
| 149 | #define LEFT 16 /* left justified */ |
|---|
| 150 | #define SMALL 32 /* Must be 32 == 0x20 */ |
|---|
| 151 | #define SPECIAL 64 /* 0x */ |
|---|
| 152 | |
|---|
| 153 | enum format_type { |
|---|
| 154 | FORMAT_TYPE_NONE, /* Just a string part */ |
|---|
| 155 | FORMAT_TYPE_WIDTH, |
|---|
| 156 | FORMAT_TYPE_PRECISION, |
|---|
| 157 | FORMAT_TYPE_CHAR, |
|---|
| 158 | FORMAT_TYPE_STR, |
|---|
| 159 | FORMAT_TYPE_PTR, |
|---|
| 160 | FORMAT_TYPE_PERCENT_CHAR, |
|---|
| 161 | FORMAT_TYPE_INVALID, |
|---|
| 162 | FORMAT_TYPE_LONG_LONG, |
|---|
| 163 | FORMAT_TYPE_ULONG, |
|---|
| 164 | FORMAT_TYPE_LONG, |
|---|
| 165 | FORMAT_TYPE_UBYTE, |
|---|
| 166 | FORMAT_TYPE_BYTE, |
|---|
| 167 | FORMAT_TYPE_USHORT, |
|---|
| 168 | FORMAT_TYPE_SHORT, |
|---|
| 169 | FORMAT_TYPE_UINT, |
|---|
| 170 | FORMAT_TYPE_INT, |
|---|
| 171 | FORMAT_TYPE_NRCHARS, |
|---|
| 172 | FORMAT_TYPE_SIZE_T, |
|---|
| 173 | FORMAT_TYPE_PTRDIFF |
|---|
| 174 | }; |
|---|
| 175 | |
|---|
| 176 | struct printf_spec { |
|---|
| 177 | enum format_type type; |
|---|
| 178 | int flags; /* flags to number() */ |
|---|
| 179 | int field_width; /* width of output field */ |
|---|
| 180 | int base; |
|---|
| 181 | int precision; /* # of digits/chars */ |
|---|
| 182 | int qualifier; |
|---|
| 183 | }; |
|---|
| 184 | |
|---|
| 185 | static char *number(char *buf, char *end, unsigned long long num, |
|---|
| 186 | struct printf_spec spec) |
|---|
| 187 | { |
|---|
| 188 | /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ |
|---|
| 189 | static const char digits[16] = "0123456789ABCDEF"; |
|---|
| 190 | |
|---|
| 191 | char tmp[66]; |
|---|
| 192 | char sign; |
|---|
| 193 | char locase; |
|---|
| 194 | int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); |
|---|
| 195 | int i; |
|---|
| 196 | |
|---|
| 197 | /* locase = 0 or 0x20. ORing digits or letters with 'locase' |
|---|
| 198 | * produces same digits or (maybe lowercased) letters */ |
|---|
| 199 | locase = (spec.flags & SMALL); |
|---|
| 200 | if (spec.flags & LEFT) |
|---|
| 201 | spec.flags &= ~ZEROPAD; |
|---|
| 202 | sign = 0; |
|---|
| 203 | if (spec.flags & SIGN) { |
|---|
| 204 | if ((signed long long) num < 0) { |
|---|
| 205 | sign = '-'; |
|---|
| 206 | num = -(signed long long) num; |
|---|
| 207 | spec.field_width--; |
|---|
| 208 | } else if (spec.flags & PLUS) { |
|---|
| 209 | sign = '+'; |
|---|
| 210 | spec.field_width--; |
|---|
| 211 | } else if (spec.flags & SPACE) { |
|---|
| 212 | sign = ' '; |
|---|
| 213 | spec.field_width--; |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| 216 | if (need_pfx) { |
|---|
| 217 | spec.field_width--; |
|---|
| 218 | if (spec.base == 16) |
|---|
| 219 | spec.field_width--; |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | /* generate full string in tmp[], in reverse order */ |
|---|
| 223 | i = 0; |
|---|
| 224 | if (num == 0) |
|---|
| 225 | tmp[i++] = '0'; |
|---|
| 226 | /* Generic code, for any base: |
|---|
| 227 | else do { |
|---|
| 228 | tmp[i++] = (digits[do_div(num,base)] | locase); |
|---|
| 229 | } while (num != 0); |
|---|
| 230 | */ |
|---|
| 231 | else if (spec.base != 10) { /* 8 or 16 */ |
|---|
| 232 | int mask = spec.base - 1; |
|---|
| 233 | int shift = 3; |
|---|
| 234 | if (spec.base == 16) |
|---|
| 235 | shift = 4; |
|---|
| 236 | do { |
|---|
| 237 | tmp[i++] = (digits[((unsigned char)num) & mask] |
|---|
| 238 | | locase); |
|---|
| 239 | num >>= shift; |
|---|
| 240 | } while (num); |
|---|
| 241 | } else { /* base 10 */ |
|---|
| 242 | i = put_dec(tmp, num) - tmp; |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | /* printing 100 using %2d gives "100", not "00" */ |
|---|
| 246 | if (i > spec.precision) |
|---|
| 247 | spec.precision = i; |
|---|
| 248 | /* leading space padding */ |
|---|
| 249 | spec.field_width -= spec.precision; |
|---|
| 250 | if (!(spec.flags & (ZEROPAD+LEFT))) { |
|---|
| 251 | while (--spec.field_width >= 0) { |
|---|
| 252 | if (buf < end) |
|---|
| 253 | *buf = ' '; |
|---|
| 254 | ++buf; |
|---|
| 255 | } |
|---|
| 256 | } |
|---|
| 257 | /* sign */ |
|---|
| 258 | if (sign) { |
|---|
| 259 | if (buf < end) |
|---|
| 260 | *buf = sign; |
|---|
| 261 | ++buf; |
|---|
| 262 | } |
|---|
| 263 | /* "0x" / "0" prefix */ |
|---|
| 264 | if (need_pfx) { |
|---|
| 265 | if (buf < end) |
|---|
| 266 | *buf = '0'; |
|---|
| 267 | ++buf; |
|---|
| 268 | if (spec.base == 16) { |
|---|
| 269 | if (buf < end) |
|---|
| 270 | *buf = ('X' | locase); |
|---|
| 271 | ++buf; |
|---|
| 272 | } |
|---|
| 273 | } |
|---|
| 274 | /* zero or space padding */ |
|---|
| 275 | if (!(spec.flags & LEFT)) { |
|---|
| 276 | char c = (spec.flags & ZEROPAD) ? '0' : ' '; |
|---|
| 277 | while (--spec.field_width >= 0) { |
|---|
| 278 | if (buf < end) |
|---|
| 279 | *buf = c; |
|---|
| 280 | ++buf; |
|---|
| 281 | } |
|---|
| 282 | } |
|---|
| 283 | /* hmm even more zero padding? */ |
|---|
| 284 | while (i <= --spec.precision) { |
|---|
| 285 | if (buf < end) |
|---|
| 286 | *buf = '0'; |
|---|
| 287 | ++buf; |
|---|
| 288 | } |
|---|
| 289 | /* actual digits of result */ |
|---|
| 290 | while (--i >= 0) { |
|---|
| 291 | if (buf < end) |
|---|
| 292 | *buf = tmp[i]; |
|---|
| 293 | ++buf; |
|---|
| 294 | } |
|---|
| 295 | /* trailing space padding */ |
|---|
| 296 | while (--spec.field_width >= 0) { |
|---|
| 297 | if (buf < end) |
|---|
| 298 | *buf = ' '; |
|---|
| 299 | ++buf; |
|---|
| 300 | } |
|---|
| 301 | return buf; |
|---|
| 302 | } |
|---|
| 303 | |
|---|
| 304 | static char *string(char *buf, char *end, char *s, struct printf_spec spec) |
|---|
| 305 | { |
|---|
| 306 | int len, i; |
|---|
| 307 | |
|---|
| 308 | if ((unsigned long)s < PAGE_SIZE) |
|---|
| 309 | s = "<NULL>"; |
|---|
| 310 | |
|---|
| 311 | len = strnlen(s, spec.precision); |
|---|
| 312 | |
|---|
| 313 | if (!(spec.flags & LEFT)) { |
|---|
| 314 | while (len < spec.field_width--) { |
|---|
| 315 | if (buf < end) |
|---|
| 316 | *buf = ' '; |
|---|
| 317 | ++buf; |
|---|
| 318 | } |
|---|
| 319 | } |
|---|
| 320 | for (i = 0; i < len; ++i) { |
|---|
| 321 | if (buf < end) |
|---|
| 322 | *buf = *s; |
|---|
| 323 | ++buf; ++s; |
|---|
| 324 | } |
|---|
| 325 | while (len < spec.field_width--) { |
|---|
| 326 | if (buf < end) |
|---|
| 327 | *buf = ' '; |
|---|
| 328 | ++buf; |
|---|
| 329 | } |
|---|
| 330 | return buf; |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | static char *resource_string(char *buf, char *end, struct resource *res, |
|---|
| 334 | struct printf_spec spec) |
|---|
| 335 | { |
|---|
| 336 | #ifndef IO_RSRC_PRINTK_SIZE |
|---|
| 337 | #define IO_RSRC_PRINTK_SIZE 4 |
|---|
| 338 | #endif |
|---|
| 339 | |
|---|
| 340 | #ifndef MEM_RSRC_PRINTK_SIZE |
|---|
| 341 | #define MEM_RSRC_PRINTK_SIZE 8 |
|---|
| 342 | #endif |
|---|
| 343 | struct printf_spec num_spec = { |
|---|
| 344 | .base = 16, |
|---|
| 345 | .precision = -1, |
|---|
| 346 | .flags = SPECIAL | SMALL | ZEROPAD, |
|---|
| 347 | }; |
|---|
| 348 | /* room for the actual numbers, the two "0x", -, [, ] and the final |
|---|
| 349 | zero */ |
|---|
| 350 | char sym[4*sizeof(resource_size_t) + 8]; |
|---|
| 351 | char *p = sym, *pend = sym + sizeof(sym); |
|---|
| 352 | int size = -1; |
|---|
| 353 | |
|---|
| 354 | if (res->flags & IORESOURCE_IO) |
|---|
| 355 | size = IO_RSRC_PRINTK_SIZE; |
|---|
| 356 | else if (res->flags & IORESOURCE_MEM) |
|---|
| 357 | size = MEM_RSRC_PRINTK_SIZE; |
|---|
| 358 | |
|---|
| 359 | *p++ = '['; |
|---|
| 360 | num_spec.field_width = size; |
|---|
| 361 | p = number(p, pend, res->start, num_spec); |
|---|
| 362 | *p++ = '-'; |
|---|
| 363 | p = number(p, pend, res->end, num_spec); |
|---|
| 364 | *p++ = ']'; |
|---|
| 365 | *p = 0; |
|---|
| 366 | |
|---|
| 367 | return string(buf, end, sym, spec); |
|---|
| 368 | } |
|---|
| 369 | |
|---|
| 370 | static char *mac_address_string(char *buf, char *end, u8 *addr, |
|---|
| 371 | struct printf_spec spec, const char *fmt) |
|---|
| 372 | { |
|---|
| 373 | char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; |
|---|
| 374 | char *p = mac_addr; |
|---|
| 375 | int i; |
|---|
| 376 | |
|---|
| 377 | for (i = 0; i < 6; i++) { |
|---|
| 378 | p = pack_hex_byte(p, addr[i]); |
|---|
| 379 | if (fmt[0] == 'M' && i != 5) |
|---|
| 380 | *p++ = ':'; |
|---|
| 381 | } |
|---|
| 382 | *p = '\0'; |
|---|
| 383 | |
|---|
| 384 | return string(buf, end, mac_addr, spec); |
|---|
| 385 | } |
|---|
| 386 | |
|---|
| 387 | /* |
|---|
| 388 | * Show a '%p' thing. A kernel extension is that the '%p' is followed |
|---|
| 389 | * by an extra set of alphanumeric characters that are extended format |
|---|
| 390 | * specifiers. |
|---|
| 391 | * |
|---|
| 392 | * Right now we handle: |
|---|
| 393 | * |
|---|
| 394 | * - 'F' For symbolic function descriptor pointers with offset |
|---|
| 395 | * - 'f' For simple symbolic function names without offset |
|---|
| 396 | * - 'R' For a struct resource pointer, it prints the range of |
|---|
| 397 | * addresses (not the name nor the flags) |
|---|
| 398 | * - 'M' For a 6-byte MAC address, it prints the address in the |
|---|
| 399 | * usual colon-separated hex notation |
|---|
| 400 | * - 'm' For a 6-byte MAC address, it prints the hex address without colons |
|---|
| 401 | * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way |
|---|
| 402 | * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) |
|---|
| 403 | * IPv6 uses colon separated network-order 16 bit hex with leading 0's |
|---|
| 404 | * - 'i' [46] for 'raw' IPv4/IPv6 addresses |
|---|
| 405 | * IPv6 omits the colons (01020304...0f) |
|---|
| 406 | * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) |
|---|
| 407 | * - 'I6c' for IPv6 addresses printed as specified by |
|---|
| 408 | * http://www.ietf.org/id/draft-kawamura-ipv6-text-representation-03.txt |
|---|
| 409 | * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 |
|---|
| 410 | * function pointers are really function descriptors, which contain a |
|---|
| 411 | * pointer to the real address. |
|---|
| 412 | */ |
|---|
| 413 | static char *pointer(const char *fmt, char *buf, char *end, void *ptr, |
|---|
| 414 | struct printf_spec spec) |
|---|
| 415 | { |
|---|
| 416 | if (!ptr) |
|---|
| 417 | return string(buf, end, "(null)", spec); |
|---|
| 418 | |
|---|
| 419 | switch (*fmt) { |
|---|
| 420 | case 'F': |
|---|
| 421 | case 'f': |
|---|
| 422 | ptr = dereference_function_descriptor(ptr); |
|---|
| 423 | case 'R': |
|---|
| 424 | return resource_string(buf, end, ptr, spec); |
|---|
| 425 | case 'M': /* Colon separated: 00:01:02:03:04:05 */ |
|---|
| 426 | case 'm': /* Contiguous: 000102030405 */ |
|---|
| 427 | return mac_address_string(buf, end, ptr, spec, fmt); |
|---|
| 428 | } |
|---|
| 429 | spec.flags |= SMALL; |
|---|
| 430 | if (spec.field_width == -1) { |
|---|
| 431 | spec.field_width = 2*sizeof(void *); |
|---|
| 432 | spec.flags |= ZEROPAD; |
|---|
| 433 | } |
|---|
| 434 | spec.base = 16; |
|---|
| 435 | |
|---|
| 436 | return number(buf, end, (unsigned long) ptr, spec); |
|---|
| 437 | } |
|---|
| 438 | |
|---|
| 439 | /* |
|---|
| 440 | * Helper function to decode printf style format. |
|---|
| 441 | * Each call decode a token from the format and return the |
|---|
| 442 | * number of characters read (or likely the delta where it wants |
|---|
| 443 | * to go on the next call). |
|---|
| 444 | * The decoded token is returned through the parameters |
|---|
| 445 | * |
|---|
| 446 | * 'h', 'l', or 'L' for integer fields |
|---|
| 447 | * 'z' support added 23/7/1999 S.H. |
|---|
| 448 | * 'z' changed to 'Z' --davidm 1/25/99 |
|---|
| 449 | * 't' added for ptrdiff_t |
|---|
| 450 | * |
|---|
| 451 | * @fmt: the format string |
|---|
| 452 | * @type of the token returned |
|---|
| 453 | * @flags: various flags such as +, -, # tokens.. |
|---|
| 454 | * @field_width: overwritten width |
|---|
| 455 | * @base: base of the number (octal, hex, ...) |
|---|
| 456 | * @precision: precision of a number |
|---|
| 457 | * @qualifier: qualifier of a number (long, size_t, ...) |
|---|
| 458 | */ |
|---|
| 459 | static int format_decode(const char *fmt, struct printf_spec *spec) |
|---|
| 460 | { |
|---|
| 461 | const char *start = fmt; |
|---|
| 462 | |
|---|
| 463 | /* we finished early by reading the field width */ |
|---|
| 464 | if (spec->type == FORMAT_TYPE_WIDTH) { |
|---|
| 465 | if (spec->field_width < 0) { |
|---|
| 466 | spec->field_width = -spec->field_width; |
|---|
| 467 | spec->flags |= LEFT; |
|---|
| 468 | } |
|---|
| 469 | spec->type = FORMAT_TYPE_NONE; |
|---|
| 470 | goto precision; |
|---|
| 471 | } |
|---|
| 472 | |
|---|
| 473 | /* we finished early by reading the precision */ |
|---|
| 474 | if (spec->type == FORMAT_TYPE_PRECISION) { |
|---|
| 475 | if (spec->precision < 0) |
|---|
| 476 | spec->precision = 0; |
|---|
| 477 | |
|---|
| 478 | spec->type = FORMAT_TYPE_NONE; |
|---|
| 479 | goto qualifier; |
|---|
| 480 | } |
|---|
| 481 | |
|---|
| 482 | /* By default */ |
|---|
| 483 | spec->type = FORMAT_TYPE_NONE; |
|---|
| 484 | |
|---|
| 485 | for (; *fmt ; ++fmt) { |
|---|
| 486 | if (*fmt == '%') |
|---|
| 487 | break; |
|---|
| 488 | } |
|---|
| 489 | |
|---|
| 490 | /* Return the current non-format string */ |
|---|
| 491 | if (fmt != start || !*fmt) |
|---|
| 492 | return fmt - start; |
|---|
| 493 | |
|---|
| 494 | /* Process flags */ |
|---|
| 495 | spec->flags = 0; |
|---|
| 496 | |
|---|
| 497 | while (1) { /* this also skips first '%' */ |
|---|
| 498 | bool found = true; |
|---|
| 499 | |
|---|
| 500 | ++fmt; |
|---|
| 501 | |
|---|
| 502 | switch (*fmt) { |
|---|
| 503 | case '-': |
|---|
| 504 | spec->flags |= LEFT; |
|---|
| 505 | break; |
|---|
| 506 | case '+': |
|---|
| 507 | spec->flags |= PLUS; |
|---|
| 508 | break; |
|---|
| 509 | case ' ': |
|---|
| 510 | spec->flags |= SPACE; |
|---|
| 511 | break; |
|---|
| 512 | case '#': |
|---|
| 513 | spec->flags |= SPECIAL; |
|---|
| 514 | break; |
|---|
| 515 | case '0': |
|---|
| 516 | spec->flags |= ZEROPAD; |
|---|
| 517 | break; |
|---|
| 518 | default: |
|---|
| 519 | found = false; |
|---|
| 520 | } |
|---|
| 521 | |
|---|
| 522 | if (!found) |
|---|
| 523 | break; |
|---|
| 524 | } |
|---|
| 525 | |
|---|
| 526 | /* get field width */ |
|---|
| 527 | spec->field_width = -1; |
|---|
| 528 | |
|---|
| 529 | if (isdigit(*fmt)) |
|---|
| 530 | spec->field_width = skip_atoi(&fmt); |
|---|
| 531 | else if (*fmt == '*') { |
|---|
| 532 | /* it's the next argument */ |
|---|
| 533 | spec->type = FORMAT_TYPE_WIDTH; |
|---|
| 534 | return ++fmt - start; |
|---|
| 535 | } |
|---|
| 536 | |
|---|
| 537 | precision: |
|---|
| 538 | /* get the precision */ |
|---|
| 539 | spec->precision = -1; |
|---|
| 540 | if (*fmt == '.') { |
|---|
| 541 | ++fmt; |
|---|
| 542 | if (isdigit(*fmt)) { |
|---|
| 543 | spec->precision = skip_atoi(&fmt); |
|---|
| 544 | if (spec->precision < 0) |
|---|
| 545 | spec->precision = 0; |
|---|
| 546 | } else if (*fmt == '*') { |
|---|
| 547 | /* it's the next argument */ |
|---|
| 548 | spec->type = FORMAT_TYPE_PRECISION; |
|---|
| 549 | return ++fmt - start; |
|---|
| 550 | } |
|---|
| 551 | } |
|---|
| 552 | |
|---|
| 553 | qualifier: |
|---|
| 554 | /* get the conversion qualifier */ |
|---|
| 555 | spec->qualifier = -1; |
|---|
| 556 | if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || |
|---|
| 557 | *fmt == 'Z' || *fmt == 'z' || *fmt == 't') { |
|---|
| 558 | spec->qualifier = *fmt++; |
|---|
| 559 | if (unlikely(spec->qualifier == *fmt)) { |
|---|
| 560 | if (spec->qualifier == 'l') { |
|---|
| 561 | spec->qualifier = 'L'; |
|---|
| 562 | ++fmt; |
|---|
| 563 | } else if (spec->qualifier == 'h') { |
|---|
| 564 | spec->qualifier = 'H'; |
|---|
| 565 | ++fmt; |
|---|
| 566 | } |
|---|
| 567 | } |
|---|
| 568 | } |
|---|
| 569 | |
|---|
| 570 | /* default base */ |
|---|
| 571 | spec->base = 10; |
|---|
| 572 | switch (*fmt) { |
|---|
| 573 | case 'c': |
|---|
| 574 | spec->type = FORMAT_TYPE_CHAR; |
|---|
| 575 | return ++fmt - start; |
|---|
| 576 | |
|---|
| 577 | case 's': |
|---|
| 578 | spec->type = FORMAT_TYPE_STR; |
|---|
| 579 | return ++fmt - start; |
|---|
| 580 | |
|---|
| 581 | case 'p': |
|---|
| 582 | spec->type = FORMAT_TYPE_PTR; |
|---|
| 583 | return fmt - start; |
|---|
| 584 | /* skip alnum */ |
|---|
| 585 | |
|---|
| 586 | case 'n': |
|---|
| 587 | spec->type = FORMAT_TYPE_NRCHARS; |
|---|
| 588 | return ++fmt - start; |
|---|
| 589 | |
|---|
| 590 | case '%': |
|---|
| 591 | spec->type = FORMAT_TYPE_PERCENT_CHAR; |
|---|
| 592 | return ++fmt - start; |
|---|
| 593 | |
|---|
| 594 | /* integer number formats - set up the flags and "break" */ |
|---|
| 595 | case 'o': |
|---|
| 596 | spec->base = 8; |
|---|
| 597 | break; |
|---|
| 598 | |
|---|
| 599 | case 'x': |
|---|
| 600 | spec->flags |= SMALL; |
|---|
| 601 | |
|---|
| 602 | case 'X': |
|---|
| 603 | spec->base = 16; |
|---|
| 604 | break; |
|---|
| 605 | |
|---|
| 606 | case 'd': |
|---|
| 607 | case 'i': |
|---|
| 608 | spec->flags |= SIGN; |
|---|
| 609 | case 'u': |
|---|
| 610 | break; |
|---|
| 611 | |
|---|
| 612 | default: |
|---|
| 613 | spec->type = FORMAT_TYPE_INVALID; |
|---|
| 614 | return fmt - start; |
|---|
| 615 | } |
|---|
| 616 | |
|---|
| 617 | if (spec->qualifier == 'L') |
|---|
| 618 | spec->type = FORMAT_TYPE_LONG_LONG; |
|---|
| 619 | else if (spec->qualifier == 'l') { |
|---|
| 620 | if (spec->flags & SIGN) |
|---|
| 621 | spec->type = FORMAT_TYPE_LONG; |
|---|
| 622 | else |
|---|
| 623 | spec->type = FORMAT_TYPE_ULONG; |
|---|
| 624 | } else if (spec->qualifier == 'Z' || spec->qualifier == 'z') { |
|---|
| 625 | spec->type = FORMAT_TYPE_SIZE_T; |
|---|
| 626 | } else if (spec->qualifier == 't') { |
|---|
| 627 | spec->type = FORMAT_TYPE_PTRDIFF; |
|---|
| 628 | } else if (spec->qualifier == 'H') { |
|---|
| 629 | if (spec->flags & SIGN) |
|---|
| 630 | spec->type = FORMAT_TYPE_BYTE; |
|---|
| 631 | else |
|---|
| 632 | spec->type = FORMAT_TYPE_UBYTE; |
|---|
| 633 | } else if (spec->qualifier == 'h') { |
|---|
| 634 | if (spec->flags & SIGN) |
|---|
| 635 | spec->type = FORMAT_TYPE_SHORT; |
|---|
| 636 | else |
|---|
| 637 | spec->type = FORMAT_TYPE_USHORT; |
|---|
| 638 | } else { |
|---|
| 639 | if (spec->flags & SIGN) |
|---|
| 640 | spec->type = FORMAT_TYPE_INT; |
|---|
| 641 | else |
|---|
| 642 | spec->type = FORMAT_TYPE_UINT; |
|---|
| 643 | } |
|---|
| 644 | |
|---|
| 645 | return ++fmt - start; |
|---|
| 646 | } |
|---|
| 647 | |
|---|
| 648 | /** |
|---|
| 649 | * bat_vsnprintf - Format a string and place it in a buffer |
|---|
| 650 | * @buf: The buffer to place the result into |
|---|
| 651 | * @size: The size of the buffer, including the trailing null space |
|---|
| 652 | * @fmt: The format string to use |
|---|
| 653 | * @args: Arguments for the format string |
|---|
| 654 | * |
|---|
| 655 | * This function follows C99 bat_vsnprintf, but has some extensions: |
|---|
| 656 | * %pS output the name of a text symbol with offset |
|---|
| 657 | * %ps output the name of a text symbol without offset |
|---|
| 658 | * %pF output the name of a function pointer with its offset |
|---|
| 659 | * %pf output the name of a function pointer without its offset |
|---|
| 660 | * %pR output the address range in a struct resource |
|---|
| 661 | * %n is ignored |
|---|
| 662 | * |
|---|
| 663 | * The return value is the number of characters which would |
|---|
| 664 | * be generated for the given input, excluding the trailing |
|---|
| 665 | * '\0', as per ISO C99. If you want to have the exact |
|---|
| 666 | * number of characters written into @buf as return value |
|---|
| 667 | * (not including the trailing '\0'), use vscnprintf(). If the |
|---|
| 668 | * return is greater than or equal to @size, the resulting |
|---|
| 669 | * string is truncated. |
|---|
| 670 | * |
|---|
| 671 | * Call this function if you are already dealing with a va_list. |
|---|
| 672 | * You probably want snprintf() instead. |
|---|
| 673 | */ |
|---|
| 674 | static int bat_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) |
|---|
| 675 | { |
|---|
| 676 | unsigned long long num; |
|---|
| 677 | char *str, *end, c; |
|---|
| 678 | int read; |
|---|
| 679 | struct printf_spec spec = {0}; |
|---|
| 680 | |
|---|
| 681 | /* Reject out-of-range values early. Large positive sizes are |
|---|
| 682 | used for unknown buffer sizes. */ |
|---|
| 683 | if (WARN_ON_ONCE((int) size < 0)) |
|---|
| 684 | return 0; |
|---|
| 685 | |
|---|
| 686 | str = buf; |
|---|
| 687 | end = buf + size; |
|---|
| 688 | |
|---|
| 689 | /* Make sure end is always >= buf */ |
|---|
| 690 | if (end < buf) { |
|---|
| 691 | end = ((void *)-1); |
|---|
| 692 | size = end - buf; |
|---|
| 693 | } |
|---|
| 694 | |
|---|
| 695 | while (*fmt) { |
|---|
| 696 | const char *old_fmt = fmt; |
|---|
| 697 | |
|---|
| 698 | read = format_decode(fmt, &spec); |
|---|
| 699 | |
|---|
| 700 | fmt += read; |
|---|
| 701 | |
|---|
| 702 | switch (spec.type) { |
|---|
| 703 | case FORMAT_TYPE_NONE: { |
|---|
| 704 | int copy = read; |
|---|
| 705 | if (str < end) { |
|---|
| 706 | if (copy > end - str) |
|---|
| 707 | copy = end - str; |
|---|
| 708 | memcpy(str, old_fmt, copy); |
|---|
| 709 | } |
|---|
| 710 | str += read; |
|---|
| 711 | break; |
|---|
| 712 | } |
|---|
| 713 | |
|---|
| 714 | case FORMAT_TYPE_WIDTH: |
|---|
| 715 | spec.field_width = va_arg(args, int); |
|---|
| 716 | break; |
|---|
| 717 | |
|---|
| 718 | case FORMAT_TYPE_PRECISION: |
|---|
| 719 | spec.precision = va_arg(args, int); |
|---|
| 720 | break; |
|---|
| 721 | |
|---|
| 722 | case FORMAT_TYPE_CHAR: |
|---|
| 723 | if (!(spec.flags & LEFT)) { |
|---|
| 724 | while (--spec.field_width > 0) { |
|---|
| 725 | if (str < end) |
|---|
| 726 | *str = ' '; |
|---|
| 727 | ++str; |
|---|
| 728 | |
|---|
| 729 | } |
|---|
| 730 | } |
|---|
| 731 | c = (unsigned char) va_arg(args, int); |
|---|
| 732 | if (str < end) |
|---|
| 733 | *str = c; |
|---|
| 734 | ++str; |
|---|
| 735 | while (--spec.field_width > 0) { |
|---|
| 736 | if (str < end) |
|---|
| 737 | *str = ' '; |
|---|
| 738 | ++str; |
|---|
| 739 | } |
|---|
| 740 | break; |
|---|
| 741 | |
|---|
| 742 | case FORMAT_TYPE_STR: |
|---|
| 743 | str = string(str, end, va_arg(args, char *), spec); |
|---|
| 744 | break; |
|---|
| 745 | |
|---|
| 746 | case FORMAT_TYPE_PTR: |
|---|
| 747 | str = pointer(fmt+1, str, end, va_arg(args, void *), |
|---|
| 748 | spec); |
|---|
| 749 | while (isalnum(*fmt)) |
|---|
| 750 | fmt++; |
|---|
| 751 | break; |
|---|
| 752 | |
|---|
| 753 | case FORMAT_TYPE_PERCENT_CHAR: |
|---|
| 754 | if (str < end) |
|---|
| 755 | *str = '%'; |
|---|
| 756 | ++str; |
|---|
| 757 | break; |
|---|
| 758 | |
|---|
| 759 | case FORMAT_TYPE_INVALID: |
|---|
| 760 | if (str < end) |
|---|
| 761 | *str = '%'; |
|---|
| 762 | ++str; |
|---|
| 763 | break; |
|---|
| 764 | |
|---|
| 765 | case FORMAT_TYPE_NRCHARS: { |
|---|
| 766 | int qualifier = spec.qualifier; |
|---|
| 767 | |
|---|
| 768 | if (qualifier == 'l') { |
|---|
| 769 | long *ip = va_arg(args, long *); |
|---|
| 770 | *ip = (str - buf); |
|---|
| 771 | } else if (qualifier == 'Z' || |
|---|
| 772 | qualifier == 'z') { |
|---|
| 773 | size_t *ip = va_arg(args, size_t *); |
|---|
| 774 | *ip = (str - buf); |
|---|
| 775 | } else { |
|---|
| 776 | int *ip = va_arg(args, int *); |
|---|
| 777 | *ip = (str - buf); |
|---|
| 778 | } |
|---|
| 779 | break; |
|---|
| 780 | } |
|---|
| 781 | |
|---|
| 782 | default: |
|---|
| 783 | switch (spec.type) { |
|---|
| 784 | case FORMAT_TYPE_LONG_LONG: |
|---|
| 785 | num = va_arg(args, long long); |
|---|
| 786 | break; |
|---|
| 787 | case FORMAT_TYPE_ULONG: |
|---|
| 788 | num = va_arg(args, unsigned long); |
|---|
| 789 | break; |
|---|
| 790 | case FORMAT_TYPE_LONG: |
|---|
| 791 | num = va_arg(args, long); |
|---|
| 792 | break; |
|---|
| 793 | case FORMAT_TYPE_SIZE_T: |
|---|
| 794 | num = va_arg(args, size_t); |
|---|
| 795 | break; |
|---|
| 796 | case FORMAT_TYPE_PTRDIFF: |
|---|
| 797 | num = va_arg(args, ptrdiff_t); |
|---|
| 798 | break; |
|---|
| 799 | case FORMAT_TYPE_UBYTE: |
|---|
| 800 | num = (unsigned char) va_arg(args, int); |
|---|
| 801 | break; |
|---|
| 802 | case FORMAT_TYPE_BYTE: |
|---|
| 803 | num = (signed char) va_arg(args, int); |
|---|
| 804 | break; |
|---|
| 805 | case FORMAT_TYPE_USHORT: |
|---|
| 806 | num = (unsigned short) va_arg(args, int); |
|---|
| 807 | break; |
|---|
| 808 | case FORMAT_TYPE_SHORT: |
|---|
| 809 | num = (short) va_arg(args, int); |
|---|
| 810 | break; |
|---|
| 811 | case FORMAT_TYPE_INT: |
|---|
| 812 | num = (int) va_arg(args, int); |
|---|
| 813 | break; |
|---|
| 814 | default: |
|---|
| 815 | num = va_arg(args, unsigned int); |
|---|
| 816 | } |
|---|
| 817 | |
|---|
| 818 | str = number(str, end, num, spec); |
|---|
| 819 | } |
|---|
| 820 | } |
|---|
| 821 | |
|---|
| 822 | if (size > 0) { |
|---|
| 823 | if (str < end) |
|---|
| 824 | *str = '\0'; |
|---|
| 825 | else |
|---|
| 826 | end[-1] = '\0'; |
|---|
| 827 | } |
|---|
| 828 | |
|---|
| 829 | /* the trailing null byte doesn't count towards the total */ |
|---|
| 830 | return str-buf; |
|---|
| 831 | |
|---|
| 832 | } |
|---|
| 833 | |
|---|
| 834 | /** |
|---|
| 835 | * bat_printk - print a kernel message using extra %p formatting |
|---|
| 836 | * strings, forward compatible with kernel version 2.6.31 printk, minus |
|---|
| 837 | * *p[sS]. |
|---|
| 838 | * |
|---|
| 839 | * @fmt: format string |
|---|
| 840 | * |
|---|
| 841 | * This is printk(). It can be called from any context. We want it to work. |
|---|
| 842 | * |
|---|
| 843 | */ |
|---|
| 844 | asmlinkage int bat_printk(const char *fmt, ...) |
|---|
| 845 | { |
|---|
| 846 | va_list args; |
|---|
| 847 | int r; |
|---|
| 848 | char buf[256]; |
|---|
| 849 | |
|---|
| 850 | va_start(args, fmt); |
|---|
| 851 | r = bat_vsnprintf(buf, sizeof(buf), fmt, args); |
|---|
| 852 | va_end(args); |
|---|
| 853 | |
|---|
| 854 | return printk("foo:%s", buf); |
|---|
| 855 | } |
|---|