vsprintf.c 46 KB

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