vsprintf.c 50 KB

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