vasnprintf.c 229 KB

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