vsprintf.c 53 KB

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