vsprintf.c 47 KB

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