vsprintf.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867
  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_UBYTE,
  367. FORMAT_TYPE_BYTE,
  368. FORMAT_TYPE_USHORT,
  369. FORMAT_TYPE_SHORT,
  370. FORMAT_TYPE_UINT,
  371. FORMAT_TYPE_INT,
  372. FORMAT_TYPE_NRCHARS,
  373. FORMAT_TYPE_SIZE_T,
  374. FORMAT_TYPE_PTRDIFF
  375. };
  376. struct printf_spec {
  377. enum format_type type;
  378. int flags; /* flags to number() */
  379. int field_width; /* width of output field */
  380. int base;
  381. int precision; /* # of digits/chars */
  382. int qualifier;
  383. };
  384. static char *number(char *buf, char *end, unsigned long long num,
  385. struct printf_spec spec)
  386. {
  387. /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
  388. static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
  389. char tmp[66];
  390. char sign;
  391. char locase;
  392. int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
  393. int i;
  394. /* locase = 0 or 0x20. ORing digits or letters with 'locase'
  395. * produces same digits or (maybe lowercased) letters */
  396. locase = (spec.flags & SMALL);
  397. if (spec.flags & LEFT)
  398. spec.flags &= ~ZEROPAD;
  399. sign = 0;
  400. if (spec.flags & SIGN) {
  401. if ((signed long long) num < 0) {
  402. sign = '-';
  403. num = - (signed long long) num;
  404. spec.field_width--;
  405. } else if (spec.flags & PLUS) {
  406. sign = '+';
  407. spec.field_width--;
  408. } else if (spec.flags & SPACE) {
  409. sign = ' ';
  410. spec.field_width--;
  411. }
  412. }
  413. if (need_pfx) {
  414. spec.field_width--;
  415. if (spec.base == 16)
  416. spec.field_width--;
  417. }
  418. /* generate full string in tmp[], in reverse order */
  419. i = 0;
  420. if (num == 0)
  421. tmp[i++] = '0';
  422. /* Generic code, for any base:
  423. else do {
  424. tmp[i++] = (digits[do_div(num,base)] | locase);
  425. } while (num != 0);
  426. */
  427. else if (spec.base != 10) { /* 8 or 16 */
  428. int mask = spec.base - 1;
  429. int shift = 3;
  430. if (spec.base == 16) shift = 4;
  431. do {
  432. tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
  433. num >>= shift;
  434. } while (num);
  435. } else { /* base 10 */
  436. i = put_dec(tmp, num) - tmp;
  437. }
  438. /* printing 100 using %2d gives "100", not "00" */
  439. if (i > spec.precision)
  440. spec.precision = i;
  441. /* leading space padding */
  442. spec.field_width -= spec.precision;
  443. if (!(spec.flags & (ZEROPAD+LEFT))) {
  444. while(--spec.field_width >= 0) {
  445. if (buf < end)
  446. *buf = ' ';
  447. ++buf;
  448. }
  449. }
  450. /* sign */
  451. if (sign) {
  452. if (buf < end)
  453. *buf = sign;
  454. ++buf;
  455. }
  456. /* "0x" / "0" prefix */
  457. if (need_pfx) {
  458. if (buf < end)
  459. *buf = '0';
  460. ++buf;
  461. if (spec.base == 16) {
  462. if (buf < end)
  463. *buf = ('X' | locase);
  464. ++buf;
  465. }
  466. }
  467. /* zero or space padding */
  468. if (!(spec.flags & LEFT)) {
  469. char c = (spec.flags & ZEROPAD) ? '0' : ' ';
  470. while (--spec.field_width >= 0) {
  471. if (buf < end)
  472. *buf = c;
  473. ++buf;
  474. }
  475. }
  476. /* hmm even more zero padding? */
  477. while (i <= --spec.precision) {
  478. if (buf < end)
  479. *buf = '0';
  480. ++buf;
  481. }
  482. /* actual digits of result */
  483. while (--i >= 0) {
  484. if (buf < end)
  485. *buf = tmp[i];
  486. ++buf;
  487. }
  488. /* trailing space padding */
  489. while (--spec.field_width >= 0) {
  490. if (buf < end)
  491. *buf = ' ';
  492. ++buf;
  493. }
  494. return buf;
  495. }
  496. static char *string(char *buf, char *end, char *s, struct printf_spec spec)
  497. {
  498. int len, i;
  499. if ((unsigned long)s < PAGE_SIZE)
  500. s = "<NULL>";
  501. len = strnlen(s, spec.precision);
  502. if (!(spec.flags & LEFT)) {
  503. while (len < spec.field_width--) {
  504. if (buf < end)
  505. *buf = ' ';
  506. ++buf;
  507. }
  508. }
  509. for (i = 0; i < len; ++i) {
  510. if (buf < end)
  511. *buf = *s;
  512. ++buf; ++s;
  513. }
  514. while (len < spec.field_width--) {
  515. if (buf < end)
  516. *buf = ' ';
  517. ++buf;
  518. }
  519. return buf;
  520. }
  521. static char *symbol_string(char *buf, char *end, void *ptr,
  522. struct printf_spec spec)
  523. {
  524. unsigned long value = (unsigned long) ptr;
  525. #ifdef CONFIG_KALLSYMS
  526. char sym[KSYM_SYMBOL_LEN];
  527. sprint_symbol(sym, value);
  528. return string(buf, end, sym, spec);
  529. #else
  530. spec.field_width = 2*sizeof(void *);
  531. spec.flags |= SPECIAL | SMALL | ZEROPAD;
  532. spec.base = 16;
  533. return number(buf, end, value, spec);
  534. #endif
  535. }
  536. static char *resource_string(char *buf, char *end, struct resource *res,
  537. struct printf_spec spec)
  538. {
  539. #ifndef IO_RSRC_PRINTK_SIZE
  540. #define IO_RSRC_PRINTK_SIZE 4
  541. #endif
  542. #ifndef MEM_RSRC_PRINTK_SIZE
  543. #define MEM_RSRC_PRINTK_SIZE 8
  544. #endif
  545. struct printf_spec num_spec = {
  546. .base = 16,
  547. .precision = -1,
  548. .flags = SPECIAL | SMALL | ZEROPAD,
  549. };
  550. /* room for the actual numbers, the two "0x", -, [, ] and the final zero */
  551. char sym[4*sizeof(resource_size_t) + 8];
  552. char *p = sym, *pend = sym + sizeof(sym);
  553. int size = -1;
  554. if (res->flags & IORESOURCE_IO)
  555. size = IO_RSRC_PRINTK_SIZE;
  556. else if (res->flags & IORESOURCE_MEM)
  557. size = MEM_RSRC_PRINTK_SIZE;
  558. *p++ = '[';
  559. num_spec.field_width = size;
  560. p = number(p, pend, res->start, num_spec);
  561. *p++ = '-';
  562. p = number(p, pend, res->end, num_spec);
  563. *p++ = ']';
  564. *p = 0;
  565. return string(buf, end, sym, spec);
  566. }
  567. static char *mac_address_string(char *buf, char *end, u8 *addr,
  568. struct printf_spec spec)
  569. {
  570. char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
  571. char *p = mac_addr;
  572. int i;
  573. for (i = 0; i < 6; i++) {
  574. p = pack_hex_byte(p, addr[i]);
  575. if (!(spec.flags & SPECIAL) && i != 5)
  576. *p++ = ':';
  577. }
  578. *p = '\0';
  579. spec.flags &= ~SPECIAL;
  580. return string(buf, end, mac_addr, spec);
  581. }
  582. static char *ip6_addr_string(char *buf, char *end, u8 *addr,
  583. struct printf_spec spec)
  584. {
  585. char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
  586. char *p = ip6_addr;
  587. int i;
  588. for (i = 0; i < 8; i++) {
  589. p = pack_hex_byte(p, addr[2 * i]);
  590. p = pack_hex_byte(p, addr[2 * i + 1]);
  591. if (!(spec.flags & SPECIAL) && i != 7)
  592. *p++ = ':';
  593. }
  594. *p = '\0';
  595. spec.flags &= ~SPECIAL;
  596. return string(buf, end, ip6_addr, spec);
  597. }
  598. static char *ip4_addr_string(char *buf, char *end, u8 *addr,
  599. struct printf_spec spec)
  600. {
  601. char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
  602. char temp[3]; /* hold each IP quad in reverse order */
  603. char *p = ip4_addr;
  604. int i, digits;
  605. for (i = 0; i < 4; i++) {
  606. digits = put_dec_trunc(temp, addr[i]) - temp;
  607. /* reverse the digits in the quad */
  608. while (digits--)
  609. *p++ = temp[digits];
  610. if (i != 3)
  611. *p++ = '.';
  612. }
  613. *p = '\0';
  614. spec.flags &= ~SPECIAL;
  615. return string(buf, end, ip4_addr, spec);
  616. }
  617. /*
  618. * Show a '%p' thing. A kernel extension is that the '%p' is followed
  619. * by an extra set of alphanumeric characters that are extended format
  620. * specifiers.
  621. *
  622. * Right now we handle:
  623. *
  624. * - 'F' For symbolic function descriptor pointers
  625. * - 'S' For symbolic direct pointers
  626. * - 'R' For a struct resource pointer, it prints the range of
  627. * addresses (not the name nor the flags)
  628. * - 'M' For a 6-byte MAC address, it prints the address in the
  629. * usual colon-separated hex notation
  630. * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
  631. * decimal for v4 and colon separated network-order 16 bit hex for v6)
  632. * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
  633. * currently the same
  634. *
  635. * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
  636. * function pointers are really function descriptors, which contain a
  637. * pointer to the real address.
  638. */
  639. static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
  640. struct printf_spec spec)
  641. {
  642. if (!ptr)
  643. return string(buf, end, "(null)", spec);
  644. switch (*fmt) {
  645. case 'F':
  646. ptr = dereference_function_descriptor(ptr);
  647. /* Fallthrough */
  648. case 'S':
  649. return symbol_string(buf, end, ptr, spec);
  650. case 'R':
  651. return resource_string(buf, end, ptr, spec);
  652. case 'm':
  653. spec.flags |= SPECIAL;
  654. /* Fallthrough */
  655. case 'M':
  656. return mac_address_string(buf, end, ptr, spec);
  657. case 'i':
  658. spec.flags |= SPECIAL;
  659. /* Fallthrough */
  660. case 'I':
  661. if (fmt[1] == '6')
  662. return ip6_addr_string(buf, end, ptr, spec);
  663. if (fmt[1] == '4')
  664. return ip4_addr_string(buf, end, ptr, spec);
  665. spec.flags &= ~SPECIAL;
  666. break;
  667. }
  668. spec.flags |= SMALL;
  669. if (spec.field_width == -1) {
  670. spec.field_width = 2*sizeof(void *);
  671. spec.flags |= ZEROPAD;
  672. }
  673. spec.base = 16;
  674. return number(buf, end, (unsigned long) ptr, spec);
  675. }
  676. /*
  677. * Helper function to decode printf style format.
  678. * Each call decode a token from the format and return the
  679. * number of characters read (or likely the delta where it wants
  680. * to go on the next call).
  681. * The decoded token is returned through the parameters
  682. *
  683. * 'h', 'l', or 'L' for integer fields
  684. * 'z' support added 23/7/1999 S.H.
  685. * 'z' changed to 'Z' --davidm 1/25/99
  686. * 't' added for ptrdiff_t
  687. *
  688. * @fmt: the format string
  689. * @type of the token returned
  690. * @flags: various flags such as +, -, # tokens..
  691. * @field_width: overwritten width
  692. * @base: base of the number (octal, hex, ...)
  693. * @precision: precision of a number
  694. * @qualifier: qualifier of a number (long, size_t, ...)
  695. */
  696. static int format_decode(const char *fmt, struct printf_spec *spec)
  697. {
  698. const char *start = fmt;
  699. /* we finished early by reading the field width */
  700. if (spec->type == FORMAT_TYPE_WIDTH) {
  701. if (spec->field_width < 0) {
  702. spec->field_width = -spec->field_width;
  703. spec->flags |= LEFT;
  704. }
  705. spec->type = FORMAT_TYPE_NONE;
  706. goto precision;
  707. }
  708. /* we finished early by reading the precision */
  709. if (spec->type == FORMAT_TYPE_PRECISION) {
  710. if (spec->precision < 0)
  711. spec->precision = 0;
  712. spec->type = FORMAT_TYPE_NONE;
  713. goto qualifier;
  714. }
  715. /* By default */
  716. spec->type = FORMAT_TYPE_NONE;
  717. for (; *fmt ; ++fmt) {
  718. if (*fmt == '%')
  719. break;
  720. }
  721. /* Return the current non-format string */
  722. if (fmt != start || !*fmt)
  723. return fmt - start;
  724. /* Process flags */
  725. spec->flags = 0;
  726. while (1) { /* this also skips first '%' */
  727. bool found = true;
  728. ++fmt;
  729. switch (*fmt) {
  730. case '-': spec->flags |= LEFT; break;
  731. case '+': spec->flags |= PLUS; break;
  732. case ' ': spec->flags |= SPACE; break;
  733. case '#': spec->flags |= SPECIAL; break;
  734. case '0': spec->flags |= ZEROPAD; break;
  735. default: found = false;
  736. }
  737. if (!found)
  738. break;
  739. }
  740. /* get field width */
  741. spec->field_width = -1;
  742. if (isdigit(*fmt))
  743. spec->field_width = skip_atoi(&fmt);
  744. else if (*fmt == '*') {
  745. /* it's the next argument */
  746. spec->type = FORMAT_TYPE_WIDTH;
  747. return ++fmt - start;
  748. }
  749. precision:
  750. /* get the precision */
  751. spec->precision = -1;
  752. if (*fmt == '.') {
  753. ++fmt;
  754. if (isdigit(*fmt)) {
  755. spec->precision = skip_atoi(&fmt);
  756. if (spec->precision < 0)
  757. spec->precision = 0;
  758. } else if (*fmt == '*') {
  759. /* it's the next argument */
  760. spec->type = FORMAT_TYPE_PRECISION;
  761. return ++fmt - start;
  762. }
  763. }
  764. qualifier:
  765. /* get the conversion qualifier */
  766. spec->qualifier = -1;
  767. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  768. *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
  769. spec->qualifier = *fmt++;
  770. if (unlikely(spec->qualifier == *fmt)) {
  771. if (spec->qualifier == 'l') {
  772. spec->qualifier = 'L';
  773. ++fmt;
  774. } else if (spec->qualifier == 'h') {
  775. spec->qualifier = 'H';
  776. ++fmt;
  777. }
  778. }
  779. }
  780. /* default base */
  781. spec->base = 10;
  782. switch (*fmt) {
  783. case 'c':
  784. spec->type = FORMAT_TYPE_CHAR;
  785. return ++fmt - start;
  786. case 's':
  787. spec->type = FORMAT_TYPE_STR;
  788. return ++fmt - start;
  789. case 'p':
  790. spec->type = FORMAT_TYPE_PTR;
  791. return fmt - start;
  792. /* skip alnum */
  793. case 'n':
  794. spec->type = FORMAT_TYPE_NRCHARS;
  795. return ++fmt - start;
  796. case '%':
  797. spec->type = FORMAT_TYPE_PERCENT_CHAR;
  798. return ++fmt - start;
  799. /* integer number formats - set up the flags and "break" */
  800. case 'o':
  801. spec->base = 8;
  802. break;
  803. case 'x':
  804. spec->flags |= SMALL;
  805. case 'X':
  806. spec->base = 16;
  807. break;
  808. case 'd':
  809. case 'i':
  810. spec->flags |= SIGN;
  811. case 'u':
  812. break;
  813. default:
  814. spec->type = FORMAT_TYPE_INVALID;
  815. return fmt - start;
  816. }
  817. if (spec->qualifier == 'L')
  818. spec->type = FORMAT_TYPE_LONG_LONG;
  819. else if (spec->qualifier == 'l') {
  820. if (spec->flags & SIGN)
  821. spec->type = FORMAT_TYPE_LONG;
  822. else
  823. spec->type = FORMAT_TYPE_ULONG;
  824. } else if (spec->qualifier == 'Z' || spec->qualifier == 'z') {
  825. spec->type = FORMAT_TYPE_SIZE_T;
  826. } else if (spec->qualifier == 't') {
  827. spec->type = FORMAT_TYPE_PTRDIFF;
  828. } else if (spec->qualifier == 'H') {
  829. if (spec->flags & SIGN)
  830. spec->type = FORMAT_TYPE_BYTE;
  831. else
  832. spec->type = FORMAT_TYPE_UBYTE;
  833. } else if (spec->qualifier == 'h') {
  834. if (spec->flags & SIGN)
  835. spec->type = FORMAT_TYPE_SHORT;
  836. else
  837. spec->type = FORMAT_TYPE_USHORT;
  838. } else {
  839. if (spec->flags & SIGN)
  840. spec->type = FORMAT_TYPE_INT;
  841. else
  842. spec->type = FORMAT_TYPE_UINT;
  843. }
  844. return ++fmt - start;
  845. }
  846. /**
  847. * vsnprintf - Format a string and place it in a buffer
  848. * @buf: The buffer to place the result into
  849. * @size: The size of the buffer, including the trailing null space
  850. * @fmt: The format string to use
  851. * @args: Arguments for the format string
  852. *
  853. * This function follows C99 vsnprintf, but has some extensions:
  854. * %pS output the name of a text symbol
  855. * %pF output the name of a function pointer
  856. * %pR output the address range in a struct resource
  857. *
  858. * The return value is the number of characters which would
  859. * be generated for the given input, excluding the trailing
  860. * '\0', as per ISO C99. If you want to have the exact
  861. * number of characters written into @buf as return value
  862. * (not including the trailing '\0'), use vscnprintf(). If the
  863. * return is greater than or equal to @size, the resulting
  864. * string is truncated.
  865. *
  866. * Call this function if you are already dealing with a va_list.
  867. * You probably want snprintf() instead.
  868. */
  869. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  870. {
  871. unsigned long long num;
  872. char *str, *end, c;
  873. int read;
  874. struct printf_spec spec = {0};
  875. /* Reject out-of-range values early. Large positive sizes are
  876. used for unknown buffer sizes. */
  877. if (unlikely((int) size < 0)) {
  878. /* There can be only one.. */
  879. static char warn = 1;
  880. WARN_ON(warn);
  881. warn = 0;
  882. return 0;
  883. }
  884. str = buf;
  885. end = buf + size;
  886. /* Make sure end is always >= buf */
  887. if (end < buf) {
  888. end = ((void *)-1);
  889. size = end - buf;
  890. }
  891. while (*fmt) {
  892. const char *old_fmt = fmt;
  893. read = format_decode(fmt, &spec);
  894. fmt += read;
  895. switch (spec.type) {
  896. case FORMAT_TYPE_NONE: {
  897. int copy = read;
  898. if (str < end) {
  899. if (copy > end - str)
  900. copy = end - str;
  901. memcpy(str, old_fmt, copy);
  902. }
  903. str += read;
  904. break;
  905. }
  906. case FORMAT_TYPE_WIDTH:
  907. spec.field_width = va_arg(args, int);
  908. break;
  909. case FORMAT_TYPE_PRECISION:
  910. spec.precision = va_arg(args, int);
  911. break;
  912. case FORMAT_TYPE_CHAR:
  913. if (!(spec.flags & LEFT)) {
  914. while (--spec.field_width > 0) {
  915. if (str < end)
  916. *str = ' ';
  917. ++str;
  918. }
  919. }
  920. c = (unsigned char) va_arg(args, int);
  921. if (str < end)
  922. *str = c;
  923. ++str;
  924. while (--spec.field_width > 0) {
  925. if (str < end)
  926. *str = ' ';
  927. ++str;
  928. }
  929. break;
  930. case FORMAT_TYPE_STR:
  931. str = string(str, end, va_arg(args, char *), spec);
  932. break;
  933. case FORMAT_TYPE_PTR:
  934. str = pointer(fmt+1, str, end, va_arg(args, void *),
  935. spec);
  936. while (isalnum(*fmt))
  937. fmt++;
  938. break;
  939. case FORMAT_TYPE_PERCENT_CHAR:
  940. if (str < end)
  941. *str = '%';
  942. ++str;
  943. break;
  944. case FORMAT_TYPE_INVALID:
  945. if (str < end)
  946. *str = '%';
  947. ++str;
  948. break;
  949. case FORMAT_TYPE_NRCHARS: {
  950. int qualifier = spec.qualifier;
  951. if (qualifier == 'l') {
  952. long *ip = va_arg(args, long *);
  953. *ip = (str - buf);
  954. } else if (qualifier == 'Z' ||
  955. qualifier == 'z') {
  956. size_t *ip = va_arg(args, size_t *);
  957. *ip = (str - buf);
  958. } else {
  959. int *ip = va_arg(args, int *);
  960. *ip = (str - buf);
  961. }
  962. break;
  963. }
  964. default:
  965. switch (spec.type) {
  966. case FORMAT_TYPE_LONG_LONG:
  967. num = va_arg(args, long long);
  968. break;
  969. case FORMAT_TYPE_ULONG:
  970. num = va_arg(args, unsigned long);
  971. break;
  972. case FORMAT_TYPE_LONG:
  973. num = va_arg(args, long);
  974. break;
  975. case FORMAT_TYPE_SIZE_T:
  976. num = va_arg(args, size_t);
  977. break;
  978. case FORMAT_TYPE_PTRDIFF:
  979. num = va_arg(args, ptrdiff_t);
  980. break;
  981. case FORMAT_TYPE_UBYTE:
  982. num = (unsigned char) va_arg(args, int);
  983. break;
  984. case FORMAT_TYPE_BYTE:
  985. num = (signed char) va_arg(args, int);
  986. break;
  987. case FORMAT_TYPE_USHORT:
  988. num = (unsigned short) va_arg(args, int);
  989. break;
  990. case FORMAT_TYPE_SHORT:
  991. num = (short) va_arg(args, int);
  992. break;
  993. case FORMAT_TYPE_INT:
  994. num = (int) va_arg(args, int);
  995. break;
  996. default:
  997. num = va_arg(args, unsigned int);
  998. }
  999. str = number(str, end, num, spec);
  1000. }
  1001. }
  1002. if (size > 0) {
  1003. if (str < end)
  1004. *str = '\0';
  1005. else
  1006. end[-1] = '\0';
  1007. }
  1008. /* the trailing null byte doesn't count towards the total */
  1009. return str-buf;
  1010. }
  1011. EXPORT_SYMBOL(vsnprintf);
  1012. /**
  1013. * vscnprintf - Format a string and place it in a buffer
  1014. * @buf: The buffer to place the result into
  1015. * @size: The size of the buffer, including the trailing null space
  1016. * @fmt: The format string to use
  1017. * @args: Arguments for the format string
  1018. *
  1019. * The return value is the number of characters which have been written into
  1020. * the @buf not including the trailing '\0'. If @size is <= 0 the function
  1021. * returns 0.
  1022. *
  1023. * Call this function if you are already dealing with a va_list.
  1024. * You probably want scnprintf() instead.
  1025. *
  1026. * See the vsnprintf() documentation for format string extensions over C99.
  1027. */
  1028. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  1029. {
  1030. int i;
  1031. i=vsnprintf(buf,size,fmt,args);
  1032. return (i >= size) ? (size - 1) : i;
  1033. }
  1034. EXPORT_SYMBOL(vscnprintf);
  1035. /**
  1036. * snprintf - Format a string and place it in a buffer
  1037. * @buf: The buffer to place the result into
  1038. * @size: The size of the buffer, including the trailing null space
  1039. * @fmt: The format string to use
  1040. * @...: Arguments for the format string
  1041. *
  1042. * The return value is the number of characters which would be
  1043. * generated for the given input, excluding the trailing null,
  1044. * as per ISO C99. If the return is greater than or equal to
  1045. * @size, the resulting string is truncated.
  1046. *
  1047. * See the vsnprintf() documentation for format string extensions over C99.
  1048. */
  1049. int snprintf(char * buf, size_t size, const char *fmt, ...)
  1050. {
  1051. va_list args;
  1052. int i;
  1053. va_start(args, fmt);
  1054. i=vsnprintf(buf,size,fmt,args);
  1055. va_end(args);
  1056. return i;
  1057. }
  1058. EXPORT_SYMBOL(snprintf);
  1059. /**
  1060. * scnprintf - Format a string and place it in a buffer
  1061. * @buf: The buffer to place the result into
  1062. * @size: The size of the buffer, including the trailing null space
  1063. * @fmt: The format string to use
  1064. * @...: Arguments for the format string
  1065. *
  1066. * The return value is the number of characters written into @buf not including
  1067. * the trailing '\0'. If @size is <= 0 the function returns 0.
  1068. */
  1069. int scnprintf(char * buf, size_t size, const char *fmt, ...)
  1070. {
  1071. va_list args;
  1072. int i;
  1073. va_start(args, fmt);
  1074. i = vsnprintf(buf, size, fmt, args);
  1075. va_end(args);
  1076. return (i >= size) ? (size - 1) : i;
  1077. }
  1078. EXPORT_SYMBOL(scnprintf);
  1079. /**
  1080. * vsprintf - Format a string and place it in a buffer
  1081. * @buf: The buffer to place the result into
  1082. * @fmt: The format string to use
  1083. * @args: Arguments for the format string
  1084. *
  1085. * The function returns the number of characters written
  1086. * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
  1087. * buffer overflows.
  1088. *
  1089. * Call this function if you are already dealing with a va_list.
  1090. * You probably want sprintf() instead.
  1091. *
  1092. * See the vsnprintf() documentation for format string extensions over C99.
  1093. */
  1094. int vsprintf(char *buf, const char *fmt, va_list args)
  1095. {
  1096. return vsnprintf(buf, INT_MAX, fmt, args);
  1097. }
  1098. EXPORT_SYMBOL(vsprintf);
  1099. /**
  1100. * sprintf - Format a string and place it in a buffer
  1101. * @buf: The buffer to place the result into
  1102. * @fmt: The format string to use
  1103. * @...: Arguments for the format string
  1104. *
  1105. * The function returns the number of characters written
  1106. * into @buf. Use snprintf() or scnprintf() in order to avoid
  1107. * buffer overflows.
  1108. *
  1109. * See the vsnprintf() documentation for format string extensions over C99.
  1110. */
  1111. int sprintf(char * buf, const char *fmt, ...)
  1112. {
  1113. va_list args;
  1114. int i;
  1115. va_start(args, fmt);
  1116. i=vsnprintf(buf, INT_MAX, fmt, args);
  1117. va_end(args);
  1118. return i;
  1119. }
  1120. EXPORT_SYMBOL(sprintf);
  1121. #ifdef CONFIG_BINARY_PRINTF
  1122. /*
  1123. * bprintf service:
  1124. * vbin_printf() - VA arguments to binary data
  1125. * bstr_printf() - Binary data to text string
  1126. */
  1127. /**
  1128. * vbin_printf - Parse a format string and place args' binary value in a buffer
  1129. * @bin_buf: The buffer to place args' binary value
  1130. * @size: The size of the buffer(by words(32bits), not characters)
  1131. * @fmt: The format string to use
  1132. * @args: Arguments for the format string
  1133. *
  1134. * The format follows C99 vsnprintf, except %n is ignored, and its argument
  1135. * is skiped.
  1136. *
  1137. * The return value is the number of words(32bits) which would be generated for
  1138. * the given input.
  1139. *
  1140. * NOTE:
  1141. * If the return value is greater than @size, the resulting bin_buf is NOT
  1142. * valid for bstr_printf().
  1143. */
  1144. int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
  1145. {
  1146. struct printf_spec spec = {0};
  1147. char *str, *end;
  1148. int read;
  1149. str = (char *)bin_buf;
  1150. end = (char *)(bin_buf + size);
  1151. #define save_arg(type) \
  1152. do { \
  1153. if (sizeof(type) == 8) { \
  1154. unsigned long long value; \
  1155. str = PTR_ALIGN(str, sizeof(u32)); \
  1156. value = va_arg(args, unsigned long long); \
  1157. if (str + sizeof(type) <= end) { \
  1158. *(u32 *)str = *(u32 *)&value; \
  1159. *(u32 *)(str + 4) = *((u32 *)&value + 1); \
  1160. } \
  1161. } else { \
  1162. unsigned long value; \
  1163. str = PTR_ALIGN(str, sizeof(type)); \
  1164. value = va_arg(args, int); \
  1165. if (str + sizeof(type) <= end) \
  1166. *(typeof(type) *)str = (type)value; \
  1167. } \
  1168. str += sizeof(type); \
  1169. } while (0)
  1170. while (*fmt) {
  1171. read = format_decode(fmt, &spec);
  1172. fmt += read;
  1173. switch (spec.type) {
  1174. case FORMAT_TYPE_NONE:
  1175. break;
  1176. case FORMAT_TYPE_WIDTH:
  1177. case FORMAT_TYPE_PRECISION:
  1178. save_arg(int);
  1179. break;
  1180. case FORMAT_TYPE_CHAR:
  1181. save_arg(char);
  1182. break;
  1183. case FORMAT_TYPE_STR: {
  1184. const char *save_str = va_arg(args, char *);
  1185. size_t len;
  1186. if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
  1187. || (unsigned long)save_str < PAGE_SIZE)
  1188. save_str = "<NULL>";
  1189. len = strlen(save_str);
  1190. if (str + len + 1 < end)
  1191. memcpy(str, save_str, len + 1);
  1192. str += len + 1;
  1193. break;
  1194. }
  1195. case FORMAT_TYPE_PTR:
  1196. save_arg(void *);
  1197. /* skip all alphanumeric pointer suffixes */
  1198. while (isalnum(*fmt))
  1199. fmt++;
  1200. break;
  1201. case FORMAT_TYPE_PERCENT_CHAR:
  1202. break;
  1203. case FORMAT_TYPE_INVALID:
  1204. break;
  1205. case FORMAT_TYPE_NRCHARS: {
  1206. /* skip %n 's argument */
  1207. int qualifier = spec.qualifier;
  1208. void *skip_arg;
  1209. if (qualifier == 'l')
  1210. skip_arg = va_arg(args, long *);
  1211. else if (qualifier == 'Z' || qualifier == 'z')
  1212. skip_arg = va_arg(args, size_t *);
  1213. else
  1214. skip_arg = va_arg(args, int *);
  1215. break;
  1216. }
  1217. default:
  1218. switch (spec.type) {
  1219. case FORMAT_TYPE_LONG_LONG:
  1220. save_arg(long long);
  1221. break;
  1222. case FORMAT_TYPE_ULONG:
  1223. case FORMAT_TYPE_LONG:
  1224. save_arg(unsigned long);
  1225. break;
  1226. case FORMAT_TYPE_SIZE_T:
  1227. save_arg(size_t);
  1228. break;
  1229. case FORMAT_TYPE_PTRDIFF:
  1230. save_arg(ptrdiff_t);
  1231. break;
  1232. case FORMAT_TYPE_UBYTE:
  1233. case FORMAT_TYPE_BYTE:
  1234. save_arg(char);
  1235. break;
  1236. case FORMAT_TYPE_USHORT:
  1237. case FORMAT_TYPE_SHORT:
  1238. save_arg(short);
  1239. break;
  1240. default:
  1241. save_arg(int);
  1242. }
  1243. }
  1244. }
  1245. return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
  1246. #undef save_arg
  1247. }
  1248. EXPORT_SYMBOL_GPL(vbin_printf);
  1249. /**
  1250. * bstr_printf - Format a string from binary arguments and place it in a buffer
  1251. * @buf: The buffer to place the result into
  1252. * @size: The size of the buffer, including the trailing null space
  1253. * @fmt: The format string to use
  1254. * @bin_buf: Binary arguments for the format string
  1255. *
  1256. * This function like C99 vsnprintf, but the difference is that vsnprintf gets
  1257. * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
  1258. * a binary buffer that generated by vbin_printf.
  1259. *
  1260. * The format follows C99 vsnprintf, but has some extensions:
  1261. * %pS output the name of a text symbol
  1262. * %pF output the name of a function pointer
  1263. * %pR output the address range in a struct resource
  1264. * %n is ignored
  1265. *
  1266. * The return value is the number of characters which would
  1267. * be generated for the given input, excluding the trailing
  1268. * '\0', as per ISO C99. If you want to have the exact
  1269. * number of characters written into @buf as return value
  1270. * (not including the trailing '\0'), use vscnprintf(). If the
  1271. * return is greater than or equal to @size, the resulting
  1272. * string is truncated.
  1273. */
  1274. int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
  1275. {
  1276. unsigned long long num;
  1277. char *str, *end, c;
  1278. const char *args = (const char *)bin_buf;
  1279. struct printf_spec spec = {0};
  1280. if (unlikely((int) size < 0)) {
  1281. /* There can be only one.. */
  1282. static char warn = 1;
  1283. WARN_ON(warn);
  1284. warn = 0;
  1285. return 0;
  1286. }
  1287. str = buf;
  1288. end = buf + size;
  1289. #define get_arg(type) \
  1290. ({ \
  1291. typeof(type) value; \
  1292. if (sizeof(type) == 8) { \
  1293. args = PTR_ALIGN(args, sizeof(u32)); \
  1294. *(u32 *)&value = *(u32 *)args; \
  1295. *((u32 *)&value + 1) = *(u32 *)(args + 4); \
  1296. } else { \
  1297. args = PTR_ALIGN(args, sizeof(type)); \
  1298. value = *(typeof(type) *)args; \
  1299. } \
  1300. args += sizeof(type); \
  1301. value; \
  1302. })
  1303. /* Make sure end is always >= buf */
  1304. if (end < buf) {
  1305. end = ((void *)-1);
  1306. size = end - buf;
  1307. }
  1308. while (*fmt) {
  1309. int read;
  1310. const char *old_fmt = fmt;
  1311. read = format_decode(fmt, &spec);
  1312. fmt += read;
  1313. switch (spec.type) {
  1314. case FORMAT_TYPE_NONE: {
  1315. int copy = read;
  1316. if (str < end) {
  1317. if (copy > end - str)
  1318. copy = end - str;
  1319. memcpy(str, old_fmt, copy);
  1320. }
  1321. str += read;
  1322. break;
  1323. }
  1324. case FORMAT_TYPE_WIDTH:
  1325. spec.field_width = get_arg(int);
  1326. break;
  1327. case FORMAT_TYPE_PRECISION:
  1328. spec.precision = get_arg(int);
  1329. break;
  1330. case FORMAT_TYPE_CHAR:
  1331. if (!(spec.flags & LEFT)) {
  1332. while (--spec.field_width > 0) {
  1333. if (str < end)
  1334. *str = ' ';
  1335. ++str;
  1336. }
  1337. }
  1338. c = (unsigned char) get_arg(char);
  1339. if (str < end)
  1340. *str = c;
  1341. ++str;
  1342. while (--spec.field_width > 0) {
  1343. if (str < end)
  1344. *str = ' ';
  1345. ++str;
  1346. }
  1347. break;
  1348. case FORMAT_TYPE_STR: {
  1349. const char *str_arg = args;
  1350. size_t len = strlen(str_arg);
  1351. args += len + 1;
  1352. str = string(str, end, (char *)str_arg, spec);
  1353. break;
  1354. }
  1355. case FORMAT_TYPE_PTR:
  1356. str = pointer(fmt+1, str, end, get_arg(void *), spec);
  1357. while (isalnum(*fmt))
  1358. fmt++;
  1359. break;
  1360. case FORMAT_TYPE_PERCENT_CHAR:
  1361. if (str < end)
  1362. *str = '%';
  1363. ++str;
  1364. break;
  1365. case FORMAT_TYPE_INVALID:
  1366. if (str < end)
  1367. *str = '%';
  1368. ++str;
  1369. break;
  1370. case FORMAT_TYPE_NRCHARS:
  1371. /* skip */
  1372. break;
  1373. default:
  1374. switch (spec.type) {
  1375. case FORMAT_TYPE_LONG_LONG:
  1376. num = get_arg(long long);
  1377. break;
  1378. case FORMAT_TYPE_ULONG:
  1379. num = get_arg(unsigned long);
  1380. break;
  1381. case FORMAT_TYPE_LONG:
  1382. num = get_arg(unsigned long);
  1383. break;
  1384. case FORMAT_TYPE_SIZE_T:
  1385. num = get_arg(size_t);
  1386. break;
  1387. case FORMAT_TYPE_PTRDIFF:
  1388. num = get_arg(ptrdiff_t);
  1389. break;
  1390. case FORMAT_TYPE_UBYTE:
  1391. num = get_arg(unsigned char);
  1392. break;
  1393. case FORMAT_TYPE_BYTE:
  1394. num = get_arg(signed char);
  1395. break;
  1396. case FORMAT_TYPE_USHORT:
  1397. num = get_arg(unsigned short);
  1398. break;
  1399. case FORMAT_TYPE_SHORT:
  1400. num = get_arg(short);
  1401. break;
  1402. case FORMAT_TYPE_UINT:
  1403. num = get_arg(unsigned int);
  1404. break;
  1405. default:
  1406. num = get_arg(int);
  1407. }
  1408. str = number(str, end, num, spec);
  1409. }
  1410. }
  1411. if (size > 0) {
  1412. if (str < end)
  1413. *str = '\0';
  1414. else
  1415. end[-1] = '\0';
  1416. }
  1417. #undef get_arg
  1418. /* the trailing null byte doesn't count towards the total */
  1419. return str - buf;
  1420. }
  1421. EXPORT_SYMBOL_GPL(bstr_printf);
  1422. /**
  1423. * bprintf - Parse a format string and place args' binary value in a buffer
  1424. * @bin_buf: The buffer to place args' binary value
  1425. * @size: The size of the buffer(by words(32bits), not characters)
  1426. * @fmt: The format string to use
  1427. * @...: Arguments for the format string
  1428. *
  1429. * The function returns the number of words(u32) written
  1430. * into @bin_buf.
  1431. */
  1432. int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
  1433. {
  1434. va_list args;
  1435. int ret;
  1436. va_start(args, fmt);
  1437. ret = vbin_printf(bin_buf, size, fmt, args);
  1438. va_end(args);
  1439. return ret;
  1440. }
  1441. EXPORT_SYMBOL_GPL(bprintf);
  1442. #endif /* CONFIG_BINARY_PRINTF */
  1443. /**
  1444. * vsscanf - Unformat a buffer into a list of arguments
  1445. * @buf: input buffer
  1446. * @fmt: format of buffer
  1447. * @args: arguments
  1448. */
  1449. int vsscanf(const char * buf, const char * fmt, va_list args)
  1450. {
  1451. const char *str = buf;
  1452. char *next;
  1453. char digit;
  1454. int num = 0;
  1455. int qualifier;
  1456. int base;
  1457. int field_width;
  1458. int is_sign = 0;
  1459. while(*fmt && *str) {
  1460. /* skip any white space in format */
  1461. /* white space in format matchs any amount of
  1462. * white space, including none, in the input.
  1463. */
  1464. if (isspace(*fmt)) {
  1465. while (isspace(*fmt))
  1466. ++fmt;
  1467. while (isspace(*str))
  1468. ++str;
  1469. }
  1470. /* anything that is not a conversion must match exactly */
  1471. if (*fmt != '%' && *fmt) {
  1472. if (*fmt++ != *str++)
  1473. break;
  1474. continue;
  1475. }
  1476. if (!*fmt)
  1477. break;
  1478. ++fmt;
  1479. /* skip this conversion.
  1480. * advance both strings to next white space
  1481. */
  1482. if (*fmt == '*') {
  1483. while (!isspace(*fmt) && *fmt)
  1484. fmt++;
  1485. while (!isspace(*str) && *str)
  1486. str++;
  1487. continue;
  1488. }
  1489. /* get field width */
  1490. field_width = -1;
  1491. if (isdigit(*fmt))
  1492. field_width = skip_atoi(&fmt);
  1493. /* get conversion qualifier */
  1494. qualifier = -1;
  1495. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  1496. *fmt == 'Z' || *fmt == 'z') {
  1497. qualifier = *fmt++;
  1498. if (unlikely(qualifier == *fmt)) {
  1499. if (qualifier == 'h') {
  1500. qualifier = 'H';
  1501. fmt++;
  1502. } else if (qualifier == 'l') {
  1503. qualifier = 'L';
  1504. fmt++;
  1505. }
  1506. }
  1507. }
  1508. base = 10;
  1509. is_sign = 0;
  1510. if (!*fmt || !*str)
  1511. break;
  1512. switch(*fmt++) {
  1513. case 'c':
  1514. {
  1515. char *s = (char *) va_arg(args,char*);
  1516. if (field_width == -1)
  1517. field_width = 1;
  1518. do {
  1519. *s++ = *str++;
  1520. } while (--field_width > 0 && *str);
  1521. num++;
  1522. }
  1523. continue;
  1524. case 's':
  1525. {
  1526. char *s = (char *) va_arg(args, char *);
  1527. if(field_width == -1)
  1528. field_width = INT_MAX;
  1529. /* first, skip leading white space in buffer */
  1530. while (isspace(*str))
  1531. str++;
  1532. /* now copy until next white space */
  1533. while (*str && !isspace(*str) && field_width--) {
  1534. *s++ = *str++;
  1535. }
  1536. *s = '\0';
  1537. num++;
  1538. }
  1539. continue;
  1540. case 'n':
  1541. /* return number of characters read so far */
  1542. {
  1543. int *i = (int *)va_arg(args,int*);
  1544. *i = str - buf;
  1545. }
  1546. continue;
  1547. case 'o':
  1548. base = 8;
  1549. break;
  1550. case 'x':
  1551. case 'X':
  1552. base = 16;
  1553. break;
  1554. case 'i':
  1555. base = 0;
  1556. case 'd':
  1557. is_sign = 1;
  1558. case 'u':
  1559. break;
  1560. case '%':
  1561. /* looking for '%' in str */
  1562. if (*str++ != '%')
  1563. return num;
  1564. continue;
  1565. default:
  1566. /* invalid format; stop here */
  1567. return num;
  1568. }
  1569. /* have some sort of integer conversion.
  1570. * first, skip white space in buffer.
  1571. */
  1572. while (isspace(*str))
  1573. str++;
  1574. digit = *str;
  1575. if (is_sign && digit == '-')
  1576. digit = *(str + 1);
  1577. if (!digit
  1578. || (base == 16 && !isxdigit(digit))
  1579. || (base == 10 && !isdigit(digit))
  1580. || (base == 8 && (!isdigit(digit) || digit > '7'))
  1581. || (base == 0 && !isdigit(digit)))
  1582. break;
  1583. switch(qualifier) {
  1584. case 'H': /* that's 'hh' in format */
  1585. if (is_sign) {
  1586. signed char *s = (signed char *) va_arg(args,signed char *);
  1587. *s = (signed char) simple_strtol(str,&next,base);
  1588. } else {
  1589. unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
  1590. *s = (unsigned char) simple_strtoul(str, &next, base);
  1591. }
  1592. break;
  1593. case 'h':
  1594. if (is_sign) {
  1595. short *s = (short *) va_arg(args,short *);
  1596. *s = (short) simple_strtol(str,&next,base);
  1597. } else {
  1598. unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
  1599. *s = (unsigned short) simple_strtoul(str, &next, base);
  1600. }
  1601. break;
  1602. case 'l':
  1603. if (is_sign) {
  1604. long *l = (long *) va_arg(args,long *);
  1605. *l = simple_strtol(str,&next,base);
  1606. } else {
  1607. unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
  1608. *l = simple_strtoul(str,&next,base);
  1609. }
  1610. break;
  1611. case 'L':
  1612. if (is_sign) {
  1613. long long *l = (long long*) va_arg(args,long long *);
  1614. *l = simple_strtoll(str,&next,base);
  1615. } else {
  1616. unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
  1617. *l = simple_strtoull(str,&next,base);
  1618. }
  1619. break;
  1620. case 'Z':
  1621. case 'z':
  1622. {
  1623. size_t *s = (size_t*) va_arg(args,size_t*);
  1624. *s = (size_t) simple_strtoul(str,&next,base);
  1625. }
  1626. break;
  1627. default:
  1628. if (is_sign) {
  1629. int *i = (int *) va_arg(args, int*);
  1630. *i = (int) simple_strtol(str,&next,base);
  1631. } else {
  1632. unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
  1633. *i = (unsigned int) simple_strtoul(str,&next,base);
  1634. }
  1635. break;
  1636. }
  1637. num++;
  1638. if (!next)
  1639. break;
  1640. str = next;
  1641. }
  1642. /*
  1643. * Now we've come all the way through so either the input string or the
  1644. * format ended. In the former case, there can be a %n at the current
  1645. * position in the format that needs to be filled.
  1646. */
  1647. if (*fmt == '%' && *(fmt + 1) == 'n') {
  1648. int *p = (int *)va_arg(args, int *);
  1649. *p = str - buf;
  1650. }
  1651. return num;
  1652. }
  1653. EXPORT_SYMBOL(vsscanf);
  1654. /**
  1655. * sscanf - Unformat a buffer into a list of arguments
  1656. * @buf: input buffer
  1657. * @fmt: formatting of buffer
  1658. * @...: resulting arguments
  1659. */
  1660. int sscanf(const char * buf, const char * fmt, ...)
  1661. {
  1662. va_list args;
  1663. int i;
  1664. va_start(args,fmt);
  1665. i = vsscanf(buf,fmt,args);
  1666. va_end(args);
  1667. return i;
  1668. }
  1669. EXPORT_SYMBOL(sscanf);