vsprintf.c 46 KB

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