vsprintf.c 48 KB

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