vasnprintf.c 223 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742
  1. /* vsprintf with automatic memory allocation.
  2. Copyright (C) 1999, 2002-2022 Free Software Foundation, Inc.
  3. This file is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as
  5. published by the Free Software Foundation; either version 2.1 of the
  6. License, or (at your option) any later version.
  7. This file is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. /* This file can be parametrized with the following macros:
  14. VASNPRINTF The name of the function being defined.
  15. FCHAR_T The element type of the format string.
  16. DCHAR_T The element type of the destination (result) string.
  17. FCHAR_T_ONLY_ASCII Set to 1 to enable verification that all characters
  18. in the format string are ASCII. MUST be set if
  19. FCHAR_T and DCHAR_T are not the same type.
  20. DIRECTIVE Structure denoting a format directive.
  21. Depends on FCHAR_T.
  22. DIRECTIVES Structure denoting the set of format directives of a
  23. format string. Depends on FCHAR_T.
  24. PRINTF_PARSE Function that parses a format string.
  25. Depends on FCHAR_T.
  26. DCHAR_CPY memcpy like function for DCHAR_T[] arrays.
  27. DCHAR_SET memset like function for DCHAR_T[] arrays.
  28. DCHAR_MBSNLEN mbsnlen like function for DCHAR_T[] arrays.
  29. SNPRINTF The system's snprintf (or similar) function.
  30. This may be either snprintf or swprintf.
  31. TCHAR_T The element type of the argument and result string
  32. of the said SNPRINTF function. This may be either
  33. char or wchar_t. The code exploits that
  34. sizeof (TCHAR_T) | sizeof (DCHAR_T) and
  35. alignof (TCHAR_T) <= alignof (DCHAR_T).
  36. DCHAR_IS_TCHAR Set to 1 if DCHAR_T and TCHAR_T are the same type.
  37. DCHAR_CONV_FROM_ENCODING A function to convert from char[] to DCHAR[].
  38. DCHAR_IS_UINT8_T Set to 1 if DCHAR_T is uint8_t.
  39. DCHAR_IS_UINT16_T Set to 1 if DCHAR_T is uint16_t.
  40. DCHAR_IS_UINT32_T Set to 1 if DCHAR_T is uint32_t.
  41. ENABLE_UNISTDIO Set to 1 to enable the unistdio extensions.
  42. ENABLE_WCHAR_FALLBACK Set to 1 to avoid EILSEQ during conversion of wide
  43. characters (wchar_t) and wide character strings
  44. (wchar_t[]) to multibyte sequences. The fallback is the
  45. hexadecimal escape syntax (\unnnn or \Unnnnnnnn) or,
  46. if wchar_t is not Unicode encoded, \wnnnn or \Wnnnnnnnn.
  47. */
  48. /* Tell glibc's <stdio.h> to provide a prototype for snprintf().
  49. This must come before <config.h> because <config.h> may include
  50. <features.h>, and once <features.h> has been included, it's too late. */
  51. #ifndef _GNU_SOURCE
  52. # define _GNU_SOURCE 1
  53. #endif
  54. #ifndef VASNPRINTF
  55. # include <config.h>
  56. #endif
  57. /* As of GCC 11.2.1, gcc -Wanalyzer-too-complex reports that main's
  58. use of CHECK macros expands to code that is too complicated for gcc
  59. -fanalyzer. Suppress the resulting bogus warnings. */
  60. #if 10 <= __GNUC__
  61. # pragma GCC diagnostic ignored "-Wanalyzer-null-argument"
  62. #endif
  63. #include <alloca.h>
  64. /* Specification. */
  65. #ifndef VASNPRINTF
  66. # if WIDE_CHAR_VERSION
  67. # include "vasnwprintf.h"
  68. # else
  69. # include "vasnprintf.h"
  70. # endif
  71. #endif
  72. #include <locale.h> /* localeconv() */
  73. #include <stdio.h> /* snprintf(), sprintf() */
  74. #include <stdlib.h> /* abort(), malloc(), realloc(), free() */
  75. #include <string.h> /* memcpy(), strlen() */
  76. #include <errno.h> /* errno */
  77. #include <limits.h> /* CHAR_BIT */
  78. #include <float.h> /* DBL_MAX_EXP, LDBL_MAX_EXP */
  79. #if HAVE_NL_LANGINFO
  80. # include <langinfo.h>
  81. #endif
  82. #ifndef VASNPRINTF
  83. # if WIDE_CHAR_VERSION
  84. # include "wprintf-parse.h"
  85. # else
  86. # include "printf-parse.h"
  87. # endif
  88. #endif
  89. /* Checked size_t computations. */
  90. #include "xsize.h"
  91. #include "attribute.h"
  92. #include "verify.h"
  93. #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
  94. # include <math.h>
  95. # include "float+.h"
  96. #endif
  97. #if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
  98. # include <math.h>
  99. # include "isnand-nolibm.h"
  100. #endif
  101. #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE) && !defined IN_LIBINTL
  102. # include <math.h>
  103. # include "isnanl-nolibm.h"
  104. # include "fpucw.h"
  105. #endif
  106. #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
  107. # include <math.h>
  108. # include "isnand-nolibm.h"
  109. # include "printf-frexp.h"
  110. #endif
  111. #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
  112. # include <math.h>
  113. # include "isnanl-nolibm.h"
  114. # include "printf-frexpl.h"
  115. # include "fpucw.h"
  116. #endif
  117. /* Default parameters. */
  118. #ifndef VASNPRINTF
  119. # if WIDE_CHAR_VERSION
  120. # define VASNPRINTF vasnwprintf
  121. # define FCHAR_T wchar_t
  122. # define DCHAR_T wchar_t
  123. # define TCHAR_T wchar_t
  124. # define DCHAR_IS_TCHAR 1
  125. # define DIRECTIVE wchar_t_directive
  126. # define DIRECTIVES wchar_t_directives
  127. # define PRINTF_PARSE wprintf_parse
  128. # define DCHAR_CPY wmemcpy
  129. # define DCHAR_SET wmemset
  130. # else
  131. # define VASNPRINTF vasnprintf
  132. # define FCHAR_T char
  133. # define DCHAR_T char
  134. # define TCHAR_T char
  135. # define DCHAR_IS_TCHAR 1
  136. # define DIRECTIVE char_directive
  137. # define DIRECTIVES char_directives
  138. # define PRINTF_PARSE printf_parse
  139. # define DCHAR_CPY memcpy
  140. # define DCHAR_SET memset
  141. # endif
  142. #endif
  143. #if WIDE_CHAR_VERSION
  144. /* TCHAR_T is wchar_t. */
  145. # define USE_SNPRINTF 1
  146. # if HAVE_DECL__SNWPRINTF
  147. /* On Windows, the function swprintf() has a different signature than
  148. on Unix; we use the function _snwprintf() or - on mingw - snwprintf()
  149. instead. The mingw function snwprintf() has fewer bugs than the
  150. MSVCRT function _snwprintf(), so prefer that. */
  151. # if defined __MINGW32__
  152. # define SNPRINTF snwprintf
  153. # else
  154. # define SNPRINTF _snwprintf
  155. # define USE_MSVC__SNPRINTF 1
  156. # endif
  157. # else
  158. /* Unix. */
  159. # define SNPRINTF swprintf
  160. # endif
  161. #else
  162. /* TCHAR_T is char. */
  163. /* Use snprintf if it exists under the name 'snprintf' or '_snprintf'.
  164. But don't use it on BeOS, since BeOS snprintf produces no output if the
  165. size argument is >= 0x3000000.
  166. Also don't use it on Linux libc5, since there snprintf with size = 1
  167. writes any output without bounds, like sprintf. */
  168. # if (HAVE_DECL__SNPRINTF || HAVE_SNPRINTF) && !defined __BEOS__ && !(__GNU_LIBRARY__ == 1)
  169. # define USE_SNPRINTF 1
  170. # else
  171. # define USE_SNPRINTF 0
  172. # endif
  173. # if HAVE_DECL__SNPRINTF
  174. /* Windows. The mingw function snprintf() has fewer bugs than the MSVCRT
  175. function _snprintf(), so prefer that. */
  176. # if defined __MINGW32__
  177. # define SNPRINTF snprintf
  178. /* Here we need to call the native snprintf, not rpl_snprintf. */
  179. # undef snprintf
  180. # else
  181. /* MSVC versions < 14 did not have snprintf, only _snprintf. */
  182. # define SNPRINTF _snprintf
  183. # define USE_MSVC__SNPRINTF 1
  184. # endif
  185. # else
  186. /* Unix. */
  187. # define SNPRINTF snprintf
  188. /* Here we need to call the native snprintf, not rpl_snprintf. */
  189. # undef snprintf
  190. # endif
  191. #endif
  192. /* Here we need to call the native sprintf, not rpl_sprintf. */
  193. #undef sprintf
  194. /* GCC >= 4.0 with -Wall emits unjustified "... may be used uninitialized"
  195. warnings in this file. Use -Dlint to suppress them. */
  196. #if defined GCC_LINT || defined lint
  197. # define IF_LINT(Code) Code
  198. #else
  199. # define IF_LINT(Code) /* empty */
  200. #endif
  201. /* Avoid some warnings from "gcc -Wshadow".
  202. This file doesn't use the exp() and remainder() functions. */
  203. #undef exp
  204. #define exp expo
  205. #undef remainder
  206. #define remainder rem
  207. #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF) && !WIDE_CHAR_VERSION
  208. # if (HAVE_STRNLEN && !defined _AIX)
  209. # define local_strnlen strnlen
  210. # else
  211. # ifndef local_strnlen_defined
  212. # define local_strnlen_defined 1
  213. static size_t
  214. local_strnlen (const char *string, size_t maxlen)
  215. {
  216. const char *end = memchr (string, '\0', maxlen);
  217. return end ? (size_t) (end - string) : maxlen;
  218. }
  219. # endif
  220. # endif
  221. #endif
  222. #if (((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF) && WIDE_CHAR_VERSION) || ((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL)) && !WIDE_CHAR_VERSION && DCHAR_IS_TCHAR)) && HAVE_WCHAR_T
  223. # if HAVE_WCSLEN
  224. # define local_wcslen wcslen
  225. # else
  226. /* Solaris 2.5.1 has wcslen() in a separate library libw.so. To avoid
  227. a dependency towards this library, here is a local substitute.
  228. Define this substitute only once, even if this file is included
  229. twice in the same compilation unit. */
  230. # ifndef local_wcslen_defined
  231. # define local_wcslen_defined 1
  232. static size_t
  233. local_wcslen (const wchar_t *s)
  234. {
  235. const wchar_t *ptr;
  236. for (ptr = s; *ptr != (wchar_t) 0; ptr++)
  237. ;
  238. return ptr - s;
  239. }
  240. # endif
  241. # endif
  242. #endif
  243. #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF) && HAVE_WCHAR_T && WIDE_CHAR_VERSION
  244. # if HAVE_WCSNLEN
  245. # define local_wcsnlen wcsnlen
  246. # else
  247. # ifndef local_wcsnlen_defined
  248. # define local_wcsnlen_defined 1
  249. static size_t
  250. local_wcsnlen (const wchar_t *s, size_t maxlen)
  251. {
  252. const wchar_t *ptr;
  253. for (ptr = s; maxlen > 0 && *ptr != (wchar_t) 0; ptr++, maxlen--)
  254. ;
  255. return ptr - s;
  256. }
  257. # endif
  258. # endif
  259. #endif
  260. #if (((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL) || ENABLE_WCHAR_FALLBACK) && HAVE_WCHAR_T) || (ENABLE_WCHAR_FALLBACK && HAVE_WINT_T)) && !WIDE_CHAR_VERSION
  261. # if ENABLE_WCHAR_FALLBACK
  262. static size_t
  263. wctomb_fallback (char *s, wchar_t wc)
  264. {
  265. static char hex[16] = "0123456789ABCDEF";
  266. s[0] = '\\';
  267. if (sizeof (wchar_t) > 2 && wc > 0xffff)
  268. {
  269. # if __STDC_ISO_10646__ || (__GLIBC__ >= 2) || (defined _WIN32 || defined __CYGWIN__)
  270. s[1] = 'U';
  271. # else
  272. s[1] = 'W';
  273. # endif
  274. s[2] = hex[(wc & 0xf0000000U) >> 28];
  275. s[3] = hex[(wc & 0xf000000U) >> 24];
  276. s[4] = hex[(wc & 0xf00000U) >> 20];
  277. s[5] = hex[(wc & 0xf0000U) >> 16];
  278. s[6] = hex[(wc & 0xf000U) >> 12];
  279. s[7] = hex[(wc & 0xf00U) >> 8];
  280. s[8] = hex[(wc & 0xf0U) >> 4];
  281. s[9] = hex[wc & 0xfU];
  282. return 10;
  283. }
  284. else
  285. {
  286. # if __STDC_ISO_10646__ || (__GLIBC__ >= 2) || (defined _WIN32 || defined __CYGWIN__)
  287. s[1] = 'u';
  288. # else
  289. s[1] = 'w';
  290. # endif
  291. s[2] = hex[(wc & 0xf000U) >> 12];
  292. s[3] = hex[(wc & 0xf00U) >> 8];
  293. s[4] = hex[(wc & 0xf0U) >> 4];
  294. s[5] = hex[wc & 0xfU];
  295. return 6;
  296. }
  297. }
  298. # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
  299. static size_t
  300. local_wcrtomb (char *s, wchar_t wc, mbstate_t *ps)
  301. {
  302. size_t count = wcrtomb (s, wc, ps);
  303. if (count == (size_t)(-1))
  304. count = wctomb_fallback (s, wc);
  305. return count;
  306. }
  307. # else
  308. static int
  309. local_wctomb (char *s, wchar_t wc)
  310. {
  311. int count = wctomb (s, wc);
  312. if (count < 0)
  313. count = wctomb_fallback (s, wc);
  314. return count;
  315. }
  316. # define local_wcrtomb(S, WC, PS) local_wctomb ((S), (WC))
  317. # endif
  318. # else
  319. # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
  320. # define local_wcrtomb(S, WC, PS) wcrtomb ((S), (WC), (PS))
  321. # else
  322. # define local_wcrtomb(S, WC, PS) wctomb ((S), (WC))
  323. # endif
  324. # endif
  325. #endif
  326. #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && !defined IN_LIBINTL
  327. /* Determine the decimal-point character according to the current locale. */
  328. # ifndef decimal_point_char_defined
  329. # define decimal_point_char_defined 1
  330. static char
  331. decimal_point_char (void)
  332. {
  333. const char *point;
  334. /* Determine it in a multithread-safe way. We know nl_langinfo is
  335. multithread-safe on glibc systems and Mac OS X systems, but is not required
  336. to be multithread-safe by POSIX. sprintf(), however, is multithread-safe.
  337. localeconv() is rarely multithread-safe. */
  338. # if HAVE_NL_LANGINFO && (__GLIBC__ || defined __UCLIBC__ || (defined __APPLE__ && defined __MACH__))
  339. point = nl_langinfo (RADIXCHAR);
  340. # elif 1
  341. char pointbuf[5];
  342. sprintf (pointbuf, "%#.0f", 1.0);
  343. point = &pointbuf[1];
  344. # else
  345. point = localeconv () -> decimal_point;
  346. # endif
  347. /* The decimal point is always a single byte: either '.' or ','. */
  348. return (point[0] != '\0' ? point[0] : '.');
  349. }
  350. # endif
  351. #endif
  352. #if NEED_PRINTF_INFINITE_DOUBLE && !NEED_PRINTF_DOUBLE && !defined IN_LIBINTL
  353. /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
  354. static int
  355. is_infinite_or_zero (double x)
  356. {
  357. return isnand (x) || x + x == x;
  358. }
  359. #endif
  360. #if NEED_PRINTF_INFINITE_LONG_DOUBLE && !NEED_PRINTF_LONG_DOUBLE && !defined IN_LIBINTL
  361. /* Equivalent to !isfinite(x) || x == 0, but does not require libm. */
  362. static int
  363. is_infinite_or_zerol (long double x)
  364. {
  365. return isnanl (x) || x + x == x;
  366. }
  367. #endif
  368. #if (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
  369. /* Converting 'long double' to decimal without rare rounding bugs requires
  370. real bignums. We use the naming conventions of GNU gmp, but vastly simpler
  371. (and slower) algorithms. */
  372. typedef unsigned int mp_limb_t;
  373. # define GMP_LIMB_BITS 32
  374. verify (sizeof (mp_limb_t) * CHAR_BIT == GMP_LIMB_BITS);
  375. typedef unsigned long long mp_twolimb_t;
  376. # define GMP_TWOLIMB_BITS 64
  377. verify (sizeof (mp_twolimb_t) * CHAR_BIT == GMP_TWOLIMB_BITS);
  378. /* Representation of a bignum >= 0. */
  379. typedef struct
  380. {
  381. size_t nlimbs;
  382. mp_limb_t *limbs; /* Bits in little-endian order, allocated with malloc(). */
  383. } mpn_t;
  384. /* Compute the product of two bignums >= 0.
  385. Return the allocated memory in case of success, NULL in case of memory
  386. allocation failure. */
  387. static void *
  388. multiply (mpn_t src1, mpn_t src2, mpn_t *dest)
  389. {
  390. const mp_limb_t *p1;
  391. const mp_limb_t *p2;
  392. size_t len1;
  393. size_t len2;
  394. if (src1.nlimbs <= src2.nlimbs)
  395. {
  396. len1 = src1.nlimbs;
  397. p1 = src1.limbs;
  398. len2 = src2.nlimbs;
  399. p2 = src2.limbs;
  400. }
  401. else
  402. {
  403. len1 = src2.nlimbs;
  404. p1 = src2.limbs;
  405. len2 = src1.nlimbs;
  406. p2 = src1.limbs;
  407. }
  408. /* Now 0 <= len1 <= len2. */
  409. if (len1 == 0)
  410. {
  411. /* src1 or src2 is zero. */
  412. dest->nlimbs = 0;
  413. dest->limbs = (mp_limb_t *) malloc (1);
  414. }
  415. else
  416. {
  417. /* Here 1 <= len1 <= len2. */
  418. size_t dlen;
  419. mp_limb_t *dp;
  420. size_t k, i, j;
  421. dlen = len1 + len2;
  422. dp = (mp_limb_t *) malloc (dlen * sizeof (mp_limb_t));
  423. if (dp == NULL)
  424. return NULL;
  425. for (k = len2; k > 0; )
  426. dp[--k] = 0;
  427. for (i = 0; i < len1; i++)
  428. {
  429. mp_limb_t digit1 = p1[i];
  430. mp_twolimb_t carry = 0;
  431. for (j = 0; j < len2; j++)
  432. {
  433. mp_limb_t digit2 = p2[j];
  434. carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2;
  435. carry += dp[i + j];
  436. dp[i + j] = (mp_limb_t) carry;
  437. carry = carry >> GMP_LIMB_BITS;
  438. }
  439. dp[i + len2] = (mp_limb_t) carry;
  440. }
  441. /* Normalise. */
  442. while (dlen > 0 && dp[dlen - 1] == 0)
  443. dlen--;
  444. dest->nlimbs = dlen;
  445. dest->limbs = dp;
  446. }
  447. return dest->limbs;
  448. }
  449. /* Compute the quotient of a bignum a >= 0 and a bignum b > 0.
  450. a is written as a = q * b + r with 0 <= r < b. q is the quotient, r
  451. the remainder.
  452. Finally, round-to-even is performed: If r > b/2 or if r = b/2 and q is odd,
  453. q is incremented.
  454. Return the allocated memory in case of success, NULL in case of memory
  455. allocation failure. */
  456. static void *
  457. divide (mpn_t a, mpn_t b, mpn_t *q)
  458. {
  459. /* Algorithm:
  460. First normalise a and b: a=[a[m-1],...,a[0]], b=[b[n-1],...,b[0]]
  461. with m>=0 and n>0 (in base beta = 2^GMP_LIMB_BITS).
  462. If m<n, then q:=0 and r:=a.
  463. If m>=n=1, perform a single-precision division:
  464. r:=0, j:=m,
  465. while j>0 do
  466. {Here (q[m-1]*beta^(m-1)+...+q[j]*beta^j) * b[0] + r*beta^j =
  467. = a[m-1]*beta^(m-1)+...+a[j]*beta^j und 0<=r<b[0]<beta}
  468. j:=j-1, r:=r*beta+a[j], q[j]:=floor(r/b[0]), r:=r-b[0]*q[j].
  469. Normalise [q[m-1],...,q[0]], yields q.
  470. If m>=n>1, perform a multiple-precision division:
  471. We have a/b < beta^(m-n+1).
  472. s:=intDsize-1-(highest bit in b[n-1]), 0<=s<intDsize.
  473. Shift a and b left by s bits, copying them. r:=a.
  474. r=[r[m],...,r[0]], b=[b[n-1],...,b[0]] with b[n-1]>=beta/2.
  475. For j=m-n,...,0: {Here 0 <= r < b*beta^(j+1).}
  476. Compute q* :
  477. q* := floor((r[j+n]*beta+r[j+n-1])/b[n-1]).
  478. In case of overflow (q* >= beta) set q* := beta-1.
  479. Compute c2 := ((r[j+n]*beta+r[j+n-1]) - q* * b[n-1])*beta + r[j+n-2]
  480. and c3 := b[n-2] * q*.
  481. {We have 0 <= c2 < 2*beta^2, even 0 <= c2 < beta^2 if no overflow
  482. occurred. Furthermore 0 <= c3 < beta^2.
  483. If there was overflow and
  484. r[j+n]*beta+r[j+n-1] - q* * b[n-1] >= beta, i.e. c2 >= beta^2,
  485. the next test can be skipped.}
  486. While c3 > c2, {Here 0 <= c2 < c3 < beta^2}
  487. Put q* := q* - 1, c2 := c2 + b[n-1]*beta, c3 := c3 - b[n-2].
  488. If q* > 0:
  489. Put r := r - b * q* * beta^j. In detail:
  490. [r[n+j],...,r[j]] := [r[n+j],...,r[j]] - q* * [b[n-1],...,b[0]].
  491. hence: u:=0, for i:=0 to n-1 do
  492. u := u + q* * b[i],
  493. r[j+i]:=r[j+i]-(u mod beta) (+ beta, if carry),
  494. u:=u div beta (+ 1, if carry in subtraction)
  495. r[n+j]:=r[n+j]-u.
  496. {Since always u = (q* * [b[i-1],...,b[0]] div beta^i) + 1
  497. < q* + 1 <= beta,
  498. the carry u does not overflow.}
  499. If a negative carry occurs, put q* := q* - 1
  500. and [r[n+j],...,r[j]] := [r[n+j],...,r[j]] + [0,b[n-1],...,b[0]].
  501. Set q[j] := q*.
  502. Normalise [q[m-n],..,q[0]]; this yields the quotient q.
  503. Shift [r[n-1],...,r[0]] right by s bits and normalise; this yields the
  504. rest r.
  505. The room for q[j] can be allocated at the memory location of r[n+j].
  506. Finally, round-to-even:
  507. Shift r left by 1 bit.
  508. If r > b or if r = b and q[0] is odd, q := q+1.
  509. */
  510. const mp_limb_t *a_ptr = a.limbs;
  511. size_t a_len = a.nlimbs;
  512. const mp_limb_t *b_ptr = b.limbs;
  513. size_t b_len = b.nlimbs;
  514. mp_limb_t *roomptr;
  515. mp_limb_t *tmp_roomptr = NULL;
  516. mp_limb_t *q_ptr;
  517. size_t q_len;
  518. mp_limb_t *r_ptr;
  519. size_t r_len;
  520. /* Allocate room for a_len+2 digits.
  521. (Need a_len+1 digits for the real division and 1 more digit for the
  522. final rounding of q.) */
  523. roomptr = (mp_limb_t *) malloc ((a_len + 2) * sizeof (mp_limb_t));
  524. if (roomptr == NULL)
  525. return NULL;
  526. /* Normalise a. */
  527. while (a_len > 0 && a_ptr[a_len - 1] == 0)
  528. a_len--;
  529. /* Normalise b. */
  530. for (;;)
  531. {
  532. if (b_len == 0)
  533. /* Division by zero. */
  534. abort ();
  535. if (b_ptr[b_len - 1] == 0)
  536. b_len--;
  537. else
  538. break;
  539. }
  540. /* Here m = a_len >= 0 and n = b_len > 0. */
  541. if (a_len < b_len)
  542. {
  543. /* m<n: trivial case. q=0, r := copy of a. */
  544. r_ptr = roomptr;
  545. r_len = a_len;
  546. memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t));
  547. q_ptr = roomptr + a_len;
  548. q_len = 0;
  549. }
  550. else if (b_len == 1)
  551. {
  552. /* n=1: single precision division.
  553. beta^(m-1) <= a < beta^m ==> beta^(m-2) <= a/b < beta^m */
  554. r_ptr = roomptr;
  555. q_ptr = roomptr + 1;
  556. {
  557. mp_limb_t den = b_ptr[0];
  558. mp_limb_t remainder = 0;
  559. const mp_limb_t *sourceptr = a_ptr + a_len;
  560. mp_limb_t *destptr = q_ptr + a_len;
  561. size_t count;
  562. for (count = a_len; count > 0; count--)
  563. {
  564. mp_twolimb_t num =
  565. ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--sourceptr;
  566. *--destptr = num / den;
  567. remainder = num % den;
  568. }
  569. /* Normalise and store r. */
  570. if (remainder > 0)
  571. {
  572. r_ptr[0] = remainder;
  573. r_len = 1;
  574. }
  575. else
  576. r_len = 0;
  577. /* Normalise q. */
  578. q_len = a_len;
  579. if (q_ptr[q_len - 1] == 0)
  580. q_len--;
  581. }
  582. }
  583. else
  584. {
  585. /* n>1: multiple precision division.
  586. beta^(m-1) <= a < beta^m, beta^(n-1) <= b < beta^n ==>
  587. beta^(m-n-1) <= a/b < beta^(m-n+1). */
  588. /* Determine s. */
  589. size_t s;
  590. {
  591. mp_limb_t msd = b_ptr[b_len - 1]; /* = b[n-1], > 0 */
  592. /* Determine s = GMP_LIMB_BITS - integer_length (msd).
  593. Code copied from gnulib's integer_length.c. */
  594. # if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) \
  595. || (__clang_major__ >= 4)
  596. s = __builtin_clz (msd);
  597. # else
  598. # if defined DBL_EXPBIT0_WORD && defined DBL_EXPBIT0_BIT
  599. if (GMP_LIMB_BITS <= DBL_MANT_BIT)
  600. {
  601. /* Use 'double' operations.
  602. Assumes an IEEE 754 'double' implementation. */
  603. # define DBL_EXP_MASK ((DBL_MAX_EXP - DBL_MIN_EXP) | 7)
  604. # define DBL_EXP_BIAS (DBL_EXP_MASK / 2 - 1)
  605. # define NWORDS \
  606. ((sizeof (double) + sizeof (unsigned int) - 1) / sizeof (unsigned int))
  607. union { double value; unsigned int word[NWORDS]; } m;
  608. /* Use a single integer to floating-point conversion. */
  609. m.value = msd;
  610. s = GMP_LIMB_BITS
  611. - (((m.word[DBL_EXPBIT0_WORD] >> DBL_EXPBIT0_BIT) & DBL_EXP_MASK)
  612. - DBL_EXP_BIAS);
  613. }
  614. else
  615. # undef NWORDS
  616. # endif
  617. {
  618. s = 31;
  619. if (msd >= 0x10000)
  620. {
  621. msd = msd >> 16;
  622. s -= 16;
  623. }
  624. if (msd >= 0x100)
  625. {
  626. msd = msd >> 8;
  627. s -= 8;
  628. }
  629. if (msd >= 0x10)
  630. {
  631. msd = msd >> 4;
  632. s -= 4;
  633. }
  634. if (msd >= 0x4)
  635. {
  636. msd = msd >> 2;
  637. s -= 2;
  638. }
  639. if (msd >= 0x2)
  640. {
  641. msd = msd >> 1;
  642. s -= 1;
  643. }
  644. }
  645. # endif
  646. }
  647. /* 0 <= s < GMP_LIMB_BITS.
  648. Copy b, shifting it left by s bits. */
  649. if (s > 0)
  650. {
  651. tmp_roomptr = (mp_limb_t *) malloc (b_len * sizeof (mp_limb_t));
  652. if (tmp_roomptr == NULL)
  653. {
  654. free (roomptr);
  655. return NULL;
  656. }
  657. {
  658. const mp_limb_t *sourceptr = b_ptr;
  659. mp_limb_t *destptr = tmp_roomptr;
  660. mp_twolimb_t accu = 0;
  661. size_t count;
  662. for (count = b_len; count > 0; count--)
  663. {
  664. accu += (mp_twolimb_t) *sourceptr++ << s;
  665. *destptr++ = (mp_limb_t) accu;
  666. accu = accu >> GMP_LIMB_BITS;
  667. }
  668. /* accu must be zero, since that was how s was determined. */
  669. if (accu != 0)
  670. abort ();
  671. }
  672. b_ptr = tmp_roomptr;
  673. }
  674. /* Copy a, shifting it left by s bits, yields r.
  675. Memory layout:
  676. At the beginning: r = roomptr[0..a_len],
  677. at the end: r = roomptr[0..b_len-1], q = roomptr[b_len..a_len] */
  678. r_ptr = roomptr;
  679. if (s == 0)
  680. {
  681. memcpy (r_ptr, a_ptr, a_len * sizeof (mp_limb_t));
  682. r_ptr[a_len] = 0;
  683. }
  684. else
  685. {
  686. const mp_limb_t *sourceptr = a_ptr;
  687. mp_limb_t *destptr = r_ptr;
  688. mp_twolimb_t accu = 0;
  689. size_t count;
  690. for (count = a_len; count > 0; count--)
  691. {
  692. accu += (mp_twolimb_t) *sourceptr++ << s;
  693. *destptr++ = (mp_limb_t) accu;
  694. accu = accu >> GMP_LIMB_BITS;
  695. }
  696. *destptr++ = (mp_limb_t) accu;
  697. }
  698. q_ptr = roomptr + b_len;
  699. q_len = a_len - b_len + 1; /* q will have m-n+1 limbs */
  700. {
  701. size_t j = a_len - b_len; /* m-n */
  702. mp_limb_t b_msd = b_ptr[b_len - 1]; /* b[n-1] */
  703. mp_limb_t b_2msd = b_ptr[b_len - 2]; /* b[n-2] */
  704. mp_twolimb_t b_msdd = /* b[n-1]*beta+b[n-2] */
  705. ((mp_twolimb_t) b_msd << GMP_LIMB_BITS) | b_2msd;
  706. /* Division loop, traversed m-n+1 times.
  707. j counts down, b is unchanged, beta/2 <= b[n-1] < beta. */
  708. for (;;)
  709. {
  710. mp_limb_t q_star;
  711. mp_limb_t c1;
  712. if (r_ptr[j + b_len] < b_msd) /* r[j+n] < b[n-1] ? */
  713. {
  714. /* Divide r[j+n]*beta+r[j+n-1] by b[n-1], no overflow. */
  715. mp_twolimb_t num =
  716. ((mp_twolimb_t) r_ptr[j + b_len] << GMP_LIMB_BITS)
  717. | r_ptr[j + b_len - 1];
  718. q_star = num / b_msd;
  719. c1 = num % b_msd;
  720. }
  721. else
  722. {
  723. /* Overflow, hence r[j+n]*beta+r[j+n-1] >= beta*b[n-1]. */
  724. q_star = (mp_limb_t)~(mp_limb_t)0; /* q* = beta-1 */
  725. /* Test whether r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] >= beta
  726. <==> r[j+n]*beta+r[j+n-1] + b[n-1] >= beta*b[n-1]+beta
  727. <==> b[n-1] < floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta)
  728. {<= beta !}.
  729. If yes, jump directly to the subtraction loop.
  730. (Otherwise, r[j+n]*beta+r[j+n-1] - (beta-1)*b[n-1] < beta
  731. <==> floor((r[j+n]*beta+r[j+n-1]+b[n-1])/beta) = b[n-1] ) */
  732. if (r_ptr[j + b_len] > b_msd
  733. || (c1 = r_ptr[j + b_len - 1] + b_msd) < b_msd)
  734. /* r[j+n] >= b[n-1]+1 or
  735. r[j+n] = b[n-1] and the addition r[j+n-1]+b[n-1] gives a
  736. carry. */
  737. goto subtract;
  738. }
  739. /* q_star = q*,
  740. c1 = (r[j+n]*beta+r[j+n-1]) - q* * b[n-1] (>=0, <beta). */
  741. {
  742. mp_twolimb_t c2 = /* c1*beta+r[j+n-2] */
  743. ((mp_twolimb_t) c1 << GMP_LIMB_BITS) | r_ptr[j + b_len - 2];
  744. mp_twolimb_t c3 = /* b[n-2] * q* */
  745. (mp_twolimb_t) b_2msd * (mp_twolimb_t) q_star;
  746. /* While c2 < c3, increase c2 and decrease c3.
  747. Consider c3-c2. While it is > 0, decrease it by
  748. b[n-1]*beta+b[n-2]. Because of b[n-1]*beta+b[n-2] >= beta^2/2
  749. this can happen only twice. */
  750. if (c3 > c2)
  751. {
  752. q_star = q_star - 1; /* q* := q* - 1 */
  753. if (c3 - c2 > b_msdd)
  754. q_star = q_star - 1; /* q* := q* - 1 */
  755. }
  756. }
  757. if (q_star > 0)
  758. subtract:
  759. {
  760. /* Subtract r := r - b * q* * beta^j. */
  761. mp_limb_t cr;
  762. {
  763. const mp_limb_t *sourceptr = b_ptr;
  764. mp_limb_t *destptr = r_ptr + j;
  765. mp_twolimb_t carry = 0;
  766. size_t count;
  767. for (count = b_len; count > 0; count--)
  768. {
  769. /* Here 0 <= carry <= q*. */
  770. carry =
  771. carry
  772. + (mp_twolimb_t) q_star * (mp_twolimb_t) *sourceptr++
  773. + (mp_limb_t) ~(*destptr);
  774. /* Here 0 <= carry <= beta*q* + beta-1. */
  775. *destptr++ = ~(mp_limb_t) carry;
  776. carry = carry >> GMP_LIMB_BITS; /* <= q* */
  777. }
  778. cr = (mp_limb_t) carry;
  779. }
  780. /* Subtract cr from r_ptr[j + b_len], then forget about
  781. r_ptr[j + b_len]. */
  782. if (cr > r_ptr[j + b_len])
  783. {
  784. /* Subtraction gave a carry. */
  785. q_star = q_star - 1; /* q* := q* - 1 */
  786. /* Add b back. */
  787. {
  788. const mp_limb_t *sourceptr = b_ptr;
  789. mp_limb_t *destptr = r_ptr + j;
  790. mp_limb_t carry = 0;
  791. size_t count;
  792. for (count = b_len; count > 0; count--)
  793. {
  794. mp_limb_t source1 = *sourceptr++;
  795. mp_limb_t source2 = *destptr;
  796. *destptr++ = source1 + source2 + carry;
  797. carry =
  798. (carry
  799. ? source1 >= (mp_limb_t) ~source2
  800. : source1 > (mp_limb_t) ~source2);
  801. }
  802. }
  803. /* Forget about the carry and about r[j+n]. */
  804. }
  805. }
  806. /* q* is determined. Store it as q[j]. */
  807. q_ptr[j] = q_star;
  808. if (j == 0)
  809. break;
  810. j--;
  811. }
  812. }
  813. r_len = b_len;
  814. /* Normalise q. */
  815. if (q_ptr[q_len - 1] == 0)
  816. q_len--;
  817. # if 0 /* Not needed here, since we need r only to compare it with b/2, and
  818. b is shifted left by s bits. */
  819. /* Shift r right by s bits. */
  820. if (s > 0)
  821. {
  822. mp_limb_t ptr = r_ptr + r_len;
  823. mp_twolimb_t accu = 0;
  824. size_t count;
  825. for (count = r_len; count > 0; count--)
  826. {
  827. accu = (mp_twolimb_t) (mp_limb_t) accu << GMP_LIMB_BITS;
  828. accu += (mp_twolimb_t) *--ptr << (GMP_LIMB_BITS - s);
  829. *ptr = (mp_limb_t) (accu >> GMP_LIMB_BITS);
  830. }
  831. }
  832. # endif
  833. /* Normalise r. */
  834. while (r_len > 0 && r_ptr[r_len - 1] == 0)
  835. r_len--;
  836. }
  837. /* Compare r << 1 with b. */
  838. if (r_len > b_len)
  839. goto increment_q;
  840. {
  841. size_t i;
  842. for (i = b_len;;)
  843. {
  844. mp_limb_t r_i =
  845. (i <= r_len && i > 0 ? r_ptr[i - 1] >> (GMP_LIMB_BITS - 1) : 0)
  846. | (i < r_len ? r_ptr[i] << 1 : 0);
  847. mp_limb_t b_i = (i < b_len ? b_ptr[i] : 0);
  848. if (r_i > b_i)
  849. goto increment_q;
  850. if (r_i < b_i)
  851. goto keep_q;
  852. if (i == 0)
  853. break;
  854. i--;
  855. }
  856. }
  857. if (q_len > 0 && ((q_ptr[0] & 1) != 0))
  858. /* q is odd. */
  859. increment_q:
  860. {
  861. size_t i;
  862. for (i = 0; i < q_len; i++)
  863. if (++(q_ptr[i]) != 0)
  864. goto keep_q;
  865. q_ptr[q_len++] = 1;
  866. }
  867. keep_q:
  868. free (tmp_roomptr);
  869. q->limbs = q_ptr;
  870. q->nlimbs = q_len;
  871. return roomptr;
  872. }
  873. /* Convert a bignum a >= 0, multiplied with 10^extra_zeroes, to decimal
  874. representation.
  875. Destroys the contents of a.
  876. Return the allocated memory - containing the decimal digits in low-to-high
  877. order, terminated with a NUL character - in case of success, NULL in case
  878. of memory allocation failure. */
  879. static char *
  880. convert_to_decimal (mpn_t a, size_t extra_zeroes)
  881. {
  882. mp_limb_t *a_ptr = a.limbs;
  883. size_t a_len = a.nlimbs;
  884. /* 0.03345 is slightly larger than log(2)/(9*log(10)). */
  885. size_t c_len = 9 * ((size_t)(a_len * (GMP_LIMB_BITS * 0.03345f)) + 1);
  886. /* We need extra_zeroes bytes for zeroes, followed by c_len bytes for the
  887. digits of a, followed by 1 byte for the terminating NUL. */
  888. char *c_ptr = (char *) malloc (xsum (xsum (extra_zeroes, c_len), 1));
  889. if (c_ptr != NULL)
  890. {
  891. char *d_ptr = c_ptr;
  892. for (; extra_zeroes > 0; extra_zeroes--)
  893. *d_ptr++ = '0';
  894. while (a_len > 0)
  895. {
  896. /* Divide a by 10^9, in-place. */
  897. mp_limb_t remainder = 0;
  898. mp_limb_t *ptr = a_ptr + a_len;
  899. size_t count;
  900. for (count = a_len; count > 0; count--)
  901. {
  902. mp_twolimb_t num =
  903. ((mp_twolimb_t) remainder << GMP_LIMB_BITS) | *--ptr;
  904. *ptr = num / 1000000000;
  905. remainder = num % 1000000000;
  906. }
  907. /* Store the remainder as 9 decimal digits. */
  908. for (count = 9; count > 0; count--)
  909. {
  910. *d_ptr++ = '0' + (remainder % 10);
  911. remainder = remainder / 10;
  912. }
  913. /* Normalize a. */
  914. if (a_ptr[a_len - 1] == 0)
  915. a_len--;
  916. }
  917. /* Remove leading zeroes. */
  918. while (d_ptr > c_ptr && d_ptr[-1] == '0')
  919. d_ptr--;
  920. /* But keep at least one zero. */
  921. if (d_ptr == c_ptr)
  922. *d_ptr++ = '0';
  923. /* Terminate the string. */
  924. *d_ptr = '\0';
  925. }
  926. return c_ptr;
  927. }
  928. # if NEED_PRINTF_LONG_DOUBLE
  929. /* Assuming x is finite and >= 0:
  930. write x as x = 2^e * m, where m is a bignum.
  931. Return the allocated memory in case of success, NULL in case of memory
  932. allocation failure. */
  933. static void *
  934. decode_long_double (long double x, int *ep, mpn_t *mp)
  935. {
  936. mpn_t m;
  937. int exp;
  938. long double y;
  939. size_t i;
  940. /* Allocate memory for result. */
  941. m.nlimbs = (LDBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
  942. m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
  943. if (m.limbs == NULL)
  944. return NULL;
  945. /* Split into exponential part and mantissa. */
  946. y = frexpl (x, &exp);
  947. if (!(y >= 0.0L && y < 1.0L))
  948. abort ();
  949. /* x = 2^exp * y = 2^(exp - LDBL_MANT_BIT) * (y * 2^LDBL_MANT_BIT), and the
  950. latter is an integer. */
  951. /* Convert the mantissa (y * 2^LDBL_MANT_BIT) to a sequence of limbs.
  952. I'm not sure whether it's safe to cast a 'long double' value between
  953. 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
  954. 'long double' values between 0 and 2^16 (to 'unsigned int' or 'int',
  955. doesn't matter). */
  956. # if (LDBL_MANT_BIT % GMP_LIMB_BITS) != 0
  957. # if (LDBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
  958. {
  959. mp_limb_t hi, lo;
  960. y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % (GMP_LIMB_BITS / 2));
  961. hi = (int) y;
  962. y -= hi;
  963. if (!(y >= 0.0L && y < 1.0L))
  964. abort ();
  965. y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
  966. lo = (int) y;
  967. y -= lo;
  968. if (!(y >= 0.0L && y < 1.0L))
  969. abort ();
  970. m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo;
  971. }
  972. # else
  973. {
  974. mp_limb_t d;
  975. y *= (mp_limb_t) 1 << (LDBL_MANT_BIT % GMP_LIMB_BITS);
  976. d = (int) y;
  977. y -= d;
  978. if (!(y >= 0.0L && y < 1.0L))
  979. abort ();
  980. m.limbs[LDBL_MANT_BIT / GMP_LIMB_BITS] = d;
  981. }
  982. # endif
  983. # endif
  984. for (i = LDBL_MANT_BIT / GMP_LIMB_BITS; i > 0; )
  985. {
  986. mp_limb_t hi, lo;
  987. y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
  988. hi = (int) y;
  989. y -= hi;
  990. if (!(y >= 0.0L && y < 1.0L))
  991. abort ();
  992. y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
  993. lo = (int) y;
  994. y -= lo;
  995. if (!(y >= 0.0L && y < 1.0L))
  996. abort ();
  997. m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo;
  998. }
  999. # if 0 /* On FreeBSD 6.1/x86, 'long double' numbers sometimes have excess
  1000. precision. */
  1001. if (!(y == 0.0L))
  1002. abort ();
  1003. # endif
  1004. /* Normalise. */
  1005. while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0)
  1006. m.nlimbs--;
  1007. *mp = m;
  1008. *ep = exp - LDBL_MANT_BIT;
  1009. return m.limbs;
  1010. }
  1011. # endif
  1012. # if NEED_PRINTF_DOUBLE
  1013. /* Assuming x is finite and >= 0:
  1014. write x as x = 2^e * m, where m is a bignum.
  1015. Return the allocated memory in case of success, NULL in case of memory
  1016. allocation failure. */
  1017. static void *
  1018. decode_double (double x, int *ep, mpn_t *mp)
  1019. {
  1020. mpn_t m;
  1021. int exp;
  1022. double y;
  1023. size_t i;
  1024. /* Allocate memory for result. */
  1025. m.nlimbs = (DBL_MANT_BIT + GMP_LIMB_BITS - 1) / GMP_LIMB_BITS;
  1026. m.limbs = (mp_limb_t *) malloc (m.nlimbs * sizeof (mp_limb_t));
  1027. if (m.limbs == NULL)
  1028. return NULL;
  1029. /* Split into exponential part and mantissa. */
  1030. y = frexp (x, &exp);
  1031. if (!(y >= 0.0 && y < 1.0))
  1032. abort ();
  1033. /* x = 2^exp * y = 2^(exp - DBL_MANT_BIT) * (y * 2^DBL_MANT_BIT), and the
  1034. latter is an integer. */
  1035. /* Convert the mantissa (y * 2^DBL_MANT_BIT) to a sequence of limbs.
  1036. I'm not sure whether it's safe to cast a 'double' value between
  1037. 2^31 and 2^32 to 'unsigned int', therefore play safe and cast only
  1038. 'double' values between 0 and 2^16 (to 'unsigned int' or 'int',
  1039. doesn't matter). */
  1040. # if (DBL_MANT_BIT % GMP_LIMB_BITS) != 0
  1041. # if (DBL_MANT_BIT % GMP_LIMB_BITS) > GMP_LIMB_BITS / 2
  1042. {
  1043. mp_limb_t hi, lo;
  1044. y *= (mp_limb_t) 1 << (DBL_MANT_BIT % (GMP_LIMB_BITS / 2));
  1045. hi = (int) y;
  1046. y -= hi;
  1047. if (!(y >= 0.0 && y < 1.0))
  1048. abort ();
  1049. y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
  1050. lo = (int) y;
  1051. y -= lo;
  1052. if (!(y >= 0.0 && y < 1.0))
  1053. abort ();
  1054. m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = (hi << (GMP_LIMB_BITS / 2)) | lo;
  1055. }
  1056. # else
  1057. {
  1058. mp_limb_t d;
  1059. y *= (mp_limb_t) 1 << (DBL_MANT_BIT % GMP_LIMB_BITS);
  1060. d = (int) y;
  1061. y -= d;
  1062. if (!(y >= 0.0 && y < 1.0))
  1063. abort ();
  1064. m.limbs[DBL_MANT_BIT / GMP_LIMB_BITS] = d;
  1065. }
  1066. # endif
  1067. # endif
  1068. for (i = DBL_MANT_BIT / GMP_LIMB_BITS; i > 0; )
  1069. {
  1070. mp_limb_t hi, lo;
  1071. y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
  1072. hi = (int) y;
  1073. y -= hi;
  1074. if (!(y >= 0.0 && y < 1.0))
  1075. abort ();
  1076. y *= (mp_limb_t) 1 << (GMP_LIMB_BITS / 2);
  1077. lo = (int) y;
  1078. y -= lo;
  1079. if (!(y >= 0.0 && y < 1.0))
  1080. abort ();
  1081. m.limbs[--i] = (hi << (GMP_LIMB_BITS / 2)) | lo;
  1082. }
  1083. if (!(y == 0.0))
  1084. abort ();
  1085. /* Normalise. */
  1086. while (m.nlimbs > 0 && m.limbs[m.nlimbs - 1] == 0)
  1087. m.nlimbs--;
  1088. *mp = m;
  1089. *ep = exp - DBL_MANT_BIT;
  1090. return m.limbs;
  1091. }
  1092. # endif
  1093. /* Assuming x = 2^e * m is finite and >= 0, and n is an integer:
  1094. Returns the decimal representation of round (x * 10^n).
  1095. Return the allocated memory - containing the decimal digits in low-to-high
  1096. order, terminated with a NUL character - in case of success, NULL in case
  1097. of memory allocation failure. */
  1098. static char *
  1099. scale10_round_decimal_decoded (int e, mpn_t m, void *memory, int n)
  1100. {
  1101. int s;
  1102. size_t extra_zeroes;
  1103. unsigned int abs_n;
  1104. unsigned int abs_s;
  1105. mp_limb_t *pow5_ptr;
  1106. size_t pow5_len;
  1107. unsigned int s_limbs;
  1108. unsigned int s_bits;
  1109. mpn_t pow5;
  1110. mpn_t z;
  1111. void *z_memory;
  1112. char *digits;
  1113. if (memory == NULL)
  1114. return NULL;
  1115. /* x = 2^e * m, hence
  1116. y = round (2^e * 10^n * m) = round (2^(e+n) * 5^n * m)
  1117. = round (2^s * 5^n * m). */
  1118. s = e + n;
  1119. extra_zeroes = 0;
  1120. /* Factor out a common power of 10 if possible. */
  1121. if (s > 0 && n > 0)
  1122. {
  1123. extra_zeroes = (s < n ? s : n);
  1124. s -= extra_zeroes;
  1125. n -= extra_zeroes;
  1126. }
  1127. /* Here y = round (2^s * 5^n * m) * 10^extra_zeroes.
  1128. Before converting to decimal, we need to compute
  1129. z = round (2^s * 5^n * m). */
  1130. /* Compute 5^|n|, possibly shifted by |s| bits if n and s have the same
  1131. sign. 2.322 is slightly larger than log(5)/log(2). */
  1132. abs_n = (n >= 0 ? n : -n);
  1133. abs_s = (s >= 0 ? s : -s);
  1134. pow5_ptr = (mp_limb_t *) malloc (((int)(abs_n * (2.322f / GMP_LIMB_BITS)) + 1
  1135. + abs_s / GMP_LIMB_BITS + 1)
  1136. * sizeof (mp_limb_t));
  1137. if (pow5_ptr == NULL)
  1138. {
  1139. free (memory);
  1140. return NULL;
  1141. }
  1142. /* Initialize with 1. */
  1143. pow5_ptr[0] = 1;
  1144. pow5_len = 1;
  1145. /* Multiply with 5^|n|. */
  1146. if (abs_n > 0)
  1147. {
  1148. static mp_limb_t const small_pow5[13 + 1] =
  1149. {
  1150. 1, 5, 25, 125, 625, 3125, 15625, 78125, 390625, 1953125, 9765625,
  1151. 48828125, 244140625, 1220703125
  1152. };
  1153. unsigned int n13;
  1154. for (n13 = 0; n13 <= abs_n; n13 += 13)
  1155. {
  1156. mp_limb_t digit1 = small_pow5[n13 + 13 <= abs_n ? 13 : abs_n - n13];
  1157. size_t j;
  1158. mp_twolimb_t carry = 0;
  1159. for (j = 0; j < pow5_len; j++)
  1160. {
  1161. mp_limb_t digit2 = pow5_ptr[j];
  1162. carry += (mp_twolimb_t) digit1 * (mp_twolimb_t) digit2;
  1163. pow5_ptr[j] = (mp_limb_t) carry;
  1164. carry = carry >> GMP_LIMB_BITS;
  1165. }
  1166. if (carry > 0)
  1167. pow5_ptr[pow5_len++] = (mp_limb_t) carry;
  1168. }
  1169. }
  1170. s_limbs = abs_s / GMP_LIMB_BITS;
  1171. s_bits = abs_s % GMP_LIMB_BITS;
  1172. if (n >= 0 ? s >= 0 : s <= 0)
  1173. {
  1174. /* Multiply with 2^|s|. */
  1175. if (s_bits > 0)
  1176. {
  1177. mp_limb_t *ptr = pow5_ptr;
  1178. mp_twolimb_t accu = 0;
  1179. size_t count;
  1180. for (count = pow5_len; count > 0; count--)
  1181. {
  1182. accu += (mp_twolimb_t) *ptr << s_bits;
  1183. *ptr++ = (mp_limb_t) accu;
  1184. accu = accu >> GMP_LIMB_BITS;
  1185. }
  1186. if (accu > 0)
  1187. {
  1188. *ptr = (mp_limb_t) accu;
  1189. pow5_len++;
  1190. }
  1191. }
  1192. if (s_limbs > 0)
  1193. {
  1194. size_t count;
  1195. for (count = pow5_len; count > 0;)
  1196. {
  1197. count--;
  1198. pow5_ptr[s_limbs + count] = pow5_ptr[count];
  1199. }
  1200. for (count = s_limbs; count > 0;)
  1201. {
  1202. count--;
  1203. pow5_ptr[count] = 0;
  1204. }
  1205. pow5_len += s_limbs;
  1206. }
  1207. pow5.limbs = pow5_ptr;
  1208. pow5.nlimbs = pow5_len;
  1209. if (n >= 0)
  1210. {
  1211. /* Multiply m with pow5. No division needed. */
  1212. z_memory = multiply (m, pow5, &z);
  1213. }
  1214. else
  1215. {
  1216. /* Divide m by pow5 and round. */
  1217. z_memory = divide (m, pow5, &z);
  1218. }
  1219. }
  1220. else
  1221. {
  1222. pow5.limbs = pow5_ptr;
  1223. pow5.nlimbs = pow5_len;
  1224. if (n >= 0)
  1225. {
  1226. /* n >= 0, s < 0.
  1227. Multiply m with pow5, then divide by 2^|s|. */
  1228. mpn_t numerator;
  1229. mpn_t denominator;
  1230. void *tmp_memory;
  1231. tmp_memory = multiply (m, pow5, &numerator);
  1232. if (tmp_memory == NULL)
  1233. {
  1234. free (pow5_ptr);
  1235. free (memory);
  1236. return NULL;
  1237. }
  1238. /* Construct 2^|s|. */
  1239. {
  1240. mp_limb_t *ptr = pow5_ptr + pow5_len;
  1241. size_t i;
  1242. for (i = 0; i < s_limbs; i++)
  1243. ptr[i] = 0;
  1244. ptr[s_limbs] = (mp_limb_t) 1 << s_bits;
  1245. denominator.limbs = ptr;
  1246. denominator.nlimbs = s_limbs + 1;
  1247. }
  1248. z_memory = divide (numerator, denominator, &z);
  1249. free (tmp_memory);
  1250. }
  1251. else
  1252. {
  1253. /* n < 0, s > 0.
  1254. Multiply m with 2^s, then divide by pow5. */
  1255. mpn_t numerator;
  1256. mp_limb_t *num_ptr;
  1257. num_ptr = (mp_limb_t *) malloc ((m.nlimbs + s_limbs + 1)
  1258. * sizeof (mp_limb_t));
  1259. if (num_ptr == NULL)
  1260. {
  1261. free (pow5_ptr);
  1262. free (memory);
  1263. return NULL;
  1264. }
  1265. {
  1266. mp_limb_t *destptr = num_ptr;
  1267. {
  1268. size_t i;
  1269. for (i = 0; i < s_limbs; i++)
  1270. *destptr++ = 0;
  1271. }
  1272. if (s_bits > 0)
  1273. {
  1274. const mp_limb_t *sourceptr = m.limbs;
  1275. mp_twolimb_t accu = 0;
  1276. size_t count;
  1277. for (count = m.nlimbs; count > 0; count--)
  1278. {
  1279. accu += (mp_twolimb_t) *sourceptr++ << s_bits;
  1280. *destptr++ = (mp_limb_t) accu;
  1281. accu = accu >> GMP_LIMB_BITS;
  1282. }
  1283. if (accu > 0)
  1284. *destptr++ = (mp_limb_t) accu;
  1285. }
  1286. else
  1287. {
  1288. const mp_limb_t *sourceptr = m.limbs;
  1289. size_t count;
  1290. for (count = m.nlimbs; count > 0; count--)
  1291. *destptr++ = *sourceptr++;
  1292. }
  1293. numerator.limbs = num_ptr;
  1294. numerator.nlimbs = destptr - num_ptr;
  1295. }
  1296. z_memory = divide (numerator, pow5, &z);
  1297. free (num_ptr);
  1298. }
  1299. }
  1300. free (pow5_ptr);
  1301. free (memory);
  1302. /* Here y = round (x * 10^n) = z * 10^extra_zeroes. */
  1303. if (z_memory == NULL)
  1304. return NULL;
  1305. digits = convert_to_decimal (z, extra_zeroes);
  1306. free (z_memory);
  1307. return digits;
  1308. }
  1309. # if NEED_PRINTF_LONG_DOUBLE
  1310. /* Assuming x is finite and >= 0, and n is an integer:
  1311. Returns the decimal representation of round (x * 10^n).
  1312. Return the allocated memory - containing the decimal digits in low-to-high
  1313. order, terminated with a NUL character - in case of success, NULL in case
  1314. of memory allocation failure. */
  1315. static char *
  1316. scale10_round_decimal_long_double (long double x, int n)
  1317. {
  1318. int e IF_LINT(= 0);
  1319. mpn_t m;
  1320. void *memory = decode_long_double (x, &e, &m);
  1321. return scale10_round_decimal_decoded (e, m, memory, n);
  1322. }
  1323. # endif
  1324. # if NEED_PRINTF_DOUBLE
  1325. /* Assuming x is finite and >= 0, and n is an integer:
  1326. Returns the decimal representation of round (x * 10^n).
  1327. Return the allocated memory - containing the decimal digits in low-to-high
  1328. order, terminated with a NUL character - in case of success, NULL in case
  1329. of memory allocation failure. */
  1330. static char *
  1331. scale10_round_decimal_double (double x, int n)
  1332. {
  1333. int e IF_LINT(= 0);
  1334. mpn_t m;
  1335. void *memory = decode_double (x, &e, &m);
  1336. return scale10_round_decimal_decoded (e, m, memory, n);
  1337. }
  1338. # endif
  1339. # if NEED_PRINTF_LONG_DOUBLE
  1340. /* Assuming x is finite and > 0:
  1341. Return an approximation for n with 10^n <= x < 10^(n+1).
  1342. The approximation is usually the right n, but may be off by 1 sometimes. */
  1343. static int
  1344. floorlog10l (long double x)
  1345. {
  1346. int exp;
  1347. long double y;
  1348. double z;
  1349. double l;
  1350. /* Split into exponential part and mantissa. */
  1351. y = frexpl (x, &exp);
  1352. if (!(y >= 0.0L && y < 1.0L))
  1353. abort ();
  1354. if (y == 0.0L)
  1355. return INT_MIN;
  1356. if (y < 0.5L)
  1357. {
  1358. while (y < (1.0L / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2))))
  1359. {
  1360. y *= 1.0L * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2));
  1361. exp -= GMP_LIMB_BITS;
  1362. }
  1363. if (y < (1.0L / (1 << 16)))
  1364. {
  1365. y *= 1.0L * (1 << 16);
  1366. exp -= 16;
  1367. }
  1368. if (y < (1.0L / (1 << 8)))
  1369. {
  1370. y *= 1.0L * (1 << 8);
  1371. exp -= 8;
  1372. }
  1373. if (y < (1.0L / (1 << 4)))
  1374. {
  1375. y *= 1.0L * (1 << 4);
  1376. exp -= 4;
  1377. }
  1378. if (y < (1.0L / (1 << 2)))
  1379. {
  1380. y *= 1.0L * (1 << 2);
  1381. exp -= 2;
  1382. }
  1383. if (y < (1.0L / (1 << 1)))
  1384. {
  1385. y *= 1.0L * (1 << 1);
  1386. exp -= 1;
  1387. }
  1388. }
  1389. if (!(y >= 0.5L && y < 1.0L))
  1390. abort ();
  1391. /* Compute an approximation for l = log2(x) = exp + log2(y). */
  1392. l = exp;
  1393. z = y;
  1394. if (z < 0.70710678118654752444)
  1395. {
  1396. z *= 1.4142135623730950488;
  1397. l -= 0.5;
  1398. }
  1399. if (z < 0.8408964152537145431)
  1400. {
  1401. z *= 1.1892071150027210667;
  1402. l -= 0.25;
  1403. }
  1404. if (z < 0.91700404320467123175)
  1405. {
  1406. z *= 1.0905077326652576592;
  1407. l -= 0.125;
  1408. }
  1409. if (z < 0.9576032806985736469)
  1410. {
  1411. z *= 1.0442737824274138403;
  1412. l -= 0.0625;
  1413. }
  1414. /* Now 0.95 <= z <= 1.01. */
  1415. z = 1 - z;
  1416. /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
  1417. Four terms are enough to get an approximation with error < 10^-7. */
  1418. l -= 1.4426950408889634074 * z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25)));
  1419. /* Finally multiply with log(2)/log(10), yields an approximation for
  1420. log10(x). */
  1421. l *= 0.30102999566398119523;
  1422. /* Round down to the next integer. */
  1423. return (int) l + (l < 0 ? -1 : 0);
  1424. }
  1425. # endif
  1426. # if NEED_PRINTF_DOUBLE
  1427. /* Assuming x is finite and > 0:
  1428. Return an approximation for n with 10^n <= x < 10^(n+1).
  1429. The approximation is usually the right n, but may be off by 1 sometimes. */
  1430. static int
  1431. floorlog10 (double x)
  1432. {
  1433. int exp;
  1434. double y;
  1435. double z;
  1436. double l;
  1437. /* Split into exponential part and mantissa. */
  1438. y = frexp (x, &exp);
  1439. if (!(y >= 0.0 && y < 1.0))
  1440. abort ();
  1441. if (y == 0.0)
  1442. return INT_MIN;
  1443. if (y < 0.5)
  1444. {
  1445. while (y < (1.0 / (1 << (GMP_LIMB_BITS / 2)) / (1 << (GMP_LIMB_BITS / 2))))
  1446. {
  1447. y *= 1.0 * (1 << (GMP_LIMB_BITS / 2)) * (1 << (GMP_LIMB_BITS / 2));
  1448. exp -= GMP_LIMB_BITS;
  1449. }
  1450. if (y < (1.0 / (1 << 16)))
  1451. {
  1452. y *= 1.0 * (1 << 16);
  1453. exp -= 16;
  1454. }
  1455. if (y < (1.0 / (1 << 8)))
  1456. {
  1457. y *= 1.0 * (1 << 8);
  1458. exp -= 8;
  1459. }
  1460. if (y < (1.0 / (1 << 4)))
  1461. {
  1462. y *= 1.0 * (1 << 4);
  1463. exp -= 4;
  1464. }
  1465. if (y < (1.0 / (1 << 2)))
  1466. {
  1467. y *= 1.0 * (1 << 2);
  1468. exp -= 2;
  1469. }
  1470. if (y < (1.0 / (1 << 1)))
  1471. {
  1472. y *= 1.0 * (1 << 1);
  1473. exp -= 1;
  1474. }
  1475. }
  1476. if (!(y >= 0.5 && y < 1.0))
  1477. abort ();
  1478. /* Compute an approximation for l = log2(x) = exp + log2(y). */
  1479. l = exp;
  1480. z = y;
  1481. if (z < 0.70710678118654752444)
  1482. {
  1483. z *= 1.4142135623730950488;
  1484. l -= 0.5;
  1485. }
  1486. if (z < 0.8408964152537145431)
  1487. {
  1488. z *= 1.1892071150027210667;
  1489. l -= 0.25;
  1490. }
  1491. if (z < 0.91700404320467123175)
  1492. {
  1493. z *= 1.0905077326652576592;
  1494. l -= 0.125;
  1495. }
  1496. if (z < 0.9576032806985736469)
  1497. {
  1498. z *= 1.0442737824274138403;
  1499. l -= 0.0625;
  1500. }
  1501. /* Now 0.95 <= z <= 1.01. */
  1502. z = 1 - z;
  1503. /* log2(1-z) = 1/log(2) * (- z - z^2/2 - z^3/3 - z^4/4 - ...)
  1504. Four terms are enough to get an approximation with error < 10^-7. */
  1505. l -= 1.4426950408889634074 * z * (1.0 + z * (0.5 + z * ((1.0 / 3) + z * 0.25)));
  1506. /* Finally multiply with log(2)/log(10), yields an approximation for
  1507. log10(x). */
  1508. l *= 0.30102999566398119523;
  1509. /* Round down to the next integer. */
  1510. return (int) l + (l < 0 ? -1 : 0);
  1511. }
  1512. # endif
  1513. /* Tests whether a string of digits consists of exactly PRECISION zeroes and
  1514. a single '1' digit. */
  1515. static int
  1516. is_borderline (const char *digits, size_t precision)
  1517. {
  1518. for (; precision > 0; precision--, digits++)
  1519. if (*digits != '0')
  1520. return 0;
  1521. if (*digits != '1')
  1522. return 0;
  1523. digits++;
  1524. return *digits == '\0';
  1525. }
  1526. #endif
  1527. #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF
  1528. /* Use a different function name, to make it possible that the 'wchar_t'
  1529. parametrization and the 'char' parametrization get compiled in the same
  1530. translation unit. */
  1531. # if WIDE_CHAR_VERSION
  1532. # define MAX_ROOM_NEEDED wmax_room_needed
  1533. # else
  1534. # define MAX_ROOM_NEEDED max_room_needed
  1535. # endif
  1536. /* Returns the number of TCHAR_T units needed as temporary space for the result
  1537. of sprintf or SNPRINTF of a single conversion directive. */
  1538. static size_t
  1539. MAX_ROOM_NEEDED (const arguments *ap, size_t arg_index, FCHAR_T conversion,
  1540. arg_type type, int flags, size_t width, int has_precision,
  1541. size_t precision, int pad_ourselves)
  1542. {
  1543. size_t tmp_length;
  1544. switch (conversion)
  1545. {
  1546. case 'd': case 'i': case 'u':
  1547. if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
  1548. tmp_length =
  1549. (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
  1550. * 0.30103 /* binary -> decimal */
  1551. )
  1552. + 1; /* turn floor into ceil */
  1553. else if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
  1554. tmp_length =
  1555. (unsigned int) (sizeof (unsigned long) * CHAR_BIT
  1556. * 0.30103 /* binary -> decimal */
  1557. )
  1558. + 1; /* turn floor into ceil */
  1559. else
  1560. tmp_length =
  1561. (unsigned int) (sizeof (unsigned int) * CHAR_BIT
  1562. * 0.30103 /* binary -> decimal */
  1563. )
  1564. + 1; /* turn floor into ceil */
  1565. if (tmp_length < precision)
  1566. tmp_length = precision;
  1567. /* Multiply by 2, as an estimate for FLAG_GROUP. */
  1568. tmp_length = xsum (tmp_length, tmp_length);
  1569. /* Add 1, to account for a leading sign. */
  1570. tmp_length = xsum (tmp_length, 1);
  1571. break;
  1572. case 'o':
  1573. if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
  1574. tmp_length =
  1575. (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
  1576. * 0.333334 /* binary -> octal */
  1577. )
  1578. + 1; /* turn floor into ceil */
  1579. else if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
  1580. tmp_length =
  1581. (unsigned int) (sizeof (unsigned long) * CHAR_BIT
  1582. * 0.333334 /* binary -> octal */
  1583. )
  1584. + 1; /* turn floor into ceil */
  1585. else
  1586. tmp_length =
  1587. (unsigned int) (sizeof (unsigned int) * CHAR_BIT
  1588. * 0.333334 /* binary -> octal */
  1589. )
  1590. + 1; /* turn floor into ceil */
  1591. if (tmp_length < precision)
  1592. tmp_length = precision;
  1593. /* Add 1, to account for a leading sign. */
  1594. tmp_length = xsum (tmp_length, 1);
  1595. break;
  1596. case 'x': case 'X':
  1597. if (type == TYPE_LONGLONGINT || type == TYPE_ULONGLONGINT)
  1598. tmp_length =
  1599. (unsigned int) (sizeof (unsigned long long) * CHAR_BIT
  1600. * 0.25 /* binary -> hexadecimal */
  1601. )
  1602. + 1; /* turn floor into ceil */
  1603. else if (type == TYPE_LONGINT || type == TYPE_ULONGINT)
  1604. tmp_length =
  1605. (unsigned int) (sizeof (unsigned long) * CHAR_BIT
  1606. * 0.25 /* binary -> hexadecimal */
  1607. )
  1608. + 1; /* turn floor into ceil */
  1609. else
  1610. tmp_length =
  1611. (unsigned int) (sizeof (unsigned int) * CHAR_BIT
  1612. * 0.25 /* binary -> hexadecimal */
  1613. )
  1614. + 1; /* turn floor into ceil */
  1615. if (tmp_length < precision)
  1616. tmp_length = precision;
  1617. /* Add 2, to account for a leading sign or alternate form. */
  1618. tmp_length = xsum (tmp_length, 2);
  1619. break;
  1620. case 'f': case 'F':
  1621. if (type == TYPE_LONGDOUBLE)
  1622. tmp_length =
  1623. (unsigned int) (LDBL_MAX_EXP
  1624. * 0.30103 /* binary -> decimal */
  1625. * 2 /* estimate for FLAG_GROUP */
  1626. )
  1627. + 1 /* turn floor into ceil */
  1628. + 10; /* sign, decimal point etc. */
  1629. else
  1630. tmp_length =
  1631. (unsigned int) (DBL_MAX_EXP
  1632. * 0.30103 /* binary -> decimal */
  1633. * 2 /* estimate for FLAG_GROUP */
  1634. )
  1635. + 1 /* turn floor into ceil */
  1636. + 10; /* sign, decimal point etc. */
  1637. tmp_length = xsum (tmp_length, precision);
  1638. break;
  1639. case 'e': case 'E': case 'g': case 'G':
  1640. tmp_length =
  1641. 12; /* sign, decimal point, exponent etc. */
  1642. tmp_length = xsum (tmp_length, precision);
  1643. break;
  1644. case 'a': case 'A':
  1645. if (type == TYPE_LONGDOUBLE)
  1646. tmp_length =
  1647. (unsigned int) (LDBL_DIG
  1648. * 0.831 /* decimal -> hexadecimal */
  1649. )
  1650. + 1; /* turn floor into ceil */
  1651. else
  1652. tmp_length =
  1653. (unsigned int) (DBL_DIG
  1654. * 0.831 /* decimal -> hexadecimal */
  1655. )
  1656. + 1; /* turn floor into ceil */
  1657. if (tmp_length < precision)
  1658. tmp_length = precision;
  1659. /* Account for sign, decimal point etc. */
  1660. tmp_length = xsum (tmp_length, 12);
  1661. break;
  1662. case 'c':
  1663. # if HAVE_WINT_T && !WIDE_CHAR_VERSION
  1664. if (type == TYPE_WIDE_CHAR)
  1665. {
  1666. tmp_length = MB_CUR_MAX;
  1667. # if ENABLE_WCHAR_FALLBACK
  1668. if (tmp_length < (sizeof (wchar_t) > 2 ? 10 : 6))
  1669. tmp_length = (sizeof (wchar_t) > 2 ? 10 : 6);
  1670. # endif
  1671. }
  1672. else
  1673. # endif
  1674. tmp_length = 1;
  1675. break;
  1676. case 's':
  1677. # if HAVE_WCHAR_T
  1678. if (type == TYPE_WIDE_STRING)
  1679. {
  1680. # if WIDE_CHAR_VERSION
  1681. /* ISO C says about %ls in fwprintf:
  1682. "If the precision is not specified or is greater than the size
  1683. of the array, the array shall contain a null wide character."
  1684. So if there is a precision, we must not use wcslen. */
  1685. const wchar_t *arg = ap->arg[arg_index].a.a_wide_string;
  1686. if (has_precision)
  1687. tmp_length = local_wcsnlen (arg, precision);
  1688. else
  1689. tmp_length = local_wcslen (arg);
  1690. # else
  1691. /* ISO C says about %ls in fprintf:
  1692. "If a precision is specified, no more than that many bytes are
  1693. written (including shift sequences, if any), and the array
  1694. shall contain a null wide character if, to equal the multibyte
  1695. character sequence length given by the precision, the function
  1696. would need to access a wide character one past the end of the
  1697. array."
  1698. So if there is a precision, we must not use wcslen. */
  1699. /* This case has already been handled separately in VASNPRINTF. */
  1700. abort ();
  1701. # endif
  1702. }
  1703. else
  1704. # endif
  1705. {
  1706. # if WIDE_CHAR_VERSION
  1707. /* ISO C says about %s in fwprintf:
  1708. "If the precision is not specified or is greater than the size
  1709. of the converted array, the converted array shall contain a
  1710. null wide character."
  1711. So if there is a precision, we must not use strlen. */
  1712. /* This case has already been handled separately in VASNPRINTF. */
  1713. abort ();
  1714. # else
  1715. /* ISO C says about %s in fprintf:
  1716. "If the precision is not specified or greater than the size of
  1717. the array, the array shall contain a null character."
  1718. So if there is a precision, we must not use strlen. */
  1719. const char *arg = ap->arg[arg_index].a.a_string;
  1720. if (has_precision)
  1721. tmp_length = local_strnlen (arg, precision);
  1722. else
  1723. tmp_length = strlen (arg);
  1724. # endif
  1725. }
  1726. break;
  1727. case 'p':
  1728. tmp_length =
  1729. (unsigned int) (sizeof (void *) * CHAR_BIT
  1730. * 0.25 /* binary -> hexadecimal */
  1731. )
  1732. + 1 /* turn floor into ceil */
  1733. + 2; /* account for leading 0x */
  1734. break;
  1735. default:
  1736. abort ();
  1737. }
  1738. if (!pad_ourselves)
  1739. {
  1740. # if ENABLE_UNISTDIO
  1741. /* Padding considers the number of characters, therefore the number of
  1742. elements after padding may be
  1743. > max (tmp_length, width)
  1744. but is certainly
  1745. <= tmp_length + width. */
  1746. tmp_length = xsum (tmp_length, width);
  1747. # else
  1748. /* Padding considers the number of elements, says POSIX. */
  1749. if (tmp_length < width)
  1750. tmp_length = width;
  1751. # endif
  1752. }
  1753. tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
  1754. return tmp_length;
  1755. }
  1756. #endif
  1757. DCHAR_T *
  1758. VASNPRINTF (DCHAR_T *resultbuf, size_t *lengthp,
  1759. const FCHAR_T *format, va_list args)
  1760. {
  1761. DIRECTIVES d;
  1762. arguments a;
  1763. if (PRINTF_PARSE (format, &d, &a) < 0)
  1764. /* errno is already set. */
  1765. return NULL;
  1766. /* Frees the memory allocated by this function. Preserves errno. */
  1767. #define CLEANUP() \
  1768. if (d.dir != d.direct_alloc_dir) \
  1769. free (d.dir); \
  1770. if (a.arg != a.direct_alloc_arg) \
  1771. free (a.arg);
  1772. if (PRINTF_FETCHARGS (args, &a) < 0)
  1773. goto fail_1_with_EINVAL;
  1774. {
  1775. size_t buf_neededlength;
  1776. TCHAR_T *buf;
  1777. TCHAR_T *buf_malloced;
  1778. const FCHAR_T *cp;
  1779. size_t i;
  1780. DIRECTIVE *dp;
  1781. /* Output string accumulator. */
  1782. DCHAR_T *result;
  1783. size_t allocated;
  1784. size_t length;
  1785. /* Allocate a small buffer that will hold a directive passed to
  1786. sprintf or snprintf. */
  1787. buf_neededlength =
  1788. xsum4 (7, d.max_width_length, d.max_precision_length, 6);
  1789. #if HAVE_ALLOCA
  1790. if (buf_neededlength < 4000 / sizeof (TCHAR_T))
  1791. {
  1792. buf = (TCHAR_T *) alloca (buf_neededlength * sizeof (TCHAR_T));
  1793. buf_malloced = NULL;
  1794. }
  1795. else
  1796. #endif
  1797. {
  1798. size_t buf_memsize = xtimes (buf_neededlength, sizeof (TCHAR_T));
  1799. if (size_overflow_p (buf_memsize))
  1800. goto out_of_memory_1;
  1801. buf = (TCHAR_T *) malloc (buf_memsize);
  1802. if (buf == NULL)
  1803. goto out_of_memory_1;
  1804. buf_malloced = buf;
  1805. }
  1806. result = resultbuf;
  1807. allocated = (resultbuf != NULL ? *lengthp : 0);
  1808. length = 0;
  1809. /* Invariants:
  1810. result is either == resultbuf or malloc-allocated.
  1811. If result == NULL, resultbuf is == NULL as well.
  1812. If length > 0, then result != NULL. */
  1813. /* Ensures that allocated >= needed. Aborts through a jump to
  1814. out_of_memory if needed is SIZE_MAX or otherwise too big. */
  1815. #define ENSURE_ALLOCATION_ELSE(needed, oom_statement) \
  1816. if ((needed) > allocated) \
  1817. { \
  1818. size_t memory_size; \
  1819. DCHAR_T *memory; \
  1820. \
  1821. allocated = (allocated > 0 ? xtimes (allocated, 2) : 12); \
  1822. if ((needed) > allocated) \
  1823. allocated = (needed); \
  1824. memory_size = xtimes (allocated, sizeof (DCHAR_T)); \
  1825. if (size_overflow_p (memory_size)) \
  1826. oom_statement \
  1827. if (result == resultbuf) \
  1828. memory = (DCHAR_T *) malloc (memory_size); \
  1829. else \
  1830. memory = (DCHAR_T *) realloc (result, memory_size); \
  1831. if (memory == NULL) \
  1832. oom_statement \
  1833. if (result == resultbuf && length > 0) \
  1834. DCHAR_CPY (memory, result, length); \
  1835. result = memory; \
  1836. }
  1837. #define ENSURE_ALLOCATION(needed) \
  1838. ENSURE_ALLOCATION_ELSE((needed), goto out_of_memory; )
  1839. for (cp = format, i = 0, dp = &d.dir[0]; ; cp = dp->dir_end, i++, dp++)
  1840. {
  1841. if (cp != dp->dir_start)
  1842. {
  1843. size_t n = dp->dir_start - cp;
  1844. size_t augmented_length = xsum (length, n);
  1845. ENSURE_ALLOCATION (augmented_length);
  1846. /* This copies a piece of FCHAR_T[] into a DCHAR_T[]. Here we
  1847. need that the format string contains only ASCII characters
  1848. if FCHAR_T and DCHAR_T are not the same type. */
  1849. if (sizeof (FCHAR_T) == sizeof (DCHAR_T))
  1850. {
  1851. DCHAR_CPY (result + length, (const DCHAR_T *) cp, n);
  1852. length = augmented_length;
  1853. }
  1854. else
  1855. {
  1856. do
  1857. result[length++] = *cp++;
  1858. while (--n > 0);
  1859. }
  1860. }
  1861. if (i == d.count)
  1862. break;
  1863. /* Execute a single directive. */
  1864. if (dp->conversion == '%')
  1865. {
  1866. size_t augmented_length;
  1867. if (!(dp->arg_index == ARG_NONE))
  1868. abort ();
  1869. augmented_length = xsum (length, 1);
  1870. ENSURE_ALLOCATION (augmented_length);
  1871. result[length] = '%';
  1872. length = augmented_length;
  1873. }
  1874. else
  1875. {
  1876. if (!(dp->arg_index != ARG_NONE))
  1877. abort ();
  1878. if (dp->conversion == 'n')
  1879. {
  1880. switch (a.arg[dp->arg_index].type)
  1881. {
  1882. case TYPE_COUNT_SCHAR_POINTER:
  1883. *a.arg[dp->arg_index].a.a_count_schar_pointer = length;
  1884. break;
  1885. case TYPE_COUNT_SHORT_POINTER:
  1886. *a.arg[dp->arg_index].a.a_count_short_pointer = length;
  1887. break;
  1888. case TYPE_COUNT_INT_POINTER:
  1889. *a.arg[dp->arg_index].a.a_count_int_pointer = length;
  1890. break;
  1891. case TYPE_COUNT_LONGINT_POINTER:
  1892. *a.arg[dp->arg_index].a.a_count_longint_pointer = length;
  1893. break;
  1894. case TYPE_COUNT_LONGLONGINT_POINTER:
  1895. *a.arg[dp->arg_index].a.a_count_longlongint_pointer = length;
  1896. break;
  1897. default:
  1898. abort ();
  1899. }
  1900. }
  1901. #if ENABLE_UNISTDIO
  1902. /* The unistdio extensions. */
  1903. else if (dp->conversion == 'U')
  1904. {
  1905. arg_type type = a.arg[dp->arg_index].type;
  1906. int flags = dp->flags;
  1907. int has_width;
  1908. size_t width;
  1909. int has_precision;
  1910. size_t precision;
  1911. has_width = 0;
  1912. width = 0;
  1913. if (dp->width_start != dp->width_end)
  1914. {
  1915. if (dp->width_arg_index != ARG_NONE)
  1916. {
  1917. int arg;
  1918. if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
  1919. abort ();
  1920. arg = a.arg[dp->width_arg_index].a.a_int;
  1921. width = arg;
  1922. if (arg < 0)
  1923. {
  1924. /* "A negative field width is taken as a '-' flag
  1925. followed by a positive field width." */
  1926. flags |= FLAG_LEFT;
  1927. width = -width;
  1928. }
  1929. }
  1930. else
  1931. {
  1932. const FCHAR_T *digitp = dp->width_start;
  1933. do
  1934. width = xsum (xtimes (width, 10), *digitp++ - '0');
  1935. while (digitp != dp->width_end);
  1936. }
  1937. has_width = 1;
  1938. }
  1939. has_precision = 0;
  1940. precision = 0;
  1941. if (dp->precision_start != dp->precision_end)
  1942. {
  1943. if (dp->precision_arg_index != ARG_NONE)
  1944. {
  1945. int arg;
  1946. if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
  1947. abort ();
  1948. arg = a.arg[dp->precision_arg_index].a.a_int;
  1949. /* "A negative precision is taken as if the precision
  1950. were omitted." */
  1951. if (arg >= 0)
  1952. {
  1953. precision = arg;
  1954. has_precision = 1;
  1955. }
  1956. }
  1957. else
  1958. {
  1959. const FCHAR_T *digitp = dp->precision_start + 1;
  1960. precision = 0;
  1961. while (digitp != dp->precision_end)
  1962. precision = xsum (xtimes (precision, 10), *digitp++ - '0');
  1963. has_precision = 1;
  1964. }
  1965. }
  1966. switch (type)
  1967. {
  1968. case TYPE_U8_STRING:
  1969. {
  1970. const uint8_t *arg = a.arg[dp->arg_index].a.a_u8_string;
  1971. const uint8_t *arg_end;
  1972. size_t characters;
  1973. if (has_precision)
  1974. {
  1975. /* Use only PRECISION characters, from the left. */
  1976. arg_end = arg;
  1977. characters = 0;
  1978. for (; precision > 0; precision--)
  1979. {
  1980. int count = u8_strmblen (arg_end);
  1981. if (count == 0)
  1982. break;
  1983. if (count < 0)
  1984. goto fail_with_EILSEQ;
  1985. arg_end += count;
  1986. characters++;
  1987. }
  1988. }
  1989. else if (has_width)
  1990. {
  1991. /* Use the entire string, and count the number of
  1992. characters. */
  1993. arg_end = arg;
  1994. characters = 0;
  1995. for (;;)
  1996. {
  1997. int count = u8_strmblen (arg_end);
  1998. if (count == 0)
  1999. break;
  2000. if (count < 0)
  2001. goto fail_with_EILSEQ;
  2002. arg_end += count;
  2003. characters++;
  2004. }
  2005. }
  2006. else
  2007. {
  2008. /* Use the entire string. */
  2009. arg_end = arg + u8_strlen (arg);
  2010. /* The number of characters doesn't matter. */
  2011. characters = 0;
  2012. }
  2013. if (characters < width && !(dp->flags & FLAG_LEFT))
  2014. {
  2015. size_t n = width - characters;
  2016. ENSURE_ALLOCATION (xsum (length, n));
  2017. DCHAR_SET (result + length, ' ', n);
  2018. length += n;
  2019. }
  2020. # if DCHAR_IS_UINT8_T
  2021. {
  2022. size_t n = arg_end - arg;
  2023. ENSURE_ALLOCATION (xsum (length, n));
  2024. DCHAR_CPY (result + length, arg, n);
  2025. length += n;
  2026. }
  2027. # else
  2028. { /* Convert. */
  2029. DCHAR_T *converted = result + length;
  2030. size_t converted_len = allocated - length;
  2031. # if DCHAR_IS_TCHAR
  2032. /* Convert from UTF-8 to locale encoding. */
  2033. converted =
  2034. u8_conv_to_encoding (locale_charset (),
  2035. iconveh_question_mark,
  2036. arg, arg_end - arg, NULL,
  2037. converted, &converted_len);
  2038. # else
  2039. /* Convert from UTF-8 to UTF-16/UTF-32. */
  2040. converted =
  2041. U8_TO_DCHAR (arg, arg_end - arg,
  2042. converted, &converted_len);
  2043. # endif
  2044. if (converted == NULL)
  2045. goto fail_with_errno;
  2046. if (converted != result + length)
  2047. {
  2048. ENSURE_ALLOCATION_ELSE (xsum (length, converted_len),
  2049. { free (converted); goto out_of_memory; });
  2050. DCHAR_CPY (result + length, converted, converted_len);
  2051. free (converted);
  2052. }
  2053. length += converted_len;
  2054. }
  2055. # endif
  2056. if (characters < width && (dp->flags & FLAG_LEFT))
  2057. {
  2058. size_t n = width - characters;
  2059. ENSURE_ALLOCATION (xsum (length, n));
  2060. DCHAR_SET (result + length, ' ', n);
  2061. length += n;
  2062. }
  2063. }
  2064. break;
  2065. case TYPE_U16_STRING:
  2066. {
  2067. const uint16_t *arg = a.arg[dp->arg_index].a.a_u16_string;
  2068. const uint16_t *arg_end;
  2069. size_t characters;
  2070. if (has_precision)
  2071. {
  2072. /* Use only PRECISION characters, from the left. */
  2073. arg_end = arg;
  2074. characters = 0;
  2075. for (; precision > 0; precision--)
  2076. {
  2077. int count = u16_strmblen (arg_end);
  2078. if (count == 0)
  2079. break;
  2080. if (count < 0)
  2081. goto fail_with_EILSEQ;
  2082. arg_end += count;
  2083. characters++;
  2084. }
  2085. }
  2086. else if (has_width)
  2087. {
  2088. /* Use the entire string, and count the number of
  2089. characters. */
  2090. arg_end = arg;
  2091. characters = 0;
  2092. for (;;)
  2093. {
  2094. int count = u16_strmblen (arg_end);
  2095. if (count == 0)
  2096. break;
  2097. if (count < 0)
  2098. goto fail_with_EILSEQ;
  2099. arg_end += count;
  2100. characters++;
  2101. }
  2102. }
  2103. else
  2104. {
  2105. /* Use the entire string. */
  2106. arg_end = arg + u16_strlen (arg);
  2107. /* The number of characters doesn't matter. */
  2108. characters = 0;
  2109. }
  2110. if (characters < width && !(dp->flags & FLAG_LEFT))
  2111. {
  2112. size_t n = width - characters;
  2113. ENSURE_ALLOCATION (xsum (length, n));
  2114. DCHAR_SET (result + length, ' ', n);
  2115. length += n;
  2116. }
  2117. # if DCHAR_IS_UINT16_T
  2118. {
  2119. size_t n = arg_end - arg;
  2120. ENSURE_ALLOCATION (xsum (length, n));
  2121. DCHAR_CPY (result + length, arg, n);
  2122. length += n;
  2123. }
  2124. # else
  2125. { /* Convert. */
  2126. DCHAR_T *converted = result + length;
  2127. size_t converted_len = allocated - length;
  2128. # if DCHAR_IS_TCHAR
  2129. /* Convert from UTF-16 to locale encoding. */
  2130. converted =
  2131. u16_conv_to_encoding (locale_charset (),
  2132. iconveh_question_mark,
  2133. arg, arg_end - arg, NULL,
  2134. converted, &converted_len);
  2135. # else
  2136. /* Convert from UTF-16 to UTF-8/UTF-32. */
  2137. converted =
  2138. U16_TO_DCHAR (arg, arg_end - arg,
  2139. converted, &converted_len);
  2140. # endif
  2141. if (converted == NULL)
  2142. goto fail_with_errno;
  2143. if (converted != result + length)
  2144. {
  2145. ENSURE_ALLOCATION_ELSE (xsum (length, converted_len),
  2146. { free (converted); goto out_of_memory; });
  2147. DCHAR_CPY (result + length, converted, converted_len);
  2148. free (converted);
  2149. }
  2150. length += converted_len;
  2151. }
  2152. # endif
  2153. if (characters < width && (dp->flags & FLAG_LEFT))
  2154. {
  2155. size_t n = width - characters;
  2156. ENSURE_ALLOCATION (xsum (length, n));
  2157. DCHAR_SET (result + length, ' ', n);
  2158. length += n;
  2159. }
  2160. }
  2161. break;
  2162. case TYPE_U32_STRING:
  2163. {
  2164. const uint32_t *arg = a.arg[dp->arg_index].a.a_u32_string;
  2165. const uint32_t *arg_end;
  2166. size_t characters;
  2167. if (has_precision)
  2168. {
  2169. /* Use only PRECISION characters, from the left. */
  2170. arg_end = arg;
  2171. characters = 0;
  2172. for (; precision > 0; precision--)
  2173. {
  2174. int count = u32_strmblen (arg_end);
  2175. if (count == 0)
  2176. break;
  2177. if (count < 0)
  2178. goto fail_with_EILSEQ;
  2179. arg_end += count;
  2180. characters++;
  2181. }
  2182. }
  2183. else if (has_width)
  2184. {
  2185. /* Use the entire string, and count the number of
  2186. characters. */
  2187. arg_end = arg;
  2188. characters = 0;
  2189. for (;;)
  2190. {
  2191. int count = u32_strmblen (arg_end);
  2192. if (count == 0)
  2193. break;
  2194. if (count < 0)
  2195. goto fail_with_EILSEQ;
  2196. arg_end += count;
  2197. characters++;
  2198. }
  2199. }
  2200. else
  2201. {
  2202. /* Use the entire string. */
  2203. arg_end = arg + u32_strlen (arg);
  2204. /* The number of characters doesn't matter. */
  2205. characters = 0;
  2206. }
  2207. if (characters < width && !(dp->flags & FLAG_LEFT))
  2208. {
  2209. size_t n = width - characters;
  2210. ENSURE_ALLOCATION (xsum (length, n));
  2211. DCHAR_SET (result + length, ' ', n);
  2212. length += n;
  2213. }
  2214. # if DCHAR_IS_UINT32_T
  2215. {
  2216. size_t n = arg_end - arg;
  2217. ENSURE_ALLOCATION (xsum (length, n));
  2218. DCHAR_CPY (result + length, arg, n);
  2219. length += n;
  2220. }
  2221. # else
  2222. { /* Convert. */
  2223. DCHAR_T *converted = result + length;
  2224. size_t converted_len = allocated - length;
  2225. # if DCHAR_IS_TCHAR
  2226. /* Convert from UTF-32 to locale encoding. */
  2227. converted =
  2228. u32_conv_to_encoding (locale_charset (),
  2229. iconveh_question_mark,
  2230. arg, arg_end - arg, NULL,
  2231. converted, &converted_len);
  2232. # else
  2233. /* Convert from UTF-32 to UTF-8/UTF-16. */
  2234. converted =
  2235. U32_TO_DCHAR (arg, arg_end - arg,
  2236. converted, &converted_len);
  2237. # endif
  2238. if (converted == NULL)
  2239. goto fail_with_errno;
  2240. if (converted != result + length)
  2241. {
  2242. ENSURE_ALLOCATION_ELSE (xsum (length, converted_len),
  2243. { free (converted); goto out_of_memory; });
  2244. DCHAR_CPY (result + length, converted, converted_len);
  2245. free (converted);
  2246. }
  2247. length += converted_len;
  2248. }
  2249. # endif
  2250. if (characters < width && (dp->flags & FLAG_LEFT))
  2251. {
  2252. size_t n = width - characters;
  2253. ENSURE_ALLOCATION (xsum (length, n));
  2254. DCHAR_SET (result + length, ' ', n);
  2255. length += n;
  2256. }
  2257. }
  2258. break;
  2259. default:
  2260. abort ();
  2261. }
  2262. }
  2263. #endif
  2264. #if (!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL) || ENABLE_WCHAR_FALLBACK) && HAVE_WCHAR_T
  2265. else if (dp->conversion == 's'
  2266. # if WIDE_CHAR_VERSION
  2267. && a.arg[dp->arg_index].type != TYPE_WIDE_STRING
  2268. # else
  2269. && a.arg[dp->arg_index].type == TYPE_WIDE_STRING
  2270. # endif
  2271. )
  2272. {
  2273. /* The normal handling of the 's' directive below requires
  2274. allocating a temporary buffer. The determination of its
  2275. length (tmp_length), in the case when a precision is
  2276. specified, below requires a conversion between a char[]
  2277. string and a wchar_t[] wide string. It could be done, but
  2278. we have no guarantee that the implementation of sprintf will
  2279. use the exactly same algorithm. Without this guarantee, it
  2280. is possible to have buffer overrun bugs. In order to avoid
  2281. such bugs, we implement the entire processing of the 's'
  2282. directive ourselves. */
  2283. int flags = dp->flags;
  2284. int has_width;
  2285. size_t width;
  2286. int has_precision;
  2287. size_t precision;
  2288. has_width = 0;
  2289. width = 0;
  2290. if (dp->width_start != dp->width_end)
  2291. {
  2292. if (dp->width_arg_index != ARG_NONE)
  2293. {
  2294. int arg;
  2295. if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
  2296. abort ();
  2297. arg = a.arg[dp->width_arg_index].a.a_int;
  2298. width = arg;
  2299. if (arg < 0)
  2300. {
  2301. /* "A negative field width is taken as a '-' flag
  2302. followed by a positive field width." */
  2303. flags |= FLAG_LEFT;
  2304. width = -width;
  2305. }
  2306. }
  2307. else
  2308. {
  2309. const FCHAR_T *digitp = dp->width_start;
  2310. do
  2311. width = xsum (xtimes (width, 10), *digitp++ - '0');
  2312. while (digitp != dp->width_end);
  2313. }
  2314. has_width = 1;
  2315. }
  2316. has_precision = 0;
  2317. precision = 6;
  2318. if (dp->precision_start != dp->precision_end)
  2319. {
  2320. if (dp->precision_arg_index != ARG_NONE)
  2321. {
  2322. int arg;
  2323. if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
  2324. abort ();
  2325. arg = a.arg[dp->precision_arg_index].a.a_int;
  2326. /* "A negative precision is taken as if the precision
  2327. were omitted." */
  2328. if (arg >= 0)
  2329. {
  2330. precision = arg;
  2331. has_precision = 1;
  2332. }
  2333. }
  2334. else
  2335. {
  2336. const FCHAR_T *digitp = dp->precision_start + 1;
  2337. precision = 0;
  2338. while (digitp != dp->precision_end)
  2339. precision = xsum (xtimes (precision, 10), *digitp++ - '0');
  2340. has_precision = 1;
  2341. }
  2342. }
  2343. # if WIDE_CHAR_VERSION
  2344. /* %s in vasnwprintf. See the specification of fwprintf. */
  2345. {
  2346. const char *arg = a.arg[dp->arg_index].a.a_string;
  2347. const char *arg_end;
  2348. size_t characters;
  2349. if (has_precision)
  2350. {
  2351. /* Use only as many bytes as needed to produce PRECISION
  2352. wide characters, from the left. */
  2353. # if HAVE_MBRTOWC
  2354. mbstate_t state;
  2355. memset (&state, '\0', sizeof (mbstate_t));
  2356. # endif
  2357. arg_end = arg;
  2358. characters = 0;
  2359. for (; precision > 0; precision--)
  2360. {
  2361. int count;
  2362. # if HAVE_MBRTOWC
  2363. count = mbrlen (arg_end, MB_CUR_MAX, &state);
  2364. # else
  2365. count = mblen (arg_end, MB_CUR_MAX);
  2366. # endif
  2367. if (count == 0)
  2368. /* Found the terminating NUL. */
  2369. break;
  2370. if (count < 0)
  2371. /* Invalid or incomplete multibyte character. */
  2372. goto fail_with_EILSEQ;
  2373. arg_end += count;
  2374. characters++;
  2375. }
  2376. }
  2377. else if (has_width)
  2378. {
  2379. /* Use the entire string, and count the number of wide
  2380. characters. */
  2381. # if HAVE_MBRTOWC
  2382. mbstate_t state;
  2383. memset (&state, '\0', sizeof (mbstate_t));
  2384. # endif
  2385. arg_end = arg;
  2386. characters = 0;
  2387. for (;;)
  2388. {
  2389. int count;
  2390. # if HAVE_MBRTOWC
  2391. count = mbrlen (arg_end, MB_CUR_MAX, &state);
  2392. # else
  2393. count = mblen (arg_end, MB_CUR_MAX);
  2394. # endif
  2395. if (count == 0)
  2396. /* Found the terminating NUL. */
  2397. break;
  2398. if (count < 0)
  2399. /* Invalid or incomplete multibyte character. */
  2400. goto fail_with_EILSEQ;
  2401. arg_end += count;
  2402. characters++;
  2403. }
  2404. }
  2405. else
  2406. {
  2407. /* Use the entire string. */
  2408. arg_end = arg + strlen (arg);
  2409. /* The number of characters doesn't matter. */
  2410. characters = 0;
  2411. }
  2412. if (characters < width && !(dp->flags & FLAG_LEFT))
  2413. {
  2414. size_t n = width - characters;
  2415. ENSURE_ALLOCATION (xsum (length, n));
  2416. DCHAR_SET (result + length, ' ', n);
  2417. length += n;
  2418. }
  2419. if (has_precision || has_width)
  2420. {
  2421. /* We know the number of wide characters in advance. */
  2422. size_t remaining;
  2423. # if HAVE_MBRTOWC
  2424. mbstate_t state;
  2425. memset (&state, '\0', sizeof (mbstate_t));
  2426. # endif
  2427. ENSURE_ALLOCATION (xsum (length, characters));
  2428. for (remaining = characters; remaining > 0; remaining--)
  2429. {
  2430. wchar_t wc;
  2431. int count;
  2432. # if HAVE_MBRTOWC
  2433. count = mbrtowc (&wc, arg, arg_end - arg, &state);
  2434. # else
  2435. count = mbtowc (&wc, arg, arg_end - arg);
  2436. # endif
  2437. if (count <= 0)
  2438. /* mbrtowc not consistent with mbrlen, or mbtowc
  2439. not consistent with mblen. */
  2440. abort ();
  2441. result[length++] = wc;
  2442. arg += count;
  2443. }
  2444. if (!(arg == arg_end))
  2445. abort ();
  2446. }
  2447. else
  2448. {
  2449. # if HAVE_MBRTOWC
  2450. mbstate_t state;
  2451. memset (&state, '\0', sizeof (mbstate_t));
  2452. # endif
  2453. while (arg < arg_end)
  2454. {
  2455. wchar_t wc;
  2456. int count;
  2457. # if HAVE_MBRTOWC
  2458. count = mbrtowc (&wc, arg, arg_end - arg, &state);
  2459. # else
  2460. count = mbtowc (&wc, arg, arg_end - arg);
  2461. # endif
  2462. if (count <= 0)
  2463. /* mbrtowc not consistent with mbrlen, or mbtowc
  2464. not consistent with mblen. */
  2465. abort ();
  2466. ENSURE_ALLOCATION (xsum (length, 1));
  2467. result[length++] = wc;
  2468. arg += count;
  2469. }
  2470. }
  2471. if (characters < width && (dp->flags & FLAG_LEFT))
  2472. {
  2473. size_t n = width - characters;
  2474. ENSURE_ALLOCATION (xsum (length, n));
  2475. DCHAR_SET (result + length, ' ', n);
  2476. length += n;
  2477. }
  2478. }
  2479. # else
  2480. /* %ls in vasnprintf. See the specification of fprintf. */
  2481. {
  2482. const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
  2483. const wchar_t *arg_end;
  2484. size_t characters;
  2485. # if !DCHAR_IS_TCHAR
  2486. /* This code assumes that TCHAR_T is 'char'. */
  2487. verify (sizeof (TCHAR_T) == 1);
  2488. TCHAR_T *tmpsrc;
  2489. DCHAR_T *tmpdst;
  2490. size_t tmpdst_len;
  2491. # endif
  2492. size_t w;
  2493. if (has_precision)
  2494. {
  2495. /* Use only as many wide characters as needed to produce
  2496. at most PRECISION bytes, from the left. */
  2497. # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
  2498. mbstate_t state;
  2499. memset (&state, '\0', sizeof (mbstate_t));
  2500. # endif
  2501. arg_end = arg;
  2502. characters = 0;
  2503. while (precision > 0)
  2504. {
  2505. char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
  2506. int count;
  2507. if (*arg_end == 0)
  2508. /* Found the terminating null wide character. */
  2509. break;
  2510. count = local_wcrtomb (cbuf, *arg_end, &state);
  2511. if (count < 0)
  2512. /* Cannot convert. */
  2513. goto fail_with_EILSEQ;
  2514. if (precision < (unsigned int) count)
  2515. break;
  2516. arg_end++;
  2517. characters += count;
  2518. precision -= count;
  2519. }
  2520. }
  2521. # if DCHAR_IS_TCHAR
  2522. else if (has_width)
  2523. # else
  2524. else
  2525. # endif
  2526. {
  2527. /* Use the entire string, and count the number of
  2528. bytes. */
  2529. # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
  2530. mbstate_t state;
  2531. memset (&state, '\0', sizeof (mbstate_t));
  2532. # endif
  2533. arg_end = arg;
  2534. characters = 0;
  2535. for (;;)
  2536. {
  2537. char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
  2538. int count;
  2539. if (*arg_end == 0)
  2540. /* Found the terminating null wide character. */
  2541. break;
  2542. count = local_wcrtomb (cbuf, *arg_end, &state);
  2543. if (count < 0)
  2544. /* Cannot convert. */
  2545. goto fail_with_EILSEQ;
  2546. arg_end++;
  2547. characters += count;
  2548. }
  2549. }
  2550. # if DCHAR_IS_TCHAR
  2551. else
  2552. {
  2553. /* Use the entire string. */
  2554. arg_end = arg + local_wcslen (arg);
  2555. /* The number of bytes doesn't matter. */
  2556. characters = 0;
  2557. }
  2558. # endif
  2559. # if !DCHAR_IS_TCHAR
  2560. /* Convert the string into a piece of temporary memory. */
  2561. tmpsrc = (TCHAR_T *) malloc (characters * sizeof (TCHAR_T));
  2562. if (tmpsrc == NULL)
  2563. goto out_of_memory;
  2564. {
  2565. TCHAR_T *tmpptr = tmpsrc;
  2566. size_t remaining;
  2567. # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
  2568. mbstate_t state;
  2569. memset (&state, '\0', sizeof (mbstate_t));
  2570. # endif
  2571. for (remaining = characters; remaining > 0; )
  2572. {
  2573. char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
  2574. int count;
  2575. if (*arg == 0)
  2576. abort ();
  2577. count = local_wcrtomb (cbuf, *arg, &state);
  2578. if (count <= 0)
  2579. /* Inconsistency. */
  2580. abort ();
  2581. memcpy (tmpptr, cbuf, count);
  2582. tmpptr += count;
  2583. arg++;
  2584. remaining -= count;
  2585. }
  2586. if (!(arg == arg_end))
  2587. abort ();
  2588. }
  2589. /* Convert from TCHAR_T[] to DCHAR_T[]. */
  2590. tmpdst =
  2591. DCHAR_CONV_FROM_ENCODING (locale_charset (),
  2592. iconveh_question_mark,
  2593. tmpsrc, characters,
  2594. NULL,
  2595. NULL, &tmpdst_len);
  2596. if (tmpdst == NULL)
  2597. {
  2598. free (tmpsrc);
  2599. goto fail_with_errno;
  2600. }
  2601. free (tmpsrc);
  2602. # endif
  2603. if (has_width)
  2604. {
  2605. # if ENABLE_UNISTDIO
  2606. /* Outside POSIX, it's preferable to compare the width
  2607. against the number of _characters_ of the converted
  2608. value. */
  2609. w = DCHAR_MBSNLEN (result + length, characters);
  2610. # else
  2611. /* The width is compared against the number of _bytes_
  2612. of the converted value, says POSIX. */
  2613. w = characters;
  2614. # endif
  2615. }
  2616. else
  2617. /* w doesn't matter. */
  2618. w = 0;
  2619. if (w < width && !(dp->flags & FLAG_LEFT))
  2620. {
  2621. size_t n = width - w;
  2622. ENSURE_ALLOCATION (xsum (length, n));
  2623. DCHAR_SET (result + length, ' ', n);
  2624. length += n;
  2625. }
  2626. # if DCHAR_IS_TCHAR
  2627. if (has_precision || has_width)
  2628. {
  2629. /* We know the number of bytes in advance. */
  2630. size_t remaining;
  2631. # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
  2632. mbstate_t state;
  2633. memset (&state, '\0', sizeof (mbstate_t));
  2634. # endif
  2635. ENSURE_ALLOCATION (xsum (length, characters));
  2636. for (remaining = characters; remaining > 0; )
  2637. {
  2638. char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
  2639. int count;
  2640. if (*arg == 0)
  2641. abort ();
  2642. count = local_wcrtomb (cbuf, *arg, &state);
  2643. if (count <= 0)
  2644. /* Inconsistency. */
  2645. abort ();
  2646. memcpy (result + length, cbuf, count);
  2647. length += count;
  2648. arg++;
  2649. remaining -= count;
  2650. }
  2651. if (!(arg == arg_end))
  2652. abort ();
  2653. }
  2654. else
  2655. {
  2656. # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
  2657. mbstate_t state;
  2658. memset (&state, '\0', sizeof (mbstate_t));
  2659. # endif
  2660. while (arg < arg_end)
  2661. {
  2662. char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
  2663. int count;
  2664. if (*arg == 0)
  2665. abort ();
  2666. count = local_wcrtomb (cbuf, *arg, &state);
  2667. if (count <= 0)
  2668. /* Cannot convert. */
  2669. goto fail_with_EILSEQ;
  2670. ENSURE_ALLOCATION (xsum (length, count));
  2671. memcpy (result + length, cbuf, count);
  2672. length += count;
  2673. arg++;
  2674. }
  2675. }
  2676. # else
  2677. ENSURE_ALLOCATION_ELSE (xsum (length, tmpdst_len),
  2678. { free (tmpdst); goto out_of_memory; });
  2679. DCHAR_CPY (result + length, tmpdst, tmpdst_len);
  2680. free (tmpdst);
  2681. length += tmpdst_len;
  2682. # endif
  2683. if (w < width && (dp->flags & FLAG_LEFT))
  2684. {
  2685. size_t n = width - w;
  2686. ENSURE_ALLOCATION (xsum (length, n));
  2687. DCHAR_SET (result + length, ' ', n);
  2688. length += n;
  2689. }
  2690. }
  2691. # endif
  2692. }
  2693. #endif
  2694. #if ENABLE_WCHAR_FALLBACK && HAVE_WINT_T && !WIDE_CHAR_VERSION
  2695. else if (dp->conversion == 'c'
  2696. && a.arg[dp->arg_index].type == TYPE_WIDE_CHAR)
  2697. {
  2698. /* Implement the 'lc' directive ourselves, in order to provide
  2699. the fallback that avoids EILSEQ. */
  2700. int flags = dp->flags;
  2701. int has_width;
  2702. size_t width;
  2703. has_width = 0;
  2704. width = 0;
  2705. if (dp->width_start != dp->width_end)
  2706. {
  2707. if (dp->width_arg_index != ARG_NONE)
  2708. {
  2709. int arg;
  2710. if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
  2711. abort ();
  2712. arg = a.arg[dp->width_arg_index].a.a_int;
  2713. width = arg;
  2714. if (arg < 0)
  2715. {
  2716. /* "A negative field width is taken as a '-' flag
  2717. followed by a positive field width." */
  2718. flags |= FLAG_LEFT;
  2719. width = -width;
  2720. }
  2721. }
  2722. else
  2723. {
  2724. const FCHAR_T *digitp = dp->width_start;
  2725. do
  2726. width = xsum (xtimes (width, 10), *digitp++ - '0');
  2727. while (digitp != dp->width_end);
  2728. }
  2729. has_width = 1;
  2730. }
  2731. /* %lc in vasnprintf. See the specification of fprintf. */
  2732. {
  2733. wchar_t arg = (wchar_t) a.arg[dp->arg_index].a.a_wide_char;
  2734. size_t characters;
  2735. # if !DCHAR_IS_TCHAR
  2736. /* This code assumes that TCHAR_T is 'char'. */
  2737. verify (sizeof (TCHAR_T) == 1);
  2738. TCHAR_T tmpsrc[64]; /* Assume MB_CUR_MAX <= 64. */
  2739. DCHAR_T *tmpdst;
  2740. size_t tmpdst_len;
  2741. # endif
  2742. size_t w;
  2743. # if DCHAR_IS_TCHAR
  2744. if (has_width)
  2745. # endif
  2746. {
  2747. /* Count the number of bytes. */
  2748. characters = 0;
  2749. if (arg != 0)
  2750. {
  2751. char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
  2752. int count;
  2753. # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
  2754. mbstate_t state;
  2755. memset (&state, '\0', sizeof (mbstate_t));
  2756. # endif
  2757. count = local_wcrtomb (cbuf, arg, &state);
  2758. if (count < 0)
  2759. /* Inconsistency. */
  2760. abort ();
  2761. characters = count;
  2762. }
  2763. }
  2764. # if DCHAR_IS_TCHAR
  2765. else
  2766. {
  2767. /* The number of bytes doesn't matter. */
  2768. characters = 0;
  2769. }
  2770. # endif
  2771. # if !DCHAR_IS_TCHAR
  2772. /* Convert the string into a piece of temporary memory. */
  2773. if (characters > 0) /* implies arg != 0 */
  2774. {
  2775. char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
  2776. int count;
  2777. # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
  2778. mbstate_t state;
  2779. memset (&state, '\0', sizeof (mbstate_t));
  2780. # endif
  2781. count = local_wcrtomb (cbuf, arg, &state);
  2782. if (count <= 0)
  2783. /* Inconsistency. */
  2784. abort ();
  2785. memcpy (tmpsrc, cbuf, count);
  2786. }
  2787. /* Convert from TCHAR_T[] to DCHAR_T[]. */
  2788. tmpdst =
  2789. DCHAR_CONV_FROM_ENCODING (locale_charset (),
  2790. iconveh_question_mark,
  2791. tmpsrc, characters,
  2792. NULL,
  2793. NULL, &tmpdst_len);
  2794. if (tmpdst == NULL)
  2795. goto fail_with_errno;
  2796. # endif
  2797. if (has_width)
  2798. {
  2799. # if ENABLE_UNISTDIO
  2800. /* Outside POSIX, it's preferable to compare the width
  2801. against the number of _characters_ of the converted
  2802. value. */
  2803. w = DCHAR_MBSNLEN (result + length, characters);
  2804. # else
  2805. /* The width is compared against the number of _bytes_
  2806. of the converted value, says POSIX. */
  2807. w = characters;
  2808. # endif
  2809. }
  2810. else
  2811. /* w doesn't matter. */
  2812. w = 0;
  2813. if (w < width && !(dp->flags & FLAG_LEFT))
  2814. {
  2815. size_t n = width - w;
  2816. ENSURE_ALLOCATION (xsum (length, n));
  2817. DCHAR_SET (result + length, ' ', n);
  2818. length += n;
  2819. }
  2820. # if DCHAR_IS_TCHAR
  2821. if (has_width)
  2822. {
  2823. /* We know the number of bytes in advance. */
  2824. ENSURE_ALLOCATION (xsum (length, characters));
  2825. if (characters > 0) /* implies arg != 0 */
  2826. {
  2827. int count;
  2828. # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
  2829. mbstate_t state;
  2830. memset (&state, '\0', sizeof (mbstate_t));
  2831. # endif
  2832. count = local_wcrtomb (result + length, arg, &state);
  2833. if (count <= 0)
  2834. /* Inconsistency. */
  2835. abort ();
  2836. length += count;
  2837. }
  2838. }
  2839. else
  2840. {
  2841. if (arg != 0)
  2842. {
  2843. char cbuf[64]; /* Assume MB_CUR_MAX <= 64. */
  2844. int count;
  2845. # if HAVE_WCRTOMB && !defined GNULIB_defined_mbstate_t
  2846. mbstate_t state;
  2847. memset (&state, '\0', sizeof (mbstate_t));
  2848. # endif
  2849. count = local_wcrtomb (cbuf, arg, &state);
  2850. if (count <= 0)
  2851. /* Inconsistency. */
  2852. abort ();
  2853. ENSURE_ALLOCATION (xsum (length, count));
  2854. memcpy (result + length, cbuf, count);
  2855. length += count;
  2856. }
  2857. }
  2858. # else
  2859. ENSURE_ALLOCATION_ELSE (xsum (length, tmpdst_len),
  2860. { free (tmpdst); goto out_of_memory; });
  2861. DCHAR_CPY (result + length, tmpdst, tmpdst_len);
  2862. free (tmpdst);
  2863. length += tmpdst_len;
  2864. # endif
  2865. if (w < width && (dp->flags & FLAG_LEFT))
  2866. {
  2867. size_t n = width - w;
  2868. ENSURE_ALLOCATION (xsum (length, n));
  2869. DCHAR_SET (result + length, ' ', n);
  2870. length += n;
  2871. }
  2872. }
  2873. }
  2874. #endif
  2875. #if (NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_DOUBLE) && !defined IN_LIBINTL
  2876. else if ((dp->conversion == 'a' || dp->conversion == 'A')
  2877. # if !(NEED_PRINTF_DIRECTIVE_A || (NEED_PRINTF_LONG_DOUBLE && NEED_PRINTF_DOUBLE))
  2878. && (0
  2879. # if NEED_PRINTF_DOUBLE
  2880. || a.arg[dp->arg_index].type == TYPE_DOUBLE
  2881. # endif
  2882. # if NEED_PRINTF_LONG_DOUBLE
  2883. || a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
  2884. # endif
  2885. )
  2886. # endif
  2887. )
  2888. {
  2889. arg_type type = a.arg[dp->arg_index].type;
  2890. int flags = dp->flags;
  2891. size_t width;
  2892. int has_precision;
  2893. size_t precision;
  2894. size_t tmp_length;
  2895. size_t count;
  2896. DCHAR_T tmpbuf[700];
  2897. DCHAR_T *tmp;
  2898. DCHAR_T *pad_ptr;
  2899. DCHAR_T *p;
  2900. width = 0;
  2901. if (dp->width_start != dp->width_end)
  2902. {
  2903. if (dp->width_arg_index != ARG_NONE)
  2904. {
  2905. int arg;
  2906. if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
  2907. abort ();
  2908. arg = a.arg[dp->width_arg_index].a.a_int;
  2909. width = arg;
  2910. if (arg < 0)
  2911. {
  2912. /* "A negative field width is taken as a '-' flag
  2913. followed by a positive field width." */
  2914. flags |= FLAG_LEFT;
  2915. width = -width;
  2916. }
  2917. }
  2918. else
  2919. {
  2920. const FCHAR_T *digitp = dp->width_start;
  2921. do
  2922. width = xsum (xtimes (width, 10), *digitp++ - '0');
  2923. while (digitp != dp->width_end);
  2924. }
  2925. }
  2926. has_precision = 0;
  2927. precision = 0;
  2928. if (dp->precision_start != dp->precision_end)
  2929. {
  2930. if (dp->precision_arg_index != ARG_NONE)
  2931. {
  2932. int arg;
  2933. if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
  2934. abort ();
  2935. arg = a.arg[dp->precision_arg_index].a.a_int;
  2936. /* "A negative precision is taken as if the precision
  2937. were omitted." */
  2938. if (arg >= 0)
  2939. {
  2940. precision = arg;
  2941. has_precision = 1;
  2942. }
  2943. }
  2944. else
  2945. {
  2946. const FCHAR_T *digitp = dp->precision_start + 1;
  2947. precision = 0;
  2948. while (digitp != dp->precision_end)
  2949. precision = xsum (xtimes (precision, 10), *digitp++ - '0');
  2950. has_precision = 1;
  2951. }
  2952. }
  2953. /* Allocate a temporary buffer of sufficient size. */
  2954. if (type == TYPE_LONGDOUBLE)
  2955. tmp_length =
  2956. (unsigned int) ((LDBL_DIG + 1)
  2957. * 0.831 /* decimal -> hexadecimal */
  2958. )
  2959. + 1; /* turn floor into ceil */
  2960. else
  2961. tmp_length =
  2962. (unsigned int) ((DBL_DIG + 1)
  2963. * 0.831 /* decimal -> hexadecimal */
  2964. )
  2965. + 1; /* turn floor into ceil */
  2966. if (tmp_length < precision)
  2967. tmp_length = precision;
  2968. /* Account for sign, decimal point etc. */
  2969. tmp_length = xsum (tmp_length, 12);
  2970. if (tmp_length < width)
  2971. tmp_length = width;
  2972. tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
  2973. if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
  2974. tmp = tmpbuf;
  2975. else
  2976. {
  2977. size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
  2978. if (size_overflow_p (tmp_memsize))
  2979. /* Overflow, would lead to out of memory. */
  2980. goto out_of_memory;
  2981. tmp = (DCHAR_T *) malloc (tmp_memsize);
  2982. if (tmp == NULL)
  2983. /* Out of memory. */
  2984. goto out_of_memory;
  2985. }
  2986. pad_ptr = NULL;
  2987. p = tmp;
  2988. if (type == TYPE_LONGDOUBLE)
  2989. {
  2990. # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_LONG_DOUBLE
  2991. long double arg = a.arg[dp->arg_index].a.a_longdouble;
  2992. if (isnanl (arg))
  2993. {
  2994. if (dp->conversion == 'A')
  2995. {
  2996. *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
  2997. }
  2998. else
  2999. {
  3000. *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
  3001. }
  3002. }
  3003. else
  3004. {
  3005. int sign = 0;
  3006. DECL_LONG_DOUBLE_ROUNDING
  3007. BEGIN_LONG_DOUBLE_ROUNDING ();
  3008. if (signbit (arg)) /* arg < 0.0L or negative zero */
  3009. {
  3010. sign = -1;
  3011. arg = -arg;
  3012. }
  3013. if (sign < 0)
  3014. *p++ = '-';
  3015. else if (flags & FLAG_SHOWSIGN)
  3016. *p++ = '+';
  3017. else if (flags & FLAG_SPACE)
  3018. *p++ = ' ';
  3019. if (arg > 0.0L && arg + arg == arg)
  3020. {
  3021. if (dp->conversion == 'A')
  3022. {
  3023. *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
  3024. }
  3025. else
  3026. {
  3027. *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
  3028. }
  3029. }
  3030. else
  3031. {
  3032. int exponent;
  3033. long double mantissa;
  3034. if (arg > 0.0L)
  3035. mantissa = printf_frexpl (arg, &exponent);
  3036. else
  3037. {
  3038. exponent = 0;
  3039. mantissa = 0.0L;
  3040. }
  3041. if (has_precision
  3042. && precision < (unsigned int) ((LDBL_DIG + 1) * 0.831) + 1)
  3043. {
  3044. /* Round the mantissa. */
  3045. long double tail = mantissa;
  3046. size_t q;
  3047. for (q = precision; ; q--)
  3048. {
  3049. int digit = (int) tail;
  3050. tail -= digit;
  3051. if (q == 0)
  3052. {
  3053. if (digit & 1 ? tail >= 0.5L : tail > 0.5L)
  3054. tail = 1 - tail;
  3055. else
  3056. tail = - tail;
  3057. break;
  3058. }
  3059. tail *= 16.0L;
  3060. }
  3061. if (tail != 0.0L)
  3062. for (q = precision; q > 0; q--)
  3063. tail *= 0.0625L;
  3064. mantissa += tail;
  3065. }
  3066. *p++ = '0';
  3067. *p++ = dp->conversion - 'A' + 'X';
  3068. pad_ptr = p;
  3069. {
  3070. int digit;
  3071. digit = (int) mantissa;
  3072. mantissa -= digit;
  3073. *p++ = '0' + digit;
  3074. if ((flags & FLAG_ALT)
  3075. || mantissa > 0.0L || precision > 0)
  3076. {
  3077. *p++ = decimal_point_char ();
  3078. /* This loop terminates because we assume
  3079. that FLT_RADIX is a power of 2. */
  3080. while (mantissa > 0.0L)
  3081. {
  3082. mantissa *= 16.0L;
  3083. digit = (int) mantissa;
  3084. mantissa -= digit;
  3085. *p++ = digit
  3086. + (digit < 10
  3087. ? '0'
  3088. : dp->conversion - 10);
  3089. if (precision > 0)
  3090. precision--;
  3091. }
  3092. while (precision > 0)
  3093. {
  3094. *p++ = '0';
  3095. precision--;
  3096. }
  3097. }
  3098. }
  3099. *p++ = dp->conversion - 'A' + 'P';
  3100. # if WIDE_CHAR_VERSION
  3101. {
  3102. static const wchar_t decimal_format[] =
  3103. { '%', '+', 'd', '\0' };
  3104. SNPRINTF (p, 6 + 1, decimal_format, exponent);
  3105. }
  3106. while (*p != '\0')
  3107. p++;
  3108. # else
  3109. if (sizeof (DCHAR_T) == 1)
  3110. {
  3111. sprintf ((char *) p, "%+d", exponent);
  3112. while (*p != '\0')
  3113. p++;
  3114. }
  3115. else
  3116. {
  3117. char expbuf[6 + 1];
  3118. const char *ep;
  3119. sprintf (expbuf, "%+d", exponent);
  3120. for (ep = expbuf; (*p = *ep) != '\0'; ep++)
  3121. p++;
  3122. }
  3123. # endif
  3124. }
  3125. END_LONG_DOUBLE_ROUNDING ();
  3126. }
  3127. # else
  3128. abort ();
  3129. # endif
  3130. }
  3131. else
  3132. {
  3133. # if NEED_PRINTF_DIRECTIVE_A || NEED_PRINTF_DOUBLE
  3134. double arg = a.arg[dp->arg_index].a.a_double;
  3135. if (isnand (arg))
  3136. {
  3137. if (dp->conversion == 'A')
  3138. {
  3139. *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
  3140. }
  3141. else
  3142. {
  3143. *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
  3144. }
  3145. }
  3146. else
  3147. {
  3148. int sign = 0;
  3149. if (signbit (arg)) /* arg < 0.0 or negative zero */
  3150. {
  3151. sign = -1;
  3152. arg = -arg;
  3153. }
  3154. if (sign < 0)
  3155. *p++ = '-';
  3156. else if (flags & FLAG_SHOWSIGN)
  3157. *p++ = '+';
  3158. else if (flags & FLAG_SPACE)
  3159. *p++ = ' ';
  3160. if (arg > 0.0 && arg + arg == arg)
  3161. {
  3162. if (dp->conversion == 'A')
  3163. {
  3164. *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
  3165. }
  3166. else
  3167. {
  3168. *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
  3169. }
  3170. }
  3171. else
  3172. {
  3173. int exponent;
  3174. double mantissa;
  3175. if (arg > 0.0)
  3176. mantissa = printf_frexp (arg, &exponent);
  3177. else
  3178. {
  3179. exponent = 0;
  3180. mantissa = 0.0;
  3181. }
  3182. if (has_precision
  3183. && precision < (unsigned int) ((DBL_DIG + 1) * 0.831) + 1)
  3184. {
  3185. /* Round the mantissa. */
  3186. double tail = mantissa;
  3187. size_t q;
  3188. for (q = precision; ; q--)
  3189. {
  3190. int digit = (int) tail;
  3191. tail -= digit;
  3192. if (q == 0)
  3193. {
  3194. if (digit & 1 ? tail >= 0.5 : tail > 0.5)
  3195. tail = 1 - tail;
  3196. else
  3197. tail = - tail;
  3198. break;
  3199. }
  3200. tail *= 16.0;
  3201. }
  3202. if (tail != 0.0)
  3203. for (q = precision; q > 0; q--)
  3204. tail *= 0.0625;
  3205. mantissa += tail;
  3206. }
  3207. *p++ = '0';
  3208. *p++ = dp->conversion - 'A' + 'X';
  3209. pad_ptr = p;
  3210. {
  3211. int digit;
  3212. digit = (int) mantissa;
  3213. mantissa -= digit;
  3214. *p++ = '0' + digit;
  3215. if ((flags & FLAG_ALT)
  3216. || mantissa > 0.0 || precision > 0)
  3217. {
  3218. *p++ = decimal_point_char ();
  3219. /* This loop terminates because we assume
  3220. that FLT_RADIX is a power of 2. */
  3221. while (mantissa > 0.0)
  3222. {
  3223. mantissa *= 16.0;
  3224. digit = (int) mantissa;
  3225. mantissa -= digit;
  3226. *p++ = digit
  3227. + (digit < 10
  3228. ? '0'
  3229. : dp->conversion - 10);
  3230. if (precision > 0)
  3231. precision--;
  3232. }
  3233. while (precision > 0)
  3234. {
  3235. *p++ = '0';
  3236. precision--;
  3237. }
  3238. }
  3239. }
  3240. *p++ = dp->conversion - 'A' + 'P';
  3241. # if WIDE_CHAR_VERSION
  3242. {
  3243. static const wchar_t decimal_format[] =
  3244. { '%', '+', 'd', '\0' };
  3245. SNPRINTF (p, 6 + 1, decimal_format, exponent);
  3246. }
  3247. while (*p != '\0')
  3248. p++;
  3249. # else
  3250. if (sizeof (DCHAR_T) == 1)
  3251. {
  3252. sprintf ((char *) p, "%+d", exponent);
  3253. while (*p != '\0')
  3254. p++;
  3255. }
  3256. else
  3257. {
  3258. char expbuf[6 + 1];
  3259. const char *ep;
  3260. sprintf (expbuf, "%+d", exponent);
  3261. for (ep = expbuf; (*p = *ep) != '\0'; ep++)
  3262. p++;
  3263. }
  3264. # endif
  3265. }
  3266. }
  3267. # else
  3268. abort ();
  3269. # endif
  3270. }
  3271. /* The generated string now extends from tmp to p, with the
  3272. zero padding insertion point being at pad_ptr. */
  3273. count = p - tmp;
  3274. if (count < width)
  3275. {
  3276. size_t pad = width - count;
  3277. DCHAR_T *end = p + pad;
  3278. if (flags & FLAG_LEFT)
  3279. {
  3280. /* Pad with spaces on the right. */
  3281. for (; pad > 0; pad--)
  3282. *p++ = ' ';
  3283. }
  3284. else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
  3285. {
  3286. /* Pad with zeroes. */
  3287. DCHAR_T *q = end;
  3288. while (p > pad_ptr)
  3289. *--q = *--p;
  3290. for (; pad > 0; pad--)
  3291. *p++ = '0';
  3292. }
  3293. else
  3294. {
  3295. /* Pad with spaces on the left. */
  3296. DCHAR_T *q = end;
  3297. while (p > tmp)
  3298. *--q = *--p;
  3299. for (; pad > 0; pad--)
  3300. *p++ = ' ';
  3301. }
  3302. p = end;
  3303. }
  3304. count = p - tmp;
  3305. if (count >= tmp_length)
  3306. /* tmp_length was incorrectly calculated - fix the
  3307. code above! */
  3308. abort ();
  3309. /* Make room for the result. */
  3310. if (count >= allocated - length)
  3311. {
  3312. size_t n = xsum (length, count);
  3313. ENSURE_ALLOCATION (n);
  3314. }
  3315. /* Append the result. */
  3316. memcpy (result + length, tmp, count * sizeof (DCHAR_T));
  3317. if (tmp != tmpbuf)
  3318. free (tmp);
  3319. length += count;
  3320. }
  3321. #endif
  3322. #if (NEED_PRINTF_INFINITE_DOUBLE || NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE || NEED_PRINTF_LONG_DOUBLE) && !defined IN_LIBINTL
  3323. else if ((dp->conversion == 'f' || dp->conversion == 'F'
  3324. || dp->conversion == 'e' || dp->conversion == 'E'
  3325. || dp->conversion == 'g' || dp->conversion == 'G'
  3326. || dp->conversion == 'a' || dp->conversion == 'A')
  3327. && (0
  3328. # if NEED_PRINTF_DOUBLE
  3329. || a.arg[dp->arg_index].type == TYPE_DOUBLE
  3330. # elif NEED_PRINTF_INFINITE_DOUBLE
  3331. || (a.arg[dp->arg_index].type == TYPE_DOUBLE
  3332. /* The systems (mingw) which produce wrong output
  3333. for Inf, -Inf, and NaN also do so for -0.0.
  3334. Therefore we treat this case here as well. */
  3335. && is_infinite_or_zero (a.arg[dp->arg_index].a.a_double))
  3336. # endif
  3337. # if NEED_PRINTF_LONG_DOUBLE
  3338. || a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
  3339. # elif NEED_PRINTF_INFINITE_LONG_DOUBLE
  3340. || (a.arg[dp->arg_index].type == TYPE_LONGDOUBLE
  3341. /* Some systems produce wrong output for Inf,
  3342. -Inf, and NaN. Some systems in this category
  3343. (IRIX 5.3) also do so for -0.0. Therefore we
  3344. treat this case here as well. */
  3345. && is_infinite_or_zerol (a.arg[dp->arg_index].a.a_longdouble))
  3346. # endif
  3347. ))
  3348. {
  3349. # if (NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE) && (NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE)
  3350. arg_type type = a.arg[dp->arg_index].type;
  3351. # endif
  3352. int flags = dp->flags;
  3353. size_t width;
  3354. size_t count;
  3355. int has_precision;
  3356. size_t precision;
  3357. size_t tmp_length;
  3358. DCHAR_T tmpbuf[700];
  3359. DCHAR_T *tmp;
  3360. DCHAR_T *pad_ptr;
  3361. DCHAR_T *p;
  3362. width = 0;
  3363. if (dp->width_start != dp->width_end)
  3364. {
  3365. if (dp->width_arg_index != ARG_NONE)
  3366. {
  3367. int arg;
  3368. if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
  3369. abort ();
  3370. arg = a.arg[dp->width_arg_index].a.a_int;
  3371. width = arg;
  3372. if (arg < 0)
  3373. {
  3374. /* "A negative field width is taken as a '-' flag
  3375. followed by a positive field width." */
  3376. flags |= FLAG_LEFT;
  3377. width = -width;
  3378. }
  3379. }
  3380. else
  3381. {
  3382. const FCHAR_T *digitp = dp->width_start;
  3383. do
  3384. width = xsum (xtimes (width, 10), *digitp++ - '0');
  3385. while (digitp != dp->width_end);
  3386. }
  3387. }
  3388. has_precision = 0;
  3389. precision = 0;
  3390. if (dp->precision_start != dp->precision_end)
  3391. {
  3392. if (dp->precision_arg_index != ARG_NONE)
  3393. {
  3394. int arg;
  3395. if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
  3396. abort ();
  3397. arg = a.arg[dp->precision_arg_index].a.a_int;
  3398. /* "A negative precision is taken as if the precision
  3399. were omitted." */
  3400. if (arg >= 0)
  3401. {
  3402. precision = arg;
  3403. has_precision = 1;
  3404. }
  3405. }
  3406. else
  3407. {
  3408. const FCHAR_T *digitp = dp->precision_start + 1;
  3409. precision = 0;
  3410. while (digitp != dp->precision_end)
  3411. precision = xsum (xtimes (precision, 10), *digitp++ - '0');
  3412. has_precision = 1;
  3413. }
  3414. }
  3415. /* POSIX specifies the default precision to be 6 for %f, %F,
  3416. %e, %E, but not for %g, %G. Implementations appear to use
  3417. the same default precision also for %g, %G. But for %a, %A,
  3418. the default precision is 0. */
  3419. if (!has_precision)
  3420. if (!(dp->conversion == 'a' || dp->conversion == 'A'))
  3421. precision = 6;
  3422. /* Allocate a temporary buffer of sufficient size. */
  3423. # if NEED_PRINTF_DOUBLE && NEED_PRINTF_LONG_DOUBLE
  3424. tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : DBL_DIG + 1);
  3425. # elif NEED_PRINTF_INFINITE_DOUBLE && NEED_PRINTF_LONG_DOUBLE
  3426. tmp_length = (type == TYPE_LONGDOUBLE ? LDBL_DIG + 1 : 0);
  3427. # elif NEED_PRINTF_LONG_DOUBLE
  3428. tmp_length = LDBL_DIG + 1;
  3429. # elif NEED_PRINTF_DOUBLE
  3430. tmp_length = DBL_DIG + 1;
  3431. # else
  3432. tmp_length = 0;
  3433. # endif
  3434. if (tmp_length < precision)
  3435. tmp_length = precision;
  3436. # if NEED_PRINTF_LONG_DOUBLE
  3437. # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
  3438. if (type == TYPE_LONGDOUBLE)
  3439. # endif
  3440. if (dp->conversion == 'f' || dp->conversion == 'F')
  3441. {
  3442. long double arg = a.arg[dp->arg_index].a.a_longdouble;
  3443. if (!(isnanl (arg) || arg + arg == arg))
  3444. {
  3445. /* arg is finite and nonzero. */
  3446. int exponent = floorlog10l (arg < 0 ? -arg : arg);
  3447. if (exponent >= 0 && tmp_length < exponent + precision)
  3448. tmp_length = exponent + precision;
  3449. }
  3450. }
  3451. # endif
  3452. # if NEED_PRINTF_DOUBLE
  3453. # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
  3454. if (type == TYPE_DOUBLE)
  3455. # endif
  3456. if (dp->conversion == 'f' || dp->conversion == 'F')
  3457. {
  3458. double arg = a.arg[dp->arg_index].a.a_double;
  3459. if (!(isnand (arg) || arg + arg == arg))
  3460. {
  3461. /* arg is finite and nonzero. */
  3462. int exponent = floorlog10 (arg < 0 ? -arg : arg);
  3463. if (exponent >= 0 && tmp_length < exponent + precision)
  3464. tmp_length = exponent + precision;
  3465. }
  3466. }
  3467. # endif
  3468. /* Account for sign, decimal point etc. */
  3469. tmp_length = xsum (tmp_length, 12);
  3470. if (tmp_length < width)
  3471. tmp_length = width;
  3472. tmp_length = xsum (tmp_length, 1); /* account for trailing NUL */
  3473. if (tmp_length <= sizeof (tmpbuf) / sizeof (DCHAR_T))
  3474. tmp = tmpbuf;
  3475. else
  3476. {
  3477. size_t tmp_memsize = xtimes (tmp_length, sizeof (DCHAR_T));
  3478. if (size_overflow_p (tmp_memsize))
  3479. /* Overflow, would lead to out of memory. */
  3480. goto out_of_memory;
  3481. tmp = (DCHAR_T *) malloc (tmp_memsize);
  3482. if (tmp == NULL)
  3483. /* Out of memory. */
  3484. goto out_of_memory;
  3485. }
  3486. pad_ptr = NULL;
  3487. p = tmp;
  3488. # if NEED_PRINTF_LONG_DOUBLE || NEED_PRINTF_INFINITE_LONG_DOUBLE
  3489. # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
  3490. if (type == TYPE_LONGDOUBLE)
  3491. # endif
  3492. {
  3493. long double arg = a.arg[dp->arg_index].a.a_longdouble;
  3494. if (isnanl (arg))
  3495. {
  3496. if (dp->conversion >= 'A' && dp->conversion <= 'Z')
  3497. {
  3498. *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
  3499. }
  3500. else
  3501. {
  3502. *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
  3503. }
  3504. }
  3505. else
  3506. {
  3507. int sign = 0;
  3508. DECL_LONG_DOUBLE_ROUNDING
  3509. BEGIN_LONG_DOUBLE_ROUNDING ();
  3510. if (signbit (arg)) /* arg < 0.0L or negative zero */
  3511. {
  3512. sign = -1;
  3513. arg = -arg;
  3514. }
  3515. if (sign < 0)
  3516. *p++ = '-';
  3517. else if (flags & FLAG_SHOWSIGN)
  3518. *p++ = '+';
  3519. else if (flags & FLAG_SPACE)
  3520. *p++ = ' ';
  3521. if (arg > 0.0L && arg + arg == arg)
  3522. {
  3523. if (dp->conversion >= 'A' && dp->conversion <= 'Z')
  3524. {
  3525. *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
  3526. }
  3527. else
  3528. {
  3529. *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
  3530. }
  3531. }
  3532. else
  3533. {
  3534. # if NEED_PRINTF_LONG_DOUBLE
  3535. pad_ptr = p;
  3536. if (dp->conversion == 'f' || dp->conversion == 'F')
  3537. {
  3538. char *digits;
  3539. size_t ndigits;
  3540. digits =
  3541. scale10_round_decimal_long_double (arg, precision);
  3542. if (digits == NULL)
  3543. {
  3544. END_LONG_DOUBLE_ROUNDING ();
  3545. goto out_of_memory;
  3546. }
  3547. ndigits = strlen (digits);
  3548. if (ndigits > precision)
  3549. do
  3550. {
  3551. --ndigits;
  3552. *p++ = digits[ndigits];
  3553. }
  3554. while (ndigits > precision);
  3555. else
  3556. *p++ = '0';
  3557. /* Here ndigits <= precision. */
  3558. if ((flags & FLAG_ALT) || precision > 0)
  3559. {
  3560. *p++ = decimal_point_char ();
  3561. for (; precision > ndigits; precision--)
  3562. *p++ = '0';
  3563. while (ndigits > 0)
  3564. {
  3565. --ndigits;
  3566. *p++ = digits[ndigits];
  3567. }
  3568. }
  3569. free (digits);
  3570. }
  3571. else if (dp->conversion == 'e' || dp->conversion == 'E')
  3572. {
  3573. int exponent;
  3574. if (arg == 0.0L)
  3575. {
  3576. exponent = 0;
  3577. *p++ = '0';
  3578. if ((flags & FLAG_ALT) || precision > 0)
  3579. {
  3580. *p++ = decimal_point_char ();
  3581. for (; precision > 0; precision--)
  3582. *p++ = '0';
  3583. }
  3584. }
  3585. else
  3586. {
  3587. /* arg > 0.0L. */
  3588. int adjusted;
  3589. char *digits;
  3590. size_t ndigits;
  3591. exponent = floorlog10l (arg);
  3592. adjusted = 0;
  3593. for (;;)
  3594. {
  3595. digits =
  3596. scale10_round_decimal_long_double (arg,
  3597. (int)precision - exponent);
  3598. if (digits == NULL)
  3599. {
  3600. END_LONG_DOUBLE_ROUNDING ();
  3601. goto out_of_memory;
  3602. }
  3603. ndigits = strlen (digits);
  3604. if (ndigits == precision + 1)
  3605. break;
  3606. if (ndigits < precision
  3607. || ndigits > precision + 2)
  3608. /* The exponent was not guessed
  3609. precisely enough. */
  3610. abort ();
  3611. if (adjusted)
  3612. /* None of two values of exponent is
  3613. the right one. Prevent an endless
  3614. loop. */
  3615. abort ();
  3616. free (digits);
  3617. if (ndigits == precision)
  3618. exponent -= 1;
  3619. else
  3620. exponent += 1;
  3621. adjusted = 1;
  3622. }
  3623. /* Here ndigits = precision+1. */
  3624. if (is_borderline (digits, precision))
  3625. {
  3626. /* Maybe the exponent guess was too high
  3627. and a smaller exponent can be reached
  3628. by turning a 10...0 into 9...9x. */
  3629. char *digits2 =
  3630. scale10_round_decimal_long_double (arg,
  3631. (int)precision - exponent + 1);
  3632. if (digits2 == NULL)
  3633. {
  3634. free (digits);
  3635. END_LONG_DOUBLE_ROUNDING ();
  3636. goto out_of_memory;
  3637. }
  3638. if (strlen (digits2) == precision + 1)
  3639. {
  3640. free (digits);
  3641. digits = digits2;
  3642. exponent -= 1;
  3643. }
  3644. else
  3645. free (digits2);
  3646. }
  3647. /* Here ndigits = precision+1. */
  3648. *p++ = digits[--ndigits];
  3649. if ((flags & FLAG_ALT) || precision > 0)
  3650. {
  3651. *p++ = decimal_point_char ();
  3652. while (ndigits > 0)
  3653. {
  3654. --ndigits;
  3655. *p++ = digits[ndigits];
  3656. }
  3657. }
  3658. free (digits);
  3659. }
  3660. *p++ = dp->conversion; /* 'e' or 'E' */
  3661. # if WIDE_CHAR_VERSION
  3662. {
  3663. static const wchar_t decimal_format[] =
  3664. { '%', '+', '.', '2', 'd', '\0' };
  3665. SNPRINTF (p, 6 + 1, decimal_format, exponent);
  3666. }
  3667. while (*p != '\0')
  3668. p++;
  3669. # else
  3670. if (sizeof (DCHAR_T) == 1)
  3671. {
  3672. sprintf ((char *) p, "%+.2d", exponent);
  3673. while (*p != '\0')
  3674. p++;
  3675. }
  3676. else
  3677. {
  3678. char expbuf[6 + 1];
  3679. const char *ep;
  3680. sprintf (expbuf, "%+.2d", exponent);
  3681. for (ep = expbuf; (*p = *ep) != '\0'; ep++)
  3682. p++;
  3683. }
  3684. # endif
  3685. }
  3686. else if (dp->conversion == 'g' || dp->conversion == 'G')
  3687. {
  3688. if (precision == 0)
  3689. precision = 1;
  3690. /* precision >= 1. */
  3691. if (arg == 0.0L)
  3692. /* The exponent is 0, >= -4, < precision.
  3693. Use fixed-point notation. */
  3694. {
  3695. size_t ndigits = precision;
  3696. /* Number of trailing zeroes that have to be
  3697. dropped. */
  3698. size_t nzeroes =
  3699. (flags & FLAG_ALT ? 0 : precision - 1);
  3700. --ndigits;
  3701. *p++ = '0';
  3702. if ((flags & FLAG_ALT) || ndigits > nzeroes)
  3703. {
  3704. *p++ = decimal_point_char ();
  3705. while (ndigits > nzeroes)
  3706. {
  3707. --ndigits;
  3708. *p++ = '0';
  3709. }
  3710. }
  3711. }
  3712. else
  3713. {
  3714. /* arg > 0.0L. */
  3715. int exponent;
  3716. int adjusted;
  3717. char *digits;
  3718. size_t ndigits;
  3719. size_t nzeroes;
  3720. exponent = floorlog10l (arg);
  3721. adjusted = 0;
  3722. for (;;)
  3723. {
  3724. digits =
  3725. scale10_round_decimal_long_double (arg,
  3726. (int)(precision - 1) - exponent);
  3727. if (digits == NULL)
  3728. {
  3729. END_LONG_DOUBLE_ROUNDING ();
  3730. goto out_of_memory;
  3731. }
  3732. ndigits = strlen (digits);
  3733. if (ndigits == precision)
  3734. break;
  3735. if (ndigits < precision - 1
  3736. || ndigits > precision + 1)
  3737. /* The exponent was not guessed
  3738. precisely enough. */
  3739. abort ();
  3740. if (adjusted)
  3741. /* None of two values of exponent is
  3742. the right one. Prevent an endless
  3743. loop. */
  3744. abort ();
  3745. free (digits);
  3746. if (ndigits < precision)
  3747. exponent -= 1;
  3748. else
  3749. exponent += 1;
  3750. adjusted = 1;
  3751. }
  3752. /* Here ndigits = precision. */
  3753. if (is_borderline (digits, precision - 1))
  3754. {
  3755. /* Maybe the exponent guess was too high
  3756. and a smaller exponent can be reached
  3757. by turning a 10...0 into 9...9x. */
  3758. char *digits2 =
  3759. scale10_round_decimal_long_double (arg,
  3760. (int)(precision - 1) - exponent + 1);
  3761. if (digits2 == NULL)
  3762. {
  3763. free (digits);
  3764. END_LONG_DOUBLE_ROUNDING ();
  3765. goto out_of_memory;
  3766. }
  3767. if (strlen (digits2) == precision)
  3768. {
  3769. free (digits);
  3770. digits = digits2;
  3771. exponent -= 1;
  3772. }
  3773. else
  3774. free (digits2);
  3775. }
  3776. /* Here ndigits = precision. */
  3777. /* Determine the number of trailing zeroes
  3778. that have to be dropped. */
  3779. nzeroes = 0;
  3780. if ((flags & FLAG_ALT) == 0)
  3781. while (nzeroes < ndigits
  3782. && digits[nzeroes] == '0')
  3783. nzeroes++;
  3784. /* The exponent is now determined. */
  3785. if (exponent >= -4
  3786. && exponent < (long)precision)
  3787. {
  3788. /* Fixed-point notation:
  3789. max(exponent,0)+1 digits, then the
  3790. decimal point, then the remaining
  3791. digits without trailing zeroes. */
  3792. if (exponent >= 0)
  3793. {
  3794. size_t ecount = exponent + 1;
  3795. /* Note: count <= precision = ndigits. */
  3796. for (; ecount > 0; ecount--)
  3797. *p++ = digits[--ndigits];
  3798. if ((flags & FLAG_ALT) || ndigits > nzeroes)
  3799. {
  3800. *p++ = decimal_point_char ();
  3801. while (ndigits > nzeroes)
  3802. {
  3803. --ndigits;
  3804. *p++ = digits[ndigits];
  3805. }
  3806. }
  3807. }
  3808. else
  3809. {
  3810. size_t ecount = -exponent - 1;
  3811. *p++ = '0';
  3812. *p++ = decimal_point_char ();
  3813. for (; ecount > 0; ecount--)
  3814. *p++ = '0';
  3815. while (ndigits > nzeroes)
  3816. {
  3817. --ndigits;
  3818. *p++ = digits[ndigits];
  3819. }
  3820. }
  3821. }
  3822. else
  3823. {
  3824. /* Exponential notation. */
  3825. *p++ = digits[--ndigits];
  3826. if ((flags & FLAG_ALT) || ndigits > nzeroes)
  3827. {
  3828. *p++ = decimal_point_char ();
  3829. while (ndigits > nzeroes)
  3830. {
  3831. --ndigits;
  3832. *p++ = digits[ndigits];
  3833. }
  3834. }
  3835. *p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
  3836. # if WIDE_CHAR_VERSION
  3837. {
  3838. static const wchar_t decimal_format[] =
  3839. { '%', '+', '.', '2', 'd', '\0' };
  3840. SNPRINTF (p, 6 + 1, decimal_format, exponent);
  3841. }
  3842. while (*p != '\0')
  3843. p++;
  3844. # else
  3845. if (sizeof (DCHAR_T) == 1)
  3846. {
  3847. sprintf ((char *) p, "%+.2d", exponent);
  3848. while (*p != '\0')
  3849. p++;
  3850. }
  3851. else
  3852. {
  3853. char expbuf[6 + 1];
  3854. const char *ep;
  3855. sprintf (expbuf, "%+.2d", exponent);
  3856. for (ep = expbuf; (*p = *ep) != '\0'; ep++)
  3857. p++;
  3858. }
  3859. # endif
  3860. }
  3861. free (digits);
  3862. }
  3863. }
  3864. else
  3865. abort ();
  3866. # else
  3867. /* arg is finite. */
  3868. if (!(arg == 0.0L))
  3869. abort ();
  3870. pad_ptr = p;
  3871. if (dp->conversion == 'f' || dp->conversion == 'F')
  3872. {
  3873. *p++ = '0';
  3874. if ((flags & FLAG_ALT) || precision > 0)
  3875. {
  3876. *p++ = decimal_point_char ();
  3877. for (; precision > 0; precision--)
  3878. *p++ = '0';
  3879. }
  3880. }
  3881. else if (dp->conversion == 'e' || dp->conversion == 'E')
  3882. {
  3883. *p++ = '0';
  3884. if ((flags & FLAG_ALT) || precision > 0)
  3885. {
  3886. *p++ = decimal_point_char ();
  3887. for (; precision > 0; precision--)
  3888. *p++ = '0';
  3889. }
  3890. *p++ = dp->conversion; /* 'e' or 'E' */
  3891. *p++ = '+';
  3892. *p++ = '0';
  3893. *p++ = '0';
  3894. }
  3895. else if (dp->conversion == 'g' || dp->conversion == 'G')
  3896. {
  3897. *p++ = '0';
  3898. if (flags & FLAG_ALT)
  3899. {
  3900. size_t ndigits =
  3901. (precision > 0 ? precision - 1 : 0);
  3902. *p++ = decimal_point_char ();
  3903. for (; ndigits > 0; --ndigits)
  3904. *p++ = '0';
  3905. }
  3906. }
  3907. else if (dp->conversion == 'a' || dp->conversion == 'A')
  3908. {
  3909. *p++ = '0';
  3910. *p++ = dp->conversion - 'A' + 'X';
  3911. pad_ptr = p;
  3912. *p++ = '0';
  3913. if ((flags & FLAG_ALT) || precision > 0)
  3914. {
  3915. *p++ = decimal_point_char ();
  3916. for (; precision > 0; precision--)
  3917. *p++ = '0';
  3918. }
  3919. *p++ = dp->conversion - 'A' + 'P';
  3920. *p++ = '+';
  3921. *p++ = '0';
  3922. }
  3923. else
  3924. abort ();
  3925. # endif
  3926. }
  3927. END_LONG_DOUBLE_ROUNDING ();
  3928. }
  3929. }
  3930. # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
  3931. else
  3932. # endif
  3933. # endif
  3934. # if NEED_PRINTF_DOUBLE || NEED_PRINTF_INFINITE_DOUBLE
  3935. {
  3936. double arg = a.arg[dp->arg_index].a.a_double;
  3937. if (isnand (arg))
  3938. {
  3939. if (dp->conversion >= 'A' && dp->conversion <= 'Z')
  3940. {
  3941. *p++ = 'N'; *p++ = 'A'; *p++ = 'N';
  3942. }
  3943. else
  3944. {
  3945. *p++ = 'n'; *p++ = 'a'; *p++ = 'n';
  3946. }
  3947. }
  3948. else
  3949. {
  3950. int sign = 0;
  3951. if (signbit (arg)) /* arg < 0.0 or negative zero */
  3952. {
  3953. sign = -1;
  3954. arg = -arg;
  3955. }
  3956. if (sign < 0)
  3957. *p++ = '-';
  3958. else if (flags & FLAG_SHOWSIGN)
  3959. *p++ = '+';
  3960. else if (flags & FLAG_SPACE)
  3961. *p++ = ' ';
  3962. if (arg > 0.0 && arg + arg == arg)
  3963. {
  3964. if (dp->conversion >= 'A' && dp->conversion <= 'Z')
  3965. {
  3966. *p++ = 'I'; *p++ = 'N'; *p++ = 'F';
  3967. }
  3968. else
  3969. {
  3970. *p++ = 'i'; *p++ = 'n'; *p++ = 'f';
  3971. }
  3972. }
  3973. else
  3974. {
  3975. # if NEED_PRINTF_DOUBLE
  3976. pad_ptr = p;
  3977. if (dp->conversion == 'f' || dp->conversion == 'F')
  3978. {
  3979. char *digits;
  3980. size_t ndigits;
  3981. digits =
  3982. scale10_round_decimal_double (arg, precision);
  3983. if (digits == NULL)
  3984. goto out_of_memory;
  3985. ndigits = strlen (digits);
  3986. if (ndigits > precision)
  3987. do
  3988. {
  3989. --ndigits;
  3990. *p++ = digits[ndigits];
  3991. }
  3992. while (ndigits > precision);
  3993. else
  3994. *p++ = '0';
  3995. /* Here ndigits <= precision. */
  3996. if ((flags & FLAG_ALT) || precision > 0)
  3997. {
  3998. *p++ = decimal_point_char ();
  3999. for (; precision > ndigits; precision--)
  4000. *p++ = '0';
  4001. while (ndigits > 0)
  4002. {
  4003. --ndigits;
  4004. *p++ = digits[ndigits];
  4005. }
  4006. }
  4007. free (digits);
  4008. }
  4009. else if (dp->conversion == 'e' || dp->conversion == 'E')
  4010. {
  4011. int exponent;
  4012. if (arg == 0.0)
  4013. {
  4014. exponent = 0;
  4015. *p++ = '0';
  4016. if ((flags & FLAG_ALT) || precision > 0)
  4017. {
  4018. *p++ = decimal_point_char ();
  4019. for (; precision > 0; precision--)
  4020. *p++ = '0';
  4021. }
  4022. }
  4023. else
  4024. {
  4025. /* arg > 0.0. */
  4026. int adjusted;
  4027. char *digits;
  4028. size_t ndigits;
  4029. exponent = floorlog10 (arg);
  4030. adjusted = 0;
  4031. for (;;)
  4032. {
  4033. digits =
  4034. scale10_round_decimal_double (arg,
  4035. (int)precision - exponent);
  4036. if (digits == NULL)
  4037. goto out_of_memory;
  4038. ndigits = strlen (digits);
  4039. if (ndigits == precision + 1)
  4040. break;
  4041. if (ndigits < precision
  4042. || ndigits > precision + 2)
  4043. /* The exponent was not guessed
  4044. precisely enough. */
  4045. abort ();
  4046. if (adjusted)
  4047. /* None of two values of exponent is
  4048. the right one. Prevent an endless
  4049. loop. */
  4050. abort ();
  4051. free (digits);
  4052. if (ndigits == precision)
  4053. exponent -= 1;
  4054. else
  4055. exponent += 1;
  4056. adjusted = 1;
  4057. }
  4058. /* Here ndigits = precision+1. */
  4059. if (is_borderline (digits, precision))
  4060. {
  4061. /* Maybe the exponent guess was too high
  4062. and a smaller exponent can be reached
  4063. by turning a 10...0 into 9...9x. */
  4064. char *digits2 =
  4065. scale10_round_decimal_double (arg,
  4066. (int)precision - exponent + 1);
  4067. if (digits2 == NULL)
  4068. {
  4069. free (digits);
  4070. goto out_of_memory;
  4071. }
  4072. if (strlen (digits2) == precision + 1)
  4073. {
  4074. free (digits);
  4075. digits = digits2;
  4076. exponent -= 1;
  4077. }
  4078. else
  4079. free (digits2);
  4080. }
  4081. /* Here ndigits = precision+1. */
  4082. *p++ = digits[--ndigits];
  4083. if ((flags & FLAG_ALT) || precision > 0)
  4084. {
  4085. *p++ = decimal_point_char ();
  4086. while (ndigits > 0)
  4087. {
  4088. --ndigits;
  4089. *p++ = digits[ndigits];
  4090. }
  4091. }
  4092. free (digits);
  4093. }
  4094. *p++ = dp->conversion; /* 'e' or 'E' */
  4095. # if WIDE_CHAR_VERSION
  4096. {
  4097. static const wchar_t decimal_format[] =
  4098. /* Produce the same number of exponent digits
  4099. as the native printf implementation. */
  4100. # if defined _WIN32 && ! defined __CYGWIN__
  4101. { '%', '+', '.', '3', 'd', '\0' };
  4102. # else
  4103. { '%', '+', '.', '2', 'd', '\0' };
  4104. # endif
  4105. SNPRINTF (p, 6 + 1, decimal_format, exponent);
  4106. }
  4107. while (*p != '\0')
  4108. p++;
  4109. # else
  4110. {
  4111. static const char decimal_format[] =
  4112. /* Produce the same number of exponent digits
  4113. as the native printf implementation. */
  4114. # if defined _WIN32 && ! defined __CYGWIN__
  4115. "%+.3d";
  4116. # else
  4117. "%+.2d";
  4118. # endif
  4119. if (sizeof (DCHAR_T) == 1)
  4120. {
  4121. sprintf ((char *) p, decimal_format, exponent);
  4122. while (*p != '\0')
  4123. p++;
  4124. }
  4125. else
  4126. {
  4127. char expbuf[6 + 1];
  4128. const char *ep;
  4129. sprintf (expbuf, decimal_format, exponent);
  4130. for (ep = expbuf; (*p = *ep) != '\0'; ep++)
  4131. p++;
  4132. }
  4133. }
  4134. # endif
  4135. }
  4136. else if (dp->conversion == 'g' || dp->conversion == 'G')
  4137. {
  4138. if (precision == 0)
  4139. precision = 1;
  4140. /* precision >= 1. */
  4141. if (arg == 0.0)
  4142. /* The exponent is 0, >= -4, < precision.
  4143. Use fixed-point notation. */
  4144. {
  4145. size_t ndigits = precision;
  4146. /* Number of trailing zeroes that have to be
  4147. dropped. */
  4148. size_t nzeroes =
  4149. (flags & FLAG_ALT ? 0 : precision - 1);
  4150. --ndigits;
  4151. *p++ = '0';
  4152. if ((flags & FLAG_ALT) || ndigits > nzeroes)
  4153. {
  4154. *p++ = decimal_point_char ();
  4155. while (ndigits > nzeroes)
  4156. {
  4157. --ndigits;
  4158. *p++ = '0';
  4159. }
  4160. }
  4161. }
  4162. else
  4163. {
  4164. /* arg > 0.0. */
  4165. int exponent;
  4166. int adjusted;
  4167. char *digits;
  4168. size_t ndigits;
  4169. size_t nzeroes;
  4170. exponent = floorlog10 (arg);
  4171. adjusted = 0;
  4172. for (;;)
  4173. {
  4174. digits =
  4175. scale10_round_decimal_double (arg,
  4176. (int)(precision - 1) - exponent);
  4177. if (digits == NULL)
  4178. goto out_of_memory;
  4179. ndigits = strlen (digits);
  4180. if (ndigits == precision)
  4181. break;
  4182. if (ndigits < precision - 1
  4183. || ndigits > precision + 1)
  4184. /* The exponent was not guessed
  4185. precisely enough. */
  4186. abort ();
  4187. if (adjusted)
  4188. /* None of two values of exponent is
  4189. the right one. Prevent an endless
  4190. loop. */
  4191. abort ();
  4192. free (digits);
  4193. if (ndigits < precision)
  4194. exponent -= 1;
  4195. else
  4196. exponent += 1;
  4197. adjusted = 1;
  4198. }
  4199. /* Here ndigits = precision. */
  4200. if (is_borderline (digits, precision - 1))
  4201. {
  4202. /* Maybe the exponent guess was too high
  4203. and a smaller exponent can be reached
  4204. by turning a 10...0 into 9...9x. */
  4205. char *digits2 =
  4206. scale10_round_decimal_double (arg,
  4207. (int)(precision - 1) - exponent + 1);
  4208. if (digits2 == NULL)
  4209. {
  4210. free (digits);
  4211. goto out_of_memory;
  4212. }
  4213. if (strlen (digits2) == precision)
  4214. {
  4215. free (digits);
  4216. digits = digits2;
  4217. exponent -= 1;
  4218. }
  4219. else
  4220. free (digits2);
  4221. }
  4222. /* Here ndigits = precision. */
  4223. /* Determine the number of trailing zeroes
  4224. that have to be dropped. */
  4225. nzeroes = 0;
  4226. if ((flags & FLAG_ALT) == 0)
  4227. while (nzeroes < ndigits
  4228. && digits[nzeroes] == '0')
  4229. nzeroes++;
  4230. /* The exponent is now determined. */
  4231. if (exponent >= -4
  4232. && exponent < (long)precision)
  4233. {
  4234. /* Fixed-point notation:
  4235. max(exponent,0)+1 digits, then the
  4236. decimal point, then the remaining
  4237. digits without trailing zeroes. */
  4238. if (exponent >= 0)
  4239. {
  4240. size_t ecount = exponent + 1;
  4241. /* Note: ecount <= precision = ndigits. */
  4242. for (; ecount > 0; ecount--)
  4243. *p++ = digits[--ndigits];
  4244. if ((flags & FLAG_ALT) || ndigits > nzeroes)
  4245. {
  4246. *p++ = decimal_point_char ();
  4247. while (ndigits > nzeroes)
  4248. {
  4249. --ndigits;
  4250. *p++ = digits[ndigits];
  4251. }
  4252. }
  4253. }
  4254. else
  4255. {
  4256. size_t ecount = -exponent - 1;
  4257. *p++ = '0';
  4258. *p++ = decimal_point_char ();
  4259. for (; ecount > 0; ecount--)
  4260. *p++ = '0';
  4261. while (ndigits > nzeroes)
  4262. {
  4263. --ndigits;
  4264. *p++ = digits[ndigits];
  4265. }
  4266. }
  4267. }
  4268. else
  4269. {
  4270. /* Exponential notation. */
  4271. *p++ = digits[--ndigits];
  4272. if ((flags & FLAG_ALT) || ndigits > nzeroes)
  4273. {
  4274. *p++ = decimal_point_char ();
  4275. while (ndigits > nzeroes)
  4276. {
  4277. --ndigits;
  4278. *p++ = digits[ndigits];
  4279. }
  4280. }
  4281. *p++ = dp->conversion - 'G' + 'E'; /* 'e' or 'E' */
  4282. # if WIDE_CHAR_VERSION
  4283. {
  4284. static const wchar_t decimal_format[] =
  4285. /* Produce the same number of exponent digits
  4286. as the native printf implementation. */
  4287. # if defined _WIN32 && ! defined __CYGWIN__
  4288. { '%', '+', '.', '3', 'd', '\0' };
  4289. # else
  4290. { '%', '+', '.', '2', 'd', '\0' };
  4291. # endif
  4292. SNPRINTF (p, 6 + 1, decimal_format, exponent);
  4293. }
  4294. while (*p != '\0')
  4295. p++;
  4296. # else
  4297. {
  4298. static const char decimal_format[] =
  4299. /* Produce the same number of exponent digits
  4300. as the native printf implementation. */
  4301. # if defined _WIN32 && ! defined __CYGWIN__
  4302. "%+.3d";
  4303. # else
  4304. "%+.2d";
  4305. # endif
  4306. if (sizeof (DCHAR_T) == 1)
  4307. {
  4308. sprintf ((char *) p, decimal_format, exponent);
  4309. while (*p != '\0')
  4310. p++;
  4311. }
  4312. else
  4313. {
  4314. char expbuf[6 + 1];
  4315. const char *ep;
  4316. sprintf (expbuf, decimal_format, exponent);
  4317. for (ep = expbuf; (*p = *ep) != '\0'; ep++)
  4318. p++;
  4319. }
  4320. }
  4321. # endif
  4322. }
  4323. free (digits);
  4324. }
  4325. }
  4326. else
  4327. abort ();
  4328. # else
  4329. /* arg is finite. */
  4330. if (!(arg == 0.0))
  4331. abort ();
  4332. pad_ptr = p;
  4333. if (dp->conversion == 'f' || dp->conversion == 'F')
  4334. {
  4335. *p++ = '0';
  4336. if ((flags & FLAG_ALT) || precision > 0)
  4337. {
  4338. *p++ = decimal_point_char ();
  4339. for (; precision > 0; precision--)
  4340. *p++ = '0';
  4341. }
  4342. }
  4343. else if (dp->conversion == 'e' || dp->conversion == 'E')
  4344. {
  4345. *p++ = '0';
  4346. if ((flags & FLAG_ALT) || precision > 0)
  4347. {
  4348. *p++ = decimal_point_char ();
  4349. for (; precision > 0; precision--)
  4350. *p++ = '0';
  4351. }
  4352. *p++ = dp->conversion; /* 'e' or 'E' */
  4353. *p++ = '+';
  4354. /* Produce the same number of exponent digits as
  4355. the native printf implementation. */
  4356. # if defined _WIN32 && ! defined __CYGWIN__
  4357. *p++ = '0';
  4358. # endif
  4359. *p++ = '0';
  4360. *p++ = '0';
  4361. }
  4362. else if (dp->conversion == 'g' || dp->conversion == 'G')
  4363. {
  4364. *p++ = '0';
  4365. if (flags & FLAG_ALT)
  4366. {
  4367. size_t ndigits =
  4368. (precision > 0 ? precision - 1 : 0);
  4369. *p++ = decimal_point_char ();
  4370. for (; ndigits > 0; --ndigits)
  4371. *p++ = '0';
  4372. }
  4373. }
  4374. else
  4375. abort ();
  4376. # endif
  4377. }
  4378. }
  4379. }
  4380. # endif
  4381. /* The generated string now extends from tmp to p, with the
  4382. zero padding insertion point being at pad_ptr. */
  4383. count = p - tmp;
  4384. if (count < width)
  4385. {
  4386. size_t pad = width - count;
  4387. DCHAR_T *end = p + pad;
  4388. if (flags & FLAG_LEFT)
  4389. {
  4390. /* Pad with spaces on the right. */
  4391. for (; pad > 0; pad--)
  4392. *p++ = ' ';
  4393. }
  4394. else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
  4395. {
  4396. /* Pad with zeroes. */
  4397. DCHAR_T *q = end;
  4398. while (p > pad_ptr)
  4399. *--q = *--p;
  4400. for (; pad > 0; pad--)
  4401. *p++ = '0';
  4402. }
  4403. else
  4404. {
  4405. /* Pad with spaces on the left. */
  4406. DCHAR_T *q = end;
  4407. while (p > tmp)
  4408. *--q = *--p;
  4409. for (; pad > 0; pad--)
  4410. *p++ = ' ';
  4411. }
  4412. p = end;
  4413. }
  4414. count = p - tmp;
  4415. if (count >= tmp_length)
  4416. /* tmp_length was incorrectly calculated - fix the
  4417. code above! */
  4418. abort ();
  4419. /* Make room for the result. */
  4420. if (count >= allocated - length)
  4421. {
  4422. size_t n = xsum (length, count);
  4423. ENSURE_ALLOCATION (n);
  4424. }
  4425. /* Append the result. */
  4426. memcpy (result + length, tmp, count * sizeof (DCHAR_T));
  4427. if (tmp != tmpbuf)
  4428. free (tmp);
  4429. length += count;
  4430. }
  4431. #endif
  4432. else
  4433. {
  4434. arg_type type = a.arg[dp->arg_index].type;
  4435. int flags = dp->flags;
  4436. #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
  4437. int has_width;
  4438. #endif
  4439. #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
  4440. size_t width;
  4441. #endif
  4442. #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
  4443. int has_precision;
  4444. size_t precision;
  4445. #endif
  4446. #if NEED_PRINTF_UNBOUNDED_PRECISION
  4447. int prec_ourselves;
  4448. #else
  4449. # define prec_ourselves 0
  4450. #endif
  4451. #if NEED_PRINTF_FLAG_LEFTADJUST
  4452. # define pad_ourselves 1
  4453. #elif !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
  4454. int pad_ourselves;
  4455. #else
  4456. # define pad_ourselves 0
  4457. #endif
  4458. TCHAR_T *fbp;
  4459. unsigned int prefix_count;
  4460. int prefixes[2] IF_LINT (= { 0 });
  4461. int orig_errno;
  4462. #if !USE_SNPRINTF
  4463. size_t tmp_length;
  4464. TCHAR_T tmpbuf[700];
  4465. TCHAR_T *tmp;
  4466. #endif
  4467. #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
  4468. has_width = 0;
  4469. #endif
  4470. #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
  4471. width = 0;
  4472. if (dp->width_start != dp->width_end)
  4473. {
  4474. if (dp->width_arg_index != ARG_NONE)
  4475. {
  4476. int arg;
  4477. if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
  4478. abort ();
  4479. arg = a.arg[dp->width_arg_index].a.a_int;
  4480. width = arg;
  4481. if (arg < 0)
  4482. {
  4483. /* "A negative field width is taken as a '-' flag
  4484. followed by a positive field width." */
  4485. flags |= FLAG_LEFT;
  4486. width = -width;
  4487. }
  4488. }
  4489. else
  4490. {
  4491. const FCHAR_T *digitp = dp->width_start;
  4492. do
  4493. width = xsum (xtimes (width, 10), *digitp++ - '0');
  4494. while (digitp != dp->width_end);
  4495. }
  4496. #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
  4497. has_width = 1;
  4498. #endif
  4499. }
  4500. #endif
  4501. #if !USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || NEED_PRINTF_UNBOUNDED_PRECISION
  4502. has_precision = 0;
  4503. precision = 6;
  4504. if (dp->precision_start != dp->precision_end)
  4505. {
  4506. if (dp->precision_arg_index != ARG_NONE)
  4507. {
  4508. int arg;
  4509. if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
  4510. abort ();
  4511. arg = a.arg[dp->precision_arg_index].a.a_int;
  4512. /* "A negative precision is taken as if the precision
  4513. were omitted." */
  4514. if (arg >= 0)
  4515. {
  4516. precision = arg;
  4517. has_precision = 1;
  4518. }
  4519. }
  4520. else
  4521. {
  4522. const FCHAR_T *digitp = dp->precision_start + 1;
  4523. precision = 0;
  4524. while (digitp != dp->precision_end)
  4525. precision = xsum (xtimes (precision, 10), *digitp++ - '0');
  4526. has_precision = 1;
  4527. }
  4528. }
  4529. #endif
  4530. /* Decide whether to handle the precision ourselves. */
  4531. #if NEED_PRINTF_UNBOUNDED_PRECISION
  4532. switch (dp->conversion)
  4533. {
  4534. case 'd': case 'i': case 'u':
  4535. case 'o':
  4536. case 'x': case 'X': case 'p':
  4537. prec_ourselves = has_precision && (precision > 0);
  4538. break;
  4539. default:
  4540. prec_ourselves = 0;
  4541. break;
  4542. }
  4543. #endif
  4544. /* Decide whether to perform the padding ourselves. */
  4545. #if !NEED_PRINTF_FLAG_LEFTADJUST && (!DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION)
  4546. switch (dp->conversion)
  4547. {
  4548. # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
  4549. /* If we need conversion from TCHAR_T[] to DCHAR_T[], we need
  4550. to perform the padding after this conversion. Functions
  4551. with unistdio extensions perform the padding based on
  4552. character count rather than element count. */
  4553. case 'c': case 's':
  4554. # endif
  4555. # if NEED_PRINTF_FLAG_ZERO
  4556. case 'f': case 'F': case 'e': case 'E': case 'g': case 'G':
  4557. case 'a': case 'A':
  4558. # endif
  4559. pad_ourselves = 1;
  4560. break;
  4561. default:
  4562. pad_ourselves = prec_ourselves;
  4563. break;
  4564. }
  4565. #endif
  4566. #if !USE_SNPRINTF
  4567. /* Allocate a temporary buffer of sufficient size for calling
  4568. sprintf. */
  4569. tmp_length =
  4570. MAX_ROOM_NEEDED (&a, dp->arg_index, dp->conversion, type,
  4571. flags, width, has_precision, precision,
  4572. pad_ourselves);
  4573. if (tmp_length <= sizeof (tmpbuf) / sizeof (TCHAR_T))
  4574. tmp = tmpbuf;
  4575. else
  4576. {
  4577. size_t tmp_memsize = xtimes (tmp_length, sizeof (TCHAR_T));
  4578. if (size_overflow_p (tmp_memsize))
  4579. /* Overflow, would lead to out of memory. */
  4580. goto out_of_memory;
  4581. tmp = (TCHAR_T *) malloc (tmp_memsize);
  4582. if (tmp == NULL)
  4583. /* Out of memory. */
  4584. goto out_of_memory;
  4585. }
  4586. #endif
  4587. /* Construct the format string for calling snprintf or
  4588. sprintf. */
  4589. fbp = buf;
  4590. *fbp++ = '%';
  4591. #if NEED_PRINTF_FLAG_GROUPING
  4592. /* The underlying implementation doesn't support the ' flag.
  4593. Produce no grouping characters in this case; this is
  4594. acceptable because the grouping is locale dependent. */
  4595. #else
  4596. if (flags & FLAG_GROUP)
  4597. *fbp++ = '\'';
  4598. #endif
  4599. if (flags & FLAG_LEFT)
  4600. *fbp++ = '-';
  4601. if (flags & FLAG_SHOWSIGN)
  4602. *fbp++ = '+';
  4603. if (flags & FLAG_SPACE)
  4604. *fbp++ = ' ';
  4605. if (flags & FLAG_ALT)
  4606. *fbp++ = '#';
  4607. #if __GLIBC__ >= 2 && !defined __UCLIBC__
  4608. if (flags & FLAG_LOCALIZED)
  4609. *fbp++ = 'I';
  4610. #endif
  4611. if (!pad_ourselves)
  4612. {
  4613. if (flags & FLAG_ZERO)
  4614. *fbp++ = '0';
  4615. if (dp->width_start != dp->width_end)
  4616. {
  4617. size_t n = dp->width_end - dp->width_start;
  4618. /* The width specification is known to consist only
  4619. of standard ASCII characters. */
  4620. if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
  4621. {
  4622. memcpy (fbp, dp->width_start, n * sizeof (TCHAR_T));
  4623. fbp += n;
  4624. }
  4625. else
  4626. {
  4627. const FCHAR_T *mp = dp->width_start;
  4628. do
  4629. *fbp++ = *mp++;
  4630. while (--n > 0);
  4631. }
  4632. }
  4633. }
  4634. if (!prec_ourselves)
  4635. {
  4636. if (dp->precision_start != dp->precision_end)
  4637. {
  4638. size_t n = dp->precision_end - dp->precision_start;
  4639. /* The precision specification is known to consist only
  4640. of standard ASCII characters. */
  4641. if (sizeof (FCHAR_T) == sizeof (TCHAR_T))
  4642. {
  4643. memcpy (fbp, dp->precision_start, n * sizeof (TCHAR_T));
  4644. fbp += n;
  4645. }
  4646. else
  4647. {
  4648. const FCHAR_T *mp = dp->precision_start;
  4649. do
  4650. *fbp++ = *mp++;
  4651. while (--n > 0);
  4652. }
  4653. }
  4654. }
  4655. switch (type)
  4656. {
  4657. case TYPE_LONGLONGINT:
  4658. case TYPE_ULONGLONGINT:
  4659. #if defined _WIN32 && ! defined __CYGWIN__
  4660. *fbp++ = 'I';
  4661. *fbp++ = '6';
  4662. *fbp++ = '4';
  4663. break;
  4664. #else
  4665. *fbp++ = 'l';
  4666. #endif
  4667. FALLTHROUGH;
  4668. case TYPE_LONGINT:
  4669. case TYPE_ULONGINT:
  4670. #if HAVE_WINT_T
  4671. case TYPE_WIDE_CHAR:
  4672. #endif
  4673. #if HAVE_WCHAR_T
  4674. case TYPE_WIDE_STRING:
  4675. #endif
  4676. *fbp++ = 'l';
  4677. break;
  4678. case TYPE_LONGDOUBLE:
  4679. *fbp++ = 'L';
  4680. break;
  4681. default:
  4682. break;
  4683. }
  4684. #if NEED_PRINTF_DIRECTIVE_F
  4685. if (dp->conversion == 'F')
  4686. *fbp = 'f';
  4687. else
  4688. #endif
  4689. *fbp = dp->conversion;
  4690. #if USE_SNPRINTF
  4691. # if ((HAVE_SNPRINTF_RETVAL_C99 && HAVE_SNPRINTF_TRUNCATION_C99) \
  4692. || ((__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) \
  4693. && !defined __UCLIBC__) \
  4694. || (defined __APPLE__ && defined __MACH__) \
  4695. || defined __ANDROID__ \
  4696. || (defined _WIN32 && ! defined __CYGWIN__))
  4697. /* On systems where we know that snprintf's return value
  4698. conforms to ISO C 99 (HAVE_SNPRINTF_RETVAL_C99) and that
  4699. snprintf always produces NUL-terminated strings
  4700. (HAVE_SNPRINTF_TRUNCATION_C99), it is possible to avoid
  4701. using %n. And it is desirable to do so, because more and
  4702. more platforms no longer support %n, for "security reasons".
  4703. In particular, the following platforms:
  4704. - On glibc2 systems from 2004-10-18 or newer, the use of
  4705. %n in format strings in writable memory may crash the
  4706. program (if compiled with _FORTIFY_SOURCE=2).
  4707. - On Mac OS X 10.13 or newer, the use of %n in format
  4708. strings in writable memory by default crashes the
  4709. program.
  4710. - On Android, starting on 2018-03-07, the use of %n in
  4711. format strings produces a fatal error (see
  4712. <https://android.googlesource.com/platform/bionic/+/41398d03b7e8e0dfb951660ae713e682e9fc0336>).
  4713. On these platforms, HAVE_SNPRINTF_RETVAL_C99 and
  4714. HAVE_SNPRINTF_TRUNCATION_C99 are 1. We have listed them
  4715. explicitly in the condition above, in case of cross-
  4716. compilation (just to be sure). */
  4717. /* On native Windows systems (such as mingw), we can avoid using
  4718. %n because:
  4719. - Although the gl_SNPRINTF_TRUNCATION_C99 test fails,
  4720. snprintf does not write more than the specified number
  4721. of bytes. (snprintf (buf, 3, "%d %d", 4567, 89) writes
  4722. '4', '5', '6' into buf, not '4', '5', '\0'.)
  4723. - Although the gl_SNPRINTF_RETVAL_C99 test fails, snprintf
  4724. allows us to recognize the case of an insufficient
  4725. buffer size: it returns -1 in this case.
  4726. On native Windows systems (such as mingw) where the OS is
  4727. Windows Vista, the use of %n in format strings by default
  4728. crashes the program. See
  4729. <https://gcc.gnu.org/ml/gcc/2007-06/msg00122.html> and
  4730. <https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/set-printf-count-output>
  4731. So we should avoid %n in this situation. */
  4732. fbp[1] = '\0';
  4733. # else /* AIX <= 5.1, HP-UX, IRIX, OSF/1, Solaris <= 9, BeOS */
  4734. fbp[1] = '%';
  4735. fbp[2] = 'n';
  4736. fbp[3] = '\0';
  4737. # endif
  4738. #else
  4739. fbp[1] = '\0';
  4740. #endif
  4741. /* Construct the arguments for calling snprintf or sprintf. */
  4742. prefix_count = 0;
  4743. if (!pad_ourselves && dp->width_arg_index != ARG_NONE)
  4744. {
  4745. if (!(a.arg[dp->width_arg_index].type == TYPE_INT))
  4746. abort ();
  4747. prefixes[prefix_count++] = a.arg[dp->width_arg_index].a.a_int;
  4748. }
  4749. if (!prec_ourselves && dp->precision_arg_index != ARG_NONE)
  4750. {
  4751. if (!(a.arg[dp->precision_arg_index].type == TYPE_INT))
  4752. abort ();
  4753. prefixes[prefix_count++] = a.arg[dp->precision_arg_index].a.a_int;
  4754. }
  4755. #if USE_SNPRINTF
  4756. /* The SNPRINTF result is appended after result[0..length].
  4757. The latter is an array of DCHAR_T; SNPRINTF appends an
  4758. array of TCHAR_T to it. This is possible because
  4759. sizeof (TCHAR_T) divides sizeof (DCHAR_T) and
  4760. alignof (TCHAR_T) <= alignof (DCHAR_T). */
  4761. # define TCHARS_PER_DCHAR (sizeof (DCHAR_T) / sizeof (TCHAR_T))
  4762. /* Ensure that maxlen below will be >= 2. Needed on BeOS,
  4763. where an snprintf() with maxlen==1 acts like sprintf(). */
  4764. ENSURE_ALLOCATION (xsum (length,
  4765. (2 + TCHARS_PER_DCHAR - 1)
  4766. / TCHARS_PER_DCHAR));
  4767. /* Prepare checking whether snprintf returns the count
  4768. via %n. */
  4769. *(TCHAR_T *) (result + length) = '\0';
  4770. #endif
  4771. orig_errno = errno;
  4772. for (;;)
  4773. {
  4774. int count = -1;
  4775. #if USE_SNPRINTF
  4776. int retcount = 0;
  4777. size_t maxlen = allocated - length;
  4778. /* SNPRINTF can fail if its second argument is
  4779. > INT_MAX. */
  4780. if (maxlen > INT_MAX / TCHARS_PER_DCHAR)
  4781. maxlen = INT_MAX / TCHARS_PER_DCHAR;
  4782. maxlen = maxlen * TCHARS_PER_DCHAR;
  4783. # define SNPRINTF_BUF(arg) \
  4784. switch (prefix_count) \
  4785. { \
  4786. case 0: \
  4787. retcount = SNPRINTF ((TCHAR_T *) (result + length), \
  4788. maxlen, buf, \
  4789. arg, &count); \
  4790. break; \
  4791. case 1: \
  4792. retcount = SNPRINTF ((TCHAR_T *) (result + length), \
  4793. maxlen, buf, \
  4794. prefixes[0], arg, &count); \
  4795. break; \
  4796. case 2: \
  4797. retcount = SNPRINTF ((TCHAR_T *) (result + length), \
  4798. maxlen, buf, \
  4799. prefixes[0], prefixes[1], arg, \
  4800. &count); \
  4801. break; \
  4802. default: \
  4803. abort (); \
  4804. }
  4805. #else
  4806. # define SNPRINTF_BUF(arg) \
  4807. switch (prefix_count) \
  4808. { \
  4809. case 0: \
  4810. count = sprintf (tmp, buf, arg); \
  4811. break; \
  4812. case 1: \
  4813. count = sprintf (tmp, buf, prefixes[0], arg); \
  4814. break; \
  4815. case 2: \
  4816. count = sprintf (tmp, buf, prefixes[0], prefixes[1],\
  4817. arg); \
  4818. break; \
  4819. default: \
  4820. abort (); \
  4821. }
  4822. #endif
  4823. errno = 0;
  4824. switch (type)
  4825. {
  4826. case TYPE_SCHAR:
  4827. {
  4828. int arg = a.arg[dp->arg_index].a.a_schar;
  4829. SNPRINTF_BUF (arg);
  4830. }
  4831. break;
  4832. case TYPE_UCHAR:
  4833. {
  4834. unsigned int arg = a.arg[dp->arg_index].a.a_uchar;
  4835. SNPRINTF_BUF (arg);
  4836. }
  4837. break;
  4838. case TYPE_SHORT:
  4839. {
  4840. int arg = a.arg[dp->arg_index].a.a_short;
  4841. SNPRINTF_BUF (arg);
  4842. }
  4843. break;
  4844. case TYPE_USHORT:
  4845. {
  4846. unsigned int arg = a.arg[dp->arg_index].a.a_ushort;
  4847. SNPRINTF_BUF (arg);
  4848. }
  4849. break;
  4850. case TYPE_INT:
  4851. {
  4852. int arg = a.arg[dp->arg_index].a.a_int;
  4853. SNPRINTF_BUF (arg);
  4854. }
  4855. break;
  4856. case TYPE_UINT:
  4857. {
  4858. unsigned int arg = a.arg[dp->arg_index].a.a_uint;
  4859. SNPRINTF_BUF (arg);
  4860. }
  4861. break;
  4862. case TYPE_LONGINT:
  4863. {
  4864. long int arg = a.arg[dp->arg_index].a.a_longint;
  4865. SNPRINTF_BUF (arg);
  4866. }
  4867. break;
  4868. case TYPE_ULONGINT:
  4869. {
  4870. unsigned long int arg = a.arg[dp->arg_index].a.a_ulongint;
  4871. SNPRINTF_BUF (arg);
  4872. }
  4873. break;
  4874. case TYPE_LONGLONGINT:
  4875. {
  4876. long long int arg = a.arg[dp->arg_index].a.a_longlongint;
  4877. SNPRINTF_BUF (arg);
  4878. }
  4879. break;
  4880. case TYPE_ULONGLONGINT:
  4881. {
  4882. unsigned long long int arg = a.arg[dp->arg_index].a.a_ulonglongint;
  4883. SNPRINTF_BUF (arg);
  4884. }
  4885. break;
  4886. case TYPE_DOUBLE:
  4887. {
  4888. double arg = a.arg[dp->arg_index].a.a_double;
  4889. SNPRINTF_BUF (arg);
  4890. }
  4891. break;
  4892. case TYPE_LONGDOUBLE:
  4893. {
  4894. long double arg = a.arg[dp->arg_index].a.a_longdouble;
  4895. SNPRINTF_BUF (arg);
  4896. }
  4897. break;
  4898. case TYPE_CHAR:
  4899. {
  4900. int arg = a.arg[dp->arg_index].a.a_char;
  4901. SNPRINTF_BUF (arg);
  4902. }
  4903. break;
  4904. #if HAVE_WINT_T
  4905. case TYPE_WIDE_CHAR:
  4906. {
  4907. wint_t arg = a.arg[dp->arg_index].a.a_wide_char;
  4908. SNPRINTF_BUF (arg);
  4909. }
  4910. break;
  4911. #endif
  4912. case TYPE_STRING:
  4913. {
  4914. const char *arg = a.arg[dp->arg_index].a.a_string;
  4915. SNPRINTF_BUF (arg);
  4916. }
  4917. break;
  4918. #if HAVE_WCHAR_T
  4919. case TYPE_WIDE_STRING:
  4920. {
  4921. const wchar_t *arg = a.arg[dp->arg_index].a.a_wide_string;
  4922. SNPRINTF_BUF (arg);
  4923. }
  4924. break;
  4925. #endif
  4926. case TYPE_POINTER:
  4927. {
  4928. void *arg = a.arg[dp->arg_index].a.a_pointer;
  4929. SNPRINTF_BUF (arg);
  4930. }
  4931. break;
  4932. default:
  4933. abort ();
  4934. }
  4935. #if USE_SNPRINTF
  4936. /* Portability: Not all implementations of snprintf()
  4937. are ISO C 99 compliant. Determine the number of
  4938. bytes that snprintf() has produced or would have
  4939. produced. */
  4940. if (count >= 0)
  4941. {
  4942. /* Verify that snprintf() has NUL-terminated its
  4943. result. */
  4944. if ((unsigned int) count < maxlen
  4945. && ((TCHAR_T *) (result + length)) [count] != '\0')
  4946. abort ();
  4947. /* Portability hack. */
  4948. if (retcount > count)
  4949. count = retcount;
  4950. }
  4951. else
  4952. {
  4953. /* snprintf() doesn't understand the '%n'
  4954. directive. */
  4955. if (fbp[1] != '\0')
  4956. {
  4957. /* Don't use the '%n' directive; instead, look
  4958. at the snprintf() return value. */
  4959. fbp[1] = '\0';
  4960. continue;
  4961. }
  4962. else
  4963. {
  4964. /* Look at the snprintf() return value. */
  4965. if (retcount < 0)
  4966. {
  4967. # if !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF
  4968. /* HP-UX 10.20 snprintf() is doubly deficient:
  4969. It doesn't understand the '%n' directive,
  4970. *and* it returns -1 (rather than the length
  4971. that would have been required) when the
  4972. buffer is too small.
  4973. But a failure at this point can also come
  4974. from other reasons than a too small buffer,
  4975. such as an invalid wide string argument to
  4976. the %ls directive, or possibly an invalid
  4977. floating-point argument. */
  4978. size_t tmp_length =
  4979. MAX_ROOM_NEEDED (&a, dp->arg_index,
  4980. dp->conversion, type, flags,
  4981. width,
  4982. has_precision,
  4983. precision, pad_ourselves);
  4984. if (maxlen < tmp_length)
  4985. {
  4986. /* Make more room. But try to do through
  4987. this reallocation only once. */
  4988. size_t bigger_need =
  4989. xsum (length,
  4990. xsum (tmp_length,
  4991. TCHARS_PER_DCHAR - 1)
  4992. / TCHARS_PER_DCHAR);
  4993. /* And always grow proportionally.
  4994. (There may be several arguments, each
  4995. needing a little more room than the
  4996. previous one.) */
  4997. size_t bigger_need2 =
  4998. xsum (xtimes (allocated, 2), 12);
  4999. if (bigger_need < bigger_need2)
  5000. bigger_need = bigger_need2;
  5001. ENSURE_ALLOCATION (bigger_need);
  5002. continue;
  5003. }
  5004. # endif
  5005. }
  5006. else
  5007. count = retcount;
  5008. }
  5009. }
  5010. #endif
  5011. /* Attempt to handle failure. */
  5012. if (count < 0)
  5013. {
  5014. /* SNPRINTF or sprintf failed. Use the errno that it
  5015. has set, if any. */
  5016. if (errno == 0)
  5017. {
  5018. if (dp->conversion == 'c' || dp->conversion == 's')
  5019. errno = EILSEQ;
  5020. else
  5021. errno = EINVAL;
  5022. }
  5023. goto fail_with_errno;
  5024. }
  5025. #if USE_SNPRINTF
  5026. /* Handle overflow of the allocated buffer.
  5027. If such an overflow occurs, a C99 compliant snprintf()
  5028. returns a count >= maxlen. However, a non-compliant
  5029. snprintf() function returns only count = maxlen - 1. To
  5030. cover both cases, test whether count >= maxlen - 1. */
  5031. if ((unsigned int) count + 1 >= maxlen)
  5032. {
  5033. /* If maxlen already has attained its allowed maximum,
  5034. allocating more memory will not increase maxlen.
  5035. Instead of looping, bail out. */
  5036. if (maxlen == INT_MAX / TCHARS_PER_DCHAR)
  5037. goto overflow;
  5038. else
  5039. {
  5040. /* Need at least (count + 1) * sizeof (TCHAR_T)
  5041. bytes. (The +1 is for the trailing NUL.)
  5042. But ask for (count + 2) * sizeof (TCHAR_T)
  5043. bytes, so that in the next round, we likely get
  5044. maxlen > (unsigned int) count + 1
  5045. and so we don't get here again.
  5046. And allocate proportionally, to avoid looping
  5047. eternally if snprintf() reports a too small
  5048. count. */
  5049. size_t n =
  5050. xmax (xsum (length,
  5051. ((unsigned int) count + 2
  5052. + TCHARS_PER_DCHAR - 1)
  5053. / TCHARS_PER_DCHAR),
  5054. xtimes (allocated, 2));
  5055. ENSURE_ALLOCATION (n);
  5056. continue;
  5057. }
  5058. }
  5059. #endif
  5060. #if NEED_PRINTF_UNBOUNDED_PRECISION
  5061. if (prec_ourselves)
  5062. {
  5063. /* Handle the precision. */
  5064. TCHAR_T *prec_ptr =
  5065. # if USE_SNPRINTF
  5066. (TCHAR_T *) (result + length);
  5067. # else
  5068. tmp;
  5069. # endif
  5070. size_t prefix_count;
  5071. size_t move;
  5072. prefix_count = 0;
  5073. /* Put the additional zeroes after the sign. */
  5074. if (count >= 1
  5075. && (*prec_ptr == '-' || *prec_ptr == '+'
  5076. || *prec_ptr == ' '))
  5077. prefix_count = 1;
  5078. /* Put the additional zeroes after the 0x prefix if
  5079. (flags & FLAG_ALT) || (dp->conversion == 'p'). */
  5080. else if (count >= 2
  5081. && prec_ptr[0] == '0'
  5082. && (prec_ptr[1] == 'x' || prec_ptr[1] == 'X'))
  5083. prefix_count = 2;
  5084. move = count - prefix_count;
  5085. if (precision > move)
  5086. {
  5087. /* Insert zeroes. */
  5088. size_t insert = precision - move;
  5089. TCHAR_T *prec_end;
  5090. # if USE_SNPRINTF
  5091. size_t n =
  5092. xsum (length,
  5093. (count + insert + TCHARS_PER_DCHAR - 1)
  5094. / TCHARS_PER_DCHAR);
  5095. length += (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
  5096. ENSURE_ALLOCATION (n);
  5097. length -= (count + TCHARS_PER_DCHAR - 1) / TCHARS_PER_DCHAR;
  5098. prec_ptr = (TCHAR_T *) (result + length);
  5099. # endif
  5100. prec_end = prec_ptr + count;
  5101. prec_ptr += prefix_count;
  5102. while (prec_end > prec_ptr)
  5103. {
  5104. prec_end--;
  5105. prec_end[insert] = prec_end[0];
  5106. }
  5107. prec_end += insert;
  5108. do
  5109. *--prec_end = '0';
  5110. while (prec_end > prec_ptr);
  5111. count += insert;
  5112. }
  5113. }
  5114. #endif
  5115. #if !USE_SNPRINTF
  5116. if (count >= tmp_length)
  5117. /* tmp_length was incorrectly calculated - fix the
  5118. code above! */
  5119. abort ();
  5120. #endif
  5121. #if !DCHAR_IS_TCHAR
  5122. /* Convert from TCHAR_T[] to DCHAR_T[]. */
  5123. if (dp->conversion == 'c' || dp->conversion == 's')
  5124. {
  5125. /* type = TYPE_CHAR or TYPE_WIDE_CHAR or TYPE_STRING
  5126. TYPE_WIDE_STRING.
  5127. The result string is not certainly ASCII. */
  5128. const TCHAR_T *tmpsrc;
  5129. DCHAR_T *tmpdst;
  5130. size_t tmpdst_len;
  5131. /* This code assumes that TCHAR_T is 'char'. */
  5132. verify (sizeof (TCHAR_T) == 1);
  5133. # if USE_SNPRINTF
  5134. tmpsrc = (TCHAR_T *) (result + length);
  5135. # else
  5136. tmpsrc = tmp;
  5137. # endif
  5138. tmpdst =
  5139. DCHAR_CONV_FROM_ENCODING (locale_charset (),
  5140. iconveh_question_mark,
  5141. tmpsrc, count,
  5142. NULL,
  5143. NULL, &tmpdst_len);
  5144. if (tmpdst == NULL)
  5145. goto fail_with_errno;
  5146. ENSURE_ALLOCATION_ELSE (xsum (length, tmpdst_len),
  5147. { free (tmpdst); goto out_of_memory; });
  5148. DCHAR_CPY (result + length, tmpdst, tmpdst_len);
  5149. free (tmpdst);
  5150. count = tmpdst_len;
  5151. }
  5152. else
  5153. {
  5154. /* The result string is ASCII.
  5155. Simple 1:1 conversion. */
  5156. # if USE_SNPRINTF
  5157. /* If sizeof (DCHAR_T) == sizeof (TCHAR_T), it's a
  5158. no-op conversion, in-place on the array starting
  5159. at (result + length). */
  5160. if (sizeof (DCHAR_T) != sizeof (TCHAR_T))
  5161. # endif
  5162. {
  5163. const TCHAR_T *tmpsrc;
  5164. DCHAR_T *tmpdst;
  5165. size_t n;
  5166. # if USE_SNPRINTF
  5167. if (result == resultbuf)
  5168. {
  5169. tmpsrc = (TCHAR_T *) (result + length);
  5170. /* ENSURE_ALLOCATION will not move tmpsrc
  5171. (because it's part of resultbuf). */
  5172. ENSURE_ALLOCATION (xsum (length, count));
  5173. }
  5174. else
  5175. {
  5176. /* ENSURE_ALLOCATION will move the array
  5177. (because it uses realloc(). */
  5178. ENSURE_ALLOCATION (xsum (length, count));
  5179. tmpsrc = (TCHAR_T *) (result + length);
  5180. }
  5181. # else
  5182. tmpsrc = tmp;
  5183. ENSURE_ALLOCATION (xsum (length, count));
  5184. # endif
  5185. tmpdst = result + length;
  5186. /* Copy backwards, because of overlapping. */
  5187. tmpsrc += count;
  5188. tmpdst += count;
  5189. for (n = count; n > 0; n--)
  5190. *--tmpdst = *--tmpsrc;
  5191. }
  5192. }
  5193. #endif
  5194. #if DCHAR_IS_TCHAR && !USE_SNPRINTF
  5195. /* Make room for the result. */
  5196. if (count > allocated - length)
  5197. {
  5198. /* Need at least count elements. But allocate
  5199. proportionally. */
  5200. size_t n =
  5201. xmax (xsum (length, count), xtimes (allocated, 2));
  5202. ENSURE_ALLOCATION (n);
  5203. }
  5204. #endif
  5205. /* Here count <= allocated - length. */
  5206. /* Perform padding. */
  5207. #if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO || NEED_PRINTF_FLAG_LEFTADJUST || NEED_PRINTF_FLAG_ZERO || NEED_PRINTF_UNBOUNDED_PRECISION
  5208. if (pad_ourselves && has_width)
  5209. {
  5210. size_t w;
  5211. # if ENABLE_UNISTDIO
  5212. /* Outside POSIX, it's preferable to compare the width
  5213. against the number of _characters_ of the converted
  5214. value. */
  5215. w = DCHAR_MBSNLEN (result + length, count);
  5216. # else
  5217. /* The width is compared against the number of _bytes_
  5218. of the converted value, says POSIX. */
  5219. w = count;
  5220. # endif
  5221. if (w < width)
  5222. {
  5223. size_t pad = width - w;
  5224. /* Make room for the result. */
  5225. if (xsum (count, pad) > allocated - length)
  5226. {
  5227. /* Need at least count + pad elements. But
  5228. allocate proportionally. */
  5229. size_t n =
  5230. xmax (xsum3 (length, count, pad),
  5231. xtimes (allocated, 2));
  5232. # if USE_SNPRINTF
  5233. length += count;
  5234. ENSURE_ALLOCATION (n);
  5235. length -= count;
  5236. # else
  5237. ENSURE_ALLOCATION (n);
  5238. # endif
  5239. }
  5240. /* Here count + pad <= allocated - length. */
  5241. {
  5242. # if !DCHAR_IS_TCHAR || USE_SNPRINTF
  5243. DCHAR_T * const rp = result + length;
  5244. # else
  5245. DCHAR_T * const rp = tmp;
  5246. # endif
  5247. DCHAR_T *p = rp + count;
  5248. DCHAR_T *end = p + pad;
  5249. DCHAR_T *pad_ptr;
  5250. # if !DCHAR_IS_TCHAR || ENABLE_UNISTDIO
  5251. if (dp->conversion == 'c'
  5252. || dp->conversion == 's')
  5253. /* No zero-padding for string directives. */
  5254. pad_ptr = NULL;
  5255. else
  5256. # endif
  5257. {
  5258. pad_ptr = (*rp == '-' ? rp + 1 : rp);
  5259. /* No zero-padding of "inf" and "nan". */
  5260. if ((*pad_ptr >= 'A' && *pad_ptr <= 'Z')
  5261. || (*pad_ptr >= 'a' && *pad_ptr <= 'z'))
  5262. pad_ptr = NULL;
  5263. }
  5264. /* The generated string now extends from rp to p,
  5265. with the zero padding insertion point being at
  5266. pad_ptr. */
  5267. count = count + pad; /* = end - rp */
  5268. if (flags & FLAG_LEFT)
  5269. {
  5270. /* Pad with spaces on the right. */
  5271. for (; pad > 0; pad--)
  5272. *p++ = ' ';
  5273. }
  5274. else if ((flags & FLAG_ZERO) && pad_ptr != NULL)
  5275. {
  5276. /* Pad with zeroes. */
  5277. DCHAR_T *q = end;
  5278. while (p > pad_ptr)
  5279. *--q = *--p;
  5280. for (; pad > 0; pad--)
  5281. *p++ = '0';
  5282. }
  5283. else
  5284. {
  5285. /* Pad with spaces on the left. */
  5286. DCHAR_T *q = end;
  5287. while (p > rp)
  5288. *--q = *--p;
  5289. for (; pad > 0; pad--)
  5290. *p++ = ' ';
  5291. }
  5292. }
  5293. }
  5294. }
  5295. #endif
  5296. /* Here still count <= allocated - length. */
  5297. #if !DCHAR_IS_TCHAR || USE_SNPRINTF
  5298. /* The snprintf() result did fit. */
  5299. #else
  5300. /* Append the sprintf() result. */
  5301. memcpy (result + length, tmp, count * sizeof (DCHAR_T));
  5302. #endif
  5303. #if !USE_SNPRINTF
  5304. if (tmp != tmpbuf)
  5305. free (tmp);
  5306. #endif
  5307. #if NEED_PRINTF_DIRECTIVE_F
  5308. if (dp->conversion == 'F')
  5309. {
  5310. /* Convert the %f result to upper case for %F. */
  5311. DCHAR_T *rp = result + length;
  5312. size_t rc;
  5313. for (rc = count; rc > 0; rc--, rp++)
  5314. if (*rp >= 'a' && *rp <= 'z')
  5315. *rp = *rp - 'a' + 'A';
  5316. }
  5317. #endif
  5318. length += count;
  5319. break;
  5320. }
  5321. errno = orig_errno;
  5322. #undef pad_ourselves
  5323. #undef prec_ourselves
  5324. }
  5325. }
  5326. }
  5327. /* Add the final NUL. */
  5328. ENSURE_ALLOCATION (xsum (length, 1));
  5329. result[length] = '\0';
  5330. if (result != resultbuf && length + 1 < allocated)
  5331. {
  5332. /* Shrink the allocated memory if possible. */
  5333. DCHAR_T *memory;
  5334. memory = (DCHAR_T *) realloc (result, (length + 1) * sizeof (DCHAR_T));
  5335. if (memory != NULL)
  5336. result = memory;
  5337. }
  5338. if (buf_malloced != NULL)
  5339. free (buf_malloced);
  5340. CLEANUP ();
  5341. *lengthp = length;
  5342. /* Note that we can produce a big string of a length > INT_MAX. POSIX
  5343. says that snprintf() fails with errno = EOVERFLOW in this case, but
  5344. that's only because snprintf() returns an 'int'. This function does
  5345. not have this limitation. */
  5346. return result;
  5347. #if USE_SNPRINTF
  5348. overflow:
  5349. errno = EOVERFLOW;
  5350. goto fail_with_errno;
  5351. #endif
  5352. out_of_memory:
  5353. errno = ENOMEM;
  5354. goto fail_with_errno;
  5355. #if ENABLE_UNISTDIO || ((!USE_SNPRINTF || !HAVE_SNPRINTF_RETVAL_C99 || USE_MSVC__SNPRINTF || (NEED_PRINTF_DIRECTIVE_LS && !defined IN_LIBINTL) || ENABLE_WCHAR_FALLBACK) && HAVE_WCHAR_T)
  5356. fail_with_EILSEQ:
  5357. errno = EILSEQ;
  5358. goto fail_with_errno;
  5359. #endif
  5360. fail_with_errno:
  5361. if (result != resultbuf)
  5362. free (result);
  5363. if (buf_malloced != NULL)
  5364. free (buf_malloced);
  5365. CLEANUP ();
  5366. return NULL;
  5367. }
  5368. out_of_memory_1:
  5369. errno = ENOMEM;
  5370. goto fail_1_with_errno;
  5371. fail_1_with_EINVAL:
  5372. errno = EINVAL;
  5373. goto fail_1_with_errno;
  5374. fail_1_with_errno:
  5375. CLEANUP ();
  5376. return NULL;
  5377. }
  5378. #undef MAX_ROOM_NEEDED
  5379. #undef TCHARS_PER_DCHAR
  5380. #undef SNPRINTF
  5381. #undef USE_SNPRINTF
  5382. #undef DCHAR_SET
  5383. #undef DCHAR_CPY
  5384. #undef PRINTF_PARSE
  5385. #undef DIRECTIVES
  5386. #undef DIRECTIVE
  5387. #undef DCHAR_IS_TCHAR
  5388. #undef TCHAR_T
  5389. #undef DCHAR_T
  5390. #undef FCHAR_T
  5391. #undef VASNPRINTF