vsprintf.c 49 KB

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