vsprintf.c 48 KB

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