vasnprintf.c 217 KB

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