vsprintf.c 52 KB

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