upb.c 576 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977149781497914980149811498214983149841498514986149871498814989149901499114992149931499414995149961499714998149991500015001150021500315004150051500615007150081500915010150111501215013150141501515016150171501815019150201502115022150231502415025150261502715028150291503015031150321503315034150351503615037150381503915040150411504215043150441504515046150471504815049150501505115052150531505415055150561505715058150591506015061150621506315064150651506615067150681506915070150711507215073150741507515076150771507815079150801508115082150831508415085150861508715088150891509015091150921509315094150951509615097150981509915100151011510215103151041510515106151071510815109151101511115112151131511415115151161511715118151191512015121151221512315124151251512615127151281512915130151311513215133151341513515136151371513815139151401514115142151431514415145151461514715148151491515015151151521515315154151551515615157151581515915160151611516215163151641516515166151671516815169151701517115172151731517415175151761517715178151791518015181151821518315184151851518615187151881518915190151911519215193151941519515196151971519815199152001520115202152031520415205152061520715208152091521015211152121521315214152151521615217152181521915220152211522215223152241522515226152271522815229152301523115232152331523415235152361523715238152391524015241152421524315244152451524615247152481524915250152511525215253152541525515256152571525815259152601526115262152631526415265152661526715268152691527015271152721527315274152751527615277152781527915280152811528215283152841528515286152871528815289152901529115292152931529415295152961529715298152991530015301153021530315304153051530615307153081530915310153111531215313153141531515316153171531815319153201532115322153231532415325153261532715328153291533015331153321533315334153351533615337153381533915340153411534215343153441534515346153471534815349153501535115352153531535415355153561535715358153591536015361153621536315364153651536615367153681536915370153711537215373153741537515376153771537815379153801538115382153831538415385153861538715388153891539015391153921539315394153951539615397153981539915400154011540215403154041540515406154071540815409154101541115412154131541415415154161541715418154191542015421154221542315424154251542615427154281542915430154311543215433154341543515436154371543815439154401544115442154431544415445154461544715448154491545015451154521545315454154551545615457154581545915460154611546215463154641546515466154671546815469154701547115472154731547415475154761547715478154791548015481154821548315484154851548615487154881548915490154911549215493154941549515496154971549815499155001550115502155031550415505155061550715508155091551015511155121551315514155151551615517155181551915520155211552215523155241552515526155271552815529155301553115532155331553415535155361553715538155391554015541155421554315544155451554615547155481554915550155511555215553155541555515556155571555815559155601556115562155631556415565155661556715568155691557015571155721557315574155751557615577155781557915580155811558215583155841558515586155871558815589155901559115592155931559415595155961559715598155991560015601156021560315604156051560615607156081560915610156111561215613156141561515616156171561815619156201562115622156231562415625156261562715628156291563015631156321563315634156351563615637156381563915640156411564215643156441564515646156471564815649156501565115652156531565415655156561565715658156591566015661156621566315664156651566615667156681566915670156711567215673156741567515676156771567815679156801568115682156831568415685156861568715688156891569015691156921569315694156951569615697156981569915700157011570215703157041570515706157071570815709157101571115712157131571415715157161571715718157191572015721157221572315724157251572615727157281572915730157311573215733157341573515736157371573815739157401574115742157431574415745157461574715748157491575015751157521575315754157551575615757157581575915760157611576215763157641576515766157671576815769157701577115772157731577415775157761577715778157791578015781157821578315784157851578615787157881578915790157911579215793157941579515796157971579815799158001580115802158031580415805158061580715808158091581015811158121581315814158151581615817158181581915820158211582215823158241582515826158271582815829158301583115832158331583415835158361583715838158391584015841158421584315844158451584615847158481584915850158511585215853158541585515856158571585815859158601586115862158631586415865158661586715868158691587015871158721587315874158751587615877158781587915880158811588215883158841588515886158871588815889158901589115892158931589415895158961589715898158991590015901159021590315904159051590615907159081590915910159111591215913159141591515916159171591815919159201592115922159231592415925159261592715928159291593015931159321593315934159351593615937159381593915940159411594215943159441594515946159471594815949159501595115952159531595415955159561595715958159591596015961159621596315964159651596615967159681596915970159711597215973159741597515976159771597815979159801598115982159831598415985159861598715988159891599015991159921599315994159951599615997159981599916000160011600216003160041600516006160071600816009160101601116012160131601416015160161601716018160191602016021160221602316024160251602616027160281602916030160311603216033160341603516036160371603816039160401604116042160431604416045160461604716048160491605016051160521605316054160551605616057160581605916060160611606216063160641606516066160671606816069160701607116072160731607416075160761607716078160791608016081160821608316084160851608616087160881608916090160911609216093160941609516096160971609816099161001610116102161031610416105161061610716108161091611016111161121611316114161151611616117161181611916120161211612216123161241612516126161271612816129161301613116132161331613416135161361613716138161391614016141161421614316144161451614616147161481614916150161511615216153161541615516156161571615816159161601616116162161631616416165161661616716168161691617016171161721617316174161751617616177161781617916180161811618216183161841618516186161871618816189161901619116192161931619416195161961619716198161991620016201162021620316204162051620616207162081620916210162111621216213162141621516216162171621816219162201622116222162231622416225162261622716228162291623016231162321623316234162351623616237162381623916240162411624216243162441624516246162471624816249162501625116252162531625416255162561625716258162591626016261162621626316264162651626616267162681626916270162711627216273162741627516276162771627816279162801628116282162831628416285162861628716288162891629016291162921629316294162951629616297162981629916300163011630216303163041630516306163071630816309163101631116312163131631416315163161631716318163191632016321163221632316324163251632616327163281632916330163311633216333163341633516336163371633816339163401634116342163431634416345163461634716348163491635016351163521635316354163551635616357163581635916360163611636216363163641636516366163671636816369163701637116372163731637416375163761637716378163791638016381163821638316384163851638616387163881638916390163911639216393163941639516396163971639816399164001640116402164031640416405164061640716408164091641016411164121641316414164151641616417164181641916420164211642216423164241642516426164271642816429164301643116432164331643416435164361643716438164391644016441164421644316444164451644616447164481644916450164511645216453164541645516456164571645816459164601646116462164631646416465164661646716468164691647016471164721647316474164751647616477164781647916480164811648216483164841648516486164871648816489164901649116492164931649416495164961649716498164991650016501165021650316504165051650616507165081650916510165111651216513165141651516516165171651816519165201652116522165231652416525165261652716528165291653016531165321653316534165351653616537165381653916540165411654216543165441654516546165471654816549165501655116552165531655416555165561655716558165591656016561165621656316564165651656616567165681656916570165711657216573165741657516576165771657816579165801658116582165831658416585165861658716588165891659016591165921659316594165951659616597165981659916600166011660216603166041660516606166071660816609166101661116612166131661416615166161661716618166191662016621166221662316624166251662616627166281662916630166311663216633166341663516636166371663816639166401664116642166431664416645166461664716648166491665016651166521665316654166551665616657166581665916660166611666216663166641666516666166671666816669166701667116672166731667416675166761667716678166791668016681166821668316684166851668616687166881668916690166911669216693166941669516696166971669816699167001670116702167031670416705167061670716708167091671016711167121671316714167151671616717167181671916720167211672216723167241672516726167271672816729167301673116732167331673416735167361673716738167391674016741167421674316744167451674616747167481674916750167511675216753167541675516756167571675816759167601676116762167631676416765167661676716768167691677016771167721677316774167751677616777167781677916780167811678216783167841678516786167871678816789167901679116792167931679416795167961679716798167991680016801168021680316804168051680616807168081680916810168111681216813168141681516816168171681816819168201682116822168231682416825168261682716828168291683016831168321683316834168351683616837168381683916840168411684216843168441684516846168471684816849168501685116852
  1. // Amalgamated source file
  2. #include "upb.h"
  3. /* This file was generated by upbc (the upb compiler) from the input
  4. * file:
  5. *
  6. * google/protobuf/descriptor.proto
  7. *
  8. * Do not edit -- your changes will be discarded when the file is
  9. * regenerated. */
  10. #include <stddef.h>
  11. struct google_protobuf_FileDescriptorSet {
  12. upb_array* file;
  13. };
  14. static const upb_msglayout_msginit_v1 *const google_protobuf_FileDescriptorSet_submsgs[1] = {
  15. &google_protobuf_FileDescriptorProto_msginit,
  16. };
  17. static const upb_msglayout_fieldinit_v1 google_protobuf_FileDescriptorSet__fields[1] = {
  18. {1, offsetof(google_protobuf_FileDescriptorSet, file), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3},
  19. };
  20. const upb_msglayout_msginit_v1 google_protobuf_FileDescriptorSet_msginit = {
  21. &google_protobuf_FileDescriptorSet_submsgs[0],
  22. &google_protobuf_FileDescriptorSet__fields[0],
  23. NULL,
  24. NULL, /* TODO. default_msg */
  25. UPB_ALIGNED_SIZEOF(google_protobuf_FileDescriptorSet), 1, 0, false, true
  26. };
  27. google_protobuf_FileDescriptorSet *google_protobuf_FileDescriptorSet_new(upb_env *env) {
  28. google_protobuf_FileDescriptorSet *msg = upb_env_malloc(env, sizeof(*msg));
  29. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  30. return msg;
  31. }
  32. google_protobuf_FileDescriptorSet *google_protobuf_FileDescriptorSet_parsenew(upb_stringview buf, upb_env *env) {
  33. google_protobuf_FileDescriptorSet *msg = google_protobuf_FileDescriptorSet_new(env);
  34. if (upb_decode(buf, msg, &google_protobuf_FileDescriptorSet_msginit, env)) {
  35. return msg;
  36. } else {
  37. return NULL;
  38. }
  39. }
  40. char *google_protobuf_FileDescriptorSet_serialize(google_protobuf_FileDescriptorSet *msg, upb_env *env, size_t *size) {
  41. return upb_encode(msg, &google_protobuf_FileDescriptorSet_msginit, env, size);
  42. }
  43. const upb_array* google_protobuf_FileDescriptorSet_file(const google_protobuf_FileDescriptorSet *msg) {
  44. return msg->file;
  45. }
  46. void google_protobuf_FileDescriptorSet_set_file(google_protobuf_FileDescriptorSet *msg, upb_array* value) {
  47. msg->file = value;
  48. }
  49. struct google_protobuf_FileDescriptorProto {
  50. upb_stringview name;
  51. upb_stringview package;
  52. upb_stringview syntax;
  53. google_protobuf_FileOptions* options;
  54. google_protobuf_SourceCodeInfo* source_code_info;
  55. upb_array* dependency;
  56. upb_array* message_type;
  57. upb_array* enum_type;
  58. upb_array* service;
  59. upb_array* extension;
  60. upb_array* public_dependency;
  61. upb_array* weak_dependency;
  62. };
  63. static const upb_msglayout_msginit_v1 *const google_protobuf_FileDescriptorProto_submsgs[6] = {
  64. &google_protobuf_DescriptorProto_msginit,
  65. &google_protobuf_EnumDescriptorProto_msginit,
  66. &google_protobuf_FieldDescriptorProto_msginit,
  67. &google_protobuf_FileOptions_msginit,
  68. &google_protobuf_ServiceDescriptorProto_msginit,
  69. &google_protobuf_SourceCodeInfo_msginit,
  70. };
  71. static const upb_msglayout_fieldinit_v1 google_protobuf_FileDescriptorProto__fields[12] = {
  72. {1, offsetof(google_protobuf_FileDescriptorProto, name), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  73. {2, offsetof(google_protobuf_FileDescriptorProto, package), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  74. {3, offsetof(google_protobuf_FileDescriptorProto, dependency), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 3},
  75. {4, offsetof(google_protobuf_FileDescriptorProto, message_type), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3},
  76. {5, offsetof(google_protobuf_FileDescriptorProto, enum_type), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 1, 11, 3},
  77. {6, offsetof(google_protobuf_FileDescriptorProto, service), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 4, 11, 3},
  78. {7, offsetof(google_protobuf_FileDescriptorProto, extension), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 2, 11, 3},
  79. {8, offsetof(google_protobuf_FileDescriptorProto, options), 3, UPB_NOT_IN_ONEOF, 3, 11, 1},
  80. {9, offsetof(google_protobuf_FileDescriptorProto, source_code_info), 4, UPB_NOT_IN_ONEOF, 5, 11, 1},
  81. {10, offsetof(google_protobuf_FileDescriptorProto, public_dependency), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 3},
  82. {11, offsetof(google_protobuf_FileDescriptorProto, weak_dependency), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 3},
  83. {12, offsetof(google_protobuf_FileDescriptorProto, syntax), 2, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  84. };
  85. const upb_msglayout_msginit_v1 google_protobuf_FileDescriptorProto_msginit = {
  86. &google_protobuf_FileDescriptorProto_submsgs[0],
  87. &google_protobuf_FileDescriptorProto__fields[0],
  88. NULL,
  89. NULL, /* TODO. default_msg */
  90. UPB_ALIGNED_SIZEOF(google_protobuf_FileDescriptorProto), 12, 0, false, true
  91. };
  92. google_protobuf_FileDescriptorProto *google_protobuf_FileDescriptorProto_new(upb_env *env) {
  93. google_protobuf_FileDescriptorProto *msg = upb_env_malloc(env, sizeof(*msg));
  94. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  95. return msg;
  96. }
  97. google_protobuf_FileDescriptorProto *google_protobuf_FileDescriptorProto_parsenew(upb_stringview buf, upb_env *env) {
  98. google_protobuf_FileDescriptorProto *msg = google_protobuf_FileDescriptorProto_new(env);
  99. if (upb_decode(buf, msg, &google_protobuf_FileDescriptorProto_msginit, env)) {
  100. return msg;
  101. } else {
  102. return NULL;
  103. }
  104. }
  105. char *google_protobuf_FileDescriptorProto_serialize(google_protobuf_FileDescriptorProto *msg, upb_env *env, size_t *size) {
  106. return upb_encode(msg, &google_protobuf_FileDescriptorProto_msginit, env, size);
  107. }
  108. upb_stringview google_protobuf_FileDescriptorProto_name(const google_protobuf_FileDescriptorProto *msg) {
  109. return msg->name;
  110. }
  111. void google_protobuf_FileDescriptorProto_set_name(google_protobuf_FileDescriptorProto *msg, upb_stringview value) {
  112. msg->name = value;
  113. }
  114. upb_stringview google_protobuf_FileDescriptorProto_package(const google_protobuf_FileDescriptorProto *msg) {
  115. return msg->package;
  116. }
  117. void google_protobuf_FileDescriptorProto_set_package(google_protobuf_FileDescriptorProto *msg, upb_stringview value) {
  118. msg->package = value;
  119. }
  120. const upb_array* google_protobuf_FileDescriptorProto_dependency(const google_protobuf_FileDescriptorProto *msg) {
  121. return msg->dependency;
  122. }
  123. void google_protobuf_FileDescriptorProto_set_dependency(google_protobuf_FileDescriptorProto *msg, upb_array* value) {
  124. msg->dependency = value;
  125. }
  126. const upb_array* google_protobuf_FileDescriptorProto_message_type(const google_protobuf_FileDescriptorProto *msg) {
  127. return msg->message_type;
  128. }
  129. void google_protobuf_FileDescriptorProto_set_message_type(google_protobuf_FileDescriptorProto *msg, upb_array* value) {
  130. msg->message_type = value;
  131. }
  132. const upb_array* google_protobuf_FileDescriptorProto_enum_type(const google_protobuf_FileDescriptorProto *msg) {
  133. return msg->enum_type;
  134. }
  135. void google_protobuf_FileDescriptorProto_set_enum_type(google_protobuf_FileDescriptorProto *msg, upb_array* value) {
  136. msg->enum_type = value;
  137. }
  138. const upb_array* google_protobuf_FileDescriptorProto_service(const google_protobuf_FileDescriptorProto *msg) {
  139. return msg->service;
  140. }
  141. void google_protobuf_FileDescriptorProto_set_service(google_protobuf_FileDescriptorProto *msg, upb_array* value) {
  142. msg->service = value;
  143. }
  144. const upb_array* google_protobuf_FileDescriptorProto_extension(const google_protobuf_FileDescriptorProto *msg) {
  145. return msg->extension;
  146. }
  147. void google_protobuf_FileDescriptorProto_set_extension(google_protobuf_FileDescriptorProto *msg, upb_array* value) {
  148. msg->extension = value;
  149. }
  150. const google_protobuf_FileOptions* google_protobuf_FileDescriptorProto_options(const google_protobuf_FileDescriptorProto *msg) {
  151. return msg->options;
  152. }
  153. void google_protobuf_FileDescriptorProto_set_options(google_protobuf_FileDescriptorProto *msg, google_protobuf_FileOptions* value) {
  154. msg->options = value;
  155. }
  156. const google_protobuf_SourceCodeInfo* google_protobuf_FileDescriptorProto_source_code_info(const google_protobuf_FileDescriptorProto *msg) {
  157. return msg->source_code_info;
  158. }
  159. void google_protobuf_FileDescriptorProto_set_source_code_info(google_protobuf_FileDescriptorProto *msg, google_protobuf_SourceCodeInfo* value) {
  160. msg->source_code_info = value;
  161. }
  162. const upb_array* google_protobuf_FileDescriptorProto_public_dependency(const google_protobuf_FileDescriptorProto *msg) {
  163. return msg->public_dependency;
  164. }
  165. void google_protobuf_FileDescriptorProto_set_public_dependency(google_protobuf_FileDescriptorProto *msg, upb_array* value) {
  166. msg->public_dependency = value;
  167. }
  168. const upb_array* google_protobuf_FileDescriptorProto_weak_dependency(const google_protobuf_FileDescriptorProto *msg) {
  169. return msg->weak_dependency;
  170. }
  171. void google_protobuf_FileDescriptorProto_set_weak_dependency(google_protobuf_FileDescriptorProto *msg, upb_array* value) {
  172. msg->weak_dependency = value;
  173. }
  174. upb_stringview google_protobuf_FileDescriptorProto_syntax(const google_protobuf_FileDescriptorProto *msg) {
  175. return msg->syntax;
  176. }
  177. void google_protobuf_FileDescriptorProto_set_syntax(google_protobuf_FileDescriptorProto *msg, upb_stringview value) {
  178. msg->syntax = value;
  179. }
  180. struct google_protobuf_DescriptorProto {
  181. upb_stringview name;
  182. google_protobuf_MessageOptions* options;
  183. upb_array* field;
  184. upb_array* nested_type;
  185. upb_array* enum_type;
  186. upb_array* extension_range;
  187. upb_array* extension;
  188. upb_array* oneof_decl;
  189. upb_array* reserved_range;
  190. upb_array* reserved_name;
  191. };
  192. static const upb_msglayout_msginit_v1 *const google_protobuf_DescriptorProto_submsgs[8] = {
  193. &google_protobuf_DescriptorProto_msginit,
  194. &google_protobuf_DescriptorProto_ExtensionRange_msginit,
  195. &google_protobuf_DescriptorProto_ReservedRange_msginit,
  196. &google_protobuf_EnumDescriptorProto_msginit,
  197. &google_protobuf_FieldDescriptorProto_msginit,
  198. &google_protobuf_MessageOptions_msginit,
  199. &google_protobuf_OneofDescriptorProto_msginit,
  200. };
  201. static const upb_msglayout_fieldinit_v1 google_protobuf_DescriptorProto__fields[10] = {
  202. {1, offsetof(google_protobuf_DescriptorProto, name), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  203. {2, offsetof(google_protobuf_DescriptorProto, field), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 4, 11, 3},
  204. {3, offsetof(google_protobuf_DescriptorProto, nested_type), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3},
  205. {4, offsetof(google_protobuf_DescriptorProto, enum_type), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 3, 11, 3},
  206. {5, offsetof(google_protobuf_DescriptorProto, extension_range), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 1, 11, 3},
  207. {6, offsetof(google_protobuf_DescriptorProto, extension), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 4, 11, 3},
  208. {7, offsetof(google_protobuf_DescriptorProto, options), 1, UPB_NOT_IN_ONEOF, 5, 11, 1},
  209. {8, offsetof(google_protobuf_DescriptorProto, oneof_decl), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 6, 11, 3},
  210. {9, offsetof(google_protobuf_DescriptorProto, reserved_range), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 2, 11, 3},
  211. {10, offsetof(google_protobuf_DescriptorProto, reserved_name), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 3},
  212. };
  213. const upb_msglayout_msginit_v1 google_protobuf_DescriptorProto_msginit = {
  214. &google_protobuf_DescriptorProto_submsgs[0],
  215. &google_protobuf_DescriptorProto__fields[0],
  216. NULL,
  217. NULL, /* TODO. default_msg */
  218. UPB_ALIGNED_SIZEOF(google_protobuf_DescriptorProto), 10, 0, false, true
  219. };
  220. google_protobuf_DescriptorProto *google_protobuf_DescriptorProto_new(upb_env *env) {
  221. google_protobuf_DescriptorProto *msg = upb_env_malloc(env, sizeof(*msg));
  222. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  223. return msg;
  224. }
  225. google_protobuf_DescriptorProto *google_protobuf_DescriptorProto_parsenew(upb_stringview buf, upb_env *env) {
  226. google_protobuf_DescriptorProto *msg = google_protobuf_DescriptorProto_new(env);
  227. if (upb_decode(buf, msg, &google_protobuf_DescriptorProto_msginit, env)) {
  228. return msg;
  229. } else {
  230. return NULL;
  231. }
  232. }
  233. char *google_protobuf_DescriptorProto_serialize(google_protobuf_DescriptorProto *msg, upb_env *env, size_t *size) {
  234. return upb_encode(msg, &google_protobuf_DescriptorProto_msginit, env, size);
  235. }
  236. upb_stringview google_protobuf_DescriptorProto_name(const google_protobuf_DescriptorProto *msg) {
  237. return msg->name;
  238. }
  239. void google_protobuf_DescriptorProto_set_name(google_protobuf_DescriptorProto *msg, upb_stringview value) {
  240. msg->name = value;
  241. }
  242. const upb_array* google_protobuf_DescriptorProto_field(const google_protobuf_DescriptorProto *msg) {
  243. return msg->field;
  244. }
  245. void google_protobuf_DescriptorProto_set_field(google_protobuf_DescriptorProto *msg, upb_array* value) {
  246. msg->field = value;
  247. }
  248. const upb_array* google_protobuf_DescriptorProto_nested_type(const google_protobuf_DescriptorProto *msg) {
  249. return msg->nested_type;
  250. }
  251. void google_protobuf_DescriptorProto_set_nested_type(google_protobuf_DescriptorProto *msg, upb_array* value) {
  252. msg->nested_type = value;
  253. }
  254. const upb_array* google_protobuf_DescriptorProto_enum_type(const google_protobuf_DescriptorProto *msg) {
  255. return msg->enum_type;
  256. }
  257. void google_protobuf_DescriptorProto_set_enum_type(google_protobuf_DescriptorProto *msg, upb_array* value) {
  258. msg->enum_type = value;
  259. }
  260. const upb_array* google_protobuf_DescriptorProto_extension_range(const google_protobuf_DescriptorProto *msg) {
  261. return msg->extension_range;
  262. }
  263. void google_protobuf_DescriptorProto_set_extension_range(google_protobuf_DescriptorProto *msg, upb_array* value) {
  264. msg->extension_range = value;
  265. }
  266. const upb_array* google_protobuf_DescriptorProto_extension(const google_protobuf_DescriptorProto *msg) {
  267. return msg->extension;
  268. }
  269. void google_protobuf_DescriptorProto_set_extension(google_protobuf_DescriptorProto *msg, upb_array* value) {
  270. msg->extension = value;
  271. }
  272. const google_protobuf_MessageOptions* google_protobuf_DescriptorProto_options(const google_protobuf_DescriptorProto *msg) {
  273. return msg->options;
  274. }
  275. void google_protobuf_DescriptorProto_set_options(google_protobuf_DescriptorProto *msg, google_protobuf_MessageOptions* value) {
  276. msg->options = value;
  277. }
  278. const upb_array* google_protobuf_DescriptorProto_oneof_decl(const google_protobuf_DescriptorProto *msg) {
  279. return msg->oneof_decl;
  280. }
  281. void google_protobuf_DescriptorProto_set_oneof_decl(google_protobuf_DescriptorProto *msg, upb_array* value) {
  282. msg->oneof_decl = value;
  283. }
  284. const upb_array* google_protobuf_DescriptorProto_reserved_range(const google_protobuf_DescriptorProto *msg) {
  285. return msg->reserved_range;
  286. }
  287. void google_protobuf_DescriptorProto_set_reserved_range(google_protobuf_DescriptorProto *msg, upb_array* value) {
  288. msg->reserved_range = value;
  289. }
  290. const upb_array* google_protobuf_DescriptorProto_reserved_name(const google_protobuf_DescriptorProto *msg) {
  291. return msg->reserved_name;
  292. }
  293. void google_protobuf_DescriptorProto_set_reserved_name(google_protobuf_DescriptorProto *msg, upb_array* value) {
  294. msg->reserved_name = value;
  295. }
  296. struct google_protobuf_DescriptorProto_ExtensionRange {
  297. int32_t start;
  298. int32_t end;
  299. google_protobuf_ExtensionRangeOptions* options;
  300. };
  301. static const upb_msglayout_msginit_v1 *const google_protobuf_DescriptorProto_ExtensionRange_submsgs[1] = {
  302. &google_protobuf_ExtensionRangeOptions_msginit,
  303. };
  304. static const upb_msglayout_fieldinit_v1 google_protobuf_DescriptorProto_ExtensionRange__fields[3] = {
  305. {1, offsetof(google_protobuf_DescriptorProto_ExtensionRange, start), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 1},
  306. {2, offsetof(google_protobuf_DescriptorProto_ExtensionRange, end), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 1},
  307. {3, offsetof(google_protobuf_DescriptorProto_ExtensionRange, options), 2, UPB_NOT_IN_ONEOF, 0, 11, 1},
  308. };
  309. const upb_msglayout_msginit_v1 google_protobuf_DescriptorProto_ExtensionRange_msginit = {
  310. &google_protobuf_DescriptorProto_ExtensionRange_submsgs[0],
  311. &google_protobuf_DescriptorProto_ExtensionRange__fields[0],
  312. NULL,
  313. NULL, /* TODO. default_msg */
  314. UPB_ALIGNED_SIZEOF(google_protobuf_DescriptorProto_ExtensionRange), 3, 0, false, true
  315. };
  316. google_protobuf_DescriptorProto_ExtensionRange *google_protobuf_DescriptorProto_ExtensionRange_new(upb_env *env) {
  317. google_protobuf_DescriptorProto_ExtensionRange *msg = upb_env_malloc(env, sizeof(*msg));
  318. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  319. return msg;
  320. }
  321. google_protobuf_DescriptorProto_ExtensionRange *google_protobuf_DescriptorProto_ExtensionRange_parsenew(upb_stringview buf, upb_env *env) {
  322. google_protobuf_DescriptorProto_ExtensionRange *msg = google_protobuf_DescriptorProto_ExtensionRange_new(env);
  323. if (upb_decode(buf, msg, &google_protobuf_DescriptorProto_ExtensionRange_msginit, env)) {
  324. return msg;
  325. } else {
  326. return NULL;
  327. }
  328. }
  329. char *google_protobuf_DescriptorProto_ExtensionRange_serialize(google_protobuf_DescriptorProto_ExtensionRange *msg, upb_env *env, size_t *size) {
  330. return upb_encode(msg, &google_protobuf_DescriptorProto_ExtensionRange_msginit, env, size);
  331. }
  332. int32_t google_protobuf_DescriptorProto_ExtensionRange_start(const google_protobuf_DescriptorProto_ExtensionRange *msg) {
  333. return msg->start;
  334. }
  335. void google_protobuf_DescriptorProto_ExtensionRange_set_start(google_protobuf_DescriptorProto_ExtensionRange *msg, int32_t value) {
  336. msg->start = value;
  337. }
  338. int32_t google_protobuf_DescriptorProto_ExtensionRange_end(const google_protobuf_DescriptorProto_ExtensionRange *msg) {
  339. return msg->end;
  340. }
  341. void google_protobuf_DescriptorProto_ExtensionRange_set_end(google_protobuf_DescriptorProto_ExtensionRange *msg, int32_t value) {
  342. msg->end = value;
  343. }
  344. const google_protobuf_ExtensionRangeOptions* google_protobuf_DescriptorProto_ExtensionRange_options(const google_protobuf_DescriptorProto_ExtensionRange *msg) {
  345. return msg->options;
  346. }
  347. void google_protobuf_DescriptorProto_ExtensionRange_set_options(google_protobuf_DescriptorProto_ExtensionRange *msg, google_protobuf_ExtensionRangeOptions* value) {
  348. msg->options = value;
  349. }
  350. struct google_protobuf_DescriptorProto_ReservedRange {
  351. int32_t start;
  352. int32_t end;
  353. };
  354. static const upb_msglayout_fieldinit_v1 google_protobuf_DescriptorProto_ReservedRange__fields[2] = {
  355. {1, offsetof(google_protobuf_DescriptorProto_ReservedRange, start), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 1},
  356. {2, offsetof(google_protobuf_DescriptorProto_ReservedRange, end), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 1},
  357. };
  358. const upb_msglayout_msginit_v1 google_protobuf_DescriptorProto_ReservedRange_msginit = {
  359. NULL,
  360. &google_protobuf_DescriptorProto_ReservedRange__fields[0],
  361. NULL,
  362. NULL, /* TODO. default_msg */
  363. UPB_ALIGNED_SIZEOF(google_protobuf_DescriptorProto_ReservedRange), 2, 0, false, true
  364. };
  365. google_protobuf_DescriptorProto_ReservedRange *google_protobuf_DescriptorProto_ReservedRange_new(upb_env *env) {
  366. google_protobuf_DescriptorProto_ReservedRange *msg = upb_env_malloc(env, sizeof(*msg));
  367. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  368. return msg;
  369. }
  370. google_protobuf_DescriptorProto_ReservedRange *google_protobuf_DescriptorProto_ReservedRange_parsenew(upb_stringview buf, upb_env *env) {
  371. google_protobuf_DescriptorProto_ReservedRange *msg = google_protobuf_DescriptorProto_ReservedRange_new(env);
  372. if (upb_decode(buf, msg, &google_protobuf_DescriptorProto_ReservedRange_msginit, env)) {
  373. return msg;
  374. } else {
  375. return NULL;
  376. }
  377. }
  378. char *google_protobuf_DescriptorProto_ReservedRange_serialize(google_protobuf_DescriptorProto_ReservedRange *msg, upb_env *env, size_t *size) {
  379. return upb_encode(msg, &google_protobuf_DescriptorProto_ReservedRange_msginit, env, size);
  380. }
  381. int32_t google_protobuf_DescriptorProto_ReservedRange_start(const google_protobuf_DescriptorProto_ReservedRange *msg) {
  382. return msg->start;
  383. }
  384. void google_protobuf_DescriptorProto_ReservedRange_set_start(google_protobuf_DescriptorProto_ReservedRange *msg, int32_t value) {
  385. msg->start = value;
  386. }
  387. int32_t google_protobuf_DescriptorProto_ReservedRange_end(const google_protobuf_DescriptorProto_ReservedRange *msg) {
  388. return msg->end;
  389. }
  390. void google_protobuf_DescriptorProto_ReservedRange_set_end(google_protobuf_DescriptorProto_ReservedRange *msg, int32_t value) {
  391. msg->end = value;
  392. }
  393. struct google_protobuf_ExtensionRangeOptions {
  394. upb_array* uninterpreted_option;
  395. };
  396. static const upb_msglayout_msginit_v1 *const google_protobuf_ExtensionRangeOptions_submsgs[1] = {
  397. &google_protobuf_UninterpretedOption_msginit,
  398. };
  399. static const upb_msglayout_fieldinit_v1 google_protobuf_ExtensionRangeOptions__fields[1] = {
  400. {999, offsetof(google_protobuf_ExtensionRangeOptions, uninterpreted_option), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3},
  401. };
  402. const upb_msglayout_msginit_v1 google_protobuf_ExtensionRangeOptions_msginit = {
  403. &google_protobuf_ExtensionRangeOptions_submsgs[0],
  404. &google_protobuf_ExtensionRangeOptions__fields[0],
  405. NULL,
  406. NULL, /* TODO. default_msg */
  407. UPB_ALIGNED_SIZEOF(google_protobuf_ExtensionRangeOptions), 1, 0, false, true
  408. };
  409. google_protobuf_ExtensionRangeOptions *google_protobuf_ExtensionRangeOptions_new(upb_env *env) {
  410. google_protobuf_ExtensionRangeOptions *msg = upb_env_malloc(env, sizeof(*msg));
  411. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  412. return msg;
  413. }
  414. google_protobuf_ExtensionRangeOptions *google_protobuf_ExtensionRangeOptions_parsenew(upb_stringview buf, upb_env *env) {
  415. google_protobuf_ExtensionRangeOptions *msg = google_protobuf_ExtensionRangeOptions_new(env);
  416. if (upb_decode(buf, msg, &google_protobuf_ExtensionRangeOptions_msginit, env)) {
  417. return msg;
  418. } else {
  419. return NULL;
  420. }
  421. }
  422. char *google_protobuf_ExtensionRangeOptions_serialize(google_protobuf_ExtensionRangeOptions *msg, upb_env *env, size_t *size) {
  423. return upb_encode(msg, &google_protobuf_ExtensionRangeOptions_msginit, env, size);
  424. }
  425. const upb_array* google_protobuf_ExtensionRangeOptions_uninterpreted_option(const google_protobuf_ExtensionRangeOptions *msg) {
  426. return msg->uninterpreted_option;
  427. }
  428. void google_protobuf_ExtensionRangeOptions_set_uninterpreted_option(google_protobuf_ExtensionRangeOptions *msg, upb_array* value) {
  429. msg->uninterpreted_option = value;
  430. }
  431. struct google_protobuf_FieldDescriptorProto {
  432. google_protobuf_FieldDescriptorProto_Label label;
  433. google_protobuf_FieldDescriptorProto_Type type;
  434. int32_t number;
  435. int32_t oneof_index;
  436. upb_stringview name;
  437. upb_stringview extendee;
  438. upb_stringview type_name;
  439. upb_stringview default_value;
  440. upb_stringview json_name;
  441. google_protobuf_FieldOptions* options;
  442. };
  443. static const upb_msglayout_msginit_v1 *const google_protobuf_FieldDescriptorProto_submsgs[1] = {
  444. &google_protobuf_FieldOptions_msginit,
  445. };
  446. static const upb_msglayout_fieldinit_v1 google_protobuf_FieldDescriptorProto__fields[10] = {
  447. {1, offsetof(google_protobuf_FieldDescriptorProto, name), 4, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  448. {2, offsetof(google_protobuf_FieldDescriptorProto, extendee), 5, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  449. {3, offsetof(google_protobuf_FieldDescriptorProto, number), 2, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 1},
  450. {4, offsetof(google_protobuf_FieldDescriptorProto, label), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 14, 1},
  451. {5, offsetof(google_protobuf_FieldDescriptorProto, type), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 14, 1},
  452. {6, offsetof(google_protobuf_FieldDescriptorProto, type_name), 6, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  453. {7, offsetof(google_protobuf_FieldDescriptorProto, default_value), 7, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  454. {8, offsetof(google_protobuf_FieldDescriptorProto, options), 9, UPB_NOT_IN_ONEOF, 0, 11, 1},
  455. {9, offsetof(google_protobuf_FieldDescriptorProto, oneof_index), 3, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 1},
  456. {10, offsetof(google_protobuf_FieldDescriptorProto, json_name), 8, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  457. };
  458. const upb_msglayout_msginit_v1 google_protobuf_FieldDescriptorProto_msginit = {
  459. &google_protobuf_FieldDescriptorProto_submsgs[0],
  460. &google_protobuf_FieldDescriptorProto__fields[0],
  461. NULL,
  462. NULL, /* TODO. default_msg */
  463. UPB_ALIGNED_SIZEOF(google_protobuf_FieldDescriptorProto), 10, 0, false, true
  464. };
  465. google_protobuf_FieldDescriptorProto *google_protobuf_FieldDescriptorProto_new(upb_env *env) {
  466. google_protobuf_FieldDescriptorProto *msg = upb_env_malloc(env, sizeof(*msg));
  467. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  468. return msg;
  469. }
  470. google_protobuf_FieldDescriptorProto *google_protobuf_FieldDescriptorProto_parsenew(upb_stringview buf, upb_env *env) {
  471. google_protobuf_FieldDescriptorProto *msg = google_protobuf_FieldDescriptorProto_new(env);
  472. if (upb_decode(buf, msg, &google_protobuf_FieldDescriptorProto_msginit, env)) {
  473. return msg;
  474. } else {
  475. return NULL;
  476. }
  477. }
  478. char *google_protobuf_FieldDescriptorProto_serialize(google_protobuf_FieldDescriptorProto *msg, upb_env *env, size_t *size) {
  479. return upb_encode(msg, &google_protobuf_FieldDescriptorProto_msginit, env, size);
  480. }
  481. upb_stringview google_protobuf_FieldDescriptorProto_name(const google_protobuf_FieldDescriptorProto *msg) {
  482. return msg->name;
  483. }
  484. void google_protobuf_FieldDescriptorProto_set_name(google_protobuf_FieldDescriptorProto *msg, upb_stringview value) {
  485. msg->name = value;
  486. }
  487. upb_stringview google_protobuf_FieldDescriptorProto_extendee(const google_protobuf_FieldDescriptorProto *msg) {
  488. return msg->extendee;
  489. }
  490. void google_protobuf_FieldDescriptorProto_set_extendee(google_protobuf_FieldDescriptorProto *msg, upb_stringview value) {
  491. msg->extendee = value;
  492. }
  493. int32_t google_protobuf_FieldDescriptorProto_number(const google_protobuf_FieldDescriptorProto *msg) {
  494. return msg->number;
  495. }
  496. void google_protobuf_FieldDescriptorProto_set_number(google_protobuf_FieldDescriptorProto *msg, int32_t value) {
  497. msg->number = value;
  498. }
  499. google_protobuf_FieldDescriptorProto_Label google_protobuf_FieldDescriptorProto_label(const google_protobuf_FieldDescriptorProto *msg) {
  500. return msg->label;
  501. }
  502. void google_protobuf_FieldDescriptorProto_set_label(google_protobuf_FieldDescriptorProto *msg, google_protobuf_FieldDescriptorProto_Label value) {
  503. msg->label = value;
  504. }
  505. google_protobuf_FieldDescriptorProto_Type google_protobuf_FieldDescriptorProto_type(const google_protobuf_FieldDescriptorProto *msg) {
  506. return msg->type;
  507. }
  508. void google_protobuf_FieldDescriptorProto_set_type(google_protobuf_FieldDescriptorProto *msg, google_protobuf_FieldDescriptorProto_Type value) {
  509. msg->type = value;
  510. }
  511. upb_stringview google_protobuf_FieldDescriptorProto_type_name(const google_protobuf_FieldDescriptorProto *msg) {
  512. return msg->type_name;
  513. }
  514. void google_protobuf_FieldDescriptorProto_set_type_name(google_protobuf_FieldDescriptorProto *msg, upb_stringview value) {
  515. msg->type_name = value;
  516. }
  517. upb_stringview google_protobuf_FieldDescriptorProto_default_value(const google_protobuf_FieldDescriptorProto *msg) {
  518. return msg->default_value;
  519. }
  520. void google_protobuf_FieldDescriptorProto_set_default_value(google_protobuf_FieldDescriptorProto *msg, upb_stringview value) {
  521. msg->default_value = value;
  522. }
  523. const google_protobuf_FieldOptions* google_protobuf_FieldDescriptorProto_options(const google_protobuf_FieldDescriptorProto *msg) {
  524. return msg->options;
  525. }
  526. void google_protobuf_FieldDescriptorProto_set_options(google_protobuf_FieldDescriptorProto *msg, google_protobuf_FieldOptions* value) {
  527. msg->options = value;
  528. }
  529. int32_t google_protobuf_FieldDescriptorProto_oneof_index(const google_protobuf_FieldDescriptorProto *msg) {
  530. return msg->oneof_index;
  531. }
  532. void google_protobuf_FieldDescriptorProto_set_oneof_index(google_protobuf_FieldDescriptorProto *msg, int32_t value) {
  533. msg->oneof_index = value;
  534. }
  535. upb_stringview google_protobuf_FieldDescriptorProto_json_name(const google_protobuf_FieldDescriptorProto *msg) {
  536. return msg->json_name;
  537. }
  538. void google_protobuf_FieldDescriptorProto_set_json_name(google_protobuf_FieldDescriptorProto *msg, upb_stringview value) {
  539. msg->json_name = value;
  540. }
  541. struct google_protobuf_OneofDescriptorProto {
  542. upb_stringview name;
  543. google_protobuf_OneofOptions* options;
  544. };
  545. static const upb_msglayout_msginit_v1 *const google_protobuf_OneofDescriptorProto_submsgs[1] = {
  546. &google_protobuf_OneofOptions_msginit,
  547. };
  548. static const upb_msglayout_fieldinit_v1 google_protobuf_OneofDescriptorProto__fields[2] = {
  549. {1, offsetof(google_protobuf_OneofDescriptorProto, name), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  550. {2, offsetof(google_protobuf_OneofDescriptorProto, options), 1, UPB_NOT_IN_ONEOF, 0, 11, 1},
  551. };
  552. const upb_msglayout_msginit_v1 google_protobuf_OneofDescriptorProto_msginit = {
  553. &google_protobuf_OneofDescriptorProto_submsgs[0],
  554. &google_protobuf_OneofDescriptorProto__fields[0],
  555. NULL,
  556. NULL, /* TODO. default_msg */
  557. UPB_ALIGNED_SIZEOF(google_protobuf_OneofDescriptorProto), 2, 0, false, true
  558. };
  559. google_protobuf_OneofDescriptorProto *google_protobuf_OneofDescriptorProto_new(upb_env *env) {
  560. google_protobuf_OneofDescriptorProto *msg = upb_env_malloc(env, sizeof(*msg));
  561. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  562. return msg;
  563. }
  564. google_protobuf_OneofDescriptorProto *google_protobuf_OneofDescriptorProto_parsenew(upb_stringview buf, upb_env *env) {
  565. google_protobuf_OneofDescriptorProto *msg = google_protobuf_OneofDescriptorProto_new(env);
  566. if (upb_decode(buf, msg, &google_protobuf_OneofDescriptorProto_msginit, env)) {
  567. return msg;
  568. } else {
  569. return NULL;
  570. }
  571. }
  572. char *google_protobuf_OneofDescriptorProto_serialize(google_protobuf_OneofDescriptorProto *msg, upb_env *env, size_t *size) {
  573. return upb_encode(msg, &google_protobuf_OneofDescriptorProto_msginit, env, size);
  574. }
  575. upb_stringview google_protobuf_OneofDescriptorProto_name(const google_protobuf_OneofDescriptorProto *msg) {
  576. return msg->name;
  577. }
  578. void google_protobuf_OneofDescriptorProto_set_name(google_protobuf_OneofDescriptorProto *msg, upb_stringview value) {
  579. msg->name = value;
  580. }
  581. const google_protobuf_OneofOptions* google_protobuf_OneofDescriptorProto_options(const google_protobuf_OneofDescriptorProto *msg) {
  582. return msg->options;
  583. }
  584. void google_protobuf_OneofDescriptorProto_set_options(google_protobuf_OneofDescriptorProto *msg, google_protobuf_OneofOptions* value) {
  585. msg->options = value;
  586. }
  587. struct google_protobuf_EnumDescriptorProto {
  588. upb_stringview name;
  589. google_protobuf_EnumOptions* options;
  590. upb_array* value;
  591. upb_array* reserved_range;
  592. upb_array* reserved_name;
  593. };
  594. static const upb_msglayout_msginit_v1 *const google_protobuf_EnumDescriptorProto_submsgs[3] = {
  595. &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit,
  596. &google_protobuf_EnumOptions_msginit,
  597. &google_protobuf_EnumValueDescriptorProto_msginit,
  598. };
  599. static const upb_msglayout_fieldinit_v1 google_protobuf_EnumDescriptorProto__fields[5] = {
  600. {1, offsetof(google_protobuf_EnumDescriptorProto, name), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  601. {2, offsetof(google_protobuf_EnumDescriptorProto, value), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 2, 11, 3},
  602. {3, offsetof(google_protobuf_EnumDescriptorProto, options), 1, UPB_NOT_IN_ONEOF, 1, 11, 1},
  603. {4, offsetof(google_protobuf_EnumDescriptorProto, reserved_range), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3},
  604. {5, offsetof(google_protobuf_EnumDescriptorProto, reserved_name), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 3},
  605. };
  606. const upb_msglayout_msginit_v1 google_protobuf_EnumDescriptorProto_msginit = {
  607. &google_protobuf_EnumDescriptorProto_submsgs[0],
  608. &google_protobuf_EnumDescriptorProto__fields[0],
  609. NULL,
  610. NULL, /* TODO. default_msg */
  611. UPB_ALIGNED_SIZEOF(google_protobuf_EnumDescriptorProto), 5, 0, false, true
  612. };
  613. google_protobuf_EnumDescriptorProto *google_protobuf_EnumDescriptorProto_new(upb_env *env) {
  614. google_protobuf_EnumDescriptorProto *msg = upb_env_malloc(env, sizeof(*msg));
  615. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  616. return msg;
  617. }
  618. google_protobuf_EnumDescriptorProto *google_protobuf_EnumDescriptorProto_parsenew(upb_stringview buf, upb_env *env) {
  619. google_protobuf_EnumDescriptorProto *msg = google_protobuf_EnumDescriptorProto_new(env);
  620. if (upb_decode(buf, msg, &google_protobuf_EnumDescriptorProto_msginit, env)) {
  621. return msg;
  622. } else {
  623. return NULL;
  624. }
  625. }
  626. char *google_protobuf_EnumDescriptorProto_serialize(google_protobuf_EnumDescriptorProto *msg, upb_env *env, size_t *size) {
  627. return upb_encode(msg, &google_protobuf_EnumDescriptorProto_msginit, env, size);
  628. }
  629. upb_stringview google_protobuf_EnumDescriptorProto_name(const google_protobuf_EnumDescriptorProto *msg) {
  630. return msg->name;
  631. }
  632. void google_protobuf_EnumDescriptorProto_set_name(google_protobuf_EnumDescriptorProto *msg, upb_stringview value) {
  633. msg->name = value;
  634. }
  635. const upb_array* google_protobuf_EnumDescriptorProto_value(const google_protobuf_EnumDescriptorProto *msg) {
  636. return msg->value;
  637. }
  638. void google_protobuf_EnumDescriptorProto_set_value(google_protobuf_EnumDescriptorProto *msg, upb_array* value) {
  639. msg->value = value;
  640. }
  641. const google_protobuf_EnumOptions* google_protobuf_EnumDescriptorProto_options(const google_protobuf_EnumDescriptorProto *msg) {
  642. return msg->options;
  643. }
  644. void google_protobuf_EnumDescriptorProto_set_options(google_protobuf_EnumDescriptorProto *msg, google_protobuf_EnumOptions* value) {
  645. msg->options = value;
  646. }
  647. const upb_array* google_protobuf_EnumDescriptorProto_reserved_range(const google_protobuf_EnumDescriptorProto *msg) {
  648. return msg->reserved_range;
  649. }
  650. void google_protobuf_EnumDescriptorProto_set_reserved_range(google_protobuf_EnumDescriptorProto *msg, upb_array* value) {
  651. msg->reserved_range = value;
  652. }
  653. const upb_array* google_protobuf_EnumDescriptorProto_reserved_name(const google_protobuf_EnumDescriptorProto *msg) {
  654. return msg->reserved_name;
  655. }
  656. void google_protobuf_EnumDescriptorProto_set_reserved_name(google_protobuf_EnumDescriptorProto *msg, upb_array* value) {
  657. msg->reserved_name = value;
  658. }
  659. struct google_protobuf_EnumDescriptorProto_EnumReservedRange {
  660. int32_t start;
  661. int32_t end;
  662. };
  663. static const upb_msglayout_fieldinit_v1 google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[2] = {
  664. {1, offsetof(google_protobuf_EnumDescriptorProto_EnumReservedRange, start), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 1},
  665. {2, offsetof(google_protobuf_EnumDescriptorProto_EnumReservedRange, end), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 1},
  666. };
  667. const upb_msglayout_msginit_v1 google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit = {
  668. NULL,
  669. &google_protobuf_EnumDescriptorProto_EnumReservedRange__fields[0],
  670. NULL,
  671. NULL, /* TODO. default_msg */
  672. UPB_ALIGNED_SIZEOF(google_protobuf_EnumDescriptorProto_EnumReservedRange), 2, 0, false, true
  673. };
  674. google_protobuf_EnumDescriptorProto_EnumReservedRange *google_protobuf_EnumDescriptorProto_EnumReservedRange_new(upb_env *env) {
  675. google_protobuf_EnumDescriptorProto_EnumReservedRange *msg = upb_env_malloc(env, sizeof(*msg));
  676. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  677. return msg;
  678. }
  679. google_protobuf_EnumDescriptorProto_EnumReservedRange *google_protobuf_EnumDescriptorProto_EnumReservedRange_parsenew(upb_stringview buf, upb_env *env) {
  680. google_protobuf_EnumDescriptorProto_EnumReservedRange *msg = google_protobuf_EnumDescriptorProto_EnumReservedRange_new(env);
  681. if (upb_decode(buf, msg, &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, env)) {
  682. return msg;
  683. } else {
  684. return NULL;
  685. }
  686. }
  687. char *google_protobuf_EnumDescriptorProto_EnumReservedRange_serialize(google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, upb_env *env, size_t *size) {
  688. return upb_encode(msg, &google_protobuf_EnumDescriptorProto_EnumReservedRange_msginit, env, size);
  689. }
  690. int32_t google_protobuf_EnumDescriptorProto_EnumReservedRange_start(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) {
  691. return msg->start;
  692. }
  693. void google_protobuf_EnumDescriptorProto_EnumReservedRange_set_start(google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, int32_t value) {
  694. msg->start = value;
  695. }
  696. int32_t google_protobuf_EnumDescriptorProto_EnumReservedRange_end(const google_protobuf_EnumDescriptorProto_EnumReservedRange *msg) {
  697. return msg->end;
  698. }
  699. void google_protobuf_EnumDescriptorProto_EnumReservedRange_set_end(google_protobuf_EnumDescriptorProto_EnumReservedRange *msg, int32_t value) {
  700. msg->end = value;
  701. }
  702. struct google_protobuf_EnumValueDescriptorProto {
  703. int32_t number;
  704. upb_stringview name;
  705. google_protobuf_EnumValueOptions* options;
  706. };
  707. static const upb_msglayout_msginit_v1 *const google_protobuf_EnumValueDescriptorProto_submsgs[1] = {
  708. &google_protobuf_EnumValueOptions_msginit,
  709. };
  710. static const upb_msglayout_fieldinit_v1 google_protobuf_EnumValueDescriptorProto__fields[3] = {
  711. {1, offsetof(google_protobuf_EnumValueDescriptorProto, name), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  712. {2, offsetof(google_protobuf_EnumValueDescriptorProto, number), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 1},
  713. {3, offsetof(google_protobuf_EnumValueDescriptorProto, options), 2, UPB_NOT_IN_ONEOF, 0, 11, 1},
  714. };
  715. const upb_msglayout_msginit_v1 google_protobuf_EnumValueDescriptorProto_msginit = {
  716. &google_protobuf_EnumValueDescriptorProto_submsgs[0],
  717. &google_protobuf_EnumValueDescriptorProto__fields[0],
  718. NULL,
  719. NULL, /* TODO. default_msg */
  720. UPB_ALIGNED_SIZEOF(google_protobuf_EnumValueDescriptorProto), 3, 0, false, true
  721. };
  722. google_protobuf_EnumValueDescriptorProto *google_protobuf_EnumValueDescriptorProto_new(upb_env *env) {
  723. google_protobuf_EnumValueDescriptorProto *msg = upb_env_malloc(env, sizeof(*msg));
  724. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  725. return msg;
  726. }
  727. google_protobuf_EnumValueDescriptorProto *google_protobuf_EnumValueDescriptorProto_parsenew(upb_stringview buf, upb_env *env) {
  728. google_protobuf_EnumValueDescriptorProto *msg = google_protobuf_EnumValueDescriptorProto_new(env);
  729. if (upb_decode(buf, msg, &google_protobuf_EnumValueDescriptorProto_msginit, env)) {
  730. return msg;
  731. } else {
  732. return NULL;
  733. }
  734. }
  735. char *google_protobuf_EnumValueDescriptorProto_serialize(google_protobuf_EnumValueDescriptorProto *msg, upb_env *env, size_t *size) {
  736. return upb_encode(msg, &google_protobuf_EnumValueDescriptorProto_msginit, env, size);
  737. }
  738. upb_stringview google_protobuf_EnumValueDescriptorProto_name(const google_protobuf_EnumValueDescriptorProto *msg) {
  739. return msg->name;
  740. }
  741. void google_protobuf_EnumValueDescriptorProto_set_name(google_protobuf_EnumValueDescriptorProto *msg, upb_stringview value) {
  742. msg->name = value;
  743. }
  744. int32_t google_protobuf_EnumValueDescriptorProto_number(const google_protobuf_EnumValueDescriptorProto *msg) {
  745. return msg->number;
  746. }
  747. void google_protobuf_EnumValueDescriptorProto_set_number(google_protobuf_EnumValueDescriptorProto *msg, int32_t value) {
  748. msg->number = value;
  749. }
  750. const google_protobuf_EnumValueOptions* google_protobuf_EnumValueDescriptorProto_options(const google_protobuf_EnumValueDescriptorProto *msg) {
  751. return msg->options;
  752. }
  753. void google_protobuf_EnumValueDescriptorProto_set_options(google_protobuf_EnumValueDescriptorProto *msg, google_protobuf_EnumValueOptions* value) {
  754. msg->options = value;
  755. }
  756. struct google_protobuf_ServiceDescriptorProto {
  757. upb_stringview name;
  758. google_protobuf_ServiceOptions* options;
  759. upb_array* method;
  760. };
  761. static const upb_msglayout_msginit_v1 *const google_protobuf_ServiceDescriptorProto_submsgs[2] = {
  762. &google_protobuf_MethodDescriptorProto_msginit,
  763. &google_protobuf_ServiceOptions_msginit,
  764. };
  765. static const upb_msglayout_fieldinit_v1 google_protobuf_ServiceDescriptorProto__fields[3] = {
  766. {1, offsetof(google_protobuf_ServiceDescriptorProto, name), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  767. {2, offsetof(google_protobuf_ServiceDescriptorProto, method), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3},
  768. {3, offsetof(google_protobuf_ServiceDescriptorProto, options), 1, UPB_NOT_IN_ONEOF, 1, 11, 1},
  769. };
  770. const upb_msglayout_msginit_v1 google_protobuf_ServiceDescriptorProto_msginit = {
  771. &google_protobuf_ServiceDescriptorProto_submsgs[0],
  772. &google_protobuf_ServiceDescriptorProto__fields[0],
  773. NULL,
  774. NULL, /* TODO. default_msg */
  775. UPB_ALIGNED_SIZEOF(google_protobuf_ServiceDescriptorProto), 3, 0, false, true
  776. };
  777. google_protobuf_ServiceDescriptorProto *google_protobuf_ServiceDescriptorProto_new(upb_env *env) {
  778. google_protobuf_ServiceDescriptorProto *msg = upb_env_malloc(env, sizeof(*msg));
  779. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  780. return msg;
  781. }
  782. google_protobuf_ServiceDescriptorProto *google_protobuf_ServiceDescriptorProto_parsenew(upb_stringview buf, upb_env *env) {
  783. google_protobuf_ServiceDescriptorProto *msg = google_protobuf_ServiceDescriptorProto_new(env);
  784. if (upb_decode(buf, msg, &google_protobuf_ServiceDescriptorProto_msginit, env)) {
  785. return msg;
  786. } else {
  787. return NULL;
  788. }
  789. }
  790. char *google_protobuf_ServiceDescriptorProto_serialize(google_protobuf_ServiceDescriptorProto *msg, upb_env *env, size_t *size) {
  791. return upb_encode(msg, &google_protobuf_ServiceDescriptorProto_msginit, env, size);
  792. }
  793. upb_stringview google_protobuf_ServiceDescriptorProto_name(const google_protobuf_ServiceDescriptorProto *msg) {
  794. return msg->name;
  795. }
  796. void google_protobuf_ServiceDescriptorProto_set_name(google_protobuf_ServiceDescriptorProto *msg, upb_stringview value) {
  797. msg->name = value;
  798. }
  799. const upb_array* google_protobuf_ServiceDescriptorProto_method(const google_protobuf_ServiceDescriptorProto *msg) {
  800. return msg->method;
  801. }
  802. void google_protobuf_ServiceDescriptorProto_set_method(google_protobuf_ServiceDescriptorProto *msg, upb_array* value) {
  803. msg->method = value;
  804. }
  805. const google_protobuf_ServiceOptions* google_protobuf_ServiceDescriptorProto_options(const google_protobuf_ServiceDescriptorProto *msg) {
  806. return msg->options;
  807. }
  808. void google_protobuf_ServiceDescriptorProto_set_options(google_protobuf_ServiceDescriptorProto *msg, google_protobuf_ServiceOptions* value) {
  809. msg->options = value;
  810. }
  811. struct google_protobuf_MethodDescriptorProto {
  812. bool client_streaming;
  813. bool server_streaming;
  814. upb_stringview name;
  815. upb_stringview input_type;
  816. upb_stringview output_type;
  817. google_protobuf_MethodOptions* options;
  818. };
  819. static const upb_msglayout_msginit_v1 *const google_protobuf_MethodDescriptorProto_submsgs[1] = {
  820. &google_protobuf_MethodOptions_msginit,
  821. };
  822. static const upb_msglayout_fieldinit_v1 google_protobuf_MethodDescriptorProto__fields[6] = {
  823. {1, offsetof(google_protobuf_MethodDescriptorProto, name), 2, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  824. {2, offsetof(google_protobuf_MethodDescriptorProto, input_type), 3, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  825. {3, offsetof(google_protobuf_MethodDescriptorProto, output_type), 4, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  826. {4, offsetof(google_protobuf_MethodDescriptorProto, options), 5, UPB_NOT_IN_ONEOF, 0, 11, 1},
  827. {5, offsetof(google_protobuf_MethodDescriptorProto, client_streaming), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  828. {6, offsetof(google_protobuf_MethodDescriptorProto, server_streaming), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  829. };
  830. const upb_msglayout_msginit_v1 google_protobuf_MethodDescriptorProto_msginit = {
  831. &google_protobuf_MethodDescriptorProto_submsgs[0],
  832. &google_protobuf_MethodDescriptorProto__fields[0],
  833. NULL,
  834. NULL, /* TODO. default_msg */
  835. UPB_ALIGNED_SIZEOF(google_protobuf_MethodDescriptorProto), 6, 0, false, true
  836. };
  837. google_protobuf_MethodDescriptorProto *google_protobuf_MethodDescriptorProto_new(upb_env *env) {
  838. google_protobuf_MethodDescriptorProto *msg = upb_env_malloc(env, sizeof(*msg));
  839. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  840. return msg;
  841. }
  842. google_protobuf_MethodDescriptorProto *google_protobuf_MethodDescriptorProto_parsenew(upb_stringview buf, upb_env *env) {
  843. google_protobuf_MethodDescriptorProto *msg = google_protobuf_MethodDescriptorProto_new(env);
  844. if (upb_decode(buf, msg, &google_protobuf_MethodDescriptorProto_msginit, env)) {
  845. return msg;
  846. } else {
  847. return NULL;
  848. }
  849. }
  850. char *google_protobuf_MethodDescriptorProto_serialize(google_protobuf_MethodDescriptorProto *msg, upb_env *env, size_t *size) {
  851. return upb_encode(msg, &google_protobuf_MethodDescriptorProto_msginit, env, size);
  852. }
  853. upb_stringview google_protobuf_MethodDescriptorProto_name(const google_protobuf_MethodDescriptorProto *msg) {
  854. return msg->name;
  855. }
  856. void google_protobuf_MethodDescriptorProto_set_name(google_protobuf_MethodDescriptorProto *msg, upb_stringview value) {
  857. msg->name = value;
  858. }
  859. upb_stringview google_protobuf_MethodDescriptorProto_input_type(const google_protobuf_MethodDescriptorProto *msg) {
  860. return msg->input_type;
  861. }
  862. void google_protobuf_MethodDescriptorProto_set_input_type(google_protobuf_MethodDescriptorProto *msg, upb_stringview value) {
  863. msg->input_type = value;
  864. }
  865. upb_stringview google_protobuf_MethodDescriptorProto_output_type(const google_protobuf_MethodDescriptorProto *msg) {
  866. return msg->output_type;
  867. }
  868. void google_protobuf_MethodDescriptorProto_set_output_type(google_protobuf_MethodDescriptorProto *msg, upb_stringview value) {
  869. msg->output_type = value;
  870. }
  871. const google_protobuf_MethodOptions* google_protobuf_MethodDescriptorProto_options(const google_protobuf_MethodDescriptorProto *msg) {
  872. return msg->options;
  873. }
  874. void google_protobuf_MethodDescriptorProto_set_options(google_protobuf_MethodDescriptorProto *msg, google_protobuf_MethodOptions* value) {
  875. msg->options = value;
  876. }
  877. bool google_protobuf_MethodDescriptorProto_client_streaming(const google_protobuf_MethodDescriptorProto *msg) {
  878. return msg->client_streaming;
  879. }
  880. void google_protobuf_MethodDescriptorProto_set_client_streaming(google_protobuf_MethodDescriptorProto *msg, bool value) {
  881. msg->client_streaming = value;
  882. }
  883. bool google_protobuf_MethodDescriptorProto_server_streaming(const google_protobuf_MethodDescriptorProto *msg) {
  884. return msg->server_streaming;
  885. }
  886. void google_protobuf_MethodDescriptorProto_set_server_streaming(google_protobuf_MethodDescriptorProto *msg, bool value) {
  887. msg->server_streaming = value;
  888. }
  889. struct google_protobuf_FileOptions {
  890. google_protobuf_FileOptions_OptimizeMode optimize_for;
  891. bool java_multiple_files;
  892. bool cc_generic_services;
  893. bool java_generic_services;
  894. bool py_generic_services;
  895. bool java_generate_equals_and_hash;
  896. bool deprecated;
  897. bool java_string_check_utf8;
  898. bool cc_enable_arenas;
  899. bool php_generic_services;
  900. upb_stringview java_package;
  901. upb_stringview java_outer_classname;
  902. upb_stringview go_package;
  903. upb_stringview objc_class_prefix;
  904. upb_stringview csharp_namespace;
  905. upb_stringview swift_prefix;
  906. upb_stringview php_class_prefix;
  907. upb_stringview php_namespace;
  908. upb_array* uninterpreted_option;
  909. };
  910. static const upb_msglayout_msginit_v1 *const google_protobuf_FileOptions_submsgs[1] = {
  911. &google_protobuf_UninterpretedOption_msginit,
  912. };
  913. static const upb_msglayout_fieldinit_v1 google_protobuf_FileOptions__fields[19] = {
  914. {1, offsetof(google_protobuf_FileOptions, java_package), 10, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  915. {8, offsetof(google_protobuf_FileOptions, java_outer_classname), 11, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  916. {9, offsetof(google_protobuf_FileOptions, optimize_for), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 14, 1},
  917. {10, offsetof(google_protobuf_FileOptions, java_multiple_files), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  918. {11, offsetof(google_protobuf_FileOptions, go_package), 12, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  919. {16, offsetof(google_protobuf_FileOptions, cc_generic_services), 2, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  920. {17, offsetof(google_protobuf_FileOptions, java_generic_services), 3, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  921. {18, offsetof(google_protobuf_FileOptions, py_generic_services), 4, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  922. {20, offsetof(google_protobuf_FileOptions, java_generate_equals_and_hash), 5, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  923. {23, offsetof(google_protobuf_FileOptions, deprecated), 6, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  924. {27, offsetof(google_protobuf_FileOptions, java_string_check_utf8), 7, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  925. {31, offsetof(google_protobuf_FileOptions, cc_enable_arenas), 8, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  926. {36, offsetof(google_protobuf_FileOptions, objc_class_prefix), 13, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  927. {37, offsetof(google_protobuf_FileOptions, csharp_namespace), 14, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  928. {39, offsetof(google_protobuf_FileOptions, swift_prefix), 15, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  929. {40, offsetof(google_protobuf_FileOptions, php_class_prefix), 16, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  930. {41, offsetof(google_protobuf_FileOptions, php_namespace), 17, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  931. {42, offsetof(google_protobuf_FileOptions, php_generic_services), 9, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  932. {999, offsetof(google_protobuf_FileOptions, uninterpreted_option), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3},
  933. };
  934. const upb_msglayout_msginit_v1 google_protobuf_FileOptions_msginit = {
  935. &google_protobuf_FileOptions_submsgs[0],
  936. &google_protobuf_FileOptions__fields[0],
  937. NULL,
  938. NULL, /* TODO. default_msg */
  939. UPB_ALIGNED_SIZEOF(google_protobuf_FileOptions), 19, 0, false, true
  940. };
  941. google_protobuf_FileOptions *google_protobuf_FileOptions_new(upb_env *env) {
  942. google_protobuf_FileOptions *msg = upb_env_malloc(env, sizeof(*msg));
  943. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  944. return msg;
  945. }
  946. google_protobuf_FileOptions *google_protobuf_FileOptions_parsenew(upb_stringview buf, upb_env *env) {
  947. google_protobuf_FileOptions *msg = google_protobuf_FileOptions_new(env);
  948. if (upb_decode(buf, msg, &google_protobuf_FileOptions_msginit, env)) {
  949. return msg;
  950. } else {
  951. return NULL;
  952. }
  953. }
  954. char *google_protobuf_FileOptions_serialize(google_protobuf_FileOptions *msg, upb_env *env, size_t *size) {
  955. return upb_encode(msg, &google_protobuf_FileOptions_msginit, env, size);
  956. }
  957. upb_stringview google_protobuf_FileOptions_java_package(const google_protobuf_FileOptions *msg) {
  958. return msg->java_package;
  959. }
  960. void google_protobuf_FileOptions_set_java_package(google_protobuf_FileOptions *msg, upb_stringview value) {
  961. msg->java_package = value;
  962. }
  963. upb_stringview google_protobuf_FileOptions_java_outer_classname(const google_protobuf_FileOptions *msg) {
  964. return msg->java_outer_classname;
  965. }
  966. void google_protobuf_FileOptions_set_java_outer_classname(google_protobuf_FileOptions *msg, upb_stringview value) {
  967. msg->java_outer_classname = value;
  968. }
  969. google_protobuf_FileOptions_OptimizeMode google_protobuf_FileOptions_optimize_for(const google_protobuf_FileOptions *msg) {
  970. return msg->optimize_for;
  971. }
  972. void google_protobuf_FileOptions_set_optimize_for(google_protobuf_FileOptions *msg, google_protobuf_FileOptions_OptimizeMode value) {
  973. msg->optimize_for = value;
  974. }
  975. bool google_protobuf_FileOptions_java_multiple_files(const google_protobuf_FileOptions *msg) {
  976. return msg->java_multiple_files;
  977. }
  978. void google_protobuf_FileOptions_set_java_multiple_files(google_protobuf_FileOptions *msg, bool value) {
  979. msg->java_multiple_files = value;
  980. }
  981. upb_stringview google_protobuf_FileOptions_go_package(const google_protobuf_FileOptions *msg) {
  982. return msg->go_package;
  983. }
  984. void google_protobuf_FileOptions_set_go_package(google_protobuf_FileOptions *msg, upb_stringview value) {
  985. msg->go_package = value;
  986. }
  987. bool google_protobuf_FileOptions_cc_generic_services(const google_protobuf_FileOptions *msg) {
  988. return msg->cc_generic_services;
  989. }
  990. void google_protobuf_FileOptions_set_cc_generic_services(google_protobuf_FileOptions *msg, bool value) {
  991. msg->cc_generic_services = value;
  992. }
  993. bool google_protobuf_FileOptions_java_generic_services(const google_protobuf_FileOptions *msg) {
  994. return msg->java_generic_services;
  995. }
  996. void google_protobuf_FileOptions_set_java_generic_services(google_protobuf_FileOptions *msg, bool value) {
  997. msg->java_generic_services = value;
  998. }
  999. bool google_protobuf_FileOptions_py_generic_services(const google_protobuf_FileOptions *msg) {
  1000. return msg->py_generic_services;
  1001. }
  1002. void google_protobuf_FileOptions_set_py_generic_services(google_protobuf_FileOptions *msg, bool value) {
  1003. msg->py_generic_services = value;
  1004. }
  1005. bool google_protobuf_FileOptions_java_generate_equals_and_hash(const google_protobuf_FileOptions *msg) {
  1006. return msg->java_generate_equals_and_hash;
  1007. }
  1008. void google_protobuf_FileOptions_set_java_generate_equals_and_hash(google_protobuf_FileOptions *msg, bool value) {
  1009. msg->java_generate_equals_and_hash = value;
  1010. }
  1011. bool google_protobuf_FileOptions_deprecated(const google_protobuf_FileOptions *msg) {
  1012. return msg->deprecated;
  1013. }
  1014. void google_protobuf_FileOptions_set_deprecated(google_protobuf_FileOptions *msg, bool value) {
  1015. msg->deprecated = value;
  1016. }
  1017. bool google_protobuf_FileOptions_java_string_check_utf8(const google_protobuf_FileOptions *msg) {
  1018. return msg->java_string_check_utf8;
  1019. }
  1020. void google_protobuf_FileOptions_set_java_string_check_utf8(google_protobuf_FileOptions *msg, bool value) {
  1021. msg->java_string_check_utf8 = value;
  1022. }
  1023. bool google_protobuf_FileOptions_cc_enable_arenas(const google_protobuf_FileOptions *msg) {
  1024. return msg->cc_enable_arenas;
  1025. }
  1026. void google_protobuf_FileOptions_set_cc_enable_arenas(google_protobuf_FileOptions *msg, bool value) {
  1027. msg->cc_enable_arenas = value;
  1028. }
  1029. upb_stringview google_protobuf_FileOptions_objc_class_prefix(const google_protobuf_FileOptions *msg) {
  1030. return msg->objc_class_prefix;
  1031. }
  1032. void google_protobuf_FileOptions_set_objc_class_prefix(google_protobuf_FileOptions *msg, upb_stringview value) {
  1033. msg->objc_class_prefix = value;
  1034. }
  1035. upb_stringview google_protobuf_FileOptions_csharp_namespace(const google_protobuf_FileOptions *msg) {
  1036. return msg->csharp_namespace;
  1037. }
  1038. void google_protobuf_FileOptions_set_csharp_namespace(google_protobuf_FileOptions *msg, upb_stringview value) {
  1039. msg->csharp_namespace = value;
  1040. }
  1041. upb_stringview google_protobuf_FileOptions_swift_prefix(const google_protobuf_FileOptions *msg) {
  1042. return msg->swift_prefix;
  1043. }
  1044. void google_protobuf_FileOptions_set_swift_prefix(google_protobuf_FileOptions *msg, upb_stringview value) {
  1045. msg->swift_prefix = value;
  1046. }
  1047. upb_stringview google_protobuf_FileOptions_php_class_prefix(const google_protobuf_FileOptions *msg) {
  1048. return msg->php_class_prefix;
  1049. }
  1050. void google_protobuf_FileOptions_set_php_class_prefix(google_protobuf_FileOptions *msg, upb_stringview value) {
  1051. msg->php_class_prefix = value;
  1052. }
  1053. upb_stringview google_protobuf_FileOptions_php_namespace(const google_protobuf_FileOptions *msg) {
  1054. return msg->php_namespace;
  1055. }
  1056. void google_protobuf_FileOptions_set_php_namespace(google_protobuf_FileOptions *msg, upb_stringview value) {
  1057. msg->php_namespace = value;
  1058. }
  1059. bool google_protobuf_FileOptions_php_generic_services(const google_protobuf_FileOptions *msg) {
  1060. return msg->php_generic_services;
  1061. }
  1062. void google_protobuf_FileOptions_set_php_generic_services(google_protobuf_FileOptions *msg, bool value) {
  1063. msg->php_generic_services = value;
  1064. }
  1065. const upb_array* google_protobuf_FileOptions_uninterpreted_option(const google_protobuf_FileOptions *msg) {
  1066. return msg->uninterpreted_option;
  1067. }
  1068. void google_protobuf_FileOptions_set_uninterpreted_option(google_protobuf_FileOptions *msg, upb_array* value) {
  1069. msg->uninterpreted_option = value;
  1070. }
  1071. struct google_protobuf_MessageOptions {
  1072. bool message_set_wire_format;
  1073. bool no_standard_descriptor_accessor;
  1074. bool deprecated;
  1075. bool map_entry;
  1076. upb_array* uninterpreted_option;
  1077. };
  1078. static const upb_msglayout_msginit_v1 *const google_protobuf_MessageOptions_submsgs[1] = {
  1079. &google_protobuf_UninterpretedOption_msginit,
  1080. };
  1081. static const upb_msglayout_fieldinit_v1 google_protobuf_MessageOptions__fields[5] = {
  1082. {1, offsetof(google_protobuf_MessageOptions, message_set_wire_format), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  1083. {2, offsetof(google_protobuf_MessageOptions, no_standard_descriptor_accessor), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  1084. {3, offsetof(google_protobuf_MessageOptions, deprecated), 2, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  1085. {7, offsetof(google_protobuf_MessageOptions, map_entry), 3, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  1086. {999, offsetof(google_protobuf_MessageOptions, uninterpreted_option), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3},
  1087. };
  1088. const upb_msglayout_msginit_v1 google_protobuf_MessageOptions_msginit = {
  1089. &google_protobuf_MessageOptions_submsgs[0],
  1090. &google_protobuf_MessageOptions__fields[0],
  1091. NULL,
  1092. NULL, /* TODO. default_msg */
  1093. UPB_ALIGNED_SIZEOF(google_protobuf_MessageOptions), 5, 0, false, true
  1094. };
  1095. google_protobuf_MessageOptions *google_protobuf_MessageOptions_new(upb_env *env) {
  1096. google_protobuf_MessageOptions *msg = upb_env_malloc(env, sizeof(*msg));
  1097. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  1098. return msg;
  1099. }
  1100. google_protobuf_MessageOptions *google_protobuf_MessageOptions_parsenew(upb_stringview buf, upb_env *env) {
  1101. google_protobuf_MessageOptions *msg = google_protobuf_MessageOptions_new(env);
  1102. if (upb_decode(buf, msg, &google_protobuf_MessageOptions_msginit, env)) {
  1103. return msg;
  1104. } else {
  1105. return NULL;
  1106. }
  1107. }
  1108. char *google_protobuf_MessageOptions_serialize(google_protobuf_MessageOptions *msg, upb_env *env, size_t *size) {
  1109. return upb_encode(msg, &google_protobuf_MessageOptions_msginit, env, size);
  1110. }
  1111. bool google_protobuf_MessageOptions_message_set_wire_format(const google_protobuf_MessageOptions *msg) {
  1112. return msg->message_set_wire_format;
  1113. }
  1114. void google_protobuf_MessageOptions_set_message_set_wire_format(google_protobuf_MessageOptions *msg, bool value) {
  1115. msg->message_set_wire_format = value;
  1116. }
  1117. bool google_protobuf_MessageOptions_no_standard_descriptor_accessor(const google_protobuf_MessageOptions *msg) {
  1118. return msg->no_standard_descriptor_accessor;
  1119. }
  1120. void google_protobuf_MessageOptions_set_no_standard_descriptor_accessor(google_protobuf_MessageOptions *msg, bool value) {
  1121. msg->no_standard_descriptor_accessor = value;
  1122. }
  1123. bool google_protobuf_MessageOptions_deprecated(const google_protobuf_MessageOptions *msg) {
  1124. return msg->deprecated;
  1125. }
  1126. void google_protobuf_MessageOptions_set_deprecated(google_protobuf_MessageOptions *msg, bool value) {
  1127. msg->deprecated = value;
  1128. }
  1129. bool google_protobuf_MessageOptions_map_entry(const google_protobuf_MessageOptions *msg) {
  1130. return msg->map_entry;
  1131. }
  1132. void google_protobuf_MessageOptions_set_map_entry(google_protobuf_MessageOptions *msg, bool value) {
  1133. msg->map_entry = value;
  1134. }
  1135. const upb_array* google_protobuf_MessageOptions_uninterpreted_option(const google_protobuf_MessageOptions *msg) {
  1136. return msg->uninterpreted_option;
  1137. }
  1138. void google_protobuf_MessageOptions_set_uninterpreted_option(google_protobuf_MessageOptions *msg, upb_array* value) {
  1139. msg->uninterpreted_option = value;
  1140. }
  1141. struct google_protobuf_FieldOptions {
  1142. google_protobuf_FieldOptions_CType ctype;
  1143. google_protobuf_FieldOptions_JSType jstype;
  1144. bool packed;
  1145. bool deprecated;
  1146. bool lazy;
  1147. bool weak;
  1148. upb_array* uninterpreted_option;
  1149. };
  1150. static const upb_msglayout_msginit_v1 *const google_protobuf_FieldOptions_submsgs[1] = {
  1151. &google_protobuf_UninterpretedOption_msginit,
  1152. };
  1153. static const upb_msglayout_fieldinit_v1 google_protobuf_FieldOptions__fields[7] = {
  1154. {1, offsetof(google_protobuf_FieldOptions, ctype), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 14, 1},
  1155. {2, offsetof(google_protobuf_FieldOptions, packed), 2, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  1156. {3, offsetof(google_protobuf_FieldOptions, deprecated), 3, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  1157. {5, offsetof(google_protobuf_FieldOptions, lazy), 4, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  1158. {6, offsetof(google_protobuf_FieldOptions, jstype), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 14, 1},
  1159. {10, offsetof(google_protobuf_FieldOptions, weak), 5, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  1160. {999, offsetof(google_protobuf_FieldOptions, uninterpreted_option), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3},
  1161. };
  1162. const upb_msglayout_msginit_v1 google_protobuf_FieldOptions_msginit = {
  1163. &google_protobuf_FieldOptions_submsgs[0],
  1164. &google_protobuf_FieldOptions__fields[0],
  1165. NULL,
  1166. NULL, /* TODO. default_msg */
  1167. UPB_ALIGNED_SIZEOF(google_protobuf_FieldOptions), 7, 0, false, true
  1168. };
  1169. google_protobuf_FieldOptions *google_protobuf_FieldOptions_new(upb_env *env) {
  1170. google_protobuf_FieldOptions *msg = upb_env_malloc(env, sizeof(*msg));
  1171. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  1172. return msg;
  1173. }
  1174. google_protobuf_FieldOptions *google_protobuf_FieldOptions_parsenew(upb_stringview buf, upb_env *env) {
  1175. google_protobuf_FieldOptions *msg = google_protobuf_FieldOptions_new(env);
  1176. if (upb_decode(buf, msg, &google_protobuf_FieldOptions_msginit, env)) {
  1177. return msg;
  1178. } else {
  1179. return NULL;
  1180. }
  1181. }
  1182. char *google_protobuf_FieldOptions_serialize(google_protobuf_FieldOptions *msg, upb_env *env, size_t *size) {
  1183. return upb_encode(msg, &google_protobuf_FieldOptions_msginit, env, size);
  1184. }
  1185. google_protobuf_FieldOptions_CType google_protobuf_FieldOptions_ctype(const google_protobuf_FieldOptions *msg) {
  1186. return msg->ctype;
  1187. }
  1188. void google_protobuf_FieldOptions_set_ctype(google_protobuf_FieldOptions *msg, google_protobuf_FieldOptions_CType value) {
  1189. msg->ctype = value;
  1190. }
  1191. bool google_protobuf_FieldOptions_packed(const google_protobuf_FieldOptions *msg) {
  1192. return msg->packed;
  1193. }
  1194. void google_protobuf_FieldOptions_set_packed(google_protobuf_FieldOptions *msg, bool value) {
  1195. msg->packed = value;
  1196. }
  1197. bool google_protobuf_FieldOptions_deprecated(const google_protobuf_FieldOptions *msg) {
  1198. return msg->deprecated;
  1199. }
  1200. void google_protobuf_FieldOptions_set_deprecated(google_protobuf_FieldOptions *msg, bool value) {
  1201. msg->deprecated = value;
  1202. }
  1203. bool google_protobuf_FieldOptions_lazy(const google_protobuf_FieldOptions *msg) {
  1204. return msg->lazy;
  1205. }
  1206. void google_protobuf_FieldOptions_set_lazy(google_protobuf_FieldOptions *msg, bool value) {
  1207. msg->lazy = value;
  1208. }
  1209. google_protobuf_FieldOptions_JSType google_protobuf_FieldOptions_jstype(const google_protobuf_FieldOptions *msg) {
  1210. return msg->jstype;
  1211. }
  1212. void google_protobuf_FieldOptions_set_jstype(google_protobuf_FieldOptions *msg, google_protobuf_FieldOptions_JSType value) {
  1213. msg->jstype = value;
  1214. }
  1215. bool google_protobuf_FieldOptions_weak(const google_protobuf_FieldOptions *msg) {
  1216. return msg->weak;
  1217. }
  1218. void google_protobuf_FieldOptions_set_weak(google_protobuf_FieldOptions *msg, bool value) {
  1219. msg->weak = value;
  1220. }
  1221. const upb_array* google_protobuf_FieldOptions_uninterpreted_option(const google_protobuf_FieldOptions *msg) {
  1222. return msg->uninterpreted_option;
  1223. }
  1224. void google_protobuf_FieldOptions_set_uninterpreted_option(google_protobuf_FieldOptions *msg, upb_array* value) {
  1225. msg->uninterpreted_option = value;
  1226. }
  1227. struct google_protobuf_OneofOptions {
  1228. upb_array* uninterpreted_option;
  1229. };
  1230. static const upb_msglayout_msginit_v1 *const google_protobuf_OneofOptions_submsgs[1] = {
  1231. &google_protobuf_UninterpretedOption_msginit,
  1232. };
  1233. static const upb_msglayout_fieldinit_v1 google_protobuf_OneofOptions__fields[1] = {
  1234. {999, offsetof(google_protobuf_OneofOptions, uninterpreted_option), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3},
  1235. };
  1236. const upb_msglayout_msginit_v1 google_protobuf_OneofOptions_msginit = {
  1237. &google_protobuf_OneofOptions_submsgs[0],
  1238. &google_protobuf_OneofOptions__fields[0],
  1239. NULL,
  1240. NULL, /* TODO. default_msg */
  1241. UPB_ALIGNED_SIZEOF(google_protobuf_OneofOptions), 1, 0, false, true
  1242. };
  1243. google_protobuf_OneofOptions *google_protobuf_OneofOptions_new(upb_env *env) {
  1244. google_protobuf_OneofOptions *msg = upb_env_malloc(env, sizeof(*msg));
  1245. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  1246. return msg;
  1247. }
  1248. google_protobuf_OneofOptions *google_protobuf_OneofOptions_parsenew(upb_stringview buf, upb_env *env) {
  1249. google_protobuf_OneofOptions *msg = google_protobuf_OneofOptions_new(env);
  1250. if (upb_decode(buf, msg, &google_protobuf_OneofOptions_msginit, env)) {
  1251. return msg;
  1252. } else {
  1253. return NULL;
  1254. }
  1255. }
  1256. char *google_protobuf_OneofOptions_serialize(google_protobuf_OneofOptions *msg, upb_env *env, size_t *size) {
  1257. return upb_encode(msg, &google_protobuf_OneofOptions_msginit, env, size);
  1258. }
  1259. const upb_array* google_protobuf_OneofOptions_uninterpreted_option(const google_protobuf_OneofOptions *msg) {
  1260. return msg->uninterpreted_option;
  1261. }
  1262. void google_protobuf_OneofOptions_set_uninterpreted_option(google_protobuf_OneofOptions *msg, upb_array* value) {
  1263. msg->uninterpreted_option = value;
  1264. }
  1265. struct google_protobuf_EnumOptions {
  1266. bool allow_alias;
  1267. bool deprecated;
  1268. upb_array* uninterpreted_option;
  1269. };
  1270. static const upb_msglayout_msginit_v1 *const google_protobuf_EnumOptions_submsgs[1] = {
  1271. &google_protobuf_UninterpretedOption_msginit,
  1272. };
  1273. static const upb_msglayout_fieldinit_v1 google_protobuf_EnumOptions__fields[3] = {
  1274. {2, offsetof(google_protobuf_EnumOptions, allow_alias), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  1275. {3, offsetof(google_protobuf_EnumOptions, deprecated), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  1276. {999, offsetof(google_protobuf_EnumOptions, uninterpreted_option), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3},
  1277. };
  1278. const upb_msglayout_msginit_v1 google_protobuf_EnumOptions_msginit = {
  1279. &google_protobuf_EnumOptions_submsgs[0],
  1280. &google_protobuf_EnumOptions__fields[0],
  1281. NULL,
  1282. NULL, /* TODO. default_msg */
  1283. UPB_ALIGNED_SIZEOF(google_protobuf_EnumOptions), 3, 0, false, true
  1284. };
  1285. google_protobuf_EnumOptions *google_protobuf_EnumOptions_new(upb_env *env) {
  1286. google_protobuf_EnumOptions *msg = upb_env_malloc(env, sizeof(*msg));
  1287. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  1288. return msg;
  1289. }
  1290. google_protobuf_EnumOptions *google_protobuf_EnumOptions_parsenew(upb_stringview buf, upb_env *env) {
  1291. google_protobuf_EnumOptions *msg = google_protobuf_EnumOptions_new(env);
  1292. if (upb_decode(buf, msg, &google_protobuf_EnumOptions_msginit, env)) {
  1293. return msg;
  1294. } else {
  1295. return NULL;
  1296. }
  1297. }
  1298. char *google_protobuf_EnumOptions_serialize(google_protobuf_EnumOptions *msg, upb_env *env, size_t *size) {
  1299. return upb_encode(msg, &google_protobuf_EnumOptions_msginit, env, size);
  1300. }
  1301. bool google_protobuf_EnumOptions_allow_alias(const google_protobuf_EnumOptions *msg) {
  1302. return msg->allow_alias;
  1303. }
  1304. void google_protobuf_EnumOptions_set_allow_alias(google_protobuf_EnumOptions *msg, bool value) {
  1305. msg->allow_alias = value;
  1306. }
  1307. bool google_protobuf_EnumOptions_deprecated(const google_protobuf_EnumOptions *msg) {
  1308. return msg->deprecated;
  1309. }
  1310. void google_protobuf_EnumOptions_set_deprecated(google_protobuf_EnumOptions *msg, bool value) {
  1311. msg->deprecated = value;
  1312. }
  1313. const upb_array* google_protobuf_EnumOptions_uninterpreted_option(const google_protobuf_EnumOptions *msg) {
  1314. return msg->uninterpreted_option;
  1315. }
  1316. void google_protobuf_EnumOptions_set_uninterpreted_option(google_protobuf_EnumOptions *msg, upb_array* value) {
  1317. msg->uninterpreted_option = value;
  1318. }
  1319. struct google_protobuf_EnumValueOptions {
  1320. bool deprecated;
  1321. upb_array* uninterpreted_option;
  1322. };
  1323. static const upb_msglayout_msginit_v1 *const google_protobuf_EnumValueOptions_submsgs[1] = {
  1324. &google_protobuf_UninterpretedOption_msginit,
  1325. };
  1326. static const upb_msglayout_fieldinit_v1 google_protobuf_EnumValueOptions__fields[2] = {
  1327. {1, offsetof(google_protobuf_EnumValueOptions, deprecated), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  1328. {999, offsetof(google_protobuf_EnumValueOptions, uninterpreted_option), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3},
  1329. };
  1330. const upb_msglayout_msginit_v1 google_protobuf_EnumValueOptions_msginit = {
  1331. &google_protobuf_EnumValueOptions_submsgs[0],
  1332. &google_protobuf_EnumValueOptions__fields[0],
  1333. NULL,
  1334. NULL, /* TODO. default_msg */
  1335. UPB_ALIGNED_SIZEOF(google_protobuf_EnumValueOptions), 2, 0, false, true
  1336. };
  1337. google_protobuf_EnumValueOptions *google_protobuf_EnumValueOptions_new(upb_env *env) {
  1338. google_protobuf_EnumValueOptions *msg = upb_env_malloc(env, sizeof(*msg));
  1339. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  1340. return msg;
  1341. }
  1342. google_protobuf_EnumValueOptions *google_protobuf_EnumValueOptions_parsenew(upb_stringview buf, upb_env *env) {
  1343. google_protobuf_EnumValueOptions *msg = google_protobuf_EnumValueOptions_new(env);
  1344. if (upb_decode(buf, msg, &google_protobuf_EnumValueOptions_msginit, env)) {
  1345. return msg;
  1346. } else {
  1347. return NULL;
  1348. }
  1349. }
  1350. char *google_protobuf_EnumValueOptions_serialize(google_protobuf_EnumValueOptions *msg, upb_env *env, size_t *size) {
  1351. return upb_encode(msg, &google_protobuf_EnumValueOptions_msginit, env, size);
  1352. }
  1353. bool google_protobuf_EnumValueOptions_deprecated(const google_protobuf_EnumValueOptions *msg) {
  1354. return msg->deprecated;
  1355. }
  1356. void google_protobuf_EnumValueOptions_set_deprecated(google_protobuf_EnumValueOptions *msg, bool value) {
  1357. msg->deprecated = value;
  1358. }
  1359. const upb_array* google_protobuf_EnumValueOptions_uninterpreted_option(const google_protobuf_EnumValueOptions *msg) {
  1360. return msg->uninterpreted_option;
  1361. }
  1362. void google_protobuf_EnumValueOptions_set_uninterpreted_option(google_protobuf_EnumValueOptions *msg, upb_array* value) {
  1363. msg->uninterpreted_option = value;
  1364. }
  1365. struct google_protobuf_ServiceOptions {
  1366. bool deprecated;
  1367. upb_array* uninterpreted_option;
  1368. };
  1369. static const upb_msglayout_msginit_v1 *const google_protobuf_ServiceOptions_submsgs[1] = {
  1370. &google_protobuf_UninterpretedOption_msginit,
  1371. };
  1372. static const upb_msglayout_fieldinit_v1 google_protobuf_ServiceOptions__fields[2] = {
  1373. {33, offsetof(google_protobuf_ServiceOptions, deprecated), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  1374. {999, offsetof(google_protobuf_ServiceOptions, uninterpreted_option), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3},
  1375. };
  1376. const upb_msglayout_msginit_v1 google_protobuf_ServiceOptions_msginit = {
  1377. &google_protobuf_ServiceOptions_submsgs[0],
  1378. &google_protobuf_ServiceOptions__fields[0],
  1379. NULL,
  1380. NULL, /* TODO. default_msg */
  1381. UPB_ALIGNED_SIZEOF(google_protobuf_ServiceOptions), 2, 0, false, true
  1382. };
  1383. google_protobuf_ServiceOptions *google_protobuf_ServiceOptions_new(upb_env *env) {
  1384. google_protobuf_ServiceOptions *msg = upb_env_malloc(env, sizeof(*msg));
  1385. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  1386. return msg;
  1387. }
  1388. google_protobuf_ServiceOptions *google_protobuf_ServiceOptions_parsenew(upb_stringview buf, upb_env *env) {
  1389. google_protobuf_ServiceOptions *msg = google_protobuf_ServiceOptions_new(env);
  1390. if (upb_decode(buf, msg, &google_protobuf_ServiceOptions_msginit, env)) {
  1391. return msg;
  1392. } else {
  1393. return NULL;
  1394. }
  1395. }
  1396. char *google_protobuf_ServiceOptions_serialize(google_protobuf_ServiceOptions *msg, upb_env *env, size_t *size) {
  1397. return upb_encode(msg, &google_protobuf_ServiceOptions_msginit, env, size);
  1398. }
  1399. bool google_protobuf_ServiceOptions_deprecated(const google_protobuf_ServiceOptions *msg) {
  1400. return msg->deprecated;
  1401. }
  1402. void google_protobuf_ServiceOptions_set_deprecated(google_protobuf_ServiceOptions *msg, bool value) {
  1403. msg->deprecated = value;
  1404. }
  1405. const upb_array* google_protobuf_ServiceOptions_uninterpreted_option(const google_protobuf_ServiceOptions *msg) {
  1406. return msg->uninterpreted_option;
  1407. }
  1408. void google_protobuf_ServiceOptions_set_uninterpreted_option(google_protobuf_ServiceOptions *msg, upb_array* value) {
  1409. msg->uninterpreted_option = value;
  1410. }
  1411. struct google_protobuf_MethodOptions {
  1412. google_protobuf_MethodOptions_IdempotencyLevel idempotency_level;
  1413. bool deprecated;
  1414. upb_array* uninterpreted_option;
  1415. };
  1416. static const upb_msglayout_msginit_v1 *const google_protobuf_MethodOptions_submsgs[1] = {
  1417. &google_protobuf_UninterpretedOption_msginit,
  1418. };
  1419. static const upb_msglayout_fieldinit_v1 google_protobuf_MethodOptions__fields[3] = {
  1420. {33, offsetof(google_protobuf_MethodOptions, deprecated), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 1},
  1421. {34, offsetof(google_protobuf_MethodOptions, idempotency_level), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 14, 1},
  1422. {999, offsetof(google_protobuf_MethodOptions, uninterpreted_option), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3},
  1423. };
  1424. const upb_msglayout_msginit_v1 google_protobuf_MethodOptions_msginit = {
  1425. &google_protobuf_MethodOptions_submsgs[0],
  1426. &google_protobuf_MethodOptions__fields[0],
  1427. NULL,
  1428. NULL, /* TODO. default_msg */
  1429. UPB_ALIGNED_SIZEOF(google_protobuf_MethodOptions), 3, 0, false, true
  1430. };
  1431. google_protobuf_MethodOptions *google_protobuf_MethodOptions_new(upb_env *env) {
  1432. google_protobuf_MethodOptions *msg = upb_env_malloc(env, sizeof(*msg));
  1433. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  1434. return msg;
  1435. }
  1436. google_protobuf_MethodOptions *google_protobuf_MethodOptions_parsenew(upb_stringview buf, upb_env *env) {
  1437. google_protobuf_MethodOptions *msg = google_protobuf_MethodOptions_new(env);
  1438. if (upb_decode(buf, msg, &google_protobuf_MethodOptions_msginit, env)) {
  1439. return msg;
  1440. } else {
  1441. return NULL;
  1442. }
  1443. }
  1444. char *google_protobuf_MethodOptions_serialize(google_protobuf_MethodOptions *msg, upb_env *env, size_t *size) {
  1445. return upb_encode(msg, &google_protobuf_MethodOptions_msginit, env, size);
  1446. }
  1447. bool google_protobuf_MethodOptions_deprecated(const google_protobuf_MethodOptions *msg) {
  1448. return msg->deprecated;
  1449. }
  1450. void google_protobuf_MethodOptions_set_deprecated(google_protobuf_MethodOptions *msg, bool value) {
  1451. msg->deprecated = value;
  1452. }
  1453. google_protobuf_MethodOptions_IdempotencyLevel google_protobuf_MethodOptions_idempotency_level(const google_protobuf_MethodOptions *msg) {
  1454. return msg->idempotency_level;
  1455. }
  1456. void google_protobuf_MethodOptions_set_idempotency_level(google_protobuf_MethodOptions *msg, google_protobuf_MethodOptions_IdempotencyLevel value) {
  1457. msg->idempotency_level = value;
  1458. }
  1459. const upb_array* google_protobuf_MethodOptions_uninterpreted_option(const google_protobuf_MethodOptions *msg) {
  1460. return msg->uninterpreted_option;
  1461. }
  1462. void google_protobuf_MethodOptions_set_uninterpreted_option(google_protobuf_MethodOptions *msg, upb_array* value) {
  1463. msg->uninterpreted_option = value;
  1464. }
  1465. struct google_protobuf_UninterpretedOption {
  1466. uint64_t positive_int_value;
  1467. int64_t negative_int_value;
  1468. double double_value;
  1469. upb_stringview identifier_value;
  1470. upb_stringview string_value;
  1471. upb_stringview aggregate_value;
  1472. upb_array* name;
  1473. };
  1474. static const upb_msglayout_msginit_v1 *const google_protobuf_UninterpretedOption_submsgs[1] = {
  1475. &google_protobuf_UninterpretedOption_NamePart_msginit,
  1476. };
  1477. static const upb_msglayout_fieldinit_v1 google_protobuf_UninterpretedOption__fields[7] = {
  1478. {2, offsetof(google_protobuf_UninterpretedOption, name), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3},
  1479. {3, offsetof(google_protobuf_UninterpretedOption, identifier_value), 3, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  1480. {4, offsetof(google_protobuf_UninterpretedOption, positive_int_value), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 4, 1},
  1481. {5, offsetof(google_protobuf_UninterpretedOption, negative_int_value), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 3, 1},
  1482. {6, offsetof(google_protobuf_UninterpretedOption, double_value), 2, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 1, 1},
  1483. {7, offsetof(google_protobuf_UninterpretedOption, string_value), 4, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 12, 1},
  1484. {8, offsetof(google_protobuf_UninterpretedOption, aggregate_value), 5, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  1485. };
  1486. const upb_msglayout_msginit_v1 google_protobuf_UninterpretedOption_msginit = {
  1487. &google_protobuf_UninterpretedOption_submsgs[0],
  1488. &google_protobuf_UninterpretedOption__fields[0],
  1489. NULL,
  1490. NULL, /* TODO. default_msg */
  1491. UPB_ALIGNED_SIZEOF(google_protobuf_UninterpretedOption), 7, 0, false, true
  1492. };
  1493. google_protobuf_UninterpretedOption *google_protobuf_UninterpretedOption_new(upb_env *env) {
  1494. google_protobuf_UninterpretedOption *msg = upb_env_malloc(env, sizeof(*msg));
  1495. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  1496. return msg;
  1497. }
  1498. google_protobuf_UninterpretedOption *google_protobuf_UninterpretedOption_parsenew(upb_stringview buf, upb_env *env) {
  1499. google_protobuf_UninterpretedOption *msg = google_protobuf_UninterpretedOption_new(env);
  1500. if (upb_decode(buf, msg, &google_protobuf_UninterpretedOption_msginit, env)) {
  1501. return msg;
  1502. } else {
  1503. return NULL;
  1504. }
  1505. }
  1506. char *google_protobuf_UninterpretedOption_serialize(google_protobuf_UninterpretedOption *msg, upb_env *env, size_t *size) {
  1507. return upb_encode(msg, &google_protobuf_UninterpretedOption_msginit, env, size);
  1508. }
  1509. const upb_array* google_protobuf_UninterpretedOption_name(const google_protobuf_UninterpretedOption *msg) {
  1510. return msg->name;
  1511. }
  1512. void google_protobuf_UninterpretedOption_set_name(google_protobuf_UninterpretedOption *msg, upb_array* value) {
  1513. msg->name = value;
  1514. }
  1515. upb_stringview google_protobuf_UninterpretedOption_identifier_value(const google_protobuf_UninterpretedOption *msg) {
  1516. return msg->identifier_value;
  1517. }
  1518. void google_protobuf_UninterpretedOption_set_identifier_value(google_protobuf_UninterpretedOption *msg, upb_stringview value) {
  1519. msg->identifier_value = value;
  1520. }
  1521. uint64_t google_protobuf_UninterpretedOption_positive_int_value(const google_protobuf_UninterpretedOption *msg) {
  1522. return msg->positive_int_value;
  1523. }
  1524. void google_protobuf_UninterpretedOption_set_positive_int_value(google_protobuf_UninterpretedOption *msg, uint64_t value) {
  1525. msg->positive_int_value = value;
  1526. }
  1527. int64_t google_protobuf_UninterpretedOption_negative_int_value(const google_protobuf_UninterpretedOption *msg) {
  1528. return msg->negative_int_value;
  1529. }
  1530. void google_protobuf_UninterpretedOption_set_negative_int_value(google_protobuf_UninterpretedOption *msg, int64_t value) {
  1531. msg->negative_int_value = value;
  1532. }
  1533. double google_protobuf_UninterpretedOption_double_value(const google_protobuf_UninterpretedOption *msg) {
  1534. return msg->double_value;
  1535. }
  1536. void google_protobuf_UninterpretedOption_set_double_value(google_protobuf_UninterpretedOption *msg, double value) {
  1537. msg->double_value = value;
  1538. }
  1539. upb_stringview google_protobuf_UninterpretedOption_string_value(const google_protobuf_UninterpretedOption *msg) {
  1540. return msg->string_value;
  1541. }
  1542. void google_protobuf_UninterpretedOption_set_string_value(google_protobuf_UninterpretedOption *msg, upb_stringview value) {
  1543. msg->string_value = value;
  1544. }
  1545. upb_stringview google_protobuf_UninterpretedOption_aggregate_value(const google_protobuf_UninterpretedOption *msg) {
  1546. return msg->aggregate_value;
  1547. }
  1548. void google_protobuf_UninterpretedOption_set_aggregate_value(google_protobuf_UninterpretedOption *msg, upb_stringview value) {
  1549. msg->aggregate_value = value;
  1550. }
  1551. struct google_protobuf_UninterpretedOption_NamePart {
  1552. bool is_extension;
  1553. upb_stringview name_part;
  1554. };
  1555. static const upb_msglayout_fieldinit_v1 google_protobuf_UninterpretedOption_NamePart__fields[2] = {
  1556. {1, offsetof(google_protobuf_UninterpretedOption_NamePart, name_part), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 2},
  1557. {2, offsetof(google_protobuf_UninterpretedOption_NamePart, is_extension), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 8, 2},
  1558. };
  1559. const upb_msglayout_msginit_v1 google_protobuf_UninterpretedOption_NamePart_msginit = {
  1560. NULL,
  1561. &google_protobuf_UninterpretedOption_NamePart__fields[0],
  1562. NULL,
  1563. NULL, /* TODO. default_msg */
  1564. UPB_ALIGNED_SIZEOF(google_protobuf_UninterpretedOption_NamePart), 2, 0, false, true
  1565. };
  1566. google_protobuf_UninterpretedOption_NamePart *google_protobuf_UninterpretedOption_NamePart_new(upb_env *env) {
  1567. google_protobuf_UninterpretedOption_NamePart *msg = upb_env_malloc(env, sizeof(*msg));
  1568. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  1569. return msg;
  1570. }
  1571. google_protobuf_UninterpretedOption_NamePart *google_protobuf_UninterpretedOption_NamePart_parsenew(upb_stringview buf, upb_env *env) {
  1572. google_protobuf_UninterpretedOption_NamePart *msg = google_protobuf_UninterpretedOption_NamePart_new(env);
  1573. if (upb_decode(buf, msg, &google_protobuf_UninterpretedOption_NamePart_msginit, env)) {
  1574. return msg;
  1575. } else {
  1576. return NULL;
  1577. }
  1578. }
  1579. char *google_protobuf_UninterpretedOption_NamePart_serialize(google_protobuf_UninterpretedOption_NamePart *msg, upb_env *env, size_t *size) {
  1580. return upb_encode(msg, &google_protobuf_UninterpretedOption_NamePart_msginit, env, size);
  1581. }
  1582. upb_stringview google_protobuf_UninterpretedOption_NamePart_name_part(const google_protobuf_UninterpretedOption_NamePart *msg) {
  1583. return msg->name_part;
  1584. }
  1585. void google_protobuf_UninterpretedOption_NamePart_set_name_part(google_protobuf_UninterpretedOption_NamePart *msg, upb_stringview value) {
  1586. msg->name_part = value;
  1587. }
  1588. bool google_protobuf_UninterpretedOption_NamePart_is_extension(const google_protobuf_UninterpretedOption_NamePart *msg) {
  1589. return msg->is_extension;
  1590. }
  1591. void google_protobuf_UninterpretedOption_NamePart_set_is_extension(google_protobuf_UninterpretedOption_NamePart *msg, bool value) {
  1592. msg->is_extension = value;
  1593. }
  1594. struct google_protobuf_SourceCodeInfo {
  1595. upb_array* location;
  1596. };
  1597. static const upb_msglayout_msginit_v1 *const google_protobuf_SourceCodeInfo_submsgs[1] = {
  1598. &google_protobuf_SourceCodeInfo_Location_msginit,
  1599. };
  1600. static const upb_msglayout_fieldinit_v1 google_protobuf_SourceCodeInfo__fields[1] = {
  1601. {1, offsetof(google_protobuf_SourceCodeInfo, location), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3},
  1602. };
  1603. const upb_msglayout_msginit_v1 google_protobuf_SourceCodeInfo_msginit = {
  1604. &google_protobuf_SourceCodeInfo_submsgs[0],
  1605. &google_protobuf_SourceCodeInfo__fields[0],
  1606. NULL,
  1607. NULL, /* TODO. default_msg */
  1608. UPB_ALIGNED_SIZEOF(google_protobuf_SourceCodeInfo), 1, 0, false, true
  1609. };
  1610. google_protobuf_SourceCodeInfo *google_protobuf_SourceCodeInfo_new(upb_env *env) {
  1611. google_protobuf_SourceCodeInfo *msg = upb_env_malloc(env, sizeof(*msg));
  1612. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  1613. return msg;
  1614. }
  1615. google_protobuf_SourceCodeInfo *google_protobuf_SourceCodeInfo_parsenew(upb_stringview buf, upb_env *env) {
  1616. google_protobuf_SourceCodeInfo *msg = google_protobuf_SourceCodeInfo_new(env);
  1617. if (upb_decode(buf, msg, &google_protobuf_SourceCodeInfo_msginit, env)) {
  1618. return msg;
  1619. } else {
  1620. return NULL;
  1621. }
  1622. }
  1623. char *google_protobuf_SourceCodeInfo_serialize(google_protobuf_SourceCodeInfo *msg, upb_env *env, size_t *size) {
  1624. return upb_encode(msg, &google_protobuf_SourceCodeInfo_msginit, env, size);
  1625. }
  1626. const upb_array* google_protobuf_SourceCodeInfo_location(const google_protobuf_SourceCodeInfo *msg) {
  1627. return msg->location;
  1628. }
  1629. void google_protobuf_SourceCodeInfo_set_location(google_protobuf_SourceCodeInfo *msg, upb_array* value) {
  1630. msg->location = value;
  1631. }
  1632. struct google_protobuf_SourceCodeInfo_Location {
  1633. upb_stringview leading_comments;
  1634. upb_stringview trailing_comments;
  1635. upb_array* path;
  1636. upb_array* span;
  1637. upb_array* leading_detached_comments;
  1638. };
  1639. static const upb_msglayout_fieldinit_v1 google_protobuf_SourceCodeInfo_Location__fields[5] = {
  1640. {1, offsetof(google_protobuf_SourceCodeInfo_Location, path), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 3},
  1641. {2, offsetof(google_protobuf_SourceCodeInfo_Location, span), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 3},
  1642. {3, offsetof(google_protobuf_SourceCodeInfo_Location, leading_comments), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  1643. {4, offsetof(google_protobuf_SourceCodeInfo_Location, trailing_comments), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  1644. {6, offsetof(google_protobuf_SourceCodeInfo_Location, leading_detached_comments), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 3},
  1645. };
  1646. const upb_msglayout_msginit_v1 google_protobuf_SourceCodeInfo_Location_msginit = {
  1647. NULL,
  1648. &google_protobuf_SourceCodeInfo_Location__fields[0],
  1649. NULL,
  1650. NULL, /* TODO. default_msg */
  1651. UPB_ALIGNED_SIZEOF(google_protobuf_SourceCodeInfo_Location), 5, 0, false, true
  1652. };
  1653. google_protobuf_SourceCodeInfo_Location *google_protobuf_SourceCodeInfo_Location_new(upb_env *env) {
  1654. google_protobuf_SourceCodeInfo_Location *msg = upb_env_malloc(env, sizeof(*msg));
  1655. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  1656. return msg;
  1657. }
  1658. google_protobuf_SourceCodeInfo_Location *google_protobuf_SourceCodeInfo_Location_parsenew(upb_stringview buf, upb_env *env) {
  1659. google_protobuf_SourceCodeInfo_Location *msg = google_protobuf_SourceCodeInfo_Location_new(env);
  1660. if (upb_decode(buf, msg, &google_protobuf_SourceCodeInfo_Location_msginit, env)) {
  1661. return msg;
  1662. } else {
  1663. return NULL;
  1664. }
  1665. }
  1666. char *google_protobuf_SourceCodeInfo_Location_serialize(google_protobuf_SourceCodeInfo_Location *msg, upb_env *env, size_t *size) {
  1667. return upb_encode(msg, &google_protobuf_SourceCodeInfo_Location_msginit, env, size);
  1668. }
  1669. const upb_array* google_protobuf_SourceCodeInfo_Location_path(const google_protobuf_SourceCodeInfo_Location *msg) {
  1670. return msg->path;
  1671. }
  1672. void google_protobuf_SourceCodeInfo_Location_set_path(google_protobuf_SourceCodeInfo_Location *msg, upb_array* value) {
  1673. msg->path = value;
  1674. }
  1675. const upb_array* google_protobuf_SourceCodeInfo_Location_span(const google_protobuf_SourceCodeInfo_Location *msg) {
  1676. return msg->span;
  1677. }
  1678. void google_protobuf_SourceCodeInfo_Location_set_span(google_protobuf_SourceCodeInfo_Location *msg, upb_array* value) {
  1679. msg->span = value;
  1680. }
  1681. upb_stringview google_protobuf_SourceCodeInfo_Location_leading_comments(const google_protobuf_SourceCodeInfo_Location *msg) {
  1682. return msg->leading_comments;
  1683. }
  1684. void google_protobuf_SourceCodeInfo_Location_set_leading_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_stringview value) {
  1685. msg->leading_comments = value;
  1686. }
  1687. upb_stringview google_protobuf_SourceCodeInfo_Location_trailing_comments(const google_protobuf_SourceCodeInfo_Location *msg) {
  1688. return msg->trailing_comments;
  1689. }
  1690. void google_protobuf_SourceCodeInfo_Location_set_trailing_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_stringview value) {
  1691. msg->trailing_comments = value;
  1692. }
  1693. const upb_array* google_protobuf_SourceCodeInfo_Location_leading_detached_comments(const google_protobuf_SourceCodeInfo_Location *msg) {
  1694. return msg->leading_detached_comments;
  1695. }
  1696. void google_protobuf_SourceCodeInfo_Location_set_leading_detached_comments(google_protobuf_SourceCodeInfo_Location *msg, upb_array* value) {
  1697. msg->leading_detached_comments = value;
  1698. }
  1699. struct google_protobuf_GeneratedCodeInfo {
  1700. upb_array* annotation;
  1701. };
  1702. static const upb_msglayout_msginit_v1 *const google_protobuf_GeneratedCodeInfo_submsgs[1] = {
  1703. &google_protobuf_GeneratedCodeInfo_Annotation_msginit,
  1704. };
  1705. static const upb_msglayout_fieldinit_v1 google_protobuf_GeneratedCodeInfo__fields[1] = {
  1706. {1, offsetof(google_protobuf_GeneratedCodeInfo, annotation), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, 0, 11, 3},
  1707. };
  1708. const upb_msglayout_msginit_v1 google_protobuf_GeneratedCodeInfo_msginit = {
  1709. &google_protobuf_GeneratedCodeInfo_submsgs[0],
  1710. &google_protobuf_GeneratedCodeInfo__fields[0],
  1711. NULL,
  1712. NULL, /* TODO. default_msg */
  1713. UPB_ALIGNED_SIZEOF(google_protobuf_GeneratedCodeInfo), 1, 0, false, true
  1714. };
  1715. google_protobuf_GeneratedCodeInfo *google_protobuf_GeneratedCodeInfo_new(upb_env *env) {
  1716. google_protobuf_GeneratedCodeInfo *msg = upb_env_malloc(env, sizeof(*msg));
  1717. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  1718. return msg;
  1719. }
  1720. google_protobuf_GeneratedCodeInfo *google_protobuf_GeneratedCodeInfo_parsenew(upb_stringview buf, upb_env *env) {
  1721. google_protobuf_GeneratedCodeInfo *msg = google_protobuf_GeneratedCodeInfo_new(env);
  1722. if (upb_decode(buf, msg, &google_protobuf_GeneratedCodeInfo_msginit, env)) {
  1723. return msg;
  1724. } else {
  1725. return NULL;
  1726. }
  1727. }
  1728. char *google_protobuf_GeneratedCodeInfo_serialize(google_protobuf_GeneratedCodeInfo *msg, upb_env *env, size_t *size) {
  1729. return upb_encode(msg, &google_protobuf_GeneratedCodeInfo_msginit, env, size);
  1730. }
  1731. const upb_array* google_protobuf_GeneratedCodeInfo_annotation(const google_protobuf_GeneratedCodeInfo *msg) {
  1732. return msg->annotation;
  1733. }
  1734. void google_protobuf_GeneratedCodeInfo_set_annotation(google_protobuf_GeneratedCodeInfo *msg, upb_array* value) {
  1735. msg->annotation = value;
  1736. }
  1737. struct google_protobuf_GeneratedCodeInfo_Annotation {
  1738. int32_t begin;
  1739. int32_t end;
  1740. upb_stringview source_file;
  1741. upb_array* path;
  1742. };
  1743. static const upb_msglayout_fieldinit_v1 google_protobuf_GeneratedCodeInfo_Annotation__fields[4] = {
  1744. {1, offsetof(google_protobuf_GeneratedCodeInfo_Annotation, path), UPB_NO_HASBIT, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 3},
  1745. {2, offsetof(google_protobuf_GeneratedCodeInfo_Annotation, source_file), 2, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 9, 1},
  1746. {3, offsetof(google_protobuf_GeneratedCodeInfo_Annotation, begin), 0, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 1},
  1747. {4, offsetof(google_protobuf_GeneratedCodeInfo_Annotation, end), 1, UPB_NOT_IN_ONEOF, UPB_NO_SUBMSG, 5, 1},
  1748. };
  1749. const upb_msglayout_msginit_v1 google_protobuf_GeneratedCodeInfo_Annotation_msginit = {
  1750. NULL,
  1751. &google_protobuf_GeneratedCodeInfo_Annotation__fields[0],
  1752. NULL,
  1753. NULL, /* TODO. default_msg */
  1754. UPB_ALIGNED_SIZEOF(google_protobuf_GeneratedCodeInfo_Annotation), 4, 0, false, true
  1755. };
  1756. google_protobuf_GeneratedCodeInfo_Annotation *google_protobuf_GeneratedCodeInfo_Annotation_new(upb_env *env) {
  1757. google_protobuf_GeneratedCodeInfo_Annotation *msg = upb_env_malloc(env, sizeof(*msg));
  1758. memset(msg, 0, sizeof(*msg)); /* TODO: defaults */
  1759. return msg;
  1760. }
  1761. google_protobuf_GeneratedCodeInfo_Annotation *google_protobuf_GeneratedCodeInfo_Annotation_parsenew(upb_stringview buf, upb_env *env) {
  1762. google_protobuf_GeneratedCodeInfo_Annotation *msg = google_protobuf_GeneratedCodeInfo_Annotation_new(env);
  1763. if (upb_decode(buf, msg, &google_protobuf_GeneratedCodeInfo_Annotation_msginit, env)) {
  1764. return msg;
  1765. } else {
  1766. return NULL;
  1767. }
  1768. }
  1769. char *google_protobuf_GeneratedCodeInfo_Annotation_serialize(google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_env *env, size_t *size) {
  1770. return upb_encode(msg, &google_protobuf_GeneratedCodeInfo_Annotation_msginit, env, size);
  1771. }
  1772. const upb_array* google_protobuf_GeneratedCodeInfo_Annotation_path(const google_protobuf_GeneratedCodeInfo_Annotation *msg) {
  1773. return msg->path;
  1774. }
  1775. void google_protobuf_GeneratedCodeInfo_Annotation_set_path(google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_array* value) {
  1776. msg->path = value;
  1777. }
  1778. upb_stringview google_protobuf_GeneratedCodeInfo_Annotation_source_file(const google_protobuf_GeneratedCodeInfo_Annotation *msg) {
  1779. return msg->source_file;
  1780. }
  1781. void google_protobuf_GeneratedCodeInfo_Annotation_set_source_file(google_protobuf_GeneratedCodeInfo_Annotation *msg, upb_stringview value) {
  1782. msg->source_file = value;
  1783. }
  1784. int32_t google_protobuf_GeneratedCodeInfo_Annotation_begin(const google_protobuf_GeneratedCodeInfo_Annotation *msg) {
  1785. return msg->begin;
  1786. }
  1787. void google_protobuf_GeneratedCodeInfo_Annotation_set_begin(google_protobuf_GeneratedCodeInfo_Annotation *msg, int32_t value) {
  1788. msg->begin = value;
  1789. }
  1790. int32_t google_protobuf_GeneratedCodeInfo_Annotation_end(const google_protobuf_GeneratedCodeInfo_Annotation *msg) {
  1791. return msg->end;
  1792. }
  1793. void google_protobuf_GeneratedCodeInfo_Annotation_set_end(google_protobuf_GeneratedCodeInfo_Annotation *msg, int32_t value) {
  1794. msg->end = value;
  1795. }
  1796. /* Maps descriptor type -> upb field type. */
  1797. const uint8_t upb_desctype_to_fieldtype[] = {
  1798. UPB_WIRE_TYPE_END_GROUP, /* ENDGROUP */
  1799. UPB_TYPE_DOUBLE, /* DOUBLE */
  1800. UPB_TYPE_FLOAT, /* FLOAT */
  1801. UPB_TYPE_INT64, /* INT64 */
  1802. UPB_TYPE_UINT64, /* UINT64 */
  1803. UPB_TYPE_INT32, /* INT32 */
  1804. UPB_TYPE_UINT64, /* FIXED64 */
  1805. UPB_TYPE_UINT32, /* FIXED32 */
  1806. UPB_TYPE_BOOL, /* BOOL */
  1807. UPB_TYPE_STRING, /* STRING */
  1808. UPB_TYPE_MESSAGE, /* GROUP */
  1809. UPB_TYPE_MESSAGE, /* MESSAGE */
  1810. UPB_TYPE_BYTES, /* BYTES */
  1811. UPB_TYPE_UINT32, /* UINT32 */
  1812. UPB_TYPE_ENUM, /* ENUM */
  1813. UPB_TYPE_INT32, /* SFIXED32 */
  1814. UPB_TYPE_INT64, /* SFIXED64 */
  1815. UPB_TYPE_INT32, /* SINT32 */
  1816. UPB_TYPE_INT64, /* SINT64 */
  1817. };
  1818. /* Data pertaining to the parse. */
  1819. typedef struct {
  1820. upb_env *env;
  1821. /* Current decoding pointer. Points to the beginning of a field until we
  1822. * have finished decoding the whole field. */
  1823. const char *ptr;
  1824. } upb_decstate;
  1825. /* Data pertaining to a single message frame. */
  1826. typedef struct {
  1827. const char *limit;
  1828. int32_t group_number; /* 0 if we are not parsing a group. */
  1829. /* These members are unset for an unknown group frame. */
  1830. char *msg;
  1831. const upb_msglayout_msginit_v1 *m;
  1832. } upb_decframe;
  1833. #define CHK(x) if (!(x)) { return false; }
  1834. static bool upb_skip_unknowngroup(upb_decstate *d, int field_number,
  1835. const char *limit);
  1836. static bool upb_decode_message(upb_decstate *d, const char *limit,
  1837. int group_number, char *msg,
  1838. const upb_msglayout_msginit_v1 *l);
  1839. static bool upb_decode_varint(const char **ptr, const char *limit,
  1840. uint64_t *val) {
  1841. uint8_t byte;
  1842. int bitpos = 0;
  1843. const char *p = *ptr;
  1844. *val = 0;
  1845. do {
  1846. CHK(bitpos < 70 && p < limit);
  1847. byte = *p;
  1848. *val |= (uint64_t)(byte & 0x7F) << bitpos;
  1849. p++;
  1850. bitpos += 7;
  1851. } while (byte & 0x80);
  1852. *ptr = p;
  1853. return true;
  1854. }
  1855. static bool upb_decode_varint32(const char **ptr, const char *limit,
  1856. uint32_t *val) {
  1857. uint64_t u64;
  1858. CHK(upb_decode_varint(ptr, limit, &u64) && u64 <= UINT32_MAX);
  1859. *val = u64;
  1860. return true;
  1861. }
  1862. static bool upb_decode_64bit(const char **ptr, const char *limit,
  1863. uint64_t *val) {
  1864. CHK(limit - *ptr >= 8);
  1865. memcpy(val, *ptr, 8);
  1866. *ptr += 8;
  1867. return true;
  1868. }
  1869. static bool upb_decode_32bit(const char **ptr, const char *limit,
  1870. uint32_t *val) {
  1871. CHK(limit - *ptr >= 4);
  1872. memcpy(val, *ptr, 4);
  1873. *ptr += 4;
  1874. return true;
  1875. }
  1876. static bool upb_decode_tag(const char **ptr, const char *limit,
  1877. int *field_number, int *wire_type) {
  1878. uint32_t tag = 0;
  1879. CHK(upb_decode_varint32(ptr, limit, &tag));
  1880. *field_number = tag >> 3;
  1881. *wire_type = tag & 7;
  1882. return true;
  1883. }
  1884. static int32_t upb_zzdecode_32(uint32_t n) {
  1885. return (n >> 1) ^ -(int32_t)(n & 1);
  1886. }
  1887. static int64_t upb_zzdecode_64(uint64_t n) {
  1888. return (n >> 1) ^ -(int64_t)(n & 1);
  1889. }
  1890. static bool upb_decode_string(const char **ptr, const char *limit,
  1891. upb_stringview *val) {
  1892. uint32_t len;
  1893. CHK(upb_decode_varint32(ptr, limit, &len) &&
  1894. len < INT32_MAX &&
  1895. limit - *ptr >= (int32_t)len);
  1896. *val = upb_stringview_make(*ptr, len);
  1897. *ptr += len;
  1898. return true;
  1899. }
  1900. static void upb_set32(void *msg, size_t ofs, uint32_t val) {
  1901. memcpy((char*)msg + ofs, &val, sizeof(val));
  1902. }
  1903. static bool upb_append_unknown(upb_decstate *d, upb_decframe *frame,
  1904. const char *start) {
  1905. UPB_UNUSED(d);
  1906. UPB_UNUSED(frame);
  1907. UPB_UNUSED(start);
  1908. return true;
  1909. }
  1910. static bool upb_skip_unknownfielddata(upb_decstate *d, upb_decframe *frame,
  1911. int field_number, int wire_type) {
  1912. switch (wire_type) {
  1913. case UPB_WIRE_TYPE_VARINT: {
  1914. uint64_t val;
  1915. return upb_decode_varint(&d->ptr, frame->limit, &val);
  1916. }
  1917. case UPB_WIRE_TYPE_32BIT: {
  1918. uint32_t val;
  1919. return upb_decode_32bit(&d->ptr, frame->limit, &val);
  1920. }
  1921. case UPB_WIRE_TYPE_64BIT: {
  1922. uint64_t val;
  1923. return upb_decode_64bit(&d->ptr, frame->limit, &val);
  1924. }
  1925. case UPB_WIRE_TYPE_DELIMITED: {
  1926. upb_stringview val;
  1927. return upb_decode_string(&d->ptr, frame->limit, &val);
  1928. }
  1929. case UPB_WIRE_TYPE_START_GROUP:
  1930. return upb_skip_unknowngroup(d, field_number, frame->limit);
  1931. case UPB_WIRE_TYPE_END_GROUP:
  1932. CHK(field_number == frame->group_number);
  1933. frame->limit = d->ptr;
  1934. return true;
  1935. }
  1936. return false;
  1937. }
  1938. static bool upb_array_grow(upb_array *arr, size_t elements) {
  1939. size_t needed = arr->len + elements;
  1940. size_t new_size = UPB_MAX(arr->size, 8);
  1941. size_t new_bytes;
  1942. size_t old_bytes;
  1943. void *new_data;
  1944. while (new_size < needed) {
  1945. new_size *= 2;
  1946. }
  1947. old_bytes = arr->len * arr->element_size;
  1948. new_bytes = new_size * arr->element_size;
  1949. new_data = upb_realloc(arr->alloc, arr->data, old_bytes, new_bytes);
  1950. CHK(new_data);
  1951. arr->data = new_data;
  1952. arr->size = new_size;
  1953. return true;
  1954. }
  1955. static void *upb_array_reserve(upb_array *arr, size_t elements) {
  1956. if (arr->size - arr->len < elements) {
  1957. CHK(upb_array_grow(arr, elements));
  1958. }
  1959. return (char*)arr->data + (arr->len * arr->element_size);
  1960. }
  1961. static void *upb_array_add(upb_array *arr, size_t elements) {
  1962. void *ret = upb_array_reserve(arr, elements);
  1963. arr->len += elements;
  1964. return ret;
  1965. }
  1966. static upb_array *upb_getarr(upb_decframe *frame,
  1967. const upb_msglayout_fieldinit_v1 *field) {
  1968. UPB_ASSERT(field->label == UPB_LABEL_REPEATED);
  1969. return *(upb_array**)&frame->msg[field->offset];
  1970. }
  1971. static upb_array *upb_getorcreatearr(upb_decstate *d,
  1972. upb_decframe *frame,
  1973. const upb_msglayout_fieldinit_v1 *field) {
  1974. upb_array *arr = upb_getarr(frame, field);
  1975. if (!arr) {
  1976. arr = upb_env_malloc(d->env, sizeof(*arr));
  1977. if (!arr) {
  1978. return NULL;
  1979. }
  1980. upb_array_init(arr, upb_desctype_to_fieldtype[field->descriptortype],
  1981. upb_arena_alloc(upb_env_arena(d->env)));
  1982. *(upb_array**)&frame->msg[field->offset] = arr;
  1983. }
  1984. return arr;
  1985. }
  1986. static void upb_sethasbit(upb_decframe *frame,
  1987. const upb_msglayout_fieldinit_v1 *field) {
  1988. UPB_ASSERT(field->hasbit != UPB_NO_HASBIT);
  1989. frame->msg[field->hasbit / 8] |= (1 << (field->hasbit % 8));
  1990. }
  1991. static void upb_setoneofcase(upb_decframe *frame,
  1992. const upb_msglayout_fieldinit_v1 *field) {
  1993. UPB_ASSERT(field->oneof_index != UPB_NOT_IN_ONEOF);
  1994. upb_set32(frame->msg, frame->m->oneofs[field->oneof_index].case_offset,
  1995. field->number);
  1996. }
  1997. static char *upb_decode_prepareslot(upb_decstate *d,
  1998. upb_decframe *frame,
  1999. const upb_msglayout_fieldinit_v1 *field) {
  2000. char *field_mem = frame->msg + field->offset;
  2001. upb_array *arr;
  2002. if (field->label == UPB_LABEL_REPEATED) {
  2003. arr = upb_getorcreatearr(d, frame, field);
  2004. field_mem = upb_array_reserve(arr, 1);
  2005. }
  2006. return field_mem;
  2007. }
  2008. static void upb_decode_setpresent(upb_decframe *frame,
  2009. const upb_msglayout_fieldinit_v1 *field) {
  2010. if (field->label == UPB_LABEL_REPEATED) {
  2011. upb_array *arr = upb_getarr(frame, field);
  2012. UPB_ASSERT(arr->len < arr->size);
  2013. arr->len++;
  2014. } else if (field->oneof_index != UPB_NOT_IN_ONEOF) {
  2015. upb_setoneofcase(frame, field);
  2016. } else if (field->hasbit != UPB_NO_HASBIT) {
  2017. upb_sethasbit(frame, field);
  2018. }
  2019. }
  2020. static bool upb_decode_submsg(upb_decstate *d,
  2021. upb_decframe *frame,
  2022. const char *limit,
  2023. const upb_msglayout_fieldinit_v1 *field,
  2024. int group_number) {
  2025. char *submsg_slot = upb_decode_prepareslot(d, frame, field);
  2026. char *submsg = *(void**)submsg_slot;
  2027. const upb_msglayout_msginit_v1 *subm;
  2028. UPB_ASSERT(field->submsg_index != UPB_NO_SUBMSG);
  2029. subm = frame->m->submsgs[field->submsg_index];
  2030. UPB_ASSERT(subm);
  2031. if (!submsg) {
  2032. submsg = upb_env_malloc(d->env, upb_msg_sizeof((upb_msglayout *)subm));
  2033. CHK(submsg);
  2034. submsg = upb_msg_init(
  2035. submsg, (upb_msglayout*)subm, upb_arena_alloc(upb_env_arena(d->env)));
  2036. *(void**)submsg_slot = submsg;
  2037. }
  2038. upb_decode_message(d, limit, group_number, submsg, subm);
  2039. return true;
  2040. }
  2041. static bool upb_decode_varintfield(upb_decstate *d, upb_decframe *frame,
  2042. const char *field_start,
  2043. const upb_msglayout_fieldinit_v1 *field) {
  2044. uint64_t val;
  2045. void *field_mem;
  2046. field_mem = upb_decode_prepareslot(d, frame, field);
  2047. CHK(field_mem);
  2048. CHK(upb_decode_varint(&d->ptr, frame->limit, &val));
  2049. switch ((upb_descriptortype_t)field->descriptortype) {
  2050. case UPB_DESCRIPTOR_TYPE_INT64:
  2051. case UPB_DESCRIPTOR_TYPE_UINT64:
  2052. memcpy(field_mem, &val, sizeof(val));
  2053. break;
  2054. case UPB_DESCRIPTOR_TYPE_INT32:
  2055. case UPB_DESCRIPTOR_TYPE_UINT32:
  2056. case UPB_DESCRIPTOR_TYPE_ENUM: {
  2057. uint32_t val32 = val;
  2058. memcpy(field_mem, &val32, sizeof(val32));
  2059. break;
  2060. }
  2061. case UPB_DESCRIPTOR_TYPE_BOOL: {
  2062. bool valbool = val != 0;
  2063. memcpy(field_mem, &valbool, sizeof(valbool));
  2064. break;
  2065. }
  2066. case UPB_DESCRIPTOR_TYPE_SINT32: {
  2067. int32_t decoded = upb_zzdecode_32(val);
  2068. memcpy(field_mem, &decoded, sizeof(decoded));
  2069. break;
  2070. }
  2071. case UPB_DESCRIPTOR_TYPE_SINT64: {
  2072. int64_t decoded = upb_zzdecode_64(val);
  2073. memcpy(field_mem, &decoded, sizeof(decoded));
  2074. break;
  2075. }
  2076. default:
  2077. return upb_append_unknown(d, frame, field_start);
  2078. }
  2079. upb_decode_setpresent(frame, field);
  2080. return true;
  2081. }
  2082. static bool upb_decode_64bitfield(upb_decstate *d, upb_decframe *frame,
  2083. const char *field_start,
  2084. const upb_msglayout_fieldinit_v1 *field) {
  2085. void *field_mem;
  2086. uint64_t val;
  2087. field_mem = upb_decode_prepareslot(d, frame, field);
  2088. CHK(field_mem);
  2089. CHK(upb_decode_64bit(&d->ptr, frame->limit, &val));
  2090. switch ((upb_descriptortype_t)field->descriptortype) {
  2091. case UPB_DESCRIPTOR_TYPE_DOUBLE:
  2092. case UPB_DESCRIPTOR_TYPE_FIXED64:
  2093. case UPB_DESCRIPTOR_TYPE_SFIXED64:
  2094. memcpy(field_mem, &val, sizeof(val));
  2095. break;
  2096. default:
  2097. return upb_append_unknown(d, frame, field_start);
  2098. }
  2099. upb_decode_setpresent(frame, field);
  2100. return true;
  2101. }
  2102. static bool upb_decode_32bitfield(upb_decstate *d, upb_decframe *frame,
  2103. const char *field_start,
  2104. const upb_msglayout_fieldinit_v1 *field) {
  2105. void *field_mem;
  2106. uint32_t val;
  2107. field_mem = upb_decode_prepareslot(d, frame, field);
  2108. CHK(field_mem);
  2109. CHK(upb_decode_32bit(&d->ptr, frame->limit, &val));
  2110. switch ((upb_descriptortype_t)field->descriptortype) {
  2111. case UPB_DESCRIPTOR_TYPE_FLOAT:
  2112. case UPB_DESCRIPTOR_TYPE_FIXED32:
  2113. case UPB_DESCRIPTOR_TYPE_SFIXED32:
  2114. memcpy(field_mem, &val, sizeof(val));
  2115. break;
  2116. default:
  2117. return upb_append_unknown(d, frame, field_start);
  2118. }
  2119. upb_decode_setpresent(frame, field);
  2120. return true;
  2121. }
  2122. static bool upb_decode_fixedpacked(upb_array *arr, upb_stringview data,
  2123. int elem_size) {
  2124. int elements = data.size / elem_size;
  2125. void *field_mem;
  2126. CHK((size_t)(elements * elem_size) == data.size);
  2127. field_mem = upb_array_add(arr, elements);
  2128. CHK(field_mem);
  2129. memcpy(field_mem, data.data, data.size);
  2130. return true;
  2131. }
  2132. static bool upb_decode_toarray(upb_decstate *d, upb_decframe *frame,
  2133. const char *field_start,
  2134. const upb_msglayout_fieldinit_v1 *field,
  2135. upb_stringview val) {
  2136. upb_array *arr = upb_getorcreatearr(d, frame, field);
  2137. #define VARINT_CASE(ctype, decode) { \
  2138. const char *ptr = val.data; \
  2139. const char *limit = ptr + val.size; \
  2140. while (ptr < limit) { \
  2141. uint64_t val; \
  2142. void *field_mem; \
  2143. ctype decoded; \
  2144. CHK(upb_decode_varint(&ptr, limit, &val)); \
  2145. decoded = (decode)(val); \
  2146. field_mem = upb_array_add(arr, 1); \
  2147. CHK(field_mem); \
  2148. memcpy(field_mem, &decoded, sizeof(ctype)); \
  2149. } \
  2150. return true; \
  2151. }
  2152. switch ((upb_descriptortype_t)field->descriptortype) {
  2153. case UPB_DESCRIPTOR_TYPE_STRING:
  2154. case UPB_DESCRIPTOR_TYPE_BYTES: {
  2155. void *field_mem = upb_array_add(arr, 1);
  2156. CHK(field_mem);
  2157. memcpy(field_mem, &val, sizeof(val));
  2158. return true;
  2159. }
  2160. case UPB_DESCRIPTOR_TYPE_FLOAT:
  2161. case UPB_DESCRIPTOR_TYPE_FIXED32:
  2162. case UPB_DESCRIPTOR_TYPE_SFIXED32:
  2163. return upb_decode_fixedpacked(arr, val, sizeof(int32_t));
  2164. case UPB_DESCRIPTOR_TYPE_DOUBLE:
  2165. case UPB_DESCRIPTOR_TYPE_FIXED64:
  2166. case UPB_DESCRIPTOR_TYPE_SFIXED64:
  2167. return upb_decode_fixedpacked(arr, val, sizeof(int64_t));
  2168. case UPB_DESCRIPTOR_TYPE_INT32:
  2169. case UPB_DESCRIPTOR_TYPE_UINT32:
  2170. case UPB_DESCRIPTOR_TYPE_ENUM:
  2171. /* TODO: proto2 enum field that isn't in the enum. */
  2172. VARINT_CASE(uint32_t, uint32_t);
  2173. case UPB_DESCRIPTOR_TYPE_INT64:
  2174. case UPB_DESCRIPTOR_TYPE_UINT64:
  2175. VARINT_CASE(uint64_t, uint64_t);
  2176. case UPB_DESCRIPTOR_TYPE_BOOL:
  2177. VARINT_CASE(bool, bool);
  2178. case UPB_DESCRIPTOR_TYPE_SINT32:
  2179. VARINT_CASE(int32_t, upb_zzdecode_32);
  2180. case UPB_DESCRIPTOR_TYPE_SINT64:
  2181. VARINT_CASE(int64_t, upb_zzdecode_64);
  2182. case UPB_DESCRIPTOR_TYPE_MESSAGE: {
  2183. const upb_msglayout_msginit_v1 *subm;
  2184. char *submsg;
  2185. void *field_mem;
  2186. CHK(val.size <= (size_t)(frame->limit - val.data));
  2187. d->ptr -= val.size;
  2188. /* Create elemente message. */
  2189. UPB_ASSERT(field->submsg_index != UPB_NO_SUBMSG);
  2190. subm = frame->m->submsgs[field->submsg_index];
  2191. UPB_ASSERT(subm);
  2192. submsg = upb_env_malloc(d->env, upb_msg_sizeof((upb_msglayout *)subm));
  2193. CHK(submsg);
  2194. submsg = upb_msg_init(submsg, (upb_msglayout*)subm,
  2195. upb_arena_alloc(upb_env_arena(d->env)));
  2196. field_mem = upb_array_add(arr, 1);
  2197. CHK(field_mem);
  2198. *(void**)field_mem = submsg;
  2199. return upb_decode_message(
  2200. d, val.data + val.size, frame->group_number, submsg, subm);
  2201. }
  2202. case UPB_DESCRIPTOR_TYPE_GROUP:
  2203. return upb_append_unknown(d, frame, field_start);
  2204. }
  2205. #undef VARINT_CASE
  2206. UPB_UNREACHABLE();
  2207. }
  2208. static bool upb_decode_delimitedfield(upb_decstate *d, upb_decframe *frame,
  2209. const char *field_start,
  2210. const upb_msglayout_fieldinit_v1 *field) {
  2211. upb_stringview val;
  2212. CHK(upb_decode_string(&d->ptr, frame->limit, &val));
  2213. if (field->label == UPB_LABEL_REPEATED) {
  2214. return upb_decode_toarray(d, frame, field_start, field, val);
  2215. } else {
  2216. switch ((upb_descriptortype_t)field->descriptortype) {
  2217. case UPB_DESCRIPTOR_TYPE_STRING:
  2218. case UPB_DESCRIPTOR_TYPE_BYTES: {
  2219. void *field_mem = upb_decode_prepareslot(d, frame, field);
  2220. CHK(field_mem);
  2221. memcpy(field_mem, &val, sizeof(val));
  2222. break;
  2223. }
  2224. case UPB_DESCRIPTOR_TYPE_MESSAGE:
  2225. CHK(val.size <= (size_t)(frame->limit - val.data));
  2226. d->ptr -= val.size;
  2227. CHK(upb_decode_submsg(d, frame, val.data + val.size, field, 0));
  2228. break;
  2229. default:
  2230. /* TODO(haberman): should we accept the last element of a packed? */
  2231. return upb_append_unknown(d, frame, field_start);
  2232. }
  2233. upb_decode_setpresent(frame, field);
  2234. return true;
  2235. }
  2236. }
  2237. static const upb_msglayout_fieldinit_v1 *upb_find_field(
  2238. const upb_msglayout_msginit_v1 *l, uint32_t field_number) {
  2239. /* Lots of optimization opportunities here. */
  2240. int i;
  2241. for (i = 0; i < l->field_count; i++) {
  2242. if (l->fields[i].number == field_number) {
  2243. return &l->fields[i];
  2244. }
  2245. }
  2246. return NULL; /* Unknown field. */
  2247. }
  2248. static bool upb_decode_field(upb_decstate *d, upb_decframe *frame) {
  2249. int field_number;
  2250. int wire_type;
  2251. const char *field_start = d->ptr;
  2252. const upb_msglayout_fieldinit_v1 *field;
  2253. CHK(upb_decode_tag(&d->ptr, frame->limit, &field_number, &wire_type));
  2254. field = upb_find_field(frame->m, field_number);
  2255. if (field) {
  2256. switch (wire_type) {
  2257. case UPB_WIRE_TYPE_VARINT:
  2258. return upb_decode_varintfield(d, frame, field_start, field);
  2259. case UPB_WIRE_TYPE_32BIT:
  2260. return upb_decode_32bitfield(d, frame, field_start, field);
  2261. case UPB_WIRE_TYPE_64BIT:
  2262. return upb_decode_64bitfield(d, frame, field_start, field);
  2263. case UPB_WIRE_TYPE_DELIMITED:
  2264. return upb_decode_delimitedfield(d, frame, field_start, field);
  2265. case UPB_WIRE_TYPE_START_GROUP:
  2266. CHK(field->descriptortype == UPB_DESCRIPTOR_TYPE_GROUP);
  2267. return upb_decode_submsg(d, frame, frame->limit, field, field_number);
  2268. case UPB_WIRE_TYPE_END_GROUP:
  2269. CHK(frame->group_number == field_number)
  2270. frame->limit = d->ptr;
  2271. return true;
  2272. default:
  2273. return false;
  2274. }
  2275. } else {
  2276. CHK(field_number != 0);
  2277. return upb_skip_unknownfielddata(d, frame, field_number, wire_type);
  2278. }
  2279. }
  2280. static bool upb_skip_unknowngroup(upb_decstate *d, int field_number,
  2281. const char *limit) {
  2282. upb_decframe frame;
  2283. frame.msg = NULL;
  2284. frame.m = NULL;
  2285. frame.group_number = field_number;
  2286. frame.limit = limit;
  2287. while (d->ptr < frame.limit) {
  2288. int wire_type;
  2289. int field_number;
  2290. CHK(upb_decode_tag(&d->ptr, frame.limit, &field_number, &wire_type));
  2291. CHK(upb_skip_unknownfielddata(d, &frame, field_number, wire_type));
  2292. }
  2293. return true;
  2294. }
  2295. static bool upb_decode_message(upb_decstate *d, const char *limit,
  2296. int group_number, char *msg,
  2297. const upb_msglayout_msginit_v1 *l) {
  2298. upb_decframe frame;
  2299. frame.group_number = group_number;
  2300. frame.limit = limit;
  2301. frame.msg = msg;
  2302. frame.m = l;
  2303. while (d->ptr < frame.limit) {
  2304. CHK(upb_decode_field(d, &frame));
  2305. }
  2306. return true;
  2307. }
  2308. bool upb_decode(upb_stringview buf, void *msg,
  2309. const upb_msglayout_msginit_v1 *l, upb_env *env) {
  2310. upb_decstate state;
  2311. state.ptr = buf.data;
  2312. state.env = env;
  2313. return upb_decode_message(&state, buf.data + buf.size, 0, msg, l);
  2314. }
  2315. #undef CHK
  2316. #include <ctype.h>
  2317. #include <stdlib.h>
  2318. #include <string.h>
  2319. typedef struct {
  2320. size_t len;
  2321. char str[1]; /* Null-terminated string data follows. */
  2322. } str_t;
  2323. static str_t *newstr(const char *data, size_t len) {
  2324. str_t *ret = upb_gmalloc(sizeof(*ret) + len);
  2325. if (!ret) return NULL;
  2326. ret->len = len;
  2327. memcpy(ret->str, data, len);
  2328. ret->str[len] = '\0';
  2329. return ret;
  2330. }
  2331. static void freestr(str_t *s) { upb_gfree(s); }
  2332. /* isalpha() etc. from <ctype.h> are locale-dependent, which we don't want. */
  2333. static bool upb_isbetween(char c, char low, char high) {
  2334. return c >= low && c <= high;
  2335. }
  2336. static bool upb_isletter(char c) {
  2337. return upb_isbetween(c, 'A', 'Z') || upb_isbetween(c, 'a', 'z') || c == '_';
  2338. }
  2339. static bool upb_isalphanum(char c) {
  2340. return upb_isletter(c) || upb_isbetween(c, '0', '9');
  2341. }
  2342. static bool upb_isident(const char *str, size_t len, bool full, upb_status *s) {
  2343. bool start = true;
  2344. size_t i;
  2345. for (i = 0; i < len; i++) {
  2346. char c = str[i];
  2347. if (c == '.') {
  2348. if (start || !full) {
  2349. upb_status_seterrf(s, "invalid name: unexpected '.' (%s)", str);
  2350. return false;
  2351. }
  2352. start = true;
  2353. } else if (start) {
  2354. if (!upb_isletter(c)) {
  2355. upb_status_seterrf(
  2356. s, "invalid name: path components must start with a letter (%s)",
  2357. str);
  2358. return false;
  2359. }
  2360. start = false;
  2361. } else {
  2362. if (!upb_isalphanum(c)) {
  2363. upb_status_seterrf(s, "invalid name: non-alphanumeric character (%s)",
  2364. str);
  2365. return false;
  2366. }
  2367. }
  2368. }
  2369. return !start;
  2370. }
  2371. static bool upb_isoneof(const upb_refcounted *def) {
  2372. return def->vtbl == &upb_oneofdef_vtbl;
  2373. }
  2374. static bool upb_isfield(const upb_refcounted *def) {
  2375. return def->vtbl == &upb_fielddef_vtbl;
  2376. }
  2377. static const upb_oneofdef *upb_trygetoneof(const upb_refcounted *def) {
  2378. return upb_isoneof(def) ? (const upb_oneofdef*)def : NULL;
  2379. }
  2380. static const upb_fielddef *upb_trygetfield(const upb_refcounted *def) {
  2381. return upb_isfield(def) ? (const upb_fielddef*)def : NULL;
  2382. }
  2383. /* upb_def ********************************************************************/
  2384. upb_deftype_t upb_def_type(const upb_def *d) { return d->type; }
  2385. const char *upb_def_fullname(const upb_def *d) { return d->fullname; }
  2386. const char *upb_def_name(const upb_def *d) {
  2387. const char *p;
  2388. if (d->fullname == NULL) {
  2389. return NULL;
  2390. } else if ((p = strrchr(d->fullname, '.')) == NULL) {
  2391. /* No '.' in the name, return the full string. */
  2392. return d->fullname;
  2393. } else {
  2394. /* Return one past the last '.'. */
  2395. return p + 1;
  2396. }
  2397. }
  2398. bool upb_def_setfullname(upb_def *def, const char *fullname, upb_status *s) {
  2399. UPB_ASSERT(!upb_def_isfrozen(def));
  2400. if (!upb_isident(fullname, strlen(fullname), true, s)) {
  2401. return false;
  2402. }
  2403. fullname = upb_gstrdup(fullname);
  2404. if (!fullname) {
  2405. upb_upberr_setoom(s);
  2406. return false;
  2407. }
  2408. upb_gfree((void*)def->fullname);
  2409. def->fullname = fullname;
  2410. return true;
  2411. }
  2412. const upb_filedef *upb_def_file(const upb_def *d) { return d->file; }
  2413. static bool upb_def_init(upb_def *def, upb_deftype_t type,
  2414. const struct upb_refcounted_vtbl *vtbl,
  2415. const void *owner) {
  2416. if (!upb_refcounted_init(upb_def_upcast_mutable(def), vtbl, owner)) return false;
  2417. def->type = type;
  2418. def->fullname = NULL;
  2419. def->came_from_user = false;
  2420. def->file = NULL;
  2421. return true;
  2422. }
  2423. static void upb_def_uninit(upb_def *def) {
  2424. upb_gfree((void*)def->fullname);
  2425. }
  2426. static const char *msgdef_name(const upb_msgdef *m) {
  2427. const char *name = upb_def_fullname(upb_msgdef_upcast(m));
  2428. return name ? name : "(anonymous)";
  2429. }
  2430. static bool upb_validate_field(upb_fielddef *f, upb_status *s) {
  2431. if (upb_fielddef_name(f) == NULL || upb_fielddef_number(f) == 0) {
  2432. upb_status_seterrmsg(s, "fielddef must have name and number set");
  2433. return false;
  2434. }
  2435. if (!f->type_is_set_) {
  2436. upb_status_seterrmsg(s, "fielddef type was not initialized");
  2437. return false;
  2438. }
  2439. if (upb_fielddef_lazy(f) &&
  2440. upb_fielddef_descriptortype(f) != UPB_DESCRIPTOR_TYPE_MESSAGE) {
  2441. upb_status_seterrmsg(s,
  2442. "only length-delimited submessage fields may be lazy");
  2443. return false;
  2444. }
  2445. if (upb_fielddef_hassubdef(f)) {
  2446. const upb_def *subdef;
  2447. if (f->subdef_is_symbolic) {
  2448. upb_status_seterrf(s, "field '%s.%s' has not been resolved",
  2449. msgdef_name(f->msg.def), upb_fielddef_name(f));
  2450. return false;
  2451. }
  2452. subdef = upb_fielddef_subdef(f);
  2453. if (subdef == NULL) {
  2454. upb_status_seterrf(s, "field %s.%s is missing required subdef",
  2455. msgdef_name(f->msg.def), upb_fielddef_name(f));
  2456. return false;
  2457. }
  2458. if (!upb_def_isfrozen(subdef) && !subdef->came_from_user) {
  2459. upb_status_seterrf(s,
  2460. "subdef of field %s.%s is not frozen or being frozen",
  2461. msgdef_name(f->msg.def), upb_fielddef_name(f));
  2462. return false;
  2463. }
  2464. }
  2465. if (upb_fielddef_type(f) == UPB_TYPE_ENUM) {
  2466. bool has_default_name = upb_fielddef_enumhasdefaultstr(f);
  2467. bool has_default_number = upb_fielddef_enumhasdefaultint32(f);
  2468. /* Previously verified by upb_validate_enumdef(). */
  2469. UPB_ASSERT(upb_enumdef_numvals(upb_fielddef_enumsubdef(f)) > 0);
  2470. /* We've already validated that we have an associated enumdef and that it
  2471. * has at least one member, so at least one of these should be true.
  2472. * Because if the user didn't set anything, we'll pick up the enum's
  2473. * default, but if the user *did* set something we should at least pick up
  2474. * the one they set (int32 or string). */
  2475. UPB_ASSERT(has_default_name || has_default_number);
  2476. if (!has_default_name) {
  2477. upb_status_seterrf(s,
  2478. "enum default for field %s.%s (%d) is not in the enum",
  2479. msgdef_name(f->msg.def), upb_fielddef_name(f),
  2480. upb_fielddef_defaultint32(f));
  2481. return false;
  2482. }
  2483. if (!has_default_number) {
  2484. upb_status_seterrf(s,
  2485. "enum default for field %s.%s (%s) is not in the enum",
  2486. msgdef_name(f->msg.def), upb_fielddef_name(f),
  2487. upb_fielddef_defaultstr(f, NULL));
  2488. return false;
  2489. }
  2490. /* Lift the effective numeric default into the field's default slot, in case
  2491. * we were only getting it "by reference" from the enumdef. */
  2492. upb_fielddef_setdefaultint32(f, upb_fielddef_defaultint32(f));
  2493. }
  2494. /* Ensure that MapEntry submessages only appear as repeated fields, not
  2495. * optional/required (singular) fields. */
  2496. if (upb_fielddef_type(f) == UPB_TYPE_MESSAGE &&
  2497. upb_fielddef_msgsubdef(f) != NULL) {
  2498. const upb_msgdef *subdef = upb_fielddef_msgsubdef(f);
  2499. if (upb_msgdef_mapentry(subdef) && !upb_fielddef_isseq(f)) {
  2500. upb_status_seterrf(s,
  2501. "Field %s refers to mapentry message but is not "
  2502. "a repeated field",
  2503. upb_fielddef_name(f) ? upb_fielddef_name(f) :
  2504. "(unnamed)");
  2505. return false;
  2506. }
  2507. }
  2508. return true;
  2509. }
  2510. static bool upb_validate_enumdef(const upb_enumdef *e, upb_status *s) {
  2511. if (upb_enumdef_numvals(e) == 0) {
  2512. upb_status_seterrf(s, "enum %s has no members (must have at least one)",
  2513. upb_enumdef_fullname(e));
  2514. return false;
  2515. }
  2516. return true;
  2517. }
  2518. /* All submessage fields are lower than all other fields.
  2519. * Secondly, fields are increasing in order. */
  2520. uint32_t field_rank(const upb_fielddef *f) {
  2521. uint32_t ret = upb_fielddef_number(f);
  2522. const uint32_t high_bit = 1 << 30;
  2523. UPB_ASSERT(ret < high_bit);
  2524. if (!upb_fielddef_issubmsg(f))
  2525. ret |= high_bit;
  2526. return ret;
  2527. }
  2528. int cmp_fields(const void *p1, const void *p2) {
  2529. const upb_fielddef *f1 = *(upb_fielddef*const*)p1;
  2530. const upb_fielddef *f2 = *(upb_fielddef*const*)p2;
  2531. return field_rank(f1) - field_rank(f2);
  2532. }
  2533. static bool assign_msg_indices(upb_msgdef *m, upb_status *s) {
  2534. /* Sort fields. upb internally relies on UPB_TYPE_MESSAGE fields having the
  2535. * lowest indexes, but we do not publicly guarantee this. */
  2536. upb_msg_field_iter j;
  2537. upb_msg_oneof_iter k;
  2538. int i;
  2539. uint32_t selector;
  2540. int n = upb_msgdef_numfields(m);
  2541. upb_fielddef **fields;
  2542. if (n == 0) {
  2543. m->selector_count = UPB_STATIC_SELECTOR_COUNT;
  2544. m->submsg_field_count = 0;
  2545. return true;
  2546. }
  2547. fields = upb_gmalloc(n * sizeof(*fields));
  2548. if (!fields) {
  2549. upb_upberr_setoom(s);
  2550. return false;
  2551. }
  2552. m->submsg_field_count = 0;
  2553. for(i = 0, upb_msg_field_begin(&j, m);
  2554. !upb_msg_field_done(&j);
  2555. upb_msg_field_next(&j), i++) {
  2556. upb_fielddef *f = upb_msg_iter_field(&j);
  2557. UPB_ASSERT(f->msg.def == m);
  2558. if (!upb_validate_field(f, s)) {
  2559. upb_gfree(fields);
  2560. return false;
  2561. }
  2562. if (upb_fielddef_issubmsg(f)) {
  2563. m->submsg_field_count++;
  2564. }
  2565. fields[i] = f;
  2566. }
  2567. qsort(fields, n, sizeof(*fields), cmp_fields);
  2568. selector = UPB_STATIC_SELECTOR_COUNT + m->submsg_field_count;
  2569. for (i = 0; i < n; i++) {
  2570. upb_fielddef *f = fields[i];
  2571. f->index_ = i;
  2572. f->selector_base = selector + upb_handlers_selectorbaseoffset(f);
  2573. selector += upb_handlers_selectorcount(f);
  2574. }
  2575. m->selector_count = selector;
  2576. #ifndef NDEBUG
  2577. {
  2578. /* Verify that all selectors for the message are distinct. */
  2579. #define TRY(type) \
  2580. if (upb_handlers_getselector(f, type, &sel)) upb_inttable_insert(&t, sel, v);
  2581. upb_inttable t;
  2582. upb_value v;
  2583. upb_selector_t sel;
  2584. upb_inttable_init(&t, UPB_CTYPE_BOOL);
  2585. v = upb_value_bool(true);
  2586. upb_inttable_insert(&t, UPB_STARTMSG_SELECTOR, v);
  2587. upb_inttable_insert(&t, UPB_ENDMSG_SELECTOR, v);
  2588. upb_inttable_insert(&t, UPB_UNKNOWN_SELECTOR, v);
  2589. for(upb_msg_field_begin(&j, m);
  2590. !upb_msg_field_done(&j);
  2591. upb_msg_field_next(&j)) {
  2592. upb_fielddef *f = upb_msg_iter_field(&j);
  2593. /* These calls will assert-fail in upb_table if the value already
  2594. * exists. */
  2595. TRY(UPB_HANDLER_INT32);
  2596. TRY(UPB_HANDLER_INT64)
  2597. TRY(UPB_HANDLER_UINT32)
  2598. TRY(UPB_HANDLER_UINT64)
  2599. TRY(UPB_HANDLER_FLOAT)
  2600. TRY(UPB_HANDLER_DOUBLE)
  2601. TRY(UPB_HANDLER_BOOL)
  2602. TRY(UPB_HANDLER_STARTSTR)
  2603. TRY(UPB_HANDLER_STRING)
  2604. TRY(UPB_HANDLER_ENDSTR)
  2605. TRY(UPB_HANDLER_STARTSUBMSG)
  2606. TRY(UPB_HANDLER_ENDSUBMSG)
  2607. TRY(UPB_HANDLER_STARTSEQ)
  2608. TRY(UPB_HANDLER_ENDSEQ)
  2609. }
  2610. upb_inttable_uninit(&t);
  2611. }
  2612. #undef TRY
  2613. #endif
  2614. for(upb_msg_oneof_begin(&k, m), i = 0;
  2615. !upb_msg_oneof_done(&k);
  2616. upb_msg_oneof_next(&k), i++) {
  2617. upb_oneofdef *o = upb_msg_iter_oneof(&k);
  2618. o->index = i;
  2619. }
  2620. upb_gfree(fields);
  2621. return true;
  2622. }
  2623. bool _upb_def_validate(upb_def *const*defs, size_t n, upb_status *s) {
  2624. size_t i;
  2625. /* First perform validation, in two passes so we can check that we have a
  2626. * transitive closure without needing to search. */
  2627. for (i = 0; i < n; i++) {
  2628. upb_def *def = defs[i];
  2629. if (upb_def_isfrozen(def)) {
  2630. /* Could relax this requirement if it's annoying. */
  2631. upb_status_seterrmsg(s, "def is already frozen");
  2632. goto err;
  2633. } else if (def->type == UPB_DEF_FIELD) {
  2634. upb_status_seterrmsg(s, "standalone fielddefs can not be frozen");
  2635. goto err;
  2636. } else {
  2637. /* Set now to detect transitive closure in the second pass. */
  2638. def->came_from_user = true;
  2639. if (def->type == UPB_DEF_ENUM &&
  2640. !upb_validate_enumdef(upb_dyncast_enumdef(def), s)) {
  2641. goto err;
  2642. }
  2643. }
  2644. }
  2645. /* Second pass of validation. Also assign selector bases and indexes, and
  2646. * compact tables. */
  2647. for (i = 0; i < n; i++) {
  2648. upb_def *def = defs[i];
  2649. upb_msgdef *m = upb_dyncast_msgdef_mutable(def);
  2650. upb_enumdef *e = upb_dyncast_enumdef_mutable(def);
  2651. if (m) {
  2652. upb_inttable_compact(&m->itof);
  2653. if (!assign_msg_indices(m, s)) {
  2654. goto err;
  2655. }
  2656. } else if (e) {
  2657. upb_inttable_compact(&e->iton);
  2658. }
  2659. }
  2660. return true;
  2661. err:
  2662. for (i = 0; i < n; i++) {
  2663. upb_def *def = defs[i];
  2664. def->came_from_user = false;
  2665. }
  2666. UPB_ASSERT(!(s && upb_ok(s)));
  2667. return false;
  2668. }
  2669. bool upb_def_freeze(upb_def *const* defs, size_t n, upb_status *s) {
  2670. /* Def graph contains FieldDefs between each MessageDef, so double the
  2671. * limit. */
  2672. const size_t maxdepth = UPB_MAX_MESSAGE_DEPTH * 2;
  2673. if (!_upb_def_validate(defs, n, s)) {
  2674. return false;
  2675. }
  2676. /* Validation all passed; freeze the objects. */
  2677. return upb_refcounted_freeze((upb_refcounted *const*)defs, n, s, maxdepth);
  2678. }
  2679. /* upb_enumdef ****************************************************************/
  2680. static void visitenum(const upb_refcounted *r, upb_refcounted_visit *visit,
  2681. void *closure) {
  2682. const upb_enumdef *e = (const upb_enumdef*)r;
  2683. const upb_def *def = upb_enumdef_upcast(e);
  2684. if (upb_def_file(def)) {
  2685. visit(r, upb_filedef_upcast(upb_def_file(def)), closure);
  2686. }
  2687. }
  2688. static void freeenum(upb_refcounted *r) {
  2689. upb_enumdef *e = (upb_enumdef*)r;
  2690. upb_inttable_iter i;
  2691. upb_inttable_begin(&i, &e->iton);
  2692. for( ; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  2693. /* To clean up the upb_gstrdup() from upb_enumdef_addval(). */
  2694. upb_gfree(upb_value_getcstr(upb_inttable_iter_value(&i)));
  2695. }
  2696. upb_strtable_uninit(&e->ntoi);
  2697. upb_inttable_uninit(&e->iton);
  2698. upb_def_uninit(upb_enumdef_upcast_mutable(e));
  2699. upb_gfree(e);
  2700. }
  2701. const struct upb_refcounted_vtbl upb_enumdef_vtbl = {&visitenum, &freeenum};
  2702. upb_enumdef *upb_enumdef_new(const void *owner) {
  2703. upb_enumdef *e = upb_gmalloc(sizeof(*e));
  2704. if (!e) return NULL;
  2705. if (!upb_def_init(upb_enumdef_upcast_mutable(e), UPB_DEF_ENUM,
  2706. &upb_enumdef_vtbl, owner)) {
  2707. goto err2;
  2708. }
  2709. if (!upb_strtable_init(&e->ntoi, UPB_CTYPE_INT32)) goto err2;
  2710. if (!upb_inttable_init(&e->iton, UPB_CTYPE_CSTR)) goto err1;
  2711. return e;
  2712. err1:
  2713. upb_strtable_uninit(&e->ntoi);
  2714. err2:
  2715. upb_gfree(e);
  2716. return NULL;
  2717. }
  2718. bool upb_enumdef_freeze(upb_enumdef *e, upb_status *status) {
  2719. upb_def *d = upb_enumdef_upcast_mutable(e);
  2720. return upb_def_freeze(&d, 1, status);
  2721. }
  2722. const char *upb_enumdef_fullname(const upb_enumdef *e) {
  2723. return upb_def_fullname(upb_enumdef_upcast(e));
  2724. }
  2725. const char *upb_enumdef_name(const upb_enumdef *e) {
  2726. return upb_def_name(upb_enumdef_upcast(e));
  2727. }
  2728. bool upb_enumdef_setfullname(upb_enumdef *e, const char *fullname,
  2729. upb_status *s) {
  2730. return upb_def_setfullname(upb_enumdef_upcast_mutable(e), fullname, s);
  2731. }
  2732. bool upb_enumdef_addval(upb_enumdef *e, const char *name, int32_t num,
  2733. upb_status *status) {
  2734. char *name2;
  2735. if (!upb_isident(name, strlen(name), false, status)) {
  2736. return false;
  2737. }
  2738. if (upb_enumdef_ntoiz(e, name, NULL)) {
  2739. upb_status_seterrf(status, "name '%s' is already defined", name);
  2740. return false;
  2741. }
  2742. if (!upb_strtable_insert(&e->ntoi, name, upb_value_int32(num))) {
  2743. upb_status_seterrmsg(status, "out of memory");
  2744. return false;
  2745. }
  2746. if (!upb_inttable_lookup(&e->iton, num, NULL)) {
  2747. name2 = upb_gstrdup(name);
  2748. if (!name2 || !upb_inttable_insert(&e->iton, num, upb_value_cstr(name2))) {
  2749. upb_status_seterrmsg(status, "out of memory");
  2750. upb_strtable_remove(&e->ntoi, name, NULL);
  2751. return false;
  2752. }
  2753. }
  2754. if (upb_enumdef_numvals(e) == 1) {
  2755. bool ok = upb_enumdef_setdefault(e, num, NULL);
  2756. UPB_ASSERT(ok);
  2757. }
  2758. return true;
  2759. }
  2760. int32_t upb_enumdef_default(const upb_enumdef *e) {
  2761. UPB_ASSERT(upb_enumdef_iton(e, e->defaultval));
  2762. return e->defaultval;
  2763. }
  2764. bool upb_enumdef_setdefault(upb_enumdef *e, int32_t val, upb_status *s) {
  2765. UPB_ASSERT(!upb_enumdef_isfrozen(e));
  2766. if (!upb_enumdef_iton(e, val)) {
  2767. upb_status_seterrf(s, "number '%d' is not in the enum.", val);
  2768. return false;
  2769. }
  2770. e->defaultval = val;
  2771. return true;
  2772. }
  2773. int upb_enumdef_numvals(const upb_enumdef *e) {
  2774. return upb_strtable_count(&e->ntoi);
  2775. }
  2776. void upb_enum_begin(upb_enum_iter *i, const upb_enumdef *e) {
  2777. /* We iterate over the ntoi table, to account for duplicate numbers. */
  2778. upb_strtable_begin(i, &e->ntoi);
  2779. }
  2780. void upb_enum_next(upb_enum_iter *iter) { upb_strtable_next(iter); }
  2781. bool upb_enum_done(upb_enum_iter *iter) { return upb_strtable_done(iter); }
  2782. bool upb_enumdef_ntoi(const upb_enumdef *def, const char *name,
  2783. size_t len, int32_t *num) {
  2784. upb_value v;
  2785. if (!upb_strtable_lookup2(&def->ntoi, name, len, &v)) {
  2786. return false;
  2787. }
  2788. if (num) *num = upb_value_getint32(v);
  2789. return true;
  2790. }
  2791. const char *upb_enumdef_iton(const upb_enumdef *def, int32_t num) {
  2792. upb_value v;
  2793. return upb_inttable_lookup32(&def->iton, num, &v) ?
  2794. upb_value_getcstr(v) : NULL;
  2795. }
  2796. const char *upb_enum_iter_name(upb_enum_iter *iter) {
  2797. return upb_strtable_iter_key(iter);
  2798. }
  2799. int32_t upb_enum_iter_number(upb_enum_iter *iter) {
  2800. return upb_value_getint32(upb_strtable_iter_value(iter));
  2801. }
  2802. /* upb_fielddef ***************************************************************/
  2803. static void upb_fielddef_init_default(upb_fielddef *f);
  2804. static void upb_fielddef_uninit_default(upb_fielddef *f) {
  2805. if (f->type_is_set_ && f->default_is_string && f->defaultval.bytes)
  2806. freestr(f->defaultval.bytes);
  2807. }
  2808. const char *upb_fielddef_fullname(const upb_fielddef *e) {
  2809. return upb_def_fullname(upb_fielddef_upcast(e));
  2810. }
  2811. static void visitfield(const upb_refcounted *r, upb_refcounted_visit *visit,
  2812. void *closure) {
  2813. const upb_fielddef *f = (const upb_fielddef*)r;
  2814. const upb_def *def = upb_fielddef_upcast(f);
  2815. if (upb_fielddef_containingtype(f)) {
  2816. visit(r, upb_msgdef_upcast2(upb_fielddef_containingtype(f)), closure);
  2817. }
  2818. if (upb_fielddef_containingoneof(f)) {
  2819. visit(r, upb_oneofdef_upcast(upb_fielddef_containingoneof(f)), closure);
  2820. }
  2821. if (upb_fielddef_subdef(f)) {
  2822. visit(r, upb_def_upcast(upb_fielddef_subdef(f)), closure);
  2823. }
  2824. if (upb_def_file(def)) {
  2825. visit(r, upb_filedef_upcast(upb_def_file(def)), closure);
  2826. }
  2827. }
  2828. static void freefield(upb_refcounted *r) {
  2829. upb_fielddef *f = (upb_fielddef*)r;
  2830. upb_fielddef_uninit_default(f);
  2831. if (f->subdef_is_symbolic)
  2832. upb_gfree(f->sub.name);
  2833. upb_def_uninit(upb_fielddef_upcast_mutable(f));
  2834. upb_gfree(f);
  2835. }
  2836. static const char *enumdefaultstr(const upb_fielddef *f) {
  2837. const upb_enumdef *e;
  2838. UPB_ASSERT(f->type_is_set_ && f->type_ == UPB_TYPE_ENUM);
  2839. e = upb_fielddef_enumsubdef(f);
  2840. if (f->default_is_string && f->defaultval.bytes) {
  2841. /* Default was explicitly set as a string. */
  2842. str_t *s = f->defaultval.bytes;
  2843. return s->str;
  2844. } else if (e) {
  2845. if (!f->default_is_string) {
  2846. /* Default was explicitly set as an integer; look it up in enumdef. */
  2847. const char *name = upb_enumdef_iton(e, f->defaultval.sint);
  2848. if (name) {
  2849. return name;
  2850. }
  2851. } else {
  2852. /* Default is completely unset; pull enumdef default. */
  2853. if (upb_enumdef_numvals(e) > 0) {
  2854. const char *name = upb_enumdef_iton(e, upb_enumdef_default(e));
  2855. UPB_ASSERT(name);
  2856. return name;
  2857. }
  2858. }
  2859. }
  2860. return NULL;
  2861. }
  2862. static bool enumdefaultint32(const upb_fielddef *f, int32_t *val) {
  2863. const upb_enumdef *e;
  2864. UPB_ASSERT(f->type_is_set_ && f->type_ == UPB_TYPE_ENUM);
  2865. e = upb_fielddef_enumsubdef(f);
  2866. if (!f->default_is_string) {
  2867. /* Default was explicitly set as an integer. */
  2868. *val = f->defaultval.sint;
  2869. return true;
  2870. } else if (e) {
  2871. if (f->defaultval.bytes) {
  2872. /* Default was explicitly set as a str; try to lookup corresponding int. */
  2873. str_t *s = f->defaultval.bytes;
  2874. if (upb_enumdef_ntoiz(e, s->str, val)) {
  2875. return true;
  2876. }
  2877. } else {
  2878. /* Default is unset; try to pull in enumdef default. */
  2879. if (upb_enumdef_numvals(e) > 0) {
  2880. *val = upb_enumdef_default(e);
  2881. return true;
  2882. }
  2883. }
  2884. }
  2885. return false;
  2886. }
  2887. const struct upb_refcounted_vtbl upb_fielddef_vtbl = {visitfield, freefield};
  2888. upb_fielddef *upb_fielddef_new(const void *o) {
  2889. upb_fielddef *f = upb_gmalloc(sizeof(*f));
  2890. if (!f) return NULL;
  2891. if (!upb_def_init(upb_fielddef_upcast_mutable(f), UPB_DEF_FIELD,
  2892. &upb_fielddef_vtbl, o)) {
  2893. upb_gfree(f);
  2894. return NULL;
  2895. }
  2896. f->msg.def = NULL;
  2897. f->sub.def = NULL;
  2898. f->oneof = NULL;
  2899. f->subdef_is_symbolic = false;
  2900. f->msg_is_symbolic = false;
  2901. f->label_ = UPB_LABEL_OPTIONAL;
  2902. f->type_ = UPB_TYPE_INT32;
  2903. f->number_ = 0;
  2904. f->type_is_set_ = false;
  2905. f->tagdelim = false;
  2906. f->is_extension_ = false;
  2907. f->lazy_ = false;
  2908. f->packed_ = true;
  2909. /* For the moment we default this to UPB_INTFMT_VARIABLE, since it will work
  2910. * with all integer types and is in some since more "default" since the most
  2911. * normal-looking proto2 types int32/int64/uint32/uint64 use variable.
  2912. *
  2913. * Other options to consider:
  2914. * - there is no default; users must set this manually (like type).
  2915. * - default signed integers to UPB_INTFMT_ZIGZAG, since it's more likely to
  2916. * be an optimal default for signed integers. */
  2917. f->intfmt = UPB_INTFMT_VARIABLE;
  2918. return f;
  2919. }
  2920. bool upb_fielddef_typeisset(const upb_fielddef *f) {
  2921. return f->type_is_set_;
  2922. }
  2923. upb_fieldtype_t upb_fielddef_type(const upb_fielddef *f) {
  2924. UPB_ASSERT(f->type_is_set_);
  2925. return f->type_;
  2926. }
  2927. uint32_t upb_fielddef_index(const upb_fielddef *f) {
  2928. return f->index_;
  2929. }
  2930. upb_label_t upb_fielddef_label(const upb_fielddef *f) {
  2931. return f->label_;
  2932. }
  2933. upb_intfmt_t upb_fielddef_intfmt(const upb_fielddef *f) {
  2934. return f->intfmt;
  2935. }
  2936. bool upb_fielddef_istagdelim(const upb_fielddef *f) {
  2937. return f->tagdelim;
  2938. }
  2939. uint32_t upb_fielddef_number(const upb_fielddef *f) {
  2940. return f->number_;
  2941. }
  2942. bool upb_fielddef_isextension(const upb_fielddef *f) {
  2943. return f->is_extension_;
  2944. }
  2945. bool upb_fielddef_lazy(const upb_fielddef *f) {
  2946. return f->lazy_;
  2947. }
  2948. bool upb_fielddef_packed(const upb_fielddef *f) {
  2949. return f->packed_;
  2950. }
  2951. const char *upb_fielddef_name(const upb_fielddef *f) {
  2952. return upb_def_fullname(upb_fielddef_upcast(f));
  2953. }
  2954. size_t upb_fielddef_getjsonname(const upb_fielddef *f, char *buf, size_t len) {
  2955. const char *name = upb_fielddef_name(f);
  2956. size_t src, dst = 0;
  2957. bool ucase_next = false;
  2958. #define WRITE(byte) \
  2959. ++dst; \
  2960. if (dst < len) buf[dst - 1] = byte; \
  2961. else if (dst == len) buf[dst - 1] = '\0'
  2962. if (!name) {
  2963. WRITE('\0');
  2964. return 0;
  2965. }
  2966. /* Implement the transformation as described in the spec:
  2967. * 1. upper case all letters after an underscore.
  2968. * 2. remove all underscores.
  2969. */
  2970. for (src = 0; name[src]; src++) {
  2971. if (name[src] == '_') {
  2972. ucase_next = true;
  2973. continue;
  2974. }
  2975. if (ucase_next) {
  2976. WRITE(toupper(name[src]));
  2977. ucase_next = false;
  2978. } else {
  2979. WRITE(name[src]);
  2980. }
  2981. }
  2982. WRITE('\0');
  2983. return dst;
  2984. #undef WRITE
  2985. }
  2986. const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f) {
  2987. return f->msg_is_symbolic ? NULL : f->msg.def;
  2988. }
  2989. const upb_oneofdef *upb_fielddef_containingoneof(const upb_fielddef *f) {
  2990. return f->oneof;
  2991. }
  2992. upb_msgdef *upb_fielddef_containingtype_mutable(upb_fielddef *f) {
  2993. return (upb_msgdef*)upb_fielddef_containingtype(f);
  2994. }
  2995. const char *upb_fielddef_containingtypename(upb_fielddef *f) {
  2996. return f->msg_is_symbolic ? f->msg.name : NULL;
  2997. }
  2998. static void release_containingtype(upb_fielddef *f) {
  2999. if (f->msg_is_symbolic) upb_gfree(f->msg.name);
  3000. }
  3001. bool upb_fielddef_setcontainingtypename(upb_fielddef *f, const char *name,
  3002. upb_status *s) {
  3003. char *name_copy;
  3004. UPB_ASSERT(!upb_fielddef_isfrozen(f));
  3005. if (upb_fielddef_containingtype(f)) {
  3006. upb_status_seterrmsg(s, "field has already been added to a message.");
  3007. return false;
  3008. }
  3009. /* TODO: validate name (upb_isident() doesn't quite work atm because this name
  3010. * may have a leading "."). */
  3011. name_copy = upb_gstrdup(name);
  3012. if (!name_copy) {
  3013. upb_upberr_setoom(s);
  3014. return false;
  3015. }
  3016. release_containingtype(f);
  3017. f->msg.name = name_copy;
  3018. f->msg_is_symbolic = true;
  3019. return true;
  3020. }
  3021. bool upb_fielddef_setname(upb_fielddef *f, const char *name, upb_status *s) {
  3022. if (upb_fielddef_containingtype(f) || upb_fielddef_containingoneof(f)) {
  3023. upb_status_seterrmsg(s, "Already added to message or oneof");
  3024. return false;
  3025. }
  3026. return upb_def_setfullname(upb_fielddef_upcast_mutable(f), name, s);
  3027. }
  3028. static void chkdefaulttype(const upb_fielddef *f, upb_fieldtype_t type) {
  3029. UPB_UNUSED(f);
  3030. UPB_UNUSED(type);
  3031. UPB_ASSERT(f->type_is_set_ && upb_fielddef_type(f) == type);
  3032. }
  3033. int64_t upb_fielddef_defaultint64(const upb_fielddef *f) {
  3034. chkdefaulttype(f, UPB_TYPE_INT64);
  3035. return f->defaultval.sint;
  3036. }
  3037. int32_t upb_fielddef_defaultint32(const upb_fielddef *f) {
  3038. if (f->type_is_set_ && upb_fielddef_type(f) == UPB_TYPE_ENUM) {
  3039. int32_t val;
  3040. bool ok = enumdefaultint32(f, &val);
  3041. UPB_ASSERT(ok);
  3042. return val;
  3043. } else {
  3044. chkdefaulttype(f, UPB_TYPE_INT32);
  3045. return f->defaultval.sint;
  3046. }
  3047. }
  3048. uint64_t upb_fielddef_defaultuint64(const upb_fielddef *f) {
  3049. chkdefaulttype(f, UPB_TYPE_UINT64);
  3050. return f->defaultval.uint;
  3051. }
  3052. uint32_t upb_fielddef_defaultuint32(const upb_fielddef *f) {
  3053. chkdefaulttype(f, UPB_TYPE_UINT32);
  3054. return f->defaultval.uint;
  3055. }
  3056. bool upb_fielddef_defaultbool(const upb_fielddef *f) {
  3057. chkdefaulttype(f, UPB_TYPE_BOOL);
  3058. return f->defaultval.uint;
  3059. }
  3060. float upb_fielddef_defaultfloat(const upb_fielddef *f) {
  3061. chkdefaulttype(f, UPB_TYPE_FLOAT);
  3062. return f->defaultval.flt;
  3063. }
  3064. double upb_fielddef_defaultdouble(const upb_fielddef *f) {
  3065. chkdefaulttype(f, UPB_TYPE_DOUBLE);
  3066. return f->defaultval.dbl;
  3067. }
  3068. const char *upb_fielddef_defaultstr(const upb_fielddef *f, size_t *len) {
  3069. UPB_ASSERT(f->type_is_set_);
  3070. UPB_ASSERT(upb_fielddef_type(f) == UPB_TYPE_STRING ||
  3071. upb_fielddef_type(f) == UPB_TYPE_BYTES ||
  3072. upb_fielddef_type(f) == UPB_TYPE_ENUM);
  3073. if (upb_fielddef_type(f) == UPB_TYPE_ENUM) {
  3074. const char *ret = enumdefaultstr(f);
  3075. UPB_ASSERT(ret);
  3076. /* Enum defaults can't have embedded NULLs. */
  3077. if (len) *len = strlen(ret);
  3078. return ret;
  3079. }
  3080. if (f->default_is_string) {
  3081. str_t *str = f->defaultval.bytes;
  3082. if (len) *len = str->len;
  3083. return str->str;
  3084. }
  3085. return NULL;
  3086. }
  3087. static void upb_fielddef_init_default(upb_fielddef *f) {
  3088. f->default_is_string = false;
  3089. switch (upb_fielddef_type(f)) {
  3090. case UPB_TYPE_DOUBLE: f->defaultval.dbl = 0; break;
  3091. case UPB_TYPE_FLOAT: f->defaultval.flt = 0; break;
  3092. case UPB_TYPE_INT32:
  3093. case UPB_TYPE_INT64: f->defaultval.sint = 0; break;
  3094. case UPB_TYPE_UINT64:
  3095. case UPB_TYPE_UINT32:
  3096. case UPB_TYPE_BOOL: f->defaultval.uint = 0; break;
  3097. case UPB_TYPE_STRING:
  3098. case UPB_TYPE_BYTES:
  3099. f->defaultval.bytes = newstr("", 0);
  3100. f->default_is_string = true;
  3101. break;
  3102. case UPB_TYPE_MESSAGE: break;
  3103. case UPB_TYPE_ENUM:
  3104. /* This is our special sentinel that indicates "not set" for an enum. */
  3105. f->default_is_string = true;
  3106. f->defaultval.bytes = NULL;
  3107. break;
  3108. }
  3109. }
  3110. const upb_def *upb_fielddef_subdef(const upb_fielddef *f) {
  3111. return f->subdef_is_symbolic ? NULL : f->sub.def;
  3112. }
  3113. const upb_msgdef *upb_fielddef_msgsubdef(const upb_fielddef *f) {
  3114. const upb_def *def = upb_fielddef_subdef(f);
  3115. return def ? upb_dyncast_msgdef(def) : NULL;
  3116. }
  3117. const upb_enumdef *upb_fielddef_enumsubdef(const upb_fielddef *f) {
  3118. const upb_def *def = upb_fielddef_subdef(f);
  3119. return def ? upb_dyncast_enumdef(def) : NULL;
  3120. }
  3121. upb_def *upb_fielddef_subdef_mutable(upb_fielddef *f) {
  3122. return (upb_def*)upb_fielddef_subdef(f);
  3123. }
  3124. const char *upb_fielddef_subdefname(const upb_fielddef *f) {
  3125. if (f->subdef_is_symbolic) {
  3126. return f->sub.name;
  3127. } else if (f->sub.def) {
  3128. return upb_def_fullname(f->sub.def);
  3129. } else {
  3130. return NULL;
  3131. }
  3132. }
  3133. bool upb_fielddef_setnumber(upb_fielddef *f, uint32_t number, upb_status *s) {
  3134. if (upb_fielddef_containingtype(f)) {
  3135. upb_status_seterrmsg(
  3136. s, "cannot change field number after adding to a message");
  3137. return false;
  3138. }
  3139. if (number == 0 || number > UPB_MAX_FIELDNUMBER) {
  3140. upb_status_seterrf(s, "invalid field number (%u)", number);
  3141. return false;
  3142. }
  3143. f->number_ = number;
  3144. return true;
  3145. }
  3146. void upb_fielddef_settype(upb_fielddef *f, upb_fieldtype_t type) {
  3147. UPB_ASSERT(!upb_fielddef_isfrozen(f));
  3148. UPB_ASSERT(upb_fielddef_checktype(type));
  3149. upb_fielddef_uninit_default(f);
  3150. f->type_ = type;
  3151. f->type_is_set_ = true;
  3152. upb_fielddef_init_default(f);
  3153. }
  3154. void upb_fielddef_setdescriptortype(upb_fielddef *f, int type) {
  3155. UPB_ASSERT(!upb_fielddef_isfrozen(f));
  3156. switch (type) {
  3157. case UPB_DESCRIPTOR_TYPE_DOUBLE:
  3158. upb_fielddef_settype(f, UPB_TYPE_DOUBLE);
  3159. break;
  3160. case UPB_DESCRIPTOR_TYPE_FLOAT:
  3161. upb_fielddef_settype(f, UPB_TYPE_FLOAT);
  3162. break;
  3163. case UPB_DESCRIPTOR_TYPE_INT64:
  3164. case UPB_DESCRIPTOR_TYPE_SFIXED64:
  3165. case UPB_DESCRIPTOR_TYPE_SINT64:
  3166. upb_fielddef_settype(f, UPB_TYPE_INT64);
  3167. break;
  3168. case UPB_DESCRIPTOR_TYPE_UINT64:
  3169. case UPB_DESCRIPTOR_TYPE_FIXED64:
  3170. upb_fielddef_settype(f, UPB_TYPE_UINT64);
  3171. break;
  3172. case UPB_DESCRIPTOR_TYPE_INT32:
  3173. case UPB_DESCRIPTOR_TYPE_SFIXED32:
  3174. case UPB_DESCRIPTOR_TYPE_SINT32:
  3175. upb_fielddef_settype(f, UPB_TYPE_INT32);
  3176. break;
  3177. case UPB_DESCRIPTOR_TYPE_UINT32:
  3178. case UPB_DESCRIPTOR_TYPE_FIXED32:
  3179. upb_fielddef_settype(f, UPB_TYPE_UINT32);
  3180. break;
  3181. case UPB_DESCRIPTOR_TYPE_BOOL:
  3182. upb_fielddef_settype(f, UPB_TYPE_BOOL);
  3183. break;
  3184. case UPB_DESCRIPTOR_TYPE_STRING:
  3185. upb_fielddef_settype(f, UPB_TYPE_STRING);
  3186. break;
  3187. case UPB_DESCRIPTOR_TYPE_BYTES:
  3188. upb_fielddef_settype(f, UPB_TYPE_BYTES);
  3189. break;
  3190. case UPB_DESCRIPTOR_TYPE_GROUP:
  3191. case UPB_DESCRIPTOR_TYPE_MESSAGE:
  3192. upb_fielddef_settype(f, UPB_TYPE_MESSAGE);
  3193. break;
  3194. case UPB_DESCRIPTOR_TYPE_ENUM:
  3195. upb_fielddef_settype(f, UPB_TYPE_ENUM);
  3196. break;
  3197. default: UPB_ASSERT(false);
  3198. }
  3199. if (type == UPB_DESCRIPTOR_TYPE_FIXED64 ||
  3200. type == UPB_DESCRIPTOR_TYPE_FIXED32 ||
  3201. type == UPB_DESCRIPTOR_TYPE_SFIXED64 ||
  3202. type == UPB_DESCRIPTOR_TYPE_SFIXED32) {
  3203. upb_fielddef_setintfmt(f, UPB_INTFMT_FIXED);
  3204. } else if (type == UPB_DESCRIPTOR_TYPE_SINT64 ||
  3205. type == UPB_DESCRIPTOR_TYPE_SINT32) {
  3206. upb_fielddef_setintfmt(f, UPB_INTFMT_ZIGZAG);
  3207. } else {
  3208. upb_fielddef_setintfmt(f, UPB_INTFMT_VARIABLE);
  3209. }
  3210. upb_fielddef_settagdelim(f, type == UPB_DESCRIPTOR_TYPE_GROUP);
  3211. }
  3212. upb_descriptortype_t upb_fielddef_descriptortype(const upb_fielddef *f) {
  3213. switch (upb_fielddef_type(f)) {
  3214. case UPB_TYPE_FLOAT: return UPB_DESCRIPTOR_TYPE_FLOAT;
  3215. case UPB_TYPE_DOUBLE: return UPB_DESCRIPTOR_TYPE_DOUBLE;
  3216. case UPB_TYPE_BOOL: return UPB_DESCRIPTOR_TYPE_BOOL;
  3217. case UPB_TYPE_STRING: return UPB_DESCRIPTOR_TYPE_STRING;
  3218. case UPB_TYPE_BYTES: return UPB_DESCRIPTOR_TYPE_BYTES;
  3219. case UPB_TYPE_ENUM: return UPB_DESCRIPTOR_TYPE_ENUM;
  3220. case UPB_TYPE_INT32:
  3221. switch (upb_fielddef_intfmt(f)) {
  3222. case UPB_INTFMT_VARIABLE: return UPB_DESCRIPTOR_TYPE_INT32;
  3223. case UPB_INTFMT_FIXED: return UPB_DESCRIPTOR_TYPE_SFIXED32;
  3224. case UPB_INTFMT_ZIGZAG: return UPB_DESCRIPTOR_TYPE_SINT32;
  3225. }
  3226. case UPB_TYPE_INT64:
  3227. switch (upb_fielddef_intfmt(f)) {
  3228. case UPB_INTFMT_VARIABLE: return UPB_DESCRIPTOR_TYPE_INT64;
  3229. case UPB_INTFMT_FIXED: return UPB_DESCRIPTOR_TYPE_SFIXED64;
  3230. case UPB_INTFMT_ZIGZAG: return UPB_DESCRIPTOR_TYPE_SINT64;
  3231. }
  3232. case UPB_TYPE_UINT32:
  3233. switch (upb_fielddef_intfmt(f)) {
  3234. case UPB_INTFMT_VARIABLE: return UPB_DESCRIPTOR_TYPE_UINT32;
  3235. case UPB_INTFMT_FIXED: return UPB_DESCRIPTOR_TYPE_FIXED32;
  3236. case UPB_INTFMT_ZIGZAG: return -1;
  3237. }
  3238. case UPB_TYPE_UINT64:
  3239. switch (upb_fielddef_intfmt(f)) {
  3240. case UPB_INTFMT_VARIABLE: return UPB_DESCRIPTOR_TYPE_UINT64;
  3241. case UPB_INTFMT_FIXED: return UPB_DESCRIPTOR_TYPE_FIXED64;
  3242. case UPB_INTFMT_ZIGZAG: return -1;
  3243. }
  3244. case UPB_TYPE_MESSAGE:
  3245. return upb_fielddef_istagdelim(f) ?
  3246. UPB_DESCRIPTOR_TYPE_GROUP : UPB_DESCRIPTOR_TYPE_MESSAGE;
  3247. }
  3248. return 0;
  3249. }
  3250. void upb_fielddef_setisextension(upb_fielddef *f, bool is_extension) {
  3251. UPB_ASSERT(!upb_fielddef_isfrozen(f));
  3252. f->is_extension_ = is_extension;
  3253. }
  3254. void upb_fielddef_setlazy(upb_fielddef *f, bool lazy) {
  3255. UPB_ASSERT(!upb_fielddef_isfrozen(f));
  3256. f->lazy_ = lazy;
  3257. }
  3258. void upb_fielddef_setpacked(upb_fielddef *f, bool packed) {
  3259. UPB_ASSERT(!upb_fielddef_isfrozen(f));
  3260. f->packed_ = packed;
  3261. }
  3262. void upb_fielddef_setlabel(upb_fielddef *f, upb_label_t label) {
  3263. UPB_ASSERT(!upb_fielddef_isfrozen(f));
  3264. UPB_ASSERT(upb_fielddef_checklabel(label));
  3265. f->label_ = label;
  3266. }
  3267. void upb_fielddef_setintfmt(upb_fielddef *f, upb_intfmt_t fmt) {
  3268. UPB_ASSERT(!upb_fielddef_isfrozen(f));
  3269. UPB_ASSERT(upb_fielddef_checkintfmt(fmt));
  3270. f->intfmt = fmt;
  3271. }
  3272. void upb_fielddef_settagdelim(upb_fielddef *f, bool tag_delim) {
  3273. UPB_ASSERT(!upb_fielddef_isfrozen(f));
  3274. f->tagdelim = tag_delim;
  3275. f->tagdelim = tag_delim;
  3276. }
  3277. static bool checksetdefault(upb_fielddef *f, upb_fieldtype_t type) {
  3278. if (!f->type_is_set_ || upb_fielddef_isfrozen(f) ||
  3279. upb_fielddef_type(f) != type) {
  3280. UPB_ASSERT(false);
  3281. return false;
  3282. }
  3283. if (f->default_is_string) {
  3284. str_t *s = f->defaultval.bytes;
  3285. UPB_ASSERT(s || type == UPB_TYPE_ENUM);
  3286. if (s) freestr(s);
  3287. }
  3288. f->default_is_string = false;
  3289. return true;
  3290. }
  3291. void upb_fielddef_setdefaultint64(upb_fielddef *f, int64_t value) {
  3292. if (checksetdefault(f, UPB_TYPE_INT64))
  3293. f->defaultval.sint = value;
  3294. }
  3295. void upb_fielddef_setdefaultint32(upb_fielddef *f, int32_t value) {
  3296. if ((upb_fielddef_type(f) == UPB_TYPE_ENUM &&
  3297. checksetdefault(f, UPB_TYPE_ENUM)) ||
  3298. checksetdefault(f, UPB_TYPE_INT32)) {
  3299. f->defaultval.sint = value;
  3300. }
  3301. }
  3302. void upb_fielddef_setdefaultuint64(upb_fielddef *f, uint64_t value) {
  3303. if (checksetdefault(f, UPB_TYPE_UINT64))
  3304. f->defaultval.uint = value;
  3305. }
  3306. void upb_fielddef_setdefaultuint32(upb_fielddef *f, uint32_t value) {
  3307. if (checksetdefault(f, UPB_TYPE_UINT32))
  3308. f->defaultval.uint = value;
  3309. }
  3310. void upb_fielddef_setdefaultbool(upb_fielddef *f, bool value) {
  3311. if (checksetdefault(f, UPB_TYPE_BOOL))
  3312. f->defaultval.uint = value;
  3313. }
  3314. void upb_fielddef_setdefaultfloat(upb_fielddef *f, float value) {
  3315. if (checksetdefault(f, UPB_TYPE_FLOAT))
  3316. f->defaultval.flt = value;
  3317. }
  3318. void upb_fielddef_setdefaultdouble(upb_fielddef *f, double value) {
  3319. if (checksetdefault(f, UPB_TYPE_DOUBLE))
  3320. f->defaultval.dbl = value;
  3321. }
  3322. bool upb_fielddef_setdefaultstr(upb_fielddef *f, const void *str, size_t len,
  3323. upb_status *s) {
  3324. str_t *str2;
  3325. UPB_ASSERT(upb_fielddef_isstring(f) || f->type_ == UPB_TYPE_ENUM);
  3326. if (f->type_ == UPB_TYPE_ENUM && !upb_isident(str, len, false, s))
  3327. return false;
  3328. if (f->default_is_string) {
  3329. str_t *s = f->defaultval.bytes;
  3330. UPB_ASSERT(s || f->type_ == UPB_TYPE_ENUM);
  3331. if (s) freestr(s);
  3332. } else {
  3333. UPB_ASSERT(f->type_ == UPB_TYPE_ENUM);
  3334. }
  3335. str2 = newstr(str, len);
  3336. f->defaultval.bytes = str2;
  3337. f->default_is_string = true;
  3338. return true;
  3339. }
  3340. void upb_fielddef_setdefaultcstr(upb_fielddef *f, const char *str,
  3341. upb_status *s) {
  3342. UPB_ASSERT(f->type_is_set_);
  3343. upb_fielddef_setdefaultstr(f, str, str ? strlen(str) : 0, s);
  3344. }
  3345. bool upb_fielddef_enumhasdefaultint32(const upb_fielddef *f) {
  3346. int32_t val;
  3347. UPB_ASSERT(f->type_is_set_ && f->type_ == UPB_TYPE_ENUM);
  3348. return enumdefaultint32(f, &val);
  3349. }
  3350. bool upb_fielddef_enumhasdefaultstr(const upb_fielddef *f) {
  3351. UPB_ASSERT(f->type_is_set_ && f->type_ == UPB_TYPE_ENUM);
  3352. return enumdefaultstr(f) != NULL;
  3353. }
  3354. static bool upb_subdef_typecheck(upb_fielddef *f, const upb_def *subdef,
  3355. upb_status *s) {
  3356. if (f->type_ == UPB_TYPE_MESSAGE) {
  3357. if (upb_dyncast_msgdef(subdef)) return true;
  3358. upb_status_seterrmsg(s, "invalid subdef type for this submessage field");
  3359. return false;
  3360. } else if (f->type_ == UPB_TYPE_ENUM) {
  3361. if (upb_dyncast_enumdef(subdef)) return true;
  3362. upb_status_seterrmsg(s, "invalid subdef type for this enum field");
  3363. return false;
  3364. } else {
  3365. upb_status_seterrmsg(s, "only message and enum fields can have a subdef");
  3366. return false;
  3367. }
  3368. }
  3369. static void release_subdef(upb_fielddef *f) {
  3370. if (f->subdef_is_symbolic) {
  3371. upb_gfree(f->sub.name);
  3372. } else if (f->sub.def) {
  3373. upb_unref2(f->sub.def, f);
  3374. }
  3375. }
  3376. bool upb_fielddef_setsubdef(upb_fielddef *f, const upb_def *subdef,
  3377. upb_status *s) {
  3378. UPB_ASSERT(!upb_fielddef_isfrozen(f));
  3379. UPB_ASSERT(upb_fielddef_hassubdef(f));
  3380. if (subdef && !upb_subdef_typecheck(f, subdef, s)) return false;
  3381. release_subdef(f);
  3382. f->sub.def = subdef;
  3383. f->subdef_is_symbolic = false;
  3384. if (f->sub.def) upb_ref2(f->sub.def, f);
  3385. return true;
  3386. }
  3387. bool upb_fielddef_setmsgsubdef(upb_fielddef *f, const upb_msgdef *subdef,
  3388. upb_status *s) {
  3389. return upb_fielddef_setsubdef(f, upb_msgdef_upcast(subdef), s);
  3390. }
  3391. bool upb_fielddef_setenumsubdef(upb_fielddef *f, const upb_enumdef *subdef,
  3392. upb_status *s) {
  3393. return upb_fielddef_setsubdef(f, upb_enumdef_upcast(subdef), s);
  3394. }
  3395. bool upb_fielddef_setsubdefname(upb_fielddef *f, const char *name,
  3396. upb_status *s) {
  3397. char *name_copy;
  3398. UPB_ASSERT(!upb_fielddef_isfrozen(f));
  3399. if (!upb_fielddef_hassubdef(f)) {
  3400. upb_status_seterrmsg(s, "field type does not accept a subdef");
  3401. return false;
  3402. }
  3403. name_copy = upb_gstrdup(name);
  3404. if (!name_copy) {
  3405. upb_upberr_setoom(s);
  3406. return false;
  3407. }
  3408. /* TODO: validate name (upb_isident() doesn't quite work atm because this name
  3409. * may have a leading "."). */
  3410. release_subdef(f);
  3411. f->sub.name = name_copy;
  3412. f->subdef_is_symbolic = true;
  3413. return true;
  3414. }
  3415. bool upb_fielddef_issubmsg(const upb_fielddef *f) {
  3416. return upb_fielddef_type(f) == UPB_TYPE_MESSAGE;
  3417. }
  3418. bool upb_fielddef_isstring(const upb_fielddef *f) {
  3419. return upb_fielddef_type(f) == UPB_TYPE_STRING ||
  3420. upb_fielddef_type(f) == UPB_TYPE_BYTES;
  3421. }
  3422. bool upb_fielddef_isseq(const upb_fielddef *f) {
  3423. return upb_fielddef_label(f) == UPB_LABEL_REPEATED;
  3424. }
  3425. bool upb_fielddef_isprimitive(const upb_fielddef *f) {
  3426. return !upb_fielddef_isstring(f) && !upb_fielddef_issubmsg(f);
  3427. }
  3428. bool upb_fielddef_ismap(const upb_fielddef *f) {
  3429. return upb_fielddef_isseq(f) && upb_fielddef_issubmsg(f) &&
  3430. upb_msgdef_mapentry(upb_fielddef_msgsubdef(f));
  3431. }
  3432. bool upb_fielddef_haspresence(const upb_fielddef *f) {
  3433. if (upb_fielddef_isseq(f)) return false;
  3434. if (upb_fielddef_issubmsg(f)) return true;
  3435. /* Primitive field: return true unless there is a message that specifies
  3436. * presence should not exist. */
  3437. if (f->msg_is_symbolic || !f->msg.def) return true;
  3438. return f->msg.def->syntax == UPB_SYNTAX_PROTO2;
  3439. }
  3440. bool upb_fielddef_hassubdef(const upb_fielddef *f) {
  3441. return upb_fielddef_issubmsg(f) || upb_fielddef_type(f) == UPB_TYPE_ENUM;
  3442. }
  3443. static bool between(int32_t x, int32_t low, int32_t high) {
  3444. return x >= low && x <= high;
  3445. }
  3446. bool upb_fielddef_checklabel(int32_t label) { return between(label, 1, 3); }
  3447. bool upb_fielddef_checktype(int32_t type) { return between(type, 1, 11); }
  3448. bool upb_fielddef_checkintfmt(int32_t fmt) { return between(fmt, 1, 3); }
  3449. bool upb_fielddef_checkdescriptortype(int32_t type) {
  3450. return between(type, 1, 18);
  3451. }
  3452. /* upb_msgdef *****************************************************************/
  3453. static void visitmsg(const upb_refcounted *r, upb_refcounted_visit *visit,
  3454. void *closure) {
  3455. upb_msg_oneof_iter o;
  3456. const upb_msgdef *m = (const upb_msgdef*)r;
  3457. const upb_def *def = upb_msgdef_upcast(m);
  3458. upb_msg_field_iter i;
  3459. for(upb_msg_field_begin(&i, m);
  3460. !upb_msg_field_done(&i);
  3461. upb_msg_field_next(&i)) {
  3462. upb_fielddef *f = upb_msg_iter_field(&i);
  3463. visit(r, upb_fielddef_upcast2(f), closure);
  3464. }
  3465. for(upb_msg_oneof_begin(&o, m);
  3466. !upb_msg_oneof_done(&o);
  3467. upb_msg_oneof_next(&o)) {
  3468. upb_oneofdef *f = upb_msg_iter_oneof(&o);
  3469. visit(r, upb_oneofdef_upcast(f), closure);
  3470. }
  3471. if (upb_def_file(def)) {
  3472. visit(r, upb_filedef_upcast(upb_def_file(def)), closure);
  3473. }
  3474. }
  3475. static void freemsg(upb_refcounted *r) {
  3476. upb_msgdef *m = (upb_msgdef*)r;
  3477. upb_strtable_uninit(&m->ntof);
  3478. upb_inttable_uninit(&m->itof);
  3479. upb_def_uninit(upb_msgdef_upcast_mutable(m));
  3480. upb_gfree(m);
  3481. }
  3482. const struct upb_refcounted_vtbl upb_msgdef_vtbl = {visitmsg, freemsg};
  3483. upb_msgdef *upb_msgdef_new(const void *owner) {
  3484. upb_msgdef *m = upb_gmalloc(sizeof(*m));
  3485. if (!m) return NULL;
  3486. if (!upb_def_init(upb_msgdef_upcast_mutable(m), UPB_DEF_MSG, &upb_msgdef_vtbl,
  3487. owner)) {
  3488. goto err2;
  3489. }
  3490. if (!upb_inttable_init(&m->itof, UPB_CTYPE_PTR)) goto err2;
  3491. if (!upb_strtable_init(&m->ntof, UPB_CTYPE_PTR)) goto err1;
  3492. m->map_entry = false;
  3493. m->syntax = UPB_SYNTAX_PROTO2;
  3494. return m;
  3495. err1:
  3496. upb_inttable_uninit(&m->itof);
  3497. err2:
  3498. upb_gfree(m);
  3499. return NULL;
  3500. }
  3501. bool upb_msgdef_freeze(upb_msgdef *m, upb_status *status) {
  3502. upb_def *d = upb_msgdef_upcast_mutable(m);
  3503. return upb_def_freeze(&d, 1, status);
  3504. }
  3505. const char *upb_msgdef_fullname(const upb_msgdef *m) {
  3506. return upb_def_fullname(upb_msgdef_upcast(m));
  3507. }
  3508. const char *upb_msgdef_name(const upb_msgdef *m) {
  3509. return upb_def_name(upb_msgdef_upcast(m));
  3510. }
  3511. bool upb_msgdef_setfullname(upb_msgdef *m, const char *fullname,
  3512. upb_status *s) {
  3513. return upb_def_setfullname(upb_msgdef_upcast_mutable(m), fullname, s);
  3514. }
  3515. bool upb_msgdef_setsyntax(upb_msgdef *m, upb_syntax_t syntax) {
  3516. if (syntax != UPB_SYNTAX_PROTO2 && syntax != UPB_SYNTAX_PROTO3) {
  3517. return false;
  3518. }
  3519. m->syntax = syntax;
  3520. return true;
  3521. }
  3522. upb_syntax_t upb_msgdef_syntax(const upb_msgdef *m) {
  3523. return m->syntax;
  3524. }
  3525. /* Helper: check that the field |f| is safe to add to msgdef |m|. Set an error
  3526. * on status |s| and return false if not. */
  3527. static bool check_field_add(const upb_msgdef *m, const upb_fielddef *f,
  3528. upb_status *s) {
  3529. if (upb_fielddef_containingtype(f) != NULL) {
  3530. upb_status_seterrmsg(s, "fielddef already belongs to a message");
  3531. return false;
  3532. } else if (upb_fielddef_name(f) == NULL || upb_fielddef_number(f) == 0) {
  3533. upb_status_seterrmsg(s, "field name or number were not set");
  3534. return false;
  3535. } else if (upb_msgdef_itof(m, upb_fielddef_number(f))) {
  3536. upb_status_seterrmsg(s, "duplicate field number");
  3537. return false;
  3538. } else if (upb_strtable_lookup(&m->ntof, upb_fielddef_name(f), NULL)) {
  3539. upb_status_seterrmsg(s, "name conflicts with existing field or oneof");
  3540. return false;
  3541. }
  3542. return true;
  3543. }
  3544. static void add_field(upb_msgdef *m, upb_fielddef *f, const void *ref_donor) {
  3545. release_containingtype(f);
  3546. f->msg.def = m;
  3547. f->msg_is_symbolic = false;
  3548. upb_inttable_insert(&m->itof, upb_fielddef_number(f), upb_value_ptr(f));
  3549. upb_strtable_insert(&m->ntof, upb_fielddef_name(f), upb_value_ptr(f));
  3550. upb_ref2(f, m);
  3551. upb_ref2(m, f);
  3552. if (ref_donor) upb_fielddef_unref(f, ref_donor);
  3553. }
  3554. bool upb_msgdef_addfield(upb_msgdef *m, upb_fielddef *f, const void *ref_donor,
  3555. upb_status *s) {
  3556. /* TODO: extensions need to have a separate namespace, because proto2 allows a
  3557. * top-level extension (ie. one not in any package) to have the same name as a
  3558. * field from the message.
  3559. *
  3560. * This also implies that there needs to be a separate lookup-by-name method
  3561. * for extensions. It seems desirable for iteration to return both extensions
  3562. * and non-extensions though.
  3563. *
  3564. * We also need to validate that the field number is in an extension range iff
  3565. * it is an extension.
  3566. *
  3567. * This method is idempotent. Check if |f| is already part of this msgdef and
  3568. * return immediately if so. */
  3569. if (upb_fielddef_containingtype(f) == m) {
  3570. if (ref_donor) upb_fielddef_unref(f, ref_donor);
  3571. return true;
  3572. }
  3573. /* Check constraints for all fields before performing any action. */
  3574. if (!check_field_add(m, f, s)) {
  3575. return false;
  3576. } else if (upb_fielddef_containingoneof(f) != NULL) {
  3577. /* Fields in a oneof can only be added by adding the oneof to the msgdef. */
  3578. upb_status_seterrmsg(s, "fielddef is part of a oneof");
  3579. return false;
  3580. }
  3581. /* Constraint checks ok, perform the action. */
  3582. add_field(m, f, ref_donor);
  3583. return true;
  3584. }
  3585. bool upb_msgdef_addoneof(upb_msgdef *m, upb_oneofdef *o, const void *ref_donor,
  3586. upb_status *s) {
  3587. upb_oneof_iter it;
  3588. /* Check various conditions that would prevent this oneof from being added. */
  3589. if (upb_oneofdef_containingtype(o)) {
  3590. upb_status_seterrmsg(s, "oneofdef already belongs to a message");
  3591. return false;
  3592. } else if (upb_oneofdef_name(o) == NULL) {
  3593. upb_status_seterrmsg(s, "oneofdef name was not set");
  3594. return false;
  3595. } else if (upb_strtable_lookup(&m->ntof, upb_oneofdef_name(o), NULL)) {
  3596. upb_status_seterrmsg(s, "name conflicts with existing field or oneof");
  3597. return false;
  3598. }
  3599. /* Check that all of the oneof's fields do not conflict with names or numbers
  3600. * of fields already in the message. */
  3601. for (upb_oneof_begin(&it, o); !upb_oneof_done(&it); upb_oneof_next(&it)) {
  3602. const upb_fielddef *f = upb_oneof_iter_field(&it);
  3603. if (!check_field_add(m, f, s)) {
  3604. return false;
  3605. }
  3606. }
  3607. /* Everything checks out -- commit now. */
  3608. /* Add oneof itself first. */
  3609. o->parent = m;
  3610. upb_strtable_insert(&m->ntof, upb_oneofdef_name(o), upb_value_ptr(o));
  3611. upb_ref2(o, m);
  3612. upb_ref2(m, o);
  3613. /* Add each field of the oneof directly to the msgdef. */
  3614. for (upb_oneof_begin(&it, o); !upb_oneof_done(&it); upb_oneof_next(&it)) {
  3615. upb_fielddef *f = upb_oneof_iter_field(&it);
  3616. add_field(m, f, NULL);
  3617. }
  3618. if (ref_donor) upb_oneofdef_unref(o, ref_donor);
  3619. return true;
  3620. }
  3621. const upb_fielddef *upb_msgdef_itof(const upb_msgdef *m, uint32_t i) {
  3622. upb_value val;
  3623. return upb_inttable_lookup32(&m->itof, i, &val) ?
  3624. upb_value_getptr(val) : NULL;
  3625. }
  3626. const upb_fielddef *upb_msgdef_ntof(const upb_msgdef *m, const char *name,
  3627. size_t len) {
  3628. upb_value val;
  3629. if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
  3630. return NULL;
  3631. }
  3632. return upb_trygetfield(upb_value_getptr(val));
  3633. }
  3634. const upb_oneofdef *upb_msgdef_ntoo(const upb_msgdef *m, const char *name,
  3635. size_t len) {
  3636. upb_value val;
  3637. if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
  3638. return NULL;
  3639. }
  3640. return upb_trygetoneof(upb_value_getptr(val));
  3641. }
  3642. bool upb_msgdef_lookupname(const upb_msgdef *m, const char *name, size_t len,
  3643. const upb_fielddef **f, const upb_oneofdef **o) {
  3644. upb_value val;
  3645. if (!upb_strtable_lookup2(&m->ntof, name, len, &val)) {
  3646. return false;
  3647. }
  3648. *o = upb_trygetoneof(upb_value_getptr(val));
  3649. *f = upb_trygetfield(upb_value_getptr(val));
  3650. UPB_ASSERT((*o != NULL) ^ (*f != NULL)); /* Exactly one of the two should be set. */
  3651. return true;
  3652. }
  3653. int upb_msgdef_numfields(const upb_msgdef *m) {
  3654. /* The number table contains only fields. */
  3655. return upb_inttable_count(&m->itof);
  3656. }
  3657. int upb_msgdef_numoneofs(const upb_msgdef *m) {
  3658. /* The name table includes oneofs, and the number table does not. */
  3659. return upb_strtable_count(&m->ntof) - upb_inttable_count(&m->itof);
  3660. }
  3661. void upb_msgdef_setmapentry(upb_msgdef *m, bool map_entry) {
  3662. UPB_ASSERT(!upb_msgdef_isfrozen(m));
  3663. m->map_entry = map_entry;
  3664. }
  3665. bool upb_msgdef_mapentry(const upb_msgdef *m) {
  3666. return m->map_entry;
  3667. }
  3668. void upb_msg_field_begin(upb_msg_field_iter *iter, const upb_msgdef *m) {
  3669. upb_inttable_begin(iter, &m->itof);
  3670. }
  3671. void upb_msg_field_next(upb_msg_field_iter *iter) { upb_inttable_next(iter); }
  3672. bool upb_msg_field_done(const upb_msg_field_iter *iter) {
  3673. return upb_inttable_done(iter);
  3674. }
  3675. upb_fielddef *upb_msg_iter_field(const upb_msg_field_iter *iter) {
  3676. return (upb_fielddef*)upb_value_getptr(upb_inttable_iter_value(iter));
  3677. }
  3678. void upb_msg_field_iter_setdone(upb_msg_field_iter *iter) {
  3679. upb_inttable_iter_setdone(iter);
  3680. }
  3681. void upb_msg_oneof_begin(upb_msg_oneof_iter *iter, const upb_msgdef *m) {
  3682. upb_strtable_begin(iter, &m->ntof);
  3683. /* We need to skip past any initial fields. */
  3684. while (!upb_strtable_done(iter) &&
  3685. !upb_isoneof(upb_value_getptr(upb_strtable_iter_value(iter)))) {
  3686. upb_strtable_next(iter);
  3687. }
  3688. }
  3689. void upb_msg_oneof_next(upb_msg_oneof_iter *iter) {
  3690. /* We need to skip past fields to return only oneofs. */
  3691. do {
  3692. upb_strtable_next(iter);
  3693. } while (!upb_strtable_done(iter) &&
  3694. !upb_isoneof(upb_value_getptr(upb_strtable_iter_value(iter))));
  3695. }
  3696. bool upb_msg_oneof_done(const upb_msg_oneof_iter *iter) {
  3697. return upb_strtable_done(iter);
  3698. }
  3699. upb_oneofdef *upb_msg_iter_oneof(const upb_msg_oneof_iter *iter) {
  3700. return (upb_oneofdef*)upb_value_getptr(upb_strtable_iter_value(iter));
  3701. }
  3702. void upb_msg_oneof_iter_setdone(upb_msg_oneof_iter *iter) {
  3703. upb_strtable_iter_setdone(iter);
  3704. }
  3705. /* upb_oneofdef ***************************************************************/
  3706. static void visitoneof(const upb_refcounted *r, upb_refcounted_visit *visit,
  3707. void *closure) {
  3708. const upb_oneofdef *o = (const upb_oneofdef*)r;
  3709. upb_oneof_iter i;
  3710. for (upb_oneof_begin(&i, o); !upb_oneof_done(&i); upb_oneof_next(&i)) {
  3711. const upb_fielddef *f = upb_oneof_iter_field(&i);
  3712. visit(r, upb_fielddef_upcast2(f), closure);
  3713. }
  3714. if (o->parent) {
  3715. visit(r, upb_msgdef_upcast2(o->parent), closure);
  3716. }
  3717. }
  3718. static void freeoneof(upb_refcounted *r) {
  3719. upb_oneofdef *o = (upb_oneofdef*)r;
  3720. upb_strtable_uninit(&o->ntof);
  3721. upb_inttable_uninit(&o->itof);
  3722. upb_gfree((void*)o->name);
  3723. upb_gfree(o);
  3724. }
  3725. const struct upb_refcounted_vtbl upb_oneofdef_vtbl = {visitoneof, freeoneof};
  3726. upb_oneofdef *upb_oneofdef_new(const void *owner) {
  3727. upb_oneofdef *o = upb_gmalloc(sizeof(*o));
  3728. if (!o) {
  3729. return NULL;
  3730. }
  3731. o->parent = NULL;
  3732. o->name = NULL;
  3733. if (!upb_refcounted_init(upb_oneofdef_upcast_mutable(o), &upb_oneofdef_vtbl,
  3734. owner)) {
  3735. goto err2;
  3736. }
  3737. if (!upb_inttable_init(&o->itof, UPB_CTYPE_PTR)) goto err2;
  3738. if (!upb_strtable_init(&o->ntof, UPB_CTYPE_PTR)) goto err1;
  3739. return o;
  3740. err1:
  3741. upb_inttable_uninit(&o->itof);
  3742. err2:
  3743. upb_gfree(o);
  3744. return NULL;
  3745. }
  3746. const char *upb_oneofdef_name(const upb_oneofdef *o) { return o->name; }
  3747. bool upb_oneofdef_setname(upb_oneofdef *o, const char *name, upb_status *s) {
  3748. UPB_ASSERT(!upb_oneofdef_isfrozen(o));
  3749. if (upb_oneofdef_containingtype(o)) {
  3750. upb_status_seterrmsg(s, "oneof already added to a message");
  3751. return false;
  3752. }
  3753. if (!upb_isident(name, strlen(name), true, s)) {
  3754. return false;
  3755. }
  3756. name = upb_gstrdup(name);
  3757. if (!name) {
  3758. upb_status_seterrmsg(s, "One of memory");
  3759. return false;
  3760. }
  3761. upb_gfree((void*)o->name);
  3762. o->name = name;
  3763. return true;
  3764. }
  3765. const upb_msgdef *upb_oneofdef_containingtype(const upb_oneofdef *o) {
  3766. return o->parent;
  3767. }
  3768. int upb_oneofdef_numfields(const upb_oneofdef *o) {
  3769. return upb_strtable_count(&o->ntof);
  3770. }
  3771. uint32_t upb_oneofdef_index(const upb_oneofdef *o) {
  3772. return o->index;
  3773. }
  3774. bool upb_oneofdef_addfield(upb_oneofdef *o, upb_fielddef *f,
  3775. const void *ref_donor,
  3776. upb_status *s) {
  3777. UPB_ASSERT(!upb_oneofdef_isfrozen(o));
  3778. UPB_ASSERT(!o->parent || !upb_msgdef_isfrozen(o->parent));
  3779. /* This method is idempotent. Check if |f| is already part of this oneofdef
  3780. * and return immediately if so. */
  3781. if (upb_fielddef_containingoneof(f) == o) {
  3782. return true;
  3783. }
  3784. /* The field must have an OPTIONAL label. */
  3785. if (upb_fielddef_label(f) != UPB_LABEL_OPTIONAL) {
  3786. upb_status_seterrmsg(s, "fields in oneof must have OPTIONAL label");
  3787. return false;
  3788. }
  3789. /* Check that no field with this name or number exists already in the oneof.
  3790. * Also check that the field is not already part of a oneof. */
  3791. if (upb_fielddef_name(f) == NULL || upb_fielddef_number(f) == 0) {
  3792. upb_status_seterrmsg(s, "field name or number were not set");
  3793. return false;
  3794. } else if (upb_oneofdef_itof(o, upb_fielddef_number(f)) ||
  3795. upb_oneofdef_ntofz(o, upb_fielddef_name(f))) {
  3796. upb_status_seterrmsg(s, "duplicate field name or number");
  3797. return false;
  3798. } else if (upb_fielddef_containingoneof(f) != NULL) {
  3799. upb_status_seterrmsg(s, "fielddef already belongs to a oneof");
  3800. return false;
  3801. }
  3802. /* We allow adding a field to the oneof either if the field is not part of a
  3803. * msgdef, or if it is and we are also part of the same msgdef. */
  3804. if (o->parent == NULL) {
  3805. /* If we're not in a msgdef, the field cannot be either. Otherwise we would
  3806. * need to magically add this oneof to a msgdef to remain consistent, which
  3807. * is surprising behavior. */
  3808. if (upb_fielddef_containingtype(f) != NULL) {
  3809. upb_status_seterrmsg(s, "fielddef already belongs to a message, but "
  3810. "oneof does not");
  3811. return false;
  3812. }
  3813. } else {
  3814. /* If we're in a msgdef, the user can add fields that either aren't in any
  3815. * msgdef (in which case they're added to our msgdef) or already a part of
  3816. * our msgdef. */
  3817. if (upb_fielddef_containingtype(f) != NULL &&
  3818. upb_fielddef_containingtype(f) != o->parent) {
  3819. upb_status_seterrmsg(s, "fielddef belongs to a different message "
  3820. "than oneof");
  3821. return false;
  3822. }
  3823. }
  3824. /* Commit phase. First add the field to our parent msgdef, if any, because
  3825. * that may fail; then add the field to our own tables. */
  3826. if (o->parent != NULL && upb_fielddef_containingtype(f) == NULL) {
  3827. if (!upb_msgdef_addfield((upb_msgdef*)o->parent, f, NULL, s)) {
  3828. return false;
  3829. }
  3830. }
  3831. release_containingtype(f);
  3832. f->oneof = o;
  3833. upb_inttable_insert(&o->itof, upb_fielddef_number(f), upb_value_ptr(f));
  3834. upb_strtable_insert(&o->ntof, upb_fielddef_name(f), upb_value_ptr(f));
  3835. upb_ref2(f, o);
  3836. upb_ref2(o, f);
  3837. if (ref_donor) upb_fielddef_unref(f, ref_donor);
  3838. return true;
  3839. }
  3840. const upb_fielddef *upb_oneofdef_ntof(const upb_oneofdef *o,
  3841. const char *name, size_t length) {
  3842. upb_value val;
  3843. return upb_strtable_lookup2(&o->ntof, name, length, &val) ?
  3844. upb_value_getptr(val) : NULL;
  3845. }
  3846. const upb_fielddef *upb_oneofdef_itof(const upb_oneofdef *o, uint32_t num) {
  3847. upb_value val;
  3848. return upb_inttable_lookup32(&o->itof, num, &val) ?
  3849. upb_value_getptr(val) : NULL;
  3850. }
  3851. void upb_oneof_begin(upb_oneof_iter *iter, const upb_oneofdef *o) {
  3852. upb_inttable_begin(iter, &o->itof);
  3853. }
  3854. void upb_oneof_next(upb_oneof_iter *iter) {
  3855. upb_inttable_next(iter);
  3856. }
  3857. bool upb_oneof_done(upb_oneof_iter *iter) {
  3858. return upb_inttable_done(iter);
  3859. }
  3860. upb_fielddef *upb_oneof_iter_field(const upb_oneof_iter *iter) {
  3861. return (upb_fielddef*)upb_value_getptr(upb_inttable_iter_value(iter));
  3862. }
  3863. void upb_oneof_iter_setdone(upb_oneof_iter *iter) {
  3864. upb_inttable_iter_setdone(iter);
  3865. }
  3866. /* upb_filedef ****************************************************************/
  3867. static void visitfiledef(const upb_refcounted *r, upb_refcounted_visit *visit,
  3868. void *closure) {
  3869. const upb_filedef *f = (const upb_filedef*)r;
  3870. size_t i;
  3871. for(i = 0; i < upb_filedef_defcount(f); i++) {
  3872. visit(r, upb_def_upcast(upb_filedef_def(f, i)), closure);
  3873. }
  3874. }
  3875. static void freefiledef(upb_refcounted *r) {
  3876. upb_filedef *f = (upb_filedef*)r;
  3877. size_t i;
  3878. for(i = 0; i < upb_filedef_depcount(f); i++) {
  3879. upb_filedef_unref(upb_filedef_dep(f, i), f);
  3880. }
  3881. upb_inttable_uninit(&f->defs);
  3882. upb_inttable_uninit(&f->deps);
  3883. upb_gfree((void*)f->name);
  3884. upb_gfree((void*)f->package);
  3885. upb_gfree((void*)f->phpprefix);
  3886. upb_gfree((void*)f->phpnamespace);
  3887. upb_gfree(f);
  3888. }
  3889. const struct upb_refcounted_vtbl upb_filedef_vtbl = {visitfiledef, freefiledef};
  3890. upb_filedef *upb_filedef_new(const void *owner) {
  3891. upb_filedef *f = upb_gmalloc(sizeof(*f));
  3892. if (!f) {
  3893. return NULL;
  3894. }
  3895. f->package = NULL;
  3896. f->name = NULL;
  3897. f->phpprefix = NULL;
  3898. f->phpnamespace = NULL;
  3899. f->syntax = UPB_SYNTAX_PROTO2;
  3900. if (!upb_refcounted_init(upb_filedef_upcast_mutable(f), &upb_filedef_vtbl,
  3901. owner)) {
  3902. goto err;
  3903. }
  3904. if (!upb_inttable_init(&f->defs, UPB_CTYPE_CONSTPTR)) {
  3905. goto err;
  3906. }
  3907. if (!upb_inttable_init(&f->deps, UPB_CTYPE_CONSTPTR)) {
  3908. goto err2;
  3909. }
  3910. return f;
  3911. err2:
  3912. upb_inttable_uninit(&f->defs);
  3913. err:
  3914. upb_gfree(f);
  3915. return NULL;
  3916. }
  3917. const char *upb_filedef_name(const upb_filedef *f) {
  3918. return f->name;
  3919. }
  3920. const char *upb_filedef_package(const upb_filedef *f) {
  3921. return f->package;
  3922. }
  3923. const char *upb_filedef_phpprefix(const upb_filedef *f) {
  3924. return f->phpprefix;
  3925. }
  3926. const char *upb_filedef_phpnamespace(const upb_filedef *f) {
  3927. return f->phpnamespace;
  3928. }
  3929. upb_syntax_t upb_filedef_syntax(const upb_filedef *f) {
  3930. return f->syntax;
  3931. }
  3932. size_t upb_filedef_defcount(const upb_filedef *f) {
  3933. return upb_inttable_count(&f->defs);
  3934. }
  3935. size_t upb_filedef_depcount(const upb_filedef *f) {
  3936. return upb_inttable_count(&f->deps);
  3937. }
  3938. const upb_def *upb_filedef_def(const upb_filedef *f, size_t i) {
  3939. upb_value v;
  3940. if (upb_inttable_lookup32(&f->defs, i, &v)) {
  3941. return upb_value_getconstptr(v);
  3942. } else {
  3943. return NULL;
  3944. }
  3945. }
  3946. const upb_filedef *upb_filedef_dep(const upb_filedef *f, size_t i) {
  3947. upb_value v;
  3948. if (upb_inttable_lookup32(&f->deps, i, &v)) {
  3949. return upb_value_getconstptr(v);
  3950. } else {
  3951. return NULL;
  3952. }
  3953. }
  3954. bool upb_filedef_setname(upb_filedef *f, const char *name, upb_status *s) {
  3955. name = upb_gstrdup(name);
  3956. if (!name) {
  3957. upb_upberr_setoom(s);
  3958. return false;
  3959. }
  3960. upb_gfree((void*)f->name);
  3961. f->name = name;
  3962. return true;
  3963. }
  3964. bool upb_filedef_setpackage(upb_filedef *f, const char *package,
  3965. upb_status *s) {
  3966. if (!upb_isident(package, strlen(package), true, s)) return false;
  3967. package = upb_gstrdup(package);
  3968. if (!package) {
  3969. upb_upberr_setoom(s);
  3970. return false;
  3971. }
  3972. upb_gfree((void*)f->package);
  3973. f->package = package;
  3974. return true;
  3975. }
  3976. bool upb_filedef_setphpprefix(upb_filedef *f, const char *phpprefix,
  3977. upb_status *s) {
  3978. phpprefix = upb_gstrdup(phpprefix);
  3979. if (!phpprefix) {
  3980. upb_upberr_setoom(s);
  3981. return false;
  3982. }
  3983. upb_gfree((void*)f->phpprefix);
  3984. f->phpprefix = phpprefix;
  3985. return true;
  3986. }
  3987. bool upb_filedef_setphpnamespace(upb_filedef *f, const char *phpnamespace,
  3988. upb_status *s) {
  3989. phpnamespace = upb_gstrdup(phpnamespace);
  3990. if (!phpnamespace) {
  3991. upb_upberr_setoom(s);
  3992. return false;
  3993. }
  3994. upb_gfree((void*)f->phpnamespace);
  3995. f->phpnamespace = phpnamespace;
  3996. return true;
  3997. }
  3998. bool upb_filedef_setsyntax(upb_filedef *f, upb_syntax_t syntax,
  3999. upb_status *s) {
  4000. UPB_UNUSED(s);
  4001. if (syntax != UPB_SYNTAX_PROTO2 &&
  4002. syntax != UPB_SYNTAX_PROTO3) {
  4003. upb_status_seterrmsg(s, "Unknown syntax value.");
  4004. return false;
  4005. }
  4006. f->syntax = syntax;
  4007. {
  4008. /* Set all messages in this file to match. */
  4009. size_t i;
  4010. for (i = 0; i < upb_filedef_defcount(f); i++) {
  4011. /* Casting const away is safe since all defs in mutable filedef must
  4012. * also be mutable. */
  4013. upb_def *def = (upb_def*)upb_filedef_def(f, i);
  4014. upb_msgdef *m = upb_dyncast_msgdef_mutable(def);
  4015. if (m) {
  4016. m->syntax = syntax;
  4017. }
  4018. }
  4019. }
  4020. return true;
  4021. }
  4022. bool upb_filedef_adddef(upb_filedef *f, upb_def *def, const void *ref_donor,
  4023. upb_status *s) {
  4024. if (def->file) {
  4025. upb_status_seterrmsg(s, "Def is already part of another filedef.");
  4026. return false;
  4027. }
  4028. if (upb_inttable_push(&f->defs, upb_value_constptr(def))) {
  4029. def->file = f;
  4030. upb_ref2(def, f);
  4031. upb_ref2(f, def);
  4032. if (ref_donor) upb_def_unref(def, ref_donor);
  4033. if (def->type == UPB_DEF_MSG) {
  4034. upb_downcast_msgdef_mutable(def)->syntax = f->syntax;
  4035. }
  4036. return true;
  4037. } else {
  4038. upb_upberr_setoom(s);
  4039. return false;
  4040. }
  4041. }
  4042. bool upb_filedef_adddep(upb_filedef *f, const upb_filedef *dep) {
  4043. if (upb_inttable_push(&f->deps, upb_value_constptr(dep))) {
  4044. /* Regular ref instead of ref2 because files can't form cycles. */
  4045. upb_filedef_ref(dep, f);
  4046. return true;
  4047. } else {
  4048. return false;
  4049. }
  4050. }
  4051. void upb_symtab_free(upb_symtab *s) {
  4052. upb_strtable_iter i;
  4053. upb_strtable_begin(&i, &s->symtab);
  4054. for (; !upb_strtable_done(&i); upb_strtable_next(&i)) {
  4055. const upb_def *def = upb_value_getptr(upb_strtable_iter_value(&i));
  4056. upb_def_unref(def, s);
  4057. }
  4058. upb_strtable_uninit(&s->symtab);
  4059. upb_gfree(s);
  4060. }
  4061. upb_symtab *upb_symtab_new() {
  4062. upb_symtab *s = upb_gmalloc(sizeof(*s));
  4063. if (!s) {
  4064. return NULL;
  4065. }
  4066. upb_strtable_init(&s->symtab, UPB_CTYPE_PTR);
  4067. return s;
  4068. }
  4069. const upb_def *upb_symtab_lookup(const upb_symtab *s, const char *sym) {
  4070. upb_value v;
  4071. upb_def *ret = upb_strtable_lookup(&s->symtab, sym, &v) ?
  4072. upb_value_getptr(v) : NULL;
  4073. return ret;
  4074. }
  4075. const upb_msgdef *upb_symtab_lookupmsg(const upb_symtab *s, const char *sym) {
  4076. upb_value v;
  4077. upb_def *def = upb_strtable_lookup(&s->symtab, sym, &v) ?
  4078. upb_value_getptr(v) : NULL;
  4079. return def ? upb_dyncast_msgdef(def) : NULL;
  4080. }
  4081. const upb_enumdef *upb_symtab_lookupenum(const upb_symtab *s, const char *sym) {
  4082. upb_value v;
  4083. upb_def *def = upb_strtable_lookup(&s->symtab, sym, &v) ?
  4084. upb_value_getptr(v) : NULL;
  4085. return def ? upb_dyncast_enumdef(def) : NULL;
  4086. }
  4087. /* Given a symbol and the base symbol inside which it is defined, find the
  4088. * symbol's definition in t. */
  4089. static upb_def *upb_resolvename(const upb_strtable *t,
  4090. const char *base, const char *sym) {
  4091. if(strlen(sym) == 0) return NULL;
  4092. if(sym[0] == '.') {
  4093. /* Symbols starting with '.' are absolute, so we do a single lookup.
  4094. * Slice to omit the leading '.' */
  4095. upb_value v;
  4096. return upb_strtable_lookup(t, sym + 1, &v) ? upb_value_getptr(v) : NULL;
  4097. } else {
  4098. /* Remove components from base until we find an entry or run out.
  4099. * TODO: This branch is totally broken, but currently not used. */
  4100. (void)base;
  4101. UPB_ASSERT(false);
  4102. return NULL;
  4103. }
  4104. }
  4105. const upb_def *upb_symtab_resolve(const upb_symtab *s, const char *base,
  4106. const char *sym) {
  4107. upb_def *ret = upb_resolvename(&s->symtab, base, sym);
  4108. return ret;
  4109. }
  4110. /* TODO(haberman): we need a lot more testing of error conditions. */
  4111. static bool symtab_add(upb_symtab *s, upb_def *const*defs, size_t n,
  4112. void *ref_donor, upb_refcounted *freeze_also,
  4113. upb_status *status) {
  4114. size_t i;
  4115. size_t add_n;
  4116. size_t freeze_n;
  4117. upb_strtable_iter iter;
  4118. upb_refcounted **add_objs = NULL;
  4119. upb_def **add_defs = NULL;
  4120. size_t add_objs_size;
  4121. upb_strtable addtab;
  4122. if (n == 0 && !freeze_also) {
  4123. return true;
  4124. }
  4125. if (!upb_strtable_init(&addtab, UPB_CTYPE_PTR)) {
  4126. upb_status_seterrmsg(status, "out of memory");
  4127. return false;
  4128. }
  4129. /* Add new defs to our "add" set. */
  4130. for (i = 0; i < n; i++) {
  4131. upb_def *def = defs[i];
  4132. const char *fullname;
  4133. upb_fielddef *f;
  4134. if (upb_def_isfrozen(def)) {
  4135. upb_status_seterrmsg(status, "added defs must be mutable");
  4136. goto err;
  4137. }
  4138. UPB_ASSERT(!upb_def_isfrozen(def));
  4139. fullname = upb_def_fullname(def);
  4140. if (!fullname) {
  4141. upb_status_seterrmsg(
  4142. status, "Anonymous defs cannot be added to a symtab");
  4143. goto err;
  4144. }
  4145. f = upb_dyncast_fielddef_mutable(def);
  4146. if (f) {
  4147. if (!upb_fielddef_containingtypename(f)) {
  4148. upb_status_seterrmsg(status,
  4149. "Standalone fielddefs must have a containing type "
  4150. "(extendee) name set");
  4151. goto err;
  4152. }
  4153. } else {
  4154. if (upb_strtable_lookup(&addtab, fullname, NULL)) {
  4155. upb_status_seterrf(status, "Conflicting defs named '%s'", fullname);
  4156. goto err;
  4157. }
  4158. if (upb_strtable_lookup(&s->symtab, fullname, NULL)) {
  4159. upb_status_seterrf(status, "Symtab already has a def named '%s'",
  4160. fullname);
  4161. goto err;
  4162. }
  4163. if (!upb_strtable_insert(&addtab, fullname, upb_value_ptr(def)))
  4164. goto oom_err;
  4165. upb_def_donateref(def, ref_donor, s);
  4166. }
  4167. if (upb_dyncast_fielddef_mutable(def)) {
  4168. /* TODO(haberman): allow adding extensions attached to files. */
  4169. upb_status_seterrf(status, "Can't add extensions to symtab.\n");
  4170. goto err;
  4171. }
  4172. }
  4173. /* Now using the table, resolve symbolic references for subdefs. */
  4174. upb_strtable_begin(&iter, &addtab);
  4175. for (; !upb_strtable_done(&iter); upb_strtable_next(&iter)) {
  4176. const char *base;
  4177. upb_def *def = upb_value_getptr(upb_strtable_iter_value(&iter));
  4178. upb_msgdef *m = upb_dyncast_msgdef_mutable(def);
  4179. upb_msg_field_iter j;
  4180. if (!m) continue;
  4181. /* Type names are resolved relative to the message in which they appear. */
  4182. base = upb_msgdef_fullname(m);
  4183. for(upb_msg_field_begin(&j, m);
  4184. !upb_msg_field_done(&j);
  4185. upb_msg_field_next(&j)) {
  4186. upb_fielddef *f = upb_msg_iter_field(&j);
  4187. const char *name = upb_fielddef_subdefname(f);
  4188. if (name && !upb_fielddef_subdef(f)) {
  4189. /* Try the lookup in the current set of to-be-added defs first. If not
  4190. * there, try existing defs. */
  4191. upb_def *subdef = upb_resolvename(&addtab, base, name);
  4192. if (subdef == NULL) {
  4193. subdef = upb_resolvename(&s->symtab, base, name);
  4194. }
  4195. if (subdef == NULL) {
  4196. upb_status_seterrf(
  4197. status, "couldn't resolve name '%s' in message '%s'", name, base);
  4198. goto err;
  4199. } else if (!upb_fielddef_setsubdef(f, subdef, status)) {
  4200. goto err;
  4201. }
  4202. }
  4203. }
  4204. }
  4205. /* We need an array of the defs in addtab, for passing to
  4206. * upb_refcounted_freeze(). */
  4207. add_objs_size = upb_strtable_count(&addtab);
  4208. if (freeze_also) {
  4209. add_objs_size++;
  4210. }
  4211. add_defs = upb_gmalloc(sizeof(void*) * add_objs_size);
  4212. if (add_defs == NULL) goto oom_err;
  4213. upb_strtable_begin(&iter, &addtab);
  4214. for (add_n = 0; !upb_strtable_done(&iter); upb_strtable_next(&iter)) {
  4215. add_defs[add_n++] = upb_value_getptr(upb_strtable_iter_value(&iter));
  4216. }
  4217. /* Validate defs. */
  4218. if (!_upb_def_validate(add_defs, add_n, status)) {
  4219. goto err;
  4220. }
  4221. /* Cheat a little and give the array a new type.
  4222. * This is probably undefined behavior, but this code will be deleted soon. */
  4223. add_objs = (upb_refcounted**)add_defs;
  4224. freeze_n = add_n;
  4225. if (freeze_also) {
  4226. add_objs[freeze_n++] = freeze_also;
  4227. }
  4228. if (!upb_refcounted_freeze(add_objs, freeze_n, status,
  4229. UPB_MAX_MESSAGE_DEPTH * 2)) {
  4230. goto err;
  4231. }
  4232. /* This must be delayed until all errors have been detected, since error
  4233. * recovery code uses this table to cleanup defs. */
  4234. upb_strtable_uninit(&addtab);
  4235. /* TODO(haberman) we don't properly handle errors after this point (like
  4236. * OOM in upb_strtable_insert() below). */
  4237. for (i = 0; i < add_n; i++) {
  4238. upb_def *def = (upb_def*)add_objs[i];
  4239. const char *name = upb_def_fullname(def);
  4240. bool success;
  4241. success = upb_strtable_insert(&s->symtab, name, upb_value_ptr(def));
  4242. UPB_ASSERT(success);
  4243. }
  4244. upb_gfree(add_defs);
  4245. return true;
  4246. oom_err:
  4247. upb_status_seterrmsg(status, "out of memory");
  4248. err: {
  4249. /* We need to donate the refs back. */
  4250. upb_strtable_begin(&iter, &addtab);
  4251. for (; !upb_strtable_done(&iter); upb_strtable_next(&iter)) {
  4252. upb_def *def = upb_value_getptr(upb_strtable_iter_value(&iter));
  4253. upb_def_donateref(def, s, ref_donor);
  4254. }
  4255. }
  4256. upb_strtable_uninit(&addtab);
  4257. upb_gfree(add_defs);
  4258. UPB_ASSERT(!upb_ok(status));
  4259. return false;
  4260. }
  4261. bool upb_symtab_add(upb_symtab *s, upb_def *const*defs, size_t n,
  4262. void *ref_donor, upb_status *status) {
  4263. return symtab_add(s, defs, n, ref_donor, NULL, status);
  4264. }
  4265. bool upb_symtab_addfile(upb_symtab *s, upb_filedef *file, upb_status *status) {
  4266. size_t n;
  4267. size_t i;
  4268. upb_def **defs;
  4269. bool ret;
  4270. n = upb_filedef_defcount(file);
  4271. if (n == 0) {
  4272. return true;
  4273. }
  4274. defs = upb_gmalloc(sizeof(*defs) * n);
  4275. if (defs == NULL) {
  4276. upb_status_seterrmsg(status, "Out of memory");
  4277. return false;
  4278. }
  4279. for (i = 0; i < n; i++) {
  4280. defs[i] = upb_filedef_mutabledef(file, i);
  4281. }
  4282. ret = symtab_add(s, defs, n, NULL, upb_filedef_upcast_mutable(file), status);
  4283. upb_gfree(defs);
  4284. return ret;
  4285. }
  4286. /* Iteration. */
  4287. static void advance_to_matching(upb_symtab_iter *iter) {
  4288. if (iter->type == UPB_DEF_ANY)
  4289. return;
  4290. while (!upb_strtable_done(&iter->iter) &&
  4291. iter->type != upb_symtab_iter_def(iter)->type) {
  4292. upb_strtable_next(&iter->iter);
  4293. }
  4294. }
  4295. void upb_symtab_begin(upb_symtab_iter *iter, const upb_symtab *s,
  4296. upb_deftype_t type) {
  4297. upb_strtable_begin(&iter->iter, &s->symtab);
  4298. iter->type = type;
  4299. advance_to_matching(iter);
  4300. }
  4301. void upb_symtab_next(upb_symtab_iter *iter) {
  4302. upb_strtable_next(&iter->iter);
  4303. advance_to_matching(iter);
  4304. }
  4305. bool upb_symtab_done(const upb_symtab_iter *iter) {
  4306. return upb_strtable_done(&iter->iter);
  4307. }
  4308. const upb_def *upb_symtab_iter_def(const upb_symtab_iter *iter) {
  4309. return upb_value_getptr(upb_strtable_iter_value(&iter->iter));
  4310. }
  4311. /* We encode backwards, to avoid pre-computing lengths (one-pass encode). */
  4312. #define UPB_PB_VARINT_MAX_LEN 10
  4313. #define CHK(x) do { if (!(x)) { return false; } } while(0)
  4314. /* Maps descriptor type -> upb field type. */
  4315. static const uint8_t upb_desctype_to_fieldtype2[] = {
  4316. UPB_WIRE_TYPE_END_GROUP, /* ENDGROUP */
  4317. UPB_TYPE_DOUBLE, /* DOUBLE */
  4318. UPB_TYPE_FLOAT, /* FLOAT */
  4319. UPB_TYPE_INT64, /* INT64 */
  4320. UPB_TYPE_UINT64, /* UINT64 */
  4321. UPB_TYPE_INT32, /* INT32 */
  4322. UPB_TYPE_UINT64, /* FIXED64 */
  4323. UPB_TYPE_UINT32, /* FIXED32 */
  4324. UPB_TYPE_BOOL, /* BOOL */
  4325. UPB_TYPE_STRING, /* STRING */
  4326. UPB_TYPE_MESSAGE, /* GROUP */
  4327. UPB_TYPE_MESSAGE, /* MESSAGE */
  4328. UPB_TYPE_BYTES, /* BYTES */
  4329. UPB_TYPE_UINT32, /* UINT32 */
  4330. UPB_TYPE_ENUM, /* ENUM */
  4331. UPB_TYPE_INT32, /* SFIXED32 */
  4332. UPB_TYPE_INT64, /* SFIXED64 */
  4333. UPB_TYPE_INT32, /* SINT32 */
  4334. UPB_TYPE_INT64, /* SINT64 */
  4335. };
  4336. static size_t upb_encode_varint(uint64_t val, char *buf) {
  4337. size_t i;
  4338. if (val < 128) { buf[0] = val; return 1; }
  4339. i = 0;
  4340. while (val) {
  4341. uint8_t byte = val & 0x7fU;
  4342. val >>= 7;
  4343. if (val) byte |= 0x80U;
  4344. buf[i++] = byte;
  4345. }
  4346. return i;
  4347. }
  4348. static uint32_t upb_zzencode_32(int32_t n) { return (n << 1) ^ (n >> 31); }
  4349. static uint64_t upb_zzencode_64(int64_t n) { return (n << 1) ^ (n >> 63); }
  4350. typedef struct {
  4351. upb_env *env;
  4352. char *buf, *ptr, *limit;
  4353. } upb_encstate;
  4354. static size_t upb_roundup_pow2(size_t bytes) {
  4355. size_t ret = 128;
  4356. while (ret < bytes) {
  4357. ret *= 2;
  4358. }
  4359. return ret;
  4360. }
  4361. static bool upb_encode_growbuffer(upb_encstate *e, size_t bytes) {
  4362. size_t old_size = e->limit - e->buf;
  4363. size_t new_size = upb_roundup_pow2(bytes + (e->limit - e->ptr));
  4364. char *new_buf = upb_env_realloc(e->env, e->buf, old_size, new_size);
  4365. CHK(new_buf);
  4366. /* We want previous data at the end, realloc() put it at the beginning. */
  4367. memmove(new_buf + new_size - old_size, e->buf, old_size);
  4368. e->ptr = new_buf + new_size - (e->limit - e->ptr);
  4369. e->limit = new_buf + new_size;
  4370. e->buf = new_buf;
  4371. return true;
  4372. }
  4373. /* Call to ensure that at least "bytes" bytes are available for writing at
  4374. * e->ptr. Returns false if the bytes could not be allocated. */
  4375. static bool upb_encode_reserve(upb_encstate *e, size_t bytes) {
  4376. CHK(UPB_LIKELY((size_t)(e->ptr - e->buf) >= bytes) ||
  4377. upb_encode_growbuffer(e, bytes));
  4378. e->ptr -= bytes;
  4379. return true;
  4380. }
  4381. /* Writes the given bytes to the buffer, handling reserve/advance. */
  4382. static bool upb_put_bytes(upb_encstate *e, const void *data, size_t len) {
  4383. CHK(upb_encode_reserve(e, len));
  4384. memcpy(e->ptr, data, len);
  4385. return true;
  4386. }
  4387. static bool upb_put_fixed64(upb_encstate *e, uint64_t val) {
  4388. /* TODO(haberman): byte-swap for big endian. */
  4389. return upb_put_bytes(e, &val, sizeof(uint64_t));
  4390. }
  4391. static bool upb_put_fixed32(upb_encstate *e, uint32_t val) {
  4392. /* TODO(haberman): byte-swap for big endian. */
  4393. return upb_put_bytes(e, &val, sizeof(uint32_t));
  4394. }
  4395. static bool upb_put_varint(upb_encstate *e, uint64_t val) {
  4396. size_t len;
  4397. char *start;
  4398. CHK(upb_encode_reserve(e, UPB_PB_VARINT_MAX_LEN));
  4399. len = upb_encode_varint(val, e->ptr);
  4400. start = e->ptr + UPB_PB_VARINT_MAX_LEN - len;
  4401. memmove(start, e->ptr, len);
  4402. e->ptr = start;
  4403. return true;
  4404. }
  4405. static bool upb_put_double(upb_encstate *e, double d) {
  4406. uint64_t u64;
  4407. UPB_ASSERT(sizeof(double) == sizeof(uint64_t));
  4408. memcpy(&u64, &d, sizeof(uint64_t));
  4409. return upb_put_fixed64(e, u64);
  4410. }
  4411. static bool upb_put_float(upb_encstate *e, float d) {
  4412. uint32_t u32;
  4413. UPB_ASSERT(sizeof(float) == sizeof(uint32_t));
  4414. memcpy(&u32, &d, sizeof(uint32_t));
  4415. return upb_put_fixed32(e, u32);
  4416. }
  4417. static uint32_t upb_readcase(const char *msg, const upb_msglayout_msginit_v1 *m,
  4418. int oneof_index) {
  4419. uint32_t ret;
  4420. memcpy(&ret, msg + m->oneofs[oneof_index].case_offset, sizeof(ret));
  4421. return ret;
  4422. }
  4423. static bool upb_readhasbit(const char *msg,
  4424. const upb_msglayout_fieldinit_v1 *f) {
  4425. UPB_ASSERT(f->hasbit != UPB_NO_HASBIT);
  4426. return msg[f->hasbit / 8] & (1 << (f->hasbit % 8));
  4427. }
  4428. static bool upb_put_tag(upb_encstate *e, int field_number, int wire_type) {
  4429. return upb_put_varint(e, (field_number << 3) | wire_type);
  4430. }
  4431. static bool upb_put_fixedarray(upb_encstate *e, const upb_array *arr,
  4432. size_t size) {
  4433. size_t bytes = arr->len * size;
  4434. return upb_put_bytes(e, arr->data, bytes) && upb_put_varint(e, bytes);
  4435. }
  4436. bool upb_encode_message(upb_encstate *e, const char *msg,
  4437. const upb_msglayout_msginit_v1 *m,
  4438. size_t *size);
  4439. static bool upb_encode_array(upb_encstate *e, const char *field_mem,
  4440. const upb_msglayout_msginit_v1 *m,
  4441. const upb_msglayout_fieldinit_v1 *f) {
  4442. const upb_array *arr = *(const upb_array**)field_mem;
  4443. if (arr == NULL || arr->len == 0) {
  4444. return true;
  4445. }
  4446. UPB_ASSERT(arr->type == upb_desctype_to_fieldtype2[f->descriptortype]);
  4447. #define VARINT_CASE(ctype, encode) { \
  4448. ctype *start = arr->data; \
  4449. ctype *ptr = start + arr->len; \
  4450. size_t pre_len = e->limit - e->ptr; \
  4451. do { \
  4452. ptr--; \
  4453. CHK(upb_put_varint(e, encode)); \
  4454. } while (ptr != start); \
  4455. CHK(upb_put_varint(e, e->limit - e->ptr - pre_len)); \
  4456. } \
  4457. break; \
  4458. do { ; } while(0)
  4459. switch (f->descriptortype) {
  4460. case UPB_DESCRIPTOR_TYPE_DOUBLE:
  4461. CHK(upb_put_fixedarray(e, arr, sizeof(double)));
  4462. break;
  4463. case UPB_DESCRIPTOR_TYPE_FLOAT:
  4464. CHK(upb_put_fixedarray(e, arr, sizeof(float)));
  4465. break;
  4466. case UPB_DESCRIPTOR_TYPE_SFIXED64:
  4467. case UPB_DESCRIPTOR_TYPE_FIXED64:
  4468. CHK(upb_put_fixedarray(e, arr, sizeof(uint64_t)));
  4469. break;
  4470. case UPB_DESCRIPTOR_TYPE_FIXED32:
  4471. case UPB_DESCRIPTOR_TYPE_SFIXED32:
  4472. CHK(upb_put_fixedarray(e, arr, sizeof(uint32_t)));
  4473. break;
  4474. case UPB_DESCRIPTOR_TYPE_INT64:
  4475. case UPB_DESCRIPTOR_TYPE_UINT64:
  4476. VARINT_CASE(uint64_t, *ptr);
  4477. case UPB_DESCRIPTOR_TYPE_UINT32:
  4478. VARINT_CASE(uint32_t, *ptr);
  4479. case UPB_DESCRIPTOR_TYPE_INT32:
  4480. case UPB_DESCRIPTOR_TYPE_ENUM:
  4481. VARINT_CASE(int32_t, (int64_t)*ptr);
  4482. case UPB_DESCRIPTOR_TYPE_BOOL:
  4483. VARINT_CASE(bool, *ptr);
  4484. case UPB_DESCRIPTOR_TYPE_SINT32:
  4485. VARINT_CASE(int32_t, upb_zzencode_32(*ptr));
  4486. case UPB_DESCRIPTOR_TYPE_SINT64:
  4487. VARINT_CASE(int64_t, upb_zzencode_64(*ptr));
  4488. case UPB_DESCRIPTOR_TYPE_STRING:
  4489. case UPB_DESCRIPTOR_TYPE_BYTES: {
  4490. upb_stringview *start = arr->data;
  4491. upb_stringview *ptr = start + arr->len;
  4492. do {
  4493. ptr--;
  4494. CHK(upb_put_bytes(e, ptr->data, ptr->size) &&
  4495. upb_put_varint(e, ptr->size) &&
  4496. upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
  4497. } while (ptr != start);
  4498. return true;
  4499. }
  4500. case UPB_DESCRIPTOR_TYPE_GROUP: {
  4501. void **start = arr->data;
  4502. void **ptr = start + arr->len;
  4503. const upb_msglayout_msginit_v1 *subm = m->submsgs[f->submsg_index];
  4504. do {
  4505. size_t size;
  4506. ptr--;
  4507. CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP) &&
  4508. upb_encode_message(e, *ptr, subm, &size) &&
  4509. upb_put_tag(e, f->number, UPB_WIRE_TYPE_START_GROUP));
  4510. } while (ptr != start);
  4511. return true;
  4512. }
  4513. case UPB_DESCRIPTOR_TYPE_MESSAGE: {
  4514. void **start = arr->data;
  4515. void **ptr = start + arr->len;
  4516. const upb_msglayout_msginit_v1 *subm = m->submsgs[f->submsg_index];
  4517. do {
  4518. size_t size;
  4519. ptr--;
  4520. CHK(upb_encode_message(e, *ptr, subm, &size) &&
  4521. upb_put_varint(e, size) &&
  4522. upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
  4523. } while (ptr != start);
  4524. return true;
  4525. }
  4526. }
  4527. #undef VARINT_CASE
  4528. /* We encode all primitive arrays as packed, regardless of what was specified
  4529. * in the .proto file. Could special case 1-sized arrays. */
  4530. CHK(upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED));
  4531. return true;
  4532. }
  4533. static bool upb_encode_scalarfield(upb_encstate *e, const char *field_mem,
  4534. const upb_msglayout_msginit_v1 *m,
  4535. const upb_msglayout_fieldinit_v1 *f,
  4536. bool is_proto3) {
  4537. bool skip_zero_value = is_proto3 && f->oneof_index == UPB_NOT_IN_ONEOF;
  4538. #define CASE(ctype, type, wire_type, encodeval) do { \
  4539. ctype val = *(ctype*)field_mem; \
  4540. if (skip_zero_value && val == 0) { \
  4541. return true; \
  4542. } \
  4543. return upb_put_ ## type(e, encodeval) && \
  4544. upb_put_tag(e, f->number, wire_type); \
  4545. } while(0)
  4546. switch (f->descriptortype) {
  4547. case UPB_DESCRIPTOR_TYPE_DOUBLE:
  4548. CASE(double, double, UPB_WIRE_TYPE_64BIT, val);
  4549. case UPB_DESCRIPTOR_TYPE_FLOAT:
  4550. CASE(float, float, UPB_WIRE_TYPE_32BIT, val);
  4551. case UPB_DESCRIPTOR_TYPE_INT64:
  4552. case UPB_DESCRIPTOR_TYPE_UINT64:
  4553. CASE(uint64_t, varint, UPB_WIRE_TYPE_VARINT, val);
  4554. case UPB_DESCRIPTOR_TYPE_UINT32:
  4555. CASE(uint32_t, varint, UPB_WIRE_TYPE_VARINT, val);
  4556. case UPB_DESCRIPTOR_TYPE_INT32:
  4557. case UPB_DESCRIPTOR_TYPE_ENUM:
  4558. CASE(int32_t, varint, UPB_WIRE_TYPE_VARINT, (int64_t)val);
  4559. case UPB_DESCRIPTOR_TYPE_SFIXED64:
  4560. case UPB_DESCRIPTOR_TYPE_FIXED64:
  4561. CASE(uint64_t, fixed64, UPB_WIRE_TYPE_64BIT, val);
  4562. case UPB_DESCRIPTOR_TYPE_FIXED32:
  4563. case UPB_DESCRIPTOR_TYPE_SFIXED32:
  4564. CASE(uint32_t, fixed32, UPB_WIRE_TYPE_32BIT, val);
  4565. case UPB_DESCRIPTOR_TYPE_BOOL:
  4566. CASE(bool, varint, UPB_WIRE_TYPE_VARINT, val);
  4567. case UPB_DESCRIPTOR_TYPE_SINT32:
  4568. CASE(int32_t, varint, UPB_WIRE_TYPE_VARINT, upb_zzencode_32(val));
  4569. case UPB_DESCRIPTOR_TYPE_SINT64:
  4570. CASE(int64_t, varint, UPB_WIRE_TYPE_VARINT, upb_zzencode_64(val));
  4571. case UPB_DESCRIPTOR_TYPE_STRING:
  4572. case UPB_DESCRIPTOR_TYPE_BYTES: {
  4573. upb_stringview view = *(upb_stringview*)field_mem;
  4574. if (skip_zero_value && view.size == 0) {
  4575. return true;
  4576. }
  4577. return upb_put_bytes(e, view.data, view.size) &&
  4578. upb_put_varint(e, view.size) &&
  4579. upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
  4580. }
  4581. case UPB_DESCRIPTOR_TYPE_GROUP: {
  4582. size_t size;
  4583. void *submsg = *(void**)field_mem;
  4584. const upb_msglayout_msginit_v1 *subm = m->submsgs[f->submsg_index];
  4585. if (skip_zero_value && submsg == NULL) {
  4586. return true;
  4587. }
  4588. return upb_put_tag(e, f->number, UPB_WIRE_TYPE_END_GROUP) &&
  4589. upb_encode_message(e, submsg, subm, &size) &&
  4590. upb_put_tag(e, f->number, UPB_WIRE_TYPE_START_GROUP);
  4591. }
  4592. case UPB_DESCRIPTOR_TYPE_MESSAGE: {
  4593. size_t size;
  4594. void *submsg = *(void**)field_mem;
  4595. const upb_msglayout_msginit_v1 *subm = m->submsgs[f->submsg_index];
  4596. if (skip_zero_value && submsg == NULL) {
  4597. return true;
  4598. }
  4599. return upb_encode_message(e, submsg, subm, &size) &&
  4600. upb_put_varint(e, size) &&
  4601. upb_put_tag(e, f->number, UPB_WIRE_TYPE_DELIMITED);
  4602. }
  4603. }
  4604. #undef CASE
  4605. UPB_UNREACHABLE();
  4606. }
  4607. bool upb_encode_hasscalarfield(const char *msg,
  4608. const upb_msglayout_msginit_v1 *m,
  4609. const upb_msglayout_fieldinit_v1 *f) {
  4610. if (f->oneof_index != UPB_NOT_IN_ONEOF) {
  4611. return upb_readcase(msg, m, f->oneof_index) == f->number;
  4612. } else if (m->is_proto2) {
  4613. return upb_readhasbit(msg, f);
  4614. } else {
  4615. /* For proto3, we'll test for the field being empty later. */
  4616. return true;
  4617. }
  4618. }
  4619. bool upb_encode_message(upb_encstate* e, const char *msg,
  4620. const upb_msglayout_msginit_v1 *m,
  4621. size_t *size) {
  4622. int i;
  4623. size_t pre_len = e->limit - e->ptr;
  4624. if (msg == NULL) {
  4625. return true;
  4626. }
  4627. for (i = m->field_count - 1; i >= 0; i--) {
  4628. const upb_msglayout_fieldinit_v1 *f = &m->fields[i];
  4629. if (f->label == UPB_LABEL_REPEATED) {
  4630. CHK(upb_encode_array(e, msg + f->offset, m, f));
  4631. } else {
  4632. if (upb_encode_hasscalarfield(msg, m, f)) {
  4633. if (f->oneof_index == UPB_NOT_IN_ONEOF) {
  4634. CHK(upb_encode_scalarfield(e, msg + f->offset, m, f, !m->is_proto2));
  4635. } else {
  4636. const upb_msglayout_oneofinit_v1 *o = &m->oneofs[f->oneof_index];
  4637. CHK(upb_encode_scalarfield(e, msg + o->data_offset,
  4638. m, f, !m->is_proto2));
  4639. }
  4640. }
  4641. }
  4642. }
  4643. *size = (e->limit - e->ptr) - pre_len;
  4644. return true;
  4645. }
  4646. char *upb_encode(const void *msg, const upb_msglayout_msginit_v1 *m,
  4647. upb_env *env, size_t *size) {
  4648. upb_encstate e;
  4649. e.env = env;
  4650. e.buf = NULL;
  4651. e.limit = NULL;
  4652. e.ptr = NULL;
  4653. if (!upb_encode_message(&e, msg, m, size)) {
  4654. *size = 0;
  4655. return NULL;
  4656. }
  4657. *size = e.limit - e.ptr;
  4658. if (*size == 0) {
  4659. static char ch;
  4660. return &ch;
  4661. } else {
  4662. UPB_ASSERT(e.ptr);
  4663. return e.ptr;
  4664. }
  4665. }
  4666. #undef CHK
  4667. /*
  4668. ** TODO(haberman): it's unclear whether a lot of the consistency checks should
  4669. ** UPB_ASSERT() or return false.
  4670. */
  4671. #include <string.h>
  4672. static void *upb_calloc(size_t size) {
  4673. void *mem = upb_gmalloc(size);
  4674. if (mem) {
  4675. memset(mem, 0, size);
  4676. }
  4677. return mem;
  4678. }
  4679. /* Defined for the sole purpose of having a unique pointer value for
  4680. * UPB_NO_CLOSURE. */
  4681. char _upb_noclosure;
  4682. static void freehandlers(upb_refcounted *r) {
  4683. upb_handlers *h = (upb_handlers*)r;
  4684. upb_inttable_iter i;
  4685. upb_inttable_begin(&i, &h->cleanup_);
  4686. for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  4687. void *val = (void*)upb_inttable_iter_key(&i);
  4688. upb_value func_val = upb_inttable_iter_value(&i);
  4689. upb_handlerfree *func = upb_value_getfptr(func_val);
  4690. func(val);
  4691. }
  4692. upb_inttable_uninit(&h->cleanup_);
  4693. upb_msgdef_unref(h->msg, h);
  4694. upb_gfree(h->sub);
  4695. upb_gfree(h);
  4696. }
  4697. static void visithandlers(const upb_refcounted *r, upb_refcounted_visit *visit,
  4698. void *closure) {
  4699. const upb_handlers *h = (const upb_handlers*)r;
  4700. upb_msg_field_iter i;
  4701. for(upb_msg_field_begin(&i, h->msg);
  4702. !upb_msg_field_done(&i);
  4703. upb_msg_field_next(&i)) {
  4704. upb_fielddef *f = upb_msg_iter_field(&i);
  4705. const upb_handlers *sub;
  4706. if (!upb_fielddef_issubmsg(f)) continue;
  4707. sub = upb_handlers_getsubhandlers(h, f);
  4708. if (sub) visit(r, upb_handlers_upcast(sub), closure);
  4709. }
  4710. }
  4711. static const struct upb_refcounted_vtbl vtbl = {visithandlers, freehandlers};
  4712. typedef struct {
  4713. upb_inttable tab; /* maps upb_msgdef* -> upb_handlers*. */
  4714. upb_handlers_callback *callback;
  4715. const void *closure;
  4716. } dfs_state;
  4717. /* TODO(haberman): discard upb_handlers* objects that do not actually have any
  4718. * handlers set and cannot reach any upb_handlers* object that does. This is
  4719. * slightly tricky to do correctly. */
  4720. static upb_handlers *newformsg(const upb_msgdef *m, const void *owner,
  4721. dfs_state *s) {
  4722. upb_msg_field_iter i;
  4723. upb_handlers *h = upb_handlers_new(m, owner);
  4724. if (!h) return NULL;
  4725. if (!upb_inttable_insertptr(&s->tab, m, upb_value_ptr(h))) goto oom;
  4726. s->callback(s->closure, h);
  4727. /* For each submessage field, get or create a handlers object and set it as
  4728. * the subhandlers. */
  4729. for(upb_msg_field_begin(&i, m);
  4730. !upb_msg_field_done(&i);
  4731. upb_msg_field_next(&i)) {
  4732. upb_fielddef *f = upb_msg_iter_field(&i);
  4733. const upb_msgdef *subdef;
  4734. upb_value subm_ent;
  4735. if (!upb_fielddef_issubmsg(f)) continue;
  4736. subdef = upb_downcast_msgdef(upb_fielddef_subdef(f));
  4737. if (upb_inttable_lookupptr(&s->tab, subdef, &subm_ent)) {
  4738. upb_handlers_setsubhandlers(h, f, upb_value_getptr(subm_ent));
  4739. } else {
  4740. upb_handlers *sub_mh = newformsg(subdef, &sub_mh, s);
  4741. if (!sub_mh) goto oom;
  4742. upb_handlers_setsubhandlers(h, f, sub_mh);
  4743. upb_handlers_unref(sub_mh, &sub_mh);
  4744. }
  4745. }
  4746. return h;
  4747. oom:
  4748. upb_handlers_unref(h, owner);
  4749. return NULL;
  4750. }
  4751. /* Given a selector for a STARTSUBMSG handler, resolves to a pointer to the
  4752. * subhandlers for this submessage field. */
  4753. #define SUBH(h, selector) (h->sub[selector])
  4754. /* The selector for a submessage field is the field index. */
  4755. #define SUBH_F(h, f) SUBH(h, f->index_)
  4756. static int32_t trygetsel(upb_handlers *h, const upb_fielddef *f,
  4757. upb_handlertype_t type) {
  4758. upb_selector_t sel;
  4759. UPB_ASSERT(!upb_handlers_isfrozen(h));
  4760. if (upb_handlers_msgdef(h) != upb_fielddef_containingtype(f)) {
  4761. upb_status_seterrf(
  4762. &h->status_, "type mismatch: field %s does not belong to message %s",
  4763. upb_fielddef_name(f), upb_msgdef_fullname(upb_handlers_msgdef(h)));
  4764. return -1;
  4765. }
  4766. if (!upb_handlers_getselector(f, type, &sel)) {
  4767. upb_status_seterrf(
  4768. &h->status_,
  4769. "type mismatch: cannot register handler type %d for field %s",
  4770. type, upb_fielddef_name(f));
  4771. return -1;
  4772. }
  4773. return sel;
  4774. }
  4775. static upb_selector_t handlers_getsel(upb_handlers *h, const upb_fielddef *f,
  4776. upb_handlertype_t type) {
  4777. int32_t sel = trygetsel(h, f, type);
  4778. UPB_ASSERT(sel >= 0);
  4779. return sel;
  4780. }
  4781. static const void **returntype(upb_handlers *h, const upb_fielddef *f,
  4782. upb_handlertype_t type) {
  4783. return &h->table[handlers_getsel(h, f, type)].attr.return_closure_type_;
  4784. }
  4785. static bool doset(upb_handlers *h, int32_t sel, const upb_fielddef *f,
  4786. upb_handlertype_t type, upb_func *func,
  4787. upb_handlerattr *attr) {
  4788. upb_handlerattr set_attr = UPB_HANDLERATTR_INITIALIZER;
  4789. const void *closure_type;
  4790. const void **context_closure_type;
  4791. UPB_ASSERT(!upb_handlers_isfrozen(h));
  4792. if (sel < 0) {
  4793. upb_status_seterrmsg(&h->status_,
  4794. "incorrect handler type for this field.");
  4795. return false;
  4796. }
  4797. if (h->table[sel].func) {
  4798. upb_status_seterrmsg(&h->status_,
  4799. "cannot change handler once it has been set.");
  4800. return false;
  4801. }
  4802. if (attr) {
  4803. set_attr = *attr;
  4804. }
  4805. /* Check that the given closure type matches the closure type that has been
  4806. * established for this context (if any). */
  4807. closure_type = upb_handlerattr_closuretype(&set_attr);
  4808. if (type == UPB_HANDLER_STRING) {
  4809. context_closure_type = returntype(h, f, UPB_HANDLER_STARTSTR);
  4810. } else if (f && upb_fielddef_isseq(f) &&
  4811. type != UPB_HANDLER_STARTSEQ &&
  4812. type != UPB_HANDLER_ENDSEQ) {
  4813. context_closure_type = returntype(h, f, UPB_HANDLER_STARTSEQ);
  4814. } else {
  4815. context_closure_type = &h->top_closure_type;
  4816. }
  4817. if (closure_type && *context_closure_type &&
  4818. closure_type != *context_closure_type) {
  4819. /* TODO(haberman): better message for debugging. */
  4820. if (f) {
  4821. upb_status_seterrf(&h->status_,
  4822. "closure type does not match for field %s",
  4823. upb_fielddef_name(f));
  4824. } else {
  4825. upb_status_seterrmsg(
  4826. &h->status_, "closure type does not match for message-level handler");
  4827. }
  4828. return false;
  4829. }
  4830. if (closure_type)
  4831. *context_closure_type = closure_type;
  4832. /* If this is a STARTSEQ or STARTSTR handler, check that the returned pointer
  4833. * matches any pre-existing expectations about what type is expected. */
  4834. if (type == UPB_HANDLER_STARTSEQ || type == UPB_HANDLER_STARTSTR) {
  4835. const void *return_type = upb_handlerattr_returnclosuretype(&set_attr);
  4836. const void *table_return_type =
  4837. upb_handlerattr_returnclosuretype(&h->table[sel].attr);
  4838. if (return_type && table_return_type && return_type != table_return_type) {
  4839. upb_status_seterrmsg(&h->status_, "closure return type does not match");
  4840. return false;
  4841. }
  4842. if (table_return_type && !return_type)
  4843. upb_handlerattr_setreturnclosuretype(&set_attr, table_return_type);
  4844. }
  4845. h->table[sel].func = (upb_func*)func;
  4846. h->table[sel].attr = set_attr;
  4847. return true;
  4848. }
  4849. /* Returns the effective closure type for this handler (which will propagate
  4850. * from outer frames if this frame has no START* handler). Not implemented for
  4851. * UPB_HANDLER_STRING at the moment since this is not needed. Returns NULL is
  4852. * the effective closure type is unspecified (either no handler was registered
  4853. * to specify it or the handler that was registered did not specify the closure
  4854. * type). */
  4855. const void *effective_closure_type(upb_handlers *h, const upb_fielddef *f,
  4856. upb_handlertype_t type) {
  4857. const void *ret;
  4858. upb_selector_t sel;
  4859. UPB_ASSERT(type != UPB_HANDLER_STRING);
  4860. ret = h->top_closure_type;
  4861. if (upb_fielddef_isseq(f) &&
  4862. type != UPB_HANDLER_STARTSEQ &&
  4863. type != UPB_HANDLER_ENDSEQ &&
  4864. h->table[sel = handlers_getsel(h, f, UPB_HANDLER_STARTSEQ)].func) {
  4865. ret = upb_handlerattr_returnclosuretype(&h->table[sel].attr);
  4866. }
  4867. if (type == UPB_HANDLER_STRING &&
  4868. h->table[sel = handlers_getsel(h, f, UPB_HANDLER_STARTSTR)].func) {
  4869. ret = upb_handlerattr_returnclosuretype(&h->table[sel].attr);
  4870. }
  4871. /* The effective type of the submessage; not used yet.
  4872. * if (type == SUBMESSAGE &&
  4873. * h->table[sel = handlers_getsel(h, f, UPB_HANDLER_STARTSUBMSG)].func) {
  4874. * ret = upb_handlerattr_returnclosuretype(&h->table[sel].attr);
  4875. * } */
  4876. return ret;
  4877. }
  4878. /* Checks whether the START* handler specified by f & type is missing even
  4879. * though it is required to convert the established type of an outer frame
  4880. * ("closure_type") into the established type of an inner frame (represented in
  4881. * the return closure type of this handler's attr. */
  4882. bool checkstart(upb_handlers *h, const upb_fielddef *f, upb_handlertype_t type,
  4883. upb_status *status) {
  4884. const void *closure_type;
  4885. const upb_handlerattr *attr;
  4886. const void *return_closure_type;
  4887. upb_selector_t sel = handlers_getsel(h, f, type);
  4888. if (h->table[sel].func) return true;
  4889. closure_type = effective_closure_type(h, f, type);
  4890. attr = &h->table[sel].attr;
  4891. return_closure_type = upb_handlerattr_returnclosuretype(attr);
  4892. if (closure_type && return_closure_type &&
  4893. closure_type != return_closure_type) {
  4894. upb_status_seterrf(status,
  4895. "expected start handler to return sub type for field %f",
  4896. upb_fielddef_name(f));
  4897. return false;
  4898. }
  4899. return true;
  4900. }
  4901. /* Public interface ***********************************************************/
  4902. upb_handlers *upb_handlers_new(const upb_msgdef *md, const void *owner) {
  4903. int extra;
  4904. upb_handlers *h;
  4905. UPB_ASSERT(upb_msgdef_isfrozen(md));
  4906. extra = sizeof(upb_handlers_tabent) * (md->selector_count - 1);
  4907. h = upb_calloc(sizeof(*h) + extra);
  4908. if (!h) return NULL;
  4909. h->msg = md;
  4910. upb_msgdef_ref(h->msg, h);
  4911. upb_status_clear(&h->status_);
  4912. if (md->submsg_field_count > 0) {
  4913. h->sub = upb_calloc(md->submsg_field_count * sizeof(*h->sub));
  4914. if (!h->sub) goto oom;
  4915. } else {
  4916. h->sub = 0;
  4917. }
  4918. if (!upb_refcounted_init(upb_handlers_upcast_mutable(h), &vtbl, owner))
  4919. goto oom;
  4920. if (!upb_inttable_init(&h->cleanup_, UPB_CTYPE_FPTR)) goto oom;
  4921. /* calloc() above initialized all handlers to NULL. */
  4922. return h;
  4923. oom:
  4924. freehandlers(upb_handlers_upcast_mutable(h));
  4925. return NULL;
  4926. }
  4927. const upb_handlers *upb_handlers_newfrozen(const upb_msgdef *m,
  4928. const void *owner,
  4929. upb_handlers_callback *callback,
  4930. const void *closure) {
  4931. dfs_state state;
  4932. upb_handlers *ret;
  4933. bool ok;
  4934. upb_refcounted *r;
  4935. state.callback = callback;
  4936. state.closure = closure;
  4937. if (!upb_inttable_init(&state.tab, UPB_CTYPE_PTR)) return NULL;
  4938. ret = newformsg(m, owner, &state);
  4939. upb_inttable_uninit(&state.tab);
  4940. if (!ret) return NULL;
  4941. r = upb_handlers_upcast_mutable(ret);
  4942. ok = upb_refcounted_freeze(&r, 1, NULL, UPB_MAX_HANDLER_DEPTH);
  4943. UPB_ASSERT(ok);
  4944. return ret;
  4945. }
  4946. const upb_status *upb_handlers_status(upb_handlers *h) {
  4947. UPB_ASSERT(!upb_handlers_isfrozen(h));
  4948. return &h->status_;
  4949. }
  4950. void upb_handlers_clearerr(upb_handlers *h) {
  4951. UPB_ASSERT(!upb_handlers_isfrozen(h));
  4952. upb_status_clear(&h->status_);
  4953. }
  4954. #define SETTER(name, handlerctype, handlertype) \
  4955. bool upb_handlers_set ## name(upb_handlers *h, const upb_fielddef *f, \
  4956. handlerctype func, upb_handlerattr *attr) { \
  4957. int32_t sel = trygetsel(h, f, handlertype); \
  4958. return doset(h, sel, f, handlertype, (upb_func*)func, attr); \
  4959. }
  4960. SETTER(int32, upb_int32_handlerfunc*, UPB_HANDLER_INT32)
  4961. SETTER(int64, upb_int64_handlerfunc*, UPB_HANDLER_INT64)
  4962. SETTER(uint32, upb_uint32_handlerfunc*, UPB_HANDLER_UINT32)
  4963. SETTER(uint64, upb_uint64_handlerfunc*, UPB_HANDLER_UINT64)
  4964. SETTER(float, upb_float_handlerfunc*, UPB_HANDLER_FLOAT)
  4965. SETTER(double, upb_double_handlerfunc*, UPB_HANDLER_DOUBLE)
  4966. SETTER(bool, upb_bool_handlerfunc*, UPB_HANDLER_BOOL)
  4967. SETTER(startstr, upb_startstr_handlerfunc*, UPB_HANDLER_STARTSTR)
  4968. SETTER(string, upb_string_handlerfunc*, UPB_HANDLER_STRING)
  4969. SETTER(endstr, upb_endfield_handlerfunc*, UPB_HANDLER_ENDSTR)
  4970. SETTER(startseq, upb_startfield_handlerfunc*, UPB_HANDLER_STARTSEQ)
  4971. SETTER(startsubmsg, upb_startfield_handlerfunc*, UPB_HANDLER_STARTSUBMSG)
  4972. SETTER(endsubmsg, upb_endfield_handlerfunc*, UPB_HANDLER_ENDSUBMSG)
  4973. SETTER(endseq, upb_endfield_handlerfunc*, UPB_HANDLER_ENDSEQ)
  4974. #undef SETTER
  4975. bool upb_handlers_setunknown(upb_handlers *h, upb_unknown_handlerfunc *func,
  4976. upb_handlerattr *attr) {
  4977. return doset(h, UPB_UNKNOWN_SELECTOR, NULL, UPB_HANDLER_INT32,
  4978. (upb_func *)func, attr);
  4979. }
  4980. bool upb_handlers_setstartmsg(upb_handlers *h, upb_startmsg_handlerfunc *func,
  4981. upb_handlerattr *attr) {
  4982. return doset(h, UPB_STARTMSG_SELECTOR, NULL, UPB_HANDLER_INT32,
  4983. (upb_func *)func, attr);
  4984. }
  4985. bool upb_handlers_setendmsg(upb_handlers *h, upb_endmsg_handlerfunc *func,
  4986. upb_handlerattr *attr) {
  4987. UPB_ASSERT(!upb_handlers_isfrozen(h));
  4988. return doset(h, UPB_ENDMSG_SELECTOR, NULL, UPB_HANDLER_INT32,
  4989. (upb_func *)func, attr);
  4990. }
  4991. bool upb_handlers_setsubhandlers(upb_handlers *h, const upb_fielddef *f,
  4992. const upb_handlers *sub) {
  4993. UPB_ASSERT(sub);
  4994. UPB_ASSERT(!upb_handlers_isfrozen(h));
  4995. UPB_ASSERT(upb_fielddef_issubmsg(f));
  4996. if (SUBH_F(h, f)) return false; /* Can't reset. */
  4997. if (upb_msgdef_upcast(upb_handlers_msgdef(sub)) != upb_fielddef_subdef(f)) {
  4998. return false;
  4999. }
  5000. SUBH_F(h, f) = sub;
  5001. upb_ref2(sub, h);
  5002. return true;
  5003. }
  5004. const upb_handlers *upb_handlers_getsubhandlers(const upb_handlers *h,
  5005. const upb_fielddef *f) {
  5006. UPB_ASSERT(upb_fielddef_issubmsg(f));
  5007. return SUBH_F(h, f);
  5008. }
  5009. bool upb_handlers_getattr(const upb_handlers *h, upb_selector_t sel,
  5010. upb_handlerattr *attr) {
  5011. if (!upb_handlers_gethandler(h, sel))
  5012. return false;
  5013. *attr = h->table[sel].attr;
  5014. return true;
  5015. }
  5016. const upb_handlers *upb_handlers_getsubhandlers_sel(const upb_handlers *h,
  5017. upb_selector_t sel) {
  5018. /* STARTSUBMSG selector in sel is the field's selector base. */
  5019. return SUBH(h, sel - UPB_STATIC_SELECTOR_COUNT);
  5020. }
  5021. const upb_msgdef *upb_handlers_msgdef(const upb_handlers *h) { return h->msg; }
  5022. bool upb_handlers_addcleanup(upb_handlers *h, void *p, upb_handlerfree *func) {
  5023. bool ok;
  5024. if (upb_inttable_lookupptr(&h->cleanup_, p, NULL)) {
  5025. return false;
  5026. }
  5027. ok = upb_inttable_insertptr(&h->cleanup_, p, upb_value_fptr(func));
  5028. UPB_ASSERT(ok);
  5029. return true;
  5030. }
  5031. /* "Static" methods ***********************************************************/
  5032. bool upb_handlers_freeze(upb_handlers *const*handlers, int n, upb_status *s) {
  5033. /* TODO: verify we have a transitive closure. */
  5034. int i;
  5035. for (i = 0; i < n; i++) {
  5036. upb_msg_field_iter j;
  5037. upb_handlers *h = handlers[i];
  5038. if (!upb_ok(&h->status_)) {
  5039. upb_status_seterrf(s, "handlers for message %s had error status: %s",
  5040. upb_msgdef_fullname(upb_handlers_msgdef(h)),
  5041. upb_status_errmsg(&h->status_));
  5042. return false;
  5043. }
  5044. /* Check that there are no closure mismatches due to missing Start* handlers
  5045. * or subhandlers with different type-level types. */
  5046. for(upb_msg_field_begin(&j, h->msg);
  5047. !upb_msg_field_done(&j);
  5048. upb_msg_field_next(&j)) {
  5049. const upb_fielddef *f = upb_msg_iter_field(&j);
  5050. if (upb_fielddef_isseq(f)) {
  5051. if (!checkstart(h, f, UPB_HANDLER_STARTSEQ, s))
  5052. return false;
  5053. }
  5054. if (upb_fielddef_isstring(f)) {
  5055. if (!checkstart(h, f, UPB_HANDLER_STARTSTR, s))
  5056. return false;
  5057. }
  5058. if (upb_fielddef_issubmsg(f)) {
  5059. bool hashandler = false;
  5060. if (upb_handlers_gethandler(
  5061. h, handlers_getsel(h, f, UPB_HANDLER_STARTSUBMSG)) ||
  5062. upb_handlers_gethandler(
  5063. h, handlers_getsel(h, f, UPB_HANDLER_ENDSUBMSG))) {
  5064. hashandler = true;
  5065. }
  5066. if (upb_fielddef_isseq(f) &&
  5067. (upb_handlers_gethandler(
  5068. h, handlers_getsel(h, f, UPB_HANDLER_STARTSEQ)) ||
  5069. upb_handlers_gethandler(
  5070. h, handlers_getsel(h, f, UPB_HANDLER_ENDSEQ)))) {
  5071. hashandler = true;
  5072. }
  5073. if (hashandler && !upb_handlers_getsubhandlers(h, f)) {
  5074. /* For now we add an empty subhandlers in this case. It makes the
  5075. * decoder code generator simpler, because it only has to handle two
  5076. * cases (submessage has handlers or not) as opposed to three
  5077. * (submessage has handlers in enclosing message but no subhandlers).
  5078. *
  5079. * This makes parsing less efficient in the case that we want to
  5080. * notice a submessage but skip its contents (like if we're testing
  5081. * for submessage presence or counting the number of repeated
  5082. * submessages). In this case we will end up parsing the submessage
  5083. * field by field and throwing away the results for each, instead of
  5084. * skipping the whole delimited thing at once. If this is an issue we
  5085. * can revisit it, but do remember that this only arises when you have
  5086. * handlers (startseq/startsubmsg/endsubmsg/endseq) set for the
  5087. * submessage but no subhandlers. The uses cases for this are
  5088. * limited. */
  5089. upb_handlers *sub = upb_handlers_new(upb_fielddef_msgsubdef(f), &sub);
  5090. upb_handlers_setsubhandlers(h, f, sub);
  5091. upb_handlers_unref(sub, &sub);
  5092. }
  5093. /* TODO(haberman): check type of submessage.
  5094. * This is slightly tricky; also consider whether we should check that
  5095. * they match at setsubhandlers time. */
  5096. }
  5097. }
  5098. }
  5099. if (!upb_refcounted_freeze((upb_refcounted*const*)handlers, n, s,
  5100. UPB_MAX_HANDLER_DEPTH)) {
  5101. return false;
  5102. }
  5103. return true;
  5104. }
  5105. upb_handlertype_t upb_handlers_getprimitivehandlertype(const upb_fielddef *f) {
  5106. switch (upb_fielddef_type(f)) {
  5107. case UPB_TYPE_INT32:
  5108. case UPB_TYPE_ENUM: return UPB_HANDLER_INT32;
  5109. case UPB_TYPE_INT64: return UPB_HANDLER_INT64;
  5110. case UPB_TYPE_UINT32: return UPB_HANDLER_UINT32;
  5111. case UPB_TYPE_UINT64: return UPB_HANDLER_UINT64;
  5112. case UPB_TYPE_FLOAT: return UPB_HANDLER_FLOAT;
  5113. case UPB_TYPE_DOUBLE: return UPB_HANDLER_DOUBLE;
  5114. case UPB_TYPE_BOOL: return UPB_HANDLER_BOOL;
  5115. default: UPB_ASSERT(false); return -1; /* Invalid input. */
  5116. }
  5117. }
  5118. bool upb_handlers_getselector(const upb_fielddef *f, upb_handlertype_t type,
  5119. upb_selector_t *s) {
  5120. switch (type) {
  5121. case UPB_HANDLER_INT32:
  5122. case UPB_HANDLER_INT64:
  5123. case UPB_HANDLER_UINT32:
  5124. case UPB_HANDLER_UINT64:
  5125. case UPB_HANDLER_FLOAT:
  5126. case UPB_HANDLER_DOUBLE:
  5127. case UPB_HANDLER_BOOL:
  5128. if (!upb_fielddef_isprimitive(f) ||
  5129. upb_handlers_getprimitivehandlertype(f) != type)
  5130. return false;
  5131. *s = f->selector_base;
  5132. break;
  5133. case UPB_HANDLER_STRING:
  5134. if (upb_fielddef_isstring(f)) {
  5135. *s = f->selector_base;
  5136. } else if (upb_fielddef_lazy(f)) {
  5137. *s = f->selector_base + 3;
  5138. } else {
  5139. return false;
  5140. }
  5141. break;
  5142. case UPB_HANDLER_STARTSTR:
  5143. if (upb_fielddef_isstring(f) || upb_fielddef_lazy(f)) {
  5144. *s = f->selector_base + 1;
  5145. } else {
  5146. return false;
  5147. }
  5148. break;
  5149. case UPB_HANDLER_ENDSTR:
  5150. if (upb_fielddef_isstring(f) || upb_fielddef_lazy(f)) {
  5151. *s = f->selector_base + 2;
  5152. } else {
  5153. return false;
  5154. }
  5155. break;
  5156. case UPB_HANDLER_STARTSEQ:
  5157. if (!upb_fielddef_isseq(f)) return false;
  5158. *s = f->selector_base - 2;
  5159. break;
  5160. case UPB_HANDLER_ENDSEQ:
  5161. if (!upb_fielddef_isseq(f)) return false;
  5162. *s = f->selector_base - 1;
  5163. break;
  5164. case UPB_HANDLER_STARTSUBMSG:
  5165. if (!upb_fielddef_issubmsg(f)) return false;
  5166. /* Selectors for STARTSUBMSG are at the beginning of the table so that the
  5167. * selector can also be used as an index into the "sub" array of
  5168. * subhandlers. The indexes for the two into these two tables are the
  5169. * same, except that in the handler table the static selectors come first. */
  5170. *s = f->index_ + UPB_STATIC_SELECTOR_COUNT;
  5171. break;
  5172. case UPB_HANDLER_ENDSUBMSG:
  5173. if (!upb_fielddef_issubmsg(f)) return false;
  5174. *s = f->selector_base;
  5175. break;
  5176. }
  5177. UPB_ASSERT((size_t)*s < upb_fielddef_containingtype(f)->selector_count);
  5178. return true;
  5179. }
  5180. uint32_t upb_handlers_selectorbaseoffset(const upb_fielddef *f) {
  5181. return upb_fielddef_isseq(f) ? 2 : 0;
  5182. }
  5183. uint32_t upb_handlers_selectorcount(const upb_fielddef *f) {
  5184. uint32_t ret = 1;
  5185. if (upb_fielddef_isseq(f)) ret += 2; /* STARTSEQ/ENDSEQ */
  5186. if (upb_fielddef_isstring(f)) ret += 2; /* [STRING]/STARTSTR/ENDSTR */
  5187. if (upb_fielddef_issubmsg(f)) {
  5188. /* ENDSUBMSG (STARTSUBMSG is at table beginning) */
  5189. ret += 0;
  5190. if (upb_fielddef_lazy(f)) {
  5191. /* STARTSTR/ENDSTR/STRING (for lazy) */
  5192. ret += 3;
  5193. }
  5194. }
  5195. return ret;
  5196. }
  5197. /* upb_handlerattr ************************************************************/
  5198. void upb_handlerattr_init(upb_handlerattr *attr) {
  5199. upb_handlerattr from = UPB_HANDLERATTR_INITIALIZER;
  5200. memcpy(attr, &from, sizeof(*attr));
  5201. }
  5202. void upb_handlerattr_uninit(upb_handlerattr *attr) {
  5203. UPB_UNUSED(attr);
  5204. }
  5205. bool upb_handlerattr_sethandlerdata(upb_handlerattr *attr, const void *hd) {
  5206. attr->handler_data_ = hd;
  5207. return true;
  5208. }
  5209. bool upb_handlerattr_setclosuretype(upb_handlerattr *attr, const void *type) {
  5210. attr->closure_type_ = type;
  5211. return true;
  5212. }
  5213. const void *upb_handlerattr_closuretype(const upb_handlerattr *attr) {
  5214. return attr->closure_type_;
  5215. }
  5216. bool upb_handlerattr_setreturnclosuretype(upb_handlerattr *attr,
  5217. const void *type) {
  5218. attr->return_closure_type_ = type;
  5219. return true;
  5220. }
  5221. const void *upb_handlerattr_returnclosuretype(const upb_handlerattr *attr) {
  5222. return attr->return_closure_type_;
  5223. }
  5224. bool upb_handlerattr_setalwaysok(upb_handlerattr *attr, bool alwaysok) {
  5225. attr->alwaysok_ = alwaysok;
  5226. return true;
  5227. }
  5228. bool upb_handlerattr_alwaysok(const upb_handlerattr *attr) {
  5229. return attr->alwaysok_;
  5230. }
  5231. /* upb_bufhandle **************************************************************/
  5232. size_t upb_bufhandle_objofs(const upb_bufhandle *h) {
  5233. return h->objofs_;
  5234. }
  5235. /* upb_byteshandler ***********************************************************/
  5236. void upb_byteshandler_init(upb_byteshandler* h) {
  5237. memset(h, 0, sizeof(*h));
  5238. }
  5239. /* For when we support handlerfree callbacks. */
  5240. void upb_byteshandler_uninit(upb_byteshandler* h) {
  5241. UPB_UNUSED(h);
  5242. }
  5243. bool upb_byteshandler_setstartstr(upb_byteshandler *h,
  5244. upb_startstr_handlerfunc *func, void *d) {
  5245. h->table[UPB_STARTSTR_SELECTOR].func = (upb_func*)func;
  5246. h->table[UPB_STARTSTR_SELECTOR].attr.handler_data_ = d;
  5247. return true;
  5248. }
  5249. bool upb_byteshandler_setstring(upb_byteshandler *h,
  5250. upb_string_handlerfunc *func, void *d) {
  5251. h->table[UPB_STRING_SELECTOR].func = (upb_func*)func;
  5252. h->table[UPB_STRING_SELECTOR].attr.handler_data_ = d;
  5253. return true;
  5254. }
  5255. bool upb_byteshandler_setendstr(upb_byteshandler *h,
  5256. upb_endfield_handlerfunc *func, void *d) {
  5257. h->table[UPB_ENDSTR_SELECTOR].func = (upb_func*)func;
  5258. h->table[UPB_ENDSTR_SELECTOR].attr.handler_data_ = d;
  5259. return true;
  5260. }
  5261. static bool is_power_of_two(size_t val) {
  5262. return (val & (val - 1)) == 0;
  5263. }
  5264. /* Align up to the given power of 2. */
  5265. static size_t align_up(size_t val, size_t align) {
  5266. UPB_ASSERT(is_power_of_two(align));
  5267. return (val + align - 1) & ~(align - 1);
  5268. }
  5269. static size_t div_round_up(size_t n, size_t d) {
  5270. return (n + d - 1) / d;
  5271. }
  5272. bool upb_fieldtype_mapkeyok(upb_fieldtype_t type) {
  5273. return type == UPB_TYPE_BOOL || type == UPB_TYPE_INT32 ||
  5274. type == UPB_TYPE_UINT32 || type == UPB_TYPE_INT64 ||
  5275. type == UPB_TYPE_UINT64 || type == UPB_TYPE_STRING;
  5276. }
  5277. void *upb_array_pack(const upb_array *arr, void *p, size_t *ofs, size_t size);
  5278. void *upb_map_pack(const upb_map *map, void *p, size_t *ofs, size_t size);
  5279. #define PTR_AT(msg, ofs, type) (type*)((char*)msg + ofs)
  5280. #define VOIDPTR_AT(msg, ofs) PTR_AT(msg, ofs, void)
  5281. #define ENCODE_MAX_NESTING 64
  5282. #define CHECK_TRUE(x) if (!(x)) { return false; }
  5283. /** upb_msgval ****************************************************************/
  5284. #define upb_alignof(t) offsetof(struct { char c; t x; }, x)
  5285. /* These functions will generate real memcpy() calls on ARM sadly, because
  5286. * the compiler assumes they might not be aligned. */
  5287. static upb_msgval upb_msgval_read(const void *p, size_t ofs,
  5288. uint8_t size) {
  5289. upb_msgval val;
  5290. p = (char*)p + ofs;
  5291. memcpy(&val, p, size);
  5292. return val;
  5293. }
  5294. static void upb_msgval_write(void *p, size_t ofs, upb_msgval val,
  5295. uint8_t size) {
  5296. p = (char*)p + ofs;
  5297. memcpy(p, &val, size);
  5298. }
  5299. static size_t upb_msgval_sizeof(upb_fieldtype_t type) {
  5300. switch (type) {
  5301. case UPB_TYPE_DOUBLE:
  5302. case UPB_TYPE_INT64:
  5303. case UPB_TYPE_UINT64:
  5304. return 8;
  5305. case UPB_TYPE_ENUM:
  5306. case UPB_TYPE_INT32:
  5307. case UPB_TYPE_UINT32:
  5308. case UPB_TYPE_FLOAT:
  5309. return 4;
  5310. case UPB_TYPE_BOOL:
  5311. return 1;
  5312. case UPB_TYPE_MESSAGE:
  5313. return sizeof(void*);
  5314. case UPB_TYPE_BYTES:
  5315. case UPB_TYPE_STRING:
  5316. return sizeof(upb_stringview);
  5317. }
  5318. UPB_UNREACHABLE();
  5319. }
  5320. static uint8_t upb_msg_fieldsize(const upb_msglayout_fieldinit_v1 *field) {
  5321. if (field->label == UPB_LABEL_REPEATED) {
  5322. return sizeof(void*);
  5323. } else {
  5324. return upb_msgval_sizeof(upb_desctype_to_fieldtype[field->descriptortype]);
  5325. }
  5326. }
  5327. static uint8_t upb_msg_fielddefsize(const upb_fielddef *f) {
  5328. if (upb_fielddef_isseq(f)) {
  5329. return sizeof(void*);
  5330. } else {
  5331. return upb_msgval_sizeof(upb_fielddef_type(f));
  5332. }
  5333. }
  5334. /* TODO(haberman): this is broken right now because upb_msgval can contain
  5335. * a char* / size_t pair, which is too big for a upb_value. To fix this
  5336. * we'll probably need to dynamically allocate a upb_msgval and store a
  5337. * pointer to that in the tables for extensions/maps. */
  5338. static upb_value upb_toval(upb_msgval val) {
  5339. upb_value ret;
  5340. UPB_UNUSED(val);
  5341. memset(&ret, 0, sizeof(upb_value)); /* XXX */
  5342. return ret;
  5343. }
  5344. static upb_msgval upb_msgval_fromval(upb_value val) {
  5345. upb_msgval ret;
  5346. UPB_UNUSED(val);
  5347. memset(&ret, 0, sizeof(upb_msgval)); /* XXX */
  5348. return ret;
  5349. }
  5350. static upb_ctype_t upb_fieldtotabtype(upb_fieldtype_t type) {
  5351. switch (type) {
  5352. case UPB_TYPE_FLOAT: return UPB_CTYPE_FLOAT;
  5353. case UPB_TYPE_DOUBLE: return UPB_CTYPE_DOUBLE;
  5354. case UPB_TYPE_BOOL: return UPB_CTYPE_BOOL;
  5355. case UPB_TYPE_BYTES:
  5356. case UPB_TYPE_MESSAGE:
  5357. case UPB_TYPE_STRING: return UPB_CTYPE_CONSTPTR;
  5358. case UPB_TYPE_ENUM:
  5359. case UPB_TYPE_INT32: return UPB_CTYPE_INT32;
  5360. case UPB_TYPE_UINT32: return UPB_CTYPE_UINT32;
  5361. case UPB_TYPE_INT64: return UPB_CTYPE_INT64;
  5362. case UPB_TYPE_UINT64: return UPB_CTYPE_UINT64;
  5363. default: UPB_ASSERT(false); return 0;
  5364. }
  5365. }
  5366. static upb_msgval upb_msgval_fromdefault(const upb_fielddef *f) {
  5367. switch (upb_fielddef_type(f)) {
  5368. case UPB_TYPE_FLOAT:
  5369. return upb_msgval_float(upb_fielddef_defaultfloat(f));
  5370. case UPB_TYPE_DOUBLE:
  5371. return upb_msgval_double(upb_fielddef_defaultdouble(f));
  5372. case UPB_TYPE_BOOL:
  5373. return upb_msgval_bool(upb_fielddef_defaultbool(f));
  5374. case UPB_TYPE_STRING:
  5375. case UPB_TYPE_BYTES: {
  5376. size_t len;
  5377. const char *ptr = upb_fielddef_defaultstr(f, &len);
  5378. return upb_msgval_makestr(ptr, len);
  5379. }
  5380. case UPB_TYPE_MESSAGE:
  5381. return upb_msgval_msg(NULL);
  5382. case UPB_TYPE_ENUM:
  5383. case UPB_TYPE_INT32:
  5384. return upb_msgval_int32(upb_fielddef_defaultint32(f));
  5385. case UPB_TYPE_UINT32:
  5386. return upb_msgval_uint32(upb_fielddef_defaultuint32(f));
  5387. case UPB_TYPE_INT64:
  5388. return upb_msgval_int64(upb_fielddef_defaultint64(f));
  5389. case UPB_TYPE_UINT64:
  5390. return upb_msgval_uint64(upb_fielddef_defaultuint64(f));
  5391. default:
  5392. UPB_ASSERT(false);
  5393. return upb_msgval_msg(NULL);
  5394. }
  5395. }
  5396. /** upb_msglayout *************************************************************/
  5397. struct upb_msglayout {
  5398. struct upb_msglayout_msginit_v1 data;
  5399. };
  5400. static void upb_msglayout_free(upb_msglayout *l) {
  5401. upb_gfree(l->data.default_msg);
  5402. upb_gfree(l);
  5403. }
  5404. static size_t upb_msglayout_place(upb_msglayout *l, size_t size) {
  5405. size_t ret;
  5406. l->data.size = align_up(l->data.size, size);
  5407. ret = l->data.size;
  5408. l->data.size += size;
  5409. return ret;
  5410. }
  5411. static uint32_t upb_msglayout_offset(const upb_msglayout *l,
  5412. const upb_fielddef *f) {
  5413. return l->data.fields[upb_fielddef_index(f)].offset;
  5414. }
  5415. static uint32_t upb_msglayout_hasbit(const upb_msglayout *l,
  5416. const upb_fielddef *f) {
  5417. return l->data.fields[upb_fielddef_index(f)].hasbit;
  5418. }
  5419. static bool upb_msglayout_initdefault(upb_msglayout *l, const upb_msgdef *m) {
  5420. upb_msg_field_iter it;
  5421. if (upb_msgdef_syntax(m) == UPB_SYNTAX_PROTO2 && l->data.size) {
  5422. /* Allocate default message and set default values in it. */
  5423. l->data.default_msg = upb_gmalloc(l->data.size);
  5424. if (!l->data.default_msg) {
  5425. return false;
  5426. }
  5427. memset(l->data.default_msg, 0, l->data.size);
  5428. for (upb_msg_field_begin(&it, m); !upb_msg_field_done(&it);
  5429. upb_msg_field_next(&it)) {
  5430. const upb_fielddef* f = upb_msg_iter_field(&it);
  5431. if (upb_fielddef_containingoneof(f)) {
  5432. continue;
  5433. }
  5434. /* TODO(haberman): handle strings. */
  5435. if (!upb_fielddef_isstring(f) &&
  5436. !upb_fielddef_issubmsg(f) &&
  5437. !upb_fielddef_isseq(f)) {
  5438. upb_msg_set(l->data.default_msg,
  5439. upb_fielddef_index(f),
  5440. upb_msgval_fromdefault(f),
  5441. l);
  5442. }
  5443. }
  5444. }
  5445. return true;
  5446. }
  5447. static bool upb_msglayout_init(const upb_msgdef *m,
  5448. upb_msglayout *l,
  5449. upb_msgfactory *factory) {
  5450. upb_msg_field_iter it;
  5451. upb_msg_oneof_iter oit;
  5452. size_t hasbit;
  5453. size_t submsg_count = 0;
  5454. const upb_msglayout_msginit_v1 **submsgs;
  5455. upb_msglayout_fieldinit_v1 *fields;
  5456. upb_msglayout_oneofinit_v1 *oneofs;
  5457. for (upb_msg_field_begin(&it, m);
  5458. !upb_msg_field_done(&it);
  5459. upb_msg_field_next(&it)) {
  5460. const upb_fielddef* f = upb_msg_iter_field(&it);
  5461. if (upb_fielddef_issubmsg(f)) {
  5462. submsg_count++;
  5463. }
  5464. }
  5465. memset(l, 0, sizeof(*l));
  5466. fields = upb_gmalloc(upb_msgdef_numfields(m) * sizeof(*fields));
  5467. submsgs = upb_gmalloc(submsg_count * sizeof(*submsgs));
  5468. oneofs = upb_gmalloc(upb_msgdef_numoneofs(m) * sizeof(*oneofs));
  5469. if ((!fields && upb_msgdef_numfields(m)) ||
  5470. (!submsgs && submsg_count) ||
  5471. (!oneofs && upb_msgdef_numoneofs(m))) {
  5472. /* OOM. */
  5473. upb_gfree(fields);
  5474. upb_gfree(submsgs);
  5475. upb_gfree(oneofs);
  5476. return false;
  5477. }
  5478. l->data.field_count = upb_msgdef_numfields(m);
  5479. l->data.oneof_count = upb_msgdef_numoneofs(m);
  5480. l->data.fields = fields;
  5481. l->data.submsgs = submsgs;
  5482. l->data.oneofs = oneofs;
  5483. l->data.is_proto2 = (upb_msgdef_syntax(m) == UPB_SYNTAX_PROTO2);
  5484. /* Allocate data offsets in three stages:
  5485. *
  5486. * 1. hasbits.
  5487. * 2. regular fields.
  5488. * 3. oneof fields.
  5489. *
  5490. * OPT: There is a lot of room for optimization here to minimize the size.
  5491. */
  5492. /* Allocate hasbits and set basic field attributes. */
  5493. submsg_count = 0;
  5494. for (upb_msg_field_begin(&it, m), hasbit = 0;
  5495. !upb_msg_field_done(&it);
  5496. upb_msg_field_next(&it)) {
  5497. const upb_fielddef* f = upb_msg_iter_field(&it);
  5498. upb_msglayout_fieldinit_v1 *field = &fields[upb_fielddef_index(f)];
  5499. field->number = upb_fielddef_number(f);
  5500. field->descriptortype = upb_fielddef_descriptortype(f);
  5501. field->label = upb_fielddef_label(f);
  5502. if (upb_fielddef_containingoneof(f)) {
  5503. field->oneof_index = upb_oneofdef_index(upb_fielddef_containingoneof(f));
  5504. } else {
  5505. field->oneof_index = UPB_NOT_IN_ONEOF;
  5506. }
  5507. if (upb_fielddef_issubmsg(f)) {
  5508. const upb_msglayout *sub_layout =
  5509. upb_msgfactory_getlayout(factory, upb_fielddef_msgsubdef(f));
  5510. field->submsg_index = submsg_count++;
  5511. submsgs[field->submsg_index] = &sub_layout->data;
  5512. } else {
  5513. field->submsg_index = UPB_NO_SUBMSG;
  5514. }
  5515. if (upb_fielddef_haspresence(f) && !upb_fielddef_containingoneof(f)) {
  5516. field->hasbit = hasbit++;
  5517. } else {
  5518. field->hasbit = UPB_NO_HASBIT;
  5519. }
  5520. }
  5521. /* Account for space used by hasbits. */
  5522. l->data.size = div_round_up(hasbit, 8);
  5523. /* Allocate non-oneof fields. */
  5524. for (upb_msg_field_begin(&it, m); !upb_msg_field_done(&it);
  5525. upb_msg_field_next(&it)) {
  5526. const upb_fielddef* f = upb_msg_iter_field(&it);
  5527. size_t field_size = upb_msg_fielddefsize(f);
  5528. size_t index = upb_fielddef_index(f);
  5529. if (upb_fielddef_containingoneof(f)) {
  5530. /* Oneofs are handled separately below. */
  5531. continue;
  5532. }
  5533. fields[index].offset = upb_msglayout_place(l, field_size);
  5534. }
  5535. /* Allocate oneof fields. Each oneof field consists of a uint32 for the case
  5536. * and space for the actual data. */
  5537. for (upb_msg_oneof_begin(&oit, m); !upb_msg_oneof_done(&oit);
  5538. upb_msg_oneof_next(&oit)) {
  5539. const upb_oneofdef* o = upb_msg_iter_oneof(&oit);
  5540. upb_oneof_iter fit;
  5541. size_t case_size = sizeof(uint32_t); /* Could potentially optimize this. */
  5542. upb_msglayout_oneofinit_v1 *oneof = &oneofs[upb_oneofdef_index(o)];
  5543. size_t field_size = 0;
  5544. /* Calculate field size: the max of all field sizes. */
  5545. for (upb_oneof_begin(&fit, o);
  5546. !upb_oneof_done(&fit);
  5547. upb_oneof_next(&fit)) {
  5548. const upb_fielddef* f = upb_oneof_iter_field(&fit);
  5549. field_size = UPB_MAX(field_size, upb_msg_fielddefsize(f));
  5550. }
  5551. /* Align and allocate case offset. */
  5552. oneof->case_offset = upb_msglayout_place(l, case_size);
  5553. oneof->data_offset = upb_msglayout_place(l, field_size);
  5554. }
  5555. /* Size of the entire structure should be a multiple of its greatest
  5556. * alignment. TODO: track overall alignment for real? */
  5557. l->data.size = align_up(l->data.size, 8);
  5558. return upb_msglayout_initdefault(l, m);
  5559. }
  5560. /** upb_msgfactory ************************************************************/
  5561. struct upb_msgfactory {
  5562. const upb_symtab *symtab; /* We own a ref. */
  5563. upb_inttable layouts;
  5564. upb_inttable mergehandlers;
  5565. };
  5566. upb_msgfactory *upb_msgfactory_new(const upb_symtab *symtab) {
  5567. upb_msgfactory *ret = upb_gmalloc(sizeof(*ret));
  5568. ret->symtab = symtab;
  5569. upb_inttable_init(&ret->layouts, UPB_CTYPE_PTR);
  5570. upb_inttable_init(&ret->mergehandlers, UPB_CTYPE_CONSTPTR);
  5571. return ret;
  5572. }
  5573. void upb_msgfactory_free(upb_msgfactory *f) {
  5574. upb_inttable_iter i;
  5575. upb_inttable_begin(&i, &f->layouts);
  5576. for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  5577. upb_msglayout *l = upb_value_getptr(upb_inttable_iter_value(&i));
  5578. upb_msglayout_free(l);
  5579. }
  5580. upb_inttable_begin(&i, &f->mergehandlers);
  5581. for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  5582. const upb_handlers *h = upb_value_getconstptr(upb_inttable_iter_value(&i));
  5583. upb_handlers_unref(h, f);
  5584. }
  5585. upb_inttable_uninit(&f->layouts);
  5586. upb_inttable_uninit(&f->mergehandlers);
  5587. upb_gfree(f);
  5588. }
  5589. const upb_symtab *upb_msgfactory_symtab(const upb_msgfactory *f) {
  5590. return f->symtab;
  5591. }
  5592. const upb_msglayout *upb_msgfactory_getlayout(upb_msgfactory *f,
  5593. const upb_msgdef *m) {
  5594. upb_value v;
  5595. UPB_ASSERT(upb_symtab_lookupmsg(f->symtab, upb_msgdef_fullname(m)) == m);
  5596. UPB_ASSERT(!upb_msgdef_mapentry(m));
  5597. if (upb_inttable_lookupptr(&f->layouts, m, &v)) {
  5598. UPB_ASSERT(upb_value_getptr(v));
  5599. return upb_value_getptr(v);
  5600. } else {
  5601. /* In case of circular dependency, layout has to be inserted first. */
  5602. upb_msglayout *l = upb_gmalloc(sizeof(*l));
  5603. upb_msgfactory *mutable_f = (void*)f;
  5604. upb_inttable_insertptr(&mutable_f->layouts, m, upb_value_ptr(l));
  5605. UPB_ASSERT(l);
  5606. if (!upb_msglayout_init(m, l, f)) {
  5607. upb_msglayout_free(l);
  5608. }
  5609. return l;
  5610. }
  5611. }
  5612. /* Our handlers that we don't expose externally. */
  5613. void *upb_msg_startstr(void *msg, const void *hd, size_t size_hint) {
  5614. uint32_t ofs = (uintptr_t)hd;
  5615. upb_alloc *alloc = upb_msg_alloc(msg);
  5616. upb_msgval val;
  5617. UPB_UNUSED(size_hint);
  5618. val = upb_msgval_read(msg, ofs, upb_msgval_sizeof(UPB_TYPE_STRING));
  5619. upb_free(alloc, (void*)val.str.data);
  5620. val.str.data = NULL;
  5621. val.str.size = 0;
  5622. upb_msgval_write(msg, ofs, val, upb_msgval_sizeof(UPB_TYPE_STRING));
  5623. return msg;
  5624. }
  5625. size_t upb_msg_str(void *msg, const void *hd, const char *ptr, size_t size,
  5626. const upb_bufhandle *handle) {
  5627. uint32_t ofs = (uintptr_t)hd;
  5628. upb_alloc *alloc = upb_msg_alloc(msg);
  5629. upb_msgval val;
  5630. size_t newsize;
  5631. UPB_UNUSED(handle);
  5632. val = upb_msgval_read(msg, ofs, upb_msgval_sizeof(UPB_TYPE_STRING));
  5633. newsize = val.str.size + size;
  5634. val.str.data = upb_realloc(alloc, (void*)val.str.data, val.str.size, newsize);
  5635. if (!val.str.data) {
  5636. return false;
  5637. }
  5638. memcpy((char*)val.str.data + val.str.size, ptr, size);
  5639. val.str.size = newsize;
  5640. upb_msgval_write(msg, ofs, val, upb_msgval_sizeof(UPB_TYPE_STRING));
  5641. return size;
  5642. }
  5643. static void callback(const void *closure, upb_handlers *h) {
  5644. upb_msgfactory *factory = (upb_msgfactory*)closure;
  5645. const upb_msgdef *md = upb_handlers_msgdef(h);
  5646. const upb_msglayout* layout = upb_msgfactory_getlayout(factory, md);
  5647. upb_msg_field_iter i;
  5648. UPB_UNUSED(factory);
  5649. for(upb_msg_field_begin(&i, md);
  5650. !upb_msg_field_done(&i);
  5651. upb_msg_field_next(&i)) {
  5652. const upb_fielddef *f = upb_msg_iter_field(&i);
  5653. size_t offset = upb_msglayout_offset(layout, f);
  5654. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  5655. upb_handlerattr_sethandlerdata(&attr, (void*)offset);
  5656. if (upb_fielddef_isseq(f)) {
  5657. } else if (upb_fielddef_isstring(f)) {
  5658. upb_handlers_setstartstr(h, f, upb_msg_startstr, &attr);
  5659. upb_handlers_setstring(h, f, upb_msg_str, &attr);
  5660. } else {
  5661. upb_msg_setscalarhandler(
  5662. h, f, offset, upb_msglayout_hasbit(layout, f));
  5663. }
  5664. }
  5665. }
  5666. const upb_handlers *upb_msgfactory_getmergehandlers(upb_msgfactory *f,
  5667. const upb_msgdef *m) {
  5668. upb_msgfactory *mutable_f = (void*)f;
  5669. /* TODO(haberman): properly cache these. */
  5670. const upb_handlers *ret = upb_handlers_newfrozen(m, f, callback, f);
  5671. upb_inttable_push(&mutable_f->mergehandlers, upb_value_constptr(ret));
  5672. return ret;
  5673. }
  5674. const upb_visitorplan *upb_msgfactory_getvisitorplan(upb_msgfactory *f,
  5675. const upb_handlers *h) {
  5676. const upb_msgdef *md = upb_handlers_msgdef(h);
  5677. return (const upb_visitorplan*)upb_msgfactory_getlayout(f, md);
  5678. }
  5679. /** upb_visitor ***************************************************************/
  5680. struct upb_visitor {
  5681. const upb_msglayout *layout;
  5682. upb_sink *sink;
  5683. };
  5684. static upb_selector_t getsel2(const upb_fielddef *f, upb_handlertype_t type) {
  5685. upb_selector_t ret;
  5686. bool ok = upb_handlers_getselector(f, type, &ret);
  5687. UPB_ASSERT(ok);
  5688. return ret;
  5689. }
  5690. static bool upb_visitor_hasfield(const upb_msg *msg, const upb_fielddef *f,
  5691. const upb_msglayout *layout) {
  5692. int field_index = upb_fielddef_index(f);
  5693. if (upb_fielddef_isseq(f)) {
  5694. return upb_msgval_getarr(upb_msg_get(msg, field_index, layout)) != NULL;
  5695. } else if (upb_msgdef_syntax(upb_fielddef_containingtype(f)) ==
  5696. UPB_SYNTAX_PROTO2) {
  5697. return upb_msg_has(msg, field_index, layout);
  5698. } else {
  5699. upb_msgval val = upb_msg_get(msg, field_index, layout);
  5700. switch (upb_fielddef_type(f)) {
  5701. case UPB_TYPE_FLOAT:
  5702. return upb_msgval_getfloat(val) != 0;
  5703. case UPB_TYPE_DOUBLE:
  5704. return upb_msgval_getdouble(val) != 0;
  5705. case UPB_TYPE_BOOL:
  5706. return upb_msgval_getbool(val);
  5707. case UPB_TYPE_ENUM:
  5708. case UPB_TYPE_INT32:
  5709. return upb_msgval_getint32(val) != 0;
  5710. case UPB_TYPE_UINT32:
  5711. return upb_msgval_getuint32(val) != 0;
  5712. case UPB_TYPE_INT64:
  5713. return upb_msgval_getint64(val) != 0;
  5714. case UPB_TYPE_UINT64:
  5715. return upb_msgval_getuint64(val) != 0;
  5716. case UPB_TYPE_STRING:
  5717. case UPB_TYPE_BYTES:
  5718. return upb_msgval_getstr(val).size > 0;
  5719. case UPB_TYPE_MESSAGE:
  5720. return upb_msgval_getmsg(val) != NULL;
  5721. }
  5722. UPB_UNREACHABLE();
  5723. }
  5724. }
  5725. static bool upb_visitor_visitmsg2(const upb_msg *msg,
  5726. const upb_msglayout *layout, upb_sink *sink,
  5727. int depth) {
  5728. const upb_msgdef *md = upb_handlers_msgdef(sink->handlers);
  5729. upb_msg_field_iter i;
  5730. upb_status status;
  5731. upb_sink_startmsg(sink);
  5732. /* Protect against cycles (possible because users may freely reassign message
  5733. * and repeated fields) by imposing a maximum recursion depth. */
  5734. if (depth > ENCODE_MAX_NESTING) {
  5735. return false;
  5736. }
  5737. for (upb_msg_field_begin(&i, md);
  5738. !upb_msg_field_done(&i);
  5739. upb_msg_field_next(&i)) {
  5740. upb_fielddef *f = upb_msg_iter_field(&i);
  5741. upb_msgval val;
  5742. if (!upb_visitor_hasfield(msg, f, layout)) {
  5743. continue;
  5744. }
  5745. val = upb_msg_get(msg, upb_fielddef_index(f), layout);
  5746. if (upb_fielddef_isseq(f)) {
  5747. const upb_array *arr = upb_msgval_getarr(val);
  5748. UPB_ASSERT(arr);
  5749. /* TODO: putary(ary, f, sink, depth);*/
  5750. } else if (upb_fielddef_issubmsg(f)) {
  5751. const upb_map *map = upb_msgval_getmap(val);
  5752. UPB_ASSERT(map);
  5753. /* TODO: putmap(map, f, sink, depth);*/
  5754. } else if (upb_fielddef_isstring(f)) {
  5755. /* TODO putstr(); */
  5756. } else {
  5757. upb_selector_t sel = getsel2(f, upb_handlers_getprimitivehandlertype(f));
  5758. UPB_ASSERT(upb_fielddef_isprimitive(f));
  5759. switch (upb_fielddef_type(f)) {
  5760. case UPB_TYPE_FLOAT:
  5761. CHECK_TRUE(upb_sink_putfloat(sink, sel, upb_msgval_getfloat(val)));
  5762. break;
  5763. case UPB_TYPE_DOUBLE:
  5764. CHECK_TRUE(upb_sink_putdouble(sink, sel, upb_msgval_getdouble(val)));
  5765. break;
  5766. case UPB_TYPE_BOOL:
  5767. CHECK_TRUE(upb_sink_putbool(sink, sel, upb_msgval_getbool(val)));
  5768. break;
  5769. case UPB_TYPE_ENUM:
  5770. case UPB_TYPE_INT32:
  5771. CHECK_TRUE(upb_sink_putint32(sink, sel, upb_msgval_getint32(val)));
  5772. break;
  5773. case UPB_TYPE_UINT32:
  5774. CHECK_TRUE(upb_sink_putuint32(sink, sel, upb_msgval_getuint32(val)));
  5775. break;
  5776. case UPB_TYPE_INT64:
  5777. CHECK_TRUE(upb_sink_putint64(sink, sel, upb_msgval_getint64(val)));
  5778. break;
  5779. case UPB_TYPE_UINT64:
  5780. CHECK_TRUE(upb_sink_putuint64(sink, sel, upb_msgval_getuint64(val)));
  5781. break;
  5782. case UPB_TYPE_STRING:
  5783. case UPB_TYPE_BYTES:
  5784. case UPB_TYPE_MESSAGE:
  5785. UPB_UNREACHABLE();
  5786. }
  5787. }
  5788. }
  5789. upb_sink_endmsg(sink, &status);
  5790. return true;
  5791. }
  5792. upb_visitor *upb_visitor_create(upb_env *e, const upb_visitorplan *vp,
  5793. upb_sink *output) {
  5794. upb_visitor *visitor = upb_env_malloc(e, sizeof(*visitor));
  5795. visitor->layout = (const upb_msglayout*)vp;
  5796. visitor->sink = output;
  5797. return visitor;
  5798. }
  5799. bool upb_visitor_visitmsg(upb_visitor *visitor, const upb_msg *msg) {
  5800. return upb_visitor_visitmsg2(msg, visitor->layout, visitor->sink, 0);
  5801. }
  5802. /** upb_msg *******************************************************************/
  5803. /* If we always read/write as a consistent type to each address, this shouldn't
  5804. * violate aliasing.
  5805. */
  5806. #define DEREF(msg, ofs, type) *PTR_AT(msg, ofs, type)
  5807. /* Internal members of a upb_msg. We can change this without breaking binary
  5808. * compatibility. We put these before the user's data. The user's upb_msg*
  5809. * points after the upb_msg_internal. */
  5810. /* Used when a message is not extendable. */
  5811. typedef struct {
  5812. /* TODO(haberman): add unknown fields. */
  5813. upb_alloc *alloc;
  5814. } upb_msg_internal;
  5815. /* Used when a message is extendable. */
  5816. typedef struct {
  5817. upb_inttable *extdict;
  5818. upb_msg_internal base;
  5819. } upb_msg_internal_withext;
  5820. static int upb_msg_internalsize(const upb_msglayout *l) {
  5821. return sizeof(upb_msg_internal) - l->data.extendable * sizeof(void*);
  5822. }
  5823. static upb_msg_internal *upb_msg_getinternal(upb_msg *msg) {
  5824. return VOIDPTR_AT(msg, -sizeof(upb_msg_internal));
  5825. }
  5826. static const upb_msg_internal *upb_msg_getinternal_const(const upb_msg *msg) {
  5827. return VOIDPTR_AT(msg, -sizeof(upb_msg_internal));
  5828. }
  5829. static upb_msg_internal_withext *upb_msg_getinternalwithext(
  5830. upb_msg *msg, const upb_msglayout *l) {
  5831. UPB_ASSERT(l->data.extendable);
  5832. return VOIDPTR_AT(msg, -sizeof(upb_msg_internal_withext));
  5833. }
  5834. static const upb_msglayout_fieldinit_v1 *upb_msg_checkfield(
  5835. int field_index, const upb_msglayout *l) {
  5836. UPB_ASSERT(field_index >= 0 && field_index < l->data.field_count);
  5837. return &l->data.fields[field_index];
  5838. }
  5839. static bool upb_msg_inoneof(const upb_msglayout_fieldinit_v1 *field) {
  5840. return field->oneof_index != UPB_NOT_IN_ONEOF;
  5841. }
  5842. static uint32_t *upb_msg_oneofcase(const upb_msg *msg, int field_index,
  5843. const upb_msglayout *l) {
  5844. const upb_msglayout_fieldinit_v1 *field = upb_msg_checkfield(field_index, l);
  5845. UPB_ASSERT(upb_msg_inoneof(field));
  5846. return PTR_AT(msg, l->data.oneofs[field->oneof_index].case_offset, uint32_t);
  5847. }
  5848. size_t upb_msg_sizeof(const upb_msglayout *l) {
  5849. return l->data.size + upb_msg_internalsize(l);
  5850. }
  5851. upb_msg *upb_msg_init(void *mem, const upb_msglayout *l, upb_alloc *a) {
  5852. upb_msg *msg = VOIDPTR_AT(mem, upb_msg_internalsize(l));
  5853. /* Initialize normal members. */
  5854. if (l->data.default_msg) {
  5855. memcpy(msg, l->data.default_msg, l->data.size);
  5856. } else {
  5857. memset(msg, 0, l->data.size);
  5858. }
  5859. /* Initialize internal members. */
  5860. upb_msg_getinternal(msg)->alloc = a;
  5861. if (l->data.extendable) {
  5862. upb_msg_getinternalwithext(msg, l)->extdict = NULL;
  5863. }
  5864. return msg;
  5865. }
  5866. void *upb_msg_uninit(upb_msg *msg, const upb_msglayout *l) {
  5867. if (l->data.extendable) {
  5868. upb_inttable *ext_dict = upb_msg_getinternalwithext(msg, l)->extdict;
  5869. if (ext_dict) {
  5870. upb_inttable_uninit2(ext_dict, upb_msg_alloc(msg));
  5871. upb_free(upb_msg_alloc(msg), ext_dict);
  5872. }
  5873. }
  5874. return VOIDPTR_AT(msg, -upb_msg_internalsize(l));
  5875. }
  5876. upb_msg *upb_msg_new(const upb_msglayout *l, upb_alloc *a) {
  5877. void *mem = upb_malloc(a, upb_msg_sizeof(l));
  5878. return mem ? upb_msg_init(mem, l, a) : NULL;
  5879. }
  5880. void upb_msg_free(upb_msg *msg, const upb_msglayout *l) {
  5881. upb_free(upb_msg_alloc(msg), upb_msg_uninit(msg, l));
  5882. }
  5883. upb_alloc *upb_msg_alloc(const upb_msg *msg) {
  5884. return upb_msg_getinternal_const(msg)->alloc;
  5885. }
  5886. bool upb_msg_has(const upb_msg *msg,
  5887. int field_index,
  5888. const upb_msglayout *l) {
  5889. const upb_msglayout_fieldinit_v1 *field = upb_msg_checkfield(field_index, l);
  5890. UPB_ASSERT(l->data.is_proto2);
  5891. if (upb_msg_inoneof(field)) {
  5892. /* Oneofs are set when the oneof number is set to this field. */
  5893. return *upb_msg_oneofcase(msg, field_index, l) == field->number;
  5894. } else {
  5895. /* Other fields are set when their hasbit is set. */
  5896. uint32_t hasbit = l->data.fields[field_index].hasbit;
  5897. return DEREF(msg, hasbit / 8, char) | (1 << (hasbit % 8));
  5898. }
  5899. }
  5900. upb_msgval upb_msg_get(const upb_msg *msg, int field_index,
  5901. const upb_msglayout *l) {
  5902. const upb_msglayout_fieldinit_v1 *field = upb_msg_checkfield(field_index, l);
  5903. int size = upb_msg_fieldsize(field);
  5904. if (upb_msg_inoneof(field)) {
  5905. if (*upb_msg_oneofcase(msg, field_index, l) == field->number) {
  5906. size_t ofs = l->data.oneofs[field->oneof_index].data_offset;
  5907. return upb_msgval_read(msg, ofs, size);
  5908. } else {
  5909. /* Return default. */
  5910. return upb_msgval_read(l->data.default_msg, field->offset, size);
  5911. }
  5912. } else {
  5913. return upb_msgval_read(msg, field->offset, size);
  5914. }
  5915. }
  5916. void upb_msg_set(upb_msg *msg, int field_index, upb_msgval val,
  5917. const upb_msglayout *l) {
  5918. const upb_msglayout_fieldinit_v1 *field = upb_msg_checkfield(field_index, l);
  5919. int size = upb_msg_fieldsize(field);
  5920. if (upb_msg_inoneof(field)) {
  5921. size_t ofs = l->data.oneofs[field->oneof_index].data_offset;
  5922. *upb_msg_oneofcase(msg, field_index, l) = field->number;
  5923. upb_msgval_write(msg, ofs, val, size);
  5924. } else {
  5925. upb_msgval_write(msg, field->offset, val, size);
  5926. }
  5927. }
  5928. /** upb_array *****************************************************************/
  5929. #define DEREF_ARR(arr, i, type) ((type*)arr->data)[i]
  5930. size_t upb_array_sizeof(upb_fieldtype_t type) {
  5931. UPB_UNUSED(type);
  5932. return sizeof(upb_array);
  5933. }
  5934. void upb_array_init(upb_array *arr, upb_fieldtype_t type, upb_alloc *alloc) {
  5935. arr->type = type;
  5936. arr->data = NULL;
  5937. arr->len = 0;
  5938. arr->size = 0;
  5939. arr->element_size = upb_msgval_sizeof(type);
  5940. arr->alloc = alloc;
  5941. }
  5942. void upb_array_uninit(upb_array *arr) {
  5943. upb_free(arr->alloc, arr->data);
  5944. }
  5945. upb_array *upb_array_new(upb_fieldtype_t type, upb_alloc *a) {
  5946. upb_array *ret = upb_malloc(a, upb_array_sizeof(type));
  5947. if (ret) {
  5948. upb_array_init(ret, type, a);
  5949. }
  5950. return ret;
  5951. }
  5952. void upb_array_free(upb_array *arr) {
  5953. upb_array_uninit(arr);
  5954. upb_free(arr->alloc, arr);
  5955. }
  5956. size_t upb_array_size(const upb_array *arr) {
  5957. return arr->len;
  5958. }
  5959. upb_fieldtype_t upb_array_type(const upb_array *arr) {
  5960. return arr->type;
  5961. }
  5962. upb_msgval upb_array_get(const upb_array *arr, size_t i) {
  5963. UPB_ASSERT(i < arr->len);
  5964. return upb_msgval_read(arr->data, i * arr->element_size, arr->element_size);
  5965. }
  5966. bool upb_array_set(upb_array *arr, size_t i, upb_msgval val) {
  5967. UPB_ASSERT(i <= arr->len);
  5968. if (i == arr->len) {
  5969. /* Extending the array. */
  5970. if (i == arr->size) {
  5971. /* Need to reallocate. */
  5972. size_t new_size = UPB_MAX(arr->size * 2, 8);
  5973. size_t new_bytes = new_size * arr->element_size;
  5974. size_t old_bytes = arr->size * arr->element_size;
  5975. upb_msgval *new_data =
  5976. upb_realloc(arr->alloc, arr->data, old_bytes, new_bytes);
  5977. if (!new_data) {
  5978. return false;
  5979. }
  5980. arr->data = new_data;
  5981. arr->size = new_size;
  5982. }
  5983. arr->len = i + 1;
  5984. }
  5985. upb_msgval_write(arr->data, i * arr->element_size, val, arr->element_size);
  5986. return true;
  5987. }
  5988. /** upb_map *******************************************************************/
  5989. struct upb_map {
  5990. upb_fieldtype_t key_type;
  5991. upb_fieldtype_t val_type;
  5992. /* We may want to optimize this to use inttable where possible, for greater
  5993. * efficiency and lower memory footprint. */
  5994. upb_strtable strtab;
  5995. upb_alloc *alloc;
  5996. };
  5997. static void upb_map_tokey(upb_fieldtype_t type, upb_msgval *key,
  5998. const char **out_key, size_t *out_len) {
  5999. switch (type) {
  6000. case UPB_TYPE_STRING:
  6001. /* Point to string data of the input key. */
  6002. *out_key = key->str.data;
  6003. *out_len = key->str.size;
  6004. return;
  6005. case UPB_TYPE_BOOL:
  6006. case UPB_TYPE_INT32:
  6007. case UPB_TYPE_UINT32:
  6008. case UPB_TYPE_INT64:
  6009. case UPB_TYPE_UINT64:
  6010. /* Point to the key itself. XXX: big-endian. */
  6011. *out_key = (const char*)key;
  6012. *out_len = upb_msgval_sizeof(type);
  6013. return;
  6014. case UPB_TYPE_BYTES:
  6015. case UPB_TYPE_DOUBLE:
  6016. case UPB_TYPE_ENUM:
  6017. case UPB_TYPE_FLOAT:
  6018. case UPB_TYPE_MESSAGE:
  6019. break; /* Cannot be a map key. */
  6020. }
  6021. UPB_UNREACHABLE();
  6022. }
  6023. static upb_msgval upb_map_fromkey(upb_fieldtype_t type, const char *key,
  6024. size_t len) {
  6025. switch (type) {
  6026. case UPB_TYPE_STRING:
  6027. return upb_msgval_makestr(key, len);
  6028. case UPB_TYPE_BOOL:
  6029. case UPB_TYPE_INT32:
  6030. case UPB_TYPE_UINT32:
  6031. case UPB_TYPE_INT64:
  6032. case UPB_TYPE_UINT64:
  6033. return upb_msgval_read(key, 0, upb_msgval_sizeof(type));
  6034. case UPB_TYPE_BYTES:
  6035. case UPB_TYPE_DOUBLE:
  6036. case UPB_TYPE_ENUM:
  6037. case UPB_TYPE_FLOAT:
  6038. case UPB_TYPE_MESSAGE:
  6039. break; /* Cannot be a map key. */
  6040. }
  6041. UPB_UNREACHABLE();
  6042. }
  6043. size_t upb_map_sizeof(upb_fieldtype_t ktype, upb_fieldtype_t vtype) {
  6044. /* Size does not currently depend on key/value type. */
  6045. UPB_UNUSED(ktype);
  6046. UPB_UNUSED(vtype);
  6047. return sizeof(upb_map);
  6048. }
  6049. bool upb_map_init(upb_map *map, upb_fieldtype_t ktype, upb_fieldtype_t vtype,
  6050. upb_alloc *a) {
  6051. upb_ctype_t vtabtype = upb_fieldtotabtype(vtype);
  6052. UPB_ASSERT(upb_fieldtype_mapkeyok(ktype));
  6053. map->key_type = ktype;
  6054. map->val_type = vtype;
  6055. map->alloc = a;
  6056. if (!upb_strtable_init2(&map->strtab, vtabtype, a)) {
  6057. return false;
  6058. }
  6059. return true;
  6060. }
  6061. void upb_map_uninit(upb_map *map) {
  6062. upb_strtable_uninit2(&map->strtab, map->alloc);
  6063. }
  6064. upb_map *upb_map_new(upb_fieldtype_t ktype, upb_fieldtype_t vtype,
  6065. upb_alloc *a) {
  6066. upb_map *map = upb_malloc(a, upb_map_sizeof(ktype, vtype));
  6067. if (!map) {
  6068. return NULL;
  6069. }
  6070. if (!upb_map_init(map, ktype, vtype, a)) {
  6071. return NULL;
  6072. }
  6073. return map;
  6074. }
  6075. void upb_map_free(upb_map *map) {
  6076. upb_map_uninit(map);
  6077. upb_free(map->alloc, map);
  6078. }
  6079. size_t upb_map_size(const upb_map *map) {
  6080. return upb_strtable_count(&map->strtab);
  6081. }
  6082. upb_fieldtype_t upb_map_keytype(const upb_map *map) {
  6083. return map->key_type;
  6084. }
  6085. upb_fieldtype_t upb_map_valuetype(const upb_map *map) {
  6086. return map->val_type;
  6087. }
  6088. bool upb_map_get(const upb_map *map, upb_msgval key, upb_msgval *val) {
  6089. upb_value tabval;
  6090. const char *key_str;
  6091. size_t key_len;
  6092. bool ret;
  6093. upb_map_tokey(map->key_type, &key, &key_str, &key_len);
  6094. ret = upb_strtable_lookup2(&map->strtab, key_str, key_len, &tabval);
  6095. if (ret) {
  6096. memcpy(val, &tabval, sizeof(tabval));
  6097. }
  6098. return ret;
  6099. }
  6100. bool upb_map_set(upb_map *map, upb_msgval key, upb_msgval val,
  6101. upb_msgval *removed) {
  6102. const char *key_str;
  6103. size_t key_len;
  6104. upb_value tabval = upb_toval(val);
  6105. upb_value removedtabval;
  6106. upb_alloc *a = map->alloc;
  6107. upb_map_tokey(map->key_type, &key, &key_str, &key_len);
  6108. /* TODO(haberman): add overwrite operation to minimize number of lookups. */
  6109. if (upb_strtable_lookup2(&map->strtab, key_str, key_len, NULL)) {
  6110. upb_strtable_remove3(&map->strtab, key_str, key_len, &removedtabval, a);
  6111. memcpy(&removed, &removedtabval, sizeof(removed));
  6112. }
  6113. return upb_strtable_insert3(&map->strtab, key_str, key_len, tabval, a);
  6114. }
  6115. bool upb_map_del(upb_map *map, upb_msgval key) {
  6116. const char *key_str;
  6117. size_t key_len;
  6118. upb_alloc *a = map->alloc;
  6119. upb_map_tokey(map->key_type, &key, &key_str, &key_len);
  6120. return upb_strtable_remove3(&map->strtab, key_str, key_len, NULL, a);
  6121. }
  6122. /** upb_mapiter ***************************************************************/
  6123. struct upb_mapiter {
  6124. upb_strtable_iter iter;
  6125. upb_fieldtype_t key_type;
  6126. };
  6127. size_t upb_mapiter_sizeof() {
  6128. return sizeof(upb_mapiter);
  6129. }
  6130. void upb_mapiter_begin(upb_mapiter *i, const upb_map *map) {
  6131. upb_strtable_begin(&i->iter, &map->strtab);
  6132. i->key_type = map->key_type;
  6133. }
  6134. upb_mapiter *upb_mapiter_new(const upb_map *t, upb_alloc *a) {
  6135. upb_mapiter *ret = upb_malloc(a, upb_mapiter_sizeof());
  6136. if (!ret) {
  6137. return NULL;
  6138. }
  6139. upb_mapiter_begin(ret, t);
  6140. return ret;
  6141. }
  6142. void upb_mapiter_free(upb_mapiter *i, upb_alloc *a) {
  6143. upb_free(a, i);
  6144. }
  6145. void upb_mapiter_next(upb_mapiter *i) {
  6146. upb_strtable_next(&i->iter);
  6147. }
  6148. bool upb_mapiter_done(const upb_mapiter *i) {
  6149. return upb_strtable_done(&i->iter);
  6150. }
  6151. upb_msgval upb_mapiter_key(const upb_mapiter *i) {
  6152. return upb_map_fromkey(i->key_type, upb_strtable_iter_key(&i->iter),
  6153. upb_strtable_iter_keylength(&i->iter));
  6154. }
  6155. upb_msgval upb_mapiter_value(const upb_mapiter *i) {
  6156. return upb_msgval_fromval(upb_strtable_iter_value(&i->iter));
  6157. }
  6158. void upb_mapiter_setdone(upb_mapiter *i) {
  6159. upb_strtable_iter_setdone(&i->iter);
  6160. }
  6161. bool upb_mapiter_isequal(const upb_mapiter *i1, const upb_mapiter *i2) {
  6162. return upb_strtable_iter_isequal(&i1->iter, &i2->iter);
  6163. }
  6164. /** Handlers for upb_msg ******************************************************/
  6165. typedef struct {
  6166. size_t offset;
  6167. int32_t hasbit;
  6168. } upb_msg_handlerdata;
  6169. /* Fallback implementation if the handler is not specialized by the producer. */
  6170. #define MSG_WRITER(type, ctype) \
  6171. bool upb_msg_set ## type (void *c, const void *hd, ctype val) { \
  6172. uint8_t *m = c; \
  6173. const upb_msg_handlerdata *d = hd; \
  6174. if (d->hasbit > 0) \
  6175. *(uint8_t*)&m[d->hasbit / 8] |= 1 << (d->hasbit % 8); \
  6176. *(ctype*)&m[d->offset] = val; \
  6177. return true; \
  6178. } \
  6179. MSG_WRITER(double, double)
  6180. MSG_WRITER(float, float)
  6181. MSG_WRITER(int32, int32_t)
  6182. MSG_WRITER(int64, int64_t)
  6183. MSG_WRITER(uint32, uint32_t)
  6184. MSG_WRITER(uint64, uint64_t)
  6185. MSG_WRITER(bool, bool)
  6186. bool upb_msg_setscalarhandler(upb_handlers *h, const upb_fielddef *f,
  6187. size_t offset, int32_t hasbit) {
  6188. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  6189. bool ok;
  6190. upb_msg_handlerdata *d = upb_gmalloc(sizeof(*d));
  6191. if (!d) return false;
  6192. d->offset = offset;
  6193. d->hasbit = hasbit;
  6194. upb_handlerattr_sethandlerdata(&attr, d);
  6195. upb_handlerattr_setalwaysok(&attr, true);
  6196. upb_handlers_addcleanup(h, d, upb_gfree);
  6197. #define TYPE(u, l) \
  6198. case UPB_TYPE_##u: \
  6199. ok = upb_handlers_set##l(h, f, upb_msg_set##l, &attr); break;
  6200. ok = false;
  6201. switch (upb_fielddef_type(f)) {
  6202. TYPE(INT64, int64);
  6203. TYPE(INT32, int32);
  6204. TYPE(ENUM, int32);
  6205. TYPE(UINT64, uint64);
  6206. TYPE(UINT32, uint32);
  6207. TYPE(DOUBLE, double);
  6208. TYPE(FLOAT, float);
  6209. TYPE(BOOL, bool);
  6210. default: UPB_ASSERT(false); break;
  6211. }
  6212. #undef TYPE
  6213. upb_handlerattr_uninit(&attr);
  6214. return ok;
  6215. }
  6216. bool upb_msg_getscalarhandlerdata(const upb_handlers *h,
  6217. upb_selector_t s,
  6218. upb_fieldtype_t *type,
  6219. size_t *offset,
  6220. int32_t *hasbit) {
  6221. const upb_msg_handlerdata *d;
  6222. upb_func *f = upb_handlers_gethandler(h, s);
  6223. if ((upb_int64_handlerfunc*)f == upb_msg_setint64) {
  6224. *type = UPB_TYPE_INT64;
  6225. } else if ((upb_int32_handlerfunc*)f == upb_msg_setint32) {
  6226. *type = UPB_TYPE_INT32;
  6227. } else if ((upb_uint64_handlerfunc*)f == upb_msg_setuint64) {
  6228. *type = UPB_TYPE_UINT64;
  6229. } else if ((upb_uint32_handlerfunc*)f == upb_msg_setuint32) {
  6230. *type = UPB_TYPE_UINT32;
  6231. } else if ((upb_double_handlerfunc*)f == upb_msg_setdouble) {
  6232. *type = UPB_TYPE_DOUBLE;
  6233. } else if ((upb_float_handlerfunc*)f == upb_msg_setfloat) {
  6234. *type = UPB_TYPE_FLOAT;
  6235. } else if ((upb_bool_handlerfunc*)f == upb_msg_setbool) {
  6236. *type = UPB_TYPE_BOOL;
  6237. } else {
  6238. return false;
  6239. }
  6240. d = upb_handlers_gethandlerdata(h, s);
  6241. *offset = d->offset;
  6242. *hasbit = d->hasbit;
  6243. return true;
  6244. }
  6245. /*
  6246. ** upb::RefCounted Implementation
  6247. **
  6248. ** Our key invariants are:
  6249. ** 1. reference cycles never span groups
  6250. ** 2. for ref2(to, from), we increment to's count iff group(from) != group(to)
  6251. **
  6252. ** The previous two are how we avoid leaking cycles. Other important
  6253. ** invariants are:
  6254. ** 3. for mutable objects "from" and "to", if there exists a ref2(to, from)
  6255. ** this implies group(from) == group(to). (In practice, what we implement
  6256. ** is even stronger; "from" and "to" will share a group if there has *ever*
  6257. ** been a ref2(to, from), but all that is necessary for correctness is the
  6258. ** weaker one).
  6259. ** 4. mutable and immutable objects are never in the same group.
  6260. */
  6261. #include <setjmp.h>
  6262. static void freeobj(upb_refcounted *o);
  6263. const char untracked_val;
  6264. const void *UPB_UNTRACKED_REF = &untracked_val;
  6265. /* arch-specific atomic primitives *******************************************/
  6266. #ifdef UPB_THREAD_UNSAFE /*---------------------------------------------------*/
  6267. static void atomic_inc(uint32_t *a) { (*a)++; }
  6268. static bool atomic_dec(uint32_t *a) { return --(*a) == 0; }
  6269. #elif defined(__GNUC__) || defined(__clang__) /*------------------------------*/
  6270. static void atomic_inc(uint32_t *a) { __sync_fetch_and_add(a, 1); }
  6271. static bool atomic_dec(uint32_t *a) { return __sync_sub_and_fetch(a, 1) == 0; }
  6272. #elif defined(WIN32) /*-------------------------------------------------------*/
  6273. #include <Windows.h>
  6274. static void atomic_inc(upb_atomic_t *a) { InterlockedIncrement(&a->val); }
  6275. static bool atomic_dec(upb_atomic_t *a) {
  6276. return InterlockedDecrement(&a->val) == 0;
  6277. }
  6278. #else
  6279. #error Atomic primitives not defined for your platform/CPU. \
  6280. Implement them or compile with UPB_THREAD_UNSAFE.
  6281. #endif
  6282. /* All static objects point to this refcount.
  6283. * It is special-cased in ref/unref below. */
  6284. uint32_t static_refcount = -1;
  6285. /* We can avoid atomic ops for statically-declared objects.
  6286. * This is a minor optimization but nice since we can avoid degrading under
  6287. * contention in this case. */
  6288. static void refgroup(uint32_t *group) {
  6289. if (group != &static_refcount)
  6290. atomic_inc(group);
  6291. }
  6292. static bool unrefgroup(uint32_t *group) {
  6293. if (group == &static_refcount) {
  6294. return false;
  6295. } else {
  6296. return atomic_dec(group);
  6297. }
  6298. }
  6299. /* Reference tracking (debug only) ********************************************/
  6300. #ifdef UPB_DEBUG_REFS
  6301. #ifdef UPB_THREAD_UNSAFE
  6302. static void upb_lock() {}
  6303. static void upb_unlock() {}
  6304. #else
  6305. /* User must define functions that lock/unlock a global mutex and link this
  6306. * file against them. */
  6307. void upb_lock();
  6308. void upb_unlock();
  6309. #endif
  6310. /* UPB_DEBUG_REFS mode counts on being able to malloc() memory in some
  6311. * code-paths that can normally never fail, like upb_refcounted_ref(). Since
  6312. * we have no way to propagage out-of-memory errors back to the user, and since
  6313. * these errors can only occur in UPB_DEBUG_REFS mode, we use an allocator that
  6314. * immediately aborts on failure (avoiding the global allocator, which might
  6315. * inject failures). */
  6316. #include <stdlib.h>
  6317. static void *upb_debugrefs_allocfunc(upb_alloc *alloc, void *ptr,
  6318. size_t oldsize, size_t size) {
  6319. UPB_UNUSED(alloc);
  6320. UPB_UNUSED(oldsize);
  6321. if (size == 0) {
  6322. free(ptr);
  6323. return NULL;
  6324. } else {
  6325. void *ret = realloc(ptr, size);
  6326. if (!ret) {
  6327. abort();
  6328. }
  6329. return ret;
  6330. }
  6331. }
  6332. upb_alloc upb_alloc_debugrefs = {&upb_debugrefs_allocfunc};
  6333. typedef struct {
  6334. int count; /* How many refs there are (duplicates only allowed for ref2). */
  6335. bool is_ref2;
  6336. } trackedref;
  6337. static trackedref *trackedref_new(bool is_ref2) {
  6338. trackedref *ret = upb_malloc(&upb_alloc_debugrefs, sizeof(*ret));
  6339. ret->count = 1;
  6340. ret->is_ref2 = is_ref2;
  6341. return ret;
  6342. }
  6343. static void track(const upb_refcounted *r, const void *owner, bool ref2) {
  6344. upb_value v;
  6345. UPB_ASSERT(owner);
  6346. if (owner == UPB_UNTRACKED_REF) return;
  6347. upb_lock();
  6348. if (upb_inttable_lookupptr(r->refs, owner, &v)) {
  6349. trackedref *ref = upb_value_getptr(v);
  6350. /* Since we allow multiple ref2's for the same to/from pair without
  6351. * allocating separate memory for each one, we lose the fine-grained
  6352. * tracking behavior we get with regular refs. Since ref2s only happen
  6353. * inside upb, we'll accept this limitation until/unless there is a really
  6354. * difficult upb-internal bug that can't be figured out without it. */
  6355. UPB_ASSERT(ref2);
  6356. UPB_ASSERT(ref->is_ref2);
  6357. ref->count++;
  6358. } else {
  6359. trackedref *ref = trackedref_new(ref2);
  6360. upb_inttable_insertptr2(r->refs, owner, upb_value_ptr(ref),
  6361. &upb_alloc_debugrefs);
  6362. if (ref2) {
  6363. /* We know this cast is safe when it is a ref2, because it's coming from
  6364. * another refcounted object. */
  6365. const upb_refcounted *from = owner;
  6366. UPB_ASSERT(!upb_inttable_lookupptr(from->ref2s, r, NULL));
  6367. upb_inttable_insertptr2(from->ref2s, r, upb_value_ptr(NULL),
  6368. &upb_alloc_debugrefs);
  6369. }
  6370. }
  6371. upb_unlock();
  6372. }
  6373. static void untrack(const upb_refcounted *r, const void *owner, bool ref2) {
  6374. upb_value v;
  6375. bool found;
  6376. trackedref *ref;
  6377. UPB_ASSERT(owner);
  6378. if (owner == UPB_UNTRACKED_REF) return;
  6379. upb_lock();
  6380. found = upb_inttable_lookupptr(r->refs, owner, &v);
  6381. /* This assert will fail if an owner attempts to release a ref it didn't have. */
  6382. UPB_ASSERT(found);
  6383. ref = upb_value_getptr(v);
  6384. UPB_ASSERT(ref->is_ref2 == ref2);
  6385. if (--ref->count == 0) {
  6386. free(ref);
  6387. upb_inttable_removeptr(r->refs, owner, NULL);
  6388. if (ref2) {
  6389. /* We know this cast is safe when it is a ref2, because it's coming from
  6390. * another refcounted object. */
  6391. const upb_refcounted *from = owner;
  6392. bool removed = upb_inttable_removeptr(from->ref2s, r, NULL);
  6393. UPB_ASSERT(removed);
  6394. }
  6395. }
  6396. upb_unlock();
  6397. }
  6398. static void checkref(const upb_refcounted *r, const void *owner, bool ref2) {
  6399. upb_value v;
  6400. bool found;
  6401. trackedref *ref;
  6402. upb_lock();
  6403. found = upb_inttable_lookupptr(r->refs, owner, &v);
  6404. UPB_ASSERT(found);
  6405. ref = upb_value_getptr(v);
  6406. UPB_ASSERT(ref->is_ref2 == ref2);
  6407. upb_unlock();
  6408. }
  6409. /* Populates the given UPB_CTYPE_INT32 inttable with counts of ref2's that
  6410. * originate from the given owner. */
  6411. static void getref2s(const upb_refcounted *owner, upb_inttable *tab) {
  6412. upb_inttable_iter i;
  6413. upb_lock();
  6414. upb_inttable_begin(&i, owner->ref2s);
  6415. for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  6416. upb_value v;
  6417. upb_value count;
  6418. trackedref *ref;
  6419. bool found;
  6420. upb_refcounted *to = (upb_refcounted*)upb_inttable_iter_key(&i);
  6421. /* To get the count we need to look in the target's table. */
  6422. found = upb_inttable_lookupptr(to->refs, owner, &v);
  6423. UPB_ASSERT(found);
  6424. ref = upb_value_getptr(v);
  6425. count = upb_value_int32(ref->count);
  6426. upb_inttable_insertptr2(tab, to, count, &upb_alloc_debugrefs);
  6427. }
  6428. upb_unlock();
  6429. }
  6430. typedef struct {
  6431. upb_inttable ref2;
  6432. const upb_refcounted *obj;
  6433. } check_state;
  6434. static void visit_check(const upb_refcounted *obj, const upb_refcounted *subobj,
  6435. void *closure) {
  6436. check_state *s = closure;
  6437. upb_inttable *ref2 = &s->ref2;
  6438. upb_value v;
  6439. bool removed;
  6440. int32_t newcount;
  6441. UPB_ASSERT(obj == s->obj);
  6442. UPB_ASSERT(subobj);
  6443. removed = upb_inttable_removeptr(ref2, subobj, &v);
  6444. /* The following assertion will fail if the visit() function visits a subobj
  6445. * that it did not have a ref2 on, or visits the same subobj too many times. */
  6446. UPB_ASSERT(removed);
  6447. newcount = upb_value_getint32(v) - 1;
  6448. if (newcount > 0) {
  6449. upb_inttable_insert2(ref2, (uintptr_t)subobj, upb_value_int32(newcount),
  6450. &upb_alloc_debugrefs);
  6451. }
  6452. }
  6453. static void visit(const upb_refcounted *r, upb_refcounted_visit *v,
  6454. void *closure) {
  6455. /* In DEBUG_REFS mode we know what existing ref2 refs there are, so we know
  6456. * exactly the set of nodes that visit() should visit. So we verify visit()'s
  6457. * correctness here. */
  6458. check_state state;
  6459. state.obj = r;
  6460. upb_inttable_init2(&state.ref2, UPB_CTYPE_INT32, &upb_alloc_debugrefs);
  6461. getref2s(r, &state.ref2);
  6462. /* This should visit any children in the ref2 table. */
  6463. if (r->vtbl->visit) r->vtbl->visit(r, visit_check, &state);
  6464. /* This assertion will fail if the visit() function missed any children. */
  6465. UPB_ASSERT(upb_inttable_count(&state.ref2) == 0);
  6466. upb_inttable_uninit2(&state.ref2, &upb_alloc_debugrefs);
  6467. if (r->vtbl->visit) r->vtbl->visit(r, v, closure);
  6468. }
  6469. static void trackinit(upb_refcounted *r) {
  6470. r->refs = upb_malloc(&upb_alloc_debugrefs, sizeof(*r->refs));
  6471. r->ref2s = upb_malloc(&upb_alloc_debugrefs, sizeof(*r->ref2s));
  6472. upb_inttable_init2(r->refs, UPB_CTYPE_PTR, &upb_alloc_debugrefs);
  6473. upb_inttable_init2(r->ref2s, UPB_CTYPE_PTR, &upb_alloc_debugrefs);
  6474. }
  6475. static void trackfree(const upb_refcounted *r) {
  6476. upb_inttable_uninit2(r->refs, &upb_alloc_debugrefs);
  6477. upb_inttable_uninit2(r->ref2s, &upb_alloc_debugrefs);
  6478. upb_free(&upb_alloc_debugrefs, r->refs);
  6479. upb_free(&upb_alloc_debugrefs, r->ref2s);
  6480. }
  6481. #else
  6482. static void track(const upb_refcounted *r, const void *owner, bool ref2) {
  6483. UPB_UNUSED(r);
  6484. UPB_UNUSED(owner);
  6485. UPB_UNUSED(ref2);
  6486. }
  6487. static void untrack(const upb_refcounted *r, const void *owner, bool ref2) {
  6488. UPB_UNUSED(r);
  6489. UPB_UNUSED(owner);
  6490. UPB_UNUSED(ref2);
  6491. }
  6492. static void checkref(const upb_refcounted *r, const void *owner, bool ref2) {
  6493. UPB_UNUSED(r);
  6494. UPB_UNUSED(owner);
  6495. UPB_UNUSED(ref2);
  6496. }
  6497. static void trackinit(upb_refcounted *r) {
  6498. UPB_UNUSED(r);
  6499. }
  6500. static void trackfree(const upb_refcounted *r) {
  6501. UPB_UNUSED(r);
  6502. }
  6503. static void visit(const upb_refcounted *r, upb_refcounted_visit *v,
  6504. void *closure) {
  6505. if (r->vtbl->visit) r->vtbl->visit(r, v, closure);
  6506. }
  6507. #endif /* UPB_DEBUG_REFS */
  6508. /* freeze() *******************************************************************/
  6509. /* The freeze() operation is by far the most complicated part of this scheme.
  6510. * We compute strongly-connected components and then mutate the graph such that
  6511. * we preserve the invariants documented at the top of this file. And we must
  6512. * handle out-of-memory errors gracefully (without leaving the graph
  6513. * inconsistent), which adds to the fun. */
  6514. /* The state used by the freeze operation (shared across many functions). */
  6515. typedef struct {
  6516. int depth;
  6517. int maxdepth;
  6518. uint64_t index;
  6519. /* Maps upb_refcounted* -> attributes (color, etc). attr layout varies by
  6520. * color. */
  6521. upb_inttable objattr;
  6522. upb_inttable stack; /* stack of upb_refcounted* for Tarjan's algorithm. */
  6523. upb_inttable groups; /* array of uint32_t*, malloc'd refcounts for new groups */
  6524. upb_status *status;
  6525. jmp_buf err;
  6526. } tarjan;
  6527. static void release_ref2(const upb_refcounted *obj,
  6528. const upb_refcounted *subobj,
  6529. void *closure);
  6530. /* Node attributes -----------------------------------------------------------*/
  6531. /* After our analysis phase all nodes will be either GRAY or WHITE. */
  6532. typedef enum {
  6533. BLACK = 0, /* Object has not been seen. */
  6534. GRAY, /* Object has been found via a refgroup but may not be reachable. */
  6535. GREEN, /* Object is reachable and is currently on the Tarjan stack. */
  6536. WHITE /* Object is reachable and has been assigned a group (SCC). */
  6537. } color_t;
  6538. UPB_NORETURN static void err(tarjan *t) { longjmp(t->err, 1); }
  6539. UPB_NORETURN static void oom(tarjan *t) {
  6540. upb_status_seterrmsg(t->status, "out of memory");
  6541. err(t);
  6542. }
  6543. static uint64_t trygetattr(const tarjan *t, const upb_refcounted *r) {
  6544. upb_value v;
  6545. return upb_inttable_lookupptr(&t->objattr, r, &v) ?
  6546. upb_value_getuint64(v) : 0;
  6547. }
  6548. static uint64_t getattr(const tarjan *t, const upb_refcounted *r) {
  6549. upb_value v;
  6550. bool found = upb_inttable_lookupptr(&t->objattr, r, &v);
  6551. UPB_ASSERT(found);
  6552. return upb_value_getuint64(v);
  6553. }
  6554. static void setattr(tarjan *t, const upb_refcounted *r, uint64_t attr) {
  6555. upb_inttable_removeptr(&t->objattr, r, NULL);
  6556. upb_inttable_insertptr(&t->objattr, r, upb_value_uint64(attr));
  6557. }
  6558. static color_t color(tarjan *t, const upb_refcounted *r) {
  6559. return trygetattr(t, r) & 0x3; /* Color is always stored in the low 2 bits. */
  6560. }
  6561. static void set_gray(tarjan *t, const upb_refcounted *r) {
  6562. UPB_ASSERT(color(t, r) == BLACK);
  6563. setattr(t, r, GRAY);
  6564. }
  6565. /* Pushes an obj onto the Tarjan stack and sets it to GREEN. */
  6566. static void push(tarjan *t, const upb_refcounted *r) {
  6567. UPB_ASSERT(color(t, r) == BLACK || color(t, r) == GRAY);
  6568. /* This defines the attr layout for the GREEN state. "index" and "lowlink"
  6569. * get 31 bits, which is plenty (limit of 2B objects frozen at a time). */
  6570. setattr(t, r, GREEN | (t->index << 2) | (t->index << 33));
  6571. if (++t->index == 0x80000000) {
  6572. upb_status_seterrmsg(t->status, "too many objects to freeze");
  6573. err(t);
  6574. }
  6575. upb_inttable_push(&t->stack, upb_value_ptr((void*)r));
  6576. }
  6577. /* Pops an obj from the Tarjan stack and sets it to WHITE, with a ptr to its
  6578. * SCC group. */
  6579. static upb_refcounted *pop(tarjan *t) {
  6580. upb_refcounted *r = upb_value_getptr(upb_inttable_pop(&t->stack));
  6581. UPB_ASSERT(color(t, r) == GREEN);
  6582. /* This defines the attr layout for nodes in the WHITE state.
  6583. * Top of group stack is [group, NULL]; we point at group. */
  6584. setattr(t, r, WHITE | (upb_inttable_count(&t->groups) - 2) << 8);
  6585. return r;
  6586. }
  6587. static void tarjan_newgroup(tarjan *t) {
  6588. uint32_t *group = upb_gmalloc(sizeof(*group));
  6589. if (!group) oom(t);
  6590. /* Push group and empty group leader (we'll fill in leader later). */
  6591. if (!upb_inttable_push(&t->groups, upb_value_ptr(group)) ||
  6592. !upb_inttable_push(&t->groups, upb_value_ptr(NULL))) {
  6593. upb_gfree(group);
  6594. oom(t);
  6595. }
  6596. *group = 0;
  6597. }
  6598. static uint32_t idx(tarjan *t, const upb_refcounted *r) {
  6599. UPB_ASSERT(color(t, r) == GREEN);
  6600. return (getattr(t, r) >> 2) & 0x7FFFFFFF;
  6601. }
  6602. static uint32_t lowlink(tarjan *t, const upb_refcounted *r) {
  6603. if (color(t, r) == GREEN) {
  6604. return getattr(t, r) >> 33;
  6605. } else {
  6606. return UINT32_MAX;
  6607. }
  6608. }
  6609. static void set_lowlink(tarjan *t, const upb_refcounted *r, uint32_t lowlink) {
  6610. UPB_ASSERT(color(t, r) == GREEN);
  6611. setattr(t, r, ((uint64_t)lowlink << 33) | (getattr(t, r) & 0x1FFFFFFFF));
  6612. }
  6613. static uint32_t *group(tarjan *t, upb_refcounted *r) {
  6614. uint64_t groupnum;
  6615. upb_value v;
  6616. bool found;
  6617. UPB_ASSERT(color(t, r) == WHITE);
  6618. groupnum = getattr(t, r) >> 8;
  6619. found = upb_inttable_lookup(&t->groups, groupnum, &v);
  6620. UPB_ASSERT(found);
  6621. return upb_value_getptr(v);
  6622. }
  6623. /* If the group leader for this object's group has not previously been set,
  6624. * the given object is assigned to be its leader. */
  6625. static upb_refcounted *groupleader(tarjan *t, upb_refcounted *r) {
  6626. uint64_t leader_slot;
  6627. upb_value v;
  6628. bool found;
  6629. UPB_ASSERT(color(t, r) == WHITE);
  6630. leader_slot = (getattr(t, r) >> 8) + 1;
  6631. found = upb_inttable_lookup(&t->groups, leader_slot, &v);
  6632. UPB_ASSERT(found);
  6633. if (upb_value_getptr(v)) {
  6634. return upb_value_getptr(v);
  6635. } else {
  6636. upb_inttable_remove(&t->groups, leader_slot, NULL);
  6637. upb_inttable_insert(&t->groups, leader_slot, upb_value_ptr(r));
  6638. return r;
  6639. }
  6640. }
  6641. /* Tarjan's algorithm --------------------------------------------------------*/
  6642. /* See:
  6643. * http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm */
  6644. static void do_tarjan(const upb_refcounted *obj, tarjan *t);
  6645. static void tarjan_visit(const upb_refcounted *obj,
  6646. const upb_refcounted *subobj,
  6647. void *closure) {
  6648. tarjan *t = closure;
  6649. if (++t->depth > t->maxdepth) {
  6650. upb_status_seterrf(t->status, "graph too deep to freeze (%d)", t->maxdepth);
  6651. err(t);
  6652. } else if (subobj->is_frozen || color(t, subobj) == WHITE) {
  6653. /* Do nothing: we don't want to visit or color already-frozen nodes,
  6654. * and WHITE nodes have already been assigned a SCC. */
  6655. } else if (color(t, subobj) < GREEN) {
  6656. /* Subdef has not yet been visited; recurse on it. */
  6657. do_tarjan(subobj, t);
  6658. set_lowlink(t, obj, UPB_MIN(lowlink(t, obj), lowlink(t, subobj)));
  6659. } else if (color(t, subobj) == GREEN) {
  6660. /* Subdef is in the stack and hence in the current SCC. */
  6661. set_lowlink(t, obj, UPB_MIN(lowlink(t, obj), idx(t, subobj)));
  6662. }
  6663. --t->depth;
  6664. }
  6665. static void do_tarjan(const upb_refcounted *obj, tarjan *t) {
  6666. if (color(t, obj) == BLACK) {
  6667. /* We haven't seen this object's group; mark the whole group GRAY. */
  6668. const upb_refcounted *o = obj;
  6669. do { set_gray(t, o); } while ((o = o->next) != obj);
  6670. }
  6671. push(t, obj);
  6672. visit(obj, tarjan_visit, t);
  6673. if (lowlink(t, obj) == idx(t, obj)) {
  6674. tarjan_newgroup(t);
  6675. while (pop(t) != obj)
  6676. ;
  6677. }
  6678. }
  6679. /* freeze() ------------------------------------------------------------------*/
  6680. static void crossref(const upb_refcounted *r, const upb_refcounted *subobj,
  6681. void *_t) {
  6682. tarjan *t = _t;
  6683. UPB_ASSERT(color(t, r) > BLACK);
  6684. if (color(t, subobj) > BLACK && r->group != subobj->group) {
  6685. /* Previously this ref was not reflected in subobj->group because they
  6686. * were in the same group; now that they are split a ref must be taken. */
  6687. refgroup(subobj->group);
  6688. }
  6689. }
  6690. static bool freeze(upb_refcounted *const*roots, int n, upb_status *s,
  6691. int maxdepth) {
  6692. volatile bool ret = false;
  6693. int i;
  6694. upb_inttable_iter iter;
  6695. /* We run in two passes so that we can allocate all memory before performing
  6696. * any mutation of the input -- this allows us to leave the input unchanged
  6697. * in the case of memory allocation failure. */
  6698. tarjan t;
  6699. t.index = 0;
  6700. t.depth = 0;
  6701. t.maxdepth = maxdepth;
  6702. t.status = s;
  6703. if (!upb_inttable_init(&t.objattr, UPB_CTYPE_UINT64)) goto err1;
  6704. if (!upb_inttable_init(&t.stack, UPB_CTYPE_PTR)) goto err2;
  6705. if (!upb_inttable_init(&t.groups, UPB_CTYPE_PTR)) goto err3;
  6706. if (setjmp(t.err) != 0) goto err4;
  6707. for (i = 0; i < n; i++) {
  6708. if (color(&t, roots[i]) < GREEN) {
  6709. do_tarjan(roots[i], &t);
  6710. }
  6711. }
  6712. /* If we've made it this far, no further errors are possible so it's safe to
  6713. * mutate the objects without risk of leaving them in an inconsistent state. */
  6714. ret = true;
  6715. /* The transformation that follows requires care. The preconditions are:
  6716. * - all objects in attr map are WHITE or GRAY, and are in mutable groups
  6717. * (groups of all mutable objs)
  6718. * - no ref2(to, from) refs have incremented count(to) if both "to" and
  6719. * "from" are in our attr map (this follows from invariants (2) and (3)) */
  6720. /* Pass 1: we remove WHITE objects from their mutable groups, and add them to
  6721. * new groups according to the SCC's we computed. These new groups will
  6722. * consist of only frozen objects. None will be immediately collectible,
  6723. * because WHITE objects are by definition reachable from one of "roots",
  6724. * which the caller must own refs on. */
  6725. upb_inttable_begin(&iter, &t.objattr);
  6726. for(; !upb_inttable_done(&iter); upb_inttable_next(&iter)) {
  6727. upb_refcounted *obj = (upb_refcounted*)upb_inttable_iter_key(&iter);
  6728. /* Since removal from a singly-linked list requires access to the object's
  6729. * predecessor, we consider obj->next instead of obj for moving. With the
  6730. * while() loop we guarantee that we will visit every node's predecessor.
  6731. * Proof:
  6732. * 1. every node's predecessor is in our attr map.
  6733. * 2. though the loop body may change a node's predecessor, it will only
  6734. * change it to be the node we are currently operating on, so with a
  6735. * while() loop we guarantee ourselves the chance to remove each node. */
  6736. while (color(&t, obj->next) == WHITE &&
  6737. group(&t, obj->next) != obj->next->group) {
  6738. upb_refcounted *leader;
  6739. /* Remove from old group. */
  6740. upb_refcounted *move = obj->next;
  6741. if (obj == move) {
  6742. /* Removing the last object from a group. */
  6743. UPB_ASSERT(*obj->group == obj->individual_count);
  6744. upb_gfree(obj->group);
  6745. } else {
  6746. obj->next = move->next;
  6747. /* This may decrease to zero; we'll collect GRAY objects (if any) that
  6748. * remain in the group in the third pass. */
  6749. UPB_ASSERT(*move->group >= move->individual_count);
  6750. *move->group -= move->individual_count;
  6751. }
  6752. /* Add to new group. */
  6753. leader = groupleader(&t, move);
  6754. if (move == leader) {
  6755. /* First object added to new group is its leader. */
  6756. move->group = group(&t, move);
  6757. move->next = move;
  6758. *move->group = move->individual_count;
  6759. } else {
  6760. /* Group already has at least one object in it. */
  6761. UPB_ASSERT(leader->group == group(&t, move));
  6762. move->group = group(&t, move);
  6763. move->next = leader->next;
  6764. leader->next = move;
  6765. *move->group += move->individual_count;
  6766. }
  6767. move->is_frozen = true;
  6768. }
  6769. }
  6770. /* Pass 2: GRAY and WHITE objects "obj" with ref2(to, obj) references must
  6771. * increment count(to) if group(obj) != group(to) (which could now be the
  6772. * case if "to" was just frozen). */
  6773. upb_inttable_begin(&iter, &t.objattr);
  6774. for(; !upb_inttable_done(&iter); upb_inttable_next(&iter)) {
  6775. upb_refcounted *obj = (upb_refcounted*)upb_inttable_iter_key(&iter);
  6776. visit(obj, crossref, &t);
  6777. }
  6778. /* Pass 3: GRAY objects are collected if their group's refcount dropped to
  6779. * zero when we removed its white nodes. This can happen if they had only
  6780. * been kept alive by virtue of sharing a group with an object that was just
  6781. * frozen.
  6782. *
  6783. * It is important that we do this last, since the GRAY object's free()
  6784. * function could call unref2() on just-frozen objects, which will decrement
  6785. * refs that were added in pass 2. */
  6786. upb_inttable_begin(&iter, &t.objattr);
  6787. for(; !upb_inttable_done(&iter); upb_inttable_next(&iter)) {
  6788. upb_refcounted *obj = (upb_refcounted*)upb_inttable_iter_key(&iter);
  6789. if (obj->group == NULL || *obj->group == 0) {
  6790. if (obj->group) {
  6791. upb_refcounted *o;
  6792. /* We eagerly free() the group's count (since we can't easily determine
  6793. * the group's remaining size it's the easiest way to ensure it gets
  6794. * done). */
  6795. upb_gfree(obj->group);
  6796. /* Visit to release ref2's (done in a separate pass since release_ref2
  6797. * depends on o->group being unmodified so it can test merged()). */
  6798. o = obj;
  6799. do { visit(o, release_ref2, NULL); } while ((o = o->next) != obj);
  6800. /* Mark "group" fields as NULL so we know to free the objects later in
  6801. * this loop, but also don't try to delete the group twice. */
  6802. o = obj;
  6803. do { o->group = NULL; } while ((o = o->next) != obj);
  6804. }
  6805. freeobj(obj);
  6806. }
  6807. }
  6808. err4:
  6809. if (!ret) {
  6810. upb_inttable_begin(&iter, &t.groups);
  6811. for(; !upb_inttable_done(&iter); upb_inttable_next(&iter))
  6812. upb_gfree(upb_value_getptr(upb_inttable_iter_value(&iter)));
  6813. }
  6814. upb_inttable_uninit(&t.groups);
  6815. err3:
  6816. upb_inttable_uninit(&t.stack);
  6817. err2:
  6818. upb_inttable_uninit(&t.objattr);
  6819. err1:
  6820. return ret;
  6821. }
  6822. /* Misc internal functions ***************************************************/
  6823. static bool merged(const upb_refcounted *r, const upb_refcounted *r2) {
  6824. return r->group == r2->group;
  6825. }
  6826. static void merge(upb_refcounted *r, upb_refcounted *from) {
  6827. upb_refcounted *base;
  6828. upb_refcounted *tmp;
  6829. if (merged(r, from)) return;
  6830. *r->group += *from->group;
  6831. upb_gfree(from->group);
  6832. base = from;
  6833. /* Set all refcount pointers in the "from" chain to the merged refcount.
  6834. *
  6835. * TODO(haberman): this linear algorithm can result in an overall O(n^2) bound
  6836. * if the user continuously extends a group by one object. Prevent this by
  6837. * using one of the techniques in this paper:
  6838. * http://bioinfo.ict.ac.cn/~dbu/AlgorithmCourses/Lectures/Union-Find-Tarjan.pdf */
  6839. do { from->group = r->group; } while ((from = from->next) != base);
  6840. /* Merge the two circularly linked lists by swapping their next pointers. */
  6841. tmp = r->next;
  6842. r->next = base->next;
  6843. base->next = tmp;
  6844. }
  6845. static void unref(const upb_refcounted *r);
  6846. static void release_ref2(const upb_refcounted *obj,
  6847. const upb_refcounted *subobj,
  6848. void *closure) {
  6849. UPB_UNUSED(closure);
  6850. untrack(subobj, obj, true);
  6851. if (!merged(obj, subobj)) {
  6852. UPB_ASSERT(subobj->is_frozen);
  6853. unref(subobj);
  6854. }
  6855. }
  6856. static void unref(const upb_refcounted *r) {
  6857. if (unrefgroup(r->group)) {
  6858. const upb_refcounted *o;
  6859. upb_gfree(r->group);
  6860. /* In two passes, since release_ref2 needs a guarantee that any subobjs
  6861. * are alive. */
  6862. o = r;
  6863. do { visit(o, release_ref2, NULL); } while((o = o->next) != r);
  6864. o = r;
  6865. do {
  6866. const upb_refcounted *next = o->next;
  6867. UPB_ASSERT(o->is_frozen || o->individual_count == 0);
  6868. freeobj((upb_refcounted*)o);
  6869. o = next;
  6870. } while(o != r);
  6871. }
  6872. }
  6873. static void freeobj(upb_refcounted *o) {
  6874. trackfree(o);
  6875. o->vtbl->free((upb_refcounted*)o);
  6876. }
  6877. /* Public interface ***********************************************************/
  6878. bool upb_refcounted_init(upb_refcounted *r,
  6879. const struct upb_refcounted_vtbl *vtbl,
  6880. const void *owner) {
  6881. #ifndef NDEBUG
  6882. /* Endianness check. This is unrelated to upb_refcounted, it's just a
  6883. * convenient place to put the check that we can be assured will run for
  6884. * basically every program using upb. */
  6885. const int x = 1;
  6886. #ifdef UPB_BIG_ENDIAN
  6887. UPB_ASSERT(*(char*)&x != 1);
  6888. #else
  6889. UPB_ASSERT(*(char*)&x == 1);
  6890. #endif
  6891. #endif
  6892. r->next = r;
  6893. r->vtbl = vtbl;
  6894. r->individual_count = 0;
  6895. r->is_frozen = false;
  6896. r->group = upb_gmalloc(sizeof(*r->group));
  6897. if (!r->group) return false;
  6898. *r->group = 0;
  6899. trackinit(r);
  6900. upb_refcounted_ref(r, owner);
  6901. return true;
  6902. }
  6903. bool upb_refcounted_isfrozen(const upb_refcounted *r) {
  6904. return r->is_frozen;
  6905. }
  6906. void upb_refcounted_ref(const upb_refcounted *r, const void *owner) {
  6907. track(r, owner, false);
  6908. if (!r->is_frozen)
  6909. ((upb_refcounted*)r)->individual_count++;
  6910. refgroup(r->group);
  6911. }
  6912. void upb_refcounted_unref(const upb_refcounted *r, const void *owner) {
  6913. untrack(r, owner, false);
  6914. if (!r->is_frozen)
  6915. ((upb_refcounted*)r)->individual_count--;
  6916. unref(r);
  6917. }
  6918. void upb_refcounted_ref2(const upb_refcounted *r, upb_refcounted *from) {
  6919. UPB_ASSERT(!from->is_frozen); /* Non-const pointer implies this. */
  6920. track(r, from, true);
  6921. if (r->is_frozen) {
  6922. refgroup(r->group);
  6923. } else {
  6924. merge((upb_refcounted*)r, from);
  6925. }
  6926. }
  6927. void upb_refcounted_unref2(const upb_refcounted *r, upb_refcounted *from) {
  6928. UPB_ASSERT(!from->is_frozen); /* Non-const pointer implies this. */
  6929. untrack(r, from, true);
  6930. if (r->is_frozen) {
  6931. unref(r);
  6932. } else {
  6933. UPB_ASSERT(merged(r, from));
  6934. }
  6935. }
  6936. void upb_refcounted_donateref(
  6937. const upb_refcounted *r, const void *from, const void *to) {
  6938. UPB_ASSERT(from != to);
  6939. if (to != NULL)
  6940. upb_refcounted_ref(r, to);
  6941. if (from != NULL)
  6942. upb_refcounted_unref(r, from);
  6943. }
  6944. void upb_refcounted_checkref(const upb_refcounted *r, const void *owner) {
  6945. checkref(r, owner, false);
  6946. }
  6947. bool upb_refcounted_freeze(upb_refcounted *const*roots, int n, upb_status *s,
  6948. int maxdepth) {
  6949. int i;
  6950. bool ret;
  6951. for (i = 0; i < n; i++) {
  6952. UPB_ASSERT(!roots[i]->is_frozen);
  6953. }
  6954. ret = freeze(roots, n, s, maxdepth);
  6955. UPB_ASSERT(!s || ret == upb_ok(s));
  6956. return ret;
  6957. }
  6958. bool upb_bufsrc_putbuf(const char *buf, size_t len, upb_bytessink *sink) {
  6959. void *subc;
  6960. bool ret;
  6961. upb_bufhandle handle;
  6962. upb_bufhandle_init(&handle);
  6963. upb_bufhandle_setbuf(&handle, buf, 0);
  6964. ret = upb_bytessink_start(sink, len, &subc);
  6965. if (ret && len != 0) {
  6966. ret = (upb_bytessink_putbuf(sink, subc, buf, len, &handle) >= len);
  6967. }
  6968. if (ret) {
  6969. ret = upb_bytessink_end(sink);
  6970. }
  6971. upb_bufhandle_uninit(&handle);
  6972. return ret;
  6973. }
  6974. struct upb_bufsink {
  6975. upb_byteshandler handler;
  6976. upb_bytessink sink;
  6977. upb_env *env;
  6978. char *ptr;
  6979. size_t len, size;
  6980. };
  6981. static void *upb_bufsink_start(void *_sink, const void *hd, size_t size_hint) {
  6982. upb_bufsink *sink = _sink;
  6983. UPB_UNUSED(hd);
  6984. UPB_UNUSED(size_hint);
  6985. sink->len = 0;
  6986. return sink;
  6987. }
  6988. static size_t upb_bufsink_string(void *_sink, const void *hd, const char *ptr,
  6989. size_t len, const upb_bufhandle *handle) {
  6990. upb_bufsink *sink = _sink;
  6991. size_t new_size = sink->size;
  6992. UPB_ASSERT(new_size > 0);
  6993. UPB_UNUSED(hd);
  6994. UPB_UNUSED(handle);
  6995. while (sink->len + len > new_size) {
  6996. new_size *= 2;
  6997. }
  6998. if (new_size != sink->size) {
  6999. sink->ptr = upb_env_realloc(sink->env, sink->ptr, sink->size, new_size);
  7000. sink->size = new_size;
  7001. }
  7002. memcpy(sink->ptr + sink->len, ptr, len);
  7003. sink->len += len;
  7004. return len;
  7005. }
  7006. upb_bufsink *upb_bufsink_new(upb_env *env) {
  7007. upb_bufsink *sink = upb_env_malloc(env, sizeof(upb_bufsink));
  7008. upb_byteshandler_init(&sink->handler);
  7009. upb_byteshandler_setstartstr(&sink->handler, upb_bufsink_start, NULL);
  7010. upb_byteshandler_setstring(&sink->handler, upb_bufsink_string, NULL);
  7011. upb_bytessink_reset(&sink->sink, &sink->handler, sink);
  7012. sink->env = env;
  7013. sink->size = 32;
  7014. sink->ptr = upb_env_malloc(env, sink->size);
  7015. sink->len = 0;
  7016. return sink;
  7017. }
  7018. void upb_bufsink_free(upb_bufsink *sink) {
  7019. upb_env_free(sink->env, sink->ptr);
  7020. upb_env_free(sink->env, sink);
  7021. }
  7022. upb_bytessink *upb_bufsink_sink(upb_bufsink *sink) {
  7023. return &sink->sink;
  7024. }
  7025. const char *upb_bufsink_getdata(const upb_bufsink *sink, size_t *len) {
  7026. *len = sink->len;
  7027. return sink->ptr;
  7028. }
  7029. /*
  7030. ** upb_table Implementation
  7031. **
  7032. ** Implementation is heavily inspired by Lua's ltable.c.
  7033. */
  7034. #include <string.h>
  7035. #define UPB_MAXARRSIZE 16 /* 64k. */
  7036. /* From Chromium. */
  7037. #define ARRAY_SIZE(x) \
  7038. ((sizeof(x)/sizeof(0[x])) / ((size_t)(!(sizeof(x) % sizeof(0[x])))))
  7039. static void upb_check_alloc(upb_table *t, upb_alloc *a) {
  7040. UPB_UNUSED(t);
  7041. UPB_UNUSED(a);
  7042. UPB_ASSERT_DEBUGVAR(t->alloc == a);
  7043. }
  7044. static const double MAX_LOAD = 0.85;
  7045. /* The minimum utilization of the array part of a mixed hash/array table. This
  7046. * is a speed/memory-usage tradeoff (though it's not straightforward because of
  7047. * cache effects). The lower this is, the more memory we'll use. */
  7048. static const double MIN_DENSITY = 0.1;
  7049. bool is_pow2(uint64_t v) { return v == 0 || (v & (v - 1)) == 0; }
  7050. int log2ceil(uint64_t v) {
  7051. int ret = 0;
  7052. bool pow2 = is_pow2(v);
  7053. while (v >>= 1) ret++;
  7054. ret = pow2 ? ret : ret + 1; /* Ceiling. */
  7055. return UPB_MIN(UPB_MAXARRSIZE, ret);
  7056. }
  7057. char *upb_strdup(const char *s, upb_alloc *a) {
  7058. return upb_strdup2(s, strlen(s), a);
  7059. }
  7060. char *upb_strdup2(const char *s, size_t len, upb_alloc *a) {
  7061. size_t n;
  7062. char *p;
  7063. /* Prevent overflow errors. */
  7064. if (len == SIZE_MAX) return NULL;
  7065. /* Always null-terminate, even if binary data; but don't rely on the input to
  7066. * have a null-terminating byte since it may be a raw binary buffer. */
  7067. n = len + 1;
  7068. p = upb_malloc(a, n);
  7069. if (p) {
  7070. memcpy(p, s, len);
  7071. p[len] = 0;
  7072. }
  7073. return p;
  7074. }
  7075. /* A type to represent the lookup key of either a strtable or an inttable. */
  7076. typedef union {
  7077. uintptr_t num;
  7078. struct {
  7079. const char *str;
  7080. size_t len;
  7081. } str;
  7082. } lookupkey_t;
  7083. static lookupkey_t strkey2(const char *str, size_t len) {
  7084. lookupkey_t k;
  7085. k.str.str = str;
  7086. k.str.len = len;
  7087. return k;
  7088. }
  7089. static lookupkey_t intkey(uintptr_t key) {
  7090. lookupkey_t k;
  7091. k.num = key;
  7092. return k;
  7093. }
  7094. typedef uint32_t hashfunc_t(upb_tabkey key);
  7095. typedef bool eqlfunc_t(upb_tabkey k1, lookupkey_t k2);
  7096. /* Base table (shared code) ***************************************************/
  7097. /* For when we need to cast away const. */
  7098. static upb_tabent *mutable_entries(upb_table *t) {
  7099. return (upb_tabent*)t->entries;
  7100. }
  7101. static bool isfull(upb_table *t) {
  7102. if (upb_table_size(t) == 0) {
  7103. return true;
  7104. } else {
  7105. return ((double)(t->count + 1) / upb_table_size(t)) > MAX_LOAD;
  7106. }
  7107. }
  7108. static bool init(upb_table *t, upb_ctype_t ctype, uint8_t size_lg2,
  7109. upb_alloc *a) {
  7110. size_t bytes;
  7111. t->count = 0;
  7112. t->ctype = ctype;
  7113. t->size_lg2 = size_lg2;
  7114. t->mask = upb_table_size(t) ? upb_table_size(t) - 1 : 0;
  7115. #ifndef NDEBUG
  7116. t->alloc = a;
  7117. #endif
  7118. bytes = upb_table_size(t) * sizeof(upb_tabent);
  7119. if (bytes > 0) {
  7120. t->entries = upb_malloc(a, bytes);
  7121. if (!t->entries) return false;
  7122. memset(mutable_entries(t), 0, bytes);
  7123. } else {
  7124. t->entries = NULL;
  7125. }
  7126. return true;
  7127. }
  7128. static void uninit(upb_table *t, upb_alloc *a) {
  7129. upb_check_alloc(t, a);
  7130. upb_free(a, mutable_entries(t));
  7131. }
  7132. static upb_tabent *emptyent(upb_table *t) {
  7133. upb_tabent *e = mutable_entries(t) + upb_table_size(t);
  7134. while (1) { if (upb_tabent_isempty(--e)) return e; UPB_ASSERT(e > t->entries); }
  7135. }
  7136. static upb_tabent *getentry_mutable(upb_table *t, uint32_t hash) {
  7137. return (upb_tabent*)upb_getentry(t, hash);
  7138. }
  7139. static const upb_tabent *findentry(const upb_table *t, lookupkey_t key,
  7140. uint32_t hash, eqlfunc_t *eql) {
  7141. const upb_tabent *e;
  7142. if (t->size_lg2 == 0) return NULL;
  7143. e = upb_getentry(t, hash);
  7144. if (upb_tabent_isempty(e)) return NULL;
  7145. while (1) {
  7146. if (eql(e->key, key)) return e;
  7147. if ((e = e->next) == NULL) return NULL;
  7148. }
  7149. }
  7150. static upb_tabent *findentry_mutable(upb_table *t, lookupkey_t key,
  7151. uint32_t hash, eqlfunc_t *eql) {
  7152. return (upb_tabent*)findentry(t, key, hash, eql);
  7153. }
  7154. static bool lookup(const upb_table *t, lookupkey_t key, upb_value *v,
  7155. uint32_t hash, eqlfunc_t *eql) {
  7156. const upb_tabent *e = findentry(t, key, hash, eql);
  7157. if (e) {
  7158. if (v) {
  7159. _upb_value_setval(v, e->val.val, t->ctype);
  7160. }
  7161. return true;
  7162. } else {
  7163. return false;
  7164. }
  7165. }
  7166. /* The given key must not already exist in the table. */
  7167. static void insert(upb_table *t, lookupkey_t key, upb_tabkey tabkey,
  7168. upb_value val, uint32_t hash,
  7169. hashfunc_t *hashfunc, eqlfunc_t *eql) {
  7170. upb_tabent *mainpos_e;
  7171. upb_tabent *our_e;
  7172. UPB_ASSERT(findentry(t, key, hash, eql) == NULL);
  7173. UPB_ASSERT_DEBUGVAR(val.ctype == t->ctype);
  7174. t->count++;
  7175. mainpos_e = getentry_mutable(t, hash);
  7176. our_e = mainpos_e;
  7177. if (upb_tabent_isempty(mainpos_e)) {
  7178. /* Our main position is empty; use it. */
  7179. our_e->next = NULL;
  7180. } else {
  7181. /* Collision. */
  7182. upb_tabent *new_e = emptyent(t);
  7183. /* Head of collider's chain. */
  7184. upb_tabent *chain = getentry_mutable(t, hashfunc(mainpos_e->key));
  7185. if (chain == mainpos_e) {
  7186. /* Existing ent is in its main posisiton (it has the same hash as us, and
  7187. * is the head of our chain). Insert to new ent and append to this chain. */
  7188. new_e->next = mainpos_e->next;
  7189. mainpos_e->next = new_e;
  7190. our_e = new_e;
  7191. } else {
  7192. /* Existing ent is not in its main position (it is a node in some other
  7193. * chain). This implies that no existing ent in the table has our hash.
  7194. * Evict it (updating its chain) and use its ent for head of our chain. */
  7195. *new_e = *mainpos_e; /* copies next. */
  7196. while (chain->next != mainpos_e) {
  7197. chain = (upb_tabent*)chain->next;
  7198. UPB_ASSERT(chain);
  7199. }
  7200. chain->next = new_e;
  7201. our_e = mainpos_e;
  7202. our_e->next = NULL;
  7203. }
  7204. }
  7205. our_e->key = tabkey;
  7206. our_e->val.val = val.val;
  7207. UPB_ASSERT(findentry(t, key, hash, eql) == our_e);
  7208. }
  7209. static bool rm(upb_table *t, lookupkey_t key, upb_value *val,
  7210. upb_tabkey *removed, uint32_t hash, eqlfunc_t *eql) {
  7211. upb_tabent *chain = getentry_mutable(t, hash);
  7212. if (upb_tabent_isempty(chain)) return false;
  7213. if (eql(chain->key, key)) {
  7214. /* Element to remove is at the head of its chain. */
  7215. t->count--;
  7216. if (val) _upb_value_setval(val, chain->val.val, t->ctype);
  7217. if (removed) *removed = chain->key;
  7218. if (chain->next) {
  7219. upb_tabent *move = (upb_tabent*)chain->next;
  7220. *chain = *move;
  7221. move->key = 0; /* Make the slot empty. */
  7222. } else {
  7223. chain->key = 0; /* Make the slot empty. */
  7224. }
  7225. return true;
  7226. } else {
  7227. /* Element to remove is either in a non-head position or not in the
  7228. * table. */
  7229. while (chain->next && !eql(chain->next->key, key)) {
  7230. chain = (upb_tabent*)chain->next;
  7231. }
  7232. if (chain->next) {
  7233. /* Found element to remove. */
  7234. upb_tabent *rm = (upb_tabent*)chain->next;
  7235. t->count--;
  7236. if (val) _upb_value_setval(val, chain->next->val.val, t->ctype);
  7237. if (removed) *removed = rm->key;
  7238. rm->key = 0; /* Make the slot empty. */
  7239. chain->next = rm->next;
  7240. return true;
  7241. } else {
  7242. /* Element to remove is not in the table. */
  7243. return false;
  7244. }
  7245. }
  7246. }
  7247. static size_t next(const upb_table *t, size_t i) {
  7248. do {
  7249. if (++i >= upb_table_size(t))
  7250. return SIZE_MAX;
  7251. } while(upb_tabent_isempty(&t->entries[i]));
  7252. return i;
  7253. }
  7254. static size_t begin(const upb_table *t) {
  7255. return next(t, -1);
  7256. }
  7257. /* upb_strtable ***************************************************************/
  7258. /* A simple "subclass" of upb_table that only adds a hash function for strings. */
  7259. static upb_tabkey strcopy(lookupkey_t k2, upb_alloc *a) {
  7260. char *str = upb_malloc(a, k2.str.len + sizeof(uint32_t) + 1);
  7261. if (str == NULL) return 0;
  7262. memcpy(str, &k2.str.len, sizeof(uint32_t));
  7263. memcpy(str + sizeof(uint32_t), k2.str.str, k2.str.len + 1);
  7264. return (uintptr_t)str;
  7265. }
  7266. static uint32_t strhash(upb_tabkey key) {
  7267. uint32_t len;
  7268. char *str = upb_tabstr(key, &len);
  7269. return MurmurHash2(str, len, 0);
  7270. }
  7271. static bool streql(upb_tabkey k1, lookupkey_t k2) {
  7272. uint32_t len;
  7273. char *str = upb_tabstr(k1, &len);
  7274. return len == k2.str.len && memcmp(str, k2.str.str, len) == 0;
  7275. }
  7276. bool upb_strtable_init2(upb_strtable *t, upb_ctype_t ctype, upb_alloc *a) {
  7277. return init(&t->t, ctype, 2, a);
  7278. }
  7279. void upb_strtable_uninit2(upb_strtable *t, upb_alloc *a) {
  7280. size_t i;
  7281. for (i = 0; i < upb_table_size(&t->t); i++)
  7282. upb_free(a, (void*)t->t.entries[i].key);
  7283. uninit(&t->t, a);
  7284. }
  7285. bool upb_strtable_resize(upb_strtable *t, size_t size_lg2, upb_alloc *a) {
  7286. upb_strtable new_table;
  7287. upb_strtable_iter i;
  7288. upb_check_alloc(&t->t, a);
  7289. if (!init(&new_table.t, t->t.ctype, size_lg2, a))
  7290. return false;
  7291. upb_strtable_begin(&i, t);
  7292. for ( ; !upb_strtable_done(&i); upb_strtable_next(&i)) {
  7293. upb_strtable_insert3(
  7294. &new_table,
  7295. upb_strtable_iter_key(&i),
  7296. upb_strtable_iter_keylength(&i),
  7297. upb_strtable_iter_value(&i),
  7298. a);
  7299. }
  7300. upb_strtable_uninit2(t, a);
  7301. *t = new_table;
  7302. return true;
  7303. }
  7304. bool upb_strtable_insert3(upb_strtable *t, const char *k, size_t len,
  7305. upb_value v, upb_alloc *a) {
  7306. lookupkey_t key;
  7307. upb_tabkey tabkey;
  7308. uint32_t hash;
  7309. upb_check_alloc(&t->t, a);
  7310. if (isfull(&t->t)) {
  7311. /* Need to resize. New table of double the size, add old elements to it. */
  7312. if (!upb_strtable_resize(t, t->t.size_lg2 + 1, a)) {
  7313. return false;
  7314. }
  7315. }
  7316. key = strkey2(k, len);
  7317. tabkey = strcopy(key, a);
  7318. if (tabkey == 0) return false;
  7319. hash = MurmurHash2(key.str.str, key.str.len, 0);
  7320. insert(&t->t, key, tabkey, v, hash, &strhash, &streql);
  7321. return true;
  7322. }
  7323. bool upb_strtable_lookup2(const upb_strtable *t, const char *key, size_t len,
  7324. upb_value *v) {
  7325. uint32_t hash = MurmurHash2(key, len, 0);
  7326. return lookup(&t->t, strkey2(key, len), v, hash, &streql);
  7327. }
  7328. bool upb_strtable_remove3(upb_strtable *t, const char *key, size_t len,
  7329. upb_value *val, upb_alloc *alloc) {
  7330. uint32_t hash = MurmurHash2(key, len, 0);
  7331. upb_tabkey tabkey;
  7332. if (rm(&t->t, strkey2(key, len), val, &tabkey, hash, &streql)) {
  7333. upb_free(alloc, (void*)tabkey);
  7334. return true;
  7335. } else {
  7336. return false;
  7337. }
  7338. }
  7339. /* Iteration */
  7340. static const upb_tabent *str_tabent(const upb_strtable_iter *i) {
  7341. return &i->t->t.entries[i->index];
  7342. }
  7343. void upb_strtable_begin(upb_strtable_iter *i, const upb_strtable *t) {
  7344. i->t = t;
  7345. i->index = begin(&t->t);
  7346. }
  7347. void upb_strtable_next(upb_strtable_iter *i) {
  7348. i->index = next(&i->t->t, i->index);
  7349. }
  7350. bool upb_strtable_done(const upb_strtable_iter *i) {
  7351. return i->index >= upb_table_size(&i->t->t) ||
  7352. upb_tabent_isempty(str_tabent(i));
  7353. }
  7354. const char *upb_strtable_iter_key(const upb_strtable_iter *i) {
  7355. UPB_ASSERT(!upb_strtable_done(i));
  7356. return upb_tabstr(str_tabent(i)->key, NULL);
  7357. }
  7358. size_t upb_strtable_iter_keylength(const upb_strtable_iter *i) {
  7359. uint32_t len;
  7360. UPB_ASSERT(!upb_strtable_done(i));
  7361. upb_tabstr(str_tabent(i)->key, &len);
  7362. return len;
  7363. }
  7364. upb_value upb_strtable_iter_value(const upb_strtable_iter *i) {
  7365. UPB_ASSERT(!upb_strtable_done(i));
  7366. return _upb_value_val(str_tabent(i)->val.val, i->t->t.ctype);
  7367. }
  7368. void upb_strtable_iter_setdone(upb_strtable_iter *i) {
  7369. i->index = SIZE_MAX;
  7370. }
  7371. bool upb_strtable_iter_isequal(const upb_strtable_iter *i1,
  7372. const upb_strtable_iter *i2) {
  7373. if (upb_strtable_done(i1) && upb_strtable_done(i2))
  7374. return true;
  7375. return i1->t == i2->t && i1->index == i2->index;
  7376. }
  7377. /* upb_inttable ***************************************************************/
  7378. /* For inttables we use a hybrid structure where small keys are kept in an
  7379. * array and large keys are put in the hash table. */
  7380. static uint32_t inthash(upb_tabkey key) { return upb_inthash(key); }
  7381. static bool inteql(upb_tabkey k1, lookupkey_t k2) {
  7382. return k1 == k2.num;
  7383. }
  7384. static upb_tabval *mutable_array(upb_inttable *t) {
  7385. return (upb_tabval*)t->array;
  7386. }
  7387. static upb_tabval *inttable_val(upb_inttable *t, uintptr_t key) {
  7388. if (key < t->array_size) {
  7389. return upb_arrhas(t->array[key]) ? &(mutable_array(t)[key]) : NULL;
  7390. } else {
  7391. upb_tabent *e =
  7392. findentry_mutable(&t->t, intkey(key), upb_inthash(key), &inteql);
  7393. return e ? &e->val : NULL;
  7394. }
  7395. }
  7396. static const upb_tabval *inttable_val_const(const upb_inttable *t,
  7397. uintptr_t key) {
  7398. return inttable_val((upb_inttable*)t, key);
  7399. }
  7400. size_t upb_inttable_count(const upb_inttable *t) {
  7401. return t->t.count + t->array_count;
  7402. }
  7403. static void check(upb_inttable *t) {
  7404. UPB_UNUSED(t);
  7405. #if defined(UPB_DEBUG_TABLE) && !defined(NDEBUG)
  7406. {
  7407. /* This check is very expensive (makes inserts/deletes O(N)). */
  7408. size_t count = 0;
  7409. upb_inttable_iter i;
  7410. upb_inttable_begin(&i, t);
  7411. for(; !upb_inttable_done(&i); upb_inttable_next(&i), count++) {
  7412. UPB_ASSERT(upb_inttable_lookup(t, upb_inttable_iter_key(&i), NULL));
  7413. }
  7414. UPB_ASSERT(count == upb_inttable_count(t));
  7415. }
  7416. #endif
  7417. }
  7418. bool upb_inttable_sizedinit(upb_inttable *t, upb_ctype_t ctype,
  7419. size_t asize, int hsize_lg2, upb_alloc *a) {
  7420. size_t array_bytes;
  7421. if (!init(&t->t, ctype, hsize_lg2, a)) return false;
  7422. /* Always make the array part at least 1 long, so that we know key 0
  7423. * won't be in the hash part, which simplifies things. */
  7424. t->array_size = UPB_MAX(1, asize);
  7425. t->array_count = 0;
  7426. array_bytes = t->array_size * sizeof(upb_value);
  7427. t->array = upb_malloc(a, array_bytes);
  7428. if (!t->array) {
  7429. uninit(&t->t, a);
  7430. return false;
  7431. }
  7432. memset(mutable_array(t), 0xff, array_bytes);
  7433. check(t);
  7434. return true;
  7435. }
  7436. bool upb_inttable_init2(upb_inttable *t, upb_ctype_t ctype, upb_alloc *a) {
  7437. return upb_inttable_sizedinit(t, ctype, 0, 4, a);
  7438. }
  7439. void upb_inttable_uninit2(upb_inttable *t, upb_alloc *a) {
  7440. uninit(&t->t, a);
  7441. upb_free(a, mutable_array(t));
  7442. }
  7443. bool upb_inttable_insert2(upb_inttable *t, uintptr_t key, upb_value val,
  7444. upb_alloc *a) {
  7445. upb_tabval tabval;
  7446. tabval.val = val.val;
  7447. UPB_ASSERT(upb_arrhas(tabval)); /* This will reject (uint64_t)-1. Fix this. */
  7448. upb_check_alloc(&t->t, a);
  7449. if (key < t->array_size) {
  7450. UPB_ASSERT(!upb_arrhas(t->array[key]));
  7451. t->array_count++;
  7452. mutable_array(t)[key].val = val.val;
  7453. } else {
  7454. if (isfull(&t->t)) {
  7455. /* Need to resize the hash part, but we re-use the array part. */
  7456. size_t i;
  7457. upb_table new_table;
  7458. if (!init(&new_table, t->t.ctype, t->t.size_lg2 + 1, a)) {
  7459. return false;
  7460. }
  7461. for (i = begin(&t->t); i < upb_table_size(&t->t); i = next(&t->t, i)) {
  7462. const upb_tabent *e = &t->t.entries[i];
  7463. uint32_t hash;
  7464. upb_value v;
  7465. _upb_value_setval(&v, e->val.val, t->t.ctype);
  7466. hash = upb_inthash(e->key);
  7467. insert(&new_table, intkey(e->key), e->key, v, hash, &inthash, &inteql);
  7468. }
  7469. UPB_ASSERT(t->t.count == new_table.count);
  7470. uninit(&t->t, a);
  7471. t->t = new_table;
  7472. }
  7473. insert(&t->t, intkey(key), key, val, upb_inthash(key), &inthash, &inteql);
  7474. }
  7475. check(t);
  7476. return true;
  7477. }
  7478. bool upb_inttable_lookup(const upb_inttable *t, uintptr_t key, upb_value *v) {
  7479. const upb_tabval *table_v = inttable_val_const(t, key);
  7480. if (!table_v) return false;
  7481. if (v) _upb_value_setval(v, table_v->val, t->t.ctype);
  7482. return true;
  7483. }
  7484. bool upb_inttable_replace(upb_inttable *t, uintptr_t key, upb_value val) {
  7485. upb_tabval *table_v = inttable_val(t, key);
  7486. if (!table_v) return false;
  7487. table_v->val = val.val;
  7488. return true;
  7489. }
  7490. bool upb_inttable_remove(upb_inttable *t, uintptr_t key, upb_value *val) {
  7491. bool success;
  7492. if (key < t->array_size) {
  7493. if (upb_arrhas(t->array[key])) {
  7494. upb_tabval empty = UPB_TABVALUE_EMPTY_INIT;
  7495. t->array_count--;
  7496. if (val) {
  7497. _upb_value_setval(val, t->array[key].val, t->t.ctype);
  7498. }
  7499. mutable_array(t)[key] = empty;
  7500. success = true;
  7501. } else {
  7502. success = false;
  7503. }
  7504. } else {
  7505. success = rm(&t->t, intkey(key), val, NULL, upb_inthash(key), &inteql);
  7506. }
  7507. check(t);
  7508. return success;
  7509. }
  7510. bool upb_inttable_push2(upb_inttable *t, upb_value val, upb_alloc *a) {
  7511. upb_check_alloc(&t->t, a);
  7512. return upb_inttable_insert2(t, upb_inttable_count(t), val, a);
  7513. }
  7514. upb_value upb_inttable_pop(upb_inttable *t) {
  7515. upb_value val;
  7516. bool ok = upb_inttable_remove(t, upb_inttable_count(t) - 1, &val);
  7517. UPB_ASSERT(ok);
  7518. return val;
  7519. }
  7520. bool upb_inttable_insertptr2(upb_inttable *t, const void *key, upb_value val,
  7521. upb_alloc *a) {
  7522. upb_check_alloc(&t->t, a);
  7523. return upb_inttable_insert2(t, (uintptr_t)key, val, a);
  7524. }
  7525. bool upb_inttable_lookupptr(const upb_inttable *t, const void *key,
  7526. upb_value *v) {
  7527. return upb_inttable_lookup(t, (uintptr_t)key, v);
  7528. }
  7529. bool upb_inttable_removeptr(upb_inttable *t, const void *key, upb_value *val) {
  7530. return upb_inttable_remove(t, (uintptr_t)key, val);
  7531. }
  7532. void upb_inttable_compact2(upb_inttable *t, upb_alloc *a) {
  7533. /* A power-of-two histogram of the table keys. */
  7534. size_t counts[UPB_MAXARRSIZE + 1] = {0};
  7535. /* The max key in each bucket. */
  7536. uintptr_t max[UPB_MAXARRSIZE + 1] = {0};
  7537. upb_inttable_iter i;
  7538. size_t arr_count;
  7539. int size_lg2;
  7540. upb_inttable new_t;
  7541. upb_check_alloc(&t->t, a);
  7542. upb_inttable_begin(&i, t);
  7543. for (; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  7544. uintptr_t key = upb_inttable_iter_key(&i);
  7545. int bucket = log2ceil(key);
  7546. max[bucket] = UPB_MAX(max[bucket], key);
  7547. counts[bucket]++;
  7548. }
  7549. /* Find the largest power of two that satisfies the MIN_DENSITY
  7550. * definition (while actually having some keys). */
  7551. arr_count = upb_inttable_count(t);
  7552. for (size_lg2 = ARRAY_SIZE(counts) - 1; size_lg2 > 0; size_lg2--) {
  7553. if (counts[size_lg2] == 0) {
  7554. /* We can halve again without losing any entries. */
  7555. continue;
  7556. } else if (arr_count >= (1 << size_lg2) * MIN_DENSITY) {
  7557. break;
  7558. }
  7559. arr_count -= counts[size_lg2];
  7560. }
  7561. UPB_ASSERT(arr_count <= upb_inttable_count(t));
  7562. {
  7563. /* Insert all elements into new, perfectly-sized table. */
  7564. size_t arr_size = max[size_lg2] + 1; /* +1 so arr[max] will fit. */
  7565. size_t hash_count = upb_inttable_count(t) - arr_count;
  7566. size_t hash_size = hash_count ? (hash_count / MAX_LOAD) + 1 : 0;
  7567. size_t hashsize_lg2 = log2ceil(hash_size);
  7568. upb_inttable_sizedinit(&new_t, t->t.ctype, arr_size, hashsize_lg2, a);
  7569. upb_inttable_begin(&i, t);
  7570. for (; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  7571. uintptr_t k = upb_inttable_iter_key(&i);
  7572. upb_inttable_insert2(&new_t, k, upb_inttable_iter_value(&i), a);
  7573. }
  7574. UPB_ASSERT(new_t.array_size == arr_size);
  7575. UPB_ASSERT(new_t.t.size_lg2 == hashsize_lg2);
  7576. }
  7577. upb_inttable_uninit2(t, a);
  7578. *t = new_t;
  7579. }
  7580. /* Iteration. */
  7581. static const upb_tabent *int_tabent(const upb_inttable_iter *i) {
  7582. UPB_ASSERT(!i->array_part);
  7583. return &i->t->t.entries[i->index];
  7584. }
  7585. static upb_tabval int_arrent(const upb_inttable_iter *i) {
  7586. UPB_ASSERT(i->array_part);
  7587. return i->t->array[i->index];
  7588. }
  7589. void upb_inttable_begin(upb_inttable_iter *i, const upb_inttable *t) {
  7590. i->t = t;
  7591. i->index = -1;
  7592. i->array_part = true;
  7593. upb_inttable_next(i);
  7594. }
  7595. void upb_inttable_next(upb_inttable_iter *iter) {
  7596. const upb_inttable *t = iter->t;
  7597. if (iter->array_part) {
  7598. while (++iter->index < t->array_size) {
  7599. if (upb_arrhas(int_arrent(iter))) {
  7600. return;
  7601. }
  7602. }
  7603. iter->array_part = false;
  7604. iter->index = begin(&t->t);
  7605. } else {
  7606. iter->index = next(&t->t, iter->index);
  7607. }
  7608. }
  7609. bool upb_inttable_done(const upb_inttable_iter *i) {
  7610. if (i->array_part) {
  7611. return i->index >= i->t->array_size ||
  7612. !upb_arrhas(int_arrent(i));
  7613. } else {
  7614. return i->index >= upb_table_size(&i->t->t) ||
  7615. upb_tabent_isempty(int_tabent(i));
  7616. }
  7617. }
  7618. uintptr_t upb_inttable_iter_key(const upb_inttable_iter *i) {
  7619. UPB_ASSERT(!upb_inttable_done(i));
  7620. return i->array_part ? i->index : int_tabent(i)->key;
  7621. }
  7622. upb_value upb_inttable_iter_value(const upb_inttable_iter *i) {
  7623. UPB_ASSERT(!upb_inttable_done(i));
  7624. return _upb_value_val(
  7625. i->array_part ? i->t->array[i->index].val : int_tabent(i)->val.val,
  7626. i->t->t.ctype);
  7627. }
  7628. void upb_inttable_iter_setdone(upb_inttable_iter *i) {
  7629. i->index = SIZE_MAX;
  7630. i->array_part = false;
  7631. }
  7632. bool upb_inttable_iter_isequal(const upb_inttable_iter *i1,
  7633. const upb_inttable_iter *i2) {
  7634. if (upb_inttable_done(i1) && upb_inttable_done(i2))
  7635. return true;
  7636. return i1->t == i2->t && i1->index == i2->index &&
  7637. i1->array_part == i2->array_part;
  7638. }
  7639. #ifdef UPB_UNALIGNED_READS_OK
  7640. /* -----------------------------------------------------------------------------
  7641. * MurmurHash2, by Austin Appleby (released as public domain).
  7642. * Reformatted and C99-ified by Joshua Haberman.
  7643. * Note - This code makes a few assumptions about how your machine behaves -
  7644. * 1. We can read a 4-byte value from any address without crashing
  7645. * 2. sizeof(int) == 4 (in upb this limitation is removed by using uint32_t
  7646. * And it has a few limitations -
  7647. * 1. It will not work incrementally.
  7648. * 2. It will not produce the same results on little-endian and big-endian
  7649. * machines. */
  7650. uint32_t MurmurHash2(const void *key, size_t len, uint32_t seed) {
  7651. /* 'm' and 'r' are mixing constants generated offline.
  7652. * They're not really 'magic', they just happen to work well. */
  7653. const uint32_t m = 0x5bd1e995;
  7654. const int32_t r = 24;
  7655. /* Initialize the hash to a 'random' value */
  7656. uint32_t h = seed ^ len;
  7657. /* Mix 4 bytes at a time into the hash */
  7658. const uint8_t * data = (const uint8_t *)key;
  7659. while(len >= 4) {
  7660. uint32_t k = *(uint32_t *)data;
  7661. k *= m;
  7662. k ^= k >> r;
  7663. k *= m;
  7664. h *= m;
  7665. h ^= k;
  7666. data += 4;
  7667. len -= 4;
  7668. }
  7669. /* Handle the last few bytes of the input array */
  7670. switch(len) {
  7671. case 3: h ^= data[2] << 16;
  7672. case 2: h ^= data[1] << 8;
  7673. case 1: h ^= data[0]; h *= m;
  7674. };
  7675. /* Do a few final mixes of the hash to ensure the last few
  7676. * bytes are well-incorporated. */
  7677. h ^= h >> 13;
  7678. h *= m;
  7679. h ^= h >> 15;
  7680. return h;
  7681. }
  7682. #else /* !UPB_UNALIGNED_READS_OK */
  7683. /* -----------------------------------------------------------------------------
  7684. * MurmurHashAligned2, by Austin Appleby
  7685. * Same algorithm as MurmurHash2, but only does aligned reads - should be safer
  7686. * on certain platforms.
  7687. * Performance will be lower than MurmurHash2 */
  7688. #define MIX(h,k,m) { k *= m; k ^= k >> r; k *= m; h *= m; h ^= k; }
  7689. uint32_t MurmurHash2(const void * key, size_t len, uint32_t seed) {
  7690. const uint32_t m = 0x5bd1e995;
  7691. const int32_t r = 24;
  7692. const uint8_t * data = (const uint8_t *)key;
  7693. uint32_t h = seed ^ len;
  7694. uint8_t align = (uintptr_t)data & 3;
  7695. if(align && (len >= 4)) {
  7696. /* Pre-load the temp registers */
  7697. uint32_t t = 0, d = 0;
  7698. int32_t sl;
  7699. int32_t sr;
  7700. switch(align) {
  7701. case 1: t |= data[2] << 16;
  7702. case 2: t |= data[1] << 8;
  7703. case 3: t |= data[0];
  7704. }
  7705. t <<= (8 * align);
  7706. data += 4-align;
  7707. len -= 4-align;
  7708. sl = 8 * (4-align);
  7709. sr = 8 * align;
  7710. /* Mix */
  7711. while(len >= 4) {
  7712. uint32_t k;
  7713. d = *(uint32_t *)data;
  7714. t = (t >> sr) | (d << sl);
  7715. k = t;
  7716. MIX(h,k,m);
  7717. t = d;
  7718. data += 4;
  7719. len -= 4;
  7720. }
  7721. /* Handle leftover data in temp registers */
  7722. d = 0;
  7723. if(len >= align) {
  7724. uint32_t k;
  7725. switch(align) {
  7726. case 3: d |= data[2] << 16;
  7727. case 2: d |= data[1] << 8;
  7728. case 1: d |= data[0];
  7729. }
  7730. k = (t >> sr) | (d << sl);
  7731. MIX(h,k,m);
  7732. data += align;
  7733. len -= align;
  7734. /* ----------
  7735. * Handle tail bytes */
  7736. switch(len) {
  7737. case 3: h ^= data[2] << 16;
  7738. case 2: h ^= data[1] << 8;
  7739. case 1: h ^= data[0]; h *= m;
  7740. };
  7741. } else {
  7742. switch(len) {
  7743. case 3: d |= data[2] << 16;
  7744. case 2: d |= data[1] << 8;
  7745. case 1: d |= data[0];
  7746. case 0: h ^= (t >> sr) | (d << sl); h *= m;
  7747. }
  7748. }
  7749. h ^= h >> 13;
  7750. h *= m;
  7751. h ^= h >> 15;
  7752. return h;
  7753. } else {
  7754. while(len >= 4) {
  7755. uint32_t k = *(uint32_t *)data;
  7756. MIX(h,k,m);
  7757. data += 4;
  7758. len -= 4;
  7759. }
  7760. /* ----------
  7761. * Handle tail bytes */
  7762. switch(len) {
  7763. case 3: h ^= data[2] << 16;
  7764. case 2: h ^= data[1] << 8;
  7765. case 1: h ^= data[0]; h *= m;
  7766. };
  7767. h ^= h >> 13;
  7768. h *= m;
  7769. h ^= h >> 15;
  7770. return h;
  7771. }
  7772. }
  7773. #undef MIX
  7774. #endif /* UPB_UNALIGNED_READS_OK */
  7775. #include <errno.h>
  7776. #include <stdarg.h>
  7777. #include <stddef.h>
  7778. #include <stdint.h>
  7779. #include <stdio.h>
  7780. #include <stdlib.h>
  7781. #include <string.h>
  7782. bool upb_dumptostderr(void *closure, const upb_status* status) {
  7783. UPB_UNUSED(closure);
  7784. fprintf(stderr, "%s\n", upb_status_errmsg(status));
  7785. return false;
  7786. }
  7787. /* Guarantee null-termination and provide ellipsis truncation.
  7788. * It may be tempting to "optimize" this by initializing these final
  7789. * four bytes up-front and then being careful never to overwrite them,
  7790. * this is safer and simpler. */
  7791. static void nullz(upb_status *status) {
  7792. const char *ellipsis = "...";
  7793. size_t len = strlen(ellipsis);
  7794. UPB_ASSERT(sizeof(status->msg) > len);
  7795. memcpy(status->msg + sizeof(status->msg) - len, ellipsis, len);
  7796. }
  7797. /* upb_upberr *****************************************************************/
  7798. upb_errorspace upb_upberr = {"upb error"};
  7799. void upb_upberr_setoom(upb_status *status) {
  7800. status->error_space_ = &upb_upberr;
  7801. upb_status_seterrmsg(status, "Out of memory");
  7802. }
  7803. /* upb_status *****************************************************************/
  7804. void upb_status_clear(upb_status *status) {
  7805. if (!status) return;
  7806. status->ok_ = true;
  7807. status->code_ = 0;
  7808. status->msg[0] = '\0';
  7809. }
  7810. bool upb_ok(const upb_status *status) { return status->ok_; }
  7811. upb_errorspace *upb_status_errspace(const upb_status *status) {
  7812. return status->error_space_;
  7813. }
  7814. int upb_status_errcode(const upb_status *status) { return status->code_; }
  7815. const char *upb_status_errmsg(const upb_status *status) { return status->msg; }
  7816. void upb_status_seterrmsg(upb_status *status, const char *msg) {
  7817. if (!status) return;
  7818. status->ok_ = false;
  7819. strncpy(status->msg, msg, sizeof(status->msg));
  7820. nullz(status);
  7821. }
  7822. void upb_status_seterrf(upb_status *status, const char *fmt, ...) {
  7823. va_list args;
  7824. va_start(args, fmt);
  7825. upb_status_vseterrf(status, fmt, args);
  7826. va_end(args);
  7827. }
  7828. void upb_status_vseterrf(upb_status *status, const char *fmt, va_list args) {
  7829. if (!status) return;
  7830. status->ok_ = false;
  7831. _upb_vsnprintf(status->msg, sizeof(status->msg), fmt, args);
  7832. nullz(status);
  7833. }
  7834. void upb_status_copy(upb_status *to, const upb_status *from) {
  7835. if (!to) return;
  7836. *to = *from;
  7837. }
  7838. /* upb_alloc ******************************************************************/
  7839. static void *upb_global_allocfunc(upb_alloc *alloc, void *ptr, size_t oldsize,
  7840. size_t size) {
  7841. UPB_UNUSED(alloc);
  7842. UPB_UNUSED(oldsize);
  7843. if (size == 0) {
  7844. free(ptr);
  7845. return NULL;
  7846. } else {
  7847. return realloc(ptr, size);
  7848. }
  7849. }
  7850. upb_alloc upb_alloc_global = {&upb_global_allocfunc};
  7851. /* upb_arena ******************************************************************/
  7852. /* Be conservative and choose 16 in case anyone is using SSE. */
  7853. static const size_t maxalign = 16;
  7854. static size_t align_up_max(size_t size) {
  7855. return ((size + maxalign - 1) / maxalign) * maxalign;
  7856. }
  7857. typedef struct mem_block {
  7858. struct mem_block *next;
  7859. size_t size;
  7860. size_t used;
  7861. bool owned;
  7862. /* Data follows. */
  7863. } mem_block;
  7864. typedef struct cleanup_ent {
  7865. struct cleanup_ent *next;
  7866. upb_cleanup_func *cleanup;
  7867. void *ud;
  7868. } cleanup_ent;
  7869. static void upb_arena_addblock(upb_arena *a, void *ptr, size_t size,
  7870. bool owned) {
  7871. mem_block *block = ptr;
  7872. block->next = a->block_head;
  7873. block->size = size;
  7874. block->used = align_up_max(sizeof(mem_block));
  7875. block->owned = owned;
  7876. a->block_head = block;
  7877. /* TODO(haberman): ASAN poison. */
  7878. }
  7879. static mem_block *upb_arena_allocblock(upb_arena *a, size_t size) {
  7880. size_t block_size = UPB_MAX(size, a->next_block_size) + sizeof(mem_block);
  7881. mem_block *block = upb_malloc(a->block_alloc, block_size);
  7882. if (!block) {
  7883. return NULL;
  7884. }
  7885. upb_arena_addblock(a, block, block_size, true);
  7886. a->next_block_size = UPB_MIN(block_size * 2, a->max_block_size);
  7887. return block;
  7888. }
  7889. static void *upb_arena_doalloc(upb_alloc *alloc, void *ptr, size_t oldsize,
  7890. size_t size) {
  7891. upb_arena *a = (upb_arena*)alloc; /* upb_alloc is initial member. */
  7892. mem_block *block = a->block_head;
  7893. void *ret;
  7894. if (size == 0) {
  7895. return NULL; /* We are an arena, don't need individual frees. */
  7896. }
  7897. size = align_up_max(size);
  7898. /* TODO(haberman): special-case if this is a realloc of the last alloc? */
  7899. if (!block || block->size - block->used < size) {
  7900. /* Slow path: have to allocate a new block. */
  7901. block = upb_arena_allocblock(a, size);
  7902. if (!block) {
  7903. return NULL; /* Out of memory. */
  7904. }
  7905. }
  7906. ret = (char*)block + block->used;
  7907. block->used += size;
  7908. if (oldsize > 0) {
  7909. memcpy(ret, ptr, oldsize); /* Preserve existing data. */
  7910. }
  7911. /* TODO(haberman): ASAN unpoison. */
  7912. a->bytes_allocated += size;
  7913. return ret;
  7914. }
  7915. /* Public Arena API ***********************************************************/
  7916. void upb_arena_init(upb_arena *a) {
  7917. a->alloc.func = &upb_arena_doalloc;
  7918. a->block_alloc = &upb_alloc_global;
  7919. a->bytes_allocated = 0;
  7920. a->next_block_size = 256;
  7921. a->max_block_size = 16384;
  7922. a->cleanup_head = NULL;
  7923. a->block_head = NULL;
  7924. }
  7925. void upb_arena_init2(upb_arena *a, void *mem, size_t size, upb_alloc *alloc) {
  7926. upb_arena_init(a);
  7927. if (size > sizeof(mem_block)) {
  7928. upb_arena_addblock(a, mem, size, false);
  7929. }
  7930. if (alloc) {
  7931. a->block_alloc = alloc;
  7932. }
  7933. }
  7934. void upb_arena_uninit(upb_arena *a) {
  7935. cleanup_ent *ent = a->cleanup_head;
  7936. mem_block *block = a->block_head;
  7937. while (ent) {
  7938. ent->cleanup(ent->ud);
  7939. ent = ent->next;
  7940. }
  7941. /* Must do this after running cleanup functions, because this will delete
  7942. * the memory we store our cleanup entries in! */
  7943. while (block) {
  7944. mem_block *next = block->next;
  7945. if (block->owned) {
  7946. upb_free(a->block_alloc, block);
  7947. }
  7948. block = next;
  7949. }
  7950. /* Protect against multiple-uninit. */
  7951. a->cleanup_head = NULL;
  7952. a->block_head = NULL;
  7953. }
  7954. bool upb_arena_addcleanup(upb_arena *a, upb_cleanup_func *func, void *ud) {
  7955. cleanup_ent *ent = upb_malloc(&a->alloc, sizeof(cleanup_ent));
  7956. if (!ent) {
  7957. return false; /* Out of memory. */
  7958. }
  7959. ent->cleanup = func;
  7960. ent->ud = ud;
  7961. ent->next = a->cleanup_head;
  7962. a->cleanup_head = ent;
  7963. return true;
  7964. }
  7965. size_t upb_arena_bytesallocated(const upb_arena *a) {
  7966. return a->bytes_allocated;
  7967. }
  7968. /* Standard error functions ***************************************************/
  7969. static bool default_err(void *ud, const upb_status *status) {
  7970. UPB_UNUSED(ud);
  7971. UPB_UNUSED(status);
  7972. return false;
  7973. }
  7974. static bool write_err_to(void *ud, const upb_status *status) {
  7975. upb_status *copy_to = ud;
  7976. upb_status_copy(copy_to, status);
  7977. return false;
  7978. }
  7979. /* upb_env ********************************************************************/
  7980. void upb_env_initonly(upb_env *e) {
  7981. e->ok_ = true;
  7982. e->error_func_ = &default_err;
  7983. e->error_ud_ = NULL;
  7984. }
  7985. void upb_env_init(upb_env *e) {
  7986. upb_arena_init(&e->arena_);
  7987. upb_env_initonly(e);
  7988. }
  7989. void upb_env_init2(upb_env *e, void *mem, size_t n, upb_alloc *alloc) {
  7990. upb_arena_init2(&e->arena_, mem, n, alloc);
  7991. upb_env_initonly(e);
  7992. }
  7993. void upb_env_uninit(upb_env *e) {
  7994. upb_arena_uninit(&e->arena_);
  7995. }
  7996. void upb_env_seterrorfunc(upb_env *e, upb_error_func *func, void *ud) {
  7997. e->error_func_ = func;
  7998. e->error_ud_ = ud;
  7999. }
  8000. void upb_env_reporterrorsto(upb_env *e, upb_status *s) {
  8001. e->error_func_ = &write_err_to;
  8002. e->error_ud_ = s;
  8003. }
  8004. bool upb_env_reporterror(upb_env *e, const upb_status *status) {
  8005. e->ok_ = false;
  8006. return e->error_func_(e->error_ud_, status);
  8007. }
  8008. void *upb_env_malloc(upb_env *e, size_t size) {
  8009. return upb_malloc(&e->arena_.alloc, size);
  8010. }
  8011. void *upb_env_realloc(upb_env *e, void *ptr, size_t oldsize, size_t size) {
  8012. return upb_realloc(&e->arena_.alloc, ptr, oldsize, size);
  8013. }
  8014. void upb_env_free(upb_env *e, void *ptr) {
  8015. upb_free(&e->arena_.alloc, ptr);
  8016. }
  8017. bool upb_env_addcleanup(upb_env *e, upb_cleanup_func *func, void *ud) {
  8018. return upb_arena_addcleanup(&e->arena_, func, ud);
  8019. }
  8020. size_t upb_env_bytesallocated(const upb_env *e) {
  8021. return upb_arena_bytesallocated(&e->arena_);
  8022. }
  8023. /* This file was generated by upbc (the upb compiler) from the input
  8024. * file:
  8025. *
  8026. * upb/descriptor/descriptor.proto
  8027. *
  8028. * Do not edit -- your changes will be discarded when the file is
  8029. * regenerated. */
  8030. static const upb_msgdef msgs[22];
  8031. static const upb_fielddef fields[107];
  8032. static const upb_enumdef enums[5];
  8033. static const upb_tabent strentries[236];
  8034. static const upb_tabent intentries[18];
  8035. static const upb_tabval arrays[187];
  8036. #ifdef UPB_DEBUG_REFS
  8037. static upb_inttable reftables[268];
  8038. #endif
  8039. static const upb_msgdef msgs[22] = {
  8040. UPB_MSGDEF_INIT("google.protobuf.DescriptorProto", 41, 8, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[0], 11, 10), UPB_STRTABLE_INIT(10, 15, UPB_CTYPE_PTR, 4, &strentries[0]), false, UPB_SYNTAX_PROTO2, &reftables[0], &reftables[1]),
  8041. UPB_MSGDEF_INIT("google.protobuf.DescriptorProto.ExtensionRange", 5, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[11], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[16]), false, UPB_SYNTAX_PROTO2, &reftables[2], &reftables[3]),
  8042. UPB_MSGDEF_INIT("google.protobuf.DescriptorProto.ReservedRange", 5, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[14], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[20]), false, UPB_SYNTAX_PROTO2, &reftables[4], &reftables[5]),
  8043. UPB_MSGDEF_INIT("google.protobuf.EnumDescriptorProto", 12, 2, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[17], 4, 3), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[24]), false, UPB_SYNTAX_PROTO2, &reftables[6], &reftables[7]),
  8044. UPB_MSGDEF_INIT("google.protobuf.EnumOptions", 9, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[0], &arrays[21], 4, 2), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[28]), false, UPB_SYNTAX_PROTO2, &reftables[8], &reftables[9]),
  8045. UPB_MSGDEF_INIT("google.protobuf.EnumValueDescriptorProto", 9, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[25], 4, 3), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[32]), false, UPB_SYNTAX_PROTO2, &reftables[10], &reftables[11]),
  8046. UPB_MSGDEF_INIT("google.protobuf.EnumValueOptions", 8, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[2], &arrays[29], 2, 1), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[36]), false, UPB_SYNTAX_PROTO2, &reftables[12], &reftables[13]),
  8047. UPB_MSGDEF_INIT("google.protobuf.FieldDescriptorProto", 24, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[31], 11, 10), UPB_STRTABLE_INIT(10, 15, UPB_CTYPE_PTR, 4, &strentries[40]), false, UPB_SYNTAX_PROTO2, &reftables[14], &reftables[15]),
  8048. UPB_MSGDEF_INIT("google.protobuf.FieldOptions", 13, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[4], &arrays[42], 11, 6), UPB_STRTABLE_INIT(7, 15, UPB_CTYPE_PTR, 4, &strentries[56]), false, UPB_SYNTAX_PROTO2, &reftables[16], &reftables[17]),
  8049. UPB_MSGDEF_INIT("google.protobuf.FileDescriptorProto", 43, 6, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[53], 13, 12), UPB_STRTABLE_INIT(12, 15, UPB_CTYPE_PTR, 4, &strentries[72]), false, UPB_SYNTAX_PROTO2, &reftables[18], &reftables[19]),
  8050. UPB_MSGDEF_INIT("google.protobuf.FileDescriptorSet", 7, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[66], 2, 1), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[88]), false, UPB_SYNTAX_PROTO2, &reftables[20], &reftables[21]),
  8051. UPB_MSGDEF_INIT("google.protobuf.FileOptions", 38, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[6], &arrays[68], 42, 17), UPB_STRTABLE_INIT(18, 31, UPB_CTYPE_PTR, 5, &strentries[92]), false, UPB_SYNTAX_PROTO2, &reftables[22], &reftables[23]),
  8052. UPB_MSGDEF_INIT("google.protobuf.MessageOptions", 11, 1, UPB_INTTABLE_INIT(1, 1, UPB_CTYPE_PTR, 1, &intentries[8], &arrays[110], 8, 4), UPB_STRTABLE_INIT(5, 7, UPB_CTYPE_PTR, 3, &strentries[124]), false, UPB_SYNTAX_PROTO2, &reftables[24], &reftables[25]),
  8053. UPB_MSGDEF_INIT("google.protobuf.MethodDescriptorProto", 16, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[118], 7, 6), UPB_STRTABLE_INIT(6, 7, UPB_CTYPE_PTR, 3, &strentries[132]), false, UPB_SYNTAX_PROTO2, &reftables[26], &reftables[27]),
  8054. UPB_MSGDEF_INIT("google.protobuf.MethodOptions", 8, 1, UPB_INTTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &intentries[10], &arrays[125], 1, 0), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[140]), false, UPB_SYNTAX_PROTO2, &reftables[28], &reftables[29]),
  8055. UPB_MSGDEF_INIT("google.protobuf.OneofDescriptorProto", 6, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[126], 2, 1), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[144]), false, UPB_SYNTAX_PROTO2, &reftables[30], &reftables[31]),
  8056. UPB_MSGDEF_INIT("google.protobuf.ServiceDescriptorProto", 12, 2, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[128], 4, 3), UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_PTR, 2, &strentries[148]), false, UPB_SYNTAX_PROTO2, &reftables[32], &reftables[33]),
  8057. UPB_MSGDEF_INIT("google.protobuf.ServiceOptions", 8, 1, UPB_INTTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &intentries[14], &arrays[132], 1, 0), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[152]), false, UPB_SYNTAX_PROTO2, &reftables[34], &reftables[35]),
  8058. UPB_MSGDEF_INIT("google.protobuf.SourceCodeInfo", 7, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[133], 2, 1), UPB_STRTABLE_INIT(1, 3, UPB_CTYPE_PTR, 2, &strentries[156]), false, UPB_SYNTAX_PROTO2, &reftables[36], &reftables[37]),
  8059. UPB_MSGDEF_INIT("google.protobuf.SourceCodeInfo.Location", 20, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[135], 7, 5), UPB_STRTABLE_INIT(5, 7, UPB_CTYPE_PTR, 3, &strentries[160]), false, UPB_SYNTAX_PROTO2, &reftables[38], &reftables[39]),
  8060. UPB_MSGDEF_INIT("google.protobuf.UninterpretedOption", 19, 1, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[142], 9, 7), UPB_STRTABLE_INIT(7, 15, UPB_CTYPE_PTR, 4, &strentries[168]), false, UPB_SYNTAX_PROTO2, &reftables[40], &reftables[41]),
  8061. UPB_MSGDEF_INIT("google.protobuf.UninterpretedOption.NamePart", 7, 0, UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_PTR, 0, NULL, &arrays[151], 3, 2), UPB_STRTABLE_INIT(2, 3, UPB_CTYPE_PTR, 2, &strentries[184]), false, UPB_SYNTAX_PROTO2, &reftables[42], &reftables[43]),
  8062. };
  8063. static const upb_fielddef fields[107] = {
  8064. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "aggregate_value", 8, &msgs[20], NULL, 16, 6, {0},&reftables[44], &reftables[45]),
  8065. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "allow_alias", 2, &msgs[4], NULL, 7, 1, {0},&reftables[46], &reftables[47]),
  8066. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "cc_enable_arenas", 31, &msgs[11], NULL, 24, 12, {0},&reftables[48], &reftables[49]),
  8067. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "cc_generic_services", 16, &msgs[11], NULL, 18, 6, {0},&reftables[50], &reftables[51]),
  8068. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "client_streaming", 5, &msgs[13], NULL, 14, 4, {0},&reftables[52], &reftables[53]),
  8069. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "csharp_namespace", 37, &msgs[11], NULL, 28, 14, {0},&reftables[54], &reftables[55]),
  8070. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "ctype", 1, &msgs[8], (const upb_def*)(&enums[2]), 7, 1, {0},&reftables[56], &reftables[57]),
  8071. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "default_value", 7, &msgs[7], NULL, 17, 7, {0},&reftables[58], &reftables[59]),
  8072. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_STRING, 0, false, false, false, false, "dependency", 3, &msgs[9], NULL, 31, 8, {0},&reftables[60], &reftables[61]),
  8073. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 3, &msgs[8], NULL, 9, 3, {0},&reftables[62], &reftables[63]),
  8074. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 33, &msgs[14], NULL, 7, 1, {0},&reftables[64], &reftables[65]),
  8075. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 3, &msgs[12], NULL, 9, 3, {0},&reftables[66], &reftables[67]),
  8076. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 23, &msgs[11], NULL, 22, 10, {0},&reftables[68], &reftables[69]),
  8077. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 1, &msgs[6], NULL, 7, 1, {0},&reftables[70], &reftables[71]),
  8078. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 3, &msgs[4], NULL, 8, 2, {0},&reftables[72], &reftables[73]),
  8079. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "deprecated", 33, &msgs[17], NULL, 7, 1, {0},&reftables[74], &reftables[75]),
  8080. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_DOUBLE, 0, false, false, false, false, "double_value", 6, &msgs[20], NULL, 12, 4, {0},&reftables[76], &reftables[77]),
  8081. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "end", 2, &msgs[2], NULL, 4, 1, {0},&reftables[78], &reftables[79]),
  8082. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "end", 2, &msgs[1], NULL, 4, 1, {0},&reftables[80], &reftables[81]),
  8083. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "enum_type", 5, &msgs[9], (const upb_def*)(&msgs[3]), 14, 1, {0},&reftables[82], &reftables[83]),
  8084. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "enum_type", 4, &msgs[0], (const upb_def*)(&msgs[3]), 19, 2, {0},&reftables[84], &reftables[85]),
  8085. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "extendee", 2, &msgs[7], NULL, 8, 2, {0},&reftables[86], &reftables[87]),
  8086. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "extension", 6, &msgs[0], (const upb_def*)(&msgs[7]), 25, 4, {0},&reftables[88], &reftables[89]),
  8087. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "extension", 7, &msgs[9], (const upb_def*)(&msgs[7]), 20, 3, {0},&reftables[90], &reftables[91]),
  8088. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "extension_range", 5, &msgs[0], (const upb_def*)(&msgs[1]), 22, 3, {0},&reftables[92], &reftables[93]),
  8089. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "field", 2, &msgs[0], (const upb_def*)(&msgs[7]), 13, 0, {0},&reftables[94], &reftables[95]),
  8090. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "file", 1, &msgs[10], (const upb_def*)(&msgs[9]), 6, 0, {0},&reftables[96], &reftables[97]),
  8091. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "go_package", 11, &msgs[11], NULL, 15, 5, {0},&reftables[98], &reftables[99]),
  8092. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "identifier_value", 3, &msgs[20], NULL, 7, 1, {0},&reftables[100], &reftables[101]),
  8093. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "input_type", 2, &msgs[13], NULL, 8, 2, {0},&reftables[102], &reftables[103]),
  8094. UPB_FIELDDEF_INIT(UPB_LABEL_REQUIRED, UPB_TYPE_BOOL, 0, false, false, false, false, "is_extension", 2, &msgs[21], NULL, 6, 1, {0},&reftables[104], &reftables[105]),
  8095. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "java_generate_equals_and_hash", 20, &msgs[11], NULL, 21, 9, {0},&reftables[106], &reftables[107]),
  8096. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "java_generic_services", 17, &msgs[11], NULL, 19, 7, {0},&reftables[108], &reftables[109]),
  8097. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "java_multiple_files", 10, &msgs[11], NULL, 14, 4, {0},&reftables[110], &reftables[111]),
  8098. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "java_outer_classname", 8, &msgs[11], NULL, 10, 2, {0},&reftables[112], &reftables[113]),
  8099. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "java_package", 1, &msgs[11], NULL, 7, 1, {0},&reftables[114], &reftables[115]),
  8100. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "java_string_check_utf8", 27, &msgs[11], NULL, 23, 11, {0},&reftables[116], &reftables[117]),
  8101. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "javanano_use_deprecated_package", 38, &msgs[11], NULL, 31, 15, {0},&reftables[118], &reftables[119]),
  8102. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "json_name", 10, &msgs[7], NULL, 21, 9, {0},&reftables[120], &reftables[121]),
  8103. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "jstype", 6, &msgs[8], (const upb_def*)(&enums[3]), 11, 5, {0},&reftables[122], &reftables[123]),
  8104. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "label", 4, &msgs[7], (const upb_def*)(&enums[0]), 12, 4, {0},&reftables[124], &reftables[125]),
  8105. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "lazy", 5, &msgs[8], NULL, 10, 4, {0},&reftables[126], &reftables[127]),
  8106. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "leading_comments", 3, &msgs[19], NULL, 9, 2, {0},&reftables[128], &reftables[129]),
  8107. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_STRING, 0, false, false, false, false, "leading_detached_comments", 6, &msgs[19], NULL, 17, 4, {0},&reftables[130], &reftables[131]),
  8108. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "location", 1, &msgs[18], (const upb_def*)(&msgs[19]), 6, 0, {0},&reftables[132], &reftables[133]),
  8109. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "map_entry", 7, &msgs[12], NULL, 10, 4, {0},&reftables[134], &reftables[135]),
  8110. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "message_set_wire_format", 1, &msgs[12], NULL, 7, 1, {0},&reftables[136], &reftables[137]),
  8111. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "message_type", 4, &msgs[9], (const upb_def*)(&msgs[0]), 11, 0, {0},&reftables[138], &reftables[139]),
  8112. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "method", 2, &msgs[16], (const upb_def*)(&msgs[13]), 7, 0, {0},&reftables[140], &reftables[141]),
  8113. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "name", 2, &msgs[20], (const upb_def*)(&msgs[21]), 6, 0, {0},&reftables[142], &reftables[143]),
  8114. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[5], NULL, 5, 1, {0},&reftables[144], &reftables[145]),
  8115. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[9], NULL, 23, 6, {0},&reftables[146], &reftables[147]),
  8116. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[3], NULL, 9, 2, {0},&reftables[148], &reftables[149]),
  8117. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[16], NULL, 9, 2, {0},&reftables[150], &reftables[151]),
  8118. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[15], NULL, 3, 0, {0},&reftables[152], &reftables[153]),
  8119. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[13], NULL, 5, 1, {0},&reftables[154], &reftables[155]),
  8120. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[7], NULL, 5, 1, {0},&reftables[156], &reftables[157]),
  8121. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "name", 1, &msgs[0], NULL, 33, 8, {0},&reftables[158], &reftables[159]),
  8122. UPB_FIELDDEF_INIT(UPB_LABEL_REQUIRED, UPB_TYPE_STRING, 0, false, false, false, false, "name_part", 1, &msgs[21], NULL, 3, 0, {0},&reftables[160], &reftables[161]),
  8123. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT64, UPB_INTFMT_VARIABLE, false, false, false, false, "negative_int_value", 5, &msgs[20], NULL, 11, 3, {0},&reftables[162], &reftables[163]),
  8124. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "nested_type", 3, &msgs[0], (const upb_def*)(&msgs[0]), 16, 1, {0},&reftables[164], &reftables[165]),
  8125. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "no_standard_descriptor_accessor", 2, &msgs[12], NULL, 8, 2, {0},&reftables[166], &reftables[167]),
  8126. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "number", 3, &msgs[7], NULL, 11, 3, {0},&reftables[168], &reftables[169]),
  8127. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "number", 2, &msgs[5], NULL, 8, 2, {0},&reftables[170], &reftables[171]),
  8128. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "objc_class_prefix", 36, &msgs[11], NULL, 25, 13, {0},&reftables[172], &reftables[173]),
  8129. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "oneof_decl", 8, &msgs[0], (const upb_def*)(&msgs[15]), 29, 6, {0},&reftables[174], &reftables[175]),
  8130. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "oneof_index", 9, &msgs[7], NULL, 20, 8, {0},&reftables[176], &reftables[177]),
  8131. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "optimize_for", 9, &msgs[11], (const upb_def*)(&enums[4]), 13, 3, {0},&reftables[178], &reftables[179]),
  8132. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 7, &msgs[0], (const upb_def*)(&msgs[12]), 26, 5, {0},&reftables[180], &reftables[181]),
  8133. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 8, &msgs[9], (const upb_def*)(&msgs[11]), 21, 4, {0},&reftables[182], &reftables[183]),
  8134. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 8, &msgs[7], (const upb_def*)(&msgs[8]), 4, 0, {0},&reftables[184], &reftables[185]),
  8135. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 4, &msgs[13], (const upb_def*)(&msgs[14]), 4, 0, {0},&reftables[186], &reftables[187]),
  8136. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 3, &msgs[16], (const upb_def*)(&msgs[17]), 8, 1, {0},&reftables[188], &reftables[189]),
  8137. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 3, &msgs[3], (const upb_def*)(&msgs[4]), 8, 1, {0},&reftables[190], &reftables[191]),
  8138. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "options", 3, &msgs[5], (const upb_def*)(&msgs[6]), 4, 0, {0},&reftables[192], &reftables[193]),
  8139. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "output_type", 3, &msgs[13], NULL, 11, 3, {0},&reftables[194], &reftables[195]),
  8140. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "package", 2, &msgs[9], NULL, 26, 7, {0},&reftables[196], &reftables[197]),
  8141. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "packed", 2, &msgs[8], NULL, 8, 2, {0},&reftables[198], &reftables[199]),
  8142. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, true, "path", 1, &msgs[19], NULL, 5, 0, {0},&reftables[200], &reftables[201]),
  8143. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "php_class_prefix", 40, &msgs[11], NULL, 32, 16, {0},&reftables[202], &reftables[203]),
  8144. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "php_namespace", 41, &msgs[11], NULL, 35, 17, {0},&reftables[204], &reftables[205]),
  8145. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_UINT64, UPB_INTFMT_VARIABLE, false, false, false, false, "positive_int_value", 4, &msgs[20], NULL, 10, 2, {0},&reftables[206], &reftables[207]),
  8146. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "public_dependency", 10, &msgs[9], NULL, 36, 9, {0},&reftables[208], &reftables[209]),
  8147. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "py_generic_services", 18, &msgs[11], NULL, 20, 8, {0},&reftables[210], &reftables[211]),
  8148. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_STRING, 0, false, false, false, false, "reserved_name", 10, &msgs[0], NULL, 38, 9, {0},&reftables[212], &reftables[213]),
  8149. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "reserved_range", 9, &msgs[0], (const upb_def*)(&msgs[2]), 32, 7, {0},&reftables[214], &reftables[215]),
  8150. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "server_streaming", 6, &msgs[13], NULL, 15, 5, {0},&reftables[216], &reftables[217]),
  8151. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "service", 6, &msgs[9], (const upb_def*)(&msgs[16]), 17, 2, {0},&reftables[218], &reftables[219]),
  8152. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_MESSAGE, 0, false, false, false, false, "source_code_info", 9, &msgs[9], (const upb_def*)(&msgs[18]), 22, 5, {0},&reftables[220], &reftables[221]),
  8153. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, true, "span", 2, &msgs[19], NULL, 8, 1, {0},&reftables[222], &reftables[223]),
  8154. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "start", 1, &msgs[2], NULL, 3, 0, {0},&reftables[224], &reftables[225]),
  8155. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "start", 1, &msgs[1], NULL, 3, 0, {0},&reftables[226], &reftables[227]),
  8156. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BYTES, 0, false, false, false, false, "string_value", 7, &msgs[20], NULL, 13, 5, {0},&reftables[228], &reftables[229]),
  8157. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "syntax", 12, &msgs[9], NULL, 40, 11, {0},&reftables[230], &reftables[231]),
  8158. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "trailing_comments", 4, &msgs[19], NULL, 12, 3, {0},&reftables[232], &reftables[233]),
  8159. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_ENUM, 0, false, false, false, false, "type", 5, &msgs[7], (const upb_def*)(&enums[1]), 13, 5, {0},&reftables[234], &reftables[235]),
  8160. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_STRING, 0, false, false, false, false, "type_name", 6, &msgs[7], NULL, 14, 6, {0},&reftables[236], &reftables[237]),
  8161. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[12], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[238], &reftables[239]),
  8162. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[17], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[240], &reftables[241]),
  8163. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[11], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[242], &reftables[243]),
  8164. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[14], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[244], &reftables[245]),
  8165. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[8], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[246], &reftables[247]),
  8166. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[6], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[248], &reftables[249]),
  8167. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "uninterpreted_option", 999, &msgs[4], (const upb_def*)(&msgs[20]), 6, 0, {0},&reftables[250], &reftables[251]),
  8168. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_MESSAGE, 0, false, false, false, false, "value", 2, &msgs[3], (const upb_def*)(&msgs[5]), 7, 0, {0},&reftables[252], &reftables[253]),
  8169. UPB_FIELDDEF_INIT(UPB_LABEL_OPTIONAL, UPB_TYPE_BOOL, 0, false, false, false, false, "weak", 10, &msgs[8], NULL, 12, 6, {0},&reftables[254], &reftables[255]),
  8170. UPB_FIELDDEF_INIT(UPB_LABEL_REPEATED, UPB_TYPE_INT32, UPB_INTFMT_VARIABLE, false, false, false, false, "weak_dependency", 11, &msgs[9], NULL, 39, 10, {0},&reftables[256], &reftables[257]),
  8171. };
  8172. static const upb_enumdef enums[5] = {
  8173. UPB_ENUMDEF_INIT("google.protobuf.FieldDescriptorProto.Label", UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_INT32, 2, &strentries[188]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[154], 4, 3), 0, &reftables[258], &reftables[259]),
  8174. UPB_ENUMDEF_INIT("google.protobuf.FieldDescriptorProto.Type", UPB_STRTABLE_INIT(18, 31, UPB_CTYPE_INT32, 5, &strentries[192]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[158], 19, 18), 0, &reftables[260], &reftables[261]),
  8175. UPB_ENUMDEF_INIT("google.protobuf.FieldOptions.CType", UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_INT32, 2, &strentries[224]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[177], 3, 3), 0, &reftables[262], &reftables[263]),
  8176. UPB_ENUMDEF_INIT("google.protobuf.FieldOptions.JSType", UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_INT32, 2, &strentries[228]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[180], 3, 3), 0, &reftables[264], &reftables[265]),
  8177. UPB_ENUMDEF_INIT("google.protobuf.FileOptions.OptimizeMode", UPB_STRTABLE_INIT(3, 3, UPB_CTYPE_INT32, 2, &strentries[232]), UPB_INTTABLE_INIT(0, 0, UPB_CTYPE_CSTR, 0, NULL, &arrays[183], 4, 3), 0, &reftables[266], &reftables[267]),
  8178. };
  8179. static const upb_tabent strentries[236] = {
  8180. {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "extension"), UPB_TABVALUE_PTR_INIT(&fields[22]), NULL},
  8181. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8182. {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "reserved_name"), UPB_TABVALUE_PTR_INIT(&fields[84]), NULL},
  8183. {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[57]), NULL},
  8184. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8185. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8186. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8187. {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "field"), UPB_TABVALUE_PTR_INIT(&fields[25]), &strentries[12]},
  8188. {UPB_TABKEY_STR("\017", "\000", "\000", "\000", "extension_range"), UPB_TABVALUE_PTR_INIT(&fields[24]), &strentries[14]},
  8189. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8190. {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "nested_type"), UPB_TABVALUE_PTR_INIT(&fields[60]), NULL},
  8191. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8192. {UPB_TABKEY_STR("\016", "\000", "\000", "\000", "reserved_range"), UPB_TABVALUE_PTR_INIT(&fields[85]), NULL},
  8193. {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[68]), NULL},
  8194. {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "oneof_decl"), UPB_TABVALUE_PTR_INIT(&fields[65]), NULL},
  8195. {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "enum_type"), UPB_TABVALUE_PTR_INIT(&fields[20]), &strentries[13]},
  8196. {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "start"), UPB_TABVALUE_PTR_INIT(&fields[91]), NULL},
  8197. {UPB_TABKEY_STR("\003", "\000", "\000", "\000", "end"), UPB_TABVALUE_PTR_INIT(&fields[18]), NULL},
  8198. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8199. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8200. {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "start"), UPB_TABVALUE_PTR_INIT(&fields[90]), NULL},
  8201. {UPB_TABKEY_STR("\003", "\000", "\000", "\000", "end"), UPB_TABVALUE_PTR_INIT(&fields[17]), NULL},
  8202. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8203. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8204. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8205. {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "value"), UPB_TABVALUE_PTR_INIT(&fields[104]), NULL},
  8206. {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[73]), NULL},
  8207. {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[52]), &strentries[26]},
  8208. {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[103]), NULL},
  8209. {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[14]), NULL},
  8210. {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "allow_alias"), UPB_TABVALUE_PTR_INIT(&fields[1]), NULL},
  8211. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8212. {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "number"), UPB_TABVALUE_PTR_INIT(&fields[63]), NULL},
  8213. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8214. {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[74]), NULL},
  8215. {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[50]), &strentries[34]},
  8216. {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[102]), NULL},
  8217. {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[13]), NULL},
  8218. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8219. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8220. {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "oneof_index"), UPB_TABVALUE_PTR_INIT(&fields[66]), NULL},
  8221. {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "label"), UPB_TABVALUE_PTR_INIT(&fields[40]), NULL},
  8222. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8223. {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[56]), NULL},
  8224. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8225. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8226. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8227. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8228. {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "number"), UPB_TABVALUE_PTR_INIT(&fields[62]), &strentries[53]},
  8229. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8230. {UPB_TABKEY_STR("\010", "\000", "\000", "\000", "extendee"), UPB_TABVALUE_PTR_INIT(&fields[21]), NULL},
  8231. {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "type_name"), UPB_TABVALUE_PTR_INIT(&fields[96]), NULL},
  8232. {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "json_name"), UPB_TABVALUE_PTR_INIT(&fields[38]), NULL},
  8233. {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "type"), UPB_TABVALUE_PTR_INIT(&fields[95]), &strentries[50]},
  8234. {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "default_value"), UPB_TABVALUE_PTR_INIT(&fields[7]), NULL},
  8235. {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[70]), NULL},
  8236. {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[101]), NULL},
  8237. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8238. {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "weak"), UPB_TABVALUE_PTR_INIT(&fields[105]), NULL},
  8239. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8240. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8241. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8242. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8243. {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "packed"), UPB_TABVALUE_PTR_INIT(&fields[77]), NULL},
  8244. {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "lazy"), UPB_TABVALUE_PTR_INIT(&fields[41]), NULL},
  8245. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8246. {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "ctype"), UPB_TABVALUE_PTR_INIT(&fields[6]), NULL},
  8247. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8248. {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "jstype"), UPB_TABVALUE_PTR_INIT(&fields[39]), NULL},
  8249. {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[9]), NULL},
  8250. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8251. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8252. {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "extension"), UPB_TABVALUE_PTR_INIT(&fields[23]), NULL},
  8253. {UPB_TABKEY_STR("\017", "\000", "\000", "\000", "weak_dependency"), UPB_TABVALUE_PTR_INIT(&fields[106]), NULL},
  8254. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8255. {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[51]), NULL},
  8256. {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "service"), UPB_TABVALUE_PTR_INIT(&fields[87]), NULL},
  8257. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8258. {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "source_code_info"), UPB_TABVALUE_PTR_INIT(&fields[88]), NULL},
  8259. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8260. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8261. {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "syntax"), UPB_TABVALUE_PTR_INIT(&fields[93]), NULL},
  8262. {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "dependency"), UPB_TABVALUE_PTR_INIT(&fields[8]), NULL},
  8263. {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "message_type"), UPB_TABVALUE_PTR_INIT(&fields[47]), NULL},
  8264. {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "package"), UPB_TABVALUE_PTR_INIT(&fields[76]), NULL},
  8265. {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[69]), &strentries[86]},
  8266. {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "enum_type"), UPB_TABVALUE_PTR_INIT(&fields[19]), NULL},
  8267. {UPB_TABKEY_STR("\021", "\000", "\000", "\000", "public_dependency"), UPB_TABVALUE_PTR_INIT(&fields[82]), &strentries[85]},
  8268. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8269. {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "file"), UPB_TABVALUE_PTR_INIT(&fields[26]), NULL},
  8270. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8271. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8272. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8273. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8274. {UPB_TABKEY_STR("\023", "\000", "\000", "\000", "cc_generic_services"), UPB_TABVALUE_PTR_INIT(&fields[3]), NULL},
  8275. {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "csharp_namespace"), UPB_TABVALUE_PTR_INIT(&fields[5]), &strentries[116]},
  8276. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8277. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8278. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8279. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8280. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8281. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8282. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8283. {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "go_package"), UPB_TABVALUE_PTR_INIT(&fields[27]), NULL},
  8284. {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "java_package"), UPB_TABVALUE_PTR_INIT(&fields[35]), &strentries[120]},
  8285. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8286. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8287. {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "java_outer_classname"), UPB_TABVALUE_PTR_INIT(&fields[34]), NULL},
  8288. {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "php_namespace"), UPB_TABVALUE_PTR_INIT(&fields[80]), &strentries[113]},
  8289. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8290. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8291. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8292. {UPB_TABKEY_STR("\023", "\000", "\000", "\000", "java_multiple_files"), UPB_TABVALUE_PTR_INIT(&fields[33]), &strentries[117]},
  8293. {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[99]), NULL},
  8294. {UPB_TABKEY_STR("\025", "\000", "\000", "\000", "java_generic_services"), UPB_TABVALUE_PTR_INIT(&fields[32]), &strentries[118]},
  8295. {UPB_TABKEY_STR("\035", "\000", "\000", "\000", "java_generate_equals_and_hash"), UPB_TABVALUE_PTR_INIT(&fields[31]), NULL},
  8296. {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "php_class_prefix"), UPB_TABVALUE_PTR_INIT(&fields[79]), NULL},
  8297. {UPB_TABKEY_STR("\037", "\000", "\000", "\000", "javanano_use_deprecated_package"), UPB_TABVALUE_PTR_INIT(&fields[37]), &strentries[123]},
  8298. {UPB_TABKEY_STR("\023", "\000", "\000", "\000", "py_generic_services"), UPB_TABVALUE_PTR_INIT(&fields[83]), NULL},
  8299. {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "optimize_for"), UPB_TABVALUE_PTR_INIT(&fields[67]), NULL},
  8300. {UPB_TABKEY_STR("\026", "\000", "\000", "\000", "java_string_check_utf8"), UPB_TABVALUE_PTR_INIT(&fields[36]), NULL},
  8301. {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[12]), &strentries[119]},
  8302. {UPB_TABKEY_STR("\021", "\000", "\000", "\000", "objc_class_prefix"), UPB_TABVALUE_PTR_INIT(&fields[64]), NULL},
  8303. {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "cc_enable_arenas"), UPB_TABVALUE_PTR_INIT(&fields[2]), NULL},
  8304. {UPB_TABKEY_STR("\027", "\000", "\000", "\000", "message_set_wire_format"), UPB_TABVALUE_PTR_INIT(&fields[46]), &strentries[128]},
  8305. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8306. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8307. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8308. {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[97]), NULL},
  8309. {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[11]), NULL},
  8310. {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "map_entry"), UPB_TABVALUE_PTR_INIT(&fields[45]), NULL},
  8311. {UPB_TABKEY_STR("\037", "\000", "\000", "\000", "no_standard_descriptor_accessor"), UPB_TABVALUE_PTR_INIT(&fields[61]), NULL},
  8312. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8313. {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "client_streaming"), UPB_TABVALUE_PTR_INIT(&fields[4]), NULL},
  8314. {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "server_streaming"), UPB_TABVALUE_PTR_INIT(&fields[86]), NULL},
  8315. {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[55]), NULL},
  8316. {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "input_type"), UPB_TABVALUE_PTR_INIT(&fields[29]), NULL},
  8317. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8318. {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "output_type"), UPB_TABVALUE_PTR_INIT(&fields[75]), NULL},
  8319. {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[71]), NULL},
  8320. {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[100]), NULL},
  8321. {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[10]), NULL},
  8322. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8323. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8324. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8325. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8326. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8327. {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[54]), NULL},
  8328. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8329. {UPB_TABKEY_STR("\007", "\000", "\000", "\000", "options"), UPB_TABVALUE_PTR_INIT(&fields[72]), &strentries[150]},
  8330. {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "method"), UPB_TABVALUE_PTR_INIT(&fields[48]), NULL},
  8331. {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[53]), &strentries[149]},
  8332. {UPB_TABKEY_STR("\024", "\000", "\000", "\000", "uninterpreted_option"), UPB_TABVALUE_PTR_INIT(&fields[98]), NULL},
  8333. {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "deprecated"), UPB_TABVALUE_PTR_INIT(&fields[15]), NULL},
  8334. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8335. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8336. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8337. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8338. {UPB_TABKEY_STR("\010", "\000", "\000", "\000", "location"), UPB_TABVALUE_PTR_INIT(&fields[44]), NULL},
  8339. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8340. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8341. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8342. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8343. {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "span"), UPB_TABVALUE_PTR_INIT(&fields[89]), &strentries[167]},
  8344. {UPB_TABKEY_STR("\031", "\000", "\000", "\000", "leading_detached_comments"), UPB_TABVALUE_PTR_INIT(&fields[43]), &strentries[165]},
  8345. {UPB_TABKEY_STR("\021", "\000", "\000", "\000", "trailing_comments"), UPB_TABVALUE_PTR_INIT(&fields[94]), NULL},
  8346. {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "leading_comments"), UPB_TABVALUE_PTR_INIT(&fields[42]), &strentries[164]},
  8347. {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "path"), UPB_TABVALUE_PTR_INIT(&fields[78]), NULL},
  8348. {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "double_value"), UPB_TABVALUE_PTR_INIT(&fields[16]), NULL},
  8349. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8350. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8351. {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "name"), UPB_TABVALUE_PTR_INIT(&fields[49]), NULL},
  8352. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8353. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8354. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8355. {UPB_TABKEY_STR("\022", "\000", "\000", "\000", "negative_int_value"), UPB_TABVALUE_PTR_INIT(&fields[59]), NULL},
  8356. {UPB_TABKEY_STR("\017", "\000", "\000", "\000", "aggregate_value"), UPB_TABVALUE_PTR_INIT(&fields[0]), NULL},
  8357. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8358. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8359. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8360. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8361. {UPB_TABKEY_STR("\022", "\000", "\000", "\000", "positive_int_value"), UPB_TABVALUE_PTR_INIT(&fields[81]), NULL},
  8362. {UPB_TABKEY_STR("\020", "\000", "\000", "\000", "identifier_value"), UPB_TABVALUE_PTR_INIT(&fields[28]), NULL},
  8363. {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "string_value"), UPB_TABVALUE_PTR_INIT(&fields[92]), &strentries[182]},
  8364. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8365. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8366. {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "is_extension"), UPB_TABVALUE_PTR_INIT(&fields[30]), NULL},
  8367. {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "name_part"), UPB_TABVALUE_PTR_INIT(&fields[58]), NULL},
  8368. {UPB_TABKEY_STR("\016", "\000", "\000", "\000", "LABEL_REQUIRED"), UPB_TABVALUE_INT_INIT(2), &strentries[190]},
  8369. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8370. {UPB_TABKEY_STR("\016", "\000", "\000", "\000", "LABEL_REPEATED"), UPB_TABVALUE_INT_INIT(3), NULL},
  8371. {UPB_TABKEY_STR("\016", "\000", "\000", "\000", "LABEL_OPTIONAL"), UPB_TABVALUE_INT_INIT(1), NULL},
  8372. {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "TYPE_FIXED64"), UPB_TABVALUE_INT_INIT(6), NULL},
  8373. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8374. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8375. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8376. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8377. {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_STRING"), UPB_TABVALUE_INT_INIT(9), NULL},
  8378. {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "TYPE_FLOAT"), UPB_TABVALUE_INT_INIT(2), &strentries[221]},
  8379. {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_DOUBLE"), UPB_TABVALUE_INT_INIT(1), NULL},
  8380. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8381. {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "TYPE_INT32"), UPB_TABVALUE_INT_INIT(5), NULL},
  8382. {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "TYPE_SFIXED32"), UPB_TABVALUE_INT_INIT(15), NULL},
  8383. {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "TYPE_FIXED32"), UPB_TABVALUE_INT_INIT(7), NULL},
  8384. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8385. {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "TYPE_MESSAGE"), UPB_TABVALUE_INT_INIT(11), &strentries[222]},
  8386. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8387. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8388. {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "TYPE_INT64"), UPB_TABVALUE_INT_INIT(3), &strentries[219]},
  8389. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8390. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8391. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8392. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8393. {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "TYPE_ENUM"), UPB_TABVALUE_INT_INIT(14), NULL},
  8394. {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_UINT32"), UPB_TABVALUE_INT_INIT(13), NULL},
  8395. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8396. {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_UINT64"), UPB_TABVALUE_INT_INIT(4), &strentries[218]},
  8397. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8398. {UPB_TABKEY_STR("\015", "\000", "\000", "\000", "TYPE_SFIXED64"), UPB_TABVALUE_INT_INIT(16), NULL},
  8399. {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "TYPE_BYTES"), UPB_TABVALUE_INT_INIT(12), NULL},
  8400. {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_SINT64"), UPB_TABVALUE_INT_INIT(18), NULL},
  8401. {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "TYPE_BOOL"), UPB_TABVALUE_INT_INIT(8), NULL},
  8402. {UPB_TABKEY_STR("\012", "\000", "\000", "\000", "TYPE_GROUP"), UPB_TABVALUE_INT_INIT(10), NULL},
  8403. {UPB_TABKEY_STR("\013", "\000", "\000", "\000", "TYPE_SINT32"), UPB_TABVALUE_INT_INIT(17), NULL},
  8404. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8405. {UPB_TABKEY_STR("\004", "\000", "\000", "\000", "CORD"), UPB_TABVALUE_INT_INIT(1), NULL},
  8406. {UPB_TABKEY_STR("\006", "\000", "\000", "\000", "STRING"), UPB_TABVALUE_INT_INIT(0), &strentries[225]},
  8407. {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "STRING_PIECE"), UPB_TABVALUE_INT_INIT(2), NULL},
  8408. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8409. {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "JS_NORMAL"), UPB_TABVALUE_INT_INIT(0), NULL},
  8410. {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "JS_NUMBER"), UPB_TABVALUE_INT_INIT(2), NULL},
  8411. {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "JS_STRING"), UPB_TABVALUE_INT_INIT(1), NULL},
  8412. {UPB_TABKEY_STR("\011", "\000", "\000", "\000", "CODE_SIZE"), UPB_TABVALUE_INT_INIT(2), NULL},
  8413. {UPB_TABKEY_STR("\005", "\000", "\000", "\000", "SPEED"), UPB_TABVALUE_INT_INIT(1), &strentries[235]},
  8414. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8415. {UPB_TABKEY_STR("\014", "\000", "\000", "\000", "LITE_RUNTIME"), UPB_TABVALUE_INT_INIT(3), NULL},
  8416. };
  8417. static const upb_tabent intentries[18] = {
  8418. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8419. {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[103]), NULL},
  8420. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8421. {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[102]), NULL},
  8422. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8423. {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[101]), NULL},
  8424. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8425. {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[99]), NULL},
  8426. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8427. {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[97]), NULL},
  8428. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8429. {UPB_TABKEY_NUM(33), UPB_TABVALUE_PTR_INIT(&fields[10]), NULL},
  8430. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8431. {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[100]), NULL},
  8432. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8433. {UPB_TABKEY_NUM(33), UPB_TABVALUE_PTR_INIT(&fields[15]), NULL},
  8434. {UPB_TABKEY_NONE, UPB_TABVALUE_EMPTY_INIT, NULL},
  8435. {UPB_TABKEY_NUM(999), UPB_TABVALUE_PTR_INIT(&fields[98]), NULL},
  8436. };
  8437. static const upb_tabval arrays[187] = {
  8438. UPB_TABVALUE_EMPTY_INIT,
  8439. UPB_TABVALUE_PTR_INIT(&fields[57]),
  8440. UPB_TABVALUE_PTR_INIT(&fields[25]),
  8441. UPB_TABVALUE_PTR_INIT(&fields[60]),
  8442. UPB_TABVALUE_PTR_INIT(&fields[20]),
  8443. UPB_TABVALUE_PTR_INIT(&fields[24]),
  8444. UPB_TABVALUE_PTR_INIT(&fields[22]),
  8445. UPB_TABVALUE_PTR_INIT(&fields[68]),
  8446. UPB_TABVALUE_PTR_INIT(&fields[65]),
  8447. UPB_TABVALUE_PTR_INIT(&fields[85]),
  8448. UPB_TABVALUE_PTR_INIT(&fields[84]),
  8449. UPB_TABVALUE_EMPTY_INIT,
  8450. UPB_TABVALUE_PTR_INIT(&fields[91]),
  8451. UPB_TABVALUE_PTR_INIT(&fields[18]),
  8452. UPB_TABVALUE_EMPTY_INIT,
  8453. UPB_TABVALUE_PTR_INIT(&fields[90]),
  8454. UPB_TABVALUE_PTR_INIT(&fields[17]),
  8455. UPB_TABVALUE_EMPTY_INIT,
  8456. UPB_TABVALUE_PTR_INIT(&fields[52]),
  8457. UPB_TABVALUE_PTR_INIT(&fields[104]),
  8458. UPB_TABVALUE_PTR_INIT(&fields[73]),
  8459. UPB_TABVALUE_EMPTY_INIT,
  8460. UPB_TABVALUE_EMPTY_INIT,
  8461. UPB_TABVALUE_PTR_INIT(&fields[1]),
  8462. UPB_TABVALUE_PTR_INIT(&fields[14]),
  8463. UPB_TABVALUE_EMPTY_INIT,
  8464. UPB_TABVALUE_PTR_INIT(&fields[50]),
  8465. UPB_TABVALUE_PTR_INIT(&fields[63]),
  8466. UPB_TABVALUE_PTR_INIT(&fields[74]),
  8467. UPB_TABVALUE_EMPTY_INIT,
  8468. UPB_TABVALUE_PTR_INIT(&fields[13]),
  8469. UPB_TABVALUE_EMPTY_INIT,
  8470. UPB_TABVALUE_PTR_INIT(&fields[56]),
  8471. UPB_TABVALUE_PTR_INIT(&fields[21]),
  8472. UPB_TABVALUE_PTR_INIT(&fields[62]),
  8473. UPB_TABVALUE_PTR_INIT(&fields[40]),
  8474. UPB_TABVALUE_PTR_INIT(&fields[95]),
  8475. UPB_TABVALUE_PTR_INIT(&fields[96]),
  8476. UPB_TABVALUE_PTR_INIT(&fields[7]),
  8477. UPB_TABVALUE_PTR_INIT(&fields[70]),
  8478. UPB_TABVALUE_PTR_INIT(&fields[66]),
  8479. UPB_TABVALUE_PTR_INIT(&fields[38]),
  8480. UPB_TABVALUE_EMPTY_INIT,
  8481. UPB_TABVALUE_PTR_INIT(&fields[6]),
  8482. UPB_TABVALUE_PTR_INIT(&fields[77]),
  8483. UPB_TABVALUE_PTR_INIT(&fields[9]),
  8484. UPB_TABVALUE_EMPTY_INIT,
  8485. UPB_TABVALUE_PTR_INIT(&fields[41]),
  8486. UPB_TABVALUE_PTR_INIT(&fields[39]),
  8487. UPB_TABVALUE_EMPTY_INIT,
  8488. UPB_TABVALUE_EMPTY_INIT,
  8489. UPB_TABVALUE_EMPTY_INIT,
  8490. UPB_TABVALUE_PTR_INIT(&fields[105]),
  8491. UPB_TABVALUE_EMPTY_INIT,
  8492. UPB_TABVALUE_PTR_INIT(&fields[51]),
  8493. UPB_TABVALUE_PTR_INIT(&fields[76]),
  8494. UPB_TABVALUE_PTR_INIT(&fields[8]),
  8495. UPB_TABVALUE_PTR_INIT(&fields[47]),
  8496. UPB_TABVALUE_PTR_INIT(&fields[19]),
  8497. UPB_TABVALUE_PTR_INIT(&fields[87]),
  8498. UPB_TABVALUE_PTR_INIT(&fields[23]),
  8499. UPB_TABVALUE_PTR_INIT(&fields[69]),
  8500. UPB_TABVALUE_PTR_INIT(&fields[88]),
  8501. UPB_TABVALUE_PTR_INIT(&fields[82]),
  8502. UPB_TABVALUE_PTR_INIT(&fields[106]),
  8503. UPB_TABVALUE_PTR_INIT(&fields[93]),
  8504. UPB_TABVALUE_EMPTY_INIT,
  8505. UPB_TABVALUE_PTR_INIT(&fields[26]),
  8506. UPB_TABVALUE_EMPTY_INIT,
  8507. UPB_TABVALUE_PTR_INIT(&fields[35]),
  8508. UPB_TABVALUE_EMPTY_INIT,
  8509. UPB_TABVALUE_EMPTY_INIT,
  8510. UPB_TABVALUE_EMPTY_INIT,
  8511. UPB_TABVALUE_EMPTY_INIT,
  8512. UPB_TABVALUE_EMPTY_INIT,
  8513. UPB_TABVALUE_EMPTY_INIT,
  8514. UPB_TABVALUE_PTR_INIT(&fields[34]),
  8515. UPB_TABVALUE_PTR_INIT(&fields[67]),
  8516. UPB_TABVALUE_PTR_INIT(&fields[33]),
  8517. UPB_TABVALUE_PTR_INIT(&fields[27]),
  8518. UPB_TABVALUE_EMPTY_INIT,
  8519. UPB_TABVALUE_EMPTY_INIT,
  8520. UPB_TABVALUE_EMPTY_INIT,
  8521. UPB_TABVALUE_EMPTY_INIT,
  8522. UPB_TABVALUE_PTR_INIT(&fields[3]),
  8523. UPB_TABVALUE_PTR_INIT(&fields[32]),
  8524. UPB_TABVALUE_PTR_INIT(&fields[83]),
  8525. UPB_TABVALUE_EMPTY_INIT,
  8526. UPB_TABVALUE_PTR_INIT(&fields[31]),
  8527. UPB_TABVALUE_EMPTY_INIT,
  8528. UPB_TABVALUE_EMPTY_INIT,
  8529. UPB_TABVALUE_PTR_INIT(&fields[12]),
  8530. UPB_TABVALUE_EMPTY_INIT,
  8531. UPB_TABVALUE_EMPTY_INIT,
  8532. UPB_TABVALUE_EMPTY_INIT,
  8533. UPB_TABVALUE_PTR_INIT(&fields[36]),
  8534. UPB_TABVALUE_EMPTY_INIT,
  8535. UPB_TABVALUE_EMPTY_INIT,
  8536. UPB_TABVALUE_EMPTY_INIT,
  8537. UPB_TABVALUE_PTR_INIT(&fields[2]),
  8538. UPB_TABVALUE_EMPTY_INIT,
  8539. UPB_TABVALUE_EMPTY_INIT,
  8540. UPB_TABVALUE_EMPTY_INIT,
  8541. UPB_TABVALUE_EMPTY_INIT,
  8542. UPB_TABVALUE_PTR_INIT(&fields[64]),
  8543. UPB_TABVALUE_PTR_INIT(&fields[5]),
  8544. UPB_TABVALUE_PTR_INIT(&fields[37]),
  8545. UPB_TABVALUE_EMPTY_INIT,
  8546. UPB_TABVALUE_PTR_INIT(&fields[79]),
  8547. UPB_TABVALUE_PTR_INIT(&fields[80]),
  8548. UPB_TABVALUE_EMPTY_INIT,
  8549. UPB_TABVALUE_PTR_INIT(&fields[46]),
  8550. UPB_TABVALUE_PTR_INIT(&fields[61]),
  8551. UPB_TABVALUE_PTR_INIT(&fields[11]),
  8552. UPB_TABVALUE_EMPTY_INIT,
  8553. UPB_TABVALUE_EMPTY_INIT,
  8554. UPB_TABVALUE_EMPTY_INIT,
  8555. UPB_TABVALUE_PTR_INIT(&fields[45]),
  8556. UPB_TABVALUE_EMPTY_INIT,
  8557. UPB_TABVALUE_PTR_INIT(&fields[55]),
  8558. UPB_TABVALUE_PTR_INIT(&fields[29]),
  8559. UPB_TABVALUE_PTR_INIT(&fields[75]),
  8560. UPB_TABVALUE_PTR_INIT(&fields[71]),
  8561. UPB_TABVALUE_PTR_INIT(&fields[4]),
  8562. UPB_TABVALUE_PTR_INIT(&fields[86]),
  8563. UPB_TABVALUE_EMPTY_INIT,
  8564. UPB_TABVALUE_EMPTY_INIT,
  8565. UPB_TABVALUE_PTR_INIT(&fields[54]),
  8566. UPB_TABVALUE_EMPTY_INIT,
  8567. UPB_TABVALUE_PTR_INIT(&fields[53]),
  8568. UPB_TABVALUE_PTR_INIT(&fields[48]),
  8569. UPB_TABVALUE_PTR_INIT(&fields[72]),
  8570. UPB_TABVALUE_EMPTY_INIT,
  8571. UPB_TABVALUE_EMPTY_INIT,
  8572. UPB_TABVALUE_PTR_INIT(&fields[44]),
  8573. UPB_TABVALUE_EMPTY_INIT,
  8574. UPB_TABVALUE_PTR_INIT(&fields[78]),
  8575. UPB_TABVALUE_PTR_INIT(&fields[89]),
  8576. UPB_TABVALUE_PTR_INIT(&fields[42]),
  8577. UPB_TABVALUE_PTR_INIT(&fields[94]),
  8578. UPB_TABVALUE_EMPTY_INIT,
  8579. UPB_TABVALUE_PTR_INIT(&fields[43]),
  8580. UPB_TABVALUE_EMPTY_INIT,
  8581. UPB_TABVALUE_EMPTY_INIT,
  8582. UPB_TABVALUE_PTR_INIT(&fields[49]),
  8583. UPB_TABVALUE_PTR_INIT(&fields[28]),
  8584. UPB_TABVALUE_PTR_INIT(&fields[81]),
  8585. UPB_TABVALUE_PTR_INIT(&fields[59]),
  8586. UPB_TABVALUE_PTR_INIT(&fields[16]),
  8587. UPB_TABVALUE_PTR_INIT(&fields[92]),
  8588. UPB_TABVALUE_PTR_INIT(&fields[0]),
  8589. UPB_TABVALUE_EMPTY_INIT,
  8590. UPB_TABVALUE_PTR_INIT(&fields[58]),
  8591. UPB_TABVALUE_PTR_INIT(&fields[30]),
  8592. UPB_TABVALUE_EMPTY_INIT,
  8593. UPB_TABVALUE_PTR_INIT("LABEL_OPTIONAL"),
  8594. UPB_TABVALUE_PTR_INIT("LABEL_REQUIRED"),
  8595. UPB_TABVALUE_PTR_INIT("LABEL_REPEATED"),
  8596. UPB_TABVALUE_EMPTY_INIT,
  8597. UPB_TABVALUE_PTR_INIT("TYPE_DOUBLE"),
  8598. UPB_TABVALUE_PTR_INIT("TYPE_FLOAT"),
  8599. UPB_TABVALUE_PTR_INIT("TYPE_INT64"),
  8600. UPB_TABVALUE_PTR_INIT("TYPE_UINT64"),
  8601. UPB_TABVALUE_PTR_INIT("TYPE_INT32"),
  8602. UPB_TABVALUE_PTR_INIT("TYPE_FIXED64"),
  8603. UPB_TABVALUE_PTR_INIT("TYPE_FIXED32"),
  8604. UPB_TABVALUE_PTR_INIT("TYPE_BOOL"),
  8605. UPB_TABVALUE_PTR_INIT("TYPE_STRING"),
  8606. UPB_TABVALUE_PTR_INIT("TYPE_GROUP"),
  8607. UPB_TABVALUE_PTR_INIT("TYPE_MESSAGE"),
  8608. UPB_TABVALUE_PTR_INIT("TYPE_BYTES"),
  8609. UPB_TABVALUE_PTR_INIT("TYPE_UINT32"),
  8610. UPB_TABVALUE_PTR_INIT("TYPE_ENUM"),
  8611. UPB_TABVALUE_PTR_INIT("TYPE_SFIXED32"),
  8612. UPB_TABVALUE_PTR_INIT("TYPE_SFIXED64"),
  8613. UPB_TABVALUE_PTR_INIT("TYPE_SINT32"),
  8614. UPB_TABVALUE_PTR_INIT("TYPE_SINT64"),
  8615. UPB_TABVALUE_PTR_INIT("STRING"),
  8616. UPB_TABVALUE_PTR_INIT("CORD"),
  8617. UPB_TABVALUE_PTR_INIT("STRING_PIECE"),
  8618. UPB_TABVALUE_PTR_INIT("JS_NORMAL"),
  8619. UPB_TABVALUE_PTR_INIT("JS_STRING"),
  8620. UPB_TABVALUE_PTR_INIT("JS_NUMBER"),
  8621. UPB_TABVALUE_EMPTY_INIT,
  8622. UPB_TABVALUE_PTR_INIT("SPEED"),
  8623. UPB_TABVALUE_PTR_INIT("CODE_SIZE"),
  8624. UPB_TABVALUE_PTR_INIT("LITE_RUNTIME"),
  8625. };
  8626. #ifdef UPB_DEBUG_REFS
  8627. static upb_inttable reftables[268] = {
  8628. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8629. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8630. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8631. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8632. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8633. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8634. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8635. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8636. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8637. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8638. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8639. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8640. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8641. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8642. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8643. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8644. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8645. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8646. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8647. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8648. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8649. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8650. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8651. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8652. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8653. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8654. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8655. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8656. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8657. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8658. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8659. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8660. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8661. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8662. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8663. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8664. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8665. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8666. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8667. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8668. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8669. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8670. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8671. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8672. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8673. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8674. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8675. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8676. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8677. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8678. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8679. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8680. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8681. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8682. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8683. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8684. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8685. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8686. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8687. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8688. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8689. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8690. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8691. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8692. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8693. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8694. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8695. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8696. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8697. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8698. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8699. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8700. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8701. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8702. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8703. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8704. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8705. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8706. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8707. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8708. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8709. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8710. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8711. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8712. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8713. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8714. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8715. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8716. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8717. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8718. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8719. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8720. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8721. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8722. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8723. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8724. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8725. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8726. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8727. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8728. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8729. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8730. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8731. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8732. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8733. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8734. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8735. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8736. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8737. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8738. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8739. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8740. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8741. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8742. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8743. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8744. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8745. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8746. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8747. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8748. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8749. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8750. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8751. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8752. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8753. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8754. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8755. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8756. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8757. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8758. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8759. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8760. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8761. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8762. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8763. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8764. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8765. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8766. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8767. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8768. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8769. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8770. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8771. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8772. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8773. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8774. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8775. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8776. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8777. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8778. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8779. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8780. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8781. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8782. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8783. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8784. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8785. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8786. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8787. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8788. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8789. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8790. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8791. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8792. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8793. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8794. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8795. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8796. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8797. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8798. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8799. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8800. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8801. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8802. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8803. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8804. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8805. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8806. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8807. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8808. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8809. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8810. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8811. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8812. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8813. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8814. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8815. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8816. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8817. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8818. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8819. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8820. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8821. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8822. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8823. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8824. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8825. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8826. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8827. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8828. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8829. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8830. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8831. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8832. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8833. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8834. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8835. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8836. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8837. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8838. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8839. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8840. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8841. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8842. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8843. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8844. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8845. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8846. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8847. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8848. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8849. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8850. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8851. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8852. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8853. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8854. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8855. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8856. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8857. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8858. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8859. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8860. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8861. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8862. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8863. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8864. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8865. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8866. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8867. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8868. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8869. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8870. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8871. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8872. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8873. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8874. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8875. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8876. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8877. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8878. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8879. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8880. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8881. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8882. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8883. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8884. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8885. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8886. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8887. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8888. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8889. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8890. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8891. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8892. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8893. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8894. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8895. UPB_EMPTY_INTTABLE_INIT(UPB_CTYPE_PTR),
  8896. };
  8897. #endif
  8898. static const upb_msgdef *refm(const upb_msgdef *m, const void *owner) {
  8899. upb_msgdef_ref(m, owner);
  8900. return m;
  8901. }
  8902. static const upb_enumdef *refe(const upb_enumdef *e, const void *owner) {
  8903. upb_enumdef_ref(e, owner);
  8904. return e;
  8905. }
  8906. /* Public API. */
  8907. const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_get(const void *owner) { return refm(&msgs[0], owner); }
  8908. const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ExtensionRange_get(const void *owner) { return refm(&msgs[1], owner); }
  8909. const upb_msgdef *upbdefs_google_protobuf_DescriptorProto_ReservedRange_get(const void *owner) { return refm(&msgs[2], owner); }
  8910. const upb_msgdef *upbdefs_google_protobuf_EnumDescriptorProto_get(const void *owner) { return refm(&msgs[3], owner); }
  8911. const upb_msgdef *upbdefs_google_protobuf_EnumOptions_get(const void *owner) { return refm(&msgs[4], owner); }
  8912. const upb_msgdef *upbdefs_google_protobuf_EnumValueDescriptorProto_get(const void *owner) { return refm(&msgs[5], owner); }
  8913. const upb_msgdef *upbdefs_google_protobuf_EnumValueOptions_get(const void *owner) { return refm(&msgs[6], owner); }
  8914. const upb_msgdef *upbdefs_google_protobuf_FieldDescriptorProto_get(const void *owner) { return refm(&msgs[7], owner); }
  8915. const upb_msgdef *upbdefs_google_protobuf_FieldOptions_get(const void *owner) { return refm(&msgs[8], owner); }
  8916. const upb_msgdef *upbdefs_google_protobuf_FileDescriptorProto_get(const void *owner) { return refm(&msgs[9], owner); }
  8917. const upb_msgdef *upbdefs_google_protobuf_FileDescriptorSet_get(const void *owner) { return refm(&msgs[10], owner); }
  8918. const upb_msgdef *upbdefs_google_protobuf_FileOptions_get(const void *owner) { return refm(&msgs[11], owner); }
  8919. const upb_msgdef *upbdefs_google_protobuf_MessageOptions_get(const void *owner) { return refm(&msgs[12], owner); }
  8920. const upb_msgdef *upbdefs_google_protobuf_MethodDescriptorProto_get(const void *owner) { return refm(&msgs[13], owner); }
  8921. const upb_msgdef *upbdefs_google_protobuf_MethodOptions_get(const void *owner) { return refm(&msgs[14], owner); }
  8922. const upb_msgdef *upbdefs_google_protobuf_OneofDescriptorProto_get(const void *owner) { return refm(&msgs[15], owner); }
  8923. const upb_msgdef *upbdefs_google_protobuf_ServiceDescriptorProto_get(const void *owner) { return refm(&msgs[16], owner); }
  8924. const upb_msgdef *upbdefs_google_protobuf_ServiceOptions_get(const void *owner) { return refm(&msgs[17], owner); }
  8925. const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo_get(const void *owner) { return refm(&msgs[18], owner); }
  8926. const upb_msgdef *upbdefs_google_protobuf_SourceCodeInfo_Location_get(const void *owner) { return refm(&msgs[19], owner); }
  8927. const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption_get(const void *owner) { return refm(&msgs[20], owner); }
  8928. const upb_msgdef *upbdefs_google_protobuf_UninterpretedOption_NamePart_get(const void *owner) { return refm(&msgs[21], owner); }
  8929. const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Label_get(const void *owner) { return refe(&enums[0], owner); }
  8930. const upb_enumdef *upbdefs_google_protobuf_FieldDescriptorProto_Type_get(const void *owner) { return refe(&enums[1], owner); }
  8931. const upb_enumdef *upbdefs_google_protobuf_FieldOptions_CType_get(const void *owner) { return refe(&enums[2], owner); }
  8932. const upb_enumdef *upbdefs_google_protobuf_FieldOptions_JSType_get(const void *owner) { return refe(&enums[3], owner); }
  8933. const upb_enumdef *upbdefs_google_protobuf_FileOptions_OptimizeMode_get(const void *owner) { return refe(&enums[4], owner); }
  8934. /*
  8935. ** XXX: The routines in this file that consume a string do not currently
  8936. ** support having the string span buffers. In the future, as upb_sink and
  8937. ** its buffering/sharing functionality evolve there should be an easy and
  8938. ** idiomatic way of correctly handling this case. For now, we accept this
  8939. ** limitation since we currently only parse descriptors from single strings.
  8940. */
  8941. #include <errno.h>
  8942. #include <stdlib.h>
  8943. #include <string.h>
  8944. /* Compares a NULL-terminated string with a non-NULL-terminated string. */
  8945. static bool upb_streq(const char *str, const char *buf, size_t n) {
  8946. return strlen(str) == n && memcmp(str, buf, n) == 0;
  8947. }
  8948. /* We keep a stack of all the messages scopes we are currently in, as well as
  8949. * the top-level file scope. This is necessary to correctly qualify the
  8950. * definitions that are contained inside. "name" tracks the name of the
  8951. * message or package (a bare name -- not qualified by any enclosing scopes). */
  8952. typedef struct {
  8953. char *name;
  8954. /* Index of the first def that is under this scope. For msgdefs, the
  8955. * msgdef itself is at start-1. */
  8956. int start;
  8957. uint32_t oneof_start;
  8958. uint32_t oneof_index;
  8959. } upb_descreader_frame;
  8960. /* The maximum number of nested declarations that are allowed, ie.
  8961. * message Foo {
  8962. * message Bar {
  8963. * message Baz {
  8964. * }
  8965. * }
  8966. * }
  8967. *
  8968. * This is a resource limit that affects how big our runtime stack can grow.
  8969. * TODO: make this a runtime-settable property of the Reader instance. */
  8970. #define UPB_MAX_MESSAGE_NESTING 64
  8971. struct upb_descreader {
  8972. upb_sink sink;
  8973. upb_inttable files;
  8974. upb_strtable files_by_name;
  8975. upb_filedef *file; /* The last file in files. */
  8976. upb_descreader_frame stack[UPB_MAX_MESSAGE_NESTING];
  8977. int stack_len;
  8978. upb_inttable oneofs;
  8979. uint32_t number;
  8980. char *name;
  8981. bool saw_number;
  8982. bool saw_name;
  8983. char *default_string;
  8984. upb_fielddef *f;
  8985. };
  8986. static char *upb_gstrndup(const char *buf, size_t n) {
  8987. char *ret = upb_gmalloc(n + 1);
  8988. if (!ret) return NULL;
  8989. memcpy(ret, buf, n);
  8990. ret[n] = '\0';
  8991. return ret;
  8992. }
  8993. /* Returns a newly allocated string that joins input strings together, for
  8994. * example:
  8995. * join("Foo.Bar", "Baz") -> "Foo.Bar.Baz"
  8996. * join("", "Baz") -> "Baz"
  8997. * Caller owns a ref on the returned string. */
  8998. static char *upb_join(const char *base, const char *name) {
  8999. if (!base || strlen(base) == 0) {
  9000. return upb_gstrdup(name);
  9001. } else {
  9002. char *ret = upb_gmalloc(strlen(base) + strlen(name) + 2);
  9003. if (!ret) {
  9004. return NULL;
  9005. }
  9006. ret[0] = '\0';
  9007. strcat(ret, base);
  9008. strcat(ret, ".");
  9009. strcat(ret, name);
  9010. return ret;
  9011. }
  9012. }
  9013. /* Qualify the defname for all defs starting with offset "start" with "str". */
  9014. static bool upb_descreader_qualify(upb_filedef *f, char *str, int32_t start) {
  9015. size_t i;
  9016. for (i = start; i < upb_filedef_defcount(f); i++) {
  9017. upb_def *def = upb_filedef_mutabledef(f, i);
  9018. char *name = upb_join(str, upb_def_fullname(def));
  9019. if (!name) {
  9020. /* Need better logic here; at this point we've qualified some names but
  9021. * not others. */
  9022. return false;
  9023. }
  9024. upb_def_setfullname(def, name, NULL);
  9025. upb_gfree(name);
  9026. }
  9027. return true;
  9028. }
  9029. /* upb_descreader ************************************************************/
  9030. static upb_msgdef *upb_descreader_top(upb_descreader *r) {
  9031. int index;
  9032. UPB_ASSERT(r->stack_len > 1);
  9033. index = r->stack[r->stack_len-1].start - 1;
  9034. UPB_ASSERT(index >= 0);
  9035. return upb_downcast_msgdef_mutable(upb_filedef_mutabledef(r->file, index));
  9036. }
  9037. static upb_def *upb_descreader_last(upb_descreader *r) {
  9038. return upb_filedef_mutabledef(r->file, upb_filedef_defcount(r->file) - 1);
  9039. }
  9040. /* Start/end handlers for FileDescriptorProto and DescriptorProto (the two
  9041. * entities that have names and can contain sub-definitions. */
  9042. void upb_descreader_startcontainer(upb_descreader *r) {
  9043. upb_descreader_frame *f = &r->stack[r->stack_len++];
  9044. f->start = upb_filedef_defcount(r->file);
  9045. f->oneof_start = upb_inttable_count(&r->oneofs);
  9046. f->oneof_index = 0;
  9047. f->name = NULL;
  9048. }
  9049. bool upb_descreader_endcontainer(upb_descreader *r) {
  9050. upb_descreader_frame *f = &r->stack[r->stack_len - 1];
  9051. while (upb_inttable_count(&r->oneofs) > f->oneof_start) {
  9052. upb_oneofdef *o = upb_value_getptr(upb_inttable_pop(&r->oneofs));
  9053. bool ok = upb_msgdef_addoneof(upb_descreader_top(r), o, &r->oneofs, NULL);
  9054. UPB_ASSERT(ok);
  9055. }
  9056. if (!upb_descreader_qualify(r->file, f->name, f->start)) {
  9057. return false;
  9058. }
  9059. upb_gfree(f->name);
  9060. f->name = NULL;
  9061. r->stack_len--;
  9062. return true;
  9063. }
  9064. void upb_descreader_setscopename(upb_descreader *r, char *str) {
  9065. upb_descreader_frame *f = &r->stack[r->stack_len-1];
  9066. upb_gfree(f->name);
  9067. f->name = str;
  9068. }
  9069. static upb_oneofdef *upb_descreader_getoneof(upb_descreader *r,
  9070. uint32_t index) {
  9071. bool found;
  9072. upb_value val;
  9073. upb_descreader_frame *f = &r->stack[r->stack_len-1];
  9074. /* DescriptorProto messages can be nested, so we will see the nested messages
  9075. * between when we see the FieldDescriptorProto and the OneofDescriptorProto.
  9076. * We need to preserve the oneofs in between these two things. */
  9077. index += f->oneof_start;
  9078. while (upb_inttable_count(&r->oneofs) <= index) {
  9079. upb_inttable_push(&r->oneofs, upb_value_ptr(upb_oneofdef_new(&r->oneofs)));
  9080. }
  9081. found = upb_inttable_lookup(&r->oneofs, index, &val);
  9082. UPB_ASSERT(found);
  9083. return upb_value_getptr(val);
  9084. }
  9085. /** Handlers for google.protobuf.FileDescriptorSet. ***************************/
  9086. static void *fileset_startfile(void *closure, const void *hd) {
  9087. upb_descreader *r = closure;
  9088. UPB_UNUSED(hd);
  9089. r->file = upb_filedef_new(&r->files);
  9090. upb_inttable_push(&r->files, upb_value_ptr(r->file));
  9091. return r;
  9092. }
  9093. /** Handlers for google.protobuf.FileDescriptorProto. *************************/
  9094. static bool file_start(void *closure, const void *hd) {
  9095. upb_descreader *r = closure;
  9096. UPB_UNUSED(hd);
  9097. upb_descreader_startcontainer(r);
  9098. return true;
  9099. }
  9100. static bool file_end(void *closure, const void *hd, upb_status *status) {
  9101. upb_descreader *r = closure;
  9102. UPB_UNUSED(hd);
  9103. UPB_UNUSED(status);
  9104. return upb_descreader_endcontainer(r);
  9105. }
  9106. static size_t file_onname(void *closure, const void *hd, const char *buf,
  9107. size_t n, const upb_bufhandle *handle) {
  9108. upb_descreader *r = closure;
  9109. char *name;
  9110. bool ok;
  9111. UPB_UNUSED(hd);
  9112. UPB_UNUSED(handle);
  9113. name = upb_gstrndup(buf, n);
  9114. upb_strtable_insert(&r->files_by_name, name, upb_value_ptr(r->file));
  9115. /* XXX: see comment at the top of the file. */
  9116. ok = upb_filedef_setname(r->file, name, NULL);
  9117. upb_gfree(name);
  9118. UPB_ASSERT(ok);
  9119. return n;
  9120. }
  9121. static size_t file_onpackage(void *closure, const void *hd, const char *buf,
  9122. size_t n, const upb_bufhandle *handle) {
  9123. upb_descreader *r = closure;
  9124. char *package;
  9125. bool ok;
  9126. UPB_UNUSED(hd);
  9127. UPB_UNUSED(handle);
  9128. package = upb_gstrndup(buf, n);
  9129. /* XXX: see comment at the top of the file. */
  9130. upb_descreader_setscopename(r, package);
  9131. ok = upb_filedef_setpackage(r->file, package, NULL);
  9132. UPB_ASSERT(ok);
  9133. return n;
  9134. }
  9135. static void *file_startphpnamespace(void *closure, const void *hd,
  9136. size_t size_hint) {
  9137. upb_descreader *r = closure;
  9138. bool ok;
  9139. UPB_UNUSED(hd);
  9140. UPB_UNUSED(size_hint);
  9141. ok = upb_filedef_setphpnamespace(r->file, "", NULL);
  9142. UPB_ASSERT(ok);
  9143. return closure;
  9144. }
  9145. static size_t file_onphpnamespace(void *closure, const void *hd,
  9146. const char *buf, size_t n,
  9147. const upb_bufhandle *handle) {
  9148. upb_descreader *r = closure;
  9149. char *php_namespace;
  9150. bool ok;
  9151. UPB_UNUSED(hd);
  9152. UPB_UNUSED(handle);
  9153. php_namespace = upb_gstrndup(buf, n);
  9154. ok = upb_filedef_setphpnamespace(r->file, php_namespace, NULL);
  9155. upb_gfree(php_namespace);
  9156. UPB_ASSERT(ok);
  9157. return n;
  9158. }
  9159. static size_t file_onphpprefix(void *closure, const void *hd, const char *buf,
  9160. size_t n, const upb_bufhandle *handle) {
  9161. upb_descreader *r = closure;
  9162. char *prefix;
  9163. bool ok;
  9164. UPB_UNUSED(hd);
  9165. UPB_UNUSED(handle);
  9166. prefix = upb_gstrndup(buf, n);
  9167. ok = upb_filedef_setphpprefix(r->file, prefix, NULL);
  9168. upb_gfree(prefix);
  9169. UPB_ASSERT(ok);
  9170. return n;
  9171. }
  9172. static size_t file_onsyntax(void *closure, const void *hd, const char *buf,
  9173. size_t n, const upb_bufhandle *handle) {
  9174. upb_descreader *r = closure;
  9175. bool ok;
  9176. UPB_UNUSED(hd);
  9177. UPB_UNUSED(handle);
  9178. /* XXX: see comment at the top of the file. */
  9179. if (upb_streq("proto2", buf, n)) {
  9180. ok = upb_filedef_setsyntax(r->file, UPB_SYNTAX_PROTO2, NULL);
  9181. } else if (upb_streq("proto3", buf, n)) {
  9182. ok = upb_filedef_setsyntax(r->file, UPB_SYNTAX_PROTO3, NULL);
  9183. } else {
  9184. ok = false;
  9185. }
  9186. UPB_ASSERT(ok);
  9187. return n;
  9188. }
  9189. static void *file_startmsg(void *closure, const void *hd) {
  9190. upb_descreader *r = closure;
  9191. upb_msgdef *m = upb_msgdef_new(&m);
  9192. bool ok = upb_filedef_addmsg(r->file, m, &m, NULL);
  9193. UPB_UNUSED(hd);
  9194. UPB_ASSERT(ok);
  9195. return r;
  9196. }
  9197. static void *file_startenum(void *closure, const void *hd) {
  9198. upb_descreader *r = closure;
  9199. upb_enumdef *e = upb_enumdef_new(&e);
  9200. bool ok = upb_filedef_addenum(r->file, e, &e, NULL);
  9201. UPB_UNUSED(hd);
  9202. UPB_ASSERT(ok);
  9203. return r;
  9204. }
  9205. static void *file_startext(void *closure, const void *hd) {
  9206. upb_descreader *r = closure;
  9207. bool ok;
  9208. r->f = upb_fielddef_new(r);
  9209. ok = upb_filedef_addext(r->file, r->f, r, NULL);
  9210. UPB_UNUSED(hd);
  9211. UPB_ASSERT(ok);
  9212. return r;
  9213. }
  9214. static size_t file_ondep(void *closure, const void *hd, const char *buf,
  9215. size_t n, const upb_bufhandle *handle) {
  9216. upb_descreader *r = closure;
  9217. upb_value val;
  9218. if (upb_strtable_lookup2(&r->files_by_name, buf, n, &val)) {
  9219. upb_filedef_adddep(r->file, upb_value_getptr(val));
  9220. }
  9221. UPB_UNUSED(hd);
  9222. UPB_UNUSED(handle);
  9223. return n;
  9224. }
  9225. /** Handlers for google.protobuf.EnumValueDescriptorProto. *********************/
  9226. static bool enumval_startmsg(void *closure, const void *hd) {
  9227. upb_descreader *r = closure;
  9228. UPB_UNUSED(hd);
  9229. r->saw_number = false;
  9230. r->saw_name = false;
  9231. return true;
  9232. }
  9233. static size_t enumval_onname(void *closure, const void *hd, const char *buf,
  9234. size_t n, const upb_bufhandle *handle) {
  9235. upb_descreader *r = closure;
  9236. UPB_UNUSED(hd);
  9237. UPB_UNUSED(handle);
  9238. /* XXX: see comment at the top of the file. */
  9239. upb_gfree(r->name);
  9240. r->name = upb_gstrndup(buf, n);
  9241. r->saw_name = true;
  9242. return n;
  9243. }
  9244. static bool enumval_onnumber(void *closure, const void *hd, int32_t val) {
  9245. upb_descreader *r = closure;
  9246. UPB_UNUSED(hd);
  9247. r->number = val;
  9248. r->saw_number = true;
  9249. return true;
  9250. }
  9251. static bool enumval_endmsg(void *closure, const void *hd, upb_status *status) {
  9252. upb_descreader *r = closure;
  9253. upb_enumdef *e;
  9254. UPB_UNUSED(hd);
  9255. if(!r->saw_number || !r->saw_name) {
  9256. upb_status_seterrmsg(status, "Enum value missing name or number.");
  9257. return false;
  9258. }
  9259. e = upb_downcast_enumdef_mutable(upb_descreader_last(r));
  9260. upb_enumdef_addval(e, r->name, r->number, status);
  9261. upb_gfree(r->name);
  9262. r->name = NULL;
  9263. return true;
  9264. }
  9265. /** Handlers for google.protobuf.EnumDescriptorProto. *************************/
  9266. static bool enum_endmsg(void *closure, const void *hd, upb_status *status) {
  9267. upb_descreader *r = closure;
  9268. upb_enumdef *e;
  9269. UPB_UNUSED(hd);
  9270. e = upb_downcast_enumdef_mutable(upb_descreader_last(r));
  9271. if (upb_def_fullname(upb_descreader_last(r)) == NULL) {
  9272. upb_status_seterrmsg(status, "Enum had no name.");
  9273. return false;
  9274. }
  9275. if (upb_enumdef_numvals(e) == 0) {
  9276. upb_status_seterrmsg(status, "Enum had no values.");
  9277. return false;
  9278. }
  9279. return true;
  9280. }
  9281. static size_t enum_onname(void *closure, const void *hd, const char *buf,
  9282. size_t n, const upb_bufhandle *handle) {
  9283. upb_descreader *r = closure;
  9284. char *fullname = upb_gstrndup(buf, n);
  9285. UPB_UNUSED(hd);
  9286. UPB_UNUSED(handle);
  9287. /* XXX: see comment at the top of the file. */
  9288. upb_def_setfullname(upb_descreader_last(r), fullname, NULL);
  9289. upb_gfree(fullname);
  9290. return n;
  9291. }
  9292. /** Handlers for google.protobuf.FieldDescriptorProto *************************/
  9293. static bool field_startmsg(void *closure, const void *hd) {
  9294. upb_descreader *r = closure;
  9295. UPB_UNUSED(hd);
  9296. UPB_ASSERT(r->f);
  9297. upb_gfree(r->default_string);
  9298. r->default_string = NULL;
  9299. /* fielddefs default to packed, but descriptors default to non-packed. */
  9300. upb_fielddef_setpacked(r->f, false);
  9301. return true;
  9302. }
  9303. /* Converts the default value in string "str" into "d". Passes a ref on str.
  9304. * Returns true on success. */
  9305. static bool parse_default(char *str, upb_fielddef *f) {
  9306. bool success = true;
  9307. char *end;
  9308. switch (upb_fielddef_type(f)) {
  9309. case UPB_TYPE_INT32: {
  9310. long val = strtol(str, &end, 0);
  9311. if (val > INT32_MAX || val < INT32_MIN || errno == ERANGE || *end)
  9312. success = false;
  9313. else
  9314. upb_fielddef_setdefaultint32(f, val);
  9315. break;
  9316. }
  9317. case UPB_TYPE_INT64: {
  9318. /* XXX: Need to write our own strtoll, since it's not available in c89. */
  9319. long long val = strtol(str, &end, 0);
  9320. if (val > INT64_MAX || val < INT64_MIN || errno == ERANGE || *end)
  9321. success = false;
  9322. else
  9323. upb_fielddef_setdefaultint64(f, val);
  9324. break;
  9325. }
  9326. case UPB_TYPE_UINT32: {
  9327. unsigned long val = strtoul(str, &end, 0);
  9328. if (val > UINT32_MAX || errno == ERANGE || *end)
  9329. success = false;
  9330. else
  9331. upb_fielddef_setdefaultuint32(f, val);
  9332. break;
  9333. }
  9334. case UPB_TYPE_UINT64: {
  9335. /* XXX: Need to write our own strtoull, since it's not available in c89. */
  9336. unsigned long long val = strtoul(str, &end, 0);
  9337. if (val > UINT64_MAX || errno == ERANGE || *end)
  9338. success = false;
  9339. else
  9340. upb_fielddef_setdefaultuint64(f, val);
  9341. break;
  9342. }
  9343. case UPB_TYPE_DOUBLE: {
  9344. double val = strtod(str, &end);
  9345. if (errno == ERANGE || *end)
  9346. success = false;
  9347. else
  9348. upb_fielddef_setdefaultdouble(f, val);
  9349. break;
  9350. }
  9351. case UPB_TYPE_FLOAT: {
  9352. /* XXX: Need to write our own strtof, since it's not available in c89. */
  9353. float val = strtod(str, &end);
  9354. if (errno == ERANGE || *end)
  9355. success = false;
  9356. else
  9357. upb_fielddef_setdefaultfloat(f, val);
  9358. break;
  9359. }
  9360. case UPB_TYPE_BOOL: {
  9361. if (strcmp(str, "false") == 0)
  9362. upb_fielddef_setdefaultbool(f, false);
  9363. else if (strcmp(str, "true") == 0)
  9364. upb_fielddef_setdefaultbool(f, true);
  9365. else
  9366. success = false;
  9367. break;
  9368. }
  9369. default: abort();
  9370. }
  9371. return success;
  9372. }
  9373. static bool field_endmsg(void *closure, const void *hd, upb_status *status) {
  9374. upb_descreader *r = closure;
  9375. upb_fielddef *f = r->f;
  9376. UPB_UNUSED(hd);
  9377. /* TODO: verify that all required fields were present. */
  9378. UPB_ASSERT(upb_fielddef_number(f) != 0);
  9379. UPB_ASSERT(upb_fielddef_name(f) != NULL);
  9380. UPB_ASSERT((upb_fielddef_subdefname(f) != NULL) == upb_fielddef_hassubdef(f));
  9381. if (r->default_string) {
  9382. if (upb_fielddef_issubmsg(f)) {
  9383. upb_status_seterrmsg(status, "Submessages cannot have defaults.");
  9384. return false;
  9385. }
  9386. if (upb_fielddef_isstring(f) || upb_fielddef_type(f) == UPB_TYPE_ENUM) {
  9387. upb_fielddef_setdefaultcstr(f, r->default_string, NULL);
  9388. } else {
  9389. if (r->default_string && !parse_default(r->default_string, f)) {
  9390. /* We don't worry too much about giving a great error message since the
  9391. * compiler should have ensured this was correct. */
  9392. upb_status_seterrmsg(status, "Error converting default value.");
  9393. return false;
  9394. }
  9395. }
  9396. }
  9397. return true;
  9398. }
  9399. static bool field_onlazy(void *closure, const void *hd, bool val) {
  9400. upb_descreader *r = closure;
  9401. UPB_UNUSED(hd);
  9402. upb_fielddef_setlazy(r->f, val);
  9403. return true;
  9404. }
  9405. static bool field_onpacked(void *closure, const void *hd, bool val) {
  9406. upb_descreader *r = closure;
  9407. UPB_UNUSED(hd);
  9408. upb_fielddef_setpacked(r->f, val);
  9409. return true;
  9410. }
  9411. static bool field_ontype(void *closure, const void *hd, int32_t val) {
  9412. upb_descreader *r = closure;
  9413. UPB_UNUSED(hd);
  9414. upb_fielddef_setdescriptortype(r->f, val);
  9415. return true;
  9416. }
  9417. static bool field_onlabel(void *closure, const void *hd, int32_t val) {
  9418. upb_descreader *r = closure;
  9419. UPB_UNUSED(hd);
  9420. upb_fielddef_setlabel(r->f, val);
  9421. return true;
  9422. }
  9423. static bool field_onnumber(void *closure, const void *hd, int32_t val) {
  9424. upb_descreader *r = closure;
  9425. bool ok;
  9426. UPB_UNUSED(hd);
  9427. ok = upb_fielddef_setnumber(r->f, val, NULL);
  9428. UPB_ASSERT(ok);
  9429. return true;
  9430. }
  9431. static size_t field_onname(void *closure, const void *hd, const char *buf,
  9432. size_t n, const upb_bufhandle *handle) {
  9433. upb_descreader *r = closure;
  9434. char *name = upb_gstrndup(buf, n);
  9435. UPB_UNUSED(hd);
  9436. UPB_UNUSED(handle);
  9437. /* XXX: see comment at the top of the file. */
  9438. upb_fielddef_setname(r->f, name, NULL);
  9439. upb_gfree(name);
  9440. return n;
  9441. }
  9442. static size_t field_ontypename(void *closure, const void *hd, const char *buf,
  9443. size_t n, const upb_bufhandle *handle) {
  9444. upb_descreader *r = closure;
  9445. char *name = upb_gstrndup(buf, n);
  9446. UPB_UNUSED(hd);
  9447. UPB_UNUSED(handle);
  9448. /* XXX: see comment at the top of the file. */
  9449. upb_fielddef_setsubdefname(r->f, name, NULL);
  9450. upb_gfree(name);
  9451. return n;
  9452. }
  9453. static size_t field_onextendee(void *closure, const void *hd, const char *buf,
  9454. size_t n, const upb_bufhandle *handle) {
  9455. upb_descreader *r = closure;
  9456. char *name = upb_gstrndup(buf, n);
  9457. UPB_UNUSED(hd);
  9458. UPB_UNUSED(handle);
  9459. /* XXX: see comment at the top of the file. */
  9460. upb_fielddef_setcontainingtypename(r->f, name, NULL);
  9461. upb_gfree(name);
  9462. return n;
  9463. }
  9464. static size_t field_ondefaultval(void *closure, const void *hd, const char *buf,
  9465. size_t n, const upb_bufhandle *handle) {
  9466. upb_descreader *r = closure;
  9467. UPB_UNUSED(hd);
  9468. UPB_UNUSED(handle);
  9469. /* Have to convert from string to the correct type, but we might not know the
  9470. * type yet, so we save it as a string until the end of the field.
  9471. * XXX: see comment at the top of the file. */
  9472. upb_gfree(r->default_string);
  9473. r->default_string = upb_gstrndup(buf, n);
  9474. return n;
  9475. }
  9476. static bool field_ononeofindex(void *closure, const void *hd, int32_t index) {
  9477. upb_descreader *r = closure;
  9478. upb_oneofdef *o = upb_descreader_getoneof(r, index);
  9479. bool ok = upb_oneofdef_addfield(o, r->f, &r->f, NULL);
  9480. UPB_UNUSED(hd);
  9481. UPB_ASSERT(ok);
  9482. return true;
  9483. }
  9484. /** Handlers for google.protobuf.OneofDescriptorProto. ************************/
  9485. static size_t oneof_name(void *closure, const void *hd, const char *buf,
  9486. size_t n, const upb_bufhandle *handle) {
  9487. upb_descreader *r = closure;
  9488. upb_descreader_frame *f = &r->stack[r->stack_len-1];
  9489. upb_oneofdef *o = upb_descreader_getoneof(r, f->oneof_index++);
  9490. char *name_null_terminated = upb_gstrndup(buf, n);
  9491. bool ok = upb_oneofdef_setname(o, name_null_terminated, NULL);
  9492. UPB_UNUSED(hd);
  9493. UPB_UNUSED(handle);
  9494. UPB_ASSERT(ok);
  9495. free(name_null_terminated);
  9496. return n;
  9497. }
  9498. /** Handlers for google.protobuf.DescriptorProto ******************************/
  9499. static bool msg_start(void *closure, const void *hd) {
  9500. upb_descreader *r = closure;
  9501. UPB_UNUSED(hd);
  9502. upb_descreader_startcontainer(r);
  9503. return true;
  9504. }
  9505. static bool msg_end(void *closure, const void *hd, upb_status *status) {
  9506. upb_descreader *r = closure;
  9507. upb_msgdef *m = upb_descreader_top(r);
  9508. UPB_UNUSED(hd);
  9509. if(!upb_def_fullname(upb_msgdef_upcast_mutable(m))) {
  9510. upb_status_seterrmsg(status, "Encountered message with no name.");
  9511. return false;
  9512. }
  9513. return upb_descreader_endcontainer(r);
  9514. }
  9515. static size_t msg_name(void *closure, const void *hd, const char *buf,
  9516. size_t n, const upb_bufhandle *handle) {
  9517. upb_descreader *r = closure;
  9518. upb_msgdef *m = upb_descreader_top(r);
  9519. /* XXX: see comment at the top of the file. */
  9520. char *name = upb_gstrndup(buf, n);
  9521. UPB_UNUSED(hd);
  9522. UPB_UNUSED(handle);
  9523. upb_def_setfullname(upb_msgdef_upcast_mutable(m), name, NULL);
  9524. upb_descreader_setscopename(r, name); /* Passes ownership of name. */
  9525. return n;
  9526. }
  9527. static void *msg_startmsg(void *closure, const void *hd) {
  9528. upb_descreader *r = closure;
  9529. upb_msgdef *m = upb_msgdef_new(&m);
  9530. bool ok = upb_filedef_addmsg(r->file, m, &m, NULL);
  9531. UPB_UNUSED(hd);
  9532. UPB_ASSERT(ok);
  9533. return r;
  9534. }
  9535. static void *msg_startext(void *closure, const void *hd) {
  9536. upb_descreader *r = closure;
  9537. upb_fielddef *f = upb_fielddef_new(&f);
  9538. bool ok = upb_filedef_addext(r->file, f, &f, NULL);
  9539. UPB_UNUSED(hd);
  9540. UPB_ASSERT(ok);
  9541. return r;
  9542. }
  9543. static void *msg_startfield(void *closure, const void *hd) {
  9544. upb_descreader *r = closure;
  9545. r->f = upb_fielddef_new(&r->f);
  9546. /* We can't add the new field to the message until its name/number are
  9547. * filled in. */
  9548. UPB_UNUSED(hd);
  9549. return r;
  9550. }
  9551. static bool msg_endfield(void *closure, const void *hd) {
  9552. upb_descreader *r = closure;
  9553. upb_msgdef *m = upb_descreader_top(r);
  9554. bool ok;
  9555. UPB_UNUSED(hd);
  9556. /* Oneof fields are added to the msgdef through their oneof, so don't need to
  9557. * be added here. */
  9558. if (upb_fielddef_containingoneof(r->f) == NULL) {
  9559. ok = upb_msgdef_addfield(m, r->f, &r->f, NULL);
  9560. UPB_ASSERT(ok);
  9561. }
  9562. r->f = NULL;
  9563. return true;
  9564. }
  9565. static bool msg_onmapentry(void *closure, const void *hd, bool mapentry) {
  9566. upb_descreader *r = closure;
  9567. upb_msgdef *m = upb_descreader_top(r);
  9568. UPB_UNUSED(hd);
  9569. upb_msgdef_setmapentry(m, mapentry);
  9570. r->f = NULL;
  9571. return true;
  9572. }
  9573. /** Code to register handlers *************************************************/
  9574. #define F(msg, field) upbdefs_google_protobuf_ ## msg ## _f_ ## field(m)
  9575. static void reghandlers(const void *closure, upb_handlers *h) {
  9576. const upb_msgdef *m = upb_handlers_msgdef(h);
  9577. UPB_UNUSED(closure);
  9578. if (upbdefs_google_protobuf_FileDescriptorSet_is(m)) {
  9579. upb_handlers_setstartsubmsg(h, F(FileDescriptorSet, file),
  9580. &fileset_startfile, NULL);
  9581. } else if (upbdefs_google_protobuf_DescriptorProto_is(m)) {
  9582. upb_handlers_setstartmsg(h, &msg_start, NULL);
  9583. upb_handlers_setendmsg(h, &msg_end, NULL);
  9584. upb_handlers_setstring(h, F(DescriptorProto, name), &msg_name, NULL);
  9585. upb_handlers_setstartsubmsg(h, F(DescriptorProto, extension), &msg_startext,
  9586. NULL);
  9587. upb_handlers_setstartsubmsg(h, F(DescriptorProto, nested_type),
  9588. &msg_startmsg, NULL);
  9589. upb_handlers_setstartsubmsg(h, F(DescriptorProto, field),
  9590. &msg_startfield, NULL);
  9591. upb_handlers_setendsubmsg(h, F(DescriptorProto, field),
  9592. &msg_endfield, NULL);
  9593. upb_handlers_setstartsubmsg(h, F(DescriptorProto, enum_type),
  9594. &file_startenum, NULL);
  9595. } else if (upbdefs_google_protobuf_FileDescriptorProto_is(m)) {
  9596. upb_handlers_setstartmsg(h, &file_start, NULL);
  9597. upb_handlers_setendmsg(h, &file_end, NULL);
  9598. upb_handlers_setstring(h, F(FileDescriptorProto, name), &file_onname,
  9599. NULL);
  9600. upb_handlers_setstring(h, F(FileDescriptorProto, package), &file_onpackage,
  9601. NULL);
  9602. upb_handlers_setstring(h, F(FileDescriptorProto, syntax), &file_onsyntax,
  9603. NULL);
  9604. upb_handlers_setstartsubmsg(h, F(FileDescriptorProto, message_type),
  9605. &file_startmsg, NULL);
  9606. upb_handlers_setstartsubmsg(h, F(FileDescriptorProto, enum_type),
  9607. &file_startenum, NULL);
  9608. upb_handlers_setstartsubmsg(h, F(FileDescriptorProto, extension),
  9609. &file_startext, NULL);
  9610. upb_handlers_setstring(h, F(FileDescriptorProto, dependency),
  9611. &file_ondep, NULL);
  9612. } else if (upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)) {
  9613. upb_handlers_setstartmsg(h, &enumval_startmsg, NULL);
  9614. upb_handlers_setendmsg(h, &enumval_endmsg, NULL);
  9615. upb_handlers_setstring(h, F(EnumValueDescriptorProto, name), &enumval_onname, NULL);
  9616. upb_handlers_setint32(h, F(EnumValueDescriptorProto, number), &enumval_onnumber,
  9617. NULL);
  9618. } else if (upbdefs_google_protobuf_EnumDescriptorProto_is(m)) {
  9619. upb_handlers_setendmsg(h, &enum_endmsg, NULL);
  9620. upb_handlers_setstring(h, F(EnumDescriptorProto, name), &enum_onname, NULL);
  9621. } else if (upbdefs_google_protobuf_FieldDescriptorProto_is(m)) {
  9622. upb_handlers_setstartmsg(h, &field_startmsg, NULL);
  9623. upb_handlers_setendmsg(h, &field_endmsg, NULL);
  9624. upb_handlers_setint32(h, F(FieldDescriptorProto, type), &field_ontype,
  9625. NULL);
  9626. upb_handlers_setint32(h, F(FieldDescriptorProto, label), &field_onlabel,
  9627. NULL);
  9628. upb_handlers_setint32(h, F(FieldDescriptorProto, number), &field_onnumber,
  9629. NULL);
  9630. upb_handlers_setstring(h, F(FieldDescriptorProto, name), &field_onname,
  9631. NULL);
  9632. upb_handlers_setstring(h, F(FieldDescriptorProto, type_name),
  9633. &field_ontypename, NULL);
  9634. upb_handlers_setstring(h, F(FieldDescriptorProto, extendee),
  9635. &field_onextendee, NULL);
  9636. upb_handlers_setstring(h, F(FieldDescriptorProto, default_value),
  9637. &field_ondefaultval, NULL);
  9638. upb_handlers_setint32(h, F(FieldDescriptorProto, oneof_index),
  9639. &field_ononeofindex, NULL);
  9640. } else if (upbdefs_google_protobuf_OneofDescriptorProto_is(m)) {
  9641. upb_handlers_setstring(h, F(OneofDescriptorProto, name), &oneof_name, NULL);
  9642. } else if (upbdefs_google_protobuf_FieldOptions_is(m)) {
  9643. upb_handlers_setbool(h, F(FieldOptions, lazy), &field_onlazy, NULL);
  9644. upb_handlers_setbool(h, F(FieldOptions, packed), &field_onpacked, NULL);
  9645. } else if (upbdefs_google_protobuf_MessageOptions_is(m)) {
  9646. upb_handlers_setbool(h, F(MessageOptions, map_entry), &msg_onmapentry, NULL);
  9647. } else if (upbdefs_google_protobuf_FileOptions_is(m)) {
  9648. upb_handlers_setstring(h, F(FileOptions, php_class_prefix),
  9649. &file_onphpprefix, NULL);
  9650. upb_handlers_setstartstr(h, F(FileOptions, php_namespace),
  9651. &file_startphpnamespace, NULL);
  9652. upb_handlers_setstring(h, F(FileOptions, php_namespace),
  9653. &file_onphpnamespace, NULL);
  9654. }
  9655. UPB_ASSERT(upb_ok(upb_handlers_status(h)));
  9656. }
  9657. #undef F
  9658. void descreader_cleanup(void *_r) {
  9659. upb_descreader *r = _r;
  9660. size_t i;
  9661. for (i = 0; i < upb_descreader_filecount(r); i++) {
  9662. upb_filedef_unref(upb_descreader_file(r, i), &r->files);
  9663. }
  9664. upb_gfree(r->name);
  9665. upb_inttable_uninit(&r->files);
  9666. upb_strtable_uninit(&r->files_by_name);
  9667. upb_inttable_uninit(&r->oneofs);
  9668. upb_gfree(r->default_string);
  9669. while (r->stack_len > 0) {
  9670. upb_descreader_frame *f = &r->stack[--r->stack_len];
  9671. upb_gfree(f->name);
  9672. }
  9673. }
  9674. /* Public API ****************************************************************/
  9675. upb_descreader *upb_descreader_create(upb_env *e, const upb_handlers *h) {
  9676. upb_descreader *r = upb_env_malloc(e, sizeof(upb_descreader));
  9677. if (!r || !upb_env_addcleanup(e, descreader_cleanup, r)) {
  9678. return NULL;
  9679. }
  9680. upb_inttable_init(&r->files, UPB_CTYPE_PTR);
  9681. upb_strtable_init(&r->files_by_name, UPB_CTYPE_PTR);
  9682. upb_inttable_init(&r->oneofs, UPB_CTYPE_PTR);
  9683. upb_sink_reset(upb_descreader_input(r), h, r);
  9684. r->stack_len = 0;
  9685. r->name = NULL;
  9686. r->default_string = NULL;
  9687. return r;
  9688. }
  9689. size_t upb_descreader_filecount(const upb_descreader *r) {
  9690. return upb_inttable_count(&r->files);
  9691. }
  9692. upb_filedef *upb_descreader_file(const upb_descreader *r, size_t i) {
  9693. upb_value v;
  9694. if (upb_inttable_lookup(&r->files, i, &v)) {
  9695. return upb_value_getptr(v);
  9696. } else {
  9697. return NULL;
  9698. }
  9699. }
  9700. upb_sink *upb_descreader_input(upb_descreader *r) {
  9701. return &r->sink;
  9702. }
  9703. const upb_handlers *upb_descreader_newhandlers(const void *owner) {
  9704. const upb_msgdef *m = upbdefs_google_protobuf_FileDescriptorSet_get(&m);
  9705. const upb_handlers *h = upb_handlers_newfrozen(m, owner, reghandlers, NULL);
  9706. upb_msgdef_unref(m, &m);
  9707. return h;
  9708. }
  9709. /*
  9710. ** protobuf decoder bytecode compiler
  9711. **
  9712. ** Code to compile a upb::Handlers into bytecode for decoding a protobuf
  9713. ** according to that specific schema and destination handlers.
  9714. **
  9715. ** Compiling to bytecode is always the first step. If we are using the
  9716. ** interpreted decoder we leave it as bytecode and interpret that. If we are
  9717. ** using a JIT decoder we use a code generator to turn the bytecode into native
  9718. ** code, LLVM IR, etc.
  9719. **
  9720. ** Bytecode definition is in decoder.int.h.
  9721. */
  9722. #include <stdarg.h>
  9723. #ifdef UPB_DUMP_BYTECODE
  9724. #include <stdio.h>
  9725. #endif
  9726. #define MAXLABEL 5
  9727. #define EMPTYLABEL -1
  9728. /* mgroup *********************************************************************/
  9729. static void freegroup(upb_refcounted *r) {
  9730. mgroup *g = (mgroup*)r;
  9731. upb_inttable_uninit(&g->methods);
  9732. #ifdef UPB_USE_JIT_X64
  9733. upb_pbdecoder_freejit(g);
  9734. #endif
  9735. upb_gfree(g->bytecode);
  9736. upb_gfree(g);
  9737. }
  9738. static void visitgroup(const upb_refcounted *r, upb_refcounted_visit *visit,
  9739. void *closure) {
  9740. const mgroup *g = (const mgroup*)r;
  9741. upb_inttable_iter i;
  9742. upb_inttable_begin(&i, &g->methods);
  9743. for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  9744. upb_pbdecodermethod *method = upb_value_getptr(upb_inttable_iter_value(&i));
  9745. visit(r, upb_pbdecodermethod_upcast(method), closure);
  9746. }
  9747. }
  9748. mgroup *newgroup(const void *owner) {
  9749. mgroup *g = upb_gmalloc(sizeof(*g));
  9750. static const struct upb_refcounted_vtbl vtbl = {visitgroup, freegroup};
  9751. upb_refcounted_init(mgroup_upcast_mutable(g), &vtbl, owner);
  9752. upb_inttable_init(&g->methods, UPB_CTYPE_PTR);
  9753. g->bytecode = NULL;
  9754. g->bytecode_end = NULL;
  9755. return g;
  9756. }
  9757. /* upb_pbdecodermethod ********************************************************/
  9758. static void freemethod(upb_refcounted *r) {
  9759. upb_pbdecodermethod *method = (upb_pbdecodermethod*)r;
  9760. if (method->dest_handlers_) {
  9761. upb_handlers_unref(method->dest_handlers_, method);
  9762. }
  9763. upb_inttable_uninit(&method->dispatch);
  9764. upb_gfree(method);
  9765. }
  9766. static void visitmethod(const upb_refcounted *r, upb_refcounted_visit *visit,
  9767. void *closure) {
  9768. const upb_pbdecodermethod *m = (const upb_pbdecodermethod*)r;
  9769. visit(r, m->group, closure);
  9770. }
  9771. static upb_pbdecodermethod *newmethod(const upb_handlers *dest_handlers,
  9772. mgroup *group) {
  9773. static const struct upb_refcounted_vtbl vtbl = {visitmethod, freemethod};
  9774. upb_pbdecodermethod *ret = upb_gmalloc(sizeof(*ret));
  9775. upb_refcounted_init(upb_pbdecodermethod_upcast_mutable(ret), &vtbl, &ret);
  9776. upb_byteshandler_init(&ret->input_handler_);
  9777. /* The method references the group and vice-versa, in a circular reference. */
  9778. upb_ref2(ret, group);
  9779. upb_ref2(group, ret);
  9780. upb_inttable_insertptr(&group->methods, dest_handlers, upb_value_ptr(ret));
  9781. upb_pbdecodermethod_unref(ret, &ret);
  9782. ret->group = mgroup_upcast_mutable(group);
  9783. ret->dest_handlers_ = dest_handlers;
  9784. ret->is_native_ = false; /* If we JIT, it will update this later. */
  9785. upb_inttable_init(&ret->dispatch, UPB_CTYPE_UINT64);
  9786. if (ret->dest_handlers_) {
  9787. upb_handlers_ref(ret->dest_handlers_, ret);
  9788. }
  9789. return ret;
  9790. }
  9791. const upb_handlers *upb_pbdecodermethod_desthandlers(
  9792. const upb_pbdecodermethod *m) {
  9793. return m->dest_handlers_;
  9794. }
  9795. const upb_byteshandler *upb_pbdecodermethod_inputhandler(
  9796. const upb_pbdecodermethod *m) {
  9797. return &m->input_handler_;
  9798. }
  9799. bool upb_pbdecodermethod_isnative(const upb_pbdecodermethod *m) {
  9800. return m->is_native_;
  9801. }
  9802. const upb_pbdecodermethod *upb_pbdecodermethod_new(
  9803. const upb_pbdecodermethodopts *opts, const void *owner) {
  9804. const upb_pbdecodermethod *ret;
  9805. upb_pbcodecache cache;
  9806. upb_pbcodecache_init(&cache);
  9807. ret = upb_pbcodecache_getdecodermethod(&cache, opts);
  9808. upb_pbdecodermethod_ref(ret, owner);
  9809. upb_pbcodecache_uninit(&cache);
  9810. return ret;
  9811. }
  9812. /* bytecode compiler **********************************************************/
  9813. /* Data used only at compilation time. */
  9814. typedef struct {
  9815. mgroup *group;
  9816. uint32_t *pc;
  9817. int fwd_labels[MAXLABEL];
  9818. int back_labels[MAXLABEL];
  9819. /* For fields marked "lazy", parse them lazily or eagerly? */
  9820. bool lazy;
  9821. } compiler;
  9822. static compiler *newcompiler(mgroup *group, bool lazy) {
  9823. compiler *ret = upb_gmalloc(sizeof(*ret));
  9824. int i;
  9825. ret->group = group;
  9826. ret->lazy = lazy;
  9827. for (i = 0; i < MAXLABEL; i++) {
  9828. ret->fwd_labels[i] = EMPTYLABEL;
  9829. ret->back_labels[i] = EMPTYLABEL;
  9830. }
  9831. return ret;
  9832. }
  9833. static void freecompiler(compiler *c) {
  9834. upb_gfree(c);
  9835. }
  9836. const size_t ptr_words = sizeof(void*) / sizeof(uint32_t);
  9837. /* How many words an instruction is. */
  9838. static int instruction_len(uint32_t instr) {
  9839. switch (getop(instr)) {
  9840. case OP_SETDISPATCH: return 1 + ptr_words;
  9841. case OP_TAGN: return 3;
  9842. case OP_SETBIGGROUPNUM: return 2;
  9843. default: return 1;
  9844. }
  9845. }
  9846. bool op_has_longofs(int32_t instruction) {
  9847. switch (getop(instruction)) {
  9848. case OP_CALL:
  9849. case OP_BRANCH:
  9850. case OP_CHECKDELIM:
  9851. return true;
  9852. /* The "tag" instructions only have 8 bytes available for the jump target,
  9853. * but that is ok because these opcodes only require short jumps. */
  9854. case OP_TAG1:
  9855. case OP_TAG2:
  9856. case OP_TAGN:
  9857. return false;
  9858. default:
  9859. UPB_ASSERT(false);
  9860. return false;
  9861. }
  9862. }
  9863. static int32_t getofs(uint32_t instruction) {
  9864. if (op_has_longofs(instruction)) {
  9865. return (int32_t)instruction >> 8;
  9866. } else {
  9867. return (int8_t)(instruction >> 8);
  9868. }
  9869. }
  9870. static void setofs(uint32_t *instruction, int32_t ofs) {
  9871. if (op_has_longofs(*instruction)) {
  9872. *instruction = getop(*instruction) | ofs << 8;
  9873. } else {
  9874. *instruction = (*instruction & ~0xff00) | ((ofs & 0xff) << 8);
  9875. }
  9876. UPB_ASSERT(getofs(*instruction) == ofs); /* Would fail in cases of overflow. */
  9877. }
  9878. static uint32_t pcofs(compiler *c) { return c->pc - c->group->bytecode; }
  9879. /* Defines a local label at the current PC location. All previous forward
  9880. * references are updated to point to this location. The location is noted
  9881. * for any future backward references. */
  9882. static void label(compiler *c, unsigned int label) {
  9883. int val;
  9884. uint32_t *codep;
  9885. UPB_ASSERT(label < MAXLABEL);
  9886. val = c->fwd_labels[label];
  9887. codep = (val == EMPTYLABEL) ? NULL : c->group->bytecode + val;
  9888. while (codep) {
  9889. int ofs = getofs(*codep);
  9890. setofs(codep, c->pc - codep - instruction_len(*codep));
  9891. codep = ofs ? codep + ofs : NULL;
  9892. }
  9893. c->fwd_labels[label] = EMPTYLABEL;
  9894. c->back_labels[label] = pcofs(c);
  9895. }
  9896. /* Creates a reference to a numbered label; either a forward reference
  9897. * (positive arg) or backward reference (negative arg). For forward references
  9898. * the value returned now is actually a "next" pointer into a linked list of all
  9899. * instructions that use this label and will be patched later when the label is
  9900. * defined with label().
  9901. *
  9902. * The returned value is the offset that should be written into the instruction.
  9903. */
  9904. static int32_t labelref(compiler *c, int label) {
  9905. UPB_ASSERT(label < MAXLABEL);
  9906. if (label == LABEL_DISPATCH) {
  9907. /* No resolving required. */
  9908. return 0;
  9909. } else if (label < 0) {
  9910. /* Backward local label. Relative to the next instruction. */
  9911. uint32_t from = (c->pc + 1) - c->group->bytecode;
  9912. return c->back_labels[-label] - from;
  9913. } else {
  9914. /* Forward local label: prepend to (possibly-empty) linked list. */
  9915. int *lptr = &c->fwd_labels[label];
  9916. int32_t ret = (*lptr == EMPTYLABEL) ? 0 : *lptr - pcofs(c);
  9917. *lptr = pcofs(c);
  9918. return ret;
  9919. }
  9920. }
  9921. static void put32(compiler *c, uint32_t v) {
  9922. mgroup *g = c->group;
  9923. if (c->pc == g->bytecode_end) {
  9924. int ofs = pcofs(c);
  9925. size_t oldsize = g->bytecode_end - g->bytecode;
  9926. size_t newsize = UPB_MAX(oldsize * 2, 64);
  9927. /* TODO(haberman): handle OOM. */
  9928. g->bytecode = upb_grealloc(g->bytecode, oldsize * sizeof(uint32_t),
  9929. newsize * sizeof(uint32_t));
  9930. g->bytecode_end = g->bytecode + newsize;
  9931. c->pc = g->bytecode + ofs;
  9932. }
  9933. *c->pc++ = v;
  9934. }
  9935. static void putop(compiler *c, int op, ...) {
  9936. va_list ap;
  9937. va_start(ap, op);
  9938. switch (op) {
  9939. case OP_SETDISPATCH: {
  9940. uintptr_t ptr = (uintptr_t)va_arg(ap, void*);
  9941. put32(c, OP_SETDISPATCH);
  9942. put32(c, ptr);
  9943. if (sizeof(uintptr_t) > sizeof(uint32_t))
  9944. put32(c, (uint64_t)ptr >> 32);
  9945. break;
  9946. }
  9947. case OP_STARTMSG:
  9948. case OP_ENDMSG:
  9949. case OP_PUSHLENDELIM:
  9950. case OP_POP:
  9951. case OP_SETDELIM:
  9952. case OP_HALT:
  9953. case OP_RET:
  9954. case OP_DISPATCH:
  9955. put32(c, op);
  9956. break;
  9957. case OP_PARSE_DOUBLE:
  9958. case OP_PARSE_FLOAT:
  9959. case OP_PARSE_INT64:
  9960. case OP_PARSE_UINT64:
  9961. case OP_PARSE_INT32:
  9962. case OP_PARSE_FIXED64:
  9963. case OP_PARSE_FIXED32:
  9964. case OP_PARSE_BOOL:
  9965. case OP_PARSE_UINT32:
  9966. case OP_PARSE_SFIXED32:
  9967. case OP_PARSE_SFIXED64:
  9968. case OP_PARSE_SINT32:
  9969. case OP_PARSE_SINT64:
  9970. case OP_STARTSEQ:
  9971. case OP_ENDSEQ:
  9972. case OP_STARTSUBMSG:
  9973. case OP_ENDSUBMSG:
  9974. case OP_STARTSTR:
  9975. case OP_STRING:
  9976. case OP_ENDSTR:
  9977. case OP_PUSHTAGDELIM:
  9978. put32(c, op | va_arg(ap, upb_selector_t) << 8);
  9979. break;
  9980. case OP_SETBIGGROUPNUM:
  9981. put32(c, op);
  9982. put32(c, va_arg(ap, int));
  9983. break;
  9984. case OP_CALL: {
  9985. const upb_pbdecodermethod *method = va_arg(ap, upb_pbdecodermethod *);
  9986. put32(c, op | (method->code_base.ofs - (pcofs(c) + 1)) << 8);
  9987. break;
  9988. }
  9989. case OP_CHECKDELIM:
  9990. case OP_BRANCH: {
  9991. uint32_t instruction = op;
  9992. int label = va_arg(ap, int);
  9993. setofs(&instruction, labelref(c, label));
  9994. put32(c, instruction);
  9995. break;
  9996. }
  9997. case OP_TAG1:
  9998. case OP_TAG2: {
  9999. int label = va_arg(ap, int);
  10000. uint64_t tag = va_arg(ap, uint64_t);
  10001. uint32_t instruction = op | (tag << 16);
  10002. UPB_ASSERT(tag <= 0xffff);
  10003. setofs(&instruction, labelref(c, label));
  10004. put32(c, instruction);
  10005. break;
  10006. }
  10007. case OP_TAGN: {
  10008. int label = va_arg(ap, int);
  10009. uint64_t tag = va_arg(ap, uint64_t);
  10010. uint32_t instruction = op | (upb_value_size(tag) << 16);
  10011. setofs(&instruction, labelref(c, label));
  10012. put32(c, instruction);
  10013. put32(c, tag);
  10014. put32(c, tag >> 32);
  10015. break;
  10016. }
  10017. }
  10018. va_end(ap);
  10019. }
  10020. #if defined(UPB_USE_JIT_X64) || defined(UPB_DUMP_BYTECODE)
  10021. const char *upb_pbdecoder_getopname(unsigned int op) {
  10022. #define QUOTE(x) #x
  10023. #define EXPAND_AND_QUOTE(x) QUOTE(x)
  10024. #define OPNAME(x) OP_##x
  10025. #define OP(x) case OPNAME(x): return EXPAND_AND_QUOTE(OPNAME(x));
  10026. #define T(x) OP(PARSE_##x)
  10027. /* Keep in sync with list in decoder.int.h. */
  10028. switch ((opcode)op) {
  10029. T(DOUBLE) T(FLOAT) T(INT64) T(UINT64) T(INT32) T(FIXED64) T(FIXED32)
  10030. T(BOOL) T(UINT32) T(SFIXED32) T(SFIXED64) T(SINT32) T(SINT64)
  10031. OP(STARTMSG) OP(ENDMSG) OP(STARTSEQ) OP(ENDSEQ) OP(STARTSUBMSG)
  10032. OP(ENDSUBMSG) OP(STARTSTR) OP(STRING) OP(ENDSTR) OP(CALL) OP(RET)
  10033. OP(PUSHLENDELIM) OP(PUSHTAGDELIM) OP(SETDELIM) OP(CHECKDELIM)
  10034. OP(BRANCH) OP(TAG1) OP(TAG2) OP(TAGN) OP(SETDISPATCH) OP(POP)
  10035. OP(SETBIGGROUPNUM) OP(DISPATCH) OP(HALT)
  10036. }
  10037. return "<unknown op>";
  10038. #undef OP
  10039. #undef T
  10040. }
  10041. #endif
  10042. #ifdef UPB_DUMP_BYTECODE
  10043. static void dumpbc(uint32_t *p, uint32_t *end, FILE *f) {
  10044. uint32_t *begin = p;
  10045. while (p < end) {
  10046. fprintf(f, "%p %8tx", p, p - begin);
  10047. uint32_t instr = *p++;
  10048. uint8_t op = getop(instr);
  10049. fprintf(f, " %s", upb_pbdecoder_getopname(op));
  10050. switch ((opcode)op) {
  10051. case OP_SETDISPATCH: {
  10052. const upb_inttable *dispatch;
  10053. memcpy(&dispatch, p, sizeof(void*));
  10054. p += ptr_words;
  10055. const upb_pbdecodermethod *method =
  10056. (void *)((char *)dispatch -
  10057. offsetof(upb_pbdecodermethod, dispatch));
  10058. fprintf(f, " %s", upb_msgdef_fullname(
  10059. upb_handlers_msgdef(method->dest_handlers_)));
  10060. break;
  10061. }
  10062. case OP_DISPATCH:
  10063. case OP_STARTMSG:
  10064. case OP_ENDMSG:
  10065. case OP_PUSHLENDELIM:
  10066. case OP_POP:
  10067. case OP_SETDELIM:
  10068. case OP_HALT:
  10069. case OP_RET:
  10070. break;
  10071. case OP_PARSE_DOUBLE:
  10072. case OP_PARSE_FLOAT:
  10073. case OP_PARSE_INT64:
  10074. case OP_PARSE_UINT64:
  10075. case OP_PARSE_INT32:
  10076. case OP_PARSE_FIXED64:
  10077. case OP_PARSE_FIXED32:
  10078. case OP_PARSE_BOOL:
  10079. case OP_PARSE_UINT32:
  10080. case OP_PARSE_SFIXED32:
  10081. case OP_PARSE_SFIXED64:
  10082. case OP_PARSE_SINT32:
  10083. case OP_PARSE_SINT64:
  10084. case OP_STARTSEQ:
  10085. case OP_ENDSEQ:
  10086. case OP_STARTSUBMSG:
  10087. case OP_ENDSUBMSG:
  10088. case OP_STARTSTR:
  10089. case OP_STRING:
  10090. case OP_ENDSTR:
  10091. case OP_PUSHTAGDELIM:
  10092. fprintf(f, " %d", instr >> 8);
  10093. break;
  10094. case OP_SETBIGGROUPNUM:
  10095. fprintf(f, " %d", *p++);
  10096. break;
  10097. case OP_CHECKDELIM:
  10098. case OP_CALL:
  10099. case OP_BRANCH:
  10100. fprintf(f, " =>0x%tx", p + getofs(instr) - begin);
  10101. break;
  10102. case OP_TAG1:
  10103. case OP_TAG2: {
  10104. fprintf(f, " tag:0x%x", instr >> 16);
  10105. if (getofs(instr)) {
  10106. fprintf(f, " =>0x%tx", p + getofs(instr) - begin);
  10107. }
  10108. break;
  10109. }
  10110. case OP_TAGN: {
  10111. uint64_t tag = *p++;
  10112. tag |= (uint64_t)*p++ << 32;
  10113. fprintf(f, " tag:0x%llx", (long long)tag);
  10114. fprintf(f, " n:%d", instr >> 16);
  10115. if (getofs(instr)) {
  10116. fprintf(f, " =>0x%tx", p + getofs(instr) - begin);
  10117. }
  10118. break;
  10119. }
  10120. }
  10121. fputs("\n", f);
  10122. }
  10123. }
  10124. #endif
  10125. static uint64_t get_encoded_tag(const upb_fielddef *f, int wire_type) {
  10126. uint32_t tag = (upb_fielddef_number(f) << 3) | wire_type;
  10127. uint64_t encoded_tag = upb_vencode32(tag);
  10128. /* No tag should be greater than 5 bytes. */
  10129. UPB_ASSERT(encoded_tag <= 0xffffffffff);
  10130. return encoded_tag;
  10131. }
  10132. static void putchecktag(compiler *c, const upb_fielddef *f,
  10133. int wire_type, int dest) {
  10134. uint64_t tag = get_encoded_tag(f, wire_type);
  10135. switch (upb_value_size(tag)) {
  10136. case 1:
  10137. putop(c, OP_TAG1, dest, tag);
  10138. break;
  10139. case 2:
  10140. putop(c, OP_TAG2, dest, tag);
  10141. break;
  10142. default:
  10143. putop(c, OP_TAGN, dest, tag);
  10144. break;
  10145. }
  10146. }
  10147. static upb_selector_t getsel(const upb_fielddef *f, upb_handlertype_t type) {
  10148. upb_selector_t selector;
  10149. bool ok = upb_handlers_getselector(f, type, &selector);
  10150. UPB_ASSERT(ok);
  10151. return selector;
  10152. }
  10153. /* Takes an existing, primary dispatch table entry and repacks it with a
  10154. * different alternate wire type. Called when we are inserting a secondary
  10155. * dispatch table entry for an alternate wire type. */
  10156. static uint64_t repack(uint64_t dispatch, int new_wt2) {
  10157. uint64_t ofs;
  10158. uint8_t wt1;
  10159. uint8_t old_wt2;
  10160. upb_pbdecoder_unpackdispatch(dispatch, &ofs, &wt1, &old_wt2);
  10161. UPB_ASSERT(old_wt2 == NO_WIRE_TYPE); /* wt2 should not be set yet. */
  10162. return upb_pbdecoder_packdispatch(ofs, wt1, new_wt2);
  10163. }
  10164. /* Marks the current bytecode position as the dispatch target for this message,
  10165. * field, and wire type. */
  10166. static void dispatchtarget(compiler *c, upb_pbdecodermethod *method,
  10167. const upb_fielddef *f, int wire_type) {
  10168. /* Offset is relative to msg base. */
  10169. uint64_t ofs = pcofs(c) - method->code_base.ofs;
  10170. uint32_t fn = upb_fielddef_number(f);
  10171. upb_inttable *d = &method->dispatch;
  10172. upb_value v;
  10173. if (upb_inttable_remove(d, fn, &v)) {
  10174. /* TODO: prioritize based on packed setting in .proto file. */
  10175. uint64_t repacked = repack(upb_value_getuint64(v), wire_type);
  10176. upb_inttable_insert(d, fn, upb_value_uint64(repacked));
  10177. upb_inttable_insert(d, fn + UPB_MAX_FIELDNUMBER, upb_value_uint64(ofs));
  10178. } else {
  10179. uint64_t val = upb_pbdecoder_packdispatch(ofs, wire_type, NO_WIRE_TYPE);
  10180. upb_inttable_insert(d, fn, upb_value_uint64(val));
  10181. }
  10182. }
  10183. static void putpush(compiler *c, const upb_fielddef *f) {
  10184. if (upb_fielddef_descriptortype(f) == UPB_DESCRIPTOR_TYPE_MESSAGE) {
  10185. putop(c, OP_PUSHLENDELIM);
  10186. } else {
  10187. uint32_t fn = upb_fielddef_number(f);
  10188. if (fn >= 1 << 24) {
  10189. putop(c, OP_PUSHTAGDELIM, 0);
  10190. putop(c, OP_SETBIGGROUPNUM, fn);
  10191. } else {
  10192. putop(c, OP_PUSHTAGDELIM, fn);
  10193. }
  10194. }
  10195. }
  10196. static upb_pbdecodermethod *find_submethod(const compiler *c,
  10197. const upb_pbdecodermethod *method,
  10198. const upb_fielddef *f) {
  10199. const upb_handlers *sub =
  10200. upb_handlers_getsubhandlers(method->dest_handlers_, f);
  10201. upb_value v;
  10202. return upb_inttable_lookupptr(&c->group->methods, sub, &v)
  10203. ? upb_value_getptr(v)
  10204. : NULL;
  10205. }
  10206. static void putsel(compiler *c, opcode op, upb_selector_t sel,
  10207. const upb_handlers *h) {
  10208. if (upb_handlers_gethandler(h, sel)) {
  10209. putop(c, op, sel);
  10210. }
  10211. }
  10212. /* Puts an opcode to call a callback, but only if a callback actually exists for
  10213. * this field and handler type. */
  10214. static void maybeput(compiler *c, opcode op, const upb_handlers *h,
  10215. const upb_fielddef *f, upb_handlertype_t type) {
  10216. putsel(c, op, getsel(f, type), h);
  10217. }
  10218. static bool haslazyhandlers(const upb_handlers *h, const upb_fielddef *f) {
  10219. if (!upb_fielddef_lazy(f))
  10220. return false;
  10221. return upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_STARTSTR)) ||
  10222. upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_STRING)) ||
  10223. upb_handlers_gethandler(h, getsel(f, UPB_HANDLER_ENDSTR));
  10224. }
  10225. /* bytecode compiler code generation ******************************************/
  10226. /* Symbolic names for our local labels. */
  10227. #define LABEL_LOOPSTART 1 /* Top of a repeated field loop. */
  10228. #define LABEL_LOOPBREAK 2 /* To jump out of a repeated loop */
  10229. #define LABEL_FIELD 3 /* Jump backward to find the most recent field. */
  10230. #define LABEL_ENDMSG 4 /* To reach the OP_ENDMSG instr for this msg. */
  10231. /* Generates bytecode to parse a single non-lazy message field. */
  10232. static void generate_msgfield(compiler *c, const upb_fielddef *f,
  10233. upb_pbdecodermethod *method) {
  10234. const upb_handlers *h = upb_pbdecodermethod_desthandlers(method);
  10235. const upb_pbdecodermethod *sub_m = find_submethod(c, method, f);
  10236. int wire_type;
  10237. if (!sub_m) {
  10238. /* Don't emit any code for this field at all; it will be parsed as an
  10239. * unknown field.
  10240. *
  10241. * TODO(haberman): we should change this to parse it as a string field
  10242. * instead. It will probably be faster, but more importantly, once we
  10243. * start vending unknown fields, a field shouldn't be treated as unknown
  10244. * just because it doesn't have subhandlers registered. */
  10245. return;
  10246. }
  10247. label(c, LABEL_FIELD);
  10248. wire_type =
  10249. (upb_fielddef_descriptortype(f) == UPB_DESCRIPTOR_TYPE_MESSAGE)
  10250. ? UPB_WIRE_TYPE_DELIMITED
  10251. : UPB_WIRE_TYPE_START_GROUP;
  10252. if (upb_fielddef_isseq(f)) {
  10253. putop(c, OP_CHECKDELIM, LABEL_ENDMSG);
  10254. putchecktag(c, f, wire_type, LABEL_DISPATCH);
  10255. dispatchtarget(c, method, f, wire_type);
  10256. putop(c, OP_PUSHTAGDELIM, 0);
  10257. putop(c, OP_STARTSEQ, getsel(f, UPB_HANDLER_STARTSEQ));
  10258. label(c, LABEL_LOOPSTART);
  10259. putpush(c, f);
  10260. putop(c, OP_STARTSUBMSG, getsel(f, UPB_HANDLER_STARTSUBMSG));
  10261. putop(c, OP_CALL, sub_m);
  10262. putop(c, OP_POP);
  10263. maybeput(c, OP_ENDSUBMSG, h, f, UPB_HANDLER_ENDSUBMSG);
  10264. if (wire_type == UPB_WIRE_TYPE_DELIMITED) {
  10265. putop(c, OP_SETDELIM);
  10266. }
  10267. putop(c, OP_CHECKDELIM, LABEL_LOOPBREAK);
  10268. putchecktag(c, f, wire_type, LABEL_LOOPBREAK);
  10269. putop(c, OP_BRANCH, -LABEL_LOOPSTART);
  10270. label(c, LABEL_LOOPBREAK);
  10271. putop(c, OP_POP);
  10272. maybeput(c, OP_ENDSEQ, h, f, UPB_HANDLER_ENDSEQ);
  10273. } else {
  10274. putop(c, OP_CHECKDELIM, LABEL_ENDMSG);
  10275. putchecktag(c, f, wire_type, LABEL_DISPATCH);
  10276. dispatchtarget(c, method, f, wire_type);
  10277. putpush(c, f);
  10278. putop(c, OP_STARTSUBMSG, getsel(f, UPB_HANDLER_STARTSUBMSG));
  10279. putop(c, OP_CALL, sub_m);
  10280. putop(c, OP_POP);
  10281. maybeput(c, OP_ENDSUBMSG, h, f, UPB_HANDLER_ENDSUBMSG);
  10282. if (wire_type == UPB_WIRE_TYPE_DELIMITED) {
  10283. putop(c, OP_SETDELIM);
  10284. }
  10285. }
  10286. }
  10287. /* Generates bytecode to parse a single string or lazy submessage field. */
  10288. static void generate_delimfield(compiler *c, const upb_fielddef *f,
  10289. upb_pbdecodermethod *method) {
  10290. const upb_handlers *h = upb_pbdecodermethod_desthandlers(method);
  10291. label(c, LABEL_FIELD);
  10292. if (upb_fielddef_isseq(f)) {
  10293. putop(c, OP_CHECKDELIM, LABEL_ENDMSG);
  10294. putchecktag(c, f, UPB_WIRE_TYPE_DELIMITED, LABEL_DISPATCH);
  10295. dispatchtarget(c, method, f, UPB_WIRE_TYPE_DELIMITED);
  10296. putop(c, OP_PUSHTAGDELIM, 0);
  10297. putop(c, OP_STARTSEQ, getsel(f, UPB_HANDLER_STARTSEQ));
  10298. label(c, LABEL_LOOPSTART);
  10299. putop(c, OP_PUSHLENDELIM);
  10300. putop(c, OP_STARTSTR, getsel(f, UPB_HANDLER_STARTSTR));
  10301. /* Need to emit even if no handler to skip past the string. */
  10302. putop(c, OP_STRING, getsel(f, UPB_HANDLER_STRING));
  10303. putop(c, OP_POP);
  10304. maybeput(c, OP_ENDSTR, h, f, UPB_HANDLER_ENDSTR);
  10305. putop(c, OP_SETDELIM);
  10306. putop(c, OP_CHECKDELIM, LABEL_LOOPBREAK);
  10307. putchecktag(c, f, UPB_WIRE_TYPE_DELIMITED, LABEL_LOOPBREAK);
  10308. putop(c, OP_BRANCH, -LABEL_LOOPSTART);
  10309. label(c, LABEL_LOOPBREAK);
  10310. putop(c, OP_POP);
  10311. maybeput(c, OP_ENDSEQ, h, f, UPB_HANDLER_ENDSEQ);
  10312. } else {
  10313. putop(c, OP_CHECKDELIM, LABEL_ENDMSG);
  10314. putchecktag(c, f, UPB_WIRE_TYPE_DELIMITED, LABEL_DISPATCH);
  10315. dispatchtarget(c, method, f, UPB_WIRE_TYPE_DELIMITED);
  10316. putop(c, OP_PUSHLENDELIM);
  10317. putop(c, OP_STARTSTR, getsel(f, UPB_HANDLER_STARTSTR));
  10318. putop(c, OP_STRING, getsel(f, UPB_HANDLER_STRING));
  10319. putop(c, OP_POP);
  10320. maybeput(c, OP_ENDSTR, h, f, UPB_HANDLER_ENDSTR);
  10321. putop(c, OP_SETDELIM);
  10322. }
  10323. }
  10324. /* Generates bytecode to parse a single primitive field. */
  10325. static void generate_primitivefield(compiler *c, const upb_fielddef *f,
  10326. upb_pbdecodermethod *method) {
  10327. const upb_handlers *h = upb_pbdecodermethod_desthandlers(method);
  10328. upb_descriptortype_t descriptor_type = upb_fielddef_descriptortype(f);
  10329. opcode parse_type;
  10330. upb_selector_t sel;
  10331. int wire_type;
  10332. label(c, LABEL_FIELD);
  10333. /* From a decoding perspective, ENUM is the same as INT32. */
  10334. if (descriptor_type == UPB_DESCRIPTOR_TYPE_ENUM)
  10335. descriptor_type = UPB_DESCRIPTOR_TYPE_INT32;
  10336. parse_type = (opcode)descriptor_type;
  10337. /* TODO(haberman): generate packed or non-packed first depending on "packed"
  10338. * setting in the fielddef. This will favor (in speed) whichever was
  10339. * specified. */
  10340. UPB_ASSERT((int)parse_type >= 0 && parse_type <= OP_MAX);
  10341. sel = getsel(f, upb_handlers_getprimitivehandlertype(f));
  10342. wire_type = upb_pb_native_wire_types[upb_fielddef_descriptortype(f)];
  10343. if (upb_fielddef_isseq(f)) {
  10344. putop(c, OP_CHECKDELIM, LABEL_ENDMSG);
  10345. putchecktag(c, f, UPB_WIRE_TYPE_DELIMITED, LABEL_DISPATCH);
  10346. dispatchtarget(c, method, f, UPB_WIRE_TYPE_DELIMITED);
  10347. putop(c, OP_PUSHLENDELIM);
  10348. putop(c, OP_STARTSEQ, getsel(f, UPB_HANDLER_STARTSEQ)); /* Packed */
  10349. label(c, LABEL_LOOPSTART);
  10350. putop(c, parse_type, sel);
  10351. putop(c, OP_CHECKDELIM, LABEL_LOOPBREAK);
  10352. putop(c, OP_BRANCH, -LABEL_LOOPSTART);
  10353. dispatchtarget(c, method, f, wire_type);
  10354. putop(c, OP_PUSHTAGDELIM, 0);
  10355. putop(c, OP_STARTSEQ, getsel(f, UPB_HANDLER_STARTSEQ)); /* Non-packed */
  10356. label(c, LABEL_LOOPSTART);
  10357. putop(c, parse_type, sel);
  10358. putop(c, OP_CHECKDELIM, LABEL_LOOPBREAK);
  10359. putchecktag(c, f, wire_type, LABEL_LOOPBREAK);
  10360. putop(c, OP_BRANCH, -LABEL_LOOPSTART);
  10361. label(c, LABEL_LOOPBREAK);
  10362. putop(c, OP_POP); /* Packed and non-packed join. */
  10363. maybeput(c, OP_ENDSEQ, h, f, UPB_HANDLER_ENDSEQ);
  10364. putop(c, OP_SETDELIM); /* Could remove for non-packed by dup ENDSEQ. */
  10365. } else {
  10366. putop(c, OP_CHECKDELIM, LABEL_ENDMSG);
  10367. putchecktag(c, f, wire_type, LABEL_DISPATCH);
  10368. dispatchtarget(c, method, f, wire_type);
  10369. putop(c, parse_type, sel);
  10370. }
  10371. }
  10372. /* Adds bytecode for parsing the given message to the given decoderplan,
  10373. * while adding all dispatch targets to this message's dispatch table. */
  10374. static void compile_method(compiler *c, upb_pbdecodermethod *method) {
  10375. const upb_handlers *h;
  10376. const upb_msgdef *md;
  10377. uint32_t* start_pc;
  10378. upb_msg_field_iter i;
  10379. upb_value val;
  10380. UPB_ASSERT(method);
  10381. /* Clear all entries in the dispatch table. */
  10382. upb_inttable_uninit(&method->dispatch);
  10383. upb_inttable_init(&method->dispatch, UPB_CTYPE_UINT64);
  10384. h = upb_pbdecodermethod_desthandlers(method);
  10385. md = upb_handlers_msgdef(h);
  10386. method->code_base.ofs = pcofs(c);
  10387. putop(c, OP_SETDISPATCH, &method->dispatch);
  10388. putsel(c, OP_STARTMSG, UPB_STARTMSG_SELECTOR, h);
  10389. label(c, LABEL_FIELD);
  10390. start_pc = c->pc;
  10391. for(upb_msg_field_begin(&i, md);
  10392. !upb_msg_field_done(&i);
  10393. upb_msg_field_next(&i)) {
  10394. const upb_fielddef *f = upb_msg_iter_field(&i);
  10395. upb_fieldtype_t type = upb_fielddef_type(f);
  10396. if (type == UPB_TYPE_MESSAGE && !(haslazyhandlers(h, f) && c->lazy)) {
  10397. generate_msgfield(c, f, method);
  10398. } else if (type == UPB_TYPE_STRING || type == UPB_TYPE_BYTES ||
  10399. type == UPB_TYPE_MESSAGE) {
  10400. generate_delimfield(c, f, method);
  10401. } else {
  10402. generate_primitivefield(c, f, method);
  10403. }
  10404. }
  10405. /* If there were no fields, or if no handlers were defined, we need to
  10406. * generate a non-empty loop body so that we can at least dispatch for unknown
  10407. * fields and check for the end of the message. */
  10408. if (c->pc == start_pc) {
  10409. /* Check for end-of-message. */
  10410. putop(c, OP_CHECKDELIM, LABEL_ENDMSG);
  10411. /* Unconditionally dispatch. */
  10412. putop(c, OP_DISPATCH, 0);
  10413. }
  10414. /* For now we just loop back to the last field of the message (or if none,
  10415. * the DISPATCH opcode for the message). */
  10416. putop(c, OP_BRANCH, -LABEL_FIELD);
  10417. /* Insert both a label and a dispatch table entry for this end-of-msg. */
  10418. label(c, LABEL_ENDMSG);
  10419. val = upb_value_uint64(pcofs(c) - method->code_base.ofs);
  10420. upb_inttable_insert(&method->dispatch, DISPATCH_ENDMSG, val);
  10421. putsel(c, OP_ENDMSG, UPB_ENDMSG_SELECTOR, h);
  10422. putop(c, OP_RET);
  10423. upb_inttable_compact(&method->dispatch);
  10424. }
  10425. /* Populate "methods" with new upb_pbdecodermethod objects reachable from "h".
  10426. * Returns the method for these handlers.
  10427. *
  10428. * Generates a new method for every destination handlers reachable from "h". */
  10429. static void find_methods(compiler *c, const upb_handlers *h) {
  10430. upb_value v;
  10431. upb_msg_field_iter i;
  10432. const upb_msgdef *md;
  10433. if (upb_inttable_lookupptr(&c->group->methods, h, &v))
  10434. return;
  10435. newmethod(h, c->group);
  10436. /* Find submethods. */
  10437. md = upb_handlers_msgdef(h);
  10438. for(upb_msg_field_begin(&i, md);
  10439. !upb_msg_field_done(&i);
  10440. upb_msg_field_next(&i)) {
  10441. const upb_fielddef *f = upb_msg_iter_field(&i);
  10442. const upb_handlers *sub_h;
  10443. if (upb_fielddef_type(f) == UPB_TYPE_MESSAGE &&
  10444. (sub_h = upb_handlers_getsubhandlers(h, f)) != NULL) {
  10445. /* We only generate a decoder method for submessages with handlers.
  10446. * Others will be parsed as unknown fields. */
  10447. find_methods(c, sub_h);
  10448. }
  10449. }
  10450. }
  10451. /* (Re-)compile bytecode for all messages in "msgs."
  10452. * Overwrites any existing bytecode in "c". */
  10453. static void compile_methods(compiler *c) {
  10454. upb_inttable_iter i;
  10455. /* Start over at the beginning of the bytecode. */
  10456. c->pc = c->group->bytecode;
  10457. upb_inttable_begin(&i, &c->group->methods);
  10458. for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  10459. upb_pbdecodermethod *method = upb_value_getptr(upb_inttable_iter_value(&i));
  10460. compile_method(c, method);
  10461. }
  10462. }
  10463. static void set_bytecode_handlers(mgroup *g) {
  10464. upb_inttable_iter i;
  10465. upb_inttable_begin(&i, &g->methods);
  10466. for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  10467. upb_pbdecodermethod *m = upb_value_getptr(upb_inttable_iter_value(&i));
  10468. upb_byteshandler *h = &m->input_handler_;
  10469. m->code_base.ptr = g->bytecode + m->code_base.ofs;
  10470. upb_byteshandler_setstartstr(h, upb_pbdecoder_startbc, m->code_base.ptr);
  10471. upb_byteshandler_setstring(h, upb_pbdecoder_decode, g);
  10472. upb_byteshandler_setendstr(h, upb_pbdecoder_end, m);
  10473. }
  10474. }
  10475. /* JIT setup. *****************************************************************/
  10476. #ifdef UPB_USE_JIT_X64
  10477. static void sethandlers(mgroup *g, bool allowjit) {
  10478. g->jit_code = NULL;
  10479. if (allowjit) {
  10480. /* Compile byte-code into machine code, create handlers. */
  10481. upb_pbdecoder_jit(g);
  10482. } else {
  10483. set_bytecode_handlers(g);
  10484. }
  10485. }
  10486. #else /* UPB_USE_JIT_X64 */
  10487. static void sethandlers(mgroup *g, bool allowjit) {
  10488. /* No JIT compiled in; use bytecode handlers unconditionally. */
  10489. UPB_UNUSED(allowjit);
  10490. set_bytecode_handlers(g);
  10491. }
  10492. #endif /* UPB_USE_JIT_X64 */
  10493. /* TODO(haberman): allow this to be constructed for an arbitrary set of dest
  10494. * handlers and other mgroups (but verify we have a transitive closure). */
  10495. const mgroup *mgroup_new(const upb_handlers *dest, bool allowjit, bool lazy,
  10496. const void *owner) {
  10497. mgroup *g;
  10498. compiler *c;
  10499. UPB_UNUSED(allowjit);
  10500. UPB_ASSERT(upb_handlers_isfrozen(dest));
  10501. g = newgroup(owner);
  10502. c = newcompiler(g, lazy);
  10503. find_methods(c, dest);
  10504. /* We compile in two passes:
  10505. * 1. all messages are assigned relative offsets from the beginning of the
  10506. * bytecode (saved in method->code_base).
  10507. * 2. forwards OP_CALL instructions can be correctly linked since message
  10508. * offsets have been previously assigned.
  10509. *
  10510. * Could avoid the second pass by linking OP_CALL instructions somehow. */
  10511. compile_methods(c);
  10512. compile_methods(c);
  10513. g->bytecode_end = c->pc;
  10514. freecompiler(c);
  10515. #ifdef UPB_DUMP_BYTECODE
  10516. {
  10517. FILE *f = fopen("/tmp/upb-bytecode", "w");
  10518. UPB_ASSERT(f);
  10519. dumpbc(g->bytecode, g->bytecode_end, stderr);
  10520. dumpbc(g->bytecode, g->bytecode_end, f);
  10521. fclose(f);
  10522. f = fopen("/tmp/upb-bytecode.bin", "wb");
  10523. UPB_ASSERT(f);
  10524. fwrite(g->bytecode, 1, g->bytecode_end - g->bytecode, f);
  10525. fclose(f);
  10526. }
  10527. #endif
  10528. sethandlers(g, allowjit);
  10529. return g;
  10530. }
  10531. /* upb_pbcodecache ************************************************************/
  10532. void upb_pbcodecache_init(upb_pbcodecache *c) {
  10533. upb_inttable_init(&c->groups, UPB_CTYPE_CONSTPTR);
  10534. c->allow_jit_ = true;
  10535. }
  10536. void upb_pbcodecache_uninit(upb_pbcodecache *c) {
  10537. upb_inttable_iter i;
  10538. upb_inttable_begin(&i, &c->groups);
  10539. for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  10540. const mgroup *group = upb_value_getconstptr(upb_inttable_iter_value(&i));
  10541. mgroup_unref(group, c);
  10542. }
  10543. upb_inttable_uninit(&c->groups);
  10544. }
  10545. bool upb_pbcodecache_allowjit(const upb_pbcodecache *c) {
  10546. return c->allow_jit_;
  10547. }
  10548. bool upb_pbcodecache_setallowjit(upb_pbcodecache *c, bool allow) {
  10549. if (upb_inttable_count(&c->groups) > 0)
  10550. return false;
  10551. c->allow_jit_ = allow;
  10552. return true;
  10553. }
  10554. const upb_pbdecodermethod *upb_pbcodecache_getdecodermethod(
  10555. upb_pbcodecache *c, const upb_pbdecodermethodopts *opts) {
  10556. upb_value v;
  10557. bool ok;
  10558. /* Right now we build a new DecoderMethod every time.
  10559. * TODO(haberman): properly cache methods by their true key. */
  10560. const mgroup *g = mgroup_new(opts->handlers, c->allow_jit_, opts->lazy, c);
  10561. upb_inttable_push(&c->groups, upb_value_constptr(g));
  10562. ok = upb_inttable_lookupptr(&g->methods, opts->handlers, &v);
  10563. UPB_ASSERT(ok);
  10564. return upb_value_getptr(v);
  10565. }
  10566. /* upb_pbdecodermethodopts ****************************************************/
  10567. void upb_pbdecodermethodopts_init(upb_pbdecodermethodopts *opts,
  10568. const upb_handlers *h) {
  10569. opts->handlers = h;
  10570. opts->lazy = false;
  10571. }
  10572. void upb_pbdecodermethodopts_setlazy(upb_pbdecodermethodopts *opts, bool lazy) {
  10573. opts->lazy = lazy;
  10574. }
  10575. /*
  10576. ** upb::Decoder (Bytecode Decoder VM)
  10577. **
  10578. ** Bytecode must previously have been generated using the bytecode compiler in
  10579. ** compile_decoder.c. This decoder then walks through the bytecode op-by-op to
  10580. ** parse the input.
  10581. **
  10582. ** Decoding is fully resumable; we just keep a pointer to the current bytecode
  10583. ** instruction and resume from there. A fair amount of the logic here is to
  10584. ** handle the fact that values can span buffer seams and we have to be able to
  10585. ** be capable of suspending/resuming from any byte in the stream. This
  10586. ** sometimes requires keeping a few trailing bytes from the last buffer around
  10587. ** in the "residual" buffer.
  10588. */
  10589. #include <inttypes.h>
  10590. #include <stddef.h>
  10591. #ifdef UPB_DUMP_BYTECODE
  10592. #include <stdio.h>
  10593. #endif
  10594. #define CHECK_SUSPEND(x) if (!(x)) return upb_pbdecoder_suspend(d);
  10595. /* Error messages that are shared between the bytecode and JIT decoders. */
  10596. const char *kPbDecoderStackOverflow = "Nesting too deep.";
  10597. const char *kPbDecoderSubmessageTooLong =
  10598. "Submessage end extends past enclosing submessage.";
  10599. /* Error messages shared within this file. */
  10600. static const char *kUnterminatedVarint = "Unterminated varint.";
  10601. /* upb_pbdecoder **************************************************************/
  10602. static opcode halt = OP_HALT;
  10603. /* A dummy character we can point to when the user passes us a NULL buffer.
  10604. * We need this because in C (NULL + 0) and (NULL - NULL) are undefined
  10605. * behavior, which would invalidate functions like curbufleft(). */
  10606. static const char dummy_char;
  10607. /* Whether an op consumes any of the input buffer. */
  10608. static bool consumes_input(opcode op) {
  10609. switch (op) {
  10610. case OP_SETDISPATCH:
  10611. case OP_STARTMSG:
  10612. case OP_ENDMSG:
  10613. case OP_STARTSEQ:
  10614. case OP_ENDSEQ:
  10615. case OP_STARTSUBMSG:
  10616. case OP_ENDSUBMSG:
  10617. case OP_STARTSTR:
  10618. case OP_ENDSTR:
  10619. case OP_PUSHTAGDELIM:
  10620. case OP_POP:
  10621. case OP_SETDELIM:
  10622. case OP_SETBIGGROUPNUM:
  10623. case OP_CHECKDELIM:
  10624. case OP_CALL:
  10625. case OP_RET:
  10626. case OP_BRANCH:
  10627. return false;
  10628. default:
  10629. return true;
  10630. }
  10631. }
  10632. static size_t stacksize(upb_pbdecoder *d, size_t entries) {
  10633. UPB_UNUSED(d);
  10634. return entries * sizeof(upb_pbdecoder_frame);
  10635. }
  10636. static size_t callstacksize(upb_pbdecoder *d, size_t entries) {
  10637. UPB_UNUSED(d);
  10638. #ifdef UPB_USE_JIT_X64
  10639. if (d->method_->is_native_) {
  10640. /* Each native stack frame needs two pointers, plus we need a few frames for
  10641. * the enter/exit trampolines. */
  10642. size_t ret = entries * sizeof(void*) * 2;
  10643. ret += sizeof(void*) * 10;
  10644. return ret;
  10645. }
  10646. #endif
  10647. return entries * sizeof(uint32_t*);
  10648. }
  10649. static bool in_residual_buf(const upb_pbdecoder *d, const char *p);
  10650. /* It's unfortunate that we have to micro-manage the compiler with
  10651. * UPB_FORCEINLINE and UPB_NOINLINE, especially since this tuning is necessarily
  10652. * specific to one hardware configuration. But empirically on a Core i7,
  10653. * performance increases 30-50% with these annotations. Every instance where
  10654. * these appear, gcc 4.2.1 made the wrong decision and degraded performance in
  10655. * benchmarks. */
  10656. static void seterr(upb_pbdecoder *d, const char *msg) {
  10657. upb_status status = UPB_STATUS_INIT;
  10658. upb_status_seterrmsg(&status, msg);
  10659. upb_env_reporterror(d->env, &status);
  10660. }
  10661. void upb_pbdecoder_seterr(upb_pbdecoder *d, const char *msg) {
  10662. seterr(d, msg);
  10663. }
  10664. /* Buffering ******************************************************************/
  10665. /* We operate on one buffer at a time, which is either the user's buffer passed
  10666. * to our "decode" callback or some residual bytes from the previous buffer. */
  10667. /* How many bytes can be safely read from d->ptr without reading past end-of-buf
  10668. * or past the current delimited end. */
  10669. static size_t curbufleft(const upb_pbdecoder *d) {
  10670. UPB_ASSERT(d->data_end >= d->ptr);
  10671. return d->data_end - d->ptr;
  10672. }
  10673. /* How many bytes are available before end-of-buffer. */
  10674. static size_t bufleft(const upb_pbdecoder *d) {
  10675. return d->end - d->ptr;
  10676. }
  10677. /* Overall stream offset of d->ptr. */
  10678. uint64_t offset(const upb_pbdecoder *d) {
  10679. return d->bufstart_ofs + (d->ptr - d->buf);
  10680. }
  10681. /* How many bytes are available before the end of this delimited region. */
  10682. size_t delim_remaining(const upb_pbdecoder *d) {
  10683. return d->top->end_ofs - offset(d);
  10684. }
  10685. /* Advances d->ptr. */
  10686. static void advance(upb_pbdecoder *d, size_t len) {
  10687. UPB_ASSERT(curbufleft(d) >= len);
  10688. d->ptr += len;
  10689. }
  10690. static bool in_buf(const char *p, const char *buf, const char *end) {
  10691. return p >= buf && p <= end;
  10692. }
  10693. static bool in_residual_buf(const upb_pbdecoder *d, const char *p) {
  10694. return in_buf(p, d->residual, d->residual_end);
  10695. }
  10696. /* Calculates the delim_end value, which is affected by both the current buffer
  10697. * and the parsing stack, so must be called whenever either is updated. */
  10698. static void set_delim_end(upb_pbdecoder *d) {
  10699. size_t delim_ofs = d->top->end_ofs - d->bufstart_ofs;
  10700. if (delim_ofs <= (size_t)(d->end - d->buf)) {
  10701. d->delim_end = d->buf + delim_ofs;
  10702. d->data_end = d->delim_end;
  10703. } else {
  10704. d->data_end = d->end;
  10705. d->delim_end = NULL;
  10706. }
  10707. }
  10708. static void switchtobuf(upb_pbdecoder *d, const char *buf, const char *end) {
  10709. d->ptr = buf;
  10710. d->buf = buf;
  10711. d->end = end;
  10712. set_delim_end(d);
  10713. }
  10714. static void advancetobuf(upb_pbdecoder *d, const char *buf, size_t len) {
  10715. UPB_ASSERT(curbufleft(d) == 0);
  10716. d->bufstart_ofs += (d->end - d->buf);
  10717. switchtobuf(d, buf, buf + len);
  10718. }
  10719. static void checkpoint(upb_pbdecoder *d) {
  10720. /* The assertion here is in the interests of efficiency, not correctness.
  10721. * We are trying to ensure that we don't checkpoint() more often than
  10722. * necessary. */
  10723. UPB_ASSERT(d->checkpoint != d->ptr);
  10724. d->checkpoint = d->ptr;
  10725. }
  10726. /* Skips "bytes" bytes in the stream, which may be more than available. If we
  10727. * skip more bytes than are available, we return a long read count to the caller
  10728. * indicating how many bytes can be skipped over before passing actual data
  10729. * again. Skipped bytes can pass a NULL buffer and the decoder guarantees they
  10730. * won't actually be read.
  10731. */
  10732. static int32_t skip(upb_pbdecoder *d, size_t bytes) {
  10733. UPB_ASSERT(!in_residual_buf(d, d->ptr) || d->size_param == 0);
  10734. UPB_ASSERT(d->skip == 0);
  10735. if (bytes > delim_remaining(d)) {
  10736. seterr(d, "Skipped value extended beyond enclosing submessage.");
  10737. return upb_pbdecoder_suspend(d);
  10738. } else if (bufleft(d) >= bytes) {
  10739. /* Skipped data is all in current buffer, and more is still available. */
  10740. advance(d, bytes);
  10741. d->skip = 0;
  10742. return DECODE_OK;
  10743. } else {
  10744. /* Skipped data extends beyond currently available buffers. */
  10745. d->pc = d->last;
  10746. d->skip = bytes - curbufleft(d);
  10747. d->bufstart_ofs += (d->end - d->buf);
  10748. d->residual_end = d->residual;
  10749. switchtobuf(d, d->residual, d->residual_end);
  10750. return d->size_param + d->skip;
  10751. }
  10752. }
  10753. /* Resumes the decoder from an initial state or from a previous suspend. */
  10754. int32_t upb_pbdecoder_resume(upb_pbdecoder *d, void *p, const char *buf,
  10755. size_t size, const upb_bufhandle *handle) {
  10756. UPB_UNUSED(p); /* Useless; just for the benefit of the JIT. */
  10757. /* d->skip and d->residual_end could probably elegantly be represented
  10758. * as a single variable, to more easily represent this invariant. */
  10759. UPB_ASSERT(!(d->skip && d->residual_end > d->residual));
  10760. /* We need to remember the original size_param, so that the value we return
  10761. * is relative to it, even if we do some skipping first. */
  10762. d->size_param = size;
  10763. d->handle = handle;
  10764. /* Have to handle this case specially (ie. not with skip()) because the user
  10765. * is allowed to pass a NULL buffer here, which won't allow us to safely
  10766. * calculate a d->end or use our normal functions like curbufleft(). */
  10767. if (d->skip && d->skip >= size) {
  10768. d->skip -= size;
  10769. d->bufstart_ofs += size;
  10770. buf = &dummy_char;
  10771. size = 0;
  10772. /* We can't just return now, because we might need to execute some ops
  10773. * like CHECKDELIM, which could call some callbacks and pop the stack. */
  10774. }
  10775. /* We need to pretend that this was the actual buffer param, since some of the
  10776. * calculations assume that d->ptr/d->buf is relative to this. */
  10777. d->buf_param = buf;
  10778. if (!buf) {
  10779. /* NULL buf is ok if its entire span is covered by the "skip" above, but
  10780. * by this point we know that "skip" doesn't cover the buffer. */
  10781. seterr(d, "Passed NULL buffer over non-skippable region.");
  10782. return upb_pbdecoder_suspend(d);
  10783. }
  10784. if (d->residual_end > d->residual) {
  10785. /* We have residual bytes from the last buffer. */
  10786. UPB_ASSERT(d->ptr == d->residual);
  10787. } else {
  10788. switchtobuf(d, buf, buf + size);
  10789. }
  10790. d->checkpoint = d->ptr;
  10791. /* Handle skips that don't cover the whole buffer (as above). */
  10792. if (d->skip) {
  10793. size_t skip_bytes = d->skip;
  10794. d->skip = 0;
  10795. CHECK_RETURN(skip(d, skip_bytes));
  10796. checkpoint(d);
  10797. }
  10798. /* If we're inside an unknown group, continue to parse unknown values. */
  10799. if (d->top->groupnum < 0) {
  10800. CHECK_RETURN(upb_pbdecoder_skipunknown(d, -1, 0));
  10801. checkpoint(d);
  10802. }
  10803. return DECODE_OK;
  10804. }
  10805. /* Suspends the decoder at the last checkpoint, without saving any residual
  10806. * bytes. If there are any unconsumed bytes, returns a short byte count. */
  10807. size_t upb_pbdecoder_suspend(upb_pbdecoder *d) {
  10808. d->pc = d->last;
  10809. if (d->checkpoint == d->residual) {
  10810. /* Checkpoint was in residual buf; no user bytes were consumed. */
  10811. d->ptr = d->residual;
  10812. return 0;
  10813. } else {
  10814. size_t ret = d->size_param - (d->end - d->checkpoint);
  10815. UPB_ASSERT(!in_residual_buf(d, d->checkpoint));
  10816. UPB_ASSERT(d->buf == d->buf_param || d->buf == &dummy_char);
  10817. d->bufstart_ofs += (d->checkpoint - d->buf);
  10818. d->residual_end = d->residual;
  10819. switchtobuf(d, d->residual, d->residual_end);
  10820. return ret;
  10821. }
  10822. }
  10823. /* Suspends the decoder at the last checkpoint, and saves any unconsumed
  10824. * bytes in our residual buffer. This is necessary if we need more user
  10825. * bytes to form a complete value, which might not be contiguous in the
  10826. * user's buffers. Always consumes all user bytes. */
  10827. static size_t suspend_save(upb_pbdecoder *d) {
  10828. /* We hit end-of-buffer before we could parse a full value.
  10829. * Save any unconsumed bytes (if any) to the residual buffer. */
  10830. d->pc = d->last;
  10831. if (d->checkpoint == d->residual) {
  10832. /* Checkpoint was in residual buf; append user byte(s) to residual buf. */
  10833. UPB_ASSERT((d->residual_end - d->residual) + d->size_param <=
  10834. sizeof(d->residual));
  10835. if (!in_residual_buf(d, d->ptr)) {
  10836. d->bufstart_ofs -= (d->residual_end - d->residual);
  10837. }
  10838. memcpy(d->residual_end, d->buf_param, d->size_param);
  10839. d->residual_end += d->size_param;
  10840. } else {
  10841. /* Checkpoint was in user buf; old residual bytes not needed. */
  10842. size_t save;
  10843. UPB_ASSERT(!in_residual_buf(d, d->checkpoint));
  10844. d->ptr = d->checkpoint;
  10845. save = curbufleft(d);
  10846. UPB_ASSERT(save <= sizeof(d->residual));
  10847. memcpy(d->residual, d->ptr, save);
  10848. d->residual_end = d->residual + save;
  10849. d->bufstart_ofs = offset(d);
  10850. }
  10851. switchtobuf(d, d->residual, d->residual_end);
  10852. return d->size_param;
  10853. }
  10854. /* Copies the next "bytes" bytes into "buf" and advances the stream.
  10855. * Requires that this many bytes are available in the current buffer. */
  10856. UPB_FORCEINLINE static void consumebytes(upb_pbdecoder *d, void *buf,
  10857. size_t bytes) {
  10858. UPB_ASSERT(bytes <= curbufleft(d));
  10859. memcpy(buf, d->ptr, bytes);
  10860. advance(d, bytes);
  10861. }
  10862. /* Slow path for getting the next "bytes" bytes, regardless of whether they are
  10863. * available in the current buffer or not. Returns a status code as described
  10864. * in decoder.int.h. */
  10865. UPB_NOINLINE static int32_t getbytes_slow(upb_pbdecoder *d, void *buf,
  10866. size_t bytes) {
  10867. const size_t avail = curbufleft(d);
  10868. consumebytes(d, buf, avail);
  10869. bytes -= avail;
  10870. UPB_ASSERT(bytes > 0);
  10871. if (in_residual_buf(d, d->ptr)) {
  10872. advancetobuf(d, d->buf_param, d->size_param);
  10873. }
  10874. if (curbufleft(d) >= bytes) {
  10875. consumebytes(d, (char *)buf + avail, bytes);
  10876. return DECODE_OK;
  10877. } else if (d->data_end == d->delim_end) {
  10878. seterr(d, "Submessage ended in the middle of a value or group");
  10879. return upb_pbdecoder_suspend(d);
  10880. } else {
  10881. return suspend_save(d);
  10882. }
  10883. }
  10884. /* Gets the next "bytes" bytes, regardless of whether they are available in the
  10885. * current buffer or not. Returns a status code as described in decoder.int.h.
  10886. */
  10887. UPB_FORCEINLINE static int32_t getbytes(upb_pbdecoder *d, void *buf,
  10888. size_t bytes) {
  10889. if (curbufleft(d) >= bytes) {
  10890. /* Buffer has enough data to satisfy. */
  10891. consumebytes(d, buf, bytes);
  10892. return DECODE_OK;
  10893. } else {
  10894. return getbytes_slow(d, buf, bytes);
  10895. }
  10896. }
  10897. UPB_NOINLINE static size_t peekbytes_slow(upb_pbdecoder *d, void *buf,
  10898. size_t bytes) {
  10899. size_t ret = curbufleft(d);
  10900. memcpy(buf, d->ptr, ret);
  10901. if (in_residual_buf(d, d->ptr)) {
  10902. size_t copy = UPB_MIN(bytes - ret, d->size_param);
  10903. memcpy((char *)buf + ret, d->buf_param, copy);
  10904. ret += copy;
  10905. }
  10906. return ret;
  10907. }
  10908. UPB_FORCEINLINE static size_t peekbytes(upb_pbdecoder *d, void *buf,
  10909. size_t bytes) {
  10910. if (curbufleft(d) >= bytes) {
  10911. memcpy(buf, d->ptr, bytes);
  10912. return bytes;
  10913. } else {
  10914. return peekbytes_slow(d, buf, bytes);
  10915. }
  10916. }
  10917. /* Decoding of wire types *****************************************************/
  10918. /* Slow path for decoding a varint from the current buffer position.
  10919. * Returns a status code as described in decoder.int.h. */
  10920. UPB_NOINLINE int32_t upb_pbdecoder_decode_varint_slow(upb_pbdecoder *d,
  10921. uint64_t *u64) {
  10922. uint8_t byte = 0x80;
  10923. int bitpos;
  10924. *u64 = 0;
  10925. for(bitpos = 0; bitpos < 70 && (byte & 0x80); bitpos += 7) {
  10926. CHECK_RETURN(getbytes(d, &byte, 1));
  10927. *u64 |= (uint64_t)(byte & 0x7F) << bitpos;
  10928. }
  10929. if(bitpos == 70 && (byte & 0x80)) {
  10930. seterr(d, kUnterminatedVarint);
  10931. return upb_pbdecoder_suspend(d);
  10932. }
  10933. return DECODE_OK;
  10934. }
  10935. /* Decodes a varint from the current buffer position.
  10936. * Returns a status code as described in decoder.int.h. */
  10937. UPB_FORCEINLINE static int32_t decode_varint(upb_pbdecoder *d, uint64_t *u64) {
  10938. if (curbufleft(d) > 0 && !(*d->ptr & 0x80)) {
  10939. *u64 = *d->ptr;
  10940. advance(d, 1);
  10941. return DECODE_OK;
  10942. } else if (curbufleft(d) >= 10) {
  10943. /* Fast case. */
  10944. upb_decoderet r = upb_vdecode_fast(d->ptr);
  10945. if (r.p == NULL) {
  10946. seterr(d, kUnterminatedVarint);
  10947. return upb_pbdecoder_suspend(d);
  10948. }
  10949. advance(d, r.p - d->ptr);
  10950. *u64 = r.val;
  10951. return DECODE_OK;
  10952. } else {
  10953. /* Slow case -- varint spans buffer seam. */
  10954. return upb_pbdecoder_decode_varint_slow(d, u64);
  10955. }
  10956. }
  10957. /* Decodes a 32-bit varint from the current buffer position.
  10958. * Returns a status code as described in decoder.int.h. */
  10959. UPB_FORCEINLINE static int32_t decode_v32(upb_pbdecoder *d, uint32_t *u32) {
  10960. uint64_t u64;
  10961. int32_t ret = decode_varint(d, &u64);
  10962. if (ret >= 0) return ret;
  10963. if (u64 > UINT32_MAX) {
  10964. seterr(d, "Unterminated 32-bit varint");
  10965. /* TODO(haberman) guarantee that this function return is >= 0 somehow,
  10966. * so we know this path will always be treated as error by our caller.
  10967. * Right now the size_t -> int32_t can overflow and produce negative values.
  10968. */
  10969. *u32 = 0;
  10970. return upb_pbdecoder_suspend(d);
  10971. }
  10972. *u32 = u64;
  10973. return DECODE_OK;
  10974. }
  10975. /* Decodes a fixed32 from the current buffer position.
  10976. * Returns a status code as described in decoder.int.h.
  10977. * TODO: proper byte swapping for big-endian machines. */
  10978. UPB_FORCEINLINE static int32_t decode_fixed32(upb_pbdecoder *d, uint32_t *u32) {
  10979. return getbytes(d, u32, 4);
  10980. }
  10981. /* Decodes a fixed64 from the current buffer position.
  10982. * Returns a status code as described in decoder.int.h.
  10983. * TODO: proper byte swapping for big-endian machines. */
  10984. UPB_FORCEINLINE static int32_t decode_fixed64(upb_pbdecoder *d, uint64_t *u64) {
  10985. return getbytes(d, u64, 8);
  10986. }
  10987. /* Non-static versions of the above functions.
  10988. * These are called by the JIT for fallback paths. */
  10989. int32_t upb_pbdecoder_decode_f32(upb_pbdecoder *d, uint32_t *u32) {
  10990. return decode_fixed32(d, u32);
  10991. }
  10992. int32_t upb_pbdecoder_decode_f64(upb_pbdecoder *d, uint64_t *u64) {
  10993. return decode_fixed64(d, u64);
  10994. }
  10995. static double as_double(uint64_t n) { double d; memcpy(&d, &n, 8); return d; }
  10996. static float as_float(uint32_t n) { float f; memcpy(&f, &n, 4); return f; }
  10997. /* Pushes a frame onto the decoder stack. */
  10998. static bool decoder_push(upb_pbdecoder *d, uint64_t end) {
  10999. upb_pbdecoder_frame *fr = d->top;
  11000. if (end > fr->end_ofs) {
  11001. seterr(d, kPbDecoderSubmessageTooLong);
  11002. return false;
  11003. } else if (fr == d->limit) {
  11004. seterr(d, kPbDecoderStackOverflow);
  11005. return false;
  11006. }
  11007. fr++;
  11008. fr->end_ofs = end;
  11009. fr->dispatch = NULL;
  11010. fr->groupnum = 0;
  11011. d->top = fr;
  11012. return true;
  11013. }
  11014. static bool pushtagdelim(upb_pbdecoder *d, uint32_t arg) {
  11015. /* While we expect to see an "end" tag (either ENDGROUP or a non-sequence
  11016. * field number) prior to hitting any enclosing submessage end, pushing our
  11017. * existing delim end prevents us from continuing to parse values from a
  11018. * corrupt proto that doesn't give us an END tag in time. */
  11019. if (!decoder_push(d, d->top->end_ofs))
  11020. return false;
  11021. d->top->groupnum = arg;
  11022. return true;
  11023. }
  11024. /* Pops a frame from the decoder stack. */
  11025. static void decoder_pop(upb_pbdecoder *d) { d->top--; }
  11026. UPB_NOINLINE int32_t upb_pbdecoder_checktag_slow(upb_pbdecoder *d,
  11027. uint64_t expected) {
  11028. uint64_t data = 0;
  11029. size_t bytes = upb_value_size(expected);
  11030. size_t read = peekbytes(d, &data, bytes);
  11031. if (read == bytes && data == expected) {
  11032. /* Advance past matched bytes. */
  11033. int32_t ok = getbytes(d, &data, read);
  11034. UPB_ASSERT(ok < 0);
  11035. return DECODE_OK;
  11036. } else if (read < bytes && memcmp(&data, &expected, read) == 0) {
  11037. return suspend_save(d);
  11038. } else {
  11039. return DECODE_MISMATCH;
  11040. }
  11041. }
  11042. int32_t upb_pbdecoder_skipunknown(upb_pbdecoder *d, int32_t fieldnum,
  11043. uint8_t wire_type) {
  11044. if (fieldnum >= 0)
  11045. goto have_tag;
  11046. while (true) {
  11047. uint32_t tag;
  11048. CHECK_RETURN(decode_v32(d, &tag));
  11049. wire_type = tag & 0x7;
  11050. fieldnum = tag >> 3;
  11051. have_tag:
  11052. if (fieldnum == 0) {
  11053. seterr(d, "Saw invalid field number (0)");
  11054. return upb_pbdecoder_suspend(d);
  11055. }
  11056. switch (wire_type) {
  11057. case UPB_WIRE_TYPE_32BIT:
  11058. CHECK_RETURN(skip(d, 4));
  11059. break;
  11060. case UPB_WIRE_TYPE_64BIT:
  11061. CHECK_RETURN(skip(d, 8));
  11062. break;
  11063. case UPB_WIRE_TYPE_VARINT: {
  11064. uint64_t u64;
  11065. CHECK_RETURN(decode_varint(d, &u64));
  11066. break;
  11067. }
  11068. case UPB_WIRE_TYPE_DELIMITED: {
  11069. uint32_t len;
  11070. CHECK_RETURN(decode_v32(d, &len));
  11071. CHECK_RETURN(skip(d, len));
  11072. break;
  11073. }
  11074. case UPB_WIRE_TYPE_START_GROUP:
  11075. CHECK_SUSPEND(pushtagdelim(d, -fieldnum));
  11076. break;
  11077. case UPB_WIRE_TYPE_END_GROUP:
  11078. if (fieldnum == -d->top->groupnum) {
  11079. decoder_pop(d);
  11080. } else if (fieldnum == d->top->groupnum) {
  11081. return DECODE_ENDGROUP;
  11082. } else {
  11083. seterr(d, "Unmatched ENDGROUP tag.");
  11084. return upb_pbdecoder_suspend(d);
  11085. }
  11086. break;
  11087. default:
  11088. seterr(d, "Invalid wire type");
  11089. return upb_pbdecoder_suspend(d);
  11090. }
  11091. if (d->top->groupnum >= 0) {
  11092. /* TODO: More code needed for handling unknown groups. */
  11093. upb_sink_putunknown(&d->top->sink, d->checkpoint, d->ptr - d->checkpoint);
  11094. return DECODE_OK;
  11095. }
  11096. /* Unknown group -- continue looping over unknown fields. */
  11097. checkpoint(d);
  11098. }
  11099. }
  11100. static void goto_endmsg(upb_pbdecoder *d) {
  11101. upb_value v;
  11102. bool found = upb_inttable_lookup32(d->top->dispatch, DISPATCH_ENDMSG, &v);
  11103. UPB_ASSERT(found);
  11104. d->pc = d->top->base + upb_value_getuint64(v);
  11105. }
  11106. /* Parses a tag and jumps to the corresponding bytecode instruction for this
  11107. * field.
  11108. *
  11109. * If the tag is unknown (or the wire type doesn't match), parses the field as
  11110. * unknown. If the tag is a valid ENDGROUP tag, jumps to the bytecode
  11111. * instruction for the end of message. */
  11112. static int32_t dispatch(upb_pbdecoder *d) {
  11113. upb_inttable *dispatch = d->top->dispatch;
  11114. uint32_t tag;
  11115. uint8_t wire_type;
  11116. uint32_t fieldnum;
  11117. upb_value val;
  11118. int32_t retval;
  11119. /* Decode tag. */
  11120. CHECK_RETURN(decode_v32(d, &tag));
  11121. wire_type = tag & 0x7;
  11122. fieldnum = tag >> 3;
  11123. /* Lookup tag. Because of packed/non-packed compatibility, we have to
  11124. * check the wire type against two possibilities. */
  11125. if (fieldnum != DISPATCH_ENDMSG &&
  11126. upb_inttable_lookup32(dispatch, fieldnum, &val)) {
  11127. uint64_t v = upb_value_getuint64(val);
  11128. if (wire_type == (v & 0xff)) {
  11129. d->pc = d->top->base + (v >> 16);
  11130. return DECODE_OK;
  11131. } else if (wire_type == ((v >> 8) & 0xff)) {
  11132. bool found =
  11133. upb_inttable_lookup(dispatch, fieldnum + UPB_MAX_FIELDNUMBER, &val);
  11134. UPB_ASSERT(found);
  11135. d->pc = d->top->base + upb_value_getuint64(val);
  11136. return DECODE_OK;
  11137. }
  11138. }
  11139. /* We have some unknown fields (or ENDGROUP) to parse. The DISPATCH or TAG
  11140. * bytecode that triggered this is preceded by a CHECKDELIM bytecode which
  11141. * we need to back up to, so that when we're done skipping unknown data we
  11142. * can re-check the delimited end. */
  11143. d->last--; /* Necessary if we get suspended */
  11144. d->pc = d->last;
  11145. UPB_ASSERT(getop(*d->last) == OP_CHECKDELIM);
  11146. /* Unknown field or ENDGROUP. */
  11147. retval = upb_pbdecoder_skipunknown(d, fieldnum, wire_type);
  11148. CHECK_RETURN(retval);
  11149. if (retval == DECODE_ENDGROUP) {
  11150. goto_endmsg(d);
  11151. return DECODE_OK;
  11152. }
  11153. return DECODE_OK;
  11154. }
  11155. /* Callers know that the stack is more than one deep because the opcodes that
  11156. * call this only occur after PUSH operations. */
  11157. upb_pbdecoder_frame *outer_frame(upb_pbdecoder *d) {
  11158. UPB_ASSERT(d->top != d->stack);
  11159. return d->top - 1;
  11160. }
  11161. /* The main decoding loop *****************************************************/
  11162. /* The main decoder VM function. Uses traditional bytecode dispatch loop with a
  11163. * switch() statement. */
  11164. size_t run_decoder_vm(upb_pbdecoder *d, const mgroup *group,
  11165. const upb_bufhandle* handle) {
  11166. #define VMCASE(op, code) \
  11167. case op: { code; if (consumes_input(op)) checkpoint(d); break; }
  11168. #define PRIMITIVE_OP(type, wt, name, convfunc, ctype) \
  11169. VMCASE(OP_PARSE_ ## type, { \
  11170. ctype val; \
  11171. CHECK_RETURN(decode_ ## wt(d, &val)); \
  11172. upb_sink_put ## name(&d->top->sink, arg, (convfunc)(val)); \
  11173. })
  11174. while(1) {
  11175. int32_t instruction;
  11176. opcode op;
  11177. uint32_t arg;
  11178. int32_t longofs;
  11179. d->last = d->pc;
  11180. instruction = *d->pc++;
  11181. op = getop(instruction);
  11182. arg = instruction >> 8;
  11183. longofs = arg;
  11184. UPB_ASSERT(d->ptr != d->residual_end);
  11185. UPB_UNUSED(group);
  11186. #ifdef UPB_DUMP_BYTECODE
  11187. fprintf(stderr, "s_ofs=%d buf_ofs=%d data_rem=%d buf_rem=%d delim_rem=%d "
  11188. "%x %s (%d)\n",
  11189. (int)offset(d),
  11190. (int)(d->ptr - d->buf),
  11191. (int)(d->data_end - d->ptr),
  11192. (int)(d->end - d->ptr),
  11193. (int)((d->top->end_ofs - d->bufstart_ofs) - (d->ptr - d->buf)),
  11194. (int)(d->pc - 1 - group->bytecode),
  11195. upb_pbdecoder_getopname(op),
  11196. arg);
  11197. #endif
  11198. switch (op) {
  11199. /* Technically, we are losing data if we see a 32-bit varint that is not
  11200. * properly sign-extended. We could detect this and error about the data
  11201. * loss, but proto2 does not do this, so we pass. */
  11202. PRIMITIVE_OP(INT32, varint, int32, int32_t, uint64_t)
  11203. PRIMITIVE_OP(INT64, varint, int64, int64_t, uint64_t)
  11204. PRIMITIVE_OP(UINT32, varint, uint32, uint32_t, uint64_t)
  11205. PRIMITIVE_OP(UINT64, varint, uint64, uint64_t, uint64_t)
  11206. PRIMITIVE_OP(FIXED32, fixed32, uint32, uint32_t, uint32_t)
  11207. PRIMITIVE_OP(FIXED64, fixed64, uint64, uint64_t, uint64_t)
  11208. PRIMITIVE_OP(SFIXED32, fixed32, int32, int32_t, uint32_t)
  11209. PRIMITIVE_OP(SFIXED64, fixed64, int64, int64_t, uint64_t)
  11210. PRIMITIVE_OP(BOOL, varint, bool, bool, uint64_t)
  11211. PRIMITIVE_OP(DOUBLE, fixed64, double, as_double, uint64_t)
  11212. PRIMITIVE_OP(FLOAT, fixed32, float, as_float, uint32_t)
  11213. PRIMITIVE_OP(SINT32, varint, int32, upb_zzdec_32, uint64_t)
  11214. PRIMITIVE_OP(SINT64, varint, int64, upb_zzdec_64, uint64_t)
  11215. VMCASE(OP_SETDISPATCH,
  11216. d->top->base = d->pc - 1;
  11217. memcpy(&d->top->dispatch, d->pc, sizeof(void*));
  11218. d->pc += sizeof(void*) / sizeof(uint32_t);
  11219. )
  11220. VMCASE(OP_STARTMSG,
  11221. CHECK_SUSPEND(upb_sink_startmsg(&d->top->sink));
  11222. )
  11223. VMCASE(OP_ENDMSG,
  11224. CHECK_SUSPEND(upb_sink_endmsg(&d->top->sink, d->status));
  11225. )
  11226. VMCASE(OP_STARTSEQ,
  11227. upb_pbdecoder_frame *outer = outer_frame(d);
  11228. CHECK_SUSPEND(upb_sink_startseq(&outer->sink, arg, &d->top->sink));
  11229. )
  11230. VMCASE(OP_ENDSEQ,
  11231. CHECK_SUSPEND(upb_sink_endseq(&d->top->sink, arg));
  11232. )
  11233. VMCASE(OP_STARTSUBMSG,
  11234. upb_pbdecoder_frame *outer = outer_frame(d);
  11235. CHECK_SUSPEND(upb_sink_startsubmsg(&outer->sink, arg, &d->top->sink));
  11236. )
  11237. VMCASE(OP_ENDSUBMSG,
  11238. CHECK_SUSPEND(upb_sink_endsubmsg(&d->top->sink, arg));
  11239. )
  11240. VMCASE(OP_STARTSTR,
  11241. uint32_t len = delim_remaining(d);
  11242. upb_pbdecoder_frame *outer = outer_frame(d);
  11243. CHECK_SUSPEND(upb_sink_startstr(&outer->sink, arg, len, &d->top->sink));
  11244. if (len == 0) {
  11245. d->pc++; /* Skip OP_STRING. */
  11246. }
  11247. )
  11248. VMCASE(OP_STRING,
  11249. uint32_t len = curbufleft(d);
  11250. size_t n = upb_sink_putstring(&d->top->sink, arg, d->ptr, len, handle);
  11251. if (n > len) {
  11252. if (n > delim_remaining(d)) {
  11253. seterr(d, "Tried to skip past end of string.");
  11254. return upb_pbdecoder_suspend(d);
  11255. } else {
  11256. int32_t ret = skip(d, n);
  11257. /* This shouldn't return DECODE_OK, because n > len. */
  11258. UPB_ASSERT(ret >= 0);
  11259. return ret;
  11260. }
  11261. }
  11262. advance(d, n);
  11263. if (n < len || d->delim_end == NULL) {
  11264. /* We aren't finished with this string yet. */
  11265. d->pc--; /* Repeat OP_STRING. */
  11266. if (n > 0) checkpoint(d);
  11267. return upb_pbdecoder_suspend(d);
  11268. }
  11269. )
  11270. VMCASE(OP_ENDSTR,
  11271. CHECK_SUSPEND(upb_sink_endstr(&d->top->sink, arg));
  11272. )
  11273. VMCASE(OP_PUSHTAGDELIM,
  11274. CHECK_SUSPEND(pushtagdelim(d, arg));
  11275. )
  11276. VMCASE(OP_SETBIGGROUPNUM,
  11277. d->top->groupnum = *d->pc++;
  11278. )
  11279. VMCASE(OP_POP,
  11280. UPB_ASSERT(d->top > d->stack);
  11281. decoder_pop(d);
  11282. )
  11283. VMCASE(OP_PUSHLENDELIM,
  11284. uint32_t len;
  11285. CHECK_RETURN(decode_v32(d, &len));
  11286. CHECK_SUSPEND(decoder_push(d, offset(d) + len));
  11287. set_delim_end(d);
  11288. )
  11289. VMCASE(OP_SETDELIM,
  11290. set_delim_end(d);
  11291. )
  11292. VMCASE(OP_CHECKDELIM,
  11293. /* We are guaranteed of this assert because we never allow ourselves to
  11294. * consume bytes beyond data_end, which covers delim_end when non-NULL.
  11295. */
  11296. UPB_ASSERT(!(d->delim_end && d->ptr > d->delim_end));
  11297. if (d->ptr == d->delim_end)
  11298. d->pc += longofs;
  11299. )
  11300. VMCASE(OP_CALL,
  11301. d->callstack[d->call_len++] = d->pc;
  11302. d->pc += longofs;
  11303. )
  11304. VMCASE(OP_RET,
  11305. UPB_ASSERT(d->call_len > 0);
  11306. d->pc = d->callstack[--d->call_len];
  11307. )
  11308. VMCASE(OP_BRANCH,
  11309. d->pc += longofs;
  11310. )
  11311. VMCASE(OP_TAG1,
  11312. uint8_t expected;
  11313. CHECK_SUSPEND(curbufleft(d) > 0);
  11314. expected = (arg >> 8) & 0xff;
  11315. if (*d->ptr == expected) {
  11316. advance(d, 1);
  11317. } else {
  11318. int8_t shortofs;
  11319. badtag:
  11320. shortofs = arg;
  11321. if (shortofs == LABEL_DISPATCH) {
  11322. CHECK_RETURN(dispatch(d));
  11323. } else {
  11324. d->pc += shortofs;
  11325. break; /* Avoid checkpoint(). */
  11326. }
  11327. }
  11328. )
  11329. VMCASE(OP_TAG2,
  11330. uint16_t expected;
  11331. CHECK_SUSPEND(curbufleft(d) > 0);
  11332. expected = (arg >> 8) & 0xffff;
  11333. if (curbufleft(d) >= 2) {
  11334. uint16_t actual;
  11335. memcpy(&actual, d->ptr, 2);
  11336. if (expected == actual) {
  11337. advance(d, 2);
  11338. } else {
  11339. goto badtag;
  11340. }
  11341. } else {
  11342. int32_t result = upb_pbdecoder_checktag_slow(d, expected);
  11343. if (result == DECODE_MISMATCH) goto badtag;
  11344. if (result >= 0) return result;
  11345. }
  11346. )
  11347. VMCASE(OP_TAGN, {
  11348. uint64_t expected;
  11349. int32_t result;
  11350. memcpy(&expected, d->pc, 8);
  11351. d->pc += 2;
  11352. result = upb_pbdecoder_checktag_slow(d, expected);
  11353. if (result == DECODE_MISMATCH) goto badtag;
  11354. if (result >= 0) return result;
  11355. })
  11356. VMCASE(OP_DISPATCH, {
  11357. CHECK_RETURN(dispatch(d));
  11358. })
  11359. VMCASE(OP_HALT, {
  11360. return d->size_param;
  11361. })
  11362. }
  11363. }
  11364. }
  11365. /* BytesHandler handlers ******************************************************/
  11366. void *upb_pbdecoder_startbc(void *closure, const void *pc, size_t size_hint) {
  11367. upb_pbdecoder *d = closure;
  11368. UPB_UNUSED(size_hint);
  11369. d->top->end_ofs = UINT64_MAX;
  11370. d->bufstart_ofs = 0;
  11371. d->call_len = 1;
  11372. d->callstack[0] = &halt;
  11373. d->pc = pc;
  11374. d->skip = 0;
  11375. return d;
  11376. }
  11377. void *upb_pbdecoder_startjit(void *closure, const void *hd, size_t size_hint) {
  11378. upb_pbdecoder *d = closure;
  11379. UPB_UNUSED(hd);
  11380. UPB_UNUSED(size_hint);
  11381. d->top->end_ofs = UINT64_MAX;
  11382. d->bufstart_ofs = 0;
  11383. d->call_len = 0;
  11384. d->skip = 0;
  11385. return d;
  11386. }
  11387. bool upb_pbdecoder_end(void *closure, const void *handler_data) {
  11388. upb_pbdecoder *d = closure;
  11389. const upb_pbdecodermethod *method = handler_data;
  11390. uint64_t end;
  11391. char dummy;
  11392. if (d->residual_end > d->residual) {
  11393. seterr(d, "Unexpected EOF: decoder still has buffered unparsed data");
  11394. return false;
  11395. }
  11396. if (d->skip) {
  11397. seterr(d, "Unexpected EOF inside skipped data");
  11398. return false;
  11399. }
  11400. if (d->top->end_ofs != UINT64_MAX) {
  11401. seterr(d, "Unexpected EOF inside delimited string");
  11402. return false;
  11403. }
  11404. /* The user's end() call indicates that the message ends here. */
  11405. end = offset(d);
  11406. d->top->end_ofs = end;
  11407. #ifdef UPB_USE_JIT_X64
  11408. if (method->is_native_) {
  11409. const mgroup *group = (const mgroup*)method->group;
  11410. if (d->top != d->stack)
  11411. d->stack->end_ofs = 0;
  11412. group->jit_code(closure, method->code_base.ptr, &dummy, 0, NULL);
  11413. } else
  11414. #endif
  11415. {
  11416. const uint32_t *p = d->pc;
  11417. d->stack->end_ofs = end;
  11418. /* Check the previous bytecode, but guard against beginning. */
  11419. if (p != method->code_base.ptr) p--;
  11420. if (getop(*p) == OP_CHECKDELIM) {
  11421. /* Rewind from OP_TAG* to OP_CHECKDELIM. */
  11422. UPB_ASSERT(getop(*d->pc) == OP_TAG1 ||
  11423. getop(*d->pc) == OP_TAG2 ||
  11424. getop(*d->pc) == OP_TAGN ||
  11425. getop(*d->pc) == OP_DISPATCH);
  11426. d->pc = p;
  11427. }
  11428. upb_pbdecoder_decode(closure, handler_data, &dummy, 0, NULL);
  11429. }
  11430. if (d->call_len != 0) {
  11431. seterr(d, "Unexpected EOF inside submessage or group");
  11432. return false;
  11433. }
  11434. return true;
  11435. }
  11436. size_t upb_pbdecoder_decode(void *decoder, const void *group, const char *buf,
  11437. size_t size, const upb_bufhandle *handle) {
  11438. int32_t result = upb_pbdecoder_resume(decoder, NULL, buf, size, handle);
  11439. if (result == DECODE_ENDGROUP) goto_endmsg(decoder);
  11440. CHECK_RETURN(result);
  11441. return run_decoder_vm(decoder, group, handle);
  11442. }
  11443. /* Public API *****************************************************************/
  11444. void upb_pbdecoder_reset(upb_pbdecoder *d) {
  11445. d->top = d->stack;
  11446. d->top->groupnum = 0;
  11447. d->ptr = d->residual;
  11448. d->buf = d->residual;
  11449. d->end = d->residual;
  11450. d->residual_end = d->residual;
  11451. }
  11452. upb_pbdecoder *upb_pbdecoder_create(upb_env *e, const upb_pbdecodermethod *m,
  11453. upb_sink *sink) {
  11454. const size_t default_max_nesting = 64;
  11455. #ifndef NDEBUG
  11456. size_t size_before = upb_env_bytesallocated(e);
  11457. #endif
  11458. upb_pbdecoder *d = upb_env_malloc(e, sizeof(upb_pbdecoder));
  11459. if (!d) return NULL;
  11460. d->method_ = m;
  11461. d->callstack = upb_env_malloc(e, callstacksize(d, default_max_nesting));
  11462. d->stack = upb_env_malloc(e, stacksize(d, default_max_nesting));
  11463. if (!d->stack || !d->callstack) {
  11464. return NULL;
  11465. }
  11466. d->env = e;
  11467. d->limit = d->stack + default_max_nesting - 1;
  11468. d->stack_size = default_max_nesting;
  11469. d->status = NULL;
  11470. upb_pbdecoder_reset(d);
  11471. upb_bytessink_reset(&d->input_, &m->input_handler_, d);
  11472. UPB_ASSERT(sink);
  11473. if (d->method_->dest_handlers_) {
  11474. if (sink->handlers != d->method_->dest_handlers_)
  11475. return NULL;
  11476. }
  11477. upb_sink_reset(&d->top->sink, sink->handlers, sink->closure);
  11478. /* If this fails, increase the value in decoder.h. */
  11479. UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(e) - size_before <=
  11480. UPB_PB_DECODER_SIZE);
  11481. return d;
  11482. }
  11483. uint64_t upb_pbdecoder_bytesparsed(const upb_pbdecoder *d) {
  11484. return offset(d);
  11485. }
  11486. const upb_pbdecodermethod *upb_pbdecoder_method(const upb_pbdecoder *d) {
  11487. return d->method_;
  11488. }
  11489. upb_bytessink *upb_pbdecoder_input(upb_pbdecoder *d) {
  11490. return &d->input_;
  11491. }
  11492. size_t upb_pbdecoder_maxnesting(const upb_pbdecoder *d) {
  11493. return d->stack_size;
  11494. }
  11495. bool upb_pbdecoder_setmaxnesting(upb_pbdecoder *d, size_t max) {
  11496. UPB_ASSERT(d->top >= d->stack);
  11497. if (max < (size_t)(d->top - d->stack)) {
  11498. /* Can't set a limit smaller than what we are currently at. */
  11499. return false;
  11500. }
  11501. if (max > d->stack_size) {
  11502. /* Need to reallocate stack and callstack to accommodate. */
  11503. size_t old_size = stacksize(d, d->stack_size);
  11504. size_t new_size = stacksize(d, max);
  11505. void *p = upb_env_realloc(d->env, d->stack, old_size, new_size);
  11506. if (!p) {
  11507. return false;
  11508. }
  11509. d->stack = p;
  11510. old_size = callstacksize(d, d->stack_size);
  11511. new_size = callstacksize(d, max);
  11512. p = upb_env_realloc(d->env, d->callstack, old_size, new_size);
  11513. if (!p) {
  11514. return false;
  11515. }
  11516. d->callstack = p;
  11517. d->stack_size = max;
  11518. }
  11519. d->limit = d->stack + max - 1;
  11520. return true;
  11521. }
  11522. /*
  11523. ** upb::Encoder
  11524. **
  11525. ** Since we are implementing pure handlers (ie. without any out-of-band access
  11526. ** to pre-computed lengths), we have to buffer all submessages before we can
  11527. ** emit even their first byte.
  11528. **
  11529. ** Not knowing the size of submessages also means we can't write a perfect
  11530. ** zero-copy implementation, even with buffering. Lengths are stored as
  11531. ** varints, which means that we don't know how many bytes to reserve for the
  11532. ** length until we know what the length is.
  11533. **
  11534. ** This leaves us with three main choices:
  11535. **
  11536. ** 1. buffer all submessage data in a temporary buffer, then copy it exactly
  11537. ** once into the output buffer.
  11538. **
  11539. ** 2. attempt to buffer data directly into the output buffer, estimating how
  11540. ** many bytes each length will take. When our guesses are wrong, use
  11541. ** memmove() to grow or shrink the allotted space.
  11542. **
  11543. ** 3. buffer directly into the output buffer, allocating a max length
  11544. ** ahead-of-time for each submessage length. If we overallocated, we waste
  11545. ** space, but no memcpy() or memmove() is required. This approach requires
  11546. ** defining a maximum size for submessages and rejecting submessages that
  11547. ** exceed that size.
  11548. **
  11549. ** (2) and (3) have the potential to have better performance, but they are more
  11550. ** complicated and subtle to implement:
  11551. **
  11552. ** (3) requires making an arbitrary choice of the maximum message size; it
  11553. ** wastes space when submessages are shorter than this and fails
  11554. ** completely when they are longer. This makes it more finicky and
  11555. ** requires configuration based on the input. It also makes it impossible
  11556. ** to perfectly match the output of reference encoders that always use the
  11557. ** optimal amount of space for each length.
  11558. **
  11559. ** (2) requires guessing the the size upfront, and if multiple lengths are
  11560. ** guessed wrong the minimum required number of memmove() operations may
  11561. ** be complicated to compute correctly. Implemented properly, it may have
  11562. ** a useful amortized or average cost, but more investigation is required
  11563. ** to determine this and what the optimal algorithm is to achieve it.
  11564. **
  11565. ** (1) makes you always pay for exactly one copy, but its implementation is
  11566. ** the simplest and its performance is predictable.
  11567. **
  11568. ** So for now, we implement (1) only. If we wish to optimize later, we should
  11569. ** be able to do it without affecting users.
  11570. **
  11571. ** The strategy is to buffer the segments of data that do *not* depend on
  11572. ** unknown lengths in one buffer, and keep a separate buffer of segment pointers
  11573. ** and lengths. When the top-level submessage ends, we can go beginning to end,
  11574. ** alternating the writing of lengths with memcpy() of the rest of the data.
  11575. ** At the top level though, no buffering is required.
  11576. */
  11577. /* The output buffer is divided into segments; a segment is a string of data
  11578. * that is "ready to go" -- it does not need any varint lengths inserted into
  11579. * the middle. The seams between segments are where varints will be inserted
  11580. * once they are known.
  11581. *
  11582. * We also use the concept of a "run", which is a range of encoded bytes that
  11583. * occur at a single submessage level. Every segment contains one or more runs.
  11584. *
  11585. * A segment can span messages. Consider:
  11586. *
  11587. * .--Submessage lengths---------.
  11588. * | | |
  11589. * | V V
  11590. * V | |--------------- | |-----------------
  11591. * Submessages: | |-----------------------------------------------
  11592. * Top-level msg: ------------------------------------------------------------
  11593. *
  11594. * Segments: ----- ------------------- -----------------
  11595. * Runs: *---- *--------------*--- *----------------
  11596. * (* marks the start)
  11597. *
  11598. * Note that the top-level menssage is not in any segment because it does not
  11599. * have any length preceding it.
  11600. *
  11601. * A segment is only interrupted when another length needs to be inserted. So
  11602. * observe how the second segment spans both the inner submessage and part of
  11603. * the next enclosing message. */
  11604. typedef struct {
  11605. uint32_t msglen; /* The length to varint-encode before this segment. */
  11606. uint32_t seglen; /* Length of the segment. */
  11607. } upb_pb_encoder_segment;
  11608. struct upb_pb_encoder {
  11609. upb_env *env;
  11610. /* Our input and output. */
  11611. upb_sink input_;
  11612. upb_bytessink *output_;
  11613. /* The "subclosure" -- used as the inner closure as part of the bytessink
  11614. * protocol. */
  11615. void *subc;
  11616. /* The output buffer and limit, and our current write position. "buf"
  11617. * initially points to "initbuf", but is dynamically allocated if we need to
  11618. * grow beyond the initial size. */
  11619. char *buf, *ptr, *limit;
  11620. /* The beginning of the current run, or undefined if we are at the top
  11621. * level. */
  11622. char *runbegin;
  11623. /* The list of segments we are accumulating. */
  11624. upb_pb_encoder_segment *segbuf, *segptr, *seglimit;
  11625. /* The stack of enclosing submessages. Each entry in the stack points to the
  11626. * segment where this submessage's length is being accumulated. */
  11627. int *stack, *top, *stacklimit;
  11628. /* Depth of startmsg/endmsg calls. */
  11629. int depth;
  11630. };
  11631. /* low-level buffering ********************************************************/
  11632. /* Low-level functions for interacting with the output buffer. */
  11633. /* TODO(haberman): handle pushback */
  11634. static void putbuf(upb_pb_encoder *e, const char *buf, size_t len) {
  11635. size_t n = upb_bytessink_putbuf(e->output_, e->subc, buf, len, NULL);
  11636. UPB_ASSERT(n == len);
  11637. }
  11638. static upb_pb_encoder_segment *top(upb_pb_encoder *e) {
  11639. return &e->segbuf[*e->top];
  11640. }
  11641. /* Call to ensure that at least "bytes" bytes are available for writing at
  11642. * e->ptr. Returns false if the bytes could not be allocated. */
  11643. static bool reserve(upb_pb_encoder *e, size_t bytes) {
  11644. if ((size_t)(e->limit - e->ptr) < bytes) {
  11645. /* Grow buffer. */
  11646. char *new_buf;
  11647. size_t needed = bytes + (e->ptr - e->buf);
  11648. size_t old_size = e->limit - e->buf;
  11649. size_t new_size = old_size;
  11650. while (new_size < needed) {
  11651. new_size *= 2;
  11652. }
  11653. new_buf = upb_env_realloc(e->env, e->buf, old_size, new_size);
  11654. if (new_buf == NULL) {
  11655. return false;
  11656. }
  11657. e->ptr = new_buf + (e->ptr - e->buf);
  11658. e->runbegin = new_buf + (e->runbegin - e->buf);
  11659. e->limit = new_buf + new_size;
  11660. e->buf = new_buf;
  11661. }
  11662. return true;
  11663. }
  11664. /* Call when "bytes" bytes have been writte at e->ptr. The caller *must* have
  11665. * previously called reserve() with at least this many bytes. */
  11666. static void encoder_advance(upb_pb_encoder *e, size_t bytes) {
  11667. UPB_ASSERT((size_t)(e->limit - e->ptr) >= bytes);
  11668. e->ptr += bytes;
  11669. }
  11670. /* Call when all of the bytes for a handler have been written. Flushes the
  11671. * bytes if possible and necessary, returning false if this failed. */
  11672. static bool commit(upb_pb_encoder *e) {
  11673. if (!e->top) {
  11674. /* We aren't inside a delimited region. Flush our accumulated bytes to
  11675. * the output.
  11676. *
  11677. * TODO(haberman): in the future we may want to delay flushing for
  11678. * efficiency reasons. */
  11679. putbuf(e, e->buf, e->ptr - e->buf);
  11680. e->ptr = e->buf;
  11681. }
  11682. return true;
  11683. }
  11684. /* Writes the given bytes to the buffer, handling reserve/advance. */
  11685. static bool encode_bytes(upb_pb_encoder *e, const void *data, size_t len) {
  11686. if (!reserve(e, len)) {
  11687. return false;
  11688. }
  11689. memcpy(e->ptr, data, len);
  11690. encoder_advance(e, len);
  11691. return true;
  11692. }
  11693. /* Finish the current run by adding the run totals to the segment and message
  11694. * length. */
  11695. static void accumulate(upb_pb_encoder *e) {
  11696. size_t run_len;
  11697. UPB_ASSERT(e->ptr >= e->runbegin);
  11698. run_len = e->ptr - e->runbegin;
  11699. e->segptr->seglen += run_len;
  11700. top(e)->msglen += run_len;
  11701. e->runbegin = e->ptr;
  11702. }
  11703. /* Call to indicate the start of delimited region for which the full length is
  11704. * not yet known. All data will be buffered until the length is known.
  11705. * Delimited regions may be nested; their lengths will all be tracked properly. */
  11706. static bool start_delim(upb_pb_encoder *e) {
  11707. if (e->top) {
  11708. /* We are already buffering, advance to the next segment and push it on the
  11709. * stack. */
  11710. accumulate(e);
  11711. if (++e->top == e->stacklimit) {
  11712. /* TODO(haberman): grow stack? */
  11713. return false;
  11714. }
  11715. if (++e->segptr == e->seglimit) {
  11716. /* Grow segment buffer. */
  11717. size_t old_size =
  11718. (e->seglimit - e->segbuf) * sizeof(upb_pb_encoder_segment);
  11719. size_t new_size = old_size * 2;
  11720. upb_pb_encoder_segment *new_buf =
  11721. upb_env_realloc(e->env, e->segbuf, old_size, new_size);
  11722. if (new_buf == NULL) {
  11723. return false;
  11724. }
  11725. e->segptr = new_buf + (e->segptr - e->segbuf);
  11726. e->seglimit = new_buf + (new_size / sizeof(upb_pb_encoder_segment));
  11727. e->segbuf = new_buf;
  11728. }
  11729. } else {
  11730. /* We were previously at the top level, start buffering. */
  11731. e->segptr = e->segbuf;
  11732. e->top = e->stack;
  11733. e->runbegin = e->ptr;
  11734. }
  11735. *e->top = e->segptr - e->segbuf;
  11736. e->segptr->seglen = 0;
  11737. e->segptr->msglen = 0;
  11738. return true;
  11739. }
  11740. /* Call to indicate the end of a delimited region. We now know the length of
  11741. * the delimited region. If we are not nested inside any other delimited
  11742. * regions, we can now emit all of the buffered data we accumulated. */
  11743. static bool end_delim(upb_pb_encoder *e) {
  11744. size_t msglen;
  11745. accumulate(e);
  11746. msglen = top(e)->msglen;
  11747. if (e->top == e->stack) {
  11748. /* All lengths are now available, emit all buffered data. */
  11749. char buf[UPB_PB_VARINT_MAX_LEN];
  11750. upb_pb_encoder_segment *s;
  11751. const char *ptr = e->buf;
  11752. for (s = e->segbuf; s <= e->segptr; s++) {
  11753. size_t lenbytes = upb_vencode64(s->msglen, buf);
  11754. putbuf(e, buf, lenbytes);
  11755. putbuf(e, ptr, s->seglen);
  11756. ptr += s->seglen;
  11757. }
  11758. e->ptr = e->buf;
  11759. e->top = NULL;
  11760. } else {
  11761. /* Need to keep buffering; propagate length info into enclosing
  11762. * submessages. */
  11763. --e->top;
  11764. top(e)->msglen += msglen + upb_varint_size(msglen);
  11765. }
  11766. return true;
  11767. }
  11768. /* tag_t **********************************************************************/
  11769. /* A precomputed (pre-encoded) tag and length. */
  11770. typedef struct {
  11771. uint8_t bytes;
  11772. char tag[7];
  11773. } tag_t;
  11774. /* Allocates a new tag for this field, and sets it in these handlerattr. */
  11775. static void new_tag(upb_handlers *h, const upb_fielddef *f, upb_wiretype_t wt,
  11776. upb_handlerattr *attr) {
  11777. uint32_t n = upb_fielddef_number(f);
  11778. tag_t *tag = upb_gmalloc(sizeof(tag_t));
  11779. tag->bytes = upb_vencode64((n << 3) | wt, tag->tag);
  11780. upb_handlerattr_init(attr);
  11781. upb_handlerattr_sethandlerdata(attr, tag);
  11782. upb_handlers_addcleanup(h, tag, upb_gfree);
  11783. }
  11784. static bool encode_tag(upb_pb_encoder *e, const tag_t *tag) {
  11785. return encode_bytes(e, tag->tag, tag->bytes);
  11786. }
  11787. /* encoding of wire types *****************************************************/
  11788. static bool encode_fixed64(upb_pb_encoder *e, uint64_t val) {
  11789. /* TODO(haberman): byte-swap for big endian. */
  11790. return encode_bytes(e, &val, sizeof(uint64_t));
  11791. }
  11792. static bool encode_fixed32(upb_pb_encoder *e, uint32_t val) {
  11793. /* TODO(haberman): byte-swap for big endian. */
  11794. return encode_bytes(e, &val, sizeof(uint32_t));
  11795. }
  11796. static bool encode_varint(upb_pb_encoder *e, uint64_t val) {
  11797. if (!reserve(e, UPB_PB_VARINT_MAX_LEN)) {
  11798. return false;
  11799. }
  11800. encoder_advance(e, upb_vencode64(val, e->ptr));
  11801. return true;
  11802. }
  11803. static uint64_t dbl2uint64(double d) {
  11804. uint64_t ret;
  11805. memcpy(&ret, &d, sizeof(uint64_t));
  11806. return ret;
  11807. }
  11808. static uint32_t flt2uint32(float d) {
  11809. uint32_t ret;
  11810. memcpy(&ret, &d, sizeof(uint32_t));
  11811. return ret;
  11812. }
  11813. /* encoding of proto types ****************************************************/
  11814. static bool startmsg(void *c, const void *hd) {
  11815. upb_pb_encoder *e = c;
  11816. UPB_UNUSED(hd);
  11817. if (e->depth++ == 0) {
  11818. upb_bytessink_start(e->output_, 0, &e->subc);
  11819. }
  11820. return true;
  11821. }
  11822. static bool endmsg(void *c, const void *hd, upb_status *status) {
  11823. upb_pb_encoder *e = c;
  11824. UPB_UNUSED(hd);
  11825. UPB_UNUSED(status);
  11826. if (--e->depth == 0) {
  11827. upb_bytessink_end(e->output_);
  11828. }
  11829. return true;
  11830. }
  11831. static void *encode_startdelimfield(void *c, const void *hd) {
  11832. bool ok = encode_tag(c, hd) && commit(c) && start_delim(c);
  11833. return ok ? c : UPB_BREAK;
  11834. }
  11835. static bool encode_unknown(void *c, const void *hd, const char *buf,
  11836. size_t len) {
  11837. UPB_UNUSED(hd);
  11838. return encode_bytes(c, buf, len) && commit(c);
  11839. }
  11840. static bool encode_enddelimfield(void *c, const void *hd) {
  11841. UPB_UNUSED(hd);
  11842. return end_delim(c);
  11843. }
  11844. static void *encode_startgroup(void *c, const void *hd) {
  11845. return (encode_tag(c, hd) && commit(c)) ? c : UPB_BREAK;
  11846. }
  11847. static bool encode_endgroup(void *c, const void *hd) {
  11848. return encode_tag(c, hd) && commit(c);
  11849. }
  11850. static void *encode_startstr(void *c, const void *hd, size_t size_hint) {
  11851. UPB_UNUSED(size_hint);
  11852. return encode_startdelimfield(c, hd);
  11853. }
  11854. static size_t encode_strbuf(void *c, const void *hd, const char *buf,
  11855. size_t len, const upb_bufhandle *h) {
  11856. UPB_UNUSED(hd);
  11857. UPB_UNUSED(h);
  11858. return encode_bytes(c, buf, len) ? len : 0;
  11859. }
  11860. #define T(type, ctype, convert, encode) \
  11861. static bool encode_scalar_##type(void *e, const void *hd, ctype val) { \
  11862. return encode_tag(e, hd) && encode(e, (convert)(val)) && commit(e); \
  11863. } \
  11864. static bool encode_packed_##type(void *e, const void *hd, ctype val) { \
  11865. UPB_UNUSED(hd); \
  11866. return encode(e, (convert)(val)); \
  11867. }
  11868. T(double, double, dbl2uint64, encode_fixed64)
  11869. T(float, float, flt2uint32, encode_fixed32)
  11870. T(int64, int64_t, uint64_t, encode_varint)
  11871. T(int32, int32_t, int64_t, encode_varint)
  11872. T(fixed64, uint64_t, uint64_t, encode_fixed64)
  11873. T(fixed32, uint32_t, uint32_t, encode_fixed32)
  11874. T(bool, bool, bool, encode_varint)
  11875. T(uint32, uint32_t, uint32_t, encode_varint)
  11876. T(uint64, uint64_t, uint64_t, encode_varint)
  11877. T(enum, int32_t, uint32_t, encode_varint)
  11878. T(sfixed32, int32_t, uint32_t, encode_fixed32)
  11879. T(sfixed64, int64_t, uint64_t, encode_fixed64)
  11880. T(sint32, int32_t, upb_zzenc_32, encode_varint)
  11881. T(sint64, int64_t, upb_zzenc_64, encode_varint)
  11882. #undef T
  11883. /* code to build the handlers *************************************************/
  11884. static void newhandlers_callback(const void *closure, upb_handlers *h) {
  11885. const upb_msgdef *m;
  11886. upb_msg_field_iter i;
  11887. UPB_UNUSED(closure);
  11888. upb_handlers_setstartmsg(h, startmsg, NULL);
  11889. upb_handlers_setendmsg(h, endmsg, NULL);
  11890. upb_handlers_setunknown(h, encode_unknown, NULL);
  11891. m = upb_handlers_msgdef(h);
  11892. for(upb_msg_field_begin(&i, m);
  11893. !upb_msg_field_done(&i);
  11894. upb_msg_field_next(&i)) {
  11895. const upb_fielddef *f = upb_msg_iter_field(&i);
  11896. bool packed = upb_fielddef_isseq(f) && upb_fielddef_isprimitive(f) &&
  11897. upb_fielddef_packed(f);
  11898. upb_handlerattr attr;
  11899. upb_wiretype_t wt =
  11900. packed ? UPB_WIRE_TYPE_DELIMITED
  11901. : upb_pb_native_wire_types[upb_fielddef_descriptortype(f)];
  11902. /* Pre-encode the tag for this field. */
  11903. new_tag(h, f, wt, &attr);
  11904. if (packed) {
  11905. upb_handlers_setstartseq(h, f, encode_startdelimfield, &attr);
  11906. upb_handlers_setendseq(h, f, encode_enddelimfield, &attr);
  11907. }
  11908. #define T(upper, lower, upbtype) \
  11909. case UPB_DESCRIPTOR_TYPE_##upper: \
  11910. if (packed) { \
  11911. upb_handlers_set##upbtype(h, f, encode_packed_##lower, &attr); \
  11912. } else { \
  11913. upb_handlers_set##upbtype(h, f, encode_scalar_##lower, &attr); \
  11914. } \
  11915. break;
  11916. switch (upb_fielddef_descriptortype(f)) {
  11917. T(DOUBLE, double, double);
  11918. T(FLOAT, float, float);
  11919. T(INT64, int64, int64);
  11920. T(INT32, int32, int32);
  11921. T(FIXED64, fixed64, uint64);
  11922. T(FIXED32, fixed32, uint32);
  11923. T(BOOL, bool, bool);
  11924. T(UINT32, uint32, uint32);
  11925. T(UINT64, uint64, uint64);
  11926. T(ENUM, enum, int32);
  11927. T(SFIXED32, sfixed32, int32);
  11928. T(SFIXED64, sfixed64, int64);
  11929. T(SINT32, sint32, int32);
  11930. T(SINT64, sint64, int64);
  11931. case UPB_DESCRIPTOR_TYPE_STRING:
  11932. case UPB_DESCRIPTOR_TYPE_BYTES:
  11933. upb_handlers_setstartstr(h, f, encode_startstr, &attr);
  11934. upb_handlers_setendstr(h, f, encode_enddelimfield, &attr);
  11935. upb_handlers_setstring(h, f, encode_strbuf, &attr);
  11936. break;
  11937. case UPB_DESCRIPTOR_TYPE_MESSAGE:
  11938. upb_handlers_setstartsubmsg(h, f, encode_startdelimfield, &attr);
  11939. upb_handlers_setendsubmsg(h, f, encode_enddelimfield, &attr);
  11940. break;
  11941. case UPB_DESCRIPTOR_TYPE_GROUP: {
  11942. /* Endgroup takes a different tag (wire_type = END_GROUP). */
  11943. upb_handlerattr attr2;
  11944. new_tag(h, f, UPB_WIRE_TYPE_END_GROUP, &attr2);
  11945. upb_handlers_setstartsubmsg(h, f, encode_startgroup, &attr);
  11946. upb_handlers_setendsubmsg(h, f, encode_endgroup, &attr2);
  11947. upb_handlerattr_uninit(&attr2);
  11948. break;
  11949. }
  11950. }
  11951. #undef T
  11952. upb_handlerattr_uninit(&attr);
  11953. }
  11954. }
  11955. void upb_pb_encoder_reset(upb_pb_encoder *e) {
  11956. e->segptr = NULL;
  11957. e->top = NULL;
  11958. e->depth = 0;
  11959. }
  11960. /* public API *****************************************************************/
  11961. const upb_handlers *upb_pb_encoder_newhandlers(const upb_msgdef *m,
  11962. const void *owner) {
  11963. return upb_handlers_newfrozen(m, owner, newhandlers_callback, NULL);
  11964. }
  11965. upb_pb_encoder *upb_pb_encoder_create(upb_env *env, const upb_handlers *h,
  11966. upb_bytessink *output) {
  11967. const size_t initial_bufsize = 256;
  11968. const size_t initial_segbufsize = 16;
  11969. /* TODO(haberman): make this configurable. */
  11970. const size_t stack_size = 64;
  11971. #ifndef NDEBUG
  11972. const size_t size_before = upb_env_bytesallocated(env);
  11973. #endif
  11974. upb_pb_encoder *e = upb_env_malloc(env, sizeof(upb_pb_encoder));
  11975. if (!e) return NULL;
  11976. e->buf = upb_env_malloc(env, initial_bufsize);
  11977. e->segbuf = upb_env_malloc(env, initial_segbufsize * sizeof(*e->segbuf));
  11978. e->stack = upb_env_malloc(env, stack_size * sizeof(*e->stack));
  11979. if (!e->buf || !e->segbuf || !e->stack) {
  11980. return NULL;
  11981. }
  11982. e->limit = e->buf + initial_bufsize;
  11983. e->seglimit = e->segbuf + initial_segbufsize;
  11984. e->stacklimit = e->stack + stack_size;
  11985. upb_pb_encoder_reset(e);
  11986. upb_sink_reset(&e->input_, h, e);
  11987. e->env = env;
  11988. e->output_ = output;
  11989. e->subc = output->closure;
  11990. e->ptr = e->buf;
  11991. /* If this fails, increase the value in encoder.h. */
  11992. UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(env) - size_before <=
  11993. UPB_PB_ENCODER_SIZE);
  11994. return e;
  11995. }
  11996. upb_sink *upb_pb_encoder_input(upb_pb_encoder *e) { return &e->input_; }
  11997. upb_filedef **upb_loaddescriptor(const char *buf, size_t n, const void *owner,
  11998. upb_status *status) {
  11999. /* Create handlers. */
  12000. const upb_pbdecodermethod *decoder_m;
  12001. const upb_handlers *reader_h = upb_descreader_newhandlers(&reader_h);
  12002. upb_env env;
  12003. upb_pbdecodermethodopts opts;
  12004. upb_pbdecoder *decoder;
  12005. upb_descreader *reader;
  12006. bool ok;
  12007. size_t i;
  12008. upb_filedef **ret = NULL;
  12009. upb_pbdecodermethodopts_init(&opts, reader_h);
  12010. decoder_m = upb_pbdecodermethod_new(&opts, &decoder_m);
  12011. upb_env_init(&env);
  12012. upb_env_reporterrorsto(&env, status);
  12013. reader = upb_descreader_create(&env, reader_h);
  12014. decoder = upb_pbdecoder_create(&env, decoder_m, upb_descreader_input(reader));
  12015. /* Push input data. */
  12016. ok = upb_bufsrc_putbuf(buf, n, upb_pbdecoder_input(decoder));
  12017. if (!ok) {
  12018. goto cleanup;
  12019. }
  12020. ret = upb_gmalloc(sizeof (*ret) * (upb_descreader_filecount(reader) + 1));
  12021. if (!ret) {
  12022. goto cleanup;
  12023. }
  12024. for (i = 0; i < upb_descreader_filecount(reader); i++) {
  12025. ret[i] = upb_descreader_file(reader, i);
  12026. upb_filedef_ref(ret[i], owner);
  12027. }
  12028. ret[i] = NULL;
  12029. cleanup:
  12030. upb_env_uninit(&env);
  12031. upb_handlers_unref(reader_h, &reader_h);
  12032. upb_pbdecodermethod_unref(decoder_m, &decoder_m);
  12033. return ret;
  12034. }
  12035. /*
  12036. * upb::pb::TextPrinter
  12037. *
  12038. * OPT: This is not optimized at all. It uses printf() which parses the format
  12039. * string every time, and it allocates memory for every put.
  12040. */
  12041. #include <ctype.h>
  12042. #include <float.h>
  12043. #include <inttypes.h>
  12044. #include <stdarg.h>
  12045. #include <stdio.h>
  12046. #include <string.h>
  12047. struct upb_textprinter {
  12048. upb_sink input_;
  12049. upb_bytessink *output_;
  12050. int indent_depth_;
  12051. bool single_line_;
  12052. void *subc;
  12053. };
  12054. #define CHECK(x) if ((x) < 0) goto err;
  12055. static const char *shortname(const char *longname) {
  12056. const char *last = strrchr(longname, '.');
  12057. return last ? last + 1 : longname;
  12058. }
  12059. static int indent(upb_textprinter *p) {
  12060. int i;
  12061. if (!p->single_line_)
  12062. for (i = 0; i < p->indent_depth_; i++)
  12063. upb_bytessink_putbuf(p->output_, p->subc, " ", 2, NULL);
  12064. return 0;
  12065. }
  12066. static int endfield(upb_textprinter *p) {
  12067. const char ch = (p->single_line_ ? ' ' : '\n');
  12068. upb_bytessink_putbuf(p->output_, p->subc, &ch, 1, NULL);
  12069. return 0;
  12070. }
  12071. static int putescaped(upb_textprinter *p, const char *buf, size_t len,
  12072. bool preserve_utf8) {
  12073. /* Based on CEscapeInternal() from Google's protobuf release. */
  12074. char dstbuf[4096], *dst = dstbuf, *dstend = dstbuf + sizeof(dstbuf);
  12075. const char *end = buf + len;
  12076. /* I think hex is prettier and more useful, but proto2 uses octal; should
  12077. * investigate whether it can parse hex also. */
  12078. const bool use_hex = false;
  12079. bool last_hex_escape = false; /* true if last output char was \xNN */
  12080. for (; buf < end; buf++) {
  12081. bool is_hex_escape;
  12082. if (dstend - dst < 4) {
  12083. upb_bytessink_putbuf(p->output_, p->subc, dstbuf, dst - dstbuf, NULL);
  12084. dst = dstbuf;
  12085. }
  12086. is_hex_escape = false;
  12087. switch (*buf) {
  12088. case '\n': *(dst++) = '\\'; *(dst++) = 'n'; break;
  12089. case '\r': *(dst++) = '\\'; *(dst++) = 'r'; break;
  12090. case '\t': *(dst++) = '\\'; *(dst++) = 't'; break;
  12091. case '\"': *(dst++) = '\\'; *(dst++) = '\"'; break;
  12092. case '\'': *(dst++) = '\\'; *(dst++) = '\''; break;
  12093. case '\\': *(dst++) = '\\'; *(dst++) = '\\'; break;
  12094. default:
  12095. /* Note that if we emit \xNN and the buf character after that is a hex
  12096. * digit then that digit must be escaped too to prevent it being
  12097. * interpreted as part of the character code by C. */
  12098. if ((!preserve_utf8 || (uint8_t)*buf < 0x80) &&
  12099. (!isprint(*buf) || (last_hex_escape && isxdigit(*buf)))) {
  12100. sprintf(dst, (use_hex ? "\\x%02x" : "\\%03o"), (uint8_t)*buf);
  12101. is_hex_escape = use_hex;
  12102. dst += 4;
  12103. } else {
  12104. *(dst++) = *buf; break;
  12105. }
  12106. }
  12107. last_hex_escape = is_hex_escape;
  12108. }
  12109. /* Flush remaining data. */
  12110. upb_bytessink_putbuf(p->output_, p->subc, dstbuf, dst - dstbuf, NULL);
  12111. return 0;
  12112. }
  12113. bool putf(upb_textprinter *p, const char *fmt, ...) {
  12114. va_list args;
  12115. va_list args_copy;
  12116. char *str;
  12117. int written;
  12118. int len;
  12119. bool ok;
  12120. va_start(args, fmt);
  12121. /* Run once to get the length of the string. */
  12122. _upb_va_copy(args_copy, args);
  12123. len = _upb_vsnprintf(NULL, 0, fmt, args_copy);
  12124. va_end(args_copy);
  12125. /* + 1 for NULL terminator (vsprintf() requires it even if we don't). */
  12126. str = upb_gmalloc(len + 1);
  12127. if (!str) return false;
  12128. written = vsprintf(str, fmt, args);
  12129. va_end(args);
  12130. UPB_ASSERT(written == len);
  12131. ok = upb_bytessink_putbuf(p->output_, p->subc, str, len, NULL);
  12132. upb_gfree(str);
  12133. return ok;
  12134. }
  12135. /* handlers *******************************************************************/
  12136. static bool textprinter_startmsg(void *c, const void *hd) {
  12137. upb_textprinter *p = c;
  12138. UPB_UNUSED(hd);
  12139. if (p->indent_depth_ == 0) {
  12140. upb_bytessink_start(p->output_, 0, &p->subc);
  12141. }
  12142. return true;
  12143. }
  12144. static bool textprinter_endmsg(void *c, const void *hd, upb_status *s) {
  12145. upb_textprinter *p = c;
  12146. UPB_UNUSED(hd);
  12147. UPB_UNUSED(s);
  12148. if (p->indent_depth_ == 0) {
  12149. upb_bytessink_end(p->output_);
  12150. }
  12151. return true;
  12152. }
  12153. #define TYPE(name, ctype, fmt) \
  12154. static bool textprinter_put ## name(void *closure, const void *handler_data, \
  12155. ctype val) { \
  12156. upb_textprinter *p = closure; \
  12157. const upb_fielddef *f = handler_data; \
  12158. CHECK(indent(p)); \
  12159. putf(p, "%s: " fmt, upb_fielddef_name(f), val); \
  12160. CHECK(endfield(p)); \
  12161. return true; \
  12162. err: \
  12163. return false; \
  12164. }
  12165. static bool textprinter_putbool(void *closure, const void *handler_data,
  12166. bool val) {
  12167. upb_textprinter *p = closure;
  12168. const upb_fielddef *f = handler_data;
  12169. CHECK(indent(p));
  12170. putf(p, "%s: %s", upb_fielddef_name(f), val ? "true" : "false");
  12171. CHECK(endfield(p));
  12172. return true;
  12173. err:
  12174. return false;
  12175. }
  12176. #define STRINGIFY_HELPER(x) #x
  12177. #define STRINGIFY_MACROVAL(x) STRINGIFY_HELPER(x)
  12178. TYPE(int32, int32_t, "%" PRId32)
  12179. TYPE(int64, int64_t, "%" PRId64)
  12180. TYPE(uint32, uint32_t, "%" PRIu32)
  12181. TYPE(uint64, uint64_t, "%" PRIu64)
  12182. TYPE(float, float, "%." STRINGIFY_MACROVAL(FLT_DIG) "g")
  12183. TYPE(double, double, "%." STRINGIFY_MACROVAL(DBL_DIG) "g")
  12184. #undef TYPE
  12185. /* Output a symbolic value from the enum if found, else just print as int32. */
  12186. static bool textprinter_putenum(void *closure, const void *handler_data,
  12187. int32_t val) {
  12188. upb_textprinter *p = closure;
  12189. const upb_fielddef *f = handler_data;
  12190. const upb_enumdef *enum_def = upb_downcast_enumdef(upb_fielddef_subdef(f));
  12191. const char *label = upb_enumdef_iton(enum_def, val);
  12192. if (label) {
  12193. indent(p);
  12194. putf(p, "%s: %s", upb_fielddef_name(f), label);
  12195. endfield(p);
  12196. } else {
  12197. if (!textprinter_putint32(closure, handler_data, val))
  12198. return false;
  12199. }
  12200. return true;
  12201. }
  12202. static void *textprinter_startstr(void *closure, const void *handler_data,
  12203. size_t size_hint) {
  12204. upb_textprinter *p = closure;
  12205. const upb_fielddef *f = handler_data;
  12206. UPB_UNUSED(size_hint);
  12207. indent(p);
  12208. putf(p, "%s: \"", upb_fielddef_name(f));
  12209. return p;
  12210. }
  12211. static bool textprinter_endstr(void *closure, const void *handler_data) {
  12212. upb_textprinter *p = closure;
  12213. UPB_UNUSED(handler_data);
  12214. putf(p, "\"");
  12215. endfield(p);
  12216. return true;
  12217. }
  12218. static size_t textprinter_putstr(void *closure, const void *hd, const char *buf,
  12219. size_t len, const upb_bufhandle *handle) {
  12220. upb_textprinter *p = closure;
  12221. const upb_fielddef *f = hd;
  12222. UPB_UNUSED(handle);
  12223. CHECK(putescaped(p, buf, len, upb_fielddef_type(f) == UPB_TYPE_STRING));
  12224. return len;
  12225. err:
  12226. return 0;
  12227. }
  12228. static void *textprinter_startsubmsg(void *closure, const void *handler_data) {
  12229. upb_textprinter *p = closure;
  12230. const char *name = handler_data;
  12231. CHECK(indent(p));
  12232. putf(p, "%s {%c", name, p->single_line_ ? ' ' : '\n');
  12233. p->indent_depth_++;
  12234. return p;
  12235. err:
  12236. return UPB_BREAK;
  12237. }
  12238. static bool textprinter_endsubmsg(void *closure, const void *handler_data) {
  12239. upb_textprinter *p = closure;
  12240. UPB_UNUSED(handler_data);
  12241. p->indent_depth_--;
  12242. CHECK(indent(p));
  12243. upb_bytessink_putbuf(p->output_, p->subc, "}", 1, NULL);
  12244. CHECK(endfield(p));
  12245. return true;
  12246. err:
  12247. return false;
  12248. }
  12249. static void onmreg(const void *c, upb_handlers *h) {
  12250. const upb_msgdef *m = upb_handlers_msgdef(h);
  12251. upb_msg_field_iter i;
  12252. UPB_UNUSED(c);
  12253. upb_handlers_setstartmsg(h, textprinter_startmsg, NULL);
  12254. upb_handlers_setendmsg(h, textprinter_endmsg, NULL);
  12255. for(upb_msg_field_begin(&i, m);
  12256. !upb_msg_field_done(&i);
  12257. upb_msg_field_next(&i)) {
  12258. upb_fielddef *f = upb_msg_iter_field(&i);
  12259. upb_handlerattr attr = UPB_HANDLERATTR_INITIALIZER;
  12260. upb_handlerattr_sethandlerdata(&attr, f);
  12261. switch (upb_fielddef_type(f)) {
  12262. case UPB_TYPE_INT32:
  12263. upb_handlers_setint32(h, f, textprinter_putint32, &attr);
  12264. break;
  12265. case UPB_TYPE_INT64:
  12266. upb_handlers_setint64(h, f, textprinter_putint64, &attr);
  12267. break;
  12268. case UPB_TYPE_UINT32:
  12269. upb_handlers_setuint32(h, f, textprinter_putuint32, &attr);
  12270. break;
  12271. case UPB_TYPE_UINT64:
  12272. upb_handlers_setuint64(h, f, textprinter_putuint64, &attr);
  12273. break;
  12274. case UPB_TYPE_FLOAT:
  12275. upb_handlers_setfloat(h, f, textprinter_putfloat, &attr);
  12276. break;
  12277. case UPB_TYPE_DOUBLE:
  12278. upb_handlers_setdouble(h, f, textprinter_putdouble, &attr);
  12279. break;
  12280. case UPB_TYPE_BOOL:
  12281. upb_handlers_setbool(h, f, textprinter_putbool, &attr);
  12282. break;
  12283. case UPB_TYPE_STRING:
  12284. case UPB_TYPE_BYTES:
  12285. upb_handlers_setstartstr(h, f, textprinter_startstr, &attr);
  12286. upb_handlers_setstring(h, f, textprinter_putstr, &attr);
  12287. upb_handlers_setendstr(h, f, textprinter_endstr, &attr);
  12288. break;
  12289. case UPB_TYPE_MESSAGE: {
  12290. const char *name =
  12291. upb_fielddef_istagdelim(f)
  12292. ? shortname(upb_msgdef_fullname(upb_fielddef_msgsubdef(f)))
  12293. : upb_fielddef_name(f);
  12294. upb_handlerattr_sethandlerdata(&attr, name);
  12295. upb_handlers_setstartsubmsg(h, f, textprinter_startsubmsg, &attr);
  12296. upb_handlers_setendsubmsg(h, f, textprinter_endsubmsg, &attr);
  12297. break;
  12298. }
  12299. case UPB_TYPE_ENUM:
  12300. upb_handlers_setint32(h, f, textprinter_putenum, &attr);
  12301. break;
  12302. }
  12303. }
  12304. }
  12305. static void textprinter_reset(upb_textprinter *p, bool single_line) {
  12306. p->single_line_ = single_line;
  12307. p->indent_depth_ = 0;
  12308. }
  12309. /* Public API *****************************************************************/
  12310. upb_textprinter *upb_textprinter_create(upb_env *env, const upb_handlers *h,
  12311. upb_bytessink *output) {
  12312. upb_textprinter *p = upb_env_malloc(env, sizeof(upb_textprinter));
  12313. if (!p) return NULL;
  12314. p->output_ = output;
  12315. upb_sink_reset(&p->input_, h, p);
  12316. textprinter_reset(p, false);
  12317. return p;
  12318. }
  12319. const upb_handlers *upb_textprinter_newhandlers(const upb_msgdef *m,
  12320. const void *owner) {
  12321. return upb_handlers_newfrozen(m, owner, &onmreg, NULL);
  12322. }
  12323. upb_sink *upb_textprinter_input(upb_textprinter *p) { return &p->input_; }
  12324. void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line) {
  12325. p->single_line_ = single_line;
  12326. }
  12327. /* Index is descriptor type. */
  12328. const uint8_t upb_pb_native_wire_types[] = {
  12329. UPB_WIRE_TYPE_END_GROUP, /* ENDGROUP */
  12330. UPB_WIRE_TYPE_64BIT, /* DOUBLE */
  12331. UPB_WIRE_TYPE_32BIT, /* FLOAT */
  12332. UPB_WIRE_TYPE_VARINT, /* INT64 */
  12333. UPB_WIRE_TYPE_VARINT, /* UINT64 */
  12334. UPB_WIRE_TYPE_VARINT, /* INT32 */
  12335. UPB_WIRE_TYPE_64BIT, /* FIXED64 */
  12336. UPB_WIRE_TYPE_32BIT, /* FIXED32 */
  12337. UPB_WIRE_TYPE_VARINT, /* BOOL */
  12338. UPB_WIRE_TYPE_DELIMITED, /* STRING */
  12339. UPB_WIRE_TYPE_START_GROUP, /* GROUP */
  12340. UPB_WIRE_TYPE_DELIMITED, /* MESSAGE */
  12341. UPB_WIRE_TYPE_DELIMITED, /* BYTES */
  12342. UPB_WIRE_TYPE_VARINT, /* UINT32 */
  12343. UPB_WIRE_TYPE_VARINT, /* ENUM */
  12344. UPB_WIRE_TYPE_32BIT, /* SFIXED32 */
  12345. UPB_WIRE_TYPE_64BIT, /* SFIXED64 */
  12346. UPB_WIRE_TYPE_VARINT, /* SINT32 */
  12347. UPB_WIRE_TYPE_VARINT, /* SINT64 */
  12348. };
  12349. /* A basic branch-based decoder, uses 32-bit values to get good performance
  12350. * on 32-bit architectures (but performs well on 64-bits also).
  12351. * This scheme comes from the original Google Protobuf implementation
  12352. * (proto2). */
  12353. upb_decoderet upb_vdecode_max8_branch32(upb_decoderet r) {
  12354. upb_decoderet err = {NULL, 0};
  12355. const char *p = r.p;
  12356. uint32_t low = (uint32_t)r.val;
  12357. uint32_t high = 0;
  12358. uint32_t b;
  12359. b = *(p++); low |= (b & 0x7fU) << 14; if (!(b & 0x80)) goto done;
  12360. b = *(p++); low |= (b & 0x7fU) << 21; if (!(b & 0x80)) goto done;
  12361. b = *(p++); low |= (b & 0x7fU) << 28;
  12362. high = (b & 0x7fU) >> 4; if (!(b & 0x80)) goto done;
  12363. b = *(p++); high |= (b & 0x7fU) << 3; if (!(b & 0x80)) goto done;
  12364. b = *(p++); high |= (b & 0x7fU) << 10; if (!(b & 0x80)) goto done;
  12365. b = *(p++); high |= (b & 0x7fU) << 17; if (!(b & 0x80)) goto done;
  12366. b = *(p++); high |= (b & 0x7fU) << 24; if (!(b & 0x80)) goto done;
  12367. b = *(p++); high |= (b & 0x7fU) << 31; if (!(b & 0x80)) goto done;
  12368. return err;
  12369. done:
  12370. r.val = ((uint64_t)high << 32) | low;
  12371. r.p = p;
  12372. return r;
  12373. }
  12374. /* Like the previous, but uses 64-bit values. */
  12375. upb_decoderet upb_vdecode_max8_branch64(upb_decoderet r) {
  12376. const char *p = r.p;
  12377. uint64_t val = r.val;
  12378. uint64_t b;
  12379. upb_decoderet err = {NULL, 0};
  12380. b = *(p++); val |= (b & 0x7fU) << 14; if (!(b & 0x80)) goto done;
  12381. b = *(p++); val |= (b & 0x7fU) << 21; if (!(b & 0x80)) goto done;
  12382. b = *(p++); val |= (b & 0x7fU) << 28; if (!(b & 0x80)) goto done;
  12383. b = *(p++); val |= (b & 0x7fU) << 35; if (!(b & 0x80)) goto done;
  12384. b = *(p++); val |= (b & 0x7fU) << 42; if (!(b & 0x80)) goto done;
  12385. b = *(p++); val |= (b & 0x7fU) << 49; if (!(b & 0x80)) goto done;
  12386. b = *(p++); val |= (b & 0x7fU) << 56; if (!(b & 0x80)) goto done;
  12387. b = *(p++); val |= (b & 0x7fU) << 63; if (!(b & 0x80)) goto done;
  12388. return err;
  12389. done:
  12390. r.val = val;
  12391. r.p = p;
  12392. return r;
  12393. }
  12394. #line 1 "upb/json/parser.rl"
  12395. /*
  12396. ** upb::json::Parser (upb_json_parser)
  12397. **
  12398. ** A parser that uses the Ragel State Machine Compiler to generate
  12399. ** the finite automata.
  12400. **
  12401. ** Ragel only natively handles regular languages, but we can manually
  12402. ** program it a bit to handle context-free languages like JSON, by using
  12403. ** the "fcall" and "fret" constructs.
  12404. **
  12405. ** This parser can handle the basics, but needs several things to be fleshed
  12406. ** out:
  12407. **
  12408. ** - handling of unicode escape sequences (including high surrogate pairs).
  12409. ** - properly check and report errors for unknown fields, stack overflow,
  12410. ** improper array nesting (or lack of nesting).
  12411. ** - handling of base64 sequences with padding characters.
  12412. ** - handling of push-back (non-success returns from sink functions).
  12413. ** - handling of keys/escape-sequences/etc that span input buffers.
  12414. */
  12415. #include <errno.h>
  12416. #include <float.h>
  12417. #include <math.h>
  12418. #include <stdint.h>
  12419. #include <stdlib.h>
  12420. #include <string.h>
  12421. #define UPB_JSON_MAX_DEPTH 64
  12422. typedef struct {
  12423. upb_sink sink;
  12424. /* The current message in which we're parsing, and the field whose value we're
  12425. * expecting next. */
  12426. const upb_msgdef *m;
  12427. const upb_fielddef *f;
  12428. /* The table mapping json name to fielddef for this message. */
  12429. upb_strtable *name_table;
  12430. /* We are in a repeated-field context, ready to emit mapentries as
  12431. * submessages. This flag alters the start-of-object (open-brace) behavior to
  12432. * begin a sequence of mapentry messages rather than a single submessage. */
  12433. bool is_map;
  12434. /* We are in a map-entry message context. This flag is set when parsing the
  12435. * value field of a single map entry and indicates to all value-field parsers
  12436. * (subobjects, strings, numbers, and bools) that the map-entry submessage
  12437. * should end as soon as the value is parsed. */
  12438. bool is_mapentry;
  12439. /* If |is_map| or |is_mapentry| is true, |mapfield| refers to the parent
  12440. * message's map field that we're currently parsing. This differs from |f|
  12441. * because |f| is the field in the *current* message (i.e., the map-entry
  12442. * message itself), not the parent's field that leads to this map. */
  12443. const upb_fielddef *mapfield;
  12444. } upb_jsonparser_frame;
  12445. struct upb_json_parser {
  12446. upb_env *env;
  12447. const upb_json_parsermethod *method;
  12448. upb_bytessink input_;
  12449. /* Stack to track the JSON scopes we are in. */
  12450. upb_jsonparser_frame stack[UPB_JSON_MAX_DEPTH];
  12451. upb_jsonparser_frame *top;
  12452. upb_jsonparser_frame *limit;
  12453. upb_status status;
  12454. /* Ragel's internal parsing stack for the parsing state machine. */
  12455. int current_state;
  12456. int parser_stack[UPB_JSON_MAX_DEPTH];
  12457. int parser_top;
  12458. /* The handle for the current buffer. */
  12459. const upb_bufhandle *handle;
  12460. /* Accumulate buffer. See details in parser.rl. */
  12461. const char *accumulated;
  12462. size_t accumulated_len;
  12463. char *accumulate_buf;
  12464. size_t accumulate_buf_size;
  12465. /* Multi-part text data. See details in parser.rl. */
  12466. int multipart_state;
  12467. upb_selector_t string_selector;
  12468. /* Input capture. See details in parser.rl. */
  12469. const char *capture;
  12470. /* Intermediate result of parsing a unicode escape sequence. */
  12471. uint32_t digit;
  12472. };
  12473. struct upb_json_parsermethod {
  12474. upb_refcounted base;
  12475. upb_byteshandler input_handler_;
  12476. /* Mainly for the purposes of refcounting, so all the fielddefs we point
  12477. * to stay alive. */
  12478. const upb_msgdef *msg;
  12479. /* Keys are upb_msgdef*, values are upb_strtable (json_name -> fielddef) */
  12480. upb_inttable name_tables;
  12481. };
  12482. #define PARSER_CHECK_RETURN(x) if (!(x)) return false
  12483. /* Used to signal that a capture has been suspended. */
  12484. static char suspend_capture;
  12485. static upb_selector_t getsel_for_handlertype(upb_json_parser *p,
  12486. upb_handlertype_t type) {
  12487. upb_selector_t sel;
  12488. bool ok = upb_handlers_getselector(p->top->f, type, &sel);
  12489. UPB_ASSERT(ok);
  12490. return sel;
  12491. }
  12492. static upb_selector_t parser_getsel(upb_json_parser *p) {
  12493. return getsel_for_handlertype(
  12494. p, upb_handlers_getprimitivehandlertype(p->top->f));
  12495. }
  12496. static bool check_stack(upb_json_parser *p) {
  12497. if ((p->top + 1) == p->limit) {
  12498. upb_status_seterrmsg(&p->status, "Nesting too deep");
  12499. upb_env_reporterror(p->env, &p->status);
  12500. return false;
  12501. }
  12502. return true;
  12503. }
  12504. static void set_name_table(upb_json_parser *p, upb_jsonparser_frame *frame) {
  12505. upb_value v;
  12506. bool ok = upb_inttable_lookupptr(&p->method->name_tables, frame->m, &v);
  12507. UPB_ASSERT(ok);
  12508. frame->name_table = upb_value_getptr(v);
  12509. }
  12510. /* There are GCC/Clang built-ins for overflow checking which we could start
  12511. * using if there was any performance benefit to it. */
  12512. static bool checked_add(size_t a, size_t b, size_t *c) {
  12513. if (SIZE_MAX - a < b) return false;
  12514. *c = a + b;
  12515. return true;
  12516. }
  12517. static size_t saturating_multiply(size_t a, size_t b) {
  12518. /* size_t is unsigned, so this is defined behavior even on overflow. */
  12519. size_t ret = a * b;
  12520. if (b != 0 && ret / b != a) {
  12521. ret = SIZE_MAX;
  12522. }
  12523. return ret;
  12524. }
  12525. /* Base64 decoding ************************************************************/
  12526. /* TODO(haberman): make this streaming. */
  12527. static const signed char b64table[] = {
  12528. -1, -1, -1, -1, -1, -1, -1, -1,
  12529. -1, -1, -1, -1, -1, -1, -1, -1,
  12530. -1, -1, -1, -1, -1, -1, -1, -1,
  12531. -1, -1, -1, -1, -1, -1, -1, -1,
  12532. -1, -1, -1, -1, -1, -1, -1, -1,
  12533. -1, -1, -1, 62/*+*/, -1, -1, -1, 63/*/ */,
  12534. 52/*0*/, 53/*1*/, 54/*2*/, 55/*3*/, 56/*4*/, 57/*5*/, 58/*6*/, 59/*7*/,
  12535. 60/*8*/, 61/*9*/, -1, -1, -1, -1, -1, -1,
  12536. -1, 0/*A*/, 1/*B*/, 2/*C*/, 3/*D*/, 4/*E*/, 5/*F*/, 6/*G*/,
  12537. 07/*H*/, 8/*I*/, 9/*J*/, 10/*K*/, 11/*L*/, 12/*M*/, 13/*N*/, 14/*O*/,
  12538. 15/*P*/, 16/*Q*/, 17/*R*/, 18/*S*/, 19/*T*/, 20/*U*/, 21/*V*/, 22/*W*/,
  12539. 23/*X*/, 24/*Y*/, 25/*Z*/, -1, -1, -1, -1, -1,
  12540. -1, 26/*a*/, 27/*b*/, 28/*c*/, 29/*d*/, 30/*e*/, 31/*f*/, 32/*g*/,
  12541. 33/*h*/, 34/*i*/, 35/*j*/, 36/*k*/, 37/*l*/, 38/*m*/, 39/*n*/, 40/*o*/,
  12542. 41/*p*/, 42/*q*/, 43/*r*/, 44/*s*/, 45/*t*/, 46/*u*/, 47/*v*/, 48/*w*/,
  12543. 49/*x*/, 50/*y*/, 51/*z*/, -1, -1, -1, -1, -1,
  12544. -1, -1, -1, -1, -1, -1, -1, -1,
  12545. -1, -1, -1, -1, -1, -1, -1, -1,
  12546. -1, -1, -1, -1, -1, -1, -1, -1,
  12547. -1, -1, -1, -1, -1, -1, -1, -1,
  12548. -1, -1, -1, -1, -1, -1, -1, -1,
  12549. -1, -1, -1, -1, -1, -1, -1, -1,
  12550. -1, -1, -1, -1, -1, -1, -1, -1,
  12551. -1, -1, -1, -1, -1, -1, -1, -1,
  12552. -1, -1, -1, -1, -1, -1, -1, -1,
  12553. -1, -1, -1, -1, -1, -1, -1, -1,
  12554. -1, -1, -1, -1, -1, -1, -1, -1,
  12555. -1, -1, -1, -1, -1, -1, -1, -1,
  12556. -1, -1, -1, -1, -1, -1, -1, -1,
  12557. -1, -1, -1, -1, -1, -1, -1, -1,
  12558. -1, -1, -1, -1, -1, -1, -1, -1,
  12559. -1, -1, -1, -1, -1, -1, -1, -1
  12560. };
  12561. /* Returns the table value sign-extended to 32 bits. Knowing that the upper
  12562. * bits will be 1 for unrecognized characters makes it easier to check for
  12563. * this error condition later (see below). */
  12564. int32_t b64lookup(unsigned char ch) { return b64table[ch]; }
  12565. /* Returns true if the given character is not a valid base64 character or
  12566. * padding. */
  12567. bool nonbase64(unsigned char ch) { return b64lookup(ch) == -1 && ch != '='; }
  12568. static bool base64_push(upb_json_parser *p, upb_selector_t sel, const char *ptr,
  12569. size_t len) {
  12570. const char *limit = ptr + len;
  12571. for (; ptr < limit; ptr += 4) {
  12572. uint32_t val;
  12573. char output[3];
  12574. if (limit - ptr < 4) {
  12575. upb_status_seterrf(&p->status,
  12576. "Base64 input for bytes field not a multiple of 4: %s",
  12577. upb_fielddef_name(p->top->f));
  12578. upb_env_reporterror(p->env, &p->status);
  12579. return false;
  12580. }
  12581. val = b64lookup(ptr[0]) << 18 |
  12582. b64lookup(ptr[1]) << 12 |
  12583. b64lookup(ptr[2]) << 6 |
  12584. b64lookup(ptr[3]);
  12585. /* Test the upper bit; returns true if any of the characters returned -1. */
  12586. if (val & 0x80000000) {
  12587. goto otherchar;
  12588. }
  12589. output[0] = val >> 16;
  12590. output[1] = (val >> 8) & 0xff;
  12591. output[2] = val & 0xff;
  12592. upb_sink_putstring(&p->top->sink, sel, output, 3, NULL);
  12593. }
  12594. return true;
  12595. otherchar:
  12596. if (nonbase64(ptr[0]) || nonbase64(ptr[1]) || nonbase64(ptr[2]) ||
  12597. nonbase64(ptr[3]) ) {
  12598. upb_status_seterrf(&p->status,
  12599. "Non-base64 characters in bytes field: %s",
  12600. upb_fielddef_name(p->top->f));
  12601. upb_env_reporterror(p->env, &p->status);
  12602. return false;
  12603. } if (ptr[2] == '=') {
  12604. uint32_t val;
  12605. char output;
  12606. /* Last group contains only two input bytes, one output byte. */
  12607. if (ptr[0] == '=' || ptr[1] == '=' || ptr[3] != '=') {
  12608. goto badpadding;
  12609. }
  12610. val = b64lookup(ptr[0]) << 18 |
  12611. b64lookup(ptr[1]) << 12;
  12612. UPB_ASSERT(!(val & 0x80000000));
  12613. output = val >> 16;
  12614. upb_sink_putstring(&p->top->sink, sel, &output, 1, NULL);
  12615. return true;
  12616. } else {
  12617. uint32_t val;
  12618. char output[2];
  12619. /* Last group contains only three input bytes, two output bytes. */
  12620. if (ptr[0] == '=' || ptr[1] == '=' || ptr[2] == '=') {
  12621. goto badpadding;
  12622. }
  12623. val = b64lookup(ptr[0]) << 18 |
  12624. b64lookup(ptr[1]) << 12 |
  12625. b64lookup(ptr[2]) << 6;
  12626. output[0] = val >> 16;
  12627. output[1] = (val >> 8) & 0xff;
  12628. upb_sink_putstring(&p->top->sink, sel, output, 2, NULL);
  12629. return true;
  12630. }
  12631. badpadding:
  12632. upb_status_seterrf(&p->status,
  12633. "Incorrect base64 padding for field: %s (%.*s)",
  12634. upb_fielddef_name(p->top->f),
  12635. 4, ptr);
  12636. upb_env_reporterror(p->env, &p->status);
  12637. return false;
  12638. }
  12639. /* Accumulate buffer **********************************************************/
  12640. /* Functionality for accumulating a buffer.
  12641. *
  12642. * Some parts of the parser need an entire value as a contiguous string. For
  12643. * example, to look up a member name in a hash table, or to turn a string into
  12644. * a number, the relevant library routines need the input string to be in
  12645. * contiguous memory, even if the value spanned two or more buffers in the
  12646. * input. These routines handle that.
  12647. *
  12648. * In the common case we can just point to the input buffer to get this
  12649. * contiguous string and avoid any actual copy. So we optimistically begin
  12650. * this way. But there are a few cases where we must instead copy into a
  12651. * separate buffer:
  12652. *
  12653. * 1. The string was not contiguous in the input (it spanned buffers).
  12654. *
  12655. * 2. The string included escape sequences that need to be interpreted to get
  12656. * the true value in a contiguous buffer. */
  12657. static void assert_accumulate_empty(upb_json_parser *p) {
  12658. UPB_ASSERT(p->accumulated == NULL);
  12659. UPB_ASSERT(p->accumulated_len == 0);
  12660. }
  12661. static void accumulate_clear(upb_json_parser *p) {
  12662. p->accumulated = NULL;
  12663. p->accumulated_len = 0;
  12664. }
  12665. /* Used internally by accumulate_append(). */
  12666. static bool accumulate_realloc(upb_json_parser *p, size_t need) {
  12667. void *mem;
  12668. size_t old_size = p->accumulate_buf_size;
  12669. size_t new_size = UPB_MAX(old_size, 128);
  12670. while (new_size < need) {
  12671. new_size = saturating_multiply(new_size, 2);
  12672. }
  12673. mem = upb_env_realloc(p->env, p->accumulate_buf, old_size, new_size);
  12674. if (!mem) {
  12675. upb_status_seterrmsg(&p->status, "Out of memory allocating buffer.");
  12676. upb_env_reporterror(p->env, &p->status);
  12677. return false;
  12678. }
  12679. p->accumulate_buf = mem;
  12680. p->accumulate_buf_size = new_size;
  12681. return true;
  12682. }
  12683. /* Logically appends the given data to the append buffer.
  12684. * If "can_alias" is true, we will try to avoid actually copying, but the buffer
  12685. * must be valid until the next accumulate_append() call (if any). */
  12686. static bool accumulate_append(upb_json_parser *p, const char *buf, size_t len,
  12687. bool can_alias) {
  12688. size_t need;
  12689. if (!p->accumulated && can_alias) {
  12690. p->accumulated = buf;
  12691. p->accumulated_len = len;
  12692. return true;
  12693. }
  12694. if (!checked_add(p->accumulated_len, len, &need)) {
  12695. upb_status_seterrmsg(&p->status, "Integer overflow.");
  12696. upb_env_reporterror(p->env, &p->status);
  12697. return false;
  12698. }
  12699. if (need > p->accumulate_buf_size && !accumulate_realloc(p, need)) {
  12700. return false;
  12701. }
  12702. if (p->accumulated != p->accumulate_buf) {
  12703. memcpy(p->accumulate_buf, p->accumulated, p->accumulated_len);
  12704. p->accumulated = p->accumulate_buf;
  12705. }
  12706. memcpy(p->accumulate_buf + p->accumulated_len, buf, len);
  12707. p->accumulated_len += len;
  12708. return true;
  12709. }
  12710. /* Returns a pointer to the data accumulated since the last accumulate_clear()
  12711. * call, and writes the length to *len. This with point either to the input
  12712. * buffer or a temporary accumulate buffer. */
  12713. static const char *accumulate_getptr(upb_json_parser *p, size_t *len) {
  12714. UPB_ASSERT(p->accumulated);
  12715. *len = p->accumulated_len;
  12716. return p->accumulated;
  12717. }
  12718. /* Mult-part text data ********************************************************/
  12719. /* When we have text data in the input, it can often come in multiple segments.
  12720. * For example, there may be some raw string data followed by an escape
  12721. * sequence. The two segments are processed with different logic. Also buffer
  12722. * seams in the input can cause multiple segments.
  12723. *
  12724. * As we see segments, there are two main cases for how we want to process them:
  12725. *
  12726. * 1. we want to push the captured input directly to string handlers.
  12727. *
  12728. * 2. we need to accumulate all the parts into a contiguous buffer for further
  12729. * processing (field name lookup, string->number conversion, etc). */
  12730. /* This is the set of states for p->multipart_state. */
  12731. enum {
  12732. /* We are not currently processing multipart data. */
  12733. MULTIPART_INACTIVE = 0,
  12734. /* We are processing multipart data by accumulating it into a contiguous
  12735. * buffer. */
  12736. MULTIPART_ACCUMULATE = 1,
  12737. /* We are processing multipart data by pushing each part directly to the
  12738. * current string handlers. */
  12739. MULTIPART_PUSHEAGERLY = 2
  12740. };
  12741. /* Start a multi-part text value where we accumulate the data for processing at
  12742. * the end. */
  12743. static void multipart_startaccum(upb_json_parser *p) {
  12744. assert_accumulate_empty(p);
  12745. UPB_ASSERT(p->multipart_state == MULTIPART_INACTIVE);
  12746. p->multipart_state = MULTIPART_ACCUMULATE;
  12747. }
  12748. /* Start a multi-part text value where we immediately push text data to a string
  12749. * value with the given selector. */
  12750. static void multipart_start(upb_json_parser *p, upb_selector_t sel) {
  12751. assert_accumulate_empty(p);
  12752. UPB_ASSERT(p->multipart_state == MULTIPART_INACTIVE);
  12753. p->multipart_state = MULTIPART_PUSHEAGERLY;
  12754. p->string_selector = sel;
  12755. }
  12756. static bool multipart_text(upb_json_parser *p, const char *buf, size_t len,
  12757. bool can_alias) {
  12758. switch (p->multipart_state) {
  12759. case MULTIPART_INACTIVE:
  12760. upb_status_seterrmsg(
  12761. &p->status, "Internal error: unexpected state MULTIPART_INACTIVE");
  12762. upb_env_reporterror(p->env, &p->status);
  12763. return false;
  12764. case MULTIPART_ACCUMULATE:
  12765. if (!accumulate_append(p, buf, len, can_alias)) {
  12766. return false;
  12767. }
  12768. break;
  12769. case MULTIPART_PUSHEAGERLY: {
  12770. const upb_bufhandle *handle = can_alias ? p->handle : NULL;
  12771. upb_sink_putstring(&p->top->sink, p->string_selector, buf, len, handle);
  12772. break;
  12773. }
  12774. }
  12775. return true;
  12776. }
  12777. /* Note: this invalidates the accumulate buffer! Call only after reading its
  12778. * contents. */
  12779. static void multipart_end(upb_json_parser *p) {
  12780. UPB_ASSERT(p->multipart_state != MULTIPART_INACTIVE);
  12781. p->multipart_state = MULTIPART_INACTIVE;
  12782. accumulate_clear(p);
  12783. }
  12784. /* Input capture **************************************************************/
  12785. /* Functionality for capturing a region of the input as text. Gracefully
  12786. * handles the case where a buffer seam occurs in the middle of the captured
  12787. * region. */
  12788. static void capture_begin(upb_json_parser *p, const char *ptr) {
  12789. UPB_ASSERT(p->multipart_state != MULTIPART_INACTIVE);
  12790. UPB_ASSERT(p->capture == NULL);
  12791. p->capture = ptr;
  12792. }
  12793. static bool capture_end(upb_json_parser *p, const char *ptr) {
  12794. UPB_ASSERT(p->capture);
  12795. if (multipart_text(p, p->capture, ptr - p->capture, true)) {
  12796. p->capture = NULL;
  12797. return true;
  12798. } else {
  12799. return false;
  12800. }
  12801. }
  12802. /* This is called at the end of each input buffer (ie. when we have hit a
  12803. * buffer seam). If we are in the middle of capturing the input, this
  12804. * processes the unprocessed capture region. */
  12805. static void capture_suspend(upb_json_parser *p, const char **ptr) {
  12806. if (!p->capture) return;
  12807. if (multipart_text(p, p->capture, *ptr - p->capture, false)) {
  12808. /* We use this as a signal that we were in the middle of capturing, and
  12809. * that capturing should resume at the beginning of the next buffer.
  12810. *
  12811. * We can't use *ptr here, because we have no guarantee that this pointer
  12812. * will be valid when we resume (if the underlying memory is freed, then
  12813. * using the pointer at all, even to compare to NULL, is likely undefined
  12814. * behavior). */
  12815. p->capture = &suspend_capture;
  12816. } else {
  12817. /* Need to back up the pointer to the beginning of the capture, since
  12818. * we were not able to actually preserve it. */
  12819. *ptr = p->capture;
  12820. }
  12821. }
  12822. static void capture_resume(upb_json_parser *p, const char *ptr) {
  12823. if (p->capture) {
  12824. UPB_ASSERT(p->capture == &suspend_capture);
  12825. p->capture = ptr;
  12826. }
  12827. }
  12828. /* Callbacks from the parser **************************************************/
  12829. /* These are the functions called directly from the parser itself.
  12830. * We define these in the same order as their declarations in the parser. */
  12831. static char escape_char(char in) {
  12832. switch (in) {
  12833. case 'r': return '\r';
  12834. case 't': return '\t';
  12835. case 'n': return '\n';
  12836. case 'f': return '\f';
  12837. case 'b': return '\b';
  12838. case '/': return '/';
  12839. case '"': return '"';
  12840. case '\\': return '\\';
  12841. default:
  12842. UPB_ASSERT(0);
  12843. return 'x';
  12844. }
  12845. }
  12846. static bool escape(upb_json_parser *p, const char *ptr) {
  12847. char ch = escape_char(*ptr);
  12848. return multipart_text(p, &ch, 1, false);
  12849. }
  12850. static void start_hex(upb_json_parser *p) {
  12851. p->digit = 0;
  12852. }
  12853. static void hexdigit(upb_json_parser *p, const char *ptr) {
  12854. char ch = *ptr;
  12855. p->digit <<= 4;
  12856. if (ch >= '0' && ch <= '9') {
  12857. p->digit += (ch - '0');
  12858. } else if (ch >= 'a' && ch <= 'f') {
  12859. p->digit += ((ch - 'a') + 10);
  12860. } else {
  12861. UPB_ASSERT(ch >= 'A' && ch <= 'F');
  12862. p->digit += ((ch - 'A') + 10);
  12863. }
  12864. }
  12865. static bool end_hex(upb_json_parser *p) {
  12866. uint32_t codepoint = p->digit;
  12867. /* emit the codepoint as UTF-8. */
  12868. char utf8[3]; /* support \u0000 -- \uFFFF -- need only three bytes. */
  12869. int length = 0;
  12870. if (codepoint <= 0x7F) {
  12871. utf8[0] = codepoint;
  12872. length = 1;
  12873. } else if (codepoint <= 0x07FF) {
  12874. utf8[1] = (codepoint & 0x3F) | 0x80;
  12875. codepoint >>= 6;
  12876. utf8[0] = (codepoint & 0x1F) | 0xC0;
  12877. length = 2;
  12878. } else /* codepoint <= 0xFFFF */ {
  12879. utf8[2] = (codepoint & 0x3F) | 0x80;
  12880. codepoint >>= 6;
  12881. utf8[1] = (codepoint & 0x3F) | 0x80;
  12882. codepoint >>= 6;
  12883. utf8[0] = (codepoint & 0x0F) | 0xE0;
  12884. length = 3;
  12885. }
  12886. /* TODO(haberman): Handle high surrogates: if codepoint is a high surrogate
  12887. * we have to wait for the next escape to get the full code point). */
  12888. return multipart_text(p, utf8, length, false);
  12889. }
  12890. static void start_text(upb_json_parser *p, const char *ptr) {
  12891. capture_begin(p, ptr);
  12892. }
  12893. static bool end_text(upb_json_parser *p, const char *ptr) {
  12894. return capture_end(p, ptr);
  12895. }
  12896. static void start_number(upb_json_parser *p, const char *ptr) {
  12897. multipart_startaccum(p);
  12898. capture_begin(p, ptr);
  12899. }
  12900. static bool parse_number(upb_json_parser *p, bool is_quoted);
  12901. static bool end_number(upb_json_parser *p, const char *ptr) {
  12902. if (!capture_end(p, ptr)) {
  12903. return false;
  12904. }
  12905. return parse_number(p, false);
  12906. }
  12907. /* |buf| is NULL-terminated. |buf| itself will never include quotes;
  12908. * |is_quoted| tells us whether this text originally appeared inside quotes. */
  12909. static bool parse_number_from_buffer(upb_json_parser *p, const char *buf,
  12910. bool is_quoted) {
  12911. size_t len = strlen(buf);
  12912. const char *bufend = buf + len;
  12913. char *end;
  12914. upb_fieldtype_t type = upb_fielddef_type(p->top->f);
  12915. double val;
  12916. double dummy;
  12917. double inf = 1.0 / 0.0; /* C89 does not have an INFINITY macro. */
  12918. errno = 0;
  12919. if (len == 0 || buf[0] == ' ') {
  12920. return false;
  12921. }
  12922. /* For integer types, first try parsing with integer-specific routines.
  12923. * If these succeed, they will be more accurate for int64/uint64 than
  12924. * strtod().
  12925. */
  12926. switch (type) {
  12927. case UPB_TYPE_ENUM:
  12928. case UPB_TYPE_INT32: {
  12929. long val = strtol(buf, &end, 0);
  12930. if (errno == ERANGE || end != bufend) {
  12931. break;
  12932. } else if (val > INT32_MAX || val < INT32_MIN) {
  12933. return false;
  12934. } else {
  12935. upb_sink_putint32(&p->top->sink, parser_getsel(p), val);
  12936. return true;
  12937. }
  12938. }
  12939. case UPB_TYPE_UINT32: {
  12940. unsigned long val = strtoul(buf, &end, 0);
  12941. if (end != bufend) {
  12942. break;
  12943. } else if (val > UINT32_MAX || errno == ERANGE) {
  12944. return false;
  12945. } else {
  12946. upb_sink_putuint32(&p->top->sink, parser_getsel(p), val);
  12947. return true;
  12948. }
  12949. }
  12950. /* XXX: We can't handle [u]int64 properly on 32-bit machines because
  12951. * strto[u]ll isn't in C89. */
  12952. case UPB_TYPE_INT64: {
  12953. long val = strtol(buf, &end, 0);
  12954. if (errno == ERANGE || end != bufend) {
  12955. break;
  12956. } else {
  12957. upb_sink_putint64(&p->top->sink, parser_getsel(p), val);
  12958. return true;
  12959. }
  12960. }
  12961. case UPB_TYPE_UINT64: {
  12962. unsigned long val = strtoul(p->accumulated, &end, 0);
  12963. if (end != bufend) {
  12964. break;
  12965. } else if (errno == ERANGE) {
  12966. return false;
  12967. } else {
  12968. upb_sink_putuint64(&p->top->sink, parser_getsel(p), val);
  12969. return true;
  12970. }
  12971. }
  12972. default:
  12973. break;
  12974. }
  12975. if (type != UPB_TYPE_DOUBLE && type != UPB_TYPE_FLOAT && is_quoted) {
  12976. /* Quoted numbers for integer types are not allowed to be in double form. */
  12977. return false;
  12978. }
  12979. if (len == strlen("Infinity") && strcmp(buf, "Infinity") == 0) {
  12980. /* C89 does not have an INFINITY macro. */
  12981. val = inf;
  12982. } else if (len == strlen("-Infinity") && strcmp(buf, "-Infinity") == 0) {
  12983. val = -inf;
  12984. } else {
  12985. val = strtod(buf, &end);
  12986. if (errno == ERANGE || end != bufend) {
  12987. return false;
  12988. }
  12989. }
  12990. switch (type) {
  12991. #define CASE(capitaltype, smalltype, ctype, min, max) \
  12992. case UPB_TYPE_ ## capitaltype: { \
  12993. if (modf(val, &dummy) != 0 || val > max || val < min) { \
  12994. return false; \
  12995. } else { \
  12996. upb_sink_put ## smalltype(&p->top->sink, parser_getsel(p), \
  12997. (ctype)val); \
  12998. return true; \
  12999. } \
  13000. break; \
  13001. }
  13002. case UPB_TYPE_ENUM:
  13003. CASE(INT32, int32, int32_t, INT32_MIN, INT32_MAX);
  13004. CASE(INT64, int64, int64_t, INT64_MIN, INT64_MAX);
  13005. CASE(UINT32, uint32, uint32_t, 0, UINT32_MAX);
  13006. CASE(UINT64, uint64, uint64_t, 0, UINT64_MAX);
  13007. #undef CASE
  13008. case UPB_TYPE_DOUBLE:
  13009. upb_sink_putdouble(&p->top->sink, parser_getsel(p), val);
  13010. return true;
  13011. case UPB_TYPE_FLOAT:
  13012. if ((val > FLT_MAX || val < -FLT_MAX) && val != inf && val != -inf) {
  13013. return false;
  13014. } else {
  13015. upb_sink_putfloat(&p->top->sink, parser_getsel(p), val);
  13016. return true;
  13017. }
  13018. default:
  13019. return false;
  13020. }
  13021. }
  13022. static bool parse_number(upb_json_parser *p, bool is_quoted) {
  13023. size_t len;
  13024. const char *buf;
  13025. /* strtol() and friends unfortunately do not support specifying the length of
  13026. * the input string, so we need to force a copy into a NULL-terminated buffer. */
  13027. if (!multipart_text(p, "\0", 1, false)) {
  13028. return false;
  13029. }
  13030. buf = accumulate_getptr(p, &len);
  13031. if (parse_number_from_buffer(p, buf, is_quoted)) {
  13032. multipart_end(p);
  13033. return true;
  13034. } else {
  13035. upb_status_seterrf(&p->status, "error parsing number: %s", buf);
  13036. upb_env_reporterror(p->env, &p->status);
  13037. multipart_end(p);
  13038. return false;
  13039. }
  13040. }
  13041. static bool parser_putbool(upb_json_parser *p, bool val) {
  13042. bool ok;
  13043. if (upb_fielddef_type(p->top->f) != UPB_TYPE_BOOL) {
  13044. upb_status_seterrf(&p->status,
  13045. "Boolean value specified for non-bool field: %s",
  13046. upb_fielddef_name(p->top->f));
  13047. upb_env_reporterror(p->env, &p->status);
  13048. return false;
  13049. }
  13050. ok = upb_sink_putbool(&p->top->sink, parser_getsel(p), val);
  13051. UPB_ASSERT(ok);
  13052. return true;
  13053. }
  13054. static bool start_stringval(upb_json_parser *p) {
  13055. UPB_ASSERT(p->top->f);
  13056. if (upb_fielddef_isstring(p->top->f)) {
  13057. upb_jsonparser_frame *inner;
  13058. upb_selector_t sel;
  13059. if (!check_stack(p)) return false;
  13060. /* Start a new parser frame: parser frames correspond one-to-one with
  13061. * handler frames, and string events occur in a sub-frame. */
  13062. inner = p->top + 1;
  13063. sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR);
  13064. upb_sink_startstr(&p->top->sink, sel, 0, &inner->sink);
  13065. inner->m = p->top->m;
  13066. inner->f = p->top->f;
  13067. inner->name_table = NULL;
  13068. inner->is_map = false;
  13069. inner->is_mapentry = false;
  13070. p->top = inner;
  13071. if (upb_fielddef_type(p->top->f) == UPB_TYPE_STRING) {
  13072. /* For STRING fields we push data directly to the handlers as it is
  13073. * parsed. We don't do this yet for BYTES fields, because our base64
  13074. * decoder is not streaming.
  13075. *
  13076. * TODO(haberman): make base64 decoding streaming also. */
  13077. multipart_start(p, getsel_for_handlertype(p, UPB_HANDLER_STRING));
  13078. return true;
  13079. } else {
  13080. multipart_startaccum(p);
  13081. return true;
  13082. }
  13083. } else if (upb_fielddef_type(p->top->f) != UPB_TYPE_BOOL &&
  13084. upb_fielddef_type(p->top->f) != UPB_TYPE_MESSAGE) {
  13085. /* No need to push a frame -- numeric values in quotes remain in the
  13086. * current parser frame. These values must accmulate so we can convert
  13087. * them all at once at the end. */
  13088. multipart_startaccum(p);
  13089. return true;
  13090. } else {
  13091. upb_status_seterrf(&p->status,
  13092. "String specified for bool or submessage field: %s",
  13093. upb_fielddef_name(p->top->f));
  13094. upb_env_reporterror(p->env, &p->status);
  13095. return false;
  13096. }
  13097. }
  13098. static bool end_stringval(upb_json_parser *p) {
  13099. bool ok = true;
  13100. switch (upb_fielddef_type(p->top->f)) {
  13101. case UPB_TYPE_BYTES:
  13102. if (!base64_push(p, getsel_for_handlertype(p, UPB_HANDLER_STRING),
  13103. p->accumulated, p->accumulated_len)) {
  13104. return false;
  13105. }
  13106. /* Fall through. */
  13107. case UPB_TYPE_STRING: {
  13108. upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR);
  13109. p->top--;
  13110. upb_sink_endstr(&p->top->sink, sel);
  13111. break;
  13112. }
  13113. case UPB_TYPE_ENUM: {
  13114. /* Resolve enum symbolic name to integer value. */
  13115. const upb_enumdef *enumdef =
  13116. (const upb_enumdef*)upb_fielddef_subdef(p->top->f);
  13117. size_t len;
  13118. const char *buf = accumulate_getptr(p, &len);
  13119. int32_t int_val = 0;
  13120. ok = upb_enumdef_ntoi(enumdef, buf, len, &int_val);
  13121. if (ok) {
  13122. upb_selector_t sel = parser_getsel(p);
  13123. upb_sink_putint32(&p->top->sink, sel, int_val);
  13124. } else {
  13125. upb_status_seterrf(&p->status, "Enum value unknown: '%.*s'", len, buf);
  13126. upb_env_reporterror(p->env, &p->status);
  13127. }
  13128. break;
  13129. }
  13130. case UPB_TYPE_INT32:
  13131. case UPB_TYPE_INT64:
  13132. case UPB_TYPE_UINT32:
  13133. case UPB_TYPE_UINT64:
  13134. case UPB_TYPE_DOUBLE:
  13135. case UPB_TYPE_FLOAT:
  13136. ok = parse_number(p, true);
  13137. break;
  13138. default:
  13139. UPB_ASSERT(false);
  13140. upb_status_seterrmsg(&p->status, "Internal error in JSON decoder");
  13141. upb_env_reporterror(p->env, &p->status);
  13142. ok = false;
  13143. break;
  13144. }
  13145. multipart_end(p);
  13146. return ok;
  13147. }
  13148. static void start_member(upb_json_parser *p) {
  13149. UPB_ASSERT(!p->top->f);
  13150. multipart_startaccum(p);
  13151. }
  13152. /* Helper: invoked during parse_mapentry() to emit the mapentry message's key
  13153. * field based on the current contents of the accumulate buffer. */
  13154. static bool parse_mapentry_key(upb_json_parser *p) {
  13155. size_t len;
  13156. const char *buf = accumulate_getptr(p, &len);
  13157. /* Emit the key field. We do a bit of ad-hoc parsing here because the
  13158. * parser state machine has already decided that this is a string field
  13159. * name, and we are reinterpreting it as some arbitrary key type. In
  13160. * particular, integer and bool keys are quoted, so we need to parse the
  13161. * quoted string contents here. */
  13162. p->top->f = upb_msgdef_itof(p->top->m, UPB_MAPENTRY_KEY);
  13163. if (p->top->f == NULL) {
  13164. upb_status_seterrmsg(&p->status, "mapentry message has no key");
  13165. upb_env_reporterror(p->env, &p->status);
  13166. return false;
  13167. }
  13168. switch (upb_fielddef_type(p->top->f)) {
  13169. case UPB_TYPE_INT32:
  13170. case UPB_TYPE_INT64:
  13171. case UPB_TYPE_UINT32:
  13172. case UPB_TYPE_UINT64:
  13173. /* Invoke end_number. The accum buffer has the number's text already. */
  13174. if (!parse_number(p, true)) {
  13175. return false;
  13176. }
  13177. break;
  13178. case UPB_TYPE_BOOL:
  13179. if (len == 4 && !strncmp(buf, "true", 4)) {
  13180. if (!parser_putbool(p, true)) {
  13181. return false;
  13182. }
  13183. } else if (len == 5 && !strncmp(buf, "false", 5)) {
  13184. if (!parser_putbool(p, false)) {
  13185. return false;
  13186. }
  13187. } else {
  13188. upb_status_seterrmsg(&p->status,
  13189. "Map bool key not 'true' or 'false'");
  13190. upb_env_reporterror(p->env, &p->status);
  13191. return false;
  13192. }
  13193. multipart_end(p);
  13194. break;
  13195. case UPB_TYPE_STRING:
  13196. case UPB_TYPE_BYTES: {
  13197. upb_sink subsink;
  13198. upb_selector_t sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSTR);
  13199. upb_sink_startstr(&p->top->sink, sel, len, &subsink);
  13200. sel = getsel_for_handlertype(p, UPB_HANDLER_STRING);
  13201. upb_sink_putstring(&subsink, sel, buf, len, NULL);
  13202. sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSTR);
  13203. upb_sink_endstr(&p->top->sink, sel);
  13204. multipart_end(p);
  13205. break;
  13206. }
  13207. default:
  13208. upb_status_seterrmsg(&p->status, "Invalid field type for map key");
  13209. upb_env_reporterror(p->env, &p->status);
  13210. return false;
  13211. }
  13212. return true;
  13213. }
  13214. /* Helper: emit one map entry (as a submessage in the map field sequence). This
  13215. * is invoked from end_membername(), at the end of the map entry's key string,
  13216. * with the map key in the accumulate buffer. It parses the key from that
  13217. * buffer, emits the handler calls to start the mapentry submessage (setting up
  13218. * its subframe in the process), and sets up state in the subframe so that the
  13219. * value parser (invoked next) will emit the mapentry's value field and then
  13220. * end the mapentry message. */
  13221. static bool handle_mapentry(upb_json_parser *p) {
  13222. const upb_fielddef *mapfield;
  13223. const upb_msgdef *mapentrymsg;
  13224. upb_jsonparser_frame *inner;
  13225. upb_selector_t sel;
  13226. /* Map entry: p->top->sink is the seq frame, so we need to start a frame
  13227. * for the mapentry itself, and then set |f| in that frame so that the map
  13228. * value field is parsed, and also set a flag to end the frame after the
  13229. * map-entry value is parsed. */
  13230. if (!check_stack(p)) return false;
  13231. mapfield = p->top->mapfield;
  13232. mapentrymsg = upb_fielddef_msgsubdef(mapfield);
  13233. inner = p->top + 1;
  13234. p->top->f = mapfield;
  13235. sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSUBMSG);
  13236. upb_sink_startsubmsg(&p->top->sink, sel, &inner->sink);
  13237. inner->m = mapentrymsg;
  13238. inner->name_table = NULL;
  13239. inner->mapfield = mapfield;
  13240. inner->is_map = false;
  13241. /* Don't set this to true *yet* -- we reuse parsing handlers below to push
  13242. * the key field value to the sink, and these handlers will pop the frame
  13243. * if they see is_mapentry (when invoked by the parser state machine, they
  13244. * would have just seen the map-entry value, not key). */
  13245. inner->is_mapentry = false;
  13246. p->top = inner;
  13247. /* send STARTMSG in submsg frame. */
  13248. upb_sink_startmsg(&p->top->sink);
  13249. parse_mapentry_key(p);
  13250. /* Set up the value field to receive the map-entry value. */
  13251. p->top->f = upb_msgdef_itof(p->top->m, UPB_MAPENTRY_VALUE);
  13252. p->top->is_mapentry = true; /* set up to pop frame after value is parsed. */
  13253. p->top->mapfield = mapfield;
  13254. if (p->top->f == NULL) {
  13255. upb_status_seterrmsg(&p->status, "mapentry message has no value");
  13256. upb_env_reporterror(p->env, &p->status);
  13257. return false;
  13258. }
  13259. return true;
  13260. }
  13261. static bool end_membername(upb_json_parser *p) {
  13262. UPB_ASSERT(!p->top->f);
  13263. if (p->top->is_map) {
  13264. return handle_mapentry(p);
  13265. } else {
  13266. size_t len;
  13267. const char *buf = accumulate_getptr(p, &len);
  13268. upb_value v;
  13269. if (upb_strtable_lookup2(p->top->name_table, buf, len, &v)) {
  13270. p->top->f = upb_value_getconstptr(v);
  13271. multipart_end(p);
  13272. return true;
  13273. } else {
  13274. /* TODO(haberman): Ignore unknown fields if requested/configured to do
  13275. * so. */
  13276. upb_status_seterrf(&p->status, "No such field: %.*s\n", (int)len, buf);
  13277. upb_env_reporterror(p->env, &p->status);
  13278. return false;
  13279. }
  13280. }
  13281. }
  13282. static void end_member(upb_json_parser *p) {
  13283. /* If we just parsed a map-entry value, end that frame too. */
  13284. if (p->top->is_mapentry) {
  13285. upb_status s = UPB_STATUS_INIT;
  13286. upb_selector_t sel;
  13287. bool ok;
  13288. const upb_fielddef *mapfield;
  13289. UPB_ASSERT(p->top > p->stack);
  13290. /* send ENDMSG on submsg. */
  13291. upb_sink_endmsg(&p->top->sink, &s);
  13292. mapfield = p->top->mapfield;
  13293. /* send ENDSUBMSG in repeated-field-of-mapentries frame. */
  13294. p->top--;
  13295. ok = upb_handlers_getselector(mapfield, UPB_HANDLER_ENDSUBMSG, &sel);
  13296. UPB_ASSERT(ok);
  13297. upb_sink_endsubmsg(&p->top->sink, sel);
  13298. }
  13299. p->top->f = NULL;
  13300. }
  13301. static bool start_subobject(upb_json_parser *p) {
  13302. UPB_ASSERT(p->top->f);
  13303. if (upb_fielddef_ismap(p->top->f)) {
  13304. upb_jsonparser_frame *inner;
  13305. upb_selector_t sel;
  13306. /* Beginning of a map. Start a new parser frame in a repeated-field
  13307. * context. */
  13308. if (!check_stack(p)) return false;
  13309. inner = p->top + 1;
  13310. sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSEQ);
  13311. upb_sink_startseq(&p->top->sink, sel, &inner->sink);
  13312. inner->m = upb_fielddef_msgsubdef(p->top->f);
  13313. inner->name_table = NULL;
  13314. inner->mapfield = p->top->f;
  13315. inner->f = NULL;
  13316. inner->is_map = true;
  13317. inner->is_mapentry = false;
  13318. p->top = inner;
  13319. return true;
  13320. } else if (upb_fielddef_issubmsg(p->top->f)) {
  13321. upb_jsonparser_frame *inner;
  13322. upb_selector_t sel;
  13323. /* Beginning of a subobject. Start a new parser frame in the submsg
  13324. * context. */
  13325. if (!check_stack(p)) return false;
  13326. inner = p->top + 1;
  13327. sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSUBMSG);
  13328. upb_sink_startsubmsg(&p->top->sink, sel, &inner->sink);
  13329. inner->m = upb_fielddef_msgsubdef(p->top->f);
  13330. set_name_table(p, inner);
  13331. inner->f = NULL;
  13332. inner->is_map = false;
  13333. inner->is_mapentry = false;
  13334. p->top = inner;
  13335. return true;
  13336. } else {
  13337. upb_status_seterrf(&p->status,
  13338. "Object specified for non-message/group field: %s",
  13339. upb_fielddef_name(p->top->f));
  13340. upb_env_reporterror(p->env, &p->status);
  13341. return false;
  13342. }
  13343. }
  13344. static void end_subobject(upb_json_parser *p) {
  13345. if (p->top->is_map) {
  13346. upb_selector_t sel;
  13347. p->top--;
  13348. sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSEQ);
  13349. upb_sink_endseq(&p->top->sink, sel);
  13350. } else {
  13351. upb_selector_t sel;
  13352. p->top--;
  13353. sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSUBMSG);
  13354. upb_sink_endsubmsg(&p->top->sink, sel);
  13355. }
  13356. }
  13357. static bool start_array(upb_json_parser *p) {
  13358. upb_jsonparser_frame *inner;
  13359. upb_selector_t sel;
  13360. UPB_ASSERT(p->top->f);
  13361. if (!upb_fielddef_isseq(p->top->f)) {
  13362. upb_status_seterrf(&p->status,
  13363. "Array specified for non-repeated field: %s",
  13364. upb_fielddef_name(p->top->f));
  13365. upb_env_reporterror(p->env, &p->status);
  13366. return false;
  13367. }
  13368. if (!check_stack(p)) return false;
  13369. inner = p->top + 1;
  13370. sel = getsel_for_handlertype(p, UPB_HANDLER_STARTSEQ);
  13371. upb_sink_startseq(&p->top->sink, sel, &inner->sink);
  13372. inner->m = p->top->m;
  13373. inner->name_table = NULL;
  13374. inner->f = p->top->f;
  13375. inner->is_map = false;
  13376. inner->is_mapentry = false;
  13377. p->top = inner;
  13378. return true;
  13379. }
  13380. static void end_array(upb_json_parser *p) {
  13381. upb_selector_t sel;
  13382. UPB_ASSERT(p->top > p->stack);
  13383. p->top--;
  13384. sel = getsel_for_handlertype(p, UPB_HANDLER_ENDSEQ);
  13385. upb_sink_endseq(&p->top->sink, sel);
  13386. }
  13387. static void start_object(upb_json_parser *p) {
  13388. if (!p->top->is_map) {
  13389. upb_sink_startmsg(&p->top->sink);
  13390. }
  13391. }
  13392. static void end_object(upb_json_parser *p) {
  13393. if (!p->top->is_map) {
  13394. upb_status status;
  13395. upb_status_clear(&status);
  13396. upb_sink_endmsg(&p->top->sink, &status);
  13397. if (!upb_ok(&status)) {
  13398. upb_env_reporterror(p->env, &status);
  13399. }
  13400. }
  13401. }
  13402. #define CHECK_RETURN_TOP(x) if (!(x)) goto error
  13403. /* The actual parser **********************************************************/
  13404. /* What follows is the Ragel parser itself. The language is specified in Ragel
  13405. * and the actions call our C functions above.
  13406. *
  13407. * Ragel has an extensive set of functionality, and we use only a small part of
  13408. * it. There are many action types but we only use a few:
  13409. *
  13410. * ">" -- transition into a machine
  13411. * "%" -- transition out of a machine
  13412. * "@" -- transition into a final state of a machine.
  13413. *
  13414. * "@" transitions are tricky because a machine can transition into a final
  13415. * state repeatedly. But in some cases we know this can't happen, for example
  13416. * a string which is delimited by a final '"' can only transition into its
  13417. * final state once, when the closing '"' is seen. */
  13418. #line 1310 "upb/json/parser.rl"
  13419. #line 1222 "upb/json/parser.c"
  13420. static const char _json_actions[] = {
  13421. 0, 1, 0, 1, 2, 1, 3, 1,
  13422. 5, 1, 6, 1, 7, 1, 8, 1,
  13423. 10, 1, 12, 1, 13, 1, 14, 1,
  13424. 15, 1, 16, 1, 17, 1, 21, 1,
  13425. 25, 1, 27, 2, 3, 8, 2, 4,
  13426. 5, 2, 6, 2, 2, 6, 8, 2,
  13427. 11, 9, 2, 13, 15, 2, 14, 15,
  13428. 2, 18, 1, 2, 19, 27, 2, 20,
  13429. 9, 2, 22, 27, 2, 23, 27, 2,
  13430. 24, 27, 2, 26, 27, 3, 14, 11,
  13431. 9
  13432. };
  13433. static const unsigned char _json_key_offsets[] = {
  13434. 0, 0, 4, 9, 14, 15, 19, 24,
  13435. 29, 34, 38, 42, 45, 48, 50, 54,
  13436. 58, 60, 62, 67, 69, 71, 80, 86,
  13437. 92, 98, 104, 106, 115, 116, 116, 116,
  13438. 121, 126, 131, 132, 133, 134, 135, 135,
  13439. 136, 137, 138, 138, 139, 140, 141, 141,
  13440. 146, 151, 152, 156, 161, 166, 171, 175,
  13441. 175, 178, 178, 178
  13442. };
  13443. static const char _json_trans_keys[] = {
  13444. 32, 123, 9, 13, 32, 34, 125, 9,
  13445. 13, 32, 34, 125, 9, 13, 34, 32,
  13446. 58, 9, 13, 32, 93, 125, 9, 13,
  13447. 32, 44, 125, 9, 13, 32, 44, 125,
  13448. 9, 13, 32, 34, 9, 13, 45, 48,
  13449. 49, 57, 48, 49, 57, 46, 69, 101,
  13450. 48, 57, 69, 101, 48, 57, 43, 45,
  13451. 48, 57, 48, 57, 48, 57, 46, 69,
  13452. 101, 48, 57, 34, 92, 34, 92, 34,
  13453. 47, 92, 98, 102, 110, 114, 116, 117,
  13454. 48, 57, 65, 70, 97, 102, 48, 57,
  13455. 65, 70, 97, 102, 48, 57, 65, 70,
  13456. 97, 102, 48, 57, 65, 70, 97, 102,
  13457. 34, 92, 34, 45, 91, 102, 110, 116,
  13458. 123, 48, 57, 34, 32, 93, 125, 9,
  13459. 13, 32, 44, 93, 9, 13, 32, 93,
  13460. 125, 9, 13, 97, 108, 115, 101, 117,
  13461. 108, 108, 114, 117, 101, 32, 34, 125,
  13462. 9, 13, 32, 34, 125, 9, 13, 34,
  13463. 32, 58, 9, 13, 32, 93, 125, 9,
  13464. 13, 32, 44, 125, 9, 13, 32, 44,
  13465. 125, 9, 13, 32, 34, 9, 13, 32,
  13466. 9, 13, 0
  13467. };
  13468. static const char _json_single_lengths[] = {
  13469. 0, 2, 3, 3, 1, 2, 3, 3,
  13470. 3, 2, 2, 1, 3, 0, 2, 2,
  13471. 0, 0, 3, 2, 2, 9, 0, 0,
  13472. 0, 0, 2, 7, 1, 0, 0, 3,
  13473. 3, 3, 1, 1, 1, 1, 0, 1,
  13474. 1, 1, 0, 1, 1, 1, 0, 3,
  13475. 3, 1, 2, 3, 3, 3, 2, 0,
  13476. 1, 0, 0, 0
  13477. };
  13478. static const char _json_range_lengths[] = {
  13479. 0, 1, 1, 1, 0, 1, 1, 1,
  13480. 1, 1, 1, 1, 0, 1, 1, 1,
  13481. 1, 1, 1, 0, 0, 0, 3, 3,
  13482. 3, 3, 0, 1, 0, 0, 0, 1,
  13483. 1, 1, 0, 0, 0, 0, 0, 0,
  13484. 0, 0, 0, 0, 0, 0, 0, 1,
  13485. 1, 0, 1, 1, 1, 1, 1, 0,
  13486. 1, 0, 0, 0
  13487. };
  13488. static const short _json_index_offsets[] = {
  13489. 0, 0, 4, 9, 14, 16, 20, 25,
  13490. 30, 35, 39, 43, 46, 50, 52, 56,
  13491. 60, 62, 64, 69, 72, 75, 85, 89,
  13492. 93, 97, 101, 104, 113, 115, 116, 117,
  13493. 122, 127, 132, 134, 136, 138, 140, 141,
  13494. 143, 145, 147, 148, 150, 152, 154, 155,
  13495. 160, 165, 167, 171, 176, 181, 186, 190,
  13496. 191, 194, 195, 196
  13497. };
  13498. static const char _json_indicies[] = {
  13499. 0, 2, 0, 1, 3, 4, 5, 3,
  13500. 1, 6, 7, 8, 6, 1, 9, 1,
  13501. 10, 11, 10, 1, 11, 1, 1, 11,
  13502. 12, 13, 14, 15, 13, 1, 16, 17,
  13503. 8, 16, 1, 17, 7, 17, 1, 18,
  13504. 19, 20, 1, 19, 20, 1, 22, 23,
  13505. 23, 21, 24, 1, 23, 23, 24, 21,
  13506. 25, 25, 26, 1, 26, 1, 26, 21,
  13507. 22, 23, 23, 20, 21, 28, 29, 27,
  13508. 31, 32, 30, 33, 33, 33, 33, 33,
  13509. 33, 33, 33, 34, 1, 35, 35, 35,
  13510. 1, 36, 36, 36, 1, 37, 37, 37,
  13511. 1, 38, 38, 38, 1, 40, 41, 39,
  13512. 42, 43, 44, 45, 46, 47, 48, 43,
  13513. 1, 49, 1, 50, 51, 53, 54, 1,
  13514. 53, 52, 55, 56, 54, 55, 1, 56,
  13515. 1, 1, 56, 52, 57, 1, 58, 1,
  13516. 59, 1, 60, 1, 61, 62, 1, 63,
  13517. 1, 64, 1, 65, 66, 1, 67, 1,
  13518. 68, 1, 69, 70, 71, 72, 70, 1,
  13519. 73, 74, 75, 73, 1, 76, 1, 77,
  13520. 78, 77, 1, 78, 1, 1, 78, 79,
  13521. 80, 81, 82, 80, 1, 83, 84, 75,
  13522. 83, 1, 84, 74, 84, 1, 85, 86,
  13523. 86, 1, 1, 1, 1, 0
  13524. };
  13525. static const char _json_trans_targs[] = {
  13526. 1, 0, 2, 3, 4, 56, 3, 4,
  13527. 56, 5, 5, 6, 7, 8, 9, 56,
  13528. 8, 9, 11, 12, 18, 57, 13, 15,
  13529. 14, 16, 17, 20, 58, 21, 20, 58,
  13530. 21, 19, 22, 23, 24, 25, 26, 20,
  13531. 58, 21, 28, 30, 31, 34, 39, 43,
  13532. 47, 29, 59, 59, 32, 31, 29, 32,
  13533. 33, 35, 36, 37, 38, 59, 40, 41,
  13534. 42, 59, 44, 45, 46, 59, 48, 49,
  13535. 55, 48, 49, 55, 50, 50, 51, 52,
  13536. 53, 54, 55, 53, 54, 59, 56
  13537. };
  13538. static const char _json_trans_actions[] = {
  13539. 0, 0, 0, 21, 77, 53, 0, 47,
  13540. 23, 17, 0, 0, 15, 19, 19, 50,
  13541. 0, 0, 0, 0, 0, 1, 0, 0,
  13542. 0, 0, 0, 3, 13, 0, 0, 35,
  13543. 5, 11, 0, 38, 7, 7, 7, 41,
  13544. 44, 9, 62, 56, 25, 0, 0, 0,
  13545. 31, 29, 33, 59, 15, 0, 27, 0,
  13546. 0, 0, 0, 0, 0, 68, 0, 0,
  13547. 0, 71, 0, 0, 0, 65, 21, 77,
  13548. 53, 0, 47, 23, 17, 0, 0, 15,
  13549. 19, 19, 50, 0, 0, 74, 0
  13550. };
  13551. static const int json_start = 1;
  13552. static const int json_en_number_machine = 10;
  13553. static const int json_en_string_machine = 19;
  13554. static const int json_en_value_machine = 27;
  13555. static const int json_en_main = 1;
  13556. #line 1313 "upb/json/parser.rl"
  13557. size_t parse(void *closure, const void *hd, const char *buf, size_t size,
  13558. const upb_bufhandle *handle) {
  13559. upb_json_parser *parser = closure;
  13560. /* Variables used by Ragel's generated code. */
  13561. int cs = parser->current_state;
  13562. int *stack = parser->parser_stack;
  13563. int top = parser->parser_top;
  13564. const char *p = buf;
  13565. const char *pe = buf + size;
  13566. parser->handle = handle;
  13567. UPB_UNUSED(hd);
  13568. UPB_UNUSED(handle);
  13569. capture_resume(parser, buf);
  13570. #line 1393 "upb/json/parser.c"
  13571. {
  13572. int _klen;
  13573. unsigned int _trans;
  13574. const char *_acts;
  13575. unsigned int _nacts;
  13576. const char *_keys;
  13577. if ( p == pe )
  13578. goto _test_eof;
  13579. if ( cs == 0 )
  13580. goto _out;
  13581. _resume:
  13582. _keys = _json_trans_keys + _json_key_offsets[cs];
  13583. _trans = _json_index_offsets[cs];
  13584. _klen = _json_single_lengths[cs];
  13585. if ( _klen > 0 ) {
  13586. const char *_lower = _keys;
  13587. const char *_mid;
  13588. const char *_upper = _keys + _klen - 1;
  13589. while (1) {
  13590. if ( _upper < _lower )
  13591. break;
  13592. _mid = _lower + ((_upper-_lower) >> 1);
  13593. if ( (*p) < *_mid )
  13594. _upper = _mid - 1;
  13595. else if ( (*p) > *_mid )
  13596. _lower = _mid + 1;
  13597. else {
  13598. _trans += (unsigned int)(_mid - _keys);
  13599. goto _match;
  13600. }
  13601. }
  13602. _keys += _klen;
  13603. _trans += _klen;
  13604. }
  13605. _klen = _json_range_lengths[cs];
  13606. if ( _klen > 0 ) {
  13607. const char *_lower = _keys;
  13608. const char *_mid;
  13609. const char *_upper = _keys + (_klen<<1) - 2;
  13610. while (1) {
  13611. if ( _upper < _lower )
  13612. break;
  13613. _mid = _lower + (((_upper-_lower) >> 1) & ~1);
  13614. if ( (*p) < _mid[0] )
  13615. _upper = _mid - 2;
  13616. else if ( (*p) > _mid[1] )
  13617. _lower = _mid + 2;
  13618. else {
  13619. _trans += (unsigned int)((_mid - _keys)>>1);
  13620. goto _match;
  13621. }
  13622. }
  13623. _trans += _klen;
  13624. }
  13625. _match:
  13626. _trans = _json_indicies[_trans];
  13627. cs = _json_trans_targs[_trans];
  13628. if ( _json_trans_actions[_trans] == 0 )
  13629. goto _again;
  13630. _acts = _json_actions + _json_trans_actions[_trans];
  13631. _nacts = (unsigned int) *_acts++;
  13632. while ( _nacts-- > 0 )
  13633. {
  13634. switch ( *_acts++ )
  13635. {
  13636. case 0:
  13637. #line 1225 "upb/json/parser.rl"
  13638. { p--; {cs = stack[--top]; goto _again;} }
  13639. break;
  13640. case 1:
  13641. #line 1226 "upb/json/parser.rl"
  13642. { p--; {stack[top++] = cs; cs = 10; goto _again;} }
  13643. break;
  13644. case 2:
  13645. #line 1230 "upb/json/parser.rl"
  13646. { start_text(parser, p); }
  13647. break;
  13648. case 3:
  13649. #line 1231 "upb/json/parser.rl"
  13650. { CHECK_RETURN_TOP(end_text(parser, p)); }
  13651. break;
  13652. case 4:
  13653. #line 1237 "upb/json/parser.rl"
  13654. { start_hex(parser); }
  13655. break;
  13656. case 5:
  13657. #line 1238 "upb/json/parser.rl"
  13658. { hexdigit(parser, p); }
  13659. break;
  13660. case 6:
  13661. #line 1239 "upb/json/parser.rl"
  13662. { CHECK_RETURN_TOP(end_hex(parser)); }
  13663. break;
  13664. case 7:
  13665. #line 1245 "upb/json/parser.rl"
  13666. { CHECK_RETURN_TOP(escape(parser, p)); }
  13667. break;
  13668. case 8:
  13669. #line 1251 "upb/json/parser.rl"
  13670. { p--; {cs = stack[--top]; goto _again;} }
  13671. break;
  13672. case 9:
  13673. #line 1254 "upb/json/parser.rl"
  13674. { {stack[top++] = cs; cs = 19; goto _again;} }
  13675. break;
  13676. case 10:
  13677. #line 1256 "upb/json/parser.rl"
  13678. { p--; {stack[top++] = cs; cs = 27; goto _again;} }
  13679. break;
  13680. case 11:
  13681. #line 1261 "upb/json/parser.rl"
  13682. { start_member(parser); }
  13683. break;
  13684. case 12:
  13685. #line 1262 "upb/json/parser.rl"
  13686. { CHECK_RETURN_TOP(end_membername(parser)); }
  13687. break;
  13688. case 13:
  13689. #line 1265 "upb/json/parser.rl"
  13690. { end_member(parser); }
  13691. break;
  13692. case 14:
  13693. #line 1271 "upb/json/parser.rl"
  13694. { start_object(parser); }
  13695. break;
  13696. case 15:
  13697. #line 1274 "upb/json/parser.rl"
  13698. { end_object(parser); }
  13699. break;
  13700. case 16:
  13701. #line 1280 "upb/json/parser.rl"
  13702. { CHECK_RETURN_TOP(start_array(parser)); }
  13703. break;
  13704. case 17:
  13705. #line 1284 "upb/json/parser.rl"
  13706. { end_array(parser); }
  13707. break;
  13708. case 18:
  13709. #line 1289 "upb/json/parser.rl"
  13710. { start_number(parser, p); }
  13711. break;
  13712. case 19:
  13713. #line 1290 "upb/json/parser.rl"
  13714. { CHECK_RETURN_TOP(end_number(parser, p)); }
  13715. break;
  13716. case 20:
  13717. #line 1292 "upb/json/parser.rl"
  13718. { CHECK_RETURN_TOP(start_stringval(parser)); }
  13719. break;
  13720. case 21:
  13721. #line 1293 "upb/json/parser.rl"
  13722. { CHECK_RETURN_TOP(end_stringval(parser)); }
  13723. break;
  13724. case 22:
  13725. #line 1295 "upb/json/parser.rl"
  13726. { CHECK_RETURN_TOP(parser_putbool(parser, true)); }
  13727. break;
  13728. case 23:
  13729. #line 1297 "upb/json/parser.rl"
  13730. { CHECK_RETURN_TOP(parser_putbool(parser, false)); }
  13731. break;
  13732. case 24:
  13733. #line 1299 "upb/json/parser.rl"
  13734. { /* null value */ }
  13735. break;
  13736. case 25:
  13737. #line 1301 "upb/json/parser.rl"
  13738. { CHECK_RETURN_TOP(start_subobject(parser)); }
  13739. break;
  13740. case 26:
  13741. #line 1302 "upb/json/parser.rl"
  13742. { end_subobject(parser); }
  13743. break;
  13744. case 27:
  13745. #line 1307 "upb/json/parser.rl"
  13746. { p--; {cs = stack[--top]; goto _again;} }
  13747. break;
  13748. #line 1579 "upb/json/parser.c"
  13749. }
  13750. }
  13751. _again:
  13752. if ( cs == 0 )
  13753. goto _out;
  13754. if ( ++p != pe )
  13755. goto _resume;
  13756. _test_eof: {}
  13757. _out: {}
  13758. }
  13759. #line 1334 "upb/json/parser.rl"
  13760. if (p != pe) {
  13761. upb_status_seterrf(&parser->status, "Parse error at '%.*s'\n", pe - p, p);
  13762. upb_env_reporterror(parser->env, &parser->status);
  13763. } else {
  13764. capture_suspend(parser, &p);
  13765. }
  13766. error:
  13767. /* Save parsing state back to parser. */
  13768. parser->current_state = cs;
  13769. parser->parser_top = top;
  13770. return p - buf;
  13771. }
  13772. bool end(void *closure, const void *hd) {
  13773. UPB_UNUSED(closure);
  13774. UPB_UNUSED(hd);
  13775. /* Prevent compile warning on unused static constants. */
  13776. UPB_UNUSED(json_start);
  13777. UPB_UNUSED(json_en_number_machine);
  13778. UPB_UNUSED(json_en_string_machine);
  13779. UPB_UNUSED(json_en_value_machine);
  13780. UPB_UNUSED(json_en_main);
  13781. return true;
  13782. }
  13783. static void json_parser_reset(upb_json_parser *p) {
  13784. int cs;
  13785. int top;
  13786. p->top = p->stack;
  13787. p->top->f = NULL;
  13788. p->top->is_map = false;
  13789. p->top->is_mapentry = false;
  13790. /* Emit Ragel initialization of the parser. */
  13791. #line 1633 "upb/json/parser.c"
  13792. {
  13793. cs = json_start;
  13794. top = 0;
  13795. }
  13796. #line 1374 "upb/json/parser.rl"
  13797. p->current_state = cs;
  13798. p->parser_top = top;
  13799. accumulate_clear(p);
  13800. p->multipart_state = MULTIPART_INACTIVE;
  13801. p->capture = NULL;
  13802. p->accumulated = NULL;
  13803. upb_status_clear(&p->status);
  13804. }
  13805. static void visit_json_parsermethod(const upb_refcounted *r,
  13806. upb_refcounted_visit *visit,
  13807. void *closure) {
  13808. const upb_json_parsermethod *method = (upb_json_parsermethod*)r;
  13809. visit(r, upb_msgdef_upcast2(method->msg), closure);
  13810. }
  13811. static void free_json_parsermethod(upb_refcounted *r) {
  13812. upb_json_parsermethod *method = (upb_json_parsermethod*)r;
  13813. upb_inttable_iter i;
  13814. upb_inttable_begin(&i, &method->name_tables);
  13815. for(; !upb_inttable_done(&i); upb_inttable_next(&i)) {
  13816. upb_value val = upb_inttable_iter_value(&i);
  13817. upb_strtable *t = upb_value_getptr(val);
  13818. upb_strtable_uninit(t);
  13819. upb_gfree(t);
  13820. }
  13821. upb_inttable_uninit(&method->name_tables);
  13822. upb_gfree(r);
  13823. }
  13824. static void add_jsonname_table(upb_json_parsermethod *m, const upb_msgdef* md) {
  13825. upb_msg_field_iter i;
  13826. upb_strtable *t;
  13827. /* It would be nice to stack-allocate this, but protobufs do not limit the
  13828. * length of fields to any reasonable limit. */
  13829. char *buf = NULL;
  13830. size_t len = 0;
  13831. if (upb_inttable_lookupptr(&m->name_tables, md, NULL)) {
  13832. return;
  13833. }
  13834. /* TODO(haberman): handle malloc failure. */
  13835. t = upb_gmalloc(sizeof(*t));
  13836. upb_strtable_init(t, UPB_CTYPE_CONSTPTR);
  13837. upb_inttable_insertptr(&m->name_tables, md, upb_value_ptr(t));
  13838. for(upb_msg_field_begin(&i, md);
  13839. !upb_msg_field_done(&i);
  13840. upb_msg_field_next(&i)) {
  13841. const upb_fielddef *f = upb_msg_iter_field(&i);
  13842. /* Add an entry for the JSON name. */
  13843. size_t field_len = upb_fielddef_getjsonname(f, buf, len);
  13844. if (field_len > len) {
  13845. size_t len2;
  13846. buf = upb_grealloc(buf, 0, field_len);
  13847. len = field_len;
  13848. len2 = upb_fielddef_getjsonname(f, buf, len);
  13849. UPB_ASSERT(len == len2);
  13850. }
  13851. upb_strtable_insert(t, buf, upb_value_constptr(f));
  13852. if (strcmp(buf, upb_fielddef_name(f)) != 0) {
  13853. /* Since the JSON name is different from the regular field name, add an
  13854. * entry for the raw name (compliant proto3 JSON parsers must accept
  13855. * both). */
  13856. upb_strtable_insert(t, upb_fielddef_name(f), upb_value_constptr(f));
  13857. }
  13858. if (upb_fielddef_issubmsg(f)) {
  13859. add_jsonname_table(m, upb_fielddef_msgsubdef(f));
  13860. }
  13861. }
  13862. upb_gfree(buf);
  13863. }
  13864. /* Public API *****************************************************************/
  13865. upb_json_parser *upb_json_parser_create(upb_env *env,
  13866. const upb_json_parsermethod *method,
  13867. upb_sink *output) {
  13868. #ifndef NDEBUG
  13869. const size_t size_before = upb_env_bytesallocated(env);
  13870. #endif
  13871. upb_json_parser *p = upb_env_malloc(env, sizeof(upb_json_parser));
  13872. if (!p) return false;
  13873. p->env = env;
  13874. p->method = method;
  13875. p->limit = p->stack + UPB_JSON_MAX_DEPTH;
  13876. p->accumulate_buf = NULL;
  13877. p->accumulate_buf_size = 0;
  13878. upb_bytessink_reset(&p->input_, &method->input_handler_, p);
  13879. json_parser_reset(p);
  13880. upb_sink_reset(&p->top->sink, output->handlers, output->closure);
  13881. p->top->m = upb_handlers_msgdef(output->handlers);
  13882. set_name_table(p, p->top);
  13883. /* If this fails, uncomment and increase the value in parser.h. */
  13884. /* fprintf(stderr, "%zd\n", upb_env_bytesallocated(env) - size_before); */
  13885. UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(env) - size_before <=
  13886. UPB_JSON_PARSER_SIZE);
  13887. return p;
  13888. }
  13889. upb_bytessink *upb_json_parser_input(upb_json_parser *p) {
  13890. return &p->input_;
  13891. }
  13892. upb_json_parsermethod *upb_json_parsermethod_new(const upb_msgdef* md,
  13893. const void* owner) {
  13894. static const struct upb_refcounted_vtbl vtbl = {visit_json_parsermethod,
  13895. free_json_parsermethod};
  13896. upb_json_parsermethod *ret = upb_gmalloc(sizeof(*ret));
  13897. upb_refcounted_init(upb_json_parsermethod_upcast_mutable(ret), &vtbl, owner);
  13898. ret->msg = md;
  13899. upb_ref2(md, ret);
  13900. upb_byteshandler_init(&ret->input_handler_);
  13901. upb_byteshandler_setstring(&ret->input_handler_, parse, ret);
  13902. upb_byteshandler_setendstr(&ret->input_handler_, end, ret);
  13903. upb_inttable_init(&ret->name_tables, UPB_CTYPE_PTR);
  13904. add_jsonname_table(ret, md);
  13905. return ret;
  13906. }
  13907. const upb_byteshandler *upb_json_parsermethod_inputhandler(
  13908. const upb_json_parsermethod *m) {
  13909. return &m->input_handler_;
  13910. }
  13911. /*
  13912. ** This currently uses snprintf() to format primitives, and could be optimized
  13913. ** further.
  13914. */
  13915. #include <string.h>
  13916. #include <stdint.h>
  13917. struct upb_json_printer {
  13918. upb_sink input_;
  13919. /* BytesSink closure. */
  13920. void *subc_;
  13921. upb_bytessink *output_;
  13922. /* We track the depth so that we know when to emit startstr/endstr on the
  13923. * output. */
  13924. int depth_;
  13925. /* Have we emitted the first element? This state is necessary to emit commas
  13926. * without leaving a trailing comma in arrays/maps. We keep this state per
  13927. * frame depth.
  13928. *
  13929. * Why max_depth * 2? UPB_MAX_HANDLER_DEPTH counts depth as nested messages.
  13930. * We count frames (contexts in which we separate elements by commas) as both
  13931. * repeated fields and messages (maps), and the worst case is a
  13932. * message->repeated field->submessage->repeated field->... nesting. */
  13933. bool first_elem_[UPB_MAX_HANDLER_DEPTH * 2];
  13934. };
  13935. /* StringPiece; a pointer plus a length. */
  13936. typedef struct {
  13937. char *ptr;
  13938. size_t len;
  13939. } strpc;
  13940. void freestrpc(void *ptr) {
  13941. strpc *pc = ptr;
  13942. upb_gfree(pc->ptr);
  13943. upb_gfree(pc);
  13944. }
  13945. /* Convert fielddef name to JSON name and return as a string piece. */
  13946. strpc *newstrpc(upb_handlers *h, const upb_fielddef *f,
  13947. bool preserve_fieldnames) {
  13948. /* TODO(haberman): handle malloc failure. */
  13949. strpc *ret = upb_gmalloc(sizeof(*ret));
  13950. if (preserve_fieldnames) {
  13951. ret->ptr = upb_gstrdup(upb_fielddef_name(f));
  13952. ret->len = strlen(ret->ptr);
  13953. } else {
  13954. size_t len;
  13955. ret->len = upb_fielddef_getjsonname(f, NULL, 0);
  13956. ret->ptr = upb_gmalloc(ret->len);
  13957. len = upb_fielddef_getjsonname(f, ret->ptr, ret->len);
  13958. UPB_ASSERT(len == ret->len);
  13959. ret->len--; /* NULL */
  13960. }
  13961. upb_handlers_addcleanup(h, ret, freestrpc);
  13962. return ret;
  13963. }
  13964. /* ------------ JSON string printing: values, maps, arrays ------------------ */
  13965. static void print_data(
  13966. upb_json_printer *p, const char *buf, unsigned int len) {
  13967. /* TODO: Will need to change if we support pushback from the sink. */
  13968. size_t n = upb_bytessink_putbuf(p->output_, p->subc_, buf, len, NULL);
  13969. UPB_ASSERT(n == len);
  13970. }
  13971. static void print_comma(upb_json_printer *p) {
  13972. if (!p->first_elem_[p->depth_]) {
  13973. print_data(p, ",", 1);
  13974. }
  13975. p->first_elem_[p->depth_] = false;
  13976. }
  13977. /* Helpers that print properly formatted elements to the JSON output stream. */
  13978. /* Used for escaping control chars in strings. */
  13979. static const char kControlCharLimit = 0x20;
  13980. UPB_INLINE bool is_json_escaped(char c) {
  13981. /* See RFC 4627. */
  13982. unsigned char uc = (unsigned char)c;
  13983. return uc < kControlCharLimit || uc == '"' || uc == '\\';
  13984. }
  13985. UPB_INLINE const char* json_nice_escape(char c) {
  13986. switch (c) {
  13987. case '"': return "\\\"";
  13988. case '\\': return "\\\\";
  13989. case '\b': return "\\b";
  13990. case '\f': return "\\f";
  13991. case '\n': return "\\n";
  13992. case '\r': return "\\r";
  13993. case '\t': return "\\t";
  13994. default: return NULL;
  13995. }
  13996. }
  13997. /* Write a properly escaped string chunk. The surrounding quotes are *not*
  13998. * printed; this is so that the caller has the option of emitting the string
  13999. * content in chunks. */
  14000. static void putstring(upb_json_printer *p, const char *buf, unsigned int len) {
  14001. const char* unescaped_run = NULL;
  14002. unsigned int i;
  14003. for (i = 0; i < len; i++) {
  14004. char c = buf[i];
  14005. /* Handle escaping. */
  14006. if (is_json_escaped(c)) {
  14007. /* Use a "nice" escape, like \n, if one exists for this character. */
  14008. const char* escape = json_nice_escape(c);
  14009. /* If we don't have a specific 'nice' escape code, use a \uXXXX-style
  14010. * escape. */
  14011. char escape_buf[8];
  14012. if (!escape) {
  14013. unsigned char byte = (unsigned char)c;
  14014. _upb_snprintf(escape_buf, sizeof(escape_buf), "\\u%04x", (int)byte);
  14015. escape = escape_buf;
  14016. }
  14017. /* N.B. that we assume that the input encoding is equal to the output
  14018. * encoding (both UTF-8 for now), so for chars >= 0x20 and != \, ", we
  14019. * can simply pass the bytes through. */
  14020. /* If there's a current run of unescaped chars, print that run first. */
  14021. if (unescaped_run) {
  14022. print_data(p, unescaped_run, &buf[i] - unescaped_run);
  14023. unescaped_run = NULL;
  14024. }
  14025. /* Then print the escape code. */
  14026. print_data(p, escape, strlen(escape));
  14027. } else {
  14028. /* Add to the current unescaped run of characters. */
  14029. if (unescaped_run == NULL) {
  14030. unescaped_run = &buf[i];
  14031. }
  14032. }
  14033. }
  14034. /* If the string ended in a run of unescaped characters, print that last run. */
  14035. if (unescaped_run) {
  14036. print_data(p, unescaped_run, &buf[len] - unescaped_run);
  14037. }
  14038. }
  14039. #define CHKLENGTH(x) if (!(x)) return -1;
  14040. /* Helpers that format floating point values according to our custom formats.
  14041. * Right now we use %.8g and %.17g for float/double, respectively, to match
  14042. * proto2::util::JsonFormat's defaults. May want to change this later. */
  14043. const char neginf[] = "\"-Infinity\"";
  14044. const char inf[] = "\"Infinity\"";
  14045. static size_t fmt_double(double val, char* buf, size_t length) {
  14046. if (val == (1.0 / 0.0)) {
  14047. CHKLENGTH(length >= strlen(inf));
  14048. strcpy(buf, inf);
  14049. return strlen(inf);
  14050. } else if (val == (-1.0 / 0.0)) {
  14051. CHKLENGTH(length >= strlen(neginf));
  14052. strcpy(buf, neginf);
  14053. return strlen(neginf);
  14054. } else {
  14055. size_t n = _upb_snprintf(buf, length, "%.17g", val);
  14056. CHKLENGTH(n > 0 && n < length);
  14057. return n;
  14058. }
  14059. }
  14060. static size_t fmt_float(float val, char* buf, size_t length) {
  14061. size_t n = _upb_snprintf(buf, length, "%.8g", val);
  14062. CHKLENGTH(n > 0 && n < length);
  14063. return n;
  14064. }
  14065. static size_t fmt_bool(bool val, char* buf, size_t length) {
  14066. size_t n = _upb_snprintf(buf, length, "%s", (val ? "true" : "false"));
  14067. CHKLENGTH(n > 0 && n < length);
  14068. return n;
  14069. }
  14070. static size_t fmt_int64(long val, char* buf, size_t length) {
  14071. size_t n = _upb_snprintf(buf, length, "%ld", val);
  14072. CHKLENGTH(n > 0 && n < length);
  14073. return n;
  14074. }
  14075. static size_t fmt_uint64(unsigned long long val, char* buf, size_t length) {
  14076. size_t n = _upb_snprintf(buf, length, "%llu", val);
  14077. CHKLENGTH(n > 0 && n < length);
  14078. return n;
  14079. }
  14080. /* Print a map key given a field name. Called by scalar field handlers and by
  14081. * startseq for repeated fields. */
  14082. static bool putkey(void *closure, const void *handler_data) {
  14083. upb_json_printer *p = closure;
  14084. const strpc *key = handler_data;
  14085. print_comma(p);
  14086. print_data(p, "\"", 1);
  14087. putstring(p, key->ptr, key->len);
  14088. print_data(p, "\":", 2);
  14089. return true;
  14090. }
  14091. #define CHKFMT(val) if ((val) == (size_t)-1) return false;
  14092. #define CHK(val) if (!(val)) return false;
  14093. #define TYPE_HANDLERS(type, fmt_func) \
  14094. static bool put##type(void *closure, const void *handler_data, type val) { \
  14095. upb_json_printer *p = closure; \
  14096. char data[64]; \
  14097. size_t length = fmt_func(val, data, sizeof(data)); \
  14098. UPB_UNUSED(handler_data); \
  14099. CHKFMT(length); \
  14100. print_data(p, data, length); \
  14101. return true; \
  14102. } \
  14103. static bool scalar_##type(void *closure, const void *handler_data, \
  14104. type val) { \
  14105. CHK(putkey(closure, handler_data)); \
  14106. CHK(put##type(closure, handler_data, val)); \
  14107. return true; \
  14108. } \
  14109. static bool repeated_##type(void *closure, const void *handler_data, \
  14110. type val) { \
  14111. upb_json_printer *p = closure; \
  14112. print_comma(p); \
  14113. CHK(put##type(closure, handler_data, val)); \
  14114. return true; \
  14115. }
  14116. #define TYPE_HANDLERS_MAPKEY(type, fmt_func) \
  14117. static bool putmapkey_##type(void *closure, const void *handler_data, \
  14118. type val) { \
  14119. upb_json_printer *p = closure; \
  14120. print_data(p, "\"", 1); \
  14121. CHK(put##type(closure, handler_data, val)); \
  14122. print_data(p, "\":", 2); \
  14123. return true; \
  14124. }
  14125. TYPE_HANDLERS(double, fmt_double)
  14126. TYPE_HANDLERS(float, fmt_float)
  14127. TYPE_HANDLERS(bool, fmt_bool)
  14128. TYPE_HANDLERS(int32_t, fmt_int64)
  14129. TYPE_HANDLERS(uint32_t, fmt_int64)
  14130. TYPE_HANDLERS(int64_t, fmt_int64)
  14131. TYPE_HANDLERS(uint64_t, fmt_uint64)
  14132. /* double and float are not allowed to be map keys. */
  14133. TYPE_HANDLERS_MAPKEY(bool, fmt_bool)
  14134. TYPE_HANDLERS_MAPKEY(int32_t, fmt_int64)
  14135. TYPE_HANDLERS_MAPKEY(uint32_t, fmt_int64)
  14136. TYPE_HANDLERS_MAPKEY(int64_t, fmt_int64)
  14137. TYPE_HANDLERS_MAPKEY(uint64_t, fmt_uint64)
  14138. #undef TYPE_HANDLERS
  14139. #undef TYPE_HANDLERS_MAPKEY
  14140. typedef struct {
  14141. void *keyname;
  14142. const upb_enumdef *enumdef;
  14143. } EnumHandlerData;
  14144. static bool scalar_enum(void *closure, const void *handler_data,
  14145. int32_t val) {
  14146. const EnumHandlerData *hd = handler_data;
  14147. upb_json_printer *p = closure;
  14148. const char *symbolic_name;
  14149. CHK(putkey(closure, hd->keyname));
  14150. symbolic_name = upb_enumdef_iton(hd->enumdef, val);
  14151. if (symbolic_name) {
  14152. print_data(p, "\"", 1);
  14153. putstring(p, symbolic_name, strlen(symbolic_name));
  14154. print_data(p, "\"", 1);
  14155. } else {
  14156. putint32_t(closure, NULL, val);
  14157. }
  14158. return true;
  14159. }
  14160. static void print_enum_symbolic_name(upb_json_printer *p,
  14161. const upb_enumdef *def,
  14162. int32_t val) {
  14163. const char *symbolic_name = upb_enumdef_iton(def, val);
  14164. if (symbolic_name) {
  14165. print_data(p, "\"", 1);
  14166. putstring(p, symbolic_name, strlen(symbolic_name));
  14167. print_data(p, "\"", 1);
  14168. } else {
  14169. putint32_t(p, NULL, val);
  14170. }
  14171. }
  14172. static bool repeated_enum(void *closure, const void *handler_data,
  14173. int32_t val) {
  14174. const EnumHandlerData *hd = handler_data;
  14175. upb_json_printer *p = closure;
  14176. print_comma(p);
  14177. print_enum_symbolic_name(p, hd->enumdef, val);
  14178. return true;
  14179. }
  14180. static bool mapvalue_enum(void *closure, const void *handler_data,
  14181. int32_t val) {
  14182. const EnumHandlerData *hd = handler_data;
  14183. upb_json_printer *p = closure;
  14184. print_enum_symbolic_name(p, hd->enumdef, val);
  14185. return true;
  14186. }
  14187. static void *scalar_startsubmsg(void *closure, const void *handler_data) {
  14188. return putkey(closure, handler_data) ? closure : UPB_BREAK;
  14189. }
  14190. static void *repeated_startsubmsg(void *closure, const void *handler_data) {
  14191. upb_json_printer *p = closure;
  14192. UPB_UNUSED(handler_data);
  14193. print_comma(p);
  14194. return closure;
  14195. }
  14196. static void start_frame(upb_json_printer *p) {
  14197. p->depth_++;
  14198. p->first_elem_[p->depth_] = true;
  14199. print_data(p, "{", 1);
  14200. }
  14201. static void end_frame(upb_json_printer *p) {
  14202. print_data(p, "}", 1);
  14203. p->depth_--;
  14204. }
  14205. static bool printer_startmsg(void *closure, const void *handler_data) {
  14206. upb_json_printer *p = closure;
  14207. UPB_UNUSED(handler_data);
  14208. if (p->depth_ == 0) {
  14209. upb_bytessink_start(p->output_, 0, &p->subc_);
  14210. }
  14211. start_frame(p);
  14212. return true;
  14213. }
  14214. static bool printer_endmsg(void *closure, const void *handler_data, upb_status *s) {
  14215. upb_json_printer *p = closure;
  14216. UPB_UNUSED(handler_data);
  14217. UPB_UNUSED(s);
  14218. end_frame(p);
  14219. if (p->depth_ == 0) {
  14220. upb_bytessink_end(p->output_);
  14221. }
  14222. return true;
  14223. }
  14224. static void *startseq(void *closure, const void *handler_data) {
  14225. upb_json_printer *p = closure;
  14226. CHK(putkey(closure, handler_data));
  14227. p->depth_++;
  14228. p->first_elem_[p->depth_] = true;
  14229. print_data(p, "[", 1);
  14230. return closure;
  14231. }
  14232. static bool endseq(void *closure, const void *handler_data) {
  14233. upb_json_printer *p = closure;
  14234. UPB_UNUSED(handler_data);
  14235. print_data(p, "]", 1);
  14236. p->depth_--;
  14237. return true;
  14238. }
  14239. static void *startmap(void *closure, const void *handler_data) {
  14240. upb_json_printer *p = closure;
  14241. CHK(putkey(closure, handler_data));
  14242. p->depth_++;
  14243. p->first_elem_[p->depth_] = true;
  14244. print_data(p, "{", 1);
  14245. return closure;
  14246. }
  14247. static bool endmap(void *closure, const void *handler_data) {
  14248. upb_json_printer *p = closure;
  14249. UPB_UNUSED(handler_data);
  14250. print_data(p, "}", 1);
  14251. p->depth_--;
  14252. return true;
  14253. }
  14254. static size_t putstr(void *closure, const void *handler_data, const char *str,
  14255. size_t len, const upb_bufhandle *handle) {
  14256. upb_json_printer *p = closure;
  14257. UPB_UNUSED(handler_data);
  14258. UPB_UNUSED(handle);
  14259. putstring(p, str, len);
  14260. return len;
  14261. }
  14262. /* This has to Base64 encode the bytes, because JSON has no "bytes" type. */
  14263. static size_t putbytes(void *closure, const void *handler_data, const char *str,
  14264. size_t len, const upb_bufhandle *handle) {
  14265. upb_json_printer *p = closure;
  14266. /* This is the regular base64, not the "web-safe" version. */
  14267. static const char base64[] =
  14268. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  14269. /* Base64-encode. */
  14270. char data[16000];
  14271. const char *limit = data + sizeof(data);
  14272. const unsigned char *from = (const unsigned char*)str;
  14273. char *to = data;
  14274. size_t remaining = len;
  14275. size_t bytes;
  14276. UPB_UNUSED(handler_data);
  14277. UPB_UNUSED(handle);
  14278. while (remaining > 2) {
  14279. /* TODO(haberman): handle encoded lengths > sizeof(data) */
  14280. UPB_ASSERT((limit - to) >= 4);
  14281. to[0] = base64[from[0] >> 2];
  14282. to[1] = base64[((from[0] & 0x3) << 4) | (from[1] >> 4)];
  14283. to[2] = base64[((from[1] & 0xf) << 2) | (from[2] >> 6)];
  14284. to[3] = base64[from[2] & 0x3f];
  14285. remaining -= 3;
  14286. to += 4;
  14287. from += 3;
  14288. }
  14289. switch (remaining) {
  14290. case 2:
  14291. to[0] = base64[from[0] >> 2];
  14292. to[1] = base64[((from[0] & 0x3) << 4) | (from[1] >> 4)];
  14293. to[2] = base64[(from[1] & 0xf) << 2];
  14294. to[3] = '=';
  14295. to += 4;
  14296. from += 2;
  14297. break;
  14298. case 1:
  14299. to[0] = base64[from[0] >> 2];
  14300. to[1] = base64[((from[0] & 0x3) << 4)];
  14301. to[2] = '=';
  14302. to[3] = '=';
  14303. to += 4;
  14304. from += 1;
  14305. break;
  14306. }
  14307. bytes = to - data;
  14308. print_data(p, "\"", 1);
  14309. putstring(p, data, bytes);
  14310. print_data(p, "\"", 1);
  14311. return len;
  14312. }
  14313. static void *scalar_startstr(void *closure, const void *handler_data,
  14314. size_t size_hint) {
  14315. upb_json_printer *p = closure;
  14316. UPB_UNUSED(handler_data);
  14317. UPB_UNUSED(size_hint);
  14318. CHK(putkey(closure, handler_data));
  14319. print_data(p, "\"", 1);
  14320. return p;
  14321. }
  14322. static size_t scalar_str(void *closure, const void *handler_data,
  14323. const char *str, size_t len,
  14324. const upb_bufhandle *handle) {
  14325. CHK(putstr(closure, handler_data, str, len, handle));
  14326. return len;
  14327. }
  14328. static bool scalar_endstr(void *closure, const void *handler_data) {
  14329. upb_json_printer *p = closure;
  14330. UPB_UNUSED(handler_data);
  14331. print_data(p, "\"", 1);
  14332. return true;
  14333. }
  14334. static void *repeated_startstr(void *closure, const void *handler_data,
  14335. size_t size_hint) {
  14336. upb_json_printer *p = closure;
  14337. UPB_UNUSED(handler_data);
  14338. UPB_UNUSED(size_hint);
  14339. print_comma(p);
  14340. print_data(p, "\"", 1);
  14341. return p;
  14342. }
  14343. static size_t repeated_str(void *closure, const void *handler_data,
  14344. const char *str, size_t len,
  14345. const upb_bufhandle *handle) {
  14346. CHK(putstr(closure, handler_data, str, len, handle));
  14347. return len;
  14348. }
  14349. static bool repeated_endstr(void *closure, const void *handler_data) {
  14350. upb_json_printer *p = closure;
  14351. UPB_UNUSED(handler_data);
  14352. print_data(p, "\"", 1);
  14353. return true;
  14354. }
  14355. static void *mapkeyval_startstr(void *closure, const void *handler_data,
  14356. size_t size_hint) {
  14357. upb_json_printer *p = closure;
  14358. UPB_UNUSED(handler_data);
  14359. UPB_UNUSED(size_hint);
  14360. print_data(p, "\"", 1);
  14361. return p;
  14362. }
  14363. static size_t mapkey_str(void *closure, const void *handler_data,
  14364. const char *str, size_t len,
  14365. const upb_bufhandle *handle) {
  14366. CHK(putstr(closure, handler_data, str, len, handle));
  14367. return len;
  14368. }
  14369. static bool mapkey_endstr(void *closure, const void *handler_data) {
  14370. upb_json_printer *p = closure;
  14371. UPB_UNUSED(handler_data);
  14372. print_data(p, "\":", 2);
  14373. return true;
  14374. }
  14375. static bool mapvalue_endstr(void *closure, const void *handler_data) {
  14376. upb_json_printer *p = closure;
  14377. UPB_UNUSED(handler_data);
  14378. print_data(p, "\"", 1);
  14379. return true;
  14380. }
  14381. static size_t scalar_bytes(void *closure, const void *handler_data,
  14382. const char *str, size_t len,
  14383. const upb_bufhandle *handle) {
  14384. CHK(putkey(closure, handler_data));
  14385. CHK(putbytes(closure, handler_data, str, len, handle));
  14386. return len;
  14387. }
  14388. static size_t repeated_bytes(void *closure, const void *handler_data,
  14389. const char *str, size_t len,
  14390. const upb_bufhandle *handle) {
  14391. upb_json_printer *p = closure;
  14392. print_comma(p);
  14393. CHK(putbytes(closure, handler_data, str, len, handle));
  14394. return len;
  14395. }
  14396. static size_t mapkey_bytes(void *closure, const void *handler_data,
  14397. const char *str, size_t len,
  14398. const upb_bufhandle *handle) {
  14399. upb_json_printer *p = closure;
  14400. CHK(putbytes(closure, handler_data, str, len, handle));
  14401. print_data(p, ":", 1);
  14402. return len;
  14403. }
  14404. static void set_enum_hd(upb_handlers *h,
  14405. const upb_fielddef *f,
  14406. bool preserve_fieldnames,
  14407. upb_handlerattr *attr) {
  14408. EnumHandlerData *hd = upb_gmalloc(sizeof(EnumHandlerData));
  14409. hd->enumdef = (const upb_enumdef *)upb_fielddef_subdef(f);
  14410. hd->keyname = newstrpc(h, f, preserve_fieldnames);
  14411. upb_handlers_addcleanup(h, hd, upb_gfree);
  14412. upb_handlerattr_sethandlerdata(attr, hd);
  14413. }
  14414. /* Set up handlers for a mapentry submessage (i.e., an individual key/value pair
  14415. * in a map).
  14416. *
  14417. * TODO: Handle missing key, missing value, out-of-order key/value, or repeated
  14418. * key or value cases properly. The right way to do this is to allocate a
  14419. * temporary structure at the start of a mapentry submessage, store key and
  14420. * value data in it as key and value handlers are called, and then print the
  14421. * key/value pair once at the end of the submessage. If we don't do this, we
  14422. * should at least detect the case and throw an error. However, so far all of
  14423. * our sources that emit mapentry messages do so canonically (with one key
  14424. * field, and then one value field), so this is not a pressing concern at the
  14425. * moment. */
  14426. void printer_sethandlers_mapentry(const void *closure, bool preserve_fieldnames,
  14427. upb_handlers *h) {
  14428. const upb_msgdef *md = upb_handlers_msgdef(h);
  14429. /* A mapentry message is printed simply as '"key": value'. Rather than
  14430. * special-case key and value for every type below, we just handle both
  14431. * fields explicitly here. */
  14432. const upb_fielddef* key_field = upb_msgdef_itof(md, UPB_MAPENTRY_KEY);
  14433. const upb_fielddef* value_field = upb_msgdef_itof(md, UPB_MAPENTRY_VALUE);
  14434. upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER;
  14435. UPB_UNUSED(closure);
  14436. switch (upb_fielddef_type(key_field)) {
  14437. case UPB_TYPE_INT32:
  14438. upb_handlers_setint32(h, key_field, putmapkey_int32_t, &empty_attr);
  14439. break;
  14440. case UPB_TYPE_INT64:
  14441. upb_handlers_setint64(h, key_field, putmapkey_int64_t, &empty_attr);
  14442. break;
  14443. case UPB_TYPE_UINT32:
  14444. upb_handlers_setuint32(h, key_field, putmapkey_uint32_t, &empty_attr);
  14445. break;
  14446. case UPB_TYPE_UINT64:
  14447. upb_handlers_setuint64(h, key_field, putmapkey_uint64_t, &empty_attr);
  14448. break;
  14449. case UPB_TYPE_BOOL:
  14450. upb_handlers_setbool(h, key_field, putmapkey_bool, &empty_attr);
  14451. break;
  14452. case UPB_TYPE_STRING:
  14453. upb_handlers_setstartstr(h, key_field, mapkeyval_startstr, &empty_attr);
  14454. upb_handlers_setstring(h, key_field, mapkey_str, &empty_attr);
  14455. upb_handlers_setendstr(h, key_field, mapkey_endstr, &empty_attr);
  14456. break;
  14457. case UPB_TYPE_BYTES:
  14458. upb_handlers_setstring(h, key_field, mapkey_bytes, &empty_attr);
  14459. break;
  14460. default:
  14461. UPB_ASSERT(false);
  14462. break;
  14463. }
  14464. switch (upb_fielddef_type(value_field)) {
  14465. case UPB_TYPE_INT32:
  14466. upb_handlers_setint32(h, value_field, putint32_t, &empty_attr);
  14467. break;
  14468. case UPB_TYPE_INT64:
  14469. upb_handlers_setint64(h, value_field, putint64_t, &empty_attr);
  14470. break;
  14471. case UPB_TYPE_UINT32:
  14472. upb_handlers_setuint32(h, value_field, putuint32_t, &empty_attr);
  14473. break;
  14474. case UPB_TYPE_UINT64:
  14475. upb_handlers_setuint64(h, value_field, putuint64_t, &empty_attr);
  14476. break;
  14477. case UPB_TYPE_BOOL:
  14478. upb_handlers_setbool(h, value_field, putbool, &empty_attr);
  14479. break;
  14480. case UPB_TYPE_FLOAT:
  14481. upb_handlers_setfloat(h, value_field, putfloat, &empty_attr);
  14482. break;
  14483. case UPB_TYPE_DOUBLE:
  14484. upb_handlers_setdouble(h, value_field, putdouble, &empty_attr);
  14485. break;
  14486. case UPB_TYPE_STRING:
  14487. upb_handlers_setstartstr(h, value_field, mapkeyval_startstr, &empty_attr);
  14488. upb_handlers_setstring(h, value_field, putstr, &empty_attr);
  14489. upb_handlers_setendstr(h, value_field, mapvalue_endstr, &empty_attr);
  14490. break;
  14491. case UPB_TYPE_BYTES:
  14492. upb_handlers_setstring(h, value_field, putbytes, &empty_attr);
  14493. break;
  14494. case UPB_TYPE_ENUM: {
  14495. upb_handlerattr enum_attr = UPB_HANDLERATTR_INITIALIZER;
  14496. set_enum_hd(h, value_field, preserve_fieldnames, &enum_attr);
  14497. upb_handlers_setint32(h, value_field, mapvalue_enum, &enum_attr);
  14498. upb_handlerattr_uninit(&enum_attr);
  14499. break;
  14500. }
  14501. case UPB_TYPE_MESSAGE:
  14502. /* No handler necessary -- the submsg handlers will print the message
  14503. * as appropriate. */
  14504. break;
  14505. }
  14506. upb_handlerattr_uninit(&empty_attr);
  14507. }
  14508. void printer_sethandlers(const void *closure, upb_handlers *h) {
  14509. const upb_msgdef *md = upb_handlers_msgdef(h);
  14510. bool is_mapentry = upb_msgdef_mapentry(md);
  14511. upb_handlerattr empty_attr = UPB_HANDLERATTR_INITIALIZER;
  14512. upb_msg_field_iter i;
  14513. const bool *preserve_fieldnames_ptr = closure;
  14514. const bool preserve_fieldnames = *preserve_fieldnames_ptr;
  14515. if (is_mapentry) {
  14516. /* mapentry messages are sufficiently different that we handle them
  14517. * separately. */
  14518. printer_sethandlers_mapentry(closure, preserve_fieldnames, h);
  14519. return;
  14520. }
  14521. upb_handlers_setstartmsg(h, printer_startmsg, &empty_attr);
  14522. upb_handlers_setendmsg(h, printer_endmsg, &empty_attr);
  14523. #define TYPE(type, name, ctype) \
  14524. case type: \
  14525. if (upb_fielddef_isseq(f)) { \
  14526. upb_handlers_set##name(h, f, repeated_##ctype, &empty_attr); \
  14527. } else { \
  14528. upb_handlers_set##name(h, f, scalar_##ctype, &name_attr); \
  14529. } \
  14530. break;
  14531. upb_msg_field_begin(&i, md);
  14532. for(; !upb_msg_field_done(&i); upb_msg_field_next(&i)) {
  14533. const upb_fielddef *f = upb_msg_iter_field(&i);
  14534. upb_handlerattr name_attr = UPB_HANDLERATTR_INITIALIZER;
  14535. upb_handlerattr_sethandlerdata(&name_attr,
  14536. newstrpc(h, f, preserve_fieldnames));
  14537. if (upb_fielddef_ismap(f)) {
  14538. upb_handlers_setstartseq(h, f, startmap, &name_attr);
  14539. upb_handlers_setendseq(h, f, endmap, &name_attr);
  14540. } else if (upb_fielddef_isseq(f)) {
  14541. upb_handlers_setstartseq(h, f, startseq, &name_attr);
  14542. upb_handlers_setendseq(h, f, endseq, &empty_attr);
  14543. }
  14544. switch (upb_fielddef_type(f)) {
  14545. TYPE(UPB_TYPE_FLOAT, float, float);
  14546. TYPE(UPB_TYPE_DOUBLE, double, double);
  14547. TYPE(UPB_TYPE_BOOL, bool, bool);
  14548. TYPE(UPB_TYPE_INT32, int32, int32_t);
  14549. TYPE(UPB_TYPE_UINT32, uint32, uint32_t);
  14550. TYPE(UPB_TYPE_INT64, int64, int64_t);
  14551. TYPE(UPB_TYPE_UINT64, uint64, uint64_t);
  14552. case UPB_TYPE_ENUM: {
  14553. /* For now, we always emit symbolic names for enums. We may want an
  14554. * option later to control this behavior, but we will wait for a real
  14555. * need first. */
  14556. upb_handlerattr enum_attr = UPB_HANDLERATTR_INITIALIZER;
  14557. set_enum_hd(h, f, preserve_fieldnames, &enum_attr);
  14558. if (upb_fielddef_isseq(f)) {
  14559. upb_handlers_setint32(h, f, repeated_enum, &enum_attr);
  14560. } else {
  14561. upb_handlers_setint32(h, f, scalar_enum, &enum_attr);
  14562. }
  14563. upb_handlerattr_uninit(&enum_attr);
  14564. break;
  14565. }
  14566. case UPB_TYPE_STRING:
  14567. if (upb_fielddef_isseq(f)) {
  14568. upb_handlers_setstartstr(h, f, repeated_startstr, &empty_attr);
  14569. upb_handlers_setstring(h, f, repeated_str, &empty_attr);
  14570. upb_handlers_setendstr(h, f, repeated_endstr, &empty_attr);
  14571. } else {
  14572. upb_handlers_setstartstr(h, f, scalar_startstr, &name_attr);
  14573. upb_handlers_setstring(h, f, scalar_str, &empty_attr);
  14574. upb_handlers_setendstr(h, f, scalar_endstr, &empty_attr);
  14575. }
  14576. break;
  14577. case UPB_TYPE_BYTES:
  14578. /* XXX: this doesn't support strings that span buffers yet. The base64
  14579. * encoder will need to be made resumable for this to work properly. */
  14580. if (upb_fielddef_isseq(f)) {
  14581. upb_handlers_setstring(h, f, repeated_bytes, &empty_attr);
  14582. } else {
  14583. upb_handlers_setstring(h, f, scalar_bytes, &name_attr);
  14584. }
  14585. break;
  14586. case UPB_TYPE_MESSAGE:
  14587. if (upb_fielddef_isseq(f)) {
  14588. upb_handlers_setstartsubmsg(h, f, repeated_startsubmsg, &name_attr);
  14589. } else {
  14590. upb_handlers_setstartsubmsg(h, f, scalar_startsubmsg, &name_attr);
  14591. }
  14592. break;
  14593. }
  14594. upb_handlerattr_uninit(&name_attr);
  14595. }
  14596. upb_handlerattr_uninit(&empty_attr);
  14597. #undef TYPE
  14598. }
  14599. static void json_printer_reset(upb_json_printer *p) {
  14600. p->depth_ = 0;
  14601. }
  14602. /* Public API *****************************************************************/
  14603. upb_json_printer *upb_json_printer_create(upb_env *e, const upb_handlers *h,
  14604. upb_bytessink *output) {
  14605. #ifndef NDEBUG
  14606. size_t size_before = upb_env_bytesallocated(e);
  14607. #endif
  14608. upb_json_printer *p = upb_env_malloc(e, sizeof(upb_json_printer));
  14609. if (!p) return NULL;
  14610. p->output_ = output;
  14611. json_printer_reset(p);
  14612. upb_sink_reset(&p->input_, h, p);
  14613. /* If this fails, increase the value in printer.h. */
  14614. UPB_ASSERT_DEBUGVAR(upb_env_bytesallocated(e) - size_before <=
  14615. UPB_JSON_PRINTER_SIZE);
  14616. return p;
  14617. }
  14618. upb_sink *upb_json_printer_input(upb_json_printer *p) {
  14619. return &p->input_;
  14620. }
  14621. const upb_handlers *upb_json_printer_newhandlers(const upb_msgdef *md,
  14622. bool preserve_fieldnames,
  14623. const void *owner) {
  14624. return upb_handlers_newfrozen(
  14625. md, owner, printer_sethandlers, &preserve_fieldnames);
  14626. }