vsprintf.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504
  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. /*
  1532. * Since %n poses a greater security risk than
  1533. * utility, ignore %n and skip its argument.
  1534. */
  1535. void *skip_arg;
  1536. WARN_ONCE(1, "Please remove ignored %%n in '%s'\n",
  1537. old_fmt);
  1538. skip_arg = va_arg(args, void *);
  1539. break;
  1540. }
  1541. default:
  1542. switch (spec.type) {
  1543. case FORMAT_TYPE_LONG_LONG:
  1544. num = va_arg(args, long long);
  1545. break;
  1546. case FORMAT_TYPE_ULONG:
  1547. num = va_arg(args, unsigned long);
  1548. break;
  1549. case FORMAT_TYPE_LONG:
  1550. num = va_arg(args, long);
  1551. break;
  1552. case FORMAT_TYPE_SIZE_T:
  1553. if (spec.flags & SIGN)
  1554. num = va_arg(args, ssize_t);
  1555. else
  1556. num = va_arg(args, size_t);
  1557. break;
  1558. case FORMAT_TYPE_PTRDIFF:
  1559. num = va_arg(args, ptrdiff_t);
  1560. break;
  1561. case FORMAT_TYPE_UBYTE:
  1562. num = (unsigned char) va_arg(args, int);
  1563. break;
  1564. case FORMAT_TYPE_BYTE:
  1565. num = (signed char) va_arg(args, int);
  1566. break;
  1567. case FORMAT_TYPE_USHORT:
  1568. num = (unsigned short) va_arg(args, int);
  1569. break;
  1570. case FORMAT_TYPE_SHORT:
  1571. num = (short) va_arg(args, int);
  1572. break;
  1573. case FORMAT_TYPE_INT:
  1574. num = (int) va_arg(args, int);
  1575. break;
  1576. default:
  1577. num = va_arg(args, unsigned int);
  1578. }
  1579. str = number(str, end, num, spec);
  1580. }
  1581. }
  1582. if (size > 0) {
  1583. if (str < end)
  1584. *str = '\0';
  1585. else
  1586. end[-1] = '\0';
  1587. }
  1588. /* the trailing null byte doesn't count towards the total */
  1589. return str-buf;
  1590. }
  1591. EXPORT_SYMBOL(vsnprintf);
  1592. /**
  1593. * vscnprintf - Format a string and place it in a buffer
  1594. * @buf: The buffer to place the result into
  1595. * @size: The size of the buffer, including the trailing null space
  1596. * @fmt: The format string to use
  1597. * @args: Arguments for the format string
  1598. *
  1599. * The return value is the number of characters which have been written into
  1600. * the @buf not including the trailing '\0'. If @size is == 0 the function
  1601. * returns 0.
  1602. *
  1603. * If you're not already dealing with a va_list consider using scnprintf().
  1604. *
  1605. * See the vsnprintf() documentation for format string extensions over C99.
  1606. */
  1607. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  1608. {
  1609. int i;
  1610. i = vsnprintf(buf, size, fmt, args);
  1611. if (likely(i < size))
  1612. return i;
  1613. if (size != 0)
  1614. return size - 1;
  1615. return 0;
  1616. }
  1617. EXPORT_SYMBOL(vscnprintf);
  1618. /**
  1619. * snprintf - Format a string and place it in a buffer
  1620. * @buf: The buffer to place the result into
  1621. * @size: The size of the buffer, including the trailing null space
  1622. * @fmt: The format string to use
  1623. * @...: Arguments for the format string
  1624. *
  1625. * The return value is the number of characters which would be
  1626. * generated for the given input, excluding the trailing null,
  1627. * as per ISO C99. If the return is greater than or equal to
  1628. * @size, the resulting string is truncated.
  1629. *
  1630. * See the vsnprintf() documentation for format string extensions over C99.
  1631. */
  1632. int snprintf(char *buf, size_t size, const char *fmt, ...)
  1633. {
  1634. va_list args;
  1635. int i;
  1636. va_start(args, fmt);
  1637. i = vsnprintf(buf, size, fmt, args);
  1638. va_end(args);
  1639. return i;
  1640. }
  1641. EXPORT_SYMBOL(snprintf);
  1642. /**
  1643. * scnprintf - Format a string and place it in a buffer
  1644. * @buf: The buffer to place the result into
  1645. * @size: The size of the buffer, including the trailing null space
  1646. * @fmt: The format string to use
  1647. * @...: Arguments for the format string
  1648. *
  1649. * The return value is the number of characters written into @buf not including
  1650. * the trailing '\0'. If @size is == 0 the function returns 0.
  1651. */
  1652. int scnprintf(char *buf, size_t size, const char *fmt, ...)
  1653. {
  1654. va_list args;
  1655. int i;
  1656. va_start(args, fmt);
  1657. i = vscnprintf(buf, size, fmt, args);
  1658. va_end(args);
  1659. return i;
  1660. }
  1661. EXPORT_SYMBOL(scnprintf);
  1662. /**
  1663. * vsprintf - Format a string and place it in a buffer
  1664. * @buf: The buffer to place the result into
  1665. * @fmt: The format string to use
  1666. * @args: Arguments for the format string
  1667. *
  1668. * The function returns the number of characters written
  1669. * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
  1670. * buffer overflows.
  1671. *
  1672. * If you're not already dealing with a va_list consider using sprintf().
  1673. *
  1674. * See the vsnprintf() documentation for format string extensions over C99.
  1675. */
  1676. int vsprintf(char *buf, const char *fmt, va_list args)
  1677. {
  1678. return vsnprintf(buf, INT_MAX, fmt, args);
  1679. }
  1680. EXPORT_SYMBOL(vsprintf);
  1681. /**
  1682. * sprintf - Format a string and place it in a buffer
  1683. * @buf: The buffer to place the result into
  1684. * @fmt: The format string to use
  1685. * @...: Arguments for the format string
  1686. *
  1687. * The function returns the number of characters written
  1688. * into @buf. Use snprintf() or scnprintf() in order to avoid
  1689. * buffer overflows.
  1690. *
  1691. * See the vsnprintf() documentation for format string extensions over C99.
  1692. */
  1693. int sprintf(char *buf, const char *fmt, ...)
  1694. {
  1695. va_list args;
  1696. int i;
  1697. va_start(args, fmt);
  1698. i = vsnprintf(buf, INT_MAX, fmt, args);
  1699. va_end(args);
  1700. return i;
  1701. }
  1702. EXPORT_SYMBOL(sprintf);
  1703. #ifdef CONFIG_BINARY_PRINTF
  1704. /*
  1705. * bprintf service:
  1706. * vbin_printf() - VA arguments to binary data
  1707. * bstr_printf() - Binary data to text string
  1708. */
  1709. /**
  1710. * vbin_printf - Parse a format string and place args' binary value in a buffer
  1711. * @bin_buf: The buffer to place args' binary value
  1712. * @size: The size of the buffer(by words(32bits), not characters)
  1713. * @fmt: The format string to use
  1714. * @args: Arguments for the format string
  1715. *
  1716. * The format follows C99 vsnprintf, except %n is ignored, and its argument
  1717. * is skiped.
  1718. *
  1719. * The return value is the number of words(32bits) which would be generated for
  1720. * the given input.
  1721. *
  1722. * NOTE:
  1723. * If the return value is greater than @size, the resulting bin_buf is NOT
  1724. * valid for bstr_printf().
  1725. */
  1726. int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
  1727. {
  1728. struct printf_spec spec = {0};
  1729. char *str, *end;
  1730. str = (char *)bin_buf;
  1731. end = (char *)(bin_buf + size);
  1732. #define save_arg(type) \
  1733. do { \
  1734. if (sizeof(type) == 8) { \
  1735. unsigned long long value; \
  1736. str = PTR_ALIGN(str, sizeof(u32)); \
  1737. value = va_arg(args, unsigned long long); \
  1738. if (str + sizeof(type) <= end) { \
  1739. *(u32 *)str = *(u32 *)&value; \
  1740. *(u32 *)(str + 4) = *((u32 *)&value + 1); \
  1741. } \
  1742. } else { \
  1743. unsigned long value; \
  1744. str = PTR_ALIGN(str, sizeof(type)); \
  1745. value = va_arg(args, int); \
  1746. if (str + sizeof(type) <= end) \
  1747. *(typeof(type) *)str = (type)value; \
  1748. } \
  1749. str += sizeof(type); \
  1750. } while (0)
  1751. while (*fmt) {
  1752. int read = format_decode(fmt, &spec);
  1753. fmt += read;
  1754. switch (spec.type) {
  1755. case FORMAT_TYPE_NONE:
  1756. case FORMAT_TYPE_INVALID:
  1757. case FORMAT_TYPE_PERCENT_CHAR:
  1758. break;
  1759. case FORMAT_TYPE_WIDTH:
  1760. case FORMAT_TYPE_PRECISION:
  1761. save_arg(int);
  1762. break;
  1763. case FORMAT_TYPE_CHAR:
  1764. save_arg(char);
  1765. break;
  1766. case FORMAT_TYPE_STR: {
  1767. const char *save_str = va_arg(args, char *);
  1768. size_t len;
  1769. if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
  1770. || (unsigned long)save_str < PAGE_SIZE)
  1771. save_str = "(null)";
  1772. len = strlen(save_str) + 1;
  1773. if (str + len < end)
  1774. memcpy(str, save_str, len);
  1775. str += len;
  1776. break;
  1777. }
  1778. case FORMAT_TYPE_PTR:
  1779. save_arg(void *);
  1780. /* skip all alphanumeric pointer suffixes */
  1781. while (isalnum(*fmt))
  1782. fmt++;
  1783. break;
  1784. case FORMAT_TYPE_NRCHARS: {
  1785. /* skip %n 's argument */
  1786. u8 qualifier = spec.qualifier;
  1787. void *skip_arg;
  1788. if (qualifier == 'l')
  1789. skip_arg = va_arg(args, long *);
  1790. else if (_tolower(qualifier) == 'z')
  1791. skip_arg = va_arg(args, size_t *);
  1792. else
  1793. skip_arg = va_arg(args, int *);
  1794. break;
  1795. }
  1796. default:
  1797. switch (spec.type) {
  1798. case FORMAT_TYPE_LONG_LONG:
  1799. save_arg(long long);
  1800. break;
  1801. case FORMAT_TYPE_ULONG:
  1802. case FORMAT_TYPE_LONG:
  1803. save_arg(unsigned long);
  1804. break;
  1805. case FORMAT_TYPE_SIZE_T:
  1806. save_arg(size_t);
  1807. break;
  1808. case FORMAT_TYPE_PTRDIFF:
  1809. save_arg(ptrdiff_t);
  1810. break;
  1811. case FORMAT_TYPE_UBYTE:
  1812. case FORMAT_TYPE_BYTE:
  1813. save_arg(char);
  1814. break;
  1815. case FORMAT_TYPE_USHORT:
  1816. case FORMAT_TYPE_SHORT:
  1817. save_arg(short);
  1818. break;
  1819. default:
  1820. save_arg(int);
  1821. }
  1822. }
  1823. }
  1824. return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
  1825. #undef save_arg
  1826. }
  1827. EXPORT_SYMBOL_GPL(vbin_printf);
  1828. /**
  1829. * bstr_printf - Format a string from binary arguments and place it in a buffer
  1830. * @buf: The buffer to place the result into
  1831. * @size: The size of the buffer, including the trailing null space
  1832. * @fmt: The format string to use
  1833. * @bin_buf: Binary arguments for the format string
  1834. *
  1835. * This function like C99 vsnprintf, but the difference is that vsnprintf gets
  1836. * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
  1837. * a binary buffer that generated by vbin_printf.
  1838. *
  1839. * The format follows C99 vsnprintf, but has some extensions:
  1840. * see vsnprintf comment for details.
  1841. *
  1842. * The return value is the number of characters which would
  1843. * be generated for the given input, excluding the trailing
  1844. * '\0', as per ISO C99. If you want to have the exact
  1845. * number of characters written into @buf as return value
  1846. * (not including the trailing '\0'), use vscnprintf(). If the
  1847. * return is greater than or equal to @size, the resulting
  1848. * string is truncated.
  1849. */
  1850. int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
  1851. {
  1852. struct printf_spec spec = {0};
  1853. char *str, *end;
  1854. const char *args = (const char *)bin_buf;
  1855. if (WARN_ON_ONCE((int) size < 0))
  1856. return 0;
  1857. str = buf;
  1858. end = buf + size;
  1859. #define get_arg(type) \
  1860. ({ \
  1861. typeof(type) value; \
  1862. if (sizeof(type) == 8) { \
  1863. args = PTR_ALIGN(args, sizeof(u32)); \
  1864. *(u32 *)&value = *(u32 *)args; \
  1865. *((u32 *)&value + 1) = *(u32 *)(args + 4); \
  1866. } else { \
  1867. args = PTR_ALIGN(args, sizeof(type)); \
  1868. value = *(typeof(type) *)args; \
  1869. } \
  1870. args += sizeof(type); \
  1871. value; \
  1872. })
  1873. /* Make sure end is always >= buf */
  1874. if (end < buf) {
  1875. end = ((void *)-1);
  1876. size = end - buf;
  1877. }
  1878. while (*fmt) {
  1879. const char *old_fmt = fmt;
  1880. int read = format_decode(fmt, &spec);
  1881. fmt += read;
  1882. switch (spec.type) {
  1883. case FORMAT_TYPE_NONE: {
  1884. int copy = read;
  1885. if (str < end) {
  1886. if (copy > end - str)
  1887. copy = end - str;
  1888. memcpy(str, old_fmt, copy);
  1889. }
  1890. str += read;
  1891. break;
  1892. }
  1893. case FORMAT_TYPE_WIDTH:
  1894. spec.field_width = get_arg(int);
  1895. break;
  1896. case FORMAT_TYPE_PRECISION:
  1897. spec.precision = get_arg(int);
  1898. break;
  1899. case FORMAT_TYPE_CHAR: {
  1900. char c;
  1901. if (!(spec.flags & LEFT)) {
  1902. while (--spec.field_width > 0) {
  1903. if (str < end)
  1904. *str = ' ';
  1905. ++str;
  1906. }
  1907. }
  1908. c = (unsigned char) get_arg(char);
  1909. if (str < end)
  1910. *str = c;
  1911. ++str;
  1912. while (--spec.field_width > 0) {
  1913. if (str < end)
  1914. *str = ' ';
  1915. ++str;
  1916. }
  1917. break;
  1918. }
  1919. case FORMAT_TYPE_STR: {
  1920. const char *str_arg = args;
  1921. args += strlen(str_arg) + 1;
  1922. str = string(str, end, (char *)str_arg, spec);
  1923. break;
  1924. }
  1925. case FORMAT_TYPE_PTR:
  1926. str = pointer(fmt+1, str, end, get_arg(void *), spec);
  1927. while (isalnum(*fmt))
  1928. fmt++;
  1929. break;
  1930. case FORMAT_TYPE_PERCENT_CHAR:
  1931. case FORMAT_TYPE_INVALID:
  1932. if (str < end)
  1933. *str = '%';
  1934. ++str;
  1935. break;
  1936. case FORMAT_TYPE_NRCHARS:
  1937. /* skip */
  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 = 0;
  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 = 1;
  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);