vsprintf.c 51 KB

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