vsprintf.c 64 KB

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