vasnprintf.c 116 KB

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