vsprintf.c 60 KB

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