vsprintf.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  1. /*
  2. * linux/lib/vsprintf.c
  3. *
  4. * Copyright (C) 1991, 1992 Linus Torvalds
  5. */
  6. /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
  7. /*
  8. * Wirzenius wrote this portably, Torvalds fucked it up :-)
  9. */
  10. /*
  11. * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
  12. * - changed to provide snprintf and vsnprintf functions
  13. * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
  14. * - scnprintf and vscnprintf
  15. */
  16. #include <stdarg.h>
  17. #include <linux/module.h>
  18. #include <linux/types.h>
  19. #include <linux/string.h>
  20. #include <linux/ctype.h>
  21. #include <linux/kernel.h>
  22. #include <linux/kallsyms.h>
  23. #include <linux/uaccess.h>
  24. #include <linux/ioport.h>
  25. #include <asm/page.h> /* for PAGE_SIZE */
  26. #include <asm/div64.h>
  27. #include <asm/sections.h> /* for dereference_function_descriptor() */
  28. /* Works only for digits and letters, but small and fast */
  29. #define TOLOWER(x) ((x) | 0x20)
  30. static unsigned int simple_guess_base(const char *cp)
  31. {
  32. if (cp[0] == '0') {
  33. if (TOLOWER(cp[1]) == 'x' && isxdigit(cp[2]))
  34. return 16;
  35. else
  36. return 8;
  37. } else {
  38. return 10;
  39. }
  40. }
  41. /**
  42. * simple_strtoul - convert a string to an unsigned long
  43. * @cp: The start of the string
  44. * @endp: A pointer to the end of the parsed string will be placed here
  45. * @base: The number base to use
  46. */
  47. unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
  48. {
  49. unsigned long result = 0;
  50. if (!base)
  51. base = simple_guess_base(cp);
  52. if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
  53. cp += 2;
  54. while (isxdigit(*cp)) {
  55. unsigned int value;
  56. value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
  57. if (value >= base)
  58. break;
  59. result = result * base + value;
  60. cp++;
  61. }
  62. if (endp)
  63. *endp = (char *)cp;
  64. return result;
  65. }
  66. EXPORT_SYMBOL(simple_strtoul);
  67. /**
  68. * simple_strtol - convert a string to a signed long
  69. * @cp: The start of the string
  70. * @endp: A pointer to the end of the parsed string will be placed here
  71. * @base: The number base to use
  72. */
  73. long simple_strtol(const char *cp, char **endp, unsigned int base)
  74. {
  75. if(*cp == '-')
  76. return -simple_strtoul(cp + 1, endp, base);
  77. return simple_strtoul(cp, endp, base);
  78. }
  79. EXPORT_SYMBOL(simple_strtol);
  80. /**
  81. * simple_strtoull - convert a string to an unsigned long long
  82. * @cp: The start of the string
  83. * @endp: A pointer to the end of the parsed string will be placed here
  84. * @base: The number base to use
  85. */
  86. unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
  87. {
  88. unsigned long long result = 0;
  89. if (!base)
  90. base = simple_guess_base(cp);
  91. if (base == 16 && cp[0] == '0' && TOLOWER(cp[1]) == 'x')
  92. cp += 2;
  93. while (isxdigit(*cp)) {
  94. unsigned int value;
  95. value = isdigit(*cp) ? *cp - '0' : TOLOWER(*cp) - 'a' + 10;
  96. if (value >= base)
  97. break;
  98. result = result * base + value;
  99. cp++;
  100. }
  101. if (endp)
  102. *endp = (char *)cp;
  103. return result;
  104. }
  105. EXPORT_SYMBOL(simple_strtoull);
  106. /**
  107. * simple_strtoll - convert a string to a signed long long
  108. * @cp: The start of the string
  109. * @endp: A pointer to the end of the parsed string will be placed here
  110. * @base: The number base to use
  111. */
  112. long long simple_strtoll(const char *cp, char **endp, unsigned int base)
  113. {
  114. if(*cp=='-')
  115. return -simple_strtoull(cp + 1, endp, base);
  116. return simple_strtoull(cp, endp, base);
  117. }
  118. /**
  119. * strict_strtoul - convert a string to an unsigned long strictly
  120. * @cp: The string to be converted
  121. * @base: The number base to use
  122. * @res: The converted result value
  123. *
  124. * strict_strtoul converts a string to an unsigned long only if the
  125. * string is really an unsigned long string, any string containing
  126. * any invalid char at the tail will be rejected and -EINVAL is returned,
  127. * only a newline char at the tail is acceptible because people generally
  128. * change a module parameter in the following way:
  129. *
  130. * echo 1024 > /sys/module/e1000/parameters/copybreak
  131. *
  132. * echo will append a newline to the tail.
  133. *
  134. * It returns 0 if conversion is successful and *res is set to the converted
  135. * value, otherwise it returns -EINVAL and *res is set to 0.
  136. *
  137. * simple_strtoul just ignores the successive invalid characters and
  138. * return the converted value of prefix part of the string.
  139. */
  140. int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
  141. {
  142. char *tail;
  143. unsigned long val;
  144. size_t len;
  145. *res = 0;
  146. len = strlen(cp);
  147. if (len == 0)
  148. return -EINVAL;
  149. val = simple_strtoul(cp, &tail, base);
  150. if (tail == cp)
  151. return -EINVAL;
  152. if ((*tail == '\0') ||
  153. ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
  154. *res = val;
  155. return 0;
  156. }
  157. return -EINVAL;
  158. }
  159. EXPORT_SYMBOL(strict_strtoul);
  160. /**
  161. * strict_strtol - convert a string to a long strictly
  162. * @cp: The string to be converted
  163. * @base: The number base to use
  164. * @res: The converted result value
  165. *
  166. * strict_strtol is similiar to strict_strtoul, but it allows the first
  167. * character of a string is '-'.
  168. *
  169. * It returns 0 if conversion is successful and *res is set to the converted
  170. * value, otherwise it returns -EINVAL and *res is set to 0.
  171. */
  172. int strict_strtol(const char *cp, unsigned int base, long *res)
  173. {
  174. int ret;
  175. if (*cp == '-') {
  176. ret = strict_strtoul(cp + 1, base, (unsigned long *)res);
  177. if (!ret)
  178. *res = -(*res);
  179. } else {
  180. ret = strict_strtoul(cp, base, (unsigned long *)res);
  181. }
  182. return ret;
  183. }
  184. EXPORT_SYMBOL(strict_strtol);
  185. /**
  186. * strict_strtoull - convert a string to an unsigned long long strictly
  187. * @cp: The string to be converted
  188. * @base: The number base to use
  189. * @res: The converted result value
  190. *
  191. * strict_strtoull converts a string to an unsigned long long only if the
  192. * string is really an unsigned long long string, any string containing
  193. * any invalid char at the tail will be rejected and -EINVAL is returned,
  194. * only a newline char at the tail is acceptible because people generally
  195. * change a module parameter in the following way:
  196. *
  197. * echo 1024 > /sys/module/e1000/parameters/copybreak
  198. *
  199. * echo will append a newline to the tail of the string.
  200. *
  201. * It returns 0 if conversion is successful and *res is set to the converted
  202. * value, otherwise it returns -EINVAL and *res is set to 0.
  203. *
  204. * simple_strtoull just ignores the successive invalid characters and
  205. * return the converted value of prefix part of the string.
  206. */
  207. int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
  208. {
  209. char *tail;
  210. unsigned long long val;
  211. size_t len;
  212. *res = 0;
  213. len = strlen(cp);
  214. if (len == 0)
  215. return -EINVAL;
  216. val = simple_strtoull(cp, &tail, base);
  217. if (tail == cp)
  218. return -EINVAL;
  219. if ((*tail == '\0') ||
  220. ((len == (size_t)(tail - cp) + 1) && (*tail == '\n'))) {
  221. *res = val;
  222. return 0;
  223. }
  224. return -EINVAL;
  225. }
  226. EXPORT_SYMBOL(strict_strtoull);
  227. /**
  228. * strict_strtoll - convert a string to a long long strictly
  229. * @cp: The string to be converted
  230. * @base: The number base to use
  231. * @res: The converted result value
  232. *
  233. * strict_strtoll is similiar to strict_strtoull, but it allows the first
  234. * character of a string is '-'.
  235. *
  236. * It returns 0 if conversion is successful and *res is set to the converted
  237. * value, otherwise it returns -EINVAL and *res is set to 0.
  238. */
  239. int strict_strtoll(const char *cp, unsigned int base, long long *res)
  240. {
  241. int ret;
  242. if (*cp == '-') {
  243. ret = strict_strtoull(cp + 1, base, (unsigned long long *)res);
  244. if (!ret)
  245. *res = -(*res);
  246. } else {
  247. ret = strict_strtoull(cp, base, (unsigned long long *)res);
  248. }
  249. return ret;
  250. }
  251. EXPORT_SYMBOL(strict_strtoll);
  252. static int skip_atoi(const char **s)
  253. {
  254. int i=0;
  255. while (isdigit(**s))
  256. i = i*10 + *((*s)++) - '0';
  257. return i;
  258. }
  259. /* Decimal conversion is by far the most typical, and is used
  260. * for /proc and /sys data. This directly impacts e.g. top performance
  261. * with many processes running. We optimize it for speed
  262. * using code from
  263. * http://www.cs.uiowa.edu/~jones/bcd/decimal.html
  264. * (with permission from the author, Douglas W. Jones). */
  265. /* Formats correctly any integer in [0,99999].
  266. * Outputs from one to five digits depending on input.
  267. * On i386 gcc 4.1.2 -O2: ~250 bytes of code. */
  268. static char* put_dec_trunc(char *buf, unsigned q)
  269. {
  270. unsigned d3, d2, d1, d0;
  271. d1 = (q>>4) & 0xf;
  272. d2 = (q>>8) & 0xf;
  273. d3 = (q>>12);
  274. d0 = 6*(d3 + d2 + d1) + (q & 0xf);
  275. q = (d0 * 0xcd) >> 11;
  276. d0 = d0 - 10*q;
  277. *buf++ = d0 + '0'; /* least significant digit */
  278. d1 = q + 9*d3 + 5*d2 + d1;
  279. if (d1 != 0) {
  280. q = (d1 * 0xcd) >> 11;
  281. d1 = d1 - 10*q;
  282. *buf++ = d1 + '0'; /* next digit */
  283. d2 = q + 2*d2;
  284. if ((d2 != 0) || (d3 != 0)) {
  285. q = (d2 * 0xd) >> 7;
  286. d2 = d2 - 10*q;
  287. *buf++ = d2 + '0'; /* next digit */
  288. d3 = q + 4*d3;
  289. if (d3 != 0) {
  290. q = (d3 * 0xcd) >> 11;
  291. d3 = d3 - 10*q;
  292. *buf++ = d3 + '0'; /* next digit */
  293. if (q != 0)
  294. *buf++ = q + '0'; /* most sign. digit */
  295. }
  296. }
  297. }
  298. return buf;
  299. }
  300. /* Same with if's removed. Always emits five digits */
  301. static char* put_dec_full(char *buf, unsigned q)
  302. {
  303. /* BTW, if q is in [0,9999], 8-bit ints will be enough, */
  304. /* but anyway, gcc produces better code with full-sized ints */
  305. unsigned d3, d2, d1, d0;
  306. d1 = (q>>4) & 0xf;
  307. d2 = (q>>8) & 0xf;
  308. d3 = (q>>12);
  309. /* Possible ways to approx. divide by 10 */
  310. /* gcc -O2 replaces multiply with shifts and adds */
  311. // (x * 0xcd) >> 11: 11001101 - shorter code than * 0x67 (on i386)
  312. // (x * 0x67) >> 10: 1100111
  313. // (x * 0x34) >> 9: 110100 - same
  314. // (x * 0x1a) >> 8: 11010 - same
  315. // (x * 0x0d) >> 7: 1101 - same, shortest code (on i386)
  316. d0 = 6*(d3 + d2 + d1) + (q & 0xf);
  317. q = (d0 * 0xcd) >> 11;
  318. d0 = d0 - 10*q;
  319. *buf++ = d0 + '0';
  320. d1 = q + 9*d3 + 5*d2 + d1;
  321. q = (d1 * 0xcd) >> 11;
  322. d1 = d1 - 10*q;
  323. *buf++ = d1 + '0';
  324. d2 = q + 2*d2;
  325. q = (d2 * 0xd) >> 7;
  326. d2 = d2 - 10*q;
  327. *buf++ = d2 + '0';
  328. d3 = q + 4*d3;
  329. q = (d3 * 0xcd) >> 11; /* - shorter code */
  330. /* q = (d3 * 0x67) >> 10; - would also work */
  331. d3 = d3 - 10*q;
  332. *buf++ = d3 + '0';
  333. *buf++ = q + '0';
  334. return buf;
  335. }
  336. /* No inlining helps gcc to use registers better */
  337. static noinline char* put_dec(char *buf, unsigned long long num)
  338. {
  339. while (1) {
  340. unsigned rem;
  341. if (num < 100000)
  342. return put_dec_trunc(buf, num);
  343. rem = do_div(num, 100000);
  344. buf = put_dec_full(buf, rem);
  345. }
  346. }
  347. #define ZEROPAD 1 /* pad with zero */
  348. #define SIGN 2 /* unsigned/signed long */
  349. #define PLUS 4 /* show plus */
  350. #define SPACE 8 /* space if plus */
  351. #define LEFT 16 /* left justified */
  352. #define SMALL 32 /* Must be 32 == 0x20 */
  353. #define SPECIAL 64 /* 0x */
  354. enum format_type {
  355. FORMAT_TYPE_NONE, /* Just a string part */
  356. FORMAT_TYPE_WIDTH,
  357. FORMAT_TYPE_PRECISION,
  358. FORMAT_TYPE_CHAR,
  359. FORMAT_TYPE_STR,
  360. FORMAT_TYPE_PTR,
  361. FORMAT_TYPE_PERCENT_CHAR,
  362. FORMAT_TYPE_INVALID,
  363. FORMAT_TYPE_LONG_LONG,
  364. FORMAT_TYPE_ULONG,
  365. FORMAT_TYPE_LONG,
  366. FORMAT_TYPE_USHORT,
  367. FORMAT_TYPE_SHORT,
  368. FORMAT_TYPE_UINT,
  369. FORMAT_TYPE_INT,
  370. FORMAT_TYPE_NRCHARS,
  371. FORMAT_TYPE_SIZE_T,
  372. FORMAT_TYPE_PTRDIFF
  373. };
  374. struct printf_spec {
  375. enum format_type type;
  376. int flags; /* flags to number() */
  377. int field_width; /* width of output field */
  378. int base;
  379. int precision; /* # of digits/chars */
  380. int qualifier;
  381. };
  382. static char *number(char *buf, char *end, unsigned long long num,
  383. struct printf_spec spec)
  384. {
  385. /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
  386. static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
  387. char tmp[66];
  388. char sign;
  389. char locase;
  390. int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
  391. int i;
  392. /* locase = 0 or 0x20. ORing digits or letters with 'locase'
  393. * produces same digits or (maybe lowercased) letters */
  394. locase = (spec.flags & SMALL);
  395. if (spec.flags & LEFT)
  396. spec.flags &= ~ZEROPAD;
  397. sign = 0;
  398. if (spec.flags & SIGN) {
  399. if ((signed long long) num < 0) {
  400. sign = '-';
  401. num = - (signed long long) num;
  402. spec.field_width--;
  403. } else if (spec.flags & PLUS) {
  404. sign = '+';
  405. spec.field_width--;
  406. } else if (spec.flags & SPACE) {
  407. sign = ' ';
  408. spec.field_width--;
  409. }
  410. }
  411. if (need_pfx) {
  412. spec.field_width--;
  413. if (spec.base == 16)
  414. spec.field_width--;
  415. }
  416. /* generate full string in tmp[], in reverse order */
  417. i = 0;
  418. if (num == 0)
  419. tmp[i++] = '0';
  420. /* Generic code, for any base:
  421. else do {
  422. tmp[i++] = (digits[do_div(num,base)] | locase);
  423. } while (num != 0);
  424. */
  425. else if (spec.base != 10) { /* 8 or 16 */
  426. int mask = spec.base - 1;
  427. int shift = 3;
  428. if (spec.base == 16) shift = 4;
  429. do {
  430. tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
  431. num >>= shift;
  432. } while (num);
  433. } else { /* base 10 */
  434. i = put_dec(tmp, num) - tmp;
  435. }
  436. /* printing 100 using %2d gives "100", not "00" */
  437. if (i > spec.precision)
  438. spec.precision = i;
  439. /* leading space padding */
  440. spec.field_width -= spec.precision;
  441. if (!(spec.flags & (ZEROPAD+LEFT))) {
  442. while(--spec.field_width >= 0) {
  443. if (buf < end)
  444. *buf = ' ';
  445. ++buf;
  446. }
  447. }
  448. /* sign */
  449. if (sign) {
  450. if (buf < end)
  451. *buf = sign;
  452. ++buf;
  453. }
  454. /* "0x" / "0" prefix */
  455. if (need_pfx) {
  456. if (buf < end)
  457. *buf = '0';
  458. ++buf;
  459. if (spec.base == 16) {
  460. if (buf < end)
  461. *buf = ('X' | locase);
  462. ++buf;
  463. }
  464. }
  465. /* zero or space padding */
  466. if (!(spec.flags & LEFT)) {
  467. char c = (spec.flags & ZEROPAD) ? '0' : ' ';
  468. while (--spec.field_width >= 0) {
  469. if (buf < end)
  470. *buf = c;
  471. ++buf;
  472. }
  473. }
  474. /* hmm even more zero padding? */
  475. while (i <= --spec.precision) {
  476. if (buf < end)
  477. *buf = '0';
  478. ++buf;
  479. }
  480. /* actual digits of result */
  481. while (--i >= 0) {
  482. if (buf < end)
  483. *buf = tmp[i];
  484. ++buf;
  485. }
  486. /* trailing space padding */
  487. while (--spec.field_width >= 0) {
  488. if (buf < end)
  489. *buf = ' ';
  490. ++buf;
  491. }
  492. return buf;
  493. }
  494. static char *string(char *buf, char *end, char *s, struct printf_spec spec)
  495. {
  496. int len, i;
  497. if ((unsigned long)s < PAGE_SIZE)
  498. s = "<NULL>";
  499. len = strnlen(s, spec.precision);
  500. if (!(spec.flags & LEFT)) {
  501. while (len < spec.field_width--) {
  502. if (buf < end)
  503. *buf = ' ';
  504. ++buf;
  505. }
  506. }
  507. for (i = 0; i < len; ++i) {
  508. if (buf < end)
  509. *buf = *s;
  510. ++buf; ++s;
  511. }
  512. while (len < spec.field_width--) {
  513. if (buf < end)
  514. *buf = ' ';
  515. ++buf;
  516. }
  517. return buf;
  518. }
  519. static char *symbol_string(char *buf, char *end, void *ptr,
  520. struct printf_spec spec)
  521. {
  522. unsigned long value = (unsigned long) ptr;
  523. #ifdef CONFIG_KALLSYMS
  524. char sym[KSYM_SYMBOL_LEN];
  525. sprint_symbol(sym, value);
  526. return string(buf, end, sym, spec);
  527. #else
  528. spec.field_width = 2*sizeof(void *);
  529. spec.flags |= SPECIAL | SMALL | ZEROPAD;
  530. spec.base = 16;
  531. return number(buf, end, value, spec);
  532. #endif
  533. }
  534. static char *resource_string(char *buf, char *end, struct resource *res,
  535. struct printf_spec spec)
  536. {
  537. #ifndef IO_RSRC_PRINTK_SIZE
  538. #define IO_RSRC_PRINTK_SIZE 4
  539. #endif
  540. #ifndef MEM_RSRC_PRINTK_SIZE
  541. #define MEM_RSRC_PRINTK_SIZE 8
  542. #endif
  543. struct printf_spec num_spec = {
  544. .base = 16,
  545. .precision = -1,
  546. .flags = SPECIAL | SMALL | ZEROPAD,
  547. };
  548. /* room for the actual numbers, the two "0x", -, [, ] and the final zero */
  549. char sym[4*sizeof(resource_size_t) + 8];
  550. char *p = sym, *pend = sym + sizeof(sym);
  551. int size = -1;
  552. if (res->flags & IORESOURCE_IO)
  553. size = IO_RSRC_PRINTK_SIZE;
  554. else if (res->flags & IORESOURCE_MEM)
  555. size = MEM_RSRC_PRINTK_SIZE;
  556. *p++ = '[';
  557. num_spec.field_width = size;
  558. p = number(p, pend, res->start, num_spec);
  559. *p++ = '-';
  560. p = number(p, pend, res->end, num_spec);
  561. *p++ = ']';
  562. *p = 0;
  563. return string(buf, end, sym, spec);
  564. }
  565. static char *mac_address_string(char *buf, char *end, u8 *addr,
  566. struct printf_spec spec)
  567. {
  568. char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
  569. char *p = mac_addr;
  570. int i;
  571. for (i = 0; i < 6; i++) {
  572. p = pack_hex_byte(p, addr[i]);
  573. if (!(spec.flags & SPECIAL) && i != 5)
  574. *p++ = ':';
  575. }
  576. *p = '\0';
  577. spec.flags &= ~SPECIAL;
  578. return string(buf, end, mac_addr, spec);
  579. }
  580. static char *ip6_addr_string(char *buf, char *end, u8 *addr,
  581. struct printf_spec spec)
  582. {
  583. char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
  584. char *p = ip6_addr;
  585. int i;
  586. for (i = 0; i < 8; i++) {
  587. p = pack_hex_byte(p, addr[2 * i]);
  588. p = pack_hex_byte(p, addr[2 * i + 1]);
  589. if (!(spec.flags & SPECIAL) && i != 7)
  590. *p++ = ':';
  591. }
  592. *p = '\0';
  593. spec.flags &= ~SPECIAL;
  594. return string(buf, end, ip6_addr, spec);
  595. }
  596. static char *ip4_addr_string(char *buf, char *end, u8 *addr,
  597. struct printf_spec spec)
  598. {
  599. char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
  600. char temp[3]; /* hold each IP quad in reverse order */
  601. char *p = ip4_addr;
  602. int i, digits;
  603. for (i = 0; i < 4; i++) {
  604. digits = put_dec_trunc(temp, addr[i]) - temp;
  605. /* reverse the digits in the quad */
  606. while (digits--)
  607. *p++ = temp[digits];
  608. if (i != 3)
  609. *p++ = '.';
  610. }
  611. *p = '\0';
  612. spec.flags &= ~SPECIAL;
  613. return string(buf, end, ip4_addr, spec);
  614. }
  615. /*
  616. * Show a '%p' thing. A kernel extension is that the '%p' is followed
  617. * by an extra set of alphanumeric characters that are extended format
  618. * specifiers.
  619. *
  620. * Right now we handle:
  621. *
  622. * - 'F' For symbolic function descriptor pointers
  623. * - 'S' For symbolic direct pointers
  624. * - 'R' For a struct resource pointer, it prints the range of
  625. * addresses (not the name nor the flags)
  626. * - 'M' For a 6-byte MAC address, it prints the address in the
  627. * usual colon-separated hex notation
  628. * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
  629. * decimal for v4 and colon separated network-order 16 bit hex for v6)
  630. * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
  631. * currently the same
  632. *
  633. * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
  634. * function pointers are really function descriptors, which contain a
  635. * pointer to the real address.
  636. */
  637. static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
  638. struct printf_spec spec)
  639. {
  640. if (!ptr)
  641. return string(buf, end, "(null)", spec);
  642. switch (*fmt) {
  643. case 'F':
  644. ptr = dereference_function_descriptor(ptr);
  645. /* Fallthrough */
  646. case 'S':
  647. return symbol_string(buf, end, ptr, spec);
  648. case 'R':
  649. return resource_string(buf, end, ptr, spec);
  650. case 'm':
  651. spec.flags |= SPECIAL;
  652. /* Fallthrough */
  653. case 'M':
  654. return mac_address_string(buf, end, ptr, spec);
  655. case 'i':
  656. spec.flags |= SPECIAL;
  657. /* Fallthrough */
  658. case 'I':
  659. if (fmt[1] == '6')
  660. return ip6_addr_string(buf, end, ptr, spec);
  661. if (fmt[1] == '4')
  662. return ip4_addr_string(buf, end, ptr, spec);
  663. spec.flags &= ~SPECIAL;
  664. break;
  665. }
  666. spec.flags |= SMALL;
  667. if (spec.field_width == -1) {
  668. spec.field_width = 2*sizeof(void *);
  669. spec.flags |= ZEROPAD;
  670. }
  671. spec.base = 16;
  672. return number(buf, end, (unsigned long) ptr, spec);
  673. }
  674. /*
  675. * Helper function to decode printf style format.
  676. * Each call decode a token from the format and return the
  677. * number of characters read (or likely the delta where it wants
  678. * to go on the next call).
  679. * The decoded token is returned through the parameters
  680. *
  681. * 'h', 'l', or 'L' for integer fields
  682. * 'z' support added 23/7/1999 S.H.
  683. * 'z' changed to 'Z' --davidm 1/25/99
  684. * 't' added for ptrdiff_t
  685. *
  686. * @fmt: the format string
  687. * @type of the token returned
  688. * @flags: various flags such as +, -, # tokens..
  689. * @field_width: overwritten width
  690. * @base: base of the number (octal, hex, ...)
  691. * @precision: precision of a number
  692. * @qualifier: qualifier of a number (long, size_t, ...)
  693. */
  694. static int format_decode(const char *fmt, struct printf_spec *spec)
  695. {
  696. const char *start = fmt;
  697. /* we finished early by reading the field width */
  698. if (spec->type == FORMAT_TYPE_WIDTH) {
  699. if (spec->field_width < 0) {
  700. spec->field_width = -spec->field_width;
  701. spec->flags |= LEFT;
  702. }
  703. spec->type = FORMAT_TYPE_NONE;
  704. goto precision;
  705. }
  706. /* we finished early by reading the precision */
  707. if (spec->type == FORMAT_TYPE_PRECISION) {
  708. if (spec->precision < 0)
  709. spec->precision = 0;
  710. spec->type = FORMAT_TYPE_NONE;
  711. goto qualifier;
  712. }
  713. /* By default */
  714. spec->type = FORMAT_TYPE_NONE;
  715. for (; *fmt ; ++fmt) {
  716. if (*fmt == '%')
  717. break;
  718. }
  719. /* Return the current non-format string */
  720. if (fmt != start || !*fmt)
  721. return fmt - start;
  722. /* Process flags */
  723. spec->flags = 0;
  724. while (1) { /* this also skips first '%' */
  725. bool found = true;
  726. ++fmt;
  727. switch (*fmt) {
  728. case '-': spec->flags |= LEFT; break;
  729. case '+': spec->flags |= PLUS; break;
  730. case ' ': spec->flags |= SPACE; break;
  731. case '#': spec->flags |= SPECIAL; break;
  732. case '0': spec->flags |= ZEROPAD; break;
  733. default: found = false;
  734. }
  735. if (!found)
  736. break;
  737. }
  738. /* get field width */
  739. spec->field_width = -1;
  740. if (isdigit(*fmt))
  741. spec->field_width = skip_atoi(&fmt);
  742. else if (*fmt == '*') {
  743. /* it's the next argument */
  744. spec->type = FORMAT_TYPE_WIDTH;
  745. return ++fmt - start;
  746. }
  747. precision:
  748. /* get the precision */
  749. spec->precision = -1;
  750. if (*fmt == '.') {
  751. ++fmt;
  752. if (isdigit(*fmt)) {
  753. spec->precision = skip_atoi(&fmt);
  754. if (spec->precision < 0)
  755. spec->precision = 0;
  756. } else if (*fmt == '*') {
  757. /* it's the next argument */
  758. spec->type = FORMAT_TYPE_PRECISION;
  759. return ++fmt - start;
  760. }
  761. }
  762. qualifier:
  763. /* get the conversion qualifier */
  764. spec->qualifier = -1;
  765. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  766. *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
  767. spec->qualifier = *fmt;
  768. ++fmt;
  769. if (spec->qualifier == 'l' && *fmt == 'l') {
  770. spec->qualifier = 'L';
  771. ++fmt;
  772. }
  773. }
  774. /* default base */
  775. spec->base = 10;
  776. switch (*fmt) {
  777. case 'c':
  778. spec->type = FORMAT_TYPE_CHAR;
  779. return ++fmt - start;
  780. case 's':
  781. spec->type = FORMAT_TYPE_STR;
  782. return ++fmt - start;
  783. case 'p':
  784. spec->type = FORMAT_TYPE_PTR;
  785. return fmt - start;
  786. /* skip alnum */
  787. case 'n':
  788. spec->type = FORMAT_TYPE_NRCHARS;
  789. return ++fmt - start;
  790. case '%':
  791. spec->type = FORMAT_TYPE_PERCENT_CHAR;
  792. return ++fmt - start;
  793. /* integer number formats - set up the flags and "break" */
  794. case 'o':
  795. spec->base = 8;
  796. break;
  797. case 'x':
  798. spec->flags |= SMALL;
  799. case 'X':
  800. spec->base = 16;
  801. break;
  802. case 'd':
  803. case 'i':
  804. spec->flags |= SIGN;
  805. case 'u':
  806. break;
  807. default:
  808. spec->type = FORMAT_TYPE_INVALID;
  809. return fmt - start;
  810. }
  811. if (spec->qualifier == 'L')
  812. spec->type = FORMAT_TYPE_LONG_LONG;
  813. else if (spec->qualifier == 'l') {
  814. if (spec->flags & SIGN)
  815. spec->type = FORMAT_TYPE_LONG;
  816. else
  817. spec->type = FORMAT_TYPE_ULONG;
  818. } else if (spec->qualifier == 'Z' || spec->qualifier == 'z') {
  819. spec->type = FORMAT_TYPE_SIZE_T;
  820. } else if (spec->qualifier == 't') {
  821. spec->type = FORMAT_TYPE_PTRDIFF;
  822. } else if (spec->qualifier == 'h') {
  823. if (spec->flags & SIGN)
  824. spec->type = FORMAT_TYPE_SHORT;
  825. else
  826. spec->type = FORMAT_TYPE_USHORT;
  827. } else {
  828. if (spec->flags & SIGN)
  829. spec->type = FORMAT_TYPE_INT;
  830. else
  831. spec->type = FORMAT_TYPE_UINT;
  832. }
  833. return ++fmt - start;
  834. }
  835. /**
  836. * vsnprintf - Format a string and place it in a buffer
  837. * @buf: The buffer to place the result into
  838. * @size: The size of the buffer, including the trailing null space
  839. * @fmt: The format string to use
  840. * @args: Arguments for the format string
  841. *
  842. * This function follows C99 vsnprintf, but has some extensions:
  843. * %pS output the name of a text symbol
  844. * %pF output the name of a function pointer
  845. * %pR output the address range in a struct resource
  846. *
  847. * The return value is the number of characters which would
  848. * be generated for the given input, excluding the trailing
  849. * '\0', as per ISO C99. If you want to have the exact
  850. * number of characters written into @buf as return value
  851. * (not including the trailing '\0'), use vscnprintf(). If the
  852. * return is greater than or equal to @size, the resulting
  853. * string is truncated.
  854. *
  855. * Call this function if you are already dealing with a va_list.
  856. * You probably want snprintf() instead.
  857. */
  858. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  859. {
  860. unsigned long long num;
  861. char *str, *end, c;
  862. int read;
  863. struct printf_spec spec = {0};
  864. /* Reject out-of-range values early. Large positive sizes are
  865. used for unknown buffer sizes. */
  866. if (unlikely((int) size < 0)) {
  867. /* There can be only one.. */
  868. static char warn = 1;
  869. WARN_ON(warn);
  870. warn = 0;
  871. return 0;
  872. }
  873. str = buf;
  874. end = buf + size;
  875. /* Make sure end is always >= buf */
  876. if (end < buf) {
  877. end = ((void *)-1);
  878. size = end - buf;
  879. }
  880. while (*fmt) {
  881. const char *old_fmt = fmt;
  882. read = format_decode(fmt, &spec);
  883. fmt += read;
  884. switch (spec.type) {
  885. case FORMAT_TYPE_NONE: {
  886. int copy = read;
  887. if (str < end) {
  888. if (copy > end - str)
  889. copy = end - str;
  890. memcpy(str, old_fmt, copy);
  891. }
  892. str += read;
  893. break;
  894. }
  895. case FORMAT_TYPE_WIDTH:
  896. spec.field_width = va_arg(args, int);
  897. break;
  898. case FORMAT_TYPE_PRECISION:
  899. spec.precision = va_arg(args, int);
  900. break;
  901. case FORMAT_TYPE_CHAR:
  902. if (!(spec.flags & LEFT)) {
  903. while (--spec.field_width > 0) {
  904. if (str < end)
  905. *str = ' ';
  906. ++str;
  907. }
  908. }
  909. c = (unsigned char) va_arg(args, int);
  910. if (str < end)
  911. *str = c;
  912. ++str;
  913. while (--spec.field_width > 0) {
  914. if (str < end)
  915. *str = ' ';
  916. ++str;
  917. }
  918. break;
  919. case FORMAT_TYPE_STR:
  920. str = string(str, end, va_arg(args, char *), spec);
  921. break;
  922. case FORMAT_TYPE_PTR:
  923. str = pointer(fmt+1, str, end, va_arg(args, void *),
  924. spec);
  925. while (isalnum(*fmt))
  926. fmt++;
  927. break;
  928. case FORMAT_TYPE_PERCENT_CHAR:
  929. if (str < end)
  930. *str = '%';
  931. ++str;
  932. break;
  933. case FORMAT_TYPE_INVALID:
  934. if (str < end)
  935. *str = '%';
  936. ++str;
  937. break;
  938. case FORMAT_TYPE_NRCHARS: {
  939. int qualifier = spec.qualifier;
  940. if (qualifier == 'l') {
  941. long *ip = va_arg(args, long *);
  942. *ip = (str - buf);
  943. } else if (qualifier == 'Z' ||
  944. qualifier == 'z') {
  945. size_t *ip = va_arg(args, size_t *);
  946. *ip = (str - buf);
  947. } else {
  948. int *ip = va_arg(args, int *);
  949. *ip = (str - buf);
  950. }
  951. break;
  952. }
  953. default:
  954. switch (spec.type) {
  955. case FORMAT_TYPE_LONG_LONG:
  956. num = va_arg(args, long long);
  957. break;
  958. case FORMAT_TYPE_ULONG:
  959. num = va_arg(args, unsigned long);
  960. break;
  961. case FORMAT_TYPE_LONG:
  962. num = va_arg(args, long);
  963. break;
  964. case FORMAT_TYPE_SIZE_T:
  965. num = va_arg(args, size_t);
  966. break;
  967. case FORMAT_TYPE_PTRDIFF:
  968. num = va_arg(args, ptrdiff_t);
  969. break;
  970. case FORMAT_TYPE_USHORT:
  971. num = (unsigned short) va_arg(args, int);
  972. break;
  973. case FORMAT_TYPE_SHORT:
  974. num = (short) va_arg(args, int);
  975. break;
  976. case FORMAT_TYPE_INT:
  977. num = (int) va_arg(args, int);
  978. break;
  979. default:
  980. num = va_arg(args, unsigned int);
  981. }
  982. str = number(str, end, num, spec);
  983. }
  984. }
  985. if (size > 0) {
  986. if (str < end)
  987. *str = '\0';
  988. else
  989. end[-1] = '\0';
  990. }
  991. /* the trailing null byte doesn't count towards the total */
  992. return str-buf;
  993. }
  994. EXPORT_SYMBOL(vsnprintf);
  995. /**
  996. * vscnprintf - Format a string and place it in a buffer
  997. * @buf: The buffer to place the result into
  998. * @size: The size of the buffer, including the trailing null space
  999. * @fmt: The format string to use
  1000. * @args: Arguments for the format string
  1001. *
  1002. * The return value is the number of characters which have been written into
  1003. * the @buf not including the trailing '\0'. If @size is <= 0 the function
  1004. * returns 0.
  1005. *
  1006. * Call this function if you are already dealing with a va_list.
  1007. * You probably want scnprintf() instead.
  1008. *
  1009. * See the vsnprintf() documentation for format string extensions over C99.
  1010. */
  1011. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  1012. {
  1013. int i;
  1014. i=vsnprintf(buf,size,fmt,args);
  1015. return (i >= size) ? (size - 1) : i;
  1016. }
  1017. EXPORT_SYMBOL(vscnprintf);
  1018. /**
  1019. * snprintf - Format a string and place it in a buffer
  1020. * @buf: The buffer to place the result into
  1021. * @size: The size of the buffer, including the trailing null space
  1022. * @fmt: The format string to use
  1023. * @...: Arguments for the format string
  1024. *
  1025. * The return value is the number of characters which would be
  1026. * generated for the given input, excluding the trailing null,
  1027. * as per ISO C99. If the return is greater than or equal to
  1028. * @size, the resulting string is truncated.
  1029. *
  1030. * See the vsnprintf() documentation for format string extensions over C99.
  1031. */
  1032. int snprintf(char * buf, size_t size, const char *fmt, ...)
  1033. {
  1034. va_list args;
  1035. int i;
  1036. va_start(args, fmt);
  1037. i=vsnprintf(buf,size,fmt,args);
  1038. va_end(args);
  1039. return i;
  1040. }
  1041. EXPORT_SYMBOL(snprintf);
  1042. /**
  1043. * scnprintf - Format a string and place it in a buffer
  1044. * @buf: The buffer to place the result into
  1045. * @size: The size of the buffer, including the trailing null space
  1046. * @fmt: The format string to use
  1047. * @...: Arguments for the format string
  1048. *
  1049. * The return value is the number of characters written into @buf not including
  1050. * the trailing '\0'. If @size is <= 0 the function returns 0.
  1051. */
  1052. int scnprintf(char * buf, size_t size, const char *fmt, ...)
  1053. {
  1054. va_list args;
  1055. int i;
  1056. va_start(args, fmt);
  1057. i = vsnprintf(buf, size, fmt, args);
  1058. va_end(args);
  1059. return (i >= size) ? (size - 1) : i;
  1060. }
  1061. EXPORT_SYMBOL(scnprintf);
  1062. /**
  1063. * vsprintf - Format a string and place it in a buffer
  1064. * @buf: The buffer to place the result into
  1065. * @fmt: The format string to use
  1066. * @args: Arguments for the format string
  1067. *
  1068. * The function returns the number of characters written
  1069. * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
  1070. * buffer overflows.
  1071. *
  1072. * Call this function if you are already dealing with a va_list.
  1073. * You probably want sprintf() instead.
  1074. *
  1075. * See the vsnprintf() documentation for format string extensions over C99.
  1076. */
  1077. int vsprintf(char *buf, const char *fmt, va_list args)
  1078. {
  1079. return vsnprintf(buf, INT_MAX, fmt, args);
  1080. }
  1081. EXPORT_SYMBOL(vsprintf);
  1082. /**
  1083. * sprintf - Format a string and place it in a buffer
  1084. * @buf: The buffer to place the result into
  1085. * @fmt: The format string to use
  1086. * @...: Arguments for the format string
  1087. *
  1088. * The function returns the number of characters written
  1089. * into @buf. Use snprintf() or scnprintf() in order to avoid
  1090. * buffer overflows.
  1091. *
  1092. * See the vsnprintf() documentation for format string extensions over C99.
  1093. */
  1094. int sprintf(char * buf, const char *fmt, ...)
  1095. {
  1096. va_list args;
  1097. int i;
  1098. va_start(args, fmt);
  1099. i=vsnprintf(buf, INT_MAX, fmt, args);
  1100. va_end(args);
  1101. return i;
  1102. }
  1103. EXPORT_SYMBOL(sprintf);
  1104. #ifdef CONFIG_BINARY_PRINTF
  1105. /*
  1106. * bprintf service:
  1107. * vbin_printf() - VA arguments to binary data
  1108. * bstr_printf() - Binary data to text string
  1109. */
  1110. /**
  1111. * vbin_printf - Parse a format string and place args' binary value in a buffer
  1112. * @bin_buf: The buffer to place args' binary value
  1113. * @size: The size of the buffer(by words(32bits), not characters)
  1114. * @fmt: The format string to use
  1115. * @args: Arguments for the format string
  1116. *
  1117. * The format follows C99 vsnprintf, except %n is ignored, and its argument
  1118. * is skiped.
  1119. *
  1120. * The return value is the number of words(32bits) which would be generated for
  1121. * the given input.
  1122. *
  1123. * NOTE:
  1124. * If the return value is greater than @size, the resulting bin_buf is NOT
  1125. * valid for bstr_printf().
  1126. */
  1127. int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
  1128. {
  1129. struct printf_spec spec = {0};
  1130. char *str, *end;
  1131. int read;
  1132. str = (char *)bin_buf;
  1133. end = (char *)(bin_buf + size);
  1134. #define save_arg(type) \
  1135. do { \
  1136. if (sizeof(type) == 8) { \
  1137. unsigned long long value; \
  1138. str = PTR_ALIGN(str, sizeof(u32)); \
  1139. value = va_arg(args, unsigned long long); \
  1140. if (str + sizeof(type) <= end) { \
  1141. *(u32 *)str = *(u32 *)&value; \
  1142. *(u32 *)(str + 4) = *((u32 *)&value + 1); \
  1143. } \
  1144. } else { \
  1145. unsigned long value; \
  1146. str = PTR_ALIGN(str, sizeof(type)); \
  1147. value = va_arg(args, int); \
  1148. if (str + sizeof(type) <= end) \
  1149. *(typeof(type) *)str = (type)value; \
  1150. } \
  1151. str += sizeof(type); \
  1152. } while (0)
  1153. while (*fmt) {
  1154. read = format_decode(fmt, &spec);
  1155. fmt += read;
  1156. switch (spec.type) {
  1157. case FORMAT_TYPE_NONE:
  1158. break;
  1159. case FORMAT_TYPE_WIDTH:
  1160. case FORMAT_TYPE_PRECISION:
  1161. save_arg(int);
  1162. break;
  1163. case FORMAT_TYPE_CHAR:
  1164. save_arg(char);
  1165. break;
  1166. case FORMAT_TYPE_STR: {
  1167. const char *save_str = va_arg(args, char *);
  1168. size_t len;
  1169. if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
  1170. || (unsigned long)save_str < PAGE_SIZE)
  1171. save_str = "<NULL>";
  1172. len = strlen(save_str);
  1173. if (str + len + 1 < end)
  1174. memcpy(str, save_str, len + 1);
  1175. str += len + 1;
  1176. break;
  1177. }
  1178. case FORMAT_TYPE_PTR:
  1179. save_arg(void *);
  1180. /* skip all alphanumeric pointer suffixes */
  1181. while (isalnum(*fmt))
  1182. fmt++;
  1183. break;
  1184. case FORMAT_TYPE_PERCENT_CHAR:
  1185. break;
  1186. case FORMAT_TYPE_INVALID:
  1187. break;
  1188. case FORMAT_TYPE_NRCHARS: {
  1189. /* skip %n 's argument */
  1190. int qualifier = spec.qualifier;
  1191. void *skip_arg;
  1192. if (qualifier == 'l')
  1193. skip_arg = va_arg(args, long *);
  1194. else if (qualifier == 'Z' || qualifier == 'z')
  1195. skip_arg = va_arg(args, size_t *);
  1196. else
  1197. skip_arg = va_arg(args, int *);
  1198. break;
  1199. }
  1200. default:
  1201. switch (spec.type) {
  1202. case FORMAT_TYPE_LONG_LONG:
  1203. save_arg(long long);
  1204. break;
  1205. case FORMAT_TYPE_ULONG:
  1206. case FORMAT_TYPE_LONG:
  1207. save_arg(unsigned long);
  1208. break;
  1209. case FORMAT_TYPE_SIZE_T:
  1210. save_arg(size_t);
  1211. break;
  1212. case FORMAT_TYPE_PTRDIFF:
  1213. save_arg(ptrdiff_t);
  1214. break;
  1215. case FORMAT_TYPE_USHORT:
  1216. case FORMAT_TYPE_SHORT:
  1217. save_arg(short);
  1218. break;
  1219. default:
  1220. save_arg(int);
  1221. }
  1222. }
  1223. }
  1224. return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
  1225. #undef save_arg
  1226. }
  1227. EXPORT_SYMBOL_GPL(vbin_printf);
  1228. /**
  1229. * bstr_printf - Format a string from binary arguments and place it in a buffer
  1230. * @buf: The buffer to place the result into
  1231. * @size: The size of the buffer, including the trailing null space
  1232. * @fmt: The format string to use
  1233. * @bin_buf: Binary arguments for the format string
  1234. *
  1235. * This function like C99 vsnprintf, but the difference is that vsnprintf gets
  1236. * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
  1237. * a binary buffer that generated by vbin_printf.
  1238. *
  1239. * The format follows C99 vsnprintf, but has some extensions:
  1240. * %pS output the name of a text symbol
  1241. * %pF output the name of a function pointer
  1242. * %pR output the address range in a struct resource
  1243. * %n is ignored
  1244. *
  1245. * The return value is the number of characters which would
  1246. * be generated for the given input, excluding the trailing
  1247. * '\0', as per ISO C99. If you want to have the exact
  1248. * number of characters written into @buf as return value
  1249. * (not including the trailing '\0'), use vscnprintf(). If the
  1250. * return is greater than or equal to @size, the resulting
  1251. * string is truncated.
  1252. */
  1253. int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
  1254. {
  1255. unsigned long long num;
  1256. char *str, *end, c;
  1257. const char *args = (const char *)bin_buf;
  1258. struct printf_spec spec = {0};
  1259. if (unlikely((int) size < 0)) {
  1260. /* There can be only one.. */
  1261. static char warn = 1;
  1262. WARN_ON(warn);
  1263. warn = 0;
  1264. return 0;
  1265. }
  1266. str = buf;
  1267. end = buf + size;
  1268. #define get_arg(type) \
  1269. ({ \
  1270. typeof(type) value; \
  1271. if (sizeof(type) == 8) { \
  1272. args = PTR_ALIGN(args, sizeof(u32)); \
  1273. *(u32 *)&value = *(u32 *)args; \
  1274. *((u32 *)&value + 1) = *(u32 *)(args + 4); \
  1275. } else { \
  1276. args = PTR_ALIGN(args, sizeof(type)); \
  1277. value = *(typeof(type) *)args; \
  1278. } \
  1279. args += sizeof(type); \
  1280. value; \
  1281. })
  1282. /* Make sure end is always >= buf */
  1283. if (end < buf) {
  1284. end = ((void *)-1);
  1285. size = end - buf;
  1286. }
  1287. while (*fmt) {
  1288. int read;
  1289. const char *old_fmt = fmt;
  1290. read = format_decode(fmt, &spec);
  1291. fmt += read;
  1292. switch (spec.type) {
  1293. case FORMAT_TYPE_NONE: {
  1294. int copy = read;
  1295. if (str < end) {
  1296. if (copy > end - str)
  1297. copy = end - str;
  1298. memcpy(str, old_fmt, copy);
  1299. }
  1300. str += read;
  1301. break;
  1302. }
  1303. case FORMAT_TYPE_WIDTH:
  1304. spec.field_width = get_arg(int);
  1305. break;
  1306. case FORMAT_TYPE_PRECISION:
  1307. spec.precision = get_arg(int);
  1308. break;
  1309. case FORMAT_TYPE_CHAR:
  1310. if (!(spec.flags & LEFT)) {
  1311. while (--spec.field_width > 0) {
  1312. if (str < end)
  1313. *str = ' ';
  1314. ++str;
  1315. }
  1316. }
  1317. c = (unsigned char) get_arg(char);
  1318. if (str < end)
  1319. *str = c;
  1320. ++str;
  1321. while (--spec.field_width > 0) {
  1322. if (str < end)
  1323. *str = ' ';
  1324. ++str;
  1325. }
  1326. break;
  1327. case FORMAT_TYPE_STR: {
  1328. const char *str_arg = args;
  1329. size_t len = strlen(str_arg);
  1330. args += len + 1;
  1331. str = string(str, end, (char *)str_arg, spec);
  1332. break;
  1333. }
  1334. case FORMAT_TYPE_PTR:
  1335. str = pointer(fmt+1, str, end, get_arg(void *), spec);
  1336. while (isalnum(*fmt))
  1337. fmt++;
  1338. break;
  1339. case FORMAT_TYPE_PERCENT_CHAR:
  1340. if (str < end)
  1341. *str = '%';
  1342. ++str;
  1343. break;
  1344. case FORMAT_TYPE_INVALID:
  1345. if (str < end)
  1346. *str = '%';
  1347. ++str;
  1348. break;
  1349. case FORMAT_TYPE_NRCHARS:
  1350. /* skip */
  1351. break;
  1352. default:
  1353. switch (spec.type) {
  1354. case FORMAT_TYPE_LONG_LONG:
  1355. num = get_arg(long long);
  1356. break;
  1357. case FORMAT_TYPE_ULONG:
  1358. num = get_arg(unsigned long);
  1359. break;
  1360. case FORMAT_TYPE_LONG:
  1361. num = get_arg(unsigned long);
  1362. break;
  1363. case FORMAT_TYPE_SIZE_T:
  1364. num = get_arg(size_t);
  1365. break;
  1366. case FORMAT_TYPE_PTRDIFF:
  1367. num = get_arg(ptrdiff_t);
  1368. break;
  1369. case FORMAT_TYPE_USHORT:
  1370. num = get_arg(unsigned short);
  1371. break;
  1372. case FORMAT_TYPE_SHORT:
  1373. num = get_arg(short);
  1374. break;
  1375. case FORMAT_TYPE_UINT:
  1376. num = get_arg(unsigned int);
  1377. break;
  1378. default:
  1379. num = get_arg(int);
  1380. }
  1381. str = number(str, end, num, spec);
  1382. }
  1383. }
  1384. if (size > 0) {
  1385. if (str < end)
  1386. *str = '\0';
  1387. else
  1388. end[-1] = '\0';
  1389. }
  1390. #undef get_arg
  1391. /* the trailing null byte doesn't count towards the total */
  1392. return str - buf;
  1393. }
  1394. EXPORT_SYMBOL_GPL(bstr_printf);
  1395. /**
  1396. * bprintf - Parse a format string and place args' binary value in a buffer
  1397. * @bin_buf: The buffer to place args' binary value
  1398. * @size: The size of the buffer(by words(32bits), not characters)
  1399. * @fmt: The format string to use
  1400. * @...: Arguments for the format string
  1401. *
  1402. * The function returns the number of words(u32) written
  1403. * into @bin_buf.
  1404. */
  1405. int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
  1406. {
  1407. va_list args;
  1408. int ret;
  1409. va_start(args, fmt);
  1410. ret = vbin_printf(bin_buf, size, fmt, args);
  1411. va_end(args);
  1412. return ret;
  1413. }
  1414. EXPORT_SYMBOL_GPL(bprintf);
  1415. #endif /* CONFIG_BINARY_PRINTF */
  1416. /**
  1417. * vsscanf - Unformat a buffer into a list of arguments
  1418. * @buf: input buffer
  1419. * @fmt: format of buffer
  1420. * @args: arguments
  1421. */
  1422. int vsscanf(const char * buf, const char * fmt, va_list args)
  1423. {
  1424. const char *str = buf;
  1425. char *next;
  1426. char digit;
  1427. int num = 0;
  1428. int qualifier;
  1429. int base;
  1430. int field_width;
  1431. int is_sign = 0;
  1432. while(*fmt && *str) {
  1433. /* skip any white space in format */
  1434. /* white space in format matchs any amount of
  1435. * white space, including none, in the input.
  1436. */
  1437. if (isspace(*fmt)) {
  1438. while (isspace(*fmt))
  1439. ++fmt;
  1440. while (isspace(*str))
  1441. ++str;
  1442. }
  1443. /* anything that is not a conversion must match exactly */
  1444. if (*fmt != '%' && *fmt) {
  1445. if (*fmt++ != *str++)
  1446. break;
  1447. continue;
  1448. }
  1449. if (!*fmt)
  1450. break;
  1451. ++fmt;
  1452. /* skip this conversion.
  1453. * advance both strings to next white space
  1454. */
  1455. if (*fmt == '*') {
  1456. while (!isspace(*fmt) && *fmt)
  1457. fmt++;
  1458. while (!isspace(*str) && *str)
  1459. str++;
  1460. continue;
  1461. }
  1462. /* get field width */
  1463. field_width = -1;
  1464. if (isdigit(*fmt))
  1465. field_width = skip_atoi(&fmt);
  1466. /* get conversion qualifier */
  1467. qualifier = -1;
  1468. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  1469. *fmt == 'Z' || *fmt == 'z') {
  1470. qualifier = *fmt++;
  1471. if (unlikely(qualifier == *fmt)) {
  1472. if (qualifier == 'h') {
  1473. qualifier = 'H';
  1474. fmt++;
  1475. } else if (qualifier == 'l') {
  1476. qualifier = 'L';
  1477. fmt++;
  1478. }
  1479. }
  1480. }
  1481. base = 10;
  1482. is_sign = 0;
  1483. if (!*fmt || !*str)
  1484. break;
  1485. switch(*fmt++) {
  1486. case 'c':
  1487. {
  1488. char *s = (char *) va_arg(args,char*);
  1489. if (field_width == -1)
  1490. field_width = 1;
  1491. do {
  1492. *s++ = *str++;
  1493. } while (--field_width > 0 && *str);
  1494. num++;
  1495. }
  1496. continue;
  1497. case 's':
  1498. {
  1499. char *s = (char *) va_arg(args, char *);
  1500. if(field_width == -1)
  1501. field_width = INT_MAX;
  1502. /* first, skip leading white space in buffer */
  1503. while (isspace(*str))
  1504. str++;
  1505. /* now copy until next white space */
  1506. while (*str && !isspace(*str) && field_width--) {
  1507. *s++ = *str++;
  1508. }
  1509. *s = '\0';
  1510. num++;
  1511. }
  1512. continue;
  1513. case 'n':
  1514. /* return number of characters read so far */
  1515. {
  1516. int *i = (int *)va_arg(args,int*);
  1517. *i = str - buf;
  1518. }
  1519. continue;
  1520. case 'o':
  1521. base = 8;
  1522. break;
  1523. case 'x':
  1524. case 'X':
  1525. base = 16;
  1526. break;
  1527. case 'i':
  1528. base = 0;
  1529. case 'd':
  1530. is_sign = 1;
  1531. case 'u':
  1532. break;
  1533. case '%':
  1534. /* looking for '%' in str */
  1535. if (*str++ != '%')
  1536. return num;
  1537. continue;
  1538. default:
  1539. /* invalid format; stop here */
  1540. return num;
  1541. }
  1542. /* have some sort of integer conversion.
  1543. * first, skip white space in buffer.
  1544. */
  1545. while (isspace(*str))
  1546. str++;
  1547. digit = *str;
  1548. if (is_sign && digit == '-')
  1549. digit = *(str + 1);
  1550. if (!digit
  1551. || (base == 16 && !isxdigit(digit))
  1552. || (base == 10 && !isdigit(digit))
  1553. || (base == 8 && (!isdigit(digit) || digit > '7'))
  1554. || (base == 0 && !isdigit(digit)))
  1555. break;
  1556. switch(qualifier) {
  1557. case 'H': /* that's 'hh' in format */
  1558. if (is_sign) {
  1559. signed char *s = (signed char *) va_arg(args,signed char *);
  1560. *s = (signed char) simple_strtol(str,&next,base);
  1561. } else {
  1562. unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
  1563. *s = (unsigned char) simple_strtoul(str, &next, base);
  1564. }
  1565. break;
  1566. case 'h':
  1567. if (is_sign) {
  1568. short *s = (short *) va_arg(args,short *);
  1569. *s = (short) simple_strtol(str,&next,base);
  1570. } else {
  1571. unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
  1572. *s = (unsigned short) simple_strtoul(str, &next, base);
  1573. }
  1574. break;
  1575. case 'l':
  1576. if (is_sign) {
  1577. long *l = (long *) va_arg(args,long *);
  1578. *l = simple_strtol(str,&next,base);
  1579. } else {
  1580. unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
  1581. *l = simple_strtoul(str,&next,base);
  1582. }
  1583. break;
  1584. case 'L':
  1585. if (is_sign) {
  1586. long long *l = (long long*) va_arg(args,long long *);
  1587. *l = simple_strtoll(str,&next,base);
  1588. } else {
  1589. unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
  1590. *l = simple_strtoull(str,&next,base);
  1591. }
  1592. break;
  1593. case 'Z':
  1594. case 'z':
  1595. {
  1596. size_t *s = (size_t*) va_arg(args,size_t*);
  1597. *s = (size_t) simple_strtoul(str,&next,base);
  1598. }
  1599. break;
  1600. default:
  1601. if (is_sign) {
  1602. int *i = (int *) va_arg(args, int*);
  1603. *i = (int) simple_strtol(str,&next,base);
  1604. } else {
  1605. unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
  1606. *i = (unsigned int) simple_strtoul(str,&next,base);
  1607. }
  1608. break;
  1609. }
  1610. num++;
  1611. if (!next)
  1612. break;
  1613. str = next;
  1614. }
  1615. /*
  1616. * Now we've come all the way through so either the input string or the
  1617. * format ended. In the former case, there can be a %n at the current
  1618. * position in the format that needs to be filled.
  1619. */
  1620. if (*fmt == '%' && *(fmt + 1) == 'n') {
  1621. int *p = (int *)va_arg(args, int *);
  1622. *p = str - buf;
  1623. }
  1624. return num;
  1625. }
  1626. EXPORT_SYMBOL(vsscanf);
  1627. /**
  1628. * sscanf - Unformat a buffer into a list of arguments
  1629. * @buf: input buffer
  1630. * @fmt: formatting of buffer
  1631. * @...: resulting arguments
  1632. */
  1633. int sscanf(const char * buf, const char * fmt, ...)
  1634. {
  1635. va_list args;
  1636. int i;
  1637. va_start(args,fmt);
  1638. i = vsscanf(buf,fmt,args);
  1639. va_end(args);
  1640. return i;
  1641. }
  1642. EXPORT_SYMBOL(sscanf);