vsprintf.c 53 KB

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