vasnprintf.c 218 KB

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