ruby-upb.c 289 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171
  1. /* Amalgamated source file */
  2. #include "ruby-upb.h"
  3. /*
  4. * Copyright (c) 2009-2021, Google LLC
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Google LLC nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /*
  30. * This is where we define macros used across upb.
  31. *
  32. * All of these macros are undef'd in port_undef.inc to avoid leaking them to
  33. * users.
  34. *
  35. * The correct usage is:
  36. *
  37. * #include "upb/foobar.h"
  38. * #include "upb/baz.h"
  39. *
  40. * // MUST be last included header.
  41. * #include "upb/port_def.inc"
  42. *
  43. * // Code for this file.
  44. * // <...>
  45. *
  46. * // Can be omitted for .c files, required for .h.
  47. * #include "upb/port_undef.inc"
  48. *
  49. * This file is private and must not be included by users!
  50. */
  51. #if !((defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
  52. (defined(__cplusplus) && __cplusplus >= 201103L) || \
  53. (defined(_MSC_VER) && _MSC_VER >= 1900))
  54. #error upb requires C99 or C++11 or MSVC >= 2015.
  55. #endif
  56. #include <stdint.h>
  57. #include <stddef.h>
  58. #if UINTPTR_MAX == 0xffffffff
  59. #define UPB_SIZE(size32, size64) size32
  60. #else
  61. #define UPB_SIZE(size32, size64) size64
  62. #endif
  63. /* If we always read/write as a consistent type to each address, this shouldn't
  64. * violate aliasing.
  65. */
  66. #define UPB_PTR_AT(msg, ofs, type) ((type*)((char*)(msg) + (ofs)))
  67. #define UPB_READ_ONEOF(msg, fieldtype, offset, case_offset, case_val, default) \
  68. *UPB_PTR_AT(msg, case_offset, int) == case_val \
  69. ? *UPB_PTR_AT(msg, offset, fieldtype) \
  70. : default
  71. #define UPB_WRITE_ONEOF(msg, fieldtype, offset, value, case_offset, case_val) \
  72. *UPB_PTR_AT(msg, case_offset, int) = case_val; \
  73. *UPB_PTR_AT(msg, offset, fieldtype) = value;
  74. #define UPB_MAPTYPE_STRING 0
  75. /* UPB_INLINE: inline if possible, emit standalone code if required. */
  76. #ifdef __cplusplus
  77. #define UPB_INLINE inline
  78. #elif defined (__GNUC__) || defined(__clang__)
  79. #define UPB_INLINE static __inline__
  80. #else
  81. #define UPB_INLINE static
  82. #endif
  83. #define UPB_ALIGN_UP(size, align) (((size) + (align) - 1) / (align) * (align))
  84. #define UPB_ALIGN_DOWN(size, align) ((size) / (align) * (align))
  85. #define UPB_ALIGN_MALLOC(size) UPB_ALIGN_UP(size, 16)
  86. #define UPB_ALIGN_OF(type) offsetof (struct { char c; type member; }, member)
  87. /* Hints to the compiler about likely/unlikely branches. */
  88. #if defined (__GNUC__) || defined(__clang__)
  89. #define UPB_LIKELY(x) __builtin_expect((x),1)
  90. #define UPB_UNLIKELY(x) __builtin_expect((x),0)
  91. #else
  92. #define UPB_LIKELY(x) (x)
  93. #define UPB_UNLIKELY(x) (x)
  94. #endif
  95. /* Macros for function attributes on compilers that support them. */
  96. #ifdef __GNUC__
  97. #define UPB_FORCEINLINE __inline__ __attribute__((always_inline))
  98. #define UPB_NOINLINE __attribute__((noinline))
  99. #define UPB_NORETURN __attribute__((__noreturn__))
  100. #define UPB_PRINTF(str, first_vararg) __attribute__((format (printf, str, first_vararg)))
  101. #elif defined(_MSC_VER)
  102. #define UPB_NOINLINE
  103. #define UPB_FORCEINLINE
  104. #define UPB_NORETURN __declspec(noreturn)
  105. #define UPB_PRINTF(str, first_vararg)
  106. #else /* !defined(__GNUC__) */
  107. #define UPB_FORCEINLINE
  108. #define UPB_NOINLINE
  109. #define UPB_NORETURN
  110. #define UPB_PRINTF(str, first_vararg)
  111. #endif
  112. #define UPB_MAX(x, y) ((x) > (y) ? (x) : (y))
  113. #define UPB_MIN(x, y) ((x) < (y) ? (x) : (y))
  114. #define UPB_UNUSED(var) (void)var
  115. /* UPB_ASSUME(): in release mode, we tell the compiler to assume this is true.
  116. */
  117. #ifdef NDEBUG
  118. #ifdef __GNUC__
  119. #define UPB_ASSUME(expr) if (!(expr)) __builtin_unreachable()
  120. #elif defined _MSC_VER
  121. #define UPB_ASSUME(expr) if (!(expr)) __assume(0)
  122. #else
  123. #define UPB_ASSUME(expr) do {} while (false && (expr))
  124. #endif
  125. #else
  126. #define UPB_ASSUME(expr) assert(expr)
  127. #endif
  128. /* UPB_ASSERT(): in release mode, we use the expression without letting it be
  129. * evaluated. This prevents "unused variable" warnings. */
  130. #ifdef NDEBUG
  131. #define UPB_ASSERT(expr) do {} while (false && (expr))
  132. #else
  133. #define UPB_ASSERT(expr) assert(expr)
  134. #endif
  135. #if defined(__GNUC__) || defined(__clang__)
  136. #define UPB_UNREACHABLE() do { assert(0); __builtin_unreachable(); } while(0)
  137. #else
  138. #define UPB_UNREACHABLE() do { assert(0); } while(0)
  139. #endif
  140. /* UPB_SETJMP() / UPB_LONGJMP(): avoid setting/restoring signal mask. */
  141. #ifdef __APPLE__
  142. #define UPB_SETJMP(buf) _setjmp(buf)
  143. #define UPB_LONGJMP(buf, val) _longjmp(buf, val)
  144. #else
  145. #define UPB_SETJMP(buf) setjmp(buf)
  146. #define UPB_LONGJMP(buf, val) longjmp(buf, val)
  147. #endif
  148. /* UPB_PTRADD(ptr, ofs): add pointer while avoiding "NULL + 0" UB */
  149. #define UPB_PTRADD(ptr, ofs) ((ofs) ? (ptr) + (ofs) : (ptr))
  150. /* Configure whether fasttable is switched on or not. *************************/
  151. #ifdef __has_attribute
  152. #define UPB_HAS_ATTRIBUTE(x) __has_attribute(x)
  153. #else
  154. #define UPB_HAS_ATTRIBUTE(x) 0
  155. #endif
  156. #if UPB_HAS_ATTRIBUTE(musttail)
  157. #define UPB_MUSTTAIL __attribute__((musttail))
  158. #else
  159. #define UPB_MUSTTAIL
  160. #endif
  161. #undef UPB_HAS_ATTRIBUTE
  162. /* This check is not fully robust: it does not require that we have "musttail"
  163. * support available. We need tail calls to avoid consuming arbitrary amounts
  164. * of stack space.
  165. *
  166. * GCC/Clang can mostly be trusted to generate tail calls as long as
  167. * optimization is enabled, but, debug builds will not generate tail calls
  168. * unless "musttail" is available.
  169. *
  170. * We should probably either:
  171. * 1. require that the compiler supports musttail.
  172. * 2. add some fallback code for when musttail isn't available (ie. return
  173. * instead of tail calling). This is safe and portable, but this comes at
  174. * a CPU cost.
  175. */
  176. #if (defined(__x86_64__) || defined(__aarch64__)) && defined(__GNUC__)
  177. #define UPB_FASTTABLE_SUPPORTED 1
  178. #else
  179. #define UPB_FASTTABLE_SUPPORTED 0
  180. #endif
  181. /* define UPB_ENABLE_FASTTABLE to force fast table support.
  182. * This is useful when we want to ensure we are really getting fasttable,
  183. * for example for testing or benchmarking. */
  184. #if defined(UPB_ENABLE_FASTTABLE)
  185. #if !UPB_FASTTABLE_SUPPORTED
  186. #error fasttable is x86-64/ARM64 only and requires GCC or Clang.
  187. #endif
  188. #define UPB_FASTTABLE 1
  189. /* Define UPB_TRY_ENABLE_FASTTABLE to use fasttable if possible.
  190. * This is useful for releasing code that might be used on multiple platforms,
  191. * for example the PHP or Ruby C extensions. */
  192. #elif defined(UPB_TRY_ENABLE_FASTTABLE)
  193. #define UPB_FASTTABLE UPB_FASTTABLE_SUPPORTED
  194. #else
  195. #define UPB_FASTTABLE 0
  196. #endif
  197. /* UPB_FASTTABLE_INIT() allows protos compiled for fasttable to gracefully
  198. * degrade to non-fasttable if we are using UPB_TRY_ENABLE_FASTTABLE. */
  199. #if !UPB_FASTTABLE && defined(UPB_TRY_ENABLE_FASTTABLE)
  200. #define UPB_FASTTABLE_INIT(...)
  201. #else
  202. #define UPB_FASTTABLE_INIT(...) __VA_ARGS__
  203. #endif
  204. #undef UPB_FASTTABLE_SUPPORTED
  205. /* ASAN poisoning (for arena) *************************************************/
  206. #if defined(__SANITIZE_ADDRESS__)
  207. #define UPB_ASAN 1
  208. #ifdef __cplusplus
  209. extern "C" {
  210. #endif
  211. void __asan_poison_memory_region(void const volatile *addr, size_t size);
  212. void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
  213. #ifdef __cplusplus
  214. } /* extern "C" */
  215. #endif
  216. #define UPB_POISON_MEMORY_REGION(addr, size) \
  217. __asan_poison_memory_region((addr), (size))
  218. #define UPB_UNPOISON_MEMORY_REGION(addr, size) \
  219. __asan_unpoison_memory_region((addr), (size))
  220. #else
  221. #define UPB_ASAN 0
  222. #define UPB_POISON_MEMORY_REGION(addr, size) \
  223. ((void)(addr), (void)(size))
  224. #define UPB_UNPOISON_MEMORY_REGION(addr, size) \
  225. ((void)(addr), (void)(size))
  226. #endif
  227. /** upb/decode.c ************************************************************/
  228. #include <setjmp.h>
  229. #include <string.h>
  230. /* Must be last. */
  231. /* Maps descriptor type -> elem_size_lg2. */
  232. static const uint8_t desctype_to_elem_size_lg2[] = {
  233. -1, /* invalid descriptor type */
  234. 3, /* DOUBLE */
  235. 2, /* FLOAT */
  236. 3, /* INT64 */
  237. 3, /* UINT64 */
  238. 2, /* INT32 */
  239. 3, /* FIXED64 */
  240. 2, /* FIXED32 */
  241. 0, /* BOOL */
  242. UPB_SIZE(3, 4), /* STRING */
  243. UPB_SIZE(2, 3), /* GROUP */
  244. UPB_SIZE(2, 3), /* MESSAGE */
  245. UPB_SIZE(3, 4), /* BYTES */
  246. 2, /* UINT32 */
  247. 2, /* ENUM */
  248. 2, /* SFIXED32 */
  249. 3, /* SFIXED64 */
  250. 2, /* SINT32 */
  251. 3, /* SINT64 */
  252. };
  253. /* Maps descriptor type -> upb map size. */
  254. static const uint8_t desctype_to_mapsize[] = {
  255. -1, /* invalid descriptor type */
  256. 8, /* DOUBLE */
  257. 4, /* FLOAT */
  258. 8, /* INT64 */
  259. 8, /* UINT64 */
  260. 4, /* INT32 */
  261. 8, /* FIXED64 */
  262. 4, /* FIXED32 */
  263. 1, /* BOOL */
  264. UPB_MAPTYPE_STRING, /* STRING */
  265. sizeof(void *), /* GROUP */
  266. sizeof(void *), /* MESSAGE */
  267. UPB_MAPTYPE_STRING, /* BYTES */
  268. 4, /* UINT32 */
  269. 4, /* ENUM */
  270. 4, /* SFIXED32 */
  271. 8, /* SFIXED64 */
  272. 4, /* SINT32 */
  273. 8, /* SINT64 */
  274. };
  275. static const unsigned fixed32_ok = (1 << UPB_DTYPE_FLOAT) |
  276. (1 << UPB_DTYPE_FIXED32) |
  277. (1 << UPB_DTYPE_SFIXED32);
  278. static const unsigned fixed64_ok = (1 << UPB_DTYPE_DOUBLE) |
  279. (1 << UPB_DTYPE_FIXED64) |
  280. (1 << UPB_DTYPE_SFIXED64);
  281. /* Op: an action to be performed for a wire-type/field-type combination. */
  282. #define OP_SCALAR_LG2(n) (n) /* n in [0, 2, 3] => op in [0, 2, 3] */
  283. #define OP_STRING 4
  284. #define OP_BYTES 5
  285. #define OP_SUBMSG 6
  286. /* Ops above are scalar-only. Repeated fields can use any op. */
  287. #define OP_FIXPCK_LG2(n) (n + 5) /* n in [2, 3] => op in [7, 8] */
  288. #define OP_VARPCK_LG2(n) (n + 9) /* n in [0, 2, 3] => op in [9, 11, 12] */
  289. static const int8_t varint_ops[19] = {
  290. -1, /* field not found */
  291. -1, /* DOUBLE */
  292. -1, /* FLOAT */
  293. OP_SCALAR_LG2(3), /* INT64 */
  294. OP_SCALAR_LG2(3), /* UINT64 */
  295. OP_SCALAR_LG2(2), /* INT32 */
  296. -1, /* FIXED64 */
  297. -1, /* FIXED32 */
  298. OP_SCALAR_LG2(0), /* BOOL */
  299. -1, /* STRING */
  300. -1, /* GROUP */
  301. -1, /* MESSAGE */
  302. -1, /* BYTES */
  303. OP_SCALAR_LG2(2), /* UINT32 */
  304. OP_SCALAR_LG2(2), /* ENUM */
  305. -1, /* SFIXED32 */
  306. -1, /* SFIXED64 */
  307. OP_SCALAR_LG2(2), /* SINT32 */
  308. OP_SCALAR_LG2(3), /* SINT64 */
  309. };
  310. static const int8_t delim_ops[37] = {
  311. /* For non-repeated field type. */
  312. -1, /* field not found */
  313. -1, /* DOUBLE */
  314. -1, /* FLOAT */
  315. -1, /* INT64 */
  316. -1, /* UINT64 */
  317. -1, /* INT32 */
  318. -1, /* FIXED64 */
  319. -1, /* FIXED32 */
  320. -1, /* BOOL */
  321. OP_STRING, /* STRING */
  322. -1, /* GROUP */
  323. OP_SUBMSG, /* MESSAGE */
  324. OP_BYTES, /* BYTES */
  325. -1, /* UINT32 */
  326. -1, /* ENUM */
  327. -1, /* SFIXED32 */
  328. -1, /* SFIXED64 */
  329. -1, /* SINT32 */
  330. -1, /* SINT64 */
  331. /* For repeated field type. */
  332. OP_FIXPCK_LG2(3), /* REPEATED DOUBLE */
  333. OP_FIXPCK_LG2(2), /* REPEATED FLOAT */
  334. OP_VARPCK_LG2(3), /* REPEATED INT64 */
  335. OP_VARPCK_LG2(3), /* REPEATED UINT64 */
  336. OP_VARPCK_LG2(2), /* REPEATED INT32 */
  337. OP_FIXPCK_LG2(3), /* REPEATED FIXED64 */
  338. OP_FIXPCK_LG2(2), /* REPEATED FIXED32 */
  339. OP_VARPCK_LG2(0), /* REPEATED BOOL */
  340. OP_STRING, /* REPEATED STRING */
  341. OP_SUBMSG, /* REPEATED GROUP */
  342. OP_SUBMSG, /* REPEATED MESSAGE */
  343. OP_BYTES, /* REPEATED BYTES */
  344. OP_VARPCK_LG2(2), /* REPEATED UINT32 */
  345. OP_VARPCK_LG2(2), /* REPEATED ENUM */
  346. OP_FIXPCK_LG2(2), /* REPEATED SFIXED32 */
  347. OP_FIXPCK_LG2(3), /* REPEATED SFIXED64 */
  348. OP_VARPCK_LG2(2), /* REPEATED SINT32 */
  349. OP_VARPCK_LG2(3), /* REPEATED SINT64 */
  350. };
  351. typedef union {
  352. bool bool_val;
  353. uint32_t uint32_val;
  354. uint64_t uint64_val;
  355. uint32_t size;
  356. } wireval;
  357. static const char *decode_msg(upb_decstate *d, const char *ptr, upb_msg *msg,
  358. const upb_msglayout *layout);
  359. UPB_NORETURN static void decode_err(upb_decstate *d) { UPB_LONGJMP(d->err, 1); }
  360. // We don't want to mark this NORETURN, see comment in .h.
  361. // Unfortunately this code to suppress the warning doesn't appear to be working.
  362. #ifdef __clang__
  363. #pragma clang diagnostic push
  364. #pragma clang diagnostic ignored "-Wunknown-warning-option"
  365. #pragma clang diagnostic ignored "-Wsuggest-attribute"
  366. #endif
  367. const char *fastdecode_err(upb_decstate *d) {
  368. longjmp(d->err, 1);
  369. return NULL;
  370. }
  371. #ifdef __clang__
  372. #pragma clang diagnostic pop
  373. #endif
  374. const uint8_t upb_utf8_offsets[] = {
  375. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  376. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  377. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  378. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  379. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  380. 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  381. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  382. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  383. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  384. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  385. 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0,
  386. };
  387. static void decode_verifyutf8(upb_decstate *d, const char *buf, int len) {
  388. if (!decode_verifyutf8_inl(buf, len)) decode_err(d);
  389. }
  390. static bool decode_reserve(upb_decstate *d, upb_array *arr, size_t elem) {
  391. bool need_realloc = arr->size - arr->len < elem;
  392. if (need_realloc && !_upb_array_realloc(arr, arr->len + elem, &d->arena)) {
  393. decode_err(d);
  394. }
  395. return need_realloc;
  396. }
  397. typedef struct {
  398. const char *ptr;
  399. uint64_t val;
  400. } decode_vret;
  401. UPB_NOINLINE
  402. static decode_vret decode_longvarint64(const char *ptr, uint64_t val) {
  403. decode_vret ret = {NULL, 0};
  404. uint64_t byte;
  405. int i;
  406. for (i = 1; i < 10; i++) {
  407. byte = (uint8_t)ptr[i];
  408. val += (byte - 1) << (i * 7);
  409. if (!(byte & 0x80)) {
  410. ret.ptr = ptr + i + 1;
  411. ret.val = val;
  412. return ret;
  413. }
  414. }
  415. return ret;
  416. }
  417. UPB_FORCEINLINE
  418. static const char *decode_varint64(upb_decstate *d, const char *ptr,
  419. uint64_t *val) {
  420. uint64_t byte = (uint8_t)*ptr;
  421. if (UPB_LIKELY((byte & 0x80) == 0)) {
  422. *val = byte;
  423. return ptr + 1;
  424. } else {
  425. decode_vret res = decode_longvarint64(ptr, byte);
  426. if (!res.ptr) decode_err(d);
  427. *val = res.val;
  428. return res.ptr;
  429. }
  430. }
  431. UPB_FORCEINLINE
  432. static const char *decode_tag(upb_decstate *d, const char *ptr,
  433. uint32_t *val) {
  434. uint64_t byte = (uint8_t)*ptr;
  435. if (UPB_LIKELY((byte & 0x80) == 0)) {
  436. *val = byte;
  437. return ptr + 1;
  438. } else {
  439. const char *start = ptr;
  440. decode_vret res = decode_longvarint64(ptr, byte);
  441. ptr = res.ptr;
  442. *val = res.val;
  443. if (!ptr || *val > UINT32_MAX || ptr - start > 5) decode_err(d);
  444. return ptr;
  445. }
  446. }
  447. static void decode_munge(int type, wireval *val) {
  448. switch (type) {
  449. case UPB_DESCRIPTOR_TYPE_BOOL:
  450. val->bool_val = val->uint64_val != 0;
  451. break;
  452. case UPB_DESCRIPTOR_TYPE_SINT32: {
  453. uint32_t n = val->uint32_val;
  454. val->uint32_val = (n >> 1) ^ -(int32_t)(n & 1);
  455. break;
  456. }
  457. case UPB_DESCRIPTOR_TYPE_SINT64: {
  458. uint64_t n = val->uint64_val;
  459. val->uint64_val = (n >> 1) ^ -(int64_t)(n & 1);
  460. break;
  461. }
  462. case UPB_DESCRIPTOR_TYPE_INT32:
  463. case UPB_DESCRIPTOR_TYPE_UINT32:
  464. if (!_upb_isle()) {
  465. /* The next stage will memcpy(dst, &val, 4) */
  466. val->uint32_val = val->uint64_val;
  467. }
  468. break;
  469. }
  470. }
  471. static const upb_msglayout_field *upb_find_field(const upb_msglayout *l,
  472. uint32_t field_number,
  473. int *last_field_index) {
  474. static upb_msglayout_field none = {0, 0, 0, 0, 0, 0};
  475. if (l == NULL) return &none;
  476. size_t idx = ((size_t)field_number) - 1; // 0 wraps to SIZE_MAX
  477. if (idx < l->dense_below) {
  478. goto found;
  479. }
  480. /* Resume scanning from last_field_index since fields are usually in order. */
  481. int last = *last_field_index;
  482. for (idx = last; idx < l->field_count; idx++) {
  483. if (l->fields[idx].number == field_number) {
  484. goto found;
  485. }
  486. }
  487. for (idx = 0; idx < last; idx++) {
  488. if (l->fields[idx].number == field_number) {
  489. goto found;
  490. }
  491. }
  492. return &none; /* Unknown field. */
  493. found:
  494. UPB_ASSERT(l->fields[idx].number == field_number);
  495. *last_field_index = idx;
  496. return &l->fields[idx];
  497. }
  498. static upb_msg *decode_newsubmsg(upb_decstate *d,
  499. upb_msglayout const *const *submsgs,
  500. const upb_msglayout_field *field) {
  501. const upb_msglayout *subl = submsgs[field->submsg_index];
  502. return _upb_msg_new_inl(subl, &d->arena);
  503. }
  504. UPB_NOINLINE
  505. const char *decode_isdonefallback(upb_decstate *d, const char *ptr,
  506. int overrun) {
  507. ptr = decode_isdonefallback_inl(d, ptr, overrun);
  508. if (ptr == NULL) {
  509. decode_err(d);
  510. }
  511. return ptr;
  512. }
  513. static const char *decode_readstr(upb_decstate *d, const char *ptr, int size,
  514. upb_strview *str) {
  515. if (d->alias) {
  516. str->data = ptr;
  517. } else {
  518. char *data = upb_arena_malloc(&d->arena, size);
  519. if (!data) decode_err(d);
  520. memcpy(data, ptr, size);
  521. str->data = data;
  522. }
  523. str->size = size;
  524. return ptr + size;
  525. }
  526. UPB_FORCEINLINE
  527. static const char *decode_tosubmsg(upb_decstate *d, const char *ptr,
  528. upb_msg *submsg,
  529. upb_msglayout const *const *submsgs,
  530. const upb_msglayout_field *field, int size) {
  531. const upb_msglayout *subl = submsgs[field->submsg_index];
  532. int saved_delta = decode_pushlimit(d, ptr, size);
  533. if (--d->depth < 0) decode_err(d);
  534. if (!decode_isdone(d, &ptr)) {
  535. ptr = decode_msg(d, ptr, submsg, subl);
  536. }
  537. if (d->end_group != DECODE_NOGROUP) decode_err(d);
  538. decode_poplimit(d, ptr, saved_delta);
  539. d->depth++;
  540. return ptr;
  541. }
  542. UPB_FORCEINLINE
  543. static const char *decode_group(upb_decstate *d, const char *ptr,
  544. upb_msg *submsg, const upb_msglayout *subl,
  545. uint32_t number) {
  546. if (--d->depth < 0) decode_err(d);
  547. if (decode_isdone(d, &ptr)) {
  548. decode_err(d);
  549. }
  550. ptr = decode_msg(d, ptr, submsg, subl);
  551. if (d->end_group != number) decode_err(d);
  552. d->end_group = DECODE_NOGROUP;
  553. d->depth++;
  554. return ptr;
  555. }
  556. UPB_FORCEINLINE
  557. static const char *decode_togroup(upb_decstate *d, const char *ptr,
  558. upb_msg *submsg,
  559. upb_msglayout const *const *submsgs,
  560. const upb_msglayout_field *field) {
  561. const upb_msglayout *subl = submsgs[field->submsg_index];
  562. return decode_group(d, ptr, submsg, subl, field->number);
  563. }
  564. static const char *decode_toarray(upb_decstate *d, const char *ptr,
  565. upb_msg *msg,
  566. upb_msglayout const *const *submsgs,
  567. const upb_msglayout_field *field, wireval *val,
  568. int op) {
  569. upb_array **arrp = UPB_PTR_AT(msg, field->offset, void);
  570. upb_array *arr = *arrp;
  571. void *mem;
  572. if (arr) {
  573. decode_reserve(d, arr, 1);
  574. } else {
  575. size_t lg2 = desctype_to_elem_size_lg2[field->descriptortype];
  576. arr = _upb_array_new(&d->arena, 4, lg2);
  577. if (!arr) decode_err(d);
  578. *arrp = arr;
  579. }
  580. switch (op) {
  581. case OP_SCALAR_LG2(0):
  582. case OP_SCALAR_LG2(2):
  583. case OP_SCALAR_LG2(3):
  584. /* Append scalar value. */
  585. mem = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << op, void);
  586. arr->len++;
  587. memcpy(mem, val, 1 << op);
  588. return ptr;
  589. case OP_STRING:
  590. decode_verifyutf8(d, ptr, val->size);
  591. /* Fallthrough. */
  592. case OP_BYTES: {
  593. /* Append bytes. */
  594. upb_strview *str = (upb_strview*)_upb_array_ptr(arr) + arr->len;
  595. arr->len++;
  596. return decode_readstr(d, ptr, val->size, str);
  597. }
  598. case OP_SUBMSG: {
  599. /* Append submessage / group. */
  600. upb_msg *submsg = decode_newsubmsg(d, submsgs, field);
  601. *UPB_PTR_AT(_upb_array_ptr(arr), arr->len * sizeof(void *), upb_msg *) =
  602. submsg;
  603. arr->len++;
  604. if (UPB_UNLIKELY(field->descriptortype == UPB_DTYPE_GROUP)) {
  605. return decode_togroup(d, ptr, submsg, submsgs, field);
  606. } else {
  607. return decode_tosubmsg(d, ptr, submsg, submsgs, field, val->size);
  608. }
  609. }
  610. case OP_FIXPCK_LG2(2):
  611. case OP_FIXPCK_LG2(3): {
  612. /* Fixed packed. */
  613. int lg2 = op - OP_FIXPCK_LG2(0);
  614. int mask = (1 << lg2) - 1;
  615. size_t count = val->size >> lg2;
  616. if ((val->size & mask) != 0) {
  617. decode_err(d); /* Length isn't a round multiple of elem size. */
  618. }
  619. decode_reserve(d, arr, count);
  620. mem = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << lg2, void);
  621. arr->len += count;
  622. memcpy(mem, ptr, val->size); /* XXX: ptr boundary. */
  623. return ptr + val->size;
  624. }
  625. case OP_VARPCK_LG2(0):
  626. case OP_VARPCK_LG2(2):
  627. case OP_VARPCK_LG2(3): {
  628. /* Varint packed. */
  629. int lg2 = op - OP_VARPCK_LG2(0);
  630. int scale = 1 << lg2;
  631. int saved_limit = decode_pushlimit(d, ptr, val->size);
  632. char *out = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << lg2, void);
  633. while (!decode_isdone(d, &ptr)) {
  634. wireval elem;
  635. ptr = decode_varint64(d, ptr, &elem.uint64_val);
  636. decode_munge(field->descriptortype, &elem);
  637. if (decode_reserve(d, arr, 1)) {
  638. out = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << lg2, void);
  639. }
  640. arr->len++;
  641. memcpy(out, &elem, scale);
  642. out += scale;
  643. }
  644. decode_poplimit(d, ptr, saved_limit);
  645. return ptr;
  646. }
  647. default:
  648. UPB_UNREACHABLE();
  649. }
  650. }
  651. static const char *decode_tomap(upb_decstate *d, const char *ptr, upb_msg *msg,
  652. upb_msglayout const *const *submsgs,
  653. const upb_msglayout_field *field, wireval *val) {
  654. upb_map **map_p = UPB_PTR_AT(msg, field->offset, upb_map *);
  655. upb_map *map = *map_p;
  656. upb_map_entry ent;
  657. const upb_msglayout *entry = submsgs[field->submsg_index];
  658. if (!map) {
  659. /* Lazily create map. */
  660. const upb_msglayout_field *key_field = &entry->fields[0];
  661. const upb_msglayout_field *val_field = &entry->fields[1];
  662. char key_size = desctype_to_mapsize[key_field->descriptortype];
  663. char val_size = desctype_to_mapsize[val_field->descriptortype];
  664. UPB_ASSERT(key_field->offset == 0);
  665. UPB_ASSERT(val_field->offset == sizeof(upb_strview));
  666. map = _upb_map_new(&d->arena, key_size, val_size);
  667. *map_p = map;
  668. }
  669. /* Parse map entry. */
  670. memset(&ent, 0, sizeof(ent));
  671. if (entry->fields[1].descriptortype == UPB_DESCRIPTOR_TYPE_MESSAGE ||
  672. entry->fields[1].descriptortype == UPB_DESCRIPTOR_TYPE_GROUP) {
  673. /* Create proactively to handle the case where it doesn't appear. */
  674. ent.v.val = upb_value_ptr(_upb_msg_new(entry->submsgs[0], &d->arena));
  675. }
  676. ptr = decode_tosubmsg(d, ptr, &ent.k, submsgs, field, val->size);
  677. _upb_map_set(map, &ent.k, map->key_size, &ent.v, map->val_size, &d->arena);
  678. return ptr;
  679. }
  680. static const char *decode_tomsg(upb_decstate *d, const char *ptr, upb_msg *msg,
  681. upb_msglayout const *const *submsgs,
  682. const upb_msglayout_field *field, wireval *val,
  683. int op) {
  684. void *mem = UPB_PTR_AT(msg, field->offset, void);
  685. int type = field->descriptortype;
  686. /* Set presence if necessary. */
  687. if (field->presence > 0) {
  688. _upb_sethas_field(msg, field);
  689. } else if (field->presence < 0) {
  690. /* Oneof case */
  691. uint32_t *oneof_case = _upb_oneofcase_field(msg, field);
  692. if (op == OP_SUBMSG && *oneof_case != field->number) {
  693. memset(mem, 0, sizeof(void*));
  694. }
  695. *oneof_case = field->number;
  696. }
  697. /* Store into message. */
  698. switch (op) {
  699. case OP_SUBMSG: {
  700. upb_msg **submsgp = mem;
  701. upb_msg *submsg = *submsgp;
  702. if (!submsg) {
  703. submsg = decode_newsubmsg(d, submsgs, field);
  704. *submsgp = submsg;
  705. }
  706. if (UPB_UNLIKELY(type == UPB_DTYPE_GROUP)) {
  707. ptr = decode_togroup(d, ptr, submsg, submsgs, field);
  708. } else {
  709. ptr = decode_tosubmsg(d, ptr, submsg, submsgs, field, val->size);
  710. }
  711. break;
  712. }
  713. case OP_STRING:
  714. decode_verifyutf8(d, ptr, val->size);
  715. /* Fallthrough. */
  716. case OP_BYTES:
  717. return decode_readstr(d, ptr, val->size, mem);
  718. case OP_SCALAR_LG2(3):
  719. memcpy(mem, val, 8);
  720. break;
  721. case OP_SCALAR_LG2(2):
  722. memcpy(mem, val, 4);
  723. break;
  724. case OP_SCALAR_LG2(0):
  725. memcpy(mem, val, 1);
  726. break;
  727. default:
  728. UPB_UNREACHABLE();
  729. }
  730. return ptr;
  731. }
  732. UPB_FORCEINLINE
  733. static bool decode_tryfastdispatch(upb_decstate *d, const char **ptr,
  734. upb_msg *msg, const upb_msglayout *layout) {
  735. #if UPB_FASTTABLE
  736. if (layout && layout->table_mask != (unsigned char)-1) {
  737. uint16_t tag = fastdecode_loadtag(*ptr);
  738. intptr_t table = decode_totable(layout);
  739. *ptr = fastdecode_tagdispatch(d, *ptr, msg, table, 0, tag);
  740. return true;
  741. }
  742. #endif
  743. return false;
  744. }
  745. UPB_NOINLINE
  746. static const char *decode_msg(upb_decstate *d, const char *ptr, upb_msg *msg,
  747. const upb_msglayout *layout) {
  748. int last_field_index = 0;
  749. while (true) {
  750. uint32_t tag;
  751. const upb_msglayout_field *field;
  752. int field_number;
  753. int wire_type;
  754. const char *field_start = ptr;
  755. wireval val;
  756. int op;
  757. UPB_ASSERT(ptr < d->limit_ptr);
  758. ptr = decode_tag(d, ptr, &tag);
  759. field_number = tag >> 3;
  760. wire_type = tag & 7;
  761. field = upb_find_field(layout, field_number, &last_field_index);
  762. switch (wire_type) {
  763. case UPB_WIRE_TYPE_VARINT:
  764. ptr = decode_varint64(d, ptr, &val.uint64_val);
  765. op = varint_ops[field->descriptortype];
  766. decode_munge(field->descriptortype, &val);
  767. break;
  768. case UPB_WIRE_TYPE_32BIT:
  769. memcpy(&val.uint32_val, ptr, 4);
  770. val.uint32_val = _upb_be_swap32(val.uint32_val);
  771. ptr += 4;
  772. op = OP_SCALAR_LG2(2);
  773. if (((1 << field->descriptortype) & fixed32_ok) == 0) goto unknown;
  774. break;
  775. case UPB_WIRE_TYPE_64BIT:
  776. memcpy(&val.uint64_val, ptr, 8);
  777. val.uint64_val = _upb_be_swap64(val.uint64_val);
  778. ptr += 8;
  779. op = OP_SCALAR_LG2(3);
  780. if (((1 << field->descriptortype) & fixed64_ok) == 0) goto unknown;
  781. break;
  782. case UPB_WIRE_TYPE_DELIMITED: {
  783. int ndx = field->descriptortype;
  784. uint64_t size;
  785. if (_upb_getmode(field) == _UPB_MODE_ARRAY) ndx += 18;
  786. ptr = decode_varint64(d, ptr, &size);
  787. if (size >= INT32_MAX ||
  788. ptr - d->end + (int32_t)size > d->limit) {
  789. decode_err(d); /* Length overflow. */
  790. }
  791. op = delim_ops[ndx];
  792. val.size = size;
  793. break;
  794. }
  795. case UPB_WIRE_TYPE_START_GROUP:
  796. val.uint32_val = field_number;
  797. op = OP_SUBMSG;
  798. if (field->descriptortype != UPB_DTYPE_GROUP) goto unknown;
  799. break;
  800. case UPB_WIRE_TYPE_END_GROUP:
  801. d->end_group = field_number;
  802. return ptr;
  803. default:
  804. decode_err(d);
  805. }
  806. if (op >= 0) {
  807. /* Parse, using op for dispatch. */
  808. switch (_upb_getmode(field)) {
  809. case _UPB_MODE_ARRAY:
  810. ptr = decode_toarray(d, ptr, msg, layout->submsgs, field, &val, op);
  811. break;
  812. case _UPB_MODE_MAP:
  813. ptr = decode_tomap(d, ptr, msg, layout->submsgs, field, &val);
  814. break;
  815. case _UPB_MODE_SCALAR:
  816. ptr = decode_tomsg(d, ptr, msg, layout->submsgs, field, &val, op);
  817. break;
  818. default:
  819. UPB_UNREACHABLE();
  820. }
  821. } else {
  822. unknown:
  823. /* Skip unknown field. */
  824. if (field_number == 0) decode_err(d);
  825. if (wire_type == UPB_WIRE_TYPE_DELIMITED) ptr += val.size;
  826. if (msg) {
  827. if (wire_type == UPB_WIRE_TYPE_START_GROUP) {
  828. d->unknown = field_start;
  829. d->unknown_msg = msg;
  830. ptr = decode_group(d, ptr, NULL, NULL, field_number);
  831. d->unknown_msg = NULL;
  832. field_start = d->unknown;
  833. }
  834. if (!_upb_msg_addunknown(msg, field_start, ptr - field_start,
  835. &d->arena)) {
  836. decode_err(d);
  837. }
  838. } else if (wire_type == UPB_WIRE_TYPE_START_GROUP) {
  839. ptr = decode_group(d, ptr, NULL, NULL, field_number);
  840. }
  841. }
  842. if (decode_isdone(d, &ptr)) return ptr;
  843. if (decode_tryfastdispatch(d, &ptr, msg, layout)) return ptr;
  844. }
  845. }
  846. const char *fastdecode_generic(struct upb_decstate *d, const char *ptr,
  847. upb_msg *msg, intptr_t table, uint64_t hasbits,
  848. uint64_t data) {
  849. (void)data;
  850. *(uint32_t*)msg |= hasbits;
  851. return decode_msg(d, ptr, msg, decode_totablep(table));
  852. }
  853. static bool decode_top(struct upb_decstate *d, const char *buf, void *msg,
  854. const upb_msglayout *l) {
  855. if (!decode_tryfastdispatch(d, &buf, msg, l)) {
  856. decode_msg(d, buf, msg, l);
  857. }
  858. return d->end_group == DECODE_NOGROUP;
  859. }
  860. bool _upb_decode(const char *buf, size_t size, void *msg,
  861. const upb_msglayout *l, const upb_extreg *extreg, int options,
  862. upb_arena *arena) {
  863. bool ok;
  864. upb_decstate state;
  865. unsigned depth = (unsigned)options >> 16;
  866. if (size == 0) {
  867. return true;
  868. } else if (size <= 16) {
  869. memset(&state.patch, 0, 32);
  870. memcpy(&state.patch, buf, size);
  871. buf = state.patch;
  872. state.end = buf + size;
  873. state.limit = 0;
  874. state.alias = false;
  875. } else {
  876. state.end = buf + size - 16;
  877. state.limit = 16;
  878. state.alias = options & UPB_DECODE_ALIAS;
  879. }
  880. state.limit_ptr = state.end;
  881. state.unknown_msg = NULL;
  882. state.depth = depth ? depth : 64;
  883. state.end_group = DECODE_NOGROUP;
  884. state.arena.head = arena->head;
  885. state.arena.last_size = arena->last_size;
  886. state.arena.cleanup_metadata = arena->cleanup_metadata;
  887. state.arena.parent = arena;
  888. if (UPB_UNLIKELY(UPB_SETJMP(state.err))) {
  889. ok = false;
  890. } else {
  891. ok = decode_top(&state, buf, msg, l);
  892. }
  893. arena->head.ptr = state.arena.head.ptr;
  894. arena->head.end = state.arena.head.end;
  895. arena->cleanup_metadata = state.arena.cleanup_metadata;
  896. return ok;
  897. }
  898. #undef OP_SCALAR_LG2
  899. #undef OP_FIXPCK_LG2
  900. #undef OP_VARPCK_LG2
  901. #undef OP_STRING
  902. #undef OP_SUBMSG
  903. /** upb/encode.c ************************************************************/
  904. /* We encode backwards, to avoid pre-computing lengths (one-pass encode). */
  905. #include <setjmp.h>
  906. #include <string.h>
  907. /* Must be last. */
  908. #define UPB_PB_VARINT_MAX_LEN 10
  909. UPB_NOINLINE
  910. static size_t encode_varint64(uint64_t val, char *buf) {
  911. size_t i = 0;
  912. do {
  913. uint8_t byte = val & 0x7fU;
  914. val >>= 7;
  915. if (val) byte |= 0x80U;
  916. buf[i++] = byte;
  917. } while (val);
  918. return i;
  919. }
  920. static uint32_t encode_zz32(int32_t n) { return ((uint32_t)n << 1) ^ (n >> 31); }
  921. static uint64_t encode_zz64(int64_t n) { return ((uint64_t)n << 1) ^ (n >> 63); }
  922. typedef struct {
  923. jmp_buf err;
  924. upb_alloc *alloc;
  925. char *buf, *ptr, *limit;
  926. int options;
  927. int depth;
  928. _upb_mapsorter sorter;
  929. } upb_encstate;
  930. static size_t upb_roundup_pow2(size_t bytes) {
  931. size_t ret = 128;
  932. while (ret < bytes) {
  933. ret *= 2;
  934. }
  935. return ret;
  936. }
  937. UPB_NORETURN static void encode_err(upb_encstate *e) {
  938. UPB_LONGJMP(e->err, 1);
  939. }
  940. UPB_NOINLINE
  941. static void encode_growbuffer(upb_encstate *e, size_t bytes) {
  942. size_t old_size = e->limit - e->buf;
  943. size_t new_size = upb_roundup_pow2(bytes + (e->limit - e->ptr));
  944. char *new_buf = upb_realloc(e->alloc, e->buf, old_size, new_size);
  945. if (!new_buf) encode_err(e);
  946. /* We want previous data at the end, realloc() put it at the beginning. */
  947. if (old_size > 0) {
  948. memmove(new_buf + new_size - old_size, e->buf, old_size);
  949. }
  950. e->ptr = new_buf + new_size - (e->limit - e->ptr);
  951. e->limit = new_buf + new_size;
  952. e->buf = new_buf;
  953. e->ptr -= bytes;
  954. }
  955. /* Call to ensure that at least "bytes" bytes are available for writing at
  956. * e->ptr. Returns false if the bytes could not be allocated. */
  957. UPB_FORCEINLINE
  958. static void encode_reserve(upb_encstate *e, size_t bytes) {
  959. if ((size_t)(e->ptr - e->buf) < bytes) {
  960. encode_growbuffer(e, bytes);
  961. return;
  962. }
  963. e->ptr -= bytes;
  964. }
  965. /* Writes the given bytes to the buffer, handling reserve/advance. */
  966. static void encode_bytes(upb_encstate *e, const void *data, size_t len) {
  967. if (len == 0) return; /* memcpy() with zero size is UB */
  968. encode_reserve(e, len);
  969. memcpy(e->ptr, data, len);
  970. }
  971. static void encode_fixed64(upb_encstate *e, uint64_t val) {
  972. val = _upb_be_swap64(val);
  973. encode_bytes(e, &val, sizeof(uint64_t));
  974. }
  975. static void encode_fixed32(upb_encstate *e, uint32_t val) {
  976. val = _upb_be_swap32(val);
  977. encode_bytes(e, &val, sizeof(uint32_t));
  978. }
  979. UPB_NOINLINE
  980. static void encode_longvarint(upb_encstate *e, uint64_t val) {
  981. size_t len;
  982. char *start;
  983. encode_reserve(e, UPB_PB_VARINT_MAX_LEN);
  984. len = encode_varint64(val, e->ptr);
  985. start = e->ptr + UPB_PB_VARINT_MAX_LEN - len;
  986. memmove(start, e->ptr, len);
  987. e->ptr = start;
  988. }
  989. UPB_FORCEINLINE
  990. static void encode_varint(upb_encstate *e, uint64_t val) {
  991. if (val < 128 && e->ptr != e->buf) {
  992. --e->ptr;
  993. *e->ptr = val;
  994. } else {
  995. encode_longvarint(e, val);
  996. }
  997. }
  998. static void encode_double(upb_encstate *e, double d) {
  999. uint64_t u64;
  1000. UPB_ASSERT(sizeof(double) == sizeof(uint64_t));
  1001. memcpy(&u64, &d, sizeof(uint64_t));
  1002. encode_fixed64(e, u64);
  1003. }
  1004. static void encode_float(upb_encstate *e, float d) {
  1005. uint32_t u32;
  1006. UPB_ASSERT(sizeof(float) == sizeof(uint32_t));
  1007. memcpy(&u32, &d, sizeof(uint32_t));
  1008. encode_fixed32(e, u32);
  1009. }
  1010. static void encode_tag(upb_encstate *e, uint32_t field_number,
  1011. uint8_t wire_type) {
  1012. encode_varint(e, (field_number << 3) | wire_type);
  1013. }
  1014. static void encode_fixedarray(upb_encstate *e, const upb_array *arr,
  1015. size_t elem_size, uint32_t tag) {
  1016. size_t bytes = arr->len * elem_size;
  1017. const char* data = _upb_array_constptr(arr);
  1018. const char* ptr = data + bytes - elem_size;
  1019. if (tag) {
  1020. while (true) {
  1021. encode_bytes(e, ptr, elem_size);
  1022. encode_varint(e, tag);
  1023. if (ptr == data) break;
  1024. ptr -= elem_size;
  1025. }
  1026. } else {
  1027. encode_bytes(e, data, bytes);
  1028. }
  1029. }
  1030. static void encode_message(upb_encstate *e, const upb_msg *msg,
  1031. const upb_msglayout *m, size_t *size);
  1032. static void encode_scalar(upb_encstate *e, const void *_field_mem,
  1033. const upb_msglayout *m, const upb_msglayout_field *f,
  1034. bool skip_zero_value) {
  1035. const char *field_mem = _field_mem;
  1036. int wire_type;
  1037. #define CASE(ctype, type, wtype, encodeval) \
  1038. { \
  1039. ctype val = *(ctype *)field_mem; \
  1040. if (skip_zero_value && val == 0) { \
  1041. return; \
  1042. } \
  1043. encode_##type(e, encodeval); \
  1044. wire_type = wtype; \
  1045. break; \
  1046. }
  1047. switch (f->descriptortype) {
  1048. case UPB_DESCRIPTOR_TYPE_DOUBLE:
  1049. CASE(double, double, UPB_WIRE_TYPE_64BIT, val);
  1050. case UPB_DESCRIPTOR_TYPE_FLOAT:
  1051. CASE(float, float, UPB_WIRE_TYPE_32BIT, val);
  1052. case UPB_DESCRIPTOR_TYPE_INT64:
  1053. case UPB_DESCRIPTOR_TYPE_UINT64:
  1054. CASE(uint64_t, varint, UPB_WIRE_TYPE_VARINT, val);
  1055. case UPB_DESCRIPTOR_TYPE_UINT32:
  1056. CASE(uint32_t, varint, UPB_WIRE_TYPE_VARINT, val);
  1057. case UPB_DESCRIPTOR_TYPE_INT32:
  1058. case UPB_DESCRIPTOR_TYPE_ENUM:
  1059. CASE(int32_t, varint, UPB_WIRE_TYPE_VARINT, (int64_t)val);
  1060. case UPB_DESCRIPTOR_TYPE_SFIXED64:
  1061. case UPB_DESCRIPTOR_TYPE_FIXED64:
  1062. CASE(uint64_t, fixed64, UPB_WIRE_TYPE_64BIT, val);
  1063. case UPB_DESCRIPTOR_TYPE_FIXED32:
  1064. case UPB_DESCRIPTOR_TYPE_SFIXED32:
  1065. CASE(uint32_t, fixed32, UPB_WIRE_TYPE_32BIT, val);
  1066. case UPB_DESCRIPTOR_TYPE_BOOL:
  1067. CASE(bool, varint, UPB_WIRE_TYPE_VARINT, val);
  1068. case UPB_DESCRIPTOR_TYPE_SINT32:
  1069. CASE(int32_t, varint, UPB_WIRE_TYPE_VARINT, encode_zz32(val));
  1070. case UPB_DESCRIPTOR_TYPE_SINT64:
  1071. CASE(int64_t, varint, UPB_WIRE_TYPE_VARINT, encode_zz64(val));
  1072. case UPB_DESCRIPTOR_TYPE_STRING:
  1073. case UPB_DESCRIPTOR_TYPE_BYTES: {
  1074. upb_strview view = *(upb_strview*)field_mem;
  1075. if (skip_zero_value && view.size == 0) {
  1076. return;
  1077. }
  1078. encode_bytes(e, view.data, view.size);
  1079. encode_varint(e, view.size);
  1080. wire_type = UPB_WIRE_TYPE_DELIMITED;
  1081. break;
  1082. }
  1083. case UPB_DESCRIPTOR_TYPE_GROUP: {
  1084. size_t size;
  1085. void *submsg = *(void **)field_mem;
  1086. const upb_msglayout *subm = m->submsgs[f->submsg_index];
  1087. if (submsg == NULL) {
  1088. return;
  1089. }
  1090. if (--e->depth == 0) encode_err(e);
  1091. encode_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP);
  1092. encode_message(e, submsg, subm, &size);
  1093. wire_type = UPB_WIRE_TYPE_START_GROUP;
  1094. e->depth++;
  1095. break;
  1096. }
  1097. case UPB_DESCRIPTOR_TYPE_MESSAGE: {
  1098. size_t size;
  1099. void *submsg = *(void **)field_mem;
  1100. const upb_msglayout *subm = m->submsgs[f->submsg_index];
  1101. if (submsg == NULL) {
  1102. return;
  1103. }
  1104. if (--e->depth == 0) encode_err(e);
  1105. encode_message(e, submsg, subm, &size);
  1106. encode_varint(e, size);
  1107. wire_type = UPB_WIRE_TYPE_DELIMITED;
  1108. e->depth++;
  1109. break;
  1110. }
  1111. default:
  1112. UPB_UNREACHABLE();
  1113. }
  1114. #undef CASE
  1115. encode_tag(e, f->number, wire_type);
  1116. }
  1117. static void encode_array(upb_encstate *e, const upb_msg *msg,
  1118. const upb_msglayout *m, const upb_msglayout_field *f) {
  1119. const upb_array *arr = *UPB_PTR_AT(msg, f->offset, upb_array*);
  1120. bool packed = f->mode & _UPB_MODE_IS_PACKED;
  1121. size_t pre_len = e->limit - e->ptr;
  1122. if (arr == NULL || arr->len == 0) {
  1123. return;
  1124. }
  1125. #define VARINT_CASE(ctype, encode) \
  1126. { \
  1127. const ctype *start = _upb_array_constptr(arr); \
  1128. const ctype *ptr = start + arr->len; \
  1129. uint32_t tag = packed ? 0 : (f->number << 3) | UPB_WIRE_TYPE_VARINT; \
  1130. do { \
  1131. ptr--; \
  1132. encode_varint(e, encode); \
  1133. if (tag) encode_varint(e, tag); \
  1134. } while (ptr != start); \
  1135. } \
  1136. break;
  1137. #define TAG(wire_type) (packed ? 0 : (f->number << 3 | wire_type))
  1138. switch (f->descriptortype) {
  1139. case UPB_DESCRIPTOR_TYPE_DOUBLE:
  1140. encode_fixedarray(e, arr, sizeof(double), TAG(UPB_WIRE_TYPE_64BIT));
  1141. break;
  1142. case UPB_DESCRIPTOR_TYPE_FLOAT:
  1143. encode_fixedarray(e, arr, sizeof(float), TAG(UPB_WIRE_TYPE_32BIT));
  1144. break;
  1145. case UPB_DESCRIPTOR_TYPE_SFIXED64:
  1146. case UPB_DESCRIPTOR_TYPE_FIXED64:
  1147. encode_fixedarray(e, arr, sizeof(uint64_t), TAG(UPB_WIRE_TYPE_64BIT));
  1148. break;
  1149. case UPB_DESCRIPTOR_TYPE_FIXED32:
  1150. case UPB_DESCRIPTOR_TYPE_SFIXED32:
  1151. encode_fixedarray(e, arr, sizeof(uint32_t), TAG(UPB_WIRE_TYPE_32BIT));
  1152. break;
  1153. case UPB_DESCRIPTOR_TYPE_INT64:
  1154. case UPB_DESCRIPTOR_TYPE_UINT64:
  1155. VARINT_CASE(uint64_t, *ptr);
  1156. case UPB_DESCRIPTOR_TYPE_UINT32:
  1157. VARINT_CASE(uint32_t, *ptr);
  1158. case UPB_DESCRIPTOR_TYPE_INT32:
  1159. case UPB_DESCRIPTOR_TYPE_ENUM:
  1160. VARINT_CASE(int32_t, (int64_t)*ptr);
  1161. case UPB_DESCRIPTOR_TYPE_BOOL:
  1162. VARINT_CASE(bool, *ptr);
  1163. case UPB_DESCRIPTOR_TYPE_SINT32:
  1164. VARINT_CASE(int32_t, encode_zz32(*ptr));
  1165. case UPB_DESCRIPTOR_TYPE_SINT64:
  1166. VARINT_CASE(int64_t, encode_zz64(*ptr));
  1167. case UPB_DESCRIPTOR_TYPE_STRING:
  1168. case UPB_DESCRIPTOR_TYPE_BYTES: {
  1169. const upb_strview *start = _upb_array_constptr(arr);
  1170. const upb_strview *ptr = start + arr->len;
  1171. do {
  1172. ptr--;
  1173. encode_bytes(e, ptr->data, ptr->size);
  1174. encode_varint(e, ptr->size);
  1175. encode_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
  1176. } while (ptr != start);
  1177. return;
  1178. }
  1179. case UPB_DESCRIPTOR_TYPE_GROUP: {
  1180. const void *const*start = _upb_array_constptr(arr);
  1181. const void *const*ptr = start + arr->len;
  1182. const upb_msglayout *subm = m->submsgs[f->submsg_index];
  1183. if (--e->depth == 0) encode_err(e);
  1184. do {
  1185. size_t size;
  1186. ptr--;
  1187. encode_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP);
  1188. encode_message(e, *ptr, subm, &size);
  1189. encode_tag(e, f->number, UPB_WIRE_TYPE_START_GROUP);
  1190. } while (ptr != start);
  1191. e->depth++;
  1192. return;
  1193. }
  1194. case UPB_DESCRIPTOR_TYPE_MESSAGE: {
  1195. const void *const*start = _upb_array_constptr(arr);
  1196. const void *const*ptr = start + arr->len;
  1197. const upb_msglayout *subm = m->submsgs[f->submsg_index];
  1198. if (--e->depth == 0) encode_err(e);
  1199. do {
  1200. size_t size;
  1201. ptr--;
  1202. encode_message(e, *ptr, subm, &size);
  1203. encode_varint(e, size);
  1204. encode_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
  1205. } while (ptr != start);
  1206. e->depth++;
  1207. return;
  1208. }
  1209. }
  1210. #undef VARINT_CASE
  1211. if (packed) {
  1212. encode_varint(e, e->limit - e->ptr - pre_len);
  1213. encode_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
  1214. }
  1215. }
  1216. static void encode_mapentry(upb_encstate *e, uint32_t number,
  1217. const upb_msglayout *layout,
  1218. const upb_map_entry *ent) {
  1219. const upb_msglayout_field *key_field = &layout->fields[0];
  1220. const upb_msglayout_field *val_field = &layout->fields[1];
  1221. size_t pre_len = e->limit - e->ptr;
  1222. size_t size;
  1223. encode_scalar(e, &ent->v, layout, val_field, false);
  1224. encode_scalar(e, &ent->k, layout, key_field, false);
  1225. size = (e->limit - e->ptr) - pre_len;
  1226. encode_varint(e, size);
  1227. encode_tag(e, number, UPB_WIRE_TYPE_DELIMITED);
  1228. }
  1229. static void encode_map(upb_encstate *e, const upb_msg *msg,
  1230. const upb_msglayout *m, const upb_msglayout_field *f) {
  1231. const upb_map *map = *UPB_PTR_AT(msg, f->offset, const upb_map*);
  1232. const upb_msglayout *layout = m->submsgs[f->submsg_index];
  1233. UPB_ASSERT(layout->field_count == 2);
  1234. if (map == NULL) return;
  1235. if (e->options & UPB_ENCODE_DETERMINISTIC) {
  1236. _upb_sortedmap sorted;
  1237. _upb_mapsorter_pushmap(&e->sorter, layout->fields[0].descriptortype, map,
  1238. &sorted);
  1239. upb_map_entry ent;
  1240. while (_upb_sortedmap_next(&e->sorter, map, &sorted, &ent)) {
  1241. encode_mapentry(e, f->number, layout, &ent);
  1242. }
  1243. _upb_mapsorter_popmap(&e->sorter, &sorted);
  1244. } else {
  1245. upb_strtable_iter i;
  1246. upb_strtable_begin(&i, &map->table);
  1247. for(; !upb_strtable_done(&i); upb_strtable_next(&i)) {
  1248. upb_strview key = upb_strtable_iter_key(&i);
  1249. const upb_value val = upb_strtable_iter_value(&i);
  1250. upb_map_entry ent;
  1251. _upb_map_fromkey(key, &ent.k, map->key_size);
  1252. _upb_map_fromvalue(val, &ent.v, map->val_size);
  1253. encode_mapentry(e, f->number, layout, &ent);
  1254. }
  1255. }
  1256. }
  1257. static void encode_scalarfield(upb_encstate *e, const char *msg,
  1258. const upb_msglayout *m,
  1259. const upb_msglayout_field *f) {
  1260. bool skip_empty = false;
  1261. if (f->presence == 0) {
  1262. /* Proto3 presence. */
  1263. skip_empty = true;
  1264. } else if (f->presence > 0) {
  1265. /* Proto2 presence: hasbit. */
  1266. if (!_upb_hasbit_field(msg, f)) return;
  1267. } else {
  1268. /* Field is in a oneof. */
  1269. if (_upb_getoneofcase_field(msg, f) != f->number) return;
  1270. }
  1271. encode_scalar(e, msg + f->offset, m, f, skip_empty);
  1272. }
  1273. static void encode_message(upb_encstate *e, const upb_msg *msg,
  1274. const upb_msglayout *m, size_t *size) {
  1275. size_t pre_len = e->limit - e->ptr;
  1276. const upb_msglayout_field *f = &m->fields[m->field_count];
  1277. const upb_msglayout_field *first = &m->fields[0];
  1278. if ((e->options & UPB_ENCODE_SKIPUNKNOWN) == 0) {
  1279. size_t unknown_size;
  1280. const char *unknown = upb_msg_getunknown(msg, &unknown_size);
  1281. if (unknown) {
  1282. encode_bytes(e, unknown, unknown_size);
  1283. }
  1284. }
  1285. while (f != first) {
  1286. f--;
  1287. switch (_upb_getmode(f)) {
  1288. case _UPB_MODE_ARRAY:
  1289. encode_array(e, msg, m, f);
  1290. break;
  1291. case _UPB_MODE_MAP:
  1292. encode_map(e, msg, m, f);
  1293. break;
  1294. case _UPB_MODE_SCALAR:
  1295. encode_scalarfield(e, msg, m, f);
  1296. break;
  1297. default:
  1298. UPB_UNREACHABLE();
  1299. }
  1300. }
  1301. *size = (e->limit - e->ptr) - pre_len;
  1302. }
  1303. char *upb_encode_ex(const void *msg, const upb_msglayout *l, int options,
  1304. upb_arena *arena, size_t *size) {
  1305. upb_encstate e;
  1306. unsigned depth = (unsigned)options >> 16;
  1307. e.alloc = upb_arena_alloc(arena);
  1308. e.buf = NULL;
  1309. e.limit = NULL;
  1310. e.ptr = NULL;
  1311. e.depth = depth ? depth : 64;
  1312. e.options = options;
  1313. _upb_mapsorter_init(&e.sorter);
  1314. char *ret = NULL;
  1315. if (UPB_SETJMP(e.err)) {
  1316. *size = 0;
  1317. ret = NULL;
  1318. } else {
  1319. encode_message(&e, msg, l, size);
  1320. *size = e.limit - e.ptr;
  1321. if (*size == 0) {
  1322. static char ch;
  1323. ret = &ch;
  1324. } else {
  1325. UPB_ASSERT(e.ptr);
  1326. ret = e.ptr;
  1327. }
  1328. }
  1329. _upb_mapsorter_destroy(&e.sorter);
  1330. return ret;
  1331. }
  1332. /** upb/msg.c ************************************************************/
  1333. /** upb_msg *******************************************************************/
  1334. static const size_t overhead = sizeof(upb_msg_internaldata);
  1335. static const upb_msg_internal *upb_msg_getinternal_const(const upb_msg *msg) {
  1336. ptrdiff_t size = sizeof(upb_msg_internal);
  1337. return (upb_msg_internal*)((char*)msg - size);
  1338. }
  1339. upb_msg *_upb_msg_new(const upb_msglayout *l, upb_arena *a) {
  1340. return _upb_msg_new_inl(l, a);
  1341. }
  1342. void _upb_msg_clear(upb_msg *msg, const upb_msglayout *l) {
  1343. void *mem = UPB_PTR_AT(msg, -sizeof(upb_msg_internal), char);
  1344. memset(mem, 0, upb_msg_sizeof(l));
  1345. }
  1346. static bool realloc_internal(upb_msg *msg, size_t need, upb_arena *arena) {
  1347. upb_msg_internal *in = upb_msg_getinternal(msg);
  1348. if (!in->internal) {
  1349. /* No internal data, allocate from scratch. */
  1350. size_t size = UPB_MAX(128, _upb_lg2ceilsize(need + overhead));
  1351. upb_msg_internaldata *internal = upb_arena_malloc(arena, size);
  1352. if (!internal) return false;
  1353. internal->size = size;
  1354. internal->unknown_end = overhead;
  1355. internal->ext_begin = size;
  1356. in->internal = internal;
  1357. } else if (in->internal->ext_begin - in->internal->unknown_end < need) {
  1358. /* Internal data is too small, reallocate. */
  1359. size_t new_size = _upb_lg2ceilsize(in->internal->size + need);
  1360. size_t ext_bytes = in->internal->size - in->internal->ext_begin;
  1361. size_t new_ext_begin = new_size - ext_bytes;
  1362. upb_msg_internaldata *internal =
  1363. upb_arena_realloc(arena, in->internal, in->internal->size, new_size);
  1364. if (!internal) return false;
  1365. if (ext_bytes) {
  1366. /* Need to move extension data to the end. */
  1367. char *ptr = (char*)internal;
  1368. memmove(ptr + new_ext_begin, ptr + internal->ext_begin, ext_bytes);
  1369. }
  1370. internal->ext_begin = new_ext_begin;
  1371. internal->size = new_size;
  1372. in->internal = internal;
  1373. }
  1374. UPB_ASSERT(in->internal->ext_begin - in->internal->unknown_end >= need);
  1375. return true;
  1376. }
  1377. bool _upb_msg_addunknown(upb_msg *msg, const char *data, size_t len,
  1378. upb_arena *arena) {
  1379. if (!realloc_internal(msg, len, arena)) return false;
  1380. upb_msg_internal *in = upb_msg_getinternal(msg);
  1381. memcpy(UPB_PTR_AT(in->internal, in->internal->unknown_end, char), data, len);
  1382. in->internal->unknown_end += len;
  1383. return true;
  1384. }
  1385. void _upb_msg_discardunknown_shallow(upb_msg *msg) {
  1386. upb_msg_internal *in = upb_msg_getinternal(msg);
  1387. if (in->internal) {
  1388. in->internal->unknown_end = overhead;
  1389. }
  1390. }
  1391. const char *upb_msg_getunknown(const upb_msg *msg, size_t *len) {
  1392. const upb_msg_internal *in = upb_msg_getinternal_const(msg);
  1393. if (in->internal) {
  1394. *len = in->internal->unknown_end - overhead;
  1395. return (char*)(in->internal + 1);
  1396. } else {
  1397. *len = 0;
  1398. return NULL;
  1399. }
  1400. }
  1401. const upb_msg_ext *_upb_msg_getexts(const upb_msg *msg, size_t *count) {
  1402. const upb_msg_internal *in = upb_msg_getinternal_const(msg);
  1403. if (in->internal) {
  1404. *count =
  1405. (in->internal->size - in->internal->ext_begin) / sizeof(upb_msg_ext);
  1406. return UPB_PTR_AT(in->internal, in->internal->ext_begin, void);
  1407. } else {
  1408. *count = 0;
  1409. return NULL;
  1410. }
  1411. }
  1412. const upb_msg_ext *_upb_msg_getext(const upb_msg *msg,
  1413. const upb_msglayout_ext *e) {
  1414. size_t n;
  1415. const upb_msg_ext *ext = _upb_msg_getexts(msg, &n);
  1416. /* For now we use linear search exclusively to find extensions. If this
  1417. * becomes an issue due to messages with lots of extensions, we can introduce
  1418. * a table of some sort. */
  1419. for (size_t i = 0; i < n; i++) {
  1420. if (ext[i].ext == e) {
  1421. return &ext[i];
  1422. }
  1423. }
  1424. return NULL;
  1425. }
  1426. upb_msg_ext *_upb_msg_getorcreateext(upb_msg *msg, const upb_msglayout_ext *e,
  1427. upb_arena *arena) {
  1428. upb_msg_ext *ext = (upb_msg_ext*)_upb_msg_getext(msg, e);
  1429. if (ext) return ext;
  1430. if (!realloc_internal(msg, sizeof(upb_msg_ext), arena)) return NULL;
  1431. upb_msg_internal *in = upb_msg_getinternal(msg);
  1432. in->internal->ext_begin -= sizeof(upb_msg_ext);
  1433. ext = UPB_PTR_AT(in->internal, in->internal->ext_begin, void);
  1434. memset(ext, 0, sizeof(upb_msg_ext));
  1435. ext->ext = e;
  1436. return ext;
  1437. }
  1438. /** upb_array *****************************************************************/
  1439. bool _upb_array_realloc(upb_array *arr, size_t min_size, upb_arena *arena) {
  1440. size_t new_size = UPB_MAX(arr->size, 4);
  1441. int elem_size_lg2 = arr->data & 7;
  1442. size_t old_bytes = arr->size << elem_size_lg2;
  1443. size_t new_bytes;
  1444. void* ptr = _upb_array_ptr(arr);
  1445. /* Log2 ceiling of size. */
  1446. while (new_size < min_size) new_size *= 2;
  1447. new_bytes = new_size << elem_size_lg2;
  1448. ptr = upb_arena_realloc(arena, ptr, old_bytes, new_bytes);
  1449. if (!ptr) {
  1450. return false;
  1451. }
  1452. arr->data = _upb_tag_arrptr(ptr, elem_size_lg2);
  1453. arr->size = new_size;
  1454. return true;
  1455. }
  1456. static upb_array *getorcreate_array(upb_array **arr_ptr, int elem_size_lg2,
  1457. upb_arena *arena) {
  1458. upb_array *arr = *arr_ptr;
  1459. if (!arr) {
  1460. arr = _upb_array_new(arena, 4, elem_size_lg2);
  1461. if (!arr) return NULL;
  1462. *arr_ptr = arr;
  1463. }
  1464. return arr;
  1465. }
  1466. void *_upb_array_resize_fallback(upb_array **arr_ptr, size_t size,
  1467. int elem_size_lg2, upb_arena *arena) {
  1468. upb_array *arr = getorcreate_array(arr_ptr, elem_size_lg2, arena);
  1469. return arr && _upb_array_resize(arr, size, arena) ? _upb_array_ptr(arr)
  1470. : NULL;
  1471. }
  1472. bool _upb_array_append_fallback(upb_array **arr_ptr, const void *value,
  1473. int elem_size_lg2, upb_arena *arena) {
  1474. upb_array *arr = getorcreate_array(arr_ptr, elem_size_lg2, arena);
  1475. if (!arr) return false;
  1476. size_t elems = arr->len;
  1477. if (!_upb_array_resize(arr, elems + 1, arena)) {
  1478. return false;
  1479. }
  1480. char *data = _upb_array_ptr(arr);
  1481. memcpy(data + (elems << elem_size_lg2), value, 1 << elem_size_lg2);
  1482. return true;
  1483. }
  1484. /** upb_map *******************************************************************/
  1485. upb_map *_upb_map_new(upb_arena *a, size_t key_size, size_t value_size) {
  1486. upb_map *map = upb_arena_malloc(a, sizeof(upb_map));
  1487. if (!map) {
  1488. return NULL;
  1489. }
  1490. upb_strtable_init(&map->table, 4, a);
  1491. map->key_size = key_size;
  1492. map->val_size = value_size;
  1493. return map;
  1494. }
  1495. static void _upb_mapsorter_getkeys(const void *_a, const void *_b, void *a_key,
  1496. void *b_key, size_t size) {
  1497. const upb_tabent *const*a = _a;
  1498. const upb_tabent *const*b = _b;
  1499. upb_strview a_tabkey = upb_tabstrview((*a)->key);
  1500. upb_strview b_tabkey = upb_tabstrview((*b)->key);
  1501. _upb_map_fromkey(a_tabkey, a_key, size);
  1502. _upb_map_fromkey(b_tabkey, b_key, size);
  1503. }
  1504. static int _upb_mapsorter_cmpi64(const void *_a, const void *_b) {
  1505. int64_t a, b;
  1506. _upb_mapsorter_getkeys(_a, _b, &a, &b, 8);
  1507. return a - b;
  1508. }
  1509. static int _upb_mapsorter_cmpu64(const void *_a, const void *_b) {
  1510. uint64_t a, b;
  1511. _upb_mapsorter_getkeys(_a, _b, &a, &b, 8);
  1512. return a - b;
  1513. }
  1514. static int _upb_mapsorter_cmpi32(const void *_a, const void *_b) {
  1515. int32_t a, b;
  1516. _upb_mapsorter_getkeys(_a, _b, &a, &b, 4);
  1517. return a - b;
  1518. }
  1519. static int _upb_mapsorter_cmpu32(const void *_a, const void *_b) {
  1520. uint32_t a, b;
  1521. _upb_mapsorter_getkeys(_a, _b, &a, &b, 4);
  1522. return a - b;
  1523. }
  1524. static int _upb_mapsorter_cmpbool(const void *_a, const void *_b) {
  1525. bool a, b;
  1526. _upb_mapsorter_getkeys(_a, _b, &a, &b, 1);
  1527. return a - b;
  1528. }
  1529. static int _upb_mapsorter_cmpstr(const void *_a, const void *_b) {
  1530. upb_strview a, b;
  1531. _upb_mapsorter_getkeys(_a, _b, &a, &b, UPB_MAPTYPE_STRING);
  1532. size_t common_size = UPB_MIN(a.size, b.size);
  1533. int cmp = memcmp(a.data, b.data, common_size);
  1534. if (cmp) return cmp;
  1535. return a.size - b.size;
  1536. }
  1537. bool _upb_mapsorter_pushmap(_upb_mapsorter *s, upb_descriptortype_t key_type,
  1538. const upb_map *map, _upb_sortedmap *sorted) {
  1539. int map_size = _upb_map_size(map);
  1540. sorted->start = s->size;
  1541. sorted->pos = sorted->start;
  1542. sorted->end = sorted->start + map_size;
  1543. /* Grow s->entries if necessary. */
  1544. if (sorted->end > s->cap) {
  1545. s->cap = _upb_lg2ceilsize(sorted->end);
  1546. s->entries = realloc(s->entries, s->cap * sizeof(*s->entries));
  1547. if (!s->entries) return false;
  1548. }
  1549. s->size = sorted->end;
  1550. /* Copy non-empty entries from the table to s->entries. */
  1551. upb_tabent const**dst = &s->entries[sorted->start];
  1552. const upb_tabent *src = map->table.t.entries;
  1553. const upb_tabent *end = src + upb_table_size(&map->table.t);
  1554. for (; src < end; src++) {
  1555. if (!upb_tabent_isempty(src)) {
  1556. *dst = src;
  1557. dst++;
  1558. }
  1559. }
  1560. UPB_ASSERT(dst == &s->entries[sorted->end]);
  1561. /* Sort entries according to the key type. */
  1562. int (*compar)(const void *, const void *);
  1563. switch (key_type) {
  1564. case UPB_DESCRIPTOR_TYPE_INT64:
  1565. case UPB_DESCRIPTOR_TYPE_SFIXED64:
  1566. case UPB_DESCRIPTOR_TYPE_SINT64:
  1567. compar = _upb_mapsorter_cmpi64;
  1568. break;
  1569. case UPB_DESCRIPTOR_TYPE_UINT64:
  1570. case UPB_DESCRIPTOR_TYPE_FIXED64:
  1571. compar = _upb_mapsorter_cmpu64;
  1572. break;
  1573. case UPB_DESCRIPTOR_TYPE_INT32:
  1574. case UPB_DESCRIPTOR_TYPE_SINT32:
  1575. case UPB_DESCRIPTOR_TYPE_SFIXED32:
  1576. case UPB_DESCRIPTOR_TYPE_ENUM:
  1577. compar = _upb_mapsorter_cmpi32;
  1578. break;
  1579. case UPB_DESCRIPTOR_TYPE_UINT32:
  1580. case UPB_DESCRIPTOR_TYPE_FIXED32:
  1581. compar = _upb_mapsorter_cmpu32;
  1582. break;
  1583. case UPB_DESCRIPTOR_TYPE_BOOL:
  1584. compar = _upb_mapsorter_cmpbool;
  1585. break;
  1586. case UPB_DESCRIPTOR_TYPE_STRING:
  1587. compar = _upb_mapsorter_cmpstr;
  1588. break;
  1589. default:
  1590. UPB_UNREACHABLE();
  1591. }
  1592. qsort(&s->entries[sorted->start], map_size, sizeof(*s->entries), compar);
  1593. return true;
  1594. }
  1595. /** upb_extreg ****************************************************************/
  1596. struct upb_extreg {
  1597. upb_arena *arena;
  1598. upb_strtable exts; /* Key is upb_msglayout* concatenated with fieldnum. */
  1599. };
  1600. #define EXTREG_KEY_SIZE (sizeof(upb_msglayout*) + sizeof(uint32_t))
  1601. static void extreg_key(char *buf, const upb_msglayout *l, uint32_t fieldnum) {
  1602. memcpy(buf, &l, sizeof(l));
  1603. memcpy(buf + sizeof(l), &fieldnum, sizeof(fieldnum));
  1604. }
  1605. upb_extreg *upb_extreg_new(upb_arena *arena) {
  1606. upb_extreg *r = upb_arena_malloc(arena, sizeof(*r));
  1607. if (!r) return NULL;
  1608. r->arena = arena;
  1609. if (!upb_strtable_init(&r->exts, 8, arena)) return NULL;
  1610. return r;
  1611. }
  1612. bool _upb_extreg_add(upb_extreg *r, const upb_msglayout_ext *e, size_t count) {
  1613. char buf[EXTREG_KEY_SIZE];
  1614. const upb_msglayout_ext *start = e;
  1615. const upb_msglayout_ext *end = e + count;
  1616. for (; e < end; e++) {
  1617. extreg_key(buf, e->extendee, e->field.number);
  1618. if (!upb_strtable_insert(&r->exts, buf, EXTREG_KEY_SIZE,
  1619. upb_value_constptr(e), r->arena)) {
  1620. goto failure;
  1621. }
  1622. }
  1623. return true;
  1624. failure:
  1625. /* Back out the entries previously added. */
  1626. for (end = e, e = start; e < end; e++) {
  1627. extreg_key(buf, e->extendee, e->field.number);
  1628. upb_strtable_remove(&r->exts, buf, EXTREG_KEY_SIZE, NULL);
  1629. }
  1630. return false;
  1631. }
  1632. const upb_msglayout_field *_upb_extreg_get(const upb_extreg *r,
  1633. const upb_msglayout *l,
  1634. uint32_t num) {
  1635. char buf[EXTREG_KEY_SIZE];
  1636. upb_value v;
  1637. extreg_key(buf, l, num);
  1638. if (upb_strtable_lookup2(&r->exts, buf, EXTREG_KEY_SIZE, &v)) {
  1639. return upb_value_getconstptr(v);
  1640. } else {
  1641. return NULL;
  1642. }
  1643. }
  1644. /** upb/table.c ************************************************************/
  1645. /*
  1646. * upb_table Implementation
  1647. *
  1648. * Implementation is heavily inspired by Lua's ltable.c.
  1649. */
  1650. #include <string.h>
  1651. /* Must be last. */
  1652. #define UPB_MAXARRSIZE 16 /* 64k. */
  1653. /* From Chromium. */
  1654. #define ARRAY_SIZE(x) \
  1655. ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
  1656. static const double MAX_LOAD = 0.85;
  1657. /* The minimum utilization of the array part of a mixed hash/array table. This
  1658. * is a speed/memory-usage tradeoff (though it's not straightforward because of
  1659. * cache effects). The lower this is, the more memory we'll use. */
  1660. static const double MIN_DENSITY = 0.1;
  1661. static bool is_pow2(uint64_t v) { return v == 0 || (v & (v - 1)) == 0; }
  1662. static upb_value _upb_value_val(uint64_t val) {
  1663. upb_value ret;
  1664. _upb_value_setval(&ret, val);
  1665. return ret;
  1666. }
  1667. static int log2ceil(uint64_t v) {
  1668. int ret = 0;
  1669. bool pow2 = is_pow2(v);
  1670. while (v >>= 1) ret++;
  1671. ret = pow2 ? ret : ret + 1; /* Ceiling. */
  1672. return UPB_MIN(UPB_MAXARRSIZE, ret);
  1673. }
  1674. char *upb_strdup2(const char *s, size_t len, upb_arena *a) {
  1675. size_t n;
  1676. char *p;
  1677. /* Prevent overflow errors. */
  1678. if (len == SIZE_MAX) return NULL;
  1679. /* Always null-terminate, even if binary data; but don't rely on the input to
  1680. * have a null-terminating byte since it may be a raw binary buffer. */
  1681. n = len + 1;
  1682. p = upb_arena_malloc(a, n);
  1683. if (p) {
  1684. memcpy(p, s, len);
  1685. p[len] = 0;
  1686. }
  1687. return p;
  1688. }
  1689. /* A type to represent the lookup key of either a strtable or an inttable. */
  1690. typedef union {
  1691. uintptr_t num;
  1692. struct {
  1693. const char *str;
  1694. size_t len;
  1695. } str;
  1696. } lookupkey_t;
  1697. static lookupkey_t strkey2(const char *str, size_t len) {
  1698. lookupkey_t k;
  1699. k.str.str = str;
  1700. k.str.len = len;
  1701. return k;
  1702. }
  1703. static lookupkey_t intkey(uintptr_t key) {
  1704. lookupkey_t k;
  1705. k.num = key;
  1706. return k;
  1707. }
  1708. typedef uint32_t hashfunc_t(upb_tabkey key);
  1709. typedef bool eqlfunc_t(upb_tabkey k1, lookupkey_t k2);
  1710. /* Base table (shared code) ***************************************************/
  1711. static uint32_t upb_inthash(uintptr_t key) {
  1712. return (uint32_t)key;
  1713. }
  1714. static const upb_tabent *upb_getentry(const upb_table *t, uint32_t hash) {
  1715. return t->entries + (hash & t->mask);
  1716. }
  1717. static bool upb_arrhas(upb_tabval key) {
  1718. return key.val != (uint64_t)-1;
  1719. }
  1720. static bool isfull(upb_table *t) {
  1721. return t->count == t->max_count;
  1722. }
  1723. static bool init(upb_table *t, uint8_t size_lg2, upb_arena *a) {
  1724. size_t bytes;
  1725. t->count = 0;
  1726. t->size_lg2 = size_lg2;
  1727. t->mask = upb_table_size(t) ? upb_table_size(t) - 1 : 0;
  1728. t->max_count = upb_table_size(t) * MAX_LOAD;
  1729. bytes = upb_table_size(t) * sizeof(upb_tabent);
  1730. if (bytes > 0) {
  1731. t->entries = upb_arena_malloc(a, bytes);
  1732. if (!t->entries) return false;
  1733. memset(t->entries, 0, bytes);
  1734. } else {
  1735. t->entries = NULL;
  1736. }
  1737. return true;
  1738. }
  1739. static upb_tabent *emptyent(upb_table *t, upb_tabent *e) {
  1740. upb_tabent *begin = t->entries;
  1741. upb_tabent *end = begin + upb_table_size(t);
  1742. for (e = e + 1; e < end; e++) {
  1743. if (upb_tabent_isempty(e)) return e;
  1744. }
  1745. for (e = begin; e < end; e++) {
  1746. if (upb_tabent_isempty(e)) return e;
  1747. }
  1748. UPB_ASSERT(false);
  1749. return NULL;
  1750. }
  1751. static upb_tabent *getentry_mutable(upb_table *t, uint32_t hash) {
  1752. return (upb_tabent*)upb_getentry(t, hash);
  1753. }
  1754. static const upb_tabent *findentry(const upb_table *t, lookupkey_t key,
  1755. uint32_t hash, eqlfunc_t *eql) {
  1756. const upb_tabent *e;
  1757. if (t->size_lg2 == 0) return NULL;
  1758. e = upb_getentry(t, hash);
  1759. if (upb_tabent_isempty(e)) return NULL;
  1760. while (1) {
  1761. if (eql(e->key, key)) return e;
  1762. if ((e = e->next) == NULL) return NULL;
  1763. }
  1764. }
  1765. static upb_tabent *findentry_mutable(upb_table *t, lookupkey_t key,
  1766. uint32_t hash, eqlfunc_t *eql) {
  1767. return (upb_tabent*)findentry(t, key, hash, eql);
  1768. }
  1769. static bool lookup(const upb_table *t, lookupkey_t key, upb_value *v,
  1770. uint32_t hash, eqlfunc_t *eql) {
  1771. const upb_tabent *e = findentry(t, key, hash, eql);
  1772. if (e) {
  1773. if (v) {
  1774. _upb_value_setval(v, e->val.val);
  1775. }
  1776. return true;
  1777. } else {
  1778. return false;
  1779. }
  1780. }
  1781. /* The given key must not already exist in the table. */
  1782. static void insert(upb_table *t, lookupkey_t key, upb_tabkey tabkey,
  1783. upb_value val, uint32_t hash,
  1784. hashfunc_t *hashfunc, eqlfunc_t *eql) {
  1785. upb_tabent *mainpos_e;
  1786. upb_tabent *our_e;
  1787. UPB_ASSERT(findentry(t, key, hash, eql) == NULL);
  1788. t->count++;
  1789. mainpos_e = getentry_mutable(t, hash);
  1790. our_e = mainpos_e;
  1791. if (upb_tabent_isempty(mainpos_e)) {
  1792. /* Our main position is empty; use it. */
  1793. our_e->next = NULL;
  1794. } else {
  1795. /* Collision. */
  1796. upb_tabent *new_e = emptyent(t, mainpos_e);
  1797. /* Head of collider's chain. */
  1798. upb_tabent *chain = getentry_mutable(t, hashfunc(mainpos_e->key));
  1799. if (chain == mainpos_e) {
  1800. /* Existing ent is in its main position (it has the same hash as us, and
  1801. * is the head of our chain). Insert to new ent and append to this chain. */
  1802. new_e->next = mainpos_e->next;
  1803. mainpos_e->next = new_e;
  1804. our_e = new_e;
  1805. } else {
  1806. /* Existing ent is not in its main position (it is a node in some other
  1807. * chain). This implies that no existing ent in the table has our hash.
  1808. * Evict it (updating its chain) and use its ent for head of our chain. */
  1809. *new_e = *mainpos_e; /* copies next. */
  1810. while (chain->next != mainpos_e) {
  1811. chain = (upb_tabent*)chain->next;
  1812. UPB_ASSERT(chain);
  1813. }
  1814. chain->next = new_e;
  1815. our_e = mainpos_e;
  1816. our_e->next = NULL;
  1817. }
  1818. }
  1819. our_e->key = tabkey;
  1820. our_e->val.val = val.val;
  1821. UPB_ASSERT(findentry(t, key, hash, eql) == our_e);
  1822. }
  1823. static bool rm(upb_table *t, lookupkey_t key, upb_value *val,
  1824. upb_tabkey *removed, uint32_t hash, eqlfunc_t *eql) {
  1825. upb_tabent *chain = getentry_mutable(t, hash);
  1826. if (upb_tabent_isempty(chain)) return false;
  1827. if (eql(chain->key, key)) {
  1828. /* Element to remove is at the head of its chain. */
  1829. t->count--;
  1830. if (val) _upb_value_setval(val, chain->val.val);
  1831. if (removed) *removed = chain->key;
  1832. if (chain->next) {
  1833. upb_tabent *move = (upb_tabent*)chain->next;
  1834. *chain = *move;
  1835. move->key = 0; /* Make the slot empty. */
  1836. } else {
  1837. chain->key = 0; /* Make the slot empty. */
  1838. }
  1839. return true;
  1840. } else {
  1841. /* Element to remove is either in a non-head position or not in the
  1842. * table. */
  1843. while (chain->next && !eql(chain->next->key, key)) {
  1844. chain = (upb_tabent*)chain->next;
  1845. }
  1846. if (chain->next) {
  1847. /* Found element to remove. */
  1848. upb_tabent *rm = (upb_tabent*)chain->next;
  1849. t->count--;
  1850. if (val) _upb_value_setval(val, chain->next->val.val);
  1851. if (removed) *removed = rm->key;
  1852. rm->key = 0; /* Make the slot empty. */
  1853. chain->next = rm->next;
  1854. return true;
  1855. } else {
  1856. /* Element to remove is not in the table. */
  1857. return false;
  1858. }
  1859. }
  1860. }
  1861. static size_t next(const upb_table *t, size_t i) {
  1862. do {
  1863. if (++i >= upb_table_size(t))
  1864. return SIZE_MAX - 1; /* Distinct from -1. */
  1865. } while(upb_tabent_isempty(&t->entries[i]));
  1866. return i;
  1867. }
  1868. static size_t begin(const upb_table *t) {
  1869. return next(t, -1);
  1870. }
  1871. /* upb_strtable ***************************************************************/
  1872. /* A simple "subclass" of upb_table that only adds a hash function for strings. */
  1873. static upb_tabkey strcopy(lookupkey_t k2, upb_arena *a) {
  1874. uint32_t len = (uint32_t) k2.str.len;
  1875. char *str = upb_arena_malloc(a, k2.str.len + sizeof(uint32_t) + 1);
  1876. if (str == NULL) return 0;
  1877. memcpy(str, &len, sizeof(uint32_t));
  1878. if (k2.str.len) memcpy(str + sizeof(uint32_t), k2.str.str, k2.str.len);
  1879. str[sizeof(uint32_t) + k2.str.len] = '\0';
  1880. return (uintptr_t)str;
  1881. }
  1882. /* Adapted from ABSL's wyhash. */
  1883. static uint64_t UnalignedLoad64(const void *p) {
  1884. uint64_t val;
  1885. memcpy(&val, p, 8);
  1886. return val;
  1887. }
  1888. static uint32_t UnalignedLoad32(const void *p) {
  1889. uint32_t val;
  1890. memcpy(&val, p, 4);
  1891. return val;
  1892. }
  1893. #if defined(_MSC_VER) && defined(_M_X64)
  1894. #include <intrin.h>
  1895. #endif
  1896. /* Computes a * b, returning the low 64 bits of the result and storing the high
  1897. * 64 bits in |*high|. */
  1898. static uint64_t upb_umul128(uint64_t v0, uint64_t v1, uint64_t* out_high) {
  1899. #ifdef __SIZEOF_INT128__
  1900. __uint128_t p = v0;
  1901. p *= v1;
  1902. *out_high = (uint64_t)(p >> 64);
  1903. return (uint64_t)p;
  1904. #elif defined(_MSC_VER) && defined(_M_X64)
  1905. return _umul128(v0, v1, out_high);
  1906. #else
  1907. uint64_t a32 = v0 >> 32;
  1908. uint64_t a00 = v0 & 0xffffffff;
  1909. uint64_t b32 = v1 >> 32;
  1910. uint64_t b00 = v1 & 0xffffffff;
  1911. uint64_t high = a32 * b32;
  1912. uint64_t low = a00 * b00;
  1913. uint64_t mid1 = a32 * b00;
  1914. uint64_t mid2 = a00 * b32;
  1915. low += (mid1 << 32) + (mid2 << 32);
  1916. // Omit carry bit, for mixing we do not care about exact numerical precision.
  1917. high += (mid1 >> 32) + (mid2 >> 32);
  1918. *out_high = high;
  1919. return low;
  1920. #endif
  1921. }
  1922. static uint64_t WyhashMix(uint64_t v0, uint64_t v1) {
  1923. uint64_t high;
  1924. uint64_t low = upb_umul128(v0, v1, &high);
  1925. return low ^ high;
  1926. }
  1927. uint64_t Wyhash(const void *data, size_t len, uint64_t seed,
  1928. const uint64_t salt[]) {
  1929. const uint8_t* ptr = (const uint8_t*)data;
  1930. uint64_t starting_length = (uint64_t)len;
  1931. uint64_t current_state = seed ^ salt[0];
  1932. if (len > 64) {
  1933. // If we have more than 64 bytes, we're going to handle chunks of 64
  1934. // bytes at a time. We're going to build up two separate hash states
  1935. // which we will then hash together.
  1936. uint64_t duplicated_state = current_state;
  1937. do {
  1938. uint64_t a = UnalignedLoad64(ptr);
  1939. uint64_t b = UnalignedLoad64(ptr + 8);
  1940. uint64_t c = UnalignedLoad64(ptr + 16);
  1941. uint64_t d = UnalignedLoad64(ptr + 24);
  1942. uint64_t e = UnalignedLoad64(ptr + 32);
  1943. uint64_t f = UnalignedLoad64(ptr + 40);
  1944. uint64_t g = UnalignedLoad64(ptr + 48);
  1945. uint64_t h = UnalignedLoad64(ptr + 56);
  1946. uint64_t cs0 = WyhashMix(a ^ salt[1], b ^ current_state);
  1947. uint64_t cs1 = WyhashMix(c ^ salt[2], d ^ current_state);
  1948. current_state = (cs0 ^ cs1);
  1949. uint64_t ds0 = WyhashMix(e ^ salt[3], f ^ duplicated_state);
  1950. uint64_t ds1 = WyhashMix(g ^ salt[4], h ^ duplicated_state);
  1951. duplicated_state = (ds0 ^ ds1);
  1952. ptr += 64;
  1953. len -= 64;
  1954. } while (len > 64);
  1955. current_state = current_state ^ duplicated_state;
  1956. }
  1957. // We now have a data `ptr` with at most 64 bytes and the current state
  1958. // of the hashing state machine stored in current_state.
  1959. while (len > 16) {
  1960. uint64_t a = UnalignedLoad64(ptr);
  1961. uint64_t b = UnalignedLoad64(ptr + 8);
  1962. current_state = WyhashMix(a ^ salt[1], b ^ current_state);
  1963. ptr += 16;
  1964. len -= 16;
  1965. }
  1966. // We now have a data `ptr` with at most 16 bytes.
  1967. uint64_t a = 0;
  1968. uint64_t b = 0;
  1969. if (len > 8) {
  1970. // When we have at least 9 and at most 16 bytes, set A to the first 64
  1971. // bits of the input and B to the last 64 bits of the input. Yes, they will
  1972. // overlap in the middle if we are working with less than the full 16
  1973. // bytes.
  1974. a = UnalignedLoad64(ptr);
  1975. b = UnalignedLoad64(ptr + len - 8);
  1976. } else if (len > 3) {
  1977. // If we have at least 4 and at most 8 bytes, set A to the first 32
  1978. // bits and B to the last 32 bits.
  1979. a = UnalignedLoad32(ptr);
  1980. b = UnalignedLoad32(ptr + len - 4);
  1981. } else if (len > 0) {
  1982. // If we have at least 1 and at most 3 bytes, read all of the provided
  1983. // bits into A, with some adjustments.
  1984. a = ((ptr[0] << 16) | (ptr[len >> 1] << 8) | ptr[len - 1]);
  1985. b = 0;
  1986. } else {
  1987. a = 0;
  1988. b = 0;
  1989. }
  1990. uint64_t w = WyhashMix(a ^ salt[1], b ^ current_state);
  1991. uint64_t z = salt[1] ^ starting_length;
  1992. return WyhashMix(w, z);
  1993. }
  1994. const uint64_t kWyhashSalt[5] = {
  1995. 0x243F6A8885A308D3ULL, 0x13198A2E03707344ULL, 0xA4093822299F31D0ULL,
  1996. 0x082EFA98EC4E6C89ULL, 0x452821E638D01377ULL,
  1997. };
  1998. static uint32_t table_hash(const char *p, size_t n) {
  1999. return Wyhash(p, n, 0, kWyhashSalt);
  2000. }
  2001. static uint32_t strhash(upb_tabkey key) {
  2002. uint32_t len;
  2003. char *str = upb_tabstr(key, &len);
  2004. return table_hash(str, len);
  2005. }
  2006. static bool streql(upb_tabkey k1, lookupkey_t k2) {
  2007. uint32_t len;
  2008. char *str = upb_tabstr(k1, &len);
  2009. return len == k2.str.len && (len == 0 || memcmp(str, k2.str.str, len) == 0);
  2010. }
  2011. bool upb_strtable_init(upb_strtable *t, size_t expected_size, upb_arena *a) {
  2012. // Multiply by approximate reciprocal of MAX_LOAD (0.85), with pow2 denominator.
  2013. size_t need_entries = (expected_size + 1) * 1204 / 1024;
  2014. UPB_ASSERT(need_entries >= expected_size * 0.85);
  2015. int size_lg2 = _upb_lg2ceil(need_entries);
  2016. return init(&t->t, size_lg2, a);
  2017. }
  2018. void upb_strtable_clear(upb_strtable *t) {
  2019. size_t bytes = upb_table_size(&t->t) * sizeof(upb_tabent);
  2020. t->t.count = 0;
  2021. memset((char*)t->t.entries, 0, bytes);
  2022. }
  2023. bool upb_strtable_resize(upb_strtable *t, size_t size_lg2, upb_arena *a) {
  2024. upb_strtable new_table;
  2025. upb_strtable_iter i;
  2026. if (!init(&new_table.t, size_lg2, a))
  2027. return false;
  2028. upb_strtable_begin(&i, t);
  2029. for ( ; !upb_strtable_done(&i); upb_strtable_next(&i)) {
  2030. upb_strview key = upb_strtable_iter_key(&i);
  2031. upb_strtable_insert(&new_table, key.data, key.size,
  2032. upb_strtable_iter_value(&i), a);
  2033. }
  2034. *t = new_table;
  2035. return true;
  2036. }
  2037. bool upb_strtable_insert(upb_strtable *t, const char *k, size_t len,
  2038. upb_value v, upb_arena *a) {
  2039. lookupkey_t key;
  2040. upb_tabkey tabkey;
  2041. uint32_t hash;
  2042. if (isfull(&t->t)) {
  2043. /* Need to resize. New table of double the size, add old elements to it. */
  2044. if (!upb_strtable_resize(t, t->t.size_lg2 + 1, a)) {
  2045. return false;
  2046. }
  2047. }
  2048. key = strkey2(k, len);
  2049. tabkey = strcopy(key, a);
  2050. if (tabkey == 0) return false;
  2051. hash = table_hash(key.str.str, key.str.len);
  2052. insert(&t->t, key, tabkey, v, hash, &strhash, &streql);
  2053. return true;
  2054. }
  2055. bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len,
  2056. upb_value *v) {
  2057. uint32_t hash = table_hash(key, len);
  2058. return lookup(&t->t, strkey2(key, len), v, hash, &streql);
  2059. }
  2060. bool upb_strtable_remove(upb_strtable *t, const char *key, size_t len,
  2061. upb_value *val) {
  2062. uint32_t hash = table_hash(key, len);
  2063. upb_tabkey tabkey;
  2064. return rm(&t->t, strkey2(key, len), val, &tabkey, hash, &streql);
  2065. }
  2066. /* Iteration */
  2067. void upb_strtable_begin(upb_strtable_iter *i, const upb_strtable *t) {
  2068. i->t = t;
  2069. i->index = begin(&t->t);
  2070. }
  2071. void upb_strtable_next(upb_strtable_iter *i) {
  2072. i->index = next(&i->t->t, i->index);
  2073. }
  2074. bool upb_strtable_done(const upb_strtable_iter *i) {
  2075. if (!i->t) return true;
  2076. return i->index >= upb_table_size(&i->t->t) ||
  2077. upb_tabent_isempty(str_tabent(i));
  2078. }
  2079. upb_strview upb_strtable_iter_key(const upb_strtable_iter *i) {
  2080. upb_strview key;
  2081. uint32_t len;
  2082. UPB_ASSERT(!upb_strtable_done(i));
  2083. key.data = upb_tabstr(str_tabent(i)->key, &len);
  2084. key.size = len;
  2085. return key;
  2086. }
  2087. upb_value upb_strtable_iter_value(const upb_strtable_iter *i) {
  2088. UPB_ASSERT(!upb_strtable_done(i));
  2089. return _upb_value_val(str_tabent(i)->val.val);
  2090. }
  2091. void upb_strtable_iter_setdone(upb_strtable_iter *i) {
  2092. i->t = NULL;
  2093. i->index = SIZE_MAX;
  2094. }
  2095. bool upb_strtable_iter_isequal(const upb_strtable_iter *i1,
  2096. const upb_strtable_iter *i2) {
  2097. if (upb_strtable_done(i1) && upb_strtable_done(i2))
  2098. return true;
  2099. return i1->t == i2->t && i1->index == i2->index;
  2100. }
  2101. /* upb_inttable ***************************************************************/
  2102. /* For inttables we use a hybrid structure where small keys are kept in an
  2103. * array and large keys are put in the hash table. */
  2104. static uint32_t inthash(upb_tabkey key) { return upb_inthash(key); }
  2105. static bool inteql(upb_tabkey k1, lookupkey_t k2) {
  2106. return k1 == k2.num;
  2107. }
  2108. static upb_tabval *mutable_array(upb_inttable *t) {
  2109. return (upb_tabval*)t->array;
  2110. }
  2111. static upb_tabval *inttable_val(upb_inttable *t, uintptr_t key) {
  2112. if (key < t->array_size) {
  2113. return upb_arrhas(t->array[key]) ? &(mutable_array(t)[key]) : NULL;
  2114. } else {
  2115. upb_tabent *e =
  2116. findentry_mutable(&t->t, intkey(key), upb_inthash(key), &inteql);
  2117. return e ? &e->val : NULL;
  2118. }
  2119. }
  2120. static const upb_tabval *inttable_val_const(const upb_inttable *t,
  2121. uintptr_t key) {
  2122. return inttable_val((upb_inttable*)t, key);
  2123. }
  2124. size_t upb_inttable_count(const upb_inttable *t) {
  2125. return t->t.count + t->array_count;
  2126. }
  2127. static void check(upb_inttable *t) {
  2128. UPB_UNUSED(t);
  2129. #if defined(UPB_DEBUG_TABLE) && !defined(NDEBUG)
  2130. {
  2131. /* This check is very expensive (makes inserts/deletes O(N)). */
  2132. size_t count = 0;
  2133. upb_inttable_iter i;
  2134. upb_inttable_begin(&i, t);
  2135. for(; !upb_inttable_done(&i); upb_inttable_next(&i), count++) {
  2136. UPB_ASSERT(upb_inttable_lookup(t, upb_inttable_iter_key(&i), NULL));
  2137. }
  2138. UPB_ASSERT(count == upb_inttable_count(t));
  2139. }
  2140. #endif
  2141. }
  2142. bool upb_inttable_sizedinit(upb_inttable *t, size_t asize, int hsize_lg2,
  2143. upb_arena *a) {
  2144. size_t array_bytes;
  2145. if (!init(&t->t, hsize_lg2, a)) return false;
  2146. /* Always make the array part at least 1 long, so that we know key 0
  2147. * won't be in the hash part, which simplifies things. */
  2148. t->array_size = UPB_MAX(1, asize);
  2149. t->array_count = 0;
  2150. array_bytes = t->array_size * sizeof(upb_value);
  2151. t->array = upb_arena_malloc(a, array_bytes);
  2152. if (!t->array) {
  2153. return false;
  2154. }
  2155. memset(mutable_array(t), 0xff, array_bytes);
  2156. check(t);
  2157. return true;
  2158. }
  2159. bool upb_inttable_init(upb_inttable *t, upb_arena *a) {
  2160. return upb_inttable_sizedinit(t, 0, 4, a);
  2161. }
  2162. bool upb_inttable_insert(upb_inttable *t, uintptr_t key, upb_value val,
  2163. upb_arena *a) {
  2164. upb_tabval tabval;
  2165. tabval.val = val.val;
  2166. UPB_ASSERT(upb_arrhas(tabval)); /* This will reject (uint64_t)-1. Fix this. */
  2167. if (key < t->array_size) {
  2168. UPB_ASSERT(!upb_arrhas(t->array[key]));
  2169. t->array_count++;
  2170. mutable_array(t)[key].val = val.val;
  2171. } else {
  2172. if (isfull(&t->t)) {
  2173. /* Need to resize the hash part, but we re-use the array part. */
  2174. size_t i;
  2175. upb_table new_table;
  2176. if (!init(&new_table, t->t.size_lg2 + 1, a)) {
  2177. return false;
  2178. }
  2179. for (i = begin(&t->t); i < upb_table_size(&t->t); i = next(&t->t, i)) {
  2180. const upb_tabent *e = &t->t.entries[i];
  2181. uint32_t hash;
  2182. upb_value v;
  2183. _upb_value_setval(&v, e->val.val);
  2184. hash = upb_inthash(e->key);
  2185. insert(&new_table, intkey(e->key), e->key, v, hash, &inthash, &inteql);
  2186. }
  2187. UPB_ASSERT(t->t.count == new_table.count);
  2188. t->t = new_table;
  2189. }
  2190. insert(&t->t, intkey(key), key, val, upb_inthash(key), &inthash, &inteql);
  2191. }
  2192. check(t);
  2193. return true;
  2194. }
  2195. bool upb_inttable_lookup(const upb_inttable *t, uintptr_t key, upb_value *v) {
  2196. const upb_tabval *table_v = inttable_val_const(t, key);
  2197. if (!table_v) return false;
  2198. if (v) _upb_value_setval(v, table_v->val);
  2199. return true;
  2200. }
  2201. bool upb_inttable_replace(upb_inttable *t, uintptr_t key, upb_value val) {
  2202. upb_tabval *table_v = inttable_val(t, key);
  2203. if (!table_v) return false;
  2204. table_v->val = val.val;
  2205. return true;
  2206. }
  2207. bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val) {
  2208. bool success;
  2209. if (key < t->array_size) {
  2210. if (upb_arrhas(t->array[key])) {
  2211. upb_tabval empty = UPB_TABVALUE_EMPTY_INIT;
  2212. t->array_count--;
  2213. if (val) {
  2214. _upb_value_setval(val, t->array[key].val);
  2215. }
  2216. mutable_array(t)[key] = empty;
  2217. success = true;
  2218. } else {
  2219. success = false;
  2220. }
  2221. } else {
  2222. success = rm(&t->t, intkey(key), val, NULL, upb_inthash(key), &inteql);
  2223. }
  2224. check(t);
  2225. return success;
  2226. }
  2227. void upb_inttable_compact(upb_inttable *t, upb_arena *a) {
  2228. /* A power-of-two histogram of the table keys. */
  2229. size_t counts[UPB_MAXARRSIZE + 1] = {0};
  2230. /* The max key in each bucket. */
  2231. uintptr_t max[UPB_MAXARRSIZE + 1] = {0};
  2232. upb_inttable_iter i;
  2233. size_t arr_count;
  2234. int size_lg2;
  2235. upb_inttable new_t;
  2236. upb_inttable_begin(&i, t);
  2237. for (; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  2238. uintptr_t key = upb_inttable_iter_key(&i);
  2239. int bucket = log2ceil(key);
  2240. max[bucket] = UPB_MAX(max[bucket], key);
  2241. counts[bucket]++;
  2242. }
  2243. /* Find the largest power of two that satisfies the MIN_DENSITY
  2244. * definition (while actually having some keys). */
  2245. arr_count = upb_inttable_count(t);
  2246. for (size_lg2 = ARRAY_SIZE(counts) - 1; size_lg2 > 0; size_lg2--) {
  2247. if (counts[size_lg2] == 0) {
  2248. /* We can halve again without losing any entries. */
  2249. continue;
  2250. } else if (arr_count >= (1 << size_lg2) * MIN_DENSITY) {
  2251. break;
  2252. }
  2253. arr_count -= counts[size_lg2];
  2254. }
  2255. UPB_ASSERT(arr_count <= upb_inttable_count(t));
  2256. {
  2257. /* Insert all elements into new, perfectly-sized table. */
  2258. size_t arr_size = max[size_lg2] + 1; /* +1 so arr[max] will fit. */
  2259. size_t hash_count = upb_inttable_count(t) - arr_count;
  2260. size_t hash_size = hash_count ? (hash_count / MAX_LOAD) + 1 : 0;
  2261. int hashsize_lg2 = log2ceil(hash_size);
  2262. upb_inttable_sizedinit(&new_t, arr_size, hashsize_lg2, a);
  2263. upb_inttable_begin(&i, t);
  2264. for (; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  2265. uintptr_t k = upb_inttable_iter_key(&i);
  2266. upb_inttable_insert(&new_t, k, upb_inttable_iter_value(&i), a);
  2267. }
  2268. UPB_ASSERT(new_t.array_size == arr_size);
  2269. UPB_ASSERT(new_t.t.size_lg2 == hashsize_lg2);
  2270. }
  2271. *t = new_t;
  2272. }
  2273. /* Iteration. */
  2274. static const upb_tabent *int_tabent(const upb_inttable_iter *i) {
  2275. UPB_ASSERT(!i->array_part);
  2276. return &i->t->t.entries[i->index];
  2277. }
  2278. static upb_tabval int_arrent(const upb_inttable_iter *i) {
  2279. UPB_ASSERT(i->array_part);
  2280. return i->t->array[i->index];
  2281. }
  2282. void upb_inttable_begin(upb_inttable_iter *i, const upb_inttable *t) {
  2283. i->t = t;
  2284. i->index = -1;
  2285. i->array_part = true;
  2286. upb_inttable_next(i);
  2287. }
  2288. void upb_inttable_next(upb_inttable_iter *iter) {
  2289. const upb_inttable *t = iter->t;
  2290. if (iter->array_part) {
  2291. while (++iter->index < t->array_size) {
  2292. if (upb_arrhas(int_arrent(iter))) {
  2293. return;
  2294. }
  2295. }
  2296. iter->array_part = false;
  2297. iter->index = begin(&t->t);
  2298. } else {
  2299. iter->index = next(&t->t, iter->index);
  2300. }
  2301. }
  2302. bool upb_inttable_done(const upb_inttable_iter *i) {
  2303. if (!i->t) return true;
  2304. if (i->array_part) {
  2305. return i->index >= i->t->array_size ||
  2306. !upb_arrhas(int_arrent(i));
  2307. } else {
  2308. return i->index >= upb_table_size(&i->t->t) ||
  2309. upb_tabent_isempty(int_tabent(i));
  2310. }
  2311. }
  2312. uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i) {
  2313. UPB_ASSERT(!upb_inttable_done(i));
  2314. return i->array_part ? i->index : int_tabent(i)->key;
  2315. }
  2316. upb_value upb_inttable_iter_value(const upb_inttable_iter *i) {
  2317. UPB_ASSERT(!upb_inttable_done(i));
  2318. return _upb_value_val(
  2319. i->array_part ? i->t->array[i->index].val : int_tabent(i)->val.val);
  2320. }
  2321. void upb_inttable_iter_setdone(upb_inttable_iter *i) {
  2322. i->t = NULL;
  2323. i->index = SIZE_MAX;
  2324. i->array_part = false;
  2325. }
  2326. bool upb_inttable_iter_isequal(const upb_inttable_iter *i1,
  2327. const upb_inttable_iter *i2) {
  2328. if (upb_inttable_done(i1) && upb_inttable_done(i2))
  2329. return true;
  2330. return i1->t == i2->t && i1->index == i2->index &&
  2331. i1->array_part == i2->array_part;
  2332. }
  2333. /** upb/upb.c ************************************************************/
  2334. #include <errno.h>
  2335. #include <stdarg.h>
  2336. #include <stddef.h>
  2337. #include <stdint.h>
  2338. #include <stdio.h>
  2339. #include <stdlib.h>
  2340. #include <string.h>
  2341. /* upb_status *****************************************************************/
  2342. void upb_status_clear(upb_status *status) {
  2343. if (!status) return;
  2344. status->ok = true;
  2345. status->msg[0] = '\0';
  2346. }
  2347. bool upb_ok(const upb_status *status) { return status->ok; }
  2348. const char *upb_status_errmsg(const upb_status *status) { return status->msg; }
  2349. void upb_status_seterrmsg(upb_status *status, const char *msg) {
  2350. if (!status) return;
  2351. status->ok = false;
  2352. strncpy(status->msg, msg, UPB_STATUS_MAX_MESSAGE - 1);
  2353. status->msg[UPB_STATUS_MAX_MESSAGE - 1] = '\0';
  2354. }
  2355. void upb_status_seterrf(upb_status *status, const char *fmt, ...) {
  2356. va_list args;
  2357. va_start(args, fmt);
  2358. upb_status_vseterrf(status, fmt, args);
  2359. va_end(args);
  2360. }
  2361. void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args) {
  2362. if (!status) return;
  2363. status->ok = false;
  2364. vsnprintf(status->msg, sizeof(status->msg), fmt, args);
  2365. status->msg[UPB_STATUS_MAX_MESSAGE - 1] = '\0';
  2366. }
  2367. void upb_status_vappenderrf(upb_status *status, const char *fmt, va_list args) {
  2368. size_t len;
  2369. if (!status) return;
  2370. status->ok = false;
  2371. len = strlen(status->msg);
  2372. vsnprintf(status->msg + len, sizeof(status->msg) - len, fmt, args);
  2373. status->msg[UPB_STATUS_MAX_MESSAGE - 1] = '\0';
  2374. }
  2375. /* upb_alloc ******************************************************************/
  2376. static void *upb_global_allocfunc(upb_alloc *alloc, void *ptr, size_t oldsize,
  2377. size_t size) {
  2378. UPB_UNUSED(alloc);
  2379. UPB_UNUSED(oldsize);
  2380. if (size == 0) {
  2381. free(ptr);
  2382. return NULL;
  2383. } else {
  2384. return realloc(ptr, size);
  2385. }
  2386. }
  2387. static uint32_t *upb_cleanup_pointer(uintptr_t cleanup_metadata) {
  2388. return (uint32_t *)(cleanup_metadata & ~0x1);
  2389. }
  2390. static bool upb_cleanup_has_initial_block(uintptr_t cleanup_metadata) {
  2391. return cleanup_metadata & 0x1;
  2392. }
  2393. static uintptr_t upb_cleanup_metadata(uint32_t *cleanup,
  2394. bool has_initial_block) {
  2395. return (uintptr_t)cleanup | has_initial_block;
  2396. }
  2397. upb_alloc upb_alloc_global = {&upb_global_allocfunc};
  2398. /* upb_arena ******************************************************************/
  2399. /* Be conservative and choose 16 in case anyone is using SSE. */
  2400. struct mem_block {
  2401. struct mem_block *next;
  2402. uint32_t size;
  2403. uint32_t cleanups;
  2404. /* Data follows. */
  2405. };
  2406. typedef struct cleanup_ent {
  2407. upb_cleanup_func *cleanup;
  2408. void *ud;
  2409. } cleanup_ent;
  2410. static const size_t memblock_reserve = UPB_ALIGN_UP(sizeof(mem_block), 16);
  2411. static upb_arena *arena_findroot(upb_arena *a) {
  2412. /* Path splitting keeps time complexity down, see:
  2413. * https://en.wikipedia.org/wiki/Disjoint-set_data_structure */
  2414. while (a->parent != a) {
  2415. upb_arena *next = a->parent;
  2416. a->parent = next->parent;
  2417. a = next;
  2418. }
  2419. return a;
  2420. }
  2421. static void upb_arena_addblock(upb_arena *a, upb_arena *root, void *ptr,
  2422. size_t size) {
  2423. mem_block *block = ptr;
  2424. /* The block is for arena |a|, but should appear in the freelist of |root|. */
  2425. block->next = root->freelist;
  2426. block->size = (uint32_t)size;
  2427. block->cleanups = 0;
  2428. root->freelist = block;
  2429. a->last_size = block->size;
  2430. if (!root->freelist_tail) root->freelist_tail = block;
  2431. a->head.ptr = UPB_PTR_AT(block, memblock_reserve, char);
  2432. a->head.end = UPB_PTR_AT(block, size, char);
  2433. a->cleanup_metadata = upb_cleanup_metadata(
  2434. &block->cleanups, upb_cleanup_has_initial_block(a->cleanup_metadata));
  2435. UPB_POISON_MEMORY_REGION(a->head.ptr, a->head.end - a->head.ptr);
  2436. }
  2437. static bool upb_arena_allocblock(upb_arena *a, size_t size) {
  2438. upb_arena *root = arena_findroot(a);
  2439. size_t block_size = UPB_MAX(size, a->last_size * 2) + memblock_reserve;
  2440. mem_block *block = upb_malloc(root->block_alloc, block_size);
  2441. if (!block) return false;
  2442. upb_arena_addblock(a, root, block, block_size);
  2443. return true;
  2444. }
  2445. void *_upb_arena_slowmalloc(upb_arena *a, size_t size) {
  2446. if (!upb_arena_allocblock(a, size)) return NULL; /* Out of memory. */
  2447. UPB_ASSERT(_upb_arenahas(a) >= size);
  2448. return upb_arena_malloc(a, size);
  2449. }
  2450. static void *upb_arena_doalloc(upb_alloc *alloc, void *ptr, size_t oldsize,
  2451. size_t size) {
  2452. upb_arena *a = (upb_arena*)alloc; /* upb_alloc is initial member. */
  2453. return upb_arena_realloc(a, ptr, oldsize, size);
  2454. }
  2455. /* Public Arena API ***********************************************************/
  2456. upb_arena *arena_initslow(void *mem, size_t n, upb_alloc *alloc) {
  2457. const size_t first_block_overhead = sizeof(upb_arena) + memblock_reserve;
  2458. upb_arena *a;
  2459. /* We need to malloc the initial block. */
  2460. n = first_block_overhead + 256;
  2461. if (!alloc || !(mem = upb_malloc(alloc, n))) {
  2462. return NULL;
  2463. }
  2464. a = UPB_PTR_AT(mem, n - sizeof(*a), upb_arena);
  2465. n -= sizeof(*a);
  2466. a->head.alloc.func = &upb_arena_doalloc;
  2467. a->block_alloc = alloc;
  2468. a->parent = a;
  2469. a->refcount = 1;
  2470. a->freelist = NULL;
  2471. a->freelist_tail = NULL;
  2472. a->cleanup_metadata = upb_cleanup_metadata(NULL, false);
  2473. upb_arena_addblock(a, a, mem, n);
  2474. return a;
  2475. }
  2476. upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc) {
  2477. upb_arena *a;
  2478. /* Round block size down to alignof(*a) since we will allocate the arena
  2479. * itself at the end. */
  2480. n = UPB_ALIGN_DOWN(n, UPB_ALIGN_OF(upb_arena));
  2481. if (UPB_UNLIKELY(n < sizeof(upb_arena))) {
  2482. return arena_initslow(mem, n, alloc);
  2483. }
  2484. a = UPB_PTR_AT(mem, n - sizeof(*a), upb_arena);
  2485. a->head.alloc.func = &upb_arena_doalloc;
  2486. a->block_alloc = alloc;
  2487. a->parent = a;
  2488. a->refcount = 1;
  2489. a->last_size = UPB_MAX(128, n);
  2490. a->head.ptr = mem;
  2491. a->head.end = UPB_PTR_AT(mem, n - sizeof(*a), char);
  2492. a->freelist = NULL;
  2493. a->cleanup_metadata = upb_cleanup_metadata(NULL, true);
  2494. return a;
  2495. }
  2496. static void arena_dofree(upb_arena *a) {
  2497. mem_block *block = a->freelist;
  2498. UPB_ASSERT(a->parent == a);
  2499. UPB_ASSERT(a->refcount == 0);
  2500. while (block) {
  2501. /* Load first since we are deleting block. */
  2502. mem_block *next = block->next;
  2503. if (block->cleanups > 0) {
  2504. cleanup_ent *end = UPB_PTR_AT(block, block->size, void);
  2505. cleanup_ent *ptr = end - block->cleanups;
  2506. for (; ptr < end; ptr++) {
  2507. ptr->cleanup(ptr->ud);
  2508. }
  2509. }
  2510. upb_free(a->block_alloc, block);
  2511. block = next;
  2512. }
  2513. }
  2514. void upb_arena_free(upb_arena *a) {
  2515. a = arena_findroot(a);
  2516. if (--a->refcount == 0) arena_dofree(a);
  2517. }
  2518. bool upb_arena_addcleanup(upb_arena *a, void *ud, upb_cleanup_func *func) {
  2519. cleanup_ent *ent;
  2520. uint32_t* cleanups = upb_cleanup_pointer(a->cleanup_metadata);
  2521. if (!cleanups || _upb_arenahas(a) < sizeof(cleanup_ent)) {
  2522. if (!upb_arena_allocblock(a, 128)) return false; /* Out of memory. */
  2523. UPB_ASSERT(_upb_arenahas(a) >= sizeof(cleanup_ent));
  2524. cleanups = upb_cleanup_pointer(a->cleanup_metadata);
  2525. }
  2526. a->head.end -= sizeof(cleanup_ent);
  2527. ent = (cleanup_ent*)a->head.end;
  2528. (*cleanups)++;
  2529. UPB_UNPOISON_MEMORY_REGION(ent, sizeof(cleanup_ent));
  2530. ent->cleanup = func;
  2531. ent->ud = ud;
  2532. return true;
  2533. }
  2534. bool upb_arena_fuse(upb_arena *a1, upb_arena *a2) {
  2535. upb_arena *r1 = arena_findroot(a1);
  2536. upb_arena *r2 = arena_findroot(a2);
  2537. if (r1 == r2) return true; /* Already fused. */
  2538. /* Do not fuse initial blocks since we cannot lifetime extend them. */
  2539. if (upb_cleanup_has_initial_block(r1->cleanup_metadata)) return false;
  2540. if (upb_cleanup_has_initial_block(r2->cleanup_metadata)) return false;
  2541. /* Only allow fuse with a common allocator */
  2542. if (r1->block_alloc != r2->block_alloc) return false;
  2543. /* We want to join the smaller tree to the larger tree.
  2544. * So swap first if they are backwards. */
  2545. if (r1->refcount < r2->refcount) {
  2546. upb_arena *tmp = r1;
  2547. r1 = r2;
  2548. r2 = tmp;
  2549. }
  2550. /* r1 takes over r2's freelist and refcount. */
  2551. r1->refcount += r2->refcount;
  2552. if (r2->freelist_tail) {
  2553. UPB_ASSERT(r2->freelist_tail->next == NULL);
  2554. r2->freelist_tail->next = r1->freelist;
  2555. r1->freelist = r2->freelist;
  2556. }
  2557. r2->parent = r1;
  2558. return true;
  2559. }
  2560. /** upb/decode_fast.c ************************************************************/
  2561. // Fast decoder: ~3x the speed of decode.c, but requires x86-64/ARM64.
  2562. // Also the table size grows by 2x.
  2563. //
  2564. // Could potentially be ported to other 64-bit archs that pass at least six
  2565. // arguments in registers and have 8 unused high bits in pointers.
  2566. //
  2567. // The overall design is to create specialized functions for every possible
  2568. // field type (eg. oneof boolean field with a 1 byte tag) and then dispatch
  2569. // to the specialized function as quickly as possible.
  2570. /* Must be last. */
  2571. #if UPB_FASTTABLE
  2572. // The standard set of arguments passed to each parsing function.
  2573. // Thanks to x86-64 calling conventions, these will stay in registers.
  2574. #define UPB_PARSE_PARAMS \
  2575. upb_decstate *d, const char *ptr, upb_msg *msg, intptr_t table, \
  2576. uint64_t hasbits, uint64_t data
  2577. #define UPB_PARSE_ARGS d, ptr, msg, table, hasbits, data
  2578. #define RETURN_GENERIC(m) \
  2579. /* Uncomment either of these for debugging purposes. */ \
  2580. /* fprintf(stderr, m); */ \
  2581. /*__builtin_trap(); */ \
  2582. return fastdecode_generic(d, ptr, msg, table, hasbits, 0);
  2583. typedef enum {
  2584. CARD_s = 0, /* Singular (optional, non-repeated) */
  2585. CARD_o = 1, /* Oneof */
  2586. CARD_r = 2, /* Repeated */
  2587. CARD_p = 3 /* Packed Repeated */
  2588. } upb_card;
  2589. UPB_NOINLINE
  2590. static const char *fastdecode_isdonefallback(UPB_PARSE_PARAMS) {
  2591. int overrun = data;
  2592. ptr = decode_isdonefallback_inl(d, ptr, overrun);
  2593. if (ptr == NULL) {
  2594. return fastdecode_err(d);
  2595. }
  2596. data = fastdecode_loadtag(ptr);
  2597. UPB_MUSTTAIL return fastdecode_tagdispatch(UPB_PARSE_ARGS);
  2598. }
  2599. UPB_FORCEINLINE
  2600. static const char *fastdecode_dispatch(UPB_PARSE_PARAMS) {
  2601. if (UPB_UNLIKELY(ptr >= d->limit_ptr)) {
  2602. int overrun = ptr - d->end;
  2603. if (UPB_LIKELY(overrun == d->limit)) {
  2604. // Parse is finished.
  2605. *(uint32_t*)msg |= hasbits; // Sync hasbits.
  2606. return ptr;
  2607. } else {
  2608. data = overrun;
  2609. UPB_MUSTTAIL return fastdecode_isdonefallback(UPB_PARSE_ARGS);
  2610. }
  2611. }
  2612. // Read two bytes of tag data (for a one-byte tag, the high byte is junk).
  2613. data = fastdecode_loadtag(ptr);
  2614. UPB_MUSTTAIL return fastdecode_tagdispatch(UPB_PARSE_ARGS);
  2615. }
  2616. UPB_FORCEINLINE
  2617. static bool fastdecode_checktag(uint16_t data, int tagbytes) {
  2618. if (tagbytes == 1) {
  2619. return (data & 0xff) == 0;
  2620. } else {
  2621. return data == 0;
  2622. }
  2623. }
  2624. UPB_FORCEINLINE
  2625. static const char *fastdecode_longsize(const char *ptr, int *size) {
  2626. int i;
  2627. UPB_ASSERT(*size & 0x80);
  2628. *size &= 0xff;
  2629. for (i = 0; i < 3; i++) {
  2630. ptr++;
  2631. size_t byte = (uint8_t)ptr[-1];
  2632. *size += (byte - 1) << (7 + 7 * i);
  2633. if (UPB_LIKELY((byte & 0x80) == 0)) return ptr;
  2634. }
  2635. ptr++;
  2636. size_t byte = (uint8_t)ptr[-1];
  2637. // len is limited by 2gb not 4gb, hence 8 and not 16 as normally expected
  2638. // for a 32 bit varint.
  2639. if (UPB_UNLIKELY(byte >= 8)) return NULL;
  2640. *size += (byte - 1) << 28;
  2641. return ptr;
  2642. }
  2643. UPB_FORCEINLINE
  2644. static bool fastdecode_boundscheck(const char *ptr, size_t len,
  2645. const char *end) {
  2646. uintptr_t uptr = (uintptr_t)ptr;
  2647. uintptr_t uend = (uintptr_t)end + 16;
  2648. uintptr_t res = uptr + len;
  2649. return res < uptr || res > uend;
  2650. }
  2651. UPB_FORCEINLINE
  2652. static bool fastdecode_boundscheck2(const char *ptr, size_t len,
  2653. const char *end) {
  2654. // This is one extra branch compared to the more normal:
  2655. // return (size_t)(end - ptr) < size;
  2656. // However it is one less computation if we are just about to use "ptr + len":
  2657. // https://godbolt.org/z/35YGPz
  2658. // In microbenchmarks this shows an overall 4% improvement.
  2659. uintptr_t uptr = (uintptr_t)ptr;
  2660. uintptr_t uend = (uintptr_t)end;
  2661. uintptr_t res = uptr + len;
  2662. return res < uptr || res > uend;
  2663. }
  2664. typedef const char *fastdecode_delimfunc(upb_decstate *d, const char *ptr,
  2665. void *ctx);
  2666. UPB_FORCEINLINE
  2667. static const char *fastdecode_delimited(upb_decstate *d, const char *ptr,
  2668. fastdecode_delimfunc *func, void *ctx) {
  2669. ptr++;
  2670. int len = (int8_t)ptr[-1];
  2671. if (fastdecode_boundscheck2(ptr, len, d->limit_ptr)) {
  2672. // Slow case: Sub-message is >=128 bytes and/or exceeds the current buffer.
  2673. // If it exceeds the buffer limit, limit/limit_ptr will change during
  2674. // sub-message parsing, so we need to preserve delta, not limit.
  2675. if (UPB_UNLIKELY(len & 0x80)) {
  2676. // Size varint >1 byte (length >= 128).
  2677. ptr = fastdecode_longsize(ptr, &len);
  2678. if (!ptr) {
  2679. // Corrupt wire format: size exceeded INT_MAX.
  2680. return NULL;
  2681. }
  2682. }
  2683. if (ptr - d->end + (int)len > d->limit) {
  2684. // Corrupt wire format: invalid limit.
  2685. return NULL;
  2686. }
  2687. int delta = decode_pushlimit(d, ptr, len);
  2688. ptr = func(d, ptr, ctx);
  2689. decode_poplimit(d, ptr, delta);
  2690. } else {
  2691. // Fast case: Sub-message is <128 bytes and fits in the current buffer.
  2692. // This means we can preserve limit/limit_ptr verbatim.
  2693. const char *saved_limit_ptr = d->limit_ptr;
  2694. int saved_limit = d->limit;
  2695. d->limit_ptr = ptr + len;
  2696. d->limit = d->limit_ptr - d->end;
  2697. UPB_ASSERT(d->limit_ptr == d->end + UPB_MIN(0, d->limit));
  2698. ptr = func(d, ptr, ctx);
  2699. d->limit_ptr = saved_limit_ptr;
  2700. d->limit = saved_limit;
  2701. UPB_ASSERT(d->limit_ptr == d->end + UPB_MIN(0, d->limit));
  2702. }
  2703. return ptr;
  2704. }
  2705. /* singular, oneof, repeated field handling ***********************************/
  2706. typedef struct {
  2707. upb_array *arr;
  2708. void *end;
  2709. } fastdecode_arr;
  2710. typedef enum {
  2711. FD_NEXT_ATLIMIT,
  2712. FD_NEXT_SAMEFIELD,
  2713. FD_NEXT_OTHERFIELD
  2714. } fastdecode_next;
  2715. typedef struct {
  2716. void *dst;
  2717. fastdecode_next next;
  2718. uint32_t tag;
  2719. } fastdecode_nextret;
  2720. UPB_FORCEINLINE
  2721. static void *fastdecode_resizearr(upb_decstate *d, void *dst,
  2722. fastdecode_arr *farr, int valbytes) {
  2723. if (UPB_UNLIKELY(dst == farr->end)) {
  2724. size_t old_size = farr->arr->size;
  2725. size_t old_bytes = old_size * valbytes;
  2726. size_t new_size = old_size * 2;
  2727. size_t new_bytes = new_size * valbytes;
  2728. char *old_ptr = _upb_array_ptr(farr->arr);
  2729. char *new_ptr = upb_arena_realloc(&d->arena, old_ptr, old_bytes, new_bytes);
  2730. uint8_t elem_size_lg2 = __builtin_ctz(valbytes);
  2731. farr->arr->size = new_size;
  2732. farr->arr->data = _upb_array_tagptr(new_ptr, elem_size_lg2);
  2733. dst = (void*)(new_ptr + (old_size * valbytes));
  2734. farr->end = (void*)(new_ptr + (new_size * valbytes));
  2735. }
  2736. return dst;
  2737. }
  2738. UPB_FORCEINLINE
  2739. static bool fastdecode_tagmatch(uint32_t tag, uint64_t data, int tagbytes) {
  2740. if (tagbytes == 1) {
  2741. return (uint8_t)tag == (uint8_t)data;
  2742. } else {
  2743. return (uint16_t)tag == (uint16_t)data;
  2744. }
  2745. }
  2746. UPB_FORCEINLINE
  2747. static void fastdecode_commitarr(void *dst, fastdecode_arr *farr,
  2748. int valbytes) {
  2749. farr->arr->len =
  2750. (size_t)((char *)dst - (char *)_upb_array_ptr(farr->arr)) / valbytes;
  2751. }
  2752. UPB_FORCEINLINE
  2753. static fastdecode_nextret fastdecode_nextrepeated(upb_decstate *d, void *dst,
  2754. const char **ptr,
  2755. fastdecode_arr *farr,
  2756. uint64_t data, int tagbytes,
  2757. int valbytes) {
  2758. fastdecode_nextret ret;
  2759. dst = (char *)dst + valbytes;
  2760. if (UPB_LIKELY(!decode_isdone(d, ptr))) {
  2761. ret.tag = fastdecode_loadtag(*ptr);
  2762. if (fastdecode_tagmatch(ret.tag, data, tagbytes)) {
  2763. ret.next = FD_NEXT_SAMEFIELD;
  2764. } else {
  2765. fastdecode_commitarr(dst, farr, valbytes);
  2766. ret.next = FD_NEXT_OTHERFIELD;
  2767. }
  2768. } else {
  2769. fastdecode_commitarr(dst, farr, valbytes);
  2770. ret.next = FD_NEXT_ATLIMIT;
  2771. }
  2772. ret.dst = dst;
  2773. return ret;
  2774. }
  2775. UPB_FORCEINLINE
  2776. static void *fastdecode_fieldmem(upb_msg *msg, uint64_t data) {
  2777. size_t ofs = data >> 48;
  2778. return (char *)msg + ofs;
  2779. }
  2780. UPB_FORCEINLINE
  2781. static void *fastdecode_getfield(upb_decstate *d, const char *ptr, upb_msg *msg,
  2782. uint64_t *data, uint64_t *hasbits,
  2783. fastdecode_arr *farr, int valbytes,
  2784. upb_card card) {
  2785. switch (card) {
  2786. case CARD_s: {
  2787. uint8_t hasbit_index = *data >> 24;
  2788. // Set hasbit and return pointer to scalar field.
  2789. *hasbits |= 1ull << hasbit_index;
  2790. return fastdecode_fieldmem(msg, *data);
  2791. }
  2792. case CARD_o: {
  2793. uint16_t case_ofs = *data >> 32;
  2794. uint32_t *oneof_case = UPB_PTR_AT(msg, case_ofs, uint32_t);
  2795. uint8_t field_number = *data >> 24;
  2796. *oneof_case = field_number;
  2797. return fastdecode_fieldmem(msg, *data);
  2798. }
  2799. case CARD_r: {
  2800. // Get pointer to upb_array and allocate/expand if necessary.
  2801. uint8_t elem_size_lg2 = __builtin_ctz(valbytes);
  2802. upb_array **arr_p = fastdecode_fieldmem(msg, *data);
  2803. char *begin;
  2804. *(uint32_t*)msg |= *hasbits;
  2805. *hasbits = 0;
  2806. if (UPB_LIKELY(!*arr_p)) {
  2807. farr->arr = _upb_array_new(&d->arena, 8, elem_size_lg2);
  2808. *arr_p = farr->arr;
  2809. } else {
  2810. farr->arr = *arr_p;
  2811. }
  2812. begin = _upb_array_ptr(farr->arr);
  2813. farr->end = begin + (farr->arr->size * valbytes);
  2814. *data = fastdecode_loadtag(ptr);
  2815. return begin + (farr->arr->len * valbytes);
  2816. }
  2817. default:
  2818. UPB_UNREACHABLE();
  2819. }
  2820. }
  2821. UPB_FORCEINLINE
  2822. static bool fastdecode_flippacked(uint64_t *data, int tagbytes) {
  2823. *data ^= (0x2 ^ 0x0); // Patch data to match packed wiretype.
  2824. return fastdecode_checktag(*data, tagbytes);
  2825. }
  2826. #define FASTDECODE_CHECKPACKED(tagbytes, card, func) \
  2827. if (UPB_UNLIKELY(!fastdecode_checktag(data, tagbytes))) { \
  2828. if (card == CARD_r && fastdecode_flippacked(&data, tagbytes)) { \
  2829. UPB_MUSTTAIL return func(UPB_PARSE_ARGS); \
  2830. } \
  2831. RETURN_GENERIC("packed check tag mismatch\n"); \
  2832. }
  2833. /* varint fields **************************************************************/
  2834. UPB_FORCEINLINE
  2835. static uint64_t fastdecode_munge(uint64_t val, int valbytes, bool zigzag) {
  2836. if (valbytes == 1) {
  2837. return val != 0;
  2838. } else if (zigzag) {
  2839. if (valbytes == 4) {
  2840. uint32_t n = val;
  2841. return (n >> 1) ^ -(int32_t)(n & 1);
  2842. } else if (valbytes == 8) {
  2843. return (val >> 1) ^ -(int64_t)(val & 1);
  2844. }
  2845. UPB_UNREACHABLE();
  2846. }
  2847. return val;
  2848. }
  2849. UPB_FORCEINLINE
  2850. static const char *fastdecode_varint64(const char *ptr, uint64_t *val) {
  2851. ptr++;
  2852. *val = (uint8_t)ptr[-1];
  2853. if (UPB_UNLIKELY(*val & 0x80)) {
  2854. int i;
  2855. for (i = 0; i < 8; i++) {
  2856. ptr++;
  2857. uint64_t byte = (uint8_t)ptr[-1];
  2858. *val += (byte - 1) << (7 + 7 * i);
  2859. if (UPB_LIKELY((byte & 0x80) == 0)) goto done;
  2860. }
  2861. ptr++;
  2862. uint64_t byte = (uint8_t)ptr[-1];
  2863. if (byte > 1) {
  2864. return NULL;
  2865. }
  2866. *val += (byte - 1) << 63;
  2867. }
  2868. done:
  2869. UPB_ASSUME(ptr != NULL);
  2870. return ptr;
  2871. }
  2872. #define FASTDECODE_UNPACKEDVARINT(d, ptr, msg, table, hasbits, data, tagbytes, \
  2873. valbytes, card, zigzag, packed) \
  2874. uint64_t val; \
  2875. void *dst; \
  2876. fastdecode_arr farr; \
  2877. \
  2878. FASTDECODE_CHECKPACKED(tagbytes, card, packed); \
  2879. \
  2880. dst = fastdecode_getfield(d, ptr, msg, &data, &hasbits, &farr, valbytes, \
  2881. card); \
  2882. if (card == CARD_r) { \
  2883. if (UPB_UNLIKELY(!dst)) { \
  2884. RETURN_GENERIC("need array resize\n"); \
  2885. } \
  2886. } \
  2887. \
  2888. again: \
  2889. if (card == CARD_r) { \
  2890. dst = fastdecode_resizearr(d, dst, &farr, valbytes); \
  2891. } \
  2892. \
  2893. ptr += tagbytes; \
  2894. ptr = fastdecode_varint64(ptr, &val); \
  2895. if (ptr == NULL) \
  2896. return fastdecode_err(d); \
  2897. val = fastdecode_munge(val, valbytes, zigzag); \
  2898. memcpy(dst, &val, valbytes); \
  2899. \
  2900. if (card == CARD_r) { \
  2901. fastdecode_nextret ret = fastdecode_nextrepeated( \
  2902. d, dst, &ptr, &farr, data, tagbytes, valbytes); \
  2903. switch (ret.next) { \
  2904. case FD_NEXT_SAMEFIELD: \
  2905. dst = ret.dst; \
  2906. goto again; \
  2907. case FD_NEXT_OTHERFIELD: \
  2908. data = ret.tag; \
  2909. UPB_MUSTTAIL return fastdecode_tagdispatch(UPB_PARSE_ARGS); \
  2910. case FD_NEXT_ATLIMIT: \
  2911. return ptr; \
  2912. } \
  2913. } \
  2914. \
  2915. UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS);
  2916. typedef struct {
  2917. uint8_t valbytes;
  2918. bool zigzag;
  2919. void *dst;
  2920. fastdecode_arr farr;
  2921. } fastdecode_varintdata;
  2922. UPB_FORCEINLINE
  2923. static const char *fastdecode_topackedvarint(upb_decstate *d, const char *ptr,
  2924. void *ctx) {
  2925. fastdecode_varintdata *data = ctx;
  2926. void *dst = data->dst;
  2927. uint64_t val;
  2928. while (!decode_isdone(d, &ptr)) {
  2929. dst = fastdecode_resizearr(d, dst, &data->farr, data->valbytes);
  2930. ptr = fastdecode_varint64(ptr, &val);
  2931. if (ptr == NULL) return NULL;
  2932. val = fastdecode_munge(val, data->valbytes, data->zigzag);
  2933. memcpy(dst, &val, data->valbytes);
  2934. dst = (char *)dst + data->valbytes;
  2935. }
  2936. fastdecode_commitarr(dst, &data->farr, data->valbytes);
  2937. return ptr;
  2938. }
  2939. #define FASTDECODE_PACKEDVARINT(d, ptr, msg, table, hasbits, data, tagbytes, \
  2940. valbytes, zigzag, unpacked) \
  2941. fastdecode_varintdata ctx = {valbytes, zigzag}; \
  2942. \
  2943. FASTDECODE_CHECKPACKED(tagbytes, CARD_r, unpacked); \
  2944. \
  2945. ctx.dst = fastdecode_getfield(d, ptr, msg, &data, &hasbits, &ctx.farr, \
  2946. valbytes, CARD_r); \
  2947. if (UPB_UNLIKELY(!ctx.dst)) { \
  2948. RETURN_GENERIC("need array resize\n"); \
  2949. } \
  2950. \
  2951. ptr += tagbytes; \
  2952. ptr = fastdecode_delimited(d, ptr, &fastdecode_topackedvarint, &ctx); \
  2953. \
  2954. if (UPB_UNLIKELY(ptr == NULL)) { \
  2955. return fastdecode_err(d); \
  2956. } \
  2957. \
  2958. UPB_MUSTTAIL return fastdecode_dispatch(d, ptr, msg, table, hasbits, 0);
  2959. #define FASTDECODE_VARINT(d, ptr, msg, table, hasbits, data, tagbytes, \
  2960. valbytes, card, zigzag, unpacked, packed) \
  2961. if (card == CARD_p) { \
  2962. FASTDECODE_PACKEDVARINT(d, ptr, msg, table, hasbits, data, tagbytes, \
  2963. valbytes, zigzag, unpacked); \
  2964. } else { \
  2965. FASTDECODE_UNPACKEDVARINT(d, ptr, msg, table, hasbits, data, tagbytes, \
  2966. valbytes, card, zigzag, packed); \
  2967. }
  2968. #define z_ZZ true
  2969. #define b_ZZ false
  2970. #define v_ZZ false
  2971. /* Generate all combinations:
  2972. * {s,o,r,p} x {b1,v4,z4,v8,z8} x {1bt,2bt} */
  2973. #define F(card, type, valbytes, tagbytes) \
  2974. UPB_NOINLINE \
  2975. const char *upb_p##card##type##valbytes##_##tagbytes##bt(UPB_PARSE_PARAMS) { \
  2976. FASTDECODE_VARINT(d, ptr, msg, table, hasbits, data, tagbytes, valbytes, \
  2977. CARD_##card, type##_ZZ, \
  2978. upb_pr##type##valbytes##_##tagbytes##bt, \
  2979. upb_pp##type##valbytes##_##tagbytes##bt); \
  2980. }
  2981. #define TYPES(card, tagbytes) \
  2982. F(card, b, 1, tagbytes) \
  2983. F(card, v, 4, tagbytes) \
  2984. F(card, v, 8, tagbytes) \
  2985. F(card, z, 4, tagbytes) \
  2986. F(card, z, 8, tagbytes)
  2987. #define TAGBYTES(card) \
  2988. TYPES(card, 1) \
  2989. TYPES(card, 2)
  2990. TAGBYTES(s)
  2991. TAGBYTES(o)
  2992. TAGBYTES(r)
  2993. TAGBYTES(p)
  2994. #undef z_ZZ
  2995. #undef b_ZZ
  2996. #undef v_ZZ
  2997. #undef o_ONEOF
  2998. #undef s_ONEOF
  2999. #undef r_ONEOF
  3000. #undef F
  3001. #undef TYPES
  3002. #undef TAGBYTES
  3003. #undef FASTDECODE_UNPACKEDVARINT
  3004. #undef FASTDECODE_PACKEDVARINT
  3005. #undef FASTDECODE_VARINT
  3006. /* fixed fields ***************************************************************/
  3007. #define FASTDECODE_UNPACKEDFIXED(d, ptr, msg, table, hasbits, data, tagbytes, \
  3008. valbytes, card, packed) \
  3009. void *dst; \
  3010. fastdecode_arr farr; \
  3011. \
  3012. FASTDECODE_CHECKPACKED(tagbytes, card, packed) \
  3013. \
  3014. dst = fastdecode_getfield(d, ptr, msg, &data, &hasbits, &farr, valbytes, \
  3015. card); \
  3016. if (card == CARD_r) { \
  3017. if (UPB_UNLIKELY(!dst)) { \
  3018. RETURN_GENERIC("couldn't allocate array in arena\n"); \
  3019. } \
  3020. } \
  3021. \
  3022. again: \
  3023. if (card == CARD_r) { \
  3024. dst = fastdecode_resizearr(d, dst, &farr, valbytes); \
  3025. } \
  3026. \
  3027. ptr += tagbytes; \
  3028. memcpy(dst, ptr, valbytes); \
  3029. ptr += valbytes; \
  3030. \
  3031. if (card == CARD_r) { \
  3032. fastdecode_nextret ret = fastdecode_nextrepeated( \
  3033. d, dst, &ptr, &farr, data, tagbytes, valbytes); \
  3034. switch (ret.next) { \
  3035. case FD_NEXT_SAMEFIELD: \
  3036. dst = ret.dst; \
  3037. goto again; \
  3038. case FD_NEXT_OTHERFIELD: \
  3039. data = ret.tag; \
  3040. UPB_MUSTTAIL return fastdecode_tagdispatch(UPB_PARSE_ARGS); \
  3041. case FD_NEXT_ATLIMIT: \
  3042. return ptr; \
  3043. } \
  3044. } \
  3045. \
  3046. UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS);
  3047. #define FASTDECODE_PACKEDFIXED(d, ptr, msg, table, hasbits, data, tagbytes, \
  3048. valbytes, unpacked) \
  3049. FASTDECODE_CHECKPACKED(tagbytes, CARD_r, unpacked) \
  3050. \
  3051. ptr += tagbytes; \
  3052. int size = (uint8_t)ptr[0]; \
  3053. ptr++; \
  3054. if (size & 0x80) { \
  3055. ptr = fastdecode_longsize(ptr, &size); \
  3056. } \
  3057. \
  3058. if (UPB_UNLIKELY(fastdecode_boundscheck(ptr, size, d->limit_ptr) || \
  3059. (size % valbytes) != 0)) { \
  3060. return fastdecode_err(d); \
  3061. } \
  3062. \
  3063. upb_array **arr_p = fastdecode_fieldmem(msg, data); \
  3064. upb_array *arr = *arr_p; \
  3065. uint8_t elem_size_lg2 = __builtin_ctz(valbytes); \
  3066. int elems = size / valbytes; \
  3067. \
  3068. if (UPB_LIKELY(!arr)) { \
  3069. *arr_p = arr = _upb_array_new(&d->arena, elems, elem_size_lg2); \
  3070. if (!arr) { \
  3071. return fastdecode_err(d); \
  3072. } \
  3073. } else { \
  3074. _upb_array_resize(arr, elems, &d->arena); \
  3075. } \
  3076. \
  3077. char *dst = _upb_array_ptr(arr); \
  3078. memcpy(dst, ptr, size); \
  3079. arr->len = elems; \
  3080. \
  3081. ptr += size; \
  3082. UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS);
  3083. #define FASTDECODE_FIXED(d, ptr, msg, table, hasbits, data, tagbytes, \
  3084. valbytes, card, unpacked, packed) \
  3085. if (card == CARD_p) { \
  3086. FASTDECODE_PACKEDFIXED(d, ptr, msg, table, hasbits, data, tagbytes, \
  3087. valbytes, unpacked); \
  3088. } else { \
  3089. FASTDECODE_UNPACKEDFIXED(d, ptr, msg, table, hasbits, data, tagbytes, \
  3090. valbytes, card, packed); \
  3091. }
  3092. /* Generate all combinations:
  3093. * {s,o,r,p} x {f4,f8} x {1bt,2bt} */
  3094. #define F(card, valbytes, tagbytes) \
  3095. UPB_NOINLINE \
  3096. const char *upb_p##card##f##valbytes##_##tagbytes##bt(UPB_PARSE_PARAMS) { \
  3097. FASTDECODE_FIXED(d, ptr, msg, table, hasbits, data, tagbytes, valbytes, \
  3098. CARD_##card, upb_ppf##valbytes##_##tagbytes##bt, \
  3099. upb_prf##valbytes##_##tagbytes##bt); \
  3100. }
  3101. #define TYPES(card, tagbytes) \
  3102. F(card, 4, tagbytes) \
  3103. F(card, 8, tagbytes)
  3104. #define TAGBYTES(card) \
  3105. TYPES(card, 1) \
  3106. TYPES(card, 2)
  3107. TAGBYTES(s)
  3108. TAGBYTES(o)
  3109. TAGBYTES(r)
  3110. TAGBYTES(p)
  3111. #undef F
  3112. #undef TYPES
  3113. #undef TAGBYTES
  3114. #undef FASTDECODE_UNPACKEDFIXED
  3115. #undef FASTDECODE_PACKEDFIXED
  3116. /* string fields **************************************************************/
  3117. typedef const char *fastdecode_copystr_func(struct upb_decstate *d,
  3118. const char *ptr, upb_msg *msg,
  3119. const upb_msglayout *table,
  3120. uint64_t hasbits, upb_strview *dst);
  3121. UPB_NOINLINE
  3122. static const char *fastdecode_verifyutf8(upb_decstate *d, const char *ptr,
  3123. upb_msg *msg, intptr_t table,
  3124. uint64_t hasbits, uint64_t data) {
  3125. upb_strview *dst = (upb_strview*)data;
  3126. if (!decode_verifyutf8_inl(dst->data, dst->size)) {
  3127. return fastdecode_err(d);
  3128. }
  3129. UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS);
  3130. }
  3131. #define FASTDECODE_LONGSTRING(d, ptr, msg, table, hasbits, dst, validate_utf8) \
  3132. int size = (uint8_t)ptr[0]; /* Could plumb through hasbits. */ \
  3133. ptr++; \
  3134. if (size & 0x80) { \
  3135. ptr = fastdecode_longsize(ptr, &size); \
  3136. } \
  3137. \
  3138. if (UPB_UNLIKELY(fastdecode_boundscheck(ptr, size, d->limit_ptr))) { \
  3139. dst->size = 0; \
  3140. return fastdecode_err(d); \
  3141. } \
  3142. \
  3143. if (d->alias) { \
  3144. dst->data = ptr; \
  3145. dst->size = size; \
  3146. } else { \
  3147. char *data = upb_arena_malloc(&d->arena, size); \
  3148. if (!data) { \
  3149. return fastdecode_err(d); \
  3150. } \
  3151. memcpy(data, ptr, size); \
  3152. dst->data = data; \
  3153. dst->size = size; \
  3154. } \
  3155. \
  3156. ptr += size; \
  3157. if (validate_utf8) { \
  3158. data = (uint64_t)dst; \
  3159. UPB_MUSTTAIL return fastdecode_verifyutf8(UPB_PARSE_ARGS); \
  3160. } else { \
  3161. UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS); \
  3162. }
  3163. UPB_NOINLINE
  3164. static const char *fastdecode_longstring_utf8(struct upb_decstate *d,
  3165. const char *ptr, upb_msg *msg,
  3166. intptr_t table, uint64_t hasbits,
  3167. uint64_t data) {
  3168. upb_strview *dst = (upb_strview*)data;
  3169. FASTDECODE_LONGSTRING(d, ptr, msg, table, hasbits, dst, true);
  3170. }
  3171. UPB_NOINLINE
  3172. static const char *fastdecode_longstring_noutf8(struct upb_decstate *d,
  3173. const char *ptr, upb_msg *msg,
  3174. intptr_t table,
  3175. uint64_t hasbits,
  3176. uint64_t data) {
  3177. upb_strview *dst = (upb_strview*)data;
  3178. FASTDECODE_LONGSTRING(d, ptr, msg, table, hasbits, dst, false);
  3179. }
  3180. UPB_FORCEINLINE
  3181. static void fastdecode_docopy(upb_decstate *d, const char *ptr, uint32_t size,
  3182. int copy, char *data, upb_strview *dst) {
  3183. d->arena.head.ptr += copy;
  3184. dst->data = data;
  3185. UPB_UNPOISON_MEMORY_REGION(data, copy);
  3186. memcpy(data, ptr, copy);
  3187. UPB_POISON_MEMORY_REGION(data + size, copy - size);
  3188. }
  3189. #define FASTDECODE_COPYSTRING(d, ptr, msg, table, hasbits, data, tagbytes, \
  3190. card, validate_utf8) \
  3191. upb_strview *dst; \
  3192. fastdecode_arr farr; \
  3193. int64_t size; \
  3194. size_t arena_has; \
  3195. size_t common_has; \
  3196. char *buf; \
  3197. \
  3198. UPB_ASSERT(!d->alias); \
  3199. UPB_ASSERT(fastdecode_checktag(data, tagbytes)); \
  3200. \
  3201. dst = fastdecode_getfield(d, ptr, msg, &data, &hasbits, &farr, \
  3202. sizeof(upb_strview), card); \
  3203. \
  3204. again: \
  3205. if (card == CARD_r) { \
  3206. dst = fastdecode_resizearr(d, dst, &farr, sizeof(upb_strview)); \
  3207. } \
  3208. \
  3209. size = (uint8_t)ptr[tagbytes]; \
  3210. ptr += tagbytes + 1; \
  3211. dst->size = size; \
  3212. \
  3213. buf = d->arena.head.ptr; \
  3214. arena_has = _upb_arenahas(&d->arena); \
  3215. common_has = UPB_MIN(arena_has, (d->end - ptr) + 16); \
  3216. \
  3217. if (UPB_LIKELY(size <= 15 - tagbytes)) { \
  3218. if (arena_has < 16) \
  3219. goto longstr; \
  3220. d->arena.head.ptr += 16; \
  3221. memcpy(buf, ptr - tagbytes - 1, 16); \
  3222. dst->data = buf + tagbytes + 1; \
  3223. } else if (UPB_LIKELY(size <= 32)) { \
  3224. if (UPB_UNLIKELY(common_has < 32)) \
  3225. goto longstr; \
  3226. fastdecode_docopy(d, ptr, size, 32, buf, dst); \
  3227. } else if (UPB_LIKELY(size <= 64)) { \
  3228. if (UPB_UNLIKELY(common_has < 64)) \
  3229. goto longstr; \
  3230. fastdecode_docopy(d, ptr, size, 64, buf, dst); \
  3231. } else if (UPB_LIKELY(size < 128)) { \
  3232. if (UPB_UNLIKELY(common_has < 128)) \
  3233. goto longstr; \
  3234. fastdecode_docopy(d, ptr, size, 128, buf, dst); \
  3235. } else { \
  3236. goto longstr; \
  3237. } \
  3238. \
  3239. ptr += size; \
  3240. \
  3241. if (card == CARD_r) { \
  3242. if (validate_utf8 && !decode_verifyutf8_inl(dst->data, dst->size)) { \
  3243. return fastdecode_err(d); \
  3244. } \
  3245. fastdecode_nextret ret = fastdecode_nextrepeated( \
  3246. d, dst, &ptr, &farr, data, tagbytes, sizeof(upb_strview)); \
  3247. switch (ret.next) { \
  3248. case FD_NEXT_SAMEFIELD: \
  3249. dst = ret.dst; \
  3250. goto again; \
  3251. case FD_NEXT_OTHERFIELD: \
  3252. data = ret.tag; \
  3253. UPB_MUSTTAIL return fastdecode_tagdispatch(UPB_PARSE_ARGS); \
  3254. case FD_NEXT_ATLIMIT: \
  3255. return ptr; \
  3256. } \
  3257. } \
  3258. \
  3259. if (card != CARD_r && validate_utf8) { \
  3260. data = (uint64_t)dst; \
  3261. UPB_MUSTTAIL return fastdecode_verifyutf8(UPB_PARSE_ARGS); \
  3262. } \
  3263. \
  3264. UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS); \
  3265. \
  3266. longstr: \
  3267. ptr--; \
  3268. if (validate_utf8) { \
  3269. UPB_MUSTTAIL return fastdecode_longstring_utf8(d, ptr, msg, table, \
  3270. hasbits, (uint64_t)dst); \
  3271. } else { \
  3272. UPB_MUSTTAIL return fastdecode_longstring_noutf8(d, ptr, msg, table, \
  3273. hasbits, (uint64_t)dst); \
  3274. }
  3275. #define FASTDECODE_STRING(d, ptr, msg, table, hasbits, data, tagbytes, card, \
  3276. copyfunc, validate_utf8) \
  3277. upb_strview *dst; \
  3278. fastdecode_arr farr; \
  3279. int64_t size; \
  3280. \
  3281. if (UPB_UNLIKELY(!fastdecode_checktag(data, tagbytes))) { \
  3282. RETURN_GENERIC("string field tag mismatch\n"); \
  3283. } \
  3284. \
  3285. if (UPB_UNLIKELY(!d->alias)) { \
  3286. UPB_MUSTTAIL return copyfunc(UPB_PARSE_ARGS); \
  3287. } \
  3288. \
  3289. dst = fastdecode_getfield(d, ptr, msg, &data, &hasbits, &farr, \
  3290. sizeof(upb_strview), card); \
  3291. \
  3292. again: \
  3293. if (card == CARD_r) { \
  3294. dst = fastdecode_resizearr(d, dst, &farr, sizeof(upb_strview)); \
  3295. } \
  3296. \
  3297. size = (int8_t)ptr[tagbytes]; \
  3298. ptr += tagbytes + 1; \
  3299. dst->data = ptr; \
  3300. dst->size = size; \
  3301. \
  3302. if (UPB_UNLIKELY(fastdecode_boundscheck(ptr, size, d->end))) { \
  3303. ptr--; \
  3304. if (validate_utf8) { \
  3305. return fastdecode_longstring_utf8(d, ptr, msg, table, hasbits, \
  3306. (uint64_t)dst); \
  3307. } else { \
  3308. return fastdecode_longstring_noutf8(d, ptr, msg, table, hasbits, \
  3309. (uint64_t)dst); \
  3310. } \
  3311. } \
  3312. \
  3313. ptr += size; \
  3314. \
  3315. if (card == CARD_r) { \
  3316. if (validate_utf8 && !decode_verifyutf8_inl(dst->data, dst->size)) { \
  3317. return fastdecode_err(d); \
  3318. } \
  3319. fastdecode_nextret ret = fastdecode_nextrepeated( \
  3320. d, dst, &ptr, &farr, data, tagbytes, sizeof(upb_strview)); \
  3321. switch (ret.next) { \
  3322. case FD_NEXT_SAMEFIELD: \
  3323. dst = ret.dst; \
  3324. if (UPB_UNLIKELY(!d->alias)) { \
  3325. /* Buffer flipped and we can't alias any more. Bounce to */ \
  3326. /* copyfunc(), but via dispatch since we need to reload table */ \
  3327. /* data also. */ \
  3328. fastdecode_commitarr(dst, &farr, sizeof(upb_strview)); \
  3329. data = ret.tag; \
  3330. UPB_MUSTTAIL return fastdecode_tagdispatch(UPB_PARSE_ARGS); \
  3331. } \
  3332. goto again; \
  3333. case FD_NEXT_OTHERFIELD: \
  3334. data = ret.tag; \
  3335. UPB_MUSTTAIL return fastdecode_tagdispatch(UPB_PARSE_ARGS); \
  3336. case FD_NEXT_ATLIMIT: \
  3337. return ptr; \
  3338. } \
  3339. } \
  3340. \
  3341. if (card != CARD_r && validate_utf8) { \
  3342. data = (uint64_t)dst; \
  3343. UPB_MUSTTAIL return fastdecode_verifyutf8(UPB_PARSE_ARGS); \
  3344. } \
  3345. \
  3346. UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS);
  3347. /* Generate all combinations:
  3348. * {p,c} x {s,o,r} x {s, b} x {1bt,2bt} */
  3349. #define s_VALIDATE true
  3350. #define b_VALIDATE false
  3351. #define F(card, tagbytes, type) \
  3352. UPB_NOINLINE \
  3353. const char *upb_c##card##type##_##tagbytes##bt(UPB_PARSE_PARAMS) { \
  3354. FASTDECODE_COPYSTRING(d, ptr, msg, table, hasbits, data, tagbytes, \
  3355. CARD_##card, type##_VALIDATE); \
  3356. } \
  3357. const char *upb_p##card##type##_##tagbytes##bt(UPB_PARSE_PARAMS) { \
  3358. FASTDECODE_STRING(d, ptr, msg, table, hasbits, data, tagbytes, \
  3359. CARD_##card, upb_c##card##type##_##tagbytes##bt, \
  3360. type##_VALIDATE); \
  3361. }
  3362. #define UTF8(card, tagbytes) \
  3363. F(card, tagbytes, s) \
  3364. F(card, tagbytes, b)
  3365. #define TAGBYTES(card) \
  3366. UTF8(card, 1) \
  3367. UTF8(card, 2)
  3368. TAGBYTES(s)
  3369. TAGBYTES(o)
  3370. TAGBYTES(r)
  3371. #undef s_VALIDATE
  3372. #undef b_VALIDATE
  3373. #undef F
  3374. #undef TAGBYTES
  3375. #undef FASTDECODE_LONGSTRING
  3376. #undef FASTDECODE_COPYSTRING
  3377. #undef FASTDECODE_STRING
  3378. /* message fields *************************************************************/
  3379. UPB_INLINE
  3380. upb_msg *decode_newmsg_ceil(upb_decstate *d, const upb_msglayout *l,
  3381. int msg_ceil_bytes) {
  3382. size_t size = l->size + sizeof(upb_msg_internal);
  3383. char *msg_data;
  3384. if (UPB_LIKELY(msg_ceil_bytes > 0 &&
  3385. _upb_arenahas(&d->arena) >= msg_ceil_bytes)) {
  3386. UPB_ASSERT(size <= (size_t)msg_ceil_bytes);
  3387. msg_data = d->arena.head.ptr;
  3388. d->arena.head.ptr += size;
  3389. UPB_UNPOISON_MEMORY_REGION(msg_data, msg_ceil_bytes);
  3390. memset(msg_data, 0, msg_ceil_bytes);
  3391. UPB_POISON_MEMORY_REGION(msg_data + size, msg_ceil_bytes - size);
  3392. } else {
  3393. msg_data = (char*)upb_arena_malloc(&d->arena, size);
  3394. memset(msg_data, 0, size);
  3395. }
  3396. return msg_data + sizeof(upb_msg_internal);
  3397. }
  3398. typedef struct {
  3399. intptr_t table;
  3400. upb_msg *msg;
  3401. } fastdecode_submsgdata;
  3402. UPB_FORCEINLINE
  3403. static const char *fastdecode_tosubmsg(upb_decstate *d, const char *ptr,
  3404. void *ctx) {
  3405. fastdecode_submsgdata *submsg = ctx;
  3406. ptr = fastdecode_dispatch(d, ptr, submsg->msg, submsg->table, 0, 0);
  3407. UPB_ASSUME(ptr != NULL);
  3408. return ptr;
  3409. }
  3410. #define FASTDECODE_SUBMSG(d, ptr, msg, table, hasbits, data, tagbytes, \
  3411. msg_ceil_bytes, card) \
  3412. \
  3413. if (UPB_UNLIKELY(!fastdecode_checktag(data, tagbytes))) { \
  3414. RETURN_GENERIC("submessage field tag mismatch\n"); \
  3415. } \
  3416. \
  3417. if (--d->depth == 0) return fastdecode_err(d); \
  3418. \
  3419. upb_msg **dst; \
  3420. uint32_t submsg_idx = (data >> 16) & 0xff; \
  3421. const upb_msglayout *tablep = decode_totablep(table); \
  3422. const upb_msglayout *subtablep = tablep->submsgs[submsg_idx]; \
  3423. fastdecode_submsgdata submsg = {decode_totable(subtablep)}; \
  3424. fastdecode_arr farr; \
  3425. \
  3426. if (subtablep->table_mask == (uint8_t)-1) { \
  3427. RETURN_GENERIC("submessage doesn't have fast tables."); \
  3428. } \
  3429. \
  3430. dst = fastdecode_getfield(d, ptr, msg, &data, &hasbits, &farr, \
  3431. sizeof(upb_msg *), card); \
  3432. \
  3433. if (card == CARD_s) { \
  3434. *(uint32_t *)msg |= hasbits; \
  3435. hasbits = 0; \
  3436. } \
  3437. \
  3438. again: \
  3439. if (card == CARD_r) { \
  3440. dst = fastdecode_resizearr(d, dst, &farr, sizeof(upb_msg *)); \
  3441. } \
  3442. \
  3443. submsg.msg = *dst; \
  3444. \
  3445. if (card == CARD_r || UPB_LIKELY(!submsg.msg)) { \
  3446. *dst = submsg.msg = decode_newmsg_ceil(d, subtablep, msg_ceil_bytes); \
  3447. } \
  3448. \
  3449. ptr += tagbytes; \
  3450. ptr = fastdecode_delimited(d, ptr, fastdecode_tosubmsg, &submsg); \
  3451. \
  3452. if (UPB_UNLIKELY(ptr == NULL || d->end_group != DECODE_NOGROUP)) { \
  3453. return fastdecode_err(d); \
  3454. } \
  3455. \
  3456. if (card == CARD_r) { \
  3457. fastdecode_nextret ret = fastdecode_nextrepeated( \
  3458. d, dst, &ptr, &farr, data, tagbytes, sizeof(upb_msg *)); \
  3459. switch (ret.next) { \
  3460. case FD_NEXT_SAMEFIELD: \
  3461. dst = ret.dst; \
  3462. goto again; \
  3463. case FD_NEXT_OTHERFIELD: \
  3464. d->depth++; \
  3465. data = ret.tag; \
  3466. UPB_MUSTTAIL return fastdecode_tagdispatch(UPB_PARSE_ARGS); \
  3467. case FD_NEXT_ATLIMIT: \
  3468. d->depth++; \
  3469. return ptr; \
  3470. } \
  3471. } \
  3472. \
  3473. d->depth++; \
  3474. UPB_MUSTTAIL return fastdecode_dispatch(UPB_PARSE_ARGS);
  3475. #define F(card, tagbytes, size_ceil, ceil_arg) \
  3476. const char *upb_p##card##m_##tagbytes##bt_max##size_ceil##b( \
  3477. UPB_PARSE_PARAMS) { \
  3478. FASTDECODE_SUBMSG(d, ptr, msg, table, hasbits, data, tagbytes, ceil_arg, \
  3479. CARD_##card); \
  3480. }
  3481. #define SIZES(card, tagbytes) \
  3482. F(card, tagbytes, 64, 64) \
  3483. F(card, tagbytes, 128, 128) \
  3484. F(card, tagbytes, 192, 192) \
  3485. F(card, tagbytes, 256, 256) \
  3486. F(card, tagbytes, max, -1)
  3487. #define TAGBYTES(card) \
  3488. SIZES(card, 1) \
  3489. SIZES(card, 2)
  3490. TAGBYTES(s)
  3491. TAGBYTES(o)
  3492. TAGBYTES(r)
  3493. #undef TAGBYTES
  3494. #undef SIZES
  3495. #undef F
  3496. #undef FASTDECODE_SUBMSG
  3497. #endif /* UPB_FASTTABLE */
  3498. /** bazel-out/k8-fastbuild/bin/external/com_google_protobuf/google/protobuf/descriptor.upb.c ************************************************************//* This file was generated by upbc (the upb compiler) from the input
  3499. * file:
  3500. *
  3501. * google/protobuf/descriptor.proto
  3502. *
  3503. * Do not edit -- your changes will be discarded when the file is
  3504. * regenerated. */
  3505. #include <stddef.h>
  3506. static const upb_msglayout *const google_protobuf_FileDescriptorSet_submsgs[1] = {
  3507. &google_protobuf_FileDescriptorProto_msginit,
  3508. };
  3509. static const upb_msglayout_field google_protobuf_FileDescriptorSet__fields[1] = {
  3510. {1, UPB_SIZE(0, 0), 0, 0, 11, _UPB_MODE_ARRAY},
  3511. };
  3512. const upb_msglayout google_protobuf_FileDescriptorSet_msginit = {
  3513. &google_protobuf_FileDescriptorSet_submsgs[0],
  3514. &google_protobuf_FileDescriptorSet__fields[0],
  3515. UPB_SIZE(8, 8), 1, false, 1, 255,
  3516. };
  3517. static const upb_msglayout *const google_protobuf_FileDescriptorProto_submsgs[6] = {
  3518. &google_protobuf_DescriptorProto_msginit,
  3519. &google_protobuf_EnumDescriptorProto_msginit,
  3520. &google_protobuf_FieldDescriptorProto_msginit,
  3521. &google_protobuf_FileOptions_msginit,
  3522. &google_protobuf_ServiceDescriptorProto_msginit,
  3523. &google_protobuf_SourceCodeInfo_msginit,
  3524. };
  3525. static const upb_msglayout_field google_protobuf_FileDescriptorProto__fields[12] = {
  3526. {1, UPB_SIZE(4, 8), 1, 0, 12, _UPB_MODE_SCALAR},
  3527. {2, UPB_SIZE(12, 24), 2, 0, 12, _UPB_MODE_SCALAR},
  3528. {3, UPB_SIZE(36, 72), 0, 0, 12, _UPB_MODE_ARRAY},
  3529. {4, UPB_SIZE(40, 80), 0, 0, 11, _UPB_MODE_ARRAY},
  3530. {5, UPB_SIZE(44, 88), 0, 1, 11, _UPB_MODE_ARRAY},
  3531. {6, UPB_SIZE(48, 96), 0, 4, 11, _UPB_MODE_ARRAY},
  3532. {7, UPB_SIZE(52, 104), 0, 2, 11, _UPB_MODE_ARRAY},
  3533. {8, UPB_SIZE(28, 56), 3, 3, 11, _UPB_MODE_SCALAR},
  3534. {9, UPB_SIZE(32, 64), 4, 5, 11, _UPB_MODE_SCALAR},
  3535. {10, UPB_SIZE(56, 112), 0, 0, 5, _UPB_MODE_ARRAY},
  3536. {11, UPB_SIZE(60, 120), 0, 0, 5, _UPB_MODE_ARRAY},
  3537. {12, UPB_SIZE(20, 40), 5, 0, 12, _UPB_MODE_SCALAR},
  3538. };
  3539. const upb_msglayout google_protobuf_FileDescriptorProto_msginit = {
  3540. &google_protobuf_FileDescriptorProto_submsgs[0],
  3541. &google_protobuf_FileDescriptorProto__fields[0],
  3542. UPB_SIZE(64, 128), 12, false, 12, 255,
  3543. };
  3544. static const upb_msglayout *const google_protobuf_DescriptorProto_submsgs[7] = {
  3545. &google_protobuf_DescriptorProto_msginit,
  3546. &google_protobuf_DescriptorProto_ExtensionRange_msginit,
  3547. &google_protobuf_DescriptorProto_ReservedRange_msginit,
  3548. &google_protobuf_EnumDescriptorProto_msginit,
  3549. &google_protobuf_FieldDescriptorProto_msginit,
  3550. &google_protobuf_MessageOptions_msginit,
  3551. &google_protobuf_OneofDescriptorProto_msginit,
  3552. };
  3553. static const upb_msglayout_field google_protobuf_DescriptorProto__fields[10] = {
  3554. {1, UPB_SIZE(4, 8), 1, 0, 12, _UPB_MODE_SCALAR},
  3555. {2, UPB_SIZE(16, 32), 0, 4, 11, _UPB_MODE_ARRAY},
  3556. {3, UPB_SIZE(20, 40), 0, 0, 11, _UPB_MODE_ARRAY},
  3557. {4, UPB_SIZE(24, 48), 0, 3, 11, _UPB_MODE_ARRAY},
  3558. {5, UPB_SIZE(28, 56), 0, 1, 11, _UPB_MODE_ARRAY},
  3559. {6, UPB_SIZE(32, 64), 0, 4, 11, _UPB_MODE_ARRAY},
  3560. {7, UPB_SIZE(12, 24), 2, 5, 11, _UPB_MODE_SCALAR},
  3561. {8, UPB_SIZE(36, 72), 0, 6, 11, _UPB_MODE_ARRAY},
  3562. {9, UPB_SIZE(40, 80), 0, 2, 11, _UPB_MODE_ARRAY},
  3563. {10, UPB_SIZE(44, 88), 0, 0, 12, _UPB_MODE_ARRAY},
  3564. };
  3565. const upb_msglayout google_protobuf_DescriptorProto_msginit = {
  3566. &google_protobuf_DescriptorProto_submsgs[0],
  3567. &google_protobuf_DescriptorProto__fields[0],
  3568. UPB_SIZE(48, 96), 10, false, 10, 255,
  3569. };
  3570. static const upb_msglayout *const google_protobuf_DescriptorProto_ExtensionRange_submsgs[1] = {
  3571. &google_protobuf_ExtensionRangeOptions_msginit,
  3572. };
  3573. static const upb_msglayout_field google_protobuf_DescriptorProto_ExtensionRange__fields[3] = {
  3574. {1, UPB_SIZE(4, 4), 1, 0, 5, _UPB_MODE_SCALAR},
  3575. {2, UPB_SIZE(8, 8), 2, 0, 5, _UPB_MODE_SCALAR},
  3576. {3, UPB_SIZE(12, 16), 3, 0, 11, _UPB_MODE_SCALAR},
  3577. };
  3578. const upb_msglayout google_protobuf_DescriptorProto_ExtensionRange_msginit = {
  3579. &google_protobuf_DescriptorProto_ExtensionRange_submsgs[0],
  3580. &google_protobuf_DescriptorProto_ExtensionRange__fields[0],
  3581. UPB_SIZE(16, 24), 3, false, 3, 255,
  3582. };
  3583. static const upb_msglayout_field google_protobuf_DescriptorProto_ReservedRange__fields[2] = {
  3584. {1, UPB_SIZE(4, 4), 1, 0, 5, _UPB_MODE_SCALAR},
  3585. {2, UPB_SIZE(8, 8), 2, 0, 5, _UPB_MODE_SCALAR},
  3586. };
  3587. const upb_msglayout google_protobuf_DescriptorProto_ReservedRange_msginit = {
  3588. NULL,
  3589. &google_protobuf_DescriptorProto_ReservedRange__fields[0],
  3590. UPB_SIZE(16, 16), 2, false, 2, 255,
  3591. };
  3592. static const upb_msglayout *const google_protobuf_ExtensionRangeOptions_submsgs[1] = {
  3593. &google_protobuf_UninterpretedOption_msginit,
  3594. };
  3595. static const upb_msglayout_field google_protobuf_ExtensionRangeOptions__fields[1] = {
  3596. {999, UPB_SIZE(0, 0), 0, 0, 11, _UPB_MODE_ARRAY},
  3597. };
  3598. const upb_msglayout google_protobuf_ExtensionRangeOptions_msginit = {
  3599. &google_protobuf_ExtensionRangeOptions_submsgs[0],
  3600. &google_protobuf_ExtensionRangeOptions__fields[0],
  3601. UPB_SIZE(8, 8), 1, false, 0, 255,
  3602. };
  3603. static const upb_msglayout *const google_protobuf_FieldDescriptorProto_submsgs[1] = {
  3604. &google_protobuf_FieldOptions_msginit,
  3605. };
  3606. static const upb_msglayout_field google_protobuf_FieldDescriptorProto__fields[11] = {
  3607. {1, UPB_SIZE(24, 24), 1, 0, 12, _UPB_MODE_SCALAR},
  3608. {2, UPB_SIZE(32, 40), 2, 0, 12, _UPB_MODE_SCALAR},
  3609. {3, UPB_SIZE(12, 12), 3, 0, 5, _UPB_MODE_SCALAR},
  3610. {4, UPB_SIZE(4, 4), 4, 0, 14, _UPB_MODE_SCALAR},
  3611. {5, UPB_SIZE(8, 8), 5, 0, 14, _UPB_MODE_SCALAR},
  3612. {6, UPB_SIZE(40, 56), 6, 0, 12, _UPB_MODE_SCALAR},
  3613. {7, UPB_SIZE(48, 72), 7, 0, 12, _UPB_MODE_SCALAR},
  3614. {8, UPB_SIZE(64, 104), 8, 0, 11, _UPB_MODE_SCALAR},
  3615. {9, UPB_SIZE(16, 16), 9, 0, 5, _UPB_MODE_SCALAR},
  3616. {10, UPB_SIZE(56, 88), 10, 0, 12, _UPB_MODE_SCALAR},
  3617. {17, UPB_SIZE(20, 20), 11, 0, 8, _UPB_MODE_SCALAR},
  3618. };
  3619. const upb_msglayout google_protobuf_FieldDescriptorProto_msginit = {
  3620. &google_protobuf_FieldDescriptorProto_submsgs[0],
  3621. &google_protobuf_FieldDescriptorProto__fields[0],
  3622. UPB_SIZE(72, 112), 11, false, 10, 255,
  3623. };
  3624. static const upb_msglayout *const google_protobuf_OneofDescriptorProto_submsgs[1] = {
  3625. &google_protobuf_OneofOptions_msginit,
  3626. };
  3627. static const upb_msglayout_field google_protobuf_OneofDescriptorProto__fields[2] = {
  3628. {1, UPB_SIZE(4, 8), 1, 0, 12, _UPB_MODE_SCALAR},
  3629. {2, UPB_SIZE(12, 24), 2, 0, 11, _UPB_MODE_SCALAR},
  3630. };
  3631. const upb_msglayout google_protobuf_OneofDescriptorProto_msginit = {
  3632. &google_protobuf_OneofDescriptorProto_submsgs[0],
  3633. &google_protobuf_OneofDescriptorProto__fields[0],
  3634. UPB_SIZE(16, 32), 2, false, 2, 255,
  3635. };
  3636. static const upb_msglayout *const google_protobuf_EnumDescriptorProto_submsgs[3] = {
  3637. &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit,
  3638. &google_protobuf_EnumOptions_msginit,
  3639. &google_protobuf_EnumValueDescriptorProto_msginit,
  3640. };
  3641. static const upb_msglayout_field google_protobuf_EnumDescriptorProto__fields[5] = {
  3642. {1, UPB_SIZE(4, 8), 1, 0, 12, _UPB_MODE_SCALAR},
  3643. {2, UPB_SIZE(16, 32), 0, 2, 11, _UPB_MODE_ARRAY},
  3644. {3, UPB_SIZE(12, 24), 2, 1, 11, _UPB_MODE_SCALAR},
  3645. {4, UPB_SIZE(20, 40), 0, 0, 11, _UPB_MODE_ARRAY},
  3646. {5, UPB_SIZE(24, 48), 0, 0, 12, _UPB_MODE_ARRAY},
  3647. };
  3648. const upb_msglayout google_protobuf_EnumDescriptorProto_msginit = {
  3649. &google_protobuf_EnumDescriptorProto_submsgs[0],
  3650. &google_protobuf_EnumDescriptorProto__fields[0],
  3651. UPB_SIZE(32, 64), 5, false, 5, 255,
  3652. };
  3653. static const upb_msglayout_field google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[2] = {
  3654. {1, UPB_SIZE(4, 4), 1, 0, 5, _UPB_MODE_SCALAR},
  3655. {2, UPB_SIZE(8, 8), 2, 0, 5, _UPB_MODE_SCALAR},
  3656. };
  3657. const upb_msglayout google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit = {
  3658. NULL,
  3659. &google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[0],
  3660. UPB_SIZE(16, 16), 2, false, 2, 255,
  3661. };
  3662. static const upb_msglayout *const google_protobuf_EnumValueDescriptorProto_submsgs[1] = {
  3663. &google_protobuf_EnumValueOptions_msginit,
  3664. };
  3665. static const upb_msglayout_field google_protobuf_EnumValueDescriptorProto__fields[3] = {
  3666. {1, UPB_SIZE(8, 8), 1, 0, 12, _UPB_MODE_SCALAR},
  3667. {2, UPB_SIZE(4, 4), 2, 0, 5, _UPB_MODE_SCALAR},
  3668. {3, UPB_SIZE(16, 24), 3, 0, 11, _UPB_MODE_SCALAR},
  3669. };
  3670. const upb_msglayout google_protobuf_EnumValueDescriptorProto_msginit = {
  3671. &google_protobuf_EnumValueDescriptorProto_submsgs[0],
  3672. &google_protobuf_EnumValueDescriptorProto__fields[0],
  3673. UPB_SIZE(24, 32), 3, false, 3, 255,
  3674. };
  3675. static const upb_msglayout *const google_protobuf_ServiceDescriptorProto_submsgs[2] = {
  3676. &google_protobuf_MethodDescriptorProto_msginit,
  3677. &google_protobuf_ServiceOptions_msginit,
  3678. };
  3679. static const upb_msglayout_field google_protobuf_ServiceDescriptorProto__fields[3] = {
  3680. {1, UPB_SIZE(4, 8), 1, 0, 12, _UPB_MODE_SCALAR},
  3681. {2, UPB_SIZE(16, 32), 0, 0, 11, _UPB_MODE_ARRAY},
  3682. {3, UPB_SIZE(12, 24), 2, 1, 11, _UPB_MODE_SCALAR},
  3683. };
  3684. const upb_msglayout google_protobuf_ServiceDescriptorProto_msginit = {
  3685. &google_protobuf_ServiceDescriptorProto_submsgs[0],
  3686. &google_protobuf_ServiceDescriptorProto__fields[0],
  3687. UPB_SIZE(24, 48), 3, false, 3, 255,
  3688. };
  3689. static const upb_msglayout *const google_protobuf_MethodDescriptorProto_submsgs[1] = {
  3690. &google_protobuf_MethodOptions_msginit,
  3691. };
  3692. static const upb_msglayout_field google_protobuf_MethodDescriptorProto__fields[6] = {
  3693. {1, UPB_SIZE(4, 8), 1, 0, 12, _UPB_MODE_SCALAR},
  3694. {2, UPB_SIZE(12, 24), 2, 0, 12, _UPB_MODE_SCALAR},
  3695. {3, UPB_SIZE(20, 40), 3, 0, 12, _UPB_MODE_SCALAR},
  3696. {4, UPB_SIZE(28, 56), 4, 0, 11, _UPB_MODE_SCALAR},
  3697. {5, UPB_SIZE(1, 1), 5, 0, 8, _UPB_MODE_SCALAR},
  3698. {6, UPB_SIZE(2, 2), 6, 0, 8, _UPB_MODE_SCALAR},
  3699. };
  3700. const upb_msglayout google_protobuf_MethodDescriptorProto_msginit = {
  3701. &google_protobuf_MethodDescriptorProto_submsgs[0],
  3702. &google_protobuf_MethodDescriptorProto__fields[0],
  3703. UPB_SIZE(32, 64), 6, false, 6, 255,
  3704. };
  3705. static const upb_msglayout *const google_protobuf_FileOptions_submsgs[1] = {
  3706. &google_protobuf_UninterpretedOption_msginit,
  3707. };
  3708. static const upb_msglayout_field google_protobuf_FileOptions__fields[21] = {
  3709. {1, UPB_SIZE(20, 24), 1, 0, 12, _UPB_MODE_SCALAR},
  3710. {8, UPB_SIZE(28, 40), 2, 0, 12, _UPB_MODE_SCALAR},
  3711. {9, UPB_SIZE(4, 4), 3, 0, 14, _UPB_MODE_SCALAR},
  3712. {10, UPB_SIZE(8, 8), 4, 0, 8, _UPB_MODE_SCALAR},
  3713. {11, UPB_SIZE(36, 56), 5, 0, 12, _UPB_MODE_SCALAR},
  3714. {16, UPB_SIZE(9, 9), 6, 0, 8, _UPB_MODE_SCALAR},
  3715. {17, UPB_SIZE(10, 10), 7, 0, 8, _UPB_MODE_SCALAR},
  3716. {18, UPB_SIZE(11, 11), 8, 0, 8, _UPB_MODE_SCALAR},
  3717. {20, UPB_SIZE(12, 12), 9, 0, 8, _UPB_MODE_SCALAR},
  3718. {23, UPB_SIZE(13, 13), 10, 0, 8, _UPB_MODE_SCALAR},
  3719. {27, UPB_SIZE(14, 14), 11, 0, 8, _UPB_MODE_SCALAR},
  3720. {31, UPB_SIZE(15, 15), 12, 0, 8, _UPB_MODE_SCALAR},
  3721. {36, UPB_SIZE(44, 72), 13, 0, 12, _UPB_MODE_SCALAR},
  3722. {37, UPB_SIZE(52, 88), 14, 0, 12, _UPB_MODE_SCALAR},
  3723. {39, UPB_SIZE(60, 104), 15, 0, 12, _UPB_MODE_SCALAR},
  3724. {40, UPB_SIZE(68, 120), 16, 0, 12, _UPB_MODE_SCALAR},
  3725. {41, UPB_SIZE(76, 136), 17, 0, 12, _UPB_MODE_SCALAR},
  3726. {42, UPB_SIZE(16, 16), 18, 0, 8, _UPB_MODE_SCALAR},
  3727. {44, UPB_SIZE(84, 152), 19, 0, 12, _UPB_MODE_SCALAR},
  3728. {45, UPB_SIZE(92, 168), 20, 0, 12, _UPB_MODE_SCALAR},
  3729. {999, UPB_SIZE(100, 184), 0, 0, 11, _UPB_MODE_ARRAY},
  3730. };
  3731. const upb_msglayout google_protobuf_FileOptions_msginit = {
  3732. &google_protobuf_FileOptions_submsgs[0],
  3733. &google_protobuf_FileOptions__fields[0],
  3734. UPB_SIZE(104, 192), 21, false, 1, 255,
  3735. };
  3736. static const upb_msglayout *const google_protobuf_MessageOptions_submsgs[1] = {
  3737. &google_protobuf_UninterpretedOption_msginit,
  3738. };
  3739. static const upb_msglayout_field google_protobuf_MessageOptions__fields[5] = {
  3740. {1, UPB_SIZE(1, 1), 1, 0, 8, _UPB_MODE_SCALAR},
  3741. {2, UPB_SIZE(2, 2), 2, 0, 8, _UPB_MODE_SCALAR},
  3742. {3, UPB_SIZE(3, 3), 3, 0, 8, _UPB_MODE_SCALAR},
  3743. {7, UPB_SIZE(4, 4), 4, 0, 8, _UPB_MODE_SCALAR},
  3744. {999, UPB_SIZE(8, 8), 0, 0, 11, _UPB_MODE_ARRAY},
  3745. };
  3746. const upb_msglayout google_protobuf_MessageOptions_msginit = {
  3747. &google_protobuf_MessageOptions_submsgs[0],
  3748. &google_protobuf_MessageOptions__fields[0],
  3749. UPB_SIZE(16, 16), 5, false, 3, 255,
  3750. };
  3751. static const upb_msglayout *const google_protobuf_FieldOptions_submsgs[1] = {
  3752. &google_protobuf_UninterpretedOption_msginit,
  3753. };
  3754. static const upb_msglayout_field google_protobuf_FieldOptions__fields[7] = {
  3755. {1, UPB_SIZE(4, 4), 1, 0, 14, _UPB_MODE_SCALAR},
  3756. {2, UPB_SIZE(12, 12), 2, 0, 8, _UPB_MODE_SCALAR},
  3757. {3, UPB_SIZE(13, 13), 3, 0, 8, _UPB_MODE_SCALAR},
  3758. {5, UPB_SIZE(14, 14), 4, 0, 8, _UPB_MODE_SCALAR},
  3759. {6, UPB_SIZE(8, 8), 5, 0, 14, _UPB_MODE_SCALAR},
  3760. {10, UPB_SIZE(15, 15), 6, 0, 8, _UPB_MODE_SCALAR},
  3761. {999, UPB_SIZE(16, 16), 0, 0, 11, _UPB_MODE_ARRAY},
  3762. };
  3763. const upb_msglayout google_protobuf_FieldOptions_msginit = {
  3764. &google_protobuf_FieldOptions_submsgs[0],
  3765. &google_protobuf_FieldOptions__fields[0],
  3766. UPB_SIZE(24, 24), 7, false, 3, 255,
  3767. };
  3768. static const upb_msglayout *const google_protobuf_OneofOptions_submsgs[1] = {
  3769. &google_protobuf_UninterpretedOption_msginit,
  3770. };
  3771. static const upb_msglayout_field google_protobuf_OneofOptions__fields[1] = {
  3772. {999, UPB_SIZE(0, 0), 0, 0, 11, _UPB_MODE_ARRAY},
  3773. };
  3774. const upb_msglayout google_protobuf_OneofOptions_msginit = {
  3775. &google_protobuf_OneofOptions_submsgs[0],
  3776. &google_protobuf_OneofOptions__fields[0],
  3777. UPB_SIZE(8, 8), 1, false, 0, 255,
  3778. };
  3779. static const upb_msglayout *const google_protobuf_EnumOptions_submsgs[1] = {
  3780. &google_protobuf_UninterpretedOption_msginit,
  3781. };
  3782. static const upb_msglayout_field google_protobuf_EnumOptions__fields[3] = {
  3783. {2, UPB_SIZE(1, 1), 1, 0, 8, _UPB_MODE_SCALAR},
  3784. {3, UPB_SIZE(2, 2), 2, 0, 8, _UPB_MODE_SCALAR},
  3785. {999, UPB_SIZE(4, 8), 0, 0, 11, _UPB_MODE_ARRAY},
  3786. };
  3787. const upb_msglayout google_protobuf_EnumOptions_msginit = {
  3788. &google_protobuf_EnumOptions_submsgs[0],
  3789. &google_protobuf_EnumOptions__fields[0],
  3790. UPB_SIZE(8, 16), 3, false, 0, 255,
  3791. };
  3792. static const upb_msglayout *const google_protobuf_EnumValueOptions_submsgs[1] = {
  3793. &google_protobuf_UninterpretedOption_msginit,
  3794. };
  3795. static const upb_msglayout_field google_protobuf_EnumValueOptions__fields[2] = {
  3796. {1, UPB_SIZE(1, 1), 1, 0, 8, _UPB_MODE_SCALAR},
  3797. {999, UPB_SIZE(4, 8), 0, 0, 11, _UPB_MODE_ARRAY},
  3798. };
  3799. const upb_msglayout google_protobuf_EnumValueOptions_msginit = {
  3800. &google_protobuf_EnumValueOptions_submsgs[0],
  3801. &google_protobuf_EnumValueOptions__fields[0],
  3802. UPB_SIZE(8, 16), 2, false, 1, 255,
  3803. };
  3804. static const upb_msglayout *const google_protobuf_ServiceOptions_submsgs[1] = {
  3805. &google_protobuf_UninterpretedOption_msginit,
  3806. };
  3807. static const upb_msglayout_field google_protobuf_ServiceOptions__fields[2] = {
  3808. {33, UPB_SIZE(1, 1), 1, 0, 8, _UPB_MODE_SCALAR},
  3809. {999, UPB_SIZE(4, 8), 0, 0, 11, _UPB_MODE_ARRAY},
  3810. };
  3811. const upb_msglayout google_protobuf_ServiceOptions_msginit = {
  3812. &google_protobuf_ServiceOptions_submsgs[0],
  3813. &google_protobuf_ServiceOptions__fields[0],
  3814. UPB_SIZE(8, 16), 2, false, 0, 255,
  3815. };
  3816. static const upb_msglayout *const google_protobuf_MethodOptions_submsgs[1] = {
  3817. &google_protobuf_UninterpretedOption_msginit,
  3818. };
  3819. static const upb_msglayout_field google_protobuf_MethodOptions__fields[3] = {
  3820. {33, UPB_SIZE(8, 8), 1, 0, 8, _UPB_MODE_SCALAR},
  3821. {34, UPB_SIZE(4, 4), 2, 0, 14, _UPB_MODE_SCALAR},
  3822. {999, UPB_SIZE(12, 16), 0, 0, 11, _UPB_MODE_ARRAY},
  3823. };
  3824. const upb_msglayout google_protobuf_MethodOptions_msginit = {
  3825. &google_protobuf_MethodOptions_submsgs[0],
  3826. &google_protobuf_MethodOptions__fields[0],
  3827. UPB_SIZE(16, 24), 3, false, 0, 255,
  3828. };
  3829. static const upb_msglayout *const google_protobuf_UninterpretedOption_submsgs[1] = {
  3830. &google_protobuf_UninterpretedOption_NamePart_msginit,
  3831. };
  3832. static const upb_msglayout_field google_protobuf_UninterpretedOption__fields[7] = {
  3833. {2, UPB_SIZE(56, 80), 0, 0, 11, _UPB_MODE_ARRAY},
  3834. {3, UPB_SIZE(32, 32), 1, 0, 12, _UPB_MODE_SCALAR},
  3835. {4, UPB_SIZE(8, 8), 2, 0, 4, _UPB_MODE_SCALAR},
  3836. {5, UPB_SIZE(16, 16), 3, 0, 3, _UPB_MODE_SCALAR},
  3837. {6, UPB_SIZE(24, 24), 4, 0, 1, _UPB_MODE_SCALAR},
  3838. {7, UPB_SIZE(40, 48), 5, 0, 12, _UPB_MODE_SCALAR},
  3839. {8, UPB_SIZE(48, 64), 6, 0, 12, _UPB_MODE_SCALAR},
  3840. };
  3841. const upb_msglayout google_protobuf_UninterpretedOption_msginit = {
  3842. &google_protobuf_UninterpretedOption_submsgs[0],
  3843. &google_protobuf_UninterpretedOption__fields[0],
  3844. UPB_SIZE(64, 96), 7, false, 0, 255,
  3845. };
  3846. static const upb_msglayout_field google_protobuf_UninterpretedOption_NamePart__fields[2] = {
  3847. {1, UPB_SIZE(4, 8), 1, 0, 12, _UPB_MODE_SCALAR},
  3848. {2, UPB_SIZE(1, 1), 2, 0, 8, _UPB_MODE_SCALAR},
  3849. };
  3850. const upb_msglayout google_protobuf_UninterpretedOption_NamePart_msginit = {
  3851. NULL,
  3852. &google_protobuf_UninterpretedOption_NamePart__fields[0],
  3853. UPB_SIZE(16, 32), 2, false, 2, 255,
  3854. };
  3855. static const upb_msglayout *const google_protobuf_SourceCodeInfo_submsgs[1] = {
  3856. &google_protobuf_SourceCodeInfo_Location_msginit,
  3857. };
  3858. static const upb_msglayout_field google_protobuf_SourceCodeInfo__fields[1] = {
  3859. {1, UPB_SIZE(0, 0), 0, 0, 11, _UPB_MODE_ARRAY},
  3860. };
  3861. const upb_msglayout google_protobuf_SourceCodeInfo_msginit = {
  3862. &google_protobuf_SourceCodeInfo_submsgs[0],
  3863. &google_protobuf_SourceCodeInfo__fields[0],
  3864. UPB_SIZE(8, 8), 1, false, 1, 255,
  3865. };
  3866. static const upb_msglayout_field google_protobuf_SourceCodeInfo_Location__fields[5] = {
  3867. {1, UPB_SIZE(20, 40), 0, 0, 5, _UPB_MODE_ARRAY | _UPB_MODE_IS_PACKED},
  3868. {2, UPB_SIZE(24, 48), 0, 0, 5, _UPB_MODE_ARRAY | _UPB_MODE_IS_PACKED},
  3869. {3, UPB_SIZE(4, 8), 1, 0, 12, _UPB_MODE_SCALAR},
  3870. {4, UPB_SIZE(12, 24), 2, 0, 12, _UPB_MODE_SCALAR},
  3871. {6, UPB_SIZE(28, 56), 0, 0, 12, _UPB_MODE_ARRAY},
  3872. };
  3873. const upb_msglayout google_protobuf_SourceCodeInfo_Location_msginit = {
  3874. NULL,
  3875. &google_protobuf_SourceCodeInfo_Location__fields[0],
  3876. UPB_SIZE(32, 64), 5, false, 4, 255,
  3877. };
  3878. static const upb_msglayout *const google_protobuf_GeneratedCodeInfo_submsgs[1] = {
  3879. &google_protobuf_GeneratedCodeInfo_Annotation_msginit,
  3880. };
  3881. static const upb_msglayout_field google_protobuf_GeneratedCodeInfo__fields[1] = {
  3882. {1, UPB_SIZE(0, 0), 0, 0, 11, _UPB_MODE_ARRAY},
  3883. };
  3884. const upb_msglayout google_protobuf_GeneratedCodeInfo_msginit = {
  3885. &google_protobuf_GeneratedCodeInfo_submsgs[0],
  3886. &google_protobuf_GeneratedCodeInfo__fields[0],
  3887. UPB_SIZE(8, 8), 1, false, 1, 255,
  3888. };
  3889. static const upb_msglayout_field google_protobuf_GeneratedCodeInfo_Annotation__fields[4] = {
  3890. {1, UPB_SIZE(20, 32), 0, 0, 5, _UPB_MODE_ARRAY | _UPB_MODE_IS_PACKED},
  3891. {2, UPB_SIZE(12, 16), 1, 0, 12, _UPB_MODE_SCALAR},
  3892. {3, UPB_SIZE(4, 4), 2, 0, 5, _UPB_MODE_SCALAR},
  3893. {4, UPB_SIZE(8, 8), 3, 0, 5, _UPB_MODE_SCALAR},
  3894. };
  3895. const upb_msglayout google_protobuf_GeneratedCodeInfo_Annotation_msginit = {
  3896. NULL,
  3897. &google_protobuf_GeneratedCodeInfo_Annotation__fields[0],
  3898. UPB_SIZE(24, 48), 4, false, 4, 255,
  3899. };
  3900. /** upb/def.c ************************************************************/
  3901. #include <ctype.h>
  3902. #include <errno.h>
  3903. #include <setjmp.h>
  3904. #include <stdlib.h>
  3905. #include <string.h>
  3906. /* Must be last. */
  3907. typedef struct {
  3908. size_t len;
  3909. char str[1]; /* Null-terminated string data follows. */
  3910. } str_t;
  3911. struct upb_fielddef {
  3912. const upb_filedef *file;
  3913. const upb_msgdef *msgdef;
  3914. const char *full_name;
  3915. const char *json_name;
  3916. union {
  3917. int64_t sint;
  3918. uint64_t uint;
  3919. double dbl;
  3920. float flt;
  3921. bool boolean;
  3922. str_t *str;
  3923. } defaultval;
  3924. const upb_oneofdef *oneof;
  3925. union {
  3926. const upb_msgdef *msgdef;
  3927. const upb_enumdef *enumdef;
  3928. const google_protobuf_FieldDescriptorProto *unresolved;
  3929. } sub;
  3930. uint32_t number_;
  3931. uint16_t index_;
  3932. uint16_t layout_index;
  3933. bool is_extension_;
  3934. bool lazy_;
  3935. bool packed_;
  3936. bool proto3_optional_;
  3937. upb_descriptortype_t type_;
  3938. upb_label_t label_;
  3939. };
  3940. struct upb_msgdef {
  3941. const upb_msglayout *layout;
  3942. const upb_filedef *file;
  3943. const char *full_name;
  3944. /* Tables for looking up fields by number and name. */
  3945. upb_inttable itof;
  3946. upb_strtable ntof;
  3947. const upb_fielddef *fields;
  3948. const upb_oneofdef *oneofs;
  3949. int field_count;
  3950. int oneof_count;
  3951. int real_oneof_count;
  3952. /* Is this a map-entry message? */
  3953. bool map_entry;
  3954. upb_wellknowntype_t well_known_type;
  3955. /* TODO(haberman): proper extension ranges (there can be multiple). */
  3956. };
  3957. struct upb_enumdef {
  3958. const upb_filedef *file;
  3959. const char *full_name;
  3960. upb_strtable ntoi;
  3961. upb_inttable iton;
  3962. int32_t defaultval;
  3963. };
  3964. struct upb_oneofdef {
  3965. const upb_msgdef *parent;
  3966. const char *full_name;
  3967. int field_count;
  3968. bool synthetic;
  3969. const upb_fielddef **fields;
  3970. upb_strtable ntof;
  3971. upb_inttable itof;
  3972. };
  3973. struct upb_filedef {
  3974. const char *name;
  3975. const char *package;
  3976. const char *phpprefix;
  3977. const char *phpnamespace;
  3978. const upb_filedef **deps;
  3979. const upb_msgdef *msgs;
  3980. const upb_enumdef *enums;
  3981. const upb_fielddef *exts;
  3982. const upb_symtab *symtab;
  3983. int dep_count;
  3984. int msg_count;
  3985. int enum_count;
  3986. int ext_count;
  3987. upb_syntax_t syntax;
  3988. };
  3989. struct upb_symtab {
  3990. upb_arena *arena;
  3991. upb_strtable syms; /* full_name -> packed def ptr */
  3992. upb_strtable files; /* file_name -> upb_filedef* */
  3993. size_t bytes_loaded;
  3994. };
  3995. /* Inside a symtab we store tagged pointers to specific def types. */
  3996. typedef enum {
  3997. UPB_DEFTYPE_FIELD = 0,
  3998. /* Only inside symtab table. */
  3999. UPB_DEFTYPE_MSG = 1,
  4000. UPB_DEFTYPE_ENUM = 2,
  4001. /* Only inside message table. */
  4002. UPB_DEFTYPE_ONEOF = 1,
  4003. UPB_DEFTYPE_FIELD_JSONNAME = 2
  4004. } upb_deftype_t;
  4005. static const void *unpack_def(upb_value v, upb_deftype_t type) {
  4006. uintptr_t num = (uintptr_t)upb_value_getconstptr(v);
  4007. return (num & 3) == type ? (const void*)(num & ~3) : NULL;
  4008. }
  4009. static upb_value pack_def(const void *ptr, upb_deftype_t type) {
  4010. uintptr_t num = (uintptr_t)ptr | type;
  4011. return upb_value_constptr((const void*)num);
  4012. }
  4013. /* isalpha() etc. from <ctype.h> are locale-dependent, which we don't want. */
  4014. static bool upb_isbetween(char c, char low, char high) {
  4015. return c >= low && c <= high;
  4016. }
  4017. static bool upb_isletter(char c) {
  4018. return upb_isbetween(c, 'A', 'Z') || upb_isbetween(c, 'a', 'z') || c == '_';
  4019. }
  4020. static bool upb_isalphanum(char c) {
  4021. return upb_isletter(c) || upb_isbetween(c, '0', '9');
  4022. }
  4023. static const char *shortdefname(const char *fullname) {
  4024. const char *p;
  4025. if (fullname == NULL) {
  4026. return NULL;
  4027. } else if ((p = strrchr(fullname, '.')) == NULL) {
  4028. /* No '.' in the name, return the full string. */
  4029. return fullname;
  4030. } else {
  4031. /* Return one past the last '.'. */
  4032. return p + 1;
  4033. }
  4034. }
  4035. /* All submessage fields are lower than all other fields.
  4036. * Secondly, fields are increasing in order. */
  4037. uint32_t field_rank(const upb_fielddef *f) {
  4038. uint32_t ret = upb_fielddef_number(f);
  4039. const uint32_t high_bit = 1 << 30;
  4040. UPB_ASSERT(ret < high_bit);
  4041. if (!upb_fielddef_issubmsg(f))
  4042. ret |= high_bit;
  4043. return ret;
  4044. }
  4045. int cmp_fields(const void *p1, const void *p2) {
  4046. const upb_fielddef *f1 = *(upb_fielddef*const*)p1;
  4047. const upb_fielddef *f2 = *(upb_fielddef*const*)p2;
  4048. return field_rank(f1) - field_rank(f2);
  4049. }
  4050. static void upb_status_setoom(upb_status *status) {
  4051. upb_status_seterrmsg(status, "out of memory");
  4052. }
  4053. static void assign_msg_wellknowntype(upb_msgdef *m) {
  4054. const char *name = upb_msgdef_fullname(m);
  4055. if (name == NULL) {
  4056. m->well_known_type = UPB_WELLKNOWN_UNSPECIFIED;
  4057. return;
  4058. }
  4059. if (!strcmp(name, "google.protobuf.Any")) {
  4060. m->well_known_type = UPB_WELLKNOWN_ANY;
  4061. } else if (!strcmp(name, "google.protobuf.FieldMask")) {
  4062. m->well_known_type = UPB_WELLKNOWN_FIELDMASK;
  4063. } else if (!strcmp(name, "google.protobuf.Duration")) {
  4064. m->well_known_type = UPB_WELLKNOWN_DURATION;
  4065. } else if (!strcmp(name, "google.protobuf.Timestamp")) {
  4066. m->well_known_type = UPB_WELLKNOWN_TIMESTAMP;
  4067. } else if (!strcmp(name, "google.protobuf.DoubleValue")) {
  4068. m->well_known_type = UPB_WELLKNOWN_DOUBLEVALUE;
  4069. } else if (!strcmp(name, "google.protobuf.FloatValue")) {
  4070. m->well_known_type = UPB_WELLKNOWN_FLOATVALUE;
  4071. } else if (!strcmp(name, "google.protobuf.Int64Value")) {
  4072. m->well_known_type = UPB_WELLKNOWN_INT64VALUE;
  4073. } else if (!strcmp(name, "google.protobuf.UInt64Value")) {
  4074. m->well_known_type = UPB_WELLKNOWN_UINT64VALUE;
  4075. } else if (!strcmp(name, "google.protobuf.Int32Value")) {
  4076. m->well_known_type = UPB_WELLKNOWN_INT32VALUE;
  4077. } else if (!strcmp(name, "google.protobuf.UInt32Value")) {
  4078. m->well_known_type = UPB_WELLKNOWN_UINT32VALUE;
  4079. } else if (!strcmp(name, "google.protobuf.BoolValue")) {
  4080. m->well_known_type = UPB_WELLKNOWN_BOOLVALUE;
  4081. } else if (!strcmp(name, "google.protobuf.StringValue")) {
  4082. m->well_known_type = UPB_WELLKNOWN_STRINGVALUE;
  4083. } else if (!strcmp(name, "google.protobuf.BytesValue")) {
  4084. m->well_known_type = UPB_WELLKNOWN_BYTESVALUE;
  4085. } else if (!strcmp(name, "google.protobuf.Value")) {
  4086. m->well_known_type = UPB_WELLKNOWN_VALUE;
  4087. } else if (!strcmp(name, "google.protobuf.ListValue")) {
  4088. m->well_known_type = UPB_WELLKNOWN_LISTVALUE;
  4089. } else if (!strcmp(name, "google.protobuf.Struct")) {
  4090. m->well_known_type = UPB_WELLKNOWN_STRUCT;
  4091. } else {
  4092. m->well_known_type = UPB_WELLKNOWN_UNSPECIFIED;
  4093. }
  4094. }
  4095. /* upb_enumdef ****************************************************************/
  4096. const char *upb_enumdef_fullname(const upb_enumdef *e) {
  4097. return e->full_name;
  4098. }
  4099. const char *upb_enumdef_name(const upb_enumdef *e) {
  4100. return shortdefname(e->full_name);
  4101. }
  4102. const upb_filedef *upb_enumdef_file(const upb_enumdef *e) {
  4103. return e->file;
  4104. }
  4105. int32_t upb_enumdef_default(const upb_enumdef *e) {
  4106. UPB_ASSERT(upb_enumdef_iton(e, e->defaultval));
  4107. return e->defaultval;
  4108. }
  4109. int upb_enumdef_numvals(const upb_enumdef *e) {
  4110. return (int)upb_strtable_count(&e->ntoi);
  4111. }
  4112. void upb_enum_begin(upb_enum_iter *i, const upb_enumdef *e) {
  4113. /* We iterate over the ntoi table, to account for duplicate numbers. */
  4114. upb_strtable_begin(i, &e->ntoi);
  4115. }
  4116. void upb_enum_next(upb_enum_iter *iter) { upb_strtable_next(iter); }
  4117. bool upb_enum_done(upb_enum_iter *iter) { return upb_strtable_done(iter); }
  4118. bool upb_enumdef_ntoi(const upb_enumdef *def, const char *name,
  4119. size_t len, int32_t *num) {
  4120. upb_value v;
  4121. if (!upb_strtable_lookup2(&def->ntoi, name, len, &v)) {
  4122. return false;
  4123. }
  4124. if (num) *num = upb_value_getint32(v);
  4125. return true;
  4126. }
  4127. const char *upb_enumdef_iton(const upb_enumdef *def, int32_t num) {
  4128. upb_value v;
  4129. return upb_inttable_lookup(&def->iton, num, &v) ? upb_value_getcstr(v) : NULL;
  4130. }
  4131. const char *upb_enum_iter_name(upb_enum_iter *iter) {
  4132. return upb_strtable_iter_key(iter).data;
  4133. }
  4134. int32_t upb_enum_iter_number(upb_enum_iter *iter) {
  4135. return upb_value_getint32(upb_strtable_iter_value(iter));
  4136. }
  4137. /* upb_fielddef ***************************************************************/
  4138. const char *upb_fielddef_fullname(const upb_fielddef *f) {
  4139. return f->full_name;
  4140. }
  4141. upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f) {
  4142. switch (f->type_) {
  4143. case UPB_DESCRIPTOR_TYPE_DOUBLE:
  4144. return UPB_TYPE_DOUBLE;
  4145. case UPB_DESCRIPTOR_TYPE_FLOAT:
  4146. return UPB_TYPE_FLOAT;
  4147. case UPB_DESCRIPTOR_TYPE_INT64:
  4148. case UPB_DESCRIPTOR_TYPE_SINT64:
  4149. case UPB_DESCRIPTOR_TYPE_SFIXED64:
  4150. return UPB_TYPE_INT64;
  4151. case UPB_DESCRIPTOR_TYPE_INT32:
  4152. case UPB_DESCRIPTOR_TYPE_SFIXED32:
  4153. case UPB_DESCRIPTOR_TYPE_SINT32:
  4154. return UPB_TYPE_INT32;
  4155. case UPB_DESCRIPTOR_TYPE_UINT64:
  4156. case UPB_DESCRIPTOR_TYPE_FIXED64:
  4157. return UPB_TYPE_UINT64;
  4158. case UPB_DESCRIPTOR_TYPE_UINT32:
  4159. case UPB_DESCRIPTOR_TYPE_FIXED32:
  4160. return UPB_TYPE_UINT32;
  4161. case UPB_DESCRIPTOR_TYPE_ENUM:
  4162. return UPB_TYPE_ENUM;
  4163. case UPB_DESCRIPTOR_TYPE_BOOL:
  4164. return UPB_TYPE_BOOL;
  4165. case UPB_DESCRIPTOR_TYPE_STRING:
  4166. return UPB_TYPE_STRING;
  4167. case UPB_DESCRIPTOR_TYPE_BYTES:
  4168. return UPB_TYPE_BYTES;
  4169. case UPB_DESCRIPTOR_TYPE_GROUP:
  4170. case UPB_DESCRIPTOR_TYPE_MESSAGE:
  4171. return UPB_TYPE_MESSAGE;
  4172. }
  4173. UPB_UNREACHABLE();
  4174. }
  4175. upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f) {
  4176. return f->type_;
  4177. }
  4178. uint32_t upb_fielddef_index(const upb_fielddef *f) {
  4179. return f->index_;
  4180. }
  4181. upb_label_t upb_fielddef_label(const upb_fielddef *f) {
  4182. return f->label_;
  4183. }
  4184. uint32_t upb_fielddef_number(const upb_fielddef *f) {
  4185. return f->number_;
  4186. }
  4187. bool upb_fielddef_isextension(const upb_fielddef *f) {
  4188. return f->is_extension_;
  4189. }
  4190. bool upb_fielddef_lazy(const upb_fielddef *f) {
  4191. return f->lazy_;
  4192. }
  4193. bool upb_fielddef_packed(const upb_fielddef *f) {
  4194. return f->packed_;
  4195. }
  4196. const char *upb_fielddef_name(const upb_fielddef *f) {
  4197. return shortdefname(f->full_name);
  4198. }
  4199. const char *upb_fielddef_jsonname(const upb_fielddef *f) {
  4200. return f->json_name;
  4201. }
  4202. const upb_filedef *upb_fielddef_file(const upb_fielddef *f) {
  4203. return f->file;
  4204. }
  4205. const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f) {
  4206. return f->msgdef;
  4207. }
  4208. const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f) {
  4209. return f->oneof;
  4210. }
  4211. const upb_oneofdef *upb_fielddef_realcontainingoneof(const upb_fielddef *f) {
  4212. if (!f->oneof || upb_oneofdef_issynthetic(f->oneof)) return NULL;
  4213. return f->oneof;
  4214. }
  4215. upb_msgval upb_fielddef_default(const upb_fielddef *f) {
  4216. UPB_ASSERT(!upb_fielddef_issubmsg(f));
  4217. upb_msgval ret;
  4218. if (upb_fielddef_isstring(f)) {
  4219. str_t *str = f->defaultval.str;
  4220. if (str) {
  4221. ret.str_val.data = str->str;
  4222. ret.str_val.size = str->len;
  4223. } else {
  4224. ret.str_val.size = 0;
  4225. }
  4226. } else {
  4227. memcpy(&ret, &f->defaultval, 8);
  4228. }
  4229. return ret;
  4230. }
  4231. static void chkdefaulttype(const upb_fielddef *f, int ctype) {
  4232. UPB_UNUSED(f);
  4233. UPB_UNUSED(ctype);
  4234. }
  4235. int64_t upb_fielddef_defaultint64(const upb_fielddef *f) {
  4236. chkdefaulttype(f, UPB_TYPE_INT64);
  4237. return f->defaultval.sint;
  4238. }
  4239. int32_t upb_fielddef_defaultint32(const upb_fielddef *f) {
  4240. chkdefaulttype(f, UPB_TYPE_INT32);
  4241. return (int32_t)f->defaultval.sint;
  4242. }
  4243. uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f) {
  4244. chkdefaulttype(f, UPB_TYPE_UINT64);
  4245. return f->defaultval.uint;
  4246. }
  4247. uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f) {
  4248. chkdefaulttype(f, UPB_TYPE_UINT32);
  4249. return (uint32_t)f->defaultval.uint;
  4250. }
  4251. bool upb_fielddef_defaultbool(const upb_fielddef *f) {
  4252. chkdefaulttype(f, UPB_TYPE_BOOL);
  4253. return f->defaultval.boolean;
  4254. }
  4255. float upb_fielddef_defaultfloat(const upb_fielddef *f) {
  4256. chkdefaulttype(f, UPB_TYPE_FLOAT);
  4257. return f->defaultval.flt;
  4258. }
  4259. double upb_fielddef_defaultdouble(const upb_fielddef *f) {
  4260. chkdefaulttype(f, UPB_TYPE_DOUBLE);
  4261. return f->defaultval.dbl;
  4262. }
  4263. const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len) {
  4264. str_t *str = f->defaultval.str;
  4265. UPB_ASSERT(upb_fielddef_type(f) == UPB_TYPE_STRING ||
  4266. upb_fielddef_type(f) == UPB_TYPE_BYTES ||
  4267. upb_fielddef_type(f) == UPB_TYPE_ENUM);
  4268. if (str) {
  4269. if (len) *len = str->len;
  4270. return str->str;
  4271. } else {
  4272. if (len) *len = 0;
  4273. return NULL;
  4274. }
  4275. }
  4276. const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f) {
  4277. return upb_fielddef_type(f) == UPB_TYPE_MESSAGE ? f->sub.msgdef : NULL;
  4278. }
  4279. const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f) {
  4280. return upb_fielddef_type(f) == UPB_TYPE_ENUM ? f->sub.enumdef : NULL;
  4281. }
  4282. const upb_msglayout_field *upb_fielddef_layout(const upb_fielddef *f) {
  4283. return &f->msgdef->layout->fields[f->layout_index];
  4284. }
  4285. bool upb_fielddef_issubmsg(const upb_fielddef *f) {
  4286. return upb_fielddef_type(f) == UPB_TYPE_MESSAGE;
  4287. }
  4288. bool upb_fielddef_isstring(const upb_fielddef *f) {
  4289. return upb_fielddef_type(f) == UPB_TYPE_STRING ||
  4290. upb_fielddef_type(f) == UPB_TYPE_BYTES;
  4291. }
  4292. bool upb_fielddef_isseq(const upb_fielddef *f) {
  4293. return upb_fielddef_label(f) == UPB_LABEL_REPEATED;
  4294. }
  4295. bool upb_fielddef_isprimitive(const upb_fielddef *f) {
  4296. return !upb_fielddef_isstring(f) && !upb_fielddef_issubmsg(f);
  4297. }
  4298. bool upb_fielddef_ismap(const upb_fielddef *f) {
  4299. return upb_fielddef_isseq(f) && upb_fielddef_issubmsg(f) &&
  4300. upb_msgdef_mapentry(upb_fielddef_msgsubdef(f));
  4301. }
  4302. bool upb_fielddef_hassubdef(const upb_fielddef *f) {
  4303. return upb_fielddef_issubmsg(f) || upb_fielddef_type(f) == UPB_TYPE_ENUM;
  4304. }
  4305. bool upb_fielddef_haspresence(const upb_fielddef *f) {
  4306. if (upb_fielddef_isseq(f)) return false;
  4307. return upb_fielddef_issubmsg(f) || upb_fielddef_containingoneof(f) ||
  4308. f->file->syntax == UPB_SYNTAX_PROTO2;
  4309. }
  4310. static bool between(int32_t x, int32_t low, int32_t high) {
  4311. return x >= low && x <= high;
  4312. }
  4313. bool upb_fielddef_checklabel(int32_t label) { return between(label, 1, 3); }
  4314. bool upb_fielddef_checktype(int32_t type) { return between(type, 1, 11); }
  4315. bool upb_fielddef_checkintfmt(int32_t fmt) { return between(fmt, 1, 3); }
  4316. bool upb_fielddef_checkdescriptortype(int32_t type) {
  4317. return between(type, 1, 18);
  4318. }
  4319. /* upb_msgdef *****************************************************************/
  4320. const char *upb_msgdef_fullname(const upb_msgdef *m) {
  4321. return m->full_name;
  4322. }
  4323. const upb_filedef *upb_msgdef_file(const upb_msgdef *m) {
  4324. return m->file;
  4325. }
  4326. const char *upb_msgdef_name(const upb_msgdef *m) {
  4327. return shortdefname(m->full_name);
  4328. }
  4329. upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m) {
  4330. return m->file->syntax;
  4331. }
  4332. const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i) {
  4333. upb_value val;
  4334. return upb_inttable_lookup(&m->itof, i, &val) ? upb_value_getconstptr(val)
  4335. : NULL;
  4336. }
  4337. const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name,
  4338. size_t len) {
  4339. upb_value val;
  4340. if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
  4341. return NULL;
  4342. }
  4343. return unpack_def(val, UPB_DEFTYPE_FIELD);
  4344. }
  4345. const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name,
  4346. size_t len) {
  4347. upb_value val;
  4348. if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
  4349. return NULL;
  4350. }
  4351. return unpack_def(val, UPB_DEFTYPE_ONEOF);
  4352. }
  4353. bool upb_msgdef_lookupname(const upb_msgdef *m, const char *name, size_t len,
  4354. const upb_fielddef **f, const upb_oneofdef **o) {
  4355. upb_value val;
  4356. if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
  4357. return false;
  4358. }
  4359. *o = unpack_def(val, UPB_DEFTYPE_ONEOF);
  4360. *f = unpack_def(val, UPB_DEFTYPE_FIELD);
  4361. return *o || *f; /* False if this was a JSON name. */
  4362. }
  4363. const upb_fielddef *upb_msgdef_lookupjsonname(const upb_msgdef *m,
  4364. const char *name, size_t len) {
  4365. upb_value val;
  4366. const upb_fielddef* f;
  4367. if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
  4368. return NULL;
  4369. }
  4370. f = unpack_def(val, UPB_DEFTYPE_FIELD);
  4371. if (!f) f = unpack_def(val, UPB_DEFTYPE_FIELD_JSONNAME);
  4372. return f;
  4373. }
  4374. int upb_msgdef_numfields(const upb_msgdef *m) {
  4375. return m->field_count;
  4376. }
  4377. int upb_msgdef_numoneofs(const upb_msgdef *m) {
  4378. return m->oneof_count;
  4379. }
  4380. int upb_msgdef_numrealoneofs(const upb_msgdef *m) {
  4381. return m->real_oneof_count;
  4382. }
  4383. int upb_msgdef_fieldcount(const upb_msgdef *m) {
  4384. return m->field_count;
  4385. }
  4386. int upb_msgdef_oneofcount(const upb_msgdef *m) {
  4387. return m->oneof_count;
  4388. }
  4389. int upb_msgdef_realoneofcount(const upb_msgdef *m) {
  4390. return m->real_oneof_count;
  4391. }
  4392. const upb_msglayout *upb_msgdef_layout(const upb_msgdef *m) {
  4393. return m->layout;
  4394. }
  4395. const upb_fielddef *upb_msgdef_field(const upb_msgdef *m, int i) {
  4396. UPB_ASSERT(i >= 0 && i < m->field_count);
  4397. return &m->fields[i];
  4398. }
  4399. const upb_oneofdef *upb_msgdef_oneof(const upb_msgdef *m, int i) {
  4400. UPB_ASSERT(i >= 0 && i < m->oneof_count);
  4401. return &m->oneofs[i];
  4402. }
  4403. bool upb_msgdef_mapentry(const upb_msgdef *m) {
  4404. return m->map_entry;
  4405. }
  4406. upb_wellknowntype_t upb_msgdef_wellknowntype(const upb_msgdef *m) {
  4407. return m->well_known_type;
  4408. }
  4409. bool upb_msgdef_isnumberwrapper(const upb_msgdef *m) {
  4410. upb_wellknowntype_t type = upb_msgdef_wellknowntype(m);
  4411. return type >= UPB_WELLKNOWN_DOUBLEVALUE &&
  4412. type <= UPB_WELLKNOWN_UINT32VALUE;
  4413. }
  4414. bool upb_msgdef_iswrapper(const upb_msgdef *m) {
  4415. upb_wellknowntype_t type = upb_msgdef_wellknowntype(m);
  4416. return type >= UPB_WELLKNOWN_DOUBLEVALUE &&
  4417. type <= UPB_WELLKNOWN_BOOLVALUE;
  4418. }
  4419. void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m) {
  4420. upb_inttable_begin(iter, &m->itof);
  4421. }
  4422. void upb_msg_field_next(upb_msg_field_iter *iter) { upb_inttable_next(iter); }
  4423. bool upb_msg_field_done(const upb_msg_field_iter *iter) {
  4424. return upb_inttable_done(iter);
  4425. }
  4426. upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter) {
  4427. return (upb_fielddef *)upb_value_getconstptr(upb_inttable_iter_value(iter));
  4428. }
  4429. void upb_msg_field_iter_setdone(upb_msg_field_iter *iter) {
  4430. upb_inttable_iter_setdone(iter);
  4431. }
  4432. bool upb_msg_field_iter_isequal(const upb_msg_field_iter * iter1,
  4433. const upb_msg_field_iter * iter2) {
  4434. return upb_inttable_iter_isequal(iter1, iter2);
  4435. }
  4436. void upb_msg_oneof_begin(upb_msg_oneof_iter *iter, const upb_msgdef *m) {
  4437. upb_strtable_begin(iter, &m->ntof);
  4438. /* We need to skip past any initial fields. */
  4439. while (!upb_strtable_done(iter) &&
  4440. !unpack_def(upb_strtable_iter_value(iter), UPB_DEFTYPE_ONEOF)) {
  4441. upb_strtable_next(iter);
  4442. }
  4443. }
  4444. void upb_msg_oneof_next(upb_msg_oneof_iter *iter) {
  4445. /* We need to skip past fields to return only oneofs. */
  4446. do {
  4447. upb_strtable_next(iter);
  4448. } while (!upb_strtable_done(iter) &&
  4449. !unpack_def(upb_strtable_iter_value(iter), UPB_DEFTYPE_ONEOF));
  4450. }
  4451. bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter) {
  4452. return upb_strtable_done(iter);
  4453. }
  4454. const upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter) {
  4455. return unpack_def(upb_strtable_iter_value(iter), UPB_DEFTYPE_ONEOF);
  4456. }
  4457. void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter) {
  4458. upb_strtable_iter_setdone(iter);
  4459. }
  4460. bool upb_msg_oneof_iter_isequal(const upb_msg_oneof_iter *iter1,
  4461. const upb_msg_oneof_iter *iter2) {
  4462. return upb_strtable_iter_isequal(iter1, iter2);
  4463. }
  4464. /* upb_oneofdef ***************************************************************/
  4465. const char *upb_oneofdef_name(const upb_oneofdef *o) {
  4466. return shortdefname(o->full_name);
  4467. }
  4468. const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o) {
  4469. return o->parent;
  4470. }
  4471. int upb_oneofdef_fieldcount(const upb_oneofdef *o) {
  4472. return o->field_count;
  4473. }
  4474. const upb_fielddef *upb_oneofdef_field(const upb_oneofdef *o, int i) {
  4475. UPB_ASSERT(i < o->field_count);
  4476. return o->fields[i];
  4477. }
  4478. int upb_oneofdef_numfields(const upb_oneofdef *o) {
  4479. return o->field_count;
  4480. }
  4481. uint32_t upb_oneofdef_index(const upb_oneofdef *o) {
  4482. return o - o->parent->oneofs;
  4483. }
  4484. bool upb_oneofdef_issynthetic(const upb_oneofdef *o) {
  4485. return o->synthetic;
  4486. }
  4487. const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o,
  4488. const char *name, size_t length) {
  4489. upb_value val;
  4490. return upb_strtable_lookup2(&o->ntof, name, length, &val) ?
  4491. upb_value_getptr(val) : NULL;
  4492. }
  4493. const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num) {
  4494. upb_value val;
  4495. return upb_inttable_lookup(&o->itof, num, &val) ? upb_value_getptr(val)
  4496. : NULL;
  4497. }
  4498. void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o) {
  4499. upb_inttable_begin(iter, &o->itof);
  4500. }
  4501. void upb_oneof_next(upb_oneof_iter *iter) {
  4502. upb_inttable_next(iter);
  4503. }
  4504. bool upb_oneof_done(upb_oneof_iter *iter) {
  4505. return upb_inttable_done(iter);
  4506. }
  4507. upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter) {
  4508. return (upb_fielddef *)upb_value_getconstptr(upb_inttable_iter_value(iter));
  4509. }
  4510. void upb_oneof_iter_setdone(upb_oneof_iter *iter) {
  4511. upb_inttable_iter_setdone(iter);
  4512. }
  4513. /* upb_filedef ****************************************************************/
  4514. const char *upb_filedef_name(const upb_filedef *f) {
  4515. return f->name;
  4516. }
  4517. const char *upb_filedef_package(const upb_filedef *f) {
  4518. return f->package;
  4519. }
  4520. const char *upb_filedef_phpprefix(const upb_filedef *f) {
  4521. return f->phpprefix;
  4522. }
  4523. const char *upb_filedef_phpnamespace(const upb_filedef *f) {
  4524. return f->phpnamespace;
  4525. }
  4526. upb_syntax_t upb_filedef_syntax(const upb_filedef *f) {
  4527. return f->syntax;
  4528. }
  4529. int upb_filedef_msgcount(const upb_filedef *f) {
  4530. return f->msg_count;
  4531. }
  4532. int upb_filedef_depcount(const upb_filedef *f) {
  4533. return f->dep_count;
  4534. }
  4535. int upb_filedef_enumcount(const upb_filedef *f) {
  4536. return f->enum_count;
  4537. }
  4538. const upb_filedef *upb_filedef_dep(const upb_filedef *f, int i) {
  4539. return i < 0 || i >= f->dep_count ? NULL : f->deps[i];
  4540. }
  4541. const upb_msgdef *upb_filedef_msg(const upb_filedef *f, int i) {
  4542. return i < 0 || i >= f->msg_count ? NULL : &f->msgs[i];
  4543. }
  4544. const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i) {
  4545. return i < 0 || i >= f->enum_count ? NULL : &f->enums[i];
  4546. }
  4547. const upb_symtab *upb_filedef_symtab(const upb_filedef *f) {
  4548. return f->symtab;
  4549. }
  4550. void upb_symtab_free(upb_symtab *s) {
  4551. upb_arena_free(s->arena);
  4552. upb_gfree(s);
  4553. }
  4554. upb_symtab *upb_symtab_new(void) {
  4555. upb_symtab *s = upb_gmalloc(sizeof(*s));
  4556. if (!s) {
  4557. return NULL;
  4558. }
  4559. s->arena = upb_arena_new();
  4560. s->bytes_loaded = 0;
  4561. if (!upb_strtable_init(&s->syms, 32, s->arena) ||
  4562. !upb_strtable_init(&s->files, 4, s->arena)) {
  4563. upb_arena_free(s->arena);
  4564. upb_gfree(s);
  4565. s = NULL;
  4566. }
  4567. return s;
  4568. }
  4569. const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym) {
  4570. upb_value v;
  4571. return upb_strtable_lookup(&s->syms, sym, &v) ?
  4572. unpack_def(v, UPB_DEFTYPE_MSG) : NULL;
  4573. }
  4574. const upb_msgdef *upb_symtab_lookupmsg2(const upb_symtab *s, const char *sym,
  4575. size_t len) {
  4576. upb_value v;
  4577. return upb_strtable_lookup2(&s->syms, sym, len, &v) ?
  4578. unpack_def(v, UPB_DEFTYPE_MSG) : NULL;
  4579. }
  4580. const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym) {
  4581. upb_value v;
  4582. return upb_strtable_lookup(&s->syms, sym, &v) ?
  4583. unpack_def(v, UPB_DEFTYPE_ENUM) : NULL;
  4584. }
  4585. const upb_filedef *upb_symtab_lookupfile(const upb_symtab *s, const char *name) {
  4586. upb_value v;
  4587. return upb_strtable_lookup(&s->files, name, &v) ? upb_value_getconstptr(v)
  4588. : NULL;
  4589. }
  4590. const upb_filedef *upb_symtab_lookupfile2(
  4591. const upb_symtab *s, const char *name, size_t len) {
  4592. upb_value v;
  4593. return upb_strtable_lookup2(&s->files, name, len, &v) ?
  4594. upb_value_getconstptr(v) : NULL;
  4595. }
  4596. int upb_symtab_filecount(const upb_symtab *s) {
  4597. return (int)upb_strtable_count(&s->files);
  4598. }
  4599. /* Code to build defs from descriptor protos. *********************************/
  4600. /* There is a question of how much validation to do here. It will be difficult
  4601. * to perfectly match the amount of validation performed by proto2. But since
  4602. * this code is used to directly build defs from Ruby (for example) we do need
  4603. * to validate important constraints like uniqueness of names and numbers. */
  4604. #define CHK_OOM(x) if (!(x)) { symtab_oomerr(ctx); }
  4605. typedef struct {
  4606. upb_symtab *symtab;
  4607. upb_filedef *file; /* File we are building. */
  4608. upb_arena *arena; /* Allocate defs here. */
  4609. const upb_msglayout **layouts; /* NULL if we should build layouts. */
  4610. upb_status *status; /* Record errors here. */
  4611. jmp_buf err; /* longjmp() on error. */
  4612. } symtab_addctx;
  4613. UPB_NORETURN UPB_NOINLINE UPB_PRINTF(2, 3)
  4614. static void symtab_errf(symtab_addctx *ctx, const char *fmt, ...) {
  4615. va_list argp;
  4616. va_start(argp, fmt);
  4617. upb_status_vseterrf(ctx->status, fmt, argp);
  4618. va_end(argp);
  4619. UPB_LONGJMP(ctx->err, 1);
  4620. }
  4621. UPB_NORETURN UPB_NOINLINE
  4622. static void symtab_oomerr(symtab_addctx *ctx) {
  4623. upb_status_setoom(ctx->status);
  4624. UPB_LONGJMP(ctx->err, 1);
  4625. }
  4626. void *symtab_alloc(symtab_addctx *ctx, size_t bytes) {
  4627. void *ret = upb_arena_malloc(ctx->arena, bytes);
  4628. if (!ret) symtab_oomerr(ctx);
  4629. return ret;
  4630. }
  4631. static void check_ident(symtab_addctx *ctx, upb_strview name, bool full) {
  4632. const char *str = name.data;
  4633. size_t len = name.size;
  4634. bool start = true;
  4635. size_t i;
  4636. for (i = 0; i < len; i++) {
  4637. char c = str[i];
  4638. if (c == '.') {
  4639. if (start || !full) {
  4640. symtab_errf(ctx, "invalid name: unexpected '.' (%.*s)", (int)len, str);
  4641. }
  4642. start = true;
  4643. } else if (start) {
  4644. if (!upb_isletter(c)) {
  4645. symtab_errf(
  4646. ctx,
  4647. "invalid name: path components must start with a letter (%.*s)",
  4648. (int)len, str);
  4649. }
  4650. start = false;
  4651. } else {
  4652. if (!upb_isalphanum(c)) {
  4653. symtab_errf(ctx, "invalid name: non-alphanumeric character (%.*s)",
  4654. (int)len, str);
  4655. }
  4656. }
  4657. }
  4658. if (start) {
  4659. symtab_errf(ctx, "invalid name: empty part (%.*s)", (int)len, str);
  4660. }
  4661. }
  4662. static size_t div_round_up(size_t n, size_t d) {
  4663. return (n + d - 1) / d;
  4664. }
  4665. static size_t upb_msgval_sizeof(upb_fieldtype_t type) {
  4666. switch (type) {
  4667. case UPB_TYPE_DOUBLE:
  4668. case UPB_TYPE_INT64:
  4669. case UPB_TYPE_UINT64:
  4670. return 8;
  4671. case UPB_TYPE_ENUM:
  4672. case UPB_TYPE_INT32:
  4673. case UPB_TYPE_UINT32:
  4674. case UPB_TYPE_FLOAT:
  4675. return 4;
  4676. case UPB_TYPE_BOOL:
  4677. return 1;
  4678. case UPB_TYPE_MESSAGE:
  4679. return sizeof(void*);
  4680. case UPB_TYPE_BYTES:
  4681. case UPB_TYPE_STRING:
  4682. return sizeof(upb_strview);
  4683. }
  4684. UPB_UNREACHABLE();
  4685. }
  4686. static uint8_t upb_msg_fielddefsize(const upb_fielddef *f) {
  4687. if (upb_msgdef_mapentry(upb_fielddef_containingtype(f))) {
  4688. upb_map_entry ent;
  4689. UPB_ASSERT(sizeof(ent.k) == sizeof(ent.v));
  4690. return sizeof(ent.k);
  4691. } else if (upb_fielddef_isseq(f)) {
  4692. return sizeof(void*);
  4693. } else {
  4694. return upb_msgval_sizeof(upb_fielddef_type(f));
  4695. }
  4696. }
  4697. static uint32_t upb_msglayout_place(upb_msglayout *l, size_t size) {
  4698. uint32_t ret;
  4699. l->size = UPB_ALIGN_UP(l->size, size);
  4700. ret = l->size;
  4701. l->size += size;
  4702. return ret;
  4703. }
  4704. static int field_number_cmp(const void *p1, const void *p2) {
  4705. const upb_msglayout_field *f1 = p1;
  4706. const upb_msglayout_field *f2 = p2;
  4707. return f1->number - f2->number;
  4708. }
  4709. static void assign_layout_indices(const upb_msgdef *m, upb_msglayout *l,
  4710. upb_msglayout_field *fields) {
  4711. int i;
  4712. int n = upb_msgdef_numfields(m);
  4713. int dense_below = 0;
  4714. for (i = 0; i < n; i++) {
  4715. upb_fielddef *f = (upb_fielddef*)upb_msgdef_itof(m, fields[i].number);
  4716. UPB_ASSERT(f);
  4717. f->layout_index = i;
  4718. if (i < UINT8_MAX && fields[i].number == i + 1 &&
  4719. (i == 0 || fields[i-1].number == i)) {
  4720. dense_below = i + 1;
  4721. }
  4722. }
  4723. l->dense_below = dense_below;
  4724. }
  4725. static void fill_fieldlayout(upb_msglayout_field *field, const upb_fielddef *f) {
  4726. field->number = upb_fielddef_number(f);
  4727. field->descriptortype = upb_fielddef_descriptortype(f);
  4728. if (field->descriptortype == UPB_DTYPE_STRING &&
  4729. f->file->syntax == UPB_SYNTAX_PROTO2) {
  4730. /* See TableDescriptorType() in upbc/generator.cc for details and
  4731. * rationale. */
  4732. field->descriptortype = UPB_DTYPE_BYTES;
  4733. }
  4734. if (upb_fielddef_ismap(f)) {
  4735. field->mode = _UPB_MODE_MAP;
  4736. } else if (upb_fielddef_isseq(f)) {
  4737. field->mode = _UPB_MODE_ARRAY;
  4738. } else {
  4739. field->mode = _UPB_MODE_SCALAR;
  4740. }
  4741. if (upb_fielddef_packed(f)) {
  4742. field->mode |= _UPB_MODE_IS_PACKED;
  4743. }
  4744. }
  4745. /* This function is the dynamic equivalent of message_layout.{cc,h} in upbc.
  4746. * It computes a dynamic layout for all of the fields in |m|. */
  4747. static void make_layout(symtab_addctx *ctx, const upb_msgdef *m) {
  4748. upb_msglayout *l = (upb_msglayout*)m->layout;
  4749. upb_msg_field_iter it;
  4750. upb_msg_oneof_iter oit;
  4751. size_t hasbit;
  4752. size_t field_count = upb_msgdef_numfields(m);
  4753. size_t submsg_count = 0;
  4754. const upb_msglayout **submsgs;
  4755. upb_msglayout_field *fields;
  4756. memset(l, 0, sizeof(*l) + sizeof(_upb_fasttable_entry));
  4757. /* Count sub-messages. */
  4758. for (size_t i = 0; i < field_count; i++) {
  4759. if (upb_fielddef_issubmsg(&m->fields[i])) {
  4760. submsg_count++;
  4761. }
  4762. }
  4763. fields = symtab_alloc(ctx, field_count * sizeof(*fields));
  4764. submsgs = symtab_alloc(ctx, submsg_count * sizeof(*submsgs));
  4765. l->field_count = upb_msgdef_numfields(m);
  4766. l->fields = fields;
  4767. l->submsgs = submsgs;
  4768. l->table_mask = 0;
  4769. /* TODO(haberman): initialize fast tables so that reflection-based parsing
  4770. * can get the same speeds as linked-in types. */
  4771. l->fasttable[0].field_parser = &fastdecode_generic;
  4772. l->fasttable[0].field_data = 0;
  4773. if (upb_msgdef_mapentry(m)) {
  4774. /* TODO(haberman): refactor this method so this special case is more
  4775. * elegant. */
  4776. const upb_fielddef *key = upb_msgdef_itof(m, 1);
  4777. const upb_fielddef *val = upb_msgdef_itof(m, 2);
  4778. fields[0].number = 1;
  4779. fields[1].number = 2;
  4780. fields[0].mode = _UPB_MODE_SCALAR;
  4781. fields[1].mode = _UPB_MODE_SCALAR;
  4782. fields[0].presence = 0;
  4783. fields[1].presence = 0;
  4784. fields[0].descriptortype = upb_fielddef_descriptortype(key);
  4785. fields[1].descriptortype = upb_fielddef_descriptortype(val);
  4786. fields[0].offset = 0;
  4787. fields[1].offset = sizeof(upb_strview);
  4788. fields[1].submsg_index = 0;
  4789. if (upb_fielddef_type(val) == UPB_TYPE_MESSAGE) {
  4790. submsgs[0] = upb_fielddef_msgsubdef(val)->layout;
  4791. }
  4792. l->field_count = 2;
  4793. l->size = 2 * sizeof(upb_strview);
  4794. l->size = UPB_ALIGN_UP(l->size, 8);
  4795. return;
  4796. }
  4797. /* Allocate data offsets in three stages:
  4798. *
  4799. * 1. hasbits.
  4800. * 2. regular fields.
  4801. * 3. oneof fields.
  4802. *
  4803. * OPT: There is a lot of room for optimization here to minimize the size.
  4804. */
  4805. /* Allocate hasbits and set basic field attributes. */
  4806. submsg_count = 0;
  4807. for (upb_msg_field_begin(&it, m), hasbit = 0;
  4808. !upb_msg_field_done(&it);
  4809. upb_msg_field_next(&it)) {
  4810. upb_fielddef* f = upb_msg_iter_field(&it);
  4811. upb_msglayout_field *field = &fields[upb_fielddef_index(f)];
  4812. fill_fieldlayout(field, f);
  4813. if (upb_fielddef_issubmsg(f)) {
  4814. const upb_msgdef *subm = upb_fielddef_msgsubdef(f);
  4815. field->submsg_index = submsg_count++;
  4816. submsgs[field->submsg_index] = subm->layout;
  4817. }
  4818. if (upb_fielddef_haspresence(f) && !upb_fielddef_realcontainingoneof(f)) {
  4819. /* We don't use hasbit 0, so that 0 can indicate "no presence" in the
  4820. * table. This wastes one hasbit, but we don't worry about it for now. */
  4821. field->presence = ++hasbit;
  4822. } else {
  4823. field->presence = 0;
  4824. }
  4825. }
  4826. /* Account for space used by hasbits. */
  4827. l->size = div_round_up(hasbit + 1, 8);
  4828. /* Allocate non-oneof fields. */
  4829. for (upb_msg_field_begin(&it, m); !upb_msg_field_done(&it);
  4830. upb_msg_field_next(&it)) {
  4831. const upb_fielddef* f = upb_msg_iter_field(&it);
  4832. size_t field_size = upb_msg_fielddefsize(f);
  4833. size_t index = upb_fielddef_index(f);
  4834. if (upb_fielddef_realcontainingoneof(f)) {
  4835. /* Oneofs are handled separately below. */
  4836. continue;
  4837. }
  4838. fields[index].offset = upb_msglayout_place(l, field_size);
  4839. }
  4840. /* Allocate oneof fields. Each oneof field consists of a uint32 for the case
  4841. * and space for the actual data. */
  4842. for (upb_msg_oneof_begin(&oit, m); !upb_msg_oneof_done(&oit);
  4843. upb_msg_oneof_next(&oit)) {
  4844. const upb_oneofdef* o = upb_msg_iter_oneof(&oit);
  4845. upb_oneof_iter fit;
  4846. size_t case_size = sizeof(uint32_t); /* Could potentially optimize this. */
  4847. size_t field_size = 0;
  4848. uint32_t case_offset;
  4849. uint32_t data_offset;
  4850. if (upb_oneofdef_issynthetic(o)) continue;
  4851. /* Calculate field size: the max of all field sizes. */
  4852. for (upb_oneof_begin(&fit, o);
  4853. !upb_oneof_done(&fit);
  4854. upb_oneof_next(&fit)) {
  4855. const upb_fielddef* f = upb_oneof_iter_field(&fit);
  4856. field_size = UPB_MAX(field_size, upb_msg_fielddefsize(f));
  4857. }
  4858. /* Align and allocate case offset. */
  4859. case_offset = upb_msglayout_place(l, case_size);
  4860. data_offset = upb_msglayout_place(l, field_size);
  4861. for (upb_oneof_begin(&fit, o);
  4862. !upb_oneof_done(&fit);
  4863. upb_oneof_next(&fit)) {
  4864. const upb_fielddef* f = upb_oneof_iter_field(&fit);
  4865. fields[upb_fielddef_index(f)].offset = data_offset;
  4866. fields[upb_fielddef_index(f)].presence = ~case_offset;
  4867. }
  4868. }
  4869. /* Size of the entire structure should be a multiple of its greatest
  4870. * alignment. TODO: track overall alignment for real? */
  4871. l->size = UPB_ALIGN_UP(l->size, 8);
  4872. /* Sort fields by number. */
  4873. qsort(fields, upb_msgdef_numfields(m), sizeof(*fields), field_number_cmp);
  4874. assign_layout_indices(m, l, fields);
  4875. }
  4876. static char *strviewdup(symtab_addctx *ctx, upb_strview view) {
  4877. return upb_strdup2(view.data, view.size, ctx->arena);
  4878. }
  4879. static bool streql2(const char *a, size_t n, const char *b) {
  4880. return n == strlen(b) && memcmp(a, b, n) == 0;
  4881. }
  4882. static bool streql_view(upb_strview view, const char *b) {
  4883. return streql2(view.data, view.size, b);
  4884. }
  4885. static const char *makefullname(symtab_addctx *ctx, const char *prefix,
  4886. upb_strview name) {
  4887. if (prefix) {
  4888. /* ret = prefix + '.' + name; */
  4889. size_t n = strlen(prefix);
  4890. char *ret = symtab_alloc(ctx, n + name.size + 2);
  4891. strcpy(ret, prefix);
  4892. ret[n] = '.';
  4893. memcpy(&ret[n + 1], name.data, name.size);
  4894. ret[n + 1 + name.size] = '\0';
  4895. return ret;
  4896. } else {
  4897. return strviewdup(ctx, name);
  4898. }
  4899. }
  4900. static void finalize_oneofs(symtab_addctx *ctx, upb_msgdef *m) {
  4901. int i;
  4902. int synthetic_count = 0;
  4903. upb_oneofdef *mutable_oneofs = (upb_oneofdef*)m->oneofs;
  4904. for (i = 0; i < m->oneof_count; i++) {
  4905. upb_oneofdef *o = &mutable_oneofs[i];
  4906. if (o->synthetic && o->field_count != 1) {
  4907. symtab_errf(ctx, "Synthetic oneofs must have one field, not %d: %s",
  4908. o->field_count, upb_oneofdef_name(o));
  4909. }
  4910. if (o->synthetic) {
  4911. synthetic_count++;
  4912. } else if (synthetic_count != 0) {
  4913. symtab_errf(ctx, "Synthetic oneofs must be after all other oneofs: %s",
  4914. upb_oneofdef_name(o));
  4915. }
  4916. o->fields = symtab_alloc(ctx, sizeof(upb_fielddef *) * o->field_count);
  4917. o->field_count = 0;
  4918. }
  4919. for (i = 0; i < m->field_count; i++) {
  4920. const upb_fielddef *f = &m->fields[i];
  4921. upb_oneofdef *o = (upb_oneofdef*)f->oneof;
  4922. if (o) {
  4923. o->fields[o->field_count++] = f;
  4924. }
  4925. }
  4926. m->real_oneof_count = m->oneof_count - synthetic_count;
  4927. }
  4928. size_t getjsonname(const char *name, char *buf, size_t len) {
  4929. size_t src, dst = 0;
  4930. bool ucase_next = false;
  4931. #define WRITE(byte) \
  4932. ++dst; \
  4933. if (dst < len) buf[dst - 1] = byte; \
  4934. else if (dst == len) buf[dst - 1] = '\0'
  4935. if (!name) {
  4936. WRITE('\0');
  4937. return 0;
  4938. }
  4939. /* Implement the transformation as described in the spec:
  4940. * 1. upper case all letters after an underscore.
  4941. * 2. remove all underscores.
  4942. */
  4943. for (src = 0; name[src]; src++) {
  4944. if (name[src] == '_') {
  4945. ucase_next = true;
  4946. continue;
  4947. }
  4948. if (ucase_next) {
  4949. WRITE(toupper(name[src]));
  4950. ucase_next = false;
  4951. } else {
  4952. WRITE(name[src]);
  4953. }
  4954. }
  4955. WRITE('\0');
  4956. return dst;
  4957. #undef WRITE
  4958. }
  4959. static char* makejsonname(symtab_addctx *ctx, const char* name) {
  4960. size_t size = getjsonname(name, NULL, 0);
  4961. char* json_name = symtab_alloc(ctx, size);
  4962. getjsonname(name, json_name, size);
  4963. return json_name;
  4964. }
  4965. static void symtab_add(symtab_addctx *ctx, const char *name, upb_value v) {
  4966. if (upb_strtable_lookup(&ctx->symtab->syms, name, NULL)) {
  4967. symtab_errf(ctx, "duplicate symbol '%s'", name);
  4968. }
  4969. size_t len = strlen(name);
  4970. CHK_OOM(upb_strtable_insert(&ctx->symtab->syms, name, len, v,
  4971. ctx->symtab->arena));
  4972. }
  4973. /* Given a symbol and the base symbol inside which it is defined, find the
  4974. * symbol's definition in t. */
  4975. static const void *symtab_resolve(symtab_addctx *ctx, const upb_fielddef *f,
  4976. const char *base, upb_strview sym,
  4977. upb_deftype_t type) {
  4978. const upb_strtable *t = &ctx->symtab->syms;
  4979. if(sym.size == 0) goto notfound;
  4980. if(sym.data[0] == '.') {
  4981. /* Symbols starting with '.' are absolute, so we do a single lookup.
  4982. * Slice to omit the leading '.' */
  4983. upb_value v;
  4984. if (!upb_strtable_lookup2(t, sym.data + 1, sym.size - 1, &v)) {
  4985. goto notfound;
  4986. }
  4987. const void *ret = unpack_def(v, type);
  4988. if (!ret) {
  4989. symtab_errf(ctx, "type mismatch when resolving field %s, name %s",
  4990. f->full_name, sym.data);
  4991. }
  4992. return ret;
  4993. } else {
  4994. /* Remove components from base until we find an entry or run out.
  4995. * TODO: This branch is totally broken, but currently not used. */
  4996. (void)base;
  4997. UPB_ASSERT(false);
  4998. goto notfound;
  4999. }
  5000. notfound:
  5001. symtab_errf(ctx, "couldn't resolve name '" UPB_STRVIEW_FORMAT "'",
  5002. UPB_STRVIEW_ARGS(sym));
  5003. }
  5004. static void create_oneofdef(
  5005. symtab_addctx *ctx, upb_msgdef *m,
  5006. const google_protobuf_OneofDescriptorProto *oneof_proto) {
  5007. upb_oneofdef *o;
  5008. upb_strview name = google_protobuf_OneofDescriptorProto_name(oneof_proto);
  5009. upb_value v;
  5010. o = (upb_oneofdef*)&m->oneofs[m->oneof_count++];
  5011. o->parent = m;
  5012. o->full_name = makefullname(ctx, m->full_name, name);
  5013. o->field_count = 0;
  5014. o->synthetic = false;
  5015. v = pack_def(o, UPB_DEFTYPE_ONEOF);
  5016. symtab_add(ctx, o->full_name, v);
  5017. CHK_OOM(upb_strtable_insert(&m->ntof, name.data, name.size, v, ctx->arena));
  5018. CHK_OOM(upb_inttable_init(&o->itof, ctx->arena));
  5019. CHK_OOM(upb_strtable_init(&o->ntof, 4, ctx->arena));
  5020. }
  5021. static str_t *newstr(symtab_addctx *ctx, const char *data, size_t len) {
  5022. str_t *ret = symtab_alloc(ctx, sizeof(*ret) + len);
  5023. if (!ret) return NULL;
  5024. ret->len = len;
  5025. if (len) memcpy(ret->str, data, len);
  5026. ret->str[len] = '\0';
  5027. return ret;
  5028. }
  5029. static void parse_default(symtab_addctx *ctx, const char *str, size_t len,
  5030. upb_fielddef *f) {
  5031. char *end;
  5032. char nullz[64];
  5033. errno = 0;
  5034. switch (upb_fielddef_type(f)) {
  5035. case UPB_TYPE_INT32:
  5036. case UPB_TYPE_INT64:
  5037. case UPB_TYPE_UINT32:
  5038. case UPB_TYPE_UINT64:
  5039. case UPB_TYPE_DOUBLE:
  5040. case UPB_TYPE_FLOAT:
  5041. /* Standard C number parsing functions expect null-terminated strings. */
  5042. if (len >= sizeof(nullz) - 1) {
  5043. symtab_errf(ctx, "Default too long: %.*s", (int)len, str);
  5044. }
  5045. memcpy(nullz, str, len);
  5046. nullz[len] = '\0';
  5047. str = nullz;
  5048. break;
  5049. default:
  5050. break;
  5051. }
  5052. switch (upb_fielddef_type(f)) {
  5053. case UPB_TYPE_INT32: {
  5054. long val = strtol(str, &end, 0);
  5055. if (val > INT32_MAX || val < INT32_MIN || errno == ERANGE || *end) {
  5056. goto invalid;
  5057. }
  5058. f->defaultval.sint = val;
  5059. break;
  5060. }
  5061. case UPB_TYPE_ENUM: {
  5062. const upb_enumdef *e = f->sub.enumdef;
  5063. int32_t val;
  5064. if (!upb_enumdef_ntoi(e, str, len, &val)) {
  5065. goto invalid;
  5066. }
  5067. f->defaultval.sint = val;
  5068. break;
  5069. }
  5070. case UPB_TYPE_INT64: {
  5071. long long val = strtoll(str, &end, 0);
  5072. if (val > INT64_MAX || val < INT64_MIN || errno == ERANGE || *end) {
  5073. goto invalid;
  5074. }
  5075. f->defaultval.sint = val;
  5076. break;
  5077. }
  5078. case UPB_TYPE_UINT32: {
  5079. unsigned long val = strtoul(str, &end, 0);
  5080. if (val > UINT32_MAX || errno == ERANGE || *end) {
  5081. goto invalid;
  5082. }
  5083. f->defaultval.uint = val;
  5084. break;
  5085. }
  5086. case UPB_TYPE_UINT64: {
  5087. unsigned long long val = strtoull(str, &end, 0);
  5088. if (val > UINT64_MAX || errno == ERANGE || *end) {
  5089. goto invalid;
  5090. }
  5091. f->defaultval.uint = val;
  5092. break;
  5093. }
  5094. case UPB_TYPE_DOUBLE: {
  5095. double val = strtod(str, &end);
  5096. if (errno == ERANGE || *end) {
  5097. goto invalid;
  5098. }
  5099. f->defaultval.dbl = val;
  5100. break;
  5101. }
  5102. case UPB_TYPE_FLOAT: {
  5103. float val = strtof(str, &end);
  5104. if (errno == ERANGE || *end) {
  5105. goto invalid;
  5106. }
  5107. f->defaultval.flt = val;
  5108. break;
  5109. }
  5110. case UPB_TYPE_BOOL: {
  5111. if (streql2(str, len, "false")) {
  5112. f->defaultval.boolean = false;
  5113. } else if (streql2(str, len, "true")) {
  5114. f->defaultval.boolean = true;
  5115. } else {
  5116. }
  5117. break;
  5118. }
  5119. case UPB_TYPE_STRING:
  5120. f->defaultval.str = newstr(ctx, str, len);
  5121. break;
  5122. case UPB_TYPE_BYTES:
  5123. /* XXX: need to interpret the C-escaped value. */
  5124. f->defaultval.str = newstr(ctx, str, len);
  5125. break;
  5126. case UPB_TYPE_MESSAGE:
  5127. /* Should not have a default value. */
  5128. symtab_errf(ctx, "Message should not have a default (%s)",
  5129. upb_fielddef_fullname(f));
  5130. }
  5131. return;
  5132. invalid:
  5133. symtab_errf(ctx, "Invalid default '%.*s' for field %s", (int)len, str,
  5134. upb_fielddef_fullname(f));
  5135. }
  5136. static void set_default_default(symtab_addctx *ctx, upb_fielddef *f) {
  5137. switch (upb_fielddef_type(f)) {
  5138. case UPB_TYPE_INT32:
  5139. case UPB_TYPE_INT64:
  5140. case UPB_TYPE_ENUM:
  5141. f->defaultval.sint = 0;
  5142. break;
  5143. case UPB_TYPE_UINT64:
  5144. case UPB_TYPE_UINT32:
  5145. f->defaultval.uint = 0;
  5146. break;
  5147. case UPB_TYPE_DOUBLE:
  5148. case UPB_TYPE_FLOAT:
  5149. f->defaultval.dbl = 0;
  5150. break;
  5151. case UPB_TYPE_STRING:
  5152. case UPB_TYPE_BYTES:
  5153. f->defaultval.str = newstr(ctx, NULL, 0);
  5154. break;
  5155. case UPB_TYPE_BOOL:
  5156. f->defaultval.boolean = false;
  5157. break;
  5158. case UPB_TYPE_MESSAGE:
  5159. break;
  5160. }
  5161. }
  5162. static void create_fielddef(
  5163. symtab_addctx *ctx, const char *prefix, upb_msgdef *m,
  5164. const google_protobuf_FieldDescriptorProto *field_proto) {
  5165. upb_fielddef *f;
  5166. const google_protobuf_FieldOptions *options;
  5167. upb_strview name;
  5168. const char *full_name;
  5169. const char *json_name;
  5170. const char *shortname;
  5171. uint32_t field_number;
  5172. if (!google_protobuf_FieldDescriptorProto_has_name(field_proto)) {
  5173. symtab_errf(ctx, "field has no name (%s)", upb_msgdef_fullname(m));
  5174. }
  5175. name = google_protobuf_FieldDescriptorProto_name(field_proto);
  5176. check_ident(ctx, name, false);
  5177. full_name = makefullname(ctx, prefix, name);
  5178. shortname = shortdefname(full_name);
  5179. if (google_protobuf_FieldDescriptorProto_has_json_name(field_proto)) {
  5180. json_name = strviewdup(
  5181. ctx, google_protobuf_FieldDescriptorProto_json_name(field_proto));
  5182. } else {
  5183. json_name = makejsonname(ctx, shortname);
  5184. }
  5185. field_number = google_protobuf_FieldDescriptorProto_number(field_proto);
  5186. if (field_number == 0 || field_number > UPB_MAX_FIELDNUMBER) {
  5187. symtab_errf(ctx, "invalid field number (%u)", field_number);
  5188. }
  5189. if (m) {
  5190. /* direct message field. */
  5191. upb_value v, field_v, json_v;
  5192. size_t json_size;
  5193. f = (upb_fielddef*)&m->fields[m->field_count];
  5194. f->index_ = m->field_count++;
  5195. f->msgdef = m;
  5196. f->is_extension_ = false;
  5197. if (upb_strtable_lookup(&m->ntof, shortname, NULL)) {
  5198. symtab_errf(ctx, "duplicate field name (%s)", shortname);
  5199. }
  5200. if (upb_strtable_lookup(&m->ntof, json_name, NULL)) {
  5201. symtab_errf(ctx, "duplicate json_name (%s)", json_name);
  5202. }
  5203. if (upb_inttable_lookup(&m->itof, field_number, NULL)) {
  5204. symtab_errf(ctx, "duplicate field number (%u)", field_number);
  5205. }
  5206. field_v = pack_def(f, UPB_DEFTYPE_FIELD);
  5207. json_v = pack_def(f, UPB_DEFTYPE_FIELD_JSONNAME);
  5208. v = upb_value_constptr(f);
  5209. json_size = strlen(json_name);
  5210. CHK_OOM(upb_strtable_insert(&m->ntof, name.data, name.size, field_v,
  5211. ctx->arena));
  5212. CHK_OOM(upb_inttable_insert(&m->itof, field_number, v, ctx->arena));
  5213. if (strcmp(shortname, json_name) != 0) {
  5214. upb_strtable_insert(&m->ntof, json_name, json_size, json_v, ctx->arena);
  5215. }
  5216. if (ctx->layouts) {
  5217. const upb_msglayout_field *fields = m->layout->fields;
  5218. int count = m->layout->field_count;
  5219. bool found = false;
  5220. int i;
  5221. for (i = 0; i < count; i++) {
  5222. if (fields[i].number == field_number) {
  5223. f->layout_index = i;
  5224. found = true;
  5225. break;
  5226. }
  5227. }
  5228. UPB_ASSERT(found);
  5229. }
  5230. } else {
  5231. /* extension field. */
  5232. f = (upb_fielddef*)&ctx->file->exts[ctx->file->ext_count++];
  5233. f->is_extension_ = true;
  5234. symtab_add(ctx, full_name, pack_def(f, UPB_DEFTYPE_FIELD));
  5235. }
  5236. f->full_name = full_name;
  5237. f->json_name = json_name;
  5238. f->file = ctx->file;
  5239. f->type_ = (int)google_protobuf_FieldDescriptorProto_type(field_proto);
  5240. f->label_ = (int)google_protobuf_FieldDescriptorProto_label(field_proto);
  5241. f->number_ = field_number;
  5242. f->oneof = NULL;
  5243. f->proto3_optional_ =
  5244. google_protobuf_FieldDescriptorProto_proto3_optional(field_proto);
  5245. /* We can't resolve the subdef or (in the case of extensions) the containing
  5246. * message yet, because it may not have been defined yet. We stash a pointer
  5247. * to the field_proto until later when we can properly resolve it. */
  5248. f->sub.unresolved = field_proto;
  5249. if (f->label_ == UPB_LABEL_REQUIRED && f->file->syntax == UPB_SYNTAX_PROTO3) {
  5250. symtab_errf(ctx, "proto3 fields cannot be required (%s)", f->full_name);
  5251. }
  5252. if (google_protobuf_FieldDescriptorProto_has_oneof_index(field_proto)) {
  5253. int oneof_index =
  5254. google_protobuf_FieldDescriptorProto_oneof_index(field_proto);
  5255. upb_oneofdef *oneof;
  5256. upb_value v = upb_value_constptr(f);
  5257. if (upb_fielddef_label(f) != UPB_LABEL_OPTIONAL) {
  5258. symtab_errf(ctx, "fields in oneof must have OPTIONAL label (%s)",
  5259. f->full_name);
  5260. }
  5261. if (!m) {
  5262. symtab_errf(ctx, "oneof_index provided for extension field (%s)",
  5263. f->full_name);
  5264. }
  5265. if (oneof_index >= m->oneof_count) {
  5266. symtab_errf(ctx, "oneof_index out of range (%s)", f->full_name);
  5267. }
  5268. oneof = (upb_oneofdef *)&m->oneofs[oneof_index];
  5269. f->oneof = oneof;
  5270. oneof->field_count++;
  5271. if (f->proto3_optional_) {
  5272. oneof->synthetic = true;
  5273. }
  5274. CHK_OOM(upb_inttable_insert(&oneof->itof, f->number_, v, ctx->arena));
  5275. CHK_OOM(
  5276. upb_strtable_insert(&oneof->ntof, name.data, name.size, v, ctx->arena));
  5277. } else {
  5278. f->oneof = NULL;
  5279. if (f->proto3_optional_) {
  5280. symtab_errf(ctx, "field with proto3_optional was not in a oneof (%s)",
  5281. f->full_name);
  5282. }
  5283. }
  5284. options = google_protobuf_FieldDescriptorProto_has_options(field_proto) ?
  5285. google_protobuf_FieldDescriptorProto_options(field_proto) : NULL;
  5286. if (options && google_protobuf_FieldOptions_has_packed(options)) {
  5287. f->packed_ = google_protobuf_FieldOptions_packed(options);
  5288. } else {
  5289. /* Repeated fields default to packed for proto3 only. */
  5290. f->packed_ = upb_fielddef_isprimitive(f) &&
  5291. f->label_ == UPB_LABEL_REPEATED && f->file->syntax == UPB_SYNTAX_PROTO3;
  5292. }
  5293. if (options) {
  5294. f->lazy_ = google_protobuf_FieldOptions_lazy(options);
  5295. } else {
  5296. f->lazy_ = false;
  5297. }
  5298. }
  5299. static void create_enumdef(
  5300. symtab_addctx *ctx, const char *prefix,
  5301. const google_protobuf_EnumDescriptorProto *enum_proto) {
  5302. upb_enumdef *e;
  5303. const google_protobuf_EnumValueDescriptorProto *const *values;
  5304. upb_strview name;
  5305. size_t i, n;
  5306. name = google_protobuf_EnumDescriptorProto_name(enum_proto);
  5307. check_ident(ctx, name, false);
  5308. e = (upb_enumdef*)&ctx->file->enums[ctx->file->enum_count++];
  5309. e->full_name = makefullname(ctx, prefix, name);
  5310. symtab_add(ctx, e->full_name, pack_def(e, UPB_DEFTYPE_ENUM));
  5311. values = google_protobuf_EnumDescriptorProto_value(enum_proto, &n);
  5312. CHK_OOM(upb_strtable_init(&e->ntoi, n, ctx->arena));
  5313. CHK_OOM(upb_inttable_init(&e->iton, ctx->arena));
  5314. e->file = ctx->file;
  5315. e->defaultval = 0;
  5316. if (n == 0) {
  5317. symtab_errf(ctx, "enums must contain at least one value (%s)",
  5318. e->full_name);
  5319. }
  5320. for (i = 0; i < n; i++) {
  5321. const google_protobuf_EnumValueDescriptorProto *value = values[i];
  5322. upb_strview name = google_protobuf_EnumValueDescriptorProto_name(value);
  5323. char *name2 = strviewdup(ctx, name);
  5324. int32_t num = google_protobuf_EnumValueDescriptorProto_number(value);
  5325. upb_value v = upb_value_int32(num);
  5326. if (i == 0 && e->file->syntax == UPB_SYNTAX_PROTO3 && num != 0) {
  5327. symtab_errf(ctx, "for proto3, the first enum value must be zero (%s)",
  5328. e->full_name);
  5329. }
  5330. if (upb_strtable_lookup(&e->ntoi, name2, NULL)) {
  5331. symtab_errf(ctx, "duplicate enum label '%s'", name2);
  5332. }
  5333. CHK_OOM(name2)
  5334. CHK_OOM(upb_strtable_insert(&e->ntoi, name2, strlen(name2), v, ctx->arena));
  5335. if (!upb_inttable_lookup(&e->iton, num, NULL)) {
  5336. upb_value v = upb_value_cstr(name2);
  5337. CHK_OOM(upb_inttable_insert(&e->iton, num, v, ctx->arena));
  5338. }
  5339. }
  5340. upb_inttable_compact(&e->iton, ctx->arena);
  5341. }
  5342. static void create_msgdef(symtab_addctx *ctx, const char *prefix,
  5343. const google_protobuf_DescriptorProto *msg_proto) {
  5344. upb_msgdef *m;
  5345. const google_protobuf_MessageOptions *options;
  5346. const google_protobuf_OneofDescriptorProto *const *oneofs;
  5347. const google_protobuf_FieldDescriptorProto *const *fields;
  5348. const google_protobuf_EnumDescriptorProto *const *enums;
  5349. const google_protobuf_DescriptorProto *const *msgs;
  5350. size_t i, n_oneof, n_field, n;
  5351. upb_strview name;
  5352. name = google_protobuf_DescriptorProto_name(msg_proto);
  5353. check_ident(ctx, name, false);
  5354. m = (upb_msgdef*)&ctx->file->msgs[ctx->file->msg_count++];
  5355. m->full_name = makefullname(ctx, prefix, name);
  5356. symtab_add(ctx, m->full_name, pack_def(m, UPB_DEFTYPE_MSG));
  5357. oneofs = google_protobuf_DescriptorProto_oneof_decl(msg_proto, &n_oneof);
  5358. fields = google_protobuf_DescriptorProto_field(msg_proto, &n_field);
  5359. CHK_OOM(upb_inttable_init(&m->itof, ctx->arena));
  5360. CHK_OOM(upb_strtable_init(&m->ntof, n_oneof + n_field, ctx->arena));
  5361. m->file = ctx->file;
  5362. m->map_entry = false;
  5363. options = google_protobuf_DescriptorProto_options(msg_proto);
  5364. if (options) {
  5365. m->map_entry = google_protobuf_MessageOptions_map_entry(options);
  5366. }
  5367. if (ctx->layouts) {
  5368. m->layout = *ctx->layouts;
  5369. ctx->layouts++;
  5370. } else {
  5371. /* Allocate now (to allow cross-linking), populate later. */
  5372. m->layout = symtab_alloc(
  5373. ctx, sizeof(*m->layout) + sizeof(_upb_fasttable_entry));
  5374. }
  5375. m->oneof_count = 0;
  5376. m->oneofs = symtab_alloc(ctx, sizeof(*m->oneofs) * n_oneof);
  5377. for (i = 0; i < n_oneof; i++) {
  5378. create_oneofdef(ctx, m, oneofs[i]);
  5379. }
  5380. m->field_count = 0;
  5381. m->fields = symtab_alloc(ctx, sizeof(*m->fields) * n_field);
  5382. for (i = 0; i < n_field; i++) {
  5383. create_fielddef(ctx, m->full_name, m, fields[i]);
  5384. }
  5385. finalize_oneofs(ctx, m);
  5386. assign_msg_wellknowntype(m);
  5387. upb_inttable_compact(&m->itof, ctx->arena);
  5388. /* This message is built. Now build nested messages and enums. */
  5389. enums = google_protobuf_DescriptorProto_enum_type(msg_proto, &n);
  5390. for (i = 0; i < n; i++) {
  5391. create_enumdef(ctx, m->full_name, enums[i]);
  5392. }
  5393. msgs = google_protobuf_DescriptorProto_nested_type(msg_proto, &n);
  5394. for (i = 0; i < n; i++) {
  5395. create_msgdef(ctx, m->full_name, msgs[i]);
  5396. }
  5397. }
  5398. static void count_types_in_msg(const google_protobuf_DescriptorProto *msg_proto,
  5399. upb_filedef *file) {
  5400. const google_protobuf_DescriptorProto *const *msgs;
  5401. size_t i, n;
  5402. file->msg_count++;
  5403. msgs = google_protobuf_DescriptorProto_nested_type(msg_proto, &n);
  5404. for (i = 0; i < n; i++) {
  5405. count_types_in_msg(msgs[i], file);
  5406. }
  5407. google_protobuf_DescriptorProto_enum_type(msg_proto, &n);
  5408. file->enum_count += n;
  5409. google_protobuf_DescriptorProto_extension(msg_proto, &n);
  5410. file->ext_count += n;
  5411. }
  5412. static void count_types_in_file(
  5413. const google_protobuf_FileDescriptorProto *file_proto,
  5414. upb_filedef *file) {
  5415. const google_protobuf_DescriptorProto *const *msgs;
  5416. size_t i, n;
  5417. msgs = google_protobuf_FileDescriptorProto_message_type(file_proto, &n);
  5418. for (i = 0; i < n; i++) {
  5419. count_types_in_msg(msgs[i], file);
  5420. }
  5421. google_protobuf_FileDescriptorProto_enum_type(file_proto, &n);
  5422. file->enum_count += n;
  5423. google_protobuf_FileDescriptorProto_extension(file_proto, &n);
  5424. file->ext_count += n;
  5425. }
  5426. static void resolve_fielddef(symtab_addctx *ctx, const char *prefix,
  5427. upb_fielddef *f) {
  5428. upb_strview name;
  5429. const google_protobuf_FieldDescriptorProto *field_proto = f->sub.unresolved;
  5430. if (f->is_extension_) {
  5431. if (!google_protobuf_FieldDescriptorProto_has_extendee(field_proto)) {
  5432. symtab_errf(ctx, "extension for field '%s' had no extendee",
  5433. f->full_name);
  5434. }
  5435. name = google_protobuf_FieldDescriptorProto_extendee(field_proto);
  5436. f->msgdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_MSG);
  5437. }
  5438. if ((upb_fielddef_issubmsg(f) || f->type_ == UPB_DESCRIPTOR_TYPE_ENUM) &&
  5439. !google_protobuf_FieldDescriptorProto_has_type_name(field_proto)) {
  5440. symtab_errf(ctx, "field '%s' is missing type name", f->full_name);
  5441. }
  5442. name = google_protobuf_FieldDescriptorProto_type_name(field_proto);
  5443. if (upb_fielddef_issubmsg(f)) {
  5444. f->sub.msgdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_MSG);
  5445. } else if (f->type_ == UPB_DESCRIPTOR_TYPE_ENUM) {
  5446. f->sub.enumdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_ENUM);
  5447. }
  5448. /* Have to delay resolving of the default value until now because of the enum
  5449. * case, since enum defaults are specified with a label. */
  5450. if (google_protobuf_FieldDescriptorProto_has_default_value(field_proto)) {
  5451. upb_strview defaultval =
  5452. google_protobuf_FieldDescriptorProto_default_value(field_proto);
  5453. if (f->file->syntax == UPB_SYNTAX_PROTO3) {
  5454. symtab_errf(ctx, "proto3 fields cannot have explicit defaults (%s)",
  5455. f->full_name);
  5456. }
  5457. if (upb_fielddef_issubmsg(f)) {
  5458. symtab_errf(ctx, "message fields cannot have explicit defaults (%s)",
  5459. f->full_name);
  5460. }
  5461. parse_default(ctx, defaultval.data, defaultval.size, f);
  5462. } else {
  5463. set_default_default(ctx, f);
  5464. }
  5465. }
  5466. static void build_filedef(
  5467. symtab_addctx *ctx, upb_filedef *file,
  5468. const google_protobuf_FileDescriptorProto *file_proto) {
  5469. const google_protobuf_FileOptions *file_options_proto;
  5470. const google_protobuf_DescriptorProto *const *msgs;
  5471. const google_protobuf_EnumDescriptorProto *const *enums;
  5472. const google_protobuf_FieldDescriptorProto *const *exts;
  5473. const upb_strview* strs;
  5474. size_t i, n;
  5475. file->symtab = ctx->symtab;
  5476. /* One pass to count and allocate. */
  5477. file->msg_count = 0;
  5478. file->enum_count = 0;
  5479. file->ext_count = 0;
  5480. count_types_in_file(file_proto, file);
  5481. file->msgs = symtab_alloc(ctx, sizeof(*file->msgs) * file->msg_count);
  5482. file->enums = symtab_alloc(ctx, sizeof(*file->enums) * file->enum_count);
  5483. file->exts = symtab_alloc(ctx, sizeof(*file->exts) * file->ext_count);
  5484. /* In the second pass we increment these as defs are added. */
  5485. file->msg_count = 0;
  5486. file->enum_count = 0;
  5487. file->ext_count = 0;
  5488. if (!google_protobuf_FileDescriptorProto_has_name(file_proto)) {
  5489. symtab_errf(ctx, "File has no name");
  5490. }
  5491. file->name =
  5492. strviewdup(ctx, google_protobuf_FileDescriptorProto_name(file_proto));
  5493. file->phpprefix = NULL;
  5494. file->phpnamespace = NULL;
  5495. if (google_protobuf_FileDescriptorProto_has_package(file_proto)) {
  5496. upb_strview package =
  5497. google_protobuf_FileDescriptorProto_package(file_proto);
  5498. check_ident(ctx, package, true);
  5499. file->package = strviewdup(ctx, package);
  5500. } else {
  5501. file->package = NULL;
  5502. }
  5503. if (google_protobuf_FileDescriptorProto_has_syntax(file_proto)) {
  5504. upb_strview syntax =
  5505. google_protobuf_FileDescriptorProto_syntax(file_proto);
  5506. if (streql_view(syntax, "proto2")) {
  5507. file->syntax = UPB_SYNTAX_PROTO2;
  5508. } else if (streql_view(syntax, "proto3")) {
  5509. file->syntax = UPB_SYNTAX_PROTO3;
  5510. } else {
  5511. symtab_errf(ctx, "Invalid syntax '" UPB_STRVIEW_FORMAT "'",
  5512. UPB_STRVIEW_ARGS(syntax));
  5513. }
  5514. } else {
  5515. file->syntax = UPB_SYNTAX_PROTO2;
  5516. }
  5517. /* Read options. */
  5518. file_options_proto = google_protobuf_FileDescriptorProto_options(file_proto);
  5519. if (file_options_proto) {
  5520. if (google_protobuf_FileOptions_has_php_class_prefix(file_options_proto)) {
  5521. file->phpprefix = strviewdup(
  5522. ctx,
  5523. google_protobuf_FileOptions_php_class_prefix(file_options_proto));
  5524. }
  5525. if (google_protobuf_FileOptions_has_php_namespace(file_options_proto)) {
  5526. file->phpnamespace = strviewdup(
  5527. ctx, google_protobuf_FileOptions_php_namespace(file_options_proto));
  5528. }
  5529. }
  5530. /* Verify dependencies. */
  5531. strs = google_protobuf_FileDescriptorProto_dependency(file_proto, &n);
  5532. file->deps = symtab_alloc(ctx, sizeof(*file->deps) * n);
  5533. for (i = 0; i < n; i++) {
  5534. upb_strview dep_name = strs[i];
  5535. upb_value v;
  5536. if (!upb_strtable_lookup2(&ctx->symtab->files, dep_name.data,
  5537. dep_name.size, &v)) {
  5538. symtab_errf(ctx,
  5539. "Depends on file '" UPB_STRVIEW_FORMAT
  5540. "', but it has not been loaded",
  5541. UPB_STRVIEW_ARGS(dep_name));
  5542. }
  5543. file->deps[i] = upb_value_getconstptr(v);
  5544. }
  5545. /* Create messages. */
  5546. msgs = google_protobuf_FileDescriptorProto_message_type(file_proto, &n);
  5547. for (i = 0; i < n; i++) {
  5548. create_msgdef(ctx, file->package, msgs[i]);
  5549. }
  5550. /* Create enums. */
  5551. enums = google_protobuf_FileDescriptorProto_enum_type(file_proto, &n);
  5552. for (i = 0; i < n; i++) {
  5553. create_enumdef(ctx, file->package, enums[i]);
  5554. }
  5555. /* Create extensions. */
  5556. exts = google_protobuf_FileDescriptorProto_extension(file_proto, &n);
  5557. file->exts = symtab_alloc(ctx, sizeof(*file->exts) * n);
  5558. for (i = 0; i < n; i++) {
  5559. create_fielddef(ctx, file->package, NULL, exts[i]);
  5560. }
  5561. /* Now that all names are in the table, build layouts and resolve refs. */
  5562. for (i = 0; i < (size_t)file->ext_count; i++) {
  5563. resolve_fielddef(ctx, file->package, (upb_fielddef*)&file->exts[i]);
  5564. }
  5565. for (i = 0; i < (size_t)file->msg_count; i++) {
  5566. const upb_msgdef *m = &file->msgs[i];
  5567. int j;
  5568. for (j = 0; j < m->field_count; j++) {
  5569. resolve_fielddef(ctx, m->full_name, (upb_fielddef*)&m->fields[j]);
  5570. }
  5571. }
  5572. if (!ctx->layouts) {
  5573. for (i = 0; i < (size_t)file->msg_count; i++) {
  5574. const upb_msgdef *m = &file->msgs[i];
  5575. make_layout(ctx, m);
  5576. }
  5577. }
  5578. }
  5579. static void remove_filedef(upb_symtab *s, upb_filedef *file) {
  5580. int i;
  5581. for (i = 0; i < file->msg_count; i++) {
  5582. const char *name = file->msgs[i].full_name;
  5583. upb_strtable_remove(&s->syms, name, strlen(name), NULL);
  5584. }
  5585. for (i = 0; i < file->enum_count; i++) {
  5586. const char *name = file->enums[i].full_name;
  5587. upb_strtable_remove(&s->syms, name, strlen(name), NULL);
  5588. }
  5589. for (i = 0; i < file->ext_count; i++) {
  5590. const char *name = file->exts[i].full_name;
  5591. upb_strtable_remove(&s->syms, name, strlen(name), NULL);
  5592. }
  5593. }
  5594. static const upb_filedef *_upb_symtab_addfile(
  5595. upb_symtab *s, const google_protobuf_FileDescriptorProto *file_proto,
  5596. const upb_msglayout **layouts, upb_status *status) {
  5597. symtab_addctx ctx;
  5598. upb_strview name = google_protobuf_FileDescriptorProto_name(file_proto);
  5599. if (upb_strtable_lookup2(&s->files, name.data, name.size, NULL)) {
  5600. upb_status_seterrf(status, "duplicate file name (%.*s)",
  5601. UPB_STRVIEW_ARGS(name));
  5602. return NULL;
  5603. }
  5604. ctx.symtab = s;
  5605. ctx.layouts = layouts;
  5606. ctx.status = status;
  5607. ctx.file = NULL;
  5608. ctx.arena = upb_arena_new();
  5609. if (!ctx.arena) {
  5610. upb_status_setoom(status);
  5611. return NULL;
  5612. }
  5613. if (UPB_UNLIKELY(UPB_SETJMP(ctx.err))) {
  5614. UPB_ASSERT(!upb_ok(status));
  5615. if (ctx.file) {
  5616. remove_filedef(s, ctx.file);
  5617. ctx.file = NULL;
  5618. }
  5619. } else {
  5620. ctx.file = symtab_alloc(&ctx, sizeof(*ctx.file));
  5621. build_filedef(&ctx, ctx.file, file_proto);
  5622. upb_strtable_insert(&s->files, name.data, name.size,
  5623. upb_value_constptr(ctx.file), ctx.arena);
  5624. UPB_ASSERT(upb_ok(status));
  5625. upb_arena_fuse(s->arena, ctx.arena);
  5626. }
  5627. upb_arena_free(ctx.arena);
  5628. return ctx.file;
  5629. }
  5630. const upb_filedef *upb_symtab_addfile(
  5631. upb_symtab *s, const google_protobuf_FileDescriptorProto *file_proto,
  5632. upb_status *status) {
  5633. return _upb_symtab_addfile(s, file_proto, NULL, status);
  5634. }
  5635. /* Include here since we want most of this file to be stdio-free. */
  5636. #include <stdio.h>
  5637. bool _upb_symtab_loaddefinit(upb_symtab *s, const upb_def_init *init) {
  5638. /* Since this function should never fail (it would indicate a bug in upb) we
  5639. * print errors to stderr instead of returning error status to the user. */
  5640. upb_def_init **deps = init->deps;
  5641. google_protobuf_FileDescriptorProto *file;
  5642. upb_arena *arena;
  5643. upb_status status;
  5644. upb_status_clear(&status);
  5645. if (upb_strtable_lookup(&s->files, init->filename, NULL)) {
  5646. return true;
  5647. }
  5648. arena = upb_arena_new();
  5649. for (; *deps; deps++) {
  5650. if (!_upb_symtab_loaddefinit(s, *deps)) goto err;
  5651. }
  5652. file = google_protobuf_FileDescriptorProto_parse_ex(
  5653. init->descriptor.data, init->descriptor.size, NULL, UPB_DECODE_ALIAS,
  5654. arena);
  5655. s->bytes_loaded += init->descriptor.size;
  5656. if (!file) {
  5657. upb_status_seterrf(
  5658. &status,
  5659. "Failed to parse compiled-in descriptor for file '%s'. This should "
  5660. "never happen.",
  5661. init->filename);
  5662. goto err;
  5663. }
  5664. if (!_upb_symtab_addfile(s, file, init->layouts, &status)) goto err;
  5665. upb_arena_free(arena);
  5666. return true;
  5667. err:
  5668. fprintf(stderr, "Error loading compiled-in descriptor: %s\n",
  5669. upb_status_errmsg(&status));
  5670. upb_arena_free(arena);
  5671. return false;
  5672. }
  5673. size_t _upb_symtab_bytesloaded(const upb_symtab *s) {
  5674. return s->bytes_loaded;
  5675. }
  5676. upb_arena *_upb_symtab_arena(const upb_symtab *s) {
  5677. return s->arena;
  5678. }
  5679. #undef CHK_OOM
  5680. /** upb/reflection.c ************************************************************/
  5681. #include <string.h>
  5682. static size_t get_field_size(const upb_msglayout_field *f) {
  5683. static unsigned char sizes[] = {
  5684. 0,/* 0 */
  5685. 8, /* UPB_DESCRIPTOR_TYPE_DOUBLE */
  5686. 4, /* UPB_DESCRIPTOR_TYPE_FLOAT */
  5687. 8, /* UPB_DESCRIPTOR_TYPE_INT64 */
  5688. 8, /* UPB_DESCRIPTOR_TYPE_UINT64 */
  5689. 4, /* UPB_DESCRIPTOR_TYPE_INT32 */
  5690. 8, /* UPB_DESCRIPTOR_TYPE_FIXED64 */
  5691. 4, /* UPB_DESCRIPTOR_TYPE_FIXED32 */
  5692. 1, /* UPB_DESCRIPTOR_TYPE_BOOL */
  5693. sizeof(upb_strview), /* UPB_DESCRIPTOR_TYPE_STRING */
  5694. sizeof(void*), /* UPB_DESCRIPTOR_TYPE_GROUP */
  5695. sizeof(void*), /* UPB_DESCRIPTOR_TYPE_MESSAGE */
  5696. sizeof(upb_strview), /* UPB_DESCRIPTOR_TYPE_BYTES */
  5697. 4, /* UPB_DESCRIPTOR_TYPE_UINT32 */
  5698. 4, /* UPB_DESCRIPTOR_TYPE_ENUM */
  5699. 4, /* UPB_DESCRIPTOR_TYPE_SFIXED32 */
  5700. 8, /* UPB_DESCRIPTOR_TYPE_SFIXED64 */
  5701. 4, /* UPB_DESCRIPTOR_TYPE_SINT32 */
  5702. 8, /* UPB_DESCRIPTOR_TYPE_SINT64 */
  5703. };
  5704. return _upb_repeated_or_map(f) ? sizeof(void *) : sizes[f->descriptortype];
  5705. }
  5706. /* Strings/bytes are special-cased in maps. */
  5707. static char _upb_fieldtype_to_mapsize[12] = {
  5708. 0,
  5709. 1, /* UPB_TYPE_BOOL */
  5710. 4, /* UPB_TYPE_FLOAT */
  5711. 4, /* UPB_TYPE_INT32 */
  5712. 4, /* UPB_TYPE_UINT32 */
  5713. 4, /* UPB_TYPE_ENUM */
  5714. sizeof(void*), /* UPB_TYPE_MESSAGE */
  5715. 8, /* UPB_TYPE_DOUBLE */
  5716. 8, /* UPB_TYPE_INT64 */
  5717. 8, /* UPB_TYPE_UINT64 */
  5718. 0, /* UPB_TYPE_STRING */
  5719. 0, /* UPB_TYPE_BYTES */
  5720. };
  5721. static const char _upb_fieldtype_to_sizelg2[12] = {
  5722. 0,
  5723. 0, /* UPB_TYPE_BOOL */
  5724. 2, /* UPB_TYPE_FLOAT */
  5725. 2, /* UPB_TYPE_INT32 */
  5726. 2, /* UPB_TYPE_UINT32 */
  5727. 2, /* UPB_TYPE_ENUM */
  5728. UPB_SIZE(2, 3), /* UPB_TYPE_MESSAGE */
  5729. 3, /* UPB_TYPE_DOUBLE */
  5730. 3, /* UPB_TYPE_INT64 */
  5731. 3, /* UPB_TYPE_UINT64 */
  5732. UPB_SIZE(3, 4), /* UPB_TYPE_STRING */
  5733. UPB_SIZE(3, 4), /* UPB_TYPE_BYTES */
  5734. };
  5735. /** upb_msg *******************************************************************/
  5736. upb_msg *upb_msg_new(const upb_msgdef *m, upb_arena *a) {
  5737. return _upb_msg_new(upb_msgdef_layout(m), a);
  5738. }
  5739. static bool in_oneof(const upb_msglayout_field *field) {
  5740. return field->presence < 0;
  5741. }
  5742. static upb_msgval _upb_msg_getraw(const upb_msg *msg, const upb_fielddef *f) {
  5743. const upb_msglayout_field *field = upb_fielddef_layout(f);
  5744. const char *mem = UPB_PTR_AT(msg, field->offset, char);
  5745. upb_msgval val = {0};
  5746. memcpy(&val, mem, get_field_size(field));
  5747. return val;
  5748. }
  5749. bool upb_msg_has(const upb_msg *msg, const upb_fielddef *f) {
  5750. const upb_msglayout_field *field = upb_fielddef_layout(f);
  5751. if (in_oneof(field)) {
  5752. return _upb_getoneofcase_field(msg, field) == field->number;
  5753. } else if (field->presence > 0) {
  5754. return _upb_hasbit_field(msg, field);
  5755. } else {
  5756. UPB_ASSERT(field->descriptortype == UPB_DESCRIPTOR_TYPE_MESSAGE ||
  5757. field->descriptortype == UPB_DESCRIPTOR_TYPE_GROUP);
  5758. return _upb_msg_getraw(msg, f).msg_val != NULL;
  5759. }
  5760. }
  5761. const upb_fielddef *upb_msg_whichoneof(const upb_msg *msg,
  5762. const upb_oneofdef *o) {
  5763. const upb_fielddef *f = upb_oneofdef_field(o, 0);
  5764. if (upb_oneofdef_issynthetic(o)) {
  5765. UPB_ASSERT(upb_oneofdef_fieldcount(o) == 1);
  5766. return upb_msg_has(msg, f) ? f : NULL;
  5767. } else {
  5768. const upb_msglayout_field *field = upb_fielddef_layout(f);
  5769. uint32_t oneof_case = _upb_getoneofcase_field(msg, field);
  5770. f = oneof_case ? upb_oneofdef_itof(o, oneof_case) : NULL;
  5771. UPB_ASSERT((f != NULL) == (oneof_case != 0));
  5772. return f;
  5773. }
  5774. }
  5775. upb_msgval upb_msg_get(const upb_msg *msg, const upb_fielddef *f) {
  5776. if (!upb_fielddef_haspresence(f) || upb_msg_has(msg, f)) {
  5777. return _upb_msg_getraw(msg, f);
  5778. } else {
  5779. return upb_fielddef_default(f);
  5780. }
  5781. }
  5782. upb_mutmsgval upb_msg_mutable(upb_msg *msg, const upb_fielddef *f,
  5783. upb_arena *a) {
  5784. const upb_msglayout_field *field = upb_fielddef_layout(f);
  5785. upb_mutmsgval ret;
  5786. char *mem = UPB_PTR_AT(msg, field->offset, char);
  5787. bool wrong_oneof =
  5788. in_oneof(field) && _upb_getoneofcase_field(msg, field) != field->number;
  5789. memcpy(&ret, mem, sizeof(void*));
  5790. if (a && (!ret.msg || wrong_oneof)) {
  5791. if (upb_fielddef_ismap(f)) {
  5792. const upb_msgdef *entry = upb_fielddef_msgsubdef(f);
  5793. const upb_fielddef *key = upb_msgdef_itof(entry, UPB_MAPENTRY_KEY);
  5794. const upb_fielddef *value = upb_msgdef_itof(entry, UPB_MAPENTRY_VALUE);
  5795. ret.map = upb_map_new(a, upb_fielddef_type(key), upb_fielddef_type(value));
  5796. } else if (upb_fielddef_isseq(f)) {
  5797. ret.array = upb_array_new(a, upb_fielddef_type(f));
  5798. } else {
  5799. UPB_ASSERT(upb_fielddef_issubmsg(f));
  5800. ret.msg = upb_msg_new(upb_fielddef_msgsubdef(f), a);
  5801. }
  5802. memcpy(mem, &ret, sizeof(void*));
  5803. if (wrong_oneof) {
  5804. *_upb_oneofcase_field(msg, field) = field->number;
  5805. } else if (field->presence > 0) {
  5806. _upb_sethas_field(msg, field);
  5807. }
  5808. }
  5809. return ret;
  5810. }
  5811. void upb_msg_set(upb_msg *msg, const upb_fielddef *f, upb_msgval val,
  5812. upb_arena *a) {
  5813. const upb_msglayout_field *field = upb_fielddef_layout(f);
  5814. char *mem = UPB_PTR_AT(msg, field->offset, char);
  5815. UPB_UNUSED(a); /* We reserve the right to make set insert into a map. */
  5816. memcpy(mem, &val, get_field_size(field));
  5817. if (field->presence > 0) {
  5818. _upb_sethas_field(msg, field);
  5819. } else if (in_oneof(field)) {
  5820. *_upb_oneofcase_field(msg, field) = field->number;
  5821. }
  5822. }
  5823. void upb_msg_clearfield(upb_msg *msg, const upb_fielddef *f) {
  5824. const upb_msglayout_field *field = upb_fielddef_layout(f);
  5825. char *mem = UPB_PTR_AT(msg, field->offset, char);
  5826. if (field->presence > 0) {
  5827. _upb_clearhas_field(msg, field);
  5828. } else if (in_oneof(field)) {
  5829. uint32_t *oneof_case = _upb_oneofcase_field(msg, field);
  5830. if (*oneof_case != field->number) return;
  5831. *oneof_case = 0;
  5832. }
  5833. memset(mem, 0, get_field_size(field));
  5834. }
  5835. void upb_msg_clear(upb_msg *msg, const upb_msgdef *m) {
  5836. _upb_msg_clear(msg, upb_msgdef_layout(m));
  5837. }
  5838. bool upb_msg_next(const upb_msg *msg, const upb_msgdef *m,
  5839. const upb_symtab *ext_pool, const upb_fielddef **out_f,
  5840. upb_msgval *out_val, size_t *iter) {
  5841. int i = *iter;
  5842. int n = upb_msgdef_fieldcount(m);
  5843. const upb_msgval zero = {0};
  5844. UPB_UNUSED(ext_pool);
  5845. while (++i < n) {
  5846. const upb_fielddef *f = upb_msgdef_field(m, i);
  5847. upb_msgval val = _upb_msg_getraw(msg, f);
  5848. /* Skip field if unset or empty. */
  5849. if (upb_fielddef_haspresence(f)) {
  5850. if (!upb_msg_has(msg, f)) continue;
  5851. } else {
  5852. upb_msgval test = val;
  5853. if (upb_fielddef_isstring(f) && !upb_fielddef_isseq(f)) {
  5854. /* Clear string pointer, only size matters (ptr could be non-NULL). */
  5855. test.str_val.data = NULL;
  5856. }
  5857. /* Continue if NULL or 0. */
  5858. if (memcmp(&test, &zero, sizeof(test)) == 0) continue;
  5859. /* Continue on empty array or map. */
  5860. if (upb_fielddef_ismap(f)) {
  5861. if (upb_map_size(test.map_val) == 0) continue;
  5862. } else if (upb_fielddef_isseq(f)) {
  5863. if (upb_array_size(test.array_val) == 0) continue;
  5864. }
  5865. }
  5866. *out_val = val;
  5867. *out_f = f;
  5868. *iter = i;
  5869. return true;
  5870. }
  5871. *iter = i;
  5872. return false;
  5873. }
  5874. bool _upb_msg_discardunknown(upb_msg *msg, const upb_msgdef *m, int depth) {
  5875. size_t iter = UPB_MSG_BEGIN;
  5876. const upb_fielddef *f;
  5877. upb_msgval val;
  5878. bool ret = true;
  5879. if (--depth == 0) return false;
  5880. _upb_msg_discardunknown_shallow(msg);
  5881. while (upb_msg_next(msg, m, NULL /*ext_pool*/, &f, &val, &iter)) {
  5882. const upb_msgdef *subm = upb_fielddef_msgsubdef(f);
  5883. if (!subm) continue;
  5884. if (upb_fielddef_ismap(f)) {
  5885. const upb_fielddef *val_f = upb_msgdef_itof(subm, 2);
  5886. const upb_msgdef *val_m = upb_fielddef_msgsubdef(val_f);
  5887. upb_map *map = (upb_map*)val.map_val;
  5888. size_t iter = UPB_MAP_BEGIN;
  5889. if (!val_m) continue;
  5890. while (upb_mapiter_next(map, &iter)) {
  5891. upb_msgval map_val = upb_mapiter_value(map, iter);
  5892. if (!_upb_msg_discardunknown((upb_msg*)map_val.msg_val, val_m, depth)) {
  5893. ret = false;
  5894. }
  5895. }
  5896. } else if (upb_fielddef_isseq(f)) {
  5897. const upb_array *arr = val.array_val;
  5898. size_t i, n = upb_array_size(arr);
  5899. for (i = 0; i < n; i++) {
  5900. upb_msgval elem = upb_array_get(arr, i);
  5901. if (!_upb_msg_discardunknown((upb_msg*)elem.msg_val, subm, depth)) {
  5902. ret = false;
  5903. }
  5904. }
  5905. } else {
  5906. if (!_upb_msg_discardunknown((upb_msg*)val.msg_val, subm, depth)) {
  5907. ret = false;
  5908. }
  5909. }
  5910. }
  5911. return ret;
  5912. }
  5913. bool upb_msg_discardunknown(upb_msg *msg, const upb_msgdef *m, int maxdepth) {
  5914. return _upb_msg_discardunknown(msg, m, maxdepth);
  5915. }
  5916. /** upb_array *****************************************************************/
  5917. upb_array *upb_array_new(upb_arena *a, upb_fieldtype_t type) {
  5918. return _upb_array_new(a, 4, _upb_fieldtype_to_sizelg2[type]);
  5919. }
  5920. size_t upb_array_size(const upb_array *arr) {
  5921. return arr->len;
  5922. }
  5923. upb_msgval upb_array_get(const upb_array *arr, size_t i) {
  5924. upb_msgval ret;
  5925. const char* data = _upb_array_constptr(arr);
  5926. int lg2 = arr->data & 7;
  5927. UPB_ASSERT(i < arr->len);
  5928. memcpy(&ret, data + (i << lg2), 1 << lg2);
  5929. return ret;
  5930. }
  5931. void upb_array_set(upb_array *arr, size_t i, upb_msgval val) {
  5932. char* data = _upb_array_ptr(arr);
  5933. int lg2 = arr->data & 7;
  5934. UPB_ASSERT(i < arr->len);
  5935. memcpy(data + (i << lg2), &val, 1 << lg2);
  5936. }
  5937. bool upb_array_append(upb_array *arr, upb_msgval val, upb_arena *arena) {
  5938. if (!upb_array_resize(arr, arr->len + 1, arena)) {
  5939. return false;
  5940. }
  5941. upb_array_set(arr, arr->len - 1, val);
  5942. return true;
  5943. }
  5944. bool upb_array_resize(upb_array *arr, size_t size, upb_arena *arena) {
  5945. return _upb_array_resize(arr, size, arena);
  5946. }
  5947. /** upb_map *******************************************************************/
  5948. upb_map *upb_map_new(upb_arena *a, upb_fieldtype_t key_type,
  5949. upb_fieldtype_t value_type) {
  5950. return _upb_map_new(a, _upb_fieldtype_to_mapsize[key_type],
  5951. _upb_fieldtype_to_mapsize[value_type]);
  5952. }
  5953. size_t upb_map_size(const upb_map *map) {
  5954. return _upb_map_size(map);
  5955. }
  5956. bool upb_map_get(const upb_map *map, upb_msgval key, upb_msgval *val) {
  5957. return _upb_map_get(map, &key, map->key_size, val, map->val_size);
  5958. }
  5959. void upb_map_clear(upb_map *map) {
  5960. _upb_map_clear(map);
  5961. }
  5962. bool upb_map_set(upb_map *map, upb_msgval key, upb_msgval val,
  5963. upb_arena *arena) {
  5964. return _upb_map_set(map, &key, map->key_size, &val, map->val_size, arena);
  5965. }
  5966. bool upb_map_delete(upb_map *map, upb_msgval key) {
  5967. return _upb_map_delete(map, &key, map->key_size);
  5968. }
  5969. bool upb_mapiter_next(const upb_map *map, size_t *iter) {
  5970. return _upb_map_next(map, iter);
  5971. }
  5972. bool upb_mapiter_done(const upb_map *map, size_t iter) {
  5973. upb_strtable_iter i;
  5974. UPB_ASSERT(iter != UPB_MAP_BEGIN);
  5975. i.t = &map->table;
  5976. i.index = iter;
  5977. return upb_strtable_done(&i);
  5978. }
  5979. /* Returns the key and value for this entry of the map. */
  5980. upb_msgval upb_mapiter_key(const upb_map *map, size_t iter) {
  5981. upb_strtable_iter i;
  5982. upb_msgval ret;
  5983. i.t = &map->table;
  5984. i.index = iter;
  5985. _upb_map_fromkey(upb_strtable_iter_key(&i), &ret, map->key_size);
  5986. return ret;
  5987. }
  5988. upb_msgval upb_mapiter_value(const upb_map *map, size_t iter) {
  5989. upb_strtable_iter i;
  5990. upb_msgval ret;
  5991. i.t = &map->table;
  5992. i.index = iter;
  5993. _upb_map_fromvalue(upb_strtable_iter_value(&i), &ret, map->val_size);
  5994. return ret;
  5995. }
  5996. /* void upb_mapiter_setvalue(upb_map *map, size_t iter, upb_msgval value); */
  5997. /** upb/json_decode.c ************************************************************/
  5998. #include <errno.h>
  5999. #include <float.h>
  6000. #include <inttypes.h>
  6001. #include <limits.h>
  6002. #include <math.h>
  6003. #include <setjmp.h>
  6004. #include <stdlib.h>
  6005. #include <string.h>
  6006. /* Special header, must be included last. */
  6007. typedef struct {
  6008. const char *ptr, *end;
  6009. upb_arena *arena; /* TODO: should we have a tmp arena for tmp data? */
  6010. const upb_symtab *any_pool;
  6011. int depth;
  6012. upb_status *status;
  6013. jmp_buf err;
  6014. int line;
  6015. const char *line_begin;
  6016. bool is_first;
  6017. int options;
  6018. const upb_fielddef *debug_field;
  6019. } jsondec;
  6020. enum { JD_OBJECT, JD_ARRAY, JD_STRING, JD_NUMBER, JD_TRUE, JD_FALSE, JD_NULL };
  6021. /* Forward declarations of mutually-recursive functions. */
  6022. static void jsondec_wellknown(jsondec *d, upb_msg *msg, const upb_msgdef *m);
  6023. static upb_msgval jsondec_value(jsondec *d, const upb_fielddef *f);
  6024. static void jsondec_wellknownvalue(jsondec *d, upb_msg *msg,
  6025. const upb_msgdef *m);
  6026. static void jsondec_object(jsondec *d, upb_msg *msg, const upb_msgdef *m);
  6027. static bool jsondec_streql(upb_strview str, const char *lit) {
  6028. return str.size == strlen(lit) && memcmp(str.data, lit, str.size) == 0;
  6029. }
  6030. static bool jsondec_isnullvalue(const upb_fielddef *f) {
  6031. return upb_fielddef_type(f) == UPB_TYPE_ENUM &&
  6032. strcmp(upb_enumdef_fullname(upb_fielddef_enumsubdef(f)),
  6033. "google.protobuf.NullValue") == 0;
  6034. }
  6035. static bool jsondec_isvalue(const upb_fielddef *f) {
  6036. return (upb_fielddef_type(f) == UPB_TYPE_MESSAGE &&
  6037. upb_msgdef_wellknowntype(upb_fielddef_msgsubdef(f)) ==
  6038. UPB_WELLKNOWN_VALUE) ||
  6039. jsondec_isnullvalue(f);
  6040. }
  6041. UPB_NORETURN static void jsondec_err(jsondec *d, const char *msg) {
  6042. upb_status_seterrf(d->status, "Error parsing JSON @%d:%d: %s", d->line,
  6043. (int)(d->ptr - d->line_begin), msg);
  6044. UPB_LONGJMP(d->err, 1);
  6045. }
  6046. UPB_PRINTF(2, 3)
  6047. UPB_NORETURN static void jsondec_errf(jsondec *d, const char *fmt, ...) {
  6048. va_list argp;
  6049. upb_status_seterrf(d->status, "Error parsing JSON @%d:%d: ", d->line,
  6050. (int)(d->ptr - d->line_begin));
  6051. va_start(argp, fmt);
  6052. upb_status_vappenderrf(d->status, fmt, argp);
  6053. va_end(argp);
  6054. UPB_LONGJMP(d->err, 1);
  6055. }
  6056. static void jsondec_skipws(jsondec *d) {
  6057. while (d->ptr != d->end) {
  6058. switch (*d->ptr) {
  6059. case '\n':
  6060. d->line++;
  6061. d->line_begin = d->ptr;
  6062. /* Fallthrough. */
  6063. case '\r':
  6064. case '\t':
  6065. case ' ':
  6066. d->ptr++;
  6067. break;
  6068. default:
  6069. return;
  6070. }
  6071. }
  6072. jsondec_err(d, "Unexpected EOF");
  6073. }
  6074. static bool jsondec_tryparsech(jsondec *d, char ch) {
  6075. if (d->ptr == d->end || *d->ptr != ch) return false;
  6076. d->ptr++;
  6077. return true;
  6078. }
  6079. static void jsondec_parselit(jsondec *d, const char *lit) {
  6080. size_t avail = d->end - d->ptr;
  6081. size_t len = strlen(lit);
  6082. if (avail < len || memcmp(d->ptr, lit, len) != 0) {
  6083. jsondec_errf(d, "Expected: '%s'", lit);
  6084. }
  6085. d->ptr += len;
  6086. }
  6087. static void jsondec_wsch(jsondec *d, char ch) {
  6088. jsondec_skipws(d);
  6089. if (!jsondec_tryparsech(d, ch)) {
  6090. jsondec_errf(d, "Expected: '%c'", ch);
  6091. }
  6092. }
  6093. static void jsondec_true(jsondec *d) { jsondec_parselit(d, "true"); }
  6094. static void jsondec_false(jsondec *d) { jsondec_parselit(d, "false"); }
  6095. static void jsondec_null(jsondec *d) { jsondec_parselit(d, "null"); }
  6096. static void jsondec_entrysep(jsondec *d) {
  6097. jsondec_skipws(d);
  6098. jsondec_parselit(d, ":");
  6099. }
  6100. static int jsondec_rawpeek(jsondec *d) {
  6101. switch (*d->ptr) {
  6102. case '{':
  6103. return JD_OBJECT;
  6104. case '[':
  6105. return JD_ARRAY;
  6106. case '"':
  6107. return JD_STRING;
  6108. case '-':
  6109. case '0':
  6110. case '1':
  6111. case '2':
  6112. case '3':
  6113. case '4':
  6114. case '5':
  6115. case '6':
  6116. case '7':
  6117. case '8':
  6118. case '9':
  6119. return JD_NUMBER;
  6120. case 't':
  6121. return JD_TRUE;
  6122. case 'f':
  6123. return JD_FALSE;
  6124. case 'n':
  6125. return JD_NULL;
  6126. default:
  6127. jsondec_errf(d, "Unexpected character: '%c'", *d->ptr);
  6128. }
  6129. }
  6130. /* JSON object/array **********************************************************/
  6131. /* These are used like so:
  6132. *
  6133. * jsondec_objstart(d);
  6134. * while (jsondec_objnext(d)) {
  6135. * ...
  6136. * }
  6137. * jsondec_objend(d) */
  6138. static int jsondec_peek(jsondec *d) {
  6139. jsondec_skipws(d);
  6140. return jsondec_rawpeek(d);
  6141. }
  6142. static void jsondec_push(jsondec *d) {
  6143. if (--d->depth < 0) {
  6144. jsondec_err(d, "Recursion limit exceeded");
  6145. }
  6146. d->is_first = true;
  6147. }
  6148. static bool jsondec_seqnext(jsondec *d, char end_ch) {
  6149. bool is_first = d->is_first;
  6150. d->is_first = false;
  6151. jsondec_skipws(d);
  6152. if (*d->ptr == end_ch) return false;
  6153. if (!is_first) jsondec_parselit(d, ",");
  6154. return true;
  6155. }
  6156. static void jsondec_arrstart(jsondec *d) {
  6157. jsondec_push(d);
  6158. jsondec_wsch(d, '[');
  6159. }
  6160. static void jsondec_arrend(jsondec *d) {
  6161. d->depth++;
  6162. jsondec_wsch(d, ']');
  6163. }
  6164. static bool jsondec_arrnext(jsondec *d) {
  6165. return jsondec_seqnext(d, ']');
  6166. }
  6167. static void jsondec_objstart(jsondec *d) {
  6168. jsondec_push(d);
  6169. jsondec_wsch(d, '{');
  6170. }
  6171. static void jsondec_objend(jsondec *d) {
  6172. d->depth++;
  6173. jsondec_wsch(d, '}');
  6174. }
  6175. static bool jsondec_objnext(jsondec *d) {
  6176. if (!jsondec_seqnext(d, '}')) return false;
  6177. if (jsondec_peek(d) != JD_STRING) {
  6178. jsondec_err(d, "Object must start with string");
  6179. }
  6180. return true;
  6181. }
  6182. /* JSON number ****************************************************************/
  6183. static bool jsondec_tryskipdigits(jsondec *d) {
  6184. const char *start = d->ptr;
  6185. while (d->ptr < d->end) {
  6186. if (*d->ptr < '0' || *d->ptr > '9') {
  6187. break;
  6188. }
  6189. d->ptr++;
  6190. }
  6191. return d->ptr != start;
  6192. }
  6193. static void jsondec_skipdigits(jsondec *d) {
  6194. if (!jsondec_tryskipdigits(d)) {
  6195. jsondec_err(d, "Expected one or more digits");
  6196. }
  6197. }
  6198. static double jsondec_number(jsondec *d) {
  6199. const char *start = d->ptr;
  6200. assert(jsondec_rawpeek(d) == JD_NUMBER);
  6201. /* Skip over the syntax of a number, as specified by JSON. */
  6202. if (*d->ptr == '-') d->ptr++;
  6203. if (jsondec_tryparsech(d, '0')) {
  6204. if (jsondec_tryskipdigits(d)) {
  6205. jsondec_err(d, "number cannot have leading zero");
  6206. }
  6207. } else {
  6208. jsondec_skipdigits(d);
  6209. }
  6210. if (d->ptr == d->end) goto parse;
  6211. if (jsondec_tryparsech(d, '.')) {
  6212. jsondec_skipdigits(d);
  6213. }
  6214. if (d->ptr == d->end) goto parse;
  6215. if (*d->ptr == 'e' || *d->ptr == 'E') {
  6216. d->ptr++;
  6217. if (d->ptr == d->end) {
  6218. jsondec_err(d, "Unexpected EOF in number");
  6219. }
  6220. if (*d->ptr == '+' || *d->ptr == '-') {
  6221. d->ptr++;
  6222. }
  6223. jsondec_skipdigits(d);
  6224. }
  6225. parse:
  6226. /* Having verified the syntax of a JSON number, use strtod() to parse
  6227. * (strtod() accepts a superset of JSON syntax). */
  6228. errno = 0;
  6229. {
  6230. char* end;
  6231. double val = strtod(start, &end);
  6232. assert(end == d->ptr);
  6233. /* Currently the min/max-val conformance tests fail if we check this. Does
  6234. * this mean the conformance tests are wrong or strtod() is wrong, or
  6235. * something else? Investigate further. */
  6236. /*
  6237. if (errno == ERANGE) {
  6238. jsondec_err(d, "Number out of range");
  6239. }
  6240. */
  6241. if (val > DBL_MAX || val < -DBL_MAX) {
  6242. jsondec_err(d, "Number out of range");
  6243. }
  6244. return val;
  6245. }
  6246. }
  6247. /* JSON string ****************************************************************/
  6248. static char jsondec_escape(jsondec *d) {
  6249. switch (*d->ptr++) {
  6250. case '"':
  6251. return '\"';
  6252. case '\\':
  6253. return '\\';
  6254. case '/':
  6255. return '/';
  6256. case 'b':
  6257. return '\b';
  6258. case 'f':
  6259. return '\f';
  6260. case 'n':
  6261. return '\n';
  6262. case 'r':
  6263. return '\r';
  6264. case 't':
  6265. return '\t';
  6266. default:
  6267. jsondec_err(d, "Invalid escape char");
  6268. }
  6269. }
  6270. static uint32_t jsondec_codepoint(jsondec *d) {
  6271. uint32_t cp = 0;
  6272. const char *end;
  6273. if (d->end - d->ptr < 4) {
  6274. jsondec_err(d, "EOF inside string");
  6275. }
  6276. end = d->ptr + 4;
  6277. while (d->ptr < end) {
  6278. char ch = *d->ptr++;
  6279. if (ch >= '0' && ch <= '9') {
  6280. ch -= '0';
  6281. } else if (ch >= 'a' && ch <= 'f') {
  6282. ch = ch - 'a' + 10;
  6283. } else if (ch >= 'A' && ch <= 'F') {
  6284. ch = ch - 'A' + 10;
  6285. } else {
  6286. jsondec_err(d, "Invalid hex digit");
  6287. }
  6288. cp = (cp << 4) | ch;
  6289. }
  6290. return cp;
  6291. }
  6292. /* Parses a \uXXXX unicode escape (possibly a surrogate pair). */
  6293. static size_t jsondec_unicode(jsondec *d, char* out) {
  6294. uint32_t cp = jsondec_codepoint(d);
  6295. if (cp >= 0xd800 && cp <= 0xdbff) {
  6296. /* Surrogate pair: two 16-bit codepoints become a 32-bit codepoint. */
  6297. uint32_t high = cp;
  6298. uint32_t low;
  6299. jsondec_parselit(d, "\\u");
  6300. low = jsondec_codepoint(d);
  6301. if (low < 0xdc00 || low > 0xdfff) {
  6302. jsondec_err(d, "Invalid low surrogate");
  6303. }
  6304. cp = (high & 0x3ff) << 10;
  6305. cp |= (low & 0x3ff);
  6306. cp += 0x10000;
  6307. } else if (cp >= 0xdc00 && cp <= 0xdfff) {
  6308. jsondec_err(d, "Unpaired low surrogate");
  6309. }
  6310. /* Write to UTF-8 */
  6311. if (cp <= 0x7f) {
  6312. out[0] = cp;
  6313. return 1;
  6314. } else if (cp <= 0x07FF) {
  6315. out[0] = ((cp >> 6) & 0x1F) | 0xC0;
  6316. out[1] = ((cp >> 0) & 0x3F) | 0x80;
  6317. return 2;
  6318. } else if (cp <= 0xFFFF) {
  6319. out[0] = ((cp >> 12) & 0x0F) | 0xE0;
  6320. out[1] = ((cp >> 6) & 0x3F) | 0x80;
  6321. out[2] = ((cp >> 0) & 0x3F) | 0x80;
  6322. return 3;
  6323. } else if (cp < 0x10FFFF) {
  6324. out[0] = ((cp >> 18) & 0x07) | 0xF0;
  6325. out[1] = ((cp >> 12) & 0x3f) | 0x80;
  6326. out[2] = ((cp >> 6) & 0x3f) | 0x80;
  6327. out[3] = ((cp >> 0) & 0x3f) | 0x80;
  6328. return 4;
  6329. } else {
  6330. jsondec_err(d, "Invalid codepoint");
  6331. }
  6332. }
  6333. static void jsondec_resize(jsondec *d, char **buf, char **end, char **buf_end) {
  6334. size_t oldsize = *buf_end - *buf;
  6335. size_t len = *end - *buf;
  6336. size_t size = UPB_MAX(8, 2 * oldsize);
  6337. *buf = upb_arena_realloc(d->arena, *buf, len, size);
  6338. if (!*buf) jsondec_err(d, "Out of memory");
  6339. *end = *buf + len;
  6340. *buf_end = *buf + size;
  6341. }
  6342. static upb_strview jsondec_string(jsondec *d) {
  6343. char *buf = NULL;
  6344. char *end = NULL;
  6345. char *buf_end = NULL;
  6346. jsondec_skipws(d);
  6347. if (*d->ptr++ != '"') {
  6348. jsondec_err(d, "Expected string");
  6349. }
  6350. while (d->ptr < d->end) {
  6351. char ch = *d->ptr++;
  6352. if (end == buf_end) {
  6353. jsondec_resize(d, &buf, &end, &buf_end);
  6354. }
  6355. switch (ch) {
  6356. case '"': {
  6357. upb_strview ret;
  6358. ret.data = buf;
  6359. ret.size = end - buf;
  6360. *end = '\0'; /* Needed for possible strtod(). */
  6361. return ret;
  6362. }
  6363. case '\\':
  6364. if (d->ptr == d->end) goto eof;
  6365. if (*d->ptr == 'u') {
  6366. d->ptr++;
  6367. if (buf_end - end < 4) {
  6368. /* Allow space for maximum-sized code point (4 bytes). */
  6369. jsondec_resize(d, &buf, &end, &buf_end);
  6370. }
  6371. end += jsondec_unicode(d, end);
  6372. } else {
  6373. *end++ = jsondec_escape(d);
  6374. }
  6375. break;
  6376. default:
  6377. if ((unsigned char)*d->ptr < 0x20) {
  6378. jsondec_err(d, "Invalid char in JSON string");
  6379. }
  6380. *end++ = ch;
  6381. break;
  6382. }
  6383. }
  6384. eof:
  6385. jsondec_err(d, "EOF inside string");
  6386. }
  6387. static void jsondec_skipval(jsondec *d) {
  6388. switch (jsondec_peek(d)) {
  6389. case JD_OBJECT:
  6390. jsondec_objstart(d);
  6391. while (jsondec_objnext(d)) {
  6392. jsondec_string(d);
  6393. jsondec_entrysep(d);
  6394. jsondec_skipval(d);
  6395. }
  6396. jsondec_objend(d);
  6397. break;
  6398. case JD_ARRAY:
  6399. jsondec_arrstart(d);
  6400. while (jsondec_arrnext(d)) {
  6401. jsondec_skipval(d);
  6402. }
  6403. jsondec_arrend(d);
  6404. break;
  6405. case JD_TRUE:
  6406. jsondec_true(d);
  6407. break;
  6408. case JD_FALSE:
  6409. jsondec_false(d);
  6410. break;
  6411. case JD_NULL:
  6412. jsondec_null(d);
  6413. break;
  6414. case JD_STRING:
  6415. jsondec_string(d);
  6416. break;
  6417. case JD_NUMBER:
  6418. jsondec_number(d);
  6419. break;
  6420. }
  6421. }
  6422. /* Base64 decoding for bytes fields. ******************************************/
  6423. static unsigned int jsondec_base64_tablelookup(const char ch) {
  6424. /* Table includes the normal base64 chars plus the URL-safe variant. */
  6425. const signed char table[256] = {
  6426. -1, -1, -1, -1, -1, -1, -1,
  6427. -1, -1, -1, -1, -1, -1, -1,
  6428. -1, -1, -1, -1, -1, -1, -1,
  6429. -1, -1, -1, -1, -1, -1, -1,
  6430. -1, -1, -1, -1, -1, -1, -1,
  6431. -1, -1, -1, -1, -1, -1, -1,
  6432. -1, 62 /*+*/, -1, 62 /*-*/, -1, 63 /*/ */, 52 /*0*/,
  6433. 53 /*1*/, 54 /*2*/, 55 /*3*/, 56 /*4*/, 57 /*5*/, 58 /*6*/, 59 /*7*/,
  6434. 60 /*8*/, 61 /*9*/, -1, -1, -1, -1, -1,
  6435. -1, -1, 0 /*A*/, 1 /*B*/, 2 /*C*/, 3 /*D*/, 4 /*E*/,
  6436. 5 /*F*/, 6 /*G*/, 07 /*H*/, 8 /*I*/, 9 /*J*/, 10 /*K*/, 11 /*L*/,
  6437. 12 /*M*/, 13 /*N*/, 14 /*O*/, 15 /*P*/, 16 /*Q*/, 17 /*R*/, 18 /*S*/,
  6438. 19 /*T*/, 20 /*U*/, 21 /*V*/, 22 /*W*/, 23 /*X*/, 24 /*Y*/, 25 /*Z*/,
  6439. -1, -1, -1, -1, 63 /*_*/, -1, 26 /*a*/,
  6440. 27 /*b*/, 28 /*c*/, 29 /*d*/, 30 /*e*/, 31 /*f*/, 32 /*g*/, 33 /*h*/,
  6441. 34 /*i*/, 35 /*j*/, 36 /*k*/, 37 /*l*/, 38 /*m*/, 39 /*n*/, 40 /*o*/,
  6442. 41 /*p*/, 42 /*q*/, 43 /*r*/, 44 /*s*/, 45 /*t*/, 46 /*u*/, 47 /*v*/,
  6443. 48 /*w*/, 49 /*x*/, 50 /*y*/, 51 /*z*/, -1, -1, -1,
  6444. -1, -1, -1, -1, -1, -1, -1,
  6445. -1, -1, -1, -1, -1, -1, -1,
  6446. -1, -1, -1, -1, -1, -1, -1,
  6447. -1, -1, -1, -1, -1, -1, -1,
  6448. -1, -1, -1, -1, -1, -1, -1,
  6449. -1, -1, -1, -1, -1, -1, -1,
  6450. -1, -1, -1, -1, -1, -1, -1,
  6451. -1, -1, -1, -1, -1, -1, -1,
  6452. -1, -1, -1, -1, -1, -1, -1,
  6453. -1, -1, -1, -1, -1, -1, -1,
  6454. -1, -1, -1, -1, -1, -1, -1,
  6455. -1, -1, -1, -1, -1, -1, -1,
  6456. -1, -1, -1, -1, -1, -1, -1,
  6457. -1, -1, -1, -1, -1, -1, -1,
  6458. -1, -1, -1, -1, -1, -1, -1,
  6459. -1, -1, -1, -1, -1, -1, -1,
  6460. -1, -1, -1, -1, -1, -1, -1,
  6461. -1, -1, -1, -1, -1, -1, -1,
  6462. -1, -1, -1, -1};
  6463. /* Sign-extend return value so high bit will be set on any unexpected char. */
  6464. return table[(unsigned)ch];
  6465. }
  6466. static char *jsondec_partialbase64(jsondec *d, const char *ptr, const char *end,
  6467. char *out) {
  6468. int32_t val = -1;
  6469. switch (end - ptr) {
  6470. case 2:
  6471. val = jsondec_base64_tablelookup(ptr[0]) << 18 |
  6472. jsondec_base64_tablelookup(ptr[1]) << 12;
  6473. out[0] = val >> 16;
  6474. out += 1;
  6475. break;
  6476. case 3:
  6477. val = jsondec_base64_tablelookup(ptr[0]) << 18 |
  6478. jsondec_base64_tablelookup(ptr[1]) << 12 |
  6479. jsondec_base64_tablelookup(ptr[2]) << 6;
  6480. out[0] = val >> 16;
  6481. out[1] = (val >> 8) & 0xff;
  6482. out += 2;
  6483. break;
  6484. }
  6485. if (val < 0) {
  6486. jsondec_err(d, "Corrupt base64");
  6487. }
  6488. return out;
  6489. }
  6490. static size_t jsondec_base64(jsondec *d, upb_strview str) {
  6491. /* We decode in place. This is safe because this is a new buffer (not
  6492. * aliasing the input) and because base64 decoding shrinks 4 bytes into 3. */
  6493. char *out = (char*)str.data;
  6494. const char *ptr = str.data;
  6495. const char *end = ptr + str.size;
  6496. const char *end4 = ptr + (str.size & -4); /* Round down to multiple of 4. */
  6497. for (; ptr < end4; ptr += 4, out += 3) {
  6498. int val = jsondec_base64_tablelookup(ptr[0]) << 18 |
  6499. jsondec_base64_tablelookup(ptr[1]) << 12 |
  6500. jsondec_base64_tablelookup(ptr[2]) << 6 |
  6501. jsondec_base64_tablelookup(ptr[3]) << 0;
  6502. if (val < 0) {
  6503. /* Junk chars or padding. Remove trailing padding, if any. */
  6504. if (end - ptr == 4 && ptr[3] == '=') {
  6505. if (ptr[2] == '=') {
  6506. end -= 2;
  6507. } else {
  6508. end -= 1;
  6509. }
  6510. }
  6511. break;
  6512. }
  6513. out[0] = val >> 16;
  6514. out[1] = (val >> 8) & 0xff;
  6515. out[2] = val & 0xff;
  6516. }
  6517. if (ptr < end) {
  6518. /* Process remaining chars. We do not require padding. */
  6519. out = jsondec_partialbase64(d, ptr, end, out);
  6520. }
  6521. return out - str.data;
  6522. }
  6523. /* Low-level integer parsing **************************************************/
  6524. /* We use these hand-written routines instead of strto[u]l() because the "long
  6525. * long" variants aren't in c89. Also our version allows setting a ptr limit. */
  6526. static const char *jsondec_buftouint64(jsondec *d, const char *ptr,
  6527. const char *end, uint64_t *val) {
  6528. uint64_t u64 = 0;
  6529. while (ptr < end) {
  6530. unsigned ch = *ptr - '0';
  6531. if (ch >= 10) break;
  6532. if (u64 > UINT64_MAX / 10 || u64 * 10 > UINT64_MAX - ch) {
  6533. jsondec_err(d, "Integer overflow");
  6534. }
  6535. u64 *= 10;
  6536. u64 += ch;
  6537. ptr++;
  6538. }
  6539. *val = u64;
  6540. return ptr;
  6541. }
  6542. static const char *jsondec_buftoint64(jsondec *d, const char *ptr,
  6543. const char *end, int64_t *val) {
  6544. bool neg = false;
  6545. uint64_t u64;
  6546. if (ptr != end && *ptr == '-') {
  6547. ptr++;
  6548. neg = true;
  6549. }
  6550. ptr = jsondec_buftouint64(d, ptr, end, &u64);
  6551. if (u64 > (uint64_t)INT64_MAX + neg) {
  6552. jsondec_err(d, "Integer overflow");
  6553. }
  6554. *val = neg ? -u64 : u64;
  6555. return ptr;
  6556. }
  6557. static uint64_t jsondec_strtouint64(jsondec *d, upb_strview str) {
  6558. const char *end = str.data + str.size;
  6559. uint64_t ret;
  6560. if (jsondec_buftouint64(d, str.data, end, &ret) != end) {
  6561. jsondec_err(d, "Non-number characters in quoted integer");
  6562. }
  6563. return ret;
  6564. }
  6565. static int64_t jsondec_strtoint64(jsondec *d, upb_strview str) {
  6566. const char *end = str.data + str.size;
  6567. int64_t ret;
  6568. if (jsondec_buftoint64(d, str.data, end, &ret) != end) {
  6569. jsondec_err(d, "Non-number characters in quoted integer");
  6570. }
  6571. return ret;
  6572. }
  6573. /* Primitive value types ******************************************************/
  6574. /* Parse INT32 or INT64 value. */
  6575. static upb_msgval jsondec_int(jsondec *d, const upb_fielddef *f) {
  6576. upb_msgval val;
  6577. switch (jsondec_peek(d)) {
  6578. case JD_NUMBER: {
  6579. double dbl = jsondec_number(d);
  6580. if (dbl > 9223372036854774784.0 || dbl < -9223372036854775808.0) {
  6581. jsondec_err(d, "JSON number is out of range.");
  6582. }
  6583. val.int64_val = dbl; /* must be guarded, overflow here is UB */
  6584. if (val.int64_val != dbl) {
  6585. jsondec_errf(d, "JSON number was not integral (%f != %" PRId64 ")", dbl,
  6586. val.int64_val);
  6587. }
  6588. break;
  6589. }
  6590. case JD_STRING: {
  6591. upb_strview str = jsondec_string(d);
  6592. val.int64_val = jsondec_strtoint64(d, str);
  6593. break;
  6594. }
  6595. default:
  6596. jsondec_err(d, "Expected number or string");
  6597. }
  6598. if (upb_fielddef_type(f) == UPB_TYPE_INT32) {
  6599. if (val.int64_val > INT32_MAX || val.int64_val < INT32_MIN) {
  6600. jsondec_err(d, "Integer out of range.");
  6601. }
  6602. val.int32_val = (int32_t)val.int64_val;
  6603. }
  6604. return val;
  6605. }
  6606. /* Parse UINT32 or UINT64 value. */
  6607. static upb_msgval jsondec_uint(jsondec *d, const upb_fielddef *f) {
  6608. upb_msgval val = {0};
  6609. switch (jsondec_peek(d)) {
  6610. case JD_NUMBER: {
  6611. double dbl = jsondec_number(d);
  6612. if (dbl > 18446744073709549568.0 || dbl < 0) {
  6613. jsondec_err(d, "JSON number is out of range.");
  6614. }
  6615. val.uint64_val = dbl; /* must be guarded, overflow here is UB */
  6616. if (val.uint64_val != dbl) {
  6617. jsondec_errf(d, "JSON number was not integral (%f != %" PRIu64 ")", dbl,
  6618. val.uint64_val);
  6619. }
  6620. break;
  6621. }
  6622. case JD_STRING: {
  6623. upb_strview str = jsondec_string(d);
  6624. val.uint64_val = jsondec_strtouint64(d, str);
  6625. break;
  6626. }
  6627. default:
  6628. jsondec_err(d, "Expected number or string");
  6629. }
  6630. if (upb_fielddef_type(f) == UPB_TYPE_UINT32) {
  6631. if (val.uint64_val > UINT32_MAX) {
  6632. jsondec_err(d, "Integer out of range.");
  6633. }
  6634. val.uint32_val = (uint32_t)val.uint64_val;
  6635. }
  6636. return val;
  6637. }
  6638. /* Parse DOUBLE or FLOAT value. */
  6639. static upb_msgval jsondec_double(jsondec *d, const upb_fielddef *f) {
  6640. upb_strview str;
  6641. upb_msgval val = {0};
  6642. switch (jsondec_peek(d)) {
  6643. case JD_NUMBER:
  6644. val.double_val = jsondec_number(d);
  6645. break;
  6646. case JD_STRING:
  6647. str = jsondec_string(d);
  6648. if (jsondec_streql(str, "NaN")) {
  6649. val.double_val = NAN;
  6650. } else if (jsondec_streql(str, "Infinity")) {
  6651. val.double_val = INFINITY;
  6652. } else if (jsondec_streql(str, "-Infinity")) {
  6653. val.double_val = -INFINITY;
  6654. } else {
  6655. val.double_val = strtod(str.data, NULL);
  6656. }
  6657. break;
  6658. default:
  6659. jsondec_err(d, "Expected number or string");
  6660. }
  6661. if (upb_fielddef_type(f) == UPB_TYPE_FLOAT) {
  6662. if (val.double_val != INFINITY && val.double_val != -INFINITY &&
  6663. (val.double_val > FLT_MAX || val.double_val < -FLT_MAX)) {
  6664. jsondec_err(d, "Float out of range");
  6665. }
  6666. val.float_val = val.double_val;
  6667. }
  6668. return val;
  6669. }
  6670. /* Parse STRING or BYTES value. */
  6671. static upb_msgval jsondec_strfield(jsondec *d, const upb_fielddef *f) {
  6672. upb_msgval val;
  6673. val.str_val = jsondec_string(d);
  6674. if (upb_fielddef_type(f) == UPB_TYPE_BYTES) {
  6675. val.str_val.size = jsondec_base64(d, val.str_val);
  6676. }
  6677. return val;
  6678. }
  6679. static upb_msgval jsondec_enum(jsondec *d, const upb_fielddef *f) {
  6680. switch (jsondec_peek(d)) {
  6681. case JD_STRING: {
  6682. const upb_enumdef *e = upb_fielddef_enumsubdef(f);
  6683. upb_strview str = jsondec_string(d);
  6684. upb_msgval val;
  6685. if (!upb_enumdef_ntoi(e, str.data, str.size, &val.int32_val)) {
  6686. if (d->options & UPB_JSONDEC_IGNOREUNKNOWN) {
  6687. val.int32_val = 0;
  6688. } else {
  6689. jsondec_errf(d, "Unknown enumerator: '" UPB_STRVIEW_FORMAT "'",
  6690. UPB_STRVIEW_ARGS(str));
  6691. }
  6692. }
  6693. return val;
  6694. }
  6695. case JD_NULL: {
  6696. if (jsondec_isnullvalue(f)) {
  6697. upb_msgval val;
  6698. jsondec_null(d);
  6699. val.int32_val = 0;
  6700. return val;
  6701. }
  6702. }
  6703. /* Fallthrough. */
  6704. default:
  6705. return jsondec_int(d, f);
  6706. }
  6707. }
  6708. static upb_msgval jsondec_bool(jsondec *d, const upb_fielddef *f) {
  6709. bool is_map_key = upb_fielddef_number(f) == 1 &&
  6710. upb_msgdef_mapentry(upb_fielddef_containingtype(f));
  6711. upb_msgval val;
  6712. if (is_map_key) {
  6713. upb_strview str = jsondec_string(d);
  6714. if (jsondec_streql(str, "true")) {
  6715. val.bool_val = true;
  6716. } else if (jsondec_streql(str, "false")) {
  6717. val.bool_val = false;
  6718. } else {
  6719. jsondec_err(d, "Invalid boolean map key");
  6720. }
  6721. } else {
  6722. switch (jsondec_peek(d)) {
  6723. case JD_TRUE:
  6724. val.bool_val = true;
  6725. jsondec_true(d);
  6726. break;
  6727. case JD_FALSE:
  6728. val.bool_val = false;
  6729. jsondec_false(d);
  6730. break;
  6731. default:
  6732. jsondec_err(d, "Expected true or false");
  6733. }
  6734. }
  6735. return val;
  6736. }
  6737. /* Composite types (array/message/map) ****************************************/
  6738. static void jsondec_array(jsondec *d, upb_msg *msg, const upb_fielddef *f) {
  6739. upb_array *arr = upb_msg_mutable(msg, f, d->arena).array;
  6740. jsondec_arrstart(d);
  6741. while (jsondec_arrnext(d)) {
  6742. upb_msgval elem = jsondec_value(d, f);
  6743. upb_array_append(arr, elem, d->arena);
  6744. }
  6745. jsondec_arrend(d);
  6746. }
  6747. static void jsondec_map(jsondec *d, upb_msg *msg, const upb_fielddef *f) {
  6748. upb_map *map = upb_msg_mutable(msg, f, d->arena).map;
  6749. const upb_msgdef *entry = upb_fielddef_msgsubdef(f);
  6750. const upb_fielddef *key_f = upb_msgdef_itof(entry, 1);
  6751. const upb_fielddef *val_f = upb_msgdef_itof(entry, 2);
  6752. jsondec_objstart(d);
  6753. while (jsondec_objnext(d)) {
  6754. upb_msgval key, val;
  6755. key = jsondec_value(d, key_f);
  6756. jsondec_entrysep(d);
  6757. val = jsondec_value(d, val_f);
  6758. upb_map_set(map, key, val, d->arena);
  6759. }
  6760. jsondec_objend(d);
  6761. }
  6762. static void jsondec_tomsg(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6763. if (upb_msgdef_wellknowntype(m) == UPB_WELLKNOWN_UNSPECIFIED) {
  6764. jsondec_object(d, msg, m);
  6765. } else {
  6766. jsondec_wellknown(d, msg, m);
  6767. }
  6768. }
  6769. static upb_msgval jsondec_msg(jsondec *d, const upb_fielddef *f) {
  6770. const upb_msgdef *m = upb_fielddef_msgsubdef(f);
  6771. upb_msg *msg = upb_msg_new(m, d->arena);
  6772. upb_msgval val;
  6773. jsondec_tomsg(d, msg, m);
  6774. val.msg_val = msg;
  6775. return val;
  6776. }
  6777. static void jsondec_field(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6778. upb_strview name;
  6779. const upb_fielddef *f;
  6780. const upb_fielddef *preserved;
  6781. name = jsondec_string(d);
  6782. jsondec_entrysep(d);
  6783. f = upb_msgdef_lookupjsonname(m, name.data, name.size);
  6784. if (!f) {
  6785. if ((d->options & UPB_JSONDEC_IGNOREUNKNOWN) == 0) {
  6786. jsondec_errf(d, "No such field: " UPB_STRVIEW_FORMAT,
  6787. UPB_STRVIEW_ARGS(name));
  6788. }
  6789. jsondec_skipval(d);
  6790. return;
  6791. }
  6792. if (jsondec_peek(d) == JD_NULL && !jsondec_isvalue(f)) {
  6793. /* JSON "null" indicates a default value, so no need to set anything. */
  6794. jsondec_null(d);
  6795. return;
  6796. }
  6797. if (upb_fielddef_realcontainingoneof(f) &&
  6798. upb_msg_whichoneof(msg, upb_fielddef_containingoneof(f))) {
  6799. jsondec_err(d, "More than one field for this oneof.");
  6800. }
  6801. preserved = d->debug_field;
  6802. d->debug_field = f;
  6803. if (upb_fielddef_ismap(f)) {
  6804. jsondec_map(d, msg, f);
  6805. } else if (upb_fielddef_isseq(f)) {
  6806. jsondec_array(d, msg, f);
  6807. } else if (upb_fielddef_issubmsg(f)) {
  6808. upb_msg *submsg = upb_msg_mutable(msg, f, d->arena).msg;
  6809. const upb_msgdef *subm = upb_fielddef_msgsubdef(f);
  6810. jsondec_tomsg(d, submsg, subm);
  6811. } else {
  6812. upb_msgval val = jsondec_value(d, f);
  6813. upb_msg_set(msg, f, val, d->arena);
  6814. }
  6815. d->debug_field = preserved;
  6816. }
  6817. static void jsondec_object(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6818. jsondec_objstart(d);
  6819. while (jsondec_objnext(d)) {
  6820. jsondec_field(d, msg, m);
  6821. }
  6822. jsondec_objend(d);
  6823. }
  6824. static upb_msgval jsondec_value(jsondec *d, const upb_fielddef *f) {
  6825. switch (upb_fielddef_type(f)) {
  6826. case UPB_TYPE_BOOL:
  6827. return jsondec_bool(d, f);
  6828. case UPB_TYPE_FLOAT:
  6829. case UPB_TYPE_DOUBLE:
  6830. return jsondec_double(d, f);
  6831. case UPB_TYPE_UINT32:
  6832. case UPB_TYPE_UINT64:
  6833. return jsondec_uint(d, f);
  6834. case UPB_TYPE_INT32:
  6835. case UPB_TYPE_INT64:
  6836. return jsondec_int(d, f);
  6837. case UPB_TYPE_STRING:
  6838. case UPB_TYPE_BYTES:
  6839. return jsondec_strfield(d, f);
  6840. case UPB_TYPE_ENUM:
  6841. return jsondec_enum(d, f);
  6842. case UPB_TYPE_MESSAGE:
  6843. return jsondec_msg(d, f);
  6844. default:
  6845. UPB_UNREACHABLE();
  6846. }
  6847. }
  6848. /* Well-known types ***********************************************************/
  6849. static int jsondec_tsdigits(jsondec *d, const char **ptr, size_t digits,
  6850. const char *after) {
  6851. uint64_t val;
  6852. const char *p = *ptr;
  6853. const char *end = p + digits;
  6854. size_t after_len = after ? strlen(after) : 0;
  6855. UPB_ASSERT(digits <= 9); /* int can't overflow. */
  6856. if (jsondec_buftouint64(d, p, end, &val) != end ||
  6857. (after_len && memcmp(end, after, after_len) != 0)) {
  6858. jsondec_err(d, "Malformed timestamp");
  6859. }
  6860. UPB_ASSERT(val < INT_MAX);
  6861. *ptr = end + after_len;
  6862. return (int)val;
  6863. }
  6864. static int jsondec_nanos(jsondec *d, const char **ptr, const char *end) {
  6865. uint64_t nanos = 0;
  6866. const char *p = *ptr;
  6867. if (p != end && *p == '.') {
  6868. const char *nano_end = jsondec_buftouint64(d, p + 1, end, &nanos);
  6869. int digits = (int)(nano_end - p - 1);
  6870. int exp_lg10 = 9 - digits;
  6871. if (digits > 9) {
  6872. jsondec_err(d, "Too many digits for partial seconds");
  6873. }
  6874. while (exp_lg10--) nanos *= 10;
  6875. *ptr = nano_end;
  6876. }
  6877. UPB_ASSERT(nanos < INT_MAX);
  6878. return (int)nanos;
  6879. }
  6880. /* jsondec_epochdays(1970, 1, 1) == 1970-01-01 == 0. */
  6881. int jsondec_epochdays(int y, int m, int d) {
  6882. const uint32_t year_base = 4800; /* Before min year, multiple of 400. */
  6883. const uint32_t m_adj = m - 3; /* March-based month. */
  6884. const uint32_t carry = m_adj > (uint32_t)m ? 1 : 0;
  6885. const uint32_t adjust = carry ? 12 : 0;
  6886. const uint32_t y_adj = y + year_base - carry;
  6887. const uint32_t month_days = ((m_adj + adjust) * 62719 + 769) / 2048;
  6888. const uint32_t leap_days = y_adj / 4 - y_adj / 100 + y_adj / 400;
  6889. return y_adj * 365 + leap_days + month_days + (d - 1) - 2472632;
  6890. }
  6891. static int64_t jsondec_unixtime(int y, int m, int d, int h, int min, int s) {
  6892. return (int64_t)jsondec_epochdays(y, m, d) * 86400 + h * 3600 + min * 60 + s;
  6893. }
  6894. static void jsondec_timestamp(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6895. upb_msgval seconds;
  6896. upb_msgval nanos;
  6897. upb_strview str = jsondec_string(d);
  6898. const char *ptr = str.data;
  6899. const char *end = ptr + str.size;
  6900. if (str.size < 20) goto malformed;
  6901. {
  6902. /* 1972-01-01T01:00:00 */
  6903. int year = jsondec_tsdigits(d, &ptr, 4, "-");
  6904. int mon = jsondec_tsdigits(d, &ptr, 2, "-");
  6905. int day = jsondec_tsdigits(d, &ptr, 2, "T");
  6906. int hour = jsondec_tsdigits(d, &ptr, 2, ":");
  6907. int min = jsondec_tsdigits(d, &ptr, 2, ":");
  6908. int sec = jsondec_tsdigits(d, &ptr, 2, NULL);
  6909. seconds.int64_val = jsondec_unixtime(year, mon, day, hour, min, sec);
  6910. }
  6911. nanos.int32_val = jsondec_nanos(d, &ptr, end);
  6912. {
  6913. /* [+-]08:00 or Z */
  6914. int ofs_hour = 0;
  6915. int ofs_min = 0;
  6916. bool neg = false;
  6917. if (ptr == end) goto malformed;
  6918. switch (*ptr++) {
  6919. case '-':
  6920. neg = true;
  6921. /* fallthrough */
  6922. case '+':
  6923. if ((end - ptr) != 5) goto malformed;
  6924. ofs_hour = jsondec_tsdigits(d, &ptr, 2, ":");
  6925. ofs_min = jsondec_tsdigits(d, &ptr, 2, NULL);
  6926. ofs_min = ((ofs_hour * 60) + ofs_min) * 60;
  6927. seconds.int64_val += (neg ? ofs_min : -ofs_min);
  6928. break;
  6929. case 'Z':
  6930. if (ptr != end) goto malformed;
  6931. break;
  6932. default:
  6933. goto malformed;
  6934. }
  6935. }
  6936. if (seconds.int64_val < -62135596800) {
  6937. jsondec_err(d, "Timestamp out of range");
  6938. }
  6939. upb_msg_set(msg, upb_msgdef_itof(m, 1), seconds, d->arena);
  6940. upb_msg_set(msg, upb_msgdef_itof(m, 2), nanos, d->arena);
  6941. return;
  6942. malformed:
  6943. jsondec_err(d, "Malformed timestamp");
  6944. }
  6945. static void jsondec_duration(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6946. upb_msgval seconds;
  6947. upb_msgval nanos;
  6948. upb_strview str = jsondec_string(d);
  6949. const char *ptr = str.data;
  6950. const char *end = ptr + str.size;
  6951. const int64_t max = (uint64_t)3652500 * 86400;
  6952. /* "3.000000001s", "3s", etc. */
  6953. ptr = jsondec_buftoint64(d, ptr, end, &seconds.int64_val);
  6954. nanos.int32_val = jsondec_nanos(d, &ptr, end);
  6955. if (end - ptr != 1 || *ptr != 's') {
  6956. jsondec_err(d, "Malformed duration");
  6957. }
  6958. if (seconds.int64_val < -max || seconds.int64_val > max) {
  6959. jsondec_err(d, "Duration out of range");
  6960. }
  6961. if (seconds.int64_val < 0) {
  6962. nanos.int32_val = - nanos.int32_val;
  6963. }
  6964. upb_msg_set(msg, upb_msgdef_itof(m, 1), seconds, d->arena);
  6965. upb_msg_set(msg, upb_msgdef_itof(m, 2), nanos, d->arena);
  6966. }
  6967. static void jsondec_listvalue(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6968. const upb_fielddef *values_f = upb_msgdef_itof(m, 1);
  6969. const upb_msgdef *value_m = upb_fielddef_msgsubdef(values_f);
  6970. upb_array *values = upb_msg_mutable(msg, values_f, d->arena).array;
  6971. jsondec_arrstart(d);
  6972. while (jsondec_arrnext(d)) {
  6973. upb_msg *value_msg = upb_msg_new(value_m, d->arena);
  6974. upb_msgval value;
  6975. value.msg_val = value_msg;
  6976. upb_array_append(values, value, d->arena);
  6977. jsondec_wellknownvalue(d, value_msg, value_m);
  6978. }
  6979. jsondec_arrend(d);
  6980. }
  6981. static void jsondec_struct(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6982. const upb_fielddef *fields_f = upb_msgdef_itof(m, 1);
  6983. const upb_msgdef *entry_m = upb_fielddef_msgsubdef(fields_f);
  6984. const upb_fielddef *value_f = upb_msgdef_itof(entry_m, 2);
  6985. const upb_msgdef *value_m = upb_fielddef_msgsubdef(value_f);
  6986. upb_map *fields = upb_msg_mutable(msg, fields_f, d->arena).map;
  6987. jsondec_objstart(d);
  6988. while (jsondec_objnext(d)) {
  6989. upb_msgval key, value;
  6990. upb_msg *value_msg = upb_msg_new(value_m, d->arena);
  6991. key.str_val = jsondec_string(d);
  6992. value.msg_val = value_msg;
  6993. upb_map_set(fields, key, value, d->arena);
  6994. jsondec_entrysep(d);
  6995. jsondec_wellknownvalue(d, value_msg, value_m);
  6996. }
  6997. jsondec_objend(d);
  6998. }
  6999. static void jsondec_wellknownvalue(jsondec *d, upb_msg *msg,
  7000. const upb_msgdef *m) {
  7001. upb_msgval val;
  7002. const upb_fielddef *f;
  7003. upb_msg *submsg;
  7004. switch (jsondec_peek(d)) {
  7005. case JD_NUMBER:
  7006. /* double number_value = 2; */
  7007. f = upb_msgdef_itof(m, 2);
  7008. val.double_val = jsondec_number(d);
  7009. break;
  7010. case JD_STRING:
  7011. /* string string_value = 3; */
  7012. f = upb_msgdef_itof(m, 3);
  7013. val.str_val = jsondec_string(d);
  7014. break;
  7015. case JD_FALSE:
  7016. /* bool bool_value = 4; */
  7017. f = upb_msgdef_itof(m, 4);
  7018. val.bool_val = false;
  7019. jsondec_false(d);
  7020. break;
  7021. case JD_TRUE:
  7022. /* bool bool_value = 4; */
  7023. f = upb_msgdef_itof(m, 4);
  7024. val.bool_val = true;
  7025. jsondec_true(d);
  7026. break;
  7027. case JD_NULL:
  7028. /* NullValue null_value = 1; */
  7029. f = upb_msgdef_itof(m, 1);
  7030. val.int32_val = 0;
  7031. jsondec_null(d);
  7032. break;
  7033. /* Note: these cases return, because upb_msg_mutable() is enough. */
  7034. case JD_OBJECT:
  7035. /* Struct struct_value = 5; */
  7036. f = upb_msgdef_itof(m, 5);
  7037. submsg = upb_msg_mutable(msg, f, d->arena).msg;
  7038. jsondec_struct(d, submsg, upb_fielddef_msgsubdef(f));
  7039. return;
  7040. case JD_ARRAY:
  7041. /* ListValue list_value = 6; */
  7042. f = upb_msgdef_itof(m, 6);
  7043. submsg = upb_msg_mutable(msg, f, d->arena).msg;
  7044. jsondec_listvalue(d, submsg, upb_fielddef_msgsubdef(f));
  7045. return;
  7046. default:
  7047. UPB_UNREACHABLE();
  7048. }
  7049. upb_msg_set(msg, f, val, d->arena);
  7050. }
  7051. static upb_strview jsondec_mask(jsondec *d, const char *buf, const char *end) {
  7052. /* FieldMask fields grow due to inserted '_' characters, so we can't do the
  7053. * transform in place. */
  7054. const char *ptr = buf;
  7055. upb_strview ret;
  7056. char *out;
  7057. ret.size = end - ptr;
  7058. while (ptr < end) {
  7059. ret.size += (*ptr >= 'A' && *ptr <= 'Z');
  7060. ptr++;
  7061. }
  7062. out = upb_arena_malloc(d->arena, ret.size);
  7063. ptr = buf;
  7064. ret.data = out;
  7065. while (ptr < end) {
  7066. char ch = *ptr++;
  7067. if (ch >= 'A' && ch <= 'Z') {
  7068. *out++ = '_';
  7069. *out++ = ch + 32;
  7070. } else if (ch == '_') {
  7071. jsondec_err(d, "field mask may not contain '_'");
  7072. } else {
  7073. *out++ = ch;
  7074. }
  7075. }
  7076. return ret;
  7077. }
  7078. static void jsondec_fieldmask(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  7079. /* repeated string paths = 1; */
  7080. const upb_fielddef *paths_f = upb_msgdef_itof(m, 1);
  7081. upb_array *arr = upb_msg_mutable(msg, paths_f, d->arena).array;
  7082. upb_strview str = jsondec_string(d);
  7083. const char *ptr = str.data;
  7084. const char *end = ptr + str.size;
  7085. upb_msgval val;
  7086. while (ptr < end) {
  7087. const char *elem_end = memchr(ptr, ',', end - ptr);
  7088. if (elem_end) {
  7089. val.str_val = jsondec_mask(d, ptr, elem_end);
  7090. ptr = elem_end + 1;
  7091. } else {
  7092. val.str_val = jsondec_mask(d, ptr, end);
  7093. ptr = end;
  7094. }
  7095. upb_array_append(arr, val, d->arena);
  7096. }
  7097. }
  7098. static void jsondec_anyfield(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  7099. if (upb_msgdef_wellknowntype(m) == UPB_WELLKNOWN_UNSPECIFIED) {
  7100. /* For regular types: {"@type": "[user type]", "f1": <V1>, "f2": <V2>}
  7101. * where f1, f2, etc. are the normal fields of this type. */
  7102. jsondec_field(d, msg, m);
  7103. } else {
  7104. /* For well-known types: {"@type": "[well-known type]", "value": <X>}
  7105. * where <X> is whatever encoding the WKT normally uses. */
  7106. upb_strview str = jsondec_string(d);
  7107. jsondec_entrysep(d);
  7108. if (!jsondec_streql(str, "value")) {
  7109. jsondec_err(d, "Key for well-known type must be 'value'");
  7110. }
  7111. jsondec_wellknown(d, msg, m);
  7112. }
  7113. }
  7114. static const upb_msgdef *jsondec_typeurl(jsondec *d, upb_msg *msg,
  7115. const upb_msgdef *m) {
  7116. const upb_fielddef *type_url_f = upb_msgdef_itof(m, 1);
  7117. const upb_msgdef *type_m;
  7118. upb_strview type_url = jsondec_string(d);
  7119. const char *end = type_url.data + type_url.size;
  7120. const char *ptr = end;
  7121. upb_msgval val;
  7122. val.str_val = type_url;
  7123. upb_msg_set(msg, type_url_f, val, d->arena);
  7124. /* Find message name after the last '/' */
  7125. while (ptr > type_url.data && *--ptr != '/') {}
  7126. if (ptr == type_url.data || ptr == end) {
  7127. jsondec_err(d, "Type url must have at least one '/' and non-empty host");
  7128. }
  7129. ptr++;
  7130. type_m = upb_symtab_lookupmsg2(d->any_pool, ptr, end - ptr);
  7131. if (!type_m) {
  7132. jsondec_err(d, "Type was not found");
  7133. }
  7134. return type_m;
  7135. }
  7136. static void jsondec_any(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  7137. /* string type_url = 1;
  7138. * bytes value = 2; */
  7139. const upb_fielddef *value_f = upb_msgdef_itof(m, 2);
  7140. upb_msg *any_msg;
  7141. const upb_msgdef *any_m = NULL;
  7142. const char *pre_type_data = NULL;
  7143. const char *pre_type_end = NULL;
  7144. upb_msgval encoded;
  7145. jsondec_objstart(d);
  7146. /* Scan looking for "@type", which is not necessarily first. */
  7147. while (!any_m && jsondec_objnext(d)) {
  7148. const char *start = d->ptr;
  7149. upb_strview name = jsondec_string(d);
  7150. jsondec_entrysep(d);
  7151. if (jsondec_streql(name, "@type")) {
  7152. any_m = jsondec_typeurl(d, msg, m);
  7153. if (pre_type_data) {
  7154. pre_type_end = start;
  7155. while (*pre_type_end != ',') pre_type_end--;
  7156. }
  7157. } else {
  7158. if (!pre_type_data) pre_type_data = start;
  7159. jsondec_skipval(d);
  7160. }
  7161. }
  7162. if (!any_m) {
  7163. jsondec_err(d, "Any object didn't contain a '@type' field");
  7164. }
  7165. any_msg = upb_msg_new(any_m, d->arena);
  7166. if (pre_type_data) {
  7167. size_t len = pre_type_end - pre_type_data + 1;
  7168. char *tmp = upb_arena_malloc(d->arena, len);
  7169. const char *saved_ptr = d->ptr;
  7170. const char *saved_end = d->end;
  7171. memcpy(tmp, pre_type_data, len - 1);
  7172. tmp[len - 1] = '}';
  7173. d->ptr = tmp;
  7174. d->end = tmp + len;
  7175. d->is_first = true;
  7176. while (jsondec_objnext(d)) {
  7177. jsondec_anyfield(d, any_msg, any_m);
  7178. }
  7179. d->ptr = saved_ptr;
  7180. d->end = saved_end;
  7181. }
  7182. while (jsondec_objnext(d)) {
  7183. jsondec_anyfield(d, any_msg, any_m);
  7184. }
  7185. jsondec_objend(d);
  7186. encoded.str_val.data = upb_encode(any_msg, upb_msgdef_layout(any_m), d->arena,
  7187. &encoded.str_val.size);
  7188. upb_msg_set(msg, value_f, encoded, d->arena);
  7189. }
  7190. static void jsondec_wrapper(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  7191. const upb_fielddef *value_f = upb_msgdef_itof(m, 1);
  7192. upb_msgval val = jsondec_value(d, value_f);
  7193. upb_msg_set(msg, value_f, val, d->arena);
  7194. }
  7195. static void jsondec_wellknown(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  7196. switch (upb_msgdef_wellknowntype(m)) {
  7197. case UPB_WELLKNOWN_ANY:
  7198. jsondec_any(d, msg, m);
  7199. break;
  7200. case UPB_WELLKNOWN_FIELDMASK:
  7201. jsondec_fieldmask(d, msg, m);
  7202. break;
  7203. case UPB_WELLKNOWN_DURATION:
  7204. jsondec_duration(d, msg, m);
  7205. break;
  7206. case UPB_WELLKNOWN_TIMESTAMP:
  7207. jsondec_timestamp(d, msg, m);
  7208. break;
  7209. case UPB_WELLKNOWN_VALUE:
  7210. jsondec_wellknownvalue(d, msg, m);
  7211. break;
  7212. case UPB_WELLKNOWN_LISTVALUE:
  7213. jsondec_listvalue(d, msg, m);
  7214. break;
  7215. case UPB_WELLKNOWN_STRUCT:
  7216. jsondec_struct(d, msg, m);
  7217. break;
  7218. case UPB_WELLKNOWN_DOUBLEVALUE:
  7219. case UPB_WELLKNOWN_FLOATVALUE:
  7220. case UPB_WELLKNOWN_INT64VALUE:
  7221. case UPB_WELLKNOWN_UINT64VALUE:
  7222. case UPB_WELLKNOWN_INT32VALUE:
  7223. case UPB_WELLKNOWN_UINT32VALUE:
  7224. case UPB_WELLKNOWN_STRINGVALUE:
  7225. case UPB_WELLKNOWN_BYTESVALUE:
  7226. case UPB_WELLKNOWN_BOOLVALUE:
  7227. jsondec_wrapper(d, msg, m);
  7228. break;
  7229. default:
  7230. UPB_UNREACHABLE();
  7231. }
  7232. }
  7233. bool upb_json_decode(const char *buf, size_t size, upb_msg *msg,
  7234. const upb_msgdef *m, const upb_symtab *any_pool,
  7235. int options, upb_arena *arena, upb_status *status) {
  7236. jsondec d;
  7237. if (size == 0) return true;
  7238. d.ptr = buf;
  7239. d.end = buf + size;
  7240. d.arena = arena;
  7241. d.any_pool = any_pool;
  7242. d.status = status;
  7243. d.options = options;
  7244. d.depth = 64;
  7245. d.line = 1;
  7246. d.line_begin = d.ptr;
  7247. d.debug_field = NULL;
  7248. d.is_first = false;
  7249. if (UPB_SETJMP(d.err)) return false;
  7250. jsondec_tomsg(&d, msg, m);
  7251. return true;
  7252. }
  7253. /** upb/json_encode.c ************************************************************/
  7254. #include <ctype.h>
  7255. #include <float.h>
  7256. #include <inttypes.h>
  7257. #include <math.h>
  7258. #include <setjmp.h>
  7259. #include <stdarg.h>
  7260. #include <stdio.h>
  7261. #include <string.h>
  7262. /* Must be last. */
  7263. typedef struct {
  7264. char *buf, *ptr, *end;
  7265. size_t overflow;
  7266. int indent_depth;
  7267. int options;
  7268. const upb_symtab *ext_pool;
  7269. jmp_buf err;
  7270. upb_status *status;
  7271. upb_arena *arena;
  7272. } jsonenc;
  7273. static void jsonenc_msg(jsonenc *e, const upb_msg *msg, const upb_msgdef *m);
  7274. static void jsonenc_scalar(jsonenc *e, upb_msgval val, const upb_fielddef *f);
  7275. static void jsonenc_msgfield(jsonenc *e, const upb_msg *msg,
  7276. const upb_msgdef *m);
  7277. static void jsonenc_msgfields(jsonenc *e, const upb_msg *msg,
  7278. const upb_msgdef *m, bool first);
  7279. static void jsonenc_value(jsonenc *e, const upb_msg *msg, const upb_msgdef *m);
  7280. UPB_NORETURN static void jsonenc_err(jsonenc *e, const char *msg) {
  7281. upb_status_seterrmsg(e->status, msg);
  7282. longjmp(e->err, 1);
  7283. }
  7284. UPB_PRINTF(2, 3)
  7285. UPB_NORETURN static void jsonenc_errf(jsonenc *e, const char *fmt, ...) {
  7286. va_list argp;
  7287. va_start(argp, fmt);
  7288. upb_status_vseterrf(e->status, fmt, argp);
  7289. va_end(argp);
  7290. longjmp(e->err, 1);
  7291. }
  7292. static upb_arena *jsonenc_arena(jsonenc *e) {
  7293. /* Create lazily, since it's only needed for Any */
  7294. if (!e->arena) {
  7295. e->arena = upb_arena_new();
  7296. }
  7297. return e->arena;
  7298. }
  7299. static void jsonenc_putbytes(jsonenc *e, const void *data, size_t len) {
  7300. size_t have = e->end - e->ptr;
  7301. if (UPB_LIKELY(have >= len)) {
  7302. memcpy(e->ptr, data, len);
  7303. e->ptr += len;
  7304. } else {
  7305. if (have) {
  7306. memcpy(e->ptr, data, have);
  7307. e->ptr += have;
  7308. }
  7309. e->overflow += (len - have);
  7310. }
  7311. }
  7312. static void jsonenc_putstr(jsonenc *e, const char *str) {
  7313. jsonenc_putbytes(e, str, strlen(str));
  7314. }
  7315. UPB_PRINTF(2, 3)
  7316. static void jsonenc_printf(jsonenc *e, const char *fmt, ...) {
  7317. size_t n;
  7318. size_t have = e->end - e->ptr;
  7319. va_list args;
  7320. va_start(args, fmt);
  7321. n = vsnprintf(e->ptr, have, fmt, args);
  7322. va_end(args);
  7323. if (UPB_LIKELY(have > n)) {
  7324. e->ptr += n;
  7325. } else {
  7326. e->ptr = UPB_PTRADD(e->ptr, have);
  7327. e->overflow += (n - have);
  7328. }
  7329. }
  7330. static void jsonenc_nanos(jsonenc *e, int32_t nanos) {
  7331. int digits = 9;
  7332. if (nanos == 0) return;
  7333. if (nanos < 0 || nanos >= 1000000000) {
  7334. jsonenc_err(e, "error formatting timestamp as JSON: invalid nanos");
  7335. }
  7336. while (nanos % 1000 == 0) {
  7337. nanos /= 1000;
  7338. digits -= 3;
  7339. }
  7340. jsonenc_printf(e, ".%.*" PRId32, digits, nanos);
  7341. }
  7342. static void jsonenc_timestamp(jsonenc *e, const upb_msg *msg,
  7343. const upb_msgdef *m) {
  7344. const upb_fielddef *seconds_f = upb_msgdef_itof(m, 1);
  7345. const upb_fielddef *nanos_f = upb_msgdef_itof(m, 2);
  7346. int64_t seconds = upb_msg_get(msg, seconds_f).int64_val;
  7347. int32_t nanos = upb_msg_get(msg, nanos_f).int32_val;
  7348. int L, N, I, J, K, hour, min, sec;
  7349. if (seconds < -62135596800) {
  7350. jsonenc_err(e,
  7351. "error formatting timestamp as JSON: minimum acceptable value "
  7352. "is 0001-01-01T00:00:00Z");
  7353. } else if (seconds > 253402300799) {
  7354. jsonenc_err(e,
  7355. "error formatting timestamp as JSON: maximum acceptable value "
  7356. "is 9999-12-31T23:59:59Z");
  7357. }
  7358. /* Julian Day -> Y/M/D, Algorithm from:
  7359. * Fliegel, H. F., and Van Flandern, T. C., "A Machine Algorithm for
  7360. * Processing Calendar Dates," Communications of the Association of
  7361. * Computing Machines, vol. 11 (1968), p. 657. */
  7362. L = (int)(seconds / 86400) + 68569 + 2440588;
  7363. N = 4 * L / 146097;
  7364. L = L - (146097 * N + 3) / 4;
  7365. I = 4000 * (L + 1) / 1461001;
  7366. L = L - 1461 * I / 4 + 31;
  7367. J = 80 * L / 2447;
  7368. K = L - 2447 * J / 80;
  7369. L = J / 11;
  7370. J = J + 2 - 12 * L;
  7371. I = 100 * (N - 49) + I + L;
  7372. sec = seconds % 60;
  7373. min = (seconds / 60) % 60;
  7374. hour = (seconds / 3600) % 24;
  7375. jsonenc_printf(e, "\"%04d-%02d-%02dT%02d:%02d:%02d", I, J, K, hour, min, sec);
  7376. jsonenc_nanos(e, nanos);
  7377. jsonenc_putstr(e, "Z\"");
  7378. }
  7379. static void jsonenc_duration(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
  7380. const upb_fielddef *seconds_f = upb_msgdef_itof(m, 1);
  7381. const upb_fielddef *nanos_f = upb_msgdef_itof(m, 2);
  7382. int64_t seconds = upb_msg_get(msg, seconds_f).int64_val;
  7383. int32_t nanos = upb_msg_get(msg, nanos_f).int32_val;
  7384. if (seconds > 315576000000 || seconds < -315576000000 ||
  7385. (seconds < 0) != (nanos < 0)) {
  7386. jsonenc_err(e, "bad duration");
  7387. }
  7388. if (nanos < 0) {
  7389. nanos = -nanos;
  7390. }
  7391. jsonenc_printf(e, "\"%" PRId64, seconds);
  7392. jsonenc_nanos(e, nanos);
  7393. jsonenc_putstr(e, "s\"");
  7394. }
  7395. static void jsonenc_enum(int32_t val, const upb_fielddef *f, jsonenc *e) {
  7396. const upb_enumdef *e_def = upb_fielddef_enumsubdef(f);
  7397. if (strcmp(upb_enumdef_fullname(e_def), "google.protobuf.NullValue") == 0) {
  7398. jsonenc_putstr(e, "null");
  7399. } else {
  7400. const char *name = upb_enumdef_iton(e_def, val);
  7401. if (name) {
  7402. jsonenc_printf(e, "\"%s\"", name);
  7403. } else {
  7404. jsonenc_printf(e, "%" PRId32, val);
  7405. }
  7406. }
  7407. }
  7408. static void jsonenc_bytes(jsonenc *e, upb_strview str) {
  7409. /* This is the regular base64, not the "web-safe" version. */
  7410. static const char base64[] =
  7411. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  7412. const unsigned char *ptr = (unsigned char*)str.data;
  7413. const unsigned char *end = UPB_PTRADD(ptr, str.size);
  7414. char buf[4];
  7415. jsonenc_putstr(e, "\"");
  7416. while (end - ptr >= 3) {
  7417. buf[0] = base64[ptr[0] >> 2];
  7418. buf[1] = base64[((ptr[0] & 0x3) << 4) | (ptr[1] >> 4)];
  7419. buf[2] = base64[((ptr[1] & 0xf) << 2) | (ptr[2] >> 6)];
  7420. buf[3] = base64[ptr[2] & 0x3f];
  7421. jsonenc_putbytes(e, buf, 4);
  7422. ptr += 3;
  7423. }
  7424. switch (end - ptr) {
  7425. case 2:
  7426. buf[0] = base64[ptr[0] >> 2];
  7427. buf[1] = base64[((ptr[0] & 0x3) << 4) | (ptr[1] >> 4)];
  7428. buf[2] = base64[(ptr[1] & 0xf) << 2];
  7429. buf[3] = '=';
  7430. jsonenc_putbytes(e, buf, 4);
  7431. break;
  7432. case 1:
  7433. buf[0] = base64[ptr[0] >> 2];
  7434. buf[1] = base64[((ptr[0] & 0x3) << 4)];
  7435. buf[2] = '=';
  7436. buf[3] = '=';
  7437. jsonenc_putbytes(e, buf, 4);
  7438. break;
  7439. }
  7440. jsonenc_putstr(e, "\"");
  7441. }
  7442. static void jsonenc_stringbody(jsonenc *e, upb_strview str) {
  7443. const char *ptr = str.data;
  7444. const char *end = UPB_PTRADD(ptr, str.size);
  7445. while (ptr < end) {
  7446. switch (*ptr) {
  7447. case '\n':
  7448. jsonenc_putstr(e, "\\n");
  7449. break;
  7450. case '\r':
  7451. jsonenc_putstr(e, "\\r");
  7452. break;
  7453. case '\t':
  7454. jsonenc_putstr(e, "\\t");
  7455. break;
  7456. case '\"':
  7457. jsonenc_putstr(e, "\\\"");
  7458. break;
  7459. case '\f':
  7460. jsonenc_putstr(e, "\\f");
  7461. break;
  7462. case '\b':
  7463. jsonenc_putstr(e, "\\b");
  7464. break;
  7465. case '\\':
  7466. jsonenc_putstr(e, "\\\\");
  7467. break;
  7468. default:
  7469. if ((uint8_t)*ptr < 0x20) {
  7470. jsonenc_printf(e, "\\u%04x", (int)(uint8_t)*ptr);
  7471. } else {
  7472. /* This could be a non-ASCII byte. We rely on the string being valid
  7473. * UTF-8. */
  7474. jsonenc_putbytes(e, ptr, 1);
  7475. }
  7476. break;
  7477. }
  7478. ptr++;
  7479. }
  7480. }
  7481. static void jsonenc_string(jsonenc *e, upb_strview str) {
  7482. jsonenc_putstr(e, "\"");
  7483. jsonenc_stringbody(e, str);
  7484. jsonenc_putstr(e, "\"");
  7485. }
  7486. static void jsonenc_double(jsonenc *e, const char *fmt, double val) {
  7487. if (val == INFINITY) {
  7488. jsonenc_putstr(e, "\"Infinity\"");
  7489. } else if (val == -INFINITY) {
  7490. jsonenc_putstr(e, "\"-Infinity\"");
  7491. } else if (val != val) {
  7492. jsonenc_putstr(e, "\"NaN\"");
  7493. } else {
  7494. char *p = e->ptr;
  7495. jsonenc_printf(e, fmt, val);
  7496. /* printf() is dependent on locales; sadly there is no easy and portable way
  7497. * to avoid this. This little post-processing step will translate 1,2 -> 1.2
  7498. * since JSON needs the latter. Arguably a hack, but it is simple and the
  7499. * alternatives are far more complicated, platform-dependent, and/or larger
  7500. * in code size. */
  7501. for (char *end = e->ptr; p < end; p++) {
  7502. if (*p == ',') *p = '.';
  7503. }
  7504. }
  7505. }
  7506. static void jsonenc_wrapper(jsonenc *e, const upb_msg *msg,
  7507. const upb_msgdef *m) {
  7508. const upb_fielddef *val_f = upb_msgdef_itof(m, 1);
  7509. upb_msgval val = upb_msg_get(msg, val_f);
  7510. jsonenc_scalar(e, val, val_f);
  7511. }
  7512. static const upb_msgdef *jsonenc_getanymsg(jsonenc *e, upb_strview type_url) {
  7513. /* Find last '/', if any. */
  7514. const char *end = type_url.data + type_url.size;
  7515. const char *ptr = end;
  7516. const upb_msgdef *ret;
  7517. if (!e->ext_pool) {
  7518. jsonenc_err(e, "Tried to encode Any, but no symtab was provided");
  7519. }
  7520. if (type_url.size == 0) goto badurl;
  7521. while (true) {
  7522. if (--ptr == type_url.data) {
  7523. /* Type URL must contain at least one '/', with host before. */
  7524. goto badurl;
  7525. }
  7526. if (*ptr == '/') {
  7527. ptr++;
  7528. break;
  7529. }
  7530. }
  7531. ret = upb_symtab_lookupmsg2(e->ext_pool, ptr, end - ptr);
  7532. if (!ret) {
  7533. jsonenc_errf(e, "Couldn't find Any type: %.*s", (int)(end - ptr), ptr);
  7534. }
  7535. return ret;
  7536. badurl:
  7537. jsonenc_errf(
  7538. e, "Bad type URL: " UPB_STRVIEW_FORMAT, UPB_STRVIEW_ARGS(type_url));
  7539. }
  7540. static void jsonenc_any(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
  7541. const upb_fielddef *type_url_f = upb_msgdef_itof(m, 1);
  7542. const upb_fielddef *value_f = upb_msgdef_itof(m, 2);
  7543. upb_strview type_url = upb_msg_get(msg, type_url_f).str_val;
  7544. upb_strview value = upb_msg_get(msg, value_f).str_val;
  7545. const upb_msgdef *any_m = jsonenc_getanymsg(e, type_url);
  7546. const upb_msglayout *any_layout = upb_msgdef_layout(any_m);
  7547. upb_arena *arena = jsonenc_arena(e);
  7548. upb_msg *any = upb_msg_new(any_m, arena);
  7549. if (!upb_decode(value.data, value.size, any, any_layout, arena)) {
  7550. jsonenc_err(e, "Error decoding message in Any");
  7551. }
  7552. jsonenc_putstr(e, "{\"@type\":");
  7553. jsonenc_string(e, type_url);
  7554. if (upb_msgdef_wellknowntype(any_m) == UPB_WELLKNOWN_UNSPECIFIED) {
  7555. /* Regular messages: {"@type": "...","foo": 1, "bar": 2} */
  7556. jsonenc_msgfields(e, any, any_m, false);
  7557. } else {
  7558. /* Well-known type: {"@type": "...","value": <well-known encoding>} */
  7559. jsonenc_putstr(e, ",\"value\":");
  7560. jsonenc_msgfield(e, any, any_m);
  7561. }
  7562. jsonenc_putstr(e, "}");
  7563. }
  7564. static void jsonenc_putsep(jsonenc *e, const char *str, bool *first) {
  7565. if (*first) {
  7566. *first = false;
  7567. } else {
  7568. jsonenc_putstr(e, str);
  7569. }
  7570. }
  7571. static void jsonenc_fieldpath(jsonenc *e, upb_strview path) {
  7572. const char *ptr = path.data;
  7573. const char *end = ptr + path.size;
  7574. while (ptr < end) {
  7575. char ch = *ptr;
  7576. if (ch >= 'A' && ch <= 'Z') {
  7577. jsonenc_err(e, "Field mask element may not have upper-case letter.");
  7578. } else if (ch == '_') {
  7579. if (ptr == end - 1 || *(ptr + 1) < 'a' || *(ptr + 1) > 'z') {
  7580. jsonenc_err(e, "Underscore must be followed by a lowercase letter.");
  7581. }
  7582. ch = *++ptr - 32;
  7583. }
  7584. jsonenc_putbytes(e, &ch, 1);
  7585. ptr++;
  7586. }
  7587. }
  7588. static void jsonenc_fieldmask(jsonenc *e, const upb_msg *msg,
  7589. const upb_msgdef *m) {
  7590. const upb_fielddef *paths_f = upb_msgdef_itof(m, 1);
  7591. const upb_array *paths = upb_msg_get(msg, paths_f).array_val;
  7592. bool first = true;
  7593. size_t i, n = 0;
  7594. if (paths) n = upb_array_size(paths);
  7595. jsonenc_putstr(e, "\"");
  7596. for (i = 0; i < n; i++) {
  7597. jsonenc_putsep(e, ",", &first);
  7598. jsonenc_fieldpath(e, upb_array_get(paths, i).str_val);
  7599. }
  7600. jsonenc_putstr(e, "\"");
  7601. }
  7602. static void jsonenc_struct(jsonenc *e, const upb_msg *msg,
  7603. const upb_msgdef *m) {
  7604. const upb_fielddef *fields_f = upb_msgdef_itof(m, 1);
  7605. const upb_map *fields = upb_msg_get(msg, fields_f).map_val;
  7606. const upb_msgdef *entry_m = upb_fielddef_msgsubdef(fields_f);
  7607. const upb_fielddef *value_f = upb_msgdef_itof(entry_m, 2);
  7608. size_t iter = UPB_MAP_BEGIN;
  7609. bool first = true;
  7610. jsonenc_putstr(e, "{");
  7611. if (fields) {
  7612. while (upb_mapiter_next(fields, &iter)) {
  7613. upb_msgval key = upb_mapiter_key(fields, iter);
  7614. upb_msgval val = upb_mapiter_value(fields, iter);
  7615. jsonenc_putsep(e, ",", &first);
  7616. jsonenc_string(e, key.str_val);
  7617. jsonenc_putstr(e, ":");
  7618. jsonenc_value(e, val.msg_val, upb_fielddef_msgsubdef(value_f));
  7619. }
  7620. }
  7621. jsonenc_putstr(e, "}");
  7622. }
  7623. static void jsonenc_listvalue(jsonenc *e, const upb_msg *msg,
  7624. const upb_msgdef *m) {
  7625. const upb_fielddef *values_f = upb_msgdef_itof(m, 1);
  7626. const upb_msgdef *values_m = upb_fielddef_msgsubdef(values_f);
  7627. const upb_array *values = upb_msg_get(msg, values_f).array_val;
  7628. size_t i;
  7629. bool first = true;
  7630. jsonenc_putstr(e, "[");
  7631. if (values) {
  7632. const size_t size = upb_array_size(values);
  7633. for (i = 0; i < size; i++) {
  7634. upb_msgval elem = upb_array_get(values, i);
  7635. jsonenc_putsep(e, ",", &first);
  7636. jsonenc_value(e, elem.msg_val, values_m);
  7637. }
  7638. }
  7639. jsonenc_putstr(e, "]");
  7640. }
  7641. static void jsonenc_value(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
  7642. /* TODO(haberman): do we want a reflection method to get oneof case? */
  7643. size_t iter = UPB_MSG_BEGIN;
  7644. const upb_fielddef *f;
  7645. upb_msgval val;
  7646. if (!upb_msg_next(msg, m, NULL, &f, &val, &iter)) {
  7647. jsonenc_err(e, "No value set in Value proto");
  7648. }
  7649. switch (upb_fielddef_number(f)) {
  7650. case 1:
  7651. jsonenc_putstr(e, "null");
  7652. break;
  7653. case 2:
  7654. jsonenc_double(e, "%.17g", val.double_val);
  7655. break;
  7656. case 3:
  7657. jsonenc_string(e, val.str_val);
  7658. break;
  7659. case 4:
  7660. jsonenc_putstr(e, val.bool_val ? "true" : "false");
  7661. break;
  7662. case 5:
  7663. jsonenc_struct(e, val.msg_val, upb_fielddef_msgsubdef(f));
  7664. break;
  7665. case 6:
  7666. jsonenc_listvalue(e, val.msg_val, upb_fielddef_msgsubdef(f));
  7667. break;
  7668. }
  7669. }
  7670. static void jsonenc_msgfield(jsonenc *e, const upb_msg *msg,
  7671. const upb_msgdef *m) {
  7672. switch (upb_msgdef_wellknowntype(m)) {
  7673. case UPB_WELLKNOWN_UNSPECIFIED:
  7674. jsonenc_msg(e, msg, m);
  7675. break;
  7676. case UPB_WELLKNOWN_ANY:
  7677. jsonenc_any(e, msg, m);
  7678. break;
  7679. case UPB_WELLKNOWN_FIELDMASK:
  7680. jsonenc_fieldmask(e, msg, m);
  7681. break;
  7682. case UPB_WELLKNOWN_DURATION:
  7683. jsonenc_duration(e, msg, m);
  7684. break;
  7685. case UPB_WELLKNOWN_TIMESTAMP:
  7686. jsonenc_timestamp(e, msg, m);
  7687. break;
  7688. case UPB_WELLKNOWN_DOUBLEVALUE:
  7689. case UPB_WELLKNOWN_FLOATVALUE:
  7690. case UPB_WELLKNOWN_INT64VALUE:
  7691. case UPB_WELLKNOWN_UINT64VALUE:
  7692. case UPB_WELLKNOWN_INT32VALUE:
  7693. case UPB_WELLKNOWN_UINT32VALUE:
  7694. case UPB_WELLKNOWN_STRINGVALUE:
  7695. case UPB_WELLKNOWN_BYTESVALUE:
  7696. case UPB_WELLKNOWN_BOOLVALUE:
  7697. jsonenc_wrapper(e, msg, m);
  7698. break;
  7699. case UPB_WELLKNOWN_VALUE:
  7700. jsonenc_value(e, msg, m);
  7701. break;
  7702. case UPB_WELLKNOWN_LISTVALUE:
  7703. jsonenc_listvalue(e, msg, m);
  7704. break;
  7705. case UPB_WELLKNOWN_STRUCT:
  7706. jsonenc_struct(e, msg, m);
  7707. break;
  7708. }
  7709. }
  7710. static void jsonenc_scalar(jsonenc *e, upb_msgval val, const upb_fielddef *f) {
  7711. switch (upb_fielddef_type(f)) {
  7712. case UPB_TYPE_BOOL:
  7713. jsonenc_putstr(e, val.bool_val ? "true" : "false");
  7714. break;
  7715. case UPB_TYPE_FLOAT:
  7716. jsonenc_double(e, "%.9g", val.float_val);
  7717. break;
  7718. case UPB_TYPE_DOUBLE:
  7719. jsonenc_double(e, "%.17g", val.double_val);
  7720. break;
  7721. case UPB_TYPE_INT32:
  7722. jsonenc_printf(e, "%" PRId32, val.int32_val);
  7723. break;
  7724. case UPB_TYPE_UINT32:
  7725. jsonenc_printf(e, "%" PRIu32, val.uint32_val);
  7726. break;
  7727. case UPB_TYPE_INT64:
  7728. jsonenc_printf(e, "\"%" PRId64 "\"", val.int64_val);
  7729. break;
  7730. case UPB_TYPE_UINT64:
  7731. jsonenc_printf(e, "\"%" PRIu64 "\"", val.uint64_val);
  7732. break;
  7733. case UPB_TYPE_STRING:
  7734. jsonenc_string(e, val.str_val);
  7735. break;
  7736. case UPB_TYPE_BYTES:
  7737. jsonenc_bytes(e, val.str_val);
  7738. break;
  7739. case UPB_TYPE_ENUM:
  7740. jsonenc_enum(val.int32_val, f, e);
  7741. break;
  7742. case UPB_TYPE_MESSAGE:
  7743. jsonenc_msgfield(e, val.msg_val, upb_fielddef_msgsubdef(f));
  7744. break;
  7745. }
  7746. }
  7747. static void jsonenc_mapkey(jsonenc *e, upb_msgval val, const upb_fielddef *f) {
  7748. jsonenc_putstr(e, "\"");
  7749. switch (upb_fielddef_type(f)) {
  7750. case UPB_TYPE_BOOL:
  7751. jsonenc_putstr(e, val.bool_val ? "true" : "false");
  7752. break;
  7753. case UPB_TYPE_INT32:
  7754. jsonenc_printf(e, "%" PRId32, val.int32_val);
  7755. break;
  7756. case UPB_TYPE_UINT32:
  7757. jsonenc_printf(e, "%" PRIu32, val.uint32_val);
  7758. break;
  7759. case UPB_TYPE_INT64:
  7760. jsonenc_printf(e, "%" PRId64, val.int64_val);
  7761. break;
  7762. case UPB_TYPE_UINT64:
  7763. jsonenc_printf(e, "%" PRIu64, val.uint64_val);
  7764. break;
  7765. case UPB_TYPE_STRING:
  7766. jsonenc_stringbody(e, val.str_val);
  7767. break;
  7768. default:
  7769. UPB_UNREACHABLE();
  7770. }
  7771. jsonenc_putstr(e, "\":");
  7772. }
  7773. static void jsonenc_array(jsonenc *e, const upb_array *arr,
  7774. const upb_fielddef *f) {
  7775. size_t i;
  7776. size_t size = arr ? upb_array_size(arr) : 0;
  7777. bool first = true;
  7778. jsonenc_putstr(e, "[");
  7779. for (i = 0; i < size; i++) {
  7780. jsonenc_putsep(e, ",", &first);
  7781. jsonenc_scalar(e, upb_array_get(arr, i), f);
  7782. }
  7783. jsonenc_putstr(e, "]");
  7784. }
  7785. static void jsonenc_map(jsonenc *e, const upb_map *map, const upb_fielddef *f) {
  7786. const upb_msgdef *entry = upb_fielddef_msgsubdef(f);
  7787. const upb_fielddef *key_f = upb_msgdef_itof(entry, 1);
  7788. const upb_fielddef *val_f = upb_msgdef_itof(entry, 2);
  7789. size_t iter = UPB_MAP_BEGIN;
  7790. bool first = true;
  7791. jsonenc_putstr(e, "{");
  7792. if (map) {
  7793. while (upb_mapiter_next(map, &iter)) {
  7794. jsonenc_putsep(e, ",", &first);
  7795. jsonenc_mapkey(e, upb_mapiter_key(map, iter), key_f);
  7796. jsonenc_scalar(e, upb_mapiter_value(map, iter), val_f);
  7797. }
  7798. }
  7799. jsonenc_putstr(e, "}");
  7800. }
  7801. static void jsonenc_fieldval(jsonenc *e, const upb_fielddef *f,
  7802. upb_msgval val, bool *first) {
  7803. const char *name;
  7804. if (e->options & UPB_JSONENC_PROTONAMES) {
  7805. name = upb_fielddef_name(f);
  7806. } else {
  7807. name = upb_fielddef_jsonname(f);
  7808. }
  7809. jsonenc_putsep(e, ",", first);
  7810. jsonenc_printf(e, "\"%s\":", name);
  7811. if (upb_fielddef_ismap(f)) {
  7812. jsonenc_map(e, val.map_val, f);
  7813. } else if (upb_fielddef_isseq(f)) {
  7814. jsonenc_array(e, val.array_val, f);
  7815. } else {
  7816. jsonenc_scalar(e, val, f);
  7817. }
  7818. }
  7819. static void jsonenc_msgfields(jsonenc *e, const upb_msg *msg,
  7820. const upb_msgdef *m, bool first) {
  7821. upb_msgval val;
  7822. const upb_fielddef *f;
  7823. if (e->options & UPB_JSONENC_EMITDEFAULTS) {
  7824. /* Iterate over all fields. */
  7825. int i = 0;
  7826. int n = upb_msgdef_fieldcount(m);
  7827. for (i = 0; i < n; i++) {
  7828. f = upb_msgdef_field(m, i);
  7829. if (!upb_fielddef_haspresence(f) || upb_msg_has(msg, f)) {
  7830. jsonenc_fieldval(e, f, upb_msg_get(msg, f), &first);
  7831. }
  7832. }
  7833. } else {
  7834. /* Iterate over non-empty fields. */
  7835. size_t iter = UPB_MSG_BEGIN;
  7836. while (upb_msg_next(msg, m, e->ext_pool, &f, &val, &iter)) {
  7837. jsonenc_fieldval(e, f, val, &first);
  7838. }
  7839. }
  7840. }
  7841. static void jsonenc_msg(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
  7842. jsonenc_putstr(e, "{");
  7843. jsonenc_msgfields(e, msg, m, true);
  7844. jsonenc_putstr(e, "}");
  7845. }
  7846. static size_t jsonenc_nullz(jsonenc *e, size_t size) {
  7847. size_t ret = e->ptr - e->buf + e->overflow;
  7848. if (size > 0) {
  7849. if (e->ptr == e->end) e->ptr--;
  7850. *e->ptr = '\0';
  7851. }
  7852. return ret;
  7853. }
  7854. size_t upb_json_encode(const upb_msg *msg, const upb_msgdef *m,
  7855. const upb_symtab *ext_pool, int options, char *buf,
  7856. size_t size, upb_status *status) {
  7857. jsonenc e;
  7858. e.buf = buf;
  7859. e.ptr = buf;
  7860. e.end = UPB_PTRADD(buf, size);
  7861. e.overflow = 0;
  7862. e.options = options;
  7863. e.ext_pool = ext_pool;
  7864. e.status = status;
  7865. e.arena = NULL;
  7866. if (setjmp(e.err)) return -1;
  7867. jsonenc_msgfield(&e, msg, m);
  7868. if (e.arena) upb_arena_free(e.arena);
  7869. return jsonenc_nullz(&e, size);
  7870. }
  7871. /** upb/port_undef.inc ************************************************************/
  7872. /* See port_def.inc. This should #undef all macros #defined there. */
  7873. #undef UPB_SIZE
  7874. #undef UPB_PTR_AT
  7875. #undef UPB_READ_ONEOF
  7876. #undef UPB_WRITE_ONEOF
  7877. #undef UPB_MAPTYPE_STRING
  7878. #undef UPB_INLINE
  7879. #undef UPB_ALIGN_UP
  7880. #undef UPB_ALIGN_DOWN
  7881. #undef UPB_ALIGN_MALLOC
  7882. #undef UPB_ALIGN_OF
  7883. #undef UPB_LIKELY
  7884. #undef UPB_UNLIKELY
  7885. #undef UPB_FORCEINLINE
  7886. #undef UPB_NOINLINE
  7887. #undef UPB_NORETURN
  7888. #undef UPB_PRINTF
  7889. #undef UPB_MAX
  7890. #undef UPB_MIN
  7891. #undef UPB_UNUSED
  7892. #undef UPB_ASSUME
  7893. #undef UPB_ASSERT
  7894. #undef UPB_UNREACHABLE
  7895. #undef UPB_SETJMP
  7896. #undef UPB_LONGJMP
  7897. #undef UPB_PTRADD
  7898. #undef UPB_MUSTTAIL
  7899. #undef UPB_FASTTABLE_SUPPORTED
  7900. #undef UPB_FASTTABLE
  7901. #undef UPB_FASTTABLE_INIT
  7902. #undef UPB_POISON_MEMORY_REGION
  7903. #undef UPB_UNPOISON_MEMORY_REGION
  7904. #undef UPB_ASAN