vsprintf.c 63 KB

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