vasnprintf.c 224 KB

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