php-upb.c 268 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136
  1. /* Amalgamated source file */
  2. #include "php-upb.h"
  3. /*
  4. * This is where we define macros used across upb.
  5. *
  6. * All of these macros are undef'd in port_undef.inc to avoid leaking them to
  7. * users.
  8. *
  9. * The correct usage is:
  10. *
  11. * #include "upb/foobar.h"
  12. * #include "upb/baz.h"
  13. *
  14. * // MUST be last included header.
  15. * #include "upb/port_def.inc"
  16. *
  17. * // Code for this file.
  18. * // <...>
  19. *
  20. * // Can be omitted for .c files, required for .h.
  21. * #include "upb/port_undef.inc"
  22. *
  23. * This file is private and must not be included by users!
  24. */
  25. #include <stdint.h>
  26. #include <stddef.h>
  27. #if UINTPTR_MAX == 0xffffffff
  28. #define UPB_SIZE(size32, size64) size32
  29. #else
  30. #define UPB_SIZE(size32, size64) size64
  31. #endif
  32. /* If we always read/write as a consistent type to each address, this shouldn't
  33. * violate aliasing.
  34. */
  35. #define UPB_PTR_AT(msg, ofs, type) ((type*)((char*)(msg) + (ofs)))
  36. #define UPB_READ_ONEOF(msg, fieldtype, offset, case_offset, case_val, default) \
  37. *UPB_PTR_AT(msg, case_offset, int) == case_val \
  38. ? *UPB_PTR_AT(msg, offset, fieldtype) \
  39. : default
  40. #define UPB_WRITE_ONEOF(msg, fieldtype, offset, value, case_offset, case_val) \
  41. *UPB_PTR_AT(msg, case_offset, int) = case_val; \
  42. *UPB_PTR_AT(msg, offset, fieldtype) = value;
  43. #define UPB_MAPTYPE_STRING 0
  44. /* UPB_INLINE: inline if possible, emit standalone code if required. */
  45. #ifdef __cplusplus
  46. #define UPB_INLINE inline
  47. #elif defined (__GNUC__) || defined(__clang__)
  48. #define UPB_INLINE static __inline__
  49. #else
  50. #define UPB_INLINE static
  51. #endif
  52. #define UPB_ALIGN_UP(size, align) (((size) + (align) - 1) / (align) * (align))
  53. #define UPB_ALIGN_DOWN(size, align) ((size) / (align) * (align))
  54. #define UPB_ALIGN_MALLOC(size) UPB_ALIGN_UP(size, 16)
  55. #define UPB_ALIGN_OF(type) offsetof (struct { char c; type member; }, member)
  56. /* Hints to the compiler about likely/unlikely branches. */
  57. #if defined (__GNUC__) || defined(__clang__)
  58. #define UPB_LIKELY(x) __builtin_expect((x),1)
  59. #define UPB_UNLIKELY(x) __builtin_expect((x),0)
  60. #else
  61. #define UPB_LIKELY(x) (x)
  62. #define UPB_UNLIKELY(x) (x)
  63. #endif
  64. /* Define UPB_BIG_ENDIAN manually if you're on big endian and your compiler
  65. * doesn't provide these preprocessor symbols. */
  66. #if defined(__BYTE_ORDER__) && (__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
  67. #define UPB_BIG_ENDIAN
  68. #endif
  69. /* Macros for function attributes on compilers that support them. */
  70. #ifdef __GNUC__
  71. #define UPB_FORCEINLINE __inline__ __attribute__((always_inline))
  72. #define UPB_NOINLINE __attribute__((noinline))
  73. #define UPB_NORETURN __attribute__((__noreturn__))
  74. #else /* !defined(__GNUC__) */
  75. #define UPB_FORCEINLINE
  76. #define UPB_NOINLINE
  77. #define UPB_NORETURN
  78. #endif
  79. #if __STDC_VERSION__ >= 199901L || __cplusplus >= 201103L
  80. /* C99/C++11 versions. */
  81. #include <stdio.h>
  82. #define _upb_snprintf snprintf
  83. #define _upb_vsnprintf vsnprintf
  84. #define _upb_va_copy(a, b) va_copy(a, b)
  85. #elif defined(_MSC_VER)
  86. /* Microsoft C/C++ versions. */
  87. #include <stdarg.h>
  88. #include <stdio.h>
  89. #if _MSC_VER < 1900
  90. int msvc_snprintf(char* s, size_t n, const char* format, ...);
  91. int msvc_vsnprintf(char* s, size_t n, const char* format, va_list arg);
  92. #define UPB_MSVC_VSNPRINTF
  93. #define _upb_snprintf msvc_snprintf
  94. #define _upb_vsnprintf msvc_vsnprintf
  95. #else
  96. #define _upb_snprintf snprintf
  97. #define _upb_vsnprintf vsnprintf
  98. #endif
  99. #define _upb_va_copy(a, b) va_copy(a, b)
  100. #elif defined __GNUC__
  101. /* A few hacky workarounds for functions not in C89.
  102. * For internal use only!
  103. * TODO(haberman): fix these by including our own implementations, or finding
  104. * another workaround.
  105. */
  106. #define _upb_snprintf __builtin_snprintf
  107. #define _upb_vsnprintf __builtin_vsnprintf
  108. #define _upb_va_copy(a, b) __va_copy(a, b)
  109. #else
  110. #error Need implementations of [v]snprintf and va_copy
  111. #endif
  112. #ifdef __cplusplus
  113. #if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__) || \
  114. (defined(_MSC_VER) && _MSC_VER >= 1900)
  115. /* C++11 is present */
  116. #else
  117. #error upb requires C++11 for C++ support
  118. #endif
  119. #endif
  120. #define UPB_MAX(x, y) ((x) > (y) ? (x) : (y))
  121. #define UPB_MIN(x, y) ((x) < (y) ? (x) : (y))
  122. #define UPB_UNUSED(var) (void)var
  123. /* UPB_ASSUME(): in release mode, we tell the compiler to assume this is true.
  124. */
  125. #ifdef NDEBUG
  126. #ifdef __GNUC__
  127. #define UPB_ASSUME(expr) if (!(expr)) __builtin_unreachable()
  128. #elif defined _MSC_VER
  129. #define UPB_ASSUME(expr) if (!(expr)) __assume(0)
  130. #else
  131. #define UPB_ASSUME(expr) do {} if (false && (expr))
  132. #endif
  133. #else
  134. #define UPB_ASSUME(expr) assert(expr)
  135. #endif
  136. /* UPB_ASSERT(): in release mode, we use the expression without letting it be
  137. * evaluated. This prevents "unused variable" warnings. */
  138. #ifdef NDEBUG
  139. #define UPB_ASSERT(expr) do {} while (false && (expr))
  140. #else
  141. #define UPB_ASSERT(expr) assert(expr)
  142. #endif
  143. /* UPB_ASSERT_DEBUGVAR(): assert that uses functions or variables that only
  144. * exist in debug mode. This turns into regular assert. */
  145. #define UPB_ASSERT_DEBUGVAR(expr) assert(expr)
  146. #if defined(__GNUC__) || defined(__clang__)
  147. #define UPB_UNREACHABLE() do { assert(0); __builtin_unreachable(); } while(0)
  148. #else
  149. #define UPB_UNREACHABLE() do { assert(0); } while(0)
  150. #endif
  151. /* UPB_INFINITY representing floating-point positive infinity. */
  152. #include <math.h>
  153. #ifdef INFINITY
  154. #define UPB_INFINITY INFINITY
  155. #else
  156. #define UPB_INFINITY (1.0 / 0.0)
  157. #endif
  158. #ifdef NAN
  159. #define UPB_NAN NAN
  160. #else
  161. #define UPB_NAN (0.0 / 0.0)
  162. #endif
  163. #include <setjmp.h>
  164. #include <string.h>
  165. /* Maps descriptor type -> upb field type. */
  166. static const uint8_t desctype_to_fieldtype[] = {
  167. -1, /* invalid descriptor type */
  168. UPB_TYPE_DOUBLE, /* DOUBLE */
  169. UPB_TYPE_FLOAT, /* FLOAT */
  170. UPB_TYPE_INT64, /* INT64 */
  171. UPB_TYPE_UINT64, /* UINT64 */
  172. UPB_TYPE_INT32, /* INT32 */
  173. UPB_TYPE_UINT64, /* FIXED64 */
  174. UPB_TYPE_UINT32, /* FIXED32 */
  175. UPB_TYPE_BOOL, /* BOOL */
  176. UPB_TYPE_STRING, /* STRING */
  177. UPB_TYPE_MESSAGE, /* GROUP */
  178. UPB_TYPE_MESSAGE, /* MESSAGE */
  179. UPB_TYPE_BYTES, /* BYTES */
  180. UPB_TYPE_UINT32, /* UINT32 */
  181. UPB_TYPE_ENUM, /* ENUM */
  182. UPB_TYPE_INT32, /* SFIXED32 */
  183. UPB_TYPE_INT64, /* SFIXED64 */
  184. UPB_TYPE_INT32, /* SINT32 */
  185. UPB_TYPE_INT64, /* SINT64 */
  186. };
  187. /* Maps descriptor type -> upb map size. */
  188. static const uint8_t desctype_to_mapsize[] = {
  189. -1, /* invalid descriptor type */
  190. 8, /* DOUBLE */
  191. 4, /* FLOAT */
  192. 8, /* INT64 */
  193. 8, /* UINT64 */
  194. 4, /* INT32 */
  195. 8, /* FIXED64 */
  196. 4, /* FIXED32 */
  197. 1, /* BOOL */
  198. UPB_MAPTYPE_STRING, /* STRING */
  199. sizeof(void *), /* GROUP */
  200. sizeof(void *), /* MESSAGE */
  201. UPB_MAPTYPE_STRING, /* BYTES */
  202. 4, /* UINT32 */
  203. 4, /* ENUM */
  204. 4, /* SFIXED32 */
  205. 8, /* SFIXED64 */
  206. 4, /* SINT32 */
  207. 8, /* SINT64 */
  208. };
  209. static const unsigned fixed32_ok = (1 << UPB_DTYPE_FLOAT) |
  210. (1 << UPB_DTYPE_FIXED32) |
  211. (1 << UPB_DTYPE_SFIXED32);
  212. static const unsigned fixed64_ok = (1 << UPB_DTYPE_DOUBLE) |
  213. (1 << UPB_DTYPE_FIXED64) |
  214. (1 << UPB_DTYPE_SFIXED64);
  215. /* Op: an action to be performed for a wire-type/field-type combination. */
  216. #define OP_SCALAR_LG2(n) (n) /* n in [0, 2, 3] => op in [0, 2, 3] */
  217. #define OP_STRING 4
  218. #define OP_BYTES 5
  219. #define OP_SUBMSG 6
  220. /* Ops above are scalar-only. Repeated fields can use any op. */
  221. #define OP_FIXPCK_LG2(n) (n + 5) /* n in [2, 3] => op in [7, 8] */
  222. #define OP_VARPCK_LG2(n) (n + 9) /* n in [0, 2, 3] => op in [9, 11, 12] */
  223. static const int8_t varint_ops[19] = {
  224. -1, /* field not found */
  225. -1, /* DOUBLE */
  226. -1, /* FLOAT */
  227. OP_SCALAR_LG2(3), /* INT64 */
  228. OP_SCALAR_LG2(3), /* UINT64 */
  229. OP_SCALAR_LG2(2), /* INT32 */
  230. -1, /* FIXED64 */
  231. -1, /* FIXED32 */
  232. OP_SCALAR_LG2(0), /* BOOL */
  233. -1, /* STRING */
  234. -1, /* GROUP */
  235. -1, /* MESSAGE */
  236. -1, /* BYTES */
  237. OP_SCALAR_LG2(2), /* UINT32 */
  238. OP_SCALAR_LG2(2), /* ENUM */
  239. -1, /* SFIXED32 */
  240. -1, /* SFIXED64 */
  241. OP_SCALAR_LG2(2), /* SINT32 */
  242. OP_SCALAR_LG2(3), /* SINT64 */
  243. };
  244. static const int8_t delim_ops[37] = {
  245. /* For non-repeated field type. */
  246. -1, /* field not found */
  247. -1, /* DOUBLE */
  248. -1, /* FLOAT */
  249. -1, /* INT64 */
  250. -1, /* UINT64 */
  251. -1, /* INT32 */
  252. -1, /* FIXED64 */
  253. -1, /* FIXED32 */
  254. -1, /* BOOL */
  255. OP_STRING, /* STRING */
  256. -1, /* GROUP */
  257. OP_SUBMSG, /* MESSAGE */
  258. OP_BYTES, /* BYTES */
  259. -1, /* UINT32 */
  260. -1, /* ENUM */
  261. -1, /* SFIXED32 */
  262. -1, /* SFIXED64 */
  263. -1, /* SINT32 */
  264. -1, /* SINT64 */
  265. /* For repeated field type. */
  266. OP_FIXPCK_LG2(3), /* REPEATED DOUBLE */
  267. OP_FIXPCK_LG2(2), /* REPEATED FLOAT */
  268. OP_VARPCK_LG2(3), /* REPEATED INT64 */
  269. OP_VARPCK_LG2(3), /* REPEATED UINT64 */
  270. OP_VARPCK_LG2(2), /* REPEATED INT32 */
  271. OP_FIXPCK_LG2(3), /* REPEATED FIXED64 */
  272. OP_FIXPCK_LG2(2), /* REPEATED FIXED32 */
  273. OP_VARPCK_LG2(0), /* REPEATED BOOL */
  274. OP_STRING, /* REPEATED STRING */
  275. OP_SUBMSG, /* REPEATED GROUP */
  276. OP_SUBMSG, /* REPEATED MESSAGE */
  277. OP_BYTES, /* REPEATED BYTES */
  278. OP_VARPCK_LG2(2), /* REPEATED UINT32 */
  279. OP_VARPCK_LG2(2), /* REPEATED ENUM */
  280. OP_FIXPCK_LG2(2), /* REPEATED SFIXED32 */
  281. OP_FIXPCK_LG2(3), /* REPEATED SFIXED64 */
  282. OP_VARPCK_LG2(2), /* REPEATED SINT32 */
  283. OP_VARPCK_LG2(3), /* REPEATED SINT64 */
  284. };
  285. /* Data pertaining to the parse. */
  286. typedef struct {
  287. const char *limit; /* End of delimited region or end of buffer. */
  288. upb_arena *arena;
  289. int depth;
  290. uint32_t end_group; /* Set to field number of END_GROUP tag, if any. */
  291. jmp_buf err;
  292. } upb_decstate;
  293. typedef union {
  294. bool bool_val;
  295. int32_t int32_val;
  296. int64_t int64_val;
  297. uint32_t uint32_val;
  298. uint64_t uint64_val;
  299. upb_strview str_val;
  300. } wireval;
  301. static const char *decode_msg(upb_decstate *d, const char *ptr, upb_msg *msg,
  302. const upb_msglayout *layout);
  303. UPB_NORETURN static void decode_err(upb_decstate *d) { longjmp(d->err, 1); }
  304. void decode_verifyutf8(upb_decstate *d, const char *buf, int len) {
  305. static const uint8_t utf8_offset[] = {
  306. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  307. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  308. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  309. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  310. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  311. 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  312. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  313. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  314. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  315. 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  316. 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0, 0,
  317. };
  318. int i, j;
  319. uint8_t offset;
  320. i = 0;
  321. while (i < len) {
  322. offset = utf8_offset[(uint8_t)buf[i]];
  323. if (offset == 0 || i + offset > len) {
  324. decode_err(d);
  325. }
  326. for (j = i + 1; j < i + offset; j++) {
  327. if ((buf[j] & 0xc0) != 0x80) {
  328. decode_err(d);
  329. }
  330. }
  331. i += offset;
  332. }
  333. if (i != len) decode_err(d);
  334. }
  335. static bool decode_reserve(upb_decstate *d, upb_array *arr, size_t elem) {
  336. bool need_realloc = arr->size - arr->len < elem;
  337. if (need_realloc && !_upb_array_realloc(arr, arr->len + elem, d->arena)) {
  338. decode_err(d);
  339. }
  340. return need_realloc;
  341. }
  342. UPB_NOINLINE
  343. static const char *decode_longvarint64(upb_decstate *d, const char *ptr,
  344. const char *limit, uint64_t *val) {
  345. uint8_t byte;
  346. int bitpos = 0;
  347. uint64_t out = 0;
  348. do {
  349. if (bitpos >= 70 || ptr == limit) decode_err(d);
  350. byte = *ptr;
  351. out |= (uint64_t)(byte & 0x7F) << bitpos;
  352. ptr++;
  353. bitpos += 7;
  354. } while (byte & 0x80);
  355. *val = out;
  356. return ptr;
  357. }
  358. UPB_FORCEINLINE
  359. static const char *decode_varint64(upb_decstate *d, const char *ptr,
  360. const char *limit, uint64_t *val) {
  361. if (UPB_LIKELY(ptr < limit && (*ptr & 0x80) == 0)) {
  362. *val = (uint8_t)*ptr;
  363. return ptr + 1;
  364. } else {
  365. return decode_longvarint64(d, ptr, limit, val);
  366. }
  367. }
  368. static const char *decode_varint32(upb_decstate *d, const char *ptr,
  369. const char *limit, uint32_t *val) {
  370. uint64_t u64;
  371. ptr = decode_varint64(d, ptr, limit, &u64);
  372. if (u64 > UINT32_MAX) decode_err(d);
  373. *val = (uint32_t)u64;
  374. return ptr;
  375. }
  376. static void decode_munge(int type, wireval *val) {
  377. switch (type) {
  378. case UPB_DESCRIPTOR_TYPE_BOOL:
  379. val->bool_val = val->uint64_val != 0;
  380. break;
  381. case UPB_DESCRIPTOR_TYPE_SINT32: {
  382. uint32_t n = val->uint32_val;
  383. val->int32_val = (n >> 1) ^ -(int32_t)(n & 1);
  384. break;
  385. }
  386. case UPB_DESCRIPTOR_TYPE_SINT64: {
  387. uint64_t n = val->uint64_val;
  388. val->int64_val = (n >> 1) ^ -(int64_t)(n & 1);
  389. break;
  390. }
  391. }
  392. }
  393. static const upb_msglayout_field *upb_find_field(const upb_msglayout *l,
  394. uint32_t field_number) {
  395. static upb_msglayout_field none = {0, 0, 0, 0, 0, 0};
  396. /* Lots of optimization opportunities here. */
  397. int i;
  398. if (l == NULL) return &none;
  399. for (i = 0; i < l->field_count; i++) {
  400. if (l->fields[i].number == field_number) {
  401. return &l->fields[i];
  402. }
  403. }
  404. return &none; /* Unknown field. */
  405. }
  406. static upb_msg *decode_newsubmsg(upb_decstate *d, const upb_msglayout *layout,
  407. const upb_msglayout_field *field) {
  408. const upb_msglayout *subl = layout->submsgs[field->submsg_index];
  409. return _upb_msg_new(subl, d->arena);
  410. }
  411. static void decode_tosubmsg(upb_decstate *d, upb_msg *submsg,
  412. const upb_msglayout *layout,
  413. const upb_msglayout_field *field, upb_strview val) {
  414. const upb_msglayout *subl = layout->submsgs[field->submsg_index];
  415. const char *saved_limit = d->limit;
  416. if (--d->depth < 0) decode_err(d);
  417. d->limit = val.data + val.size;
  418. decode_msg(d, val.data, submsg, subl);
  419. d->limit = saved_limit;
  420. if (d->end_group != 0) decode_err(d);
  421. d->depth++;
  422. }
  423. static const char *decode_group(upb_decstate *d, const char *ptr,
  424. upb_msg *submsg, const upb_msglayout *subl,
  425. uint32_t number) {
  426. if (--d->depth < 0) decode_err(d);
  427. ptr = decode_msg(d, ptr, submsg, subl);
  428. if (d->end_group != number) decode_err(d);
  429. d->end_group = 0;
  430. d->depth++;
  431. return ptr;
  432. }
  433. static const char *decode_togroup(upb_decstate *d, const char *ptr,
  434. upb_msg *submsg, const upb_msglayout *layout,
  435. const upb_msglayout_field *field) {
  436. const upb_msglayout *subl = layout->submsgs[field->submsg_index];
  437. return decode_group(d, ptr, submsg, subl, field->number);
  438. }
  439. static const char *decode_toarray(upb_decstate *d, const char *ptr,
  440. upb_msg *msg, const upb_msglayout *layout,
  441. const upb_msglayout_field *field, wireval val,
  442. int op) {
  443. upb_array **arrp = UPB_PTR_AT(msg, field->offset, void);
  444. upb_array *arr = *arrp;
  445. void *mem;
  446. if (!arr) {
  447. upb_fieldtype_t type = desctype_to_fieldtype[field->descriptortype];
  448. arr = _upb_array_new(d->arena, type);
  449. if (!arr) decode_err(d);
  450. *arrp = arr;
  451. }
  452. decode_reserve(d, arr, 1);
  453. switch (op) {
  454. case OP_SCALAR_LG2(0):
  455. case OP_SCALAR_LG2(2):
  456. case OP_SCALAR_LG2(3):
  457. /* Append scalar value. */
  458. mem = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << op, void);
  459. arr->len++;
  460. memcpy(mem, &val, 1 << op);
  461. return ptr;
  462. case OP_STRING:
  463. decode_verifyutf8(d, val.str_val.data, val.str_val.size);
  464. /* Fallthrough. */
  465. case OP_BYTES:
  466. /* Append bytes. */
  467. mem =
  468. UPB_PTR_AT(_upb_array_ptr(arr), arr->len * sizeof(upb_strview), void);
  469. arr->len++;
  470. memcpy(mem, &val, sizeof(upb_strview));
  471. return ptr;
  472. case OP_SUBMSG: {
  473. /* Append submessage / group. */
  474. upb_msg *submsg = decode_newsubmsg(d, layout, field);
  475. *UPB_PTR_AT(_upb_array_ptr(arr), arr->len * sizeof(void *), upb_msg *) =
  476. submsg;
  477. arr->len++;
  478. if (UPB_UNLIKELY(field->descriptortype == UPB_DTYPE_GROUP)) {
  479. ptr = decode_togroup(d, ptr, submsg, layout, field);
  480. } else {
  481. decode_tosubmsg(d, submsg, layout, field, val.str_val);
  482. }
  483. return ptr;
  484. }
  485. case OP_FIXPCK_LG2(2):
  486. case OP_FIXPCK_LG2(3): {
  487. /* Fixed packed. */
  488. int lg2 = op - OP_FIXPCK_LG2(0);
  489. int mask = (1 << lg2) - 1;
  490. size_t count = val.str_val.size >> lg2;
  491. if ((val.str_val.size & mask) != 0) {
  492. decode_err(d); /* Length isn't a round multiple of elem size. */
  493. }
  494. decode_reserve(d, arr, count);
  495. mem = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << lg2, void);
  496. arr->len += count;
  497. memcpy(mem, val.str_val.data, val.str_val.size);
  498. return ptr;
  499. }
  500. case OP_VARPCK_LG2(0):
  501. case OP_VARPCK_LG2(2):
  502. case OP_VARPCK_LG2(3): {
  503. /* Varint packed. */
  504. int lg2 = op - OP_VARPCK_LG2(0);
  505. int scale = 1 << lg2;
  506. const char *ptr = val.str_val.data;
  507. const char *end = ptr + val.str_val.size;
  508. char *out = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << lg2, void);
  509. while (ptr < end) {
  510. wireval elem;
  511. ptr = decode_varint64(d, ptr, end, &elem.uint64_val);
  512. decode_munge(field->descriptortype, &elem);
  513. if (decode_reserve(d, arr, 1)) {
  514. out = UPB_PTR_AT(_upb_array_ptr(arr), arr->len << lg2, void);
  515. }
  516. arr->len++;
  517. memcpy(out, &elem, scale);
  518. out += scale;
  519. }
  520. if (ptr != end) decode_err(d);
  521. return ptr;
  522. }
  523. default:
  524. UPB_UNREACHABLE();
  525. }
  526. }
  527. static void decode_tomap(upb_decstate *d, upb_msg *msg,
  528. const upb_msglayout *layout,
  529. const upb_msglayout_field *field, wireval val) {
  530. upb_map **map_p = UPB_PTR_AT(msg, field->offset, upb_map *);
  531. upb_map *map = *map_p;
  532. upb_map_entry ent;
  533. const upb_msglayout *entry = layout->submsgs[field->submsg_index];
  534. if (!map) {
  535. /* Lazily create map. */
  536. const upb_msglayout *entry = layout->submsgs[field->submsg_index];
  537. const upb_msglayout_field *key_field = &entry->fields[0];
  538. const upb_msglayout_field *val_field = &entry->fields[1];
  539. char key_size = desctype_to_mapsize[key_field->descriptortype];
  540. char val_size = desctype_to_mapsize[val_field->descriptortype];
  541. UPB_ASSERT(key_field->offset == 0);
  542. UPB_ASSERT(val_field->offset == sizeof(upb_strview));
  543. map = _upb_map_new(d->arena, key_size, val_size);
  544. *map_p = map;
  545. }
  546. /* Parse map entry. */
  547. memset(&ent, 0, sizeof(ent));
  548. if (entry->fields[1].descriptortype == UPB_DESCRIPTOR_TYPE_MESSAGE ||
  549. entry->fields[1].descriptortype == UPB_DESCRIPTOR_TYPE_GROUP) {
  550. /* Create proactively to handle the case where it doesn't appear. */
  551. ent.v.val.val = (uint64_t)_upb_msg_new(entry->submsgs[0], d->arena);
  552. }
  553. decode_tosubmsg(d, &ent.k, layout, field, val.str_val);
  554. /* Insert into map. */
  555. _upb_map_set(map, &ent.k, map->key_size, &ent.v, map->val_size, d->arena);
  556. }
  557. static const char *decode_tomsg(upb_decstate *d, const char *ptr, upb_msg *msg,
  558. const upb_msglayout *layout,
  559. const upb_msglayout_field *field, wireval val,
  560. int op) {
  561. void *mem = UPB_PTR_AT(msg, field->offset, void);
  562. int type = field->descriptortype;
  563. /* Set presence if necessary. */
  564. if (field->presence < 0) {
  565. /* Oneof case */
  566. uint32_t *oneof_case = _upb_oneofcase_field(msg, field);
  567. if (op == OP_SUBMSG && *oneof_case != field->number) {
  568. memset(mem, 0, sizeof(void*));
  569. }
  570. *oneof_case = field->number;
  571. } else if (field->presence > 0) {
  572. _upb_sethas_field(msg, field);
  573. }
  574. /* Store into message. */
  575. switch (op) {
  576. case OP_SUBMSG: {
  577. upb_msg **submsgp = mem;
  578. upb_msg *submsg = *submsgp;
  579. if (!submsg) {
  580. submsg = decode_newsubmsg(d, layout, field);
  581. *submsgp = submsg;
  582. }
  583. if (UPB_UNLIKELY(type == UPB_DTYPE_GROUP)) {
  584. ptr = decode_togroup(d, ptr, submsg, layout, field);
  585. } else {
  586. decode_tosubmsg(d, submsg, layout, field, val.str_val);
  587. }
  588. break;
  589. }
  590. case OP_STRING:
  591. decode_verifyutf8(d, val.str_val.data, val.str_val.size);
  592. /* Fallthrough. */
  593. case OP_BYTES:
  594. memcpy(mem, &val, sizeof(upb_strview));
  595. break;
  596. case OP_SCALAR_LG2(3):
  597. memcpy(mem, &val, 8);
  598. break;
  599. case OP_SCALAR_LG2(2):
  600. memcpy(mem, &val, 4);
  601. break;
  602. case OP_SCALAR_LG2(0):
  603. memcpy(mem, &val, 1);
  604. break;
  605. default:
  606. UPB_UNREACHABLE();
  607. }
  608. return ptr;
  609. }
  610. static const char *decode_msg(upb_decstate *d, const char *ptr, upb_msg *msg,
  611. const upb_msglayout *layout) {
  612. while (ptr < d->limit) {
  613. uint32_t tag;
  614. const upb_msglayout_field *field;
  615. int field_number;
  616. int wire_type;
  617. const char *field_start = ptr;
  618. wireval val;
  619. int op;
  620. ptr = decode_varint32(d, ptr, d->limit, &tag);
  621. field_number = tag >> 3;
  622. wire_type = tag & 7;
  623. field = upb_find_field(layout, field_number);
  624. switch (wire_type) {
  625. case UPB_WIRE_TYPE_VARINT:
  626. ptr = decode_varint64(d, ptr, d->limit, &val.uint64_val);
  627. op = varint_ops[field->descriptortype];
  628. decode_munge(field->descriptortype, &val);
  629. break;
  630. case UPB_WIRE_TYPE_32BIT:
  631. if (d->limit - ptr < 4) decode_err(d);
  632. memcpy(&val, ptr, 4);
  633. ptr += 4;
  634. op = OP_SCALAR_LG2(2);
  635. if (((1 << field->descriptortype) & fixed32_ok) == 0) goto unknown;
  636. break;
  637. case UPB_WIRE_TYPE_64BIT:
  638. if (d->limit - ptr < 8) decode_err(d);
  639. memcpy(&val, ptr, 8);
  640. ptr += 8;
  641. op = OP_SCALAR_LG2(3);
  642. if (((1 << field->descriptortype) & fixed64_ok) == 0) goto unknown;
  643. break;
  644. case UPB_WIRE_TYPE_DELIMITED: {
  645. uint32_t size;
  646. int ndx = field->descriptortype;
  647. if (_upb_isrepeated(field)) ndx += 18;
  648. ptr = decode_varint32(d, ptr, d->limit, &size);
  649. if (size >= INT32_MAX || (size_t)(d->limit - ptr) < size) {
  650. decode_err(d); /* Length overflow. */
  651. }
  652. val.str_val.data = ptr;
  653. val.str_val.size = size;
  654. ptr += size;
  655. op = delim_ops[ndx];
  656. break;
  657. }
  658. case UPB_WIRE_TYPE_START_GROUP:
  659. val.int32_val = field_number;
  660. op = OP_SUBMSG;
  661. if (field->descriptortype != UPB_DTYPE_GROUP) goto unknown;
  662. break;
  663. case UPB_WIRE_TYPE_END_GROUP:
  664. d->end_group = field_number;
  665. return ptr;
  666. default:
  667. decode_err(d);
  668. }
  669. if (op >= 0) {
  670. /* Parse, using op for dispatch. */
  671. switch (field->label) {
  672. case UPB_LABEL_REPEATED:
  673. case _UPB_LABEL_PACKED:
  674. ptr = decode_toarray(d, ptr, msg, layout, field, val, op);
  675. break;
  676. case _UPB_LABEL_MAP:
  677. decode_tomap(d, msg, layout, field, val);
  678. break;
  679. default:
  680. ptr = decode_tomsg(d, ptr, msg, layout, field, val, op);
  681. break;
  682. }
  683. } else {
  684. unknown:
  685. /* Skip unknown field. */
  686. if (field_number == 0) decode_err(d);
  687. if (wire_type == UPB_WIRE_TYPE_START_GROUP) {
  688. ptr = decode_group(d, ptr, NULL, NULL, field_number);
  689. }
  690. if (msg) {
  691. if (!_upb_msg_addunknown(msg, field_start, ptr - field_start,
  692. d->arena)) {
  693. decode_err(d);
  694. }
  695. }
  696. }
  697. }
  698. if (ptr != d->limit) decode_err(d);
  699. return ptr;
  700. }
  701. bool upb_decode(const char *buf, size_t size, void *msg, const upb_msglayout *l,
  702. upb_arena *arena) {
  703. upb_decstate state;
  704. state.limit = buf + size;
  705. state.arena = arena;
  706. state.depth = 64;
  707. state.end_group = 0;
  708. if (setjmp(state.err)) return false;
  709. if (size == 0) return true;
  710. decode_msg(&state, buf, msg, l);
  711. return state.end_group == 0;
  712. }
  713. #undef OP_SCALAR_LG2
  714. #undef OP_FIXPCK_LG2
  715. #undef OP_VARPCK_LG2
  716. #undef OP_STRING
  717. #undef OP_SUBMSG
  718. /* We encode backwards, to avoid pre-computing lengths (one-pass encode). */
  719. #include <string.h>
  720. #define UPB_PB_VARINT_MAX_LEN 10
  721. #define CHK(x) do { if (!(x)) { return false; } } while(0)
  722. static size_t upb_encode_varint(uint64_t val, char *buf) {
  723. size_t i;
  724. if (val < 128) { buf[0] = val; return 1; }
  725. i = 0;
  726. while (val) {
  727. uint8_t byte = val & 0x7fU;
  728. val >>= 7;
  729. if (val) byte |= 0x80U;
  730. buf[i++] = byte;
  731. }
  732. return i;
  733. }
  734. static uint32_t upb_zzencode_32(int32_t n) { return ((uint32_t)n << 1) ^ (n >> 31); }
  735. static uint64_t upb_zzencode_64(int64_t n) { return ((uint64_t)n << 1) ^ (n >> 63); }
  736. typedef struct {
  737. upb_alloc *alloc;
  738. char *buf, *ptr, *limit;
  739. } upb_encstate;
  740. static size_t upb_roundup_pow2(size_t bytes) {
  741. size_t ret = 128;
  742. while (ret < bytes) {
  743. ret *= 2;
  744. }
  745. return ret;
  746. }
  747. static bool upb_encode_growbuffer(upb_encstate *e, size_t bytes) {
  748. size_t old_size = e->limit - e->buf;
  749. size_t new_size = upb_roundup_pow2(bytes + (e->limit - e->ptr));
  750. char *new_buf = upb_realloc(e->alloc, e->buf, old_size, new_size);
  751. CHK(new_buf);
  752. /* We want previous data at the end, realloc() put it at the beginning. */
  753. if (old_size > 0) {
  754. memmove(new_buf + new_size - old_size, e->buf, old_size);
  755. }
  756. e->ptr = new_buf + new_size - (e->limit - e->ptr);
  757. e->limit = new_buf + new_size;
  758. e->buf = new_buf;
  759. return true;
  760. }
  761. /* Call to ensure that at least "bytes" bytes are available for writing at
  762. * e->ptr. Returns false if the bytes could not be allocated. */
  763. static bool upb_encode_reserve(upb_encstate *e, size_t bytes) {
  764. CHK(UPB_LIKELY((size_t)(e->ptr - e->buf) >= bytes) ||
  765. upb_encode_growbuffer(e, bytes));
  766. e->ptr -= bytes;
  767. return true;
  768. }
  769. /* Writes the given bytes to the buffer, handling reserve/advance. */
  770. static bool upb_put_bytes(upb_encstate *e, const void *data, size_t len) {
  771. if (len == 0) return true;
  772. CHK(upb_encode_reserve(e, len));
  773. memcpy(e->ptr, data, len);
  774. return true;
  775. }
  776. static bool upb_put_fixed64(upb_encstate *e, uint64_t val) {
  777. /* TODO(haberman): byte-swap for big endian. */
  778. return upb_put_bytes(e, &val, sizeof(uint64_t));
  779. }
  780. static bool upb_put_fixed32(upb_encstate *e, uint32_t val) {
  781. /* TODO(haberman): byte-swap for big endian. */
  782. return upb_put_bytes(e, &val, sizeof(uint32_t));
  783. }
  784. static bool upb_put_varint(upb_encstate *e, uint64_t val) {
  785. size_t len;
  786. char *start;
  787. CHK(upb_encode_reserve(e, UPB_PB_VARINT_MAX_LEN));
  788. len = upb_encode_varint(val, e->ptr);
  789. start = e->ptr + UPB_PB_VARINT_MAX_LEN - len;
  790. memmove(start, e->ptr, len);
  791. e->ptr = start;
  792. return true;
  793. }
  794. static bool upb_put_double(upb_encstate *e, double d) {
  795. uint64_t u64;
  796. UPB_ASSERT(sizeof(double) == sizeof(uint64_t));
  797. memcpy(&u64, &d, sizeof(uint64_t));
  798. return upb_put_fixed64(e, u64);
  799. }
  800. static bool upb_put_float(upb_encstate *e, float d) {
  801. uint32_t u32;
  802. UPB_ASSERT(sizeof(float) == sizeof(uint32_t));
  803. memcpy(&u32, &d, sizeof(uint32_t));
  804. return upb_put_fixed32(e, u32);
  805. }
  806. static bool upb_put_tag(upb_encstate *e, int field_number, int wire_type) {
  807. return upb_put_varint(e, (field_number << 3) | wire_type);
  808. }
  809. static bool upb_put_fixedarray(upb_encstate *e, const upb_array *arr,
  810. size_t elem_size, uint32_t tag) {
  811. size_t bytes = arr->len * elem_size;
  812. const char* data = _upb_array_constptr(arr);
  813. const char* ptr = data + bytes - elem_size;
  814. if (tag) {
  815. while (true) {
  816. CHK(upb_put_bytes(e, ptr, elem_size) && upb_put_varint(e, tag));
  817. if (ptr == data) break;
  818. ptr -= elem_size;
  819. }
  820. return true;
  821. } else {
  822. return upb_put_bytes(e, data, bytes) && upb_put_varint(e, bytes);
  823. }
  824. }
  825. bool upb_encode_message(upb_encstate *e, const char *msg,
  826. const upb_msglayout *m, size_t *size);
  827. static bool upb_encode_scalarfield(upb_encstate *e, const void *_field_mem,
  828. const upb_msglayout *m,
  829. const upb_msglayout_field *f,
  830. bool skip_zero_value) {
  831. const char *field_mem = _field_mem;
  832. #define CASE(ctype, type, wire_type, encodeval) do { \
  833. ctype val = *(ctype*)field_mem; \
  834. if (skip_zero_value && val == 0) { \
  835. return true; \
  836. } \
  837. return upb_put_ ## type(e, encodeval) && \
  838. upb_put_tag(e, f->number, wire_type); \
  839. } while(0)
  840. switch (f->descriptortype) {
  841. case UPB_DESCRIPTOR_TYPE_DOUBLE:
  842. CASE(double, double, UPB_WIRE_TYPE_64BIT, val);
  843. case UPB_DESCRIPTOR_TYPE_FLOAT:
  844. CASE(float, float, UPB_WIRE_TYPE_32BIT, val);
  845. case UPB_DESCRIPTOR_TYPE_INT64:
  846. case UPB_DESCRIPTOR_TYPE_UINT64:
  847. CASE(uint64_t, varint, UPB_WIRE_TYPE_VARINT, val);
  848. case UPB_DESCRIPTOR_TYPE_UINT32:
  849. CASE(uint32_t, varint, UPB_WIRE_TYPE_VARINT, val);
  850. case UPB_DESCRIPTOR_TYPE_INT32:
  851. case UPB_DESCRIPTOR_TYPE_ENUM:
  852. CASE(int32_t, varint, UPB_WIRE_TYPE_VARINT, (int64_t)val);
  853. case UPB_DESCRIPTOR_TYPE_SFIXED64:
  854. case UPB_DESCRIPTOR_TYPE_FIXED64:
  855. CASE(uint64_t, fixed64, UPB_WIRE_TYPE_64BIT, val);
  856. case UPB_DESCRIPTOR_TYPE_FIXED32:
  857. case UPB_DESCRIPTOR_TYPE_SFIXED32:
  858. CASE(uint32_t, fixed32, UPB_WIRE_TYPE_32BIT, val);
  859. case UPB_DESCRIPTOR_TYPE_BOOL:
  860. CASE(bool, varint, UPB_WIRE_TYPE_VARINT, val);
  861. case UPB_DESCRIPTOR_TYPE_SINT32:
  862. CASE(int32_t, varint, UPB_WIRE_TYPE_VARINT, upb_zzencode_32(val));
  863. case UPB_DESCRIPTOR_TYPE_SINT64:
  864. CASE(int64_t, varint, UPB_WIRE_TYPE_VARINT, upb_zzencode_64(val));
  865. case UPB_DESCRIPTOR_TYPE_STRING:
  866. case UPB_DESCRIPTOR_TYPE_BYTES: {
  867. upb_strview view = *(upb_strview*)field_mem;
  868. if (skip_zero_value && view.size == 0) {
  869. return true;
  870. }
  871. return upb_put_bytes(e, view.data, view.size) &&
  872. upb_put_varint(e, view.size) &&
  873. upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
  874. }
  875. case UPB_DESCRIPTOR_TYPE_GROUP: {
  876. size_t size;
  877. void *submsg = *(void **)field_mem;
  878. const upb_msglayout *subm = m->submsgs[f->submsg_index];
  879. if (submsg == NULL) {
  880. return true;
  881. }
  882. return upb_put_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP) &&
  883. upb_encode_message(e, submsg, subm, &size) &&
  884. upb_put_tag(e, f->number, UPB_WIRE_TYPE_START_GROUP);
  885. }
  886. case UPB_DESCRIPTOR_TYPE_MESSAGE: {
  887. size_t size;
  888. void *submsg = *(void **)field_mem;
  889. const upb_msglayout *subm = m->submsgs[f->submsg_index];
  890. if (submsg == NULL) {
  891. return true;
  892. }
  893. return upb_encode_message(e, submsg, subm, &size) &&
  894. upb_put_varint(e, size) &&
  895. upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
  896. }
  897. }
  898. #undef CASE
  899. UPB_UNREACHABLE();
  900. }
  901. static bool upb_encode_array(upb_encstate *e, const char *field_mem,
  902. const upb_msglayout *m,
  903. const upb_msglayout_field *f) {
  904. const upb_array *arr = *(const upb_array**)field_mem;
  905. bool packed = f->label == _UPB_LABEL_PACKED;
  906. if (arr == NULL || arr->len == 0) {
  907. return true;
  908. }
  909. #define VARINT_CASE(ctype, encode) \
  910. { \
  911. const ctype *start = _upb_array_constptr(arr); \
  912. const ctype *ptr = start + arr->len; \
  913. size_t pre_len = e->limit - e->ptr; \
  914. uint32_t tag = packed ? 0 : (f->number << 3) | UPB_WIRE_TYPE_VARINT; \
  915. do { \
  916. ptr--; \
  917. CHK(upb_put_varint(e, encode)); \
  918. if (tag) CHK(upb_put_varint(e, tag)); \
  919. } while (ptr != start); \
  920. if (!tag) CHK(upb_put_varint(e, e->limit - e->ptr - pre_len)); \
  921. } \
  922. break; \
  923. do { \
  924. ; \
  925. } while (0)
  926. #define TAG(wire_type) (packed ? 0 : (f->number << 3 | wire_type))
  927. switch (f->descriptortype) {
  928. case UPB_DESCRIPTOR_TYPE_DOUBLE:
  929. CHK(upb_put_fixedarray(e, arr, sizeof(double), TAG(UPB_WIRE_TYPE_64BIT)));
  930. break;
  931. case UPB_DESCRIPTOR_TYPE_FLOAT:
  932. CHK(upb_put_fixedarray(e, arr, sizeof(float), TAG(UPB_WIRE_TYPE_32BIT)));
  933. break;
  934. case UPB_DESCRIPTOR_TYPE_SFIXED64:
  935. case UPB_DESCRIPTOR_TYPE_FIXED64:
  936. CHK(upb_put_fixedarray(e, arr, sizeof(uint64_t), TAG(UPB_WIRE_TYPE_64BIT)));
  937. break;
  938. case UPB_DESCRIPTOR_TYPE_FIXED32:
  939. case UPB_DESCRIPTOR_TYPE_SFIXED32:
  940. CHK(upb_put_fixedarray(e, arr, sizeof(uint32_t), TAG(UPB_WIRE_TYPE_32BIT)));
  941. break;
  942. case UPB_DESCRIPTOR_TYPE_INT64:
  943. case UPB_DESCRIPTOR_TYPE_UINT64:
  944. VARINT_CASE(uint64_t, *ptr);
  945. case UPB_DESCRIPTOR_TYPE_UINT32:
  946. VARINT_CASE(uint32_t, *ptr);
  947. case UPB_DESCRIPTOR_TYPE_INT32:
  948. case UPB_DESCRIPTOR_TYPE_ENUM:
  949. VARINT_CASE(int32_t, (int64_t)*ptr);
  950. case UPB_DESCRIPTOR_TYPE_BOOL:
  951. VARINT_CASE(bool, *ptr);
  952. case UPB_DESCRIPTOR_TYPE_SINT32:
  953. VARINT_CASE(int32_t, upb_zzencode_32(*ptr));
  954. case UPB_DESCRIPTOR_TYPE_SINT64:
  955. VARINT_CASE(int64_t, upb_zzencode_64(*ptr));
  956. case UPB_DESCRIPTOR_TYPE_STRING:
  957. case UPB_DESCRIPTOR_TYPE_BYTES: {
  958. const upb_strview *start = _upb_array_constptr(arr);
  959. const upb_strview *ptr = start + arr->len;
  960. do {
  961. ptr--;
  962. CHK(upb_put_bytes(e, ptr->data, ptr->size) &&
  963. upb_put_varint(e, ptr->size) &&
  964. upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
  965. } while (ptr != start);
  966. return true;
  967. }
  968. case UPB_DESCRIPTOR_TYPE_GROUP: {
  969. const void *const*start = _upb_array_constptr(arr);
  970. const void *const*ptr = start + arr->len;
  971. const upb_msglayout *subm = m->submsgs[f->submsg_index];
  972. do {
  973. size_t size;
  974. ptr--;
  975. CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP) &&
  976. upb_encode_message(e, *ptr, subm, &size) &&
  977. upb_put_tag(e, f->number, UPB_WIRE_TYPE_START_GROUP));
  978. } while (ptr != start);
  979. return true;
  980. }
  981. case UPB_DESCRIPTOR_TYPE_MESSAGE: {
  982. const void *const*start = _upb_array_constptr(arr);
  983. const void *const*ptr = start + arr->len;
  984. const upb_msglayout *subm = m->submsgs[f->submsg_index];
  985. do {
  986. size_t size;
  987. ptr--;
  988. CHK(upb_encode_message(e, *ptr, subm, &size) &&
  989. upb_put_varint(e, size) &&
  990. upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
  991. } while (ptr != start);
  992. return true;
  993. }
  994. }
  995. #undef VARINT_CASE
  996. if (packed) {
  997. CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
  998. }
  999. return true;
  1000. }
  1001. static bool upb_encode_map(upb_encstate *e, const char *field_mem,
  1002. const upb_msglayout *m,
  1003. const upb_msglayout_field *f) {
  1004. const upb_map *map = *(const upb_map**)field_mem;
  1005. const upb_msglayout *entry = m->submsgs[f->submsg_index];
  1006. const upb_msglayout_field *key_field = &entry->fields[0];
  1007. const upb_msglayout_field *val_field = &entry->fields[1];
  1008. upb_strtable_iter i;
  1009. if (map == NULL) {
  1010. return true;
  1011. }
  1012. upb_strtable_begin(&i, &map->table);
  1013. for(; !upb_strtable_done(&i); upb_strtable_next(&i)) {
  1014. size_t pre_len = e->limit - e->ptr;
  1015. size_t size;
  1016. upb_strview key = upb_strtable_iter_key(&i);
  1017. const upb_value val = upb_strtable_iter_value(&i);
  1018. upb_map_entry ent;
  1019. _upb_map_fromkey(key, &ent.k, map->key_size);
  1020. _upb_map_fromvalue(val, &ent.v, map->val_size);
  1021. CHK(upb_encode_scalarfield(e, &ent.v, entry, val_field, false));
  1022. CHK(upb_encode_scalarfield(e, &ent.k, entry, key_field, false));
  1023. size = (e->limit - e->ptr) - pre_len;
  1024. CHK(upb_put_varint(e, size));
  1025. CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
  1026. }
  1027. return true;
  1028. }
  1029. bool upb_encode_message(upb_encstate *e, const char *msg,
  1030. const upb_msglayout *m, size_t *size) {
  1031. int i;
  1032. size_t pre_len = e->limit - e->ptr;
  1033. const char *unknown;
  1034. size_t unknown_size;
  1035. unknown = upb_msg_getunknown(msg, &unknown_size);
  1036. if (unknown) {
  1037. upb_put_bytes(e, unknown, unknown_size);
  1038. }
  1039. for (i = m->field_count - 1; i >= 0; i--) {
  1040. const upb_msglayout_field *f = &m->fields[i];
  1041. if (_upb_isrepeated(f)) {
  1042. CHK(upb_encode_array(e, msg + f->offset, m, f));
  1043. } else if (f->label == _UPB_LABEL_MAP) {
  1044. CHK(upb_encode_map(e, msg + f->offset, m, f));
  1045. } else {
  1046. bool skip_empty = false;
  1047. if (f->presence == 0) {
  1048. /* Proto3 presence. */
  1049. skip_empty = true;
  1050. } else if (f->presence > 0) {
  1051. /* Proto2 presence: hasbit. */
  1052. if (!_upb_hasbit_field(msg, f)) {
  1053. continue;
  1054. }
  1055. } else {
  1056. /* Field is in a oneof. */
  1057. if (_upb_getoneofcase_field(msg, f) != f->number) {
  1058. continue;
  1059. }
  1060. }
  1061. CHK(upb_encode_scalarfield(e, msg + f->offset, m, f, skip_empty));
  1062. }
  1063. }
  1064. *size = (e->limit - e->ptr) - pre_len;
  1065. return true;
  1066. }
  1067. char *upb_encode(const void *msg, const upb_msglayout *m, upb_arena *arena,
  1068. size_t *size) {
  1069. upb_encstate e;
  1070. e.alloc = upb_arena_alloc(arena);
  1071. e.buf = NULL;
  1072. e.limit = NULL;
  1073. e.ptr = NULL;
  1074. if (!upb_encode_message(&e, msg, m, size)) {
  1075. *size = 0;
  1076. return NULL;
  1077. }
  1078. *size = e.limit - e.ptr;
  1079. if (*size == 0) {
  1080. static char ch;
  1081. return &ch;
  1082. } else {
  1083. UPB_ASSERT(e.ptr);
  1084. return e.ptr;
  1085. }
  1086. }
  1087. #undef CHK
  1088. /** upb_msg *******************************************************************/
  1089. static const char _upb_fieldtype_to_sizelg2[12] = {
  1090. 0,
  1091. 0, /* UPB_TYPE_BOOL */
  1092. 2, /* UPB_TYPE_FLOAT */
  1093. 2, /* UPB_TYPE_INT32 */
  1094. 2, /* UPB_TYPE_UINT32 */
  1095. 2, /* UPB_TYPE_ENUM */
  1096. UPB_SIZE(2, 3), /* UPB_TYPE_MESSAGE */
  1097. 3, /* UPB_TYPE_DOUBLE */
  1098. 3, /* UPB_TYPE_INT64 */
  1099. 3, /* UPB_TYPE_UINT64 */
  1100. UPB_SIZE(3, 4), /* UPB_TYPE_STRING */
  1101. UPB_SIZE(3, 4), /* UPB_TYPE_BYTES */
  1102. };
  1103. static uintptr_t tag_arrptr(void* ptr, int elem_size_lg2) {
  1104. UPB_ASSERT(elem_size_lg2 <= 4);
  1105. return (uintptr_t)ptr | elem_size_lg2;
  1106. }
  1107. static int upb_msg_internalsize(const upb_msglayout *l) {
  1108. return sizeof(upb_msg_internal) - l->extendable * sizeof(void *);
  1109. }
  1110. static size_t upb_msg_sizeof(const upb_msglayout *l) {
  1111. return l->size + upb_msg_internalsize(l);
  1112. }
  1113. static const upb_msg_internal *upb_msg_getinternal_const(const upb_msg *msg) {
  1114. ptrdiff_t size = sizeof(upb_msg_internal);
  1115. return UPB_PTR_AT(msg, -size, upb_msg_internal);
  1116. }
  1117. static upb_msg_internal *upb_msg_getinternal(upb_msg *msg) {
  1118. return (upb_msg_internal*)upb_msg_getinternal_const(msg);
  1119. }
  1120. void _upb_msg_clear(upb_msg *msg, const upb_msglayout *l) {
  1121. ptrdiff_t internal = upb_msg_internalsize(l);
  1122. void *mem = UPB_PTR_AT(msg, -internal, char);
  1123. memset(mem, 0, l->size + internal);
  1124. }
  1125. upb_msg *_upb_msg_new(const upb_msglayout *l, upb_arena *a) {
  1126. void *mem = upb_arena_malloc(a, upb_msg_sizeof(l));
  1127. upb_msg *msg;
  1128. if (!mem) {
  1129. return NULL;
  1130. }
  1131. msg = UPB_PTR_AT(mem, upb_msg_internalsize(l), upb_msg);
  1132. _upb_msg_clear(msg, l);
  1133. return msg;
  1134. }
  1135. bool _upb_msg_addunknown(upb_msg *msg, const char *data, size_t len,
  1136. upb_arena *arena) {
  1137. upb_msg_internal *in = upb_msg_getinternal(msg);
  1138. if (len > in->unknown_size - in->unknown_len) {
  1139. upb_alloc *alloc = upb_arena_alloc(arena);
  1140. size_t need = in->unknown_size + len;
  1141. size_t newsize = UPB_MAX(in->unknown_size * 2, need);
  1142. void *mem = upb_realloc(alloc, in->unknown, in->unknown_size, newsize);
  1143. if (!mem) return false;
  1144. in->unknown = mem;
  1145. in->unknown_size = newsize;
  1146. }
  1147. memcpy(in->unknown + in->unknown_len, data, len);
  1148. in->unknown_len += len;
  1149. return true;
  1150. }
  1151. void _upb_msg_discardunknown_shallow(upb_msg *msg) {
  1152. upb_msg_internal *in = upb_msg_getinternal(msg);
  1153. in->unknown_len = 0;
  1154. }
  1155. const char *upb_msg_getunknown(const upb_msg *msg, size_t *len) {
  1156. const upb_msg_internal *in = upb_msg_getinternal_const(msg);
  1157. *len = in->unknown_len;
  1158. return in->unknown;
  1159. }
  1160. /** upb_array *****************************************************************/
  1161. upb_array *_upb_array_new(upb_arena *a, upb_fieldtype_t type) {
  1162. upb_array *arr = upb_arena_malloc(a, sizeof(upb_array));
  1163. if (!arr) {
  1164. return NULL;
  1165. }
  1166. arr->data = tag_arrptr(NULL, _upb_fieldtype_to_sizelg2[type]);
  1167. arr->len = 0;
  1168. arr->size = 0;
  1169. return arr;
  1170. }
  1171. bool _upb_array_realloc(upb_array *arr, size_t min_size, upb_arena *arena) {
  1172. size_t new_size = UPB_MAX(arr->size, 4);
  1173. int elem_size_lg2 = arr->data & 7;
  1174. size_t old_bytes = arr->size << elem_size_lg2;
  1175. size_t new_bytes;
  1176. void* ptr = _upb_array_ptr(arr);
  1177. /* Log2 ceiling of size. */
  1178. while (new_size < min_size) new_size *= 2;
  1179. new_bytes = new_size << elem_size_lg2;
  1180. ptr = upb_arena_realloc(arena, ptr, old_bytes, new_bytes);
  1181. if (!ptr) {
  1182. return false;
  1183. }
  1184. arr->data = tag_arrptr(ptr, elem_size_lg2);
  1185. arr->size = new_size;
  1186. return true;
  1187. }
  1188. static upb_array *getorcreate_array(upb_array **arr_ptr, upb_fieldtype_t type,
  1189. upb_arena *arena) {
  1190. upb_array *arr = *arr_ptr;
  1191. if (!arr) {
  1192. arr = _upb_array_new(arena, type);
  1193. if (!arr) return NULL;
  1194. *arr_ptr = arr;
  1195. }
  1196. return arr;
  1197. }
  1198. void *_upb_array_resize_fallback(upb_array **arr_ptr, size_t size,
  1199. upb_fieldtype_t type, upb_arena *arena) {
  1200. upb_array *arr = getorcreate_array(arr_ptr, type, arena);
  1201. return arr && _upb_array_resize(arr, size, arena) ? _upb_array_ptr(arr) : NULL;
  1202. }
  1203. bool _upb_array_append_fallback(upb_array **arr_ptr, const void *value,
  1204. upb_fieldtype_t type, upb_arena *arena) {
  1205. upb_array *arr = getorcreate_array(arr_ptr, type, arena);
  1206. size_t elem = arr->len;
  1207. int lg2 = _upb_fieldtype_to_sizelg2[type];
  1208. char *data;
  1209. if (!arr || !_upb_array_resize(arr, elem + 1, arena)) return false;
  1210. data = _upb_array_ptr(arr);
  1211. memcpy(data + (elem << lg2), value, 1 << lg2);
  1212. return true;
  1213. }
  1214. /** upb_map *******************************************************************/
  1215. upb_map *_upb_map_new(upb_arena *a, size_t key_size, size_t value_size) {
  1216. upb_map *map = upb_arena_malloc(a, sizeof(upb_map));
  1217. if (!map) {
  1218. return NULL;
  1219. }
  1220. upb_strtable_init2(&map->table, UPB_CTYPE_INT32, upb_arena_alloc(a));
  1221. map->key_size = key_size;
  1222. map->val_size = value_size;
  1223. return map;
  1224. }
  1225. /*
  1226. ** upb_table Implementation
  1227. **
  1228. ** Implementation is heavily inspired by Lua's ltable.c.
  1229. */
  1230. #include <string.h>
  1231. #define UPB_MAXARRSIZE 16 /* 64k. */
  1232. /* From Chromium. */
  1233. #define ARRAY_SIZE(x) \
  1234. ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
  1235. static const double MAX_LOAD = 0.85;
  1236. /* The minimum utilization of the array part of a mixed hash/array table. This
  1237. * is a speed/memory-usage tradeoff (though it's not straightforward because of
  1238. * cache effects). The lower this is, the more memory we'll use. */
  1239. static const double MIN_DENSITY = 0.1;
  1240. bool is_pow2(uint64_t v) { return v == 0 || (v & (v - 1)) == 0; }
  1241. int log2ceil(uint64_t v) {
  1242. int ret = 0;
  1243. bool pow2 = is_pow2(v);
  1244. while (v >>= 1) ret++;
  1245. ret = pow2 ? ret : ret + 1; /* Ceiling. */
  1246. return UPB_MIN(UPB_MAXARRSIZE, ret);
  1247. }
  1248. char *upb_strdup(const char *s, upb_alloc *a) {
  1249. return upb_strdup2(s, strlen(s), a);
  1250. }
  1251. char *upb_strdup2(const char *s, size_t len, upb_alloc *a) {
  1252. size_t n;
  1253. char *p;
  1254. /* Prevent overflow errors. */
  1255. if (len == SIZE_MAX) return NULL;
  1256. /* Always null-terminate, even if binary data; but don't rely on the input to
  1257. * have a null-terminating byte since it may be a raw binary buffer. */
  1258. n = len + 1;
  1259. p = upb_malloc(a, n);
  1260. if (p) {
  1261. memcpy(p, s, len);
  1262. p[len] = 0;
  1263. }
  1264. return p;
  1265. }
  1266. /* A type to represent the lookup key of either a strtable or an inttable. */
  1267. typedef union {
  1268. uintptr_t num;
  1269. struct {
  1270. const char *str;
  1271. size_t len;
  1272. } str;
  1273. } lookupkey_t;
  1274. static lookupkey_t strkey2(const char *str, size_t len) {
  1275. lookupkey_t k;
  1276. k.str.str = str;
  1277. k.str.len = len;
  1278. return k;
  1279. }
  1280. static lookupkey_t intkey(uintptr_t key) {
  1281. lookupkey_t k;
  1282. k.num = key;
  1283. return k;
  1284. }
  1285. typedef uint32_t hashfunc_t(upb_tabkey key);
  1286. typedef bool eqlfunc_t(upb_tabkey k1, lookupkey_t k2);
  1287. /* Base table (shared code) ***************************************************/
  1288. /* For when we need to cast away const. */
  1289. static upb_tabent *mutable_entries(upb_table *t) {
  1290. return (upb_tabent*)t->entries;
  1291. }
  1292. static bool isfull(upb_table *t) {
  1293. if (upb_table_size(t) == 0) {
  1294. return true;
  1295. } else {
  1296. return ((double)(t->count + 1) / upb_table_size(t)) > MAX_LOAD;
  1297. }
  1298. }
  1299. static bool init(upb_table *t, uint8_t size_lg2, upb_alloc *a) {
  1300. size_t bytes;
  1301. t->count = 0;
  1302. t->size_lg2 = size_lg2;
  1303. t->mask = upb_table_size(t) ? upb_table_size(t) - 1 : 0;
  1304. bytes = upb_table_size(t) * sizeof(upb_tabent);
  1305. if (bytes > 0) {
  1306. t->entries = upb_malloc(a, bytes);
  1307. if (!t->entries) return false;
  1308. memset(mutable_entries(t), 0, bytes);
  1309. } else {
  1310. t->entries = NULL;
  1311. }
  1312. return true;
  1313. }
  1314. static void uninit(upb_table *t, upb_alloc *a) {
  1315. upb_free(a, mutable_entries(t));
  1316. }
  1317. static upb_tabent *emptyent(upb_table *t) {
  1318. upb_tabent *e = mutable_entries(t) + upb_table_size(t);
  1319. while (1) { if (upb_tabent_isempty(--e)) return e; UPB_ASSERT(e > t->entries); }
  1320. }
  1321. static upb_tabent *getentry_mutable(upb_table *t, uint32_t hash) {
  1322. return (upb_tabent*)upb_getentry(t, hash);
  1323. }
  1324. static const upb_tabent *findentry(const upb_table *t, lookupkey_t key,
  1325. uint32_t hash, eqlfunc_t *eql) {
  1326. const upb_tabent *e;
  1327. if (t->size_lg2 == 0) return NULL;
  1328. e = upb_getentry(t, hash);
  1329. if (upb_tabent_isempty(e)) return NULL;
  1330. while (1) {
  1331. if (eql(e->key, key)) return e;
  1332. if ((e = e->next) == NULL) return NULL;
  1333. }
  1334. }
  1335. static upb_tabent *findentry_mutable(upb_table *t, lookupkey_t key,
  1336. uint32_t hash, eqlfunc_t *eql) {
  1337. return (upb_tabent*)findentry(t, key, hash, eql);
  1338. }
  1339. static bool lookup(const upb_table *t, lookupkey_t key, upb_value *v,
  1340. uint32_t hash, eqlfunc_t *eql) {
  1341. const upb_tabent *e = findentry(t, key, hash, eql);
  1342. if (e) {
  1343. if (v) {
  1344. _upb_value_setval(v, e->val.val);
  1345. }
  1346. return true;
  1347. } else {
  1348. return false;
  1349. }
  1350. }
  1351. /* The given key must not already exist in the table. */
  1352. static void insert(upb_table *t, lookupkey_t key, upb_tabkey tabkey,
  1353. upb_value val, uint32_t hash,
  1354. hashfunc_t *hashfunc, eqlfunc_t *eql) {
  1355. upb_tabent *mainpos_e;
  1356. upb_tabent *our_e;
  1357. UPB_ASSERT(findentry(t, key, hash, eql) == NULL);
  1358. t->count++;
  1359. mainpos_e = getentry_mutable(t, hash);
  1360. our_e = mainpos_e;
  1361. if (upb_tabent_isempty(mainpos_e)) {
  1362. /* Our main position is empty; use it. */
  1363. our_e->next = NULL;
  1364. } else {
  1365. /* Collision. */
  1366. upb_tabent *new_e = emptyent(t);
  1367. /* Head of collider's chain. */
  1368. upb_tabent *chain = getentry_mutable(t, hashfunc(mainpos_e->key));
  1369. if (chain == mainpos_e) {
  1370. /* Existing ent is in its main posisiton (it has the same hash as us, and
  1371. * is the head of our chain). Insert to new ent and append to this chain. */
  1372. new_e->next = mainpos_e->next;
  1373. mainpos_e->next = new_e;
  1374. our_e = new_e;
  1375. } else {
  1376. /* Existing ent is not in its main position (it is a node in some other
  1377. * chain). This implies that no existing ent in the table has our hash.
  1378. * Evict it (updating its chain) and use its ent for head of our chain. */
  1379. *new_e = *mainpos_e; /* copies next. */
  1380. while (chain->next != mainpos_e) {
  1381. chain = (upb_tabent*)chain->next;
  1382. UPB_ASSERT(chain);
  1383. }
  1384. chain->next = new_e;
  1385. our_e = mainpos_e;
  1386. our_e->next = NULL;
  1387. }
  1388. }
  1389. our_e->key = tabkey;
  1390. our_e->val.val = val.val;
  1391. UPB_ASSERT(findentry(t, key, hash, eql) == our_e);
  1392. }
  1393. static bool rm(upb_table *t, lookupkey_t key, upb_value *val,
  1394. upb_tabkey *removed, uint32_t hash, eqlfunc_t *eql) {
  1395. upb_tabent *chain = getentry_mutable(t, hash);
  1396. if (upb_tabent_isempty(chain)) return false;
  1397. if (eql(chain->key, key)) {
  1398. /* Element to remove is at the head of its chain. */
  1399. t->count--;
  1400. if (val) _upb_value_setval(val, chain->val.val);
  1401. if (removed) *removed = chain->key;
  1402. if (chain->next) {
  1403. upb_tabent *move = (upb_tabent*)chain->next;
  1404. *chain = *move;
  1405. move->key = 0; /* Make the slot empty. */
  1406. } else {
  1407. chain->key = 0; /* Make the slot empty. */
  1408. }
  1409. return true;
  1410. } else {
  1411. /* Element to remove is either in a non-head position or not in the
  1412. * table. */
  1413. while (chain->next && !eql(chain->next->key, key)) {
  1414. chain = (upb_tabent*)chain->next;
  1415. }
  1416. if (chain->next) {
  1417. /* Found element to remove. */
  1418. upb_tabent *rm = (upb_tabent*)chain->next;
  1419. t->count--;
  1420. if (val) _upb_value_setval(val, chain->next->val.val);
  1421. if (removed) *removed = rm->key;
  1422. rm->key = 0; /* Make the slot empty. */
  1423. chain->next = rm->next;
  1424. return true;
  1425. } else {
  1426. /* Element to remove is not in the table. */
  1427. return false;
  1428. }
  1429. }
  1430. }
  1431. static size_t next(const upb_table *t, size_t i) {
  1432. do {
  1433. if (++i >= upb_table_size(t))
  1434. return SIZE_MAX - 1; /* Distinct from -1. */
  1435. } while(upb_tabent_isempty(&t->entries[i]));
  1436. return i;
  1437. }
  1438. static size_t begin(const upb_table *t) {
  1439. return next(t, -1);
  1440. }
  1441. /* upb_strtable ***************************************************************/
  1442. /* A simple "subclass" of upb_table that only adds a hash function for strings. */
  1443. static upb_tabkey strcopy(lookupkey_t k2, upb_alloc *a) {
  1444. uint32_t len = (uint32_t) k2.str.len;
  1445. char *str = upb_malloc(a, k2.str.len + sizeof(uint32_t) + 1);
  1446. if (str == NULL) return 0;
  1447. memcpy(str, &len, sizeof(uint32_t));
  1448. if (k2.str.len) memcpy(str + sizeof(uint32_t), k2.str.str, k2.str.len);
  1449. str[sizeof(uint32_t) + k2.str.len] = '\0';
  1450. return (uintptr_t)str;
  1451. }
  1452. static uint32_t strhash(upb_tabkey key) {
  1453. uint32_t len;
  1454. char *str = upb_tabstr(key, &len);
  1455. return upb_murmur_hash2(str, len, 0);
  1456. }
  1457. static bool streql(upb_tabkey k1, lookupkey_t k2) {
  1458. uint32_t len;
  1459. char *str = upb_tabstr(k1, &len);
  1460. return len == k2.str.len && (len == 0 || memcmp(str, k2.str.str, len) == 0);
  1461. }
  1462. bool upb_strtable_init2(upb_strtable *t, upb_ctype_t ctype, upb_alloc *a) {
  1463. UPB_UNUSED(ctype); /* TODO(haberman): rm */
  1464. return init(&t->t, 2, a);
  1465. }
  1466. void upb_strtable_clear(upb_strtable *t) {
  1467. size_t bytes = upb_table_size(&t->t) * sizeof(upb_tabent);
  1468. t->t.count = 0;
  1469. memset((char*)t->t.entries, 0, bytes);
  1470. }
  1471. void upb_strtable_uninit2(upb_strtable *t, upb_alloc *a) {
  1472. size_t i;
  1473. for (i = 0; i < upb_table_size(&t->t); i++)
  1474. upb_free(a, (void*)t->t.entries[i].key);
  1475. uninit(&t->t, a);
  1476. }
  1477. bool upb_strtable_resize(upb_strtable *t, size_t size_lg2, upb_alloc *a) {
  1478. upb_strtable new_table;
  1479. upb_strtable_iter i;
  1480. if (!init(&new_table.t, size_lg2, a))
  1481. return false;
  1482. upb_strtable_begin(&i, t);
  1483. for ( ; !upb_strtable_done(&i); upb_strtable_next(&i)) {
  1484. upb_strview key = upb_strtable_iter_key(&i);
  1485. upb_strtable_insert3(
  1486. &new_table, key.data, key.size,
  1487. upb_strtable_iter_value(&i), a);
  1488. }
  1489. upb_strtable_uninit2(t, a);
  1490. *t = new_table;
  1491. return true;
  1492. }
  1493. bool upb_strtable_insert3(upb_strtable *t, const char *k, size_t len,
  1494. upb_value v, upb_alloc *a) {
  1495. lookupkey_t key;
  1496. upb_tabkey tabkey;
  1497. uint32_t hash;
  1498. if (isfull(&t->t)) {
  1499. /* Need to resize. New table of double the size, add old elements to it. */
  1500. if (!upb_strtable_resize(t, t->t.size_lg2 + 1, a)) {
  1501. return false;
  1502. }
  1503. }
  1504. key = strkey2(k, len);
  1505. tabkey = strcopy(key, a);
  1506. if (tabkey == 0) return false;
  1507. hash = upb_murmur_hash2(key.str.str, key.str.len, 0);
  1508. insert(&t->t, key, tabkey, v, hash, &strhash, &streql);
  1509. return true;
  1510. }
  1511. bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len,
  1512. upb_value *v) {
  1513. uint32_t hash = upb_murmur_hash2(key, len, 0);
  1514. return lookup(&t->t, strkey2(key, len), v, hash, &streql);
  1515. }
  1516. bool upb_strtable_remove3(upb_strtable *t, const char *key, size_t len,
  1517. upb_value *val, upb_alloc *alloc) {
  1518. uint32_t hash = upb_murmur_hash2(key, len, 0);
  1519. upb_tabkey tabkey;
  1520. if (rm(&t->t, strkey2(key, len), val, &tabkey, hash, &streql)) {
  1521. if (alloc) {
  1522. /* Arena-based allocs don't need to free and won't pass this. */
  1523. upb_free(alloc, (void*)tabkey);
  1524. }
  1525. return true;
  1526. } else {
  1527. return false;
  1528. }
  1529. }
  1530. /* Iteration */
  1531. void upb_strtable_begin(upb_strtable_iter *i, const upb_strtable *t) {
  1532. i->t = t;
  1533. i->index = begin(&t->t);
  1534. }
  1535. void upb_strtable_next(upb_strtable_iter *i) {
  1536. i->index = next(&i->t->t, i->index);
  1537. }
  1538. bool upb_strtable_done(const upb_strtable_iter *i) {
  1539. if (!i->t) return true;
  1540. return i->index >= upb_table_size(&i->t->t) ||
  1541. upb_tabent_isempty(str_tabent(i));
  1542. }
  1543. upb_strview upb_strtable_iter_key(const upb_strtable_iter *i) {
  1544. upb_strview key;
  1545. uint32_t len;
  1546. UPB_ASSERT(!upb_strtable_done(i));
  1547. key.data = upb_tabstr(str_tabent(i)->key, &len);
  1548. key.size = len;
  1549. return key;
  1550. }
  1551. upb_value upb_strtable_iter_value(const upb_strtable_iter *i) {
  1552. UPB_ASSERT(!upb_strtable_done(i));
  1553. return _upb_value_val(str_tabent(i)->val.val);
  1554. }
  1555. void upb_strtable_iter_setdone(upb_strtable_iter *i) {
  1556. i->t = NULL;
  1557. i->index = SIZE_MAX;
  1558. }
  1559. bool upb_strtable_iter_isequal(const upb_strtable_iter *i1,
  1560. const upb_strtable_iter *i2) {
  1561. if (upb_strtable_done(i1) && upb_strtable_done(i2))
  1562. return true;
  1563. return i1->t == i2->t && i1->index == i2->index;
  1564. }
  1565. /* upb_inttable ***************************************************************/
  1566. /* For inttables we use a hybrid structure where small keys are kept in an
  1567. * array and large keys are put in the hash table. */
  1568. static uint32_t inthash(upb_tabkey key) { return upb_inthash(key); }
  1569. static bool inteql(upb_tabkey k1, lookupkey_t k2) {
  1570. return k1 == k2.num;
  1571. }
  1572. static upb_tabval *mutable_array(upb_inttable *t) {
  1573. return (upb_tabval*)t->array;
  1574. }
  1575. static upb_tabval *inttable_val(upb_inttable *t, uintptr_t key) {
  1576. if (key < t->array_size) {
  1577. return upb_arrhas(t->array[key]) ? &(mutable_array(t)[key]) : NULL;
  1578. } else {
  1579. upb_tabent *e =
  1580. findentry_mutable(&t->t, intkey(key), upb_inthash(key), &inteql);
  1581. return e ? &e->val : NULL;
  1582. }
  1583. }
  1584. static const upb_tabval *inttable_val_const(const upb_inttable *t,
  1585. uintptr_t key) {
  1586. return inttable_val((upb_inttable*)t, key);
  1587. }
  1588. size_t upb_inttable_count(const upb_inttable *t) {
  1589. return t->t.count + t->array_count;
  1590. }
  1591. static void check(upb_inttable *t) {
  1592. UPB_UNUSED(t);
  1593. #if defined(UPB_DEBUG_TABLE) && !defined(NDEBUG)
  1594. {
  1595. /* This check is very expensive (makes inserts/deletes O(N)). */
  1596. size_t count = 0;
  1597. upb_inttable_iter i;
  1598. upb_inttable_begin(&i, t);
  1599. for(; !upb_inttable_done(&i); upb_inttable_next(&i), count++) {
  1600. UPB_ASSERT(upb_inttable_lookup(t, upb_inttable_iter_key(&i), NULL));
  1601. }
  1602. UPB_ASSERT(count == upb_inttable_count(t));
  1603. }
  1604. #endif
  1605. }
  1606. bool upb_inttable_sizedinit(upb_inttable *t, size_t asize, int hsize_lg2,
  1607. upb_alloc *a) {
  1608. size_t array_bytes;
  1609. if (!init(&t->t, hsize_lg2, a)) return false;
  1610. /* Always make the array part at least 1 long, so that we know key 0
  1611. * won't be in the hash part, which simplifies things. */
  1612. t->array_size = UPB_MAX(1, asize);
  1613. t->array_count = 0;
  1614. array_bytes = t->array_size * sizeof(upb_value);
  1615. t->array = upb_malloc(a, array_bytes);
  1616. if (!t->array) {
  1617. uninit(&t->t, a);
  1618. return false;
  1619. }
  1620. memset(mutable_array(t), 0xff, array_bytes);
  1621. check(t);
  1622. return true;
  1623. }
  1624. bool upb_inttable_init2(upb_inttable *t, upb_ctype_t ctype, upb_alloc *a) {
  1625. UPB_UNUSED(ctype); /* TODO(haberman): rm */
  1626. return upb_inttable_sizedinit(t, 0, 4, a);
  1627. }
  1628. void upb_inttable_uninit2(upb_inttable *t, upb_alloc *a) {
  1629. uninit(&t->t, a);
  1630. upb_free(a, mutable_array(t));
  1631. }
  1632. bool upb_inttable_insert2(upb_inttable *t, uintptr_t key, upb_value val,
  1633. upb_alloc *a) {
  1634. upb_tabval tabval;
  1635. tabval.val = val.val;
  1636. UPB_ASSERT(upb_arrhas(tabval)); /* This will reject (uint64_t)-1. Fix this. */
  1637. if (key < t->array_size) {
  1638. UPB_ASSERT(!upb_arrhas(t->array[key]));
  1639. t->array_count++;
  1640. mutable_array(t)[key].val = val.val;
  1641. } else {
  1642. if (isfull(&t->t)) {
  1643. /* Need to resize the hash part, but we re-use the array part. */
  1644. size_t i;
  1645. upb_table new_table;
  1646. if (!init(&new_table, t->t.size_lg2 + 1, a)) {
  1647. return false;
  1648. }
  1649. for (i = begin(&t->t); i < upb_table_size(&t->t); i = next(&t->t, i)) {
  1650. const upb_tabent *e = &t->t.entries[i];
  1651. uint32_t hash;
  1652. upb_value v;
  1653. _upb_value_setval(&v, e->val.val);
  1654. hash = upb_inthash(e->key);
  1655. insert(&new_table, intkey(e->key), e->key, v, hash, &inthash, &inteql);
  1656. }
  1657. UPB_ASSERT(t->t.count == new_table.count);
  1658. uninit(&t->t, a);
  1659. t->t = new_table;
  1660. }
  1661. insert(&t->t, intkey(key), key, val, upb_inthash(key), &inthash, &inteql);
  1662. }
  1663. check(t);
  1664. return true;
  1665. }
  1666. bool upb_inttable_lookup(const upb_inttable *t, uintptr_t key, upb_value *v) {
  1667. const upb_tabval *table_v = inttable_val_const(t, key);
  1668. if (!table_v) return false;
  1669. if (v) _upb_value_setval(v, table_v->val);
  1670. return true;
  1671. }
  1672. bool upb_inttable_replace(upb_inttable *t, uintptr_t key, upb_value val) {
  1673. upb_tabval *table_v = inttable_val(t, key);
  1674. if (!table_v) return false;
  1675. table_v->val = val.val;
  1676. return true;
  1677. }
  1678. bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val) {
  1679. bool success;
  1680. if (key < t->array_size) {
  1681. if (upb_arrhas(t->array[key])) {
  1682. upb_tabval empty = UPB_TABVALUE_EMPTY_INIT;
  1683. t->array_count--;
  1684. if (val) {
  1685. _upb_value_setval(val, t->array[key].val);
  1686. }
  1687. mutable_array(t)[key] = empty;
  1688. success = true;
  1689. } else {
  1690. success = false;
  1691. }
  1692. } else {
  1693. success = rm(&t->t, intkey(key), val, NULL, upb_inthash(key), &inteql);
  1694. }
  1695. check(t);
  1696. return success;
  1697. }
  1698. bool upb_inttable_insertptr2(upb_inttable *t, const void *key, upb_value val,
  1699. upb_alloc *a) {
  1700. return upb_inttable_insert2(t, (uintptr_t)key, val, a);
  1701. }
  1702. bool upb_inttable_lookupptr(const upb_inttable *t, const void *key,
  1703. upb_value *v) {
  1704. return upb_inttable_lookup(t, (uintptr_t)key, v);
  1705. }
  1706. bool upb_inttable_removeptr(upb_inttable *t, const void *key, upb_value *val) {
  1707. return upb_inttable_remove(t, (uintptr_t)key, val);
  1708. }
  1709. void upb_inttable_compact2(upb_inttable *t, upb_alloc *a) {
  1710. /* A power-of-two histogram of the table keys. */
  1711. size_t counts[UPB_MAXARRSIZE + 1] = {0};
  1712. /* The max key in each bucket. */
  1713. uintptr_t max[UPB_MAXARRSIZE + 1] = {0};
  1714. upb_inttable_iter i;
  1715. size_t arr_count;
  1716. int size_lg2;
  1717. upb_inttable new_t;
  1718. upb_inttable_begin(&i, t);
  1719. for (; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  1720. uintptr_t key = upb_inttable_iter_key(&i);
  1721. int bucket = log2ceil(key);
  1722. max[bucket] = UPB_MAX(max[bucket], key);
  1723. counts[bucket]++;
  1724. }
  1725. /* Find the largest power of two that satisfies the MIN_DENSITY
  1726. * definition (while actually having some keys). */
  1727. arr_count = upb_inttable_count(t);
  1728. for (size_lg2 = ARRAY_SIZE(counts) - 1; size_lg2 > 0; size_lg2--) {
  1729. if (counts[size_lg2] == 0) {
  1730. /* We can halve again without losing any entries. */
  1731. continue;
  1732. } else if (arr_count >= (1 << size_lg2) * MIN_DENSITY) {
  1733. break;
  1734. }
  1735. arr_count -= counts[size_lg2];
  1736. }
  1737. UPB_ASSERT(arr_count <= upb_inttable_count(t));
  1738. {
  1739. /* Insert all elements into new, perfectly-sized table. */
  1740. size_t arr_size = max[size_lg2] + 1; /* +1 so arr[max] will fit. */
  1741. size_t hash_count = upb_inttable_count(t) - arr_count;
  1742. size_t hash_size = hash_count ? (hash_count / MAX_LOAD) + 1 : 0;
  1743. int hashsize_lg2 = log2ceil(hash_size);
  1744. upb_inttable_sizedinit(&new_t, arr_size, hashsize_lg2, a);
  1745. upb_inttable_begin(&i, t);
  1746. for (; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  1747. uintptr_t k = upb_inttable_iter_key(&i);
  1748. upb_inttable_insert2(&new_t, k, upb_inttable_iter_value(&i), a);
  1749. }
  1750. UPB_ASSERT(new_t.array_size == arr_size);
  1751. UPB_ASSERT(new_t.t.size_lg2 == hashsize_lg2);
  1752. }
  1753. upb_inttable_uninit2(t, a);
  1754. *t = new_t;
  1755. }
  1756. /* Iteration. */
  1757. static const upb_tabent *int_tabent(const upb_inttable_iter *i) {
  1758. UPB_ASSERT(!i->array_part);
  1759. return &i->t->t.entries[i->index];
  1760. }
  1761. static upb_tabval int_arrent(const upb_inttable_iter *i) {
  1762. UPB_ASSERT(i->array_part);
  1763. return i->t->array[i->index];
  1764. }
  1765. void upb_inttable_begin(upb_inttable_iter *i, const upb_inttable *t) {
  1766. i->t = t;
  1767. i->index = -1;
  1768. i->array_part = true;
  1769. upb_inttable_next(i);
  1770. }
  1771. void upb_inttable_next(upb_inttable_iter *iter) {
  1772. const upb_inttable *t = iter->t;
  1773. if (iter->array_part) {
  1774. while (++iter->index < t->array_size) {
  1775. if (upb_arrhas(int_arrent(iter))) {
  1776. return;
  1777. }
  1778. }
  1779. iter->array_part = false;
  1780. iter->index = begin(&t->t);
  1781. } else {
  1782. iter->index = next(&t->t, iter->index);
  1783. }
  1784. }
  1785. bool upb_inttable_done(const upb_inttable_iter *i) {
  1786. if (!i->t) return true;
  1787. if (i->array_part) {
  1788. return i->index >= i->t->array_size ||
  1789. !upb_arrhas(int_arrent(i));
  1790. } else {
  1791. return i->index >= upb_table_size(&i->t->t) ||
  1792. upb_tabent_isempty(int_tabent(i));
  1793. }
  1794. }
  1795. uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i) {
  1796. UPB_ASSERT(!upb_inttable_done(i));
  1797. return i->array_part ? i->index : int_tabent(i)->key;
  1798. }
  1799. upb_value upb_inttable_iter_value(const upb_inttable_iter *i) {
  1800. UPB_ASSERT(!upb_inttable_done(i));
  1801. return _upb_value_val(
  1802. i->array_part ? i->t->array[i->index].val : int_tabent(i)->val.val);
  1803. }
  1804. void upb_inttable_iter_setdone(upb_inttable_iter *i) {
  1805. i->t = NULL;
  1806. i->index = SIZE_MAX;
  1807. i->array_part = false;
  1808. }
  1809. bool upb_inttable_iter_isequal(const upb_inttable_iter *i1,
  1810. const upb_inttable_iter *i2) {
  1811. if (upb_inttable_done(i1) && upb_inttable_done(i2))
  1812. return true;
  1813. return i1->t == i2->t && i1->index == i2->index &&
  1814. i1->array_part == i2->array_part;
  1815. }
  1816. #if defined(UPB_UNALIGNED_READS_OK) || defined(__s390x__)
  1817. /* -----------------------------------------------------------------------------
  1818. * MurmurHash2, by Austin Appleby (released as public domain).
  1819. * Reformatted and C99-ified by Joshua Haberman.
  1820. * Note - This code makes a few assumptions about how your machine behaves -
  1821. * 1. We can read a 4-byte value from any address without crashing
  1822. * 2. sizeof(int) == 4 (in upb this limitation is removed by using uint32_t
  1823. * And it has a few limitations -
  1824. * 1. It will not work incrementally.
  1825. * 2. It will not produce the same results on little-endian and big-endian
  1826. * machines. */
  1827. uint32_t upb_murmur_hash2(const void *key, size_t len, uint32_t seed) {
  1828. /* 'm' and 'r' are mixing constants generated offline.
  1829. * They're not really 'magic', they just happen to work well. */
  1830. const uint32_t m = 0x5bd1e995;
  1831. const int32_t r = 24;
  1832. /* Initialize the hash to a 'random' value */
  1833. uint32_t h = seed ^ len;
  1834. /* Mix 4 bytes at a time into the hash */
  1835. const uint8_t * data = (const uint8_t *)key;
  1836. while(len >= 4) {
  1837. uint32_t k;
  1838. memcpy(&k, data, sizeof(k));
  1839. k *= m;
  1840. k ^= k >> r;
  1841. k *= m;
  1842. h *= m;
  1843. h ^= k;
  1844. data += 4;
  1845. len -= 4;
  1846. }
  1847. /* Handle the last few bytes of the input array */
  1848. switch(len) {
  1849. case 3: h ^= data[2] << 16;
  1850. case 2: h ^= data[1] << 8;
  1851. case 1: h ^= data[0]; h *= m;
  1852. };
  1853. /* Do a few final mixes of the hash to ensure the last few
  1854. * bytes are well-incorporated. */
  1855. h ^= h >> 13;
  1856. h *= m;
  1857. h ^= h >> 15;
  1858. return h;
  1859. }
  1860. #else /* !UPB_UNALIGNED_READS_OK */
  1861. /* -----------------------------------------------------------------------------
  1862. * MurmurHashAligned2, by Austin Appleby
  1863. * Same algorithm as MurmurHash2, but only does aligned reads - should be safer
  1864. * on certain platforms.
  1865. * Performance will be lower than MurmurHash2 */
  1866. #define MIX(h,k,m) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; }
  1867. uint32_t upb_murmur_hash2(const void * key, size_t len, uint32_t seed) {
  1868. const uint32_t m = 0x5bd1e995;
  1869. const int32_t r = 24;
  1870. const uint8_t * data = (const uint8_t *)key;
  1871. uint32_t h = (uint32_t)(seed ^ len);
  1872. uint8_t align = (uintptr_t)data & 3;
  1873. if(align && (len >= 4)) {
  1874. /* Pre-load the temp registers */
  1875. uint32_t t = 0, d = 0;
  1876. int32_t sl;
  1877. int32_t sr;
  1878. switch(align) {
  1879. case 1: t |= data[2] << 16; /* fallthrough */
  1880. case 2: t |= data[1] << 8; /* fallthrough */
  1881. case 3: t |= data[0];
  1882. }
  1883. t <<= (8 * align);
  1884. data += 4-align;
  1885. len -= 4-align;
  1886. sl = 8 * (4-align);
  1887. sr = 8 * align;
  1888. /* Mix */
  1889. while(len >= 4) {
  1890. uint32_t k;
  1891. d = *(uint32_t *)data;
  1892. t = (t >> sr) | (d << sl);
  1893. k = t;
  1894. MIX(h,k,m);
  1895. t = d;
  1896. data += 4;
  1897. len -= 4;
  1898. }
  1899. /* Handle leftover data in temp registers */
  1900. d = 0;
  1901. if(len >= align) {
  1902. uint32_t k;
  1903. switch(align) {
  1904. case 3: d |= data[2] << 16; /* fallthrough */
  1905. case 2: d |= data[1] << 8; /* fallthrough */
  1906. case 1: d |= data[0]; /* fallthrough */
  1907. }
  1908. k = (t >> sr) | (d << sl);
  1909. MIX(h,k,m);
  1910. data += align;
  1911. len -= align;
  1912. /* ----------
  1913. * Handle tail bytes */
  1914. switch(len) {
  1915. case 3: h ^= data[2] << 16; /* fallthrough */
  1916. case 2: h ^= data[1] << 8; /* fallthrough */
  1917. case 1: h ^= data[0]; h *= m; /* fallthrough */
  1918. };
  1919. } else {
  1920. switch(len) {
  1921. case 3: d |= data[2] << 16; /* fallthrough */
  1922. case 2: d |= data[1] << 8; /* fallthrough */
  1923. case 1: d |= data[0]; /* fallthrough */
  1924. case 0: h ^= (t >> sr) | (d << sl); h *= m;
  1925. }
  1926. }
  1927. h ^= h >> 13;
  1928. h *= m;
  1929. h ^= h >> 15;
  1930. return h;
  1931. } else {
  1932. while(len >= 4) {
  1933. uint32_t k = *(uint32_t *)data;
  1934. MIX(h,k,m);
  1935. data += 4;
  1936. len -= 4;
  1937. }
  1938. /* ----------
  1939. * Handle tail bytes */
  1940. switch(len) {
  1941. case 3: h ^= data[2] << 16; /* fallthrough */
  1942. case 2: h ^= data[1] << 8; /* fallthrough */
  1943. case 1: h ^= data[0]; h *= m;
  1944. };
  1945. h ^= h >> 13;
  1946. h *= m;
  1947. h ^= h >> 15;
  1948. return h;
  1949. }
  1950. }
  1951. #undef MIX
  1952. #endif /* UPB_UNALIGNED_READS_OK */
  1953. #include <errno.h>
  1954. #include <stdarg.h>
  1955. #include <stddef.h>
  1956. #include <stdint.h>
  1957. #include <stdio.h>
  1958. #include <stdlib.h>
  1959. #include <string.h>
  1960. /* upb_status *****************************************************************/
  1961. void upb_status_clear(upb_status *status) {
  1962. if (!status) return;
  1963. status->ok = true;
  1964. status->msg[0] = '\0';
  1965. }
  1966. bool upb_ok(const upb_status *status) { return status->ok; }
  1967. const char *upb_status_errmsg(const upb_status *status) { return status->msg; }
  1968. void upb_status_seterrmsg(upb_status *status, const char *msg) {
  1969. if (!status) return;
  1970. status->ok = false;
  1971. strncpy(status->msg, msg, UPB_STATUS_MAX_MESSAGE - 1);
  1972. status->msg[UPB_STATUS_MAX_MESSAGE - 1] = '\0';
  1973. }
  1974. void upb_status_seterrf(upb_status *status, const char *fmt, ...) {
  1975. va_list args;
  1976. va_start(args, fmt);
  1977. upb_status_vseterrf(status, fmt, args);
  1978. va_end(args);
  1979. }
  1980. void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args) {
  1981. if (!status) return;
  1982. status->ok = false;
  1983. _upb_vsnprintf(status->msg, sizeof(status->msg), fmt, args);
  1984. status->msg[UPB_STATUS_MAX_MESSAGE - 1] = '\0';
  1985. }
  1986. void upb_status_vappenderrf(upb_status *status, const char *fmt, va_list args) {
  1987. size_t len;
  1988. if (!status) return;
  1989. status->ok = false;
  1990. len = strlen(status->msg);
  1991. _upb_vsnprintf(status->msg + len, sizeof(status->msg) - len, fmt, args);
  1992. status->msg[UPB_STATUS_MAX_MESSAGE - 1] = '\0';
  1993. }
  1994. /* upb_alloc ******************************************************************/
  1995. static void *upb_global_allocfunc(upb_alloc *alloc, void *ptr, size_t oldsize,
  1996. size_t size) {
  1997. UPB_UNUSED(alloc);
  1998. UPB_UNUSED(oldsize);
  1999. if (size == 0) {
  2000. free(ptr);
  2001. return NULL;
  2002. } else {
  2003. return realloc(ptr, size);
  2004. }
  2005. }
  2006. upb_alloc upb_alloc_global = {&upb_global_allocfunc};
  2007. /* upb_arena ******************************************************************/
  2008. /* Be conservative and choose 16 in case anyone is using SSE. */
  2009. typedef struct mem_block {
  2010. struct mem_block *next;
  2011. uint32_t size;
  2012. uint32_t cleanups;
  2013. /* Data follows. */
  2014. } mem_block;
  2015. typedef struct cleanup_ent {
  2016. upb_cleanup_func *cleanup;
  2017. void *ud;
  2018. } cleanup_ent;
  2019. struct upb_arena {
  2020. _upb_arena_head head;
  2021. uint32_t *cleanups;
  2022. /* Allocator to allocate arena blocks. We are responsible for freeing these
  2023. * when we are destroyed. */
  2024. upb_alloc *block_alloc;
  2025. uint32_t last_size;
  2026. /* When multiple arenas are fused together, each arena points to a parent
  2027. * arena (root points to itself). The root tracks how many live arenas
  2028. * reference it. */
  2029. uint32_t refcount; /* Only used when a->parent == a */
  2030. struct upb_arena *parent;
  2031. /* Linked list of blocks to free/cleanup. */
  2032. mem_block *freelist, *freelist_tail;
  2033. };
  2034. static const size_t memblock_reserve = UPB_ALIGN_UP(sizeof(mem_block), 16);
  2035. static upb_arena *arena_findroot(upb_arena *a) {
  2036. /* Path splitting keeps time complexity down, see:
  2037. * https://en.wikipedia.org/wiki/Disjoint-set_data_structure */
  2038. while (a->parent != a) {
  2039. upb_arena *next = a->parent;
  2040. a->parent = next->parent;
  2041. a = next;
  2042. }
  2043. return a;
  2044. }
  2045. static void upb_arena_addblock(upb_arena *a, void *ptr, size_t size) {
  2046. mem_block *block = ptr;
  2047. upb_arena *root = arena_findroot(a);
  2048. /* The block is for arena |a|, but should appear in the freelist of |root|. */
  2049. block->next = root->freelist;
  2050. block->size = (uint32_t)size;
  2051. block->cleanups = 0;
  2052. root->freelist = block;
  2053. a->last_size = block->size;
  2054. if (!root->freelist_tail) root->freelist_tail = block;
  2055. a->head.ptr = UPB_PTR_AT(block, memblock_reserve, char);
  2056. a->head.end = UPB_PTR_AT(block, size, char);
  2057. a->cleanups = &block->cleanups;
  2058. /* TODO(haberman): ASAN poison. */
  2059. }
  2060. static bool upb_arena_allocblock(upb_arena *a, size_t size) {
  2061. size_t block_size = UPB_MAX(size, a->last_size * 2) + memblock_reserve;
  2062. mem_block *block = upb_malloc(a->block_alloc, block_size);
  2063. if (!block) return false;
  2064. upb_arena_addblock(a, block, block_size);
  2065. return true;
  2066. }
  2067. static bool arena_has(upb_arena *a, size_t size) {
  2068. _upb_arena_head *h = (_upb_arena_head*)a;
  2069. return (size_t)(h->end - h->ptr) >= size;
  2070. }
  2071. void *_upb_arena_slowmalloc(upb_arena *a, size_t size) {
  2072. if (!upb_arena_allocblock(a, size)) return NULL; /* Out of memory. */
  2073. UPB_ASSERT(arena_has(a, size));
  2074. return upb_arena_malloc(a, size);
  2075. }
  2076. static void *upb_arena_doalloc(upb_alloc *alloc, void *ptr, size_t oldsize,
  2077. size_t size) {
  2078. upb_arena *a = (upb_arena*)alloc; /* upb_alloc is initial member. */
  2079. return upb_arena_realloc(a, ptr, oldsize, size);
  2080. }
  2081. /* Public Arena API ***********************************************************/
  2082. upb_arena *arena_initslow(void *mem, size_t n, upb_alloc *alloc) {
  2083. const size_t first_block_overhead = sizeof(upb_arena) + memblock_reserve;
  2084. upb_arena *a;
  2085. /* We need to malloc the initial block. */
  2086. n = first_block_overhead + 256;
  2087. if (!alloc || !(mem = upb_malloc(alloc, n))) {
  2088. return NULL;
  2089. }
  2090. a = UPB_PTR_AT(mem, n - sizeof(*a), upb_arena);
  2091. n -= sizeof(*a);
  2092. a->head.alloc.func = &upb_arena_doalloc;
  2093. a->block_alloc = alloc;
  2094. a->parent = a;
  2095. a->refcount = 1;
  2096. a->freelist = NULL;
  2097. a->freelist_tail = NULL;
  2098. upb_arena_addblock(a, mem, n);
  2099. return a;
  2100. }
  2101. upb_arena *upb_arena_init(void *mem, size_t n, upb_alloc *alloc) {
  2102. upb_arena *a;
  2103. /* Round block size down to alignof(*a) since we will allocate the arena
  2104. * itself at the end. */
  2105. n = UPB_ALIGN_DOWN(n, UPB_ALIGN_OF(upb_arena));
  2106. if (UPB_UNLIKELY(n < sizeof(upb_arena))) {
  2107. return arena_initslow(mem, n, alloc);
  2108. }
  2109. a = UPB_PTR_AT(mem, n - sizeof(*a), upb_arena);
  2110. n -= sizeof(*a);
  2111. a->head.alloc.func = &upb_arena_doalloc;
  2112. a->block_alloc = alloc;
  2113. a->parent = a;
  2114. a->refcount = 1;
  2115. a->last_size = 128;
  2116. a->head.ptr = mem;
  2117. a->head.end = UPB_PTR_AT(mem, n, char);
  2118. a->freelist = NULL;
  2119. a->cleanups = NULL;
  2120. return a;
  2121. }
  2122. static void arena_dofree(upb_arena *a) {
  2123. mem_block *block = a->freelist;
  2124. UPB_ASSERT(a->parent == a);
  2125. UPB_ASSERT(a->refcount == 0);
  2126. while (block) {
  2127. /* Load first since we are deleting block. */
  2128. mem_block *next = block->next;
  2129. if (block->cleanups > 0) {
  2130. cleanup_ent *end = UPB_PTR_AT(block, block->size, void);
  2131. cleanup_ent *ptr = end - block->cleanups;
  2132. for (; ptr < end; ptr++) {
  2133. ptr->cleanup(ptr->ud);
  2134. }
  2135. }
  2136. upb_free(a->block_alloc, block);
  2137. block = next;
  2138. }
  2139. }
  2140. void upb_arena_free(upb_arena *a) {
  2141. a = arena_findroot(a);
  2142. if (--a->refcount == 0) arena_dofree(a);
  2143. }
  2144. bool upb_arena_addcleanup(upb_arena *a, void *ud, upb_cleanup_func *func) {
  2145. cleanup_ent *ent;
  2146. if (!a->cleanups || !arena_has(a, sizeof(cleanup_ent))) {
  2147. if (!upb_arena_allocblock(a, 128)) return false; /* Out of memory. */
  2148. UPB_ASSERT(arena_has(a, sizeof(cleanup_ent)));
  2149. }
  2150. a->head.end -= sizeof(cleanup_ent);
  2151. ent = (cleanup_ent*)a->head.end;
  2152. (*a->cleanups)++;
  2153. ent->cleanup = func;
  2154. ent->ud = ud;
  2155. return true;
  2156. }
  2157. void upb_arena_fuse(upb_arena *a1, upb_arena *a2) {
  2158. upb_arena *r1 = arena_findroot(a1);
  2159. upb_arena *r2 = arena_findroot(a2);
  2160. if (r1 == r2) return; /* Already fused. */
  2161. /* We want to join the smaller tree to the larger tree.
  2162. * So swap first if they are backwards. */
  2163. if (r1->refcount < r2->refcount) {
  2164. upb_arena *tmp = r1;
  2165. r1 = r2;
  2166. r2 = tmp;
  2167. }
  2168. /* r1 takes over r2's freelist and refcount. */
  2169. r1->refcount += r2->refcount;
  2170. if (r2->freelist_tail) {
  2171. UPB_ASSERT(r2->freelist_tail->next == NULL);
  2172. r2->freelist_tail->next = r1->freelist;
  2173. r1->freelist = r2->freelist;
  2174. }
  2175. r2->parent = r1;
  2176. }
  2177. /* This file was generated by upbc (the upb compiler) from the input
  2178. * file:
  2179. *
  2180. * google/protobuf/descriptor.proto
  2181. *
  2182. * Do not edit -- your changes will be discarded when the file is
  2183. * regenerated. */
  2184. #include <stddef.h>
  2185. static const upb_msglayout *const google_protobuf_FileDescriptorSet_submsgs[1] = {
  2186. &google_protobuf_FileDescriptorProto_msginit,
  2187. };
  2188. static const upb_msglayout_field google_protobuf_FileDescriptorSet__fields[1] = {
  2189. {1, UPB_SIZE(0, 0), 0, 0, 11, 3},
  2190. };
  2191. const upb_msglayout google_protobuf_FileDescriptorSet_msginit = {
  2192. &google_protobuf_FileDescriptorSet_submsgs[0],
  2193. &google_protobuf_FileDescriptorSet__fields[0],
  2194. UPB_SIZE(4, 8), 1, false,
  2195. };
  2196. static const upb_msglayout *const google_protobuf_FileDescriptorProto_submsgs[6] = {
  2197. &google_protobuf_DescriptorProto_msginit,
  2198. &google_protobuf_EnumDescriptorProto_msginit,
  2199. &google_protobuf_FieldDescriptorProto_msginit,
  2200. &google_protobuf_FileOptions_msginit,
  2201. &google_protobuf_ServiceDescriptorProto_msginit,
  2202. &google_protobuf_SourceCodeInfo_msginit,
  2203. };
  2204. static const upb_msglayout_field google_protobuf_FileDescriptorProto__fields[12] = {
  2205. {1, UPB_SIZE(4, 8), 1, 0, 12, 1},
  2206. {2, UPB_SIZE(12, 24), 2, 0, 12, 1},
  2207. {3, UPB_SIZE(36, 72), 0, 0, 12, 3},
  2208. {4, UPB_SIZE(40, 80), 0, 0, 11, 3},
  2209. {5, UPB_SIZE(44, 88), 0, 1, 11, 3},
  2210. {6, UPB_SIZE(48, 96), 0, 4, 11, 3},
  2211. {7, UPB_SIZE(52, 104), 0, 2, 11, 3},
  2212. {8, UPB_SIZE(28, 56), 4, 3, 11, 1},
  2213. {9, UPB_SIZE(32, 64), 5, 5, 11, 1},
  2214. {10, UPB_SIZE(56, 112), 0, 0, 5, 3},
  2215. {11, UPB_SIZE(60, 120), 0, 0, 5, 3},
  2216. {12, UPB_SIZE(20, 40), 3, 0, 12, 1},
  2217. };
  2218. const upb_msglayout google_protobuf_FileDescriptorProto_msginit = {
  2219. &google_protobuf_FileDescriptorProto_submsgs[0],
  2220. &google_protobuf_FileDescriptorProto__fields[0],
  2221. UPB_SIZE(64, 128), 12, false,
  2222. };
  2223. static const upb_msglayout *const google_protobuf_DescriptorProto_submsgs[8] = {
  2224. &google_protobuf_DescriptorProto_msginit,
  2225. &google_protobuf_DescriptorProto_ExtensionRange_msginit,
  2226. &google_protobuf_DescriptorProto_ReservedRange_msginit,
  2227. &google_protobuf_EnumDescriptorProto_msginit,
  2228. &google_protobuf_FieldDescriptorProto_msginit,
  2229. &google_protobuf_MessageOptions_msginit,
  2230. &google_protobuf_OneofDescriptorProto_msginit,
  2231. };
  2232. static const upb_msglayout_field google_protobuf_DescriptorProto__fields[10] = {
  2233. {1, UPB_SIZE(4, 8), 1, 0, 12, 1},
  2234. {2, UPB_SIZE(16, 32), 0, 4, 11, 3},
  2235. {3, UPB_SIZE(20, 40), 0, 0, 11, 3},
  2236. {4, UPB_SIZE(24, 48), 0, 3, 11, 3},
  2237. {5, UPB_SIZE(28, 56), 0, 1, 11, 3},
  2238. {6, UPB_SIZE(32, 64), 0, 4, 11, 3},
  2239. {7, UPB_SIZE(12, 24), 2, 5, 11, 1},
  2240. {8, UPB_SIZE(36, 72), 0, 6, 11, 3},
  2241. {9, UPB_SIZE(40, 80), 0, 2, 11, 3},
  2242. {10, UPB_SIZE(44, 88), 0, 0, 12, 3},
  2243. };
  2244. const upb_msglayout google_protobuf_DescriptorProto_msginit = {
  2245. &google_protobuf_DescriptorProto_submsgs[0],
  2246. &google_protobuf_DescriptorProto__fields[0],
  2247. UPB_SIZE(48, 96), 10, false,
  2248. };
  2249. static const upb_msglayout *const google_protobuf_DescriptorProto_ExtensionRange_submsgs[1] = {
  2250. &google_protobuf_ExtensionRangeOptions_msginit,
  2251. };
  2252. static const upb_msglayout_field google_protobuf_DescriptorProto_ExtensionRange__fields[3] = {
  2253. {1, UPB_SIZE(4, 4), 1, 0, 5, 1},
  2254. {2, UPB_SIZE(8, 8), 2, 0, 5, 1},
  2255. {3, UPB_SIZE(12, 16), 3, 0, 11, 1},
  2256. };
  2257. const upb_msglayout google_protobuf_DescriptorProto_ExtensionRange_msginit = {
  2258. &google_protobuf_DescriptorProto_ExtensionRange_submsgs[0],
  2259. &google_protobuf_DescriptorProto_ExtensionRange__fields[0],
  2260. UPB_SIZE(16, 24), 3, false,
  2261. };
  2262. static const upb_msglayout_field google_protobuf_DescriptorProto_ReservedRange__fields[2] = {
  2263. {1, UPB_SIZE(4, 4), 1, 0, 5, 1},
  2264. {2, UPB_SIZE(8, 8), 2, 0, 5, 1},
  2265. };
  2266. const upb_msglayout google_protobuf_DescriptorProto_ReservedRange_msginit = {
  2267. NULL,
  2268. &google_protobuf_DescriptorProto_ReservedRange__fields[0],
  2269. UPB_SIZE(12, 12), 2, false,
  2270. };
  2271. static const upb_msglayout *const google_protobuf_ExtensionRangeOptions_submsgs[1] = {
  2272. &google_protobuf_UninterpretedOption_msginit,
  2273. };
  2274. static const upb_msglayout_field google_protobuf_ExtensionRangeOptions__fields[1] = {
  2275. {999, UPB_SIZE(0, 0), 0, 0, 11, 3},
  2276. };
  2277. const upb_msglayout google_protobuf_ExtensionRangeOptions_msginit = {
  2278. &google_protobuf_ExtensionRangeOptions_submsgs[0],
  2279. &google_protobuf_ExtensionRangeOptions__fields[0],
  2280. UPB_SIZE(4, 8), 1, false,
  2281. };
  2282. static const upb_msglayout *const google_protobuf_FieldDescriptorProto_submsgs[1] = {
  2283. &google_protobuf_FieldOptions_msginit,
  2284. };
  2285. static const upb_msglayout_field google_protobuf_FieldDescriptorProto__fields[11] = {
  2286. {1, UPB_SIZE(36, 40), 6, 0, 12, 1},
  2287. {2, UPB_SIZE(44, 56), 7, 0, 12, 1},
  2288. {3, UPB_SIZE(24, 24), 3, 0, 5, 1},
  2289. {4, UPB_SIZE(8, 8), 1, 0, 14, 1},
  2290. {5, UPB_SIZE(16, 16), 2, 0, 14, 1},
  2291. {6, UPB_SIZE(52, 72), 8, 0, 12, 1},
  2292. {7, UPB_SIZE(60, 88), 9, 0, 12, 1},
  2293. {8, UPB_SIZE(76, 120), 11, 0, 11, 1},
  2294. {9, UPB_SIZE(28, 28), 4, 0, 5, 1},
  2295. {10, UPB_SIZE(68, 104), 10, 0, 12, 1},
  2296. {17, UPB_SIZE(32, 32), 5, 0, 8, 1},
  2297. };
  2298. const upb_msglayout google_protobuf_FieldDescriptorProto_msginit = {
  2299. &google_protobuf_FieldDescriptorProto_submsgs[0],
  2300. &google_protobuf_FieldDescriptorProto__fields[0],
  2301. UPB_SIZE(80, 128), 11, false,
  2302. };
  2303. static const upb_msglayout *const google_protobuf_OneofDescriptorProto_submsgs[1] = {
  2304. &google_protobuf_OneofOptions_msginit,
  2305. };
  2306. static const upb_msglayout_field google_protobuf_OneofDescriptorProto__fields[2] = {
  2307. {1, UPB_SIZE(4, 8), 1, 0, 12, 1},
  2308. {2, UPB_SIZE(12, 24), 2, 0, 11, 1},
  2309. };
  2310. const upb_msglayout google_protobuf_OneofDescriptorProto_msginit = {
  2311. &google_protobuf_OneofDescriptorProto_submsgs[0],
  2312. &google_protobuf_OneofDescriptorProto__fields[0],
  2313. UPB_SIZE(16, 32), 2, false,
  2314. };
  2315. static const upb_msglayout *const google_protobuf_EnumDescriptorProto_submsgs[3] = {
  2316. &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit,
  2317. &google_protobuf_EnumOptions_msginit,
  2318. &google_protobuf_EnumValueDescriptorProto_msginit,
  2319. };
  2320. static const upb_msglayout_field google_protobuf_EnumDescriptorProto__fields[5] = {
  2321. {1, UPB_SIZE(4, 8), 1, 0, 12, 1},
  2322. {2, UPB_SIZE(16, 32), 0, 2, 11, 3},
  2323. {3, UPB_SIZE(12, 24), 2, 1, 11, 1},
  2324. {4, UPB_SIZE(20, 40), 0, 0, 11, 3},
  2325. {5, UPB_SIZE(24, 48), 0, 0, 12, 3},
  2326. };
  2327. const upb_msglayout google_protobuf_EnumDescriptorProto_msginit = {
  2328. &google_protobuf_EnumDescriptorProto_submsgs[0],
  2329. &google_protobuf_EnumDescriptorProto__fields[0],
  2330. UPB_SIZE(32, 64), 5, false,
  2331. };
  2332. static const upb_msglayout_field google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[2] = {
  2333. {1, UPB_SIZE(4, 4), 1, 0, 5, 1},
  2334. {2, UPB_SIZE(8, 8), 2, 0, 5, 1},
  2335. };
  2336. const upb_msglayout google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit = {
  2337. NULL,
  2338. &google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[0],
  2339. UPB_SIZE(12, 12), 2, false,
  2340. };
  2341. static const upb_msglayout *const google_protobuf_EnumValueDescriptorProto_submsgs[1] = {
  2342. &google_protobuf_EnumValueOptions_msginit,
  2343. };
  2344. static const upb_msglayout_field google_protobuf_EnumValueDescriptorProto__fields[3] = {
  2345. {1, UPB_SIZE(8, 8), 2, 0, 12, 1},
  2346. {2, UPB_SIZE(4, 4), 1, 0, 5, 1},
  2347. {3, UPB_SIZE(16, 24), 3, 0, 11, 1},
  2348. };
  2349. const upb_msglayout google_protobuf_EnumValueDescriptorProto_msginit = {
  2350. &google_protobuf_EnumValueDescriptorProto_submsgs[0],
  2351. &google_protobuf_EnumValueDescriptorProto__fields[0],
  2352. UPB_SIZE(24, 32), 3, false,
  2353. };
  2354. static const upb_msglayout *const google_protobuf_ServiceDescriptorProto_submsgs[2] = {
  2355. &google_protobuf_MethodDescriptorProto_msginit,
  2356. &google_protobuf_ServiceOptions_msginit,
  2357. };
  2358. static const upb_msglayout_field google_protobuf_ServiceDescriptorProto__fields[3] = {
  2359. {1, UPB_SIZE(4, 8), 1, 0, 12, 1},
  2360. {2, UPB_SIZE(16, 32), 0, 0, 11, 3},
  2361. {3, UPB_SIZE(12, 24), 2, 1, 11, 1},
  2362. };
  2363. const upb_msglayout google_protobuf_ServiceDescriptorProto_msginit = {
  2364. &google_protobuf_ServiceDescriptorProto_submsgs[0],
  2365. &google_protobuf_ServiceDescriptorProto__fields[0],
  2366. UPB_SIZE(24, 48), 3, false,
  2367. };
  2368. static const upb_msglayout *const google_protobuf_MethodDescriptorProto_submsgs[1] = {
  2369. &google_protobuf_MethodOptions_msginit,
  2370. };
  2371. static const upb_msglayout_field google_protobuf_MethodDescriptorProto__fields[6] = {
  2372. {1, UPB_SIZE(4, 8), 3, 0, 12, 1},
  2373. {2, UPB_SIZE(12, 24), 4, 0, 12, 1},
  2374. {3, UPB_SIZE(20, 40), 5, 0, 12, 1},
  2375. {4, UPB_SIZE(28, 56), 6, 0, 11, 1},
  2376. {5, UPB_SIZE(1, 1), 1, 0, 8, 1},
  2377. {6, UPB_SIZE(2, 2), 2, 0, 8, 1},
  2378. };
  2379. const upb_msglayout google_protobuf_MethodDescriptorProto_msginit = {
  2380. &google_protobuf_MethodDescriptorProto_submsgs[0],
  2381. &google_protobuf_MethodDescriptorProto__fields[0],
  2382. UPB_SIZE(32, 64), 6, false,
  2383. };
  2384. static const upb_msglayout *const google_protobuf_FileOptions_submsgs[1] = {
  2385. &google_protobuf_UninterpretedOption_msginit,
  2386. };
  2387. static const upb_msglayout_field google_protobuf_FileOptions__fields[21] = {
  2388. {1, UPB_SIZE(28, 32), 11, 0, 12, 1},
  2389. {8, UPB_SIZE(36, 48), 12, 0, 12, 1},
  2390. {9, UPB_SIZE(8, 8), 1, 0, 14, 1},
  2391. {10, UPB_SIZE(16, 16), 2, 0, 8, 1},
  2392. {11, UPB_SIZE(44, 64), 13, 0, 12, 1},
  2393. {16, UPB_SIZE(17, 17), 3, 0, 8, 1},
  2394. {17, UPB_SIZE(18, 18), 4, 0, 8, 1},
  2395. {18, UPB_SIZE(19, 19), 5, 0, 8, 1},
  2396. {20, UPB_SIZE(20, 20), 6, 0, 8, 1},
  2397. {23, UPB_SIZE(21, 21), 7, 0, 8, 1},
  2398. {27, UPB_SIZE(22, 22), 8, 0, 8, 1},
  2399. {31, UPB_SIZE(23, 23), 9, 0, 8, 1},
  2400. {36, UPB_SIZE(52, 80), 14, 0, 12, 1},
  2401. {37, UPB_SIZE(60, 96), 15, 0, 12, 1},
  2402. {39, UPB_SIZE(68, 112), 16, 0, 12, 1},
  2403. {40, UPB_SIZE(76, 128), 17, 0, 12, 1},
  2404. {41, UPB_SIZE(84, 144), 18, 0, 12, 1},
  2405. {42, UPB_SIZE(24, 24), 10, 0, 8, 1},
  2406. {44, UPB_SIZE(92, 160), 19, 0, 12, 1},
  2407. {45, UPB_SIZE(100, 176), 20, 0, 12, 1},
  2408. {999, UPB_SIZE(108, 192), 0, 0, 11, 3},
  2409. };
  2410. const upb_msglayout google_protobuf_FileOptions_msginit = {
  2411. &google_protobuf_FileOptions_submsgs[0],
  2412. &google_protobuf_FileOptions__fields[0],
  2413. UPB_SIZE(112, 208), 21, false,
  2414. };
  2415. static const upb_msglayout *const google_protobuf_MessageOptions_submsgs[1] = {
  2416. &google_protobuf_UninterpretedOption_msginit,
  2417. };
  2418. static const upb_msglayout_field google_protobuf_MessageOptions__fields[5] = {
  2419. {1, UPB_SIZE(1, 1), 1, 0, 8, 1},
  2420. {2, UPB_SIZE(2, 2), 2, 0, 8, 1},
  2421. {3, UPB_SIZE(3, 3), 3, 0, 8, 1},
  2422. {7, UPB_SIZE(4, 4), 4, 0, 8, 1},
  2423. {999, UPB_SIZE(8, 8), 0, 0, 11, 3},
  2424. };
  2425. const upb_msglayout google_protobuf_MessageOptions_msginit = {
  2426. &google_protobuf_MessageOptions_submsgs[0],
  2427. &google_protobuf_MessageOptions__fields[0],
  2428. UPB_SIZE(12, 16), 5, false,
  2429. };
  2430. static const upb_msglayout *const google_protobuf_FieldOptions_submsgs[1] = {
  2431. &google_protobuf_UninterpretedOption_msginit,
  2432. };
  2433. static const upb_msglayout_field google_protobuf_FieldOptions__fields[7] = {
  2434. {1, UPB_SIZE(8, 8), 1, 0, 14, 1},
  2435. {2, UPB_SIZE(24, 24), 3, 0, 8, 1},
  2436. {3, UPB_SIZE(25, 25), 4, 0, 8, 1},
  2437. {5, UPB_SIZE(26, 26), 5, 0, 8, 1},
  2438. {6, UPB_SIZE(16, 16), 2, 0, 14, 1},
  2439. {10, UPB_SIZE(27, 27), 6, 0, 8, 1},
  2440. {999, UPB_SIZE(28, 32), 0, 0, 11, 3},
  2441. };
  2442. const upb_msglayout google_protobuf_FieldOptions_msginit = {
  2443. &google_protobuf_FieldOptions_submsgs[0],
  2444. &google_protobuf_FieldOptions__fields[0],
  2445. UPB_SIZE(32, 40), 7, false,
  2446. };
  2447. static const upb_msglayout *const google_protobuf_OneofOptions_submsgs[1] = {
  2448. &google_protobuf_UninterpretedOption_msginit,
  2449. };
  2450. static const upb_msglayout_field google_protobuf_OneofOptions__fields[1] = {
  2451. {999, UPB_SIZE(0, 0), 0, 0, 11, 3},
  2452. };
  2453. const upb_msglayout google_protobuf_OneofOptions_msginit = {
  2454. &google_protobuf_OneofOptions_submsgs[0],
  2455. &google_protobuf_OneofOptions__fields[0],
  2456. UPB_SIZE(4, 8), 1, false,
  2457. };
  2458. static const upb_msglayout *const google_protobuf_EnumOptions_submsgs[1] = {
  2459. &google_protobuf_UninterpretedOption_msginit,
  2460. };
  2461. static const upb_msglayout_field google_protobuf_EnumOptions__fields[3] = {
  2462. {2, UPB_SIZE(1, 1), 1, 0, 8, 1},
  2463. {3, UPB_SIZE(2, 2), 2, 0, 8, 1},
  2464. {999, UPB_SIZE(4, 8), 0, 0, 11, 3},
  2465. };
  2466. const upb_msglayout google_protobuf_EnumOptions_msginit = {
  2467. &google_protobuf_EnumOptions_submsgs[0],
  2468. &google_protobuf_EnumOptions__fields[0],
  2469. UPB_SIZE(8, 16), 3, false,
  2470. };
  2471. static const upb_msglayout *const google_protobuf_EnumValueOptions_submsgs[1] = {
  2472. &google_protobuf_UninterpretedOption_msginit,
  2473. };
  2474. static const upb_msglayout_field google_protobuf_EnumValueOptions__fields[2] = {
  2475. {1, UPB_SIZE(1, 1), 1, 0, 8, 1},
  2476. {999, UPB_SIZE(4, 8), 0, 0, 11, 3},
  2477. };
  2478. const upb_msglayout google_protobuf_EnumValueOptions_msginit = {
  2479. &google_protobuf_EnumValueOptions_submsgs[0],
  2480. &google_protobuf_EnumValueOptions__fields[0],
  2481. UPB_SIZE(8, 16), 2, false,
  2482. };
  2483. static const upb_msglayout *const google_protobuf_ServiceOptions_submsgs[1] = {
  2484. &google_protobuf_UninterpretedOption_msginit,
  2485. };
  2486. static const upb_msglayout_field google_protobuf_ServiceOptions__fields[2] = {
  2487. {33, UPB_SIZE(1, 1), 1, 0, 8, 1},
  2488. {999, UPB_SIZE(4, 8), 0, 0, 11, 3},
  2489. };
  2490. const upb_msglayout google_protobuf_ServiceOptions_msginit = {
  2491. &google_protobuf_ServiceOptions_submsgs[0],
  2492. &google_protobuf_ServiceOptions__fields[0],
  2493. UPB_SIZE(8, 16), 2, false,
  2494. };
  2495. static const upb_msglayout *const google_protobuf_MethodOptions_submsgs[1] = {
  2496. &google_protobuf_UninterpretedOption_msginit,
  2497. };
  2498. static const upb_msglayout_field google_protobuf_MethodOptions__fields[3] = {
  2499. {33, UPB_SIZE(16, 16), 2, 0, 8, 1},
  2500. {34, UPB_SIZE(8, 8), 1, 0, 14, 1},
  2501. {999, UPB_SIZE(20, 24), 0, 0, 11, 3},
  2502. };
  2503. const upb_msglayout google_protobuf_MethodOptions_msginit = {
  2504. &google_protobuf_MethodOptions_submsgs[0],
  2505. &google_protobuf_MethodOptions__fields[0],
  2506. UPB_SIZE(24, 32), 3, false,
  2507. };
  2508. static const upb_msglayout *const google_protobuf_UninterpretedOption_submsgs[1] = {
  2509. &google_protobuf_UninterpretedOption_NamePart_msginit,
  2510. };
  2511. static const upb_msglayout_field google_protobuf_UninterpretedOption__fields[7] = {
  2512. {2, UPB_SIZE(56, 80), 0, 0, 11, 3},
  2513. {3, UPB_SIZE(32, 32), 4, 0, 12, 1},
  2514. {4, UPB_SIZE(8, 8), 1, 0, 4, 1},
  2515. {5, UPB_SIZE(16, 16), 2, 0, 3, 1},
  2516. {6, UPB_SIZE(24, 24), 3, 0, 1, 1},
  2517. {7, UPB_SIZE(40, 48), 5, 0, 12, 1},
  2518. {8, UPB_SIZE(48, 64), 6, 0, 12, 1},
  2519. };
  2520. const upb_msglayout google_protobuf_UninterpretedOption_msginit = {
  2521. &google_protobuf_UninterpretedOption_submsgs[0],
  2522. &google_protobuf_UninterpretedOption__fields[0],
  2523. UPB_SIZE(64, 96), 7, false,
  2524. };
  2525. static const upb_msglayout_field google_protobuf_UninterpretedOption_NamePart__fields[2] = {
  2526. {1, UPB_SIZE(4, 8), 2, 0, 12, 2},
  2527. {2, UPB_SIZE(1, 1), 1, 0, 8, 2},
  2528. };
  2529. const upb_msglayout google_protobuf_UninterpretedOption_NamePart_msginit = {
  2530. NULL,
  2531. &google_protobuf_UninterpretedOption_NamePart__fields[0],
  2532. UPB_SIZE(16, 32), 2, false,
  2533. };
  2534. static const upb_msglayout *const google_protobuf_SourceCodeInfo_submsgs[1] = {
  2535. &google_protobuf_SourceCodeInfo_Location_msginit,
  2536. };
  2537. static const upb_msglayout_field google_protobuf_SourceCodeInfo__fields[1] = {
  2538. {1, UPB_SIZE(0, 0), 0, 0, 11, 3},
  2539. };
  2540. const upb_msglayout google_protobuf_SourceCodeInfo_msginit = {
  2541. &google_protobuf_SourceCodeInfo_submsgs[0],
  2542. &google_protobuf_SourceCodeInfo__fields[0],
  2543. UPB_SIZE(4, 8), 1, false,
  2544. };
  2545. static const upb_msglayout_field google_protobuf_SourceCodeInfo_Location__fields[5] = {
  2546. {1, UPB_SIZE(20, 40), 0, 0, 5, _UPB_LABEL_PACKED},
  2547. {2, UPB_SIZE(24, 48), 0, 0, 5, _UPB_LABEL_PACKED},
  2548. {3, UPB_SIZE(4, 8), 1, 0, 12, 1},
  2549. {4, UPB_SIZE(12, 24), 2, 0, 12, 1},
  2550. {6, UPB_SIZE(28, 56), 0, 0, 12, 3},
  2551. };
  2552. const upb_msglayout google_protobuf_SourceCodeInfo_Location_msginit = {
  2553. NULL,
  2554. &google_protobuf_SourceCodeInfo_Location__fields[0],
  2555. UPB_SIZE(32, 64), 5, false,
  2556. };
  2557. static const upb_msglayout *const google_protobuf_GeneratedCodeInfo_submsgs[1] = {
  2558. &google_protobuf_GeneratedCodeInfo_Annotation_msginit,
  2559. };
  2560. static const upb_msglayout_field google_protobuf_GeneratedCodeInfo__fields[1] = {
  2561. {1, UPB_SIZE(0, 0), 0, 0, 11, 3},
  2562. };
  2563. const upb_msglayout google_protobuf_GeneratedCodeInfo_msginit = {
  2564. &google_protobuf_GeneratedCodeInfo_submsgs[0],
  2565. &google_protobuf_GeneratedCodeInfo__fields[0],
  2566. UPB_SIZE(4, 8), 1, false,
  2567. };
  2568. static const upb_msglayout_field google_protobuf_GeneratedCodeInfo_Annotation__fields[4] = {
  2569. {1, UPB_SIZE(20, 32), 0, 0, 5, _UPB_LABEL_PACKED},
  2570. {2, UPB_SIZE(12, 16), 3, 0, 12, 1},
  2571. {3, UPB_SIZE(4, 4), 1, 0, 5, 1},
  2572. {4, UPB_SIZE(8, 8), 2, 0, 5, 1},
  2573. };
  2574. const upb_msglayout google_protobuf_GeneratedCodeInfo_Annotation_msginit = {
  2575. NULL,
  2576. &google_protobuf_GeneratedCodeInfo_Annotation__fields[0],
  2577. UPB_SIZE(24, 48), 4, false,
  2578. };
  2579. /* This file was generated by upbc (the upb compiler) from the input
  2580. * file:
  2581. *
  2582. * google/protobuf/descriptor.proto
  2583. *
  2584. * Do not edit -- your changes will be discarded when the file is
  2585. * regenerated. */
  2586. extern const upb_msglayout google_protobuf_FileDescriptorSet_msginit;
  2587. extern const upb_msglayout google_protobuf_FileDescriptorProto_msginit;
  2588. extern const upb_msglayout google_protobuf_DescriptorProto_msginit;
  2589. extern const upb_msglayout google_protobuf_DescriptorProto_ExtensionRange_msginit;
  2590. extern const upb_msglayout google_protobuf_DescriptorProto_ReservedRange_msginit;
  2591. extern const upb_msglayout google_protobuf_ExtensionRangeOptions_msginit;
  2592. extern const upb_msglayout google_protobuf_FieldDescriptorProto_msginit;
  2593. extern const upb_msglayout google_protobuf_OneofDescriptorProto_msginit;
  2594. extern const upb_msglayout google_protobuf_EnumDescriptorProto_msginit;
  2595. extern const upb_msglayout google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit;
  2596. extern const upb_msglayout google_protobuf_EnumValueDescriptorProto_msginit;
  2597. extern const upb_msglayout google_protobuf_ServiceDescriptorProto_msginit;
  2598. extern const upb_msglayout google_protobuf_MethodDescriptorProto_msginit;
  2599. extern const upb_msglayout google_protobuf_FileOptions_msginit;
  2600. extern const upb_msglayout google_protobuf_MessageOptions_msginit;
  2601. extern const upb_msglayout google_protobuf_FieldOptions_msginit;
  2602. extern const upb_msglayout google_protobuf_OneofOptions_msginit;
  2603. extern const upb_msglayout google_protobuf_EnumOptions_msginit;
  2604. extern const upb_msglayout google_protobuf_EnumValueOptions_msginit;
  2605. extern const upb_msglayout google_protobuf_ServiceOptions_msginit;
  2606. extern const upb_msglayout google_protobuf_MethodOptions_msginit;
  2607. extern const upb_msglayout google_protobuf_UninterpretedOption_msginit;
  2608. extern const upb_msglayout google_protobuf_UninterpretedOption_NamePart_msginit;
  2609. extern const upb_msglayout google_protobuf_SourceCodeInfo_msginit;
  2610. extern const upb_msglayout google_protobuf_SourceCodeInfo_Location_msginit;
  2611. extern const upb_msglayout google_protobuf_GeneratedCodeInfo_msginit;
  2612. extern const upb_msglayout google_protobuf_GeneratedCodeInfo_Annotation_msginit;
  2613. static const upb_msglayout *layouts[27] = {
  2614. &google_protobuf_FileDescriptorSet_msginit,
  2615. &google_protobuf_FileDescriptorProto_msginit,
  2616. &google_protobuf_DescriptorProto_msginit,
  2617. &google_protobuf_DescriptorProto_ExtensionRange_msginit,
  2618. &google_protobuf_DescriptorProto_ReservedRange_msginit,
  2619. &google_protobuf_ExtensionRangeOptions_msginit,
  2620. &google_protobuf_FieldDescriptorProto_msginit,
  2621. &google_protobuf_OneofDescriptorProto_msginit,
  2622. &google_protobuf_EnumDescriptorProto_msginit,
  2623. &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit,
  2624. &google_protobuf_EnumValueDescriptorProto_msginit,
  2625. &google_protobuf_ServiceDescriptorProto_msginit,
  2626. &google_protobuf_MethodDescriptorProto_msginit,
  2627. &google_protobuf_FileOptions_msginit,
  2628. &google_protobuf_MessageOptions_msginit,
  2629. &google_protobuf_FieldOptions_msginit,
  2630. &google_protobuf_OneofOptions_msginit,
  2631. &google_protobuf_EnumOptions_msginit,
  2632. &google_protobuf_EnumValueOptions_msginit,
  2633. &google_protobuf_ServiceOptions_msginit,
  2634. &google_protobuf_MethodOptions_msginit,
  2635. &google_protobuf_UninterpretedOption_msginit,
  2636. &google_protobuf_UninterpretedOption_NamePart_msginit,
  2637. &google_protobuf_SourceCodeInfo_msginit,
  2638. &google_protobuf_SourceCodeInfo_Location_msginit,
  2639. &google_protobuf_GeneratedCodeInfo_msginit,
  2640. &google_protobuf_GeneratedCodeInfo_Annotation_msginit,
  2641. };
  2642. static const char descriptor[7619] = {'\n', ' ', 'g', 'o', 'o', 'g', 'l', 'e', '/', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '/', 'd', 'e', 's', 'c', 'r', 'i', 'p',
  2643. 't', 'o', 'r', '.', 'p', 'r', 'o', 't', 'o', '\022', '\017', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u',
  2644. 'f', '\"', 'M', '\n', '\021', 'F', 'i', 'l', 'e', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'S', 'e', 't', '\022', '8', '\n',
  2645. '\004', 'f', 'i', 'l', 'e', '\030', '\001', ' ', '\003', '(', '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't',
  2646. 'o', 'b', 'u', 'f', '.', 'F', 'i', 'l', 'e', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R',
  2647. '\004', 'f', 'i', 'l', 'e', '\"', '\344', '\004', '\n', '\023', 'F', 'i', 'l', 'e', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P',
  2648. 'r', 'o', 't', 'o', '\022', '\022', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001', ' ', '\001', '(', '\t', 'R', '\004', 'n', 'a', 'm', 'e', '\022',
  2649. '\030', '\n', '\007', 'p', 'a', 'c', 'k', 'a', 'g', 'e', '\030', '\002', ' ', '\001', '(', '\t', 'R', '\007', 'p', 'a', 'c', 'k', 'a', 'g', 'e',
  2650. '\022', '\036', '\n', '\n', 'd', 'e', 'p', 'e', 'n', 'd', 'e', 'n', 'c', 'y', '\030', '\003', ' ', '\003', '(', '\t', 'R', '\n', 'd', 'e', 'p',
  2651. 'e', 'n', 'd', 'e', 'n', 'c', 'y', '\022', '+', '\n', '\021', 'p', 'u', 'b', 'l', 'i', 'c', '_', 'd', 'e', 'p', 'e', 'n', 'd', 'e',
  2652. 'n', 'c', 'y', '\030', '\n', ' ', '\003', '(', '\005', 'R', '\020', 'p', 'u', 'b', 'l', 'i', 'c', 'D', 'e', 'p', 'e', 'n', 'd', 'e', 'n',
  2653. 'c', 'y', '\022', '\'', '\n', '\017', 'w', 'e', 'a', 'k', '_', 'd', 'e', 'p', 'e', 'n', 'd', 'e', 'n', 'c', 'y', '\030', '\013', ' ', '\003',
  2654. '(', '\005', 'R', '\016', 'w', 'e', 'a', 'k', 'D', 'e', 'p', 'e', 'n', 'd', 'e', 'n', 'c', 'y', '\022', 'C', '\n', '\014', 'm', 'e', 's',
  2655. 's', 'a', 'g', 'e', '_', 't', 'y', 'p', 'e', '\030', '\004', ' ', '\003', '(', '\013', '2', ' ', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.',
  2656. 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R',
  2657. '\013', 'm', 'e', 's', 's', 'a', 'g', 'e', 'T', 'y', 'p', 'e', '\022', 'A', '\n', '\t', 'e', 'n', 'u', 'm', '_', 't', 'y', 'p', 'e',
  2658. '\030', '\005', ' ', '\003', '(', '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.',
  2659. 'E', 'n', 'u', 'm', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R', '\010', 'e', 'n', 'u', 'm',
  2660. 'T', 'y', 'p', 'e', '\022', 'A', '\n', '\007', 's', 'e', 'r', 'v', 'i', 'c', 'e', '\030', '\006', ' ', '\003', '(', '\013', '2', '\'', '.', 'g',
  2661. 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'S', 'e', 'r', 'v', 'i', 'c', 'e', 'D', 'e', 's',
  2662. 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R', '\007', 's', 'e', 'r', 'v', 'i', 'c', 'e', '\022', 'C', '\n', '\t',
  2663. 'e', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', '\030', '\007', ' ', '\003', '(', '\013', '2', '%', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.',
  2664. 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'F', 'i', 'e', 'l', 'd', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P',
  2665. 'r', 'o', 't', 'o', 'R', '\t', 'e', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', '\022', '6', '\n', '\007', 'o', 'p', 't', 'i', 'o', 'n',
  2666. 's', '\030', '\010', ' ', '\001', '(', '\013', '2', '\034', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f',
  2667. '.', 'F', 'i', 'l', 'e', 'O', 'p', 't', 'i', 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\022', 'I', '\n', '\020',
  2668. 's', 'o', 'u', 'r', 'c', 'e', '_', 'c', 'o', 'd', 'e', '_', 'i', 'n', 'f', 'o', '\030', '\t', ' ', '\001', '(', '\013', '2', '\037', '.',
  2669. 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'S', 'o', 'u', 'r', 'c', 'e', 'C', 'o', 'd',
  2670. 'e', 'I', 'n', 'f', 'o', 'R', '\016', 's', 'o', 'u', 'r', 'c', 'e', 'C', 'o', 'd', 'e', 'I', 'n', 'f', 'o', '\022', '\026', '\n', '\006',
  2671. 's', 'y', 'n', 't', 'a', 'x', '\030', '\014', ' ', '\001', '(', '\t', 'R', '\006', 's', 'y', 'n', 't', 'a', 'x', '\"', '\271', '\006', '\n', '\017',
  2672. 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '\022', '\022', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001',
  2673. ' ', '\001', '(', '\t', 'R', '\004', 'n', 'a', 'm', 'e', '\022', ';', '\n', '\005', 'f', 'i', 'e', 'l', 'd', '\030', '\002', ' ', '\003', '(', '\013',
  2674. '2', '%', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'F', 'i', 'e', 'l', 'd', 'D',
  2675. 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R', '\005', 'f', 'i', 'e', 'l', 'd', '\022', 'C', '\n', '\t',
  2676. 'e', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', '\030', '\006', ' ', '\003', '(', '\013', '2', '%', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.',
  2677. 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'F', 'i', 'e', 'l', 'd', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P',
  2678. 'r', 'o', 't', 'o', 'R', '\t', 'e', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', '\022', 'A', '\n', '\013', 'n', 'e', 's', 't', 'e', 'd',
  2679. '_', 't', 'y', 'p', 'e', '\030', '\003', ' ', '\003', '(', '\013', '2', ' ', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't',
  2680. 'o', 'b', 'u', 'f', '.', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R', '\n', 'n', 'e', 's',
  2681. 't', 'e', 'd', 'T', 'y', 'p', 'e', '\022', 'A', '\n', '\t', 'e', 'n', 'u', 'm', '_', 't', 'y', 'p', 'e', '\030', '\004', ' ', '\003', '(',
  2682. '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'E', 'n', 'u', 'm', 'D',
  2683. 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R', '\010', 'e', 'n', 'u', 'm', 'T', 'y', 'p', 'e', '\022',
  2684. 'X', '\n', '\017', 'e', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', '_', 'r', 'a', 'n', 'g', 'e', '\030', '\005', ' ', '\003', '(', '\013', '2',
  2685. '/', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'D', 'e', 's', 'c', 'r', 'i', 'p',
  2686. 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '.', 'E', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', 'R', 'a', 'n', 'g', 'e', 'R', '\016',
  2687. 'e', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', 'R', 'a', 'n', 'g', 'e', '\022', 'D', '\n', '\n', 'o', 'n', 'e', 'o', 'f', '_', 'd',
  2688. 'e', 'c', 'l', '\030', '\010', ' ', '\003', '(', '\013', '2', '%', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b',
  2689. 'u', 'f', '.', 'O', 'n', 'e', 'o', 'f', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R', '\t',
  2690. 'o', 'n', 'e', 'o', 'f', 'D', 'e', 'c', 'l', '\022', '9', '\n', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\030', '\007', ' ', '\001', '(',
  2691. '\013', '2', '\037', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'M', 'e', 's', 's', 'a',
  2692. 'g', 'e', 'O', 'p', 't', 'i', 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\022', 'U', '\n', '\016', 'r', 'e', 's',
  2693. 'e', 'r', 'v', 'e', 'd', '_', 'r', 'a', 'n', 'g', 'e', '\030', '\t', ' ', '\003', '(', '\013', '2', '.', '.', 'g', 'o', 'o', 'g', 'l',
  2694. 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't',
  2695. 'o', '.', 'R', 'e', 's', 'e', 'r', 'v', 'e', 'd', 'R', 'a', 'n', 'g', 'e', 'R', '\r', 'r', 'e', 's', 'e', 'r', 'v', 'e', 'd',
  2696. 'R', 'a', 'n', 'g', 'e', '\022', '#', '\n', '\r', 'r', 'e', 's', 'e', 'r', 'v', 'e', 'd', '_', 'n', 'a', 'm', 'e', '\030', '\n', ' ',
  2697. '\003', '(', '\t', 'R', '\014', 'r', 'e', 's', 'e', 'r', 'v', 'e', 'd', 'N', 'a', 'm', 'e', '\032', 'z', '\n', '\016', 'E', 'x', 't', 'e',
  2698. 'n', 's', 'i', 'o', 'n', 'R', 'a', 'n', 'g', 'e', '\022', '\024', '\n', '\005', 's', 't', 'a', 'r', 't', '\030', '\001', ' ', '\001', '(', '\005',
  2699. 'R', '\005', 's', 't', 'a', 'r', 't', '\022', '\020', '\n', '\003', 'e', 'n', 'd', '\030', '\002', ' ', '\001', '(', '\005', 'R', '\003', 'e', 'n', 'd',
  2700. '\022', '@', '\n', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\030', '\003', ' ', '\001', '(', '\013', '2', '&', '.', 'g', 'o', 'o', 'g', 'l',
  2701. 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'E', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', 'R', 'a', 'n', 'g', 'e',
  2702. 'O', 'p', 't', 'i', 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\032', '7', '\n', '\r', 'R', 'e', 's', 'e', 'r',
  2703. 'v', 'e', 'd', 'R', 'a', 'n', 'g', 'e', '\022', '\024', '\n', '\005', 's', 't', 'a', 'r', 't', '\030', '\001', ' ', '\001', '(', '\005', 'R', '\005',
  2704. 's', 't', 'a', 'r', 't', '\022', '\020', '\n', '\003', 'e', 'n', 'd', '\030', '\002', ' ', '\001', '(', '\005', 'R', '\003', 'e', 'n', 'd', '\"', '|',
  2705. '\n', '\025', 'E', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', 'R', 'a', 'n', 'g', 'e', 'O', 'p', 't', 'i', 'o', 'n', 's', '\022', 'X',
  2706. '\n', '\024', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't', 'i', 'o', 'n', '\030', '\347', '\007',
  2707. ' ', '\003', '(', '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n',
  2708. 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n', 'i', 'n', 't', 'e',
  2709. 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', '*', '\t', '\010', '\350', '\007', '\020', '\200', '\200', '\200', '\200', '\002', '\"',
  2710. '\301', '\006', '\n', '\024', 'F', 'i', 'e', 'l', 'd', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '\022',
  2711. '\022', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001', ' ', '\001', '(', '\t', 'R', '\004', 'n', 'a', 'm', 'e', '\022', '\026', '\n', '\006', 'n', 'u',
  2712. 'm', 'b', 'e', 'r', '\030', '\003', ' ', '\001', '(', '\005', 'R', '\006', 'n', 'u', 'm', 'b', 'e', 'r', '\022', 'A', '\n', '\005', 'l', 'a', 'b',
  2713. 'e', 'l', '\030', '\004', ' ', '\001', '(', '\016', '2', '+', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u',
  2714. 'f', '.', 'F', 'i', 'e', 'l', 'd', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '.', 'L', 'a',
  2715. 'b', 'e', 'l', 'R', '\005', 'l', 'a', 'b', 'e', 'l', '\022', '>', '\n', '\004', 't', 'y', 'p', 'e', '\030', '\005', ' ', '\001', '(', '\016', '2',
  2716. '*', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'F', 'i', 'e', 'l', 'd', 'D', 'e',
  2717. 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '.', 'T', 'y', 'p', 'e', 'R', '\004', 't', 'y', 'p', 'e', '\022',
  2718. '\033', '\n', '\t', 't', 'y', 'p', 'e', '_', 'n', 'a', 'm', 'e', '\030', '\006', ' ', '\001', '(', '\t', 'R', '\010', 't', 'y', 'p', 'e', 'N',
  2719. 'a', 'm', 'e', '\022', '\032', '\n', '\010', 'e', 'x', 't', 'e', 'n', 'd', 'e', 'e', '\030', '\002', ' ', '\001', '(', '\t', 'R', '\010', 'e', 'x',
  2720. 't', 'e', 'n', 'd', 'e', 'e', '\022', '#', '\n', '\r', 'd', 'e', 'f', 'a', 'u', 'l', 't', '_', 'v', 'a', 'l', 'u', 'e', '\030', '\007',
  2721. ' ', '\001', '(', '\t', 'R', '\014', 'd', 'e', 'f', 'a', 'u', 'l', 't', 'V', 'a', 'l', 'u', 'e', '\022', '\037', '\n', '\013', 'o', 'n', 'e',
  2722. 'o', 'f', '_', 'i', 'n', 'd', 'e', 'x', '\030', '\t', ' ', '\001', '(', '\005', 'R', '\n', 'o', 'n', 'e', 'o', 'f', 'I', 'n', 'd', 'e',
  2723. 'x', '\022', '\033', '\n', '\t', 'j', 's', 'o', 'n', '_', 'n', 'a', 'm', 'e', '\030', '\n', ' ', '\001', '(', '\t', 'R', '\010', 'j', 's', 'o',
  2724. 'n', 'N', 'a', 'm', 'e', '\022', '7', '\n', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\030', '\010', ' ', '\001', '(', '\013', '2', '\035', '.',
  2725. 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'F', 'i', 'e', 'l', 'd', 'O', 'p', 't', 'i',
  2726. 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\022', '\'', '\n', '\017', 'p', 'r', 'o', 't', 'o', '3', '_', 'o', 'p',
  2727. 't', 'i', 'o', 'n', 'a', 'l', '\030', '\021', ' ', '\001', '(', '\010', 'R', '\016', 'p', 'r', 'o', 't', 'o', '3', 'O', 'p', 't', 'i', 'o',
  2728. 'n', 'a', 'l', '\"', '\266', '\002', '\n', '\004', 'T', 'y', 'p', 'e', '\022', '\017', '\n', '\013', 'T', 'Y', 'P', 'E', '_', 'D', 'O', 'U', 'B',
  2729. 'L', 'E', '\020', '\001', '\022', '\016', '\n', '\n', 'T', 'Y', 'P', 'E', '_', 'F', 'L', 'O', 'A', 'T', '\020', '\002', '\022', '\016', '\n', '\n', 'T',
  2730. 'Y', 'P', 'E', '_', 'I', 'N', 'T', '6', '4', '\020', '\003', '\022', '\017', '\n', '\013', 'T', 'Y', 'P', 'E', '_', 'U', 'I', 'N', 'T', '6',
  2731. '4', '\020', '\004', '\022', '\016', '\n', '\n', 'T', 'Y', 'P', 'E', '_', 'I', 'N', 'T', '3', '2', '\020', '\005', '\022', '\020', '\n', '\014', 'T', 'Y',
  2732. 'P', 'E', '_', 'F', 'I', 'X', 'E', 'D', '6', '4', '\020', '\006', '\022', '\020', '\n', '\014', 'T', 'Y', 'P', 'E', '_', 'F', 'I', 'X', 'E',
  2733. 'D', '3', '2', '\020', '\007', '\022', '\r', '\n', '\t', 'T', 'Y', 'P', 'E', '_', 'B', 'O', 'O', 'L', '\020', '\010', '\022', '\017', '\n', '\013', 'T',
  2734. 'Y', 'P', 'E', '_', 'S', 'T', 'R', 'I', 'N', 'G', '\020', '\t', '\022', '\016', '\n', '\n', 'T', 'Y', 'P', 'E', '_', 'G', 'R', 'O', 'U',
  2735. 'P', '\020', '\n', '\022', '\020', '\n', '\014', 'T', 'Y', 'P', 'E', '_', 'M', 'E', 'S', 'S', 'A', 'G', 'E', '\020', '\013', '\022', '\016', '\n', '\n',
  2736. 'T', 'Y', 'P', 'E', '_', 'B', 'Y', 'T', 'E', 'S', '\020', '\014', '\022', '\017', '\n', '\013', 'T', 'Y', 'P', 'E', '_', 'U', 'I', 'N', 'T',
  2737. '3', '2', '\020', '\r', '\022', '\r', '\n', '\t', 'T', 'Y', 'P', 'E', '_', 'E', 'N', 'U', 'M', '\020', '\016', '\022', '\021', '\n', '\r', 'T', 'Y',
  2738. 'P', 'E', '_', 'S', 'F', 'I', 'X', 'E', 'D', '3', '2', '\020', '\017', '\022', '\021', '\n', '\r', 'T', 'Y', 'P', 'E', '_', 'S', 'F', 'I',
  2739. 'X', 'E', 'D', '6', '4', '\020', '\020', '\022', '\017', '\n', '\013', 'T', 'Y', 'P', 'E', '_', 'S', 'I', 'N', 'T', '3', '2', '\020', '\021', '\022',
  2740. '\017', '\n', '\013', 'T', 'Y', 'P', 'E', '_', 'S', 'I', 'N', 'T', '6', '4', '\020', '\022', '\"', 'C', '\n', '\005', 'L', 'a', 'b', 'e', 'l',
  2741. '\022', '\022', '\n', '\016', 'L', 'A', 'B', 'E', 'L', '_', 'O', 'P', 'T', 'I', 'O', 'N', 'A', 'L', '\020', '\001', '\022', '\022', '\n', '\016', 'L',
  2742. 'A', 'B', 'E', 'L', '_', 'R', 'E', 'Q', 'U', 'I', 'R', 'E', 'D', '\020', '\002', '\022', '\022', '\n', '\016', 'L', 'A', 'B', 'E', 'L', '_',
  2743. 'R', 'E', 'P', 'E', 'A', 'T', 'E', 'D', '\020', '\003', '\"', 'c', '\n', '\024', 'O', 'n', 'e', 'o', 'f', 'D', 'e', 's', 'c', 'r', 'i',
  2744. 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '\022', '\022', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001', ' ', '\001', '(', '\t', 'R', '\004',
  2745. 'n', 'a', 'm', 'e', '\022', '7', '\n', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\030', '\002', ' ', '\001', '(', '\013', '2', '\035', '.', 'g',
  2746. 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'O', 'n', 'e', 'o', 'f', 'O', 'p', 't', 'i', 'o',
  2747. 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\"', '\343', '\002', '\n', '\023', 'E', 'n', 'u', 'm', 'D', 'e', 's', 'c', 'r',
  2748. 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '\022', '\022', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001', ' ', '\001', '(', '\t', 'R',
  2749. '\004', 'n', 'a', 'm', 'e', '\022', '?', '\n', '\005', 'v', 'a', 'l', 'u', 'e', '\030', '\002', ' ', '\003', '(', '\013', '2', ')', '.', 'g', 'o',
  2750. 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'E', 'n', 'u', 'm', 'V', 'a', 'l', 'u', 'e', 'D', 'e',
  2751. 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 'R', '\005', 'v', 'a', 'l', 'u', 'e', '\022', '6', '\n', '\007', 'o',
  2752. 'p', 't', 'i', 'o', 'n', 's', '\030', '\003', ' ', '\001', '(', '\013', '2', '\034', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o',
  2753. 't', 'o', 'b', 'u', 'f', '.', 'E', 'n', 'u', 'm', 'O', 'p', 't', 'i', 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n',
  2754. 's', '\022', ']', '\n', '\016', 'r', 'e', 's', 'e', 'r', 'v', 'e', 'd', '_', 'r', 'a', 'n', 'g', 'e', '\030', '\004', ' ', '\003', '(', '\013',
  2755. '2', '6', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'E', 'n', 'u', 'm', 'D', 'e',
  2756. 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '.', 'E', 'n', 'u', 'm', 'R', 'e', 's', 'e', 'r', 'v', 'e',
  2757. 'd', 'R', 'a', 'n', 'g', 'e', 'R', '\r', 'r', 'e', 's', 'e', 'r', 'v', 'e', 'd', 'R', 'a', 'n', 'g', 'e', '\022', '#', '\n', '\r',
  2758. 'r', 'e', 's', 'e', 'r', 'v', 'e', 'd', '_', 'n', 'a', 'm', 'e', '\030', '\005', ' ', '\003', '(', '\t', 'R', '\014', 'r', 'e', 's', 'e',
  2759. 'r', 'v', 'e', 'd', 'N', 'a', 'm', 'e', '\032', ';', '\n', '\021', 'E', 'n', 'u', 'm', 'R', 'e', 's', 'e', 'r', 'v', 'e', 'd', 'R',
  2760. 'a', 'n', 'g', 'e', '\022', '\024', '\n', '\005', 's', 't', 'a', 'r', 't', '\030', '\001', ' ', '\001', '(', '\005', 'R', '\005', 's', 't', 'a', 'r',
  2761. 't', '\022', '\020', '\n', '\003', 'e', 'n', 'd', '\030', '\002', ' ', '\001', '(', '\005', 'R', '\003', 'e', 'n', 'd', '\"', '\203', '\001', '\n', '\030', 'E',
  2762. 'n', 'u', 'm', 'V', 'a', 'l', 'u', 'e', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '\022', '\022',
  2763. '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001', ' ', '\001', '(', '\t', 'R', '\004', 'n', 'a', 'm', 'e', '\022', '\026', '\n', '\006', 'n', 'u', 'm',
  2764. 'b', 'e', 'r', '\030', '\002', ' ', '\001', '(', '\005', 'R', '\006', 'n', 'u', 'm', 'b', 'e', 'r', '\022', ';', '\n', '\007', 'o', 'p', 't', 'i',
  2765. 'o', 'n', 's', '\030', '\003', ' ', '\001', '(', '\013', '2', '!', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b',
  2766. 'u', 'f', '.', 'E', 'n', 'u', 'm', 'V', 'a', 'l', 'u', 'e', 'O', 'p', 't', 'i', 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i',
  2767. 'o', 'n', 's', '\"', '\247', '\001', '\n', '\026', 'S', 'e', 'r', 'v', 'i', 'c', 'e', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r',
  2768. 'P', 'r', 'o', 't', 'o', '\022', '\022', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001', ' ', '\001', '(', '\t', 'R', '\004', 'n', 'a', 'm', 'e',
  2769. '\022', '>', '\n', '\006', 'm', 'e', 't', 'h', 'o', 'd', '\030', '\002', ' ', '\003', '(', '\013', '2', '&', '.', 'g', 'o', 'o', 'g', 'l', 'e',
  2770. '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'M', 'e', 't', 'h', 'o', 'd', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o',
  2771. 'r', 'P', 'r', 'o', 't', 'o', 'R', '\006', 'm', 'e', 't', 'h', 'o', 'd', '\022', '9', '\n', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's',
  2772. '\030', '\003', ' ', '\001', '(', '\013', '2', '\037', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.',
  2773. 'S', 'e', 'r', 'v', 'i', 'c', 'e', 'O', 'p', 't', 'i', 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\"', '\211',
  2774. '\002', '\n', '\025', 'M', 'e', 't', 'h', 'o', 'd', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', '\022',
  2775. '\022', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\001', ' ', '\001', '(', '\t', 'R', '\004', 'n', 'a', 'm', 'e', '\022', '\035', '\n', '\n', 'i', 'n',
  2776. 'p', 'u', 't', '_', 't', 'y', 'p', 'e', '\030', '\002', ' ', '\001', '(', '\t', 'R', '\t', 'i', 'n', 'p', 'u', 't', 'T', 'y', 'p', 'e',
  2777. '\022', '\037', '\n', '\013', 'o', 'u', 't', 'p', 'u', 't', '_', 't', 'y', 'p', 'e', '\030', '\003', ' ', '\001', '(', '\t', 'R', '\n', 'o', 'u',
  2778. 't', 'p', 'u', 't', 'T', 'y', 'p', 'e', '\022', '8', '\n', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\030', '\004', ' ', '\001', '(', '\013',
  2779. '2', '\036', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'M', 'e', 't', 'h', 'o', 'd',
  2780. 'O', 'p', 't', 'i', 'o', 'n', 's', 'R', '\007', 'o', 'p', 't', 'i', 'o', 'n', 's', '\022', '0', '\n', '\020', 'c', 'l', 'i', 'e', 'n',
  2781. 't', '_', 's', 't', 'r', 'e', 'a', 'm', 'i', 'n', 'g', '\030', '\005', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R',
  2782. '\017', 'c', 'l', 'i', 'e', 'n', 't', 'S', 't', 'r', 'e', 'a', 'm', 'i', 'n', 'g', '\022', '0', '\n', '\020', 's', 'e', 'r', 'v', 'e',
  2783. 'r', '_', 's', 't', 'r', 'e', 'a', 'm', 'i', 'n', 'g', '\030', '\006', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R',
  2784. '\017', 's', 'e', 'r', 'v', 'e', 'r', 'S', 't', 'r', 'e', 'a', 'm', 'i', 'n', 'g', '\"', '\221', '\t', '\n', '\013', 'F', 'i', 'l', 'e',
  2785. 'O', 'p', 't', 'i', 'o', 'n', 's', '\022', '!', '\n', '\014', 'j', 'a', 'v', 'a', '_', 'p', 'a', 'c', 'k', 'a', 'g', 'e', '\030', '\001',
  2786. ' ', '\001', '(', '\t', 'R', '\013', 'j', 'a', 'v', 'a', 'P', 'a', 'c', 'k', 'a', 'g', 'e', '\022', '0', '\n', '\024', 'j', 'a', 'v', 'a',
  2787. '_', 'o', 'u', 't', 'e', 'r', '_', 'c', 'l', 'a', 's', 's', 'n', 'a', 'm', 'e', '\030', '\010', ' ', '\001', '(', '\t', 'R', '\022', 'j',
  2788. 'a', 'v', 'a', 'O', 'u', 't', 'e', 'r', 'C', 'l', 'a', 's', 's', 'n', 'a', 'm', 'e', '\022', '5', '\n', '\023', 'j', 'a', 'v', 'a',
  2789. '_', 'm', 'u', 'l', 't', 'i', 'p', 'l', 'e', '_', 'f', 'i', 'l', 'e', 's', '\030', '\n', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a',
  2790. 'l', 's', 'e', 'R', '\021', 'j', 'a', 'v', 'a', 'M', 'u', 'l', 't', 'i', 'p', 'l', 'e', 'F', 'i', 'l', 'e', 's', '\022', 'D', '\n',
  2791. '\035', 'j', 'a', 'v', 'a', '_', 'g', 'e', 'n', 'e', 'r', 'a', 't', 'e', '_', 'e', 'q', 'u', 'a', 'l', 's', '_', 'a', 'n', 'd',
  2792. '_', 'h', 'a', 's', 'h', '\030', '\024', ' ', '\001', '(', '\010', 'B', '\002', '\030', '\001', 'R', '\031', 'j', 'a', 'v', 'a', 'G', 'e', 'n', 'e',
  2793. 'r', 'a', 't', 'e', 'E', 'q', 'u', 'a', 'l', 's', 'A', 'n', 'd', 'H', 'a', 's', 'h', '\022', ':', '\n', '\026', 'j', 'a', 'v', 'a',
  2794. '_', 's', 't', 'r', 'i', 'n', 'g', '_', 'c', 'h', 'e', 'c', 'k', '_', 'u', 't', 'f', '8', '\030', '\033', ' ', '\001', '(', '\010', ':',
  2795. '\005', 'f', 'a', 'l', 's', 'e', 'R', '\023', 'j', 'a', 'v', 'a', 'S', 't', 'r', 'i', 'n', 'g', 'C', 'h', 'e', 'c', 'k', 'U', 't',
  2796. 'f', '8', '\022', 'S', '\n', '\014', 'o', 'p', 't', 'i', 'm', 'i', 'z', 'e', '_', 'f', 'o', 'r', '\030', '\t', ' ', '\001', '(', '\016', '2',
  2797. ')', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'F', 'i', 'l', 'e', 'O', 'p', 't',
  2798. 'i', 'o', 'n', 's', '.', 'O', 'p', 't', 'i', 'm', 'i', 'z', 'e', 'M', 'o', 'd', 'e', ':', '\005', 'S', 'P', 'E', 'E', 'D', 'R',
  2799. '\013', 'o', 'p', 't', 'i', 'm', 'i', 'z', 'e', 'F', 'o', 'r', '\022', '\035', '\n', '\n', 'g', 'o', '_', 'p', 'a', 'c', 'k', 'a', 'g',
  2800. 'e', '\030', '\013', ' ', '\001', '(', '\t', 'R', '\t', 'g', 'o', 'P', 'a', 'c', 'k', 'a', 'g', 'e', '\022', '5', '\n', '\023', 'c', 'c', '_',
  2801. 'g', 'e', 'n', 'e', 'r', 'i', 'c', '_', 's', 'e', 'r', 'v', 'i', 'c', 'e', 's', '\030', '\020', ' ', '\001', '(', '\010', ':', '\005', 'f',
  2802. 'a', 'l', 's', 'e', 'R', '\021', 'c', 'c', 'G', 'e', 'n', 'e', 'r', 'i', 'c', 'S', 'e', 'r', 'v', 'i', 'c', 'e', 's', '\022', '9',
  2803. '\n', '\025', 'j', 'a', 'v', 'a', '_', 'g', 'e', 'n', 'e', 'r', 'i', 'c', '_', 's', 'e', 'r', 'v', 'i', 'c', 'e', 's', '\030', '\021',
  2804. ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\023', 'j', 'a', 'v', 'a', 'G', 'e', 'n', 'e', 'r', 'i', 'c', 'S',
  2805. 'e', 'r', 'v', 'i', 'c', 'e', 's', '\022', '5', '\n', '\023', 'p', 'y', '_', 'g', 'e', 'n', 'e', 'r', 'i', 'c', '_', 's', 'e', 'r',
  2806. 'v', 'i', 'c', 'e', 's', '\030', '\022', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\021', 'p', 'y', 'G', 'e', 'n',
  2807. 'e', 'r', 'i', 'c', 'S', 'e', 'r', 'v', 'i', 'c', 'e', 's', '\022', '7', '\n', '\024', 'p', 'h', 'p', '_', 'g', 'e', 'n', 'e', 'r',
  2808. 'i', 'c', '_', 's', 'e', 'r', 'v', 'i', 'c', 'e', 's', '\030', '*', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R',
  2809. '\022', 'p', 'h', 'p', 'G', 'e', 'n', 'e', 'r', 'i', 'c', 'S', 'e', 'r', 'v', 'i', 'c', 'e', 's', '\022', '%', '\n', '\n', 'd', 'e',
  2810. 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\030', '\027', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\n', 'd', 'e',
  2811. 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\022', '.', '\n', '\020', 'c', 'c', '_', 'e', 'n', 'a', 'b', 'l', 'e', '_', 'a', 'r', 'e',
  2812. 'n', 'a', 's', '\030', '\037', ' ', '\001', '(', '\010', ':', '\004', 't', 'r', 'u', 'e', 'R', '\016', 'c', 'c', 'E', 'n', 'a', 'b', 'l', 'e',
  2813. 'A', 'r', 'e', 'n', 'a', 's', '\022', '*', '\n', '\021', 'o', 'b', 'j', 'c', '_', 'c', 'l', 'a', 's', 's', '_', 'p', 'r', 'e', 'f',
  2814. 'i', 'x', '\030', '$', ' ', '\001', '(', '\t', 'R', '\017', 'o', 'b', 'j', 'c', 'C', 'l', 'a', 's', 's', 'P', 'r', 'e', 'f', 'i', 'x',
  2815. '\022', ')', '\n', '\020', 'c', 's', 'h', 'a', 'r', 'p', '_', 'n', 'a', 'm', 'e', 's', 'p', 'a', 'c', 'e', '\030', '%', ' ', '\001', '(',
  2816. '\t', 'R', '\017', 'c', 's', 'h', 'a', 'r', 'p', 'N', 'a', 'm', 'e', 's', 'p', 'a', 'c', 'e', '\022', '!', '\n', '\014', 's', 'w', 'i',
  2817. 'f', 't', '_', 'p', 'r', 'e', 'f', 'i', 'x', '\030', '\'', ' ', '\001', '(', '\t', 'R', '\013', 's', 'w', 'i', 'f', 't', 'P', 'r', 'e',
  2818. 'f', 'i', 'x', '\022', '(', '\n', '\020', 'p', 'h', 'p', '_', 'c', 'l', 'a', 's', 's', '_', 'p', 'r', 'e', 'f', 'i', 'x', '\030', '(',
  2819. ' ', '\001', '(', '\t', 'R', '\016', 'p', 'h', 'p', 'C', 'l', 'a', 's', 's', 'P', 'r', 'e', 'f', 'i', 'x', '\022', '#', '\n', '\r', 'p',
  2820. 'h', 'p', '_', 'n', 'a', 'm', 'e', 's', 'p', 'a', 'c', 'e', '\030', ')', ' ', '\001', '(', '\t', 'R', '\014', 'p', 'h', 'p', 'N', 'a',
  2821. 'm', 'e', 's', 'p', 'a', 'c', 'e', '\022', '4', '\n', '\026', 'p', 'h', 'p', '_', 'm', 'e', 't', 'a', 'd', 'a', 't', 'a', '_', 'n',
  2822. 'a', 'm', 'e', 's', 'p', 'a', 'c', 'e', '\030', ',', ' ', '\001', '(', '\t', 'R', '\024', 'p', 'h', 'p', 'M', 'e', 't', 'a', 'd', 'a',
  2823. 't', 'a', 'N', 'a', 'm', 'e', 's', 'p', 'a', 'c', 'e', '\022', '!', '\n', '\014', 'r', 'u', 'b', 'y', '_', 'p', 'a', 'c', 'k', 'a',
  2824. 'g', 'e', '\030', '-', ' ', '\001', '(', '\t', 'R', '\013', 'r', 'u', 'b', 'y', 'P', 'a', 'c', 'k', 'a', 'g', 'e', '\022', 'X', '\n', '\024',
  2825. 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't', 'i', 'o', 'n', '\030', '\347', '\007', ' ', '\003',
  2826. '(', '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n',
  2827. 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p',
  2828. 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', '\"', ':', '\n', '\014', 'O', 'p', 't', 'i', 'm', 'i', 'z', 'e', 'M', 'o',
  2829. 'd', 'e', '\022', '\t', '\n', '\005', 'S', 'P', 'E', 'E', 'D', '\020', '\001', '\022', '\r', '\n', '\t', 'C', 'O', 'D', 'E', '_', 'S', 'I', 'Z',
  2830. 'E', '\020', '\002', '\022', '\020', '\n', '\014', 'L', 'I', 'T', 'E', '_', 'R', 'U', 'N', 'T', 'I', 'M', 'E', '\020', '\003', '*', '\t', '\010', '\350',
  2831. '\007', '\020', '\200', '\200', '\200', '\200', '\002', 'J', '\004', '\010', '&', '\020', '\'', '\"', '\321', '\002', '\n', '\016', 'M', 'e', 's', 's', 'a', 'g', 'e',
  2832. 'O', 'p', 't', 'i', 'o', 'n', 's', '\022', '<', '\n', '\027', 'm', 'e', 's', 's', 'a', 'g', 'e', '_', 's', 'e', 't', '_', 'w', 'i',
  2833. 'r', 'e', '_', 'f', 'o', 'r', 'm', 'a', 't', '\030', '\001', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\024', 'm',
  2834. 'e', 's', 's', 'a', 'g', 'e', 'S', 'e', 't', 'W', 'i', 'r', 'e', 'F', 'o', 'r', 'm', 'a', 't', '\022', 'L', '\n', '\037', 'n', 'o',
  2835. '_', 's', 't', 'a', 'n', 'd', 'a', 'r', 'd', '_', 'd', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', '_', 'a', 'c', 'c', 'e',
  2836. 's', 's', 'o', 'r', '\030', '\002', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\034', 'n', 'o', 'S', 't', 'a', 'n',
  2837. 'd', 'a', 'r', 'd', 'D', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'A', 'c', 'c', 'e', 's', 's', 'o', 'r', '\022', '%', '\n',
  2838. '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\030', '\003', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R',
  2839. '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\022', '\033', '\n', '\t', 'm', 'a', 'p', '_', 'e', 'n', 't', 'r', 'y', '\030',
  2840. '\007', ' ', '\001', '(', '\010', 'R', '\010', 'm', 'a', 'p', 'E', 'n', 't', 'r', 'y', '\022', 'X', '\n', '\024', 'u', 'n', 'i', 'n', 't', 'e',
  2841. 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't', 'i', 'o', 'n', '\030', '\347', '\007', ' ', '\003', '(', '\013', '2', '$', '.', 'g',
  2842. 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e',
  2843. 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O',
  2844. 'p', 't', 'i', 'o', 'n', '*', '\t', '\010', '\350', '\007', '\020', '\200', '\200', '\200', '\200', '\002', 'J', '\004', '\010', '\010', '\020', '\t', 'J', '\004', '\010',
  2845. '\t', '\020', '\n', '\"', '\342', '\003', '\n', '\014', 'F', 'i', 'e', 'l', 'd', 'O', 'p', 't', 'i', 'o', 'n', 's', '\022', 'A', '\n', '\005', 'c',
  2846. 't', 'y', 'p', 'e', '\030', '\001', ' ', '\001', '(', '\016', '2', '#', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o',
  2847. 'b', 'u', 'f', '.', 'F', 'i', 'e', 'l', 'd', 'O', 'p', 't', 'i', 'o', 'n', 's', '.', 'C', 'T', 'y', 'p', 'e', ':', '\006', 'S',
  2848. 'T', 'R', 'I', 'N', 'G', 'R', '\005', 'c', 't', 'y', 'p', 'e', '\022', '\026', '\n', '\006', 'p', 'a', 'c', 'k', 'e', 'd', '\030', '\002', ' ',
  2849. '\001', '(', '\010', 'R', '\006', 'p', 'a', 'c', 'k', 'e', 'd', '\022', 'G', '\n', '\006', 'j', 's', 't', 'y', 'p', 'e', '\030', '\006', ' ', '\001',
  2850. '(', '\016', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'F', 'i', 'e', 'l',
  2851. 'd', 'O', 'p', 't', 'i', 'o', 'n', 's', '.', 'J', 'S', 'T', 'y', 'p', 'e', ':', '\t', 'J', 'S', '_', 'N', 'O', 'R', 'M', 'A',
  2852. 'L', 'R', '\006', 'j', 's', 't', 'y', 'p', 'e', '\022', '\031', '\n', '\004', 'l', 'a', 'z', 'y', '\030', '\005', ' ', '\001', '(', '\010', ':', '\005',
  2853. 'f', 'a', 'l', 's', 'e', 'R', '\004', 'l', 'a', 'z', 'y', '\022', '%', '\n', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd',
  2854. '\030', '\003', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd',
  2855. '\022', '\031', '\n', '\004', 'w', 'e', 'a', 'k', '\030', '\n', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\004', 'w', 'e',
  2856. 'a', 'k', '\022', 'X', '\n', '\024', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't', 'i', 'o',
  2857. 'n', '\030', '\347', '\007', ' ', '\003', '(', '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u',
  2858. 'f', '.', 'U', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n',
  2859. 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', '\"', '/', '\n', '\005', 'C', 'T', 'y', 'p',
  2860. 'e', '\022', '\n', '\n', '\006', 'S', 'T', 'R', 'I', 'N', 'G', '\020', '\000', '\022', '\010', '\n', '\004', 'C', 'O', 'R', 'D', '\020', '\001', '\022', '\020',
  2861. '\n', '\014', 'S', 'T', 'R', 'I', 'N', 'G', '_', 'P', 'I', 'E', 'C', 'E', '\020', '\002', '\"', '5', '\n', '\006', 'J', 'S', 'T', 'y', 'p',
  2862. 'e', '\022', '\r', '\n', '\t', 'J', 'S', '_', 'N', 'O', 'R', 'M', 'A', 'L', '\020', '\000', '\022', '\r', '\n', '\t', 'J', 'S', '_', 'S', 'T',
  2863. 'R', 'I', 'N', 'G', '\020', '\001', '\022', '\r', '\n', '\t', 'J', 'S', '_', 'N', 'U', 'M', 'B', 'E', 'R', '\020', '\002', '*', '\t', '\010', '\350',
  2864. '\007', '\020', '\200', '\200', '\200', '\200', '\002', 'J', '\004', '\010', '\004', '\020', '\005', '\"', 's', '\n', '\014', 'O', 'n', 'e', 'o', 'f', 'O', 'p', 't',
  2865. 'i', 'o', 'n', 's', '\022', 'X', '\n', '\024', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't',
  2866. 'i', 'o', 'n', '\030', '\347', '\007', ' ', '\003', '(', '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o',
  2867. 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023',
  2868. 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', '*', '\t', '\010', '\350', '\007', '\020',
  2869. '\200', '\200', '\200', '\200', '\002', '\"', '\300', '\001', '\n', '\013', 'E', 'n', 'u', 'm', 'O', 'p', 't', 'i', 'o', 'n', 's', '\022', '\037', '\n', '\013',
  2870. 'a', 'l', 'l', 'o', 'w', '_', 'a', 'l', 'i', 'a', 's', '\030', '\002', ' ', '\001', '(', '\010', 'R', '\n', 'a', 'l', 'l', 'o', 'w', 'A',
  2871. 'l', 'i', 'a', 's', '\022', '%', '\n', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\030', '\003', ' ', '\001', '(', '\010', ':',
  2872. '\005', 'f', 'a', 'l', 's', 'e', 'R', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\022', 'X', '\n', '\024', 'u', 'n', 'i',
  2873. 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't', 'i', 'o', 'n', '\030', '\347', '\007', ' ', '\003', '(', '\013', '2',
  2874. '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n', 't', 'e', 'r',
  2875. 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't',
  2876. 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', '*', '\t', '\010', '\350', '\007', '\020', '\200', '\200', '\200', '\200', '\002', 'J', '\004', '\010', '\005', '\020', '\006',
  2877. '\"', '\236', '\001', '\n', '\020', 'E', 'n', 'u', 'm', 'V', 'a', 'l', 'u', 'e', 'O', 'p', 't', 'i', 'o', 'n', 's', '\022', '%', '\n', '\n',
  2878. 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\030', '\001', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\n',
  2879. 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\022', 'X', '\n', '\024', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't',
  2880. 'e', 'd', '_', 'o', 'p', 't', 'i', 'o', 'n', '\030', '\347', '\007', ' ', '\003', '(', '\013', '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e',
  2881. '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p',
  2882. 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n',
  2883. '*', '\t', '\010', '\350', '\007', '\020', '\200', '\200', '\200', '\200', '\002', '\"', '\234', '\001', '\n', '\016', 'S', 'e', 'r', 'v', 'i', 'c', 'e', 'O', 'p',
  2884. 't', 'i', 'o', 'n', 's', '\022', '%', '\n', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\030', '!', ' ', '\001', '(', '\010',
  2885. ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't', 'e', 'd', '\022', 'X', '\n', '\024', 'u', 'n',
  2886. 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't', 'i', 'o', 'n', '\030', '\347', '\007', ' ', '\003', '(', '\013',
  2887. '2', '$', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n', 't', 'e',
  2888. 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e',
  2889. 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', '*', '\t', '\010', '\350', '\007', '\020', '\200', '\200', '\200', '\200', '\002', '\"', '\340', '\002', '\n', '\r',
  2890. 'M', 'e', 't', 'h', 'o', 'd', 'O', 'p', 't', 'i', 'o', 'n', 's', '\022', '%', '\n', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't',
  2891. 'e', 'd', '\030', '!', ' ', '\001', '(', '\010', ':', '\005', 'f', 'a', 'l', 's', 'e', 'R', '\n', 'd', 'e', 'p', 'r', 'e', 'c', 'a', 't',
  2892. 'e', 'd', '\022', 'q', '\n', '\021', 'i', 'd', 'e', 'm', 'p', 'o', 't', 'e', 'n', 'c', 'y', '_', 'l', 'e', 'v', 'e', 'l', '\030', '\"',
  2893. ' ', '\001', '(', '\016', '2', '/', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'M', 'e',
  2894. 't', 'h', 'o', 'd', 'O', 'p', 't', 'i', 'o', 'n', 's', '.', 'I', 'd', 'e', 'm', 'p', 'o', 't', 'e', 'n', 'c', 'y', 'L', 'e',
  2895. 'v', 'e', 'l', ':', '\023', 'I', 'D', 'E', 'M', 'P', 'O', 'T', 'E', 'N', 'C', 'Y', '_', 'U', 'N', 'K', 'N', 'O', 'W', 'N', 'R',
  2896. '\020', 'i', 'd', 'e', 'm', 'p', 'o', 't', 'e', 'n', 'c', 'y', 'L', 'e', 'v', 'e', 'l', '\022', 'X', '\n', '\024', 'u', 'n', 'i', 'n',
  2897. 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', '_', 'o', 'p', 't', 'i', 'o', 'n', '\030', '\347', '\007', ' ', '\003', '(', '\013', '2', '$',
  2898. '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n', 't', 'e', 'r', 'p',
  2899. 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', 'R', '\023', 'u', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e',
  2900. 'd', 'O', 'p', 't', 'i', 'o', 'n', '\"', 'P', '\n', '\020', 'I', 'd', 'e', 'm', 'p', 'o', 't', 'e', 'n', 'c', 'y', 'L', 'e', 'v',
  2901. 'e', 'l', '\022', '\027', '\n', '\023', 'I', 'D', 'E', 'M', 'P', 'O', 'T', 'E', 'N', 'C', 'Y', '_', 'U', 'N', 'K', 'N', 'O', 'W', 'N',
  2902. '\020', '\000', '\022', '\023', '\n', '\017', 'N', 'O', '_', 'S', 'I', 'D', 'E', '_', 'E', 'F', 'F', 'E', 'C', 'T', 'S', '\020', '\001', '\022', '\016',
  2903. '\n', '\n', 'I', 'D', 'E', 'M', 'P', 'O', 'T', 'E', 'N', 'T', '\020', '\002', '*', '\t', '\010', '\350', '\007', '\020', '\200', '\200', '\200', '\200', '\002',
  2904. '\"', '\232', '\003', '\n', '\023', 'U', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o', 'n', '\022',
  2905. 'A', '\n', '\004', 'n', 'a', 'm', 'e', '\030', '\002', ' ', '\003', '(', '\013', '2', '-', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r',
  2906. 'o', 't', 'o', 'b', 'u', 'f', '.', 'U', 'n', 'i', 'n', 't', 'e', 'r', 'p', 'r', 'e', 't', 'e', 'd', 'O', 'p', 't', 'i', 'o',
  2907. 'n', '.', 'N', 'a', 'm', 'e', 'P', 'a', 'r', 't', 'R', '\004', 'n', 'a', 'm', 'e', '\022', ')', '\n', '\020', 'i', 'd', 'e', 'n', 't',
  2908. 'i', 'f', 'i', 'e', 'r', '_', 'v', 'a', 'l', 'u', 'e', '\030', '\003', ' ', '\001', '(', '\t', 'R', '\017', 'i', 'd', 'e', 'n', 't', 'i',
  2909. 'f', 'i', 'e', 'r', 'V', 'a', 'l', 'u', 'e', '\022', ',', '\n', '\022', 'p', 'o', 's', 'i', 't', 'i', 'v', 'e', '_', 'i', 'n', 't',
  2910. '_', 'v', 'a', 'l', 'u', 'e', '\030', '\004', ' ', '\001', '(', '\004', 'R', '\020', 'p', 'o', 's', 'i', 't', 'i', 'v', 'e', 'I', 'n', 't',
  2911. 'V', 'a', 'l', 'u', 'e', '\022', ',', '\n', '\022', 'n', 'e', 'g', 'a', 't', 'i', 'v', 'e', '_', 'i', 'n', 't', '_', 'v', 'a', 'l',
  2912. 'u', 'e', '\030', '\005', ' ', '\001', '(', '\003', 'R', '\020', 'n', 'e', 'g', 'a', 't', 'i', 'v', 'e', 'I', 'n', 't', 'V', 'a', 'l', 'u',
  2913. 'e', '\022', '!', '\n', '\014', 'd', 'o', 'u', 'b', 'l', 'e', '_', 'v', 'a', 'l', 'u', 'e', '\030', '\006', ' ', '\001', '(', '\001', 'R', '\013',
  2914. 'd', 'o', 'u', 'b', 'l', 'e', 'V', 'a', 'l', 'u', 'e', '\022', '!', '\n', '\014', 's', 't', 'r', 'i', 'n', 'g', '_', 'v', 'a', 'l',
  2915. 'u', 'e', '\030', '\007', ' ', '\001', '(', '\014', 'R', '\013', 's', 't', 'r', 'i', 'n', 'g', 'V', 'a', 'l', 'u', 'e', '\022', '\'', '\n', '\017',
  2916. 'a', 'g', 'g', 'r', 'e', 'g', 'a', 't', 'e', '_', 'v', 'a', 'l', 'u', 'e', '\030', '\010', ' ', '\001', '(', '\t', 'R', '\016', 'a', 'g',
  2917. 'g', 'r', 'e', 'g', 'a', 't', 'e', 'V', 'a', 'l', 'u', 'e', '\032', 'J', '\n', '\010', 'N', 'a', 'm', 'e', 'P', 'a', 'r', 't', '\022',
  2918. '\033', '\n', '\t', 'n', 'a', 'm', 'e', '_', 'p', 'a', 'r', 't', '\030', '\001', ' ', '\002', '(', '\t', 'R', '\010', 'n', 'a', 'm', 'e', 'P',
  2919. 'a', 'r', 't', '\022', '!', '\n', '\014', 'i', 's', '_', 'e', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', '\030', '\002', ' ', '\002', '(', '\010',
  2920. 'R', '\013', 'i', 's', 'E', 'x', 't', 'e', 'n', 's', 'i', 'o', 'n', '\"', '\247', '\002', '\n', '\016', 'S', 'o', 'u', 'r', 'c', 'e', 'C',
  2921. 'o', 'd', 'e', 'I', 'n', 'f', 'o', '\022', 'D', '\n', '\010', 'l', 'o', 'c', 'a', 't', 'i', 'o', 'n', '\030', '\001', ' ', '\003', '(', '\013',
  2922. '2', '(', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'S', 'o', 'u', 'r', 'c', 'e',
  2923. 'C', 'o', 'd', 'e', 'I', 'n', 'f', 'o', '.', 'L', 'o', 'c', 'a', 't', 'i', 'o', 'n', 'R', '\010', 'l', 'o', 'c', 'a', 't', 'i',
  2924. 'o', 'n', '\032', '\316', '\001', '\n', '\010', 'L', 'o', 'c', 'a', 't', 'i', 'o', 'n', '\022', '\026', '\n', '\004', 'p', 'a', 't', 'h', '\030', '\001',
  2925. ' ', '\003', '(', '\005', 'B', '\002', '\020', '\001', 'R', '\004', 'p', 'a', 't', 'h', '\022', '\026', '\n', '\004', 's', 'p', 'a', 'n', '\030', '\002', ' ',
  2926. '\003', '(', '\005', 'B', '\002', '\020', '\001', 'R', '\004', 's', 'p', 'a', 'n', '\022', ')', '\n', '\020', 'l', 'e', 'a', 'd', 'i', 'n', 'g', '_',
  2927. 'c', 'o', 'm', 'm', 'e', 'n', 't', 's', '\030', '\003', ' ', '\001', '(', '\t', 'R', '\017', 'l', 'e', 'a', 'd', 'i', 'n', 'g', 'C', 'o',
  2928. 'm', 'm', 'e', 'n', 't', 's', '\022', '+', '\n', '\021', 't', 'r', 'a', 'i', 'l', 'i', 'n', 'g', '_', 'c', 'o', 'm', 'm', 'e', 'n',
  2929. 't', 's', '\030', '\004', ' ', '\001', '(', '\t', 'R', '\020', 't', 'r', 'a', 'i', 'l', 'i', 'n', 'g', 'C', 'o', 'm', 'm', 'e', 'n', 't',
  2930. 's', '\022', ':', '\n', '\031', 'l', 'e', 'a', 'd', 'i', 'n', 'g', '_', 'd', 'e', 't', 'a', 'c', 'h', 'e', 'd', '_', 'c', 'o', 'm',
  2931. 'm', 'e', 'n', 't', 's', '\030', '\006', ' ', '\003', '(', '\t', 'R', '\027', 'l', 'e', 'a', 'd', 'i', 'n', 'g', 'D', 'e', 't', 'a', 'c',
  2932. 'h', 'e', 'd', 'C', 'o', 'm', 'm', 'e', 'n', 't', 's', '\"', '\321', '\001', '\n', '\021', 'G', 'e', 'n', 'e', 'r', 'a', 't', 'e', 'd',
  2933. 'C', 'o', 'd', 'e', 'I', 'n', 'f', 'o', '\022', 'M', '\n', '\n', 'a', 'n', 'n', 'o', 't', 'a', 't', 'i', 'o', 'n', '\030', '\001', ' ',
  2934. '\003', '(', '\013', '2', '-', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'G', 'e', 'n',
  2935. 'e', 'r', 'a', 't', 'e', 'd', 'C', 'o', 'd', 'e', 'I', 'n', 'f', 'o', '.', 'A', 'n', 'n', 'o', 't', 'a', 't', 'i', 'o', 'n',
  2936. 'R', '\n', 'a', 'n', 'n', 'o', 't', 'a', 't', 'i', 'o', 'n', '\032', 'm', '\n', '\n', 'A', 'n', 'n', 'o', 't', 'a', 't', 'i', 'o',
  2937. 'n', '\022', '\026', '\n', '\004', 'p', 'a', 't', 'h', '\030', '\001', ' ', '\003', '(', '\005', 'B', '\002', '\020', '\001', 'R', '\004', 'p', 'a', 't', 'h',
  2938. '\022', '\037', '\n', '\013', 's', 'o', 'u', 'r', 'c', 'e', '_', 'f', 'i', 'l', 'e', '\030', '\002', ' ', '\001', '(', '\t', 'R', '\n', 's', 'o',
  2939. 'u', 'r', 'c', 'e', 'F', 'i', 'l', 'e', '\022', '\024', '\n', '\005', 'b', 'e', 'g', 'i', 'n', '\030', '\003', ' ', '\001', '(', '\005', 'R', '\005',
  2940. 'b', 'e', 'g', 'i', 'n', '\022', '\020', '\n', '\003', 'e', 'n', 'd', '\030', '\004', ' ', '\001', '(', '\005', 'R', '\003', 'e', 'n', 'd', 'B', '\217',
  2941. '\001', '\n', '\023', 'c', 'o', 'm', '.', 'g', 'o', 'o', 'g', 'l', 'e', '.', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', 'B', '\020', 'D',
  2942. 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', 'P', 'r', 'o', 't', 'o', 's', 'H', '\001', 'Z', '>', 'g', 'i', 't', 'h', 'u', 'b',
  2943. '.', 'c', 'o', 'm', '/', 'g', 'o', 'l', 'a', 'n', 'g', '/', 'p', 'r', 'o', 't', 'o', 'b', 'u', 'f', '/', 'p', 'r', 'o', 't',
  2944. 'o', 'c', '-', 'g', 'e', 'n', '-', 'g', 'o', '/', 'd', 'e', 's', 'c', 'r', 'i', 'p', 't', 'o', 'r', ';', 'd', 'e', 's', 'c',
  2945. 'r', 'i', 'p', 't', 'o', 'r', '\370', '\001', '\001', '\242', '\002', '\003', 'G', 'P', 'B', '\252', '\002', '\032', 'G', 'o', 'o', 'g', 'l', 'e', '.',
  2946. 'P', 'r', 'o', 't', 'o', 'b', 'u', 'f', '.', 'R', 'e', 'f', 'l', 'e', 'c', 't', 'i', 'o', 'n',
  2947. };
  2948. static upb_def_init *deps[1] = {
  2949. NULL
  2950. };
  2951. upb_def_init google_protobuf_descriptor_proto_upbdefinit = {
  2952. deps,
  2953. layouts,
  2954. "google/protobuf/descriptor.proto",
  2955. UPB_STRVIEW_INIT(descriptor, 7619)
  2956. };
  2957. #include <ctype.h>
  2958. #include <errno.h>
  2959. #include <stdlib.h>
  2960. #include <string.h>
  2961. typedef struct {
  2962. size_t len;
  2963. char str[1]; /* Null-terminated string data follows. */
  2964. } str_t;
  2965. static str_t *newstr(upb_alloc *alloc, const char *data, size_t len) {
  2966. str_t *ret = upb_malloc(alloc, sizeof(*ret) + len);
  2967. if (!ret) return NULL;
  2968. ret->len = len;
  2969. if (len) memcpy(ret->str, data, len);
  2970. ret->str[len] = '\0';
  2971. return ret;
  2972. }
  2973. struct upb_fielddef {
  2974. const upb_filedef *file;
  2975. const upb_msgdef *msgdef;
  2976. const char *full_name;
  2977. const char *json_name;
  2978. union {
  2979. int64_t sint;
  2980. uint64_t uint;
  2981. double dbl;
  2982. float flt;
  2983. bool boolean;
  2984. str_t *str;
  2985. } defaultval;
  2986. const upb_oneofdef *oneof;
  2987. union {
  2988. const upb_msgdef *msgdef;
  2989. const upb_enumdef *enumdef;
  2990. const google_protobuf_FieldDescriptorProto *unresolved;
  2991. } sub;
  2992. uint32_t number_;
  2993. uint16_t index_;
  2994. uint16_t layout_index;
  2995. uint32_t selector_base; /* Used to index into a upb::Handlers table. */
  2996. bool is_extension_;
  2997. bool lazy_;
  2998. bool packed_;
  2999. bool proto3_optional_;
  3000. upb_descriptortype_t type_;
  3001. upb_label_t label_;
  3002. };
  3003. struct upb_msgdef {
  3004. const upb_msglayout *layout;
  3005. const upb_filedef *file;
  3006. const char *full_name;
  3007. uint32_t selector_count;
  3008. uint32_t submsg_field_count;
  3009. /* Tables for looking up fields by number and name. */
  3010. upb_inttable itof;
  3011. upb_strtable ntof;
  3012. const upb_fielddef *fields;
  3013. const upb_oneofdef *oneofs;
  3014. int field_count;
  3015. int oneof_count;
  3016. int real_oneof_count;
  3017. /* Is this a map-entry message? */
  3018. bool map_entry;
  3019. upb_wellknowntype_t well_known_type;
  3020. /* TODO(haberman): proper extension ranges (there can be multiple). */
  3021. };
  3022. struct upb_enumdef {
  3023. const upb_filedef *file;
  3024. const char *full_name;
  3025. upb_strtable ntoi;
  3026. upb_inttable iton;
  3027. int32_t defaultval;
  3028. };
  3029. struct upb_oneofdef {
  3030. const upb_msgdef *parent;
  3031. const char *full_name;
  3032. uint32_t index;
  3033. upb_strtable ntof;
  3034. upb_inttable itof;
  3035. };
  3036. struct upb_filedef {
  3037. const char *name;
  3038. const char *package;
  3039. const char *phpprefix;
  3040. const char *phpnamespace;
  3041. upb_syntax_t syntax;
  3042. const upb_filedef **deps;
  3043. const upb_msgdef *msgs;
  3044. const upb_enumdef *enums;
  3045. const upb_fielddef *exts;
  3046. int dep_count;
  3047. int msg_count;
  3048. int enum_count;
  3049. int ext_count;
  3050. };
  3051. struct upb_symtab {
  3052. upb_arena *arena;
  3053. upb_strtable syms; /* full_name -> packed def ptr */
  3054. upb_strtable files; /* file_name -> upb_filedef* */
  3055. };
  3056. /* Inside a symtab we store tagged pointers to specific def types. */
  3057. typedef enum {
  3058. UPB_DEFTYPE_FIELD = 0,
  3059. /* Only inside symtab table. */
  3060. UPB_DEFTYPE_MSG = 1,
  3061. UPB_DEFTYPE_ENUM = 2,
  3062. /* Only inside message table. */
  3063. UPB_DEFTYPE_ONEOF = 1,
  3064. UPB_DEFTYPE_FIELD_JSONNAME = 2
  3065. } upb_deftype_t;
  3066. static const void *unpack_def(upb_value v, upb_deftype_t type) {
  3067. uintptr_t num = (uintptr_t)upb_value_getconstptr(v);
  3068. return (num & 3) == type ? (const void*)(num & ~3) : NULL;
  3069. }
  3070. static upb_value pack_def(const void *ptr, upb_deftype_t type) {
  3071. uintptr_t num = (uintptr_t)ptr | type;
  3072. return upb_value_constptr((const void*)num);
  3073. }
  3074. /* isalpha() etc. from <ctype.h> are locale-dependent, which we don't want. */
  3075. static bool upb_isbetween(char c, char low, char high) {
  3076. return c >= low && c <= high;
  3077. }
  3078. static bool upb_isletter(char c) {
  3079. return upb_isbetween(c, 'A', 'Z') || upb_isbetween(c, 'a', 'z') || c == '_';
  3080. }
  3081. static bool upb_isalphanum(char c) {
  3082. return upb_isletter(c) || upb_isbetween(c, '0', '9');
  3083. }
  3084. static bool upb_isident(upb_strview name, bool full, upb_status *s) {
  3085. const char *str = name.data;
  3086. size_t len = name.size;
  3087. bool start = true;
  3088. size_t i;
  3089. for (i = 0; i < len; i++) {
  3090. char c = str[i];
  3091. if (c == '.') {
  3092. if (start || !full) {
  3093. upb_status_seterrf(s, "invalid name: unexpected '.' (%s)", str);
  3094. return false;
  3095. }
  3096. start = true;
  3097. } else if (start) {
  3098. if (!upb_isletter(c)) {
  3099. upb_status_seterrf(
  3100. s, "invalid name: path components must start with a letter (%s)",
  3101. str);
  3102. return false;
  3103. }
  3104. start = false;
  3105. } else {
  3106. if (!upb_isalphanum(c)) {
  3107. upb_status_seterrf(s, "invalid name: non-alphanumeric character (%s)",
  3108. str);
  3109. return false;
  3110. }
  3111. }
  3112. }
  3113. return !start;
  3114. }
  3115. static const char *shortdefname(const char *fullname) {
  3116. const char *p;
  3117. if (fullname == NULL) {
  3118. return NULL;
  3119. } else if ((p = strrchr(fullname, '.')) == NULL) {
  3120. /* No '.' in the name, return the full string. */
  3121. return fullname;
  3122. } else {
  3123. /* Return one past the last '.'. */
  3124. return p + 1;
  3125. }
  3126. }
  3127. /* All submessage fields are lower than all other fields.
  3128. * Secondly, fields are increasing in order. */
  3129. uint32_t field_rank(const upb_fielddef *f) {
  3130. uint32_t ret = upb_fielddef_number(f);
  3131. const uint32_t high_bit = 1 << 30;
  3132. UPB_ASSERT(ret < high_bit);
  3133. if (!upb_fielddef_issubmsg(f))
  3134. ret |= high_bit;
  3135. return ret;
  3136. }
  3137. int cmp_fields(const void *p1, const void *p2) {
  3138. const upb_fielddef *f1 = *(upb_fielddef*const*)p1;
  3139. const upb_fielddef *f2 = *(upb_fielddef*const*)p2;
  3140. return field_rank(f1) - field_rank(f2);
  3141. }
  3142. /* A few implementation details of handlers. We put these here to avoid
  3143. * a def -> handlers dependency. */
  3144. #define UPB_STATIC_SELECTOR_COUNT 3 /* Warning: also in upb/handlers.h. */
  3145. static uint32_t upb_handlers_selectorbaseoffset(const upb_fielddef *f) {
  3146. return upb_fielddef_isseq(f) ? 2 : 0;
  3147. }
  3148. static uint32_t upb_handlers_selectorcount(const upb_fielddef *f) {
  3149. uint32_t ret = 1;
  3150. if (upb_fielddef_isseq(f)) ret += 2; /* STARTSEQ/ENDSEQ */
  3151. if (upb_fielddef_isstring(f)) ret += 2; /* [STRING]/STARTSTR/ENDSTR */
  3152. if (upb_fielddef_issubmsg(f)) {
  3153. /* ENDSUBMSG (STARTSUBMSG is at table beginning) */
  3154. ret += 0;
  3155. if (upb_fielddef_lazy(f)) {
  3156. /* STARTSTR/ENDSTR/STRING (for lazy) */
  3157. ret += 3;
  3158. }
  3159. }
  3160. return ret;
  3161. }
  3162. static void upb_status_setoom(upb_status *status) {
  3163. upb_status_seterrmsg(status, "out of memory");
  3164. }
  3165. static bool assign_msg_indices(upb_msgdef *m, upb_status *s) {
  3166. /* Sort fields. upb internally relies on UPB_TYPE_MESSAGE fields having the
  3167. * lowest indexes, but we do not publicly guarantee this. */
  3168. upb_msg_field_iter j;
  3169. int i;
  3170. uint32_t selector;
  3171. int n = upb_msgdef_numfields(m);
  3172. upb_fielddef **fields;
  3173. if (n == 0) {
  3174. m->selector_count = UPB_STATIC_SELECTOR_COUNT;
  3175. m->submsg_field_count = 0;
  3176. return true;
  3177. }
  3178. fields = upb_gmalloc(n * sizeof(*fields));
  3179. if (!fields) {
  3180. upb_status_setoom(s);
  3181. return false;
  3182. }
  3183. m->submsg_field_count = 0;
  3184. for(i = 0, upb_msg_field_begin(&j, m);
  3185. !upb_msg_field_done(&j);
  3186. upb_msg_field_next(&j), i++) {
  3187. upb_fielddef *f = upb_msg_iter_field(&j);
  3188. UPB_ASSERT(f->msgdef == m);
  3189. if (upb_fielddef_issubmsg(f)) {
  3190. m->submsg_field_count++;
  3191. }
  3192. fields[i] = f;
  3193. }
  3194. qsort(fields, n, sizeof(*fields), cmp_fields);
  3195. selector = UPB_STATIC_SELECTOR_COUNT + m->submsg_field_count;
  3196. for (i = 0; i < n; i++) {
  3197. upb_fielddef *f = fields[i];
  3198. f->index_ = i;
  3199. f->selector_base = selector + upb_handlers_selectorbaseoffset(f);
  3200. selector += upb_handlers_selectorcount(f);
  3201. }
  3202. m->selector_count = selector;
  3203. upb_gfree(fields);
  3204. return true;
  3205. }
  3206. static bool check_oneofs(upb_msgdef *m, upb_status *s) {
  3207. int i;
  3208. int first_synthetic = -1;
  3209. upb_oneofdef *mutable_oneofs = (upb_oneofdef*)m->oneofs;
  3210. for (i = 0; i < m->oneof_count; i++) {
  3211. mutable_oneofs[i].index = i;
  3212. if (upb_oneofdef_issynthetic(&mutable_oneofs[i])) {
  3213. if (first_synthetic == -1) {
  3214. first_synthetic = i;
  3215. }
  3216. } else {
  3217. if (first_synthetic != -1) {
  3218. upb_status_seterrf(
  3219. s, "Synthetic oneofs must be after all other oneofs: %s",
  3220. upb_oneofdef_name(&mutable_oneofs[i]));
  3221. return false;
  3222. }
  3223. }
  3224. }
  3225. if (first_synthetic == -1) {
  3226. m->real_oneof_count = m->oneof_count;
  3227. } else {
  3228. m->real_oneof_count = first_synthetic;
  3229. }
  3230. return true;
  3231. }
  3232. static void assign_msg_wellknowntype(upb_msgdef *m) {
  3233. const char *name = upb_msgdef_fullname(m);
  3234. if (name == NULL) {
  3235. m->well_known_type = UPB_WELLKNOWN_UNSPECIFIED;
  3236. return;
  3237. }
  3238. if (!strcmp(name, "google.protobuf.Any")) {
  3239. m->well_known_type = UPB_WELLKNOWN_ANY;
  3240. } else if (!strcmp(name, "google.protobuf.FieldMask")) {
  3241. m->well_known_type = UPB_WELLKNOWN_FIELDMASK;
  3242. } else if (!strcmp(name, "google.protobuf.Duration")) {
  3243. m->well_known_type = UPB_WELLKNOWN_DURATION;
  3244. } else if (!strcmp(name, "google.protobuf.Timestamp")) {
  3245. m->well_known_type = UPB_WELLKNOWN_TIMESTAMP;
  3246. } else if (!strcmp(name, "google.protobuf.DoubleValue")) {
  3247. m->well_known_type = UPB_WELLKNOWN_DOUBLEVALUE;
  3248. } else if (!strcmp(name, "google.protobuf.FloatValue")) {
  3249. m->well_known_type = UPB_WELLKNOWN_FLOATVALUE;
  3250. } else if (!strcmp(name, "google.protobuf.Int64Value")) {
  3251. m->well_known_type = UPB_WELLKNOWN_INT64VALUE;
  3252. } else if (!strcmp(name, "google.protobuf.UInt64Value")) {
  3253. m->well_known_type = UPB_WELLKNOWN_UINT64VALUE;
  3254. } else if (!strcmp(name, "google.protobuf.Int32Value")) {
  3255. m->well_known_type = UPB_WELLKNOWN_INT32VALUE;
  3256. } else if (!strcmp(name, "google.protobuf.UInt32Value")) {
  3257. m->well_known_type = UPB_WELLKNOWN_UINT32VALUE;
  3258. } else if (!strcmp(name, "google.protobuf.BoolValue")) {
  3259. m->well_known_type = UPB_WELLKNOWN_BOOLVALUE;
  3260. } else if (!strcmp(name, "google.protobuf.StringValue")) {
  3261. m->well_known_type = UPB_WELLKNOWN_STRINGVALUE;
  3262. } else if (!strcmp(name, "google.protobuf.BytesValue")) {
  3263. m->well_known_type = UPB_WELLKNOWN_BYTESVALUE;
  3264. } else if (!strcmp(name, "google.protobuf.Value")) {
  3265. m->well_known_type = UPB_WELLKNOWN_VALUE;
  3266. } else if (!strcmp(name, "google.protobuf.ListValue")) {
  3267. m->well_known_type = UPB_WELLKNOWN_LISTVALUE;
  3268. } else if (!strcmp(name, "google.protobuf.Struct")) {
  3269. m->well_known_type = UPB_WELLKNOWN_STRUCT;
  3270. } else {
  3271. m->well_known_type = UPB_WELLKNOWN_UNSPECIFIED;
  3272. }
  3273. }
  3274. /* upb_enumdef ****************************************************************/
  3275. const char *upb_enumdef_fullname(const upb_enumdef *e) {
  3276. return e->full_name;
  3277. }
  3278. const char *upb_enumdef_name(const upb_enumdef *e) {
  3279. return shortdefname(e->full_name);
  3280. }
  3281. const upb_filedef *upb_enumdef_file(const upb_enumdef *e) {
  3282. return e->file;
  3283. }
  3284. int32_t upb_enumdef_default(const upb_enumdef *e) {
  3285. UPB_ASSERT(upb_enumdef_iton(e, e->defaultval));
  3286. return e->defaultval;
  3287. }
  3288. int upb_enumdef_numvals(const upb_enumdef *e) {
  3289. return (int)upb_strtable_count(&e->ntoi);
  3290. }
  3291. void upb_enum_begin(upb_enum_iter *i, const upb_enumdef *e) {
  3292. /* We iterate over the ntoi table, to account for duplicate numbers. */
  3293. upb_strtable_begin(i, &e->ntoi);
  3294. }
  3295. void upb_enum_next(upb_enum_iter *iter) { upb_strtable_next(iter); }
  3296. bool upb_enum_done(upb_enum_iter *iter) { return upb_strtable_done(iter); }
  3297. bool upb_enumdef_ntoi(const upb_enumdef *def, const char *name,
  3298. size_t len, int32_t *num) {
  3299. upb_value v;
  3300. if (!upb_strtable_lookup2(&def->ntoi, name, len, &v)) {
  3301. return false;
  3302. }
  3303. if (num) *num = upb_value_getint32(v);
  3304. return true;
  3305. }
  3306. const char *upb_enumdef_iton(const upb_enumdef *def, int32_t num) {
  3307. upb_value v;
  3308. return upb_inttable_lookup32(&def->iton, num, &v) ?
  3309. upb_value_getcstr(v) : NULL;
  3310. }
  3311. const char *upb_enum_iter_name(upb_enum_iter *iter) {
  3312. return upb_strtable_iter_key(iter).data;
  3313. }
  3314. int32_t upb_enum_iter_number(upb_enum_iter *iter) {
  3315. return upb_value_getint32(upb_strtable_iter_value(iter));
  3316. }
  3317. /* upb_fielddef ***************************************************************/
  3318. const char *upb_fielddef_fullname(const upb_fielddef *f) {
  3319. return f->full_name;
  3320. }
  3321. upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f) {
  3322. switch (f->type_) {
  3323. case UPB_DESCRIPTOR_TYPE_DOUBLE:
  3324. return UPB_TYPE_DOUBLE;
  3325. case UPB_DESCRIPTOR_TYPE_FLOAT:
  3326. return UPB_TYPE_FLOAT;
  3327. case UPB_DESCRIPTOR_TYPE_INT64:
  3328. case UPB_DESCRIPTOR_TYPE_SINT64:
  3329. case UPB_DESCRIPTOR_TYPE_SFIXED64:
  3330. return UPB_TYPE_INT64;
  3331. case UPB_DESCRIPTOR_TYPE_INT32:
  3332. case UPB_DESCRIPTOR_TYPE_SFIXED32:
  3333. case UPB_DESCRIPTOR_TYPE_SINT32:
  3334. return UPB_TYPE_INT32;
  3335. case UPB_DESCRIPTOR_TYPE_UINT64:
  3336. case UPB_DESCRIPTOR_TYPE_FIXED64:
  3337. return UPB_TYPE_UINT64;
  3338. case UPB_DESCRIPTOR_TYPE_UINT32:
  3339. case UPB_DESCRIPTOR_TYPE_FIXED32:
  3340. return UPB_TYPE_UINT32;
  3341. case UPB_DESCRIPTOR_TYPE_ENUM:
  3342. return UPB_TYPE_ENUM;
  3343. case UPB_DESCRIPTOR_TYPE_BOOL:
  3344. return UPB_TYPE_BOOL;
  3345. case UPB_DESCRIPTOR_TYPE_STRING:
  3346. return UPB_TYPE_STRING;
  3347. case UPB_DESCRIPTOR_TYPE_BYTES:
  3348. return UPB_TYPE_BYTES;
  3349. case UPB_DESCRIPTOR_TYPE_GROUP:
  3350. case UPB_DESCRIPTOR_TYPE_MESSAGE:
  3351. return UPB_TYPE_MESSAGE;
  3352. }
  3353. UPB_UNREACHABLE();
  3354. }
  3355. upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f) {
  3356. return f->type_;
  3357. }
  3358. uint32_t upb_fielddef_index(const upb_fielddef *f) {
  3359. return f->index_;
  3360. }
  3361. upb_label_t upb_fielddef_label(const upb_fielddef *f) {
  3362. return f->label_;
  3363. }
  3364. uint32_t upb_fielddef_number(const upb_fielddef *f) {
  3365. return f->number_;
  3366. }
  3367. bool upb_fielddef_isextension(const upb_fielddef *f) {
  3368. return f->is_extension_;
  3369. }
  3370. bool upb_fielddef_lazy(const upb_fielddef *f) {
  3371. return f->lazy_;
  3372. }
  3373. bool upb_fielddef_packed(const upb_fielddef *f) {
  3374. return f->packed_;
  3375. }
  3376. const char *upb_fielddef_name(const upb_fielddef *f) {
  3377. return shortdefname(f->full_name);
  3378. }
  3379. const char *upb_fielddef_jsonname(const upb_fielddef *f) {
  3380. return f->json_name;
  3381. }
  3382. uint32_t upb_fielddef_selectorbase(const upb_fielddef *f) {
  3383. return f->selector_base;
  3384. }
  3385. const upb_filedef *upb_fielddef_file(const upb_fielddef *f) {
  3386. return f->file;
  3387. }
  3388. const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f) {
  3389. return f->msgdef;
  3390. }
  3391. const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f) {
  3392. return f->oneof;
  3393. }
  3394. const upb_oneofdef *upb_fielddef_realcontainingoneof(const upb_fielddef *f) {
  3395. if (!f->oneof || upb_oneofdef_issynthetic(f->oneof)) return NULL;
  3396. return f->oneof;
  3397. }
  3398. static void chkdefaulttype(const upb_fielddef *f, int ctype) {
  3399. UPB_UNUSED(f);
  3400. UPB_UNUSED(ctype);
  3401. }
  3402. int64_t upb_fielddef_defaultint64(const upb_fielddef *f) {
  3403. chkdefaulttype(f, UPB_TYPE_INT64);
  3404. return f->defaultval.sint;
  3405. }
  3406. int32_t upb_fielddef_defaultint32(const upb_fielddef *f) {
  3407. chkdefaulttype(f, UPB_TYPE_INT32);
  3408. return (int32_t)f->defaultval.sint;
  3409. }
  3410. uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f) {
  3411. chkdefaulttype(f, UPB_TYPE_UINT64);
  3412. return f->defaultval.uint;
  3413. }
  3414. uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f) {
  3415. chkdefaulttype(f, UPB_TYPE_UINT32);
  3416. return (uint32_t)f->defaultval.uint;
  3417. }
  3418. bool upb_fielddef_defaultbool(const upb_fielddef *f) {
  3419. chkdefaulttype(f, UPB_TYPE_BOOL);
  3420. return f->defaultval.boolean;
  3421. }
  3422. float upb_fielddef_defaultfloat(const upb_fielddef *f) {
  3423. chkdefaulttype(f, UPB_TYPE_FLOAT);
  3424. return f->defaultval.flt;
  3425. }
  3426. double upb_fielddef_defaultdouble(const upb_fielddef *f) {
  3427. chkdefaulttype(f, UPB_TYPE_DOUBLE);
  3428. return f->defaultval.dbl;
  3429. }
  3430. const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len) {
  3431. str_t *str = f->defaultval.str;
  3432. UPB_ASSERT(upb_fielddef_type(f) == UPB_TYPE_STRING ||
  3433. upb_fielddef_type(f) == UPB_TYPE_BYTES ||
  3434. upb_fielddef_type(f) == UPB_TYPE_ENUM);
  3435. if (str) {
  3436. if (len) *len = str->len;
  3437. return str->str;
  3438. } else {
  3439. if (len) *len = 0;
  3440. return NULL;
  3441. }
  3442. }
  3443. const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f) {
  3444. return upb_fielddef_type(f) == UPB_TYPE_MESSAGE ? f->sub.msgdef : NULL;
  3445. }
  3446. const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f) {
  3447. return upb_fielddef_type(f) == UPB_TYPE_ENUM ? f->sub.enumdef : NULL;
  3448. }
  3449. const upb_msglayout_field *upb_fielddef_layout(const upb_fielddef *f) {
  3450. return &f->msgdef->layout->fields[f->layout_index];
  3451. }
  3452. bool upb_fielddef_issubmsg(const upb_fielddef *f) {
  3453. return upb_fielddef_type(f) == UPB_TYPE_MESSAGE;
  3454. }
  3455. bool upb_fielddef_isstring(const upb_fielddef *f) {
  3456. return upb_fielddef_type(f) == UPB_TYPE_STRING ||
  3457. upb_fielddef_type(f) == UPB_TYPE_BYTES;
  3458. }
  3459. bool upb_fielddef_isseq(const upb_fielddef *f) {
  3460. return upb_fielddef_label(f) == UPB_LABEL_REPEATED;
  3461. }
  3462. bool upb_fielddef_isprimitive(const upb_fielddef *f) {
  3463. return !upb_fielddef_isstring(f) && !upb_fielddef_issubmsg(f);
  3464. }
  3465. bool upb_fielddef_ismap(const upb_fielddef *f) {
  3466. return upb_fielddef_isseq(f) && upb_fielddef_issubmsg(f) &&
  3467. upb_msgdef_mapentry(upb_fielddef_msgsubdef(f));
  3468. }
  3469. bool upb_fielddef_hassubdef(const upb_fielddef *f) {
  3470. return upb_fielddef_issubmsg(f) || upb_fielddef_type(f) == UPB_TYPE_ENUM;
  3471. }
  3472. bool upb_fielddef_haspresence(const upb_fielddef *f) {
  3473. if (upb_fielddef_isseq(f)) return false;
  3474. return upb_fielddef_issubmsg(f) || upb_fielddef_containingoneof(f) ||
  3475. f->file->syntax == UPB_SYNTAX_PROTO2;
  3476. }
  3477. static bool between(int32_t x, int32_t low, int32_t high) {
  3478. return x >= low && x <= high;
  3479. }
  3480. bool upb_fielddef_checklabel(int32_t label) { return between(label, 1, 3); }
  3481. bool upb_fielddef_checktype(int32_t type) { return between(type, 1, 11); }
  3482. bool upb_fielddef_checkintfmt(int32_t fmt) { return between(fmt, 1, 3); }
  3483. bool upb_fielddef_checkdescriptortype(int32_t type) {
  3484. return between(type, 1, 18);
  3485. }
  3486. /* upb_msgdef *****************************************************************/
  3487. const char *upb_msgdef_fullname(const upb_msgdef *m) {
  3488. return m->full_name;
  3489. }
  3490. const upb_filedef *upb_msgdef_file(const upb_msgdef *m) {
  3491. return m->file;
  3492. }
  3493. const char *upb_msgdef_name(const upb_msgdef *m) {
  3494. return shortdefname(m->full_name);
  3495. }
  3496. upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m) {
  3497. return m->file->syntax;
  3498. }
  3499. size_t upb_msgdef_selectorcount(const upb_msgdef *m) {
  3500. return m->selector_count;
  3501. }
  3502. uint32_t upb_msgdef_submsgfieldcount(const upb_msgdef *m) {
  3503. return m->submsg_field_count;
  3504. }
  3505. const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i) {
  3506. upb_value val;
  3507. return upb_inttable_lookup32(&m->itof, i, &val) ?
  3508. upb_value_getconstptr(val) : NULL;
  3509. }
  3510. const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name,
  3511. size_t len) {
  3512. upb_value val;
  3513. if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
  3514. return NULL;
  3515. }
  3516. return unpack_def(val, UPB_DEFTYPE_FIELD);
  3517. }
  3518. const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name,
  3519. size_t len) {
  3520. upb_value val;
  3521. if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
  3522. return NULL;
  3523. }
  3524. return unpack_def(val, UPB_DEFTYPE_ONEOF);
  3525. }
  3526. bool upb_msgdef_lookupname(const upb_msgdef *m, const char *name, size_t len,
  3527. const upb_fielddef **f, const upb_oneofdef **o) {
  3528. upb_value val;
  3529. if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
  3530. return false;
  3531. }
  3532. *o = unpack_def(val, UPB_DEFTYPE_ONEOF);
  3533. *f = unpack_def(val, UPB_DEFTYPE_FIELD);
  3534. return *o || *f; /* False if this was a JSON name. */
  3535. }
  3536. const upb_fielddef *upb_msgdef_lookupjsonname(const upb_msgdef *m,
  3537. const char *name, size_t len) {
  3538. upb_value val;
  3539. const upb_fielddef* f;
  3540. if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
  3541. return NULL;
  3542. }
  3543. f = unpack_def(val, UPB_DEFTYPE_FIELD);
  3544. if (!f) f = unpack_def(val, UPB_DEFTYPE_FIELD_JSONNAME);
  3545. return f;
  3546. }
  3547. int upb_msgdef_numfields(const upb_msgdef *m) {
  3548. return m->field_count;
  3549. }
  3550. int upb_msgdef_numoneofs(const upb_msgdef *m) {
  3551. return m->oneof_count;
  3552. }
  3553. int upb_msgdef_numrealoneofs(const upb_msgdef *m) {
  3554. return m->real_oneof_count;
  3555. }
  3556. const upb_msglayout *upb_msgdef_layout(const upb_msgdef *m) {
  3557. return m->layout;
  3558. }
  3559. const upb_fielddef *_upb_msgdef_field(const upb_msgdef *m, int i) {
  3560. if (i >= m->field_count) return NULL;
  3561. return &m->fields[i];
  3562. }
  3563. bool upb_msgdef_mapentry(const upb_msgdef *m) {
  3564. return m->map_entry;
  3565. }
  3566. upb_wellknowntype_t upb_msgdef_wellknowntype(const upb_msgdef *m) {
  3567. return m->well_known_type;
  3568. }
  3569. bool upb_msgdef_isnumberwrapper(const upb_msgdef *m) {
  3570. upb_wellknowntype_t type = upb_msgdef_wellknowntype(m);
  3571. return type >= UPB_WELLKNOWN_DOUBLEVALUE &&
  3572. type <= UPB_WELLKNOWN_UINT32VALUE;
  3573. }
  3574. bool upb_msgdef_iswrapper(const upb_msgdef *m) {
  3575. upb_wellknowntype_t type = upb_msgdef_wellknowntype(m);
  3576. return type >= UPB_WELLKNOWN_DOUBLEVALUE &&
  3577. type <= UPB_WELLKNOWN_BOOLVALUE;
  3578. }
  3579. void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m) {
  3580. upb_inttable_begin(iter, &m->itof);
  3581. }
  3582. void upb_msg_field_next(upb_msg_field_iter *iter) { upb_inttable_next(iter); }
  3583. bool upb_msg_field_done(const upb_msg_field_iter *iter) {
  3584. return upb_inttable_done(iter);
  3585. }
  3586. upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter) {
  3587. return (upb_fielddef *)upb_value_getconstptr(upb_inttable_iter_value(iter));
  3588. }
  3589. void upb_msg_field_iter_setdone(upb_msg_field_iter *iter) {
  3590. upb_inttable_iter_setdone(iter);
  3591. }
  3592. bool upb_msg_field_iter_isequal(const upb_msg_field_iter * iter1,
  3593. const upb_msg_field_iter * iter2) {
  3594. return upb_inttable_iter_isequal(iter1, iter2);
  3595. }
  3596. void upb_msg_oneof_begin(upb_msg_oneof_iter *iter, const upb_msgdef *m) {
  3597. upb_strtable_begin(iter, &m->ntof);
  3598. /* We need to skip past any initial fields. */
  3599. while (!upb_strtable_done(iter) &&
  3600. !unpack_def(upb_strtable_iter_value(iter), UPB_DEFTYPE_ONEOF)) {
  3601. upb_strtable_next(iter);
  3602. }
  3603. }
  3604. void upb_msg_oneof_next(upb_msg_oneof_iter *iter) {
  3605. /* We need to skip past fields to return only oneofs. */
  3606. do {
  3607. upb_strtable_next(iter);
  3608. } while (!upb_strtable_done(iter) &&
  3609. !unpack_def(upb_strtable_iter_value(iter), UPB_DEFTYPE_ONEOF));
  3610. }
  3611. bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter) {
  3612. return upb_strtable_done(iter);
  3613. }
  3614. const upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter) {
  3615. return unpack_def(upb_strtable_iter_value(iter), UPB_DEFTYPE_ONEOF);
  3616. }
  3617. void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter) {
  3618. upb_strtable_iter_setdone(iter);
  3619. }
  3620. bool upb_msg_oneof_iter_isequal(const upb_msg_oneof_iter *iter1,
  3621. const upb_msg_oneof_iter *iter2) {
  3622. return upb_strtable_iter_isequal(iter1, iter2);
  3623. }
  3624. /* upb_oneofdef ***************************************************************/
  3625. const char *upb_oneofdef_name(const upb_oneofdef *o) {
  3626. return shortdefname(o->full_name);
  3627. }
  3628. const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o) {
  3629. return o->parent;
  3630. }
  3631. int upb_oneofdef_numfields(const upb_oneofdef *o) {
  3632. return (int)upb_strtable_count(&o->ntof);
  3633. }
  3634. uint32_t upb_oneofdef_index(const upb_oneofdef *o) {
  3635. return o->index;
  3636. }
  3637. bool upb_oneofdef_issynthetic(const upb_oneofdef *o) {
  3638. upb_inttable_iter iter;
  3639. const upb_fielddef *f;
  3640. upb_inttable_begin(&iter, &o->itof);
  3641. if (upb_oneofdef_numfields(o) != 1) return false;
  3642. f = upb_value_getptr(upb_inttable_iter_value(&iter));
  3643. UPB_ASSERT(f);
  3644. return f->proto3_optional_;
  3645. }
  3646. const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o,
  3647. const char *name, size_t length) {
  3648. upb_value val;
  3649. return upb_strtable_lookup2(&o->ntof, name, length, &val) ?
  3650. upb_value_getptr(val) : NULL;
  3651. }
  3652. const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num) {
  3653. upb_value val;
  3654. return upb_inttable_lookup32(&o->itof, num, &val) ?
  3655. upb_value_getptr(val) : NULL;
  3656. }
  3657. void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o) {
  3658. upb_inttable_begin(iter, &o->itof);
  3659. }
  3660. void upb_oneof_next(upb_oneof_iter *iter) {
  3661. upb_inttable_next(iter);
  3662. }
  3663. bool upb_oneof_done(upb_oneof_iter *iter) {
  3664. return upb_inttable_done(iter);
  3665. }
  3666. upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter) {
  3667. return (upb_fielddef *)upb_value_getconstptr(upb_inttable_iter_value(iter));
  3668. }
  3669. void upb_oneof_iter_setdone(upb_oneof_iter *iter) {
  3670. upb_inttable_iter_setdone(iter);
  3671. }
  3672. /* Dynamic Layout Generation. *************************************************/
  3673. static size_t div_round_up(size_t n, size_t d) {
  3674. return (n + d - 1) / d;
  3675. }
  3676. static size_t upb_msgval_sizeof(upb_fieldtype_t type) {
  3677. switch (type) {
  3678. case UPB_TYPE_DOUBLE:
  3679. case UPB_TYPE_INT64:
  3680. case UPB_TYPE_UINT64:
  3681. return 8;
  3682. case UPB_TYPE_ENUM:
  3683. case UPB_TYPE_INT32:
  3684. case UPB_TYPE_UINT32:
  3685. case UPB_TYPE_FLOAT:
  3686. return 4;
  3687. case UPB_TYPE_BOOL:
  3688. return 1;
  3689. case UPB_TYPE_MESSAGE:
  3690. return sizeof(void*);
  3691. case UPB_TYPE_BYTES:
  3692. case UPB_TYPE_STRING:
  3693. return sizeof(upb_strview);
  3694. }
  3695. UPB_UNREACHABLE();
  3696. }
  3697. static uint8_t upb_msg_fielddefsize(const upb_fielddef *f) {
  3698. if (upb_msgdef_mapentry(upb_fielddef_containingtype(f))) {
  3699. upb_map_entry ent;
  3700. UPB_ASSERT(sizeof(ent.k) == sizeof(ent.v));
  3701. return sizeof(ent.k);
  3702. } else if (upb_fielddef_isseq(f)) {
  3703. return sizeof(void*);
  3704. } else {
  3705. return upb_msgval_sizeof(upb_fielddef_type(f));
  3706. }
  3707. }
  3708. static uint32_t upb_msglayout_place(upb_msglayout *l, size_t size) {
  3709. uint32_t ret;
  3710. l->size = UPB_ALIGN_UP(l->size, size);
  3711. ret = l->size;
  3712. l->size += size;
  3713. return ret;
  3714. }
  3715. static int field_number_cmp(const void *p1, const void *p2) {
  3716. const upb_msglayout_field *f1 = p1;
  3717. const upb_msglayout_field *f2 = p2;
  3718. return f1->number - f2->number;
  3719. }
  3720. static void assign_layout_indices(const upb_msgdef *m, upb_msglayout_field *fields) {
  3721. int i;
  3722. int n = upb_msgdef_numfields(m);
  3723. for (i = 0; i < n; i++) {
  3724. upb_fielddef *f = (upb_fielddef*)upb_msgdef_itof(m, fields[i].number);
  3725. UPB_ASSERT(f);
  3726. f->layout_index = i;
  3727. }
  3728. }
  3729. /* This function is the dynamic equivalent of message_layout.{cc,h} in upbc.
  3730. * It computes a dynamic layout for all of the fields in |m|. */
  3731. static bool make_layout(const upb_symtab *symtab, const upb_msgdef *m) {
  3732. upb_msglayout *l = (upb_msglayout*)m->layout;
  3733. upb_msg_field_iter it;
  3734. upb_msg_oneof_iter oit;
  3735. size_t hasbit;
  3736. size_t submsg_count = m->submsg_field_count;
  3737. const upb_msglayout **submsgs;
  3738. upb_msglayout_field *fields;
  3739. upb_alloc *alloc = upb_arena_alloc(symtab->arena);
  3740. memset(l, 0, sizeof(*l));
  3741. fields = upb_malloc(alloc, upb_msgdef_numfields(m) * sizeof(*fields));
  3742. submsgs = upb_malloc(alloc, submsg_count * sizeof(*submsgs));
  3743. if ((!fields && upb_msgdef_numfields(m)) ||
  3744. (!submsgs && submsg_count)) {
  3745. /* OOM. */
  3746. return false;
  3747. }
  3748. l->field_count = upb_msgdef_numfields(m);
  3749. l->fields = fields;
  3750. l->submsgs = submsgs;
  3751. if (upb_msgdef_mapentry(m)) {
  3752. /* TODO(haberman): refactor this method so this special case is more
  3753. * elegant. */
  3754. const upb_fielddef *key = upb_msgdef_itof(m, 1);
  3755. const upb_fielddef *val = upb_msgdef_itof(m, 2);
  3756. fields[0].number = 1;
  3757. fields[1].number = 2;
  3758. fields[0].label = UPB_LABEL_OPTIONAL;
  3759. fields[1].label = UPB_LABEL_OPTIONAL;
  3760. fields[0].presence = 0;
  3761. fields[1].presence = 0;
  3762. fields[0].descriptortype = upb_fielddef_descriptortype(key);
  3763. fields[1].descriptortype = upb_fielddef_descriptortype(val);
  3764. fields[0].offset = 0;
  3765. fields[1].offset = sizeof(upb_strview);
  3766. fields[1].submsg_index = 0;
  3767. if (upb_fielddef_type(val) == UPB_TYPE_MESSAGE) {
  3768. submsgs[0] = upb_fielddef_msgsubdef(val)->layout;
  3769. }
  3770. l->field_count = 2;
  3771. l->size = 2 * sizeof(upb_strview);
  3772. l->size = UPB_ALIGN_UP(l->size, 8);
  3773. return true;
  3774. }
  3775. /* Allocate data offsets in three stages:
  3776. *
  3777. * 1. hasbits.
  3778. * 2. regular fields.
  3779. * 3. oneof fields.
  3780. *
  3781. * OPT: There is a lot of room for optimization here to minimize the size.
  3782. */
  3783. /* Allocate hasbits and set basic field attributes. */
  3784. submsg_count = 0;
  3785. for (upb_msg_field_begin(&it, m), hasbit = 0;
  3786. !upb_msg_field_done(&it);
  3787. upb_msg_field_next(&it)) {
  3788. upb_fielddef* f = upb_msg_iter_field(&it);
  3789. upb_msglayout_field *field = &fields[upb_fielddef_index(f)];
  3790. field->number = upb_fielddef_number(f);
  3791. field->descriptortype = upb_fielddef_descriptortype(f);
  3792. field->label = upb_fielddef_label(f);
  3793. if (field->descriptortype == UPB_DTYPE_STRING &&
  3794. f->file->syntax == UPB_SYNTAX_PROTO2) {
  3795. /* See TableDescriptorType() in upbc/generator.cc for details and
  3796. * rationale. */
  3797. field->descriptortype = UPB_DTYPE_BYTES;
  3798. }
  3799. if (upb_fielddef_ismap(f)) {
  3800. field->label = _UPB_LABEL_MAP;
  3801. } else if (upb_fielddef_packed(f)) {
  3802. field->label = _UPB_LABEL_PACKED;
  3803. }
  3804. if (upb_fielddef_issubmsg(f)) {
  3805. const upb_msgdef *subm = upb_fielddef_msgsubdef(f);
  3806. field->submsg_index = submsg_count++;
  3807. submsgs[field->submsg_index] = subm->layout;
  3808. }
  3809. if (upb_fielddef_haspresence(f) && !upb_fielddef_realcontainingoneof(f)) {
  3810. /* We don't use hasbit 0, so that 0 can indicate "no presence" in the
  3811. * table. This wastes one hasbit, but we don't worry about it for now. */
  3812. field->presence = ++hasbit;
  3813. } else {
  3814. field->presence = 0;
  3815. }
  3816. }
  3817. /* Account for space used by hasbits. */
  3818. l->size = div_round_up(hasbit, 8);
  3819. /* Allocate non-oneof fields. */
  3820. for (upb_msg_field_begin(&it, m); !upb_msg_field_done(&it);
  3821. upb_msg_field_next(&it)) {
  3822. const upb_fielddef* f = upb_msg_iter_field(&it);
  3823. size_t field_size = upb_msg_fielddefsize(f);
  3824. size_t index = upb_fielddef_index(f);
  3825. if (upb_fielddef_realcontainingoneof(f)) {
  3826. /* Oneofs are handled separately below. */
  3827. continue;
  3828. }
  3829. fields[index].offset = upb_msglayout_place(l, field_size);
  3830. }
  3831. /* Allocate oneof fields. Each oneof field consists of a uint32 for the case
  3832. * and space for the actual data. */
  3833. for (upb_msg_oneof_begin(&oit, m); !upb_msg_oneof_done(&oit);
  3834. upb_msg_oneof_next(&oit)) {
  3835. const upb_oneofdef* o = upb_msg_iter_oneof(&oit);
  3836. upb_oneof_iter fit;
  3837. size_t case_size = sizeof(uint32_t); /* Could potentially optimize this. */
  3838. size_t field_size = 0;
  3839. uint32_t case_offset;
  3840. uint32_t data_offset;
  3841. if (upb_oneofdef_issynthetic(o)) continue;
  3842. /* Calculate field size: the max of all field sizes. */
  3843. for (upb_oneof_begin(&fit, o);
  3844. !upb_oneof_done(&fit);
  3845. upb_oneof_next(&fit)) {
  3846. const upb_fielddef* f = upb_oneof_iter_field(&fit);
  3847. field_size = UPB_MAX(field_size, upb_msg_fielddefsize(f));
  3848. }
  3849. /* Align and allocate case offset. */
  3850. case_offset = upb_msglayout_place(l, case_size);
  3851. data_offset = upb_msglayout_place(l, field_size);
  3852. for (upb_oneof_begin(&fit, o);
  3853. !upb_oneof_done(&fit);
  3854. upb_oneof_next(&fit)) {
  3855. const upb_fielddef* f = upb_oneof_iter_field(&fit);
  3856. fields[upb_fielddef_index(f)].offset = data_offset;
  3857. fields[upb_fielddef_index(f)].presence = ~case_offset;
  3858. }
  3859. }
  3860. /* Size of the entire structure should be a multiple of its greatest
  3861. * alignment. TODO: track overall alignment for real? */
  3862. l->size = UPB_ALIGN_UP(l->size, 8);
  3863. /* Sort fields by number. */
  3864. qsort(fields, upb_msgdef_numfields(m), sizeof(*fields), field_number_cmp);
  3865. assign_layout_indices(m, fields);
  3866. return true;
  3867. }
  3868. /* Code to build defs from descriptor protos. *********************************/
  3869. /* There is a question of how much validation to do here. It will be difficult
  3870. * to perfectly match the amount of validation performed by proto2. But since
  3871. * this code is used to directly build defs from Ruby (for example) we do need
  3872. * to validate important constraints like uniqueness of names and numbers. */
  3873. #define CHK(x) if (!(x)) { return false; }
  3874. #define CHK_OOM(x) if (!(x)) { upb_status_setoom(ctx->status); return false; }
  3875. typedef struct {
  3876. const upb_symtab *symtab;
  3877. upb_filedef *file; /* File we are building. */
  3878. upb_alloc *alloc; /* Allocate defs here. */
  3879. upb_alloc *tmp; /* Alloc for addtab and any other tmp data. */
  3880. upb_strtable *addtab; /* full_name -> packed def ptr for new defs */
  3881. const upb_msglayout **layouts; /* NULL if we should build layouts. */
  3882. upb_status *status; /* Record errors here. */
  3883. } symtab_addctx;
  3884. static char* strviewdup(const symtab_addctx *ctx, upb_strview view) {
  3885. return upb_strdup2(view.data, view.size, ctx->alloc);
  3886. }
  3887. static bool streql2(const char *a, size_t n, const char *b) {
  3888. return n == strlen(b) && memcmp(a, b, n) == 0;
  3889. }
  3890. static bool streql_view(upb_strview view, const char *b) {
  3891. return streql2(view.data, view.size, b);
  3892. }
  3893. static const char *makefullname(const symtab_addctx *ctx, const char *prefix,
  3894. upb_strview name) {
  3895. if (prefix) {
  3896. /* ret = prefix + '.' + name; */
  3897. size_t n = strlen(prefix);
  3898. char *ret = upb_malloc(ctx->alloc, n + name.size + 2);
  3899. CHK_OOM(ret);
  3900. strcpy(ret, prefix);
  3901. ret[n] = '.';
  3902. memcpy(&ret[n + 1], name.data, name.size);
  3903. ret[n + 1 + name.size] = '\0';
  3904. return ret;
  3905. } else {
  3906. return strviewdup(ctx, name);
  3907. }
  3908. }
  3909. size_t getjsonname(const char *name, char *buf, size_t len) {
  3910. size_t src, dst = 0;
  3911. bool ucase_next = false;
  3912. #define WRITE(byte) \
  3913. ++dst; \
  3914. if (dst < len) buf[dst - 1] = byte; \
  3915. else if (dst == len) buf[dst - 1] = '\0'
  3916. if (!name) {
  3917. WRITE('\0');
  3918. return 0;
  3919. }
  3920. /* Implement the transformation as described in the spec:
  3921. * 1. upper case all letters after an underscore.
  3922. * 2. remove all underscores.
  3923. */
  3924. for (src = 0; name[src]; src++) {
  3925. if (name[src] == '_') {
  3926. ucase_next = true;
  3927. continue;
  3928. }
  3929. if (ucase_next) {
  3930. WRITE(toupper(name[src]));
  3931. ucase_next = false;
  3932. } else {
  3933. WRITE(name[src]);
  3934. }
  3935. }
  3936. WRITE('\0');
  3937. return dst;
  3938. #undef WRITE
  3939. }
  3940. static char* makejsonname(const char* name, upb_alloc *alloc) {
  3941. size_t size = getjsonname(name, NULL, 0);
  3942. char* json_name = upb_malloc(alloc, size);
  3943. getjsonname(name, json_name, size);
  3944. return json_name;
  3945. }
  3946. static bool symtab_add(const symtab_addctx *ctx, const char *name,
  3947. upb_value v) {
  3948. upb_value tmp;
  3949. if (upb_strtable_lookup(ctx->addtab, name, &tmp) ||
  3950. upb_strtable_lookup(&ctx->symtab->syms, name, &tmp)) {
  3951. upb_status_seterrf(ctx->status, "duplicate symbol '%s'", name);
  3952. return false;
  3953. }
  3954. CHK_OOM(upb_strtable_insert3(ctx->addtab, name, strlen(name), v, ctx->tmp));
  3955. return true;
  3956. }
  3957. /* Given a symbol and the base symbol inside which it is defined, find the
  3958. * symbol's definition in t. */
  3959. static bool resolvename(const upb_strtable *t, const upb_fielddef *f,
  3960. const char *base, upb_strview sym,
  3961. upb_deftype_t type, upb_status *status,
  3962. const void **def) {
  3963. if(sym.size == 0) return false;
  3964. if(sym.data[0] == '.') {
  3965. /* Symbols starting with '.' are absolute, so we do a single lookup.
  3966. * Slice to omit the leading '.' */
  3967. upb_value v;
  3968. if (!upb_strtable_lookup2(t, sym.data + 1, sym.size - 1, &v)) {
  3969. return false;
  3970. }
  3971. *def = unpack_def(v, type);
  3972. if (!*def) {
  3973. upb_status_seterrf(status,
  3974. "type mismatch when resolving field %s, name %s",
  3975. f->full_name, sym.data);
  3976. return false;
  3977. }
  3978. return true;
  3979. } else {
  3980. /* Remove components from base until we find an entry or run out.
  3981. * TODO: This branch is totally broken, but currently not used. */
  3982. (void)base;
  3983. UPB_ASSERT(false);
  3984. return false;
  3985. }
  3986. }
  3987. const void *symtab_resolve(const symtab_addctx *ctx, const upb_fielddef *f,
  3988. const char *base, upb_strview sym,
  3989. upb_deftype_t type) {
  3990. const void *ret;
  3991. if (!resolvename(ctx->addtab, f, base, sym, type, ctx->status, &ret) &&
  3992. !resolvename(&ctx->symtab->syms, f, base, sym, type, ctx->status, &ret)) {
  3993. if (upb_ok(ctx->status)) {
  3994. upb_status_seterrf(ctx->status, "couldn't resolve name '%s'", sym.data);
  3995. }
  3996. return false;
  3997. }
  3998. return ret;
  3999. }
  4000. static bool create_oneofdef(
  4001. const symtab_addctx *ctx, upb_msgdef *m,
  4002. const google_protobuf_OneofDescriptorProto *oneof_proto) {
  4003. upb_oneofdef *o;
  4004. upb_strview name = google_protobuf_OneofDescriptorProto_name(oneof_proto);
  4005. upb_value v;
  4006. o = (upb_oneofdef*)&m->oneofs[m->oneof_count++];
  4007. o->parent = m;
  4008. o->full_name = makefullname(ctx, m->full_name, name);
  4009. v = pack_def(o, UPB_DEFTYPE_ONEOF);
  4010. CHK_OOM(symtab_add(ctx, o->full_name, v));
  4011. CHK_OOM(upb_strtable_insert3(&m->ntof, name.data, name.size, v, ctx->alloc));
  4012. CHK_OOM(upb_inttable_init2(&o->itof, UPB_CTYPE_CONSTPTR, ctx->alloc));
  4013. CHK_OOM(upb_strtable_init2(&o->ntof, UPB_CTYPE_CONSTPTR, ctx->alloc));
  4014. return true;
  4015. }
  4016. static bool parse_default(const symtab_addctx *ctx, const char *str, size_t len,
  4017. upb_fielddef *f) {
  4018. char *end;
  4019. char nullz[64];
  4020. errno = 0;
  4021. switch (upb_fielddef_type(f)) {
  4022. case UPB_TYPE_INT32:
  4023. case UPB_TYPE_INT64:
  4024. case UPB_TYPE_UINT32:
  4025. case UPB_TYPE_UINT64:
  4026. case UPB_TYPE_DOUBLE:
  4027. case UPB_TYPE_FLOAT:
  4028. /* Standard C number parsing functions expect null-terminated strings. */
  4029. if (len >= sizeof(nullz) - 1) {
  4030. return false;
  4031. }
  4032. memcpy(nullz, str, len);
  4033. nullz[len] = '\0';
  4034. str = nullz;
  4035. break;
  4036. default:
  4037. break;
  4038. }
  4039. switch (upb_fielddef_type(f)) {
  4040. case UPB_TYPE_INT32: {
  4041. long val = strtol(str, &end, 0);
  4042. CHK(val <= INT32_MAX && val >= INT32_MIN && errno != ERANGE && !*end);
  4043. f->defaultval.sint = val;
  4044. break;
  4045. }
  4046. case UPB_TYPE_ENUM: {
  4047. const upb_enumdef *e = f->sub.enumdef;
  4048. int32_t val;
  4049. CHK(upb_enumdef_ntoi(e, str, len, &val));
  4050. f->defaultval.sint = val;
  4051. break;
  4052. }
  4053. case UPB_TYPE_INT64: {
  4054. /* XXX: Need to write our own strtoll, since it's not available in c89. */
  4055. int64_t val = strtol(str, &end, 0);
  4056. CHK(val <= INT64_MAX && val >= INT64_MIN && errno != ERANGE && !*end);
  4057. f->defaultval.sint = val;
  4058. break;
  4059. }
  4060. case UPB_TYPE_UINT32: {
  4061. unsigned long val = strtoul(str, &end, 0);
  4062. CHK(val <= UINT32_MAX && errno != ERANGE && !*end);
  4063. f->defaultval.uint = val;
  4064. break;
  4065. }
  4066. case UPB_TYPE_UINT64: {
  4067. /* XXX: Need to write our own strtoull, since it's not available in c89. */
  4068. uint64_t val = strtoul(str, &end, 0);
  4069. CHK(val <= UINT64_MAX && errno != ERANGE && !*end);
  4070. f->defaultval.uint = val;
  4071. break;
  4072. }
  4073. case UPB_TYPE_DOUBLE: {
  4074. double val = strtod(str, &end);
  4075. CHK(errno != ERANGE && !*end);
  4076. f->defaultval.dbl = val;
  4077. break;
  4078. }
  4079. case UPB_TYPE_FLOAT: {
  4080. /* XXX: Need to write our own strtof, since it's not available in c89. */
  4081. float val = strtod(str, &end);
  4082. CHK(errno != ERANGE && !*end);
  4083. f->defaultval.flt = val;
  4084. break;
  4085. }
  4086. case UPB_TYPE_BOOL: {
  4087. if (streql2(str, len, "false")) {
  4088. f->defaultval.boolean = false;
  4089. } else if (streql2(str, len, "true")) {
  4090. f->defaultval.boolean = true;
  4091. } else {
  4092. return false;
  4093. }
  4094. break;
  4095. }
  4096. case UPB_TYPE_STRING:
  4097. f->defaultval.str = newstr(ctx->alloc, str, len);
  4098. break;
  4099. case UPB_TYPE_BYTES:
  4100. /* XXX: need to interpret the C-escaped value. */
  4101. f->defaultval.str = newstr(ctx->alloc, str, len);
  4102. break;
  4103. case UPB_TYPE_MESSAGE:
  4104. /* Should not have a default value. */
  4105. return false;
  4106. }
  4107. return true;
  4108. }
  4109. static void set_default_default(const symtab_addctx *ctx, upb_fielddef *f) {
  4110. switch (upb_fielddef_type(f)) {
  4111. case UPB_TYPE_INT32:
  4112. case UPB_TYPE_INT64:
  4113. case UPB_TYPE_ENUM:
  4114. f->defaultval.sint = 0;
  4115. break;
  4116. case UPB_TYPE_UINT64:
  4117. case UPB_TYPE_UINT32:
  4118. f->defaultval.uint = 0;
  4119. break;
  4120. case UPB_TYPE_DOUBLE:
  4121. case UPB_TYPE_FLOAT:
  4122. f->defaultval.dbl = 0;
  4123. break;
  4124. case UPB_TYPE_STRING:
  4125. case UPB_TYPE_BYTES:
  4126. f->defaultval.str = newstr(ctx->alloc, NULL, 0);
  4127. break;
  4128. case UPB_TYPE_BOOL:
  4129. f->defaultval.boolean = false;
  4130. break;
  4131. case UPB_TYPE_MESSAGE:
  4132. break;
  4133. }
  4134. }
  4135. static bool create_fielddef(
  4136. const symtab_addctx *ctx, const char *prefix, upb_msgdef *m,
  4137. const google_protobuf_FieldDescriptorProto *field_proto) {
  4138. upb_alloc *alloc = ctx->alloc;
  4139. upb_fielddef *f;
  4140. const google_protobuf_FieldOptions *options;
  4141. upb_strview name;
  4142. const char *full_name;
  4143. const char *json_name;
  4144. const char *shortname;
  4145. uint32_t field_number;
  4146. if (!google_protobuf_FieldDescriptorProto_has_name(field_proto)) {
  4147. upb_status_seterrmsg(ctx->status, "field has no name");
  4148. return false;
  4149. }
  4150. name = google_protobuf_FieldDescriptorProto_name(field_proto);
  4151. CHK(upb_isident(name, false, ctx->status));
  4152. full_name = makefullname(ctx, prefix, name);
  4153. shortname = shortdefname(full_name);
  4154. if (google_protobuf_FieldDescriptorProto_has_json_name(field_proto)) {
  4155. json_name = strviewdup(
  4156. ctx, google_protobuf_FieldDescriptorProto_json_name(field_proto));
  4157. } else {
  4158. json_name = makejsonname(shortname, ctx->alloc);
  4159. }
  4160. field_number = google_protobuf_FieldDescriptorProto_number(field_proto);
  4161. if (field_number == 0 || field_number > UPB_MAX_FIELDNUMBER) {
  4162. upb_status_seterrf(ctx->status, "invalid field number (%u)", field_number);
  4163. return false;
  4164. }
  4165. if (m) {
  4166. /* direct message field. */
  4167. upb_value v, field_v, json_v;
  4168. size_t json_size;
  4169. f = (upb_fielddef*)&m->fields[m->field_count++];
  4170. f->msgdef = m;
  4171. f->is_extension_ = false;
  4172. if (upb_strtable_lookup(&m->ntof, shortname, NULL)) {
  4173. upb_status_seterrf(ctx->status, "duplicate field name (%s)", shortname);
  4174. return false;
  4175. }
  4176. if (upb_strtable_lookup(&m->ntof, json_name, NULL)) {
  4177. upb_status_seterrf(ctx->status, "duplicate json_name (%s)", json_name);
  4178. return false;
  4179. }
  4180. if (upb_inttable_lookup(&m->itof, field_number, NULL)) {
  4181. upb_status_seterrf(ctx->status, "duplicate field number (%u)",
  4182. field_number);
  4183. return false;
  4184. }
  4185. field_v = pack_def(f, UPB_DEFTYPE_FIELD);
  4186. json_v = pack_def(f, UPB_DEFTYPE_FIELD_JSONNAME);
  4187. v = upb_value_constptr(f);
  4188. json_size = strlen(json_name);
  4189. CHK_OOM(
  4190. upb_strtable_insert3(&m->ntof, name.data, name.size, field_v, alloc));
  4191. CHK_OOM(upb_inttable_insert2(&m->itof, field_number, v, alloc));
  4192. if (strcmp(shortname, json_name) != 0) {
  4193. upb_strtable_insert3(&m->ntof, json_name, json_size, json_v, alloc);
  4194. }
  4195. if (ctx->layouts) {
  4196. const upb_msglayout_field *fields = m->layout->fields;
  4197. int count = m->layout->field_count;
  4198. bool found = false;
  4199. int i;
  4200. for (i = 0; i < count; i++) {
  4201. if (fields[i].number == field_number) {
  4202. f->layout_index = i;
  4203. found = true;
  4204. break;
  4205. }
  4206. }
  4207. UPB_ASSERT(found);
  4208. }
  4209. } else {
  4210. /* extension field. */
  4211. f = (upb_fielddef*)&ctx->file->exts[ctx->file->ext_count++];
  4212. f->is_extension_ = true;
  4213. CHK_OOM(symtab_add(ctx, full_name, pack_def(f, UPB_DEFTYPE_FIELD)));
  4214. }
  4215. f->full_name = full_name;
  4216. f->json_name = json_name;
  4217. f->file = ctx->file;
  4218. f->type_ = (int)google_protobuf_FieldDescriptorProto_type(field_proto);
  4219. f->label_ = (int)google_protobuf_FieldDescriptorProto_label(field_proto);
  4220. f->number_ = field_number;
  4221. f->oneof = NULL;
  4222. f->proto3_optional_ =
  4223. google_protobuf_FieldDescriptorProto_proto3_optional(field_proto);
  4224. /* We can't resolve the subdef or (in the case of extensions) the containing
  4225. * message yet, because it may not have been defined yet. We stash a pointer
  4226. * to the field_proto until later when we can properly resolve it. */
  4227. f->sub.unresolved = field_proto;
  4228. if (f->label_ == UPB_LABEL_REQUIRED && f->file->syntax == UPB_SYNTAX_PROTO3) {
  4229. upb_status_seterrf(ctx->status, "proto3 fields cannot be required (%s)",
  4230. f->full_name);
  4231. return false;
  4232. }
  4233. if (google_protobuf_FieldDescriptorProto_has_oneof_index(field_proto)) {
  4234. int oneof_index =
  4235. google_protobuf_FieldDescriptorProto_oneof_index(field_proto);
  4236. upb_oneofdef *oneof;
  4237. upb_value v = upb_value_constptr(f);
  4238. if (upb_fielddef_label(f) != UPB_LABEL_OPTIONAL) {
  4239. upb_status_seterrf(ctx->status,
  4240. "fields in oneof must have OPTIONAL label (%s)",
  4241. f->full_name);
  4242. return false;
  4243. }
  4244. if (!m) {
  4245. upb_status_seterrf(ctx->status,
  4246. "oneof_index provided for extension field (%s)",
  4247. f->full_name);
  4248. return false;
  4249. }
  4250. if (oneof_index >= m->oneof_count) {
  4251. upb_status_seterrf(ctx->status, "oneof_index out of range (%s)",
  4252. f->full_name);
  4253. return false;
  4254. }
  4255. oneof = (upb_oneofdef*)&m->oneofs[oneof_index];
  4256. f->oneof = oneof;
  4257. CHK(upb_inttable_insert2(&oneof->itof, f->number_, v, alloc));
  4258. CHK(upb_strtable_insert3(&oneof->ntof, name.data, name.size, v, alloc));
  4259. } else {
  4260. f->oneof = NULL;
  4261. }
  4262. options = google_protobuf_FieldDescriptorProto_has_options(field_proto) ?
  4263. google_protobuf_FieldDescriptorProto_options(field_proto) : NULL;
  4264. if (options && google_protobuf_FieldOptions_has_packed(options)) {
  4265. f->packed_ = google_protobuf_FieldOptions_packed(options);
  4266. } else {
  4267. /* Repeated fields default to packed for proto3 only. */
  4268. f->packed_ = upb_fielddef_isprimitive(f) &&
  4269. f->label_ == UPB_LABEL_REPEATED && f->file->syntax == UPB_SYNTAX_PROTO3;
  4270. }
  4271. if (options) {
  4272. f->lazy_ = google_protobuf_FieldOptions_lazy(options);
  4273. } else {
  4274. f->lazy_ = false;
  4275. }
  4276. return true;
  4277. }
  4278. static bool create_enumdef(
  4279. const symtab_addctx *ctx, const char *prefix,
  4280. const google_protobuf_EnumDescriptorProto *enum_proto) {
  4281. upb_enumdef *e;
  4282. const google_protobuf_EnumValueDescriptorProto *const *values;
  4283. upb_strview name;
  4284. size_t i, n;
  4285. name = google_protobuf_EnumDescriptorProto_name(enum_proto);
  4286. CHK(upb_isident(name, false, ctx->status));
  4287. e = (upb_enumdef*)&ctx->file->enums[ctx->file->enum_count++];
  4288. e->full_name = makefullname(ctx, prefix, name);
  4289. CHK_OOM(symtab_add(ctx, e->full_name, pack_def(e, UPB_DEFTYPE_ENUM)));
  4290. CHK_OOM(upb_strtable_init2(&e->ntoi, UPB_CTYPE_INT32, ctx->alloc));
  4291. CHK_OOM(upb_inttable_init2(&e->iton, UPB_CTYPE_CSTR, ctx->alloc));
  4292. e->file = ctx->file;
  4293. e->defaultval = 0;
  4294. values = google_protobuf_EnumDescriptorProto_value(enum_proto, &n);
  4295. if (n == 0) {
  4296. upb_status_seterrf(ctx->status,
  4297. "enums must contain at least one value (%s)",
  4298. e->full_name);
  4299. return false;
  4300. }
  4301. for (i = 0; i < n; i++) {
  4302. const google_protobuf_EnumValueDescriptorProto *value = values[i];
  4303. upb_strview name = google_protobuf_EnumValueDescriptorProto_name(value);
  4304. char *name2 = strviewdup(ctx, name);
  4305. int32_t num = google_protobuf_EnumValueDescriptorProto_number(value);
  4306. upb_value v = upb_value_int32(num);
  4307. if (i == 0 && e->file->syntax == UPB_SYNTAX_PROTO3 && num != 0) {
  4308. upb_status_seterrf(ctx->status,
  4309. "for proto3, the first enum value must be zero (%s)",
  4310. e->full_name);
  4311. return false;
  4312. }
  4313. if (upb_strtable_lookup(&e->ntoi, name2, NULL)) {
  4314. upb_status_seterrf(ctx->status, "duplicate enum label '%s'", name2);
  4315. return false;
  4316. }
  4317. CHK_OOM(name2)
  4318. CHK_OOM(
  4319. upb_strtable_insert3(&e->ntoi, name2, strlen(name2), v, ctx->alloc));
  4320. if (!upb_inttable_lookup(&e->iton, num, NULL)) {
  4321. upb_value v = upb_value_cstr(name2);
  4322. CHK_OOM(upb_inttable_insert2(&e->iton, num, v, ctx->alloc));
  4323. }
  4324. }
  4325. upb_inttable_compact2(&e->iton, ctx->alloc);
  4326. return true;
  4327. }
  4328. static bool create_msgdef(symtab_addctx *ctx, const char *prefix,
  4329. const google_protobuf_DescriptorProto *msg_proto) {
  4330. upb_msgdef *m;
  4331. const google_protobuf_MessageOptions *options;
  4332. const google_protobuf_OneofDescriptorProto *const *oneofs;
  4333. const google_protobuf_FieldDescriptorProto *const *fields;
  4334. const google_protobuf_EnumDescriptorProto *const *enums;
  4335. const google_protobuf_DescriptorProto *const *msgs;
  4336. size_t i, n;
  4337. upb_strview name;
  4338. name = google_protobuf_DescriptorProto_name(msg_proto);
  4339. CHK(upb_isident(name, false, ctx->status));
  4340. m = (upb_msgdef*)&ctx->file->msgs[ctx->file->msg_count++];
  4341. m->full_name = makefullname(ctx, prefix, name);
  4342. CHK_OOM(symtab_add(ctx, m->full_name, pack_def(m, UPB_DEFTYPE_MSG)));
  4343. CHK_OOM(upb_inttable_init2(&m->itof, UPB_CTYPE_CONSTPTR, ctx->alloc));
  4344. CHK_OOM(upb_strtable_init2(&m->ntof, UPB_CTYPE_CONSTPTR, ctx->alloc));
  4345. m->file = ctx->file;
  4346. m->map_entry = false;
  4347. options = google_protobuf_DescriptorProto_options(msg_proto);
  4348. if (options) {
  4349. m->map_entry = google_protobuf_MessageOptions_map_entry(options);
  4350. }
  4351. if (ctx->layouts) {
  4352. m->layout = *ctx->layouts;
  4353. ctx->layouts++;
  4354. } else {
  4355. /* Allocate now (to allow cross-linking), populate later. */
  4356. m->layout = upb_malloc(ctx->alloc, sizeof(*m->layout));
  4357. }
  4358. oneofs = google_protobuf_DescriptorProto_oneof_decl(msg_proto, &n);
  4359. m->oneof_count = 0;
  4360. m->oneofs = upb_malloc(ctx->alloc, sizeof(*m->oneofs) * n);
  4361. for (i = 0; i < n; i++) {
  4362. CHK(create_oneofdef(ctx, m, oneofs[i]));
  4363. }
  4364. fields = google_protobuf_DescriptorProto_field(msg_proto, &n);
  4365. m->field_count = 0;
  4366. m->fields = upb_malloc(ctx->alloc, sizeof(*m->fields) * n);
  4367. for (i = 0; i < n; i++) {
  4368. CHK(create_fielddef(ctx, m->full_name, m, fields[i]));
  4369. }
  4370. CHK(assign_msg_indices(m, ctx->status));
  4371. CHK(check_oneofs(m, ctx->status));
  4372. assign_msg_wellknowntype(m);
  4373. upb_inttable_compact2(&m->itof, ctx->alloc);
  4374. /* This message is built. Now build nested messages and enums. */
  4375. enums = google_protobuf_DescriptorProto_enum_type(msg_proto, &n);
  4376. for (i = 0; i < n; i++) {
  4377. CHK(create_enumdef(ctx, m->full_name, enums[i]));
  4378. }
  4379. msgs = google_protobuf_DescriptorProto_nested_type(msg_proto, &n);
  4380. for (i = 0; i < n; i++) {
  4381. CHK(create_msgdef(ctx, m->full_name, msgs[i]));
  4382. }
  4383. return true;
  4384. }
  4385. typedef struct {
  4386. int msg_count;
  4387. int enum_count;
  4388. int ext_count;
  4389. } decl_counts;
  4390. static void count_types_in_msg(const google_protobuf_DescriptorProto *msg_proto,
  4391. decl_counts *counts) {
  4392. const google_protobuf_DescriptorProto *const *msgs;
  4393. size_t i, n;
  4394. counts->msg_count++;
  4395. msgs = google_protobuf_DescriptorProto_nested_type(msg_proto, &n);
  4396. for (i = 0; i < n; i++) {
  4397. count_types_in_msg(msgs[i], counts);
  4398. }
  4399. google_protobuf_DescriptorProto_enum_type(msg_proto, &n);
  4400. counts->enum_count += n;
  4401. google_protobuf_DescriptorProto_extension(msg_proto, &n);
  4402. counts->ext_count += n;
  4403. }
  4404. static void count_types_in_file(
  4405. const google_protobuf_FileDescriptorProto *file_proto,
  4406. decl_counts *counts) {
  4407. const google_protobuf_DescriptorProto *const *msgs;
  4408. size_t i, n;
  4409. msgs = google_protobuf_FileDescriptorProto_message_type(file_proto, &n);
  4410. for (i = 0; i < n; i++) {
  4411. count_types_in_msg(msgs[i], counts);
  4412. }
  4413. google_protobuf_FileDescriptorProto_enum_type(file_proto, &n);
  4414. counts->enum_count += n;
  4415. google_protobuf_FileDescriptorProto_extension(file_proto, &n);
  4416. counts->ext_count += n;
  4417. }
  4418. static bool resolve_fielddef(const symtab_addctx *ctx, const char *prefix,
  4419. upb_fielddef *f) {
  4420. upb_strview name;
  4421. const google_protobuf_FieldDescriptorProto *field_proto = f->sub.unresolved;
  4422. if (f->is_extension_) {
  4423. if (!google_protobuf_FieldDescriptorProto_has_extendee(field_proto)) {
  4424. upb_status_seterrf(ctx->status,
  4425. "extension for field '%s' had no extendee",
  4426. f->full_name);
  4427. return false;
  4428. }
  4429. name = google_protobuf_FieldDescriptorProto_extendee(field_proto);
  4430. f->msgdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_MSG);
  4431. CHK(f->msgdef);
  4432. }
  4433. if ((upb_fielddef_issubmsg(f) || f->type_ == UPB_DESCRIPTOR_TYPE_ENUM) &&
  4434. !google_protobuf_FieldDescriptorProto_has_type_name(field_proto)) {
  4435. upb_status_seterrf(ctx->status, "field '%s' is missing type name",
  4436. f->full_name);
  4437. return false;
  4438. }
  4439. name = google_protobuf_FieldDescriptorProto_type_name(field_proto);
  4440. if (upb_fielddef_issubmsg(f)) {
  4441. f->sub.msgdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_MSG);
  4442. CHK(f->sub.msgdef);
  4443. } else if (f->type_ == UPB_DESCRIPTOR_TYPE_ENUM) {
  4444. f->sub.enumdef = symtab_resolve(ctx, f, prefix, name, UPB_DEFTYPE_ENUM);
  4445. CHK(f->sub.enumdef);
  4446. }
  4447. /* Have to delay resolving of the default value until now because of the enum
  4448. * case, since enum defaults are specified with a label. */
  4449. if (google_protobuf_FieldDescriptorProto_has_default_value(field_proto)) {
  4450. upb_strview defaultval =
  4451. google_protobuf_FieldDescriptorProto_default_value(field_proto);
  4452. if (f->file->syntax == UPB_SYNTAX_PROTO3) {
  4453. upb_status_seterrf(ctx->status,
  4454. "proto3 fields cannot have explicit defaults (%s)",
  4455. f->full_name);
  4456. return false;
  4457. }
  4458. if (upb_fielddef_issubmsg(f)) {
  4459. upb_status_seterrf(ctx->status,
  4460. "message fields cannot have explicit defaults (%s)",
  4461. f->full_name);
  4462. return false;
  4463. }
  4464. if (!parse_default(ctx, defaultval.data, defaultval.size, f)) {
  4465. upb_status_seterrf(ctx->status,
  4466. "couldn't parse default '" UPB_STRVIEW_FORMAT
  4467. "' for field (%s)",
  4468. UPB_STRVIEW_ARGS(defaultval), f->full_name);
  4469. return false;
  4470. }
  4471. } else {
  4472. set_default_default(ctx, f);
  4473. }
  4474. return true;
  4475. }
  4476. static bool build_filedef(
  4477. symtab_addctx *ctx, upb_filedef *file,
  4478. const google_protobuf_FileDescriptorProto *file_proto) {
  4479. upb_alloc *alloc = ctx->alloc;
  4480. const google_protobuf_FileOptions *file_options_proto;
  4481. const google_protobuf_DescriptorProto *const *msgs;
  4482. const google_protobuf_EnumDescriptorProto *const *enums;
  4483. const google_protobuf_FieldDescriptorProto *const *exts;
  4484. const upb_strview* strs;
  4485. size_t i, n;
  4486. decl_counts counts = {0, 0, 0};
  4487. count_types_in_file(file_proto, &counts);
  4488. file->msgs = upb_malloc(alloc, sizeof(*file->msgs) * counts.msg_count);
  4489. file->enums = upb_malloc(alloc, sizeof(*file->enums) * counts.enum_count);
  4490. file->exts = upb_malloc(alloc, sizeof(*file->exts) * counts.ext_count);
  4491. CHK_OOM(counts.msg_count == 0 || file->msgs);
  4492. CHK_OOM(counts.enum_count == 0 || file->enums);
  4493. CHK_OOM(counts.ext_count == 0 || file->exts);
  4494. /* We increment these as defs are added. */
  4495. file->msg_count = 0;
  4496. file->enum_count = 0;
  4497. file->ext_count = 0;
  4498. if (!google_protobuf_FileDescriptorProto_has_name(file_proto)) {
  4499. upb_status_seterrmsg(ctx->status, "File has no name");
  4500. return false;
  4501. }
  4502. file->name =
  4503. strviewdup(ctx, google_protobuf_FileDescriptorProto_name(file_proto));
  4504. file->phpprefix = NULL;
  4505. file->phpnamespace = NULL;
  4506. if (google_protobuf_FileDescriptorProto_has_package(file_proto)) {
  4507. upb_strview package =
  4508. google_protobuf_FileDescriptorProto_package(file_proto);
  4509. CHK(upb_isident(package, true, ctx->status));
  4510. file->package = strviewdup(ctx, package);
  4511. } else {
  4512. file->package = NULL;
  4513. }
  4514. if (google_protobuf_FileDescriptorProto_has_syntax(file_proto)) {
  4515. upb_strview syntax =
  4516. google_protobuf_FileDescriptorProto_syntax(file_proto);
  4517. if (streql_view(syntax, "proto2")) {
  4518. file->syntax = UPB_SYNTAX_PROTO2;
  4519. } else if (streql_view(syntax, "proto3")) {
  4520. file->syntax = UPB_SYNTAX_PROTO3;
  4521. } else {
  4522. upb_status_seterrf(ctx->status, "Invalid syntax '" UPB_STRVIEW_FORMAT "'",
  4523. UPB_STRVIEW_ARGS(syntax));
  4524. return false;
  4525. }
  4526. } else {
  4527. file->syntax = UPB_SYNTAX_PROTO2;
  4528. }
  4529. /* Read options. */
  4530. file_options_proto = google_protobuf_FileDescriptorProto_options(file_proto);
  4531. if (file_options_proto) {
  4532. if (google_protobuf_FileOptions_has_php_class_prefix(file_options_proto)) {
  4533. file->phpprefix = strviewdup(
  4534. ctx,
  4535. google_protobuf_FileOptions_php_class_prefix(file_options_proto));
  4536. }
  4537. if (google_protobuf_FileOptions_has_php_namespace(file_options_proto)) {
  4538. file->phpnamespace = strviewdup(
  4539. ctx, google_protobuf_FileOptions_php_namespace(file_options_proto));
  4540. }
  4541. }
  4542. /* Verify dependencies. */
  4543. strs = google_protobuf_FileDescriptorProto_dependency(file_proto, &n);
  4544. file->deps = upb_malloc(alloc, sizeof(*file->deps) * n) ;
  4545. CHK_OOM(n == 0 || file->deps);
  4546. for (i = 0; i < n; i++) {
  4547. upb_strview dep_name = strs[i];
  4548. upb_value v;
  4549. if (!upb_strtable_lookup2(&ctx->symtab->files, dep_name.data,
  4550. dep_name.size, &v)) {
  4551. upb_status_seterrf(ctx->status,
  4552. "Depends on file '" UPB_STRVIEW_FORMAT
  4553. "', but it has not been loaded",
  4554. UPB_STRVIEW_ARGS(dep_name));
  4555. return false;
  4556. }
  4557. file->deps[i] = upb_value_getconstptr(v);
  4558. }
  4559. /* Create messages. */
  4560. msgs = google_protobuf_FileDescriptorProto_message_type(file_proto, &n);
  4561. for (i = 0; i < n; i++) {
  4562. CHK(create_msgdef(ctx, file->package, msgs[i]));
  4563. }
  4564. /* Create enums. */
  4565. enums = google_protobuf_FileDescriptorProto_enum_type(file_proto, &n);
  4566. for (i = 0; i < n; i++) {
  4567. CHK(create_enumdef(ctx, file->package, enums[i]));
  4568. }
  4569. /* Create extensions. */
  4570. exts = google_protobuf_FileDescriptorProto_extension(file_proto, &n);
  4571. file->exts = upb_malloc(alloc, sizeof(*file->exts) * n);
  4572. CHK_OOM(n == 0 || file->exts);
  4573. for (i = 0; i < n; i++) {
  4574. CHK(create_fielddef(ctx, file->package, NULL, exts[i]));
  4575. }
  4576. /* Now that all names are in the table, build layouts and resolve refs. */
  4577. for (i = 0; i < (size_t)file->ext_count; i++) {
  4578. CHK(resolve_fielddef(ctx, file->package, (upb_fielddef*)&file->exts[i]));
  4579. }
  4580. for (i = 0; i < (size_t)file->msg_count; i++) {
  4581. const upb_msgdef *m = &file->msgs[i];
  4582. int j;
  4583. for (j = 0; j < m->field_count; j++) {
  4584. CHK(resolve_fielddef(ctx, m->full_name, (upb_fielddef*)&m->fields[j]));
  4585. }
  4586. }
  4587. if (!ctx->layouts) {
  4588. for (i = 0; i < (size_t)file->msg_count; i++) {
  4589. const upb_msgdef *m = &file->msgs[i];
  4590. make_layout(ctx->symtab, m);
  4591. }
  4592. }
  4593. return true;
  4594. }
  4595. static bool upb_symtab_addtotabs(upb_symtab *s, symtab_addctx *ctx) {
  4596. const upb_filedef *file = ctx->file;
  4597. upb_alloc *alloc = upb_arena_alloc(s->arena);
  4598. upb_strtable_iter iter;
  4599. CHK_OOM(upb_strtable_insert3(&s->files, file->name, strlen(file->name),
  4600. upb_value_constptr(file), alloc));
  4601. upb_strtable_begin(&iter, ctx->addtab);
  4602. for (; !upb_strtable_done(&iter); upb_strtable_next(&iter)) {
  4603. upb_strview key = upb_strtable_iter_key(&iter);
  4604. upb_value value = upb_strtable_iter_value(&iter);
  4605. CHK_OOM(upb_strtable_insert3(&s->syms, key.data, key.size, value, alloc));
  4606. }
  4607. return true;
  4608. }
  4609. /* upb_filedef ****************************************************************/
  4610. const char *upb_filedef_name(const upb_filedef *f) {
  4611. return f->name;
  4612. }
  4613. const char *upb_filedef_package(const upb_filedef *f) {
  4614. return f->package;
  4615. }
  4616. const char *upb_filedef_phpprefix(const upb_filedef *f) {
  4617. return f->phpprefix;
  4618. }
  4619. const char *upb_filedef_phpnamespace(const upb_filedef *f) {
  4620. return f->phpnamespace;
  4621. }
  4622. upb_syntax_t upb_filedef_syntax(const upb_filedef *f) {
  4623. return f->syntax;
  4624. }
  4625. int upb_filedef_msgcount(const upb_filedef *f) {
  4626. return f->msg_count;
  4627. }
  4628. int upb_filedef_depcount(const upb_filedef *f) {
  4629. return f->dep_count;
  4630. }
  4631. int upb_filedef_enumcount(const upb_filedef *f) {
  4632. return f->enum_count;
  4633. }
  4634. const upb_filedef *upb_filedef_dep(const upb_filedef *f, int i) {
  4635. return i < 0 || i >= f->dep_count ? NULL : f->deps[i];
  4636. }
  4637. const upb_msgdef *upb_filedef_msg(const upb_filedef *f, int i) {
  4638. return i < 0 || i >= f->msg_count ? NULL : &f->msgs[i];
  4639. }
  4640. const upb_enumdef *upb_filedef_enum(const upb_filedef *f, int i) {
  4641. return i < 0 || i >= f->enum_count ? NULL : &f->enums[i];
  4642. }
  4643. void upb_symtab_free(upb_symtab *s) {
  4644. upb_arena_free(s->arena);
  4645. upb_gfree(s);
  4646. }
  4647. upb_symtab *upb_symtab_new(void) {
  4648. upb_symtab *s = upb_gmalloc(sizeof(*s));
  4649. upb_alloc *alloc;
  4650. if (!s) {
  4651. return NULL;
  4652. }
  4653. s->arena = upb_arena_new();
  4654. alloc = upb_arena_alloc(s->arena);
  4655. if (!upb_strtable_init2(&s->syms, UPB_CTYPE_CONSTPTR, alloc) ||
  4656. !upb_strtable_init2(&s->files, UPB_CTYPE_CONSTPTR, alloc)) {
  4657. upb_arena_free(s->arena);
  4658. upb_gfree(s);
  4659. s = NULL;
  4660. }
  4661. return s;
  4662. }
  4663. const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym) {
  4664. upb_value v;
  4665. return upb_strtable_lookup(&s->syms, sym, &v) ?
  4666. unpack_def(v, UPB_DEFTYPE_MSG) : NULL;
  4667. }
  4668. const upb_msgdef *upb_symtab_lookupmsg2(const upb_symtab *s, const char *sym,
  4669. size_t len) {
  4670. upb_value v;
  4671. return upb_strtable_lookup2(&s->syms, sym, len, &v) ?
  4672. unpack_def(v, UPB_DEFTYPE_MSG) : NULL;
  4673. }
  4674. const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym) {
  4675. upb_value v;
  4676. return upb_strtable_lookup(&s->syms, sym, &v) ?
  4677. unpack_def(v, UPB_DEFTYPE_ENUM) : NULL;
  4678. }
  4679. const upb_filedef *upb_symtab_lookupfile(const upb_symtab *s, const char *name) {
  4680. upb_value v;
  4681. return upb_strtable_lookup(&s->files, name, &v) ? upb_value_getconstptr(v)
  4682. : NULL;
  4683. }
  4684. const upb_filedef *upb_symtab_lookupfile2(
  4685. const upb_symtab *s, const char *name, size_t len) {
  4686. upb_value v;
  4687. return upb_strtable_lookup2(&s->files, name, len, &v) ?
  4688. upb_value_getconstptr(v) : NULL;
  4689. }
  4690. int upb_symtab_filecount(const upb_symtab *s) {
  4691. return (int)upb_strtable_count(&s->files);
  4692. }
  4693. static const upb_filedef *_upb_symtab_addfile(
  4694. upb_symtab *s, const google_protobuf_FileDescriptorProto *file_proto,
  4695. const upb_msglayout **layouts, upb_status *status) {
  4696. upb_arena *tmparena = upb_arena_new();
  4697. upb_strtable addtab;
  4698. upb_alloc *alloc = upb_arena_alloc(s->arena);
  4699. upb_filedef *file = upb_malloc(alloc, sizeof(*file));
  4700. bool ok;
  4701. symtab_addctx ctx;
  4702. ctx.file = file;
  4703. ctx.symtab = s;
  4704. ctx.alloc = alloc;
  4705. ctx.tmp = upb_arena_alloc(tmparena);
  4706. ctx.addtab = &addtab;
  4707. ctx.layouts = layouts;
  4708. ctx.status = status;
  4709. ok = file && upb_strtable_init2(&addtab, UPB_CTYPE_CONSTPTR, ctx.tmp) &&
  4710. build_filedef(&ctx, file, file_proto) && upb_symtab_addtotabs(s, &ctx);
  4711. upb_arena_free(tmparena);
  4712. return ok ? file : NULL;
  4713. }
  4714. const upb_filedef *upb_symtab_addfile(
  4715. upb_symtab *s, const google_protobuf_FileDescriptorProto *file_proto,
  4716. upb_status *status) {
  4717. return _upb_symtab_addfile(s, file_proto, NULL, status);
  4718. }
  4719. /* Include here since we want most of this file to be stdio-free. */
  4720. #include <stdio.h>
  4721. bool _upb_symtab_loaddefinit(upb_symtab *s, const upb_def_init *init) {
  4722. /* Since this function should never fail (it would indicate a bug in upb) we
  4723. * print errors to stderr instead of returning error status to the user. */
  4724. upb_def_init **deps = init->deps;
  4725. google_protobuf_FileDescriptorProto *file;
  4726. upb_arena *arena;
  4727. upb_status status;
  4728. upb_status_clear(&status);
  4729. if (upb_strtable_lookup(&s->files, init->filename, NULL)) {
  4730. return true;
  4731. }
  4732. arena = upb_arena_new();
  4733. for (; *deps; deps++) {
  4734. if (!_upb_symtab_loaddefinit(s, *deps)) goto err;
  4735. }
  4736. file = google_protobuf_FileDescriptorProto_parse(
  4737. init->descriptor.data, init->descriptor.size, arena);
  4738. if (!file) {
  4739. upb_status_seterrf(
  4740. &status,
  4741. "Failed to parse compiled-in descriptor for file '%s'. This should "
  4742. "never happen.",
  4743. init->filename);
  4744. goto err;
  4745. }
  4746. if (!_upb_symtab_addfile(s, file, init->layouts, &status)) goto err;
  4747. upb_arena_free(arena);
  4748. return true;
  4749. err:
  4750. fprintf(stderr, "Error loading compiled-in descriptor: %s\n",
  4751. upb_status_errmsg(&status));
  4752. upb_arena_free(arena);
  4753. return false;
  4754. }
  4755. #undef CHK
  4756. #undef CHK_OOM
  4757. #include <string.h>
  4758. static size_t get_field_size(const upb_msglayout_field *f) {
  4759. static unsigned char sizes[] = {
  4760. 0,/* 0 */
  4761. 8, /* UPB_DESCRIPTOR_TYPE_DOUBLE */
  4762. 4, /* UPB_DESCRIPTOR_TYPE_FLOAT */
  4763. 8, /* UPB_DESCRIPTOR_TYPE_INT64 */
  4764. 8, /* UPB_DESCRIPTOR_TYPE_UINT64 */
  4765. 4, /* UPB_DESCRIPTOR_TYPE_INT32 */
  4766. 8, /* UPB_DESCRIPTOR_TYPE_FIXED64 */
  4767. 4, /* UPB_DESCRIPTOR_TYPE_FIXED32 */
  4768. 1, /* UPB_DESCRIPTOR_TYPE_BOOL */
  4769. sizeof(upb_strview), /* UPB_DESCRIPTOR_TYPE_STRING */
  4770. sizeof(void*), /* UPB_DESCRIPTOR_TYPE_GROUP */
  4771. sizeof(void*), /* UPB_DESCRIPTOR_TYPE_MESSAGE */
  4772. sizeof(upb_strview), /* UPB_DESCRIPTOR_TYPE_BYTES */
  4773. 4, /* UPB_DESCRIPTOR_TYPE_UINT32 */
  4774. 4, /* UPB_DESCRIPTOR_TYPE_ENUM */
  4775. 4, /* UPB_DESCRIPTOR_TYPE_SFIXED32 */
  4776. 8, /* UPB_DESCRIPTOR_TYPE_SFIXED64 */
  4777. 4, /* UPB_DESCRIPTOR_TYPE_SINT32 */
  4778. 8, /* UPB_DESCRIPTOR_TYPE_SINT64 */
  4779. };
  4780. return _upb_repeated_or_map(f) ? sizeof(void *) : sizes[f->descriptortype];
  4781. }
  4782. /* Strings/bytes are special-cased in maps. */
  4783. static char _upb_fieldtype_to_mapsize[12] = {
  4784. 0,
  4785. 1, /* UPB_TYPE_BOOL */
  4786. 4, /* UPB_TYPE_FLOAT */
  4787. 4, /* UPB_TYPE_INT32 */
  4788. 4, /* UPB_TYPE_UINT32 */
  4789. 4, /* UPB_TYPE_ENUM */
  4790. sizeof(void*), /* UPB_TYPE_MESSAGE */
  4791. 8, /* UPB_TYPE_DOUBLE */
  4792. 8, /* UPB_TYPE_INT64 */
  4793. 8, /* UPB_TYPE_UINT64 */
  4794. 0, /* UPB_TYPE_STRING */
  4795. 0, /* UPB_TYPE_BYTES */
  4796. };
  4797. /** upb_msg *******************************************************************/
  4798. upb_msg *upb_msg_new(const upb_msgdef *m, upb_arena *a) {
  4799. return _upb_msg_new(upb_msgdef_layout(m), a);
  4800. }
  4801. static bool in_oneof(const upb_msglayout_field *field) {
  4802. return field->presence < 0;
  4803. }
  4804. static upb_msgval _upb_msg_getraw(const upb_msg *msg, const upb_fielddef *f) {
  4805. const upb_msglayout_field *field = upb_fielddef_layout(f);
  4806. const char *mem = UPB_PTR_AT(msg, field->offset, char);
  4807. upb_msgval val = {0};
  4808. memcpy(&val, mem, get_field_size(field));
  4809. return val;
  4810. }
  4811. bool upb_msg_has(const upb_msg *msg, const upb_fielddef *f) {
  4812. const upb_msglayout_field *field = upb_fielddef_layout(f);
  4813. if (in_oneof(field)) {
  4814. return _upb_getoneofcase_field(msg, field) == field->number;
  4815. } else if (field->presence > 0) {
  4816. return _upb_hasbit_field(msg, field);
  4817. } else {
  4818. UPB_ASSERT(field->descriptortype == UPB_DESCRIPTOR_TYPE_MESSAGE ||
  4819. field->descriptortype == UPB_DESCRIPTOR_TYPE_GROUP);
  4820. return _upb_msg_getraw(msg, f).msg_val != NULL;
  4821. }
  4822. }
  4823. const upb_fielddef *upb_msg_whichoneof(const upb_msg *msg,
  4824. const upb_oneofdef *o) {
  4825. upb_oneof_iter i;
  4826. const upb_fielddef *f;
  4827. const upb_msglayout_field *field;
  4828. const upb_msgdef *m = upb_oneofdef_containingtype(o);
  4829. uint32_t oneof_case;
  4830. /* This is far from optimal. */
  4831. upb_oneof_begin(&i, o);
  4832. if (upb_oneof_done(&i)) return false;
  4833. f = upb_oneof_iter_field(&i);
  4834. field = upb_fielddef_layout(f);
  4835. if (in_oneof(field)) {
  4836. oneof_case = _upb_getoneofcase_field(msg, field);
  4837. } else {
  4838. return _upb_hasbit_field(msg, field) ? f : NULL;
  4839. }
  4840. return oneof_case ? upb_msgdef_itof(m, oneof_case) : NULL;
  4841. }
  4842. upb_msgval upb_msg_get(const upb_msg *msg, const upb_fielddef *f) {
  4843. if (!upb_fielddef_haspresence(f) || upb_msg_has(msg, f)) {
  4844. return _upb_msg_getraw(msg, f);
  4845. } else {
  4846. /* TODO(haberman): change upb_fielddef to not require this switch(). */
  4847. upb_msgval val = {0};
  4848. switch (upb_fielddef_type(f)) {
  4849. case UPB_TYPE_INT32:
  4850. case UPB_TYPE_ENUM:
  4851. val.int32_val = upb_fielddef_defaultint32(f);
  4852. break;
  4853. case UPB_TYPE_INT64:
  4854. val.int64_val = upb_fielddef_defaultint64(f);
  4855. break;
  4856. case UPB_TYPE_UINT32:
  4857. val.uint32_val = upb_fielddef_defaultuint32(f);
  4858. break;
  4859. case UPB_TYPE_UINT64:
  4860. val.uint64_val = upb_fielddef_defaultuint64(f);
  4861. break;
  4862. case UPB_TYPE_FLOAT:
  4863. val.float_val = upb_fielddef_defaultfloat(f);
  4864. break;
  4865. case UPB_TYPE_DOUBLE:
  4866. val.double_val = upb_fielddef_defaultdouble(f);
  4867. break;
  4868. case UPB_TYPE_BOOL:
  4869. val.double_val = upb_fielddef_defaultbool(f);
  4870. break;
  4871. case UPB_TYPE_STRING:
  4872. case UPB_TYPE_BYTES:
  4873. val.str_val.data = upb_fielddef_defaultstr(f, &val.str_val.size);
  4874. break;
  4875. case UPB_TYPE_MESSAGE:
  4876. val.msg_val = NULL;
  4877. break;
  4878. }
  4879. return val;
  4880. }
  4881. }
  4882. upb_mutmsgval upb_msg_mutable(upb_msg *msg, const upb_fielddef *f,
  4883. upb_arena *a) {
  4884. const upb_msglayout_field *field = upb_fielddef_layout(f);
  4885. upb_mutmsgval ret;
  4886. char *mem = UPB_PTR_AT(msg, field->offset, char);
  4887. bool wrong_oneof =
  4888. in_oneof(field) && _upb_getoneofcase_field(msg, field) != field->number;
  4889. memcpy(&ret, mem, sizeof(void*));
  4890. if (a && (!ret.msg || wrong_oneof)) {
  4891. if (upb_fielddef_ismap(f)) {
  4892. const upb_msgdef *entry = upb_fielddef_msgsubdef(f);
  4893. const upb_fielddef *key = upb_msgdef_itof(entry, UPB_MAPENTRY_KEY);
  4894. const upb_fielddef *value = upb_msgdef_itof(entry, UPB_MAPENTRY_VALUE);
  4895. ret.map = upb_map_new(a, upb_fielddef_type(key), upb_fielddef_type(value));
  4896. } else if (upb_fielddef_isseq(f)) {
  4897. ret.array = upb_array_new(a, upb_fielddef_type(f));
  4898. } else {
  4899. UPB_ASSERT(upb_fielddef_issubmsg(f));
  4900. ret.msg = upb_msg_new(upb_fielddef_msgsubdef(f), a);
  4901. }
  4902. memcpy(mem, &ret, sizeof(void*));
  4903. if (wrong_oneof) {
  4904. *_upb_oneofcase_field(msg, field) = field->number;
  4905. } else if (field->presence > 0) {
  4906. _upb_sethas_field(msg, field);
  4907. }
  4908. }
  4909. return ret;
  4910. }
  4911. void upb_msg_set(upb_msg *msg, const upb_fielddef *f, upb_msgval val,
  4912. upb_arena *a) {
  4913. const upb_msglayout_field *field = upb_fielddef_layout(f);
  4914. char *mem = UPB_PTR_AT(msg, field->offset, char);
  4915. UPB_UNUSED(a); /* We reserve the right to make set insert into a map. */
  4916. memcpy(mem, &val, get_field_size(field));
  4917. if (field->presence > 0) {
  4918. _upb_sethas_field(msg, field);
  4919. } else if (in_oneof(field)) {
  4920. *_upb_oneofcase_field(msg, field) = field->number;
  4921. }
  4922. }
  4923. void upb_msg_clearfield(upb_msg *msg, const upb_fielddef *f) {
  4924. const upb_msglayout_field *field = upb_fielddef_layout(f);
  4925. char *mem = UPB_PTR_AT(msg, field->offset, char);
  4926. if (field->presence > 0) {
  4927. _upb_clearhas_field(msg, field);
  4928. } else if (in_oneof(field)) {
  4929. uint32_t *oneof_case = _upb_oneofcase_field(msg, field);
  4930. if (*oneof_case != field->number) return;
  4931. *oneof_case = 0;
  4932. }
  4933. memset(mem, 0, get_field_size(field));
  4934. }
  4935. void upb_msg_clear(upb_msg *msg, const upb_msgdef *m) {
  4936. _upb_msg_clear(msg, upb_msgdef_layout(m));
  4937. }
  4938. bool upb_msg_next(const upb_msg *msg, const upb_msgdef *m,
  4939. const upb_symtab *ext_pool, const upb_fielddef **out_f,
  4940. upb_msgval *out_val, size_t *iter) {
  4941. size_t i = *iter;
  4942. const upb_msgval zero = {0};
  4943. const upb_fielddef *f;
  4944. UPB_UNUSED(ext_pool);
  4945. while ((f = _upb_msgdef_field(m, (int)++i)) != NULL) {
  4946. upb_msgval val = _upb_msg_getraw(msg, f);
  4947. /* Skip field if unset or empty. */
  4948. if (upb_fielddef_haspresence(f)) {
  4949. if (!upb_msg_has(msg, f)) continue;
  4950. } else {
  4951. upb_msgval test = val;
  4952. if (upb_fielddef_isstring(f) && !upb_fielddef_isseq(f)) {
  4953. /* Clear string pointer, only size matters (ptr could be non-NULL). */
  4954. test.str_val.data = NULL;
  4955. }
  4956. /* Continue if NULL or 0. */
  4957. if (memcmp(&test, &zero, sizeof(test)) == 0) continue;
  4958. /* Continue on empty array or map. */
  4959. if (upb_fielddef_ismap(f)) {
  4960. if (upb_map_size(test.map_val) == 0) continue;
  4961. } else if (upb_fielddef_isseq(f)) {
  4962. if (upb_array_size(test.array_val) == 0) continue;
  4963. }
  4964. }
  4965. *out_val = val;
  4966. *out_f = f;
  4967. *iter = i;
  4968. return true;
  4969. }
  4970. *iter = i;
  4971. return false;
  4972. }
  4973. bool _upb_msg_discardunknown(upb_msg *msg, const upb_msgdef *m, int depth) {
  4974. size_t iter = UPB_MSG_BEGIN;
  4975. const upb_fielddef *f;
  4976. upb_msgval val;
  4977. bool ret = true;
  4978. if (--depth == 0) return false;
  4979. _upb_msg_discardunknown_shallow(msg);
  4980. while (upb_msg_next(msg, m, NULL /*ext_pool*/, &f, &val, &iter)) {
  4981. const upb_msgdef *subm = upb_fielddef_msgsubdef(f);
  4982. if (!subm) continue;
  4983. if (upb_fielddef_ismap(f)) {
  4984. const upb_fielddef *val_f = upb_msgdef_itof(subm, 2);
  4985. const upb_msgdef *val_m = upb_fielddef_msgsubdef(val_f);
  4986. upb_map *map = (upb_map*)val.map_val;
  4987. size_t iter = UPB_MAP_BEGIN;
  4988. if (!val_m) continue;
  4989. while (upb_mapiter_next(map, &iter)) {
  4990. upb_msgval map_val = upb_mapiter_value(map, iter);
  4991. if (!_upb_msg_discardunknown((upb_msg*)map_val.msg_val, val_m, depth)) {
  4992. ret = false;
  4993. }
  4994. }
  4995. } else if (upb_fielddef_isseq(f)) {
  4996. const upb_array *arr = val.array_val;
  4997. size_t i, n = upb_array_size(arr);
  4998. for (i = 0; i < n; i++) {
  4999. upb_msgval elem = upb_array_get(arr, i);
  5000. if (!_upb_msg_discardunknown((upb_msg*)elem.msg_val, subm, depth)) {
  5001. ret = false;
  5002. }
  5003. }
  5004. } else {
  5005. if (!_upb_msg_discardunknown((upb_msg*)val.msg_val, subm, depth)) {
  5006. ret = false;
  5007. }
  5008. }
  5009. }
  5010. return ret;
  5011. }
  5012. bool upb_msg_discardunknown(upb_msg *msg, const upb_msgdef *m, int maxdepth) {
  5013. return _upb_msg_discardunknown(msg, m, maxdepth);
  5014. }
  5015. /** upb_array *****************************************************************/
  5016. upb_array *upb_array_new(upb_arena *a, upb_fieldtype_t type) {
  5017. return _upb_array_new(a, type);
  5018. }
  5019. size_t upb_array_size(const upb_array *arr) {
  5020. return arr->len;
  5021. }
  5022. upb_msgval upb_array_get(const upb_array *arr, size_t i) {
  5023. upb_msgval ret;
  5024. const char* data = _upb_array_constptr(arr);
  5025. int lg2 = arr->data & 7;
  5026. UPB_ASSERT(i < arr->len);
  5027. memcpy(&ret, data + (i << lg2), 1 << lg2);
  5028. return ret;
  5029. }
  5030. void upb_array_set(upb_array *arr, size_t i, upb_msgval val) {
  5031. char* data = _upb_array_ptr(arr);
  5032. int lg2 = arr->data & 7;
  5033. UPB_ASSERT(i < arr->len);
  5034. memcpy(data + (i << lg2), &val, 1 << lg2);
  5035. }
  5036. bool upb_array_append(upb_array *arr, upb_msgval val, upb_arena *arena) {
  5037. if (!_upb_array_realloc(arr, arr->len + 1, arena)) {
  5038. return false;
  5039. }
  5040. arr->len++;
  5041. upb_array_set(arr, arr->len - 1, val);
  5042. return true;
  5043. }
  5044. bool upb_array_resize(upb_array *arr, size_t size, upb_arena *arena) {
  5045. return _upb_array_resize(arr, size, arena);
  5046. }
  5047. /** upb_map *******************************************************************/
  5048. upb_map *upb_map_new(upb_arena *a, upb_fieldtype_t key_type,
  5049. upb_fieldtype_t value_type) {
  5050. return _upb_map_new(a, _upb_fieldtype_to_mapsize[key_type],
  5051. _upb_fieldtype_to_mapsize[value_type]);
  5052. }
  5053. size_t upb_map_size(const upb_map *map) {
  5054. return _upb_map_size(map);
  5055. }
  5056. bool upb_map_get(const upb_map *map, upb_msgval key, upb_msgval *val) {
  5057. return _upb_map_get(map, &key, map->key_size, val, map->val_size);
  5058. }
  5059. bool upb_map_set(upb_map *map, upb_msgval key, upb_msgval val,
  5060. upb_arena *arena) {
  5061. return _upb_map_set(map, &key, map->key_size, &val, map->val_size, arena);
  5062. }
  5063. bool upb_map_delete(upb_map *map, upb_msgval key) {
  5064. return _upb_map_delete(map, &key, map->key_size);
  5065. }
  5066. bool upb_mapiter_next(const upb_map *map, size_t *iter) {
  5067. return _upb_map_next(map, iter);
  5068. }
  5069. bool upb_mapiter_done(const upb_map *map, size_t iter) {
  5070. upb_strtable_iter i;
  5071. UPB_ASSERT(iter != UPB_MAP_BEGIN);
  5072. i.t = &map->table;
  5073. i.index = iter;
  5074. return upb_strtable_done(&i);
  5075. }
  5076. /* Returns the key and value for this entry of the map. */
  5077. upb_msgval upb_mapiter_key(const upb_map *map, size_t iter) {
  5078. upb_strtable_iter i;
  5079. upb_msgval ret;
  5080. i.t = &map->table;
  5081. i.index = iter;
  5082. _upb_map_fromkey(upb_strtable_iter_key(&i), &ret, map->key_size);
  5083. return ret;
  5084. }
  5085. upb_msgval upb_mapiter_value(const upb_map *map, size_t iter) {
  5086. upb_strtable_iter i;
  5087. upb_msgval ret;
  5088. i.t = &map->table;
  5089. i.index = iter;
  5090. _upb_map_fromvalue(upb_strtable_iter_value(&i), &ret, map->val_size);
  5091. return ret;
  5092. }
  5093. /* void upb_mapiter_setvalue(upb_map *map, size_t iter, upb_msgval value); */
  5094. #ifdef UPB_MSVC_VSNPRINTF
  5095. /* Visual C++ earlier than 2015 doesn't have standard C99 snprintf and
  5096. * vsnprintf. To support them, missing functions are manually implemented
  5097. * using the existing secure functions. */
  5098. int msvc_vsnprintf(char* s, size_t n, const char* format, va_list arg) {
  5099. if (!s) {
  5100. return _vscprintf(format, arg);
  5101. }
  5102. int ret = _vsnprintf_s(s, n, _TRUNCATE, format, arg);
  5103. if (ret < 0) {
  5104. ret = _vscprintf(format, arg);
  5105. }
  5106. return ret;
  5107. }
  5108. int msvc_snprintf(char* s, size_t n, const char* format, ...) {
  5109. va_list arg;
  5110. va_start(arg, format);
  5111. int ret = msvc_vsnprintf(s, n, format, arg);
  5112. va_end(arg);
  5113. return ret;
  5114. }
  5115. #endif
  5116. #include <errno.h>
  5117. #include <float.h>
  5118. #include <inttypes.h>
  5119. #include <limits.h>
  5120. #include <setjmp.h>
  5121. #include <stdlib.h>
  5122. #include <string.h>
  5123. /* Special header, must be included last. */
  5124. typedef struct {
  5125. const char *ptr, *end;
  5126. upb_arena *arena; /* TODO: should we have a tmp arena for tmp data? */
  5127. const upb_symtab *any_pool;
  5128. int depth;
  5129. upb_status *status;
  5130. jmp_buf err;
  5131. int line;
  5132. const char *line_begin;
  5133. bool is_first;
  5134. int options;
  5135. const upb_fielddef *debug_field;
  5136. } jsondec;
  5137. enum { JD_OBJECT, JD_ARRAY, JD_STRING, JD_NUMBER, JD_TRUE, JD_FALSE, JD_NULL };
  5138. /* Forward declarations of mutually-recursive functions. */
  5139. static void jsondec_wellknown(jsondec *d, upb_msg *msg, const upb_msgdef *m);
  5140. static upb_msgval jsondec_value(jsondec *d, const upb_fielddef *f);
  5141. static void jsondec_wellknownvalue(jsondec *d, upb_msg *msg,
  5142. const upb_msgdef *m);
  5143. static void jsondec_object(jsondec *d, upb_msg *msg, const upb_msgdef *m);
  5144. static bool jsondec_streql(upb_strview str, const char *lit) {
  5145. return str.size == strlen(lit) && memcmp(str.data, lit, str.size) == 0;
  5146. }
  5147. UPB_NORETURN static void jsondec_err(jsondec *d, const char *msg) {
  5148. upb_status_seterrf(d->status, "Error parsing JSON @%d:%d: %s", d->line,
  5149. (int)(d->ptr - d->line_begin), msg);
  5150. longjmp(d->err, 1);
  5151. }
  5152. UPB_NORETURN static void jsondec_errf(jsondec *d, const char *fmt, ...) {
  5153. va_list argp;
  5154. upb_status_seterrf(d->status, "Error parsing JSON @%d:%d: ", d->line,
  5155. (int)(d->ptr - d->line_begin));
  5156. va_start(argp, fmt);
  5157. upb_status_vappenderrf(d->status, fmt, argp);
  5158. va_end(argp);
  5159. longjmp(d->err, 1);
  5160. }
  5161. static void jsondec_skipws(jsondec *d) {
  5162. while (d->ptr != d->end) {
  5163. switch (*d->ptr) {
  5164. case '\n':
  5165. d->line++;
  5166. d->line_begin = d->ptr;
  5167. /* Fallthrough. */
  5168. case '\r':
  5169. case '\t':
  5170. case ' ':
  5171. d->ptr++;
  5172. break;
  5173. default:
  5174. return;
  5175. }
  5176. }
  5177. jsondec_err(d, "Unexpected EOF");
  5178. }
  5179. static bool jsondec_tryparsech(jsondec *d, char ch) {
  5180. if (d->ptr == d->end || *d->ptr != ch) return false;
  5181. d->ptr++;
  5182. return true;
  5183. }
  5184. static void jsondec_parselit(jsondec *d, const char *lit) {
  5185. size_t avail = d->end - d->ptr;
  5186. size_t len = strlen(lit);
  5187. if (avail < len || memcmp(d->ptr, lit, len) != 0) {
  5188. jsondec_errf(d, "Expected: '%s'", lit);
  5189. }
  5190. d->ptr += len;
  5191. }
  5192. static void jsondec_wsch(jsondec *d, char ch) {
  5193. jsondec_skipws(d);
  5194. if (!jsondec_tryparsech(d, ch)) {
  5195. jsondec_errf(d, "Expected: '%c'", ch);
  5196. }
  5197. }
  5198. static void jsondec_true(jsondec *d) { jsondec_parselit(d, "true"); }
  5199. static void jsondec_false(jsondec *d) { jsondec_parselit(d, "false"); }
  5200. static void jsondec_null(jsondec *d) { jsondec_parselit(d, "null"); }
  5201. static void jsondec_entrysep(jsondec *d) {
  5202. jsondec_skipws(d);
  5203. jsondec_parselit(d, ":");
  5204. }
  5205. static int jsondec_rawpeek(jsondec *d) {
  5206. switch (*d->ptr) {
  5207. case '{':
  5208. return JD_OBJECT;
  5209. case '[':
  5210. return JD_ARRAY;
  5211. case '"':
  5212. return JD_STRING;
  5213. case '-':
  5214. case '0':
  5215. case '1':
  5216. case '2':
  5217. case '3':
  5218. case '4':
  5219. case '5':
  5220. case '6':
  5221. case '7':
  5222. case '8':
  5223. case '9':
  5224. return JD_NUMBER;
  5225. case 't':
  5226. return JD_TRUE;
  5227. case 'f':
  5228. return JD_FALSE;
  5229. case 'n':
  5230. return JD_NULL;
  5231. default:
  5232. jsondec_errf(d, "Unexpected character: '%c'", *d->ptr);
  5233. }
  5234. }
  5235. /* JSON object/array **********************************************************/
  5236. /* These are used like so:
  5237. *
  5238. * jsondec_objstart(d);
  5239. * while (jsondec_objnext(d)) {
  5240. * ...
  5241. * }
  5242. * jsondec_objend(d) */
  5243. static int jsondec_peek(jsondec *d) {
  5244. jsondec_skipws(d);
  5245. return jsondec_rawpeek(d);
  5246. }
  5247. static void jsondec_push(jsondec *d) {
  5248. if (--d->depth < 0) {
  5249. jsondec_err(d, "Recursion limit exceeded");
  5250. }
  5251. d->is_first = true;
  5252. }
  5253. static bool jsondec_seqnext(jsondec *d, char end_ch) {
  5254. bool is_first = d->is_first;
  5255. d->is_first = false;
  5256. jsondec_skipws(d);
  5257. if (*d->ptr == end_ch) return false;
  5258. if (!is_first) jsondec_parselit(d, ",");
  5259. return true;
  5260. }
  5261. static void jsondec_arrstart(jsondec *d) {
  5262. jsondec_push(d);
  5263. jsondec_wsch(d, '[');
  5264. }
  5265. static void jsondec_arrend(jsondec *d) {
  5266. d->depth++;
  5267. jsondec_wsch(d, ']');
  5268. }
  5269. static bool jsondec_arrnext(jsondec *d) {
  5270. return jsondec_seqnext(d, ']');
  5271. }
  5272. static void jsondec_objstart(jsondec *d) {
  5273. jsondec_push(d);
  5274. jsondec_wsch(d, '{');
  5275. }
  5276. static void jsondec_objend(jsondec *d) {
  5277. d->depth++;
  5278. jsondec_wsch(d, '}');
  5279. }
  5280. static bool jsondec_objnext(jsondec *d) {
  5281. if (!jsondec_seqnext(d, '}')) return false;
  5282. if (jsondec_peek(d) != JD_STRING) {
  5283. jsondec_err(d, "Object must start with string");
  5284. }
  5285. return true;
  5286. }
  5287. /* JSON number ****************************************************************/
  5288. static bool jsondec_tryskipdigits(jsondec *d) {
  5289. const char *start = d->ptr;
  5290. while (d->ptr < d->end) {
  5291. if (*d->ptr < '0' || *d->ptr > '9') {
  5292. break;
  5293. }
  5294. d->ptr++;
  5295. }
  5296. return d->ptr != start;
  5297. }
  5298. static void jsondec_skipdigits(jsondec *d) {
  5299. if (!jsondec_tryskipdigits(d)) {
  5300. jsondec_err(d, "Expected one or more digits");
  5301. }
  5302. }
  5303. static double jsondec_number(jsondec *d) {
  5304. const char *start = d->ptr;
  5305. assert(jsondec_rawpeek(d) == JD_NUMBER);
  5306. /* Skip over the syntax of a number, as specified by JSON. */
  5307. if (*d->ptr == '-') d->ptr++;
  5308. if (jsondec_tryparsech(d, '0')) {
  5309. if (jsondec_tryskipdigits(d)) {
  5310. jsondec_err(d, "number cannot have leading zero");
  5311. }
  5312. } else {
  5313. jsondec_skipdigits(d);
  5314. }
  5315. if (d->ptr == d->end) goto parse;
  5316. if (jsondec_tryparsech(d, '.')) {
  5317. jsondec_skipdigits(d);
  5318. }
  5319. if (d->ptr == d->end) goto parse;
  5320. if (*d->ptr == 'e' || *d->ptr == 'E') {
  5321. d->ptr++;
  5322. if (d->ptr == d->end) {
  5323. jsondec_err(d, "Unexpected EOF in number");
  5324. }
  5325. if (*d->ptr == '+' || *d->ptr == '-') {
  5326. d->ptr++;
  5327. }
  5328. jsondec_skipdigits(d);
  5329. }
  5330. parse:
  5331. /* Having verified the syntax of a JSON number, use strtod() to parse
  5332. * (strtod() accepts a superset of JSON syntax). */
  5333. errno = 0;
  5334. {
  5335. char* end;
  5336. double val = strtod(start, &end);
  5337. assert(end == d->ptr);
  5338. /* Currently the min/max-val conformance tests fail if we check this. Does
  5339. * this mean the conformance tests are wrong or strtod() is wrong, or
  5340. * something else? Investigate further. */
  5341. /*
  5342. if (errno == ERANGE) {
  5343. jsondec_err(d, "Number out of range");
  5344. }
  5345. */
  5346. if (val > DBL_MAX || val < -DBL_MAX) {
  5347. jsondec_err(d, "Number out of range");
  5348. }
  5349. return val;
  5350. }
  5351. }
  5352. /* JSON string ****************************************************************/
  5353. static char jsondec_escape(jsondec *d) {
  5354. switch (*d->ptr++) {
  5355. case '"':
  5356. return '\"';
  5357. case '\\':
  5358. return '\\';
  5359. case '/':
  5360. return '/';
  5361. case 'b':
  5362. return '\b';
  5363. case 'f':
  5364. return '\f';
  5365. case 'n':
  5366. return '\n';
  5367. case 'r':
  5368. return '\r';
  5369. case 't':
  5370. return '\t';
  5371. default:
  5372. jsondec_err(d, "Invalid escape char");
  5373. }
  5374. }
  5375. static uint32_t jsondec_codepoint(jsondec *d) {
  5376. uint32_t cp = 0;
  5377. const char *end;
  5378. if (d->end - d->ptr < 4) {
  5379. jsondec_err(d, "EOF inside string");
  5380. }
  5381. end = d->ptr + 4;
  5382. while (d->ptr < end) {
  5383. char ch = *d->ptr++;
  5384. if (ch >= '0' && ch <= '9') {
  5385. ch -= '0';
  5386. } else if (ch >= 'a' && ch <= 'f') {
  5387. ch = ch - 'a' + 10;
  5388. } else if (ch >= 'A' && ch <= 'F') {
  5389. ch = ch - 'A' + 10;
  5390. } else {
  5391. jsondec_err(d, "Invalid hex digit");
  5392. }
  5393. cp = (cp << 4) | ch;
  5394. }
  5395. return cp;
  5396. }
  5397. /* Parses a \uXXXX unicode escape (possibly a surrogate pair). */
  5398. static size_t jsondec_unicode(jsondec *d, char* out) {
  5399. uint32_t cp = jsondec_codepoint(d);
  5400. if (cp >= 0xd800 && cp <= 0xdbff) {
  5401. /* Surrogate pair: two 16-bit codepoints become a 32-bit codepoint. */
  5402. uint32_t high = cp;
  5403. uint32_t low;
  5404. jsondec_parselit(d, "\\u");
  5405. low = jsondec_codepoint(d);
  5406. if (low < 0xdc00 || low > 0xdfff) {
  5407. jsondec_err(d, "Invalid low surrogate");
  5408. }
  5409. cp = (high & 0x3ff) << 10;
  5410. cp |= (low & 0x3ff);
  5411. cp += 0x10000;
  5412. } else if (cp >= 0xdc00 && cp <= 0xdfff) {
  5413. jsondec_err(d, "Unpaired low surrogate");
  5414. }
  5415. /* Write to UTF-8 */
  5416. if (cp <= 0x7f) {
  5417. out[0] = cp;
  5418. return 1;
  5419. } else if (cp <= 0x07FF) {
  5420. out[0] = ((cp >> 6) & 0x1F) | 0xC0;
  5421. out[1] = ((cp >> 0) & 0x3F) | 0x80;
  5422. return 2;
  5423. } else if (cp <= 0xFFFF) {
  5424. out[0] = ((cp >> 12) & 0x0F) | 0xE0;
  5425. out[1] = ((cp >> 6) & 0x3F) | 0x80;
  5426. out[2] = ((cp >> 0) & 0x3F) | 0x80;
  5427. return 3;
  5428. } else if (cp < 0x10FFFF) {
  5429. out[0] = ((cp >> 18) & 0x07) | 0xF0;
  5430. out[1] = ((cp >> 12) & 0x3f) | 0x80;
  5431. out[2] = ((cp >> 6) & 0x3f) | 0x80;
  5432. out[3] = ((cp >> 0) & 0x3f) | 0x80;
  5433. return 4;
  5434. } else {
  5435. jsondec_err(d, "Invalid codepoint");
  5436. }
  5437. }
  5438. static void jsondec_resize(jsondec *d, char **buf, char **end, char **buf_end) {
  5439. size_t oldsize = *buf_end - *buf;
  5440. size_t len = *end - *buf;
  5441. size_t size = UPB_MAX(8, 2 * oldsize);
  5442. *buf = upb_arena_realloc(d->arena, *buf, len, size);
  5443. *end = *buf + len;
  5444. *buf_end = *buf + size;
  5445. }
  5446. static upb_strview jsondec_string(jsondec *d) {
  5447. char *buf = NULL;
  5448. char *end = NULL;
  5449. char *buf_end = NULL;
  5450. jsondec_skipws(d);
  5451. if (*d->ptr++ != '"') {
  5452. jsondec_err(d, "Expected string");
  5453. }
  5454. while (d->ptr < d->end) {
  5455. char ch = *d->ptr++;
  5456. if (end == buf_end) {
  5457. jsondec_resize(d, &buf, &end, &buf_end);
  5458. }
  5459. switch (ch) {
  5460. case '"': {
  5461. upb_strview ret;
  5462. ret.data = buf;
  5463. ret.size = end - buf;
  5464. *end = '\0'; /* Needed for possible strtod(). */
  5465. return ret;
  5466. }
  5467. case '\\':
  5468. if (d->ptr == d->end) goto eof;
  5469. if (*d->ptr == 'u') {
  5470. d->ptr++;
  5471. if (buf_end - end < 4) {
  5472. /* Allow space for maximum-sized code point (4 bytes). */
  5473. jsondec_resize(d, &buf, &end, &buf_end);
  5474. }
  5475. end += jsondec_unicode(d, end);
  5476. } else {
  5477. *end++ = jsondec_escape(d);
  5478. }
  5479. break;
  5480. default:
  5481. if ((unsigned char)*d->ptr < 0x20) {
  5482. jsondec_err(d, "Invalid char in JSON string");
  5483. }
  5484. *end++ = ch;
  5485. break;
  5486. }
  5487. }
  5488. eof:
  5489. jsondec_err(d, "EOF inside string");
  5490. }
  5491. static void jsondec_skipval(jsondec *d) {
  5492. switch (jsondec_peek(d)) {
  5493. case JD_OBJECT:
  5494. jsondec_objstart(d);
  5495. while (jsondec_objnext(d)) {
  5496. jsondec_string(d);
  5497. jsondec_entrysep(d);
  5498. jsondec_skipval(d);
  5499. }
  5500. jsondec_objend(d);
  5501. break;
  5502. case JD_ARRAY:
  5503. jsondec_arrstart(d);
  5504. while (jsondec_arrnext(d)) {
  5505. jsondec_skipval(d);
  5506. }
  5507. jsondec_arrend(d);
  5508. break;
  5509. case JD_TRUE:
  5510. jsondec_true(d);
  5511. break;
  5512. case JD_FALSE:
  5513. jsondec_false(d);
  5514. break;
  5515. case JD_NULL:
  5516. jsondec_null(d);
  5517. break;
  5518. case JD_STRING:
  5519. jsondec_string(d);
  5520. break;
  5521. case JD_NUMBER:
  5522. jsondec_number(d);
  5523. break;
  5524. }
  5525. }
  5526. /* Base64 decoding for bytes fields. ******************************************/
  5527. static unsigned int jsondec_base64_tablelookup(const char ch) {
  5528. /* Table includes the normal base64 chars plus the URL-safe variant. */
  5529. const signed char table[256] = {
  5530. -1, -1, -1, -1, -1, -1, -1,
  5531. -1, -1, -1, -1, -1, -1, -1,
  5532. -1, -1, -1, -1, -1, -1, -1,
  5533. -1, -1, -1, -1, -1, -1, -1,
  5534. -1, -1, -1, -1, -1, -1, -1,
  5535. -1, -1, -1, -1, -1, -1, -1,
  5536. -1, 62 /*+*/, -1, 62 /*-*/, -1, 63 /*/ */, 52 /*0*/,
  5537. 53 /*1*/, 54 /*2*/, 55 /*3*/, 56 /*4*/, 57 /*5*/, 58 /*6*/, 59 /*7*/,
  5538. 60 /*8*/, 61 /*9*/, -1, -1, -1, -1, -1,
  5539. -1, -1, 0 /*A*/, 1 /*B*/, 2 /*C*/, 3 /*D*/, 4 /*E*/,
  5540. 5 /*F*/, 6 /*G*/, 07 /*H*/, 8 /*I*/, 9 /*J*/, 10 /*K*/, 11 /*L*/,
  5541. 12 /*M*/, 13 /*N*/, 14 /*O*/, 15 /*P*/, 16 /*Q*/, 17 /*R*/, 18 /*S*/,
  5542. 19 /*T*/, 20 /*U*/, 21 /*V*/, 22 /*W*/, 23 /*X*/, 24 /*Y*/, 25 /*Z*/,
  5543. -1, -1, -1, -1, 63 /*_*/, -1, 26 /*a*/,
  5544. 27 /*b*/, 28 /*c*/, 29 /*d*/, 30 /*e*/, 31 /*f*/, 32 /*g*/, 33 /*h*/,
  5545. 34 /*i*/, 35 /*j*/, 36 /*k*/, 37 /*l*/, 38 /*m*/, 39 /*n*/, 40 /*o*/,
  5546. 41 /*p*/, 42 /*q*/, 43 /*r*/, 44 /*s*/, 45 /*t*/, 46 /*u*/, 47 /*v*/,
  5547. 48 /*w*/, 49 /*x*/, 50 /*y*/, 51 /*z*/, -1, -1, -1,
  5548. -1, -1, -1, -1, -1, -1, -1,
  5549. -1, -1, -1, -1, -1, -1, -1,
  5550. -1, -1, -1, -1, -1, -1, -1,
  5551. -1, -1, -1, -1, -1, -1, -1,
  5552. -1, -1, -1, -1, -1, -1, -1,
  5553. -1, -1, -1, -1, -1, -1, -1,
  5554. -1, -1, -1, -1, -1, -1, -1,
  5555. -1, -1, -1, -1, -1, -1, -1,
  5556. -1, -1, -1, -1, -1, -1, -1,
  5557. -1, -1, -1, -1, -1, -1, -1,
  5558. -1, -1, -1, -1, -1, -1, -1,
  5559. -1, -1, -1, -1, -1, -1, -1,
  5560. -1, -1, -1, -1, -1, -1, -1,
  5561. -1, -1, -1, -1, -1, -1, -1,
  5562. -1, -1, -1, -1, -1, -1, -1,
  5563. -1, -1, -1, -1, -1, -1, -1,
  5564. -1, -1, -1, -1, -1, -1, -1,
  5565. -1, -1, -1, -1, -1, -1, -1,
  5566. -1, -1, -1, -1};
  5567. /* Sign-extend return value so high bit will be set on any unexpected char. */
  5568. return table[(unsigned)ch];
  5569. }
  5570. static char *jsondec_partialbase64(jsondec *d, const char *ptr, const char *end,
  5571. char *out) {
  5572. int32_t val = -1;
  5573. switch (end - ptr) {
  5574. case 2:
  5575. val = jsondec_base64_tablelookup(ptr[0]) << 18 |
  5576. jsondec_base64_tablelookup(ptr[1]) << 12;
  5577. out[0] = val >> 16;
  5578. out += 1;
  5579. break;
  5580. case 3:
  5581. val = jsondec_base64_tablelookup(ptr[0]) << 18 |
  5582. jsondec_base64_tablelookup(ptr[1]) << 12 |
  5583. jsondec_base64_tablelookup(ptr[2]) << 6;
  5584. out[0] = val >> 16;
  5585. out[1] = (val >> 8) & 0xff;
  5586. out += 2;
  5587. break;
  5588. }
  5589. if (val < 0) {
  5590. jsondec_err(d, "Corrupt base64");
  5591. }
  5592. return out;
  5593. }
  5594. static size_t jsondec_base64(jsondec *d, upb_strview str) {
  5595. /* We decode in place. This is safe because this is a new buffer (not
  5596. * aliasing the input) and because base64 decoding shrinks 4 bytes into 3. */
  5597. char *out = (char*)str.data;
  5598. const char *ptr = str.data;
  5599. const char *end = ptr + str.size;
  5600. const char *end4 = ptr + (str.size & -4); /* Round down to multiple of 4. */
  5601. for (; ptr < end4; ptr += 4, out += 3) {
  5602. int val = jsondec_base64_tablelookup(ptr[0]) << 18 |
  5603. jsondec_base64_tablelookup(ptr[1]) << 12 |
  5604. jsondec_base64_tablelookup(ptr[2]) << 6 |
  5605. jsondec_base64_tablelookup(ptr[3]) << 0;
  5606. if (val < 0) {
  5607. /* Junk chars or padding. Remove trailing padding, if any. */
  5608. if (end - ptr == 4 && ptr[3] == '=') {
  5609. if (ptr[2] == '=') {
  5610. end -= 2;
  5611. } else {
  5612. end -= 1;
  5613. }
  5614. }
  5615. break;
  5616. }
  5617. out[0] = val >> 16;
  5618. out[1] = (val >> 8) & 0xff;
  5619. out[2] = val & 0xff;
  5620. }
  5621. if (ptr < end) {
  5622. /* Process remaining chars. We do not require padding. */
  5623. out = jsondec_partialbase64(d, ptr, end, out);
  5624. }
  5625. return out - str.data;
  5626. }
  5627. /* Low-level integer parsing **************************************************/
  5628. /* We use these hand-written routines instead of strto[u]l() because the "long
  5629. * long" variants aren't in c89. Also our version allows setting a ptr limit. */
  5630. static const char *jsondec_buftouint64(jsondec *d, const char *ptr,
  5631. const char *end, uint64_t *val) {
  5632. uint64_t u64 = 0;
  5633. while (ptr < end) {
  5634. unsigned ch = *ptr - '0';
  5635. if (ch >= 10) break;
  5636. if (u64 > UINT64_MAX / 10 || u64 * 10 > UINT64_MAX - ch) {
  5637. jsondec_err(d, "Integer overflow");
  5638. }
  5639. u64 *= 10;
  5640. u64 += ch;
  5641. ptr++;
  5642. }
  5643. *val = u64;
  5644. return ptr;
  5645. }
  5646. static const char *jsondec_buftoint64(jsondec *d, const char *ptr,
  5647. const char *end, int64_t *val) {
  5648. bool neg = false;
  5649. uint64_t u64;
  5650. if (ptr != end && *ptr == '-') {
  5651. ptr++;
  5652. neg = true;
  5653. }
  5654. ptr = jsondec_buftouint64(d, ptr, end, &u64);
  5655. if (u64 > (uint64_t)INT64_MAX + neg) {
  5656. jsondec_err(d, "Integer overflow");
  5657. }
  5658. *val = neg ? -u64 : u64;
  5659. return ptr;
  5660. }
  5661. static uint64_t jsondec_strtouint64(jsondec *d, upb_strview str) {
  5662. const char *end = str.data + str.size;
  5663. uint64_t ret;
  5664. if (jsondec_buftouint64(d, str.data, end, &ret) != end) {
  5665. jsondec_err(d, "Non-number characters in quoted integer");
  5666. }
  5667. return ret;
  5668. }
  5669. static int64_t jsondec_strtoint64(jsondec *d, upb_strview str) {
  5670. const char *end = str.data + str.size;
  5671. int64_t ret;
  5672. if (jsondec_buftoint64(d, str.data, end, &ret) != end) {
  5673. jsondec_err(d, "Non-number characters in quoted integer");
  5674. }
  5675. return ret;
  5676. }
  5677. /* Primitive value types ******************************************************/
  5678. /* Parse INT32 or INT64 value. */
  5679. static upb_msgval jsondec_int(jsondec *d, const upb_fielddef *f) {
  5680. upb_msgval val;
  5681. switch (jsondec_peek(d)) {
  5682. case JD_NUMBER: {
  5683. double dbl = jsondec_number(d);
  5684. if (dbl > 9223372036854774784.0 || dbl < -9223372036854775808.0) {
  5685. jsondec_err(d, "JSON number is out of range.");
  5686. }
  5687. val.int64_val = dbl; /* must be guarded, overflow here is UB */
  5688. if (val.int64_val != dbl) {
  5689. jsondec_errf(d, "JSON number was not integral (%d != %" PRId64 ")", dbl,
  5690. val.int64_val);
  5691. }
  5692. break;
  5693. }
  5694. case JD_STRING: {
  5695. upb_strview str = jsondec_string(d);
  5696. val.int64_val = jsondec_strtoint64(d, str);
  5697. break;
  5698. }
  5699. default:
  5700. jsondec_err(d, "Expected number or string");
  5701. }
  5702. if (upb_fielddef_type(f) == UPB_TYPE_INT32) {
  5703. if (val.int64_val > INT32_MAX || val.int64_val < INT32_MIN) {
  5704. jsondec_err(d, "Integer out of range.");
  5705. }
  5706. val.int32_val = (int32_t)val.int64_val;
  5707. }
  5708. return val;
  5709. }
  5710. /* Parse UINT32 or UINT64 value. */
  5711. static upb_msgval jsondec_uint(jsondec *d, const upb_fielddef *f) {
  5712. upb_msgval val;
  5713. switch (jsondec_peek(d)) {
  5714. case JD_NUMBER: {
  5715. double dbl = jsondec_number(d);
  5716. if (dbl > 18446744073709549568.0 || dbl < 0) {
  5717. jsondec_err(d, "JSON number is out of range.");
  5718. }
  5719. val.uint64_val = dbl; /* must be guarded, overflow here is UB */
  5720. if (val.uint64_val != dbl) {
  5721. jsondec_errf(d, "JSON number was not integral (%d != %" PRIu64 ")", dbl,
  5722. val.uint64_val);
  5723. }
  5724. break;
  5725. }
  5726. case JD_STRING: {
  5727. upb_strview str = jsondec_string(d);
  5728. val.uint64_val = jsondec_strtouint64(d, str);
  5729. break;
  5730. }
  5731. default:
  5732. jsondec_err(d, "Expected number or string");
  5733. }
  5734. if (upb_fielddef_type(f) == UPB_TYPE_UINT32) {
  5735. if (val.uint64_val > UINT32_MAX) {
  5736. jsondec_err(d, "Integer out of range.");
  5737. }
  5738. val.uint32_val = (uint32_t)val.uint64_val;
  5739. }
  5740. return val;
  5741. }
  5742. /* Parse DOUBLE or FLOAT value. */
  5743. static upb_msgval jsondec_double(jsondec *d, const upb_fielddef *f) {
  5744. upb_strview str;
  5745. upb_msgval val;
  5746. switch (jsondec_peek(d)) {
  5747. case JD_NUMBER:
  5748. val.double_val = jsondec_number(d);
  5749. break;
  5750. case JD_STRING:
  5751. str = jsondec_string(d);
  5752. if (jsondec_streql(str, "NaN")) {
  5753. val.double_val = UPB_NAN;
  5754. } else if (jsondec_streql(str, "Infinity")) {
  5755. val.double_val = UPB_INFINITY;
  5756. } else if (jsondec_streql(str, "-Infinity")) {
  5757. val.double_val = -UPB_INFINITY;
  5758. } else {
  5759. val.double_val = strtod(str.data, NULL);
  5760. }
  5761. break;
  5762. default:
  5763. jsondec_err(d, "Expected number or string");
  5764. }
  5765. if (upb_fielddef_type(f) == UPB_TYPE_FLOAT) {
  5766. if (val.double_val != UPB_INFINITY && val.double_val != -UPB_INFINITY &&
  5767. (val.double_val > FLT_MAX || val.double_val < -FLT_MAX)) {
  5768. jsondec_err(d, "Float out of range");
  5769. }
  5770. val.float_val = val.double_val;
  5771. }
  5772. return val;
  5773. }
  5774. /* Parse STRING or BYTES value. */
  5775. static upb_msgval jsondec_strfield(jsondec *d, const upb_fielddef *f) {
  5776. upb_msgval val;
  5777. val.str_val = jsondec_string(d);
  5778. if (upb_fielddef_type(f) == UPB_TYPE_BYTES) {
  5779. val.str_val.size = jsondec_base64(d, val.str_val);
  5780. }
  5781. return val;
  5782. }
  5783. static upb_msgval jsondec_enum(jsondec *d, const upb_fielddef *f) {
  5784. if (jsondec_peek(d) == JD_STRING) {
  5785. const upb_enumdef *e = upb_fielddef_enumsubdef(f);
  5786. upb_strview str = jsondec_string(d);
  5787. upb_msgval val;
  5788. if (!upb_enumdef_ntoi(e, str.data, str.size, &val.int32_val)) {
  5789. if (d->options & UPB_JSONDEC_IGNOREUNKNOWN) {
  5790. val.int32_val = 0;
  5791. } else {
  5792. jsondec_errf(d, "Unknown enumerator: '" UPB_STRVIEW_FORMAT "'",
  5793. UPB_STRVIEW_ARGS(str));
  5794. }
  5795. }
  5796. return val;
  5797. } else {
  5798. return jsondec_int(d, f);
  5799. }
  5800. }
  5801. static upb_msgval jsondec_bool(jsondec *d, const upb_fielddef *f) {
  5802. bool is_map_key = upb_fielddef_number(f) == 1 &&
  5803. upb_msgdef_mapentry(upb_fielddef_containingtype(f));
  5804. upb_msgval val;
  5805. if (is_map_key) {
  5806. upb_strview str = jsondec_string(d);
  5807. if (jsondec_streql(str, "true")) {
  5808. val.bool_val = true;
  5809. } else if (jsondec_streql(str, "false")) {
  5810. val.bool_val = false;
  5811. } else {
  5812. jsondec_err(d, "Invalid boolean map key");
  5813. }
  5814. } else {
  5815. switch (jsondec_peek(d)) {
  5816. case JD_TRUE:
  5817. val.bool_val = true;
  5818. jsondec_true(d);
  5819. break;
  5820. case JD_FALSE:
  5821. val.bool_val = false;
  5822. jsondec_false(d);
  5823. break;
  5824. default:
  5825. jsondec_err(d, "Expected true or false");
  5826. }
  5827. }
  5828. return val;
  5829. }
  5830. /* Composite types (array/message/map) ****************************************/
  5831. static void jsondec_array(jsondec *d, upb_msg *msg, const upb_fielddef *f) {
  5832. upb_array *arr = upb_msg_mutable(msg, f, d->arena).array;
  5833. jsondec_arrstart(d);
  5834. while (jsondec_arrnext(d)) {
  5835. upb_msgval elem = jsondec_value(d, f);
  5836. upb_array_append(arr, elem, d->arena);
  5837. }
  5838. jsondec_arrend(d);
  5839. }
  5840. static void jsondec_map(jsondec *d, upb_msg *msg, const upb_fielddef *f) {
  5841. upb_map *map = upb_msg_mutable(msg, f, d->arena).map;
  5842. const upb_msgdef *entry = upb_fielddef_msgsubdef(f);
  5843. const upb_fielddef *key_f = upb_msgdef_itof(entry, 1);
  5844. const upb_fielddef *val_f = upb_msgdef_itof(entry, 2);
  5845. jsondec_objstart(d);
  5846. while (jsondec_objnext(d)) {
  5847. upb_msgval key, val;
  5848. key = jsondec_value(d, key_f);
  5849. jsondec_entrysep(d);
  5850. val = jsondec_value(d, val_f);
  5851. upb_map_set(map, key, val, d->arena);
  5852. }
  5853. jsondec_objend(d);
  5854. }
  5855. static void jsondec_tomsg(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  5856. if (upb_msgdef_wellknowntype(m) == UPB_WELLKNOWN_UNSPECIFIED) {
  5857. jsondec_object(d, msg, m);
  5858. } else {
  5859. jsondec_wellknown(d, msg, m);
  5860. }
  5861. }
  5862. static upb_msgval jsondec_msg(jsondec *d, const upb_fielddef *f) {
  5863. const upb_msgdef *m = upb_fielddef_msgsubdef(f);
  5864. upb_msg *msg = upb_msg_new(m, d->arena);
  5865. upb_msgval val;
  5866. jsondec_tomsg(d, msg, m);
  5867. val.msg_val = msg;
  5868. return val;
  5869. }
  5870. static bool jsondec_isvalue(const upb_fielddef *f) {
  5871. return upb_fielddef_type(f) == UPB_TYPE_MESSAGE &&
  5872. upb_msgdef_wellknowntype(upb_fielddef_msgsubdef(f)) ==
  5873. UPB_WELLKNOWN_VALUE;
  5874. }
  5875. static void jsondec_field(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  5876. upb_strview name;
  5877. const upb_fielddef *f;
  5878. const upb_fielddef *preserved;
  5879. name = jsondec_string(d);
  5880. jsondec_entrysep(d);
  5881. f = upb_msgdef_lookupjsonname(m, name.data, name.size);
  5882. if (!f) {
  5883. if ((d->options & UPB_JSONDEC_IGNOREUNKNOWN) == 0) {
  5884. jsondec_errf(d, "Unknown field: '" UPB_STRVIEW_FORMAT "'",
  5885. UPB_STRVIEW_ARGS(name));
  5886. }
  5887. jsondec_skipval(d);
  5888. return;
  5889. }
  5890. if (upb_fielddef_realcontainingoneof(f) &&
  5891. upb_msg_whichoneof(msg, upb_fielddef_containingoneof(f))) {
  5892. jsondec_err(d, "More than one field for this oneof.");
  5893. }
  5894. if (jsondec_peek(d) == JD_NULL && !jsondec_isvalue(f)) {
  5895. /* JSON "null" indicates a default value, so no need to set anything. */
  5896. jsondec_null(d);
  5897. return;
  5898. }
  5899. preserved = d->debug_field;
  5900. d->debug_field = f;
  5901. if (upb_fielddef_ismap(f)) {
  5902. jsondec_map(d, msg, f);
  5903. } else if (upb_fielddef_isseq(f)) {
  5904. jsondec_array(d, msg, f);
  5905. } else if (upb_fielddef_issubmsg(f)) {
  5906. upb_msg *submsg = upb_msg_mutable(msg, f, d->arena).msg;
  5907. const upb_msgdef *subm = upb_fielddef_msgsubdef(f);
  5908. jsondec_tomsg(d, submsg, subm);
  5909. } else {
  5910. upb_msgval val = jsondec_value(d, f);
  5911. upb_msg_set(msg, f, val, d->arena);
  5912. }
  5913. d->debug_field = preserved;
  5914. }
  5915. static void jsondec_object(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  5916. jsondec_objstart(d);
  5917. while (jsondec_objnext(d)) {
  5918. jsondec_field(d, msg, m);
  5919. }
  5920. jsondec_objend(d);
  5921. }
  5922. static upb_msgval jsondec_value(jsondec *d, const upb_fielddef *f) {
  5923. switch (upb_fielddef_type(f)) {
  5924. case UPB_TYPE_BOOL:
  5925. return jsondec_bool(d, f);
  5926. case UPB_TYPE_FLOAT:
  5927. case UPB_TYPE_DOUBLE:
  5928. return jsondec_double(d, f);
  5929. case UPB_TYPE_UINT32:
  5930. case UPB_TYPE_UINT64:
  5931. return jsondec_uint(d, f);
  5932. case UPB_TYPE_INT32:
  5933. case UPB_TYPE_INT64:
  5934. return jsondec_int(d, f);
  5935. case UPB_TYPE_STRING:
  5936. case UPB_TYPE_BYTES:
  5937. return jsondec_strfield(d, f);
  5938. case UPB_TYPE_ENUM:
  5939. return jsondec_enum(d, f);
  5940. case UPB_TYPE_MESSAGE:
  5941. return jsondec_msg(d, f);
  5942. default:
  5943. UPB_UNREACHABLE();
  5944. }
  5945. }
  5946. /* Well-known types ***********************************************************/
  5947. static int jsondec_tsdigits(jsondec *d, const char **ptr, size_t digits,
  5948. const char *after) {
  5949. uint64_t val;
  5950. const char *p = *ptr;
  5951. const char *end = p + digits;
  5952. size_t after_len = after ? strlen(after) : 0;
  5953. UPB_ASSERT(digits <= 9); /* int can't overflow. */
  5954. if (jsondec_buftouint64(d, p, end, &val) != end ||
  5955. (after_len && memcmp(end, after, after_len) != 0)) {
  5956. jsondec_err(d, "Malformed timestamp");
  5957. }
  5958. UPB_ASSERT(val < INT_MAX);
  5959. *ptr = end + after_len;
  5960. return (int)val;
  5961. }
  5962. static int jsondec_nanos(jsondec *d, const char **ptr, const char *end) {
  5963. uint64_t nanos = 0;
  5964. const char *p = *ptr;
  5965. if (p != end && *p == '.') {
  5966. const char *nano_end = jsondec_buftouint64(d, p + 1, end, &nanos);
  5967. int digits = (int)(nano_end - p - 1);
  5968. int exp_lg10 = 9 - digits;
  5969. if (digits > 9) {
  5970. jsondec_err(d, "Too many digits for partial seconds");
  5971. }
  5972. while (exp_lg10--) nanos *= 10;
  5973. *ptr = nano_end;
  5974. }
  5975. UPB_ASSERT(nanos < INT_MAX);
  5976. return (int)nanos;
  5977. }
  5978. /* jsondec_epochdays(1970, 1, 1) == 1970-01-01 == 0. */
  5979. int jsondec_epochdays(int y, int m, int d) {
  5980. const uint32_t year_base = 4800; /* Before min year, multiple of 400. */
  5981. const uint32_t m_adj = m - 3; /* March-based month. */
  5982. const uint32_t carry = m_adj > (uint32_t)m ? 1 : 0;
  5983. const uint32_t adjust = carry ? 12 : 0;
  5984. const uint32_t y_adj = y + year_base - carry;
  5985. const uint32_t month_days = ((m_adj + adjust) * 62719 + 769) / 2048;
  5986. const uint32_t leap_days = y_adj / 4 - y_adj / 100 + y_adj / 400;
  5987. return y_adj * 365 + leap_days + month_days + (d - 1) - 2472632;
  5988. }
  5989. static int64_t jsondec_unixtime(int y, int m, int d, int h, int min, int s) {
  5990. return (int64_t)jsondec_epochdays(y, m, d) * 86400 + h * 3600 + min * 60 + s;
  5991. }
  5992. static void jsondec_timestamp(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  5993. upb_msgval seconds;
  5994. upb_msgval nanos;
  5995. upb_strview str = jsondec_string(d);
  5996. const char *ptr = str.data;
  5997. const char *end = ptr + str.size;
  5998. if (str.size < 20) goto malformed;
  5999. {
  6000. /* 1972-01-01T01:00:00 */
  6001. int year = jsondec_tsdigits(d, &ptr, 4, "-");
  6002. int mon = jsondec_tsdigits(d, &ptr, 2, "-");
  6003. int day = jsondec_tsdigits(d, &ptr, 2, "T");
  6004. int hour = jsondec_tsdigits(d, &ptr, 2, ":");
  6005. int min = jsondec_tsdigits(d, &ptr, 2, ":");
  6006. int sec = jsondec_tsdigits(d, &ptr, 2, NULL);
  6007. seconds.int64_val = jsondec_unixtime(year, mon, day, hour, min, sec);
  6008. }
  6009. nanos.int32_val = jsondec_nanos(d, &ptr, end);
  6010. {
  6011. /* [+-]08:00 or Z */
  6012. int ofs = 0;
  6013. bool neg = false;
  6014. if (ptr == end) goto malformed;
  6015. switch (*ptr++) {
  6016. case '-':
  6017. neg = true;
  6018. /* fallthrough */
  6019. case '+':
  6020. if ((end - ptr) != 5) goto malformed;
  6021. ofs = jsondec_tsdigits(d, &ptr, 2, ":00");
  6022. ofs *= 60 * 60;
  6023. seconds.int64_val += (neg ? ofs : -ofs);
  6024. break;
  6025. case 'Z':
  6026. if (ptr != end) goto malformed;
  6027. break;
  6028. default:
  6029. goto malformed;
  6030. }
  6031. }
  6032. if (seconds.int64_val < -62135596800) {
  6033. jsondec_err(d, "Timestamp out of range");
  6034. }
  6035. upb_msg_set(msg, upb_msgdef_itof(m, 1), seconds, d->arena);
  6036. upb_msg_set(msg, upb_msgdef_itof(m, 2), nanos, d->arena);
  6037. return;
  6038. malformed:
  6039. jsondec_err(d, "Malformed timestamp");
  6040. }
  6041. static void jsondec_duration(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6042. upb_msgval seconds;
  6043. upb_msgval nanos;
  6044. upb_strview str = jsondec_string(d);
  6045. const char *ptr = str.data;
  6046. const char *end = ptr + str.size;
  6047. /* "3.000000001s", "3s", etc. */
  6048. ptr = jsondec_buftoint64(d, ptr, end, &seconds.int64_val);
  6049. nanos.int32_val = jsondec_nanos(d, &ptr, end);
  6050. if (end - ptr != 1 || *ptr != 's') {
  6051. jsondec_err(d, "Malformed duration");
  6052. }
  6053. if (seconds.int64_val < -315576000000LL || seconds.int64_val > 315576000000LL) {
  6054. jsondec_err(d, "Duration out of range");
  6055. }
  6056. if (seconds.int64_val < 0) {
  6057. nanos.int32_val = - nanos.int32_val;
  6058. }
  6059. upb_msg_set(msg, upb_msgdef_itof(m, 1), seconds, d->arena);
  6060. upb_msg_set(msg, upb_msgdef_itof(m, 2), nanos, d->arena);
  6061. }
  6062. static void jsondec_listvalue(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6063. const upb_fielddef *values_f = upb_msgdef_itof(m, 1);
  6064. const upb_msgdef *value_m = upb_fielddef_msgsubdef(values_f);
  6065. upb_array *values = upb_msg_mutable(msg, values_f, d->arena).array;
  6066. jsondec_arrstart(d);
  6067. while (jsondec_arrnext(d)) {
  6068. upb_msg *value_msg = upb_msg_new(value_m, d->arena);
  6069. upb_msgval value;
  6070. value.msg_val = value_msg;
  6071. upb_array_append(values, value, d->arena);
  6072. jsondec_wellknownvalue(d, value_msg, value_m);
  6073. }
  6074. jsondec_arrend(d);
  6075. }
  6076. static void jsondec_struct(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6077. const upb_fielddef *fields_f = upb_msgdef_itof(m, 1);
  6078. const upb_msgdef *entry_m = upb_fielddef_msgsubdef(fields_f);
  6079. const upb_fielddef *value_f = upb_msgdef_itof(entry_m, 2);
  6080. const upb_msgdef *value_m = upb_fielddef_msgsubdef(value_f);
  6081. upb_map *fields = upb_msg_mutable(msg, fields_f, d->arena).map;
  6082. jsondec_objstart(d);
  6083. while (jsondec_objnext(d)) {
  6084. upb_msgval key, value;
  6085. upb_msg *value_msg = upb_msg_new(value_m, d->arena);
  6086. key.str_val = jsondec_string(d);
  6087. value.msg_val = value_msg;
  6088. upb_map_set(fields, key, value, d->arena);
  6089. jsondec_entrysep(d);
  6090. jsondec_wellknownvalue(d, value_msg, value_m);
  6091. }
  6092. jsondec_objend(d);
  6093. }
  6094. static void jsondec_wellknownvalue(jsondec *d, upb_msg *msg,
  6095. const upb_msgdef *m) {
  6096. upb_msgval val;
  6097. const upb_fielddef *f;
  6098. upb_msg *submsg;
  6099. switch (jsondec_peek(d)) {
  6100. case JD_NUMBER:
  6101. /* double number_value = 2; */
  6102. f = upb_msgdef_itof(m, 2);
  6103. val.double_val = jsondec_number(d);
  6104. break;
  6105. case JD_STRING:
  6106. /* string string_value = 3; */
  6107. f = upb_msgdef_itof(m, 3);
  6108. val.str_val = jsondec_string(d);
  6109. break;
  6110. case JD_FALSE:
  6111. /* bool bool_value = 4; */
  6112. f = upb_msgdef_itof(m, 4);
  6113. val.bool_val = false;
  6114. jsondec_false(d);
  6115. break;
  6116. case JD_TRUE:
  6117. /* bool bool_value = 4; */
  6118. f = upb_msgdef_itof(m, 4);
  6119. val.bool_val = true;
  6120. jsondec_true(d);
  6121. break;
  6122. case JD_NULL:
  6123. /* NullValue null_value = 1; */
  6124. f = upb_msgdef_itof(m, 1);
  6125. val.int32_val = 0;
  6126. jsondec_null(d);
  6127. break;
  6128. /* Note: these cases return, because upb_msg_mutable() is enough. */
  6129. case JD_OBJECT:
  6130. /* Struct struct_value = 5; */
  6131. f = upb_msgdef_itof(m, 5);
  6132. submsg = upb_msg_mutable(msg, f, d->arena).msg;
  6133. jsondec_struct(d, submsg, upb_fielddef_msgsubdef(f));
  6134. return;
  6135. case JD_ARRAY:
  6136. /* ListValue list_value = 6; */
  6137. f = upb_msgdef_itof(m, 6);
  6138. submsg = upb_msg_mutable(msg, f, d->arena).msg;
  6139. jsondec_listvalue(d, submsg, upb_fielddef_msgsubdef(f));
  6140. return;
  6141. default:
  6142. UPB_UNREACHABLE();
  6143. }
  6144. upb_msg_set(msg, f, val, d->arena);
  6145. }
  6146. static upb_strview jsondec_mask(jsondec *d, const char *buf, const char *end) {
  6147. /* FieldMask fields grow due to inserted '_' characters, so we can't do the
  6148. * transform in place. */
  6149. const char *ptr = buf;
  6150. upb_strview ret;
  6151. char *out;
  6152. ret.size = end - ptr;
  6153. while (ptr < end) {
  6154. ret.size += (*ptr >= 'A' && *ptr <= 'Z');
  6155. ptr++;
  6156. }
  6157. out = upb_arena_malloc(d->arena, ret.size);
  6158. ptr = buf;
  6159. ret.data = out;
  6160. while (ptr < end) {
  6161. char ch = *ptr++;
  6162. if (ch >= 'A' && ch <= 'Z') {
  6163. *out++ = '_';
  6164. *out++ = ch + 32;
  6165. } else if (ch == '_') {
  6166. jsondec_err(d, "field mask may not contain '_'");
  6167. } else {
  6168. *out++ = ch;
  6169. }
  6170. }
  6171. return ret;
  6172. }
  6173. static void jsondec_fieldmask(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6174. /* repeated string paths = 1; */
  6175. const upb_fielddef *paths_f = upb_msgdef_itof(m, 1);
  6176. upb_array *arr = upb_msg_mutable(msg, paths_f, d->arena).array;
  6177. upb_strview str = jsondec_string(d);
  6178. const char *ptr = str.data;
  6179. const char *end = ptr + str.size;
  6180. upb_msgval val;
  6181. while (ptr < end) {
  6182. const char *elem_end = memchr(ptr, ',', end - ptr);
  6183. if (elem_end) {
  6184. val.str_val = jsondec_mask(d, ptr, elem_end);
  6185. ptr = elem_end + 1;
  6186. } else {
  6187. val.str_val = jsondec_mask(d, ptr, end);
  6188. ptr = end;
  6189. }
  6190. upb_array_append(arr, val, d->arena);
  6191. }
  6192. }
  6193. static void jsondec_anyfield(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6194. if (upb_msgdef_wellknowntype(m) == UPB_WELLKNOWN_UNSPECIFIED) {
  6195. /* For regular types: {"@type": "[user type]", "f1": <V1>, "f2": <V2>}
  6196. * where f1, f2, etc. are the normal fields of this type. */
  6197. jsondec_field(d, msg, m);
  6198. } else {
  6199. /* For well-known types: {"@type": "[well-known type]", "value": <X>}
  6200. * where <X> is whatever encoding the WKT normally uses. */
  6201. upb_strview str = jsondec_string(d);
  6202. jsondec_entrysep(d);
  6203. if (!jsondec_streql(str, "value")) {
  6204. jsondec_err(d, "Key for well-known type must be 'value'");
  6205. }
  6206. jsondec_wellknown(d, msg, m);
  6207. }
  6208. }
  6209. static const upb_msgdef *jsondec_typeurl(jsondec *d, upb_msg *msg,
  6210. const upb_msgdef *m) {
  6211. const upb_fielddef *type_url_f = upb_msgdef_itof(m, 1);
  6212. const upb_msgdef *type_m;
  6213. upb_strview type_url = jsondec_string(d);
  6214. const char *end = type_url.data + type_url.size;
  6215. const char *ptr = end;
  6216. upb_msgval val;
  6217. val.str_val = type_url;
  6218. upb_msg_set(msg, type_url_f, val, d->arena);
  6219. /* Find message name after the last '/' */
  6220. while (ptr > type_url.data && *--ptr != '/') {}
  6221. if (ptr == type_url.data || ptr == end) {
  6222. jsondec_err(d, "Type url must have at least one '/' and non-empty host");
  6223. }
  6224. ptr++;
  6225. type_m = upb_symtab_lookupmsg2(d->any_pool, ptr, end - ptr);
  6226. if (!type_m) {
  6227. jsondec_err(d, "Type was not found");
  6228. }
  6229. return type_m;
  6230. }
  6231. static void jsondec_any(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6232. /* string type_url = 1;
  6233. * bytes value = 2; */
  6234. const upb_fielddef *value_f = upb_msgdef_itof(m, 2);
  6235. upb_msg *any_msg;
  6236. const upb_msgdef *any_m = NULL;
  6237. const char *pre_type_data = NULL;
  6238. const char *pre_type_end = NULL;
  6239. upb_msgval encoded;
  6240. jsondec_objstart(d);
  6241. /* Scan looking for "@type", which is not necessarily first. */
  6242. while (!any_m && jsondec_objnext(d)) {
  6243. const char *start = d->ptr;
  6244. upb_strview name = jsondec_string(d);
  6245. jsondec_entrysep(d);
  6246. if (jsondec_streql(name, "@type")) {
  6247. any_m = jsondec_typeurl(d, msg, m);
  6248. if (pre_type_data) {
  6249. pre_type_end = start;
  6250. while (*pre_type_end != ',') pre_type_end--;
  6251. }
  6252. } else {
  6253. if (!pre_type_data) pre_type_data = start;
  6254. jsondec_skipval(d);
  6255. }
  6256. }
  6257. if (!any_m) {
  6258. jsondec_err(d, "Any object didn't contain a '@type' field");
  6259. }
  6260. any_msg = upb_msg_new(any_m, d->arena);
  6261. if (pre_type_data) {
  6262. size_t len = pre_type_end - pre_type_data + 1;
  6263. char *tmp = upb_arena_malloc(d->arena, len);
  6264. const char *saved_ptr = d->ptr;
  6265. const char *saved_end = d->end;
  6266. memcpy(tmp, pre_type_data, len - 1);
  6267. tmp[len - 1] = '}';
  6268. d->ptr = tmp;
  6269. d->end = tmp + len;
  6270. d->is_first = true;
  6271. while (jsondec_objnext(d)) {
  6272. jsondec_anyfield(d, any_msg, any_m);
  6273. }
  6274. d->ptr = saved_ptr;
  6275. d->end = saved_end;
  6276. }
  6277. while (jsondec_objnext(d)) {
  6278. jsondec_anyfield(d, any_msg, any_m);
  6279. }
  6280. jsondec_objend(d);
  6281. encoded.str_val.data = upb_encode(any_msg, upb_msgdef_layout(any_m), d->arena,
  6282. &encoded.str_val.size);
  6283. upb_msg_set(msg, value_f, encoded, d->arena);
  6284. }
  6285. static void jsondec_wrapper(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6286. const upb_fielddef *value_f = upb_msgdef_itof(m, 1);
  6287. upb_msgval val = jsondec_value(d, value_f);
  6288. upb_msg_set(msg, value_f, val, d->arena);
  6289. }
  6290. static void jsondec_wellknown(jsondec *d, upb_msg *msg, const upb_msgdef *m) {
  6291. switch (upb_msgdef_wellknowntype(m)) {
  6292. case UPB_WELLKNOWN_ANY:
  6293. jsondec_any(d, msg, m);
  6294. break;
  6295. case UPB_WELLKNOWN_FIELDMASK:
  6296. jsondec_fieldmask(d, msg, m);
  6297. break;
  6298. case UPB_WELLKNOWN_DURATION:
  6299. jsondec_duration(d, msg, m);
  6300. break;
  6301. case UPB_WELLKNOWN_TIMESTAMP:
  6302. jsondec_timestamp(d, msg, m);
  6303. break;
  6304. case UPB_WELLKNOWN_VALUE:
  6305. jsondec_wellknownvalue(d, msg, m);
  6306. break;
  6307. case UPB_WELLKNOWN_LISTVALUE:
  6308. jsondec_listvalue(d, msg, m);
  6309. break;
  6310. case UPB_WELLKNOWN_STRUCT:
  6311. jsondec_struct(d, msg, m);
  6312. break;
  6313. case UPB_WELLKNOWN_DOUBLEVALUE:
  6314. case UPB_WELLKNOWN_FLOATVALUE:
  6315. case UPB_WELLKNOWN_INT64VALUE:
  6316. case UPB_WELLKNOWN_UINT64VALUE:
  6317. case UPB_WELLKNOWN_INT32VALUE:
  6318. case UPB_WELLKNOWN_UINT32VALUE:
  6319. case UPB_WELLKNOWN_STRINGVALUE:
  6320. case UPB_WELLKNOWN_BYTESVALUE:
  6321. case UPB_WELLKNOWN_BOOLVALUE:
  6322. jsondec_wrapper(d, msg, m);
  6323. break;
  6324. default:
  6325. UPB_UNREACHABLE();
  6326. }
  6327. }
  6328. bool upb_json_decode(const char *buf, size_t size, upb_msg *msg,
  6329. const upb_msgdef *m, const upb_symtab *any_pool,
  6330. int options, upb_arena *arena, upb_status *status) {
  6331. jsondec d;
  6332. d.ptr = buf;
  6333. d.end = buf + size;
  6334. d.arena = arena;
  6335. d.any_pool = any_pool;
  6336. d.status = status;
  6337. d.options = options;
  6338. d.depth = 64;
  6339. d.line = 1;
  6340. d.line_begin = d.ptr;
  6341. d.debug_field = NULL;
  6342. d.is_first = false;
  6343. if (setjmp(d.err)) return false;
  6344. jsondec_tomsg(&d, msg, m);
  6345. return true;
  6346. }
  6347. #include <ctype.h>
  6348. #include <float.h>
  6349. #include <inttypes.h>
  6350. #include <stdarg.h>
  6351. #include <stdio.h>
  6352. #include <string.h>
  6353. #include <setjmp.h>
  6354. typedef struct {
  6355. char *buf, *ptr, *end;
  6356. size_t overflow;
  6357. int indent_depth;
  6358. int options;
  6359. const upb_symtab *ext_pool;
  6360. jmp_buf err;
  6361. upb_status *status;
  6362. upb_arena *arena;
  6363. } jsonenc;
  6364. static void jsonenc_msg(jsonenc *e, const upb_msg *msg, const upb_msgdef *m);
  6365. static void jsonenc_scalar(jsonenc *e, upb_msgval val, const upb_fielddef *f);
  6366. static void jsonenc_msgfield(jsonenc *e, const upb_msg *msg,
  6367. const upb_msgdef *m);
  6368. static void jsonenc_msgfields(jsonenc *e, const upb_msg *msg,
  6369. const upb_msgdef *m);
  6370. static void jsonenc_value(jsonenc *e, const upb_msg *msg, const upb_msgdef *m);
  6371. UPB_NORETURN static void jsonenc_err(jsonenc *e, const char *msg) {
  6372. upb_status_seterrmsg(e->status, msg);
  6373. longjmp(e->err, 1);
  6374. }
  6375. UPB_NORETURN static void jsonenc_errf(jsonenc *e, const char *fmt, ...) {
  6376. va_list argp;
  6377. va_start(argp, fmt);
  6378. upb_status_vseterrf(e->status, fmt, argp);
  6379. va_end(argp);
  6380. longjmp(e->err, 1);
  6381. }
  6382. static upb_arena *jsonenc_arena(jsonenc *e) {
  6383. /* Create lazily, since it's only needed for Any */
  6384. if (!e->arena) {
  6385. e->arena = upb_arena_new();
  6386. }
  6387. return e->arena;
  6388. }
  6389. static void jsonenc_putbytes(jsonenc *e, const void *data, size_t len) {
  6390. size_t have = e->end - e->ptr;
  6391. if (UPB_LIKELY(have >= len)) {
  6392. memcpy(e->ptr, data, len);
  6393. e->ptr += len;
  6394. } else {
  6395. if (have) memcpy(e->ptr, data, have);
  6396. e->ptr += have;
  6397. e->overflow += (len - have);
  6398. }
  6399. }
  6400. static void jsonenc_putstr(jsonenc *e, const char *str) {
  6401. jsonenc_putbytes(e, str, strlen(str));
  6402. }
  6403. static void jsonenc_printf(jsonenc *e, const char *fmt, ...) {
  6404. size_t n;
  6405. size_t have = e->end - e->ptr;
  6406. va_list args;
  6407. va_start(args, fmt);
  6408. n = _upb_vsnprintf(e->ptr, have, fmt, args);
  6409. va_end(args);
  6410. if (UPB_LIKELY(have > n)) {
  6411. e->ptr += n;
  6412. } else {
  6413. e->ptr += have;
  6414. e->overflow += (n - have);
  6415. }
  6416. }
  6417. static void jsonenc_nanos(jsonenc *e, int32_t nanos) {
  6418. int digits = 9;
  6419. if (nanos == 0) return;
  6420. if (nanos < 0 || nanos >= 1000000000) {
  6421. jsonenc_err(e, "error formatting timestamp as JSON: invalid nanos");
  6422. }
  6423. while (nanos % 1000 == 0) {
  6424. nanos /= 1000;
  6425. digits -= 3;
  6426. }
  6427. jsonenc_printf(e, ".%0.*" PRId32, digits, nanos);
  6428. }
  6429. static void jsonenc_timestamp(jsonenc *e, const upb_msg *msg,
  6430. const upb_msgdef *m) {
  6431. const upb_fielddef *seconds_f = upb_msgdef_itof(m, 1);
  6432. const upb_fielddef *nanos_f = upb_msgdef_itof(m, 2);
  6433. int64_t seconds = upb_msg_get(msg, seconds_f).int64_val;
  6434. int32_t nanos = upb_msg_get(msg, nanos_f).int32_val;
  6435. int L, N, I, J, K, hour, min, sec;
  6436. if (seconds < -62135596800) {
  6437. jsonenc_err(e,
  6438. "error formatting timestamp as JSON: minimum acceptable value "
  6439. "is 0001-01-01T00:00:00Z");
  6440. } else if (seconds > 253402300799) {
  6441. jsonenc_err(e,
  6442. "error formatting timestamp as JSON: maximum acceptable value "
  6443. "is 9999-12-31T23:59:59Z");
  6444. }
  6445. /* Julian Day -> Y/M/D, Algorithm from:
  6446. * Fliegel, H. F., and Van Flandern, T. C., "A Machine Algorithm for
  6447. * Processing Calendar Dates," Communications of the Association of
  6448. * Computing Machines, vol. 11 (1968), p. 657. */
  6449. L = (int)(seconds / 86400) + 68569 + 2440588;
  6450. N = 4 * L / 146097;
  6451. L = L - (146097 * N + 3) / 4;
  6452. I = 4000 * (L + 1) / 1461001;
  6453. L = L - 1461 * I / 4 + 31;
  6454. J = 80 * L / 2447;
  6455. K = L - 2447 * J / 80;
  6456. L = J / 11;
  6457. J = J + 2 - 12 * L;
  6458. I = 100 * (N - 49) + I + L;
  6459. sec = seconds % 60;
  6460. min = (seconds / 60) % 60;
  6461. hour = (seconds / 3600) % 24;
  6462. jsonenc_printf(e, "\"%04d-%02d-%02dT%02d:%02d:%02d", I, J, K, hour, min, sec);
  6463. jsonenc_nanos(e, nanos);
  6464. jsonenc_putstr(e, "Z\"");
  6465. }
  6466. static void jsonenc_duration(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
  6467. const upb_fielddef *seconds_f = upb_msgdef_itof(m, 1);
  6468. const upb_fielddef *nanos_f = upb_msgdef_itof(m, 2);
  6469. int64_t seconds = upb_msg_get(msg, seconds_f).int64_val;
  6470. int32_t nanos = upb_msg_get(msg, nanos_f).int32_val;
  6471. if (seconds > 315576000000 || seconds < -315576000000 ||
  6472. (seconds < 0) != (nanos < 0)) {
  6473. jsonenc_err(e, "bad duration");
  6474. }
  6475. if (nanos < 0) {
  6476. nanos = -nanos;
  6477. }
  6478. jsonenc_printf(e, "\"%" PRId64, seconds);
  6479. jsonenc_nanos(e, nanos);
  6480. jsonenc_putstr(e, "s\"");
  6481. }
  6482. static void jsonenc_enum(int32_t val, const upb_fielddef *f, jsonenc *e) {
  6483. const upb_enumdef *e_def = upb_fielddef_enumsubdef(f);
  6484. const char *name = upb_enumdef_iton(e_def, val);
  6485. if (name) {
  6486. jsonenc_printf(e, "\"%s\"", name);
  6487. } else {
  6488. jsonenc_printf(e, "%" PRId32, val);
  6489. }
  6490. }
  6491. static void jsonenc_bytes(jsonenc *e, upb_strview str) {
  6492. /* This is the regular base64, not the "web-safe" version. */
  6493. static const char base64[] =
  6494. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  6495. const unsigned char *ptr = (unsigned char*)str.data;
  6496. const unsigned char *end = ptr + str.size;
  6497. char buf[4];
  6498. jsonenc_putstr(e, "\"");
  6499. while (end - ptr >= 3) {
  6500. buf[0] = base64[ptr[0] >> 2];
  6501. buf[1] = base64[((ptr[0] & 0x3) << 4) | (ptr[1] >> 4)];
  6502. buf[2] = base64[((ptr[1] & 0xf) << 2) | (ptr[2] >> 6)];
  6503. buf[3] = base64[ptr[2] & 0x3f];
  6504. jsonenc_putbytes(e, buf, 4);
  6505. ptr += 3;
  6506. }
  6507. switch (end - ptr) {
  6508. case 2:
  6509. buf[0] = base64[ptr[0] >> 2];
  6510. buf[1] = base64[((ptr[0] & 0x3) << 4) | (ptr[1] >> 4)];
  6511. buf[2] = base64[(ptr[1] & 0xf) << 2];
  6512. buf[3] = '=';
  6513. jsonenc_putbytes(e, buf, 4);
  6514. break;
  6515. case 1:
  6516. buf[0] = base64[ptr[0] >> 2];
  6517. buf[1] = base64[((ptr[0] & 0x3) << 4)];
  6518. buf[2] = '=';
  6519. buf[3] = '=';
  6520. jsonenc_putbytes(e, buf, 4);
  6521. break;
  6522. }
  6523. jsonenc_putstr(e, "\"");
  6524. }
  6525. static void jsonenc_stringbody(jsonenc *e, upb_strview str) {
  6526. const char *ptr = str.data;
  6527. const char *end = ptr + str.size;
  6528. while (ptr < end) {
  6529. switch (*ptr) {
  6530. case '\n':
  6531. jsonenc_putstr(e, "\\n");
  6532. break;
  6533. case '\r':
  6534. jsonenc_putstr(e, "\\r");
  6535. break;
  6536. case '\t':
  6537. jsonenc_putstr(e, "\\t");
  6538. break;
  6539. case '\"':
  6540. jsonenc_putstr(e, "\\\"");
  6541. break;
  6542. case '\f':
  6543. jsonenc_putstr(e, "\\f");
  6544. break;
  6545. case '\b':
  6546. jsonenc_putstr(e, "\\b");
  6547. break;
  6548. case '\\':
  6549. jsonenc_putstr(e, "\\\\");
  6550. break;
  6551. default:
  6552. if ((uint8_t)*ptr < 0x20) {
  6553. jsonenc_printf(e, "\\u%04x", (int)(uint8_t)*ptr);
  6554. } else {
  6555. /* This could be a non-ASCII byte. We rely on the string being valid
  6556. * UTF-8. */
  6557. jsonenc_putbytes(e, ptr, 1);
  6558. }
  6559. break;
  6560. }
  6561. ptr++;
  6562. }
  6563. }
  6564. static void jsonenc_string(jsonenc *e, upb_strview str) {
  6565. jsonenc_putstr(e, "\"");
  6566. jsonenc_stringbody(e, str);
  6567. jsonenc_putstr(e, "\"");
  6568. }
  6569. static void jsonenc_double(jsonenc *e, const char *fmt, double val) {
  6570. if (val == UPB_INFINITY) {
  6571. jsonenc_putstr(e, "\"Infinity\"");
  6572. } else if (val == -UPB_INFINITY) {
  6573. jsonenc_putstr(e, "\"-Infinity\"");
  6574. } else if (val != val) {
  6575. jsonenc_putstr(e, "\"NaN\"");
  6576. } else {
  6577. jsonenc_printf(e, fmt, val);
  6578. }
  6579. }
  6580. static void jsonenc_wrapper(jsonenc *e, const upb_msg *msg,
  6581. const upb_msgdef *m) {
  6582. const upb_fielddef *val_f = upb_msgdef_itof(m, 1);
  6583. upb_msgval val = upb_msg_get(msg, val_f);
  6584. jsonenc_scalar(e, val, val_f);
  6585. }
  6586. static const upb_msgdef *jsonenc_getanymsg(jsonenc *e, upb_strview type_url) {
  6587. /* Find last '/', if any. */
  6588. const char *end = type_url.data + type_url.size;
  6589. const char *ptr = end;
  6590. const upb_msgdef *ret;
  6591. if (!e->ext_pool) {
  6592. jsonenc_err(e, "Tried to encode Any, but no symtab was provided");
  6593. }
  6594. if (type_url.size == 0) goto badurl;
  6595. while (true) {
  6596. if (--ptr == type_url.data) {
  6597. /* Type URL must contain at least one '/', with host before. */
  6598. goto badurl;
  6599. }
  6600. if (*ptr == '/') {
  6601. ptr++;
  6602. break;
  6603. }
  6604. }
  6605. ret = upb_symtab_lookupmsg2(e->ext_pool, ptr, end - ptr);
  6606. if (!ret) {
  6607. jsonenc_errf(e, "Couldn't find Any type: %.*s", (int)(end - ptr), ptr);
  6608. }
  6609. return ret;
  6610. badurl:
  6611. jsonenc_errf(
  6612. e, "Bad type URL: " UPB_STRVIEW_FORMAT, UPB_STRVIEW_ARGS(type_url));
  6613. }
  6614. static void jsonenc_any(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
  6615. const upb_fielddef *type_url_f = upb_msgdef_itof(m, 1);
  6616. const upb_fielddef *value_f = upb_msgdef_itof(m, 2);
  6617. upb_strview type_url = upb_msg_get(msg, type_url_f).str_val;
  6618. upb_strview value = upb_msg_get(msg, value_f).str_val;
  6619. const upb_msgdef *any_m = jsonenc_getanymsg(e, type_url);
  6620. const upb_msglayout *any_layout = upb_msgdef_layout(any_m);
  6621. upb_arena *arena = jsonenc_arena(e);
  6622. upb_msg *any = upb_msg_new(any_m, arena);
  6623. if (!upb_decode(value.data, value.size, any, any_layout, arena)) {
  6624. jsonenc_err(e, "Error decoding message in Any");
  6625. }
  6626. jsonenc_putstr(e, "{\"@type\":");
  6627. jsonenc_string(e, type_url);
  6628. jsonenc_putstr(e, ",");
  6629. if (upb_msgdef_wellknowntype(any_m) == UPB_WELLKNOWN_UNSPECIFIED) {
  6630. /* Regular messages: {"@type": "...","foo": 1, "bar": 2} */
  6631. jsonenc_msgfields(e, any, any_m);
  6632. } else {
  6633. /* Well-known type: {"@type": "...","value": <well-known encoding>} */
  6634. jsonenc_putstr(e, "\"value\":");
  6635. jsonenc_msgfield(e, any, any_m);
  6636. }
  6637. jsonenc_putstr(e, "}");
  6638. }
  6639. static void jsonenc_putsep(jsonenc *e, const char *str, bool *first) {
  6640. if (*first) {
  6641. *first = false;
  6642. } else {
  6643. jsonenc_putstr(e, str);
  6644. }
  6645. }
  6646. static void jsonenc_fieldpath(jsonenc *e, upb_strview path) {
  6647. const char *ptr = path.data;
  6648. const char *end = ptr + path.size;
  6649. while (ptr < end) {
  6650. char ch = *ptr;
  6651. if (ch >= 'A' && ch <= 'Z') {
  6652. jsonenc_err(e, "Field mask element may not have upper-case letter.");
  6653. } else if (ch == '_') {
  6654. if (ptr == end - 1 || *(ptr + 1) < 'a' || *(ptr + 1) > 'z') {
  6655. jsonenc_err(e, "Underscore must be followed by a lowercase letter.");
  6656. }
  6657. ch = *++ptr - 32;
  6658. }
  6659. jsonenc_putbytes(e, &ch, 1);
  6660. ptr++;
  6661. }
  6662. }
  6663. static void jsonenc_fieldmask(jsonenc *e, const upb_msg *msg,
  6664. const upb_msgdef *m) {
  6665. const upb_fielddef *paths_f = upb_msgdef_itof(m, 1);
  6666. const upb_array *paths = upb_msg_get(msg, paths_f).array_val;
  6667. bool first = true;
  6668. size_t i, n = 0;
  6669. if (paths) n = upb_array_size(paths);
  6670. jsonenc_putstr(e, "\"");
  6671. for (i = 0; i < n; i++) {
  6672. jsonenc_putsep(e, ",", &first);
  6673. jsonenc_fieldpath(e, upb_array_get(paths, i).str_val);
  6674. }
  6675. jsonenc_putstr(e, "\"");
  6676. }
  6677. static void jsonenc_struct(jsonenc *e, const upb_msg *msg,
  6678. const upb_msgdef *m) {
  6679. const upb_fielddef *fields_f = upb_msgdef_itof(m, 1);
  6680. const upb_map *fields = upb_msg_get(msg, fields_f).map_val;
  6681. const upb_msgdef *entry_m = upb_fielddef_msgsubdef(fields_f);
  6682. const upb_fielddef *value_f = upb_msgdef_itof(entry_m, 2);
  6683. size_t iter = UPB_MAP_BEGIN;
  6684. bool first = true;
  6685. jsonenc_putstr(e, "{");
  6686. if (fields) {
  6687. while (upb_mapiter_next(fields, &iter)) {
  6688. upb_msgval key = upb_mapiter_key(fields, iter);
  6689. upb_msgval val = upb_mapiter_value(fields, iter);
  6690. jsonenc_putsep(e, ",", &first);
  6691. jsonenc_string(e, key.str_val);
  6692. jsonenc_putstr(e, ":");
  6693. jsonenc_value(e, val.msg_val, upb_fielddef_msgsubdef(value_f));
  6694. }
  6695. }
  6696. jsonenc_putstr(e, "}");
  6697. }
  6698. static void jsonenc_listvalue(jsonenc *e, const upb_msg *msg,
  6699. const upb_msgdef *m) {
  6700. const upb_fielddef *values_f = upb_msgdef_itof(m, 1);
  6701. const upb_msgdef *values_m = upb_fielddef_msgsubdef(values_f);
  6702. const upb_array *values = upb_msg_get(msg, values_f).array_val;
  6703. size_t i;
  6704. bool first = true;
  6705. jsonenc_putstr(e, "[");
  6706. if (values) {
  6707. const size_t size = upb_array_size(values);
  6708. for (i = 0; i < size; i++) {
  6709. upb_msgval elem = upb_array_get(values, i);
  6710. jsonenc_putsep(e, ",", &first);
  6711. jsonenc_value(e, elem.msg_val, values_m);
  6712. }
  6713. }
  6714. jsonenc_putstr(e, "]");
  6715. }
  6716. static void jsonenc_value(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
  6717. /* TODO(haberman): do we want a reflection method to get oneof case? */
  6718. size_t iter = UPB_MSG_BEGIN;
  6719. const upb_fielddef *f;
  6720. upb_msgval val;
  6721. if (!upb_msg_next(msg, m, NULL, &f, &val, &iter)) {
  6722. jsonenc_err(e, "No value set in Value proto");
  6723. }
  6724. switch (upb_fielddef_number(f)) {
  6725. case 1:
  6726. jsonenc_putstr(e, "null");
  6727. break;
  6728. case 2:
  6729. jsonenc_double(e, "%.17g", val.double_val);
  6730. break;
  6731. case 3:
  6732. jsonenc_string(e, val.str_val);
  6733. break;
  6734. case 4:
  6735. jsonenc_putstr(e, val.bool_val ? "true" : "false");
  6736. break;
  6737. case 5:
  6738. jsonenc_struct(e, val.msg_val, upb_fielddef_msgsubdef(f));
  6739. break;
  6740. case 6:
  6741. jsonenc_listvalue(e, val.msg_val, upb_fielddef_msgsubdef(f));
  6742. break;
  6743. }
  6744. }
  6745. static void jsonenc_msgfield(jsonenc *e, const upb_msg *msg,
  6746. const upb_msgdef *m) {
  6747. switch (upb_msgdef_wellknowntype(m)) {
  6748. case UPB_WELLKNOWN_UNSPECIFIED:
  6749. jsonenc_msg(e, msg, m);
  6750. break;
  6751. case UPB_WELLKNOWN_ANY:
  6752. jsonenc_any(e, msg, m);
  6753. break;
  6754. case UPB_WELLKNOWN_FIELDMASK:
  6755. jsonenc_fieldmask(e, msg, m);
  6756. break;
  6757. case UPB_WELLKNOWN_DURATION:
  6758. jsonenc_duration(e, msg, m);
  6759. break;
  6760. case UPB_WELLKNOWN_TIMESTAMP:
  6761. jsonenc_timestamp(e, msg, m);
  6762. break;
  6763. case UPB_WELLKNOWN_DOUBLEVALUE:
  6764. case UPB_WELLKNOWN_FLOATVALUE:
  6765. case UPB_WELLKNOWN_INT64VALUE:
  6766. case UPB_WELLKNOWN_UINT64VALUE:
  6767. case UPB_WELLKNOWN_INT32VALUE:
  6768. case UPB_WELLKNOWN_UINT32VALUE:
  6769. case UPB_WELLKNOWN_STRINGVALUE:
  6770. case UPB_WELLKNOWN_BYTESVALUE:
  6771. case UPB_WELLKNOWN_BOOLVALUE:
  6772. jsonenc_wrapper(e, msg, m);
  6773. break;
  6774. case UPB_WELLKNOWN_VALUE:
  6775. jsonenc_value(e, msg, m);
  6776. break;
  6777. case UPB_WELLKNOWN_LISTVALUE:
  6778. jsonenc_listvalue(e, msg, m);
  6779. break;
  6780. case UPB_WELLKNOWN_STRUCT:
  6781. jsonenc_struct(e, msg, m);
  6782. break;
  6783. }
  6784. }
  6785. static void jsonenc_scalar(jsonenc *e, upb_msgval val, const upb_fielddef *f) {
  6786. switch (upb_fielddef_type(f)) {
  6787. case UPB_TYPE_BOOL:
  6788. jsonenc_putstr(e, val.bool_val ? "true" : "false");
  6789. break;
  6790. case UPB_TYPE_FLOAT:
  6791. jsonenc_double(e, "%.9g", val.float_val);
  6792. break;
  6793. case UPB_TYPE_DOUBLE:
  6794. jsonenc_double(e, "%.17g", val.double_val);
  6795. break;
  6796. case UPB_TYPE_INT32:
  6797. jsonenc_printf(e, "%" PRId32, val.int32_val);
  6798. break;
  6799. case UPB_TYPE_UINT32:
  6800. jsonenc_printf(e, "%" PRIu32, val.uint32_val);
  6801. break;
  6802. case UPB_TYPE_INT64:
  6803. jsonenc_printf(e, "\"%" PRId64 "\"", val.int64_val);
  6804. break;
  6805. case UPB_TYPE_UINT64:
  6806. jsonenc_printf(e, "\"%" PRIu64 "\"", val.uint64_val);
  6807. break;
  6808. case UPB_TYPE_STRING:
  6809. jsonenc_string(e, val.str_val);
  6810. break;
  6811. case UPB_TYPE_BYTES:
  6812. jsonenc_bytes(e, val.str_val);
  6813. break;
  6814. case UPB_TYPE_ENUM:
  6815. jsonenc_enum(val.int32_val, f, e);
  6816. break;
  6817. case UPB_TYPE_MESSAGE:
  6818. jsonenc_msgfield(e, val.msg_val, upb_fielddef_msgsubdef(f));
  6819. break;
  6820. }
  6821. }
  6822. static void jsonenc_mapkey(jsonenc *e, upb_msgval val, const upb_fielddef *f) {
  6823. jsonenc_putstr(e, "\"");
  6824. switch (upb_fielddef_type(f)) {
  6825. case UPB_TYPE_BOOL:
  6826. jsonenc_putstr(e, val.bool_val ? "true" : "false");
  6827. break;
  6828. case UPB_TYPE_INT32:
  6829. jsonenc_printf(e, "%" PRId32, val.int32_val);
  6830. break;
  6831. case UPB_TYPE_UINT32:
  6832. jsonenc_printf(e, "%" PRIu32, val.uint32_val);
  6833. break;
  6834. case UPB_TYPE_INT64:
  6835. jsonenc_printf(e, "%" PRId64, val.int64_val);
  6836. break;
  6837. case UPB_TYPE_UINT64:
  6838. jsonenc_printf(e, "%" PRIu64, val.uint64_val);
  6839. break;
  6840. case UPB_TYPE_STRING:
  6841. jsonenc_stringbody(e, val.str_val);
  6842. break;
  6843. default:
  6844. UPB_UNREACHABLE();
  6845. }
  6846. jsonenc_putstr(e, "\":");
  6847. }
  6848. static void jsonenc_array(jsonenc *e, const upb_array *arr,
  6849. const upb_fielddef *f) {
  6850. size_t i;
  6851. size_t size = upb_array_size(arr);
  6852. bool first = true;
  6853. jsonenc_putstr(e, "[");
  6854. for (i = 0; i < size; i++) {
  6855. jsonenc_putsep(e, ",", &first);
  6856. jsonenc_scalar(e, upb_array_get(arr, i), f);
  6857. }
  6858. jsonenc_putstr(e, "]");
  6859. }
  6860. static void jsonenc_map(jsonenc *e, const upb_map *map, const upb_fielddef *f) {
  6861. const upb_msgdef *entry = upb_fielddef_msgsubdef(f);
  6862. const upb_fielddef *key_f = upb_msgdef_itof(entry, 1);
  6863. const upb_fielddef *val_f = upb_msgdef_itof(entry, 2);
  6864. size_t iter = UPB_MAP_BEGIN;
  6865. bool first = true;
  6866. jsonenc_putstr(e, "{");
  6867. while (upb_mapiter_next(map, &iter)) {
  6868. jsonenc_putsep(e, ",", &first);
  6869. jsonenc_mapkey(e, upb_mapiter_key(map, iter), key_f);
  6870. jsonenc_scalar(e, upb_mapiter_value(map, iter), val_f);
  6871. }
  6872. jsonenc_putstr(e, "}");
  6873. }
  6874. static void jsonenc_fieldval(jsonenc *e, const upb_fielddef *f,
  6875. upb_msgval val, bool *first) {
  6876. const char *name;
  6877. if (e->options & UPB_JSONENC_PROTONAMES) {
  6878. name = upb_fielddef_name(f);
  6879. } else {
  6880. name = upb_fielddef_jsonname(f);
  6881. }
  6882. jsonenc_putsep(e, ",", first);
  6883. jsonenc_printf(e, "\"%s\":", name);
  6884. if (upb_fielddef_ismap(f)) {
  6885. jsonenc_map(e, val.map_val, f);
  6886. } else if (upb_fielddef_isseq(f)) {
  6887. jsonenc_array(e, val.array_val, f);
  6888. } else {
  6889. jsonenc_scalar(e, val, f);
  6890. }
  6891. }
  6892. static void jsonenc_msgfields(jsonenc *e, const upb_msg *msg,
  6893. const upb_msgdef *m) {
  6894. upb_msgval val;
  6895. const upb_fielddef *f;
  6896. bool first = true;
  6897. if (e->options & UPB_JSONENC_EMITDEFAULTS) {
  6898. /* Iterate over all fields. */
  6899. upb_msg_field_iter i;
  6900. for (upb_msg_field_begin(&i, m); !upb_msg_field_done(&i);
  6901. upb_msg_field_next(&i)) {
  6902. f = upb_msg_iter_field(&i);
  6903. jsonenc_fieldval(e, f, upb_msg_get(msg, f), &first);
  6904. }
  6905. } else {
  6906. /* Iterate over non-empty fields. */
  6907. size_t iter = UPB_MSG_BEGIN;
  6908. while (upb_msg_next(msg, m, e->ext_pool, &f, &val, &iter)) {
  6909. jsonenc_fieldval(e, f, val, &first);
  6910. }
  6911. }
  6912. }
  6913. static void jsonenc_msg(jsonenc *e, const upb_msg *msg, const upb_msgdef *m) {
  6914. jsonenc_putstr(e, "{");
  6915. jsonenc_msgfields(e, msg, m);
  6916. jsonenc_putstr(e, "}");
  6917. }
  6918. static size_t jsonenc_nullz(jsonenc *e, size_t size) {
  6919. size_t ret = e->ptr - e->buf + e->overflow;
  6920. if (size > 0) {
  6921. if (e->ptr == e->end) e->ptr--;
  6922. *e->ptr = '\0';
  6923. }
  6924. return ret;
  6925. }
  6926. size_t upb_json_encode(const upb_msg *msg, const upb_msgdef *m,
  6927. const upb_symtab *ext_pool, int options, char *buf,
  6928. size_t size, upb_status *status) {
  6929. jsonenc e;
  6930. e.buf = buf;
  6931. e.ptr = buf;
  6932. e.end = buf + size;
  6933. e.overflow = 0;
  6934. e.options = options;
  6935. e.ext_pool = ext_pool;
  6936. e.status = status;
  6937. e.arena = NULL;
  6938. if (setjmp(e.err)) return -1;
  6939. jsonenc_msgfield(&e, msg, m);
  6940. if (e.arena) upb_arena_free(e.arena);
  6941. return jsonenc_nullz(&e, size);
  6942. }
  6943. /* See port_def.inc. This should #undef all macros #defined there. */
  6944. #undef UPB_MAPTYPE_STRING
  6945. #undef UPB_SIZE
  6946. #undef UPB_PTR_AT
  6947. #undef UPB_READ_ONEOF
  6948. #undef UPB_WRITE_ONEOF
  6949. #undef UPB_INLINE
  6950. #undef UPB_ALIGN_UP
  6951. #undef UPB_ALIGN_DOWN
  6952. #undef UPB_ALIGN_MALLOC
  6953. #undef UPB_ALIGN_OF
  6954. #undef UPB_FORCEINLINE
  6955. #undef UPB_NOINLINE
  6956. #undef UPB_NORETURN
  6957. #undef UPB_MAX
  6958. #undef UPB_MIN
  6959. #undef UPB_UNUSED
  6960. #undef UPB_ASSUME
  6961. #undef UPB_ASSERT
  6962. #undef UPB_ASSERT_DEBUGVAR
  6963. #undef UPB_UNREACHABLE
  6964. #undef UPB_INFINITY
  6965. #undef UPB_NAN
  6966. #undef UPB_MSVC_VSNPRINTF
  6967. #undef _upb_snprintf
  6968. #undef _upb_vsnprintf
  6969. #undef _upb_va_copy