vasnprintf.c 138 KB

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