vsprintf.c 53 KB

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