vsprintf.c 63 KB

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