vsprintf.c 53 KB

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