vsprintf.c 53 KB

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