vsprintf.c 50 KB

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