vsprintf.c 43 KB

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