yyjson.c 339 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281
  1. /*==============================================================================
  2. Copyright (c) 2020 YaoYuan <ibireme@gmail.com>
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in all
  10. copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. SOFTWARE.
  18. *============================================================================*/
  19. #include "yyjson.h"
  20. #include <math.h>
  21. /*==============================================================================
  22. * Warning Suppress
  23. *============================================================================*/
  24. #if defined(__clang__)
  25. # pragma clang diagnostic ignored "-Wunused-function"
  26. # pragma clang diagnostic ignored "-Wunused-parameter"
  27. # pragma clang diagnostic ignored "-Wunused-label"
  28. # pragma clang diagnostic ignored "-Wunused-macros"
  29. # pragma clang diagnostic ignored "-Wunused-variable"
  30. #elif defined(__GNUC__)
  31. # pragma GCC diagnostic ignored "-Wunused-function"
  32. # pragma GCC diagnostic ignored "-Wunused-parameter"
  33. # pragma GCC diagnostic ignored "-Wunused-label"
  34. # pragma GCC diagnostic ignored "-Wunused-macros"
  35. # pragma GCC diagnostic ignored "-Wunused-variable"
  36. #elif defined(_MSC_VER)
  37. # pragma warning(disable:4100) /* unreferenced formal parameter */
  38. # pragma warning(disable:4101) /* unreferenced variable */
  39. # pragma warning(disable:4102) /* unreferenced label */
  40. # pragma warning(disable:4127) /* conditional expression is constant */
  41. # pragma warning(disable:4706) /* assignment within conditional expression */
  42. #endif
  43. /*==============================================================================
  44. * Version
  45. *============================================================================*/
  46. uint32_t yyjson_version(void) {
  47. return YYJSON_VERSION_HEX;
  48. }
  49. /*==============================================================================
  50. * Flags
  51. *============================================================================*/
  52. /* msvc intrinsic */
  53. #if YYJSON_MSC_VER >= 1400
  54. # include <intrin.h>
  55. # if defined(_M_AMD64) || defined(_M_ARM64)
  56. # define MSC_HAS_BIT_SCAN_64 1
  57. # pragma intrinsic(_BitScanForward64)
  58. # pragma intrinsic(_BitScanReverse64)
  59. # else
  60. # define MSC_HAS_BIT_SCAN_64 0
  61. # endif
  62. # if defined(_M_AMD64) || defined(_M_ARM64) || \
  63. defined(_M_IX86) || defined(_M_ARM)
  64. # define MSC_HAS_BIT_SCAN 1
  65. # pragma intrinsic(_BitScanForward)
  66. # pragma intrinsic(_BitScanReverse)
  67. # else
  68. # define MSC_HAS_BIT_SCAN 0
  69. # endif
  70. # if defined(_M_AMD64)
  71. # define MSC_HAS_UMUL128 1
  72. # pragma intrinsic(_umul128)
  73. # else
  74. # define MSC_HAS_UMUL128 0
  75. # endif
  76. #else
  77. # define MSC_HAS_BIT_SCAN_64 0
  78. # define MSC_HAS_BIT_SCAN 0
  79. # define MSC_HAS_UMUL128 0
  80. #endif
  81. /* gcc builtin */
  82. #if yyjson_has_builtin(__builtin_clzll) || yyjson_gcc_available(3, 4, 0)
  83. # define GCC_HAS_CLZLL 1
  84. #else
  85. # define GCC_HAS_CLZLL 0
  86. #endif
  87. #if yyjson_has_builtin(__builtin_ctzll) || yyjson_gcc_available(3, 4, 0)
  88. # define GCC_HAS_CTZLL 1
  89. #else
  90. # define GCC_HAS_CTZLL 0
  91. #endif
  92. /* int128 type */
  93. #if defined(__SIZEOF_INT128__) && (__SIZEOF_INT128__ == 16) && \
  94. (defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER))
  95. # define YYJSON_HAS_INT128 1
  96. #else
  97. # define YYJSON_HAS_INT128 0
  98. #endif
  99. /* IEEE 754 floating-point binary representation */
  100. #if defined(__STDC_IEC_559__) || defined(__STDC_IEC_60559_BFP__)
  101. # define YYJSON_HAS_IEEE_754 1
  102. #elif (FLT_RADIX == 2) && (DBL_MANT_DIG == 53) && (DBL_DIG == 15) && \
  103. (DBL_MIN_EXP == -1021) && (DBL_MAX_EXP == 1024) && \
  104. (DBL_MIN_10_EXP == -307) && (DBL_MAX_10_EXP == 308)
  105. # define YYJSON_HAS_IEEE_754 1
  106. #else
  107. # define YYJSON_HAS_IEEE_754 0
  108. #endif
  109. /*
  110. Correct rounding in double number computations.
  111. On the x86 architecture, some compilers may use x87 FPU instructions for
  112. floating-point arithmetic. The x87 FPU loads all floating point number as
  113. 80-bit double-extended precision internally, then rounds the result to original
  114. precision, which may produce inaccurate results. For a more detailed
  115. explanation, see the paper: https://arxiv.org/abs/cs/0701192
  116. Here are some examples of double precision calculation error:
  117. 2877.0 / 1e6 == 0.002877, but x87 returns 0.0028770000000000002
  118. 43683.0 * 1e21 == 4.3683e25, but x87 returns 4.3683000000000004e25
  119. Here are some examples of compiler flags to generate x87 instructions on x86:
  120. clang -m32 -mno-sse
  121. gcc/icc -m32 -mfpmath=387
  122. msvc /arch:SSE or /arch:IA32
  123. If we are sure that there's no similar error described above, we can define the
  124. YYJSON_DOUBLE_MATH_CORRECT as 1 to enable the fast path calculation. This is
  125. not an accurate detection, it's just try to avoid the error at compile-time.
  126. An accurate detection can be done at run-time:
  127. bool is_double_math_correct(void) {
  128. volatile double r = 43683.0;
  129. r *= 1e21;
  130. return r == 4.3683e25;
  131. }
  132. See also: utils.h in https://github.com/google/double-conversion/
  133. */
  134. #if !defined(FLT_EVAL_METHOD) && defined(__FLT_EVAL_METHOD__)
  135. # define FLT_EVAL_METHOD __FLT_EVAL_METHOD__
  136. #endif
  137. #if defined(FLT_EVAL_METHOD) && FLT_EVAL_METHOD != 0 && FLT_EVAL_METHOD != 1
  138. # define YYJSON_DOUBLE_MATH_CORRECT 0
  139. #elif defined(i386) || defined(__i386) || defined(__i386__) || \
  140. defined(_X86_) || defined(__X86__) || defined(_M_IX86) || \
  141. defined(__I86__) || defined(__IA32__) || defined(__THW_INTEL)
  142. # if (defined(_MSC_VER) && defined(_M_IX86_FP) && _M_IX86_FP == 2) || \
  143. (defined(__SSE2_MATH__) && __SSE2_MATH__)
  144. # define YYJSON_DOUBLE_MATH_CORRECT 1
  145. # else
  146. # define YYJSON_DOUBLE_MATH_CORRECT 0
  147. # endif
  148. #elif defined(__mc68000__) || defined(__pnacl__) || defined(__native_client__)
  149. # define YYJSON_DOUBLE_MATH_CORRECT 0
  150. #else
  151. # define YYJSON_DOUBLE_MATH_CORRECT 1
  152. #endif
  153. /* endian */
  154. #if yyjson_has_include(<sys/types.h>)
  155. # include <sys/types.h> /* POSIX */
  156. #endif
  157. #if yyjson_has_include(<endian.h>)
  158. # include <endian.h> /* Linux */
  159. #elif yyjson_has_include(<sys/endian.h>)
  160. # include <sys/endian.h> /* BSD, Android */
  161. #elif yyjson_has_include(<machine/endian.h>)
  162. # include <machine/endian.h> /* BSD, Darwin */
  163. #endif
  164. #define YYJSON_BIG_ENDIAN 4321
  165. #define YYJSON_LITTLE_ENDIAN 1234
  166. #if defined(BYTE_ORDER) && BYTE_ORDER
  167. # if defined(BIG_ENDIAN) && (BYTE_ORDER == BIG_ENDIAN)
  168. # define YYJSON_ENDIAN YYJSON_BIG_ENDIAN
  169. # elif defined(LITTLE_ENDIAN) && (BYTE_ORDER == LITTLE_ENDIAN)
  170. # define YYJSON_ENDIAN YYJSON_LITTLE_ENDIAN
  171. # endif
  172. #elif defined(__BYTE_ORDER) && __BYTE_ORDER
  173. # if defined(__BIG_ENDIAN) && (__BYTE_ORDER == __BIG_ENDIAN)
  174. # define YYJSON_ENDIAN YYJSON_BIG_ENDIAN
  175. # elif defined(__LITTLE_ENDIAN) && (__BYTE_ORDER == __LITTLE_ENDIAN)
  176. # define YYJSON_ENDIAN YYJSON_LITTLE_ENDIAN
  177. # endif
  178. #elif defined(__BYTE_ORDER__) && __BYTE_ORDER__
  179. # if defined(__ORDER_BIG_ENDIAN__) && \
  180. (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  181. # define YYJSON_ENDIAN YYJSON_BIG_ENDIAN
  182. # elif defined(__ORDER_LITTLE_ENDIAN__) && \
  183. (__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
  184. # define YYJSON_ENDIAN YYJSON_LITTLE_ENDIAN
  185. # endif
  186. #elif (defined(__LITTLE_ENDIAN__) && __LITTLE_ENDIAN__ == 1) || \
  187. defined(__i386) || defined(__i386__) || \
  188. defined(_X86_) || defined(__X86__) || \
  189. defined(_M_IX86) || defined(__THW_INTEL__) || \
  190. defined(__x86_64) || defined(__x86_64__) || \
  191. defined(__amd64) || defined(__amd64__) || \
  192. defined(_M_AMD64) || defined(_M_X64) || \
  193. defined(_M_ARM) || defined(_M_ARM64) || \
  194. defined(__ARMEL__) || defined(__THUMBEL__) || defined(__AARCH64EL__) || \
  195. defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__) || \
  196. defined(__EMSCRIPTEN__) || defined(__wasm__) || \
  197. defined(__loongarch__)
  198. # define YYJSON_ENDIAN YYJSON_LITTLE_ENDIAN
  199. #elif (defined(__BIG_ENDIAN__) && __BIG_ENDIAN__ == 1) || \
  200. defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || \
  201. defined(_MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__) || \
  202. defined(__or1k__) || defined(__OR1K__)
  203. # define YYJSON_ENDIAN YYJSON_BIG_ENDIAN
  204. #else
  205. # define YYJSON_ENDIAN 0 /* unknown endian, detect at run-time */
  206. #endif
  207. /*
  208. This macro controls how yyjson handles unaligned memory accesses.
  209. By default, yyjson uses `memcpy()` for memory copying. This takes advantage of
  210. the compiler's automatic optimizations to generate unaligned memory access
  211. instructions when the target architecture supports it.
  212. However, for some older compilers or architectures where `memcpy()` isn't
  213. optimized well and may generate unnecessary function calls, consider defining
  214. this macro as 1. In such cases, yyjson switches to manual byte-by-byte access,
  215. potentially improving performance. An example of the generated assembly code on
  216. the ARM platform can be found here: https://godbolt.org/z/334jjhxPT
  217. As this flag has already been enabled for some common architectures in the
  218. following code, users typically don't need to manually specify it. If users are
  219. unsure about it, please review the generated assembly code or perform actual
  220. benchmark to make an informed decision.
  221. */
  222. #ifndef YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS
  223. # if defined(__ia64) || defined(_IA64) || defined(__IA64__) || \
  224. defined(__ia64__) || defined(_M_IA64) || defined(__itanium__)
  225. # define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* Itanium */
  226. # elif (defined(__arm__) || defined(__arm64__) || defined(__aarch64__)) && \
  227. (defined(__GNUC__) || defined(__clang__)) && \
  228. (!defined(__ARM_FEATURE_UNALIGNED) || !__ARM_FEATURE_UNALIGNED)
  229. # define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* ARM */
  230. # elif defined(__sparc) || defined(__sparc__)
  231. # define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* SPARC */
  232. # elif defined(__mips) || defined(__mips__) || defined(__MIPS__)
  233. # define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* MIPS */
  234. # elif defined(__m68k__) || defined(M68000)
  235. # define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* M68K */
  236. # else
  237. # define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 0
  238. # endif
  239. #endif
  240. /*
  241. Estimated initial ratio of the JSON data (data_size / value_count).
  242. For example:
  243. data: {"id":12345678,"name":"Harry"}
  244. data_size: 30
  245. value_count: 5
  246. ratio: 6
  247. yyjson uses dynamic memory with a growth factor of 1.5 when reading and writing
  248. JSON, the ratios below are used to determine the initial memory size.
  249. A too large ratio will waste memory, and a too small ratio will cause multiple
  250. memory growths and degrade performance. Currently, these ratios are generated
  251. with some commonly used JSON datasets.
  252. */
  253. #define YYJSON_READER_ESTIMATED_PRETTY_RATIO 16
  254. #define YYJSON_READER_ESTIMATED_MINIFY_RATIO 6
  255. #define YYJSON_WRITER_ESTIMATED_PRETTY_RATIO 32
  256. #define YYJSON_WRITER_ESTIMATED_MINIFY_RATIO 18
  257. /* The initial and maximum size of the memory pool's chunk in yyjson_mut_doc. */
  258. #define YYJSON_MUT_DOC_STR_POOL_INIT_SIZE 0x100
  259. #define YYJSON_MUT_DOC_STR_POOL_MAX_SIZE 0x10000000
  260. #define YYJSON_MUT_DOC_VAL_POOL_INIT_SIZE (0x10 * sizeof(yyjson_mut_val))
  261. #define YYJSON_MUT_DOC_VAL_POOL_MAX_SIZE (0x1000000 * sizeof(yyjson_mut_val))
  262. /* Default value for compile-time options. */
  263. #ifndef YYJSON_DISABLE_READER
  264. #define YYJSON_DISABLE_READER 0
  265. #endif
  266. #ifndef YYJSON_DISABLE_WRITER
  267. #define YYJSON_DISABLE_WRITER 0
  268. #endif
  269. #ifndef YYJSON_DISABLE_UTILS
  270. #define YYJSON_DISABLE_UTILS 0
  271. #endif
  272. #ifndef YYJSON_DISABLE_FAST_FP_CONV
  273. #define YYJSON_DISABLE_FAST_FP_CONV 0
  274. #endif
  275. #ifndef YYJSON_DISABLE_NON_STANDARD
  276. #define YYJSON_DISABLE_NON_STANDARD 0
  277. #endif
  278. #ifndef YYJSON_DISABLE_UTF8_VALIDATION
  279. #define YYJSON_DISABLE_UTF8_VALIDATION 0
  280. #endif
  281. /*==============================================================================
  282. * Macros
  283. *============================================================================*/
  284. /* Macros used for loop unrolling and other purpose. */
  285. #define repeat2(x) { x x }
  286. #define repeat3(x) { x x x }
  287. #define repeat4(x) { x x x x }
  288. #define repeat8(x) { x x x x x x x x }
  289. #define repeat16(x) { x x x x x x x x x x x x x x x x }
  290. #define repeat2_incr(x) { x(0) x(1) }
  291. #define repeat4_incr(x) { x(0) x(1) x(2) x(3) }
  292. #define repeat8_incr(x) { x(0) x(1) x(2) x(3) x(4) x(5) x(6) x(7) }
  293. #define repeat16_incr(x) { x(0) x(1) x(2) x(3) x(4) x(5) x(6) x(7) \
  294. x(8) x(9) x(10) x(11) x(12) x(13) x(14) x(15) }
  295. #define repeat_in_1_18(x) { x(1) x(2) x(3) x(4) x(5) x(6) x(7) x(8) \
  296. x(9) x(10) x(11) x(12) x(13) x(14) x(15) x(16) \
  297. x(17) x(18) }
  298. /* Macros used to provide branch prediction information for compiler. */
  299. #undef likely
  300. #define likely(x) yyjson_likely(x)
  301. #undef unlikely
  302. #define unlikely(x) yyjson_unlikely(x)
  303. /* Macros used to provide inline information for compiler. */
  304. #undef static_inline
  305. #define static_inline static yyjson_inline
  306. #undef static_noinline
  307. #define static_noinline static yyjson_noinline
  308. /* Macros for min and max. */
  309. #undef yyjson_min
  310. #define yyjson_min(x, y) ((x) < (y) ? (x) : (y))
  311. #undef yyjson_max
  312. #define yyjson_max(x, y) ((x) > (y) ? (x) : (y))
  313. /* Used to write u64 literal for C89 which doesn't support "ULL" suffix. */
  314. #undef U64
  315. #define U64(hi, lo) ((((u64)hi##UL) << 32U) + lo##UL)
  316. /* Used to cast away (remove) const qualifier. */
  317. #define constcast(type) (type)(void *)(size_t)(const void *)
  318. /* flag test */
  319. #define has_read_flag(_flag) unlikely(read_flag_eq(flg, YYJSON_READ_##_flag))
  320. #define has_write_flag(_flag) unlikely(write_flag_eq(flg, YYJSON_WRITE_##_flag))
  321. static_inline bool read_flag_eq(yyjson_read_flag flg, yyjson_read_flag chk) {
  322. #if YYJSON_DISABLE_NON_STANDARD
  323. if (chk == YYJSON_READ_ALLOW_INF_AND_NAN ||
  324. chk == YYJSON_READ_ALLOW_COMMENTS ||
  325. chk == YYJSON_READ_ALLOW_TRAILING_COMMAS ||
  326. chk == YYJSON_READ_ALLOW_INVALID_UNICODE)
  327. return false; /* this should be evaluated at compile-time */
  328. #endif
  329. return (flg & chk) != 0;
  330. }
  331. static_inline bool write_flag_eq(yyjson_write_flag flg, yyjson_write_flag chk) {
  332. #if YYJSON_DISABLE_NON_STANDARD
  333. if (chk == YYJSON_WRITE_ALLOW_INF_AND_NAN ||
  334. chk == YYJSON_WRITE_ALLOW_INVALID_UNICODE)
  335. return false; /* this should be evaluated at compile-time */
  336. #endif
  337. return (flg & chk) != 0;
  338. }
  339. /*==============================================================================
  340. * Integer Constants
  341. *============================================================================*/
  342. /* U64 constant values */
  343. #undef U64_MAX
  344. #define U64_MAX U64(0xFFFFFFFF, 0xFFFFFFFF)
  345. #undef I64_MAX
  346. #define I64_MAX U64(0x7FFFFFFF, 0xFFFFFFFF)
  347. #undef USIZE_MAX
  348. #define USIZE_MAX ((usize)(~(usize)0))
  349. /* Maximum number of digits for reading u32/u64/usize safety (not overflow). */
  350. #undef U32_SAFE_DIG
  351. #define U32_SAFE_DIG 9 /* u32 max is 4294967295, 10 digits */
  352. #undef U64_SAFE_DIG
  353. #define U64_SAFE_DIG 19 /* u64 max is 18446744073709551615, 20 digits */
  354. #undef USIZE_SAFE_DIG
  355. #define USIZE_SAFE_DIG (sizeof(usize) == 8 ? U64_SAFE_DIG : U32_SAFE_DIG)
  356. /*==============================================================================
  357. * IEEE-754 Double Number Constants
  358. *============================================================================*/
  359. /* Inf raw value (positive) */
  360. #define F64_RAW_INF U64(0x7FF00000, 0x00000000)
  361. /* NaN raw value (quiet NaN, no payload, no sign) */
  362. #if defined(__hppa__) || (defined(__mips__) && !defined(__mips_nan2008))
  363. #define F64_RAW_NAN U64(0x7FF7FFFF, 0xFFFFFFFF)
  364. #else
  365. #define F64_RAW_NAN U64(0x7FF80000, 0x00000000)
  366. #endif
  367. /* double number bits */
  368. #define F64_BITS 64
  369. /* double number exponent part bits */
  370. #define F64_EXP_BITS 11
  371. /* double number significand part bits */
  372. #define F64_SIG_BITS 52
  373. /* double number significand part bits (with 1 hidden bit) */
  374. #define F64_SIG_FULL_BITS 53
  375. /* double number significand bit mask */
  376. #define F64_SIG_MASK U64(0x000FFFFF, 0xFFFFFFFF)
  377. /* double number exponent bit mask */
  378. #define F64_EXP_MASK U64(0x7FF00000, 0x00000000)
  379. /* double number exponent bias */
  380. #define F64_EXP_BIAS 1023
  381. /* double number significant digits count in decimal */
  382. #define F64_DEC_DIG 17
  383. /* max significant digits count in decimal when reading double number */
  384. #define F64_MAX_DEC_DIG 768
  385. /* maximum decimal power of double number (1.7976931348623157e308) */
  386. #define F64_MAX_DEC_EXP 308
  387. /* minimum decimal power of double number (4.9406564584124654e-324) */
  388. #define F64_MIN_DEC_EXP (-324)
  389. /* maximum binary power of double number */
  390. #define F64_MAX_BIN_EXP 1024
  391. /* minimum binary power of double number */
  392. #define F64_MIN_BIN_EXP (-1021)
  393. /*==============================================================================
  394. * Types
  395. *============================================================================*/
  396. /** Type define for primitive types. */
  397. typedef float f32;
  398. typedef double f64;
  399. typedef int8_t i8;
  400. typedef uint8_t u8;
  401. typedef int16_t i16;
  402. typedef uint16_t u16;
  403. typedef int32_t i32;
  404. typedef uint32_t u32;
  405. typedef int64_t i64;
  406. typedef uint64_t u64;
  407. typedef size_t usize;
  408. /** 128-bit integer, used by floating-point number reader and writer. */
  409. #if YYJSON_HAS_INT128
  410. __extension__ typedef __int128 i128;
  411. __extension__ typedef unsigned __int128 u128;
  412. #endif
  413. /** 16/32/64-bit vector */
  414. typedef struct v16 { char c[2]; } v16;
  415. typedef struct v32 { char c[4]; } v32;
  416. typedef struct v64 { char c[8]; } v64;
  417. /** 16/32/64-bit vector union */
  418. typedef union v16_uni { v16 v; u16 u; } v16_uni;
  419. typedef union v32_uni { v32 v; u32 u; } v32_uni;
  420. typedef union v64_uni { v64 v; u64 u; } v64_uni;
  421. /*==============================================================================
  422. * Load/Store Utils
  423. *============================================================================*/
  424. #if YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS
  425. #define byte_move_idx(x) ((char *)dst)[x] = ((const char *)src)[x];
  426. static_inline void byte_copy_2(void *dst, const void *src) {
  427. repeat2_incr(byte_move_idx)
  428. }
  429. static_inline void byte_copy_4(void *dst, const void *src) {
  430. repeat4_incr(byte_move_idx)
  431. }
  432. static_inline void byte_copy_8(void *dst, const void *src) {
  433. repeat8_incr(byte_move_idx)
  434. }
  435. static_inline void byte_copy_16(void *dst, const void *src) {
  436. repeat16_incr(byte_move_idx)
  437. }
  438. static_inline void byte_move_2(void *dst, const void *src) {
  439. repeat2_incr(byte_move_idx)
  440. }
  441. static_inline void byte_move_4(void *dst, const void *src) {
  442. repeat4_incr(byte_move_idx)
  443. }
  444. static_inline void byte_move_8(void *dst, const void *src) {
  445. repeat8_incr(byte_move_idx)
  446. }
  447. static_inline void byte_move_16(void *dst, const void *src) {
  448. repeat16_incr(byte_move_idx)
  449. }
  450. static_inline bool byte_match_2(void *buf, const char *pat) {
  451. return
  452. ((char *)buf)[0] == ((const char *)pat)[0] &&
  453. ((char *)buf)[1] == ((const char *)pat)[1];
  454. }
  455. static_inline bool byte_match_4(void *buf, const char *pat) {
  456. return
  457. ((char *)buf)[0] == ((const char *)pat)[0] &&
  458. ((char *)buf)[1] == ((const char *)pat)[1] &&
  459. ((char *)buf)[2] == ((const char *)pat)[2] &&
  460. ((char *)buf)[3] == ((const char *)pat)[3];
  461. }
  462. static_inline u16 byte_load_2(const void *src) {
  463. v16_uni uni;
  464. uni.v.c[0] = ((const char *)src)[0];
  465. uni.v.c[1] = ((const char *)src)[1];
  466. return uni.u;
  467. }
  468. static_inline u32 byte_load_3(const void *src) {
  469. v32_uni uni;
  470. uni.v.c[0] = ((const char *)src)[0];
  471. uni.v.c[1] = ((const char *)src)[1];
  472. uni.v.c[2] = ((const char *)src)[2];
  473. uni.v.c[3] = 0;
  474. return uni.u;
  475. }
  476. static_inline u32 byte_load_4(const void *src) {
  477. v32_uni uni;
  478. uni.v.c[0] = ((const char *)src)[0];
  479. uni.v.c[1] = ((const char *)src)[1];
  480. uni.v.c[2] = ((const char *)src)[2];
  481. uni.v.c[3] = ((const char *)src)[3];
  482. return uni.u;
  483. }
  484. #undef byte_move_expr
  485. #else
  486. static_inline void byte_copy_2(void *dst, const void *src) {
  487. memcpy(dst, src, 2);
  488. }
  489. static_inline void byte_copy_4(void *dst, const void *src) {
  490. memcpy(dst, src, 4);
  491. }
  492. static_inline void byte_copy_8(void *dst, const void *src) {
  493. memcpy(dst, src, 8);
  494. }
  495. static_inline void byte_copy_16(void *dst, const void *src) {
  496. memcpy(dst, src, 16);
  497. }
  498. static_inline void byte_move_2(void *dst, const void *src) {
  499. u16 tmp;
  500. memcpy(&tmp, src, 2);
  501. memcpy(dst, &tmp, 2);
  502. }
  503. static_inline void byte_move_4(void *dst, const void *src) {
  504. u32 tmp;
  505. memcpy(&tmp, src, 4);
  506. memcpy(dst, &tmp, 4);
  507. }
  508. static_inline void byte_move_8(void *dst, const void *src) {
  509. u64 tmp;
  510. memcpy(&tmp, src, 8);
  511. memcpy(dst, &tmp, 8);
  512. }
  513. static_inline void byte_move_16(void *dst, const void *src) {
  514. char *pdst = (char *)dst;
  515. const char *psrc = (const char *)src;
  516. u64 tmp1, tmp2;
  517. memcpy(&tmp1, psrc, 8);
  518. memcpy(&tmp2, psrc + 8, 8);
  519. memcpy(pdst, &tmp1, 8);
  520. memcpy(pdst + 8, &tmp2, 8);
  521. }
  522. static_inline bool byte_match_2(void *buf, const char *pat) {
  523. v16_uni u1, u2;
  524. memcpy(&u1, buf, 2);
  525. memcpy(&u2, pat, 2);
  526. return u1.u == u2.u;
  527. }
  528. static_inline bool byte_match_4(void *buf, const char *pat) {
  529. v32_uni u1, u2;
  530. memcpy(&u1, buf, 4);
  531. memcpy(&u2, pat, 4);
  532. return u1.u == u2.u;
  533. }
  534. static_inline u16 byte_load_2(const void *src) {
  535. v16_uni uni;
  536. memcpy(&uni, src, 2);
  537. return uni.u;
  538. }
  539. static_inline u32 byte_load_3(const void *src) {
  540. v32_uni uni;
  541. memcpy(&uni, src, 2);
  542. uni.v.c[2] = ((const char *)src)[2];
  543. uni.v.c[3] = 0;
  544. return uni.u;
  545. }
  546. static_inline u32 byte_load_4(const void *src) {
  547. v32_uni uni;
  548. memcpy(&uni, src, 4);
  549. return uni.u;
  550. }
  551. #endif
  552. /*==============================================================================
  553. * Number Utils
  554. * These functions are used to detect and convert NaN and Inf numbers.
  555. *============================================================================*/
  556. /** Convert raw binary to double. */
  557. static_inline f64 f64_from_raw(u64 u) {
  558. /* use memcpy to avoid violating the strict aliasing rule */
  559. f64 f;
  560. memcpy(&f, &u, 8);
  561. return f;
  562. }
  563. /** Convert double to raw binary. */
  564. static_inline u64 f64_to_raw(f64 f) {
  565. /* use memcpy to avoid violating the strict aliasing rule */
  566. u64 u;
  567. memcpy(&u, &f, 8);
  568. return u;
  569. }
  570. /** Get raw 'infinity' with sign. */
  571. static_inline u64 f64_raw_get_inf(bool sign) {
  572. #if YYJSON_HAS_IEEE_754
  573. return F64_RAW_INF | ((u64)sign << 63);
  574. #elif defined(INFINITY)
  575. return f64_to_raw(sign ? -INFINITY : INFINITY);
  576. #else
  577. return f64_to_raw(sign ? -HUGE_VAL : HUGE_VAL);
  578. #endif
  579. }
  580. /** Get raw 'nan' with sign. */
  581. static_inline u64 f64_raw_get_nan(bool sign) {
  582. #if YYJSON_HAS_IEEE_754
  583. return F64_RAW_NAN | ((u64)sign << 63);
  584. #elif defined(NAN)
  585. return f64_to_raw(sign ? (f64)-NAN : (f64)NAN);
  586. #else
  587. return f64_to_raw((sign ? -0.0 : 0.0) / 0.0);
  588. #endif
  589. }
  590. /**
  591. Convert normalized u64 (highest bit is 1) to f64.
  592. Some compiler (such as Microsoft Visual C++ 6.0) do not support converting
  593. number from u64 to f64. This function will first convert u64 to i64 and then
  594. to f64, with `to nearest` rounding mode.
  595. */
  596. static_inline f64 normalized_u64_to_f64(u64 val) {
  597. #if YYJSON_U64_TO_F64_NO_IMPL
  598. i64 sig = (i64)((val >> 1) | (val & 1));
  599. return ((f64)sig) * (f64)2.0;
  600. #else
  601. return (f64)val;
  602. #endif
  603. }
  604. /*==============================================================================
  605. * Size Utils
  606. * These functions are used for memory allocation.
  607. *============================================================================*/
  608. /** Returns whether the size is overflow after increment. */
  609. static_inline bool size_add_is_overflow(usize size, usize add) {
  610. return size > (size + add);
  611. }
  612. /** Returns whether the size is power of 2 (size should not be 0). */
  613. static_inline bool size_is_pow2(usize size) {
  614. return (size & (size - 1)) == 0;
  615. }
  616. /** Align size upwards (may overflow). */
  617. static_inline usize size_align_up(usize size, usize align) {
  618. if (size_is_pow2(align)) {
  619. return (size + (align - 1)) & ~(align - 1);
  620. } else {
  621. return size + align - (size + align - 1) % align - 1;
  622. }
  623. }
  624. /** Align size downwards. */
  625. static_inline usize size_align_down(usize size, usize align) {
  626. if (size_is_pow2(align)) {
  627. return size & ~(align - 1);
  628. } else {
  629. return size - (size % align);
  630. }
  631. }
  632. /** Align address upwards (may overflow). */
  633. static_inline void *mem_align_up(void *mem, usize align) {
  634. usize size;
  635. memcpy(&size, &mem, sizeof(usize));
  636. size = size_align_up(size, align);
  637. memcpy(&mem, &size, sizeof(usize));
  638. return mem;
  639. }
  640. /*==============================================================================
  641. * Bits Utils
  642. * These functions are used by the floating-point number reader and writer.
  643. *============================================================================*/
  644. /** Returns the number of leading 0-bits in value (input should not be 0). */
  645. static_inline u32 u64_lz_bits(u64 v) {
  646. #if GCC_HAS_CLZLL
  647. return (u32)__builtin_clzll(v);
  648. #elif MSC_HAS_BIT_SCAN_64
  649. unsigned long r;
  650. _BitScanReverse64(&r, v);
  651. return (u32)63 - (u32)r;
  652. #elif MSC_HAS_BIT_SCAN
  653. unsigned long hi, lo;
  654. bool hi_set = _BitScanReverse(&hi, (u32)(v >> 32)) != 0;
  655. _BitScanReverse(&lo, (u32)v);
  656. hi |= 32;
  657. return (u32)63 - (u32)(hi_set ? hi : lo);
  658. #else
  659. /*
  660. branchless, use de Bruijn sequences
  661. see: https://www.chessprogramming.org/BitScan
  662. */
  663. const u8 table[64] = {
  664. 63, 16, 62, 7, 15, 36, 61, 3, 6, 14, 22, 26, 35, 47, 60, 2,
  665. 9, 5, 28, 11, 13, 21, 42, 19, 25, 31, 34, 40, 46, 52, 59, 1,
  666. 17, 8, 37, 4, 23, 27, 48, 10, 29, 12, 43, 20, 32, 41, 53, 18,
  667. 38, 24, 49, 30, 44, 33, 54, 39, 50, 45, 55, 51, 56, 57, 58, 0
  668. };
  669. v |= v >> 1;
  670. v |= v >> 2;
  671. v |= v >> 4;
  672. v |= v >> 8;
  673. v |= v >> 16;
  674. v |= v >> 32;
  675. return table[(v * U64(0x03F79D71, 0xB4CB0A89)) >> 58];
  676. #endif
  677. }
  678. /** Returns the number of trailing 0-bits in value (input should not be 0). */
  679. static_inline u32 u64_tz_bits(u64 v) {
  680. #if GCC_HAS_CTZLL
  681. return (u32)__builtin_ctzll(v);
  682. #elif MSC_HAS_BIT_SCAN_64
  683. unsigned long r;
  684. _BitScanForward64(&r, v);
  685. return (u32)r;
  686. #elif MSC_HAS_BIT_SCAN
  687. unsigned long lo, hi;
  688. bool lo_set = _BitScanForward(&lo, (u32)(v)) != 0;
  689. _BitScanForward(&hi, (u32)(v >> 32));
  690. hi += 32;
  691. return lo_set ? lo : hi;
  692. #else
  693. /*
  694. branchless, use de Bruijn sequences
  695. see: https://www.chessprogramming.org/BitScan
  696. */
  697. const u8 table[64] = {
  698. 0, 1, 2, 53, 3, 7, 54, 27, 4, 38, 41, 8, 34, 55, 48, 28,
  699. 62, 5, 39, 46, 44, 42, 22, 9, 24, 35, 59, 56, 49, 18, 29, 11,
  700. 63, 52, 6, 26, 37, 40, 33, 47, 61, 45, 43, 21, 23, 58, 17, 10,
  701. 51, 25, 36, 32, 60, 20, 57, 16, 50, 31, 19, 15, 30, 14, 13, 12
  702. };
  703. return table[((v & (~v + 1)) * U64(0x022FDD63, 0xCC95386D)) >> 58];
  704. #endif
  705. }
  706. /*==============================================================================
  707. * 128-bit Integer Utils
  708. * These functions are used by the floating-point number reader and writer.
  709. *============================================================================*/
  710. /** Multiplies two 64-bit unsigned integers (a * b),
  711. returns the 128-bit result as 'hi' and 'lo'. */
  712. static_inline void u128_mul(u64 a, u64 b, u64 *hi, u64 *lo) {
  713. #if YYJSON_HAS_INT128
  714. u128 m = (u128)a * b;
  715. *hi = (u64)(m >> 64);
  716. *lo = (u64)(m);
  717. #elif MSC_HAS_UMUL128
  718. *lo = _umul128(a, b, hi);
  719. #else
  720. u32 a0 = (u32)(a), a1 = (u32)(a >> 32);
  721. u32 b0 = (u32)(b), b1 = (u32)(b >> 32);
  722. u64 p00 = (u64)a0 * b0, p01 = (u64)a0 * b1;
  723. u64 p10 = (u64)a1 * b0, p11 = (u64)a1 * b1;
  724. u64 m0 = p01 + (p00 >> 32);
  725. u32 m00 = (u32)(m0), m01 = (u32)(m0 >> 32);
  726. u64 m1 = p10 + m00;
  727. u32 m10 = (u32)(m1), m11 = (u32)(m1 >> 32);
  728. *hi = p11 + m01 + m11;
  729. *lo = ((u64)m10 << 32) | (u32)p00;
  730. #endif
  731. }
  732. /** Multiplies two 64-bit unsigned integers and add a value (a * b + c),
  733. returns the 128-bit result as 'hi' and 'lo'. */
  734. static_inline void u128_mul_add(u64 a, u64 b, u64 c, u64 *hi, u64 *lo) {
  735. #if YYJSON_HAS_INT128
  736. u128 m = (u128)a * b + c;
  737. *hi = (u64)(m >> 64);
  738. *lo = (u64)(m);
  739. #else
  740. u64 h, l, t;
  741. u128_mul(a, b, &h, &l);
  742. t = l + c;
  743. h += (u64)(((t < l) | (t < c)));
  744. *hi = h;
  745. *lo = t;
  746. #endif
  747. }
  748. /*==============================================================================
  749. * File Utils
  750. * These functions are used to read and write JSON files.
  751. *============================================================================*/
  752. #define YYJSON_FOPEN_EXT
  753. #if !defined(_MSC_VER) && defined(__GLIBC__) && defined(__GLIBC_PREREQ)
  754. # if __GLIBC_PREREQ(2, 7)
  755. # undef YYJSON_FOPEN_EXT
  756. # define YYJSON_FOPEN_EXT "e" /* glibc extension to enable O_CLOEXEC */
  757. # endif
  758. #endif
  759. static_inline FILE *fopen_safe(const char *path, const char *mode) {
  760. #if YYJSON_MSC_VER >= 1400
  761. FILE *file = NULL;
  762. if (fopen_s(&file, path, mode) != 0) return NULL;
  763. return file;
  764. #else
  765. return fopen(path, mode);
  766. #endif
  767. }
  768. static_inline FILE *fopen_readonly(const char *path) {
  769. return fopen_safe(path, "rb" YYJSON_FOPEN_EXT);
  770. }
  771. static_inline FILE *fopen_writeonly(const char *path) {
  772. return fopen_safe(path, "wb" YYJSON_FOPEN_EXT);
  773. }
  774. static_inline usize fread_safe(void *buf, usize size, FILE *file) {
  775. #if YYJSON_MSC_VER >= 1400
  776. return fread_s(buf, size, 1, size, file);
  777. #else
  778. return fread(buf, 1, size, file);
  779. #endif
  780. }
  781. /*==============================================================================
  782. * Default Memory Allocator
  783. * This is a simple libc memory allocator wrapper.
  784. *============================================================================*/
  785. static void *default_malloc(void *ctx, usize size) {
  786. return malloc(size);
  787. }
  788. static void *default_realloc(void *ctx, void *ptr, usize old_size, usize size) {
  789. return realloc(ptr, size);
  790. }
  791. static void default_free(void *ctx, void *ptr) {
  792. free(ptr);
  793. }
  794. static const yyjson_alc YYJSON_DEFAULT_ALC = {
  795. default_malloc,
  796. default_realloc,
  797. default_free,
  798. NULL
  799. };
  800. static void *null_malloc(void *ctx, usize size) {
  801. return NULL;
  802. }
  803. static void *null_realloc(void *ctx, void *ptr, usize old_size, usize size) {
  804. return NULL;
  805. }
  806. static void null_free(void *ctx, void *ptr) {
  807. return;
  808. }
  809. static const yyjson_alc YYJSON_NULL_ALC = {
  810. null_malloc,
  811. null_realloc,
  812. null_free,
  813. NULL
  814. };
  815. /*==============================================================================
  816. * Pool Memory Allocator
  817. * This is a simple memory allocator that uses linked list memory chunk.
  818. * The following code will be executed only when the library user creates
  819. * this allocator manually.
  820. *============================================================================*/
  821. /** chunk header */
  822. typedef struct pool_chunk {
  823. usize size; /* chunk memory size (include chunk header) */
  824. struct pool_chunk *next;
  825. } pool_chunk;
  826. /** ctx header */
  827. typedef struct pool_ctx {
  828. usize size; /* total memory size (include ctx header) */
  829. pool_chunk *free_list;
  830. } pool_ctx;
  831. static void *pool_malloc(void *ctx_ptr, usize size) {
  832. pool_ctx *ctx = (pool_ctx *)ctx_ptr;
  833. pool_chunk *next, *prev = NULL, *cur = ctx->free_list;
  834. if (unlikely(size == 0 || size >= ctx->size)) return NULL;
  835. size = size_align_up(size, sizeof(pool_chunk)) + sizeof(pool_chunk);
  836. while (cur) {
  837. if (cur->size < size) {
  838. /* not enough space, try next chunk */
  839. prev = cur;
  840. cur = cur->next;
  841. continue;
  842. }
  843. if (cur->size >= size + sizeof(pool_chunk) * 2) {
  844. /* too much space, split this chunk */
  845. next = (pool_chunk *)(void *)((u8 *)cur + size);
  846. next->size = cur->size - size;
  847. next->next = cur->next;
  848. cur->size = size;
  849. } else {
  850. /* just enough space, use whole chunk */
  851. next = cur->next;
  852. }
  853. if (prev) prev->next = next;
  854. else ctx->free_list = next;
  855. return (void *)(cur + 1);
  856. }
  857. return NULL;
  858. }
  859. static void pool_free(void *ctx_ptr, void *ptr) {
  860. pool_ctx *ctx = (pool_ctx *)ctx_ptr;
  861. pool_chunk *cur = ((pool_chunk *)ptr) - 1;
  862. pool_chunk *prev = NULL, *next = ctx->free_list;
  863. while (next && next < cur) {
  864. prev = next;
  865. next = next->next;
  866. }
  867. if (prev) prev->next = cur;
  868. else ctx->free_list = cur;
  869. cur->next = next;
  870. if (next && ((u8 *)cur + cur->size) == (u8 *)next) {
  871. /* merge cur to higher chunk */
  872. cur->size += next->size;
  873. cur->next = next->next;
  874. }
  875. if (prev && ((u8 *)prev + prev->size) == (u8 *)cur) {
  876. /* merge cur to lower chunk */
  877. prev->size += cur->size;
  878. prev->next = cur->next;
  879. }
  880. }
  881. static void *pool_realloc(void *ctx_ptr, void *ptr,
  882. usize old_size, usize size) {
  883. pool_ctx *ctx = (pool_ctx *)ctx_ptr;
  884. pool_chunk *cur = ((pool_chunk *)ptr) - 1, *prev, *next, *tmp;
  885. usize free_size;
  886. void *new_ptr;
  887. if (unlikely(size == 0 || size >= ctx->size)) return NULL;
  888. size = size_align_up(size, sizeof(pool_chunk)) + sizeof(pool_chunk);
  889. /* reduce size */
  890. if (unlikely(size <= cur->size)) {
  891. free_size = cur->size - size;
  892. if (free_size >= sizeof(pool_chunk) * 2) {
  893. tmp = (pool_chunk *)(void *)((u8 *)cur + cur->size - free_size);
  894. tmp->size = free_size;
  895. pool_free(ctx_ptr, (void *)(tmp + 1));
  896. cur->size -= free_size;
  897. }
  898. return ptr;
  899. }
  900. /* find next and prev chunk */
  901. prev = NULL;
  902. next = ctx->free_list;
  903. while (next && next < cur) {
  904. prev = next;
  905. next = next->next;
  906. }
  907. /* merge to higher chunk if they are contiguous */
  908. if ((u8 *)cur + cur->size == (u8 *)next &&
  909. cur->size + next->size >= size) {
  910. free_size = cur->size + next->size - size;
  911. if (free_size > sizeof(pool_chunk) * 2) {
  912. tmp = (pool_chunk *)(void *)((u8 *)cur + size);
  913. if (prev) prev->next = tmp;
  914. else ctx->free_list = tmp;
  915. tmp->next = next->next;
  916. tmp->size = free_size;
  917. cur->size = size;
  918. } else {
  919. if (prev) prev->next = next->next;
  920. else ctx->free_list = next->next;
  921. cur->size += next->size;
  922. }
  923. return ptr;
  924. }
  925. /* fallback to malloc and memcpy */
  926. new_ptr = pool_malloc(ctx_ptr, size - sizeof(pool_chunk));
  927. if (new_ptr) {
  928. memcpy(new_ptr, ptr, cur->size - sizeof(pool_chunk));
  929. pool_free(ctx_ptr, ptr);
  930. }
  931. return new_ptr;
  932. }
  933. bool yyjson_alc_pool_init(yyjson_alc *alc, void *buf, usize size) {
  934. pool_chunk *chunk;
  935. pool_ctx *ctx;
  936. if (unlikely(!alc)) return false;
  937. *alc = YYJSON_NULL_ALC;
  938. if (size < sizeof(pool_ctx) * 4) return false;
  939. ctx = (pool_ctx *)mem_align_up(buf, sizeof(pool_ctx));
  940. if (unlikely(!ctx)) return false;
  941. size -= (usize)((u8 *)ctx - (u8 *)buf);
  942. size = size_align_down(size, sizeof(pool_ctx));
  943. chunk = (pool_chunk *)(ctx + 1);
  944. chunk->size = size - sizeof(pool_ctx);
  945. chunk->next = NULL;
  946. ctx->size = size;
  947. ctx->free_list = chunk;
  948. alc->malloc = pool_malloc;
  949. alc->realloc = pool_realloc;
  950. alc->free = pool_free;
  951. alc->ctx = (void *)ctx;
  952. return true;
  953. }
  954. /*==============================================================================
  955. * JSON document and value
  956. *============================================================================*/
  957. static_inline void unsafe_yyjson_str_pool_release(yyjson_str_pool *pool,
  958. yyjson_alc *alc) {
  959. yyjson_str_chunk *chunk = pool->chunks, *next;
  960. while (chunk) {
  961. next = chunk->next;
  962. alc->free(alc->ctx, chunk);
  963. chunk = next;
  964. }
  965. }
  966. static_inline void unsafe_yyjson_val_pool_release(yyjson_val_pool *pool,
  967. yyjson_alc *alc) {
  968. yyjson_val_chunk *chunk = pool->chunks, *next;
  969. while (chunk) {
  970. next = chunk->next;
  971. alc->free(alc->ctx, chunk);
  972. chunk = next;
  973. }
  974. }
  975. bool unsafe_yyjson_str_pool_grow(yyjson_str_pool *pool,
  976. const yyjson_alc *alc, usize len) {
  977. yyjson_str_chunk *chunk;
  978. usize size, max_len;
  979. /* create a new chunk */
  980. max_len = USIZE_MAX - sizeof(yyjson_str_chunk);
  981. if (unlikely(len > max_len)) return false;
  982. size = len + sizeof(yyjson_str_chunk);
  983. size = yyjson_max(pool->chunk_size, size);
  984. chunk = (yyjson_str_chunk *)alc->malloc(alc->ctx, size);
  985. if (unlikely(!chunk)) return false;
  986. /* insert the new chunk as the head of the linked list */
  987. chunk->next = pool->chunks;
  988. chunk->chunk_size = size;
  989. pool->chunks = chunk;
  990. pool->cur = (char *)chunk + sizeof(yyjson_str_chunk);
  991. pool->end = (char *)chunk + size;
  992. /* the next chunk is twice the size of the current one */
  993. size = yyjson_min(pool->chunk_size * 2, pool->chunk_size_max);
  994. if (size < pool->chunk_size) size = pool->chunk_size_max; /* overflow */
  995. pool->chunk_size = size;
  996. return true;
  997. }
  998. bool unsafe_yyjson_val_pool_grow(yyjson_val_pool *pool,
  999. const yyjson_alc *alc, usize count) {
  1000. yyjson_val_chunk *chunk;
  1001. usize size, max_count;
  1002. /* create a new chunk */
  1003. max_count = USIZE_MAX / sizeof(yyjson_mut_val) - 1;
  1004. if (unlikely(count > max_count)) return false;
  1005. size = (count + 1) * sizeof(yyjson_mut_val);
  1006. size = yyjson_max(pool->chunk_size, size);
  1007. chunk = (yyjson_val_chunk *)alc->malloc(alc->ctx, size);
  1008. if (unlikely(!chunk)) return false;
  1009. /* insert the new chunk as the head of the linked list */
  1010. chunk->next = pool->chunks;
  1011. chunk->chunk_size = size;
  1012. pool->chunks = chunk;
  1013. pool->cur = (yyjson_mut_val *)(void *)((u8 *)chunk) + 1;
  1014. pool->end = (yyjson_mut_val *)(void *)((u8 *)chunk + size);
  1015. /* the next chunk is twice the size of the current one */
  1016. size = yyjson_min(pool->chunk_size * 2, pool->chunk_size_max);
  1017. if (size < pool->chunk_size) size = pool->chunk_size_max; /* overflow */
  1018. pool->chunk_size = size;
  1019. return true;
  1020. }
  1021. bool yyjson_mut_doc_set_str_pool_size(yyjson_mut_doc *doc, size_t len) {
  1022. usize max_size = USIZE_MAX - sizeof(yyjson_str_chunk);
  1023. if (!doc || !len || len > max_size) return false;
  1024. doc->str_pool.chunk_size = len + sizeof(yyjson_str_chunk);
  1025. return true;
  1026. }
  1027. bool yyjson_mut_doc_set_val_pool_size(yyjson_mut_doc *doc, size_t count) {
  1028. usize max_count = USIZE_MAX / sizeof(yyjson_mut_val) - 1;
  1029. if (!doc || !count || count > max_count) return false;
  1030. doc->val_pool.chunk_size = (count + 1) * sizeof(yyjson_mut_val);
  1031. return true;
  1032. }
  1033. void yyjson_mut_doc_free(yyjson_mut_doc *doc) {
  1034. if (doc) {
  1035. yyjson_alc alc = doc->alc;
  1036. unsafe_yyjson_str_pool_release(&doc->str_pool, &alc);
  1037. unsafe_yyjson_val_pool_release(&doc->val_pool, &alc);
  1038. alc.free(alc.ctx, doc);
  1039. }
  1040. }
  1041. yyjson_mut_doc *yyjson_mut_doc_new(const yyjson_alc *alc) {
  1042. yyjson_mut_doc *doc;
  1043. if (!alc) alc = &YYJSON_DEFAULT_ALC;
  1044. doc = (yyjson_mut_doc *)alc->malloc(alc->ctx, sizeof(yyjson_mut_doc));
  1045. if (!doc) return NULL;
  1046. memset(doc, 0, sizeof(yyjson_mut_doc));
  1047. doc->alc = *alc;
  1048. doc->str_pool.chunk_size = YYJSON_MUT_DOC_STR_POOL_INIT_SIZE;
  1049. doc->str_pool.chunk_size_max = YYJSON_MUT_DOC_STR_POOL_MAX_SIZE;
  1050. doc->val_pool.chunk_size = YYJSON_MUT_DOC_VAL_POOL_INIT_SIZE;
  1051. doc->val_pool.chunk_size_max = YYJSON_MUT_DOC_VAL_POOL_MAX_SIZE;
  1052. return doc;
  1053. }
  1054. yyjson_mut_doc *yyjson_doc_mut_copy(yyjson_doc *doc, const yyjson_alc *alc) {
  1055. yyjson_mut_doc *m_doc;
  1056. yyjson_mut_val *m_val;
  1057. if (!doc || !doc->root) return NULL;
  1058. m_doc = yyjson_mut_doc_new(alc);
  1059. if (!m_doc) return NULL;
  1060. m_val = yyjson_val_mut_copy(m_doc, doc->root);
  1061. if (!m_val) {
  1062. yyjson_mut_doc_free(m_doc);
  1063. return NULL;
  1064. }
  1065. yyjson_mut_doc_set_root(m_doc, m_val);
  1066. return m_doc;
  1067. }
  1068. yyjson_mut_doc *yyjson_mut_doc_mut_copy(yyjson_mut_doc *doc,
  1069. const yyjson_alc *alc) {
  1070. yyjson_mut_doc *m_doc;
  1071. yyjson_mut_val *m_val;
  1072. if (!doc) return NULL;
  1073. if (!doc->root) return yyjson_mut_doc_new(alc);
  1074. m_doc = yyjson_mut_doc_new(alc);
  1075. if (!m_doc) return NULL;
  1076. m_val = yyjson_mut_val_mut_copy(m_doc, doc->root);
  1077. if (!m_val) {
  1078. yyjson_mut_doc_free(m_doc);
  1079. return NULL;
  1080. }
  1081. yyjson_mut_doc_set_root(m_doc, m_val);
  1082. return m_doc;
  1083. }
  1084. yyjson_mut_val *yyjson_val_mut_copy(yyjson_mut_doc *m_doc,
  1085. yyjson_val *i_vals) {
  1086. /*
  1087. The immutable object or array stores all sub-values in a contiguous memory,
  1088. We copy them to another contiguous memory as mutable values,
  1089. then reconnect the mutable values with the original relationship.
  1090. */
  1091. usize i_vals_len;
  1092. yyjson_mut_val *m_vals, *m_val;
  1093. yyjson_val *i_val, *i_end;
  1094. if (!m_doc || !i_vals) return NULL;
  1095. i_end = unsafe_yyjson_get_next(i_vals);
  1096. i_vals_len = (usize)(unsafe_yyjson_get_next(i_vals) - i_vals);
  1097. m_vals = unsafe_yyjson_mut_val(m_doc, i_vals_len);
  1098. if (!m_vals) return NULL;
  1099. i_val = i_vals;
  1100. m_val = m_vals;
  1101. for (; i_val < i_end; i_val++, m_val++) {
  1102. yyjson_type type = unsafe_yyjson_get_type(i_val);
  1103. m_val->tag = i_val->tag;
  1104. m_val->uni.u64 = i_val->uni.u64;
  1105. if (type == YYJSON_TYPE_STR || type == YYJSON_TYPE_RAW) {
  1106. const char *str = i_val->uni.str;
  1107. usize str_len = unsafe_yyjson_get_len(i_val);
  1108. m_val->uni.str = unsafe_yyjson_mut_strncpy(m_doc, str, str_len);
  1109. if (!m_val->uni.str) return NULL;
  1110. } else if (type == YYJSON_TYPE_ARR) {
  1111. usize len = unsafe_yyjson_get_len(i_val);
  1112. if (len > 0) {
  1113. yyjson_val *ii_val = i_val + 1, *ii_next;
  1114. yyjson_mut_val *mm_val = m_val + 1, *mm_ctn = m_val, *mm_next;
  1115. while (len-- > 1) {
  1116. ii_next = unsafe_yyjson_get_next(ii_val);
  1117. mm_next = mm_val + (ii_next - ii_val);
  1118. mm_val->next = mm_next;
  1119. ii_val = ii_next;
  1120. mm_val = mm_next;
  1121. }
  1122. mm_val->next = mm_ctn + 1;
  1123. mm_ctn->uni.ptr = mm_val;
  1124. }
  1125. } else if (type == YYJSON_TYPE_OBJ) {
  1126. usize len = unsafe_yyjson_get_len(i_val);
  1127. if (len > 0) {
  1128. yyjson_val *ii_key = i_val + 1, *ii_nextkey;
  1129. yyjson_mut_val *mm_key = m_val + 1, *mm_ctn = m_val;
  1130. yyjson_mut_val *mm_nextkey;
  1131. while (len-- > 1) {
  1132. ii_nextkey = unsafe_yyjson_get_next(ii_key + 1);
  1133. mm_nextkey = mm_key + (ii_nextkey - ii_key);
  1134. mm_key->next = mm_key + 1;
  1135. mm_key->next->next = mm_nextkey;
  1136. ii_key = ii_nextkey;
  1137. mm_key = mm_nextkey;
  1138. }
  1139. mm_key->next = mm_key + 1;
  1140. mm_key->next->next = mm_ctn + 1;
  1141. mm_ctn->uni.ptr = mm_key;
  1142. }
  1143. }
  1144. }
  1145. return m_vals;
  1146. }
  1147. static yyjson_mut_val *unsafe_yyjson_mut_val_mut_copy(yyjson_mut_doc *m_doc,
  1148. yyjson_mut_val *m_vals) {
  1149. /*
  1150. The mutable object or array stores all sub-values in a circular linked
  1151. list, so we can traverse them in the same loop. The traversal starts from
  1152. the last item, continues with the first item in a list, and ends with the
  1153. second to last item, which needs to be linked to the last item to close the
  1154. circle.
  1155. */
  1156. yyjson_mut_val *m_val = unsafe_yyjson_mut_val(m_doc, 1);
  1157. if (unlikely(!m_val)) return NULL;
  1158. m_val->tag = m_vals->tag;
  1159. switch (unsafe_yyjson_get_type(m_vals)) {
  1160. case YYJSON_TYPE_OBJ:
  1161. case YYJSON_TYPE_ARR:
  1162. if (unsafe_yyjson_get_len(m_vals) > 0) {
  1163. yyjson_mut_val *last = (yyjson_mut_val *)m_vals->uni.ptr;
  1164. yyjson_mut_val *next = last->next, *prev;
  1165. prev = unsafe_yyjson_mut_val_mut_copy(m_doc, last);
  1166. if (!prev) return NULL;
  1167. m_val->uni.ptr = (void *)prev;
  1168. while (next != last) {
  1169. prev->next = unsafe_yyjson_mut_val_mut_copy(m_doc, next);
  1170. if (!prev->next) return NULL;
  1171. prev = prev->next;
  1172. next = next->next;
  1173. }
  1174. prev->next = (yyjson_mut_val *)m_val->uni.ptr;
  1175. }
  1176. break;
  1177. case YYJSON_TYPE_RAW:
  1178. case YYJSON_TYPE_STR: {
  1179. const char *str = m_vals->uni.str;
  1180. usize str_len = unsafe_yyjson_get_len(m_vals);
  1181. m_val->uni.str = unsafe_yyjson_mut_strncpy(m_doc, str, str_len);
  1182. if (!m_val->uni.str) return NULL;
  1183. break;
  1184. }
  1185. default:
  1186. m_val->uni = m_vals->uni;
  1187. break;
  1188. }
  1189. return m_val;
  1190. }
  1191. yyjson_mut_val *yyjson_mut_val_mut_copy(yyjson_mut_doc *doc,
  1192. yyjson_mut_val *val) {
  1193. if (doc && val) return unsafe_yyjson_mut_val_mut_copy(doc, val);
  1194. return NULL;
  1195. }
  1196. /* Count the number of values and the total length of the strings. */
  1197. static void yyjson_mut_stat(yyjson_mut_val *val,
  1198. usize *val_sum, usize *str_sum) {
  1199. yyjson_type type = unsafe_yyjson_get_type(val);
  1200. *val_sum += 1;
  1201. if (type == YYJSON_TYPE_ARR || type == YYJSON_TYPE_OBJ) {
  1202. yyjson_mut_val *child = (yyjson_mut_val *)val->uni.ptr;
  1203. usize len = unsafe_yyjson_get_len(val), i;
  1204. len <<= (u8)(type == YYJSON_TYPE_OBJ);
  1205. *val_sum += len;
  1206. for (i = 0; i < len; i++) {
  1207. yyjson_type stype = unsafe_yyjson_get_type(child);
  1208. if (stype == YYJSON_TYPE_STR || stype == YYJSON_TYPE_RAW) {
  1209. *str_sum += unsafe_yyjson_get_len(child) + 1;
  1210. } else if (stype == YYJSON_TYPE_ARR || stype == YYJSON_TYPE_OBJ) {
  1211. yyjson_mut_stat(child, val_sum, str_sum);
  1212. *val_sum -= 1;
  1213. }
  1214. child = child->next;
  1215. }
  1216. } else if (type == YYJSON_TYPE_STR || type == YYJSON_TYPE_RAW) {
  1217. *str_sum += unsafe_yyjson_get_len(val) + 1;
  1218. }
  1219. }
  1220. /* Copy mutable values to immutable value pool. */
  1221. static usize yyjson_imut_copy(yyjson_val **val_ptr, char **buf_ptr,
  1222. yyjson_mut_val *mval) {
  1223. yyjson_val *val = *val_ptr;
  1224. yyjson_type type = unsafe_yyjson_get_type(mval);
  1225. if (type == YYJSON_TYPE_ARR || type == YYJSON_TYPE_OBJ) {
  1226. yyjson_mut_val *child = (yyjson_mut_val *)mval->uni.ptr;
  1227. usize len = unsafe_yyjson_get_len(mval), i;
  1228. usize val_sum = 1;
  1229. if (type == YYJSON_TYPE_OBJ) {
  1230. if (len) child = child->next->next;
  1231. len <<= 1;
  1232. } else {
  1233. if (len) child = child->next;
  1234. }
  1235. *val_ptr = val + 1;
  1236. for (i = 0; i < len; i++) {
  1237. val_sum += yyjson_imut_copy(val_ptr, buf_ptr, child);
  1238. child = child->next;
  1239. }
  1240. val->tag = mval->tag;
  1241. val->uni.ofs = val_sum * sizeof(yyjson_val);
  1242. return val_sum;
  1243. } else if (type == YYJSON_TYPE_STR || type == YYJSON_TYPE_RAW) {
  1244. char *buf = *buf_ptr;
  1245. usize len = unsafe_yyjson_get_len(mval);
  1246. memcpy((void *)buf, (const void *)mval->uni.str, len);
  1247. buf[len] = '\0';
  1248. val->tag = mval->tag;
  1249. val->uni.str = buf;
  1250. *val_ptr = val + 1;
  1251. *buf_ptr = buf + len + 1;
  1252. return 1;
  1253. } else {
  1254. val->tag = mval->tag;
  1255. val->uni = mval->uni;
  1256. *val_ptr = val + 1;
  1257. return 1;
  1258. }
  1259. }
  1260. yyjson_doc *yyjson_mut_doc_imut_copy(yyjson_mut_doc *mdoc,
  1261. const yyjson_alc *alc) {
  1262. if (!mdoc) return NULL;
  1263. return yyjson_mut_val_imut_copy(mdoc->root, alc);
  1264. }
  1265. yyjson_doc *yyjson_mut_val_imut_copy(yyjson_mut_val *mval,
  1266. const yyjson_alc *alc) {
  1267. usize val_num = 0, str_sum = 0, hdr_size, buf_size;
  1268. yyjson_doc *doc = NULL;
  1269. yyjson_val *val_hdr = NULL;
  1270. /* This value should be NULL here. Setting a non-null value suppresses
  1271. warning from the clang analyzer. */
  1272. char *str_hdr = (char *)(void *)&str_sum;
  1273. if (!mval) return NULL;
  1274. if (!alc) alc = &YYJSON_DEFAULT_ALC;
  1275. /* traverse the input value to get pool size */
  1276. yyjson_mut_stat(mval, &val_num, &str_sum);
  1277. /* create doc and val pool */
  1278. hdr_size = size_align_up(sizeof(yyjson_doc), sizeof(yyjson_val));
  1279. buf_size = hdr_size + val_num * sizeof(yyjson_val);
  1280. doc = (yyjson_doc *)alc->malloc(alc->ctx, buf_size);
  1281. if (!doc) return NULL;
  1282. memset(doc, 0, sizeof(yyjson_doc));
  1283. val_hdr = (yyjson_val *)(void *)((char *)(void *)doc + hdr_size);
  1284. doc->root = val_hdr;
  1285. doc->alc = *alc;
  1286. /* create str pool */
  1287. if (str_sum > 0) {
  1288. str_hdr = (char *)alc->malloc(alc->ctx, str_sum);
  1289. doc->str_pool = str_hdr;
  1290. if (!str_hdr) {
  1291. alc->free(alc->ctx, (void *)doc);
  1292. return NULL;
  1293. }
  1294. }
  1295. /* copy vals and strs */
  1296. doc->val_read = yyjson_imut_copy(&val_hdr, &str_hdr, mval);
  1297. doc->dat_read = str_sum + 1;
  1298. return doc;
  1299. }
  1300. static_inline bool unsafe_yyjson_num_equals(void *lhs, void *rhs) {
  1301. yyjson_val_uni *luni = &((yyjson_val *)lhs)->uni;
  1302. yyjson_val_uni *runi = &((yyjson_val *)rhs)->uni;
  1303. yyjson_subtype lt = unsafe_yyjson_get_subtype(lhs);
  1304. yyjson_subtype rt = unsafe_yyjson_get_subtype(rhs);
  1305. if (lt == rt)
  1306. return luni->u64 == runi->u64;
  1307. if (lt == YYJSON_SUBTYPE_SINT && rt == YYJSON_SUBTYPE_UINT)
  1308. return luni->i64 >= 0 && luni->u64 == runi->u64;
  1309. if (lt == YYJSON_SUBTYPE_UINT && rt == YYJSON_SUBTYPE_SINT)
  1310. return runi->i64 >= 0 && luni->u64 == runi->u64;
  1311. return false;
  1312. }
  1313. static_inline bool unsafe_yyjson_str_equals(void *lhs, void *rhs) {
  1314. usize len = unsafe_yyjson_get_len(lhs);
  1315. if (len != unsafe_yyjson_get_len(rhs)) return false;
  1316. return !memcmp(unsafe_yyjson_get_str(lhs),
  1317. unsafe_yyjson_get_str(rhs), len);
  1318. }
  1319. bool unsafe_yyjson_equals(yyjson_val *lhs, yyjson_val *rhs) {
  1320. yyjson_type type = unsafe_yyjson_get_type(lhs);
  1321. if (type != unsafe_yyjson_get_type(rhs)) return false;
  1322. switch (type) {
  1323. case YYJSON_TYPE_OBJ: {
  1324. usize len = unsafe_yyjson_get_len(lhs);
  1325. if (len != unsafe_yyjson_get_len(rhs)) return false;
  1326. if (len > 0) {
  1327. yyjson_obj_iter iter;
  1328. yyjson_obj_iter_init(rhs, &iter);
  1329. lhs = unsafe_yyjson_get_first(lhs);
  1330. while (len-- > 0) {
  1331. rhs = yyjson_obj_iter_getn(&iter, lhs->uni.str,
  1332. unsafe_yyjson_get_len(lhs));
  1333. if (!rhs || !unsafe_yyjson_equals(lhs + 1, rhs))
  1334. return false;
  1335. lhs = unsafe_yyjson_get_next(lhs + 1);
  1336. }
  1337. }
  1338. /* yyjson allows duplicate keys, so the check may be inaccurate */
  1339. return true;
  1340. }
  1341. case YYJSON_TYPE_ARR: {
  1342. usize len = unsafe_yyjson_get_len(lhs);
  1343. if (len != unsafe_yyjson_get_len(rhs)) return false;
  1344. if (len > 0) {
  1345. lhs = unsafe_yyjson_get_first(lhs);
  1346. rhs = unsafe_yyjson_get_first(rhs);
  1347. while (len-- > 0) {
  1348. if (!unsafe_yyjson_equals(lhs, rhs)) return false;
  1349. lhs = unsafe_yyjson_get_next(lhs);
  1350. rhs = unsafe_yyjson_get_next(rhs);
  1351. }
  1352. }
  1353. return true;
  1354. }
  1355. case YYJSON_TYPE_NUM:
  1356. return unsafe_yyjson_num_equals(lhs, rhs);
  1357. case YYJSON_TYPE_RAW:
  1358. case YYJSON_TYPE_STR:
  1359. return unsafe_yyjson_str_equals(lhs, rhs);
  1360. case YYJSON_TYPE_NULL:
  1361. case YYJSON_TYPE_BOOL:
  1362. return lhs->tag == rhs->tag;
  1363. default:
  1364. return false;
  1365. }
  1366. }
  1367. bool unsafe_yyjson_mut_equals(yyjson_mut_val *lhs, yyjson_mut_val *rhs) {
  1368. yyjson_type type = unsafe_yyjson_get_type(lhs);
  1369. if (type != unsafe_yyjson_get_type(rhs)) return false;
  1370. switch (type) {
  1371. case YYJSON_TYPE_OBJ: {
  1372. usize len = unsafe_yyjson_get_len(lhs);
  1373. if (len != unsafe_yyjson_get_len(rhs)) return false;
  1374. if (len > 0) {
  1375. yyjson_mut_obj_iter iter;
  1376. yyjson_mut_obj_iter_init(rhs, &iter);
  1377. lhs = (yyjson_mut_val *)lhs->uni.ptr;
  1378. while (len-- > 0) {
  1379. rhs = yyjson_mut_obj_iter_getn(&iter, lhs->uni.str,
  1380. unsafe_yyjson_get_len(lhs));
  1381. if (!rhs || !unsafe_yyjson_mut_equals(lhs->next, rhs))
  1382. return false;
  1383. lhs = lhs->next->next;
  1384. }
  1385. }
  1386. /* yyjson allows duplicate keys, so the check may be inaccurate */
  1387. return true;
  1388. }
  1389. case YYJSON_TYPE_ARR: {
  1390. usize len = unsafe_yyjson_get_len(lhs);
  1391. if (len != unsafe_yyjson_get_len(rhs)) return false;
  1392. if (len > 0) {
  1393. lhs = (yyjson_mut_val *)lhs->uni.ptr;
  1394. rhs = (yyjson_mut_val *)rhs->uni.ptr;
  1395. while (len-- > 0) {
  1396. if (!unsafe_yyjson_mut_equals(lhs, rhs)) return false;
  1397. lhs = lhs->next;
  1398. rhs = rhs->next;
  1399. }
  1400. }
  1401. return true;
  1402. }
  1403. case YYJSON_TYPE_NUM:
  1404. return unsafe_yyjson_num_equals(lhs, rhs);
  1405. case YYJSON_TYPE_RAW:
  1406. case YYJSON_TYPE_STR:
  1407. return unsafe_yyjson_str_equals(lhs, rhs);
  1408. case YYJSON_TYPE_NULL:
  1409. case YYJSON_TYPE_BOOL:
  1410. return lhs->tag == rhs->tag;
  1411. default:
  1412. return false;
  1413. }
  1414. }
  1415. #if !YYJSON_DISABLE_UTILS
  1416. /*==============================================================================
  1417. * JSON Pointer API (RFC 6901)
  1418. *============================================================================*/
  1419. /**
  1420. Get a token from JSON pointer string.
  1421. @param ptr [in,out]
  1422. in: string that points to current token prefix `/`
  1423. out: string that points to next token prefix `/`, or string end
  1424. @param end [in] end of the entire JSON Pointer string
  1425. @param len [out] unescaped token length
  1426. @param esc [out] number of escaped characters in this token
  1427. @return head of the token, or NULL if syntax error
  1428. */
  1429. static_inline const char *ptr_next_token(const char **ptr, const char *end,
  1430. usize *len, usize *esc) {
  1431. const char *hdr = *ptr + 1;
  1432. const char *cur = hdr;
  1433. /* skip unescaped characters */
  1434. while (cur < end && *cur != '/' && *cur != '~') cur++;
  1435. if (likely(cur == end || *cur != '~')) {
  1436. /* no escaped characters, return */
  1437. *ptr = cur;
  1438. *len = (usize)(cur - hdr);
  1439. *esc = 0;
  1440. return hdr;
  1441. } else {
  1442. /* handle escaped characters */
  1443. usize esc_num = 0;
  1444. while (cur < end && *cur != '/') {
  1445. if (*cur++ == '~') {
  1446. if (cur == end || (*cur != '0' && *cur != '1')) {
  1447. *ptr = cur - 1;
  1448. return NULL;
  1449. }
  1450. esc_num++;
  1451. }
  1452. }
  1453. *ptr = cur;
  1454. *len = (usize)(cur - hdr) - esc_num;
  1455. *esc = esc_num;
  1456. return hdr;
  1457. }
  1458. }
  1459. /**
  1460. Convert token string to index.
  1461. @param cur [in] token head
  1462. @param len [in] token length
  1463. @param idx [out] the index number, or USIZE_MAX if token is '-'
  1464. @return true if token is a valid array index
  1465. */
  1466. static_inline bool ptr_token_to_idx(const char *cur, usize len, usize *idx) {
  1467. const char *end = cur + len;
  1468. usize num = 0, add;
  1469. if (unlikely(len == 0 || len > USIZE_SAFE_DIG)) return false;
  1470. if (*cur == '0') {
  1471. if (unlikely(len > 1)) return false;
  1472. *idx = 0;
  1473. return true;
  1474. }
  1475. if (*cur == '-') {
  1476. if (unlikely(len > 1)) return false;
  1477. *idx = USIZE_MAX;
  1478. return true;
  1479. }
  1480. for (; cur < end && (add = (usize)((u8)*cur - (u8)'0')) <= 9; cur++) {
  1481. num = num * 10 + add;
  1482. }
  1483. if (unlikely(num == 0 || cur < end)) return false;
  1484. *idx = num;
  1485. return true;
  1486. }
  1487. /**
  1488. Compare JSON key with token.
  1489. @param key a string key (yyjson_val or yyjson_mut_val)
  1490. @param token a JSON pointer token
  1491. @param len unescaped token length
  1492. @param esc number of escaped characters in this token
  1493. @return true if `str` is equals to `token`
  1494. */
  1495. static_inline bool ptr_token_eq(void *key,
  1496. const char *token, usize len, usize esc) {
  1497. yyjson_val *val = (yyjson_val *)key;
  1498. if (unsafe_yyjson_get_len(val) != len) return false;
  1499. if (likely(!esc)) {
  1500. return memcmp(val->uni.str, token, len) == 0;
  1501. } else {
  1502. const char *str = val->uni.str;
  1503. for (; len-- > 0; token++, str++) {
  1504. if (*token == '~') {
  1505. if (*str != (*++token == '0' ? '~' : '/')) return false;
  1506. } else {
  1507. if (*str != *token) return false;
  1508. }
  1509. }
  1510. return true;
  1511. }
  1512. }
  1513. /**
  1514. Get a value from array by token.
  1515. @param arr an array, should not be NULL or non-array type
  1516. @param token a JSON pointer token
  1517. @param len unescaped token length
  1518. @param esc number of escaped characters in this token
  1519. @return value at index, or NULL if token is not index or index is out of range
  1520. */
  1521. static_inline yyjson_val *ptr_arr_get(yyjson_val *arr, const char *token,
  1522. usize len, usize esc) {
  1523. yyjson_val *val = unsafe_yyjson_get_first(arr);
  1524. usize num = unsafe_yyjson_get_len(arr), idx = 0;
  1525. if (unlikely(num == 0)) return NULL;
  1526. if (unlikely(!ptr_token_to_idx(token, len, &idx))) return NULL;
  1527. if (unlikely(idx >= num)) return NULL;
  1528. if (unsafe_yyjson_arr_is_flat(arr)) {
  1529. return val + idx;
  1530. } else {
  1531. while (idx-- > 0) val = unsafe_yyjson_get_next(val);
  1532. return val;
  1533. }
  1534. }
  1535. /**
  1536. Get a value from object by token.
  1537. @param obj [in] an object, should not be NULL or non-object type
  1538. @param token [in] a JSON pointer token
  1539. @param len [in] unescaped token length
  1540. @param esc [in] number of escaped characters in this token
  1541. @return value associated with the token, or NULL if no value
  1542. */
  1543. static_inline yyjson_val *ptr_obj_get(yyjson_val *obj, const char *token,
  1544. usize len, usize esc) {
  1545. yyjson_val *key = unsafe_yyjson_get_first(obj);
  1546. usize num = unsafe_yyjson_get_len(obj);
  1547. if (unlikely(num == 0)) return NULL;
  1548. for (; num > 0; num--, key = unsafe_yyjson_get_next(key + 1)) {
  1549. if (ptr_token_eq(key, token, len, esc)) return key + 1;
  1550. }
  1551. return NULL;
  1552. }
  1553. /**
  1554. Get a value from array by token.
  1555. @param arr [in] an array, should not be NULL or non-array type
  1556. @param token [in] a JSON pointer token
  1557. @param len [in] unescaped token length
  1558. @param esc [in] number of escaped characters in this token
  1559. @param pre [out] previous (sibling) value of the returned value
  1560. @param last [out] whether index is last
  1561. @return value at index, or NULL if token is not index or index is out of range
  1562. */
  1563. static_inline yyjson_mut_val *ptr_mut_arr_get(yyjson_mut_val *arr,
  1564. const char *token,
  1565. usize len, usize esc,
  1566. yyjson_mut_val **pre,
  1567. bool *last) {
  1568. yyjson_mut_val *val = (yyjson_mut_val *)arr->uni.ptr; /* last (tail) */
  1569. usize num = unsafe_yyjson_get_len(arr), idx;
  1570. if (last) *last = false;
  1571. if (pre) *pre = NULL;
  1572. if (unlikely(num == 0)) {
  1573. if (last && len == 1 && (*token == '0' || *token == '-')) *last = true;
  1574. return NULL;
  1575. }
  1576. if (unlikely(!ptr_token_to_idx(token, len, &idx))) return NULL;
  1577. if (last) *last = (idx == num || idx == USIZE_MAX);
  1578. if (unlikely(idx >= num)) return NULL;
  1579. while (idx-- > 0) val = val->next;
  1580. *pre = val;
  1581. return val->next;
  1582. }
  1583. /**
  1584. Get a value from object by token.
  1585. @param obj [in] an object, should not be NULL or non-object type
  1586. @param token [in] a JSON pointer token
  1587. @param len [in] unescaped token length
  1588. @param esc [in] number of escaped characters in this token
  1589. @param pre [out] previous (sibling) key of the returned value's key
  1590. @return value associated with the token, or NULL if no value
  1591. */
  1592. static_inline yyjson_mut_val *ptr_mut_obj_get(yyjson_mut_val *obj,
  1593. const char *token,
  1594. usize len, usize esc,
  1595. yyjson_mut_val **pre) {
  1596. yyjson_mut_val *pre_key = (yyjson_mut_val *)obj->uni.ptr, *key;
  1597. usize num = unsafe_yyjson_get_len(obj);
  1598. if (pre) *pre = NULL;
  1599. if (unlikely(num == 0)) return NULL;
  1600. for (; num > 0; num--, pre_key = key) {
  1601. key = pre_key->next->next;
  1602. if (ptr_token_eq(key, token, len, esc)) {
  1603. *pre = pre_key;
  1604. return key->next;
  1605. }
  1606. }
  1607. return NULL;
  1608. }
  1609. /**
  1610. Create a string value with JSON pointer token.
  1611. @param token [in] a JSON pointer token
  1612. @param len [in] unescaped token length
  1613. @param esc [in] number of escaped characters in this token
  1614. @param doc [in] used for memory allocation when creating value
  1615. @return new string value, or NULL if memory allocation failed
  1616. */
  1617. static_inline yyjson_mut_val *ptr_new_key(const char *token,
  1618. usize len, usize esc,
  1619. yyjson_mut_doc *doc) {
  1620. const char *src = token;
  1621. if (likely(!esc)) {
  1622. return yyjson_mut_strncpy(doc, src, len);
  1623. } else {
  1624. const char *end = src + len + esc;
  1625. char *dst = unsafe_yyjson_mut_str_alc(doc, len + esc);
  1626. char *str = dst;
  1627. if (unlikely(!dst)) return NULL;
  1628. for (; src < end; src++, dst++) {
  1629. if (*src != '~') *dst = *src;
  1630. else *dst = (*++src == '0' ? '~' : '/');
  1631. }
  1632. *dst = '\0';
  1633. return yyjson_mut_strn(doc, str, len);
  1634. }
  1635. }
  1636. /* macros for yyjson_ptr */
  1637. #define return_err(_ret, _code, _pos, _msg) do { \
  1638. if (err) { \
  1639. err->code = YYJSON_PTR_ERR_##_code; \
  1640. err->msg = _msg; \
  1641. err->pos = (usize)(_pos); \
  1642. } \
  1643. return _ret; \
  1644. } while (false)
  1645. #define return_err_resolve(_ret, _pos) \
  1646. return_err(_ret, RESOLVE, _pos, "JSON pointer cannot be resolved")
  1647. #define return_err_syntax(_ret, _pos) \
  1648. return_err(_ret, SYNTAX, _pos, "invalid escaped character")
  1649. #define return_err_alloc(_ret) \
  1650. return_err(_ret, MEMORY_ALLOCATION, 0, "failed to create value")
  1651. yyjson_val *unsafe_yyjson_ptr_getx(yyjson_val *val,
  1652. const char *ptr, size_t ptr_len,
  1653. yyjson_ptr_err *err) {
  1654. const char *hdr = ptr, *end = ptr + ptr_len, *token;
  1655. usize len, esc;
  1656. yyjson_type type;
  1657. while (true) {
  1658. token = ptr_next_token(&ptr, end, &len, &esc);
  1659. if (unlikely(!token)) return_err_syntax(NULL, ptr - hdr);
  1660. type = unsafe_yyjson_get_type(val);
  1661. if (type == YYJSON_TYPE_OBJ) {
  1662. val = ptr_obj_get(val, token, len, esc);
  1663. } else if (type == YYJSON_TYPE_ARR) {
  1664. val = ptr_arr_get(val, token, len, esc);
  1665. } else {
  1666. val = NULL;
  1667. }
  1668. if (!val) return_err_resolve(NULL, token - hdr);
  1669. if (ptr == end) return val;
  1670. }
  1671. }
  1672. yyjson_mut_val *unsafe_yyjson_mut_ptr_getx(yyjson_mut_val *val,
  1673. const char *ptr,
  1674. size_t ptr_len,
  1675. yyjson_ptr_ctx *ctx,
  1676. yyjson_ptr_err *err) {
  1677. const char *hdr = ptr, *end = ptr + ptr_len, *token;
  1678. usize len, esc;
  1679. yyjson_mut_val *ctn, *pre = NULL;
  1680. yyjson_type type;
  1681. bool idx_is_last = false;
  1682. while (true) {
  1683. token = ptr_next_token(&ptr, end, &len, &esc);
  1684. if (unlikely(!token)) return_err_syntax(NULL, ptr - hdr);
  1685. ctn = val;
  1686. type = unsafe_yyjson_get_type(val);
  1687. if (type == YYJSON_TYPE_OBJ) {
  1688. val = ptr_mut_obj_get(val, token, len, esc, &pre);
  1689. } else if (type == YYJSON_TYPE_ARR) {
  1690. val = ptr_mut_arr_get(val, token, len, esc, &pre, &idx_is_last);
  1691. } else {
  1692. val = NULL;
  1693. }
  1694. if (ctx && (ptr == end)) {
  1695. if (type == YYJSON_TYPE_OBJ ||
  1696. (type == YYJSON_TYPE_ARR && (val || idx_is_last))) {
  1697. ctx->ctn = ctn;
  1698. ctx->pre = pre;
  1699. }
  1700. }
  1701. if (!val) return_err_resolve(NULL, token - hdr);
  1702. if (ptr == end) return val;
  1703. }
  1704. }
  1705. bool unsafe_yyjson_mut_ptr_putx(yyjson_mut_val *val,
  1706. const char *ptr, size_t ptr_len,
  1707. yyjson_mut_val *new_val,
  1708. yyjson_mut_doc *doc,
  1709. bool create_parent, bool insert_new,
  1710. yyjson_ptr_ctx *ctx,
  1711. yyjson_ptr_err *err) {
  1712. const char *hdr = ptr, *end = ptr + ptr_len, *token;
  1713. usize token_len, esc, ctn_len;
  1714. yyjson_mut_val *ctn, *key, *pre = NULL;
  1715. yyjson_mut_val *sep_ctn = NULL, *sep_key = NULL, *sep_val = NULL;
  1716. yyjson_type ctn_type;
  1717. bool idx_is_last = false;
  1718. /* skip exist parent nodes */
  1719. while (true) {
  1720. token = ptr_next_token(&ptr, end, &token_len, &esc);
  1721. if (unlikely(!token)) return_err_syntax(false, ptr - hdr);
  1722. ctn = val;
  1723. ctn_type = unsafe_yyjson_get_type(ctn);
  1724. if (ctn_type == YYJSON_TYPE_OBJ) {
  1725. val = ptr_mut_obj_get(ctn, token, token_len, esc, &pre);
  1726. } else if (ctn_type == YYJSON_TYPE_ARR) {
  1727. val = ptr_mut_arr_get(ctn, token, token_len, esc, &pre,
  1728. &idx_is_last);
  1729. } else return_err_resolve(false, token - hdr);
  1730. if (!val) break;
  1731. if (ptr == end) break; /* is last token */
  1732. }
  1733. /* create parent nodes if not exist */
  1734. if (unlikely(ptr != end)) { /* not last token */
  1735. if (!create_parent) return_err_resolve(false, token - hdr);
  1736. /* add value at last index if container is array */
  1737. if (ctn_type == YYJSON_TYPE_ARR) {
  1738. if (!idx_is_last || !insert_new) {
  1739. return_err_resolve(false, token - hdr);
  1740. }
  1741. val = yyjson_mut_obj(doc);
  1742. if (!val) return_err_alloc(false);
  1743. /* delay attaching until all operations are completed */
  1744. sep_ctn = ctn;
  1745. sep_key = NULL;
  1746. sep_val = val;
  1747. /* move to next token */
  1748. ctn = val;
  1749. val = NULL;
  1750. ctn_type = YYJSON_TYPE_OBJ;
  1751. token = ptr_next_token(&ptr, end, &token_len, &esc);
  1752. if (unlikely(!token)) return_err_resolve(false, token - hdr);
  1753. }
  1754. /* container is object, create parent nodes */
  1755. while (ptr != end) { /* not last token */
  1756. key = ptr_new_key(token, token_len, esc, doc);
  1757. if (!key) return_err_alloc(false);
  1758. val = yyjson_mut_obj(doc);
  1759. if (!val) return_err_alloc(false);
  1760. /* delay attaching until all operations are completed */
  1761. if (!sep_ctn) {
  1762. sep_ctn = ctn;
  1763. sep_key = key;
  1764. sep_val = val;
  1765. } else {
  1766. yyjson_mut_obj_add(ctn, key, val);
  1767. }
  1768. /* move to next token */
  1769. ctn = val;
  1770. val = NULL;
  1771. token = ptr_next_token(&ptr, end, &token_len, &esc);
  1772. if (unlikely(!token)) return_err_syntax(false, ptr - hdr);
  1773. }
  1774. }
  1775. /* JSON pointer is resolved, insert or replace target value */
  1776. ctn_len = unsafe_yyjson_get_len(ctn);
  1777. if (ctn_type == YYJSON_TYPE_OBJ) {
  1778. if (ctx) ctx->ctn = ctn;
  1779. if (!val || insert_new) {
  1780. /* insert new key-value pair */
  1781. key = ptr_new_key(token, token_len, esc, doc);
  1782. if (unlikely(!key)) return_err_alloc(false);
  1783. if (ctx) ctx->pre = ctn_len ? (yyjson_mut_val *)ctn->uni.ptr : key;
  1784. unsafe_yyjson_mut_obj_add(ctn, key, new_val, ctn_len);
  1785. } else {
  1786. /* replace exist value */
  1787. key = pre->next->next;
  1788. if (ctx) ctx->pre = pre;
  1789. if (ctx) ctx->old = val;
  1790. yyjson_mut_obj_put(ctn, key, new_val);
  1791. }
  1792. } else {
  1793. /* array */
  1794. if (ctx && (val || idx_is_last)) ctx->ctn = ctn;
  1795. if (insert_new) {
  1796. /* append new value */
  1797. if (val) {
  1798. pre->next = new_val;
  1799. new_val->next = val;
  1800. if (ctx) ctx->pre = pre;
  1801. unsafe_yyjson_set_len(ctn, ctn_len + 1);
  1802. } else if (idx_is_last) {
  1803. if (ctx) ctx->pre = ctn_len ?
  1804. (yyjson_mut_val *)ctn->uni.ptr : new_val;
  1805. yyjson_mut_arr_append(ctn, new_val);
  1806. } else {
  1807. return_err_resolve(false, token - hdr);
  1808. }
  1809. } else {
  1810. /* replace exist value */
  1811. if (!val) return_err_resolve(false, token - hdr);
  1812. if (ctn_len > 1) {
  1813. new_val->next = val->next;
  1814. pre->next = new_val;
  1815. if (ctn->uni.ptr == val) ctn->uni.ptr = new_val;
  1816. } else {
  1817. new_val->next = new_val;
  1818. ctn->uni.ptr = new_val;
  1819. pre = new_val;
  1820. }
  1821. if (ctx) ctx->pre = pre;
  1822. if (ctx) ctx->old = val;
  1823. }
  1824. }
  1825. /* all operations are completed, attach the new components to the target */
  1826. if (unlikely(sep_ctn)) {
  1827. if (sep_key) yyjson_mut_obj_add(sep_ctn, sep_key, sep_val);
  1828. else yyjson_mut_arr_append(sep_ctn, sep_val);
  1829. }
  1830. return true;
  1831. }
  1832. yyjson_mut_val *unsafe_yyjson_mut_ptr_replacex(
  1833. yyjson_mut_val *val, const char *ptr, size_t len, yyjson_mut_val *new_val,
  1834. yyjson_ptr_ctx *ctx, yyjson_ptr_err *err) {
  1835. yyjson_mut_val *cur_val;
  1836. yyjson_ptr_ctx cur_ctx;
  1837. memset(&cur_ctx, 0, sizeof(cur_ctx));
  1838. if (!ctx) ctx = &cur_ctx;
  1839. cur_val = unsafe_yyjson_mut_ptr_getx(val, ptr, len, ctx, err);
  1840. if (!cur_val) return NULL;
  1841. if (yyjson_mut_is_obj(ctx->ctn)) {
  1842. yyjson_mut_val *key = ctx->pre->next->next;
  1843. yyjson_mut_obj_put(ctx->ctn, key, new_val);
  1844. } else {
  1845. yyjson_ptr_ctx_replace(ctx, new_val);
  1846. }
  1847. ctx->old = cur_val;
  1848. return cur_val;
  1849. }
  1850. yyjson_mut_val *unsafe_yyjson_mut_ptr_removex(yyjson_mut_val *val,
  1851. const char *ptr,
  1852. size_t len,
  1853. yyjson_ptr_ctx *ctx,
  1854. yyjson_ptr_err *err) {
  1855. yyjson_mut_val *cur_val;
  1856. yyjson_ptr_ctx cur_ctx;
  1857. memset(&cur_ctx, 0, sizeof(cur_ctx));
  1858. if (!ctx) ctx = &cur_ctx;
  1859. cur_val = unsafe_yyjson_mut_ptr_getx(val, ptr, len, ctx, err);
  1860. if (cur_val) {
  1861. if (yyjson_mut_is_obj(ctx->ctn)) {
  1862. yyjson_mut_val *key = ctx->pre->next->next;
  1863. yyjson_mut_obj_put(ctx->ctn, key, NULL);
  1864. } else {
  1865. yyjson_ptr_ctx_remove(ctx);
  1866. }
  1867. ctx->pre = NULL;
  1868. ctx->old = cur_val;
  1869. }
  1870. return cur_val;
  1871. }
  1872. /* macros for yyjson_ptr */
  1873. #undef return_err
  1874. #undef return_err_resolve
  1875. #undef return_err_syntax
  1876. #undef return_err_alloc
  1877. /*==============================================================================
  1878. * JSON Patch API (RFC 6902)
  1879. *============================================================================*/
  1880. /* JSON Patch operation */
  1881. typedef enum patch_op {
  1882. PATCH_OP_ADD, /* path, value */
  1883. PATCH_OP_REMOVE, /* path */
  1884. PATCH_OP_REPLACE, /* path, value */
  1885. PATCH_OP_MOVE, /* from, path */
  1886. PATCH_OP_COPY, /* from, path */
  1887. PATCH_OP_TEST, /* path, value */
  1888. PATCH_OP_NONE /* invalid */
  1889. } patch_op;
  1890. static patch_op patch_op_get(yyjson_val *op) {
  1891. const char *str = op->uni.str;
  1892. switch (unsafe_yyjson_get_len(op)) {
  1893. case 3:
  1894. if (!memcmp(str, "add", 3)) return PATCH_OP_ADD;
  1895. return PATCH_OP_NONE;
  1896. case 4:
  1897. if (!memcmp(str, "move", 4)) return PATCH_OP_MOVE;
  1898. if (!memcmp(str, "copy", 4)) return PATCH_OP_COPY;
  1899. if (!memcmp(str, "test", 4)) return PATCH_OP_TEST;
  1900. return PATCH_OP_NONE;
  1901. case 6:
  1902. if (!memcmp(str, "remove", 6)) return PATCH_OP_REMOVE;
  1903. return PATCH_OP_NONE;
  1904. case 7:
  1905. if (!memcmp(str, "replace", 7)) return PATCH_OP_REPLACE;
  1906. return PATCH_OP_NONE;
  1907. default:
  1908. return PATCH_OP_NONE;
  1909. }
  1910. }
  1911. /* macros for yyjson_patch */
  1912. #define return_err(_code, _msg) do { \
  1913. if (err->ptr.code == YYJSON_PTR_ERR_MEMORY_ALLOCATION) { \
  1914. err->code = YYJSON_PATCH_ERROR_MEMORY_ALLOCATION; \
  1915. err->msg = _msg; \
  1916. memset(&err->ptr, 0, sizeof(yyjson_ptr_err)); \
  1917. } else { \
  1918. err->code = YYJSON_PATCH_ERROR_##_code; \
  1919. err->msg = _msg; \
  1920. err->idx = iter.idx ? iter.idx - 1 : 0; \
  1921. } \
  1922. return NULL; \
  1923. } while (false)
  1924. #define return_err_copy() \
  1925. return_err(MEMORY_ALLOCATION, "failed to copy value")
  1926. #define return_err_key(_key) \
  1927. return_err(MISSING_KEY, "missing key " _key)
  1928. #define return_err_val(_key) \
  1929. return_err(INVALID_MEMBER, "invalid member " _key)
  1930. #define ptr_get(_ptr) yyjson_mut_ptr_getx( \
  1931. root, _ptr->uni.str, _ptr##_len, NULL, &err->ptr)
  1932. #define ptr_add(_ptr, _val) yyjson_mut_ptr_addx( \
  1933. root, _ptr->uni.str, _ptr##_len, _val, doc, false, NULL, &err->ptr)
  1934. #define ptr_remove(_ptr) yyjson_mut_ptr_removex( \
  1935. root, _ptr->uni.str, _ptr##_len, NULL, &err->ptr)
  1936. #define ptr_replace(_ptr, _val)yyjson_mut_ptr_replacex( \
  1937. root, _ptr->uni.str, _ptr##_len, _val, NULL, &err->ptr)
  1938. yyjson_mut_val *yyjson_patch(yyjson_mut_doc *doc,
  1939. yyjson_val *orig,
  1940. yyjson_val *patch,
  1941. yyjson_patch_err *err) {
  1942. yyjson_mut_val *root;
  1943. yyjson_val *obj;
  1944. yyjson_arr_iter iter;
  1945. yyjson_patch_err err_tmp;
  1946. if (!err) err = &err_tmp;
  1947. memset(err, 0, sizeof(*err));
  1948. memset(&iter, 0, sizeof(iter));
  1949. if (unlikely(!doc || !orig || !patch)) {
  1950. return_err(INVALID_PARAMETER, "input parameter is NULL");
  1951. }
  1952. if (unlikely(!yyjson_is_arr(patch))) {
  1953. return_err(INVALID_PARAMETER, "input patch is not array");
  1954. }
  1955. root = yyjson_val_mut_copy(doc, orig);
  1956. if (unlikely(!root)) return_err_copy();
  1957. /* iterate through the patch array */
  1958. yyjson_arr_iter_init(patch, &iter);
  1959. while ((obj = yyjson_arr_iter_next(&iter))) {
  1960. patch_op op_enum;
  1961. yyjson_val *op, *path, *from = NULL, *value;
  1962. yyjson_mut_val *val = NULL, *test;
  1963. usize path_len, from_len = 0;
  1964. if (unlikely(!unsafe_yyjson_is_obj(obj))) {
  1965. return_err(INVALID_OPERATION, "JSON patch operation is not object");
  1966. }
  1967. /* get required member: op */
  1968. op = yyjson_obj_get(obj, "op");
  1969. if (unlikely(!op)) return_err_key("`op`");
  1970. if (unlikely(!yyjson_is_str(op))) return_err_val("`op`");
  1971. op_enum = patch_op_get(op);
  1972. /* get required member: path */
  1973. path = yyjson_obj_get(obj, "path");
  1974. if (unlikely(!path)) return_err_key("`path`");
  1975. if (unlikely(!yyjson_is_str(path))) return_err_val("`path`");
  1976. path_len = unsafe_yyjson_get_len(path);
  1977. /* get required member: value, from */
  1978. switch ((int)op_enum) {
  1979. case PATCH_OP_ADD: case PATCH_OP_REPLACE: case PATCH_OP_TEST:
  1980. value = yyjson_obj_get(obj, "value");
  1981. if (unlikely(!value)) return_err_key("`value`");
  1982. val = yyjson_val_mut_copy(doc, value);
  1983. if (unlikely(!val)) return_err_copy();
  1984. break;
  1985. case PATCH_OP_MOVE: case PATCH_OP_COPY:
  1986. from = yyjson_obj_get(obj, "from");
  1987. if (unlikely(!from)) return_err_key("`from`");
  1988. if (unlikely(!yyjson_is_str(from))) return_err_val("`from`");
  1989. from_len = unsafe_yyjson_get_len(from);
  1990. break;
  1991. default:
  1992. break;
  1993. }
  1994. /* perform an operation */
  1995. switch ((int)op_enum) {
  1996. case PATCH_OP_ADD: /* add(path, val) */
  1997. if (unlikely(path_len == 0)) { root = val; break; }
  1998. if (unlikely(!ptr_add(path, val))) {
  1999. return_err(POINTER, "failed to add `path`");
  2000. }
  2001. break;
  2002. case PATCH_OP_REMOVE: /* remove(path) */
  2003. if (unlikely(!ptr_remove(path))) {
  2004. return_err(POINTER, "failed to remove `path`");
  2005. }
  2006. break;
  2007. case PATCH_OP_REPLACE: /* replace(path, val) */
  2008. if (unlikely(path_len == 0)) { root = val; break; }
  2009. if (unlikely(!ptr_replace(path, val))) {
  2010. return_err(POINTER, "failed to replace `path`");
  2011. }
  2012. break;
  2013. case PATCH_OP_MOVE: /* val = remove(from), add(path, val) */
  2014. if (unlikely(from_len == 0 && path_len == 0)) break;
  2015. val = ptr_remove(from);
  2016. if (unlikely(!val)) {
  2017. return_err(POINTER, "failed to remove `from`");
  2018. }
  2019. if (unlikely(path_len == 0)) { root = val; break; }
  2020. if (unlikely(!ptr_add(path, val))) {
  2021. return_err(POINTER, "failed to add `path`");
  2022. }
  2023. break;
  2024. case PATCH_OP_COPY: /* val = get(from).copy, add(path, val) */
  2025. val = ptr_get(from);
  2026. if (unlikely(!val)) {
  2027. return_err(POINTER, "failed to get `from`");
  2028. }
  2029. if (unlikely(path_len == 0)) { root = val; break; }
  2030. val = yyjson_mut_val_mut_copy(doc, val);
  2031. if (unlikely(!val)) return_err_copy();
  2032. if (unlikely(!ptr_add(path, val))) {
  2033. return_err(POINTER, "failed to add `path`");
  2034. }
  2035. break;
  2036. case PATCH_OP_TEST: /* test = get(path), test.eq(val) */
  2037. test = ptr_get(path);
  2038. if (unlikely(!test)) {
  2039. return_err(POINTER, "failed to get `path`");
  2040. }
  2041. if (unlikely(!yyjson_mut_equals(val, test))) {
  2042. return_err(EQUAL, "failed to test equal");
  2043. }
  2044. break;
  2045. default:
  2046. return_err(INVALID_MEMBER, "unsupported `op`");
  2047. }
  2048. }
  2049. return root;
  2050. }
  2051. yyjson_mut_val *yyjson_mut_patch(yyjson_mut_doc *doc,
  2052. yyjson_mut_val *orig,
  2053. yyjson_mut_val *patch,
  2054. yyjson_patch_err *err) {
  2055. yyjson_mut_val *root, *obj;
  2056. yyjson_mut_arr_iter iter;
  2057. yyjson_patch_err err_tmp;
  2058. if (!err) err = &err_tmp;
  2059. memset(err, 0, sizeof(*err));
  2060. memset(&iter, 0, sizeof(iter));
  2061. if (unlikely(!doc || !orig || !patch)) {
  2062. return_err(INVALID_PARAMETER, "input parameter is NULL");
  2063. }
  2064. if (unlikely(!yyjson_mut_is_arr(patch))) {
  2065. return_err(INVALID_PARAMETER, "input patch is not array");
  2066. }
  2067. root = yyjson_mut_val_mut_copy(doc, orig);
  2068. if (unlikely(!root)) return_err_copy();
  2069. /* iterate through the patch array */
  2070. yyjson_mut_arr_iter_init(patch, &iter);
  2071. while ((obj = yyjson_mut_arr_iter_next(&iter))) {
  2072. patch_op op_enum;
  2073. yyjson_mut_val *op, *path, *from = NULL, *value;
  2074. yyjson_mut_val *val = NULL, *test;
  2075. usize path_len, from_len = 0;
  2076. if (!unsafe_yyjson_is_obj(obj)) {
  2077. return_err(INVALID_OPERATION, "JSON patch operation is not object");
  2078. }
  2079. /* get required member: op */
  2080. op = yyjson_mut_obj_get(obj, "op");
  2081. if (unlikely(!op)) return_err_key("`op`");
  2082. if (unlikely(!yyjson_mut_is_str(op))) return_err_val("`op`");
  2083. op_enum = patch_op_get((yyjson_val *)(void *)op);
  2084. /* get required member: path */
  2085. path = yyjson_mut_obj_get(obj, "path");
  2086. if (unlikely(!path)) return_err_key("`path`");
  2087. if (unlikely(!yyjson_mut_is_str(path))) return_err_val("`path`");
  2088. path_len = unsafe_yyjson_get_len(path);
  2089. /* get required member: value, from */
  2090. switch ((int)op_enum) {
  2091. case PATCH_OP_ADD: case PATCH_OP_REPLACE: case PATCH_OP_TEST:
  2092. value = yyjson_mut_obj_get(obj, "value");
  2093. if (unlikely(!value)) return_err_key("`value`");
  2094. val = yyjson_mut_val_mut_copy(doc, value);
  2095. if (unlikely(!val)) return_err_copy();
  2096. break;
  2097. case PATCH_OP_MOVE: case PATCH_OP_COPY:
  2098. from = yyjson_mut_obj_get(obj, "from");
  2099. if (unlikely(!from)) return_err_key("`from`");
  2100. if (unlikely(!yyjson_mut_is_str(from))) {
  2101. return_err_val("`from`");
  2102. }
  2103. from_len = unsafe_yyjson_get_len(from);
  2104. break;
  2105. default:
  2106. break;
  2107. }
  2108. /* perform an operation */
  2109. switch ((int)op_enum) {
  2110. case PATCH_OP_ADD: /* add(path, val) */
  2111. if (unlikely(path_len == 0)) { root = val; break; }
  2112. if (unlikely(!ptr_add(path, val))) {
  2113. return_err(POINTER, "failed to add `path`");
  2114. }
  2115. break;
  2116. case PATCH_OP_REMOVE: /* remove(path) */
  2117. if (unlikely(!ptr_remove(path))) {
  2118. return_err(POINTER, "failed to remove `path`");
  2119. }
  2120. break;
  2121. case PATCH_OP_REPLACE: /* replace(path, val) */
  2122. if (unlikely(path_len == 0)) { root = val; break; }
  2123. if (unlikely(!ptr_replace(path, val))) {
  2124. return_err(POINTER, "failed to replace `path`");
  2125. }
  2126. break;
  2127. case PATCH_OP_MOVE: /* val = remove(from), add(path, val) */
  2128. if (unlikely(from_len == 0 && path_len == 0)) break;
  2129. val = ptr_remove(from);
  2130. if (unlikely(!val)) {
  2131. return_err(POINTER, "failed to remove `from`");
  2132. }
  2133. if (unlikely(path_len == 0)) { root = val; break; }
  2134. if (unlikely(!ptr_add(path, val))) {
  2135. return_err(POINTER, "failed to add `path`");
  2136. }
  2137. break;
  2138. case PATCH_OP_COPY: /* val = get(from).copy, add(path, val) */
  2139. val = ptr_get(from);
  2140. if (unlikely(!val)) {
  2141. return_err(POINTER, "failed to get `from`");
  2142. }
  2143. if (unlikely(path_len == 0)) { root = val; break; }
  2144. val = yyjson_mut_val_mut_copy(doc, val);
  2145. if (unlikely(!val)) return_err_copy();
  2146. if (unlikely(!ptr_add(path, val))) {
  2147. return_err(POINTER, "failed to add `path`");
  2148. }
  2149. break;
  2150. case PATCH_OP_TEST: /* test = get(path), test.eq(val) */
  2151. test = ptr_get(path);
  2152. if (unlikely(!test)) {
  2153. return_err(POINTER, "failed to get `path`");
  2154. }
  2155. if (unlikely(!yyjson_mut_equals(val, test))) {
  2156. return_err(EQUAL, "failed to test equal");
  2157. }
  2158. break;
  2159. default:
  2160. return_err(INVALID_MEMBER, "unsupported `op`");
  2161. }
  2162. }
  2163. return root;
  2164. }
  2165. /* macros for yyjson_patch */
  2166. #undef return_err
  2167. #undef return_err_copy
  2168. #undef return_err_key
  2169. #undef return_err_val
  2170. #undef ptr_get
  2171. #undef ptr_add
  2172. #undef ptr_remove
  2173. #undef ptr_replace
  2174. /*==============================================================================
  2175. * JSON Merge-Patch API (RFC 7386)
  2176. *============================================================================*/
  2177. yyjson_mut_val *yyjson_merge_patch(yyjson_mut_doc *doc,
  2178. yyjson_val *orig,
  2179. yyjson_val *patch) {
  2180. usize idx, max;
  2181. yyjson_val *key, *orig_val, *patch_val, local_orig;
  2182. yyjson_mut_val *builder, *mut_key, *mut_val, *merged_val;
  2183. if (unlikely(!yyjson_is_obj(patch))) {
  2184. return yyjson_val_mut_copy(doc, patch);
  2185. }
  2186. builder = yyjson_mut_obj(doc);
  2187. if (unlikely(!builder)) return NULL;
  2188. if (!yyjson_is_obj(orig)) {
  2189. orig = &local_orig;
  2190. orig->tag = builder->tag;
  2191. orig->uni = builder->uni;
  2192. }
  2193. /* If orig is contributing, copy any items not modified by the patch */
  2194. if (orig != &local_orig) {
  2195. yyjson_obj_foreach(orig, idx, max, key, orig_val) {
  2196. patch_val = yyjson_obj_getn(patch,
  2197. unsafe_yyjson_get_str(key),
  2198. unsafe_yyjson_get_len(key));
  2199. if (!patch_val) {
  2200. mut_key = yyjson_val_mut_copy(doc, key);
  2201. mut_val = yyjson_val_mut_copy(doc, orig_val);
  2202. if (!yyjson_mut_obj_add(builder, mut_key, mut_val)) return NULL;
  2203. }
  2204. }
  2205. }
  2206. /* Merge items modified by the patch. */
  2207. yyjson_obj_foreach(patch, idx, max, key, patch_val) {
  2208. /* null indicates the field is removed. */
  2209. if (unsafe_yyjson_is_null(patch_val)) {
  2210. continue;
  2211. }
  2212. mut_key = yyjson_val_mut_copy(doc, key);
  2213. orig_val = yyjson_obj_getn(orig,
  2214. unsafe_yyjson_get_str(key),
  2215. unsafe_yyjson_get_len(key));
  2216. merged_val = yyjson_merge_patch(doc, orig_val, patch_val);
  2217. if (!yyjson_mut_obj_add(builder, mut_key, merged_val)) return NULL;
  2218. }
  2219. return builder;
  2220. }
  2221. yyjson_mut_val *yyjson_mut_merge_patch(yyjson_mut_doc *doc,
  2222. yyjson_mut_val *orig,
  2223. yyjson_mut_val *patch) {
  2224. usize idx, max;
  2225. yyjson_mut_val *key, *orig_val, *patch_val, local_orig;
  2226. yyjson_mut_val *builder, *mut_key, *mut_val, *merged_val;
  2227. if (unlikely(!yyjson_mut_is_obj(patch))) {
  2228. return yyjson_mut_val_mut_copy(doc, patch);
  2229. }
  2230. builder = yyjson_mut_obj(doc);
  2231. if (unlikely(!builder)) return NULL;
  2232. if (!yyjson_mut_is_obj(orig)) {
  2233. orig = &local_orig;
  2234. orig->tag = builder->tag;
  2235. orig->uni = builder->uni;
  2236. }
  2237. /* If orig is contributing, copy any items not modified by the patch */
  2238. if (orig != &local_orig) {
  2239. yyjson_mut_obj_foreach(orig, idx, max, key, orig_val) {
  2240. patch_val = yyjson_mut_obj_getn(patch,
  2241. unsafe_yyjson_get_str(key),
  2242. unsafe_yyjson_get_len(key));
  2243. if (!patch_val) {
  2244. mut_key = yyjson_mut_val_mut_copy(doc, key);
  2245. mut_val = yyjson_mut_val_mut_copy(doc, orig_val);
  2246. if (!yyjson_mut_obj_add(builder, mut_key, mut_val)) return NULL;
  2247. }
  2248. }
  2249. }
  2250. /* Merge items modified by the patch. */
  2251. yyjson_mut_obj_foreach(patch, idx, max, key, patch_val) {
  2252. /* null indicates the field is removed. */
  2253. if (unsafe_yyjson_is_null(patch_val)) {
  2254. continue;
  2255. }
  2256. mut_key = yyjson_mut_val_mut_copy(doc, key);
  2257. orig_val = yyjson_mut_obj_getn(orig,
  2258. unsafe_yyjson_get_str(key),
  2259. unsafe_yyjson_get_len(key));
  2260. merged_val = yyjson_mut_merge_patch(doc, orig_val, patch_val);
  2261. if (!yyjson_mut_obj_add(builder, mut_key, merged_val)) return NULL;
  2262. }
  2263. return builder;
  2264. }
  2265. #endif /* YYJSON_DISABLE_UTILS */
  2266. /*==============================================================================
  2267. * Power10 Lookup Table
  2268. * These data are used by the floating-point number reader and writer.
  2269. *============================================================================*/
  2270. #if (!YYJSON_DISABLE_READER || !YYJSON_DISABLE_WRITER) && \
  2271. (!YYJSON_DISABLE_FAST_FP_CONV)
  2272. /** Minimum decimal exponent in pow10_sig_table. */
  2273. #define POW10_SIG_TABLE_MIN_EXP -343
  2274. /** Maximum decimal exponent in pow10_sig_table. */
  2275. #define POW10_SIG_TABLE_MAX_EXP 324
  2276. /** Minimum exact decimal exponent in pow10_sig_table */
  2277. #define POW10_SIG_TABLE_MIN_EXACT_EXP 0
  2278. /** Maximum exact decimal exponent in pow10_sig_table */
  2279. #define POW10_SIG_TABLE_MAX_EXACT_EXP 55
  2280. /** Normalized significant 128 bits of pow10, no rounded up (size: 10.4KB).
  2281. This lookup table is used by both the double number reader and writer.
  2282. (generate with misc/make_tables.c) */
  2283. static const u64 pow10_sig_table[] = {
  2284. U64(0xBF29DCAB, 0xA82FDEAE), U64(0x7432EE87, 0x3880FC33), /* ~= 10^-343 */
  2285. U64(0xEEF453D6, 0x923BD65A), U64(0x113FAA29, 0x06A13B3F), /* ~= 10^-342 */
  2286. U64(0x9558B466, 0x1B6565F8), U64(0x4AC7CA59, 0xA424C507), /* ~= 10^-341 */
  2287. U64(0xBAAEE17F, 0xA23EBF76), U64(0x5D79BCF0, 0x0D2DF649), /* ~= 10^-340 */
  2288. U64(0xE95A99DF, 0x8ACE6F53), U64(0xF4D82C2C, 0x107973DC), /* ~= 10^-339 */
  2289. U64(0x91D8A02B, 0xB6C10594), U64(0x79071B9B, 0x8A4BE869), /* ~= 10^-338 */
  2290. U64(0xB64EC836, 0xA47146F9), U64(0x9748E282, 0x6CDEE284), /* ~= 10^-337 */
  2291. U64(0xE3E27A44, 0x4D8D98B7), U64(0xFD1B1B23, 0x08169B25), /* ~= 10^-336 */
  2292. U64(0x8E6D8C6A, 0xB0787F72), U64(0xFE30F0F5, 0xE50E20F7), /* ~= 10^-335 */
  2293. U64(0xB208EF85, 0x5C969F4F), U64(0xBDBD2D33, 0x5E51A935), /* ~= 10^-334 */
  2294. U64(0xDE8B2B66, 0xB3BC4723), U64(0xAD2C7880, 0x35E61382), /* ~= 10^-333 */
  2295. U64(0x8B16FB20, 0x3055AC76), U64(0x4C3BCB50, 0x21AFCC31), /* ~= 10^-332 */
  2296. U64(0xADDCB9E8, 0x3C6B1793), U64(0xDF4ABE24, 0x2A1BBF3D), /* ~= 10^-331 */
  2297. U64(0xD953E862, 0x4B85DD78), U64(0xD71D6DAD, 0x34A2AF0D), /* ~= 10^-330 */
  2298. U64(0x87D4713D, 0x6F33AA6B), U64(0x8672648C, 0x40E5AD68), /* ~= 10^-329 */
  2299. U64(0xA9C98D8C, 0xCB009506), U64(0x680EFDAF, 0x511F18C2), /* ~= 10^-328 */
  2300. U64(0xD43BF0EF, 0xFDC0BA48), U64(0x0212BD1B, 0x2566DEF2), /* ~= 10^-327 */
  2301. U64(0x84A57695, 0xFE98746D), U64(0x014BB630, 0xF7604B57), /* ~= 10^-326 */
  2302. U64(0xA5CED43B, 0x7E3E9188), U64(0x419EA3BD, 0x35385E2D), /* ~= 10^-325 */
  2303. U64(0xCF42894A, 0x5DCE35EA), U64(0x52064CAC, 0x828675B9), /* ~= 10^-324 */
  2304. U64(0x818995CE, 0x7AA0E1B2), U64(0x7343EFEB, 0xD1940993), /* ~= 10^-323 */
  2305. U64(0xA1EBFB42, 0x19491A1F), U64(0x1014EBE6, 0xC5F90BF8), /* ~= 10^-322 */
  2306. U64(0xCA66FA12, 0x9F9B60A6), U64(0xD41A26E0, 0x77774EF6), /* ~= 10^-321 */
  2307. U64(0xFD00B897, 0x478238D0), U64(0x8920B098, 0x955522B4), /* ~= 10^-320 */
  2308. U64(0x9E20735E, 0x8CB16382), U64(0x55B46E5F, 0x5D5535B0), /* ~= 10^-319 */
  2309. U64(0xC5A89036, 0x2FDDBC62), U64(0xEB2189F7, 0x34AA831D), /* ~= 10^-318 */
  2310. U64(0xF712B443, 0xBBD52B7B), U64(0xA5E9EC75, 0x01D523E4), /* ~= 10^-317 */
  2311. U64(0x9A6BB0AA, 0x55653B2D), U64(0x47B233C9, 0x2125366E), /* ~= 10^-316 */
  2312. U64(0xC1069CD4, 0xEABE89F8), U64(0x999EC0BB, 0x696E840A), /* ~= 10^-315 */
  2313. U64(0xF148440A, 0x256E2C76), U64(0xC00670EA, 0x43CA250D), /* ~= 10^-314 */
  2314. U64(0x96CD2A86, 0x5764DBCA), U64(0x38040692, 0x6A5E5728), /* ~= 10^-313 */
  2315. U64(0xBC807527, 0xED3E12BC), U64(0xC6050837, 0x04F5ECF2), /* ~= 10^-312 */
  2316. U64(0xEBA09271, 0xE88D976B), U64(0xF7864A44, 0xC633682E), /* ~= 10^-311 */
  2317. U64(0x93445B87, 0x31587EA3), U64(0x7AB3EE6A, 0xFBE0211D), /* ~= 10^-310 */
  2318. U64(0xB8157268, 0xFDAE9E4C), U64(0x5960EA05, 0xBAD82964), /* ~= 10^-309 */
  2319. U64(0xE61ACF03, 0x3D1A45DF), U64(0x6FB92487, 0x298E33BD), /* ~= 10^-308 */
  2320. U64(0x8FD0C162, 0x06306BAB), U64(0xA5D3B6D4, 0x79F8E056), /* ~= 10^-307 */
  2321. U64(0xB3C4F1BA, 0x87BC8696), U64(0x8F48A489, 0x9877186C), /* ~= 10^-306 */
  2322. U64(0xE0B62E29, 0x29ABA83C), U64(0x331ACDAB, 0xFE94DE87), /* ~= 10^-305 */
  2323. U64(0x8C71DCD9, 0xBA0B4925), U64(0x9FF0C08B, 0x7F1D0B14), /* ~= 10^-304 */
  2324. U64(0xAF8E5410, 0x288E1B6F), U64(0x07ECF0AE, 0x5EE44DD9), /* ~= 10^-303 */
  2325. U64(0xDB71E914, 0x32B1A24A), U64(0xC9E82CD9, 0xF69D6150), /* ~= 10^-302 */
  2326. U64(0x892731AC, 0x9FAF056E), U64(0xBE311C08, 0x3A225CD2), /* ~= 10^-301 */
  2327. U64(0xAB70FE17, 0xC79AC6CA), U64(0x6DBD630A, 0x48AAF406), /* ~= 10^-300 */
  2328. U64(0xD64D3D9D, 0xB981787D), U64(0x092CBBCC, 0xDAD5B108), /* ~= 10^-299 */
  2329. U64(0x85F04682, 0x93F0EB4E), U64(0x25BBF560, 0x08C58EA5), /* ~= 10^-298 */
  2330. U64(0xA76C5823, 0x38ED2621), U64(0xAF2AF2B8, 0x0AF6F24E), /* ~= 10^-297 */
  2331. U64(0xD1476E2C, 0x07286FAA), U64(0x1AF5AF66, 0x0DB4AEE1), /* ~= 10^-296 */
  2332. U64(0x82CCA4DB, 0x847945CA), U64(0x50D98D9F, 0xC890ED4D), /* ~= 10^-295 */
  2333. U64(0xA37FCE12, 0x6597973C), U64(0xE50FF107, 0xBAB528A0), /* ~= 10^-294 */
  2334. U64(0xCC5FC196, 0xFEFD7D0C), U64(0x1E53ED49, 0xA96272C8), /* ~= 10^-293 */
  2335. U64(0xFF77B1FC, 0xBEBCDC4F), U64(0x25E8E89C, 0x13BB0F7A), /* ~= 10^-292 */
  2336. U64(0x9FAACF3D, 0xF73609B1), U64(0x77B19161, 0x8C54E9AC), /* ~= 10^-291 */
  2337. U64(0xC795830D, 0x75038C1D), U64(0xD59DF5B9, 0xEF6A2417), /* ~= 10^-290 */
  2338. U64(0xF97AE3D0, 0xD2446F25), U64(0x4B057328, 0x6B44AD1D), /* ~= 10^-289 */
  2339. U64(0x9BECCE62, 0x836AC577), U64(0x4EE367F9, 0x430AEC32), /* ~= 10^-288 */
  2340. U64(0xC2E801FB, 0x244576D5), U64(0x229C41F7, 0x93CDA73F), /* ~= 10^-287 */
  2341. U64(0xF3A20279, 0xED56D48A), U64(0x6B435275, 0x78C1110F), /* ~= 10^-286 */
  2342. U64(0x9845418C, 0x345644D6), U64(0x830A1389, 0x6B78AAA9), /* ~= 10^-285 */
  2343. U64(0xBE5691EF, 0x416BD60C), U64(0x23CC986B, 0xC656D553), /* ~= 10^-284 */
  2344. U64(0xEDEC366B, 0x11C6CB8F), U64(0x2CBFBE86, 0xB7EC8AA8), /* ~= 10^-283 */
  2345. U64(0x94B3A202, 0xEB1C3F39), U64(0x7BF7D714, 0x32F3D6A9), /* ~= 10^-282 */
  2346. U64(0xB9E08A83, 0xA5E34F07), U64(0xDAF5CCD9, 0x3FB0CC53), /* ~= 10^-281 */
  2347. U64(0xE858AD24, 0x8F5C22C9), U64(0xD1B3400F, 0x8F9CFF68), /* ~= 10^-280 */
  2348. U64(0x91376C36, 0xD99995BE), U64(0x23100809, 0xB9C21FA1), /* ~= 10^-279 */
  2349. U64(0xB5854744, 0x8FFFFB2D), U64(0xABD40A0C, 0x2832A78A), /* ~= 10^-278 */
  2350. U64(0xE2E69915, 0xB3FFF9F9), U64(0x16C90C8F, 0x323F516C), /* ~= 10^-277 */
  2351. U64(0x8DD01FAD, 0x907FFC3B), U64(0xAE3DA7D9, 0x7F6792E3), /* ~= 10^-276 */
  2352. U64(0xB1442798, 0xF49FFB4A), U64(0x99CD11CF, 0xDF41779C), /* ~= 10^-275 */
  2353. U64(0xDD95317F, 0x31C7FA1D), U64(0x40405643, 0xD711D583), /* ~= 10^-274 */
  2354. U64(0x8A7D3EEF, 0x7F1CFC52), U64(0x482835EA, 0x666B2572), /* ~= 10^-273 */
  2355. U64(0xAD1C8EAB, 0x5EE43B66), U64(0xDA324365, 0x0005EECF), /* ~= 10^-272 */
  2356. U64(0xD863B256, 0x369D4A40), U64(0x90BED43E, 0x40076A82), /* ~= 10^-271 */
  2357. U64(0x873E4F75, 0xE2224E68), U64(0x5A7744A6, 0xE804A291), /* ~= 10^-270 */
  2358. U64(0xA90DE353, 0x5AAAE202), U64(0x711515D0, 0xA205CB36), /* ~= 10^-269 */
  2359. U64(0xD3515C28, 0x31559A83), U64(0x0D5A5B44, 0xCA873E03), /* ~= 10^-268 */
  2360. U64(0x8412D999, 0x1ED58091), U64(0xE858790A, 0xFE9486C2), /* ~= 10^-267 */
  2361. U64(0xA5178FFF, 0x668AE0B6), U64(0x626E974D, 0xBE39A872), /* ~= 10^-266 */
  2362. U64(0xCE5D73FF, 0x402D98E3), U64(0xFB0A3D21, 0x2DC8128F), /* ~= 10^-265 */
  2363. U64(0x80FA687F, 0x881C7F8E), U64(0x7CE66634, 0xBC9D0B99), /* ~= 10^-264 */
  2364. U64(0xA139029F, 0x6A239F72), U64(0x1C1FFFC1, 0xEBC44E80), /* ~= 10^-263 */
  2365. U64(0xC9874347, 0x44AC874E), U64(0xA327FFB2, 0x66B56220), /* ~= 10^-262 */
  2366. U64(0xFBE91419, 0x15D7A922), U64(0x4BF1FF9F, 0x0062BAA8), /* ~= 10^-261 */
  2367. U64(0x9D71AC8F, 0xADA6C9B5), U64(0x6F773FC3, 0x603DB4A9), /* ~= 10^-260 */
  2368. U64(0xC4CE17B3, 0x99107C22), U64(0xCB550FB4, 0x384D21D3), /* ~= 10^-259 */
  2369. U64(0xF6019DA0, 0x7F549B2B), U64(0x7E2A53A1, 0x46606A48), /* ~= 10^-258 */
  2370. U64(0x99C10284, 0x4F94E0FB), U64(0x2EDA7444, 0xCBFC426D), /* ~= 10^-257 */
  2371. U64(0xC0314325, 0x637A1939), U64(0xFA911155, 0xFEFB5308), /* ~= 10^-256 */
  2372. U64(0xF03D93EE, 0xBC589F88), U64(0x793555AB, 0x7EBA27CA), /* ~= 10^-255 */
  2373. U64(0x96267C75, 0x35B763B5), U64(0x4BC1558B, 0x2F3458DE), /* ~= 10^-254 */
  2374. U64(0xBBB01B92, 0x83253CA2), U64(0x9EB1AAED, 0xFB016F16), /* ~= 10^-253 */
  2375. U64(0xEA9C2277, 0x23EE8BCB), U64(0x465E15A9, 0x79C1CADC), /* ~= 10^-252 */
  2376. U64(0x92A1958A, 0x7675175F), U64(0x0BFACD89, 0xEC191EC9), /* ~= 10^-251 */
  2377. U64(0xB749FAED, 0x14125D36), U64(0xCEF980EC, 0x671F667B), /* ~= 10^-250 */
  2378. U64(0xE51C79A8, 0x5916F484), U64(0x82B7E127, 0x80E7401A), /* ~= 10^-249 */
  2379. U64(0x8F31CC09, 0x37AE58D2), U64(0xD1B2ECB8, 0xB0908810), /* ~= 10^-248 */
  2380. U64(0xB2FE3F0B, 0x8599EF07), U64(0x861FA7E6, 0xDCB4AA15), /* ~= 10^-247 */
  2381. U64(0xDFBDCECE, 0x67006AC9), U64(0x67A791E0, 0x93E1D49A), /* ~= 10^-246 */
  2382. U64(0x8BD6A141, 0x006042BD), U64(0xE0C8BB2C, 0x5C6D24E0), /* ~= 10^-245 */
  2383. U64(0xAECC4991, 0x4078536D), U64(0x58FAE9F7, 0x73886E18), /* ~= 10^-244 */
  2384. U64(0xDA7F5BF5, 0x90966848), U64(0xAF39A475, 0x506A899E), /* ~= 10^-243 */
  2385. U64(0x888F9979, 0x7A5E012D), U64(0x6D8406C9, 0x52429603), /* ~= 10^-242 */
  2386. U64(0xAAB37FD7, 0xD8F58178), U64(0xC8E5087B, 0xA6D33B83), /* ~= 10^-241 */
  2387. U64(0xD5605FCD, 0xCF32E1D6), U64(0xFB1E4A9A, 0x90880A64), /* ~= 10^-240 */
  2388. U64(0x855C3BE0, 0xA17FCD26), U64(0x5CF2EEA0, 0x9A55067F), /* ~= 10^-239 */
  2389. U64(0xA6B34AD8, 0xC9DFC06F), U64(0xF42FAA48, 0xC0EA481E), /* ~= 10^-238 */
  2390. U64(0xD0601D8E, 0xFC57B08B), U64(0xF13B94DA, 0xF124DA26), /* ~= 10^-237 */
  2391. U64(0x823C1279, 0x5DB6CE57), U64(0x76C53D08, 0xD6B70858), /* ~= 10^-236 */
  2392. U64(0xA2CB1717, 0xB52481ED), U64(0x54768C4B, 0x0C64CA6E), /* ~= 10^-235 */
  2393. U64(0xCB7DDCDD, 0xA26DA268), U64(0xA9942F5D, 0xCF7DFD09), /* ~= 10^-234 */
  2394. U64(0xFE5D5415, 0x0B090B02), U64(0xD3F93B35, 0x435D7C4C), /* ~= 10^-233 */
  2395. U64(0x9EFA548D, 0x26E5A6E1), U64(0xC47BC501, 0x4A1A6DAF), /* ~= 10^-232 */
  2396. U64(0xC6B8E9B0, 0x709F109A), U64(0x359AB641, 0x9CA1091B), /* ~= 10^-231 */
  2397. U64(0xF867241C, 0x8CC6D4C0), U64(0xC30163D2, 0x03C94B62), /* ~= 10^-230 */
  2398. U64(0x9B407691, 0xD7FC44F8), U64(0x79E0DE63, 0x425DCF1D), /* ~= 10^-229 */
  2399. U64(0xC2109436, 0x4DFB5636), U64(0x985915FC, 0x12F542E4), /* ~= 10^-228 */
  2400. U64(0xF294B943, 0xE17A2BC4), U64(0x3E6F5B7B, 0x17B2939D), /* ~= 10^-227 */
  2401. U64(0x979CF3CA, 0x6CEC5B5A), U64(0xA705992C, 0xEECF9C42), /* ~= 10^-226 */
  2402. U64(0xBD8430BD, 0x08277231), U64(0x50C6FF78, 0x2A838353), /* ~= 10^-225 */
  2403. U64(0xECE53CEC, 0x4A314EBD), U64(0xA4F8BF56, 0x35246428), /* ~= 10^-224 */
  2404. U64(0x940F4613, 0xAE5ED136), U64(0x871B7795, 0xE136BE99), /* ~= 10^-223 */
  2405. U64(0xB9131798, 0x99F68584), U64(0x28E2557B, 0x59846E3F), /* ~= 10^-222 */
  2406. U64(0xE757DD7E, 0xC07426E5), U64(0x331AEADA, 0x2FE589CF), /* ~= 10^-221 */
  2407. U64(0x9096EA6F, 0x3848984F), U64(0x3FF0D2C8, 0x5DEF7621), /* ~= 10^-220 */
  2408. U64(0xB4BCA50B, 0x065ABE63), U64(0x0FED077A, 0x756B53A9), /* ~= 10^-219 */
  2409. U64(0xE1EBCE4D, 0xC7F16DFB), U64(0xD3E84959, 0x12C62894), /* ~= 10^-218 */
  2410. U64(0x8D3360F0, 0x9CF6E4BD), U64(0x64712DD7, 0xABBBD95C), /* ~= 10^-217 */
  2411. U64(0xB080392C, 0xC4349DEC), U64(0xBD8D794D, 0x96AACFB3), /* ~= 10^-216 */
  2412. U64(0xDCA04777, 0xF541C567), U64(0xECF0D7A0, 0xFC5583A0), /* ~= 10^-215 */
  2413. U64(0x89E42CAA, 0xF9491B60), U64(0xF41686C4, 0x9DB57244), /* ~= 10^-214 */
  2414. U64(0xAC5D37D5, 0xB79B6239), U64(0x311C2875, 0xC522CED5), /* ~= 10^-213 */
  2415. U64(0xD77485CB, 0x25823AC7), U64(0x7D633293, 0x366B828B), /* ~= 10^-212 */
  2416. U64(0x86A8D39E, 0xF77164BC), U64(0xAE5DFF9C, 0x02033197), /* ~= 10^-211 */
  2417. U64(0xA8530886, 0xB54DBDEB), U64(0xD9F57F83, 0x0283FDFC), /* ~= 10^-210 */
  2418. U64(0xD267CAA8, 0x62A12D66), U64(0xD072DF63, 0xC324FD7B), /* ~= 10^-209 */
  2419. U64(0x8380DEA9, 0x3DA4BC60), U64(0x4247CB9E, 0x59F71E6D), /* ~= 10^-208 */
  2420. U64(0xA4611653, 0x8D0DEB78), U64(0x52D9BE85, 0xF074E608), /* ~= 10^-207 */
  2421. U64(0xCD795BE8, 0x70516656), U64(0x67902E27, 0x6C921F8B), /* ~= 10^-206 */
  2422. U64(0x806BD971, 0x4632DFF6), U64(0x00BA1CD8, 0xA3DB53B6), /* ~= 10^-205 */
  2423. U64(0xA086CFCD, 0x97BF97F3), U64(0x80E8A40E, 0xCCD228A4), /* ~= 10^-204 */
  2424. U64(0xC8A883C0, 0xFDAF7DF0), U64(0x6122CD12, 0x8006B2CD), /* ~= 10^-203 */
  2425. U64(0xFAD2A4B1, 0x3D1B5D6C), U64(0x796B8057, 0x20085F81), /* ~= 10^-202 */
  2426. U64(0x9CC3A6EE, 0xC6311A63), U64(0xCBE33036, 0x74053BB0), /* ~= 10^-201 */
  2427. U64(0xC3F490AA, 0x77BD60FC), U64(0xBEDBFC44, 0x11068A9C), /* ~= 10^-200 */
  2428. U64(0xF4F1B4D5, 0x15ACB93B), U64(0xEE92FB55, 0x15482D44), /* ~= 10^-199 */
  2429. U64(0x99171105, 0x2D8BF3C5), U64(0x751BDD15, 0x2D4D1C4A), /* ~= 10^-198 */
  2430. U64(0xBF5CD546, 0x78EEF0B6), U64(0xD262D45A, 0x78A0635D), /* ~= 10^-197 */
  2431. U64(0xEF340A98, 0x172AACE4), U64(0x86FB8971, 0x16C87C34), /* ~= 10^-196 */
  2432. U64(0x9580869F, 0x0E7AAC0E), U64(0xD45D35E6, 0xAE3D4DA0), /* ~= 10^-195 */
  2433. U64(0xBAE0A846, 0xD2195712), U64(0x89748360, 0x59CCA109), /* ~= 10^-194 */
  2434. U64(0xE998D258, 0x869FACD7), U64(0x2BD1A438, 0x703FC94B), /* ~= 10^-193 */
  2435. U64(0x91FF8377, 0x5423CC06), U64(0x7B6306A3, 0x4627DDCF), /* ~= 10^-192 */
  2436. U64(0xB67F6455, 0x292CBF08), U64(0x1A3BC84C, 0x17B1D542), /* ~= 10^-191 */
  2437. U64(0xE41F3D6A, 0x7377EECA), U64(0x20CABA5F, 0x1D9E4A93), /* ~= 10^-190 */
  2438. U64(0x8E938662, 0x882AF53E), U64(0x547EB47B, 0x7282EE9C), /* ~= 10^-189 */
  2439. U64(0xB23867FB, 0x2A35B28D), U64(0xE99E619A, 0x4F23AA43), /* ~= 10^-188 */
  2440. U64(0xDEC681F9, 0xF4C31F31), U64(0x6405FA00, 0xE2EC94D4), /* ~= 10^-187 */
  2441. U64(0x8B3C113C, 0x38F9F37E), U64(0xDE83BC40, 0x8DD3DD04), /* ~= 10^-186 */
  2442. U64(0xAE0B158B, 0x4738705E), U64(0x9624AB50, 0xB148D445), /* ~= 10^-185 */
  2443. U64(0xD98DDAEE, 0x19068C76), U64(0x3BADD624, 0xDD9B0957), /* ~= 10^-184 */
  2444. U64(0x87F8A8D4, 0xCFA417C9), U64(0xE54CA5D7, 0x0A80E5D6), /* ~= 10^-183 */
  2445. U64(0xA9F6D30A, 0x038D1DBC), U64(0x5E9FCF4C, 0xCD211F4C), /* ~= 10^-182 */
  2446. U64(0xD47487CC, 0x8470652B), U64(0x7647C320, 0x0069671F), /* ~= 10^-181 */
  2447. U64(0x84C8D4DF, 0xD2C63F3B), U64(0x29ECD9F4, 0x0041E073), /* ~= 10^-180 */
  2448. U64(0xA5FB0A17, 0xC777CF09), U64(0xF4681071, 0x00525890), /* ~= 10^-179 */
  2449. U64(0xCF79CC9D, 0xB955C2CC), U64(0x7182148D, 0x4066EEB4), /* ~= 10^-178 */
  2450. U64(0x81AC1FE2, 0x93D599BF), U64(0xC6F14CD8, 0x48405530), /* ~= 10^-177 */
  2451. U64(0xA21727DB, 0x38CB002F), U64(0xB8ADA00E, 0x5A506A7C), /* ~= 10^-176 */
  2452. U64(0xCA9CF1D2, 0x06FDC03B), U64(0xA6D90811, 0xF0E4851C), /* ~= 10^-175 */
  2453. U64(0xFD442E46, 0x88BD304A), U64(0x908F4A16, 0x6D1DA663), /* ~= 10^-174 */
  2454. U64(0x9E4A9CEC, 0x15763E2E), U64(0x9A598E4E, 0x043287FE), /* ~= 10^-173 */
  2455. U64(0xC5DD4427, 0x1AD3CDBA), U64(0x40EFF1E1, 0x853F29FD), /* ~= 10^-172 */
  2456. U64(0xF7549530, 0xE188C128), U64(0xD12BEE59, 0xE68EF47C), /* ~= 10^-171 */
  2457. U64(0x9A94DD3E, 0x8CF578B9), U64(0x82BB74F8, 0x301958CE), /* ~= 10^-170 */
  2458. U64(0xC13A148E, 0x3032D6E7), U64(0xE36A5236, 0x3C1FAF01), /* ~= 10^-169 */
  2459. U64(0xF18899B1, 0xBC3F8CA1), U64(0xDC44E6C3, 0xCB279AC1), /* ~= 10^-168 */
  2460. U64(0x96F5600F, 0x15A7B7E5), U64(0x29AB103A, 0x5EF8C0B9), /* ~= 10^-167 */
  2461. U64(0xBCB2B812, 0xDB11A5DE), U64(0x7415D448, 0xF6B6F0E7), /* ~= 10^-166 */
  2462. U64(0xEBDF6617, 0x91D60F56), U64(0x111B495B, 0x3464AD21), /* ~= 10^-165 */
  2463. U64(0x936B9FCE, 0xBB25C995), U64(0xCAB10DD9, 0x00BEEC34), /* ~= 10^-164 */
  2464. U64(0xB84687C2, 0x69EF3BFB), U64(0x3D5D514F, 0x40EEA742), /* ~= 10^-163 */
  2465. U64(0xE65829B3, 0x046B0AFA), U64(0x0CB4A5A3, 0x112A5112), /* ~= 10^-162 */
  2466. U64(0x8FF71A0F, 0xE2C2E6DC), U64(0x47F0E785, 0xEABA72AB), /* ~= 10^-161 */
  2467. U64(0xB3F4E093, 0xDB73A093), U64(0x59ED2167, 0x65690F56), /* ~= 10^-160 */
  2468. U64(0xE0F218B8, 0xD25088B8), U64(0x306869C1, 0x3EC3532C), /* ~= 10^-159 */
  2469. U64(0x8C974F73, 0x83725573), U64(0x1E414218, 0xC73A13FB), /* ~= 10^-158 */
  2470. U64(0xAFBD2350, 0x644EEACF), U64(0xE5D1929E, 0xF90898FA), /* ~= 10^-157 */
  2471. U64(0xDBAC6C24, 0x7D62A583), U64(0xDF45F746, 0xB74ABF39), /* ~= 10^-156 */
  2472. U64(0x894BC396, 0xCE5DA772), U64(0x6B8BBA8C, 0x328EB783), /* ~= 10^-155 */
  2473. U64(0xAB9EB47C, 0x81F5114F), U64(0x066EA92F, 0x3F326564), /* ~= 10^-154 */
  2474. U64(0xD686619B, 0xA27255A2), U64(0xC80A537B, 0x0EFEFEBD), /* ~= 10^-153 */
  2475. U64(0x8613FD01, 0x45877585), U64(0xBD06742C, 0xE95F5F36), /* ~= 10^-152 */
  2476. U64(0xA798FC41, 0x96E952E7), U64(0x2C481138, 0x23B73704), /* ~= 10^-151 */
  2477. U64(0xD17F3B51, 0xFCA3A7A0), U64(0xF75A1586, 0x2CA504C5), /* ~= 10^-150 */
  2478. U64(0x82EF8513, 0x3DE648C4), U64(0x9A984D73, 0xDBE722FB), /* ~= 10^-149 */
  2479. U64(0xA3AB6658, 0x0D5FDAF5), U64(0xC13E60D0, 0xD2E0EBBA), /* ~= 10^-148 */
  2480. U64(0xCC963FEE, 0x10B7D1B3), U64(0x318DF905, 0x079926A8), /* ~= 10^-147 */
  2481. U64(0xFFBBCFE9, 0x94E5C61F), U64(0xFDF17746, 0x497F7052), /* ~= 10^-146 */
  2482. U64(0x9FD561F1, 0xFD0F9BD3), U64(0xFEB6EA8B, 0xEDEFA633), /* ~= 10^-145 */
  2483. U64(0xC7CABA6E, 0x7C5382C8), U64(0xFE64A52E, 0xE96B8FC0), /* ~= 10^-144 */
  2484. U64(0xF9BD690A, 0x1B68637B), U64(0x3DFDCE7A, 0xA3C673B0), /* ~= 10^-143 */
  2485. U64(0x9C1661A6, 0x51213E2D), U64(0x06BEA10C, 0xA65C084E), /* ~= 10^-142 */
  2486. U64(0xC31BFA0F, 0xE5698DB8), U64(0x486E494F, 0xCFF30A62), /* ~= 10^-141 */
  2487. U64(0xF3E2F893, 0xDEC3F126), U64(0x5A89DBA3, 0xC3EFCCFA), /* ~= 10^-140 */
  2488. U64(0x986DDB5C, 0x6B3A76B7), U64(0xF8962946, 0x5A75E01C), /* ~= 10^-139 */
  2489. U64(0xBE895233, 0x86091465), U64(0xF6BBB397, 0xF1135823), /* ~= 10^-138 */
  2490. U64(0xEE2BA6C0, 0x678B597F), U64(0x746AA07D, 0xED582E2C), /* ~= 10^-137 */
  2491. U64(0x94DB4838, 0x40B717EF), U64(0xA8C2A44E, 0xB4571CDC), /* ~= 10^-136 */
  2492. U64(0xBA121A46, 0x50E4DDEB), U64(0x92F34D62, 0x616CE413), /* ~= 10^-135 */
  2493. U64(0xE896A0D7, 0xE51E1566), U64(0x77B020BA, 0xF9C81D17), /* ~= 10^-134 */
  2494. U64(0x915E2486, 0xEF32CD60), U64(0x0ACE1474, 0xDC1D122E), /* ~= 10^-133 */
  2495. U64(0xB5B5ADA8, 0xAAFF80B8), U64(0x0D819992, 0x132456BA), /* ~= 10^-132 */
  2496. U64(0xE3231912, 0xD5BF60E6), U64(0x10E1FFF6, 0x97ED6C69), /* ~= 10^-131 */
  2497. U64(0x8DF5EFAB, 0xC5979C8F), U64(0xCA8D3FFA, 0x1EF463C1), /* ~= 10^-130 */
  2498. U64(0xB1736B96, 0xB6FD83B3), U64(0xBD308FF8, 0xA6B17CB2), /* ~= 10^-129 */
  2499. U64(0xDDD0467C, 0x64BCE4A0), U64(0xAC7CB3F6, 0xD05DDBDE), /* ~= 10^-128 */
  2500. U64(0x8AA22C0D, 0xBEF60EE4), U64(0x6BCDF07A, 0x423AA96B), /* ~= 10^-127 */
  2501. U64(0xAD4AB711, 0x2EB3929D), U64(0x86C16C98, 0xD2C953C6), /* ~= 10^-126 */
  2502. U64(0xD89D64D5, 0x7A607744), U64(0xE871C7BF, 0x077BA8B7), /* ~= 10^-125 */
  2503. U64(0x87625F05, 0x6C7C4A8B), U64(0x11471CD7, 0x64AD4972), /* ~= 10^-124 */
  2504. U64(0xA93AF6C6, 0xC79B5D2D), U64(0xD598E40D, 0x3DD89BCF), /* ~= 10^-123 */
  2505. U64(0xD389B478, 0x79823479), U64(0x4AFF1D10, 0x8D4EC2C3), /* ~= 10^-122 */
  2506. U64(0x843610CB, 0x4BF160CB), U64(0xCEDF722A, 0x585139BA), /* ~= 10^-121 */
  2507. U64(0xA54394FE, 0x1EEDB8FE), U64(0xC2974EB4, 0xEE658828), /* ~= 10^-120 */
  2508. U64(0xCE947A3D, 0xA6A9273E), U64(0x733D2262, 0x29FEEA32), /* ~= 10^-119 */
  2509. U64(0x811CCC66, 0x8829B887), U64(0x0806357D, 0x5A3F525F), /* ~= 10^-118 */
  2510. U64(0xA163FF80, 0x2A3426A8), U64(0xCA07C2DC, 0xB0CF26F7), /* ~= 10^-117 */
  2511. U64(0xC9BCFF60, 0x34C13052), U64(0xFC89B393, 0xDD02F0B5), /* ~= 10^-116 */
  2512. U64(0xFC2C3F38, 0x41F17C67), U64(0xBBAC2078, 0xD443ACE2), /* ~= 10^-115 */
  2513. U64(0x9D9BA783, 0x2936EDC0), U64(0xD54B944B, 0x84AA4C0D), /* ~= 10^-114 */
  2514. U64(0xC5029163, 0xF384A931), U64(0x0A9E795E, 0x65D4DF11), /* ~= 10^-113 */
  2515. U64(0xF64335BC, 0xF065D37D), U64(0x4D4617B5, 0xFF4A16D5), /* ~= 10^-112 */
  2516. U64(0x99EA0196, 0x163FA42E), U64(0x504BCED1, 0xBF8E4E45), /* ~= 10^-111 */
  2517. U64(0xC06481FB, 0x9BCF8D39), U64(0xE45EC286, 0x2F71E1D6), /* ~= 10^-110 */
  2518. U64(0xF07DA27A, 0x82C37088), U64(0x5D767327, 0xBB4E5A4C), /* ~= 10^-109 */
  2519. U64(0x964E858C, 0x91BA2655), U64(0x3A6A07F8, 0xD510F86F), /* ~= 10^-108 */
  2520. U64(0xBBE226EF, 0xB628AFEA), U64(0x890489F7, 0x0A55368B), /* ~= 10^-107 */
  2521. U64(0xEADAB0AB, 0xA3B2DBE5), U64(0x2B45AC74, 0xCCEA842E), /* ~= 10^-106 */
  2522. U64(0x92C8AE6B, 0x464FC96F), U64(0x3B0B8BC9, 0x0012929D), /* ~= 10^-105 */
  2523. U64(0xB77ADA06, 0x17E3BBCB), U64(0x09CE6EBB, 0x40173744), /* ~= 10^-104 */
  2524. U64(0xE5599087, 0x9DDCAABD), U64(0xCC420A6A, 0x101D0515), /* ~= 10^-103 */
  2525. U64(0x8F57FA54, 0xC2A9EAB6), U64(0x9FA94682, 0x4A12232D), /* ~= 10^-102 */
  2526. U64(0xB32DF8E9, 0xF3546564), U64(0x47939822, 0xDC96ABF9), /* ~= 10^-101 */
  2527. U64(0xDFF97724, 0x70297EBD), U64(0x59787E2B, 0x93BC56F7), /* ~= 10^-100 */
  2528. U64(0x8BFBEA76, 0xC619EF36), U64(0x57EB4EDB, 0x3C55B65A), /* ~= 10^-99 */
  2529. U64(0xAEFAE514, 0x77A06B03), U64(0xEDE62292, 0x0B6B23F1), /* ~= 10^-98 */
  2530. U64(0xDAB99E59, 0x958885C4), U64(0xE95FAB36, 0x8E45ECED), /* ~= 10^-97 */
  2531. U64(0x88B402F7, 0xFD75539B), U64(0x11DBCB02, 0x18EBB414), /* ~= 10^-96 */
  2532. U64(0xAAE103B5, 0xFCD2A881), U64(0xD652BDC2, 0x9F26A119), /* ~= 10^-95 */
  2533. U64(0xD59944A3, 0x7C0752A2), U64(0x4BE76D33, 0x46F0495F), /* ~= 10^-94 */
  2534. U64(0x857FCAE6, 0x2D8493A5), U64(0x6F70A440, 0x0C562DDB), /* ~= 10^-93 */
  2535. U64(0xA6DFBD9F, 0xB8E5B88E), U64(0xCB4CCD50, 0x0F6BB952), /* ~= 10^-92 */
  2536. U64(0xD097AD07, 0xA71F26B2), U64(0x7E2000A4, 0x1346A7A7), /* ~= 10^-91 */
  2537. U64(0x825ECC24, 0xC873782F), U64(0x8ED40066, 0x8C0C28C8), /* ~= 10^-90 */
  2538. U64(0xA2F67F2D, 0xFA90563B), U64(0x72890080, 0x2F0F32FA), /* ~= 10^-89 */
  2539. U64(0xCBB41EF9, 0x79346BCA), U64(0x4F2B40A0, 0x3AD2FFB9), /* ~= 10^-88 */
  2540. U64(0xFEA126B7, 0xD78186BC), U64(0xE2F610C8, 0x4987BFA8), /* ~= 10^-87 */
  2541. U64(0x9F24B832, 0xE6B0F436), U64(0x0DD9CA7D, 0x2DF4D7C9), /* ~= 10^-86 */
  2542. U64(0xC6EDE63F, 0xA05D3143), U64(0x91503D1C, 0x79720DBB), /* ~= 10^-85 */
  2543. U64(0xF8A95FCF, 0x88747D94), U64(0x75A44C63, 0x97CE912A), /* ~= 10^-84 */
  2544. U64(0x9B69DBE1, 0xB548CE7C), U64(0xC986AFBE, 0x3EE11ABA), /* ~= 10^-83 */
  2545. U64(0xC24452DA, 0x229B021B), U64(0xFBE85BAD, 0xCE996168), /* ~= 10^-82 */
  2546. U64(0xF2D56790, 0xAB41C2A2), U64(0xFAE27299, 0x423FB9C3), /* ~= 10^-81 */
  2547. U64(0x97C560BA, 0x6B0919A5), U64(0xDCCD879F, 0xC967D41A), /* ~= 10^-80 */
  2548. U64(0xBDB6B8E9, 0x05CB600F), U64(0x5400E987, 0xBBC1C920), /* ~= 10^-79 */
  2549. U64(0xED246723, 0x473E3813), U64(0x290123E9, 0xAAB23B68), /* ~= 10^-78 */
  2550. U64(0x9436C076, 0x0C86E30B), U64(0xF9A0B672, 0x0AAF6521), /* ~= 10^-77 */
  2551. U64(0xB9447093, 0x8FA89BCE), U64(0xF808E40E, 0x8D5B3E69), /* ~= 10^-76 */
  2552. U64(0xE7958CB8, 0x7392C2C2), U64(0xB60B1D12, 0x30B20E04), /* ~= 10^-75 */
  2553. U64(0x90BD77F3, 0x483BB9B9), U64(0xB1C6F22B, 0x5E6F48C2), /* ~= 10^-74 */
  2554. U64(0xB4ECD5F0, 0x1A4AA828), U64(0x1E38AEB6, 0x360B1AF3), /* ~= 10^-73 */
  2555. U64(0xE2280B6C, 0x20DD5232), U64(0x25C6DA63, 0xC38DE1B0), /* ~= 10^-72 */
  2556. U64(0x8D590723, 0x948A535F), U64(0x579C487E, 0x5A38AD0E), /* ~= 10^-71 */
  2557. U64(0xB0AF48EC, 0x79ACE837), U64(0x2D835A9D, 0xF0C6D851), /* ~= 10^-70 */
  2558. U64(0xDCDB1B27, 0x98182244), U64(0xF8E43145, 0x6CF88E65), /* ~= 10^-69 */
  2559. U64(0x8A08F0F8, 0xBF0F156B), U64(0x1B8E9ECB, 0x641B58FF), /* ~= 10^-68 */
  2560. U64(0xAC8B2D36, 0xEED2DAC5), U64(0xE272467E, 0x3D222F3F), /* ~= 10^-67 */
  2561. U64(0xD7ADF884, 0xAA879177), U64(0x5B0ED81D, 0xCC6ABB0F), /* ~= 10^-66 */
  2562. U64(0x86CCBB52, 0xEA94BAEA), U64(0x98E94712, 0x9FC2B4E9), /* ~= 10^-65 */
  2563. U64(0xA87FEA27, 0xA539E9A5), U64(0x3F2398D7, 0x47B36224), /* ~= 10^-64 */
  2564. U64(0xD29FE4B1, 0x8E88640E), U64(0x8EEC7F0D, 0x19A03AAD), /* ~= 10^-63 */
  2565. U64(0x83A3EEEE, 0xF9153E89), U64(0x1953CF68, 0x300424AC), /* ~= 10^-62 */
  2566. U64(0xA48CEAAA, 0xB75A8E2B), U64(0x5FA8C342, 0x3C052DD7), /* ~= 10^-61 */
  2567. U64(0xCDB02555, 0x653131B6), U64(0x3792F412, 0xCB06794D), /* ~= 10^-60 */
  2568. U64(0x808E1755, 0x5F3EBF11), U64(0xE2BBD88B, 0xBEE40BD0), /* ~= 10^-59 */
  2569. U64(0xA0B19D2A, 0xB70E6ED6), U64(0x5B6ACEAE, 0xAE9D0EC4), /* ~= 10^-58 */
  2570. U64(0xC8DE0475, 0x64D20A8B), U64(0xF245825A, 0x5A445275), /* ~= 10^-57 */
  2571. U64(0xFB158592, 0xBE068D2E), U64(0xEED6E2F0, 0xF0D56712), /* ~= 10^-56 */
  2572. U64(0x9CED737B, 0xB6C4183D), U64(0x55464DD6, 0x9685606B), /* ~= 10^-55 */
  2573. U64(0xC428D05A, 0xA4751E4C), U64(0xAA97E14C, 0x3C26B886), /* ~= 10^-54 */
  2574. U64(0xF5330471, 0x4D9265DF), U64(0xD53DD99F, 0x4B3066A8), /* ~= 10^-53 */
  2575. U64(0x993FE2C6, 0xD07B7FAB), U64(0xE546A803, 0x8EFE4029), /* ~= 10^-52 */
  2576. U64(0xBF8FDB78, 0x849A5F96), U64(0xDE985204, 0x72BDD033), /* ~= 10^-51 */
  2577. U64(0xEF73D256, 0xA5C0F77C), U64(0x963E6685, 0x8F6D4440), /* ~= 10^-50 */
  2578. U64(0x95A86376, 0x27989AAD), U64(0xDDE70013, 0x79A44AA8), /* ~= 10^-49 */
  2579. U64(0xBB127C53, 0xB17EC159), U64(0x5560C018, 0x580D5D52), /* ~= 10^-48 */
  2580. U64(0xE9D71B68, 0x9DDE71AF), U64(0xAAB8F01E, 0x6E10B4A6), /* ~= 10^-47 */
  2581. U64(0x92267121, 0x62AB070D), U64(0xCAB39613, 0x04CA70E8), /* ~= 10^-46 */
  2582. U64(0xB6B00D69, 0xBB55C8D1), U64(0x3D607B97, 0xC5FD0D22), /* ~= 10^-45 */
  2583. U64(0xE45C10C4, 0x2A2B3B05), U64(0x8CB89A7D, 0xB77C506A), /* ~= 10^-44 */
  2584. U64(0x8EB98A7A, 0x9A5B04E3), U64(0x77F3608E, 0x92ADB242), /* ~= 10^-43 */
  2585. U64(0xB267ED19, 0x40F1C61C), U64(0x55F038B2, 0x37591ED3), /* ~= 10^-42 */
  2586. U64(0xDF01E85F, 0x912E37A3), U64(0x6B6C46DE, 0xC52F6688), /* ~= 10^-41 */
  2587. U64(0x8B61313B, 0xBABCE2C6), U64(0x2323AC4B, 0x3B3DA015), /* ~= 10^-40 */
  2588. U64(0xAE397D8A, 0xA96C1B77), U64(0xABEC975E, 0x0A0D081A), /* ~= 10^-39 */
  2589. U64(0xD9C7DCED, 0x53C72255), U64(0x96E7BD35, 0x8C904A21), /* ~= 10^-38 */
  2590. U64(0x881CEA14, 0x545C7575), U64(0x7E50D641, 0x77DA2E54), /* ~= 10^-37 */
  2591. U64(0xAA242499, 0x697392D2), U64(0xDDE50BD1, 0xD5D0B9E9), /* ~= 10^-36 */
  2592. U64(0xD4AD2DBF, 0xC3D07787), U64(0x955E4EC6, 0x4B44E864), /* ~= 10^-35 */
  2593. U64(0x84EC3C97, 0xDA624AB4), U64(0xBD5AF13B, 0xEF0B113E), /* ~= 10^-34 */
  2594. U64(0xA6274BBD, 0xD0FADD61), U64(0xECB1AD8A, 0xEACDD58E), /* ~= 10^-33 */
  2595. U64(0xCFB11EAD, 0x453994BA), U64(0x67DE18ED, 0xA5814AF2), /* ~= 10^-32 */
  2596. U64(0x81CEB32C, 0x4B43FCF4), U64(0x80EACF94, 0x8770CED7), /* ~= 10^-31 */
  2597. U64(0xA2425FF7, 0x5E14FC31), U64(0xA1258379, 0xA94D028D), /* ~= 10^-30 */
  2598. U64(0xCAD2F7F5, 0x359A3B3E), U64(0x096EE458, 0x13A04330), /* ~= 10^-29 */
  2599. U64(0xFD87B5F2, 0x8300CA0D), U64(0x8BCA9D6E, 0x188853FC), /* ~= 10^-28 */
  2600. U64(0x9E74D1B7, 0x91E07E48), U64(0x775EA264, 0xCF55347D), /* ~= 10^-27 */
  2601. U64(0xC6120625, 0x76589DDA), U64(0x95364AFE, 0x032A819D), /* ~= 10^-26 */
  2602. U64(0xF79687AE, 0xD3EEC551), U64(0x3A83DDBD, 0x83F52204), /* ~= 10^-25 */
  2603. U64(0x9ABE14CD, 0x44753B52), U64(0xC4926A96, 0x72793542), /* ~= 10^-24 */
  2604. U64(0xC16D9A00, 0x95928A27), U64(0x75B7053C, 0x0F178293), /* ~= 10^-23 */
  2605. U64(0xF1C90080, 0xBAF72CB1), U64(0x5324C68B, 0x12DD6338), /* ~= 10^-22 */
  2606. U64(0x971DA050, 0x74DA7BEE), U64(0xD3F6FC16, 0xEBCA5E03), /* ~= 10^-21 */
  2607. U64(0xBCE50864, 0x92111AEA), U64(0x88F4BB1C, 0xA6BCF584), /* ~= 10^-20 */
  2608. U64(0xEC1E4A7D, 0xB69561A5), U64(0x2B31E9E3, 0xD06C32E5), /* ~= 10^-19 */
  2609. U64(0x9392EE8E, 0x921D5D07), U64(0x3AFF322E, 0x62439FCF), /* ~= 10^-18 */
  2610. U64(0xB877AA32, 0x36A4B449), U64(0x09BEFEB9, 0xFAD487C2), /* ~= 10^-17 */
  2611. U64(0xE69594BE, 0xC44DE15B), U64(0x4C2EBE68, 0x7989A9B3), /* ~= 10^-16 */
  2612. U64(0x901D7CF7, 0x3AB0ACD9), U64(0x0F9D3701, 0x4BF60A10), /* ~= 10^-15 */
  2613. U64(0xB424DC35, 0x095CD80F), U64(0x538484C1, 0x9EF38C94), /* ~= 10^-14 */
  2614. U64(0xE12E1342, 0x4BB40E13), U64(0x2865A5F2, 0x06B06FB9), /* ~= 10^-13 */
  2615. U64(0x8CBCCC09, 0x6F5088CB), U64(0xF93F87B7, 0x442E45D3), /* ~= 10^-12 */
  2616. U64(0xAFEBFF0B, 0xCB24AAFE), U64(0xF78F69A5, 0x1539D748), /* ~= 10^-11 */
  2617. U64(0xDBE6FECE, 0xBDEDD5BE), U64(0xB573440E, 0x5A884D1B), /* ~= 10^-10 */
  2618. U64(0x89705F41, 0x36B4A597), U64(0x31680A88, 0xF8953030), /* ~= 10^-9 */
  2619. U64(0xABCC7711, 0x8461CEFC), U64(0xFDC20D2B, 0x36BA7C3D), /* ~= 10^-8 */
  2620. U64(0xD6BF94D5, 0xE57A42BC), U64(0x3D329076, 0x04691B4C), /* ~= 10^-7 */
  2621. U64(0x8637BD05, 0xAF6C69B5), U64(0xA63F9A49, 0xC2C1B10F), /* ~= 10^-6 */
  2622. U64(0xA7C5AC47, 0x1B478423), U64(0x0FCF80DC, 0x33721D53), /* ~= 10^-5 */
  2623. U64(0xD1B71758, 0xE219652B), U64(0xD3C36113, 0x404EA4A8), /* ~= 10^-4 */
  2624. U64(0x83126E97, 0x8D4FDF3B), U64(0x645A1CAC, 0x083126E9), /* ~= 10^-3 */
  2625. U64(0xA3D70A3D, 0x70A3D70A), U64(0x3D70A3D7, 0x0A3D70A3), /* ~= 10^-2 */
  2626. U64(0xCCCCCCCC, 0xCCCCCCCC), U64(0xCCCCCCCC, 0xCCCCCCCC), /* ~= 10^-1 */
  2627. U64(0x80000000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^0 */
  2628. U64(0xA0000000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^1 */
  2629. U64(0xC8000000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^2 */
  2630. U64(0xFA000000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^3 */
  2631. U64(0x9C400000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^4 */
  2632. U64(0xC3500000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^5 */
  2633. U64(0xF4240000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^6 */
  2634. U64(0x98968000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^7 */
  2635. U64(0xBEBC2000, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^8 */
  2636. U64(0xEE6B2800, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^9 */
  2637. U64(0x9502F900, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^10 */
  2638. U64(0xBA43B740, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^11 */
  2639. U64(0xE8D4A510, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^12 */
  2640. U64(0x9184E72A, 0x00000000), U64(0x00000000, 0x00000000), /* == 10^13 */
  2641. U64(0xB5E620F4, 0x80000000), U64(0x00000000, 0x00000000), /* == 10^14 */
  2642. U64(0xE35FA931, 0xA0000000), U64(0x00000000, 0x00000000), /* == 10^15 */
  2643. U64(0x8E1BC9BF, 0x04000000), U64(0x00000000, 0x00000000), /* == 10^16 */
  2644. U64(0xB1A2BC2E, 0xC5000000), U64(0x00000000, 0x00000000), /* == 10^17 */
  2645. U64(0xDE0B6B3A, 0x76400000), U64(0x00000000, 0x00000000), /* == 10^18 */
  2646. U64(0x8AC72304, 0x89E80000), U64(0x00000000, 0x00000000), /* == 10^19 */
  2647. U64(0xAD78EBC5, 0xAC620000), U64(0x00000000, 0x00000000), /* == 10^20 */
  2648. U64(0xD8D726B7, 0x177A8000), U64(0x00000000, 0x00000000), /* == 10^21 */
  2649. U64(0x87867832, 0x6EAC9000), U64(0x00000000, 0x00000000), /* == 10^22 */
  2650. U64(0xA968163F, 0x0A57B400), U64(0x00000000, 0x00000000), /* == 10^23 */
  2651. U64(0xD3C21BCE, 0xCCEDA100), U64(0x00000000, 0x00000000), /* == 10^24 */
  2652. U64(0x84595161, 0x401484A0), U64(0x00000000, 0x00000000), /* == 10^25 */
  2653. U64(0xA56FA5B9, 0x9019A5C8), U64(0x00000000, 0x00000000), /* == 10^26 */
  2654. U64(0xCECB8F27, 0xF4200F3A), U64(0x00000000, 0x00000000), /* == 10^27 */
  2655. U64(0x813F3978, 0xF8940984), U64(0x40000000, 0x00000000), /* == 10^28 */
  2656. U64(0xA18F07D7, 0x36B90BE5), U64(0x50000000, 0x00000000), /* == 10^29 */
  2657. U64(0xC9F2C9CD, 0x04674EDE), U64(0xA4000000, 0x00000000), /* == 10^30 */
  2658. U64(0xFC6F7C40, 0x45812296), U64(0x4D000000, 0x00000000), /* == 10^31 */
  2659. U64(0x9DC5ADA8, 0x2B70B59D), U64(0xF0200000, 0x00000000), /* == 10^32 */
  2660. U64(0xC5371912, 0x364CE305), U64(0x6C280000, 0x00000000), /* == 10^33 */
  2661. U64(0xF684DF56, 0xC3E01BC6), U64(0xC7320000, 0x00000000), /* == 10^34 */
  2662. U64(0x9A130B96, 0x3A6C115C), U64(0x3C7F4000, 0x00000000), /* == 10^35 */
  2663. U64(0xC097CE7B, 0xC90715B3), U64(0x4B9F1000, 0x00000000), /* == 10^36 */
  2664. U64(0xF0BDC21A, 0xBB48DB20), U64(0x1E86D400, 0x00000000), /* == 10^37 */
  2665. U64(0x96769950, 0xB50D88F4), U64(0x13144480, 0x00000000), /* == 10^38 */
  2666. U64(0xBC143FA4, 0xE250EB31), U64(0x17D955A0, 0x00000000), /* == 10^39 */
  2667. U64(0xEB194F8E, 0x1AE525FD), U64(0x5DCFAB08, 0x00000000), /* == 10^40 */
  2668. U64(0x92EFD1B8, 0xD0CF37BE), U64(0x5AA1CAE5, 0x00000000), /* == 10^41 */
  2669. U64(0xB7ABC627, 0x050305AD), U64(0xF14A3D9E, 0x40000000), /* == 10^42 */
  2670. U64(0xE596B7B0, 0xC643C719), U64(0x6D9CCD05, 0xD0000000), /* == 10^43 */
  2671. U64(0x8F7E32CE, 0x7BEA5C6F), U64(0xE4820023, 0xA2000000), /* == 10^44 */
  2672. U64(0xB35DBF82, 0x1AE4F38B), U64(0xDDA2802C, 0x8A800000), /* == 10^45 */
  2673. U64(0xE0352F62, 0xA19E306E), U64(0xD50B2037, 0xAD200000), /* == 10^46 */
  2674. U64(0x8C213D9D, 0xA502DE45), U64(0x4526F422, 0xCC340000), /* == 10^47 */
  2675. U64(0xAF298D05, 0x0E4395D6), U64(0x9670B12B, 0x7F410000), /* == 10^48 */
  2676. U64(0xDAF3F046, 0x51D47B4C), U64(0x3C0CDD76, 0x5F114000), /* == 10^49 */
  2677. U64(0x88D8762B, 0xF324CD0F), U64(0xA5880A69, 0xFB6AC800), /* == 10^50 */
  2678. U64(0xAB0E93B6, 0xEFEE0053), U64(0x8EEA0D04, 0x7A457A00), /* == 10^51 */
  2679. U64(0xD5D238A4, 0xABE98068), U64(0x72A49045, 0x98D6D880), /* == 10^52 */
  2680. U64(0x85A36366, 0xEB71F041), U64(0x47A6DA2B, 0x7F864750), /* == 10^53 */
  2681. U64(0xA70C3C40, 0xA64E6C51), U64(0x999090B6, 0x5F67D924), /* == 10^54 */
  2682. U64(0xD0CF4B50, 0xCFE20765), U64(0xFFF4B4E3, 0xF741CF6D), /* == 10^55 */
  2683. U64(0x82818F12, 0x81ED449F), U64(0xBFF8F10E, 0x7A8921A4), /* ~= 10^56 */
  2684. U64(0xA321F2D7, 0x226895C7), U64(0xAFF72D52, 0x192B6A0D), /* ~= 10^57 */
  2685. U64(0xCBEA6F8C, 0xEB02BB39), U64(0x9BF4F8A6, 0x9F764490), /* ~= 10^58 */
  2686. U64(0xFEE50B70, 0x25C36A08), U64(0x02F236D0, 0x4753D5B4), /* ~= 10^59 */
  2687. U64(0x9F4F2726, 0x179A2245), U64(0x01D76242, 0x2C946590), /* ~= 10^60 */
  2688. U64(0xC722F0EF, 0x9D80AAD6), U64(0x424D3AD2, 0xB7B97EF5), /* ~= 10^61 */
  2689. U64(0xF8EBAD2B, 0x84E0D58B), U64(0xD2E08987, 0x65A7DEB2), /* ~= 10^62 */
  2690. U64(0x9B934C3B, 0x330C8577), U64(0x63CC55F4, 0x9F88EB2F), /* ~= 10^63 */
  2691. U64(0xC2781F49, 0xFFCFA6D5), U64(0x3CBF6B71, 0xC76B25FB), /* ~= 10^64 */
  2692. U64(0xF316271C, 0x7FC3908A), U64(0x8BEF464E, 0x3945EF7A), /* ~= 10^65 */
  2693. U64(0x97EDD871, 0xCFDA3A56), U64(0x97758BF0, 0xE3CBB5AC), /* ~= 10^66 */
  2694. U64(0xBDE94E8E, 0x43D0C8EC), U64(0x3D52EEED, 0x1CBEA317), /* ~= 10^67 */
  2695. U64(0xED63A231, 0xD4C4FB27), U64(0x4CA7AAA8, 0x63EE4BDD), /* ~= 10^68 */
  2696. U64(0x945E455F, 0x24FB1CF8), U64(0x8FE8CAA9, 0x3E74EF6A), /* ~= 10^69 */
  2697. U64(0xB975D6B6, 0xEE39E436), U64(0xB3E2FD53, 0x8E122B44), /* ~= 10^70 */
  2698. U64(0xE7D34C64, 0xA9C85D44), U64(0x60DBBCA8, 0x7196B616), /* ~= 10^71 */
  2699. U64(0x90E40FBE, 0xEA1D3A4A), U64(0xBC8955E9, 0x46FE31CD), /* ~= 10^72 */
  2700. U64(0xB51D13AE, 0xA4A488DD), U64(0x6BABAB63, 0x98BDBE41), /* ~= 10^73 */
  2701. U64(0xE264589A, 0x4DCDAB14), U64(0xC696963C, 0x7EED2DD1), /* ~= 10^74 */
  2702. U64(0x8D7EB760, 0x70A08AEC), U64(0xFC1E1DE5, 0xCF543CA2), /* ~= 10^75 */
  2703. U64(0xB0DE6538, 0x8CC8ADA8), U64(0x3B25A55F, 0x43294BCB), /* ~= 10^76 */
  2704. U64(0xDD15FE86, 0xAFFAD912), U64(0x49EF0EB7, 0x13F39EBE), /* ~= 10^77 */
  2705. U64(0x8A2DBF14, 0x2DFCC7AB), U64(0x6E356932, 0x6C784337), /* ~= 10^78 */
  2706. U64(0xACB92ED9, 0x397BF996), U64(0x49C2C37F, 0x07965404), /* ~= 10^79 */
  2707. U64(0xD7E77A8F, 0x87DAF7FB), U64(0xDC33745E, 0xC97BE906), /* ~= 10^80 */
  2708. U64(0x86F0AC99, 0xB4E8DAFD), U64(0x69A028BB, 0x3DED71A3), /* ~= 10^81 */
  2709. U64(0xA8ACD7C0, 0x222311BC), U64(0xC40832EA, 0x0D68CE0C), /* ~= 10^82 */
  2710. U64(0xD2D80DB0, 0x2AABD62B), U64(0xF50A3FA4, 0x90C30190), /* ~= 10^83 */
  2711. U64(0x83C7088E, 0x1AAB65DB), U64(0x792667C6, 0xDA79E0FA), /* ~= 10^84 */
  2712. U64(0xA4B8CAB1, 0xA1563F52), U64(0x577001B8, 0x91185938), /* ~= 10^85 */
  2713. U64(0xCDE6FD5E, 0x09ABCF26), U64(0xED4C0226, 0xB55E6F86), /* ~= 10^86 */
  2714. U64(0x80B05E5A, 0xC60B6178), U64(0x544F8158, 0x315B05B4), /* ~= 10^87 */
  2715. U64(0xA0DC75F1, 0x778E39D6), U64(0x696361AE, 0x3DB1C721), /* ~= 10^88 */
  2716. U64(0xC913936D, 0xD571C84C), U64(0x03BC3A19, 0xCD1E38E9), /* ~= 10^89 */
  2717. U64(0xFB587849, 0x4ACE3A5F), U64(0x04AB48A0, 0x4065C723), /* ~= 10^90 */
  2718. U64(0x9D174B2D, 0xCEC0E47B), U64(0x62EB0D64, 0x283F9C76), /* ~= 10^91 */
  2719. U64(0xC45D1DF9, 0x42711D9A), U64(0x3BA5D0BD, 0x324F8394), /* ~= 10^92 */
  2720. U64(0xF5746577, 0x930D6500), U64(0xCA8F44EC, 0x7EE36479), /* ~= 10^93 */
  2721. U64(0x9968BF6A, 0xBBE85F20), U64(0x7E998B13, 0xCF4E1ECB), /* ~= 10^94 */
  2722. U64(0xBFC2EF45, 0x6AE276E8), U64(0x9E3FEDD8, 0xC321A67E), /* ~= 10^95 */
  2723. U64(0xEFB3AB16, 0xC59B14A2), U64(0xC5CFE94E, 0xF3EA101E), /* ~= 10^96 */
  2724. U64(0x95D04AEE, 0x3B80ECE5), U64(0xBBA1F1D1, 0x58724A12), /* ~= 10^97 */
  2725. U64(0xBB445DA9, 0xCA61281F), U64(0x2A8A6E45, 0xAE8EDC97), /* ~= 10^98 */
  2726. U64(0xEA157514, 0x3CF97226), U64(0xF52D09D7, 0x1A3293BD), /* ~= 10^99 */
  2727. U64(0x924D692C, 0xA61BE758), U64(0x593C2626, 0x705F9C56), /* ~= 10^100 */
  2728. U64(0xB6E0C377, 0xCFA2E12E), U64(0x6F8B2FB0, 0x0C77836C), /* ~= 10^101 */
  2729. U64(0xE498F455, 0xC38B997A), U64(0x0B6DFB9C, 0x0F956447), /* ~= 10^102 */
  2730. U64(0x8EDF98B5, 0x9A373FEC), U64(0x4724BD41, 0x89BD5EAC), /* ~= 10^103 */
  2731. U64(0xB2977EE3, 0x00C50FE7), U64(0x58EDEC91, 0xEC2CB657), /* ~= 10^104 */
  2732. U64(0xDF3D5E9B, 0xC0F653E1), U64(0x2F2967B6, 0x6737E3ED), /* ~= 10^105 */
  2733. U64(0x8B865B21, 0x5899F46C), U64(0xBD79E0D2, 0x0082EE74), /* ~= 10^106 */
  2734. U64(0xAE67F1E9, 0xAEC07187), U64(0xECD85906, 0x80A3AA11), /* ~= 10^107 */
  2735. U64(0xDA01EE64, 0x1A708DE9), U64(0xE80E6F48, 0x20CC9495), /* ~= 10^108 */
  2736. U64(0x884134FE, 0x908658B2), U64(0x3109058D, 0x147FDCDD), /* ~= 10^109 */
  2737. U64(0xAA51823E, 0x34A7EEDE), U64(0xBD4B46F0, 0x599FD415), /* ~= 10^110 */
  2738. U64(0xD4E5E2CD, 0xC1D1EA96), U64(0x6C9E18AC, 0x7007C91A), /* ~= 10^111 */
  2739. U64(0x850FADC0, 0x9923329E), U64(0x03E2CF6B, 0xC604DDB0), /* ~= 10^112 */
  2740. U64(0xA6539930, 0xBF6BFF45), U64(0x84DB8346, 0xB786151C), /* ~= 10^113 */
  2741. U64(0xCFE87F7C, 0xEF46FF16), U64(0xE6126418, 0x65679A63), /* ~= 10^114 */
  2742. U64(0x81F14FAE, 0x158C5F6E), U64(0x4FCB7E8F, 0x3F60C07E), /* ~= 10^115 */
  2743. U64(0xA26DA399, 0x9AEF7749), U64(0xE3BE5E33, 0x0F38F09D), /* ~= 10^116 */
  2744. U64(0xCB090C80, 0x01AB551C), U64(0x5CADF5BF, 0xD3072CC5), /* ~= 10^117 */
  2745. U64(0xFDCB4FA0, 0x02162A63), U64(0x73D9732F, 0xC7C8F7F6), /* ~= 10^118 */
  2746. U64(0x9E9F11C4, 0x014DDA7E), U64(0x2867E7FD, 0xDCDD9AFA), /* ~= 10^119 */
  2747. U64(0xC646D635, 0x01A1511D), U64(0xB281E1FD, 0x541501B8), /* ~= 10^120 */
  2748. U64(0xF7D88BC2, 0x4209A565), U64(0x1F225A7C, 0xA91A4226), /* ~= 10^121 */
  2749. U64(0x9AE75759, 0x6946075F), U64(0x3375788D, 0xE9B06958), /* ~= 10^122 */
  2750. U64(0xC1A12D2F, 0xC3978937), U64(0x0052D6B1, 0x641C83AE), /* ~= 10^123 */
  2751. U64(0xF209787B, 0xB47D6B84), U64(0xC0678C5D, 0xBD23A49A), /* ~= 10^124 */
  2752. U64(0x9745EB4D, 0x50CE6332), U64(0xF840B7BA, 0x963646E0), /* ~= 10^125 */
  2753. U64(0xBD176620, 0xA501FBFF), U64(0xB650E5A9, 0x3BC3D898), /* ~= 10^126 */
  2754. U64(0xEC5D3FA8, 0xCE427AFF), U64(0xA3E51F13, 0x8AB4CEBE), /* ~= 10^127 */
  2755. U64(0x93BA47C9, 0x80E98CDF), U64(0xC66F336C, 0x36B10137), /* ~= 10^128 */
  2756. U64(0xB8A8D9BB, 0xE123F017), U64(0xB80B0047, 0x445D4184), /* ~= 10^129 */
  2757. U64(0xE6D3102A, 0xD96CEC1D), U64(0xA60DC059, 0x157491E5), /* ~= 10^130 */
  2758. U64(0x9043EA1A, 0xC7E41392), U64(0x87C89837, 0xAD68DB2F), /* ~= 10^131 */
  2759. U64(0xB454E4A1, 0x79DD1877), U64(0x29BABE45, 0x98C311FB), /* ~= 10^132 */
  2760. U64(0xE16A1DC9, 0xD8545E94), U64(0xF4296DD6, 0xFEF3D67A), /* ~= 10^133 */
  2761. U64(0x8CE2529E, 0x2734BB1D), U64(0x1899E4A6, 0x5F58660C), /* ~= 10^134 */
  2762. U64(0xB01AE745, 0xB101E9E4), U64(0x5EC05DCF, 0xF72E7F8F), /* ~= 10^135 */
  2763. U64(0xDC21A117, 0x1D42645D), U64(0x76707543, 0xF4FA1F73), /* ~= 10^136 */
  2764. U64(0x899504AE, 0x72497EBA), U64(0x6A06494A, 0x791C53A8), /* ~= 10^137 */
  2765. U64(0xABFA45DA, 0x0EDBDE69), U64(0x0487DB9D, 0x17636892), /* ~= 10^138 */
  2766. U64(0xD6F8D750, 0x9292D603), U64(0x45A9D284, 0x5D3C42B6), /* ~= 10^139 */
  2767. U64(0x865B8692, 0x5B9BC5C2), U64(0x0B8A2392, 0xBA45A9B2), /* ~= 10^140 */
  2768. U64(0xA7F26836, 0xF282B732), U64(0x8E6CAC77, 0x68D7141E), /* ~= 10^141 */
  2769. U64(0xD1EF0244, 0xAF2364FF), U64(0x3207D795, 0x430CD926), /* ~= 10^142 */
  2770. U64(0x8335616A, 0xED761F1F), U64(0x7F44E6BD, 0x49E807B8), /* ~= 10^143 */
  2771. U64(0xA402B9C5, 0xA8D3A6E7), U64(0x5F16206C, 0x9C6209A6), /* ~= 10^144 */
  2772. U64(0xCD036837, 0x130890A1), U64(0x36DBA887, 0xC37A8C0F), /* ~= 10^145 */
  2773. U64(0x80222122, 0x6BE55A64), U64(0xC2494954, 0xDA2C9789), /* ~= 10^146 */
  2774. U64(0xA02AA96B, 0x06DEB0FD), U64(0xF2DB9BAA, 0x10B7BD6C), /* ~= 10^147 */
  2775. U64(0xC83553C5, 0xC8965D3D), U64(0x6F928294, 0x94E5ACC7), /* ~= 10^148 */
  2776. U64(0xFA42A8B7, 0x3ABBF48C), U64(0xCB772339, 0xBA1F17F9), /* ~= 10^149 */
  2777. U64(0x9C69A972, 0x84B578D7), U64(0xFF2A7604, 0x14536EFB), /* ~= 10^150 */
  2778. U64(0xC38413CF, 0x25E2D70D), U64(0xFEF51385, 0x19684ABA), /* ~= 10^151 */
  2779. U64(0xF46518C2, 0xEF5B8CD1), U64(0x7EB25866, 0x5FC25D69), /* ~= 10^152 */
  2780. U64(0x98BF2F79, 0xD5993802), U64(0xEF2F773F, 0xFBD97A61), /* ~= 10^153 */
  2781. U64(0xBEEEFB58, 0x4AFF8603), U64(0xAAFB550F, 0xFACFD8FA), /* ~= 10^154 */
  2782. U64(0xEEAABA2E, 0x5DBF6784), U64(0x95BA2A53, 0xF983CF38), /* ~= 10^155 */
  2783. U64(0x952AB45C, 0xFA97A0B2), U64(0xDD945A74, 0x7BF26183), /* ~= 10^156 */
  2784. U64(0xBA756174, 0x393D88DF), U64(0x94F97111, 0x9AEEF9E4), /* ~= 10^157 */
  2785. U64(0xE912B9D1, 0x478CEB17), U64(0x7A37CD56, 0x01AAB85D), /* ~= 10^158 */
  2786. U64(0x91ABB422, 0xCCB812EE), U64(0xAC62E055, 0xC10AB33A), /* ~= 10^159 */
  2787. U64(0xB616A12B, 0x7FE617AA), U64(0x577B986B, 0x314D6009), /* ~= 10^160 */
  2788. U64(0xE39C4976, 0x5FDF9D94), U64(0xED5A7E85, 0xFDA0B80B), /* ~= 10^161 */
  2789. U64(0x8E41ADE9, 0xFBEBC27D), U64(0x14588F13, 0xBE847307), /* ~= 10^162 */
  2790. U64(0xB1D21964, 0x7AE6B31C), U64(0x596EB2D8, 0xAE258FC8), /* ~= 10^163 */
  2791. U64(0xDE469FBD, 0x99A05FE3), U64(0x6FCA5F8E, 0xD9AEF3BB), /* ~= 10^164 */
  2792. U64(0x8AEC23D6, 0x80043BEE), U64(0x25DE7BB9, 0x480D5854), /* ~= 10^165 */
  2793. U64(0xADA72CCC, 0x20054AE9), U64(0xAF561AA7, 0x9A10AE6A), /* ~= 10^166 */
  2794. U64(0xD910F7FF, 0x28069DA4), U64(0x1B2BA151, 0x8094DA04), /* ~= 10^167 */
  2795. U64(0x87AA9AFF, 0x79042286), U64(0x90FB44D2, 0xF05D0842), /* ~= 10^168 */
  2796. U64(0xA99541BF, 0x57452B28), U64(0x353A1607, 0xAC744A53), /* ~= 10^169 */
  2797. U64(0xD3FA922F, 0x2D1675F2), U64(0x42889B89, 0x97915CE8), /* ~= 10^170 */
  2798. U64(0x847C9B5D, 0x7C2E09B7), U64(0x69956135, 0xFEBADA11), /* ~= 10^171 */
  2799. U64(0xA59BC234, 0xDB398C25), U64(0x43FAB983, 0x7E699095), /* ~= 10^172 */
  2800. U64(0xCF02B2C2, 0x1207EF2E), U64(0x94F967E4, 0x5E03F4BB), /* ~= 10^173 */
  2801. U64(0x8161AFB9, 0x4B44F57D), U64(0x1D1BE0EE, 0xBAC278F5), /* ~= 10^174 */
  2802. U64(0xA1BA1BA7, 0x9E1632DC), U64(0x6462D92A, 0x69731732), /* ~= 10^175 */
  2803. U64(0xCA28A291, 0x859BBF93), U64(0x7D7B8F75, 0x03CFDCFE), /* ~= 10^176 */
  2804. U64(0xFCB2CB35, 0xE702AF78), U64(0x5CDA7352, 0x44C3D43E), /* ~= 10^177 */
  2805. U64(0x9DEFBF01, 0xB061ADAB), U64(0x3A088813, 0x6AFA64A7), /* ~= 10^178 */
  2806. U64(0xC56BAEC2, 0x1C7A1916), U64(0x088AAA18, 0x45B8FDD0), /* ~= 10^179 */
  2807. U64(0xF6C69A72, 0xA3989F5B), U64(0x8AAD549E, 0x57273D45), /* ~= 10^180 */
  2808. U64(0x9A3C2087, 0xA63F6399), U64(0x36AC54E2, 0xF678864B), /* ~= 10^181 */
  2809. U64(0xC0CB28A9, 0x8FCF3C7F), U64(0x84576A1B, 0xB416A7DD), /* ~= 10^182 */
  2810. U64(0xF0FDF2D3, 0xF3C30B9F), U64(0x656D44A2, 0xA11C51D5), /* ~= 10^183 */
  2811. U64(0x969EB7C4, 0x7859E743), U64(0x9F644AE5, 0xA4B1B325), /* ~= 10^184 */
  2812. U64(0xBC4665B5, 0x96706114), U64(0x873D5D9F, 0x0DDE1FEE), /* ~= 10^185 */
  2813. U64(0xEB57FF22, 0xFC0C7959), U64(0xA90CB506, 0xD155A7EA), /* ~= 10^186 */
  2814. U64(0x9316FF75, 0xDD87CBD8), U64(0x09A7F124, 0x42D588F2), /* ~= 10^187 */
  2815. U64(0xB7DCBF53, 0x54E9BECE), U64(0x0C11ED6D, 0x538AEB2F), /* ~= 10^188 */
  2816. U64(0xE5D3EF28, 0x2A242E81), U64(0x8F1668C8, 0xA86DA5FA), /* ~= 10^189 */
  2817. U64(0x8FA47579, 0x1A569D10), U64(0xF96E017D, 0x694487BC), /* ~= 10^190 */
  2818. U64(0xB38D92D7, 0x60EC4455), U64(0x37C981DC, 0xC395A9AC), /* ~= 10^191 */
  2819. U64(0xE070F78D, 0x3927556A), U64(0x85BBE253, 0xF47B1417), /* ~= 10^192 */
  2820. U64(0x8C469AB8, 0x43B89562), U64(0x93956D74, 0x78CCEC8E), /* ~= 10^193 */
  2821. U64(0xAF584166, 0x54A6BABB), U64(0x387AC8D1, 0x970027B2), /* ~= 10^194 */
  2822. U64(0xDB2E51BF, 0xE9D0696A), U64(0x06997B05, 0xFCC0319E), /* ~= 10^195 */
  2823. U64(0x88FCF317, 0xF22241E2), U64(0x441FECE3, 0xBDF81F03), /* ~= 10^196 */
  2824. U64(0xAB3C2FDD, 0xEEAAD25A), U64(0xD527E81C, 0xAD7626C3), /* ~= 10^197 */
  2825. U64(0xD60B3BD5, 0x6A5586F1), U64(0x8A71E223, 0xD8D3B074), /* ~= 10^198 */
  2826. U64(0x85C70565, 0x62757456), U64(0xF6872D56, 0x67844E49), /* ~= 10^199 */
  2827. U64(0xA738C6BE, 0xBB12D16C), U64(0xB428F8AC, 0x016561DB), /* ~= 10^200 */
  2828. U64(0xD106F86E, 0x69D785C7), U64(0xE13336D7, 0x01BEBA52), /* ~= 10^201 */
  2829. U64(0x82A45B45, 0x0226B39C), U64(0xECC00246, 0x61173473), /* ~= 10^202 */
  2830. U64(0xA34D7216, 0x42B06084), U64(0x27F002D7, 0xF95D0190), /* ~= 10^203 */
  2831. U64(0xCC20CE9B, 0xD35C78A5), U64(0x31EC038D, 0xF7B441F4), /* ~= 10^204 */
  2832. U64(0xFF290242, 0xC83396CE), U64(0x7E670471, 0x75A15271), /* ~= 10^205 */
  2833. U64(0x9F79A169, 0xBD203E41), U64(0x0F0062C6, 0xE984D386), /* ~= 10^206 */
  2834. U64(0xC75809C4, 0x2C684DD1), U64(0x52C07B78, 0xA3E60868), /* ~= 10^207 */
  2835. U64(0xF92E0C35, 0x37826145), U64(0xA7709A56, 0xCCDF8A82), /* ~= 10^208 */
  2836. U64(0x9BBCC7A1, 0x42B17CCB), U64(0x88A66076, 0x400BB691), /* ~= 10^209 */
  2837. U64(0xC2ABF989, 0x935DDBFE), U64(0x6ACFF893, 0xD00EA435), /* ~= 10^210 */
  2838. U64(0xF356F7EB, 0xF83552FE), U64(0x0583F6B8, 0xC4124D43), /* ~= 10^211 */
  2839. U64(0x98165AF3, 0x7B2153DE), U64(0xC3727A33, 0x7A8B704A), /* ~= 10^212 */
  2840. U64(0xBE1BF1B0, 0x59E9A8D6), U64(0x744F18C0, 0x592E4C5C), /* ~= 10^213 */
  2841. U64(0xEDA2EE1C, 0x7064130C), U64(0x1162DEF0, 0x6F79DF73), /* ~= 10^214 */
  2842. U64(0x9485D4D1, 0xC63E8BE7), U64(0x8ADDCB56, 0x45AC2BA8), /* ~= 10^215 */
  2843. U64(0xB9A74A06, 0x37CE2EE1), U64(0x6D953E2B, 0xD7173692), /* ~= 10^216 */
  2844. U64(0xE8111C87, 0xC5C1BA99), U64(0xC8FA8DB6, 0xCCDD0437), /* ~= 10^217 */
  2845. U64(0x910AB1D4, 0xDB9914A0), U64(0x1D9C9892, 0x400A22A2), /* ~= 10^218 */
  2846. U64(0xB54D5E4A, 0x127F59C8), U64(0x2503BEB6, 0xD00CAB4B), /* ~= 10^219 */
  2847. U64(0xE2A0B5DC, 0x971F303A), U64(0x2E44AE64, 0x840FD61D), /* ~= 10^220 */
  2848. U64(0x8DA471A9, 0xDE737E24), U64(0x5CEAECFE, 0xD289E5D2), /* ~= 10^221 */
  2849. U64(0xB10D8E14, 0x56105DAD), U64(0x7425A83E, 0x872C5F47), /* ~= 10^222 */
  2850. U64(0xDD50F199, 0x6B947518), U64(0xD12F124E, 0x28F77719), /* ~= 10^223 */
  2851. U64(0x8A5296FF, 0xE33CC92F), U64(0x82BD6B70, 0xD99AAA6F), /* ~= 10^224 */
  2852. U64(0xACE73CBF, 0xDC0BFB7B), U64(0x636CC64D, 0x1001550B), /* ~= 10^225 */
  2853. U64(0xD8210BEF, 0xD30EFA5A), U64(0x3C47F7E0, 0x5401AA4E), /* ~= 10^226 */
  2854. U64(0x8714A775, 0xE3E95C78), U64(0x65ACFAEC, 0x34810A71), /* ~= 10^227 */
  2855. U64(0xA8D9D153, 0x5CE3B396), U64(0x7F1839A7, 0x41A14D0D), /* ~= 10^228 */
  2856. U64(0xD31045A8, 0x341CA07C), U64(0x1EDE4811, 0x1209A050), /* ~= 10^229 */
  2857. U64(0x83EA2B89, 0x2091E44D), U64(0x934AED0A, 0xAB460432), /* ~= 10^230 */
  2858. U64(0xA4E4B66B, 0x68B65D60), U64(0xF81DA84D, 0x5617853F), /* ~= 10^231 */
  2859. U64(0xCE1DE406, 0x42E3F4B9), U64(0x36251260, 0xAB9D668E), /* ~= 10^232 */
  2860. U64(0x80D2AE83, 0xE9CE78F3), U64(0xC1D72B7C, 0x6B426019), /* ~= 10^233 */
  2861. U64(0xA1075A24, 0xE4421730), U64(0xB24CF65B, 0x8612F81F), /* ~= 10^234 */
  2862. U64(0xC94930AE, 0x1D529CFC), U64(0xDEE033F2, 0x6797B627), /* ~= 10^235 */
  2863. U64(0xFB9B7CD9, 0xA4A7443C), U64(0x169840EF, 0x017DA3B1), /* ~= 10^236 */
  2864. U64(0x9D412E08, 0x06E88AA5), U64(0x8E1F2895, 0x60EE864E), /* ~= 10^237 */
  2865. U64(0xC491798A, 0x08A2AD4E), U64(0xF1A6F2BA, 0xB92A27E2), /* ~= 10^238 */
  2866. U64(0xF5B5D7EC, 0x8ACB58A2), U64(0xAE10AF69, 0x6774B1DB), /* ~= 10^239 */
  2867. U64(0x9991A6F3, 0xD6BF1765), U64(0xACCA6DA1, 0xE0A8EF29), /* ~= 10^240 */
  2868. U64(0xBFF610B0, 0xCC6EDD3F), U64(0x17FD090A, 0x58D32AF3), /* ~= 10^241 */
  2869. U64(0xEFF394DC, 0xFF8A948E), U64(0xDDFC4B4C, 0xEF07F5B0), /* ~= 10^242 */
  2870. U64(0x95F83D0A, 0x1FB69CD9), U64(0x4ABDAF10, 0x1564F98E), /* ~= 10^243 */
  2871. U64(0xBB764C4C, 0xA7A4440F), U64(0x9D6D1AD4, 0x1ABE37F1), /* ~= 10^244 */
  2872. U64(0xEA53DF5F, 0xD18D5513), U64(0x84C86189, 0x216DC5ED), /* ~= 10^245 */
  2873. U64(0x92746B9B, 0xE2F8552C), U64(0x32FD3CF5, 0xB4E49BB4), /* ~= 10^246 */
  2874. U64(0xB7118682, 0xDBB66A77), U64(0x3FBC8C33, 0x221DC2A1), /* ~= 10^247 */
  2875. U64(0xE4D5E823, 0x92A40515), U64(0x0FABAF3F, 0xEAA5334A), /* ~= 10^248 */
  2876. U64(0x8F05B116, 0x3BA6832D), U64(0x29CB4D87, 0xF2A7400E), /* ~= 10^249 */
  2877. U64(0xB2C71D5B, 0xCA9023F8), U64(0x743E20E9, 0xEF511012), /* ~= 10^250 */
  2878. U64(0xDF78E4B2, 0xBD342CF6), U64(0x914DA924, 0x6B255416), /* ~= 10^251 */
  2879. U64(0x8BAB8EEF, 0xB6409C1A), U64(0x1AD089B6, 0xC2F7548E), /* ~= 10^252 */
  2880. U64(0xAE9672AB, 0xA3D0C320), U64(0xA184AC24, 0x73B529B1), /* ~= 10^253 */
  2881. U64(0xDA3C0F56, 0x8CC4F3E8), U64(0xC9E5D72D, 0x90A2741E), /* ~= 10^254 */
  2882. U64(0x88658996, 0x17FB1871), U64(0x7E2FA67C, 0x7A658892), /* ~= 10^255 */
  2883. U64(0xAA7EEBFB, 0x9DF9DE8D), U64(0xDDBB901B, 0x98FEEAB7), /* ~= 10^256 */
  2884. U64(0xD51EA6FA, 0x85785631), U64(0x552A7422, 0x7F3EA565), /* ~= 10^257 */
  2885. U64(0x8533285C, 0x936B35DE), U64(0xD53A8895, 0x8F87275F), /* ~= 10^258 */
  2886. U64(0xA67FF273, 0xB8460356), U64(0x8A892ABA, 0xF368F137), /* ~= 10^259 */
  2887. U64(0xD01FEF10, 0xA657842C), U64(0x2D2B7569, 0xB0432D85), /* ~= 10^260 */
  2888. U64(0x8213F56A, 0x67F6B29B), U64(0x9C3B2962, 0x0E29FC73), /* ~= 10^261 */
  2889. U64(0xA298F2C5, 0x01F45F42), U64(0x8349F3BA, 0x91B47B8F), /* ~= 10^262 */
  2890. U64(0xCB3F2F76, 0x42717713), U64(0x241C70A9, 0x36219A73), /* ~= 10^263 */
  2891. U64(0xFE0EFB53, 0xD30DD4D7), U64(0xED238CD3, 0x83AA0110), /* ~= 10^264 */
  2892. U64(0x9EC95D14, 0x63E8A506), U64(0xF4363804, 0x324A40AA), /* ~= 10^265 */
  2893. U64(0xC67BB459, 0x7CE2CE48), U64(0xB143C605, 0x3EDCD0D5), /* ~= 10^266 */
  2894. U64(0xF81AA16F, 0xDC1B81DA), U64(0xDD94B786, 0x8E94050A), /* ~= 10^267 */
  2895. U64(0x9B10A4E5, 0xE9913128), U64(0xCA7CF2B4, 0x191C8326), /* ~= 10^268 */
  2896. U64(0xC1D4CE1F, 0x63F57D72), U64(0xFD1C2F61, 0x1F63A3F0), /* ~= 10^269 */
  2897. U64(0xF24A01A7, 0x3CF2DCCF), U64(0xBC633B39, 0x673C8CEC), /* ~= 10^270 */
  2898. U64(0x976E4108, 0x8617CA01), U64(0xD5BE0503, 0xE085D813), /* ~= 10^271 */
  2899. U64(0xBD49D14A, 0xA79DBC82), U64(0x4B2D8644, 0xD8A74E18), /* ~= 10^272 */
  2900. U64(0xEC9C459D, 0x51852BA2), U64(0xDDF8E7D6, 0x0ED1219E), /* ~= 10^273 */
  2901. U64(0x93E1AB82, 0x52F33B45), U64(0xCABB90E5, 0xC942B503), /* ~= 10^274 */
  2902. U64(0xB8DA1662, 0xE7B00A17), U64(0x3D6A751F, 0x3B936243), /* ~= 10^275 */
  2903. U64(0xE7109BFB, 0xA19C0C9D), U64(0x0CC51267, 0x0A783AD4), /* ~= 10^276 */
  2904. U64(0x906A617D, 0x450187E2), U64(0x27FB2B80, 0x668B24C5), /* ~= 10^277 */
  2905. U64(0xB484F9DC, 0x9641E9DA), U64(0xB1F9F660, 0x802DEDF6), /* ~= 10^278 */
  2906. U64(0xE1A63853, 0xBBD26451), U64(0x5E7873F8, 0xA0396973), /* ~= 10^279 */
  2907. U64(0x8D07E334, 0x55637EB2), U64(0xDB0B487B, 0x6423E1E8), /* ~= 10^280 */
  2908. U64(0xB049DC01, 0x6ABC5E5F), U64(0x91CE1A9A, 0x3D2CDA62), /* ~= 10^281 */
  2909. U64(0xDC5C5301, 0xC56B75F7), U64(0x7641A140, 0xCC7810FB), /* ~= 10^282 */
  2910. U64(0x89B9B3E1, 0x1B6329BA), U64(0xA9E904C8, 0x7FCB0A9D), /* ~= 10^283 */
  2911. U64(0xAC2820D9, 0x623BF429), U64(0x546345FA, 0x9FBDCD44), /* ~= 10^284 */
  2912. U64(0xD732290F, 0xBACAF133), U64(0xA97C1779, 0x47AD4095), /* ~= 10^285 */
  2913. U64(0x867F59A9, 0xD4BED6C0), U64(0x49ED8EAB, 0xCCCC485D), /* ~= 10^286 */
  2914. U64(0xA81F3014, 0x49EE8C70), U64(0x5C68F256, 0xBFFF5A74), /* ~= 10^287 */
  2915. U64(0xD226FC19, 0x5C6A2F8C), U64(0x73832EEC, 0x6FFF3111), /* ~= 10^288 */
  2916. U64(0x83585D8F, 0xD9C25DB7), U64(0xC831FD53, 0xC5FF7EAB), /* ~= 10^289 */
  2917. U64(0xA42E74F3, 0xD032F525), U64(0xBA3E7CA8, 0xB77F5E55), /* ~= 10^290 */
  2918. U64(0xCD3A1230, 0xC43FB26F), U64(0x28CE1BD2, 0xE55F35EB), /* ~= 10^291 */
  2919. U64(0x80444B5E, 0x7AA7CF85), U64(0x7980D163, 0xCF5B81B3), /* ~= 10^292 */
  2920. U64(0xA0555E36, 0x1951C366), U64(0xD7E105BC, 0xC332621F), /* ~= 10^293 */
  2921. U64(0xC86AB5C3, 0x9FA63440), U64(0x8DD9472B, 0xF3FEFAA7), /* ~= 10^294 */
  2922. U64(0xFA856334, 0x878FC150), U64(0xB14F98F6, 0xF0FEB951), /* ~= 10^295 */
  2923. U64(0x9C935E00, 0xD4B9D8D2), U64(0x6ED1BF9A, 0x569F33D3), /* ~= 10^296 */
  2924. U64(0xC3B83581, 0x09E84F07), U64(0x0A862F80, 0xEC4700C8), /* ~= 10^297 */
  2925. U64(0xF4A642E1, 0x4C6262C8), U64(0xCD27BB61, 0x2758C0FA), /* ~= 10^298 */
  2926. U64(0x98E7E9CC, 0xCFBD7DBD), U64(0x8038D51C, 0xB897789C), /* ~= 10^299 */
  2927. U64(0xBF21E440, 0x03ACDD2C), U64(0xE0470A63, 0xE6BD56C3), /* ~= 10^300 */
  2928. U64(0xEEEA5D50, 0x04981478), U64(0x1858CCFC, 0xE06CAC74), /* ~= 10^301 */
  2929. U64(0x95527A52, 0x02DF0CCB), U64(0x0F37801E, 0x0C43EBC8), /* ~= 10^302 */
  2930. U64(0xBAA718E6, 0x8396CFFD), U64(0xD3056025, 0x8F54E6BA), /* ~= 10^303 */
  2931. U64(0xE950DF20, 0x247C83FD), U64(0x47C6B82E, 0xF32A2069), /* ~= 10^304 */
  2932. U64(0x91D28B74, 0x16CDD27E), U64(0x4CDC331D, 0x57FA5441), /* ~= 10^305 */
  2933. U64(0xB6472E51, 0x1C81471D), U64(0xE0133FE4, 0xADF8E952), /* ~= 10^306 */
  2934. U64(0xE3D8F9E5, 0x63A198E5), U64(0x58180FDD, 0xD97723A6), /* ~= 10^307 */
  2935. U64(0x8E679C2F, 0x5E44FF8F), U64(0x570F09EA, 0xA7EA7648), /* ~= 10^308 */
  2936. U64(0xB201833B, 0x35D63F73), U64(0x2CD2CC65, 0x51E513DA), /* ~= 10^309 */
  2937. U64(0xDE81E40A, 0x034BCF4F), U64(0xF8077F7E, 0xA65E58D1), /* ~= 10^310 */
  2938. U64(0x8B112E86, 0x420F6191), U64(0xFB04AFAF, 0x27FAF782), /* ~= 10^311 */
  2939. U64(0xADD57A27, 0xD29339F6), U64(0x79C5DB9A, 0xF1F9B563), /* ~= 10^312 */
  2940. U64(0xD94AD8B1, 0xC7380874), U64(0x18375281, 0xAE7822BC), /* ~= 10^313 */
  2941. U64(0x87CEC76F, 0x1C830548), U64(0x8F229391, 0x0D0B15B5), /* ~= 10^314 */
  2942. U64(0xA9C2794A, 0xE3A3C69A), U64(0xB2EB3875, 0x504DDB22), /* ~= 10^315 */
  2943. U64(0xD433179D, 0x9C8CB841), U64(0x5FA60692, 0xA46151EB), /* ~= 10^316 */
  2944. U64(0x849FEEC2, 0x81D7F328), U64(0xDBC7C41B, 0xA6BCD333), /* ~= 10^317 */
  2945. U64(0xA5C7EA73, 0x224DEFF3), U64(0x12B9B522, 0x906C0800), /* ~= 10^318 */
  2946. U64(0xCF39E50F, 0xEAE16BEF), U64(0xD768226B, 0x34870A00), /* ~= 10^319 */
  2947. U64(0x81842F29, 0xF2CCE375), U64(0xE6A11583, 0x00D46640), /* ~= 10^320 */
  2948. U64(0xA1E53AF4, 0x6F801C53), U64(0x60495AE3, 0xC1097FD0), /* ~= 10^321 */
  2949. U64(0xCA5E89B1, 0x8B602368), U64(0x385BB19C, 0xB14BDFC4), /* ~= 10^322 */
  2950. U64(0xFCF62C1D, 0xEE382C42), U64(0x46729E03, 0xDD9ED7B5), /* ~= 10^323 */
  2951. U64(0x9E19DB92, 0xB4E31BA9), U64(0x6C07A2C2, 0x6A8346D1) /* ~= 10^324 */
  2952. };
  2953. /**
  2954. Get the cached pow10 value from pow10_sig_table.
  2955. @param exp10 The exponent of pow(10, e). This value must in range
  2956. POW10_SIG_TABLE_MIN_EXP to POW10_SIG_TABLE_MAX_EXP.
  2957. @param hi The highest 64 bits of pow(10, e).
  2958. @param lo The lower 64 bits after `hi`.
  2959. */
  2960. static_inline void pow10_table_get_sig(i32 exp10, u64 *hi, u64 *lo) {
  2961. i32 idx = exp10 - (POW10_SIG_TABLE_MIN_EXP);
  2962. *hi = pow10_sig_table[idx * 2];
  2963. *lo = pow10_sig_table[idx * 2 + 1];
  2964. }
  2965. /**
  2966. Get the exponent (base 2) for highest 64 bits significand in pow10_sig_table.
  2967. */
  2968. static_inline void pow10_table_get_exp(i32 exp10, i32 *exp2) {
  2969. /* e2 = floor(log2(pow(10, e))) - 64 + 1 */
  2970. /* = floor(e * log2(10) - 63) */
  2971. *exp2 = (exp10 * 217706 - 4128768) >> 16;
  2972. }
  2973. #endif
  2974. #if !YYJSON_DISABLE_READER
  2975. /*==============================================================================
  2976. * JSON Character Matcher
  2977. *============================================================================*/
  2978. /** Character type */
  2979. typedef u8 char_type;
  2980. /** Whitespace character: ' ', '\\t', '\\n', '\\r'. */
  2981. static const char_type CHAR_TYPE_SPACE = 1 << 0;
  2982. /** Number character: '-', [0-9]. */
  2983. static const char_type CHAR_TYPE_NUMBER = 1 << 1;
  2984. /** JSON Escaped character: '"', '\', [0x00-0x1F]. */
  2985. static const char_type CHAR_TYPE_ESC_ASCII = 1 << 2;
  2986. /** Non-ASCII character: [0x80-0xFF]. */
  2987. static const char_type CHAR_TYPE_NON_ASCII = 1 << 3;
  2988. /** JSON container character: '{', '['. */
  2989. static const char_type CHAR_TYPE_CONTAINER = 1 << 4;
  2990. /** Comment character: '/'. */
  2991. static const char_type CHAR_TYPE_COMMENT = 1 << 5;
  2992. /** Line end character: '\\n', '\\r', '\0'. */
  2993. static const char_type CHAR_TYPE_LINE_END = 1 << 6;
  2994. /** Hexadecimal numeric character: [0-9a-fA-F]. */
  2995. static const char_type CHAR_TYPE_HEX = 1 << 7;
  2996. /** Character type table (generate with misc/make_tables.c) */
  2997. static const char_type char_table[256] = {
  2998. 0x44, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  2999. 0x04, 0x05, 0x45, 0x04, 0x04, 0x45, 0x04, 0x04,
  3000. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  3001. 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
  3002. 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00,
  3003. 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x20,
  3004. 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82,
  3005. 0x82, 0x82, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3006. 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00,
  3007. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3008. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3009. 0x00, 0x00, 0x00, 0x10, 0x04, 0x00, 0x00, 0x00,
  3010. 0x00, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00,
  3011. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3012. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3013. 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
  3014. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  3015. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  3016. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  3017. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  3018. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  3019. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  3020. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  3021. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  3022. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  3023. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  3024. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  3025. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  3026. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  3027. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  3028. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08,
  3029. 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08
  3030. };
  3031. /** Match a character with specified type. */
  3032. static_inline bool char_is_type(u8 c, char_type type) {
  3033. return (char_table[c] & type) != 0;
  3034. }
  3035. /** Match a whitespace: ' ', '\\t', '\\n', '\\r'. */
  3036. static_inline bool char_is_space(u8 c) {
  3037. return char_is_type(c, (char_type)CHAR_TYPE_SPACE);
  3038. }
  3039. /** Match a whitespace or comment: ' ', '\\t', '\\n', '\\r', '/'. */
  3040. static_inline bool char_is_space_or_comment(u8 c) {
  3041. return char_is_type(c, (char_type)(CHAR_TYPE_SPACE | CHAR_TYPE_COMMENT));
  3042. }
  3043. /** Match a JSON number: '-', [0-9]. */
  3044. static_inline bool char_is_number(u8 c) {
  3045. return char_is_type(c, (char_type)CHAR_TYPE_NUMBER);
  3046. }
  3047. /** Match a JSON container: '{', '['. */
  3048. static_inline bool char_is_container(u8 c) {
  3049. return char_is_type(c, (char_type)CHAR_TYPE_CONTAINER);
  3050. }
  3051. /** Match a stop character in ASCII string: '"', '\', [0x00-0x1F,0x80-0xFF]. */
  3052. static_inline bool char_is_ascii_stop(u8 c) {
  3053. return char_is_type(c, (char_type)(CHAR_TYPE_ESC_ASCII |
  3054. CHAR_TYPE_NON_ASCII));
  3055. }
  3056. /** Match a line end character: '\\n', '\\r', '\0'. */
  3057. static_inline bool char_is_line_end(u8 c) {
  3058. return char_is_type(c, (char_type)CHAR_TYPE_LINE_END);
  3059. }
  3060. /** Match a hexadecimal numeric character: [0-9a-fA-F]. */
  3061. static_inline bool char_is_hex(u8 c) {
  3062. return char_is_type(c, (char_type)CHAR_TYPE_HEX);
  3063. }
  3064. /*==============================================================================
  3065. * Digit Character Matcher
  3066. *============================================================================*/
  3067. /** Digit type */
  3068. typedef u8 digi_type;
  3069. /** Digit: '0'. */
  3070. static const digi_type DIGI_TYPE_ZERO = 1 << 0;
  3071. /** Digit: [1-9]. */
  3072. static const digi_type DIGI_TYPE_NONZERO = 1 << 1;
  3073. /** Plus sign (positive): '+'. */
  3074. static const digi_type DIGI_TYPE_POS = 1 << 2;
  3075. /** Minus sign (negative): '-'. */
  3076. static const digi_type DIGI_TYPE_NEG = 1 << 3;
  3077. /** Decimal point: '.' */
  3078. static const digi_type DIGI_TYPE_DOT = 1 << 4;
  3079. /** Exponent sign: 'e, 'E'. */
  3080. static const digi_type DIGI_TYPE_EXP = 1 << 5;
  3081. /** Digit type table (generate with misc/make_tables.c) */
  3082. static const digi_type digi_table[256] = {
  3083. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3084. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3085. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3086. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3087. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3088. 0x00, 0x00, 0x00, 0x04, 0x00, 0x08, 0x10, 0x00,
  3089. 0x01, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
  3090. 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3091. 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00,
  3092. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3093. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3094. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3095. 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00,
  3096. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3097. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  3098. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  3099. };
  3100. /** Match a character with specified type. */
  3101. static_inline bool digi_is_type(u8 d, digi_type type) {
  3102. return (digi_table[d] & type) != 0;
  3103. }
  3104. /** Match a sign: '+', '-' */
  3105. static_inline bool digi_is_sign(u8 d) {
  3106. return digi_is_type(d, (digi_type)(DIGI_TYPE_POS | DIGI_TYPE_NEG));
  3107. }
  3108. /** Match a none zero digit: [1-9] */
  3109. static_inline bool digi_is_nonzero(u8 d) {
  3110. return digi_is_type(d, (digi_type)DIGI_TYPE_NONZERO);
  3111. }
  3112. /** Match a digit: [0-9] */
  3113. static_inline bool digi_is_digit(u8 d) {
  3114. return digi_is_type(d, (digi_type)(DIGI_TYPE_ZERO | DIGI_TYPE_NONZERO));
  3115. }
  3116. /** Match an exponent sign: 'e', 'E'. */
  3117. static_inline bool digi_is_exp(u8 d) {
  3118. return digi_is_type(d, (digi_type)DIGI_TYPE_EXP);
  3119. }
  3120. /** Match a floating point indicator: '.', 'e', 'E'. */
  3121. static_inline bool digi_is_fp(u8 d) {
  3122. return digi_is_type(d, (digi_type)(DIGI_TYPE_DOT | DIGI_TYPE_EXP));
  3123. }
  3124. /** Match a digit or floating point indicator: [0-9], '.', 'e', 'E'. */
  3125. static_inline bool digi_is_digit_or_fp(u8 d) {
  3126. return digi_is_type(d, (digi_type)(DIGI_TYPE_ZERO | DIGI_TYPE_NONZERO |
  3127. DIGI_TYPE_DOT | DIGI_TYPE_EXP));
  3128. }
  3129. /*==============================================================================
  3130. * Hex Character Reader
  3131. * This function is used by JSON reader to read escaped characters.
  3132. *============================================================================*/
  3133. /**
  3134. This table is used to convert 4 hex character sequence to a number.
  3135. A valid hex character [0-9A-Fa-f] will mapped to it's raw number [0x00, 0x0F],
  3136. an invalid hex character will mapped to [0xF0].
  3137. (generate with misc/make_tables.c)
  3138. */
  3139. static const u8 hex_conv_table[256] = {
  3140. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3141. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3142. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3143. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3144. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3145. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3146. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  3147. 0x08, 0x09, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3148. 0xF0, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xF0,
  3149. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3150. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3151. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3152. 0xF0, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0xF0,
  3153. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3154. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3155. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3156. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3157. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3158. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3159. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3160. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3161. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3162. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3163. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3164. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3165. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3166. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3167. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3168. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3169. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3170. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  3171. 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0, 0xF0
  3172. };
  3173. /**
  3174. Scans an escaped character sequence as a UTF-16 code unit (branchless).
  3175. e.g. "\\u005C" should pass "005C" as `cur`.
  3176. This requires the string has 4-byte zero padding.
  3177. */
  3178. static_inline bool read_hex_u16(const u8 *cur, u16 *val) {
  3179. u16 c0, c1, c2, c3, t0, t1;
  3180. c0 = hex_conv_table[cur[0]];
  3181. c1 = hex_conv_table[cur[1]];
  3182. c2 = hex_conv_table[cur[2]];
  3183. c3 = hex_conv_table[cur[3]];
  3184. t0 = (u16)((c0 << 8) | c2);
  3185. t1 = (u16)((c1 << 8) | c3);
  3186. *val = (u16)((t0 << 4) | t1);
  3187. return ((t0 | t1) & (u16)0xF0F0) == 0;
  3188. }
  3189. /*==============================================================================
  3190. * JSON Reader Utils
  3191. * These functions are used by JSON reader to read literals and comments.
  3192. *============================================================================*/
  3193. /** Read 'true' literal, '*cur' should be 't'. */
  3194. static_inline bool read_true(u8 **ptr, yyjson_val *val) {
  3195. u8 *cur = *ptr;
  3196. u8 **end = ptr;
  3197. if (likely(byte_match_4(cur, "true"))) {
  3198. val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_TRUE;
  3199. *end = cur + 4;
  3200. return true;
  3201. }
  3202. return false;
  3203. }
  3204. /** Read 'false' literal, '*cur' should be 'f'. */
  3205. static_inline bool read_false(u8 **ptr, yyjson_val *val) {
  3206. u8 *cur = *ptr;
  3207. u8 **end = ptr;
  3208. if (likely(byte_match_4(cur + 1, "alse"))) {
  3209. val->tag = YYJSON_TYPE_BOOL | YYJSON_SUBTYPE_FALSE;
  3210. *end = cur + 5;
  3211. return true;
  3212. }
  3213. return false;
  3214. }
  3215. /** Read 'null' literal, '*cur' should be 'n'. */
  3216. static_inline bool read_null(u8 **ptr, yyjson_val *val) {
  3217. u8 *cur = *ptr;
  3218. u8 **end = ptr;
  3219. if (likely(byte_match_4(cur, "null"))) {
  3220. val->tag = YYJSON_TYPE_NULL;
  3221. *end = cur + 4;
  3222. return true;
  3223. }
  3224. return false;
  3225. }
  3226. /** Read 'Inf' or 'Infinity' literal (ignoring case). */
  3227. static_inline bool read_inf(bool sign, u8 **ptr, u8 **pre, yyjson_val *val) {
  3228. u8 *hdr = *ptr - sign;
  3229. u8 *cur = *ptr;
  3230. u8 **end = ptr;
  3231. if ((cur[0] == 'I' || cur[0] == 'i') &&
  3232. (cur[1] == 'N' || cur[1] == 'n') &&
  3233. (cur[2] == 'F' || cur[2] == 'f')) {
  3234. if ((cur[3] == 'I' || cur[3] == 'i') &&
  3235. (cur[4] == 'N' || cur[4] == 'n') &&
  3236. (cur[5] == 'I' || cur[5] == 'i') &&
  3237. (cur[6] == 'T' || cur[6] == 't') &&
  3238. (cur[7] == 'Y' || cur[7] == 'y')) {
  3239. cur += 8;
  3240. } else {
  3241. cur += 3;
  3242. }
  3243. *end = cur;
  3244. if (pre) {
  3245. /* add null-terminator for previous raw string */
  3246. if (*pre) **pre = '\0';
  3247. *pre = cur;
  3248. val->tag = ((u64)(cur - hdr) << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW;
  3249. val->uni.str = (const char *)hdr;
  3250. } else {
  3251. val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL;
  3252. val->uni.u64 = f64_raw_get_inf(sign);
  3253. }
  3254. return true;
  3255. }
  3256. return false;
  3257. }
  3258. /** Read 'NaN' literal (ignoring case). */
  3259. static_inline bool read_nan(bool sign, u8 **ptr, u8 **pre, yyjson_val *val) {
  3260. u8 *hdr = *ptr - sign;
  3261. u8 *cur = *ptr;
  3262. u8 **end = ptr;
  3263. if ((cur[0] == 'N' || cur[0] == 'n') &&
  3264. (cur[1] == 'A' || cur[1] == 'a') &&
  3265. (cur[2] == 'N' || cur[2] == 'n')) {
  3266. cur += 3;
  3267. *end = cur;
  3268. if (pre) {
  3269. /* add null-terminator for previous raw string */
  3270. if (*pre) **pre = '\0';
  3271. *pre = cur;
  3272. val->tag = ((u64)(cur - hdr) << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW;
  3273. val->uni.str = (const char *)hdr;
  3274. } else {
  3275. val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL;
  3276. val->uni.u64 = f64_raw_get_nan(sign);
  3277. }
  3278. return true;
  3279. }
  3280. return false;
  3281. }
  3282. /** Read 'Inf', 'Infinity' or 'NaN' literal (ignoring case). */
  3283. static_inline bool read_inf_or_nan(bool sign, u8 **ptr, u8 **pre,
  3284. yyjson_val *val) {
  3285. if (read_inf(sign, ptr, pre, val)) return true;
  3286. if (read_nan(sign, ptr, pre, val)) return true;
  3287. return false;
  3288. }
  3289. /** Read a JSON number as raw string. */
  3290. static_noinline bool read_number_raw(u8 **ptr,
  3291. u8 **pre,
  3292. yyjson_read_flag flg,
  3293. yyjson_val *val,
  3294. const char **msg) {
  3295. #define return_err(_pos, _msg) do { \
  3296. *msg = _msg; \
  3297. *end = _pos; \
  3298. return false; \
  3299. } while (false)
  3300. #define return_raw() do { \
  3301. val->tag = ((u64)(cur - hdr) << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW; \
  3302. val->uni.str = (const char *)hdr; \
  3303. *pre = cur; *end = cur; return true; \
  3304. } while (false)
  3305. u8 *hdr = *ptr;
  3306. u8 *cur = *ptr;
  3307. u8 **end = ptr;
  3308. /* add null-terminator for previous raw string */
  3309. if (*pre) **pre = '\0';
  3310. /* skip sign */
  3311. cur += (*cur == '-');
  3312. /* read first digit, check leading zero */
  3313. if (unlikely(!digi_is_digit(*cur))) {
  3314. if (has_read_flag(ALLOW_INF_AND_NAN)) {
  3315. if (read_inf_or_nan(*hdr == '-', &cur, pre, val)) return_raw();
  3316. }
  3317. return_err(cur, "no digit after minus sign");
  3318. }
  3319. /* read integral part */
  3320. if (*cur == '0') {
  3321. cur++;
  3322. if (unlikely(digi_is_digit(*cur))) {
  3323. return_err(cur - 1, "number with leading zero is not allowed");
  3324. }
  3325. if (!digi_is_fp(*cur)) return_raw();
  3326. } else {
  3327. while (digi_is_digit(*cur)) cur++;
  3328. if (!digi_is_fp(*cur)) return_raw();
  3329. }
  3330. /* read fraction part */
  3331. if (*cur == '.') {
  3332. cur++;
  3333. if (!digi_is_digit(*cur++)) {
  3334. return_err(cur, "no digit after decimal point");
  3335. }
  3336. while (digi_is_digit(*cur)) cur++;
  3337. }
  3338. /* read exponent part */
  3339. if (digi_is_exp(*cur)) {
  3340. cur += 1 + digi_is_sign(cur[1]);
  3341. if (!digi_is_digit(*cur++)) {
  3342. return_err(cur, "no digit after exponent sign");
  3343. }
  3344. while (digi_is_digit(*cur)) cur++;
  3345. }
  3346. return_raw();
  3347. #undef return_err
  3348. #undef return_raw
  3349. }
  3350. /**
  3351. Skips spaces and comments as many as possible.
  3352. It will return false in these cases:
  3353. 1. No character is skipped. The 'end' pointer is set as input cursor.
  3354. 2. A multiline comment is not closed. The 'end' pointer is set as the head
  3355. of this comment block.
  3356. */
  3357. static_noinline bool skip_spaces_and_comments(u8 **ptr) {
  3358. u8 *hdr = *ptr;
  3359. u8 *cur = *ptr;
  3360. u8 **end = ptr;
  3361. while (true) {
  3362. if (byte_match_2(cur, "/*")) {
  3363. hdr = cur;
  3364. cur += 2;
  3365. while (true) {
  3366. if (byte_match_2(cur, "*/")) {
  3367. cur += 2;
  3368. break;
  3369. }
  3370. if (*cur == 0) {
  3371. *end = hdr;
  3372. return false;
  3373. }
  3374. cur++;
  3375. }
  3376. continue;
  3377. }
  3378. if (byte_match_2(cur, "//")) {
  3379. cur += 2;
  3380. while (!char_is_line_end(*cur)) cur++;
  3381. continue;
  3382. }
  3383. if (char_is_space(*cur)) {
  3384. cur += 1;
  3385. while (char_is_space(*cur)) cur++;
  3386. continue;
  3387. }
  3388. break;
  3389. }
  3390. *end = cur;
  3391. return hdr != cur;
  3392. }
  3393. /**
  3394. Check truncated string.
  3395. Returns true if `cur` match `str` but is truncated.
  3396. */
  3397. static_inline bool is_truncated_str(u8 *cur, u8 *end,
  3398. const char *str,
  3399. bool case_sensitive) {
  3400. usize len = strlen(str);
  3401. if (cur + len <= end || end <= cur) return false;
  3402. if (case_sensitive) {
  3403. return memcmp(cur, str, (usize)(end - cur)) == 0;
  3404. }
  3405. for (; cur < end; cur++, str++) {
  3406. if ((*cur != (u8)*str) && (*cur != (u8)*str - 'a' + 'A')) {
  3407. return false;
  3408. }
  3409. }
  3410. return true;
  3411. }
  3412. /**
  3413. Check truncated JSON on parsing errors.
  3414. Returns true if the input is valid but truncated.
  3415. */
  3416. static_noinline bool is_truncated_end(u8 *hdr, u8 *cur, u8 *end,
  3417. yyjson_read_code code,
  3418. yyjson_read_flag flg) {
  3419. if (cur >= end) return true;
  3420. if (code == YYJSON_READ_ERROR_LITERAL) {
  3421. if (is_truncated_str(cur, end, "true", true) ||
  3422. is_truncated_str(cur, end, "false", true) ||
  3423. is_truncated_str(cur, end, "null", true)) {
  3424. return true;
  3425. }
  3426. }
  3427. if (code == YYJSON_READ_ERROR_UNEXPECTED_CHARACTER ||
  3428. code == YYJSON_READ_ERROR_INVALID_NUMBER ||
  3429. code == YYJSON_READ_ERROR_LITERAL) {
  3430. if (has_read_flag(ALLOW_INF_AND_NAN)) {
  3431. if (*cur == '-') cur++;
  3432. if (is_truncated_str(cur, end, "infinity", false) ||
  3433. is_truncated_str(cur, end, "nan", false)) {
  3434. return true;
  3435. }
  3436. }
  3437. }
  3438. if (code == YYJSON_READ_ERROR_UNEXPECTED_CONTENT) {
  3439. if (has_read_flag(ALLOW_INF_AND_NAN)) {
  3440. if (hdr + 3 <= cur &&
  3441. is_truncated_str(cur - 3, end, "infinity", false)) {
  3442. return true; /* e.g. infin would be read as inf + in */
  3443. }
  3444. }
  3445. }
  3446. if (code == YYJSON_READ_ERROR_INVALID_STRING) {
  3447. usize len = (usize)(end - cur);
  3448. /* unicode escape sequence */
  3449. if (*cur == '\\') {
  3450. if (len == 1) return true;
  3451. if (len <= 5) {
  3452. if (*++cur != 'u') return false;
  3453. for (++cur; cur < end; cur++) {
  3454. if (!char_is_hex(*cur)) return false;
  3455. }
  3456. return true;
  3457. }
  3458. return false;
  3459. }
  3460. /* 2 to 4 bytes UTF-8, see `read_string()` for details. */
  3461. if (*cur & 0x80) {
  3462. u8 c0 = cur[0], c1 = cur[1], c2 = cur[2];
  3463. if (len == 1) {
  3464. /* 2 bytes UTF-8, truncated */
  3465. if ((c0 & 0xE0) == 0xC0 && (c0 & 0x1E) != 0x00) return true;
  3466. /* 3 bytes UTF-8, truncated */
  3467. if ((c0 & 0xF0) == 0xE0) return true;
  3468. /* 4 bytes UTF-8, truncated */
  3469. if ((c0 & 0xF8) == 0xF0 && (c0 & 0x07) <= 0x04) return true;
  3470. }
  3471. if (len == 2) {
  3472. /* 3 bytes UTF-8, truncated */
  3473. if ((c0 & 0xF0) == 0xE0 &&
  3474. (c1 & 0xC0) == 0x80) {
  3475. u8 pat = (u8)(((c0 & 0x0F) << 1) | ((c1 & 0x20) >> 5));
  3476. return 0x01 <= pat && pat != 0x1B;
  3477. }
  3478. /* 4 bytes UTF-8, truncated */
  3479. if ((c0 & 0xF8) == 0xF0 &&
  3480. (c1 & 0xC0) == 0x80) {
  3481. u8 pat = (u8)(((c0 & 0x07) << 2) | ((c1 & 0x30) >> 4));
  3482. return 0x01 <= pat && pat <= 0x10;
  3483. }
  3484. }
  3485. if (len == 3) {
  3486. /* 4 bytes UTF-8, truncated */
  3487. if ((c0 & 0xF8) == 0xF0 &&
  3488. (c1 & 0xC0) == 0x80 &&
  3489. (c2 & 0xC0) == 0x80) {
  3490. u8 pat = (u8)(((c0 & 0x07) << 2) | ((c1 & 0x30) >> 4));
  3491. return 0x01 <= pat && pat <= 0x10;
  3492. }
  3493. }
  3494. }
  3495. }
  3496. return false;
  3497. }
  3498. #if YYJSON_HAS_IEEE_754 && !YYJSON_DISABLE_FAST_FP_CONV /* FP_READER */
  3499. /*==============================================================================
  3500. * BigInt For Floating Point Number Reader
  3501. *
  3502. * The bigint algorithm is used by floating-point number reader to get correctly
  3503. * rounded result for numbers with lots of digits. This part of code is rarely
  3504. * used for common numbers.
  3505. *============================================================================*/
  3506. /** Maximum exponent of exact pow10 */
  3507. #define U64_POW10_MAX_EXP 19
  3508. /** Table: [ 10^0, ..., 10^19 ] (generate with misc/make_tables.c) */
  3509. static const u64 u64_pow10_table[U64_POW10_MAX_EXP + 1] = {
  3510. U64(0x00000000, 0x00000001), U64(0x00000000, 0x0000000A),
  3511. U64(0x00000000, 0x00000064), U64(0x00000000, 0x000003E8),
  3512. U64(0x00000000, 0x00002710), U64(0x00000000, 0x000186A0),
  3513. U64(0x00000000, 0x000F4240), U64(0x00000000, 0x00989680),
  3514. U64(0x00000000, 0x05F5E100), U64(0x00000000, 0x3B9ACA00),
  3515. U64(0x00000002, 0x540BE400), U64(0x00000017, 0x4876E800),
  3516. U64(0x000000E8, 0xD4A51000), U64(0x00000918, 0x4E72A000),
  3517. U64(0x00005AF3, 0x107A4000), U64(0x00038D7E, 0xA4C68000),
  3518. U64(0x002386F2, 0x6FC10000), U64(0x01634578, 0x5D8A0000),
  3519. U64(0x0DE0B6B3, 0xA7640000), U64(0x8AC72304, 0x89E80000)
  3520. };
  3521. /** Maximum numbers of chunks used by a bigint (58 is enough here). */
  3522. #define BIGINT_MAX_CHUNKS 64
  3523. /** Unsigned arbitrarily large integer */
  3524. typedef struct bigint {
  3525. u32 used; /* used chunks count, should not be 0 */
  3526. u64 bits[BIGINT_MAX_CHUNKS]; /* chunks */
  3527. } bigint;
  3528. /**
  3529. Evaluate 'big += val'.
  3530. @param big A big number (can be 0).
  3531. @param val An unsigned integer (can be 0).
  3532. */
  3533. static_inline void bigint_add_u64(bigint *big, u64 val) {
  3534. u32 idx, max;
  3535. u64 num = big->bits[0];
  3536. u64 add = num + val;
  3537. big->bits[0] = add;
  3538. if (likely((add >= num) || (add >= val))) return;
  3539. for ((void)(idx = 1), max = big->used; idx < max; idx++) {
  3540. if (likely(big->bits[idx] != U64_MAX)) {
  3541. big->bits[idx] += 1;
  3542. return;
  3543. }
  3544. big->bits[idx] = 0;
  3545. }
  3546. big->bits[big->used++] = 1;
  3547. }
  3548. /**
  3549. Evaluate 'big *= val'.
  3550. @param big A big number (can be 0).
  3551. @param val An unsigned integer (cannot be 0).
  3552. */
  3553. static_inline void bigint_mul_u64(bigint *big, u64 val) {
  3554. u32 idx = 0, max = big->used;
  3555. u64 hi, lo, carry = 0;
  3556. for (; idx < max; idx++) {
  3557. if (big->bits[idx]) break;
  3558. }
  3559. for (; idx < max; idx++) {
  3560. u128_mul_add(big->bits[idx], val, carry, &hi, &lo);
  3561. big->bits[idx] = lo;
  3562. carry = hi;
  3563. }
  3564. if (carry) big->bits[big->used++] = carry;
  3565. }
  3566. /**
  3567. Evaluate 'big *= 2^exp'.
  3568. @param big A big number (can be 0).
  3569. @param exp An exponent integer (can be 0).
  3570. */
  3571. static_inline void bigint_mul_pow2(bigint *big, u32 exp) {
  3572. u32 shft = exp % 64;
  3573. u32 move = exp / 64;
  3574. u32 idx = big->used;
  3575. if (unlikely(shft == 0)) {
  3576. for (; idx > 0; idx--) {
  3577. big->bits[idx + move - 1] = big->bits[idx - 1];
  3578. }
  3579. big->used += move;
  3580. while (move) big->bits[--move] = 0;
  3581. } else {
  3582. big->bits[idx] = 0;
  3583. for (; idx > 0; idx--) {
  3584. u64 num = big->bits[idx] << shft;
  3585. num |= big->bits[idx - 1] >> (64 - shft);
  3586. big->bits[idx + move] = num;
  3587. }
  3588. big->bits[move] = big->bits[0] << shft;
  3589. big->used += move + (big->bits[big->used + move] > 0);
  3590. while (move) big->bits[--move] = 0;
  3591. }
  3592. }
  3593. /**
  3594. Evaluate 'big *= 10^exp'.
  3595. @param big A big number (can be 0).
  3596. @param exp An exponent integer (cannot be 0).
  3597. */
  3598. static_inline void bigint_mul_pow10(bigint *big, i32 exp) {
  3599. for (; exp >= U64_POW10_MAX_EXP; exp -= U64_POW10_MAX_EXP) {
  3600. bigint_mul_u64(big, u64_pow10_table[U64_POW10_MAX_EXP]);
  3601. }
  3602. if (exp) {
  3603. bigint_mul_u64(big, u64_pow10_table[exp]);
  3604. }
  3605. }
  3606. /**
  3607. Compare two bigint.
  3608. @return -1 if 'a < b', +1 if 'a > b', 0 if 'a == b'.
  3609. */
  3610. static_inline i32 bigint_cmp(bigint *a, bigint *b) {
  3611. u32 idx = a->used;
  3612. if (a->used < b->used) return -1;
  3613. if (a->used > b->used) return +1;
  3614. while (idx-- > 0) {
  3615. u64 av = a->bits[idx];
  3616. u64 bv = b->bits[idx];
  3617. if (av < bv) return -1;
  3618. if (av > bv) return +1;
  3619. }
  3620. return 0;
  3621. }
  3622. /**
  3623. Evaluate 'big = val'.
  3624. @param big A big number (can be 0).
  3625. @param val An unsigned integer (can be 0).
  3626. */
  3627. static_inline void bigint_set_u64(bigint *big, u64 val) {
  3628. big->used = 1;
  3629. big->bits[0] = val;
  3630. }
  3631. /** Set a bigint with floating point number string. */
  3632. static_noinline void bigint_set_buf(bigint *big, u64 sig, i32 *exp,
  3633. u8 *sig_cut, u8 *sig_end, u8 *dot_pos) {
  3634. if (unlikely(!sig_cut)) {
  3635. /* no digit cut, set significant part only */
  3636. bigint_set_u64(big, sig);
  3637. return;
  3638. } else {
  3639. /* some digits were cut, read them from 'sig_cut' to 'sig_end' */
  3640. u8 *hdr = sig_cut;
  3641. u8 *cur = hdr;
  3642. u32 len = 0;
  3643. u64 val = 0;
  3644. bool dig_big_cut = false;
  3645. bool has_dot = (hdr < dot_pos) & (dot_pos < sig_end);
  3646. u32 dig_len_total = U64_SAFE_DIG + (u32)(sig_end - hdr) - has_dot;
  3647. sig -= (*sig_cut >= '5'); /* sig was rounded before */
  3648. if (dig_len_total > F64_MAX_DEC_DIG) {
  3649. dig_big_cut = true;
  3650. sig_end -= dig_len_total - (F64_MAX_DEC_DIG + 1);
  3651. sig_end -= (dot_pos + 1 == sig_end);
  3652. dig_len_total = (F64_MAX_DEC_DIG + 1);
  3653. }
  3654. *exp -= (i32)dig_len_total - U64_SAFE_DIG;
  3655. big->used = 1;
  3656. big->bits[0] = sig;
  3657. while (cur < sig_end) {
  3658. if (likely(cur != dot_pos)) {
  3659. val = val * 10 + (u8)(*cur++ - '0');
  3660. len++;
  3661. if (unlikely(cur == sig_end && dig_big_cut)) {
  3662. /* The last digit must be non-zero, */
  3663. /* set it to '1' for correct rounding. */
  3664. val = val - (val % 10) + 1;
  3665. }
  3666. if (len == U64_SAFE_DIG || cur == sig_end) {
  3667. bigint_mul_pow10(big, (i32)len);
  3668. bigint_add_u64(big, val);
  3669. val = 0;
  3670. len = 0;
  3671. }
  3672. } else {
  3673. cur++;
  3674. }
  3675. }
  3676. }
  3677. }
  3678. /*==============================================================================
  3679. * Diy Floating Point
  3680. *============================================================================*/
  3681. /** "Do It Yourself Floating Point" struct. */
  3682. typedef struct diy_fp {
  3683. u64 sig; /* significand */
  3684. i32 exp; /* exponent, base 2 */
  3685. i32 pad; /* padding, useless */
  3686. } diy_fp;
  3687. /** Get cached rounded diy_fp with pow(10, e) The input value must in range
  3688. [POW10_SIG_TABLE_MIN_EXP, POW10_SIG_TABLE_MAX_EXP]. */
  3689. static_inline diy_fp diy_fp_get_cached_pow10(i32 exp10) {
  3690. diy_fp fp;
  3691. u64 sig_ext;
  3692. pow10_table_get_sig(exp10, &fp.sig, &sig_ext);
  3693. pow10_table_get_exp(exp10, &fp.exp);
  3694. fp.sig += (sig_ext >> 63);
  3695. return fp;
  3696. }
  3697. /** Returns fp * fp2. */
  3698. static_inline diy_fp diy_fp_mul(diy_fp fp, diy_fp fp2) {
  3699. u64 hi, lo;
  3700. u128_mul(fp.sig, fp2.sig, &hi, &lo);
  3701. fp.sig = hi + (lo >> 63);
  3702. fp.exp += fp2.exp + 64;
  3703. return fp;
  3704. }
  3705. /** Convert diy_fp to IEEE-754 raw value. */
  3706. static_inline u64 diy_fp_to_ieee_raw(diy_fp fp) {
  3707. u64 sig = fp.sig;
  3708. i32 exp = fp.exp;
  3709. u32 lz_bits;
  3710. if (unlikely(fp.sig == 0)) return 0;
  3711. lz_bits = u64_lz_bits(sig);
  3712. sig <<= lz_bits;
  3713. sig >>= F64_BITS - F64_SIG_FULL_BITS;
  3714. exp -= (i32)lz_bits;
  3715. exp += F64_BITS - F64_SIG_FULL_BITS;
  3716. exp += F64_SIG_BITS;
  3717. if (unlikely(exp >= F64_MAX_BIN_EXP)) {
  3718. /* overflow */
  3719. return F64_RAW_INF;
  3720. } else if (likely(exp >= F64_MIN_BIN_EXP - 1)) {
  3721. /* normal */
  3722. exp += F64_EXP_BIAS;
  3723. return ((u64)exp << F64_SIG_BITS) | (sig & F64_SIG_MASK);
  3724. } else if (likely(exp >= F64_MIN_BIN_EXP - F64_SIG_FULL_BITS)) {
  3725. /* subnormal */
  3726. return sig >> (F64_MIN_BIN_EXP - exp - 1);
  3727. } else {
  3728. /* underflow */
  3729. return 0;
  3730. }
  3731. }
  3732. /*==============================================================================
  3733. * JSON Number Reader (IEEE-754)
  3734. *============================================================================*/
  3735. /** Maximum exact pow10 exponent for double value. */
  3736. #define F64_POW10_EXP_MAX_EXACT 22
  3737. /** Cached pow10 table. */
  3738. static const f64 f64_pow10_table[] = {
  3739. 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12,
  3740. 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22
  3741. };
  3742. /**
  3743. Read a JSON number.
  3744. 1. This function assume that the floating-point number is in IEEE-754 format.
  3745. 2. This function support uint64/int64/double number. If an integer number
  3746. cannot fit in uint64/int64, it will returns as a double number. If a double
  3747. number is infinite, the return value is based on flag.
  3748. 3. This function (with inline attribute) may generate a lot of instructions.
  3749. */
  3750. static_inline bool read_number(u8 **ptr,
  3751. u8 **pre,
  3752. yyjson_read_flag flg,
  3753. yyjson_val *val,
  3754. const char **msg) {
  3755. #define return_err(_pos, _msg) do { \
  3756. *msg = _msg; \
  3757. *end = _pos; \
  3758. return false; \
  3759. } while (false)
  3760. #define return_0() do { \
  3761. val->tag = YYJSON_TYPE_NUM | (u8)((u8)sign << 3); \
  3762. val->uni.u64 = 0; \
  3763. *end = cur; return true; \
  3764. } while (false)
  3765. #define return_i64(_v) do { \
  3766. val->tag = YYJSON_TYPE_NUM | (u8)((u8)sign << 3); \
  3767. val->uni.u64 = (u64)(sign ? (u64)(~(_v) + 1) : (u64)(_v)); \
  3768. *end = cur; return true; \
  3769. } while (false)
  3770. #define return_f64(_v) do { \
  3771. val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; \
  3772. val->uni.f64 = sign ? -(f64)(_v) : (f64)(_v); \
  3773. *end = cur; return true; \
  3774. } while (false)
  3775. #define return_f64_bin(_v) do { \
  3776. val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; \
  3777. val->uni.u64 = ((u64)sign << 63) | (u64)(_v); \
  3778. *end = cur; return true; \
  3779. } while (false)
  3780. #define return_inf() do { \
  3781. if (has_read_flag(BIGNUM_AS_RAW)) return_raw(); \
  3782. if (has_read_flag(ALLOW_INF_AND_NAN)) return_f64_bin(F64_RAW_INF); \
  3783. else return_err(hdr, "number is infinity when parsed as double"); \
  3784. } while (false)
  3785. #define return_raw() do { \
  3786. if (*pre) **pre = '\0'; /* add null-terminator for previous raw string */ \
  3787. val->tag = ((u64)(cur - hdr) << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW; \
  3788. val->uni.str = (const char *)hdr; \
  3789. *pre = cur; *end = cur; return true; \
  3790. } while (false)
  3791. u8 *sig_cut = NULL; /* significant part cutting position for long number */
  3792. u8 *sig_end = NULL; /* significant part ending position */
  3793. u8 *dot_pos = NULL; /* decimal point position */
  3794. u64 sig = 0; /* significant part of the number */
  3795. i32 exp = 0; /* exponent part of the number */
  3796. bool exp_sign; /* temporary exponent sign from literal part */
  3797. i64 exp_sig = 0; /* temporary exponent number from significant part */
  3798. i64 exp_lit = 0; /* temporary exponent number from exponent literal part */
  3799. u64 num; /* temporary number for reading */
  3800. u8 *tmp; /* temporary cursor for reading */
  3801. u8 *hdr = *ptr;
  3802. u8 *cur = *ptr;
  3803. u8 **end = ptr;
  3804. bool sign;
  3805. /* read number as raw string if has `YYJSON_READ_NUMBER_AS_RAW` flag */
  3806. if (unlikely(pre && !has_read_flag(BIGNUM_AS_RAW))) {
  3807. return read_number_raw(ptr, pre, flg, val, msg);
  3808. }
  3809. sign = (*hdr == '-');
  3810. cur += sign;
  3811. /* begin with a leading zero or non-digit */
  3812. if (unlikely(!digi_is_nonzero(*cur))) { /* 0 or non-digit char */
  3813. if (unlikely(*cur != '0')) { /* non-digit char */
  3814. if (has_read_flag(ALLOW_INF_AND_NAN)) {
  3815. if (read_inf_or_nan(sign, &cur, pre, val)) {
  3816. *end = cur;
  3817. return true;
  3818. }
  3819. }
  3820. return_err(cur, "no digit after minus sign");
  3821. }
  3822. /* begin with 0 */
  3823. if (likely(!digi_is_digit_or_fp(*++cur))) return_0();
  3824. if (likely(*cur == '.')) {
  3825. dot_pos = cur++;
  3826. if (unlikely(!digi_is_digit(*cur))) {
  3827. return_err(cur, "no digit after decimal point");
  3828. }
  3829. while (unlikely(*cur == '0')) cur++;
  3830. if (likely(digi_is_digit(*cur))) {
  3831. /* first non-zero digit after decimal point */
  3832. sig = (u64)(*cur - '0'); /* read first digit */
  3833. cur--;
  3834. goto digi_frac_1; /* continue read fraction part */
  3835. }
  3836. }
  3837. if (unlikely(digi_is_digit(*cur))) {
  3838. return_err(cur - 1, "number with leading zero is not allowed");
  3839. }
  3840. if (unlikely(digi_is_exp(*cur))) { /* 0 with any exponent is still 0 */
  3841. cur += (usize)1 + digi_is_sign(cur[1]);
  3842. if (unlikely(!digi_is_digit(*cur))) {
  3843. return_err(cur, "no digit after exponent sign");
  3844. }
  3845. while (digi_is_digit(*++cur));
  3846. }
  3847. return_f64_bin(0);
  3848. }
  3849. /* begin with non-zero digit */
  3850. sig = (u64)(*cur - '0');
  3851. /*
  3852. Read integral part, same as the following code.
  3853. for (int i = 1; i <= 18; i++) {
  3854. num = cur[i] - '0';
  3855. if (num <= 9) sig = num + sig * 10;
  3856. else goto digi_sepr_i;
  3857. }
  3858. */
  3859. #define expr_intg(i) \
  3860. if (likely((num = (u64)(cur[i] - (u8)'0')) <= 9)) sig = num + sig * 10; \
  3861. else { goto digi_sepr_##i; }
  3862. repeat_in_1_18(expr_intg)
  3863. #undef expr_intg
  3864. cur += 19; /* skip continuous 19 digits */
  3865. if (!digi_is_digit_or_fp(*cur)) {
  3866. /* this number is an integer consisting of 19 digits */
  3867. if (sign && (sig > ((u64)1 << 63))) { /* overflow */
  3868. if (has_read_flag(BIGNUM_AS_RAW)) return_raw();
  3869. return_f64(normalized_u64_to_f64(sig));
  3870. }
  3871. return_i64(sig);
  3872. }
  3873. goto digi_intg_more; /* read more digits in integral part */
  3874. /* process first non-digit character */
  3875. #define expr_sepr(i) \
  3876. digi_sepr_##i: \
  3877. if (likely(!digi_is_fp(cur[i]))) { cur += i; return_i64(sig); } \
  3878. dot_pos = cur + i; \
  3879. if (likely(cur[i] == '.')) goto digi_frac_##i; \
  3880. cur += i; sig_end = cur; goto digi_exp_more;
  3881. repeat_in_1_18(expr_sepr)
  3882. #undef expr_sepr
  3883. /* read fraction part */
  3884. #define expr_frac(i) \
  3885. digi_frac_##i: \
  3886. if (likely((num = (u64)(cur[i + 1] - (u8)'0')) <= 9)) \
  3887. sig = num + sig * 10; \
  3888. else { goto digi_stop_##i; }
  3889. repeat_in_1_18(expr_frac)
  3890. #undef expr_frac
  3891. cur += 20; /* skip 19 digits and 1 decimal point */
  3892. if (!digi_is_digit(*cur)) goto digi_frac_end; /* fraction part end */
  3893. goto digi_frac_more; /* read more digits in fraction part */
  3894. /* significant part end */
  3895. #define expr_stop(i) \
  3896. digi_stop_##i: \
  3897. cur += i + 1; \
  3898. goto digi_frac_end;
  3899. repeat_in_1_18(expr_stop)
  3900. #undef expr_stop
  3901. /* read more digits in integral part */
  3902. digi_intg_more:
  3903. if (digi_is_digit(*cur)) {
  3904. if (!digi_is_digit_or_fp(cur[1])) {
  3905. /* this number is an integer consisting of 20 digits */
  3906. num = (u64)(*cur - '0');
  3907. if ((sig < (U64_MAX / 10)) ||
  3908. (sig == (U64_MAX / 10) && num <= (U64_MAX % 10))) {
  3909. sig = num + sig * 10;
  3910. cur++;
  3911. /* convert to double if overflow */
  3912. if (sign) {
  3913. if (has_read_flag(BIGNUM_AS_RAW)) return_raw();
  3914. return_f64(normalized_u64_to_f64(sig));
  3915. }
  3916. return_i64(sig);
  3917. }
  3918. }
  3919. }
  3920. if (digi_is_exp(*cur)) {
  3921. dot_pos = cur;
  3922. goto digi_exp_more;
  3923. }
  3924. if (*cur == '.') {
  3925. dot_pos = cur++;
  3926. if (!digi_is_digit(*cur)) {
  3927. return_err(cur, "no digit after decimal point");
  3928. }
  3929. }
  3930. /* read more digits in fraction part */
  3931. digi_frac_more:
  3932. sig_cut = cur; /* too large to fit in u64, excess digits need to be cut */
  3933. sig += (*cur >= '5'); /* round */
  3934. while (digi_is_digit(*++cur));
  3935. if (!dot_pos) {
  3936. if (!digi_is_fp(*cur) && has_read_flag(BIGNUM_AS_RAW)) {
  3937. return_raw(); /* it's a large integer */
  3938. }
  3939. dot_pos = cur;
  3940. if (*cur == '.') {
  3941. if (!digi_is_digit(*++cur)) {
  3942. return_err(cur, "no digit after decimal point");
  3943. }
  3944. while (digi_is_digit(*cur)) cur++;
  3945. }
  3946. }
  3947. exp_sig = (i64)(dot_pos - sig_cut);
  3948. exp_sig += (dot_pos < sig_cut);
  3949. /* ignore trailing zeros */
  3950. tmp = cur - 1;
  3951. while (*tmp == '0' || *tmp == '.') tmp--;
  3952. if (tmp < sig_cut) {
  3953. sig_cut = NULL;
  3954. } else {
  3955. sig_end = cur;
  3956. }
  3957. if (digi_is_exp(*cur)) goto digi_exp_more;
  3958. goto digi_exp_finish;
  3959. /* fraction part end */
  3960. digi_frac_end:
  3961. if (unlikely(dot_pos + 1 == cur)) {
  3962. return_err(cur, "no digit after decimal point");
  3963. }
  3964. sig_end = cur;
  3965. exp_sig = -(i64)((u64)(cur - dot_pos) - 1);
  3966. if (likely(!digi_is_exp(*cur))) {
  3967. if (unlikely(exp_sig < F64_MIN_DEC_EXP - 19)) {
  3968. return_f64_bin(0); /* underflow */
  3969. }
  3970. exp = (i32)exp_sig;
  3971. goto digi_finish;
  3972. } else {
  3973. goto digi_exp_more;
  3974. }
  3975. /* read exponent part */
  3976. digi_exp_more:
  3977. exp_sign = (*++cur == '-');
  3978. cur += digi_is_sign(*cur);
  3979. if (unlikely(!digi_is_digit(*cur))) {
  3980. return_err(cur, "no digit after exponent sign");
  3981. }
  3982. while (*cur == '0') cur++;
  3983. /* read exponent literal */
  3984. tmp = cur;
  3985. while (digi_is_digit(*cur)) {
  3986. exp_lit = (i64)((u8)(*cur++ - '0') + (u64)exp_lit * 10);
  3987. }
  3988. if (unlikely(cur - tmp >= U64_SAFE_DIG)) {
  3989. if (exp_sign) {
  3990. return_f64_bin(0); /* underflow */
  3991. } else {
  3992. return_inf(); /* overflow */
  3993. }
  3994. }
  3995. exp_sig += exp_sign ? -exp_lit : exp_lit;
  3996. /* validate exponent value */
  3997. digi_exp_finish:
  3998. if (unlikely(exp_sig < F64_MIN_DEC_EXP - 19)) {
  3999. return_f64_bin(0); /* underflow */
  4000. }
  4001. if (unlikely(exp_sig > F64_MAX_DEC_EXP)) {
  4002. return_inf(); /* overflow */
  4003. }
  4004. exp = (i32)exp_sig;
  4005. /* all digit read finished */
  4006. digi_finish:
  4007. /*
  4008. Fast path 1:
  4009. 1. The floating-point number calculation should be accurate, see the
  4010. comments of macro `YYJSON_DOUBLE_MATH_CORRECT`.
  4011. 2. Correct rounding should be performed (fegetround() == FE_TONEAREST).
  4012. 3. The input of floating point number calculation does not lose precision,
  4013. which means: 64 - leading_zero(input) - trailing_zero(input) < 53.
  4014. We don't check all available inputs here, because that would make the code
  4015. more complicated, and not friendly to branch predictor.
  4016. */
  4017. #if YYJSON_DOUBLE_MATH_CORRECT
  4018. if (sig < ((u64)1 << 53) &&
  4019. exp >= -F64_POW10_EXP_MAX_EXACT &&
  4020. exp <= +F64_POW10_EXP_MAX_EXACT) {
  4021. f64 dbl = (f64)sig;
  4022. if (exp < 0) {
  4023. dbl /= f64_pow10_table[-exp];
  4024. } else {
  4025. dbl *= f64_pow10_table[+exp];
  4026. }
  4027. return_f64(dbl);
  4028. }
  4029. #endif
  4030. /*
  4031. Fast path 2:
  4032. To keep it simple, we only accept normal number here,
  4033. let the slow path to handle subnormal and infinity number.
  4034. */
  4035. if (likely(!sig_cut &&
  4036. exp > -F64_MAX_DEC_EXP + 1 &&
  4037. exp < +F64_MAX_DEC_EXP - 20)) {
  4038. /*
  4039. The result value is exactly equal to (sig * 10^exp),
  4040. the exponent part (10^exp) can be converted to (sig2 * 2^exp2).
  4041. The sig2 can be an infinite length number, only the highest 128 bits
  4042. is cached in the pow10_sig_table.
  4043. Now we have these bits:
  4044. sig1 (normalized 64bit) : aaaaaaaa
  4045. sig2 (higher 64bit) : bbbbbbbb
  4046. sig2_ext (lower 64bit) : cccccccc
  4047. sig2_cut (extra unknown bits) : dddddddddddd....
  4048. And the calculation process is:
  4049. ----------------------------------------
  4050. aaaaaaaa *
  4051. bbbbbbbbccccccccdddddddddddd....
  4052. ----------------------------------------
  4053. abababababababab +
  4054. acacacacacacacac +
  4055. adadadadadadadadadad....
  4056. ----------------------------------------
  4057. [hi____][lo____] +
  4058. [hi2___][lo2___] +
  4059. [unknown___________....]
  4060. ----------------------------------------
  4061. The addition with carry may affect higher bits, but if there is a 0
  4062. in higher bits, the bits higher than 0 will not be affected.
  4063. `lo2` + `unknown` may get a carry bit and may affect `hi2`, the max
  4064. value of `hi2` is 0xFFFFFFFFFFFFFFFE, so `hi2` will not overflow.
  4065. `lo` + `hi2` may also get a carry bit and may affect `hi`, but only
  4066. the highest significant 53 bits of `hi` is needed. If there is a 0
  4067. in the lower bits of `hi`, then all the following bits can be dropped.
  4068. To convert the result to IEEE-754 double number, we need to perform
  4069. correct rounding:
  4070. 1. if bit 54 is 0, round down,
  4071. 2. if bit 54 is 1 and any bit beyond bit 54 is 1, round up,
  4072. 3. if bit 54 is 1 and all bits beyond bit 54 are 0, round to even,
  4073. as the extra bits is unknown, this case will not be handled here.
  4074. */
  4075. u64 raw;
  4076. u64 sig1, sig2, sig2_ext, hi, lo, hi2, lo2, add, bits;
  4077. i32 exp2;
  4078. u32 lz;
  4079. bool exact = false, carry, round_up;
  4080. /* convert (10^exp) to (sig2 * 2^exp2) */
  4081. pow10_table_get_sig(exp, &sig2, &sig2_ext);
  4082. pow10_table_get_exp(exp, &exp2);
  4083. /* normalize and multiply */
  4084. lz = u64_lz_bits(sig);
  4085. sig1 = sig << lz;
  4086. exp2 -= (i32)lz;
  4087. u128_mul(sig1, sig2, &hi, &lo);
  4088. /*
  4089. The `hi` is in range [0x4000000000000000, 0xFFFFFFFFFFFFFFFE],
  4090. To get normalized value, `hi` should be shifted to the left by 0 or 1.
  4091. The highest significant 53 bits is used by IEEE-754 double number,
  4092. and the bit 54 is used to detect rounding direction.
  4093. The lowest (64 - 54 - 1) bits is used to check whether it contains 0.
  4094. */
  4095. bits = hi & (((u64)1 << (64 - 54 - 1)) - 1);
  4096. if (bits - 1 < (((u64)1 << (64 - 54 - 1)) - 2)) {
  4097. /*
  4098. (bits != 0 && bits != 0x1FF) => (bits - 1 < 0x1FF - 1)
  4099. The `bits` is not zero, so we don't need to check `round to even`
  4100. case. The `bits` contains bit `0`, so we can drop the extra bits
  4101. after `0`.
  4102. */
  4103. exact = true;
  4104. } else {
  4105. /*
  4106. (bits == 0 || bits == 0x1FF)
  4107. The `bits` is filled with all `0` or all `1`, so we need to check
  4108. lower bits with another 64-bit multiplication.
  4109. */
  4110. u128_mul(sig1, sig2_ext, &hi2, &lo2);
  4111. add = lo + hi2;
  4112. if (add + 1 > (u64)1) {
  4113. /*
  4114. (add != 0 && add != U64_MAX) => (add + 1 > 1)
  4115. The `add` is not zero, so we don't need to check `round to
  4116. even` case. The `add` contains bit `0`, so we can drop the
  4117. extra bits after `0`. The `hi` cannot be U64_MAX, so it will
  4118. not overflow.
  4119. */
  4120. carry = add < lo || add < hi2;
  4121. hi += carry;
  4122. exact = true;
  4123. }
  4124. }
  4125. if (exact) {
  4126. /* normalize */
  4127. lz = hi < ((u64)1 << 63);
  4128. hi <<= lz;
  4129. exp2 -= (i32)lz;
  4130. exp2 += 64;
  4131. /* test the bit 54 and get rounding direction */
  4132. round_up = (hi & ((u64)1 << (64 - 54))) > (u64)0;
  4133. hi += (round_up ? ((u64)1 << (64 - 54)) : (u64)0);
  4134. /* test overflow */
  4135. if (hi < ((u64)1 << (64 - 54))) {
  4136. hi = ((u64)1 << 63);
  4137. exp2 += 1;
  4138. }
  4139. /* This is a normal number, convert it to IEEE-754 format. */
  4140. hi >>= F64_BITS - F64_SIG_FULL_BITS;
  4141. exp2 += F64_BITS - F64_SIG_FULL_BITS + F64_SIG_BITS;
  4142. exp2 += F64_EXP_BIAS;
  4143. raw = ((u64)exp2 << F64_SIG_BITS) | (hi & F64_SIG_MASK);
  4144. return_f64_bin(raw);
  4145. }
  4146. }
  4147. /*
  4148. Slow path: read double number exactly with diyfp.
  4149. 1. Use cached diyfp to get an approximation value.
  4150. 2. Use bigcomp to check the approximation value if needed.
  4151. This algorithm refers to google's double-conversion project:
  4152. https://github.com/google/double-conversion
  4153. */
  4154. {
  4155. const i32 ERR_ULP_LOG = 3;
  4156. const i32 ERR_ULP = 1 << ERR_ULP_LOG;
  4157. const i32 ERR_CACHED_POW = ERR_ULP / 2;
  4158. const i32 ERR_MUL_FIXED = ERR_ULP / 2;
  4159. const i32 DIY_SIG_BITS = 64;
  4160. const i32 EXP_BIAS = F64_EXP_BIAS + F64_SIG_BITS;
  4161. const i32 EXP_SUBNORMAL = -EXP_BIAS + 1;
  4162. u64 fp_err;
  4163. u32 bits;
  4164. i32 order_of_magnitude;
  4165. i32 effective_significand_size;
  4166. i32 precision_digits_count;
  4167. u64 precision_bits;
  4168. u64 half_way;
  4169. u64 raw;
  4170. diy_fp fp, fp_upper;
  4171. bigint big_full, big_comp;
  4172. i32 cmp;
  4173. fp.sig = sig;
  4174. fp.exp = 0;
  4175. fp_err = sig_cut ? (u64)(ERR_ULP / 2) : (u64)0;
  4176. /* normalize */
  4177. bits = u64_lz_bits(fp.sig);
  4178. fp.sig <<= bits;
  4179. fp.exp -= (i32)bits;
  4180. fp_err <<= bits;
  4181. /* multiply and add error */
  4182. fp = diy_fp_mul(fp, diy_fp_get_cached_pow10(exp));
  4183. fp_err += (u64)ERR_CACHED_POW + (fp_err != 0) + (u64)ERR_MUL_FIXED;
  4184. /* normalize */
  4185. bits = u64_lz_bits(fp.sig);
  4186. fp.sig <<= bits;
  4187. fp.exp -= (i32)bits;
  4188. fp_err <<= bits;
  4189. /* effective significand */
  4190. order_of_magnitude = DIY_SIG_BITS + fp.exp;
  4191. if (likely(order_of_magnitude >= EXP_SUBNORMAL + F64_SIG_FULL_BITS)) {
  4192. effective_significand_size = F64_SIG_FULL_BITS;
  4193. } else if (order_of_magnitude <= EXP_SUBNORMAL) {
  4194. effective_significand_size = 0;
  4195. } else {
  4196. effective_significand_size = order_of_magnitude - EXP_SUBNORMAL;
  4197. }
  4198. /* precision digits count */
  4199. precision_digits_count = DIY_SIG_BITS - effective_significand_size;
  4200. if (unlikely(precision_digits_count + ERR_ULP_LOG >= DIY_SIG_BITS)) {
  4201. i32 shr = (precision_digits_count + ERR_ULP_LOG) - DIY_SIG_BITS + 1;
  4202. fp.sig >>= shr;
  4203. fp.exp += shr;
  4204. fp_err = (fp_err >> shr) + 1 + (u32)ERR_ULP;
  4205. precision_digits_count -= shr;
  4206. }
  4207. /* half way */
  4208. precision_bits = fp.sig & (((u64)1 << precision_digits_count) - 1);
  4209. precision_bits *= (u32)ERR_ULP;
  4210. half_way = (u64)1 << (precision_digits_count - 1);
  4211. half_way *= (u32)ERR_ULP;
  4212. /* rounding */
  4213. fp.sig >>= precision_digits_count;
  4214. fp.sig += (precision_bits >= half_way + fp_err);
  4215. fp.exp += precision_digits_count;
  4216. /* get IEEE double raw value */
  4217. raw = diy_fp_to_ieee_raw(fp);
  4218. if (unlikely(raw == F64_RAW_INF)) return_inf();
  4219. if (likely(precision_bits <= half_way - fp_err ||
  4220. precision_bits >= half_way + fp_err)) {
  4221. return_f64_bin(raw); /* number is accurate */
  4222. }
  4223. /* now the number is the correct value, or the next lower value */
  4224. /* upper boundary */
  4225. if (raw & F64_EXP_MASK) {
  4226. fp_upper.sig = (raw & F64_SIG_MASK) + ((u64)1 << F64_SIG_BITS);
  4227. fp_upper.exp = (i32)((raw & F64_EXP_MASK) >> F64_SIG_BITS);
  4228. } else {
  4229. fp_upper.sig = (raw & F64_SIG_MASK);
  4230. fp_upper.exp = 1;
  4231. }
  4232. fp_upper.exp -= F64_EXP_BIAS + F64_SIG_BITS;
  4233. fp_upper.sig <<= 1;
  4234. fp_upper.exp -= 1;
  4235. fp_upper.sig += 1; /* add half ulp */
  4236. /* compare with bigint */
  4237. bigint_set_buf(&big_full, sig, &exp, sig_cut, sig_end, dot_pos);
  4238. bigint_set_u64(&big_comp, fp_upper.sig);
  4239. if (exp >= 0) {
  4240. bigint_mul_pow10(&big_full, +exp);
  4241. } else {
  4242. bigint_mul_pow10(&big_comp, -exp);
  4243. }
  4244. if (fp_upper.exp > 0) {
  4245. bigint_mul_pow2(&big_comp, (u32)+fp_upper.exp);
  4246. } else {
  4247. bigint_mul_pow2(&big_full, (u32)-fp_upper.exp);
  4248. }
  4249. cmp = bigint_cmp(&big_full, &big_comp);
  4250. if (likely(cmp != 0)) {
  4251. /* round down or round up */
  4252. raw += (cmp > 0);
  4253. } else {
  4254. /* falls midway, round to even */
  4255. raw += (raw & 1);
  4256. }
  4257. if (unlikely(raw == F64_RAW_INF)) return_inf();
  4258. return_f64_bin(raw);
  4259. }
  4260. #undef return_err
  4261. #undef return_inf
  4262. #undef return_0
  4263. #undef return_i64
  4264. #undef return_f64
  4265. #undef return_f64_bin
  4266. #undef return_raw
  4267. }
  4268. #else /* FP_READER */
  4269. /**
  4270. Read a JSON number.
  4271. This is a fallback function if the custom number reader is disabled.
  4272. This function use libc's strtod() to read floating-point number.
  4273. */
  4274. static_inline bool read_number(u8 **ptr,
  4275. u8 **pre,
  4276. yyjson_read_flag flg,
  4277. yyjson_val *val,
  4278. const char **msg) {
  4279. #define return_err(_pos, _msg) do { \
  4280. *msg = _msg; \
  4281. *end = _pos; \
  4282. return false; \
  4283. } while (false)
  4284. #define return_0() do { \
  4285. val->tag = YYJSON_TYPE_NUM | (u64)((u8)sign << 3); \
  4286. val->uni.u64 = 0; \
  4287. *end = cur; return true; \
  4288. } while (false)
  4289. #define return_i64(_v) do { \
  4290. val->tag = YYJSON_TYPE_NUM | (u64)((u8)sign << 3); \
  4291. val->uni.u64 = (u64)(sign ? (u64)(~(_v) + 1) : (u64)(_v)); \
  4292. *end = cur; return true; \
  4293. } while (false)
  4294. #define return_f64(_v) do { \
  4295. val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; \
  4296. val->uni.f64 = sign ? -(f64)(_v) : (f64)(_v); \
  4297. *end = cur; return true; \
  4298. } while (false)
  4299. #define return_f64_bin(_v) do { \
  4300. val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL; \
  4301. val->uni.u64 = ((u64)sign << 63) | (u64)(_v); \
  4302. *end = cur; return true; \
  4303. } while (false)
  4304. #define return_inf() do { \
  4305. if (has_read_flag(BIGNUM_AS_RAW)) return_raw(); \
  4306. if (has_read_flag(ALLOW_INF_AND_NAN)) return_f64_bin(F64_RAW_INF); \
  4307. else return_err(hdr, "number is infinity when parsed as double"); \
  4308. } while (false)
  4309. #define return_raw() do { \
  4310. if (*pre) **pre = '\0'; /* add null-terminator for previous raw string */ \
  4311. val->tag = ((u64)(cur - hdr) << YYJSON_TAG_BIT) | YYJSON_TYPE_RAW; \
  4312. val->uni.str = (const char *)hdr; \
  4313. *pre = cur; *end = cur; return true; \
  4314. } while (false)
  4315. u64 sig, num;
  4316. u8 *hdr = *ptr;
  4317. u8 *cur = *ptr;
  4318. u8 **end = ptr;
  4319. u8 *dot = NULL;
  4320. u8 *f64_end = NULL;
  4321. bool sign;
  4322. /* read number as raw string if has `YYJSON_READ_NUMBER_AS_RAW` flag */
  4323. if (unlikely(pre && !has_read_flag(BIGNUM_AS_RAW))) {
  4324. return read_number_raw(ptr, pre, flg, val, msg);
  4325. }
  4326. sign = (*hdr == '-');
  4327. cur += sign;
  4328. sig = (u8)(*cur - '0');
  4329. /* read first digit, check leading zero */
  4330. if (unlikely(!digi_is_digit(*cur))) {
  4331. if (has_read_flag(ALLOW_INF_AND_NAN)) {
  4332. if (read_inf_or_nan(sign, &cur, pre, val)) {
  4333. *end = cur;
  4334. return true;
  4335. }
  4336. }
  4337. return_err(cur, "no digit after minus sign");
  4338. }
  4339. if (*cur == '0') {
  4340. cur++;
  4341. if (unlikely(digi_is_digit(*cur))) {
  4342. return_err(cur - 1, "number with leading zero is not allowed");
  4343. }
  4344. if (!digi_is_fp(*cur)) return_0();
  4345. goto read_double;
  4346. }
  4347. /* read continuous digits, up to 19 characters */
  4348. #define expr_intg(i) \
  4349. if (likely((num = (u64)(cur[i] - (u8)'0')) <= 9)) sig = num + sig * 10; \
  4350. else { cur += i; goto intg_end; }
  4351. repeat_in_1_18(expr_intg)
  4352. #undef expr_intg
  4353. /* here are 19 continuous digits, skip them */
  4354. cur += 19;
  4355. if (digi_is_digit(cur[0]) && !digi_is_digit_or_fp(cur[1])) {
  4356. /* this number is an integer consisting of 20 digits */
  4357. num = (u8)(*cur - '0');
  4358. if ((sig < (U64_MAX / 10)) ||
  4359. (sig == (U64_MAX / 10) && num <= (U64_MAX % 10))) {
  4360. sig = num + sig * 10;
  4361. cur++;
  4362. if (sign) {
  4363. if (has_read_flag(BIGNUM_AS_RAW)) return_raw();
  4364. return_f64(normalized_u64_to_f64(sig));
  4365. }
  4366. return_i64(sig);
  4367. }
  4368. }
  4369. intg_end:
  4370. /* continuous digits ended */
  4371. if (!digi_is_digit_or_fp(*cur)) {
  4372. /* this number is an integer consisting of 1 to 19 digits */
  4373. if (sign && (sig > ((u64)1 << 63))) {
  4374. if (has_read_flag(BIGNUM_AS_RAW)) return_raw();
  4375. return_f64(normalized_u64_to_f64(sig));
  4376. }
  4377. return_i64(sig);
  4378. }
  4379. read_double:
  4380. /* this number should be read as double */
  4381. while (digi_is_digit(*cur)) cur++;
  4382. if (!digi_is_fp(*cur) && has_read_flag(BIGNUM_AS_RAW)) {
  4383. return_raw(); /* it's a large integer */
  4384. }
  4385. if (*cur == '.') {
  4386. /* skip fraction part */
  4387. dot = cur;
  4388. cur++;
  4389. if (!digi_is_digit(*cur)) {
  4390. return_err(cur, "no digit after decimal point");
  4391. }
  4392. cur++;
  4393. while (digi_is_digit(*cur)) cur++;
  4394. }
  4395. if (digi_is_exp(*cur)) {
  4396. /* skip exponent part */
  4397. cur += 1 + digi_is_sign(cur[1]);
  4398. if (!digi_is_digit(*cur)) {
  4399. return_err(cur, "no digit after exponent sign");
  4400. }
  4401. cur++;
  4402. while (digi_is_digit(*cur)) cur++;
  4403. }
  4404. /*
  4405. libc's strtod() is used to parse the floating-point number.
  4406. Note that the decimal point character used by strtod() is locale-dependent,
  4407. and the rounding direction may affected by fesetround().
  4408. For currently known locales, (en, zh, ja, ko, am, he, hi) use '.' as the
  4409. decimal point, while other locales use ',' as the decimal point.
  4410. Here strtod() is called twice for different locales, but if another thread
  4411. happens calls setlocale() between two strtod(), parsing may still fail.
  4412. */
  4413. val->uni.f64 = strtod((const char *)hdr, (char **)&f64_end);
  4414. if (unlikely(f64_end != cur)) {
  4415. /* replace '.' with ',' for locale */
  4416. bool cut = (*cur == ',');
  4417. if (cut) *cur = ' ';
  4418. if (dot) *dot = ',';
  4419. val->uni.f64 = strtod((const char *)hdr, (char **)&f64_end);
  4420. /* restore ',' to '.' */
  4421. if (cut) *cur = ',';
  4422. if (dot) *dot = '.';
  4423. if (unlikely(f64_end != cur)) {
  4424. return_err(hdr, "strtod() failed to parse the number");
  4425. }
  4426. }
  4427. if (unlikely(val->uni.f64 >= HUGE_VAL || val->uni.f64 <= -HUGE_VAL)) {
  4428. return_inf();
  4429. }
  4430. val->tag = YYJSON_TYPE_NUM | YYJSON_SUBTYPE_REAL;
  4431. *end = cur;
  4432. return true;
  4433. #undef return_err
  4434. #undef return_0
  4435. #undef return_i64
  4436. #undef return_f64
  4437. #undef return_f64_bin
  4438. #undef return_inf
  4439. #undef return_raw
  4440. }
  4441. #endif /* FP_READER */
  4442. /*==============================================================================
  4443. * JSON String Reader
  4444. *============================================================================*/
  4445. /**
  4446. Read a JSON string.
  4447. @param ptr The head pointer of string before '"' prefix (inout).
  4448. @param lst JSON last position.
  4449. @param inv Allow invalid unicode.
  4450. @param val The string value to be written.
  4451. @param msg The error message pointer.
  4452. @return Whether success.
  4453. */
  4454. static_inline bool read_string(u8 **ptr,
  4455. u8 *lst,
  4456. bool inv,
  4457. yyjson_val *val,
  4458. const char **msg) {
  4459. /*
  4460. Each unicode code point is encoded as 1 to 4 bytes in UTF-8 encoding,
  4461. we use 4-byte mask and pattern value to validate UTF-8 byte sequence,
  4462. this requires the input data to have 4-byte zero padding.
  4463. ---------------------------------------------------
  4464. 1 byte
  4465. unicode range [U+0000, U+007F]
  4466. unicode min [.......0]
  4467. unicode max [.1111111]
  4468. bit pattern [0.......]
  4469. ---------------------------------------------------
  4470. 2 byte
  4471. unicode range [U+0080, U+07FF]
  4472. unicode min [......10 ..000000]
  4473. unicode max [...11111 ..111111]
  4474. bit require [...xxxx. ........] (1E 00)
  4475. bit mask [xxx..... xx......] (E0 C0)
  4476. bit pattern [110..... 10......] (C0 80)
  4477. ---------------------------------------------------
  4478. 3 byte
  4479. unicode range [U+0800, U+FFFF]
  4480. unicode min [........ ..100000 ..000000]
  4481. unicode max [....1111 ..111111 ..111111]
  4482. bit require [....xxxx ..x..... ........] (0F 20 00)
  4483. bit mask [xxxx.... xx...... xx......] (F0 C0 C0)
  4484. bit pattern [1110.... 10...... 10......] (E0 80 80)
  4485. ---------------------------------------------------
  4486. 3 byte invalid (reserved for surrogate halves)
  4487. unicode range [U+D800, U+DFFF]
  4488. unicode min [....1101 ..100000 ..000000]
  4489. unicode max [....1101 ..111111 ..111111]
  4490. bit mask [....xxxx ..x..... ........] (0F 20 00)
  4491. bit pattern [....1101 ..1..... ........] (0D 20 00)
  4492. ---------------------------------------------------
  4493. 4 byte
  4494. unicode range [U+10000, U+10FFFF]
  4495. unicode min [........ ...10000 ..000000 ..000000]
  4496. unicode max [.....100 ..001111 ..111111 ..111111]
  4497. bit require [.....xxx ..xx.... ........ ........] (07 30 00 00)
  4498. bit mask [xxxxx... xx...... xx...... xx......] (F8 C0 C0 C0)
  4499. bit pattern [11110... 10...... 10...... 10......] (F0 80 80 80)
  4500. ---------------------------------------------------
  4501. */
  4502. #if YYJSON_ENDIAN == YYJSON_BIG_ENDIAN
  4503. const u32 b1_mask = 0x80000000UL;
  4504. const u32 b1_patt = 0x00000000UL;
  4505. const u32 b2_mask = 0xE0C00000UL;
  4506. const u32 b2_patt = 0xC0800000UL;
  4507. const u32 b2_requ = 0x1E000000UL;
  4508. const u32 b3_mask = 0xF0C0C000UL;
  4509. const u32 b3_patt = 0xE0808000UL;
  4510. const u32 b3_requ = 0x0F200000UL;
  4511. const u32 b3_erro = 0x0D200000UL;
  4512. const u32 b4_mask = 0xF8C0C0C0UL;
  4513. const u32 b4_patt = 0xF0808080UL;
  4514. const u32 b4_requ = 0x07300000UL;
  4515. const u32 b4_err0 = 0x04000000UL;
  4516. const u32 b4_err1 = 0x03300000UL;
  4517. #elif YYJSON_ENDIAN == YYJSON_LITTLE_ENDIAN
  4518. const u32 b1_mask = 0x00000080UL;
  4519. const u32 b1_patt = 0x00000000UL;
  4520. const u32 b2_mask = 0x0000C0E0UL;
  4521. const u32 b2_patt = 0x000080C0UL;
  4522. const u32 b2_requ = 0x0000001EUL;
  4523. const u32 b3_mask = 0x00C0C0F0UL;
  4524. const u32 b3_patt = 0x008080E0UL;
  4525. const u32 b3_requ = 0x0000200FUL;
  4526. const u32 b3_erro = 0x0000200DUL;
  4527. const u32 b4_mask = 0xC0C0C0F8UL;
  4528. const u32 b4_patt = 0x808080F0UL;
  4529. const u32 b4_requ = 0x00003007UL;
  4530. const u32 b4_err0 = 0x00000004UL;
  4531. const u32 b4_err1 = 0x00003003UL;
  4532. #else
  4533. /* this should be evaluated at compile-time */
  4534. v32_uni b1_mask_uni = {{ 0x80, 0x00, 0x00, 0x00 }};
  4535. v32_uni b1_patt_uni = {{ 0x00, 0x00, 0x00, 0x00 }};
  4536. v32_uni b2_mask_uni = {{ 0xE0, 0xC0, 0x00, 0x00 }};
  4537. v32_uni b2_patt_uni = {{ 0xC0, 0x80, 0x00, 0x00 }};
  4538. v32_uni b2_requ_uni = {{ 0x1E, 0x00, 0x00, 0x00 }};
  4539. v32_uni b3_mask_uni = {{ 0xF0, 0xC0, 0xC0, 0x00 }};
  4540. v32_uni b3_patt_uni = {{ 0xE0, 0x80, 0x80, 0x00 }};
  4541. v32_uni b3_requ_uni = {{ 0x0F, 0x20, 0x00, 0x00 }};
  4542. v32_uni b3_erro_uni = {{ 0x0D, 0x20, 0x00, 0x00 }};
  4543. v32_uni b4_mask_uni = {{ 0xF8, 0xC0, 0xC0, 0xC0 }};
  4544. v32_uni b4_patt_uni = {{ 0xF0, 0x80, 0x80, 0x80 }};
  4545. v32_uni b4_requ_uni = {{ 0x07, 0x30, 0x00, 0x00 }};
  4546. v32_uni b4_err0_uni = {{ 0x04, 0x00, 0x00, 0x00 }};
  4547. v32_uni b4_err1_uni = {{ 0x03, 0x30, 0x00, 0x00 }};
  4548. u32 b1_mask = b1_mask_uni.u;
  4549. u32 b1_patt = b1_patt_uni.u;
  4550. u32 b2_mask = b2_mask_uni.u;
  4551. u32 b2_patt = b2_patt_uni.u;
  4552. u32 b2_requ = b2_requ_uni.u;
  4553. u32 b3_mask = b3_mask_uni.u;
  4554. u32 b3_patt = b3_patt_uni.u;
  4555. u32 b3_requ = b3_requ_uni.u;
  4556. u32 b3_erro = b3_erro_uni.u;
  4557. u32 b4_mask = b4_mask_uni.u;
  4558. u32 b4_patt = b4_patt_uni.u;
  4559. u32 b4_requ = b4_requ_uni.u;
  4560. u32 b4_err0 = b4_err0_uni.u;
  4561. u32 b4_err1 = b4_err1_uni.u;
  4562. #endif
  4563. #define is_valid_seq_1(uni) ( \
  4564. ((uni & b1_mask) == b1_patt) \
  4565. )
  4566. #define is_valid_seq_2(uni) ( \
  4567. ((uni & b2_mask) == b2_patt) && \
  4568. ((uni & b2_requ)) \
  4569. )
  4570. #define is_valid_seq_3(uni) ( \
  4571. ((uni & b3_mask) == b3_patt) && \
  4572. ((tmp = (uni & b3_requ))) && \
  4573. ((tmp != b3_erro)) \
  4574. )
  4575. #define is_valid_seq_4(uni) ( \
  4576. ((uni & b4_mask) == b4_patt) && \
  4577. ((tmp = (uni & b4_requ))) && \
  4578. ((tmp & b4_err0) == 0 || (tmp & b4_err1) == 0) \
  4579. )
  4580. #define return_err(_end, _msg) do { \
  4581. *msg = _msg; \
  4582. *end = _end; \
  4583. return false; \
  4584. } while (false)
  4585. u8 *cur = *ptr;
  4586. u8 **end = ptr;
  4587. u8 *src = ++cur, *dst, *pos;
  4588. u16 hi, lo;
  4589. u32 uni, tmp;
  4590. skip_ascii:
  4591. /* Most strings have no escaped characters, so we can jump them quickly. */
  4592. skip_ascii_begin:
  4593. /*
  4594. We want to make loop unrolling, as shown in the following code. Some
  4595. compiler may not generate instructions as expected, so we rewrite it with
  4596. explicit goto statements. We hope the compiler can generate instructions
  4597. like this: https://godbolt.org/z/8vjsYq
  4598. while (true) repeat16({
  4599. if (likely(!(char_is_ascii_stop(*src)))) src++;
  4600. else break;
  4601. })
  4602. */
  4603. #define expr_jump(i) \
  4604. if (likely(!char_is_ascii_stop(src[i]))) {} \
  4605. else goto skip_ascii_stop##i;
  4606. #define expr_stop(i) \
  4607. skip_ascii_stop##i: \
  4608. src += i; \
  4609. goto skip_ascii_end;
  4610. repeat16_incr(expr_jump)
  4611. src += 16;
  4612. goto skip_ascii_begin;
  4613. repeat16_incr(expr_stop)
  4614. #undef expr_jump
  4615. #undef expr_stop
  4616. skip_ascii_end:
  4617. /*
  4618. GCC may store src[i] in a register at each line of expr_jump(i) above.
  4619. These instructions are useless and will degrade performance.
  4620. This inline asm is a hint for gcc: "the memory has been modified,
  4621. do not cache it".
  4622. MSVC, Clang, ICC can generate expected instructions without this hint.
  4623. */
  4624. #if YYJSON_IS_REAL_GCC
  4625. __asm__ volatile("":"=m"(*src));
  4626. #endif
  4627. if (likely(*src == '"')) {
  4628. val->tag = ((u64)(src - cur) << YYJSON_TAG_BIT) |
  4629. (u64)(YYJSON_TYPE_STR | YYJSON_SUBTYPE_NOESC);
  4630. val->uni.str = (const char *)cur;
  4631. *src = '\0';
  4632. *end = src + 1;
  4633. return true;
  4634. }
  4635. skip_utf8:
  4636. if (*src & 0x80) { /* non-ASCII character */
  4637. /*
  4638. Non-ASCII character appears here, which means that the text is likely
  4639. to be written in non-English or emoticons. According to some common
  4640. data set statistics, byte sequences of the same length may appear
  4641. consecutively. We process the byte sequences of the same length in each
  4642. loop, which is more friendly to branch prediction.
  4643. */
  4644. pos = src;
  4645. #if YYJSON_DISABLE_UTF8_VALIDATION
  4646. while (true) repeat8({
  4647. if (likely((*src & 0xF0) == 0xE0)) src += 3;
  4648. else break;
  4649. })
  4650. if (*src < 0x80) goto skip_ascii;
  4651. while (true) repeat8({
  4652. if (likely((*src & 0xE0) == 0xC0)) src += 2;
  4653. else break;
  4654. })
  4655. while (true) repeat8({
  4656. if (likely((*src & 0xF8) == 0xF0)) src += 4;
  4657. else break;
  4658. })
  4659. #else
  4660. uni = byte_load_4(src);
  4661. while (is_valid_seq_3(uni)) {
  4662. src += 3;
  4663. uni = byte_load_4(src);
  4664. }
  4665. if (is_valid_seq_1(uni)) goto skip_ascii;
  4666. while (is_valid_seq_2(uni)) {
  4667. src += 2;
  4668. uni = byte_load_4(src);
  4669. }
  4670. while (is_valid_seq_4(uni)) {
  4671. src += 4;
  4672. uni = byte_load_4(src);
  4673. }
  4674. #endif
  4675. if (unlikely(pos == src)) {
  4676. if (!inv) return_err(src, "invalid UTF-8 encoding in string");
  4677. ++src;
  4678. }
  4679. goto skip_ascii;
  4680. }
  4681. /* The escape character appears, we need to copy it. */
  4682. dst = src;
  4683. copy_escape:
  4684. if (likely(*src == '\\')) {
  4685. switch (*++src) {
  4686. case '"': *dst++ = '"'; src++; break;
  4687. case '\\': *dst++ = '\\'; src++; break;
  4688. case '/': *dst++ = '/'; src++; break;
  4689. case 'b': *dst++ = '\b'; src++; break;
  4690. case 'f': *dst++ = '\f'; src++; break;
  4691. case 'n': *dst++ = '\n'; src++; break;
  4692. case 'r': *dst++ = '\r'; src++; break;
  4693. case 't': *dst++ = '\t'; src++; break;
  4694. case 'u':
  4695. if (unlikely(!read_hex_u16(++src, &hi))) {
  4696. return_err(src - 2, "invalid escaped sequence in string");
  4697. }
  4698. src += 4;
  4699. if (likely((hi & 0xF800) != 0xD800)) {
  4700. /* a BMP character */
  4701. if (hi >= 0x800) {
  4702. *dst++ = (u8)(0xE0 | (hi >> 12));
  4703. *dst++ = (u8)(0x80 | ((hi >> 6) & 0x3F));
  4704. *dst++ = (u8)(0x80 | (hi & 0x3F));
  4705. } else if (hi >= 0x80) {
  4706. *dst++ = (u8)(0xC0 | (hi >> 6));
  4707. *dst++ = (u8)(0x80 | (hi & 0x3F));
  4708. } else {
  4709. *dst++ = (u8)hi;
  4710. }
  4711. } else {
  4712. /* a non-BMP character, represented as a surrogate pair */
  4713. if (unlikely((hi & 0xFC00) != 0xD800)) {
  4714. return_err(src - 6, "invalid high surrogate in string");
  4715. }
  4716. if (unlikely(!byte_match_2(src, "\\u"))) {
  4717. return_err(src, "no low surrogate in string");
  4718. }
  4719. if (unlikely(!read_hex_u16(src + 2, &lo))) {
  4720. return_err(src, "invalid escaped sequence in string");
  4721. }
  4722. if (unlikely((lo & 0xFC00) != 0xDC00)) {
  4723. return_err(src, "invalid low surrogate in string");
  4724. }
  4725. uni = ((((u32)hi - 0xD800) << 10) |
  4726. ((u32)lo - 0xDC00)) + 0x10000;
  4727. *dst++ = (u8)(0xF0 | (uni >> 18));
  4728. *dst++ = (u8)(0x80 | ((uni >> 12) & 0x3F));
  4729. *dst++ = (u8)(0x80 | ((uni >> 6) & 0x3F));
  4730. *dst++ = (u8)(0x80 | (uni & 0x3F));
  4731. src += 6;
  4732. }
  4733. break;
  4734. default: return_err(src, "invalid escaped character in string");
  4735. }
  4736. } else if (likely(*src == '"')) {
  4737. val->tag = ((u64)(dst - cur) << YYJSON_TAG_BIT) | YYJSON_TYPE_STR;
  4738. val->uni.str = (const char *)cur;
  4739. *dst = '\0';
  4740. *end = src + 1;
  4741. return true;
  4742. } else {
  4743. if (!inv) return_err(src, "unexpected control character in string");
  4744. if (src >= lst) return_err(src, "unclosed string");
  4745. *dst++ = *src++;
  4746. }
  4747. copy_ascii:
  4748. /*
  4749. Copy continuous ASCII, loop unrolling, same as the following code:
  4750. while (true) repeat16({
  4751. if (unlikely(char_is_ascii_stop(*src))) break;
  4752. *dst++ = *src++;
  4753. })
  4754. */
  4755. #if YYJSON_IS_REAL_GCC
  4756. # define expr_jump(i) \
  4757. if (likely(!(char_is_ascii_stop(src[i])))) {} \
  4758. else { __asm__ volatile("":"=m"(src[i])); goto copy_ascii_stop_##i; }
  4759. #else
  4760. # define expr_jump(i) \
  4761. if (likely(!(char_is_ascii_stop(src[i])))) {} \
  4762. else { goto copy_ascii_stop_##i; }
  4763. #endif
  4764. repeat16_incr(expr_jump)
  4765. #undef expr_jump
  4766. byte_move_16(dst, src);
  4767. src += 16;
  4768. dst += 16;
  4769. goto copy_ascii;
  4770. /*
  4771. The memory will be moved forward by at least 1 byte. So the `byte_move`
  4772. can be one byte more than needed to reduce the number of instructions.
  4773. */
  4774. copy_ascii_stop_0:
  4775. goto copy_utf8;
  4776. copy_ascii_stop_1:
  4777. byte_move_2(dst, src);
  4778. src += 1;
  4779. dst += 1;
  4780. goto copy_utf8;
  4781. copy_ascii_stop_2:
  4782. byte_move_2(dst, src);
  4783. src += 2;
  4784. dst += 2;
  4785. goto copy_utf8;
  4786. copy_ascii_stop_3:
  4787. byte_move_4(dst, src);
  4788. src += 3;
  4789. dst += 3;
  4790. goto copy_utf8;
  4791. copy_ascii_stop_4:
  4792. byte_move_4(dst, src);
  4793. src += 4;
  4794. dst += 4;
  4795. goto copy_utf8;
  4796. copy_ascii_stop_5:
  4797. byte_move_4(dst, src);
  4798. byte_move_2(dst + 4, src + 4);
  4799. src += 5;
  4800. dst += 5;
  4801. goto copy_utf8;
  4802. copy_ascii_stop_6:
  4803. byte_move_4(dst, src);
  4804. byte_move_2(dst + 4, src + 4);
  4805. src += 6;
  4806. dst += 6;
  4807. goto copy_utf8;
  4808. copy_ascii_stop_7:
  4809. byte_move_8(dst, src);
  4810. src += 7;
  4811. dst += 7;
  4812. goto copy_utf8;
  4813. copy_ascii_stop_8:
  4814. byte_move_8(dst, src);
  4815. src += 8;
  4816. dst += 8;
  4817. goto copy_utf8;
  4818. copy_ascii_stop_9:
  4819. byte_move_8(dst, src);
  4820. byte_move_2(dst + 8, src + 8);
  4821. src += 9;
  4822. dst += 9;
  4823. goto copy_utf8;
  4824. copy_ascii_stop_10:
  4825. byte_move_8(dst, src);
  4826. byte_move_2(dst + 8, src + 8);
  4827. src += 10;
  4828. dst += 10;
  4829. goto copy_utf8;
  4830. copy_ascii_stop_11:
  4831. byte_move_8(dst, src);
  4832. byte_move_4(dst + 8, src + 8);
  4833. src += 11;
  4834. dst += 11;
  4835. goto copy_utf8;
  4836. copy_ascii_stop_12:
  4837. byte_move_8(dst, src);
  4838. byte_move_4(dst + 8, src + 8);
  4839. src += 12;
  4840. dst += 12;
  4841. goto copy_utf8;
  4842. copy_ascii_stop_13:
  4843. byte_move_8(dst, src);
  4844. byte_move_4(dst + 8, src + 8);
  4845. byte_move_2(dst + 12, src + 12);
  4846. src += 13;
  4847. dst += 13;
  4848. goto copy_utf8;
  4849. copy_ascii_stop_14:
  4850. byte_move_8(dst, src);
  4851. byte_move_4(dst + 8, src + 8);
  4852. byte_move_2(dst + 12, src + 12);
  4853. src += 14;
  4854. dst += 14;
  4855. goto copy_utf8;
  4856. copy_ascii_stop_15:
  4857. byte_move_16(dst, src);
  4858. src += 15;
  4859. dst += 15;
  4860. goto copy_utf8;
  4861. copy_utf8:
  4862. if (*src & 0x80) { /* non-ASCII character */
  4863. pos = src;
  4864. uni = byte_load_4(src);
  4865. #if YYJSON_DISABLE_UTF8_VALIDATION
  4866. while (true) repeat4({
  4867. if ((uni & b3_mask) == b3_patt) {
  4868. byte_copy_4(dst, &uni);
  4869. dst += 3;
  4870. src += 3;
  4871. uni = byte_load_4(src);
  4872. } else break;
  4873. })
  4874. if ((uni & b1_mask) == b1_patt) goto copy_ascii;
  4875. while (true) repeat4({
  4876. if ((uni & b2_mask) == b2_patt) {
  4877. byte_copy_2(dst, &uni);
  4878. dst += 2;
  4879. src += 2;
  4880. uni = byte_load_4(src);
  4881. } else break;
  4882. })
  4883. while (true) repeat4({
  4884. if ((uni & b4_mask) == b4_patt) {
  4885. byte_copy_4(dst, &uni);
  4886. dst += 4;
  4887. src += 4;
  4888. uni = byte_load_4(src);
  4889. } else break;
  4890. })
  4891. #else
  4892. while (is_valid_seq_3(uni)) {
  4893. byte_copy_4(dst, &uni);
  4894. dst += 3;
  4895. src += 3;
  4896. uni = byte_load_4(src);
  4897. }
  4898. if (is_valid_seq_1(uni)) goto copy_ascii;
  4899. while (is_valid_seq_2(uni)) {
  4900. byte_copy_2(dst, &uni);
  4901. dst += 2;
  4902. src += 2;
  4903. uni = byte_load_4(src);
  4904. }
  4905. while (is_valid_seq_4(uni)) {
  4906. byte_copy_4(dst, &uni);
  4907. dst += 4;
  4908. src += 4;
  4909. uni = byte_load_4(src);
  4910. }
  4911. #endif
  4912. if (unlikely(pos == src)) {
  4913. if (!inv) return_err(src, "invalid UTF-8 encoding in string");
  4914. goto copy_ascii_stop_1;
  4915. }
  4916. goto copy_ascii;
  4917. }
  4918. goto copy_escape;
  4919. #undef return_err
  4920. #undef is_valid_seq_1
  4921. #undef is_valid_seq_2
  4922. #undef is_valid_seq_3
  4923. #undef is_valid_seq_4
  4924. }
  4925. /*==============================================================================
  4926. * JSON Reader Implementation
  4927. *
  4928. * We use goto statements to build the finite state machine (FSM).
  4929. * The FSM's state was held by program counter (PC) and the 'goto' make the
  4930. * state transitions.
  4931. *============================================================================*/
  4932. /** Read single value JSON document. */
  4933. static_noinline yyjson_doc *read_root_single(u8 *hdr,
  4934. u8 *cur,
  4935. u8 *end,
  4936. yyjson_alc alc,
  4937. yyjson_read_flag flg,
  4938. yyjson_read_err *err) {
  4939. #define return_err(_pos, _code, _msg) do { \
  4940. if (is_truncated_end(hdr, _pos, end, YYJSON_READ_ERROR_##_code, flg)) { \
  4941. err->pos = (usize)(end - hdr); \
  4942. err->code = YYJSON_READ_ERROR_UNEXPECTED_END; \
  4943. err->msg = "unexpected end of data"; \
  4944. } else { \
  4945. err->pos = (usize)(_pos - hdr); \
  4946. err->code = YYJSON_READ_ERROR_##_code; \
  4947. err->msg = _msg; \
  4948. } \
  4949. if (val_hdr) alc.free(alc.ctx, (void *)val_hdr); \
  4950. return NULL; \
  4951. } while (false)
  4952. usize hdr_len; /* value count used by doc */
  4953. usize alc_num; /* value count capacity */
  4954. yyjson_val *val_hdr; /* the head of allocated values */
  4955. yyjson_val *val; /* current value */
  4956. yyjson_doc *doc; /* the JSON document, equals to val_hdr */
  4957. const char *msg; /* error message */
  4958. bool raw; /* read number as raw */
  4959. bool inv; /* allow invalid unicode */
  4960. u8 *raw_end; /* raw end for null-terminator */
  4961. u8 **pre; /* previous raw end pointer */
  4962. hdr_len = sizeof(yyjson_doc) / sizeof(yyjson_val);
  4963. hdr_len += (sizeof(yyjson_doc) % sizeof(yyjson_val)) > 0;
  4964. alc_num = hdr_len + 1; /* single value */
  4965. val_hdr = (yyjson_val *)alc.malloc(alc.ctx, alc_num * sizeof(yyjson_val));
  4966. if (unlikely(!val_hdr)) goto fail_alloc;
  4967. val = val_hdr + hdr_len;
  4968. raw = has_read_flag(NUMBER_AS_RAW) || has_read_flag(BIGNUM_AS_RAW);
  4969. inv = has_read_flag(ALLOW_INVALID_UNICODE) != 0;
  4970. raw_end = NULL;
  4971. pre = raw ? &raw_end : NULL;
  4972. if (char_is_number(*cur)) {
  4973. if (likely(read_number(&cur, pre, flg, val, &msg))) goto doc_end;
  4974. goto fail_number;
  4975. }
  4976. if (*cur == '"') {
  4977. if (likely(read_string(&cur, end, inv, val, &msg))) goto doc_end;
  4978. goto fail_string;
  4979. }
  4980. if (*cur == 't') {
  4981. if (likely(read_true(&cur, val))) goto doc_end;
  4982. goto fail_literal;
  4983. }
  4984. if (*cur == 'f') {
  4985. if (likely(read_false(&cur, val))) goto doc_end;
  4986. goto fail_literal;
  4987. }
  4988. if (*cur == 'n') {
  4989. if (likely(read_null(&cur, val))) goto doc_end;
  4990. if (has_read_flag(ALLOW_INF_AND_NAN)) {
  4991. if (read_nan(false, &cur, pre, val)) goto doc_end;
  4992. }
  4993. goto fail_literal;
  4994. }
  4995. if (has_read_flag(ALLOW_INF_AND_NAN)) {
  4996. if (read_inf_or_nan(false, &cur, pre, val)) goto doc_end;
  4997. }
  4998. goto fail_character;
  4999. doc_end:
  5000. /* check invalid contents after json document */
  5001. if (unlikely(cur < end) && !has_read_flag(STOP_WHEN_DONE)) {
  5002. if (has_read_flag(ALLOW_COMMENTS)) {
  5003. if (!skip_spaces_and_comments(&cur)) {
  5004. if (byte_match_2(cur, "/*")) goto fail_comment;
  5005. }
  5006. } else {
  5007. while (char_is_space(*cur)) cur++;
  5008. }
  5009. if (unlikely(cur < end)) goto fail_garbage;
  5010. }
  5011. if (pre && *pre) **pre = '\0';
  5012. doc = (yyjson_doc *)val_hdr;
  5013. doc->root = val_hdr + hdr_len;
  5014. doc->alc = alc;
  5015. doc->dat_read = (usize)(cur - hdr);
  5016. doc->val_read = 1;
  5017. doc->str_pool = has_read_flag(INSITU) ? NULL : (char *)hdr;
  5018. return doc;
  5019. fail_string:
  5020. return_err(cur, INVALID_STRING, msg);
  5021. fail_number:
  5022. return_err(cur, INVALID_NUMBER, msg);
  5023. fail_alloc:
  5024. return_err(cur, MEMORY_ALLOCATION, "memory allocation failed");
  5025. fail_literal:
  5026. return_err(cur, LITERAL, "invalid literal");
  5027. fail_comment:
  5028. return_err(cur, INVALID_COMMENT, "unclosed multiline comment");
  5029. fail_character:
  5030. return_err(cur, UNEXPECTED_CHARACTER, "unexpected character");
  5031. fail_garbage:
  5032. return_err(cur, UNEXPECTED_CONTENT, "unexpected content after document");
  5033. #undef return_err
  5034. }
  5035. /** Read JSON document (accept all style, but optimized for minify). */
  5036. static_inline yyjson_doc *read_root_minify(u8 *hdr,
  5037. u8 *cur,
  5038. u8 *end,
  5039. yyjson_alc alc,
  5040. yyjson_read_flag flg,
  5041. yyjson_read_err *err) {
  5042. #define return_err(_pos, _code, _msg) do { \
  5043. if (is_truncated_end(hdr, _pos, end, YYJSON_READ_ERROR_##_code, flg)) { \
  5044. err->pos = (usize)(end - hdr); \
  5045. err->code = YYJSON_READ_ERROR_UNEXPECTED_END; \
  5046. err->msg = "unexpected end of data"; \
  5047. } else { \
  5048. err->pos = (usize)(_pos - hdr); \
  5049. err->code = YYJSON_READ_ERROR_##_code; \
  5050. err->msg = _msg; \
  5051. } \
  5052. if (val_hdr) alc.free(alc.ctx, (void *)val_hdr); \
  5053. return NULL; \
  5054. } while (false)
  5055. #define val_incr() do { \
  5056. val++; \
  5057. if (unlikely(val >= val_end)) { \
  5058. usize alc_old = alc_len; \
  5059. alc_len += alc_len / 2; \
  5060. if ((sizeof(usize) < 8) && (alc_len >= alc_max)) goto fail_alloc; \
  5061. val_tmp = (yyjson_val *)alc.realloc(alc.ctx, (void *)val_hdr, \
  5062. alc_old * sizeof(yyjson_val), \
  5063. alc_len * sizeof(yyjson_val)); \
  5064. if ((!val_tmp)) goto fail_alloc; \
  5065. val = val_tmp + (usize)(val - val_hdr); \
  5066. ctn = val_tmp + (usize)(ctn - val_hdr); \
  5067. val_hdr = val_tmp; \
  5068. val_end = val_tmp + (alc_len - 2); \
  5069. } \
  5070. } while (false)
  5071. usize dat_len; /* data length in bytes, hint for allocator */
  5072. usize hdr_len; /* value count used by yyjson_doc */
  5073. usize alc_len; /* value count allocated */
  5074. usize alc_max; /* maximum value count for allocator */
  5075. usize ctn_len; /* the number of elements in current container */
  5076. yyjson_val *val_hdr; /* the head of allocated values */
  5077. yyjson_val *val_end; /* the end of allocated values */
  5078. yyjson_val *val_tmp; /* temporary pointer for realloc */
  5079. yyjson_val *val; /* current JSON value */
  5080. yyjson_val *ctn; /* current container */
  5081. yyjson_val *ctn_parent; /* parent of current container */
  5082. yyjson_doc *doc; /* the JSON document, equals to val_hdr */
  5083. const char *msg; /* error message */
  5084. bool raw; /* read number as raw */
  5085. bool inv; /* allow invalid unicode */
  5086. u8 *raw_end; /* raw end for null-terminator */
  5087. u8 **pre; /* previous raw end pointer */
  5088. dat_len = has_read_flag(STOP_WHEN_DONE) ? 256 : (usize)(end - cur);
  5089. hdr_len = sizeof(yyjson_doc) / sizeof(yyjson_val);
  5090. hdr_len += (sizeof(yyjson_doc) % sizeof(yyjson_val)) > 0;
  5091. alc_max = USIZE_MAX / sizeof(yyjson_val);
  5092. alc_len = hdr_len + (dat_len / YYJSON_READER_ESTIMATED_MINIFY_RATIO) + 4;
  5093. alc_len = yyjson_min(alc_len, alc_max);
  5094. val_hdr = (yyjson_val *)alc.malloc(alc.ctx, alc_len * sizeof(yyjson_val));
  5095. if (unlikely(!val_hdr)) goto fail_alloc;
  5096. val_end = val_hdr + (alc_len - 2); /* padding for key-value pair reading */
  5097. val = val_hdr + hdr_len;
  5098. ctn = val;
  5099. ctn_len = 0;
  5100. raw = has_read_flag(NUMBER_AS_RAW) || has_read_flag(BIGNUM_AS_RAW);
  5101. inv = has_read_flag(ALLOW_INVALID_UNICODE) != 0;
  5102. raw_end = NULL;
  5103. pre = raw ? &raw_end : NULL;
  5104. if (*cur++ == '{') {
  5105. ctn->tag = YYJSON_TYPE_OBJ;
  5106. ctn->uni.ofs = 0;
  5107. goto obj_key_begin;
  5108. } else {
  5109. ctn->tag = YYJSON_TYPE_ARR;
  5110. ctn->uni.ofs = 0;
  5111. goto arr_val_begin;
  5112. }
  5113. arr_begin:
  5114. /* save current container */
  5115. ctn->tag = (((u64)ctn_len + 1) << YYJSON_TAG_BIT) |
  5116. (ctn->tag & YYJSON_TAG_MASK);
  5117. /* create a new array value, save parent container offset */
  5118. val_incr();
  5119. val->tag = YYJSON_TYPE_ARR;
  5120. val->uni.ofs = (usize)((u8 *)val - (u8 *)ctn);
  5121. /* push the new array value as current container */
  5122. ctn = val;
  5123. ctn_len = 0;
  5124. arr_val_begin:
  5125. if (*cur == '{') {
  5126. cur++;
  5127. goto obj_begin;
  5128. }
  5129. if (*cur == '[') {
  5130. cur++;
  5131. goto arr_begin;
  5132. }
  5133. if (char_is_number(*cur)) {
  5134. val_incr();
  5135. ctn_len++;
  5136. if (likely(read_number(&cur, pre, flg, val, &msg))) goto arr_val_end;
  5137. goto fail_number;
  5138. }
  5139. if (*cur == '"') {
  5140. val_incr();
  5141. ctn_len++;
  5142. if (likely(read_string(&cur, end, inv, val, &msg))) goto arr_val_end;
  5143. goto fail_string;
  5144. }
  5145. if (*cur == 't') {
  5146. val_incr();
  5147. ctn_len++;
  5148. if (likely(read_true(&cur, val))) goto arr_val_end;
  5149. goto fail_literal;
  5150. }
  5151. if (*cur == 'f') {
  5152. val_incr();
  5153. ctn_len++;
  5154. if (likely(read_false(&cur, val))) goto arr_val_end;
  5155. goto fail_literal;
  5156. }
  5157. if (*cur == 'n') {
  5158. val_incr();
  5159. ctn_len++;
  5160. if (likely(read_null(&cur, val))) goto arr_val_end;
  5161. if (has_read_flag(ALLOW_INF_AND_NAN)) {
  5162. if (read_nan(false, &cur, pre, val)) goto arr_val_end;
  5163. }
  5164. goto fail_literal;
  5165. }
  5166. if (*cur == ']') {
  5167. cur++;
  5168. if (likely(ctn_len == 0)) goto arr_end;
  5169. if (has_read_flag(ALLOW_TRAILING_COMMAS)) goto arr_end;
  5170. while (*cur != ',') cur--;
  5171. goto fail_trailing_comma;
  5172. }
  5173. if (char_is_space(*cur)) {
  5174. while (char_is_space(*++cur));
  5175. goto arr_val_begin;
  5176. }
  5177. if (has_read_flag(ALLOW_INF_AND_NAN) &&
  5178. (*cur == 'i' || *cur == 'I' || *cur == 'N')) {
  5179. val_incr();
  5180. ctn_len++;
  5181. if (read_inf_or_nan(false, &cur, pre, val)) goto arr_val_end;
  5182. goto fail_character;
  5183. }
  5184. if (has_read_flag(ALLOW_COMMENTS)) {
  5185. if (skip_spaces_and_comments(&cur)) goto arr_val_begin;
  5186. if (byte_match_2(cur, "/*")) goto fail_comment;
  5187. }
  5188. goto fail_character;
  5189. arr_val_end:
  5190. if (*cur == ',') {
  5191. cur++;
  5192. goto arr_val_begin;
  5193. }
  5194. if (*cur == ']') {
  5195. cur++;
  5196. goto arr_end;
  5197. }
  5198. if (char_is_space(*cur)) {
  5199. while (char_is_space(*++cur));
  5200. goto arr_val_end;
  5201. }
  5202. if (has_read_flag(ALLOW_COMMENTS)) {
  5203. if (skip_spaces_and_comments(&cur)) goto arr_val_end;
  5204. if (byte_match_2(cur, "/*")) goto fail_comment;
  5205. }
  5206. goto fail_character;
  5207. arr_end:
  5208. /* get parent container */
  5209. ctn_parent = (yyjson_val *)(void *)((u8 *)ctn - ctn->uni.ofs);
  5210. /* save the next sibling value offset */
  5211. ctn->uni.ofs = (usize)((u8 *)val - (u8 *)ctn) + sizeof(yyjson_val);
  5212. ctn->tag = ((ctn_len) << YYJSON_TAG_BIT) | YYJSON_TYPE_ARR;
  5213. if (unlikely(ctn == ctn_parent)) goto doc_end;
  5214. /* pop parent as current container */
  5215. ctn = ctn_parent;
  5216. ctn_len = (usize)(ctn->tag >> YYJSON_TAG_BIT);
  5217. if ((ctn->tag & YYJSON_TYPE_MASK) == YYJSON_TYPE_OBJ) {
  5218. goto obj_val_end;
  5219. } else {
  5220. goto arr_val_end;
  5221. }
  5222. obj_begin:
  5223. /* push container */
  5224. ctn->tag = (((u64)ctn_len + 1) << YYJSON_TAG_BIT) |
  5225. (ctn->tag & YYJSON_TAG_MASK);
  5226. val_incr();
  5227. val->tag = YYJSON_TYPE_OBJ;
  5228. /* offset to the parent */
  5229. val->uni.ofs = (usize)((u8 *)val - (u8 *)ctn);
  5230. ctn = val;
  5231. ctn_len = 0;
  5232. obj_key_begin:
  5233. if (likely(*cur == '"')) {
  5234. val_incr();
  5235. ctn_len++;
  5236. if (likely(read_string(&cur, end, inv, val, &msg))) goto obj_key_end;
  5237. goto fail_string;
  5238. }
  5239. if (likely(*cur == '}')) {
  5240. cur++;
  5241. if (likely(ctn_len == 0)) goto obj_end;
  5242. if (has_read_flag(ALLOW_TRAILING_COMMAS)) goto obj_end;
  5243. while (*cur != ',') cur--;
  5244. goto fail_trailing_comma;
  5245. }
  5246. if (char_is_space(*cur)) {
  5247. while (char_is_space(*++cur));
  5248. goto obj_key_begin;
  5249. }
  5250. if (has_read_flag(ALLOW_COMMENTS)) {
  5251. if (skip_spaces_and_comments(&cur)) goto obj_key_begin;
  5252. if (byte_match_2(cur, "/*")) goto fail_comment;
  5253. }
  5254. goto fail_character;
  5255. obj_key_end:
  5256. if (*cur == ':') {
  5257. cur++;
  5258. goto obj_val_begin;
  5259. }
  5260. if (char_is_space(*cur)) {
  5261. while (char_is_space(*++cur));
  5262. goto obj_key_end;
  5263. }
  5264. if (has_read_flag(ALLOW_COMMENTS)) {
  5265. if (skip_spaces_and_comments(&cur)) goto obj_key_end;
  5266. if (byte_match_2(cur, "/*")) goto fail_comment;
  5267. }
  5268. goto fail_character;
  5269. obj_val_begin:
  5270. if (*cur == '"') {
  5271. val++;
  5272. ctn_len++;
  5273. if (likely(read_string(&cur, end, inv, val, &msg))) goto obj_val_end;
  5274. goto fail_string;
  5275. }
  5276. if (char_is_number(*cur)) {
  5277. val++;
  5278. ctn_len++;
  5279. if (likely(read_number(&cur, pre, flg, val, &msg))) goto obj_val_end;
  5280. goto fail_number;
  5281. }
  5282. if (*cur == '{') {
  5283. cur++;
  5284. goto obj_begin;
  5285. }
  5286. if (*cur == '[') {
  5287. cur++;
  5288. goto arr_begin;
  5289. }
  5290. if (*cur == 't') {
  5291. val++;
  5292. ctn_len++;
  5293. if (likely(read_true(&cur, val))) goto obj_val_end;
  5294. goto fail_literal;
  5295. }
  5296. if (*cur == 'f') {
  5297. val++;
  5298. ctn_len++;
  5299. if (likely(read_false(&cur, val))) goto obj_val_end;
  5300. goto fail_literal;
  5301. }
  5302. if (*cur == 'n') {
  5303. val++;
  5304. ctn_len++;
  5305. if (likely(read_null(&cur, val))) goto obj_val_end;
  5306. if (has_read_flag(ALLOW_INF_AND_NAN)) {
  5307. if (read_nan(false, &cur, pre, val)) goto obj_val_end;
  5308. }
  5309. goto fail_literal;
  5310. }
  5311. if (char_is_space(*cur)) {
  5312. while (char_is_space(*++cur));
  5313. goto obj_val_begin;
  5314. }
  5315. if (has_read_flag(ALLOW_INF_AND_NAN) &&
  5316. (*cur == 'i' || *cur == 'I' || *cur == 'N')) {
  5317. val++;
  5318. ctn_len++;
  5319. if (read_inf_or_nan(false, &cur, pre, val)) goto obj_val_end;
  5320. goto fail_character;
  5321. }
  5322. if (has_read_flag(ALLOW_COMMENTS)) {
  5323. if (skip_spaces_and_comments(&cur)) goto obj_val_begin;
  5324. if (byte_match_2(cur, "/*")) goto fail_comment;
  5325. }
  5326. goto fail_character;
  5327. obj_val_end:
  5328. if (likely(*cur == ',')) {
  5329. cur++;
  5330. goto obj_key_begin;
  5331. }
  5332. if (likely(*cur == '}')) {
  5333. cur++;
  5334. goto obj_end;
  5335. }
  5336. if (char_is_space(*cur)) {
  5337. while (char_is_space(*++cur));
  5338. goto obj_val_end;
  5339. }
  5340. if (has_read_flag(ALLOW_COMMENTS)) {
  5341. if (skip_spaces_and_comments(&cur)) goto obj_val_end;
  5342. if (byte_match_2(cur, "/*")) goto fail_comment;
  5343. }
  5344. goto fail_character;
  5345. obj_end:
  5346. /* pop container */
  5347. ctn_parent = (yyjson_val *)(void *)((u8 *)ctn - ctn->uni.ofs);
  5348. /* point to the next value */
  5349. ctn->uni.ofs = (usize)((u8 *)val - (u8 *)ctn) + sizeof(yyjson_val);
  5350. ctn->tag = (ctn_len << (YYJSON_TAG_BIT - 1)) | YYJSON_TYPE_OBJ;
  5351. if (unlikely(ctn == ctn_parent)) goto doc_end;
  5352. ctn = ctn_parent;
  5353. ctn_len = (usize)(ctn->tag >> YYJSON_TAG_BIT);
  5354. if ((ctn->tag & YYJSON_TYPE_MASK) == YYJSON_TYPE_OBJ) {
  5355. goto obj_val_end;
  5356. } else {
  5357. goto arr_val_end;
  5358. }
  5359. doc_end:
  5360. /* check invalid contents after json document */
  5361. if (unlikely(cur < end) && !has_read_flag(STOP_WHEN_DONE)) {
  5362. if (has_read_flag(ALLOW_COMMENTS)) {
  5363. skip_spaces_and_comments(&cur);
  5364. if (byte_match_2(cur, "/*")) goto fail_comment;
  5365. } else {
  5366. while (char_is_space(*cur)) cur++;
  5367. }
  5368. if (unlikely(cur < end)) goto fail_garbage;
  5369. }
  5370. if (pre && *pre) **pre = '\0';
  5371. doc = (yyjson_doc *)val_hdr;
  5372. doc->root = val_hdr + hdr_len;
  5373. doc->alc = alc;
  5374. doc->dat_read = (usize)(cur - hdr);
  5375. doc->val_read = (usize)((val - doc->root) + 1);
  5376. doc->str_pool = has_read_flag(INSITU) ? NULL : (char *)hdr;
  5377. return doc;
  5378. fail_string:
  5379. return_err(cur, INVALID_STRING, msg);
  5380. fail_number:
  5381. return_err(cur, INVALID_NUMBER, msg);
  5382. fail_alloc:
  5383. return_err(cur, MEMORY_ALLOCATION, "memory allocation failed");
  5384. fail_trailing_comma:
  5385. return_err(cur, JSON_STRUCTURE, "trailing comma is not allowed");
  5386. fail_literal:
  5387. return_err(cur, LITERAL, "invalid literal");
  5388. fail_comment:
  5389. return_err(cur, INVALID_COMMENT, "unclosed multiline comment");
  5390. fail_character:
  5391. return_err(cur, UNEXPECTED_CHARACTER, "unexpected character");
  5392. fail_garbage:
  5393. return_err(cur, UNEXPECTED_CONTENT, "unexpected content after document");
  5394. #undef val_incr
  5395. #undef return_err
  5396. }
  5397. /** Read JSON document (accept all style, but optimized for pretty). */
  5398. static_inline yyjson_doc *read_root_pretty(u8 *hdr,
  5399. u8 *cur,
  5400. u8 *end,
  5401. yyjson_alc alc,
  5402. yyjson_read_flag flg,
  5403. yyjson_read_err *err) {
  5404. #define return_err(_pos, _code, _msg) do { \
  5405. if (is_truncated_end(hdr, _pos, end, YYJSON_READ_ERROR_##_code, flg)) { \
  5406. err->pos = (usize)(end - hdr); \
  5407. err->code = YYJSON_READ_ERROR_UNEXPECTED_END; \
  5408. err->msg = "unexpected end of data"; \
  5409. } else { \
  5410. err->pos = (usize)(_pos - hdr); \
  5411. err->code = YYJSON_READ_ERROR_##_code; \
  5412. err->msg = _msg; \
  5413. } \
  5414. if (val_hdr) alc.free(alc.ctx, (void *)val_hdr); \
  5415. return NULL; \
  5416. } while (false)
  5417. #define val_incr() do { \
  5418. val++; \
  5419. if (unlikely(val >= val_end)) { \
  5420. usize alc_old = alc_len; \
  5421. alc_len += alc_len / 2; \
  5422. if ((sizeof(usize) < 8) && (alc_len >= alc_max)) goto fail_alloc; \
  5423. val_tmp = (yyjson_val *)alc.realloc(alc.ctx, (void *)val_hdr, \
  5424. alc_old * sizeof(yyjson_val), \
  5425. alc_len * sizeof(yyjson_val)); \
  5426. if ((!val_tmp)) goto fail_alloc; \
  5427. val = val_tmp + (usize)(val - val_hdr); \
  5428. ctn = val_tmp + (usize)(ctn - val_hdr); \
  5429. val_hdr = val_tmp; \
  5430. val_end = val_tmp + (alc_len - 2); \
  5431. } \
  5432. } while (false)
  5433. usize dat_len; /* data length in bytes, hint for allocator */
  5434. usize hdr_len; /* value count used by yyjson_doc */
  5435. usize alc_len; /* value count allocated */
  5436. usize alc_max; /* maximum value count for allocator */
  5437. usize ctn_len; /* the number of elements in current container */
  5438. yyjson_val *val_hdr; /* the head of allocated values */
  5439. yyjson_val *val_end; /* the end of allocated values */
  5440. yyjson_val *val_tmp; /* temporary pointer for realloc */
  5441. yyjson_val *val; /* current JSON value */
  5442. yyjson_val *ctn; /* current container */
  5443. yyjson_val *ctn_parent; /* parent of current container */
  5444. yyjson_doc *doc; /* the JSON document, equals to val_hdr */
  5445. const char *msg; /* error message */
  5446. bool raw; /* read number as raw */
  5447. bool inv; /* allow invalid unicode */
  5448. u8 *raw_end; /* raw end for null-terminator */
  5449. u8 **pre; /* previous raw end pointer */
  5450. dat_len = has_read_flag(STOP_WHEN_DONE) ? 256 : (usize)(end - cur);
  5451. hdr_len = sizeof(yyjson_doc) / sizeof(yyjson_val);
  5452. hdr_len += (sizeof(yyjson_doc) % sizeof(yyjson_val)) > 0;
  5453. alc_max = USIZE_MAX / sizeof(yyjson_val);
  5454. alc_len = hdr_len + (dat_len / YYJSON_READER_ESTIMATED_PRETTY_RATIO) + 4;
  5455. alc_len = yyjson_min(alc_len, alc_max);
  5456. val_hdr = (yyjson_val *)alc.malloc(alc.ctx, alc_len * sizeof(yyjson_val));
  5457. if (unlikely(!val_hdr)) goto fail_alloc;
  5458. val_end = val_hdr + (alc_len - 2); /* padding for key-value pair reading */
  5459. val = val_hdr + hdr_len;
  5460. ctn = val;
  5461. ctn_len = 0;
  5462. raw = has_read_flag(NUMBER_AS_RAW) || has_read_flag(BIGNUM_AS_RAW);
  5463. inv = has_read_flag(ALLOW_INVALID_UNICODE) != 0;
  5464. raw_end = NULL;
  5465. pre = raw ? &raw_end : NULL;
  5466. if (*cur++ == '{') {
  5467. ctn->tag = YYJSON_TYPE_OBJ;
  5468. ctn->uni.ofs = 0;
  5469. if (*cur == '\n') cur++;
  5470. goto obj_key_begin;
  5471. } else {
  5472. ctn->tag = YYJSON_TYPE_ARR;
  5473. ctn->uni.ofs = 0;
  5474. if (*cur == '\n') cur++;
  5475. goto arr_val_begin;
  5476. }
  5477. arr_begin:
  5478. /* save current container */
  5479. ctn->tag = (((u64)ctn_len + 1) << YYJSON_TAG_BIT) |
  5480. (ctn->tag & YYJSON_TAG_MASK);
  5481. /* create a new array value, save parent container offset */
  5482. val_incr();
  5483. val->tag = YYJSON_TYPE_ARR;
  5484. val->uni.ofs = (usize)((u8 *)val - (u8 *)ctn);
  5485. /* push the new array value as current container */
  5486. ctn = val;
  5487. ctn_len = 0;
  5488. if (*cur == '\n') cur++;
  5489. arr_val_begin:
  5490. #if YYJSON_IS_REAL_GCC
  5491. while (true) repeat16({
  5492. if (byte_match_2(cur, " ")) cur += 2;
  5493. else break;
  5494. })
  5495. #else
  5496. while (true) repeat16({
  5497. if (likely(byte_match_2(cur, " "))) cur += 2;
  5498. else break;
  5499. })
  5500. #endif
  5501. if (*cur == '{') {
  5502. cur++;
  5503. goto obj_begin;
  5504. }
  5505. if (*cur == '[') {
  5506. cur++;
  5507. goto arr_begin;
  5508. }
  5509. if (char_is_number(*cur)) {
  5510. val_incr();
  5511. ctn_len++;
  5512. if (likely(read_number(&cur, pre, flg, val, &msg))) goto arr_val_end;
  5513. goto fail_number;
  5514. }
  5515. if (*cur == '"') {
  5516. val_incr();
  5517. ctn_len++;
  5518. if (likely(read_string(&cur, end, inv, val, &msg))) goto arr_val_end;
  5519. goto fail_string;
  5520. }
  5521. if (*cur == 't') {
  5522. val_incr();
  5523. ctn_len++;
  5524. if (likely(read_true(&cur, val))) goto arr_val_end;
  5525. goto fail_literal;
  5526. }
  5527. if (*cur == 'f') {
  5528. val_incr();
  5529. ctn_len++;
  5530. if (likely(read_false(&cur, val))) goto arr_val_end;
  5531. goto fail_literal;
  5532. }
  5533. if (*cur == 'n') {
  5534. val_incr();
  5535. ctn_len++;
  5536. if (likely(read_null(&cur, val))) goto arr_val_end;
  5537. if (has_read_flag(ALLOW_INF_AND_NAN)) {
  5538. if (read_nan(false, &cur, pre, val)) goto arr_val_end;
  5539. }
  5540. goto fail_literal;
  5541. }
  5542. if (*cur == ']') {
  5543. cur++;
  5544. if (likely(ctn_len == 0)) goto arr_end;
  5545. if (has_read_flag(ALLOW_TRAILING_COMMAS)) goto arr_end;
  5546. while (*cur != ',') cur--;
  5547. goto fail_trailing_comma;
  5548. }
  5549. if (char_is_space(*cur)) {
  5550. while (char_is_space(*++cur));
  5551. goto arr_val_begin;
  5552. }
  5553. if (has_read_flag(ALLOW_INF_AND_NAN) &&
  5554. (*cur == 'i' || *cur == 'I' || *cur == 'N')) {
  5555. val_incr();
  5556. ctn_len++;
  5557. if (read_inf_or_nan(false, &cur, pre, val)) goto arr_val_end;
  5558. goto fail_character;
  5559. }
  5560. if (has_read_flag(ALLOW_COMMENTS)) {
  5561. if (skip_spaces_and_comments(&cur)) goto arr_val_begin;
  5562. if (byte_match_2(cur, "/*")) goto fail_comment;
  5563. }
  5564. goto fail_character;
  5565. arr_val_end:
  5566. if (byte_match_2(cur, ",\n")) {
  5567. cur += 2;
  5568. goto arr_val_begin;
  5569. }
  5570. if (*cur == ',') {
  5571. cur++;
  5572. goto arr_val_begin;
  5573. }
  5574. if (*cur == ']') {
  5575. cur++;
  5576. goto arr_end;
  5577. }
  5578. if (char_is_space(*cur)) {
  5579. while (char_is_space(*++cur));
  5580. goto arr_val_end;
  5581. }
  5582. if (has_read_flag(ALLOW_COMMENTS)) {
  5583. if (skip_spaces_and_comments(&cur)) goto arr_val_end;
  5584. if (byte_match_2(cur, "/*")) goto fail_comment;
  5585. }
  5586. goto fail_character;
  5587. arr_end:
  5588. /* get parent container */
  5589. ctn_parent = (yyjson_val *)(void *)((u8 *)ctn - ctn->uni.ofs);
  5590. /* save the next sibling value offset */
  5591. ctn->uni.ofs = (usize)((u8 *)val - (u8 *)ctn) + sizeof(yyjson_val);
  5592. ctn->tag = ((ctn_len) << YYJSON_TAG_BIT) | YYJSON_TYPE_ARR;
  5593. if (unlikely(ctn == ctn_parent)) goto doc_end;
  5594. /* pop parent as current container */
  5595. ctn = ctn_parent;
  5596. ctn_len = (usize)(ctn->tag >> YYJSON_TAG_BIT);
  5597. if (*cur == '\n') cur++;
  5598. if ((ctn->tag & YYJSON_TYPE_MASK) == YYJSON_TYPE_OBJ) {
  5599. goto obj_val_end;
  5600. } else {
  5601. goto arr_val_end;
  5602. }
  5603. obj_begin:
  5604. /* push container */
  5605. ctn->tag = (((u64)ctn_len + 1) << YYJSON_TAG_BIT) |
  5606. (ctn->tag & YYJSON_TAG_MASK);
  5607. val_incr();
  5608. val->tag = YYJSON_TYPE_OBJ;
  5609. /* offset to the parent */
  5610. val->uni.ofs = (usize)((u8 *)val - (u8 *)ctn);
  5611. ctn = val;
  5612. ctn_len = 0;
  5613. if (*cur == '\n') cur++;
  5614. obj_key_begin:
  5615. #if YYJSON_IS_REAL_GCC
  5616. while (true) repeat16({
  5617. if (byte_match_2(cur, " ")) cur += 2;
  5618. else break;
  5619. })
  5620. #else
  5621. while (true) repeat16({
  5622. if (likely(byte_match_2(cur, " "))) cur += 2;
  5623. else break;
  5624. })
  5625. #endif
  5626. if (likely(*cur == '"')) {
  5627. val_incr();
  5628. ctn_len++;
  5629. if (likely(read_string(&cur, end, inv, val, &msg))) goto obj_key_end;
  5630. goto fail_string;
  5631. }
  5632. if (likely(*cur == '}')) {
  5633. cur++;
  5634. if (likely(ctn_len == 0)) goto obj_end;
  5635. if (has_read_flag(ALLOW_TRAILING_COMMAS)) goto obj_end;
  5636. while (*cur != ',') cur--;
  5637. goto fail_trailing_comma;
  5638. }
  5639. if (char_is_space(*cur)) {
  5640. while (char_is_space(*++cur));
  5641. goto obj_key_begin;
  5642. }
  5643. if (has_read_flag(ALLOW_COMMENTS)) {
  5644. if (skip_spaces_and_comments(&cur)) goto obj_key_begin;
  5645. if (byte_match_2(cur, "/*")) goto fail_comment;
  5646. }
  5647. goto fail_character;
  5648. obj_key_end:
  5649. if (byte_match_2(cur, ": ")) {
  5650. cur += 2;
  5651. goto obj_val_begin;
  5652. }
  5653. if (*cur == ':') {
  5654. cur++;
  5655. goto obj_val_begin;
  5656. }
  5657. if (char_is_space(*cur)) {
  5658. while (char_is_space(*++cur));
  5659. goto obj_key_end;
  5660. }
  5661. if (has_read_flag(ALLOW_COMMENTS)) {
  5662. if (skip_spaces_and_comments(&cur)) goto obj_key_end;
  5663. if (byte_match_2(cur, "/*")) goto fail_comment;
  5664. }
  5665. goto fail_character;
  5666. obj_val_begin:
  5667. if (*cur == '"') {
  5668. val++;
  5669. ctn_len++;
  5670. if (likely(read_string(&cur, end, inv, val, &msg))) goto obj_val_end;
  5671. goto fail_string;
  5672. }
  5673. if (char_is_number(*cur)) {
  5674. val++;
  5675. ctn_len++;
  5676. if (likely(read_number(&cur, pre, flg, val, &msg))) goto obj_val_end;
  5677. goto fail_number;
  5678. }
  5679. if (*cur == '{') {
  5680. cur++;
  5681. goto obj_begin;
  5682. }
  5683. if (*cur == '[') {
  5684. cur++;
  5685. goto arr_begin;
  5686. }
  5687. if (*cur == 't') {
  5688. val++;
  5689. ctn_len++;
  5690. if (likely(read_true(&cur, val))) goto obj_val_end;
  5691. goto fail_literal;
  5692. }
  5693. if (*cur == 'f') {
  5694. val++;
  5695. ctn_len++;
  5696. if (likely(read_false(&cur, val))) goto obj_val_end;
  5697. goto fail_literal;
  5698. }
  5699. if (*cur == 'n') {
  5700. val++;
  5701. ctn_len++;
  5702. if (likely(read_null(&cur, val))) goto obj_val_end;
  5703. if (has_read_flag(ALLOW_INF_AND_NAN)) {
  5704. if (read_nan(false, &cur, pre, val)) goto obj_val_end;
  5705. }
  5706. goto fail_literal;
  5707. }
  5708. if (char_is_space(*cur)) {
  5709. while (char_is_space(*++cur));
  5710. goto obj_val_begin;
  5711. }
  5712. if (has_read_flag(ALLOW_INF_AND_NAN) &&
  5713. (*cur == 'i' || *cur == 'I' || *cur == 'N')) {
  5714. val++;
  5715. ctn_len++;
  5716. if (read_inf_or_nan(false, &cur, pre, val)) goto obj_val_end;
  5717. goto fail_character;
  5718. }
  5719. if (has_read_flag(ALLOW_COMMENTS)) {
  5720. if (skip_spaces_and_comments(&cur)) goto obj_val_begin;
  5721. if (byte_match_2(cur, "/*")) goto fail_comment;
  5722. }
  5723. goto fail_character;
  5724. obj_val_end:
  5725. if (byte_match_2(cur, ",\n")) {
  5726. cur += 2;
  5727. goto obj_key_begin;
  5728. }
  5729. if (likely(*cur == ',')) {
  5730. cur++;
  5731. goto obj_key_begin;
  5732. }
  5733. if (likely(*cur == '}')) {
  5734. cur++;
  5735. goto obj_end;
  5736. }
  5737. if (char_is_space(*cur)) {
  5738. while (char_is_space(*++cur));
  5739. goto obj_val_end;
  5740. }
  5741. if (has_read_flag(ALLOW_COMMENTS)) {
  5742. if (skip_spaces_and_comments(&cur)) goto obj_val_end;
  5743. if (byte_match_2(cur, "/*")) goto fail_comment;
  5744. }
  5745. goto fail_character;
  5746. obj_end:
  5747. /* pop container */
  5748. ctn_parent = (yyjson_val *)(void *)((u8 *)ctn - ctn->uni.ofs);
  5749. /* point to the next value */
  5750. ctn->uni.ofs = (usize)((u8 *)val - (u8 *)ctn) + sizeof(yyjson_val);
  5751. ctn->tag = (ctn_len << (YYJSON_TAG_BIT - 1)) | YYJSON_TYPE_OBJ;
  5752. if (unlikely(ctn == ctn_parent)) goto doc_end;
  5753. ctn = ctn_parent;
  5754. ctn_len = (usize)(ctn->tag >> YYJSON_TAG_BIT);
  5755. if (*cur == '\n') cur++;
  5756. if ((ctn->tag & YYJSON_TYPE_MASK) == YYJSON_TYPE_OBJ) {
  5757. goto obj_val_end;
  5758. } else {
  5759. goto arr_val_end;
  5760. }
  5761. doc_end:
  5762. /* check invalid contents after json document */
  5763. if (unlikely(cur < end) && !has_read_flag(STOP_WHEN_DONE)) {
  5764. if (has_read_flag(ALLOW_COMMENTS)) {
  5765. skip_spaces_and_comments(&cur);
  5766. if (byte_match_2(cur, "/*")) goto fail_comment;
  5767. } else {
  5768. while (char_is_space(*cur)) cur++;
  5769. }
  5770. if (unlikely(cur < end)) goto fail_garbage;
  5771. }
  5772. if (pre && *pre) **pre = '\0';
  5773. doc = (yyjson_doc *)val_hdr;
  5774. doc->root = val_hdr + hdr_len;
  5775. doc->alc = alc;
  5776. doc->dat_read = (usize)(cur - hdr);
  5777. doc->val_read = (usize)((val - val_hdr)) - hdr_len + 1;
  5778. doc->str_pool = has_read_flag(INSITU) ? NULL : (char *)hdr;
  5779. return doc;
  5780. fail_string:
  5781. return_err(cur, INVALID_STRING, msg);
  5782. fail_number:
  5783. return_err(cur, INVALID_NUMBER, msg);
  5784. fail_alloc:
  5785. return_err(cur, MEMORY_ALLOCATION, "memory allocation failed");
  5786. fail_trailing_comma:
  5787. return_err(cur, JSON_STRUCTURE, "trailing comma is not allowed");
  5788. fail_literal:
  5789. return_err(cur, LITERAL, "invalid literal");
  5790. fail_comment:
  5791. return_err(cur, INVALID_COMMENT, "unclosed multiline comment");
  5792. fail_character:
  5793. return_err(cur, UNEXPECTED_CHARACTER, "unexpected character");
  5794. fail_garbage:
  5795. return_err(cur, UNEXPECTED_CONTENT, "unexpected content after document");
  5796. #undef val_incr
  5797. #undef return_err
  5798. }
  5799. /*==============================================================================
  5800. * JSON Reader Entrance
  5801. *============================================================================*/
  5802. yyjson_doc *yyjson_read_opts(char *dat,
  5803. usize len,
  5804. yyjson_read_flag flg,
  5805. const yyjson_alc *alc_ptr,
  5806. yyjson_read_err *err) {
  5807. #define return_err(_pos, _code, _msg) do { \
  5808. err->pos = (usize)(_pos); \
  5809. err->msg = _msg; \
  5810. err->code = YYJSON_READ_ERROR_##_code; \
  5811. if (!has_read_flag(INSITU) && hdr) alc.free(alc.ctx, (void *)hdr); \
  5812. return NULL; \
  5813. } while (false)
  5814. yyjson_read_err dummy_err;
  5815. yyjson_alc alc;
  5816. yyjson_doc *doc;
  5817. u8 *hdr = NULL, *end, *cur;
  5818. /* validate input parameters */
  5819. if (!err) err = &dummy_err;
  5820. if (likely(!alc_ptr)) {
  5821. alc = YYJSON_DEFAULT_ALC;
  5822. } else {
  5823. alc = *alc_ptr;
  5824. }
  5825. if (unlikely(!dat)) {
  5826. return_err(0, INVALID_PARAMETER, "input data is NULL");
  5827. }
  5828. if (unlikely(!len)) {
  5829. return_err(0, INVALID_PARAMETER, "input length is 0");
  5830. }
  5831. /* add 4-byte zero padding for input data if necessary */
  5832. if (has_read_flag(INSITU)) {
  5833. hdr = (u8 *)dat;
  5834. end = (u8 *)dat + len;
  5835. cur = (u8 *)dat;
  5836. } else {
  5837. if (unlikely(len >= USIZE_MAX - YYJSON_PADDING_SIZE)) {
  5838. return_err(0, MEMORY_ALLOCATION, "memory allocation failed");
  5839. }
  5840. hdr = (u8 *)alc.malloc(alc.ctx, len + YYJSON_PADDING_SIZE);
  5841. if (unlikely(!hdr)) {
  5842. return_err(0, MEMORY_ALLOCATION, "memory allocation failed");
  5843. }
  5844. end = hdr + len;
  5845. cur = hdr;
  5846. memcpy(hdr, dat, len);
  5847. memset(end, 0, YYJSON_PADDING_SIZE);
  5848. }
  5849. /* skip empty contents before json document */
  5850. if (unlikely(char_is_space_or_comment(*cur))) {
  5851. if (has_read_flag(ALLOW_COMMENTS)) {
  5852. if (!skip_spaces_and_comments(&cur)) {
  5853. return_err(cur - hdr, INVALID_COMMENT,
  5854. "unclosed multiline comment");
  5855. }
  5856. } else {
  5857. if (likely(char_is_space(*cur))) {
  5858. while (char_is_space(*++cur));
  5859. }
  5860. }
  5861. if (unlikely(cur >= end)) {
  5862. return_err(0, EMPTY_CONTENT, "input data is empty");
  5863. }
  5864. }
  5865. /* read json document */
  5866. if (likely(char_is_container(*cur))) {
  5867. if (char_is_space(cur[1]) && char_is_space(cur[2])) {
  5868. doc = read_root_pretty(hdr, cur, end, alc, flg, err);
  5869. } else {
  5870. doc = read_root_minify(hdr, cur, end, alc, flg, err);
  5871. }
  5872. } else {
  5873. doc = read_root_single(hdr, cur, end, alc, flg, err);
  5874. }
  5875. /* check result */
  5876. if (likely(doc)) {
  5877. memset(err, 0, sizeof(yyjson_read_err));
  5878. } else {
  5879. /* RFC 8259: JSON text MUST be encoded using UTF-8 */
  5880. if (err->pos == 0 && err->code != YYJSON_READ_ERROR_MEMORY_ALLOCATION) {
  5881. if ((hdr[0] == 0xEF && hdr[1] == 0xBB && hdr[2] == 0xBF)) {
  5882. err->msg = "byte order mark (BOM) is not supported";
  5883. } else if (len >= 4 &&
  5884. ((hdr[0] == 0x00 && hdr[1] == 0x00 &&
  5885. hdr[2] == 0xFE && hdr[3] == 0xFF) ||
  5886. (hdr[0] == 0xFF && hdr[1] == 0xFE &&
  5887. hdr[2] == 0x00 && hdr[3] == 0x00))) {
  5888. err->msg = "UTF-32 encoding is not supported";
  5889. } else if (len >= 2 &&
  5890. ((hdr[0] == 0xFE && hdr[1] == 0xFF) ||
  5891. (hdr[0] == 0xFF && hdr[1] == 0xFE))) {
  5892. err->msg = "UTF-16 encoding is not supported";
  5893. }
  5894. }
  5895. if (!has_read_flag(INSITU)) alc.free(alc.ctx, (void *)hdr);
  5896. }
  5897. return doc;
  5898. #undef return_err
  5899. }
  5900. yyjson_doc *yyjson_read_file(const char *path,
  5901. yyjson_read_flag flg,
  5902. const yyjson_alc *alc_ptr,
  5903. yyjson_read_err *err) {
  5904. #define return_err(_code, _msg) do { \
  5905. err->pos = 0; \
  5906. err->msg = _msg; \
  5907. err->code = YYJSON_READ_ERROR_##_code; \
  5908. return NULL; \
  5909. } while (false)
  5910. yyjson_read_err dummy_err;
  5911. yyjson_doc *doc;
  5912. FILE *file;
  5913. if (!err) err = &dummy_err;
  5914. if (unlikely(!path)) return_err(INVALID_PARAMETER, "input path is NULL");
  5915. file = fopen_readonly(path);
  5916. if (unlikely(!file)) return_err(FILE_OPEN, "file opening failed");
  5917. doc = yyjson_read_fp(file, flg, alc_ptr, err);
  5918. fclose(file);
  5919. return doc;
  5920. #undef return_err
  5921. }
  5922. yyjson_doc *yyjson_read_fp(FILE *file,
  5923. yyjson_read_flag flg,
  5924. const yyjson_alc *alc_ptr,
  5925. yyjson_read_err *err) {
  5926. #define return_err(_code, _msg) do { \
  5927. err->pos = 0; \
  5928. err->msg = _msg; \
  5929. err->code = YYJSON_READ_ERROR_##_code; \
  5930. if (buf) alc.free(alc.ctx, buf); \
  5931. return NULL; \
  5932. } while (false)
  5933. yyjson_read_err dummy_err;
  5934. yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC;
  5935. yyjson_doc *doc;
  5936. long file_size = 0, file_pos;
  5937. void *buf = NULL;
  5938. usize buf_size = 0;
  5939. /* validate input parameters */
  5940. if (!err) err = &dummy_err;
  5941. if (unlikely(!file)) return_err(INVALID_PARAMETER, "input file is NULL");
  5942. /* get current position */
  5943. file_pos = ftell(file);
  5944. if (file_pos != -1) {
  5945. /* get total file size, may fail */
  5946. if (fseek(file, 0, SEEK_END) == 0) file_size = ftell(file);
  5947. /* reset to original position, may fail */
  5948. if (fseek(file, file_pos, SEEK_SET) != 0) file_size = 0;
  5949. /* get file size from current postion to end */
  5950. if (file_size > 0) file_size -= file_pos;
  5951. }
  5952. /* read file */
  5953. if (file_size > 0) {
  5954. /* read the entire file in one call */
  5955. buf_size = (usize)file_size + YYJSON_PADDING_SIZE;
  5956. buf = alc.malloc(alc.ctx, buf_size);
  5957. if (buf == NULL) {
  5958. return_err(MEMORY_ALLOCATION, "fail to alloc memory");
  5959. }
  5960. if (fread_safe(buf, (usize)file_size, file) != (usize)file_size) {
  5961. return_err(FILE_READ, "file reading failed");
  5962. }
  5963. } else {
  5964. /* failed to get file size, read it as a stream */
  5965. usize chunk_min = (usize)64;
  5966. usize chunk_max = (usize)512 * 1024 * 1024;
  5967. usize chunk_now = chunk_min;
  5968. usize read_size;
  5969. void *tmp;
  5970. buf_size = YYJSON_PADDING_SIZE;
  5971. while (true) {
  5972. if (buf_size + chunk_now < buf_size) { /* overflow */
  5973. return_err(MEMORY_ALLOCATION, "fail to alloc memory");
  5974. }
  5975. buf_size += chunk_now;
  5976. if (!buf) {
  5977. buf = alc.malloc(alc.ctx, buf_size);
  5978. if (!buf) return_err(MEMORY_ALLOCATION, "fail to alloc memory");
  5979. } else {
  5980. tmp = alc.realloc(alc.ctx, buf, buf_size - chunk_now, buf_size);
  5981. if (!tmp) return_err(MEMORY_ALLOCATION, "fail to alloc memory");
  5982. buf = tmp;
  5983. }
  5984. tmp = ((u8 *)buf) + buf_size - YYJSON_PADDING_SIZE - chunk_now;
  5985. read_size = fread_safe(tmp, chunk_now, file);
  5986. file_size += (long)read_size;
  5987. if (read_size != chunk_now) break;
  5988. chunk_now *= 2;
  5989. if (chunk_now > chunk_max) chunk_now = chunk_max;
  5990. }
  5991. }
  5992. /* read JSON */
  5993. memset((u8 *)buf + file_size, 0, YYJSON_PADDING_SIZE);
  5994. flg |= YYJSON_READ_INSITU;
  5995. doc = yyjson_read_opts((char *)buf, (usize)file_size, flg, &alc, err);
  5996. if (doc) {
  5997. doc->str_pool = (char *)buf;
  5998. return doc;
  5999. } else {
  6000. alc.free(alc.ctx, buf);
  6001. return NULL;
  6002. }
  6003. #undef return_err
  6004. }
  6005. const char *yyjson_read_number(const char *dat,
  6006. yyjson_val *val,
  6007. yyjson_read_flag flg,
  6008. const yyjson_alc *alc,
  6009. yyjson_read_err *err) {
  6010. #define return_err(_pos, _code, _msg) do { \
  6011. err->pos = _pos > hdr ? (usize)(_pos - hdr) : 0; \
  6012. err->msg = _msg; \
  6013. err->code = YYJSON_READ_ERROR_##_code; \
  6014. return NULL; \
  6015. } while (false)
  6016. u8 *hdr = constcast(u8 *)dat, *cur = hdr;
  6017. bool raw; /* read number as raw */
  6018. u8 *raw_end; /* raw end for null-terminator */
  6019. u8 **pre; /* previous raw end pointer */
  6020. const char *msg;
  6021. yyjson_read_err dummy_err;
  6022. #if !YYJSON_HAS_IEEE_754 || YYJSON_DISABLE_FAST_FP_CONV
  6023. u8 buf[128];
  6024. usize dat_len;
  6025. #endif
  6026. if (!err) err = &dummy_err;
  6027. if (unlikely(!dat)) {
  6028. return_err(cur, INVALID_PARAMETER, "input data is NULL");
  6029. }
  6030. if (unlikely(!val)) {
  6031. return_err(cur, INVALID_PARAMETER, "output value is NULL");
  6032. }
  6033. #if !YYJSON_HAS_IEEE_754 || YYJSON_DISABLE_FAST_FP_CONV
  6034. if (!alc) alc = &YYJSON_DEFAULT_ALC;
  6035. dat_len = strlen(dat);
  6036. if (dat_len < sizeof(buf)) {
  6037. memcpy(buf, dat, dat_len + 1);
  6038. hdr = buf;
  6039. cur = hdr;
  6040. } else {
  6041. hdr = (u8 *)alc->malloc(alc->ctx, dat_len + 1);
  6042. cur = hdr;
  6043. if (unlikely(!hdr)) {
  6044. return_err(cur, MEMORY_ALLOCATION, "memory allocation failed");
  6045. }
  6046. memcpy(hdr, dat, dat_len + 1);
  6047. }
  6048. hdr[dat_len] = 0;
  6049. #endif
  6050. raw = (flg & (YYJSON_READ_NUMBER_AS_RAW | YYJSON_READ_BIGNUM_AS_RAW)) != 0;
  6051. raw_end = NULL;
  6052. pre = raw ? &raw_end : NULL;
  6053. #if !YYJSON_HAS_IEEE_754 || YYJSON_DISABLE_FAST_FP_CONV
  6054. if (!read_number(&cur, pre, flg, val, &msg)) {
  6055. if (dat_len >= sizeof(buf)) alc->free(alc->ctx, hdr);
  6056. return_err(cur, INVALID_NUMBER, msg);
  6057. }
  6058. if (dat_len >= sizeof(buf)) alc->free(alc->ctx, hdr);
  6059. if (yyjson_is_raw(val)) val->uni.str = dat;
  6060. return dat + (cur - hdr);
  6061. #else
  6062. if (!read_number(&cur, pre, flg, val, &msg)) {
  6063. return_err(cur, INVALID_NUMBER, msg);
  6064. }
  6065. return (const char *)cur;
  6066. #endif
  6067. #undef return_err
  6068. }
  6069. #endif /* YYJSON_DISABLE_READER */
  6070. #if !YYJSON_DISABLE_WRITER
  6071. /*==============================================================================
  6072. * Integer Writer
  6073. *
  6074. * The maximum value of uint32_t is 4294967295 (10 digits),
  6075. * these digits are named as 'aabbccddee' here.
  6076. *
  6077. * Although most compilers may convert the "division by constant value" into
  6078. * "multiply and shift", manual conversion can still help some compilers
  6079. * generate fewer and better instructions.
  6080. *
  6081. * Reference:
  6082. * Division by Invariant Integers using Multiplication, 1994.
  6083. * https://gmplib.org/~tege/divcnst-pldi94.pdf
  6084. * Improved division by invariant integers, 2011.
  6085. * https://gmplib.org/~tege/division-paper.pdf
  6086. *============================================================================*/
  6087. /** Digit table from 00 to 99. */
  6088. yyjson_align(2)
  6089. static const char digit_table[200] = {
  6090. '0', '0', '0', '1', '0', '2', '0', '3', '0', '4',
  6091. '0', '5', '0', '6', '0', '7', '0', '8', '0', '9',
  6092. '1', '0', '1', '1', '1', '2', '1', '3', '1', '4',
  6093. '1', '5', '1', '6', '1', '7', '1', '8', '1', '9',
  6094. '2', '0', '2', '1', '2', '2', '2', '3', '2', '4',
  6095. '2', '5', '2', '6', '2', '7', '2', '8', '2', '9',
  6096. '3', '0', '3', '1', '3', '2', '3', '3', '3', '4',
  6097. '3', '5', '3', '6', '3', '7', '3', '8', '3', '9',
  6098. '4', '0', '4', '1', '4', '2', '4', '3', '4', '4',
  6099. '4', '5', '4', '6', '4', '7', '4', '8', '4', '9',
  6100. '5', '0', '5', '1', '5', '2', '5', '3', '5', '4',
  6101. '5', '5', '5', '6', '5', '7', '5', '8', '5', '9',
  6102. '6', '0', '6', '1', '6', '2', '6', '3', '6', '4',
  6103. '6', '5', '6', '6', '6', '7', '6', '8', '6', '9',
  6104. '7', '0', '7', '1', '7', '2', '7', '3', '7', '4',
  6105. '7', '5', '7', '6', '7', '7', '7', '8', '7', '9',
  6106. '8', '0', '8', '1', '8', '2', '8', '3', '8', '4',
  6107. '8', '5', '8', '6', '8', '7', '8', '8', '8', '9',
  6108. '9', '0', '9', '1', '9', '2', '9', '3', '9', '4',
  6109. '9', '5', '9', '6', '9', '7', '9', '8', '9', '9'
  6110. };
  6111. static_inline u8 *write_u32_len_8(u32 val, u8 *buf) {
  6112. u32 aa, bb, cc, dd, aabb, ccdd; /* 8 digits: aabbccdd */
  6113. aabb = (u32)(((u64)val * 109951163) >> 40); /* (val / 10000) */
  6114. ccdd = val - aabb * 10000; /* (val % 10000) */
  6115. aa = (aabb * 5243) >> 19; /* (aabb / 100) */
  6116. cc = (ccdd * 5243) >> 19; /* (ccdd / 100) */
  6117. bb = aabb - aa * 100; /* (aabb % 100) */
  6118. dd = ccdd - cc * 100; /* (ccdd % 100) */
  6119. byte_copy_2(buf + 0, digit_table + aa * 2);
  6120. byte_copy_2(buf + 2, digit_table + bb * 2);
  6121. byte_copy_2(buf + 4, digit_table + cc * 2);
  6122. byte_copy_2(buf + 6, digit_table + dd * 2);
  6123. return buf + 8;
  6124. }
  6125. static_inline u8 *write_u32_len_4(u32 val, u8 *buf) {
  6126. u32 aa, bb; /* 4 digits: aabb */
  6127. aa = (val * 5243) >> 19; /* (val / 100) */
  6128. bb = val - aa * 100; /* (val % 100) */
  6129. byte_copy_2(buf + 0, digit_table + aa * 2);
  6130. byte_copy_2(buf + 2, digit_table + bb * 2);
  6131. return buf + 4;
  6132. }
  6133. static_inline u8 *write_u32_len_1_8(u32 val, u8 *buf) {
  6134. u32 aa, bb, cc, dd, aabb, bbcc, ccdd, lz;
  6135. if (val < 100) { /* 1-2 digits: aa */
  6136. lz = val < 10; /* leading zero: 0 or 1 */
  6137. byte_copy_2(buf + 0, digit_table + val * 2 + lz);
  6138. buf -= lz;
  6139. return buf + 2;
  6140. } else if (val < 10000) { /* 3-4 digits: aabb */
  6141. aa = (val * 5243) >> 19; /* (val / 100) */
  6142. bb = val - aa * 100; /* (val % 100) */
  6143. lz = aa < 10; /* leading zero: 0 or 1 */
  6144. byte_copy_2(buf + 0, digit_table + aa * 2 + lz);
  6145. buf -= lz;
  6146. byte_copy_2(buf + 2, digit_table + bb * 2);
  6147. return buf + 4;
  6148. } else if (val < 1000000) { /* 5-6 digits: aabbcc */
  6149. aa = (u32)(((u64)val * 429497) >> 32); /* (val / 10000) */
  6150. bbcc = val - aa * 10000; /* (val % 10000) */
  6151. bb = (bbcc * 5243) >> 19; /* (bbcc / 100) */
  6152. cc = bbcc - bb * 100; /* (bbcc % 100) */
  6153. lz = aa < 10; /* leading zero: 0 or 1 */
  6154. byte_copy_2(buf + 0, digit_table + aa * 2 + lz);
  6155. buf -= lz;
  6156. byte_copy_2(buf + 2, digit_table + bb * 2);
  6157. byte_copy_2(buf + 4, digit_table + cc * 2);
  6158. return buf + 6;
  6159. } else { /* 7-8 digits: aabbccdd */
  6160. aabb = (u32)(((u64)val * 109951163) >> 40); /* (val / 10000) */
  6161. ccdd = val - aabb * 10000; /* (val % 10000) */
  6162. aa = (aabb * 5243) >> 19; /* (aabb / 100) */
  6163. cc = (ccdd * 5243) >> 19; /* (ccdd / 100) */
  6164. bb = aabb - aa * 100; /* (aabb % 100) */
  6165. dd = ccdd - cc * 100; /* (ccdd % 100) */
  6166. lz = aa < 10; /* leading zero: 0 or 1 */
  6167. byte_copy_2(buf + 0, digit_table + aa * 2 + lz);
  6168. buf -= lz;
  6169. byte_copy_2(buf + 2, digit_table + bb * 2);
  6170. byte_copy_2(buf + 4, digit_table + cc * 2);
  6171. byte_copy_2(buf + 6, digit_table + dd * 2);
  6172. return buf + 8;
  6173. }
  6174. }
  6175. static_inline u8 *write_u64_len_5_8(u32 val, u8 *buf) {
  6176. u32 aa, bb, cc, dd, aabb, bbcc, ccdd, lz;
  6177. if (val < 1000000) { /* 5-6 digits: aabbcc */
  6178. aa = (u32)(((u64)val * 429497) >> 32); /* (val / 10000) */
  6179. bbcc = val - aa * 10000; /* (val % 10000) */
  6180. bb = (bbcc * 5243) >> 19; /* (bbcc / 100) */
  6181. cc = bbcc - bb * 100; /* (bbcc % 100) */
  6182. lz = aa < 10; /* leading zero: 0 or 1 */
  6183. byte_copy_2(buf + 0, digit_table + aa * 2 + lz);
  6184. buf -= lz;
  6185. byte_copy_2(buf + 2, digit_table + bb * 2);
  6186. byte_copy_2(buf + 4, digit_table + cc * 2);
  6187. return buf + 6;
  6188. } else { /* 7-8 digits: aabbccdd */
  6189. aabb = (u32)(((u64)val * 109951163) >> 40); /* (val / 10000) */
  6190. ccdd = val - aabb * 10000; /* (val % 10000) */
  6191. aa = (aabb * 5243) >> 19; /* (aabb / 100) */
  6192. cc = (ccdd * 5243) >> 19; /* (ccdd / 100) */
  6193. bb = aabb - aa * 100; /* (aabb % 100) */
  6194. dd = ccdd - cc * 100; /* (ccdd % 100) */
  6195. lz = aa < 10; /* leading zero: 0 or 1 */
  6196. byte_copy_2(buf + 0, digit_table + aa * 2 + lz);
  6197. buf -= lz;
  6198. byte_copy_2(buf + 2, digit_table + bb * 2);
  6199. byte_copy_2(buf + 4, digit_table + cc * 2);
  6200. byte_copy_2(buf + 6, digit_table + dd * 2);
  6201. return buf + 8;
  6202. }
  6203. }
  6204. static_inline u8 *write_u64(u64 val, u8 *buf) {
  6205. u64 tmp, hgh;
  6206. u32 mid, low;
  6207. if (val < 100000000) { /* 1-8 digits */
  6208. buf = write_u32_len_1_8((u32)val, buf);
  6209. return buf;
  6210. } else if (val < (u64)100000000 * 100000000) { /* 9-16 digits */
  6211. hgh = val / 100000000; /* (val / 100000000) */
  6212. low = (u32)(val - hgh * 100000000); /* (val % 100000000) */
  6213. buf = write_u32_len_1_8((u32)hgh, buf);
  6214. buf = write_u32_len_8(low, buf);
  6215. return buf;
  6216. } else { /* 17-20 digits */
  6217. tmp = val / 100000000; /* (val / 100000000) */
  6218. low = (u32)(val - tmp * 100000000); /* (val % 100000000) */
  6219. hgh = (u32)(tmp / 10000); /* (tmp / 10000) */
  6220. mid = (u32)(tmp - hgh * 10000); /* (tmp % 10000) */
  6221. buf = write_u64_len_5_8((u32)hgh, buf);
  6222. buf = write_u32_len_4(mid, buf);
  6223. buf = write_u32_len_8(low, buf);
  6224. return buf;
  6225. }
  6226. }
  6227. /*==============================================================================
  6228. * Number Writer
  6229. *============================================================================*/
  6230. #if YYJSON_HAS_IEEE_754 && !YYJSON_DISABLE_FAST_FP_CONV /* FP_WRITER */
  6231. /** Trailing zero count table for number 0 to 99.
  6232. (generate with misc/make_tables.c) */
  6233. static const u8 dec_trailing_zero_table[] = {
  6234. 2, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6235. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6236. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6237. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6238. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6239. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6240. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6241. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6242. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6243. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0
  6244. };
  6245. /** Write an unsigned integer with a length of 1 to 16. */
  6246. static_inline u8 *write_u64_len_1_to_16(u64 val, u8 *buf) {
  6247. u64 hgh;
  6248. u32 low;
  6249. if (val < 100000000) { /* 1-8 digits */
  6250. buf = write_u32_len_1_8((u32)val, buf);
  6251. return buf;
  6252. } else { /* 9-16 digits */
  6253. hgh = val / 100000000; /* (val / 100000000) */
  6254. low = (u32)(val - hgh * 100000000); /* (val % 100000000) */
  6255. buf = write_u32_len_1_8((u32)hgh, buf);
  6256. buf = write_u32_len_8(low, buf);
  6257. return buf;
  6258. }
  6259. }
  6260. /** Write an unsigned integer with a length of 1 to 17. */
  6261. static_inline u8 *write_u64_len_1_to_17(u64 val, u8 *buf) {
  6262. u64 hgh;
  6263. u32 mid, low, one;
  6264. if (val >= (u64)100000000 * 10000000) { /* len: 16 to 17 */
  6265. hgh = val / 100000000; /* (val / 100000000) */
  6266. low = (u32)(val - hgh * 100000000); /* (val % 100000000) */
  6267. one = (u32)(hgh / 100000000); /* (hgh / 100000000) */
  6268. mid = (u32)(hgh - (u64)one * 100000000); /* (hgh % 100000000) */
  6269. *buf = (u8)((u8)one + (u8)'0');
  6270. buf += one > 0;
  6271. buf = write_u32_len_8(mid, buf);
  6272. buf = write_u32_len_8(low, buf);
  6273. return buf;
  6274. } else if (val >= (u64)100000000){ /* len: 9 to 15 */
  6275. hgh = val / 100000000; /* (val / 100000000) */
  6276. low = (u32)(val - hgh * 100000000); /* (val % 100000000) */
  6277. buf = write_u32_len_1_8((u32)hgh, buf);
  6278. buf = write_u32_len_8(low, buf);
  6279. return buf;
  6280. } else { /* len: 1 to 8 */
  6281. buf = write_u32_len_1_8((u32)val, buf);
  6282. return buf;
  6283. }
  6284. }
  6285. /**
  6286. Write an unsigned integer with a length of 15 to 17 with trailing zero trimmed.
  6287. These digits are named as "aabbccddeeffgghhii" here.
  6288. For example, input 1234567890123000, output "1234567890123".
  6289. */
  6290. static_inline u8 *write_u64_len_15_to_17_trim(u8 *buf, u64 sig) {
  6291. bool lz; /* leading zero */
  6292. u32 tz1, tz2, tz; /* trailing zero */
  6293. u32 abbccddee = (u32)(sig / 100000000);
  6294. u32 ffgghhii = (u32)(sig - (u64)abbccddee * 100000000);
  6295. u32 abbcc = abbccddee / 10000; /* (abbccddee / 10000) */
  6296. u32 ddee = abbccddee - abbcc * 10000; /* (abbccddee % 10000) */
  6297. u32 abb = (u32)(((u64)abbcc * 167773) >> 24); /* (abbcc / 100) */
  6298. u32 a = (abb * 41) >> 12; /* (abb / 100) */
  6299. u32 bb = abb - a * 100; /* (abb % 100) */
  6300. u32 cc = abbcc - abb * 100; /* (abbcc % 100) */
  6301. /* write abbcc */
  6302. buf[0] = (u8)(a + '0');
  6303. buf += a > 0;
  6304. lz = bb < 10 && a == 0;
  6305. byte_copy_2(buf + 0, digit_table + bb * 2 + lz);
  6306. buf -= lz;
  6307. byte_copy_2(buf + 2, digit_table + cc * 2);
  6308. if (ffgghhii) {
  6309. u32 dd = (ddee * 5243) >> 19; /* (ddee / 100) */
  6310. u32 ee = ddee - dd * 100; /* (ddee % 100) */
  6311. u32 ffgg = (u32)(((u64)ffgghhii * 109951163) >> 40); /* (val / 10000) */
  6312. u32 hhii = ffgghhii - ffgg * 10000; /* (val % 10000) */
  6313. u32 ff = (ffgg * 5243) >> 19; /* (aabb / 100) */
  6314. u32 gg = ffgg - ff * 100; /* (aabb % 100) */
  6315. byte_copy_2(buf + 4, digit_table + dd * 2);
  6316. byte_copy_2(buf + 6, digit_table + ee * 2);
  6317. byte_copy_2(buf + 8, digit_table + ff * 2);
  6318. byte_copy_2(buf + 10, digit_table + gg * 2);
  6319. if (hhii) {
  6320. u32 hh = (hhii * 5243) >> 19; /* (ccdd / 100) */
  6321. u32 ii = hhii - hh * 100; /* (ccdd % 100) */
  6322. byte_copy_2(buf + 12, digit_table + hh * 2);
  6323. byte_copy_2(buf + 14, digit_table + ii * 2);
  6324. tz1 = dec_trailing_zero_table[hh];
  6325. tz2 = dec_trailing_zero_table[ii];
  6326. tz = ii ? tz2 : (tz1 + 2);
  6327. buf += 16 - tz;
  6328. return buf;
  6329. } else {
  6330. tz1 = dec_trailing_zero_table[ff];
  6331. tz2 = dec_trailing_zero_table[gg];
  6332. tz = gg ? tz2 : (tz1 + 2);
  6333. buf += 12 - tz;
  6334. return buf;
  6335. }
  6336. } else {
  6337. if (ddee) {
  6338. u32 dd = (ddee * 5243) >> 19; /* (ddee / 100) */
  6339. u32 ee = ddee - dd * 100; /* (ddee % 100) */
  6340. byte_copy_2(buf + 4, digit_table + dd * 2);
  6341. byte_copy_2(buf + 6, digit_table + ee * 2);
  6342. tz1 = dec_trailing_zero_table[dd];
  6343. tz2 = dec_trailing_zero_table[ee];
  6344. tz = ee ? tz2 : (tz1 + 2);
  6345. buf += 8 - tz;
  6346. return buf;
  6347. } else {
  6348. tz1 = dec_trailing_zero_table[bb];
  6349. tz2 = dec_trailing_zero_table[cc];
  6350. tz = cc ? tz2 : (tz1 + tz2);
  6351. buf += 4 - tz;
  6352. return buf;
  6353. }
  6354. }
  6355. }
  6356. /** Write a signed integer in the range -324 to 308. */
  6357. static_inline u8 *write_f64_exp(i32 exp, u8 *buf) {
  6358. buf[0] = '-';
  6359. buf += exp < 0;
  6360. exp = exp < 0 ? -exp : exp;
  6361. if (exp < 100) {
  6362. u32 lz = exp < 10;
  6363. byte_copy_2(buf + 0, digit_table + (u32)exp * 2 + lz);
  6364. return buf + 2 - lz;
  6365. } else {
  6366. u32 hi = ((u32)exp * 656) >> 16; /* exp / 100 */
  6367. u32 lo = (u32)exp - hi * 100; /* exp % 100 */
  6368. buf[0] = (u8)((u8)hi + (u8)'0');
  6369. byte_copy_2(buf + 1, digit_table + lo * 2);
  6370. return buf + 3;
  6371. }
  6372. }
  6373. /** Multiplies 128-bit integer and returns highest 64-bit rounded value. */
  6374. static_inline u64 round_to_odd(u64 hi, u64 lo, u64 cp) {
  6375. u64 x_hi, x_lo, y_hi, y_lo;
  6376. u128_mul(cp, lo, &x_hi, &x_lo);
  6377. u128_mul_add(cp, hi, x_hi, &y_hi, &y_lo);
  6378. return y_hi | (y_lo > 1);
  6379. }
  6380. /**
  6381. Convert double number from binary to decimal.
  6382. The output significand is shortest decimal but may have trailing zeros.
  6383. This function use the Schubfach algorithm:
  6384. Raffaello Giulietti, The Schubfach way to render doubles (5th version), 2022.
  6385. https://drive.google.com/file/d/1gp5xv4CAa78SVgCeWfGqqI4FfYYYuNFb
  6386. https://mail.openjdk.java.net/pipermail/core-libs-dev/2021-November/083536.html
  6387. https://github.com/openjdk/jdk/pull/3402 (Java implementation)
  6388. https://github.com/abolz/Drachennest (C++ implementation)
  6389. See also:
  6390. Dragonbox: A New Floating-Point Binary-to-Decimal Conversion Algorithm, 2022.
  6391. https://github.com/jk-jeon/dragonbox/blob/master/other_files/Dragonbox.pdf
  6392. https://github.com/jk-jeon/dragonbox
  6393. @param sig_raw The raw value of significand in IEEE 754 format.
  6394. @param exp_raw The raw value of exponent in IEEE 754 format.
  6395. @param sig_bin The decoded value of significand in binary.
  6396. @param exp_bin The decoded value of exponent in binary.
  6397. @param sig_dec The output value of significand in decimal.
  6398. @param exp_dec The output value of exponent in decimal.
  6399. @warning The input double number should not be 0, inf, nan.
  6400. */
  6401. static_inline void f64_bin_to_dec(u64 sig_raw, u32 exp_raw,
  6402. u64 sig_bin, i32 exp_bin,
  6403. u64 *sig_dec, i32 *exp_dec) {
  6404. bool is_even, regular_spacing, u_inside, w_inside, round_up;
  6405. u64 s, sp, cb, cbl, cbr, vb, vbl, vbr, pow10hi, pow10lo, upper, lower, mid;
  6406. i32 k, h, exp10;
  6407. is_even = !(sig_bin & 1);
  6408. regular_spacing = (sig_raw == 0 && exp_raw > 1);
  6409. cbl = 4 * sig_bin - 2 + regular_spacing;
  6410. cb = 4 * sig_bin;
  6411. cbr = 4 * sig_bin + 2;
  6412. /* exp_bin: [-1074, 971] */
  6413. /* k = regular_spacing ? floor(log10(pow(2, exp_bin))) */
  6414. /* : floor(log10(pow(2, exp_bin) * 3.0 / 4.0)) */
  6415. /* = regular_spacing ? floor(exp_bin * log10(2)) */
  6416. /* : floor(exp_bin * log10(2) + log10(3.0 / 4.0)) */
  6417. k = (i32)(exp_bin * 315653 - (regular_spacing ? 131237 : 0)) >> 20;
  6418. /* k: [-324, 292] */
  6419. /* h = exp_bin + floor(log2(pow(10, e))) */
  6420. /* = exp_bin + floor(log2(10) * e) */
  6421. exp10 = -k;
  6422. h = exp_bin + ((exp10 * 217707) >> 16) + 1;
  6423. pow10_table_get_sig(exp10, &pow10hi, &pow10lo);
  6424. pow10lo += (exp10 < POW10_SIG_TABLE_MIN_EXACT_EXP ||
  6425. exp10 > POW10_SIG_TABLE_MAX_EXACT_EXP);
  6426. vbl = round_to_odd(pow10hi, pow10lo, cbl << h);
  6427. vb = round_to_odd(pow10hi, pow10lo, cb << h);
  6428. vbr = round_to_odd(pow10hi, pow10lo, cbr << h);
  6429. lower = vbl + !is_even;
  6430. upper = vbr - !is_even;
  6431. s = vb / 4;
  6432. if (s >= 10) {
  6433. sp = s / 10;
  6434. u_inside = (lower <= 40 * sp);
  6435. w_inside = (upper >= 40 * sp + 40);
  6436. if (u_inside != w_inside) {
  6437. *sig_dec = sp + w_inside;
  6438. *exp_dec = k + 1;
  6439. return;
  6440. }
  6441. }
  6442. u_inside = (lower <= 4 * s);
  6443. w_inside = (upper >= 4 * s + 4);
  6444. mid = 4 * s + 2;
  6445. round_up = (vb > mid) || (vb == mid && (s & 1) != 0);
  6446. *sig_dec = s + ((u_inside != w_inside) ? w_inside : round_up);
  6447. *exp_dec = k;
  6448. }
  6449. /**
  6450. Write a double number (requires 32 bytes buffer).
  6451. We follows the ECMAScript specification to print floating point numbers,
  6452. but with the following changes:
  6453. 1. Keep the negative sign of 0.0 to preserve input information.
  6454. 2. Keep decimal point to indicate the number is floating point.
  6455. 3. Remove positive sign of exponent part.
  6456. */
  6457. static_inline u8 *write_f64_raw(u8 *buf, u64 raw, yyjson_write_flag flg) {
  6458. u64 sig_bin, sig_dec, sig_raw;
  6459. i32 exp_bin, exp_dec, sig_len, dot_pos, i, max;
  6460. u32 exp_raw, hi, lo;
  6461. u8 *hdr, *num_hdr, *num_end, *dot_end;
  6462. bool sign;
  6463. /* decode raw bytes from IEEE-754 double format. */
  6464. sign = (bool)(raw >> (F64_BITS - 1));
  6465. sig_raw = raw & F64_SIG_MASK;
  6466. exp_raw = (u32)((raw & F64_EXP_MASK) >> F64_SIG_BITS);
  6467. /* return inf and nan */
  6468. if (unlikely(exp_raw == ((u32)1 << F64_EXP_BITS) - 1)) {
  6469. if (has_write_flag(INF_AND_NAN_AS_NULL)) {
  6470. byte_copy_4(buf, "null");
  6471. return buf + 4;
  6472. }
  6473. else if (has_write_flag(ALLOW_INF_AND_NAN)) {
  6474. if (sig_raw == 0) {
  6475. buf[0] = '-';
  6476. buf += sign;
  6477. byte_copy_8(buf, "Infinity");
  6478. buf += 8;
  6479. return buf;
  6480. } else {
  6481. byte_copy_4(buf, "NaN");
  6482. return buf + 3;
  6483. }
  6484. }
  6485. return NULL;
  6486. }
  6487. /* add sign for all finite double value, including 0.0 and inf */
  6488. buf[0] = '-';
  6489. buf += sign;
  6490. hdr = buf;
  6491. /* return zero */
  6492. if ((raw << 1) == 0) {
  6493. byte_copy_4(buf, "0.0");
  6494. buf += 3;
  6495. return buf;
  6496. }
  6497. if (likely(exp_raw != 0)) {
  6498. /* normal number */
  6499. sig_bin = sig_raw | ((u64)1 << F64_SIG_BITS);
  6500. exp_bin = (i32)exp_raw - F64_EXP_BIAS - F64_SIG_BITS;
  6501. /* fast path for small integer number without fraction */
  6502. if (-F64_SIG_BITS <= exp_bin && exp_bin <= 0) {
  6503. if (u64_tz_bits(sig_bin) >= (u32)-exp_bin) {
  6504. /* number is integer in range 1 to 0x1FFFFFFFFFFFFF */
  6505. sig_dec = sig_bin >> -exp_bin;
  6506. buf = write_u64_len_1_to_16(sig_dec, buf);
  6507. byte_copy_2(buf, ".0");
  6508. buf += 2;
  6509. return buf;
  6510. }
  6511. }
  6512. /* binary to decimal */
  6513. f64_bin_to_dec(sig_raw, exp_raw, sig_bin, exp_bin, &sig_dec, &exp_dec);
  6514. /* the sig length is 15 to 17 */
  6515. sig_len = 17;
  6516. sig_len -= (sig_dec < (u64)100000000 * 100000000);
  6517. sig_len -= (sig_dec < (u64)100000000 * 10000000);
  6518. /* the decimal point position relative to the first digit */
  6519. dot_pos = sig_len + exp_dec;
  6520. if (-6 < dot_pos && dot_pos <= 21) {
  6521. /* no need to write exponent part */
  6522. if (dot_pos <= 0) {
  6523. /* dot before first digit */
  6524. /* such as 0.1234, 0.000001234 */
  6525. num_hdr = hdr + (2 - dot_pos);
  6526. num_end = write_u64_len_15_to_17_trim(num_hdr, sig_dec);
  6527. hdr[0] = '0';
  6528. hdr[1] = '.';
  6529. hdr += 2;
  6530. max = -dot_pos;
  6531. for (i = 0; i < max; i++) hdr[i] = '0';
  6532. return num_end;
  6533. } else {
  6534. /* dot after first digit */
  6535. /* such as 1.234, 1234.0, 123400000000000000000.0 */
  6536. memset(hdr + 0, '0', 8);
  6537. memset(hdr + 8, '0', 8);
  6538. memset(hdr + 16, '0', 8);
  6539. num_hdr = hdr + 1;
  6540. num_end = write_u64_len_15_to_17_trim(num_hdr, sig_dec);
  6541. for (i = 0; i < dot_pos; i++) hdr[i] = hdr[i + 1];
  6542. hdr[dot_pos] = '.';
  6543. dot_end = hdr + dot_pos + 2;
  6544. return dot_end < num_end ? num_end : dot_end;
  6545. }
  6546. } else {
  6547. /* write with scientific notation */
  6548. /* such as 1.234e56 */
  6549. u8 *end = write_u64_len_15_to_17_trim(buf + 1, sig_dec);
  6550. end -= (end == buf + 2); /* remove '.0', e.g. 2.0e34 -> 2e34 */
  6551. exp_dec += sig_len - 1;
  6552. hdr[0] = hdr[1];
  6553. hdr[1] = '.';
  6554. end[0] = 'e';
  6555. buf = write_f64_exp(exp_dec, end + 1);
  6556. return buf;
  6557. }
  6558. } else {
  6559. /* subnormal number */
  6560. sig_bin = sig_raw;
  6561. exp_bin = 1 - F64_EXP_BIAS - F64_SIG_BITS;
  6562. /* binary to decimal */
  6563. f64_bin_to_dec(sig_raw, exp_raw, sig_bin, exp_bin, &sig_dec, &exp_dec);
  6564. /* write significand part */
  6565. buf = write_u64_len_1_to_17(sig_dec, buf + 1);
  6566. hdr[0] = hdr[1];
  6567. hdr[1] = '.';
  6568. do {
  6569. buf--;
  6570. exp_dec++;
  6571. } while (*buf == '0');
  6572. exp_dec += (i32)(buf - hdr - 2);
  6573. buf += (*buf != '.');
  6574. buf[0] = 'e';
  6575. buf++;
  6576. /* write exponent part */
  6577. buf[0] = '-';
  6578. buf++;
  6579. exp_dec = -exp_dec;
  6580. hi = ((u32)exp_dec * 656) >> 16; /* exp / 100 */
  6581. lo = (u32)exp_dec - hi * 100; /* exp % 100 */
  6582. buf[0] = (u8)((u8)hi + (u8)'0');
  6583. byte_copy_2(buf + 1, digit_table + lo * 2);
  6584. buf += 3;
  6585. return buf;
  6586. }
  6587. }
  6588. #else /* FP_WRITER */
  6589. /** Write a double number (requires 32 bytes buffer). */
  6590. static_inline u8 *write_f64_raw(u8 *buf, u64 raw, yyjson_write_flag flg) {
  6591. /*
  6592. For IEEE 754, `DBL_DECIMAL_DIG` is 17 for round-trip.
  6593. For non-IEEE formats, 17 is used to avoid buffer overflow,
  6594. round-trip is not guaranteed.
  6595. */
  6596. #if defined(DBL_DECIMAL_DIG) && DBL_DECIMAL_DIG != 17
  6597. int dig = DBL_DECIMAL_DIG > 17 ? 17 : DBL_DECIMAL_DIG;
  6598. #else
  6599. int dig = 17;
  6600. #endif
  6601. /*
  6602. The snprintf() function is locale-dependent. For currently known locales,
  6603. (en, zh, ja, ko, am, he, hi) use '.' as the decimal point, while other
  6604. locales use ',' as the decimal point. we need to replace ',' with '.'
  6605. to avoid the locale setting.
  6606. */
  6607. f64 val = f64_from_raw(raw);
  6608. #if YYJSON_MSC_VER >= 1400
  6609. int len = sprintf_s((char *)buf, 32, "%.*g", dig, val);
  6610. #elif defined(snprintf) || (YYJSON_STDC_VER >= 199901L)
  6611. int len = snprintf((char *)buf, 32, "%.*g", dig, val);
  6612. #else
  6613. int len = sprintf((char *)buf, "%.*g", dig, val);
  6614. #endif
  6615. u8 *cur = buf;
  6616. if (unlikely(len < 1)) return NULL;
  6617. cur += (*cur == '-');
  6618. if (unlikely(!digi_is_digit(*cur))) {
  6619. /* nan, inf, or bad output */
  6620. if (has_write_flag(INF_AND_NAN_AS_NULL)) {
  6621. byte_copy_4(buf, "null");
  6622. return buf + 4;
  6623. }
  6624. else if (has_write_flag(ALLOW_INF_AND_NAN)) {
  6625. if (*cur == 'i') {
  6626. byte_copy_8(cur, "Infinity");
  6627. cur += 8;
  6628. return cur;
  6629. } else if (*cur == 'n') {
  6630. byte_copy_4(buf, "NaN");
  6631. return buf + 3;
  6632. }
  6633. }
  6634. return NULL;
  6635. } else {
  6636. /* finite number */
  6637. int i = 0;
  6638. bool fp = false;
  6639. for (; i < len; i++) {
  6640. if (buf[i] == ',') buf[i] = '.';
  6641. if (digi_is_fp((u8)buf[i])) fp = true;
  6642. }
  6643. if (!fp) {
  6644. buf[len++] = '.';
  6645. buf[len++] = '0';
  6646. }
  6647. }
  6648. return buf + len;
  6649. }
  6650. #endif /* FP_WRITER */
  6651. /** Write a JSON number (requires 32 bytes buffer). */
  6652. static_inline u8 *write_number(u8 *cur, yyjson_val *val,
  6653. yyjson_write_flag flg) {
  6654. if (val->tag & YYJSON_SUBTYPE_REAL) {
  6655. u64 raw = val->uni.u64;
  6656. return write_f64_raw(cur, raw, flg);
  6657. } else {
  6658. u64 pos = val->uni.u64;
  6659. u64 neg = ~pos + 1;
  6660. usize sgn = ((val->tag & YYJSON_SUBTYPE_SINT) > 0) & ((i64)pos < 0);
  6661. *cur = '-';
  6662. return write_u64(sgn ? neg : pos, cur + sgn);
  6663. }
  6664. }
  6665. /*==============================================================================
  6666. * String Writer
  6667. *============================================================================*/
  6668. /** Character encode type, if (type > CHAR_ENC_ERR_1) bytes = type / 2; */
  6669. typedef u8 char_enc_type;
  6670. #define CHAR_ENC_CPY_1 0 /* 1-byte UTF-8, copy. */
  6671. #define CHAR_ENC_ERR_1 1 /* 1-byte UTF-8, error. */
  6672. #define CHAR_ENC_ESC_A 2 /* 1-byte ASCII, escaped as '\x'. */
  6673. #define CHAR_ENC_ESC_1 3 /* 1-byte UTF-8, escaped as '\uXXXX'. */
  6674. #define CHAR_ENC_CPY_2 4 /* 2-byte UTF-8, copy. */
  6675. #define CHAR_ENC_ESC_2 5 /* 2-byte UTF-8, escaped as '\uXXXX'. */
  6676. #define CHAR_ENC_CPY_3 6 /* 3-byte UTF-8, copy. */
  6677. #define CHAR_ENC_ESC_3 7 /* 3-byte UTF-8, escaped as '\uXXXX'. */
  6678. #define CHAR_ENC_CPY_4 8 /* 4-byte UTF-8, copy. */
  6679. #define CHAR_ENC_ESC_4 9 /* 4-byte UTF-8, escaped as '\uXXXX\uXXXX'. */
  6680. /** Character encode type table: don't escape unicode, don't escape '/'.
  6681. (generate with misc/make_tables.c) */
  6682. static const char_enc_type enc_table_cpy[256] = {
  6683. 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 2, 2, 3, 3,
  6684. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  6685. 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6686. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6687. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6688. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0,
  6689. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6690. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6691. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6692. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6693. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6694. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6695. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  6696. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  6697. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  6698. 8, 8, 8, 8, 8, 8, 8, 8, 1, 1, 1, 1, 1, 1, 1, 1
  6699. };
  6700. /** Character encode type table: don't escape unicode, escape '/'.
  6701. (generate with misc/make_tables.c) */
  6702. static const char_enc_type enc_table_cpy_slash[256] = {
  6703. 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 2, 2, 3, 3,
  6704. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  6705. 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
  6706. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6707. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6708. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0,
  6709. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6710. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6711. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6712. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6713. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6714. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6715. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  6716. 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
  6717. 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  6718. 8, 8, 8, 8, 8, 8, 8, 8, 1, 1, 1, 1, 1, 1, 1, 1
  6719. };
  6720. /** Character encode type table: escape unicode, don't escape '/'.
  6721. (generate with misc/make_tables.c) */
  6722. static const char_enc_type enc_table_esc[256] = {
  6723. 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 2, 2, 3, 3,
  6724. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  6725. 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6726. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6727. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6728. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0,
  6729. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6730. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6731. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6732. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6733. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6734. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6735. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  6736. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  6737. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  6738. 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1, 1, 1, 1, 1, 1
  6739. };
  6740. /** Character encode type table: escape unicode, escape '/'.
  6741. (generate with misc/make_tables.c) */
  6742. static const char_enc_type enc_table_esc_slash[256] = {
  6743. 3, 3, 3, 3, 3, 3, 3, 3, 2, 2, 2, 3, 2, 2, 3, 3,
  6744. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  6745. 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2,
  6746. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6747. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6748. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0,
  6749. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6750. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6751. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6752. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6753. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6754. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  6755. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  6756. 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,
  6757. 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
  6758. 9, 9, 9, 9, 9, 9, 9, 9, 1, 1, 1, 1, 1, 1, 1, 1
  6759. };
  6760. /** Escaped hex character table: ["00" "01" "02" ... "FD" "FE" "FF"].
  6761. (generate with misc/make_tables.c) */
  6762. yyjson_align(2)
  6763. static const u8 esc_hex_char_table[512] = {
  6764. '0', '0', '0', '1', '0', '2', '0', '3',
  6765. '0', '4', '0', '5', '0', '6', '0', '7',
  6766. '0', '8', '0', '9', '0', 'A', '0', 'B',
  6767. '0', 'C', '0', 'D', '0', 'E', '0', 'F',
  6768. '1', '0', '1', '1', '1', '2', '1', '3',
  6769. '1', '4', '1', '5', '1', '6', '1', '7',
  6770. '1', '8', '1', '9', '1', 'A', '1', 'B',
  6771. '1', 'C', '1', 'D', '1', 'E', '1', 'F',
  6772. '2', '0', '2', '1', '2', '2', '2', '3',
  6773. '2', '4', '2', '5', '2', '6', '2', '7',
  6774. '2', '8', '2', '9', '2', 'A', '2', 'B',
  6775. '2', 'C', '2', 'D', '2', 'E', '2', 'F',
  6776. '3', '0', '3', '1', '3', '2', '3', '3',
  6777. '3', '4', '3', '5', '3', '6', '3', '7',
  6778. '3', '8', '3', '9', '3', 'A', '3', 'B',
  6779. '3', 'C', '3', 'D', '3', 'E', '3', 'F',
  6780. '4', '0', '4', '1', '4', '2', '4', '3',
  6781. '4', '4', '4', '5', '4', '6', '4', '7',
  6782. '4', '8', '4', '9', '4', 'A', '4', 'B',
  6783. '4', 'C', '4', 'D', '4', 'E', '4', 'F',
  6784. '5', '0', '5', '1', '5', '2', '5', '3',
  6785. '5', '4', '5', '5', '5', '6', '5', '7',
  6786. '5', '8', '5', '9', '5', 'A', '5', 'B',
  6787. '5', 'C', '5', 'D', '5', 'E', '5', 'F',
  6788. '6', '0', '6', '1', '6', '2', '6', '3',
  6789. '6', '4', '6', '5', '6', '6', '6', '7',
  6790. '6', '8', '6', '9', '6', 'A', '6', 'B',
  6791. '6', 'C', '6', 'D', '6', 'E', '6', 'F',
  6792. '7', '0', '7', '1', '7', '2', '7', '3',
  6793. '7', '4', '7', '5', '7', '6', '7', '7',
  6794. '7', '8', '7', '9', '7', 'A', '7', 'B',
  6795. '7', 'C', '7', 'D', '7', 'E', '7', 'F',
  6796. '8', '0', '8', '1', '8', '2', '8', '3',
  6797. '8', '4', '8', '5', '8', '6', '8', '7',
  6798. '8', '8', '8', '9', '8', 'A', '8', 'B',
  6799. '8', 'C', '8', 'D', '8', 'E', '8', 'F',
  6800. '9', '0', '9', '1', '9', '2', '9', '3',
  6801. '9', '4', '9', '5', '9', '6', '9', '7',
  6802. '9', '8', '9', '9', '9', 'A', '9', 'B',
  6803. '9', 'C', '9', 'D', '9', 'E', '9', 'F',
  6804. 'A', '0', 'A', '1', 'A', '2', 'A', '3',
  6805. 'A', '4', 'A', '5', 'A', '6', 'A', '7',
  6806. 'A', '8', 'A', '9', 'A', 'A', 'A', 'B',
  6807. 'A', 'C', 'A', 'D', 'A', 'E', 'A', 'F',
  6808. 'B', '0', 'B', '1', 'B', '2', 'B', '3',
  6809. 'B', '4', 'B', '5', 'B', '6', 'B', '7',
  6810. 'B', '8', 'B', '9', 'B', 'A', 'B', 'B',
  6811. 'B', 'C', 'B', 'D', 'B', 'E', 'B', 'F',
  6812. 'C', '0', 'C', '1', 'C', '2', 'C', '3',
  6813. 'C', '4', 'C', '5', 'C', '6', 'C', '7',
  6814. 'C', '8', 'C', '9', 'C', 'A', 'C', 'B',
  6815. 'C', 'C', 'C', 'D', 'C', 'E', 'C', 'F',
  6816. 'D', '0', 'D', '1', 'D', '2', 'D', '3',
  6817. 'D', '4', 'D', '5', 'D', '6', 'D', '7',
  6818. 'D', '8', 'D', '9', 'D', 'A', 'D', 'B',
  6819. 'D', 'C', 'D', 'D', 'D', 'E', 'D', 'F',
  6820. 'E', '0', 'E', '1', 'E', '2', 'E', '3',
  6821. 'E', '4', 'E', '5', 'E', '6', 'E', '7',
  6822. 'E', '8', 'E', '9', 'E', 'A', 'E', 'B',
  6823. 'E', 'C', 'E', 'D', 'E', 'E', 'E', 'F',
  6824. 'F', '0', 'F', '1', 'F', '2', 'F', '3',
  6825. 'F', '4', 'F', '5', 'F', '6', 'F', '7',
  6826. 'F', '8', 'F', '9', 'F', 'A', 'F', 'B',
  6827. 'F', 'C', 'F', 'D', 'F', 'E', 'F', 'F'
  6828. };
  6829. /** Escaped single character table. (generate with misc/make_tables.c) */
  6830. yyjson_align(2)
  6831. static const u8 esc_single_char_table[512] = {
  6832. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6833. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6834. '\\', 'b', '\\', 't', '\\', 'n', ' ', ' ',
  6835. '\\', 'f', '\\', 'r', ' ', ' ', ' ', ' ',
  6836. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6837. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6838. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6839. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6840. ' ', ' ', ' ', ' ', '\\', '"', ' ', ' ',
  6841. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6842. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6843. ' ', ' ', ' ', ' ', ' ', ' ', '\\', '/',
  6844. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6845. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6846. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6847. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6848. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6849. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6850. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6851. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6852. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6853. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6854. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6855. '\\', '\\', ' ', ' ', ' ', ' ', ' ', ' ',
  6856. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6857. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6858. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6859. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6860. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6861. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6862. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6863. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6864. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6865. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6866. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6867. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6868. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6869. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6870. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6871. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6872. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6873. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6874. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6875. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6876. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6877. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6878. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6879. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6880. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6881. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6882. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6883. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6884. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6885. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6886. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6887. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6888. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6889. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6890. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6891. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6892. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6893. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6894. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  6895. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
  6896. };
  6897. /** Returns the encode table with options. */
  6898. static_inline const char_enc_type *get_enc_table_with_flag(
  6899. yyjson_read_flag flg) {
  6900. if (has_write_flag(ESCAPE_UNICODE)) {
  6901. if (has_write_flag(ESCAPE_SLASHES)) {
  6902. return enc_table_esc_slash;
  6903. } else {
  6904. return enc_table_esc;
  6905. }
  6906. } else {
  6907. if (has_write_flag(ESCAPE_SLASHES)) {
  6908. return enc_table_cpy_slash;
  6909. } else {
  6910. return enc_table_cpy;
  6911. }
  6912. }
  6913. }
  6914. /** Write raw string. */
  6915. static_inline u8 *write_raw(u8 *cur, const u8 *raw, usize raw_len) {
  6916. memcpy(cur, raw, raw_len);
  6917. return cur + raw_len;
  6918. }
  6919. /**
  6920. Write string no-escape.
  6921. @param cur Buffer cursor.
  6922. @param str A UTF-8 string, null-terminator is not required.
  6923. @param str_len Length of string in bytes.
  6924. @return The buffer cursor after string.
  6925. */
  6926. static_inline u8 *write_string_noesc(u8 *cur, const u8 *str, usize str_len) {
  6927. *cur++ = '"';
  6928. while (str_len >= 16) {
  6929. byte_copy_16(cur, str);
  6930. cur += 16;
  6931. str += 16;
  6932. str_len -= 16;
  6933. }
  6934. while (str_len >= 4) {
  6935. byte_copy_4(cur, str);
  6936. cur += 4;
  6937. str += 4;
  6938. str_len -= 4;
  6939. }
  6940. while (str_len) {
  6941. *cur++ = *str++;
  6942. str_len -= 1;
  6943. }
  6944. *cur++ = '"';
  6945. return cur;
  6946. }
  6947. /**
  6948. Write UTF-8 string (requires len * 6 + 2 bytes buffer).
  6949. @param cur Buffer cursor.
  6950. @param esc Escape unicode.
  6951. @param inv Allow invalid unicode.
  6952. @param str A UTF-8 string, null-terminator is not required.
  6953. @param str_len Length of string in bytes.
  6954. @param enc_table Encode type table for character.
  6955. @return The buffer cursor after string, or NULL on invalid unicode.
  6956. */
  6957. static_inline u8 *write_string(u8 *cur, bool esc, bool inv,
  6958. const u8 *str, usize str_len,
  6959. const char_enc_type *enc_table) {
  6960. /* UTF-8 character mask and pattern, see `read_string()` for details. */
  6961. #if YYJSON_ENDIAN == YYJSON_BIG_ENDIAN
  6962. const u16 b2_mask = 0xE0C0UL;
  6963. const u16 b2_patt = 0xC080UL;
  6964. const u16 b2_requ = 0x1E00UL;
  6965. const u32 b3_mask = 0xF0C0C000UL;
  6966. const u32 b3_patt = 0xE0808000UL;
  6967. const u32 b3_requ = 0x0F200000UL;
  6968. const u32 b3_erro = 0x0D200000UL;
  6969. const u32 b4_mask = 0xF8C0C0C0UL;
  6970. const u32 b4_patt = 0xF0808080UL;
  6971. const u32 b4_requ = 0x07300000UL;
  6972. const u32 b4_err0 = 0x04000000UL;
  6973. const u32 b4_err1 = 0x03300000UL;
  6974. #elif YYJSON_ENDIAN == YYJSON_LITTLE_ENDIAN
  6975. const u16 b2_mask = 0xC0E0UL;
  6976. const u16 b2_patt = 0x80C0UL;
  6977. const u16 b2_requ = 0x001EUL;
  6978. const u32 b3_mask = 0x00C0C0F0UL;
  6979. const u32 b3_patt = 0x008080E0UL;
  6980. const u32 b3_requ = 0x0000200FUL;
  6981. const u32 b3_erro = 0x0000200DUL;
  6982. const u32 b4_mask = 0xC0C0C0F8UL;
  6983. const u32 b4_patt = 0x808080F0UL;
  6984. const u32 b4_requ = 0x00003007UL;
  6985. const u32 b4_err0 = 0x00000004UL;
  6986. const u32 b4_err1 = 0x00003003UL;
  6987. #else
  6988. /* this should be evaluated at compile-time */
  6989. v16_uni b2_mask_uni = {{ 0xE0, 0xC0 }};
  6990. v16_uni b2_patt_uni = {{ 0xC0, 0x80 }};
  6991. v16_uni b2_requ_uni = {{ 0x1E, 0x00 }};
  6992. v32_uni b3_mask_uni = {{ 0xF0, 0xC0, 0xC0, 0x00 }};
  6993. v32_uni b3_patt_uni = {{ 0xE0, 0x80, 0x80, 0x00 }};
  6994. v32_uni b3_requ_uni = {{ 0x0F, 0x20, 0x00, 0x00 }};
  6995. v32_uni b3_erro_uni = {{ 0x0D, 0x20, 0x00, 0x00 }};
  6996. v32_uni b4_mask_uni = {{ 0xF8, 0xC0, 0xC0, 0xC0 }};
  6997. v32_uni b4_patt_uni = {{ 0xF0, 0x80, 0x80, 0x80 }};
  6998. v32_uni b4_requ_uni = {{ 0x07, 0x30, 0x00, 0x00 }};
  6999. v32_uni b4_err0_uni = {{ 0x04, 0x00, 0x00, 0x00 }};
  7000. v32_uni b4_err1_uni = {{ 0x03, 0x30, 0x00, 0x00 }};
  7001. u16 b2_mask = b2_mask_uni.u;
  7002. u16 b2_patt = b2_patt_uni.u;
  7003. u16 b2_requ = b2_requ_uni.u;
  7004. u32 b3_mask = b3_mask_uni.u;
  7005. u32 b3_patt = b3_patt_uni.u;
  7006. u32 b3_requ = b3_requ_uni.u;
  7007. u32 b3_erro = b3_erro_uni.u;
  7008. u32 b4_mask = b4_mask_uni.u;
  7009. u32 b4_patt = b4_patt_uni.u;
  7010. u32 b4_requ = b4_requ_uni.u;
  7011. u32 b4_err0 = b4_err0_uni.u;
  7012. u32 b4_err1 = b4_err1_uni.u;
  7013. #endif
  7014. #define is_valid_seq_2(uni) ( \
  7015. ((uni & b2_mask) == b2_patt) && \
  7016. ((uni & b2_requ)) \
  7017. )
  7018. #define is_valid_seq_3(uni) ( \
  7019. ((uni & b3_mask) == b3_patt) && \
  7020. ((tmp = (uni & b3_requ))) && \
  7021. ((tmp != b3_erro)) \
  7022. )
  7023. #define is_valid_seq_4(uni) ( \
  7024. ((uni & b4_mask) == b4_patt) && \
  7025. ((tmp = (uni & b4_requ))) && \
  7026. ((tmp & b4_err0) == 0 || (tmp & b4_err1) == 0) \
  7027. )
  7028. /* The replacement character U+FFFD, used to indicate invalid character. */
  7029. const v32 rep = {{ 'F', 'F', 'F', 'D' }};
  7030. const v32 pre = {{ '\\', 'u', '0', '0' }};
  7031. const u8 *src = str;
  7032. const u8 *end = str + str_len;
  7033. *cur++ = '"';
  7034. copy_ascii:
  7035. /*
  7036. Copy continuous ASCII, loop unrolling, same as the following code:
  7037. while (end > src) (
  7038. if (unlikely(enc_table[*src])) break;
  7039. *cur++ = *src++;
  7040. );
  7041. */
  7042. #define expr_jump(i) \
  7043. if (unlikely(enc_table[src[i]])) goto stop_char_##i;
  7044. #define expr_stop(i) \
  7045. stop_char_##i: \
  7046. memcpy(cur, src, i); \
  7047. cur += i; src += i; goto copy_utf8;
  7048. while (end - src >= 16) {
  7049. repeat16_incr(expr_jump)
  7050. byte_copy_16(cur, src);
  7051. cur += 16; src += 16;
  7052. }
  7053. while (end - src >= 4) {
  7054. repeat4_incr(expr_jump)
  7055. byte_copy_4(cur, src);
  7056. cur += 4; src += 4;
  7057. }
  7058. while (end > src) {
  7059. expr_jump(0)
  7060. *cur++ = *src++;
  7061. }
  7062. *cur++ = '"';
  7063. return cur;
  7064. repeat16_incr(expr_stop)
  7065. #undef expr_jump
  7066. #undef expr_stop
  7067. copy_utf8:
  7068. if (unlikely(src + 4 > end)) {
  7069. if (end == src) goto copy_end;
  7070. if (end - src < enc_table[*src] / 2) goto err_one;
  7071. }
  7072. switch (enc_table[*src]) {
  7073. case CHAR_ENC_CPY_1: {
  7074. *cur++ = *src++;
  7075. goto copy_ascii;
  7076. }
  7077. case CHAR_ENC_CPY_2: {
  7078. u16 v;
  7079. #if YYJSON_DISABLE_UTF8_VALIDATION
  7080. byte_copy_2(cur, src);
  7081. #else
  7082. v = byte_load_2(src);
  7083. if (unlikely(!is_valid_seq_2(v))) goto err_cpy;
  7084. byte_copy_2(cur, src);
  7085. #endif
  7086. cur += 2;
  7087. src += 2;
  7088. goto copy_utf8;
  7089. }
  7090. case CHAR_ENC_CPY_3: {
  7091. u32 v, tmp;
  7092. #if YYJSON_DISABLE_UTF8_VALIDATION
  7093. if (likely(src + 4 <= end)) {
  7094. byte_copy_4(cur, src);
  7095. } else {
  7096. byte_copy_2(cur, src);
  7097. cur[2] = src[2];
  7098. }
  7099. #else
  7100. if (likely(src + 4 <= end)) {
  7101. v = byte_load_4(src);
  7102. if (unlikely(!is_valid_seq_3(v))) goto err_cpy;
  7103. byte_copy_4(cur, src);
  7104. } else {
  7105. v = byte_load_3(src);
  7106. if (unlikely(!is_valid_seq_3(v))) goto err_cpy;
  7107. byte_copy_4(cur, &v);
  7108. }
  7109. #endif
  7110. cur += 3;
  7111. src += 3;
  7112. goto copy_utf8;
  7113. }
  7114. case CHAR_ENC_CPY_4: {
  7115. u32 v, tmp;
  7116. #if YYJSON_DISABLE_UTF8_VALIDATION
  7117. byte_copy_4(cur, src);
  7118. #else
  7119. v = byte_load_4(src);
  7120. if (unlikely(!is_valid_seq_4(v))) goto err_cpy;
  7121. byte_copy_4(cur, src);
  7122. #endif
  7123. cur += 4;
  7124. src += 4;
  7125. goto copy_utf8;
  7126. }
  7127. case CHAR_ENC_ESC_A: {
  7128. byte_copy_2(cur, &esc_single_char_table[*src * 2]);
  7129. cur += 2;
  7130. src += 1;
  7131. goto copy_utf8;
  7132. }
  7133. case CHAR_ENC_ESC_1: {
  7134. byte_copy_4(cur + 0, &pre);
  7135. byte_copy_2(cur + 4, &esc_hex_char_table[*src * 2]);
  7136. cur += 6;
  7137. src += 1;
  7138. goto copy_utf8;
  7139. }
  7140. case CHAR_ENC_ESC_2: {
  7141. u16 u, v;
  7142. #if !YYJSON_DISABLE_UTF8_VALIDATION
  7143. v = byte_load_2(src);
  7144. if (unlikely(!is_valid_seq_2(v))) goto err_esc;
  7145. #endif
  7146. u = (u16)(((u16)(src[0] & 0x1F) << 6) |
  7147. ((u16)(src[1] & 0x3F) << 0));
  7148. byte_copy_2(cur + 0, &pre);
  7149. byte_copy_2(cur + 2, &esc_hex_char_table[(u >> 8) * 2]);
  7150. byte_copy_2(cur + 4, &esc_hex_char_table[(u & 0xFF) * 2]);
  7151. cur += 6;
  7152. src += 2;
  7153. goto copy_utf8;
  7154. }
  7155. case CHAR_ENC_ESC_3: {
  7156. u16 u;
  7157. u32 v, tmp;
  7158. #if !YYJSON_DISABLE_UTF8_VALIDATION
  7159. v = byte_load_3(src);
  7160. if (unlikely(!is_valid_seq_3(v))) goto err_esc;
  7161. #endif
  7162. u = (u16)(((u16)(src[0] & 0x0F) << 12) |
  7163. ((u16)(src[1] & 0x3F) << 6) |
  7164. ((u16)(src[2] & 0x3F) << 0));
  7165. byte_copy_2(cur + 0, &pre);
  7166. byte_copy_2(cur + 2, &esc_hex_char_table[(u >> 8) * 2]);
  7167. byte_copy_2(cur + 4, &esc_hex_char_table[(u & 0xFF) * 2]);
  7168. cur += 6;
  7169. src += 3;
  7170. goto copy_utf8;
  7171. }
  7172. case CHAR_ENC_ESC_4: {
  7173. u32 hi, lo, u, v, tmp;
  7174. #if !YYJSON_DISABLE_UTF8_VALIDATION
  7175. v = byte_load_4(src);
  7176. if (unlikely(!is_valid_seq_4(v))) goto err_esc;
  7177. #endif
  7178. u = ((u32)(src[0] & 0x07) << 18) |
  7179. ((u32)(src[1] & 0x3F) << 12) |
  7180. ((u32)(src[2] & 0x3F) << 6) |
  7181. ((u32)(src[3] & 0x3F) << 0);
  7182. u -= 0x10000;
  7183. hi = (u >> 10) + 0xD800;
  7184. lo = (u & 0x3FF) + 0xDC00;
  7185. byte_copy_2(cur + 0, &pre);
  7186. byte_copy_2(cur + 2, &esc_hex_char_table[(hi >> 8) * 2]);
  7187. byte_copy_2(cur + 4, &esc_hex_char_table[(hi & 0xFF) * 2]);
  7188. byte_copy_2(cur + 6, &pre);
  7189. byte_copy_2(cur + 8, &esc_hex_char_table[(lo >> 8) * 2]);
  7190. byte_copy_2(cur + 10, &esc_hex_char_table[(lo & 0xFF) * 2]);
  7191. cur += 12;
  7192. src += 4;
  7193. goto copy_utf8;
  7194. }
  7195. case CHAR_ENC_ERR_1: {
  7196. goto err_one;
  7197. }
  7198. default: break;
  7199. }
  7200. copy_end:
  7201. *cur++ = '"';
  7202. return cur;
  7203. err_one:
  7204. if (esc) goto err_esc;
  7205. else goto err_cpy;
  7206. err_cpy:
  7207. if (!inv) return NULL;
  7208. *cur++ = *src++;
  7209. goto copy_utf8;
  7210. err_esc:
  7211. if (!inv) return NULL;
  7212. byte_copy_2(cur + 0, &pre);
  7213. byte_copy_4(cur + 2, &rep);
  7214. cur += 6;
  7215. src += 1;
  7216. goto copy_utf8;
  7217. #undef is_valid_seq_2
  7218. #undef is_valid_seq_3
  7219. #undef is_valid_seq_4
  7220. }
  7221. /*==============================================================================
  7222. * Writer Utilities
  7223. *============================================================================*/
  7224. /** Write null (requires 8 bytes buffer). */
  7225. static_inline u8 *write_null(u8 *cur) {
  7226. v64 v = {{ 'n', 'u', 'l', 'l', ',', '\n', 0, 0 }};
  7227. byte_copy_8(cur, &v);
  7228. return cur + 4;
  7229. }
  7230. /** Write bool (requires 8 bytes buffer). */
  7231. static_inline u8 *write_bool(u8 *cur, bool val) {
  7232. v64 v0 = {{ 'f', 'a', 'l', 's', 'e', ',', '\n', 0 }};
  7233. v64 v1 = {{ 't', 'r', 'u', 'e', ',', '\n', 0, 0 }};
  7234. if (val) {
  7235. byte_copy_8(cur, &v1);
  7236. } else {
  7237. byte_copy_8(cur, &v0);
  7238. }
  7239. return cur + 5 - val;
  7240. }
  7241. /** Write indent (requires level x 4 bytes buffer).
  7242. Param spaces should not larger than 4. */
  7243. static_inline u8 *write_indent(u8 *cur, usize level, usize spaces) {
  7244. while (level-- > 0) {
  7245. byte_copy_4(cur, " ");
  7246. cur += spaces;
  7247. }
  7248. return cur;
  7249. }
  7250. /** Write data to file pointer. */
  7251. static bool write_dat_to_fp(FILE *fp, u8 *dat, usize len,
  7252. yyjson_write_err *err) {
  7253. if (fwrite(dat, len, 1, fp) != 1) {
  7254. err->msg = "file writing failed";
  7255. err->code = YYJSON_WRITE_ERROR_FILE_WRITE;
  7256. return false;
  7257. }
  7258. return true;
  7259. }
  7260. /** Write data to file. */
  7261. static bool write_dat_to_file(const char *path, u8 *dat, usize len,
  7262. yyjson_write_err *err) {
  7263. #define return_err(_code, _msg) do { \
  7264. err->msg = _msg; \
  7265. err->code = YYJSON_WRITE_ERROR_##_code; \
  7266. if (file) fclose(file); \
  7267. return false; \
  7268. } while (false)
  7269. FILE *file = fopen_writeonly(path);
  7270. if (file == NULL) {
  7271. return_err(FILE_OPEN, "file opening failed");
  7272. }
  7273. if (fwrite(dat, len, 1, file) != 1) {
  7274. return_err(FILE_WRITE, "file writing failed");
  7275. }
  7276. if (fclose(file) != 0) {
  7277. file = NULL;
  7278. return_err(FILE_WRITE, "file closing failed");
  7279. }
  7280. return true;
  7281. #undef return_err
  7282. }
  7283. /*==============================================================================
  7284. * JSON Writer Implementation
  7285. *============================================================================*/
  7286. typedef struct yyjson_write_ctx {
  7287. usize tag;
  7288. } yyjson_write_ctx;
  7289. static_inline void yyjson_write_ctx_set(yyjson_write_ctx *ctx,
  7290. usize size, bool is_obj) {
  7291. ctx->tag = (size << 1) | (usize)is_obj;
  7292. }
  7293. static_inline void yyjson_write_ctx_get(yyjson_write_ctx *ctx,
  7294. usize *size, bool *is_obj) {
  7295. usize tag = ctx->tag;
  7296. *size = tag >> 1;
  7297. *is_obj = (bool)(tag & 1);
  7298. }
  7299. /** Write single JSON value. */
  7300. static_inline u8 *yyjson_write_single(yyjson_val *val,
  7301. yyjson_write_flag flg,
  7302. yyjson_alc alc,
  7303. usize *dat_len,
  7304. yyjson_write_err *err) {
  7305. #define return_err(_code, _msg) do { \
  7306. if (hdr) alc.free(alc.ctx, (void *)hdr); \
  7307. *dat_len = 0; \
  7308. err->code = YYJSON_WRITE_ERROR_##_code; \
  7309. err->msg = _msg; \
  7310. return NULL; \
  7311. } while (false)
  7312. #define incr_len(_len) do { \
  7313. hdr = (u8 *)alc.malloc(alc.ctx, _len); \
  7314. if (!hdr) goto fail_alloc; \
  7315. cur = hdr; \
  7316. } while (false)
  7317. #define check_str_len(_len) do { \
  7318. if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \
  7319. goto fail_alloc; \
  7320. } while (false)
  7321. u8 *hdr = NULL, *cur;
  7322. usize str_len;
  7323. const u8 *str_ptr;
  7324. const char_enc_type *enc_table = get_enc_table_with_flag(flg);
  7325. bool cpy = (enc_table == enc_table_cpy);
  7326. bool esc = has_write_flag(ESCAPE_UNICODE) != 0;
  7327. bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0;
  7328. switch (unsafe_yyjson_get_type(val)) {
  7329. case YYJSON_TYPE_RAW:
  7330. str_len = unsafe_yyjson_get_len(val);
  7331. str_ptr = (const u8 *)unsafe_yyjson_get_str(val);
  7332. check_str_len(str_len);
  7333. incr_len(str_len + 1);
  7334. cur = write_raw(cur, str_ptr, str_len);
  7335. break;
  7336. case YYJSON_TYPE_STR:
  7337. str_len = unsafe_yyjson_get_len(val);
  7338. str_ptr = (const u8 *)unsafe_yyjson_get_str(val);
  7339. check_str_len(str_len);
  7340. incr_len(str_len * 6 + 4);
  7341. if (likely(cpy) && unsafe_yyjson_get_subtype(val)) {
  7342. cur = write_string_noesc(cur, str_ptr, str_len);
  7343. } else {
  7344. cur = write_string(cur, esc, inv, str_ptr, str_len, enc_table);
  7345. if (unlikely(!cur)) goto fail_str;
  7346. }
  7347. break;
  7348. case YYJSON_TYPE_NUM:
  7349. incr_len(32);
  7350. cur = write_number(cur, val, flg);
  7351. if (unlikely(!cur)) goto fail_num;
  7352. break;
  7353. case YYJSON_TYPE_BOOL:
  7354. incr_len(8);
  7355. cur = write_bool(cur, unsafe_yyjson_get_bool(val));
  7356. break;
  7357. case YYJSON_TYPE_NULL:
  7358. incr_len(8);
  7359. cur = write_null(cur);
  7360. break;
  7361. case YYJSON_TYPE_ARR:
  7362. incr_len(4);
  7363. byte_copy_2(cur, "[]");
  7364. cur += 2;
  7365. break;
  7366. case YYJSON_TYPE_OBJ:
  7367. incr_len(4);
  7368. byte_copy_2(cur, "{}");
  7369. cur += 2;
  7370. break;
  7371. default:
  7372. goto fail_type;
  7373. }
  7374. *cur = '\0';
  7375. *dat_len = (usize)(cur - hdr);
  7376. memset(err, 0, sizeof(yyjson_write_err));
  7377. return hdr;
  7378. fail_alloc:
  7379. return_err(MEMORY_ALLOCATION, "memory allocation failed");
  7380. fail_type:
  7381. return_err(INVALID_VALUE_TYPE, "invalid JSON value type");
  7382. fail_num:
  7383. return_err(NAN_OR_INF, "nan or inf number is not allowed");
  7384. fail_str:
  7385. return_err(INVALID_STRING, "invalid utf-8 encoding in string");
  7386. #undef return_err
  7387. #undef check_str_len
  7388. #undef incr_len
  7389. }
  7390. /** Write JSON document minify.
  7391. The root of this document should be a non-empty container. */
  7392. static_inline u8 *yyjson_write_minify(const yyjson_val *root,
  7393. const yyjson_write_flag flg,
  7394. const yyjson_alc alc,
  7395. usize *dat_len,
  7396. yyjson_write_err *err) {
  7397. #define return_err(_code, _msg) do { \
  7398. *dat_len = 0; \
  7399. err->code = YYJSON_WRITE_ERROR_##_code; \
  7400. err->msg = _msg; \
  7401. if (hdr) alc.free(alc.ctx, hdr); \
  7402. return NULL; \
  7403. } while (false)
  7404. #define incr_len(_len) do { \
  7405. ext_len = (usize)(_len); \
  7406. if (unlikely((u8 *)(cur + ext_len) >= (u8 *)ctx)) { \
  7407. alc_inc = yyjson_max(alc_len / 2, ext_len); \
  7408. alc_inc = size_align_up(alc_inc, sizeof(yyjson_write_ctx)); \
  7409. if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \
  7410. goto fail_alloc; \
  7411. alc_len += alc_inc; \
  7412. tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \
  7413. if (unlikely(!tmp)) goto fail_alloc; \
  7414. ctx_len = (usize)(end - (u8 *)ctx); \
  7415. ctx_tmp = (yyjson_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \
  7416. memmove((void *)ctx_tmp, (void *)(tmp + ((u8 *)ctx - hdr)), ctx_len); \
  7417. ctx = ctx_tmp; \
  7418. cur = tmp + (cur - hdr); \
  7419. end = tmp + alc_len; \
  7420. hdr = tmp; \
  7421. } \
  7422. } while (false)
  7423. #define check_str_len(_len) do { \
  7424. if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \
  7425. goto fail_alloc; \
  7426. } while (false)
  7427. yyjson_val *val;
  7428. yyjson_type val_type;
  7429. usize ctn_len, ctn_len_tmp;
  7430. bool ctn_obj, ctn_obj_tmp, is_key;
  7431. u8 *hdr, *cur, *end, *tmp;
  7432. yyjson_write_ctx *ctx, *ctx_tmp;
  7433. usize alc_len, alc_inc, ctx_len, ext_len, str_len;
  7434. const u8 *str_ptr;
  7435. const char_enc_type *enc_table = get_enc_table_with_flag(flg);
  7436. bool cpy = (enc_table == enc_table_cpy);
  7437. bool esc = has_write_flag(ESCAPE_UNICODE) != 0;
  7438. bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0;
  7439. alc_len = root->uni.ofs / sizeof(yyjson_val);
  7440. alc_len = alc_len * YYJSON_WRITER_ESTIMATED_MINIFY_RATIO + 64;
  7441. alc_len = size_align_up(alc_len, sizeof(yyjson_write_ctx));
  7442. hdr = (u8 *)alc.malloc(alc.ctx, alc_len);
  7443. if (!hdr) goto fail_alloc;
  7444. cur = hdr;
  7445. end = hdr + alc_len;
  7446. ctx = (yyjson_write_ctx *)(void *)end;
  7447. doc_begin:
  7448. val = constcast(yyjson_val *)root;
  7449. val_type = unsafe_yyjson_get_type(val);
  7450. ctn_obj = (val_type == YYJSON_TYPE_OBJ);
  7451. ctn_len = unsafe_yyjson_get_len(val) << (u8)ctn_obj;
  7452. *cur++ = (u8)('[' | ((u8)ctn_obj << 5));
  7453. val++;
  7454. val_begin:
  7455. val_type = unsafe_yyjson_get_type(val);
  7456. if (val_type == YYJSON_TYPE_STR) {
  7457. is_key = ((u8)ctn_obj & (u8)~ctn_len);
  7458. str_len = unsafe_yyjson_get_len(val);
  7459. str_ptr = (const u8 *)unsafe_yyjson_get_str(val);
  7460. check_str_len(str_len);
  7461. incr_len(str_len * 6 + 16);
  7462. if (likely(cpy) && unsafe_yyjson_get_subtype(val)) {
  7463. cur = write_string_noesc(cur, str_ptr, str_len);
  7464. } else {
  7465. cur = write_string(cur, esc, inv, str_ptr, str_len, enc_table);
  7466. if (unlikely(!cur)) goto fail_str;
  7467. }
  7468. *cur++ = is_key ? ':' : ',';
  7469. goto val_end;
  7470. }
  7471. if (val_type == YYJSON_TYPE_NUM) {
  7472. incr_len(32);
  7473. cur = write_number(cur, val, flg);
  7474. if (unlikely(!cur)) goto fail_num;
  7475. *cur++ = ',';
  7476. goto val_end;
  7477. }
  7478. if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) ==
  7479. (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) {
  7480. ctn_len_tmp = unsafe_yyjson_get_len(val);
  7481. ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ);
  7482. incr_len(16);
  7483. if (unlikely(ctn_len_tmp == 0)) {
  7484. /* write empty container */
  7485. *cur++ = (u8)('[' | ((u8)ctn_obj_tmp << 5));
  7486. *cur++ = (u8)(']' | ((u8)ctn_obj_tmp << 5));
  7487. *cur++ = ',';
  7488. goto val_end;
  7489. } else {
  7490. /* push context, setup new container */
  7491. yyjson_write_ctx_set(--ctx, ctn_len, ctn_obj);
  7492. ctn_len = ctn_len_tmp << (u8)ctn_obj_tmp;
  7493. ctn_obj = ctn_obj_tmp;
  7494. *cur++ = (u8)('[' | ((u8)ctn_obj << 5));
  7495. val++;
  7496. goto val_begin;
  7497. }
  7498. }
  7499. if (val_type == YYJSON_TYPE_BOOL) {
  7500. incr_len(16);
  7501. cur = write_bool(cur, unsafe_yyjson_get_bool(val));
  7502. cur++;
  7503. goto val_end;
  7504. }
  7505. if (val_type == YYJSON_TYPE_NULL) {
  7506. incr_len(16);
  7507. cur = write_null(cur);
  7508. cur++;
  7509. goto val_end;
  7510. }
  7511. if (val_type == YYJSON_TYPE_RAW) {
  7512. str_len = unsafe_yyjson_get_len(val);
  7513. str_ptr = (const u8 *)unsafe_yyjson_get_str(val);
  7514. check_str_len(str_len);
  7515. incr_len(str_len + 2);
  7516. cur = write_raw(cur, str_ptr, str_len);
  7517. *cur++ = ',';
  7518. goto val_end;
  7519. }
  7520. goto fail_type;
  7521. val_end:
  7522. val++;
  7523. ctn_len--;
  7524. if (unlikely(ctn_len == 0)) goto ctn_end;
  7525. goto val_begin;
  7526. ctn_end:
  7527. cur--;
  7528. *cur++ = (u8)(']' | ((u8)ctn_obj << 5));
  7529. *cur++ = ',';
  7530. if (unlikely((u8 *)ctx >= end)) goto doc_end;
  7531. yyjson_write_ctx_get(ctx++, &ctn_len, &ctn_obj);
  7532. ctn_len--;
  7533. if (likely(ctn_len > 0)) {
  7534. goto val_begin;
  7535. } else {
  7536. goto ctn_end;
  7537. }
  7538. doc_end:
  7539. *--cur = '\0';
  7540. *dat_len = (usize)(cur - hdr);
  7541. memset(err, 0, sizeof(yyjson_write_err));
  7542. return hdr;
  7543. fail_alloc:
  7544. return_err(MEMORY_ALLOCATION, "memory allocation failed");
  7545. fail_type:
  7546. return_err(INVALID_VALUE_TYPE, "invalid JSON value type");
  7547. fail_num:
  7548. return_err(NAN_OR_INF, "nan or inf number is not allowed");
  7549. fail_str:
  7550. return_err(INVALID_STRING, "invalid utf-8 encoding in string");
  7551. #undef return_err
  7552. #undef incr_len
  7553. #undef check_str_len
  7554. }
  7555. /** Write JSON document pretty.
  7556. The root of this document should be a non-empty container. */
  7557. static_inline u8 *yyjson_write_pretty(const yyjson_val *root,
  7558. const yyjson_write_flag flg,
  7559. const yyjson_alc alc,
  7560. usize *dat_len,
  7561. yyjson_write_err *err) {
  7562. #define return_err(_code, _msg) do { \
  7563. *dat_len = 0; \
  7564. err->code = YYJSON_WRITE_ERROR_##_code; \
  7565. err->msg = _msg; \
  7566. if (hdr) alc.free(alc.ctx, hdr); \
  7567. return NULL; \
  7568. } while (false)
  7569. #define incr_len(_len) do { \
  7570. ext_len = (usize)(_len); \
  7571. if (unlikely((u8 *)(cur + ext_len) >= (u8 *)ctx)) { \
  7572. alc_inc = yyjson_max(alc_len / 2, ext_len); \
  7573. alc_inc = size_align_up(alc_inc, sizeof(yyjson_write_ctx)); \
  7574. if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \
  7575. goto fail_alloc; \
  7576. alc_len += alc_inc; \
  7577. tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \
  7578. if (unlikely(!tmp)) goto fail_alloc; \
  7579. ctx_len = (usize)(end - (u8 *)ctx); \
  7580. ctx_tmp = (yyjson_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \
  7581. memmove((void *)ctx_tmp, (void *)(tmp + ((u8 *)ctx - hdr)), ctx_len); \
  7582. ctx = ctx_tmp; \
  7583. cur = tmp + (cur - hdr); \
  7584. end = tmp + alc_len; \
  7585. hdr = tmp; \
  7586. } \
  7587. } while (false)
  7588. #define check_str_len(_len) do { \
  7589. if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \
  7590. goto fail_alloc; \
  7591. } while (false)
  7592. yyjson_val *val;
  7593. yyjson_type val_type;
  7594. usize ctn_len, ctn_len_tmp;
  7595. bool ctn_obj, ctn_obj_tmp, is_key, no_indent;
  7596. u8 *hdr, *cur, *end, *tmp;
  7597. yyjson_write_ctx *ctx, *ctx_tmp;
  7598. usize alc_len, alc_inc, ctx_len, ext_len, str_len, level;
  7599. const u8 *str_ptr;
  7600. const char_enc_type *enc_table = get_enc_table_with_flag(flg);
  7601. bool cpy = (enc_table == enc_table_cpy);
  7602. bool esc = has_write_flag(ESCAPE_UNICODE) != 0;
  7603. bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0;
  7604. usize spaces = has_write_flag(PRETTY_TWO_SPACES) ? 2 : 4;
  7605. alc_len = root->uni.ofs / sizeof(yyjson_val);
  7606. alc_len = alc_len * YYJSON_WRITER_ESTIMATED_PRETTY_RATIO + 64;
  7607. alc_len = size_align_up(alc_len, sizeof(yyjson_write_ctx));
  7608. hdr = (u8 *)alc.malloc(alc.ctx, alc_len);
  7609. if (!hdr) goto fail_alloc;
  7610. cur = hdr;
  7611. end = hdr + alc_len;
  7612. ctx = (yyjson_write_ctx *)(void *)end;
  7613. doc_begin:
  7614. val = constcast(yyjson_val *)root;
  7615. val_type = unsafe_yyjson_get_type(val);
  7616. ctn_obj = (val_type == YYJSON_TYPE_OBJ);
  7617. ctn_len = unsafe_yyjson_get_len(val) << (u8)ctn_obj;
  7618. *cur++ = (u8)('[' | ((u8)ctn_obj << 5));
  7619. *cur++ = '\n';
  7620. val++;
  7621. level = 1;
  7622. val_begin:
  7623. val_type = unsafe_yyjson_get_type(val);
  7624. if (val_type == YYJSON_TYPE_STR) {
  7625. is_key = (bool)((u8)ctn_obj & (u8)~ctn_len);
  7626. no_indent = (bool)((u8)ctn_obj & (u8)ctn_len);
  7627. str_len = unsafe_yyjson_get_len(val);
  7628. str_ptr = (const u8 *)unsafe_yyjson_get_str(val);
  7629. check_str_len(str_len);
  7630. incr_len(str_len * 6 + 16 + (no_indent ? 0 : level * 4));
  7631. cur = write_indent(cur, no_indent ? 0 : level, spaces);
  7632. if (likely(cpy) && unsafe_yyjson_get_subtype(val)) {
  7633. cur = write_string_noesc(cur, str_ptr, str_len);
  7634. } else {
  7635. cur = write_string(cur, esc, inv, str_ptr, str_len, enc_table);
  7636. if (unlikely(!cur)) goto fail_str;
  7637. }
  7638. *cur++ = is_key ? ':' : ',';
  7639. *cur++ = is_key ? ' ' : '\n';
  7640. goto val_end;
  7641. }
  7642. if (val_type == YYJSON_TYPE_NUM) {
  7643. no_indent = (bool)((u8)ctn_obj & (u8)ctn_len);
  7644. incr_len(32 + (no_indent ? 0 : level * 4));
  7645. cur = write_indent(cur, no_indent ? 0 : level, spaces);
  7646. cur = write_number(cur, val, flg);
  7647. if (unlikely(!cur)) goto fail_num;
  7648. *cur++ = ',';
  7649. *cur++ = '\n';
  7650. goto val_end;
  7651. }
  7652. if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) ==
  7653. (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) {
  7654. no_indent = (bool)((u8)ctn_obj & (u8)ctn_len);
  7655. ctn_len_tmp = unsafe_yyjson_get_len(val);
  7656. ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ);
  7657. if (unlikely(ctn_len_tmp == 0)) {
  7658. /* write empty container */
  7659. incr_len(16 + (no_indent ? 0 : level * 4));
  7660. cur = write_indent(cur, no_indent ? 0 : level, spaces);
  7661. *cur++ = (u8)('[' | ((u8)ctn_obj_tmp << 5));
  7662. *cur++ = (u8)(']' | ((u8)ctn_obj_tmp << 5));
  7663. *cur++ = ',';
  7664. *cur++ = '\n';
  7665. goto val_end;
  7666. } else {
  7667. /* push context, setup new container */
  7668. incr_len(32 + (no_indent ? 0 : level * 4));
  7669. yyjson_write_ctx_set(--ctx, ctn_len, ctn_obj);
  7670. ctn_len = ctn_len_tmp << (u8)ctn_obj_tmp;
  7671. ctn_obj = ctn_obj_tmp;
  7672. cur = write_indent(cur, no_indent ? 0 : level, spaces);
  7673. level++;
  7674. *cur++ = (u8)('[' | ((u8)ctn_obj << 5));
  7675. *cur++ = '\n';
  7676. val++;
  7677. goto val_begin;
  7678. }
  7679. }
  7680. if (val_type == YYJSON_TYPE_BOOL) {
  7681. no_indent = (bool)((u8)ctn_obj & (u8)ctn_len);
  7682. incr_len(16 + (no_indent ? 0 : level * 4));
  7683. cur = write_indent(cur, no_indent ? 0 : level, spaces);
  7684. cur = write_bool(cur, unsafe_yyjson_get_bool(val));
  7685. cur += 2;
  7686. goto val_end;
  7687. }
  7688. if (val_type == YYJSON_TYPE_NULL) {
  7689. no_indent = (bool)((u8)ctn_obj & (u8)ctn_len);
  7690. incr_len(16 + (no_indent ? 0 : level * 4));
  7691. cur = write_indent(cur, no_indent ? 0 : level, spaces);
  7692. cur = write_null(cur);
  7693. cur += 2;
  7694. goto val_end;
  7695. }
  7696. if (val_type == YYJSON_TYPE_RAW) {
  7697. str_len = unsafe_yyjson_get_len(val);
  7698. str_ptr = (const u8 *)unsafe_yyjson_get_str(val);
  7699. check_str_len(str_len);
  7700. incr_len(str_len + 3);
  7701. cur = write_raw(cur, str_ptr, str_len);
  7702. *cur++ = ',';
  7703. *cur++ = '\n';
  7704. goto val_end;
  7705. }
  7706. goto fail_type;
  7707. val_end:
  7708. val++;
  7709. ctn_len--;
  7710. if (unlikely(ctn_len == 0)) goto ctn_end;
  7711. goto val_begin;
  7712. ctn_end:
  7713. cur -= 2;
  7714. *cur++ = '\n';
  7715. incr_len(level * 4);
  7716. cur = write_indent(cur, --level, spaces);
  7717. *cur++ = (u8)(']' | ((u8)ctn_obj << 5));
  7718. if (unlikely((u8 *)ctx >= end)) goto doc_end;
  7719. yyjson_write_ctx_get(ctx++, &ctn_len, &ctn_obj);
  7720. ctn_len--;
  7721. *cur++ = ',';
  7722. *cur++ = '\n';
  7723. if (likely(ctn_len > 0)) {
  7724. goto val_begin;
  7725. } else {
  7726. goto ctn_end;
  7727. }
  7728. doc_end:
  7729. *cur = '\0';
  7730. *dat_len = (usize)(cur - hdr);
  7731. memset(err, 0, sizeof(yyjson_write_err));
  7732. return hdr;
  7733. fail_alloc:
  7734. return_err(MEMORY_ALLOCATION, "memory allocation failed");
  7735. fail_type:
  7736. return_err(INVALID_VALUE_TYPE, "invalid JSON value type");
  7737. fail_num:
  7738. return_err(NAN_OR_INF, "nan or inf number is not allowed");
  7739. fail_str:
  7740. return_err(INVALID_STRING, "invalid utf-8 encoding in string");
  7741. #undef return_err
  7742. #undef incr_len
  7743. #undef check_str_len
  7744. }
  7745. char *yyjson_val_write_opts(const yyjson_val *val,
  7746. yyjson_write_flag flg,
  7747. const yyjson_alc *alc_ptr,
  7748. usize *dat_len,
  7749. yyjson_write_err *err) {
  7750. yyjson_write_err dummy_err;
  7751. usize dummy_dat_len;
  7752. yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC;
  7753. yyjson_val *root = constcast(yyjson_val *)val;
  7754. err = err ? err : &dummy_err;
  7755. dat_len = dat_len ? dat_len : &dummy_dat_len;
  7756. if (unlikely(!root)) {
  7757. *dat_len = 0;
  7758. err->msg = "input JSON is NULL";
  7759. err->code = YYJSON_READ_ERROR_INVALID_PARAMETER;
  7760. return NULL;
  7761. }
  7762. if (!unsafe_yyjson_is_ctn(root) || unsafe_yyjson_get_len(root) == 0) {
  7763. return (char *)yyjson_write_single(root, flg, alc, dat_len, err);
  7764. } else if (flg & (YYJSON_WRITE_PRETTY | YYJSON_WRITE_PRETTY_TWO_SPACES)) {
  7765. return (char *)yyjson_write_pretty(root, flg, alc, dat_len, err);
  7766. } else {
  7767. return (char *)yyjson_write_minify(root, flg, alc, dat_len, err);
  7768. }
  7769. }
  7770. char *yyjson_write_opts(const yyjson_doc *doc,
  7771. yyjson_write_flag flg,
  7772. const yyjson_alc *alc_ptr,
  7773. usize *dat_len,
  7774. yyjson_write_err *err) {
  7775. yyjson_val *root = doc ? doc->root : NULL;
  7776. return yyjson_val_write_opts(root, flg, alc_ptr, dat_len, err);
  7777. }
  7778. bool yyjson_val_write_file(const char *path,
  7779. const yyjson_val *val,
  7780. yyjson_write_flag flg,
  7781. const yyjson_alc *alc_ptr,
  7782. yyjson_write_err *err) {
  7783. yyjson_write_err dummy_err;
  7784. u8 *dat;
  7785. usize dat_len = 0;
  7786. yyjson_val *root = constcast(yyjson_val *)val;
  7787. bool suc;
  7788. alc_ptr = alc_ptr ? alc_ptr : &YYJSON_DEFAULT_ALC;
  7789. err = err ? err : &dummy_err;
  7790. if (unlikely(!path || !*path)) {
  7791. err->msg = "input path is invalid";
  7792. err->code = YYJSON_READ_ERROR_INVALID_PARAMETER;
  7793. return false;
  7794. }
  7795. dat = (u8 *)yyjson_val_write_opts(root, flg, alc_ptr, &dat_len, err);
  7796. if (unlikely(!dat)) return false;
  7797. suc = write_dat_to_file(path, dat, dat_len, err);
  7798. alc_ptr->free(alc_ptr->ctx, dat);
  7799. return suc;
  7800. }
  7801. bool yyjson_val_write_fp(FILE *fp,
  7802. const yyjson_val *val,
  7803. yyjson_write_flag flg,
  7804. const yyjson_alc *alc_ptr,
  7805. yyjson_write_err *err) {
  7806. yyjson_write_err dummy_err;
  7807. u8 *dat;
  7808. usize dat_len = 0;
  7809. yyjson_val *root = constcast(yyjson_val *)val;
  7810. bool suc;
  7811. alc_ptr = alc_ptr ? alc_ptr : &YYJSON_DEFAULT_ALC;
  7812. err = err ? err : &dummy_err;
  7813. if (unlikely(!fp)) {
  7814. err->msg = "input fp is invalid";
  7815. err->code = YYJSON_READ_ERROR_INVALID_PARAMETER;
  7816. return false;
  7817. }
  7818. dat = (u8 *)yyjson_val_write_opts(root, flg, alc_ptr, &dat_len, err);
  7819. if (unlikely(!dat)) return false;
  7820. suc = write_dat_to_fp(fp, dat, dat_len, err);
  7821. alc_ptr->free(alc_ptr->ctx, dat);
  7822. return suc;
  7823. }
  7824. bool yyjson_write_file(const char *path,
  7825. const yyjson_doc *doc,
  7826. yyjson_write_flag flg,
  7827. const yyjson_alc *alc_ptr,
  7828. yyjson_write_err *err) {
  7829. yyjson_val *root = doc ? doc->root : NULL;
  7830. return yyjson_val_write_file(path, root, flg, alc_ptr, err);
  7831. }
  7832. bool yyjson_write_fp(FILE *fp,
  7833. const yyjson_doc *doc,
  7834. yyjson_write_flag flg,
  7835. const yyjson_alc *alc_ptr,
  7836. yyjson_write_err *err) {
  7837. yyjson_val *root = doc ? doc->root : NULL;
  7838. return yyjson_val_write_fp(fp, root, flg, alc_ptr, err);
  7839. }
  7840. /*==============================================================================
  7841. * Mutable JSON Writer Implementation
  7842. *============================================================================*/
  7843. typedef struct yyjson_mut_write_ctx {
  7844. usize tag;
  7845. yyjson_mut_val *ctn;
  7846. } yyjson_mut_write_ctx;
  7847. static_inline void yyjson_mut_write_ctx_set(yyjson_mut_write_ctx *ctx,
  7848. yyjson_mut_val *ctn,
  7849. usize size, bool is_obj) {
  7850. ctx->tag = (size << 1) | (usize)is_obj;
  7851. ctx->ctn = ctn;
  7852. }
  7853. static_inline void yyjson_mut_write_ctx_get(yyjson_mut_write_ctx *ctx,
  7854. yyjson_mut_val **ctn,
  7855. usize *size, bool *is_obj) {
  7856. usize tag = ctx->tag;
  7857. *size = tag >> 1;
  7858. *is_obj = (bool)(tag & 1);
  7859. *ctn = ctx->ctn;
  7860. }
  7861. /** Get the estimated number of values for the mutable JSON document. */
  7862. static_inline usize yyjson_mut_doc_estimated_val_num(
  7863. const yyjson_mut_doc *doc) {
  7864. usize sum = 0;
  7865. yyjson_val_chunk *chunk = doc->val_pool.chunks;
  7866. while (chunk) {
  7867. sum += chunk->chunk_size / sizeof(yyjson_mut_val) - 1;
  7868. if (chunk == doc->val_pool.chunks) {
  7869. sum -= (usize)(doc->val_pool.end - doc->val_pool.cur);
  7870. }
  7871. chunk = chunk->next;
  7872. }
  7873. return sum;
  7874. }
  7875. /** Write single JSON value. */
  7876. static_inline u8 *yyjson_mut_write_single(yyjson_mut_val *val,
  7877. yyjson_write_flag flg,
  7878. yyjson_alc alc,
  7879. usize *dat_len,
  7880. yyjson_write_err *err) {
  7881. return yyjson_write_single((yyjson_val *)val, flg, alc, dat_len, err);
  7882. }
  7883. /** Write JSON document minify.
  7884. The root of this document should be a non-empty container. */
  7885. static_inline u8 *yyjson_mut_write_minify(const yyjson_mut_val *root,
  7886. usize estimated_val_num,
  7887. yyjson_write_flag flg,
  7888. yyjson_alc alc,
  7889. usize *dat_len,
  7890. yyjson_write_err *err) {
  7891. #define return_err(_code, _msg) do { \
  7892. *dat_len = 0; \
  7893. err->code = YYJSON_WRITE_ERROR_##_code; \
  7894. err->msg = _msg; \
  7895. if (hdr) alc.free(alc.ctx, hdr); \
  7896. return NULL; \
  7897. } while (false)
  7898. #define incr_len(_len) do { \
  7899. ext_len = (usize)(_len); \
  7900. if (unlikely((u8 *)(cur + ext_len) >= (u8 *)ctx)) { \
  7901. alc_inc = yyjson_max(alc_len / 2, ext_len); \
  7902. alc_inc = size_align_up(alc_inc, sizeof(yyjson_mut_write_ctx)); \
  7903. if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \
  7904. goto fail_alloc; \
  7905. alc_len += alc_inc; \
  7906. tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \
  7907. if (unlikely(!tmp)) goto fail_alloc; \
  7908. ctx_len = (usize)(end - (u8 *)ctx); \
  7909. ctx_tmp = (yyjson_mut_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \
  7910. memmove((void *)ctx_tmp, (void *)(tmp + ((u8 *)ctx - hdr)), ctx_len); \
  7911. ctx = ctx_tmp; \
  7912. cur = tmp + (cur - hdr); \
  7913. end = tmp + alc_len; \
  7914. hdr = tmp; \
  7915. } \
  7916. } while (false)
  7917. #define check_str_len(_len) do { \
  7918. if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \
  7919. goto fail_alloc; \
  7920. } while (false)
  7921. yyjson_mut_val *val, *ctn;
  7922. yyjson_type val_type;
  7923. usize ctn_len, ctn_len_tmp;
  7924. bool ctn_obj, ctn_obj_tmp, is_key;
  7925. u8 *hdr, *cur, *end, *tmp;
  7926. yyjson_mut_write_ctx *ctx, *ctx_tmp;
  7927. usize alc_len, alc_inc, ctx_len, ext_len, str_len;
  7928. const u8 *str_ptr;
  7929. const char_enc_type *enc_table = get_enc_table_with_flag(flg);
  7930. bool cpy = (enc_table == enc_table_cpy);
  7931. bool esc = has_write_flag(ESCAPE_UNICODE) != 0;
  7932. bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0;
  7933. alc_len = estimated_val_num * YYJSON_WRITER_ESTIMATED_MINIFY_RATIO + 64;
  7934. alc_len = size_align_up(alc_len, sizeof(yyjson_mut_write_ctx));
  7935. hdr = (u8 *)alc.malloc(alc.ctx, alc_len);
  7936. if (!hdr) goto fail_alloc;
  7937. cur = hdr;
  7938. end = hdr + alc_len;
  7939. ctx = (yyjson_mut_write_ctx *)(void *)end;
  7940. doc_begin:
  7941. val = constcast(yyjson_mut_val *)root;
  7942. val_type = unsafe_yyjson_get_type(val);
  7943. ctn_obj = (val_type == YYJSON_TYPE_OBJ);
  7944. ctn_len = unsafe_yyjson_get_len(val) << (u8)ctn_obj;
  7945. *cur++ = (u8)('[' | ((u8)ctn_obj << 5));
  7946. ctn = val;
  7947. val = (yyjson_mut_val *)val->uni.ptr; /* tail */
  7948. val = ctn_obj ? val->next->next : val->next;
  7949. val_begin:
  7950. val_type = unsafe_yyjson_get_type(val);
  7951. if (val_type == YYJSON_TYPE_STR) {
  7952. is_key = ((u8)ctn_obj & (u8)~ctn_len);
  7953. str_len = unsafe_yyjson_get_len(val);
  7954. str_ptr = (const u8 *)unsafe_yyjson_get_str(val);
  7955. check_str_len(str_len);
  7956. incr_len(str_len * 6 + 16);
  7957. if (likely(cpy) && unsafe_yyjson_get_subtype(val)) {
  7958. cur = write_string_noesc(cur, str_ptr, str_len);
  7959. } else {
  7960. cur = write_string(cur, esc, inv, str_ptr, str_len, enc_table);
  7961. if (unlikely(!cur)) goto fail_str;
  7962. }
  7963. *cur++ = is_key ? ':' : ',';
  7964. goto val_end;
  7965. }
  7966. if (val_type == YYJSON_TYPE_NUM) {
  7967. incr_len(32);
  7968. cur = write_number(cur, (yyjson_val *)val, flg);
  7969. if (unlikely(!cur)) goto fail_num;
  7970. *cur++ = ',';
  7971. goto val_end;
  7972. }
  7973. if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) ==
  7974. (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) {
  7975. ctn_len_tmp = unsafe_yyjson_get_len(val);
  7976. ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ);
  7977. incr_len(16);
  7978. if (unlikely(ctn_len_tmp == 0)) {
  7979. /* write empty container */
  7980. *cur++ = (u8)('[' | ((u8)ctn_obj_tmp << 5));
  7981. *cur++ = (u8)(']' | ((u8)ctn_obj_tmp << 5));
  7982. *cur++ = ',';
  7983. goto val_end;
  7984. } else {
  7985. /* push context, setup new container */
  7986. yyjson_mut_write_ctx_set(--ctx, ctn, ctn_len, ctn_obj);
  7987. ctn_len = ctn_len_tmp << (u8)ctn_obj_tmp;
  7988. ctn_obj = ctn_obj_tmp;
  7989. *cur++ = (u8)('[' | ((u8)ctn_obj << 5));
  7990. ctn = val;
  7991. val = (yyjson_mut_val *)ctn->uni.ptr; /* tail */
  7992. val = ctn_obj ? val->next->next : val->next;
  7993. goto val_begin;
  7994. }
  7995. }
  7996. if (val_type == YYJSON_TYPE_BOOL) {
  7997. incr_len(16);
  7998. cur = write_bool(cur, unsafe_yyjson_get_bool(val));
  7999. cur++;
  8000. goto val_end;
  8001. }
  8002. if (val_type == YYJSON_TYPE_NULL) {
  8003. incr_len(16);
  8004. cur = write_null(cur);
  8005. cur++;
  8006. goto val_end;
  8007. }
  8008. if (val_type == YYJSON_TYPE_RAW) {
  8009. str_len = unsafe_yyjson_get_len(val);
  8010. str_ptr = (const u8 *)unsafe_yyjson_get_str(val);
  8011. check_str_len(str_len);
  8012. incr_len(str_len + 2);
  8013. cur = write_raw(cur, str_ptr, str_len);
  8014. *cur++ = ',';
  8015. goto val_end;
  8016. }
  8017. goto fail_type;
  8018. val_end:
  8019. ctn_len--;
  8020. if (unlikely(ctn_len == 0)) goto ctn_end;
  8021. val = val->next;
  8022. goto val_begin;
  8023. ctn_end:
  8024. cur--;
  8025. *cur++ = (u8)(']' | ((u8)ctn_obj << 5));
  8026. *cur++ = ',';
  8027. if (unlikely((u8 *)ctx >= end)) goto doc_end;
  8028. val = ctn->next;
  8029. yyjson_mut_write_ctx_get(ctx++, &ctn, &ctn_len, &ctn_obj);
  8030. ctn_len--;
  8031. if (likely(ctn_len > 0)) {
  8032. goto val_begin;
  8033. } else {
  8034. goto ctn_end;
  8035. }
  8036. doc_end:
  8037. *--cur = '\0';
  8038. *dat_len = (usize)(cur - hdr);
  8039. err->code = YYJSON_WRITE_SUCCESS;
  8040. err->msg = "success";
  8041. return hdr;
  8042. fail_alloc:
  8043. return_err(MEMORY_ALLOCATION, "memory allocation failed");
  8044. fail_type:
  8045. return_err(INVALID_VALUE_TYPE, "invalid JSON value type");
  8046. fail_num:
  8047. return_err(NAN_OR_INF, "nan or inf number is not allowed");
  8048. fail_str:
  8049. return_err(INVALID_STRING, "invalid utf-8 encoding in string");
  8050. #undef return_err
  8051. #undef incr_len
  8052. #undef check_str_len
  8053. }
  8054. /** Write JSON document pretty.
  8055. The root of this document should be a non-empty container. */
  8056. static_inline u8 *yyjson_mut_write_pretty(const yyjson_mut_val *root,
  8057. usize estimated_val_num,
  8058. yyjson_write_flag flg,
  8059. yyjson_alc alc,
  8060. usize *dat_len,
  8061. yyjson_write_err *err) {
  8062. #define return_err(_code, _msg) do { \
  8063. *dat_len = 0; \
  8064. err->code = YYJSON_WRITE_ERROR_##_code; \
  8065. err->msg = _msg; \
  8066. if (hdr) alc.free(alc.ctx, hdr); \
  8067. return NULL; \
  8068. } while (false)
  8069. #define incr_len(_len) do { \
  8070. ext_len = (usize)(_len); \
  8071. if (unlikely((u8 *)(cur + ext_len) >= (u8 *)ctx)) { \
  8072. alc_inc = yyjson_max(alc_len / 2, ext_len); \
  8073. alc_inc = size_align_up(alc_inc, sizeof(yyjson_mut_write_ctx)); \
  8074. if ((sizeof(usize) < 8) && size_add_is_overflow(alc_len, alc_inc)) \
  8075. goto fail_alloc; \
  8076. alc_len += alc_inc; \
  8077. tmp = (u8 *)alc.realloc(alc.ctx, hdr, alc_len - alc_inc, alc_len); \
  8078. if (unlikely(!tmp)) goto fail_alloc; \
  8079. ctx_len = (usize)(end - (u8 *)ctx); \
  8080. ctx_tmp = (yyjson_mut_write_ctx *)(void *)(tmp + (alc_len - ctx_len)); \
  8081. memmove((void *)ctx_tmp, (void *)(tmp + ((u8 *)ctx - hdr)), ctx_len); \
  8082. ctx = ctx_tmp; \
  8083. cur = tmp + (cur - hdr); \
  8084. end = tmp + alc_len; \
  8085. hdr = tmp; \
  8086. } \
  8087. } while (false)
  8088. #define check_str_len(_len) do { \
  8089. if ((sizeof(usize) < 8) && (_len >= (USIZE_MAX - 16) / 6)) \
  8090. goto fail_alloc; \
  8091. } while (false)
  8092. yyjson_mut_val *val, *ctn;
  8093. yyjson_type val_type;
  8094. usize ctn_len, ctn_len_tmp;
  8095. bool ctn_obj, ctn_obj_tmp, is_key, no_indent;
  8096. u8 *hdr, *cur, *end, *tmp;
  8097. yyjson_mut_write_ctx *ctx, *ctx_tmp;
  8098. usize alc_len, alc_inc, ctx_len, ext_len, str_len, level;
  8099. const u8 *str_ptr;
  8100. const char_enc_type *enc_table = get_enc_table_with_flag(flg);
  8101. bool cpy = (enc_table == enc_table_cpy);
  8102. bool esc = has_write_flag(ESCAPE_UNICODE) != 0;
  8103. bool inv = has_write_flag(ALLOW_INVALID_UNICODE) != 0;
  8104. usize spaces = has_write_flag(PRETTY_TWO_SPACES) ? 2 : 4;
  8105. alc_len = estimated_val_num * YYJSON_WRITER_ESTIMATED_PRETTY_RATIO + 64;
  8106. alc_len = size_align_up(alc_len, sizeof(yyjson_mut_write_ctx));
  8107. hdr = (u8 *)alc.malloc(alc.ctx, alc_len);
  8108. if (!hdr) goto fail_alloc;
  8109. cur = hdr;
  8110. end = hdr + alc_len;
  8111. ctx = (yyjson_mut_write_ctx *)(void *)end;
  8112. doc_begin:
  8113. val = constcast(yyjson_mut_val *)root;
  8114. val_type = unsafe_yyjson_get_type(val);
  8115. ctn_obj = (val_type == YYJSON_TYPE_OBJ);
  8116. ctn_len = unsafe_yyjson_get_len(val) << (u8)ctn_obj;
  8117. *cur++ = (u8)('[' | ((u8)ctn_obj << 5));
  8118. *cur++ = '\n';
  8119. ctn = val;
  8120. val = (yyjson_mut_val *)val->uni.ptr; /* tail */
  8121. val = ctn_obj ? val->next->next : val->next;
  8122. level = 1;
  8123. val_begin:
  8124. val_type = unsafe_yyjson_get_type(val);
  8125. if (val_type == YYJSON_TYPE_STR) {
  8126. is_key = (bool)((u8)ctn_obj & (u8)~ctn_len);
  8127. no_indent = (bool)((u8)ctn_obj & (u8)ctn_len);
  8128. str_len = unsafe_yyjson_get_len(val);
  8129. str_ptr = (const u8 *)unsafe_yyjson_get_str(val);
  8130. check_str_len(str_len);
  8131. incr_len(str_len * 6 + 16 + (no_indent ? 0 : level * 4));
  8132. cur = write_indent(cur, no_indent ? 0 : level, spaces);
  8133. if (likely(cpy) && unsafe_yyjson_get_subtype(val)) {
  8134. cur = write_string_noesc(cur, str_ptr, str_len);
  8135. } else {
  8136. cur = write_string(cur, esc, inv, str_ptr, str_len, enc_table);
  8137. if (unlikely(!cur)) goto fail_str;
  8138. }
  8139. *cur++ = is_key ? ':' : ',';
  8140. *cur++ = is_key ? ' ' : '\n';
  8141. goto val_end;
  8142. }
  8143. if (val_type == YYJSON_TYPE_NUM) {
  8144. no_indent = (bool)((u8)ctn_obj & (u8)ctn_len);
  8145. incr_len(32 + (no_indent ? 0 : level * 4));
  8146. cur = write_indent(cur, no_indent ? 0 : level, spaces);
  8147. cur = write_number(cur, (yyjson_val *)val, flg);
  8148. if (unlikely(!cur)) goto fail_num;
  8149. *cur++ = ',';
  8150. *cur++ = '\n';
  8151. goto val_end;
  8152. }
  8153. if ((val_type & (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) ==
  8154. (YYJSON_TYPE_ARR & YYJSON_TYPE_OBJ)) {
  8155. no_indent = (bool)((u8)ctn_obj & (u8)ctn_len);
  8156. ctn_len_tmp = unsafe_yyjson_get_len(val);
  8157. ctn_obj_tmp = (val_type == YYJSON_TYPE_OBJ);
  8158. if (unlikely(ctn_len_tmp == 0)) {
  8159. /* write empty container */
  8160. incr_len(16 + (no_indent ? 0 : level * 4));
  8161. cur = write_indent(cur, no_indent ? 0 : level, spaces);
  8162. *cur++ = (u8)('[' | ((u8)ctn_obj_tmp << 5));
  8163. *cur++ = (u8)(']' | ((u8)ctn_obj_tmp << 5));
  8164. *cur++ = ',';
  8165. *cur++ = '\n';
  8166. goto val_end;
  8167. } else {
  8168. /* push context, setup new container */
  8169. incr_len(32 + (no_indent ? 0 : level * 4));
  8170. yyjson_mut_write_ctx_set(--ctx, ctn, ctn_len, ctn_obj);
  8171. ctn_len = ctn_len_tmp << (u8)ctn_obj_tmp;
  8172. ctn_obj = ctn_obj_tmp;
  8173. cur = write_indent(cur, no_indent ? 0 : level, spaces);
  8174. level++;
  8175. *cur++ = (u8)('[' | ((u8)ctn_obj << 5));
  8176. *cur++ = '\n';
  8177. ctn = val;
  8178. val = (yyjson_mut_val *)ctn->uni.ptr; /* tail */
  8179. val = ctn_obj ? val->next->next : val->next;
  8180. goto val_begin;
  8181. }
  8182. }
  8183. if (val_type == YYJSON_TYPE_BOOL) {
  8184. no_indent = (bool)((u8)ctn_obj & (u8)ctn_len);
  8185. incr_len(16 + (no_indent ? 0 : level * 4));
  8186. cur = write_indent(cur, no_indent ? 0 : level, spaces);
  8187. cur = write_bool(cur, unsafe_yyjson_get_bool(val));
  8188. cur += 2;
  8189. goto val_end;
  8190. }
  8191. if (val_type == YYJSON_TYPE_NULL) {
  8192. no_indent = (bool)((u8)ctn_obj & (u8)ctn_len);
  8193. incr_len(16 + (no_indent ? 0 : level * 4));
  8194. cur = write_indent(cur, no_indent ? 0 : level, spaces);
  8195. cur = write_null(cur);
  8196. cur += 2;
  8197. goto val_end;
  8198. }
  8199. if (val_type == YYJSON_TYPE_RAW) {
  8200. str_len = unsafe_yyjson_get_len(val);
  8201. str_ptr = (const u8 *)unsafe_yyjson_get_str(val);
  8202. check_str_len(str_len);
  8203. incr_len(str_len + 3);
  8204. cur = write_raw(cur, str_ptr, str_len);
  8205. *cur++ = ',';
  8206. *cur++ = '\n';
  8207. goto val_end;
  8208. }
  8209. goto fail_type;
  8210. val_end:
  8211. ctn_len--;
  8212. if (unlikely(ctn_len == 0)) goto ctn_end;
  8213. val = val->next;
  8214. goto val_begin;
  8215. ctn_end:
  8216. cur -= 2;
  8217. *cur++ = '\n';
  8218. incr_len(level * 4);
  8219. cur = write_indent(cur, --level, spaces);
  8220. *cur++ = (u8)(']' | ((u8)ctn_obj << 5));
  8221. if (unlikely((u8 *)ctx >= end)) goto doc_end;
  8222. val = ctn->next;
  8223. yyjson_mut_write_ctx_get(ctx++, &ctn, &ctn_len, &ctn_obj);
  8224. ctn_len--;
  8225. *cur++ = ',';
  8226. *cur++ = '\n';
  8227. if (likely(ctn_len > 0)) {
  8228. goto val_begin;
  8229. } else {
  8230. goto ctn_end;
  8231. }
  8232. doc_end:
  8233. *cur = '\0';
  8234. *dat_len = (usize)(cur - hdr);
  8235. err->code = YYJSON_WRITE_SUCCESS;
  8236. err->msg = "success";
  8237. return hdr;
  8238. fail_alloc:
  8239. return_err(MEMORY_ALLOCATION, "memory allocation failed");
  8240. fail_type:
  8241. return_err(INVALID_VALUE_TYPE, "invalid JSON value type");
  8242. fail_num:
  8243. return_err(NAN_OR_INF, "nan or inf number is not allowed");
  8244. fail_str:
  8245. return_err(INVALID_STRING, "invalid utf-8 encoding in string");
  8246. #undef return_err
  8247. #undef incr_len
  8248. #undef check_str_len
  8249. }
  8250. static char *yyjson_mut_write_opts_impl(const yyjson_mut_val *val,
  8251. usize estimated_val_num,
  8252. yyjson_write_flag flg,
  8253. const yyjson_alc *alc_ptr,
  8254. usize *dat_len,
  8255. yyjson_write_err *err) {
  8256. yyjson_write_err dummy_err;
  8257. usize dummy_dat_len;
  8258. yyjson_alc alc = alc_ptr ? *alc_ptr : YYJSON_DEFAULT_ALC;
  8259. yyjson_mut_val *root = constcast(yyjson_mut_val *)val;
  8260. err = err ? err : &dummy_err;
  8261. dat_len = dat_len ? dat_len : &dummy_dat_len;
  8262. if (unlikely(!root)) {
  8263. *dat_len = 0;
  8264. err->msg = "input JSON is NULL";
  8265. err->code = YYJSON_WRITE_ERROR_INVALID_PARAMETER;
  8266. return NULL;
  8267. }
  8268. if (!unsafe_yyjson_is_ctn(root) || unsafe_yyjson_get_len(root) == 0) {
  8269. return (char *)yyjson_mut_write_single(root, flg, alc, dat_len, err);
  8270. } else if (flg & (YYJSON_WRITE_PRETTY | YYJSON_WRITE_PRETTY_TWO_SPACES)) {
  8271. return (char *)yyjson_mut_write_pretty(root, estimated_val_num,
  8272. flg, alc, dat_len, err);
  8273. } else {
  8274. return (char *)yyjson_mut_write_minify(root, estimated_val_num,
  8275. flg, alc, dat_len, err);
  8276. }
  8277. }
  8278. char *yyjson_mut_val_write_opts(const yyjson_mut_val *val,
  8279. yyjson_write_flag flg,
  8280. const yyjson_alc *alc_ptr,
  8281. usize *dat_len,
  8282. yyjson_write_err *err) {
  8283. return yyjson_mut_write_opts_impl(val, 0, flg, alc_ptr, dat_len, err);
  8284. }
  8285. char *yyjson_mut_write_opts(const yyjson_mut_doc *doc,
  8286. yyjson_write_flag flg,
  8287. const yyjson_alc *alc_ptr,
  8288. usize *dat_len,
  8289. yyjson_write_err *err) {
  8290. yyjson_mut_val *root;
  8291. usize estimated_val_num;
  8292. if (likely(doc)) {
  8293. root = doc->root;
  8294. estimated_val_num = yyjson_mut_doc_estimated_val_num(doc);
  8295. } else {
  8296. root = NULL;
  8297. estimated_val_num = 0;
  8298. }
  8299. return yyjson_mut_write_opts_impl(root, estimated_val_num,
  8300. flg, alc_ptr, dat_len, err);
  8301. }
  8302. bool yyjson_mut_val_write_file(const char *path,
  8303. const yyjson_mut_val *val,
  8304. yyjson_write_flag flg,
  8305. const yyjson_alc *alc_ptr,
  8306. yyjson_write_err *err) {
  8307. yyjson_write_err dummy_err;
  8308. u8 *dat;
  8309. usize dat_len = 0;
  8310. yyjson_mut_val *root = constcast(yyjson_mut_val *)val;
  8311. bool suc;
  8312. alc_ptr = alc_ptr ? alc_ptr : &YYJSON_DEFAULT_ALC;
  8313. err = err ? err : &dummy_err;
  8314. if (unlikely(!path || !*path)) {
  8315. err->msg = "input path is invalid";
  8316. err->code = YYJSON_WRITE_ERROR_INVALID_PARAMETER;
  8317. return false;
  8318. }
  8319. dat = (u8 *)yyjson_mut_val_write_opts(root, flg, alc_ptr, &dat_len, err);
  8320. if (unlikely(!dat)) return false;
  8321. suc = write_dat_to_file(path, dat, dat_len, err);
  8322. alc_ptr->free(alc_ptr->ctx, dat);
  8323. return suc;
  8324. }
  8325. bool yyjson_mut_val_write_fp(FILE *fp,
  8326. const yyjson_mut_val *val,
  8327. yyjson_write_flag flg,
  8328. const yyjson_alc *alc_ptr,
  8329. yyjson_write_err *err) {
  8330. yyjson_write_err dummy_err;
  8331. u8 *dat;
  8332. usize dat_len = 0;
  8333. yyjson_mut_val *root = constcast(yyjson_mut_val *)val;
  8334. bool suc;
  8335. alc_ptr = alc_ptr ? alc_ptr : &YYJSON_DEFAULT_ALC;
  8336. err = err ? err : &dummy_err;
  8337. if (unlikely(!fp)) {
  8338. err->msg = "input fp is invalid";
  8339. err->code = YYJSON_WRITE_ERROR_INVALID_PARAMETER;
  8340. return false;
  8341. }
  8342. dat = (u8 *)yyjson_mut_val_write_opts(root, flg, alc_ptr, &dat_len, err);
  8343. if (unlikely(!dat)) return false;
  8344. suc = write_dat_to_fp(fp, dat, dat_len, err);
  8345. alc_ptr->free(alc_ptr->ctx, dat);
  8346. return suc;
  8347. }
  8348. bool yyjson_mut_write_file(const char *path,
  8349. const yyjson_mut_doc *doc,
  8350. yyjson_write_flag flg,
  8351. const yyjson_alc *alc_ptr,
  8352. yyjson_write_err *err) {
  8353. yyjson_mut_val *root = doc ? doc->root : NULL;
  8354. return yyjson_mut_val_write_file(path, root, flg, alc_ptr, err);
  8355. }
  8356. bool yyjson_mut_write_fp(FILE *fp,
  8357. const yyjson_mut_doc *doc,
  8358. yyjson_write_flag flg,
  8359. const yyjson_alc *alc_ptr,
  8360. yyjson_write_err *err) {
  8361. yyjson_mut_val *root = doc ? doc->root : NULL;
  8362. return yyjson_mut_val_write_fp(fp, root, flg, alc_ptr, err);
  8363. }
  8364. #endif /* YYJSON_DISABLE_WRITER */