vsprintf.c 67 KB

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