vasnprintf.c 123 KB

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