vsprintf.c 45 KB

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