vsprintf.c 63 KB

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