vsprintf.c 43 KB

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