vsprintf.c 53 KB

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