vasnprintf.c 217 KB

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