vsprintf.c 51 KB

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