vsprintf.c 46 KB

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