vasnprintf.c 229 KB

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