vsprintf.c 53 KB

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