vsprintf.c 67 KB

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