vasnprintf.c 118 KB

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