vsprintf.c 51 KB

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