vsprintf.c 56 KB

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