vsprintf.c 51 KB

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