vsprintf.c 51 KB

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