vsprintf.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045
  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
  1035. * http://tools.ietf.org/html/draft-ietf-6man-text-addr-representation-00
  1036. * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
  1037. * case.
  1038. * %n is ignored
  1039. *
  1040. * The return value is the number of characters which would
  1041. * be generated for the given input, excluding the trailing
  1042. * '\0', as per ISO C99. If you want to have the exact
  1043. * number of characters written into @buf as return value
  1044. * (not including the trailing '\0'), use vscnprintf(). If the
  1045. * return is greater than or equal to @size, the resulting
  1046. * string is truncated.
  1047. *
  1048. * If you're not already dealing with a va_list consider using snprintf().
  1049. */
  1050. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  1051. {
  1052. unsigned long long num;
  1053. char *str, *end;
  1054. struct printf_spec spec = {0};
  1055. /* Reject out-of-range values early. Large positive sizes are
  1056. used for unknown buffer sizes. */
  1057. if (WARN_ON_ONCE((int) size < 0))
  1058. return 0;
  1059. str = buf;
  1060. end = buf + size;
  1061. /* Make sure end is always >= buf */
  1062. if (end < buf) {
  1063. end = ((void *)-1);
  1064. size = end - buf;
  1065. }
  1066. while (*fmt) {
  1067. const char *old_fmt = fmt;
  1068. int read = format_decode(fmt, &spec);
  1069. fmt += read;
  1070. switch (spec.type) {
  1071. case FORMAT_TYPE_NONE: {
  1072. int copy = read;
  1073. if (str < end) {
  1074. if (copy > end - str)
  1075. copy = end - str;
  1076. memcpy(str, old_fmt, copy);
  1077. }
  1078. str += read;
  1079. break;
  1080. }
  1081. case FORMAT_TYPE_WIDTH:
  1082. spec.field_width = va_arg(args, int);
  1083. break;
  1084. case FORMAT_TYPE_PRECISION:
  1085. spec.precision = va_arg(args, int);
  1086. break;
  1087. case FORMAT_TYPE_CHAR: {
  1088. char c;
  1089. if (!(spec.flags & LEFT)) {
  1090. while (--spec.field_width > 0) {
  1091. if (str < end)
  1092. *str = ' ';
  1093. ++str;
  1094. }
  1095. }
  1096. c = (unsigned char) va_arg(args, int);
  1097. if (str < end)
  1098. *str = c;
  1099. ++str;
  1100. while (--spec.field_width > 0) {
  1101. if (str < end)
  1102. *str = ' ';
  1103. ++str;
  1104. }
  1105. break;
  1106. }
  1107. case FORMAT_TYPE_STR:
  1108. str = string(str, end, va_arg(args, char *), spec);
  1109. break;
  1110. case FORMAT_TYPE_PTR:
  1111. str = pointer(fmt+1, str, end, va_arg(args, void *),
  1112. spec);
  1113. while (isalnum(*fmt))
  1114. fmt++;
  1115. break;
  1116. case FORMAT_TYPE_PERCENT_CHAR:
  1117. if (str < end)
  1118. *str = '%';
  1119. ++str;
  1120. break;
  1121. case FORMAT_TYPE_INVALID:
  1122. if (str < end)
  1123. *str = '%';
  1124. ++str;
  1125. break;
  1126. case FORMAT_TYPE_NRCHARS: {
  1127. u8 qualifier = spec.qualifier;
  1128. if (qualifier == 'l') {
  1129. long *ip = va_arg(args, long *);
  1130. *ip = (str - buf);
  1131. } else if (TOLOWER(qualifier) == 'z') {
  1132. size_t *ip = va_arg(args, size_t *);
  1133. *ip = (str - buf);
  1134. } else {
  1135. int *ip = va_arg(args, int *);
  1136. *ip = (str - buf);
  1137. }
  1138. break;
  1139. }
  1140. default:
  1141. switch (spec.type) {
  1142. case FORMAT_TYPE_LONG_LONG:
  1143. num = va_arg(args, long long);
  1144. break;
  1145. case FORMAT_TYPE_ULONG:
  1146. num = va_arg(args, unsigned long);
  1147. break;
  1148. case FORMAT_TYPE_LONG:
  1149. num = va_arg(args, long);
  1150. break;
  1151. case FORMAT_TYPE_SIZE_T:
  1152. num = va_arg(args, size_t);
  1153. break;
  1154. case FORMAT_TYPE_PTRDIFF:
  1155. num = va_arg(args, ptrdiff_t);
  1156. break;
  1157. case FORMAT_TYPE_UBYTE:
  1158. num = (unsigned char) va_arg(args, int);
  1159. break;
  1160. case FORMAT_TYPE_BYTE:
  1161. num = (signed char) va_arg(args, int);
  1162. break;
  1163. case FORMAT_TYPE_USHORT:
  1164. num = (unsigned short) va_arg(args, int);
  1165. break;
  1166. case FORMAT_TYPE_SHORT:
  1167. num = (short) va_arg(args, int);
  1168. break;
  1169. case FORMAT_TYPE_INT:
  1170. num = (int) va_arg(args, int);
  1171. break;
  1172. default:
  1173. num = va_arg(args, unsigned int);
  1174. }
  1175. str = number(str, end, num, spec);
  1176. }
  1177. }
  1178. if (size > 0) {
  1179. if (str < end)
  1180. *str = '\0';
  1181. else
  1182. end[-1] = '\0';
  1183. }
  1184. /* the trailing null byte doesn't count towards the total */
  1185. return str-buf;
  1186. }
  1187. EXPORT_SYMBOL(vsnprintf);
  1188. /**
  1189. * vscnprintf - Format a string and place it in a buffer
  1190. * @buf: The buffer to place the result into
  1191. * @size: The size of the buffer, including the trailing null space
  1192. * @fmt: The format string to use
  1193. * @args: Arguments for the format string
  1194. *
  1195. * The return value is the number of characters which have been written into
  1196. * the @buf not including the trailing '\0'. If @size is == 0 the function
  1197. * returns 0.
  1198. *
  1199. * If you're not already dealing with a va_list consider using scnprintf().
  1200. *
  1201. * See the vsnprintf() documentation for format string extensions over C99.
  1202. */
  1203. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  1204. {
  1205. int i;
  1206. i = vsnprintf(buf, size, fmt, args);
  1207. if (likely(i < size))
  1208. return i;
  1209. if (size != 0)
  1210. return size - 1;
  1211. return 0;
  1212. }
  1213. EXPORT_SYMBOL(vscnprintf);
  1214. /**
  1215. * snprintf - Format a string and place it in a buffer
  1216. * @buf: The buffer to place the result into
  1217. * @size: The size of the buffer, including the trailing null space
  1218. * @fmt: The format string to use
  1219. * @...: Arguments for the format string
  1220. *
  1221. * The return value is the number of characters which would be
  1222. * generated for the given input, excluding the trailing null,
  1223. * as per ISO C99. If the return is greater than or equal to
  1224. * @size, the resulting string is truncated.
  1225. *
  1226. * See the vsnprintf() documentation for format string extensions over C99.
  1227. */
  1228. int snprintf(char *buf, size_t size, const char *fmt, ...)
  1229. {
  1230. va_list args;
  1231. int i;
  1232. va_start(args, fmt);
  1233. i = vsnprintf(buf, size, fmt, args);
  1234. va_end(args);
  1235. return i;
  1236. }
  1237. EXPORT_SYMBOL(snprintf);
  1238. /**
  1239. * scnprintf - Format a string and place it in a buffer
  1240. * @buf: The buffer to place the result into
  1241. * @size: The size of the buffer, including the trailing null space
  1242. * @fmt: The format string to use
  1243. * @...: Arguments for the format string
  1244. *
  1245. * The return value is the number of characters written into @buf not including
  1246. * the trailing '\0'. If @size is == 0 the function returns 0.
  1247. */
  1248. int scnprintf(char *buf, size_t size, const char *fmt, ...)
  1249. {
  1250. va_list args;
  1251. int i;
  1252. va_start(args, fmt);
  1253. i = vscnprintf(buf, size, fmt, args);
  1254. va_end(args);
  1255. return i;
  1256. }
  1257. EXPORT_SYMBOL(scnprintf);
  1258. /**
  1259. * vsprintf - Format a string and place it in a buffer
  1260. * @buf: The buffer to place the result into
  1261. * @fmt: The format string to use
  1262. * @args: Arguments for the format string
  1263. *
  1264. * The function returns the number of characters written
  1265. * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
  1266. * buffer overflows.
  1267. *
  1268. * If you're not already dealing with a va_list consider using sprintf().
  1269. *
  1270. * See the vsnprintf() documentation for format string extensions over C99.
  1271. */
  1272. int vsprintf(char *buf, const char *fmt, va_list args)
  1273. {
  1274. return vsnprintf(buf, INT_MAX, fmt, args);
  1275. }
  1276. EXPORT_SYMBOL(vsprintf);
  1277. /**
  1278. * sprintf - Format a string and place it in a buffer
  1279. * @buf: The buffer to place the result into
  1280. * @fmt: The format string to use
  1281. * @...: Arguments for the format string
  1282. *
  1283. * The function returns the number of characters written
  1284. * into @buf. Use snprintf() or scnprintf() in order to avoid
  1285. * buffer overflows.
  1286. *
  1287. * See the vsnprintf() documentation for format string extensions over C99.
  1288. */
  1289. int sprintf(char *buf, const char *fmt, ...)
  1290. {
  1291. va_list args;
  1292. int i;
  1293. va_start(args, fmt);
  1294. i = vsnprintf(buf, INT_MAX, fmt, args);
  1295. va_end(args);
  1296. return i;
  1297. }
  1298. EXPORT_SYMBOL(sprintf);
  1299. #ifdef CONFIG_BINARY_PRINTF
  1300. /*
  1301. * bprintf service:
  1302. * vbin_printf() - VA arguments to binary data
  1303. * bstr_printf() - Binary data to text string
  1304. */
  1305. /**
  1306. * vbin_printf - Parse a format string and place args' binary value in a buffer
  1307. * @bin_buf: The buffer to place args' binary value
  1308. * @size: The size of the buffer(by words(32bits), not characters)
  1309. * @fmt: The format string to use
  1310. * @args: Arguments for the format string
  1311. *
  1312. * The format follows C99 vsnprintf, except %n is ignored, and its argument
  1313. * is skiped.
  1314. *
  1315. * The return value is the number of words(32bits) which would be generated for
  1316. * the given input.
  1317. *
  1318. * NOTE:
  1319. * If the return value is greater than @size, the resulting bin_buf is NOT
  1320. * valid for bstr_printf().
  1321. */
  1322. int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
  1323. {
  1324. struct printf_spec spec = {0};
  1325. char *str, *end;
  1326. str = (char *)bin_buf;
  1327. end = (char *)(bin_buf + size);
  1328. #define save_arg(type) \
  1329. do { \
  1330. if (sizeof(type) == 8) { \
  1331. unsigned long long value; \
  1332. str = PTR_ALIGN(str, sizeof(u32)); \
  1333. value = va_arg(args, unsigned long long); \
  1334. if (str + sizeof(type) <= end) { \
  1335. *(u32 *)str = *(u32 *)&value; \
  1336. *(u32 *)(str + 4) = *((u32 *)&value + 1); \
  1337. } \
  1338. } else { \
  1339. unsigned long value; \
  1340. str = PTR_ALIGN(str, sizeof(type)); \
  1341. value = va_arg(args, int); \
  1342. if (str + sizeof(type) <= end) \
  1343. *(typeof(type) *)str = (type)value; \
  1344. } \
  1345. str += sizeof(type); \
  1346. } while (0)
  1347. while (*fmt) {
  1348. int read = format_decode(fmt, &spec);
  1349. fmt += read;
  1350. switch (spec.type) {
  1351. case FORMAT_TYPE_NONE:
  1352. case FORMAT_TYPE_INVALID:
  1353. case FORMAT_TYPE_PERCENT_CHAR:
  1354. break;
  1355. case FORMAT_TYPE_WIDTH:
  1356. case FORMAT_TYPE_PRECISION:
  1357. save_arg(int);
  1358. break;
  1359. case FORMAT_TYPE_CHAR:
  1360. save_arg(char);
  1361. break;
  1362. case FORMAT_TYPE_STR: {
  1363. const char *save_str = va_arg(args, char *);
  1364. size_t len;
  1365. if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
  1366. || (unsigned long)save_str < PAGE_SIZE)
  1367. save_str = "(null)";
  1368. len = strlen(save_str) + 1;
  1369. if (str + len < end)
  1370. memcpy(str, save_str, len);
  1371. str += len;
  1372. break;
  1373. }
  1374. case FORMAT_TYPE_PTR:
  1375. save_arg(void *);
  1376. /* skip all alphanumeric pointer suffixes */
  1377. while (isalnum(*fmt))
  1378. fmt++;
  1379. break;
  1380. case FORMAT_TYPE_NRCHARS: {
  1381. /* skip %n 's argument */
  1382. u8 qualifier = spec.qualifier;
  1383. void *skip_arg;
  1384. if (qualifier == 'l')
  1385. skip_arg = va_arg(args, long *);
  1386. else if (TOLOWER(qualifier) == 'z')
  1387. skip_arg = va_arg(args, size_t *);
  1388. else
  1389. skip_arg = va_arg(args, int *);
  1390. break;
  1391. }
  1392. default:
  1393. switch (spec.type) {
  1394. case FORMAT_TYPE_LONG_LONG:
  1395. save_arg(long long);
  1396. break;
  1397. case FORMAT_TYPE_ULONG:
  1398. case FORMAT_TYPE_LONG:
  1399. save_arg(unsigned long);
  1400. break;
  1401. case FORMAT_TYPE_SIZE_T:
  1402. save_arg(size_t);
  1403. break;
  1404. case FORMAT_TYPE_PTRDIFF:
  1405. save_arg(ptrdiff_t);
  1406. break;
  1407. case FORMAT_TYPE_UBYTE:
  1408. case FORMAT_TYPE_BYTE:
  1409. save_arg(char);
  1410. break;
  1411. case FORMAT_TYPE_USHORT:
  1412. case FORMAT_TYPE_SHORT:
  1413. save_arg(short);
  1414. break;
  1415. default:
  1416. save_arg(int);
  1417. }
  1418. }
  1419. }
  1420. return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
  1421. #undef save_arg
  1422. }
  1423. EXPORT_SYMBOL_GPL(vbin_printf);
  1424. /**
  1425. * bstr_printf - Format a string from binary arguments and place it in a buffer
  1426. * @buf: The buffer to place the result into
  1427. * @size: The size of the buffer, including the trailing null space
  1428. * @fmt: The format string to use
  1429. * @bin_buf: Binary arguments for the format string
  1430. *
  1431. * This function like C99 vsnprintf, but the difference is that vsnprintf gets
  1432. * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
  1433. * a binary buffer that generated by vbin_printf.
  1434. *
  1435. * The format follows C99 vsnprintf, but has some extensions:
  1436. * see vsnprintf comment for details.
  1437. *
  1438. * The return value is the number of characters which would
  1439. * be generated for the given input, excluding the trailing
  1440. * '\0', as per ISO C99. If you want to have the exact
  1441. * number of characters written into @buf as return value
  1442. * (not including the trailing '\0'), use vscnprintf(). If the
  1443. * return is greater than or equal to @size, the resulting
  1444. * string is truncated.
  1445. */
  1446. int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
  1447. {
  1448. struct printf_spec spec = {0};
  1449. char *str, *end;
  1450. const char *args = (const char *)bin_buf;
  1451. if (WARN_ON_ONCE((int) size < 0))
  1452. return 0;
  1453. str = buf;
  1454. end = buf + size;
  1455. #define get_arg(type) \
  1456. ({ \
  1457. typeof(type) value; \
  1458. if (sizeof(type) == 8) { \
  1459. args = PTR_ALIGN(args, sizeof(u32)); \
  1460. *(u32 *)&value = *(u32 *)args; \
  1461. *((u32 *)&value + 1) = *(u32 *)(args + 4); \
  1462. } else { \
  1463. args = PTR_ALIGN(args, sizeof(type)); \
  1464. value = *(typeof(type) *)args; \
  1465. } \
  1466. args += sizeof(type); \
  1467. value; \
  1468. })
  1469. /* Make sure end is always >= buf */
  1470. if (end < buf) {
  1471. end = ((void *)-1);
  1472. size = end - buf;
  1473. }
  1474. while (*fmt) {
  1475. const char *old_fmt = fmt;
  1476. int read = format_decode(fmt, &spec);
  1477. fmt += read;
  1478. switch (spec.type) {
  1479. case FORMAT_TYPE_NONE: {
  1480. int copy = read;
  1481. if (str < end) {
  1482. if (copy > end - str)
  1483. copy = end - str;
  1484. memcpy(str, old_fmt, copy);
  1485. }
  1486. str += read;
  1487. break;
  1488. }
  1489. case FORMAT_TYPE_WIDTH:
  1490. spec.field_width = get_arg(int);
  1491. break;
  1492. case FORMAT_TYPE_PRECISION:
  1493. spec.precision = get_arg(int);
  1494. break;
  1495. case FORMAT_TYPE_CHAR: {
  1496. char c;
  1497. if (!(spec.flags & LEFT)) {
  1498. while (--spec.field_width > 0) {
  1499. if (str < end)
  1500. *str = ' ';
  1501. ++str;
  1502. }
  1503. }
  1504. c = (unsigned char) get_arg(char);
  1505. if (str < end)
  1506. *str = c;
  1507. ++str;
  1508. while (--spec.field_width > 0) {
  1509. if (str < end)
  1510. *str = ' ';
  1511. ++str;
  1512. }
  1513. break;
  1514. }
  1515. case FORMAT_TYPE_STR: {
  1516. const char *str_arg = args;
  1517. args += strlen(str_arg) + 1;
  1518. str = string(str, end, (char *)str_arg, spec);
  1519. break;
  1520. }
  1521. case FORMAT_TYPE_PTR:
  1522. str = pointer(fmt+1, str, end, get_arg(void *), spec);
  1523. while (isalnum(*fmt))
  1524. fmt++;
  1525. break;
  1526. case FORMAT_TYPE_PERCENT_CHAR:
  1527. case FORMAT_TYPE_INVALID:
  1528. if (str < end)
  1529. *str = '%';
  1530. ++str;
  1531. break;
  1532. case FORMAT_TYPE_NRCHARS:
  1533. /* skip */
  1534. break;
  1535. default: {
  1536. unsigned long long num;
  1537. switch (spec.type) {
  1538. case FORMAT_TYPE_LONG_LONG:
  1539. num = get_arg(long long);
  1540. break;
  1541. case FORMAT_TYPE_ULONG:
  1542. case FORMAT_TYPE_LONG:
  1543. num = get_arg(unsigned long);
  1544. break;
  1545. case FORMAT_TYPE_SIZE_T:
  1546. num = get_arg(size_t);
  1547. break;
  1548. case FORMAT_TYPE_PTRDIFF:
  1549. num = get_arg(ptrdiff_t);
  1550. break;
  1551. case FORMAT_TYPE_UBYTE:
  1552. num = get_arg(unsigned char);
  1553. break;
  1554. case FORMAT_TYPE_BYTE:
  1555. num = get_arg(signed char);
  1556. break;
  1557. case FORMAT_TYPE_USHORT:
  1558. num = get_arg(unsigned short);
  1559. break;
  1560. case FORMAT_TYPE_SHORT:
  1561. num = get_arg(short);
  1562. break;
  1563. case FORMAT_TYPE_UINT:
  1564. num = get_arg(unsigned int);
  1565. break;
  1566. default:
  1567. num = get_arg(int);
  1568. }
  1569. str = number(str, end, num, spec);
  1570. } /* default: */
  1571. } /* switch(spec.type) */
  1572. } /* while(*fmt) */
  1573. if (size > 0) {
  1574. if (str < end)
  1575. *str = '\0';
  1576. else
  1577. end[-1] = '\0';
  1578. }
  1579. #undef get_arg
  1580. /* the trailing null byte doesn't count towards the total */
  1581. return str - buf;
  1582. }
  1583. EXPORT_SYMBOL_GPL(bstr_printf);
  1584. /**
  1585. * bprintf - Parse a format string and place args' binary value in a buffer
  1586. * @bin_buf: The buffer to place args' binary value
  1587. * @size: The size of the buffer(by words(32bits), not characters)
  1588. * @fmt: The format string to use
  1589. * @...: Arguments for the format string
  1590. *
  1591. * The function returns the number of words(u32) written
  1592. * into @bin_buf.
  1593. */
  1594. int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
  1595. {
  1596. va_list args;
  1597. int ret;
  1598. va_start(args, fmt);
  1599. ret = vbin_printf(bin_buf, size, fmt, args);
  1600. va_end(args);
  1601. return ret;
  1602. }
  1603. EXPORT_SYMBOL_GPL(bprintf);
  1604. #endif /* CONFIG_BINARY_PRINTF */
  1605. /**
  1606. * vsscanf - Unformat a buffer into a list of arguments
  1607. * @buf: input buffer
  1608. * @fmt: format of buffer
  1609. * @args: arguments
  1610. */
  1611. int vsscanf(const char *buf, const char *fmt, va_list args)
  1612. {
  1613. const char *str = buf;
  1614. char *next;
  1615. char digit;
  1616. int num = 0;
  1617. u8 qualifier;
  1618. u8 base;
  1619. s16 field_width;
  1620. bool is_sign;
  1621. while (*fmt && *str) {
  1622. /* skip any white space in format */
  1623. /* white space in format matchs any amount of
  1624. * white space, including none, in the input.
  1625. */
  1626. if (isspace(*fmt)) {
  1627. fmt = skip_spaces(++fmt);
  1628. str = skip_spaces(str);
  1629. }
  1630. /* anything that is not a conversion must match exactly */
  1631. if (*fmt != '%' && *fmt) {
  1632. if (*fmt++ != *str++)
  1633. break;
  1634. continue;
  1635. }
  1636. if (!*fmt)
  1637. break;
  1638. ++fmt;
  1639. /* skip this conversion.
  1640. * advance both strings to next white space
  1641. */
  1642. if (*fmt == '*') {
  1643. while (!isspace(*fmt) && *fmt != '%' && *fmt)
  1644. fmt++;
  1645. while (!isspace(*str) && *str)
  1646. str++;
  1647. continue;
  1648. }
  1649. /* get field width */
  1650. field_width = -1;
  1651. if (isdigit(*fmt))
  1652. field_width = skip_atoi(&fmt);
  1653. /* get conversion qualifier */
  1654. qualifier = -1;
  1655. if (*fmt == 'h' || TOLOWER(*fmt) == 'l' ||
  1656. TOLOWER(*fmt) == 'z') {
  1657. qualifier = *fmt++;
  1658. if (unlikely(qualifier == *fmt)) {
  1659. if (qualifier == 'h') {
  1660. qualifier = 'H';
  1661. fmt++;
  1662. } else if (qualifier == 'l') {
  1663. qualifier = 'L';
  1664. fmt++;
  1665. }
  1666. }
  1667. }
  1668. if (!*fmt || !*str)
  1669. break;
  1670. base = 10;
  1671. is_sign = 0;
  1672. switch (*fmt++) {
  1673. case 'c':
  1674. {
  1675. char *s = (char *)va_arg(args, char*);
  1676. if (field_width == -1)
  1677. field_width = 1;
  1678. do {
  1679. *s++ = *str++;
  1680. } while (--field_width > 0 && *str);
  1681. num++;
  1682. }
  1683. continue;
  1684. case 's':
  1685. {
  1686. char *s = (char *)va_arg(args, char *);
  1687. if (field_width == -1)
  1688. field_width = SHRT_MAX;
  1689. /* first, skip leading white space in buffer */
  1690. str = skip_spaces(str);
  1691. /* now copy until next white space */
  1692. while (*str && !isspace(*str) && field_width--)
  1693. *s++ = *str++;
  1694. *s = '\0';
  1695. num++;
  1696. }
  1697. continue;
  1698. case 'n':
  1699. /* return number of characters read so far */
  1700. {
  1701. int *i = (int *)va_arg(args, int*);
  1702. *i = str - buf;
  1703. }
  1704. continue;
  1705. case 'o':
  1706. base = 8;
  1707. break;
  1708. case 'x':
  1709. case 'X':
  1710. base = 16;
  1711. break;
  1712. case 'i':
  1713. base = 0;
  1714. case 'd':
  1715. is_sign = 1;
  1716. case 'u':
  1717. break;
  1718. case '%':
  1719. /* looking for '%' in str */
  1720. if (*str++ != '%')
  1721. return num;
  1722. continue;
  1723. default:
  1724. /* invalid format; stop here */
  1725. return num;
  1726. }
  1727. /* have some sort of integer conversion.
  1728. * first, skip white space in buffer.
  1729. */
  1730. str = skip_spaces(str);
  1731. digit = *str;
  1732. if (is_sign && digit == '-')
  1733. digit = *(str + 1);
  1734. if (!digit
  1735. || (base == 16 && !isxdigit(digit))
  1736. || (base == 10 && !isdigit(digit))
  1737. || (base == 8 && (!isdigit(digit) || digit > '7'))
  1738. || (base == 0 && !isdigit(digit)))
  1739. break;
  1740. switch (qualifier) {
  1741. case 'H': /* that's 'hh' in format */
  1742. if (is_sign) {
  1743. signed char *s = (signed char *)va_arg(args, signed char *);
  1744. *s = (signed char)simple_strtol(str, &next, base);
  1745. } else {
  1746. unsigned char *s = (unsigned char *)va_arg(args, unsigned char *);
  1747. *s = (unsigned char)simple_strtoul(str, &next, base);
  1748. }
  1749. break;
  1750. case 'h':
  1751. if (is_sign) {
  1752. short *s = (short *)va_arg(args, short *);
  1753. *s = (short)simple_strtol(str, &next, base);
  1754. } else {
  1755. unsigned short *s = (unsigned short *)va_arg(args, unsigned short *);
  1756. *s = (unsigned short)simple_strtoul(str, &next, base);
  1757. }
  1758. break;
  1759. case 'l':
  1760. if (is_sign) {
  1761. long *l = (long *)va_arg(args, long *);
  1762. *l = simple_strtol(str, &next, base);
  1763. } else {
  1764. unsigned long *l = (unsigned long *)va_arg(args, unsigned long *);
  1765. *l = simple_strtoul(str, &next, base);
  1766. }
  1767. break;
  1768. case 'L':
  1769. if (is_sign) {
  1770. long long *l = (long long *)va_arg(args, long long *);
  1771. *l = simple_strtoll(str, &next, base);
  1772. } else {
  1773. unsigned long long *l = (unsigned long long *)va_arg(args, unsigned long long *);
  1774. *l = simple_strtoull(str, &next, base);
  1775. }
  1776. break;
  1777. case 'Z':
  1778. case 'z':
  1779. {
  1780. size_t *s = (size_t *)va_arg(args, size_t *);
  1781. *s = (size_t)simple_strtoul(str, &next, base);
  1782. }
  1783. break;
  1784. default:
  1785. if (is_sign) {
  1786. int *i = (int *)va_arg(args, int *);
  1787. *i = (int)simple_strtol(str, &next, base);
  1788. } else {
  1789. unsigned int *i = (unsigned int *)va_arg(args, unsigned int*);
  1790. *i = (unsigned int)simple_strtoul(str, &next, base);
  1791. }
  1792. break;
  1793. }
  1794. num++;
  1795. if (!next)
  1796. break;
  1797. str = next;
  1798. }
  1799. /*
  1800. * Now we've come all the way through so either the input string or the
  1801. * format ended. In the former case, there can be a %n at the current
  1802. * position in the format that needs to be filled.
  1803. */
  1804. if (*fmt == '%' && *(fmt + 1) == 'n') {
  1805. int *p = (int *)va_arg(args, int *);
  1806. *p = str - buf;
  1807. }
  1808. return num;
  1809. }
  1810. EXPORT_SYMBOL(vsscanf);
  1811. /**
  1812. * sscanf - Unformat a buffer into a list of arguments
  1813. * @buf: input buffer
  1814. * @fmt: formatting of buffer
  1815. * @...: resulting arguments
  1816. */
  1817. int sscanf(const char *buf, const char *fmt, ...)
  1818. {
  1819. va_list args;
  1820. int i;
  1821. va_start(args, fmt);
  1822. i = vsscanf(buf, fmt, args);
  1823. va_end(args);
  1824. return i;
  1825. }
  1826. EXPORT_SYMBOL(sscanf);