vsprintf.c 47 KB

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