vsprintf.c 53 KB

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