vsprintf.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856
  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_WITDH,
  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_USHORT,
  367. FORMAT_TYPE_SHORT,
  368. FORMAT_TYPE_UINT,
  369. FORMAT_TYPE_INT,
  370. FORMAT_TYPE_NRCHARS,
  371. FORMAT_TYPE_SIZE_T,
  372. FORMAT_TYPE_PTRDIFF
  373. };
  374. struct printf_spec {
  375. enum format_type type;
  376. int flags; /* flags to number() */
  377. int field_width; /* width of output field */
  378. int base;
  379. int precision; /* # of digits/chars */
  380. int qualifier;
  381. };
  382. static char *number(char *buf, char *end, unsigned long long num,
  383. struct printf_spec spec)
  384. {
  385. /* we are called with base 8, 10 or 16, only, thus don't need "G..." */
  386. static const char digits[16] = "0123456789ABCDEF"; /* "GHIJKLMNOPQRSTUVWXYZ"; */
  387. char tmp[66];
  388. char sign;
  389. char locase;
  390. int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
  391. int i;
  392. /* locase = 0 or 0x20. ORing digits or letters with 'locase'
  393. * produces same digits or (maybe lowercased) letters */
  394. locase = (spec.flags & SMALL);
  395. if (spec.flags & LEFT)
  396. spec.flags &= ~ZEROPAD;
  397. sign = 0;
  398. if (spec.flags & SIGN) {
  399. if ((signed long long) num < 0) {
  400. sign = '-';
  401. num = - (signed long long) num;
  402. spec.field_width--;
  403. } else if (spec.flags & PLUS) {
  404. sign = '+';
  405. spec.field_width--;
  406. } else if (spec.flags & SPACE) {
  407. sign = ' ';
  408. spec.field_width--;
  409. }
  410. }
  411. if (need_pfx) {
  412. spec.field_width--;
  413. if (spec.base == 16)
  414. spec.field_width--;
  415. }
  416. /* generate full string in tmp[], in reverse order */
  417. i = 0;
  418. if (num == 0)
  419. tmp[i++] = '0';
  420. /* Generic code, for any base:
  421. else do {
  422. tmp[i++] = (digits[do_div(num,base)] | locase);
  423. } while (num != 0);
  424. */
  425. else if (spec.base != 10) { /* 8 or 16 */
  426. int mask = spec.base - 1;
  427. int shift = 3;
  428. if (spec.base == 16) shift = 4;
  429. do {
  430. tmp[i++] = (digits[((unsigned char)num) & mask] | locase);
  431. num >>= shift;
  432. } while (num);
  433. } else { /* base 10 */
  434. i = put_dec(tmp, num) - tmp;
  435. }
  436. /* printing 100 using %2d gives "100", not "00" */
  437. if (i > spec.precision)
  438. spec.precision = i;
  439. /* leading space padding */
  440. spec.field_width -= spec.precision;
  441. if (!(spec.flags & (ZEROPAD+LEFT))) {
  442. while(--spec.field_width >= 0) {
  443. if (buf < end)
  444. *buf = ' ';
  445. ++buf;
  446. }
  447. }
  448. /* sign */
  449. if (sign) {
  450. if (buf < end)
  451. *buf = sign;
  452. ++buf;
  453. }
  454. /* "0x" / "0" prefix */
  455. if (need_pfx) {
  456. if (buf < end)
  457. *buf = '0';
  458. ++buf;
  459. if (spec.base == 16) {
  460. if (buf < end)
  461. *buf = ('X' | locase);
  462. ++buf;
  463. }
  464. }
  465. /* zero or space padding */
  466. if (!(spec.flags & LEFT)) {
  467. char c = (spec.flags & ZEROPAD) ? '0' : ' ';
  468. while (--spec.field_width >= 0) {
  469. if (buf < end)
  470. *buf = c;
  471. ++buf;
  472. }
  473. }
  474. /* hmm even more zero padding? */
  475. while (i <= --spec.precision) {
  476. if (buf < end)
  477. *buf = '0';
  478. ++buf;
  479. }
  480. /* actual digits of result */
  481. while (--i >= 0) {
  482. if (buf < end)
  483. *buf = tmp[i];
  484. ++buf;
  485. }
  486. /* trailing space padding */
  487. while (--spec.field_width >= 0) {
  488. if (buf < end)
  489. *buf = ' ';
  490. ++buf;
  491. }
  492. return buf;
  493. }
  494. static char *string(char *buf, char *end, char *s, struct printf_spec spec)
  495. {
  496. int len, i;
  497. if ((unsigned long)s < PAGE_SIZE)
  498. s = "<NULL>";
  499. len = strnlen(s, spec.precision);
  500. if (!(spec.flags & LEFT)) {
  501. while (len < spec.field_width--) {
  502. if (buf < end)
  503. *buf = ' ';
  504. ++buf;
  505. }
  506. }
  507. for (i = 0; i < len; ++i) {
  508. if (buf < end)
  509. *buf = *s;
  510. ++buf; ++s;
  511. }
  512. while (len < spec.field_width--) {
  513. if (buf < end)
  514. *buf = ' ';
  515. ++buf;
  516. }
  517. return buf;
  518. }
  519. static char *symbol_string(char *buf, char *end, void *ptr,
  520. struct printf_spec spec)
  521. {
  522. unsigned long value = (unsigned long) ptr;
  523. #ifdef CONFIG_KALLSYMS
  524. char sym[KSYM_SYMBOL_LEN];
  525. sprint_symbol(sym, value);
  526. return string(buf, end, sym, spec);
  527. #else
  528. spec.field_width = 2*sizeof(void *);
  529. spec.flags |= SPECIAL | SMALL | ZEROPAD;
  530. spec.base = 16;
  531. return number(buf, end, value, spec);
  532. #endif
  533. }
  534. static char *resource_string(char *buf, char *end, struct resource *res,
  535. struct printf_spec spec)
  536. {
  537. #ifndef IO_RSRC_PRINTK_SIZE
  538. #define IO_RSRC_PRINTK_SIZE 4
  539. #endif
  540. #ifndef MEM_RSRC_PRINTK_SIZE
  541. #define MEM_RSRC_PRINTK_SIZE 8
  542. #endif
  543. struct printf_spec num_spec = {
  544. .base = 16,
  545. .precision = -1,
  546. .flags = SPECIAL | SMALL | ZEROPAD,
  547. };
  548. /* room for the actual numbers, the two "0x", -, [, ] and the final zero */
  549. char sym[4*sizeof(resource_size_t) + 8];
  550. char *p = sym, *pend = sym + sizeof(sym);
  551. int size = -1;
  552. if (res->flags & IORESOURCE_IO)
  553. size = IO_RSRC_PRINTK_SIZE;
  554. else if (res->flags & IORESOURCE_MEM)
  555. size = MEM_RSRC_PRINTK_SIZE;
  556. *p++ = '[';
  557. num_spec.field_width = size;
  558. p = number(p, pend, res->start, num_spec);
  559. *p++ = '-';
  560. p = number(p, pend, res->end, num_spec);
  561. *p++ = ']';
  562. *p = 0;
  563. return string(buf, end, sym, spec);
  564. }
  565. static char *mac_address_string(char *buf, char *end, u8 *addr,
  566. struct printf_spec spec)
  567. {
  568. char mac_addr[6 * 3]; /* (6 * 2 hex digits), 5 colons and trailing zero */
  569. char *p = mac_addr;
  570. int i;
  571. for (i = 0; i < 6; i++) {
  572. p = pack_hex_byte(p, addr[i]);
  573. if (!(spec.flags & SPECIAL) && i != 5)
  574. *p++ = ':';
  575. }
  576. *p = '\0';
  577. spec.flags &= ~SPECIAL;
  578. return string(buf, end, mac_addr, spec);
  579. }
  580. static char *ip6_addr_string(char *buf, char *end, u8 *addr,
  581. struct printf_spec spec)
  582. {
  583. char ip6_addr[8 * 5]; /* (8 * 4 hex digits), 7 colons and trailing zero */
  584. char *p = ip6_addr;
  585. int i;
  586. for (i = 0; i < 8; i++) {
  587. p = pack_hex_byte(p, addr[2 * i]);
  588. p = pack_hex_byte(p, addr[2 * i + 1]);
  589. if (!(spec.flags & SPECIAL) && i != 7)
  590. *p++ = ':';
  591. }
  592. *p = '\0';
  593. spec.flags &= ~SPECIAL;
  594. return string(buf, end, ip6_addr, spec);
  595. }
  596. static char *ip4_addr_string(char *buf, char *end, u8 *addr,
  597. struct printf_spec spec)
  598. {
  599. char ip4_addr[4 * 4]; /* (4 * 3 decimal digits), 3 dots and trailing zero */
  600. char temp[3]; /* hold each IP quad in reverse order */
  601. char *p = ip4_addr;
  602. int i, digits;
  603. for (i = 0; i < 4; i++) {
  604. digits = put_dec_trunc(temp, addr[i]) - temp;
  605. /* reverse the digits in the quad */
  606. while (digits--)
  607. *p++ = temp[digits];
  608. if (i != 3)
  609. *p++ = '.';
  610. }
  611. *p = '\0';
  612. spec.flags &= ~SPECIAL;
  613. return string(buf, end, ip4_addr, spec);
  614. }
  615. /*
  616. * Show a '%p' thing. A kernel extension is that the '%p' is followed
  617. * by an extra set of alphanumeric characters that are extended format
  618. * specifiers.
  619. *
  620. * Right now we handle:
  621. *
  622. * - 'F' For symbolic function descriptor pointers
  623. * - 'S' For symbolic direct pointers
  624. * - 'R' For a struct resource pointer, it prints the range of
  625. * addresses (not the name nor the flags)
  626. * - 'M' For a 6-byte MAC address, it prints the address in the
  627. * usual colon-separated hex notation
  628. * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way (dot-separated
  629. * decimal for v4 and colon separated network-order 16 bit hex for v6)
  630. * - 'i' [46] for 'raw' IPv4/IPv6 addresses, IPv6 omits the colons, IPv4 is
  631. * currently the same
  632. *
  633. * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
  634. * function pointers are really function descriptors, which contain a
  635. * pointer to the real address.
  636. */
  637. static char *pointer(const char *fmt, char *buf, char *end, void *ptr,
  638. struct printf_spec spec)
  639. {
  640. if (!ptr)
  641. return string(buf, end, "(null)", spec);
  642. switch (*fmt) {
  643. case 'F':
  644. ptr = dereference_function_descriptor(ptr);
  645. /* Fallthrough */
  646. case 'S':
  647. return symbol_string(buf, end, ptr, spec);
  648. case 'R':
  649. return resource_string(buf, end, ptr, spec);
  650. case 'm':
  651. spec.flags |= SPECIAL;
  652. /* Fallthrough */
  653. case 'M':
  654. return mac_address_string(buf, end, ptr, spec);
  655. case 'i':
  656. spec.flags |= SPECIAL;
  657. /* Fallthrough */
  658. case 'I':
  659. if (fmt[1] == '6')
  660. return ip6_addr_string(buf, end, ptr, spec);
  661. if (fmt[1] == '4')
  662. return ip4_addr_string(buf, end, ptr, spec);
  663. spec.flags &= ~SPECIAL;
  664. break;
  665. }
  666. spec.flags |= SMALL;
  667. if (spec.field_width == -1) {
  668. spec.field_width = 2*sizeof(void *);
  669. spec.flags |= ZEROPAD;
  670. }
  671. spec.base = 16;
  672. return number(buf, end, (unsigned long) ptr, spec);
  673. }
  674. /*
  675. * Helper function to decode printf style format.
  676. * Each call decode a token from the format and return the
  677. * number of characters read (or likely the delta where it wants
  678. * to go on the next call).
  679. * The decoded token is returned through the parameters
  680. *
  681. * 'h', 'l', or 'L' for integer fields
  682. * 'z' support added 23/7/1999 S.H.
  683. * 'z' changed to 'Z' --davidm 1/25/99
  684. * 't' added for ptrdiff_t
  685. *
  686. * @fmt: the format string
  687. * @type of the token returned
  688. * @flags: various flags such as +, -, # tokens..
  689. * @field_width: overwritten width
  690. * @base: base of the number (octal, hex, ...)
  691. * @precision: precision of a number
  692. * @qualifier: qualifier of a number (long, size_t, ...)
  693. */
  694. static int format_decode(const char *fmt, struct printf_spec *spec)
  695. {
  696. const char *start = fmt;
  697. /* we finished early by reading the field width */
  698. if (spec->type == FORMAT_TYPE_WITDH) {
  699. if (spec->field_width < 0) {
  700. spec->field_width = -spec->field_width;
  701. spec->flags |= LEFT;
  702. }
  703. spec->type = FORMAT_TYPE_NONE;
  704. goto precision;
  705. }
  706. /* we finished early by reading the precision */
  707. if (spec->type == FORMAT_TYPE_PRECISION) {
  708. if (spec->precision < 0)
  709. spec->precision = 0;
  710. spec->type = FORMAT_TYPE_NONE;
  711. goto qualifier;
  712. }
  713. /* By default */
  714. spec->type = FORMAT_TYPE_NONE;
  715. for (; *fmt ; ++fmt) {
  716. if (*fmt == '%')
  717. break;
  718. }
  719. /* Return the current non-format string */
  720. if (fmt != start || !*fmt)
  721. return fmt - start;
  722. /* Process flags */
  723. spec->flags = 0;
  724. while (1) { /* this also skips first '%' */
  725. bool found = true;
  726. ++fmt;
  727. switch (*fmt) {
  728. case '-': spec->flags |= LEFT; break;
  729. case '+': spec->flags |= PLUS; break;
  730. case ' ': spec->flags |= SPACE; break;
  731. case '#': spec->flags |= SPECIAL; break;
  732. case '0': spec->flags |= ZEROPAD; break;
  733. default: found = false;
  734. }
  735. if (!found)
  736. break;
  737. }
  738. /* get field width */
  739. spec->field_width = -1;
  740. if (isdigit(*fmt))
  741. spec->field_width = skip_atoi(&fmt);
  742. else if (*fmt == '*') {
  743. /* it's the next argument */
  744. spec->type = FORMAT_TYPE_WITDH;
  745. return ++fmt - start;
  746. }
  747. precision:
  748. /* get the precision */
  749. spec->precision = -1;
  750. if (*fmt == '.') {
  751. ++fmt;
  752. if (isdigit(*fmt)) {
  753. spec->precision = skip_atoi(&fmt);
  754. if (spec->precision < 0)
  755. spec->precision = 0;
  756. } else if (*fmt == '*') {
  757. /* it's the next argument */
  758. spec->type = FORMAT_TYPE_PRECISION;
  759. return ++fmt - start;
  760. }
  761. }
  762. qualifier:
  763. /* get the conversion qualifier */
  764. spec->qualifier = -1;
  765. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  766. *fmt == 'Z' || *fmt == 'z' || *fmt == 't') {
  767. spec->qualifier = *fmt;
  768. ++fmt;
  769. if (spec->qualifier == 'l' && *fmt == 'l') {
  770. spec->qualifier = 'L';
  771. ++fmt;
  772. }
  773. }
  774. /* default base */
  775. spec->base = 10;
  776. switch (*fmt) {
  777. case 'c':
  778. spec->type = FORMAT_TYPE_CHAR;
  779. return ++fmt - start;
  780. case 's':
  781. spec->type = FORMAT_TYPE_STR;
  782. return ++fmt - start;
  783. case 'p':
  784. spec->type = FORMAT_TYPE_PTR;
  785. return fmt - start;
  786. /* skip alnum */
  787. case 'n':
  788. spec->type = FORMAT_TYPE_NRCHARS;
  789. return ++fmt - start;
  790. case '%':
  791. spec->type = FORMAT_TYPE_PERCENT_CHAR;
  792. return ++fmt - start;
  793. /* integer number formats - set up the flags and "break" */
  794. case 'o':
  795. spec->base = 8;
  796. break;
  797. case 'x':
  798. spec->flags |= SMALL;
  799. case 'X':
  800. spec->base = 16;
  801. break;
  802. case 'd':
  803. case 'i':
  804. spec->flags |= SIGN;
  805. case 'u':
  806. break;
  807. default:
  808. spec->type = FORMAT_TYPE_INVALID;
  809. return fmt - start;
  810. }
  811. if (spec->qualifier == 'L')
  812. spec->type = FORMAT_TYPE_LONG_LONG;
  813. else if (spec->qualifier == 'l') {
  814. if (spec->flags & SIGN)
  815. spec->type = FORMAT_TYPE_LONG;
  816. else
  817. spec->type = FORMAT_TYPE_ULONG;
  818. } else if (spec->qualifier == 'Z' || spec->qualifier == 'z') {
  819. spec->type = FORMAT_TYPE_SIZE_T;
  820. } else if (spec->qualifier == 't') {
  821. spec->type = FORMAT_TYPE_PTRDIFF;
  822. } else if (spec->qualifier == 'h') {
  823. if (spec->flags & SIGN)
  824. spec->type = FORMAT_TYPE_SHORT;
  825. else
  826. spec->type = FORMAT_TYPE_USHORT;
  827. } else {
  828. if (spec->flags & SIGN)
  829. spec->type = FORMAT_TYPE_INT;
  830. else
  831. spec->type = FORMAT_TYPE_UINT;
  832. }
  833. return ++fmt - start;
  834. }
  835. /**
  836. * vsnprintf - Format a string and place it in a buffer
  837. * @buf: The buffer to place the result into
  838. * @size: The size of the buffer, including the trailing null space
  839. * @fmt: The format string to use
  840. * @args: Arguments for the format string
  841. *
  842. * This function follows C99 vsnprintf, but has some extensions:
  843. * %pS output the name of a text symbol
  844. * %pF output the name of a function pointer
  845. * %pR output the address range in a struct resource
  846. *
  847. * The return value is the number of characters which would
  848. * be generated for the given input, excluding the trailing
  849. * '\0', as per ISO C99. If you want to have the exact
  850. * number of characters written into @buf as return value
  851. * (not including the trailing '\0'), use vscnprintf(). If the
  852. * return is greater than or equal to @size, the resulting
  853. * string is truncated.
  854. *
  855. * Call this function if you are already dealing with a va_list.
  856. * You probably want snprintf() instead.
  857. */
  858. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  859. {
  860. unsigned long long num;
  861. char *str, *end, c;
  862. int read;
  863. struct printf_spec spec = {0};
  864. /* Reject out-of-range values early. Large positive sizes are
  865. used for unknown buffer sizes. */
  866. if (unlikely((int) size < 0)) {
  867. /* There can be only one.. */
  868. static char warn = 1;
  869. WARN_ON(warn);
  870. warn = 0;
  871. return 0;
  872. }
  873. str = buf;
  874. end = buf + size;
  875. /* Make sure end is always >= buf */
  876. if (end < buf) {
  877. end = ((void *)-1);
  878. size = end - buf;
  879. }
  880. while (*fmt) {
  881. const char *old_fmt = fmt;
  882. read = format_decode(fmt, &spec);
  883. fmt += read;
  884. switch (spec.type) {
  885. case FORMAT_TYPE_NONE: {
  886. int copy = read;
  887. if (str < end) {
  888. if (copy > end - str)
  889. copy = end - str;
  890. memcpy(str, old_fmt, copy);
  891. }
  892. str += read;
  893. break;
  894. }
  895. case FORMAT_TYPE_WITDH:
  896. spec.field_width = va_arg(args, int);
  897. break;
  898. case FORMAT_TYPE_PRECISION:
  899. spec.precision = va_arg(args, int);
  900. break;
  901. case FORMAT_TYPE_CHAR:
  902. if (!(spec.flags & LEFT)) {
  903. while (--spec.field_width > 0) {
  904. if (str < end)
  905. *str = ' ';
  906. ++str;
  907. }
  908. }
  909. c = (unsigned char) va_arg(args, int);
  910. if (str < end)
  911. *str = c;
  912. ++str;
  913. while (--spec.field_width > 0) {
  914. if (str < end)
  915. *str = ' ';
  916. ++str;
  917. }
  918. break;
  919. case FORMAT_TYPE_STR:
  920. str = string(str, end, va_arg(args, char *), spec);
  921. break;
  922. case FORMAT_TYPE_PTR:
  923. str = pointer(fmt+1, str, end, va_arg(args, void *),
  924. spec);
  925. while (isalnum(*fmt))
  926. fmt++;
  927. break;
  928. case FORMAT_TYPE_PERCENT_CHAR:
  929. if (str < end)
  930. *str = '%';
  931. ++str;
  932. break;
  933. case FORMAT_TYPE_INVALID:
  934. if (str < end)
  935. *str = '%';
  936. ++str;
  937. if (*fmt) {
  938. if (str < end)
  939. *str = *fmt;
  940. ++str;
  941. } else {
  942. --fmt;
  943. }
  944. break;
  945. case FORMAT_TYPE_NRCHARS: {
  946. int qualifier = spec.qualifier;
  947. if (qualifier == 'l') {
  948. long *ip = va_arg(args, long *);
  949. *ip = (str - buf);
  950. } else if (qualifier == 'Z' ||
  951. qualifier == 'z') {
  952. size_t *ip = va_arg(args, size_t *);
  953. *ip = (str - buf);
  954. } else {
  955. int *ip = va_arg(args, int *);
  956. *ip = (str - buf);
  957. }
  958. break;
  959. }
  960. default:
  961. switch (spec.type) {
  962. case FORMAT_TYPE_LONG_LONG:
  963. num = va_arg(args, long long);
  964. break;
  965. case FORMAT_TYPE_ULONG:
  966. num = va_arg(args, unsigned long);
  967. break;
  968. case FORMAT_TYPE_LONG:
  969. num = va_arg(args, long);
  970. break;
  971. case FORMAT_TYPE_SIZE_T:
  972. num = va_arg(args, size_t);
  973. break;
  974. case FORMAT_TYPE_PTRDIFF:
  975. num = va_arg(args, ptrdiff_t);
  976. break;
  977. case FORMAT_TYPE_USHORT:
  978. num = (unsigned short) va_arg(args, int);
  979. break;
  980. case FORMAT_TYPE_SHORT:
  981. num = (short) va_arg(args, int);
  982. break;
  983. case FORMAT_TYPE_INT:
  984. num = (int) va_arg(args, int);
  985. break;
  986. default:
  987. num = va_arg(args, unsigned int);
  988. }
  989. str = number(str, end, num, spec);
  990. }
  991. }
  992. if (size > 0) {
  993. if (str < end)
  994. *str = '\0';
  995. else
  996. end[-1] = '\0';
  997. }
  998. /* the trailing null byte doesn't count towards the total */
  999. return str-buf;
  1000. }
  1001. EXPORT_SYMBOL(vsnprintf);
  1002. /**
  1003. * vscnprintf - Format a string and place it in a buffer
  1004. * @buf: The buffer to place the result into
  1005. * @size: The size of the buffer, including the trailing null space
  1006. * @fmt: The format string to use
  1007. * @args: Arguments for the format string
  1008. *
  1009. * The return value is the number of characters which have been written into
  1010. * the @buf not including the trailing '\0'. If @size is <= 0 the function
  1011. * returns 0.
  1012. *
  1013. * Call this function if you are already dealing with a va_list.
  1014. * You probably want scnprintf() instead.
  1015. *
  1016. * See the vsnprintf() documentation for format string extensions over C99.
  1017. */
  1018. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  1019. {
  1020. int i;
  1021. i=vsnprintf(buf,size,fmt,args);
  1022. return (i >= size) ? (size - 1) : i;
  1023. }
  1024. EXPORT_SYMBOL(vscnprintf);
  1025. /**
  1026. * snprintf - Format a string and place it in a buffer
  1027. * @buf: The buffer to place the result into
  1028. * @size: The size of the buffer, including the trailing null space
  1029. * @fmt: The format string to use
  1030. * @...: Arguments for the format string
  1031. *
  1032. * The return value is the number of characters which would be
  1033. * generated for the given input, excluding the trailing null,
  1034. * as per ISO C99. If the return is greater than or equal to
  1035. * @size, the resulting string is truncated.
  1036. *
  1037. * See the vsnprintf() documentation for format string extensions over C99.
  1038. */
  1039. int snprintf(char * buf, size_t size, const char *fmt, ...)
  1040. {
  1041. va_list args;
  1042. int i;
  1043. va_start(args, fmt);
  1044. i=vsnprintf(buf,size,fmt,args);
  1045. va_end(args);
  1046. return i;
  1047. }
  1048. EXPORT_SYMBOL(snprintf);
  1049. /**
  1050. * scnprintf - Format a string and place it in a buffer
  1051. * @buf: The buffer to place the result into
  1052. * @size: The size of the buffer, including the trailing null space
  1053. * @fmt: The format string to use
  1054. * @...: Arguments for the format string
  1055. *
  1056. * The return value is the number of characters written into @buf not including
  1057. * the trailing '\0'. If @size is <= 0 the function returns 0.
  1058. */
  1059. int scnprintf(char * buf, size_t size, const char *fmt, ...)
  1060. {
  1061. va_list args;
  1062. int i;
  1063. va_start(args, fmt);
  1064. i = vsnprintf(buf, size, fmt, args);
  1065. va_end(args);
  1066. return (i >= size) ? (size - 1) : i;
  1067. }
  1068. EXPORT_SYMBOL(scnprintf);
  1069. /**
  1070. * vsprintf - Format a string and place it in a buffer
  1071. * @buf: The buffer to place the result into
  1072. * @fmt: The format string to use
  1073. * @args: Arguments for the format string
  1074. *
  1075. * The function returns the number of characters written
  1076. * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
  1077. * buffer overflows.
  1078. *
  1079. * Call this function if you are already dealing with a va_list.
  1080. * You probably want sprintf() instead.
  1081. *
  1082. * See the vsnprintf() documentation for format string extensions over C99.
  1083. */
  1084. int vsprintf(char *buf, const char *fmt, va_list args)
  1085. {
  1086. return vsnprintf(buf, INT_MAX, fmt, args);
  1087. }
  1088. EXPORT_SYMBOL(vsprintf);
  1089. /**
  1090. * sprintf - Format a string and place it in a buffer
  1091. * @buf: The buffer to place the result into
  1092. * @fmt: The format string to use
  1093. * @...: Arguments for the format string
  1094. *
  1095. * The function returns the number of characters written
  1096. * into @buf. Use snprintf() or scnprintf() in order to avoid
  1097. * buffer overflows.
  1098. *
  1099. * See the vsnprintf() documentation for format string extensions over C99.
  1100. */
  1101. int sprintf(char * buf, const char *fmt, ...)
  1102. {
  1103. va_list args;
  1104. int i;
  1105. va_start(args, fmt);
  1106. i=vsnprintf(buf, INT_MAX, fmt, args);
  1107. va_end(args);
  1108. return i;
  1109. }
  1110. EXPORT_SYMBOL(sprintf);
  1111. #ifdef CONFIG_BINARY_PRINTF
  1112. /*
  1113. * bprintf service:
  1114. * vbin_printf() - VA arguments to binary data
  1115. * bstr_printf() - Binary data to text string
  1116. */
  1117. /**
  1118. * vbin_printf - Parse a format string and place args' binary value in a buffer
  1119. * @bin_buf: The buffer to place args' binary value
  1120. * @size: The size of the buffer(by words(32bits), not characters)
  1121. * @fmt: The format string to use
  1122. * @args: Arguments for the format string
  1123. *
  1124. * The format follows C99 vsnprintf, except %n is ignored, and its argument
  1125. * is skiped.
  1126. *
  1127. * The return value is the number of words(32bits) which would be generated for
  1128. * the given input.
  1129. *
  1130. * NOTE:
  1131. * If the return value is greater than @size, the resulting bin_buf is NOT
  1132. * valid for bstr_printf().
  1133. */
  1134. int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
  1135. {
  1136. struct printf_spec spec = {0};
  1137. char *str, *end;
  1138. int read;
  1139. str = (char *)bin_buf;
  1140. end = (char *)(bin_buf + size);
  1141. #define save_arg(type) \
  1142. do { \
  1143. if (sizeof(type) == 8) { \
  1144. unsigned long long value; \
  1145. str = PTR_ALIGN(str, sizeof(u32)); \
  1146. value = va_arg(args, unsigned long long); \
  1147. if (str + sizeof(type) <= end) { \
  1148. *(u32 *)str = *(u32 *)&value; \
  1149. *(u32 *)(str + 4) = *((u32 *)&value + 1); \
  1150. } \
  1151. } else { \
  1152. unsigned long value; \
  1153. str = PTR_ALIGN(str, sizeof(type)); \
  1154. value = va_arg(args, int); \
  1155. if (str + sizeof(type) <= end) \
  1156. *(typeof(type) *)str = (type)value; \
  1157. } \
  1158. str += sizeof(type); \
  1159. } while (0)
  1160. while (*fmt) {
  1161. read = format_decode(fmt, &spec);
  1162. fmt += read;
  1163. switch (spec.type) {
  1164. case FORMAT_TYPE_NONE:
  1165. break;
  1166. case FORMAT_TYPE_WITDH:
  1167. case FORMAT_TYPE_PRECISION:
  1168. save_arg(int);
  1169. break;
  1170. case FORMAT_TYPE_CHAR:
  1171. save_arg(char);
  1172. break;
  1173. case FORMAT_TYPE_STR: {
  1174. const char *save_str = va_arg(args, char *);
  1175. size_t len;
  1176. if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
  1177. || (unsigned long)save_str < PAGE_SIZE)
  1178. save_str = "<NULL>";
  1179. len = strlen(save_str);
  1180. if (str + len + 1 < end)
  1181. memcpy(str, save_str, len + 1);
  1182. str += len + 1;
  1183. break;
  1184. }
  1185. case FORMAT_TYPE_PTR:
  1186. save_arg(void *);
  1187. /* skip all alphanumeric pointer suffixes */
  1188. while (isalnum(*fmt))
  1189. fmt++;
  1190. break;
  1191. case FORMAT_TYPE_PERCENT_CHAR:
  1192. break;
  1193. case FORMAT_TYPE_INVALID:
  1194. if (!*fmt)
  1195. --fmt;
  1196. break;
  1197. case FORMAT_TYPE_NRCHARS: {
  1198. /* skip %n 's argument */
  1199. int qualifier = spec.qualifier;
  1200. void *skip_arg;
  1201. if (qualifier == 'l')
  1202. skip_arg = va_arg(args, long *);
  1203. else if (qualifier == 'Z' || qualifier == 'z')
  1204. skip_arg = va_arg(args, size_t *);
  1205. else
  1206. skip_arg = va_arg(args, int *);
  1207. break;
  1208. }
  1209. default:
  1210. switch (spec.type) {
  1211. case FORMAT_TYPE_LONG_LONG:
  1212. save_arg(long long);
  1213. break;
  1214. case FORMAT_TYPE_ULONG:
  1215. case FORMAT_TYPE_LONG:
  1216. save_arg(unsigned long);
  1217. break;
  1218. case FORMAT_TYPE_SIZE_T:
  1219. save_arg(size_t);
  1220. break;
  1221. case FORMAT_TYPE_PTRDIFF:
  1222. save_arg(ptrdiff_t);
  1223. break;
  1224. case FORMAT_TYPE_USHORT:
  1225. case FORMAT_TYPE_SHORT:
  1226. save_arg(short);
  1227. break;
  1228. default:
  1229. save_arg(int);
  1230. }
  1231. }
  1232. }
  1233. return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
  1234. #undef save_arg
  1235. }
  1236. EXPORT_SYMBOL_GPL(vbin_printf);
  1237. /**
  1238. * bstr_printf - Format a string from binary arguments and place it in a buffer
  1239. * @buf: The buffer to place the result into
  1240. * @size: The size of the buffer, including the trailing null space
  1241. * @fmt: The format string to use
  1242. * @bin_buf: Binary arguments for the format string
  1243. *
  1244. * This function like C99 vsnprintf, but the difference is that vsnprintf gets
  1245. * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
  1246. * a binary buffer that generated by vbin_printf.
  1247. *
  1248. * The format follows C99 vsnprintf, but has some extensions:
  1249. * %pS output the name of a text symbol
  1250. * %pF output the name of a function pointer
  1251. * %pR output the address range in a struct resource
  1252. * %n is ignored
  1253. *
  1254. * The return value is the number of characters which would
  1255. * be generated for the given input, excluding the trailing
  1256. * '\0', as per ISO C99. If you want to have the exact
  1257. * number of characters written into @buf as return value
  1258. * (not including the trailing '\0'), use vscnprintf(). If the
  1259. * return is greater than or equal to @size, the resulting
  1260. * string is truncated.
  1261. */
  1262. int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
  1263. {
  1264. unsigned long long num;
  1265. char *str, *end, c;
  1266. const char *args = (const char *)bin_buf;
  1267. struct printf_spec spec = {0};
  1268. if (unlikely((int) size < 0)) {
  1269. /* There can be only one.. */
  1270. static char warn = 1;
  1271. WARN_ON(warn);
  1272. warn = 0;
  1273. return 0;
  1274. }
  1275. str = buf;
  1276. end = buf + size;
  1277. #define get_arg(type) \
  1278. ({ \
  1279. typeof(type) value; \
  1280. if (sizeof(type) == 8) { \
  1281. args = PTR_ALIGN(args, sizeof(u32)); \
  1282. *(u32 *)&value = *(u32 *)args; \
  1283. *((u32 *)&value + 1) = *(u32 *)(args + 4); \
  1284. } else { \
  1285. args = PTR_ALIGN(args, sizeof(type)); \
  1286. value = *(typeof(type) *)args; \
  1287. } \
  1288. args += sizeof(type); \
  1289. value; \
  1290. })
  1291. /* Make sure end is always >= buf */
  1292. if (end < buf) {
  1293. end = ((void *)-1);
  1294. size = end - buf;
  1295. }
  1296. while (*fmt) {
  1297. int read;
  1298. const char *old_fmt = fmt;
  1299. read = format_decode(fmt, &spec);
  1300. fmt += read;
  1301. switch (spec.type) {
  1302. case FORMAT_TYPE_NONE: {
  1303. int copy = read;
  1304. if (str < end) {
  1305. if (copy > end - str)
  1306. copy = end - str;
  1307. memcpy(str, old_fmt, copy);
  1308. }
  1309. str += read;
  1310. break;
  1311. }
  1312. case FORMAT_TYPE_WITDH:
  1313. spec.field_width = get_arg(int);
  1314. break;
  1315. case FORMAT_TYPE_PRECISION:
  1316. spec.precision = get_arg(int);
  1317. break;
  1318. case FORMAT_TYPE_CHAR:
  1319. if (!(spec.flags & LEFT)) {
  1320. while (--spec.field_width > 0) {
  1321. if (str < end)
  1322. *str = ' ';
  1323. ++str;
  1324. }
  1325. }
  1326. c = (unsigned char) get_arg(char);
  1327. if (str < end)
  1328. *str = c;
  1329. ++str;
  1330. while (--spec.field_width > 0) {
  1331. if (str < end)
  1332. *str = ' ';
  1333. ++str;
  1334. }
  1335. break;
  1336. case FORMAT_TYPE_STR: {
  1337. const char *str_arg = args;
  1338. size_t len = strlen(str_arg);
  1339. args += len + 1;
  1340. str = string(str, end, (char *)str_arg, spec);
  1341. break;
  1342. }
  1343. case FORMAT_TYPE_PTR:
  1344. str = pointer(fmt+1, str, end, get_arg(void *), spec);
  1345. while (isalnum(*fmt))
  1346. fmt++;
  1347. break;
  1348. case FORMAT_TYPE_PERCENT_CHAR:
  1349. if (str < end)
  1350. *str = '%';
  1351. ++str;
  1352. break;
  1353. case FORMAT_TYPE_INVALID:
  1354. if (str < end)
  1355. *str = '%';
  1356. ++str;
  1357. if (*fmt) {
  1358. if (str < end)
  1359. *str = *fmt;
  1360. ++str;
  1361. } else {
  1362. --fmt;
  1363. }
  1364. break;
  1365. case FORMAT_TYPE_NRCHARS:
  1366. /* skip */
  1367. break;
  1368. default:
  1369. switch (spec.type) {
  1370. case FORMAT_TYPE_LONG_LONG:
  1371. num = get_arg(long long);
  1372. break;
  1373. case FORMAT_TYPE_ULONG:
  1374. num = get_arg(unsigned long);
  1375. break;
  1376. case FORMAT_TYPE_LONG:
  1377. num = get_arg(unsigned long);
  1378. break;
  1379. case FORMAT_TYPE_SIZE_T:
  1380. num = get_arg(size_t);
  1381. break;
  1382. case FORMAT_TYPE_PTRDIFF:
  1383. num = get_arg(ptrdiff_t);
  1384. break;
  1385. case FORMAT_TYPE_USHORT:
  1386. num = get_arg(unsigned short);
  1387. break;
  1388. case FORMAT_TYPE_SHORT:
  1389. num = get_arg(short);
  1390. break;
  1391. case FORMAT_TYPE_UINT:
  1392. num = get_arg(unsigned int);
  1393. break;
  1394. default:
  1395. num = get_arg(int);
  1396. }
  1397. str = number(str, end, num, spec);
  1398. }
  1399. }
  1400. if (size > 0) {
  1401. if (str < end)
  1402. *str = '\0';
  1403. else
  1404. end[-1] = '\0';
  1405. }
  1406. #undef get_arg
  1407. /* the trailing null byte doesn't count towards the total */
  1408. return str - buf;
  1409. }
  1410. EXPORT_SYMBOL_GPL(bstr_printf);
  1411. /**
  1412. * bprintf - Parse a format string and place args' binary value in a buffer
  1413. * @bin_buf: The buffer to place args' binary value
  1414. * @size: The size of the buffer(by words(32bits), not characters)
  1415. * @fmt: The format string to use
  1416. * @...: Arguments for the format string
  1417. *
  1418. * The function returns the number of words(u32) written
  1419. * into @bin_buf.
  1420. */
  1421. int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
  1422. {
  1423. va_list args;
  1424. int ret;
  1425. va_start(args, fmt);
  1426. ret = vbin_printf(bin_buf, size, fmt, args);
  1427. va_end(args);
  1428. return ret;
  1429. }
  1430. EXPORT_SYMBOL_GPL(bprintf);
  1431. #endif /* CONFIG_BINARY_PRINTF */
  1432. /**
  1433. * vsscanf - Unformat a buffer into a list of arguments
  1434. * @buf: input buffer
  1435. * @fmt: format of buffer
  1436. * @args: arguments
  1437. */
  1438. int vsscanf(const char * buf, const char * fmt, va_list args)
  1439. {
  1440. const char *str = buf;
  1441. char *next;
  1442. char digit;
  1443. int num = 0;
  1444. int qualifier;
  1445. int base;
  1446. int field_width;
  1447. int is_sign = 0;
  1448. while(*fmt && *str) {
  1449. /* skip any white space in format */
  1450. /* white space in format matchs any amount of
  1451. * white space, including none, in the input.
  1452. */
  1453. if (isspace(*fmt)) {
  1454. while (isspace(*fmt))
  1455. ++fmt;
  1456. while (isspace(*str))
  1457. ++str;
  1458. }
  1459. /* anything that is not a conversion must match exactly */
  1460. if (*fmt != '%' && *fmt) {
  1461. if (*fmt++ != *str++)
  1462. break;
  1463. continue;
  1464. }
  1465. if (!*fmt)
  1466. break;
  1467. ++fmt;
  1468. /* skip this conversion.
  1469. * advance both strings to next white space
  1470. */
  1471. if (*fmt == '*') {
  1472. while (!isspace(*fmt) && *fmt)
  1473. fmt++;
  1474. while (!isspace(*str) && *str)
  1475. str++;
  1476. continue;
  1477. }
  1478. /* get field width */
  1479. field_width = -1;
  1480. if (isdigit(*fmt))
  1481. field_width = skip_atoi(&fmt);
  1482. /* get conversion qualifier */
  1483. qualifier = -1;
  1484. if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
  1485. *fmt == 'Z' || *fmt == 'z') {
  1486. qualifier = *fmt++;
  1487. if (unlikely(qualifier == *fmt)) {
  1488. if (qualifier == 'h') {
  1489. qualifier = 'H';
  1490. fmt++;
  1491. } else if (qualifier == 'l') {
  1492. qualifier = 'L';
  1493. fmt++;
  1494. }
  1495. }
  1496. }
  1497. base = 10;
  1498. is_sign = 0;
  1499. if (!*fmt || !*str)
  1500. break;
  1501. switch(*fmt++) {
  1502. case 'c':
  1503. {
  1504. char *s = (char *) va_arg(args,char*);
  1505. if (field_width == -1)
  1506. field_width = 1;
  1507. do {
  1508. *s++ = *str++;
  1509. } while (--field_width > 0 && *str);
  1510. num++;
  1511. }
  1512. continue;
  1513. case 's':
  1514. {
  1515. char *s = (char *) va_arg(args, char *);
  1516. if(field_width == -1)
  1517. field_width = INT_MAX;
  1518. /* first, skip leading white space in buffer */
  1519. while (isspace(*str))
  1520. str++;
  1521. /* now copy until next white space */
  1522. while (*str && !isspace(*str) && field_width--) {
  1523. *s++ = *str++;
  1524. }
  1525. *s = '\0';
  1526. num++;
  1527. }
  1528. continue;
  1529. case 'n':
  1530. /* return number of characters read so far */
  1531. {
  1532. int *i = (int *)va_arg(args,int*);
  1533. *i = str - buf;
  1534. }
  1535. continue;
  1536. case 'o':
  1537. base = 8;
  1538. break;
  1539. case 'x':
  1540. case 'X':
  1541. base = 16;
  1542. break;
  1543. case 'i':
  1544. base = 0;
  1545. case 'd':
  1546. is_sign = 1;
  1547. case 'u':
  1548. break;
  1549. case '%':
  1550. /* looking for '%' in str */
  1551. if (*str++ != '%')
  1552. return num;
  1553. continue;
  1554. default:
  1555. /* invalid format; stop here */
  1556. return num;
  1557. }
  1558. /* have some sort of integer conversion.
  1559. * first, skip white space in buffer.
  1560. */
  1561. while (isspace(*str))
  1562. str++;
  1563. digit = *str;
  1564. if (is_sign && digit == '-')
  1565. digit = *(str + 1);
  1566. if (!digit
  1567. || (base == 16 && !isxdigit(digit))
  1568. || (base == 10 && !isdigit(digit))
  1569. || (base == 8 && (!isdigit(digit) || digit > '7'))
  1570. || (base == 0 && !isdigit(digit)))
  1571. break;
  1572. switch(qualifier) {
  1573. case 'H': /* that's 'hh' in format */
  1574. if (is_sign) {
  1575. signed char *s = (signed char *) va_arg(args,signed char *);
  1576. *s = (signed char) simple_strtol(str,&next,base);
  1577. } else {
  1578. unsigned char *s = (unsigned char *) va_arg(args, unsigned char *);
  1579. *s = (unsigned char) simple_strtoul(str, &next, base);
  1580. }
  1581. break;
  1582. case 'h':
  1583. if (is_sign) {
  1584. short *s = (short *) va_arg(args,short *);
  1585. *s = (short) simple_strtol(str,&next,base);
  1586. } else {
  1587. unsigned short *s = (unsigned short *) va_arg(args, unsigned short *);
  1588. *s = (unsigned short) simple_strtoul(str, &next, base);
  1589. }
  1590. break;
  1591. case 'l':
  1592. if (is_sign) {
  1593. long *l = (long *) va_arg(args,long *);
  1594. *l = simple_strtol(str,&next,base);
  1595. } else {
  1596. unsigned long *l = (unsigned long*) va_arg(args,unsigned long*);
  1597. *l = simple_strtoul(str,&next,base);
  1598. }
  1599. break;
  1600. case 'L':
  1601. if (is_sign) {
  1602. long long *l = (long long*) va_arg(args,long long *);
  1603. *l = simple_strtoll(str,&next,base);
  1604. } else {
  1605. unsigned long long *l = (unsigned long long*) va_arg(args,unsigned long long*);
  1606. *l = simple_strtoull(str,&next,base);
  1607. }
  1608. break;
  1609. case 'Z':
  1610. case 'z':
  1611. {
  1612. size_t *s = (size_t*) va_arg(args,size_t*);
  1613. *s = (size_t) simple_strtoul(str,&next,base);
  1614. }
  1615. break;
  1616. default:
  1617. if (is_sign) {
  1618. int *i = (int *) va_arg(args, int*);
  1619. *i = (int) simple_strtol(str,&next,base);
  1620. } else {
  1621. unsigned int *i = (unsigned int*) va_arg(args, unsigned int*);
  1622. *i = (unsigned int) simple_strtoul(str,&next,base);
  1623. }
  1624. break;
  1625. }
  1626. num++;
  1627. if (!next)
  1628. break;
  1629. str = next;
  1630. }
  1631. /*
  1632. * Now we've come all the way through so either the input string or the
  1633. * format ended. In the former case, there can be a %n at the current
  1634. * position in the format that needs to be filled.
  1635. */
  1636. if (*fmt == '%' && *(fmt + 1) == 'n') {
  1637. int *p = (int *)va_arg(args, int *);
  1638. *p = str - buf;
  1639. }
  1640. return num;
  1641. }
  1642. EXPORT_SYMBOL(vsscanf);
  1643. /**
  1644. * sscanf - Unformat a buffer into a list of arguments
  1645. * @buf: input buffer
  1646. * @fmt: formatting of buffer
  1647. * @...: resulting arguments
  1648. */
  1649. int sscanf(const char * buf, const char * fmt, ...)
  1650. {
  1651. va_list args;
  1652. int i;
  1653. va_start(args,fmt);
  1654. i = vsscanf(buf,fmt,args);
  1655. va_end(args);
  1656. return i;
  1657. }
  1658. EXPORT_SYMBOL(sscanf);