vasnprintf.c 219 KB

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