vsprintf.c 58 KB

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