vsprintf.c 69 KB

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