vsprintf.c 58 KB

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