vasnprintf.c 229 KB

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