vsprintf.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698
  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/clk.h>
  18. #include <linux/clk-provider.h>
  19. #include <linux/module.h> /* for KSYM_SYMBOL_LEN */
  20. #include <linux/types.h>
  21. #include <linux/string.h>
  22. #include <linux/ctype.h>
  23. #include <linux/kernel.h>
  24. #include <linux/kallsyms.h>
  25. #include <linux/math64.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/ioport.h>
  28. #include <linux/dcache.h>
  29. #include <linux/cred.h>
  30. #include <net/addrconf.h>
  31. #include <asm/page.h> /* for PAGE_SIZE */
  32. #include <asm/sections.h> /* for dereference_function_descriptor() */
  33. #include <asm/byteorder.h> /* cpu_to_le16 */
  34. #include <linux/string_helpers.h>
  35. #include "kstrtox.h"
  36. /**
  37. * simple_strtoull - convert a string to an unsigned long long
  38. * @cp: The start of the string
  39. * @endp: A pointer to the end of the parsed string will be placed here
  40. * @base: The number base to use
  41. *
  42. * This function is obsolete. Please use kstrtoull instead.
  43. */
  44. unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
  45. {
  46. unsigned long long result;
  47. unsigned int rv;
  48. cp = _parse_integer_fixup_radix(cp, &base);
  49. rv = _parse_integer(cp, base, &result);
  50. /* FIXME */
  51. cp += (rv & ~KSTRTOX_OVERFLOW);
  52. if (endp)
  53. *endp = (char *)cp;
  54. return result;
  55. }
  56. EXPORT_SYMBOL(simple_strtoull);
  57. /**
  58. * simple_strtoul - convert a string to an unsigned long
  59. * @cp: The start of the string
  60. * @endp: A pointer to the end of the parsed string will be placed here
  61. * @base: The number base to use
  62. *
  63. * This function is obsolete. Please use kstrtoul instead.
  64. */
  65. unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
  66. {
  67. return simple_strtoull(cp, endp, base);
  68. }
  69. EXPORT_SYMBOL(simple_strtoul);
  70. /**
  71. * simple_strtol - convert a string to a signed long
  72. * @cp: The start of the string
  73. * @endp: A pointer to the end of the parsed string will be placed here
  74. * @base: The number base to use
  75. *
  76. * This function is obsolete. Please use kstrtol instead.
  77. */
  78. long simple_strtol(const char *cp, char **endp, unsigned int base)
  79. {
  80. if (*cp == '-')
  81. return -simple_strtoul(cp + 1, endp, base);
  82. return simple_strtoul(cp, endp, base);
  83. }
  84. EXPORT_SYMBOL(simple_strtol);
  85. /**
  86. * simple_strtoll - convert a string to a signed long long
  87. * @cp: The start of the string
  88. * @endp: A pointer to the end of the parsed string will be placed here
  89. * @base: The number base to use
  90. *
  91. * This function is obsolete. Please use kstrtoll instead.
  92. */
  93. long long simple_strtoll(const char *cp, char **endp, unsigned int base)
  94. {
  95. if (*cp == '-')
  96. return -simple_strtoull(cp + 1, endp, base);
  97. return simple_strtoull(cp, endp, base);
  98. }
  99. EXPORT_SYMBOL(simple_strtoll);
  100. static noinline_for_stack
  101. int skip_atoi(const char **s)
  102. {
  103. int i = 0;
  104. do {
  105. i = i*10 + *((*s)++) - '0';
  106. } while (isdigit(**s));
  107. return i;
  108. }
  109. /*
  110. * Decimal conversion is by far the most typical, and is used for
  111. * /proc and /sys data. This directly impacts e.g. top performance
  112. * with many processes running. We optimize it for speed by emitting
  113. * two characters at a time, using a 200 byte lookup table. This
  114. * roughly halves the number of multiplications compared to computing
  115. * the digits one at a time. Implementation strongly inspired by the
  116. * previous version, which in turn used ideas described at
  117. * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
  118. * from the author, Douglas W. Jones).
  119. *
  120. * It turns out there is precisely one 26 bit fixed-point
  121. * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
  122. * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
  123. * range happens to be somewhat larger (x <= 1073741898), but that's
  124. * irrelevant for our purpose.
  125. *
  126. * For dividing a number in the range [10^4, 10^6-1] by 100, we still
  127. * need a 32x32->64 bit multiply, so we simply use the same constant.
  128. *
  129. * For dividing a number in the range [100, 10^4-1] by 100, there are
  130. * several options. The simplest is (x * 0x147b) >> 19, which is valid
  131. * for all x <= 43698.
  132. */
  133. static const u16 decpair[100] = {
  134. #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
  135. _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
  136. _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
  137. _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
  138. _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
  139. _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
  140. _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
  141. _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
  142. _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
  143. _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
  144. _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
  145. #undef _
  146. };
  147. /*
  148. * This will print a single '0' even if r == 0, since we would
  149. * immediately jump to out_r where two 0s would be written but only
  150. * one of them accounted for in buf. This is needed by ip4_string
  151. * below. All other callers pass a non-zero value of r.
  152. */
  153. static noinline_for_stack
  154. char *put_dec_trunc8(char *buf, unsigned r)
  155. {
  156. unsigned q;
  157. /* 1 <= r < 10^8 */
  158. if (r < 100)
  159. goto out_r;
  160. /* 100 <= r < 10^8 */
  161. q = (r * (u64)0x28f5c29) >> 32;
  162. *((u16 *)buf) = decpair[r - 100*q];
  163. buf += 2;
  164. /* 1 <= q < 10^6 */
  165. if (q < 100)
  166. goto out_q;
  167. /* 100 <= q < 10^6 */
  168. r = (q * (u64)0x28f5c29) >> 32;
  169. *((u16 *)buf) = decpair[q - 100*r];
  170. buf += 2;
  171. /* 1 <= r < 10^4 */
  172. if (r < 100)
  173. goto out_r;
  174. /* 100 <= r < 10^4 */
  175. q = (r * 0x147b) >> 19;
  176. *((u16 *)buf) = decpair[r - 100*q];
  177. buf += 2;
  178. out_q:
  179. /* 1 <= q < 100 */
  180. r = q;
  181. out_r:
  182. /* 1 <= r < 100 */
  183. *((u16 *)buf) = decpair[r];
  184. buf += r < 10 ? 1 : 2;
  185. return buf;
  186. }
  187. #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
  188. static noinline_for_stack
  189. char *put_dec_full8(char *buf, unsigned r)
  190. {
  191. unsigned q;
  192. /* 0 <= r < 10^8 */
  193. q = (r * (u64)0x28f5c29) >> 32;
  194. *((u16 *)buf) = decpair[r - 100*q];
  195. buf += 2;
  196. /* 0 <= q < 10^6 */
  197. r = (q * (u64)0x28f5c29) >> 32;
  198. *((u16 *)buf) = decpair[q - 100*r];
  199. buf += 2;
  200. /* 0 <= r < 10^4 */
  201. q = (r * 0x147b) >> 19;
  202. *((u16 *)buf) = decpair[r - 100*q];
  203. buf += 2;
  204. /* 0 <= q < 100 */
  205. *((u16 *)buf) = decpair[q];
  206. buf += 2;
  207. return buf;
  208. }
  209. static noinline_for_stack
  210. char *put_dec(char *buf, unsigned long long n)
  211. {
  212. if (n >= 100*1000*1000)
  213. buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
  214. /* 1 <= n <= 1.6e11 */
  215. if (n >= 100*1000*1000)
  216. buf = put_dec_full8(buf, do_div(n, 100*1000*1000));
  217. /* 1 <= n < 1e8 */
  218. return put_dec_trunc8(buf, n);
  219. }
  220. #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
  221. static void
  222. put_dec_full4(char *buf, unsigned r)
  223. {
  224. unsigned q;
  225. /* 0 <= r < 10^4 */
  226. q = (r * 0x147b) >> 19;
  227. *((u16 *)buf) = decpair[r - 100*q];
  228. buf += 2;
  229. /* 0 <= q < 100 */
  230. *((u16 *)buf) = decpair[q];
  231. }
  232. /*
  233. * Call put_dec_full4 on x % 10000, return x / 10000.
  234. * The approximation x/10000 == (x * 0x346DC5D7) >> 43
  235. * holds for all x < 1,128,869,999. The largest value this
  236. * helper will ever be asked to convert is 1,125,520,955.
  237. * (second call in the put_dec code, assuming n is all-ones).
  238. */
  239. static noinline_for_stack
  240. unsigned put_dec_helper4(char *buf, unsigned x)
  241. {
  242. uint32_t q = (x * (uint64_t)0x346DC5D7) >> 43;
  243. put_dec_full4(buf, x - q * 10000);
  244. return q;
  245. }
  246. /* Based on code by Douglas W. Jones found at
  247. * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
  248. * (with permission from the author).
  249. * Performs no 64-bit division and hence should be fast on 32-bit machines.
  250. */
  251. static
  252. char *put_dec(char *buf, unsigned long long n)
  253. {
  254. uint32_t d3, d2, d1, q, h;
  255. if (n < 100*1000*1000)
  256. return put_dec_trunc8(buf, n);
  257. d1 = ((uint32_t)n >> 16); /* implicit "& 0xffff" */
  258. h = (n >> 32);
  259. d2 = (h ) & 0xffff;
  260. d3 = (h >> 16); /* implicit "& 0xffff" */
  261. /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
  262. = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
  263. q = 656 * d3 + 7296 * d2 + 5536 * d1 + ((uint32_t)n & 0xffff);
  264. q = put_dec_helper4(buf, q);
  265. q += 7671 * d3 + 9496 * d2 + 6 * d1;
  266. q = put_dec_helper4(buf+4, q);
  267. q += 4749 * d3 + 42 * d2;
  268. q = put_dec_helper4(buf+8, q);
  269. q += 281 * d3;
  270. buf += 12;
  271. if (q)
  272. buf = put_dec_trunc8(buf, q);
  273. else while (buf[-1] == '0')
  274. --buf;
  275. return buf;
  276. }
  277. #endif
  278. /*
  279. * Convert passed number to decimal string.
  280. * Returns the length of string. On buffer overflow, returns 0.
  281. *
  282. * If speed is not important, use snprintf(). It's easy to read the code.
  283. */
  284. int num_to_str(char *buf, int size, unsigned long long num)
  285. {
  286. /* put_dec requires 2-byte alignment of the buffer. */
  287. char tmp[sizeof(num) * 3] __aligned(2);
  288. int idx, len;
  289. /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
  290. if (num <= 9) {
  291. tmp[0] = '0' + num;
  292. len = 1;
  293. } else {
  294. len = put_dec(tmp, num) - tmp;
  295. }
  296. if (len > size)
  297. return 0;
  298. for (idx = 0; idx < len; ++idx)
  299. buf[idx] = tmp[len - idx - 1];
  300. return len;
  301. }
  302. #define SIGN 1 /* unsigned/signed, must be 1 */
  303. #define LEFT 2 /* left justified */
  304. #define PLUS 4 /* show plus */
  305. #define SPACE 8 /* space if plus */
  306. #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
  307. #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
  308. #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
  309. enum format_type {
  310. FORMAT_TYPE_NONE, /* Just a string part */
  311. FORMAT_TYPE_WIDTH,
  312. FORMAT_TYPE_PRECISION,
  313. FORMAT_TYPE_CHAR,
  314. FORMAT_TYPE_STR,
  315. FORMAT_TYPE_PTR,
  316. FORMAT_TYPE_PERCENT_CHAR,
  317. FORMAT_TYPE_INVALID,
  318. FORMAT_TYPE_LONG_LONG,
  319. FORMAT_TYPE_ULONG,
  320. FORMAT_TYPE_LONG,
  321. FORMAT_TYPE_UBYTE,
  322. FORMAT_TYPE_BYTE,
  323. FORMAT_TYPE_USHORT,
  324. FORMAT_TYPE_SHORT,
  325. FORMAT_TYPE_UINT,
  326. FORMAT_TYPE_INT,
  327. FORMAT_TYPE_SIZE_T,
  328. FORMAT_TYPE_PTRDIFF
  329. };
  330. struct printf_spec {
  331. u8 type; /* format_type enum */
  332. u8 flags; /* flags to number() */
  333. u8 base; /* number base, 8, 10 or 16 only */
  334. u8 qualifier; /* number qualifier, one of 'hHlLtzZ' */
  335. s16 field_width; /* width of output field */
  336. s16 precision; /* # of digits/chars */
  337. };
  338. static noinline_for_stack
  339. char *number(char *buf, char *end, unsigned long long num,
  340. struct printf_spec spec)
  341. {
  342. /* put_dec requires 2-byte alignment of the buffer. */
  343. char tmp[3 * sizeof(num)] __aligned(2);
  344. char sign;
  345. char locase;
  346. int need_pfx = ((spec.flags & SPECIAL) && spec.base != 10);
  347. int i;
  348. bool is_zero = num == 0LL;
  349. /* locase = 0 or 0x20. ORing digits or letters with 'locase'
  350. * produces same digits or (maybe lowercased) letters */
  351. locase = (spec.flags & SMALL);
  352. if (spec.flags & LEFT)
  353. spec.flags &= ~ZEROPAD;
  354. sign = 0;
  355. if (spec.flags & SIGN) {
  356. if ((signed long long)num < 0) {
  357. sign = '-';
  358. num = -(signed long long)num;
  359. spec.field_width--;
  360. } else if (spec.flags & PLUS) {
  361. sign = '+';
  362. spec.field_width--;
  363. } else if (spec.flags & SPACE) {
  364. sign = ' ';
  365. spec.field_width--;
  366. }
  367. }
  368. if (need_pfx) {
  369. if (spec.base == 16)
  370. spec.field_width -= 2;
  371. else if (!is_zero)
  372. spec.field_width--;
  373. }
  374. /* generate full string in tmp[], in reverse order */
  375. i = 0;
  376. if (num < spec.base)
  377. tmp[i++] = hex_asc_upper[num] | locase;
  378. else if (spec.base != 10) { /* 8 or 16 */
  379. int mask = spec.base - 1;
  380. int shift = 3;
  381. if (spec.base == 16)
  382. shift = 4;
  383. do {
  384. tmp[i++] = (hex_asc_upper[((unsigned char)num) & mask] | locase);
  385. num >>= shift;
  386. } while (num);
  387. } else { /* base 10 */
  388. i = put_dec(tmp, num) - tmp;
  389. }
  390. /* printing 100 using %2d gives "100", not "00" */
  391. if (i > spec.precision)
  392. spec.precision = i;
  393. /* leading space padding */
  394. spec.field_width -= spec.precision;
  395. if (!(spec.flags & (ZEROPAD | LEFT))) {
  396. while (--spec.field_width >= 0) {
  397. if (buf < end)
  398. *buf = ' ';
  399. ++buf;
  400. }
  401. }
  402. /* sign */
  403. if (sign) {
  404. if (buf < end)
  405. *buf = sign;
  406. ++buf;
  407. }
  408. /* "0x" / "0" prefix */
  409. if (need_pfx) {
  410. if (spec.base == 16 || !is_zero) {
  411. if (buf < end)
  412. *buf = '0';
  413. ++buf;
  414. }
  415. if (spec.base == 16) {
  416. if (buf < end)
  417. *buf = ('X' | locase);
  418. ++buf;
  419. }
  420. }
  421. /* zero or space padding */
  422. if (!(spec.flags & LEFT)) {
  423. char c = ' ' + (spec.flags & ZEROPAD);
  424. BUILD_BUG_ON(' ' + ZEROPAD != '0');
  425. while (--spec.field_width >= 0) {
  426. if (buf < end)
  427. *buf = c;
  428. ++buf;
  429. }
  430. }
  431. /* hmm even more zero padding? */
  432. while (i <= --spec.precision) {
  433. if (buf < end)
  434. *buf = '0';
  435. ++buf;
  436. }
  437. /* actual digits of result */
  438. while (--i >= 0) {
  439. if (buf < end)
  440. *buf = tmp[i];
  441. ++buf;
  442. }
  443. /* trailing space padding */
  444. while (--spec.field_width >= 0) {
  445. if (buf < end)
  446. *buf = ' ';
  447. ++buf;
  448. }
  449. return buf;
  450. }
  451. static noinline_for_stack
  452. char *string(char *buf, char *end, const char *s, struct printf_spec spec)
  453. {
  454. int len, i;
  455. if ((unsigned long)s < PAGE_SIZE)
  456. s = "(null)";
  457. len = strnlen(s, spec.precision);
  458. if (!(spec.flags & LEFT)) {
  459. while (len < spec.field_width--) {
  460. if (buf < end)
  461. *buf = ' ';
  462. ++buf;
  463. }
  464. }
  465. for (i = 0; i < len; ++i) {
  466. if (buf < end)
  467. *buf = *s;
  468. ++buf; ++s;
  469. }
  470. while (len < spec.field_width--) {
  471. if (buf < end)
  472. *buf = ' ';
  473. ++buf;
  474. }
  475. return buf;
  476. }
  477. static void widen(char *buf, char *end, unsigned len, unsigned spaces)
  478. {
  479. size_t size;
  480. if (buf >= end) /* nowhere to put anything */
  481. return;
  482. size = end - buf;
  483. if (size <= spaces) {
  484. memset(buf, ' ', size);
  485. return;
  486. }
  487. if (len) {
  488. if (len > size - spaces)
  489. len = size - spaces;
  490. memmove(buf + spaces, buf, len);
  491. }
  492. memset(buf, ' ', spaces);
  493. }
  494. static noinline_for_stack
  495. char *dentry_name(char *buf, char *end, const struct dentry *d, struct printf_spec spec,
  496. const char *fmt)
  497. {
  498. const char *array[4], *s;
  499. const struct dentry *p;
  500. int depth;
  501. int i, n;
  502. switch (fmt[1]) {
  503. case '2': case '3': case '4':
  504. depth = fmt[1] - '0';
  505. break;
  506. default:
  507. depth = 1;
  508. }
  509. rcu_read_lock();
  510. for (i = 0; i < depth; i++, d = p) {
  511. p = ACCESS_ONCE(d->d_parent);
  512. array[i] = ACCESS_ONCE(d->d_name.name);
  513. if (p == d) {
  514. if (i)
  515. array[i] = "";
  516. i++;
  517. break;
  518. }
  519. }
  520. s = array[--i];
  521. for (n = 0; n != spec.precision; n++, buf++) {
  522. char c = *s++;
  523. if (!c) {
  524. if (!i)
  525. break;
  526. c = '/';
  527. s = array[--i];
  528. }
  529. if (buf < end)
  530. *buf = c;
  531. }
  532. rcu_read_unlock();
  533. if (n < spec.field_width) {
  534. /* we want to pad the sucker */
  535. unsigned spaces = spec.field_width - n;
  536. if (!(spec.flags & LEFT)) {
  537. widen(buf - n, end, n, spaces);
  538. return buf + spaces;
  539. }
  540. while (spaces--) {
  541. if (buf < end)
  542. *buf = ' ';
  543. ++buf;
  544. }
  545. }
  546. return buf;
  547. }
  548. static noinline_for_stack
  549. char *symbol_string(char *buf, char *end, void *ptr,
  550. struct printf_spec spec, const char *fmt)
  551. {
  552. unsigned long value;
  553. #ifdef CONFIG_KALLSYMS
  554. char sym[KSYM_SYMBOL_LEN];
  555. #endif
  556. if (fmt[1] == 'R')
  557. ptr = __builtin_extract_return_addr(ptr);
  558. value = (unsigned long)ptr;
  559. #ifdef CONFIG_KALLSYMS
  560. if (*fmt == 'B')
  561. sprint_backtrace(sym, value);
  562. else if (*fmt != 'f' && *fmt != 's')
  563. sprint_symbol(sym, value);
  564. else
  565. sprint_symbol_no_offset(sym, value);
  566. return string(buf, end, sym, spec);
  567. #else
  568. spec.field_width = 2 * sizeof(void *);
  569. spec.flags |= SPECIAL | SMALL | ZEROPAD;
  570. spec.base = 16;
  571. return number(buf, end, value, spec);
  572. #endif
  573. }
  574. static noinline_for_stack
  575. char *resource_string(char *buf, char *end, struct resource *res,
  576. struct printf_spec spec, const char *fmt)
  577. {
  578. #ifndef IO_RSRC_PRINTK_SIZE
  579. #define IO_RSRC_PRINTK_SIZE 6
  580. #endif
  581. #ifndef MEM_RSRC_PRINTK_SIZE
  582. #define MEM_RSRC_PRINTK_SIZE 10
  583. #endif
  584. static const struct printf_spec io_spec = {
  585. .base = 16,
  586. .field_width = IO_RSRC_PRINTK_SIZE,
  587. .precision = -1,
  588. .flags = SPECIAL | SMALL | ZEROPAD,
  589. };
  590. static const struct printf_spec mem_spec = {
  591. .base = 16,
  592. .field_width = MEM_RSRC_PRINTK_SIZE,
  593. .precision = -1,
  594. .flags = SPECIAL | SMALL | ZEROPAD,
  595. };
  596. static const struct printf_spec bus_spec = {
  597. .base = 16,
  598. .field_width = 2,
  599. .precision = -1,
  600. .flags = SMALL | ZEROPAD,
  601. };
  602. static const struct printf_spec dec_spec = {
  603. .base = 10,
  604. .precision = -1,
  605. .flags = 0,
  606. };
  607. static const struct printf_spec str_spec = {
  608. .field_width = -1,
  609. .precision = 10,
  610. .flags = LEFT,
  611. };
  612. static const struct printf_spec flag_spec = {
  613. .base = 16,
  614. .precision = -1,
  615. .flags = SPECIAL | SMALL,
  616. };
  617. /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
  618. * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
  619. #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
  620. #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
  621. #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
  622. #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
  623. char sym[max(2*RSRC_BUF_SIZE + DECODED_BUF_SIZE,
  624. 2*RSRC_BUF_SIZE + FLAG_BUF_SIZE + RAW_BUF_SIZE)];
  625. char *p = sym, *pend = sym + sizeof(sym);
  626. int decode = (fmt[0] == 'R') ? 1 : 0;
  627. const struct printf_spec *specp;
  628. *p++ = '[';
  629. if (res->flags & IORESOURCE_IO) {
  630. p = string(p, pend, "io ", str_spec);
  631. specp = &io_spec;
  632. } else if (res->flags & IORESOURCE_MEM) {
  633. p = string(p, pend, "mem ", str_spec);
  634. specp = &mem_spec;
  635. } else if (res->flags & IORESOURCE_IRQ) {
  636. p = string(p, pend, "irq ", str_spec);
  637. specp = &dec_spec;
  638. } else if (res->flags & IORESOURCE_DMA) {
  639. p = string(p, pend, "dma ", str_spec);
  640. specp = &dec_spec;
  641. } else if (res->flags & IORESOURCE_BUS) {
  642. p = string(p, pend, "bus ", str_spec);
  643. specp = &bus_spec;
  644. } else {
  645. p = string(p, pend, "??? ", str_spec);
  646. specp = &mem_spec;
  647. decode = 0;
  648. }
  649. if (decode && res->flags & IORESOURCE_UNSET) {
  650. p = string(p, pend, "size ", str_spec);
  651. p = number(p, pend, resource_size(res), *specp);
  652. } else {
  653. p = number(p, pend, res->start, *specp);
  654. if (res->start != res->end) {
  655. *p++ = '-';
  656. p = number(p, pend, res->end, *specp);
  657. }
  658. }
  659. if (decode) {
  660. if (res->flags & IORESOURCE_MEM_64)
  661. p = string(p, pend, " 64bit", str_spec);
  662. if (res->flags & IORESOURCE_PREFETCH)
  663. p = string(p, pend, " pref", str_spec);
  664. if (res->flags & IORESOURCE_WINDOW)
  665. p = string(p, pend, " window", str_spec);
  666. if (res->flags & IORESOURCE_DISABLED)
  667. p = string(p, pend, " disabled", str_spec);
  668. } else {
  669. p = string(p, pend, " flags ", str_spec);
  670. p = number(p, pend, res->flags, flag_spec);
  671. }
  672. *p++ = ']';
  673. *p = '\0';
  674. return string(buf, end, sym, spec);
  675. }
  676. static noinline_for_stack
  677. char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
  678. const char *fmt)
  679. {
  680. int i, len = 1; /* if we pass '%ph[CDN]', field width remains
  681. negative value, fallback to the default */
  682. char separator;
  683. if (spec.field_width == 0)
  684. /* nothing to print */
  685. return buf;
  686. if (ZERO_OR_NULL_PTR(addr))
  687. /* NULL pointer */
  688. return string(buf, end, NULL, spec);
  689. switch (fmt[1]) {
  690. case 'C':
  691. separator = ':';
  692. break;
  693. case 'D':
  694. separator = '-';
  695. break;
  696. case 'N':
  697. separator = 0;
  698. break;
  699. default:
  700. separator = ' ';
  701. break;
  702. }
  703. if (spec.field_width > 0)
  704. len = min_t(int, spec.field_width, 64);
  705. for (i = 0; i < len; ++i) {
  706. if (buf < end)
  707. *buf = hex_asc_hi(addr[i]);
  708. ++buf;
  709. if (buf < end)
  710. *buf = hex_asc_lo(addr[i]);
  711. ++buf;
  712. if (separator && i != len - 1) {
  713. if (buf < end)
  714. *buf = separator;
  715. ++buf;
  716. }
  717. }
  718. return buf;
  719. }
  720. static noinline_for_stack
  721. char *bitmap_string(char *buf, char *end, unsigned long *bitmap,
  722. struct printf_spec spec, const char *fmt)
  723. {
  724. const int CHUNKSZ = 32;
  725. int nr_bits = max_t(int, spec.field_width, 0);
  726. int i, chunksz;
  727. bool first = true;
  728. /* reused to print numbers */
  729. spec = (struct printf_spec){ .flags = SMALL | ZEROPAD, .base = 16 };
  730. chunksz = nr_bits & (CHUNKSZ - 1);
  731. if (chunksz == 0)
  732. chunksz = CHUNKSZ;
  733. i = ALIGN(nr_bits, CHUNKSZ) - CHUNKSZ;
  734. for (; i >= 0; i -= CHUNKSZ) {
  735. u32 chunkmask, val;
  736. int word, bit;
  737. chunkmask = ((1ULL << chunksz) - 1);
  738. word = i / BITS_PER_LONG;
  739. bit = i % BITS_PER_LONG;
  740. val = (bitmap[word] >> bit) & chunkmask;
  741. if (!first) {
  742. if (buf < end)
  743. *buf = ',';
  744. buf++;
  745. }
  746. first = false;
  747. spec.field_width = DIV_ROUND_UP(chunksz, 4);
  748. buf = number(buf, end, val, spec);
  749. chunksz = CHUNKSZ;
  750. }
  751. return buf;
  752. }
  753. static noinline_for_stack
  754. char *bitmap_list_string(char *buf, char *end, unsigned long *bitmap,
  755. struct printf_spec spec, const char *fmt)
  756. {
  757. int nr_bits = max_t(int, spec.field_width, 0);
  758. /* current bit is 'cur', most recently seen range is [rbot, rtop] */
  759. int cur, rbot, rtop;
  760. bool first = true;
  761. /* reused to print numbers */
  762. spec = (struct printf_spec){ .base = 10 };
  763. rbot = cur = find_first_bit(bitmap, nr_bits);
  764. while (cur < nr_bits) {
  765. rtop = cur;
  766. cur = find_next_bit(bitmap, nr_bits, cur + 1);
  767. if (cur < nr_bits && cur <= rtop + 1)
  768. continue;
  769. if (!first) {
  770. if (buf < end)
  771. *buf = ',';
  772. buf++;
  773. }
  774. first = false;
  775. buf = number(buf, end, rbot, spec);
  776. if (rbot < rtop) {
  777. if (buf < end)
  778. *buf = '-';
  779. buf++;
  780. buf = number(buf, end, rtop, spec);
  781. }
  782. rbot = cur;
  783. }
  784. return buf;
  785. }
  786. static noinline_for_stack
  787. char *mac_address_string(char *buf, char *end, u8 *addr,
  788. struct printf_spec spec, const char *fmt)
  789. {
  790. char mac_addr[sizeof("xx:xx:xx:xx:xx:xx")];
  791. char *p = mac_addr;
  792. int i;
  793. char separator;
  794. bool reversed = false;
  795. switch (fmt[1]) {
  796. case 'F':
  797. separator = '-';
  798. break;
  799. case 'R':
  800. reversed = true;
  801. /* fall through */
  802. default:
  803. separator = ':';
  804. break;
  805. }
  806. for (i = 0; i < 6; i++) {
  807. if (reversed)
  808. p = hex_byte_pack(p, addr[5 - i]);
  809. else
  810. p = hex_byte_pack(p, addr[i]);
  811. if (fmt[0] == 'M' && i != 5)
  812. *p++ = separator;
  813. }
  814. *p = '\0';
  815. return string(buf, end, mac_addr, spec);
  816. }
  817. static noinline_for_stack
  818. char *ip4_string(char *p, const u8 *addr, const char *fmt)
  819. {
  820. int i;
  821. bool leading_zeros = (fmt[0] == 'i');
  822. int index;
  823. int step;
  824. switch (fmt[2]) {
  825. case 'h':
  826. #ifdef __BIG_ENDIAN
  827. index = 0;
  828. step = 1;
  829. #else
  830. index = 3;
  831. step = -1;
  832. #endif
  833. break;
  834. case 'l':
  835. index = 3;
  836. step = -1;
  837. break;
  838. case 'n':
  839. case 'b':
  840. default:
  841. index = 0;
  842. step = 1;
  843. break;
  844. }
  845. for (i = 0; i < 4; i++) {
  846. char temp[4] __aligned(2); /* hold each IP quad in reverse order */
  847. int digits = put_dec_trunc8(temp, addr[index]) - temp;
  848. if (leading_zeros) {
  849. if (digits < 3)
  850. *p++ = '0';
  851. if (digits < 2)
  852. *p++ = '0';
  853. }
  854. /* reverse the digits in the quad */
  855. while (digits--)
  856. *p++ = temp[digits];
  857. if (i < 3)
  858. *p++ = '.';
  859. index += step;
  860. }
  861. *p = '\0';
  862. return p;
  863. }
  864. static noinline_for_stack
  865. char *ip6_compressed_string(char *p, const char *addr)
  866. {
  867. int i, j, range;
  868. unsigned char zerolength[8];
  869. int longest = 1;
  870. int colonpos = -1;
  871. u16 word;
  872. u8 hi, lo;
  873. bool needcolon = false;
  874. bool useIPv4;
  875. struct in6_addr in6;
  876. memcpy(&in6, addr, sizeof(struct in6_addr));
  877. useIPv4 = ipv6_addr_v4mapped(&in6) || ipv6_addr_is_isatap(&in6);
  878. memset(zerolength, 0, sizeof(zerolength));
  879. if (useIPv4)
  880. range = 6;
  881. else
  882. range = 8;
  883. /* find position of longest 0 run */
  884. for (i = 0; i < range; i++) {
  885. for (j = i; j < range; j++) {
  886. if (in6.s6_addr16[j] != 0)
  887. break;
  888. zerolength[i]++;
  889. }
  890. }
  891. for (i = 0; i < range; i++) {
  892. if (zerolength[i] > longest) {
  893. longest = zerolength[i];
  894. colonpos = i;
  895. }
  896. }
  897. if (longest == 1) /* don't compress a single 0 */
  898. colonpos = -1;
  899. /* emit address */
  900. for (i = 0; i < range; i++) {
  901. if (i == colonpos) {
  902. if (needcolon || i == 0)
  903. *p++ = ':';
  904. *p++ = ':';
  905. needcolon = false;
  906. i += longest - 1;
  907. continue;
  908. }
  909. if (needcolon) {
  910. *p++ = ':';
  911. needcolon = false;
  912. }
  913. /* hex u16 without leading 0s */
  914. word = ntohs(in6.s6_addr16[i]);
  915. hi = word >> 8;
  916. lo = word & 0xff;
  917. if (hi) {
  918. if (hi > 0x0f)
  919. p = hex_byte_pack(p, hi);
  920. else
  921. *p++ = hex_asc_lo(hi);
  922. p = hex_byte_pack(p, lo);
  923. }
  924. else if (lo > 0x0f)
  925. p = hex_byte_pack(p, lo);
  926. else
  927. *p++ = hex_asc_lo(lo);
  928. needcolon = true;
  929. }
  930. if (useIPv4) {
  931. if (needcolon)
  932. *p++ = ':';
  933. p = ip4_string(p, &in6.s6_addr[12], "I4");
  934. }
  935. *p = '\0';
  936. return p;
  937. }
  938. static noinline_for_stack
  939. char *ip6_string(char *p, const char *addr, const char *fmt)
  940. {
  941. int i;
  942. for (i = 0; i < 8; i++) {
  943. p = hex_byte_pack(p, *addr++);
  944. p = hex_byte_pack(p, *addr++);
  945. if (fmt[0] == 'I' && i != 7)
  946. *p++ = ':';
  947. }
  948. *p = '\0';
  949. return p;
  950. }
  951. static noinline_for_stack
  952. char *ip6_addr_string(char *buf, char *end, const u8 *addr,
  953. struct printf_spec spec, const char *fmt)
  954. {
  955. char ip6_addr[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
  956. if (fmt[0] == 'I' && fmt[2] == 'c')
  957. ip6_compressed_string(ip6_addr, addr);
  958. else
  959. ip6_string(ip6_addr, addr, fmt);
  960. return string(buf, end, ip6_addr, spec);
  961. }
  962. static noinline_for_stack
  963. char *ip4_addr_string(char *buf, char *end, const u8 *addr,
  964. struct printf_spec spec, const char *fmt)
  965. {
  966. char ip4_addr[sizeof("255.255.255.255")];
  967. ip4_string(ip4_addr, addr, fmt);
  968. return string(buf, end, ip4_addr, spec);
  969. }
  970. static noinline_for_stack
  971. char *ip6_addr_string_sa(char *buf, char *end, const struct sockaddr_in6 *sa,
  972. struct printf_spec spec, const char *fmt)
  973. {
  974. bool have_p = false, have_s = false, have_f = false, have_c = false;
  975. char ip6_addr[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
  976. sizeof(":12345") + sizeof("/123456789") +
  977. sizeof("%1234567890")];
  978. char *p = ip6_addr, *pend = ip6_addr + sizeof(ip6_addr);
  979. const u8 *addr = (const u8 *) &sa->sin6_addr;
  980. char fmt6[2] = { fmt[0], '6' };
  981. u8 off = 0;
  982. fmt++;
  983. while (isalpha(*++fmt)) {
  984. switch (*fmt) {
  985. case 'p':
  986. have_p = true;
  987. break;
  988. case 'f':
  989. have_f = true;
  990. break;
  991. case 's':
  992. have_s = true;
  993. break;
  994. case 'c':
  995. have_c = true;
  996. break;
  997. }
  998. }
  999. if (have_p || have_s || have_f) {
  1000. *p = '[';
  1001. off = 1;
  1002. }
  1003. if (fmt6[0] == 'I' && have_c)
  1004. p = ip6_compressed_string(ip6_addr + off, addr);
  1005. else
  1006. p = ip6_string(ip6_addr + off, addr, fmt6);
  1007. if (have_p || have_s || have_f)
  1008. *p++ = ']';
  1009. if (have_p) {
  1010. *p++ = ':';
  1011. p = number(p, pend, ntohs(sa->sin6_port), spec);
  1012. }
  1013. if (have_f) {
  1014. *p++ = '/';
  1015. p = number(p, pend, ntohl(sa->sin6_flowinfo &
  1016. IPV6_FLOWINFO_MASK), spec);
  1017. }
  1018. if (have_s) {
  1019. *p++ = '%';
  1020. p = number(p, pend, sa->sin6_scope_id, spec);
  1021. }
  1022. *p = '\0';
  1023. return string(buf, end, ip6_addr, spec);
  1024. }
  1025. static noinline_for_stack
  1026. char *ip4_addr_string_sa(char *buf, char *end, const struct sockaddr_in *sa,
  1027. struct printf_spec spec, const char *fmt)
  1028. {
  1029. bool have_p = false;
  1030. char *p, ip4_addr[sizeof("255.255.255.255") + sizeof(":12345")];
  1031. char *pend = ip4_addr + sizeof(ip4_addr);
  1032. const u8 *addr = (const u8 *) &sa->sin_addr.s_addr;
  1033. char fmt4[3] = { fmt[0], '4', 0 };
  1034. fmt++;
  1035. while (isalpha(*++fmt)) {
  1036. switch (*fmt) {
  1037. case 'p':
  1038. have_p = true;
  1039. break;
  1040. case 'h':
  1041. case 'l':
  1042. case 'n':
  1043. case 'b':
  1044. fmt4[2] = *fmt;
  1045. break;
  1046. }
  1047. }
  1048. p = ip4_string(ip4_addr, addr, fmt4);
  1049. if (have_p) {
  1050. *p++ = ':';
  1051. p = number(p, pend, ntohs(sa->sin_port), spec);
  1052. }
  1053. *p = '\0';
  1054. return string(buf, end, ip4_addr, spec);
  1055. }
  1056. static noinline_for_stack
  1057. char *escaped_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
  1058. const char *fmt)
  1059. {
  1060. bool found = true;
  1061. int count = 1;
  1062. unsigned int flags = 0;
  1063. int len;
  1064. if (spec.field_width == 0)
  1065. return buf; /* nothing to print */
  1066. if (ZERO_OR_NULL_PTR(addr))
  1067. return string(buf, end, NULL, spec); /* NULL pointer */
  1068. do {
  1069. switch (fmt[count++]) {
  1070. case 'a':
  1071. flags |= ESCAPE_ANY;
  1072. break;
  1073. case 'c':
  1074. flags |= ESCAPE_SPECIAL;
  1075. break;
  1076. case 'h':
  1077. flags |= ESCAPE_HEX;
  1078. break;
  1079. case 'n':
  1080. flags |= ESCAPE_NULL;
  1081. break;
  1082. case 'o':
  1083. flags |= ESCAPE_OCTAL;
  1084. break;
  1085. case 'p':
  1086. flags |= ESCAPE_NP;
  1087. break;
  1088. case 's':
  1089. flags |= ESCAPE_SPACE;
  1090. break;
  1091. default:
  1092. found = false;
  1093. break;
  1094. }
  1095. } while (found);
  1096. if (!flags)
  1097. flags = ESCAPE_ANY_NP;
  1098. len = spec.field_width < 0 ? 1 : spec.field_width;
  1099. /*
  1100. * string_escape_mem() writes as many characters as it can to
  1101. * the given buffer, and returns the total size of the output
  1102. * had the buffer been big enough.
  1103. */
  1104. buf += string_escape_mem(addr, len, buf, buf < end ? end - buf : 0, flags, NULL);
  1105. return buf;
  1106. }
  1107. static noinline_for_stack
  1108. char *uuid_string(char *buf, char *end, const u8 *addr,
  1109. struct printf_spec spec, const char *fmt)
  1110. {
  1111. char uuid[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
  1112. char *p = uuid;
  1113. int i;
  1114. static const u8 be[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
  1115. static const u8 le[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
  1116. const u8 *index = be;
  1117. bool uc = false;
  1118. switch (*(++fmt)) {
  1119. case 'L':
  1120. uc = true; /* fall-through */
  1121. case 'l':
  1122. index = le;
  1123. break;
  1124. case 'B':
  1125. uc = true;
  1126. break;
  1127. }
  1128. for (i = 0; i < 16; i++) {
  1129. p = hex_byte_pack(p, addr[index[i]]);
  1130. switch (i) {
  1131. case 3:
  1132. case 5:
  1133. case 7:
  1134. case 9:
  1135. *p++ = '-';
  1136. break;
  1137. }
  1138. }
  1139. *p = 0;
  1140. if (uc) {
  1141. p = uuid;
  1142. do {
  1143. *p = toupper(*p);
  1144. } while (*(++p));
  1145. }
  1146. return string(buf, end, uuid, spec);
  1147. }
  1148. static
  1149. char *netdev_feature_string(char *buf, char *end, const u8 *addr,
  1150. struct printf_spec spec)
  1151. {
  1152. spec.flags |= SPECIAL | SMALL | ZEROPAD;
  1153. if (spec.field_width == -1)
  1154. spec.field_width = 2 + 2 * sizeof(netdev_features_t);
  1155. spec.base = 16;
  1156. return number(buf, end, *(const netdev_features_t *)addr, spec);
  1157. }
  1158. static noinline_for_stack
  1159. char *address_val(char *buf, char *end, const void *addr,
  1160. struct printf_spec spec, const char *fmt)
  1161. {
  1162. unsigned long long num;
  1163. spec.flags |= SPECIAL | SMALL | ZEROPAD;
  1164. spec.base = 16;
  1165. switch (fmt[1]) {
  1166. case 'd':
  1167. num = *(const dma_addr_t *)addr;
  1168. spec.field_width = sizeof(dma_addr_t) * 2 + 2;
  1169. break;
  1170. case 'p':
  1171. default:
  1172. num = *(const phys_addr_t *)addr;
  1173. spec.field_width = sizeof(phys_addr_t) * 2 + 2;
  1174. break;
  1175. }
  1176. return number(buf, end, num, spec);
  1177. }
  1178. static noinline_for_stack
  1179. char *clock(char *buf, char *end, struct clk *clk, struct printf_spec spec,
  1180. const char *fmt)
  1181. {
  1182. if (!IS_ENABLED(CONFIG_HAVE_CLK) || !clk)
  1183. return string(buf, end, NULL, spec);
  1184. switch (fmt[1]) {
  1185. case 'r':
  1186. return number(buf, end, clk_get_rate(clk), spec);
  1187. case 'n':
  1188. default:
  1189. #ifdef CONFIG_COMMON_CLK
  1190. return string(buf, end, __clk_get_name(clk), spec);
  1191. #else
  1192. spec.base = 16;
  1193. spec.field_width = sizeof(unsigned long) * 2 + 2;
  1194. spec.flags |= SPECIAL | SMALL | ZEROPAD;
  1195. return number(buf, end, (unsigned long)clk, spec);
  1196. #endif
  1197. }
  1198. }
  1199. int kptr_restrict __read_mostly;
  1200. /*
  1201. * Show a '%p' thing. A kernel extension is that the '%p' is followed
  1202. * by an extra set of alphanumeric characters that are extended format
  1203. * specifiers.
  1204. *
  1205. * Right now we handle:
  1206. *
  1207. * - 'F' For symbolic function descriptor pointers with offset
  1208. * - 'f' For simple symbolic function names without offset
  1209. * - 'S' For symbolic direct pointers with offset
  1210. * - 's' For symbolic direct pointers without offset
  1211. * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
  1212. * - 'B' For backtraced symbolic direct pointers with offset
  1213. * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
  1214. * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
  1215. * - 'b[l]' For a bitmap, the number of bits is determined by the field
  1216. * width which must be explicitly specified either as part of the
  1217. * format string '%32b[l]' or through '%*b[l]', [l] selects
  1218. * range-list format instead of hex format
  1219. * - 'M' For a 6-byte MAC address, it prints the address in the
  1220. * usual colon-separated hex notation
  1221. * - 'm' For a 6-byte MAC address, it prints the hex address without colons
  1222. * - 'MF' For a 6-byte MAC FDDI address, it prints the address
  1223. * with a dash-separated hex notation
  1224. * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
  1225. * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
  1226. * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
  1227. * IPv6 uses colon separated network-order 16 bit hex with leading 0's
  1228. * [S][pfs]
  1229. * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
  1230. * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
  1231. * - 'i' [46] for 'raw' IPv4/IPv6 addresses
  1232. * IPv6 omits the colons (01020304...0f)
  1233. * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
  1234. * [S][pfs]
  1235. * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
  1236. * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
  1237. * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
  1238. * - 'I[6S]c' for IPv6 addresses printed as specified by
  1239. * http://tools.ietf.org/html/rfc5952
  1240. * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
  1241. * of the following flags (see string_escape_mem() for the
  1242. * details):
  1243. * a - ESCAPE_ANY
  1244. * c - ESCAPE_SPECIAL
  1245. * h - ESCAPE_HEX
  1246. * n - ESCAPE_NULL
  1247. * o - ESCAPE_OCTAL
  1248. * p - ESCAPE_NP
  1249. * s - ESCAPE_SPACE
  1250. * By default ESCAPE_ANY_NP is used.
  1251. * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
  1252. * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
  1253. * Options for %pU are:
  1254. * b big endian lower case hex (default)
  1255. * B big endian UPPER case hex
  1256. * l little endian lower case hex
  1257. * L little endian UPPER case hex
  1258. * big endian output byte order is:
  1259. * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
  1260. * little endian output byte order is:
  1261. * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
  1262. * - 'V' For a struct va_format which contains a format string * and va_list *,
  1263. * call vsnprintf(->format, *->va_list).
  1264. * Implements a "recursive vsnprintf".
  1265. * Do not use this feature without some mechanism to verify the
  1266. * correctness of the format string and va_list arguments.
  1267. * - 'K' For a kernel pointer that should be hidden from unprivileged users
  1268. * - 'NF' For a netdev_features_t
  1269. * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
  1270. * a certain separator (' ' by default):
  1271. * C colon
  1272. * D dash
  1273. * N no separator
  1274. * The maximum supported length is 64 bytes of the input. Consider
  1275. * to use print_hex_dump() for the larger input.
  1276. * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
  1277. * (default assumed to be phys_addr_t, passed by reference)
  1278. * - 'd[234]' For a dentry name (optionally 2-4 last components)
  1279. * - 'D[234]' Same as 'd' but for a struct file
  1280. * - 'C' For a clock, it prints the name (Common Clock Framework) or address
  1281. * (legacy clock framework) of the clock
  1282. * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
  1283. * (legacy clock framework) of the clock
  1284. * - 'Cr' For a clock, it prints the current rate of the clock
  1285. * - 'n' For ignored argument
  1286. *
  1287. * ** Please update also Documentation/printk-formats.txt when making changes **
  1288. *
  1289. * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
  1290. * function pointers are really function descriptors, which contain a
  1291. * pointer to the real address.
  1292. */
  1293. static noinline_for_stack
  1294. char *pointer(const char *fmt, char *buf, char *end, void *ptr,
  1295. struct printf_spec spec)
  1296. {
  1297. int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);
  1298. if (!ptr && *fmt != 'K') {
  1299. /*
  1300. * Print (null) with the same width as a pointer so it makes
  1301. * tabular output look nice.
  1302. */
  1303. if (spec.field_width == -1)
  1304. spec.field_width = default_width;
  1305. return string(buf, end, "(null)", spec);
  1306. }
  1307. switch (*fmt) {
  1308. case 'F':
  1309. case 'f':
  1310. ptr = dereference_function_descriptor(ptr);
  1311. /* Fallthrough */
  1312. case 'S':
  1313. case 's':
  1314. case 'B':
  1315. return symbol_string(buf, end, ptr, spec, fmt);
  1316. case 'R':
  1317. case 'r':
  1318. return resource_string(buf, end, ptr, spec, fmt);
  1319. case 'h':
  1320. return hex_string(buf, end, ptr, spec, fmt);
  1321. case 'b':
  1322. switch (fmt[1]) {
  1323. case 'l':
  1324. return bitmap_list_string(buf, end, ptr, spec, fmt);
  1325. default:
  1326. return bitmap_string(buf, end, ptr, spec, fmt);
  1327. }
  1328. case 'M': /* Colon separated: 00:01:02:03:04:05 */
  1329. case 'm': /* Contiguous: 000102030405 */
  1330. /* [mM]F (FDDI) */
  1331. /* [mM]R (Reverse order; Bluetooth) */
  1332. return mac_address_string(buf, end, ptr, spec, fmt);
  1333. case 'I': /* Formatted IP supported
  1334. * 4: 1.2.3.4
  1335. * 6: 0001:0203:...:0708
  1336. * 6c: 1::708 or 1::1.2.3.4
  1337. */
  1338. case 'i': /* Contiguous:
  1339. * 4: 001.002.003.004
  1340. * 6: 000102...0f
  1341. */
  1342. switch (fmt[1]) {
  1343. case '6':
  1344. return ip6_addr_string(buf, end, ptr, spec, fmt);
  1345. case '4':
  1346. return ip4_addr_string(buf, end, ptr, spec, fmt);
  1347. case 'S': {
  1348. const union {
  1349. struct sockaddr raw;
  1350. struct sockaddr_in v4;
  1351. struct sockaddr_in6 v6;
  1352. } *sa = ptr;
  1353. switch (sa->raw.sa_family) {
  1354. case AF_INET:
  1355. return ip4_addr_string_sa(buf, end, &sa->v4, spec, fmt);
  1356. case AF_INET6:
  1357. return ip6_addr_string_sa(buf, end, &sa->v6, spec, fmt);
  1358. default:
  1359. return string(buf, end, "(invalid address)", spec);
  1360. }}
  1361. }
  1362. break;
  1363. case 'E':
  1364. return escaped_string(buf, end, ptr, spec, fmt);
  1365. case 'U':
  1366. return uuid_string(buf, end, ptr, spec, fmt);
  1367. case 'V':
  1368. {
  1369. va_list va;
  1370. va_copy(va, *((struct va_format *)ptr)->va);
  1371. buf += vsnprintf(buf, end > buf ? end - buf : 0,
  1372. ((struct va_format *)ptr)->fmt, va);
  1373. va_end(va);
  1374. return buf;
  1375. }
  1376. case 'K':
  1377. /*
  1378. * %pK cannot be used in IRQ context because its test
  1379. * for CAP_SYSLOG would be meaningless.
  1380. */
  1381. if (kptr_restrict && (in_irq() || in_serving_softirq() ||
  1382. in_nmi())) {
  1383. if (spec.field_width == -1)
  1384. spec.field_width = default_width;
  1385. return string(buf, end, "pK-error", spec);
  1386. }
  1387. switch (kptr_restrict) {
  1388. case 0:
  1389. /* Always print %pK values */
  1390. break;
  1391. case 1: {
  1392. /*
  1393. * Only print the real pointer value if the current
  1394. * process has CAP_SYSLOG and is running with the
  1395. * same credentials it started with. This is because
  1396. * access to files is checked at open() time, but %pK
  1397. * checks permission at read() time. We don't want to
  1398. * leak pointer values if a binary opens a file using
  1399. * %pK and then elevates privileges before reading it.
  1400. */
  1401. const struct cred *cred = current_cred();
  1402. if (!has_capability_noaudit(current, CAP_SYSLOG) ||
  1403. !uid_eq(cred->euid, cred->uid) ||
  1404. !gid_eq(cred->egid, cred->gid))
  1405. ptr = NULL;
  1406. break;
  1407. }
  1408. case 2:
  1409. default:
  1410. /* Always print 0's for %pK */
  1411. ptr = NULL;
  1412. break;
  1413. }
  1414. break;
  1415. case 'N':
  1416. switch (fmt[1]) {
  1417. case 'F':
  1418. return netdev_feature_string(buf, end, ptr, spec);
  1419. }
  1420. break;
  1421. case 'a':
  1422. return address_val(buf, end, ptr, spec, fmt);
  1423. case 'd':
  1424. return dentry_name(buf, end, ptr, spec, fmt);
  1425. case 'C':
  1426. return clock(buf, end, ptr, spec, fmt);
  1427. case 'D':
  1428. return dentry_name(buf, end,
  1429. ((const struct file *)ptr)->f_path.dentry,
  1430. spec, fmt);
  1431. }
  1432. spec.flags |= SMALL;
  1433. if (spec.field_width == -1) {
  1434. spec.field_width = default_width;
  1435. spec.flags |= ZEROPAD;
  1436. }
  1437. spec.base = 16;
  1438. return number(buf, end, (unsigned long) ptr, spec);
  1439. }
  1440. /*
  1441. * Helper function to decode printf style format.
  1442. * Each call decode a token from the format and return the
  1443. * number of characters read (or likely the delta where it wants
  1444. * to go on the next call).
  1445. * The decoded token is returned through the parameters
  1446. *
  1447. * 'h', 'l', or 'L' for integer fields
  1448. * 'z' support added 23/7/1999 S.H.
  1449. * 'z' changed to 'Z' --davidm 1/25/99
  1450. * 't' added for ptrdiff_t
  1451. *
  1452. * @fmt: the format string
  1453. * @type of the token returned
  1454. * @flags: various flags such as +, -, # tokens..
  1455. * @field_width: overwritten width
  1456. * @base: base of the number (octal, hex, ...)
  1457. * @precision: precision of a number
  1458. * @qualifier: qualifier of a number (long, size_t, ...)
  1459. */
  1460. static noinline_for_stack
  1461. int format_decode(const char *fmt, struct printf_spec *spec)
  1462. {
  1463. const char *start = fmt;
  1464. /* we finished early by reading the field width */
  1465. if (spec->type == FORMAT_TYPE_WIDTH) {
  1466. if (spec->field_width < 0) {
  1467. spec->field_width = -spec->field_width;
  1468. spec->flags |= LEFT;
  1469. }
  1470. spec->type = FORMAT_TYPE_NONE;
  1471. goto precision;
  1472. }
  1473. /* we finished early by reading the precision */
  1474. if (spec->type == FORMAT_TYPE_PRECISION) {
  1475. if (spec->precision < 0)
  1476. spec->precision = 0;
  1477. spec->type = FORMAT_TYPE_NONE;
  1478. goto qualifier;
  1479. }
  1480. /* By default */
  1481. spec->type = FORMAT_TYPE_NONE;
  1482. for (; *fmt ; ++fmt) {
  1483. if (*fmt == '%')
  1484. break;
  1485. }
  1486. /* Return the current non-format string */
  1487. if (fmt != start || !*fmt)
  1488. return fmt - start;
  1489. /* Process flags */
  1490. spec->flags = 0;
  1491. while (1) { /* this also skips first '%' */
  1492. bool found = true;
  1493. ++fmt;
  1494. switch (*fmt) {
  1495. case '-': spec->flags |= LEFT; break;
  1496. case '+': spec->flags |= PLUS; break;
  1497. case ' ': spec->flags |= SPACE; break;
  1498. case '#': spec->flags |= SPECIAL; break;
  1499. case '0': spec->flags |= ZEROPAD; break;
  1500. default: found = false;
  1501. }
  1502. if (!found)
  1503. break;
  1504. }
  1505. /* get field width */
  1506. spec->field_width = -1;
  1507. if (isdigit(*fmt))
  1508. spec->field_width = skip_atoi(&fmt);
  1509. else if (*fmt == '*') {
  1510. /* it's the next argument */
  1511. spec->type = FORMAT_TYPE_WIDTH;
  1512. return ++fmt - start;
  1513. }
  1514. precision:
  1515. /* get the precision */
  1516. spec->precision = -1;
  1517. if (*fmt == '.') {
  1518. ++fmt;
  1519. if (isdigit(*fmt)) {
  1520. spec->precision = skip_atoi(&fmt);
  1521. if (spec->precision < 0)
  1522. spec->precision = 0;
  1523. } else if (*fmt == '*') {
  1524. /* it's the next argument */
  1525. spec->type = FORMAT_TYPE_PRECISION;
  1526. return ++fmt - start;
  1527. }
  1528. }
  1529. qualifier:
  1530. /* get the conversion qualifier */
  1531. spec->qualifier = -1;
  1532. if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
  1533. _tolower(*fmt) == 'z' || *fmt == 't') {
  1534. spec->qualifier = *fmt++;
  1535. if (unlikely(spec->qualifier == *fmt)) {
  1536. if (spec->qualifier == 'l') {
  1537. spec->qualifier = 'L';
  1538. ++fmt;
  1539. } else if (spec->qualifier == 'h') {
  1540. spec->qualifier = 'H';
  1541. ++fmt;
  1542. }
  1543. }
  1544. }
  1545. /* default base */
  1546. spec->base = 10;
  1547. switch (*fmt) {
  1548. case 'c':
  1549. spec->type = FORMAT_TYPE_CHAR;
  1550. return ++fmt - start;
  1551. case 's':
  1552. spec->type = FORMAT_TYPE_STR;
  1553. return ++fmt - start;
  1554. case 'p':
  1555. spec->type = FORMAT_TYPE_PTR;
  1556. return ++fmt - start;
  1557. case '%':
  1558. spec->type = FORMAT_TYPE_PERCENT_CHAR;
  1559. return ++fmt - start;
  1560. /* integer number formats - set up the flags and "break" */
  1561. case 'o':
  1562. spec->base = 8;
  1563. break;
  1564. case 'x':
  1565. spec->flags |= SMALL;
  1566. case 'X':
  1567. spec->base = 16;
  1568. break;
  1569. case 'd':
  1570. case 'i':
  1571. spec->flags |= SIGN;
  1572. case 'u':
  1573. break;
  1574. case 'n':
  1575. /*
  1576. * Since %n poses a greater security risk than
  1577. * utility, treat it as any other invalid or
  1578. * unsupported format specifier.
  1579. */
  1580. /* Fall-through */
  1581. default:
  1582. WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
  1583. spec->type = FORMAT_TYPE_INVALID;
  1584. return fmt - start;
  1585. }
  1586. if (spec->qualifier == 'L')
  1587. spec->type = FORMAT_TYPE_LONG_LONG;
  1588. else if (spec->qualifier == 'l') {
  1589. BUILD_BUG_ON(FORMAT_TYPE_ULONG + SIGN != FORMAT_TYPE_LONG);
  1590. spec->type = FORMAT_TYPE_ULONG + (spec->flags & SIGN);
  1591. } else if (_tolower(spec->qualifier) == 'z') {
  1592. spec->type = FORMAT_TYPE_SIZE_T;
  1593. } else if (spec->qualifier == 't') {
  1594. spec->type = FORMAT_TYPE_PTRDIFF;
  1595. } else if (spec->qualifier == 'H') {
  1596. BUILD_BUG_ON(FORMAT_TYPE_UBYTE + SIGN != FORMAT_TYPE_BYTE);
  1597. spec->type = FORMAT_TYPE_UBYTE + (spec->flags & SIGN);
  1598. } else if (spec->qualifier == 'h') {
  1599. BUILD_BUG_ON(FORMAT_TYPE_USHORT + SIGN != FORMAT_TYPE_SHORT);
  1600. spec->type = FORMAT_TYPE_USHORT + (spec->flags & SIGN);
  1601. } else {
  1602. BUILD_BUG_ON(FORMAT_TYPE_UINT + SIGN != FORMAT_TYPE_INT);
  1603. spec->type = FORMAT_TYPE_UINT + (spec->flags & SIGN);
  1604. }
  1605. return ++fmt - start;
  1606. }
  1607. /**
  1608. * vsnprintf - Format a string and place it in a buffer
  1609. * @buf: The buffer to place the result into
  1610. * @size: The size of the buffer, including the trailing null space
  1611. * @fmt: The format string to use
  1612. * @args: Arguments for the format string
  1613. *
  1614. * This function follows C99 vsnprintf, but has some extensions:
  1615. * %n is ignored
  1616. * %p* is handled by pointer()
  1617. *
  1618. * See pointer() or Documentation/printk-formats.txt for more
  1619. * extensive description.
  1620. *
  1621. * ** Please update the documentation in both places when making changes **
  1622. *
  1623. * The return value is the number of characters which would
  1624. * be generated for the given input, excluding the trailing
  1625. * '\0', as per ISO C99. If you want to have the exact
  1626. * number of characters written into @buf as return value
  1627. * (not including the trailing '\0'), use vscnprintf(). If the
  1628. * return is greater than or equal to @size, the resulting
  1629. * string is truncated.
  1630. *
  1631. * If you're not already dealing with a va_list consider using snprintf().
  1632. */
  1633. int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
  1634. {
  1635. unsigned long long num;
  1636. char *str, *end;
  1637. struct printf_spec spec = {0};
  1638. /* Reject out-of-range values early. Large positive sizes are
  1639. used for unknown buffer sizes. */
  1640. if (WARN_ON_ONCE(size > INT_MAX))
  1641. return 0;
  1642. str = buf;
  1643. end = buf + size;
  1644. /* Make sure end is always >= buf */
  1645. if (end < buf) {
  1646. end = ((void *)-1);
  1647. size = end - buf;
  1648. }
  1649. while (*fmt) {
  1650. const char *old_fmt = fmt;
  1651. int read = format_decode(fmt, &spec);
  1652. fmt += read;
  1653. switch (spec.type) {
  1654. case FORMAT_TYPE_NONE: {
  1655. int copy = read;
  1656. if (str < end) {
  1657. if (copy > end - str)
  1658. copy = end - str;
  1659. memcpy(str, old_fmt, copy);
  1660. }
  1661. str += read;
  1662. break;
  1663. }
  1664. case FORMAT_TYPE_WIDTH:
  1665. spec.field_width = va_arg(args, int);
  1666. break;
  1667. case FORMAT_TYPE_PRECISION:
  1668. spec.precision = va_arg(args, int);
  1669. break;
  1670. case FORMAT_TYPE_CHAR: {
  1671. char c;
  1672. if (!(spec.flags & LEFT)) {
  1673. while (--spec.field_width > 0) {
  1674. if (str < end)
  1675. *str = ' ';
  1676. ++str;
  1677. }
  1678. }
  1679. c = (unsigned char) va_arg(args, int);
  1680. if (str < end)
  1681. *str = c;
  1682. ++str;
  1683. while (--spec.field_width > 0) {
  1684. if (str < end)
  1685. *str = ' ';
  1686. ++str;
  1687. }
  1688. break;
  1689. }
  1690. case FORMAT_TYPE_STR:
  1691. str = string(str, end, va_arg(args, char *), spec);
  1692. break;
  1693. case FORMAT_TYPE_PTR:
  1694. str = pointer(fmt, str, end, va_arg(args, void *),
  1695. spec);
  1696. while (isalnum(*fmt))
  1697. fmt++;
  1698. break;
  1699. case FORMAT_TYPE_PERCENT_CHAR:
  1700. if (str < end)
  1701. *str = '%';
  1702. ++str;
  1703. break;
  1704. case FORMAT_TYPE_INVALID:
  1705. /*
  1706. * Presumably the arguments passed gcc's type
  1707. * checking, but there is no safe or sane way
  1708. * for us to continue parsing the format and
  1709. * fetching from the va_list; the remaining
  1710. * specifiers and arguments would be out of
  1711. * sync.
  1712. */
  1713. goto out;
  1714. default:
  1715. switch (spec.type) {
  1716. case FORMAT_TYPE_LONG_LONG:
  1717. num = va_arg(args, long long);
  1718. break;
  1719. case FORMAT_TYPE_ULONG:
  1720. num = va_arg(args, unsigned long);
  1721. break;
  1722. case FORMAT_TYPE_LONG:
  1723. num = va_arg(args, long);
  1724. break;
  1725. case FORMAT_TYPE_SIZE_T:
  1726. if (spec.flags & SIGN)
  1727. num = va_arg(args, ssize_t);
  1728. else
  1729. num = va_arg(args, size_t);
  1730. break;
  1731. case FORMAT_TYPE_PTRDIFF:
  1732. num = va_arg(args, ptrdiff_t);
  1733. break;
  1734. case FORMAT_TYPE_UBYTE:
  1735. num = (unsigned char) va_arg(args, int);
  1736. break;
  1737. case FORMAT_TYPE_BYTE:
  1738. num = (signed char) va_arg(args, int);
  1739. break;
  1740. case FORMAT_TYPE_USHORT:
  1741. num = (unsigned short) va_arg(args, int);
  1742. break;
  1743. case FORMAT_TYPE_SHORT:
  1744. num = (short) va_arg(args, int);
  1745. break;
  1746. case FORMAT_TYPE_INT:
  1747. num = (int) va_arg(args, int);
  1748. break;
  1749. default:
  1750. num = va_arg(args, unsigned int);
  1751. }
  1752. str = number(str, end, num, spec);
  1753. }
  1754. }
  1755. out:
  1756. if (size > 0) {
  1757. if (str < end)
  1758. *str = '\0';
  1759. else
  1760. end[-1] = '\0';
  1761. }
  1762. /* the trailing null byte doesn't count towards the total */
  1763. return str-buf;
  1764. }
  1765. EXPORT_SYMBOL(vsnprintf);
  1766. /**
  1767. * vscnprintf - Format a string and place it in a buffer
  1768. * @buf: The buffer to place the result into
  1769. * @size: The size of the buffer, including the trailing null space
  1770. * @fmt: The format string to use
  1771. * @args: Arguments for the format string
  1772. *
  1773. * The return value is the number of characters which have been written into
  1774. * the @buf not including the trailing '\0'. If @size is == 0 the function
  1775. * returns 0.
  1776. *
  1777. * If you're not already dealing with a va_list consider using scnprintf().
  1778. *
  1779. * See the vsnprintf() documentation for format string extensions over C99.
  1780. */
  1781. int vscnprintf(char *buf, size_t size, const char *fmt, va_list args)
  1782. {
  1783. int i;
  1784. i = vsnprintf(buf, size, fmt, args);
  1785. if (likely(i < size))
  1786. return i;
  1787. if (size != 0)
  1788. return size - 1;
  1789. return 0;
  1790. }
  1791. EXPORT_SYMBOL(vscnprintf);
  1792. /**
  1793. * snprintf - Format a string and place it in a buffer
  1794. * @buf: The buffer to place the result into
  1795. * @size: The size of the buffer, including the trailing null space
  1796. * @fmt: The format string to use
  1797. * @...: Arguments for the format string
  1798. *
  1799. * The return value is the number of characters which would be
  1800. * generated for the given input, excluding the trailing null,
  1801. * as per ISO C99. If the return is greater than or equal to
  1802. * @size, the resulting string is truncated.
  1803. *
  1804. * See the vsnprintf() documentation for format string extensions over C99.
  1805. */
  1806. int snprintf(char *buf, size_t size, const char *fmt, ...)
  1807. {
  1808. va_list args;
  1809. int i;
  1810. va_start(args, fmt);
  1811. i = vsnprintf(buf, size, fmt, args);
  1812. va_end(args);
  1813. return i;
  1814. }
  1815. EXPORT_SYMBOL(snprintf);
  1816. /**
  1817. * scnprintf - Format a string and place it in a buffer
  1818. * @buf: The buffer to place the result into
  1819. * @size: The size of the buffer, including the trailing null space
  1820. * @fmt: The format string to use
  1821. * @...: Arguments for the format string
  1822. *
  1823. * The return value is the number of characters written into @buf not including
  1824. * the trailing '\0'. If @size is == 0 the function returns 0.
  1825. */
  1826. int scnprintf(char *buf, size_t size, const char *fmt, ...)
  1827. {
  1828. va_list args;
  1829. int i;
  1830. va_start(args, fmt);
  1831. i = vscnprintf(buf, size, fmt, args);
  1832. va_end(args);
  1833. return i;
  1834. }
  1835. EXPORT_SYMBOL(scnprintf);
  1836. /**
  1837. * vsprintf - Format a string and place it in a buffer
  1838. * @buf: The buffer to place the result into
  1839. * @fmt: The format string to use
  1840. * @args: Arguments for the format string
  1841. *
  1842. * The function returns the number of characters written
  1843. * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
  1844. * buffer overflows.
  1845. *
  1846. * If you're not already dealing with a va_list consider using sprintf().
  1847. *
  1848. * See the vsnprintf() documentation for format string extensions over C99.
  1849. */
  1850. int vsprintf(char *buf, const char *fmt, va_list args)
  1851. {
  1852. return vsnprintf(buf, INT_MAX, fmt, args);
  1853. }
  1854. EXPORT_SYMBOL(vsprintf);
  1855. /**
  1856. * sprintf - Format a string and place it in a buffer
  1857. * @buf: The buffer to place the result into
  1858. * @fmt: The format string to use
  1859. * @...: Arguments for the format string
  1860. *
  1861. * The function returns the number of characters written
  1862. * into @buf. Use snprintf() or scnprintf() in order to avoid
  1863. * buffer overflows.
  1864. *
  1865. * See the vsnprintf() documentation for format string extensions over C99.
  1866. */
  1867. int sprintf(char *buf, const char *fmt, ...)
  1868. {
  1869. va_list args;
  1870. int i;
  1871. va_start(args, fmt);
  1872. i = vsnprintf(buf, INT_MAX, fmt, args);
  1873. va_end(args);
  1874. return i;
  1875. }
  1876. EXPORT_SYMBOL(sprintf);
  1877. #ifdef CONFIG_BINARY_PRINTF
  1878. /*
  1879. * bprintf service:
  1880. * vbin_printf() - VA arguments to binary data
  1881. * bstr_printf() - Binary data to text string
  1882. */
  1883. /**
  1884. * vbin_printf - Parse a format string and place args' binary value in a buffer
  1885. * @bin_buf: The buffer to place args' binary value
  1886. * @size: The size of the buffer(by words(32bits), not characters)
  1887. * @fmt: The format string to use
  1888. * @args: Arguments for the format string
  1889. *
  1890. * The format follows C99 vsnprintf, except %n is ignored, and its argument
  1891. * is skipped.
  1892. *
  1893. * The return value is the number of words(32bits) which would be generated for
  1894. * the given input.
  1895. *
  1896. * NOTE:
  1897. * If the return value is greater than @size, the resulting bin_buf is NOT
  1898. * valid for bstr_printf().
  1899. */
  1900. int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
  1901. {
  1902. struct printf_spec spec = {0};
  1903. char *str, *end;
  1904. str = (char *)bin_buf;
  1905. end = (char *)(bin_buf + size);
  1906. #define save_arg(type) \
  1907. do { \
  1908. if (sizeof(type) == 8) { \
  1909. unsigned long long value; \
  1910. str = PTR_ALIGN(str, sizeof(u32)); \
  1911. value = va_arg(args, unsigned long long); \
  1912. if (str + sizeof(type) <= end) { \
  1913. *(u32 *)str = *(u32 *)&value; \
  1914. *(u32 *)(str + 4) = *((u32 *)&value + 1); \
  1915. } \
  1916. } else { \
  1917. unsigned long value; \
  1918. str = PTR_ALIGN(str, sizeof(type)); \
  1919. value = va_arg(args, int); \
  1920. if (str + sizeof(type) <= end) \
  1921. *(typeof(type) *)str = (type)value; \
  1922. } \
  1923. str += sizeof(type); \
  1924. } while (0)
  1925. while (*fmt) {
  1926. int read = format_decode(fmt, &spec);
  1927. fmt += read;
  1928. switch (spec.type) {
  1929. case FORMAT_TYPE_NONE:
  1930. case FORMAT_TYPE_PERCENT_CHAR:
  1931. break;
  1932. case FORMAT_TYPE_INVALID:
  1933. goto out;
  1934. case FORMAT_TYPE_WIDTH:
  1935. case FORMAT_TYPE_PRECISION:
  1936. save_arg(int);
  1937. break;
  1938. case FORMAT_TYPE_CHAR:
  1939. save_arg(char);
  1940. break;
  1941. case FORMAT_TYPE_STR: {
  1942. const char *save_str = va_arg(args, char *);
  1943. size_t len;
  1944. if ((unsigned long)save_str > (unsigned long)-PAGE_SIZE
  1945. || (unsigned long)save_str < PAGE_SIZE)
  1946. save_str = "(null)";
  1947. len = strlen(save_str) + 1;
  1948. if (str + len < end)
  1949. memcpy(str, save_str, len);
  1950. str += len;
  1951. break;
  1952. }
  1953. case FORMAT_TYPE_PTR:
  1954. save_arg(void *);
  1955. /* skip all alphanumeric pointer suffixes */
  1956. while (isalnum(*fmt))
  1957. fmt++;
  1958. break;
  1959. default:
  1960. switch (spec.type) {
  1961. case FORMAT_TYPE_LONG_LONG:
  1962. save_arg(long long);
  1963. break;
  1964. case FORMAT_TYPE_ULONG:
  1965. case FORMAT_TYPE_LONG:
  1966. save_arg(unsigned long);
  1967. break;
  1968. case FORMAT_TYPE_SIZE_T:
  1969. save_arg(size_t);
  1970. break;
  1971. case FORMAT_TYPE_PTRDIFF:
  1972. save_arg(ptrdiff_t);
  1973. break;
  1974. case FORMAT_TYPE_UBYTE:
  1975. case FORMAT_TYPE_BYTE:
  1976. save_arg(char);
  1977. break;
  1978. case FORMAT_TYPE_USHORT:
  1979. case FORMAT_TYPE_SHORT:
  1980. save_arg(short);
  1981. break;
  1982. default:
  1983. save_arg(int);
  1984. }
  1985. }
  1986. }
  1987. out:
  1988. return (u32 *)(PTR_ALIGN(str, sizeof(u32))) - bin_buf;
  1989. #undef save_arg
  1990. }
  1991. EXPORT_SYMBOL_GPL(vbin_printf);
  1992. /**
  1993. * bstr_printf - Format a string from binary arguments and place it in a buffer
  1994. * @buf: The buffer to place the result into
  1995. * @size: The size of the buffer, including the trailing null space
  1996. * @fmt: The format string to use
  1997. * @bin_buf: Binary arguments for the format string
  1998. *
  1999. * This function like C99 vsnprintf, but the difference is that vsnprintf gets
  2000. * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
  2001. * a binary buffer that generated by vbin_printf.
  2002. *
  2003. * The format follows C99 vsnprintf, but has some extensions:
  2004. * see vsnprintf comment for details.
  2005. *
  2006. * The return value is the number of characters which would
  2007. * be generated for the given input, excluding the trailing
  2008. * '\0', as per ISO C99. If you want to have the exact
  2009. * number of characters written into @buf as return value
  2010. * (not including the trailing '\0'), use vscnprintf(). If the
  2011. * return is greater than or equal to @size, the resulting
  2012. * string is truncated.
  2013. */
  2014. int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
  2015. {
  2016. struct printf_spec spec = {0};
  2017. char *str, *end;
  2018. const char *args = (const char *)bin_buf;
  2019. if (WARN_ON_ONCE((int) size < 0))
  2020. return 0;
  2021. str = buf;
  2022. end = buf + size;
  2023. #define get_arg(type) \
  2024. ({ \
  2025. typeof(type) value; \
  2026. if (sizeof(type) == 8) { \
  2027. args = PTR_ALIGN(args, sizeof(u32)); \
  2028. *(u32 *)&value = *(u32 *)args; \
  2029. *((u32 *)&value + 1) = *(u32 *)(args + 4); \
  2030. } else { \
  2031. args = PTR_ALIGN(args, sizeof(type)); \
  2032. value = *(typeof(type) *)args; \
  2033. } \
  2034. args += sizeof(type); \
  2035. value; \
  2036. })
  2037. /* Make sure end is always >= buf */
  2038. if (end < buf) {
  2039. end = ((void *)-1);
  2040. size = end - buf;
  2041. }
  2042. while (*fmt) {
  2043. const char *old_fmt = fmt;
  2044. int read = format_decode(fmt, &spec);
  2045. fmt += read;
  2046. switch (spec.type) {
  2047. case FORMAT_TYPE_NONE: {
  2048. int copy = read;
  2049. if (str < end) {
  2050. if (copy > end - str)
  2051. copy = end - str;
  2052. memcpy(str, old_fmt, copy);
  2053. }
  2054. str += read;
  2055. break;
  2056. }
  2057. case FORMAT_TYPE_WIDTH:
  2058. spec.field_width = get_arg(int);
  2059. break;
  2060. case FORMAT_TYPE_PRECISION:
  2061. spec.precision = get_arg(int);
  2062. break;
  2063. case FORMAT_TYPE_CHAR: {
  2064. char c;
  2065. if (!(spec.flags & LEFT)) {
  2066. while (--spec.field_width > 0) {
  2067. if (str < end)
  2068. *str = ' ';
  2069. ++str;
  2070. }
  2071. }
  2072. c = (unsigned char) get_arg(char);
  2073. if (str < end)
  2074. *str = c;
  2075. ++str;
  2076. while (--spec.field_width > 0) {
  2077. if (str < end)
  2078. *str = ' ';
  2079. ++str;
  2080. }
  2081. break;
  2082. }
  2083. case FORMAT_TYPE_STR: {
  2084. const char *str_arg = args;
  2085. args += strlen(str_arg) + 1;
  2086. str = string(str, end, (char *)str_arg, spec);
  2087. break;
  2088. }
  2089. case FORMAT_TYPE_PTR:
  2090. str = pointer(fmt, str, end, get_arg(void *), spec);
  2091. while (isalnum(*fmt))
  2092. fmt++;
  2093. break;
  2094. case FORMAT_TYPE_PERCENT_CHAR:
  2095. if (str < end)
  2096. *str = '%';
  2097. ++str;
  2098. break;
  2099. case FORMAT_TYPE_INVALID:
  2100. goto out;
  2101. default: {
  2102. unsigned long long num;
  2103. switch (spec.type) {
  2104. case FORMAT_TYPE_LONG_LONG:
  2105. num = get_arg(long long);
  2106. break;
  2107. case FORMAT_TYPE_ULONG:
  2108. case FORMAT_TYPE_LONG:
  2109. num = get_arg(unsigned long);
  2110. break;
  2111. case FORMAT_TYPE_SIZE_T:
  2112. num = get_arg(size_t);
  2113. break;
  2114. case FORMAT_TYPE_PTRDIFF:
  2115. num = get_arg(ptrdiff_t);
  2116. break;
  2117. case FORMAT_TYPE_UBYTE:
  2118. num = get_arg(unsigned char);
  2119. break;
  2120. case FORMAT_TYPE_BYTE:
  2121. num = get_arg(signed char);
  2122. break;
  2123. case FORMAT_TYPE_USHORT:
  2124. num = get_arg(unsigned short);
  2125. break;
  2126. case FORMAT_TYPE_SHORT:
  2127. num = get_arg(short);
  2128. break;
  2129. case FORMAT_TYPE_UINT:
  2130. num = get_arg(unsigned int);
  2131. break;
  2132. default:
  2133. num = get_arg(int);
  2134. }
  2135. str = number(str, end, num, spec);
  2136. } /* default: */
  2137. } /* switch(spec.type) */
  2138. } /* while(*fmt) */
  2139. out:
  2140. if (size > 0) {
  2141. if (str < end)
  2142. *str = '\0';
  2143. else
  2144. end[-1] = '\0';
  2145. }
  2146. #undef get_arg
  2147. /* the trailing null byte doesn't count towards the total */
  2148. return str - buf;
  2149. }
  2150. EXPORT_SYMBOL_GPL(bstr_printf);
  2151. /**
  2152. * bprintf - Parse a format string and place args' binary value in a buffer
  2153. * @bin_buf: The buffer to place args' binary value
  2154. * @size: The size of the buffer(by words(32bits), not characters)
  2155. * @fmt: The format string to use
  2156. * @...: Arguments for the format string
  2157. *
  2158. * The function returns the number of words(u32) written
  2159. * into @bin_buf.
  2160. */
  2161. int bprintf(u32 *bin_buf, size_t size, const char *fmt, ...)
  2162. {
  2163. va_list args;
  2164. int ret;
  2165. va_start(args, fmt);
  2166. ret = vbin_printf(bin_buf, size, fmt, args);
  2167. va_end(args);
  2168. return ret;
  2169. }
  2170. EXPORT_SYMBOL_GPL(bprintf);
  2171. #endif /* CONFIG_BINARY_PRINTF */
  2172. /**
  2173. * vsscanf - Unformat a buffer into a list of arguments
  2174. * @buf: input buffer
  2175. * @fmt: format of buffer
  2176. * @args: arguments
  2177. */
  2178. int vsscanf(const char *buf, const char *fmt, va_list args)
  2179. {
  2180. const char *str = buf;
  2181. char *next;
  2182. char digit;
  2183. int num = 0;
  2184. u8 qualifier;
  2185. unsigned int base;
  2186. union {
  2187. long long s;
  2188. unsigned long long u;
  2189. } val;
  2190. s16 field_width;
  2191. bool is_sign;
  2192. while (*fmt) {
  2193. /* skip any white space in format */
  2194. /* white space in format matchs any amount of
  2195. * white space, including none, in the input.
  2196. */
  2197. if (isspace(*fmt)) {
  2198. fmt = skip_spaces(++fmt);
  2199. str = skip_spaces(str);
  2200. }
  2201. /* anything that is not a conversion must match exactly */
  2202. if (*fmt != '%' && *fmt) {
  2203. if (*fmt++ != *str++)
  2204. break;
  2205. continue;
  2206. }
  2207. if (!*fmt)
  2208. break;
  2209. ++fmt;
  2210. /* skip this conversion.
  2211. * advance both strings to next white space
  2212. */
  2213. if (*fmt == '*') {
  2214. if (!*str)
  2215. break;
  2216. while (!isspace(*fmt) && *fmt != '%' && *fmt)
  2217. fmt++;
  2218. while (!isspace(*str) && *str)
  2219. str++;
  2220. continue;
  2221. }
  2222. /* get field width */
  2223. field_width = -1;
  2224. if (isdigit(*fmt)) {
  2225. field_width = skip_atoi(&fmt);
  2226. if (field_width <= 0)
  2227. break;
  2228. }
  2229. /* get conversion qualifier */
  2230. qualifier = -1;
  2231. if (*fmt == 'h' || _tolower(*fmt) == 'l' ||
  2232. _tolower(*fmt) == 'z') {
  2233. qualifier = *fmt++;
  2234. if (unlikely(qualifier == *fmt)) {
  2235. if (qualifier == 'h') {
  2236. qualifier = 'H';
  2237. fmt++;
  2238. } else if (qualifier == 'l') {
  2239. qualifier = 'L';
  2240. fmt++;
  2241. }
  2242. }
  2243. }
  2244. if (!*fmt)
  2245. break;
  2246. if (*fmt == 'n') {
  2247. /* return number of characters read so far */
  2248. *va_arg(args, int *) = str - buf;
  2249. ++fmt;
  2250. continue;
  2251. }
  2252. if (!*str)
  2253. break;
  2254. base = 10;
  2255. is_sign = false;
  2256. switch (*fmt++) {
  2257. case 'c':
  2258. {
  2259. char *s = (char *)va_arg(args, char*);
  2260. if (field_width == -1)
  2261. field_width = 1;
  2262. do {
  2263. *s++ = *str++;
  2264. } while (--field_width > 0 && *str);
  2265. num++;
  2266. }
  2267. continue;
  2268. case 's':
  2269. {
  2270. char *s = (char *)va_arg(args, char *);
  2271. if (field_width == -1)
  2272. field_width = SHRT_MAX;
  2273. /* first, skip leading white space in buffer */
  2274. str = skip_spaces(str);
  2275. /* now copy until next white space */
  2276. while (*str && !isspace(*str) && field_width--)
  2277. *s++ = *str++;
  2278. *s = '\0';
  2279. num++;
  2280. }
  2281. continue;
  2282. case 'o':
  2283. base = 8;
  2284. break;
  2285. case 'x':
  2286. case 'X':
  2287. base = 16;
  2288. break;
  2289. case 'i':
  2290. base = 0;
  2291. case 'd':
  2292. is_sign = true;
  2293. case 'u':
  2294. break;
  2295. case '%':
  2296. /* looking for '%' in str */
  2297. if (*str++ != '%')
  2298. return num;
  2299. continue;
  2300. default:
  2301. /* invalid format; stop here */
  2302. return num;
  2303. }
  2304. /* have some sort of integer conversion.
  2305. * first, skip white space in buffer.
  2306. */
  2307. str = skip_spaces(str);
  2308. digit = *str;
  2309. if (is_sign && digit == '-')
  2310. digit = *(str + 1);
  2311. if (!digit
  2312. || (base == 16 && !isxdigit(digit))
  2313. || (base == 10 && !isdigit(digit))
  2314. || (base == 8 && (!isdigit(digit) || digit > '7'))
  2315. || (base == 0 && !isdigit(digit)))
  2316. break;
  2317. if (is_sign)
  2318. val.s = qualifier != 'L' ?
  2319. simple_strtol(str, &next, base) :
  2320. simple_strtoll(str, &next, base);
  2321. else
  2322. val.u = qualifier != 'L' ?
  2323. simple_strtoul(str, &next, base) :
  2324. simple_strtoull(str, &next, base);
  2325. if (field_width > 0 && next - str > field_width) {
  2326. if (base == 0)
  2327. _parse_integer_fixup_radix(str, &base);
  2328. while (next - str > field_width) {
  2329. if (is_sign)
  2330. val.s = div_s64(val.s, base);
  2331. else
  2332. val.u = div_u64(val.u, base);
  2333. --next;
  2334. }
  2335. }
  2336. switch (qualifier) {
  2337. case 'H': /* that's 'hh' in format */
  2338. if (is_sign)
  2339. *va_arg(args, signed char *) = val.s;
  2340. else
  2341. *va_arg(args, unsigned char *) = val.u;
  2342. break;
  2343. case 'h':
  2344. if (is_sign)
  2345. *va_arg(args, short *) = val.s;
  2346. else
  2347. *va_arg(args, unsigned short *) = val.u;
  2348. break;
  2349. case 'l':
  2350. if (is_sign)
  2351. *va_arg(args, long *) = val.s;
  2352. else
  2353. *va_arg(args, unsigned long *) = val.u;
  2354. break;
  2355. case 'L':
  2356. if (is_sign)
  2357. *va_arg(args, long long *) = val.s;
  2358. else
  2359. *va_arg(args, unsigned long long *) = val.u;
  2360. break;
  2361. case 'Z':
  2362. case 'z':
  2363. *va_arg(args, size_t *) = val.u;
  2364. break;
  2365. default:
  2366. if (is_sign)
  2367. *va_arg(args, int *) = val.s;
  2368. else
  2369. *va_arg(args, unsigned int *) = val.u;
  2370. break;
  2371. }
  2372. num++;
  2373. if (!next)
  2374. break;
  2375. str = next;
  2376. }
  2377. return num;
  2378. }
  2379. EXPORT_SYMBOL(vsscanf);
  2380. /**
  2381. * sscanf - Unformat a buffer into a list of arguments
  2382. * @buf: input buffer
  2383. * @fmt: formatting of buffer
  2384. * @...: resulting arguments
  2385. */
  2386. int sscanf(const char *buf, const char *fmt, ...)
  2387. {
  2388. va_list args;
  2389. int i;
  2390. va_start(args, fmt);
  2391. i = vsscanf(buf, fmt, args);
  2392. va_end(args);
  2393. return i;
  2394. }
  2395. EXPORT_SYMBOL(sscanf);