lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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> /* for KSYM_SYMBOL_LEN */ |
| 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 <linux/cred.h> |
| 29 | #include <net/addrconf.h> |
| 30 | |
| 31 | #include <asm/page.h> /* for PAGE_SIZE */ |
| 32 | #include <asm/div64.h> |
| 33 | #include <asm/sections.h> /* for dereference_function_descriptor() */ |
| 34 | |
| 35 | #include "kstrtox.h" |
| 36 | |
| 37 | /** |
| 38 | * simple_strtoull - convert a string to an unsigned long long |
| 39 | * @cp: The start of the string |
| 40 | * @endp: A pointer to the end of the parsed string will be placed here |
| 41 | * @base: The number base to use |
| 42 | */ |
| 43 | unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base) |
| 44 | { |
| 45 | unsigned long long result; |
| 46 | unsigned int rv; |
| 47 | |
| 48 | cp = _parse_integer_fixup_radix(cp, &base); |
| 49 | rv = _parse_integer(cp, base, &result); |
| 50 | /* FIXME */ |
| 51 | cp += (rv & ~KSTRTOX_OVERFLOW); |
| 52 | |
| 53 | if (endp) |
| 54 | *endp = (char *)cp; |
| 55 | |
| 56 | return result; |
| 57 | } |
| 58 | EXPORT_SYMBOL(simple_strtoull); |
| 59 | |
| 60 | /** |
| 61 | * simple_strtoul - convert a string to an unsigned long |
| 62 | * @cp: The start of the string |
| 63 | * @endp: A pointer to the end of the parsed string will be placed here |
| 64 | * @base: The number base to use |
| 65 | */ |
| 66 | unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base) |
| 67 | { |
| 68 | return simple_strtoull(cp, endp, base); |
| 69 | } |
| 70 | EXPORT_SYMBOL(simple_strtoul); |
| 71 | |
| 72 | /** |
| 73 | * simple_strtol - convert a string to a signed long |
| 74 | * @cp: The start of the string |
| 75 | * @endp: A pointer to the end of the parsed string will be placed here |
| 76 | * @base: The number base to use |
| 77 | */ |
| 78 | long simple_strtol(const char *cp, char **endp, unsigned int base) |
| 79 | { |
| 80 | if (*cp == '-') |
| 81 | return -simple_strtoul(cp + 1, endp, base); |
| 82 | |
| 83 | return simple_strtoul(cp, endp, base); |
| 84 | } |
| 85 | EXPORT_SYMBOL(simple_strtol); |
| 86 | |
| 87 | /** |
| 88 | * simple_strtoll - convert a string to a signed long long |
| 89 | * @cp: The start of the string |
| 90 | * @endp: A pointer to the end of the parsed string will be placed here |
| 91 | * @base: The number base to use |
| 92 | */ |
| 93 | long long simple_strtoll(const char *cp, char **endp, unsigned int base) |
| 94 | { |
| 95 | if (*cp == '-') |
| 96 | return -simple_strtoull(cp + 1, endp, base); |
| 97 | |
| 98 | return simple_strtoull(cp, endp, base); |
| 99 | } |
| 100 | EXPORT_SYMBOL(simple_strtoll); |
| 101 | |
| 102 | static noinline_for_stack |
| 103 | int skip_atoi(const char **s) |
| 104 | { |
| 105 | int i = 0; |
| 106 | |
| 107 | while (isdigit(**s)) |
| 108 | i = i*10 + *((*s)++) - '0'; |
| 109 | |
| 110 | return i; |
| 111 | } |
| 112 | |
| 113 | /* Decimal conversion is by far the most typical, and is used |
| 114 | * for /proc and /sys data. This directly impacts e.g. top performance |
| 115 | * with many processes running. We optimize it for speed |
| 116 | * using code from |
| 117 | * http://www.cs.uiowa.edu/~jones/bcd/decimal.html |
| 118 | * (with permission from the author, Douglas W. Jones). */ |
| 119 | |
| 120 | /* Formats correctly any integer in [0,99999]. |
| 121 | * Outputs from one to five digits depending on input. |
| 122 | * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */ |
| 123 | static noinline_for_stack |
| 124 | char *put_dec_trunc(char *buf, unsigned q) |
| 125 | { |
| 126 | unsigned d3, d2, d1, d0; |
| 127 | d1 = (q>>4) & 0xf; |
| 128 | d2 = (q>>8) & 0xf; |
| 129 | d3 = (q>>12); |
| 130 | |
| 131 | d0 = 6*(d3 + d2 + d1) + (q & 0xf); |
| 132 | q = (d0 * 0xcd) >> 11; |
| 133 | d0 = d0 - 10*q; |
| 134 | *buf++ = d0 + '0'; /* least significant digit */ |
| 135 | d1 = q + 9*d3 + 5*d2 + d1; |
| 136 | if (d1 != 0) { |
| 137 | q = (d1 * 0xcd) >> 11; |
| 138 | d1 = d1 - 10*q; |
| 139 | *buf++ = d1 + '0'; /* next digit */ |
| 140 | |
| 141 | d2 = q + 2*d2; |
| 142 | if ((d2 != 0) || (d3 != 0)) { |
| 143 | q = (d2 * 0xd) >> 7; |
| 144 | d2 = d2 - 10*q; |
| 145 | *buf++ = d2 + '0'; /* next digit */ |
| 146 | |
| 147 | d3 = q + 4*d3; |
| 148 | if (d3 != 0) { |
| 149 | q = (d3 * 0xcd) >> 11; |
| 150 | d3 = d3 - 10*q; |
| 151 | *buf++ = d3 + '0'; /* next digit */ |
| 152 | if (q != 0) |
| 153 | *buf++ = q + '0'; /* most sign. digit */ |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return buf; |
| 159 | } |
| 160 | /* Same with if's removed. Always emits five digits */ |
| 161 | static noinline_for_stack |
| 162 | char *put_dec_full(char *buf, unsigned q) |
| 163 | { |
| 164 | /* BTW, if q is in [0,9999], 8-bit ints will be enough, */ |
| 165 | /* but anyway, gcc produces better code with full-sized ints */ |
| 166 | unsigned d3, d2, d1, d0; |
| 167 | d1 = (q>>4) & 0xf; |
| 168 | d2 = (q>>8) & 0xf; |
| 169 | d3 = (q>>12); |
| 170 | |
| 171 | /* |
| 172 | * Possible ways to approx. divide by 10 |
| 173 | * gcc -O2 replaces multiply with shifts and adds |
| 174 | * (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386) |
| 175 | * (x * 0x67) >> 10: 1100111 |
| 176 | * (x * 0x34) >> 9: 110100 - same |
| 177 | * (x * 0x1a) >> 8: 11010 - same |
| 178 | * (x * 0x0d) >> 7: 1101 - same, shortest code (on i386) |
| 179 | */ |
| 180 | d0 = 6*(d3 + d2 + d1) + (q & 0xf); |
| 181 | q = (d0 * 0xcd) >> 11; |
| 182 | d0 = d0 - 10*q; |
| 183 | *buf++ = d0 + '0'; |
| 184 | d1 = q + 9*d3 + 5*d2 + d1; |
| 185 | q = (d1 * 0xcd) >> 11; |
| 186 | d1 = d1 - 10*q; |
| 187 | *buf++ = d1 + '0'; |
| 188 | |
| 189 | d2 = q + 2*d2; |
| 190 | q = (d2 * 0xd) >> 7; |
| 191 | d2 = d2 - 10*q; |
| 192 | *buf++ = d2 + '0'; |
| 193 | |
| 194 | d3 = q + 4*d3; |
| 195 | q = (d3 * 0xcd) >> 11; /* - shorter code */ |
| 196 | /* q = (d3 * 0x67) >> 10; - would also work */ |
| 197 | d3 = d3 - 10*q; |
| 198 | *buf++ = d3 + '0'; |
| 199 | *buf++ = q + '0'; |
| 200 | |
| 201 | return buf; |
| 202 | } |
| 203 | /* No inlining helps gcc to use registers better */ |
| 204 | static noinline_for_stack |
| 205 | char *put_dec(char *buf, unsigned long long num) |
| 206 | { |
| 207 | while (1) { |
| 208 | unsigned rem; |
| 209 | if (num < 100000) |
| 210 | return put_dec_trunc(buf, num); |
| 211 | rem = do_div(num, 100000); |
| 212 | buf = put_dec_full(buf, rem); |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Convert passed number to decimal string. |
| 218 | * Returns the length of string. On buffer overflow, returns 0. |
| 219 | * |
| 220 | * If speed is not important, use snprintf(). It's easy to read the code. |
| 221 | */ |
| 222 | int num_to_str(char *buf, int size, unsigned long long num) |
| 223 | { |
| 224 | char tmp[21]; /* Enough for 2^64 in decimal */ |
| 225 | int idx, len; |
| 226 | |
| 227 | len = put_dec(tmp, num) - tmp; |
| 228 | |
| 229 | if (len > size) |
| 230 | return 0; |
| 231 | for (idx = 0; idx < len; ++idx) |
| 232 | buf[idx] = tmp[len - idx - 1]; |
| 233 | return len; |
| 234 | } |
| 235 | |
| 236 | #define ZEROPAD 1 /* pad with zero */ |
| 237 | #define SIGN 2 /* unsigned/signed long */ |
| 238 | #define PLUS 4 /* show plus */ |
| 239 | #define SPACE 8 /* space if plus */ |
| 240 | #define LEFT 16 /* left justified */ |
| 241 | #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */ |
| 242 | #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */ |
| 243 | |
| 244 | enum format_type { |
| 245 | FORMAT_TYPE_NONE, /* Just a string part */ |
| 246 | FORMAT_TYPE_WIDTH, |
| 247 | FORMAT_TYPE_PRECISION, |
| 248 | FORMAT_TYPE_CHAR, |
| 249 | FORMAT_TYPE_STR, |
| 250 | FORMAT_TYPE_PTR, |
| 251 | FORMAT_TYPE_PERCENT_CHAR, |
| 252 | FORMAT_TYPE_INVALID, |
| 253 | FORMAT_TYPE_LONG_LONG, |
| 254 | FORMAT_TYPE_ULONG, |
| 255 | FORMAT_TYPE_LONG, |
| 256 | FORMAT_TYPE_UBYTE, |
| 257 | FORMAT_TYPE_BYTE, |
| 258 | FORMAT_TYPE_USHORT, |
| 259 | FORMAT_TYPE_SHORT, |
| 260 | FORMAT_TYPE_UINT, |
| 261 | FORMAT_TYPE_INT, |
| 262 | FORMAT_TYPE_NRCHARS, |
| 263 | FORMAT_TYPE_SIZE_T, |
| 264 | FORMAT_TYPE_PTRDIFF |
| 265 | }; |
| 266 | |
| 267 | struct printf_spec { |
| 268 | u8 type; /* format_type enum */ |
| 269 | u8 flags; /* flags to number() */ |
| 270 | u8 base; /* number base, 8, 10 or 16 only */ |
| 271 | u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */ |
| 272 | s16 field_width; /* width of output field */ |
| 273 | s16 precision; /* # of digits/chars */ |
| 274 | }; |
| 275 | |
| 276 | static noinline_for_stack |
| 277 | char *number(char *buf, char *end, unsigned long long num, |
| 278 | struct printf_spec spec) |
| 279 | { |
| 280 | /* we are called with base 8, 10 or 16, only, thus don't need "G..." */ |
| 281 | static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */ |
| 282 | |
| 283 | char tmp[66]; |
| 284 | char sign; |
| 285 | char locase; |
| 286 | int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10); |
| 287 | int i; |
| 288 | |
| 289 | /* locase = 0 or 0x20. ORing digits or letters with 'locase' |
| 290 | * produces same digits or (maybe lowercased) letters */ |
| 291 | locase = (spec.flags & SMALL); |
| 292 | if (spec.flags & LEFT) |
| 293 | spec.flags &= ~ZEROPAD; |
| 294 | sign = 0; |
| 295 | if (spec.flags & SIGN) { |
| 296 | if ((signed long long)num < 0) { |
| 297 | sign = '-'; |
| 298 | num = -(signed long long)num; |
| 299 | spec.field_width--; |
| 300 | } else if (spec.flags & PLUS) { |
| 301 | sign = '+'; |
| 302 | spec.field_width--; |
| 303 | } else if (spec.flags & SPACE) { |
| 304 | sign = ' '; |
| 305 | spec.field_width--; |
| 306 | } |
| 307 | } |
| 308 | if (need_pfx) { |
| 309 | spec.field_width--; |
| 310 | if (spec.base == 16) |
| 311 | spec.field_width--; |
| 312 | } |
| 313 | |
| 314 | /* generate full string in tmp[], in reverse order */ |
| 315 | i = 0; |
| 316 | if (num == 0) |
| 317 | tmp[i++] = '0'; |
| 318 | /* Generic code, for any base: |
| 319 | else do { |
| 320 | tmp[i++] = (digits[do_div(num,base)] | locase); |
| 321 | } while (num != 0); |
| 322 | */ |
| 323 | else if (spec.base != 10) { /* 8 or 16 */ |
| 324 | int mask = spec.base - 1; |
| 325 | int shift = 3; |
| 326 | |
| 327 | if (spec.base == 16) |
| 328 | shift = 4; |
| 329 | do { |
| 330 | tmp[i++] = (digits[((unsigned char)num) & mask] | locase); |
| 331 | num >>= shift; |
| 332 | } while (num); |
| 333 | } else { /* base 10 */ |
| 334 | i = put_dec(tmp, num) - tmp; |
| 335 | } |
| 336 | |
| 337 | /* printing 100 using %2d gives "100", not "00" */ |
| 338 | if (i > spec.precision) |
| 339 | spec.precision = i; |
| 340 | /* leading space padding */ |
| 341 | spec.field_width -= spec.precision; |
| 342 | if (!(spec.flags & (ZEROPAD+LEFT))) { |
| 343 | while (--spec.field_width >= 0) { |
| 344 | if (buf < end) |
| 345 | *buf = ' '; |
| 346 | ++buf; |
| 347 | } |
| 348 | } |
| 349 | /* sign */ |
| 350 | if (sign) { |
| 351 | if (buf < end) |
| 352 | *buf = sign; |
| 353 | ++buf; |
| 354 | } |
| 355 | /* "0x" / "0" prefix */ |
| 356 | if (need_pfx) { |
| 357 | if (buf < end) |
| 358 | *buf = '0'; |
| 359 | ++buf; |
| 360 | if (spec.base == 16) { |
| 361 | if (buf < end) |
| 362 | *buf = ('X' | locase); |
| 363 | ++buf; |
| 364 | } |
| 365 | } |
| 366 | /* zero or space padding */ |
| 367 | if (!(spec.flags & LEFT)) { |
| 368 | char c = (spec.flags & ZEROPAD) ? '0' : ' '; |
| 369 | while (--spec.field_width >= 0) { |
| 370 | if (buf < end) |
| 371 | *buf = c; |
| 372 | ++buf; |
| 373 | } |
| 374 | } |
| 375 | /* hmm even more zero padding? */ |
| 376 | while (i <= --spec.precision) { |
| 377 | if (buf < end) |
| 378 | *buf = '0'; |
| 379 | ++buf; |
| 380 | } |
| 381 | /* actual digits of result */ |
| 382 | while (--i >= 0) { |
| 383 | if (buf < end) |
| 384 | *buf = tmp[i]; |
| 385 | ++buf; |
| 386 | } |
| 387 | /* trailing space padding */ |
| 388 | while (--spec.field_width >= 0) { |
| 389 | if (buf < end) |
| 390 | *buf = ' '; |
| 391 | ++buf; |
| 392 | } |
| 393 | |
| 394 | return buf; |
| 395 | } |
| 396 | |
| 397 | static noinline_for_stack |
| 398 | char *string(char *buf, char *end, const char *s, struct printf_spec spec) |
| 399 | { |
| 400 | int len, i; |
| 401 | |
| 402 | if ((unsigned long)s < PAGE_SIZE) |
| 403 | s = "(null)"; |
| 404 | |
| 405 | len = strnlen(s, spec.precision); |
| 406 | |
| 407 | if (!(spec.flags & LEFT)) { |
| 408 | while (len < spec.field_width--) { |
| 409 | if (buf < end) |
| 410 | *buf = ' '; |
| 411 | ++buf; |
| 412 | } |
| 413 | } |
| 414 | for (i = 0; i < len; ++i) { |
| 415 | if (buf < end) |
| 416 | *buf = *s; |
| 417 | ++buf; ++s; |
| 418 | } |
| 419 | while (len < spec.field_width--) { |
| 420 | if (buf < end) |
| 421 | *buf = ' '; |
| 422 | ++buf; |
| 423 | } |
| 424 | |
| 425 | return buf; |
| 426 | } |
| 427 | |
| 428 | static noinline_for_stack |
| 429 | char *symbol_string(char *buf, char *end, void *ptr, |
| 430 | struct printf_spec spec, char ext) |
| 431 | { |
| 432 | unsigned long value = (unsigned long) ptr; |
| 433 | #ifdef CONFIG_KALLSYMS |
| 434 | char sym[KSYM_SYMBOL_LEN]; |
| 435 | if (ext == 'B') |
| 436 | sprint_backtrace(sym, value); |
| 437 | else if (ext != 'f' && ext != 's') |
| 438 | sprint_symbol(sym, value); |
| 439 | else |
| 440 | kallsyms_lookup(value, NULL, NULL, NULL, sym); |
| 441 | |
| 442 | return string(buf, end, sym, spec); |
| 443 | #else |
| 444 | spec.field_width = 2 * sizeof(void *); |
| 445 | spec.flags |= SPECIAL | SMALL | ZEROPAD; |
| 446 | spec.base = 16; |
| 447 | |
| 448 | return number(buf, end, value, spec); |
| 449 | #endif |
| 450 | } |
| 451 | |
| 452 | static noinline_for_stack |
| 453 | char *resource_string(char *buf, char *end, struct resource *res, |
| 454 | struct printf_spec spec, const char *fmt) |
| 455 | { |
| 456 | #ifndef IO_RSRC_PRINTK_SIZE |
| 457 | #define IO_RSRC_PRINTK_SIZE 6 |
| 458 | #endif |
| 459 | |
| 460 | #ifndef MEM_RSRC_PRINTK_SIZE |
| 461 | #define MEM_RSRC_PRINTK_SIZE 10 |
| 462 | #endif |
| 463 | static const struct printf_spec io_spec = { |
| 464 | .base = 16, |
| 465 | .field_width = IO_RSRC_PRINTK_SIZE, |
| 466 | .precision = -1, |
| 467 | .flags = SPECIAL | SMALL | ZEROPAD, |
| 468 | }; |
| 469 | static const struct printf_spec mem_spec = { |
| 470 | .base = 16, |
| 471 | .field_width = MEM_RSRC_PRINTK_SIZE, |
| 472 | .precision = -1, |
| 473 | .flags = SPECIAL | SMALL | ZEROPAD, |
| 474 | }; |
| 475 | static const struct printf_spec bus_spec = { |
| 476 | .base = 16, |
| 477 | .field_width = 2, |
| 478 | .precision = -1, |
| 479 | .flags = SMALL | ZEROPAD, |
| 480 | }; |
| 481 | static const struct printf_spec dec_spec = { |
| 482 | .base = 10, |
| 483 | .precision = -1, |
| 484 | .flags = 0, |
| 485 | }; |
| 486 | static const struct printf_spec str_spec = { |
| 487 | .field_width = -1, |
| 488 | .precision = 10, |
| 489 | .flags = LEFT, |
| 490 | }; |
| 491 | static const struct printf_spec flag_spec = { |
| 492 | .base = 16, |
| 493 | .precision = -1, |
| 494 | .flags = SPECIAL | SMALL, |
| 495 | }; |
| 496 | |
| 497 | /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8) |
| 498 | * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */ |
| 499 | #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4) |
| 500 | #define FLAG_BUF_SIZE (2 * sizeof(res->flags)) |
| 501 | #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]") |
| 502 | #define RAW_BUF_SIZE sizeof("[mem - flags 0x]") |
| 503 | char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE, |
| 504 | 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)]; |
| 505 | |
| 506 | char *p = sym, *pend = sym + sizeof(sym); |
| 507 | int decode = (fmt[0] == 'R') ? 1 : 0; |
| 508 | const struct printf_spec *specp; |
| 509 | |
| 510 | *p++ = '['; |
| 511 | if (res->flags & IORESOURCE_IO) { |
| 512 | p = string(p, pend, "io ", str_spec); |
| 513 | specp = &io_spec; |
| 514 | } else if (res->flags & IORESOURCE_MEM) { |
| 515 | p = string(p, pend, "mem ", str_spec); |
| 516 | specp = &mem_spec; |
| 517 | } else if (res->flags & IORESOURCE_IRQ) { |
| 518 | p = string(p, pend, "irq ", str_spec); |
| 519 | specp = &dec_spec; |
| 520 | } else if (res->flags & IORESOURCE_DMA) { |
| 521 | p = string(p, pend, "dma ", str_spec); |
| 522 | specp = &dec_spec; |
| 523 | } else if (res->flags & IORESOURCE_BUS) { |
| 524 | p = string(p, pend, "bus ", str_spec); |
| 525 | specp = &bus_spec; |
| 526 | } else { |
| 527 | p = string(p, pend, "??? ", str_spec); |
| 528 | specp = &mem_spec; |
| 529 | decode = 0; |
| 530 | } |
| 531 | p = number(p, pend, res->start, *specp); |
| 532 | if (res->start != res->end) { |
| 533 | *p++ = '-'; |
| 534 | p = number(p, pend, res->end, *specp); |
| 535 | } |
| 536 | if (decode) { |
| 537 | if (res->flags & IORESOURCE_MEM_64) |
| 538 | p = string(p, pend, " 64bit", str_spec); |
| 539 | if (res->flags & IORESOURCE_PREFETCH) |
| 540 | p = string(p, pend, " pref", str_spec); |
| 541 | if (res->flags & IORESOURCE_WINDOW) |
| 542 | p = string(p, pend, " window", str_spec); |
| 543 | if (res->flags & IORESOURCE_DISABLED) |
| 544 | p = string(p, pend, " disabled", str_spec); |
| 545 | } else { |
| 546 | p = string(p, pend, " flags ", str_spec); |
| 547 | p = number(p, pend, res->flags, flag_spec); |
| 548 | } |
| 549 | *p++ = ']'; |
| 550 | *p = '\0'; |
| 551 | |
| 552 | return string(buf, end, sym, spec); |
| 553 | } |
| 554 | |
| 555 | static noinline_for_stack |
| 556 | char *mac_address_string(char *buf, char *end, u8 *addr, |
| 557 | struct printf_spec spec, const char *fmt) |
| 558 | { |
| 559 | char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")]; |
| 560 | char *p = mac_addr; |
| 561 | int i; |
| 562 | char separator; |
| 563 | |
| 564 | if (fmt[1] == 'F') { /* FDDI canonical format */ |
| 565 | separator = '-'; |
| 566 | } else { |
| 567 | separator = ':'; |
| 568 | } |
| 569 | |
| 570 | for (i = 0; i < 6; i++) { |
| 571 | p = hex_byte_pack(p, addr[i]); |
| 572 | if (fmt[0] == 'M' && i != 5) |
| 573 | *p++ = separator; |
| 574 | } |
| 575 | *p = '\0'; |
| 576 | |
| 577 | return string(buf, end, mac_addr, spec); |
| 578 | } |
| 579 | |
| 580 | static noinline_for_stack |
| 581 | char *ip4_string(char *p, const u8 *addr, const char *fmt) |
| 582 | { |
| 583 | int i; |
| 584 | bool leading_zeros = (fmt[0] == 'i'); |
| 585 | int index; |
| 586 | int step; |
| 587 | |
| 588 | switch (fmt[2]) { |
| 589 | case 'h': |
| 590 | #ifdef __BIG_ENDIAN |
| 591 | index = 0; |
| 592 | step = 1; |
| 593 | #else |
| 594 | index = 3; |
| 595 | step = -1; |
| 596 | #endif |
| 597 | break; |
| 598 | case 'l': |
| 599 | index = 3; |
| 600 | step = -1; |
| 601 | break; |
| 602 | case 'n': |
| 603 | case 'b': |
| 604 | default: |
| 605 | index = 0; |
| 606 | step = 1; |
| 607 | break; |
| 608 | } |
| 609 | for (i = 0; i < 4; i++) { |
| 610 | char temp[3]; /* hold each IP quad in reverse order */ |
| 611 | int digits = put_dec_trunc(temp, addr[index]) - temp; |
| 612 | if (leading_zeros) { |
| 613 | if (digits < 3) |
| 614 | *p++ = '0'; |
| 615 | if (digits < 2) |
| 616 | *p++ = '0'; |
| 617 | } |
| 618 | /* reverse the digits in the quad */ |
| 619 | while (digits--) |
| 620 | *p++ = temp[digits]; |
| 621 | if (i < 3) |
| 622 | *p++ = '.'; |
| 623 | index += step; |
| 624 | } |
| 625 | *p = '\0'; |
| 626 | |
| 627 | return p; |
| 628 | } |
| 629 | |
| 630 | static noinline_for_stack |
| 631 | char *ip6_compressed_string(char *p, const char *addr) |
| 632 | { |
| 633 | int i, j, range; |
| 634 | unsigned char zerolength[8]; |
| 635 | int longest = 1; |
| 636 | int colonpos = -1; |
| 637 | u16 word; |
| 638 | u8 hi, lo; |
| 639 | bool needcolon = false; |
| 640 | bool useIPv4; |
| 641 | struct in6_addr in6; |
| 642 | |
| 643 | memcpy(&in6, addr, sizeof(struct in6_addr)); |
| 644 | |
| 645 | useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6); |
| 646 | |
| 647 | memset(zerolength, 0, sizeof(zerolength)); |
| 648 | |
| 649 | if (useIPv4) |
| 650 | range = 6; |
| 651 | else |
| 652 | range = 8; |
| 653 | |
| 654 | /* find position of longest 0 run */ |
| 655 | for (i = 0; i < range; i++) { |
| 656 | for (j = i; j < range; j++) { |
| 657 | if (in6.s6_addr16[j] != 0) |
| 658 | break; |
| 659 | zerolength[i]++; |
| 660 | } |
| 661 | } |
| 662 | for (i = 0; i < range; i++) { |
| 663 | if (zerolength[i] > longest) { |
| 664 | longest = zerolength[i]; |
| 665 | colonpos = i; |
| 666 | } |
| 667 | } |
| 668 | if (longest == 1) /* don't compress a single 0 */ |
| 669 | colonpos = -1; |
| 670 | |
| 671 | /* emit address */ |
| 672 | for (i = 0; i < range; i++) { |
| 673 | if (i == colonpos) { |
| 674 | if (needcolon || i == 0) |
| 675 | *p++ = ':'; |
| 676 | *p++ = ':'; |
| 677 | needcolon = false; |
| 678 | i += longest - 1; |
| 679 | continue; |
| 680 | } |
| 681 | if (needcolon) { |
| 682 | *p++ = ':'; |
| 683 | needcolon = false; |
| 684 | } |
| 685 | /* hex u16 without leading 0s */ |
| 686 | word = ntohs(in6.s6_addr16[i]); |
| 687 | hi = word >> 8; |
| 688 | lo = word & 0xff; |
| 689 | if (hi) { |
| 690 | if (hi > 0x0f) |
| 691 | p = hex_byte_pack(p, hi); |
| 692 | else |
| 693 | *p++ = hex_asc_lo(hi); |
| 694 | p = hex_byte_pack(p, lo); |
| 695 | } |
| 696 | else if (lo > 0x0f) |
| 697 | p = hex_byte_pack(p, lo); |
| 698 | else |
| 699 | *p++ = hex_asc_lo(lo); |
| 700 | needcolon = true; |
| 701 | } |
| 702 | |
| 703 | if (useIPv4) { |
| 704 | if (needcolon) |
| 705 | *p++ = ':'; |
| 706 | p = ip4_string(p, &in6.s6_addr[12], "I4"); |
| 707 | } |
| 708 | *p = '\0'; |
| 709 | |
| 710 | return p; |
| 711 | } |
| 712 | |
| 713 | static noinline_for_stack |
| 714 | char *ip6_string(char *p, const char *addr, const char *fmt) |
| 715 | { |
| 716 | int i; |
| 717 | |
| 718 | for (i = 0; i < 8; i++) { |
| 719 | p = hex_byte_pack(p, *addr++); |
| 720 | p = hex_byte_pack(p, *addr++); |
| 721 | if (fmt[0] == 'I' && i != 7) |
| 722 | *p++ = ':'; |
| 723 | } |
| 724 | *p = '\0'; |
| 725 | |
| 726 | return p; |
| 727 | } |
| 728 | |
| 729 | static noinline_for_stack |
| 730 | char *ip6_addr_string(char *buf, char *end, const u8 *addr, |
| 731 | struct printf_spec spec, const char *fmt) |
| 732 | { |
| 733 | char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")]; |
| 734 | |
| 735 | if (fmt[0] == 'I' && fmt[2] == 'c') |
| 736 | ip6_compressed_string(ip6_addr, addr); |
| 737 | else |
| 738 | ip6_string(ip6_addr, addr, fmt); |
| 739 | |
| 740 | return string(buf, end, ip6_addr, spec); |
| 741 | } |
| 742 | |
| 743 | static noinline_for_stack |
| 744 | char *ip4_addr_string(char *buf, char *end, const u8 *addr, |
| 745 | struct printf_spec spec, const char *fmt) |
| 746 | { |
| 747 | char ip4_addr[sizeof("255.255.255.255")]; |
| 748 | |
| 749 | ip4_string(ip4_addr, addr, fmt); |
| 750 | |
| 751 | return string(buf, end, ip4_addr, spec); |
| 752 | } |
| 753 | |
| 754 | static noinline_for_stack |
| 755 | char *uuid_string(char *buf, char *end, const u8 *addr, |
| 756 | struct printf_spec spec, const char *fmt) |
| 757 | { |
| 758 | char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]; |
| 759 | char *p = uuid; |
| 760 | int i; |
| 761 | static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; |
| 762 | static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15}; |
| 763 | const u8 *index = be; |
| 764 | bool uc = false; |
| 765 | |
| 766 | switch (*(++fmt)) { |
| 767 | case 'L': |
| 768 | uc = true; /* fall-through */ |
| 769 | case 'l': |
| 770 | index = le; |
| 771 | break; |
| 772 | case 'B': |
| 773 | uc = true; |
| 774 | break; |
| 775 | } |
| 776 | |
| 777 | for (i = 0; i < 16; i++) { |
| 778 | p = hex_byte_pack(p, addr[index[i]]); |
| 779 | switch (i) { |
| 780 | case 3: |
| 781 | case 5: |
| 782 | case 7: |
| 783 | case 9: |
| 784 | *p++ = '-'; |
| 785 | break; |
| 786 | } |
| 787 | } |
| 788 | |
| 789 | *p = 0; |
| 790 | |
| 791 | if (uc) { |
| 792 | p = uuid; |
| 793 | do { |
| 794 | *p = toupper(*p); |
| 795 | } while (*(++p)); |
| 796 | } |
| 797 | |
| 798 | return string(buf, end, uuid, spec); |
| 799 | } |
| 800 | |
| 801 | static |
| 802 | char *netdev_feature_string(char *buf, char *end, const u8 *addr, |
| 803 | struct printf_spec spec) |
| 804 | { |
| 805 | spec.flags |= SPECIAL | SMALL | ZEROPAD; |
| 806 | if (spec.field_width == -1) |
| 807 | spec.field_width = 2 + 2 * sizeof(netdev_features_t); |
| 808 | spec.base = 16; |
| 809 | |
| 810 | return number(buf, end, *(const netdev_features_t *)addr, spec); |
| 811 | } |
| 812 | |
| 813 | int kptr_restrict __read_mostly; |
| 814 | |
| 815 | /* |
| 816 | * Show a '%p' thing. A kernel extension is that the '%p' is followed |
| 817 | * by an extra set of alphanumeric characters that are extended format |
| 818 | * specifiers. |
| 819 | * |
| 820 | * Right now we handle: |
| 821 | * |
| 822 | * - 'F' For symbolic function descriptor pointers with offset |
| 823 | * - 'f' For simple symbolic function names without offset |
| 824 | * - 'S' For symbolic direct pointers with offset |
| 825 | * - 's' For symbolic direct pointers without offset |
| 826 | * - 'B' For backtraced symbolic direct pointers with offset |
| 827 | * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref] |
| 828 | * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201] |
| 829 | * - 'M' For a 6-byte MAC address, it prints the address in the |
| 830 | * usual colon-separated hex notation |
| 831 | * - 'm' For a 6-byte MAC address, it prints the hex address without colons |
| 832 | * - 'MF' For a 6-byte MAC FDDI address, it prints the address |
| 833 | * with a dash-separated hex notation |
| 834 | * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way |
| 835 | * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4) |
| 836 | * IPv6 uses colon separated network-order 16 bit hex with leading 0's |
| 837 | * - 'i' [46] for 'raw' IPv4/IPv6 addresses |
| 838 | * IPv6 omits the colons (01020304...0f) |
| 839 | * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006) |
| 840 | * - '[Ii]4[hnbl]' IPv4 addresses in host, network, big or little endian order |
| 841 | * - 'I6c' for IPv6 addresses printed as specified by |
| 842 | * http://tools.ietf.org/html/rfc5952 |
| 843 | * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form |
| 844 | * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" |
| 845 | * Options for %pU are: |
| 846 | * b big endian lower case hex (default) |
| 847 | * B big endian UPPER case hex |
| 848 | * l little endian lower case hex |
| 849 | * L little endian UPPER case hex |
| 850 | * big endian output byte order is: |
| 851 | * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15] |
| 852 | * little endian output byte order is: |
| 853 | * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15] |
| 854 | * - 'V' For a struct va_format which contains a format string * and va_list *, |
| 855 | * call vsnprintf(->format, *->va_list). |
| 856 | * Implements a "recursive vsnprintf". |
| 857 | * Do not use this feature without some mechanism to verify the |
| 858 | * correctness of the format string and va_list arguments. |
| 859 | * - 'K' For a kernel pointer that should be hidden from unprivileged users |
| 860 | * - 'NF' For a netdev_features_t |
| 861 | * |
| 862 | * Note: The difference between 'S' and 'F' is that on ia64 and ppc64 |
| 863 | * function pointers are really function descriptors, which contain a |
| 864 | * pointer to the real address. |
| 865 | */ |
| 866 | static noinline_for_stack |
| 867 | char *pointer(const char *fmt, char *buf, char *end, void *ptr, |
| 868 | struct printf_spec spec) |
| 869 | { |
| 870 | if (!ptr && *fmt != 'K') { |
| 871 | /* |
| 872 | * Print (null) with the same width as a pointer so it makes |
| 873 | * tabular output look nice. |
| 874 | */ |
| 875 | if (spec.field_width == -1) |
| 876 | spec.field_width = 2 * sizeof(void *); |
| 877 | return string(buf, end, "(null)", spec); |
| 878 | } |
| 879 | |
| 880 | switch (*fmt) { |
| 881 | case 'F': |
| 882 | case 'f': |
| 883 | ptr = dereference_function_descriptor(ptr); |
| 884 | /* Fallthrough */ |
| 885 | case 'S': |
| 886 | case 's': |
| 887 | case 'B': |
| 888 | return symbol_string(buf, end, ptr, spec, *fmt); |
| 889 | case 'R': |
| 890 | case 'r': |
| 891 | return resource_string(buf, end, ptr, spec, fmt); |
| 892 | case 'M': /* Colon separated: 00:01:02:03:04:05 */ |
| 893 | case 'm': /* Contiguous: 000102030405 */ |
| 894 | /* [mM]F (FDDI, bit reversed) */ |
| 895 | return mac_address_string(buf, end, ptr, spec, fmt); |
| 896 | case 'I': /* Formatted IP supported |
| 897 | * 4: 1.2.3.4 |
| 898 | * 6: 0001:0203:...:0708 |
| 899 | * 6c: 1::708 or 1::1.2.3.4 |
| 900 | */ |
| 901 | case 'i': /* Contiguous: |
| 902 | * 4: 001.002.003.004 |
| 903 | * 6: 000102...0f |
| 904 | */ |
| 905 | switch (fmt[1]) { |
| 906 | case '6': |
| 907 | return ip6_addr_string(buf, end, ptr, spec, fmt); |
| 908 | case '4': |
| 909 | return ip4_addr_string(buf, end, ptr, spec, fmt); |
| 910 | } |
| 911 | break; |
| 912 | case 'U': |
| 913 | return uuid_string(buf, end, ptr, spec, fmt); |
| 914 | case 'V': |
| 915 | { |
| 916 | va_list va; |
| 917 | |
| 918 | va_copy(va, *((struct va_format *)ptr)->va); |
| 919 | buf += vsnprintf(buf, end > buf ? end - buf : 0, |
| 920 | ((struct va_format *)ptr)->fmt, va); |
| 921 | va_end(va); |
| 922 | return buf; |
| 923 | } |
| 924 | case 'K': |
| 925 | /* |
| 926 | * %pK cannot be used in IRQ context because its test |
| 927 | * for CAP_SYSLOG would be meaningless. |
| 928 | */ |
| 929 | if (kptr_restrict && (in_irq() || in_serving_softirq() || |
| 930 | in_nmi())) { |
| 931 | if (spec.field_width == -1) |
| 932 | spec.field_width = 2 * sizeof(void *); |
| 933 | return string(buf, end, "pK-error", spec); |
| 934 | } |
| 935 | |
| 936 | switch (kptr_restrict) { |
| 937 | case 0: |
| 938 | /* Always print %pK values */ |
| 939 | break; |
| 940 | case 1: { |
| 941 | /* |
| 942 | * Only print the real pointer value if the current |
| 943 | * process has CAP_SYSLOG and is running with the |
| 944 | * same credentials it started with. This is because |
| 945 | * access to files is checked at open() time, but %pK |
| 946 | * checks permission at read() time. We don't want to |
| 947 | * leak pointer values if a binary opens a file using |
| 948 | * %pK and then elevates privileges before reading it. |
| 949 | */ |
| 950 | const struct cred *cred = current_cred(); |
| 951 | |
| 952 | if (!has_capability_noaudit(current, CAP_SYSLOG) || |
| 953 | (cred->euid != cred->uid) || |
| 954 | (cred->egid != cred->gid)) |
| 955 | ptr = NULL; |
| 956 | break; |
| 957 | } |
| 958 | case 2: |
| 959 | default: |
| 960 | /* Always print 0's for %pK */ |
| 961 | ptr = NULL; |
| 962 | break; |
| 963 | } |
| 964 | break; |
| 965 | |
| 966 | case 'N': |
| 967 | switch (fmt[1]) { |
| 968 | case 'F': |
| 969 | return netdev_feature_string(buf, end, ptr, spec); |
| 970 | } |
| 971 | break; |
| 972 | } |
| 973 | spec.flags |= SMALL; |
| 974 | if (spec.field_width == -1) { |
| 975 | spec.field_width = 2 * sizeof(void *); |
| 976 | spec.flags |= ZEROPAD; |
| 977 | } |
| 978 | spec.base = 16; |
| 979 | |
| 980 | return number(buf, end, (unsigned long) ptr, spec); |
| 981 | } |
| 982 | |
| 983 | /* |
| 984 | * Helper function to decode printf style format. |
| 985 | * Each call decode a token from the format and return the |
| 986 | * number of characters read (or likely the delta where it wants |
| 987 | * to go on the next call). |
| 988 | * The decoded token is returned through the parameters |
| 989 | * |
| 990 | * 'h', 'l', or 'L' for integer fields |
| 991 | * 'z' support added 23/7/1999 S.H. |
| 992 | * 'z' changed to 'Z' --davidm 1/25/99 |
| 993 | * 't' added for ptrdiff_t |
| 994 | * |
| 995 | * @fmt: the format string |
| 996 | * @type of the token returned |
| 997 | * @flags: various flags such as +, -, # tokens.. |
| 998 | * @field_width: overwritten width |
| 999 | * @base: base of the number (octal, hex, ...) |
| 1000 | * @precision: precision of a number |
| 1001 | * @qualifier: qualifier of a number (long, size_t, ...) |
| 1002 | */ |
| 1003 | static noinline_for_stack |
| 1004 | int format_decode(const char *fmt, struct printf_spec *spec) |
| 1005 | { |
| 1006 | const char *start = fmt; |
| 1007 | |
| 1008 | /* we finished early by reading the field width */ |
| 1009 | if (spec->type == FORMAT_TYPE_WIDTH) { |
| 1010 | if (spec->field_width < 0) { |
| 1011 | spec->field_width = -spec->field_width; |
| 1012 | spec->flags |= LEFT; |
| 1013 | } |
| 1014 | spec->type = FORMAT_TYPE_NONE; |
| 1015 | goto precision; |
| 1016 | } |
| 1017 | |
| 1018 | /* we finished early by reading the precision */ |
| 1019 | if (spec->type == FORMAT_TYPE_PRECISION) { |
| 1020 | if (spec->precision < 0) |
| 1021 | spec->precision = 0; |
| 1022 | |
| 1023 | spec->type = FORMAT_TYPE_NONE; |
| 1024 | goto qualifier; |
| 1025 | } |
| 1026 | |
| 1027 | /* By default */ |
| 1028 | spec->type = FORMAT_TYPE_NONE; |
| 1029 | |
| 1030 | for (; *fmt ; ++fmt) { |
| 1031 | if (*fmt == '%') |
| 1032 | break; |
| 1033 | } |
| 1034 | |
| 1035 | /* Return the current non-format string */ |
| 1036 | if (fmt != start || !*fmt) |
| 1037 | return fmt - start; |
| 1038 | |
| 1039 | /* Process flags */ |
| 1040 | spec->flags = 0; |
| 1041 | |
| 1042 | while (1) { /* this also skips first '%' */ |
| 1043 | bool found = true; |
| 1044 | |
| 1045 | ++fmt; |
| 1046 | |
| 1047 | switch (*fmt) { |
| 1048 | case '-': spec->flags |= LEFT; break; |
| 1049 | case '+': spec->flags |= PLUS; break; |
| 1050 | case ' ': spec->flags |= SPACE; break; |
| 1051 | case '#': spec->flags |= SPECIAL; break; |
| 1052 | case '0': spec->flags |= ZEROPAD; break; |
| 1053 | default: found = false; |
| 1054 | } |
| 1055 | |
| 1056 | if (!found) |
| 1057 | break; |
| 1058 | } |
| 1059 | |
| 1060 | /* get field width */ |
| 1061 | spec->field_width = -1; |
| 1062 | |
| 1063 | if (isdigit(*fmt)) |
| 1064 | spec->field_width = skip_atoi(&fmt); |
| 1065 | else if (*fmt == '*') { |
| 1066 | /* it's the next argument */ |
| 1067 | spec->type = FORMAT_TYPE_WIDTH; |
| 1068 | return ++fmt - start; |
| 1069 | } |
| 1070 | |
| 1071 | precision: |
| 1072 | /* get the precision */ |
| 1073 | spec->precision = -1; |
| 1074 | if (*fmt == '.') { |
| 1075 | ++fmt; |
| 1076 | if (isdigit(*fmt)) { |
| 1077 | spec->precision = skip_atoi(&fmt); |
| 1078 | if (spec->precision < 0) |
| 1079 | spec->precision = 0; |
| 1080 | } else if (*fmt == '*') { |
| 1081 | /* it's the next argument */ |
| 1082 | spec->type = FORMAT_TYPE_PRECISION; |
| 1083 | return ++fmt - start; |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | qualifier: |
| 1088 | /* get the conversion qualifier */ |
| 1089 | spec->qualifier = -1; |
| 1090 | if (*fmt == 'h' || _tolower(*fmt) == 'l' || |
| 1091 | _tolower(*fmt) == 'z' || *fmt == 't') { |
| 1092 | spec->qualifier = *fmt++; |
| 1093 | if (unlikely(spec->qualifier == *fmt)) { |
| 1094 | if (spec->qualifier == 'l') { |
| 1095 | spec->qualifier = 'L'; |
| 1096 | ++fmt; |
| 1097 | } else if (spec->qualifier == 'h') { |
| 1098 | spec->qualifier = 'H'; |
| 1099 | ++fmt; |
| 1100 | } |
| 1101 | } |
| 1102 | } |
| 1103 | |
| 1104 | /* default base */ |
| 1105 | spec->base = 10; |
| 1106 | switch (*fmt) { |
| 1107 | case 'c': |
| 1108 | spec->type = FORMAT_TYPE_CHAR; |
| 1109 | return ++fmt - start; |
| 1110 | |
| 1111 | case 's': |
| 1112 | spec->type = FORMAT_TYPE_STR; |
| 1113 | return ++fmt - start; |
| 1114 | |
| 1115 | case 'p': |
| 1116 | spec->type = FORMAT_TYPE_PTR; |
| 1117 | return fmt - start; |
| 1118 | /* skip alnum */ |
| 1119 | |
| 1120 | case 'n': |
| 1121 | spec->type = FORMAT_TYPE_NRCHARS; |
| 1122 | return ++fmt - start; |
| 1123 | |
| 1124 | case '%': |
| 1125 | spec->type = FORMAT_TYPE_PERCENT_CHAR; |
| 1126 | return ++fmt - start; |
| 1127 | |
| 1128 | /* integer number formats - set up the flags and "break" */ |
| 1129 | case 'o': |
| 1130 | spec->base = 8; |
| 1131 | break; |
| 1132 | |
| 1133 | case 'x': |
| 1134 | spec->flags |= SMALL; |
| 1135 | |
| 1136 | case 'X': |
| 1137 | spec->base = 16; |
| 1138 | break; |
| 1139 | |
| 1140 | case 'd': |
| 1141 | case 'i': |
| 1142 | spec->flags |= SIGN; |
| 1143 | case 'u': |
| 1144 | break; |
| 1145 | |
| 1146 | default: |
| 1147 | spec->type = FORMAT_TYPE_INVALID; |
| 1148 | return fmt - start; |
| 1149 | } |
| 1150 | |
| 1151 | if (spec->qualifier == 'L') |
| 1152 | spec->type = FORMAT_TYPE_LONG_LONG; |
| 1153 | else if (spec->qualifier == 'l') { |
| 1154 | if (spec->flags & SIGN) |
| 1155 | spec->type = FORMAT_TYPE_LONG; |
| 1156 | else |
| 1157 | spec->type = FORMAT_TYPE_ULONG; |
| 1158 | } else if (_tolower(spec->qualifier) == 'z') { |
| 1159 | spec->type = FORMAT_TYPE_SIZE_T; |
| 1160 | } else if (spec->qualifier == 't') { |
| 1161 | spec->type = FORMAT_TYPE_PTRDIFF; |
| 1162 | } else if (spec->qualifier == 'H') { |
| 1163 | if (spec->flags & SIGN) |
| 1164 | spec->type = FORMAT_TYPE_BYTE; |
| 1165 | else |
| 1166 | spec->type = FORMAT_TYPE_UBYTE; |
| 1167 | } else if (spec->qualifier == 'h') { |
| 1168 | if (spec->flags & SIGN) |
| 1169 | spec->type = FORMAT_TYPE_SHORT; |
| 1170 | else |
| 1171 | spec->type = FORMAT_TYPE_USHORT; |
| 1172 | } else { |
| 1173 | if (spec->flags & SIGN) |
| 1174 | spec->type = FORMAT_TYPE_INT; |
| 1175 | else |
| 1176 | spec->type = FORMAT_TYPE_UINT; |
| 1177 | } |
| 1178 | |
| 1179 | return ++fmt - start; |
| 1180 | } |
| 1181 | |
| 1182 | /** |
| 1183 | * vsnprintf - Format a string and place it in a buffer |
| 1184 | * @buf: The buffer to place the result into |
| 1185 | * @size: The size of the buffer, including the trailing null space |
| 1186 | * @fmt: The format string to use |
| 1187 | * @args: Arguments for the format string |
| 1188 | * |
| 1189 | * This function follows C99 vsnprintf, but has some extensions: |
| 1190 | * %pS output the name of a text symbol with offset |
| 1191 | * %ps output the name of a text symbol without offset |
| 1192 | * %pF output the name of a function pointer with its offset |
| 1193 | * %pf output the name of a function pointer without its offset |
| 1194 | * %pB output the name of a backtrace symbol with its offset |
| 1195 | * %pR output the address range in a struct resource with decoded flags |
| 1196 | * %pr output the address range in a struct resource with raw flags |
| 1197 | * %pM output a 6-byte MAC address with colons |
| 1198 | * %pm output a 6-byte MAC address without colons |
| 1199 | * %pI4 print an IPv4 address without leading zeros |
| 1200 | * %pi4 print an IPv4 address with leading zeros |
| 1201 | * %pI6 print an IPv6 address with colons |
| 1202 | * %pi6 print an IPv6 address without colons |
| 1203 | * %pI6c print an IPv6 address as specified by RFC 5952 |
| 1204 | * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper |
| 1205 | * case. |
| 1206 | * %n is ignored |
| 1207 | * |
| 1208 | * The return value is the number of characters which would |
| 1209 | * be generated for the given input, excluding the trailing |
| 1210 | * '\0', as per ISO C99. If you want to have the exact |
| 1211 | * number of characters written into @buf as return value |
| 1212 | * (not including the trailing '\0'), use vscnprintf(). If the |
| 1213 | * return is greater than or equal to @size, the resulting |
| 1214 | * string is truncated. |
| 1215 | * |
| 1216 | * If you're not already dealing with a va_list consider using snprintf(). |
| 1217 | */ |
| 1218 | int vsnprintf(char *buf, size_t size, const char *fmt, va_list args) |
| 1219 | { |
| 1220 | unsigned long long num; |
| 1221 | char *str, *end; |
| 1222 | struct printf_spec spec = {0}; |
| 1223 | |
| 1224 | /* Reject out-of-range values early. Large positive sizes are |
| 1225 | used for unknown buffer sizes. */ |
| 1226 | if (WARN_ON_ONCE((int) size < 0)) |
| 1227 | return 0; |
| 1228 | |
| 1229 | str = buf; |
| 1230 | end = buf + size; |
| 1231 | |
| 1232 | /* Make sure end is always >= buf */ |
| 1233 | if (end < buf) { |
| 1234 | end = ((void *)-1); |
| 1235 | size = end - buf; |
| 1236 | } |
| 1237 | |
| 1238 | while (*fmt) { |
| 1239 | const char *old_fmt = fmt; |
| 1240 | int read = format_decode(fmt, &spec); |
| 1241 | |
| 1242 | fmt += read; |
| 1243 | |
| 1244 | switch (spec.type) { |
| 1245 | case FORMAT_TYPE_NONE: { |
| 1246 | int copy = read; |
| 1247 | if (str < end) { |
| 1248 | if (copy > end - str) |
| 1249 | copy = end - str; |
| 1250 | memcpy(str, old_fmt, copy); |
| 1251 | } |
| 1252 | str += read; |
| 1253 | break; |
| 1254 | } |
| 1255 | |
| 1256 | case FORMAT_TYPE_WIDTH: |
| 1257 | spec.field_width = va_arg(args, int); |
| 1258 | break; |
| 1259 | |
| 1260 | case FORMAT_TYPE_PRECISION: |
| 1261 | spec.precision = va_arg(args, int); |
| 1262 | break; |
| 1263 | |
| 1264 | case FORMAT_TYPE_CHAR: { |
| 1265 | char c; |
| 1266 | |
| 1267 | if (!(spec.flags & LEFT)) { |
| 1268 | while (--spec.field_width > 0) { |
| 1269 | if (str < end) |
| 1270 | *str = ' '; |
| 1271 | ++str; |
| 1272 | |
| 1273 | } |
| 1274 | } |
| 1275 | c = (unsigned char) va_arg(args, int); |
| 1276 | if (str < end) |
| 1277 | *str = c; |
| 1278 | ++str; |
| 1279 | while (--spec.field_width > 0) { |
| 1280 | if (str < end) |
| 1281 | *str = ' '; |
| 1282 | ++str; |
| 1283 | } |
| 1284 | break; |
| 1285 | } |
| 1286 | |
| 1287 | case FORMAT_TYPE_STR: |
| 1288 | str = string(str, end, va_arg(args, char *), spec); |
| 1289 | break; |
| 1290 | |
| 1291 | case FORMAT_TYPE_PTR: |
| 1292 | str = pointer(fmt+1, str, end, va_arg(args, void *), |
| 1293 | spec); |
| 1294 | while (isalnum(*fmt)) |
| 1295 | fmt++; |
| 1296 | break; |
| 1297 | |
| 1298 | case FORMAT_TYPE_PERCENT_CHAR: |
| 1299 | if (str < end) |
| 1300 | *str = '%'; |
| 1301 | ++str; |
| 1302 | break; |
| 1303 | |
| 1304 | case FORMAT_TYPE_INVALID: |
| 1305 | if (str < end) |
| 1306 | *str = '%'; |
| 1307 | ++str; |
| 1308 | break; |
| 1309 | |
| 1310 | case FORMAT_TYPE_NRCHARS: { |
| 1311 | u8 qualifier = spec.qualifier; |
| 1312 | |
| 1313 | if (qualifier == 'l') { |
| 1314 | long *ip = va_arg(args, long *); |
| 1315 | *ip = (str - buf); |
| 1316 | } else if (_tolower(qualifier) == 'z') { |
| 1317 | size_t *ip = va_arg(args, size_t *); |
| 1318 | *ip = (str - buf); |
| 1319 | } else { |
| 1320 | int *ip = va_arg(args, int *); |
| 1321 | *ip = (str - buf); |
| 1322 | } |
| 1323 | break; |
| 1324 | } |
| 1325 | |
| 1326 | default: |
| 1327 | switch (spec.type) { |
| 1328 | case FORMAT_TYPE_LONG_LONG: |
| 1329 | num = va_arg(args, long long); |
| 1330 | break; |
| 1331 | case FORMAT_TYPE_ULONG: |
| 1332 | num = va_arg(args, unsigned long); |
| 1333 | break; |
| 1334 | case FORMAT_TYPE_LONG: |
| 1335 | num = va_arg(args, long); |
| 1336 | break; |
| 1337 | case FORMAT_TYPE_SIZE_T: |
| 1338 | num = va_arg(args, size_t); |
| 1339 | break; |
| 1340 | case FORMAT_TYPE_PTRDIFF: |
| 1341 | num = va_arg(args, ptrdiff_t); |
| 1342 | break; |
| 1343 | case FORMAT_TYPE_UBYTE: |
| 1344 | num = (unsigned char) va_arg(args, int); |
| 1345 | break; |
| 1346 | case FORMAT_TYPE_BYTE: |
| 1347 | num = (signed char) va_arg(args, int); |
| 1348 | break; |
| 1349 | case FORMAT_TYPE_USHORT: |
| 1350 | num = (unsigned short) va_arg(args, int); |
| 1351 | break; |
| 1352 | case FORMAT_TYPE_SHORT: |
| 1353 | num = (short) va_arg(args, int); |
| 1354 | break; |
| 1355 | case FORMAT_TYPE_INT: |
| 1356 | num = (int) va_arg(args, int); |
| 1357 | break; |
| 1358 | default: |
| 1359 | num = va_arg(args, unsigned int); |
| 1360 | } |
| 1361 | |
| 1362 | str = number(str, end, num, spec); |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | if (size > 0) { |
| 1367 | if (str < end) |
| 1368 | *str = '\0'; |
| 1369 | else |
| 1370 | end[-1] = '\0'; |
| 1371 | } |
| 1372 | |
| 1373 | /* the trailing null byte doesn't count towards the total */ |
| 1374 | return str-buf; |
| 1375 | |
| 1376 | } |
| 1377 | EXPORT_SYMBOL(vsnprintf); |
| 1378 | |
| 1379 | /** |
| 1380 | * vscnprintf - Format a string and place it in a buffer |
| 1381 | * @buf: The buffer to place the result into |
| 1382 | * @size: The size of the buffer, including the trailing null space |
| 1383 | * @fmt: The format string to use |
| 1384 | * @args: Arguments for the format string |
| 1385 | * |
| 1386 | * The return value is the number of characters which have been written into |
| 1387 | * the @buf not including the trailing '\0'. If @size is == 0 the function |
| 1388 | * returns 0. |
| 1389 | * |
| 1390 | * If you're not already dealing with a va_list consider using scnprintf(). |
| 1391 | * |
| 1392 | * See the vsnprintf() documentation for format string extensions over C99. |
| 1393 | */ |
| 1394 | int vscnprintf(char *buf, size_t size, const char *fmt, va_list args) |
| 1395 | { |
| 1396 | int i; |
| 1397 | |
| 1398 | i = vsnprintf(buf, size, fmt, args); |
| 1399 | |
| 1400 | if (likely(i < size)) |
| 1401 | return i; |
| 1402 | if (size != 0) |
| 1403 | return size - 1; |
| 1404 | return 0; |
| 1405 | } |
| 1406 | EXPORT_SYMBOL(vscnprintf); |
| 1407 | |
| 1408 | /** |
| 1409 | * snprintf - Format a string and place it in a buffer |
| 1410 | * @buf: The buffer to place the result into |
| 1411 | * @size: The size of the buffer, including the trailing null space |
| 1412 | * @fmt: The format string to use |
| 1413 | * @...: Arguments for the format string |
| 1414 | * |
| 1415 | * The return value is the number of characters which would be |
| 1416 | * generated for the given input, excluding the trailing null, |
| 1417 | * as per ISO C99. If the return is greater than or equal to |
| 1418 | * @size, the resulting string is truncated. |
| 1419 | * |
| 1420 | * See the vsnprintf() documentation for format string extensions over C99. |
| 1421 | */ |
| 1422 | int snprintf(char *buf, size_t size, const char *fmt, ...) |
| 1423 | { |
| 1424 | va_list args; |
| 1425 | int i; |
| 1426 | |
| 1427 | va_start(args, fmt); |
| 1428 | i = vsnprintf(buf, size, fmt, args); |
| 1429 | va_end(args); |
| 1430 | |
| 1431 | return i; |
| 1432 | } |
| 1433 | EXPORT_SYMBOL(snprintf); |
| 1434 | |
| 1435 | /** |
| 1436 | * scnprintf - Format a string and place it in a buffer |
| 1437 | * @buf: The buffer to place the result into |
| 1438 | * @size: The size of the buffer, including the trailing null space |
| 1439 | * @fmt: The format string to use |
| 1440 | * @...: Arguments for the format string |
| 1441 | * |
| 1442 | * The return value is the number of characters written into @buf not including |
| 1443 | * the trailing '\0'. If @size is == 0 the function returns 0. |
| 1444 | */ |
| 1445 | |
| 1446 | int scnprintf(char *buf, size_t size, const char *fmt, ...) |
| 1447 | { |
| 1448 | va_list args; |
| 1449 | int i; |
| 1450 | |
| 1451 | va_start(args, fmt); |
| 1452 | i = vscnprintf(buf, size, fmt, args); |
| 1453 | va_end(args); |
| 1454 | |
| 1455 | return i; |
| 1456 | } |
| 1457 | EXPORT_SYMBOL(scnprintf); |
| 1458 | |
| 1459 | /** |
| 1460 | * vsprintf - Format a string and place it in a buffer |
| 1461 | * @buf: The buffer to place the result into |
| 1462 | * @fmt: The format string to use |
| 1463 | * @args: Arguments for the format string |
| 1464 | * |
| 1465 | * The function returns the number of characters written |
| 1466 | * into @buf. Use vsnprintf() or vscnprintf() in order to avoid |
| 1467 | * buffer overflows. |
| 1468 | * |
| 1469 | * If you're not already dealing with a va_list consider using sprintf(). |
| 1470 | * |
| 1471 | * See the vsnprintf() documentation for format string extensions over C99. |
| 1472 | */ |
| 1473 | int vsprintf(char *buf, const char *fmt, va_list args) |
| 1474 | { |
| 1475 | return vsnprintf(buf, INT_MAX, fmt, args); |
| 1476 | } |
| 1477 | EXPORT_SYMBOL(vsprintf); |
| 1478 | |
| 1479 | /** |
| 1480 | * sprintf - Format a string and place it in a buffer |
| 1481 | * @buf: The buffer to place the result into |
| 1482 | * @fmt: The format string to use |
| 1483 | * @...: Arguments for the format string |
| 1484 | * |
| 1485 | * The function returns the number of characters written |
| 1486 | * into @buf. Use snprintf() or scnprintf() in order to avoid |
| 1487 | * buffer overflows. |
| 1488 | * |
| 1489 | * See the vsnprintf() documentation for format string extensions over C99. |
| 1490 | */ |
| 1491 | int sprintf(char *buf, const char *fmt, ...) |
| 1492 | { |
| 1493 | va_list args; |
| 1494 | int i; |
| 1495 | |
| 1496 | va_start(args, fmt); |
| 1497 | i = vsnprintf(buf, INT_MAX, fmt, args); |
| 1498 | va_end(args); |
| 1499 | |
| 1500 | return i; |
| 1501 | } |
| 1502 | EXPORT_SYMBOL(sprintf); |
| 1503 | |
| 1504 | #ifdef CONFIG_BINARY_PRINTF |
| 1505 | /* |
| 1506 | * bprintf service: |
| 1507 | * vbin_printf() - VA arguments to binary data |
| 1508 | * bstr_printf() - Binary data to text string |
| 1509 | */ |
| 1510 | |
| 1511 | /** |
| 1512 | * vbin_printf - Parse a format string and place args' binary value in a buffer |
| 1513 | * @bin_buf: The buffer to place args' binary value |
| 1514 | * @size: The size of the buffer(by words(32bits), not characters) |
| 1515 | * @fmt: The format string to use |
| 1516 | * @args: Arguments for the format string |
| 1517 | * |
| 1518 | * The format follows C99 vsnprintf, except %n is ignored, and its argument |
| 1519 | * is skiped. |
| 1520 | * |
| 1521 | * The return value is the number of words(32bits) which would be generated for |
| 1522 | * the given input. |
| 1523 | * |
| 1524 | * NOTE: |
| 1525 | * If the return value is greater than @size, the resulting bin_buf is NOT |
| 1526 | * valid for bstr_printf(). |
| 1527 | */ |
| 1528 | int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args) |
| 1529 | { |
| 1530 | struct printf_spec spec = {0}; |
| 1531 | char *str, *end; |
| 1532 | |
| 1533 | str = (char *)bin_buf; |
| 1534 | end = (char *)(bin_buf + size); |
| 1535 | |
| 1536 | #define save_arg(type) \ |
| 1537 | do { \ |
| 1538 | if (sizeof(type) == 8) { \ |
| 1539 | unsigned long long value; \ |
| 1540 | str = PTR_ALIGN(str, sizeof(u32)); \ |
| 1541 | value = va_arg(args, unsigned long long); \ |
| 1542 | if (str + sizeof(type) <= end) { \ |
| 1543 | *(u32 *)str = *(u32 *)&value; \ |
| 1544 | *(u32 *)(str + 4) = *((u32 *)&value + 1); \ |
| 1545 | } \ |
| 1546 | } else { \ |
| 1547 | unsigned long value; \ |
| 1548 | str = PTR_ALIGN(str, sizeof(type)); \ |
| 1549 | value = va_arg(args, int); \ |
| 1550 | if (str + sizeof(type) <= end) \ |
| 1551 | *(typeof(type) *)str = (type)value; \ |
| 1552 | } \ |
| 1553 | str += sizeof(type); \ |
| 1554 | } while (0) |
| 1555 | |
| 1556 | while (*fmt) { |
| 1557 | int read = format_decode(fmt, &spec); |
| 1558 | |
| 1559 | fmt += read; |
| 1560 | |
| 1561 | switch (spec.type) { |
| 1562 | case FORMAT_TYPE_NONE: |
| 1563 | case FORMAT_TYPE_INVALID: |
| 1564 | case FORMAT_TYPE_PERCENT_CHAR: |
| 1565 | break; |
| 1566 | |
| 1567 | case FORMAT_TYPE_WIDTH: |
| 1568 | case FORMAT_TYPE_PRECISION: |
| 1569 | save_arg(int); |
| 1570 | break; |
| 1571 | |
| 1572 | case FORMAT_TYPE_CHAR: |
| 1573 | save_arg(char); |
| 1574 | break; |
| 1575 | |
| 1576 | case FORMAT_TYPE_STR: { |
| 1577 | const char *save_str = va_arg(args, char *); |
| 1578 | size_t len; |
| 1579 | |
| 1580 | if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE |
| 1581 | || (unsigned long)save_str < PAGE_SIZE) |
| 1582 | save_str = "(null)"; |
| 1583 | len = strlen(save_str) + 1; |
| 1584 | if (str + len < end) |
| 1585 | memcpy(str, save_str, len); |
| 1586 | str += len; |
| 1587 | break; |
| 1588 | } |
| 1589 | |
| 1590 | case FORMAT_TYPE_PTR: |
| 1591 | save_arg(void *); |
| 1592 | /* skip all alphanumeric pointer suffixes */ |
| 1593 | while (isalnum(*fmt)) |
| 1594 | fmt++; |
| 1595 | break; |
| 1596 | |
| 1597 | case FORMAT_TYPE_NRCHARS: { |
| 1598 | /* skip %n 's argument */ |
| 1599 | u8 qualifier = spec.qualifier; |
| 1600 | void *skip_arg; |
| 1601 | if (qualifier == 'l') |
| 1602 | skip_arg = va_arg(args, long *); |
| 1603 | else if (_tolower(qualifier) == 'z') |
| 1604 | skip_arg = va_arg(args, size_t *); |
| 1605 | else |
| 1606 | skip_arg = va_arg(args, int *); |
| 1607 | break; |
| 1608 | } |
| 1609 | |
| 1610 | default: |
| 1611 | switch (spec.type) { |
| 1612 | |
| 1613 | case FORMAT_TYPE_LONG_LONG: |
| 1614 | save_arg(long long); |
| 1615 | break; |
| 1616 | case FORMAT_TYPE_ULONG: |
| 1617 | case FORMAT_TYPE_LONG: |
| 1618 | save_arg(unsigned long); |
| 1619 | break; |
| 1620 | case FORMAT_TYPE_SIZE_T: |
| 1621 | save_arg(size_t); |
| 1622 | break; |
| 1623 | case FORMAT_TYPE_PTRDIFF: |
| 1624 | save_arg(ptrdiff_t); |
| 1625 | break; |
| 1626 | case FORMAT_TYPE_UBYTE: |
| 1627 | case FORMAT_TYPE_BYTE: |
| 1628 | save_arg(char); |
| 1629 | break; |
| 1630 | case FORMAT_TYPE_USHORT: |
| 1631 | case FORMAT_TYPE_SHORT: |
| 1632 | save_arg(short); |
| 1633 | break; |
| 1634 | default: |
| 1635 | save_arg(int); |
| 1636 | } |
| 1637 | } |
| 1638 | } |
| 1639 | |
| 1640 | return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf; |
| 1641 | #undef save_arg |
| 1642 | } |
| 1643 | EXPORT_SYMBOL_GPL(vbin_printf); |
| 1644 | |
| 1645 | /** |
| 1646 | * bstr_printf - Format a string from binary arguments and place it in a buffer |
| 1647 | * @buf: The buffer to place the result into |
| 1648 | * @size: The size of the buffer, including the trailing null space |
| 1649 | * @fmt: The format string to use |
| 1650 | * @bin_buf: Binary arguments for the format string |
| 1651 | * |
| 1652 | * This function like C99 vsnprintf, but the difference is that vsnprintf gets |
| 1653 | * arguments from stack, and bstr_printf gets arguments from @bin_buf which is |
| 1654 | * a binary buffer that generated by vbin_printf. |
| 1655 | * |
| 1656 | * The format follows C99 vsnprintf, but has some extensions: |
| 1657 | * see vsnprintf comment for details. |
| 1658 | * |
| 1659 | * The return value is the number of characters which would |
| 1660 | * be generated for the given input, excluding the trailing |
| 1661 | * '\0', as per ISO C99. If you want to have the exact |
| 1662 | * number of characters written into @buf as return value |
| 1663 | * (not including the trailing '\0'), use vscnprintf(). If the |
| 1664 | * return is greater than or equal to @size, the resulting |
| 1665 | * string is truncated. |
| 1666 | */ |
| 1667 | int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf) |
| 1668 | { |
| 1669 | struct printf_spec spec = {0}; |
| 1670 | char *str, *end; |
| 1671 | const char *args = (const char *)bin_buf; |
| 1672 | |
| 1673 | if (WARN_ON_ONCE((int) size < 0)) |
| 1674 | return 0; |
| 1675 | |
| 1676 | str = buf; |
| 1677 | end = buf + size; |
| 1678 | |
| 1679 | #define get_arg(type) \ |
| 1680 | ({ \ |
| 1681 | typeof(type) value; \ |
| 1682 | if (sizeof(type) == 8) { \ |
| 1683 | args = PTR_ALIGN(args, sizeof(u32)); \ |
| 1684 | *(u32 *)&value = *(u32 *)args; \ |
| 1685 | *((u32 *)&value + 1) = *(u32 *)(args + 4); \ |
| 1686 | } else { \ |
| 1687 | args = PTR_ALIGN(args, sizeof(type)); \ |
| 1688 | value = *(typeof(type) *)args; \ |
| 1689 | } \ |
| 1690 | args += sizeof(type); \ |
| 1691 | value; \ |
| 1692 | }) |
| 1693 | |
| 1694 | /* Make sure end is always >= buf */ |
| 1695 | if (end < buf) { |
| 1696 | end = ((void *)-1); |
| 1697 | size = end - buf; |
| 1698 | } |
| 1699 | |
| 1700 | while (*fmt) { |
| 1701 | const char *old_fmt = fmt; |
| 1702 | int read = format_decode(fmt, &spec); |
| 1703 | |
| 1704 | fmt += read; |
| 1705 | |
| 1706 | switch (spec.type) { |
| 1707 | case FORMAT_TYPE_NONE: { |
| 1708 | int copy = read; |
| 1709 | if (str < end) { |
| 1710 | if (copy > end - str) |
| 1711 | copy = end - str; |
| 1712 | memcpy(str, old_fmt, copy); |
| 1713 | } |
| 1714 | str += read; |
| 1715 | break; |
| 1716 | } |
| 1717 | |
| 1718 | case FORMAT_TYPE_WIDTH: |
| 1719 | spec.field_width = get_arg(int); |
| 1720 | break; |
| 1721 | |
| 1722 | case FORMAT_TYPE_PRECISION: |
| 1723 | spec.precision = get_arg(int); |
| 1724 | break; |
| 1725 | |
| 1726 | case FORMAT_TYPE_CHAR: { |
| 1727 | char c; |
| 1728 | |
| 1729 | if (!(spec.flags & LEFT)) { |
| 1730 | while (--spec.field_width > 0) { |
| 1731 | if (str < end) |
| 1732 | *str = ' '; |
| 1733 | ++str; |
| 1734 | } |
| 1735 | } |
| 1736 | c = (unsigned char) get_arg(char); |
| 1737 | if (str < end) |
| 1738 | *str = c; |
| 1739 | ++str; |
| 1740 | while (--spec.field_width > 0) { |
| 1741 | if (str < end) |
| 1742 | *str = ' '; |
| 1743 | ++str; |
| 1744 | } |
| 1745 | break; |
| 1746 | } |
| 1747 | |
| 1748 | case FORMAT_TYPE_STR: { |
| 1749 | const char *str_arg = args; |
| 1750 | args += strlen(str_arg) + 1; |
| 1751 | str = string(str, end, (char *)str_arg, spec); |
| 1752 | break; |
| 1753 | } |
| 1754 | |
| 1755 | case FORMAT_TYPE_PTR: |
| 1756 | str = pointer(fmt+1, str, end, get_arg(void *), spec); |
| 1757 | while (isalnum(*fmt)) |
| 1758 | fmt++; |
| 1759 | break; |
| 1760 | |
| 1761 | case FORMAT_TYPE_PERCENT_CHAR: |
| 1762 | case FORMAT_TYPE_INVALID: |
| 1763 | if (str < end) |
| 1764 | *str = '%'; |
| 1765 | ++str; |
| 1766 | break; |
| 1767 | |
| 1768 | case FORMAT_TYPE_NRCHARS: |
| 1769 | /* skip */ |
| 1770 | break; |
| 1771 | |
| 1772 | default: { |
| 1773 | unsigned long long num; |
| 1774 | |
| 1775 | switch (spec.type) { |
| 1776 | |
| 1777 | case FORMAT_TYPE_LONG_LONG: |
| 1778 | num = get_arg(long long); |
| 1779 | break; |
| 1780 | case FORMAT_TYPE_ULONG: |
| 1781 | case FORMAT_TYPE_LONG: |
| 1782 | num = get_arg(unsigned long); |
| 1783 | break; |
| 1784 | case FORMAT_TYPE_SIZE_T: |
| 1785 | num = get_arg(size_t); |
| 1786 | break; |
| 1787 | case FORMAT_TYPE_PTRDIFF: |
| 1788 | num = get_arg(ptrdiff_t); |
| 1789 | break; |
| 1790 | case FORMAT_TYPE_UBYTE: |
| 1791 | num = get_arg(unsigned char); |
| 1792 | break; |
| 1793 | case FORMAT_TYPE_BYTE: |
| 1794 | num = get_arg(signed char); |
| 1795 | break; |
| 1796 | case FORMAT_TYPE_USHORT: |
| 1797 | num = get_arg(unsigned short); |
| 1798 | break; |
| 1799 | case FORMAT_TYPE_SHORT: |
| 1800 | num = get_arg(short); |
| 1801 | break; |
| 1802 | case FORMAT_TYPE_UINT: |
| 1803 | num = get_arg(unsigned int); |
| 1804 | break; |
| 1805 | default: |
| 1806 | num = get_arg(int); |
| 1807 | } |
| 1808 | |
| 1809 | str = number(str, end, num, spec); |
| 1810 | } /* default: */ |
| 1811 | } /* switch(spec.type) */ |
| 1812 | } /* while(*fmt) */ |
| 1813 | |
| 1814 | if (size > 0) { |
| 1815 | if (str < end) |
| 1816 | *str = '\0'; |
| 1817 | else |
| 1818 | end[-1] = '\0'; |
| 1819 | } |
| 1820 | |
| 1821 | #undef get_arg |
| 1822 | |
| 1823 | /* the trailing null byte doesn't count towards the total */ |
| 1824 | return str - buf; |
| 1825 | } |
| 1826 | EXPORT_SYMBOL_GPL(bstr_printf); |
| 1827 | |
| 1828 | /** |
| 1829 | * bprintf - Parse a format string and place args' binary value in a buffer |
| 1830 | * @bin_buf: The buffer to place args' binary value |
| 1831 | * @size: The size of the buffer(by words(32bits), not characters) |
| 1832 | * @fmt: The format string to use |
| 1833 | * @...: Arguments for the format string |
| 1834 | * |
| 1835 | * The function returns the number of words(u32) written |
| 1836 | * into @bin_buf. |
| 1837 | */ |
| 1838 | int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...) |
| 1839 | { |
| 1840 | va_list args; |
| 1841 | int ret; |
| 1842 | |
| 1843 | va_start(args, fmt); |
| 1844 | ret = vbin_printf(bin_buf, size, fmt, args); |
| 1845 | va_end(args); |
| 1846 | |
| 1847 | return ret; |
| 1848 | } |
| 1849 | EXPORT_SYMBOL_GPL(bprintf); |
| 1850 | |
| 1851 | #endif /* CONFIG_BINARY_PRINTF */ |
| 1852 | |
| 1853 | /** |
| 1854 | * vsscanf - Unformat a buffer into a list of arguments |
| 1855 | * @buf: input buffer |
| 1856 | * @fmt: format of buffer |
| 1857 | * @args: arguments |
| 1858 | */ |
| 1859 | int vsscanf(const char *buf, const char *fmt, va_list args) |
| 1860 | { |
| 1861 | const char *str = buf; |
| 1862 | char *next; |
| 1863 | char digit; |
| 1864 | int num = 0; |
| 1865 | u8 qualifier; |
| 1866 | u8 base; |
| 1867 | s16 field_width; |
| 1868 | bool is_sign; |
| 1869 | |
| 1870 | while (*fmt && *str) { |
| 1871 | /* skip any white space in format */ |
| 1872 | /* white space in format matchs any amount of |
| 1873 | * white space, including none, in the input. |
| 1874 | */ |
| 1875 | if (isspace(*fmt)) { |
| 1876 | fmt = skip_spaces(++fmt); |
| 1877 | str = skip_spaces(str); |
| 1878 | } |
| 1879 | |
| 1880 | /* anything that is not a conversion must match exactly */ |
| 1881 | if (*fmt != '%' && *fmt) { |
| 1882 | if (*fmt++ != *str++) |
| 1883 | break; |
| 1884 | continue; |
| 1885 | } |
| 1886 | |
| 1887 | if (!*fmt) |
| 1888 | break; |
| 1889 | ++fmt; |
| 1890 | |
| 1891 | /* skip this conversion. |
| 1892 | * advance both strings to next white space |
| 1893 | */ |
| 1894 | if (*fmt == '*') { |
| 1895 | while (!isspace(*fmt) && *fmt != '%' && *fmt) |
| 1896 | fmt++; |
| 1897 | while (!isspace(*str) && *str) |
| 1898 | str++; |
| 1899 | continue; |
| 1900 | } |
| 1901 | |
| 1902 | /* get field width */ |
| 1903 | field_width = -1; |
| 1904 | if (isdigit(*fmt)) |
| 1905 | field_width = skip_atoi(&fmt); |
| 1906 | |
| 1907 | /* get conversion qualifier */ |
| 1908 | qualifier = -1; |
| 1909 | if (*fmt == 'h' || _tolower(*fmt) == 'l' || |
| 1910 | _tolower(*fmt) == 'z') { |
| 1911 | qualifier = *fmt++; |
| 1912 | if (unlikely(qualifier == *fmt)) { |
| 1913 | if (qualifier == 'h') { |
| 1914 | qualifier = 'H'; |
| 1915 | fmt++; |
| 1916 | } else if (qualifier == 'l') { |
| 1917 | qualifier = 'L'; |
| 1918 | fmt++; |
| 1919 | } |
| 1920 | } |
| 1921 | } |
| 1922 | |
| 1923 | if (!*fmt || !*str) |
| 1924 | break; |
| 1925 | |
| 1926 | base = 10; |
| 1927 | is_sign = 0; |
| 1928 | |
| 1929 | switch (*fmt++) { |
| 1930 | case 'c': |
| 1931 | { |
| 1932 | char *s = (char *)va_arg(args, char*); |
| 1933 | if (field_width == -1) |
| 1934 | field_width = 1; |
| 1935 | do { |
| 1936 | *s++ = *str++; |
| 1937 | } while (--field_width > 0 && *str); |
| 1938 | num++; |
| 1939 | } |
| 1940 | continue; |
| 1941 | case 's': |
| 1942 | { |
| 1943 | char *s = (char *)va_arg(args, char *); |
| 1944 | if (field_width == -1) |
| 1945 | field_width = SHRT_MAX; |
| 1946 | /* first, skip leading white space in buffer */ |
| 1947 | str = skip_spaces(str); |
| 1948 | |
| 1949 | /* now copy until next white space */ |
| 1950 | while (*str && !isspace(*str) && field_width--) |
| 1951 | *s++ = *str++; |
| 1952 | *s = '\0'; |
| 1953 | num++; |
| 1954 | } |
| 1955 | continue; |
| 1956 | case 'n': |
| 1957 | /* return number of characters read so far */ |
| 1958 | { |
| 1959 | int *i = (int *)va_arg(args, int*); |
| 1960 | *i = str - buf; |
| 1961 | } |
| 1962 | continue; |
| 1963 | case 'o': |
| 1964 | base = 8; |
| 1965 | break; |
| 1966 | case 'x': |
| 1967 | case 'X': |
| 1968 | base = 16; |
| 1969 | break; |
| 1970 | case 'i': |
| 1971 | base = 0; |
| 1972 | case 'd': |
| 1973 | is_sign = 1; |
| 1974 | case 'u': |
| 1975 | break; |
| 1976 | case '%': |
| 1977 | /* looking for '%' in str */ |
| 1978 | if (*str++ != '%') |
| 1979 | return num; |
| 1980 | continue; |
| 1981 | default: |
| 1982 | /* invalid format; stop here */ |
| 1983 | return num; |
| 1984 | } |
| 1985 | |
| 1986 | /* have some sort of integer conversion. |
| 1987 | * first, skip white space in buffer. |
| 1988 | */ |
| 1989 | str = skip_spaces(str); |
| 1990 | |
| 1991 | digit = *str; |
| 1992 | if (is_sign && digit == '-') |
| 1993 | digit = *(str + 1); |
| 1994 | |
| 1995 | if (!digit |
| 1996 | || (base == 16 && !isxdigit(digit)) |
| 1997 | || (base == 10 && !isdigit(digit)) |
| 1998 | || (base == 8 && (!isdigit(digit) || digit > '7')) |
| 1999 | || (base == 0 && !isdigit(digit))) |
| 2000 | break; |
| 2001 | |
| 2002 | switch (qualifier) { |
| 2003 | case 'H': /* that's 'hh' in format */ |
| 2004 | if (is_sign) { |
| 2005 | signed char *s = (signed char *)va_arg(args, signed char *); |
| 2006 | *s = (signed char)simple_strtol(str, &next, base); |
| 2007 | } else { |
| 2008 | unsigned char *s = (unsigned char *)va_arg(args, unsigned char *); |
| 2009 | *s = (unsigned char)simple_strtoul(str, &next, base); |
| 2010 | } |
| 2011 | break; |
| 2012 | case 'h': |
| 2013 | if (is_sign) { |
| 2014 | short *s = (short *)va_arg(args, short *); |
| 2015 | *s = (short)simple_strtol(str, &next, base); |
| 2016 | } else { |
| 2017 | unsigned short *s = (unsigned short *)va_arg(args, unsigned short *); |
| 2018 | *s = (unsigned short)simple_strtoul(str, &next, base); |
| 2019 | } |
| 2020 | break; |
| 2021 | case 'l': |
| 2022 | if (is_sign) { |
| 2023 | long *l = (long *)va_arg(args, long *); |
| 2024 | *l = simple_strtol(str, &next, base); |
| 2025 | } else { |
| 2026 | unsigned long *l = (unsigned long *)va_arg(args, unsigned long *); |
| 2027 | *l = simple_strtoul(str, &next, base); |
| 2028 | } |
| 2029 | break; |
| 2030 | case 'L': |
| 2031 | if (is_sign) { |
| 2032 | long long *l = (long long *)va_arg(args, long long *); |
| 2033 | *l = simple_strtoll(str, &next, base); |
| 2034 | } else { |
| 2035 | unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *); |
| 2036 | *l = simple_strtoull(str, &next, base); |
| 2037 | } |
| 2038 | break; |
| 2039 | case 'Z': |
| 2040 | case 'z': |
| 2041 | { |
| 2042 | size_t *s = (size_t *)va_arg(args, size_t *); |
| 2043 | *s = (size_t)simple_strtoul(str, &next, base); |
| 2044 | } |
| 2045 | break; |
| 2046 | default: |
| 2047 | if (is_sign) { |
| 2048 | int *i = (int *)va_arg(args, int *); |
| 2049 | *i = (int)simple_strtol(str, &next, base); |
| 2050 | } else { |
| 2051 | unsigned int *i = (unsigned int *)va_arg(args, unsigned int*); |
| 2052 | *i = (unsigned int)simple_strtoul(str, &next, base); |
| 2053 | } |
| 2054 | break; |
| 2055 | } |
| 2056 | num++; |
| 2057 | |
| 2058 | if (!next) |
| 2059 | break; |
| 2060 | str = next; |
| 2061 | } |
| 2062 | |
| 2063 | /* |
| 2064 | * Now we've come all the way through so either the input string or the |
| 2065 | * format ended. In the former case, there can be a %n at the current |
| 2066 | * position in the format that needs to be filled. |
| 2067 | */ |
| 2068 | if (*fmt == '%' && *(fmt + 1) == 'n') { |
| 2069 | int *p = (int *)va_arg(args, int *); |
| 2070 | *p = str - buf; |
| 2071 | } |
| 2072 | |
| 2073 | return num; |
| 2074 | } |
| 2075 | EXPORT_SYMBOL(vsscanf); |
| 2076 | |
| 2077 | /** |
| 2078 | * sscanf - Unformat a buffer into a list of arguments |
| 2079 | * @buf: input buffer |
| 2080 | * @fmt: formatting of buffer |
| 2081 | * @...: resulting arguments |
| 2082 | */ |
| 2083 | int sscanf(const char *buf, const char *fmt, ...) |
| 2084 | { |
| 2085 | va_list args; |
| 2086 | int i; |
| 2087 | |
| 2088 | va_start(args, fmt); |
| 2089 | i = vsscanf(buf, fmt, args); |
| 2090 | va_end(args); |
| 2091 | |
| 2092 | return i; |
| 2093 | } |
| 2094 | EXPORT_SYMBOL(sscanf); |