vsprintf.c 62 KB

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