vsprintf.c 49 KB

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