vsprintf.c 47 KB

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